]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/w1/w1.c
[PATCH] w1: Make w1 connector notifications depend on connector.
[mirror_ubuntu-artful-kernel.git] / drivers / w1 / w1.c
CommitLineData
1da177e4 1/*
7785925d 2 * w1.c
1da177e4
LT
3 *
4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
7785925d 5 *
1da177e4
LT
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>
674a396c 33#include <linux/kthread.h>
1da177e4
LT
34
35#include <asm/atomic.h>
36
37#include "w1.h"
1da177e4
LT
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;
3aca692d 48static int w1_control_timeout = 1;
1da177e4
LT
49int w1_max_slave_count = 10;
50int w1_max_slave_ttl = 10;
51
52module_param_named(timeout, w1_timeout, int, 0);
3aca692d 53module_param_named(control_timeout, w1_control_timeout, int, 0);
1da177e4
LT
54module_param_named(max_slave_count, w1_max_slave_count, int, 0);
55module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
56
12003375 57DECLARE_MUTEX(w1_mlock);
1da177e4
LT
58LIST_HEAD(w1_masters);
59
674a396c 60static struct task_struct *w1_control_thread;
1da177e4
LT
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
1da177e4
LT
72static void w1_master_release(struct device *dev)
73{
db2d0008 74 struct w1_master *md = dev_to_w1_master(dev);
3aca692d
EP
75
76 dev_dbg(dev, "%s: Releasing %s.\n", __func__, md->name);
3aca692d
EP
77 memset(md, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
78 kfree(md);
1da177e4
LT
79}
80
81static void w1_slave_release(struct device *dev)
82{
db2d0008 83 struct w1_slave *sl = dev_to_w1_slave(dev);
3aca692d 84
12003375 85 printk("%s: Releasing %s.\n", __func__, sl->name);
3aca692d
EP
86
87 while (atomic_read(&sl->refcnt)) {
12003375 88 printk("Waiting for %s to become free: refcnt=%d.\n",
3aca692d
EP
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);
1da177e4
LT
98}
99
d2a4ef6a 100static ssize_t w1_slave_read_name(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4 101{
3aca692d 102 struct w1_slave *sl = dev_to_w1_slave(dev);
d2a4ef6a 103
3aca692d 104 return sprintf(buf, "%s\n", sl->name);
1da177e4
LT
105}
106
d2a4ef6a 107static ssize_t w1_slave_read_id(struct kobject *kobj, char *buf, loff_t off, size_t count)
1da177e4 108{
3aca692d 109 struct w1_slave *sl = kobj_to_w1_slave(kobj);
1da177e4 110
3aca692d
EP
111 if (off > 8) {
112 count = 0;
d2a4ef6a
EP
113 } else {
114 if (off + count > 8)
115 count = 8 - off;
116
117 memcpy(buf, (u8 *)&sl->reg_num, count);
118 }
d2a4ef6a
EP
119
120 return count;
3aca692d 121}
d2a4ef6a
EP
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,
7785925d
EP
134};
135
d2a4ef6a 136/* Default family */
f522d239
EP
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};
d2a4ef6a 205
312c004d 206static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size);
7785925d 207
1da177e4
LT
208static struct bus_type w1_bus_type = {
209 .name = "w1",
210 .match = w1_master_match,
312c004d 211 .uevent = w1_uevent,
1da177e4
LT
212};
213
7f772ed8
EP
214struct device_driver w1_master_driver = {
215 .name = "w1_master_driver",
1da177e4
LT
216 .bus = &w1_bus_type,
217 .probe = w1_master_probe,
1da177e4
LT
218};
219
7f772ed8 220struct device w1_master_device = {
1da177e4
LT
221 .parent = NULL,
222 .bus = &w1_bus_type,
223 .bus_id = "w1 bus master",
7f772ed8 224 .driver = &w1_master_driver,
1da177e4
LT
225 .release = &w1_master_release
226};
227
12003375 228struct device_driver w1_slave_driver = {
7f772ed8
EP
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,
3aca692d 238 .release = &w1_slave_release
7f772ed8
EP
239};
240
060b8845 241static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4 242{
db2d0008 243 struct w1_master *md = dev_to_w1_master(dev);
1da177e4 244 ssize_t count;
7785925d 245
1da177e4
LT
246 if (down_interruptible (&md->mutex))
247 return -EBUSY;
248
249 count = sprintf(buf, "%s\n", md->name);
7785925d 250
1da177e4
LT
251 up(&md->mutex);
252
253 return count;
254}
255
2a9d0c17
EP
256static ssize_t w1_master_attribute_store_search(struct device * dev,
257 struct device_attribute *attr,
258 const char * buf, size_t count)
259{
db2d0008 260 struct w1_master *md = dev_to_w1_master(dev);
2a9d0c17
EP
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{
db2d0008 276 struct w1_master *md = dev_to_w1_master(dev);
2a9d0c17
EP
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
060b8845 289static ssize_t w1_master_attribute_show_pointer(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4 290{
db2d0008 291 struct w1_master *md = dev_to_w1_master(dev);
1da177e4 292 ssize_t count;
7785925d 293
1da177e4
LT
294 if (down_interruptible(&md->mutex))
295 return -EBUSY;
296
297 count = sprintf(buf, "0x%p\n", md->bus_master);
7785925d 298
1da177e4
LT
299 up(&md->mutex);
300 return count;
301}
302
060b8845 303static ssize_t w1_master_attribute_show_timeout(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
304{
305 ssize_t count;
306 count = sprintf(buf, "%d\n", w1_timeout);
307 return count;
308}
309
060b8845 310static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4 311{
db2d0008 312 struct w1_master *md = dev_to_w1_master(dev);
1da177e4 313 ssize_t count;
7785925d 314
1da177e4
LT
315 if (down_interruptible(&md->mutex))
316 return -EBUSY;
317
318 count = sprintf(buf, "%d\n", md->max_slave_count);
7785925d 319
1da177e4
LT
320 up(&md->mutex);
321 return count;
322}
323
060b8845 324static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4 325{
db2d0008 326 struct w1_master *md = dev_to_w1_master(dev);
1da177e4 327 ssize_t count;
7785925d 328
1da177e4
LT
329 if (down_interruptible(&md->mutex))
330 return -EBUSY;
331
332 count = sprintf(buf, "%lu\n", md->attempts);
7785925d 333
1da177e4
LT
334 up(&md->mutex);
335 return count;
336}
337
060b8845 338static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4 339{
db2d0008 340 struct w1_master *md = dev_to_w1_master(dev);
1da177e4 341 ssize_t count;
7785925d 342
1da177e4
LT
343 if (down_interruptible(&md->mutex))
344 return -EBUSY;
345
346 count = sprintf(buf, "%d\n", md->slave_count);
7785925d 347
1da177e4
LT
348 up(&md->mutex);
349 return count;
350}
351
060b8845 352static ssize_t w1_master_attribute_show_slaves(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4 353{
db2d0008 354 struct w1_master *md = dev_to_w1_master(dev);
1da177e4
LT
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
7785925d 369 c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
1da177e4
LT
370 }
371 }
372
373 up(&md->mutex);
374
375 return PAGE_SIZE - c;
376}
377
7785925d
EP
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
2a9d0c17
EP
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
7785925d
EP
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);
2a9d0c17 396static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUGO);
7785925d
EP
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,
2a9d0c17 406 &w1_master_attribute_search.attr,
7785925d 407 NULL
1da177e4
LT
408};
409
7785925d
EP
410static struct attribute_group w1_master_defattr_group = {
411 .attrs = w1_master_default_attrs,
1da177e4
LT
412};
413
7785925d
EP
414int w1_create_master_attributes(struct w1_master *master)
415{
416 return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
417}
418
12003375 419void w1_destroy_master_attributes(struct w1_master *master)
7785925d
EP
420{
421 sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
422}
423
7f772ed8 424#ifdef CONFIG_HOTPLUG
312c004d 425static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
7f772ed8
EP
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 {
312c004d 441 dev_dbg(dev, "Unknown event.\n");
7f772ed8
EP
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
12003375 450 err = add_uevent_var(envp, num_envp, &cur_index, buffer, buffer_size, &cur_len, "W1_FID=%02X", sl->reg_num.family);
7f772ed8
EP
451 if (err)
452 return err;
453
12003375 454 err = add_uevent_var(envp, num_envp, &cur_index, buffer, buffer_size, &cur_len, "W1_SLAVE_ID=%024LX", (u64)sl->reg_num.id);
7f772ed8
EP
455 if (err)
456 return err;
457
458 return 0;
459};
460#else
312c004d 461static int w1_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
7f772ed8
EP
462{
463 return 0;
464}
465#endif
466
1da177e4
LT
467static int __w1_attach_slave_device(struct w1_slave *sl)
468{
469 int err;
470
471 sl->dev.parent = &sl->master->dev;
7f772ed8 472 sl->dev.driver = &w1_slave_driver;
1da177e4
LT
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),
6b729861
EP
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);
1da177e4 484
3aca692d 485 dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__, &sl->dev.bus_id[0]);
1da177e4
LT
486
487 err = device_register(&sl->dev);
488 if (err < 0) {
489 dev_err(&sl->dev,
6b729861
EP
490 "Device registration [%s] failed. err=%d\n",
491 sl->dev.bus_id, err);
1da177e4
LT
492 return err;
493 }
494
d2a4ef6a
EP
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 }
1da177e4 503
d2a4ef6a
EP
504 /* Create "id" entry */
505 err = sysfs_create_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
1da177e4
LT
506 if (err < 0) {
507 dev_err(&sl->dev,
6b729861
EP
508 "sysfs file creation for [%s] failed. err=%d\n",
509 sl->dev.bus_id, err);
d2a4ef6a 510 goto out_rem1;
1da177e4
LT
511 }
512
d2a4ef6a
EP
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;
1da177e4
LT
520 }
521
522 list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
523
524 return 0;
d2a4ef6a
EP
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;
1da177e4
LT
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
12003375 556 memset(&msg, 0, sizeof(msg));
1da177e4
LT
557 memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
558 atomic_set(&sl->refcnt, 0);
3aca692d 559 init_completion(&sl->released);
1da177e4
LT
560
561 spin_lock(&w1_flock);
562 f = w1_family_registered(rn->family);
563 if (!f) {
99c5bfe9 564 f= &w1_default_family;
1da177e4
LT
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);
1da177e4
LT
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
12003375 587 memcpy(msg.id.id, rn, sizeof(msg.id));
1da177e4
LT
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;
7785925d 597
7c8f5703 598 dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__, sl->name, sl);
1da177e4 599
3aca692d 600 list_del(&sl->w1_slave_entry);
1da177e4 601
d2a4ef6a
EP
602 if (sl->family->fops && sl->family->fops->remove_slave)
603 sl->family->fops->remove_slave(sl);
604
12003375
EP
605 memset(&msg, 0, sizeof(msg));
606 memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
3aca692d
EP
607 msg.type = W1_SLAVE_REMOVE;
608 w1_netlink_send(sl->master, &msg);
609
d2a4ef6a
EP
610 sysfs_remove_bin_file(&sl->dev.kobj, &w1_slave_attr_bin_id);
611 device_remove_file(&sl->dev, &w1_slave_attr_name);
1da177e4 612 device_unregister(&sl->dev);
1da177e4 613
3aca692d
EP
614 wait_for_completion(&sl->released);
615 kfree(sl);
1da177e4
LT
616}
617
ccd69940 618static struct w1_master *w1_search_master(void *data)
1da177e4
LT
619{
620 struct w1_master *dev;
621 int found = 0;
7785925d 622
12003375 623 down(&w1_mlock);
1da177e4
LT
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 }
12003375 631 up(&w1_mlock);
1da177e4
LT
632
633 return (found)?dev:NULL;
634}
635
12003375
EP
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
6adf87bd
EP
683void w1_reconnect_slaves(struct w1_family *f)
684{
685 struct w1_master *dev;
686
12003375 687 down(&w1_mlock);
6adf87bd 688 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
7c8f5703 689 dev_dbg(&dev->dev, "Reconnecting slaves in %s into new family %02x.\n",
6adf87bd
EP
690 dev->name, f->fid);
691 set_bit(W1_MASTER_NEED_RECONNECT, &dev->flags);
692 }
12003375 693 up(&w1_mlock);
6adf87bd
EP
694}
695
ccd69940 696static void w1_slave_found(void *data, u64 rn)
1da177e4
LT
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;
0e65f828 704 u64 rn_le = cpu_to_le64(rn);
1da177e4
LT
705
706 dev = w1_search_master(data);
707 if (!dev) {
ccd69940
EP
708 printk(KERN_ERR "Failed to find w1 master device for data %p, "
709 "it is impossible.\n", data);
1da177e4
LT
710 return;
711 }
7785925d 712
1da177e4
LT
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;
7785925d 725 } else if (sl->reg_num.family == tmp->family) {
1da177e4
LT
726 family_found = 1;
727 break;
728 }
729
730 slave_count++;
731 }
732
8523ff45 733 if (slave_count == dev->slave_count &&
0e65f828 734 rn && ((rn >> 56) & 0xff) == w1_calc_crc8((u8 *)&rn_le, 7)) {
8523ff45 735 w1_attach_slave_device(dev, tmp);
1da177e4 736 }
7785925d 737
1da177e4
LT
738 atomic_dec(&dev->refcnt);
739}
740
6b729861
EP
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 */
12003375 755void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
1da177e4 756{
6b729861
EP
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;
1da177e4 762
6b729861
EP
763 search_bit = 0;
764 rn = last_rn = 0;
765 last_device = 0;
766 last_zero = -1;
1da177e4
LT
767
768 desc_bit = 64;
769
6b729861
EP
770 while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
771 last_rn = rn;
1da177e4
LT
772 rn = 0;
773
1da177e4
LT
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)) {
2da5bf80 781 dev_dbg(&dev->dev, "No devices present on the wire.\n");
1da177e4
LT
782 break;
783 }
784
6b729861 785 /* Start the search */
12003375 786 w1_write_8(dev, search_type);
1da177e4 787 for (i = 0; i < 64; ++i) {
6b729861
EP
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 */
1da177e4 793 else
6b729861 794 search_bit = ((last_rn >> i) & 0x1);
1da177e4 795
6b729861
EP
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;
1da177e4 802
6b729861
EP
803 /* If both directions were valid, and we took the 0 path... */
804 if (triplet_ret == 0)
805 last_zero = i;
1da177e4 806
6b729861
EP
807 /* extract the direction taken & update the device number */
808 tmp64 = (triplet_ret >> 2);
809 rn |= (tmp64 << i);
810 }
7785925d 811
6b729861
EP
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 }
1da177e4
LT
818 }
819}
820
7785925d 821static int w1_control(void *data)
1da177e4 822{
7785925d
EP
823 struct w1_slave *sl, *sln;
824 struct w1_master *dev, *n;
674a396c 825 int have_to_wait = 0;
1da177e4 826
674a396c 827 while (!kthread_should_stop() || have_to_wait) {
1da177e4
LT
828 have_to_wait = 0;
829
3e1d1d28 830 try_to_freeze();
3aca692d 831 msleep_interruptible(w1_control_timeout * 1000);
1da177e4 832
7785925d 833 list_for_each_entry_safe(dev, n, &w1_masters, w1_master_entry) {
674a396c 834 if (!kthread_should_stop() && !dev->flags)
1da177e4
LT
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
674a396c 845 if (kthread_should_stop() || test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {
6adf87bd 846 set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
1da177e4 847
12003375 848 down(&w1_mlock);
6adf87bd 849 list_del(&dev->w1_master_entry);
12003375 850 up(&w1_mlock);
6adf87bd 851
3aca692d 852 down(&dev->mutex);
6adf87bd 853 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
6adf87bd 854 w1_slave_detach(sl);
6adf87bd
EP
855 }
856 w1_destroy_master_attributes(dev);
3aca692d 857 up(&dev->mutex);
6adf87bd
EP
858 atomic_dec(&dev->refcnt);
859 continue;
860 }
861
862 if (test_bit(W1_MASTER_NEED_RECONNECT, &dev->flags)) {
7c8f5703 863 dev_dbg(&dev->dev, "Reconnecting slaves in device %s.\n", dev->name);
6adf87bd 864 down(&dev->mutex);
3aca692d 865 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
6adf87bd
EP
866 if (sl->family->fid == W1_FAMILY_DEFAULT) {
867 struct w1_reg_num rn;
6adf87bd
EP
868
869 memcpy(&rn, &sl->reg_num, sizeof(rn));
3aca692d 870 w1_slave_detach(sl);
1da177e4 871
6adf87bd
EP
872 w1_attach_slave_device(dev, &rn);
873 }
874 }
7c8f5703 875 dev_dbg(&dev->dev, "Reconnecting slaves in device %s has been finished.\n", dev->name);
6adf87bd
EP
876 clear_bit(W1_MASTER_NEED_RECONNECT, &dev->flags);
877 up(&dev->mutex);
1da177e4 878 }
1da177e4
LT
879 }
880 }
881
674a396c 882 return 0;
1da177e4
LT
883}
884
12003375
EP
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
1da177e4
LT
907int w1_process(void *data)
908{
909 struct w1_master *dev = (struct w1_master *) data;
1da177e4 910
674a396c 911 while (!kthread_should_stop() && !test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {
3e1d1d28 912 try_to_freeze();
1da177e4
LT
913 msleep_interruptible(w1_timeout * 1000);
914
674a396c 915 if (kthread_should_stop() || test_bit(W1_MASTER_NEED_EXIT, &dev->flags))
1da177e4
LT
916 break;
917
918 if (!dev->initialized)
919 continue;
920
2a9d0c17
EP
921 if (dev->search_count == 0)
922 continue;
923
1da177e4
LT
924 if (down_interruptible(&dev->mutex))
925 continue;
926
12003375 927 w1_search_process(dev, W1_SEARCH);
2a9d0c17 928
1da177e4
LT
929 up(&dev->mutex);
930 }
931
932 atomic_dec(&dev->refcnt);
1da177e4
LT
933
934 return 0;
935}
936
7785925d 937static int w1_init(void)
1da177e4
LT
938{
939 int retval;
940
941 printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
942
12003375
EP
943 w1_init_netlink();
944
1da177e4
LT
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
7f772ed8 951 retval = driver_register(&w1_master_driver);
1da177e4
LT
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
7f772ed8
EP
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
674a396c
EP
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);
1da177e4 970 printk(KERN_ERR "Failed to create control thread. err=%d\n",
674a396c 971 retval);
7f772ed8 972 goto err_out_slave_unregister;
1da177e4
LT
973 }
974
975 return 0;
976
7f772ed8
EP
977err_out_slave_unregister:
978 driver_unregister(&w1_slave_driver);
979
980err_out_master_unregister:
981 driver_unregister(&w1_master_driver);
1da177e4
LT
982
983err_out_bus_unregister:
984 bus_unregister(&w1_bus_type);
985
986err_out_exit_init:
987 return retval;
988}
989
7785925d 990static void w1_fini(void)
1da177e4
LT
991{
992 struct w1_master *dev;
1da177e4 993
7785925d 994 list_for_each_entry(dev, &w1_masters, w1_master_entry)
1da177e4 995 __w1_remove_master_device(dev);
1da177e4 996
12003375
EP
997 w1_fini_netlink();
998
674a396c 999 kthread_stop(w1_control_thread);
1da177e4 1000
7f772ed8
EP
1001 driver_unregister(&w1_slave_driver);
1002 driver_unregister(&w1_master_driver);
1da177e4
LT
1003 bus_unregister(&w1_bus_type);
1004}
1005
1006module_init(w1_init);
1007module_exit(w1_fini);