xref: /linux/drivers/w1/w1.c (revision 4d7696f1b05f4aeb586c74868fe3da2731daca4b)
1 /*
2  *	w1.c
3  *
4  * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
5  *
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21 
22 #include <linux/delay.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/list.h>
27 #include <linux/interrupt.h>
28 #include <linux/spinlock.h>
29 #include <linux/timer.h>
30 #include <linux/device.h>
31 #include <linux/slab.h>
32 #include <linux/sched.h>
33 #include <linux/kthread.h>
34 #include <linux/freezer.h>
35 
36 #include <linux/atomic.h>
37 
38 #include "w1.h"
39 #include "w1_log.h"
40 #include "w1_int.h"
41 #include "w1_family.h"
42 #include "w1_netlink.h"
43 
44 MODULE_LICENSE("GPL");
45 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
46 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
47 
48 static int w1_timeout = 10;
49 int w1_max_slave_count = 10;
50 int w1_max_slave_ttl = 10;
51 
52 module_param_named(timeout, w1_timeout, int, 0);
53 module_param_named(max_slave_count, w1_max_slave_count, int, 0);
54 module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
55 
56 DEFINE_MUTEX(w1_mlock);
57 LIST_HEAD(w1_masters);
58 
59 static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn);
60 
61 static int w1_master_match(struct device *dev, struct device_driver *drv)
62 {
63 	return 1;
64 }
65 
66 static int w1_master_probe(struct device *dev)
67 {
68 	return -ENODEV;
69 }
70 
71 static void w1_master_release(struct device *dev)
72 {
73 	struct w1_master *md = dev_to_w1_master(dev);
74 
75 	dev_dbg(dev, "%s: Releasing %s.\n", __func__, md->name);
76 	memset(md, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
77 	kfree(md);
78 }
79 
80 static void w1_slave_release(struct device *dev)
81 {
82 	struct w1_slave *sl = dev_to_w1_slave(dev);
83 
84 	dev_dbg(dev, "%s: Releasing %s.\n", __func__, sl->name);
85 
86 	while (atomic_read(&sl->refcnt)) {
87 		dev_dbg(dev, "Waiting for %s to become free: refcnt=%d.\n",
88 				sl->name, atomic_read(&sl->refcnt));
89 		if (msleep_interruptible(1000))
90 			flush_signals(current);
91 	}
92 
93 	w1_family_put(sl->family);
94 	sl->master->slave_count--;
95 
96 	complete(&sl->released);
97 }
98 
99 static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf)
100 {
101 	struct w1_slave *sl = dev_to_w1_slave(dev);
102 
103 	return sprintf(buf, "%s\n", sl->name);
104 }
105 static DEVICE_ATTR_RO(name);
106 
107 static ssize_t id_show(struct device *dev,
108 	struct device_attribute *attr, char *buf)
109 {
110 	struct w1_slave *sl = dev_to_w1_slave(dev);
111 	ssize_t count = sizeof(sl->reg_num);
112 
113 	memcpy(buf, (u8 *)&sl->reg_num, count);
114 	return count;
115 }
116 static DEVICE_ATTR_RO(id);
117 
118 static struct attribute *w1_slave_attrs[] = {
119 	&dev_attr_name.attr,
120 	&dev_attr_id.attr,
121 	NULL,
122 };
123 ATTRIBUTE_GROUPS(w1_slave);
124 
125 /* Default family */
126 
127 static ssize_t rw_write(struct file *filp, struct kobject *kobj,
128 			struct bin_attribute *bin_attr, char *buf, loff_t off,
129 			size_t count)
130 {
131 	struct w1_slave *sl = kobj_to_w1_slave(kobj);
132 
133 	mutex_lock(&sl->master->mutex);
134 	if (w1_reset_select_slave(sl)) {
135 		count = 0;
136 		goto out_up;
137 	}
138 
139 	w1_write_block(sl->master, buf, count);
140 
141 out_up:
142 	mutex_unlock(&sl->master->mutex);
143 	return count;
144 }
145 
146 static ssize_t rw_read(struct file *filp, struct kobject *kobj,
147 		       struct bin_attribute *bin_attr, char *buf, loff_t off,
148 		       size_t count)
149 {
150 	struct w1_slave *sl = kobj_to_w1_slave(kobj);
151 
152 	mutex_lock(&sl->master->mutex);
153 	w1_read_block(sl->master, buf, count);
154 	mutex_unlock(&sl->master->mutex);
155 	return count;
156 }
157 
158 static BIN_ATTR_RW(rw, PAGE_SIZE);
159 
160 static struct bin_attribute *w1_slave_bin_attrs[] = {
161 	&bin_attr_rw,
162 	NULL,
163 };
164 
165 static const struct attribute_group w1_slave_default_group = {
166 	.bin_attrs = w1_slave_bin_attrs,
167 };
168 
169 static const struct attribute_group *w1_slave_default_groups[] = {
170 	&w1_slave_default_group,
171 	NULL,
172 };
173 
174 static struct w1_family_ops w1_default_fops = {
175 	.groups		= w1_slave_default_groups,
176 };
177 
178 static struct w1_family w1_default_family = {
179 	.fops = &w1_default_fops,
180 };
181 
182 static int w1_uevent(struct device *dev, struct kobj_uevent_env *env);
183 
184 static struct bus_type w1_bus_type = {
185 	.name = "w1",
186 	.match = w1_master_match,
187 	.uevent = w1_uevent,
188 };
189 
190 struct device_driver w1_master_driver = {
191 	.name = "w1_master_driver",
192 	.bus = &w1_bus_type,
193 	.probe = w1_master_probe,
194 };
195 
196 struct device w1_master_device = {
197 	.parent = NULL,
198 	.bus = &w1_bus_type,
199 	.init_name = "w1 bus master",
200 	.driver = &w1_master_driver,
201 	.release = &w1_master_release
202 };
203 
204 static struct device_driver w1_slave_driver = {
205 	.name = "w1_slave_driver",
206 	.bus = &w1_bus_type,
207 };
208 
209 #if 0
210 struct device w1_slave_device = {
211 	.parent = NULL,
212 	.bus = &w1_bus_type,
213 	.init_name = "w1 bus slave",
214 	.driver = &w1_slave_driver,
215 	.release = &w1_slave_release
216 };
217 #endif  /*  0  */
218 
219 static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
220 {
221 	struct w1_master *md = dev_to_w1_master(dev);
222 	ssize_t count;
223 
224 	mutex_lock(&md->mutex);
225 	count = sprintf(buf, "%s\n", md->name);
226 	mutex_unlock(&md->mutex);
227 
228 	return count;
229 }
230 
231 static ssize_t w1_master_attribute_store_search(struct device * dev,
232 						struct device_attribute *attr,
233 						const char * buf, size_t count)
234 {
235 	long tmp;
236 	struct w1_master *md = dev_to_w1_master(dev);
237 
238 	if (strict_strtol(buf, 0, &tmp) == -EINVAL)
239 		return -EINVAL;
240 
241 	mutex_lock(&md->mutex);
242 	md->search_count = tmp;
243 	mutex_unlock(&md->mutex);
244 	wake_up_process(md->thread);
245 
246 	return count;
247 }
248 
249 static ssize_t w1_master_attribute_show_search(struct device *dev,
250 					       struct device_attribute *attr,
251 					       char *buf)
252 {
253 	struct w1_master *md = dev_to_w1_master(dev);
254 	ssize_t count;
255 
256 	mutex_lock(&md->mutex);
257 	count = sprintf(buf, "%d\n", md->search_count);
258 	mutex_unlock(&md->mutex);
259 
260 	return count;
261 }
262 
263 static ssize_t w1_master_attribute_store_pullup(struct device *dev,
264 						struct device_attribute *attr,
265 						const char *buf, size_t count)
266 {
267 	long tmp;
268 	struct w1_master *md = dev_to_w1_master(dev);
269 
270 	if (strict_strtol(buf, 0, &tmp) == -EINVAL)
271 		return -EINVAL;
272 
273 	mutex_lock(&md->mutex);
274 	md->enable_pullup = tmp;
275 	mutex_unlock(&md->mutex);
276 	wake_up_process(md->thread);
277 
278 	return count;
279 }
280 
281 static ssize_t w1_master_attribute_show_pullup(struct device *dev,
282 					       struct device_attribute *attr,
283 					       char *buf)
284 {
285 	struct w1_master *md = dev_to_w1_master(dev);
286 	ssize_t count;
287 
288 	mutex_lock(&md->mutex);
289 	count = sprintf(buf, "%d\n", md->enable_pullup);
290 	mutex_unlock(&md->mutex);
291 
292 	return count;
293 }
294 
295 static ssize_t w1_master_attribute_show_pointer(struct device *dev, struct device_attribute *attr, char *buf)
296 {
297 	struct w1_master *md = dev_to_w1_master(dev);
298 	ssize_t count;
299 
300 	mutex_lock(&md->mutex);
301 	count = sprintf(buf, "0x%p\n", md->bus_master);
302 	mutex_unlock(&md->mutex);
303 	return count;
304 }
305 
306 static ssize_t w1_master_attribute_show_timeout(struct device *dev, struct device_attribute *attr, char *buf)
307 {
308 	ssize_t count;
309 	count = sprintf(buf, "%d\n", w1_timeout);
310 	return count;
311 }
312 
313 static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
314 {
315 	struct w1_master *md = dev_to_w1_master(dev);
316 	ssize_t count;
317 
318 	mutex_lock(&md->mutex);
319 	count = sprintf(buf, "%d\n", md->max_slave_count);
320 	mutex_unlock(&md->mutex);
321 	return count;
322 }
323 
324 static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf)
325 {
326 	struct w1_master *md = dev_to_w1_master(dev);
327 	ssize_t count;
328 
329 	mutex_lock(&md->mutex);
330 	count = sprintf(buf, "%lu\n", md->attempts);
331 	mutex_unlock(&md->mutex);
332 	return count;
333 }
334 
335 static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
336 {
337 	struct w1_master *md = dev_to_w1_master(dev);
338 	ssize_t count;
339 
340 	mutex_lock(&md->mutex);
341 	count = sprintf(buf, "%d\n", md->slave_count);
342 	mutex_unlock(&md->mutex);
343 	return count;
344 }
345 
346 static ssize_t w1_master_attribute_show_slaves(struct device *dev,
347 	struct device_attribute *attr, char *buf)
348 {
349 	struct w1_master *md = dev_to_w1_master(dev);
350 	int c = PAGE_SIZE;
351 
352 	mutex_lock(&md->mutex);
353 
354 	if (md->slave_count == 0)
355 		c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
356 	else {
357 		struct list_head *ent, *n;
358 		struct w1_slave *sl;
359 
360 		list_for_each_safe(ent, n, &md->slist) {
361 			sl = list_entry(ent, struct w1_slave, w1_slave_entry);
362 
363 			c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
364 		}
365 	}
366 
367 	mutex_unlock(&md->mutex);
368 
369 	return PAGE_SIZE - c;
370 }
371 
372 static ssize_t w1_master_attribute_show_add(struct device *dev,
373 	struct device_attribute *attr, char *buf)
374 {
375 	int c = PAGE_SIZE;
376 	c -= snprintf(buf+PAGE_SIZE - c, c,
377 		"write device id xx-xxxxxxxxxxxx to add slave\n");
378 	return PAGE_SIZE - c;
379 }
380 
381 static int w1_atoreg_num(struct device *dev, const char *buf, size_t count,
382 	struct w1_reg_num *rn)
383 {
384 	unsigned int family;
385 	unsigned long long id;
386 	int i;
387 	u64 rn64_le;
388 
389 	/* The CRC value isn't read from the user because the sysfs directory
390 	 * doesn't include it and most messages from the bus search don't
391 	 * print it either.  It would be unreasonable for the user to then
392 	 * provide it.
393 	 */
394 	const char *error_msg = "bad slave string format, expecting "
395 		"ff-dddddddddddd\n";
396 
397 	if (buf[2] != '-') {
398 		dev_err(dev, "%s", error_msg);
399 		return -EINVAL;
400 	}
401 	i = sscanf(buf, "%02x-%012llx", &family, &id);
402 	if (i != 2) {
403 		dev_err(dev, "%s", error_msg);
404 		return -EINVAL;
405 	}
406 	rn->family = family;
407 	rn->id = id;
408 
409 	rn64_le = cpu_to_le64(*(u64 *)rn);
410 	rn->crc = w1_calc_crc8((u8 *)&rn64_le, 7);
411 
412 #if 0
413 	dev_info(dev, "With CRC device is %02x.%012llx.%02x.\n",
414 		  rn->family, (unsigned long long)rn->id, rn->crc);
415 #endif
416 
417 	return 0;
418 }
419 
420 /* Searches the slaves in the w1_master and returns a pointer or NULL.
421  * Note: must hold the mutex
422  */
423 static struct w1_slave *w1_slave_search_device(struct w1_master *dev,
424 	struct w1_reg_num *rn)
425 {
426 	struct w1_slave *sl;
427 	list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
428 		if (sl->reg_num.family == rn->family &&
429 				sl->reg_num.id == rn->id &&
430 				sl->reg_num.crc == rn->crc) {
431 			return sl;
432 		}
433 	}
434 	return NULL;
435 }
436 
437 static ssize_t w1_master_attribute_store_add(struct device *dev,
438 						struct device_attribute *attr,
439 						const char *buf, size_t count)
440 {
441 	struct w1_master *md = dev_to_w1_master(dev);
442 	struct w1_reg_num rn;
443 	struct w1_slave *sl;
444 	ssize_t result = count;
445 
446 	if (w1_atoreg_num(dev, buf, count, &rn))
447 		return -EINVAL;
448 
449 	mutex_lock(&md->mutex);
450 	sl = w1_slave_search_device(md, &rn);
451 	/* It would be nice to do a targeted search one the one-wire bus
452 	 * for the new device to see if it is out there or not.  But the
453 	 * current search doesn't support that.
454 	 */
455 	if (sl) {
456 		dev_info(dev, "Device %s already exists\n", sl->name);
457 		result = -EINVAL;
458 	} else {
459 		w1_attach_slave_device(md, &rn);
460 	}
461 	mutex_unlock(&md->mutex);
462 
463 	return result;
464 }
465 
466 static ssize_t w1_master_attribute_show_remove(struct device *dev,
467 	struct device_attribute *attr, char *buf)
468 {
469 	int c = PAGE_SIZE;
470 	c -= snprintf(buf+PAGE_SIZE - c, c,
471 		"write device id xx-xxxxxxxxxxxx to remove slave\n");
472 	return PAGE_SIZE - c;
473 }
474 
475 static ssize_t w1_master_attribute_store_remove(struct device *dev,
476 						struct device_attribute *attr,
477 						const char *buf, size_t count)
478 {
479 	struct w1_master *md = dev_to_w1_master(dev);
480 	struct w1_reg_num rn;
481 	struct w1_slave *sl;
482 	ssize_t result = count;
483 
484 	if (w1_atoreg_num(dev, buf, count, &rn))
485 		return -EINVAL;
486 
487 	mutex_lock(&md->mutex);
488 	sl = w1_slave_search_device(md, &rn);
489 	if (sl) {
490 		w1_slave_detach(sl);
491 	} else {
492 		dev_info(dev, "Device %02x-%012llx doesn't exists\n", rn.family,
493 			(unsigned long long)rn.id);
494 		result = -EINVAL;
495 	}
496 	mutex_unlock(&md->mutex);
497 
498 	return result;
499 }
500 
501 #define W1_MASTER_ATTR_RO(_name, _mode)				\
502 	struct device_attribute w1_master_attribute_##_name =	\
503 		__ATTR(w1_master_##_name, _mode,		\
504 		       w1_master_attribute_show_##_name, NULL)
505 
506 #define W1_MASTER_ATTR_RW(_name, _mode)				\
507 	struct device_attribute w1_master_attribute_##_name =	\
508 		__ATTR(w1_master_##_name, _mode,		\
509 		       w1_master_attribute_show_##_name,	\
510 		       w1_master_attribute_store_##_name)
511 
512 static W1_MASTER_ATTR_RO(name, S_IRUGO);
513 static W1_MASTER_ATTR_RO(slaves, S_IRUGO);
514 static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);
515 static W1_MASTER_ATTR_RO(max_slave_count, S_IRUGO);
516 static W1_MASTER_ATTR_RO(attempts, S_IRUGO);
517 static W1_MASTER_ATTR_RO(timeout, S_IRUGO);
518 static W1_MASTER_ATTR_RO(pointer, S_IRUGO);
519 static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUSR | S_IWGRP);
520 static W1_MASTER_ATTR_RW(pullup, S_IRUGO | S_IWUSR | S_IWGRP);
521 static W1_MASTER_ATTR_RW(add, S_IRUGO | S_IWUSR | S_IWGRP);
522 static W1_MASTER_ATTR_RW(remove, S_IRUGO | S_IWUSR | S_IWGRP);
523 
524 static struct attribute *w1_master_default_attrs[] = {
525 	&w1_master_attribute_name.attr,
526 	&w1_master_attribute_slaves.attr,
527 	&w1_master_attribute_slave_count.attr,
528 	&w1_master_attribute_max_slave_count.attr,
529 	&w1_master_attribute_attempts.attr,
530 	&w1_master_attribute_timeout.attr,
531 	&w1_master_attribute_pointer.attr,
532 	&w1_master_attribute_search.attr,
533 	&w1_master_attribute_pullup.attr,
534 	&w1_master_attribute_add.attr,
535 	&w1_master_attribute_remove.attr,
536 	NULL
537 };
538 
539 static struct attribute_group w1_master_defattr_group = {
540 	.attrs = w1_master_default_attrs,
541 };
542 
543 int w1_create_master_attributes(struct w1_master *master)
544 {
545 	return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
546 }
547 
548 void w1_destroy_master_attributes(struct w1_master *master)
549 {
550 	sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
551 }
552 
553 static int w1_uevent(struct device *dev, struct kobj_uevent_env *env)
554 {
555 	struct w1_master *md = NULL;
556 	struct w1_slave *sl = NULL;
557 	char *event_owner, *name;
558 	int err = 0;
559 
560 	if (dev->driver == &w1_master_driver) {
561 		md = container_of(dev, struct w1_master, dev);
562 		event_owner = "master";
563 		name = md->name;
564 	} else if (dev->driver == &w1_slave_driver) {
565 		sl = container_of(dev, struct w1_slave, dev);
566 		event_owner = "slave";
567 		name = sl->name;
568 	} else {
569 		dev_dbg(dev, "Unknown event.\n");
570 		return -EINVAL;
571 	}
572 
573 	dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n",
574 			event_owner, name, dev_name(dev));
575 
576 	if (dev->driver != &w1_slave_driver || !sl)
577 		goto end;
578 
579 	err = add_uevent_var(env, "W1_FID=%02X", sl->reg_num.family);
580 	if (err)
581 		goto end;
582 
583 	err = add_uevent_var(env, "W1_SLAVE_ID=%024LX",
584 			     (unsigned long long)sl->reg_num.id);
585 end:
586 	return err;
587 }
588 
589 /*
590  * Handle sysfs file creation and removal here, before userspace is told that
591  * the device is added / removed from the system
592  */
593 static int w1_bus_notify(struct notifier_block *nb, unsigned long action,
594 			 void *data)
595 {
596 	struct device *dev = data;
597 	struct w1_slave *sl;
598 	struct w1_family_ops *fops;
599 	int err;
600 
601 	/*
602 	 * Only care about slave devices at the moment.  Yes, we should use a
603 	 * separate "type" for this, but for now, look at the release function
604 	 * to know which type it is...
605 	 */
606 	if (dev->release != w1_slave_release)
607 		return 0;
608 
609 	sl = dev_to_w1_slave(dev);
610 	fops = sl->family->fops;
611 
612 	switch (action) {
613 	case BUS_NOTIFY_ADD_DEVICE:
614 		/* if the family driver needs to initialize something... */
615 		if (fops->add_slave) {
616 			err = fops->add_slave(sl);
617 			if (err < 0) {
618 				dev_err(&sl->dev,
619 					"add_slave() call failed. err=%d\n",
620 					err);
621 				return err;
622 			}
623 		}
624 		if (fops->groups) {
625 			err = sysfs_create_groups(&sl->dev.kobj, fops->groups);
626 			if (err) {
627 				dev_err(&sl->dev,
628 					"sysfs group creation failed. err=%d\n",
629 					err);
630 				return err;
631 			}
632 		}
633 
634 		break;
635 	case BUS_NOTIFY_DEL_DEVICE:
636 		if (fops->remove_slave)
637 			sl->family->fops->remove_slave(sl);
638 		if (fops->groups)
639 			sysfs_remove_groups(&sl->dev.kobj, fops->groups);
640 		break;
641 	}
642 	return 0;
643 }
644 
645 static struct notifier_block w1_bus_nb = {
646 	.notifier_call = w1_bus_notify,
647 };
648 
649 static int __w1_attach_slave_device(struct w1_slave *sl)
650 {
651 	int err;
652 
653 	sl->dev.parent = &sl->master->dev;
654 	sl->dev.driver = &w1_slave_driver;
655 	sl->dev.bus = &w1_bus_type;
656 	sl->dev.release = &w1_slave_release;
657 	sl->dev.groups = w1_slave_groups;
658 
659 	dev_set_name(&sl->dev, "%02x-%012llx",
660 		 (unsigned int) sl->reg_num.family,
661 		 (unsigned long long) sl->reg_num.id);
662 	snprintf(&sl->name[0], sizeof(sl->name),
663 		 "%02x-%012llx",
664 		 (unsigned int) sl->reg_num.family,
665 		 (unsigned long long) sl->reg_num.id);
666 
667 	dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__,
668 		dev_name(&sl->dev), sl);
669 
670 	err = device_register(&sl->dev);
671 	if (err < 0) {
672 		dev_err(&sl->dev,
673 			"Device registration [%s] failed. err=%d\n",
674 			dev_name(&sl->dev), err);
675 		return err;
676 	}
677 
678 
679 	dev_set_uevent_suppress(&sl->dev, false);
680 	kobject_uevent(&sl->dev.kobj, KOBJ_ADD);
681 
682 	list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
683 
684 	return 0;
685 }
686 
687 static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
688 {
689 	struct w1_slave *sl;
690 	struct w1_family *f;
691 	int err;
692 	struct w1_netlink_msg msg;
693 
694 	sl = kzalloc(sizeof(struct w1_slave), GFP_KERNEL);
695 	if (!sl) {
696 		dev_err(&dev->dev,
697 			 "%s: failed to allocate new slave device.\n",
698 			 __func__);
699 		return -ENOMEM;
700 	}
701 
702 
703 	sl->owner = THIS_MODULE;
704 	sl->master = dev;
705 	set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
706 
707 	memset(&msg, 0, sizeof(msg));
708 	memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
709 	atomic_set(&sl->refcnt, 0);
710 	init_completion(&sl->released);
711 
712 	request_module("w1-family-0x%0x", rn->family);
713 
714 	spin_lock(&w1_flock);
715 	f = w1_family_registered(rn->family);
716 	if (!f) {
717 		f= &w1_default_family;
718 		dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
719 			  rn->family, rn->family,
720 			  (unsigned long long)rn->id, rn->crc);
721 	}
722 	__w1_family_get(f);
723 	spin_unlock(&w1_flock);
724 
725 	sl->family = f;
726 
727 
728 	err = __w1_attach_slave_device(sl);
729 	if (err < 0) {
730 		dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
731 			 sl->name);
732 		w1_family_put(sl->family);
733 		kfree(sl);
734 		return err;
735 	}
736 
737 	sl->ttl = dev->slave_ttl;
738 	dev->slave_count++;
739 
740 	memcpy(msg.id.id, rn, sizeof(msg.id));
741 	msg.type = W1_SLAVE_ADD;
742 	w1_netlink_send(dev, &msg);
743 
744 	return 0;
745 }
746 
747 void w1_slave_detach(struct w1_slave *sl)
748 {
749 	struct w1_netlink_msg msg;
750 
751 	dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__, sl->name, sl);
752 
753 	list_del(&sl->w1_slave_entry);
754 
755 	memset(&msg, 0, sizeof(msg));
756 	memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
757 	msg.type = W1_SLAVE_REMOVE;
758 	w1_netlink_send(sl->master, &msg);
759 
760 	device_unregister(&sl->dev);
761 
762 	wait_for_completion(&sl->released);
763 	kfree(sl);
764 }
765 
766 struct w1_master *w1_search_master_id(u32 id)
767 {
768 	struct w1_master *dev;
769 	int found = 0;
770 
771 	mutex_lock(&w1_mlock);
772 	list_for_each_entry(dev, &w1_masters, w1_master_entry) {
773 		if (dev->id == id) {
774 			found = 1;
775 			atomic_inc(&dev->refcnt);
776 			break;
777 		}
778 	}
779 	mutex_unlock(&w1_mlock);
780 
781 	return (found)?dev:NULL;
782 }
783 
784 struct w1_slave *w1_search_slave(struct w1_reg_num *id)
785 {
786 	struct w1_master *dev;
787 	struct w1_slave *sl = NULL;
788 	int found = 0;
789 
790 	mutex_lock(&w1_mlock);
791 	list_for_each_entry(dev, &w1_masters, w1_master_entry) {
792 		mutex_lock(&dev->mutex);
793 		list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
794 			if (sl->reg_num.family == id->family &&
795 					sl->reg_num.id == id->id &&
796 					sl->reg_num.crc == id->crc) {
797 				found = 1;
798 				atomic_inc(&dev->refcnt);
799 				atomic_inc(&sl->refcnt);
800 				break;
801 			}
802 		}
803 		mutex_unlock(&dev->mutex);
804 
805 		if (found)
806 			break;
807 	}
808 	mutex_unlock(&w1_mlock);
809 
810 	return (found)?sl:NULL;
811 }
812 
813 void w1_reconnect_slaves(struct w1_family *f, int attach)
814 {
815 	struct w1_slave *sl, *sln;
816 	struct w1_master *dev;
817 
818 	mutex_lock(&w1_mlock);
819 	list_for_each_entry(dev, &w1_masters, w1_master_entry) {
820 		dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
821 			"for family %02x.\n", dev->name, f->fid);
822 		mutex_lock(&dev->mutex);
823 		list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
824 			/* If it is a new family, slaves with the default
825 			 * family driver and are that family will be
826 			 * connected.  If the family is going away, devices
827 			 * matching that family are reconneced.
828 			 */
829 			if ((attach && sl->family->fid == W1_FAMILY_DEFAULT
830 				&& sl->reg_num.family == f->fid) ||
831 				(!attach && sl->family->fid == f->fid)) {
832 				struct w1_reg_num rn;
833 
834 				memcpy(&rn, &sl->reg_num, sizeof(rn));
835 				w1_slave_detach(sl);
836 
837 				w1_attach_slave_device(dev, &rn);
838 			}
839 		}
840 		dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
841 			"has been finished.\n", dev->name);
842 		mutex_unlock(&dev->mutex);
843 	}
844 	mutex_unlock(&w1_mlock);
845 }
846 
847 void w1_slave_found(struct w1_master *dev, u64 rn)
848 {
849 	struct w1_slave *sl;
850 	struct w1_reg_num *tmp;
851 	u64 rn_le = cpu_to_le64(rn);
852 
853 	atomic_inc(&dev->refcnt);
854 
855 	tmp = (struct w1_reg_num *) &rn;
856 
857 	sl = w1_slave_search_device(dev, tmp);
858 	if (sl) {
859 		set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
860 	} else {
861 		if (rn && tmp->crc == w1_calc_crc8((u8 *)&rn_le, 7))
862 			w1_attach_slave_device(dev, tmp);
863 	}
864 
865 	atomic_dec(&dev->refcnt);
866 }
867 
868 /**
869  * Performs a ROM Search & registers any devices found.
870  * The 1-wire search is a simple binary tree search.
871  * For each bit of the address, we read two bits and write one bit.
872  * The bit written will put to sleep all devies that don't match that bit.
873  * When the two reads differ, the direction choice is obvious.
874  * When both bits are 0, we must choose a path to take.
875  * When we can scan all 64 bits without having to choose a path, we are done.
876  *
877  * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
878  *
879  * @dev        The master device to search
880  * @cb         Function to call when a device is found
881  */
882 void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
883 {
884 	u64 last_rn, rn, tmp64;
885 	int i, slave_count = 0;
886 	int last_zero, last_device;
887 	int search_bit, desc_bit;
888 	u8  triplet_ret = 0;
889 
890 	search_bit = 0;
891 	rn = last_rn = 0;
892 	last_device = 0;
893 	last_zero = -1;
894 
895 	desc_bit = 64;
896 
897 	while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
898 		last_rn = rn;
899 		rn = 0;
900 
901 		/*
902 		 * Reset bus and all 1-wire device state machines
903 		 * so they can respond to our requests.
904 		 *
905 		 * Return 0 - device(s) present, 1 - no devices present.
906 		 */
907 		mutex_lock(&dev->bus_mutex);
908 		if (w1_reset_bus(dev)) {
909 			mutex_unlock(&dev->bus_mutex);
910 			dev_dbg(&dev->dev, "No devices present on the wire.\n");
911 			break;
912 		}
913 
914 		/* Do fast search on single slave bus */
915 		if (dev->max_slave_count == 1) {
916 			int rv;
917 			w1_write_8(dev, W1_READ_ROM);
918 			rv = w1_read_block(dev, (u8 *)&rn, 8);
919 			mutex_unlock(&dev->bus_mutex);
920 
921 			if (rv == 8 && rn)
922 				cb(dev, rn);
923 
924 			break;
925 		}
926 
927 		/* Start the search */
928 		w1_write_8(dev, search_type);
929 		for (i = 0; i < 64; ++i) {
930 			/* Determine the direction/search bit */
931 			if (i == desc_bit)
932 				search_bit = 1;	  /* took the 0 path last time, so take the 1 path */
933 			else if (i > desc_bit)
934 				search_bit = 0;	  /* take the 0 path on the next branch */
935 			else
936 				search_bit = ((last_rn >> i) & 0x1);
937 
938 			/** Read two bits and write one bit */
939 			triplet_ret = w1_triplet(dev, search_bit);
940 
941 			/* quit if no device responded */
942 			if ( (triplet_ret & 0x03) == 0x03 )
943 				break;
944 
945 			/* If both directions were valid, and we took the 0 path... */
946 			if (triplet_ret == 0)
947 				last_zero = i;
948 
949 			/* extract the direction taken & update the device number */
950 			tmp64 = (triplet_ret >> 2);
951 			rn |= (tmp64 << i);
952 
953 			/* ensure we're called from kthread and not by netlink callback */
954 			if (!dev->priv && kthread_should_stop()) {
955 				mutex_unlock(&dev->bus_mutex);
956 				dev_dbg(&dev->dev, "Abort w1_search\n");
957 				return;
958 			}
959 		}
960 		mutex_unlock(&dev->bus_mutex);
961 
962 		if ( (triplet_ret & 0x03) != 0x03 ) {
963 			if ( (desc_bit == last_zero) || (last_zero < 0))
964 				last_device = 1;
965 			desc_bit = last_zero;
966 			cb(dev, rn);
967 		}
968 	}
969 }
970 
971 void w1_search_process_cb(struct w1_master *dev, u8 search_type,
972 	w1_slave_found_callback cb)
973 {
974 	struct w1_slave *sl, *sln;
975 
976 	list_for_each_entry(sl, &dev->slist, w1_slave_entry)
977 		clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
978 
979 	w1_search_devices(dev, search_type, cb);
980 
981 	list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
982 		if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl)
983 			w1_slave_detach(sl);
984 		else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))
985 			sl->ttl = dev->slave_ttl;
986 	}
987 
988 	if (dev->search_count > 0)
989 		dev->search_count--;
990 }
991 
992 static void w1_search_process(struct w1_master *dev, u8 search_type)
993 {
994 	w1_search_process_cb(dev, search_type, w1_slave_found);
995 }
996 
997 int w1_process(void *data)
998 {
999 	struct w1_master *dev = (struct w1_master *) data;
1000 	/* As long as w1_timeout is only set by a module parameter the sleep
1001 	 * time can be calculated in jiffies once.
1002 	 */
1003 	const unsigned long jtime = msecs_to_jiffies(w1_timeout * 1000);
1004 
1005 	while (!kthread_should_stop()) {
1006 		if (dev->search_count) {
1007 			mutex_lock(&dev->mutex);
1008 			w1_search_process(dev, W1_SEARCH);
1009 			mutex_unlock(&dev->mutex);
1010 		}
1011 
1012 		try_to_freeze();
1013 		__set_current_state(TASK_INTERRUPTIBLE);
1014 
1015 		if (kthread_should_stop())
1016 			break;
1017 
1018 		/* Only sleep when the search is active. */
1019 		if (dev->search_count)
1020 			schedule_timeout(jtime);
1021 		else
1022 			schedule();
1023 	}
1024 
1025 	atomic_dec(&dev->refcnt);
1026 
1027 	return 0;
1028 }
1029 
1030 static int __init w1_init(void)
1031 {
1032 	int retval;
1033 
1034 	printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
1035 
1036 	w1_init_netlink();
1037 
1038 	retval = bus_register(&w1_bus_type);
1039 	if (retval) {
1040 		printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
1041 		goto err_out_exit_init;
1042 	}
1043 
1044 	retval = bus_register_notifier(&w1_bus_type, &w1_bus_nb);
1045 	if (retval)
1046 		goto err_out_bus_unregister;
1047 
1048 	retval = driver_register(&w1_master_driver);
1049 	if (retval) {
1050 		printk(KERN_ERR
1051 			"Failed to register master driver. err=%d.\n",
1052 			retval);
1053 		goto err_out_bus_unregister;
1054 	}
1055 
1056 	retval = driver_register(&w1_slave_driver);
1057 	if (retval) {
1058 		printk(KERN_ERR
1059 			"Failed to register slave driver. err=%d.\n",
1060 			retval);
1061 		goto err_out_master_unregister;
1062 	}
1063 
1064 	return 0;
1065 
1066 #if 0
1067 /* For undoing the slave register if there was a step after it. */
1068 err_out_slave_unregister:
1069 	driver_unregister(&w1_slave_driver);
1070 #endif
1071 
1072 err_out_master_unregister:
1073 	driver_unregister(&w1_master_driver);
1074 
1075 err_out_bus_unregister:
1076 	bus_unregister(&w1_bus_type);
1077 
1078 err_out_exit_init:
1079 	return retval;
1080 }
1081 
1082 static void __exit w1_fini(void)
1083 {
1084 	struct w1_master *dev;
1085 
1086 	/* Set netlink removal messages and some cleanup */
1087 	list_for_each_entry(dev, &w1_masters, w1_master_entry)
1088 		__w1_remove_master_device(dev);
1089 
1090 	w1_fini_netlink();
1091 
1092 	driver_unregister(&w1_slave_driver);
1093 	driver_unregister(&w1_master_driver);
1094 	bus_unregister(&w1_bus_type);
1095 }
1096 
1097 module_init(w1_init);
1098 module_exit(w1_fini);
1099