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,
33 static void bus_put(struct bus_type *bus)
35 kset_put(&bus->subsys);
39 drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
41 struct driver_attribute * drv_attr = to_drv_attr(attr);
42 struct device_driver * drv = to_driver(kobj);
46 ret = drv_attr->show(drv, buf);
51 drv_attr_store(struct kobject * kobj, struct attribute * attr,
52 const char * buf, size_t count)
54 struct driver_attribute * drv_attr = to_drv_attr(attr);
55 struct device_driver * drv = to_driver(kobj);
59 ret = drv_attr->store(drv, buf, count);
63 static struct sysfs_ops driver_sysfs_ops = {
64 .show = drv_attr_show,
65 .store = drv_attr_store,
69 static void driver_release(struct kobject * kobj)
72 * Yes this is an empty release function, it is this way because struct
73 * device is always a static object, not a dynamic one. Yes, this is
74 * not nice and bad, but remember, drivers are code, reference counted
75 * by the module count, not a device, which is really data. And yes,
76 * in the future I do want to have all drivers be created dynamically,
77 * and am working toward that goal, but it will take a bit longer...
79 * But do not let this example give _anyone_ the idea that they can
80 * create a release function without any code in it at all, to do that
81 * is almost always wrong. If you have any questions about this,
82 * please send an email to <greg@kroah.com>
86 static struct kobj_type ktype_driver = {
87 .sysfs_ops = &driver_sysfs_ops,
88 .release = driver_release,
93 * sysfs bindings for buses
98 bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
100 struct bus_attribute * bus_attr = to_bus_attr(attr);
101 struct bus_type * bus = to_bus(kobj);
105 ret = bus_attr->show(bus, buf);
110 bus_attr_store(struct kobject * kobj, struct attribute * attr,
111 const char * buf, size_t count)
113 struct bus_attribute * bus_attr = to_bus_attr(attr);
114 struct bus_type * bus = to_bus(kobj);
118 ret = bus_attr->store(bus, buf, count);
122 static struct sysfs_ops bus_sysfs_ops = {
123 .show = bus_attr_show,
124 .store = bus_attr_store,
127 int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
131 error = sysfs_create_file(&bus->subsys.kobj, &attr->attr);
138 void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
141 sysfs_remove_file(&bus->subsys.kobj, &attr->attr);
146 static struct kobj_type bus_ktype = {
147 .sysfs_ops = &bus_sysfs_ops,
150 static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
152 struct kobj_type *ktype = get_ktype(kobj);
154 if (ktype == &bus_ktype)
159 static struct kset_uevent_ops bus_uevent_ops = {
160 .filter = bus_uevent_filter,
163 static decl_subsys(bus, &bus_ktype, &bus_uevent_ops);
166 #ifdef CONFIG_HOTPLUG
167 /* Manually detach a device from its associated driver. */
168 static int driver_helper(struct device *dev, void *data)
170 const char *name = data;
172 if (strcmp(name, dev->bus_id) == 0)
177 static ssize_t driver_unbind(struct device_driver *drv,
178 const char *buf, size_t count)
180 struct bus_type *bus = get_bus(drv->bus);
184 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
185 if (dev && dev->driver == drv) {
186 if (dev->parent) /* Needed for USB */
187 down(&dev->parent->sem);
188 device_release_driver(dev);
190 up(&dev->parent->sem);
197 static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
200 * Manually attach a device to a driver.
201 * Note: the driver must want to bind to the device,
202 * it is not possible to override the driver's id table.
204 static ssize_t driver_bind(struct device_driver *drv,
205 const char *buf, size_t count)
207 struct bus_type *bus = get_bus(drv->bus);
211 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
212 if (dev && dev->driver == NULL) {
213 if (dev->parent) /* Needed for USB */
214 down(&dev->parent->sem);
216 err = driver_probe_device(drv, dev);
219 up(&dev->parent->sem);
221 if (err > 0) /* success */
223 else if (err == 0) /* driver didn't accept device */
230 static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
232 static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
234 return sprintf(buf, "%d\n", bus->drivers_autoprobe);
237 static ssize_t store_drivers_autoprobe(struct bus_type *bus,
238 const char *buf, size_t count)
241 bus->drivers_autoprobe = 0;
243 bus->drivers_autoprobe = 1;
247 static ssize_t store_drivers_probe(struct bus_type *bus,
248 const char *buf, size_t count)
252 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
255 if (bus_rescan_devices_helper(dev, NULL) != 0)
261 static struct device * next_device(struct klist_iter * i)
263 struct klist_node * n = klist_next(i);
264 return n ? container_of(n, struct device, knode_bus) : NULL;
268 * bus_for_each_dev - device iterator.
270 * @start: device to start iterating from.
271 * @data: data for the callback.
272 * @fn: function to be called for each device.
274 * Iterate over @bus's list of devices, and call @fn for each,
275 * passing it @data. If @start is not NULL, we use that device to
276 * begin iterating from.
278 * We check the return of @fn each time. If it returns anything
279 * other than 0, we break out and return that value.
281 * NOTE: The device that returns a non-zero value is not retained
282 * in any way, nor is its refcount incremented. If the caller needs
283 * to retain this data, it should do, and increment the reference
284 * count in the supplied callback.
287 int bus_for_each_dev(struct bus_type * bus, struct device * start,
288 void * data, int (*fn)(struct device *, void *))
297 klist_iter_init_node(&bus->klist_devices, &i,
298 (start ? &start->knode_bus : NULL));
299 while ((dev = next_device(&i)) && !error)
300 error = fn(dev, data);
306 * bus_find_device - device iterator for locating a particular device.
308 * @start: Device to begin with
309 * @data: Data to pass to match function
310 * @match: Callback function to check device
312 * This is similar to the bus_for_each_dev() function above, but it
313 * returns a reference to a device that is 'found' for later use, as
314 * determined by the @match callback.
316 * The callback should return 0 if the device doesn't match and non-zero
317 * if it does. If the callback returns non-zero, this function will
318 * return to the caller and not iterate over any more devices.
320 struct device * bus_find_device(struct bus_type *bus,
321 struct device *start, void *data,
322 int (*match)(struct device *, void *))
330 klist_iter_init_node(&bus->klist_devices, &i,
331 (start ? &start->knode_bus : NULL));
332 while ((dev = next_device(&i)))
333 if (match(dev, data) && get_device(dev))
340 static struct device_driver * next_driver(struct klist_iter * i)
342 struct klist_node * n = klist_next(i);
343 return n ? container_of(n, struct device_driver, knode_bus) : NULL;
347 * bus_for_each_drv - driver iterator
348 * @bus: bus we're dealing with.
349 * @start: driver to start iterating on.
350 * @data: data to pass to the callback.
351 * @fn: function to call for each driver.
353 * This is nearly identical to the device iterator above.
354 * We iterate over each driver that belongs to @bus, and call
355 * @fn for each. If @fn returns anything but 0, we break out
356 * and return it. If @start is not NULL, we use it as the head
359 * NOTE: we don't return the driver that returns a non-zero
360 * value, nor do we leave the reference count incremented for that
361 * driver. If the caller needs to know that info, it must set it
362 * in the callback. It must also be sure to increment the refcount
363 * so it doesn't disappear before returning to the caller.
366 int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
367 void * data, int (*fn)(struct device_driver *, void *))
370 struct device_driver * drv;
376 klist_iter_init_node(&bus->klist_drivers, &i,
377 start ? &start->knode_bus : NULL);
378 while ((drv = next_driver(&i)) && !error)
379 error = fn(drv, data);
384 static int device_add_attrs(struct bus_type *bus, struct device *dev)
392 for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
393 error = device_create_file(dev,&bus->dev_attrs[i]);
396 device_remove_file(dev, &bus->dev_attrs[i]);
403 static void device_remove_attrs(struct bus_type * bus, struct device * dev)
407 if (bus->dev_attrs) {
408 for (i = 0; attr_name(bus->dev_attrs[i]); i++)
409 device_remove_file(dev,&bus->dev_attrs[i]);
413 #ifdef CONFIG_SYSFS_DEPRECATED
414 static int make_deprecated_bus_links(struct device *dev)
416 return sysfs_create_link(&dev->kobj,
417 &dev->bus->subsys.kobj, "bus");
420 static void remove_deprecated_bus_links(struct device *dev)
422 sysfs_remove_link(&dev->kobj, "bus");
425 static inline int make_deprecated_bus_links(struct device *dev) { return 0; }
426 static inline void remove_deprecated_bus_links(struct device *dev) { }
430 * bus_add_device - add device to bus
431 * @dev: device being added
433 * - Add the device to its bus's list of devices.
434 * - Create link to device's bus.
436 int bus_add_device(struct device * dev)
438 struct bus_type * bus = get_bus(dev->bus);
442 pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
443 error = device_add_attrs(bus, dev);
446 error = sysfs_create_link(&bus->devices.kobj,
447 &dev->kobj, dev->bus_id);
450 error = sysfs_create_link(&dev->kobj,
451 &dev->bus->subsys.kobj, "subsystem");
454 error = make_deprecated_bus_links(dev);
461 sysfs_remove_link(&dev->kobj, "subsystem");
463 sysfs_remove_link(&bus->devices.kobj, dev->bus_id);
465 device_remove_attrs(bus, dev);
472 * bus_attach_device - add device to bus
473 * @dev: device tried to attach to a driver
475 * - Add device to bus's list of devices.
476 * - Try to attach to driver.
478 void bus_attach_device(struct device * dev)
480 struct bus_type *bus = dev->bus;
484 dev->is_registered = 1;
485 if (bus->drivers_autoprobe)
486 ret = device_attach(dev);
489 klist_add_tail(&dev->knode_bus, &bus->klist_devices);
491 dev->is_registered = 0;
496 * bus_remove_device - remove device from bus
497 * @dev: device to be removed
499 * - Remove symlink from bus's directory.
500 * - Delete device from bus's list.
501 * - Detach from its driver.
502 * - Drop reference taken in bus_add_device().
504 void bus_remove_device(struct device * dev)
507 sysfs_remove_link(&dev->kobj, "subsystem");
508 remove_deprecated_bus_links(dev);
509 sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
510 device_remove_attrs(dev->bus, dev);
511 if (dev->is_registered) {
512 dev->is_registered = 0;
513 klist_del(&dev->knode_bus);
515 pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
516 device_release_driver(dev);
521 static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
526 if (bus->drv_attrs) {
527 for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
528 error = driver_create_file(drv, &bus->drv_attrs[i]);
537 driver_remove_file(drv, &bus->drv_attrs[i]);
542 static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
546 if (bus->drv_attrs) {
547 for (i = 0; attr_name(bus->drv_attrs[i]); i++)
548 driver_remove_file(drv, &bus->drv_attrs[i]);
552 #ifdef CONFIG_HOTPLUG
554 * Thanks to drivers making their tables __devinit, we can't allow manual
555 * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
557 static int __must_check add_bind_files(struct device_driver *drv)
561 ret = driver_create_file(drv, &driver_attr_unbind);
563 ret = driver_create_file(drv, &driver_attr_bind);
565 driver_remove_file(drv, &driver_attr_unbind);
570 static void remove_bind_files(struct device_driver *drv)
572 driver_remove_file(drv, &driver_attr_bind);
573 driver_remove_file(drv, &driver_attr_unbind);
576 static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
577 static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
578 show_drivers_autoprobe, store_drivers_autoprobe);
580 static int add_probe_files(struct bus_type *bus)
584 retval = bus_create_file(bus, &bus_attr_drivers_probe);
588 retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
590 bus_remove_file(bus, &bus_attr_drivers_probe);
595 static void remove_probe_files(struct bus_type *bus)
597 bus_remove_file(bus, &bus_attr_drivers_autoprobe);
598 bus_remove_file(bus, &bus_attr_drivers_probe);
601 static inline int add_bind_files(struct device_driver *drv) { return 0; }
602 static inline void remove_bind_files(struct device_driver *drv) {}
603 static inline int add_probe_files(struct bus_type *bus) { return 0; }
604 static inline void remove_probe_files(struct bus_type *bus) {}
608 * bus_add_driver - Add a driver to the bus.
612 int bus_add_driver(struct device_driver *drv)
614 struct bus_type * bus = get_bus(drv->bus);
620 pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
621 error = kobject_set_name(&drv->kobj, "%s", drv->name);
624 drv->kobj.kset = &bus->drivers;
625 error = kobject_register(&drv->kobj);
629 if (drv->bus->drivers_autoprobe) {
630 error = driver_attach(drv);
634 klist_add_tail(&drv->knode_bus, &bus->klist_drivers);
635 module_add_driver(drv->owner, drv);
637 error = driver_add_attrs(bus, drv);
639 /* How the hell do we get out of this pickle? Give up */
640 printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
641 __FUNCTION__, drv->name);
643 error = add_bind_files(drv);
646 printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
647 __FUNCTION__, drv->name);
652 kobject_unregister(&drv->kobj);
659 * bus_remove_driver - delete driver from bus's knowledge.
662 * Detach the driver from the devices it controls, and remove
663 * it from its bus's list of drivers. Finally, we drop the reference
664 * to the bus we took in bus_add_driver().
667 void bus_remove_driver(struct device_driver * drv)
672 remove_bind_files(drv);
673 driver_remove_attrs(drv->bus, drv);
674 klist_remove(&drv->knode_bus);
675 pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
677 module_remove_driver(drv);
678 kobject_unregister(&drv->kobj);
683 /* Helper for bus_rescan_devices's iter */
684 static int __must_check bus_rescan_devices_helper(struct device *dev,
690 if (dev->parent) /* Needed for USB */
691 down(&dev->parent->sem);
692 ret = device_attach(dev);
694 up(&dev->parent->sem);
696 return ret < 0 ? ret : 0;
700 * bus_rescan_devices - rescan devices on the bus for possible drivers
701 * @bus: the bus to scan.
703 * This function will look for devices on the bus with no driver
704 * attached and rescan it against existing drivers to see if it matches
705 * any by calling device_attach() for the unbound devices.
707 int bus_rescan_devices(struct bus_type * bus)
709 return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
713 * device_reprobe - remove driver for a device and probe for a new driver
714 * @dev: the device to reprobe
716 * This function detaches the attached driver (if any) for the given
717 * device and restarts the driver probing process. It is intended
718 * to use if probing criteria changed during a devices lifetime and
719 * driver attachment should change accordingly.
721 int device_reprobe(struct device *dev)
724 if (dev->parent) /* Needed for USB */
725 down(&dev->parent->sem);
726 device_release_driver(dev);
728 up(&dev->parent->sem);
730 return bus_rescan_devices_helper(dev, NULL);
732 EXPORT_SYMBOL_GPL(device_reprobe);
734 struct bus_type *get_bus(struct bus_type *bus)
736 return bus ? container_of(kset_get(&bus->subsys),
737 struct bus_type, subsys) : NULL;
741 * find_bus - locate bus by name.
742 * @name: name of bus.
744 * Call kset_find_obj() to iterate over list of buses to
745 * find a bus by name. Return bus if found.
747 * Note that kset_find_obj increments bus' reference count.
750 struct bus_type * find_bus(char * name)
752 struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
753 return k ? to_bus(k) : NULL;
759 * bus_add_attrs - Add default attributes for this bus.
760 * @bus: Bus that has just been registered.
763 static int bus_add_attrs(struct bus_type * bus)
768 if (bus->bus_attrs) {
769 for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
770 error = bus_create_file(bus,&bus->bus_attrs[i]);
779 bus_remove_file(bus,&bus->bus_attrs[i]);
783 static void bus_remove_attrs(struct bus_type * bus)
787 if (bus->bus_attrs) {
788 for (i = 0; attr_name(bus->bus_attrs[i]); i++)
789 bus_remove_file(bus,&bus->bus_attrs[i]);
793 static void klist_devices_get(struct klist_node *n)
795 struct device *dev = container_of(n, struct device, knode_bus);
800 static void klist_devices_put(struct klist_node *n)
802 struct device *dev = container_of(n, struct device, knode_bus);
808 * bus_register - register a bus with the system.
811 * Once we have that, we registered the bus with the kobject
812 * infrastructure, then register the children subsystems it has:
813 * the devices and drivers that belong to the bus.
815 int bus_register(struct bus_type * bus)
819 BLOCKING_INIT_NOTIFIER_HEAD(&bus->bus_notifier);
821 retval = kobject_set_name(&bus->subsys.kobj, "%s", bus->name);
825 bus->subsys.kobj.kset = &bus_subsys;
827 retval = subsystem_register(&bus->subsys);
831 kobject_set_name(&bus->devices.kobj, "devices");
832 bus->devices.kobj.parent = &bus->subsys.kobj;
833 retval = kset_register(&bus->devices);
835 goto bus_devices_fail;
837 kobject_set_name(&bus->drivers.kobj, "drivers");
838 bus->drivers.kobj.parent = &bus->subsys.kobj;
839 bus->drivers.ktype = &ktype_driver;
840 retval = kset_register(&bus->drivers);
842 goto bus_drivers_fail;
844 klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put);
845 klist_init(&bus->klist_drivers, NULL, NULL);
847 bus->drivers_autoprobe = 1;
848 retval = add_probe_files(bus);
850 goto bus_probe_files_fail;
852 retval = bus_add_attrs(bus);
856 pr_debug("bus type '%s' registered\n", bus->name);
860 remove_probe_files(bus);
861 bus_probe_files_fail:
862 kset_unregister(&bus->drivers);
864 kset_unregister(&bus->devices);
866 subsystem_unregister(&bus->subsys);
872 * bus_unregister - remove a bus from the system
875 * Unregister the child subsystems and the bus itself.
876 * Finally, we call bus_put() to release the refcount
878 void bus_unregister(struct bus_type * bus)
880 pr_debug("bus %s: unregistering\n", bus->name);
881 bus_remove_attrs(bus);
882 remove_probe_files(bus);
883 kset_unregister(&bus->drivers);
884 kset_unregister(&bus->devices);
885 subsystem_unregister(&bus->subsys);
888 int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
890 return blocking_notifier_chain_register(&bus->bus_notifier, nb);
892 EXPORT_SYMBOL_GPL(bus_register_notifier);
894 int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
896 return blocking_notifier_chain_unregister(&bus->bus_notifier, nb);
898 EXPORT_SYMBOL_GPL(bus_unregister_notifier);
900 int __init buses_init(void)
902 return subsystem_register(&bus_subsys);
906 EXPORT_SYMBOL_GPL(bus_for_each_dev);
907 EXPORT_SYMBOL_GPL(bus_find_device);
908 EXPORT_SYMBOL_GPL(bus_for_each_drv);
910 EXPORT_SYMBOL_GPL(bus_register);
911 EXPORT_SYMBOL_GPL(bus_unregister);
912 EXPORT_SYMBOL_GPL(bus_rescan_devices);
914 EXPORT_SYMBOL_GPL(bus_create_file);
915 EXPORT_SYMBOL_GPL(bus_remove_file);