[PATCH] w1: Detouching bug fixed.
[linux-2.6.git] / drivers / w1 / w1_int.c
index 5f0bafb..c3f67ea 100644 (file)
@@ -1,8 +1,8 @@
 /*
- *     w1_int.c
+ *     w1_int.c
  *
  * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
- * 
+ *
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -29,9 +29,9 @@
 
 static u32 w1_ids = 1;
 
-extern struct device_driver w1_driver;
+extern struct device_driver w1_master_driver;
 extern struct bus_type w1_bus_type;
-extern struct device w1_device;
+extern struct device w1_master_device;
 extern int w1_max_slave_count;
 extern int w1_max_slave_ttl;
 extern struct list_head w1_masters;
@@ -39,8 +39,9 @@ extern spinlock_t w1_mlock;
 
 extern int w1_process(void *);
 
-struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
-             struct device_driver *driver, struct device *device)
+static struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
+                                      struct device_driver *driver,
+                                      struct device *device)
 {
        struct w1_master *dev;
        int err;
@@ -60,21 +61,21 @@ struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
 
        dev->bus_master = (struct w1_bus_master *)(dev + 1);
 
-       dev->owner              = THIS_MODULE;
-       dev->max_slave_count    = slave_count;
-       dev->slave_count        = 0;
-       dev->attempts           = 0;
-       dev->kpid               = -1;
-       dev->initialized        = 0;
-       dev->id                 = id;
+       dev->owner              = THIS_MODULE;
+       dev->max_slave_count    = slave_count;
+       dev->slave_count        = 0;
+       dev->attempts           = 0;
+       dev->kpid               = -1;
+       dev->initialized        = 0;
+       dev->id                 = id;
        dev->slave_ttl          = slave_ttl;
+        dev->search_count      = -1; /* continual scan */
 
        atomic_set(&dev->refcnt, 2);
 
        INIT_LIST_HEAD(&dev->slist);
        init_MUTEX(&dev->mutex);
 
-       init_completion(&dev->dev_released);
        init_completion(&dev->dev_exited);
 
        memcpy(&dev->dev, device, sizeof(struct device));
@@ -84,19 +85,16 @@ struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
 
        dev->driver = driver;
 
-       dev->groups = 23;
+       dev->groups = 1;
        dev->seq = 1;
-       dev->nls = netlink_kernel_create(NETLINK_NFLOG, NULL);
-       if (!dev->nls) {
-               printk(KERN_ERR "Failed to create new netlink socket(%u) for w1 master %s.\n",
-                       NETLINK_NFLOG, dev->dev.bus_id);
-       }
+       dev_init_netlink(dev);
 
        err = device_register(&dev->dev);
        if (err) {
                printk(KERN_ERR "Failed to register master device. err=%d\n", err);
-               if (dev->nls && dev->nls->sk_socket)
-                       sock_release(dev->nls->sk_socket);
+
+               dev_fini_netlink(dev);
+
                memset(dev, 0, sizeof(struct w1_master));
                kfree(dev);
                dev = NULL;
@@ -108,10 +106,6 @@ struct w1_master * w1_alloc_dev(u32 id, int slave_count, int slave_ttl,
 void w1_free_dev(struct w1_master *dev)
 {
        device_unregister(&dev->dev);
-       if (dev->nls && dev->nls->sk_socket)
-               sock_release(dev->nls->sk_socket);
-       memset(dev, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
-       kfree(dev);
 }
 
 int w1_add_master_device(struct w1_bus_master *master)
@@ -120,7 +114,14 @@ int w1_add_master_device(struct w1_bus_master *master)
        int retval = 0;
        struct w1_netlink_msg msg;
 
-       dev = w1_alloc_dev(w1_ids++, w1_max_slave_count, w1_max_slave_ttl, &w1_driver, &w1_device);
+        /* validate minimum functionality */
+        if (!(master->touch_bit && master->reset_bus) &&
+            !(master->write_bit && master->read_bit)) {
+               printk(KERN_ERR "w1_add_master_device: invalid function set\n");
+               return(-EINVAL);
+        }
+
+       dev = w1_alloc_dev(w1_ids++, w1_max_slave_count, w1_max_slave_ttl, &w1_master_driver, &w1_master_device);
        if (!dev)
                return -ENOMEM;
 
@@ -153,7 +154,7 @@ int w1_add_master_device(struct w1_bus_master *master)
        return 0;
 
 err_out_kill_thread:
-       dev->need_exit = 1;
+       set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
        if (kill_proc(dev->kpid, SIGTERM, 1))
                dev_err(&dev->dev,
                         "Failed to send signal to w1 kernel thread %d.\n",
@@ -171,7 +172,7 @@ void __w1_remove_master_device(struct w1_master *dev)
        int err;
        struct w1_netlink_msg msg;
 
-       dev->need_exit = 1;
+       set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
        err = kill_proc(dev->kpid, SIGTERM, 1);
        if (err)
                dev_err(&dev->dev,
@@ -179,7 +180,7 @@ void __w1_remove_master_device(struct w1_master *dev)
                         __func__, dev->kpid);
 
        while (atomic_read(&dev->refcnt)) {
-               printk(KERN_INFO "Waiting for %s to become free: refcnt=%d.\n",
+               dev_dbg(&dev->dev, "Waiting for %s to become free: refcnt=%d.\n",
                                dev->name, atomic_read(&dev->refcnt));
 
                if (msleep_interruptible(1000))
@@ -197,10 +198,8 @@ 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 list_head *ent, *n;
 
-       list_for_each_safe(ent, n, &w1_masters) {
-               dev = list_entry(ent, struct w1_master, w1_master_entry);
+       list_for_each_entry(dev, &w1_masters, w1_master_entry) {
                if (!dev->initialized)
                        continue;
 
@@ -218,3 +217,5 @@ void w1_remove_master_device(struct w1_bus_master *bm)
 
 EXPORT_SYMBOL(w1_add_master_device);
 EXPORT_SYMBOL(w1_remove_master_device);
+
+MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_W1);