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