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