Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[firefly-linux-kernel-4.4.55.git] / net / bluetooth / 6lowpan.c
index bdb01eb3bfcc1dde325fc0d9ecfd150cde91a561..206b65ccd5b8bd318e554e452966fae1c582112f 100644 (file)
@@ -14,6 +14,7 @@
 #include <linux/if_arp.h>
 #include <linux/netdevice.h>
 #include <linux/etherdevice.h>
+#include <linux/module.h>
 #include <linux/debugfs.h>
 
 #include <net/ipv6.h>
@@ -99,6 +100,8 @@ static inline bool peer_del(struct lowpan_dev *dev, struct lowpan_peer *peer)
 {
        list_del(&peer->list);
 
+       module_put(THIS_MODULE);
+
        if (atomic_dec_and_test(&dev->peer_count)) {
                BT_DBG("last peer");
                return true;
@@ -604,6 +607,17 @@ static void ifup(struct net_device *netdev)
        rtnl_unlock();
 }
 
+static void ifdown(struct net_device *netdev)
+{
+       int err;
+
+       rtnl_lock();
+       err = dev_close(netdev);
+       if (err < 0)
+               BT_INFO("iface %s cannot be closed (%d)", netdev->name, err);
+       rtnl_unlock();
+}
+
 static void do_notify_peers(struct work_struct *work)
 {
        struct lowpan_dev *dev = container_of(work, struct lowpan_dev,
@@ -698,7 +712,7 @@ static int setup_netdev(struct l2cap_chan *chan, struct lowpan_dev **dev)
        unsigned long flags;
 
        netdev = alloc_netdev(sizeof(struct lowpan_dev), IFACE_NAME_TEMPLATE,
-                             netdev_setup);
+                             NET_NAME_UNKNOWN, netdev_setup);
        if (!netdev)
                return -ENOMEM;
 
@@ -751,6 +765,9 @@ static inline void chan_ready_cb(struct l2cap_chan *chan)
                }
        }
 
+       if (!try_module_get(THIS_MODULE))
+               return;
+
        add_peer_chan(chan, dev);
        ifup(dev->netdev);
 }
@@ -823,6 +840,8 @@ static void chan_close_cb(struct l2cap_chan *chan)
 
                cancel_delayed_work_sync(&dev->notify_peers);
 
+               ifdown(dev->netdev);
+
                if (!removed) {
                        INIT_WORK(&entry->delete_netdev, delete_netdev);
                        schedule_work(&entry->delete_netdev);
@@ -1180,6 +1199,43 @@ static const struct file_operations lowpan_control_fops = {
        .release        = single_release,
 };
 
+static void disconnect_devices(void)
+{
+       struct lowpan_dev *entry, *tmp, *new_dev;
+       struct list_head devices;
+       unsigned long flags;
+
+       INIT_LIST_HEAD(&devices);
+
+       /* We make a separate list of devices because the unregister_netdev()
+        * will call device_event() which will also want to modify the same
+        * devices list.
+        */
+
+       read_lock_irqsave(&devices_lock, flags);
+
+       list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices, list) {
+               new_dev = kmalloc(sizeof(*new_dev), GFP_ATOMIC);
+               if (!new_dev)
+                       break;
+
+               new_dev->netdev = entry->netdev;
+               INIT_LIST_HEAD(&new_dev->list);
+
+               list_add(&new_dev->list, &devices);
+       }
+
+       read_unlock_irqrestore(&devices_lock, flags);
+
+       list_for_each_entry_safe(entry, tmp, &devices, list) {
+               ifdown(entry->netdev);
+               BT_DBG("Unregistering netdev %s %p",
+                      entry->netdev->name, entry->netdev);
+               unregister_netdev(entry->netdev);
+               kfree(entry);
+       }
+}
+
 static int device_event(struct notifier_block *unused,
                        unsigned long event, void *ptr)
 {
@@ -1196,6 +1252,8 @@ static int device_event(struct notifier_block *unused,
                list_for_each_entry_safe(entry, tmp, &bt_6lowpan_devices,
                                         list) {
                        if (entry->netdev == netdev) {
+                               BT_DBG("Unregistered netdev %s %p",
+                                      netdev->name, netdev);
                                list_del(&entry->list);
                                kfree(entry);
                                break;
@@ -1212,7 +1270,7 @@ static struct notifier_block bt_6lowpan_dev_notifier = {
        .notifier_call = device_event,
 };
 
-int bt_6lowpan_init(void)
+static int __init bt_6lowpan_init(void)
 {
        lowpan_psm_debugfs = debugfs_create_file("6lowpan_psm", 0644,
                                                 bt_debugfs, NULL,
@@ -1224,7 +1282,7 @@ int bt_6lowpan_init(void)
        return register_netdevice_notifier(&bt_6lowpan_dev_notifier);
 }
 
-void bt_6lowpan_exit(void)
+static void __exit bt_6lowpan_exit(void)
 {
        debugfs_remove(lowpan_psm_debugfs);
        debugfs_remove(lowpan_control_debugfs);
@@ -1234,5 +1292,15 @@ void bt_6lowpan_exit(void)
                l2cap_chan_put(listen_chan);
        }
 
+       disconnect_devices();
+
        unregister_netdevice_notifier(&bt_6lowpan_dev_notifier);
 }
+
+module_init(bt_6lowpan_init);
+module_exit(bt_6lowpan_exit);
+
+MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
+MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
+MODULE_VERSION(VERSION);
+MODULE_LICENSE("GPL");