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