MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / base / bus.c
blob0c07498ddca1e92fbb4f096d47d813be49136121
1 /*
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
9 */
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"
20 #define to_dev(node) container_of(node, struct device, bus_list)
21 #define to_drv(node) container_of(node, struct device_driver, kobj.entry)
23 #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
24 #define to_bus(obj) container_of(obj, struct bus_type, subsys.kset.kobj)
27 * sysfs bindings for drivers
30 #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
31 #define to_driver(obj) container_of(obj, struct device_driver, kobj)
34 static ssize_t
35 drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
37 struct driver_attribute * drv_attr = to_drv_attr(attr);
38 struct device_driver * drv = to_driver(kobj);
39 ssize_t ret = 0;
41 if (drv_attr->show)
42 ret = drv_attr->show(drv, buf);
43 return ret;
46 static ssize_t
47 drv_attr_store(struct kobject * kobj, struct attribute * attr,
48 const char * buf, size_t count)
50 struct driver_attribute * drv_attr = to_drv_attr(attr);
51 struct device_driver * drv = to_driver(kobj);
52 ssize_t ret = 0;
54 if (drv_attr->store)
55 ret = drv_attr->store(drv, buf, count);
56 return ret;
59 static struct sysfs_ops driver_sysfs_ops = {
60 .show = drv_attr_show,
61 .store = drv_attr_store,
65 static void driver_release(struct kobject * kobj)
67 struct device_driver * drv = to_driver(kobj);
68 up(&drv->unload_sem);
71 static struct kobj_type ktype_driver = {
72 .sysfs_ops = &driver_sysfs_ops,
73 .release = driver_release,
78 * sysfs bindings for buses
82 static ssize_t
83 bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
85 struct bus_attribute * bus_attr = to_bus_attr(attr);
86 struct bus_type * bus = to_bus(kobj);
87 ssize_t ret = 0;
89 if (bus_attr->show)
90 ret = bus_attr->show(bus, buf);
91 return ret;
94 static ssize_t
95 bus_attr_store(struct kobject * kobj, struct attribute * attr,
96 const char * buf, size_t count)
98 struct bus_attribute * bus_attr = to_bus_attr(attr);
99 struct bus_type * bus = to_bus(kobj);
100 ssize_t ret = 0;
102 if (bus_attr->store)
103 ret = bus_attr->store(bus, buf, count);
104 return ret;
107 static struct sysfs_ops bus_sysfs_ops = {
108 .show = bus_attr_show,
109 .store = bus_attr_store,
112 int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
114 int error;
115 if (get_bus(bus)) {
116 error = sysfs_create_file(&bus->subsys.kset.kobj, &attr->attr);
117 put_bus(bus);
118 } else
119 error = -EINVAL;
120 return error;
123 void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
125 if (get_bus(bus)) {
126 sysfs_remove_file(&bus->subsys.kset.kobj, &attr->attr);
127 put_bus(bus);
131 static struct kobj_type ktype_bus = {
132 .sysfs_ops = &bus_sysfs_ops,
136 decl_subsys(bus, &ktype_bus, NULL);
139 * bus_for_each_dev - device iterator.
140 * @bus: bus type.
141 * @start: device to start iterating from.
142 * @data: data for the callback.
143 * @fn: function to be called for each device.
145 * Iterate over @bus's list of devices, and call @fn for each,
146 * passing it @data. If @start is not NULL, we use that device to
147 * begin iterating from.
149 * We check the return of @fn each time. If it returns anything
150 * other than 0, we break out and return that value.
152 * NOTE: The device that returns a non-zero value is not retained
153 * in any way, nor is its refcount incremented. If the caller needs
154 * to retain this data, it should do, and increment the reference
155 * count in the supplied callback.
157 int bus_for_each_dev(struct bus_type * bus, struct device * start,
158 void * data, int (*fn)(struct device *, void *))
160 struct device *dev;
161 struct list_head * head;
162 int error = 0;
165 if (!(bus = get_bus(bus)))
166 return -EINVAL;
168 head = &bus->devices.list;
169 dev = list_prepare_entry(start, head, bus_list);
171 down_read(&bus->subsys.rwsem);
172 list_for_each_entry_continue(dev, head, bus_list) {
173 get_device(dev);
174 error = fn(dev, data);
175 put_device(dev);
176 if (error)
177 break;
179 up_read(&bus->subsys.rwsem);
180 put_bus(bus);
182 return error;
186 * bus_for_each_drv - driver iterator
187 * @bus: bus we're dealing with.
188 * @start: driver to start iterating on.
189 * @data: data to pass to the callback.
190 * @fn: function to call for each driver.
192 * This is nearly identical to the device iterator above.
193 * We iterate over each driver that belongs to @bus, and call
194 * @fn for each. If @fn returns anything but 0, we break out
195 * and return it. If @start is not NULL, we use it as the head
196 * of the list.
198 * NOTE: we don't return the driver that returns a non-zero
199 * value, nor do we leave the reference count incremented for that
200 * driver. If the caller needs to know that info, it must set it
201 * in the callback. It must also be sure to increment the refcount
202 * so it doesn't disappear before returning to the caller.
205 int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
206 void * data, int (*fn)(struct device_driver *, void *))
208 struct list_head * head;
209 struct device_driver *drv;
210 int error = 0;
212 if(!(bus = get_bus(bus)))
213 return -EINVAL;
215 head = &bus->drivers.list;
216 drv = list_prepare_entry(start, head, kobj.entry);
218 down_read(&bus->subsys.rwsem);
219 list_for_each_entry_continue(drv, head, kobj.entry) {
220 get_driver(drv);
221 error = fn(drv, data);
222 put_driver(drv);
223 if(error)
224 break;
226 up_read(&bus->subsys.rwsem);
227 put_bus(bus);
228 return error;
232 * device_bind_driver - bind a driver to one device.
233 * @dev: device.
235 * Allow manual attachment of a driver to a deivce.
236 * Caller must have already set @dev->driver.
238 * Note that this does not modify the bus reference count
239 * nor take the bus's rwsem. Please verify those are accounted
240 * for before calling this. (It is ok to call with no other effort
241 * from a driver's probe() method.)
244 void device_bind_driver(struct device * dev)
246 pr_debug("bound device '%s' to driver '%s'\n",
247 dev->bus_id, dev->driver->name);
248 list_add_tail(&dev->driver_list, &dev->driver->devices);
249 sysfs_create_link(&dev->driver->kobj, &dev->kobj,
250 kobject_name(&dev->kobj));
255 * bus_match - check compatibility between device & driver.
256 * @dev: device.
257 * @drv: driver.
259 * First, we call the bus's match function, which should compare
260 * the device IDs the driver supports with the device IDs of the
261 * device. Note we don't do this ourselves because we don't know
262 * the format of the ID structures, nor what is to be considered
263 * a match and what is not.
265 * If we find a match, we call @drv->probe(@dev) if it exists, and
266 * call attach() above.
268 static int bus_match(struct device * dev, struct device_driver * drv)
270 int error = -ENODEV;
271 if (dev->bus->match(dev, drv)) {
272 dev->driver = drv;
273 if (drv->probe) {
274 if ((error = drv->probe(dev))) {
275 dev->driver = NULL;
276 return error;
280 device_bind_driver(dev);
282 error = 0;
284 return error;
289 * device_attach - try to attach device to a driver.
290 * @dev: device.
292 * Walk the list of drivers that the bus has and call bus_match()
293 * for each pair. If a compatible pair is found, break out and return.
295 static int device_attach(struct device * dev)
297 struct bus_type * bus = dev->bus;
298 struct list_head * entry;
299 int error;
302 if (dev->driver) {
303 device_bind_driver(dev);
304 return 1;
307 if (bus->match) {
308 list_for_each(entry, &bus->drivers.list) {
309 struct device_driver * drv = to_drv(entry);
310 error = bus_match(dev, drv);
312 if (!error)
313 /* success, driver matched */
314 return 1;
315 if (error != -ENODEV)
316 /* driver matched but the probe failed */
317 printk(KERN_WARNING
318 "%s: probe of %s failed with error %d\n",
319 drv->name, dev->bus_id, error);
324 return 0;
329 * driver_attach - try to bind driver to devices.
330 * @drv: driver.
332 * Walk the list of devices that the bus has on it and try to match
333 * the driver with each one.
334 * If bus_match() returns 0 and the @dev->driver is set, we've found
335 * a compatible pair.
337 * Note that we ignore the -ENODEV error from bus_match(), since it's
338 * perfectly valid for a driver not to bind to any devices.
340 void driver_attach(struct device_driver * drv)
342 struct bus_type * bus = drv->bus;
343 struct list_head * entry;
344 int error;
347 if (!bus->match)
348 return;
350 list_for_each(entry, &bus->devices.list) {
351 struct device * dev = container_of(entry, struct device, bus_list);
353 if (!dev->driver) {
354 error = bus_match(dev, drv);
355 if (error && (error != -ENODEV))
356 /* driver matched but the probe failed */
357 printk(KERN_WARNING
358 "%s: probe of %s failed with error %d\n",
359 drv->name, dev->bus_id, error);
369 * device_release_driver - manually detach device from driver.
370 * @dev: device.
372 * Manually detach device from driver.
373 * Note that this is called without incrementing the bus
374 * reference count nor taking the bus's rwsem. Be sure that
375 * those are accounted for before calling this function.
378 void device_release_driver(struct device * dev)
380 struct device_driver * drv = dev->driver;
381 if (drv) {
382 sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj));
383 list_del_init(&dev->driver_list);
384 device_detach_shutdown(dev);
386 if (drv->remove)
387 drv->remove(dev);
388 dev->driver = NULL;
394 * driver_detach - detach driver from all devices it controls.
395 * @drv: driver.
398 static void driver_detach(struct device_driver * drv)
400 struct list_head * entry, * next;
401 list_for_each_safe(entry, next, &drv->devices) {
402 struct device * dev = container_of(entry, struct device, driver_list);
406 device_release_driver(dev);
410 static int device_add_attrs(struct bus_type * bus, struct device * dev)
412 int error = 0;
413 int i;
415 if (bus->dev_attrs) {
416 for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
417 error = device_create_file(dev,&bus->dev_attrs[i]);
418 if (error)
419 goto Err;
422 Done:
423 return error;
424 Err:
425 while (--i >= 0)
426 device_remove_file(dev,&bus->dev_attrs[i]);
427 goto Done;
431 static void device_remove_attrs(struct bus_type * bus, struct device * dev)
433 int i;
435 if (bus->dev_attrs) {
436 for (i = 0; attr_name(bus->dev_attrs[i]); i++)
437 device_remove_file(dev,&bus->dev_attrs[i]);
443 * bus_add_device - add device to bus
444 * @dev: device being added
446 * - Add the device to its bus's list of devices.
447 * - Try to attach to driver.
448 * - Create link to device's physical location.
450 int bus_add_device(struct device * dev)
452 struct bus_type * bus = get_bus(dev->bus);
453 int error = 0;
456 if (bus) {
457 down_write(&dev->bus->subsys.rwsem);
458 pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
459 list_add_tail(&dev->bus_list, &dev->bus->devices.list);
461 device_attach(dev);
462 up_write(&dev->bus->subsys.rwsem);
463 device_add_attrs(bus, dev);
464 sysfs_create_link(&bus->devices.kobj, &dev->kobj, dev->bus_id);
468 return error;
472 * bus_remove_device - remove device from bus
473 * @dev: device to be removed
475 * - Remove symlink from bus's directory.
476 * - Delete device from bus's list.
477 * - Detach from its driver.
478 * - Drop reference taken in bus_add_device().
480 void bus_remove_device(struct device * dev)
482 if (dev->bus) {
483 sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
484 device_remove_attrs(dev->bus, dev);
485 down_write(&dev->bus->subsys.rwsem);
486 pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
487 device_release_driver(dev);
488 list_del_init(&dev->bus_list);
489 up_write(&dev->bus->subsys.rwsem);
490 put_bus(dev->bus);
494 static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
496 int error = 0;
497 int i;
499 if (bus->drv_attrs) {
500 for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
501 error = driver_create_file(drv, &bus->drv_attrs[i]);
502 if (error)
503 goto Err;
506 Done:
507 return error;
508 Err:
509 while (--i >= 0)
510 driver_remove_file(drv, &bus->drv_attrs[i]);
511 goto Done;
515 static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
517 int i;
519 if (bus->drv_attrs) {
520 for (i = 0; attr_name(bus->drv_attrs[i]); i++)
521 driver_remove_file(drv, &bus->drv_attrs[i]);
527 * bus_add_driver - Add a driver to the bus.
528 * @drv: driver.
531 int bus_add_driver(struct device_driver * drv)
533 struct bus_type * bus = get_bus(drv->bus);
534 int error = 0;
537 if (bus) {
538 pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
539 error = kobject_set_name(&drv->kobj, drv->name);
540 if (error) {
541 put_bus(bus);
542 return error;
544 drv->kobj.kset = &bus->drivers;
545 if ((error = kobject_register(&drv->kobj))) {
546 put_bus(bus);
547 return error;
551 down_write(&bus->subsys.rwsem);
552 driver_attach(drv);
555 up_write(&bus->subsys.rwsem);
557 driver_add_attrs(bus, drv);
559 return error;
564 * bus_remove_driver - delete driver from bus's knowledge.
565 * @drv: driver.
567 * Detach the driver from the devices it controls, and remove
568 * it from its bus's list of drivers. Finally, we drop the reference
569 * to the bus we took in bus_add_driver().
572 void bus_remove_driver(struct device_driver * drv)
574 if (drv->bus) {
575 driver_remove_attrs(drv->bus, drv);
576 down_write(&drv->bus->subsys.rwsem);
577 pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
579 driver_detach(drv);
582 up_write(&drv->bus->subsys.rwsem);
583 kobject_unregister(&drv->kobj);
584 put_bus(drv->bus);
591 /* Helper for bus_rescan_devices's iter */
592 static int bus_rescan_devices_helper(struct device *dev, void *data)
594 int *count = data;
596 if (!dev->driver && device_attach(dev))
597 (*count)++;
601 return 0;
606 * bus_rescan_devices - rescan devices on the bus for possible drivers
607 * @bus: the bus to scan.
609 * This function will look for devices on the bus with no driver
610 * attached and rescan it against existing drivers to see if it
611 * matches any. Calls device_attach(). Returns the number of devices
612 * that were sucessfully bound to a driver.
614 int bus_rescan_devices(struct bus_type * bus)
616 int count = 0;
618 bus_for_each_dev(bus, NULL, &count, bus_rescan_devices_helper);
620 return count;
624 struct bus_type * get_bus(struct bus_type * bus)
626 return bus ? container_of(subsys_get(&bus->subsys), struct bus_type, subsys) : NULL;
629 void put_bus(struct bus_type * bus)
631 subsys_put(&bus->subsys);
636 * find_bus - locate bus by name.
637 * @name: name of bus.
639 * Call kset_find_obj() to iterate over list of buses to
640 * find a bus by name. Return bus if found.
642 * Note that kset_find_obj increments bus' reference count.
645 struct bus_type * find_bus(char * name)
647 struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
648 return k ? to_bus(k) : NULL;
653 * bus_add_attrs - Add default attributes for this bus.
654 * @bus: Bus that has just been registered.
657 static int bus_add_attrs(struct bus_type * bus)
659 int error = 0;
660 int i;
662 if (bus->bus_attrs) {
663 for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
664 if ((error = bus_create_file(bus,&bus->bus_attrs[i])))
665 goto Err;
668 Done:
669 return error;
670 Err:
671 while (--i >= 0)
672 bus_remove_file(bus,&bus->bus_attrs[i]);
673 goto Done;
676 static void bus_remove_attrs(struct bus_type * bus)
678 int i;
680 if (bus->bus_attrs) {
681 for (i = 0; attr_name(bus->bus_attrs[i]); i++)
682 bus_remove_file(bus,&bus->bus_attrs[i]);
687 * bus_register - register a bus with the system.
688 * @bus: bus.
690 * Once we have that, we registered the bus with the kobject
691 * infrastructure, then register the children subsystems it has:
692 * the devices and drivers that belong to the bus.
694 int bus_register(struct bus_type * bus)
696 int retval;
698 retval = kobject_set_name(&bus->subsys.kset.kobj, bus->name);
699 if (retval)
700 goto out;
702 subsys_set_kset(bus, bus_subsys);
703 retval = subsystem_register(&bus->subsys);
704 if (retval)
705 goto out;
707 kobject_set_name(&bus->devices.kobj, "devices");
708 bus->devices.subsys = &bus->subsys;
709 retval = kset_register(&bus->devices);
710 if (retval)
711 goto bus_devices_fail;
713 kobject_set_name(&bus->drivers.kobj, "drivers");
714 bus->drivers.subsys = &bus->subsys;
715 bus->drivers.ktype = &ktype_driver;
716 retval = kset_register(&bus->drivers);
717 if (retval)
718 goto bus_drivers_fail;
719 bus_add_attrs(bus);
721 pr_debug("bus type '%s' registered\n", bus->name);
722 return 0;
724 bus_drivers_fail:
725 kset_unregister(&bus->devices);
726 bus_devices_fail:
727 subsystem_unregister(&bus->subsys);
728 out:
729 return retval;
734 * bus_unregister - remove a bus from the system
735 * @bus: bus.
737 * Unregister the child subsystems and the bus itself.
738 * Finally, we call put_bus() to release the refcount
740 void bus_unregister(struct bus_type * bus)
742 pr_debug("bus %s: unregistering\n", bus->name);
743 bus_remove_attrs(bus);
744 kset_unregister(&bus->drivers);
745 kset_unregister(&bus->devices);
746 subsystem_unregister(&bus->subsys);
749 int __init buses_init(void)
751 return subsystem_register(&bus_subsys);
755 EXPORT_SYMBOL(bus_for_each_dev);
756 EXPORT_SYMBOL(bus_for_each_drv);
758 EXPORT_SYMBOL(device_bind_driver);
759 EXPORT_SYMBOL(device_release_driver);
761 EXPORT_SYMBOL(bus_add_device);
762 EXPORT_SYMBOL(bus_remove_device);
763 EXPORT_SYMBOL(bus_register);
764 EXPORT_SYMBOL(bus_unregister);
765 EXPORT_SYMBOL(bus_rescan_devices);
766 EXPORT_SYMBOL(get_bus);
767 EXPORT_SYMBOL(put_bus);
768 EXPORT_SYMBOL(find_bus);
770 EXPORT_SYMBOL(bus_create_file);
771 EXPORT_SYMBOL(bus_remove_file);