Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * bus.c - bus 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/init.h> |
| 15 | #include <linux/string.h> |
| 16 | #include "base.h" |
| 17 | #include "power/power.h" |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr) |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 20 | #define to_bus(obj) container_of(obj, struct bus_type, subsys.kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | /* |
| 23 | * sysfs bindings for drivers |
| 24 | */ |
| 25 | |
| 26 | #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr) |
| 27 | #define to_driver(obj) container_of(obj, struct device_driver, kobj) |
| 28 | |
| 29 | |
Kay Sievers | b8c5cec | 2007-02-16 17:33:36 +0100 | [diff] [blame] | 30 | static int __must_check bus_rescan_devices_helper(struct device *dev, |
| 31 | void *data); |
| 32 | |
Greg Kroah-Hartman | 5901d01 | 2007-09-13 02:53:13 -0700 | [diff] [blame] | 33 | static struct bus_type *bus_get(struct bus_type *bus) |
| 34 | { |
| 35 | return bus ? container_of(kset_get(&bus->subsys), |
| 36 | struct bus_type, subsys) : NULL; |
| 37 | } |
| 38 | |
Greg Kroah-Hartman | fc1ede5 | 2007-09-13 02:53:13 -0700 | [diff] [blame] | 39 | static void bus_put(struct bus_type *bus) |
| 40 | { |
| 41 | kset_put(&bus->subsys); |
| 42 | } |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | static ssize_t |
| 45 | drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) |
| 46 | { |
| 47 | struct driver_attribute * drv_attr = to_drv_attr(attr); |
| 48 | struct device_driver * drv = to_driver(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 49 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
| 51 | if (drv_attr->show) |
| 52 | ret = drv_attr->show(drv, buf); |
| 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | static ssize_t |
| 57 | drv_attr_store(struct kobject * kobj, struct attribute * attr, |
| 58 | const char * buf, size_t count) |
| 59 | { |
| 60 | struct driver_attribute * drv_attr = to_drv_attr(attr); |
| 61 | struct device_driver * drv = to_driver(kobj); |
Dmitry Torokhov | 4a0c20b | 2005-04-29 01:23:47 -0500 | [diff] [blame] | 62 | ssize_t ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | if (drv_attr->store) |
| 65 | ret = drv_attr->store(drv, buf, count); |
| 66 | return ret; |
| 67 | } |
| 68 | |
| 69 | static struct sysfs_ops driver_sysfs_ops = { |
| 70 | .show = drv_attr_show, |
| 71 | .store = drv_attr_store, |
| 72 | }; |
| 73 | |
| 74 | |
| 75 | static void driver_release(struct kobject * kobj) |
| 76 | { |
Greg Kroah-Hartman | 74e9f5f | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 77 | /* |
| 78 | * Yes this is an empty release function, it is this way because struct |
| 79 | * device is always a static object, not a dynamic one. Yes, this is |
| 80 | * not nice and bad, but remember, drivers are code, reference counted |
| 81 | * by the module count, not a device, which is really data. And yes, |
| 82 | * in the future I do want to have all drivers be created dynamically, |
| 83 | * and am working toward that goal, but it will take a bit longer... |
| 84 | * |
| 85 | * But do not let this example give _anyone_ the idea that they can |
| 86 | * create a release function without any code in it at all, to do that |
| 87 | * is almost always wrong. If you have any questions about this, |
| 88 | * please send an email to <greg@kroah.com> |
| 89 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Greg Kroah-Hartman | a1148fb | 2007-10-11 10:47:49 -0600 | [diff] [blame] | 92 | static struct kobj_type driver_ktype = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | .sysfs_ops = &driver_sysfs_ops, |
| 94 | .release = driver_release, |
| 95 | }; |
| 96 | |
| 97 | |
| 98 | /* |
| 99 | * sysfs bindings for buses |
| 100 | */ |
| 101 | |
| 102 | |
| 103 | static ssize_t |
| 104 | bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) |
| 105 | { |
| 106 | struct bus_attribute * bus_attr = to_bus_attr(attr); |
| 107 | struct bus_type * bus = to_bus(kobj); |
| 108 | ssize_t ret = 0; |
| 109 | |
| 110 | if (bus_attr->show) |
| 111 | ret = bus_attr->show(bus, buf); |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | static ssize_t |
| 116 | bus_attr_store(struct kobject * kobj, struct attribute * attr, |
| 117 | const char * buf, size_t count) |
| 118 | { |
| 119 | struct bus_attribute * bus_attr = to_bus_attr(attr); |
| 120 | struct bus_type * bus = to_bus(kobj); |
| 121 | ssize_t ret = 0; |
| 122 | |
| 123 | if (bus_attr->store) |
| 124 | ret = bus_attr->store(bus, buf, count); |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | static struct sysfs_ops bus_sysfs_ops = { |
| 129 | .show = bus_attr_show, |
| 130 | .store = bus_attr_store, |
| 131 | }; |
| 132 | |
| 133 | int bus_create_file(struct bus_type * bus, struct bus_attribute * attr) |
| 134 | { |
| 135 | int error; |
Greg Kroah-Hartman | 5901d01 | 2007-09-13 02:53:13 -0700 | [diff] [blame] | 136 | if (bus_get(bus)) { |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 137 | error = sysfs_create_file(&bus->subsys.kobj, &attr->attr); |
Greg Kroah-Hartman | fc1ede5 | 2007-09-13 02:53:13 -0700 | [diff] [blame] | 138 | bus_put(bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } else |
| 140 | error = -EINVAL; |
| 141 | return error; |
| 142 | } |
| 143 | |
| 144 | void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr) |
| 145 | { |
Greg Kroah-Hartman | 5901d01 | 2007-09-13 02:53:13 -0700 | [diff] [blame] | 146 | if (bus_get(bus)) { |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 147 | sysfs_remove_file(&bus->subsys.kobj, &attr->attr); |
Greg Kroah-Hartman | fc1ede5 | 2007-09-13 02:53:13 -0700 | [diff] [blame] | 148 | bus_put(bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Kay Sievers | 80f03e3 | 2007-05-26 11:21:36 +0200 | [diff] [blame] | 152 | static struct kobj_type bus_ktype = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | .sysfs_ops = &bus_sysfs_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
Kay Sievers | 80f03e3 | 2007-05-26 11:21:36 +0200 | [diff] [blame] | 156 | static int bus_uevent_filter(struct kset *kset, struct kobject *kobj) |
| 157 | { |
| 158 | struct kobj_type *ktype = get_ktype(kobj); |
| 159 | |
| 160 | if (ktype == &bus_ktype) |
| 161 | return 1; |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static struct kset_uevent_ops bus_uevent_ops = { |
| 166 | .filter = bus_uevent_filter, |
| 167 | }; |
| 168 | |
Greg Kroah-Hartman | 59a5483 | 2007-10-29 23:22:26 -0500 | [diff] [blame] | 169 | static struct kset *bus_kset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Russell King | 2139bdd | 2006-02-07 12:58:20 -0800 | [diff] [blame] | 172 | #ifdef CONFIG_HOTPLUG |
Alan Stern | 2b08c8d | 2005-11-23 15:43:50 -0800 | [diff] [blame] | 173 | /* Manually detach a device from its associated driver. */ |
Greg Kroah-Hartman | 151ef38 | 2005-06-22 16:09:05 -0700 | [diff] [blame] | 174 | static int driver_helper(struct device *dev, void *data) |
| 175 | { |
| 176 | const char *name = data; |
| 177 | |
| 178 | if (strcmp(name, dev->bus_id) == 0) |
| 179 | return 1; |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static ssize_t driver_unbind(struct device_driver *drv, |
| 184 | const char *buf, size_t count) |
| 185 | { |
Greg Kroah-Hartman | 5901d01 | 2007-09-13 02:53:13 -0700 | [diff] [blame] | 186 | struct bus_type *bus = bus_get(drv->bus); |
Greg Kroah-Hartman | 151ef38 | 2005-06-22 16:09:05 -0700 | [diff] [blame] | 187 | struct device *dev; |
| 188 | int err = -ENODEV; |
| 189 | |
| 190 | dev = bus_find_device(bus, NULL, (void *)buf, driver_helper); |
Alan Stern | 2b08c8d | 2005-11-23 15:43:50 -0800 | [diff] [blame] | 191 | if (dev && dev->driver == drv) { |
Alan Stern | bf74ad5 | 2005-11-17 16:54:12 -0500 | [diff] [blame] | 192 | if (dev->parent) /* Needed for USB */ |
| 193 | down(&dev->parent->sem); |
Greg Kroah-Hartman | 151ef38 | 2005-06-22 16:09:05 -0700 | [diff] [blame] | 194 | device_release_driver(dev); |
Alan Stern | bf74ad5 | 2005-11-17 16:54:12 -0500 | [diff] [blame] | 195 | if (dev->parent) |
| 196 | up(&dev->parent->sem); |
Greg Kroah-Hartman | 151ef38 | 2005-06-22 16:09:05 -0700 | [diff] [blame] | 197 | err = count; |
| 198 | } |
Alan Stern | 2b08c8d | 2005-11-23 15:43:50 -0800 | [diff] [blame] | 199 | put_device(dev); |
Greg Kroah-Hartman | fc1ede5 | 2007-09-13 02:53:13 -0700 | [diff] [blame] | 200 | bus_put(bus); |
Alan Stern | 2b08c8d | 2005-11-23 15:43:50 -0800 | [diff] [blame] | 201 | return err; |
Greg Kroah-Hartman | 151ef38 | 2005-06-22 16:09:05 -0700 | [diff] [blame] | 202 | } |
| 203 | static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind); |
| 204 | |
Greg Kroah-Hartman | afdce75 | 2005-06-22 16:09:05 -0700 | [diff] [blame] | 205 | /* |
| 206 | * Manually attach a device to a driver. |
| 207 | * Note: the driver must want to bind to the device, |
| 208 | * it is not possible to override the driver's id table. |
| 209 | */ |
| 210 | static ssize_t driver_bind(struct device_driver *drv, |
| 211 | const char *buf, size_t count) |
| 212 | { |
Greg Kroah-Hartman | 5901d01 | 2007-09-13 02:53:13 -0700 | [diff] [blame] | 213 | struct bus_type *bus = bus_get(drv->bus); |
Greg Kroah-Hartman | afdce75 | 2005-06-22 16:09:05 -0700 | [diff] [blame] | 214 | struct device *dev; |
| 215 | int err = -ENODEV; |
| 216 | |
| 217 | dev = bus_find_device(bus, NULL, (void *)buf, driver_helper); |
Alan Stern | 2b08c8d | 2005-11-23 15:43:50 -0800 | [diff] [blame] | 218 | if (dev && dev->driver == NULL) { |
Alan Stern | bf74ad5 | 2005-11-17 16:54:12 -0500 | [diff] [blame] | 219 | if (dev->parent) /* Needed for USB */ |
| 220 | down(&dev->parent->sem); |
Greg Kroah-Hartman | afdce75 | 2005-06-22 16:09:05 -0700 | [diff] [blame] | 221 | down(&dev->sem); |
| 222 | err = driver_probe_device(drv, dev); |
| 223 | up(&dev->sem); |
Alan Stern | bf74ad5 | 2005-11-17 16:54:12 -0500 | [diff] [blame] | 224 | if (dev->parent) |
| 225 | up(&dev->parent->sem); |
Ryan Wilson | 3722540 | 2006-03-22 16:26:25 -0500 | [diff] [blame] | 226 | |
| 227 | if (err > 0) /* success */ |
| 228 | err = count; |
| 229 | else if (err == 0) /* driver didn't accept device */ |
| 230 | err = -ENODEV; |
Greg Kroah-Hartman | afdce75 | 2005-06-22 16:09:05 -0700 | [diff] [blame] | 231 | } |
Alan Stern | 2b08c8d | 2005-11-23 15:43:50 -0800 | [diff] [blame] | 232 | put_device(dev); |
Greg Kroah-Hartman | fc1ede5 | 2007-09-13 02:53:13 -0700 | [diff] [blame] | 233 | bus_put(bus); |
Alan Stern | 2b08c8d | 2005-11-23 15:43:50 -0800 | [diff] [blame] | 234 | return err; |
Greg Kroah-Hartman | afdce75 | 2005-06-22 16:09:05 -0700 | [diff] [blame] | 235 | } |
| 236 | static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind); |
| 237 | |
Kay Sievers | b8c5cec | 2007-02-16 17:33:36 +0100 | [diff] [blame] | 238 | static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf) |
| 239 | { |
| 240 | return sprintf(buf, "%d\n", bus->drivers_autoprobe); |
| 241 | } |
| 242 | |
| 243 | static ssize_t store_drivers_autoprobe(struct bus_type *bus, |
| 244 | const char *buf, size_t count) |
| 245 | { |
| 246 | if (buf[0] == '0') |
| 247 | bus->drivers_autoprobe = 0; |
| 248 | else |
| 249 | bus->drivers_autoprobe = 1; |
| 250 | return count; |
| 251 | } |
| 252 | |
| 253 | static ssize_t store_drivers_probe(struct bus_type *bus, |
| 254 | const char *buf, size_t count) |
| 255 | { |
| 256 | struct device *dev; |
| 257 | |
| 258 | dev = bus_find_device(bus, NULL, (void *)buf, driver_helper); |
| 259 | if (!dev) |
| 260 | return -ENODEV; |
| 261 | if (bus_rescan_devices_helper(dev, NULL) != 0) |
| 262 | return -EINVAL; |
| 263 | return count; |
| 264 | } |
Russell King | 2139bdd | 2006-02-07 12:58:20 -0800 | [diff] [blame] | 265 | #endif |
Greg Kroah-Hartman | 151ef38 | 2005-06-22 16:09:05 -0700 | [diff] [blame] | 266 | |
mochel@digitalimplant.org | 465c7a3 | 2005-03-21 11:49:14 -0800 | [diff] [blame] | 267 | static struct device * next_device(struct klist_iter * i) |
| 268 | { |
| 269 | struct klist_node * n = klist_next(i); |
| 270 | return n ? container_of(n, struct device, knode_bus) : NULL; |
| 271 | } |
| 272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | /** |
| 274 | * bus_for_each_dev - device iterator. |
| 275 | * @bus: bus type. |
| 276 | * @start: device to start iterating from. |
| 277 | * @data: data for the callback. |
| 278 | * @fn: function to be called for each device. |
| 279 | * |
| 280 | * Iterate over @bus's list of devices, and call @fn for each, |
| 281 | * passing it @data. If @start is not NULL, we use that device to |
| 282 | * begin iterating from. |
| 283 | * |
| 284 | * We check the return of @fn each time. If it returns anything |
| 285 | * other than 0, we break out and return that value. |
| 286 | * |
| 287 | * NOTE: The device that returns a non-zero value is not retained |
| 288 | * in any way, nor is its refcount incremented. If the caller needs |
| 289 | * to retain this data, it should do, and increment the reference |
| 290 | * count in the supplied callback. |
| 291 | */ |
| 292 | |
| 293 | int bus_for_each_dev(struct bus_type * bus, struct device * start, |
| 294 | void * data, int (*fn)(struct device *, void *)) |
| 295 | { |
mochel@digitalimplant.org | 465c7a3 | 2005-03-21 11:49:14 -0800 | [diff] [blame] | 296 | struct klist_iter i; |
| 297 | struct device * dev; |
| 298 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | |
mochel@digitalimplant.org | 465c7a3 | 2005-03-21 11:49:14 -0800 | [diff] [blame] | 300 | if (!bus) |
| 301 | return -EINVAL; |
| 302 | |
| 303 | klist_iter_init_node(&bus->klist_devices, &i, |
| 304 | (start ? &start->knode_bus : NULL)); |
| 305 | while ((dev = next_device(&i)) && !error) |
| 306 | error = fn(dev, data); |
| 307 | klist_iter_exit(&i); |
| 308 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Cornelia Huck | 0edb586 | 2005-06-22 16:59:51 +0200 | [diff] [blame] | 311 | /** |
| 312 | * bus_find_device - device iterator for locating a particular device. |
| 313 | * @bus: bus type |
| 314 | * @start: Device to begin with |
| 315 | * @data: Data to pass to match function |
| 316 | * @match: Callback function to check device |
| 317 | * |
| 318 | * This is similar to the bus_for_each_dev() function above, but it |
| 319 | * returns a reference to a device that is 'found' for later use, as |
| 320 | * determined by the @match callback. |
| 321 | * |
| 322 | * The callback should return 0 if the device doesn't match and non-zero |
| 323 | * if it does. If the callback returns non-zero, this function will |
| 324 | * return to the caller and not iterate over any more devices. |
| 325 | */ |
| 326 | struct device * bus_find_device(struct bus_type *bus, |
| 327 | struct device *start, void *data, |
| 328 | int (*match)(struct device *, void *)) |
| 329 | { |
| 330 | struct klist_iter i; |
| 331 | struct device *dev; |
| 332 | |
| 333 | if (!bus) |
| 334 | return NULL; |
| 335 | |
| 336 | klist_iter_init_node(&bus->klist_devices, &i, |
| 337 | (start ? &start->knode_bus : NULL)); |
| 338 | while ((dev = next_device(&i))) |
| 339 | if (match(dev, data) && get_device(dev)) |
| 340 | break; |
| 341 | klist_iter_exit(&i); |
| 342 | return dev; |
| 343 | } |
mochel@digitalimplant.org | 38fdac3 | 2005-03-21 12:00:18 -0800 | [diff] [blame] | 344 | |
| 345 | |
| 346 | static struct device_driver * next_driver(struct klist_iter * i) |
| 347 | { |
| 348 | struct klist_node * n = klist_next(i); |
| 349 | return n ? container_of(n, struct device_driver, knode_bus) : NULL; |
| 350 | } |
| 351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | /** |
| 353 | * bus_for_each_drv - driver iterator |
| 354 | * @bus: bus we're dealing with. |
| 355 | * @start: driver to start iterating on. |
| 356 | * @data: data to pass to the callback. |
| 357 | * @fn: function to call for each driver. |
| 358 | * |
| 359 | * This is nearly identical to the device iterator above. |
| 360 | * We iterate over each driver that belongs to @bus, and call |
| 361 | * @fn for each. If @fn returns anything but 0, we break out |
| 362 | * and return it. If @start is not NULL, we use it as the head |
| 363 | * of the list. |
| 364 | * |
| 365 | * NOTE: we don't return the driver that returns a non-zero |
| 366 | * value, nor do we leave the reference count incremented for that |
| 367 | * driver. If the caller needs to know that info, it must set it |
| 368 | * in the callback. It must also be sure to increment the refcount |
| 369 | * so it doesn't disappear before returning to the caller. |
| 370 | */ |
| 371 | |
| 372 | int bus_for_each_drv(struct bus_type * bus, struct device_driver * start, |
| 373 | void * data, int (*fn)(struct device_driver *, void *)) |
| 374 | { |
mochel@digitalimplant.org | 38fdac3 | 2005-03-21 12:00:18 -0800 | [diff] [blame] | 375 | struct klist_iter i; |
| 376 | struct device_driver * drv; |
| 377 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
mochel@digitalimplant.org | 38fdac3 | 2005-03-21 12:00:18 -0800 | [diff] [blame] | 379 | if (!bus) |
| 380 | return -EINVAL; |
| 381 | |
| 382 | klist_iter_init_node(&bus->klist_drivers, &i, |
| 383 | start ? &start->knode_bus : NULL); |
| 384 | while ((drv = next_driver(&i)) && !error) |
| 385 | error = fn(drv, data); |
| 386 | klist_iter_exit(&i); |
| 387 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Andrew Morton | 4aca67e | 2007-02-13 22:39:26 -0800 | [diff] [blame] | 390 | static int device_add_attrs(struct bus_type *bus, struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | { |
| 392 | int error = 0; |
| 393 | int i; |
| 394 | |
Andrew Morton | 4aca67e | 2007-02-13 22:39:26 -0800 | [diff] [blame] | 395 | if (!bus->dev_attrs) |
| 396 | return 0; |
| 397 | |
| 398 | for (i = 0; attr_name(bus->dev_attrs[i]); i++) { |
| 399 | error = device_create_file(dev,&bus->dev_attrs[i]); |
| 400 | if (error) { |
| 401 | while (--i >= 0) |
| 402 | device_remove_file(dev, &bus->dev_attrs[i]); |
| 403 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | static void device_remove_attrs(struct bus_type * bus, struct device * dev) |
| 410 | { |
| 411 | int i; |
| 412 | |
| 413 | if (bus->dev_attrs) { |
| 414 | for (i = 0; attr_name(bus->dev_attrs[i]); i++) |
| 415 | device_remove_file(dev,&bus->dev_attrs[i]); |
| 416 | } |
| 417 | } |
| 418 | |
Kay Sievers | b9cafc7 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 419 | #ifdef CONFIG_SYSFS_DEPRECATED |
| 420 | static int make_deprecated_bus_links(struct device *dev) |
| 421 | { |
| 422 | return sysfs_create_link(&dev->kobj, |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 423 | &dev->bus->subsys.kobj, "bus"); |
Kay Sievers | b9cafc7 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | static void remove_deprecated_bus_links(struct device *dev) |
| 427 | { |
| 428 | sysfs_remove_link(&dev->kobj, "bus"); |
| 429 | } |
| 430 | #else |
| 431 | static inline int make_deprecated_bus_links(struct device *dev) { return 0; } |
| 432 | static inline void remove_deprecated_bus_links(struct device *dev) { } |
| 433 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
| 435 | /** |
| 436 | * bus_add_device - add device to bus |
| 437 | * @dev: device being added |
| 438 | * |
| 439 | * - Add the device to its bus's list of devices. |
Kay Sievers | 53877d0 | 2006-04-04 20:42:26 +0200 | [diff] [blame] | 440 | * - Create link to device's bus. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | */ |
| 442 | int bus_add_device(struct device * dev) |
| 443 | { |
Greg Kroah-Hartman | 5901d01 | 2007-09-13 02:53:13 -0700 | [diff] [blame] | 444 | struct bus_type * bus = bus_get(dev->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | int error = 0; |
| 446 | |
| 447 | if (bus) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id); |
Greg Kroah-Hartman | d377e85 | 2005-06-22 16:09:05 -0700 | [diff] [blame] | 449 | error = device_add_attrs(bus, dev); |
Andrew Morton | f86db39 | 2006-08-14 22:43:20 -0700 | [diff] [blame] | 450 | if (error) |
Cornelia Huck | 513e733 | 2006-09-22 11:37:08 +0200 | [diff] [blame] | 451 | goto out_put; |
Greg Kroah-Hartman | 3d89959 | 2007-11-01 13:31:26 -0700 | [diff] [blame] | 452 | error = sysfs_create_link(&bus->devices_kset->kobj, |
Andrew Morton | f86db39 | 2006-08-14 22:43:20 -0700 | [diff] [blame] | 453 | &dev->kobj, dev->bus_id); |
| 454 | if (error) |
Cornelia Huck | 513e733 | 2006-09-22 11:37:08 +0200 | [diff] [blame] | 455 | goto out_id; |
Andrew Morton | f86db39 | 2006-08-14 22:43:20 -0700 | [diff] [blame] | 456 | error = sysfs_create_link(&dev->kobj, |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 457 | &dev->bus->subsys.kobj, "subsystem"); |
Andrew Morton | f86db39 | 2006-08-14 22:43:20 -0700 | [diff] [blame] | 458 | if (error) |
Cornelia Huck | 513e733 | 2006-09-22 11:37:08 +0200 | [diff] [blame] | 459 | goto out_subsys; |
Kay Sievers | b9cafc7 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 460 | error = make_deprecated_bus_links(dev); |
Cornelia Huck | 513e733 | 2006-09-22 11:37:08 +0200 | [diff] [blame] | 461 | if (error) |
| 462 | goto out_deprecated; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } |
Cornelia Huck | 513e733 | 2006-09-22 11:37:08 +0200 | [diff] [blame] | 464 | return 0; |
| 465 | |
| 466 | out_deprecated: |
| 467 | sysfs_remove_link(&dev->kobj, "subsystem"); |
| 468 | out_subsys: |
Greg Kroah-Hartman | 3d89959 | 2007-11-01 13:31:26 -0700 | [diff] [blame] | 469 | sysfs_remove_link(&bus->devices_kset->kobj, dev->bus_id); |
Cornelia Huck | 513e733 | 2006-09-22 11:37:08 +0200 | [diff] [blame] | 470 | out_id: |
| 471 | device_remove_attrs(bus, dev); |
| 472 | out_put: |
Greg Kroah-Hartman | fc1ede5 | 2007-09-13 02:53:13 -0700 | [diff] [blame] | 473 | bus_put(dev->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | return error; |
| 475 | } |
| 476 | |
| 477 | /** |
Kay Sievers | 53877d0 | 2006-04-04 20:42:26 +0200 | [diff] [blame] | 478 | * bus_attach_device - add device to bus |
| 479 | * @dev: device tried to attach to a driver |
| 480 | * |
Alan Stern | f2eaae1 | 2006-09-18 16:22:34 -0400 | [diff] [blame] | 481 | * - Add device to bus's list of devices. |
Kay Sievers | 53877d0 | 2006-04-04 20:42:26 +0200 | [diff] [blame] | 482 | * - Try to attach to driver. |
| 483 | */ |
Cornelia Huck | c6a4669 | 2007-02-05 16:15:26 -0800 | [diff] [blame] | 484 | void bus_attach_device(struct device * dev) |
Kay Sievers | 53877d0 | 2006-04-04 20:42:26 +0200 | [diff] [blame] | 485 | { |
Andrew Morton | f86db39 | 2006-08-14 22:43:20 -0700 | [diff] [blame] | 486 | struct bus_type *bus = dev->bus; |
| 487 | int ret = 0; |
Kay Sievers | 53877d0 | 2006-04-04 20:42:26 +0200 | [diff] [blame] | 488 | |
| 489 | if (bus) { |
Alan Stern | f2eaae1 | 2006-09-18 16:22:34 -0400 | [diff] [blame] | 490 | dev->is_registered = 1; |
Kay Sievers | b8c5cec | 2007-02-16 17:33:36 +0100 | [diff] [blame] | 491 | if (bus->drivers_autoprobe) |
| 492 | ret = device_attach(dev); |
Cornelia Huck | c6a4669 | 2007-02-05 16:15:26 -0800 | [diff] [blame] | 493 | WARN_ON(ret < 0); |
| 494 | if (ret >= 0) |
Andrew Morton | f86db39 | 2006-08-14 22:43:20 -0700 | [diff] [blame] | 495 | klist_add_tail(&dev->knode_bus, &bus->klist_devices); |
Cornelia Huck | c6a4669 | 2007-02-05 16:15:26 -0800 | [diff] [blame] | 496 | else |
Alan Stern | f2eaae1 | 2006-09-18 16:22:34 -0400 | [diff] [blame] | 497 | dev->is_registered = 0; |
Kay Sievers | 53877d0 | 2006-04-04 20:42:26 +0200 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
| 501 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | * bus_remove_device - remove device from bus |
| 503 | * @dev: device to be removed |
| 504 | * |
| 505 | * - Remove symlink from bus's directory. |
| 506 | * - Delete device from bus's list. |
| 507 | * - Detach from its driver. |
| 508 | * - Drop reference taken in bus_add_device(). |
| 509 | */ |
| 510 | void bus_remove_device(struct device * dev) |
| 511 | { |
| 512 | if (dev->bus) { |
Kay Sievers | b9d9c82 | 2006-06-15 15:31:56 +0200 | [diff] [blame] | 513 | sysfs_remove_link(&dev->kobj, "subsystem"); |
Kay Sievers | b9cafc7 | 2006-09-14 11:23:28 +0200 | [diff] [blame] | 514 | remove_deprecated_bus_links(dev); |
Greg Kroah-Hartman | 3d89959 | 2007-11-01 13:31:26 -0700 | [diff] [blame] | 515 | sysfs_remove_link(&dev->bus->devices_kset->kobj, dev->bus_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | device_remove_attrs(dev->bus, dev); |
Alan Stern | f70fa62 | 2006-10-05 17:03:24 -0400 | [diff] [blame] | 517 | if (dev->is_registered) { |
| 518 | dev->is_registered = 0; |
| 519 | klist_del(&dev->knode_bus); |
| 520 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id); |
| 522 | device_release_driver(dev); |
Greg Kroah-Hartman | fc1ede5 | 2007-09-13 02:53:13 -0700 | [diff] [blame] | 523 | bus_put(dev->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | } |
| 525 | } |
| 526 | |
| 527 | static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv) |
| 528 | { |
| 529 | int error = 0; |
| 530 | int i; |
| 531 | |
| 532 | if (bus->drv_attrs) { |
| 533 | for (i = 0; attr_name(bus->drv_attrs[i]); i++) { |
| 534 | error = driver_create_file(drv, &bus->drv_attrs[i]); |
| 535 | if (error) |
| 536 | goto Err; |
| 537 | } |
| 538 | } |
| 539 | Done: |
| 540 | return error; |
| 541 | Err: |
| 542 | while (--i >= 0) |
| 543 | driver_remove_file(drv, &bus->drv_attrs[i]); |
| 544 | goto Done; |
| 545 | } |
| 546 | |
| 547 | |
| 548 | static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv) |
| 549 | { |
| 550 | int i; |
| 551 | |
| 552 | if (bus->drv_attrs) { |
| 553 | for (i = 0; attr_name(bus->drv_attrs[i]); i++) |
| 554 | driver_remove_file(drv, &bus->drv_attrs[i]); |
| 555 | } |
| 556 | } |
| 557 | |
Greg Kroah-Hartman | 874c624 | 2005-12-13 15:17:34 -0800 | [diff] [blame] | 558 | #ifdef CONFIG_HOTPLUG |
| 559 | /* |
| 560 | * Thanks to drivers making their tables __devinit, we can't allow manual |
| 561 | * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled. |
| 562 | */ |
Andrew Morton | f86db39 | 2006-08-14 22:43:20 -0700 | [diff] [blame] | 563 | static int __must_check add_bind_files(struct device_driver *drv) |
Greg Kroah-Hartman | 874c624 | 2005-12-13 15:17:34 -0800 | [diff] [blame] | 564 | { |
Andrew Morton | f86db39 | 2006-08-14 22:43:20 -0700 | [diff] [blame] | 565 | int ret; |
| 566 | |
| 567 | ret = driver_create_file(drv, &driver_attr_unbind); |
| 568 | if (ret == 0) { |
| 569 | ret = driver_create_file(drv, &driver_attr_bind); |
| 570 | if (ret) |
| 571 | driver_remove_file(drv, &driver_attr_unbind); |
| 572 | } |
| 573 | return ret; |
Greg Kroah-Hartman | 874c624 | 2005-12-13 15:17:34 -0800 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | static void remove_bind_files(struct device_driver *drv) |
| 577 | { |
| 578 | driver_remove_file(drv, &driver_attr_bind); |
| 579 | driver_remove_file(drv, &driver_attr_unbind); |
| 580 | } |
Kay Sievers | b8c5cec | 2007-02-16 17:33:36 +0100 | [diff] [blame] | 581 | |
Kay Sievers | 8380770 | 2007-07-30 02:28:56 +0200 | [diff] [blame] | 582 | static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe); |
| 583 | static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO, |
| 584 | show_drivers_autoprobe, store_drivers_autoprobe); |
| 585 | |
Kay Sievers | b8c5cec | 2007-02-16 17:33:36 +0100 | [diff] [blame] | 586 | static int add_probe_files(struct bus_type *bus) |
| 587 | { |
| 588 | int retval; |
| 589 | |
Kay Sievers | 8380770 | 2007-07-30 02:28:56 +0200 | [diff] [blame] | 590 | retval = bus_create_file(bus, &bus_attr_drivers_probe); |
Kay Sievers | b8c5cec | 2007-02-16 17:33:36 +0100 | [diff] [blame] | 591 | if (retval) |
| 592 | goto out; |
| 593 | |
Kay Sievers | 8380770 | 2007-07-30 02:28:56 +0200 | [diff] [blame] | 594 | retval = bus_create_file(bus, &bus_attr_drivers_autoprobe); |
Kay Sievers | b8c5cec | 2007-02-16 17:33:36 +0100 | [diff] [blame] | 595 | if (retval) |
Kay Sievers | 8380770 | 2007-07-30 02:28:56 +0200 | [diff] [blame] | 596 | bus_remove_file(bus, &bus_attr_drivers_probe); |
Kay Sievers | b8c5cec | 2007-02-16 17:33:36 +0100 | [diff] [blame] | 597 | out: |
| 598 | return retval; |
| 599 | } |
| 600 | |
| 601 | static void remove_probe_files(struct bus_type *bus) |
| 602 | { |
Kay Sievers | 8380770 | 2007-07-30 02:28:56 +0200 | [diff] [blame] | 603 | bus_remove_file(bus, &bus_attr_drivers_autoprobe); |
| 604 | bus_remove_file(bus, &bus_attr_drivers_probe); |
Kay Sievers | b8c5cec | 2007-02-16 17:33:36 +0100 | [diff] [blame] | 605 | } |
Greg Kroah-Hartman | 874c624 | 2005-12-13 15:17:34 -0800 | [diff] [blame] | 606 | #else |
Yoichi Yuasa | 35acfdd | 2006-07-15 01:30:11 +0900 | [diff] [blame] | 607 | static inline int add_bind_files(struct device_driver *drv) { return 0; } |
Greg Kroah-Hartman | 874c624 | 2005-12-13 15:17:34 -0800 | [diff] [blame] | 608 | static inline void remove_bind_files(struct device_driver *drv) {} |
Kay Sievers | b8c5cec | 2007-02-16 17:33:36 +0100 | [diff] [blame] | 609 | static inline int add_probe_files(struct bus_type *bus) { return 0; } |
| 610 | static inline void remove_probe_files(struct bus_type *bus) {} |
Greg Kroah-Hartman | 874c624 | 2005-12-13 15:17:34 -0800 | [diff] [blame] | 611 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
Kay Sievers | 7ac1cf4 | 2007-08-12 20:43:55 +0200 | [diff] [blame] | 613 | static ssize_t driver_uevent_store(struct device_driver *drv, |
| 614 | const char *buf, size_t count) |
| 615 | { |
| 616 | enum kobject_action action; |
| 617 | |
| 618 | if (kobject_action_type(buf, count, &action) == 0) |
| 619 | kobject_uevent(&drv->kobj, action); |
| 620 | return count; |
| 621 | } |
| 622 | static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store); |
| 623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | /** |
| 625 | * bus_add_driver - Add a driver to the bus. |
| 626 | * @drv: driver. |
| 627 | * |
| 628 | */ |
Andrew Morton | f86db39 | 2006-08-14 22:43:20 -0700 | [diff] [blame] | 629 | int bus_add_driver(struct device_driver *drv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | { |
Greg Kroah-Hartman | 5901d01 | 2007-09-13 02:53:13 -0700 | [diff] [blame] | 631 | struct bus_type * bus = bus_get(drv->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | int error = 0; |
| 633 | |
Jeff Garzik | d9fd4d3b3 | 2006-10-04 07:48:03 -0400 | [diff] [blame] | 634 | if (!bus) |
Greg Kroah-Hartman | 4f6e194 | 2007-04-20 11:29:52 -0700 | [diff] [blame] | 635 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
Jeff Garzik | d9fd4d3b3 | 2006-10-04 07:48:03 -0400 | [diff] [blame] | 637 | pr_debug("bus %s: add driver %s\n", bus->name, drv->name); |
| 638 | error = kobject_set_name(&drv->kobj, "%s", drv->name); |
| 639 | if (error) |
| 640 | goto out_put_bus; |
Greg Kroah-Hartman | 6dcec25 | 2007-11-01 13:31:26 -0700 | [diff] [blame] | 641 | drv->kobj.kset = bus->drivers_kset; |
Greg Kroah-Hartman | 3514fac | 2007-10-16 10:11:44 -0600 | [diff] [blame] | 642 | drv->kobj.ktype = &driver_ktype; |
Cornelia Huck | dc0afa8 | 2007-07-09 11:39:18 -0700 | [diff] [blame] | 643 | error = kobject_register(&drv->kobj); |
| 644 | if (error) |
Jeff Garzik | d9fd4d3b3 | 2006-10-04 07:48:03 -0400 | [diff] [blame] | 645 | goto out_put_bus; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
Kay Sievers | b8c5cec | 2007-02-16 17:33:36 +0100 | [diff] [blame] | 647 | if (drv->bus->drivers_autoprobe) { |
| 648 | error = driver_attach(drv); |
| 649 | if (error) |
| 650 | goto out_unregister; |
| 651 | } |
Jeff Garzik | d9fd4d3b3 | 2006-10-04 07:48:03 -0400 | [diff] [blame] | 652 | klist_add_tail(&drv->knode_bus, &bus->klist_drivers); |
| 653 | module_add_driver(drv->owner, drv); |
| 654 | |
Kay Sievers | 7ac1cf4 | 2007-08-12 20:43:55 +0200 | [diff] [blame] | 655 | error = driver_create_file(drv, &driver_attr_uevent); |
| 656 | if (error) { |
| 657 | printk(KERN_ERR "%s: uevent attr (%s) failed\n", |
| 658 | __FUNCTION__, drv->name); |
| 659 | } |
Jeff Garzik | d9fd4d3b3 | 2006-10-04 07:48:03 -0400 | [diff] [blame] | 660 | error = driver_add_attrs(bus, drv); |
| 661 | if (error) { |
| 662 | /* How the hell do we get out of this pickle? Give up */ |
| 663 | printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n", |
| 664 | __FUNCTION__, drv->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | } |
Jeff Garzik | d9fd4d3b3 | 2006-10-04 07:48:03 -0400 | [diff] [blame] | 666 | error = add_bind_files(drv); |
| 667 | if (error) { |
| 668 | /* Ditto */ |
| 669 | printk(KERN_ERR "%s: add_bind_files(%s) failed\n", |
| 670 | __FUNCTION__, drv->name); |
| 671 | } |
| 672 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | return error; |
Andrew Morton | f86db39 | 2006-08-14 22:43:20 -0700 | [diff] [blame] | 674 | out_unregister: |
| 675 | kobject_unregister(&drv->kobj); |
| 676 | out_put_bus: |
Greg Kroah-Hartman | fc1ede5 | 2007-09-13 02:53:13 -0700 | [diff] [blame] | 677 | bus_put(bus); |
Andrew Morton | f86db39 | 2006-08-14 22:43:20 -0700 | [diff] [blame] | 678 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | /** |
| 682 | * bus_remove_driver - delete driver from bus's knowledge. |
| 683 | * @drv: driver. |
| 684 | * |
| 685 | * Detach the driver from the devices it controls, and remove |
| 686 | * it from its bus's list of drivers. Finally, we drop the reference |
| 687 | * to the bus we took in bus_add_driver(). |
| 688 | */ |
| 689 | |
| 690 | void bus_remove_driver(struct device_driver * drv) |
| 691 | { |
Jeff Garzik | d9fd4d3b3 | 2006-10-04 07:48:03 -0400 | [diff] [blame] | 692 | if (!drv->bus) |
| 693 | return; |
| 694 | |
| 695 | remove_bind_files(drv); |
| 696 | driver_remove_attrs(drv->bus, drv); |
Kay Sievers | 7ac1cf4 | 2007-08-12 20:43:55 +0200 | [diff] [blame] | 697 | driver_remove_file(drv, &driver_attr_uevent); |
Jeff Garzik | d9fd4d3b3 | 2006-10-04 07:48:03 -0400 | [diff] [blame] | 698 | klist_remove(&drv->knode_bus); |
| 699 | pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name); |
| 700 | driver_detach(drv); |
| 701 | module_remove_driver(drv); |
| 702 | kobject_unregister(&drv->kobj); |
Greg Kroah-Hartman | fc1ede5 | 2007-09-13 02:53:13 -0700 | [diff] [blame] | 703 | bus_put(drv->bus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | |
| 707 | /* Helper for bus_rescan_devices's iter */ |
Andrew Morton | f86db39 | 2006-08-14 22:43:20 -0700 | [diff] [blame] | 708 | static int __must_check bus_rescan_devices_helper(struct device *dev, |
| 709 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | { |
Andrew Morton | f86db39 | 2006-08-14 22:43:20 -0700 | [diff] [blame] | 711 | int ret = 0; |
| 712 | |
Alan Stern | bf74ad5 | 2005-11-17 16:54:12 -0500 | [diff] [blame] | 713 | if (!dev->driver) { |
| 714 | if (dev->parent) /* Needed for USB */ |
| 715 | down(&dev->parent->sem); |
Andrew Morton | f86db39 | 2006-08-14 22:43:20 -0700 | [diff] [blame] | 716 | ret = device_attach(dev); |
Alan Stern | bf74ad5 | 2005-11-17 16:54:12 -0500 | [diff] [blame] | 717 | if (dev->parent) |
| 718 | up(&dev->parent->sem); |
| 719 | } |
Andrew Morton | f86db39 | 2006-08-14 22:43:20 -0700 | [diff] [blame] | 720 | return ret < 0 ? ret : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | /** |
Greg Kroah-Hartman | 23d3d60 | 2005-06-22 16:09:05 -0700 | [diff] [blame] | 724 | * bus_rescan_devices - rescan devices on the bus for possible drivers |
| 725 | * @bus: the bus to scan. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | * |
Greg Kroah-Hartman | 23d3d60 | 2005-06-22 16:09:05 -0700 | [diff] [blame] | 727 | * This function will look for devices on the bus with no driver |
| 728 | * attached and rescan it against existing drivers to see if it matches |
| 729 | * any by calling device_attach() for the unbound devices. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | */ |
Andrew Morton | f86db39 | 2006-08-14 22:43:20 -0700 | [diff] [blame] | 731 | int bus_rescan_devices(struct bus_type * bus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | { |
Andrew Morton | f86db39 | 2006-08-14 22:43:20 -0700 | [diff] [blame] | 733 | return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | } |
| 735 | |
Moore, Eric | e935d5d | 2006-03-14 09:18:18 -0700 | [diff] [blame] | 736 | /** |
| 737 | * device_reprobe - remove driver for a device and probe for a new driver |
| 738 | * @dev: the device to reprobe |
| 739 | * |
| 740 | * This function detaches the attached driver (if any) for the given |
| 741 | * device and restarts the driver probing process. It is intended |
| 742 | * to use if probing criteria changed during a devices lifetime and |
| 743 | * driver attachment should change accordingly. |
| 744 | */ |
Andrew Morton | f86db39 | 2006-08-14 22:43:20 -0700 | [diff] [blame] | 745 | int device_reprobe(struct device *dev) |
Moore, Eric | e935d5d | 2006-03-14 09:18:18 -0700 | [diff] [blame] | 746 | { |
| 747 | if (dev->driver) { |
| 748 | if (dev->parent) /* Needed for USB */ |
| 749 | down(&dev->parent->sem); |
| 750 | device_release_driver(dev); |
| 751 | if (dev->parent) |
| 752 | up(&dev->parent->sem); |
| 753 | } |
Andrew Morton | f86db39 | 2006-08-14 22:43:20 -0700 | [diff] [blame] | 754 | return bus_rescan_devices_helper(dev, NULL); |
Moore, Eric | e935d5d | 2006-03-14 09:18:18 -0700 | [diff] [blame] | 755 | } |
| 756 | EXPORT_SYMBOL_GPL(device_reprobe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | /** |
| 759 | * find_bus - locate bus by name. |
| 760 | * @name: name of bus. |
| 761 | * |
| 762 | * Call kset_find_obj() to iterate over list of buses to |
| 763 | * find a bus by name. Return bus if found. |
| 764 | * |
| 765 | * Note that kset_find_obj increments bus' reference count. |
| 766 | */ |
Adrian Bunk | 7e4ef08 | 2006-06-26 22:26:56 +0200 | [diff] [blame] | 767 | #if 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | struct bus_type * find_bus(char * name) |
| 769 | { |
Greg Kroah-Hartman | 59a5483 | 2007-10-29 23:22:26 -0500 | [diff] [blame] | 770 | struct kobject * k = kset_find_obj(bus_kset, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | return k ? to_bus(k) : NULL; |
| 772 | } |
Adrian Bunk | 7e4ef08 | 2006-06-26 22:26:56 +0200 | [diff] [blame] | 773 | #endif /* 0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
| 775 | |
| 776 | /** |
| 777 | * bus_add_attrs - Add default attributes for this bus. |
| 778 | * @bus: Bus that has just been registered. |
| 779 | */ |
| 780 | |
| 781 | static int bus_add_attrs(struct bus_type * bus) |
| 782 | { |
| 783 | int error = 0; |
| 784 | int i; |
| 785 | |
| 786 | if (bus->bus_attrs) { |
| 787 | for (i = 0; attr_name(bus->bus_attrs[i]); i++) { |
Cornelia Huck | dc0afa8 | 2007-07-09 11:39:18 -0700 | [diff] [blame] | 788 | error = bus_create_file(bus,&bus->bus_attrs[i]); |
| 789 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | goto Err; |
| 791 | } |
| 792 | } |
| 793 | Done: |
| 794 | return error; |
| 795 | Err: |
| 796 | while (--i >= 0) |
| 797 | bus_remove_file(bus,&bus->bus_attrs[i]); |
| 798 | goto Done; |
| 799 | } |
| 800 | |
| 801 | static void bus_remove_attrs(struct bus_type * bus) |
| 802 | { |
| 803 | int i; |
| 804 | |
| 805 | if (bus->bus_attrs) { |
| 806 | for (i = 0; attr_name(bus->bus_attrs[i]); i++) |
| 807 | bus_remove_file(bus,&bus->bus_attrs[i]); |
| 808 | } |
| 809 | } |
| 810 | |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 811 | static void klist_devices_get(struct klist_node *n) |
| 812 | { |
| 813 | struct device *dev = container_of(n, struct device, knode_bus); |
| 814 | |
| 815 | get_device(dev); |
| 816 | } |
| 817 | |
| 818 | static void klist_devices_put(struct klist_node *n) |
| 819 | { |
| 820 | struct device *dev = container_of(n, struct device, knode_bus); |
| 821 | |
| 822 | put_device(dev); |
| 823 | } |
| 824 | |
Kay Sievers | 7ac1cf4 | 2007-08-12 20:43:55 +0200 | [diff] [blame] | 825 | static ssize_t bus_uevent_store(struct bus_type *bus, |
| 826 | const char *buf, size_t count) |
| 827 | { |
| 828 | enum kobject_action action; |
| 829 | |
| 830 | if (kobject_action_type(buf, count, &action) == 0) |
| 831 | kobject_uevent(&bus->subsys.kobj, action); |
| 832 | return count; |
| 833 | } |
| 834 | static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store); |
| 835 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | /** |
| 837 | * bus_register - register a bus with the system. |
| 838 | * @bus: bus. |
| 839 | * |
| 840 | * Once we have that, we registered the bus with the kobject |
| 841 | * infrastructure, then register the children subsystems it has: |
| 842 | * the devices and drivers that belong to the bus. |
| 843 | */ |
| 844 | int bus_register(struct bus_type * bus) |
| 845 | { |
| 846 | int retval; |
| 847 | |
Benjamin Herrenschmidt | 116af37 | 2006-10-25 13:44:59 +1000 | [diff] [blame] | 848 | BLOCKING_INIT_NOTIFIER_HEAD(&bus->bus_notifier); |
| 849 | |
Greg Kroah-Hartman | 823bccf | 2007-04-13 13:15:19 -0700 | [diff] [blame] | 850 | retval = kobject_set_name(&bus->subsys.kobj, "%s", bus->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | if (retval) |
| 852 | goto out; |
| 853 | |
Greg Kroah-Hartman | 59a5483 | 2007-10-29 23:22:26 -0500 | [diff] [blame] | 854 | bus->subsys.kobj.kset = bus_kset; |
Greg Kroah-Hartman | 3514fac | 2007-10-16 10:11:44 -0600 | [diff] [blame] | 855 | bus->subsys.kobj.ktype = &bus_ktype; |
Greg Kroah-Hartman | d6b05b8 | 2007-09-12 15:06:57 -0700 | [diff] [blame] | 856 | |
Greg Kroah-Hartman | 2fb9113 | 2007-11-06 15:03:30 -0800 | [diff] [blame] | 857 | retval = kset_register(&bus->subsys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | if (retval) |
| 859 | goto out; |
| 860 | |
Kay Sievers | 7ac1cf4 | 2007-08-12 20:43:55 +0200 | [diff] [blame] | 861 | retval = bus_create_file(bus, &bus_attr_uevent); |
| 862 | if (retval) |
| 863 | goto bus_uevent_fail; |
| 864 | |
Greg Kroah-Hartman | 3d89959 | 2007-11-01 13:31:26 -0700 | [diff] [blame] | 865 | bus->devices_kset = kset_create_and_add("devices", NULL, |
| 866 | &bus->subsys.kobj); |
| 867 | if (!bus->devices_kset) { |
| 868 | retval = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | goto bus_devices_fail; |
Greg Kroah-Hartman | 3d89959 | 2007-11-01 13:31:26 -0700 | [diff] [blame] | 870 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | |
Greg Kroah-Hartman | 6dcec25 | 2007-11-01 13:31:26 -0700 | [diff] [blame] | 872 | bus->drivers_kset = kset_create_and_add("drivers", NULL, |
| 873 | &bus->subsys.kobj); |
| 874 | if (!bus->drivers_kset) { |
| 875 | retval = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | goto bus_drivers_fail; |
Greg Kroah-Hartman | 6dcec25 | 2007-11-01 13:31:26 -0700 | [diff] [blame] | 877 | } |
mochel@digitalimplant.org | 465c7a3 | 2005-03-21 11:49:14 -0800 | [diff] [blame] | 878 | |
James Bottomley | 34bb61f | 2005-09-06 16:56:51 -0700 | [diff] [blame] | 879 | klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put); |
Alan Stern | 81107bf | 2006-09-18 16:24:28 -0400 | [diff] [blame] | 880 | klist_init(&bus->klist_drivers, NULL, NULL); |
Kay Sievers | b8c5cec | 2007-02-16 17:33:36 +0100 | [diff] [blame] | 881 | |
| 882 | bus->drivers_autoprobe = 1; |
| 883 | retval = add_probe_files(bus); |
| 884 | if (retval) |
| 885 | goto bus_probe_files_fail; |
| 886 | |
Cornelia Huck | 1bb6881 | 2006-09-22 11:37:04 +0200 | [diff] [blame] | 887 | retval = bus_add_attrs(bus); |
| 888 | if (retval) |
| 889 | goto bus_attrs_fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 890 | |
| 891 | pr_debug("bus type '%s' registered\n", bus->name); |
| 892 | return 0; |
| 893 | |
Cornelia Huck | 1bb6881 | 2006-09-22 11:37:04 +0200 | [diff] [blame] | 894 | bus_attrs_fail: |
Kay Sievers | b8c5cec | 2007-02-16 17:33:36 +0100 | [diff] [blame] | 895 | remove_probe_files(bus); |
| 896 | bus_probe_files_fail: |
Greg Kroah-Hartman | 6dcec25 | 2007-11-01 13:31:26 -0700 | [diff] [blame] | 897 | kset_unregister(bus->drivers_kset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | bus_drivers_fail: |
Greg Kroah-Hartman | 3d89959 | 2007-11-01 13:31:26 -0700 | [diff] [blame] | 899 | kset_unregister(bus->devices_kset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | bus_devices_fail: |
Kay Sievers | 7ac1cf4 | 2007-08-12 20:43:55 +0200 | [diff] [blame] | 901 | bus_remove_file(bus, &bus_attr_uevent); |
| 902 | bus_uevent_fail: |
Greg Kroah-Hartman | 2fb9113 | 2007-11-06 15:03:30 -0800 | [diff] [blame] | 903 | kset_unregister(&bus->subsys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | out: |
| 905 | return retval; |
| 906 | } |
| 907 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | /** |
| 909 | * bus_unregister - remove a bus from the system |
| 910 | * @bus: bus. |
| 911 | * |
| 912 | * Unregister the child subsystems and the bus itself. |
Greg Kroah-Hartman | fc1ede5 | 2007-09-13 02:53:13 -0700 | [diff] [blame] | 913 | * Finally, we call bus_put() to release the refcount |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | */ |
| 915 | void bus_unregister(struct bus_type * bus) |
| 916 | { |
| 917 | pr_debug("bus %s: unregistering\n", bus->name); |
| 918 | bus_remove_attrs(bus); |
Kay Sievers | b8c5cec | 2007-02-16 17:33:36 +0100 | [diff] [blame] | 919 | remove_probe_files(bus); |
Greg Kroah-Hartman | 6dcec25 | 2007-11-01 13:31:26 -0700 | [diff] [blame] | 920 | kset_unregister(bus->drivers_kset); |
Greg Kroah-Hartman | 3d89959 | 2007-11-01 13:31:26 -0700 | [diff] [blame] | 921 | kset_unregister(bus->devices_kset); |
Kay Sievers | 7ac1cf4 | 2007-08-12 20:43:55 +0200 | [diff] [blame] | 922 | bus_remove_file(bus, &bus_attr_uevent); |
Greg Kroah-Hartman | 2fb9113 | 2007-11-06 15:03:30 -0800 | [diff] [blame] | 923 | kset_unregister(&bus->subsys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | } |
| 925 | |
Benjamin Herrenschmidt | 116af37 | 2006-10-25 13:44:59 +1000 | [diff] [blame] | 926 | int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb) |
| 927 | { |
| 928 | return blocking_notifier_chain_register(&bus->bus_notifier, nb); |
| 929 | } |
| 930 | EXPORT_SYMBOL_GPL(bus_register_notifier); |
| 931 | |
| 932 | int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb) |
| 933 | { |
| 934 | return blocking_notifier_chain_unregister(&bus->bus_notifier, nb); |
| 935 | } |
| 936 | EXPORT_SYMBOL_GPL(bus_unregister_notifier); |
| 937 | |
Greg Kroah-Hartman | 0fed80f | 2007-11-01 19:41:16 -0700 | [diff] [blame^] | 938 | struct kset *bus_get_kset(struct bus_type *bus) |
| 939 | { |
| 940 | return &bus->subsys; |
| 941 | } |
| 942 | EXPORT_SYMBOL_GPL(bus_get_kset); |
| 943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | int __init buses_init(void) |
| 945 | { |
Greg Kroah-Hartman | 59a5483 | 2007-10-29 23:22:26 -0500 | [diff] [blame] | 946 | bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL); |
| 947 | if (!bus_kset) |
| 948 | return -ENOMEM; |
| 949 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | |
| 953 | EXPORT_SYMBOL_GPL(bus_for_each_dev); |
Cornelia Huck | 0edb586 | 2005-06-22 16:59:51 +0200 | [diff] [blame] | 954 | EXPORT_SYMBOL_GPL(bus_find_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | EXPORT_SYMBOL_GPL(bus_for_each_drv); |
| 956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | EXPORT_SYMBOL_GPL(bus_register); |
| 958 | EXPORT_SYMBOL_GPL(bus_unregister); |
| 959 | EXPORT_SYMBOL_GPL(bus_rescan_devices); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | |
| 961 | EXPORT_SYMBOL_GPL(bus_create_file); |
| 962 | EXPORT_SYMBOL_GPL(bus_remove_file); |