0001
0002
0003
0004
0005
0006 #include <linux/delay.h>
0007 #include <linux/kernel.h>
0008 #include <linux/module.h>
0009 #include <linux/moduleparam.h>
0010 #include <linux/list.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/spinlock.h>
0013 #include <linux/timer.h>
0014 #include <linux/device.h>
0015 #include <linux/slab.h>
0016 #include <linux/sched.h>
0017 #include <linux/kthread.h>
0018 #include <linux/freezer.h>
0019 #include <linux/hwmon.h>
0020 #include <linux/of.h>
0021
0022 #include <linux/atomic.h>
0023
0024 #include "w1_internal.h"
0025 #include "w1_netlink.h"
0026
0027 #define W1_FAMILY_DEFAULT 0
0028 #define W1_FAMILY_DS28E04 0x1C
0029
0030
0031 static int w1_timeout = 10;
0032 module_param_named(timeout, w1_timeout, int, 0);
0033 MODULE_PARM_DESC(timeout, "time in seconds between automatic slave searches");
0034
0035 static int w1_timeout_us = 0;
0036 module_param_named(timeout_us, w1_timeout_us, int, 0);
0037 MODULE_PARM_DESC(timeout_us,
0038 "time in microseconds between automatic slave searches");
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 int w1_max_slave_count = 64;
0049 module_param_named(max_slave_count, w1_max_slave_count, int, 0);
0050 MODULE_PARM_DESC(max_slave_count,
0051 "maximum number of slaves detected in a search");
0052
0053 int w1_max_slave_ttl = 10;
0054 module_param_named(slave_ttl, w1_max_slave_ttl, int, 0);
0055 MODULE_PARM_DESC(slave_ttl,
0056 "Number of searches not seeing a slave before it will be removed");
0057
0058 DEFINE_MUTEX(w1_mlock);
0059 LIST_HEAD(w1_masters);
0060
0061 static int w1_master_match(struct device *dev, struct device_driver *drv)
0062 {
0063 return 1;
0064 }
0065
0066 static int w1_master_probe(struct device *dev)
0067 {
0068 return -ENODEV;
0069 }
0070
0071 static void w1_master_release(struct device *dev)
0072 {
0073 struct w1_master *md = dev_to_w1_master(dev);
0074
0075 dev_dbg(dev, "%s: Releasing %s.\n", __func__, md->name);
0076 memset(md, 0, sizeof(struct w1_master) + sizeof(struct w1_bus_master));
0077 kfree(md);
0078 }
0079
0080 static void w1_slave_release(struct device *dev)
0081 {
0082 struct w1_slave *sl = dev_to_w1_slave(dev);
0083
0084 dev_dbg(dev, "%s: Releasing %s [%p]\n", __func__, sl->name, sl);
0085
0086 w1_family_put(sl->family);
0087 sl->master->slave_count--;
0088 }
0089
0090 static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf)
0091 {
0092 struct w1_slave *sl = dev_to_w1_slave(dev);
0093
0094 return sprintf(buf, "%s\n", sl->name);
0095 }
0096 static DEVICE_ATTR_RO(name);
0097
0098 static ssize_t id_show(struct device *dev,
0099 struct device_attribute *attr, char *buf)
0100 {
0101 struct w1_slave *sl = dev_to_w1_slave(dev);
0102 ssize_t count = sizeof(sl->reg_num);
0103
0104 memcpy(buf, (u8 *)&sl->reg_num, count);
0105 return count;
0106 }
0107 static DEVICE_ATTR_RO(id);
0108
0109 static struct attribute *w1_slave_attrs[] = {
0110 &dev_attr_name.attr,
0111 &dev_attr_id.attr,
0112 NULL,
0113 };
0114 ATTRIBUTE_GROUPS(w1_slave);
0115
0116
0117
0118 static ssize_t rw_write(struct file *filp, struct kobject *kobj,
0119 struct bin_attribute *bin_attr, char *buf, loff_t off,
0120 size_t count)
0121 {
0122 struct w1_slave *sl = kobj_to_w1_slave(kobj);
0123
0124 mutex_lock(&sl->master->mutex);
0125 if (w1_reset_select_slave(sl)) {
0126 count = 0;
0127 goto out_up;
0128 }
0129
0130 w1_write_block(sl->master, buf, count);
0131
0132 out_up:
0133 mutex_unlock(&sl->master->mutex);
0134 return count;
0135 }
0136
0137 static ssize_t rw_read(struct file *filp, struct kobject *kobj,
0138 struct bin_attribute *bin_attr, char *buf, loff_t off,
0139 size_t count)
0140 {
0141 struct w1_slave *sl = kobj_to_w1_slave(kobj);
0142
0143 mutex_lock(&sl->master->mutex);
0144 w1_read_block(sl->master, buf, count);
0145 mutex_unlock(&sl->master->mutex);
0146 return count;
0147 }
0148
0149 static BIN_ATTR_RW(rw, PAGE_SIZE);
0150
0151 static struct bin_attribute *w1_slave_bin_attrs[] = {
0152 &bin_attr_rw,
0153 NULL,
0154 };
0155
0156 static const struct attribute_group w1_slave_default_group = {
0157 .bin_attrs = w1_slave_bin_attrs,
0158 };
0159
0160 static const struct attribute_group *w1_slave_default_groups[] = {
0161 &w1_slave_default_group,
0162 NULL,
0163 };
0164
0165 static const struct w1_family_ops w1_default_fops = {
0166 .groups = w1_slave_default_groups,
0167 };
0168
0169 static struct w1_family w1_default_family = {
0170 .fops = &w1_default_fops,
0171 };
0172
0173 static int w1_uevent(struct device *dev, struct kobj_uevent_env *env);
0174
0175 static struct bus_type w1_bus_type = {
0176 .name = "w1",
0177 .match = w1_master_match,
0178 .uevent = w1_uevent,
0179 };
0180
0181 struct device_driver w1_master_driver = {
0182 .name = "w1_master_driver",
0183 .bus = &w1_bus_type,
0184 .probe = w1_master_probe,
0185 };
0186
0187 struct device w1_master_device = {
0188 .parent = NULL,
0189 .bus = &w1_bus_type,
0190 .init_name = "w1 bus master",
0191 .driver = &w1_master_driver,
0192 .release = &w1_master_release
0193 };
0194
0195 static struct device_driver w1_slave_driver = {
0196 .name = "w1_slave_driver",
0197 .bus = &w1_bus_type,
0198 };
0199
0200 #if 0
0201 struct device w1_slave_device = {
0202 .parent = NULL,
0203 .bus = &w1_bus_type,
0204 .init_name = "w1 bus slave",
0205 .driver = &w1_slave_driver,
0206 .release = &w1_slave_release
0207 };
0208 #endif
0209
0210 static ssize_t w1_master_attribute_show_name(struct device *dev, struct device_attribute *attr, char *buf)
0211 {
0212 struct w1_master *md = dev_to_w1_master(dev);
0213 ssize_t count;
0214
0215 mutex_lock(&md->mutex);
0216 count = sprintf(buf, "%s\n", md->name);
0217 mutex_unlock(&md->mutex);
0218
0219 return count;
0220 }
0221
0222 static ssize_t w1_master_attribute_store_search(struct device * dev,
0223 struct device_attribute *attr,
0224 const char * buf, size_t count)
0225 {
0226 long tmp;
0227 struct w1_master *md = dev_to_w1_master(dev);
0228 int ret;
0229
0230 ret = kstrtol(buf, 0, &tmp);
0231 if (ret)
0232 return ret;
0233
0234 mutex_lock(&md->mutex);
0235 md->search_count = tmp;
0236 mutex_unlock(&md->mutex);
0237
0238 if (tmp)
0239 wake_up_process(md->thread);
0240
0241 return count;
0242 }
0243
0244 static ssize_t w1_master_attribute_show_search(struct device *dev,
0245 struct device_attribute *attr,
0246 char *buf)
0247 {
0248 struct w1_master *md = dev_to_w1_master(dev);
0249 ssize_t count;
0250
0251 mutex_lock(&md->mutex);
0252 count = sprintf(buf, "%d\n", md->search_count);
0253 mutex_unlock(&md->mutex);
0254
0255 return count;
0256 }
0257
0258 static ssize_t w1_master_attribute_store_pullup(struct device *dev,
0259 struct device_attribute *attr,
0260 const char *buf, size_t count)
0261 {
0262 long tmp;
0263 struct w1_master *md = dev_to_w1_master(dev);
0264 int ret;
0265
0266 ret = kstrtol(buf, 0, &tmp);
0267 if (ret)
0268 return ret;
0269
0270 mutex_lock(&md->mutex);
0271 md->enable_pullup = tmp;
0272 mutex_unlock(&md->mutex);
0273
0274 return count;
0275 }
0276
0277 static ssize_t w1_master_attribute_show_pullup(struct device *dev,
0278 struct device_attribute *attr,
0279 char *buf)
0280 {
0281 struct w1_master *md = dev_to_w1_master(dev);
0282 ssize_t count;
0283
0284 mutex_lock(&md->mutex);
0285 count = sprintf(buf, "%d\n", md->enable_pullup);
0286 mutex_unlock(&md->mutex);
0287
0288 return count;
0289 }
0290
0291 static ssize_t w1_master_attribute_show_pointer(struct device *dev, struct device_attribute *attr, char *buf)
0292 {
0293 struct w1_master *md = dev_to_w1_master(dev);
0294 ssize_t count;
0295
0296 mutex_lock(&md->mutex);
0297 count = sprintf(buf, "0x%p\n", md->bus_master);
0298 mutex_unlock(&md->mutex);
0299 return count;
0300 }
0301
0302 static ssize_t w1_master_attribute_show_timeout(struct device *dev, struct device_attribute *attr, char *buf)
0303 {
0304 ssize_t count;
0305 count = sprintf(buf, "%d\n", w1_timeout);
0306 return count;
0307 }
0308
0309 static ssize_t w1_master_attribute_show_timeout_us(struct device *dev,
0310 struct device_attribute *attr, char *buf)
0311 {
0312 ssize_t count;
0313 count = sprintf(buf, "%d\n", w1_timeout_us);
0314 return count;
0315 }
0316
0317 static ssize_t w1_master_attribute_store_max_slave_count(struct device *dev,
0318 struct device_attribute *attr, const char *buf, size_t count)
0319 {
0320 int tmp;
0321 struct w1_master *md = dev_to_w1_master(dev);
0322
0323 if (kstrtoint(buf, 0, &tmp) || tmp < 1)
0324 return -EINVAL;
0325
0326 mutex_lock(&md->mutex);
0327 md->max_slave_count = tmp;
0328
0329 clear_bit(W1_WARN_MAX_COUNT, &md->flags);
0330 mutex_unlock(&md->mutex);
0331
0332 return count;
0333 }
0334
0335 static ssize_t w1_master_attribute_show_max_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
0336 {
0337 struct w1_master *md = dev_to_w1_master(dev);
0338 ssize_t count;
0339
0340 mutex_lock(&md->mutex);
0341 count = sprintf(buf, "%d\n", md->max_slave_count);
0342 mutex_unlock(&md->mutex);
0343 return count;
0344 }
0345
0346 static ssize_t w1_master_attribute_show_attempts(struct device *dev, struct device_attribute *attr, char *buf)
0347 {
0348 struct w1_master *md = dev_to_w1_master(dev);
0349 ssize_t count;
0350
0351 mutex_lock(&md->mutex);
0352 count = sprintf(buf, "%lu\n", md->attempts);
0353 mutex_unlock(&md->mutex);
0354 return count;
0355 }
0356
0357 static ssize_t w1_master_attribute_show_slave_count(struct device *dev, struct device_attribute *attr, char *buf)
0358 {
0359 struct w1_master *md = dev_to_w1_master(dev);
0360 ssize_t count;
0361
0362 mutex_lock(&md->mutex);
0363 count = sprintf(buf, "%d\n", md->slave_count);
0364 mutex_unlock(&md->mutex);
0365 return count;
0366 }
0367
0368 static ssize_t w1_master_attribute_show_slaves(struct device *dev,
0369 struct device_attribute *attr, char *buf)
0370 {
0371 struct w1_master *md = dev_to_w1_master(dev);
0372 int c = PAGE_SIZE;
0373 struct list_head *ent, *n;
0374 struct w1_slave *sl = NULL;
0375
0376 mutex_lock(&md->list_mutex);
0377
0378 list_for_each_safe(ent, n, &md->slist) {
0379 sl = list_entry(ent, struct w1_slave, w1_slave_entry);
0380
0381 c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name);
0382 }
0383 if (!sl)
0384 c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n");
0385
0386 mutex_unlock(&md->list_mutex);
0387
0388 return PAGE_SIZE - c;
0389 }
0390
0391 static ssize_t w1_master_attribute_show_add(struct device *dev,
0392 struct device_attribute *attr, char *buf)
0393 {
0394 int c = PAGE_SIZE;
0395 c -= snprintf(buf+PAGE_SIZE - c, c,
0396 "write device id xx-xxxxxxxxxxxx to add slave\n");
0397 return PAGE_SIZE - c;
0398 }
0399
0400 static int w1_atoreg_num(struct device *dev, const char *buf, size_t count,
0401 struct w1_reg_num *rn)
0402 {
0403 unsigned int family;
0404 unsigned long long id;
0405 int i;
0406 u64 rn64_le;
0407
0408
0409
0410
0411
0412
0413 const char *error_msg = "bad slave string format, expecting "
0414 "ff-dddddddddddd\n";
0415
0416 if (buf[2] != '-') {
0417 dev_err(dev, "%s", error_msg);
0418 return -EINVAL;
0419 }
0420 i = sscanf(buf, "%02x-%012llx", &family, &id);
0421 if (i != 2) {
0422 dev_err(dev, "%s", error_msg);
0423 return -EINVAL;
0424 }
0425 rn->family = family;
0426 rn->id = id;
0427
0428 rn64_le = cpu_to_le64(*(u64 *)rn);
0429 rn->crc = w1_calc_crc8((u8 *)&rn64_le, 7);
0430
0431 #if 0
0432 dev_info(dev, "With CRC device is %02x.%012llx.%02x.\n",
0433 rn->family, (unsigned long long)rn->id, rn->crc);
0434 #endif
0435
0436 return 0;
0437 }
0438
0439
0440
0441
0442 struct w1_slave *w1_slave_search_device(struct w1_master *dev,
0443 struct w1_reg_num *rn)
0444 {
0445 struct w1_slave *sl;
0446 mutex_lock(&dev->list_mutex);
0447 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
0448 if (sl->reg_num.family == rn->family &&
0449 sl->reg_num.id == rn->id &&
0450 sl->reg_num.crc == rn->crc) {
0451 mutex_unlock(&dev->list_mutex);
0452 return sl;
0453 }
0454 }
0455 mutex_unlock(&dev->list_mutex);
0456 return NULL;
0457 }
0458
0459 static ssize_t w1_master_attribute_store_add(struct device *dev,
0460 struct device_attribute *attr,
0461 const char *buf, size_t count)
0462 {
0463 struct w1_master *md = dev_to_w1_master(dev);
0464 struct w1_reg_num rn;
0465 struct w1_slave *sl;
0466 ssize_t result = count;
0467
0468 if (w1_atoreg_num(dev, buf, count, &rn))
0469 return -EINVAL;
0470
0471 mutex_lock(&md->mutex);
0472 sl = w1_slave_search_device(md, &rn);
0473
0474
0475
0476
0477 if (sl) {
0478 dev_info(dev, "Device %s already exists\n", sl->name);
0479 result = -EINVAL;
0480 } else {
0481 w1_attach_slave_device(md, &rn);
0482 }
0483 mutex_unlock(&md->mutex);
0484
0485 return result;
0486 }
0487
0488 static ssize_t w1_master_attribute_show_remove(struct device *dev,
0489 struct device_attribute *attr, char *buf)
0490 {
0491 int c = PAGE_SIZE;
0492 c -= snprintf(buf+PAGE_SIZE - c, c,
0493 "write device id xx-xxxxxxxxxxxx to remove slave\n");
0494 return PAGE_SIZE - c;
0495 }
0496
0497 static ssize_t w1_master_attribute_store_remove(struct device *dev,
0498 struct device_attribute *attr,
0499 const char *buf, size_t count)
0500 {
0501 struct w1_master *md = dev_to_w1_master(dev);
0502 struct w1_reg_num rn;
0503 struct w1_slave *sl;
0504 ssize_t result = count;
0505
0506 if (w1_atoreg_num(dev, buf, count, &rn))
0507 return -EINVAL;
0508
0509 mutex_lock(&md->mutex);
0510 sl = w1_slave_search_device(md, &rn);
0511 if (sl) {
0512 result = w1_slave_detach(sl);
0513
0514 if (result == 0)
0515 result = count;
0516 } else {
0517 dev_info(dev, "Device %02x-%012llx doesn't exists\n", rn.family,
0518 (unsigned long long)rn.id);
0519 result = -EINVAL;
0520 }
0521 mutex_unlock(&md->mutex);
0522
0523 return result;
0524 }
0525
0526 #define W1_MASTER_ATTR_RO(_name, _mode) \
0527 struct device_attribute w1_master_attribute_##_name = \
0528 __ATTR(w1_master_##_name, _mode, \
0529 w1_master_attribute_show_##_name, NULL)
0530
0531 #define W1_MASTER_ATTR_RW(_name, _mode) \
0532 struct device_attribute w1_master_attribute_##_name = \
0533 __ATTR(w1_master_##_name, _mode, \
0534 w1_master_attribute_show_##_name, \
0535 w1_master_attribute_store_##_name)
0536
0537 static W1_MASTER_ATTR_RO(name, S_IRUGO);
0538 static W1_MASTER_ATTR_RO(slaves, S_IRUGO);
0539 static W1_MASTER_ATTR_RO(slave_count, S_IRUGO);
0540 static W1_MASTER_ATTR_RW(max_slave_count, S_IRUGO | S_IWUSR | S_IWGRP);
0541 static W1_MASTER_ATTR_RO(attempts, S_IRUGO);
0542 static W1_MASTER_ATTR_RO(timeout, S_IRUGO);
0543 static W1_MASTER_ATTR_RO(timeout_us, S_IRUGO);
0544 static W1_MASTER_ATTR_RO(pointer, S_IRUGO);
0545 static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUSR | S_IWGRP);
0546 static W1_MASTER_ATTR_RW(pullup, S_IRUGO | S_IWUSR | S_IWGRP);
0547 static W1_MASTER_ATTR_RW(add, S_IRUGO | S_IWUSR | S_IWGRP);
0548 static W1_MASTER_ATTR_RW(remove, S_IRUGO | S_IWUSR | S_IWGRP);
0549
0550 static struct attribute *w1_master_default_attrs[] = {
0551 &w1_master_attribute_name.attr,
0552 &w1_master_attribute_slaves.attr,
0553 &w1_master_attribute_slave_count.attr,
0554 &w1_master_attribute_max_slave_count.attr,
0555 &w1_master_attribute_attempts.attr,
0556 &w1_master_attribute_timeout.attr,
0557 &w1_master_attribute_timeout_us.attr,
0558 &w1_master_attribute_pointer.attr,
0559 &w1_master_attribute_search.attr,
0560 &w1_master_attribute_pullup.attr,
0561 &w1_master_attribute_add.attr,
0562 &w1_master_attribute_remove.attr,
0563 NULL
0564 };
0565
0566 static const struct attribute_group w1_master_defattr_group = {
0567 .attrs = w1_master_default_attrs,
0568 };
0569
0570 int w1_create_master_attributes(struct w1_master *master)
0571 {
0572 return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group);
0573 }
0574
0575 void w1_destroy_master_attributes(struct w1_master *master)
0576 {
0577 sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group);
0578 }
0579
0580 static int w1_uevent(struct device *dev, struct kobj_uevent_env *env)
0581 {
0582 struct w1_master *md = NULL;
0583 struct w1_slave *sl = NULL;
0584 char *event_owner, *name;
0585 int err = 0;
0586
0587 if (dev->driver == &w1_master_driver) {
0588 md = container_of(dev, struct w1_master, dev);
0589 event_owner = "master";
0590 name = md->name;
0591 } else if (dev->driver == &w1_slave_driver) {
0592 sl = container_of(dev, struct w1_slave, dev);
0593 event_owner = "slave";
0594 name = sl->name;
0595 } else {
0596 dev_dbg(dev, "Unknown event.\n");
0597 return -EINVAL;
0598 }
0599
0600 dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n",
0601 event_owner, name, dev_name(dev));
0602
0603 if (dev->driver != &w1_slave_driver || !sl)
0604 goto end;
0605
0606 err = add_uevent_var(env, "W1_FID=%02X", sl->reg_num.family);
0607 if (err)
0608 goto end;
0609
0610 err = add_uevent_var(env, "W1_SLAVE_ID=%024LX",
0611 (unsigned long long)sl->reg_num.id);
0612 end:
0613 return err;
0614 }
0615
0616 static int w1_family_notify(unsigned long action, struct w1_slave *sl)
0617 {
0618 const struct w1_family_ops *fops;
0619 int err;
0620
0621 fops = sl->family->fops;
0622
0623 if (!fops)
0624 return 0;
0625
0626 switch (action) {
0627 case BUS_NOTIFY_ADD_DEVICE:
0628
0629 if (fops->add_slave) {
0630 err = fops->add_slave(sl);
0631 if (err < 0) {
0632 dev_err(&sl->dev,
0633 "add_slave() call failed. err=%d\n",
0634 err);
0635 return err;
0636 }
0637 }
0638 if (fops->groups) {
0639 err = sysfs_create_groups(&sl->dev.kobj, fops->groups);
0640 if (err) {
0641 dev_err(&sl->dev,
0642 "sysfs group creation failed. err=%d\n",
0643 err);
0644 return err;
0645 }
0646 }
0647 if (IS_REACHABLE(CONFIG_HWMON) && fops->chip_info) {
0648 struct device *hwmon
0649 = hwmon_device_register_with_info(&sl->dev,
0650 "w1_slave_temp", sl,
0651 fops->chip_info,
0652 NULL);
0653 if (IS_ERR(hwmon)) {
0654 dev_warn(&sl->dev,
0655 "could not create hwmon device\n");
0656 } else {
0657 sl->hwmon = hwmon;
0658 }
0659 }
0660 break;
0661 case BUS_NOTIFY_DEL_DEVICE:
0662 if (IS_REACHABLE(CONFIG_HWMON) && fops->chip_info &&
0663 sl->hwmon)
0664 hwmon_device_unregister(sl->hwmon);
0665 if (fops->remove_slave)
0666 sl->family->fops->remove_slave(sl);
0667 if (fops->groups)
0668 sysfs_remove_groups(&sl->dev.kobj, fops->groups);
0669 break;
0670 }
0671 return 0;
0672 }
0673
0674 static int __w1_attach_slave_device(struct w1_slave *sl)
0675 {
0676 int err;
0677
0678 sl->dev.parent = &sl->master->dev;
0679 sl->dev.driver = &w1_slave_driver;
0680 sl->dev.bus = &w1_bus_type;
0681 sl->dev.release = &w1_slave_release;
0682 sl->dev.groups = w1_slave_groups;
0683 sl->dev.of_node = of_find_matching_node(sl->master->dev.of_node,
0684 sl->family->of_match_table);
0685
0686 dev_set_name(&sl->dev, "%02x-%012llx",
0687 (unsigned int) sl->reg_num.family,
0688 (unsigned long long) sl->reg_num.id);
0689 snprintf(&sl->name[0], sizeof(sl->name),
0690 "%02x-%012llx",
0691 (unsigned int) sl->reg_num.family,
0692 (unsigned long long) sl->reg_num.id);
0693
0694 dev_dbg(&sl->dev, "%s: registering %s as %p.\n", __func__,
0695 dev_name(&sl->dev), sl);
0696
0697
0698 dev_set_uevent_suppress(&sl->dev, true);
0699
0700 err = device_register(&sl->dev);
0701 if (err < 0) {
0702 dev_err(&sl->dev,
0703 "Device registration [%s] failed. err=%d\n",
0704 dev_name(&sl->dev), err);
0705 put_device(&sl->dev);
0706 return err;
0707 }
0708 w1_family_notify(BUS_NOTIFY_ADD_DEVICE, sl);
0709
0710 dev_set_uevent_suppress(&sl->dev, false);
0711 kobject_uevent(&sl->dev.kobj, KOBJ_ADD);
0712
0713 mutex_lock(&sl->master->list_mutex);
0714 list_add_tail(&sl->w1_slave_entry, &sl->master->slist);
0715 mutex_unlock(&sl->master->list_mutex);
0716
0717 return 0;
0718 }
0719
0720 int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn)
0721 {
0722 struct w1_slave *sl;
0723 struct w1_family *f;
0724 int err;
0725 struct w1_netlink_msg msg;
0726
0727 sl = kzalloc(sizeof(struct w1_slave), GFP_KERNEL);
0728 if (!sl) {
0729 dev_err(&dev->dev,
0730 "%s: failed to allocate new slave device.\n",
0731 __func__);
0732 return -ENOMEM;
0733 }
0734
0735
0736 sl->owner = THIS_MODULE;
0737 sl->master = dev;
0738 set_bit(W1_SLAVE_ACTIVE, &sl->flags);
0739
0740 memset(&msg, 0, sizeof(msg));
0741 memcpy(&sl->reg_num, rn, sizeof(sl->reg_num));
0742 atomic_set(&sl->refcnt, 1);
0743 atomic_inc(&sl->master->refcnt);
0744 dev->slave_count++;
0745 dev_info(&dev->dev, "Attaching one wire slave %02x.%012llx crc %02x\n",
0746 rn->family, (unsigned long long)rn->id, rn->crc);
0747
0748
0749 mutex_unlock(&dev->mutex);
0750 request_module("w1-family-0x%02X", rn->family);
0751 mutex_lock(&dev->mutex);
0752
0753 spin_lock(&w1_flock);
0754 f = w1_family_registered(rn->family);
0755 if (!f) {
0756 f= &w1_default_family;
0757 dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n",
0758 rn->family, rn->family,
0759 (unsigned long long)rn->id, rn->crc);
0760 }
0761 __w1_family_get(f);
0762 spin_unlock(&w1_flock);
0763
0764 sl->family = f;
0765
0766 err = __w1_attach_slave_device(sl);
0767 if (err < 0) {
0768 dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__,
0769 sl->name);
0770 dev->slave_count--;
0771 w1_family_put(sl->family);
0772 atomic_dec(&sl->master->refcnt);
0773 kfree(sl);
0774 return err;
0775 }
0776
0777 sl->ttl = dev->slave_ttl;
0778
0779 memcpy(msg.id.id, rn, sizeof(msg.id));
0780 msg.type = W1_SLAVE_ADD;
0781 w1_netlink_send(dev, &msg);
0782
0783 return 0;
0784 }
0785
0786 int w1_unref_slave(struct w1_slave *sl)
0787 {
0788 struct w1_master *dev = sl->master;
0789 int refcnt;
0790 mutex_lock(&dev->list_mutex);
0791 refcnt = atomic_sub_return(1, &sl->refcnt);
0792 if (refcnt == 0) {
0793 struct w1_netlink_msg msg;
0794
0795 dev_dbg(&sl->dev, "%s: detaching %s [%p].\n", __func__,
0796 sl->name, sl);
0797
0798 list_del(&sl->w1_slave_entry);
0799
0800 memset(&msg, 0, sizeof(msg));
0801 memcpy(msg.id.id, &sl->reg_num, sizeof(msg.id));
0802 msg.type = W1_SLAVE_REMOVE;
0803 w1_netlink_send(sl->master, &msg);
0804
0805 w1_family_notify(BUS_NOTIFY_DEL_DEVICE, sl);
0806 device_unregister(&sl->dev);
0807 #ifdef DEBUG
0808 memset(sl, 0, sizeof(*sl));
0809 #endif
0810 kfree(sl);
0811 }
0812 atomic_dec(&dev->refcnt);
0813 mutex_unlock(&dev->list_mutex);
0814 return refcnt;
0815 }
0816
0817 int w1_slave_detach(struct w1_slave *sl)
0818 {
0819
0820 int destroy_now;
0821 mutex_lock(&sl->master->list_mutex);
0822 destroy_now = !test_bit(W1_SLAVE_DETACH, &sl->flags);
0823 set_bit(W1_SLAVE_DETACH, &sl->flags);
0824 mutex_unlock(&sl->master->list_mutex);
0825
0826 if (destroy_now)
0827 destroy_now = !w1_unref_slave(sl);
0828 return destroy_now ? 0 : -EBUSY;
0829 }
0830
0831 struct w1_master *w1_search_master_id(u32 id)
0832 {
0833 struct w1_master *dev;
0834 int found = 0;
0835
0836 mutex_lock(&w1_mlock);
0837 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
0838 if (dev->id == id) {
0839 found = 1;
0840 atomic_inc(&dev->refcnt);
0841 break;
0842 }
0843 }
0844 mutex_unlock(&w1_mlock);
0845
0846 return (found)?dev:NULL;
0847 }
0848
0849 struct w1_slave *w1_search_slave(struct w1_reg_num *id)
0850 {
0851 struct w1_master *dev;
0852 struct w1_slave *sl = NULL;
0853 int found = 0;
0854
0855 mutex_lock(&w1_mlock);
0856 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
0857 mutex_lock(&dev->list_mutex);
0858 list_for_each_entry(sl, &dev->slist, w1_slave_entry) {
0859 if (sl->reg_num.family == id->family &&
0860 sl->reg_num.id == id->id &&
0861 sl->reg_num.crc == id->crc) {
0862 found = 1;
0863 atomic_inc(&dev->refcnt);
0864 atomic_inc(&sl->refcnt);
0865 break;
0866 }
0867 }
0868 mutex_unlock(&dev->list_mutex);
0869
0870 if (found)
0871 break;
0872 }
0873 mutex_unlock(&w1_mlock);
0874
0875 return (found)?sl:NULL;
0876 }
0877
0878 void w1_reconnect_slaves(struct w1_family *f, int attach)
0879 {
0880 struct w1_slave *sl, *sln;
0881 struct w1_master *dev;
0882
0883 mutex_lock(&w1_mlock);
0884 list_for_each_entry(dev, &w1_masters, w1_master_entry) {
0885 dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
0886 "for family %02x.\n", dev->name, f->fid);
0887 mutex_lock(&dev->mutex);
0888 mutex_lock(&dev->list_mutex);
0889 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
0890
0891
0892
0893
0894
0895 if ((attach && sl->family->fid == W1_FAMILY_DEFAULT
0896 && sl->reg_num.family == f->fid) ||
0897 (!attach && sl->family->fid == f->fid)) {
0898 struct w1_reg_num rn;
0899
0900 mutex_unlock(&dev->list_mutex);
0901 memcpy(&rn, &sl->reg_num, sizeof(rn));
0902
0903
0904
0905 if (!w1_slave_detach(sl))
0906 w1_attach_slave_device(dev, &rn);
0907 mutex_lock(&dev->list_mutex);
0908 }
0909 }
0910 dev_dbg(&dev->dev, "Reconnecting slaves in device %s "
0911 "has been finished.\n", dev->name);
0912 mutex_unlock(&dev->list_mutex);
0913 mutex_unlock(&dev->mutex);
0914 }
0915 mutex_unlock(&w1_mlock);
0916 }
0917
0918 static int w1_addr_crc_is_valid(struct w1_master *dev, u64 rn)
0919 {
0920 u64 rn_le = cpu_to_le64(rn);
0921 struct w1_reg_num *tmp = (struct w1_reg_num *)&rn;
0922 u8 crc;
0923
0924 crc = w1_calc_crc8((u8 *)&rn_le, 7);
0925
0926
0927
0928
0929
0930
0931
0932
0933
0934 if (crc != tmp->crc && tmp->family == W1_FAMILY_DS28E04) {
0935 u64 corr_le = rn_le;
0936
0937 ((u8 *)&corr_le)[1] |= 0x7f;
0938 crc = w1_calc_crc8((u8 *)&corr_le, 7);
0939
0940 dev_info(&dev->dev, "DS28E04 crc workaround on %02x.%012llx.%02x\n",
0941 tmp->family, (unsigned long long)tmp->id, tmp->crc);
0942 }
0943
0944 if (crc != tmp->crc) {
0945 dev_dbg(&dev->dev, "w1 addr crc mismatch: %02x.%012llx.%02x != 0x%02x.\n",
0946 tmp->family, (unsigned long long)tmp->id, tmp->crc, crc);
0947 return 0;
0948 }
0949 return 1;
0950 }
0951
0952 void w1_slave_found(struct w1_master *dev, u64 rn)
0953 {
0954 struct w1_slave *sl;
0955 struct w1_reg_num *tmp;
0956
0957 atomic_inc(&dev->refcnt);
0958
0959 tmp = (struct w1_reg_num *) &rn;
0960
0961 sl = w1_slave_search_device(dev, tmp);
0962 if (sl) {
0963 set_bit(W1_SLAVE_ACTIVE, &sl->flags);
0964 } else {
0965 if (rn && w1_addr_crc_is_valid(dev, rn))
0966 w1_attach_slave_device(dev, tmp);
0967 }
0968
0969 atomic_dec(&dev->refcnt);
0970 }
0971
0972
0973
0974
0975
0976
0977
0978
0979
0980
0981
0982
0983
0984
0985
0986
0987
0988
0989 void w1_search(struct w1_master *dev, u8 search_type, w1_slave_found_callback cb)
0990 {
0991 u64 last_rn, rn, tmp64;
0992 int i, slave_count = 0;
0993 int last_zero, last_device;
0994 int search_bit, desc_bit;
0995 u8 triplet_ret = 0;
0996
0997 search_bit = 0;
0998 rn = dev->search_id;
0999 last_rn = 0;
1000 last_device = 0;
1001 last_zero = -1;
1002
1003 desc_bit = 64;
1004
1005 while ( !last_device && (slave_count++ < dev->max_slave_count) ) {
1006 last_rn = rn;
1007 rn = 0;
1008
1009
1010
1011
1012
1013
1014
1015 mutex_lock(&dev->bus_mutex);
1016 if (w1_reset_bus(dev)) {
1017 mutex_unlock(&dev->bus_mutex);
1018 dev_dbg(&dev->dev, "No devices present on the wire.\n");
1019 break;
1020 }
1021
1022
1023 if (dev->max_slave_count == 1) {
1024 int rv;
1025 w1_write_8(dev, W1_READ_ROM);
1026 rv = w1_read_block(dev, (u8 *)&rn, 8);
1027 mutex_unlock(&dev->bus_mutex);
1028
1029 if (rv == 8 && rn)
1030 cb(dev, rn);
1031
1032 break;
1033 }
1034
1035
1036 w1_write_8(dev, search_type);
1037 for (i = 0; i < 64; ++i) {
1038
1039 if (i == desc_bit)
1040 search_bit = 1;
1041 else if (i > desc_bit)
1042 search_bit = 0;
1043 else
1044 search_bit = ((last_rn >> i) & 0x1);
1045
1046
1047 triplet_ret = w1_triplet(dev, search_bit);
1048
1049
1050 if ( (triplet_ret & 0x03) == 0x03 )
1051 break;
1052
1053
1054 if (triplet_ret == 0)
1055 last_zero = i;
1056
1057
1058 tmp64 = (triplet_ret >> 2);
1059 rn |= (tmp64 << i);
1060
1061 if (test_bit(W1_ABORT_SEARCH, &dev->flags)) {
1062 mutex_unlock(&dev->bus_mutex);
1063 dev_dbg(&dev->dev, "Abort w1_search\n");
1064 return;
1065 }
1066 }
1067 mutex_unlock(&dev->bus_mutex);
1068
1069 if ( (triplet_ret & 0x03) != 0x03 ) {
1070 if ((desc_bit == last_zero) || (last_zero < 0)) {
1071 last_device = 1;
1072 dev->search_id = 0;
1073 } else {
1074 dev->search_id = rn;
1075 }
1076 desc_bit = last_zero;
1077 cb(dev, rn);
1078 }
1079
1080 if (!last_device && slave_count == dev->max_slave_count &&
1081 !test_bit(W1_WARN_MAX_COUNT, &dev->flags)) {
1082
1083
1084
1085
1086
1087
1088
1089 dev_info(&dev->dev, "%s: max_slave_count %d reached, "
1090 "will continue next search.\n", __func__,
1091 dev->max_slave_count);
1092 set_bit(W1_WARN_MAX_COUNT, &dev->flags);
1093 }
1094 }
1095 }
1096
1097 void w1_search_process_cb(struct w1_master *dev, u8 search_type,
1098 w1_slave_found_callback cb)
1099 {
1100 struct w1_slave *sl, *sln;
1101
1102 mutex_lock(&dev->list_mutex);
1103 list_for_each_entry(sl, &dev->slist, w1_slave_entry)
1104 clear_bit(W1_SLAVE_ACTIVE, &sl->flags);
1105 mutex_unlock(&dev->list_mutex);
1106
1107 w1_search_devices(dev, search_type, cb);
1108
1109 mutex_lock(&dev->list_mutex);
1110 list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) {
1111 if (!test_bit(W1_SLAVE_ACTIVE, &sl->flags) && !--sl->ttl) {
1112 mutex_unlock(&dev->list_mutex);
1113 w1_slave_detach(sl);
1114 mutex_lock(&dev->list_mutex);
1115 }
1116 else if (test_bit(W1_SLAVE_ACTIVE, &sl->flags))
1117 sl->ttl = dev->slave_ttl;
1118 }
1119 mutex_unlock(&dev->list_mutex);
1120
1121 if (dev->search_count > 0)
1122 dev->search_count--;
1123 }
1124
1125 static void w1_search_process(struct w1_master *dev, u8 search_type)
1126 {
1127 w1_search_process_cb(dev, search_type, w1_slave_found);
1128 }
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138 int w1_process_callbacks(struct w1_master *dev)
1139 {
1140 int ret = 0;
1141 struct w1_async_cmd *async_cmd, *async_n;
1142
1143
1144 while (!list_empty(&dev->async_list)) {
1145 list_for_each_entry_safe(async_cmd, async_n, &dev->async_list,
1146 async_entry) {
1147
1148
1149 mutex_unlock(&dev->list_mutex);
1150 async_cmd->cb(dev, async_cmd);
1151 ret = 1;
1152 mutex_lock(&dev->list_mutex);
1153 }
1154 }
1155 return ret;
1156 }
1157
1158 int w1_process(void *data)
1159 {
1160 struct w1_master *dev = (struct w1_master *) data;
1161
1162
1163
1164 const unsigned long jtime =
1165 usecs_to_jiffies(w1_timeout * 1000000 + w1_timeout_us);
1166
1167 unsigned long jremain = 0;
1168
1169 for (;;) {
1170
1171 if (!jremain && dev->search_count) {
1172 mutex_lock(&dev->mutex);
1173 w1_search_process(dev, W1_SEARCH);
1174 mutex_unlock(&dev->mutex);
1175 }
1176
1177 mutex_lock(&dev->list_mutex);
1178
1179
1180
1181 if (!w1_process_callbacks(dev) && jremain) {
1182
1183
1184
1185
1186 jremain = 1;
1187 }
1188
1189 __set_current_state(TASK_INTERRUPTIBLE);
1190
1191
1192
1193
1194 mutex_unlock(&dev->list_mutex);
1195
1196 if (kthread_should_stop())
1197 break;
1198
1199
1200 if (dev->search_count) {
1201 if (!jremain)
1202 jremain = jtime;
1203 jremain = schedule_timeout(jremain);
1204 }
1205 else
1206 schedule();
1207 }
1208
1209 atomic_dec(&dev->refcnt);
1210
1211 return 0;
1212 }
1213
1214 static int __init w1_init(void)
1215 {
1216 int retval;
1217
1218 pr_info("Driver for 1-wire Dallas network protocol.\n");
1219
1220 w1_init_netlink();
1221
1222 retval = bus_register(&w1_bus_type);
1223 if (retval) {
1224 pr_err("Failed to register bus. err=%d.\n", retval);
1225 goto err_out_exit_init;
1226 }
1227
1228 retval = driver_register(&w1_master_driver);
1229 if (retval) {
1230 pr_err("Failed to register master driver. err=%d.\n",
1231 retval);
1232 goto err_out_bus_unregister;
1233 }
1234
1235 retval = driver_register(&w1_slave_driver);
1236 if (retval) {
1237 pr_err("Failed to register slave driver. err=%d.\n",
1238 retval);
1239 goto err_out_master_unregister;
1240 }
1241
1242 return 0;
1243
1244 #if 0
1245
1246 err_out_slave_unregister:
1247 driver_unregister(&w1_slave_driver);
1248 #endif
1249
1250 err_out_master_unregister:
1251 driver_unregister(&w1_master_driver);
1252
1253 err_out_bus_unregister:
1254 bus_unregister(&w1_bus_type);
1255
1256 err_out_exit_init:
1257 return retval;
1258 }
1259
1260 static void __exit w1_fini(void)
1261 {
1262 struct w1_master *dev;
1263
1264
1265 list_for_each_entry(dev, &w1_masters, w1_master_entry)
1266 __w1_remove_master_device(dev);
1267
1268 w1_fini_netlink();
1269
1270 driver_unregister(&w1_slave_driver);
1271 driver_unregister(&w1_master_driver);
1272 bus_unregister(&w1_bus_type);
1273 }
1274
1275 module_init(w1_init);
1276 module_exit(w1_fini);
1277
1278 MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>");
1279 MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol.");
1280 MODULE_LICENSE("GPL");