1 /* 2 * w1.c 3 * 4 * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net> 5 * 6 * 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 #include <linux/kthread.h> 34 #include <linux/freezer.h> 35 36 #include <linux/atomic.h> 37 38 #include "w1.h" 39 #include "w1_log.h" 40 #include "w1_int.h" 41 #include "w1_family.h" 42 #include "w1_netlink.h" 43 44 MODULE_LICENSE("GPL"); 45 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>"); 46 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol."); 47 48 static int w1_timeout = 10; 49 int w1_max_slave_count = 10; 50 int w1_max_slave_ttl = 10; 51 52 module_param_named(timeout, w1_timeout, int, 0); 53 module_param_named(max_slave_count, w1_max_slave_count, int, 0); 54 module_param_named(slave_ttl, w1_max_slave_ttl, int, 0); 55 56 DEFINE_MUTEX(w1_mlock); 57 LIST_HEAD(w1_masters); 58 59 static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn); 60 61 static int w1_master_match(struct device *dev, struct device_driver *drv) 62 { 63 return 1; 64 } 65 66 static int w1_master_probe(struct device *dev) 67 { 68 return -ENODEV; 69 } 70 71 static void w1_master_release(struct device *dev) 72 { 73 struct w1_master *md = dev_to_w1_master(dev); 74 75 dev_dbg(dev, "%s: Releasing %s.\n", __func__, md->name); 76 memset(md, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master)); 77 kfree(md); 78 } 79 80 static void w1_slave_release(struct device *dev) 81 { 82 struct w1_slave *sl = dev_to_w1_slave(dev); 83 84 dev_dbg(dev, "%s: Releasing %s.\n", __func__, sl->name); 85 86 while (atomic_read(&sl->refcnt)) { 87 dev_dbg(dev, "Waiting for %s to become free: refcnt=%d.\n", 88 sl->name, atomic_read(&sl->refcnt)); 89 if (msleep_interruptible(1000)) 90 flush_signals(current); 91 } 92 93 w1_family_put(sl->family); 94 sl->master->slave_count--; 95 96 complete(&sl->released); 97 } 98 99 static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf) 100 { 101 struct w1_slave *sl = dev_to_w1_slave(dev); 102 103 return sprintf(buf, "%s\n", sl->name); 104 } 105 static DEVICE_ATTR_RO(name); 106 107 static ssize_t id_show(struct device *dev, 108 struct device_attribute *attr, char *buf) 109 { 110 struct w1_slave *sl = dev_to_w1_slave(dev); 111 ssize_t count = sizeof(sl->reg_num); 112 113 memcpy(buf, (u8 *)&sl->reg_num, count); 114 return count; 115 } 116 static DEVICE_ATTR_RO(id); 117 118 static struct attribute *w1_slave_attrs[] = { 119 &dev_attr_name.attr, 120 &dev_attr_id.attr, 121 NULL, 122 }; 123 ATTRIBUTE_GROUPS(w1_slave); 124 125 /* Default family */ 126 127 static ssize_t rw_write(struct file *filp, struct kobject *kobj, 128 struct bin_attribute *bin_attr, char *buf, loff_t off, 129 size_t count) 130 { 131 struct w1_slave *sl = kobj_to_w1_slave(kobj); 132 133 mutex_lock(&sl->master->mutex); 134 if (w1_reset_select_slave(sl)) { 135 count = 0; 136 goto out_up; 137 } 138 139 w1_write_block(sl->master, buf, count); 140 141 out_up: 142 mutex_unlock(&sl->master->mutex); 143 return count; 144 } 145 146 static ssize_t rw_read(struct file *filp, struct kobject *kobj, 147 struct bin_attribute *bin_attr, char *buf, loff_t off, 148 size_t count) 149 { 150 struct w1_slave *sl = kobj_to_w1_slave(kobj); 151 152 mutex_lock(&sl->master->mutex); 153 w1_read_block(sl->master, buf, count); 154 mutex_unlock(&sl->master->mutex); 155 return count; 156 } 157 158 static BIN_ATTR_RW(rw, PAGE_SIZE); 159 160 static struct bin_attribute *w1_slave_bin_attrs[] = { 161 &bin_attr_rw, 162 NULL, 163 }; 164 165 static const struct attribute_group w1_slave_default_group = { 166 .bin_attrs = w1_slave_bin_attrs, 167 }; 168 169 static const struct attribute_group *w1_slave_default_groups[] = { 170 &w1_slave_default_group, 171 NULL, 172 }; 173 174 static struct w1_family_ops w1_default_fops = { 175 .groups = w1_slave_default_groups, 176 }; 177 178 static struct w1_family w1_default_family = { 179 .fops = &w1_default_fops, 180 }; 181 182 static int w1_uevent(struct device *dev, struct kobj_uevent_env *env); 183 184 static struct bus_type w1_bus_type = { 185 .name = "w1", 186 .match = w1_master_match, 187 .uevent = w1_uevent, 188 }; 189 190 struct device_driver w1_master_driver = { 191 .name = "w1_master_driver", 192 .bus = &w1_bus_type, 193 .probe = w1_master_probe, 194 }; 195 196 struct device w1_master_device = { 197 .parent = NULL, 198 .bus = &w1_bus_type, 199 .init_name = "w1 bus master", 200 .driver = &w1_master_driver, 201 .release = &w1_master_release 202 }; 203 204 static struct device_driver w1_slave_driver = { 205 .name = "w1_slave_driver", 206 .bus = &w1_bus_type, 207 }; 208 209 #if 0 210 struct device w1_slave_device = { 211 .parent = NULL, 212 .bus = &w1_bus_type, 213 .init_name = "w1 bus slave", 214 .driver = &w1_slave_driver, 215 .release = &w1_slave_release 216 }; 217 #endif /* 0 */ 218 219 static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf) 220 { 221 struct w1_master *md = dev_to_w1_master(dev); 222 ssize_t count; 223 224 mutex_lock(&md->mutex); 225 count = sprintf(buf, "%s\n", md->name); 226 mutex_unlock(&md->mutex); 227 228 return count; 229 } 230 231 static ssize_t w1_master_attribute_store_search(struct device * dev, 232 struct device_attribute *attr, 233 const char * buf, size_t count) 234 { 235 long tmp; 236 struct w1_master *md = dev_to_w1_master(dev); 237 int ret; 238 239 ret = kstrtol(buf, 0, &tmp); 240 if (ret) 241 return ret; 242 243 mutex_lock(&md->mutex); 244 md->search_count = tmp; 245 mutex_unlock(&md->mutex); 246 wake_up_process(md->thread); 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 { 255 struct w1_master *md = dev_to_w1_master(dev); 256 ssize_t count; 257 258 mutex_lock(&md->mutex); 259 count = sprintf(buf, "%d\n", md->search_count); 260 mutex_unlock(&md->mutex); 261 262 return count; 263 } 264 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); 271 int ret; 272 273 ret = kstrtol(buf, 0, &tmp); 274 if (ret) 275 return ret; 276 277 mutex_lock(&md->mutex); 278 md->enable_pullup = tmp; 279 mutex_unlock(&md->mutex); 280 wake_up_process(md->thread); 281 282 return count; 283 } 284 285 static ssize_t w1_master_attribute_show_pullup(struct device *dev, 286 struct device_attribute *attr, 287 char *buf) 288 { 289 struct w1_master *md = dev_to_w1_master(dev); 290 ssize_t count; 291 292 mutex_lock(&md->mutex); 293 count = sprintf(buf, "%d\n", md->enable_pullup); 294 mutex_unlock(&md->mutex); 295 296 return count; 297 } 298 299 static ssize_t w1_master_attribute_show_pointer(struct device *dev, struct device_attribute *attr, char *buf) 300 { 301 struct w1_master *md = dev_to_w1_master(dev); 302 ssize_t count; 303 304 mutex_lock(&md->mutex); 305 count = sprintf(buf, "0x%p\n", md->bus_master); 306 mutex_unlock(&md->mutex); 307 return count; 308 } 309 310 static ssize_t w1_master_attribute_show_timeout(struct device *dev, struct device_attribute *attr, char *buf) 311 { 312 ssize_t count; 313 count = sprintf(buf, "%d\n", w1_timeout); 314 return count; 315 } 316 317 static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, struct device_attribute *attr, char *buf) 318 { 319 struct w1_master *md = dev_to_w1_master(dev); 320 ssize_t count; 321 322 mutex_lock(&md->mutex); 323 count = sprintf(buf, "%d\n", md->max_slave_count); 324 mutex_unlock(&md->mutex); 325 return count; 326 } 327 328 static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf) 329 { 330 struct w1_master *md = dev_to_w1_master(dev); 331 ssize_t count; 332 333 mutex_lock(&md->mutex); 334 count = sprintf(buf, "%lu\n", md->attempts); 335 mutex_unlock(&md->mutex); 336 return count; 337 } 338 339 static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf) 340 { 341 struct w1_master *md = dev_to_w1_master(dev); 342 ssize_t count; 343 344 mutex_lock(&md->mutex); 345 count = sprintf(buf, "%d\n", md->slave_count); 346 mutex_unlock(&md->mutex); 347 return count; 348 } 349 350 static ssize_t w1_master_attribute_show_slaves(struct device *dev, 351 struct device_attribute *attr, char *buf) 352 { 353 struct w1_master *md = dev_to_w1_master(dev); 354 int c = PAGE_SIZE; 355 356 mutex_lock(&md->mutex); 357 358 if (md->slave_count == 0) 359 c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n"); 360 else { 361 struct list_head *ent, *n; 362 struct w1_slave *sl; 363 364 list_for_each_safe(ent, n, &md->slist) { 365 sl = list_entry(ent, struct w1_slave, w1_slave_entry); 366 367 c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name); 368 } 369 } 370 371 mutex_unlock(&md->mutex); 372 373 return PAGE_SIZE - c; 374 } 375 376 static ssize_t w1_master_attribute_show_add(struct device *dev, 377 struct device_attribute *attr, char *buf) 378 { 379 int c = PAGE_SIZE; 380 c -= snprintf(buf+PAGE_SIZE - c, c, 381 "write device id xx-xxxxxxxxxxxx to add slave\n"); 382 return PAGE_SIZE - c; 383 } 384 385 static int w1_atoreg_num(struct device *dev, const char *buf, size_t count, 386 struct w1_reg_num *rn) 387 { 388 unsigned int family; 389 unsigned long long id; 390 int i; 391 u64 rn64_le; 392 393 /* The CRC value isn't read from the user because the sysfs directory 394 * doesn't include it and most messages from the bus search don't 395 * print it either. It would be unreasonable for the user to then 396 * provide it. 397 */ 398 const char *error_msg = "bad slave string format, expecting " 399 "ff-dddddddddddd\n"; 400 401 if (buf[2] != '-') { 402 dev_err(dev, "%s", error_msg); 403 return -EINVAL; 404 } 405 i = sscanf(buf, "%02x-%012llx", &family, &id); 406 if (i != 2) { 407 dev_err(dev, "%s", error_msg); 408 return -EINVAL; 409 } 410 rn->family = family; 411 rn->id = id; 412 413 rn64_le = cpu_to_le64(*(u64 *)rn); 414 rn->crc = w1_calc_crc8((u8 *)&rn64_le, 7); 415 416 #if 0 417 dev_info(dev, "With CRC device is %02x.%012llx.%02x.\n", 418 rn->family, (unsigned long long)rn->id, rn->crc); 419 #endif 420 421 return 0; 422 } 423 424 /* Searches the slaves in the w1_master and returns a pointer or NULL. 425 * Note: must hold the mutex 426 */ 427 static struct w1_slave *w1_slave_search_device(struct w1_master *dev, 428 struct w1_reg_num *rn) 429 { 430 struct w1_slave *sl; 431 list_for_each_entry(sl, &dev->slist, w1_slave_entry) { 432 if (sl->reg_num.family == rn->family && 433 sl->reg_num.id == rn->id && 434 sl->reg_num.crc == rn->crc) { 435 return sl; 436 } 437 } 438 return NULL; 439 } 440 441 static ssize_t w1_master_attribute_store_add(struct device *dev, 442 struct device_attribute *attr, 443 const char *buf, size_t count) 444 { 445 struct w1_master *md = dev_to_w1_master(dev); 446 struct w1_reg_num rn; 447 struct w1_slave *sl; 448 ssize_t result = count; 449 450 if (w1_atoreg_num(dev, buf, count, &rn)) 451 return -EINVAL; 452 453 mutex_lock(&md->mutex); 454 sl = w1_slave_search_device(md, &rn); 455 /* It would be nice to do a targeted search one the one-wire bus 456 * for the new device to see if it is out there or not. But the 457 * current search doesn't support that. 458 */ 459 if (sl) { 460 dev_info(dev, "Device %s already exists\n", sl->name); 461 result = -EINVAL; 462 } else { 463 w1_attach_slave_device(md, &rn); 464 } 465 mutex_unlock(&md->mutex); 466 467 return result; 468 } 469 470 static ssize_t w1_master_attribute_show_remove(struct device *dev, 471 struct device_attribute *attr, char *buf) 472 { 473 int c = PAGE_SIZE; 474 c -= snprintf(buf+PAGE_SIZE - c, c, 475 "write device id xx-xxxxxxxxxxxx to remove slave\n"); 476 return PAGE_SIZE - c; 477 } 478 479 static ssize_t w1_master_attribute_store_remove(struct device *dev, 480 struct device_attribute *attr, 481 const char *buf, size_t count) 482 { 483 struct w1_master *md = dev_to_w1_master(dev); 484 struct w1_reg_num rn; 485 struct w1_slave *sl; 486 ssize_t result = count; 487 488 if (w1_atoreg_num(dev, buf, count, &rn)) 489 return -EINVAL; 490 491 mutex_lock(&md->mutex); 492 sl = w1_slave_search_device(md, &rn); 493 if (sl) { 494 w1_slave_detach(sl); 495 } else { 496 dev_info(dev, "Device %02x-%012llx doesn't exists\n", rn.family, 497 (unsigned long long)rn.id); 498 result = -EINVAL; 499 } 500 mutex_unlock(&md->mutex); 501 502 return result; 503 } 504 505 #define W1_MASTER_ATTR_RO(_name, _mode) \ 506 struct device_attribute w1_master_attribute_##_name = \ 507 __ATTR(w1_master_##_name, _mode, \ 508 w1_master_attribute_show_##_name, NULL) 509 510 #define W1_MASTER_ATTR_RW(_name, _mode) \ 511 struct device_attribute w1_master_attribute_##_name = \ 512 __ATTR(w1_master_##_name, _mode, \ 513 w1_master_attribute_show_##_name, \ 514 w1_master_attribute_store_##_name) 515 516 static W1_MASTER_ATTR_RO(name, S_IRUGO); 517 static W1_MASTER_ATTR_RO(slaves, S_IRUGO); 518 static W1_MASTER_ATTR_RO(slave_count, S_IRUGO); 519 static W1_MASTER_ATTR_RO(max_slave_count, S_IRUGO); 520 static W1_MASTER_ATTR_RO(attempts, S_IRUGO); 521 static W1_MASTER_ATTR_RO(timeout, S_IRUGO); 522 static W1_MASTER_ATTR_RO(pointer, S_IRUGO); 523 static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUSR | S_IWGRP); 524 static W1_MASTER_ATTR_RW(pullup, S_IRUGO | S_IWUSR | S_IWGRP); 525 static W1_MASTER_ATTR_RW(add, S_IRUGO | S_IWUSR | S_IWGRP); 526 static W1_MASTER_ATTR_RW(remove, S_IRUGO | S_IWUSR | S_IWGRP); 527 528 static struct attribute *w1_master_default_attrs[] = { 529 &w1_master_attribute_name.attr, 530 &w1_master_attribute_slaves.attr, 531 &w1_master_attribute_slave_count.attr, 532 &w1_master_attribute_max_slave_count.attr, 533 &w1_master_attribute_attempts.attr, 534 &w1_master_attribute_timeout.attr, 535 &w1_master_attribute_pointer.attr, 536 &w1_master_attribute_search.attr, 537 &w1_master_attribute_pullup.attr, 538 &w1_master_attribute_add.attr, 539 &w1_master_attribute_remove.attr, 540 NULL 541 }; 542 543 static struct attribute_group w1_master_defattr_group = { 544 .attrs = w1_master_default_attrs, 545 }; 546 547 int w1_create_master_attributes(struct w1_master *master) 548 { 549 return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group); 550 } 551 552 void w1_destroy_master_attributes(struct w1_master *master) 553 { 554 sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group); 555 } 556 557 static int w1_uevent(struct device *dev, struct kobj_uevent_env *env) 558 { 559 struct w1_master *md = NULL; 560 struct w1_slave *sl = NULL; 561 char *event_owner, *name; 562 int err = 0; 563 564 if (dev->driver == &w1_master_driver) { 565 md = container_of(dev, struct w1_master, dev); 566 event_owner = "master"; 567 name = md->name; 568 } else if (dev->driver == &w1_slave_driver) { 569 sl = container_of(dev, struct w1_slave, dev); 570 event_owner = "slave"; 571 name = sl->name; 572 } else { 573 dev_dbg(dev, "Unknown event.\n"); 574 return -EINVAL; 575 } 576 577 dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n", 578 event_owner, name, dev_name(dev)); 579 580 if (dev->driver != &w1_slave_driver || !sl) 581 goto end; 582 583 err = add_uevent_var(env, "W1_FID=%02X", sl->reg_num.family); 584 if (err) 585 goto end; 586 587 err = add_uevent_var(env, "W1_SLAVE_ID=%024LX", 588 (unsigned long long)sl->reg_num.id); 589 end: 590 return err; 591 } 592 593 /* 594 * Handle sysfs file creation and removal here, before userspace is told that 595 * the device is added / removed from the system 596 */ 597 static int w1_bus_notify(struct notifier_block *nb, unsigned long action, 598 void *data) 599 { 600 struct device *dev = data; 601 struct w1_slave *sl; 602 struct w1_family_ops *fops; 603 int err; 604 605 /* 606 * Only care about slave devices at the moment. Yes, we should use a 607 * separate "type" for this, but for now, look at the release function 608 * to know which type it is... 609 */ 610 if (dev->release != w1_slave_release) 611 return 0; 612 613 sl = dev_to_w1_slave(dev); 614 fops = sl->family->fops; 615 616 switch (action) { 617 case BUS_NOTIFY_ADD_DEVICE: 618 /* if the family driver needs to initialize something... */ 619 if (fops->add_slave) { 620 err = fops->add_slave(sl); 621 if (err < 0) { 622 dev_err(&sl->dev, 623 "add_slave() call failed. err=%d\n", 624 err); 625 return err; 626 } 627 } 628 if (fops->groups) { 629 err = sysfs_create_groups(&sl->dev.kobj, fops->groups); 630 if (err) { 631 dev_err(&sl->dev, 632 "sysfs group creation failed. err=%d\n", 633 err); 634 return err; 635 } 636 } 637 638 break; 639 case BUS_NOTIFY_DEL_DEVICE: 640 if (fops->remove_slave) 641 sl->family->fops->remove_slave(sl); 642 if (fops->groups) 643 sysfs_remove_groups(&sl->dev.kobj, fops->groups); 644 break; 645 } 646 return 0; 647 } 648 649 static struct notifier_block w1_bus_nb = { 650 .notifier_call = w1_bus_notify, 651 }; 652 653 static int __w1_attach_slave_device(struct w1_slave *sl) 654 { 655 int err; 656 657 sl->dev.parent = &sl->master->dev; 658 sl->dev.driver = &w1_slave_driver; 659 sl->dev.bus = &w1_bus_type; 660 sl->dev.release = &w1_slave_release; 661 sl->dev.groups = w1_slave_groups; 662 663 dev_set_name(&sl->dev, "%02x-%012llx", 664 (unsigned int) sl->reg_num.family, 665 (unsigned long long) sl->reg_num.id); 666 snprintf(&sl->name[0], sizeof(sl->name), 667 "%02x-%012llx", 668 (unsigned int) sl->reg_num.family, 669 (unsigned long long) sl->reg_num.id); 670 671 dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__, 672 dev_name(&sl->dev), sl); 673 674 err = device_register(&sl->dev); 675 if (err < 0) { 676 dev_err(&sl->dev, 677 "Device registration [%s] failed. err=%d\n", 678 dev_name(&sl->dev), err); 679 return err; 680 } 681 682 683 dev_set_uevent_suppress(&sl->dev, false); 684 kobject_uevent(&sl->dev.kobj, KOBJ_ADD); 685 686 list_add_tail(&sl->w1_slave_entry, &sl->master->slist); 687 688 return 0; 689 } 690 691 static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn) 692 { 693 struct w1_slave *sl; 694 struct w1_family *f; 695 int err; 696 struct w1_netlink_msg msg; 697 698 sl = kzalloc(sizeof(struct w1_slave), GFP_KERNEL); 699 if (!sl) { 700 dev_err(&dev->dev, 701 "%s: failed to allocate new slave device.\n", 702 __func__); 703 return -ENOMEM; 704 } 705 706 707 sl->owner = THIS_MODULE; 708 sl->master = dev; 709 set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags); 710 711 memset(&msg, 0, sizeof(msg)); 712 memcpy(&sl->reg_num, rn, sizeof(sl->reg_num)); 713 atomic_set(&sl->refcnt, 0); 714 init_completion(&sl->released); 715 716 request_module("w1-family-0x%0x", rn->family); 717 718 spin_lock(&w1_flock); 719 f = w1_family_registered(rn->family); 720 if (!f) { 721 f= &w1_default_family; 722 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n", 723 rn->family, rn->family, 724 (unsigned long long)rn->id, rn->crc); 725 } 726 __w1_family_get(f); 727 spin_unlock(&w1_flock); 728 729 sl->family = f; 730 731 732 err = __w1_attach_slave_device(sl); 733 if (err < 0) { 734 dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__, 735 sl->name); 736 w1_family_put(sl->family); 737 kfree(sl); 738 return err; 739 } 740 741 sl->ttl = dev->slave_ttl; 742 dev->slave_count++; 743 744 memcpy(msg.id.id, rn, sizeof(msg.id)); 745 msg.type = W1_SLAVE_ADD; 746 w1_netlink_send(dev, &msg); 747 748 return 0; 749 } 750 751 void w1_slave_detach(struct w1_slave *sl) 752 { 753 struct w1_netlink_msg msg; 754 755 dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__, sl->name, sl); 756 757 list_del(&sl->w1_slave_entry); 758 759 memset(&msg, 0, sizeof(msg)); 760 memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id)); 761 msg.type = W1_SLAVE_REMOVE; 762 w1_netlink_send(sl->master, &msg); 763 764 device_unregister(&sl->dev); 765 766 wait_for_completion(&sl->released); 767 kfree(sl); 768 } 769 770 struct w1_master *w1_search_master_id(u32 id) 771 { 772 struct w1_master *dev; 773 int found = 0; 774 775 mutex_lock(&w1_mlock); 776 list_for_each_entry(dev, &w1_masters, w1_master_entry) { 777 if (dev->id == id) { 778 found = 1; 779 atomic_inc(&dev->refcnt); 780 break; 781 } 782 } 783 mutex_unlock(&w1_mlock); 784 785 return (found)?dev:NULL; 786 } 787 788 struct w1_slave *w1_search_slave(struct w1_reg_num *id) 789 { 790 struct w1_master *dev; 791 struct w1_slave *sl = NULL; 792 int found = 0; 793 794 mutex_lock(&w1_mlock); 795 list_for_each_entry(dev, &w1_masters, w1_master_entry) { 796 mutex_lock(&dev->mutex); 797 list_for_each_entry(sl, &dev->slist, w1_slave_entry) { 798 if (sl->reg_num.family == id->family && 799 sl->reg_num.id == id->id && 800 sl->reg_num.crc == id->crc) { 801 found = 1; 802 atomic_inc(&dev->refcnt); 803 atomic_inc(&sl->refcnt); 804 break; 805 } 806 } 807 mutex_unlock(&dev->mutex); 808 809 if (found) 810 break; 811 } 812 mutex_unlock(&w1_mlock); 813 814 return (found)?sl:NULL; 815 } 816 817 void w1_reconnect_slaves(struct w1_family *f, int attach) 818 { 819 struct w1_slave *sl, *sln; 820 struct w1_master *dev; 821 822 mutex_lock(&w1_mlock); 823 list_for_each_entry(dev, &w1_masters, w1_master_entry) { 824 dev_dbg(&dev->dev, "Reconnecting slaves in device %s " 825 "for family %02x.\n", dev->name, f->fid); 826 mutex_lock(&dev->mutex); 827 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) { 828 /* If it is a new family, slaves with the default 829 * family driver and are that family will be 830 * connected. If the family is going away, devices 831 * matching that family are reconneced. 832 */ 833 if ((attach && sl->family->fid == W1_FAMILY_DEFAULT 834 && sl->reg_num.family == f->fid) || 835 (!attach && sl->family->fid == f->fid)) { 836 struct w1_reg_num rn; 837 838 memcpy(&rn, &sl->reg_num, sizeof(rn)); 839 w1_slave_detach(sl); 840 841 w1_attach_slave_device(dev, &rn); 842 } 843 } 844 dev_dbg(&dev->dev, "Reconnecting slaves in device %s " 845 "has been finished.\n", dev->name); 846 mutex_unlock(&dev->mutex); 847 } 848 mutex_unlock(&w1_mlock); 849 } 850 851 void w1_slave_found(struct w1_master *dev, u64 rn) 852 { 853 struct w1_slave *sl; 854 struct w1_reg_num *tmp; 855 u64 rn_le = cpu_to_le64(rn); 856 857 atomic_inc(&dev->refcnt); 858 859 tmp = (struct w1_reg_num *) &rn; 860 861 sl = w1_slave_search_device(dev, tmp); 862 if (sl) { 863 set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags); 864 } else { 865 if (rn && tmp->crc == w1_calc_crc8((u8 *)&rn_le, 7)) 866 w1_attach_slave_device(dev, tmp); 867 } 868 869 atomic_dec(&dev->refcnt); 870 } 871 872 /** 873 * Performs a ROM Search & registers any devices found. 874 * The 1-wire search is a simple binary tree search. 875 * For each bit of the address, we read two bits and write one bit. 876 * The bit written will put to sleep all devies that don't match that bit. 877 * When the two reads differ, the direction choice is obvious. 878 * When both bits are 0, we must choose a path to take. 879 * When we can scan all 64 bits without having to choose a path, we are done. 880 * 881 * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com 882 * 883 * @dev The master device to search 884 * @cb Function to call when a device is found 885 */ 886 void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb) 887 { 888 u64 last_rn, rn, tmp64; 889 int i, slave_count = 0; 890 int last_zero, last_device; 891 int search_bit, desc_bit; 892 u8 triplet_ret = 0; 893 894 search_bit = 0; 895 rn = last_rn = 0; 896 last_device = 0; 897 last_zero = -1; 898 899 desc_bit = 64; 900 901 while ( !last_device && (slave_count++ < dev->max_slave_count) ) { 902 last_rn = rn; 903 rn = 0; 904 905 /* 906 * Reset bus and all 1-wire device state machines 907 * so they can respond to our requests. 908 * 909 * Return 0 - device(s) present, 1 - no devices present. 910 */ 911 mutex_lock(&dev->bus_mutex); 912 if (w1_reset_bus(dev)) { 913 mutex_unlock(&dev->bus_mutex); 914 dev_dbg(&dev->dev, "No devices present on the wire.\n"); 915 break; 916 } 917 918 /* Do fast search on single slave bus */ 919 if (dev->max_slave_count == 1) { 920 int rv; 921 w1_write_8(dev, W1_READ_ROM); 922 rv = w1_read_block(dev, (u8 *)&rn, 8); 923 mutex_unlock(&dev->bus_mutex); 924 925 if (rv == 8 && rn) 926 cb(dev, rn); 927 928 break; 929 } 930 931 /* Start the search */ 932 w1_write_8(dev, search_type); 933 for (i = 0; i < 64; ++i) { 934 /* Determine the direction/search bit */ 935 if (i == desc_bit) 936 search_bit = 1; /* took the 0 path last time, so take the 1 path */ 937 else if (i > desc_bit) 938 search_bit = 0; /* take the 0 path on the next branch */ 939 else 940 search_bit = ((last_rn >> i) & 0x1); 941 942 /** Read two bits and write one bit */ 943 triplet_ret = w1_triplet(dev, search_bit); 944 945 /* quit if no device responded */ 946 if ( (triplet_ret & 0x03) == 0x03 ) 947 break; 948 949 /* If both directions were valid, and we took the 0 path... */ 950 if (triplet_ret == 0) 951 last_zero = i; 952 953 /* extract the direction taken & update the device number */ 954 tmp64 = (triplet_ret >> 2); 955 rn |= (tmp64 << i); 956 957 /* ensure we're called from kthread and not by netlink callback */ 958 if (!dev->priv && kthread_should_stop()) { 959 mutex_unlock(&dev->bus_mutex); 960 dev_dbg(&dev->dev, "Abort w1_search\n"); 961 return; 962 } 963 } 964 mutex_unlock(&dev->bus_mutex); 965 966 if ( (triplet_ret & 0x03) != 0x03 ) { 967 if ( (desc_bit == last_zero) || (last_zero < 0)) 968 last_device = 1; 969 desc_bit = last_zero; 970 cb(dev, rn); 971 } 972 } 973 } 974 975 void w1_search_process_cb(struct w1_master *dev, u8 search_type, 976 w1_slave_found_callback cb) 977 { 978 struct w1_slave *sl, *sln; 979 980 list_for_each_entry(sl, &dev->slist, w1_slave_entry) 981 clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags); 982 983 w1_search_devices(dev, search_type, cb); 984 985 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) { 986 if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) 987 w1_slave_detach(sl); 988 else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags)) 989 sl->ttl = dev->slave_ttl; 990 } 991 992 if (dev->search_count > 0) 993 dev->search_count--; 994 } 995 996 static void w1_search_process(struct w1_master *dev, u8 search_type) 997 { 998 w1_search_process_cb(dev, search_type, w1_slave_found); 999 } 1000 1001 int w1_process(void *data) 1002 { 1003 struct w1_master *dev = (struct w1_master *) data; 1004 /* As long as w1_timeout is only set by a module parameter the sleep 1005 * time can be calculated in jiffies once. 1006 */ 1007 const unsigned long jtime = msecs_to_jiffies(w1_timeout * 1000); 1008 1009 while (!kthread_should_stop()) { 1010 if (dev->search_count) { 1011 mutex_lock(&dev->mutex); 1012 w1_search_process(dev, W1_SEARCH); 1013 mutex_unlock(&dev->mutex); 1014 } 1015 1016 try_to_freeze(); 1017 __set_current_state(TASK_INTERRUPTIBLE); 1018 1019 if (kthread_should_stop()) 1020 break; 1021 1022 /* Only sleep when the search is active. */ 1023 if (dev->search_count) 1024 schedule_timeout(jtime); 1025 else 1026 schedule(); 1027 } 1028 1029 atomic_dec(&dev->refcnt); 1030 1031 return 0; 1032 } 1033 1034 static int __init w1_init(void) 1035 { 1036 int retval; 1037 1038 printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n"); 1039 1040 w1_init_netlink(); 1041 1042 retval = bus_register(&w1_bus_type); 1043 if (retval) { 1044 printk(KERN_ERR "Failed to register bus. err=%d.\n", retval); 1045 goto err_out_exit_init; 1046 } 1047 1048 retval = bus_register_notifier(&w1_bus_type, &w1_bus_nb); 1049 if (retval) 1050 goto err_out_bus_unregister; 1051 1052 retval = driver_register(&w1_master_driver); 1053 if (retval) { 1054 printk(KERN_ERR 1055 "Failed to register master driver. err=%d.\n", 1056 retval); 1057 goto err_out_bus_unregister; 1058 } 1059 1060 retval = driver_register(&w1_slave_driver); 1061 if (retval) { 1062 printk(KERN_ERR 1063 "Failed to register slave driver. err=%d.\n", 1064 retval); 1065 goto err_out_master_unregister; 1066 } 1067 1068 return 0; 1069 1070 #if 0 1071 /* For undoing the slave register if there was a step after it. */ 1072 err_out_slave_unregister: 1073 driver_unregister(&w1_slave_driver); 1074 #endif 1075 1076 err_out_master_unregister: 1077 driver_unregister(&w1_master_driver); 1078 1079 err_out_bus_unregister: 1080 bus_unregister(&w1_bus_type); 1081 1082 err_out_exit_init: 1083 return retval; 1084 } 1085 1086 static void __exit w1_fini(void) 1087 { 1088 struct w1_master *dev; 1089 1090 /* Set netlink removal messages and some cleanup */ 1091 list_for_each_entry(dev, &w1_masters, w1_master_entry) 1092 __w1_remove_master_device(dev); 1093 1094 w1_fini_netlink(); 1095 1096 driver_unregister(&w1_slave_driver); 1097 driver_unregister(&w1_master_driver); 1098 bus_unregister(&w1_bus_type); 1099 } 1100 1101 module_init(w1_init); 1102 module_exit(w1_fini); 1103