blob: 67b6d5fb25bc35f7d41e0e17ca4f7ad1168cf12c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Evgeniy Polyakov77859252005-05-20 22:33:25 +04002 * w1.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Evgeniy Polyakova8018762011-08-25 15:59:06 -07004 * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net>
Evgeniy Polyakov77859252005-05-20 22:33:25 +04005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/delay.h>
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/list.h>
27#include <linux/interrupt.h>
28#include <linux/spinlock.h>
29#include <linux/timer.h>
30#include <linux/device.h>
31#include <linux/slab.h>
32#include <linux/sched.h>
Evgeniy Polyakov674a396c2006-02-20 11:15:37 +030033#include <linux/kthread.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080034#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Arun Sharma600634972011-07-26 16:09:06 -070036#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include "w1.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "w1_log.h"
40#include "w1_int.h"
41#include "w1_family.h"
42#include "w1_netlink.h"
43
44MODULE_LICENSE("GPL");
Evgeniy Polyakova8018762011-08-25 15:59:06 -070045MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
47
48static int w1_timeout = 10;
49int w1_max_slave_count = 10;
50int w1_max_slave_ttl = 10;
51
52module_param_named(timeout, w1_timeout, int, 0);
53module_param_named(max_slave_count, w1_max_slave_count, int, 0);
54module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
55
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +040056DEFINE_MUTEX(w1_mlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057LIST_HEAD(w1_masters);
58
David Fries9b467412008-10-15 22:04:43 -070059static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn);
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static int w1_master_match(struct device *dev, struct device_driver *drv)
62{
63 return 1;
64}
65
66static int w1_master_probe(struct device *dev)
67{
68 return -ENODEV;
69}
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static void w1_master_release(struct device *dev)
72{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +040073 struct w1_master *md = dev_to_w1_master(dev);
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +040074
75 dev_dbg(dev, "%s: Releasing %s.\n", __func__, md->name);
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +040076 memset(md, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
77 kfree(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80static void w1_slave_release(struct device *dev)
81{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +040082 struct w1_slave *sl = dev_to_w1_slave(dev);
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +040083
David Fries7dc8f522008-10-15 22:04:58 -070084 dev_dbg(dev, "%s: Releasing %s.\n", __func__, sl->name);
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +040085
86 while (atomic_read(&sl->refcnt)) {
David Fries7dc8f522008-10-15 22:04:58 -070087 dev_dbg(dev, "Waiting for %s to become free: refcnt=%d.\n",
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +040088 sl->name, atomic_read(&sl->refcnt));
89 if (msleep_interruptible(1000))
90 flush_signals(current);
91 }
92
93 w1_family_put(sl->family);
94 sl->master->slave_count--;
95
96 complete(&sl->released);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Greg Kroah-Hartman5b187b32013-08-21 13:24:33 -070099static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400101 struct w1_slave *sl = dev_to_w1_slave(dev);
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +0400102
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400103 return sprintf(buf, "%s\n", sl->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
Greg Kroah-Hartman5b187b32013-08-21 13:24:33 -0700105static DEVICE_ATTR_RO(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Greg Kroah-Hartman5b187b32013-08-21 13:24:33 -0700107static ssize_t id_show(struct device *dev,
David Fries07e00342008-10-15 22:04:50 -0700108 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
David Fries07e00342008-10-15 22:04:50 -0700110 struct w1_slave *sl = dev_to_w1_slave(dev);
111 ssize_t count = sizeof(sl->reg_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
David Fries07e00342008-10-15 22:04:50 -0700113 memcpy(buf, (u8 *)&sl->reg_num, count);
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +0400114 return count;
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400115}
Greg Kroah-Hartman5b187b32013-08-21 13:24:33 -0700116static DEVICE_ATTR_RO(id);
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +0400117
Greg Kroah-Hartman5b187b32013-08-21 13:24:33 -0700118static struct attribute *w1_slave_attrs[] = {
119 &dev_attr_name.attr,
120 &dev_attr_id.attr,
121 NULL,
122};
123ATTRIBUTE_GROUPS(w1_slave);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400124
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +0400125/* Default family */
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300126
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700127static ssize_t rw_write(struct file *filp, struct kobject *kobj,
128 struct bin_attribute *bin_attr, char *buf, loff_t off,
129 size_t count)
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300130{
131 struct w1_slave *sl = kobj_to_w1_slave(kobj);
132
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400133 mutex_lock(&sl->master->mutex);
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300134 if (w1_reset_select_slave(sl)) {
135 count = 0;
136 goto out_up;
137 }
138
139 w1_write_block(sl->master, buf, count);
140
141out_up:
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400142 mutex_unlock(&sl->master->mutex);
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300143 return count;
144}
145
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700146static ssize_t rw_read(struct file *filp, struct kobject *kobj,
147 struct bin_attribute *bin_attr, char *buf, loff_t off,
148 size_t count)
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300149{
150 struct w1_slave *sl = kobj_to_w1_slave(kobj);
151
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400152 mutex_lock(&sl->master->mutex);
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300153 w1_read_block(sl->master, buf, count);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400154 mutex_unlock(&sl->master->mutex);
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300155 return count;
156}
157
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700158static BIN_ATTR_RW(rw, PAGE_SIZE);
159
160static struct bin_attribute *w1_slave_bin_attrs[] = {
161 &bin_attr_rw,
162 NULL,
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300163};
164
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700165static const struct attribute_group w1_slave_default_group = {
166 .bin_attrs = w1_slave_bin_attrs,
167};
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300168
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700169static const struct attribute_group *w1_slave_default_groups[] = {
170 &w1_slave_default_group,
171 NULL,
172};
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300173
174static struct w1_family_ops w1_default_fops = {
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700175 .groups = w1_slave_default_groups,
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300176};
177
178static struct w1_family w1_default_family = {
179 .fops = &w1_default_fops,
180};
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +0400181
Kay Sievers7eff2e72007-08-14 15:15:12 +0200182static int w1_uevent(struct device *dev, struct kobj_uevent_env *env);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static struct bus_type w1_bus_type = {
185 .name = "w1",
186 .match = w1_master_match,
Kay Sievers312c0042005-11-16 09:00:00 +0100187 .uevent = w1_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400190struct device_driver w1_master_driver = {
191 .name = "w1_master_driver",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 .bus = &w1_bus_type,
193 .probe = w1_master_probe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194};
195
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400196struct device w1_master_device = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 .parent = NULL,
198 .bus = &w1_bus_type,
Kay Sievers40f91de2009-01-06 10:44:34 -0800199 .init_name = "w1 bus master",
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400200 .driver = &w1_master_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 .release = &w1_master_release
202};
203
Evgeniy Polyakov2c5bfda2006-04-04 20:35:22 +0400204static struct device_driver w1_slave_driver = {
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400205 .name = "w1_slave_driver",
206 .bus = &w1_bus_type,
207};
208
Evgeniy Polyakov2c5bfda2006-04-04 20:35:22 +0400209#if 0
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400210struct device w1_slave_device = {
211 .parent = NULL,
212 .bus = &w1_bus_type,
Kay Sievers40f91de2009-01-06 10:44:34 -0800213 .init_name = "w1 bus slave",
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400214 .driver = &w1_slave_driver,
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400215 .release = &w1_slave_release
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400216};
Evgeniy Polyakov2c5bfda2006-04-04 20:35:22 +0400217#endif /* 0 */
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400218
Yani Ioannou060b8842005-05-17 06:44:04 -0400219static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400221 struct w1_master *md = dev_to_w1_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 ssize_t count;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400223
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400224 mutex_lock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 count = sprintf(buf, "%s\n", md->name);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400226 mutex_unlock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 return count;
229}
230
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400231static ssize_t w1_master_attribute_store_search(struct device * dev,
232 struct device_attribute *attr,
233 const char * buf, size_t count)
234{
David Fries6a158c02008-10-15 22:04:42 -0700235 long tmp;
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400236 struct w1_master *md = dev_to_w1_master(dev);
Jingoo Hanbf4228f2013-09-11 14:26:03 -0700237 int ret;
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400238
Jingoo Hanbf4228f2013-09-11 14:26:03 -0700239 ret = kstrtol(buf, 0, &tmp);
240 if (ret)
241 return ret;
David Fries6a158c02008-10-15 22:04:42 -0700242
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400243 mutex_lock(&md->mutex);
David Fries6a158c02008-10-15 22:04:42 -0700244 md->search_count = tmp;
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400245 mutex_unlock(&md->mutex);
David Fries3c52e4e2008-10-15 22:04:41 -0700246 wake_up_process(md->thread);
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400247
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{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400255 struct w1_master *md = dev_to_w1_master(dev);
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400256 ssize_t count;
257
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400258 mutex_lock(&md->mutex);
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400259 count = sprintf(buf, "%d\n", md->search_count);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400260 mutex_unlock(&md->mutex);
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400261
262 return count;
263}
264
David Fries6a158c02008-10-15 22:04:42 -0700265static 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);
Jingoo Hanbf4228f2013-09-11 14:26:03 -0700271 int ret;
David Fries6a158c02008-10-15 22:04:42 -0700272
Jingoo Hanbf4228f2013-09-11 14:26:03 -0700273 ret = kstrtol(buf, 0, &tmp);
274 if (ret)
275 return ret;
David Fries6a158c02008-10-15 22:04:42 -0700276
277 mutex_lock(&md->mutex);
278 md->enable_pullup = tmp;
279 mutex_unlock(&md->mutex);
280 wake_up_process(md->thread);
281
282 return count;
283}
284
285static ssize_t w1_master_attribute_show_pullup(struct device *dev,
286 struct device_attribute *attr,
287 char *buf)
288{
289 struct w1_master *md = dev_to_w1_master(dev);
290 ssize_t count;
291
292 mutex_lock(&md->mutex);
293 count = sprintf(buf, "%d\n", md->enable_pullup);
294 mutex_unlock(&md->mutex);
295
296 return count;
297}
298
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
Yani Ioannou060b8842005-05-17 06:44:04 -0400317static 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 -0700318{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400319 struct w1_master *md = dev_to_w1_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 ssize_t count;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400321
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400322 mutex_lock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 count = sprintf(buf, "%d\n", md->max_slave_count);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400324 mutex_unlock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return count;
326}
327
Yani Ioannou060b8842005-05-17 06:44:04 -0400328static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400330 struct w1_master *md = dev_to_w1_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 ssize_t count;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400332
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400333 mutex_lock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 count = sprintf(buf, "%lu\n", md->attempts);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400335 mutex_unlock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return count;
337}
338
Yani Ioannou060b8842005-05-17 06:44:04 -0400339static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400341 struct w1_master *md = dev_to_w1_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 ssize_t count;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400343
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400344 mutex_lock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 count = sprintf(buf, "%d\n", md->slave_count);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400346 mutex_unlock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return count;
348}
349
David Fries9b467412008-10-15 22:04:43 -0700350static ssize_t w1_master_attribute_show_slaves(struct device *dev,
351 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400353 struct w1_master *md = dev_to_w1_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 int c = PAGE_SIZE;
355
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400356 mutex_lock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 if (md->slave_count == 0)
359 c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
360 else {
361 struct list_head *ent, *n;
362 struct w1_slave *sl;
363
364 list_for_each_safe(ent, n, &md->slist) {
365 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
366
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400367 c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
369 }
370
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400371 mutex_unlock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 return PAGE_SIZE - c;
374}
375
David Fries9b467412008-10-15 22:04:43 -0700376static ssize_t w1_master_attribute_show_add(struct device *dev,
377 struct device_attribute *attr, char *buf)
378{
379 int c = PAGE_SIZE;
380 c -= snprintf(buf+PAGE_SIZE - c, c,
381 "write device id xx-xxxxxxxxxxxx to add slave\n");
382 return PAGE_SIZE - c;
383}
384
385static int w1_atoreg_num(struct device *dev, const char *buf, size_t count,
386 struct w1_reg_num *rn)
387{
388 unsigned int family;
389 unsigned long long id;
390 int i;
391 u64 rn64_le;
392
393 /* The CRC value isn't read from the user because the sysfs directory
394 * doesn't include it and most messages from the bus search don't
395 * print it either. It would be unreasonable for the user to then
396 * provide it.
397 */
398 const char *error_msg = "bad slave string format, expecting "
399 "ff-dddddddddddd\n";
400
401 if (buf[2] != '-') {
402 dev_err(dev, "%s", error_msg);
403 return -EINVAL;
404 }
405 i = sscanf(buf, "%02x-%012llx", &family, &id);
406 if (i != 2) {
407 dev_err(dev, "%s", error_msg);
408 return -EINVAL;
409 }
410 rn->family = family;
411 rn->id = id;
412
413 rn64_le = cpu_to_le64(*(u64 *)rn);
414 rn->crc = w1_calc_crc8((u8 *)&rn64_le, 7);
415
416#if 0
417 dev_info(dev, "With CRC device is %02x.%012llx.%02x.\n",
418 rn->family, (unsigned long long)rn->id, rn->crc);
419#endif
420
421 return 0;
422}
423
424/* Searches the slaves in the w1_master and returns a pointer or NULL.
425 * Note: must hold the mutex
426 */
427static struct w1_slave *w1_slave_search_device(struct w1_master *dev,
428 struct w1_reg_num *rn)
429{
430 struct w1_slave *sl;
431 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
432 if (sl->reg_num.family == rn->family &&
433 sl->reg_num.id == rn->id &&
434 sl->reg_num.crc == rn->crc) {
435 return sl;
436 }
437 }
438 return NULL;
439}
440
441static ssize_t w1_master_attribute_store_add(struct device *dev,
442 struct device_attribute *attr,
443 const char *buf, size_t count)
444{
445 struct w1_master *md = dev_to_w1_master(dev);
446 struct w1_reg_num rn;
447 struct w1_slave *sl;
448 ssize_t result = count;
449
450 if (w1_atoreg_num(dev, buf, count, &rn))
451 return -EINVAL;
452
453 mutex_lock(&md->mutex);
454 sl = w1_slave_search_device(md, &rn);
455 /* It would be nice to do a targeted search one the one-wire bus
456 * for the new device to see if it is out there or not. But the
457 * current search doesn't support that.
458 */
459 if (sl) {
460 dev_info(dev, "Device %s already exists\n", sl->name);
461 result = -EINVAL;
462 } else {
463 w1_attach_slave_device(md, &rn);
464 }
465 mutex_unlock(&md->mutex);
466
467 return result;
468}
469
470static ssize_t w1_master_attribute_show_remove(struct device *dev,
471 struct device_attribute *attr, char *buf)
472{
473 int c = PAGE_SIZE;
474 c -= snprintf(buf+PAGE_SIZE - c, c,
475 "write device id xx-xxxxxxxxxxxx to remove slave\n");
476 return PAGE_SIZE - c;
477}
478
479static ssize_t w1_master_attribute_store_remove(struct device *dev,
480 struct device_attribute *attr,
481 const char *buf, size_t count)
482{
483 struct w1_master *md = dev_to_w1_master(dev);
484 struct w1_reg_num rn;
485 struct w1_slave *sl;
486 ssize_t result = count;
487
488 if (w1_atoreg_num(dev, buf, count, &rn))
489 return -EINVAL;
490
491 mutex_lock(&md->mutex);
492 sl = w1_slave_search_device(md, &rn);
493 if (sl) {
494 w1_slave_detach(sl);
495 } else {
496 dev_info(dev, "Device %02x-%012llx doesn't exists\n", rn.family,
497 (unsigned long long)rn.id);
498 result = -EINVAL;
499 }
500 mutex_unlock(&md->mutex);
501
502 return result;
503}
504
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400505#define W1_MASTER_ATTR_RO(_name, _mode) \
506 struct device_attribute w1_master_attribute_##_name = \
507 __ATTR(w1_master_##_name, _mode, \
508 w1_master_attribute_show_##_name, NULL)
509
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400510#define W1_MASTER_ATTR_RW(_name, _mode) \
511 struct device_attribute w1_master_attribute_##_name = \
512 __ATTR(w1_master_##_name, _mode, \
513 w1_master_attribute_show_##_name, \
514 w1_master_attribute_store_##_name)
515
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400516static W1_MASTER_ATTR_RO(name, S_IRUGO);
517static W1_MASTER_ATTR_RO(slaves, S_IRUGO);
518static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);
519static W1_MASTER_ATTR_RO(max_slave_count, S_IRUGO);
520static W1_MASTER_ATTR_RO(attempts, S_IRUGO);
521static W1_MASTER_ATTR_RO(timeout, S_IRUGO);
522static W1_MASTER_ATTR_RO(pointer, S_IRUGO);
Brian Swetland12aa4c62010-10-27 15:34:49 -0700523static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUSR | S_IWGRP);
524static W1_MASTER_ATTR_RW(pullup, S_IRUGO | S_IWUSR | S_IWGRP);
525static W1_MASTER_ATTR_RW(add, S_IRUGO | S_IWUSR | S_IWGRP);
526static W1_MASTER_ATTR_RW(remove, S_IRUGO | S_IWUSR | S_IWGRP);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400527
528static struct attribute *w1_master_default_attrs[] = {
529 &w1_master_attribute_name.attr,
530 &w1_master_attribute_slaves.attr,
531 &w1_master_attribute_slave_count.attr,
532 &w1_master_attribute_max_slave_count.attr,
533 &w1_master_attribute_attempts.attr,
534 &w1_master_attribute_timeout.attr,
535 &w1_master_attribute_pointer.attr,
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400536 &w1_master_attribute_search.attr,
David Fries6a158c02008-10-15 22:04:42 -0700537 &w1_master_attribute_pullup.attr,
David Fries9b467412008-10-15 22:04:43 -0700538 &w1_master_attribute_add.attr,
539 &w1_master_attribute_remove.attr,
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400540 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541};
542
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400543static struct attribute_group w1_master_defattr_group = {
544 .attrs = w1_master_default_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545};
546
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400547int w1_create_master_attributes(struct w1_master *master)
548{
549 return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
550}
551
David Friesc30c9b12008-10-15 22:04:38 -0700552void w1_destroy_master_attributes(struct w1_master *master)
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400553{
554 sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
555}
556
Kay Sievers7eff2e72007-08-14 15:15:12 +0200557static int w1_uevent(struct device *dev, struct kobj_uevent_env *env)
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400558{
559 struct w1_master *md = NULL;
560 struct w1_slave *sl = NULL;
561 char *event_owner, *name;
Devendra Naga526be412012-05-19 07:31:54 +0400562 int err = 0;
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400563
564 if (dev->driver == &w1_master_driver) {
565 md = container_of(dev, struct w1_master, dev);
566 event_owner = "master";
567 name = md->name;
568 } else if (dev->driver == &w1_slave_driver) {
569 sl = container_of(dev, struct w1_slave, dev);
570 event_owner = "slave";
571 name = sl->name;
572 } else {
Kay Sievers312c0042005-11-16 09:00:00 +0100573 dev_dbg(dev, "Unknown event.\n");
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400574 return -EINVAL;
575 }
576
Andrew Mortonc6976a42006-05-14 21:43:50 -0700577 dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n",
Kay Sievers40f91de2009-01-06 10:44:34 -0800578 event_owner, name, dev_name(dev));
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400579
580 if (dev->driver != &w1_slave_driver || !sl)
Devendra Naga526be412012-05-19 07:31:54 +0400581 goto end;
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400582
Kay Sievers7eff2e72007-08-14 15:15:12 +0200583 err = add_uevent_var(env, "W1_FID=%02X", sl->reg_num.family);
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400584 if (err)
Devendra Naga526be412012-05-19 07:31:54 +0400585 goto end;
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400586
Kay Sievers7eff2e72007-08-14 15:15:12 +0200587 err = add_uevent_var(env, "W1_SLAVE_ID=%024LX",
588 (unsigned long long)sl->reg_num.id);
Devendra Naga526be412012-05-19 07:31:54 +0400589end:
590 return err;
591}
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400592
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700593/*
594 * Handle sysfs file creation and removal here, before userspace is told that
595 * the device is added / removed from the system
596 */
597static int w1_bus_notify(struct notifier_block *nb, unsigned long action,
598 void *data)
599{
600 struct device *dev = data;
601 struct w1_slave *sl;
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700602 struct w1_family_ops *fops;
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700603 int err;
604
605 /*
606 * Only care about slave devices at the moment. Yes, we should use a
607 * separate "type" for this, but for now, look at the release function
608 * to know which type it is...
609 */
610 if (dev->release != w1_slave_release)
611 return 0;
612
613 sl = dev_to_w1_slave(dev);
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700614 fops = sl->family->fops;
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700615
Hans-Frieder Vogt2962aec2013-10-06 21:13:35 +0200616 if (!fops)
617 return 0;
618
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700619 switch (action) {
620 case BUS_NOTIFY_ADD_DEVICE:
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700621 /* if the family driver needs to initialize something... */
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700622 if (fops->add_slave) {
623 err = fops->add_slave(sl);
624 if (err < 0) {
625 dev_err(&sl->dev,
626 "add_slave() call failed. err=%d\n",
627 err);
628 return err;
629 }
630 }
631 if (fops->groups) {
632 err = sysfs_create_groups(&sl->dev.kobj, fops->groups);
633 if (err) {
634 dev_err(&sl->dev,
635 "sysfs group creation failed. err=%d\n",
636 err);
637 return err;
638 }
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700639 }
640
641 break;
642 case BUS_NOTIFY_DEL_DEVICE:
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700643 if (fops->remove_slave)
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700644 sl->family->fops->remove_slave(sl);
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700645 if (fops->groups)
646 sysfs_remove_groups(&sl->dev.kobj, fops->groups);
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700647 break;
648 }
649 return 0;
650}
651
652static struct notifier_block w1_bus_nb = {
653 .notifier_call = w1_bus_notify,
654};
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656static int __w1_attach_slave_device(struct w1_slave *sl)
657{
658 int err;
659
660 sl->dev.parent = &sl->master->dev;
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400661 sl->dev.driver = &w1_slave_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 sl->dev.bus = &w1_bus_type;
663 sl->dev.release = &w1_slave_release;
Greg Kroah-Hartman5b187b32013-08-21 13:24:33 -0700664 sl->dev.groups = w1_slave_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Kay Sievers40f91de2009-01-06 10:44:34 -0800666 dev_set_name(&sl->dev, "%02x-%012llx",
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400667 (unsigned int) sl->reg_num.family,
668 (unsigned long long) sl->reg_num.id);
669 snprintf(&sl->name[0], sizeof(sl->name),
670 "%02x-%012llx",
671 (unsigned int) sl->reg_num.family,
672 (unsigned long long) sl->reg_num.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Andrew Mortonc6976a42006-05-14 21:43:50 -0700674 dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__,
Kay Sievers40f91de2009-01-06 10:44:34 -0800675 dev_name(&sl->dev), sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 err = device_register(&sl->dev);
678 if (err < 0) {
679 dev_err(&sl->dev,
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400680 "Device registration [%s] failed. err=%d\n",
Kay Sievers40f91de2009-01-06 10:44:34 -0800681 dev_name(&sl->dev), err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return err;
683 }
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700686 dev_set_uevent_suppress(&sl->dev, false);
687 kobject_uevent(&sl->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
690
691 return 0;
692}
693
694static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
695{
696 struct w1_slave *sl;
697 struct w1_family *f;
698 int err;
699 struct w1_netlink_msg msg;
700
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700701 sl = kzalloc(sizeof(struct w1_slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (!sl) {
703 dev_err(&dev->dev,
704 "%s: failed to allocate new slave device.\n",
705 __func__);
706 return -ENOMEM;
707 }
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 sl->owner = THIS_MODULE;
711 sl->master = dev;
Michal Nazarewiczbb670932013-11-12 15:11:42 -0800712 set_bit(W1_SLAVE_ACTIVE, &sl->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300714 memset(&msg, 0, sizeof(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
716 atomic_set(&sl->refcnt, 0);
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400717 init_completion(&sl->released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Hans-Frieder Vogtbc04d762013-10-06 21:13:40 +0200719 /* slave modules need to be loaded in a context with unlocked mutex */
720 mutex_unlock(&dev->mutex);
Alexander Stein8d7bda52013-05-26 20:06:50 +0200721 request_module("w1-family-0x%0x", rn->family);
Hans-Frieder Vogtbc04d762013-10-06 21:13:40 +0200722 mutex_lock(&dev->mutex);
Alexander Stein8d7bda52013-05-26 20:06:50 +0200723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 spin_lock(&w1_flock);
725 f = w1_family_registered(rn->family);
726 if (!f) {
Evgeniy Polyakov99c5bfe2005-06-04 01:31:26 +0400727 f= &w1_default_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
729 rn->family, rn->family,
730 (unsigned long long)rn->id, rn->crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
732 __w1_family_get(f);
733 spin_unlock(&w1_flock);
734
735 sl->family = f;
736
737
738 err = __w1_attach_slave_device(sl);
739 if (err < 0) {
740 dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
741 sl->name);
742 w1_family_put(sl->family);
743 kfree(sl);
744 return err;
745 }
746
747 sl->ttl = dev->slave_ttl;
748 dev->slave_count++;
749
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300750 memcpy(msg.id.id, rn, sizeof(msg.id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 msg.type = W1_SLAVE_ADD;
752 w1_netlink_send(dev, &msg);
753
754 return 0;
755}
756
David Friesc30c9b12008-10-15 22:04:38 -0700757void w1_slave_detach(struct w1_slave *sl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 struct w1_netlink_msg msg;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400760
Evgeniy Polyakov7c8f5702005-08-11 17:27:50 +0400761 dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__, sl->name, sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400763 list_del(&sl->w1_slave_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300765 memset(&msg, 0, sizeof(msg));
766 memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 msg.type = W1_SLAVE_REMOVE;
768 w1_netlink_send(sl->master, &msg);
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400769
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400770 device_unregister(&sl->dev);
771
772 wait_for_completion(&sl->released);
773 kfree(sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300776struct w1_master *w1_search_master_id(u32 id)
777{
778 struct w1_master *dev;
779 int found = 0;
780
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400781 mutex_lock(&w1_mlock);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300782 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
783 if (dev->id == id) {
784 found = 1;
785 atomic_inc(&dev->refcnt);
786 break;
787 }
788 }
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400789 mutex_unlock(&w1_mlock);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300790
791 return (found)?dev:NULL;
792}
793
794struct w1_slave *w1_search_slave(struct w1_reg_num *id)
795{
796 struct w1_master *dev;
797 struct w1_slave *sl = NULL;
798 int found = 0;
799
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400800 mutex_lock(&w1_mlock);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300801 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400802 mutex_lock(&dev->mutex);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300803 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
804 if (sl->reg_num.family == id->family &&
805 sl->reg_num.id == id->id &&
806 sl->reg_num.crc == id->crc) {
807 found = 1;
808 atomic_inc(&dev->refcnt);
809 atomic_inc(&sl->refcnt);
810 break;
811 }
812 }
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400813 mutex_unlock(&dev->mutex);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300814
815 if (found)
816 break;
817 }
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400818 mutex_unlock(&w1_mlock);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300819
820 return (found)?sl:NULL;
821}
822
David Friesc30c9b12008-10-15 22:04:38 -0700823void w1_reconnect_slaves(struct w1_family *f, int attach)
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400824{
David Friesc30c9b12008-10-15 22:04:38 -0700825 struct w1_slave *sl, *sln;
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400826 struct w1_master *dev;
827
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400828 mutex_lock(&w1_mlock);
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400829 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
David Friesc30c9b12008-10-15 22:04:38 -0700830 dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
831 "for family %02x.\n", dev->name, f->fid);
832 mutex_lock(&dev->mutex);
833 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
834 /* If it is a new family, slaves with the default
835 * family driver and are that family will be
836 * connected. If the family is going away, devices
837 * matching that family are reconneced.
838 */
839 if ((attach && sl->family->fid == W1_FAMILY_DEFAULT
840 && sl->reg_num.family == f->fid) ||
841 (!attach && sl->family->fid == f->fid)) {
842 struct w1_reg_num rn;
843
844 memcpy(&rn, &sl->reg_num, sizeof(rn));
845 w1_slave_detach(sl);
846
847 w1_attach_slave_device(dev, &rn);
848 }
849 }
850 dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
851 "has been finished.\n", dev->name);
852 mutex_unlock(&dev->mutex);
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400853 }
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400854 mutex_unlock(&w1_mlock);
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400855}
856
David Fries963bb102011-05-26 16:26:03 -0700857void w1_slave_found(struct w1_master *dev, u64 rn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 struct w1_slave *sl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 struct w1_reg_num *tmp;
Evgeniy Polyakov0e65f822005-06-30 22:52:38 +0400861 u64 rn_le = cpu_to_le64(rn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
David Friesc30c9b12008-10-15 22:04:38 -0700863 atomic_inc(&dev->refcnt);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 tmp = (struct w1_reg_num *) &rn;
866
David Friescd7b28d2008-10-15 22:04:44 -0700867 sl = w1_slave_search_device(dev, tmp);
868 if (sl) {
Michal Nazarewiczbb670932013-11-12 15:11:42 -0800869 set_bit(W1_SLAVE_ACTIVE, &sl->flags);
David Friescd7b28d2008-10-15 22:04:44 -0700870 } else {
871 if (rn && tmp->crc == w1_calc_crc8((u8 *)&rn_le, 7))
872 w1_attach_slave_device(dev, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 atomic_dec(&dev->refcnt);
876}
877
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400878/**
879 * Performs a ROM Search & registers any devices found.
880 * The 1-wire search is a simple binary tree search.
881 * For each bit of the address, we read two bits and write one bit.
882 * The bit written will put to sleep all devies that don't match that bit.
883 * When the two reads differ, the direction choice is obvious.
884 * When both bits are 0, we must choose a path to take.
885 * When we can scan all 64 bits without having to choose a path, we are done.
886 *
887 * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
888 *
889 * @dev The master device to search
890 * @cb Function to call when a device is found
891 */
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300892void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400894 u64 last_rn, rn, tmp64;
895 int i, slave_count = 0;
896 int last_zero, last_device;
897 int search_bit, desc_bit;
898 u8 triplet_ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400900 search_bit = 0;
901 rn = last_rn = 0;
902 last_device = 0;
903 last_zero = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 desc_bit = 64;
906
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400907 while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
908 last_rn = rn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 rn = 0;
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 /*
912 * Reset bus and all 1-wire device state machines
913 * so they can respond to our requests.
914 *
915 * Return 0 - device(s) present, 1 - no devices present.
916 */
NeilBrownb02f8be2012-05-18 15:59:52 +1000917 mutex_lock(&dev->bus_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (w1_reset_bus(dev)) {
NeilBrownb02f8be2012-05-18 15:59:52 +1000919 mutex_unlock(&dev->bus_mutex);
Evgeniy Polyakov2da5bf82005-08-12 11:46:22 -0700920 dev_dbg(&dev->dev, "No devices present on the wire.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 break;
922 }
923
Evgeniy Polyakovc9cbf552011-12-10 04:36:11 +0300924 /* Do fast search on single slave bus */
925 if (dev->max_slave_count == 1) {
NeilBrownb02f8be2012-05-18 15:59:52 +1000926 int rv;
Evgeniy Polyakovc9cbf552011-12-10 04:36:11 +0300927 w1_write_8(dev, W1_READ_ROM);
NeilBrownb02f8be2012-05-18 15:59:52 +1000928 rv = w1_read_block(dev, (u8 *)&rn, 8);
929 mutex_unlock(&dev->bus_mutex);
Evgeniy Polyakovc9cbf552011-12-10 04:36:11 +0300930
NeilBrownb02f8be2012-05-18 15:59:52 +1000931 if (rv == 8 && rn)
Evgeniy Polyakovc9cbf552011-12-10 04:36:11 +0300932 cb(dev, rn);
933
934 break;
935 }
936
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400937 /* Start the search */
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300938 w1_write_8(dev, search_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 for (i = 0; i < 64; ++i) {
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400940 /* Determine the direction/search bit */
941 if (i == desc_bit)
942 search_bit = 1; /* took the 0 path last time, so take the 1 path */
943 else if (i > desc_bit)
944 search_bit = 0; /* take the 0 path on the next branch */
945 else
946 search_bit = ((last_rn >> i) & 0x1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400948 /** Read two bits and write one bit */
949 triplet_ret = w1_triplet(dev, search_bit);
950
951 /* quit if no device responded */
952 if ( (triplet_ret & 0x03) == 0x03 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 break;
954
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400955 /* If both directions were valid, and we took the 0 path... */
956 if (triplet_ret == 0)
957 last_zero = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400959 /* extract the direction taken & update the device number */
960 tmp64 = (triplet_ret >> 2);
961 rn |= (tmp64 << i);
David Fries0d671b22008-10-15 22:04:39 -0700962
David Fries42105692014-01-15 22:29:13 -0600963 if (test_bit(W1_ABORT_SEARCH, &dev->flags)) {
NeilBrownb02f8be2012-05-18 15:59:52 +1000964 mutex_unlock(&dev->bus_mutex);
David Fries7dc8f522008-10-15 22:04:58 -0700965 dev_dbg(&dev->dev, "Abort w1_search\n");
David Fries0d671b22008-10-15 22:04:39 -0700966 return;
967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
NeilBrownb02f8be2012-05-18 15:59:52 +1000969 mutex_unlock(&dev->bus_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400971 if ( (triplet_ret & 0x03) != 0x03 ) {
972 if ( (desc_bit == last_zero) || (last_zero < 0))
973 last_device = 1;
974 desc_bit = last_zero;
David Friesc30c9b12008-10-15 22:04:38 -0700975 cb(dev, rn);
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
978}
979
David Fries963bb102011-05-26 16:26:03 -0700980void w1_search_process_cb(struct w1_master *dev, u8 search_type,
981 w1_slave_found_callback cb)
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300982{
983 struct w1_slave *sl, *sln;
984
985 list_for_each_entry(sl, &dev->slist, w1_slave_entry)
Michal Nazarewiczbb670932013-11-12 15:11:42 -0800986 clear_bit(W1_SLAVE_ACTIVE, &sl->flags);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300987
David Fries963bb102011-05-26 16:26:03 -0700988 w1_search_devices(dev, search_type, cb);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300989
990 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
Michal Nazarewiczbb670932013-11-12 15:11:42 -0800991 if (!test_bit(W1_SLAVE_ACTIVE, &sl->flags) && !--sl->ttl)
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300992 w1_slave_detach(sl);
Michal Nazarewiczbb670932013-11-12 15:11:42 -0800993 else if (test_bit(W1_SLAVE_ACTIVE, &sl->flags))
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300994 sl->ttl = dev->slave_ttl;
995 }
996
997 if (dev->search_count > 0)
998 dev->search_count--;
999}
1000
David Fries963bb102011-05-26 16:26:03 -07001001static void w1_search_process(struct w1_master *dev, u8 search_type)
1002{
1003 w1_search_process_cb(dev, search_type, w1_slave_found);
1004}
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006int w1_process(void *data)
1007{
1008 struct w1_master *dev = (struct w1_master *) data;
David Fries3c52e4e2008-10-15 22:04:41 -07001009 /* As long as w1_timeout is only set by a module parameter the sleep
1010 * time can be calculated in jiffies once.
1011 */
1012 const unsigned long jtime = msecs_to_jiffies(w1_timeout * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
David Fries3c52e4e2008-10-15 22:04:41 -07001014 while (!kthread_should_stop()) {
David Fries01e14d62008-10-15 22:04:40 -07001015 if (dev->search_count) {
1016 mutex_lock(&dev->mutex);
1017 w1_search_process(dev, W1_SEARCH);
1018 mutex_unlock(&dev->mutex);
1019 }
1020
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001021 try_to_freeze();
David Fries3c52e4e2008-10-15 22:04:41 -07001022 __set_current_state(TASK_INTERRUPTIBLE);
1023
1024 if (kthread_should_stop())
1025 break;
1026
1027 /* Only sleep when the search is active. */
1028 if (dev->search_count)
1029 schedule_timeout(jtime);
1030 else
1031 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033
1034 atomic_dec(&dev->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 return 0;
1037}
1038
Peter Huewe73a98fc2009-12-22 09:38:02 +01001039static int __init w1_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 int retval;
1042
1043 printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
1044
Evgeniy Polyakov12003372006-03-23 19:11:58 +03001045 w1_init_netlink();
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 retval = bus_register(&w1_bus_type);
1048 if (retval) {
1049 printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
1050 goto err_out_exit_init;
1051 }
1052
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -07001053 retval = bus_register_notifier(&w1_bus_type, &w1_bus_nb);
1054 if (retval)
1055 goto err_out_bus_unregister;
1056
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +04001057 retval = driver_register(&w1_master_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (retval) {
1059 printk(KERN_ERR
1060 "Failed to register master driver. err=%d.\n",
1061 retval);
1062 goto err_out_bus_unregister;
1063 }
1064
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +04001065 retval = driver_register(&w1_slave_driver);
1066 if (retval) {
1067 printk(KERN_ERR
Dmitry Artamonowecf19482012-04-02 09:42:22 +04001068 "Failed to register slave driver. err=%d.\n",
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +04001069 retval);
1070 goto err_out_master_unregister;
1071 }
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return 0;
1074
David Friesc30c9b12008-10-15 22:04:38 -07001075#if 0
1076/* For undoing the slave register if there was a step after it. */
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +04001077err_out_slave_unregister:
1078 driver_unregister(&w1_slave_driver);
David Friesc30c9b12008-10-15 22:04:38 -07001079#endif
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +04001080
1081err_out_master_unregister:
1082 driver_unregister(&w1_master_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084err_out_bus_unregister:
1085 bus_unregister(&w1_bus_type);
1086
1087err_out_exit_init:
1088 return retval;
1089}
1090
Peter Huewe73a98fc2009-12-22 09:38:02 +01001091static void __exit w1_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 struct w1_master *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
David Friesc30c9b12008-10-15 22:04:38 -07001095 /* Set netlink removal messages and some cleanup */
Evgeniy Polyakov77859252005-05-20 22:33:25 +04001096 list_for_each_entry(dev, &w1_masters, w1_master_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 __w1_remove_master_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Evgeniy Polyakov12003372006-03-23 19:11:58 +03001099 w1_fini_netlink();
1100
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +04001101 driver_unregister(&w1_slave_driver);
1102 driver_unregister(&w1_master_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 bus_unregister(&w1_bus_type);
1104}
1105
1106module_init(w1_init);
1107module_exit(w1_fini);