mm: Use static initialization for "srcu"
[linux/fpc-iii.git] / drivers / w1 / w1.c
blob90a3d9338fd282a5bc1c8a678c4a505544b86f70
1 /*
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>
31 #include "w1.h"
32 #include "w1_log.h"
33 #include "w1_int.h"
34 #include "w1_family.h"
35 #include "w1_netlink.h"
37 MODULE_LICENSE("GPL");
38 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
39 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
41 static int w1_timeout = 10;
42 static int w1_timeout_us = 0;
43 int w1_max_slave_count = 64;
44 int w1_max_slave_ttl = 10;
46 module_param_named(timeout, w1_timeout, int, 0);
47 MODULE_PARM_DESC(timeout, "time in seconds between automatic slave searches");
48 module_param_named(timeout_us, w1_timeout_us, int, 0);
49 MODULE_PARM_DESC(timeout_us,
50 "time in microseconds between automatic slave searches");
51 /* A search stops when w1_max_slave_count devices have been found in that
52 * search. The next search will start over and detect the same set of devices
53 * on a static 1-wire bus. Memory is not allocated based on this number, just
54 * on the number of devices known to the kernel. Having a high number does not
55 * consume additional resources. As a special case, if there is only one
56 * device on the network and w1_max_slave_count is set to 1, the device id can
57 * be read directly skipping the normal slower search process.
59 module_param_named(max_slave_count, w1_max_slave_count, int, 0);
60 MODULE_PARM_DESC(max_slave_count,
61 "maximum number of slaves detected in a search");
62 module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
63 MODULE_PARM_DESC(slave_ttl,
64 "Number of searches not seeing a slave before it will be removed");
66 DEFINE_MUTEX(w1_mlock);
67 LIST_HEAD(w1_masters);
69 static int w1_master_match(struct device *dev, struct device_driver *drv)
71 return 1;
74 static int w1_master_probe(struct device *dev)
76 return -ENODEV;
79 static void w1_master_release(struct device *dev)
81 struct w1_master *md = dev_to_w1_master(dev);
83 dev_dbg(dev, "%s: Releasing %s.\n", __func__, md->name);
84 memset(md, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
85 kfree(md);
88 static void w1_slave_release(struct device *dev)
90 struct w1_slave *sl = dev_to_w1_slave(dev);
92 dev_dbg(dev, "%s: Releasing %s [%p]\n", __func__, sl->name, sl);
94 w1_family_put(sl->family);
95 sl->master->slave_count--;
98 static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf)
100 struct w1_slave *sl = dev_to_w1_slave(dev);
102 return sprintf(buf, "%s\n", sl->name);
104 static DEVICE_ATTR_RO(name);
106 static ssize_t id_show(struct device *dev,
107 struct device_attribute *attr, char *buf)
109 struct w1_slave *sl = dev_to_w1_slave(dev);
110 ssize_t count = sizeof(sl->reg_num);
112 memcpy(buf, (u8 *)&sl->reg_num, count);
113 return count;
115 static DEVICE_ATTR_RO(id);
117 static struct attribute *w1_slave_attrs[] = {
118 &dev_attr_name.attr,
119 &dev_attr_id.attr,
120 NULL,
122 ATTRIBUTE_GROUPS(w1_slave);
124 /* Default family */
126 static ssize_t rw_write(struct file *filp, struct kobject *kobj,
127 struct bin_attribute *bin_attr, char *buf, loff_t off,
128 size_t count)
130 struct w1_slave *sl = kobj_to_w1_slave(kobj);
132 mutex_lock(&sl->master->mutex);
133 if (w1_reset_select_slave(sl)) {
134 count = 0;
135 goto out_up;
138 w1_write_block(sl->master, buf, count);
140 out_up:
141 mutex_unlock(&sl->master->mutex);
142 return count;
145 static ssize_t rw_read(struct file *filp, struct kobject *kobj,
146 struct bin_attribute *bin_attr, char *buf, loff_t off,
147 size_t count)
149 struct w1_slave *sl = kobj_to_w1_slave(kobj);
151 mutex_lock(&sl->master->mutex);
152 w1_read_block(sl->master, buf, count);
153 mutex_unlock(&sl->master->mutex);
154 return count;
157 static BIN_ATTR_RW(rw, PAGE_SIZE);
159 static struct bin_attribute *w1_slave_bin_attrs[] = {
160 &bin_attr_rw,
161 NULL,
164 static const struct attribute_group w1_slave_default_group = {
165 .bin_attrs = w1_slave_bin_attrs,
168 static const struct attribute_group *w1_slave_default_groups[] = {
169 &w1_slave_default_group,
170 NULL,
173 static struct w1_family_ops w1_default_fops = {
174 .groups = w1_slave_default_groups,
177 static struct w1_family w1_default_family = {
178 .fops = &w1_default_fops,
181 static int w1_uevent(struct device *dev, struct kobj_uevent_env *env);
183 static struct bus_type w1_bus_type = {
184 .name = "w1",
185 .match = w1_master_match,
186 .uevent = w1_uevent,
189 struct device_driver w1_master_driver = {
190 .name = "w1_master_driver",
191 .bus = &w1_bus_type,
192 .probe = w1_master_probe,
195 struct device w1_master_device = {
196 .parent = NULL,
197 .bus = &w1_bus_type,
198 .init_name = "w1 bus master",
199 .driver = &w1_master_driver,
200 .release = &w1_master_release
203 static struct device_driver w1_slave_driver = {
204 .name = "w1_slave_driver",
205 .bus = &w1_bus_type,
208 #if 0
209 struct device w1_slave_device = {
210 .parent = NULL,
211 .bus = &w1_bus_type,
212 .init_name = "w1 bus slave",
213 .driver = &w1_slave_driver,
214 .release = &w1_slave_release
216 #endif /* 0 */
218 static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
220 struct w1_master *md = dev_to_w1_master(dev);
221 ssize_t count;
223 mutex_lock(&md->mutex);
224 count = sprintf(buf, "%s\n", md->name);
225 mutex_unlock(&md->mutex);
227 return count;
230 static ssize_t w1_master_attribute_store_search(struct device * dev,
231 struct device_attribute *attr,
232 const char * buf, size_t count)
234 long tmp;
235 struct w1_master *md = dev_to_w1_master(dev);
236 int ret;
238 ret = kstrtol(buf, 0, &tmp);
239 if (ret)
240 return ret;
242 mutex_lock(&md->mutex);
243 md->search_count = tmp;
244 mutex_unlock(&md->mutex);
245 /* Only wake if it is going to be searching. */
246 if (tmp)
247 wake_up_process(md->thread);
249 return count;
252 static ssize_t w1_master_attribute_show_search(struct device *dev,
253 struct device_attribute *attr,
254 char *buf)
256 struct w1_master *md = dev_to_w1_master(dev);
257 ssize_t count;
259 mutex_lock(&md->mutex);
260 count = sprintf(buf, "%d\n", md->search_count);
261 mutex_unlock(&md->mutex);
263 return count;
266 static ssize_t w1_master_attribute_store_pullup(struct device *dev,
267 struct device_attribute *attr,
268 const char *buf, size_t count)
270 long tmp;
271 struct w1_master *md = dev_to_w1_master(dev);
272 int ret;
274 ret = kstrtol(buf, 0, &tmp);
275 if (ret)
276 return ret;
278 mutex_lock(&md->mutex);
279 md->enable_pullup = tmp;
280 mutex_unlock(&md->mutex);
282 return count;
285 static ssize_t w1_master_attribute_show_pullup(struct device *dev,
286 struct device_attribute *attr,
287 char *buf)
289 struct w1_master *md = dev_to_w1_master(dev);
290 ssize_t count;
292 mutex_lock(&md->mutex);
293 count = sprintf(buf, "%d\n", md->enable_pullup);
294 mutex_unlock(&md->mutex);
296 return count;
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);
302 ssize_t count;
304 mutex_lock(&md->mutex);
305 count = sprintf(buf, "0x%p\n", md->bus_master);
306 mutex_unlock(&md->mutex);
307 return count;
310 static ssize_t w1_master_attribute_show_timeout(struct device *dev, struct device_attribute *attr, char *buf)
312 ssize_t count;
313 count = sprintf(buf, "%d\n", w1_timeout);
314 return count;
317 static ssize_t w1_master_attribute_show_timeout_us(struct device *dev,
318 struct device_attribute *attr, char *buf)
320 ssize_t count;
321 count = sprintf(buf, "%d\n", w1_timeout_us);
322 return count;
325 static ssize_t w1_master_attribute_store_max_slave_count(struct device *dev,
326 struct device_attribute *attr, const char *buf, size_t count)
328 int tmp;
329 struct w1_master *md = dev_to_w1_master(dev);
331 if (kstrtoint(buf, 0, &tmp) || tmp < 1)
332 return -EINVAL;
334 mutex_lock(&md->mutex);
335 md->max_slave_count = tmp;
336 /* allow each time the max_slave_count is updated */
337 clear_bit(W1_WARN_MAX_COUNT, &md->flags);
338 mutex_unlock(&md->mutex);
340 return count;
343 static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
345 struct w1_master *md = dev_to_w1_master(dev);
346 ssize_t count;
348 mutex_lock(&md->mutex);
349 count = sprintf(buf, "%d\n", md->max_slave_count);
350 mutex_unlock(&md->mutex);
351 return count;
354 static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf)
356 struct w1_master *md = dev_to_w1_master(dev);
357 ssize_t count;
359 mutex_lock(&md->mutex);
360 count = sprintf(buf, "%lu\n", md->attempts);
361 mutex_unlock(&md->mutex);
362 return count;
365 static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
367 struct w1_master *md = dev_to_w1_master(dev);
368 ssize_t count;
370 mutex_lock(&md->mutex);
371 count = sprintf(buf, "%d\n", md->slave_count);
372 mutex_unlock(&md->mutex);
373 return count;
376 static ssize_t w1_master_attribute_show_slaves(struct device *dev,
377 struct device_attribute *attr, char *buf)
379 struct w1_master *md = dev_to_w1_master(dev);
380 int c = PAGE_SIZE;
381 struct list_head *ent, *n;
382 struct w1_slave *sl = NULL;
384 mutex_lock(&md->list_mutex);
386 list_for_each_safe(ent, n, &md->slist) {
387 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
389 c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
391 if (!sl)
392 c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
394 mutex_unlock(&md->list_mutex);
396 return PAGE_SIZE - c;
399 static ssize_t w1_master_attribute_show_add(struct device *dev,
400 struct device_attribute *attr, char *buf)
402 int c = PAGE_SIZE;
403 c -= snprintf(buf+PAGE_SIZE - c, c,
404 "write device id xx-xxxxxxxxxxxx to add slave\n");
405 return PAGE_SIZE - c;
408 static int w1_atoreg_num(struct device *dev, const char *buf, size_t count,
409 struct w1_reg_num *rn)
411 unsigned int family;
412 unsigned long long id;
413 int i;
414 u64 rn64_le;
416 /* The CRC value isn't read from the user because the sysfs directory
417 * doesn't include it and most messages from the bus search don't
418 * print it either. It would be unreasonable for the user to then
419 * provide it.
421 const char *error_msg = "bad slave string format, expecting "
422 "ff-dddddddddddd\n";
424 if (buf[2] != '-') {
425 dev_err(dev, "%s", error_msg);
426 return -EINVAL;
428 i = sscanf(buf, "%02x-%012llx", &family, &id);
429 if (i != 2) {
430 dev_err(dev, "%s", error_msg);
431 return -EINVAL;
433 rn->family = family;
434 rn->id = id;
436 rn64_le = cpu_to_le64(*(u64 *)rn);
437 rn->crc = w1_calc_crc8((u8 *)&rn64_le, 7);
439 #if 0
440 dev_info(dev, "With CRC device is %02x.%012llx.%02x.\n",
441 rn->family, (unsigned long long)rn->id, rn->crc);
442 #endif
444 return 0;
447 /* Searches the slaves in the w1_master and returns a pointer or NULL.
448 * Note: must not hold list_mutex
450 struct w1_slave *w1_slave_search_device(struct w1_master *dev,
451 struct w1_reg_num *rn)
453 struct w1_slave *sl;
454 mutex_lock(&dev->list_mutex);
455 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
456 if (sl->reg_num.family == rn->family &&
457 sl->reg_num.id == rn->id &&
458 sl->reg_num.crc == rn->crc) {
459 mutex_unlock(&dev->list_mutex);
460 return sl;
463 mutex_unlock(&dev->list_mutex);
464 return NULL;
467 static ssize_t w1_master_attribute_store_add(struct device *dev,
468 struct device_attribute *attr,
469 const char *buf, size_t count)
471 struct w1_master *md = dev_to_w1_master(dev);
472 struct w1_reg_num rn;
473 struct w1_slave *sl;
474 ssize_t result = count;
476 if (w1_atoreg_num(dev, buf, count, &rn))
477 return -EINVAL;
479 mutex_lock(&md->mutex);
480 sl = w1_slave_search_device(md, &rn);
481 /* It would be nice to do a targeted search one the one-wire bus
482 * for the new device to see if it is out there or not. But the
483 * current search doesn't support that.
485 if (sl) {
486 dev_info(dev, "Device %s already exists\n", sl->name);
487 result = -EINVAL;
488 } else {
489 w1_attach_slave_device(md, &rn);
491 mutex_unlock(&md->mutex);
493 return result;
496 static ssize_t w1_master_attribute_show_remove(struct device *dev,
497 struct device_attribute *attr, char *buf)
499 int c = PAGE_SIZE;
500 c -= snprintf(buf+PAGE_SIZE - c, c,
501 "write device id xx-xxxxxxxxxxxx to remove slave\n");
502 return PAGE_SIZE - c;
505 static ssize_t w1_master_attribute_store_remove(struct device *dev,
506 struct device_attribute *attr,
507 const char *buf, size_t count)
509 struct w1_master *md = dev_to_w1_master(dev);
510 struct w1_reg_num rn;
511 struct w1_slave *sl;
512 ssize_t result = count;
514 if (w1_atoreg_num(dev, buf, count, &rn))
515 return -EINVAL;
517 mutex_lock(&md->mutex);
518 sl = w1_slave_search_device(md, &rn);
519 if (sl) {
520 result = w1_slave_detach(sl);
521 /* refcnt 0 means it was detached in the call */
522 if (result == 0)
523 result = count;
524 } else {
525 dev_info(dev, "Device %02x-%012llx doesn't exists\n", rn.family,
526 (unsigned long long)rn.id);
527 result = -EINVAL;
529 mutex_unlock(&md->mutex);
531 return result;
534 #define W1_MASTER_ATTR_RO(_name, _mode) \
535 struct device_attribute w1_master_attribute_##_name = \
536 __ATTR(w1_master_##_name, _mode, \
537 w1_master_attribute_show_##_name, NULL)
539 #define W1_MASTER_ATTR_RW(_name, _mode) \
540 struct device_attribute w1_master_attribute_##_name = \
541 __ATTR(w1_master_##_name, _mode, \
542 w1_master_attribute_show_##_name, \
543 w1_master_attribute_store_##_name)
545 static W1_MASTER_ATTR_RO(name, S_IRUGO);
546 static W1_MASTER_ATTR_RO(slaves, S_IRUGO);
547 static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);
548 static W1_MASTER_ATTR_RW(max_slave_count, S_IRUGO | S_IWUSR | S_IWGRP);
549 static W1_MASTER_ATTR_RO(attempts, S_IRUGO);
550 static W1_MASTER_ATTR_RO(timeout, S_IRUGO);
551 static W1_MASTER_ATTR_RO(timeout_us, S_IRUGO);
552 static W1_MASTER_ATTR_RO(pointer, S_IRUGO);
553 static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUSR | S_IWGRP);
554 static W1_MASTER_ATTR_RW(pullup, S_IRUGO | S_IWUSR | S_IWGRP);
555 static W1_MASTER_ATTR_RW(add, S_IRUGO | S_IWUSR | S_IWGRP);
556 static W1_MASTER_ATTR_RW(remove, S_IRUGO | S_IWUSR | S_IWGRP);
558 static struct attribute *w1_master_default_attrs[] = {
559 &w1_master_attribute_name.attr,
560 &w1_master_attribute_slaves.attr,
561 &w1_master_attribute_slave_count.attr,
562 &w1_master_attribute_max_slave_count.attr,
563 &w1_master_attribute_attempts.attr,
564 &w1_master_attribute_timeout.attr,
565 &w1_master_attribute_timeout_us.attr,
566 &w1_master_attribute_pointer.attr,
567 &w1_master_attribute_search.attr,
568 &w1_master_attribute_pullup.attr,
569 &w1_master_attribute_add.attr,
570 &w1_master_attribute_remove.attr,
571 NULL
574 static struct attribute_group w1_master_defattr_group = {
575 .attrs = w1_master_default_attrs,
578 int w1_create_master_attributes(struct w1_master *master)
580 return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
583 void w1_destroy_master_attributes(struct w1_master *master)
585 sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
588 static int w1_uevent(struct device *dev, struct kobj_uevent_env *env)
590 struct w1_master *md = NULL;
591 struct w1_slave *sl = NULL;
592 char *event_owner, *name;
593 int err = 0;
595 if (dev->driver == &w1_master_driver) {
596 md = container_of(dev, struct w1_master, dev);
597 event_owner = "master";
598 name = md->name;
599 } else if (dev->driver == &w1_slave_driver) {
600 sl = container_of(dev, struct w1_slave, dev);
601 event_owner = "slave";
602 name = sl->name;
603 } else {
604 dev_dbg(dev, "Unknown event.\n");
605 return -EINVAL;
608 dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n",
609 event_owner, name, dev_name(dev));
611 if (dev->driver != &w1_slave_driver || !sl)
612 goto end;
614 err = add_uevent_var(env, "W1_FID=%02X", sl->reg_num.family);
615 if (err)
616 goto end;
618 err = add_uevent_var(env, "W1_SLAVE_ID=%024LX",
619 (unsigned long long)sl->reg_num.id);
620 end:
621 return err;
624 static int w1_family_notify(unsigned long action, struct w1_slave *sl)
626 struct w1_family_ops *fops;
627 int err;
629 fops = sl->family->fops;
631 if (!fops)
632 return 0;
634 switch (action) {
635 case BUS_NOTIFY_ADD_DEVICE:
636 /* if the family driver needs to initialize something... */
637 if (fops->add_slave) {
638 err = fops->add_slave(sl);
639 if (err < 0) {
640 dev_err(&sl->dev,
641 "add_slave() call failed. err=%d\n",
642 err);
643 return err;
646 if (fops->groups) {
647 err = sysfs_create_groups(&sl->dev.kobj, fops->groups);
648 if (err) {
649 dev_err(&sl->dev,
650 "sysfs group creation failed. err=%d\n",
651 err);
652 return err;
656 break;
657 case BUS_NOTIFY_DEL_DEVICE:
658 if (fops->remove_slave)
659 sl->family->fops->remove_slave(sl);
660 if (fops->groups)
661 sysfs_remove_groups(&sl->dev.kobj, fops->groups);
662 break;
664 return 0;
667 static int __w1_attach_slave_device(struct w1_slave *sl)
669 int err;
671 sl->dev.parent = &sl->master->dev;
672 sl->dev.driver = &w1_slave_driver;
673 sl->dev.bus = &w1_bus_type;
674 sl->dev.release = &w1_slave_release;
675 sl->dev.groups = w1_slave_groups;
677 dev_set_name(&sl->dev, "%02x-%012llx",
678 (unsigned int) sl->reg_num.family,
679 (unsigned long long) sl->reg_num.id);
680 snprintf(&sl->name[0], sizeof(sl->name),
681 "%02x-%012llx",
682 (unsigned int) sl->reg_num.family,
683 (unsigned long long) sl->reg_num.id);
685 dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__,
686 dev_name(&sl->dev), sl);
688 /* suppress for w1_family_notify before sending KOBJ_ADD */
689 dev_set_uevent_suppress(&sl->dev, true);
691 err = device_register(&sl->dev);
692 if (err < 0) {
693 dev_err(&sl->dev,
694 "Device registration [%s] failed. err=%d\n",
695 dev_name(&sl->dev), err);
696 return err;
698 w1_family_notify(BUS_NOTIFY_ADD_DEVICE, sl);
700 dev_set_uevent_suppress(&sl->dev, false);
701 kobject_uevent(&sl->dev.kobj, KOBJ_ADD);
703 mutex_lock(&sl->master->list_mutex);
704 list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
705 mutex_unlock(&sl->master->list_mutex);
707 return 0;
710 int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
712 struct w1_slave *sl;
713 struct w1_family *f;
714 int err;
715 struct w1_netlink_msg msg;
717 sl = kzalloc(sizeof(struct w1_slave), GFP_KERNEL);
718 if (!sl) {
719 dev_err(&dev->dev,
720 "%s: failed to allocate new slave device.\n",
721 __func__);
722 return -ENOMEM;
726 sl->owner = THIS_MODULE;
727 sl->master = dev;
728 set_bit(W1_SLAVE_ACTIVE, &sl->flags);
730 memset(&msg, 0, sizeof(msg));
731 memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
732 atomic_set(&sl->refcnt, 1);
733 atomic_inc(&sl->master->refcnt);
735 /* slave modules need to be loaded in a context with unlocked mutex */
736 mutex_unlock(&dev->mutex);
737 request_module("w1-family-0x%02x", rn->family);
738 mutex_lock(&dev->mutex);
740 spin_lock(&w1_flock);
741 f = w1_family_registered(rn->family);
742 if (!f) {
743 f= &w1_default_family;
744 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
745 rn->family, rn->family,
746 (unsigned long long)rn->id, rn->crc);
748 __w1_family_get(f);
749 spin_unlock(&w1_flock);
751 sl->family = f;
754 err = __w1_attach_slave_device(sl);
755 if (err < 0) {
756 dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
757 sl->name);
758 w1_family_put(sl->family);
759 atomic_dec(&sl->master->refcnt);
760 kfree(sl);
761 return err;
764 sl->ttl = dev->slave_ttl;
765 dev->slave_count++;
767 memcpy(msg.id.id, rn, sizeof(msg.id));
768 msg.type = W1_SLAVE_ADD;
769 w1_netlink_send(dev, &msg);
771 return 0;
774 int w1_unref_slave(struct w1_slave *sl)
776 struct w1_master *dev = sl->master;
777 int refcnt;
778 mutex_lock(&dev->list_mutex);
779 refcnt = atomic_sub_return(1, &sl->refcnt);
780 if (refcnt == 0) {
781 struct w1_netlink_msg msg;
783 dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__,
784 sl->name, sl);
786 list_del(&sl->w1_slave_entry);
788 memset(&msg, 0, sizeof(msg));
789 memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
790 msg.type = W1_SLAVE_REMOVE;
791 w1_netlink_send(sl->master, &msg);
793 w1_family_notify(BUS_NOTIFY_DEL_DEVICE, sl);
794 device_unregister(&sl->dev);
795 #ifdef DEBUG
796 memset(sl, 0, sizeof(*sl));
797 #endif
798 kfree(sl);
800 atomic_dec(&dev->refcnt);
801 mutex_unlock(&dev->list_mutex);
802 return refcnt;
805 int w1_slave_detach(struct w1_slave *sl)
807 /* Only detach a slave once as it decreases the refcnt each time. */
808 int destroy_now;
809 mutex_lock(&sl->master->list_mutex);
810 destroy_now = !test_bit(W1_SLAVE_DETACH, &sl->flags);
811 set_bit(W1_SLAVE_DETACH, &sl->flags);
812 mutex_unlock(&sl->master->list_mutex);
814 if (destroy_now)
815 destroy_now = !w1_unref_slave(sl);
816 return destroy_now ? 0 : -EBUSY;
819 struct w1_master *w1_search_master_id(u32 id)
821 struct w1_master *dev;
822 int found = 0;
824 mutex_lock(&w1_mlock);
825 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
826 if (dev->id == id) {
827 found = 1;
828 atomic_inc(&dev->refcnt);
829 break;
832 mutex_unlock(&w1_mlock);
834 return (found)?dev:NULL;
837 struct w1_slave *w1_search_slave(struct w1_reg_num *id)
839 struct w1_master *dev;
840 struct w1_slave *sl = NULL;
841 int found = 0;
843 mutex_lock(&w1_mlock);
844 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
845 mutex_lock(&dev->list_mutex);
846 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
847 if (sl->reg_num.family == id->family &&
848 sl->reg_num.id == id->id &&
849 sl->reg_num.crc == id->crc) {
850 found = 1;
851 atomic_inc(&dev->refcnt);
852 atomic_inc(&sl->refcnt);
853 break;
856 mutex_unlock(&dev->list_mutex);
858 if (found)
859 break;
861 mutex_unlock(&w1_mlock);
863 return (found)?sl:NULL;
866 void w1_reconnect_slaves(struct w1_family *f, int attach)
868 struct w1_slave *sl, *sln;
869 struct w1_master *dev;
871 mutex_lock(&w1_mlock);
872 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
873 dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
874 "for family %02x.\n", dev->name, f->fid);
875 mutex_lock(&dev->mutex);
876 mutex_lock(&dev->list_mutex);
877 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
878 /* If it is a new family, slaves with the default
879 * family driver and are that family will be
880 * connected. If the family is going away, devices
881 * matching that family are reconneced.
883 if ((attach && sl->family->fid == W1_FAMILY_DEFAULT
884 && sl->reg_num.family == f->fid) ||
885 (!attach && sl->family->fid == f->fid)) {
886 struct w1_reg_num rn;
888 mutex_unlock(&dev->list_mutex);
889 memcpy(&rn, &sl->reg_num, sizeof(rn));
890 /* If it was already in use let the automatic
891 * scan pick it up again later.
893 if (!w1_slave_detach(sl))
894 w1_attach_slave_device(dev, &rn);
895 mutex_lock(&dev->list_mutex);
898 dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
899 "has been finished.\n", dev->name);
900 mutex_unlock(&dev->list_mutex);
901 mutex_unlock(&dev->mutex);
903 mutex_unlock(&w1_mlock);
906 void w1_slave_found(struct w1_master *dev, u64 rn)
908 struct w1_slave *sl;
909 struct w1_reg_num *tmp;
910 u64 rn_le = cpu_to_le64(rn);
912 atomic_inc(&dev->refcnt);
914 tmp = (struct w1_reg_num *) &rn;
916 sl = w1_slave_search_device(dev, tmp);
917 if (sl) {
918 set_bit(W1_SLAVE_ACTIVE, &sl->flags);
919 } else {
920 if (rn && tmp->crc == w1_calc_crc8((u8 *)&rn_le, 7))
921 w1_attach_slave_device(dev, tmp);
924 atomic_dec(&dev->refcnt);
928 * w1_search() - Performs a ROM Search & registers any devices found.
929 * @dev: The master device to search
930 * @search_type: W1_SEARCH to search all devices, or W1_ALARM_SEARCH
931 * to return only devices in the alarmed state
932 * @cb: Function to call when a device is found
934 * The 1-wire search is a simple binary tree search.
935 * For each bit of the address, we read two bits and write one bit.
936 * The bit written will put to sleep all devies that don't match that bit.
937 * When the two reads differ, the direction choice is obvious.
938 * When both bits are 0, we must choose a path to take.
939 * When we can scan all 64 bits without having to choose a path, we are done.
941 * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
944 void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
946 u64 last_rn, rn, tmp64;
947 int i, slave_count = 0;
948 int last_zero, last_device;
949 int search_bit, desc_bit;
950 u8 triplet_ret = 0;
952 search_bit = 0;
953 rn = dev->search_id;
954 last_rn = 0;
955 last_device = 0;
956 last_zero = -1;
958 desc_bit = 64;
960 while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
961 last_rn = rn;
962 rn = 0;
965 * Reset bus and all 1-wire device state machines
966 * so they can respond to our requests.
968 * Return 0 - device(s) present, 1 - no devices present.
970 mutex_lock(&dev->bus_mutex);
971 if (w1_reset_bus(dev)) {
972 mutex_unlock(&dev->bus_mutex);
973 dev_dbg(&dev->dev, "No devices present on the wire.\n");
974 break;
977 /* Do fast search on single slave bus */
978 if (dev->max_slave_count == 1) {
979 int rv;
980 w1_write_8(dev, W1_READ_ROM);
981 rv = w1_read_block(dev, (u8 *)&rn, 8);
982 mutex_unlock(&dev->bus_mutex);
984 if (rv == 8 && rn)
985 cb(dev, rn);
987 break;
990 /* Start the search */
991 w1_write_8(dev, search_type);
992 for (i = 0; i < 64; ++i) {
993 /* Determine the direction/search bit */
994 if (i == desc_bit)
995 search_bit = 1; /* took the 0 path last time, so take the 1 path */
996 else if (i > desc_bit)
997 search_bit = 0; /* take the 0 path on the next branch */
998 else
999 search_bit = ((last_rn >> i) & 0x1);
1001 /* Read two bits and write one bit */
1002 triplet_ret = w1_triplet(dev, search_bit);
1004 /* quit if no device responded */
1005 if ( (triplet_ret & 0x03) == 0x03 )
1006 break;
1008 /* If both directions were valid, and we took the 0 path... */
1009 if (triplet_ret == 0)
1010 last_zero = i;
1012 /* extract the direction taken & update the device number */
1013 tmp64 = (triplet_ret >> 2);
1014 rn |= (tmp64 << i);
1016 if (test_bit(W1_ABORT_SEARCH, &dev->flags)) {
1017 mutex_unlock(&dev->bus_mutex);
1018 dev_dbg(&dev->dev, "Abort w1_search\n");
1019 return;
1022 mutex_unlock(&dev->bus_mutex);
1024 if ( (triplet_ret & 0x03) != 0x03 ) {
1025 if ((desc_bit == last_zero) || (last_zero < 0)) {
1026 last_device = 1;
1027 dev->search_id = 0;
1028 } else {
1029 dev->search_id = rn;
1031 desc_bit = last_zero;
1032 cb(dev, rn);
1035 if (!last_device && slave_count == dev->max_slave_count &&
1036 !test_bit(W1_WARN_MAX_COUNT, &dev->flags)) {
1037 /* Only max_slave_count will be scanned in a search,
1038 * but it will start where it left off next search
1039 * until all ids are identified and then it will start
1040 * over. A continued search will report the previous
1041 * last id as the first id (provided it is still on the
1042 * bus).
1044 dev_info(&dev->dev, "%s: max_slave_count %d reached, "
1045 "will continue next search.\n", __func__,
1046 dev->max_slave_count);
1047 set_bit(W1_WARN_MAX_COUNT, &dev->flags);
1052 void w1_search_process_cb(struct w1_master *dev, u8 search_type,
1053 w1_slave_found_callback cb)
1055 struct w1_slave *sl, *sln;
1057 mutex_lock(&dev->list_mutex);
1058 list_for_each_entry(sl, &dev->slist, w1_slave_entry)
1059 clear_bit(W1_SLAVE_ACTIVE, &sl->flags);
1060 mutex_unlock(&dev->list_mutex);
1062 w1_search_devices(dev, search_type, cb);
1064 mutex_lock(&dev->list_mutex);
1065 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
1066 if (!test_bit(W1_SLAVE_ACTIVE, &sl->flags) && !--sl->ttl) {
1067 mutex_unlock(&dev->list_mutex);
1068 w1_slave_detach(sl);
1069 mutex_lock(&dev->list_mutex);
1071 else if (test_bit(W1_SLAVE_ACTIVE, &sl->flags))
1072 sl->ttl = dev->slave_ttl;
1074 mutex_unlock(&dev->list_mutex);
1076 if (dev->search_count > 0)
1077 dev->search_count--;
1080 static void w1_search_process(struct w1_master *dev, u8 search_type)
1082 w1_search_process_cb(dev, search_type, w1_slave_found);
1086 * w1_process_callbacks() - execute each dev->async_list callback entry
1087 * @dev: w1_master device
1089 * The w1 master list_mutex must be held.
1091 * Return: 1 if there were commands to executed 0 otherwise
1093 int w1_process_callbacks(struct w1_master *dev)
1095 int ret = 0;
1096 struct w1_async_cmd *async_cmd, *async_n;
1098 /* The list can be added to in another thread, loop until it is empty */
1099 while (!list_empty(&dev->async_list)) {
1100 list_for_each_entry_safe(async_cmd, async_n, &dev->async_list,
1101 async_entry) {
1102 /* drop the lock, if it is a search it can take a long
1103 * time */
1104 mutex_unlock(&dev->list_mutex);
1105 async_cmd->cb(dev, async_cmd);
1106 ret = 1;
1107 mutex_lock(&dev->list_mutex);
1110 return ret;
1113 int w1_process(void *data)
1115 struct w1_master *dev = (struct w1_master *) data;
1116 /* As long as w1_timeout is only set by a module parameter the sleep
1117 * time can be calculated in jiffies once.
1119 const unsigned long jtime =
1120 usecs_to_jiffies(w1_timeout * 1000000 + w1_timeout_us);
1121 /* remainder if it woke up early */
1122 unsigned long jremain = 0;
1124 for (;;) {
1126 if (!jremain && dev->search_count) {
1127 mutex_lock(&dev->mutex);
1128 w1_search_process(dev, W1_SEARCH);
1129 mutex_unlock(&dev->mutex);
1132 mutex_lock(&dev->list_mutex);
1133 /* Note, w1_process_callback drops the lock while processing,
1134 * but locks it again before returning.
1136 if (!w1_process_callbacks(dev) && jremain) {
1137 /* a wake up is either to stop the thread, process
1138 * callbacks, or search, it isn't process callbacks, so
1139 * schedule a search.
1141 jremain = 1;
1144 __set_current_state(TASK_INTERRUPTIBLE);
1146 /* hold list_mutex until after interruptible to prevent loosing
1147 * the wakeup signal when async_cmd is added.
1149 mutex_unlock(&dev->list_mutex);
1151 if (kthread_should_stop())
1152 break;
1154 /* Only sleep when the search is active. */
1155 if (dev->search_count) {
1156 if (!jremain)
1157 jremain = jtime;
1158 jremain = schedule_timeout(jremain);
1160 else
1161 schedule();
1164 atomic_dec(&dev->refcnt);
1166 return 0;
1169 static int __init w1_init(void)
1171 int retval;
1173 pr_info("Driver for 1-wire Dallas network protocol.\n");
1175 w1_init_netlink();
1177 retval = bus_register(&w1_bus_type);
1178 if (retval) {
1179 pr_err("Failed to register bus. err=%d.\n", retval);
1180 goto err_out_exit_init;
1183 retval = driver_register(&w1_master_driver);
1184 if (retval) {
1185 pr_err("Failed to register master driver. err=%d.\n",
1186 retval);
1187 goto err_out_bus_unregister;
1190 retval = driver_register(&w1_slave_driver);
1191 if (retval) {
1192 pr_err("Failed to register slave driver. err=%d.\n",
1193 retval);
1194 goto err_out_master_unregister;
1197 return 0;
1199 #if 0
1200 /* For undoing the slave register if there was a step after it. */
1201 err_out_slave_unregister:
1202 driver_unregister(&w1_slave_driver);
1203 #endif
1205 err_out_master_unregister:
1206 driver_unregister(&w1_master_driver);
1208 err_out_bus_unregister:
1209 bus_unregister(&w1_bus_type);
1211 err_out_exit_init:
1212 return retval;
1215 static void __exit w1_fini(void)
1217 struct w1_master *dev;
1219 /* Set netlink removal messages and some cleanup */
1220 list_for_each_entry(dev, &w1_masters, w1_master_entry)
1221 __w1_remove_master_device(dev);
1223 w1_fini_netlink();
1225 driver_unregister(&w1_slave_driver);
1226 driver_unregister(&w1_master_driver);
1227 bus_unregister(&w1_bus_type);
1230 module_init(w1_init);
1231 module_exit(w1_fini);