vserver 1.9.5.x5
[linux-2.6.git] / drivers / w1 / w1.c
1 /*
2  *      w1.c
3  *
4  * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
5  * 
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21
22 #include <asm/atomic.h>
23
24 #include <linux/delay.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/list.h>
29 #include <linux/interrupt.h>
30 #include <linux/spinlock.h>
31 #include <linux/timer.h>
32 #include <linux/device.h>
33 #include <linux/slab.h>
34 #include <linux/sched.h>
35
36 #include "w1.h"
37 #include "w1_io.h"
38 #include "w1_log.h"
39 #include "w1_int.h"
40 #include "w1_family.h"
41 #include "w1_netlink.h"
42
43 MODULE_LICENSE("GPL");
44 MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
45 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
46
47 static int w1_timeout = 10;
48 int w1_max_slave_count = 10;
49 int w1_max_slave_ttl = 10;
50
51 module_param_named(timeout, w1_timeout, int, 0);
52 module_param_named(max_slave_count, w1_max_slave_count, int, 0);
53 module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
54
55 DEFINE_SPINLOCK(w1_mlock);
56 LIST_HEAD(w1_masters);
57
58 static pid_t control_thread;
59 static int control_needs_exit;
60 static DECLARE_COMPLETION(w1_control_complete);
61 static DECLARE_WAIT_QUEUE_HEAD(w1_control_wait);
62
63 static int w1_master_match(struct device *dev, struct device_driver *drv)
64 {
65         return 1;
66 }
67
68 static int w1_master_probe(struct device *dev)
69 {
70         return -ENODEV;
71 }
72
73 static int w1_master_remove(struct device *dev)
74 {
75         return 0;
76 }
77
78 static void w1_master_release(struct device *dev)
79 {
80         struct w1_master *md = container_of(dev, struct w1_master, dev);
81
82         complete(&md->dev_released);
83 }
84
85 static void w1_slave_release(struct device *dev)
86 {
87         struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
88
89         complete(&sl->dev_released);
90 }
91
92 static ssize_t w1_default_read_name(struct device *dev, char *buf)
93 {
94         return sprintf(buf, "No family registered.\n");
95 }
96
97 static ssize_t w1_default_read_bin(struct kobject *kobj, char *buf, loff_t off,
98                      size_t count)
99 {
100         return sprintf(buf, "No family registered.\n");
101 }
102
103 struct bus_type w1_bus_type = {
104         .name = "w1",
105         .match = w1_master_match,
106 };
107
108 struct device_driver w1_driver = {
109         .name = "w1_driver",
110         .bus = &w1_bus_type,
111         .probe = w1_master_probe,
112         .remove = w1_master_remove,
113 };
114
115 struct device w1_device = {
116         .parent = NULL,
117         .bus = &w1_bus_type,
118         .bus_id = "w1 bus master",
119         .driver = &w1_driver,
120         .release = &w1_master_release
121 };
122
123 static struct device_attribute w1_slave_attribute = {
124         .attr = {
125                         .name = "name",
126                         .mode = S_IRUGO,
127                         .owner = THIS_MODULE
128         },
129         .show = &w1_default_read_name,
130 };
131
132 static struct device_attribute w1_slave_attribute_val = {
133         .attr = {
134                         .name = "value",
135                         .mode = S_IRUGO,
136                         .owner = THIS_MODULE
137         },
138         .show = &w1_default_read_name,
139 };
140
141 ssize_t w1_master_attribute_show_name(struct device *dev, char *buf)
142 {
143         struct w1_master *md = container_of (dev, struct w1_master, dev);
144         ssize_t count;
145         
146         if (down_interruptible (&md->mutex))
147                 return -EBUSY;
148
149         count = sprintf(buf, "%s\n", md->name);
150         
151         up(&md->mutex);
152
153         return count;
154 }
155
156 ssize_t w1_master_attribute_show_pointer(struct device *dev, char *buf)
157 {
158         struct w1_master *md = container_of(dev, struct w1_master, dev);
159         ssize_t count;
160         
161         if (down_interruptible(&md->mutex))
162                 return -EBUSY;
163
164         count = sprintf(buf, "0x%p\n", md->bus_master);
165         
166         up(&md->mutex);
167         return count;
168 }
169
170 ssize_t w1_master_attribute_show_timeout(struct device *dev, char *buf)
171 {
172         ssize_t count;
173         count = sprintf(buf, "%d\n", w1_timeout);
174         return count;
175 }
176
177 ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, char *buf)
178 {
179         struct w1_master *md = container_of(dev, struct w1_master, dev);
180         ssize_t count;
181         
182         if (down_interruptible(&md->mutex))
183                 return -EBUSY;
184
185         count = sprintf(buf, "%d\n", md->max_slave_count);
186         
187         up(&md->mutex);
188         return count;
189 }
190
191 ssize_t w1_master_attribute_show_attempts(struct device *dev, char *buf)
192 {
193         struct w1_master *md = container_of(dev, struct w1_master, dev);
194         ssize_t count;
195         
196         if (down_interruptible(&md->mutex))
197                 return -EBUSY;
198
199         count = sprintf(buf, "%lu\n", md->attempts);
200         
201         up(&md->mutex);
202         return count;
203 }
204
205 ssize_t w1_master_attribute_show_slave_count(struct device *dev, char *buf)
206 {
207         struct w1_master *md = container_of(dev, struct w1_master, dev);
208         ssize_t count;
209         
210         if (down_interruptible(&md->mutex))
211                 return -EBUSY;
212
213         count = sprintf(buf, "%d\n", md->slave_count);
214         
215         up(&md->mutex);
216         return count;
217 }
218
219 ssize_t w1_master_attribute_show_slaves(struct device *dev, char *buf)
220
221 {
222         struct w1_master *md = container_of(dev, struct w1_master, dev);
223         int c = PAGE_SIZE;
224
225         if (down_interruptible(&md->mutex))
226                 return -EBUSY;
227
228         if (md->slave_count == 0)
229                 c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
230         else {
231                 struct list_head *ent, *n;
232                 struct w1_slave *sl;
233
234                 list_for_each_safe(ent, n, &md->slist) {
235                         sl = list_entry(ent, struct w1_slave, w1_slave_entry);
236
237                         c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
238                 }
239         }
240
241         up(&md->mutex);
242
243         return PAGE_SIZE - c;
244 }
245
246 static struct device_attribute w1_master_attribute_slaves = {
247         .attr = {
248                         .name = "w1_master_slaves",
249                         .mode = S_IRUGO,
250                         .owner = THIS_MODULE,
251         },
252         .show = &w1_master_attribute_show_slaves,
253 };
254 static struct device_attribute w1_master_attribute_slave_count = {
255         .attr = {
256                         .name = "w1_master_slave_count",
257                         .mode = S_IRUGO,
258                         .owner = THIS_MODULE
259         },
260         .show = &w1_master_attribute_show_slave_count,
261 };
262 static struct device_attribute w1_master_attribute_attempts = {
263         .attr = {
264                         .name = "w1_master_attempts",
265                         .mode = S_IRUGO,
266                         .owner = THIS_MODULE
267         },
268         .show = &w1_master_attribute_show_attempts,
269 };
270 static struct device_attribute w1_master_attribute_max_slave_count = {
271         .attr = {
272                         .name = "w1_master_max_slave_count",
273                         .mode = S_IRUGO,
274                         .owner = THIS_MODULE
275         },
276         .show = &w1_master_attribute_show_max_slave_count,
277 };
278 static struct device_attribute w1_master_attribute_timeout = {
279         .attr = {
280                         .name = "w1_master_timeout",
281                         .mode = S_IRUGO,
282                         .owner = THIS_MODULE
283         },
284         .show = &w1_master_attribute_show_timeout,
285 };
286 static struct device_attribute w1_master_attribute_pointer = {
287         .attr = {
288                         .name = "w1_master_pointer",
289                         .mode = S_IRUGO,
290                         .owner = THIS_MODULE
291         },
292         .show = &w1_master_attribute_show_pointer,
293 };
294 static struct device_attribute w1_master_attribute_name = {
295         .attr = {
296                         .name = "w1_master_name",
297                         .mode = S_IRUGO,
298                         .owner = THIS_MODULE
299         },
300         .show = &w1_master_attribute_show_name,
301 };
302
303 static struct bin_attribute w1_slave_bin_attribute = {
304         .attr = {
305                         .name = "w1_slave",
306                         .mode = S_IRUGO,
307                         .owner = THIS_MODULE,
308         },
309         .size = W1_SLAVE_DATA_SIZE,
310         .read = &w1_default_read_bin,
311 };
312
313 static int __w1_attach_slave_device(struct w1_slave *sl)
314 {
315         int err;
316
317         sl->dev.parent = &sl->master->dev;
318         sl->dev.driver = sl->master->driver;
319         sl->dev.bus = &w1_bus_type;
320         sl->dev.release = &w1_slave_release;
321
322         snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id),
323                   "%02x-%012llx",
324                   (unsigned int) sl->reg_num.family,
325                   (unsigned long long) sl->reg_num.id);
326         snprintf (&sl->name[0], sizeof(sl->name),
327                   "%02x-%012llx",
328                   (unsigned int) sl->reg_num.family,
329                   (unsigned long long) sl->reg_num.id);
330
331         dev_dbg(&sl->dev, "%s: registering %s.\n", __func__,
332                 &sl->dev.bus_id[0]);
333
334         err = device_register(&sl->dev);
335         if (err < 0) {
336                 dev_err(&sl->dev,
337                          "Device registration [%s] failed. err=%d\n",
338                          sl->dev.bus_id, err);
339                 return err;
340         }
341
342         memcpy(&sl->attr_bin, &w1_slave_bin_attribute, sizeof(sl->attr_bin));
343         memcpy(&sl->attr_name, &w1_slave_attribute, sizeof(sl->attr_name));
344         memcpy(&sl->attr_val, &w1_slave_attribute_val, sizeof(sl->attr_val));
345         
346         sl->attr_bin.read = sl->family->fops->rbin;
347         sl->attr_name.show = sl->family->fops->rname;
348         sl->attr_val.show = sl->family->fops->rval;
349         sl->attr_val.attr.name = sl->family->fops->rvalname;
350
351         err = device_create_file(&sl->dev, &sl->attr_name);
352         if (err < 0) {
353                 dev_err(&sl->dev,
354                          "sysfs file creation for [%s] failed. err=%d\n",
355                          sl->dev.bus_id, err);
356                 device_unregister(&sl->dev);
357                 return err;
358         }
359
360         err = device_create_file(&sl->dev, &sl->attr_val);
361         if (err < 0) {
362                 dev_err(&sl->dev,
363                          "sysfs file creation for [%s] failed. err=%d\n",
364                          sl->dev.bus_id, err);
365                 device_remove_file(&sl->dev, &sl->attr_name);
366                 device_unregister(&sl->dev);
367                 return err;
368         }
369
370         err = sysfs_create_bin_file(&sl->dev.kobj, &sl->attr_bin);
371         if (err < 0) {
372                 dev_err(&sl->dev,
373                          "sysfs file creation for [%s] failed. err=%d\n",
374                          sl->dev.bus_id, err);
375                 device_remove_file(&sl->dev, &sl->attr_name);
376                 device_remove_file(&sl->dev, &sl->attr_val);
377                 device_unregister(&sl->dev);
378                 return err;
379         }
380
381         list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
382
383         return 0;
384 }
385
386 static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
387 {
388         struct w1_slave *sl;
389         struct w1_family *f;
390         int err;
391         struct w1_netlink_msg msg;
392
393         sl = kmalloc(sizeof(struct w1_slave), GFP_KERNEL);
394         if (!sl) {
395                 dev_err(&dev->dev,
396                          "%s: failed to allocate new slave device.\n",
397                          __func__);
398                 return -ENOMEM;
399         }
400
401         memset(sl, 0, sizeof(*sl));
402
403         sl->owner = THIS_MODULE;
404         sl->master = dev;
405         set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
406
407         memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
408         atomic_set(&sl->refcnt, 0);
409         init_completion(&sl->dev_released);
410
411         spin_lock(&w1_flock);
412         f = w1_family_registered(rn->family);
413         if (!f) {
414                 spin_unlock(&w1_flock);
415                 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
416                           rn->family, rn->family, rn->id, rn->crc);
417                 kfree(sl);
418                 return -ENODEV;
419         }
420         __w1_family_get(f);
421         spin_unlock(&w1_flock);
422
423         sl->family = f;
424
425
426         err = __w1_attach_slave_device(sl);
427         if (err < 0) {
428                 dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
429                          sl->name);
430                 w1_family_put(sl->family);
431                 kfree(sl);
432                 return err;
433         }
434
435         sl->ttl = dev->slave_ttl;
436         dev->slave_count++;
437
438         memcpy(&msg.id.id, rn, sizeof(msg.id.id));
439         msg.type = W1_SLAVE_ADD;
440         w1_netlink_send(dev, &msg);
441
442         return 0;
443 }
444
445 static void w1_slave_detach(struct w1_slave *sl)
446 {
447         struct w1_netlink_msg msg;
448         
449         dev_info(&sl->dev, "%s: detaching %s.\n", __func__, sl->name);
450
451         while (atomic_read(&sl->refcnt)) {
452                 printk(KERN_INFO "Waiting for %s to become free: refcnt=%d.\n",
453                                 sl->name, atomic_read(&sl->refcnt));
454
455                 if (msleep_interruptible(1000))
456                         flush_signals(current);
457         }
458
459         sysfs_remove_bin_file (&sl->dev.kobj, &sl->attr_bin);
460         device_remove_file(&sl->dev, &sl->attr_name);
461         device_remove_file(&sl->dev, &sl->attr_val);
462         device_unregister(&sl->dev);
463         w1_family_put(sl->family);
464
465         memcpy(&msg.id.id, &sl->reg_num, sizeof(msg.id.id));
466         msg.type = W1_SLAVE_REMOVE;
467         w1_netlink_send(sl->master, &msg);
468 }
469
470 static struct w1_master *w1_search_master(unsigned long data)
471 {
472         struct w1_master *dev;
473         int found = 0;
474         
475         spin_lock_irq(&w1_mlock);
476         list_for_each_entry(dev, &w1_masters, w1_master_entry) {
477                 if (dev->bus_master->data == data) {
478                         found = 1;
479                         atomic_inc(&dev->refcnt);
480                         break;
481                 }
482         }
483         spin_unlock_irq(&w1_mlock);
484
485         return (found)?dev:NULL;
486 }
487
488 void w1_slave_found(unsigned long data, u64 rn)
489 {
490         int slave_count;
491         struct w1_slave *sl;
492         struct list_head *ent;
493         struct w1_reg_num *tmp;
494         int family_found = 0;
495         struct w1_master *dev;
496
497         dev = w1_search_master(data);
498         if (!dev) {
499                 printk(KERN_ERR "Failed to find w1 master device for data %08lx, it is impossible.\n",
500                                 data);
501                 return;
502         }
503         
504         tmp = (struct w1_reg_num *) &rn;
505
506         slave_count = 0;
507         list_for_each(ent, &dev->slist) {
508
509                 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
510
511                 if (sl->reg_num.family == tmp->family &&
512                     sl->reg_num.id == tmp->id &&
513                     sl->reg_num.crc == tmp->crc) {
514                         set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
515                         break;
516                 }
517                 else if (sl->reg_num.family == tmp->family) {
518                         family_found = 1;
519                         break;
520                 }
521
522                 slave_count++;
523         }
524
525         if (slave_count == dev->slave_count &&
526                 rn && ((rn >> 56) & 0xff) == w1_calc_crc8((u8 *)&rn, 7)) {
527                 w1_attach_slave_device(dev, (struct w1_reg_num *) &rn);
528         }
529                         
530         atomic_dec(&dev->refcnt);
531 }
532
533 void w1_search(struct w1_master *dev)
534 {
535         u64 last, rn, tmp;
536         int i, count = 0;
537         int last_family_desc, last_zero, last_device;
538         int search_bit, id_bit, comp_bit, desc_bit;
539
540         search_bit = id_bit = comp_bit = 0;
541         rn = tmp = last = 0;
542         last_device = last_zero = last_family_desc = 0;
543
544         desc_bit = 64;
545
546         while (!(id_bit && comp_bit) && !last_device
547                 && count++ < dev->max_slave_count) {
548                 last = rn;
549                 rn = 0;
550
551                 last_family_desc = 0;
552
553                 /*
554                  * Reset bus and all 1-wire device state machines
555                  * so they can respond to our requests.
556                  *
557                  * Return 0 - device(s) present, 1 - no devices present.
558                  */
559                 if (w1_reset_bus(dev)) {
560                         dev_info(&dev->dev, "No devices present on the wire.\n");
561                         break;
562                 }
563
564 #if 1
565                 w1_write_8(dev, W1_SEARCH);
566                 for (i = 0; i < 64; ++i) {
567                         /*
568                          * Read 2 bits from bus.
569                          * All who don't sleep must send ID bit and COMPLEMENT ID bit.
570                          * They actually are ANDed between all senders.
571                          */
572                         id_bit = w1_touch_bit(dev, 1);
573                         comp_bit = w1_touch_bit(dev, 1);
574
575                         if (id_bit && comp_bit)
576                                 break;
577
578                         if (id_bit == 0 && comp_bit == 0) {
579                                 if (i == desc_bit)
580                                         search_bit = 1;
581                                 else if (i > desc_bit)
582                                         search_bit = 0;
583                                 else
584                                         search_bit = ((last >> i) & 0x1);
585
586                                 if (search_bit == 0) {
587                                         last_zero = i;
588                                         if (last_zero < 9)
589                                                 last_family_desc = last_zero;
590                                 }
591
592                         }
593                         else
594                                 search_bit = id_bit;
595
596                         tmp = search_bit;
597                         rn |= (tmp << i);
598
599                         /*
600                          * Write 1 bit to bus
601                          * and make all who don't have "search_bit" in "i"'th position
602                          * in it's registration number sleep.
603                          */
604                         if (dev->bus_master->touch_bit)
605                                 w1_touch_bit(dev, search_bit);
606                         else
607                                 w1_write_bit(dev, search_bit);
608
609                 }
610 #endif
611
612                 if (desc_bit == last_zero)
613                         last_device = 1;
614
615                 desc_bit = last_zero;
616         
617                 w1_slave_found(dev->bus_master->data, rn);
618         }
619 }
620
621 int w1_create_master_attributes(struct w1_master *dev)
622 {
623         if (    device_create_file(&dev->dev, &w1_master_attribute_slaves) < 0 ||
624                 device_create_file(&dev->dev, &w1_master_attribute_slave_count) < 0 ||
625                 device_create_file(&dev->dev, &w1_master_attribute_attempts) < 0 ||
626                 device_create_file(&dev->dev, &w1_master_attribute_max_slave_count) < 0 ||
627                 device_create_file(&dev->dev, &w1_master_attribute_timeout) < 0||
628                 device_create_file(&dev->dev, &w1_master_attribute_pointer) < 0||
629                 device_create_file(&dev->dev, &w1_master_attribute_name) < 0)
630                 return -EINVAL;
631
632         return 0;
633 }
634
635 void w1_destroy_master_attributes(struct w1_master *dev)
636 {
637         device_remove_file(&dev->dev, &w1_master_attribute_slaves);
638         device_remove_file(&dev->dev, &w1_master_attribute_slave_count);
639         device_remove_file(&dev->dev, &w1_master_attribute_attempts);
640         device_remove_file(&dev->dev, &w1_master_attribute_max_slave_count);
641         device_remove_file(&dev->dev, &w1_master_attribute_timeout);
642         device_remove_file(&dev->dev, &w1_master_attribute_pointer);
643         device_remove_file(&dev->dev, &w1_master_attribute_name);
644 }
645
646
647 int w1_control(void *data)
648 {
649         struct w1_slave *sl;
650         struct w1_master *dev;
651         struct list_head *ent, *ment, *n, *mn;
652         int err, have_to_wait = 0, timeout;
653
654         daemonize("w1_control");
655         allow_signal(SIGTERM);
656
657         while (!control_needs_exit || have_to_wait) {
658                 have_to_wait = 0;
659
660                 timeout = w1_timeout*HZ;
661                 do {
662                         timeout = interruptible_sleep_on_timeout(&w1_control_wait, timeout);
663                         try_to_freeze(PF_FREEZE);
664                 } while (!signal_pending(current) && (timeout > 0));
665
666                 if (signal_pending(current))
667                         flush_signals(current);
668
669                 list_for_each_safe(ment, mn, &w1_masters) {
670                         dev = list_entry(ment, struct w1_master, w1_master_entry);
671
672                         if (!control_needs_exit && !dev->need_exit)
673                                 continue;
674                         /*
675                          * Little race: we can create thread but not set the flag.
676                          * Get a chance for external process to set flag up.
677                          */
678                         if (!dev->initialized) {
679                                 have_to_wait = 1;
680                                 continue;
681                         }
682
683                         spin_lock(&w1_mlock);
684                         list_del(&dev->w1_master_entry);
685                         spin_unlock(&w1_mlock);
686
687                         if (control_needs_exit) {
688                                 dev->need_exit = 1;
689
690                                 err = kill_proc(dev->kpid, SIGTERM, 1);
691                                 if (err)
692                                         dev_err(&dev->dev,
693                                                  "Failed to send signal to w1 kernel thread %d.\n",
694                                                  dev->kpid);
695                         }
696
697                         wait_for_completion(&dev->dev_exited);
698
699                         list_for_each_safe(ent, n, &dev->slist) {
700                                 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
701
702                                 if (!sl)
703                                         dev_warn(&dev->dev,
704                                                   "%s: slave entry is NULL.\n",
705                                                   __func__);
706                                 else {
707                                         list_del(&sl->w1_slave_entry);
708
709                                         w1_slave_detach(sl);
710                                         kfree(sl);
711                                 }
712                         }
713                         w1_destroy_master_attributes(dev);
714                         atomic_dec(&dev->refcnt);
715                 }
716         }
717
718         complete_and_exit(&w1_control_complete, 0);
719 }
720
721 int w1_process(void *data)
722 {
723         struct w1_master *dev = (struct w1_master *) data;
724         unsigned long timeout;
725         struct list_head *ent, *n;
726         struct w1_slave *sl;
727
728         daemonize("%s", dev->name);
729         allow_signal(SIGTERM);
730
731         while (!dev->need_exit) {
732                 timeout = w1_timeout*HZ;
733                 do {
734                         timeout = interruptible_sleep_on_timeout(&dev->kwait, timeout);
735                         try_to_freeze(PF_FREEZE);
736                 } while (!signal_pending(current) && (timeout > 0));
737
738                 if (signal_pending(current))
739                         flush_signals(current);
740
741                 if (dev->need_exit)
742                         break;
743
744                 if (!dev->initialized)
745                         continue;
746
747                 if (down_interruptible(&dev->mutex))
748                         continue;
749
750                 list_for_each_safe(ent, n, &dev->slist) {
751                         sl = list_entry(ent, struct w1_slave, w1_slave_entry);
752
753                         if (sl)
754                                 clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
755                 }
756                 
757                 w1_search_devices(dev, w1_slave_found);
758
759                 list_for_each_safe(ent, n, &dev->slist) {
760                         sl = list_entry(ent, struct w1_slave, w1_slave_entry);
761
762                         if (sl && !test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) {
763                                 list_del (&sl->w1_slave_entry);
764
765                                 w1_slave_detach (sl);
766                                 kfree (sl);
767
768                                 dev->slave_count--;
769                         }
770                         else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))
771                                 sl->ttl = dev->slave_ttl;
772                 }
773                 up(&dev->mutex);
774         }
775
776         atomic_dec(&dev->refcnt);
777         complete_and_exit(&dev->dev_exited, 0);
778
779         return 0;
780 }
781
782 int w1_init(void)
783 {
784         int retval;
785
786         printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
787
788         retval = bus_register(&w1_bus_type);
789         if (retval) {
790                 printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
791                 goto err_out_exit_init;
792         }
793
794         retval = driver_register(&w1_driver);
795         if (retval) {
796                 printk(KERN_ERR
797                         "Failed to register master driver. err=%d.\n",
798                         retval);
799                 goto err_out_bus_unregister;
800         }
801
802         control_thread = kernel_thread(&w1_control, NULL, 0);
803         if (control_thread < 0) {
804                 printk(KERN_ERR "Failed to create control thread. err=%d\n",
805                         control_thread);
806                 retval = control_thread;
807                 goto err_out_driver_unregister;
808         }
809
810         return 0;
811
812 err_out_driver_unregister:
813         driver_unregister(&w1_driver);
814
815 err_out_bus_unregister:
816         bus_unregister(&w1_bus_type);
817
818 err_out_exit_init:
819         return retval;
820 }
821
822 void w1_fini(void)
823 {
824         struct w1_master *dev;
825         struct list_head *ent, *n;
826
827         list_for_each_safe(ent, n, &w1_masters) {
828                 dev = list_entry(ent, struct w1_master, w1_master_entry);
829                 __w1_remove_master_device(dev);
830         }
831
832         control_needs_exit = 1;
833
834         wait_for_completion(&w1_control_complete);
835
836         driver_unregister(&w1_driver);
837         bus_unregister(&w1_bus_type);
838 }
839
840 module_init(w1_init);
841 module_exit(w1_fini);
842
843 EXPORT_SYMBOL(w1_create_master_attributes);
844 EXPORT_SYMBOL(w1_destroy_master_attributes);