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