mm: fix BUG_ON() in vmf_insert_pfn_pud() from VM_MIXEDMAP removal
[linux/fpc-iii.git] / drivers / w1 / w1.c
blob890c038c25f8717f38d1c901d4ec9662d13905c5
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>
28 #include <linux/hwmon.h>
29 #include <linux/of.h>
31 #include <linux/atomic.h>
33 #include "w1_internal.h"
34 #include "w1_netlink.h"
36 #define W1_FAMILY_DEFAULT 0
38 static int w1_timeout = 10;
39 module_param_named(timeout, w1_timeout, int, 0);
40 MODULE_PARM_DESC(timeout, "time in seconds between automatic slave searches");
42 static int w1_timeout_us = 0;
43 module_param_named(timeout_us, w1_timeout_us, int, 0);
44 MODULE_PARM_DESC(timeout_us,
45 "time in microseconds between automatic slave searches");
47 /* A search stops when w1_max_slave_count devices have been found in that
48 * search. The next search will start over and detect the same set of devices
49 * on a static 1-wire bus. Memory is not allocated based on this number, just
50 * on the number of devices known to the kernel. Having a high number does not
51 * consume additional resources. As a special case, if there is only one
52 * device on the network and w1_max_slave_count is set to 1, the device id can
53 * be read directly skipping the normal slower search process.
55 int w1_max_slave_count = 64;
56 module_param_named(max_slave_count, w1_max_slave_count, int, 0);
57 MODULE_PARM_DESC(max_slave_count,
58 "maximum number of slaves detected in a search");
60 int w1_max_slave_ttl = 10;
61 module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
62 MODULE_PARM_DESC(slave_ttl,
63 "Number of searches not seeing a slave before it will be removed");
65 DEFINE_MUTEX(w1_mlock);
66 LIST_HEAD(w1_masters);
68 static int w1_master_match(struct device *dev, struct device_driver *drv)
70 return 1;
73 static int w1_master_probe(struct device *dev)
75 return -ENODEV;
78 static void w1_master_release(struct device *dev)
80 struct w1_master *md = dev_to_w1_master(dev);
82 dev_dbg(dev, "%s: Releasing %s.\n", __func__, md->name);
83 memset(md, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
84 kfree(md);
87 static void w1_slave_release(struct device *dev)
89 struct w1_slave *sl = dev_to_w1_slave(dev);
91 dev_dbg(dev, "%s: Releasing %s [%p]\n", __func__, sl->name, sl);
93 w1_family_put(sl->family);
94 sl->master->slave_count--;
97 static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf)
99 struct w1_slave *sl = dev_to_w1_slave(dev);
101 return sprintf(buf, "%s\n", sl->name);
103 static DEVICE_ATTR_RO(name);
105 static ssize_t id_show(struct device *dev,
106 struct device_attribute *attr, char *buf)
108 struct w1_slave *sl = dev_to_w1_slave(dev);
109 ssize_t count = sizeof(sl->reg_num);
111 memcpy(buf, (u8 *)&sl->reg_num, count);
112 return count;
114 static DEVICE_ATTR_RO(id);
116 static struct attribute *w1_slave_attrs[] = {
117 &dev_attr_name.attr,
118 &dev_attr_id.attr,
119 NULL,
121 ATTRIBUTE_GROUPS(w1_slave);
123 /* Default family */
125 static ssize_t rw_write(struct file *filp, struct kobject *kobj,
126 struct bin_attribute *bin_attr, char *buf, loff_t off,
127 size_t count)
129 struct w1_slave *sl = kobj_to_w1_slave(kobj);
131 mutex_lock(&sl->master->mutex);
132 if (w1_reset_select_slave(sl)) {
133 count = 0;
134 goto out_up;
137 w1_write_block(sl->master, buf, count);
139 out_up:
140 mutex_unlock(&sl->master->mutex);
141 return count;
144 static ssize_t rw_read(struct file *filp, struct kobject *kobj,
145 struct bin_attribute *bin_attr, char *buf, loff_t off,
146 size_t count)
148 struct w1_slave *sl = kobj_to_w1_slave(kobj);
150 mutex_lock(&sl->master->mutex);
151 w1_read_block(sl->master, buf, count);
152 mutex_unlock(&sl->master->mutex);
153 return count;
156 static BIN_ATTR_RW(rw, PAGE_SIZE);
158 static struct bin_attribute *w1_slave_bin_attrs[] = {
159 &bin_attr_rw,
160 NULL,
163 static const struct attribute_group w1_slave_default_group = {
164 .bin_attrs = w1_slave_bin_attrs,
167 static const struct attribute_group *w1_slave_default_groups[] = {
168 &w1_slave_default_group,
169 NULL,
172 static struct w1_family_ops w1_default_fops = {
173 .groups = w1_slave_default_groups,
176 static struct w1_family w1_default_family = {
177 .fops = &w1_default_fops,
180 static int w1_uevent(struct device *dev, struct kobj_uevent_env *env);
182 static struct bus_type w1_bus_type = {
183 .name = "w1",
184 .match = w1_master_match,
185 .uevent = w1_uevent,
188 struct device_driver w1_master_driver = {
189 .name = "w1_master_driver",
190 .bus = &w1_bus_type,
191 .probe = w1_master_probe,
194 struct device w1_master_device = {
195 .parent = NULL,
196 .bus = &w1_bus_type,
197 .init_name = "w1 bus master",
198 .driver = &w1_master_driver,
199 .release = &w1_master_release
202 static struct device_driver w1_slave_driver = {
203 .name = "w1_slave_driver",
204 .bus = &w1_bus_type,
207 #if 0
208 struct device w1_slave_device = {
209 .parent = NULL,
210 .bus = &w1_bus_type,
211 .init_name = "w1 bus slave",
212 .driver = &w1_slave_driver,
213 .release = &w1_slave_release
215 #endif /* 0 */
217 static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
219 struct w1_master *md = dev_to_w1_master(dev);
220 ssize_t count;
222 mutex_lock(&md->mutex);
223 count = sprintf(buf, "%s\n", md->name);
224 mutex_unlock(&md->mutex);
226 return count;
229 static ssize_t w1_master_attribute_store_search(struct device * dev,
230 struct device_attribute *attr,
231 const char * buf, size_t count)
233 long tmp;
234 struct w1_master *md = dev_to_w1_master(dev);
235 int ret;
237 ret = kstrtol(buf, 0, &tmp);
238 if (ret)
239 return ret;
241 mutex_lock(&md->mutex);
242 md->search_count = tmp;
243 mutex_unlock(&md->mutex);
244 /* Only wake if it is going to be searching. */
245 if (tmp)
246 wake_up_process(md->thread);
248 return count;
251 static ssize_t w1_master_attribute_show_search(struct device *dev,
252 struct device_attribute *attr,
253 char *buf)
255 struct w1_master *md = dev_to_w1_master(dev);
256 ssize_t count;
258 mutex_lock(&md->mutex);
259 count = sprintf(buf, "%d\n", md->search_count);
260 mutex_unlock(&md->mutex);
262 return count;
265 static ssize_t w1_master_attribute_store_pullup(struct device *dev,
266 struct device_attribute *attr,
267 const char *buf, size_t count)
269 long tmp;
270 struct w1_master *md = dev_to_w1_master(dev);
271 int ret;
273 ret = kstrtol(buf, 0, &tmp);
274 if (ret)
275 return ret;
277 mutex_lock(&md->mutex);
278 md->enable_pullup = tmp;
279 mutex_unlock(&md->mutex);
281 return count;
284 static ssize_t w1_master_attribute_show_pullup(struct device *dev,
285 struct device_attribute *attr,
286 char *buf)
288 struct w1_master *md = dev_to_w1_master(dev);
289 ssize_t count;
291 mutex_lock(&md->mutex);
292 count = sprintf(buf, "%d\n", md->enable_pullup);
293 mutex_unlock(&md->mutex);
295 return count;
298 static ssize_t w1_master_attribute_show_pointer(struct device *dev, struct device_attribute *attr, char *buf)
300 struct w1_master *md = dev_to_w1_master(dev);
301 ssize_t count;
303 mutex_lock(&md->mutex);
304 count = sprintf(buf, "0x%p\n", md->bus_master);
305 mutex_unlock(&md->mutex);
306 return count;
309 static ssize_t w1_master_attribute_show_timeout(struct device *dev, struct device_attribute *attr, char *buf)
311 ssize_t count;
312 count = sprintf(buf, "%d\n", w1_timeout);
313 return count;
316 static ssize_t w1_master_attribute_show_timeout_us(struct device *dev,
317 struct device_attribute *attr, char *buf)
319 ssize_t count;
320 count = sprintf(buf, "%d\n", w1_timeout_us);
321 return count;
324 static ssize_t w1_master_attribute_store_max_slave_count(struct device *dev,
325 struct device_attribute *attr, const char *buf, size_t count)
327 int tmp;
328 struct w1_master *md = dev_to_w1_master(dev);
330 if (kstrtoint(buf, 0, &tmp) || tmp < 1)
331 return -EINVAL;
333 mutex_lock(&md->mutex);
334 md->max_slave_count = tmp;
335 /* allow each time the max_slave_count is updated */
336 clear_bit(W1_WARN_MAX_COUNT, &md->flags);
337 mutex_unlock(&md->mutex);
339 return count;
342 static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
344 struct w1_master *md = dev_to_w1_master(dev);
345 ssize_t count;
347 mutex_lock(&md->mutex);
348 count = sprintf(buf, "%d\n", md->max_slave_count);
349 mutex_unlock(&md->mutex);
350 return count;
353 static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf)
355 struct w1_master *md = dev_to_w1_master(dev);
356 ssize_t count;
358 mutex_lock(&md->mutex);
359 count = sprintf(buf, "%lu\n", md->attempts);
360 mutex_unlock(&md->mutex);
361 return count;
364 static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
366 struct w1_master *md = dev_to_w1_master(dev);
367 ssize_t count;
369 mutex_lock(&md->mutex);
370 count = sprintf(buf, "%d\n", md->slave_count);
371 mutex_unlock(&md->mutex);
372 return count;
375 static ssize_t w1_master_attribute_show_slaves(struct device *dev,
376 struct device_attribute *attr, char *buf)
378 struct w1_master *md = dev_to_w1_master(dev);
379 int c = PAGE_SIZE;
380 struct list_head *ent, *n;
381 struct w1_slave *sl = NULL;
383 mutex_lock(&md->list_mutex);
385 list_for_each_safe(ent, n, &md->slist) {
386 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
388 c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
390 if (!sl)
391 c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
393 mutex_unlock(&md->list_mutex);
395 return PAGE_SIZE - c;
398 static ssize_t w1_master_attribute_show_add(struct device *dev,
399 struct device_attribute *attr, char *buf)
401 int c = PAGE_SIZE;
402 c -= snprintf(buf+PAGE_SIZE - c, c,
403 "write device id xx-xxxxxxxxxxxx to add slave\n");
404 return PAGE_SIZE - c;
407 static int w1_atoreg_num(struct device *dev, const char *buf, size_t count,
408 struct w1_reg_num *rn)
410 unsigned int family;
411 unsigned long long id;
412 int i;
413 u64 rn64_le;
415 /* The CRC value isn't read from the user because the sysfs directory
416 * doesn't include it and most messages from the bus search don't
417 * print it either. It would be unreasonable for the user to then
418 * provide it.
420 const char *error_msg = "bad slave string format, expecting "
421 "ff-dddddddddddd\n";
423 if (buf[2] != '-') {
424 dev_err(dev, "%s", error_msg);
425 return -EINVAL;
427 i = sscanf(buf, "%02x-%012llx", &family, &id);
428 if (i != 2) {
429 dev_err(dev, "%s", error_msg);
430 return -EINVAL;
432 rn->family = family;
433 rn->id = id;
435 rn64_le = cpu_to_le64(*(u64 *)rn);
436 rn->crc = w1_calc_crc8((u8 *)&rn64_le, 7);
438 #if 0
439 dev_info(dev, "With CRC device is %02x.%012llx.%02x.\n",
440 rn->family, (unsigned long long)rn->id, rn->crc);
441 #endif
443 return 0;
446 /* Searches the slaves in the w1_master and returns a pointer or NULL.
447 * Note: must not hold list_mutex
449 struct w1_slave *w1_slave_search_device(struct w1_master *dev,
450 struct w1_reg_num *rn)
452 struct w1_slave *sl;
453 mutex_lock(&dev->list_mutex);
454 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
455 if (sl->reg_num.family == rn->family &&
456 sl->reg_num.id == rn->id &&
457 sl->reg_num.crc == rn->crc) {
458 mutex_unlock(&dev->list_mutex);
459 return sl;
462 mutex_unlock(&dev->list_mutex);
463 return NULL;
466 static ssize_t w1_master_attribute_store_add(struct device *dev,
467 struct device_attribute *attr,
468 const char *buf, size_t count)
470 struct w1_master *md = dev_to_w1_master(dev);
471 struct w1_reg_num rn;
472 struct w1_slave *sl;
473 ssize_t result = count;
475 if (w1_atoreg_num(dev, buf, count, &rn))
476 return -EINVAL;
478 mutex_lock(&md->mutex);
479 sl = w1_slave_search_device(md, &rn);
480 /* It would be nice to do a targeted search one the one-wire bus
481 * for the new device to see if it is out there or not. But the
482 * current search doesn't support that.
484 if (sl) {
485 dev_info(dev, "Device %s already exists\n", sl->name);
486 result = -EINVAL;
487 } else {
488 w1_attach_slave_device(md, &rn);
490 mutex_unlock(&md->mutex);
492 return result;
495 static ssize_t w1_master_attribute_show_remove(struct device *dev,
496 struct device_attribute *attr, char *buf)
498 int c = PAGE_SIZE;
499 c -= snprintf(buf+PAGE_SIZE - c, c,
500 "write device id xx-xxxxxxxxxxxx to remove slave\n");
501 return PAGE_SIZE - c;
504 static ssize_t w1_master_attribute_store_remove(struct device *dev,
505 struct device_attribute *attr,
506 const char *buf, size_t count)
508 struct w1_master *md = dev_to_w1_master(dev);
509 struct w1_reg_num rn;
510 struct w1_slave *sl;
511 ssize_t result = count;
513 if (w1_atoreg_num(dev, buf, count, &rn))
514 return -EINVAL;
516 mutex_lock(&md->mutex);
517 sl = w1_slave_search_device(md, &rn);
518 if (sl) {
519 result = w1_slave_detach(sl);
520 /* refcnt 0 means it was detached in the call */
521 if (result == 0)
522 result = count;
523 } else {
524 dev_info(dev, "Device %02x-%012llx doesn't exists\n", rn.family,
525 (unsigned long long)rn.id);
526 result = -EINVAL;
528 mutex_unlock(&md->mutex);
530 return result;
533 #define W1_MASTER_ATTR_RO(_name, _mode) \
534 struct device_attribute w1_master_attribute_##_name = \
535 __ATTR(w1_master_##_name, _mode, \
536 w1_master_attribute_show_##_name, NULL)
538 #define W1_MASTER_ATTR_RW(_name, _mode) \
539 struct device_attribute w1_master_attribute_##_name = \
540 __ATTR(w1_master_##_name, _mode, \
541 w1_master_attribute_show_##_name, \
542 w1_master_attribute_store_##_name)
544 static W1_MASTER_ATTR_RO(name, S_IRUGO);
545 static W1_MASTER_ATTR_RO(slaves, S_IRUGO);
546 static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);
547 static W1_MASTER_ATTR_RW(max_slave_count, S_IRUGO | S_IWUSR | S_IWGRP);
548 static W1_MASTER_ATTR_RO(attempts, S_IRUGO);
549 static W1_MASTER_ATTR_RO(timeout, S_IRUGO);
550 static W1_MASTER_ATTR_RO(timeout_us, S_IRUGO);
551 static W1_MASTER_ATTR_RO(pointer, S_IRUGO);
552 static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUSR | S_IWGRP);
553 static W1_MASTER_ATTR_RW(pullup, S_IRUGO | S_IWUSR | S_IWGRP);
554 static W1_MASTER_ATTR_RW(add, S_IRUGO | S_IWUSR | S_IWGRP);
555 static W1_MASTER_ATTR_RW(remove, S_IRUGO | S_IWUSR | S_IWGRP);
557 static struct attribute *w1_master_default_attrs[] = {
558 &w1_master_attribute_name.attr,
559 &w1_master_attribute_slaves.attr,
560 &w1_master_attribute_slave_count.attr,
561 &w1_master_attribute_max_slave_count.attr,
562 &w1_master_attribute_attempts.attr,
563 &w1_master_attribute_timeout.attr,
564 &w1_master_attribute_timeout_us.attr,
565 &w1_master_attribute_pointer.attr,
566 &w1_master_attribute_search.attr,
567 &w1_master_attribute_pullup.attr,
568 &w1_master_attribute_add.attr,
569 &w1_master_attribute_remove.attr,
570 NULL
573 static const struct attribute_group w1_master_defattr_group = {
574 .attrs = w1_master_default_attrs,
577 int w1_create_master_attributes(struct w1_master *master)
579 return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
582 void w1_destroy_master_attributes(struct w1_master *master)
584 sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
587 static int w1_uevent(struct device *dev, struct kobj_uevent_env *env)
589 struct w1_master *md = NULL;
590 struct w1_slave *sl = NULL;
591 char *event_owner, *name;
592 int err = 0;
594 if (dev->driver == &w1_master_driver) {
595 md = container_of(dev, struct w1_master, dev);
596 event_owner = "master";
597 name = md->name;
598 } else if (dev->driver == &w1_slave_driver) {
599 sl = container_of(dev, struct w1_slave, dev);
600 event_owner = "slave";
601 name = sl->name;
602 } else {
603 dev_dbg(dev, "Unknown event.\n");
604 return -EINVAL;
607 dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n",
608 event_owner, name, dev_name(dev));
610 if (dev->driver != &w1_slave_driver || !sl)
611 goto end;
613 err = add_uevent_var(env, "W1_FID=%02X", sl->reg_num.family);
614 if (err)
615 goto end;
617 err = add_uevent_var(env, "W1_SLAVE_ID=%024LX",
618 (unsigned long long)sl->reg_num.id);
619 end:
620 return err;
623 static int w1_family_notify(unsigned long action, struct w1_slave *sl)
625 struct w1_family_ops *fops;
626 int err;
628 fops = sl->family->fops;
630 if (!fops)
631 return 0;
633 switch (action) {
634 case BUS_NOTIFY_ADD_DEVICE:
635 /* if the family driver needs to initialize something... */
636 if (fops->add_slave) {
637 err = fops->add_slave(sl);
638 if (err < 0) {
639 dev_err(&sl->dev,
640 "add_slave() call failed. err=%d\n",
641 err);
642 return err;
645 if (fops->groups) {
646 err = sysfs_create_groups(&sl->dev.kobj, fops->groups);
647 if (err) {
648 dev_err(&sl->dev,
649 "sysfs group creation failed. err=%d\n",
650 err);
651 return err;
654 if (IS_REACHABLE(CONFIG_HWMON) && fops->chip_info) {
655 struct device *hwmon
656 = hwmon_device_register_with_info(&sl->dev,
657 "w1_slave_temp", sl,
658 fops->chip_info,
659 NULL);
660 if (IS_ERR(hwmon)) {
661 dev_warn(&sl->dev,
662 "could not create hwmon device\n");
663 } else {
664 sl->hwmon = hwmon;
667 break;
668 case BUS_NOTIFY_DEL_DEVICE:
669 if (IS_REACHABLE(CONFIG_HWMON) && fops->chip_info &&
670 sl->hwmon)
671 hwmon_device_unregister(sl->hwmon);
672 if (fops->remove_slave)
673 sl->family->fops->remove_slave(sl);
674 if (fops->groups)
675 sysfs_remove_groups(&sl->dev.kobj, fops->groups);
676 break;
678 return 0;
681 static int __w1_attach_slave_device(struct w1_slave *sl)
683 int err;
685 sl->dev.parent = &sl->master->dev;
686 sl->dev.driver = &w1_slave_driver;
687 sl->dev.bus = &w1_bus_type;
688 sl->dev.release = &w1_slave_release;
689 sl->dev.groups = w1_slave_groups;
690 sl->dev.of_node = of_find_matching_node(sl->master->dev.of_node,
691 sl->family->of_match_table);
693 dev_set_name(&sl->dev, "%02x-%012llx",
694 (unsigned int) sl->reg_num.family,
695 (unsigned long long) sl->reg_num.id);
696 snprintf(&sl->name[0], sizeof(sl->name),
697 "%02x-%012llx",
698 (unsigned int) sl->reg_num.family,
699 (unsigned long long) sl->reg_num.id);
701 dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__,
702 dev_name(&sl->dev), sl);
704 /* suppress for w1_family_notify before sending KOBJ_ADD */
705 dev_set_uevent_suppress(&sl->dev, true);
707 err = device_register(&sl->dev);
708 if (err < 0) {
709 dev_err(&sl->dev,
710 "Device registration [%s] failed. err=%d\n",
711 dev_name(&sl->dev), err);
712 put_device(&sl->dev);
713 return err;
715 w1_family_notify(BUS_NOTIFY_ADD_DEVICE, sl);
717 dev_set_uevent_suppress(&sl->dev, false);
718 kobject_uevent(&sl->dev.kobj, KOBJ_ADD);
720 mutex_lock(&sl->master->list_mutex);
721 list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
722 mutex_unlock(&sl->master->list_mutex);
724 return 0;
727 int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
729 struct w1_slave *sl;
730 struct w1_family *f;
731 int err;
732 struct w1_netlink_msg msg;
734 sl = kzalloc(sizeof(struct w1_slave), GFP_KERNEL);
735 if (!sl) {
736 dev_err(&dev->dev,
737 "%s: failed to allocate new slave device.\n",
738 __func__);
739 return -ENOMEM;
743 sl->owner = THIS_MODULE;
744 sl->master = dev;
745 set_bit(W1_SLAVE_ACTIVE, &sl->flags);
747 memset(&msg, 0, sizeof(msg));
748 memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
749 atomic_set(&sl->refcnt, 1);
750 atomic_inc(&sl->master->refcnt);
751 dev->slave_count++;
752 dev_info(&dev->dev, "Attaching one wire slave %02x.%012llx crc %02x\n",
753 rn->family, (unsigned long long)rn->id, rn->crc);
755 /* slave modules need to be loaded in a context with unlocked mutex */
756 mutex_unlock(&dev->mutex);
757 request_module("w1-family-0x%02X", rn->family);
758 mutex_lock(&dev->mutex);
760 spin_lock(&w1_flock);
761 f = w1_family_registered(rn->family);
762 if (!f) {
763 f= &w1_default_family;
764 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
765 rn->family, rn->family,
766 (unsigned long long)rn->id, rn->crc);
768 __w1_family_get(f);
769 spin_unlock(&w1_flock);
771 sl->family = f;
773 err = __w1_attach_slave_device(sl);
774 if (err < 0) {
775 dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
776 sl->name);
777 dev->slave_count--;
778 w1_family_put(sl->family);
779 atomic_dec(&sl->master->refcnt);
780 kfree(sl);
781 return err;
784 sl->ttl = dev->slave_ttl;
786 memcpy(msg.id.id, rn, sizeof(msg.id));
787 msg.type = W1_SLAVE_ADD;
788 w1_netlink_send(dev, &msg);
790 return 0;
793 int w1_unref_slave(struct w1_slave *sl)
795 struct w1_master *dev = sl->master;
796 int refcnt;
797 mutex_lock(&dev->list_mutex);
798 refcnt = atomic_sub_return(1, &sl->refcnt);
799 if (refcnt == 0) {
800 struct w1_netlink_msg msg;
802 dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__,
803 sl->name, sl);
805 list_del(&sl->w1_slave_entry);
807 memset(&msg, 0, sizeof(msg));
808 memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
809 msg.type = W1_SLAVE_REMOVE;
810 w1_netlink_send(sl->master, &msg);
812 w1_family_notify(BUS_NOTIFY_DEL_DEVICE, sl);
813 device_unregister(&sl->dev);
814 #ifdef DEBUG
815 memset(sl, 0, sizeof(*sl));
816 #endif
817 kfree(sl);
819 atomic_dec(&dev->refcnt);
820 mutex_unlock(&dev->list_mutex);
821 return refcnt;
824 int w1_slave_detach(struct w1_slave *sl)
826 /* Only detach a slave once as it decreases the refcnt each time. */
827 int destroy_now;
828 mutex_lock(&sl->master->list_mutex);
829 destroy_now = !test_bit(W1_SLAVE_DETACH, &sl->flags);
830 set_bit(W1_SLAVE_DETACH, &sl->flags);
831 mutex_unlock(&sl->master->list_mutex);
833 if (destroy_now)
834 destroy_now = !w1_unref_slave(sl);
835 return destroy_now ? 0 : -EBUSY;
838 struct w1_master *w1_search_master_id(u32 id)
840 struct w1_master *dev;
841 int found = 0;
843 mutex_lock(&w1_mlock);
844 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
845 if (dev->id == id) {
846 found = 1;
847 atomic_inc(&dev->refcnt);
848 break;
851 mutex_unlock(&w1_mlock);
853 return (found)?dev:NULL;
856 struct w1_slave *w1_search_slave(struct w1_reg_num *id)
858 struct w1_master *dev;
859 struct w1_slave *sl = NULL;
860 int found = 0;
862 mutex_lock(&w1_mlock);
863 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
864 mutex_lock(&dev->list_mutex);
865 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
866 if (sl->reg_num.family == id->family &&
867 sl->reg_num.id == id->id &&
868 sl->reg_num.crc == id->crc) {
869 found = 1;
870 atomic_inc(&dev->refcnt);
871 atomic_inc(&sl->refcnt);
872 break;
875 mutex_unlock(&dev->list_mutex);
877 if (found)
878 break;
880 mutex_unlock(&w1_mlock);
882 return (found)?sl:NULL;
885 void w1_reconnect_slaves(struct w1_family *f, int attach)
887 struct w1_slave *sl, *sln;
888 struct w1_master *dev;
890 mutex_lock(&w1_mlock);
891 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
892 dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
893 "for family %02x.\n", dev->name, f->fid);
894 mutex_lock(&dev->mutex);
895 mutex_lock(&dev->list_mutex);
896 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
897 /* If it is a new family, slaves with the default
898 * family driver and are that family will be
899 * connected. If the family is going away, devices
900 * matching that family are reconneced.
902 if ((attach && sl->family->fid == W1_FAMILY_DEFAULT
903 && sl->reg_num.family == f->fid) ||
904 (!attach && sl->family->fid == f->fid)) {
905 struct w1_reg_num rn;
907 mutex_unlock(&dev->list_mutex);
908 memcpy(&rn, &sl->reg_num, sizeof(rn));
909 /* If it was already in use let the automatic
910 * scan pick it up again later.
912 if (!w1_slave_detach(sl))
913 w1_attach_slave_device(dev, &rn);
914 mutex_lock(&dev->list_mutex);
917 dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
918 "has been finished.\n", dev->name);
919 mutex_unlock(&dev->list_mutex);
920 mutex_unlock(&dev->mutex);
922 mutex_unlock(&w1_mlock);
925 void w1_slave_found(struct w1_master *dev, u64 rn)
927 struct w1_slave *sl;
928 struct w1_reg_num *tmp;
929 u64 rn_le = cpu_to_le64(rn);
931 atomic_inc(&dev->refcnt);
933 tmp = (struct w1_reg_num *) &rn;
935 sl = w1_slave_search_device(dev, tmp);
936 if (sl) {
937 set_bit(W1_SLAVE_ACTIVE, &sl->flags);
938 } else {
939 if (rn && tmp->crc == w1_calc_crc8((u8 *)&rn_le, 7))
940 w1_attach_slave_device(dev, tmp);
943 atomic_dec(&dev->refcnt);
947 * w1_search() - Performs a ROM Search & registers any devices found.
948 * @dev: The master device to search
949 * @search_type: W1_SEARCH to search all devices, or W1_ALARM_SEARCH
950 * to return only devices in the alarmed state
951 * @cb: Function to call when a device is found
953 * The 1-wire search is a simple binary tree search.
954 * For each bit of the address, we read two bits and write one bit.
955 * The bit written will put to sleep all devies that don't match that bit.
956 * When the two reads differ, the direction choice is obvious.
957 * When both bits are 0, we must choose a path to take.
958 * When we can scan all 64 bits without having to choose a path, we are done.
960 * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
963 void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
965 u64 last_rn, rn, tmp64;
966 int i, slave_count = 0;
967 int last_zero, last_device;
968 int search_bit, desc_bit;
969 u8 triplet_ret = 0;
971 search_bit = 0;
972 rn = dev->search_id;
973 last_rn = 0;
974 last_device = 0;
975 last_zero = -1;
977 desc_bit = 64;
979 while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
980 last_rn = rn;
981 rn = 0;
984 * Reset bus and all 1-wire device state machines
985 * so they can respond to our requests.
987 * Return 0 - device(s) present, 1 - no devices present.
989 mutex_lock(&dev->bus_mutex);
990 if (w1_reset_bus(dev)) {
991 mutex_unlock(&dev->bus_mutex);
992 dev_dbg(&dev->dev, "No devices present on the wire.\n");
993 break;
996 /* Do fast search on single slave bus */
997 if (dev->max_slave_count == 1) {
998 int rv;
999 w1_write_8(dev, W1_READ_ROM);
1000 rv = w1_read_block(dev, (u8 *)&rn, 8);
1001 mutex_unlock(&dev->bus_mutex);
1003 if (rv == 8 && rn)
1004 cb(dev, rn);
1006 break;
1009 /* Start the search */
1010 w1_write_8(dev, search_type);
1011 for (i = 0; i < 64; ++i) {
1012 /* Determine the direction/search bit */
1013 if (i == desc_bit)
1014 search_bit = 1; /* took the 0 path last time, so take the 1 path */
1015 else if (i > desc_bit)
1016 search_bit = 0; /* take the 0 path on the next branch */
1017 else
1018 search_bit = ((last_rn >> i) & 0x1);
1020 /* Read two bits and write one bit */
1021 triplet_ret = w1_triplet(dev, search_bit);
1023 /* quit if no device responded */
1024 if ( (triplet_ret & 0x03) == 0x03 )
1025 break;
1027 /* If both directions were valid, and we took the 0 path... */
1028 if (triplet_ret == 0)
1029 last_zero = i;
1031 /* extract the direction taken & update the device number */
1032 tmp64 = (triplet_ret >> 2);
1033 rn |= (tmp64 << i);
1035 if (test_bit(W1_ABORT_SEARCH, &dev->flags)) {
1036 mutex_unlock(&dev->bus_mutex);
1037 dev_dbg(&dev->dev, "Abort w1_search\n");
1038 return;
1041 mutex_unlock(&dev->bus_mutex);
1043 if ( (triplet_ret & 0x03) != 0x03 ) {
1044 if ((desc_bit == last_zero) || (last_zero < 0)) {
1045 last_device = 1;
1046 dev->search_id = 0;
1047 } else {
1048 dev->search_id = rn;
1050 desc_bit = last_zero;
1051 cb(dev, rn);
1054 if (!last_device && slave_count == dev->max_slave_count &&
1055 !test_bit(W1_WARN_MAX_COUNT, &dev->flags)) {
1056 /* Only max_slave_count will be scanned in a search,
1057 * but it will start where it left off next search
1058 * until all ids are identified and then it will start
1059 * over. A continued search will report the previous
1060 * last id as the first id (provided it is still on the
1061 * bus).
1063 dev_info(&dev->dev, "%s: max_slave_count %d reached, "
1064 "will continue next search.\n", __func__,
1065 dev->max_slave_count);
1066 set_bit(W1_WARN_MAX_COUNT, &dev->flags);
1071 void w1_search_process_cb(struct w1_master *dev, u8 search_type,
1072 w1_slave_found_callback cb)
1074 struct w1_slave *sl, *sln;
1076 mutex_lock(&dev->list_mutex);
1077 list_for_each_entry(sl, &dev->slist, w1_slave_entry)
1078 clear_bit(W1_SLAVE_ACTIVE, &sl->flags);
1079 mutex_unlock(&dev->list_mutex);
1081 w1_search_devices(dev, search_type, cb);
1083 mutex_lock(&dev->list_mutex);
1084 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
1085 if (!test_bit(W1_SLAVE_ACTIVE, &sl->flags) && !--sl->ttl) {
1086 mutex_unlock(&dev->list_mutex);
1087 w1_slave_detach(sl);
1088 mutex_lock(&dev->list_mutex);
1090 else if (test_bit(W1_SLAVE_ACTIVE, &sl->flags))
1091 sl->ttl = dev->slave_ttl;
1093 mutex_unlock(&dev->list_mutex);
1095 if (dev->search_count > 0)
1096 dev->search_count--;
1099 static void w1_search_process(struct w1_master *dev, u8 search_type)
1101 w1_search_process_cb(dev, search_type, w1_slave_found);
1105 * w1_process_callbacks() - execute each dev->async_list callback entry
1106 * @dev: w1_master device
1108 * The w1 master list_mutex must be held.
1110 * Return: 1 if there were commands to executed 0 otherwise
1112 int w1_process_callbacks(struct w1_master *dev)
1114 int ret = 0;
1115 struct w1_async_cmd *async_cmd, *async_n;
1117 /* The list can be added to in another thread, loop until it is empty */
1118 while (!list_empty(&dev->async_list)) {
1119 list_for_each_entry_safe(async_cmd, async_n, &dev->async_list,
1120 async_entry) {
1121 /* drop the lock, if it is a search it can take a long
1122 * time */
1123 mutex_unlock(&dev->list_mutex);
1124 async_cmd->cb(dev, async_cmd);
1125 ret = 1;
1126 mutex_lock(&dev->list_mutex);
1129 return ret;
1132 int w1_process(void *data)
1134 struct w1_master *dev = (struct w1_master *) data;
1135 /* As long as w1_timeout is only set by a module parameter the sleep
1136 * time can be calculated in jiffies once.
1138 const unsigned long jtime =
1139 usecs_to_jiffies(w1_timeout * 1000000 + w1_timeout_us);
1140 /* remainder if it woke up early */
1141 unsigned long jremain = 0;
1143 for (;;) {
1145 if (!jremain && dev->search_count) {
1146 mutex_lock(&dev->mutex);
1147 w1_search_process(dev, W1_SEARCH);
1148 mutex_unlock(&dev->mutex);
1151 mutex_lock(&dev->list_mutex);
1152 /* Note, w1_process_callback drops the lock while processing,
1153 * but locks it again before returning.
1155 if (!w1_process_callbacks(dev) && jremain) {
1156 /* a wake up is either to stop the thread, process
1157 * callbacks, or search, it isn't process callbacks, so
1158 * schedule a search.
1160 jremain = 1;
1163 __set_current_state(TASK_INTERRUPTIBLE);
1165 /* hold list_mutex until after interruptible to prevent loosing
1166 * the wakeup signal when async_cmd is added.
1168 mutex_unlock(&dev->list_mutex);
1170 if (kthread_should_stop())
1171 break;
1173 /* Only sleep when the search is active. */
1174 if (dev->search_count) {
1175 if (!jremain)
1176 jremain = jtime;
1177 jremain = schedule_timeout(jremain);
1179 else
1180 schedule();
1183 atomic_dec(&dev->refcnt);
1185 return 0;
1188 static int __init w1_init(void)
1190 int retval;
1192 pr_info("Driver for 1-wire Dallas network protocol.\n");
1194 w1_init_netlink();
1196 retval = bus_register(&w1_bus_type);
1197 if (retval) {
1198 pr_err("Failed to register bus. err=%d.\n", retval);
1199 goto err_out_exit_init;
1202 retval = driver_register(&w1_master_driver);
1203 if (retval) {
1204 pr_err("Failed to register master driver. err=%d.\n",
1205 retval);
1206 goto err_out_bus_unregister;
1209 retval = driver_register(&w1_slave_driver);
1210 if (retval) {
1211 pr_err("Failed to register slave driver. err=%d.\n",
1212 retval);
1213 goto err_out_master_unregister;
1216 return 0;
1218 #if 0
1219 /* For undoing the slave register if there was a step after it. */
1220 err_out_slave_unregister:
1221 driver_unregister(&w1_slave_driver);
1222 #endif
1224 err_out_master_unregister:
1225 driver_unregister(&w1_master_driver);
1227 err_out_bus_unregister:
1228 bus_unregister(&w1_bus_type);
1230 err_out_exit_init:
1231 return retval;
1234 static void __exit w1_fini(void)
1236 struct w1_master *dev;
1238 /* Set netlink removal messages and some cleanup */
1239 list_for_each_entry(dev, &w1_masters, w1_master_entry)
1240 __w1_remove_master_device(dev);
1242 w1_fini_netlink();
1244 driver_unregister(&w1_slave_driver);
1245 driver_unregister(&w1_master_driver);
1246 bus_unregister(&w1_bus_type);
1249 module_init(w1_init);
1250 module_exit(w1_fini);
1252 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
1253 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
1254 MODULE_LICENSE("GPL");