treewide: Add SPDX license identifier for missed files
[linux-2.6-block.git] / drivers / w1 / w1.c
CommitLineData
1da177e4 1/*
a8018766 2 * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
7785925d 3 *
1da177e4
LT
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
1da177e4
LT
13 */
14
15#include <linux/delay.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/moduleparam.h>
19#include <linux/list.h>
20#include <linux/interrupt.h>
21#include <linux/spinlock.h>
22#include <linux/timer.h>
23#include <linux/device.h>
24#include <linux/slab.h>
25#include <linux/sched.h>
674a396c 26#include <linux/kthread.h>
7dfb7103 27#include <linux/freezer.h>
2eb79548 28#include <linux/hwmon.h>
fae68031 29#include <linux/of.h>
1da177e4 30
60063497 31#include <linux/atomic.h>
1da177e4 32
de0d6dbd 33#include "w1_internal.h"
1da177e4
LT
34#include "w1_netlink.h"
35
de0d6dbd
AD
36#define W1_FAMILY_DEFAULT 0
37
1da177e4 38static int w1_timeout = 10;
1da177e4 39module_param_named(timeout, w1_timeout, int, 0);
b3be177a 40MODULE_PARM_DESC(timeout, "time in seconds between automatic slave searches");
50fa2951
AD
41
42static int w1_timeout_us = 0;
c3098356 43module_param_named(timeout_us, w1_timeout_us, int, 0);
a46b195c
WY
44MODULE_PARM_DESC(timeout_us,
45 "time in microseconds between automatic slave searches");
50fa2951 46
b3be177a
DF
47/* A search stops when w1_max_slave_count devices have been found in that
48 * search. The next search will start over and detect the same set of devices
49 * on a static 1-wire bus. Memory is not allocated based on this number, just
50 * on the number of devices known to the kernel. Having a high number does not
51 * consume additional resources. As a special case, if there is only one
52 * device on the network and w1_max_slave_count is set to 1, the device id can
53 * be read directly skipping the normal slower search process.
54 */
50fa2951 55int w1_max_slave_count = 64;
1da177e4 56module_param_named(max_slave_count, w1_max_slave_count, int, 0);
b3be177a
DF
57MODULE_PARM_DESC(max_slave_count,
58 "maximum number of slaves detected in a search");
50fa2951
AD
59
60int w1_max_slave_ttl = 10;
1da177e4 61module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
b3be177a
DF
62MODULE_PARM_DESC(slave_ttl,
63 "Number of searches not seeing a slave before it will be removed");
1da177e4 64
abd52a13 65DEFINE_MUTEX(w1_mlock);
1da177e4
LT
66LIST_HEAD(w1_masters);
67
1da177e4
LT
68static int w1_master_match(struct device *dev, struct device_driver *drv)
69{
70 return 1;
71}
72
73static int w1_master_probe(struct device *dev)
74{
75 return -ENODEV;
76}
77
1da177e4
LT
78static void w1_master_release(struct device *dev)
79{
db2d0008 80 struct w1_master *md = dev_to_w1_master(dev);
3aca692d
EP
81
82 dev_dbg(dev, "%s: Releasing %s.\n", __func__, md->name);
3aca692d
EP
83 memset(md, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
84 kfree(md);
1da177e4
LT
85}
86
87static void w1_slave_release(struct device *dev)
88{
db2d0008 89 struct w1_slave *sl = dev_to_w1_slave(dev);
3aca692d 90
9fcbbac5 91 dev_dbg(dev, "%s: Releasing %s [%p]\n", __func__, sl->name, sl);
3aca692d
EP
92
93 w1_family_put(sl->family);
94 sl->master->slave_count--;
1da177e4
LT
95}
96
5b187b3c 97static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4 98{
3aca692d 99 struct w1_slave *sl = dev_to_w1_slave(dev);
d2a4ef6a 100
3aca692d 101 return sprintf(buf, "%s\n", sl->name);
1da177e4 102}
5b187b3c 103static DEVICE_ATTR_RO(name);
1da177e4 104
5b187b3c 105static ssize_t id_show(struct device *dev,
07e00341 106 struct device_attribute *attr, char *buf)
1da177e4 107{
07e00341
DF
108 struct w1_slave *sl = dev_to_w1_slave(dev);
109 ssize_t count = sizeof(sl->reg_num);
d2a4ef6a 110
07e00341 111 memcpy(buf, (u8 *)&sl->reg_num, count);
d2a4ef6a 112 return count;
3aca692d 113}
5b187b3c 114static DEVICE_ATTR_RO(id);
d2a4ef6a 115
5b187b3c
GKH
116static struct attribute *w1_slave_attrs[] = {
117 &dev_attr_name.attr,
118 &dev_attr_id.attr,
119 NULL,
120};
121ATTRIBUTE_GROUPS(w1_slave);
7785925d 122
d2a4ef6a 123/* Default family */
f522d239 124
36c27a65
GKH
125static ssize_t rw_write(struct file *filp, struct kobject *kobj,
126 struct bin_attribute *bin_attr, char *buf, loff_t off,
127 size_t count)
f522d239
EP
128{
129 struct w1_slave *sl = kobj_to_w1_slave(kobj);
130
abd52a13 131 mutex_lock(&sl->master->mutex);
f522d239
EP
132 if (w1_reset_select_slave(sl)) {
133 count = 0;
134 goto out_up;
135 }
136
137 w1_write_block(sl->master, buf, count);
138
139out_up:
abd52a13 140 mutex_unlock(&sl->master->mutex);
f522d239
EP
141 return count;
142}
143
36c27a65
GKH
144static ssize_t rw_read(struct file *filp, struct kobject *kobj,
145 struct bin_attribute *bin_attr, char *buf, loff_t off,
146 size_t count)
f522d239
EP
147{
148 struct w1_slave *sl = kobj_to_w1_slave(kobj);
149
abd52a13 150 mutex_lock(&sl->master->mutex);
f522d239 151 w1_read_block(sl->master, buf, count);
abd52a13 152 mutex_unlock(&sl->master->mutex);
f522d239
EP
153 return count;
154}
155
36c27a65
GKH
156static BIN_ATTR_RW(rw, PAGE_SIZE);
157
158static struct bin_attribute *w1_slave_bin_attrs[] = {
159 &bin_attr_rw,
160 NULL,
f522d239
EP
161};
162
36c27a65
GKH
163static const struct attribute_group w1_slave_default_group = {
164 .bin_attrs = w1_slave_bin_attrs,
165};
f522d239 166
36c27a65
GKH
167static const struct attribute_group *w1_slave_default_groups[] = {
168 &w1_slave_default_group,
169 NULL,
170};
f522d239
EP
171
172static struct w1_family_ops w1_default_fops = {
36c27a65 173 .groups = w1_slave_default_groups,
f522d239
EP
174};
175
176static struct w1_family w1_default_family = {
177 .fops = &w1_default_fops,
178};
d2a4ef6a 179
7eff2e7a 180static int w1_uevent(struct device *dev, struct kobj_uevent_env *env);
7785925d 181
1da177e4
LT
182static struct bus_type w1_bus_type = {
183 .name = "w1",
184 .match = w1_master_match,
312c004d 185 .uevent = w1_uevent,
1da177e4
LT
186};
187
7f772ed8
EP
188struct device_driver w1_master_driver = {
189 .name = "w1_master_driver",
1da177e4
LT
190 .bus = &w1_bus_type,
191 .probe = w1_master_probe,
1da177e4
LT
192};
193
7f772ed8 194struct device w1_master_device = {
1da177e4
LT
195 .parent = NULL,
196 .bus = &w1_bus_type,
40f91de6 197 .init_name = "w1 bus master",
7f772ed8 198 .driver = &w1_master_driver,
1da177e4
LT
199 .release = &w1_master_release
200};
201
2c5bfdac 202static struct device_driver w1_slave_driver = {
7f772ed8
EP
203 .name = "w1_slave_driver",
204 .bus = &w1_bus_type,
205};
206
2c5bfdac 207#if 0
7f772ed8
EP
208struct device w1_slave_device = {
209 .parent = NULL,
210 .bus = &w1_bus_type,
40f91de6 211 .init_name = "w1 bus slave",
7f772ed8 212 .driver = &w1_slave_driver,
3aca692d 213 .release = &w1_slave_release
7f772ed8 214};
2c5bfdac 215#endif /* 0 */
7f772ed8 216
060b8845 217static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4 218{
db2d0008 219 struct w1_master *md = dev_to_w1_master(dev);
1da177e4 220 ssize_t count;
7785925d 221
abd52a13 222 mutex_lock(&md->mutex);
1da177e4 223 count = sprintf(buf, "%s\n", md->name);
abd52a13 224 mutex_unlock(&md->mutex);
1da177e4
LT
225
226 return count;
227}
228
2a9d0c17
EP
229static ssize_t w1_master_attribute_store_search(struct device * dev,
230 struct device_attribute *attr,
231 const char * buf, size_t count)
232{
6a158c0d 233 long tmp;
db2d0008 234 struct w1_master *md = dev_to_w1_master(dev);
bf4228f0 235 int ret;
2a9d0c17 236
bf4228f0
JH
237 ret = kstrtol(buf, 0, &tmp);
238 if (ret)
239 return ret;
6a158c0d 240
abd52a13 241 mutex_lock(&md->mutex);
6a158c0d 242 md->search_count = tmp;
abd52a13 243 mutex_unlock(&md->mutex);
af8c7237
DF
244 /* Only wake if it is going to be searching. */
245 if (tmp)
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);
6a158c0d
DF
280
281 return count;
282}
283
284static ssize_t w1_master_attribute_show_pullup(struct device *dev,
285 struct device_attribute *attr,
286 char *buf)
287{
288 struct w1_master *md = dev_to_w1_master(dev);
289 ssize_t count;
290
291 mutex_lock(&md->mutex);
292 count = sprintf(buf, "%d\n", md->enable_pullup);
293 mutex_unlock(&md->mutex);
294
295 return count;
296}
297
060b8845 298static ssize_t w1_master_attribute_show_pointer(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4 299{
db2d0008 300 struct w1_master *md = dev_to_w1_master(dev);
1da177e4 301 ssize_t count;
7785925d 302
abd52a13 303 mutex_lock(&md->mutex);
1da177e4 304 count = sprintf(buf, "0x%p\n", md->bus_master);
abd52a13 305 mutex_unlock(&md->mutex);
1da177e4
LT
306 return count;
307}
308
060b8845 309static ssize_t w1_master_attribute_show_timeout(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
310{
311 ssize_t count;
312 count = sprintf(buf, "%d\n", w1_timeout);
313 return count;
314}
315
c3098356
DK
316static ssize_t w1_master_attribute_show_timeout_us(struct device *dev,
317 struct device_attribute *attr, char *buf)
318{
319 ssize_t count;
320 count = sprintf(buf, "%d\n", w1_timeout_us);
321 return count;
322}
323
a1613056
DF
324static ssize_t w1_master_attribute_store_max_slave_count(struct device *dev,
325 struct device_attribute *attr, const char *buf, size_t count)
326{
a7155f4e 327 int tmp;
a1613056
DF
328 struct w1_master *md = dev_to_w1_master(dev);
329
b9c11a23 330 if (kstrtoint(buf, 0, &tmp) || tmp < 1)
a1613056
DF
331 return -EINVAL;
332
333 mutex_lock(&md->mutex);
334 md->max_slave_count = tmp;
335 /* allow each time the max_slave_count is updated */
336 clear_bit(W1_WARN_MAX_COUNT, &md->flags);
337 mutex_unlock(&md->mutex);
338
339 return count;
340}
341
060b8845 342static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4 343{
db2d0008 344 struct w1_master *md = dev_to_w1_master(dev);
1da177e4 345 ssize_t count;
7785925d 346
abd52a13 347 mutex_lock(&md->mutex);
1da177e4 348 count = sprintf(buf, "%d\n", md->max_slave_count);
abd52a13 349 mutex_unlock(&md->mutex);
1da177e4
LT
350 return count;
351}
352
060b8845 353static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4 354{
db2d0008 355 struct w1_master *md = dev_to_w1_master(dev);
1da177e4 356 ssize_t count;
7785925d 357
abd52a13 358 mutex_lock(&md->mutex);
1da177e4 359 count = sprintf(buf, "%lu\n", md->attempts);
abd52a13 360 mutex_unlock(&md->mutex);
1da177e4
LT
361 return count;
362}
363
060b8845 364static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
1da177e4 365{
db2d0008 366 struct w1_master *md = dev_to_w1_master(dev);
1da177e4 367 ssize_t count;
7785925d 368
abd52a13 369 mutex_lock(&md->mutex);
1da177e4 370 count = sprintf(buf, "%d\n", md->slave_count);
abd52a13 371 mutex_unlock(&md->mutex);
1da177e4
LT
372 return count;
373}
374
9b467411
DF
375static ssize_t w1_master_attribute_show_slaves(struct device *dev,
376 struct device_attribute *attr, char *buf)
1da177e4 377{
db2d0008 378 struct w1_master *md = dev_to_w1_master(dev);
1da177e4 379 int c = PAGE_SIZE;
9fcbbac5
DF
380 struct list_head *ent, *n;
381 struct w1_slave *sl = NULL;
1da177e4 382
9fcbbac5 383 mutex_lock(&md->list_mutex);
1da177e4 384
9fcbbac5
DF
385 list_for_each_safe(ent, n, &md->slist) {
386 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
1da177e4 387
9fcbbac5 388 c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
1da177e4 389 }
9fcbbac5
DF
390 if (!sl)
391 c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
1da177e4 392
9fcbbac5 393 mutex_unlock(&md->list_mutex);
1da177e4
LT
394
395 return PAGE_SIZE - c;
396}
397
9b467411
DF
398static ssize_t w1_master_attribute_show_add(struct device *dev,
399 struct device_attribute *attr, char *buf)
400{
401 int c = PAGE_SIZE;
402 c -= snprintf(buf+PAGE_SIZE - c, c,
403 "write device id xx-xxxxxxxxxxxx to add slave\n");
404 return PAGE_SIZE - c;
405}
406
407static int w1_atoreg_num(struct device *dev, const char *buf, size_t count,
408 struct w1_reg_num *rn)
409{
410 unsigned int family;
411 unsigned long long id;
412 int i;
413 u64 rn64_le;
414
415 /* The CRC value isn't read from the user because the sysfs directory
416 * doesn't include it and most messages from the bus search don't
417 * print it either. It would be unreasonable for the user to then
418 * provide it.
419 */
420 const char *error_msg = "bad slave string format, expecting "
421 "ff-dddddddddddd\n";
422
423 if (buf[2] != '-') {
424 dev_err(dev, "%s", error_msg);
425 return -EINVAL;
426 }
427 i = sscanf(buf, "%02x-%012llx", &family, &id);
428 if (i != 2) {
429 dev_err(dev, "%s", error_msg);
430 return -EINVAL;
431 }
432 rn->family = family;
433 rn->id = id;
434
435 rn64_le = cpu_to_le64(*(u64 *)rn);
436 rn->crc = w1_calc_crc8((u8 *)&rn64_le, 7);
437
438#if 0
439 dev_info(dev, "With CRC device is %02x.%012llx.%02x.\n",
440 rn->family, (unsigned long long)rn->id, rn->crc);
441#endif
442
443 return 0;
444}
445
446/* Searches the slaves in the w1_master and returns a pointer or NULL.
9fcbbac5 447 * Note: must not hold list_mutex
9b467411 448 */
70b34d2e 449struct w1_slave *w1_slave_search_device(struct w1_master *dev,
9b467411
DF
450 struct w1_reg_num *rn)
451{
452 struct w1_slave *sl;
9fcbbac5 453 mutex_lock(&dev->list_mutex);
9b467411
DF
454 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
455 if (sl->reg_num.family == rn->family &&
456 sl->reg_num.id == rn->id &&
457 sl->reg_num.crc == rn->crc) {
9fcbbac5 458 mutex_unlock(&dev->list_mutex);
9b467411
DF
459 return sl;
460 }
461 }
9fcbbac5 462 mutex_unlock(&dev->list_mutex);
9b467411
DF
463 return NULL;
464}
465
466static ssize_t w1_master_attribute_store_add(struct device *dev,
467 struct device_attribute *attr,
468 const char *buf, size_t count)
469{
470 struct w1_master *md = dev_to_w1_master(dev);
471 struct w1_reg_num rn;
472 struct w1_slave *sl;
473 ssize_t result = count;
474
475 if (w1_atoreg_num(dev, buf, count, &rn))
476 return -EINVAL;
477
478 mutex_lock(&md->mutex);
479 sl = w1_slave_search_device(md, &rn);
480 /* It would be nice to do a targeted search one the one-wire bus
481 * for the new device to see if it is out there or not. But the
482 * current search doesn't support that.
483 */
484 if (sl) {
485 dev_info(dev, "Device %s already exists\n", sl->name);
486 result = -EINVAL;
487 } else {
488 w1_attach_slave_device(md, &rn);
489 }
490 mutex_unlock(&md->mutex);
491
492 return result;
493}
494
495static ssize_t w1_master_attribute_show_remove(struct device *dev,
496 struct device_attribute *attr, char *buf)
497{
498 int c = PAGE_SIZE;
499 c -= snprintf(buf+PAGE_SIZE - c, c,
500 "write device id xx-xxxxxxxxxxxx to remove slave\n");
501 return PAGE_SIZE - c;
502}
503
504static ssize_t w1_master_attribute_store_remove(struct device *dev,
505 struct device_attribute *attr,
506 const char *buf, size_t count)
507{
508 struct w1_master *md = dev_to_w1_master(dev);
509 struct w1_reg_num rn;
510 struct w1_slave *sl;
511 ssize_t result = count;
512
513 if (w1_atoreg_num(dev, buf, count, &rn))
514 return -EINVAL;
515
516 mutex_lock(&md->mutex);
517 sl = w1_slave_search_device(md, &rn);
518 if (sl) {
9fcbbac5
DF
519 result = w1_slave_detach(sl);
520 /* refcnt 0 means it was detached in the call */
521 if (result == 0)
522 result = count;
9b467411
DF
523 } else {
524 dev_info(dev, "Device %02x-%012llx doesn't exists\n", rn.family,
525 (unsigned long long)rn.id);
526 result = -EINVAL;
527 }
528 mutex_unlock(&md->mutex);
529
530 return result;
531}
532
7785925d
EP
533#define W1_MASTER_ATTR_RO(_name, _mode) \
534 struct device_attribute w1_master_attribute_##_name = \
535 __ATTR(w1_master_##_name, _mode, \
536 w1_master_attribute_show_##_name, NULL)
537
2a9d0c17
EP
538#define W1_MASTER_ATTR_RW(_name, _mode) \
539 struct device_attribute w1_master_attribute_##_name = \
540 __ATTR(w1_master_##_name, _mode, \
541 w1_master_attribute_show_##_name, \
542 w1_master_attribute_store_##_name)
543
7785925d
EP
544static W1_MASTER_ATTR_RO(name, S_IRUGO);
545static W1_MASTER_ATTR_RO(slaves, S_IRUGO);
546static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);
a1613056 547static W1_MASTER_ATTR_RW(max_slave_count, S_IRUGO | S_IWUSR | S_IWGRP);
7785925d
EP
548static W1_MASTER_ATTR_RO(attempts, S_IRUGO);
549static W1_MASTER_ATTR_RO(timeout, S_IRUGO);
c3098356 550static W1_MASTER_ATTR_RO(timeout_us, S_IRUGO);
7785925d 551static W1_MASTER_ATTR_RO(pointer, S_IRUGO);
12aa4c64
BS
552static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUSR | S_IWGRP);
553static W1_MASTER_ATTR_RW(pullup, S_IRUGO | S_IWUSR | S_IWGRP);
554static W1_MASTER_ATTR_RW(add, S_IRUGO | S_IWUSR | S_IWGRP);
555static W1_MASTER_ATTR_RW(remove, S_IRUGO | S_IWUSR | S_IWGRP);
7785925d
EP
556
557static struct attribute *w1_master_default_attrs[] = {
558 &w1_master_attribute_name.attr,
559 &w1_master_attribute_slaves.attr,
560 &w1_master_attribute_slave_count.attr,
561 &w1_master_attribute_max_slave_count.attr,
562 &w1_master_attribute_attempts.attr,
563 &w1_master_attribute_timeout.attr,
c3098356 564 &w1_master_attribute_timeout_us.attr,
7785925d 565 &w1_master_attribute_pointer.attr,
2a9d0c17 566 &w1_master_attribute_search.attr,
6a158c0d 567 &w1_master_attribute_pullup.attr,
9b467411
DF
568 &w1_master_attribute_add.attr,
569 &w1_master_attribute_remove.attr,
7785925d 570 NULL
1da177e4
LT
571};
572
a890d56a 573static const struct attribute_group w1_master_defattr_group = {
7785925d 574 .attrs = w1_master_default_attrs,
1da177e4
LT
575};
576
7785925d
EP
577int w1_create_master_attributes(struct w1_master *master)
578{
579 return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
580}
581
c30c9b15 582void w1_destroy_master_attributes(struct w1_master *master)
7785925d
EP
583{
584 sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
585}
586
7eff2e7a 587static int w1_uevent(struct device *dev, struct kobj_uevent_env *env)
7f772ed8
EP
588{
589 struct w1_master *md = NULL;
590 struct w1_slave *sl = NULL;
591 char *event_owner, *name;
526be416 592 int err = 0;
7f772ed8
EP
593
594 if (dev->driver == &w1_master_driver) {
595 md = container_of(dev, struct w1_master, dev);
596 event_owner = "master";
597 name = md->name;
598 } else if (dev->driver == &w1_slave_driver) {
599 sl = container_of(dev, struct w1_slave, dev);
600 event_owner = "slave";
601 name = sl->name;
602 } else {
312c004d 603 dev_dbg(dev, "Unknown event.\n");
7f772ed8
EP
604 return -EINVAL;
605 }
606
c6976a4e 607 dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n",
40f91de6 608 event_owner, name, dev_name(dev));
7f772ed8
EP
609
610 if (dev->driver != &w1_slave_driver || !sl)
526be416 611 goto end;
7f772ed8 612
7eff2e7a 613 err = add_uevent_var(env, "W1_FID=%02X", sl->reg_num.family);
7f772ed8 614 if (err)
526be416 615 goto end;
7f772ed8 616
7eff2e7a
KS
617 err = add_uevent_var(env, "W1_SLAVE_ID=%024LX",
618 (unsigned long long)sl->reg_num.id);
526be416
DN
619end:
620 return err;
621}
7f772ed8 622
18d7f891 623static int w1_family_notify(unsigned long action, struct w1_slave *sl)
47eba33a 624{
36c27a65 625 struct w1_family_ops *fops;
47eba33a
GKH
626 int err;
627
36c27a65 628 fops = sl->family->fops;
47eba33a 629
2962aece
HFV
630 if (!fops)
631 return 0;
632
47eba33a
GKH
633 switch (action) {
634 case BUS_NOTIFY_ADD_DEVICE:
47eba33a 635 /* if the family driver needs to initialize something... */
36c27a65
GKH
636 if (fops->add_slave) {
637 err = fops->add_slave(sl);
638 if (err < 0) {
639 dev_err(&sl->dev,
640 "add_slave() call failed. err=%d\n",
641 err);
642 return err;
643 }
644 }
645 if (fops->groups) {
646 err = sysfs_create_groups(&sl->dev.kobj, fops->groups);
647 if (err) {
648 dev_err(&sl->dev,
649 "sysfs group creation failed. err=%d\n",
650 err);
651 return err;
652 }
47eba33a 653 }
2eb79548
JRN
654 if (IS_REACHABLE(CONFIG_HWMON) && fops->chip_info) {
655 struct device *hwmon
656 = hwmon_device_register_with_info(&sl->dev,
657 "w1_slave_temp", sl,
658 fops->chip_info,
659 NULL);
660 if (IS_ERR(hwmon)) {
661 dev_warn(&sl->dev,
662 "could not create hwmon device\n");
663 } else {
664 sl->hwmon = hwmon;
665 }
666 }
47eba33a
GKH
667 break;
668 case BUS_NOTIFY_DEL_DEVICE:
2eb79548
JRN
669 if (IS_REACHABLE(CONFIG_HWMON) && fops->chip_info &&
670 sl->hwmon)
671 hwmon_device_unregister(sl->hwmon);
36c27a65 672 if (fops->remove_slave)
47eba33a 673 sl->family->fops->remove_slave(sl);
36c27a65
GKH
674 if (fops->groups)
675 sysfs_remove_groups(&sl->dev.kobj, fops->groups);
47eba33a
GKH
676 break;
677 }
678 return 0;
679}
680
1da177e4
LT
681static int __w1_attach_slave_device(struct w1_slave *sl)
682{
683 int err;
684
685 sl->dev.parent = &sl->master->dev;
7f772ed8 686 sl->dev.driver = &w1_slave_driver;
1da177e4
LT
687 sl->dev.bus = &w1_bus_type;
688 sl->dev.release = &w1_slave_release;
5b187b3c 689 sl->dev.groups = w1_slave_groups;
fae68031
DM
690 sl->dev.of_node = of_find_matching_node(sl->master->dev.of_node,
691 sl->family->of_match_table);
1da177e4 692
40f91de6 693 dev_set_name(&sl->dev, "%02x-%012llx",
6b729861
EP
694 (unsigned int) sl->reg_num.family,
695 (unsigned long long) sl->reg_num.id);
696 snprintf(&sl->name[0], sizeof(sl->name),
697 "%02x-%012llx",
698 (unsigned int) sl->reg_num.family,
699 (unsigned long long) sl->reg_num.id);
1da177e4 700
c6976a4e 701 dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__,
40f91de6 702 dev_name(&sl->dev), sl);
1da177e4 703
18d7f891
DF
704 /* suppress for w1_family_notify before sending KOBJ_ADD */
705 dev_set_uevent_suppress(&sl->dev, true);
706
1da177e4
LT
707 err = device_register(&sl->dev);
708 if (err < 0) {
709 dev_err(&sl->dev,
6b729861 710 "Device registration [%s] failed. err=%d\n",
40f91de6 711 dev_name(&sl->dev), err);
0ec4eb71 712 put_device(&sl->dev);
1da177e4
LT
713 return err;
714 }
18d7f891 715 w1_family_notify(BUS_NOTIFY_ADD_DEVICE, sl);
1da177e4 716
47eba33a
GKH
717 dev_set_uevent_suppress(&sl->dev, false);
718 kobject_uevent(&sl->dev.kobj, KOBJ_ADD);
1da177e4 719
9fcbbac5 720 mutex_lock(&sl->master->list_mutex);
1da177e4 721 list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
9fcbbac5 722 mutex_unlock(&sl->master->list_mutex);
1da177e4
LT
723
724 return 0;
725}
726
70b34d2e 727int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
1da177e4
LT
728{
729 struct w1_slave *sl;
730 struct w1_family *f;
731 int err;
732 struct w1_netlink_msg msg;
733
dd00cc48 734 sl = kzalloc(sizeof(struct w1_slave), GFP_KERNEL);
1da177e4
LT
735 if (!sl) {
736 dev_err(&dev->dev,
737 "%s: failed to allocate new slave device.\n",
738 __func__);
739 return -ENOMEM;
740 }
741
1da177e4
LT
742
743 sl->owner = THIS_MODULE;
744 sl->master = dev;
bb670937 745 set_bit(W1_SLAVE_ACTIVE, &sl->flags);
1da177e4 746
12003375 747 memset(&msg, 0, sizeof(msg));
1da177e4 748 memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
9fcbbac5
DF
749 atomic_set(&sl->refcnt, 1);
750 atomic_inc(&sl->master->refcnt);
2c927c0c 751 dev->slave_count++;
f6887531
AW
752 dev_info(&dev->dev, "Attaching one wire slave %02x.%012llx crc %02x\n",
753 rn->family, (unsigned long long)rn->id, rn->crc);
1da177e4 754
bc04d76d
HFV
755 /* slave modules need to be loaded in a context with unlocked mutex */
756 mutex_unlock(&dev->mutex);
065c0956 757 request_module("w1-family-0x%02X", rn->family);
bc04d76d 758 mutex_lock(&dev->mutex);
8d7bda51 759
1da177e4
LT
760 spin_lock(&w1_flock);
761 f = w1_family_registered(rn->family);
762 if (!f) {
99c5bfe9 763 f= &w1_default_family;
1da177e4
LT
764 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
765 rn->family, rn->family,
766 (unsigned long long)rn->id, rn->crc);
1da177e4
LT
767 }
768 __w1_family_get(f);
769 spin_unlock(&w1_flock);
770
771 sl->family = f;
772
1da177e4
LT
773 err = __w1_attach_slave_device(sl);
774 if (err < 0) {
775 dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
776 sl->name);
2c927c0c 777 dev->slave_count--;
1da177e4 778 w1_family_put(sl->family);
d2ce4ea1 779 atomic_dec(&sl->master->refcnt);
1da177e4
LT
780 kfree(sl);
781 return err;
782 }
783
784 sl->ttl = dev->slave_ttl;
1da177e4 785
12003375 786 memcpy(msg.id.id, rn, sizeof(msg.id));
1da177e4
LT
787 msg.type = W1_SLAVE_ADD;
788 w1_netlink_send(dev, &msg);
789
790 return 0;
791}
792
9fcbbac5 793int w1_unref_slave(struct w1_slave *sl)
1da177e4 794{
9fcbbac5
DF
795 struct w1_master *dev = sl->master;
796 int refcnt;
797 mutex_lock(&dev->list_mutex);
798 refcnt = atomic_sub_return(1, &sl->refcnt);
799 if (refcnt == 0) {
800 struct w1_netlink_msg msg;
801
802 dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__,
803 sl->name, sl);
804
805 list_del(&sl->w1_slave_entry);
806
807 memset(&msg, 0, sizeof(msg));
808 memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
809 msg.type = W1_SLAVE_REMOVE;
810 w1_netlink_send(sl->master, &msg);
811
18d7f891 812 w1_family_notify(BUS_NOTIFY_DEL_DEVICE, sl);
9fcbbac5
DF
813 device_unregister(&sl->dev);
814 #ifdef DEBUG
815 memset(sl, 0, sizeof(*sl));
816 #endif
817 kfree(sl);
818 }
819 atomic_dec(&dev->refcnt);
820 mutex_unlock(&dev->list_mutex);
821 return refcnt;
822}
1da177e4 823
9fcbbac5
DF
824int w1_slave_detach(struct w1_slave *sl)
825{
826 /* Only detach a slave once as it decreases the refcnt each time. */
827 int destroy_now;
828 mutex_lock(&sl->master->list_mutex);
829 destroy_now = !test_bit(W1_SLAVE_DETACH, &sl->flags);
830 set_bit(W1_SLAVE_DETACH, &sl->flags);
831 mutex_unlock(&sl->master->list_mutex);
832
833 if (destroy_now)
834 destroy_now = !w1_unref_slave(sl);
835 return destroy_now ? 0 : -EBUSY;
1da177e4
LT
836}
837
12003375
EP
838struct w1_master *w1_search_master_id(u32 id)
839{
840 struct w1_master *dev;
841 int found = 0;
842
abd52a13 843 mutex_lock(&w1_mlock);
12003375
EP
844 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
845 if (dev->id == id) {
846 found = 1;
847 atomic_inc(&dev->refcnt);
848 break;
849 }
850 }
abd52a13 851 mutex_unlock(&w1_mlock);
12003375
EP
852
853 return (found)?dev:NULL;
854}
855
856struct w1_slave *w1_search_slave(struct w1_reg_num *id)
857{
858 struct w1_master *dev;
859 struct w1_slave *sl = NULL;
860 int found = 0;
861
abd52a13 862 mutex_lock(&w1_mlock);
12003375 863 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
9fcbbac5 864 mutex_lock(&dev->list_mutex);
12003375
EP
865 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
866 if (sl->reg_num.family == id->family &&
867 sl->reg_num.id == id->id &&
868 sl->reg_num.crc == id->crc) {
869 found = 1;
870 atomic_inc(&dev->refcnt);
871 atomic_inc(&sl->refcnt);
872 break;
873 }
874 }
9fcbbac5 875 mutex_unlock(&dev->list_mutex);
12003375
EP
876
877 if (found)
878 break;
879 }
abd52a13 880 mutex_unlock(&w1_mlock);
12003375
EP
881
882 return (found)?sl:NULL;
883}
884
c30c9b15 885void w1_reconnect_slaves(struct w1_family *f, int attach)
6adf87bd 886{
c30c9b15 887 struct w1_slave *sl, *sln;
6adf87bd
EP
888 struct w1_master *dev;
889
abd52a13 890 mutex_lock(&w1_mlock);
6adf87bd 891 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
c30c9b15
DF
892 dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
893 "for family %02x.\n", dev->name, f->fid);
894 mutex_lock(&dev->mutex);
9fcbbac5 895 mutex_lock(&dev->list_mutex);
c30c9b15
DF
896 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
897 /* If it is a new family, slaves with the default
898 * family driver and are that family will be
899 * connected. If the family is going away, devices
900 * matching that family are reconneced.
901 */
902 if ((attach && sl->family->fid == W1_FAMILY_DEFAULT
903 && sl->reg_num.family == f->fid) ||
904 (!attach && sl->family->fid == f->fid)) {
905 struct w1_reg_num rn;
906
9fcbbac5 907 mutex_unlock(&dev->list_mutex);
c30c9b15 908 memcpy(&rn, &sl->reg_num, sizeof(rn));
9fcbbac5
DF
909 /* If it was already in use let the automatic
910 * scan pick it up again later.
911 */
912 if (!w1_slave_detach(sl))
913 w1_attach_slave_device(dev, &rn);
914 mutex_lock(&dev->list_mutex);
c30c9b15
DF
915 }
916 }
917 dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
918 "has been finished.\n", dev->name);
9fcbbac5 919 mutex_unlock(&dev->list_mutex);
c30c9b15 920 mutex_unlock(&dev->mutex);
6adf87bd 921 }
abd52a13 922 mutex_unlock(&w1_mlock);
6adf87bd
EP
923}
924
963bb101 925void w1_slave_found(struct w1_master *dev, u64 rn)
1da177e4 926{
1da177e4 927 struct w1_slave *sl;
1da177e4 928 struct w1_reg_num *tmp;
0e65f828 929 u64 rn_le = cpu_to_le64(rn);
1da177e4 930
c30c9b15 931 atomic_inc(&dev->refcnt);
7785925d 932
1da177e4
LT
933 tmp = (struct w1_reg_num *) &rn;
934
cd7b28d3
DF
935 sl = w1_slave_search_device(dev, tmp);
936 if (sl) {
bb670937 937 set_bit(W1_SLAVE_ACTIVE, &sl->flags);
cd7b28d3
DF
938 } else {
939 if (rn && tmp->crc == w1_calc_crc8((u8 *)&rn_le, 7))
940 w1_attach_slave_device(dev, tmp);
1da177e4 941 }
7785925d 942
1da177e4
LT
943 atomic_dec(&dev->refcnt);
944}
945
6b729861 946/**
b3be177a
DF
947 * w1_search() - Performs a ROM Search & registers any devices found.
948 * @dev: The master device to search
949 * @search_type: W1_SEARCH to search all devices, or W1_ALARM_SEARCH
950 * to return only devices in the alarmed state
951 * @cb: Function to call when a device is found
952 *
6b729861
EP
953 * The 1-wire search is a simple binary tree search.
954 * For each bit of the address, we read two bits and write one bit.
955 * The bit written will put to sleep all devies that don't match that bit.
956 * When the two reads differ, the direction choice is obvious.
957 * When both bits are 0, we must choose a path to take.
958 * When we can scan all 64 bits without having to choose a path, we are done.
959 *
960 * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
961 *
6b729861 962 */
12003375 963void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
1da177e4 964{
6b729861
EP
965 u64 last_rn, rn, tmp64;
966 int i, slave_count = 0;
967 int last_zero, last_device;
968 int search_bit, desc_bit;
969 u8 triplet_ret = 0;
1da177e4 970
6b729861 971 search_bit = 0;
3c6955e5
DF
972 rn = dev->search_id;
973 last_rn = 0;
6b729861
EP
974 last_device = 0;
975 last_zero = -1;
1da177e4
LT
976
977 desc_bit = 64;
978
6b729861
EP
979 while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
980 last_rn = rn;
1da177e4
LT
981 rn = 0;
982
1da177e4
LT
983 /*
984 * Reset bus and all 1-wire device state machines
985 * so they can respond to our requests.
986 *
987 * Return 0 - device(s) present, 1 - no devices present.
988 */
b02f8bed 989 mutex_lock(&dev->bus_mutex);
1da177e4 990 if (w1_reset_bus(dev)) {
b02f8bed 991 mutex_unlock(&dev->bus_mutex);
2da5bf80 992 dev_dbg(&dev->dev, "No devices present on the wire.\n");
1da177e4
LT
993 break;
994 }
995
c9cbf558
EP
996 /* Do fast search on single slave bus */
997 if (dev->max_slave_count == 1) {
b02f8bed 998 int rv;
c9cbf558 999 w1_write_8(dev, W1_READ_ROM);
b02f8bed
N
1000 rv = w1_read_block(dev, (u8 *)&rn, 8);
1001 mutex_unlock(&dev->bus_mutex);
c9cbf558 1002
b02f8bed 1003 if (rv == 8 && rn)
c9cbf558
EP
1004 cb(dev, rn);
1005
1006 break;
1007 }
1008
6b729861 1009 /* Start the search */
12003375 1010 w1_write_8(dev, search_type);
1da177e4 1011 for (i = 0; i < 64; ++i) {
6b729861
EP
1012 /* Determine the direction/search bit */
1013 if (i == desc_bit)
1014 search_bit = 1; /* took the 0 path last time, so take the 1 path */
1015 else if (i > desc_bit)
1016 search_bit = 0; /* take the 0 path on the next branch */
1da177e4 1017 else
6b729861 1018 search_bit = ((last_rn >> i) & 0x1);
1da177e4 1019
b3be177a 1020 /* Read two bits and write one bit */
6b729861
EP
1021 triplet_ret = w1_triplet(dev, search_bit);
1022
1023 /* quit if no device responded */
1024 if ( (triplet_ret & 0x03) == 0x03 )
1025 break;
1da177e4 1026
6b729861
EP
1027 /* If both directions were valid, and we took the 0 path... */
1028 if (triplet_ret == 0)
1029 last_zero = i;
1da177e4 1030
6b729861
EP
1031 /* extract the direction taken & update the device number */
1032 tmp64 = (triplet_ret >> 2);
1033 rn |= (tmp64 << i);
0d671b27 1034
42105698 1035 if (test_bit(W1_ABORT_SEARCH, &dev->flags)) {
b02f8bed 1036 mutex_unlock(&dev->bus_mutex);
7dc8f527 1037 dev_dbg(&dev->dev, "Abort w1_search\n");
0d671b27
DF
1038 return;
1039 }
6b729861 1040 }
b02f8bed 1041 mutex_unlock(&dev->bus_mutex);
7785925d 1042
6b729861 1043 if ( (triplet_ret & 0x03) != 0x03 ) {
3c6955e5 1044 if ((desc_bit == last_zero) || (last_zero < 0)) {
6b729861 1045 last_device = 1;
3c6955e5
DF
1046 dev->search_id = 0;
1047 } else {
1048 dev->search_id = rn;
1049 }
6b729861 1050 desc_bit = last_zero;
c30c9b15 1051 cb(dev, rn);
6b729861 1052 }
a1613056
DF
1053
1054 if (!last_device && slave_count == dev->max_slave_count &&
1055 !test_bit(W1_WARN_MAX_COUNT, &dev->flags)) {
3c6955e5
DF
1056 /* Only max_slave_count will be scanned in a search,
1057 * but it will start where it left off next search
1058 * until all ids are identified and then it will start
1059 * over. A continued search will report the previous
1060 * last id as the first id (provided it is still on the
1061 * bus).
1062 */
a1613056 1063 dev_info(&dev->dev, "%s: max_slave_count %d reached, "
3c6955e5 1064 "will continue next search.\n", __func__,
a1613056
DF
1065 dev->max_slave_count);
1066 set_bit(W1_WARN_MAX_COUNT, &dev->flags);
1067 }
1da177e4
LT
1068 }
1069}
1070
963bb101
DF
1071void w1_search_process_cb(struct w1_master *dev, u8 search_type,
1072 w1_slave_found_callback cb)
12003375
EP
1073{
1074 struct w1_slave *sl, *sln;
1075
9fcbbac5 1076 mutex_lock(&dev->list_mutex);
12003375 1077 list_for_each_entry(sl, &dev->slist, w1_slave_entry)
bb670937 1078 clear_bit(W1_SLAVE_ACTIVE, &sl->flags);
9fcbbac5 1079 mutex_unlock(&dev->list_mutex);
12003375 1080
963bb101 1081 w1_search_devices(dev, search_type, cb);
12003375 1082
9fcbbac5 1083 mutex_lock(&dev->list_mutex);
12003375 1084 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
9fcbbac5
DF
1085 if (!test_bit(W1_SLAVE_ACTIVE, &sl->flags) && !--sl->ttl) {
1086 mutex_unlock(&dev->list_mutex);
12003375 1087 w1_slave_detach(sl);
9fcbbac5
DF
1088 mutex_lock(&dev->list_mutex);
1089 }
bb670937 1090 else if (test_bit(W1_SLAVE_ACTIVE, &sl->flags))
12003375
EP
1091 sl->ttl = dev->slave_ttl;
1092 }
9fcbbac5 1093 mutex_unlock(&dev->list_mutex);
12003375
EP
1094
1095 if (dev->search_count > 0)
1096 dev->search_count--;
1097}
1098
963bb101
DF
1099static void w1_search_process(struct w1_master *dev, u8 search_type)
1100{
1101 w1_search_process_cb(dev, search_type, w1_slave_found);
1102}
1103
b3be177a
DF
1104/**
1105 * w1_process_callbacks() - execute each dev->async_list callback entry
1106 * @dev: w1_master device
1107 *
a0f10464
AK
1108 * The w1 master list_mutex must be held.
1109 *
b3be177a
DF
1110 * Return: 1 if there were commands to executed 0 otherwise
1111 */
9fcbbac5
DF
1112int w1_process_callbacks(struct w1_master *dev)
1113{
1114 int ret = 0;
1115 struct w1_async_cmd *async_cmd, *async_n;
1116
1117 /* The list can be added to in another thread, loop until it is empty */
1118 while (!list_empty(&dev->async_list)) {
1119 list_for_each_entry_safe(async_cmd, async_n, &dev->async_list,
1120 async_entry) {
1121 /* drop the lock, if it is a search it can take a long
1122 * time */
1123 mutex_unlock(&dev->list_mutex);
1124 async_cmd->cb(dev, async_cmd);
1125 ret = 1;
1126 mutex_lock(&dev->list_mutex);
1127 }
1128 }
1129 return ret;
1130}
1131
1da177e4
LT
1132int w1_process(void *data)
1133{
1134 struct w1_master *dev = (struct w1_master *) data;
3c52e4e6
DF
1135 /* As long as w1_timeout is only set by a module parameter the sleep
1136 * time can be calculated in jiffies once.
1137 */
c3098356
DK
1138 const unsigned long jtime =
1139 usecs_to_jiffies(w1_timeout * 1000000 + w1_timeout_us);
9fcbbac5
DF
1140 /* remainder if it woke up early */
1141 unsigned long jremain = 0;
1da177e4 1142
9fcbbac5
DF
1143 for (;;) {
1144
1145 if (!jremain && dev->search_count) {
01e14d6d
DF
1146 mutex_lock(&dev->mutex);
1147 w1_search_process(dev, W1_SEARCH);
1148 mutex_unlock(&dev->mutex);
1149 }
1150
9fcbbac5
DF
1151 mutex_lock(&dev->list_mutex);
1152 /* Note, w1_process_callback drops the lock while processing,
1153 * but locks it again before returning.
1154 */
1155 if (!w1_process_callbacks(dev) && jremain) {
1156 /* a wake up is either to stop the thread, process
1157 * callbacks, or search, it isn't process callbacks, so
1158 * schedule a search.
1159 */
1160 jremain = 1;
1161 }
1162
3c52e4e6
DF
1163 __set_current_state(TASK_INTERRUPTIBLE);
1164
9fcbbac5
DF
1165 /* hold list_mutex until after interruptible to prevent loosing
1166 * the wakeup signal when async_cmd is added.
1167 */
1168 mutex_unlock(&dev->list_mutex);
1169
3c52e4e6
DF
1170 if (kthread_should_stop())
1171 break;
1172
1173 /* Only sleep when the search is active. */
9fcbbac5
DF
1174 if (dev->search_count) {
1175 if (!jremain)
1176 jremain = jtime;
1177 jremain = schedule_timeout(jremain);
1178 }
3c52e4e6
DF
1179 else
1180 schedule();
1da177e4
LT
1181 }
1182
1183 atomic_dec(&dev->refcnt);
1da177e4
LT
1184
1185 return 0;
1186}
1187
73a98fce 1188static int __init w1_init(void)
1da177e4
LT
1189{
1190 int retval;
1191
fdc9167a 1192 pr_info("Driver for 1-wire Dallas network protocol.\n");
1da177e4 1193
12003375
EP
1194 w1_init_netlink();
1195
1da177e4
LT
1196 retval = bus_register(&w1_bus_type);
1197 if (retval) {
fdc9167a 1198 pr_err("Failed to register bus. err=%d.\n", retval);
1da177e4
LT
1199 goto err_out_exit_init;
1200 }
1201
7f772ed8 1202 retval = driver_register(&w1_master_driver);
1da177e4 1203 if (retval) {
fdc9167a 1204 pr_err("Failed to register master driver. err=%d.\n",
1da177e4
LT
1205 retval);
1206 goto err_out_bus_unregister;
1207 }
1208
7f772ed8
EP
1209 retval = driver_register(&w1_slave_driver);
1210 if (retval) {
fdc9167a 1211 pr_err("Failed to register slave driver. err=%d.\n",
7f772ed8
EP
1212 retval);
1213 goto err_out_master_unregister;
1214 }
1215
1da177e4
LT
1216 return 0;
1217
c30c9b15
DF
1218#if 0
1219/* For undoing the slave register if there was a step after it. */
7f772ed8
EP
1220err_out_slave_unregister:
1221 driver_unregister(&w1_slave_driver);
c30c9b15 1222#endif
7f772ed8
EP
1223
1224err_out_master_unregister:
1225 driver_unregister(&w1_master_driver);
1da177e4
LT
1226
1227err_out_bus_unregister:
1228 bus_unregister(&w1_bus_type);
1229
1230err_out_exit_init:
1231 return retval;
1232}
1233
73a98fce 1234static void __exit w1_fini(void)
1da177e4
LT
1235{
1236 struct w1_master *dev;
1da177e4 1237
c30c9b15 1238 /* Set netlink removal messages and some cleanup */
7785925d 1239 list_for_each_entry(dev, &w1_masters, w1_master_entry)
1da177e4 1240 __w1_remove_master_device(dev);
1da177e4 1241
12003375
EP
1242 w1_fini_netlink();
1243
7f772ed8
EP
1244 driver_unregister(&w1_slave_driver);
1245 driver_unregister(&w1_master_driver);
1da177e4
LT
1246 bus_unregister(&w1_bus_type);
1247}
1248
1249module_init(w1_init);
1250module_exit(w1_fini);
50fa2951
AD
1251
1252MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
1253MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
1254MODULE_LICENSE("GPL");