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