]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * bus.c - bus driver management | |
3 | * | |
4 | * Copyright (c) 2002-3 Patrick Mochel | |
5 | * Copyright (c) 2002-3 Open Source Development Labs | |
6 | * | |
7 | * This file is released under the GPLv2 | |
8 | * | |
9 | */ | |
10 | ||
1da177e4 LT |
11 | #include <linux/device.h> |
12 | #include <linux/module.h> | |
13 | #include <linux/errno.h> | |
14 | #include <linux/init.h> | |
15 | #include <linux/string.h> | |
16 | #include "base.h" | |
17 | #include "power/power.h" | |
18 | ||
1da177e4 | 19 | #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr) |
823bccfc | 20 | #define to_bus(obj) container_of(obj, struct bus_type, subsys.kobj) |
1da177e4 LT |
21 | |
22 | /* | |
23 | * sysfs bindings for drivers | |
24 | */ | |
25 | ||
26 | #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr) | |
27 | #define to_driver(obj) container_of(obj, struct device_driver, kobj) | |
28 | ||
29 | ||
b8c5cec2 KS |
30 | static int __must_check bus_rescan_devices_helper(struct device *dev, |
31 | void *data); | |
32 | ||
1da177e4 LT |
33 | static ssize_t |
34 | drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) | |
35 | { | |
36 | struct driver_attribute * drv_attr = to_drv_attr(attr); | |
37 | struct device_driver * drv = to_driver(kobj); | |
4a0c20bf | 38 | ssize_t ret = -EIO; |
1da177e4 LT |
39 | |
40 | if (drv_attr->show) | |
41 | ret = drv_attr->show(drv, buf); | |
42 | return ret; | |
43 | } | |
44 | ||
45 | static ssize_t | |
46 | drv_attr_store(struct kobject * kobj, struct attribute * attr, | |
47 | const char * buf, size_t count) | |
48 | { | |
49 | struct driver_attribute * drv_attr = to_drv_attr(attr); | |
50 | struct device_driver * drv = to_driver(kobj); | |
4a0c20bf | 51 | ssize_t ret = -EIO; |
1da177e4 LT |
52 | |
53 | if (drv_attr->store) | |
54 | ret = drv_attr->store(drv, buf, count); | |
55 | return ret; | |
56 | } | |
57 | ||
58 | static struct sysfs_ops driver_sysfs_ops = { | |
59 | .show = drv_attr_show, | |
60 | .store = drv_attr_store, | |
61 | }; | |
62 | ||
63 | ||
64 | static void driver_release(struct kobject * kobj) | |
65 | { | |
74e9f5fa GKH |
66 | /* |
67 | * Yes this is an empty release function, it is this way because struct | |
68 | * device is always a static object, not a dynamic one. Yes, this is | |
69 | * not nice and bad, but remember, drivers are code, reference counted | |
70 | * by the module count, not a device, which is really data. And yes, | |
71 | * in the future I do want to have all drivers be created dynamically, | |
72 | * and am working toward that goal, but it will take a bit longer... | |
73 | * | |
74 | * But do not let this example give _anyone_ the idea that they can | |
75 | * create a release function without any code in it at all, to do that | |
76 | * is almost always wrong. If you have any questions about this, | |
77 | * please send an email to <greg@kroah.com> | |
78 | */ | |
1da177e4 LT |
79 | } |
80 | ||
81 | static struct kobj_type ktype_driver = { | |
82 | .sysfs_ops = &driver_sysfs_ops, | |
83 | .release = driver_release, | |
84 | }; | |
85 | ||
86 | ||
87 | /* | |
88 | * sysfs bindings for buses | |
89 | */ | |
90 | ||
91 | ||
92 | static ssize_t | |
93 | bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) | |
94 | { | |
95 | struct bus_attribute * bus_attr = to_bus_attr(attr); | |
96 | struct bus_type * bus = to_bus(kobj); | |
97 | ssize_t ret = 0; | |
98 | ||
99 | if (bus_attr->show) | |
100 | ret = bus_attr->show(bus, buf); | |
101 | return ret; | |
102 | } | |
103 | ||
104 | static ssize_t | |
105 | bus_attr_store(struct kobject * kobj, struct attribute * attr, | |
106 | const char * buf, size_t count) | |
107 | { | |
108 | struct bus_attribute * bus_attr = to_bus_attr(attr); | |
109 | struct bus_type * bus = to_bus(kobj); | |
110 | ssize_t ret = 0; | |
111 | ||
112 | if (bus_attr->store) | |
113 | ret = bus_attr->store(bus, buf, count); | |
114 | return ret; | |
115 | } | |
116 | ||
117 | static struct sysfs_ops bus_sysfs_ops = { | |
118 | .show = bus_attr_show, | |
119 | .store = bus_attr_store, | |
120 | }; | |
121 | ||
122 | int bus_create_file(struct bus_type * bus, struct bus_attribute * attr) | |
123 | { | |
124 | int error; | |
125 | if (get_bus(bus)) { | |
823bccfc | 126 | error = sysfs_create_file(&bus->subsys.kobj, &attr->attr); |
1da177e4 LT |
127 | put_bus(bus); |
128 | } else | |
129 | error = -EINVAL; | |
130 | return error; | |
131 | } | |
132 | ||
133 | void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr) | |
134 | { | |
135 | if (get_bus(bus)) { | |
823bccfc | 136 | sysfs_remove_file(&bus->subsys.kobj, &attr->attr); |
1da177e4 LT |
137 | put_bus(bus); |
138 | } | |
139 | } | |
140 | ||
80f03e34 | 141 | static struct kobj_type bus_ktype = { |
1da177e4 | 142 | .sysfs_ops = &bus_sysfs_ops, |
80f03e34 KS |
143 | }; |
144 | ||
145 | static int bus_uevent_filter(struct kset *kset, struct kobject *kobj) | |
146 | { | |
147 | struct kobj_type *ktype = get_ktype(kobj); | |
148 | ||
149 | if (ktype == &bus_ktype) | |
150 | return 1; | |
151 | return 0; | |
152 | } | |
1da177e4 | 153 | |
80f03e34 KS |
154 | static struct kset_uevent_ops bus_uevent_ops = { |
155 | .filter = bus_uevent_filter, | |
1da177e4 LT |
156 | }; |
157 | ||
80f03e34 | 158 | static decl_subsys(bus, &bus_ktype, &bus_uevent_ops); |
1da177e4 | 159 | |
1da177e4 | 160 | |
2139bdd5 | 161 | #ifdef CONFIG_HOTPLUG |
2b08c8d0 | 162 | /* Manually detach a device from its associated driver. */ |
151ef38f GKH |
163 | static int driver_helper(struct device *dev, void *data) |
164 | { | |
165 | const char *name = data; | |
166 | ||
167 | if (strcmp(name, dev->bus_id) == 0) | |
168 | return 1; | |
169 | return 0; | |
170 | } | |
171 | ||
172 | static ssize_t driver_unbind(struct device_driver *drv, | |
173 | const char *buf, size_t count) | |
174 | { | |
175 | struct bus_type *bus = get_bus(drv->bus); | |
176 | struct device *dev; | |
177 | int err = -ENODEV; | |
178 | ||
179 | dev = bus_find_device(bus, NULL, (void *)buf, driver_helper); | |
2b08c8d0 | 180 | if (dev && dev->driver == drv) { |
bf74ad5b AS |
181 | if (dev->parent) /* Needed for USB */ |
182 | down(&dev->parent->sem); | |
151ef38f | 183 | device_release_driver(dev); |
bf74ad5b AS |
184 | if (dev->parent) |
185 | up(&dev->parent->sem); | |
151ef38f GKH |
186 | err = count; |
187 | } | |
2b08c8d0 AS |
188 | put_device(dev); |
189 | put_bus(bus); | |
190 | return err; | |
151ef38f GKH |
191 | } |
192 | static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind); | |
193 | ||
afdce75f GKH |
194 | /* |
195 | * Manually attach a device to a driver. | |
196 | * Note: the driver must want to bind to the device, | |
197 | * it is not possible to override the driver's id table. | |
198 | */ | |
199 | static ssize_t driver_bind(struct device_driver *drv, | |
200 | const char *buf, size_t count) | |
201 | { | |
202 | struct bus_type *bus = get_bus(drv->bus); | |
203 | struct device *dev; | |
204 | int err = -ENODEV; | |
205 | ||
206 | dev = bus_find_device(bus, NULL, (void *)buf, driver_helper); | |
2b08c8d0 | 207 | if (dev && dev->driver == NULL) { |
bf74ad5b AS |
208 | if (dev->parent) /* Needed for USB */ |
209 | down(&dev->parent->sem); | |
afdce75f GKH |
210 | down(&dev->sem); |
211 | err = driver_probe_device(drv, dev); | |
212 | up(&dev->sem); | |
bf74ad5b AS |
213 | if (dev->parent) |
214 | up(&dev->parent->sem); | |
37225401 RW |
215 | |
216 | if (err > 0) /* success */ | |
217 | err = count; | |
218 | else if (err == 0) /* driver didn't accept device */ | |
219 | err = -ENODEV; | |
afdce75f | 220 | } |
2b08c8d0 AS |
221 | put_device(dev); |
222 | put_bus(bus); | |
223 | return err; | |
afdce75f GKH |
224 | } |
225 | static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind); | |
226 | ||
b8c5cec2 KS |
227 | static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf) |
228 | { | |
229 | return sprintf(buf, "%d\n", bus->drivers_autoprobe); | |
230 | } | |
231 | ||
232 | static ssize_t store_drivers_autoprobe(struct bus_type *bus, | |
233 | const char *buf, size_t count) | |
234 | { | |
235 | if (buf[0] == '0') | |
236 | bus->drivers_autoprobe = 0; | |
237 | else | |
238 | bus->drivers_autoprobe = 1; | |
239 | return count; | |
240 | } | |
241 | ||
242 | static ssize_t store_drivers_probe(struct bus_type *bus, | |
243 | const char *buf, size_t count) | |
244 | { | |
245 | struct device *dev; | |
246 | ||
247 | dev = bus_find_device(bus, NULL, (void *)buf, driver_helper); | |
248 | if (!dev) | |
249 | return -ENODEV; | |
250 | if (bus_rescan_devices_helper(dev, NULL) != 0) | |
251 | return -EINVAL; | |
252 | return count; | |
253 | } | |
2139bdd5 | 254 | #endif |
151ef38f | 255 | |
465c7a3a PM |
256 | static struct device * next_device(struct klist_iter * i) |
257 | { | |
258 | struct klist_node * n = klist_next(i); | |
259 | return n ? container_of(n, struct device, knode_bus) : NULL; | |
260 | } | |
261 | ||
1da177e4 LT |
262 | /** |
263 | * bus_for_each_dev - device iterator. | |
264 | * @bus: bus type. | |
265 | * @start: device to start iterating from. | |
266 | * @data: data for the callback. | |
267 | * @fn: function to be called for each device. | |
268 | * | |
269 | * Iterate over @bus's list of devices, and call @fn for each, | |
270 | * passing it @data. If @start is not NULL, we use that device to | |
271 | * begin iterating from. | |
272 | * | |
273 | * We check the return of @fn each time. If it returns anything | |
274 | * other than 0, we break out and return that value. | |
275 | * | |
276 | * NOTE: The device that returns a non-zero value is not retained | |
277 | * in any way, nor is its refcount incremented. If the caller needs | |
278 | * to retain this data, it should do, and increment the reference | |
279 | * count in the supplied callback. | |
280 | */ | |
281 | ||
282 | int bus_for_each_dev(struct bus_type * bus, struct device * start, | |
283 | void * data, int (*fn)(struct device *, void *)) | |
284 | { | |
465c7a3a PM |
285 | struct klist_iter i; |
286 | struct device * dev; | |
287 | int error = 0; | |
1da177e4 | 288 | |
465c7a3a PM |
289 | if (!bus) |
290 | return -EINVAL; | |
291 | ||
292 | klist_iter_init_node(&bus->klist_devices, &i, | |
293 | (start ? &start->knode_bus : NULL)); | |
294 | while ((dev = next_device(&i)) && !error) | |
295 | error = fn(dev, data); | |
296 | klist_iter_exit(&i); | |
297 | return error; | |
1da177e4 LT |
298 | } |
299 | ||
0edb5860 CH |
300 | /** |
301 | * bus_find_device - device iterator for locating a particular device. | |
302 | * @bus: bus type | |
303 | * @start: Device to begin with | |
304 | * @data: Data to pass to match function | |
305 | * @match: Callback function to check device | |
306 | * | |
307 | * This is similar to the bus_for_each_dev() function above, but it | |
308 | * returns a reference to a device that is 'found' for later use, as | |
309 | * determined by the @match callback. | |
310 | * | |
311 | * The callback should return 0 if the device doesn't match and non-zero | |
312 | * if it does. If the callback returns non-zero, this function will | |
313 | * return to the caller and not iterate over any more devices. | |
314 | */ | |
315 | struct device * bus_find_device(struct bus_type *bus, | |
316 | struct device *start, void *data, | |
317 | int (*match)(struct device *, void *)) | |
318 | { | |
319 | struct klist_iter i; | |
320 | struct device *dev; | |
321 | ||
322 | if (!bus) | |
323 | return NULL; | |
324 | ||
325 | klist_iter_init_node(&bus->klist_devices, &i, | |
326 | (start ? &start->knode_bus : NULL)); | |
327 | while ((dev = next_device(&i))) | |
328 | if (match(dev, data) && get_device(dev)) | |
329 | break; | |
330 | klist_iter_exit(&i); | |
331 | return dev; | |
332 | } | |
38fdac3c PM |
333 | |
334 | ||
335 | static struct device_driver * next_driver(struct klist_iter * i) | |
336 | { | |
337 | struct klist_node * n = klist_next(i); | |
338 | return n ? container_of(n, struct device_driver, knode_bus) : NULL; | |
339 | } | |
340 | ||
1da177e4 LT |
341 | /** |
342 | * bus_for_each_drv - driver iterator | |
343 | * @bus: bus we're dealing with. | |
344 | * @start: driver to start iterating on. | |
345 | * @data: data to pass to the callback. | |
346 | * @fn: function to call for each driver. | |
347 | * | |
348 | * This is nearly identical to the device iterator above. | |
349 | * We iterate over each driver that belongs to @bus, and call | |
350 | * @fn for each. If @fn returns anything but 0, we break out | |
351 | * and return it. If @start is not NULL, we use it as the head | |
352 | * of the list. | |
353 | * | |
354 | * NOTE: we don't return the driver that returns a non-zero | |
355 | * value, nor do we leave the reference count incremented for that | |
356 | * driver. If the caller needs to know that info, it must set it | |
357 | * in the callback. It must also be sure to increment the refcount | |
358 | * so it doesn't disappear before returning to the caller. | |
359 | */ | |
360 | ||
361 | int bus_for_each_drv(struct bus_type * bus, struct device_driver * start, | |
362 | void * data, int (*fn)(struct device_driver *, void *)) | |
363 | { | |
38fdac3c PM |
364 | struct klist_iter i; |
365 | struct device_driver * drv; | |
366 | int error = 0; | |
1da177e4 | 367 | |
38fdac3c PM |
368 | if (!bus) |
369 | return -EINVAL; | |
370 | ||
371 | klist_iter_init_node(&bus->klist_drivers, &i, | |
372 | start ? &start->knode_bus : NULL); | |
373 | while ((drv = next_driver(&i)) && !error) | |
374 | error = fn(drv, data); | |
375 | klist_iter_exit(&i); | |
376 | return error; | |
1da177e4 LT |
377 | } |
378 | ||
4aca67e5 | 379 | static int device_add_attrs(struct bus_type *bus, struct device *dev) |
1da177e4 LT |
380 | { |
381 | int error = 0; | |
382 | int i; | |
383 | ||
4aca67e5 AM |
384 | if (!bus->dev_attrs) |
385 | return 0; | |
386 | ||
387 | for (i = 0; attr_name(bus->dev_attrs[i]); i++) { | |
388 | error = device_create_file(dev,&bus->dev_attrs[i]); | |
389 | if (error) { | |
390 | while (--i >= 0) | |
391 | device_remove_file(dev, &bus->dev_attrs[i]); | |
392 | break; | |
1da177e4 LT |
393 | } |
394 | } | |
1da177e4 | 395 | return error; |
1da177e4 LT |
396 | } |
397 | ||
1da177e4 LT |
398 | static void device_remove_attrs(struct bus_type * bus, struct device * dev) |
399 | { | |
400 | int i; | |
401 | ||
402 | if (bus->dev_attrs) { | |
403 | for (i = 0; attr_name(bus->dev_attrs[i]); i++) | |
404 | device_remove_file(dev,&bus->dev_attrs[i]); | |
405 | } | |
406 | } | |
407 | ||
b9cafc7d KS |
408 | #ifdef CONFIG_SYSFS_DEPRECATED |
409 | static int make_deprecated_bus_links(struct device *dev) | |
410 | { | |
411 | return sysfs_create_link(&dev->kobj, | |
823bccfc | 412 | &dev->bus->subsys.kobj, "bus"); |
b9cafc7d KS |
413 | } |
414 | ||
415 | static void remove_deprecated_bus_links(struct device *dev) | |
416 | { | |
417 | sysfs_remove_link(&dev->kobj, "bus"); | |
418 | } | |
419 | #else | |
420 | static inline int make_deprecated_bus_links(struct device *dev) { return 0; } | |
421 | static inline void remove_deprecated_bus_links(struct device *dev) { } | |
422 | #endif | |
1da177e4 LT |
423 | |
424 | /** | |
425 | * bus_add_device - add device to bus | |
426 | * @dev: device being added | |
427 | * | |
428 | * - Add the device to its bus's list of devices. | |
53877d06 | 429 | * - Create link to device's bus. |
1da177e4 LT |
430 | */ |
431 | int bus_add_device(struct device * dev) | |
432 | { | |
433 | struct bus_type * bus = get_bus(dev->bus); | |
434 | int error = 0; | |
435 | ||
436 | if (bus) { | |
1da177e4 | 437 | pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id); |
d377e85b | 438 | error = device_add_attrs(bus, dev); |
f86db396 | 439 | if (error) |
513e7337 | 440 | goto out_put; |
f86db396 AM |
441 | error = sysfs_create_link(&bus->devices.kobj, |
442 | &dev->kobj, dev->bus_id); | |
443 | if (error) | |
513e7337 | 444 | goto out_id; |
f86db396 | 445 | error = sysfs_create_link(&dev->kobj, |
823bccfc | 446 | &dev->bus->subsys.kobj, "subsystem"); |
f86db396 | 447 | if (error) |
513e7337 | 448 | goto out_subsys; |
b9cafc7d | 449 | error = make_deprecated_bus_links(dev); |
513e7337 CH |
450 | if (error) |
451 | goto out_deprecated; | |
1da177e4 | 452 | } |
513e7337 CH |
453 | return 0; |
454 | ||
455 | out_deprecated: | |
456 | sysfs_remove_link(&dev->kobj, "subsystem"); | |
457 | out_subsys: | |
458 | sysfs_remove_link(&bus->devices.kobj, dev->bus_id); | |
459 | out_id: | |
460 | device_remove_attrs(bus, dev); | |
461 | out_put: | |
462 | put_bus(dev->bus); | |
1da177e4 LT |
463 | return error; |
464 | } | |
465 | ||
53877d06 KS |
466 | /** |
467 | * bus_attach_device - add device to bus | |
468 | * @dev: device tried to attach to a driver | |
469 | * | |
f2eaae19 | 470 | * - Add device to bus's list of devices. |
53877d06 KS |
471 | * - Try to attach to driver. |
472 | */ | |
c6a46696 | 473 | void bus_attach_device(struct device * dev) |
53877d06 | 474 | { |
f86db396 AM |
475 | struct bus_type *bus = dev->bus; |
476 | int ret = 0; | |
53877d06 KS |
477 | |
478 | if (bus) { | |
f2eaae19 | 479 | dev->is_registered = 1; |
b8c5cec2 KS |
480 | if (bus->drivers_autoprobe) |
481 | ret = device_attach(dev); | |
c6a46696 CH |
482 | WARN_ON(ret < 0); |
483 | if (ret >= 0) | |
f86db396 | 484 | klist_add_tail(&dev->knode_bus, &bus->klist_devices); |
c6a46696 | 485 | else |
f2eaae19 | 486 | dev->is_registered = 0; |
53877d06 KS |
487 | } |
488 | } | |
489 | ||
1da177e4 LT |
490 | /** |
491 | * bus_remove_device - remove device from bus | |
492 | * @dev: device to be removed | |
493 | * | |
494 | * - Remove symlink from bus's directory. | |
495 | * - Delete device from bus's list. | |
496 | * - Detach from its driver. | |
497 | * - Drop reference taken in bus_add_device(). | |
498 | */ | |
499 | void bus_remove_device(struct device * dev) | |
500 | { | |
501 | if (dev->bus) { | |
b9d9c82b | 502 | sysfs_remove_link(&dev->kobj, "subsystem"); |
b9cafc7d | 503 | remove_deprecated_bus_links(dev); |
1da177e4 LT |
504 | sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id); |
505 | device_remove_attrs(dev->bus, dev); | |
f70fa629 AS |
506 | if (dev->is_registered) { |
507 | dev->is_registered = 0; | |
508 | klist_del(&dev->knode_bus); | |
509 | } | |
1da177e4 LT |
510 | pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id); |
511 | device_release_driver(dev); | |
1da177e4 LT |
512 | put_bus(dev->bus); |
513 | } | |
514 | } | |
515 | ||
516 | static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv) | |
517 | { | |
518 | int error = 0; | |
519 | int i; | |
520 | ||
521 | if (bus->drv_attrs) { | |
522 | for (i = 0; attr_name(bus->drv_attrs[i]); i++) { | |
523 | error = driver_create_file(drv, &bus->drv_attrs[i]); | |
524 | if (error) | |
525 | goto Err; | |
526 | } | |
527 | } | |
528 | Done: | |
529 | return error; | |
530 | Err: | |
531 | while (--i >= 0) | |
532 | driver_remove_file(drv, &bus->drv_attrs[i]); | |
533 | goto Done; | |
534 | } | |
535 | ||
536 | ||
537 | static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv) | |
538 | { | |
539 | int i; | |
540 | ||
541 | if (bus->drv_attrs) { | |
542 | for (i = 0; attr_name(bus->drv_attrs[i]); i++) | |
543 | driver_remove_file(drv, &bus->drv_attrs[i]); | |
544 | } | |
545 | } | |
546 | ||
874c6241 GKH |
547 | #ifdef CONFIG_HOTPLUG |
548 | /* | |
549 | * Thanks to drivers making their tables __devinit, we can't allow manual | |
550 | * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled. | |
551 | */ | |
f86db396 | 552 | static int __must_check add_bind_files(struct device_driver *drv) |
874c6241 | 553 | { |
f86db396 AM |
554 | int ret; |
555 | ||
556 | ret = driver_create_file(drv, &driver_attr_unbind); | |
557 | if (ret == 0) { | |
558 | ret = driver_create_file(drv, &driver_attr_bind); | |
559 | if (ret) | |
560 | driver_remove_file(drv, &driver_attr_unbind); | |
561 | } | |
562 | return ret; | |
874c6241 GKH |
563 | } |
564 | ||
565 | static void remove_bind_files(struct device_driver *drv) | |
566 | { | |
567 | driver_remove_file(drv, &driver_attr_bind); | |
568 | driver_remove_file(drv, &driver_attr_unbind); | |
569 | } | |
b8c5cec2 KS |
570 | |
571 | static int add_probe_files(struct bus_type *bus) | |
572 | { | |
573 | int retval; | |
574 | ||
575 | bus->drivers_probe_attr.attr.name = "drivers_probe"; | |
576 | bus->drivers_probe_attr.attr.mode = S_IWUSR; | |
b8c5cec2 KS |
577 | bus->drivers_probe_attr.store = store_drivers_probe; |
578 | retval = bus_create_file(bus, &bus->drivers_probe_attr); | |
579 | if (retval) | |
580 | goto out; | |
581 | ||
582 | bus->drivers_autoprobe_attr.attr.name = "drivers_autoprobe"; | |
583 | bus->drivers_autoprobe_attr.attr.mode = S_IWUSR | S_IRUGO; | |
b8c5cec2 KS |
584 | bus->drivers_autoprobe_attr.show = show_drivers_autoprobe; |
585 | bus->drivers_autoprobe_attr.store = store_drivers_autoprobe; | |
586 | retval = bus_create_file(bus, &bus->drivers_autoprobe_attr); | |
587 | if (retval) | |
588 | bus_remove_file(bus, &bus->drivers_probe_attr); | |
589 | out: | |
590 | return retval; | |
591 | } | |
592 | ||
593 | static void remove_probe_files(struct bus_type *bus) | |
594 | { | |
595 | bus_remove_file(bus, &bus->drivers_autoprobe_attr); | |
596 | bus_remove_file(bus, &bus->drivers_probe_attr); | |
597 | } | |
874c6241 | 598 | #else |
35acfdd7 | 599 | static inline int add_bind_files(struct device_driver *drv) { return 0; } |
874c6241 | 600 | static inline void remove_bind_files(struct device_driver *drv) {} |
b8c5cec2 KS |
601 | static inline int add_probe_files(struct bus_type *bus) { return 0; } |
602 | static inline void remove_probe_files(struct bus_type *bus) {} | |
874c6241 | 603 | #endif |
1da177e4 LT |
604 | |
605 | /** | |
606 | * bus_add_driver - Add a driver to the bus. | |
607 | * @drv: driver. | |
608 | * | |
609 | */ | |
f86db396 | 610 | int bus_add_driver(struct device_driver *drv) |
1da177e4 LT |
611 | { |
612 | struct bus_type * bus = get_bus(drv->bus); | |
613 | int error = 0; | |
614 | ||
d9fd4d3b | 615 | if (!bus) |
4f6e1945 | 616 | return -EINVAL; |
d9fd4d3b JG |
617 | |
618 | pr_debug("bus %s: add driver %s\n", bus->name, drv->name); | |
619 | error = kobject_set_name(&drv->kobj, "%s", drv->name); | |
620 | if (error) | |
621 | goto out_put_bus; | |
622 | drv->kobj.kset = &bus->drivers; | |
dc0afa83 CH |
623 | error = kobject_register(&drv->kobj); |
624 | if (error) | |
d9fd4d3b JG |
625 | goto out_put_bus; |
626 | ||
b8c5cec2 KS |
627 | if (drv->bus->drivers_autoprobe) { |
628 | error = driver_attach(drv); | |
629 | if (error) | |
630 | goto out_unregister; | |
631 | } | |
d9fd4d3b JG |
632 | klist_add_tail(&drv->knode_bus, &bus->klist_drivers); |
633 | module_add_driver(drv->owner, drv); | |
634 | ||
635 | error = driver_add_attrs(bus, drv); | |
636 | if (error) { | |
637 | /* How the hell do we get out of this pickle? Give up */ | |
638 | printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n", | |
639 | __FUNCTION__, drv->name); | |
640 | } | |
641 | error = add_bind_files(drv); | |
642 | if (error) { | |
643 | /* Ditto */ | |
644 | printk(KERN_ERR "%s: add_bind_files(%s) failed\n", | |
645 | __FUNCTION__, drv->name); | |
1da177e4 | 646 | } |
d9fd4d3b | 647 | |
1da177e4 | 648 | return error; |
f86db396 AM |
649 | out_unregister: |
650 | kobject_unregister(&drv->kobj); | |
651 | out_put_bus: | |
652 | put_bus(bus); | |
653 | return error; | |
1da177e4 LT |
654 | } |
655 | ||
1da177e4 LT |
656 | /** |
657 | * bus_remove_driver - delete driver from bus's knowledge. | |
658 | * @drv: driver. | |
659 | * | |
660 | * Detach the driver from the devices it controls, and remove | |
661 | * it from its bus's list of drivers. Finally, we drop the reference | |
662 | * to the bus we took in bus_add_driver(). | |
663 | */ | |
664 | ||
665 | void bus_remove_driver(struct device_driver * drv) | |
666 | { | |
d9fd4d3b JG |
667 | if (!drv->bus) |
668 | return; | |
669 | ||
670 | remove_bind_files(drv); | |
671 | driver_remove_attrs(drv->bus, drv); | |
672 | klist_remove(&drv->knode_bus); | |
673 | pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name); | |
674 | driver_detach(drv); | |
675 | module_remove_driver(drv); | |
676 | kobject_unregister(&drv->kobj); | |
677 | put_bus(drv->bus); | |
1da177e4 LT |
678 | } |
679 | ||
680 | ||
681 | /* Helper for bus_rescan_devices's iter */ | |
f86db396 AM |
682 | static int __must_check bus_rescan_devices_helper(struct device *dev, |
683 | void *data) | |
1da177e4 | 684 | { |
f86db396 AM |
685 | int ret = 0; |
686 | ||
bf74ad5b AS |
687 | if (!dev->driver) { |
688 | if (dev->parent) /* Needed for USB */ | |
689 | down(&dev->parent->sem); | |
f86db396 | 690 | ret = device_attach(dev); |
bf74ad5b AS |
691 | if (dev->parent) |
692 | up(&dev->parent->sem); | |
693 | } | |
f86db396 | 694 | return ret < 0 ? ret : 0; |
1da177e4 LT |
695 | } |
696 | ||
1da177e4 | 697 | /** |
23d3d602 GKH |
698 | * bus_rescan_devices - rescan devices on the bus for possible drivers |
699 | * @bus: the bus to scan. | |
1da177e4 | 700 | * |
23d3d602 GKH |
701 | * This function will look for devices on the bus with no driver |
702 | * attached and rescan it against existing drivers to see if it matches | |
703 | * any by calling device_attach() for the unbound devices. | |
1da177e4 | 704 | */ |
f86db396 | 705 | int bus_rescan_devices(struct bus_type * bus) |
1da177e4 | 706 | { |
f86db396 | 707 | return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper); |
1da177e4 LT |
708 | } |
709 | ||
e935d5da ME |
710 | /** |
711 | * device_reprobe - remove driver for a device and probe for a new driver | |
712 | * @dev: the device to reprobe | |
713 | * | |
714 | * This function detaches the attached driver (if any) for the given | |
715 | * device and restarts the driver probing process. It is intended | |
716 | * to use if probing criteria changed during a devices lifetime and | |
717 | * driver attachment should change accordingly. | |
718 | */ | |
f86db396 | 719 | int device_reprobe(struct device *dev) |
e935d5da ME |
720 | { |
721 | if (dev->driver) { | |
722 | if (dev->parent) /* Needed for USB */ | |
723 | down(&dev->parent->sem); | |
724 | device_release_driver(dev); | |
725 | if (dev->parent) | |
726 | up(&dev->parent->sem); | |
727 | } | |
f86db396 | 728 | return bus_rescan_devices_helper(dev, NULL); |
e935d5da ME |
729 | } |
730 | EXPORT_SYMBOL_GPL(device_reprobe); | |
1da177e4 | 731 | |
f86db396 | 732 | struct bus_type *get_bus(struct bus_type *bus) |
1da177e4 | 733 | { |
f86db396 AM |
734 | return bus ? container_of(subsys_get(&bus->subsys), |
735 | struct bus_type, subsys) : NULL; | |
1da177e4 LT |
736 | } |
737 | ||
738 | void put_bus(struct bus_type * bus) | |
739 | { | |
740 | subsys_put(&bus->subsys); | |
741 | } | |
742 | ||
743 | ||
744 | /** | |
745 | * find_bus - locate bus by name. | |
746 | * @name: name of bus. | |
747 | * | |
748 | * Call kset_find_obj() to iterate over list of buses to | |
749 | * find a bus by name. Return bus if found. | |
750 | * | |
751 | * Note that kset_find_obj increments bus' reference count. | |
752 | */ | |
7e4ef085 | 753 | #if 0 |
1da177e4 LT |
754 | struct bus_type * find_bus(char * name) |
755 | { | |
756 | struct kobject * k = kset_find_obj(&bus_subsys.kset, name); | |
757 | return k ? to_bus(k) : NULL; | |
758 | } | |
7e4ef085 | 759 | #endif /* 0 */ |
1da177e4 LT |
760 | |
761 | ||
762 | /** | |
763 | * bus_add_attrs - Add default attributes for this bus. | |
764 | * @bus: Bus that has just been registered. | |
765 | */ | |
766 | ||
767 | static int bus_add_attrs(struct bus_type * bus) | |
768 | { | |
769 | int error = 0; | |
770 | int i; | |
771 | ||
772 | if (bus->bus_attrs) { | |
773 | for (i = 0; attr_name(bus->bus_attrs[i]); i++) { | |
dc0afa83 CH |
774 | error = bus_create_file(bus,&bus->bus_attrs[i]); |
775 | if (error) | |
1da177e4 LT |
776 | goto Err; |
777 | } | |
778 | } | |
779 | Done: | |
780 | return error; | |
781 | Err: | |
782 | while (--i >= 0) | |
783 | bus_remove_file(bus,&bus->bus_attrs[i]); | |
784 | goto Done; | |
785 | } | |
786 | ||
787 | static void bus_remove_attrs(struct bus_type * bus) | |
788 | { | |
789 | int i; | |
790 | ||
791 | if (bus->bus_attrs) { | |
792 | for (i = 0; attr_name(bus->bus_attrs[i]); i++) | |
793 | bus_remove_file(bus,&bus->bus_attrs[i]); | |
794 | } | |
795 | } | |
796 | ||
34bb61f9 JB |
797 | static void klist_devices_get(struct klist_node *n) |
798 | { | |
799 | struct device *dev = container_of(n, struct device, knode_bus); | |
800 | ||
801 | get_device(dev); | |
802 | } | |
803 | ||
804 | static void klist_devices_put(struct klist_node *n) | |
805 | { | |
806 | struct device *dev = container_of(n, struct device, knode_bus); | |
807 | ||
808 | put_device(dev); | |
809 | } | |
810 | ||
1da177e4 LT |
811 | /** |
812 | * bus_register - register a bus with the system. | |
813 | * @bus: bus. | |
814 | * | |
815 | * Once we have that, we registered the bus with the kobject | |
816 | * infrastructure, then register the children subsystems it has: | |
817 | * the devices and drivers that belong to the bus. | |
818 | */ | |
819 | int bus_register(struct bus_type * bus) | |
820 | { | |
821 | int retval; | |
822 | ||
116af378 BH |
823 | BLOCKING_INIT_NOTIFIER_HEAD(&bus->bus_notifier); |
824 | ||
823bccfc | 825 | retval = kobject_set_name(&bus->subsys.kobj, "%s", bus->name); |
1da177e4 LT |
826 | if (retval) |
827 | goto out; | |
828 | ||
829 | subsys_set_kset(bus, bus_subsys); | |
830 | retval = subsystem_register(&bus->subsys); | |
831 | if (retval) | |
832 | goto out; | |
833 | ||
834 | kobject_set_name(&bus->devices.kobj, "devices"); | |
823bccfc | 835 | bus->devices.kobj.parent = &bus->subsys.kobj; |
1da177e4 LT |
836 | retval = kset_register(&bus->devices); |
837 | if (retval) | |
838 | goto bus_devices_fail; | |
839 | ||
840 | kobject_set_name(&bus->drivers.kobj, "drivers"); | |
823bccfc | 841 | bus->drivers.kobj.parent = &bus->subsys.kobj; |
1da177e4 LT |
842 | bus->drivers.ktype = &ktype_driver; |
843 | retval = kset_register(&bus->drivers); | |
844 | if (retval) | |
845 | goto bus_drivers_fail; | |
465c7a3a | 846 | |
34bb61f9 | 847 | klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put); |
81107bf5 | 848 | klist_init(&bus->klist_drivers, NULL, NULL); |
b8c5cec2 KS |
849 | |
850 | bus->drivers_autoprobe = 1; | |
851 | retval = add_probe_files(bus); | |
852 | if (retval) | |
853 | goto bus_probe_files_fail; | |
854 | ||
1bb6881a CH |
855 | retval = bus_add_attrs(bus); |
856 | if (retval) | |
857 | goto bus_attrs_fail; | |
1da177e4 LT |
858 | |
859 | pr_debug("bus type '%s' registered\n", bus->name); | |
860 | return 0; | |
861 | ||
1bb6881a | 862 | bus_attrs_fail: |
b8c5cec2 KS |
863 | remove_probe_files(bus); |
864 | bus_probe_files_fail: | |
1bb6881a | 865 | kset_unregister(&bus->drivers); |
1da177e4 LT |
866 | bus_drivers_fail: |
867 | kset_unregister(&bus->devices); | |
868 | bus_devices_fail: | |
869 | subsystem_unregister(&bus->subsys); | |
870 | out: | |
871 | return retval; | |
872 | } | |
873 | ||
1da177e4 LT |
874 | /** |
875 | * bus_unregister - remove a bus from the system | |
876 | * @bus: bus. | |
877 | * | |
878 | * Unregister the child subsystems and the bus itself. | |
879 | * Finally, we call put_bus() to release the refcount | |
880 | */ | |
881 | void bus_unregister(struct bus_type * bus) | |
882 | { | |
883 | pr_debug("bus %s: unregistering\n", bus->name); | |
884 | bus_remove_attrs(bus); | |
b8c5cec2 | 885 | remove_probe_files(bus); |
1da177e4 LT |
886 | kset_unregister(&bus->drivers); |
887 | kset_unregister(&bus->devices); | |
888 | subsystem_unregister(&bus->subsys); | |
889 | } | |
890 | ||
116af378 BH |
891 | int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb) |
892 | { | |
893 | return blocking_notifier_chain_register(&bus->bus_notifier, nb); | |
894 | } | |
895 | EXPORT_SYMBOL_GPL(bus_register_notifier); | |
896 | ||
897 | int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb) | |
898 | { | |
899 | return blocking_notifier_chain_unregister(&bus->bus_notifier, nb); | |
900 | } | |
901 | EXPORT_SYMBOL_GPL(bus_unregister_notifier); | |
902 | ||
1da177e4 LT |
903 | int __init buses_init(void) |
904 | { | |
905 | return subsystem_register(&bus_subsys); | |
906 | } | |
907 | ||
908 | ||
909 | EXPORT_SYMBOL_GPL(bus_for_each_dev); | |
0edb5860 | 910 | EXPORT_SYMBOL_GPL(bus_find_device); |
1da177e4 LT |
911 | EXPORT_SYMBOL_GPL(bus_for_each_drv); |
912 | ||
1da177e4 LT |
913 | EXPORT_SYMBOL_GPL(bus_register); |
914 | EXPORT_SYMBOL_GPL(bus_unregister); | |
915 | EXPORT_SYMBOL_GPL(bus_rescan_devices); | |
1da177e4 LT |
916 | |
917 | EXPORT_SYMBOL_GPL(bus_create_file); | |
918 | EXPORT_SYMBOL_GPL(bus_remove_file); |