4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
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.
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.
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
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 #include <linux/freezer.h>
36 #include <asm/atomic.h>
41 #include "w1_family.h"
42 #include "w1_netlink.h"
44 MODULE_LICENSE("GPL");
45 MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
46 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
48 static int w1_timeout = 10;
49 int w1_max_slave_count = 10;
50 int w1_max_slave_ttl = 10;
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);
56 DEFINE_MUTEX(w1_mlock);
57 LIST_HEAD(w1_masters);
59 static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn);
61 static int w1_master_match(struct device *dev, struct device_driver *drv)
66 static int w1_master_probe(struct device *dev)
71 static void w1_master_release(struct device *dev)
73 struct w1_master *md = dev_to_w1_master(dev);
75 dev_dbg(dev, "%s: Releasing %s.\n", __func__, md->name);
76 memset(md, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
80 static void w1_slave_release(struct device *dev)
82 struct w1_slave *sl = dev_to_w1_slave(dev);
84 printk("%s: Releasing %s.\n", __func__, sl->name);
86 while (atomic_read(&sl->refcnt)) {
87 printk("Waiting for %s to become free: refcnt=%d.\n",
88 sl->name, atomic_read(&sl->refcnt));
89 if (msleep_interruptible(1000))
90 flush_signals(current);
93 w1_family_put(sl->family);
94 sl->master->slave_count--;
96 complete(&sl->released);
99 static ssize_t w1_slave_read_name(struct device *dev, struct device_attribute *attr, char *buf)
101 struct w1_slave *sl = dev_to_w1_slave(dev);
103 return sprintf(buf, "%s\n", sl->name);
106 static ssize_t w1_slave_read_id(struct kobject *kobj,
107 struct bin_attribute *bin_attr,
108 char *buf, loff_t off, size_t count)
110 struct w1_slave *sl = kobj_to_w1_slave(kobj);
118 memcpy(buf, (u8 *)&sl->reg_num, count);
124 static struct device_attribute w1_slave_attr_name =
125 __ATTR(name, S_IRUGO, w1_slave_read_name, NULL);
127 static struct bin_attribute w1_slave_attr_bin_id = {
133 .read = w1_slave_read_id,
138 static ssize_t w1_default_write(struct kobject *kobj,
139 struct bin_attribute *bin_attr,
140 char *buf, loff_t off, size_t count)
142 struct w1_slave *sl = kobj_to_w1_slave(kobj);
144 mutex_lock(&sl->master->mutex);
145 if (w1_reset_select_slave(sl)) {
150 w1_write_block(sl->master, buf, count);
153 mutex_unlock(&sl->master->mutex);
157 static ssize_t w1_default_read(struct kobject *kobj,
158 struct bin_attribute *bin_attr,
159 char *buf, loff_t off, size_t count)
161 struct w1_slave *sl = kobj_to_w1_slave(kobj);
163 mutex_lock(&sl->master->mutex);
164 w1_read_block(sl->master, buf, count);
165 mutex_unlock(&sl->master->mutex);
169 static struct bin_attribute w1_default_attr = {
172 .mode = S_IRUGO | S_IWUSR,
175 .read = w1_default_read,
176 .write = w1_default_write,
179 static int w1_default_add_slave(struct w1_slave *sl)
181 return sysfs_create_bin_file(&sl->dev.kobj, &w1_default_attr);
184 static void w1_default_remove_slave(struct w1_slave *sl)
186 sysfs_remove_bin_file(&sl->dev.kobj, &w1_default_attr);
189 static struct w1_family_ops w1_default_fops = {
190 .add_slave = w1_default_add_slave,
191 .remove_slave = w1_default_remove_slave,
194 static struct w1_family w1_default_family = {
195 .fops = &w1_default_fops,
198 static int w1_uevent(struct device *dev, struct kobj_uevent_env *env);
200 static struct bus_type w1_bus_type = {
202 .match = w1_master_match,
206 struct device_driver w1_master_driver = {
207 .name = "w1_master_driver",
209 .probe = w1_master_probe,
212 struct device w1_master_device = {
215 .bus_id = "w1 bus master",
216 .driver = &w1_master_driver,
217 .release = &w1_master_release
220 static struct device_driver w1_slave_driver = {
221 .name = "w1_slave_driver",
226 struct device w1_slave_device = {
229 .bus_id = "w1 bus slave",
230 .driver = &w1_slave_driver,
231 .release = &w1_slave_release
235 static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
237 struct w1_master *md = dev_to_w1_master(dev);
240 mutex_lock(&md->mutex);
241 count = sprintf(buf, "%s\n", md->name);
242 mutex_unlock(&md->mutex);
247 static ssize_t w1_master_attribute_store_search(struct device * dev,
248 struct device_attribute *attr,
249 const char * buf, size_t count)
252 struct w1_master *md = dev_to_w1_master(dev);
254 if (strict_strtol(buf, 0, &tmp) == -EINVAL)
257 mutex_lock(&md->mutex);
258 md->search_count = tmp;
259 mutex_unlock(&md->mutex);
260 wake_up_process(md->thread);
265 static ssize_t w1_master_attribute_show_search(struct device *dev,
266 struct device_attribute *attr,
269 struct w1_master *md = dev_to_w1_master(dev);
272 mutex_lock(&md->mutex);
273 count = sprintf(buf, "%d\n", md->search_count);
274 mutex_unlock(&md->mutex);
279 static ssize_t w1_master_attribute_store_pullup(struct device *dev,
280 struct device_attribute *attr,
281 const char *buf, size_t count)
284 struct w1_master *md = dev_to_w1_master(dev);
286 if (strict_strtol(buf, 0, &tmp) == -EINVAL)
289 mutex_lock(&md->mutex);
290 md->enable_pullup = tmp;
291 mutex_unlock(&md->mutex);
292 wake_up_process(md->thread);
297 static ssize_t w1_master_attribute_show_pullup(struct device *dev,
298 struct device_attribute *attr,
301 struct w1_master *md = dev_to_w1_master(dev);
304 mutex_lock(&md->mutex);
305 count = sprintf(buf, "%d\n", md->enable_pullup);
306 mutex_unlock(&md->mutex);
311 static ssize_t w1_master_attribute_show_pointer(struct device *dev, struct device_attribute *attr, char *buf)
313 struct w1_master *md = dev_to_w1_master(dev);
316 mutex_lock(&md->mutex);
317 count = sprintf(buf, "0x%p\n", md->bus_master);
318 mutex_unlock(&md->mutex);
322 static ssize_t w1_master_attribute_show_timeout(struct device *dev, struct device_attribute *attr, char *buf)
325 count = sprintf(buf, "%d\n", w1_timeout);
329 static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
331 struct w1_master *md = dev_to_w1_master(dev);
334 mutex_lock(&md->mutex);
335 count = sprintf(buf, "%d\n", md->max_slave_count);
336 mutex_unlock(&md->mutex);
340 static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf)
342 struct w1_master *md = dev_to_w1_master(dev);
345 mutex_lock(&md->mutex);
346 count = sprintf(buf, "%lu\n", md->attempts);
347 mutex_unlock(&md->mutex);
351 static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
353 struct w1_master *md = dev_to_w1_master(dev);
356 mutex_lock(&md->mutex);
357 count = sprintf(buf, "%d\n", md->slave_count);
358 mutex_unlock(&md->mutex);
362 static ssize_t w1_master_attribute_show_slaves(struct device *dev,
363 struct device_attribute *attr, char *buf)
365 struct w1_master *md = dev_to_w1_master(dev);
368 mutex_lock(&md->mutex);
370 if (md->slave_count == 0)
371 c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
373 struct list_head *ent, *n;
376 list_for_each_safe(ent, n, &md->slist) {
377 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
379 c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
383 mutex_unlock(&md->mutex);
385 return PAGE_SIZE - c;
388 static ssize_t w1_master_attribute_show_add(struct device *dev,
389 struct device_attribute *attr, char *buf)
392 c -= snprintf(buf+PAGE_SIZE - c, c,
393 "write device id xx-xxxxxxxxxxxx to add slave\n");
394 return PAGE_SIZE - c;
397 static int w1_atoreg_num(struct device *dev, const char *buf, size_t count,
398 struct w1_reg_num *rn)
401 unsigned long long id;
405 /* The CRC value isn't read from the user because the sysfs directory
406 * doesn't include it and most messages from the bus search don't
407 * print it either. It would be unreasonable for the user to then
410 const char *error_msg = "bad slave string format, expecting "
414 dev_err(dev, "%s", error_msg);
417 i = sscanf(buf, "%02x-%012llx", &family, &id);
419 dev_err(dev, "%s", error_msg);
425 rn64_le = cpu_to_le64(*(u64 *)rn);
426 rn->crc = w1_calc_crc8((u8 *)&rn64_le, 7);
429 dev_info(dev, "With CRC device is %02x.%012llx.%02x.\n",
430 rn->family, (unsigned long long)rn->id, rn->crc);
436 /* Searches the slaves in the w1_master and returns a pointer or NULL.
437 * Note: must hold the mutex
439 static struct w1_slave *w1_slave_search_device(struct w1_master *dev,
440 struct w1_reg_num *rn)
443 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
444 if (sl->reg_num.family == rn->family &&
445 sl->reg_num.id == rn->id &&
446 sl->reg_num.crc == rn->crc) {
453 static ssize_t w1_master_attribute_store_add(struct device *dev,
454 struct device_attribute *attr,
455 const char *buf, size_t count)
457 struct w1_master *md = dev_to_w1_master(dev);
458 struct w1_reg_num rn;
460 ssize_t result = count;
462 if (w1_atoreg_num(dev, buf, count, &rn))
465 mutex_lock(&md->mutex);
466 sl = w1_slave_search_device(md, &rn);
467 /* It would be nice to do a targeted search one the one-wire bus
468 * for the new device to see if it is out there or not. But the
469 * current search doesn't support that.
472 dev_info(dev, "Device %s already exists\n", sl->name);
475 w1_attach_slave_device(md, &rn);
477 mutex_unlock(&md->mutex);
482 static ssize_t w1_master_attribute_show_remove(struct device *dev,
483 struct device_attribute *attr, char *buf)
486 c -= snprintf(buf+PAGE_SIZE - c, c,
487 "write device id xx-xxxxxxxxxxxx to remove slave\n");
488 return PAGE_SIZE - c;
491 static ssize_t w1_master_attribute_store_remove(struct device *dev,
492 struct device_attribute *attr,
493 const char *buf, size_t count)
495 struct w1_master *md = dev_to_w1_master(dev);
496 struct w1_reg_num rn;
498 ssize_t result = count;
500 if (w1_atoreg_num(dev, buf, count, &rn))
503 mutex_lock(&md->mutex);
504 sl = w1_slave_search_device(md, &rn);
508 dev_info(dev, "Device %02x-%012llx doesn't exists\n", rn.family,
509 (unsigned long long)rn.id);
512 mutex_unlock(&md->mutex);
517 #define W1_MASTER_ATTR_RO(_name, _mode) \
518 struct device_attribute w1_master_attribute_##_name = \
519 __ATTR(w1_master_##_name, _mode, \
520 w1_master_attribute_show_##_name, NULL)
522 #define W1_MASTER_ATTR_RW(_name, _mode) \
523 struct device_attribute w1_master_attribute_##_name = \
524 __ATTR(w1_master_##_name, _mode, \
525 w1_master_attribute_show_##_name, \
526 w1_master_attribute_store_##_name)
528 static W1_MASTER_ATTR_RO(name, S_IRUGO);
529 static W1_MASTER_ATTR_RO(slaves, S_IRUGO);
530 static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);
531 static W1_MASTER_ATTR_RO(max_slave_count, S_IRUGO);
532 static W1_MASTER_ATTR_RO(attempts, S_IRUGO);
533 static W1_MASTER_ATTR_RO(timeout, S_IRUGO);
534 static W1_MASTER_ATTR_RO(pointer, S_IRUGO);
535 static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUGO);
536 static W1_MASTER_ATTR_RW(pullup, S_IRUGO | S_IWUGO);
537 static W1_MASTER_ATTR_RW(add, S_IRUGO | S_IWUGO);
538 static W1_MASTER_ATTR_RW(remove, S_IRUGO | S_IWUGO);
540 static struct attribute *w1_master_default_attrs[] = {
541 &w1_master_attribute_name.attr,
542 &w1_master_attribute_slaves.attr,
543 &w1_master_attribute_slave_count.attr,
544 &w1_master_attribute_max_slave_count.attr,
545 &w1_master_attribute_attempts.attr,
546 &w1_master_attribute_timeout.attr,
547 &w1_master_attribute_pointer.attr,
548 &w1_master_attribute_search.attr,
549 &w1_master_attribute_pullup.attr,
550 &w1_master_attribute_add.attr,
551 &w1_master_attribute_remove.attr,
555 static struct attribute_group w1_master_defattr_group = {
556 .attrs = w1_master_default_attrs,
559 int w1_create_master_attributes(struct w1_master *master)
561 return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
564 void w1_destroy_master_attributes(struct w1_master *master)
566 sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
569 #ifdef CONFIG_HOTPLUG
570 static int w1_uevent(struct device *dev, struct kobj_uevent_env *env)
572 struct w1_master *md = NULL;
573 struct w1_slave *sl = NULL;
574 char *event_owner, *name;
577 if (dev->driver == &w1_master_driver) {
578 md = container_of(dev, struct w1_master, dev);
579 event_owner = "master";
581 } else if (dev->driver == &w1_slave_driver) {
582 sl = container_of(dev, struct w1_slave, dev);
583 event_owner = "slave";
586 dev_dbg(dev, "Unknown event.\n");
590 dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n",
591 event_owner, name, dev->bus_id);
593 if (dev->driver != &w1_slave_driver || !sl)
596 err = add_uevent_var(env, "W1_FID=%02X", sl->reg_num.family);
600 err = add_uevent_var(env, "W1_SLAVE_ID=%024LX",
601 (unsigned long long)sl->reg_num.id);
608 static int w1_uevent(struct device *dev, struct kobj_uevent_env *env)
614 static int __w1_attach_slave_device(struct w1_slave *sl)
618 sl->dev.parent = &sl->master->dev;
619 sl->dev.driver = &w1_slave_driver;
620 sl->dev.bus = &w1_bus_type;
621 sl->dev.release = &w1_slave_release;
623 snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id),
625 (unsigned int) sl->reg_num.family,
626 (unsigned long long) sl->reg_num.id);
627 snprintf(&sl->name[0], sizeof(sl->name),
629 (unsigned int) sl->reg_num.family,
630 (unsigned long long) sl->reg_num.id);
632 dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__,
633 &sl->dev.bus_id[0], sl);
635 err = device_register(&sl->dev);
638 "Device registration [%s] failed. err=%d\n",
639 sl->dev.bus_id, err);
643 /* Create "name" entry */
644 err = device_create_file(&sl->dev, &w1_slave_attr_name);
647 "sysfs file creation for [%s] failed. err=%d\n",
648 sl->dev.bus_id, err);
652 /* Create "id" entry */
653 err = sysfs_create_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
656 "sysfs file creation for [%s] failed. err=%d\n",
657 sl->dev.bus_id, err);
661 /* if the family driver needs to initialize something... */
662 if (sl->family->fops && sl->family->fops->add_slave &&
663 ((err = sl->family->fops->add_slave(sl)) < 0)) {
665 "sysfs file creation for [%s] failed. err=%d\n",
666 sl->dev.bus_id, err);
670 list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
675 sysfs_remove_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
677 device_remove_file(&sl->dev, &w1_slave_attr_name);
679 device_unregister(&sl->dev);
683 static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
688 struct w1_netlink_msg msg;
690 sl = kzalloc(sizeof(struct w1_slave), GFP_KERNEL);
693 "%s: failed to allocate new slave device.\n",
699 sl->owner = THIS_MODULE;
701 set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
703 memset(&msg, 0, sizeof(msg));
704 memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
705 atomic_set(&sl->refcnt, 0);
706 init_completion(&sl->released);
708 spin_lock(&w1_flock);
709 f = w1_family_registered(rn->family);
711 f= &w1_default_family;
712 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
713 rn->family, rn->family,
714 (unsigned long long)rn->id, rn->crc);
717 spin_unlock(&w1_flock);
722 err = __w1_attach_slave_device(sl);
724 dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
726 w1_family_put(sl->family);
731 sl->ttl = dev->slave_ttl;
734 memcpy(msg.id.id, rn, sizeof(msg.id));
735 msg.type = W1_SLAVE_ADD;
736 w1_netlink_send(dev, &msg);
741 void w1_slave_detach(struct w1_slave *sl)
743 struct w1_netlink_msg msg;
745 dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__, sl->name, sl);
747 list_del(&sl->w1_slave_entry);
749 if (sl->family->fops && sl->family->fops->remove_slave)
750 sl->family->fops->remove_slave(sl);
752 memset(&msg, 0, sizeof(msg));
753 memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
754 msg.type = W1_SLAVE_REMOVE;
755 w1_netlink_send(sl->master, &msg);
757 sysfs_remove_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
758 device_remove_file(&sl->dev, &w1_slave_attr_name);
759 device_unregister(&sl->dev);
761 wait_for_completion(&sl->released);
765 struct w1_master *w1_search_master_id(u32 id)
767 struct w1_master *dev;
770 mutex_lock(&w1_mlock);
771 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
774 atomic_inc(&dev->refcnt);
778 mutex_unlock(&w1_mlock);
780 return (found)?dev:NULL;
783 struct w1_slave *w1_search_slave(struct w1_reg_num *id)
785 struct w1_master *dev;
786 struct w1_slave *sl = NULL;
789 mutex_lock(&w1_mlock);
790 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
791 mutex_lock(&dev->mutex);
792 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
793 if (sl->reg_num.family == id->family &&
794 sl->reg_num.id == id->id &&
795 sl->reg_num.crc == id->crc) {
797 atomic_inc(&dev->refcnt);
798 atomic_inc(&sl->refcnt);
802 mutex_unlock(&dev->mutex);
807 mutex_unlock(&w1_mlock);
809 return (found)?sl:NULL;
812 void w1_reconnect_slaves(struct w1_family *f, int attach)
814 struct w1_slave *sl, *sln;
815 struct w1_master *dev;
817 mutex_lock(&w1_mlock);
818 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
819 dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
820 "for family %02x.\n", dev->name, f->fid);
821 mutex_lock(&dev->mutex);
822 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
823 /* If it is a new family, slaves with the default
824 * family driver and are that family will be
825 * connected. If the family is going away, devices
826 * matching that family are reconneced.
828 if ((attach && sl->family->fid == W1_FAMILY_DEFAULT
829 && sl->reg_num.family == f->fid) ||
830 (!attach && sl->family->fid == f->fid)) {
831 struct w1_reg_num rn;
833 memcpy(&rn, &sl->reg_num, sizeof(rn));
836 w1_attach_slave_device(dev, &rn);
839 dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
840 "has been finished.\n", dev->name);
841 mutex_unlock(&dev->mutex);
843 mutex_unlock(&w1_mlock);
846 static void w1_slave_found(struct w1_master *dev, u64 rn)
850 struct list_head *ent;
851 struct w1_reg_num *tmp;
852 u64 rn_le = cpu_to_le64(rn);
854 atomic_inc(&dev->refcnt);
856 tmp = (struct w1_reg_num *) &rn;
859 list_for_each(ent, &dev->slist) {
861 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
863 if (sl->reg_num.family == tmp->family &&
864 sl->reg_num.id == tmp->id &&
865 sl->reg_num.crc == tmp->crc) {
866 set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
873 if (slave_count == dev->slave_count &&
874 rn && ((rn >> 56) & 0xff) == w1_calc_crc8((u8 *)&rn_le, 7)) {
875 w1_attach_slave_device(dev, tmp);
878 atomic_dec(&dev->refcnt);
882 * Performs a ROM Search & registers any devices found.
883 * The 1-wire search is a simple binary tree search.
884 * For each bit of the address, we read two bits and write one bit.
885 * The bit written will put to sleep all devies that don't match that bit.
886 * When the two reads differ, the direction choice is obvious.
887 * When both bits are 0, we must choose a path to take.
888 * When we can scan all 64 bits without having to choose a path, we are done.
890 * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
892 * @dev The master device to search
893 * @cb Function to call when a device is found
895 void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
897 u64 last_rn, rn, tmp64;
898 int i, slave_count = 0;
899 int last_zero, last_device;
900 int search_bit, desc_bit;
910 while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
915 * Reset bus and all 1-wire device state machines
916 * so they can respond to our requests.
918 * Return 0 - device(s) present, 1 - no devices present.
920 if (w1_reset_bus(dev)) {
921 dev_dbg(&dev->dev, "No devices present on the wire.\n");
925 /* Start the search */
926 w1_write_8(dev, search_type);
927 for (i = 0; i < 64; ++i) {
928 /* Determine the direction/search bit */
930 search_bit = 1; /* took the 0 path last time, so take the 1 path */
931 else if (i > desc_bit)
932 search_bit = 0; /* take the 0 path on the next branch */
934 search_bit = ((last_rn >> i) & 0x1);
936 /** Read two bits and write one bit */
937 triplet_ret = w1_triplet(dev, search_bit);
939 /* quit if no device responded */
940 if ( (triplet_ret & 0x03) == 0x03 )
943 /* If both directions were valid, and we took the 0 path... */
944 if (triplet_ret == 0)
947 /* extract the direction taken & update the device number */
948 tmp64 = (triplet_ret >> 2);
951 if (kthread_should_stop()) {
952 printk(KERN_INFO "Abort w1_search (exiting)\n");
957 if ( (triplet_ret & 0x03) != 0x03 ) {
958 if ( (desc_bit == last_zero) || (last_zero < 0))
960 desc_bit = last_zero;
966 void w1_search_process(struct w1_master *dev, u8 search_type)
968 struct w1_slave *sl, *sln;
970 list_for_each_entry(sl, &dev->slist, w1_slave_entry)
971 clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
973 w1_search_devices(dev, search_type, w1_slave_found);
975 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
976 if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl)
978 else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))
979 sl->ttl = dev->slave_ttl;
982 if (dev->search_count > 0)
986 int w1_process(void *data)
988 struct w1_master *dev = (struct w1_master *) data;
989 /* As long as w1_timeout is only set by a module parameter the sleep
990 * time can be calculated in jiffies once.
992 const unsigned long jtime = msecs_to_jiffies(w1_timeout * 1000);
994 while (!kthread_should_stop()) {
995 if (dev->search_count) {
996 mutex_lock(&dev->mutex);
997 w1_search_process(dev, W1_SEARCH);
998 mutex_unlock(&dev->mutex);
1002 __set_current_state(TASK_INTERRUPTIBLE);
1004 if (kthread_should_stop())
1007 /* Only sleep when the search is active. */
1008 if (dev->search_count)
1009 schedule_timeout(jtime);
1014 atomic_dec(&dev->refcnt);
1019 static int w1_init(void)
1023 printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
1027 retval = bus_register(&w1_bus_type);
1029 printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
1030 goto err_out_exit_init;
1033 retval = driver_register(&w1_master_driver);
1036 "Failed to register master driver. err=%d.\n",
1038 goto err_out_bus_unregister;
1041 retval = driver_register(&w1_slave_driver);
1044 "Failed to register master driver. err=%d.\n",
1046 goto err_out_master_unregister;
1052 /* For undoing the slave register if there was a step after it. */
1053 err_out_slave_unregister:
1054 driver_unregister(&w1_slave_driver);
1057 err_out_master_unregister:
1058 driver_unregister(&w1_master_driver);
1060 err_out_bus_unregister:
1061 bus_unregister(&w1_bus_type);
1067 static void w1_fini(void)
1069 struct w1_master *dev;
1071 /* Set netlink removal messages and some cleanup */
1072 list_for_each_entry(dev, &w1_masters, w1_master_entry)
1073 __w1_remove_master_device(dev);
1077 driver_unregister(&w1_slave_driver);
1078 driver_unregister(&w1_master_driver);
1079 bus_unregister(&w1_bus_type);
1082 module_init(w1_init);
1083 module_exit(w1_fini);