blob: 90a3d9338fd282a5bc1c8a678c4a505544b86f70 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Evgeniy Polyakova8018762011-08-25 15:59:06 -07002 * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
Evgeniy Polyakov77859252005-05-20 22:33:25 +04003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
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>
Evgeniy Polyakov674a396c2006-02-20 11:15:37 +030026#include <linux/kthread.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080027#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Arun Sharma600634972011-07-26 16:09:06 -070029#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include "w1.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "w1_log.h"
33#include "w1_int.h"
34#include "w1_family.h"
35#include "w1_netlink.h"
36
37MODULE_LICENSE("GPL");
Evgeniy Polyakova8018762011-08-25 15:59:06 -070038MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070039MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
40
41static int w1_timeout = 10;
Dmitry Khromovc3098352015-05-12 22:29:44 +030042static int w1_timeout_us = 0;
David Friesa1613052014-01-15 22:29:15 -060043int w1_max_slave_count = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044int w1_max_slave_ttl = 10;
45
46module_param_named(timeout, w1_timeout, int, 0);
David Friesb3be1772014-01-15 22:29:25 -060047MODULE_PARM_DESC(timeout, "time in seconds between automatic slave searches");
Dmitry Khromovc3098352015-05-12 22:29:44 +030048module_param_named(timeout_us, w1_timeout_us, int, 0);
Wei Yongjuna46b1952016-08-08 13:52:41 +000049MODULE_PARM_DESC(timeout_us,
50 "time in microseconds between automatic slave searches");
David Friesb3be1772014-01-15 22:29:25 -060051/* A search stops when w1_max_slave_count devices have been found in that
52 * search. The next search will start over and detect the same set of devices
53 * on a static 1-wire bus. Memory is not allocated based on this number, just
54 * on the number of devices known to the kernel. Having a high number does not
55 * consume additional resources. As a special case, if there is only one
56 * device on the network and w1_max_slave_count is set to 1, the device id can
57 * be read directly skipping the normal slower search process.
58 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059module_param_named(max_slave_count, w1_max_slave_count, int, 0);
David Friesb3be1772014-01-15 22:29:25 -060060MODULE_PARM_DESC(max_slave_count,
61 "maximum number of slaves detected in a search");
Linus Torvalds1da177e2005-04-16 15:20:36 -070062module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
David Friesb3be1772014-01-15 22:29:25 -060063MODULE_PARM_DESC(slave_ttl,
64 "Number of searches not seeing a slave before it will be removed");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +040066DEFINE_MUTEX(w1_mlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067LIST_HEAD(w1_masters);
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int w1_master_match(struct device *dev, struct device_driver *drv)
70{
71 return 1;
72}
73
74static int w1_master_probe(struct device *dev)
75{
76 return -ENODEV;
77}
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static void w1_master_release(struct device *dev)
80{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +040081 struct w1_master *md = dev_to_w1_master(dev);
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +040082
83 dev_dbg(dev, "%s: Releasing %s.\n", __func__, md->name);
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +040084 memset(md, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
85 kfree(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88static void w1_slave_release(struct device *dev)
89{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +040090 struct w1_slave *sl = dev_to_w1_slave(dev);
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +040091
David Fries9fcbbac2014-01-15 22:29:18 -060092 dev_dbg(dev, "%s: Releasing %s [%p]\n", __func__, sl->name, sl);
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +040093
94 w1_family_put(sl->family);
95 sl->master->slave_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Greg Kroah-Hartman5b187b32013-08-21 13:24:33 -070098static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400100 struct w1_slave *sl = dev_to_w1_slave(dev);
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +0400101
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400102 return sprintf(buf, "%s\n", sl->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
Greg Kroah-Hartman5b187b32013-08-21 13:24:33 -0700104static DEVICE_ATTR_RO(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Greg Kroah-Hartman5b187b32013-08-21 13:24:33 -0700106static ssize_t id_show(struct device *dev,
David Fries07e00342008-10-15 22:04:50 -0700107 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
David Fries07e00342008-10-15 22:04:50 -0700109 struct w1_slave *sl = dev_to_w1_slave(dev);
110 ssize_t count = sizeof(sl->reg_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
David Fries07e00342008-10-15 22:04:50 -0700112 memcpy(buf, (u8 *)&sl->reg_num, count);
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +0400113 return count;
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400114}
Greg Kroah-Hartman5b187b32013-08-21 13:24:33 -0700115static DEVICE_ATTR_RO(id);
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +0400116
Greg Kroah-Hartman5b187b32013-08-21 13:24:33 -0700117static struct attribute *w1_slave_attrs[] = {
118 &dev_attr_name.attr,
119 &dev_attr_id.attr,
120 NULL,
121};
122ATTRIBUTE_GROUPS(w1_slave);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400123
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +0400124/* Default family */
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300125
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700126static ssize_t rw_write(struct file *filp, struct kobject *kobj,
127 struct bin_attribute *bin_attr, char *buf, loff_t off,
128 size_t count)
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300129{
130 struct w1_slave *sl = kobj_to_w1_slave(kobj);
131
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400132 mutex_lock(&sl->master->mutex);
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300133 if (w1_reset_select_slave(sl)) {
134 count = 0;
135 goto out_up;
136 }
137
138 w1_write_block(sl->master, buf, count);
139
140out_up:
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400141 mutex_unlock(&sl->master->mutex);
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300142 return count;
143}
144
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700145static ssize_t rw_read(struct file *filp, struct kobject *kobj,
146 struct bin_attribute *bin_attr, char *buf, loff_t off,
147 size_t count)
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300148{
149 struct w1_slave *sl = kobj_to_w1_slave(kobj);
150
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400151 mutex_lock(&sl->master->mutex);
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300152 w1_read_block(sl->master, buf, count);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400153 mutex_unlock(&sl->master->mutex);
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300154 return count;
155}
156
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700157static BIN_ATTR_RW(rw, PAGE_SIZE);
158
159static struct bin_attribute *w1_slave_bin_attrs[] = {
160 &bin_attr_rw,
161 NULL,
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300162};
163
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700164static const struct attribute_group w1_slave_default_group = {
165 .bin_attrs = w1_slave_bin_attrs,
166};
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300167
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700168static const struct attribute_group *w1_slave_default_groups[] = {
169 &w1_slave_default_group,
170 NULL,
171};
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300172
173static struct w1_family_ops w1_default_fops = {
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700174 .groups = w1_slave_default_groups,
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300175};
176
177static struct w1_family w1_default_family = {
178 .fops = &w1_default_fops,
179};
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +0400180
Kay Sievers7eff2e72007-08-14 15:15:12 +0200181static int w1_uevent(struct device *dev, struct kobj_uevent_env *env);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183static struct bus_type w1_bus_type = {
184 .name = "w1",
185 .match = w1_master_match,
Kay Sievers312c0042005-11-16 09:00:00 +0100186 .uevent = w1_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400189struct device_driver w1_master_driver = {
190 .name = "w1_master_driver",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 .bus = &w1_bus_type,
192 .probe = w1_master_probe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193};
194
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400195struct device w1_master_device = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 .parent = NULL,
197 .bus = &w1_bus_type,
Kay Sievers40f91de2009-01-06 10:44:34 -0800198 .init_name = "w1 bus master",
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400199 .driver = &w1_master_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 .release = &w1_master_release
201};
202
Evgeniy Polyakov2c5bfda2006-04-04 20:35:22 +0400203static struct device_driver w1_slave_driver = {
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400204 .name = "w1_slave_driver",
205 .bus = &w1_bus_type,
206};
207
Evgeniy Polyakov2c5bfda2006-04-04 20:35:22 +0400208#if 0
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400209struct device w1_slave_device = {
210 .parent = NULL,
211 .bus = &w1_bus_type,
Kay Sievers40f91de2009-01-06 10:44:34 -0800212 .init_name = "w1 bus slave",
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400213 .driver = &w1_slave_driver,
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400214 .release = &w1_slave_release
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400215};
Evgeniy Polyakov2c5bfda2006-04-04 20:35:22 +0400216#endif /* 0 */
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400217
Yani Ioannou060b8842005-05-17 06:44:04 -0400218static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400220 struct w1_master *md = dev_to_w1_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 ssize_t count;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400222
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400223 mutex_lock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 count = sprintf(buf, "%s\n", md->name);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400225 mutex_unlock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 return count;
228}
229
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400230static ssize_t w1_master_attribute_store_search(struct device * dev,
231 struct device_attribute *attr,
232 const char * buf, size_t count)
233{
David Fries6a158c02008-10-15 22:04:42 -0700234 long tmp;
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400235 struct w1_master *md = dev_to_w1_master(dev);
Jingoo Hanbf4228f2013-09-11 14:26:03 -0700236 int ret;
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400237
Jingoo Hanbf4228f2013-09-11 14:26:03 -0700238 ret = kstrtol(buf, 0, &tmp);
239 if (ret)
240 return ret;
David Fries6a158c02008-10-15 22:04:42 -0700241
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400242 mutex_lock(&md->mutex);
David Fries6a158c02008-10-15 22:04:42 -0700243 md->search_count = tmp;
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400244 mutex_unlock(&md->mutex);
David Friesaf8c7232014-01-15 22:29:14 -0600245 /* Only wake if it is going to be searching. */
246 if (tmp)
247 wake_up_process(md->thread);
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400248
249 return count;
250}
251
252static ssize_t w1_master_attribute_show_search(struct device *dev,
253 struct device_attribute *attr,
254 char *buf)
255{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400256 struct w1_master *md = dev_to_w1_master(dev);
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400257 ssize_t count;
258
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400259 mutex_lock(&md->mutex);
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400260 count = sprintf(buf, "%d\n", md->search_count);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400261 mutex_unlock(&md->mutex);
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400262
263 return count;
264}
265
David Fries6a158c02008-10-15 22:04:42 -0700266static ssize_t w1_master_attribute_store_pullup(struct device *dev,
267 struct device_attribute *attr,
268 const char *buf, size_t count)
269{
270 long tmp;
271 struct w1_master *md = dev_to_w1_master(dev);
Jingoo Hanbf4228f2013-09-11 14:26:03 -0700272 int ret;
David Fries6a158c02008-10-15 22:04:42 -0700273
Jingoo Hanbf4228f2013-09-11 14:26:03 -0700274 ret = kstrtol(buf, 0, &tmp);
275 if (ret)
276 return ret;
David Fries6a158c02008-10-15 22:04:42 -0700277
278 mutex_lock(&md->mutex);
279 md->enable_pullup = tmp;
280 mutex_unlock(&md->mutex);
David Fries6a158c02008-10-15 22:04:42 -0700281
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
Yani Ioannou060b8842005-05-17 06:44:04 -0400299static ssize_t w1_master_attribute_show_pointer(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400301 struct w1_master *md = dev_to_w1_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 ssize_t count;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400303
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400304 mutex_lock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 count = sprintf(buf, "0x%p\n", md->bus_master);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400306 mutex_unlock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return count;
308}
309
Yani Ioannou060b8842005-05-17 06:44:04 -0400310static ssize_t w1_master_attribute_show_timeout(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 ssize_t count;
313 count = sprintf(buf, "%d\n", w1_timeout);
314 return count;
315}
316
Dmitry Khromovc3098352015-05-12 22:29:44 +0300317static ssize_t w1_master_attribute_show_timeout_us(struct device *dev,
318 struct device_attribute *attr, char *buf)
319{
320 ssize_t count;
321 count = sprintf(buf, "%d\n", w1_timeout_us);
322 return count;
323}
324
David Friesa1613052014-01-15 22:29:15 -0600325static ssize_t w1_master_attribute_store_max_slave_count(struct device *dev,
326 struct device_attribute *attr, const char *buf, size_t count)
327{
Dan Carpentera7155f42014-02-12 00:07:55 +0300328 int tmp;
David Friesa1613052014-01-15 22:29:15 -0600329 struct w1_master *md = dev_to_w1_master(dev);
330
Dan Carpenterb9c11a22016-04-14 12:35:48 +0300331 if (kstrtoint(buf, 0, &tmp) || tmp < 1)
David Friesa1613052014-01-15 22:29:15 -0600332 return -EINVAL;
333
334 mutex_lock(&md->mutex);
335 md->max_slave_count = tmp;
336 /* allow each time the max_slave_count is updated */
337 clear_bit(W1_WARN_MAX_COUNT, &md->flags);
338 mutex_unlock(&md->mutex);
339
340 return count;
341}
342
Yani Ioannou060b8842005-05-17 06:44:04 -0400343static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400345 struct w1_master *md = dev_to_w1_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 ssize_t count;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400347
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400348 mutex_lock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 count = sprintf(buf, "%d\n", md->max_slave_count);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400350 mutex_unlock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return count;
352}
353
Yani Ioannou060b8842005-05-17 06:44:04 -0400354static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400356 struct w1_master *md = dev_to_w1_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 ssize_t count;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400358
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400359 mutex_lock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 count = sprintf(buf, "%lu\n", md->attempts);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400361 mutex_unlock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return count;
363}
364
Yani Ioannou060b8842005-05-17 06:44:04 -0400365static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400367 struct w1_master *md = dev_to_w1_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 ssize_t count;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400369
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400370 mutex_lock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 count = sprintf(buf, "%d\n", md->slave_count);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400372 mutex_unlock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return count;
374}
375
David Fries9b467412008-10-15 22:04:43 -0700376static ssize_t w1_master_attribute_show_slaves(struct device *dev,
377 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400379 struct w1_master *md = dev_to_w1_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 int c = PAGE_SIZE;
David Fries9fcbbac2014-01-15 22:29:18 -0600381 struct list_head *ent, *n;
382 struct w1_slave *sl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
David Fries9fcbbac2014-01-15 22:29:18 -0600384 mutex_lock(&md->list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
David Fries9fcbbac2014-01-15 22:29:18 -0600386 list_for_each_safe(ent, n, &md->slist) {
387 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
David Fries9fcbbac2014-01-15 22:29:18 -0600389 c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
David Fries9fcbbac2014-01-15 22:29:18 -0600391 if (!sl)
392 c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
David Fries9fcbbac2014-01-15 22:29:18 -0600394 mutex_unlock(&md->list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 return PAGE_SIZE - c;
397}
398
David Fries9b467412008-10-15 22:04:43 -0700399static ssize_t w1_master_attribute_show_add(struct device *dev,
400 struct device_attribute *attr, char *buf)
401{
402 int c = PAGE_SIZE;
403 c -= snprintf(buf+PAGE_SIZE - c, c,
404 "write device id xx-xxxxxxxxxxxx to add slave\n");
405 return PAGE_SIZE - c;
406}
407
408static int w1_atoreg_num(struct device *dev, const char *buf, size_t count,
409 struct w1_reg_num *rn)
410{
411 unsigned int family;
412 unsigned long long id;
413 int i;
414 u64 rn64_le;
415
416 /* The CRC value isn't read from the user because the sysfs directory
417 * doesn't include it and most messages from the bus search don't
418 * print it either. It would be unreasonable for the user to then
419 * provide it.
420 */
421 const char *error_msg = "bad slave string format, expecting "
422 "ff-dddddddddddd\n";
423
424 if (buf[2] != '-') {
425 dev_err(dev, "%s", error_msg);
426 return -EINVAL;
427 }
428 i = sscanf(buf, "%02x-%012llx", &family, &id);
429 if (i != 2) {
430 dev_err(dev, "%s", error_msg);
431 return -EINVAL;
432 }
433 rn->family = family;
434 rn->id = id;
435
436 rn64_le = cpu_to_le64(*(u64 *)rn);
437 rn->crc = w1_calc_crc8((u8 *)&rn64_le, 7);
438
439#if 0
440 dev_info(dev, "With CRC device is %02x.%012llx.%02x.\n",
441 rn->family, (unsigned long long)rn->id, rn->crc);
442#endif
443
444 return 0;
445}
446
447/* Searches the slaves in the w1_master and returns a pointer or NULL.
David Fries9fcbbac2014-01-15 22:29:18 -0600448 * Note: must not hold list_mutex
David Fries9b467412008-10-15 22:04:43 -0700449 */
David Fries70b34d22014-01-15 22:29:17 -0600450struct w1_slave *w1_slave_search_device(struct w1_master *dev,
David Fries9b467412008-10-15 22:04:43 -0700451 struct w1_reg_num *rn)
452{
453 struct w1_slave *sl;
David Fries9fcbbac2014-01-15 22:29:18 -0600454 mutex_lock(&dev->list_mutex);
David Fries9b467412008-10-15 22:04:43 -0700455 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
456 if (sl->reg_num.family == rn->family &&
457 sl->reg_num.id == rn->id &&
458 sl->reg_num.crc == rn->crc) {
David Fries9fcbbac2014-01-15 22:29:18 -0600459 mutex_unlock(&dev->list_mutex);
David Fries9b467412008-10-15 22:04:43 -0700460 return sl;
461 }
462 }
David Fries9fcbbac2014-01-15 22:29:18 -0600463 mutex_unlock(&dev->list_mutex);
David Fries9b467412008-10-15 22:04:43 -0700464 return NULL;
465}
466
467static ssize_t w1_master_attribute_store_add(struct device *dev,
468 struct device_attribute *attr,
469 const char *buf, size_t count)
470{
471 struct w1_master *md = dev_to_w1_master(dev);
472 struct w1_reg_num rn;
473 struct w1_slave *sl;
474 ssize_t result = count;
475
476 if (w1_atoreg_num(dev, buf, count, &rn))
477 return -EINVAL;
478
479 mutex_lock(&md->mutex);
480 sl = w1_slave_search_device(md, &rn);
481 /* It would be nice to do a targeted search one the one-wire bus
482 * for the new device to see if it is out there or not. But the
483 * current search doesn't support that.
484 */
485 if (sl) {
486 dev_info(dev, "Device %s already exists\n", sl->name);
487 result = -EINVAL;
488 } else {
489 w1_attach_slave_device(md, &rn);
490 }
491 mutex_unlock(&md->mutex);
492
493 return result;
494}
495
496static ssize_t w1_master_attribute_show_remove(struct device *dev,
497 struct device_attribute *attr, char *buf)
498{
499 int c = PAGE_SIZE;
500 c -= snprintf(buf+PAGE_SIZE - c, c,
501 "write device id xx-xxxxxxxxxxxx to remove slave\n");
502 return PAGE_SIZE - c;
503}
504
505static ssize_t w1_master_attribute_store_remove(struct device *dev,
506 struct device_attribute *attr,
507 const char *buf, size_t count)
508{
509 struct w1_master *md = dev_to_w1_master(dev);
510 struct w1_reg_num rn;
511 struct w1_slave *sl;
512 ssize_t result = count;
513
514 if (w1_atoreg_num(dev, buf, count, &rn))
515 return -EINVAL;
516
517 mutex_lock(&md->mutex);
518 sl = w1_slave_search_device(md, &rn);
519 if (sl) {
David Fries9fcbbac2014-01-15 22:29:18 -0600520 result = w1_slave_detach(sl);
521 /* refcnt 0 means it was detached in the call */
522 if (result == 0)
523 result = count;
David Fries9b467412008-10-15 22:04:43 -0700524 } else {
525 dev_info(dev, "Device %02x-%012llx doesn't exists\n", rn.family,
526 (unsigned long long)rn.id);
527 result = -EINVAL;
528 }
529 mutex_unlock(&md->mutex);
530
531 return result;
532}
533
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400534#define W1_MASTER_ATTR_RO(_name, _mode) \
535 struct device_attribute w1_master_attribute_##_name = \
536 __ATTR(w1_master_##_name, _mode, \
537 w1_master_attribute_show_##_name, NULL)
538
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400539#define W1_MASTER_ATTR_RW(_name, _mode) \
540 struct device_attribute w1_master_attribute_##_name = \
541 __ATTR(w1_master_##_name, _mode, \
542 w1_master_attribute_show_##_name, \
543 w1_master_attribute_store_##_name)
544
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400545static W1_MASTER_ATTR_RO(name, S_IRUGO);
546static W1_MASTER_ATTR_RO(slaves, S_IRUGO);
547static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);
David Friesa1613052014-01-15 22:29:15 -0600548static W1_MASTER_ATTR_RW(max_slave_count, S_IRUGO | S_IWUSR | S_IWGRP);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400549static W1_MASTER_ATTR_RO(attempts, S_IRUGO);
550static W1_MASTER_ATTR_RO(timeout, S_IRUGO);
Dmitry Khromovc3098352015-05-12 22:29:44 +0300551static W1_MASTER_ATTR_RO(timeout_us, S_IRUGO);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400552static W1_MASTER_ATTR_RO(pointer, S_IRUGO);
Brian Swetland12aa4c62010-10-27 15:34:49 -0700553static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUSR | S_IWGRP);
554static W1_MASTER_ATTR_RW(pullup, S_IRUGO | S_IWUSR | S_IWGRP);
555static W1_MASTER_ATTR_RW(add, S_IRUGO | S_IWUSR | S_IWGRP);
556static W1_MASTER_ATTR_RW(remove, S_IRUGO | S_IWUSR | S_IWGRP);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400557
558static struct attribute *w1_master_default_attrs[] = {
559 &w1_master_attribute_name.attr,
560 &w1_master_attribute_slaves.attr,
561 &w1_master_attribute_slave_count.attr,
562 &w1_master_attribute_max_slave_count.attr,
563 &w1_master_attribute_attempts.attr,
564 &w1_master_attribute_timeout.attr,
Dmitry Khromovc3098352015-05-12 22:29:44 +0300565 &w1_master_attribute_timeout_us.attr,
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400566 &w1_master_attribute_pointer.attr,
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400567 &w1_master_attribute_search.attr,
David Fries6a158c02008-10-15 22:04:42 -0700568 &w1_master_attribute_pullup.attr,
David Fries9b467412008-10-15 22:04:43 -0700569 &w1_master_attribute_add.attr,
570 &w1_master_attribute_remove.attr,
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400571 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572};
573
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400574static struct attribute_group w1_master_defattr_group = {
575 .attrs = w1_master_default_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576};
577
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400578int w1_create_master_attributes(struct w1_master *master)
579{
580 return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
581}
582
David Friesc30c9b12008-10-15 22:04:38 -0700583void w1_destroy_master_attributes(struct w1_master *master)
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400584{
585 sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
586}
587
Kay Sievers7eff2e72007-08-14 15:15:12 +0200588static int w1_uevent(struct device *dev, struct kobj_uevent_env *env)
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400589{
590 struct w1_master *md = NULL;
591 struct w1_slave *sl = NULL;
592 char *event_owner, *name;
Devendra Naga526be412012-05-19 07:31:54 +0400593 int err = 0;
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400594
595 if (dev->driver == &w1_master_driver) {
596 md = container_of(dev, struct w1_master, dev);
597 event_owner = "master";
598 name = md->name;
599 } else if (dev->driver == &w1_slave_driver) {
600 sl = container_of(dev, struct w1_slave, dev);
601 event_owner = "slave";
602 name = sl->name;
603 } else {
Kay Sievers312c0042005-11-16 09:00:00 +0100604 dev_dbg(dev, "Unknown event.\n");
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400605 return -EINVAL;
606 }
607
Andrew Mortonc6976a42006-05-14 21:43:50 -0700608 dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n",
Kay Sievers40f91de2009-01-06 10:44:34 -0800609 event_owner, name, dev_name(dev));
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400610
611 if (dev->driver != &w1_slave_driver || !sl)
Devendra Naga526be412012-05-19 07:31:54 +0400612 goto end;
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400613
Kay Sievers7eff2e72007-08-14 15:15:12 +0200614 err = add_uevent_var(env, "W1_FID=%02X", sl->reg_num.family);
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400615 if (err)
Devendra Naga526be412012-05-19 07:31:54 +0400616 goto end;
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400617
Kay Sievers7eff2e72007-08-14 15:15:12 +0200618 err = add_uevent_var(env, "W1_SLAVE_ID=%024LX",
619 (unsigned long long)sl->reg_num.id);
Devendra Naga526be412012-05-19 07:31:54 +0400620end:
621 return err;
622}
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400623
David Fries18d7f892014-04-16 01:21:21 -0500624static int w1_family_notify(unsigned long action, struct w1_slave *sl)
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700625{
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700626 struct w1_family_ops *fops;
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700627 int err;
628
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700629 fops = sl->family->fops;
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700630
Hans-Frieder Vogt2962aec2013-10-06 21:13:35 +0200631 if (!fops)
632 return 0;
633
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700634 switch (action) {
635 case BUS_NOTIFY_ADD_DEVICE:
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700636 /* if the family driver needs to initialize something... */
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700637 if (fops->add_slave) {
638 err = fops->add_slave(sl);
639 if (err < 0) {
640 dev_err(&sl->dev,
641 "add_slave() call failed. err=%d\n",
642 err);
643 return err;
644 }
645 }
646 if (fops->groups) {
647 err = sysfs_create_groups(&sl->dev.kobj, fops->groups);
648 if (err) {
649 dev_err(&sl->dev,
650 "sysfs group creation failed. err=%d\n",
651 err);
652 return err;
653 }
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700654 }
655
656 break;
657 case BUS_NOTIFY_DEL_DEVICE:
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700658 if (fops->remove_slave)
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700659 sl->family->fops->remove_slave(sl);
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700660 if (fops->groups)
661 sysfs_remove_groups(&sl->dev.kobj, fops->groups);
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700662 break;
663 }
664 return 0;
665}
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667static int __w1_attach_slave_device(struct w1_slave *sl)
668{
669 int err;
670
671 sl->dev.parent = &sl->master->dev;
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400672 sl->dev.driver = &w1_slave_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 sl->dev.bus = &w1_bus_type;
674 sl->dev.release = &w1_slave_release;
Greg Kroah-Hartman5b187b32013-08-21 13:24:33 -0700675 sl->dev.groups = w1_slave_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Kay Sievers40f91de2009-01-06 10:44:34 -0800677 dev_set_name(&sl->dev, "%02x-%012llx",
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400678 (unsigned int) sl->reg_num.family,
679 (unsigned long long) sl->reg_num.id);
680 snprintf(&sl->name[0], sizeof(sl->name),
681 "%02x-%012llx",
682 (unsigned int) sl->reg_num.family,
683 (unsigned long long) sl->reg_num.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Andrew Mortonc6976a42006-05-14 21:43:50 -0700685 dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__,
Kay Sievers40f91de2009-01-06 10:44:34 -0800686 dev_name(&sl->dev), sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
David Fries18d7f892014-04-16 01:21:21 -0500688 /* suppress for w1_family_notify before sending KOBJ_ADD */
689 dev_set_uevent_suppress(&sl->dev, true);
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 err = device_register(&sl->dev);
692 if (err < 0) {
693 dev_err(&sl->dev,
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400694 "Device registration [%s] failed. err=%d\n",
Kay Sievers40f91de2009-01-06 10:44:34 -0800695 dev_name(&sl->dev), err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return err;
697 }
David Fries18d7f892014-04-16 01:21:21 -0500698 w1_family_notify(BUS_NOTIFY_ADD_DEVICE, sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700700 dev_set_uevent_suppress(&sl->dev, false);
701 kobject_uevent(&sl->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
David Fries9fcbbac2014-01-15 22:29:18 -0600703 mutex_lock(&sl->master->list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
David Fries9fcbbac2014-01-15 22:29:18 -0600705 mutex_unlock(&sl->master->list_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 return 0;
708}
709
David Fries70b34d22014-01-15 22:29:17 -0600710int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 struct w1_slave *sl;
713 struct w1_family *f;
714 int err;
715 struct w1_netlink_msg msg;
716
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700717 sl = kzalloc(sizeof(struct w1_slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (!sl) {
719 dev_err(&dev->dev,
720 "%s: failed to allocate new slave device.\n",
721 __func__);
722 return -ENOMEM;
723 }
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 sl->owner = THIS_MODULE;
727 sl->master = dev;
Michal Nazarewiczbb670932013-11-12 15:11:42 -0800728 set_bit(W1_SLAVE_ACTIVE, &sl->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300730 memset(&msg, 0, sizeof(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
David Fries9fcbbac2014-01-15 22:29:18 -0600732 atomic_set(&sl->refcnt, 1);
733 atomic_inc(&sl->master->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Hans-Frieder Vogtbc04d762013-10-06 21:13:40 +0200735 /* slave modules need to be loaded in a context with unlocked mutex */
736 mutex_unlock(&dev->mutex);
NeilBrown4b7e4f82014-11-19 14:04:20 +1100737 request_module("w1-family-0x%02x", rn->family);
Hans-Frieder Vogtbc04d762013-10-06 21:13:40 +0200738 mutex_lock(&dev->mutex);
Alexander Stein8d7bda52013-05-26 20:06:50 +0200739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 spin_lock(&w1_flock);
741 f = w1_family_registered(rn->family);
742 if (!f) {
Evgeniy Polyakov99c5bfe2005-06-04 01:31:26 +0400743 f= &w1_default_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
745 rn->family, rn->family,
746 (unsigned long long)rn->id, rn->crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748 __w1_family_get(f);
749 spin_unlock(&w1_flock);
750
751 sl->family = f;
752
753
754 err = __w1_attach_slave_device(sl);
755 if (err < 0) {
756 dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
757 sl->name);
758 w1_family_put(sl->family);
Maciej S. Szmigierod2ce4ea2017-01-21 23:50:18 +0100759 atomic_dec(&sl->master->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 kfree(sl);
761 return err;
762 }
763
764 sl->ttl = dev->slave_ttl;
765 dev->slave_count++;
766
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300767 memcpy(msg.id.id, rn, sizeof(msg.id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 msg.type = W1_SLAVE_ADD;
769 w1_netlink_send(dev, &msg);
770
771 return 0;
772}
773
David Fries9fcbbac2014-01-15 22:29:18 -0600774int w1_unref_slave(struct w1_slave *sl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
David Fries9fcbbac2014-01-15 22:29:18 -0600776 struct w1_master *dev = sl->master;
777 int refcnt;
778 mutex_lock(&dev->list_mutex);
779 refcnt = atomic_sub_return(1, &sl->refcnt);
780 if (refcnt == 0) {
781 struct w1_netlink_msg msg;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400782
David Fries9fcbbac2014-01-15 22:29:18 -0600783 dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__,
784 sl->name, sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
David Fries9fcbbac2014-01-15 22:29:18 -0600786 list_del(&sl->w1_slave_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
David Fries9fcbbac2014-01-15 22:29:18 -0600788 memset(&msg, 0, sizeof(msg));
789 memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
790 msg.type = W1_SLAVE_REMOVE;
791 w1_netlink_send(sl->master, &msg);
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400792
David Fries18d7f892014-04-16 01:21:21 -0500793 w1_family_notify(BUS_NOTIFY_DEL_DEVICE, sl);
David Fries9fcbbac2014-01-15 22:29:18 -0600794 device_unregister(&sl->dev);
795 #ifdef DEBUG
796 memset(sl, 0, sizeof(*sl));
797 #endif
798 kfree(sl);
799 }
800 atomic_dec(&dev->refcnt);
801 mutex_unlock(&dev->list_mutex);
802 return refcnt;
803}
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400804
David Fries9fcbbac2014-01-15 22:29:18 -0600805int w1_slave_detach(struct w1_slave *sl)
806{
807 /* Only detach a slave once as it decreases the refcnt each time. */
808 int destroy_now;
809 mutex_lock(&sl->master->list_mutex);
810 destroy_now = !test_bit(W1_SLAVE_DETACH, &sl->flags);
811 set_bit(W1_SLAVE_DETACH, &sl->flags);
812 mutex_unlock(&sl->master->list_mutex);
813
814 if (destroy_now)
815 destroy_now = !w1_unref_slave(sl);
816 return destroy_now ? 0 : -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300819struct w1_master *w1_search_master_id(u32 id)
820{
821 struct w1_master *dev;
822 int found = 0;
823
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400824 mutex_lock(&w1_mlock);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300825 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
826 if (dev->id == id) {
827 found = 1;
828 atomic_inc(&dev->refcnt);
829 break;
830 }
831 }
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400832 mutex_unlock(&w1_mlock);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300833
834 return (found)?dev:NULL;
835}
836
837struct w1_slave *w1_search_slave(struct w1_reg_num *id)
838{
839 struct w1_master *dev;
840 struct w1_slave *sl = NULL;
841 int found = 0;
842
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400843 mutex_lock(&w1_mlock);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300844 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
David Fries9fcbbac2014-01-15 22:29:18 -0600845 mutex_lock(&dev->list_mutex);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300846 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
847 if (sl->reg_num.family == id->family &&
848 sl->reg_num.id == id->id &&
849 sl->reg_num.crc == id->crc) {
850 found = 1;
851 atomic_inc(&dev->refcnt);
852 atomic_inc(&sl->refcnt);
853 break;
854 }
855 }
David Fries9fcbbac2014-01-15 22:29:18 -0600856 mutex_unlock(&dev->list_mutex);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300857
858 if (found)
859 break;
860 }
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400861 mutex_unlock(&w1_mlock);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300862
863 return (found)?sl:NULL;
864}
865
David Friesc30c9b12008-10-15 22:04:38 -0700866void w1_reconnect_slaves(struct w1_family *f, int attach)
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400867{
David Friesc30c9b12008-10-15 22:04:38 -0700868 struct w1_slave *sl, *sln;
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400869 struct w1_master *dev;
870
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400871 mutex_lock(&w1_mlock);
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400872 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
David Friesc30c9b12008-10-15 22:04:38 -0700873 dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
874 "for family %02x.\n", dev->name, f->fid);
875 mutex_lock(&dev->mutex);
David Fries9fcbbac2014-01-15 22:29:18 -0600876 mutex_lock(&dev->list_mutex);
David Friesc30c9b12008-10-15 22:04:38 -0700877 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
878 /* If it is a new family, slaves with the default
879 * family driver and are that family will be
880 * connected. If the family is going away, devices
881 * matching that family are reconneced.
882 */
883 if ((attach && sl->family->fid == W1_FAMILY_DEFAULT
884 && sl->reg_num.family == f->fid) ||
885 (!attach && sl->family->fid == f->fid)) {
886 struct w1_reg_num rn;
887
David Fries9fcbbac2014-01-15 22:29:18 -0600888 mutex_unlock(&dev->list_mutex);
David Friesc30c9b12008-10-15 22:04:38 -0700889 memcpy(&rn, &sl->reg_num, sizeof(rn));
David Fries9fcbbac2014-01-15 22:29:18 -0600890 /* If it was already in use let the automatic
891 * scan pick it up again later.
892 */
893 if (!w1_slave_detach(sl))
894 w1_attach_slave_device(dev, &rn);
895 mutex_lock(&dev->list_mutex);
David Friesc30c9b12008-10-15 22:04:38 -0700896 }
897 }
898 dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
899 "has been finished.\n", dev->name);
David Fries9fcbbac2014-01-15 22:29:18 -0600900 mutex_unlock(&dev->list_mutex);
David Friesc30c9b12008-10-15 22:04:38 -0700901 mutex_unlock(&dev->mutex);
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400902 }
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400903 mutex_unlock(&w1_mlock);
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400904}
905
David Fries963bb102011-05-26 16:26:03 -0700906void w1_slave_found(struct w1_master *dev, u64 rn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 struct w1_slave *sl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 struct w1_reg_num *tmp;
Evgeniy Polyakov0e65f822005-06-30 22:52:38 +0400910 u64 rn_le = cpu_to_le64(rn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
David Friesc30c9b12008-10-15 22:04:38 -0700912 atomic_inc(&dev->refcnt);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 tmp = (struct w1_reg_num *) &rn;
915
David Friescd7b28d2008-10-15 22:04:44 -0700916 sl = w1_slave_search_device(dev, tmp);
917 if (sl) {
Michal Nazarewiczbb670932013-11-12 15:11:42 -0800918 set_bit(W1_SLAVE_ACTIVE, &sl->flags);
David Friescd7b28d2008-10-15 22:04:44 -0700919 } else {
920 if (rn && tmp->crc == w1_calc_crc8((u8 *)&rn_le, 7))
921 w1_attach_slave_device(dev, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 atomic_dec(&dev->refcnt);
925}
926
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400927/**
David Friesb3be1772014-01-15 22:29:25 -0600928 * w1_search() - Performs a ROM Search & registers any devices found.
929 * @dev: The master device to search
930 * @search_type: W1_SEARCH to search all devices, or W1_ALARM_SEARCH
931 * to return only devices in the alarmed state
932 * @cb: Function to call when a device is found
933 *
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400934 * The 1-wire search is a simple binary tree search.
935 * For each bit of the address, we read two bits and write one bit.
936 * The bit written will put to sleep all devies that don't match that bit.
937 * When the two reads differ, the direction choice is obvious.
938 * When both bits are 0, we must choose a path to take.
939 * When we can scan all 64 bits without having to choose a path, we are done.
940 *
941 * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
942 *
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400943 */
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300944void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400946 u64 last_rn, rn, tmp64;
947 int i, slave_count = 0;
948 int last_zero, last_device;
949 int search_bit, desc_bit;
950 u8 triplet_ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400952 search_bit = 0;
David Fries3c6955e2014-01-15 22:29:16 -0600953 rn = dev->search_id;
954 last_rn = 0;
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400955 last_device = 0;
956 last_zero = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 desc_bit = 64;
959
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400960 while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
961 last_rn = rn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 rn = 0;
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 /*
965 * Reset bus and all 1-wire device state machines
966 * so they can respond to our requests.
967 *
968 * Return 0 - device(s) present, 1 - no devices present.
969 */
NeilBrownb02f8be2012-05-18 15:59:52 +1000970 mutex_lock(&dev->bus_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (w1_reset_bus(dev)) {
NeilBrownb02f8be2012-05-18 15:59:52 +1000972 mutex_unlock(&dev->bus_mutex);
Evgeniy Polyakov2da5bf82005-08-12 11:46:22 -0700973 dev_dbg(&dev->dev, "No devices present on the wire.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 break;
975 }
976
Evgeniy Polyakovc9cbf552011-12-10 04:36:11 +0300977 /* Do fast search on single slave bus */
978 if (dev->max_slave_count == 1) {
NeilBrownb02f8be2012-05-18 15:59:52 +1000979 int rv;
Evgeniy Polyakovc9cbf552011-12-10 04:36:11 +0300980 w1_write_8(dev, W1_READ_ROM);
NeilBrownb02f8be2012-05-18 15:59:52 +1000981 rv = w1_read_block(dev, (u8 *)&rn, 8);
982 mutex_unlock(&dev->bus_mutex);
Evgeniy Polyakovc9cbf552011-12-10 04:36:11 +0300983
NeilBrownb02f8be2012-05-18 15:59:52 +1000984 if (rv == 8 && rn)
Evgeniy Polyakovc9cbf552011-12-10 04:36:11 +0300985 cb(dev, rn);
986
987 break;
988 }
989
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400990 /* Start the search */
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300991 w1_write_8(dev, search_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 for (i = 0; i < 64; ++i) {
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400993 /* Determine the direction/search bit */
994 if (i == desc_bit)
995 search_bit = 1; /* took the 0 path last time, so take the 1 path */
996 else if (i > desc_bit)
997 search_bit = 0; /* take the 0 path on the next branch */
998 else
999 search_bit = ((last_rn >> i) & 0x1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
David Friesb3be1772014-01-15 22:29:25 -06001001 /* Read two bits and write one bit */
Evgeniy Polyakov6b729862005-06-04 01:30:43 +04001002 triplet_ret = w1_triplet(dev, search_bit);
1003
1004 /* quit if no device responded */
1005 if ( (triplet_ret & 0x03) == 0x03 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 break;
1007
Evgeniy Polyakov6b729862005-06-04 01:30:43 +04001008 /* If both directions were valid, and we took the 0 path... */
1009 if (triplet_ret == 0)
1010 last_zero = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Evgeniy Polyakov6b729862005-06-04 01:30:43 +04001012 /* extract the direction taken & update the device number */
1013 tmp64 = (triplet_ret >> 2);
1014 rn |= (tmp64 << i);
David Fries0d671b22008-10-15 22:04:39 -07001015
David Fries42105692014-01-15 22:29:13 -06001016 if (test_bit(W1_ABORT_SEARCH, &dev->flags)) {
NeilBrownb02f8be2012-05-18 15:59:52 +10001017 mutex_unlock(&dev->bus_mutex);
David Fries7dc8f522008-10-15 22:04:58 -07001018 dev_dbg(&dev->dev, "Abort w1_search\n");
David Fries0d671b22008-10-15 22:04:39 -07001019 return;
1020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
NeilBrownb02f8be2012-05-18 15:59:52 +10001022 mutex_unlock(&dev->bus_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Evgeniy Polyakov6b729862005-06-04 01:30:43 +04001024 if ( (triplet_ret & 0x03) != 0x03 ) {
David Fries3c6955e2014-01-15 22:29:16 -06001025 if ((desc_bit == last_zero) || (last_zero < 0)) {
Evgeniy Polyakov6b729862005-06-04 01:30:43 +04001026 last_device = 1;
David Fries3c6955e2014-01-15 22:29:16 -06001027 dev->search_id = 0;
1028 } else {
1029 dev->search_id = rn;
1030 }
Evgeniy Polyakov6b729862005-06-04 01:30:43 +04001031 desc_bit = last_zero;
David Friesc30c9b12008-10-15 22:04:38 -07001032 cb(dev, rn);
Evgeniy Polyakov6b729862005-06-04 01:30:43 +04001033 }
David Friesa1613052014-01-15 22:29:15 -06001034
1035 if (!last_device && slave_count == dev->max_slave_count &&
1036 !test_bit(W1_WARN_MAX_COUNT, &dev->flags)) {
David Fries3c6955e2014-01-15 22:29:16 -06001037 /* Only max_slave_count will be scanned in a search,
1038 * but it will start where it left off next search
1039 * until all ids are identified and then it will start
1040 * over. A continued search will report the previous
1041 * last id as the first id (provided it is still on the
1042 * bus).
1043 */
David Friesa1613052014-01-15 22:29:15 -06001044 dev_info(&dev->dev, "%s: max_slave_count %d reached, "
David Fries3c6955e2014-01-15 22:29:16 -06001045 "will continue next search.\n", __func__,
David Friesa1613052014-01-15 22:29:15 -06001046 dev->max_slave_count);
1047 set_bit(W1_WARN_MAX_COUNT, &dev->flags);
1048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
1050}
1051
David Fries963bb102011-05-26 16:26:03 -07001052void w1_search_process_cb(struct w1_master *dev, u8 search_type,
1053 w1_slave_found_callback cb)
Evgeniy Polyakov12003372006-03-23 19:11:58 +03001054{
1055 struct w1_slave *sl, *sln;
1056
David Fries9fcbbac2014-01-15 22:29:18 -06001057 mutex_lock(&dev->list_mutex);
Evgeniy Polyakov12003372006-03-23 19:11:58 +03001058 list_for_each_entry(sl, &dev->slist, w1_slave_entry)
Michal Nazarewiczbb670932013-11-12 15:11:42 -08001059 clear_bit(W1_SLAVE_ACTIVE, &sl->flags);
David Fries9fcbbac2014-01-15 22:29:18 -06001060 mutex_unlock(&dev->list_mutex);
Evgeniy Polyakov12003372006-03-23 19:11:58 +03001061
David Fries963bb102011-05-26 16:26:03 -07001062 w1_search_devices(dev, search_type, cb);
Evgeniy Polyakov12003372006-03-23 19:11:58 +03001063
David Fries9fcbbac2014-01-15 22:29:18 -06001064 mutex_lock(&dev->list_mutex);
Evgeniy Polyakov12003372006-03-23 19:11:58 +03001065 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
David Fries9fcbbac2014-01-15 22:29:18 -06001066 if (!test_bit(W1_SLAVE_ACTIVE, &sl->flags) && !--sl->ttl) {
1067 mutex_unlock(&dev->list_mutex);
Evgeniy Polyakov12003372006-03-23 19:11:58 +03001068 w1_slave_detach(sl);
David Fries9fcbbac2014-01-15 22:29:18 -06001069 mutex_lock(&dev->list_mutex);
1070 }
Michal Nazarewiczbb670932013-11-12 15:11:42 -08001071 else if (test_bit(W1_SLAVE_ACTIVE, &sl->flags))
Evgeniy Polyakov12003372006-03-23 19:11:58 +03001072 sl->ttl = dev->slave_ttl;
1073 }
David Fries9fcbbac2014-01-15 22:29:18 -06001074 mutex_unlock(&dev->list_mutex);
Evgeniy Polyakov12003372006-03-23 19:11:58 +03001075
1076 if (dev->search_count > 0)
1077 dev->search_count--;
1078}
1079
David Fries963bb102011-05-26 16:26:03 -07001080static void w1_search_process(struct w1_master *dev, u8 search_type)
1081{
1082 w1_search_process_cb(dev, search_type, w1_slave_found);
1083}
1084
David Friesb3be1772014-01-15 22:29:25 -06001085/**
1086 * w1_process_callbacks() - execute each dev->async_list callback entry
1087 * @dev: w1_master device
1088 *
Alexey Khoroshilova0f10462014-05-07 01:26:04 +04001089 * The w1 master list_mutex must be held.
1090 *
David Friesb3be1772014-01-15 22:29:25 -06001091 * Return: 1 if there were commands to executed 0 otherwise
1092 */
David Fries9fcbbac2014-01-15 22:29:18 -06001093int w1_process_callbacks(struct w1_master *dev)
1094{
1095 int ret = 0;
1096 struct w1_async_cmd *async_cmd, *async_n;
1097
1098 /* The list can be added to in another thread, loop until it is empty */
1099 while (!list_empty(&dev->async_list)) {
1100 list_for_each_entry_safe(async_cmd, async_n, &dev->async_list,
1101 async_entry) {
1102 /* drop the lock, if it is a search it can take a long
1103 * time */
1104 mutex_unlock(&dev->list_mutex);
1105 async_cmd->cb(dev, async_cmd);
1106 ret = 1;
1107 mutex_lock(&dev->list_mutex);
1108 }
1109 }
1110 return ret;
1111}
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113int w1_process(void *data)
1114{
1115 struct w1_master *dev = (struct w1_master *) data;
David Fries3c52e4e2008-10-15 22:04:41 -07001116 /* As long as w1_timeout is only set by a module parameter the sleep
1117 * time can be calculated in jiffies once.
1118 */
Dmitry Khromovc3098352015-05-12 22:29:44 +03001119 const unsigned long jtime =
1120 usecs_to_jiffies(w1_timeout * 1000000 + w1_timeout_us);
David Fries9fcbbac2014-01-15 22:29:18 -06001121 /* remainder if it woke up early */
1122 unsigned long jremain = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
David Fries9fcbbac2014-01-15 22:29:18 -06001124 for (;;) {
1125
1126 if (!jremain && dev->search_count) {
David Fries01e14d62008-10-15 22:04:40 -07001127 mutex_lock(&dev->mutex);
1128 w1_search_process(dev, W1_SEARCH);
1129 mutex_unlock(&dev->mutex);
1130 }
1131
David Fries9fcbbac2014-01-15 22:29:18 -06001132 mutex_lock(&dev->list_mutex);
1133 /* Note, w1_process_callback drops the lock while processing,
1134 * but locks it again before returning.
1135 */
1136 if (!w1_process_callbacks(dev) && jremain) {
1137 /* a wake up is either to stop the thread, process
1138 * callbacks, or search, it isn't process callbacks, so
1139 * schedule a search.
1140 */
1141 jremain = 1;
1142 }
1143
David Fries3c52e4e2008-10-15 22:04:41 -07001144 __set_current_state(TASK_INTERRUPTIBLE);
1145
David Fries9fcbbac2014-01-15 22:29:18 -06001146 /* hold list_mutex until after interruptible to prevent loosing
1147 * the wakeup signal when async_cmd is added.
1148 */
1149 mutex_unlock(&dev->list_mutex);
1150
David Fries3c52e4e2008-10-15 22:04:41 -07001151 if (kthread_should_stop())
1152 break;
1153
1154 /* Only sleep when the search is active. */
David Fries9fcbbac2014-01-15 22:29:18 -06001155 if (dev->search_count) {
1156 if (!jremain)
1157 jremain = jtime;
1158 jremain = schedule_timeout(jremain);
1159 }
David Fries3c52e4e2008-10-15 22:04:41 -07001160 else
1161 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163
1164 atomic_dec(&dev->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 return 0;
1167}
1168
Peter Huewe73a98fc2009-12-22 09:38:02 +01001169static int __init w1_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 int retval;
1172
Fjodor Schelichowfdc91672014-06-19 02:52:00 +02001173 pr_info("Driver for 1-wire Dallas network protocol.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Evgeniy Polyakov12003372006-03-23 19:11:58 +03001175 w1_init_netlink();
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 retval = bus_register(&w1_bus_type);
1178 if (retval) {
Fjodor Schelichowfdc91672014-06-19 02:52:00 +02001179 pr_err("Failed to register bus. err=%d.\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 goto err_out_exit_init;
1181 }
1182
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +04001183 retval = driver_register(&w1_master_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (retval) {
Fjodor Schelichowfdc91672014-06-19 02:52:00 +02001185 pr_err("Failed to register master driver. err=%d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 retval);
1187 goto err_out_bus_unregister;
1188 }
1189
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +04001190 retval = driver_register(&w1_slave_driver);
1191 if (retval) {
Fjodor Schelichowfdc91672014-06-19 02:52:00 +02001192 pr_err("Failed to register slave driver. err=%d.\n",
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +04001193 retval);
1194 goto err_out_master_unregister;
1195 }
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return 0;
1198
David Friesc30c9b12008-10-15 22:04:38 -07001199#if 0
1200/* For undoing the slave register if there was a step after it. */
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +04001201err_out_slave_unregister:
1202 driver_unregister(&w1_slave_driver);
David Friesc30c9b12008-10-15 22:04:38 -07001203#endif
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +04001204
1205err_out_master_unregister:
1206 driver_unregister(&w1_master_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208err_out_bus_unregister:
1209 bus_unregister(&w1_bus_type);
1210
1211err_out_exit_init:
1212 return retval;
1213}
1214
Peter Huewe73a98fc2009-12-22 09:38:02 +01001215static void __exit w1_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
1217 struct w1_master *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
David Friesc30c9b12008-10-15 22:04:38 -07001219 /* Set netlink removal messages and some cleanup */
Evgeniy Polyakov77859252005-05-20 22:33:25 +04001220 list_for_each_entry(dev, &w1_masters, w1_master_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 __w1_remove_master_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Evgeniy Polyakov12003372006-03-23 19:11:58 +03001223 w1_fini_netlink();
1224
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +04001225 driver_unregister(&w1_slave_driver);
1226 driver_unregister(&w1_master_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 bus_unregister(&w1_bus_type);
1228}
1229
1230module_init(w1_init);
1231module_exit(w1_fini);