]>
Commit | Line | Data |
---|---|---|
c942fddf | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 | 2 | /* |
a8018766 | 3 | * Copyright (c) 2004 Evgeniy Polyakov <zbr@ioremap.net> |
1da177e4 LT |
4 | */ |
5 | ||
6 | #include <linux/kernel.h> | |
7 | #include <linux/list.h> | |
8 | #include <linux/delay.h> | |
674a396c | 9 | #include <linux/kthread.h> |
5a0e3ad6 | 10 | #include <linux/slab.h> |
3f07c014 | 11 | #include <linux/sched/signal.h> |
9623932c | 12 | #include <linux/export.h> |
4d184129 | 13 | #include <linux/moduleparam.h> |
1da177e4 | 14 | |
de0d6dbd | 15 | #include "w1_internal.h" |
1da177e4 LT |
16 | #include "w1_netlink.h" |
17 | ||
9141f57c DF |
18 | static int w1_search_count = -1; /* Default is continual scan */ |
19 | module_param_named(search_count, w1_search_count, int, 0); | |
1da177e4 | 20 | |
6a158c0d DF |
21 | static int w1_enable_pullup = 1; |
22 | module_param_named(enable_pullup, w1_enable_pullup, int, 0); | |
23 | ||
7242d42a | 24 | static struct w1_master *w1_alloc_dev(u32 id, int slave_count, int slave_ttl, |
7785925d EP |
25 | struct device_driver *driver, |
26 | struct device *device) | |
1da177e4 LT |
27 | { |
28 | struct w1_master *dev; | |
29 | int err; | |
30 | ||
31 | /* | |
32 | * We are in process context(kernel thread), so can sleep. | |
33 | */ | |
dd00cc48 | 34 | dev = kzalloc(sizeof(struct w1_master) + sizeof(struct w1_bus_master), GFP_KERNEL); |
1da177e4 | 35 | if (!dev) { |
fdc9167a | 36 | pr_err("Failed to allocate %zd bytes for new w1 device.\n", |
1da177e4 LT |
37 | sizeof(struct w1_master)); |
38 | return NULL; | |
39 | } | |
40 | ||
1da177e4 LT |
41 | |
42 | dev->bus_master = (struct w1_bus_master *)(dev + 1); | |
43 | ||
7785925d EP |
44 | dev->owner = THIS_MODULE; |
45 | dev->max_slave_count = slave_count; | |
46 | dev->slave_count = 0; | |
47 | dev->attempts = 0; | |
7785925d EP |
48 | dev->initialized = 0; |
49 | dev->id = id; | |
1da177e4 | 50 | dev->slave_ttl = slave_ttl; |
9141f57c | 51 | dev->search_count = w1_search_count; |
6a158c0d | 52 | dev->enable_pullup = w1_enable_pullup; |
1da177e4 | 53 | |
01e14d6d DF |
54 | /* 1 for w1_process to decrement |
55 | * 1 for __w1_remove_master_device to decrement | |
56 | */ | |
1da177e4 LT |
57 | atomic_set(&dev->refcnt, 2); |
58 | ||
59 | INIT_LIST_HEAD(&dev->slist); | |
9fcbbac5 | 60 | INIT_LIST_HEAD(&dev->async_list); |
abd52a13 | 61 | mutex_init(&dev->mutex); |
b02f8bed | 62 | mutex_init(&dev->bus_mutex); |
9fcbbac5 | 63 | mutex_init(&dev->list_mutex); |
1da177e4 | 64 | |
1da177e4 | 65 | memcpy(&dev->dev, device, sizeof(struct device)); |
40f91de6 | 66 | dev_set_name(&dev->dev, "w1_bus_master%u", dev->id); |
1da177e4 | 67 | snprintf(dev->name, sizeof(dev->name), "w1_bus_master%u", dev->id); |
68a436ae | 68 | dev->dev.init_name = dev->name; |
1da177e4 LT |
69 | |
70 | dev->driver = driver; | |
71 | ||
1da177e4 | 72 | dev->seq = 1; |
1da177e4 LT |
73 | |
74 | err = device_register(&dev->dev); | |
75 | if (err) { | |
fdc9167a | 76 | pr_err("Failed to register master device. err=%d\n", err); |
5052436d | 77 | put_device(&dev->dev); |
1da177e4 LT |
78 | dev = NULL; |
79 | } | |
80 | ||
81 | return dev; | |
82 | } | |
83 | ||
2c5bfdac | 84 | static void w1_free_dev(struct w1_master *dev) |
1da177e4 LT |
85 | { |
86 | device_unregister(&dev->dev); | |
1da177e4 LT |
87 | } |
88 | ||
b3be177a DF |
89 | /** |
90 | * w1_add_master_device() - registers a new master device | |
91 | * @master: master bus device to register | |
92 | */ | |
1da177e4 LT |
93 | int w1_add_master_device(struct w1_bus_master *master) |
94 | { | |
af00a2d5 | 95 | struct w1_master *dev, *entry; |
1da177e4 LT |
96 | int retval = 0; |
97 | struct w1_netlink_msg msg; | |
af00a2d5 | 98 | int id, found; |
1da177e4 | 99 | |
7242d42a TW |
100 | /* validate minimum functionality */ |
101 | if (!(master->touch_bit && master->reset_bus) && | |
102 | !(master->write_bit && master->read_bit) && | |
c1f858b7 | 103 | !(master->write_byte && master->read_byte && master->reset_bus)) { |
fdc9167a | 104 | pr_err("w1_add_master_device: invalid function set\n"); |
6adf87bd | 105 | return(-EINVAL); |
7242d42a | 106 | } |
be57ce26 | 107 | |
af00a2d5 DF |
108 | /* Lock until the device is added (or not) to w1_masters. */ |
109 | mutex_lock(&w1_mlock); | |
110 | /* Search for the first available id (starting at 1). */ | |
111 | id = 0; | |
112 | do { | |
113 | ++id; | |
114 | found = 0; | |
115 | list_for_each_entry(entry, &w1_masters, w1_master_entry) { | |
116 | if (entry->id == id) { | |
117 | found = 1; | |
118 | break; | |
119 | } | |
120 | } | |
121 | } while (found); | |
122 | ||
123 | dev = w1_alloc_dev(id, w1_max_slave_count, w1_max_slave_ttl, | |
124 | &w1_master_driver, &w1_master_device); | |
125 | if (!dev) { | |
126 | mutex_unlock(&w1_mlock); | |
1da177e4 | 127 | return -ENOMEM; |
af00a2d5 | 128 | } |
1da177e4 | 129 | |
01e14d6d | 130 | retval = w1_create_master_attributes(dev); |
af00a2d5 DF |
131 | if (retval) { |
132 | mutex_unlock(&w1_mlock); | |
01e14d6d | 133 | goto err_out_free_dev; |
af00a2d5 | 134 | } |
01e14d6d DF |
135 | |
136 | memcpy(dev->bus_master, master, sizeof(struct w1_bus_master)); | |
137 | ||
138 | dev->initialized = 1; | |
139 | ||
674a396c EP |
140 | dev->thread = kthread_run(&w1_process, dev, "%s", dev->name); |
141 | if (IS_ERR(dev->thread)) { | |
142 | retval = PTR_ERR(dev->thread); | |
1da177e4 LT |
143 | dev_err(&dev->dev, |
144 | "Failed to create new kernel thread. err=%d\n", | |
674a396c | 145 | retval); |
af00a2d5 | 146 | mutex_unlock(&w1_mlock); |
01e14d6d | 147 | goto err_out_rm_attr; |
1da177e4 LT |
148 | } |
149 | ||
1da177e4 | 150 | list_add(&dev->w1_master_entry, &w1_masters); |
abd52a13 | 151 | mutex_unlock(&w1_mlock); |
1da177e4 | 152 | |
12003375 | 153 | memset(&msg, 0, sizeof(msg)); |
1da177e4 | 154 | msg.id.mst.id = dev->id; |
1da177e4 LT |
155 | msg.type = W1_MASTER_ADD; |
156 | w1_netlink_send(dev, &msg); | |
157 | ||
158 | return 0; | |
159 | ||
01e14d6d | 160 | #if 0 /* Thread cleanup code, not required currently. */ |
1da177e4 | 161 | err_out_kill_thread: |
42105698 | 162 | set_bit(W1_ABORT_SEARCH, &dev->flags); |
674a396c | 163 | kthread_stop(dev->thread); |
01e14d6d DF |
164 | #endif |
165 | err_out_rm_attr: | |
166 | w1_destroy_master_attributes(dev); | |
1da177e4 LT |
167 | err_out_free_dev: |
168 | w1_free_dev(dev); | |
169 | ||
170 | return retval; | |
171 | } | |
50fa2951 | 172 | EXPORT_SYMBOL(w1_add_master_device); |
1da177e4 LT |
173 | |
174 | void __w1_remove_master_device(struct w1_master *dev) | |
175 | { | |
1da177e4 | 176 | struct w1_netlink_msg msg; |
c30c9b15 | 177 | struct w1_slave *sl, *sln; |
1da177e4 | 178 | |
c30c9b15 DF |
179 | mutex_lock(&w1_mlock); |
180 | list_del(&dev->w1_master_entry); | |
181 | mutex_unlock(&w1_mlock); | |
182 | ||
9fcbbac5 DF |
183 | set_bit(W1_ABORT_SEARCH, &dev->flags); |
184 | kthread_stop(dev->thread); | |
185 | ||
c30c9b15 | 186 | mutex_lock(&dev->mutex); |
9fcbbac5 DF |
187 | mutex_lock(&dev->list_mutex); |
188 | list_for_each_entry_safe(sl, sln, &dev->slist, w1_slave_entry) { | |
189 | mutex_unlock(&dev->list_mutex); | |
c30c9b15 | 190 | w1_slave_detach(sl); |
9fcbbac5 DF |
191 | mutex_lock(&dev->list_mutex); |
192 | } | |
c30c9b15 | 193 | w1_destroy_master_attributes(dev); |
9fcbbac5 | 194 | mutex_unlock(&dev->list_mutex); |
c30c9b15 DF |
195 | mutex_unlock(&dev->mutex); |
196 | atomic_dec(&dev->refcnt); | |
197 | ||
1da177e4 | 198 | while (atomic_read(&dev->refcnt)) { |
674a396c | 199 | dev_info(&dev->dev, "Waiting for %s to become free: refcnt=%d.\n", |
1da177e4 LT |
200 | dev->name, atomic_read(&dev->refcnt)); |
201 | ||
202 | if (msleep_interruptible(1000)) | |
203 | flush_signals(current); | |
a0f10464 | 204 | mutex_lock(&dev->list_mutex); |
9fcbbac5 | 205 | w1_process_callbacks(dev); |
a0f10464 | 206 | mutex_unlock(&dev->list_mutex); |
1da177e4 | 207 | } |
a0f10464 | 208 | mutex_lock(&dev->list_mutex); |
9fcbbac5 | 209 | w1_process_callbacks(dev); |
a0f10464 | 210 | mutex_unlock(&dev->list_mutex); |
1da177e4 | 211 | |
12003375 | 212 | memset(&msg, 0, sizeof(msg)); |
1da177e4 | 213 | msg.id.mst.id = dev->id; |
1da177e4 LT |
214 | msg.type = W1_MASTER_REMOVE; |
215 | w1_netlink_send(dev, &msg); | |
216 | ||
217 | w1_free_dev(dev); | |
218 | } | |
219 | ||
b3be177a DF |
220 | /** |
221 | * w1_remove_master_device() - unregister a master device | |
222 | * @bm: master bus device to remove | |
223 | */ | |
1da177e4 LT |
224 | void w1_remove_master_device(struct w1_bus_master *bm) |
225 | { | |
59d9445e | 226 | struct w1_master *dev, *found = NULL; |
1da177e4 | 227 | |
7785925d | 228 | list_for_each_entry(dev, &w1_masters, w1_master_entry) { |
1da177e4 LT |
229 | if (!dev->initialized) |
230 | continue; | |
231 | ||
59d9445e EP |
232 | if (dev->bus_master->data == bm->data) { |
233 | found = dev; | |
1da177e4 | 234 | break; |
59d9445e | 235 | } |
1da177e4 LT |
236 | } |
237 | ||
59d9445e | 238 | if (!found) { |
fdc9167a | 239 | pr_err("Device doesn't exist.\n"); |
1da177e4 LT |
240 | return; |
241 | } | |
242 | ||
59d9445e | 243 | __w1_remove_master_device(found); |
1da177e4 | 244 | } |
1da177e4 | 245 | EXPORT_SYMBOL(w1_remove_master_device); |