xref: /openbmc/linux/drivers/w1/w1.c (revision fcc8487d)
1 /*
2  * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
3  *
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.
8  *
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.
13  */
14 
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 
29 #include <linux/atomic.h>
30 
31 #include "w1.h"
32 #include "w1_int.h"
33 #include "w1_family.h"
34 #include "w1_netlink.h"
35 
36 MODULE_LICENSE("GPL");
37 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
38 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
39 
40 static int w1_timeout = 10;
41 static int w1_timeout_us = 0;
42 int w1_max_slave_count = 64;
43 int w1_max_slave_ttl = 10;
44 
45 module_param_named(timeout, w1_timeout, int, 0);
46 MODULE_PARM_DESC(timeout, "time in seconds between automatic slave searches");
47 module_param_named(timeout_us, w1_timeout_us, int, 0);
48 MODULE_PARM_DESC(timeout_us,
49 		 "time in microseconds between automatic slave searches");
50 /* A search stops when w1_max_slave_count devices have been found in that
51  * search.  The next search will start over and detect the same set of devices
52  * on a static 1-wire bus.  Memory is not allocated based on this number, just
53  * on the number of devices known to the kernel.  Having a high number does not
54  * consume additional resources.  As a special case, if there is only one
55  * device on the network and w1_max_slave_count is set to 1, the device id can
56  * be read directly skipping the normal slower search process.
57  */
58 module_param_named(max_slave_count, w1_max_slave_count, int, 0);
59 MODULE_PARM_DESC(max_slave_count,
60 	"maximum number of slaves detected in a search");
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");
64 
65 DEFINE_MUTEX(w1_mlock);
66 LIST_HEAD(w1_masters);
67 
68 static int w1_master_match(struct device *dev, struct device_driver *drv)
69 {
70 	return 1;
71 }
72 
73 static int w1_master_probe(struct device *dev)
74 {
75 	return -ENODEV;
76 }
77 
78 static void w1_master_release(struct device *dev)
79 {
80 	struct w1_master *md = dev_to_w1_master(dev);
81 
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);
85 }
86 
87 static void w1_slave_release(struct device *dev)
88 {
89 	struct w1_slave *sl = dev_to_w1_slave(dev);
90 
91 	dev_dbg(dev, "%s: Releasing %s [%p]\n", __func__, sl->name, sl);
92 
93 	w1_family_put(sl->family);
94 	sl->master->slave_count--;
95 }
96 
97 static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf)
98 {
99 	struct w1_slave *sl = dev_to_w1_slave(dev);
100 
101 	return sprintf(buf, "%s\n", sl->name);
102 }
103 static DEVICE_ATTR_RO(name);
104 
105 static ssize_t id_show(struct device *dev,
106 	struct device_attribute *attr, char *buf)
107 {
108 	struct w1_slave *sl = dev_to_w1_slave(dev);
109 	ssize_t count = sizeof(sl->reg_num);
110 
111 	memcpy(buf, (u8 *)&sl->reg_num, count);
112 	return count;
113 }
114 static DEVICE_ATTR_RO(id);
115 
116 static struct attribute *w1_slave_attrs[] = {
117 	&dev_attr_name.attr,
118 	&dev_attr_id.attr,
119 	NULL,
120 };
121 ATTRIBUTE_GROUPS(w1_slave);
122 
123 /* Default family */
124 
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)
128 {
129 	struct w1_slave *sl = kobj_to_w1_slave(kobj);
130 
131 	mutex_lock(&sl->master->mutex);
132 	if (w1_reset_select_slave(sl)) {
133 		count = 0;
134 		goto out_up;
135 	}
136 
137 	w1_write_block(sl->master, buf, count);
138 
139 out_up:
140 	mutex_unlock(&sl->master->mutex);
141 	return count;
142 }
143 
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)
147 {
148 	struct w1_slave *sl = kobj_to_w1_slave(kobj);
149 
150 	mutex_lock(&sl->master->mutex);
151 	w1_read_block(sl->master, buf, count);
152 	mutex_unlock(&sl->master->mutex);
153 	return count;
154 }
155 
156 static BIN_ATTR_RW(rw, PAGE_SIZE);
157 
158 static struct bin_attribute *w1_slave_bin_attrs[] = {
159 	&bin_attr_rw,
160 	NULL,
161 };
162 
163 static const struct attribute_group w1_slave_default_group = {
164 	.bin_attrs = w1_slave_bin_attrs,
165 };
166 
167 static const struct attribute_group *w1_slave_default_groups[] = {
168 	&w1_slave_default_group,
169 	NULL,
170 };
171 
172 static struct w1_family_ops w1_default_fops = {
173 	.groups		= w1_slave_default_groups,
174 };
175 
176 static struct w1_family w1_default_family = {
177 	.fops = &w1_default_fops,
178 };
179 
180 static int w1_uevent(struct device *dev, struct kobj_uevent_env *env);
181 
182 static struct bus_type w1_bus_type = {
183 	.name = "w1",
184 	.match = w1_master_match,
185 	.uevent = w1_uevent,
186 };
187 
188 struct device_driver w1_master_driver = {
189 	.name = "w1_master_driver",
190 	.bus = &w1_bus_type,
191 	.probe = w1_master_probe,
192 };
193 
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
200 };
201 
202 static struct device_driver w1_slave_driver = {
203 	.name = "w1_slave_driver",
204 	.bus = &w1_bus_type,
205 };
206 
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
214 };
215 #endif  /*  0  */
216 
217 static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
218 {
219 	struct w1_master *md = dev_to_w1_master(dev);
220 	ssize_t count;
221 
222 	mutex_lock(&md->mutex);
223 	count = sprintf(buf, "%s\n", md->name);
224 	mutex_unlock(&md->mutex);
225 
226 	return count;
227 }
228 
229 static ssize_t w1_master_attribute_store_search(struct device * dev,
230 						struct device_attribute *attr,
231 						const char * buf, size_t count)
232 {
233 	long tmp;
234 	struct w1_master *md = dev_to_w1_master(dev);
235 	int ret;
236 
237 	ret = kstrtol(buf, 0, &tmp);
238 	if (ret)
239 		return ret;
240 
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);
247 
248 	return count;
249 }
250 
251 static ssize_t w1_master_attribute_show_search(struct device *dev,
252 					       struct device_attribute *attr,
253 					       char *buf)
254 {
255 	struct w1_master *md = dev_to_w1_master(dev);
256 	ssize_t count;
257 
258 	mutex_lock(&md->mutex);
259 	count = sprintf(buf, "%d\n", md->search_count);
260 	mutex_unlock(&md->mutex);
261 
262 	return count;
263 }
264 
265 static ssize_t w1_master_attribute_store_pullup(struct device *dev,
266 						struct device_attribute *attr,
267 						const char *buf, size_t count)
268 {
269 	long tmp;
270 	struct w1_master *md = dev_to_w1_master(dev);
271 	int ret;
272 
273 	ret = kstrtol(buf, 0, &tmp);
274 	if (ret)
275 		return ret;
276 
277 	mutex_lock(&md->mutex);
278 	md->enable_pullup = tmp;
279 	mutex_unlock(&md->mutex);
280 
281 	return count;
282 }
283 
284 static ssize_t w1_master_attribute_show_pullup(struct device *dev,
285 					       struct device_attribute *attr,
286 					       char *buf)
287 {
288 	struct w1_master *md = dev_to_w1_master(dev);
289 	ssize_t count;
290 
291 	mutex_lock(&md->mutex);
292 	count = sprintf(buf, "%d\n", md->enable_pullup);
293 	mutex_unlock(&md->mutex);
294 
295 	return count;
296 }
297 
298 static ssize_t w1_master_attribute_show_pointer(struct device *dev, struct device_attribute *attr, char *buf)
299 {
300 	struct w1_master *md = dev_to_w1_master(dev);
301 	ssize_t count;
302 
303 	mutex_lock(&md->mutex);
304 	count = sprintf(buf, "0x%p\n", md->bus_master);
305 	mutex_unlock(&md->mutex);
306 	return count;
307 }
308 
309 static ssize_t w1_master_attribute_show_timeout(struct device *dev, struct device_attribute *attr, char *buf)
310 {
311 	ssize_t count;
312 	count = sprintf(buf, "%d\n", w1_timeout);
313 	return count;
314 }
315 
316 static ssize_t w1_master_attribute_show_timeout_us(struct device *dev,
317 	struct device_attribute *attr, char *buf)
318 {
319 	ssize_t count;
320 	count = sprintf(buf, "%d\n", w1_timeout_us);
321 	return count;
322 }
323 
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)
326 {
327 	int tmp;
328 	struct w1_master *md = dev_to_w1_master(dev);
329 
330 	if (kstrtoint(buf, 0, &tmp) || tmp < 1)
331 		return -EINVAL;
332 
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);
338 
339 	return count;
340 }
341 
342 static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
343 {
344 	struct w1_master *md = dev_to_w1_master(dev);
345 	ssize_t count;
346 
347 	mutex_lock(&md->mutex);
348 	count = sprintf(buf, "%d\n", md->max_slave_count);
349 	mutex_unlock(&md->mutex);
350 	return count;
351 }
352 
353 static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf)
354 {
355 	struct w1_master *md = dev_to_w1_master(dev);
356 	ssize_t count;
357 
358 	mutex_lock(&md->mutex);
359 	count = sprintf(buf, "%lu\n", md->attempts);
360 	mutex_unlock(&md->mutex);
361 	return count;
362 }
363 
364 static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
365 {
366 	struct w1_master *md = dev_to_w1_master(dev);
367 	ssize_t count;
368 
369 	mutex_lock(&md->mutex);
370 	count = sprintf(buf, "%d\n", md->slave_count);
371 	mutex_unlock(&md->mutex);
372 	return count;
373 }
374 
375 static ssize_t w1_master_attribute_show_slaves(struct device *dev,
376 	struct device_attribute *attr, char *buf)
377 {
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;
382 
383 	mutex_lock(&md->list_mutex);
384 
385 	list_for_each_safe(ent, n, &md->slist) {
386 		sl = list_entry(ent, struct w1_slave, w1_slave_entry);
387 
388 		c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
389 	}
390 	if (!sl)
391 		c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
392 
393 	mutex_unlock(&md->list_mutex);
394 
395 	return PAGE_SIZE - c;
396 }
397 
398 static ssize_t w1_master_attribute_show_add(struct device *dev,
399 	struct device_attribute *attr, char *buf)
400 {
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;
405 }
406 
407 static int w1_atoreg_num(struct device *dev, const char *buf, size_t count,
408 	struct w1_reg_num *rn)
409 {
410 	unsigned int family;
411 	unsigned long long id;
412 	int i;
413 	u64 rn64_le;
414 
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.
419 	 */
420 	const char *error_msg = "bad slave string format, expecting "
421 		"ff-dddddddddddd\n";
422 
423 	if (buf[2] != '-') {
424 		dev_err(dev, "%s", error_msg);
425 		return -EINVAL;
426 	}
427 	i = sscanf(buf, "%02x-%012llx", &family, &id);
428 	if (i != 2) {
429 		dev_err(dev, "%s", error_msg);
430 		return -EINVAL;
431 	}
432 	rn->family = family;
433 	rn->id = id;
434 
435 	rn64_le = cpu_to_le64(*(u64 *)rn);
436 	rn->crc = w1_calc_crc8((u8 *)&rn64_le, 7);
437 
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
442 
443 	return 0;
444 }
445 
446 /* Searches the slaves in the w1_master and returns a pointer or NULL.
447  * Note: must not hold list_mutex
448  */
449 struct w1_slave *w1_slave_search_device(struct w1_master *dev,
450 	struct w1_reg_num *rn)
451 {
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;
460 		}
461 	}
462 	mutex_unlock(&dev->list_mutex);
463 	return NULL;
464 }
465 
466 static ssize_t w1_master_attribute_store_add(struct device *dev,
467 						struct device_attribute *attr,
468 						const char *buf, size_t count)
469 {
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;
474 
475 	if (w1_atoreg_num(dev, buf, count, &rn))
476 		return -EINVAL;
477 
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.
483 	 */
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);
489 	}
490 	mutex_unlock(&md->mutex);
491 
492 	return result;
493 }
494 
495 static ssize_t w1_master_attribute_show_remove(struct device *dev,
496 	struct device_attribute *attr, char *buf)
497 {
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;
502 }
503 
504 static ssize_t w1_master_attribute_store_remove(struct device *dev,
505 						struct device_attribute *attr,
506 						const char *buf, size_t count)
507 {
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;
512 
513 	if (w1_atoreg_num(dev, buf, count, &rn))
514 		return -EINVAL;
515 
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;
527 	}
528 	mutex_unlock(&md->mutex);
529 
530 	return result;
531 }
532 
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)
537 
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)
543 
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);
556 
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
571 };
572 
573 static struct attribute_group w1_master_defattr_group = {
574 	.attrs = w1_master_default_attrs,
575 };
576 
577 int w1_create_master_attributes(struct w1_master *master)
578 {
579 	return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
580 }
581 
582 void w1_destroy_master_attributes(struct w1_master *master)
583 {
584 	sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
585 }
586 
587 static int w1_uevent(struct device *dev, struct kobj_uevent_env *env)
588 {
589 	struct w1_master *md = NULL;
590 	struct w1_slave *sl = NULL;
591 	char *event_owner, *name;
592 	int err = 0;
593 
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;
605 	}
606 
607 	dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n",
608 			event_owner, name, dev_name(dev));
609 
610 	if (dev->driver != &w1_slave_driver || !sl)
611 		goto end;
612 
613 	err = add_uevent_var(env, "W1_FID=%02X", sl->reg_num.family);
614 	if (err)
615 		goto end;
616 
617 	err = add_uevent_var(env, "W1_SLAVE_ID=%024LX",
618 			     (unsigned long long)sl->reg_num.id);
619 end:
620 	return err;
621 }
622 
623 static int w1_family_notify(unsigned long action, struct w1_slave *sl)
624 {
625 	struct w1_family_ops *fops;
626 	int err;
627 
628 	fops = sl->family->fops;
629 
630 	if (!fops)
631 		return 0;
632 
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;
643 			}
644 		}
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;
652 			}
653 		}
654 
655 		break;
656 	case BUS_NOTIFY_DEL_DEVICE:
657 		if (fops->remove_slave)
658 			sl->family->fops->remove_slave(sl);
659 		if (fops->groups)
660 			sysfs_remove_groups(&sl->dev.kobj, fops->groups);
661 		break;
662 	}
663 	return 0;
664 }
665 
666 static int __w1_attach_slave_device(struct w1_slave *sl)
667 {
668 	int err;
669 
670 	sl->dev.parent = &sl->master->dev;
671 	sl->dev.driver = &w1_slave_driver;
672 	sl->dev.bus = &w1_bus_type;
673 	sl->dev.release = &w1_slave_release;
674 	sl->dev.groups = w1_slave_groups;
675 
676 	dev_set_name(&sl->dev, "%02x-%012llx",
677 		 (unsigned int) sl->reg_num.family,
678 		 (unsigned long long) sl->reg_num.id);
679 	snprintf(&sl->name[0], sizeof(sl->name),
680 		 "%02x-%012llx",
681 		 (unsigned int) sl->reg_num.family,
682 		 (unsigned long long) sl->reg_num.id);
683 
684 	dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__,
685 		dev_name(&sl->dev), sl);
686 
687 	/* suppress for w1_family_notify before sending KOBJ_ADD */
688 	dev_set_uevent_suppress(&sl->dev, true);
689 
690 	err = device_register(&sl->dev);
691 	if (err < 0) {
692 		dev_err(&sl->dev,
693 			"Device registration [%s] failed. err=%d\n",
694 			dev_name(&sl->dev), err);
695 		return err;
696 	}
697 	w1_family_notify(BUS_NOTIFY_ADD_DEVICE, sl);
698 
699 	dev_set_uevent_suppress(&sl->dev, false);
700 	kobject_uevent(&sl->dev.kobj, KOBJ_ADD);
701 
702 	mutex_lock(&sl->master->list_mutex);
703 	list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
704 	mutex_unlock(&sl->master->list_mutex);
705 
706 	return 0;
707 }
708 
709 int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
710 {
711 	struct w1_slave *sl;
712 	struct w1_family *f;
713 	int err;
714 	struct w1_netlink_msg msg;
715 
716 	sl = kzalloc(sizeof(struct w1_slave), GFP_KERNEL);
717 	if (!sl) {
718 		dev_err(&dev->dev,
719 			 "%s: failed to allocate new slave device.\n",
720 			 __func__);
721 		return -ENOMEM;
722 	}
723 
724 
725 	sl->owner = THIS_MODULE;
726 	sl->master = dev;
727 	set_bit(W1_SLAVE_ACTIVE, &sl->flags);
728 
729 	memset(&msg, 0, sizeof(msg));
730 	memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
731 	atomic_set(&sl->refcnt, 1);
732 	atomic_inc(&sl->master->refcnt);
733 
734 	/* slave modules need to be loaded in a context with unlocked mutex */
735 	mutex_unlock(&dev->mutex);
736 	request_module("w1-family-0x%02x", rn->family);
737 	mutex_lock(&dev->mutex);
738 
739 	spin_lock(&w1_flock);
740 	f = w1_family_registered(rn->family);
741 	if (!f) {
742 		f= &w1_default_family;
743 		dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
744 			  rn->family, rn->family,
745 			  (unsigned long long)rn->id, rn->crc);
746 	}
747 	__w1_family_get(f);
748 	spin_unlock(&w1_flock);
749 
750 	sl->family = f;
751 
752 
753 	err = __w1_attach_slave_device(sl);
754 	if (err < 0) {
755 		dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
756 			 sl->name);
757 		w1_family_put(sl->family);
758 		atomic_dec(&sl->master->refcnt);
759 		kfree(sl);
760 		return err;
761 	}
762 
763 	sl->ttl = dev->slave_ttl;
764 	dev->slave_count++;
765 
766 	memcpy(msg.id.id, rn, sizeof(msg.id));
767 	msg.type = W1_SLAVE_ADD;
768 	w1_netlink_send(dev, &msg);
769 
770 	return 0;
771 }
772 
773 int w1_unref_slave(struct w1_slave *sl)
774 {
775 	struct w1_master *dev = sl->master;
776 	int refcnt;
777 	mutex_lock(&dev->list_mutex);
778 	refcnt = atomic_sub_return(1, &sl->refcnt);
779 	if (refcnt == 0) {
780 		struct w1_netlink_msg msg;
781 
782 		dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__,
783 			sl->name, sl);
784 
785 		list_del(&sl->w1_slave_entry);
786 
787 		memset(&msg, 0, sizeof(msg));
788 		memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
789 		msg.type = W1_SLAVE_REMOVE;
790 		w1_netlink_send(sl->master, &msg);
791 
792 		w1_family_notify(BUS_NOTIFY_DEL_DEVICE, sl);
793 		device_unregister(&sl->dev);
794 		#ifdef DEBUG
795 		memset(sl, 0, sizeof(*sl));
796 		#endif
797 		kfree(sl);
798 	}
799 	atomic_dec(&dev->refcnt);
800 	mutex_unlock(&dev->list_mutex);
801 	return refcnt;
802 }
803 
804 int w1_slave_detach(struct w1_slave *sl)
805 {
806 	/* Only detach a slave once as it decreases the refcnt each time. */
807 	int destroy_now;
808 	mutex_lock(&sl->master->list_mutex);
809 	destroy_now = !test_bit(W1_SLAVE_DETACH, &sl->flags);
810 	set_bit(W1_SLAVE_DETACH, &sl->flags);
811 	mutex_unlock(&sl->master->list_mutex);
812 
813 	if (destroy_now)
814 		destroy_now = !w1_unref_slave(sl);
815 	return destroy_now ? 0 : -EBUSY;
816 }
817 
818 struct w1_master *w1_search_master_id(u32 id)
819 {
820 	struct w1_master *dev;
821 	int found = 0;
822 
823 	mutex_lock(&w1_mlock);
824 	list_for_each_entry(dev, &w1_masters, w1_master_entry) {
825 		if (dev->id == id) {
826 			found = 1;
827 			atomic_inc(&dev->refcnt);
828 			break;
829 		}
830 	}
831 	mutex_unlock(&w1_mlock);
832 
833 	return (found)?dev:NULL;
834 }
835 
836 struct w1_slave *w1_search_slave(struct w1_reg_num *id)
837 {
838 	struct w1_master *dev;
839 	struct w1_slave *sl = NULL;
840 	int found = 0;
841 
842 	mutex_lock(&w1_mlock);
843 	list_for_each_entry(dev, &w1_masters, w1_master_entry) {
844 		mutex_lock(&dev->list_mutex);
845 		list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
846 			if (sl->reg_num.family == id->family &&
847 					sl->reg_num.id == id->id &&
848 					sl->reg_num.crc == id->crc) {
849 				found = 1;
850 				atomic_inc(&dev->refcnt);
851 				atomic_inc(&sl->refcnt);
852 				break;
853 			}
854 		}
855 		mutex_unlock(&dev->list_mutex);
856 
857 		if (found)
858 			break;
859 	}
860 	mutex_unlock(&w1_mlock);
861 
862 	return (found)?sl:NULL;
863 }
864 
865 void w1_reconnect_slaves(struct w1_family *f, int attach)
866 {
867 	struct w1_slave *sl, *sln;
868 	struct w1_master *dev;
869 
870 	mutex_lock(&w1_mlock);
871 	list_for_each_entry(dev, &w1_masters, w1_master_entry) {
872 		dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
873 			"for family %02x.\n", dev->name, f->fid);
874 		mutex_lock(&dev->mutex);
875 		mutex_lock(&dev->list_mutex);
876 		list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
877 			/* If it is a new family, slaves with the default
878 			 * family driver and are that family will be
879 			 * connected.  If the family is going away, devices
880 			 * matching that family are reconneced.
881 			 */
882 			if ((attach && sl->family->fid == W1_FAMILY_DEFAULT
883 				&& sl->reg_num.family == f->fid) ||
884 				(!attach && sl->family->fid == f->fid)) {
885 				struct w1_reg_num rn;
886 
887 				mutex_unlock(&dev->list_mutex);
888 				memcpy(&rn, &sl->reg_num, sizeof(rn));
889 				/* If it was already in use let the automatic
890 				 * scan pick it up again later.
891 				 */
892 				if (!w1_slave_detach(sl))
893 					w1_attach_slave_device(dev, &rn);
894 				mutex_lock(&dev->list_mutex);
895 			}
896 		}
897 		dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
898 			"has been finished.\n", dev->name);
899 		mutex_unlock(&dev->list_mutex);
900 		mutex_unlock(&dev->mutex);
901 	}
902 	mutex_unlock(&w1_mlock);
903 }
904 
905 void w1_slave_found(struct w1_master *dev, u64 rn)
906 {
907 	struct w1_slave *sl;
908 	struct w1_reg_num *tmp;
909 	u64 rn_le = cpu_to_le64(rn);
910 
911 	atomic_inc(&dev->refcnt);
912 
913 	tmp = (struct w1_reg_num *) &rn;
914 
915 	sl = w1_slave_search_device(dev, tmp);
916 	if (sl) {
917 		set_bit(W1_SLAVE_ACTIVE, &sl->flags);
918 	} else {
919 		if (rn && tmp->crc == w1_calc_crc8((u8 *)&rn_le, 7))
920 			w1_attach_slave_device(dev, tmp);
921 	}
922 
923 	atomic_dec(&dev->refcnt);
924 }
925 
926 /**
927  * w1_search() - Performs a ROM Search & registers any devices found.
928  * @dev: The master device to search
929  * @search_type: W1_SEARCH to search all devices, or W1_ALARM_SEARCH
930  * to return only devices in the alarmed state
931  * @cb: Function to call when a device is found
932  *
933  * The 1-wire search is a simple binary tree search.
934  * For each bit of the address, we read two bits and write one bit.
935  * The bit written will put to sleep all devies that don't match that bit.
936  * When the two reads differ, the direction choice is obvious.
937  * When both bits are 0, we must choose a path to take.
938  * When we can scan all 64 bits without having to choose a path, we are done.
939  *
940  * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
941  *
942  */
943 void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
944 {
945 	u64 last_rn, rn, tmp64;
946 	int i, slave_count = 0;
947 	int last_zero, last_device;
948 	int search_bit, desc_bit;
949 	u8  triplet_ret = 0;
950 
951 	search_bit = 0;
952 	rn = dev->search_id;
953 	last_rn = 0;
954 	last_device = 0;
955 	last_zero = -1;
956 
957 	desc_bit = 64;
958 
959 	while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
960 		last_rn = rn;
961 		rn = 0;
962 
963 		/*
964 		 * Reset bus and all 1-wire device state machines
965 		 * so they can respond to our requests.
966 		 *
967 		 * Return 0 - device(s) present, 1 - no devices present.
968 		 */
969 		mutex_lock(&dev->bus_mutex);
970 		if (w1_reset_bus(dev)) {
971 			mutex_unlock(&dev->bus_mutex);
972 			dev_dbg(&dev->dev, "No devices present on the wire.\n");
973 			break;
974 		}
975 
976 		/* Do fast search on single slave bus */
977 		if (dev->max_slave_count == 1) {
978 			int rv;
979 			w1_write_8(dev, W1_READ_ROM);
980 			rv = w1_read_block(dev, (u8 *)&rn, 8);
981 			mutex_unlock(&dev->bus_mutex);
982 
983 			if (rv == 8 && rn)
984 				cb(dev, rn);
985 
986 			break;
987 		}
988 
989 		/* Start the search */
990 		w1_write_8(dev, search_type);
991 		for (i = 0; i < 64; ++i) {
992 			/* Determine the direction/search bit */
993 			if (i == desc_bit)
994 				search_bit = 1;	  /* took the 0 path last time, so take the 1 path */
995 			else if (i > desc_bit)
996 				search_bit = 0;	  /* take the 0 path on the next branch */
997 			else
998 				search_bit = ((last_rn >> i) & 0x1);
999 
1000 			/* Read two bits and write one bit */
1001 			triplet_ret = w1_triplet(dev, search_bit);
1002 
1003 			/* quit if no device responded */
1004 			if ( (triplet_ret & 0x03) == 0x03 )
1005 				break;
1006 
1007 			/* If both directions were valid, and we took the 0 path... */
1008 			if (triplet_ret == 0)
1009 				last_zero = i;
1010 
1011 			/* extract the direction taken & update the device number */
1012 			tmp64 = (triplet_ret >> 2);
1013 			rn |= (tmp64 << i);
1014 
1015 			if (test_bit(W1_ABORT_SEARCH, &dev->flags)) {
1016 				mutex_unlock(&dev->bus_mutex);
1017 				dev_dbg(&dev->dev, "Abort w1_search\n");
1018 				return;
1019 			}
1020 		}
1021 		mutex_unlock(&dev->bus_mutex);
1022 
1023 		if ( (triplet_ret & 0x03) != 0x03 ) {
1024 			if ((desc_bit == last_zero) || (last_zero < 0)) {
1025 				last_device = 1;
1026 				dev->search_id = 0;
1027 			} else {
1028 				dev->search_id = rn;
1029 			}
1030 			desc_bit = last_zero;
1031 			cb(dev, rn);
1032 		}
1033 
1034 		if (!last_device && slave_count == dev->max_slave_count &&
1035 			!test_bit(W1_WARN_MAX_COUNT, &dev->flags)) {
1036 			/* Only max_slave_count will be scanned in a search,
1037 			 * but it will start where it left off next search
1038 			 * until all ids are identified and then it will start
1039 			 * over.  A continued search will report the previous
1040 			 * last id as the first id (provided it is still on the
1041 			 * bus).
1042 			 */
1043 			dev_info(&dev->dev, "%s: max_slave_count %d reached, "
1044 				"will continue next search.\n", __func__,
1045 				dev->max_slave_count);
1046 			set_bit(W1_WARN_MAX_COUNT, &dev->flags);
1047 		}
1048 	}
1049 }
1050 
1051 void w1_search_process_cb(struct w1_master *dev, u8 search_type,
1052 	w1_slave_found_callback cb)
1053 {
1054 	struct w1_slave *sl, *sln;
1055 
1056 	mutex_lock(&dev->list_mutex);
1057 	list_for_each_entry(sl, &dev->slist, w1_slave_entry)
1058 		clear_bit(W1_SLAVE_ACTIVE, &sl->flags);
1059 	mutex_unlock(&dev->list_mutex);
1060 
1061 	w1_search_devices(dev, search_type, cb);
1062 
1063 	mutex_lock(&dev->list_mutex);
1064 	list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
1065 		if (!test_bit(W1_SLAVE_ACTIVE, &sl->flags) && !--sl->ttl) {
1066 			mutex_unlock(&dev->list_mutex);
1067 			w1_slave_detach(sl);
1068 			mutex_lock(&dev->list_mutex);
1069 		}
1070 		else if (test_bit(W1_SLAVE_ACTIVE, &sl->flags))
1071 			sl->ttl = dev->slave_ttl;
1072 	}
1073 	mutex_unlock(&dev->list_mutex);
1074 
1075 	if (dev->search_count > 0)
1076 		dev->search_count--;
1077 }
1078 
1079 static void w1_search_process(struct w1_master *dev, u8 search_type)
1080 {
1081 	w1_search_process_cb(dev, search_type, w1_slave_found);
1082 }
1083 
1084 /**
1085  * w1_process_callbacks() - execute each dev->async_list callback entry
1086  * @dev: w1_master device
1087  *
1088  * The w1 master list_mutex must be held.
1089  *
1090  * Return: 1 if there were commands to executed 0 otherwise
1091  */
1092 int w1_process_callbacks(struct w1_master *dev)
1093 {
1094 	int ret = 0;
1095 	struct w1_async_cmd *async_cmd, *async_n;
1096 
1097 	/* The list can be added to in another thread, loop until it is empty */
1098 	while (!list_empty(&dev->async_list)) {
1099 		list_for_each_entry_safe(async_cmd, async_n, &dev->async_list,
1100 			async_entry) {
1101 			/* drop the lock, if it is a search it can take a long
1102 			 * time */
1103 			mutex_unlock(&dev->list_mutex);
1104 			async_cmd->cb(dev, async_cmd);
1105 			ret = 1;
1106 			mutex_lock(&dev->list_mutex);
1107 		}
1108 	}
1109 	return ret;
1110 }
1111 
1112 int w1_process(void *data)
1113 {
1114 	struct w1_master *dev = (struct w1_master *) data;
1115 	/* As long as w1_timeout is only set by a module parameter the sleep
1116 	 * time can be calculated in jiffies once.
1117 	 */
1118 	const unsigned long jtime =
1119 	  usecs_to_jiffies(w1_timeout * 1000000 + w1_timeout_us);
1120 	/* remainder if it woke up early */
1121 	unsigned long jremain = 0;
1122 
1123 	for (;;) {
1124 
1125 		if (!jremain && dev->search_count) {
1126 			mutex_lock(&dev->mutex);
1127 			w1_search_process(dev, W1_SEARCH);
1128 			mutex_unlock(&dev->mutex);
1129 		}
1130 
1131 		mutex_lock(&dev->list_mutex);
1132 		/* Note, w1_process_callback drops the lock while processing,
1133 		 * but locks it again before returning.
1134 		 */
1135 		if (!w1_process_callbacks(dev) && jremain) {
1136 			/* a wake up is either to stop the thread, process
1137 			 * callbacks, or search, it isn't process callbacks, so
1138 			 * schedule a search.
1139 			 */
1140 			jremain = 1;
1141 		}
1142 
1143 		__set_current_state(TASK_INTERRUPTIBLE);
1144 
1145 		/* hold list_mutex until after interruptible to prevent loosing
1146 		 * the wakeup signal when async_cmd is added.
1147 		 */
1148 		mutex_unlock(&dev->list_mutex);
1149 
1150 		if (kthread_should_stop())
1151 			break;
1152 
1153 		/* Only sleep when the search is active. */
1154 		if (dev->search_count) {
1155 			if (!jremain)
1156 				jremain = jtime;
1157 			jremain = schedule_timeout(jremain);
1158 		}
1159 		else
1160 			schedule();
1161 	}
1162 
1163 	atomic_dec(&dev->refcnt);
1164 
1165 	return 0;
1166 }
1167 
1168 static int __init w1_init(void)
1169 {
1170 	int retval;
1171 
1172 	pr_info("Driver for 1-wire Dallas network protocol.\n");
1173 
1174 	w1_init_netlink();
1175 
1176 	retval = bus_register(&w1_bus_type);
1177 	if (retval) {
1178 		pr_err("Failed to register bus. err=%d.\n", retval);
1179 		goto err_out_exit_init;
1180 	}
1181 
1182 	retval = driver_register(&w1_master_driver);
1183 	if (retval) {
1184 		pr_err("Failed to register master driver. err=%d.\n",
1185 			retval);
1186 		goto err_out_bus_unregister;
1187 	}
1188 
1189 	retval = driver_register(&w1_slave_driver);
1190 	if (retval) {
1191 		pr_err("Failed to register slave driver. err=%d.\n",
1192 			retval);
1193 		goto err_out_master_unregister;
1194 	}
1195 
1196 	return 0;
1197 
1198 #if 0
1199 /* For undoing the slave register if there was a step after it. */
1200 err_out_slave_unregister:
1201 	driver_unregister(&w1_slave_driver);
1202 #endif
1203 
1204 err_out_master_unregister:
1205 	driver_unregister(&w1_master_driver);
1206 
1207 err_out_bus_unregister:
1208 	bus_unregister(&w1_bus_type);
1209 
1210 err_out_exit_init:
1211 	return retval;
1212 }
1213 
1214 static void __exit w1_fini(void)
1215 {
1216 	struct w1_master *dev;
1217 
1218 	/* Set netlink removal messages and some cleanup */
1219 	list_for_each_entry(dev, &w1_masters, w1_master_entry)
1220 		__w1_remove_master_device(dev);
1221 
1222 	w1_fini_netlink();
1223 
1224 	driver_unregister(&w1_slave_driver);
1225 	driver_unregister(&w1_master_driver);
1226 	bus_unregister(&w1_bus_type);
1227 }
1228 
1229 module_init(w1_init);
1230 module_exit(w1_fini);
1231