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