w1.c revision 12003375acd879e498c6c511faf27531296f9640
1/*
2 *	w1.c
3 *
4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
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
35#include <asm/atomic.h>
36
37#include "w1.h"
38#include "w1_log.h"
39#include "w1_int.h"
40#include "w1_family.h"
41#include "w1_netlink.h"
42
43MODULE_LICENSE("GPL");
44MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
45MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
46
47static int w1_timeout = 10;
48static int w1_control_timeout = 1;
49int w1_max_slave_count = 10;
50int w1_max_slave_ttl = 10;
51
52module_param_named(timeout, w1_timeout, int, 0);
53module_param_named(control_timeout, w1_control_timeout, int, 0);
54module_param_named(max_slave_count, w1_max_slave_count, int, 0);
55module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
56
57DECLARE_MUTEX(w1_mlock);
58LIST_HEAD(w1_masters);
59
60static struct task_struct *w1_control_thread;
61
62static int w1_master_match(struct device *dev, struct device_driver *drv)
63{
64	return 1;
65}
66
67static int w1_master_probe(struct device *dev)
68{
69	return -ENODEV;
70}
71
72static void w1_master_release(struct device *dev)
73{
74	struct w1_master *md = dev_to_w1_master(dev);
75
76	dev_dbg(dev, "%s: Releasing %s.\n", __func__, md->name);
77	memset(md, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
78	kfree(md);
79}
80
81static void w1_slave_release(struct device *dev)
82{
83	struct w1_slave *sl = dev_to_w1_slave(dev);
84
85	printk("%s: Releasing %s.\n", __func__, sl->name);
86
87	while (atomic_read(&sl->refcnt)) {
88		printk("Waiting for %s to become free: refcnt=%d.\n",
89				sl->name, atomic_read(&sl->refcnt));
90		if (msleep_interruptible(1000))
91			flush_signals(current);
92	}
93
94	w1_family_put(sl->family);
95	sl->master->slave_count--;
96
97	complete(&sl->released);
98}
99
100static ssize_t w1_slave_read_name(struct device *dev, struct device_attribute *attr, char *buf)
101{
102	struct w1_slave *sl = dev_to_w1_slave(dev);
103
104	return sprintf(buf, "%s\n", sl->name);
105}
106
107static ssize_t w1_slave_read_id(struct kobject *kobj, char *buf, loff_t off, size_t count)
108{
109	struct w1_slave *sl = kobj_to_w1_slave(kobj);
110
111	if (off > 8) {
112		count = 0;
113	} else {
114		if (off + count > 8)
115			count = 8 - off;
116
117		memcpy(buf, (u8 *)&sl->reg_num, count);
118	}
119
120	return count;
121}
122
123static struct device_attribute w1_slave_attr_name =
124	__ATTR(name, S_IRUGO, w1_slave_read_name, NULL);
125
126static struct bin_attribute w1_slave_attr_bin_id = {
127      .attr = {
128              .name = "id",
129              .mode = S_IRUGO,
130              .owner = THIS_MODULE,
131      },
132      .size = 8,
133      .read = w1_slave_read_id,
134};
135
136/* Default family */
137
138static ssize_t w1_default_write(struct kobject *kobj, char *buf, loff_t off, size_t count)
139{
140	struct w1_slave *sl = kobj_to_w1_slave(kobj);
141
142	if (down_interruptible(&sl->master->mutex)) {
143		count = 0;
144		goto out;
145	}
146
147	if (w1_reset_select_slave(sl)) {
148		count = 0;
149		goto out_up;
150	}
151
152	w1_write_block(sl->master, buf, count);
153
154out_up:
155	up(&sl->master->mutex);
156out:
157	return count;
158}
159
160static ssize_t w1_default_read(struct kobject *kobj, char *buf, loff_t off, size_t count)
161{
162	struct w1_slave *sl = kobj_to_w1_slave(kobj);
163
164	if (down_interruptible(&sl->master->mutex)) {
165		count = 0;
166		goto out;
167	}
168
169	w1_read_block(sl->master, buf, count);
170
171	up(&sl->master->mutex);
172out:
173	return count;
174}
175
176static struct bin_attribute w1_default_attr = {
177      .attr = {
178              .name = "rw",
179              .mode = S_IRUGO | S_IWUSR,
180              .owner = THIS_MODULE,
181      },
182      .size = PAGE_SIZE,
183      .read = w1_default_read,
184      .write = w1_default_write,
185};
186
187static int w1_default_add_slave(struct w1_slave *sl)
188{
189	return sysfs_create_bin_file(&sl->dev.kobj, &w1_default_attr);
190}
191
192static void w1_default_remove_slave(struct w1_slave *sl)
193{
194	sysfs_remove_bin_file(&sl->dev.kobj, &w1_default_attr);
195}
196
197static struct w1_family_ops w1_default_fops = {
198	.add_slave	= w1_default_add_slave,
199	.remove_slave	= w1_default_remove_slave,
200};
201
202static struct w1_family w1_default_family = {
203	.fops = &w1_default_fops,
204};
205
206static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size);
207
208static struct bus_type w1_bus_type = {
209	.name = "w1",
210	.match = w1_master_match,
211	.uevent = w1_uevent,
212};
213
214struct device_driver w1_master_driver = {
215	.name = "w1_master_driver",
216	.bus = &w1_bus_type,
217	.probe = w1_master_probe,
218};
219
220struct device w1_master_device = {
221	.parent = NULL,
222	.bus = &w1_bus_type,
223	.bus_id = "w1 bus master",
224	.driver = &w1_master_driver,
225	.release = &w1_master_release
226};
227
228struct device_driver w1_slave_driver = {
229	.name = "w1_slave_driver",
230	.bus = &w1_bus_type,
231};
232
233struct device w1_slave_device = {
234	.parent = NULL,
235	.bus = &w1_bus_type,
236	.bus_id = "w1 bus slave",
237	.driver = &w1_slave_driver,
238	.release = &w1_slave_release
239};
240
241static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
242{
243	struct w1_master *md = dev_to_w1_master(dev);
244	ssize_t count;
245
246	if (down_interruptible (&md->mutex))
247		return -EBUSY;
248
249	count = sprintf(buf, "%s\n", md->name);
250
251	up(&md->mutex);
252
253	return count;
254}
255
256static ssize_t w1_master_attribute_store_search(struct device * dev,
257						struct device_attribute *attr,
258						const char * buf, size_t count)
259{
260	struct w1_master *md = dev_to_w1_master(dev);
261
262	if (down_interruptible (&md->mutex))
263		return -EBUSY;
264
265	md->search_count = simple_strtol(buf, NULL, 0);
266
267	up(&md->mutex);
268
269	return count;
270}
271
272static ssize_t w1_master_attribute_show_search(struct device *dev,
273					       struct device_attribute *attr,
274					       char *buf)
275{
276	struct w1_master *md = dev_to_w1_master(dev);
277	ssize_t count;
278
279	if (down_interruptible (&md->mutex))
280		return -EBUSY;
281
282	count = sprintf(buf, "%d\n", md->search_count);
283
284	up(&md->mutex);
285
286	return count;
287}
288
289static ssize_t w1_master_attribute_show_pointer(struct device *dev, struct device_attribute *attr, char *buf)
290{
291	struct w1_master *md = dev_to_w1_master(dev);
292	ssize_t count;
293
294	if (down_interruptible(&md->mutex))
295		return -EBUSY;
296
297	count = sprintf(buf, "0x%p\n", md->bus_master);
298
299	up(&md->mutex);
300	return count;
301}
302
303static ssize_t w1_master_attribute_show_timeout(struct device *dev, struct device_attribute *attr, char *buf)
304{
305	ssize_t count;
306	count = sprintf(buf, "%d\n", w1_timeout);
307	return count;
308}
309
310static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
311{
312	struct w1_master *md = dev_to_w1_master(dev);
313	ssize_t count;
314
315	if (down_interruptible(&md->mutex))
316		return -EBUSY;
317
318	count = sprintf(buf, "%d\n", md->max_slave_count);
319
320	up(&md->mutex);
321	return count;
322}
323
324static 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	if (down_interruptible(&md->mutex))
330		return -EBUSY;
331
332	count = sprintf(buf, "%lu\n", md->attempts);
333
334	up(&md->mutex);
335	return count;
336}
337
338static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
339{
340	struct w1_master *md = dev_to_w1_master(dev);
341	ssize_t count;
342
343	if (down_interruptible(&md->mutex))
344		return -EBUSY;
345
346	count = sprintf(buf, "%d\n", md->slave_count);
347
348	up(&md->mutex);
349	return count;
350}
351
352static ssize_t w1_master_attribute_show_slaves(struct device *dev, struct device_attribute *attr, char *buf)
353{
354	struct w1_master *md = dev_to_w1_master(dev);
355	int c = PAGE_SIZE;
356
357	if (down_interruptible(&md->mutex))
358		return -EBUSY;
359
360	if (md->slave_count == 0)
361		c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
362	else {
363		struct list_head *ent, *n;
364		struct w1_slave *sl;
365
366		list_for_each_safe(ent, n, &md->slist) {
367			sl = list_entry(ent, struct w1_slave, w1_slave_entry);
368
369			c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
370		}
371	}
372
373	up(&md->mutex);
374
375	return PAGE_SIZE - c;
376}
377
378#define W1_MASTER_ATTR_RO(_name, _mode)				\
379	struct device_attribute w1_master_attribute_##_name =	\
380		__ATTR(w1_master_##_name, _mode,		\
381		       w1_master_attribute_show_##_name, NULL)
382
383#define W1_MASTER_ATTR_RW(_name, _mode)				\
384	struct device_attribute w1_master_attribute_##_name =	\
385		__ATTR(w1_master_##_name, _mode,		\
386		       w1_master_attribute_show_##_name,	\
387		       w1_master_attribute_store_##_name)
388
389static W1_MASTER_ATTR_RO(name, S_IRUGO);
390static W1_MASTER_ATTR_RO(slaves, S_IRUGO);
391static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);
392static W1_MASTER_ATTR_RO(max_slave_count, S_IRUGO);
393static W1_MASTER_ATTR_RO(attempts, S_IRUGO);
394static W1_MASTER_ATTR_RO(timeout, S_IRUGO);
395static W1_MASTER_ATTR_RO(pointer, S_IRUGO);
396static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUGO);
397
398static struct attribute *w1_master_default_attrs[] = {
399	&w1_master_attribute_name.attr,
400	&w1_master_attribute_slaves.attr,
401	&w1_master_attribute_slave_count.attr,
402	&w1_master_attribute_max_slave_count.attr,
403	&w1_master_attribute_attempts.attr,
404	&w1_master_attribute_timeout.attr,
405	&w1_master_attribute_pointer.attr,
406	&w1_master_attribute_search.attr,
407	NULL
408};
409
410static struct attribute_group w1_master_defattr_group = {
411	.attrs = w1_master_default_attrs,
412};
413
414int w1_create_master_attributes(struct w1_master *master)
415{
416	return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
417}
418
419void w1_destroy_master_attributes(struct w1_master *master)
420{
421	sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
422}
423
424#ifdef CONFIG_HOTPLUG
425static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
426{
427	struct w1_master *md = NULL;
428	struct w1_slave *sl = NULL;
429	char *event_owner, *name;
430	int err, cur_index=0, cur_len=0;
431
432	if (dev->driver == &w1_master_driver) {
433		md = container_of(dev, struct w1_master, dev);
434		event_owner = "master";
435		name = md->name;
436	} else if (dev->driver == &w1_slave_driver) {
437		sl = container_of(dev, struct w1_slave, dev);
438		event_owner = "slave";
439		name = sl->name;
440	} else {
441		dev_dbg(dev, "Unknown event.\n");
442		return -EINVAL;
443	}
444
445	dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n", event_owner, name, dev->bus_id);
446
447	if (dev->driver != &w1_slave_driver || !sl)
448		return 0;
449
450	err = add_uevent_var(envp, num_envp, &cur_index, buffer, buffer_size, &cur_len, "W1_FID=%02X", sl->reg_num.family);
451	if (err)
452		return err;
453
454	err = add_uevent_var(envp, num_envp, &cur_index, buffer, buffer_size, &cur_len, "W1_SLAVE_ID=%024LX", (u64)sl->reg_num.id);
455	if (err)
456		return err;
457
458	return 0;
459};
460#else
461static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
462{
463	return 0;
464}
465#endif
466
467static int __w1_attach_slave_device(struct w1_slave *sl)
468{
469	int err;
470
471	sl->dev.parent = &sl->master->dev;
472	sl->dev.driver = &w1_slave_driver;
473	sl->dev.bus = &w1_bus_type;
474	sl->dev.release = &w1_slave_release;
475
476	snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id),
477		 "%02x-%012llx",
478		 (unsigned int) sl->reg_num.family,
479		 (unsigned long long) sl->reg_num.id);
480	snprintf(&sl->name[0], sizeof(sl->name),
481		 "%02x-%012llx",
482		 (unsigned int) sl->reg_num.family,
483		 (unsigned long long) sl->reg_num.id);
484
485	dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__, &sl->dev.bus_id[0]);
486
487	err = device_register(&sl->dev);
488	if (err < 0) {
489		dev_err(&sl->dev,
490			"Device registration [%s] failed. err=%d\n",
491			sl->dev.bus_id, err);
492		return err;
493	}
494
495	/* Create "name" entry */
496	err = device_create_file(&sl->dev, &w1_slave_attr_name);
497	if (err < 0) {
498		dev_err(&sl->dev,
499			"sysfs file creation for [%s] failed. err=%d\n",
500			sl->dev.bus_id, err);
501		goto out_unreg;
502	}
503
504	/* Create "id" entry */
505	err = sysfs_create_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
506	if (err < 0) {
507		dev_err(&sl->dev,
508			"sysfs file creation for [%s] failed. err=%d\n",
509			sl->dev.bus_id, err);
510		goto out_rem1;
511	}
512
513	/* if the family driver needs to initialize something... */
514	if (sl->family->fops && sl->family->fops->add_slave &&
515	    ((err = sl->family->fops->add_slave(sl)) < 0)) {
516		dev_err(&sl->dev,
517			"sysfs file creation for [%s] failed. err=%d\n",
518			sl->dev.bus_id, err);
519		goto out_rem2;
520	}
521
522	list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
523
524	return 0;
525
526out_rem2:
527	sysfs_remove_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
528out_rem1:
529	device_remove_file(&sl->dev, &w1_slave_attr_name);
530out_unreg:
531	device_unregister(&sl->dev);
532	return err;
533}
534
535static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
536{
537	struct w1_slave *sl;
538	struct w1_family *f;
539	int err;
540	struct w1_netlink_msg msg;
541
542	sl = kmalloc(sizeof(struct w1_slave), GFP_KERNEL);
543	if (!sl) {
544		dev_err(&dev->dev,
545			 "%s: failed to allocate new slave device.\n",
546			 __func__);
547		return -ENOMEM;
548	}
549
550	memset(sl, 0, sizeof(*sl));
551
552	sl->owner = THIS_MODULE;
553	sl->master = dev;
554	set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
555
556	memset(&msg, 0, sizeof(msg));
557	memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
558	atomic_set(&sl->refcnt, 0);
559	init_completion(&sl->released);
560
561	spin_lock(&w1_flock);
562	f = w1_family_registered(rn->family);
563	if (!f) {
564		f= &w1_default_family;
565		dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
566			  rn->family, rn->family,
567			  (unsigned long long)rn->id, rn->crc);
568	}
569	__w1_family_get(f);
570	spin_unlock(&w1_flock);
571
572	sl->family = f;
573
574
575	err = __w1_attach_slave_device(sl);
576	if (err < 0) {
577		dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
578			 sl->name);
579		w1_family_put(sl->family);
580		kfree(sl);
581		return err;
582	}
583
584	sl->ttl = dev->slave_ttl;
585	dev->slave_count++;
586
587	memcpy(msg.id.id, rn, sizeof(msg.id));
588	msg.type = W1_SLAVE_ADD;
589	w1_netlink_send(dev, &msg);
590
591	return 0;
592}
593
594static void w1_slave_detach(struct w1_slave *sl)
595{
596	struct w1_netlink_msg msg;
597
598	dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__, sl->name, sl);
599
600	list_del(&sl->w1_slave_entry);
601
602	if (sl->family->fops && sl->family->fops->remove_slave)
603		sl->family->fops->remove_slave(sl);
604
605	memset(&msg, 0, sizeof(msg));
606	memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
607	msg.type = W1_SLAVE_REMOVE;
608	w1_netlink_send(sl->master, &msg);
609
610	sysfs_remove_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
611	device_remove_file(&sl->dev, &w1_slave_attr_name);
612	device_unregister(&sl->dev);
613
614	wait_for_completion(&sl->released);
615	kfree(sl);
616}
617
618static struct w1_master *w1_search_master(void *data)
619{
620	struct w1_master *dev;
621	int found = 0;
622
623	down(&w1_mlock);
624	list_for_each_entry(dev, &w1_masters, w1_master_entry) {
625		if (dev->bus_master->data == data) {
626			found = 1;
627			atomic_inc(&dev->refcnt);
628			break;
629		}
630	}
631	up(&w1_mlock);
632
633	return (found)?dev:NULL;
634}
635
636struct w1_master *w1_search_master_id(u32 id)
637{
638	struct w1_master *dev;
639	int found = 0;
640
641	down(&w1_mlock);
642	list_for_each_entry(dev, &w1_masters, w1_master_entry) {
643		if (dev->id == id) {
644			found = 1;
645			atomic_inc(&dev->refcnt);
646			break;
647		}
648	}
649	up(&w1_mlock);
650
651	return (found)?dev:NULL;
652}
653
654struct w1_slave *w1_search_slave(struct w1_reg_num *id)
655{
656	struct w1_master *dev;
657	struct w1_slave *sl = NULL;
658	int found = 0;
659
660	down(&w1_mlock);
661	list_for_each_entry(dev, &w1_masters, w1_master_entry) {
662		down(&dev->mutex);
663		list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
664			if (sl->reg_num.family == id->family &&
665					sl->reg_num.id == id->id &&
666					sl->reg_num.crc == id->crc) {
667				found = 1;
668				atomic_inc(&dev->refcnt);
669				atomic_inc(&sl->refcnt);
670				break;
671			}
672		}
673		up(&dev->mutex);
674
675		if (found)
676			break;
677	}
678	up(&w1_mlock);
679
680	return (found)?sl:NULL;
681}
682
683void w1_reconnect_slaves(struct w1_family *f)
684{
685	struct w1_master *dev;
686
687	down(&w1_mlock);
688	list_for_each_entry(dev, &w1_masters, w1_master_entry) {
689		dev_dbg(&dev->dev, "Reconnecting slaves in %s into new family %02x.\n",
690				dev->name, f->fid);
691		set_bit(W1_MASTER_NEED_RECONNECT, &dev->flags);
692	}
693	up(&w1_mlock);
694}
695
696static void w1_slave_found(void *data, u64 rn)
697{
698	int slave_count;
699	struct w1_slave *sl;
700	struct list_head *ent;
701	struct w1_reg_num *tmp;
702	int family_found = 0;
703	struct w1_master *dev;
704	u64 rn_le = cpu_to_le64(rn);
705
706	dev = w1_search_master(data);
707	if (!dev) {
708		printk(KERN_ERR "Failed to find w1 master device for data %p, "
709		       "it is impossible.\n", data);
710		return;
711	}
712
713	tmp = (struct w1_reg_num *) &rn;
714
715	slave_count = 0;
716	list_for_each(ent, &dev->slist) {
717
718		sl = list_entry(ent, struct w1_slave, w1_slave_entry);
719
720		if (sl->reg_num.family == tmp->family &&
721		    sl->reg_num.id == tmp->id &&
722		    sl->reg_num.crc == tmp->crc) {
723			set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
724			break;
725		} else if (sl->reg_num.family == tmp->family) {
726			family_found = 1;
727			break;
728		}
729
730		slave_count++;
731	}
732
733	if (slave_count == dev->slave_count &&
734		rn && ((rn >> 56) & 0xff) == w1_calc_crc8((u8 *)&rn_le, 7)) {
735		w1_attach_slave_device(dev, tmp);
736	}
737
738	atomic_dec(&dev->refcnt);
739}
740
741/**
742 * Performs a ROM Search & registers any devices found.
743 * The 1-wire search is a simple binary tree search.
744 * For each bit of the address, we read two bits and write one bit.
745 * The bit written will put to sleep all devies that don't match that bit.
746 * When the two reads differ, the direction choice is obvious.
747 * When both bits are 0, we must choose a path to take.
748 * When we can scan all 64 bits without having to choose a path, we are done.
749 *
750 * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
751 *
752 * @dev        The master device to search
753 * @cb         Function to call when a device is found
754 */
755void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
756{
757	u64 last_rn, rn, tmp64;
758	int i, slave_count = 0;
759	int last_zero, last_device;
760	int search_bit, desc_bit;
761	u8  triplet_ret = 0;
762
763	search_bit = 0;
764	rn = last_rn = 0;
765	last_device = 0;
766	last_zero = -1;
767
768	desc_bit = 64;
769
770	while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
771		last_rn = rn;
772		rn = 0;
773
774		/*
775		 * Reset bus and all 1-wire device state machines
776		 * so they can respond to our requests.
777		 *
778		 * Return 0 - device(s) present, 1 - no devices present.
779		 */
780		if (w1_reset_bus(dev)) {
781			dev_dbg(&dev->dev, "No devices present on the wire.\n");
782			break;
783		}
784
785		/* Start the search */
786		w1_write_8(dev, search_type);
787		for (i = 0; i < 64; ++i) {
788			/* Determine the direction/search bit */
789			if (i == desc_bit)
790				search_bit = 1;	  /* took the 0 path last time, so take the 1 path */
791			else if (i > desc_bit)
792				search_bit = 0;	  /* take the 0 path on the next branch */
793			else
794				search_bit = ((last_rn >> i) & 0x1);
795
796			/** Read two bits and write one bit */
797			triplet_ret = w1_triplet(dev, search_bit);
798
799			/* quit if no device responded */
800			if ( (triplet_ret & 0x03) == 0x03 )
801				break;
802
803			/* If both directions were valid, and we took the 0 path... */
804			if (triplet_ret == 0)
805				last_zero = i;
806
807			/* extract the direction taken & update the device number */
808			tmp64 = (triplet_ret >> 2);
809			rn |= (tmp64 << i);
810		}
811
812		if ( (triplet_ret & 0x03) != 0x03 ) {
813			if ( (desc_bit == last_zero) || (last_zero < 0))
814				last_device = 1;
815			desc_bit = last_zero;
816			cb(dev->bus_master->data, rn);
817		}
818	}
819}
820
821static int w1_control(void *data)
822{
823	struct w1_slave *sl, *sln;
824	struct w1_master *dev, *n;
825	int have_to_wait = 0;
826
827	while (!kthread_should_stop() || have_to_wait) {
828		have_to_wait = 0;
829
830		try_to_freeze();
831		msleep_interruptible(w1_control_timeout * 1000);
832
833		list_for_each_entry_safe(dev, n, &w1_masters, w1_master_entry) {
834			if (!kthread_should_stop() && !dev->flags)
835				continue;
836			/*
837			 * Little race: we can create thread but not set the flag.
838			 * Get a chance for external process to set flag up.
839			 */
840			if (!dev->initialized) {
841				have_to_wait = 1;
842				continue;
843			}
844
845			if (kthread_should_stop() || test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {
846				set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
847
848				down(&w1_mlock);
849				list_del(&dev->w1_master_entry);
850				up(&w1_mlock);
851
852				down(&dev->mutex);
853				list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
854					w1_slave_detach(sl);
855				}
856				w1_destroy_master_attributes(dev);
857				up(&dev->mutex);
858				atomic_dec(&dev->refcnt);
859				continue;
860			}
861
862			if (test_bit(W1_MASTER_NEED_RECONNECT, &dev->flags)) {
863				dev_dbg(&dev->dev, "Reconnecting slaves in device %s.\n", dev->name);
864				down(&dev->mutex);
865				list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
866					if (sl->family->fid == W1_FAMILY_DEFAULT) {
867						struct w1_reg_num rn;
868
869						memcpy(&rn, &sl->reg_num, sizeof(rn));
870						w1_slave_detach(sl);
871
872						w1_attach_slave_device(dev, &rn);
873					}
874				}
875				dev_dbg(&dev->dev, "Reconnecting slaves in device %s has been finished.\n", dev->name);
876				clear_bit(W1_MASTER_NEED_RECONNECT, &dev->flags);
877				up(&dev->mutex);
878			}
879		}
880	}
881
882	return 0;
883}
884
885void w1_search_process(struct w1_master *dev, u8 search_type)
886{
887	struct w1_slave *sl, *sln;
888
889	list_for_each_entry(sl, &dev->slist, w1_slave_entry)
890		clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
891
892	w1_search_devices(dev, search_type, w1_slave_found);
893
894	list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
895		if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) {
896			w1_slave_detach(sl);
897
898			dev->slave_count--;
899		} else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))
900			sl->ttl = dev->slave_ttl;
901	}
902
903	if (dev->search_count > 0)
904		dev->search_count--;
905}
906
907int w1_process(void *data)
908{
909	struct w1_master *dev = (struct w1_master *) data;
910
911	while (!kthread_should_stop() && !test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {
912		try_to_freeze();
913		msleep_interruptible(w1_timeout * 1000);
914
915		if (kthread_should_stop() || test_bit(W1_MASTER_NEED_EXIT, &dev->flags))
916			break;
917
918		if (!dev->initialized)
919			continue;
920
921		if (dev->search_count == 0)
922			continue;
923
924		if (down_interruptible(&dev->mutex))
925			continue;
926
927		w1_search_process(dev, W1_SEARCH);
928
929		up(&dev->mutex);
930	}
931
932	atomic_dec(&dev->refcnt);
933
934	return 0;
935}
936
937static int w1_init(void)
938{
939	int retval;
940
941	printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
942
943	w1_init_netlink();
944
945	retval = bus_register(&w1_bus_type);
946	if (retval) {
947		printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
948		goto err_out_exit_init;
949	}
950
951	retval = driver_register(&w1_master_driver);
952	if (retval) {
953		printk(KERN_ERR
954			"Failed to register master driver. err=%d.\n",
955			retval);
956		goto err_out_bus_unregister;
957	}
958
959	retval = driver_register(&w1_slave_driver);
960	if (retval) {
961		printk(KERN_ERR
962			"Failed to register master driver. err=%d.\n",
963			retval);
964		goto err_out_master_unregister;
965	}
966
967	w1_control_thread = kthread_run(w1_control, NULL, "w1_control");
968	if (IS_ERR(w1_control_thread)) {
969		retval = PTR_ERR(w1_control_thread);
970		printk(KERN_ERR "Failed to create control thread. err=%d\n",
971			retval);
972		goto err_out_slave_unregister;
973	}
974
975	return 0;
976
977err_out_slave_unregister:
978	driver_unregister(&w1_slave_driver);
979
980err_out_master_unregister:
981	driver_unregister(&w1_master_driver);
982
983err_out_bus_unregister:
984	bus_unregister(&w1_bus_type);
985
986err_out_exit_init:
987	return retval;
988}
989
990static void w1_fini(void)
991{
992	struct w1_master *dev;
993
994	list_for_each_entry(dev, &w1_masters, w1_master_entry)
995		__w1_remove_master_device(dev);
996
997	w1_fini_netlink();
998
999	kthread_stop(w1_control_thread);
1000
1001	driver_unregister(&w1_slave_driver);
1002	driver_unregister(&w1_master_driver);
1003	bus_unregister(&w1_bus_type);
1004}
1005
1006module_init(w1_init);
1007module_exit(w1_fini);
1008