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 BVA |
208 | struct bus_type *bus = priv->bus; |
209 | ||
37e98d9b | 210 | lockdep_unregister_key(&priv->lock_key); |
174be70b BVA |
211 | kfree(priv); |
212 | bus->p = NULL; | |
213 | } | |
214 | ||
c83d9ab4 | 215 | static const struct kobj_type bus_ktype = { |
1da177e4 | 216 | .sysfs_ops = &bus_sysfs_ops, |
174be70b | 217 | .release = bus_release, |
80f03e34 KS |
218 | }; |
219 | ||
c45a88bb | 220 | static int bus_uevent_filter(const struct kobject *kobj) |
80f03e34 | 221 | { |
ee6d3dd4 | 222 | const struct kobj_type *ktype = get_ktype(kobj); |
80f03e34 KS |
223 | |
224 | if (ktype == &bus_ktype) | |
225 | return 1; | |
226 | return 0; | |
227 | } | |
1da177e4 | 228 | |
9cd43611 | 229 | static const struct kset_uevent_ops bus_uevent_ops = { |
80f03e34 | 230 | .filter = bus_uevent_filter, |
1da177e4 LT |
231 | }; |
232 | ||
2b08c8d0 | 233 | /* Manually detach a device from its associated driver. */ |
2581c9cc GKH |
234 | static ssize_t unbind_store(struct device_driver *drv, const char *buf, |
235 | size_t count) | |
151ef38f | 236 | { |
5901d014 | 237 | struct bus_type *bus = bus_get(drv->bus); |
151ef38f GKH |
238 | struct device *dev; |
239 | int err = -ENODEV; | |
240 | ||
1f9ffc04 | 241 | dev = bus_find_device_by_name(bus, NULL, buf); |
2b08c8d0 | 242 | if (dev && dev->driver == drv) { |
ed88747c | 243 | device_driver_detach(dev); |
151ef38f GKH |
244 | err = count; |
245 | } | |
2b08c8d0 | 246 | put_device(dev); |
fc1ede58 | 247 | bus_put(bus); |
2b08c8d0 | 248 | return err; |
151ef38f | 249 | } |
16b0dd40 | 250 | static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, 0200, NULL, unbind_store); |
151ef38f | 251 | |
afdce75f GKH |
252 | /* |
253 | * Manually attach a device to a driver. | |
254 | * Note: the driver must want to bind to the device, | |
255 | * it is not possible to override the driver's id table. | |
256 | */ | |
2581c9cc GKH |
257 | static ssize_t bind_store(struct device_driver *drv, const char *buf, |
258 | size_t count) | |
afdce75f | 259 | { |
5901d014 | 260 | struct bus_type *bus = bus_get(drv->bus); |
afdce75f GKH |
261 | struct device *dev; |
262 | int err = -ENODEV; | |
263 | ||
1f9ffc04 | 264 | dev = bus_find_device_by_name(bus, NULL, buf); |
204db60c | 265 | if (dev && driver_match_device(drv, dev)) { |
ed88747c | 266 | err = device_driver_attach(drv, dev); |
ef6dcbdd | 267 | if (!err) { |
4a3ad20c | 268 | /* success */ |
37225401 | 269 | err = count; |
4a3ad20c | 270 | } |
afdce75f | 271 | } |
2b08c8d0 | 272 | put_device(dev); |
fc1ede58 | 273 | bus_put(bus); |
2b08c8d0 | 274 | return err; |
afdce75f | 275 | } |
16b0dd40 | 276 | static DRIVER_ATTR_IGNORE_LOCKDEP(bind, 0200, NULL, bind_store); |
afdce75f | 277 | |
2e7189b6 | 278 | static ssize_t drivers_autoprobe_show(struct bus_type *bus, char *buf) |
b8c5cec2 | 279 | { |
948b3edb | 280 | return sysfs_emit(buf, "%d\n", bus->p->drivers_autoprobe); |
b8c5cec2 KS |
281 | } |
282 | ||
2e7189b6 | 283 | static ssize_t drivers_autoprobe_store(struct bus_type *bus, |
b8c5cec2 KS |
284 | const char *buf, size_t count) |
285 | { | |
286 | if (buf[0] == '0') | |
c6f7e72a | 287 | bus->p->drivers_autoprobe = 0; |
b8c5cec2 | 288 | else |
c6f7e72a | 289 | bus->p->drivers_autoprobe = 1; |
b8c5cec2 KS |
290 | return count; |
291 | } | |
292 | ||
2e7189b6 | 293 | static ssize_t drivers_probe_store(struct bus_type *bus, |
b8c5cec2 KS |
294 | const char *buf, size_t count) |
295 | { | |
296 | struct device *dev; | |
0372ffb3 | 297 | int err = -EINVAL; |
b8c5cec2 | 298 | |
1f9ffc04 | 299 | dev = bus_find_device_by_name(bus, NULL, buf); |
b8c5cec2 KS |
300 | if (!dev) |
301 | return -ENODEV; | |
0372ffb3 AW |
302 | if (bus_rescan_devices_helper(dev, NULL) == 0) |
303 | err = count; | |
304 | put_device(dev); | |
305 | return err; | |
b8c5cec2 | 306 | } |
151ef38f | 307 | |
4a3ad20c | 308 | static struct device *next_device(struct klist_iter *i) |
465c7a3a | 309 | { |
4a3ad20c | 310 | struct klist_node *n = klist_next(i); |
ae1b4171 GKH |
311 | struct device *dev = NULL; |
312 | struct device_private *dev_prv; | |
313 | ||
314 | if (n) { | |
315 | dev_prv = to_device_private_bus(n); | |
316 | dev = dev_prv->device; | |
317 | } | |
318 | return dev; | |
465c7a3a | 319 | } |
320 | ||
1da177e4 | 321 | /** |
4a3ad20c GKH |
322 | * bus_for_each_dev - device iterator. |
323 | * @bus: bus type. | |
324 | * @start: device to start iterating from. | |
325 | * @data: data for the callback. | |
326 | * @fn: function to be called for each device. | |
1da177e4 | 327 | * |
4a3ad20c GKH |
328 | * Iterate over @bus's list of devices, and call @fn for each, |
329 | * passing it @data. If @start is not NULL, we use that device to | |
330 | * begin iterating from. | |
1da177e4 | 331 | * |
4a3ad20c GKH |
332 | * We check the return of @fn each time. If it returns anything |
333 | * other than 0, we break out and return that value. | |
1da177e4 | 334 | * |
4a3ad20c GKH |
335 | * NOTE: The device that returns a non-zero value is not retained |
336 | * in any way, nor is its refcount incremented. If the caller needs | |
0fa1b0a1 | 337 | * to retain this data, it should do so, and increment the reference |
4a3ad20c | 338 | * count in the supplied callback. |
1da177e4 | 339 | */ |
e0766ea4 | 340 | int bus_for_each_dev(const struct bus_type *bus, struct device *start, |
4a3ad20c | 341 | void *data, int (*fn)(struct device *, void *)) |
1da177e4 | 342 | { |
465c7a3a | 343 | struct klist_iter i; |
4a3ad20c | 344 | struct device *dev; |
465c7a3a | 345 | int error = 0; |
1da177e4 | 346 | |
4fa3e78b | 347 | if (!bus || !bus->p) |
465c7a3a | 348 | return -EINVAL; |
349 | ||
7cd9c9bb GKH |
350 | klist_iter_init_node(&bus->p->klist_devices, &i, |
351 | (start ? &start->p->knode_bus : NULL)); | |
93ead7c9 | 352 | while (!error && (dev = next_device(&i))) |
7cd9c9bb GKH |
353 | error = fn(dev, data); |
354 | klist_iter_exit(&i); | |
465c7a3a | 355 | return error; |
1da177e4 | 356 | } |
4a3ad20c | 357 | EXPORT_SYMBOL_GPL(bus_for_each_dev); |
1da177e4 | 358 | |
0edb5860 CH |
359 | /** |
360 | * bus_find_device - device iterator for locating a particular device. | |
361 | * @bus: bus type | |
362 | * @start: Device to begin with | |
363 | * @data: Data to pass to match function | |
364 | * @match: Callback function to check device | |
365 | * | |
366 | * This is similar to the bus_for_each_dev() function above, but it | |
367 | * returns a reference to a device that is 'found' for later use, as | |
368 | * determined by the @match callback. | |
369 | * | |
370 | * The callback should return 0 if the device doesn't match and non-zero | |
371 | * if it does. If the callback returns non-zero, this function will | |
372 | * return to the caller and not iterate over any more devices. | |
373 | */ | |
e0766ea4 | 374 | struct device *bus_find_device(const struct bus_type *bus, |
418e3ea1 SP |
375 | struct device *start, const void *data, |
376 | int (*match)(struct device *dev, const void *data)) | |
0edb5860 CH |
377 | { |
378 | struct klist_iter i; | |
379 | struct device *dev; | |
380 | ||
4fa3e78b | 381 | if (!bus || !bus->p) |
0edb5860 CH |
382 | return NULL; |
383 | ||
7cd9c9bb GKH |
384 | klist_iter_init_node(&bus->p->klist_devices, &i, |
385 | (start ? &start->p->knode_bus : NULL)); | |
0edb5860 CH |
386 | while ((dev = next_device(&i))) |
387 | if (match(dev, data) && get_device(dev)) | |
388 | break; | |
389 | klist_iter_exit(&i); | |
390 | return dev; | |
391 | } | |
4a3ad20c | 392 | EXPORT_SYMBOL_GPL(bus_find_device); |
38fdac3c | 393 | |
4a3ad20c | 394 | static struct device_driver *next_driver(struct klist_iter *i) |
38fdac3c | 395 | { |
4a3ad20c | 396 | struct klist_node *n = klist_next(i); |
e5dd1278 GKH |
397 | struct driver_private *drv_priv; |
398 | ||
399 | if (n) { | |
400 | drv_priv = container_of(n, struct driver_private, knode_bus); | |
401 | return drv_priv->driver; | |
402 | } | |
403 | return NULL; | |
38fdac3c | 404 | } |
405 | ||
1da177e4 | 406 | /** |
4a3ad20c GKH |
407 | * bus_for_each_drv - driver iterator |
408 | * @bus: bus we're dealing with. | |
409 | * @start: driver to start iterating on. | |
410 | * @data: data to pass to the callback. | |
411 | * @fn: function to call for each driver. | |
1da177e4 | 412 | * |
4a3ad20c GKH |
413 | * This is nearly identical to the device iterator above. |
414 | * We iterate over each driver that belongs to @bus, and call | |
415 | * @fn for each. If @fn returns anything but 0, we break out | |
416 | * and return it. If @start is not NULL, we use it as the head | |
417 | * of the list. | |
1da177e4 | 418 | * |
4a3ad20c GKH |
419 | * NOTE: we don't return the driver that returns a non-zero |
420 | * value, nor do we leave the reference count incremented for that | |
421 | * driver. If the caller needs to know that info, it must set it | |
422 | * in the callback. It must also be sure to increment the refcount | |
423 | * so it doesn't disappear before returning to the caller. | |
1da177e4 | 424 | */ |
e0766ea4 | 425 | int bus_for_each_drv(const struct bus_type *bus, struct device_driver *start, |
4a3ad20c | 426 | void *data, int (*fn)(struct device_driver *, void *)) |
1da177e4 | 427 | { |
38fdac3c | 428 | struct klist_iter i; |
4a3ad20c | 429 | struct device_driver *drv; |
38fdac3c | 430 | int error = 0; |
1da177e4 | 431 | |
38fdac3c | 432 | if (!bus) |
433 | return -EINVAL; | |
434 | ||
7cd9c9bb GKH |
435 | klist_iter_init_node(&bus->p->klist_drivers, &i, |
436 | start ? &start->p->knode_bus : NULL); | |
437 | while ((drv = next_driver(&i)) && !error) | |
438 | error = fn(drv, data); | |
439 | klist_iter_exit(&i); | |
38fdac3c | 440 | return error; |
1da177e4 | 441 | } |
4a3ad20c | 442 | EXPORT_SYMBOL_GPL(bus_for_each_drv); |
1da177e4 | 443 | |
1da177e4 | 444 | /** |
4a3ad20c GKH |
445 | * bus_add_device - add device to bus |
446 | * @dev: device being added | |
1da177e4 | 447 | * |
2023c610 AS |
448 | * - Add device's bus attributes. |
449 | * - Create links to device's bus. | |
4a3ad20c | 450 | * - Add the device to its bus's list of devices. |
1da177e4 | 451 | */ |
4a3ad20c | 452 | int bus_add_device(struct device *dev) |
1da177e4 | 453 | { |
4a3ad20c | 454 | struct bus_type *bus = bus_get(dev->bus); |
1da177e4 LT |
455 | int error = 0; |
456 | ||
457 | if (bus) { | |
1e0b2cf9 | 458 | pr_debug("bus: '%s': add device %s\n", bus->name, dev_name(dev)); |
fa6fdb33 GKH |
459 | error = device_add_groups(dev, bus->dev_groups); |
460 | if (error) | |
b46c7337 | 461 | goto out_put; |
c6f7e72a | 462 | error = sysfs_create_link(&bus->p->devices_kset->kobj, |
1e0b2cf9 | 463 | &dev->kobj, dev_name(dev)); |
f86db396 | 464 | if (error) |
1c34203a | 465 | goto out_groups; |
f86db396 | 466 | error = sysfs_create_link(&dev->kobj, |
c6f7e72a | 467 | &dev->bus->p->subsys.kobj, "subsystem"); |
f86db396 | 468 | if (error) |
513e7337 | 469 | goto out_subsys; |
2023c610 | 470 | klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices); |
1da177e4 | 471 | } |
513e7337 CH |
472 | return 0; |
473 | ||
513e7337 | 474 | out_subsys: |
1e0b2cf9 | 475 | sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev)); |
fa6fdb33 GKH |
476 | out_groups: |
477 | device_remove_groups(dev, bus->dev_groups); | |
513e7337 | 478 | out_put: |
fc1ede58 | 479 | bus_put(dev->bus); |
1da177e4 LT |
480 | return error; |
481 | } | |
482 | ||
53877d06 | 483 | /** |
2023c610 AS |
484 | * bus_probe_device - probe drivers for a new device |
485 | * @dev: device to probe | |
53877d06 | 486 | * |
2023c610 | 487 | * - Automatically probe for a driver if the bus allows it. |
53877d06 | 488 | */ |
2023c610 | 489 | void bus_probe_device(struct device *dev) |
53877d06 | 490 | { |
f86db396 | 491 | struct bus_type *bus = dev->bus; |
ca22e56d | 492 | struct subsys_interface *sif; |
53877d06 | 493 | |
ca22e56d KS |
494 | if (!bus) |
495 | return; | |
496 | ||
765230b5 DT |
497 | if (bus->p->drivers_autoprobe) |
498 | device_initial_probe(dev); | |
ca22e56d KS |
499 | |
500 | mutex_lock(&bus->p->mutex); | |
501 | list_for_each_entry(sif, &bus->p->interfaces, node) | |
502 | if (sif->add_dev) | |
503 | sif->add_dev(dev, sif); | |
504 | mutex_unlock(&bus->p->mutex); | |
53877d06 KS |
505 | } |
506 | ||
1da177e4 | 507 | /** |
4a3ad20c GKH |
508 | * bus_remove_device - remove device from bus |
509 | * @dev: device to be removed | |
1da177e4 | 510 | * |
ca22e56d KS |
511 | * - Remove device from all interfaces. |
512 | * - Remove symlink from bus' directory. | |
4a3ad20c GKH |
513 | * - Delete device from bus's list. |
514 | * - Detach from its driver. | |
515 | * - Drop reference taken in bus_add_device(). | |
1da177e4 | 516 | */ |
4a3ad20c | 517 | void bus_remove_device(struct device *dev) |
1da177e4 | 518 | { |
ca22e56d KS |
519 | struct bus_type *bus = dev->bus; |
520 | struct subsys_interface *sif; | |
521 | ||
522 | if (!bus) | |
523 | return; | |
524 | ||
525 | mutex_lock(&bus->p->mutex); | |
526 | list_for_each_entry(sif, &bus->p->interfaces, node) | |
527 | if (sif->remove_dev) | |
528 | sif->remove_dev(dev, sif); | |
529 | mutex_unlock(&bus->p->mutex); | |
530 | ||
531 | sysfs_remove_link(&dev->kobj, "subsystem"); | |
532 | sysfs_remove_link(&dev->bus->p->devices_kset->kobj, | |
533 | dev_name(dev)); | |
fa6fdb33 | 534 | device_remove_groups(dev, dev->bus->dev_groups); |
ca22e56d KS |
535 | if (klist_node_attached(&dev->p->knode_bus)) |
536 | klist_del(&dev->p->knode_bus); | |
537 | ||
538 | pr_debug("bus: '%s': remove device %s\n", | |
539 | dev->bus->name, dev_name(dev)); | |
540 | device_release_driver(dev); | |
541 | bus_put(dev->bus); | |
1da177e4 LT |
542 | } |
543 | ||
f86db396 | 544 | static int __must_check add_bind_files(struct device_driver *drv) |
874c6241 | 545 | { |
f86db396 AM |
546 | int ret; |
547 | ||
548 | ret = driver_create_file(drv, &driver_attr_unbind); | |
549 | if (ret == 0) { | |
550 | ret = driver_create_file(drv, &driver_attr_bind); | |
551 | if (ret) | |
552 | driver_remove_file(drv, &driver_attr_unbind); | |
553 | } | |
554 | return ret; | |
874c6241 GKH |
555 | } |
556 | ||
557 | static void remove_bind_files(struct device_driver *drv) | |
558 | { | |
559 | driver_remove_file(drv, &driver_attr_bind); | |
560 | driver_remove_file(drv, &driver_attr_unbind); | |
561 | } | |
b8c5cec2 | 562 | |
2e7189b6 GKH |
563 | static BUS_ATTR_WO(drivers_probe); |
564 | static BUS_ATTR_RW(drivers_autoprobe); | |
8380770c | 565 | |
b8c5cec2 KS |
566 | static int add_probe_files(struct bus_type *bus) |
567 | { | |
568 | int retval; | |
569 | ||
8380770c | 570 | retval = bus_create_file(bus, &bus_attr_drivers_probe); |
b8c5cec2 KS |
571 | if (retval) |
572 | goto out; | |
573 | ||
8380770c | 574 | retval = bus_create_file(bus, &bus_attr_drivers_autoprobe); |
b8c5cec2 | 575 | if (retval) |
8380770c | 576 | bus_remove_file(bus, &bus_attr_drivers_probe); |
b8c5cec2 KS |
577 | out: |
578 | return retval; | |
579 | } | |
580 | ||
581 | static void remove_probe_files(struct bus_type *bus) | |
582 | { | |
8380770c KS |
583 | bus_remove_file(bus, &bus_attr_drivers_autoprobe); |
584 | bus_remove_file(bus, &bus_attr_drivers_probe); | |
b8c5cec2 | 585 | } |
1da177e4 | 586 | |
2581c9cc GKH |
587 | static ssize_t uevent_store(struct device_driver *drv, const char *buf, |
588 | size_t count) | |
7ac1cf4a | 589 | { |
df44b479 PR |
590 | int rc; |
591 | ||
592 | rc = kobject_synth_uevent(&drv->p->kobj, buf, count); | |
593 | return rc ? rc : count; | |
7ac1cf4a | 594 | } |
2581c9cc | 595 | static DRIVER_ATTR_WO(uevent); |
7ac1cf4a | 596 | |
1da177e4 | 597 | /** |
4a3ad20c GKH |
598 | * bus_add_driver - Add a driver to the bus. |
599 | * @drv: driver. | |
1da177e4 | 600 | */ |
f86db396 | 601 | int bus_add_driver(struct device_driver *drv) |
1da177e4 | 602 | { |
e5dd1278 GKH |
603 | struct bus_type *bus; |
604 | struct driver_private *priv; | |
1da177e4 LT |
605 | int error = 0; |
606 | ||
e5dd1278 | 607 | bus = bus_get(drv->bus); |
d9fd4d3b | 608 | if (!bus) |
4f6e1945 | 609 | return -EINVAL; |
d9fd4d3b | 610 | |
7dc72b28 | 611 | pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name); |
e5dd1278 GKH |
612 | |
613 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); | |
07634464 CH |
614 | if (!priv) { |
615 | error = -ENOMEM; | |
616 | goto out_put_bus; | |
617 | } | |
e5dd1278 GKH |
618 | klist_init(&priv->klist_devices, NULL, NULL); |
619 | priv->driver = drv; | |
620 | drv->p = priv; | |
c8e90d82 GKH |
621 | priv->kobj.kset = bus->p->drivers_kset; |
622 | error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL, | |
623 | "%s", drv->name); | |
dc0afa83 | 624 | if (error) |
07634464 | 625 | goto out_unregister; |
d9fd4d3b | 626 | |
190888ac | 627 | klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers); |
c6f7e72a | 628 | if (drv->bus->p->drivers_autoprobe) { |
ef0ff683 AD |
629 | error = driver_attach(drv); |
630 | if (error) | |
310862e5 | 631 | goto out_del_list; |
b8c5cec2 | 632 | } |
d9fd4d3b JG |
633 | module_add_driver(drv->owner, drv); |
634 | ||
7ac1cf4a KS |
635 | error = driver_create_file(drv, &driver_attr_uevent); |
636 | if (error) { | |
637 | printk(KERN_ERR "%s: uevent attr (%s) failed\n", | |
2b3a302a | 638 | __func__, drv->name); |
7ac1cf4a | 639 | } |
e18945b1 | 640 | error = driver_add_groups(drv, bus->drv_groups); |
d9fd4d3b JG |
641 | if (error) { |
642 | /* How the hell do we get out of this pickle? Give up */ | |
4044b2fc | 643 | printk(KERN_ERR "%s: driver_add_groups(%s) failed\n", |
ed0617b5 | 644 | __func__, drv->name); |
e18945b1 | 645 | } |
1a6f2a75 DT |
646 | |
647 | if (!drv->suppress_bind_attrs) { | |
648 | error = add_bind_files(drv); | |
649 | if (error) { | |
650 | /* Ditto */ | |
651 | printk(KERN_ERR "%s: add_bind_files(%s) failed\n", | |
652 | __func__, drv->name); | |
653 | } | |
1da177e4 | 654 | } |
d9fd4d3b | 655 | |
5c8563d7 | 656 | return 0; |
1a6f2a75 | 657 | |
310862e5 SS |
658 | out_del_list: |
659 | klist_del(&priv->knode_bus); | |
f86db396 | 660 | out_unregister: |
99b28f1b | 661 | kobject_put(&priv->kobj); |
0f9b011d | 662 | /* drv->p is freed in driver_release() */ |
5c8563d7 | 663 | drv->p = NULL; |
f86db396 | 664 | out_put_bus: |
fc1ede58 | 665 | bus_put(bus); |
f86db396 | 666 | return error; |
1da177e4 LT |
667 | } |
668 | ||
1da177e4 | 669 | /** |
4a3ad20c GKH |
670 | * bus_remove_driver - delete driver from bus's knowledge. |
671 | * @drv: driver. | |
1da177e4 | 672 | * |
4a3ad20c GKH |
673 | * Detach the driver from the devices it controls, and remove |
674 | * it from its bus's list of drivers. Finally, we drop the reference | |
675 | * to the bus we took in bus_add_driver(). | |
1da177e4 | 676 | */ |
4a3ad20c | 677 | void bus_remove_driver(struct device_driver *drv) |
1da177e4 | 678 | { |
d9fd4d3b JG |
679 | if (!drv->bus) |
680 | return; | |
681 | ||
1a6f2a75 DT |
682 | if (!drv->suppress_bind_attrs) |
683 | remove_bind_files(drv); | |
ed0617b5 | 684 | driver_remove_groups(drv, drv->bus->drv_groups); |
7ac1cf4a | 685 | driver_remove_file(drv, &driver_attr_uevent); |
e5dd1278 | 686 | klist_remove(&drv->p->knode_bus); |
7dc72b28 | 687 | pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name); |
d9fd4d3b JG |
688 | driver_detach(drv); |
689 | module_remove_driver(drv); | |
c10997f6 | 690 | kobject_put(&drv->p->kobj); |
fc1ede58 | 691 | bus_put(drv->bus); |
1da177e4 LT |
692 | } |
693 | ||
1da177e4 | 694 | /* Helper for bus_rescan_devices's iter */ |
f86db396 | 695 | static int __must_check bus_rescan_devices_helper(struct device *dev, |
4a3ad20c | 696 | void *data) |
1da177e4 | 697 | { |
f86db396 AM |
698 | int ret = 0; |
699 | ||
bf74ad5b | 700 | if (!dev->driver) { |
8c97a46a | 701 | if (dev->parent && dev->bus->need_parent_lock) |
8e9394ce | 702 | device_lock(dev->parent); |
f86db396 | 703 | ret = device_attach(dev); |
8c97a46a | 704 | if (dev->parent && dev->bus->need_parent_lock) |
8e9394ce | 705 | device_unlock(dev->parent); |
bf74ad5b | 706 | } |
f86db396 | 707 | return ret < 0 ? ret : 0; |
1da177e4 LT |
708 | } |
709 | ||
1da177e4 | 710 | /** |
23d3d602 GKH |
711 | * bus_rescan_devices - rescan devices on the bus for possible drivers |
712 | * @bus: the bus to scan. | |
1da177e4 | 713 | * |
23d3d602 GKH |
714 | * This function will look for devices on the bus with no driver |
715 | * attached and rescan it against existing drivers to see if it matches | |
716 | * any by calling device_attach() for the unbound devices. | |
1da177e4 | 717 | */ |
4a3ad20c | 718 | int bus_rescan_devices(struct bus_type *bus) |
1da177e4 | 719 | { |
f86db396 | 720 | return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper); |
1da177e4 | 721 | } |
4a3ad20c | 722 | EXPORT_SYMBOL_GPL(bus_rescan_devices); |
1da177e4 | 723 | |
e935d5da ME |
724 | /** |
725 | * device_reprobe - remove driver for a device and probe for a new driver | |
726 | * @dev: the device to reprobe | |
727 | * | |
728 | * This function detaches the attached driver (if any) for the given | |
729 | * device and restarts the driver probing process. It is intended | |
730 | * to use if probing criteria changed during a devices lifetime and | |
731 | * driver attachment should change accordingly. | |
732 | */ | |
f86db396 | 733 | int device_reprobe(struct device *dev) |
e935d5da | 734 | { |
ed88747c AD |
735 | if (dev->driver) |
736 | device_driver_detach(dev); | |
f86db396 | 737 | return bus_rescan_devices_helper(dev, NULL); |
e935d5da ME |
738 | } |
739 | EXPORT_SYMBOL_GPL(device_reprobe); | |
1da177e4 | 740 | |
12478ba0 GKH |
741 | static int bus_add_groups(struct bus_type *bus, |
742 | const struct attribute_group **groups) | |
743 | { | |
3e9b2bae | 744 | return sysfs_create_groups(&bus->p->subsys.kobj, groups); |
12478ba0 GKH |
745 | } |
746 | ||
747 | static void bus_remove_groups(struct bus_type *bus, | |
748 | const struct attribute_group **groups) | |
749 | { | |
3e9b2bae | 750 | sysfs_remove_groups(&bus->p->subsys.kobj, groups); |
12478ba0 GKH |
751 | } |
752 | ||
34bb61f9 JB |
753 | static void klist_devices_get(struct klist_node *n) |
754 | { | |
ae1b4171 GKH |
755 | struct device_private *dev_prv = to_device_private_bus(n); |
756 | struct device *dev = dev_prv->device; | |
34bb61f9 JB |
757 | |
758 | get_device(dev); | |
759 | } | |
760 | ||
761 | static void klist_devices_put(struct klist_node *n) | |
762 | { | |
ae1b4171 GKH |
763 | struct device_private *dev_prv = to_device_private_bus(n); |
764 | struct device *dev = dev_prv->device; | |
34bb61f9 JB |
765 | |
766 | put_device(dev); | |
767 | } | |
768 | ||
7ac1cf4a KS |
769 | static ssize_t bus_uevent_store(struct bus_type *bus, |
770 | const char *buf, size_t count) | |
771 | { | |
df44b479 PR |
772 | int rc; |
773 | ||
774 | rc = kobject_synth_uevent(&bus->p->subsys.kobj, buf, count); | |
775 | return rc ? rc : count; | |
7ac1cf4a | 776 | } |
a4723041 GKH |
777 | /* |
778 | * "open code" the old BUS_ATTR() macro here. We want to use BUS_ATTR_WO() | |
779 | * here, but can not use it as earlier in the file we have | |
780 | * DEVICE_ATTR_WO(uevent), which would cause a clash with the with the store | |
781 | * function name. | |
782 | */ | |
16b0dd40 | 783 | static struct bus_attribute bus_attr_uevent = __ATTR(uevent, 0200, NULL, |
a4723041 | 784 | bus_uevent_store); |
7ac1cf4a | 785 | |
1da177e4 | 786 | /** |
be871b7e | 787 | * bus_register - register a driver-core subsystem |
78d79559 | 788 | * @bus: bus to register |
1da177e4 | 789 | * |
78d79559 | 790 | * Once we have that, we register the bus with the kobject |
4a3ad20c | 791 | * infrastructure, then register the children subsystems it has: |
ca22e56d | 792 | * the devices and drivers that belong to the subsystem. |
1da177e4 | 793 | */ |
be871b7e | 794 | int bus_register(struct bus_type *bus) |
1da177e4 LT |
795 | { |
796 | int retval; | |
6b6e39a6 | 797 | struct subsys_private *priv; |
37e98d9b | 798 | struct lock_class_key *key; |
c6f7e72a | 799 | |
6b6e39a6 | 800 | priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL); |
c6f7e72a GKH |
801 | if (!priv) |
802 | return -ENOMEM; | |
803 | ||
804 | priv->bus = bus; | |
805 | bus->p = priv; | |
1da177e4 | 806 | |
c6f7e72a | 807 | BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier); |
116af378 | 808 | |
c6f7e72a | 809 | retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name); |
1da177e4 LT |
810 | if (retval) |
811 | goto out; | |
812 | ||
c6f7e72a GKH |
813 | priv->subsys.kobj.kset = bus_kset; |
814 | priv->subsys.kobj.ktype = &bus_ktype; | |
815 | priv->drivers_autoprobe = 1; | |
d6b05b84 | 816 | |
c6f7e72a | 817 | retval = kset_register(&priv->subsys); |
1da177e4 LT |
818 | if (retval) |
819 | goto out; | |
820 | ||
7ac1cf4a KS |
821 | retval = bus_create_file(bus, &bus_attr_uevent); |
822 | if (retval) | |
823 | goto bus_uevent_fail; | |
824 | ||
c6f7e72a GKH |
825 | priv->devices_kset = kset_create_and_add("devices", NULL, |
826 | &priv->subsys.kobj); | |
827 | if (!priv->devices_kset) { | |
3d899596 | 828 | retval = -ENOMEM; |
1da177e4 | 829 | goto bus_devices_fail; |
3d899596 | 830 | } |
1da177e4 | 831 | |
c6f7e72a GKH |
832 | priv->drivers_kset = kset_create_and_add("drivers", NULL, |
833 | &priv->subsys.kobj); | |
834 | if (!priv->drivers_kset) { | |
6dcec251 | 835 | retval = -ENOMEM; |
1da177e4 | 836 | goto bus_drivers_fail; |
6dcec251 | 837 | } |
465c7a3a | 838 | |
ca22e56d | 839 | INIT_LIST_HEAD(&priv->interfaces); |
37e98d9b GKH |
840 | key = &priv->lock_key; |
841 | lockdep_register_key(key); | |
ca22e56d | 842 | __mutex_init(&priv->mutex, "subsys mutex", key); |
c6f7e72a GKH |
843 | klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put); |
844 | klist_init(&priv->klist_drivers, NULL, NULL); | |
b8c5cec2 | 845 | |
b8c5cec2 KS |
846 | retval = add_probe_files(bus); |
847 | if (retval) | |
848 | goto bus_probe_files_fail; | |
849 | ||
12478ba0 GKH |
850 | retval = bus_add_groups(bus, bus->bus_groups); |
851 | if (retval) | |
852 | goto bus_groups_fail; | |
1da177e4 | 853 | |
7dc72b28 | 854 | pr_debug("bus: '%s': registered\n", bus->name); |
1da177e4 LT |
855 | return 0; |
856 | ||
12478ba0 | 857 | bus_groups_fail: |
b8c5cec2 KS |
858 | remove_probe_files(bus); |
859 | bus_probe_files_fail: | |
c6f7e72a | 860 | kset_unregister(bus->p->drivers_kset); |
1da177e4 | 861 | bus_drivers_fail: |
c6f7e72a | 862 | kset_unregister(bus->p->devices_kset); |
1da177e4 | 863 | bus_devices_fail: |
7ac1cf4a KS |
864 | bus_remove_file(bus, &bus_attr_uevent); |
865 | bus_uevent_fail: | |
c6f7e72a | 866 | kset_unregister(&bus->p->subsys); |
1da177e4 | 867 | out: |
600c20f3 | 868 | kfree(bus->p); |
f48f3feb | 869 | bus->p = NULL; |
1da177e4 LT |
870 | return retval; |
871 | } | |
be871b7e | 872 | EXPORT_SYMBOL_GPL(bus_register); |
1da177e4 | 873 | |
1da177e4 | 874 | /** |
4a3ad20c GKH |
875 | * bus_unregister - remove a bus from the system |
876 | * @bus: bus. | |
1da177e4 | 877 | * |
4a3ad20c GKH |
878 | * Unregister the child subsystems and the bus itself. |
879 | * Finally, we call bus_put() to release the refcount | |
1da177e4 | 880 | */ |
4a3ad20c | 881 | void bus_unregister(struct bus_type *bus) |
1da177e4 | 882 | { |
7dc72b28 | 883 | pr_debug("bus: '%s': unregistering\n", bus->name); |
ca22e56d KS |
884 | if (bus->dev_root) |
885 | device_unregister(bus->dev_root); | |
12478ba0 | 886 | bus_remove_groups(bus, bus->bus_groups); |
b8c5cec2 | 887 | remove_probe_files(bus); |
c6f7e72a GKH |
888 | kset_unregister(bus->p->drivers_kset); |
889 | kset_unregister(bus->p->devices_kset); | |
7ac1cf4a | 890 | bus_remove_file(bus, &bus_attr_uevent); |
c6f7e72a | 891 | kset_unregister(&bus->p->subsys); |
1da177e4 | 892 | } |
4a3ad20c | 893 | EXPORT_SYMBOL_GPL(bus_unregister); |
1da177e4 | 894 | |
116af378 BH |
895 | int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb) |
896 | { | |
c6f7e72a | 897 | return blocking_notifier_chain_register(&bus->p->bus_notifier, nb); |
116af378 BH |
898 | } |
899 | EXPORT_SYMBOL_GPL(bus_register_notifier); | |
900 | ||
901 | int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb) | |
902 | { | |
c6f7e72a | 903 | return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb); |
116af378 BH |
904 | } |
905 | EXPORT_SYMBOL_GPL(bus_unregister_notifier); | |
906 | ||
ed9f9181 GKH |
907 | void bus_notify(struct device *dev, enum bus_notifier_event value) |
908 | { | |
909 | struct bus_type *bus = dev->bus; | |
910 | ||
911 | if (bus) | |
912 | blocking_notifier_call_chain(&bus->p->bus_notifier, value, dev); | |
913 | } | |
914 | ||
0fed80f7 GKH |
915 | struct kset *bus_get_kset(struct bus_type *bus) |
916 | { | |
c6f7e72a | 917 | return &bus->p->subsys; |
0fed80f7 GKH |
918 | } |
919 | EXPORT_SYMBOL_GPL(bus_get_kset); | |
920 | ||
8afbb427 | 921 | static struct klist *bus_get_device_klist(struct bus_type *bus) |
b249072e | 922 | { |
c6f7e72a | 923 | return &bus->p->klist_devices; |
b249072e | 924 | } |
b249072e | 925 | |
99178b03 | 926 | /* |
dca25ebd | 927 | * Yes, this forcibly breaks the klist abstraction temporarily. It |
99178b03 GKH |
928 | * just wants to sort the klist, not change reference counts and |
929 | * take/drop locks rapidly in the process. It does all this while | |
930 | * holding the lock for the list, so objects can't otherwise be | |
931 | * added/removed while we're swizzling. | |
932 | */ | |
933 | static void device_insertion_sort_klist(struct device *a, struct list_head *list, | |
934 | int (*compare)(const struct device *a, | |
935 | const struct device *b)) | |
936 | { | |
99178b03 | 937 | struct klist_node *n; |
ae1b4171 | 938 | struct device_private *dev_prv; |
99178b03 GKH |
939 | struct device *b; |
940 | ||
4c62785e | 941 | list_for_each_entry(n, list, n_node) { |
ae1b4171 GKH |
942 | dev_prv = to_device_private_bus(n); |
943 | b = dev_prv->device; | |
99178b03 | 944 | if (compare(a, b) <= 0) { |
ae1b4171 GKH |
945 | list_move_tail(&a->p->knode_bus.n_node, |
946 | &b->p->knode_bus.n_node); | |
99178b03 GKH |
947 | return; |
948 | } | |
949 | } | |
ae1b4171 | 950 | list_move_tail(&a->p->knode_bus.n_node, list); |
99178b03 GKH |
951 | } |
952 | ||
953 | void bus_sort_breadthfirst(struct bus_type *bus, | |
954 | int (*compare)(const struct device *a, | |
955 | const struct device *b)) | |
956 | { | |
957 | LIST_HEAD(sorted_devices); | |
4c62785e | 958 | struct klist_node *n, *tmp; |
ae1b4171 | 959 | struct device_private *dev_prv; |
99178b03 GKH |
960 | struct device *dev; |
961 | struct klist *device_klist; | |
962 | ||
963 | device_klist = bus_get_device_klist(bus); | |
964 | ||
965 | spin_lock(&device_klist->k_lock); | |
4c62785e | 966 | list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) { |
ae1b4171 GKH |
967 | dev_prv = to_device_private_bus(n); |
968 | dev = dev_prv->device; | |
99178b03 GKH |
969 | device_insertion_sort_klist(dev, &sorted_devices, compare); |
970 | } | |
971 | list_splice(&sorted_devices, &device_klist->k_list); | |
972 | spin_unlock(&device_klist->k_lock); | |
973 | } | |
974 | EXPORT_SYMBOL_GPL(bus_sort_breadthfirst); | |
975 | ||
b0a8a59a GKH |
976 | struct subsys_dev_iter { |
977 | struct klist_iter ki; | |
978 | const struct device_type *type; | |
979 | }; | |
980 | ||
ca22e56d KS |
981 | /** |
982 | * subsys_dev_iter_init - initialize subsys device iterator | |
983 | * @iter: subsys iterator to initialize | |
984 | * @subsys: the subsys we wanna iterate over | |
985 | * @start: the device to start iterating from, if any | |
986 | * @type: device_type of the devices to iterate over, NULL for all | |
987 | * | |
988 | * Initialize subsys iterator @iter such that it iterates over devices | |
989 | * of @subsys. If @start is set, the list iteration will start there, | |
990 | * otherwise if it is NULL, the iteration starts at the beginning of | |
991 | * the list. | |
992 | */ | |
2e45fc55 GKH |
993 | static void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys, |
994 | struct device *start, const struct device_type *type) | |
ca22e56d KS |
995 | { |
996 | struct klist_node *start_knode = NULL; | |
997 | ||
998 | if (start) | |
999 | start_knode = &start->p->knode_bus; | |
7cd9c9bb GKH |
1000 | klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode); |
1001 | iter->type = type; | |
ca22e56d | 1002 | } |
ca22e56d KS |
1003 | |
1004 | /** | |
1005 | * subsys_dev_iter_next - iterate to the next device | |
1006 | * @iter: subsys iterator to proceed | |
1007 | * | |
1008 | * Proceed @iter to the next device and return it. Returns NULL if | |
1009 | * iteration is complete. | |
1010 | * | |
1011 | * The returned device is referenced and won't be released till | |
1012 | * iterator is proceed to the next device or exited. The caller is | |
1013 | * free to do whatever it wants to do with the device including | |
1014 | * calling back into subsys code. | |
1015 | */ | |
38cdadef | 1016 | static struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter) |
ca22e56d KS |
1017 | { |
1018 | struct klist_node *knode; | |
1019 | struct device *dev; | |
1020 | ||
1021 | for (;;) { | |
1022 | knode = klist_next(&iter->ki); | |
1023 | if (!knode) | |
1024 | return NULL; | |
371fd7a2 | 1025 | dev = to_device_private_bus(knode)->device; |
ca22e56d KS |
1026 | if (!iter->type || iter->type == dev->type) |
1027 | return dev; | |
1028 | } | |
1029 | } | |
ca22e56d KS |
1030 | |
1031 | /** | |
1032 | * subsys_dev_iter_exit - finish iteration | |
1033 | * @iter: subsys iterator to finish | |
1034 | * | |
1035 | * Finish an iteration. Always call this function after iteration is | |
1036 | * complete whether the iteration ran till the end or not. | |
1037 | */ | |
af6d0743 | 1038 | static void subsys_dev_iter_exit(struct subsys_dev_iter *iter) |
ca22e56d KS |
1039 | { |
1040 | klist_iter_exit(&iter->ki); | |
1041 | } | |
ca22e56d KS |
1042 | |
1043 | int subsys_interface_register(struct subsys_interface *sif) | |
1044 | { | |
1045 | struct bus_type *subsys; | |
1046 | struct subsys_dev_iter iter; | |
1047 | struct device *dev; | |
1048 | ||
1049 | if (!sif || !sif->subsys) | |
1050 | return -ENODEV; | |
1051 | ||
1052 | subsys = bus_get(sif->subsys); | |
1053 | if (!subsys) | |
1054 | return -EINVAL; | |
1055 | ||
1056 | mutex_lock(&subsys->p->mutex); | |
1057 | list_add_tail(&sif->node, &subsys->p->interfaces); | |
1058 | if (sif->add_dev) { | |
1059 | subsys_dev_iter_init(&iter, subsys, NULL, NULL); | |
1060 | while ((dev = subsys_dev_iter_next(&iter))) | |
1061 | sif->add_dev(dev, sif); | |
1062 | subsys_dev_iter_exit(&iter); | |
1063 | } | |
1064 | mutex_unlock(&subsys->p->mutex); | |
1065 | ||
1066 | return 0; | |
1067 | } | |
1068 | EXPORT_SYMBOL_GPL(subsys_interface_register); | |
1069 | ||
1070 | void subsys_interface_unregister(struct subsys_interface *sif) | |
1071 | { | |
2b31594a | 1072 | struct bus_type *subsys; |
ca22e56d KS |
1073 | struct subsys_dev_iter iter; |
1074 | struct device *dev; | |
1075 | ||
2b31594a | 1076 | if (!sif || !sif->subsys) |
ca22e56d KS |
1077 | return; |
1078 | ||
2b31594a JC |
1079 | subsys = sif->subsys; |
1080 | ||
ca22e56d KS |
1081 | mutex_lock(&subsys->p->mutex); |
1082 | list_del_init(&sif->node); | |
1083 | if (sif->remove_dev) { | |
1084 | subsys_dev_iter_init(&iter, subsys, NULL, NULL); | |
1085 | while ((dev = subsys_dev_iter_next(&iter))) | |
1086 | sif->remove_dev(dev, sif); | |
1087 | subsys_dev_iter_exit(&iter); | |
1088 | } | |
1089 | mutex_unlock(&subsys->p->mutex); | |
1090 | ||
1091 | bus_put(subsys); | |
1092 | } | |
1093 | EXPORT_SYMBOL_GPL(subsys_interface_unregister); | |
1094 | ||
1095 | static void system_root_device_release(struct device *dev) | |
1096 | { | |
1097 | kfree(dev); | |
1098 | } | |
d73ce004 TH |
1099 | |
1100 | static int subsys_register(struct bus_type *subsys, | |
1101 | const struct attribute_group **groups, | |
1102 | struct kobject *parent_of_root) | |
ca22e56d KS |
1103 | { |
1104 | struct device *dev; | |
1105 | int err; | |
1106 | ||
1107 | err = bus_register(subsys); | |
1108 | if (err < 0) | |
1109 | return err; | |
1110 | ||
1111 | dev = kzalloc(sizeof(struct device), GFP_KERNEL); | |
1112 | if (!dev) { | |
1113 | err = -ENOMEM; | |
1114 | goto err_dev; | |
1115 | } | |
1116 | ||
1117 | err = dev_set_name(dev, "%s", subsys->name); | |
1118 | if (err < 0) | |
1119 | goto err_name; | |
1120 | ||
d73ce004 | 1121 | dev->kobj.parent = parent_of_root; |
ca22e56d KS |
1122 | dev->groups = groups; |
1123 | dev->release = system_root_device_release; | |
1124 | ||
1125 | err = device_register(dev); | |
1126 | if (err < 0) | |
1127 | goto err_dev_reg; | |
1128 | ||
1129 | subsys->dev_root = dev; | |
1130 | return 0; | |
1131 | ||
1132 | err_dev_reg: | |
1133 | put_device(dev); | |
1134 | dev = NULL; | |
1135 | err_name: | |
1136 | kfree(dev); | |
1137 | err_dev: | |
1138 | bus_unregister(subsys); | |
1139 | return err; | |
1140 | } | |
d73ce004 TH |
1141 | |
1142 | /** | |
1143 | * subsys_system_register - register a subsystem at /sys/devices/system/ | |
1144 | * @subsys: system subsystem | |
1145 | * @groups: default attributes for the root device | |
1146 | * | |
1147 | * All 'system' subsystems have a /sys/devices/system/<name> root device | |
1148 | * with the name of the subsystem. The root device can carry subsystem- | |
1149 | * wide attributes. All registered devices are below this single root | |
1150 | * device and are named after the subsystem with a simple enumeration | |
e227867f | 1151 | * number appended. The registered devices are not explicitly named; |
d73ce004 TH |
1152 | * only 'id' in the device needs to be set. |
1153 | * | |
1154 | * Do not use this interface for anything new, it exists for compatibility | |
1155 | * with bad ideas only. New subsystems should use plain subsystems; and | |
1156 | * add the subsystem-wide attributes should be added to the subsystem | |
1157 | * directory itself and not some create fake root-device placed in | |
1158 | * /sys/devices/system/<name>. | |
1159 | */ | |
1160 | int subsys_system_register(struct bus_type *subsys, | |
1161 | const struct attribute_group **groups) | |
1162 | { | |
1163 | return subsys_register(subsys, groups, &system_kset->kobj); | |
1164 | } | |
ca22e56d KS |
1165 | EXPORT_SYMBOL_GPL(subsys_system_register); |
1166 | ||
d73ce004 TH |
1167 | /** |
1168 | * subsys_virtual_register - register a subsystem at /sys/devices/virtual/ | |
1169 | * @subsys: virtual subsystem | |
1170 | * @groups: default attributes for the root device | |
1171 | * | |
1172 | * All 'virtual' subsystems have a /sys/devices/system/<name> root device | |
1173 | * with the name of the subystem. The root device can carry subsystem-wide | |
1174 | * attributes. All registered devices are below this single root device. | |
1175 | * There's no restriction on device naming. This is for kernel software | |
1176 | * constructs which need sysfs interface. | |
1177 | */ | |
1178 | int subsys_virtual_register(struct bus_type *subsys, | |
1179 | const struct attribute_group **groups) | |
1180 | { | |
1181 | struct kobject *virtual_dir; | |
1182 | ||
1183 | virtual_dir = virtual_device_parent(NULL); | |
1184 | if (!virtual_dir) | |
1185 | return -ENOMEM; | |
1186 | ||
1187 | return subsys_register(subsys, groups, virtual_dir); | |
1188 | } | |
1c04fc35 | 1189 | EXPORT_SYMBOL_GPL(subsys_virtual_register); |
d73ce004 | 1190 | |
1da177e4 LT |
1191 | int __init buses_init(void) |
1192 | { | |
59a54833 GKH |
1193 | bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL); |
1194 | if (!bus_kset) | |
1195 | return -ENOMEM; | |
ca22e56d KS |
1196 | |
1197 | system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj); | |
1198 | if (!system_kset) | |
1199 | return -ENOMEM; | |
1200 | ||
59a54833 | 1201 | return 0; |
1da177e4 | 1202 | } |