4 * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
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 <linux/atomic.h>
41 #include "w1_family.h"
42 #include "w1_netlink.h"
44 MODULE_LICENSE("GPL");
45 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
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 dev_dbg(dev
, "%s: Releasing %s.\n", __func__
, sl
->name
);
86 while (atomic_read(&sl
->refcnt
)) {
87 dev_dbg(dev
, "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
name_show(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
);
105 static DEVICE_ATTR_RO(name
);
107 static ssize_t
id_show(struct device
*dev
,
108 struct device_attribute
*attr
, char *buf
)
110 struct w1_slave
*sl
= dev_to_w1_slave(dev
);
111 ssize_t count
= sizeof(sl
->reg_num
);
113 memcpy(buf
, (u8
*)&sl
->reg_num
, count
);
116 static DEVICE_ATTR_RO(id
);
118 static struct attribute
*w1_slave_attrs
[] = {
123 ATTRIBUTE_GROUPS(w1_slave
);
127 static ssize_t
rw_write(struct file
*filp
, struct kobject
*kobj
,
128 struct bin_attribute
*bin_attr
, char *buf
, loff_t off
,
131 struct w1_slave
*sl
= kobj_to_w1_slave(kobj
);
133 mutex_lock(&sl
->master
->mutex
);
134 if (w1_reset_select_slave(sl
)) {
139 w1_write_block(sl
->master
, buf
, count
);
142 mutex_unlock(&sl
->master
->mutex
);
146 static ssize_t
rw_read(struct file
*filp
, struct kobject
*kobj
,
147 struct bin_attribute
*bin_attr
, char *buf
, loff_t off
,
150 struct w1_slave
*sl
= kobj_to_w1_slave(kobj
);
152 mutex_lock(&sl
->master
->mutex
);
153 w1_read_block(sl
->master
, buf
, count
);
154 mutex_unlock(&sl
->master
->mutex
);
158 static BIN_ATTR_RW(rw
, PAGE_SIZE
);
160 static struct bin_attribute
*w1_slave_bin_attrs
[] = {
165 static const struct attribute_group w1_slave_default_group
= {
166 .bin_attrs
= w1_slave_bin_attrs
,
169 static const struct attribute_group
*w1_slave_default_groups
[] = {
170 &w1_slave_default_group
,
174 static struct w1_family_ops w1_default_fops
= {
175 .groups
= w1_slave_default_groups
,
178 static struct w1_family w1_default_family
= {
179 .fops
= &w1_default_fops
,
182 static int w1_uevent(struct device
*dev
, struct kobj_uevent_env
*env
);
184 static struct bus_type w1_bus_type
= {
186 .match
= w1_master_match
,
190 struct device_driver w1_master_driver
= {
191 .name
= "w1_master_driver",
193 .probe
= w1_master_probe
,
196 struct device w1_master_device
= {
199 .init_name
= "w1 bus master",
200 .driver
= &w1_master_driver
,
201 .release
= &w1_master_release
204 static struct device_driver w1_slave_driver
= {
205 .name
= "w1_slave_driver",
210 struct device w1_slave_device
= {
213 .init_name
= "w1 bus slave",
214 .driver
= &w1_slave_driver
,
215 .release
= &w1_slave_release
219 static ssize_t
w1_master_attribute_show_name(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
221 struct w1_master
*md
= dev_to_w1_master(dev
);
224 mutex_lock(&md
->mutex
);
225 count
= sprintf(buf
, "%s\n", md
->name
);
226 mutex_unlock(&md
->mutex
);
231 static ssize_t
w1_master_attribute_store_search(struct device
* dev
,
232 struct device_attribute
*attr
,
233 const char * buf
, size_t count
)
236 struct w1_master
*md
= dev_to_w1_master(dev
);
239 ret
= kstrtol(buf
, 0, &tmp
);
243 mutex_lock(&md
->mutex
);
244 md
->search_count
= tmp
;
245 mutex_unlock(&md
->mutex
);
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
);
280 wake_up_process(md
->thread
);
285 static ssize_t
w1_master_attribute_show_pullup(struct device
*dev
,
286 struct device_attribute
*attr
,
289 struct w1_master
*md
= dev_to_w1_master(dev
);
292 mutex_lock(&md
->mutex
);
293 count
= sprintf(buf
, "%d\n", md
->enable_pullup
);
294 mutex_unlock(&md
->mutex
);
299 static ssize_t
w1_master_attribute_show_pointer(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
301 struct w1_master
*md
= dev_to_w1_master(dev
);
304 mutex_lock(&md
->mutex
);
305 count
= sprintf(buf
, "0x%p\n", md
->bus_master
);
306 mutex_unlock(&md
->mutex
);
310 static ssize_t
w1_master_attribute_show_timeout(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
313 count
= sprintf(buf
, "%d\n", w1_timeout
);
317 static ssize_t
w1_master_attribute_show_max_slave_count(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
319 struct w1_master
*md
= dev_to_w1_master(dev
);
322 mutex_lock(&md
->mutex
);
323 count
= sprintf(buf
, "%d\n", md
->max_slave_count
);
324 mutex_unlock(&md
->mutex
);
328 static ssize_t
w1_master_attribute_show_attempts(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
330 struct w1_master
*md
= dev_to_w1_master(dev
);
333 mutex_lock(&md
->mutex
);
334 count
= sprintf(buf
, "%lu\n", md
->attempts
);
335 mutex_unlock(&md
->mutex
);
339 static ssize_t
w1_master_attribute_show_slave_count(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
341 struct w1_master
*md
= dev_to_w1_master(dev
);
344 mutex_lock(&md
->mutex
);
345 count
= sprintf(buf
, "%d\n", md
->slave_count
);
346 mutex_unlock(&md
->mutex
);
350 static ssize_t
w1_master_attribute_show_slaves(struct device
*dev
,
351 struct device_attribute
*attr
, char *buf
)
353 struct w1_master
*md
= dev_to_w1_master(dev
);
356 mutex_lock(&md
->mutex
);
358 if (md
->slave_count
== 0)
359 c
-= snprintf(buf
+ PAGE_SIZE
- c
, c
, "not found.\n");
361 struct list_head
*ent
, *n
;
364 list_for_each_safe(ent
, n
, &md
->slist
) {
365 sl
= list_entry(ent
, struct w1_slave
, w1_slave_entry
);
367 c
-= snprintf(buf
+ PAGE_SIZE
- c
, c
, "%s\n", sl
->name
);
371 mutex_unlock(&md
->mutex
);
373 return PAGE_SIZE
- c
;
376 static ssize_t
w1_master_attribute_show_add(struct device
*dev
,
377 struct device_attribute
*attr
, char *buf
)
380 c
-= snprintf(buf
+PAGE_SIZE
- c
, c
,
381 "write device id xx-xxxxxxxxxxxx to add slave\n");
382 return PAGE_SIZE
- c
;
385 static int w1_atoreg_num(struct device
*dev
, const char *buf
, size_t count
,
386 struct w1_reg_num
*rn
)
389 unsigned long long id
;
393 /* The CRC value isn't read from the user because the sysfs directory
394 * doesn't include it and most messages from the bus search don't
395 * print it either. It would be unreasonable for the user to then
398 const char *error_msg
= "bad slave string format, expecting "
402 dev_err(dev
, "%s", error_msg
);
405 i
= sscanf(buf
, "%02x-%012llx", &family
, &id
);
407 dev_err(dev
, "%s", error_msg
);
413 rn64_le
= cpu_to_le64(*(u64
*)rn
);
414 rn
->crc
= w1_calc_crc8((u8
*)&rn64_le
, 7);
417 dev_info(dev
, "With CRC device is %02x.%012llx.%02x.\n",
418 rn
->family
, (unsigned long long)rn
->id
, rn
->crc
);
424 /* Searches the slaves in the w1_master and returns a pointer or NULL.
425 * Note: must hold the mutex
427 static struct w1_slave
*w1_slave_search_device(struct w1_master
*dev
,
428 struct w1_reg_num
*rn
)
431 list_for_each_entry(sl
, &dev
->slist
, w1_slave_entry
) {
432 if (sl
->reg_num
.family
== rn
->family
&&
433 sl
->reg_num
.id
== rn
->id
&&
434 sl
->reg_num
.crc
== rn
->crc
) {
441 static ssize_t
w1_master_attribute_store_add(struct device
*dev
,
442 struct device_attribute
*attr
,
443 const char *buf
, size_t count
)
445 struct w1_master
*md
= dev_to_w1_master(dev
);
446 struct w1_reg_num rn
;
448 ssize_t result
= count
;
450 if (w1_atoreg_num(dev
, buf
, count
, &rn
))
453 mutex_lock(&md
->mutex
);
454 sl
= w1_slave_search_device(md
, &rn
);
455 /* It would be nice to do a targeted search one the one-wire bus
456 * for the new device to see if it is out there or not. But the
457 * current search doesn't support that.
460 dev_info(dev
, "Device %s already exists\n", sl
->name
);
463 w1_attach_slave_device(md
, &rn
);
465 mutex_unlock(&md
->mutex
);
470 static ssize_t
w1_master_attribute_show_remove(struct device
*dev
,
471 struct device_attribute
*attr
, char *buf
)
474 c
-= snprintf(buf
+PAGE_SIZE
- c
, c
,
475 "write device id xx-xxxxxxxxxxxx to remove slave\n");
476 return PAGE_SIZE
- c
;
479 static ssize_t
w1_master_attribute_store_remove(struct device
*dev
,
480 struct device_attribute
*attr
,
481 const char *buf
, size_t count
)
483 struct w1_master
*md
= dev_to_w1_master(dev
);
484 struct w1_reg_num rn
;
486 ssize_t result
= count
;
488 if (w1_atoreg_num(dev
, buf
, count
, &rn
))
491 mutex_lock(&md
->mutex
);
492 sl
= w1_slave_search_device(md
, &rn
);
496 dev_info(dev
, "Device %02x-%012llx doesn't exists\n", rn
.family
,
497 (unsigned long long)rn
.id
);
500 mutex_unlock(&md
->mutex
);
505 #define W1_MASTER_ATTR_RO(_name, _mode) \
506 struct device_attribute w1_master_attribute_##_name = \
507 __ATTR(w1_master_##_name, _mode, \
508 w1_master_attribute_show_##_name, NULL)
510 #define W1_MASTER_ATTR_RW(_name, _mode) \
511 struct device_attribute w1_master_attribute_##_name = \
512 __ATTR(w1_master_##_name, _mode, \
513 w1_master_attribute_show_##_name, \
514 w1_master_attribute_store_##_name)
516 static W1_MASTER_ATTR_RO(name
, S_IRUGO
);
517 static W1_MASTER_ATTR_RO(slaves
, S_IRUGO
);
518 static W1_MASTER_ATTR_RO(slave_count
, S_IRUGO
);
519 static W1_MASTER_ATTR_RO(max_slave_count
, S_IRUGO
);
520 static W1_MASTER_ATTR_RO(attempts
, S_IRUGO
);
521 static W1_MASTER_ATTR_RO(timeout
, S_IRUGO
);
522 static W1_MASTER_ATTR_RO(pointer
, S_IRUGO
);
523 static W1_MASTER_ATTR_RW(search
, S_IRUGO
| S_IWUSR
| S_IWGRP
);
524 static W1_MASTER_ATTR_RW(pullup
, S_IRUGO
| S_IWUSR
| S_IWGRP
);
525 static W1_MASTER_ATTR_RW(add
, S_IRUGO
| S_IWUSR
| S_IWGRP
);
526 static W1_MASTER_ATTR_RW(remove
, S_IRUGO
| S_IWUSR
| S_IWGRP
);
528 static struct attribute
*w1_master_default_attrs
[] = {
529 &w1_master_attribute_name
.attr
,
530 &w1_master_attribute_slaves
.attr
,
531 &w1_master_attribute_slave_count
.attr
,
532 &w1_master_attribute_max_slave_count
.attr
,
533 &w1_master_attribute_attempts
.attr
,
534 &w1_master_attribute_timeout
.attr
,
535 &w1_master_attribute_pointer
.attr
,
536 &w1_master_attribute_search
.attr
,
537 &w1_master_attribute_pullup
.attr
,
538 &w1_master_attribute_add
.attr
,
539 &w1_master_attribute_remove
.attr
,
543 static struct attribute_group w1_master_defattr_group
= {
544 .attrs
= w1_master_default_attrs
,
547 int w1_create_master_attributes(struct w1_master
*master
)
549 return sysfs_create_group(&master
->dev
.kobj
, &w1_master_defattr_group
);
552 void w1_destroy_master_attributes(struct w1_master
*master
)
554 sysfs_remove_group(&master
->dev
.kobj
, &w1_master_defattr_group
);
557 static int w1_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
559 struct w1_master
*md
= NULL
;
560 struct w1_slave
*sl
= NULL
;
561 char *event_owner
, *name
;
564 if (dev
->driver
== &w1_master_driver
) {
565 md
= container_of(dev
, struct w1_master
, dev
);
566 event_owner
= "master";
568 } else if (dev
->driver
== &w1_slave_driver
) {
569 sl
= container_of(dev
, struct w1_slave
, dev
);
570 event_owner
= "slave";
573 dev_dbg(dev
, "Unknown event.\n");
577 dev_dbg(dev
, "Hotplug event for %s %s, bus_id=%s.\n",
578 event_owner
, name
, dev_name(dev
));
580 if (dev
->driver
!= &w1_slave_driver
|| !sl
)
583 err
= add_uevent_var(env
, "W1_FID=%02X", sl
->reg_num
.family
);
587 err
= add_uevent_var(env
, "W1_SLAVE_ID=%024LX",
588 (unsigned long long)sl
->reg_num
.id
);
594 * Handle sysfs file creation and removal here, before userspace is told that
595 * the device is added / removed from the system
597 static int w1_bus_notify(struct notifier_block
*nb
, unsigned long action
,
600 struct device
*dev
= data
;
602 struct w1_family_ops
*fops
;
606 * Only care about slave devices at the moment. Yes, we should use a
607 * separate "type" for this, but for now, look at the release function
608 * to know which type it is...
610 if (dev
->release
!= w1_slave_release
)
613 sl
= dev_to_w1_slave(dev
);
614 fops
= sl
->family
->fops
;
620 case BUS_NOTIFY_ADD_DEVICE
:
621 /* if the family driver needs to initialize something... */
622 if (fops
->add_slave
) {
623 err
= fops
->add_slave(sl
);
626 "add_slave() call failed. err=%d\n",
632 err
= sysfs_create_groups(&sl
->dev
.kobj
, fops
->groups
);
635 "sysfs group creation failed. err=%d\n",
642 case BUS_NOTIFY_DEL_DEVICE
:
643 if (fops
->remove_slave
)
644 sl
->family
->fops
->remove_slave(sl
);
646 sysfs_remove_groups(&sl
->dev
.kobj
, fops
->groups
);
652 static struct notifier_block w1_bus_nb
= {
653 .notifier_call
= w1_bus_notify
,
656 static int __w1_attach_slave_device(struct w1_slave
*sl
)
660 sl
->dev
.parent
= &sl
->master
->dev
;
661 sl
->dev
.driver
= &w1_slave_driver
;
662 sl
->dev
.bus
= &w1_bus_type
;
663 sl
->dev
.release
= &w1_slave_release
;
664 sl
->dev
.groups
= w1_slave_groups
;
666 dev_set_name(&sl
->dev
, "%02x-%012llx",
667 (unsigned int) sl
->reg_num
.family
,
668 (unsigned long long) sl
->reg_num
.id
);
669 snprintf(&sl
->name
[0], sizeof(sl
->name
),
671 (unsigned int) sl
->reg_num
.family
,
672 (unsigned long long) sl
->reg_num
.id
);
674 dev_dbg(&sl
->dev
, "%s: registering %s as %p.\n", __func__
,
675 dev_name(&sl
->dev
), sl
);
677 err
= device_register(&sl
->dev
);
680 "Device registration [%s] failed. err=%d\n",
681 dev_name(&sl
->dev
), err
);
686 dev_set_uevent_suppress(&sl
->dev
, false);
687 kobject_uevent(&sl
->dev
.kobj
, KOBJ_ADD
);
689 list_add_tail(&sl
->w1_slave_entry
, &sl
->master
->slist
);
694 static int w1_attach_slave_device(struct w1_master
*dev
, struct w1_reg_num
*rn
)
699 struct w1_netlink_msg msg
;
701 sl
= kzalloc(sizeof(struct w1_slave
), GFP_KERNEL
);
704 "%s: failed to allocate new slave device.\n",
710 sl
->owner
= THIS_MODULE
;
712 set_bit(W1_SLAVE_ACTIVE
, (long *)&sl
->flags
);
714 memset(&msg
, 0, sizeof(msg
));
715 memcpy(&sl
->reg_num
, rn
, sizeof(sl
->reg_num
));
716 atomic_set(&sl
->refcnt
, 0);
717 init_completion(&sl
->released
);
719 /* slave modules need to be loaded in a context with unlocked mutex */
720 mutex_unlock(&dev
->mutex
);
721 request_module("w1-family-0x%0x", rn
->family
);
722 mutex_lock(&dev
->mutex
);
724 spin_lock(&w1_flock
);
725 f
= w1_family_registered(rn
->family
);
727 f
= &w1_default_family
;
728 dev_info(&dev
->dev
, "Family %x for %02x.%012llx.%02x is not registered.\n",
729 rn
->family
, rn
->family
,
730 (unsigned long long)rn
->id
, rn
->crc
);
733 spin_unlock(&w1_flock
);
738 err
= __w1_attach_slave_device(sl
);
740 dev_err(&dev
->dev
, "%s: Attaching %s failed.\n", __func__
,
742 w1_family_put(sl
->family
);
747 sl
->ttl
= dev
->slave_ttl
;
750 memcpy(msg
.id
.id
, rn
, sizeof(msg
.id
));
751 msg
.type
= W1_SLAVE_ADD
;
752 w1_netlink_send(dev
, &msg
);
757 void w1_slave_detach(struct w1_slave
*sl
)
759 struct w1_netlink_msg msg
;
761 dev_dbg(&sl
->dev
, "%s: detaching %s [%p].\n", __func__
, sl
->name
, sl
);
763 list_del(&sl
->w1_slave_entry
);
765 memset(&msg
, 0, sizeof(msg
));
766 memcpy(msg
.id
.id
, &sl
->reg_num
, sizeof(msg
.id
));
767 msg
.type
= W1_SLAVE_REMOVE
;
768 w1_netlink_send(sl
->master
, &msg
);
770 device_unregister(&sl
->dev
);
772 wait_for_completion(&sl
->released
);
776 struct w1_master
*w1_search_master_id(u32 id
)
778 struct w1_master
*dev
;
781 mutex_lock(&w1_mlock
);
782 list_for_each_entry(dev
, &w1_masters
, w1_master_entry
) {
785 atomic_inc(&dev
->refcnt
);
789 mutex_unlock(&w1_mlock
);
791 return (found
)?dev
:NULL
;
794 struct w1_slave
*w1_search_slave(struct w1_reg_num
*id
)
796 struct w1_master
*dev
;
797 struct w1_slave
*sl
= NULL
;
800 mutex_lock(&w1_mlock
);
801 list_for_each_entry(dev
, &w1_masters
, w1_master_entry
) {
802 mutex_lock(&dev
->mutex
);
803 list_for_each_entry(sl
, &dev
->slist
, w1_slave_entry
) {
804 if (sl
->reg_num
.family
== id
->family
&&
805 sl
->reg_num
.id
== id
->id
&&
806 sl
->reg_num
.crc
== id
->crc
) {
808 atomic_inc(&dev
->refcnt
);
809 atomic_inc(&sl
->refcnt
);
813 mutex_unlock(&dev
->mutex
);
818 mutex_unlock(&w1_mlock
);
820 return (found
)?sl
:NULL
;
823 void w1_reconnect_slaves(struct w1_family
*f
, int attach
)
825 struct w1_slave
*sl
, *sln
;
826 struct w1_master
*dev
;
828 mutex_lock(&w1_mlock
);
829 list_for_each_entry(dev
, &w1_masters
, w1_master_entry
) {
830 dev_dbg(&dev
->dev
, "Reconnecting slaves in device %s "
831 "for family %02x.\n", dev
->name
, f
->fid
);
832 mutex_lock(&dev
->mutex
);
833 list_for_each_entry_safe(sl
, sln
, &dev
->slist
, w1_slave_entry
) {
834 /* If it is a new family, slaves with the default
835 * family driver and are that family will be
836 * connected. If the family is going away, devices
837 * matching that family are reconneced.
839 if ((attach
&& sl
->family
->fid
== W1_FAMILY_DEFAULT
840 && sl
->reg_num
.family
== f
->fid
) ||
841 (!attach
&& sl
->family
->fid
== f
->fid
)) {
842 struct w1_reg_num rn
;
844 memcpy(&rn
, &sl
->reg_num
, sizeof(rn
));
847 w1_attach_slave_device(dev
, &rn
);
850 dev_dbg(&dev
->dev
, "Reconnecting slaves in device %s "
851 "has been finished.\n", dev
->name
);
852 mutex_unlock(&dev
->mutex
);
854 mutex_unlock(&w1_mlock
);
857 void w1_slave_found(struct w1_master
*dev
, u64 rn
)
860 struct w1_reg_num
*tmp
;
861 u64 rn_le
= cpu_to_le64(rn
);
863 atomic_inc(&dev
->refcnt
);
865 tmp
= (struct w1_reg_num
*) &rn
;
867 sl
= w1_slave_search_device(dev
, tmp
);
869 set_bit(W1_SLAVE_ACTIVE
, (long *)&sl
->flags
);
871 if (rn
&& tmp
->crc
== w1_calc_crc8((u8
*)&rn_le
, 7))
872 w1_attach_slave_device(dev
, tmp
);
875 atomic_dec(&dev
->refcnt
);
879 * Performs a ROM Search & registers any devices found.
880 * The 1-wire search is a simple binary tree search.
881 * For each bit of the address, we read two bits and write one bit.
882 * The bit written will put to sleep all devies that don't match that bit.
883 * When the two reads differ, the direction choice is obvious.
884 * When both bits are 0, we must choose a path to take.
885 * When we can scan all 64 bits without having to choose a path, we are done.
887 * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
889 * @dev The master device to search
890 * @cb Function to call when a device is found
892 void w1_search(struct w1_master
*dev
, u8 search_type
, w1_slave_found_callback cb
)
894 u64 last_rn
, rn
, tmp64
;
895 int i
, slave_count
= 0;
896 int last_zero
, last_device
;
897 int search_bit
, desc_bit
;
907 while ( !last_device
&& (slave_count
++ < dev
->max_slave_count
) ) {
912 * Reset bus and all 1-wire device state machines
913 * so they can respond to our requests.
915 * Return 0 - device(s) present, 1 - no devices present.
917 mutex_lock(&dev
->bus_mutex
);
918 if (w1_reset_bus(dev
)) {
919 mutex_unlock(&dev
->bus_mutex
);
920 dev_dbg(&dev
->dev
, "No devices present on the wire.\n");
924 /* Do fast search on single slave bus */
925 if (dev
->max_slave_count
== 1) {
927 w1_write_8(dev
, W1_READ_ROM
);
928 rv
= w1_read_block(dev
, (u8
*)&rn
, 8);
929 mutex_unlock(&dev
->bus_mutex
);
937 /* Start the search */
938 w1_write_8(dev
, search_type
);
939 for (i
= 0; i
< 64; ++i
) {
940 /* Determine the direction/search bit */
942 search_bit
= 1; /* took the 0 path last time, so take the 1 path */
943 else if (i
> desc_bit
)
944 search_bit
= 0; /* take the 0 path on the next branch */
946 search_bit
= ((last_rn
>> i
) & 0x1);
948 /** Read two bits and write one bit */
949 triplet_ret
= w1_triplet(dev
, search_bit
);
951 /* quit if no device responded */
952 if ( (triplet_ret
& 0x03) == 0x03 )
955 /* If both directions were valid, and we took the 0 path... */
956 if (triplet_ret
== 0)
959 /* extract the direction taken & update the device number */
960 tmp64
= (triplet_ret
>> 2);
963 /* ensure we're called from kthread and not by netlink callback */
964 if (!dev
->priv
&& kthread_should_stop()) {
965 mutex_unlock(&dev
->bus_mutex
);
966 dev_dbg(&dev
->dev
, "Abort w1_search\n");
970 mutex_unlock(&dev
->bus_mutex
);
972 if ( (triplet_ret
& 0x03) != 0x03 ) {
973 if ( (desc_bit
== last_zero
) || (last_zero
< 0))
975 desc_bit
= last_zero
;
981 void w1_search_process_cb(struct w1_master
*dev
, u8 search_type
,
982 w1_slave_found_callback cb
)
984 struct w1_slave
*sl
, *sln
;
986 list_for_each_entry(sl
, &dev
->slist
, w1_slave_entry
)
987 clear_bit(W1_SLAVE_ACTIVE
, (long *)&sl
->flags
);
989 w1_search_devices(dev
, search_type
, cb
);
991 list_for_each_entry_safe(sl
, sln
, &dev
->slist
, w1_slave_entry
) {
992 if (!test_bit(W1_SLAVE_ACTIVE
, (unsigned long *)&sl
->flags
) && !--sl
->ttl
)
994 else if (test_bit(W1_SLAVE_ACTIVE
, (unsigned long *)&sl
->flags
))
995 sl
->ttl
= dev
->slave_ttl
;
998 if (dev
->search_count
> 0)
1002 static void w1_search_process(struct w1_master
*dev
, u8 search_type
)
1004 w1_search_process_cb(dev
, search_type
, w1_slave_found
);
1007 int w1_process(void *data
)
1009 struct w1_master
*dev
= (struct w1_master
*) data
;
1010 /* As long as w1_timeout is only set by a module parameter the sleep
1011 * time can be calculated in jiffies once.
1013 const unsigned long jtime
= msecs_to_jiffies(w1_timeout
* 1000);
1015 while (!kthread_should_stop()) {
1016 if (dev
->search_count
) {
1017 mutex_lock(&dev
->mutex
);
1018 w1_search_process(dev
, W1_SEARCH
);
1019 mutex_unlock(&dev
->mutex
);
1023 __set_current_state(TASK_INTERRUPTIBLE
);
1025 if (kthread_should_stop())
1028 /* Only sleep when the search is active. */
1029 if (dev
->search_count
)
1030 schedule_timeout(jtime
);
1035 atomic_dec(&dev
->refcnt
);
1040 static int __init
w1_init(void)
1044 printk(KERN_INFO
"Driver for 1-wire Dallas network protocol.\n");
1048 retval
= bus_register(&w1_bus_type
);
1050 printk(KERN_ERR
"Failed to register bus. err=%d.\n", retval
);
1051 goto err_out_exit_init
;
1054 retval
= bus_register_notifier(&w1_bus_type
, &w1_bus_nb
);
1056 goto err_out_bus_unregister
;
1058 retval
= driver_register(&w1_master_driver
);
1061 "Failed to register master driver. err=%d.\n",
1063 goto err_out_bus_unregister
;
1066 retval
= driver_register(&w1_slave_driver
);
1069 "Failed to register slave driver. err=%d.\n",
1071 goto err_out_master_unregister
;
1077 /* For undoing the slave register if there was a step after it. */
1078 err_out_slave_unregister
:
1079 driver_unregister(&w1_slave_driver
);
1082 err_out_master_unregister
:
1083 driver_unregister(&w1_master_driver
);
1085 err_out_bus_unregister
:
1086 bus_unregister(&w1_bus_type
);
1092 static void __exit
w1_fini(void)
1094 struct w1_master
*dev
;
1096 /* Set netlink removal messages and some cleanup */
1097 list_for_each_entry(dev
, &w1_masters
, w1_master_entry
)
1098 __w1_remove_master_device(dev
);
1102 driver_unregister(&w1_slave_driver
);
1103 driver_unregister(&w1_master_driver
);
1104 bus_unregister(&w1_bus_type
);
1107 module_init(w1_init
);
1108 module_exit(w1_fini
);