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