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