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