Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 2 | * w1.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2004 Evgeniy Polyakov <johnpol@2ka.mipt.ru> |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 5 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | */ |
| 21 | |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/moduleparam.h> |
| 26 | #include <linux/list.h> |
| 27 | #include <linux/interrupt.h> |
| 28 | #include <linux/spinlock.h> |
| 29 | #include <linux/timer.h> |
| 30 | #include <linux/device.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/sched.h> |
| 33 | |
| 34 | #include <asm/atomic.h> |
| 35 | |
| 36 | #include "w1.h" |
| 37 | #include "w1_io.h" |
| 38 | #include "w1_log.h" |
| 39 | #include "w1_int.h" |
| 40 | #include "w1_family.h" |
| 41 | #include "w1_netlink.h" |
| 42 | |
| 43 | MODULE_LICENSE("GPL"); |
| 44 | MODULE_AUTHOR("Evgeniy Polyakov <johnpol@2ka.mipt.ru>"); |
| 45 | MODULE_DESCRIPTION("Driver for 1-wire Dallas network protocol."); |
| 46 | |
| 47 | static int w1_timeout = 10; |
| 48 | int w1_max_slave_count = 10; |
| 49 | int w1_max_slave_ttl = 10; |
| 50 | |
| 51 | module_param_named(timeout, w1_timeout, int, 0); |
| 52 | module_param_named(max_slave_count, w1_max_slave_count, int, 0); |
| 53 | module_param_named(slave_ttl, w1_max_slave_ttl, int, 0); |
| 54 | |
| 55 | DEFINE_SPINLOCK(w1_mlock); |
| 56 | LIST_HEAD(w1_masters); |
| 57 | |
| 58 | static pid_t control_thread; |
| 59 | static int control_needs_exit; |
| 60 | static DECLARE_COMPLETION(w1_control_complete); |
| 61 | |
Evgeniy Polyakov | 99c5bfe | 2005-06-04 01:31:26 +0400 | [diff] [blame] | 62 | /* stuff for the default family */ |
| 63 | static ssize_t w1_famdefault_read_name(struct device *dev, struct device_attribute *attr, char *buf) |
| 64 | { |
| 65 | struct w1_slave *sl = container_of(dev, struct w1_slave, dev); |
| 66 | return(sprintf(buf, "%s\n", sl->name)); |
| 67 | } |
| 68 | static struct w1_family_ops w1_default_fops = { |
| 69 | .rname = &w1_famdefault_read_name, |
| 70 | }; |
| 71 | static struct w1_family w1_default_family = { |
| 72 | .fops = &w1_default_fops, |
| 73 | }; |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | static int w1_master_match(struct device *dev, struct device_driver *drv) |
| 76 | { |
| 77 | return 1; |
| 78 | } |
| 79 | |
| 80 | static int w1_master_probe(struct device *dev) |
| 81 | { |
| 82 | return -ENODEV; |
| 83 | } |
| 84 | |
| 85 | static int w1_master_remove(struct device *dev) |
| 86 | { |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static void w1_master_release(struct device *dev) |
| 91 | { |
| 92 | struct w1_master *md = container_of(dev, struct w1_master, dev); |
| 93 | |
| 94 | complete(&md->dev_released); |
| 95 | } |
| 96 | |
| 97 | static void w1_slave_release(struct device *dev) |
| 98 | { |
| 99 | struct w1_slave *sl = container_of(dev, struct w1_slave, dev); |
| 100 | |
| 101 | complete(&sl->dev_released); |
| 102 | } |
| 103 | |
Yani Ioannou | 060b884 | 2005-05-17 06:44:04 -0400 | [diff] [blame] | 104 | static ssize_t w1_default_read_name(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
| 106 | return sprintf(buf, "No family registered.\n"); |
| 107 | } |
| 108 | |
| 109 | static ssize_t w1_default_read_bin(struct kobject *kobj, char *buf, loff_t off, |
| 110 | size_t count) |
| 111 | { |
| 112 | return sprintf(buf, "No family registered.\n"); |
| 113 | } |
| 114 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 115 | static struct device_attribute w1_slave_attribute = |
| 116 | __ATTR(name, S_IRUGO, w1_default_read_name, NULL); |
| 117 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 118 | static struct bin_attribute w1_slave_bin_attribute = { |
| 119 | .attr = { |
| 120 | .name = "w1_slave", |
| 121 | .mode = S_IRUGO, |
| 122 | .owner = THIS_MODULE, |
| 123 | }, |
| 124 | .size = W1_SLAVE_DATA_SIZE, |
| 125 | .read = &w1_default_read_bin, |
| 126 | }; |
| 127 | |
Evgeniy Polyakov | 7f772ed | 2005-08-11 13:20:07 +0400 | [diff] [blame] | 128 | static int w1_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size); |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | static struct bus_type w1_bus_type = { |
| 131 | .name = "w1", |
| 132 | .match = w1_master_match, |
Evgeniy Polyakov | 7f772ed | 2005-08-11 13:20:07 +0400 | [diff] [blame] | 133 | .hotplug = w1_hotplug, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
Evgeniy Polyakov | 7f772ed | 2005-08-11 13:20:07 +0400 | [diff] [blame] | 136 | struct device_driver w1_master_driver = { |
| 137 | .name = "w1_master_driver", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | .bus = &w1_bus_type, |
| 139 | .probe = w1_master_probe, |
| 140 | .remove = w1_master_remove, |
| 141 | }; |
| 142 | |
Evgeniy Polyakov | 7f772ed | 2005-08-11 13:20:07 +0400 | [diff] [blame] | 143 | struct device w1_master_device = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | .parent = NULL, |
| 145 | .bus = &w1_bus_type, |
| 146 | .bus_id = "w1 bus master", |
Evgeniy Polyakov | 7f772ed | 2005-08-11 13:20:07 +0400 | [diff] [blame] | 147 | .driver = &w1_master_driver, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | .release = &w1_master_release |
| 149 | }; |
| 150 | |
Evgeniy Polyakov | 7f772ed | 2005-08-11 13:20:07 +0400 | [diff] [blame] | 151 | struct device_driver w1_slave_driver = { |
| 152 | .name = "w1_slave_driver", |
| 153 | .bus = &w1_bus_type, |
| 154 | }; |
| 155 | |
| 156 | struct device w1_slave_device = { |
| 157 | .parent = NULL, |
| 158 | .bus = &w1_bus_type, |
| 159 | .bus_id = "w1 bus slave", |
| 160 | .driver = &w1_slave_driver, |
| 161 | }; |
| 162 | |
Yani Ioannou | 060b884 | 2005-05-17 06:44:04 -0400 | [diff] [blame] | 163 | 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] | 164 | { |
Evgeniy Polyakov | 6b72986 | 2005-06-04 01:30:43 +0400 | [diff] [blame] | 165 | struct w1_master *md = container_of(dev, struct w1_master, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | ssize_t count; |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | if (down_interruptible (&md->mutex)) |
| 169 | return -EBUSY; |
| 170 | |
| 171 | count = sprintf(buf, "%s\n", md->name); |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | up(&md->mutex); |
| 174 | |
| 175 | return count; |
| 176 | } |
| 177 | |
Evgeniy Polyakov | 2a9d0c1 | 2005-06-04 01:31:02 +0400 | [diff] [blame] | 178 | static ssize_t w1_master_attribute_store_search(struct device * dev, |
| 179 | struct device_attribute *attr, |
| 180 | const char * buf, size_t count) |
| 181 | { |
| 182 | struct w1_master *md = container_of(dev, struct w1_master, dev); |
| 183 | |
| 184 | if (down_interruptible (&md->mutex)) |
| 185 | return -EBUSY; |
| 186 | |
| 187 | md->search_count = simple_strtol(buf, NULL, 0); |
| 188 | |
| 189 | up(&md->mutex); |
| 190 | |
| 191 | return count; |
| 192 | } |
| 193 | |
| 194 | static ssize_t w1_master_attribute_show_search(struct device *dev, |
| 195 | struct device_attribute *attr, |
| 196 | char *buf) |
| 197 | { |
| 198 | struct w1_master *md = container_of(dev, struct w1_master, dev); |
| 199 | ssize_t count; |
| 200 | |
| 201 | if (down_interruptible (&md->mutex)) |
| 202 | return -EBUSY; |
| 203 | |
| 204 | count = sprintf(buf, "%d\n", md->search_count); |
| 205 | |
| 206 | up(&md->mutex); |
| 207 | |
| 208 | return count; |
| 209 | } |
| 210 | |
Yani Ioannou | 060b884 | 2005-05-17 06:44:04 -0400 | [diff] [blame] | 211 | 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] | 212 | { |
| 213 | struct w1_master *md = container_of(dev, struct w1_master, dev); |
| 214 | ssize_t count; |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | if (down_interruptible(&md->mutex)) |
| 217 | return -EBUSY; |
| 218 | |
| 219 | count = sprintf(buf, "0x%p\n", md->bus_master); |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | up(&md->mutex); |
| 222 | return count; |
| 223 | } |
| 224 | |
Yani Ioannou | 060b884 | 2005-05-17 06:44:04 -0400 | [diff] [blame] | 225 | 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] | 226 | { |
| 227 | ssize_t count; |
| 228 | count = sprintf(buf, "%d\n", w1_timeout); |
| 229 | return count; |
| 230 | } |
| 231 | |
Yani Ioannou | 060b884 | 2005-05-17 06:44:04 -0400 | [diff] [blame] | 232 | 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] | 233 | { |
| 234 | struct w1_master *md = container_of(dev, struct w1_master, dev); |
| 235 | ssize_t count; |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | if (down_interruptible(&md->mutex)) |
| 238 | return -EBUSY; |
| 239 | |
| 240 | count = sprintf(buf, "%d\n", md->max_slave_count); |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | up(&md->mutex); |
| 243 | return count; |
| 244 | } |
| 245 | |
Yani Ioannou | 060b884 | 2005-05-17 06:44:04 -0400 | [diff] [blame] | 246 | 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] | 247 | { |
| 248 | struct w1_master *md = container_of(dev, struct w1_master, dev); |
| 249 | ssize_t count; |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 250 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | if (down_interruptible(&md->mutex)) |
| 252 | return -EBUSY; |
| 253 | |
| 254 | count = sprintf(buf, "%lu\n", md->attempts); |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | up(&md->mutex); |
| 257 | return count; |
| 258 | } |
| 259 | |
Yani Ioannou | 060b884 | 2005-05-17 06:44:04 -0400 | [diff] [blame] | 260 | 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] | 261 | { |
| 262 | struct w1_master *md = container_of(dev, struct w1_master, dev); |
| 263 | ssize_t count; |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | if (down_interruptible(&md->mutex)) |
| 266 | return -EBUSY; |
| 267 | |
| 268 | count = sprintf(buf, "%d\n", md->slave_count); |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | up(&md->mutex); |
| 271 | return count; |
| 272 | } |
| 273 | |
Yani Ioannou | 060b884 | 2005-05-17 06:44:04 -0400 | [diff] [blame] | 274 | static ssize_t w1_master_attribute_show_slaves(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | { |
| 276 | struct w1_master *md = container_of(dev, struct w1_master, dev); |
| 277 | int c = PAGE_SIZE; |
| 278 | |
| 279 | if (down_interruptible(&md->mutex)) |
| 280 | return -EBUSY; |
| 281 | |
| 282 | if (md->slave_count == 0) |
| 283 | c -= snprintf(buf + PAGE_SIZE - c, c, "not found.\n"); |
| 284 | else { |
| 285 | struct list_head *ent, *n; |
| 286 | struct w1_slave *sl; |
| 287 | |
| 288 | list_for_each_safe(ent, n, &md->slist) { |
| 289 | sl = list_entry(ent, struct w1_slave, w1_slave_entry); |
| 290 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 291 | c -= snprintf(buf + PAGE_SIZE - c, c, "%s\n", sl->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
| 295 | up(&md->mutex); |
| 296 | |
| 297 | return PAGE_SIZE - c; |
| 298 | } |
| 299 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 300 | #define W1_MASTER_ATTR_RO(_name, _mode) \ |
| 301 | struct device_attribute w1_master_attribute_##_name = \ |
| 302 | __ATTR(w1_master_##_name, _mode, \ |
| 303 | w1_master_attribute_show_##_name, NULL) |
| 304 | |
Evgeniy Polyakov | 2a9d0c1 | 2005-06-04 01:31:02 +0400 | [diff] [blame] | 305 | #define W1_MASTER_ATTR_RW(_name, _mode) \ |
| 306 | struct device_attribute w1_master_attribute_##_name = \ |
| 307 | __ATTR(w1_master_##_name, _mode, \ |
| 308 | w1_master_attribute_show_##_name, \ |
| 309 | w1_master_attribute_store_##_name) |
| 310 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 311 | static W1_MASTER_ATTR_RO(name, S_IRUGO); |
| 312 | static W1_MASTER_ATTR_RO(slaves, S_IRUGO); |
| 313 | static W1_MASTER_ATTR_RO(slave_count, S_IRUGO); |
| 314 | static W1_MASTER_ATTR_RO(max_slave_count, S_IRUGO); |
| 315 | static W1_MASTER_ATTR_RO(attempts, S_IRUGO); |
| 316 | static W1_MASTER_ATTR_RO(timeout, S_IRUGO); |
| 317 | static W1_MASTER_ATTR_RO(pointer, S_IRUGO); |
Evgeniy Polyakov | 2a9d0c1 | 2005-06-04 01:31:02 +0400 | [diff] [blame] | 318 | static W1_MASTER_ATTR_RW(search, S_IRUGO | S_IWUGO); |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 319 | |
| 320 | static struct attribute *w1_master_default_attrs[] = { |
| 321 | &w1_master_attribute_name.attr, |
| 322 | &w1_master_attribute_slaves.attr, |
| 323 | &w1_master_attribute_slave_count.attr, |
| 324 | &w1_master_attribute_max_slave_count.attr, |
| 325 | &w1_master_attribute_attempts.attr, |
| 326 | &w1_master_attribute_timeout.attr, |
| 327 | &w1_master_attribute_pointer.attr, |
Evgeniy Polyakov | 2a9d0c1 | 2005-06-04 01:31:02 +0400 | [diff] [blame] | 328 | &w1_master_attribute_search.attr, |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 329 | NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | }; |
| 331 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 332 | static struct attribute_group w1_master_defattr_group = { |
| 333 | .attrs = w1_master_default_attrs, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | }; |
| 335 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 336 | int w1_create_master_attributes(struct w1_master *master) |
| 337 | { |
| 338 | return sysfs_create_group(&master->dev.kobj, &w1_master_defattr_group); |
| 339 | } |
| 340 | |
| 341 | void w1_destroy_master_attributes(struct w1_master *master) |
| 342 | { |
| 343 | sysfs_remove_group(&master->dev.kobj, &w1_master_defattr_group); |
| 344 | } |
| 345 | |
Evgeniy Polyakov | 7f772ed | 2005-08-11 13:20:07 +0400 | [diff] [blame] | 346 | #ifdef CONFIG_HOTPLUG |
| 347 | static int w1_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size) |
| 348 | { |
| 349 | struct w1_master *md = NULL; |
| 350 | struct w1_slave *sl = NULL; |
| 351 | char *event_owner, *name; |
| 352 | int err, cur_index=0, cur_len=0; |
| 353 | |
| 354 | if (dev->driver == &w1_master_driver) { |
| 355 | md = container_of(dev, struct w1_master, dev); |
| 356 | event_owner = "master"; |
| 357 | name = md->name; |
| 358 | } else if (dev->driver == &w1_slave_driver) { |
| 359 | sl = container_of(dev, struct w1_slave, dev); |
| 360 | event_owner = "slave"; |
| 361 | name = sl->name; |
| 362 | } else { |
| 363 | dev_dbg(dev, "Unknown hotplug event.\n"); |
| 364 | return -EINVAL; |
| 365 | } |
| 366 | |
| 367 | dev_dbg(dev, "Hotplug event for %s %s, bus_id=%s.\n", event_owner, name, dev->bus_id); |
| 368 | |
| 369 | if (dev->driver != &w1_slave_driver || !sl) |
| 370 | return 0; |
| 371 | |
| 372 | err = add_hotplug_env_var(envp, num_envp, &cur_index, buffer, buffer_size, &cur_len, "W1_FID=%02X", sl->reg_num.family); |
| 373 | if (err) |
| 374 | return err; |
| 375 | |
Evgeniy Polyakov | 5e8eb85 | 2005-08-11 13:45:54 +0400 | [diff] [blame^] | 376 | err = add_hotplug_env_var(envp, num_envp, &cur_index, buffer, buffer_size, &cur_len, "W1_SLAVE_ID=%024LX", (u64)sl->reg_num.id); |
Evgeniy Polyakov | 7f772ed | 2005-08-11 13:20:07 +0400 | [diff] [blame] | 377 | if (err) |
| 378 | return err; |
| 379 | |
| 380 | return 0; |
| 381 | }; |
| 382 | #else |
| 383 | static int w1_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size) |
| 384 | { |
| 385 | return 0; |
| 386 | } |
| 387 | #endif |
| 388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | static int __w1_attach_slave_device(struct w1_slave *sl) |
| 390 | { |
| 391 | int err; |
| 392 | |
| 393 | sl->dev.parent = &sl->master->dev; |
Evgeniy Polyakov | 7f772ed | 2005-08-11 13:20:07 +0400 | [diff] [blame] | 394 | sl->dev.driver = &w1_slave_driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | sl->dev.bus = &w1_bus_type; |
| 396 | sl->dev.release = &w1_slave_release; |
| 397 | |
| 398 | snprintf(&sl->dev.bus_id[0], sizeof(sl->dev.bus_id), |
Evgeniy Polyakov | 6b72986 | 2005-06-04 01:30:43 +0400 | [diff] [blame] | 399 | "%02x-%012llx", |
| 400 | (unsigned int) sl->reg_num.family, |
| 401 | (unsigned long long) sl->reg_num.id); |
| 402 | snprintf(&sl->name[0], sizeof(sl->name), |
| 403 | "%02x-%012llx", |
| 404 | (unsigned int) sl->reg_num.family, |
| 405 | (unsigned long long) sl->reg_num.id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
| 407 | dev_dbg(&sl->dev, "%s: registering %s.\n", __func__, |
| 408 | &sl->dev.bus_id[0]); |
| 409 | |
| 410 | err = device_register(&sl->dev); |
| 411 | if (err < 0) { |
| 412 | dev_err(&sl->dev, |
Evgeniy Polyakov | 6b72986 | 2005-06-04 01:30:43 +0400 | [diff] [blame] | 413 | "Device registration [%s] failed. err=%d\n", |
| 414 | sl->dev.bus_id, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | return err; |
| 416 | } |
| 417 | |
| 418 | memcpy(&sl->attr_bin, &w1_slave_bin_attribute, sizeof(sl->attr_bin)); |
| 419 | memcpy(&sl->attr_name, &w1_slave_attribute, sizeof(sl->attr_name)); |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | sl->attr_bin.read = sl->family->fops->rbin; |
| 422 | sl->attr_name.show = sl->family->fops->rname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
| 424 | err = device_create_file(&sl->dev, &sl->attr_name); |
| 425 | if (err < 0) { |
| 426 | dev_err(&sl->dev, |
Evgeniy Polyakov | 6b72986 | 2005-06-04 01:30:43 +0400 | [diff] [blame] | 427 | "sysfs file creation for [%s] failed. err=%d\n", |
| 428 | sl->dev.bus_id, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | device_unregister(&sl->dev); |
| 430 | return err; |
| 431 | } |
| 432 | |
Evgeniy Polyakov | 99c5bfe | 2005-06-04 01:31:26 +0400 | [diff] [blame] | 433 | if ( sl->attr_bin.read ) { |
| 434 | err = sysfs_create_bin_file(&sl->dev.kobj, &sl->attr_bin); |
| 435 | if (err < 0) { |
| 436 | dev_err(&sl->dev, |
| 437 | "sysfs file creation for [%s] failed. err=%d\n", |
| 438 | sl->dev.bus_id, err); |
| 439 | device_remove_file(&sl->dev, &sl->attr_name); |
| 440 | device_unregister(&sl->dev); |
| 441 | return err; |
| 442 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | list_add_tail(&sl->w1_slave_entry, &sl->master->slist); |
| 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static int w1_attach_slave_device(struct w1_master *dev, struct w1_reg_num *rn) |
| 451 | { |
| 452 | struct w1_slave *sl; |
| 453 | struct w1_family *f; |
| 454 | int err; |
| 455 | struct w1_netlink_msg msg; |
| 456 | |
| 457 | sl = kmalloc(sizeof(struct w1_slave), GFP_KERNEL); |
| 458 | if (!sl) { |
| 459 | dev_err(&dev->dev, |
| 460 | "%s: failed to allocate new slave device.\n", |
| 461 | __func__); |
| 462 | return -ENOMEM; |
| 463 | } |
| 464 | |
| 465 | memset(sl, 0, sizeof(*sl)); |
| 466 | |
| 467 | sl->owner = THIS_MODULE; |
| 468 | sl->master = dev; |
| 469 | set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags); |
| 470 | |
| 471 | memcpy(&sl->reg_num, rn, sizeof(sl->reg_num)); |
| 472 | atomic_set(&sl->refcnt, 0); |
| 473 | init_completion(&sl->dev_released); |
| 474 | |
| 475 | spin_lock(&w1_flock); |
| 476 | f = w1_family_registered(rn->family); |
| 477 | if (!f) { |
Evgeniy Polyakov | 99c5bfe | 2005-06-04 01:31:26 +0400 | [diff] [blame] | 478 | f= &w1_default_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | dev_info(&dev->dev, "Family %x for %02x.%012llx.%02x is not registered.\n", |
| 480 | rn->family, rn->family, |
| 481 | (unsigned long long)rn->id, rn->crc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
| 483 | __w1_family_get(f); |
| 484 | spin_unlock(&w1_flock); |
| 485 | |
| 486 | sl->family = f; |
| 487 | |
| 488 | |
| 489 | err = __w1_attach_slave_device(sl); |
| 490 | if (err < 0) { |
| 491 | dev_err(&dev->dev, "%s: Attaching %s failed.\n", __func__, |
| 492 | sl->name); |
| 493 | w1_family_put(sl->family); |
| 494 | kfree(sl); |
| 495 | return err; |
| 496 | } |
| 497 | |
| 498 | sl->ttl = dev->slave_ttl; |
| 499 | dev->slave_count++; |
| 500 | |
| 501 | memcpy(&msg.id.id, rn, sizeof(msg.id.id)); |
| 502 | msg.type = W1_SLAVE_ADD; |
| 503 | w1_netlink_send(dev, &msg); |
| 504 | |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static void w1_slave_detach(struct w1_slave *sl) |
| 509 | { |
| 510 | struct w1_netlink_msg msg; |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | dev_info(&sl->dev, "%s: detaching %s.\n", __func__, sl->name); |
| 513 | |
| 514 | while (atomic_read(&sl->refcnt)) { |
| 515 | printk(KERN_INFO "Waiting for %s to become free: refcnt=%d.\n", |
| 516 | sl->name, atomic_read(&sl->refcnt)); |
| 517 | |
| 518 | if (msleep_interruptible(1000)) |
| 519 | flush_signals(current); |
| 520 | } |
| 521 | |
Evgeniy Polyakov | 99c5bfe | 2005-06-04 01:31:26 +0400 | [diff] [blame] | 522 | if ( sl->attr_bin.read ) { |
| 523 | sysfs_remove_bin_file (&sl->dev.kobj, &sl->attr_bin); |
| 524 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | device_remove_file(&sl->dev, &sl->attr_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | device_unregister(&sl->dev); |
| 527 | w1_family_put(sl->family); |
| 528 | |
Evgeniy Polyakov | 6adf87b | 2005-06-04 01:29:25 +0400 | [diff] [blame] | 529 | sl->master->slave_count--; |
| 530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | memcpy(&msg.id.id, &sl->reg_num, sizeof(msg.id.id)); |
| 532 | msg.type = W1_SLAVE_REMOVE; |
| 533 | w1_netlink_send(sl->master, &msg); |
| 534 | } |
| 535 | |
| 536 | static struct w1_master *w1_search_master(unsigned long data) |
| 537 | { |
| 538 | struct w1_master *dev; |
| 539 | int found = 0; |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 540 | |
| 541 | spin_lock_bh(&w1_mlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | list_for_each_entry(dev, &w1_masters, w1_master_entry) { |
| 543 | if (dev->bus_master->data == data) { |
| 544 | found = 1; |
| 545 | atomic_inc(&dev->refcnt); |
| 546 | break; |
| 547 | } |
| 548 | } |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 549 | spin_unlock_bh(&w1_mlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
| 551 | return (found)?dev:NULL; |
| 552 | } |
| 553 | |
Evgeniy Polyakov | 6adf87b | 2005-06-04 01:29:25 +0400 | [diff] [blame] | 554 | void w1_reconnect_slaves(struct w1_family *f) |
| 555 | { |
| 556 | struct w1_master *dev; |
| 557 | |
| 558 | spin_lock_bh(&w1_mlock); |
| 559 | list_for_each_entry(dev, &w1_masters, w1_master_entry) { |
| 560 | dev_info(&dev->dev, "Reconnecting slaves in %s into new family %02x.\n", |
| 561 | dev->name, f->fid); |
| 562 | set_bit(W1_MASTER_NEED_RECONNECT, &dev->flags); |
| 563 | } |
| 564 | spin_unlock_bh(&w1_mlock); |
| 565 | } |
| 566 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 567 | static void w1_slave_found(unsigned long data, u64 rn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | { |
| 569 | int slave_count; |
| 570 | struct w1_slave *sl; |
| 571 | struct list_head *ent; |
| 572 | struct w1_reg_num *tmp; |
| 573 | int family_found = 0; |
| 574 | struct w1_master *dev; |
Evgeniy Polyakov | 0e65f82 | 2005-06-30 22:52:38 +0400 | [diff] [blame] | 575 | u64 rn_le = cpu_to_le64(rn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | |
| 577 | dev = w1_search_master(data); |
| 578 | if (!dev) { |
| 579 | printk(KERN_ERR "Failed to find w1 master device for data %08lx, it is impossible.\n", |
| 580 | data); |
| 581 | return; |
| 582 | } |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | tmp = (struct w1_reg_num *) &rn; |
| 585 | |
| 586 | slave_count = 0; |
| 587 | list_for_each(ent, &dev->slist) { |
| 588 | |
| 589 | sl = list_entry(ent, struct w1_slave, w1_slave_entry); |
| 590 | |
| 591 | if (sl->reg_num.family == tmp->family && |
| 592 | sl->reg_num.id == tmp->id && |
| 593 | sl->reg_num.crc == tmp->crc) { |
| 594 | set_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags); |
| 595 | break; |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 596 | } else if (sl->reg_num.family == tmp->family) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | family_found = 1; |
| 598 | break; |
| 599 | } |
| 600 | |
| 601 | slave_count++; |
| 602 | } |
| 603 | |
johnpol@2ka.mipt.ru | 8523ff4 | 2005-04-18 21:16:57 -0700 | [diff] [blame] | 604 | if (slave_count == dev->slave_count && |
Evgeniy Polyakov | 0e65f82 | 2005-06-30 22:52:38 +0400 | [diff] [blame] | 605 | rn && ((rn >> 56) & 0xff) == w1_calc_crc8((u8 *)&rn_le, 7)) { |
johnpol@2ka.mipt.ru | 8523ff4 | 2005-04-18 21:16:57 -0700 | [diff] [blame] | 606 | w1_attach_slave_device(dev, tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | } |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | atomic_dec(&dev->refcnt); |
| 610 | } |
| 611 | |
Evgeniy Polyakov | 6b72986 | 2005-06-04 01:30:43 +0400 | [diff] [blame] | 612 | /** |
| 613 | * Performs a ROM Search & registers any devices found. |
| 614 | * The 1-wire search is a simple binary tree search. |
| 615 | * For each bit of the address, we read two bits and write one bit. |
| 616 | * The bit written will put to sleep all devies that don't match that bit. |
| 617 | * When the two reads differ, the direction choice is obvious. |
| 618 | * When both bits are 0, we must choose a path to take. |
| 619 | * When we can scan all 64 bits without having to choose a path, we are done. |
| 620 | * |
| 621 | * See "Application note 187 1-wire search algorithm" at www.maxim-ic.com |
| 622 | * |
| 623 | * @dev The master device to search |
| 624 | * @cb Function to call when a device is found |
| 625 | */ |
| 626 | void w1_search(struct w1_master *dev, w1_slave_found_callback cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | { |
Evgeniy Polyakov | 6b72986 | 2005-06-04 01:30:43 +0400 | [diff] [blame] | 628 | u64 last_rn, rn, tmp64; |
| 629 | int i, slave_count = 0; |
| 630 | int last_zero, last_device; |
| 631 | int search_bit, desc_bit; |
| 632 | u8 triplet_ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
Evgeniy Polyakov | 6b72986 | 2005-06-04 01:30:43 +0400 | [diff] [blame] | 634 | search_bit = 0; |
| 635 | rn = last_rn = 0; |
| 636 | last_device = 0; |
| 637 | last_zero = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
| 639 | desc_bit = 64; |
| 640 | |
Evgeniy Polyakov | 6b72986 | 2005-06-04 01:30:43 +0400 | [diff] [blame] | 641 | while ( !last_device && (slave_count++ < dev->max_slave_count) ) { |
| 642 | last_rn = rn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | rn = 0; |
| 644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | /* |
| 646 | * Reset bus and all 1-wire device state machines |
| 647 | * so they can respond to our requests. |
| 648 | * |
| 649 | * Return 0 - device(s) present, 1 - no devices present. |
| 650 | */ |
| 651 | if (w1_reset_bus(dev)) { |
Evgeniy Polyakov | 2da5bf8 | 2005-08-12 11:46:22 -0700 | [diff] [blame] | 652 | dev_dbg(&dev->dev, "No devices present on the wire.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | break; |
| 654 | } |
| 655 | |
Evgeniy Polyakov | 6b72986 | 2005-06-04 01:30:43 +0400 | [diff] [blame] | 656 | /* Start the search */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | w1_write_8(dev, W1_SEARCH); |
| 658 | for (i = 0; i < 64; ++i) { |
Evgeniy Polyakov | 6b72986 | 2005-06-04 01:30:43 +0400 | [diff] [blame] | 659 | /* Determine the direction/search bit */ |
| 660 | if (i == desc_bit) |
| 661 | search_bit = 1; /* took the 0 path last time, so take the 1 path */ |
| 662 | else if (i > desc_bit) |
| 663 | search_bit = 0; /* take the 0 path on the next branch */ |
| 664 | else |
| 665 | search_bit = ((last_rn >> i) & 0x1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
Evgeniy Polyakov | 6b72986 | 2005-06-04 01:30:43 +0400 | [diff] [blame] | 667 | /** Read two bits and write one bit */ |
| 668 | triplet_ret = w1_triplet(dev, search_bit); |
| 669 | |
| 670 | /* quit if no device responded */ |
| 671 | if ( (triplet_ret & 0x03) == 0x03 ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | break; |
| 673 | |
Evgeniy Polyakov | 6b72986 | 2005-06-04 01:30:43 +0400 | [diff] [blame] | 674 | /* If both directions were valid, and we took the 0 path... */ |
| 675 | if (triplet_ret == 0) |
| 676 | last_zero = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
Evgeniy Polyakov | 6b72986 | 2005-06-04 01:30:43 +0400 | [diff] [blame] | 678 | /* extract the direction taken & update the device number */ |
| 679 | tmp64 = (triplet_ret >> 2); |
| 680 | rn |= (tmp64 << i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
Evgeniy Polyakov | 6b72986 | 2005-06-04 01:30:43 +0400 | [diff] [blame] | 683 | if ( (triplet_ret & 0x03) != 0x03 ) { |
| 684 | if ( (desc_bit == last_zero) || (last_zero < 0)) |
| 685 | last_device = 1; |
| 686 | desc_bit = last_zero; |
| 687 | cb(dev->bus_master->data, rn); |
| 688 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 692 | static int w1_control(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 694 | struct w1_slave *sl, *sln; |
| 695 | struct w1_master *dev, *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | int err, have_to_wait = 0; |
| 697 | |
| 698 | daemonize("w1_control"); |
| 699 | allow_signal(SIGTERM); |
| 700 | |
| 701 | while (!control_needs_exit || have_to_wait) { |
| 702 | have_to_wait = 0; |
| 703 | |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 704 | try_to_freeze(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | msleep_interruptible(w1_timeout * 1000); |
| 706 | |
| 707 | if (signal_pending(current)) |
| 708 | flush_signals(current); |
| 709 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 710 | list_for_each_entry_safe(dev, n, &w1_masters, w1_master_entry) { |
Evgeniy Polyakov | 6adf87b | 2005-06-04 01:29:25 +0400 | [diff] [blame] | 711 | if (!control_needs_exit && !dev->flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | continue; |
| 713 | /* |
| 714 | * Little race: we can create thread but not set the flag. |
| 715 | * Get a chance for external process to set flag up. |
| 716 | */ |
| 717 | if (!dev->initialized) { |
| 718 | have_to_wait = 1; |
| 719 | continue; |
| 720 | } |
| 721 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | if (control_needs_exit) { |
Evgeniy Polyakov | 6adf87b | 2005-06-04 01:29:25 +0400 | [diff] [blame] | 723 | set_bit(W1_MASTER_NEED_EXIT, &dev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | |
| 725 | err = kill_proc(dev->kpid, SIGTERM, 1); |
| 726 | if (err) |
| 727 | dev_err(&dev->dev, |
| 728 | "Failed to send signal to w1 kernel thread %d.\n", |
| 729 | dev->kpid); |
| 730 | } |
| 731 | |
Evgeniy Polyakov | 6adf87b | 2005-06-04 01:29:25 +0400 | [diff] [blame] | 732 | if (test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) { |
| 733 | wait_for_completion(&dev->dev_exited); |
| 734 | spin_lock_bh(&w1_mlock); |
| 735 | list_del(&dev->w1_master_entry); |
| 736 | spin_unlock_bh(&w1_mlock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | |
Evgeniy Polyakov | 6adf87b | 2005-06-04 01:29:25 +0400 | [diff] [blame] | 738 | list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) { |
| 739 | list_del(&sl->w1_slave_entry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | |
Evgeniy Polyakov | 6adf87b | 2005-06-04 01:29:25 +0400 | [diff] [blame] | 741 | w1_slave_detach(sl); |
| 742 | kfree(sl); |
| 743 | } |
| 744 | w1_destroy_master_attributes(dev); |
| 745 | atomic_dec(&dev->refcnt); |
| 746 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | } |
Evgeniy Polyakov | 6adf87b | 2005-06-04 01:29:25 +0400 | [diff] [blame] | 748 | |
| 749 | if (test_bit(W1_MASTER_NEED_RECONNECT, &dev->flags)) { |
| 750 | dev_info(&dev->dev, "Reconnecting slaves in device %s.\n", dev->name); |
| 751 | down(&dev->mutex); |
| 752 | list_for_each_entry(sl, &dev->slist, w1_slave_entry) { |
| 753 | if (sl->family->fid == W1_FAMILY_DEFAULT) { |
| 754 | struct w1_reg_num rn; |
| 755 | list_del(&sl->w1_slave_entry); |
| 756 | w1_slave_detach(sl); |
| 757 | |
| 758 | memcpy(&rn, &sl->reg_num, sizeof(rn)); |
| 759 | |
| 760 | kfree(sl); |
| 761 | |
| 762 | w1_attach_slave_device(dev, &rn); |
| 763 | } |
| 764 | } |
| 765 | clear_bit(W1_MASTER_NEED_RECONNECT, &dev->flags); |
| 766 | up(&dev->mutex); |
| 767 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | |
| 771 | complete_and_exit(&w1_control_complete, 0); |
| 772 | } |
| 773 | |
| 774 | int w1_process(void *data) |
| 775 | { |
| 776 | struct w1_master *dev = (struct w1_master *) data; |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 777 | struct w1_slave *sl, *sln; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
| 779 | daemonize("%s", dev->name); |
| 780 | allow_signal(SIGTERM); |
| 781 | |
Evgeniy Polyakov | 6adf87b | 2005-06-04 01:29:25 +0400 | [diff] [blame] | 782 | while (!test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) { |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 783 | try_to_freeze(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | msleep_interruptible(w1_timeout * 1000); |
| 785 | |
| 786 | if (signal_pending(current)) |
| 787 | flush_signals(current); |
| 788 | |
Evgeniy Polyakov | 6adf87b | 2005-06-04 01:29:25 +0400 | [diff] [blame] | 789 | if (test_bit(W1_MASTER_NEED_EXIT, &dev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | break; |
| 791 | |
| 792 | if (!dev->initialized) |
| 793 | continue; |
| 794 | |
Evgeniy Polyakov | 2a9d0c1 | 2005-06-04 01:31:02 +0400 | [diff] [blame] | 795 | if (dev->search_count == 0) |
| 796 | continue; |
| 797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | if (down_interruptible(&dev->mutex)) |
| 799 | continue; |
| 800 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 801 | list_for_each_entry(sl, &dev->slist, w1_slave_entry) |
Evgeniy Polyakov | 2a9d0c1 | 2005-06-04 01:31:02 +0400 | [diff] [blame] | 802 | clear_bit(W1_SLAVE_ACTIVE, (long *)&sl->flags); |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | w1_search_devices(dev, w1_slave_found); |
| 805 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 806 | list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) { |
| 807 | if (!test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags) && !--sl->ttl) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | list_del (&sl->w1_slave_entry); |
| 809 | |
| 810 | w1_slave_detach (sl); |
| 811 | kfree (sl); |
| 812 | |
| 813 | dev->slave_count--; |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 814 | } else if (test_bit(W1_SLAVE_ACTIVE, (unsigned long *)&sl->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | sl->ttl = dev->slave_ttl; |
| 816 | } |
Evgeniy Polyakov | 2a9d0c1 | 2005-06-04 01:31:02 +0400 | [diff] [blame] | 817 | |
| 818 | if (dev->search_count > 0) |
| 819 | dev->search_count--; |
| 820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | up(&dev->mutex); |
| 822 | } |
| 823 | |
| 824 | atomic_dec(&dev->refcnt); |
| 825 | complete_and_exit(&dev->dev_exited, 0); |
| 826 | |
| 827 | return 0; |
| 828 | } |
| 829 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 830 | static int w1_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | { |
| 832 | int retval; |
| 833 | |
| 834 | printk(KERN_INFO "Driver for 1-wire Dallas network protocol.\n"); |
| 835 | |
| 836 | retval = bus_register(&w1_bus_type); |
| 837 | if (retval) { |
| 838 | printk(KERN_ERR "Failed to register bus. err=%d.\n", retval); |
| 839 | goto err_out_exit_init; |
| 840 | } |
| 841 | |
Evgeniy Polyakov | 7f772ed | 2005-08-11 13:20:07 +0400 | [diff] [blame] | 842 | retval = driver_register(&w1_master_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | if (retval) { |
| 844 | printk(KERN_ERR |
| 845 | "Failed to register master driver. err=%d.\n", |
| 846 | retval); |
| 847 | goto err_out_bus_unregister; |
| 848 | } |
| 849 | |
Evgeniy Polyakov | 7f772ed | 2005-08-11 13:20:07 +0400 | [diff] [blame] | 850 | retval = driver_register(&w1_slave_driver); |
| 851 | if (retval) { |
| 852 | printk(KERN_ERR |
| 853 | "Failed to register master driver. err=%d.\n", |
| 854 | retval); |
| 855 | goto err_out_master_unregister; |
| 856 | } |
| 857 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | control_thread = kernel_thread(&w1_control, NULL, 0); |
| 859 | if (control_thread < 0) { |
| 860 | printk(KERN_ERR "Failed to create control thread. err=%d\n", |
| 861 | control_thread); |
| 862 | retval = control_thread; |
Evgeniy Polyakov | 7f772ed | 2005-08-11 13:20:07 +0400 | [diff] [blame] | 863 | goto err_out_slave_unregister; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | return 0; |
| 867 | |
Evgeniy Polyakov | 7f772ed | 2005-08-11 13:20:07 +0400 | [diff] [blame] | 868 | err_out_slave_unregister: |
| 869 | driver_unregister(&w1_slave_driver); |
| 870 | |
| 871 | err_out_master_unregister: |
| 872 | driver_unregister(&w1_master_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | |
| 874 | err_out_bus_unregister: |
| 875 | bus_unregister(&w1_bus_type); |
| 876 | |
| 877 | err_out_exit_init: |
| 878 | return retval; |
| 879 | } |
| 880 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 881 | static void w1_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | { |
| 883 | struct w1_master *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
Evgeniy Polyakov | 7785925 | 2005-05-20 22:33:25 +0400 | [diff] [blame] | 885 | list_for_each_entry(dev, &w1_masters, w1_master_entry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | __w1_remove_master_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | |
| 888 | control_needs_exit = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | wait_for_completion(&w1_control_complete); |
| 890 | |
Evgeniy Polyakov | 7f772ed | 2005-08-11 13:20:07 +0400 | [diff] [blame] | 891 | driver_unregister(&w1_slave_driver); |
| 892 | driver_unregister(&w1_master_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | bus_unregister(&w1_bus_type); |
| 894 | } |
| 895 | |
| 896 | module_init(w1_init); |
| 897 | module_exit(w1_fini); |