blob: 97b35cb8b6dae903e77317a00ac81e2257c7a1c3 [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;
David Friesa1613052014-01-15 22:29:15 -060049int w1_max_slave_count = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050int 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static int w1_master_match(struct device *dev, struct device_driver *drv)
60{
61 return 1;
62}
63
64static int w1_master_probe(struct device *dev)
65{
66 return -ENODEV;
67}
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static void w1_master_release(struct device *dev)
70{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +040071 struct w1_master *md = dev_to_w1_master(dev);
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +040072
73 dev_dbg(dev, "%s: Releasing %s.\n", __func__, md->name);
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +040074 memset(md, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
75 kfree(md);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076}
77
78static void w1_slave_release(struct device *dev)
79{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +040080 struct w1_slave *sl = dev_to_w1_slave(dev);
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +040081
David Fries7dc8f522008-10-15 22:04:58 -070082 dev_dbg(dev, "%s: Releasing %s.\n", __func__, sl->name);
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +040083
84 while (atomic_read(&sl->refcnt)) {
David Fries7dc8f522008-10-15 22:04:58 -070085 dev_dbg(dev, "Waiting for %s to become free: refcnt=%d.\n",
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +040086 sl->name, atomic_read(&sl->refcnt));
87 if (msleep_interruptible(1000))
88 flush_signals(current);
89 }
90
91 w1_family_put(sl->family);
92 sl->master->slave_count--;
93
94 complete(&sl->released);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Greg Kroah-Hartman5b187b32013-08-21 13:24:33 -070097static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +040099 struct w1_slave *sl = dev_to_w1_slave(dev);
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +0400100
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400101 return sprintf(buf, "%s\n", sl->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
Greg Kroah-Hartman5b187b32013-08-21 13:24:33 -0700103static DEVICE_ATTR_RO(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Greg Kroah-Hartman5b187b32013-08-21 13:24:33 -0700105static ssize_t id_show(struct device *dev,
David Fries07e00342008-10-15 22:04:50 -0700106 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
David Fries07e00342008-10-15 22:04:50 -0700108 struct w1_slave *sl = dev_to_w1_slave(dev);
109 ssize_t count = sizeof(sl->reg_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
David Fries07e00342008-10-15 22:04:50 -0700111 memcpy(buf, (u8 *)&sl->reg_num, count);
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +0400112 return count;
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400113}
Greg Kroah-Hartman5b187b32013-08-21 13:24:33 -0700114static DEVICE_ATTR_RO(id);
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +0400115
Greg Kroah-Hartman5b187b32013-08-21 13:24:33 -0700116static struct attribute *w1_slave_attrs[] = {
117 &dev_attr_name.attr,
118 &dev_attr_id.attr,
119 NULL,
120};
121ATTRIBUTE_GROUPS(w1_slave);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400122
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +0400123/* Default family */
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300124
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700125static ssize_t rw_write(struct file *filp, struct kobject *kobj,
126 struct bin_attribute *bin_attr, char *buf, loff_t off,
127 size_t count)
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300128{
129 struct w1_slave *sl = kobj_to_w1_slave(kobj);
130
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400131 mutex_lock(&sl->master->mutex);
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300132 if (w1_reset_select_slave(sl)) {
133 count = 0;
134 goto out_up;
135 }
136
137 w1_write_block(sl->master, buf, count);
138
139out_up:
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400140 mutex_unlock(&sl->master->mutex);
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300141 return count;
142}
143
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700144static ssize_t rw_read(struct file *filp, struct kobject *kobj,
145 struct bin_attribute *bin_attr, char *buf, loff_t off,
146 size_t count)
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300147{
148 struct w1_slave *sl = kobj_to_w1_slave(kobj);
149
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400150 mutex_lock(&sl->master->mutex);
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300151 w1_read_block(sl->master, buf, count);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400152 mutex_unlock(&sl->master->mutex);
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300153 return count;
154}
155
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700156static BIN_ATTR_RW(rw, PAGE_SIZE);
157
158static struct bin_attribute *w1_slave_bin_attrs[] = {
159 &bin_attr_rw,
160 NULL,
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300161};
162
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700163static const struct attribute_group w1_slave_default_group = {
164 .bin_attrs = w1_slave_bin_attrs,
165};
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300166
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700167static const struct attribute_group *w1_slave_default_groups[] = {
168 &w1_slave_default_group,
169 NULL,
170};
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300171
172static struct w1_family_ops w1_default_fops = {
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700173 .groups = w1_slave_default_groups,
Evgeniy Polyakovf522d232006-03-23 19:11:58 +0300174};
175
176static struct w1_family w1_default_family = {
177 .fops = &w1_default_fops,
178};
Evgeniy Polyakovd2a4ef62005-08-11 17:27:50 +0400179
Kay Sievers7eff2e72007-08-14 15:15:12 +0200180static int w1_uevent(struct device *dev, struct kobj_uevent_env *env);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static struct bus_type w1_bus_type = {
183 .name = "w1",
184 .match = w1_master_match,
Kay Sievers312c0042005-11-16 09:00:00 +0100185 .uevent = w1_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186};
187
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400188struct device_driver w1_master_driver = {
189 .name = "w1_master_driver",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 .bus = &w1_bus_type,
191 .probe = w1_master_probe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192};
193
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400194struct device w1_master_device = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 .parent = NULL,
196 .bus = &w1_bus_type,
Kay Sievers40f91de2009-01-06 10:44:34 -0800197 .init_name = "w1 bus master",
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400198 .driver = &w1_master_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .release = &w1_master_release
200};
201
Evgeniy Polyakov2c5bfda2006-04-04 20:35:22 +0400202static struct device_driver w1_slave_driver = {
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400203 .name = "w1_slave_driver",
204 .bus = &w1_bus_type,
205};
206
Evgeniy Polyakov2c5bfda2006-04-04 20:35:22 +0400207#if 0
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400208struct device w1_slave_device = {
209 .parent = NULL,
210 .bus = &w1_bus_type,
Kay Sievers40f91de2009-01-06 10:44:34 -0800211 .init_name = "w1 bus slave",
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400212 .driver = &w1_slave_driver,
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400213 .release = &w1_slave_release
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400214};
Evgeniy Polyakov2c5bfda2006-04-04 20:35:22 +0400215#endif /* 0 */
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400216
Yani Ioannou060b8842005-05-17 06:44:04 -0400217static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400219 struct w1_master *md = dev_to_w1_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 ssize_t count;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400221
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400222 mutex_lock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 count = sprintf(buf, "%s\n", md->name);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400224 mutex_unlock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 return count;
227}
228
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400229static ssize_t w1_master_attribute_store_search(struct device * dev,
230 struct device_attribute *attr,
231 const char * buf, size_t count)
232{
David Fries6a158c02008-10-15 22:04:42 -0700233 long tmp;
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400234 struct w1_master *md = dev_to_w1_master(dev);
Jingoo Hanbf4228f2013-09-11 14:26:03 -0700235 int ret;
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400236
Jingoo Hanbf4228f2013-09-11 14:26:03 -0700237 ret = kstrtol(buf, 0, &tmp);
238 if (ret)
239 return ret;
David Fries6a158c02008-10-15 22:04:42 -0700240
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400241 mutex_lock(&md->mutex);
David Fries6a158c02008-10-15 22:04:42 -0700242 md->search_count = tmp;
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400243 mutex_unlock(&md->mutex);
David Friesaf8c7232014-01-15 22:29:14 -0600244 /* Only wake if it is going to be searching. */
245 if (tmp)
246 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
David Friesa1613052014-01-15 22:29:15 -0600317static ssize_t w1_master_attribute_store_max_slave_count(struct device *dev,
318 struct device_attribute *attr, const char *buf, size_t count)
319{
320 long tmp;
321 struct w1_master *md = dev_to_w1_master(dev);
322
323 if (kstrtol(buf, 0, &tmp) == -EINVAL || tmp < 1)
324 return -EINVAL;
325
326 mutex_lock(&md->mutex);
327 md->max_slave_count = tmp;
328 /* allow each time the max_slave_count is updated */
329 clear_bit(W1_WARN_MAX_COUNT, &md->flags);
330 mutex_unlock(&md->mutex);
331
332 return count;
333}
334
Yani Ioannou060b8842005-05-17 06:44:04 -0400335static 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 -0700336{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400337 struct w1_master *md = dev_to_w1_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 ssize_t count;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400339
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400340 mutex_lock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 count = sprintf(buf, "%d\n", md->max_slave_count);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400342 mutex_unlock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return count;
344}
345
Yani Ioannou060b8842005-05-17 06:44:04 -0400346static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400348 struct w1_master *md = dev_to_w1_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 ssize_t count;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400350
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400351 mutex_lock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 count = sprintf(buf, "%lu\n", md->attempts);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400353 mutex_unlock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return count;
355}
356
Yani Ioannou060b8842005-05-17 06:44:04 -0400357static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400359 struct w1_master *md = dev_to_w1_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 ssize_t count;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400361
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400362 mutex_lock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 count = sprintf(buf, "%d\n", md->slave_count);
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400364 mutex_unlock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return count;
366}
367
David Fries9b467412008-10-15 22:04:43 -0700368static ssize_t w1_master_attribute_show_slaves(struct device *dev,
369 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Evgeniy Polyakovdb2d0002005-08-11 17:27:49 +0400371 struct w1_master *md = dev_to_w1_master(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 int c = PAGE_SIZE;
373
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400374 mutex_lock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 if (md->slave_count == 0)
377 c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
378 else {
379 struct list_head *ent, *n;
380 struct w1_slave *sl;
381
382 list_for_each_safe(ent, n, &md->slist) {
383 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
384
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400385 c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387 }
388
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400389 mutex_unlock(&md->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 return PAGE_SIZE - c;
392}
393
David Fries9b467412008-10-15 22:04:43 -0700394static ssize_t w1_master_attribute_show_add(struct device *dev,
395 struct device_attribute *attr, char *buf)
396{
397 int c = PAGE_SIZE;
398 c -= snprintf(buf+PAGE_SIZE - c, c,
399 "write device id xx-xxxxxxxxxxxx to add slave\n");
400 return PAGE_SIZE - c;
401}
402
403static int w1_atoreg_num(struct device *dev, const char *buf, size_t count,
404 struct w1_reg_num *rn)
405{
406 unsigned int family;
407 unsigned long long id;
408 int i;
409 u64 rn64_le;
410
411 /* The CRC value isn't read from the user because the sysfs directory
412 * doesn't include it and most messages from the bus search don't
413 * print it either. It would be unreasonable for the user to then
414 * provide it.
415 */
416 const char *error_msg = "bad slave string format, expecting "
417 "ff-dddddddddddd\n";
418
419 if (buf[2] != '-') {
420 dev_err(dev, "%s", error_msg);
421 return -EINVAL;
422 }
423 i = sscanf(buf, "%02x-%012llx", &family, &id);
424 if (i != 2) {
425 dev_err(dev, "%s", error_msg);
426 return -EINVAL;
427 }
428 rn->family = family;
429 rn->id = id;
430
431 rn64_le = cpu_to_le64(*(u64 *)rn);
432 rn->crc = w1_calc_crc8((u8 *)&rn64_le, 7);
433
434#if 0
435 dev_info(dev, "With CRC device is %02x.%012llx.%02x.\n",
436 rn->family, (unsigned long long)rn->id, rn->crc);
437#endif
438
439 return 0;
440}
441
442/* Searches the slaves in the w1_master and returns a pointer or NULL.
443 * Note: must hold the mutex
444 */
David Fries70b34d22014-01-15 22:29:17 -0600445struct w1_slave *w1_slave_search_device(struct w1_master *dev,
David Fries9b467412008-10-15 22:04:43 -0700446 struct w1_reg_num *rn)
447{
448 struct w1_slave *sl;
449 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
450 if (sl->reg_num.family == rn->family &&
451 sl->reg_num.id == rn->id &&
452 sl->reg_num.crc == rn->crc) {
453 return sl;
454 }
455 }
456 return NULL;
457}
458
459static ssize_t w1_master_attribute_store_add(struct device *dev,
460 struct device_attribute *attr,
461 const char *buf, size_t count)
462{
463 struct w1_master *md = dev_to_w1_master(dev);
464 struct w1_reg_num rn;
465 struct w1_slave *sl;
466 ssize_t result = count;
467
468 if (w1_atoreg_num(dev, buf, count, &rn))
469 return -EINVAL;
470
471 mutex_lock(&md->mutex);
472 sl = w1_slave_search_device(md, &rn);
473 /* It would be nice to do a targeted search one the one-wire bus
474 * for the new device to see if it is out there or not. But the
475 * current search doesn't support that.
476 */
477 if (sl) {
478 dev_info(dev, "Device %s already exists\n", sl->name);
479 result = -EINVAL;
480 } else {
481 w1_attach_slave_device(md, &rn);
482 }
483 mutex_unlock(&md->mutex);
484
485 return result;
486}
487
488static ssize_t w1_master_attribute_show_remove(struct device *dev,
489 struct device_attribute *attr, char *buf)
490{
491 int c = PAGE_SIZE;
492 c -= snprintf(buf+PAGE_SIZE - c, c,
493 "write device id xx-xxxxxxxxxxxx to remove slave\n");
494 return PAGE_SIZE - c;
495}
496
497static ssize_t w1_master_attribute_store_remove(struct device *dev,
498 struct device_attribute *attr,
499 const char *buf, size_t count)
500{
501 struct w1_master *md = dev_to_w1_master(dev);
502 struct w1_reg_num rn;
503 struct w1_slave *sl;
504 ssize_t result = count;
505
506 if (w1_atoreg_num(dev, buf, count, &rn))
507 return -EINVAL;
508
509 mutex_lock(&md->mutex);
510 sl = w1_slave_search_device(md, &rn);
511 if (sl) {
512 w1_slave_detach(sl);
513 } else {
514 dev_info(dev, "Device %02x-%012llx doesn't exists\n", rn.family,
515 (unsigned long long)rn.id);
516 result = -EINVAL;
517 }
518 mutex_unlock(&md->mutex);
519
520 return result;
521}
522
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400523#define W1_MASTER_ATTR_RO(_name, _mode) \
524 struct device_attribute w1_master_attribute_##_name = \
525 __ATTR(w1_master_##_name, _mode, \
526 w1_master_attribute_show_##_name, NULL)
527
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400528#define W1_MASTER_ATTR_RW(_name, _mode) \
529 struct device_attribute w1_master_attribute_##_name = \
530 __ATTR(w1_master_##_name, _mode, \
531 w1_master_attribute_show_##_name, \
532 w1_master_attribute_store_##_name)
533
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400534static W1_MASTER_ATTR_RO(name, S_IRUGO);
535static W1_MASTER_ATTR_RO(slaves, S_IRUGO);
536static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);
David Friesa1613052014-01-15 22:29:15 -0600537static W1_MASTER_ATTR_RW(max_slave_count, S_IRUGO | S_IWUSR | S_IWGRP);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400538static W1_MASTER_ATTR_RO(attempts, S_IRUGO);
539static W1_MASTER_ATTR_RO(timeout, S_IRUGO);
540static W1_MASTER_ATTR_RO(pointer, S_IRUGO);
Brian Swetland12aa4c62010-10-27 15:34:49 -0700541static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUSR | S_IWGRP);
542static W1_MASTER_ATTR_RW(pullup, S_IRUGO | S_IWUSR | S_IWGRP);
543static W1_MASTER_ATTR_RW(add, S_IRUGO | S_IWUSR | S_IWGRP);
544static W1_MASTER_ATTR_RW(remove, S_IRUGO | S_IWUSR | S_IWGRP);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400545
546static struct attribute *w1_master_default_attrs[] = {
547 &w1_master_attribute_name.attr,
548 &w1_master_attribute_slaves.attr,
549 &w1_master_attribute_slave_count.attr,
550 &w1_master_attribute_max_slave_count.attr,
551 &w1_master_attribute_attempts.attr,
552 &w1_master_attribute_timeout.attr,
553 &w1_master_attribute_pointer.attr,
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400554 &w1_master_attribute_search.attr,
David Fries6a158c02008-10-15 22:04:42 -0700555 &w1_master_attribute_pullup.attr,
David Fries9b467412008-10-15 22:04:43 -0700556 &w1_master_attribute_add.attr,
557 &w1_master_attribute_remove.attr,
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400558 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559};
560
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400561static struct attribute_group w1_master_defattr_group = {
562 .attrs = w1_master_default_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563};
564
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400565int w1_create_master_attributes(struct w1_master *master)
566{
567 return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
568}
569
David Friesc30c9b12008-10-15 22:04:38 -0700570void w1_destroy_master_attributes(struct w1_master *master)
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400571{
572 sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
573}
574
Kay Sievers7eff2e72007-08-14 15:15:12 +0200575static int w1_uevent(struct device *dev, struct kobj_uevent_env *env)
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400576{
577 struct w1_master *md = NULL;
578 struct w1_slave *sl = NULL;
579 char *event_owner, *name;
Devendra Naga526be412012-05-19 07:31:54 +0400580 int err = 0;
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400581
582 if (dev->driver == &w1_master_driver) {
583 md = container_of(dev, struct w1_master, dev);
584 event_owner = "master";
585 name = md->name;
586 } else if (dev->driver == &w1_slave_driver) {
587 sl = container_of(dev, struct w1_slave, dev);
588 event_owner = "slave";
589 name = sl->name;
590 } else {
Kay Sievers312c0042005-11-16 09:00:00 +0100591 dev_dbg(dev, "Unknown event.\n");
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400592 return -EINVAL;
593 }
594
Andrew Mortonc6976a42006-05-14 21:43:50 -0700595 dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n",
Kay Sievers40f91de2009-01-06 10:44:34 -0800596 event_owner, name, dev_name(dev));
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400597
598 if (dev->driver != &w1_slave_driver || !sl)
Devendra Naga526be412012-05-19 07:31:54 +0400599 goto end;
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400600
Kay Sievers7eff2e72007-08-14 15:15:12 +0200601 err = add_uevent_var(env, "W1_FID=%02X", sl->reg_num.family);
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400602 if (err)
Devendra Naga526be412012-05-19 07:31:54 +0400603 goto end;
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400604
Kay Sievers7eff2e72007-08-14 15:15:12 +0200605 err = add_uevent_var(env, "W1_SLAVE_ID=%024LX",
606 (unsigned long long)sl->reg_num.id);
Devendra Naga526be412012-05-19 07:31:54 +0400607end:
608 return err;
609}
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400610
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700611/*
612 * Handle sysfs file creation and removal here, before userspace is told that
613 * the device is added / removed from the system
614 */
615static int w1_bus_notify(struct notifier_block *nb, unsigned long action,
616 void *data)
617{
618 struct device *dev = data;
619 struct w1_slave *sl;
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700620 struct w1_family_ops *fops;
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700621 int err;
622
623 /*
624 * Only care about slave devices at the moment. Yes, we should use a
625 * separate "type" for this, but for now, look at the release function
626 * to know which type it is...
627 */
628 if (dev->release != w1_slave_release)
629 return 0;
630
631 sl = dev_to_w1_slave(dev);
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700632 fops = sl->family->fops;
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700633
Hans-Frieder Vogt2962aec2013-10-06 21:13:35 +0200634 if (!fops)
635 return 0;
636
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700637 switch (action) {
638 case BUS_NOTIFY_ADD_DEVICE:
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700639 /* if the family driver needs to initialize something... */
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700640 if (fops->add_slave) {
641 err = fops->add_slave(sl);
642 if (err < 0) {
643 dev_err(&sl->dev,
644 "add_slave() call failed. err=%d\n",
645 err);
646 return err;
647 }
648 }
649 if (fops->groups) {
650 err = sysfs_create_groups(&sl->dev.kobj, fops->groups);
651 if (err) {
652 dev_err(&sl->dev,
653 "sysfs group creation failed. err=%d\n",
654 err);
655 return err;
656 }
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700657 }
658
659 break;
660 case BUS_NOTIFY_DEL_DEVICE:
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700661 if (fops->remove_slave)
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700662 sl->family->fops->remove_slave(sl);
Greg Kroah-Hartman36c27a62013-08-21 15:44:55 -0700663 if (fops->groups)
664 sysfs_remove_groups(&sl->dev.kobj, fops->groups);
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700665 break;
666 }
667 return 0;
668}
669
670static struct notifier_block w1_bus_nb = {
671 .notifier_call = w1_bus_notify,
672};
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674static int __w1_attach_slave_device(struct w1_slave *sl)
675{
676 int err;
677
678 sl->dev.parent = &sl->master->dev;
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400679 sl->dev.driver = &w1_slave_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 sl->dev.bus = &w1_bus_type;
681 sl->dev.release = &w1_slave_release;
Greg Kroah-Hartman5b187b32013-08-21 13:24:33 -0700682 sl->dev.groups = w1_slave_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Kay Sievers40f91de2009-01-06 10:44:34 -0800684 dev_set_name(&sl->dev, "%02x-%012llx",
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400685 (unsigned int) sl->reg_num.family,
686 (unsigned long long) sl->reg_num.id);
687 snprintf(&sl->name[0], sizeof(sl->name),
688 "%02x-%012llx",
689 (unsigned int) sl->reg_num.family,
690 (unsigned long long) sl->reg_num.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Andrew Mortonc6976a42006-05-14 21:43:50 -0700692 dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__,
Kay Sievers40f91de2009-01-06 10:44:34 -0800693 dev_name(&sl->dev), sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 err = device_register(&sl->dev);
696 if (err < 0) {
697 dev_err(&sl->dev,
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400698 "Device registration [%s] failed. err=%d\n",
Kay Sievers40f91de2009-01-06 10:44:34 -0800699 dev_name(&sl->dev), err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return err;
701 }
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -0700704 dev_set_uevent_suppress(&sl->dev, false);
705 kobject_uevent(&sl->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
708
709 return 0;
710}
711
David Fries70b34d22014-01-15 22:29:17 -0600712int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 struct w1_slave *sl;
715 struct w1_family *f;
716 int err;
717 struct w1_netlink_msg msg;
718
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700719 sl = kzalloc(sizeof(struct w1_slave), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (!sl) {
721 dev_err(&dev->dev,
722 "%s: failed to allocate new slave device.\n",
723 __func__);
724 return -ENOMEM;
725 }
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 sl->owner = THIS_MODULE;
729 sl->master = dev;
Michal Nazarewiczbb670932013-11-12 15:11:42 -0800730 set_bit(W1_SLAVE_ACTIVE, &sl->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300732 memset(&msg, 0, sizeof(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
734 atomic_set(&sl->refcnt, 0);
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400735 init_completion(&sl->released);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Hans-Frieder Vogtbc04d762013-10-06 21:13:40 +0200737 /* slave modules need to be loaded in a context with unlocked mutex */
738 mutex_unlock(&dev->mutex);
Alexander Stein8d7bda52013-05-26 20:06:50 +0200739 request_module("w1-family-0x%0x", rn->family);
Hans-Frieder Vogtbc04d762013-10-06 21:13:40 +0200740 mutex_lock(&dev->mutex);
Alexander Stein8d7bda52013-05-26 20:06:50 +0200741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 spin_lock(&w1_flock);
743 f = w1_family_registered(rn->family);
744 if (!f) {
Evgeniy Polyakov99c5bfe2005-06-04 01:31:26 +0400745 f= &w1_default_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
747 rn->family, rn->family,
748 (unsigned long long)rn->id, rn->crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750 __w1_family_get(f);
751 spin_unlock(&w1_flock);
752
753 sl->family = f;
754
755
756 err = __w1_attach_slave_device(sl);
757 if (err < 0) {
758 dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
759 sl->name);
760 w1_family_put(sl->family);
761 kfree(sl);
762 return err;
763 }
764
765 sl->ttl = dev->slave_ttl;
766 dev->slave_count++;
767
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300768 memcpy(msg.id.id, rn, sizeof(msg.id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 msg.type = W1_SLAVE_ADD;
770 w1_netlink_send(dev, &msg);
771
772 return 0;
773}
774
David Friesc30c9b12008-10-15 22:04:38 -0700775void w1_slave_detach(struct w1_slave *sl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 struct w1_netlink_msg msg;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400778
Evgeniy Polyakov7c8f5702005-08-11 17:27:50 +0400779 dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__, sl->name, sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400781 list_del(&sl->w1_slave_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300783 memset(&msg, 0, sizeof(msg));
784 memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 msg.type = W1_SLAVE_REMOVE;
786 w1_netlink_send(sl->master, &msg);
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400787
Evgeniy Polyakov3aca6922005-08-11 17:27:50 +0400788 device_unregister(&sl->dev);
789
790 wait_for_completion(&sl->released);
791 kfree(sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300794struct w1_master *w1_search_master_id(u32 id)
795{
796 struct w1_master *dev;
797 int found = 0;
798
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400799 mutex_lock(&w1_mlock);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300800 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
801 if (dev->id == id) {
802 found = 1;
803 atomic_inc(&dev->refcnt);
804 break;
805 }
806 }
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400807 mutex_unlock(&w1_mlock);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300808
809 return (found)?dev:NULL;
810}
811
812struct w1_slave *w1_search_slave(struct w1_reg_num *id)
813{
814 struct w1_master *dev;
815 struct w1_slave *sl = NULL;
816 int found = 0;
817
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400818 mutex_lock(&w1_mlock);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300819 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400820 mutex_lock(&dev->mutex);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300821 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
822 if (sl->reg_num.family == id->family &&
823 sl->reg_num.id == id->id &&
824 sl->reg_num.crc == id->crc) {
825 found = 1;
826 atomic_inc(&dev->refcnt);
827 atomic_inc(&sl->refcnt);
828 break;
829 }
830 }
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400831 mutex_unlock(&dev->mutex);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300832
833 if (found)
834 break;
835 }
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400836 mutex_unlock(&w1_mlock);
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300837
838 return (found)?sl:NULL;
839}
840
David Friesc30c9b12008-10-15 22:04:38 -0700841void w1_reconnect_slaves(struct w1_family *f, int attach)
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400842{
David Friesc30c9b12008-10-15 22:04:38 -0700843 struct w1_slave *sl, *sln;
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400844 struct w1_master *dev;
845
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400846 mutex_lock(&w1_mlock);
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400847 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
David Friesc30c9b12008-10-15 22:04:38 -0700848 dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
849 "for family %02x.\n", dev->name, f->fid);
850 mutex_lock(&dev->mutex);
851 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
852 /* If it is a new family, slaves with the default
853 * family driver and are that family will be
854 * connected. If the family is going away, devices
855 * matching that family are reconneced.
856 */
857 if ((attach && sl->family->fid == W1_FAMILY_DEFAULT
858 && sl->reg_num.family == f->fid) ||
859 (!attach && sl->family->fid == f->fid)) {
860 struct w1_reg_num rn;
861
862 memcpy(&rn, &sl->reg_num, sizeof(rn));
863 w1_slave_detach(sl);
864
865 w1_attach_slave_device(dev, &rn);
866 }
867 }
868 dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
869 "has been finished.\n", dev->name);
870 mutex_unlock(&dev->mutex);
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400871 }
Evgeniy Polyakovabd52a12006-04-03 12:04:27 +0400872 mutex_unlock(&w1_mlock);
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400873}
874
David Fries963bb102011-05-26 16:26:03 -0700875void w1_slave_found(struct w1_master *dev, u64 rn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 struct w1_slave *sl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 struct w1_reg_num *tmp;
Evgeniy Polyakov0e65f822005-06-30 22:52:38 +0400879 u64 rn_le = cpu_to_le64(rn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
David Friesc30c9b12008-10-15 22:04:38 -0700881 atomic_inc(&dev->refcnt);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 tmp = (struct w1_reg_num *) &rn;
884
David Friescd7b28d2008-10-15 22:04:44 -0700885 sl = w1_slave_search_device(dev, tmp);
886 if (sl) {
Michal Nazarewiczbb670932013-11-12 15:11:42 -0800887 set_bit(W1_SLAVE_ACTIVE, &sl->flags);
David Friescd7b28d2008-10-15 22:04:44 -0700888 } else {
889 if (rn && tmp->crc == w1_calc_crc8((u8 *)&rn_le, 7))
890 w1_attach_slave_device(dev, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 atomic_dec(&dev->refcnt);
894}
895
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400896/**
897 * Performs a ROM Search & registers any devices found.
898 * The 1-wire search is a simple binary tree search.
899 * For each bit of the address, we read two bits and write one bit.
900 * The bit written will put to sleep all devies that don't match that bit.
901 * When the two reads differ, the direction choice is obvious.
902 * When both bits are 0, we must choose a path to take.
903 * When we can scan all 64 bits without having to choose a path, we are done.
904 *
905 * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
906 *
907 * @dev The master device to search
908 * @cb Function to call when a device is found
909 */
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300910void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400912 u64 last_rn, rn, tmp64;
913 int i, slave_count = 0;
914 int last_zero, last_device;
915 int search_bit, desc_bit;
916 u8 triplet_ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400918 search_bit = 0;
David Fries3c6955e2014-01-15 22:29:16 -0600919 rn = dev->search_id;
920 last_rn = 0;
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400921 last_device = 0;
922 last_zero = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 desc_bit = 64;
925
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400926 while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
927 last_rn = rn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 rn = 0;
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 /*
931 * Reset bus and all 1-wire device state machines
932 * so they can respond to our requests.
933 *
934 * Return 0 - device(s) present, 1 - no devices present.
935 */
NeilBrownb02f8be2012-05-18 15:59:52 +1000936 mutex_lock(&dev->bus_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (w1_reset_bus(dev)) {
NeilBrownb02f8be2012-05-18 15:59:52 +1000938 mutex_unlock(&dev->bus_mutex);
Evgeniy Polyakov2da5bf82005-08-12 11:46:22 -0700939 dev_dbg(&dev->dev, "No devices present on the wire.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 break;
941 }
942
Evgeniy Polyakovc9cbf552011-12-10 04:36:11 +0300943 /* Do fast search on single slave bus */
944 if (dev->max_slave_count == 1) {
NeilBrownb02f8be2012-05-18 15:59:52 +1000945 int rv;
Evgeniy Polyakovc9cbf552011-12-10 04:36:11 +0300946 w1_write_8(dev, W1_READ_ROM);
NeilBrownb02f8be2012-05-18 15:59:52 +1000947 rv = w1_read_block(dev, (u8 *)&rn, 8);
948 mutex_unlock(&dev->bus_mutex);
Evgeniy Polyakovc9cbf552011-12-10 04:36:11 +0300949
NeilBrownb02f8be2012-05-18 15:59:52 +1000950 if (rv == 8 && rn)
Evgeniy Polyakovc9cbf552011-12-10 04:36:11 +0300951 cb(dev, rn);
952
953 break;
954 }
955
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400956 /* Start the search */
Evgeniy Polyakov12003372006-03-23 19:11:58 +0300957 w1_write_8(dev, search_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 for (i = 0; i < 64; ++i) {
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400959 /* Determine the direction/search bit */
960 if (i == desc_bit)
961 search_bit = 1; /* took the 0 path last time, so take the 1 path */
962 else if (i > desc_bit)
963 search_bit = 0; /* take the 0 path on the next branch */
964 else
965 search_bit = ((last_rn >> i) & 0x1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400967 /** Read two bits and write one bit */
968 triplet_ret = w1_triplet(dev, search_bit);
969
970 /* quit if no device responded */
971 if ( (triplet_ret & 0x03) == 0x03 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 break;
973
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400974 /* If both directions were valid, and we took the 0 path... */
975 if (triplet_ret == 0)
976 last_zero = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400978 /* extract the direction taken & update the device number */
979 tmp64 = (triplet_ret >> 2);
980 rn |= (tmp64 << i);
David Fries0d671b22008-10-15 22:04:39 -0700981
David Fries42105692014-01-15 22:29:13 -0600982 if (test_bit(W1_ABORT_SEARCH, &dev->flags)) {
NeilBrownb02f8be2012-05-18 15:59:52 +1000983 mutex_unlock(&dev->bus_mutex);
David Fries7dc8f522008-10-15 22:04:58 -0700984 dev_dbg(&dev->dev, "Abort w1_search\n");
David Fries0d671b22008-10-15 22:04:39 -0700985 return;
986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
NeilBrownb02f8be2012-05-18 15:59:52 +1000988 mutex_unlock(&dev->bus_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400990 if ( (triplet_ret & 0x03) != 0x03 ) {
David Fries3c6955e2014-01-15 22:29:16 -0600991 if ((desc_bit == last_zero) || (last_zero < 0)) {
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400992 last_device = 1;
David Fries3c6955e2014-01-15 22:29:16 -0600993 dev->search_id = 0;
994 } else {
995 dev->search_id = rn;
996 }
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400997 desc_bit = last_zero;
David Friesc30c9b12008-10-15 22:04:38 -0700998 cb(dev, rn);
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400999 }
David Friesa1613052014-01-15 22:29:15 -06001000
1001 if (!last_device && slave_count == dev->max_slave_count &&
1002 !test_bit(W1_WARN_MAX_COUNT, &dev->flags)) {
David Fries3c6955e2014-01-15 22:29:16 -06001003 /* Only max_slave_count will be scanned in a search,
1004 * but it will start where it left off next search
1005 * until all ids are identified and then it will start
1006 * over. A continued search will report the previous
1007 * last id as the first id (provided it is still on the
1008 * bus).
1009 */
David Friesa1613052014-01-15 22:29:15 -06001010 dev_info(&dev->dev, "%s: max_slave_count %d reached, "
David Fries3c6955e2014-01-15 22:29:16 -06001011 "will continue next search.\n", __func__,
David Friesa1613052014-01-15 22:29:15 -06001012 dev->max_slave_count);
1013 set_bit(W1_WARN_MAX_COUNT, &dev->flags);
1014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 }
1016}
1017
David Fries963bb102011-05-26 16:26:03 -07001018void w1_search_process_cb(struct w1_master *dev, u8 search_type,
1019 w1_slave_found_callback cb)
Evgeniy Polyakov12003372006-03-23 19:11:58 +03001020{
1021 struct w1_slave *sl, *sln;
1022
1023 list_for_each_entry(sl, &dev->slist, w1_slave_entry)
Michal Nazarewiczbb670932013-11-12 15:11:42 -08001024 clear_bit(W1_SLAVE_ACTIVE, &sl->flags);
Evgeniy Polyakov12003372006-03-23 19:11:58 +03001025
David Fries963bb102011-05-26 16:26:03 -07001026 w1_search_devices(dev, search_type, cb);
Evgeniy Polyakov12003372006-03-23 19:11:58 +03001027
1028 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
Michal Nazarewiczbb670932013-11-12 15:11:42 -08001029 if (!test_bit(W1_SLAVE_ACTIVE, &sl->flags) && !--sl->ttl)
Evgeniy Polyakov12003372006-03-23 19:11:58 +03001030 w1_slave_detach(sl);
Michal Nazarewiczbb670932013-11-12 15:11:42 -08001031 else if (test_bit(W1_SLAVE_ACTIVE, &sl->flags))
Evgeniy Polyakov12003372006-03-23 19:11:58 +03001032 sl->ttl = dev->slave_ttl;
1033 }
1034
1035 if (dev->search_count > 0)
1036 dev->search_count--;
1037}
1038
David Fries963bb102011-05-26 16:26:03 -07001039static void w1_search_process(struct w1_master *dev, u8 search_type)
1040{
1041 w1_search_process_cb(dev, search_type, w1_slave_found);
1042}
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044int w1_process(void *data)
1045{
1046 struct w1_master *dev = (struct w1_master *) data;
David Fries3c52e4e2008-10-15 22:04:41 -07001047 /* As long as w1_timeout is only set by a module parameter the sleep
1048 * time can be calculated in jiffies once.
1049 */
1050 const unsigned long jtime = msecs_to_jiffies(w1_timeout * 1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
David Fries3c52e4e2008-10-15 22:04:41 -07001052 while (!kthread_should_stop()) {
David Fries01e14d62008-10-15 22:04:40 -07001053 if (dev->search_count) {
1054 mutex_lock(&dev->mutex);
1055 w1_search_process(dev, W1_SEARCH);
1056 mutex_unlock(&dev->mutex);
1057 }
1058
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07001059 try_to_freeze();
David Fries3c52e4e2008-10-15 22:04:41 -07001060 __set_current_state(TASK_INTERRUPTIBLE);
1061
1062 if (kthread_should_stop())
1063 break;
1064
1065 /* Only sleep when the search is active. */
1066 if (dev->search_count)
1067 schedule_timeout(jtime);
1068 else
1069 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071
1072 atomic_dec(&dev->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 return 0;
1075}
1076
Peter Huewe73a98fc2009-12-22 09:38:02 +01001077static int __init w1_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
1079 int retval;
1080
1081 printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
1082
Evgeniy Polyakov12003372006-03-23 19:11:58 +03001083 w1_init_netlink();
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 retval = bus_register(&w1_bus_type);
1086 if (retval) {
1087 printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
1088 goto err_out_exit_init;
1089 }
1090
Greg Kroah-Hartman47eba332013-08-21 13:24:32 -07001091 retval = bus_register_notifier(&w1_bus_type, &w1_bus_nb);
1092 if (retval)
1093 goto err_out_bus_unregister;
1094
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +04001095 retval = driver_register(&w1_master_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (retval) {
1097 printk(KERN_ERR
1098 "Failed to register master driver. err=%d.\n",
1099 retval);
1100 goto err_out_bus_unregister;
1101 }
1102
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +04001103 retval = driver_register(&w1_slave_driver);
1104 if (retval) {
1105 printk(KERN_ERR
Dmitry Artamonowecf19482012-04-02 09:42:22 +04001106 "Failed to register slave driver. err=%d.\n",
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +04001107 retval);
1108 goto err_out_master_unregister;
1109 }
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return 0;
1112
David Friesc30c9b12008-10-15 22:04:38 -07001113#if 0
1114/* For undoing the slave register if there was a step after it. */
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +04001115err_out_slave_unregister:
1116 driver_unregister(&w1_slave_driver);
David Friesc30c9b12008-10-15 22:04:38 -07001117#endif
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +04001118
1119err_out_master_unregister:
1120 driver_unregister(&w1_master_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122err_out_bus_unregister:
1123 bus_unregister(&w1_bus_type);
1124
1125err_out_exit_init:
1126 return retval;
1127}
1128
Peter Huewe73a98fc2009-12-22 09:38:02 +01001129static void __exit w1_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 struct w1_master *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
David Friesc30c9b12008-10-15 22:04:38 -07001133 /* Set netlink removal messages and some cleanup */
Evgeniy Polyakov77859252005-05-20 22:33:25 +04001134 list_for_each_entry(dev, &w1_masters, w1_master_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 __w1_remove_master_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Evgeniy Polyakov12003372006-03-23 19:11:58 +03001137 w1_fini_netlink();
1138
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +04001139 driver_unregister(&w1_slave_driver);
1140 driver_unregister(&w1_master_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 bus_unregister(&w1_bus_type);
1142}
1143
1144module_init(w1_init);
1145module_exit(w1_fini);