tunnels: fix netns vs proto registration ordering
[safe/jmp/linux-2.6] / drivers / w1 / w1_int.c
index 258defd..4a46ed5 100644 (file)
 #include "w1_netlink.h"
 #include "w1_int.h"
 
-static u32 w1_ids = 1;
+static int w1_search_count = -1; /* Default is continual scan */
+module_param_named(search_count, w1_search_count, int, 0);
+
+static int w1_enable_pullup = 1;
+module_param_named(enable_pullup, w1_enable_pullup, int, 0);
 
 static struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
                                       struct device_driver *driver,
@@ -41,7 +45,7 @@ static struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
        /*
         * We are in process context(kernel thread), so can sleep.
         */
-       dev = kmalloc(sizeof(struct w1_master) + sizeof(struct w1_bus_master), GFP_KERNEL);
+       dev = kzalloc(sizeof(struct w1_master) + sizeof(struct w1_bus_master), GFP_KERNEL);
        if (!dev) {
                printk(KERN_ERR
                        "Failed to allocate %zd bytes for new w1 device.\n",
@@ -49,7 +53,6 @@ static struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
                return NULL;
        }
 
-       memset(dev, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
 
        dev->bus_master = (struct w1_bus_master *)(dev + 1);
 
@@ -60,16 +63,19 @@ static struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
        dev->initialized        = 0;
        dev->id                 = id;
        dev->slave_ttl          = slave_ttl;
-        dev->search_count      = -1; /* continual scan */
+       dev->search_count       = w1_search_count;
+       dev->enable_pullup      = w1_enable_pullup;
 
+       /* 1 for w1_process to decrement
+        * 1 for __w1_remove_master_device to decrement
+        */
        atomic_set(&dev->refcnt, 2);
 
        INIT_LIST_HEAD(&dev->slist);
        mutex_init(&dev->mutex);
 
        memcpy(&dev->dev, device, sizeof(struct device));
-       snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id),
-                 "w1_bus_master%u", dev->id);
+       dev_set_name(&dev->dev, "w1_bus_master%u", dev->id);
        snprintf(dev->name, sizeof(dev->name), "w1_bus_master%u", dev->id);
 
        dev->driver = driver;
@@ -94,9 +100,10 @@ static void w1_free_dev(struct w1_master *dev)
 
 int w1_add_master_device(struct w1_bus_master *master)
 {
-       struct w1_master *dev;
+       struct w1_master *dev, *entry;
        int retval = 0;
        struct w1_netlink_msg msg;
+       int id, found;
 
         /* validate minimum functionality */
         if (!(master->touch_bit && master->reset_bus) &&
@@ -105,10 +112,50 @@ int w1_add_master_device(struct w1_bus_master *master)
                printk(KERN_ERR "w1_add_master_device: invalid function set\n");
                return(-EINVAL);
         }
+       /* While it would be electrically possible to make a device that
+        * generated a strong pullup in bit bang mode, only hardare that
+        * controls 1-wire time frames are even expected to support a strong
+        * pullup.  w1_io.c would need to support calling set_pullup before
+        * the last write_bit operation of a w1_write_8 which it currently
+        * doesn't.
+        */
+       if (!master->write_byte && !master->touch_bit && master->set_pullup) {
+               printk(KERN_ERR "w1_add_master_device: set_pullup requires "
+                       "write_byte or touch_bit, disabling\n");
+               master->set_pullup = NULL;
+       }
 
-       dev = w1_alloc_dev(w1_ids++, w1_max_slave_count, w1_max_slave_ttl, &w1_master_driver, &w1_master_device);
-       if (!dev)
+       /* Lock until the device is added (or not) to w1_masters. */
+       mutex_lock(&w1_mlock);
+       /* Search for the first available id (starting at 1). */
+       id = 0;
+       do {
+               ++id;
+               found = 0;
+               list_for_each_entry(entry, &w1_masters, w1_master_entry) {
+                       if (entry->id == id) {
+                               found = 1;
+                               break;
+                       }
+               }
+       } while (found);
+
+       dev = w1_alloc_dev(id, w1_max_slave_count, w1_max_slave_ttl,
+               &w1_master_driver, &w1_master_device);
+       if (!dev) {
+               mutex_unlock(&w1_mlock);
                return -ENOMEM;
+       }
+
+       retval =  w1_create_master_attributes(dev);
+       if (retval) {
+               mutex_unlock(&w1_mlock);
+               goto err_out_free_dev;
+       }
+
+       memcpy(dev->bus_master, master, sizeof(struct w1_bus_master));
+
+       dev->initialized = 1;
 
        dev->thread = kthread_run(&w1_process, dev, "%s", dev->name);
        if (IS_ERR(dev->thread)) {
@@ -116,18 +163,10 @@ int w1_add_master_device(struct w1_bus_master *master)
                dev_err(&dev->dev,
                         "Failed to create new kernel thread. err=%d\n",
                         retval);
-               goto err_out_free_dev;
+               mutex_unlock(&w1_mlock);
+               goto err_out_rm_attr;
        }
 
-       retval =  w1_create_master_attributes(dev);
-       if (retval)
-               goto err_out_kill_thread;
-
-       memcpy(dev->bus_master, master, sizeof(struct w1_bus_master));
-
-       dev->initialized = 1;
-
-       mutex_lock(&w1_mlock);
        list_add(&dev->w1_master_entry, &w1_masters);
        mutex_unlock(&w1_mlock);
 
@@ -138,8 +177,12 @@ int w1_add_master_device(struct w1_bus_master *master)
 
        return 0;
 
+#if 0 /* Thread cleanup code, not required currently. */
 err_out_kill_thread:
        kthread_stop(dev->thread);
+#endif
+err_out_rm_attr:
+       w1_destroy_master_attributes(dev);
 err_out_free_dev:
        w1_free_dev(dev);
 
@@ -149,10 +192,21 @@ err_out_free_dev:
 void __w1_remove_master_device(struct w1_master *dev)
 {
        struct w1_netlink_msg msg;
+       struct w1_slave *sl, *sln;
 
-       set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
        kthread_stop(dev->thread);
 
+       mutex_lock(&w1_mlock);
+       list_del(&dev->w1_master_entry);
+       mutex_unlock(&w1_mlock);
+
+       mutex_lock(&dev->mutex);
+       list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry)
+               w1_slave_detach(sl);
+       w1_destroy_master_attributes(dev);
+       mutex_unlock(&dev->mutex);
+       atomic_dec(&dev->refcnt);
+
        while (atomic_read(&dev->refcnt)) {
                dev_info(&dev->dev, "Waiting for %s to become free: refcnt=%d.\n",
                                dev->name, atomic_read(&dev->refcnt));
@@ -171,22 +225,24 @@ void __w1_remove_master_device(struct w1_master *dev)
 
 void w1_remove_master_device(struct w1_bus_master *bm)
 {
-       struct w1_master *dev = NULL;
+       struct w1_master *dev, *found = NULL;
 
        list_for_each_entry(dev, &w1_masters, w1_master_entry) {
                if (!dev->initialized)
                        continue;
 
-               if (dev->bus_master->data == bm->data)
+               if (dev->bus_master->data == bm->data) {
+                       found = dev;
                        break;
+               }
        }
 
-       if (!dev) {
+       if (!found) {
                printk(KERN_ERR "Device doesn't exist.\n");
                return;
        }
 
-       __w1_remove_master_device(dev);
+       __w1_remove_master_device(found);
 }
 
 EXPORT_SYMBOL(w1_add_master_device);