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