Commit | Line | Data |
---|---|---|
989d42e8 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * bus.c - bus driver management | |
4 | * | |
5 | * Copyright (c) 2002-3 Patrick Mochel | |
6 | * Copyright (c) 2002-3 Open Source Development Labs | |
e5dd1278 GKH |
7 | * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de> |
8 | * Copyright (c) 2007 Novell Inc. | |
1da177e4 LT |
9 | */ |
10 | ||
765230b5 | 11 | #include <linux/async.h> |
5aee2bf2 | 12 | #include <linux/device/bus.h> |
1da177e4 LT |
13 | #include <linux/device.h> |
14 | #include <linux/module.h> | |
15 | #include <linux/errno.h> | |
5a0e3ad6 | 16 | #include <linux/slab.h> |
1da177e4 LT |
17 | #include <linux/init.h> |
18 | #include <linux/string.h> | |
ca22e56d | 19 | #include <linux/mutex.h> |
63967685 | 20 | #include <linux/sysfs.h> |
1da177e4 LT |
21 | #include "base.h" |
22 | #include "power/power.h" | |
23 | ||
ca22e56d | 24 | /* /sys/devices/system */ |
97ec448a | 25 | static struct kset *system_kset; |
ca22e56d | 26 | |
273afac6 GKH |
27 | /* /sys/bus */ |
28 | static struct kset *bus_kset; | |
29 | ||
1da177e4 | 30 | #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr) |
1da177e4 LT |
31 | |
32 | /* | |
33 | * sysfs bindings for drivers | |
34 | */ | |
35 | ||
36 | #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr) | |
1da177e4 | 37 | |
4f4b3743 DV |
38 | #define DRIVER_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \ |
39 | struct driver_attribute driver_attr_##_name = \ | |
40 | __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) | |
1da177e4 | 41 | |
b8c5cec2 KS |
42 | static int __must_check bus_rescan_devices_helper(struct device *dev, |
43 | void *data); | |
44 | ||
273afac6 GKH |
45 | /** |
46 | * bus_to_subsys - Turn a struct bus_type into a struct subsys_private | |
47 | * | |
48 | * @bus: pointer to the struct bus_type to look up | |
49 | * | |
50 | * The driver core internals needs to work on the subsys_private structure, not | |
51 | * the external struct bus_type pointer. This function walks the list of | |
52 | * registered busses in the system and finds the matching one and returns the | |
53 | * internal struct subsys_private that relates to that bus. | |
54 | * | |
55 | * Note, the reference count of the return value is INCREMENTED if it is not | |
56 | * NULL. A call to subsys_put() must be done when finished with the pointer in | |
57 | * order for it to be properly freed. | |
58 | */ | |
59 | static struct subsys_private *bus_to_subsys(const struct bus_type *bus) | |
60 | { | |
61 | struct subsys_private *sp = NULL; | |
62 | struct kobject *kobj; | |
63 | ||
64 | if (!bus) | |
65 | return NULL; | |
66 | ||
67 | spin_lock(&bus_kset->list_lock); | |
68 | ||
69 | if (list_empty(&bus_kset->list)) | |
70 | goto done; | |
71 | ||
72 | list_for_each_entry(kobj, &bus_kset->list, entry) { | |
73 | struct kset *kset = container_of(kobj, struct kset, kobj); | |
74 | ||
75 | sp = container_of_const(kset, struct subsys_private, subsys); | |
76 | if (sp->bus == bus) | |
77 | goto done; | |
78 | } | |
79 | sp = NULL; | |
80 | done: | |
81 | sp = subsys_get(sp); | |
82 | spin_unlock(&bus_kset->list_lock); | |
83 | return sp; | |
84 | } | |
85 | ||
5901d014 GKH |
86 | static struct bus_type *bus_get(struct bus_type *bus) |
87 | { | |
273afac6 GKH |
88 | struct subsys_private *sp = bus_to_subsys(bus); |
89 | ||
90 | if (sp) | |
c6f7e72a | 91 | return bus; |
c6f7e72a | 92 | return NULL; |
5901d014 GKH |
93 | } |
94 | ||
273afac6 | 95 | static void bus_put(const struct bus_type *bus) |
fc1ede58 | 96 | { |
273afac6 GKH |
97 | struct subsys_private *sp = bus_to_subsys(bus); |
98 | ||
99 | /* two puts are required as the call to bus_to_subsys incremented it again */ | |
100 | subsys_put(sp); | |
101 | subsys_put(sp); | |
fc1ede58 GKH |
102 | } |
103 | ||
4a3ad20c GKH |
104 | static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr, |
105 | char *buf) | |
1da177e4 | 106 | { |
4a3ad20c | 107 | struct driver_attribute *drv_attr = to_drv_attr(attr); |
e5dd1278 | 108 | struct driver_private *drv_priv = to_driver(kobj); |
4a0c20bf | 109 | ssize_t ret = -EIO; |
1da177e4 LT |
110 | |
111 | if (drv_attr->show) | |
e5dd1278 | 112 | ret = drv_attr->show(drv_priv->driver, buf); |
1da177e4 LT |
113 | return ret; |
114 | } | |
115 | ||
4a3ad20c GKH |
116 | static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr, |
117 | const char *buf, size_t count) | |
1da177e4 | 118 | { |
4a3ad20c | 119 | struct driver_attribute *drv_attr = to_drv_attr(attr); |
e5dd1278 | 120 | struct driver_private *drv_priv = to_driver(kobj); |
4a0c20bf | 121 | ssize_t ret = -EIO; |
1da177e4 LT |
122 | |
123 | if (drv_attr->store) | |
e5dd1278 | 124 | ret = drv_attr->store(drv_priv->driver, buf, count); |
1da177e4 LT |
125 | return ret; |
126 | } | |
127 | ||
52cf25d0 | 128 | static const struct sysfs_ops driver_sysfs_ops = { |
1da177e4 LT |
129 | .show = drv_attr_show, |
130 | .store = drv_attr_store, | |
131 | }; | |
132 | ||
e5dd1278 | 133 | static void driver_release(struct kobject *kobj) |
1da177e4 | 134 | { |
e5dd1278 GKH |
135 | struct driver_private *drv_priv = to_driver(kobj); |
136 | ||
2b3a302a | 137 | pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__); |
e5dd1278 | 138 | kfree(drv_priv); |
1da177e4 LT |
139 | } |
140 | ||
c83d9ab4 | 141 | static const struct kobj_type driver_ktype = { |
1da177e4 LT |
142 | .sysfs_ops = &driver_sysfs_ops, |
143 | .release = driver_release, | |
144 | }; | |
145 | ||
1da177e4 LT |
146 | /* |
147 | * sysfs bindings for buses | |
148 | */ | |
4a3ad20c GKH |
149 | static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr, |
150 | char *buf) | |
1da177e4 | 151 | { |
4a3ad20c | 152 | struct bus_attribute *bus_attr = to_bus_attr(attr); |
6b6e39a6 | 153 | struct subsys_private *subsys_priv = to_subsys_private(kobj); |
1da177e4 LT |
154 | ssize_t ret = 0; |
155 | ||
156 | if (bus_attr->show) | |
6b6e39a6 | 157 | ret = bus_attr->show(subsys_priv->bus, buf); |
1da177e4 LT |
158 | return ret; |
159 | } | |
160 | ||
4a3ad20c GKH |
161 | static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr, |
162 | const char *buf, size_t count) | |
1da177e4 | 163 | { |
4a3ad20c | 164 | struct bus_attribute *bus_attr = to_bus_attr(attr); |
6b6e39a6 | 165 | struct subsys_private *subsys_priv = to_subsys_private(kobj); |
1da177e4 LT |
166 | ssize_t ret = 0; |
167 | ||
168 | if (bus_attr->store) | |
6b6e39a6 | 169 | ret = bus_attr->store(subsys_priv->bus, buf, count); |
1da177e4 LT |
170 | return ret; |
171 | } | |
172 | ||
52cf25d0 | 173 | static const struct sysfs_ops bus_sysfs_ops = { |
1da177e4 LT |
174 | .show = bus_attr_show, |
175 | .store = bus_attr_store, | |
176 | }; | |
177 | ||
0396f286 | 178 | int bus_create_file(const struct bus_type *bus, struct bus_attribute *attr) |
1da177e4 | 179 | { |
0396f286 | 180 | struct subsys_private *sp = bus_to_subsys(bus); |
1da177e4 | 181 | int error; |
0396f286 GKH |
182 | |
183 | if (!sp) | |
184 | return -EINVAL; | |
185 | ||
186 | error = sysfs_create_file(&sp->subsys.kobj, &attr->attr); | |
187 | ||
188 | subsys_put(sp); | |
1da177e4 LT |
189 | return error; |
190 | } | |
4a3ad20c | 191 | EXPORT_SYMBOL_GPL(bus_create_file); |
1da177e4 | 192 | |
0396f286 | 193 | void bus_remove_file(const struct bus_type *bus, struct bus_attribute *attr) |
1da177e4 | 194 | { |
0396f286 GKH |
195 | struct subsys_private *sp = bus_to_subsys(bus); |
196 | ||
197 | if (!sp) | |
198 | return; | |
199 | ||
200 | sysfs_remove_file(&sp->subsys.kobj, &attr->attr); | |
201 | subsys_put(sp); | |
1da177e4 | 202 | } |
4a3ad20c | 203 | EXPORT_SYMBOL_GPL(bus_remove_file); |
1da177e4 | 204 | |
174be70b BVA |
205 | static void bus_release(struct kobject *kobj) |
206 | { | |
371fd7a2 | 207 | struct subsys_private *priv = to_subsys_private(kobj); |
174be70b | 208 | |
37e98d9b | 209 | lockdep_unregister_key(&priv->lock_key); |
174be70b | 210 | kfree(priv); |
174be70b BVA |
211 | } |
212 | ||
c83d9ab4 | 213 | static const struct kobj_type bus_ktype = { |
1da177e4 | 214 | .sysfs_ops = &bus_sysfs_ops, |
174be70b | 215 | .release = bus_release, |
80f03e34 KS |
216 | }; |
217 | ||
c45a88bb | 218 | static int bus_uevent_filter(const struct kobject *kobj) |
80f03e34 | 219 | { |
ee6d3dd4 | 220 | const struct kobj_type *ktype = get_ktype(kobj); |
80f03e34 KS |
221 | |
222 | if (ktype == &bus_ktype) | |
223 | return 1; | |
224 | return 0; | |
225 | } | |
1da177e4 | 226 | |
9cd43611 | 227 | static const struct kset_uevent_ops bus_uevent_ops = { |
80f03e34 | 228 | .filter = bus_uevent_filter, |
1da177e4 LT |
229 | }; |
230 | ||
2b08c8d0 | 231 | /* Manually detach a device from its associated driver. */ |
2581c9cc GKH |
232 | static ssize_t unbind_store(struct device_driver *drv, const char *buf, |
233 | size_t count) | |
151ef38f | 234 | { |
5901d014 | 235 | struct bus_type *bus = bus_get(drv->bus); |
151ef38f GKH |
236 | struct device *dev; |
237 | int err = -ENODEV; | |
238 | ||
1f9ffc04 | 239 | dev = bus_find_device_by_name(bus, NULL, buf); |
2b08c8d0 | 240 | if (dev && dev->driver == drv) { |
ed88747c | 241 | device_driver_detach(dev); |
151ef38f GKH |
242 | err = count; |
243 | } | |
2b08c8d0 | 244 | put_device(dev); |
fc1ede58 | 245 | bus_put(bus); |
2b08c8d0 | 246 | return err; |
151ef38f | 247 | } |
16b0dd40 | 248 | static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, 0200, NULL, unbind_store); |
151ef38f | 249 | |
afdce75f GKH |
250 | /* |
251 | * Manually attach a device to a driver. | |
252 | * Note: the driver must want to bind to the device, | |
253 | * it is not possible to override the driver's id table. | |
254 | */ | |
2581c9cc GKH |
255 | static ssize_t bind_store(struct device_driver *drv, const char *buf, |
256 | size_t count) | |
afdce75f | 257 | { |
5901d014 | 258 | struct bus_type *bus = bus_get(drv->bus); |
afdce75f GKH |
259 | struct device *dev; |
260 | int err = -ENODEV; | |
261 | ||
1f9ffc04 | 262 | dev = bus_find_device_by_name(bus, NULL, buf); |
204db60c | 263 | if (dev && driver_match_device(drv, dev)) { |
ed88747c | 264 | err = device_driver_attach(drv, dev); |
ef6dcbdd | 265 | if (!err) { |
4a3ad20c | 266 | /* success */ |
37225401 | 267 | err = count; |
4a3ad20c | 268 | } |
afdce75f | 269 | } |
2b08c8d0 | 270 | put_device(dev); |
fc1ede58 | 271 | bus_put(bus); |
2b08c8d0 | 272 | return err; |
afdce75f | 273 | } |
16b0dd40 | 274 | static DRIVER_ATTR_IGNORE_LOCKDEP(bind, 0200, NULL, bind_store); |
afdce75f | 275 | |
2e7189b6 | 276 | static ssize_t drivers_autoprobe_show(struct bus_type *bus, char *buf) |
b8c5cec2 | 277 | { |
a00fdb98 GKH |
278 | struct subsys_private *sp = bus_to_subsys(bus); |
279 | int ret; | |
280 | ||
281 | if (!sp) | |
282 | return -EINVAL; | |
283 | ||
284 | ret = sysfs_emit(buf, "%d\n", sp->drivers_autoprobe); | |
285 | subsys_put(sp); | |
286 | return ret; | |
b8c5cec2 KS |
287 | } |
288 | ||
2e7189b6 | 289 | static ssize_t drivers_autoprobe_store(struct bus_type *bus, |
b8c5cec2 KS |
290 | const char *buf, size_t count) |
291 | { | |
a00fdb98 GKH |
292 | struct subsys_private *sp = bus_to_subsys(bus); |
293 | ||
294 | if (!sp) | |
295 | return -EINVAL; | |
296 | ||
b8c5cec2 | 297 | if (buf[0] == '0') |
a00fdb98 | 298 | sp->drivers_autoprobe = 0; |
b8c5cec2 | 299 | else |
a00fdb98 GKH |
300 | sp->drivers_autoprobe = 1; |
301 | ||
302 | subsys_put(sp); | |
b8c5cec2 KS |
303 | return count; |
304 | } | |
305 | ||
2e7189b6 | 306 | static ssize_t drivers_probe_store(struct bus_type *bus, |
b8c5cec2 KS |
307 | const char *buf, size_t count) |
308 | { | |
309 | struct device *dev; | |
0372ffb3 | 310 | int err = -EINVAL; |
b8c5cec2 | 311 | |
1f9ffc04 | 312 | dev = bus_find_device_by_name(bus, NULL, buf); |
b8c5cec2 KS |
313 | if (!dev) |
314 | return -ENODEV; | |
0372ffb3 AW |
315 | if (bus_rescan_devices_helper(dev, NULL) == 0) |
316 | err = count; | |
317 | put_device(dev); | |
318 | return err; | |
b8c5cec2 | 319 | } |
151ef38f | 320 | |
4a3ad20c | 321 | static struct device *next_device(struct klist_iter *i) |
465c7a3a | 322 | { |
4a3ad20c | 323 | struct klist_node *n = klist_next(i); |
ae1b4171 GKH |
324 | struct device *dev = NULL; |
325 | struct device_private *dev_prv; | |
326 | ||
327 | if (n) { | |
328 | dev_prv = to_device_private_bus(n); | |
329 | dev = dev_prv->device; | |
330 | } | |
331 | return dev; | |
465c7a3a | 332 | } |
333 | ||
1da177e4 | 334 | /** |
4a3ad20c GKH |
335 | * bus_for_each_dev - device iterator. |
336 | * @bus: bus type. | |
337 | * @start: device to start iterating from. | |
338 | * @data: data for the callback. | |
339 | * @fn: function to be called for each device. | |
1da177e4 | 340 | * |
4a3ad20c GKH |
341 | * Iterate over @bus's list of devices, and call @fn for each, |
342 | * passing it @data. If @start is not NULL, we use that device to | |
343 | * begin iterating from. | |
1da177e4 | 344 | * |
4a3ad20c GKH |
345 | * We check the return of @fn each time. If it returns anything |
346 | * other than 0, we break out and return that value. | |
1da177e4 | 347 | * |
4a3ad20c GKH |
348 | * NOTE: The device that returns a non-zero value is not retained |
349 | * in any way, nor is its refcount incremented. If the caller needs | |
0fa1b0a1 | 350 | * to retain this data, it should do so, and increment the reference |
4a3ad20c | 351 | * count in the supplied callback. |
1da177e4 | 352 | */ |
e0766ea4 | 353 | int bus_for_each_dev(const struct bus_type *bus, struct device *start, |
4a3ad20c | 354 | void *data, int (*fn)(struct device *, void *)) |
1da177e4 | 355 | { |
83b9148d | 356 | struct subsys_private *sp = bus_to_subsys(bus); |
465c7a3a | 357 | struct klist_iter i; |
4a3ad20c | 358 | struct device *dev; |
465c7a3a | 359 | int error = 0; |
1da177e4 | 360 | |
83b9148d | 361 | if (!sp) |
465c7a3a | 362 | return -EINVAL; |
363 | ||
83b9148d | 364 | klist_iter_init_node(&sp->klist_devices, &i, |
7cd9c9bb | 365 | (start ? &start->p->knode_bus : NULL)); |
93ead7c9 | 366 | while (!error && (dev = next_device(&i))) |
7cd9c9bb GKH |
367 | error = fn(dev, data); |
368 | klist_iter_exit(&i); | |
83b9148d | 369 | subsys_put(sp); |
465c7a3a | 370 | return error; |
1da177e4 | 371 | } |
4a3ad20c | 372 | EXPORT_SYMBOL_GPL(bus_for_each_dev); |
1da177e4 | 373 | |
0edb5860 CH |
374 | /** |
375 | * bus_find_device - device iterator for locating a particular device. | |
376 | * @bus: bus type | |
377 | * @start: Device to begin with | |
378 | * @data: Data to pass to match function | |
379 | * @match: Callback function to check device | |
380 | * | |
381 | * This is similar to the bus_for_each_dev() function above, but it | |
382 | * returns a reference to a device that is 'found' for later use, as | |
383 | * determined by the @match callback. | |
384 | * | |
385 | * The callback should return 0 if the device doesn't match and non-zero | |
386 | * if it does. If the callback returns non-zero, this function will | |
387 | * return to the caller and not iterate over any more devices. | |
388 | */ | |
e0766ea4 | 389 | struct device *bus_find_device(const struct bus_type *bus, |
418e3ea1 SP |
390 | struct device *start, const void *data, |
391 | int (*match)(struct device *dev, const void *data)) | |
0edb5860 | 392 | { |
83b9148d | 393 | struct subsys_private *sp = bus_to_subsys(bus); |
0edb5860 CH |
394 | struct klist_iter i; |
395 | struct device *dev; | |
396 | ||
83b9148d | 397 | if (!sp) |
0edb5860 CH |
398 | return NULL; |
399 | ||
83b9148d | 400 | klist_iter_init_node(&sp->klist_devices, &i, |
7cd9c9bb | 401 | (start ? &start->p->knode_bus : NULL)); |
0edb5860 CH |
402 | while ((dev = next_device(&i))) |
403 | if (match(dev, data) && get_device(dev)) | |
404 | break; | |
405 | klist_iter_exit(&i); | |
83b9148d | 406 | subsys_put(sp); |
0edb5860 CH |
407 | return dev; |
408 | } | |
4a3ad20c | 409 | EXPORT_SYMBOL_GPL(bus_find_device); |
38fdac3c | 410 | |
4a3ad20c | 411 | static struct device_driver *next_driver(struct klist_iter *i) |
38fdac3c | 412 | { |
4a3ad20c | 413 | struct klist_node *n = klist_next(i); |
e5dd1278 GKH |
414 | struct driver_private *drv_priv; |
415 | ||
416 | if (n) { | |
417 | drv_priv = container_of(n, struct driver_private, knode_bus); | |
418 | return drv_priv->driver; | |
419 | } | |
420 | return NULL; | |
38fdac3c | 421 | } |
422 | ||
1da177e4 | 423 | /** |
4a3ad20c GKH |
424 | * bus_for_each_drv - driver iterator |
425 | * @bus: bus we're dealing with. | |
426 | * @start: driver to start iterating on. | |
427 | * @data: data to pass to the callback. | |
428 | * @fn: function to call for each driver. | |
1da177e4 | 429 | * |
4a3ad20c GKH |
430 | * This is nearly identical to the device iterator above. |
431 | * We iterate over each driver that belongs to @bus, and call | |
432 | * @fn for each. If @fn returns anything but 0, we break out | |
433 | * and return it. If @start is not NULL, we use it as the head | |
434 | * of the list. | |
1da177e4 | 435 | * |
4a3ad20c GKH |
436 | * NOTE: we don't return the driver that returns a non-zero |
437 | * value, nor do we leave the reference count incremented for that | |
438 | * driver. If the caller needs to know that info, it must set it | |
439 | * in the callback. It must also be sure to increment the refcount | |
440 | * so it doesn't disappear before returning to the caller. | |
1da177e4 | 441 | */ |
e0766ea4 | 442 | int bus_for_each_drv(const struct bus_type *bus, struct device_driver *start, |
4a3ad20c | 443 | void *data, int (*fn)(struct device_driver *, void *)) |
1da177e4 | 444 | { |
83b9148d | 445 | struct subsys_private *sp = bus_to_subsys(bus); |
38fdac3c | 446 | struct klist_iter i; |
4a3ad20c | 447 | struct device_driver *drv; |
38fdac3c | 448 | int error = 0; |
1da177e4 | 449 | |
83b9148d | 450 | if (!sp) |
38fdac3c | 451 | return -EINVAL; |
452 | ||
83b9148d | 453 | klist_iter_init_node(&sp->klist_drivers, &i, |
7cd9c9bb GKH |
454 | start ? &start->p->knode_bus : NULL); |
455 | while ((drv = next_driver(&i)) && !error) | |
456 | error = fn(drv, data); | |
457 | klist_iter_exit(&i); | |
83b9148d | 458 | subsys_put(sp); |
38fdac3c | 459 | return error; |
1da177e4 | 460 | } |
4a3ad20c | 461 | EXPORT_SYMBOL_GPL(bus_for_each_drv); |
1da177e4 | 462 | |
1da177e4 | 463 | /** |
4a3ad20c GKH |
464 | * bus_add_device - add device to bus |
465 | * @dev: device being added | |
1da177e4 | 466 | * |
2023c610 AS |
467 | * - Add device's bus attributes. |
468 | * - Create links to device's bus. | |
4a3ad20c | 469 | * - Add the device to its bus's list of devices. |
1da177e4 | 470 | */ |
4a3ad20c | 471 | int bus_add_device(struct device *dev) |
1da177e4 | 472 | { |
5221b82d GKH |
473 | struct subsys_private *sp = bus_to_subsys(dev->bus); |
474 | int error; | |
1da177e4 | 475 | |
5221b82d GKH |
476 | if (!sp) { |
477 | /* | |
478 | * This is a normal operation for many devices that do not | |
479 | * have a bus assigned to them, just say that all went | |
480 | * well. | |
481 | */ | |
482 | return 0; | |
1da177e4 | 483 | } |
5221b82d GKH |
484 | |
485 | /* | |
486 | * Reference in sp is now incremented and will be dropped when | |
487 | * the device is removed from the bus | |
488 | */ | |
489 | ||
490 | pr_debug("bus: '%s': add device %s\n", sp->bus->name, dev_name(dev)); | |
491 | ||
492 | error = device_add_groups(dev, sp->bus->dev_groups); | |
493 | if (error) | |
494 | goto out_put; | |
495 | ||
496 | error = sysfs_create_link(&sp->devices_kset->kobj, &dev->kobj, dev_name(dev)); | |
497 | if (error) | |
498 | goto out_groups; | |
499 | ||
500 | error = sysfs_create_link(&dev->kobj, &sp->subsys.kobj, "subsystem"); | |
501 | if (error) | |
502 | goto out_subsys; | |
503 | ||
504 | klist_add_tail(&dev->p->knode_bus, &sp->klist_devices); | |
513e7337 CH |
505 | return 0; |
506 | ||
513e7337 | 507 | out_subsys: |
5221b82d | 508 | sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev)); |
fa6fdb33 | 509 | out_groups: |
5221b82d | 510 | device_remove_groups(dev, sp->bus->dev_groups); |
513e7337 | 511 | out_put: |
5221b82d | 512 | subsys_put(sp); |
1da177e4 LT |
513 | return error; |
514 | } | |
515 | ||
53877d06 | 516 | /** |
2023c610 AS |
517 | * bus_probe_device - probe drivers for a new device |
518 | * @dev: device to probe | |
53877d06 | 519 | * |
2023c610 | 520 | * - Automatically probe for a driver if the bus allows it. |
53877d06 | 521 | */ |
2023c610 | 522 | void bus_probe_device(struct device *dev) |
53877d06 | 523 | { |
5221b82d | 524 | struct subsys_private *sp = bus_to_subsys(dev->bus); |
ca22e56d | 525 | struct subsys_interface *sif; |
53877d06 | 526 | |
5221b82d | 527 | if (!sp) |
ca22e56d KS |
528 | return; |
529 | ||
5221b82d | 530 | if (sp->drivers_autoprobe) |
765230b5 | 531 | device_initial_probe(dev); |
ca22e56d | 532 | |
5221b82d GKH |
533 | mutex_lock(&sp->mutex); |
534 | list_for_each_entry(sif, &sp->interfaces, node) | |
ca22e56d KS |
535 | if (sif->add_dev) |
536 | sif->add_dev(dev, sif); | |
5221b82d GKH |
537 | mutex_unlock(&sp->mutex); |
538 | subsys_put(sp); | |
53877d06 KS |
539 | } |
540 | ||
1da177e4 | 541 | /** |
4a3ad20c GKH |
542 | * bus_remove_device - remove device from bus |
543 | * @dev: device to be removed | |
1da177e4 | 544 | * |
ca22e56d KS |
545 | * - Remove device from all interfaces. |
546 | * - Remove symlink from bus' directory. | |
4a3ad20c GKH |
547 | * - Delete device from bus's list. |
548 | * - Detach from its driver. | |
549 | * - Drop reference taken in bus_add_device(). | |
1da177e4 | 550 | */ |
4a3ad20c | 551 | void bus_remove_device(struct device *dev) |
1da177e4 | 552 | { |
5221b82d | 553 | struct subsys_private *sp = bus_to_subsys(dev->bus); |
ca22e56d KS |
554 | struct subsys_interface *sif; |
555 | ||
5221b82d | 556 | if (!sp) |
ca22e56d KS |
557 | return; |
558 | ||
5221b82d GKH |
559 | mutex_lock(&sp->mutex); |
560 | list_for_each_entry(sif, &sp->interfaces, node) | |
ca22e56d KS |
561 | if (sif->remove_dev) |
562 | sif->remove_dev(dev, sif); | |
5221b82d | 563 | mutex_unlock(&sp->mutex); |
ca22e56d KS |
564 | |
565 | sysfs_remove_link(&dev->kobj, "subsystem"); | |
5221b82d | 566 | sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev)); |
fa6fdb33 | 567 | device_remove_groups(dev, dev->bus->dev_groups); |
ca22e56d KS |
568 | if (klist_node_attached(&dev->p->knode_bus)) |
569 | klist_del(&dev->p->knode_bus); | |
570 | ||
571 | pr_debug("bus: '%s': remove device %s\n", | |
572 | dev->bus->name, dev_name(dev)); | |
573 | device_release_driver(dev); | |
5221b82d GKH |
574 | |
575 | /* | |
576 | * Decrement the reference count twice, once for the bus_to_subsys() | |
577 | * call in the start of this function, and the second one from the | |
578 | * reference increment in bus_add_device() | |
579 | */ | |
580 | subsys_put(sp); | |
581 | subsys_put(sp); | |
1da177e4 LT |
582 | } |
583 | ||
f86db396 | 584 | static int __must_check add_bind_files(struct device_driver *drv) |
874c6241 | 585 | { |
f86db396 AM |
586 | int ret; |
587 | ||
588 | ret = driver_create_file(drv, &driver_attr_unbind); | |
589 | if (ret == 0) { | |
590 | ret = driver_create_file(drv, &driver_attr_bind); | |
591 | if (ret) | |
592 | driver_remove_file(drv, &driver_attr_unbind); | |
593 | } | |
594 | return ret; | |
874c6241 GKH |
595 | } |
596 | ||
597 | static void remove_bind_files(struct device_driver *drv) | |
598 | { | |
599 | driver_remove_file(drv, &driver_attr_bind); | |
600 | driver_remove_file(drv, &driver_attr_unbind); | |
601 | } | |
b8c5cec2 | 602 | |
2e7189b6 GKH |
603 | static BUS_ATTR_WO(drivers_probe); |
604 | static BUS_ATTR_RW(drivers_autoprobe); | |
8380770c | 605 | |
b8c5cec2 KS |
606 | static int add_probe_files(struct bus_type *bus) |
607 | { | |
608 | int retval; | |
609 | ||
8380770c | 610 | retval = bus_create_file(bus, &bus_attr_drivers_probe); |
b8c5cec2 KS |
611 | if (retval) |
612 | goto out; | |
613 | ||
8380770c | 614 | retval = bus_create_file(bus, &bus_attr_drivers_autoprobe); |
b8c5cec2 | 615 | if (retval) |
8380770c | 616 | bus_remove_file(bus, &bus_attr_drivers_probe); |
b8c5cec2 KS |
617 | out: |
618 | return retval; | |
619 | } | |
620 | ||
621 | static void remove_probe_files(struct bus_type *bus) | |
622 | { | |
8380770c KS |
623 | bus_remove_file(bus, &bus_attr_drivers_autoprobe); |
624 | bus_remove_file(bus, &bus_attr_drivers_probe); | |
b8c5cec2 | 625 | } |
1da177e4 | 626 | |
2581c9cc GKH |
627 | static ssize_t uevent_store(struct device_driver *drv, const char *buf, |
628 | size_t count) | |
7ac1cf4a | 629 | { |
df44b479 PR |
630 | int rc; |
631 | ||
632 | rc = kobject_synth_uevent(&drv->p->kobj, buf, count); | |
633 | return rc ? rc : count; | |
7ac1cf4a | 634 | } |
2581c9cc | 635 | static DRIVER_ATTR_WO(uevent); |
7ac1cf4a | 636 | |
1da177e4 | 637 | /** |
4a3ad20c GKH |
638 | * bus_add_driver - Add a driver to the bus. |
639 | * @drv: driver. | |
1da177e4 | 640 | */ |
f86db396 | 641 | int bus_add_driver(struct device_driver *drv) |
1da177e4 | 642 | { |
e4f05682 | 643 | struct subsys_private *sp = bus_to_subsys(drv->bus); |
e5dd1278 | 644 | struct driver_private *priv; |
1da177e4 LT |
645 | int error = 0; |
646 | ||
e4f05682 | 647 | if (!sp) |
4f6e1945 | 648 | return -EINVAL; |
d9fd4d3b | 649 | |
e4f05682 GKH |
650 | /* |
651 | * Reference in sp is now incremented and will be dropped when | |
652 | * the driver is removed from the bus | |
653 | */ | |
654 | pr_debug("bus: '%s': add driver %s\n", sp->bus->name, drv->name); | |
e5dd1278 GKH |
655 | |
656 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); | |
07634464 CH |
657 | if (!priv) { |
658 | error = -ENOMEM; | |
659 | goto out_put_bus; | |
660 | } | |
e5dd1278 GKH |
661 | klist_init(&priv->klist_devices, NULL, NULL); |
662 | priv->driver = drv; | |
663 | drv->p = priv; | |
e4f05682 | 664 | priv->kobj.kset = sp->drivers_kset; |
c8e90d82 GKH |
665 | error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL, |
666 | "%s", drv->name); | |
dc0afa83 | 667 | if (error) |
07634464 | 668 | goto out_unregister; |
d9fd4d3b | 669 | |
e4f05682 GKH |
670 | klist_add_tail(&priv->knode_bus, &sp->klist_drivers); |
671 | if (sp->drivers_autoprobe) { | |
ef0ff683 AD |
672 | error = driver_attach(drv); |
673 | if (error) | |
310862e5 | 674 | goto out_del_list; |
b8c5cec2 | 675 | } |
d9fd4d3b JG |
676 | module_add_driver(drv->owner, drv); |
677 | ||
7ac1cf4a KS |
678 | error = driver_create_file(drv, &driver_attr_uevent); |
679 | if (error) { | |
680 | printk(KERN_ERR "%s: uevent attr (%s) failed\n", | |
2b3a302a | 681 | __func__, drv->name); |
7ac1cf4a | 682 | } |
e4f05682 | 683 | error = driver_add_groups(drv, sp->bus->drv_groups); |
d9fd4d3b JG |
684 | if (error) { |
685 | /* How the hell do we get out of this pickle? Give up */ | |
4044b2fc | 686 | printk(KERN_ERR "%s: driver_add_groups(%s) failed\n", |
ed0617b5 | 687 | __func__, drv->name); |
e18945b1 | 688 | } |
1a6f2a75 DT |
689 | |
690 | if (!drv->suppress_bind_attrs) { | |
691 | error = add_bind_files(drv); | |
692 | if (error) { | |
693 | /* Ditto */ | |
694 | printk(KERN_ERR "%s: add_bind_files(%s) failed\n", | |
695 | __func__, drv->name); | |
696 | } | |
1da177e4 | 697 | } |
d9fd4d3b | 698 | |
5c8563d7 | 699 | return 0; |
1a6f2a75 | 700 | |
310862e5 SS |
701 | out_del_list: |
702 | klist_del(&priv->knode_bus); | |
f86db396 | 703 | out_unregister: |
99b28f1b | 704 | kobject_put(&priv->kobj); |
0f9b011d | 705 | /* drv->p is freed in driver_release() */ |
5c8563d7 | 706 | drv->p = NULL; |
f86db396 | 707 | out_put_bus: |
e4f05682 | 708 | subsys_put(sp); |
f86db396 | 709 | return error; |
1da177e4 LT |
710 | } |
711 | ||
1da177e4 | 712 | /** |
4a3ad20c GKH |
713 | * bus_remove_driver - delete driver from bus's knowledge. |
714 | * @drv: driver. | |
1da177e4 | 715 | * |
4a3ad20c GKH |
716 | * Detach the driver from the devices it controls, and remove |
717 | * it from its bus's list of drivers. Finally, we drop the reference | |
718 | * to the bus we took in bus_add_driver(). | |
1da177e4 | 719 | */ |
4a3ad20c | 720 | void bus_remove_driver(struct device_driver *drv) |
1da177e4 | 721 | { |
e4f05682 GKH |
722 | struct subsys_private *sp = bus_to_subsys(drv->bus); |
723 | ||
724 | if (!sp) | |
d9fd4d3b JG |
725 | return; |
726 | ||
e4f05682 GKH |
727 | pr_debug("bus: '%s': remove driver %s\n", sp->bus->name, drv->name); |
728 | ||
1a6f2a75 DT |
729 | if (!drv->suppress_bind_attrs) |
730 | remove_bind_files(drv); | |
e4f05682 | 731 | driver_remove_groups(drv, sp->bus->drv_groups); |
7ac1cf4a | 732 | driver_remove_file(drv, &driver_attr_uevent); |
e5dd1278 | 733 | klist_remove(&drv->p->knode_bus); |
d9fd4d3b JG |
734 | driver_detach(drv); |
735 | module_remove_driver(drv); | |
c10997f6 | 736 | kobject_put(&drv->p->kobj); |
e4f05682 GKH |
737 | |
738 | /* | |
739 | * Decrement the reference count twice, once for the bus_to_subsys() | |
740 | * call in the start of this function, and the second one from the | |
741 | * reference increment in bus_add_driver() | |
742 | */ | |
743 | subsys_put(sp); | |
744 | subsys_put(sp); | |
1da177e4 LT |
745 | } |
746 | ||
1da177e4 | 747 | /* Helper for bus_rescan_devices's iter */ |
f86db396 | 748 | static int __must_check bus_rescan_devices_helper(struct device *dev, |
4a3ad20c | 749 | void *data) |
1da177e4 | 750 | { |
f86db396 AM |
751 | int ret = 0; |
752 | ||
bf74ad5b | 753 | if (!dev->driver) { |
8c97a46a | 754 | if (dev->parent && dev->bus->need_parent_lock) |
8e9394ce | 755 | device_lock(dev->parent); |
f86db396 | 756 | ret = device_attach(dev); |
8c97a46a | 757 | if (dev->parent && dev->bus->need_parent_lock) |
8e9394ce | 758 | device_unlock(dev->parent); |
bf74ad5b | 759 | } |
f86db396 | 760 | return ret < 0 ? ret : 0; |
1da177e4 LT |
761 | } |
762 | ||
1da177e4 | 763 | /** |
23d3d602 GKH |
764 | * bus_rescan_devices - rescan devices on the bus for possible drivers |
765 | * @bus: the bus to scan. | |
1da177e4 | 766 | * |
23d3d602 GKH |
767 | * This function will look for devices on the bus with no driver |
768 | * attached and rescan it against existing drivers to see if it matches | |
769 | * any by calling device_attach() for the unbound devices. | |
1da177e4 | 770 | */ |
4a3ad20c | 771 | int bus_rescan_devices(struct bus_type *bus) |
1da177e4 | 772 | { |
f86db396 | 773 | return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper); |
1da177e4 | 774 | } |
4a3ad20c | 775 | EXPORT_SYMBOL_GPL(bus_rescan_devices); |
1da177e4 | 776 | |
e935d5da ME |
777 | /** |
778 | * device_reprobe - remove driver for a device and probe for a new driver | |
779 | * @dev: the device to reprobe | |
780 | * | |
781 | * This function detaches the attached driver (if any) for the given | |
782 | * device and restarts the driver probing process. It is intended | |
783 | * to use if probing criteria changed during a devices lifetime and | |
784 | * driver attachment should change accordingly. | |
785 | */ | |
f86db396 | 786 | int device_reprobe(struct device *dev) |
e935d5da | 787 | { |
ed88747c AD |
788 | if (dev->driver) |
789 | device_driver_detach(dev); | |
f86db396 | 790 | return bus_rescan_devices_helper(dev, NULL); |
e935d5da ME |
791 | } |
792 | EXPORT_SYMBOL_GPL(device_reprobe); | |
1da177e4 | 793 | |
34bb61f9 JB |
794 | static void klist_devices_get(struct klist_node *n) |
795 | { | |
ae1b4171 GKH |
796 | struct device_private *dev_prv = to_device_private_bus(n); |
797 | struct device *dev = dev_prv->device; | |
34bb61f9 JB |
798 | |
799 | get_device(dev); | |
800 | } | |
801 | ||
802 | static void klist_devices_put(struct klist_node *n) | |
803 | { | |
ae1b4171 GKH |
804 | struct device_private *dev_prv = to_device_private_bus(n); |
805 | struct device *dev = dev_prv->device; | |
34bb61f9 JB |
806 | |
807 | put_device(dev); | |
808 | } | |
809 | ||
7ac1cf4a KS |
810 | static ssize_t bus_uevent_store(struct bus_type *bus, |
811 | const char *buf, size_t count) | |
812 | { | |
a00fdb98 GKH |
813 | struct subsys_private *sp = bus_to_subsys(bus); |
814 | int ret; | |
df44b479 | 815 | |
a00fdb98 GKH |
816 | if (!sp) |
817 | return -EINVAL; | |
818 | ||
819 | ret = kobject_synth_uevent(&sp->subsys.kobj, buf, count); | |
820 | subsys_put(sp); | |
821 | ||
822 | if (ret) | |
823 | return ret; | |
824 | return count; | |
7ac1cf4a | 825 | } |
a4723041 GKH |
826 | /* |
827 | * "open code" the old BUS_ATTR() macro here. We want to use BUS_ATTR_WO() | |
828 | * here, but can not use it as earlier in the file we have | |
829 | * DEVICE_ATTR_WO(uevent), which would cause a clash with the with the store | |
830 | * function name. | |
831 | */ | |
16b0dd40 | 832 | static struct bus_attribute bus_attr_uevent = __ATTR(uevent, 0200, NULL, |
a4723041 | 833 | bus_uevent_store); |
7ac1cf4a | 834 | |
1da177e4 | 835 | /** |
be871b7e | 836 | * bus_register - register a driver-core subsystem |
78d79559 | 837 | * @bus: bus to register |
1da177e4 | 838 | * |
78d79559 | 839 | * Once we have that, we register the bus with the kobject |
4a3ad20c | 840 | * infrastructure, then register the children subsystems it has: |
ca22e56d | 841 | * the devices and drivers that belong to the subsystem. |
1da177e4 | 842 | */ |
be871b7e | 843 | int bus_register(struct bus_type *bus) |
1da177e4 LT |
844 | { |
845 | int retval; | |
6b6e39a6 | 846 | struct subsys_private *priv; |
3465e2e4 | 847 | struct kobject *bus_kobj; |
37e98d9b | 848 | struct lock_class_key *key; |
c6f7e72a | 849 | |
6b6e39a6 | 850 | priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL); |
c6f7e72a GKH |
851 | if (!priv) |
852 | return -ENOMEM; | |
853 | ||
854 | priv->bus = bus; | |
1da177e4 | 855 | |
c6f7e72a | 856 | BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier); |
116af378 | 857 | |
3465e2e4 GKH |
858 | bus_kobj = &priv->subsys.kobj; |
859 | retval = kobject_set_name(bus_kobj, "%s", bus->name); | |
1da177e4 LT |
860 | if (retval) |
861 | goto out; | |
862 | ||
3465e2e4 GKH |
863 | bus_kobj->kset = bus_kset; |
864 | bus_kobj->ktype = &bus_ktype; | |
c6f7e72a | 865 | priv->drivers_autoprobe = 1; |
d6b05b84 | 866 | |
c6f7e72a | 867 | retval = kset_register(&priv->subsys); |
1da177e4 LT |
868 | if (retval) |
869 | goto out; | |
870 | ||
7ac1cf4a KS |
871 | retval = bus_create_file(bus, &bus_attr_uevent); |
872 | if (retval) | |
873 | goto bus_uevent_fail; | |
874 | ||
3465e2e4 | 875 | priv->devices_kset = kset_create_and_add("devices", NULL, bus_kobj); |
c6f7e72a | 876 | if (!priv->devices_kset) { |
3d899596 | 877 | retval = -ENOMEM; |
1da177e4 | 878 | goto bus_devices_fail; |
3d899596 | 879 | } |
1da177e4 | 880 | |
3465e2e4 | 881 | priv->drivers_kset = kset_create_and_add("drivers", NULL, bus_kobj); |
c6f7e72a | 882 | if (!priv->drivers_kset) { |
6dcec251 | 883 | retval = -ENOMEM; |
1da177e4 | 884 | goto bus_drivers_fail; |
6dcec251 | 885 | } |
465c7a3a | 886 | |
ca22e56d | 887 | INIT_LIST_HEAD(&priv->interfaces); |
37e98d9b GKH |
888 | key = &priv->lock_key; |
889 | lockdep_register_key(key); | |
ca22e56d | 890 | __mutex_init(&priv->mutex, "subsys mutex", key); |
c6f7e72a GKH |
891 | klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put); |
892 | klist_init(&priv->klist_drivers, NULL, NULL); | |
b8c5cec2 | 893 | |
b8c5cec2 KS |
894 | retval = add_probe_files(bus); |
895 | if (retval) | |
896 | goto bus_probe_files_fail; | |
897 | ||
3465e2e4 | 898 | retval = sysfs_create_groups(bus_kobj, bus->bus_groups); |
12478ba0 GKH |
899 | if (retval) |
900 | goto bus_groups_fail; | |
1da177e4 | 901 | |
7dc72b28 | 902 | pr_debug("bus: '%s': registered\n", bus->name); |
1da177e4 LT |
903 | return 0; |
904 | ||
12478ba0 | 905 | bus_groups_fail: |
b8c5cec2 KS |
906 | remove_probe_files(bus); |
907 | bus_probe_files_fail: | |
3465e2e4 | 908 | kset_unregister(priv->drivers_kset); |
1da177e4 | 909 | bus_drivers_fail: |
3465e2e4 | 910 | kset_unregister(priv->devices_kset); |
1da177e4 | 911 | bus_devices_fail: |
7ac1cf4a KS |
912 | bus_remove_file(bus, &bus_attr_uevent); |
913 | bus_uevent_fail: | |
3465e2e4 | 914 | kset_unregister(&priv->subsys); |
1da177e4 | 915 | out: |
3465e2e4 | 916 | kfree(priv); |
1da177e4 LT |
917 | return retval; |
918 | } | |
be871b7e | 919 | EXPORT_SYMBOL_GPL(bus_register); |
1da177e4 | 920 | |
1da177e4 | 921 | /** |
4a3ad20c GKH |
922 | * bus_unregister - remove a bus from the system |
923 | * @bus: bus. | |
1da177e4 | 924 | * |
4a3ad20c GKH |
925 | * Unregister the child subsystems and the bus itself. |
926 | * Finally, we call bus_put() to release the refcount | |
1da177e4 | 927 | */ |
4a3ad20c | 928 | void bus_unregister(struct bus_type *bus) |
1da177e4 | 929 | { |
3465e2e4 GKH |
930 | struct subsys_private *sp = bus_to_subsys(bus); |
931 | struct kobject *bus_kobj; | |
932 | ||
933 | if (!sp) | |
934 | return; | |
935 | ||
7dc72b28 | 936 | pr_debug("bus: '%s': unregistering\n", bus->name); |
ca22e56d KS |
937 | if (bus->dev_root) |
938 | device_unregister(bus->dev_root); | |
3465e2e4 GKH |
939 | |
940 | bus_kobj = &sp->subsys.kobj; | |
941 | sysfs_remove_groups(bus_kobj, bus->bus_groups); | |
b8c5cec2 | 942 | remove_probe_files(bus); |
7ac1cf4a | 943 | bus_remove_file(bus, &bus_attr_uevent); |
3465e2e4 GKH |
944 | |
945 | kset_unregister(sp->drivers_kset); | |
946 | kset_unregister(sp->devices_kset); | |
947 | kset_unregister(&sp->subsys); | |
948 | subsys_put(sp); | |
1da177e4 | 949 | } |
4a3ad20c | 950 | EXPORT_SYMBOL_GPL(bus_unregister); |
1da177e4 | 951 | |
bc8b7931 | 952 | int bus_register_notifier(const struct bus_type *bus, struct notifier_block *nb) |
116af378 | 953 | { |
32a8121a GKH |
954 | struct subsys_private *sp = bus_to_subsys(bus); |
955 | int retval; | |
956 | ||
957 | if (!sp) | |
958 | return -EINVAL; | |
959 | ||
960 | retval = blocking_notifier_chain_register(&sp->bus_notifier, nb); | |
961 | subsys_put(sp); | |
962 | return retval; | |
116af378 BH |
963 | } |
964 | EXPORT_SYMBOL_GPL(bus_register_notifier); | |
965 | ||
bc8b7931 | 966 | int bus_unregister_notifier(const struct bus_type *bus, struct notifier_block *nb) |
116af378 | 967 | { |
32a8121a GKH |
968 | struct subsys_private *sp = bus_to_subsys(bus); |
969 | int retval; | |
970 | ||
971 | if (!sp) | |
972 | return -EINVAL; | |
973 | retval = blocking_notifier_chain_unregister(&sp->bus_notifier, nb); | |
974 | subsys_put(sp); | |
975 | return retval; | |
116af378 BH |
976 | } |
977 | EXPORT_SYMBOL_GPL(bus_unregister_notifier); | |
978 | ||
ed9f9181 GKH |
979 | void bus_notify(struct device *dev, enum bus_notifier_event value) |
980 | { | |
32a8121a | 981 | struct subsys_private *sp = bus_to_subsys(dev->bus); |
ed9f9181 | 982 | |
32a8121a GKH |
983 | if (!sp) |
984 | return; | |
985 | ||
986 | blocking_notifier_call_chain(&sp->bus_notifier, value, dev); | |
987 | subsys_put(sp); | |
ed9f9181 GKH |
988 | } |
989 | ||
f91482be | 990 | struct kset *bus_get_kset(const struct bus_type *bus) |
0fed80f7 | 991 | { |
beea7892 GKH |
992 | struct subsys_private *sp = bus_to_subsys(bus); |
993 | struct kset *kset; | |
994 | ||
995 | if (!sp) | |
996 | return NULL; | |
997 | ||
998 | kset = &sp->subsys; | |
999 | subsys_put(sp); | |
1000 | ||
1001 | return kset; | |
0fed80f7 GKH |
1002 | } |
1003 | EXPORT_SYMBOL_GPL(bus_get_kset); | |
1004 | ||
99178b03 | 1005 | /* |
dca25ebd | 1006 | * Yes, this forcibly breaks the klist abstraction temporarily. It |
99178b03 GKH |
1007 | * just wants to sort the klist, not change reference counts and |
1008 | * take/drop locks rapidly in the process. It does all this while | |
1009 | * holding the lock for the list, so objects can't otherwise be | |
1010 | * added/removed while we're swizzling. | |
1011 | */ | |
1012 | static void device_insertion_sort_klist(struct device *a, struct list_head *list, | |
1013 | int (*compare)(const struct device *a, | |
1014 | const struct device *b)) | |
1015 | { | |
99178b03 | 1016 | struct klist_node *n; |
ae1b4171 | 1017 | struct device_private *dev_prv; |
99178b03 GKH |
1018 | struct device *b; |
1019 | ||
4c62785e | 1020 | list_for_each_entry(n, list, n_node) { |
ae1b4171 GKH |
1021 | dev_prv = to_device_private_bus(n); |
1022 | b = dev_prv->device; | |
99178b03 | 1023 | if (compare(a, b) <= 0) { |
ae1b4171 GKH |
1024 | list_move_tail(&a->p->knode_bus.n_node, |
1025 | &b->p->knode_bus.n_node); | |
99178b03 GKH |
1026 | return; |
1027 | } | |
1028 | } | |
ae1b4171 | 1029 | list_move_tail(&a->p->knode_bus.n_node, list); |
99178b03 GKH |
1030 | } |
1031 | ||
1032 | void bus_sort_breadthfirst(struct bus_type *bus, | |
1033 | int (*compare)(const struct device *a, | |
1034 | const struct device *b)) | |
1035 | { | |
b5aaecb8 | 1036 | struct subsys_private *sp = bus_to_subsys(bus); |
99178b03 | 1037 | LIST_HEAD(sorted_devices); |
4c62785e | 1038 | struct klist_node *n, *tmp; |
ae1b4171 | 1039 | struct device_private *dev_prv; |
99178b03 GKH |
1040 | struct device *dev; |
1041 | struct klist *device_klist; | |
1042 | ||
b5aaecb8 GKH |
1043 | if (!sp) |
1044 | return; | |
1045 | device_klist = &sp->klist_devices; | |
99178b03 GKH |
1046 | |
1047 | spin_lock(&device_klist->k_lock); | |
4c62785e | 1048 | list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) { |
ae1b4171 GKH |
1049 | dev_prv = to_device_private_bus(n); |
1050 | dev = dev_prv->device; | |
99178b03 GKH |
1051 | device_insertion_sort_klist(dev, &sorted_devices, compare); |
1052 | } | |
1053 | list_splice(&sorted_devices, &device_klist->k_list); | |
1054 | spin_unlock(&device_klist->k_lock); | |
b5aaecb8 | 1055 | subsys_put(sp); |
99178b03 GKH |
1056 | } |
1057 | EXPORT_SYMBOL_GPL(bus_sort_breadthfirst); | |
1058 | ||
b0a8a59a GKH |
1059 | struct subsys_dev_iter { |
1060 | struct klist_iter ki; | |
1061 | const struct device_type *type; | |
1062 | }; | |
1063 | ||
ca22e56d KS |
1064 | /** |
1065 | * subsys_dev_iter_init - initialize subsys device iterator | |
1066 | * @iter: subsys iterator to initialize | |
adac0375 | 1067 | * @sp: the subsys private (i.e. bus) we wanna iterate over |
ca22e56d KS |
1068 | * @start: the device to start iterating from, if any |
1069 | * @type: device_type of the devices to iterate over, NULL for all | |
1070 | * | |
1071 | * Initialize subsys iterator @iter such that it iterates over devices | |
1072 | * of @subsys. If @start is set, the list iteration will start there, | |
1073 | * otherwise if it is NULL, the iteration starts at the beginning of | |
1074 | * the list. | |
1075 | */ | |
adac0375 | 1076 | static void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct subsys_private *sp, |
2e45fc55 | 1077 | struct device *start, const struct device_type *type) |
ca22e56d KS |
1078 | { |
1079 | struct klist_node *start_knode = NULL; | |
1080 | ||
1081 | if (start) | |
1082 | start_knode = &start->p->knode_bus; | |
adac0375 | 1083 | klist_iter_init_node(&sp->klist_devices, &iter->ki, start_knode); |
7cd9c9bb | 1084 | iter->type = type; |
ca22e56d | 1085 | } |
ca22e56d KS |
1086 | |
1087 | /** | |
1088 | * subsys_dev_iter_next - iterate to the next device | |
1089 | * @iter: subsys iterator to proceed | |
1090 | * | |
1091 | * Proceed @iter to the next device and return it. Returns NULL if | |
1092 | * iteration is complete. | |
1093 | * | |
1094 | * The returned device is referenced and won't be released till | |
1095 | * iterator is proceed to the next device or exited. The caller is | |
1096 | * free to do whatever it wants to do with the device including | |
1097 | * calling back into subsys code. | |
1098 | */ | |
38cdadef | 1099 | static struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter) |
ca22e56d KS |
1100 | { |
1101 | struct klist_node *knode; | |
1102 | struct device *dev; | |
1103 | ||
1104 | for (;;) { | |
1105 | knode = klist_next(&iter->ki); | |
1106 | if (!knode) | |
1107 | return NULL; | |
371fd7a2 | 1108 | dev = to_device_private_bus(knode)->device; |
ca22e56d KS |
1109 | if (!iter->type || iter->type == dev->type) |
1110 | return dev; | |
1111 | } | |
1112 | } | |
ca22e56d KS |
1113 | |
1114 | /** | |
1115 | * subsys_dev_iter_exit - finish iteration | |
1116 | * @iter: subsys iterator to finish | |
1117 | * | |
1118 | * Finish an iteration. Always call this function after iteration is | |
1119 | * complete whether the iteration ran till the end or not. | |
1120 | */ | |
af6d0743 | 1121 | static void subsys_dev_iter_exit(struct subsys_dev_iter *iter) |
ca22e56d KS |
1122 | { |
1123 | klist_iter_exit(&iter->ki); | |
1124 | } | |
ca22e56d KS |
1125 | |
1126 | int subsys_interface_register(struct subsys_interface *sif) | |
1127 | { | |
adac0375 | 1128 | struct subsys_private *sp; |
ca22e56d KS |
1129 | struct subsys_dev_iter iter; |
1130 | struct device *dev; | |
1131 | ||
1132 | if (!sif || !sif->subsys) | |
1133 | return -ENODEV; | |
1134 | ||
adac0375 GKH |
1135 | sp = bus_to_subsys(sif->subsys); |
1136 | if (!sp) | |
ca22e56d KS |
1137 | return -EINVAL; |
1138 | ||
adac0375 GKH |
1139 | /* |
1140 | * Reference in sp is now incremented and will be dropped when | |
1141 | * the interface is removed from the bus | |
1142 | */ | |
1143 | ||
1144 | mutex_lock(&sp->mutex); | |
1145 | list_add_tail(&sif->node, &sp->interfaces); | |
ca22e56d | 1146 | if (sif->add_dev) { |
adac0375 | 1147 | subsys_dev_iter_init(&iter, sp, NULL, NULL); |
ca22e56d KS |
1148 | while ((dev = subsys_dev_iter_next(&iter))) |
1149 | sif->add_dev(dev, sif); | |
1150 | subsys_dev_iter_exit(&iter); | |
1151 | } | |
adac0375 | 1152 | mutex_unlock(&sp->mutex); |
ca22e56d KS |
1153 | |
1154 | return 0; | |
1155 | } | |
1156 | EXPORT_SYMBOL_GPL(subsys_interface_register); | |
1157 | ||
1158 | void subsys_interface_unregister(struct subsys_interface *sif) | |
1159 | { | |
adac0375 | 1160 | struct subsys_private *sp; |
ca22e56d KS |
1161 | struct subsys_dev_iter iter; |
1162 | struct device *dev; | |
1163 | ||
2b31594a | 1164 | if (!sif || !sif->subsys) |
ca22e56d KS |
1165 | return; |
1166 | ||
adac0375 GKH |
1167 | sp = bus_to_subsys(sif->subsys); |
1168 | if (!sp) | |
1169 | return; | |
2b31594a | 1170 | |
adac0375 | 1171 | mutex_lock(&sp->mutex); |
ca22e56d KS |
1172 | list_del_init(&sif->node); |
1173 | if (sif->remove_dev) { | |
adac0375 | 1174 | subsys_dev_iter_init(&iter, sp, NULL, NULL); |
ca22e56d KS |
1175 | while ((dev = subsys_dev_iter_next(&iter))) |
1176 | sif->remove_dev(dev, sif); | |
1177 | subsys_dev_iter_exit(&iter); | |
1178 | } | |
adac0375 | 1179 | mutex_unlock(&sp->mutex); |
ca22e56d | 1180 | |
adac0375 GKH |
1181 | /* |
1182 | * Decrement the reference count twice, once for the bus_to_subsys() | |
1183 | * call in the start of this function, and the second one from the | |
1184 | * reference increment in subsys_interface_register() | |
1185 | */ | |
1186 | subsys_put(sp); | |
1187 | subsys_put(sp); | |
ca22e56d KS |
1188 | } |
1189 | EXPORT_SYMBOL_GPL(subsys_interface_unregister); | |
1190 | ||
1191 | static void system_root_device_release(struct device *dev) | |
1192 | { | |
1193 | kfree(dev); | |
1194 | } | |
d73ce004 TH |
1195 | |
1196 | static int subsys_register(struct bus_type *subsys, | |
1197 | const struct attribute_group **groups, | |
1198 | struct kobject *parent_of_root) | |
ca22e56d KS |
1199 | { |
1200 | struct device *dev; | |
1201 | int err; | |
1202 | ||
1203 | err = bus_register(subsys); | |
1204 | if (err < 0) | |
1205 | return err; | |
1206 | ||
1207 | dev = kzalloc(sizeof(struct device), GFP_KERNEL); | |
1208 | if (!dev) { | |
1209 | err = -ENOMEM; | |
1210 | goto err_dev; | |
1211 | } | |
1212 | ||
1213 | err = dev_set_name(dev, "%s", subsys->name); | |
1214 | if (err < 0) | |
1215 | goto err_name; | |
1216 | ||
d73ce004 | 1217 | dev->kobj.parent = parent_of_root; |
ca22e56d KS |
1218 | dev->groups = groups; |
1219 | dev->release = system_root_device_release; | |
1220 | ||
1221 | err = device_register(dev); | |
1222 | if (err < 0) | |
1223 | goto err_dev_reg; | |
1224 | ||
1225 | subsys->dev_root = dev; | |
1226 | return 0; | |
1227 | ||
1228 | err_dev_reg: | |
1229 | put_device(dev); | |
1230 | dev = NULL; | |
1231 | err_name: | |
1232 | kfree(dev); | |
1233 | err_dev: | |
1234 | bus_unregister(subsys); | |
1235 | return err; | |
1236 | } | |
d73ce004 TH |
1237 | |
1238 | /** | |
1239 | * subsys_system_register - register a subsystem at /sys/devices/system/ | |
1240 | * @subsys: system subsystem | |
1241 | * @groups: default attributes for the root device | |
1242 | * | |
1243 | * All 'system' subsystems have a /sys/devices/system/<name> root device | |
1244 | * with the name of the subsystem. The root device can carry subsystem- | |
1245 | * wide attributes. All registered devices are below this single root | |
1246 | * device and are named after the subsystem with a simple enumeration | |
e227867f | 1247 | * number appended. The registered devices are not explicitly named; |
d73ce004 TH |
1248 | * only 'id' in the device needs to be set. |
1249 | * | |
1250 | * Do not use this interface for anything new, it exists for compatibility | |
1251 | * with bad ideas only. New subsystems should use plain subsystems; and | |
1252 | * add the subsystem-wide attributes should be added to the subsystem | |
1253 | * directory itself and not some create fake root-device placed in | |
1254 | * /sys/devices/system/<name>. | |
1255 | */ | |
1256 | int subsys_system_register(struct bus_type *subsys, | |
1257 | const struct attribute_group **groups) | |
1258 | { | |
1259 | return subsys_register(subsys, groups, &system_kset->kobj); | |
1260 | } | |
ca22e56d KS |
1261 | EXPORT_SYMBOL_GPL(subsys_system_register); |
1262 | ||
d73ce004 TH |
1263 | /** |
1264 | * subsys_virtual_register - register a subsystem at /sys/devices/virtual/ | |
1265 | * @subsys: virtual subsystem | |
1266 | * @groups: default attributes for the root device | |
1267 | * | |
1268 | * All 'virtual' subsystems have a /sys/devices/system/<name> root device | |
1269 | * with the name of the subystem. The root device can carry subsystem-wide | |
1270 | * attributes. All registered devices are below this single root device. | |
1271 | * There's no restriction on device naming. This is for kernel software | |
1272 | * constructs which need sysfs interface. | |
1273 | */ | |
1274 | int subsys_virtual_register(struct bus_type *subsys, | |
1275 | const struct attribute_group **groups) | |
1276 | { | |
1277 | struct kobject *virtual_dir; | |
1278 | ||
1279 | virtual_dir = virtual_device_parent(NULL); | |
1280 | if (!virtual_dir) | |
1281 | return -ENOMEM; | |
1282 | ||
1283 | return subsys_register(subsys, groups, virtual_dir); | |
1284 | } | |
1c04fc35 | 1285 | EXPORT_SYMBOL_GPL(subsys_virtual_register); |
d73ce004 | 1286 | |
adc18506 GKH |
1287 | /** |
1288 | * driver_find - locate driver on a bus by its name. | |
1289 | * @name: name of the driver. | |
1290 | * @bus: bus to scan for the driver. | |
1291 | * | |
1292 | * Call kset_find_obj() to iterate over list of drivers on | |
1293 | * a bus to find driver by name. Return driver if found. | |
1294 | * | |
1295 | * This routine provides no locking to prevent the driver it returns | |
1296 | * from being unregistered or unloaded while the caller is using it. | |
1297 | * The caller is responsible for preventing this. | |
1298 | */ | |
1299 | struct device_driver *driver_find(const char *name, struct bus_type *bus) | |
1300 | { | |
fb451966 GKH |
1301 | struct subsys_private *sp = bus_to_subsys(bus); |
1302 | struct kobject *k; | |
adc18506 GKH |
1303 | struct driver_private *priv; |
1304 | ||
fb451966 GKH |
1305 | if (!sp) |
1306 | return NULL; | |
1307 | ||
1308 | k = kset_find_obj(sp->drivers_kset, name); | |
1309 | subsys_put(sp); | |
1310 | if (!k) | |
1311 | return NULL; | |
1312 | ||
1313 | priv = to_driver(k); | |
1314 | ||
1315 | /* Drop reference added by kset_find_obj() */ | |
1316 | kobject_put(k); | |
1317 | return priv->driver; | |
adc18506 GKH |
1318 | } |
1319 | EXPORT_SYMBOL_GPL(driver_find); | |
1320 | ||
63b823d7 GKH |
1321 | /* |
1322 | * Warning, the value could go to "removed" instantly after calling this function, so be very | |
1323 | * careful when calling it... | |
1324 | */ | |
1325 | bool bus_is_registered(const struct bus_type *bus) | |
1326 | { | |
1327 | struct subsys_private *sp = bus_to_subsys(bus); | |
1328 | bool is_initialized = false; | |
1329 | ||
1330 | if (sp) { | |
1331 | is_initialized = true; | |
1332 | subsys_put(sp); | |
1333 | } | |
1334 | return is_initialized; | |
1335 | } | |
1336 | ||
1da177e4 LT |
1337 | int __init buses_init(void) |
1338 | { | |
59a54833 GKH |
1339 | bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL); |
1340 | if (!bus_kset) | |
1341 | return -ENOMEM; | |
ca22e56d KS |
1342 | |
1343 | system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj); | |
1344 | if (!system_kset) | |
1345 | return -ENOMEM; | |
1346 | ||
59a54833 | 1347 | return 0; |
1da177e4 | 1348 | } |