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 | |
mochel@digitalimplant.org | 94e7b1c | 2005-03-21 12:25:36 -0800 | [diff] [blame] | 22 | static struct device * next_device(struct klist_iter * i) |
| 23 | { |
| 24 | struct klist_node * n = klist_next(i); |
| 25 | return n ? container_of(n, struct device, knode_driver) : NULL; |
| 26 | } |
| 27 | |
mochel@digitalimplant.org | fae3cd0 | 2005-03-21 10:59:56 -0800 | [diff] [blame] | 28 | /** |
| 29 | * driver_for_each_device - Iterator for devices bound to a driver. |
| 30 | * @drv: Driver we're iterating. |
Randy Dunlap | c41455f | 2005-10-23 11:59:14 -0700 | [diff] [blame] | 31 | * @start: Device to begin with |
mochel@digitalimplant.org | fae3cd0 | 2005-03-21 10:59:56 -0800 | [diff] [blame] | 32 | * @data: Data to pass to the callback. |
| 33 | * @fn: Function to call for each device. |
| 34 | * |
mochel@digitalimplant.org | 4d12d2d | 2005-03-24 20:08:04 -0800 | [diff] [blame] | 35 | * Iterate over the @drv's list of devices calling @fn for each one. |
mochel@digitalimplant.org | fae3cd0 | 2005-03-21 10:59:56 -0800 | [diff] [blame] | 36 | */ |
| 37 | |
| 38 | int driver_for_each_device(struct device_driver * drv, struct device * start, |
| 39 | void * data, int (*fn)(struct device *, void *)) |
| 40 | { |
mochel@digitalimplant.org | 94e7b1c | 2005-03-21 12:25:36 -0800 | [diff] [blame] | 41 | struct klist_iter i; |
mochel@digitalimplant.org | fae3cd0 | 2005-03-21 10:59:56 -0800 | [diff] [blame] | 42 | struct device * dev; |
| 43 | int error = 0; |
| 44 | |
mochel@digitalimplant.org | 94e7b1c | 2005-03-21 12:25:36 -0800 | [diff] [blame] | 45 | if (!drv) |
| 46 | return -EINVAL; |
| 47 | |
| 48 | klist_iter_init_node(&drv->klist_devices, &i, |
| 49 | start ? &start->knode_driver : NULL); |
| 50 | while ((dev = next_device(&i)) && !error) |
mochel@digitalimplant.org | fae3cd0 | 2005-03-21 10:59:56 -0800 | [diff] [blame] | 51 | error = fn(dev, data); |
mochel@digitalimplant.org | 94e7b1c | 2005-03-21 12:25:36 -0800 | [diff] [blame] | 52 | klist_iter_exit(&i); |
mochel@digitalimplant.org | fae3cd0 | 2005-03-21 10:59:56 -0800 | [diff] [blame] | 53 | return error; |
| 54 | } |
| 55 | |
gregkh@suse.de | 126eddf | 2005-03-22 12:17:13 -0800 | [diff] [blame] | 56 | EXPORT_SYMBOL_GPL(driver_for_each_device); |
mochel@digitalimplant.org | fae3cd0 | 2005-03-21 10:59:56 -0800 | [diff] [blame] | 57 | |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | /** |
Cornelia Huck | 0edb586 | 2005-06-22 16:59:51 +0200 | [diff] [blame] | 60 | * driver_find_device - device iterator for locating a particular device. |
Randy Dunlap | c41455f | 2005-10-23 11:59:14 -0700 | [diff] [blame] | 61 | * @drv: The device's driver |
Cornelia Huck | 0edb586 | 2005-06-22 16:59:51 +0200 | [diff] [blame] | 62 | * @start: Device to begin with |
| 63 | * @data: Data to pass to match function |
| 64 | * @match: Callback function to check device |
| 65 | * |
| 66 | * This is similar to the driver_for_each_device() function above, but |
| 67 | * it returns a reference to a device that is 'found' for later use, as |
| 68 | * determined by the @match callback. |
| 69 | * |
| 70 | * The callback should return 0 if the device doesn't match and non-zero |
| 71 | * if it does. If the callback returns non-zero, this function will |
| 72 | * return to the caller and not iterate over any more devices. |
| 73 | */ |
| 74 | struct device * driver_find_device(struct device_driver *drv, |
| 75 | struct device * start, void * data, |
| 76 | int (*match)(struct device *, void *)) |
| 77 | { |
| 78 | struct klist_iter i; |
| 79 | struct device *dev; |
| 80 | |
| 81 | if (!drv) |
| 82 | return NULL; |
| 83 | |
| 84 | klist_iter_init_node(&drv->klist_devices, &i, |
| 85 | (start ? &start->knode_driver : NULL)); |
| 86 | while ((dev = next_device(&i))) |
| 87 | if (match(dev, data) && get_device(dev)) |
| 88 | break; |
| 89 | klist_iter_exit(&i); |
| 90 | return dev; |
| 91 | } |
| 92 | EXPORT_SYMBOL_GPL(driver_find_device); |
| 93 | |
| 94 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | * driver_create_file - create sysfs file for driver. |
| 96 | * @drv: driver. |
| 97 | * @attr: driver attribute descriptor. |
| 98 | */ |
| 99 | |
| 100 | int driver_create_file(struct device_driver * drv, struct driver_attribute * attr) |
| 101 | { |
| 102 | int error; |
| 103 | if (get_driver(drv)) { |
| 104 | error = sysfs_create_file(&drv->kobj, &attr->attr); |
| 105 | put_driver(drv); |
| 106 | } else |
| 107 | error = -EINVAL; |
| 108 | return error; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * driver_remove_file - remove sysfs file for driver. |
| 114 | * @drv: driver. |
| 115 | * @attr: driver attribute descriptor. |
| 116 | */ |
| 117 | |
| 118 | void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr) |
| 119 | { |
| 120 | if (get_driver(drv)) { |
| 121 | sysfs_remove_file(&drv->kobj, &attr->attr); |
| 122 | put_driver(drv); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /** |
| 128 | * get_driver - increment driver reference count. |
| 129 | * @drv: driver. |
| 130 | */ |
| 131 | struct device_driver * get_driver(struct device_driver * drv) |
| 132 | { |
| 133 | return drv ? to_drv(kobject_get(&drv->kobj)) : NULL; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /** |
| 138 | * put_driver - decrement driver's refcount. |
| 139 | * @drv: driver. |
| 140 | */ |
| 141 | void put_driver(struct device_driver * drv) |
| 142 | { |
| 143 | kobject_put(&drv->kobj); |
| 144 | } |
| 145 | |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 146 | static void klist_devices_get(struct klist_node *n) |
| 147 | { |
| 148 | struct device *dev = container_of(n, struct device, knode_driver); |
| 149 | |
| 150 | get_device(dev); |
| 151 | } |
| 152 | |
| 153 | static void klist_devices_put(struct klist_node *n) |
| 154 | { |
| 155 | struct device *dev = container_of(n, struct device, knode_driver); |
| 156 | |
| 157 | put_device(dev); |
| 158 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
| 160 | /** |
| 161 | * driver_register - register driver with bus |
| 162 | * @drv: driver to register |
| 163 | * |
| 164 | * We pass off most of the work to the bus_add_driver() call, |
| 165 | * since most of the things we have to do deal with the bus |
| 166 | * structures. |
| 167 | * |
| 168 | * The one interesting aspect is that we setup @drv->unloaded |
| 169 | * as a completion that gets complete when the driver reference |
| 170 | * count reaches 0. |
| 171 | */ |
| 172 | int driver_register(struct device_driver * drv) |
| 173 | { |
Russell King | 594c828 | 2006-01-05 14:29:51 +0000 | [diff] [blame] | 174 | if ((drv->bus->probe && drv->probe) || |
| 175 | (drv->bus->remove && drv->remove) || |
| 176 | (drv->bus->shutdown && drv->shutdown)) { |
| 177 | printk(KERN_WARNING "Driver '%s' needs updating - please use bus_type methods\n", drv->name); |
| 178 | } |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 179 | klist_init(&drv->klist_devices, klist_devices_get, klist_devices_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | init_completion(&drv->unloaded); |
| 181 | return bus_add_driver(drv); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | /** |
| 186 | * driver_unregister - remove driver from system. |
| 187 | * @drv: driver. |
| 188 | * |
| 189 | * Again, we pass off most of the work to the bus-level call. |
| 190 | * |
| 191 | * Though, once that is done, we wait until @drv->unloaded is completed. |
| 192 | * This will block until the driver refcount reaches 0, and it is |
| 193 | * released. Only modular drivers will call this function, and we |
| 194 | * have to guarantee that it won't complete, letting the driver |
| 195 | * unload until all references are gone. |
| 196 | */ |
| 197 | |
| 198 | void driver_unregister(struct device_driver * drv) |
| 199 | { |
| 200 | bus_remove_driver(drv); |
| 201 | wait_for_completion(&drv->unloaded); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * driver_find - locate driver on a bus by its name. |
| 206 | * @name: name of the driver. |
| 207 | * @bus: bus to scan for the driver. |
| 208 | * |
| 209 | * Call kset_find_obj() to iterate over list of drivers on |
| 210 | * a bus to find driver by name. Return driver if found. |
| 211 | * |
| 212 | * Note that kset_find_obj increments driver's reference count. |
| 213 | */ |
| 214 | struct device_driver *driver_find(const char *name, struct bus_type *bus) |
| 215 | { |
| 216 | struct kobject *k = kset_find_obj(&bus->drivers, name); |
| 217 | if (k) |
| 218 | return to_drv(k); |
| 219 | return NULL; |
| 220 | } |
| 221 | |
| 222 | EXPORT_SYMBOL_GPL(driver_register); |
| 223 | EXPORT_SYMBOL_GPL(driver_unregister); |
| 224 | EXPORT_SYMBOL_GPL(get_driver); |
| 225 | EXPORT_SYMBOL_GPL(put_driver); |
| 226 | EXPORT_SYMBOL_GPL(driver_find); |
| 227 | |
| 228 | EXPORT_SYMBOL_GPL(driver_create_file); |
| 229 | EXPORT_SYMBOL_GPL(driver_remove_file); |