blob: bc38085dbb105d0c57cc820b92a2dc77b5c2efdf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torvalds1da177e2005-04-16 15:20:36 -070011#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 Torvalds1da177e2005-04-16 15:20:36 -070019#define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -070020#define to_bus(obj) container_of(obj, struct bus_type, subsys.kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
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 Sieversb8c5cec2007-02-16 17:33:36 +010030static int __must_check bus_rescan_devices_helper(struct device *dev,
31 void *data);
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static ssize_t
34drv_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 Torokhov4a0c20b2005-04-29 01:23:47 -050038 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40 if (drv_attr->show)
41 ret = drv_attr->show(drv, buf);
42 return ret;
43}
44
45static ssize_t
46drv_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 Torokhov4a0c20b2005-04-29 01:23:47 -050051 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 if (drv_attr->store)
54 ret = drv_attr->store(drv, buf, count);
55 return ret;
56}
57
58static struct sysfs_ops driver_sysfs_ops = {
59 .show = drv_attr_show,
60 .store = drv_attr_store,
61};
62
63
64static void driver_release(struct kobject * kobj)
65{
Greg Kroah-Hartman74e9f5f2002-04-09 12:14:34 -070066 /*
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 Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81static 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
92static ssize_t
93bus_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
104static ssize_t
105bus_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
117static struct sysfs_ops bus_sysfs_ops = {
118 .show = bus_attr_show,
119 .store = bus_attr_store,
120};
121
122int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
123{
124 int error;
125 if (get_bus(bus)) {
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700126 error = sysfs_create_file(&bus->subsys.kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 put_bus(bus);
128 } else
129 error = -EINVAL;
130 return error;
131}
132
133void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
134{
135 if (get_bus(bus)) {
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700136 sysfs_remove_file(&bus->subsys.kobj, &attr->attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 put_bus(bus);
138 }
139}
140
Kay Sievers80f03e32007-05-26 11:21:36 +0200141static struct kobj_type bus_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .sysfs_ops = &bus_sysfs_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
Kay Sievers80f03e32007-05-26 11:21:36 +0200145static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
146{
147 struct kobj_type *ktype = get_ktype(kobj);
148
149 if (ktype == &bus_ktype)
150 return 1;
151 return 0;
152}
153
154static struct kset_uevent_ops bus_uevent_ops = {
155 .filter = bus_uevent_filter,
156};
157
158static decl_subsys(bus, &bus_ktype, &bus_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Russell King2139bdd2006-02-07 12:58:20 -0800161#ifdef CONFIG_HOTPLUG
Alan Stern2b08c8d2005-11-23 15:43:50 -0800162/* Manually detach a device from its associated driver. */
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700163static int driver_helper(struct device *dev, void *data)
164{
165 const char *name = data;
166
167 if (strcmp(name, dev->bus_id) == 0)
168 return 1;
169 return 0;
170}
171
172static ssize_t driver_unbind(struct device_driver *drv,
173 const char *buf, size_t count)
174{
175 struct bus_type *bus = get_bus(drv->bus);
176 struct device *dev;
177 int err = -ENODEV;
178
179 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
Alan Stern2b08c8d2005-11-23 15:43:50 -0800180 if (dev && dev->driver == drv) {
Alan Sternbf74ad52005-11-17 16:54:12 -0500181 if (dev->parent) /* Needed for USB */
182 down(&dev->parent->sem);
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700183 device_release_driver(dev);
Alan Sternbf74ad52005-11-17 16:54:12 -0500184 if (dev->parent)
185 up(&dev->parent->sem);
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700186 err = count;
187 }
Alan Stern2b08c8d2005-11-23 15:43:50 -0800188 put_device(dev);
189 put_bus(bus);
190 return err;
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700191}
192static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
193
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700194/*
195 * Manually attach a device to a driver.
196 * Note: the driver must want to bind to the device,
197 * it is not possible to override the driver's id table.
198 */
199static ssize_t driver_bind(struct device_driver *drv,
200 const char *buf, size_t count)
201{
202 struct bus_type *bus = get_bus(drv->bus);
203 struct device *dev;
204 int err = -ENODEV;
205
206 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
Alan Stern2b08c8d2005-11-23 15:43:50 -0800207 if (dev && dev->driver == NULL) {
Alan Sternbf74ad52005-11-17 16:54:12 -0500208 if (dev->parent) /* Needed for USB */
209 down(&dev->parent->sem);
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700210 down(&dev->sem);
211 err = driver_probe_device(drv, dev);
212 up(&dev->sem);
Alan Sternbf74ad52005-11-17 16:54:12 -0500213 if (dev->parent)
214 up(&dev->parent->sem);
Ryan Wilson37225402006-03-22 16:26:25 -0500215
216 if (err > 0) /* success */
217 err = count;
218 else if (err == 0) /* driver didn't accept device */
219 err = -ENODEV;
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700220 }
Alan Stern2b08c8d2005-11-23 15:43:50 -0800221 put_device(dev);
222 put_bus(bus);
223 return err;
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700224}
225static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
226
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100227static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
228{
229 return sprintf(buf, "%d\n", bus->drivers_autoprobe);
230}
231
232static ssize_t store_drivers_autoprobe(struct bus_type *bus,
233 const char *buf, size_t count)
234{
235 if (buf[0] == '0')
236 bus->drivers_autoprobe = 0;
237 else
238 bus->drivers_autoprobe = 1;
239 return count;
240}
241
242static ssize_t store_drivers_probe(struct bus_type *bus,
243 const char *buf, size_t count)
244{
245 struct device *dev;
246
247 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
248 if (!dev)
249 return -ENODEV;
250 if (bus_rescan_devices_helper(dev, NULL) != 0)
251 return -EINVAL;
252 return count;
253}
Russell King2139bdd2006-02-07 12:58:20 -0800254#endif
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700255
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800256static struct device * next_device(struct klist_iter * i)
257{
258 struct klist_node * n = klist_next(i);
259 return n ? container_of(n, struct device, knode_bus) : NULL;
260}
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/**
263 * bus_for_each_dev - device iterator.
264 * @bus: bus type.
265 * @start: device to start iterating from.
266 * @data: data for the callback.
267 * @fn: function to be called for each device.
268 *
269 * Iterate over @bus's list of devices, and call @fn for each,
270 * passing it @data. If @start is not NULL, we use that device to
271 * begin iterating from.
272 *
273 * We check the return of @fn each time. If it returns anything
274 * other than 0, we break out and return that value.
275 *
276 * NOTE: The device that returns a non-zero value is not retained
277 * in any way, nor is its refcount incremented. If the caller needs
278 * to retain this data, it should do, and increment the reference
279 * count in the supplied callback.
280 */
281
282int bus_for_each_dev(struct bus_type * bus, struct device * start,
283 void * data, int (*fn)(struct device *, void *))
284{
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800285 struct klist_iter i;
286 struct device * dev;
287 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800289 if (!bus)
290 return -EINVAL;
291
292 klist_iter_init_node(&bus->klist_devices, &i,
293 (start ? &start->knode_bus : NULL));
294 while ((dev = next_device(&i)) && !error)
295 error = fn(dev, data);
296 klist_iter_exit(&i);
297 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Cornelia Huck0edb5862005-06-22 16:59:51 +0200300/**
301 * bus_find_device - device iterator for locating a particular device.
302 * @bus: bus type
303 * @start: Device to begin with
304 * @data: Data to pass to match function
305 * @match: Callback function to check device
306 *
307 * This is similar to the bus_for_each_dev() function above, but it
308 * returns a reference to a device that is 'found' for later use, as
309 * determined by the @match callback.
310 *
311 * The callback should return 0 if the device doesn't match and non-zero
312 * if it does. If the callback returns non-zero, this function will
313 * return to the caller and not iterate over any more devices.
314 */
315struct device * bus_find_device(struct bus_type *bus,
316 struct device *start, void *data,
317 int (*match)(struct device *, void *))
318{
319 struct klist_iter i;
320 struct device *dev;
321
322 if (!bus)
323 return NULL;
324
325 klist_iter_init_node(&bus->klist_devices, &i,
326 (start ? &start->knode_bus : NULL));
327 while ((dev = next_device(&i)))
328 if (match(dev, data) && get_device(dev))
329 break;
330 klist_iter_exit(&i);
331 return dev;
332}
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800333
334
335static struct device_driver * next_driver(struct klist_iter * i)
336{
337 struct klist_node * n = klist_next(i);
338 return n ? container_of(n, struct device_driver, knode_bus) : NULL;
339}
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341/**
342 * bus_for_each_drv - driver iterator
343 * @bus: bus we're dealing with.
344 * @start: driver to start iterating on.
345 * @data: data to pass to the callback.
346 * @fn: function to call for each driver.
347 *
348 * This is nearly identical to the device iterator above.
349 * We iterate over each driver that belongs to @bus, and call
350 * @fn for each. If @fn returns anything but 0, we break out
351 * and return it. If @start is not NULL, we use it as the head
352 * of the list.
353 *
354 * NOTE: we don't return the driver that returns a non-zero
355 * value, nor do we leave the reference count incremented for that
356 * driver. If the caller needs to know that info, it must set it
357 * in the callback. It must also be sure to increment the refcount
358 * so it doesn't disappear before returning to the caller.
359 */
360
361int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
362 void * data, int (*fn)(struct device_driver *, void *))
363{
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800364 struct klist_iter i;
365 struct device_driver * drv;
366 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800368 if (!bus)
369 return -EINVAL;
370
371 klist_iter_init_node(&bus->klist_drivers, &i,
372 start ? &start->knode_bus : NULL);
373 while ((drv = next_driver(&i)) && !error)
374 error = fn(drv, data);
375 klist_iter_exit(&i);
376 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Andrew Morton4aca67e2007-02-13 22:39:26 -0800379static int device_add_attrs(struct bus_type *bus, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 int error = 0;
382 int i;
383
Andrew Morton4aca67e2007-02-13 22:39:26 -0800384 if (!bus->dev_attrs)
385 return 0;
386
387 for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
388 error = device_create_file(dev,&bus->dev_attrs[i]);
389 if (error) {
390 while (--i >= 0)
391 device_remove_file(dev, &bus->dev_attrs[i]);
392 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398static void device_remove_attrs(struct bus_type * bus, struct device * dev)
399{
400 int i;
401
402 if (bus->dev_attrs) {
403 for (i = 0; attr_name(bus->dev_attrs[i]); i++)
404 device_remove_file(dev,&bus->dev_attrs[i]);
405 }
406}
407
Kay Sieversb9cafc72006-09-14 11:23:28 +0200408#ifdef CONFIG_SYSFS_DEPRECATED
409static int make_deprecated_bus_links(struct device *dev)
410{
411 return sysfs_create_link(&dev->kobj,
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700412 &dev->bus->subsys.kobj, "bus");
Kay Sieversb9cafc72006-09-14 11:23:28 +0200413}
414
415static void remove_deprecated_bus_links(struct device *dev)
416{
417 sysfs_remove_link(&dev->kobj, "bus");
418}
419#else
420static inline int make_deprecated_bus_links(struct device *dev) { return 0; }
421static inline void remove_deprecated_bus_links(struct device *dev) { }
422#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424/**
425 * bus_add_device - add device to bus
426 * @dev: device being added
427 *
428 * - Add the device to its bus's list of devices.
Kay Sievers53877d02006-04-04 20:42:26 +0200429 * - Create link to device's bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 */
431int bus_add_device(struct device * dev)
432{
433 struct bus_type * bus = get_bus(dev->bus);
434 int error = 0;
435
436 if (bus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
Greg Kroah-Hartmand377e852005-06-22 16:09:05 -0700438 error = device_add_attrs(bus, dev);
Andrew Mortonf86db392006-08-14 22:43:20 -0700439 if (error)
Cornelia Huck513e7332006-09-22 11:37:08 +0200440 goto out_put;
Andrew Mortonf86db392006-08-14 22:43:20 -0700441 error = sysfs_create_link(&bus->devices.kobj,
442 &dev->kobj, dev->bus_id);
443 if (error)
Cornelia Huck513e7332006-09-22 11:37:08 +0200444 goto out_id;
Andrew Mortonf86db392006-08-14 22:43:20 -0700445 error = sysfs_create_link(&dev->kobj,
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700446 &dev->bus->subsys.kobj, "subsystem");
Andrew Mortonf86db392006-08-14 22:43:20 -0700447 if (error)
Cornelia Huck513e7332006-09-22 11:37:08 +0200448 goto out_subsys;
Kay Sieversb9cafc72006-09-14 11:23:28 +0200449 error = make_deprecated_bus_links(dev);
Cornelia Huck513e7332006-09-22 11:37:08 +0200450 if (error)
451 goto out_deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
Cornelia Huck513e7332006-09-22 11:37:08 +0200453 return 0;
454
455out_deprecated:
456 sysfs_remove_link(&dev->kobj, "subsystem");
457out_subsys:
458 sysfs_remove_link(&bus->devices.kobj, dev->bus_id);
459out_id:
460 device_remove_attrs(bus, dev);
461out_put:
462 put_bus(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return error;
464}
465
466/**
Kay Sievers53877d02006-04-04 20:42:26 +0200467 * bus_attach_device - add device to bus
468 * @dev: device tried to attach to a driver
469 *
Alan Sternf2eaae12006-09-18 16:22:34 -0400470 * - Add device to bus's list of devices.
Kay Sievers53877d02006-04-04 20:42:26 +0200471 * - Try to attach to driver.
472 */
Cornelia Huckc6a46692007-02-05 16:15:26 -0800473void bus_attach_device(struct device * dev)
Kay Sievers53877d02006-04-04 20:42:26 +0200474{
Andrew Mortonf86db392006-08-14 22:43:20 -0700475 struct bus_type *bus = dev->bus;
476 int ret = 0;
Kay Sievers53877d02006-04-04 20:42:26 +0200477
478 if (bus) {
Alan Sternf2eaae12006-09-18 16:22:34 -0400479 dev->is_registered = 1;
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100480 if (bus->drivers_autoprobe)
481 ret = device_attach(dev);
Cornelia Huckc6a46692007-02-05 16:15:26 -0800482 WARN_ON(ret < 0);
483 if (ret >= 0)
Andrew Mortonf86db392006-08-14 22:43:20 -0700484 klist_add_tail(&dev->knode_bus, &bus->klist_devices);
Cornelia Huckc6a46692007-02-05 16:15:26 -0800485 else
Alan Sternf2eaae12006-09-18 16:22:34 -0400486 dev->is_registered = 0;
Kay Sievers53877d02006-04-04 20:42:26 +0200487 }
488}
489
490/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 * bus_remove_device - remove device from bus
492 * @dev: device to be removed
493 *
494 * - Remove symlink from bus's directory.
495 * - Delete device from bus's list.
496 * - Detach from its driver.
497 * - Drop reference taken in bus_add_device().
498 */
499void bus_remove_device(struct device * dev)
500{
501 if (dev->bus) {
Kay Sieversb9d9c822006-06-15 15:31:56 +0200502 sysfs_remove_link(&dev->kobj, "subsystem");
Kay Sieversb9cafc72006-09-14 11:23:28 +0200503 remove_deprecated_bus_links(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
505 device_remove_attrs(dev->bus, dev);
Alan Sternf70fa622006-10-05 17:03:24 -0400506 if (dev->is_registered) {
507 dev->is_registered = 0;
508 klist_del(&dev->knode_bus);
509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
511 device_release_driver(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 put_bus(dev->bus);
513 }
514}
515
516static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
517{
518 int error = 0;
519 int i;
520
521 if (bus->drv_attrs) {
522 for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
523 error = driver_create_file(drv, &bus->drv_attrs[i]);
524 if (error)
525 goto Err;
526 }
527 }
528 Done:
529 return error;
530 Err:
531 while (--i >= 0)
532 driver_remove_file(drv, &bus->drv_attrs[i]);
533 goto Done;
534}
535
536
537static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
538{
539 int i;
540
541 if (bus->drv_attrs) {
542 for (i = 0; attr_name(bus->drv_attrs[i]); i++)
543 driver_remove_file(drv, &bus->drv_attrs[i]);
544 }
545}
546
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800547#ifdef CONFIG_HOTPLUG
548/*
549 * Thanks to drivers making their tables __devinit, we can't allow manual
550 * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
551 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700552static int __must_check add_bind_files(struct device_driver *drv)
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800553{
Andrew Mortonf86db392006-08-14 22:43:20 -0700554 int ret;
555
556 ret = driver_create_file(drv, &driver_attr_unbind);
557 if (ret == 0) {
558 ret = driver_create_file(drv, &driver_attr_bind);
559 if (ret)
560 driver_remove_file(drv, &driver_attr_unbind);
561 }
562 return ret;
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800563}
564
565static void remove_bind_files(struct device_driver *drv)
566{
567 driver_remove_file(drv, &driver_attr_bind);
568 driver_remove_file(drv, &driver_attr_unbind);
569}
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100570
Kay Sievers83807702007-07-30 02:28:56 +0200571static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
572static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
573 show_drivers_autoprobe, store_drivers_autoprobe);
574
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100575static int add_probe_files(struct bus_type *bus)
576{
577 int retval;
578
Kay Sievers83807702007-07-30 02:28:56 +0200579 retval = bus_create_file(bus, &bus_attr_drivers_probe);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100580 if (retval)
581 goto out;
582
Kay Sievers83807702007-07-30 02:28:56 +0200583 retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100584 if (retval)
Kay Sievers83807702007-07-30 02:28:56 +0200585 bus_remove_file(bus, &bus_attr_drivers_probe);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100586out:
587 return retval;
588}
589
590static void remove_probe_files(struct bus_type *bus)
591{
Kay Sievers83807702007-07-30 02:28:56 +0200592 bus_remove_file(bus, &bus_attr_drivers_autoprobe);
593 bus_remove_file(bus, &bus_attr_drivers_probe);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100594}
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800595#else
Yoichi Yuasa35acfdd2006-07-15 01:30:11 +0900596static inline int add_bind_files(struct device_driver *drv) { return 0; }
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800597static inline void remove_bind_files(struct device_driver *drv) {}
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100598static inline int add_probe_files(struct bus_type *bus) { return 0; }
599static inline void remove_probe_files(struct bus_type *bus) {}
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800600#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602/**
603 * bus_add_driver - Add a driver to the bus.
604 * @drv: driver.
605 *
606 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700607int bus_add_driver(struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 struct bus_type * bus = get_bus(drv->bus);
610 int error = 0;
611
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400612 if (!bus)
Greg Kroah-Hartman4f6e1942007-04-20 11:29:52 -0700613 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400615 pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
616 error = kobject_set_name(&drv->kobj, "%s", drv->name);
617 if (error)
618 goto out_put_bus;
619 drv->kobj.kset = &bus->drivers;
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700620 error = kobject_register(&drv->kobj);
621 if (error)
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400622 goto out_put_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100624 if (drv->bus->drivers_autoprobe) {
625 error = driver_attach(drv);
626 if (error)
627 goto out_unregister;
628 }
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400629 klist_add_tail(&drv->knode_bus, &bus->klist_drivers);
630 module_add_driver(drv->owner, drv);
631
632 error = driver_add_attrs(bus, drv);
633 if (error) {
634 /* How the hell do we get out of this pickle? Give up */
635 printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
636 __FUNCTION__, drv->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400638 error = add_bind_files(drv);
639 if (error) {
640 /* Ditto */
641 printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
642 __FUNCTION__, drv->name);
643 }
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return error;
Andrew Mortonf86db392006-08-14 22:43:20 -0700646out_unregister:
647 kobject_unregister(&drv->kobj);
648out_put_bus:
649 put_bus(bus);
650 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653/**
654 * bus_remove_driver - delete driver from bus's knowledge.
655 * @drv: driver.
656 *
657 * Detach the driver from the devices it controls, and remove
658 * it from its bus's list of drivers. Finally, we drop the reference
659 * to the bus we took in bus_add_driver().
660 */
661
662void bus_remove_driver(struct device_driver * drv)
663{
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400664 if (!drv->bus)
665 return;
666
667 remove_bind_files(drv);
668 driver_remove_attrs(drv->bus, drv);
669 klist_remove(&drv->knode_bus);
670 pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
671 driver_detach(drv);
672 module_remove_driver(drv);
673 kobject_unregister(&drv->kobj);
674 put_bus(drv->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
677
678/* Helper for bus_rescan_devices's iter */
Andrew Mortonf86db392006-08-14 22:43:20 -0700679static int __must_check bus_rescan_devices_helper(struct device *dev,
680 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Andrew Mortonf86db392006-08-14 22:43:20 -0700682 int ret = 0;
683
Alan Sternbf74ad52005-11-17 16:54:12 -0500684 if (!dev->driver) {
685 if (dev->parent) /* Needed for USB */
686 down(&dev->parent->sem);
Andrew Mortonf86db392006-08-14 22:43:20 -0700687 ret = device_attach(dev);
Alan Sternbf74ad52005-11-17 16:54:12 -0500688 if (dev->parent)
689 up(&dev->parent->sem);
690 }
Andrew Mortonf86db392006-08-14 22:43:20 -0700691 return ret < 0 ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694/**
Greg Kroah-Hartman23d3d602005-06-22 16:09:05 -0700695 * bus_rescan_devices - rescan devices on the bus for possible drivers
696 * @bus: the bus to scan.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 *
Greg Kroah-Hartman23d3d602005-06-22 16:09:05 -0700698 * This function will look for devices on the bus with no driver
699 * attached and rescan it against existing drivers to see if it matches
700 * any by calling device_attach() for the unbound devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700702int bus_rescan_devices(struct bus_type * bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Andrew Mortonf86db392006-08-14 22:43:20 -0700704 return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
Moore, Erice935d5d2006-03-14 09:18:18 -0700707/**
708 * device_reprobe - remove driver for a device and probe for a new driver
709 * @dev: the device to reprobe
710 *
711 * This function detaches the attached driver (if any) for the given
712 * device and restarts the driver probing process. It is intended
713 * to use if probing criteria changed during a devices lifetime and
714 * driver attachment should change accordingly.
715 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700716int device_reprobe(struct device *dev)
Moore, Erice935d5d2006-03-14 09:18:18 -0700717{
718 if (dev->driver) {
719 if (dev->parent) /* Needed for USB */
720 down(&dev->parent->sem);
721 device_release_driver(dev);
722 if (dev->parent)
723 up(&dev->parent->sem);
724 }
Andrew Mortonf86db392006-08-14 22:43:20 -0700725 return bus_rescan_devices_helper(dev, NULL);
Moore, Erice935d5d2006-03-14 09:18:18 -0700726}
727EXPORT_SYMBOL_GPL(device_reprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Andrew Mortonf86db392006-08-14 22:43:20 -0700729struct bus_type *get_bus(struct bus_type *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Greg Kroah-Hartman1ef4cfa2007-09-12 15:06:57 -0700731 return bus ? container_of(kset_get(&bus->subsys),
Andrew Mortonf86db392006-08-14 22:43:20 -0700732 struct bus_type, subsys) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
735void put_bus(struct bus_type * bus)
736{
Greg Kroah-Hartman6e9d9302007-09-12 15:06:57 -0700737 kset_put(&bus->subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
740
741/**
742 * find_bus - locate bus by name.
743 * @name: name of bus.
744 *
745 * Call kset_find_obj() to iterate over list of buses to
746 * find a bus by name. Return bus if found.
747 *
748 * Note that kset_find_obj increments bus' reference count.
749 */
Adrian Bunk7e4ef082006-06-26 22:26:56 +0200750#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751struct bus_type * find_bus(char * name)
752{
753 struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
754 return k ? to_bus(k) : NULL;
755}
Adrian Bunk7e4ef082006-06-26 22:26:56 +0200756#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758
759/**
760 * bus_add_attrs - Add default attributes for this bus.
761 * @bus: Bus that has just been registered.
762 */
763
764static int bus_add_attrs(struct bus_type * bus)
765{
766 int error = 0;
767 int i;
768
769 if (bus->bus_attrs) {
770 for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700771 error = bus_create_file(bus,&bus->bus_attrs[i]);
772 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 goto Err;
774 }
775 }
776 Done:
777 return error;
778 Err:
779 while (--i >= 0)
780 bus_remove_file(bus,&bus->bus_attrs[i]);
781 goto Done;
782}
783
784static void bus_remove_attrs(struct bus_type * bus)
785{
786 int i;
787
788 if (bus->bus_attrs) {
789 for (i = 0; attr_name(bus->bus_attrs[i]); i++)
790 bus_remove_file(bus,&bus->bus_attrs[i]);
791 }
792}
793
James Bottomley34bb61f2005-09-06 16:56:51 -0700794static void klist_devices_get(struct klist_node *n)
795{
796 struct device *dev = container_of(n, struct device, knode_bus);
797
798 get_device(dev);
799}
800
801static void klist_devices_put(struct klist_node *n)
802{
803 struct device *dev = container_of(n, struct device, knode_bus);
804
805 put_device(dev);
806}
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/**
809 * bus_register - register a bus with the system.
810 * @bus: bus.
811 *
812 * Once we have that, we registered the bus with the kobject
813 * infrastructure, then register the children subsystems it has:
814 * the devices and drivers that belong to the bus.
815 */
816int bus_register(struct bus_type * bus)
817{
818 int retval;
819
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000820 BLOCKING_INIT_NOTIFIER_HEAD(&bus->bus_notifier);
821
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700822 retval = kobject_set_name(&bus->subsys.kobj, "%s", bus->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (retval)
824 goto out;
825
Greg Kroah-Hartmand6b05b82007-09-12 15:06:57 -0700826 bus->subsys.kobj.kset = &bus_subsys;
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 retval = subsystem_register(&bus->subsys);
829 if (retval)
830 goto out;
831
832 kobject_set_name(&bus->devices.kobj, "devices");
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700833 bus->devices.kobj.parent = &bus->subsys.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 retval = kset_register(&bus->devices);
835 if (retval)
836 goto bus_devices_fail;
837
838 kobject_set_name(&bus->drivers.kobj, "drivers");
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700839 bus->drivers.kobj.parent = &bus->subsys.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 bus->drivers.ktype = &ktype_driver;
841 retval = kset_register(&bus->drivers);
842 if (retval)
843 goto bus_drivers_fail;
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800844
James Bottomley34bb61f2005-09-06 16:56:51 -0700845 klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put);
Alan Stern81107bf2006-09-18 16:24:28 -0400846 klist_init(&bus->klist_drivers, NULL, NULL);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100847
848 bus->drivers_autoprobe = 1;
849 retval = add_probe_files(bus);
850 if (retval)
851 goto bus_probe_files_fail;
852
Cornelia Huck1bb68812006-09-22 11:37:04 +0200853 retval = bus_add_attrs(bus);
854 if (retval)
855 goto bus_attrs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 pr_debug("bus type '%s' registered\n", bus->name);
858 return 0;
859
Cornelia Huck1bb68812006-09-22 11:37:04 +0200860bus_attrs_fail:
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100861 remove_probe_files(bus);
862bus_probe_files_fail:
Cornelia Huck1bb68812006-09-22 11:37:04 +0200863 kset_unregister(&bus->drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864bus_drivers_fail:
865 kset_unregister(&bus->devices);
866bus_devices_fail:
867 subsystem_unregister(&bus->subsys);
868out:
869 return retval;
870}
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872/**
873 * bus_unregister - remove a bus from the system
874 * @bus: bus.
875 *
876 * Unregister the child subsystems and the bus itself.
877 * Finally, we call put_bus() to release the refcount
878 */
879void bus_unregister(struct bus_type * bus)
880{
881 pr_debug("bus %s: unregistering\n", bus->name);
882 bus_remove_attrs(bus);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100883 remove_probe_files(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 kset_unregister(&bus->drivers);
885 kset_unregister(&bus->devices);
886 subsystem_unregister(&bus->subsys);
887}
888
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000889int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
890{
891 return blocking_notifier_chain_register(&bus->bus_notifier, nb);
892}
893EXPORT_SYMBOL_GPL(bus_register_notifier);
894
895int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
896{
897 return blocking_notifier_chain_unregister(&bus->bus_notifier, nb);
898}
899EXPORT_SYMBOL_GPL(bus_unregister_notifier);
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901int __init buses_init(void)
902{
903 return subsystem_register(&bus_subsys);
904}
905
906
907EXPORT_SYMBOL_GPL(bus_for_each_dev);
Cornelia Huck0edb5862005-06-22 16:59:51 +0200908EXPORT_SYMBOL_GPL(bus_find_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909EXPORT_SYMBOL_GPL(bus_for_each_drv);
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911EXPORT_SYMBOL_GPL(bus_register);
912EXPORT_SYMBOL_GPL(bus_unregister);
913EXPORT_SYMBOL_GPL(bus_rescan_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915EXPORT_SYMBOL_GPL(bus_create_file);
916EXPORT_SYMBOL_GPL(bus_remove_file);