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