blob: b2fe0f7b0b7c4de10afe12652de6c5a68f67b304 [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 *
4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
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>
33
34#include <asm/atomic.h>
35
36#include "w1.h"
37#include "w1_io.h"
38#include "w1_log.h"
39#include "w1_int.h"
40#include "w1_family.h"
41#include "w1_netlink.h"
42
43MODULE_LICENSE("GPL");
44MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>");
45MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
46
47static int w1_timeout = 10;
48int w1_max_slave_count = 10;
49int w1_max_slave_ttl = 10;
50
51module_param_named(timeout, w1_timeout, int, 0);
52module_param_named(max_slave_count, w1_max_slave_count, int, 0);
53module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
54
55DEFINE_SPINLOCK(w1_mlock);
56LIST_HEAD(w1_masters);
57
58static pid_t control_thread;
59static int control_needs_exit;
60static DECLARE_COMPLETION(w1_control_complete);
61
Evgeniy Polyakov99c5bfe2005-06-04 01:31:26 +040062/* stuff for the default family */
63static ssize_t w1_famdefault_read_name(struct device *dev, struct device_attribute *attr, char *buf)
64{
65 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
66 return(sprintf(buf, "%s\n", sl->name));
67}
68static struct w1_family_ops w1_default_fops = {
69 .rname = &w1_famdefault_read_name,
70};
71static struct w1_family w1_default_family = {
72 .fops = &w1_default_fops,
73};
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static int w1_master_match(struct device *dev, struct device_driver *drv)
76{
77 return 1;
78}
79
80static int w1_master_probe(struct device *dev)
81{
82 return -ENODEV;
83}
84
85static int w1_master_remove(struct device *dev)
86{
87 return 0;
88}
89
90static void w1_master_release(struct device *dev)
91{
92 struct w1_master *md = container_of(dev, struct w1_master, dev);
93
94 complete(&md->dev_released);
95}
96
97static void w1_slave_release(struct device *dev)
98{
99 struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
100
101 complete(&sl->dev_released);
102}
103
Yani Ioannou060b8842005-05-17 06:44:04 -0400104static ssize_t w1_default_read_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 return sprintf(buf, "No family registered.\n");
107}
108
109static ssize_t w1_default_read_bin(struct kobject *kobj, char *buf, loff_t off,
110 size_t count)
111{
112 return sprintf(buf, "No family registered.\n");
113}
114
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400115static struct device_attribute w1_slave_attribute =
116 __ATTR(name, S_IRUGO, w1_default_read_name, NULL);
117
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400118static struct bin_attribute w1_slave_bin_attribute = {
119 .attr = {
120 .name = "w1_slave",
121 .mode = S_IRUGO,
122 .owner = THIS_MODULE,
123 },
124 .size = W1_SLAVE_DATA_SIZE,
125 .read = &w1_default_read_bin,
126};
127
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400128static int w1_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static struct bus_type w1_bus_type = {
131 .name = "w1",
132 .match = w1_master_match,
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400133 .hotplug = w1_hotplug,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400136struct device_driver w1_master_driver = {
137 .name = "w1_master_driver",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .bus = &w1_bus_type,
139 .probe = w1_master_probe,
140 .remove = w1_master_remove,
141};
142
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400143struct device w1_master_device = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 .parent = NULL,
145 .bus = &w1_bus_type,
146 .bus_id = "w1 bus master",
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400147 .driver = &w1_master_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 .release = &w1_master_release
149};
150
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400151struct device_driver w1_slave_driver = {
152 .name = "w1_slave_driver",
153 .bus = &w1_bus_type,
154};
155
156struct device w1_slave_device = {
157 .parent = NULL,
158 .bus = &w1_bus_type,
159 .bus_id = "w1 bus slave",
160 .driver = &w1_slave_driver,
161};
162
Yani Ioannou060b8842005-05-17 06:44:04 -0400163static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400165 struct w1_master *md = container_of(dev, struct w1_master, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 ssize_t count;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (down_interruptible (&md->mutex))
169 return -EBUSY;
170
171 count = sprintf(buf, "%s\n", md->name);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 up(&md->mutex);
174
175 return count;
176}
177
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400178static ssize_t w1_master_attribute_store_search(struct device * dev,
179 struct device_attribute *attr,
180 const char * buf, size_t count)
181{
182 struct w1_master *md = container_of(dev, struct w1_master, dev);
183
184 if (down_interruptible (&md->mutex))
185 return -EBUSY;
186
187 md->search_count = simple_strtol(buf, NULL, 0);
188
189 up(&md->mutex);
190
191 return count;
192}
193
194static ssize_t w1_master_attribute_show_search(struct device *dev,
195 struct device_attribute *attr,
196 char *buf)
197{
198 struct w1_master *md = container_of(dev, struct w1_master, dev);
199 ssize_t count;
200
201 if (down_interruptible (&md->mutex))
202 return -EBUSY;
203
204 count = sprintf(buf, "%d\n", md->search_count);
205
206 up(&md->mutex);
207
208 return count;
209}
210
Yani Ioannou060b8842005-05-17 06:44:04 -0400211static ssize_t w1_master_attribute_show_pointer(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 struct w1_master *md = container_of(dev, struct w1_master, dev);
214 ssize_t count;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (down_interruptible(&md->mutex))
217 return -EBUSY;
218
219 count = sprintf(buf, "0x%p\n", md->bus_master);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 up(&md->mutex);
222 return count;
223}
224
Yani Ioannou060b8842005-05-17 06:44:04 -0400225static ssize_t w1_master_attribute_show_timeout(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 ssize_t count;
228 count = sprintf(buf, "%d\n", w1_timeout);
229 return count;
230}
231
Yani Ioannou060b8842005-05-17 06:44:04 -0400232static 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 -0700233{
234 struct w1_master *md = container_of(dev, struct w1_master, dev);
235 ssize_t count;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (down_interruptible(&md->mutex))
238 return -EBUSY;
239
240 count = sprintf(buf, "%d\n", md->max_slave_count);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 up(&md->mutex);
243 return count;
244}
245
Yani Ioannou060b8842005-05-17 06:44:04 -0400246static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct w1_master *md = container_of(dev, struct w1_master, dev);
249 ssize_t count;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (down_interruptible(&md->mutex))
252 return -EBUSY;
253
254 count = sprintf(buf, "%lu\n", md->attempts);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 up(&md->mutex);
257 return count;
258}
259
Yani Ioannou060b8842005-05-17 06:44:04 -0400260static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 struct w1_master *md = container_of(dev, struct w1_master, dev);
263 ssize_t count;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (down_interruptible(&md->mutex))
266 return -EBUSY;
267
268 count = sprintf(buf, "%d\n", md->slave_count);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 up(&md->mutex);
271 return count;
272}
273
Yani Ioannou060b8842005-05-17 06:44:04 -0400274static ssize_t w1_master_attribute_show_slaves(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 struct w1_master *md = container_of(dev, struct w1_master, dev);
277 int c = PAGE_SIZE;
278
279 if (down_interruptible(&md->mutex))
280 return -EBUSY;
281
282 if (md->slave_count == 0)
283 c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
284 else {
285 struct list_head *ent, *n;
286 struct w1_slave *sl;
287
288 list_for_each_safe(ent, n, &md->slist) {
289 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
290
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400291 c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293 }
294
295 up(&md->mutex);
296
297 return PAGE_SIZE - c;
298}
299
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400300#define W1_MASTER_ATTR_RO(_name, _mode) \
301 struct device_attribute w1_master_attribute_##_name = \
302 __ATTR(w1_master_##_name, _mode, \
303 w1_master_attribute_show_##_name, NULL)
304
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400305#define W1_MASTER_ATTR_RW(_name, _mode) \
306 struct device_attribute w1_master_attribute_##_name = \
307 __ATTR(w1_master_##_name, _mode, \
308 w1_master_attribute_show_##_name, \
309 w1_master_attribute_store_##_name)
310
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400311static W1_MASTER_ATTR_RO(name, S_IRUGO);
312static W1_MASTER_ATTR_RO(slaves, S_IRUGO);
313static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);
314static W1_MASTER_ATTR_RO(max_slave_count, S_IRUGO);
315static W1_MASTER_ATTR_RO(attempts, S_IRUGO);
316static W1_MASTER_ATTR_RO(timeout, S_IRUGO);
317static W1_MASTER_ATTR_RO(pointer, S_IRUGO);
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400318static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUGO);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400319
320static struct attribute *w1_master_default_attrs[] = {
321 &w1_master_attribute_name.attr,
322 &w1_master_attribute_slaves.attr,
323 &w1_master_attribute_slave_count.attr,
324 &w1_master_attribute_max_slave_count.attr,
325 &w1_master_attribute_attempts.attr,
326 &w1_master_attribute_timeout.attr,
327 &w1_master_attribute_pointer.attr,
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400328 &w1_master_attribute_search.attr,
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400329 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330};
331
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400332static struct attribute_group w1_master_defattr_group = {
333 .attrs = w1_master_default_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334};
335
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400336int w1_create_master_attributes(struct w1_master *master)
337{
338 return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
339}
340
341void w1_destroy_master_attributes(struct w1_master *master)
342{
343 sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
344}
345
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400346#ifdef CONFIG_HOTPLUG
347static int w1_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
348{
349 struct w1_master *md = NULL;
350 struct w1_slave *sl = NULL;
351 char *event_owner, *name;
352 int err, cur_index=0, cur_len=0;
353
354 if (dev->driver == &w1_master_driver) {
355 md = container_of(dev, struct w1_master, dev);
356 event_owner = "master";
357 name = md->name;
358 } else if (dev->driver == &w1_slave_driver) {
359 sl = container_of(dev, struct w1_slave, dev);
360 event_owner = "slave";
361 name = sl->name;
362 } else {
363 dev_dbg(dev, "Unknown hotplug event.\n");
364 return -EINVAL;
365 }
366
367 dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n", event_owner, name, dev->bus_id);
368
369 if (dev->driver != &w1_slave_driver || !sl)
370 return 0;
371
372 err = add_hotplug_env_var(envp, num_envp, &cur_index, buffer, buffer_size, &cur_len, "W1_FID=%02X", sl->reg_num.family);
373 if (err)
374 return err;
375
Evgeniy Polyakov5e8eb852005-08-11 13:45:54 +0400376 err = add_hotplug_env_var(envp, num_envp, &cur_index, buffer, buffer_size, &cur_len, "W1_SLAVE_ID=%024LX", (u64)sl->reg_num.id);
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400377 if (err)
378 return err;
379
380 return 0;
381};
382#else
383static int w1_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
384{
385 return 0;
386}
387#endif
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389static int __w1_attach_slave_device(struct w1_slave *sl)
390{
391 int err;
392
393 sl->dev.parent = &sl->master->dev;
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400394 sl->dev.driver = &w1_slave_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 sl->dev.bus = &w1_bus_type;
396 sl->dev.release = &w1_slave_release;
397
398 snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id),
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400399 "%02x-%012llx",
400 (unsigned int) sl->reg_num.family,
401 (unsigned long long) sl->reg_num.id);
402 snprintf(&sl->name[0], sizeof(sl->name),
403 "%02x-%012llx",
404 (unsigned int) sl->reg_num.family,
405 (unsigned long long) sl->reg_num.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 dev_dbg(&sl->dev, "%s: registering %s.\n", __func__,
408 &sl->dev.bus_id[0]);
409
410 err = device_register(&sl->dev);
411 if (err < 0) {
412 dev_err(&sl->dev,
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400413 "Device registration [%s] failed. err=%d\n",
414 sl->dev.bus_id, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return err;
416 }
417
418 memcpy(&sl->attr_bin, &w1_slave_bin_attribute, sizeof(sl->attr_bin));
419 memcpy(&sl->attr_name, &w1_slave_attribute, sizeof(sl->attr_name));
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 sl->attr_bin.read = sl->family->fops->rbin;
422 sl->attr_name.show = sl->family->fops->rname;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 err = device_create_file(&sl->dev, &sl->attr_name);
425 if (err < 0) {
426 dev_err(&sl->dev,
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400427 "sysfs file creation for [%s] failed. err=%d\n",
428 sl->dev.bus_id, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 device_unregister(&sl->dev);
430 return err;
431 }
432
Evgeniy Polyakov99c5bfe2005-06-04 01:31:26 +0400433 if ( sl->attr_bin.read ) {
434 err = sysfs_create_bin_file(&sl->dev.kobj, &sl->attr_bin);
435 if (err < 0) {
436 dev_err(&sl->dev,
437 "sysfs file creation for [%s] failed. err=%d\n",
438 sl->dev.bus_id, err);
439 device_remove_file(&sl->dev, &sl->attr_name);
440 device_unregister(&sl->dev);
441 return err;
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
445 list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
446
447 return 0;
448}
449
450static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
451{
452 struct w1_slave *sl;
453 struct w1_family *f;
454 int err;
455 struct w1_netlink_msg msg;
456
457 sl = kmalloc(sizeof(struct w1_slave), GFP_KERNEL);
458 if (!sl) {
459 dev_err(&dev->dev,
460 "%s: failed to allocate new slave device.\n",
461 __func__);
462 return -ENOMEM;
463 }
464
465 memset(sl, 0, sizeof(*sl));
466
467 sl->owner = THIS_MODULE;
468 sl->master = dev;
469 set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
470
471 memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
472 atomic_set(&sl->refcnt, 0);
473 init_completion(&sl->dev_released);
474
475 spin_lock(&w1_flock);
476 f = w1_family_registered(rn->family);
477 if (!f) {
Evgeniy Polyakov99c5bfe2005-06-04 01:31:26 +0400478 f= &w1_default_family;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
480 rn->family, rn->family,
481 (unsigned long long)rn->id, rn->crc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483 __w1_family_get(f);
484 spin_unlock(&w1_flock);
485
486 sl->family = f;
487
488
489 err = __w1_attach_slave_device(sl);
490 if (err < 0) {
491 dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
492 sl->name);
493 w1_family_put(sl->family);
494 kfree(sl);
495 return err;
496 }
497
498 sl->ttl = dev->slave_ttl;
499 dev->slave_count++;
500
501 memcpy(&msg.id.id, rn, sizeof(msg.id.id));
502 msg.type = W1_SLAVE_ADD;
503 w1_netlink_send(dev, &msg);
504
505 return 0;
506}
507
508static void w1_slave_detach(struct w1_slave *sl)
509{
510 struct w1_netlink_msg msg;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 dev_info(&sl->dev, "%s: detaching %s.\n", __func__, sl->name);
513
514 while (atomic_read(&sl->refcnt)) {
515 printk(KERN_INFO "Waiting for %s to become free: refcnt=%d.\n",
516 sl->name, atomic_read(&sl->refcnt));
517
518 if (msleep_interruptible(1000))
519 flush_signals(current);
520 }
521
Evgeniy Polyakov99c5bfe2005-06-04 01:31:26 +0400522 if ( sl->attr_bin.read ) {
523 sysfs_remove_bin_file (&sl->dev.kobj, &sl->attr_bin);
524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 device_remove_file(&sl->dev, &sl->attr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 device_unregister(&sl->dev);
527 w1_family_put(sl->family);
528
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400529 sl->master->slave_count--;
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 memcpy(&msg.id.id, &sl->reg_num, sizeof(msg.id.id));
532 msg.type = W1_SLAVE_REMOVE;
533 w1_netlink_send(sl->master, &msg);
534}
535
536static struct w1_master *w1_search_master(unsigned long data)
537{
538 struct w1_master *dev;
539 int found = 0;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400540
541 spin_lock_bh(&w1_mlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
543 if (dev->bus_master->data == data) {
544 found = 1;
545 atomic_inc(&dev->refcnt);
546 break;
547 }
548 }
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400549 spin_unlock_bh(&w1_mlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 return (found)?dev:NULL;
552}
553
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400554void w1_reconnect_slaves(struct w1_family *f)
555{
556 struct w1_master *dev;
557
558 spin_lock_bh(&w1_mlock);
559 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
560 dev_info(&dev->dev, "Reconnecting slaves in %s into new family %02x.\n",
561 dev->name, f->fid);
562 set_bit(W1_MASTER_NEED_RECONNECT, &dev->flags);
563 }
564 spin_unlock_bh(&w1_mlock);
565}
566
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400567static void w1_slave_found(unsigned long data, u64 rn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 int slave_count;
570 struct w1_slave *sl;
571 struct list_head *ent;
572 struct w1_reg_num *tmp;
573 int family_found = 0;
574 struct w1_master *dev;
Evgeniy Polyakov0e65f822005-06-30 22:52:38 +0400575 u64 rn_le = cpu_to_le64(rn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 dev = w1_search_master(data);
578 if (!dev) {
579 printk(KERN_ERR "Failed to find w1 master device for data %08lx, it is impossible.\n",
580 data);
581 return;
582 }
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 tmp = (struct w1_reg_num *) &rn;
585
586 slave_count = 0;
587 list_for_each(ent, &dev->slist) {
588
589 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
590
591 if (sl->reg_num.family == tmp->family &&
592 sl->reg_num.id == tmp->id &&
593 sl->reg_num.crc == tmp->crc) {
594 set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
595 break;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400596 } else if (sl->reg_num.family == tmp->family) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 family_found = 1;
598 break;
599 }
600
601 slave_count++;
602 }
603
johnpol@2ka.mipt.ru8523ff42005-04-18 21:16:57 -0700604 if (slave_count == dev->slave_count &&
Evgeniy Polyakov0e65f822005-06-30 22:52:38 +0400605 rn && ((rn >> 56) & 0xff) == w1_calc_crc8((u8 *)&rn_le, 7)) {
johnpol@2ka.mipt.ru8523ff42005-04-18 21:16:57 -0700606 w1_attach_slave_device(dev, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 atomic_dec(&dev->refcnt);
610}
611
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400612/**
613 * Performs a ROM Search & registers any devices found.
614 * The 1-wire search is a simple binary tree search.
615 * For each bit of the address, we read two bits and write one bit.
616 * The bit written will put to sleep all devies that don't match that bit.
617 * When the two reads differ, the direction choice is obvious.
618 * When both bits are 0, we must choose a path to take.
619 * When we can scan all 64 bits without having to choose a path, we are done.
620 *
621 * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
622 *
623 * @dev The master device to search
624 * @cb Function to call when a device is found
625 */
626void w1_search(struct w1_master *dev, w1_slave_found_callback cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400628 u64 last_rn, rn, tmp64;
629 int i, slave_count = 0;
630 int last_zero, last_device;
631 int search_bit, desc_bit;
632 u8 triplet_ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400634 search_bit = 0;
635 rn = last_rn = 0;
636 last_device = 0;
637 last_zero = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 desc_bit = 64;
640
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400641 while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
642 last_rn = rn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 rn = 0;
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /*
646 * Reset bus and all 1-wire device state machines
647 * so they can respond to our requests.
648 *
649 * Return 0 - device(s) present, 1 - no devices present.
650 */
651 if (w1_reset_bus(dev)) {
Evgeniy Polyakov2da5bf82005-08-12 11:46:22 -0700652 dev_dbg(&dev->dev, "No devices present on the wire.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 break;
654 }
655
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400656 /* Start the search */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 w1_write_8(dev, W1_SEARCH);
658 for (i = 0; i < 64; ++i) {
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400659 /* Determine the direction/search bit */
660 if (i == desc_bit)
661 search_bit = 1; /* took the 0 path last time, so take the 1 path */
662 else if (i > desc_bit)
663 search_bit = 0; /* take the 0 path on the next branch */
664 else
665 search_bit = ((last_rn >> i) & 0x1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400667 /** Read two bits and write one bit */
668 triplet_ret = w1_triplet(dev, search_bit);
669
670 /* quit if no device responded */
671 if ( (triplet_ret & 0x03) == 0x03 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 break;
673
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400674 /* If both directions were valid, and we took the 0 path... */
675 if (triplet_ret == 0)
676 last_zero = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400678 /* extract the direction taken & update the device number */
679 tmp64 = (triplet_ret >> 2);
680 rn |= (tmp64 << i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Evgeniy Polyakov6b729862005-06-04 01:30:43 +0400683 if ( (triplet_ret & 0x03) != 0x03 ) {
684 if ( (desc_bit == last_zero) || (last_zero < 0))
685 last_device = 1;
686 desc_bit = last_zero;
687 cb(dev->bus_master->data, rn);
688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690}
691
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400692static int w1_control(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400694 struct w1_slave *sl, *sln;
695 struct w1_master *dev, *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 int err, have_to_wait = 0;
697
698 daemonize("w1_control");
699 allow_signal(SIGTERM);
700
701 while (!control_needs_exit || have_to_wait) {
702 have_to_wait = 0;
703
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700704 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 msleep_interruptible(w1_timeout * 1000);
706
707 if (signal_pending(current))
708 flush_signals(current);
709
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400710 list_for_each_entry_safe(dev, n, &w1_masters, w1_master_entry) {
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400711 if (!control_needs_exit && !dev->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 continue;
713 /*
714 * Little race: we can create thread but not set the flag.
715 * Get a chance for external process to set flag up.
716 */
717 if (!dev->initialized) {
718 have_to_wait = 1;
719 continue;
720 }
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (control_needs_exit) {
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400723 set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 err = kill_proc(dev->kpid, SIGTERM, 1);
726 if (err)
727 dev_err(&dev->dev,
728 "Failed to send signal to w1 kernel thread %d.\n",
729 dev->kpid);
730 }
731
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400732 if (test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {
733 wait_for_completion(&dev->dev_exited);
734 spin_lock_bh(&w1_mlock);
735 list_del(&dev->w1_master_entry);
736 spin_unlock_bh(&w1_mlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400738 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
739 list_del(&sl->w1_slave_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400741 w1_slave_detach(sl);
742 kfree(sl);
743 }
744 w1_destroy_master_attributes(dev);
745 atomic_dec(&dev->refcnt);
746 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400748
749 if (test_bit(W1_MASTER_NEED_RECONNECT, &dev->flags)) {
750 dev_info(&dev->dev, "Reconnecting slaves in device %s.\n", dev->name);
751 down(&dev->mutex);
752 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
753 if (sl->family->fid == W1_FAMILY_DEFAULT) {
754 struct w1_reg_num rn;
755 list_del(&sl->w1_slave_entry);
756 w1_slave_detach(sl);
757
758 memcpy(&rn, &sl->reg_num, sizeof(rn));
759
760 kfree(sl);
761
762 w1_attach_slave_device(dev, &rn);
763 }
764 }
765 clear_bit(W1_MASTER_NEED_RECONNECT, &dev->flags);
766 up(&dev->mutex);
767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769 }
770
771 complete_and_exit(&w1_control_complete, 0);
772}
773
774int w1_process(void *data)
775{
776 struct w1_master *dev = (struct w1_master *) data;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400777 struct w1_slave *sl, *sln;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 daemonize("%s", dev->name);
780 allow_signal(SIGTERM);
781
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400782 while (!test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700783 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 msleep_interruptible(w1_timeout * 1000);
785
786 if (signal_pending(current))
787 flush_signals(current);
788
Evgeniy Polyakov6adf87b2005-06-04 01:29:25 +0400789 if (test_bit(W1_MASTER_NEED_EXIT, &dev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 break;
791
792 if (!dev->initialized)
793 continue;
794
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400795 if (dev->search_count == 0)
796 continue;
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (down_interruptible(&dev->mutex))
799 continue;
800
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400801 list_for_each_entry(sl, &dev->slist, w1_slave_entry)
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400802 clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 w1_search_devices(dev, w1_slave_found);
805
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400806 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
807 if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 list_del (&sl->w1_slave_entry);
809
810 w1_slave_detach (sl);
811 kfree (sl);
812
813 dev->slave_count--;
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400814 } else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 sl->ttl = dev->slave_ttl;
816 }
Evgeniy Polyakov2a9d0c12005-06-04 01:31:02 +0400817
818 if (dev->search_count > 0)
819 dev->search_count--;
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 up(&dev->mutex);
822 }
823
824 atomic_dec(&dev->refcnt);
825 complete_and_exit(&dev->dev_exited, 0);
826
827 return 0;
828}
829
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400830static int w1_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
832 int retval;
833
834 printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n");
835
836 retval = bus_register(&w1_bus_type);
837 if (retval) {
838 printk(KERN_ERR "Failed to register bus. err=%d.\n", retval);
839 goto err_out_exit_init;
840 }
841
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400842 retval = driver_register(&w1_master_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (retval) {
844 printk(KERN_ERR
845 "Failed to register master driver. err=%d.\n",
846 retval);
847 goto err_out_bus_unregister;
848 }
849
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400850 retval = driver_register(&w1_slave_driver);
851 if (retval) {
852 printk(KERN_ERR
853 "Failed to register master driver. err=%d.\n",
854 retval);
855 goto err_out_master_unregister;
856 }
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 control_thread = kernel_thread(&w1_control, NULL, 0);
859 if (control_thread < 0) {
860 printk(KERN_ERR "Failed to create control thread. err=%d\n",
861 control_thread);
862 retval = control_thread;
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400863 goto err_out_slave_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865
866 return 0;
867
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400868err_out_slave_unregister:
869 driver_unregister(&w1_slave_driver);
870
871err_out_master_unregister:
872 driver_unregister(&w1_master_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874err_out_bus_unregister:
875 bus_unregister(&w1_bus_type);
876
877err_out_exit_init:
878 return retval;
879}
880
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400881static void w1_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883 struct w1_master *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Evgeniy Polyakov77859252005-05-20 22:33:25 +0400885 list_for_each_entry(dev, &w1_masters, w1_master_entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 __w1_remove_master_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 control_needs_exit = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 wait_for_completion(&w1_control_complete);
890
Evgeniy Polyakov7f772ed2005-08-11 13:20:07 +0400891 driver_unregister(&w1_slave_driver);
892 driver_unregister(&w1_master_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 bus_unregister(&w1_bus_type);
894}
895
896module_init(w1_init);
897module_exit(w1_fini);