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 |
Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 6 | * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de> |
| 7 | * Copyright (c) 2007 Novell Inc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * This file is released under the GPLv2 |
| 10 | * |
| 11 | */ |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/device.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/string.h> |
| 17 | #include "base.h" |
| 18 | |
| 19 | #define to_dev(node) container_of(node, struct device, driver_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
mochel@digitalimplant.org | fae3cd0 | 2005-03-21 10:59:56 -0800 | [diff] [blame] | 21 | |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 22 | static struct device *next_device(struct klist_iter *i) |
mochel@digitalimplant.org | 94e7b1c5 | 2005-03-21 12:25:36 -0800 | [diff] [blame] | 23 | { |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 24 | struct klist_node *n = klist_next(i); |
mochel@digitalimplant.org | 94e7b1c5 | 2005-03-21 12:25:36 -0800 | [diff] [blame] | 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 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 29 | * driver_for_each_device - Iterator for devices bound to a driver. |
| 30 | * @drv: Driver we're iterating. |
| 31 | * @start: Device to begin with |
| 32 | * @data: Data to pass to the callback. |
| 33 | * @fn: Function to call for each device. |
mochel@digitalimplant.org | fae3cd0 | 2005-03-21 10:59:56 -0800 | [diff] [blame] | 34 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -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 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 37 | int driver_for_each_device(struct device_driver *drv, struct device *start, |
| 38 | void *data, int (*fn)(struct device *, void *)) |
mochel@digitalimplant.org | fae3cd0 | 2005-03-21 10:59:56 -0800 | [diff] [blame] | 39 | { |
mochel@digitalimplant.org | 94e7b1c5 | 2005-03-21 12:25:36 -0800 | [diff] [blame] | 40 | struct klist_iter i; |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 41 | struct device *dev; |
mochel@digitalimplant.org | fae3cd0 | 2005-03-21 10:59:56 -0800 | [diff] [blame] | 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 | |
Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 47 | klist_iter_init_node(&drv->p->klist_devices, &i, |
mochel@digitalimplant.org | 94e7b1c5 | 2005-03-21 12:25:36 -0800 | [diff] [blame] | 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 | } |
gregkh@suse.de | 126eddf | 2005-03-22 12:17:13 -0800 | [diff] [blame] | 54 | EXPORT_SYMBOL_GPL(driver_for_each_device); |
mochel@digitalimplant.org | fae3cd0 | 2005-03-21 10:59:56 -0800 | [diff] [blame] | 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | /** |
Cornelia Huck | 0edb586 | 2005-06-22 16:59:51 +0200 | [diff] [blame] | 57 | * driver_find_device - device iterator for locating a particular device. |
Randy Dunlap | c41455f | 2005-10-23 11:59:14 -0700 | [diff] [blame] | 58 | * @drv: The device's driver |
Cornelia Huck | 0edb586 | 2005-06-22 16:59:51 +0200 | [diff] [blame] | 59 | * @start: Device to begin with |
| 60 | * @data: Data to pass to match function |
| 61 | * @match: Callback function to check device |
| 62 | * |
| 63 | * This is similar to the driver_for_each_device() function above, but |
| 64 | * it returns a reference to a device that is 'found' for later use, as |
| 65 | * determined by the @match callback. |
| 66 | * |
| 67 | * The callback should return 0 if the device doesn't match and non-zero |
| 68 | * if it does. If the callback returns non-zero, this function will |
| 69 | * return to the caller and not iterate over any more devices. |
| 70 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 71 | struct device *driver_find_device(struct device_driver *drv, |
| 72 | struct device *start, void *data, |
| 73 | int (*match)(struct device *dev, void *data)) |
Cornelia Huck | 0edb586 | 2005-06-22 16:59:51 +0200 | [diff] [blame] | 74 | { |
| 75 | struct klist_iter i; |
| 76 | struct device *dev; |
| 77 | |
| 78 | if (!drv) |
| 79 | return NULL; |
| 80 | |
Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 81 | klist_iter_init_node(&drv->p->klist_devices, &i, |
Cornelia Huck | 0edb586 | 2005-06-22 16:59:51 +0200 | [diff] [blame] | 82 | (start ? &start->knode_driver : NULL)); |
| 83 | while ((dev = next_device(&i))) |
| 84 | if (match(dev, data) && get_device(dev)) |
| 85 | break; |
| 86 | klist_iter_exit(&i); |
| 87 | return dev; |
| 88 | } |
| 89 | EXPORT_SYMBOL_GPL(driver_find_device); |
| 90 | |
| 91 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 92 | * driver_create_file - create sysfs file for driver. |
| 93 | * @drv: driver. |
| 94 | * @attr: driver attribute descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 96 | int driver_create_file(struct device_driver *drv, |
| 97 | struct driver_attribute *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { |
| 99 | int error; |
Cornelia Huck | 0c98b19 | 2008-01-31 10:39:38 +0100 | [diff] [blame] | 100 | if (drv) |
Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 101 | error = sysfs_create_file(&drv->p->kobj, &attr->attr); |
Cornelia Huck | 0c98b19 | 2008-01-31 10:39:38 +0100 | [diff] [blame] | 102 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | error = -EINVAL; |
| 104 | return error; |
| 105 | } |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 106 | EXPORT_SYMBOL_GPL(driver_create_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 109 | * driver_remove_file - remove sysfs file for driver. |
| 110 | * @drv: driver. |
| 111 | * @attr: driver attribute descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 113 | void driver_remove_file(struct device_driver *drv, |
| 114 | struct driver_attribute *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
Cornelia Huck | 0c98b19 | 2008-01-31 10:39:38 +0100 | [diff] [blame] | 116 | if (drv) |
Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 117 | sysfs_remove_file(&drv->p->kobj, &attr->attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 119 | EXPORT_SYMBOL_GPL(driver_remove_file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
| 121 | /** |
Greg Kroah-Hartman | cbe9c59 | 2007-12-19 15:54:39 -0400 | [diff] [blame] | 122 | * driver_add_kobj - add a kobject below the specified driver |
Randy Dunlap | 7199677 | 2008-02-18 13:09:03 -0800 | [diff] [blame] | 123 | * @drv: requesting device driver |
| 124 | * @kobj: kobject to add below this driver |
| 125 | * @fmt: format string that names the kobject |
Greg Kroah-Hartman | cbe9c59 | 2007-12-19 15:54:39 -0400 | [diff] [blame] | 126 | * |
| 127 | * You really don't want to do this, this is only here due to one looney |
| 128 | * iseries driver, go poke those developers if you are annoyed about |
| 129 | * this... |
| 130 | */ |
| 131 | int driver_add_kobj(struct device_driver *drv, struct kobject *kobj, |
| 132 | const char *fmt, ...) |
| 133 | { |
| 134 | va_list args; |
| 135 | char *name; |
Jesper Juhl | d478376 | 2008-03-28 14:16:12 -0700 | [diff] [blame] | 136 | int ret; |
Greg Kroah-Hartman | cbe9c59 | 2007-12-19 15:54:39 -0400 | [diff] [blame] | 137 | |
| 138 | va_start(args, fmt); |
| 139 | name = kvasprintf(GFP_KERNEL, fmt, args); |
| 140 | va_end(args); |
| 141 | |
| 142 | if (!name) |
| 143 | return -ENOMEM; |
| 144 | |
Jesper Juhl | d478376 | 2008-03-28 14:16:12 -0700 | [diff] [blame] | 145 | ret = kobject_add(kobj, &drv->p->kobj, "%s", name); |
| 146 | kfree(name); |
| 147 | return ret; |
Greg Kroah-Hartman | cbe9c59 | 2007-12-19 15:54:39 -0400 | [diff] [blame] | 148 | } |
| 149 | EXPORT_SYMBOL_GPL(driver_add_kobj); |
| 150 | |
| 151 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 152 | * get_driver - increment driver reference count. |
| 153 | * @drv: driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 155 | struct device_driver *get_driver(struct device_driver *drv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 157 | if (drv) { |
| 158 | struct driver_private *priv; |
| 159 | struct kobject *kobj; |
| 160 | |
| 161 | kobj = kobject_get(&drv->p->kobj); |
| 162 | priv = to_driver(kobj); |
| 163 | return priv->driver; |
| 164 | } |
| 165 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 167 | EXPORT_SYMBOL_GPL(get_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
| 169 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 170 | * put_driver - decrement driver's refcount. |
| 171 | * @drv: driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 173 | void put_driver(struct device_driver *drv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 175 | kobject_put(&drv->p->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 177 | EXPORT_SYMBOL_GPL(put_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Cornelia Huck | 57c7453 | 2007-12-05 12:50:23 +0100 | [diff] [blame] | 179 | static int driver_add_groups(struct device_driver *drv, |
| 180 | struct attribute_group **groups) |
| 181 | { |
| 182 | int error = 0; |
| 183 | int i; |
| 184 | |
| 185 | if (groups) { |
| 186 | for (i = 0; groups[i]; i++) { |
Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 187 | error = sysfs_create_group(&drv->p->kobj, groups[i]); |
Cornelia Huck | 57c7453 | 2007-12-05 12:50:23 +0100 | [diff] [blame] | 188 | if (error) { |
| 189 | while (--i >= 0) |
Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 190 | sysfs_remove_group(&drv->p->kobj, |
Cornelia Huck | 57c7453 | 2007-12-05 12:50:23 +0100 | [diff] [blame] | 191 | groups[i]); |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | return error; |
| 197 | } |
| 198 | |
| 199 | static void driver_remove_groups(struct device_driver *drv, |
| 200 | struct attribute_group **groups) |
| 201 | { |
| 202 | int i; |
| 203 | |
| 204 | if (groups) |
| 205 | for (i = 0; groups[i]; i++) |
Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 206 | sysfs_remove_group(&drv->p->kobj, groups[i]); |
Cornelia Huck | 57c7453 | 2007-12-05 12:50:23 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 210 | * driver_register - register driver with bus |
| 211 | * @drv: driver to register |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 213 | * We pass off most of the work to the bus_add_driver() call, |
| 214 | * since most of the things we have to do deal with the bus |
| 215 | * structures. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 217 | int driver_register(struct device_driver *drv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
Cornelia Huck | 57c7453 | 2007-12-05 12:50:23 +0100 | [diff] [blame] | 219 | int ret; |
| 220 | |
Russell King | 594c828 | 2006-01-05 14:29:51 +0000 | [diff] [blame] | 221 | if ((drv->bus->probe && drv->probe) || |
| 222 | (drv->bus->remove && drv->remove) || |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 223 | (drv->bus->shutdown && drv->shutdown)) |
| 224 | printk(KERN_WARNING "Driver '%s' needs updating - please use " |
| 225 | "bus_type methods\n", drv->name); |
Cornelia Huck | 57c7453 | 2007-12-05 12:50:23 +0100 | [diff] [blame] | 226 | ret = bus_add_driver(drv); |
| 227 | if (ret) |
| 228 | return ret; |
| 229 | ret = driver_add_groups(drv, drv->groups); |
| 230 | if (ret) |
| 231 | bus_remove_driver(drv); |
| 232 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 234 | EXPORT_SYMBOL_GPL(driver_register); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 237 | * driver_unregister - remove driver from system. |
| 238 | * @drv: driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 240 | * 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] | 241 | */ |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 242 | void driver_unregister(struct device_driver *drv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | { |
Cornelia Huck | 57c7453 | 2007-12-05 12:50:23 +0100 | [diff] [blame] | 244 | driver_remove_groups(drv, drv->groups); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | bus_remove_driver(drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 247 | EXPORT_SYMBOL_GPL(driver_unregister); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
| 249 | /** |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 250 | * driver_find - locate driver on a bus by its name. |
| 251 | * @name: name of the driver. |
| 252 | * @bus: bus to scan for the driver. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 254 | * Call kset_find_obj() to iterate over list of drivers on |
| 255 | * a bus to find driver by name. Return driver if found. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | * |
Greg Kroah-Hartman | 4a3ad20 | 2008-01-24 22:50:12 -0800 | [diff] [blame] | 257 | * Note that kset_find_obj increments driver's reference count. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | */ |
| 259 | struct device_driver *driver_find(const char *name, struct bus_type *bus) |
| 260 | { |
Greg Kroah-Hartman | c6f7e72 | 2007-11-01 19:41:16 -0700 | [diff] [blame] | 261 | struct kobject *k = kset_find_obj(bus->p->drivers_kset, name); |
Greg Kroah-Hartman | e5dd127 | 2007-11-28 15:59:15 -0800 | [diff] [blame] | 262 | struct driver_private *priv; |
| 263 | |
| 264 | if (k) { |
| 265 | priv = to_driver(k); |
| 266 | return priv->driver; |
| 267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | return NULL; |
| 269 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | EXPORT_SYMBOL_GPL(driver_find); |