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