Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * driver.c - centralized device 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/string.h> |
| 16 | #include "base.h" |
| 17 | |
| 18 | #define to_dev(node) container_of(node, struct device, driver_list) |
| 19 | #define to_drv(obj) container_of(obj, struct device_driver, kobj) |
| 20 | |
mochel@digitalimplant.org | fae3cd0 | 2005-03-21 10:59:56 -0800 | [diff] [blame^] | 21 | |
| 22 | /** |
| 23 | * driver_for_each_device - Iterator for devices bound to a driver. |
| 24 | * @drv: Driver we're iterating. |
| 25 | * @data: Data to pass to the callback. |
| 26 | * @fn: Function to call for each device. |
| 27 | * |
| 28 | * Take the bus's rwsem and iterate over the @drv's list of devices, |
| 29 | * calling @fn for each one. |
| 30 | */ |
| 31 | |
| 32 | int driver_for_each_device(struct device_driver * drv, struct device * start, |
| 33 | void * data, int (*fn)(struct device *, void *)) |
| 34 | { |
| 35 | struct list_head * head; |
| 36 | struct device * dev; |
| 37 | int error = 0; |
| 38 | |
| 39 | down_read(&drv->bus->subsys.rwsem); |
| 40 | head = &drv->devices; |
| 41 | dev = list_prepare_entry(start, head, driver_list); |
| 42 | list_for_each_entry_continue(dev, head, driver_list) { |
| 43 | get_device(dev); |
| 44 | error = fn(dev, data); |
| 45 | put_device(dev); |
| 46 | if (error) |
| 47 | break; |
| 48 | } |
| 49 | up_read(&drv->bus->subsys.rwsem); |
| 50 | return error; |
| 51 | } |
| 52 | |
| 53 | EXPORT_SYMBOL(driver_for_each_device); |
| 54 | |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | /** |
| 57 | * driver_create_file - create sysfs file for driver. |
| 58 | * @drv: driver. |
| 59 | * @attr: driver attribute descriptor. |
| 60 | */ |
| 61 | |
| 62 | int driver_create_file(struct device_driver * drv, struct driver_attribute * attr) |
| 63 | { |
| 64 | int error; |
| 65 | if (get_driver(drv)) { |
| 66 | error = sysfs_create_file(&drv->kobj, &attr->attr); |
| 67 | put_driver(drv); |
| 68 | } else |
| 69 | error = -EINVAL; |
| 70 | return error; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /** |
| 75 | * driver_remove_file - remove sysfs file for driver. |
| 76 | * @drv: driver. |
| 77 | * @attr: driver attribute descriptor. |
| 78 | */ |
| 79 | |
| 80 | void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr) |
| 81 | { |
| 82 | if (get_driver(drv)) { |
| 83 | sysfs_remove_file(&drv->kobj, &attr->attr); |
| 84 | put_driver(drv); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /** |
| 90 | * get_driver - increment driver reference count. |
| 91 | * @drv: driver. |
| 92 | */ |
| 93 | struct device_driver * get_driver(struct device_driver * drv) |
| 94 | { |
| 95 | return drv ? to_drv(kobject_get(&drv->kobj)) : NULL; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /** |
| 100 | * put_driver - decrement driver's refcount. |
| 101 | * @drv: driver. |
| 102 | */ |
| 103 | void put_driver(struct device_driver * drv) |
| 104 | { |
| 105 | kobject_put(&drv->kobj); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | /** |
| 110 | * driver_register - register driver with bus |
| 111 | * @drv: driver to register |
| 112 | * |
| 113 | * We pass off most of the work to the bus_add_driver() call, |
| 114 | * since most of the things we have to do deal with the bus |
| 115 | * structures. |
| 116 | * |
| 117 | * The one interesting aspect is that we setup @drv->unloaded |
| 118 | * as a completion that gets complete when the driver reference |
| 119 | * count reaches 0. |
| 120 | */ |
| 121 | int driver_register(struct device_driver * drv) |
| 122 | { |
| 123 | INIT_LIST_HEAD(&drv->devices); |
| 124 | init_completion(&drv->unloaded); |
| 125 | return bus_add_driver(drv); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | /** |
| 130 | * driver_unregister - remove driver from system. |
| 131 | * @drv: driver. |
| 132 | * |
| 133 | * Again, we pass off most of the work to the bus-level call. |
| 134 | * |
| 135 | * Though, once that is done, we wait until @drv->unloaded is completed. |
| 136 | * This will block until the driver refcount reaches 0, and it is |
| 137 | * released. Only modular drivers will call this function, and we |
| 138 | * have to guarantee that it won't complete, letting the driver |
| 139 | * unload until all references are gone. |
| 140 | */ |
| 141 | |
| 142 | void driver_unregister(struct device_driver * drv) |
| 143 | { |
| 144 | bus_remove_driver(drv); |
| 145 | wait_for_completion(&drv->unloaded); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * driver_find - locate driver on a bus by its name. |
| 150 | * @name: name of the driver. |
| 151 | * @bus: bus to scan for the driver. |
| 152 | * |
| 153 | * Call kset_find_obj() to iterate over list of drivers on |
| 154 | * a bus to find driver by name. Return driver if found. |
| 155 | * |
| 156 | * Note that kset_find_obj increments driver's reference count. |
| 157 | */ |
| 158 | struct device_driver *driver_find(const char *name, struct bus_type *bus) |
| 159 | { |
| 160 | struct kobject *k = kset_find_obj(&bus->drivers, name); |
| 161 | if (k) |
| 162 | return to_drv(k); |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | EXPORT_SYMBOL_GPL(driver_register); |
| 167 | EXPORT_SYMBOL_GPL(driver_unregister); |
| 168 | EXPORT_SYMBOL_GPL(get_driver); |
| 169 | EXPORT_SYMBOL_GPL(put_driver); |
| 170 | EXPORT_SYMBOL_GPL(driver_find); |
| 171 | |
| 172 | EXPORT_SYMBOL_GPL(driver_create_file); |
| 173 | EXPORT_SYMBOL_GPL(driver_remove_file); |