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>
35 #include <asm/atomic.h>
40 #include "w1_family.h"
41 #include "w1_netlink.h"
43 MODULE_LICENSE("GPL");
44 MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
45 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
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;
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);
57 DECLARE_MUTEX(w1_mlock
);
58 LIST_HEAD(w1_masters
);
60 static struct task_struct
*w1_control_thread
;
62 static int w1_master_match(struct device
*dev
, struct device_driver
*drv
)
67 static int w1_master_probe(struct device
*dev
)
72 static void w1_master_release(struct device
*dev
)
74 struct w1_master
*md
= dev_to_w1_master(dev
);
76 dev_dbg(dev
, "%s: Releasing %s.\n", __func__
, md
->name
);
77 memset(md
, 0, sizeof(struct w1_master
) + sizeof(struct w1_bus_master
));
81 static void w1_slave_release(struct device
*dev
)
83 struct w1_slave
*sl
= dev_to_w1_slave(dev
);
85 printk("%s: Releasing %s.\n", __func__
, sl
->name
);
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
);
94 w1_family_put(sl
->family
);
95 sl
->master
->slave_count
--;
97 complete(&sl
->released
);
100 static ssize_t
w1_slave_read_name(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
102 struct w1_slave
*sl
= dev_to_w1_slave(dev
);
104 return sprintf(buf
, "%s\n", sl
->name
);
107 static ssize_t
w1_slave_read_id(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
109 struct w1_slave
*sl
= kobj_to_w1_slave(kobj
);
117 memcpy(buf
, (u8
*)&sl
->reg_num
, count
);
123 static struct device_attribute w1_slave_attr_name
=
124 __ATTR(name
, S_IRUGO
, w1_slave_read_name
, NULL
);
126 static struct bin_attribute w1_slave_attr_bin_id
= {
130 .owner
= THIS_MODULE
,
133 .read
= w1_slave_read_id
,
138 static ssize_t
w1_default_write(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
140 struct w1_slave
*sl
= kobj_to_w1_slave(kobj
);
142 if (down_interruptible(&sl
->master
->mutex
)) {
147 if (w1_reset_select_slave(sl
)) {
152 w1_write_block(sl
->master
, buf
, count
);
155 up(&sl
->master
->mutex
);
160 static ssize_t
w1_default_read(struct kobject
*kobj
, char *buf
, loff_t off
, size_t count
)
162 struct w1_slave
*sl
= kobj_to_w1_slave(kobj
);
164 if (down_interruptible(&sl
->master
->mutex
)) {
169 w1_read_block(sl
->master
, buf
, count
);
171 up(&sl
->master
->mutex
);
176 static struct bin_attribute w1_default_attr
= {
179 .mode
= S_IRUGO
| S_IWUSR
,
180 .owner
= THIS_MODULE
,
183 .read
= w1_default_read
,
184 .write
= w1_default_write
,
187 static int w1_default_add_slave(struct w1_slave
*sl
)
189 return sysfs_create_bin_file(&sl
->dev
.kobj
, &w1_default_attr
);
192 static void w1_default_remove_slave(struct w1_slave
*sl
)
194 sysfs_remove_bin_file(&sl
->dev
.kobj
, &w1_default_attr
);
197 static struct w1_family_ops w1_default_fops
= {
198 .add_slave
= w1_default_add_slave
,
199 .remove_slave
= w1_default_remove_slave
,
202 static struct w1_family w1_default_family
= {
203 .fops
= &w1_default_fops
,
206 static int w1_uevent(struct device
*dev
, char **envp
, int num_envp
, char *buffer
, int buffer_size
);
208 static struct bus_type w1_bus_type
= {
210 .match
= w1_master_match
,
214 struct device_driver w1_master_driver
= {
215 .name
= "w1_master_driver",
217 .probe
= w1_master_probe
,
220 struct device w1_master_device
= {
223 .bus_id
= "w1 bus master",
224 .driver
= &w1_master_driver
,
225 .release
= &w1_master_release
228 struct device_driver w1_slave_driver
= {
229 .name
= "w1_slave_driver",
233 struct device w1_slave_device
= {
236 .bus_id
= "w1 bus slave",
237 .driver
= &w1_slave_driver
,
238 .release
= &w1_slave_release
241 static ssize_t
w1_master_attribute_show_name(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
243 struct w1_master
*md
= dev_to_w1_master(dev
);
246 if (down_interruptible (&md
->mutex
))
249 count
= sprintf(buf
, "%s\n", md
->name
);
256 static ssize_t
w1_master_attribute_store_search(struct device
* dev
,
257 struct device_attribute
*attr
,
258 const char * buf
, size_t count
)
260 struct w1_master
*md
= dev_to_w1_master(dev
);
262 if (down_interruptible (&md
->mutex
))
265 md
->search_count
= simple_strtol(buf
, NULL
, 0);
272 static ssize_t
w1_master_attribute_show_search(struct device
*dev
,
273 struct device_attribute
*attr
,
276 struct w1_master
*md
= dev_to_w1_master(dev
);
279 if (down_interruptible (&md
->mutex
))
282 count
= sprintf(buf
, "%d\n", md
->search_count
);
289 static ssize_t
w1_master_attribute_show_pointer(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
291 struct w1_master
*md
= dev_to_w1_master(dev
);
294 if (down_interruptible(&md
->mutex
))
297 count
= sprintf(buf
, "0x%p\n", md
->bus_master
);
303 static ssize_t
w1_master_attribute_show_timeout(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
306 count
= sprintf(buf
, "%d\n", w1_timeout
);
310 static ssize_t
w1_master_attribute_show_max_slave_count(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
312 struct w1_master
*md
= dev_to_w1_master(dev
);
315 if (down_interruptible(&md
->mutex
))
318 count
= sprintf(buf
, "%d\n", md
->max_slave_count
);
324 static ssize_t
w1_master_attribute_show_attempts(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
326 struct w1_master
*md
= dev_to_w1_master(dev
);
329 if (down_interruptible(&md
->mutex
))
332 count
= sprintf(buf
, "%lu\n", md
->attempts
);
338 static ssize_t
w1_master_attribute_show_slave_count(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
340 struct w1_master
*md
= dev_to_w1_master(dev
);
343 if (down_interruptible(&md
->mutex
))
346 count
= sprintf(buf
, "%d\n", md
->slave_count
);
352 static ssize_t
w1_master_attribute_show_slaves(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
354 struct w1_master
*md
= dev_to_w1_master(dev
);
357 if (down_interruptible(&md
->mutex
))
360 if (md
->slave_count
== 0)
361 c
-= snprintf(buf
+ PAGE_SIZE
- c
, c
, "not found.\n");
363 struct list_head
*ent
, *n
;
366 list_for_each_safe(ent
, n
, &md
->slist
) {
367 sl
= list_entry(ent
, struct w1_slave
, w1_slave_entry
);
369 c
-= snprintf(buf
+ PAGE_SIZE
- c
, c
, "%s\n", sl
->name
);
375 return PAGE_SIZE
- c
;
378 #define W1_MASTER_ATTR_RO(_name, _mode) \
379 struct device_attribute w1_master_attribute_##_name = \
380 __ATTR(w1_master_##_name, _mode, \
381 w1_master_attribute_show_##_name, NULL)
383 #define W1_MASTER_ATTR_RW(_name, _mode) \
384 struct device_attribute w1_master_attribute_##_name = \
385 __ATTR(w1_master_##_name, _mode, \
386 w1_master_attribute_show_##_name, \
387 w1_master_attribute_store_##_name)
389 static W1_MASTER_ATTR_RO(name
, S_IRUGO
);
390 static W1_MASTER_ATTR_RO(slaves
, S_IRUGO
);
391 static W1_MASTER_ATTR_RO(slave_count
, S_IRUGO
);
392 static W1_MASTER_ATTR_RO(max_slave_count
, S_IRUGO
);
393 static W1_MASTER_ATTR_RO(attempts
, S_IRUGO
);
394 static W1_MASTER_ATTR_RO(timeout
, S_IRUGO
);
395 static W1_MASTER_ATTR_RO(pointer
, S_IRUGO
);
396 static W1_MASTER_ATTR_RW(search
, S_IRUGO
| S_IWUGO
);
398 static struct attribute
*w1_master_default_attrs
[] = {
399 &w1_master_attribute_name
.attr
,
400 &w1_master_attribute_slaves
.attr
,
401 &w1_master_attribute_slave_count
.attr
,
402 &w1_master_attribute_max_slave_count
.attr
,
403 &w1_master_attribute_attempts
.attr
,
404 &w1_master_attribute_timeout
.attr
,
405 &w1_master_attribute_pointer
.attr
,
406 &w1_master_attribute_search
.attr
,
410 static struct attribute_group w1_master_defattr_group
= {
411 .attrs
= w1_master_default_attrs
,
414 int w1_create_master_attributes(struct w1_master
*master
)
416 return sysfs_create_group(&master
->dev
.kobj
, &w1_master_defattr_group
);
419 void w1_destroy_master_attributes(struct w1_master
*master
)
421 sysfs_remove_group(&master
->dev
.kobj
, &w1_master_defattr_group
);
424 #ifdef CONFIG_HOTPLUG
425 static int w1_uevent(struct device
*dev
, char **envp
, int num_envp
, char *buffer
, int buffer_size
)
427 struct w1_master
*md
= NULL
;
428 struct w1_slave
*sl
= NULL
;
429 char *event_owner
, *name
;
430 int err
, cur_index
=0, cur_len
=0;
432 if (dev
->driver
== &w1_master_driver
) {
433 md
= container_of(dev
, struct w1_master
, dev
);
434 event_owner
= "master";
436 } else if (dev
->driver
== &w1_slave_driver
) {
437 sl
= container_of(dev
, struct w1_slave
, dev
);
438 event_owner
= "slave";
441 dev_dbg(dev
, "Unknown event.\n");
445 dev_dbg(dev
, "Hotplug event for %s %s, bus_id=%s.\n", event_owner
, name
, dev
->bus_id
);
447 if (dev
->driver
!= &w1_slave_driver
|| !sl
)
450 err
= add_uevent_var(envp
, num_envp
, &cur_index
, buffer
, buffer_size
, &cur_len
, "W1_FID=%02X", sl
->reg_num
.family
);
454 err
= add_uevent_var(envp
, num_envp
, &cur_index
, buffer
, buffer_size
, &cur_len
, "W1_SLAVE_ID=%024LX", (u64
)sl
->reg_num
.id
);
461 static int w1_uevent(struct device
*dev
, char **envp
, int num_envp
, char *buffer
, int buffer_size
)
467 static int __w1_attach_slave_device(struct w1_slave
*sl
)
471 sl
->dev
.parent
= &sl
->master
->dev
;
472 sl
->dev
.driver
= &w1_slave_driver
;
473 sl
->dev
.bus
= &w1_bus_type
;
474 sl
->dev
.release
= &w1_slave_release
;
476 snprintf(&sl
->dev
.bus_id
[0], sizeof(sl
->dev
.bus_id
),
478 (unsigned int) sl
->reg_num
.family
,
479 (unsigned long long) sl
->reg_num
.id
);
480 snprintf(&sl
->name
[0], sizeof(sl
->name
),
482 (unsigned int) sl
->reg_num
.family
,
483 (unsigned long long) sl
->reg_num
.id
);
485 dev_dbg(&sl
->dev
, "%s: registering %s as %p.\n", __func__
, &sl
->dev
.bus_id
[0]);
487 err
= device_register(&sl
->dev
);
490 "Device registration [%s] failed. err=%d\n",
491 sl
->dev
.bus_id
, err
);
495 /* Create "name" entry */
496 err
= device_create_file(&sl
->dev
, &w1_slave_attr_name
);
499 "sysfs file creation for [%s] failed. err=%d\n",
500 sl
->dev
.bus_id
, err
);
504 /* Create "id" entry */
505 err
= sysfs_create_bin_file(&sl
->dev
.kobj
, &w1_slave_attr_bin_id
);
508 "sysfs file creation for [%s] failed. err=%d\n",
509 sl
->dev
.bus_id
, err
);
513 /* if the family driver needs to initialize something... */
514 if (sl
->family
->fops
&& sl
->family
->fops
->add_slave
&&
515 ((err
= sl
->family
->fops
->add_slave(sl
)) < 0)) {
517 "sysfs file creation for [%s] failed. err=%d\n",
518 sl
->dev
.bus_id
, err
);
522 list_add_tail(&sl
->w1_slave_entry
, &sl
->master
->slist
);
527 sysfs_remove_bin_file(&sl
->dev
.kobj
, &w1_slave_attr_bin_id
);
529 device_remove_file(&sl
->dev
, &w1_slave_attr_name
);
531 device_unregister(&sl
->dev
);
535 static int w1_attach_slave_device(struct w1_master
*dev
, struct w1_reg_num
*rn
)
540 struct w1_netlink_msg msg
;
542 sl
= kmalloc(sizeof(struct w1_slave
), GFP_KERNEL
);
545 "%s: failed to allocate new slave device.\n",
550 memset(sl
, 0, sizeof(*sl
));
552 sl
->owner
= THIS_MODULE
;
554 set_bit(W1_SLAVE_ACTIVE
, (long *)&sl
->flags
);
556 memset(&msg
, 0, sizeof(msg
));
557 memcpy(&sl
->reg_num
, rn
, sizeof(sl
->reg_num
));
558 atomic_set(&sl
->refcnt
, 0);
559 init_completion(&sl
->released
);
561 spin_lock(&w1_flock
);
562 f
= w1_family_registered(rn
->family
);
564 f
= &w1_default_family
;
565 dev_info(&dev
->dev
, "Family %x for %02x.%012llx.%02x is not registered.\n",
566 rn
->family
, rn
->family
,
567 (unsigned long long)rn
->id
, rn
->crc
);
570 spin_unlock(&w1_flock
);
575 err
= __w1_attach_slave_device(sl
);
577 dev_err(&dev
->dev
, "%s: Attaching %s failed.\n", __func__
,
579 w1_family_put(sl
->family
);
584 sl
->ttl
= dev
->slave_ttl
;
587 memcpy(msg
.id
.id
, rn
, sizeof(msg
.id
));
588 msg
.type
= W1_SLAVE_ADD
;
589 w1_netlink_send(dev
, &msg
);
594 static void w1_slave_detach(struct w1_slave
*sl
)
596 struct w1_netlink_msg msg
;
598 dev_dbg(&sl
->dev
, "%s: detaching %s [%p].\n", __func__
, sl
->name
, sl
);
600 list_del(&sl
->w1_slave_entry
);
602 if (sl
->family
->fops
&& sl
->family
->fops
->remove_slave
)
603 sl
->family
->fops
->remove_slave(sl
);
605 memset(&msg
, 0, sizeof(msg
));
606 memcpy(msg
.id
.id
, &sl
->reg_num
, sizeof(msg
.id
));
607 msg
.type
= W1_SLAVE_REMOVE
;
608 w1_netlink_send(sl
->master
, &msg
);
610 sysfs_remove_bin_file(&sl
->dev
.kobj
, &w1_slave_attr_bin_id
);
611 device_remove_file(&sl
->dev
, &w1_slave_attr_name
);
612 device_unregister(&sl
->dev
);
614 wait_for_completion(&sl
->released
);
618 static struct w1_master
*w1_search_master(void *data
)
620 struct w1_master
*dev
;
624 list_for_each_entry(dev
, &w1_masters
, w1_master_entry
) {
625 if (dev
->bus_master
->data
== data
) {
627 atomic_inc(&dev
->refcnt
);
633 return (found
)?dev
:NULL
;
636 struct w1_master
*w1_search_master_id(u32 id
)
638 struct w1_master
*dev
;
642 list_for_each_entry(dev
, &w1_masters
, w1_master_entry
) {
645 atomic_inc(&dev
->refcnt
);
651 return (found
)?dev
:NULL
;
654 struct w1_slave
*w1_search_slave(struct w1_reg_num
*id
)
656 struct w1_master
*dev
;
657 struct w1_slave
*sl
= NULL
;
661 list_for_each_entry(dev
, &w1_masters
, w1_master_entry
) {
663 list_for_each_entry(sl
, &dev
->slist
, w1_slave_entry
) {
664 if (sl
->reg_num
.family
== id
->family
&&
665 sl
->reg_num
.id
== id
->id
&&
666 sl
->reg_num
.crc
== id
->crc
) {
668 atomic_inc(&dev
->refcnt
);
669 atomic_inc(&sl
->refcnt
);
680 return (found
)?sl
:NULL
;
683 void w1_reconnect_slaves(struct w1_family
*f
)
685 struct w1_master
*dev
;
688 list_for_each_entry(dev
, &w1_masters
, w1_master_entry
) {
689 dev_dbg(&dev
->dev
, "Reconnecting slaves in %s into new family %02x.\n",
691 set_bit(W1_MASTER_NEED_RECONNECT
, &dev
->flags
);
696 static void w1_slave_found(void *data
, u64 rn
)
700 struct list_head
*ent
;
701 struct w1_reg_num
*tmp
;
702 int family_found
= 0;
703 struct w1_master
*dev
;
704 u64 rn_le
= cpu_to_le64(rn
);
706 dev
= w1_search_master(data
);
708 printk(KERN_ERR
"Failed to find w1 master device for data %p, "
709 "it is impossible.\n", data
);
713 tmp
= (struct w1_reg_num
*) &rn
;
716 list_for_each(ent
, &dev
->slist
) {
718 sl
= list_entry(ent
, struct w1_slave
, w1_slave_entry
);
720 if (sl
->reg_num
.family
== tmp
->family
&&
721 sl
->reg_num
.id
== tmp
->id
&&
722 sl
->reg_num
.crc
== tmp
->crc
) {
723 set_bit(W1_SLAVE_ACTIVE
, (long *)&sl
->flags
);
725 } else if (sl
->reg_num
.family
== tmp
->family
) {
733 if (slave_count
== dev
->slave_count
&&
734 rn
&& ((rn
>> 56) & 0xff) == w1_calc_crc8((u8
*)&rn_le
, 7)) {
735 w1_attach_slave_device(dev
, tmp
);
738 atomic_dec(&dev
->refcnt
);
742 * Performs a ROM Search & registers any devices found.
743 * The 1-wire search is a simple binary tree search.
744 * For each bit of the address, we read two bits and write one bit.
745 * The bit written will put to sleep all devies that don't match that bit.
746 * When the two reads differ, the direction choice is obvious.
747 * When both bits are 0, we must choose a path to take.
748 * When we can scan all 64 bits without having to choose a path, we are done.
750 * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
752 * @dev The master device to search
753 * @cb Function to call when a device is found
755 void w1_search(struct w1_master
*dev
, u8 search_type
, w1_slave_found_callback cb
)
757 u64 last_rn
, rn
, tmp64
;
758 int i
, slave_count
= 0;
759 int last_zero
, last_device
;
760 int search_bit
, desc_bit
;
770 while ( !last_device
&& (slave_count
++ < dev
->max_slave_count
) ) {
775 * Reset bus and all 1-wire device state machines
776 * so they can respond to our requests.
778 * Return 0 - device(s) present, 1 - no devices present.
780 if (w1_reset_bus(dev
)) {
781 dev_dbg(&dev
->dev
, "No devices present on the wire.\n");
785 /* Start the search */
786 w1_write_8(dev
, search_type
);
787 for (i
= 0; i
< 64; ++i
) {
788 /* Determine the direction/search bit */
790 search_bit
= 1; /* took the 0 path last time, so take the 1 path */
791 else if (i
> desc_bit
)
792 search_bit
= 0; /* take the 0 path on the next branch */
794 search_bit
= ((last_rn
>> i
) & 0x1);
796 /** Read two bits and write one bit */
797 triplet_ret
= w1_triplet(dev
, search_bit
);
799 /* quit if no device responded */
800 if ( (triplet_ret
& 0x03) == 0x03 )
803 /* If both directions were valid, and we took the 0 path... */
804 if (triplet_ret
== 0)
807 /* extract the direction taken & update the device number */
808 tmp64
= (triplet_ret
>> 2);
812 if ( (triplet_ret
& 0x03) != 0x03 ) {
813 if ( (desc_bit
== last_zero
) || (last_zero
< 0))
815 desc_bit
= last_zero
;
816 cb(dev
->bus_master
->data
, rn
);
821 static int w1_control(void *data
)
823 struct w1_slave
*sl
, *sln
;
824 struct w1_master
*dev
, *n
;
825 int have_to_wait
= 0;
827 while (!kthread_should_stop() || have_to_wait
) {
831 msleep_interruptible(w1_control_timeout
* 1000);
833 list_for_each_entry_safe(dev
, n
, &w1_masters
, w1_master_entry
) {
834 if (!kthread_should_stop() && !dev
->flags
)
837 * Little race: we can create thread but not set the flag.
838 * Get a chance for external process to set flag up.
840 if (!dev
->initialized
) {
845 if (kthread_should_stop() || test_bit(W1_MASTER_NEED_EXIT
, &dev
->flags
)) {
846 set_bit(W1_MASTER_NEED_EXIT
, &dev
->flags
);
849 list_del(&dev
->w1_master_entry
);
853 list_for_each_entry_safe(sl
, sln
, &dev
->slist
, w1_slave_entry
) {
856 w1_destroy_master_attributes(dev
);
858 atomic_dec(&dev
->refcnt
);
862 if (test_bit(W1_MASTER_NEED_RECONNECT
, &dev
->flags
)) {
863 dev_dbg(&dev
->dev
, "Reconnecting slaves in device %s.\n", dev
->name
);
865 list_for_each_entry_safe(sl
, sln
, &dev
->slist
, w1_slave_entry
) {
866 if (sl
->family
->fid
== W1_FAMILY_DEFAULT
) {
867 struct w1_reg_num rn
;
869 memcpy(&rn
, &sl
->reg_num
, sizeof(rn
));
872 w1_attach_slave_device(dev
, &rn
);
875 dev_dbg(&dev
->dev
, "Reconnecting slaves in device %s has been finished.\n", dev
->name
);
876 clear_bit(W1_MASTER_NEED_RECONNECT
, &dev
->flags
);
885 void w1_search_process(struct w1_master
*dev
, u8 search_type
)
887 struct w1_slave
*sl
, *sln
;
889 list_for_each_entry(sl
, &dev
->slist
, w1_slave_entry
)
890 clear_bit(W1_SLAVE_ACTIVE
, (long *)&sl
->flags
);
892 w1_search_devices(dev
, search_type
, w1_slave_found
);
894 list_for_each_entry_safe(sl
, sln
, &dev
->slist
, w1_slave_entry
) {
895 if (!test_bit(W1_SLAVE_ACTIVE
, (unsigned long *)&sl
->flags
) && !--sl
->ttl
) {
899 } else if (test_bit(W1_SLAVE_ACTIVE
, (unsigned long *)&sl
->flags
))
900 sl
->ttl
= dev
->slave_ttl
;
903 if (dev
->search_count
> 0)
907 int w1_process(void *data
)
909 struct w1_master
*dev
= (struct w1_master
*) data
;
911 while (!kthread_should_stop() && !test_bit(W1_MASTER_NEED_EXIT
, &dev
->flags
)) {
913 msleep_interruptible(w1_timeout
* 1000);
915 if (kthread_should_stop() || test_bit(W1_MASTER_NEED_EXIT
, &dev
->flags
))
918 if (!dev
->initialized
)
921 if (dev
->search_count
== 0)
924 if (down_interruptible(&dev
->mutex
))
927 w1_search_process(dev
, W1_SEARCH
);
932 atomic_dec(&dev
->refcnt
);
937 static int w1_init(void)
941 printk(KERN_INFO
"Driver for 1-wire Dallas network protocol.\n");
945 retval
= bus_register(&w1_bus_type
);
947 printk(KERN_ERR
"Failed to register bus. err=%d.\n", retval
);
948 goto err_out_exit_init
;
951 retval
= driver_register(&w1_master_driver
);
954 "Failed to register master driver. err=%d.\n",
956 goto err_out_bus_unregister
;
959 retval
= driver_register(&w1_slave_driver
);
962 "Failed to register master driver. err=%d.\n",
964 goto err_out_master_unregister
;
967 w1_control_thread
= kthread_run(w1_control
, NULL
, "w1_control");
968 if (IS_ERR(w1_control_thread
)) {
969 retval
= PTR_ERR(w1_control_thread
);
970 printk(KERN_ERR
"Failed to create control thread. err=%d\n",
972 goto err_out_slave_unregister
;
977 err_out_slave_unregister
:
978 driver_unregister(&w1_slave_driver
);
980 err_out_master_unregister
:
981 driver_unregister(&w1_master_driver
);
983 err_out_bus_unregister
:
984 bus_unregister(&w1_bus_type
);
990 static void w1_fini(void)
992 struct w1_master
*dev
;
994 list_for_each_entry(dev
, &w1_masters
, w1_master_entry
)
995 __w1_remove_master_device(dev
);
999 kthread_stop(w1_control_thread
);
1001 driver_unregister(&w1_slave_driver
);
1002 driver_unregister(&w1_master_driver
);
1003 bus_unregister(&w1_bus_type
);
1006 module_init(w1_init
);
1007 module_exit(w1_fini
);