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 | ||
11 | #include <linux/config.h> | |
12 | #include <linux/device.h> | |
13 | #include <linux/module.h> | |
14 | #include <linux/errno.h> | |
15 | #include <linux/init.h> | |
16 | #include <linux/string.h> | |
17 | #include "base.h" | |
18 | #include "power/power.h" | |
19 | ||
20 | #define to_dev(node) container_of(node, struct device, bus_list) | |
1da177e4 LT |
21 | |
22 | #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr) | |
23 | #define to_bus(obj) container_of(obj, struct bus_type, subsys.kset.kobj) | |
24 | ||
25 | /* | |
26 | * sysfs bindings for drivers | |
27 | */ | |
28 | ||
29 | #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr) | |
30 | #define to_driver(obj) container_of(obj, struct device_driver, kobj) | |
31 | ||
32 | ||
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 | { | |
66 | struct device_driver * drv = to_driver(kobj); | |
67 | complete(&drv->unloaded); | |
68 | } | |
69 | ||
70 | static struct kobj_type ktype_driver = { | |
71 | .sysfs_ops = &driver_sysfs_ops, | |
72 | .release = driver_release, | |
73 | }; | |
74 | ||
75 | ||
76 | /* | |
77 | * sysfs bindings for buses | |
78 | */ | |
79 | ||
80 | ||
81 | static ssize_t | |
82 | bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) | |
83 | { | |
84 | struct bus_attribute * bus_attr = to_bus_attr(attr); | |
85 | struct bus_type * bus = to_bus(kobj); | |
86 | ssize_t ret = 0; | |
87 | ||
88 | if (bus_attr->show) | |
89 | ret = bus_attr->show(bus, buf); | |
90 | return ret; | |
91 | } | |
92 | ||
93 | static ssize_t | |
94 | bus_attr_store(struct kobject * kobj, struct attribute * attr, | |
95 | const char * buf, size_t count) | |
96 | { | |
97 | struct bus_attribute * bus_attr = to_bus_attr(attr); | |
98 | struct bus_type * bus = to_bus(kobj); | |
99 | ssize_t ret = 0; | |
100 | ||
101 | if (bus_attr->store) | |
102 | ret = bus_attr->store(bus, buf, count); | |
103 | return ret; | |
104 | } | |
105 | ||
106 | static struct sysfs_ops bus_sysfs_ops = { | |
107 | .show = bus_attr_show, | |
108 | .store = bus_attr_store, | |
109 | }; | |
110 | ||
111 | int bus_create_file(struct bus_type * bus, struct bus_attribute * attr) | |
112 | { | |
113 | int error; | |
114 | if (get_bus(bus)) { | |
115 | error = sysfs_create_file(&bus->subsys.kset.kobj, &attr->attr); | |
116 | put_bus(bus); | |
117 | } else | |
118 | error = -EINVAL; | |
119 | return error; | |
120 | } | |
121 | ||
122 | void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr) | |
123 | { | |
124 | if (get_bus(bus)) { | |
125 | sysfs_remove_file(&bus->subsys.kset.kobj, &attr->attr); | |
126 | put_bus(bus); | |
127 | } | |
128 | } | |
129 | ||
130 | static struct kobj_type ktype_bus = { | |
131 | .sysfs_ops = &bus_sysfs_ops, | |
132 | ||
133 | }; | |
134 | ||
135 | decl_subsys(bus, &ktype_bus, NULL); | |
136 | ||
137 | static int __bus_for_each_dev(struct bus_type *bus, struct device *start, | |
138 | void *data, int (*fn)(struct device *, void *)) | |
139 | { | |
140 | struct list_head *head; | |
141 | struct device *dev; | |
142 | int error = 0; | |
143 | ||
144 | if (!(bus = get_bus(bus))) | |
145 | return -EINVAL; | |
146 | ||
147 | head = &bus->devices.list; | |
148 | dev = list_prepare_entry(start, head, bus_list); | |
149 | list_for_each_entry_continue(dev, head, bus_list) { | |
150 | get_device(dev); | |
151 | error = fn(dev, data); | |
152 | put_device(dev); | |
153 | if (error) | |
154 | break; | |
155 | } | |
156 | put_bus(bus); | |
157 | return error; | |
158 | } | |
159 | ||
160 | static int __bus_for_each_drv(struct bus_type *bus, struct device_driver *start, | |
161 | void * data, int (*fn)(struct device_driver *, void *)) | |
162 | { | |
163 | struct list_head *head; | |
164 | struct device_driver *drv; | |
165 | int error = 0; | |
166 | ||
167 | if (!(bus = get_bus(bus))) | |
168 | return -EINVAL; | |
169 | ||
170 | head = &bus->drivers.list; | |
171 | drv = list_prepare_entry(start, head, kobj.entry); | |
172 | list_for_each_entry_continue(drv, head, kobj.entry) { | |
173 | get_driver(drv); | |
174 | error = fn(drv, data); | |
175 | put_driver(drv); | |
176 | if (error) | |
177 | break; | |
178 | } | |
179 | put_bus(bus); | |
180 | return error; | |
181 | } | |
182 | ||
183 | /** | |
184 | * bus_for_each_dev - device iterator. | |
185 | * @bus: bus type. | |
186 | * @start: device to start iterating from. | |
187 | * @data: data for the callback. | |
188 | * @fn: function to be called for each device. | |
189 | * | |
190 | * Iterate over @bus's list of devices, and call @fn for each, | |
191 | * passing it @data. If @start is not NULL, we use that device to | |
192 | * begin iterating from. | |
193 | * | |
194 | * We check the return of @fn each time. If it returns anything | |
195 | * other than 0, we break out and return that value. | |
196 | * | |
197 | * NOTE: The device that returns a non-zero value is not retained | |
198 | * in any way, nor is its refcount incremented. If the caller needs | |
199 | * to retain this data, it should do, and increment the reference | |
200 | * count in the supplied callback. | |
201 | */ | |
202 | ||
203 | int bus_for_each_dev(struct bus_type * bus, struct device * start, | |
204 | void * data, int (*fn)(struct device *, void *)) | |
205 | { | |
206 | int ret; | |
207 | ||
208 | down_read(&bus->subsys.rwsem); | |
209 | ret = __bus_for_each_dev(bus, start, data, fn); | |
210 | up_read(&bus->subsys.rwsem); | |
211 | return ret; | |
212 | } | |
213 | ||
214 | /** | |
215 | * bus_for_each_drv - driver iterator | |
216 | * @bus: bus we're dealing with. | |
217 | * @start: driver to start iterating on. | |
218 | * @data: data to pass to the callback. | |
219 | * @fn: function to call for each driver. | |
220 | * | |
221 | * This is nearly identical to the device iterator above. | |
222 | * We iterate over each driver that belongs to @bus, and call | |
223 | * @fn for each. If @fn returns anything but 0, we break out | |
224 | * and return it. If @start is not NULL, we use it as the head | |
225 | * of the list. | |
226 | * | |
227 | * NOTE: we don't return the driver that returns a non-zero | |
228 | * value, nor do we leave the reference count incremented for that | |
229 | * driver. If the caller needs to know that info, it must set it | |
230 | * in the callback. It must also be sure to increment the refcount | |
231 | * so it doesn't disappear before returning to the caller. | |
232 | */ | |
233 | ||
234 | int bus_for_each_drv(struct bus_type * bus, struct device_driver * start, | |
235 | void * data, int (*fn)(struct device_driver *, void *)) | |
236 | { | |
237 | int ret; | |
238 | ||
239 | down_read(&bus->subsys.rwsem); | |
240 | ret = __bus_for_each_drv(bus, start, data, fn); | |
241 | up_read(&bus->subsys.rwsem); | |
242 | return ret; | |
243 | } | |
244 | ||
1da177e4 LT |
245 | static int device_add_attrs(struct bus_type * bus, struct device * dev) |
246 | { | |
247 | int error = 0; | |
248 | int i; | |
249 | ||
250 | if (bus->dev_attrs) { | |
251 | for (i = 0; attr_name(bus->dev_attrs[i]); i++) { | |
252 | error = device_create_file(dev,&bus->dev_attrs[i]); | |
253 | if (error) | |
254 | goto Err; | |
255 | } | |
256 | } | |
257 | Done: | |
258 | return error; | |
259 | Err: | |
260 | while (--i >= 0) | |
261 | device_remove_file(dev,&bus->dev_attrs[i]); | |
262 | goto Done; | |
263 | } | |
264 | ||
265 | ||
266 | static void device_remove_attrs(struct bus_type * bus, struct device * dev) | |
267 | { | |
268 | int i; | |
269 | ||
270 | if (bus->dev_attrs) { | |
271 | for (i = 0; attr_name(bus->dev_attrs[i]); i++) | |
272 | device_remove_file(dev,&bus->dev_attrs[i]); | |
273 | } | |
274 | } | |
275 | ||
276 | ||
277 | /** | |
278 | * bus_add_device - add device to bus | |
279 | * @dev: device being added | |
280 | * | |
281 | * - Add the device to its bus's list of devices. | |
282 | * - Try to attach to driver. | |
283 | * - Create link to device's physical location. | |
284 | */ | |
285 | int bus_add_device(struct device * dev) | |
286 | { | |
287 | struct bus_type * bus = get_bus(dev->bus); | |
288 | int error = 0; | |
289 | ||
290 | if (bus) { | |
291 | down_write(&dev->bus->subsys.rwsem); | |
292 | pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id); | |
293 | list_add_tail(&dev->bus_list, &dev->bus->devices.list); | |
294 | device_attach(dev); | |
295 | up_write(&dev->bus->subsys.rwsem); | |
296 | device_add_attrs(bus, dev); | |
297 | sysfs_create_link(&bus->devices.kobj, &dev->kobj, dev->bus_id); | |
298 | sysfs_create_link(&dev->kobj, &dev->bus->subsys.kset.kobj, "bus"); | |
299 | } | |
300 | return error; | |
301 | } | |
302 | ||
303 | /** | |
304 | * bus_remove_device - remove device from bus | |
305 | * @dev: device to be removed | |
306 | * | |
307 | * - Remove symlink from bus's directory. | |
308 | * - Delete device from bus's list. | |
309 | * - Detach from its driver. | |
310 | * - Drop reference taken in bus_add_device(). | |
311 | */ | |
312 | void bus_remove_device(struct device * dev) | |
313 | { | |
314 | if (dev->bus) { | |
315 | sysfs_remove_link(&dev->kobj, "bus"); | |
316 | sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id); | |
317 | device_remove_attrs(dev->bus, dev); | |
318 | down_write(&dev->bus->subsys.rwsem); | |
319 | pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id); | |
320 | device_release_driver(dev); | |
321 | list_del_init(&dev->bus_list); | |
322 | up_write(&dev->bus->subsys.rwsem); | |
323 | put_bus(dev->bus); | |
324 | } | |
325 | } | |
326 | ||
327 | static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv) | |
328 | { | |
329 | int error = 0; | |
330 | int i; | |
331 | ||
332 | if (bus->drv_attrs) { | |
333 | for (i = 0; attr_name(bus->drv_attrs[i]); i++) { | |
334 | error = driver_create_file(drv, &bus->drv_attrs[i]); | |
335 | if (error) | |
336 | goto Err; | |
337 | } | |
338 | } | |
339 | Done: | |
340 | return error; | |
341 | Err: | |
342 | while (--i >= 0) | |
343 | driver_remove_file(drv, &bus->drv_attrs[i]); | |
344 | goto Done; | |
345 | } | |
346 | ||
347 | ||
348 | static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv) | |
349 | { | |
350 | int i; | |
351 | ||
352 | if (bus->drv_attrs) { | |
353 | for (i = 0; attr_name(bus->drv_attrs[i]); i++) | |
354 | driver_remove_file(drv, &bus->drv_attrs[i]); | |
355 | } | |
356 | } | |
357 | ||
358 | ||
359 | /** | |
360 | * bus_add_driver - Add a driver to the bus. | |
361 | * @drv: driver. | |
362 | * | |
363 | */ | |
364 | int bus_add_driver(struct device_driver * drv) | |
365 | { | |
366 | struct bus_type * bus = get_bus(drv->bus); | |
367 | int error = 0; | |
368 | ||
369 | if (bus) { | |
370 | pr_debug("bus %s: add driver %s\n", bus->name, drv->name); | |
371 | error = kobject_set_name(&drv->kobj, "%s", drv->name); | |
372 | if (error) { | |
373 | put_bus(bus); | |
374 | return error; | |
375 | } | |
376 | drv->kobj.kset = &bus->drivers; | |
377 | if ((error = kobject_register(&drv->kobj))) { | |
378 | put_bus(bus); | |
379 | return error; | |
380 | } | |
381 | ||
382 | down_write(&bus->subsys.rwsem); | |
383 | driver_attach(drv); | |
384 | up_write(&bus->subsys.rwsem); | |
385 | module_add_driver(drv->owner, drv); | |
386 | ||
387 | driver_add_attrs(bus, drv); | |
388 | } | |
389 | return error; | |
390 | } | |
391 | ||
392 | ||
393 | /** | |
394 | * bus_remove_driver - delete driver from bus's knowledge. | |
395 | * @drv: driver. | |
396 | * | |
397 | * Detach the driver from the devices it controls, and remove | |
398 | * it from its bus's list of drivers. Finally, we drop the reference | |
399 | * to the bus we took in bus_add_driver(). | |
400 | */ | |
401 | ||
402 | void bus_remove_driver(struct device_driver * drv) | |
403 | { | |
404 | if (drv->bus) { | |
405 | driver_remove_attrs(drv->bus, drv); | |
406 | down_write(&drv->bus->subsys.rwsem); | |
407 | pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name); | |
408 | driver_detach(drv); | |
409 | up_write(&drv->bus->subsys.rwsem); | |
410 | module_remove_driver(drv); | |
411 | kobject_unregister(&drv->kobj); | |
412 | put_bus(drv->bus); | |
413 | } | |
414 | } | |
415 | ||
416 | ||
417 | /* Helper for bus_rescan_devices's iter */ | |
418 | static int bus_rescan_devices_helper(struct device *dev, void *data) | |
419 | { | |
420 | int *count = data; | |
421 | ||
422 | if (!dev->driver && device_attach(dev)) | |
423 | (*count)++; | |
424 | ||
425 | return 0; | |
426 | } | |
427 | ||
428 | ||
429 | /** | |
430 | * bus_rescan_devices - rescan devices on the bus for possible drivers | |
431 | * @bus: the bus to scan. | |
432 | * | |
433 | * This function will look for devices on the bus with no driver | |
434 | * attached and rescan it against existing drivers to see if it | |
435 | * matches any. Calls device_attach(). Returns the number of devices | |
436 | * that were sucessfully bound to a driver. | |
437 | */ | |
438 | int bus_rescan_devices(struct bus_type * bus) | |
439 | { | |
440 | int count = 0; | |
441 | ||
442 | down_write(&bus->subsys.rwsem); | |
443 | __bus_for_each_dev(bus, NULL, &count, bus_rescan_devices_helper); | |
444 | up_write(&bus->subsys.rwsem); | |
445 | ||
446 | return count; | |
447 | } | |
448 | ||
449 | ||
450 | struct bus_type * get_bus(struct bus_type * bus) | |
451 | { | |
452 | return bus ? container_of(subsys_get(&bus->subsys), struct bus_type, subsys) : NULL; | |
453 | } | |
454 | ||
455 | void put_bus(struct bus_type * bus) | |
456 | { | |
457 | subsys_put(&bus->subsys); | |
458 | } | |
459 | ||
460 | ||
461 | /** | |
462 | * find_bus - locate bus by name. | |
463 | * @name: name of bus. | |
464 | * | |
465 | * Call kset_find_obj() to iterate over list of buses to | |
466 | * find a bus by name. Return bus if found. | |
467 | * | |
468 | * Note that kset_find_obj increments bus' reference count. | |
469 | */ | |
470 | ||
471 | struct bus_type * find_bus(char * name) | |
472 | { | |
473 | struct kobject * k = kset_find_obj(&bus_subsys.kset, name); | |
474 | return k ? to_bus(k) : NULL; | |
475 | } | |
476 | ||
477 | ||
478 | /** | |
479 | * bus_add_attrs - Add default attributes for this bus. | |
480 | * @bus: Bus that has just been registered. | |
481 | */ | |
482 | ||
483 | static int bus_add_attrs(struct bus_type * bus) | |
484 | { | |
485 | int error = 0; | |
486 | int i; | |
487 | ||
488 | if (bus->bus_attrs) { | |
489 | for (i = 0; attr_name(bus->bus_attrs[i]); i++) { | |
490 | if ((error = bus_create_file(bus,&bus->bus_attrs[i]))) | |
491 | goto Err; | |
492 | } | |
493 | } | |
494 | Done: | |
495 | return error; | |
496 | Err: | |
497 | while (--i >= 0) | |
498 | bus_remove_file(bus,&bus->bus_attrs[i]); | |
499 | goto Done; | |
500 | } | |
501 | ||
502 | static void bus_remove_attrs(struct bus_type * bus) | |
503 | { | |
504 | int i; | |
505 | ||
506 | if (bus->bus_attrs) { | |
507 | for (i = 0; attr_name(bus->bus_attrs[i]); i++) | |
508 | bus_remove_file(bus,&bus->bus_attrs[i]); | |
509 | } | |
510 | } | |
511 | ||
512 | /** | |
513 | * bus_register - register a bus with the system. | |
514 | * @bus: bus. | |
515 | * | |
516 | * Once we have that, we registered the bus with the kobject | |
517 | * infrastructure, then register the children subsystems it has: | |
518 | * the devices and drivers that belong to the bus. | |
519 | */ | |
520 | int bus_register(struct bus_type * bus) | |
521 | { | |
522 | int retval; | |
523 | ||
524 | retval = kobject_set_name(&bus->subsys.kset.kobj, "%s", bus->name); | |
525 | if (retval) | |
526 | goto out; | |
527 | ||
528 | subsys_set_kset(bus, bus_subsys); | |
529 | retval = subsystem_register(&bus->subsys); | |
530 | if (retval) | |
531 | goto out; | |
532 | ||
533 | kobject_set_name(&bus->devices.kobj, "devices"); | |
534 | bus->devices.subsys = &bus->subsys; | |
535 | retval = kset_register(&bus->devices); | |
536 | if (retval) | |
537 | goto bus_devices_fail; | |
538 | ||
539 | kobject_set_name(&bus->drivers.kobj, "drivers"); | |
540 | bus->drivers.subsys = &bus->subsys; | |
541 | bus->drivers.ktype = &ktype_driver; | |
542 | retval = kset_register(&bus->drivers); | |
543 | if (retval) | |
544 | goto bus_drivers_fail; | |
545 | bus_add_attrs(bus); | |
546 | ||
547 | pr_debug("bus type '%s' registered\n", bus->name); | |
548 | return 0; | |
549 | ||
550 | bus_drivers_fail: | |
551 | kset_unregister(&bus->devices); | |
552 | bus_devices_fail: | |
553 | subsystem_unregister(&bus->subsys); | |
554 | out: | |
555 | return retval; | |
556 | } | |
557 | ||
558 | ||
559 | /** | |
560 | * bus_unregister - remove a bus from the system | |
561 | * @bus: bus. | |
562 | * | |
563 | * Unregister the child subsystems and the bus itself. | |
564 | * Finally, we call put_bus() to release the refcount | |
565 | */ | |
566 | void bus_unregister(struct bus_type * bus) | |
567 | { | |
568 | pr_debug("bus %s: unregistering\n", bus->name); | |
569 | bus_remove_attrs(bus); | |
570 | kset_unregister(&bus->drivers); | |
571 | kset_unregister(&bus->devices); | |
572 | subsystem_unregister(&bus->subsys); | |
573 | } | |
574 | ||
575 | int __init buses_init(void) | |
576 | { | |
577 | return subsystem_register(&bus_subsys); | |
578 | } | |
579 | ||
580 | ||
581 | EXPORT_SYMBOL_GPL(bus_for_each_dev); | |
582 | EXPORT_SYMBOL_GPL(bus_for_each_drv); | |
583 | ||
1da177e4 LT |
584 | EXPORT_SYMBOL_GPL(bus_add_device); |
585 | EXPORT_SYMBOL_GPL(bus_remove_device); | |
586 | EXPORT_SYMBOL_GPL(bus_register); | |
587 | EXPORT_SYMBOL_GPL(bus_unregister); | |
588 | EXPORT_SYMBOL_GPL(bus_rescan_devices); | |
589 | EXPORT_SYMBOL_GPL(get_bus); | |
590 | EXPORT_SYMBOL_GPL(put_bus); | |
591 | EXPORT_SYMBOL_GPL(find_bus); | |
592 | ||
593 | EXPORT_SYMBOL_GPL(bus_create_file); | |
594 | EXPORT_SYMBOL_GPL(bus_remove_file); |