2 * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/delay.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/list.h>
20 #include <linux/interrupt.h>
21 #include <linux/spinlock.h>
22 #include <linux/timer.h>
23 #include <linux/device.h>
24 #include <linux/slab.h>
25 #include <linux/sched.h>
26 #include <linux/kthread.h>
27 #include <linux/freezer.h>
29 #include <linux/atomic.h>
33 #include "w1_family.h"
34 #include "w1_netlink.h"
36 MODULE_LICENSE("GPL");
37 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
38 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
40 static int w1_timeout = 10;
41 static int w1_timeout_us = 0;
42 int w1_max_slave_count = 64;
43 int w1_max_slave_ttl = 10;
45 module_param_named(timeout, w1_timeout, int, 0);
46 MODULE_PARM_DESC(timeout, "time in seconds between automatic slave searches");
47 module_param_named(timeout_us, w1_timeout_us, int, 0);
48 MODULE_PARM_DESC(timeout_us,
49 "time in microseconds between automatic slave searches");
50 /* A search stops when w1_max_slave_count devices have been found in that
51 * search. The next search will start over and detect the same set of devices
52 * on a static 1-wire bus. Memory is not allocated based on this number, just
53 * on the number of devices known to the kernel. Having a high number does not
54 * consume additional resources. As a special case, if there is only one
55 * device on the network and w1_max_slave_count is set to 1, the device id can
56 * be read directly skipping the normal slower search process.
58 module_param_named(max_slave_count, w1_max_slave_count, int, 0);
59 MODULE_PARM_DESC(max_slave_count,
60 "maximum number of slaves detected in a search");
61 module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
62 MODULE_PARM_DESC(slave_ttl,
63 "Number of searches not seeing a slave before it will be removed");
65 DEFINE_MUTEX(w1_mlock);
66 LIST_HEAD(w1_masters);
68 static int w1_master_match(struct device *dev, struct device_driver *drv)
73 static int w1_master_probe(struct device *dev)
78 static void w1_master_release(struct device *dev)
80 struct w1_master *md = dev_to_w1_master(dev);
82 dev_dbg(dev, "%s: Releasing %s.\n", __func__, md->name);
83 memset(md, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
87 static void w1_slave_release(struct device *dev)
89 struct w1_slave *sl = dev_to_w1_slave(dev);
91 dev_dbg(dev, "%s: Releasing %s [%p]\n", __func__, sl->name, sl);
93 w1_family_put(sl->family);
94 sl->master->slave_count--;
97 static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf)
99 struct w1_slave *sl = dev_to_w1_slave(dev);
101 return sprintf(buf, "%s\n", sl->name);
103 static DEVICE_ATTR_RO(name);
105 static ssize_t id_show(struct device *dev,
106 struct device_attribute *attr, char *buf)
108 struct w1_slave *sl = dev_to_w1_slave(dev);
109 ssize_t count = sizeof(sl->reg_num);
111 memcpy(buf, (u8 *)&sl->reg_num, count);
114 static DEVICE_ATTR_RO(id);
116 static struct attribute *w1_slave_attrs[] = {
121 ATTRIBUTE_GROUPS(w1_slave);
125 static ssize_t rw_write(struct file *filp, struct kobject *kobj,
126 struct bin_attribute *bin_attr, char *buf, loff_t off,
129 struct w1_slave *sl = kobj_to_w1_slave(kobj);
131 mutex_lock(&sl->master->mutex);
132 if (w1_reset_select_slave(sl)) {
137 w1_write_block(sl->master, buf, count);
140 mutex_unlock(&sl->master->mutex);
144 static ssize_t rw_read(struct file *filp, struct kobject *kobj,
145 struct bin_attribute *bin_attr, char *buf, loff_t off,
148 struct w1_slave *sl = kobj_to_w1_slave(kobj);
150 mutex_lock(&sl->master->mutex);
151 w1_read_block(sl->master, buf, count);
152 mutex_unlock(&sl->master->mutex);
156 static BIN_ATTR_RW(rw, PAGE_SIZE);
158 static struct bin_attribute *w1_slave_bin_attrs[] = {
163 static const struct attribute_group w1_slave_default_group = {
164 .bin_attrs = w1_slave_bin_attrs,
167 static const struct attribute_group *w1_slave_default_groups[] = {
168 &w1_slave_default_group,
172 static struct w1_family_ops w1_default_fops = {
173 .groups = w1_slave_default_groups,
176 static struct w1_family w1_default_family = {
177 .fops = &w1_default_fops,
180 static int w1_uevent(struct device *dev, struct kobj_uevent_env *env);
182 static struct bus_type w1_bus_type = {
184 .match = w1_master_match,
188 struct device_driver w1_master_driver = {
189 .name = "w1_master_driver",
191 .probe = w1_master_probe,
194 struct device w1_master_device = {
197 .init_name = "w1 bus master",
198 .driver = &w1_master_driver,
199 .release = &w1_master_release
202 static struct device_driver w1_slave_driver = {
203 .name = "w1_slave_driver",
208 struct device w1_slave_device = {
211 .init_name = "w1 bus slave",
212 .driver = &w1_slave_driver,
213 .release = &w1_slave_release
217 static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
219 struct w1_master *md = dev_to_w1_master(dev);
222 mutex_lock(&md->mutex);
223 count = sprintf(buf, "%s\n", md->name);
224 mutex_unlock(&md->mutex);
229 static ssize_t w1_master_attribute_store_search(struct device * dev,
230 struct device_attribute *attr,
231 const char * buf, size_t count)
234 struct w1_master *md = dev_to_w1_master(dev);
237 ret = kstrtol(buf, 0, &tmp);
241 mutex_lock(&md->mutex);
242 md->search_count = tmp;
243 mutex_unlock(&md->mutex);
244 /* Only wake if it is going to be searching. */
246 wake_up_process(md->thread);
251 static ssize_t w1_master_attribute_show_search(struct device *dev,
252 struct device_attribute *attr,
255 struct w1_master *md = dev_to_w1_master(dev);
258 mutex_lock(&md->mutex);
259 count = sprintf(buf, "%d\n", md->search_count);
260 mutex_unlock(&md->mutex);
265 static ssize_t w1_master_attribute_store_pullup(struct device *dev,
266 struct device_attribute *attr,
267 const char *buf, size_t count)
270 struct w1_master *md = dev_to_w1_master(dev);
273 ret = kstrtol(buf, 0, &tmp);
277 mutex_lock(&md->mutex);
278 md->enable_pullup = tmp;
279 mutex_unlock(&md->mutex);
284 static ssize_t w1_master_attribute_show_pullup(struct device *dev,
285 struct device_attribute *attr,
288 struct w1_master *md = dev_to_w1_master(dev);
291 mutex_lock(&md->mutex);
292 count = sprintf(buf, "%d\n", md->enable_pullup);
293 mutex_unlock(&md->mutex);
298 static ssize_t w1_master_attribute_show_pointer(struct device *dev, struct device_attribute *attr, char *buf)
300 struct w1_master *md = dev_to_w1_master(dev);
303 mutex_lock(&md->mutex);
304 count = sprintf(buf, "0x%p\n", md->bus_master);
305 mutex_unlock(&md->mutex);
309 static ssize_t w1_master_attribute_show_timeout(struct device *dev, struct device_attribute *attr, char *buf)
312 count = sprintf(buf, "%d\n", w1_timeout);
316 static ssize_t w1_master_attribute_show_timeout_us(struct device *dev,
317 struct device_attribute *attr, char *buf)
320 count = sprintf(buf, "%d\n", w1_timeout_us);
324 static ssize_t w1_master_attribute_store_max_slave_count(struct device *dev,
325 struct device_attribute *attr, const char *buf, size_t count)
328 struct w1_master *md = dev_to_w1_master(dev);
330 if (kstrtoint(buf, 0, &tmp) || tmp < 1)
333 mutex_lock(&md->mutex);
334 md->max_slave_count = tmp;
335 /* allow each time the max_slave_count is updated */
336 clear_bit(W1_WARN_MAX_COUNT, &md->flags);
337 mutex_unlock(&md->mutex);
342 static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
344 struct w1_master *md = dev_to_w1_master(dev);
347 mutex_lock(&md->mutex);
348 count = sprintf(buf, "%d\n", md->max_slave_count);
349 mutex_unlock(&md->mutex);
353 static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf)
355 struct w1_master *md = dev_to_w1_master(dev);
358 mutex_lock(&md->mutex);
359 count = sprintf(buf, "%lu\n", md->attempts);
360 mutex_unlock(&md->mutex);
364 static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
366 struct w1_master *md = dev_to_w1_master(dev);
369 mutex_lock(&md->mutex);
370 count = sprintf(buf, "%d\n", md->slave_count);
371 mutex_unlock(&md->mutex);
375 static ssize_t w1_master_attribute_show_slaves(struct device *dev,
376 struct device_attribute *attr, char *buf)
378 struct w1_master *md = dev_to_w1_master(dev);
380 struct list_head *ent, *n;
381 struct w1_slave *sl = NULL;
383 mutex_lock(&md->list_mutex);
385 list_for_each_safe(ent, n, &md->slist) {
386 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
388 c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
391 c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
393 mutex_unlock(&md->list_mutex);
395 return PAGE_SIZE - c;
398 static ssize_t w1_master_attribute_show_add(struct device *dev,
399 struct device_attribute *attr, char *buf)
402 c -= snprintf(buf+PAGE_SIZE - c, c,
403 "write device id xx-xxxxxxxxxxxx to add slave\n");
404 return PAGE_SIZE - c;
407 static int w1_atoreg_num(struct device *dev, const char *buf, size_t count,
408 struct w1_reg_num *rn)
411 unsigned long long id;
415 /* The CRC value isn't read from the user because the sysfs directory
416 * doesn't include it and most messages from the bus search don't
417 * print it either. It would be unreasonable for the user to then
420 const char *error_msg = "bad slave string format, expecting "
424 dev_err(dev, "%s", error_msg);
427 i = sscanf(buf, "%02x-%012llx", &family, &id);
429 dev_err(dev, "%s", error_msg);
435 rn64_le = cpu_to_le64(*(u64 *)rn);
436 rn->crc = w1_calc_crc8((u8 *)&rn64_le, 7);
439 dev_info(dev, "With CRC device is %02x.%012llx.%02x.\n",
440 rn->family, (unsigned long long)rn->id, rn->crc);
446 /* Searches the slaves in the w1_master and returns a pointer or NULL.
447 * Note: must not hold list_mutex
449 struct w1_slave *w1_slave_search_device(struct w1_master *dev,
450 struct w1_reg_num *rn)
453 mutex_lock(&dev->list_mutex);
454 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
455 if (sl->reg_num.family == rn->family &&
456 sl->reg_num.id == rn->id &&
457 sl->reg_num.crc == rn->crc) {
458 mutex_unlock(&dev->list_mutex);
462 mutex_unlock(&dev->list_mutex);
466 static ssize_t w1_master_attribute_store_add(struct device *dev,
467 struct device_attribute *attr,
468 const char *buf, size_t count)
470 struct w1_master *md = dev_to_w1_master(dev);
471 struct w1_reg_num rn;
473 ssize_t result = count;
475 if (w1_atoreg_num(dev, buf, count, &rn))
478 mutex_lock(&md->mutex);
479 sl = w1_slave_search_device(md, &rn);
480 /* It would be nice to do a targeted search one the one-wire bus
481 * for the new device to see if it is out there or not. But the
482 * current search doesn't support that.
485 dev_info(dev, "Device %s already exists\n", sl->name);
488 w1_attach_slave_device(md, &rn);
490 mutex_unlock(&md->mutex);
495 static ssize_t w1_master_attribute_show_remove(struct device *dev,
496 struct device_attribute *attr, char *buf)
499 c -= snprintf(buf+PAGE_SIZE - c, c,
500 "write device id xx-xxxxxxxxxxxx to remove slave\n");
501 return PAGE_SIZE - c;
504 static ssize_t w1_master_attribute_store_remove(struct device *dev,
505 struct device_attribute *attr,
506 const char *buf, size_t count)
508 struct w1_master *md = dev_to_w1_master(dev);
509 struct w1_reg_num rn;
511 ssize_t result = count;
513 if (w1_atoreg_num(dev, buf, count, &rn))
516 mutex_lock(&md->mutex);
517 sl = w1_slave_search_device(md, &rn);
519 result = w1_slave_detach(sl);
520 /* refcnt 0 means it was detached in the call */
524 dev_info(dev, "Device %02x-%012llx doesn't exists\n", rn.family,
525 (unsigned long long)rn.id);
528 mutex_unlock(&md->mutex);
533 #define W1_MASTER_ATTR_RO(_name, _mode) \
534 struct device_attribute w1_master_attribute_##_name = \
535 __ATTR(w1_master_##_name, _mode, \
536 w1_master_attribute_show_##_name, NULL)
538 #define W1_MASTER_ATTR_RW(_name, _mode) \
539 struct device_attribute w1_master_attribute_##_name = \
540 __ATTR(w1_master_##_name, _mode, \
541 w1_master_attribute_show_##_name, \
542 w1_master_attribute_store_##_name)
544 static W1_MASTER_ATTR_RO(name, S_IRUGO);
545 static W1_MASTER_ATTR_RO(slaves, S_IRUGO);
546 static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);
547 static W1_MASTER_ATTR_RW(max_slave_count, S_IRUGO | S_IWUSR | S_IWGRP);
548 static W1_MASTER_ATTR_RO(attempts, S_IRUGO);
549 static W1_MASTER_ATTR_RO(timeout, S_IRUGO);
550 static W1_MASTER_ATTR_RO(timeout_us, S_IRUGO);
551 static W1_MASTER_ATTR_RO(pointer, S_IRUGO);
552 static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUSR | S_IWGRP);
553 static W1_MASTER_ATTR_RW(pullup, S_IRUGO | S_IWUSR | S_IWGRP);
554 static W1_MASTER_ATTR_RW(add, S_IRUGO | S_IWUSR | S_IWGRP);
555 static W1_MASTER_ATTR_RW(remove, S_IRUGO | S_IWUSR | S_IWGRP);
557 static struct attribute *w1_master_default_attrs[] = {
558 &w1_master_attribute_name.attr,
559 &w1_master_attribute_slaves.attr,
560 &w1_master_attribute_slave_count.attr,
561 &w1_master_attribute_max_slave_count.attr,
562 &w1_master_attribute_attempts.attr,
563 &w1_master_attribute_timeout.attr,
564 &w1_master_attribute_timeout_us.attr,
565 &w1_master_attribute_pointer.attr,
566 &w1_master_attribute_search.attr,
567 &w1_master_attribute_pullup.attr,
568 &w1_master_attribute_add.attr,
569 &w1_master_attribute_remove.attr,
573 static struct attribute_group w1_master_defattr_group = {
574 .attrs = w1_master_default_attrs,
577 int w1_create_master_attributes(struct w1_master *master)
579 return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
582 void w1_destroy_master_attributes(struct w1_master *master)
584 sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
587 static int w1_uevent(struct device *dev, struct kobj_uevent_env *env)
589 struct w1_master *md = NULL;
590 struct w1_slave *sl = NULL;
591 char *event_owner, *name;
594 if (dev->driver == &w1_master_driver) {
595 md = container_of(dev, struct w1_master, dev);
596 event_owner = "master";
598 } else if (dev->driver == &w1_slave_driver) {
599 sl = container_of(dev, struct w1_slave, dev);
600 event_owner = "slave";
603 dev_dbg(dev, "Unknown event.\n");
607 dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n",
608 event_owner, name, dev_name(dev));
610 if (dev->driver != &w1_slave_driver || !sl)
613 err = add_uevent_var(env, "W1_FID=%02X", sl->reg_num.family);
617 err = add_uevent_var(env, "W1_SLAVE_ID=%024LX",
618 (unsigned long long)sl->reg_num.id);
623 static int w1_family_notify(unsigned long action, struct w1_slave *sl)
625 struct w1_family_ops *fops;
628 fops = sl->family->fops;
634 case BUS_NOTIFY_ADD_DEVICE:
635 /* if the family driver needs to initialize something... */
636 if (fops->add_slave) {
637 err = fops->add_slave(sl);
640 "add_slave() call failed. err=%d\n",
646 err = sysfs_create_groups(&sl->dev.kobj, fops->groups);
649 "sysfs group creation failed. err=%d\n",
656 case BUS_NOTIFY_DEL_DEVICE:
657 if (fops->remove_slave)
658 sl->family->fops->remove_slave(sl);
660 sysfs_remove_groups(&sl->dev.kobj, fops->groups);
666 static int __w1_attach_slave_device(struct w1_slave *sl)
670 sl->dev.parent = &sl->master->dev;
671 sl->dev.driver = &w1_slave_driver;
672 sl->dev.bus = &w1_bus_type;
673 sl->dev.release = &w1_slave_release;
674 sl->dev.groups = w1_slave_groups;
676 dev_set_name(&sl->dev, "%02x-%012llx",
677 (unsigned int) sl->reg_num.family,
678 (unsigned long long) sl->reg_num.id);
679 snprintf(&sl->name[0], sizeof(sl->name),
681 (unsigned int) sl->reg_num.family,
682 (unsigned long long) sl->reg_num.id);
684 dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__,
685 dev_name(&sl->dev), sl);
687 /* suppress for w1_family_notify before sending KOBJ_ADD */
688 dev_set_uevent_suppress(&sl->dev, true);
690 err = device_register(&sl->dev);
693 "Device registration [%s] failed. err=%d\n",
694 dev_name(&sl->dev), err);
697 w1_family_notify(BUS_NOTIFY_ADD_DEVICE, sl);
699 dev_set_uevent_suppress(&sl->dev, false);
700 kobject_uevent(&sl->dev.kobj, KOBJ_ADD);
702 mutex_lock(&sl->master->list_mutex);
703 list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
704 mutex_unlock(&sl->master->list_mutex);
709 int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
714 struct w1_netlink_msg msg;
716 sl = kzalloc(sizeof(struct w1_slave), GFP_KERNEL);
719 "%s: failed to allocate new slave device.\n",
725 sl->owner = THIS_MODULE;
727 set_bit(W1_SLAVE_ACTIVE, &sl->flags);
729 memset(&msg, 0, sizeof(msg));
730 memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
731 atomic_set(&sl->refcnt, 1);
732 atomic_inc(&sl->master->refcnt);
734 /* slave modules need to be loaded in a context with unlocked mutex */
735 mutex_unlock(&dev->mutex);
736 request_module("w1-family-0x%02x", rn->family);
737 mutex_lock(&dev->mutex);
739 spin_lock(&w1_flock);
740 f = w1_family_registered(rn->family);
742 f= &w1_default_family;
743 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
744 rn->family, rn->family,
745 (unsigned long long)rn->id, rn->crc);
748 spin_unlock(&w1_flock);
753 err = __w1_attach_slave_device(sl);
755 dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
757 w1_family_put(sl->family);
758 atomic_dec(&sl->master->refcnt);
763 sl->ttl = dev->slave_ttl;
766 memcpy(msg.id.id, rn, sizeof(msg.id));
767 msg.type = W1_SLAVE_ADD;
768 w1_netlink_send(dev, &msg);
773 int w1_unref_slave(struct w1_slave *sl)
775 struct w1_master *dev = sl->master;
777 mutex_lock(&dev->list_mutex);
778 refcnt = atomic_sub_return(1, &sl->refcnt);
780 struct w1_netlink_msg msg;
782 dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__,
785 list_del(&sl->w1_slave_entry);
787 memset(&msg, 0, sizeof(msg));
788 memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
789 msg.type = W1_SLAVE_REMOVE;
790 w1_netlink_send(sl->master, &msg);
792 w1_family_notify(BUS_NOTIFY_DEL_DEVICE, sl);
793 device_unregister(&sl->dev);
795 memset(sl, 0, sizeof(*sl));
799 atomic_dec(&dev->refcnt);
800 mutex_unlock(&dev->list_mutex);
804 int w1_slave_detach(struct w1_slave *sl)
806 /* Only detach a slave once as it decreases the refcnt each time. */
808 mutex_lock(&sl->master->list_mutex);
809 destroy_now = !test_bit(W1_SLAVE_DETACH, &sl->flags);
810 set_bit(W1_SLAVE_DETACH, &sl->flags);
811 mutex_unlock(&sl->master->list_mutex);
814 destroy_now = !w1_unref_slave(sl);
815 return destroy_now ? 0 : -EBUSY;
818 struct w1_master *w1_search_master_id(u32 id)
820 struct w1_master *dev;
823 mutex_lock(&w1_mlock);
824 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
827 atomic_inc(&dev->refcnt);
831 mutex_unlock(&w1_mlock);
833 return (found)?dev:NULL;
836 struct w1_slave *w1_search_slave(struct w1_reg_num *id)
838 struct w1_master *dev;
839 struct w1_slave *sl = NULL;
842 mutex_lock(&w1_mlock);
843 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
844 mutex_lock(&dev->list_mutex);
845 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
846 if (sl->reg_num.family == id->family &&
847 sl->reg_num.id == id->id &&
848 sl->reg_num.crc == id->crc) {
850 atomic_inc(&dev->refcnt);
851 atomic_inc(&sl->refcnt);
855 mutex_unlock(&dev->list_mutex);
860 mutex_unlock(&w1_mlock);
862 return (found)?sl:NULL;
865 void w1_reconnect_slaves(struct w1_family *f, int attach)
867 struct w1_slave *sl, *sln;
868 struct w1_master *dev;
870 mutex_lock(&w1_mlock);
871 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
872 dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
873 "for family %02x.\n", dev->name, f->fid);
874 mutex_lock(&dev->mutex);
875 mutex_lock(&dev->list_mutex);
876 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
877 /* If it is a new family, slaves with the default
878 * family driver and are that family will be
879 * connected. If the family is going away, devices
880 * matching that family are reconneced.
882 if ((attach && sl->family->fid == W1_FAMILY_DEFAULT
883 && sl->reg_num.family == f->fid) ||
884 (!attach && sl->family->fid == f->fid)) {
885 struct w1_reg_num rn;
887 mutex_unlock(&dev->list_mutex);
888 memcpy(&rn, &sl->reg_num, sizeof(rn));
889 /* If it was already in use let the automatic
890 * scan pick it up again later.
892 if (!w1_slave_detach(sl))
893 w1_attach_slave_device(dev, &rn);
894 mutex_lock(&dev->list_mutex);
897 dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
898 "has been finished.\n", dev->name);
899 mutex_unlock(&dev->list_mutex);
900 mutex_unlock(&dev->mutex);
902 mutex_unlock(&w1_mlock);
905 void w1_slave_found(struct w1_master *dev, u64 rn)
908 struct w1_reg_num *tmp;
909 u64 rn_le = cpu_to_le64(rn);
911 atomic_inc(&dev->refcnt);
913 tmp = (struct w1_reg_num *) &rn;
915 sl = w1_slave_search_device(dev, tmp);
917 set_bit(W1_SLAVE_ACTIVE, &sl->flags);
919 if (rn && tmp->crc == w1_calc_crc8((u8 *)&rn_le, 7))
920 w1_attach_slave_device(dev, tmp);
923 atomic_dec(&dev->refcnt);
927 * w1_search() - Performs a ROM Search & registers any devices found.
928 * @dev: The master device to search
929 * @search_type: W1_SEARCH to search all devices, or W1_ALARM_SEARCH
930 * to return only devices in the alarmed state
931 * @cb: Function to call when a device is found
933 * The 1-wire search is a simple binary tree search.
934 * For each bit of the address, we read two bits and write one bit.
935 * The bit written will put to sleep all devies that don't match that bit.
936 * When the two reads differ, the direction choice is obvious.
937 * When both bits are 0, we must choose a path to take.
938 * When we can scan all 64 bits without having to choose a path, we are done.
940 * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
943 void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
945 u64 last_rn, rn, tmp64;
946 int i, slave_count = 0;
947 int last_zero, last_device;
948 int search_bit, desc_bit;
959 while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
964 * Reset bus and all 1-wire device state machines
965 * so they can respond to our requests.
967 * Return 0 - device(s) present, 1 - no devices present.
969 mutex_lock(&dev->bus_mutex);
970 if (w1_reset_bus(dev)) {
971 mutex_unlock(&dev->bus_mutex);
972 dev_dbg(&dev->dev, "No devices present on the wire.\n");
976 /* Do fast search on single slave bus */
977 if (dev->max_slave_count == 1) {
979 w1_write_8(dev, W1_READ_ROM);
980 rv = w1_read_block(dev, (u8 *)&rn, 8);
981 mutex_unlock(&dev->bus_mutex);
989 /* Start the search */
990 w1_write_8(dev, search_type);
991 for (i = 0; i < 64; ++i) {
992 /* Determine the direction/search bit */
994 search_bit = 1; /* took the 0 path last time, so take the 1 path */
995 else if (i > desc_bit)
996 search_bit = 0; /* take the 0 path on the next branch */
998 search_bit = ((last_rn >> i) & 0x1);
1000 /* Read two bits and write one bit */
1001 triplet_ret = w1_triplet(dev, search_bit);
1003 /* quit if no device responded */
1004 if ( (triplet_ret & 0x03) == 0x03 )
1007 /* If both directions were valid, and we took the 0 path... */
1008 if (triplet_ret == 0)
1011 /* extract the direction taken & update the device number */
1012 tmp64 = (triplet_ret >> 2);
1015 if (test_bit(W1_ABORT_SEARCH, &dev->flags)) {
1016 mutex_unlock(&dev->bus_mutex);
1017 dev_dbg(&dev->dev, "Abort w1_search\n");
1021 mutex_unlock(&dev->bus_mutex);
1023 if ( (triplet_ret & 0x03) != 0x03 ) {
1024 if ((desc_bit == last_zero) || (last_zero < 0)) {
1028 dev->search_id = rn;
1030 desc_bit = last_zero;
1034 if (!last_device && slave_count == dev->max_slave_count &&
1035 !test_bit(W1_WARN_MAX_COUNT, &dev->flags)) {
1036 /* Only max_slave_count will be scanned in a search,
1037 * but it will start where it left off next search
1038 * until all ids are identified and then it will start
1039 * over. A continued search will report the previous
1040 * last id as the first id (provided it is still on the
1043 dev_info(&dev->dev, "%s: max_slave_count %d reached, "
1044 "will continue next search.\n", __func__,
1045 dev->max_slave_count);
1046 set_bit(W1_WARN_MAX_COUNT, &dev->flags);
1051 void w1_search_process_cb(struct w1_master *dev, u8 search_type,
1052 w1_slave_found_callback cb)
1054 struct w1_slave *sl, *sln;
1056 mutex_lock(&dev->list_mutex);
1057 list_for_each_entry(sl, &dev->slist, w1_slave_entry)
1058 clear_bit(W1_SLAVE_ACTIVE, &sl->flags);
1059 mutex_unlock(&dev->list_mutex);
1061 w1_search_devices(dev, search_type, cb);
1063 mutex_lock(&dev->list_mutex);
1064 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
1065 if (!test_bit(W1_SLAVE_ACTIVE, &sl->flags) && !--sl->ttl) {
1066 mutex_unlock(&dev->list_mutex);
1067 w1_slave_detach(sl);
1068 mutex_lock(&dev->list_mutex);
1070 else if (test_bit(W1_SLAVE_ACTIVE, &sl->flags))
1071 sl->ttl = dev->slave_ttl;
1073 mutex_unlock(&dev->list_mutex);
1075 if (dev->search_count > 0)
1076 dev->search_count--;
1079 static void w1_search_process(struct w1_master *dev, u8 search_type)
1081 w1_search_process_cb(dev, search_type, w1_slave_found);
1085 * w1_process_callbacks() - execute each dev->async_list callback entry
1086 * @dev: w1_master device
1088 * The w1 master list_mutex must be held.
1090 * Return: 1 if there were commands to executed 0 otherwise
1092 int w1_process_callbacks(struct w1_master *dev)
1095 struct w1_async_cmd *async_cmd, *async_n;
1097 /* The list can be added to in another thread, loop until it is empty */
1098 while (!list_empty(&dev->async_list)) {
1099 list_for_each_entry_safe(async_cmd, async_n, &dev->async_list,
1101 /* drop the lock, if it is a search it can take a long
1103 mutex_unlock(&dev->list_mutex);
1104 async_cmd->cb(dev, async_cmd);
1106 mutex_lock(&dev->list_mutex);
1112 int w1_process(void *data)
1114 struct w1_master *dev = (struct w1_master *) data;
1115 /* As long as w1_timeout is only set by a module parameter the sleep
1116 * time can be calculated in jiffies once.
1118 const unsigned long jtime =
1119 usecs_to_jiffies(w1_timeout * 1000000 + w1_timeout_us);
1120 /* remainder if it woke up early */
1121 unsigned long jremain = 0;
1125 if (!jremain && dev->search_count) {
1126 mutex_lock(&dev->mutex);
1127 w1_search_process(dev, W1_SEARCH);
1128 mutex_unlock(&dev->mutex);
1131 mutex_lock(&dev->list_mutex);
1132 /* Note, w1_process_callback drops the lock while processing,
1133 * but locks it again before returning.
1135 if (!w1_process_callbacks(dev) && jremain) {
1136 /* a wake up is either to stop the thread, process
1137 * callbacks, or search, it isn't process callbacks, so
1138 * schedule a search.
1143 __set_current_state(TASK_INTERRUPTIBLE);
1145 /* hold list_mutex until after interruptible to prevent loosing
1146 * the wakeup signal when async_cmd is added.
1148 mutex_unlock(&dev->list_mutex);
1150 if (kthread_should_stop())
1153 /* Only sleep when the search is active. */
1154 if (dev->search_count) {
1157 jremain = schedule_timeout(jremain);
1163 atomic_dec(&dev->refcnt);
1168 static int __init w1_init(void)
1172 pr_info("Driver for 1-wire Dallas network protocol.\n");
1176 retval = bus_register(&w1_bus_type);
1178 pr_err("Failed to register bus. err=%d.\n", retval);
1179 goto err_out_exit_init;
1182 retval = driver_register(&w1_master_driver);
1184 pr_err("Failed to register master driver. err=%d.\n",
1186 goto err_out_bus_unregister;
1189 retval = driver_register(&w1_slave_driver);
1191 pr_err("Failed to register slave driver. err=%d.\n",
1193 goto err_out_master_unregister;
1199 /* For undoing the slave register if there was a step after it. */
1200 err_out_slave_unregister:
1201 driver_unregister(&w1_slave_driver);
1204 err_out_master_unregister:
1205 driver_unregister(&w1_master_driver);
1207 err_out_bus_unregister:
1208 bus_unregister(&w1_bus_type);
1214 static void __exit w1_fini(void)
1216 struct w1_master *dev;
1218 /* Set netlink removal messages and some cleanup */
1219 list_for_each_entry(dev, &w1_masters, w1_master_entry)
1220 __w1_remove_master_device(dev);
1224 driver_unregister(&w1_slave_driver);
1225 driver_unregister(&w1_master_driver);
1226 bus_unregister(&w1_bus_type);
1229 module_init(w1_init);
1230 module_exit(w1_fini);