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