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