2 * bus.c - bus driver management
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
7 * This file is released under the GPLv2
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>
17 #include "power/power.h"
19 #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
20 #define to_bus(obj) container_of(obj, struct bus_type, subsys.kobj)
23 * sysfs bindings for drivers
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)
30 static int __must_check
bus_rescan_devices_helper(struct device
*dev
,
34 drv_attr_show(struct kobject
* kobj
, struct attribute
* attr
, char * buf
)
36 struct driver_attribute
* drv_attr
= to_drv_attr(attr
);
37 struct device_driver
* drv
= to_driver(kobj
);
41 ret
= drv_attr
->show(drv
, buf
);
46 drv_attr_store(struct kobject
* kobj
, struct attribute
* attr
,
47 const char * buf
, size_t count
)
49 struct driver_attribute
* drv_attr
= to_drv_attr(attr
);
50 struct device_driver
* drv
= to_driver(kobj
);
54 ret
= drv_attr
->store(drv
, buf
, count
);
58 static struct sysfs_ops driver_sysfs_ops
= {
59 .show
= drv_attr_show
,
60 .store
= drv_attr_store
,
64 static void driver_release(struct kobject
* kobj
)
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...
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>
81 static struct kobj_type ktype_driver
= {
82 .sysfs_ops
= &driver_sysfs_ops
,
83 .release
= driver_release
,
88 * sysfs bindings for buses
93 bus_attr_show(struct kobject
* kobj
, struct attribute
* attr
, char * buf
)
95 struct bus_attribute
* bus_attr
= to_bus_attr(attr
);
96 struct bus_type
* bus
= to_bus(kobj
);
100 ret
= bus_attr
->show(bus
, buf
);
105 bus_attr_store(struct kobject
* kobj
, struct attribute
* attr
,
106 const char * buf
, size_t count
)
108 struct bus_attribute
* bus_attr
= to_bus_attr(attr
);
109 struct bus_type
* bus
= to_bus(kobj
);
113 ret
= bus_attr
->store(bus
, buf
, count
);
117 static struct sysfs_ops bus_sysfs_ops
= {
118 .show
= bus_attr_show
,
119 .store
= bus_attr_store
,
122 int bus_create_file(struct bus_type
* bus
, struct bus_attribute
* attr
)
126 error
= sysfs_create_file(&bus
->subsys
.kobj
, &attr
->attr
);
133 void bus_remove_file(struct bus_type
* bus
, struct bus_attribute
* attr
)
136 sysfs_remove_file(&bus
->subsys
.kobj
, &attr
->attr
);
141 static struct kobj_type ktype_bus
= {
142 .sysfs_ops
= &bus_sysfs_ops
,
146 static decl_subsys(bus
, &ktype_bus
, NULL
);
149 #ifdef CONFIG_HOTPLUG
150 /* Manually detach a device from its associated driver. */
151 static int driver_helper(struct device
*dev
, void *data
)
153 const char *name
= data
;
155 if (strcmp(name
, dev
->bus_id
) == 0)
160 static ssize_t
driver_unbind(struct device_driver
*drv
,
161 const char *buf
, size_t count
)
163 struct bus_type
*bus
= get_bus(drv
->bus
);
167 dev
= bus_find_device(bus
, NULL
, (void *)buf
, driver_helper
);
168 if (dev
&& dev
->driver
== drv
) {
169 if (dev
->parent
) /* Needed for USB */
170 down(&dev
->parent
->sem
);
171 device_release_driver(dev
);
173 up(&dev
->parent
->sem
);
180 static DRIVER_ATTR(unbind
, S_IWUSR
, NULL
, driver_unbind
);
183 * Manually attach a device to a driver.
184 * Note: the driver must want to bind to the device,
185 * it is not possible to override the driver's id table.
187 static ssize_t
driver_bind(struct device_driver
*drv
,
188 const char *buf
, size_t count
)
190 struct bus_type
*bus
= get_bus(drv
->bus
);
194 dev
= bus_find_device(bus
, NULL
, (void *)buf
, driver_helper
);
195 if (dev
&& dev
->driver
== NULL
) {
196 if (dev
->parent
) /* Needed for USB */
197 down(&dev
->parent
->sem
);
199 err
= driver_probe_device(drv
, dev
);
202 up(&dev
->parent
->sem
);
204 if (err
> 0) /* success */
206 else if (err
== 0) /* driver didn't accept device */
213 static DRIVER_ATTR(bind
, S_IWUSR
, NULL
, driver_bind
);
215 static ssize_t
show_drivers_autoprobe(struct bus_type
*bus
, char *buf
)
217 return sprintf(buf
, "%d\n", bus
->drivers_autoprobe
);
220 static ssize_t
store_drivers_autoprobe(struct bus_type
*bus
,
221 const char *buf
, size_t count
)
224 bus
->drivers_autoprobe
= 0;
226 bus
->drivers_autoprobe
= 1;
230 static ssize_t
store_drivers_probe(struct bus_type
*bus
,
231 const char *buf
, size_t count
)
235 dev
= bus_find_device(bus
, NULL
, (void *)buf
, driver_helper
);
238 if (bus_rescan_devices_helper(dev
, NULL
) != 0)
244 static struct device
* next_device(struct klist_iter
* i
)
246 struct klist_node
* n
= klist_next(i
);
247 return n
? container_of(n
, struct device
, knode_bus
) : NULL
;
251 * bus_for_each_dev - device iterator.
253 * @start: device to start iterating from.
254 * @data: data for the callback.
255 * @fn: function to be called for each device.
257 * Iterate over @bus's list of devices, and call @fn for each,
258 * passing it @data. If @start is not NULL, we use that device to
259 * begin iterating from.
261 * We check the return of @fn each time. If it returns anything
262 * other than 0, we break out and return that value.
264 * NOTE: The device that returns a non-zero value is not retained
265 * in any way, nor is its refcount incremented. If the caller needs
266 * to retain this data, it should do, and increment the reference
267 * count in the supplied callback.
270 int bus_for_each_dev(struct bus_type
* bus
, struct device
* start
,
271 void * data
, int (*fn
)(struct device
*, void *))
280 klist_iter_init_node(&bus
->klist_devices
, &i
,
281 (start
? &start
->knode_bus
: NULL
));
282 while ((dev
= next_device(&i
)) && !error
)
283 error
= fn(dev
, data
);
289 * bus_find_device - device iterator for locating a particular device.
291 * @start: Device to begin with
292 * @data: Data to pass to match function
293 * @match: Callback function to check device
295 * This is similar to the bus_for_each_dev() function above, but it
296 * returns a reference to a device that is 'found' for later use, as
297 * determined by the @match callback.
299 * The callback should return 0 if the device doesn't match and non-zero
300 * if it does. If the callback returns non-zero, this function will
301 * return to the caller and not iterate over any more devices.
303 struct device
* bus_find_device(struct bus_type
*bus
,
304 struct device
*start
, void *data
,
305 int (*match
)(struct device
*, void *))
313 klist_iter_init_node(&bus
->klist_devices
, &i
,
314 (start
? &start
->knode_bus
: NULL
));
315 while ((dev
= next_device(&i
)))
316 if (match(dev
, data
) && get_device(dev
))
323 static struct device_driver
* next_driver(struct klist_iter
* i
)
325 struct klist_node
* n
= klist_next(i
);
326 return n
? container_of(n
, struct device_driver
, knode_bus
) : NULL
;
330 * bus_for_each_drv - driver iterator
331 * @bus: bus we're dealing with.
332 * @start: driver to start iterating on.
333 * @data: data to pass to the callback.
334 * @fn: function to call for each driver.
336 * This is nearly identical to the device iterator above.
337 * We iterate over each driver that belongs to @bus, and call
338 * @fn for each. If @fn returns anything but 0, we break out
339 * and return it. If @start is not NULL, we use it as the head
342 * NOTE: we don't return the driver that returns a non-zero
343 * value, nor do we leave the reference count incremented for that
344 * driver. If the caller needs to know that info, it must set it
345 * in the callback. It must also be sure to increment the refcount
346 * so it doesn't disappear before returning to the caller.
349 int bus_for_each_drv(struct bus_type
* bus
, struct device_driver
* start
,
350 void * data
, int (*fn
)(struct device_driver
*, void *))
353 struct device_driver
* drv
;
359 klist_iter_init_node(&bus
->klist_drivers
, &i
,
360 start
? &start
->knode_bus
: NULL
);
361 while ((drv
= next_driver(&i
)) && !error
)
362 error
= fn(drv
, data
);
367 static int device_add_attrs(struct bus_type
*bus
, struct device
*dev
)
375 for (i
= 0; attr_name(bus
->dev_attrs
[i
]); i
++) {
376 error
= device_create_file(dev
,&bus
->dev_attrs
[i
]);
379 device_remove_file(dev
, &bus
->dev_attrs
[i
]);
386 static void device_remove_attrs(struct bus_type
* bus
, struct device
* dev
)
390 if (bus
->dev_attrs
) {
391 for (i
= 0; attr_name(bus
->dev_attrs
[i
]); i
++)
392 device_remove_file(dev
,&bus
->dev_attrs
[i
]);
396 #ifdef CONFIG_SYSFS_DEPRECATED
397 static int make_deprecated_bus_links(struct device
*dev
)
399 return sysfs_create_link(&dev
->kobj
,
400 &dev
->bus
->subsys
.kobj
, "bus");
403 static void remove_deprecated_bus_links(struct device
*dev
)
405 sysfs_remove_link(&dev
->kobj
, "bus");
408 static inline int make_deprecated_bus_links(struct device
*dev
) { return 0; }
409 static inline void remove_deprecated_bus_links(struct device
*dev
) { }
413 * bus_add_device - add device to bus
414 * @dev: device being added
416 * - Add the device to its bus's list of devices.
417 * - Create link to device's bus.
419 int bus_add_device(struct device
* dev
)
421 struct bus_type
* bus
= get_bus(dev
->bus
);
425 pr_debug("bus %s: add device %s\n", bus
->name
, dev
->bus_id
);
426 error
= device_add_attrs(bus
, dev
);
429 error
= sysfs_create_link(&bus
->devices
.kobj
,
430 &dev
->kobj
, dev
->bus_id
);
433 error
= sysfs_create_link(&dev
->kobj
,
434 &dev
->bus
->subsys
.kobj
, "subsystem");
437 error
= make_deprecated_bus_links(dev
);
444 sysfs_remove_link(&dev
->kobj
, "subsystem");
446 sysfs_remove_link(&bus
->devices
.kobj
, dev
->bus_id
);
448 device_remove_attrs(bus
, dev
);
455 * bus_attach_device - add device to bus
456 * @dev: device tried to attach to a driver
458 * - Add device to bus's list of devices.
459 * - Try to attach to driver.
461 void bus_attach_device(struct device
* dev
)
463 struct bus_type
*bus
= dev
->bus
;
467 dev
->is_registered
= 1;
468 if (bus
->drivers_autoprobe
)
469 ret
= device_attach(dev
);
472 klist_add_tail(&dev
->knode_bus
, &bus
->klist_devices
);
474 dev
->is_registered
= 0;
479 * bus_remove_device - remove device from bus
480 * @dev: device to be removed
482 * - Remove symlink from bus's directory.
483 * - Delete device from bus's list.
484 * - Detach from its driver.
485 * - Drop reference taken in bus_add_device().
487 void bus_remove_device(struct device
* dev
)
490 sysfs_remove_link(&dev
->kobj
, "subsystem");
491 remove_deprecated_bus_links(dev
);
492 sysfs_remove_link(&dev
->bus
->devices
.kobj
, dev
->bus_id
);
493 device_remove_attrs(dev
->bus
, dev
);
494 if (dev
->is_registered
) {
495 dev
->is_registered
= 0;
496 klist_del(&dev
->knode_bus
);
498 pr_debug("bus %s: remove device %s\n", dev
->bus
->name
, dev
->bus_id
);
499 device_release_driver(dev
);
504 static int driver_add_attrs(struct bus_type
* bus
, struct device_driver
* drv
)
509 if (bus
->drv_attrs
) {
510 for (i
= 0; attr_name(bus
->drv_attrs
[i
]); i
++) {
511 error
= driver_create_file(drv
, &bus
->drv_attrs
[i
]);
520 driver_remove_file(drv
, &bus
->drv_attrs
[i
]);
525 static void driver_remove_attrs(struct bus_type
* bus
, struct device_driver
* drv
)
529 if (bus
->drv_attrs
) {
530 for (i
= 0; attr_name(bus
->drv_attrs
[i
]); i
++)
531 driver_remove_file(drv
, &bus
->drv_attrs
[i
]);
535 #ifdef CONFIG_HOTPLUG
537 * Thanks to drivers making their tables __devinit, we can't allow manual
538 * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
540 static int __must_check
add_bind_files(struct device_driver
*drv
)
544 ret
= driver_create_file(drv
, &driver_attr_unbind
);
546 ret
= driver_create_file(drv
, &driver_attr_bind
);
548 driver_remove_file(drv
, &driver_attr_unbind
);
553 static void remove_bind_files(struct device_driver
*drv
)
555 driver_remove_file(drv
, &driver_attr_bind
);
556 driver_remove_file(drv
, &driver_attr_unbind
);
559 static int add_probe_files(struct bus_type
*bus
)
563 bus
->drivers_probe_attr
.attr
.name
= "drivers_probe";
564 bus
->drivers_probe_attr
.attr
.mode
= S_IWUSR
;
565 bus
->drivers_probe_attr
.attr
.owner
= bus
->owner
;
566 bus
->drivers_probe_attr
.store
= store_drivers_probe
;
567 retval
= bus_create_file(bus
, &bus
->drivers_probe_attr
);
571 bus
->drivers_autoprobe_attr
.attr
.name
= "drivers_autoprobe";
572 bus
->drivers_autoprobe_attr
.attr
.mode
= S_IWUSR
| S_IRUGO
;
573 bus
->drivers_autoprobe_attr
.attr
.owner
= bus
->owner
;
574 bus
->drivers_autoprobe_attr
.show
= show_drivers_autoprobe
;
575 bus
->drivers_autoprobe_attr
.store
= store_drivers_autoprobe
;
576 retval
= bus_create_file(bus
, &bus
->drivers_autoprobe_attr
);
578 bus_remove_file(bus
, &bus
->drivers_probe_attr
);
583 static void remove_probe_files(struct bus_type
*bus
)
585 bus_remove_file(bus
, &bus
->drivers_autoprobe_attr
);
586 bus_remove_file(bus
, &bus
->drivers_probe_attr
);
589 static inline int add_bind_files(struct device_driver
*drv
) { return 0; }
590 static inline void remove_bind_files(struct device_driver
*drv
) {}
591 static inline int add_probe_files(struct bus_type
*bus
) { return 0; }
592 static inline void remove_probe_files(struct bus_type
*bus
) {}
596 * bus_add_driver - Add a driver to the bus.
600 int bus_add_driver(struct device_driver
*drv
)
602 struct bus_type
* bus
= get_bus(drv
->bus
);
608 pr_debug("bus %s: add driver %s\n", bus
->name
, drv
->name
);
609 error
= kobject_set_name(&drv
->kobj
, "%s", drv
->name
);
612 drv
->kobj
.kset
= &bus
->drivers
;
613 if ((error
= kobject_register(&drv
->kobj
)))
616 if (drv
->bus
->drivers_autoprobe
) {
617 error
= driver_attach(drv
);
621 klist_add_tail(&drv
->knode_bus
, &bus
->klist_drivers
);
622 module_add_driver(drv
->owner
, drv
);
624 error
= driver_add_attrs(bus
, drv
);
626 /* How the hell do we get out of this pickle? Give up */
627 printk(KERN_ERR
"%s: driver_add_attrs(%s) failed\n",
628 __FUNCTION__
, drv
->name
);
630 error
= add_bind_files(drv
);
633 printk(KERN_ERR
"%s: add_bind_files(%s) failed\n",
634 __FUNCTION__
, drv
->name
);
639 kobject_unregister(&drv
->kobj
);
646 * bus_remove_driver - delete driver from bus's knowledge.
649 * Detach the driver from the devices it controls, and remove
650 * it from its bus's list of drivers. Finally, we drop the reference
651 * to the bus we took in bus_add_driver().
654 void bus_remove_driver(struct device_driver
* drv
)
659 remove_bind_files(drv
);
660 driver_remove_attrs(drv
->bus
, drv
);
661 klist_remove(&drv
->knode_bus
);
662 pr_debug("bus %s: remove driver %s\n", drv
->bus
->name
, drv
->name
);
664 module_remove_driver(drv
);
665 kobject_unregister(&drv
->kobj
);
670 /* Helper for bus_rescan_devices's iter */
671 static int __must_check
bus_rescan_devices_helper(struct device
*dev
,
677 if (dev
->parent
) /* Needed for USB */
678 down(&dev
->parent
->sem
);
679 ret
= device_attach(dev
);
681 up(&dev
->parent
->sem
);
683 return ret
< 0 ? ret
: 0;
687 * bus_rescan_devices - rescan devices on the bus for possible drivers
688 * @bus: the bus to scan.
690 * This function will look for devices on the bus with no driver
691 * attached and rescan it against existing drivers to see if it matches
692 * any by calling device_attach() for the unbound devices.
694 int bus_rescan_devices(struct bus_type
* bus
)
696 return bus_for_each_dev(bus
, NULL
, NULL
, bus_rescan_devices_helper
);
700 * device_reprobe - remove driver for a device and probe for a new driver
701 * @dev: the device to reprobe
703 * This function detaches the attached driver (if any) for the given
704 * device and restarts the driver probing process. It is intended
705 * to use if probing criteria changed during a devices lifetime and
706 * driver attachment should change accordingly.
708 int device_reprobe(struct device
*dev
)
711 if (dev
->parent
) /* Needed for USB */
712 down(&dev
->parent
->sem
);
713 device_release_driver(dev
);
715 up(&dev
->parent
->sem
);
717 return bus_rescan_devices_helper(dev
, NULL
);
719 EXPORT_SYMBOL_GPL(device_reprobe
);
721 struct bus_type
*get_bus(struct bus_type
*bus
)
723 return bus
? container_of(subsys_get(&bus
->subsys
),
724 struct bus_type
, subsys
) : NULL
;
727 void put_bus(struct bus_type
* bus
)
729 subsys_put(&bus
->subsys
);
734 * find_bus - locate bus by name.
735 * @name: name of bus.
737 * Call kset_find_obj() to iterate over list of buses to
738 * find a bus by name. Return bus if found.
740 * Note that kset_find_obj increments bus' reference count.
743 struct bus_type
* find_bus(char * name
)
745 struct kobject
* k
= kset_find_obj(&bus_subsys
.kset
, name
);
746 return k
? to_bus(k
) : NULL
;
752 * bus_add_attrs - Add default attributes for this bus.
753 * @bus: Bus that has just been registered.
756 static int bus_add_attrs(struct bus_type
* bus
)
761 if (bus
->bus_attrs
) {
762 for (i
= 0; attr_name(bus
->bus_attrs
[i
]); i
++) {
763 if ((error
= bus_create_file(bus
,&bus
->bus_attrs
[i
])))
771 bus_remove_file(bus
,&bus
->bus_attrs
[i
]);
775 static void bus_remove_attrs(struct bus_type
* bus
)
779 if (bus
->bus_attrs
) {
780 for (i
= 0; attr_name(bus
->bus_attrs
[i
]); i
++)
781 bus_remove_file(bus
,&bus
->bus_attrs
[i
]);
785 static void klist_devices_get(struct klist_node
*n
)
787 struct device
*dev
= container_of(n
, struct device
, knode_bus
);
792 static void klist_devices_put(struct klist_node
*n
)
794 struct device
*dev
= container_of(n
, struct device
, knode_bus
);
800 * bus_register - register a bus with the system.
803 * Once we have that, we registered the bus with the kobject
804 * infrastructure, then register the children subsystems it has:
805 * the devices and drivers that belong to the bus.
807 int bus_register(struct bus_type
* bus
)
811 BLOCKING_INIT_NOTIFIER_HEAD(&bus
->bus_notifier
);
813 retval
= kobject_set_name(&bus
->subsys
.kobj
, "%s", bus
->name
);
817 subsys_set_kset(bus
, bus_subsys
);
818 retval
= subsystem_register(&bus
->subsys
);
822 kobject_set_name(&bus
->devices
.kobj
, "devices");
823 bus
->devices
.kobj
.parent
= &bus
->subsys
.kobj
;
824 retval
= kset_register(&bus
->devices
);
826 goto bus_devices_fail
;
828 kobject_set_name(&bus
->drivers
.kobj
, "drivers");
829 bus
->drivers
.kobj
.parent
= &bus
->subsys
.kobj
;
830 bus
->drivers
.ktype
= &ktype_driver
;
831 retval
= kset_register(&bus
->drivers
);
833 goto bus_drivers_fail
;
835 klist_init(&bus
->klist_devices
, klist_devices_get
, klist_devices_put
);
836 klist_init(&bus
->klist_drivers
, NULL
, NULL
);
838 bus
->drivers_autoprobe
= 1;
839 retval
= add_probe_files(bus
);
841 goto bus_probe_files_fail
;
843 retval
= bus_add_attrs(bus
);
847 pr_debug("bus type '%s' registered\n", bus
->name
);
851 remove_probe_files(bus
);
852 bus_probe_files_fail
:
853 kset_unregister(&bus
->drivers
);
855 kset_unregister(&bus
->devices
);
857 subsystem_unregister(&bus
->subsys
);
863 * bus_unregister - remove a bus from the system
866 * Unregister the child subsystems and the bus itself.
867 * Finally, we call put_bus() to release the refcount
869 void bus_unregister(struct bus_type
* bus
)
871 pr_debug("bus %s: unregistering\n", bus
->name
);
872 bus_remove_attrs(bus
);
873 remove_probe_files(bus
);
874 kset_unregister(&bus
->drivers
);
875 kset_unregister(&bus
->devices
);
876 subsystem_unregister(&bus
->subsys
);
879 int bus_register_notifier(struct bus_type
*bus
, struct notifier_block
*nb
)
881 return blocking_notifier_chain_register(&bus
->bus_notifier
, nb
);
883 EXPORT_SYMBOL_GPL(bus_register_notifier
);
885 int bus_unregister_notifier(struct bus_type
*bus
, struct notifier_block
*nb
)
887 return blocking_notifier_chain_unregister(&bus
->bus_notifier
, nb
);
889 EXPORT_SYMBOL_GPL(bus_unregister_notifier
);
891 int __init
buses_init(void)
893 return subsystem_register(&bus_subsys
);
897 EXPORT_SYMBOL_GPL(bus_for_each_dev
);
898 EXPORT_SYMBOL_GPL(bus_find_device
);
899 EXPORT_SYMBOL_GPL(bus_for_each_drv
);
901 EXPORT_SYMBOL_GPL(bus_register
);
902 EXPORT_SYMBOL_GPL(bus_unregister
);
903 EXPORT_SYMBOL_GPL(bus_rescan_devices
);
905 EXPORT_SYMBOL_GPL(bus_create_file
);
906 EXPORT_SYMBOL_GPL(bus_remove_file
);