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