b41366a0784cc39cf91a126d5c218084e773dfe6
[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 <linux/delay.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/list.h>
27 #include <linux/interrupt.h>
28 #include <linux/spinlock.h>
29 #include <linux/timer.h>
30 #include <linux/device.h>
31 #include <linux/slab.h>
32 #include <linux/sched.h>
33 #include <linux/kthread.h>
34
35 #include <asm/atomic.h>
36
37 #include "w1.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 static int w1_control_timeout = 1;
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(control_timeout, w1_control_timeout, int, 0);
54 module_param_named(max_slave_count, w1_max_slave_count, int, 0);
55 module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
56
57 DEFINE_MUTEX(w1_mlock);
58 LIST_HEAD(w1_masters);
59
60 static struct task_struct *w1_control_thread;
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 void w1_master_release(struct device *dev)
73 {
74         struct w1_master *md = dev_to_w1_master(dev);
75
76         dev_dbg(dev, "%s: Releasing %s.\n", __func__, md->name);
77         memset(md, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
78         kfree(md);
79 }
80
81 static void w1_slave_release(struct device *dev)
82 {
83         struct w1_slave *sl = dev_to_w1_slave(dev);
84
85         printk("%s: Releasing %s.\n", __func__, sl->name);
86
87         while (atomic_read(&sl->refcnt)) {
88                 printk("Waiting for %s to become free: refcnt=%d.\n",
89                                 sl->name, atomic_read(&sl->refcnt));
90                 if (msleep_interruptible(1000))
91                         flush_signals(current);
92         }
93
94         w1_family_put(sl->family);
95         sl->master->slave_count--;
96
97         complete(&sl->released);
98 }
99
100 static ssize_t w1_slave_read_name(struct device *dev, struct device_attribute *attr, char *buf)
101 {
102         struct w1_slave *sl = dev_to_w1_slave(dev);
103
104         return sprintf(buf, "%s\n", sl->name);
105 }
106
107 static ssize_t w1_slave_read_id(struct kobject *kobj, char *buf, loff_t off, size_t count)
108 {
109         struct w1_slave *sl = kobj_to_w1_slave(kobj);
110
111         if (off > 8) {
112                 count = 0;
113         } else {
114                 if (off + count > 8)
115                         count = 8 - off;
116
117                 memcpy(buf, (u8 *)&sl->reg_num, count);
118         }
119
120         return count;
121 }
122
123 static struct device_attribute w1_slave_attr_name =
124         __ATTR(name, S_IRUGO, w1_slave_read_name, NULL);
125
126 static struct bin_attribute w1_slave_attr_bin_id = {
127       .attr = {
128               .name = "id",
129               .mode = S_IRUGO,
130               .owner = THIS_MODULE,
131       },
132       .size = 8,
133       .read = w1_slave_read_id,
134 };
135
136 /* Default family */
137
138 static ssize_t w1_default_write(struct kobject *kobj, char *buf, loff_t off, size_t count)
139 {
140         struct w1_slave *sl = kobj_to_w1_slave(kobj);
141
142         mutex_lock(&sl->master->mutex);
143         if (w1_reset_select_slave(sl)) {
144                 count = 0;
145                 goto out_up;
146         }
147
148         w1_write_block(sl->master, buf, count);
149
150 out_up:
151         mutex_unlock(&sl->master->mutex);
152         return count;
153 }
154
155 static ssize_t w1_default_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
156 {
157         struct w1_slave *sl = kobj_to_w1_slave(kobj);
158
159         mutex_lock(&sl->master->mutex);
160         w1_read_block(sl->master, buf, count);
161         mutex_unlock(&sl->master->mutex);
162         return count;
163 }
164
165 static struct bin_attribute w1_default_attr = {
166       .attr = {
167               .name = "rw",
168               .mode = S_IRUGO | S_IWUSR,
169               .owner = THIS_MODULE,
170       },
171       .size = PAGE_SIZE,
172       .read = w1_default_read,
173       .write = w1_default_write,
174 };
175
176 static int w1_default_add_slave(struct w1_slave *sl)
177 {
178         return sysfs_create_bin_file(&sl->dev.kobj, &w1_default_attr);
179 }
180
181 static void w1_default_remove_slave(struct w1_slave *sl)
182 {
183         sysfs_remove_bin_file(&sl->dev.kobj, &w1_default_attr);
184 }
185
186 static struct w1_family_ops w1_default_fops = {
187         .add_slave      = w1_default_add_slave,
188         .remove_slave   = w1_default_remove_slave,
189 };
190
191 static struct w1_family w1_default_family = {
192         .fops = &w1_default_fops,
193 };
194
195 static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size);
196
197 static struct bus_type w1_bus_type = {
198         .name = "w1",
199         .match = w1_master_match,
200         .uevent = w1_uevent,
201 };
202
203 struct device_driver w1_master_driver = {
204         .name = "w1_master_driver",
205         .bus = &w1_bus_type,
206         .probe = w1_master_probe,
207 };
208
209 struct device w1_master_device = {
210         .parent = NULL,
211         .bus = &w1_bus_type,
212         .bus_id = "w1 bus master",
213         .driver = &w1_master_driver,
214         .release = &w1_master_release
215 };
216
217 static struct device_driver w1_slave_driver = {
218         .name = "w1_slave_driver",
219         .bus = &w1_bus_type,
220 };
221
222 #if 0
223 struct device w1_slave_device = {
224         .parent = NULL,
225         .bus = &w1_bus_type,
226         .bus_id = "w1 bus slave",
227         .driver = &w1_slave_driver,
228         .release = &w1_slave_release
229 };
230 #endif  /*  0  */
231
232 static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
233 {
234         struct w1_master *md = dev_to_w1_master(dev);
235         ssize_t count;
236
237         mutex_lock(&md->mutex);
238         count = sprintf(buf, "%s\n", md->name);
239         mutex_unlock(&md->mutex);
240
241         return count;
242 }
243
244 static ssize_t w1_master_attribute_store_search(struct device * dev,
245                                                 struct device_attribute *attr,
246                                                 const char * buf, size_t count)
247 {
248         struct w1_master *md = dev_to_w1_master(dev);
249
250         mutex_lock(&md->mutex);
251         md->search_count = simple_strtol(buf, NULL, 0);
252         mutex_unlock(&md->mutex);
253
254         return count;
255 }
256
257 static ssize_t w1_master_attribute_show_search(struct device *dev,
258                                                struct device_attribute *attr,
259                                                char *buf)
260 {
261         struct w1_master *md = dev_to_w1_master(dev);
262         ssize_t count;
263
264         mutex_lock(&md->mutex);
265         count = sprintf(buf, "%d\n", md->search_count);
266         mutex_unlock(&md->mutex);
267
268         return count;
269 }
270
271 static ssize_t w1_master_attribute_show_pointer(struct device *dev, struct device_attribute *attr, char *buf)
272 {
273         struct w1_master *md = dev_to_w1_master(dev);
274         ssize_t count;
275
276         mutex_lock(&md->mutex);
277         count = sprintf(buf, "0x%p\n", md->bus_master);
278         mutex_unlock(&md->mutex);
279         return count;
280 }
281
282 static ssize_t w1_master_attribute_show_timeout(struct device *dev, struct device_attribute *attr, char *buf)
283 {
284         ssize_t count;
285         count = sprintf(buf, "%d\n", w1_timeout);
286         return count;
287 }
288
289 static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
290 {
291         struct w1_master *md = dev_to_w1_master(dev);
292         ssize_t count;
293
294         mutex_lock(&md->mutex);
295         count = sprintf(buf, "%d\n", md->max_slave_count);
296         mutex_unlock(&md->mutex);
297         return count;
298 }
299
300 static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf)
301 {
302         struct w1_master *md = dev_to_w1_master(dev);
303         ssize_t count;
304
305         mutex_lock(&md->mutex);
306         count = sprintf(buf, "%lu\n", md->attempts);
307         mutex_unlock(&md->mutex);
308         return count;
309 }
310
311 static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
312 {
313         struct w1_master *md = dev_to_w1_master(dev);
314         ssize_t count;
315
316         mutex_lock(&md->mutex);
317         count = sprintf(buf, "%d\n", md->slave_count);
318         mutex_unlock(&md->mutex);
319         return count;
320 }
321
322 static ssize_t w1_master_attribute_show_slaves(struct device *dev, struct device_attribute *attr, char *buf)
323 {
324         struct w1_master *md = dev_to_w1_master(dev);
325         int c = PAGE_SIZE;
326
327         mutex_lock(&md->mutex);
328
329         if (md->slave_count == 0)
330                 c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
331         else {
332                 struct list_head *ent, *n;
333                 struct w1_slave *sl;
334
335                 list_for_each_safe(ent, n, &md->slist) {
336                         sl = list_entry(ent, struct w1_slave, w1_slave_entry);
337
338                         c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
339                 }
340         }
341
342         mutex_unlock(&md->mutex);
343
344         return PAGE_SIZE - c;
345 }
346
347 #define W1_MASTER_ATTR_RO(_name, _mode)                         \
348         struct device_attribute w1_master_attribute_##_name =   \
349                 __ATTR(w1_master_##_name, _mode,                \
350                        w1_master_attribute_show_##_name, NULL)
351
352 #define W1_MASTER_ATTR_RW(_name, _mode)                         \
353         struct device_attribute w1_master_attribute_##_name =   \
354                 __ATTR(w1_master_##_name, _mode,                \
355                        w1_master_attribute_show_##_name,        \
356                        w1_master_attribute_store_##_name)
357
358 static W1_MASTER_ATTR_RO(name, S_IRUGO);
359 static W1_MASTER_ATTR_RO(slaves, S_IRUGO);
360 static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);
361 static W1_MASTER_ATTR_RO(max_slave_count, S_IRUGO);
362 static W1_MASTER_ATTR_RO(attempts, S_IRUGO);
363 static W1_MASTER_ATTR_RO(timeout, S_IRUGO);
364 static W1_MASTER_ATTR_RO(pointer, S_IRUGO);
365 static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUGO);
366
367 static struct attribute *w1_master_default_attrs[] = {
368         &w1_master_attribute_name.attr,
369         &w1_master_attribute_slaves.attr,
370         &w1_master_attribute_slave_count.attr,
371         &w1_master_attribute_max_slave_count.attr,
372         &w1_master_attribute_attempts.attr,
373         &w1_master_attribute_timeout.attr,
374         &w1_master_attribute_pointer.attr,
375         &w1_master_attribute_search.attr,
376         NULL
377 };
378
379 static struct attribute_group w1_master_defattr_group = {
380         .attrs = w1_master_default_attrs,
381 };
382
383 int w1_create_master_attributes(struct w1_master *master)
384 {
385         return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
386 }
387
388 static void w1_destroy_master_attributes(struct w1_master *master)
389 {
390         sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
391 }
392
393 #ifdef CONFIG_HOTPLUG
394 static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
395 {
396         struct w1_master *md = NULL;
397         struct w1_slave *sl = NULL;
398         char *event_owner, *name;
399         int err, cur_index=0, cur_len=0;
400
401         if (dev->driver == &w1_master_driver) {
402                 md = container_of(dev, struct w1_master, dev);
403                 event_owner = "master";
404                 name = md->name;
405         } else if (dev->driver == &w1_slave_driver) {
406                 sl = container_of(dev, struct w1_slave, dev);
407                 event_owner = "slave";
408                 name = sl->name;
409         } else {
410                 dev_dbg(dev, "Unknown event.\n");
411                 return -EINVAL;
412         }
413
414         dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n", event_owner, name, dev->bus_id);
415
416         if (dev->driver != &w1_slave_driver || !sl)
417                 return 0;
418
419         err = add_uevent_var(envp, num_envp, &cur_index, buffer, buffer_size, &cur_len, "W1_FID=%02X", sl->reg_num.family);
420         if (err)
421                 return err;
422
423         err = add_uevent_var(envp, num_envp, &cur_index, buffer, buffer_size, &cur_len, "W1_SLAVE_ID=%024LX", (u64)sl->reg_num.id);
424         if (err)
425                 return err;
426
427         return 0;
428 };
429 #else
430 static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
431 {
432         return 0;
433 }
434 #endif
435
436 static int __w1_attach_slave_device(struct w1_slave *sl)
437 {
438         int err;
439
440         sl->dev.parent = &sl->master->dev;
441         sl->dev.driver = &w1_slave_driver;
442         sl->dev.bus = &w1_bus_type;
443         sl->dev.release = &w1_slave_release;
444
445         snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id),
446                  "%02x-%012llx",
447                  (unsigned int) sl->reg_num.family,
448                  (unsigned long long) sl->reg_num.id);
449         snprintf(&sl->name[0], sizeof(sl->name),
450                  "%02x-%012llx",
451                  (unsigned int) sl->reg_num.family,
452                  (unsigned long long) sl->reg_num.id);
453
454         dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__, &sl->dev.bus_id[0]);
455
456         err = device_register(&sl->dev);
457         if (err < 0) {
458                 dev_err(&sl->dev,
459                         "Device registration [%s] failed. err=%d\n",
460                         sl->dev.bus_id, err);
461                 return err;
462         }
463
464         /* Create "name" entry */
465         err = device_create_file(&sl->dev, &w1_slave_attr_name);
466         if (err < 0) {
467                 dev_err(&sl->dev,
468                         "sysfs file creation for [%s] failed. err=%d\n",
469                         sl->dev.bus_id, err);
470                 goto out_unreg;
471         }
472
473         /* Create "id" entry */
474         err = sysfs_create_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
475         if (err < 0) {
476                 dev_err(&sl->dev,
477                         "sysfs file creation for [%s] failed. err=%d\n",
478                         sl->dev.bus_id, err);
479                 goto out_rem1;
480         }
481
482         /* if the family driver needs to initialize something... */
483         if (sl->family->fops && sl->family->fops->add_slave &&
484             ((err = sl->family->fops->add_slave(sl)) < 0)) {
485                 dev_err(&sl->dev,
486                         "sysfs file creation for [%s] failed. err=%d\n",
487                         sl->dev.bus_id, err);
488                 goto out_rem2;
489         }
490
491         list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
492
493         return 0;
494
495 out_rem2:
496         sysfs_remove_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
497 out_rem1:
498         device_remove_file(&sl->dev, &w1_slave_attr_name);
499 out_unreg:
500         device_unregister(&sl->dev);
501         return err;
502 }
503
504 static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
505 {
506         struct w1_slave *sl;
507         struct w1_family *f;
508         int err;
509         struct w1_netlink_msg msg;
510
511         sl = kmalloc(sizeof(struct w1_slave), GFP_KERNEL);
512         if (!sl) {
513                 dev_err(&dev->dev,
514                          "%s: failed to allocate new slave device.\n",
515                          __func__);
516                 return -ENOMEM;
517         }
518
519         memset(sl, 0, sizeof(*sl));
520
521         sl->owner = THIS_MODULE;
522         sl->master = dev;
523         set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
524
525         memset(&msg, 0, sizeof(msg));
526         memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
527         atomic_set(&sl->refcnt, 0);
528         init_completion(&sl->released);
529
530         spin_lock(&w1_flock);
531         f = w1_family_registered(rn->family);
532         if (!f) {
533                 f= &w1_default_family;
534                 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
535                           rn->family, rn->family,
536                           (unsigned long long)rn->id, rn->crc);
537         }
538         __w1_family_get(f);
539         spin_unlock(&w1_flock);
540
541         sl->family = f;
542
543
544         err = __w1_attach_slave_device(sl);
545         if (err < 0) {
546                 dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
547                          sl->name);
548                 w1_family_put(sl->family);
549                 kfree(sl);
550                 return err;
551         }
552
553         sl->ttl = dev->slave_ttl;
554         dev->slave_count++;
555
556         memcpy(msg.id.id, rn, sizeof(msg.id));
557         msg.type = W1_SLAVE_ADD;
558         w1_netlink_send(dev, &msg);
559
560         return 0;
561 }
562
563 static void w1_slave_detach(struct w1_slave *sl)
564 {
565         struct w1_netlink_msg msg;
566
567         dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__, sl->name, sl);
568
569         list_del(&sl->w1_slave_entry);
570
571         if (sl->family->fops && sl->family->fops->remove_slave)
572                 sl->family->fops->remove_slave(sl);
573
574         memset(&msg, 0, sizeof(msg));
575         memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
576         msg.type = W1_SLAVE_REMOVE;
577         w1_netlink_send(sl->master, &msg);
578
579         sysfs_remove_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
580         device_remove_file(&sl->dev, &w1_slave_attr_name);
581         device_unregister(&sl->dev);
582
583         wait_for_completion(&sl->released);
584         kfree(sl);
585 }
586
587 static struct w1_master *w1_search_master(void *data)
588 {
589         struct w1_master *dev;
590         int found = 0;
591
592         mutex_lock(&w1_mlock);
593         list_for_each_entry(dev, &w1_masters, w1_master_entry) {
594                 if (dev->bus_master->data == data) {
595                         found = 1;
596                         atomic_inc(&dev->refcnt);
597                         break;
598                 }
599         }
600         mutex_unlock(&w1_mlock);
601
602         return (found)?dev:NULL;
603 }
604
605 struct w1_master *w1_search_master_id(u32 id)
606 {
607         struct w1_master *dev;
608         int found = 0;
609
610         mutex_lock(&w1_mlock);
611         list_for_each_entry(dev, &w1_masters, w1_master_entry) {
612                 if (dev->id == id) {
613                         found = 1;
614                         atomic_inc(&dev->refcnt);
615                         break;
616                 }
617         }
618         mutex_unlock(&w1_mlock);
619
620         return (found)?dev:NULL;
621 }
622
623 struct w1_slave *w1_search_slave(struct w1_reg_num *id)
624 {
625         struct w1_master *dev;
626         struct w1_slave *sl = NULL;
627         int found = 0;
628
629         mutex_lock(&w1_mlock);
630         list_for_each_entry(dev, &w1_masters, w1_master_entry) {
631                 mutex_lock(&dev->mutex);
632                 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
633                         if (sl->reg_num.family == id->family &&
634                                         sl->reg_num.id == id->id &&
635                                         sl->reg_num.crc == id->crc) {
636                                 found = 1;
637                                 atomic_inc(&dev->refcnt);
638                                 atomic_inc(&sl->refcnt);
639                                 break;
640                         }
641                 }
642                 mutex_unlock(&dev->mutex);
643
644                 if (found)
645                         break;
646         }
647         mutex_unlock(&w1_mlock);
648
649         return (found)?sl:NULL;
650 }
651
652 void w1_reconnect_slaves(struct w1_family *f)
653 {
654         struct w1_master *dev;
655
656         mutex_lock(&w1_mlock);
657         list_for_each_entry(dev, &w1_masters, w1_master_entry) {
658                 dev_dbg(&dev->dev, "Reconnecting slaves in %s into new family %02x.\n",
659                                 dev->name, f->fid);
660                 set_bit(W1_MASTER_NEED_RECONNECT, &dev->flags);
661         }
662         mutex_unlock(&w1_mlock);
663 }
664
665 static void w1_slave_found(void *data, u64 rn)
666 {
667         int slave_count;
668         struct w1_slave *sl;
669         struct list_head *ent;
670         struct w1_reg_num *tmp;
671         int family_found = 0;
672         struct w1_master *dev;
673         u64 rn_le = cpu_to_le64(rn);
674
675         dev = w1_search_master(data);
676         if (!dev) {
677                 printk(KERN_ERR "Failed to find w1 master device for data %p, "
678                        "it is impossible.\n", data);
679                 return;
680         }
681
682         tmp = (struct w1_reg_num *) &rn;
683
684         slave_count = 0;
685         list_for_each(ent, &dev->slist) {
686
687                 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
688
689                 if (sl->reg_num.family == tmp->family &&
690                     sl->reg_num.id == tmp->id &&
691                     sl->reg_num.crc == tmp->crc) {
692                         set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
693                         break;
694                 } else if (sl->reg_num.family == tmp->family) {
695                         family_found = 1;
696                         break;
697                 }
698
699                 slave_count++;
700         }
701
702         if (slave_count == dev->slave_count &&
703                 rn && ((rn >> 56) & 0xff) == w1_calc_crc8((u8 *)&rn_le, 7)) {
704                 w1_attach_slave_device(dev, tmp);
705         }
706
707         atomic_dec(&dev->refcnt);
708 }
709
710 /**
711  * Performs a ROM Search & registers any devices found.
712  * The 1-wire search is a simple binary tree search.
713  * For each bit of the address, we read two bits and write one bit.
714  * The bit written will put to sleep all devies that don't match that bit.
715  * When the two reads differ, the direction choice is obvious.
716  * When both bits are 0, we must choose a path to take.
717  * When we can scan all 64 bits without having to choose a path, we are done.
718  *
719  * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
720  *
721  * @dev        The master device to search
722  * @cb         Function to call when a device is found
723  */
724 void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
725 {
726         u64 last_rn, rn, tmp64;
727         int i, slave_count = 0;
728         int last_zero, last_device;
729         int search_bit, desc_bit;
730         u8  triplet_ret = 0;
731
732         search_bit = 0;
733         rn = last_rn = 0;
734         last_device = 0;
735         last_zero = -1;
736
737         desc_bit = 64;
738
739         while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
740                 last_rn = rn;
741                 rn = 0;
742
743                 /*
744                  * Reset bus and all 1-wire device state machines
745                  * so they can respond to our requests.
746                  *
747                  * Return 0 - device(s) present, 1 - no devices present.
748                  */
749                 if (w1_reset_bus(dev)) {
750                         dev_dbg(&dev->dev, "No devices present on the wire.\n");
751                         break;
752                 }
753
754                 /* Start the search */
755                 w1_write_8(dev, search_type);
756                 for (i = 0; i < 64; ++i) {
757                         /* Determine the direction/search bit */
758                         if (i == desc_bit)
759                                 search_bit = 1;   /* took the 0 path last time, so take the 1 path */
760                         else if (i > desc_bit)
761                                 search_bit = 0;   /* take the 0 path on the next branch */
762                         else
763                                 search_bit = ((last_rn >> i) & 0x1);
764
765                         /** Read two bits and write one bit */
766                         triplet_ret = w1_triplet(dev, search_bit);
767
768                         /* quit if no device responded */
769                         if ( (triplet_ret & 0x03) == 0x03 )
770                                 break;
771
772                         /* If both directions were valid, and we took the 0 path... */
773                         if (triplet_ret == 0)
774                                 last_zero = i;
775
776                         /* extract the direction taken & update the device number */
777                         tmp64 = (triplet_ret >> 2);
778                         rn |= (tmp64 << i);
779                 }
780
781                 if ( (triplet_ret & 0x03) != 0x03 ) {
782                         if ( (desc_bit == last_zero) || (last_zero < 0))
783                                 last_device = 1;
784                         desc_bit = last_zero;
785                         cb(dev->bus_master->data, rn);
786                 }
787         }
788 }
789
790 static int w1_control(void *data)
791 {
792         struct w1_slave *sl, *sln;
793         struct w1_master *dev, *n;
794         int have_to_wait = 0;
795
796         while (!kthread_should_stop() || have_to_wait) {
797                 have_to_wait = 0;
798
799                 try_to_freeze();
800                 msleep_interruptible(w1_control_timeout * 1000);
801
802                 list_for_each_entry_safe(dev, n, &w1_masters, w1_master_entry) {
803                         if (!kthread_should_stop() && !dev->flags)
804                                 continue;
805                         /*
806                          * Little race: we can create thread but not set the flag.
807                          * Get a chance for external process to set flag up.
808                          */
809                         if (!dev->initialized) {
810                                 have_to_wait = 1;
811                                 continue;
812                         }
813
814                         if (kthread_should_stop() || test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {
815                                 set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
816
817                                 mutex_lock(&w1_mlock);
818                                 list_del(&dev->w1_master_entry);
819                                 mutex_unlock(&w1_mlock);
820
821                                 mutex_lock(&dev->mutex);
822                                 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
823                                         w1_slave_detach(sl);
824                                 }
825                                 w1_destroy_master_attributes(dev);
826                                 mutex_unlock(&dev->mutex);
827                                 atomic_dec(&dev->refcnt);
828                                 continue;
829                         }
830
831                         if (test_bit(W1_MASTER_NEED_RECONNECT, &dev->flags)) {
832                                 dev_dbg(&dev->dev, "Reconnecting slaves in device %s.\n", dev->name);
833                                 mutex_lock(&dev->mutex);
834                                 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
835                                         if (sl->family->fid == W1_FAMILY_DEFAULT) {
836                                                 struct w1_reg_num rn;
837
838                                                 memcpy(&rn, &sl->reg_num, sizeof(rn));
839                                                 w1_slave_detach(sl);
840
841                                                 w1_attach_slave_device(dev, &rn);
842                                         }
843                                 }
844                                 dev_dbg(&dev->dev, "Reconnecting slaves in device %s has been finished.\n", dev->name);
845                                 clear_bit(W1_MASTER_NEED_RECONNECT, &dev->flags);
846                                 mutex_unlock(&dev->mutex);
847                         }
848                 }
849         }
850
851         return 0;
852 }
853
854 void w1_search_process(struct w1_master *dev, u8 search_type)
855 {
856         struct w1_slave *sl, *sln;
857
858         list_for_each_entry(sl, &dev->slist, w1_slave_entry)
859                 clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
860
861         w1_search_devices(dev, search_type, w1_slave_found);
862
863         list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
864                 if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) {
865                         w1_slave_detach(sl);
866
867                         dev->slave_count--;
868                 } else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))
869                         sl->ttl = dev->slave_ttl;
870         }
871
872         if (dev->search_count > 0)
873                 dev->search_count--;
874 }
875
876 int w1_process(void *data)
877 {
878         struct w1_master *dev = (struct w1_master *) data;
879
880         while (!kthread_should_stop() && !test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {
881                 try_to_freeze();
882                 msleep_interruptible(w1_timeout * 1000);
883
884                 if (kthread_should_stop() || test_bit(W1_MASTER_NEED_EXIT, &dev->flags))
885                         break;
886
887                 if (!dev->initialized)
888                         continue;
889
890                 if (dev->search_count == 0)
891                         continue;
892
893                 mutex_lock(&dev->mutex);
894                 w1_search_process(dev, W1_SEARCH);
895                 mutex_unlock(&dev->mutex);
896         }
897
898         atomic_dec(&dev->refcnt);
899
900         return 0;
901 }
902
903 static int w1_init(void)
904 {
905         int retval;
906
907         printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
908
909         w1_init_netlink();
910
911         retval = bus_register(&w1_bus_type);
912         if (retval) {
913                 printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
914                 goto err_out_exit_init;
915         }
916
917         retval = driver_register(&w1_master_driver);
918         if (retval) {
919                 printk(KERN_ERR
920                         "Failed to register master driver. err=%d.\n",
921                         retval);
922                 goto err_out_bus_unregister;
923         }
924
925         retval = driver_register(&w1_slave_driver);
926         if (retval) {
927                 printk(KERN_ERR
928                         "Failed to register master driver. err=%d.\n",
929                         retval);
930                 goto err_out_master_unregister;
931         }
932
933         w1_control_thread = kthread_run(w1_control, NULL, "w1_control");
934         if (IS_ERR(w1_control_thread)) {
935                 retval = PTR_ERR(w1_control_thread);
936                 printk(KERN_ERR "Failed to create control thread. err=%d\n",
937                         retval);
938                 goto err_out_slave_unregister;
939         }
940
941         return 0;
942
943 err_out_slave_unregister:
944         driver_unregister(&w1_slave_driver);
945
946 err_out_master_unregister:
947         driver_unregister(&w1_master_driver);
948
949 err_out_bus_unregister:
950         bus_unregister(&w1_bus_type);
951
952 err_out_exit_init:
953         return retval;
954 }
955
956 static void w1_fini(void)
957 {
958         struct w1_master *dev;
959
960         list_for_each_entry(dev, &w1_masters, w1_master_entry)
961                 __w1_remove_master_device(dev);
962
963         w1_fini_netlink();
964
965         kthread_stop(w1_control_thread);
966
967         driver_unregister(&w1_slave_driver);
968         driver_unregister(&w1_master_driver);
969         bus_unregister(&w1_bus_type);
970 }
971
972 module_init(w1_init);
973 module_exit(w1_fini);