aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/w1/w1.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/w1/w1.c')
-rw-r--r--drivers/w1/w1.c546
1 files changed, 270 insertions, 276 deletions
diff --git a/drivers/w1/w1.c b/drivers/w1/w1.c
index 24a192e3b8b4..b460927ec32a 100644
--- a/drivers/w1/w1.c
+++ b/drivers/w1/w1.c
@@ -1,8 +1,8 @@
1/* 1/*
2 * w1.c 2 * w1.c
3 * 3 *
4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru> 4 * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru>
5 * 5 *
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 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 8 * it under the terms of the GNU General Public License as published by
@@ -59,6 +59,19 @@ static pid_t control_thread;
59static int control_needs_exit; 59static int control_needs_exit;
60static DECLARE_COMPLETION(w1_control_complete); 60static DECLARE_COMPLETION(w1_control_complete);
61 61
62/* 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
62static int w1_master_match(struct device *dev, struct device_driver *drv) 75static int w1_master_match(struct device *dev, struct device_driver *drv)
63{ 76{
64 return 1; 77 return 1;
@@ -99,6 +112,20 @@ static ssize_t w1_default_read_bin(struct kobject *kobj, char *buf, loff_t off,
99 return sprintf(buf, "No family registered.\n"); 112 return sprintf(buf, "No family registered.\n");
100} 113}
101 114
115static struct device_attribute w1_slave_attribute =
116 __ATTR(name, S_IRUGO, w1_default_read_name, NULL);
117
118static 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
128
102static struct bus_type w1_bus_type = { 129static struct bus_type w1_bus_type = {
103 .name = "w1", 130 .name = "w1",
104 .match = w1_master_match, 131 .match = w1_master_match,
@@ -119,34 +146,49 @@ struct device w1_device = {
119 .release = &w1_master_release 146 .release = &w1_master_release
120}; 147};
121 148
122static struct device_attribute w1_slave_attribute = {
123 .attr = {
124 .name = "name",
125 .mode = S_IRUGO,
126 .owner = THIS_MODULE
127 },
128 .show = &w1_default_read_name,
129};
130
131static struct device_attribute w1_slave_attribute_val = {
132 .attr = {
133 .name = "value",
134 .mode = S_IRUGO,
135 .owner = THIS_MODULE
136 },
137 .show = &w1_default_read_name,
138};
139
140static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf) 149static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
141{ 150{
142 struct w1_master *md = container_of (dev, struct w1_master, dev); 151 struct w1_master *md = container_of(dev, struct w1_master, dev);
143 ssize_t count; 152 ssize_t count;
144 153
145 if (down_interruptible (&md->mutex)) 154 if (down_interruptible (&md->mutex))
146 return -EBUSY; 155 return -EBUSY;
147 156
148 count = sprintf(buf, "%s\n", md->name); 157 count = sprintf(buf, "%s\n", md->name);
149 158
159 up(&md->mutex);
160
161 return count;
162}
163
164static ssize_t w1_master_attribute_store_search(struct device * dev,
165 struct device_attribute *attr,
166 const char * buf, size_t count)
167{
168 struct w1_master *md = container_of(dev, struct w1_master, dev);
169
170 if (down_interruptible (&md->mutex))
171 return -EBUSY;
172
173 md->search_count = simple_strtol(buf, NULL, 0);
174
175 up(&md->mutex);
176
177 return count;
178}
179
180static ssize_t w1_master_attribute_show_search(struct device *dev,
181 struct device_attribute *attr,
182 char *buf)
183{
184 struct w1_master *md = container_of(dev, struct w1_master, dev);
185 ssize_t count;
186
187 if (down_interruptible (&md->mutex))
188 return -EBUSY;
189
190 count = sprintf(buf, "%d\n", md->search_count);
191
150 up(&md->mutex); 192 up(&md->mutex);
151 193
152 return count; 194 return count;
@@ -156,12 +198,12 @@ static ssize_t w1_master_attribute_show_pointer(struct device *dev, struct devic
156{ 198{
157 struct w1_master *md = container_of(dev, struct w1_master, dev); 199 struct w1_master *md = container_of(dev, struct w1_master, dev);
158 ssize_t count; 200 ssize_t count;
159 201
160 if (down_interruptible(&md->mutex)) 202 if (down_interruptible(&md->mutex))
161 return -EBUSY; 203 return -EBUSY;
162 204
163 count = sprintf(buf, "0x%p\n", md->bus_master); 205 count = sprintf(buf, "0x%p\n", md->bus_master);
164 206
165 up(&md->mutex); 207 up(&md->mutex);
166 return count; 208 return count;
167} 209}
@@ -177,12 +219,12 @@ static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, stru
177{ 219{
178 struct w1_master *md = container_of(dev, struct w1_master, dev); 220 struct w1_master *md = container_of(dev, struct w1_master, dev);
179 ssize_t count; 221 ssize_t count;
180 222
181 if (down_interruptible(&md->mutex)) 223 if (down_interruptible(&md->mutex))
182 return -EBUSY; 224 return -EBUSY;
183 225
184 count = sprintf(buf, "%d\n", md->max_slave_count); 226 count = sprintf(buf, "%d\n", md->max_slave_count);
185 227
186 up(&md->mutex); 228 up(&md->mutex);
187 return count; 229 return count;
188} 230}
@@ -191,12 +233,12 @@ static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct devi
191{ 233{
192 struct w1_master *md = container_of(dev, struct w1_master, dev); 234 struct w1_master *md = container_of(dev, struct w1_master, dev);
193 ssize_t count; 235 ssize_t count;
194 236
195 if (down_interruptible(&md->mutex)) 237 if (down_interruptible(&md->mutex))
196 return -EBUSY; 238 return -EBUSY;
197 239
198 count = sprintf(buf, "%lu\n", md->attempts); 240 count = sprintf(buf, "%lu\n", md->attempts);
199 241
200 up(&md->mutex); 242 up(&md->mutex);
201 return count; 243 return count;
202} 244}
@@ -205,18 +247,17 @@ static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct d
205{ 247{
206 struct w1_master *md = container_of(dev, struct w1_master, dev); 248 struct w1_master *md = container_of(dev, struct w1_master, dev);
207 ssize_t count; 249 ssize_t count;
208 250
209 if (down_interruptible(&md->mutex)) 251 if (down_interruptible(&md->mutex))
210 return -EBUSY; 252 return -EBUSY;
211 253
212 count = sprintf(buf, "%d\n", md->slave_count); 254 count = sprintf(buf, "%d\n", md->slave_count);
213 255
214 up(&md->mutex); 256 up(&md->mutex);
215 return count; 257 return count;
216} 258}
217 259
218static ssize_t w1_master_attribute_show_slaves(struct device *dev, struct device_attribute *attr, char *buf) 260static ssize_t w1_master_attribute_show_slaves(struct device *dev, struct device_attribute *attr, char *buf)
219
220{ 261{
221 struct w1_master *md = container_of(dev, struct w1_master, dev); 262 struct w1_master *md = container_of(dev, struct w1_master, dev);
222 int c = PAGE_SIZE; 263 int c = PAGE_SIZE;
@@ -233,7 +274,7 @@ static ssize_t w1_master_attribute_show_slaves(struct device *dev, struct device
233 list_for_each_safe(ent, n, &md->slist) { 274 list_for_each_safe(ent, n, &md->slist) {
234 sl = list_entry(ent, struct w1_slave, w1_slave_entry); 275 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
235 276
236 c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name); 277 c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
237 } 278 }
238 } 279 }
239 280
@@ -242,73 +283,52 @@ static ssize_t w1_master_attribute_show_slaves(struct device *dev, struct device
242 return PAGE_SIZE - c; 283 return PAGE_SIZE - c;
243} 284}
244 285
245static struct device_attribute w1_master_attribute_slaves = { 286#define W1_MASTER_ATTR_RO(_name, _mode) \
246 .attr = { 287 struct device_attribute w1_master_attribute_##_name = \
247 .name = "w1_master_slaves", 288 __ATTR(w1_master_##_name, _mode, \
248 .mode = S_IRUGO, 289 w1_master_attribute_show_##_name, NULL)
249 .owner = THIS_MODULE, 290
250 }, 291#define W1_MASTER_ATTR_RW(_name, _mode) \
251 .show = &w1_master_attribute_show_slaves, 292 struct device_attribute w1_master_attribute_##_name = \
252}; 293 __ATTR(w1_master_##_name, _mode, \
253static struct device_attribute w1_master_attribute_slave_count = { 294 w1_master_attribute_show_##_name, \
254 .attr = { 295 w1_master_attribute_store_##_name)
255 .name = "w1_master_slave_count", 296
256 .mode = S_IRUGO, 297static W1_MASTER_ATTR_RO(name, S_IRUGO);
257 .owner = THIS_MODULE 298static W1_MASTER_ATTR_RO(slaves, S_IRUGO);
258 }, 299static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);
259 .show = &w1_master_attribute_show_slave_count, 300static W1_MASTER_ATTR_RO(max_slave_count, S_IRUGO);
260}; 301static W1_MASTER_ATTR_RO(attempts, S_IRUGO);
261static struct device_attribute w1_master_attribute_attempts = { 302static W1_MASTER_ATTR_RO(timeout, S_IRUGO);
262 .attr = { 303static W1_MASTER_ATTR_RO(pointer, S_IRUGO);
263 .name = "w1_master_attempts", 304static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUGO);
264 .mode = S_IRUGO, 305
265 .owner = THIS_MODULE 306static struct attribute *w1_master_default_attrs[] = {
266 }, 307 &w1_master_attribute_name.attr,
267 .show = &w1_master_attribute_show_attempts, 308 &w1_master_attribute_slaves.attr,
268}; 309 &w1_master_attribute_slave_count.attr,
269static struct device_attribute w1_master_attribute_max_slave_count = { 310 &w1_master_attribute_max_slave_count.attr,
270 .attr = { 311 &w1_master_attribute_attempts.attr,
271 .name = "w1_master_max_slave_count", 312 &w1_master_attribute_timeout.attr,
272 .mode = S_IRUGO, 313 &w1_master_attribute_pointer.attr,
273 .owner = THIS_MODULE 314 &w1_master_attribute_search.attr,
274 }, 315 NULL
275 .show = &w1_master_attribute_show_max_slave_count,
276};
277static struct device_attribute w1_master_attribute_timeout = {
278 .attr = {
279 .name = "w1_master_timeout",
280 .mode = S_IRUGO,
281 .owner = THIS_MODULE
282 },
283 .show = &w1_master_attribute_show_timeout,
284};
285static struct device_attribute w1_master_attribute_pointer = {
286 .attr = {
287 .name = "w1_master_pointer",
288 .mode = S_IRUGO,
289 .owner = THIS_MODULE
290 },
291 .show = &w1_master_attribute_show_pointer,
292};
293static struct device_attribute w1_master_attribute_name = {
294 .attr = {
295 .name = "w1_master_name",
296 .mode = S_IRUGO,
297 .owner = THIS_MODULE
298 },
299 .show = &w1_master_attribute_show_name,
300}; 316};
301 317
302static struct bin_attribute w1_slave_bin_attribute = { 318static struct attribute_group w1_master_defattr_group = {
303 .attr = { 319 .attrs = w1_master_default_attrs,
304 .name = "w1_slave",
305 .mode = S_IRUGO,
306 .owner = THIS_MODULE,
307 },
308 .size = W1_SLAVE_DATA_SIZE,
309 .read = &w1_default_read_bin,
310}; 320};
311 321
322int w1_create_master_attributes(struct w1_master *master)
323{
324 return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
325}
326
327void w1_destroy_master_attributes(struct w1_master *master)
328{
329 sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
330}
331
312static int __w1_attach_slave_device(struct w1_slave *sl) 332static int __w1_attach_slave_device(struct w1_slave *sl)
313{ 333{
314 int err; 334 int err;
@@ -319,13 +339,13 @@ static int __w1_attach_slave_device(struct w1_slave *sl)
319 sl->dev.release = &w1_slave_release; 339 sl->dev.release = &w1_slave_release;
320 340
321 snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id), 341 snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id),
322 "%02x-%012llx", 342 "%02x-%012llx",
323 (unsigned int) sl->reg_num.family, 343 (unsigned int) sl->reg_num.family,
324 (unsigned long long) sl->reg_num.id); 344 (unsigned long long) sl->reg_num.id);
325 snprintf (&sl->name[0], sizeof(sl->name), 345 snprintf(&sl->name[0], sizeof(sl->name),
326 "%02x-%012llx", 346 "%02x-%012llx",
327 (unsigned int) sl->reg_num.family, 347 (unsigned int) sl->reg_num.family,
328 (unsigned long long) sl->reg_num.id); 348 (unsigned long long) sl->reg_num.id);
329 349
330 dev_dbg(&sl->dev, "%s: registering %s.\n", __func__, 350 dev_dbg(&sl->dev, "%s: registering %s.\n", __func__,
331 &sl->dev.bus_id[0]); 351 &sl->dev.bus_id[0]);
@@ -333,48 +353,36 @@ static int __w1_attach_slave_device(struct w1_slave *sl)
333 err = device_register(&sl->dev); 353 err = device_register(&sl->dev);
334 if (err < 0) { 354 if (err < 0) {
335 dev_err(&sl->dev, 355 dev_err(&sl->dev,
336 "Device registration [%s] failed. err=%d\n", 356 "Device registration [%s] failed. err=%d\n",
337 sl->dev.bus_id, err); 357 sl->dev.bus_id, err);
338 return err; 358 return err;
339 } 359 }
340 360
341 memcpy(&sl->attr_bin, &w1_slave_bin_attribute, sizeof(sl->attr_bin)); 361 memcpy(&sl->attr_bin, &w1_slave_bin_attribute, sizeof(sl->attr_bin));
342 memcpy(&sl->attr_name, &w1_slave_attribute, sizeof(sl->attr_name)); 362 memcpy(&sl->attr_name, &w1_slave_attribute, sizeof(sl->attr_name));
343 memcpy(&sl->attr_val, &w1_slave_attribute_val, sizeof(sl->attr_val)); 363
344
345 sl->attr_bin.read = sl->family->fops->rbin; 364 sl->attr_bin.read = sl->family->fops->rbin;
346 sl->attr_name.show = sl->family->fops->rname; 365 sl->attr_name.show = sl->family->fops->rname;
347 sl->attr_val.show = sl->family->fops->rval;
348 sl->attr_val.attr.name = sl->family->fops->rvalname;
349 366
350 err = device_create_file(&sl->dev, &sl->attr_name); 367 err = device_create_file(&sl->dev, &sl->attr_name);
351 if (err < 0) { 368 if (err < 0) {
352 dev_err(&sl->dev, 369 dev_err(&sl->dev,
353 "sysfs file creation for [%s] failed. err=%d\n", 370 "sysfs file creation for [%s] failed. err=%d\n",
354 sl->dev.bus_id, err); 371 sl->dev.bus_id, err);
355 device_unregister(&sl->dev);
356 return err;
357 }
358
359 err = device_create_file(&sl->dev, &sl->attr_val);
360 if (err < 0) {
361 dev_err(&sl->dev,
362 "sysfs file creation for [%s] failed. err=%d\n",
363 sl->dev.bus_id, err);
364 device_remove_file(&sl->dev, &sl->attr_name);
365 device_unregister(&sl->dev); 372 device_unregister(&sl->dev);
366 return err; 373 return err;
367 } 374 }
368 375
369 err = sysfs_create_bin_file(&sl->dev.kobj, &sl->attr_bin); 376 if ( sl->attr_bin.read ) {
370 if (err < 0) { 377 err = sysfs_create_bin_file(&sl->dev.kobj, &sl->attr_bin);
371 dev_err(&sl->dev, 378 if (err < 0) {
372 "sysfs file creation for [%s] failed. err=%d\n", 379 dev_err(&sl->dev,
373 sl->dev.bus_id, err); 380 "sysfs file creation for [%s] failed. err=%d\n",
374 device_remove_file(&sl->dev, &sl->attr_name); 381 sl->dev.bus_id, err);
375 device_remove_file(&sl->dev, &sl->attr_val); 382 device_remove_file(&sl->dev, &sl->attr_name);
376 device_unregister(&sl->dev); 383 device_unregister(&sl->dev);
377 return err; 384 return err;
385 }
378 } 386 }
379 387
380 list_add_tail(&sl->w1_slave_entry, &sl->master->slist); 388 list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
@@ -410,12 +418,10 @@ static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
410 spin_lock(&w1_flock); 418 spin_lock(&w1_flock);
411 f = w1_family_registered(rn->family); 419 f = w1_family_registered(rn->family);
412 if (!f) { 420 if (!f) {
413 spin_unlock(&w1_flock); 421 f= &w1_default_family;
414 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n", 422 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
415 rn->family, rn->family, 423 rn->family, rn->family,
416 (unsigned long long)rn->id, rn->crc); 424 (unsigned long long)rn->id, rn->crc);
417 kfree(sl);
418 return -ENODEV;
419 } 425 }
420 __w1_family_get(f); 426 __w1_family_get(f);
421 spin_unlock(&w1_flock); 427 spin_unlock(&w1_flock);
@@ -445,7 +451,7 @@ static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
445static void w1_slave_detach(struct w1_slave *sl) 451static void w1_slave_detach(struct w1_slave *sl)
446{ 452{
447 struct w1_netlink_msg msg; 453 struct w1_netlink_msg msg;
448 454
449 dev_info(&sl->dev, "%s: detaching %s.\n", __func__, sl->name); 455 dev_info(&sl->dev, "%s: detaching %s.\n", __func__, sl->name);
450 456
451 while (atomic_read(&sl->refcnt)) { 457 while (atomic_read(&sl->refcnt)) {
@@ -456,12 +462,15 @@ static void w1_slave_detach(struct w1_slave *sl)
456 flush_signals(current); 462 flush_signals(current);
457 } 463 }
458 464
459 sysfs_remove_bin_file (&sl->dev.kobj, &sl->attr_bin); 465 if ( sl->attr_bin.read ) {
466 sysfs_remove_bin_file (&sl->dev.kobj, &sl->attr_bin);
467 }
460 device_remove_file(&sl->dev, &sl->attr_name); 468 device_remove_file(&sl->dev, &sl->attr_name);
461 device_remove_file(&sl->dev, &sl->attr_val);
462 device_unregister(&sl->dev); 469 device_unregister(&sl->dev);
463 w1_family_put(sl->family); 470 w1_family_put(sl->family);
464 471
472 sl->master->slave_count--;
473
465 memcpy(&msg.id.id, &sl->reg_num, sizeof(msg.id.id)); 474 memcpy(&msg.id.id, &sl->reg_num, sizeof(msg.id.id));
466 msg.type = W1_SLAVE_REMOVE; 475 msg.type = W1_SLAVE_REMOVE;
467 w1_netlink_send(sl->master, &msg); 476 w1_netlink_send(sl->master, &msg);
@@ -471,8 +480,8 @@ static struct w1_master *w1_search_master(unsigned long data)
471{ 480{
472 struct w1_master *dev; 481 struct w1_master *dev;
473 int found = 0; 482 int found = 0;
474 483
475 spin_lock_irq(&w1_mlock); 484 spin_lock_bh(&w1_mlock);
476 list_for_each_entry(dev, &w1_masters, w1_master_entry) { 485 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
477 if (dev->bus_master->data == data) { 486 if (dev->bus_master->data == data) {
478 found = 1; 487 found = 1;
@@ -480,12 +489,26 @@ static struct w1_master *w1_search_master(unsigned long data)
480 break; 489 break;
481 } 490 }
482 } 491 }
483 spin_unlock_irq(&w1_mlock); 492 spin_unlock_bh(&w1_mlock);
484 493
485 return (found)?dev:NULL; 494 return (found)?dev:NULL;
486} 495}
487 496
488void w1_slave_found(unsigned long data, u64 rn) 497void w1_reconnect_slaves(struct w1_family *f)
498{
499 struct w1_master *dev;
500
501 spin_lock_bh(&w1_mlock);
502 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
503 dev_info(&dev->dev, "Reconnecting slaves in %s into new family %02x.\n",
504 dev->name, f->fid);
505 set_bit(W1_MASTER_NEED_RECONNECT, &dev->flags);
506 }
507 spin_unlock_bh(&w1_mlock);
508}
509
510
511static void w1_slave_found(unsigned long data, u64 rn)
489{ 512{
490 int slave_count; 513 int slave_count;
491 struct w1_slave *sl; 514 struct w1_slave *sl;
@@ -500,7 +523,7 @@ void w1_slave_found(unsigned long data, u64 rn)
500 data); 523 data);
501 return; 524 return;
502 } 525 }
503 526
504 tmp = (struct w1_reg_num *) &rn; 527 tmp = (struct w1_reg_num *) &rn;
505 528
506 slave_count = 0; 529 slave_count = 0;
@@ -513,8 +536,7 @@ void w1_slave_found(unsigned long data, u64 rn)
513 sl->reg_num.crc == tmp->crc) { 536 sl->reg_num.crc == tmp->crc) {
514 set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags); 537 set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
515 break; 538 break;
516 } 539 } else if (sl->reg_num.family == tmp->family) {
517 else if (sl->reg_num.family == tmp->family) {
518 family_found = 1; 540 family_found = 1;
519 break; 541 break;
520 } 542 }
@@ -528,30 +550,43 @@ void w1_slave_found(unsigned long data, u64 rn)
528 rn && ((le64_to_cpu(rn) >> 56) & 0xff) == w1_calc_crc8((u8 *)&rn, 7)) { 550 rn && ((le64_to_cpu(rn) >> 56) & 0xff) == w1_calc_crc8((u8 *)&rn, 7)) {
529 w1_attach_slave_device(dev, tmp); 551 w1_attach_slave_device(dev, tmp);
530 } 552 }
531 553
532 atomic_dec(&dev->refcnt); 554 atomic_dec(&dev->refcnt);
533} 555}
534 556
535void w1_search(struct w1_master *dev) 557/**
558 * Performs a ROM Search & registers any devices found.
559 * The 1-wire search is a simple binary tree search.
560 * For each bit of the address, we read two bits and write one bit.
561 * The bit written will put to sleep all devies that don't match that bit.
562 * When the two reads differ, the direction choice is obvious.
563 * When both bits are 0, we must choose a path to take.
564 * When we can scan all 64 bits without having to choose a path, we are done.
565 *
566 * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com
567 *
568 * @dev The master device to search
569 * @cb Function to call when a device is found
570 */
571void w1_search(struct w1_master *dev, w1_slave_found_callback cb)
536{ 572{
537 u64 last, rn, tmp; 573 u64 last_rn, rn, tmp64;
538 int i, count = 0; 574 int i, slave_count = 0;
539 int last_family_desc, last_zero, last_device; 575 int last_zero, last_device;
540 int search_bit, id_bit, comp_bit, desc_bit; 576 int search_bit, desc_bit;
577 u8 triplet_ret = 0;
541 578
542 search_bit = id_bit = comp_bit = 0; 579 search_bit = 0;
543 rn = tmp = last = 0; 580 rn = last_rn = 0;
544 last_device = last_zero = last_family_desc = 0; 581 last_device = 0;
582 last_zero = -1;
545 583
546 desc_bit = 64; 584 desc_bit = 64;
547 585
548 while (!(id_bit && comp_bit) && !last_device 586 while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
549 && count++ < dev->max_slave_count) { 587 last_rn = rn;
550 last = rn;
551 rn = 0; 588 rn = 0;
552 589
553 last_family_desc = 0;
554
555 /* 590 /*
556 * Reset bus and all 1-wire device state machines 591 * Reset bus and all 1-wire device state machines
557 * so they can respond to our requests. 592 * so they can respond to our requests.
@@ -563,94 +598,46 @@ void w1_search(struct w1_master *dev)
563 break; 598 break;
564 } 599 }
565 600
566#if 1 601 /* Start the search */
567 w1_write_8(dev, W1_SEARCH); 602 w1_write_8(dev, W1_SEARCH);
568 for (i = 0; i < 64; ++i) { 603 for (i = 0; i < 64; ++i) {
569 /* 604 /* Determine the direction/search bit */
570 * Read 2 bits from bus. 605 if (i == desc_bit)
571 * All who don't sleep must send ID bit and COMPLEMENT ID bit. 606 search_bit = 1; /* took the 0 path last time, so take the 1 path */
572 * They actually are ANDed between all senders. 607 else if (i > desc_bit)
573 */ 608 search_bit = 0; /* take the 0 path on the next branch */
574 id_bit = w1_touch_bit(dev, 1);
575 comp_bit = w1_touch_bit(dev, 1);
576
577 if (id_bit && comp_bit)
578 break;
579
580 if (id_bit == 0 && comp_bit == 0) {
581 if (i == desc_bit)
582 search_bit = 1;
583 else if (i > desc_bit)
584 search_bit = 0;
585 else
586 search_bit = ((last >> i) & 0x1);
587
588 if (search_bit == 0) {
589 last_zero = i;
590 if (last_zero < 9)
591 last_family_desc = last_zero;
592 }
593
594 }
595 else 609 else
596 search_bit = id_bit; 610 search_bit = ((last_rn >> i) & 0x1);
597 611
598 tmp = search_bit; 612 /** Read two bits and write one bit */
599 rn |= (tmp << i); 613 triplet_ret = w1_triplet(dev, search_bit);
600 614
601 /* 615 /* quit if no device responded */
602 * Write 1 bit to bus 616 if ( (triplet_ret & 0x03) == 0x03 )
603 * and make all who don't have "search_bit" in "i"'th position 617 break;
604 * in it's registration number sleep.
605 */
606 if (dev->bus_master->touch_bit)
607 w1_touch_bit(dev, search_bit);
608 else
609 w1_write_bit(dev, search_bit);
610 618
611 } 619 /* If both directions were valid, and we took the 0 path... */
612#endif 620 if (triplet_ret == 0)
621 last_zero = i;
613 622
614 if (desc_bit == last_zero) 623 /* extract the direction taken & update the device number */
615 last_device = 1; 624 tmp64 = (triplet_ret >> 2);
625 rn |= (tmp64 << i);
626 }
616 627
617 desc_bit = last_zero; 628 if ( (triplet_ret & 0x03) != 0x03 ) {
618 629 if ( (desc_bit == last_zero) || (last_zero < 0))
619 w1_slave_found(dev->bus_master->data, rn); 630 last_device = 1;
631 desc_bit = last_zero;
632 cb(dev->bus_master->data, rn);
633 }
620 } 634 }
621} 635}
622 636
623int w1_create_master_attributes(struct w1_master *dev) 637static int w1_control(void *data)
624{
625 if ( device_create_file(&dev->dev, &w1_master_attribute_slaves) < 0 ||
626 device_create_file(&dev->dev, &w1_master_attribute_slave_count) < 0 ||
627 device_create_file(&dev->dev, &w1_master_attribute_attempts) < 0 ||
628 device_create_file(&dev->dev, &w1_master_attribute_max_slave_count) < 0 ||
629 device_create_file(&dev->dev, &w1_master_attribute_timeout) < 0||
630 device_create_file(&dev->dev, &w1_master_attribute_pointer) < 0||
631 device_create_file(&dev->dev, &w1_master_attribute_name) < 0)
632 return -EINVAL;
633
634 return 0;
635}
636
637void w1_destroy_master_attributes(struct w1_master *dev)
638{ 638{
639 device_remove_file(&dev->dev, &w1_master_attribute_slaves); 639 struct w1_slave *sl, *sln;
640 device_remove_file(&dev->dev, &w1_master_attribute_slave_count); 640 struct w1_master *dev, *n;
641 device_remove_file(&dev->dev, &w1_master_attribute_attempts);
642 device_remove_file(&dev->dev, &w1_master_attribute_max_slave_count);
643 device_remove_file(&dev->dev, &w1_master_attribute_timeout);
644 device_remove_file(&dev->dev, &w1_master_attribute_pointer);
645 device_remove_file(&dev->dev, &w1_master_attribute_name);
646}
647
648
649int w1_control(void *data)
650{
651 struct w1_slave *sl;
652 struct w1_master *dev;
653 struct list_head *ent, *ment, *n, *mn;
654 int err, have_to_wait = 0; 641 int err, have_to_wait = 0;
655 642
656 daemonize("w1_control"); 643 daemonize("w1_control");
@@ -665,10 +652,8 @@ int w1_control(void *data)
665 if (signal_pending(current)) 652 if (signal_pending(current))
666 flush_signals(current); 653 flush_signals(current);
667 654
668 list_for_each_safe(ment, mn, &w1_masters) { 655 list_for_each_entry_safe(dev, n, &w1_masters, w1_master_entry) {
669 dev = list_entry(ment, struct w1_master, w1_master_entry); 656 if (!control_needs_exit && !dev->flags)
670
671 if (!control_needs_exit && !dev->need_exit)
672 continue; 657 continue;
673 /* 658 /*
674 * Little race: we can create thread but not set the flag. 659 * Little race: we can create thread but not set the flag.
@@ -679,12 +664,8 @@ int w1_control(void *data)
679 continue; 664 continue;
680 } 665 }
681 666
682 spin_lock(&w1_mlock);
683 list_del(&dev->w1_master_entry);
684 spin_unlock(&w1_mlock);
685
686 if (control_needs_exit) { 667 if (control_needs_exit) {
687 dev->need_exit = 1; 668 set_bit(W1_MASTER_NEED_EXIT, &dev->flags);
688 669
689 err = kill_proc(dev->kpid, SIGTERM, 1); 670 err = kill_proc(dev->kpid, SIGTERM, 1);
690 if (err) 671 if (err)
@@ -693,24 +674,42 @@ int w1_control(void *data)
693 dev->kpid); 674 dev->kpid);
694 } 675 }
695 676
696 wait_for_completion(&dev->dev_exited); 677 if (test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {
697 678 wait_for_completion(&dev->dev_exited);
698 list_for_each_safe(ent, n, &dev->slist) { 679 spin_lock_bh(&w1_mlock);
699 sl = list_entry(ent, struct w1_slave, w1_slave_entry); 680 list_del(&dev->w1_master_entry);
681 spin_unlock_bh(&w1_mlock);
700 682
701 if (!sl) 683 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
702 dev_warn(&dev->dev,
703 "%s: slave entry is NULL.\n",
704 __func__);
705 else {
706 list_del(&sl->w1_slave_entry); 684 list_del(&sl->w1_slave_entry);
707 685
708 w1_slave_detach(sl); 686 w1_slave_detach(sl);
709 kfree(sl); 687 kfree(sl);
710 } 688 }
689 w1_destroy_master_attributes(dev);
690 atomic_dec(&dev->refcnt);
691 continue;
692 }
693
694 if (test_bit(W1_MASTER_NEED_RECONNECT, &dev->flags)) {
695 dev_info(&dev->dev, "Reconnecting slaves in device %s.\n", dev->name);
696 down(&dev->mutex);
697 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
698 if (sl->family->fid == W1_FAMILY_DEFAULT) {
699 struct w1_reg_num rn;
700 list_del(&sl->w1_slave_entry);
701 w1_slave_detach(sl);
702
703 memcpy(&rn, &sl->reg_num, sizeof(rn));
704
705 kfree(sl);
706
707 w1_attach_slave_device(dev, &rn);
708 }
709 }
710 clear_bit(W1_MASTER_NEED_RECONNECT, &dev->flags);
711 up(&dev->mutex);
711 } 712 }
712 w1_destroy_master_attributes(dev);
713 atomic_dec(&dev->refcnt);
714 } 713 }
715 } 714 }
716 715
@@ -720,51 +719,50 @@ int w1_control(void *data)
720int w1_process(void *data) 719int w1_process(void *data)
721{ 720{
722 struct w1_master *dev = (struct w1_master *) data; 721 struct w1_master *dev = (struct w1_master *) data;
723 struct list_head *ent, *n; 722 struct w1_slave *sl, *sln;
724 struct w1_slave *sl;
725 723
726 daemonize("%s", dev->name); 724 daemonize("%s", dev->name);
727 allow_signal(SIGTERM); 725 allow_signal(SIGTERM);
728 726
729 while (!dev->need_exit) { 727 while (!test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) {
730 try_to_freeze(PF_FREEZE); 728 try_to_freeze(PF_FREEZE);
731 msleep_interruptible(w1_timeout * 1000); 729 msleep_interruptible(w1_timeout * 1000);
732 730
733 if (signal_pending(current)) 731 if (signal_pending(current))
734 flush_signals(current); 732 flush_signals(current);
735 733
736 if (dev->need_exit) 734 if (test_bit(W1_MASTER_NEED_EXIT, &dev->flags))
737 break; 735 break;
738 736
739 if (!dev->initialized) 737 if (!dev->initialized)
740 continue; 738 continue;
741 739
740 if (dev->search_count == 0)
741 continue;
742
742 if (down_interruptible(&dev->mutex)) 743 if (down_interruptible(&dev->mutex))
743 continue; 744 continue;
744 745
745 list_for_each_safe(ent, n, &dev->slist) { 746 list_for_each_entry(sl, &dev->slist, w1_slave_entry)
746 sl = list_entry(ent, struct w1_slave, w1_slave_entry); 747 clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
747 748
748 if (sl)
749 clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags);
750 }
751
752 w1_search_devices(dev, w1_slave_found); 749 w1_search_devices(dev, w1_slave_found);
753 750
754 list_for_each_safe(ent, n, &dev->slist) { 751 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
755 sl = list_entry(ent, struct w1_slave, w1_slave_entry); 752 if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) {
756
757 if (sl && !test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) {
758 list_del (&sl->w1_slave_entry); 753 list_del (&sl->w1_slave_entry);
759 754
760 w1_slave_detach (sl); 755 w1_slave_detach (sl);
761 kfree (sl); 756 kfree (sl);
762 757
763 dev->slave_count--; 758 dev->slave_count--;
764 } 759 } else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))
765 else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags))
766 sl->ttl = dev->slave_ttl; 760 sl->ttl = dev->slave_ttl;
767 } 761 }
762
763 if (dev->search_count > 0)
764 dev->search_count--;
765
768 up(&dev->mutex); 766 up(&dev->mutex);
769 } 767 }
770 768
@@ -774,7 +772,7 @@ int w1_process(void *data)
774 return 0; 772 return 0;
775} 773}
776 774
777int w1_init(void) 775static int w1_init(void)
778{ 776{
779 int retval; 777 int retval;
780 778
@@ -814,18 +812,14 @@ err_out_exit_init:
814 return retval; 812 return retval;
815} 813}
816 814
817void w1_fini(void) 815static void w1_fini(void)
818{ 816{
819 struct w1_master *dev; 817 struct w1_master *dev;
820 struct list_head *ent, *n;
821 818
822 list_for_each_safe(ent, n, &w1_masters) { 819 list_for_each_entry(dev, &w1_masters, w1_master_entry)
823 dev = list_entry(ent, struct w1_master, w1_master_entry);
824 __w1_remove_master_device(dev); 820 __w1_remove_master_device(dev);
825 }
826 821
827 control_needs_exit = 1; 822 control_needs_exit = 1;
828
829 wait_for_completion(&w1_control_complete); 823 wait_for_completion(&w1_control_complete);
830 824
831 driver_unregister(&w1_driver); 825 driver_unregister(&w1_driver);