blob: 4f53b758ac2b672ce970a7ac1887491f3e502d5b [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
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -070033static void bus_put(struct bus_type *bus)
34{
35 kset_put(&bus->subsys);
36}
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static ssize_t
39drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
40{
41 struct driver_attribute * drv_attr = to_drv_attr(attr);
42 struct device_driver * drv = to_driver(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050043 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 if (drv_attr->show)
46 ret = drv_attr->show(drv, buf);
47 return ret;
48}
49
50static ssize_t
51drv_attr_store(struct kobject * kobj, struct attribute * attr,
52 const char * buf, size_t count)
53{
54 struct driver_attribute * drv_attr = to_drv_attr(attr);
55 struct device_driver * drv = to_driver(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050056 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58 if (drv_attr->store)
59 ret = drv_attr->store(drv, buf, count);
60 return ret;
61}
62
63static struct sysfs_ops driver_sysfs_ops = {
64 .show = drv_attr_show,
65 .store = drv_attr_store,
66};
67
68
69static void driver_release(struct kobject * kobj)
70{
Greg Kroah-Hartman74e9f5f2002-04-09 12:14:34 -070071 /*
72 * Yes this is an empty release function, it is this way because struct
73 * device is always a static object, not a dynamic one. Yes, this is
74 * not nice and bad, but remember, drivers are code, reference counted
75 * by the module count, not a device, which is really data. And yes,
76 * in the future I do want to have all drivers be created dynamically,
77 * and am working toward that goal, but it will take a bit longer...
78 *
79 * But do not let this example give _anyone_ the idea that they can
80 * create a release function without any code in it at all, to do that
81 * is almost always wrong. If you have any questions about this,
82 * please send an email to <greg@kroah.com>
83 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86static struct kobj_type ktype_driver = {
87 .sysfs_ops = &driver_sysfs_ops,
88 .release = driver_release,
89};
90
91
92/*
93 * sysfs bindings for buses
94 */
95
96
97static ssize_t
98bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
99{
100 struct bus_attribute * bus_attr = to_bus_attr(attr);
101 struct bus_type * bus = to_bus(kobj);
102 ssize_t ret = 0;
103
104 if (bus_attr->show)
105 ret = bus_attr->show(bus, buf);
106 return ret;
107}
108
109static ssize_t
110bus_attr_store(struct kobject * kobj, struct attribute * attr,
111 const char * buf, size_t count)
112{
113 struct bus_attribute * bus_attr = to_bus_attr(attr);
114 struct bus_type * bus = to_bus(kobj);
115 ssize_t ret = 0;
116
117 if (bus_attr->store)
118 ret = bus_attr->store(bus, buf, count);
119 return ret;
120}
121
122static struct sysfs_ops bus_sysfs_ops = {
123 .show = bus_attr_show,
124 .store = bus_attr_store,
125};
126
127int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
128{
129 int error;
130 if (get_bus(bus)) {
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700131 error = sysfs_create_file(&bus->subsys.kobj, &attr->attr);
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700132 bus_put(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 } else
134 error = -EINVAL;
135 return error;
136}
137
138void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
139{
140 if (get_bus(bus)) {
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700141 sysfs_remove_file(&bus->subsys.kobj, &attr->attr);
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700142 bus_put(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144}
145
Kay Sievers80f03e32007-05-26 11:21:36 +0200146static struct kobj_type bus_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 .sysfs_ops = &bus_sysfs_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
Kay Sievers80f03e32007-05-26 11:21:36 +0200150static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
151{
152 struct kobj_type *ktype = get_ktype(kobj);
153
154 if (ktype == &bus_ktype)
155 return 1;
156 return 0;
157}
158
159static struct kset_uevent_ops bus_uevent_ops = {
160 .filter = bus_uevent_filter,
161};
162
163static decl_subsys(bus, &bus_ktype, &bus_uevent_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Russell King2139bdd2006-02-07 12:58:20 -0800166#ifdef CONFIG_HOTPLUG
Alan Stern2b08c8d2005-11-23 15:43:50 -0800167/* Manually detach a device from its associated driver. */
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700168static int driver_helper(struct device *dev, void *data)
169{
170 const char *name = data;
171
172 if (strcmp(name, dev->bus_id) == 0)
173 return 1;
174 return 0;
175}
176
177static ssize_t driver_unbind(struct device_driver *drv,
178 const char *buf, size_t count)
179{
180 struct bus_type *bus = get_bus(drv->bus);
181 struct device *dev;
182 int err = -ENODEV;
183
184 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
Alan Stern2b08c8d2005-11-23 15:43:50 -0800185 if (dev && dev->driver == drv) {
Alan Sternbf74ad52005-11-17 16:54:12 -0500186 if (dev->parent) /* Needed for USB */
187 down(&dev->parent->sem);
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700188 device_release_driver(dev);
Alan Sternbf74ad52005-11-17 16:54:12 -0500189 if (dev->parent)
190 up(&dev->parent->sem);
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700191 err = count;
192 }
Alan Stern2b08c8d2005-11-23 15:43:50 -0800193 put_device(dev);
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700194 bus_put(bus);
Alan Stern2b08c8d2005-11-23 15:43:50 -0800195 return err;
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700196}
197static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
198
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700199/*
200 * Manually attach a device to a driver.
201 * Note: the driver must want to bind to the device,
202 * it is not possible to override the driver's id table.
203 */
204static ssize_t driver_bind(struct device_driver *drv,
205 const char *buf, size_t count)
206{
207 struct bus_type *bus = get_bus(drv->bus);
208 struct device *dev;
209 int err = -ENODEV;
210
211 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
Alan Stern2b08c8d2005-11-23 15:43:50 -0800212 if (dev && dev->driver == NULL) {
Alan Sternbf74ad52005-11-17 16:54:12 -0500213 if (dev->parent) /* Needed for USB */
214 down(&dev->parent->sem);
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700215 down(&dev->sem);
216 err = driver_probe_device(drv, dev);
217 up(&dev->sem);
Alan Sternbf74ad52005-11-17 16:54:12 -0500218 if (dev->parent)
219 up(&dev->parent->sem);
Ryan Wilson37225402006-03-22 16:26:25 -0500220
221 if (err > 0) /* success */
222 err = count;
223 else if (err == 0) /* driver didn't accept device */
224 err = -ENODEV;
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700225 }
Alan Stern2b08c8d2005-11-23 15:43:50 -0800226 put_device(dev);
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700227 bus_put(bus);
Alan Stern2b08c8d2005-11-23 15:43:50 -0800228 return err;
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700229}
230static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
231
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100232static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
233{
234 return sprintf(buf, "%d\n", bus->drivers_autoprobe);
235}
236
237static ssize_t store_drivers_autoprobe(struct bus_type *bus,
238 const char *buf, size_t count)
239{
240 if (buf[0] == '0')
241 bus->drivers_autoprobe = 0;
242 else
243 bus->drivers_autoprobe = 1;
244 return count;
245}
246
247static ssize_t store_drivers_probe(struct bus_type *bus,
248 const char *buf, size_t count)
249{
250 struct device *dev;
251
252 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
253 if (!dev)
254 return -ENODEV;
255 if (bus_rescan_devices_helper(dev, NULL) != 0)
256 return -EINVAL;
257 return count;
258}
Russell King2139bdd2006-02-07 12:58:20 -0800259#endif
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700260
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800261static struct device * next_device(struct klist_iter * i)
262{
263 struct klist_node * n = klist_next(i);
264 return n ? container_of(n, struct device, knode_bus) : NULL;
265}
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/**
268 * bus_for_each_dev - device iterator.
269 * @bus: bus type.
270 * @start: device to start iterating from.
271 * @data: data for the callback.
272 * @fn: function to be called for each device.
273 *
274 * Iterate over @bus's list of devices, and call @fn for each,
275 * passing it @data. If @start is not NULL, we use that device to
276 * begin iterating from.
277 *
278 * We check the return of @fn each time. If it returns anything
279 * other than 0, we break out and return that value.
280 *
281 * NOTE: The device that returns a non-zero value is not retained
282 * in any way, nor is its refcount incremented. If the caller needs
283 * to retain this data, it should do, and increment the reference
284 * count in the supplied callback.
285 */
286
287int bus_for_each_dev(struct bus_type * bus, struct device * start,
288 void * data, int (*fn)(struct device *, void *))
289{
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800290 struct klist_iter i;
291 struct device * dev;
292 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800294 if (!bus)
295 return -EINVAL;
296
297 klist_iter_init_node(&bus->klist_devices, &i,
298 (start ? &start->knode_bus : NULL));
299 while ((dev = next_device(&i)) && !error)
300 error = fn(dev, data);
301 klist_iter_exit(&i);
302 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
Cornelia Huck0edb5862005-06-22 16:59:51 +0200305/**
306 * bus_find_device - device iterator for locating a particular device.
307 * @bus: bus type
308 * @start: Device to begin with
309 * @data: Data to pass to match function
310 * @match: Callback function to check device
311 *
312 * This is similar to the bus_for_each_dev() function above, but it
313 * returns a reference to a device that is 'found' for later use, as
314 * determined by the @match callback.
315 *
316 * The callback should return 0 if the device doesn't match and non-zero
317 * if it does. If the callback returns non-zero, this function will
318 * return to the caller and not iterate over any more devices.
319 */
320struct device * bus_find_device(struct bus_type *bus,
321 struct device *start, void *data,
322 int (*match)(struct device *, void *))
323{
324 struct klist_iter i;
325 struct device *dev;
326
327 if (!bus)
328 return NULL;
329
330 klist_iter_init_node(&bus->klist_devices, &i,
331 (start ? &start->knode_bus : NULL));
332 while ((dev = next_device(&i)))
333 if (match(dev, data) && get_device(dev))
334 break;
335 klist_iter_exit(&i);
336 return dev;
337}
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800338
339
340static struct device_driver * next_driver(struct klist_iter * i)
341{
342 struct klist_node * n = klist_next(i);
343 return n ? container_of(n, struct device_driver, knode_bus) : NULL;
344}
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346/**
347 * bus_for_each_drv - driver iterator
348 * @bus: bus we're dealing with.
349 * @start: driver to start iterating on.
350 * @data: data to pass to the callback.
351 * @fn: function to call for each driver.
352 *
353 * This is nearly identical to the device iterator above.
354 * We iterate over each driver that belongs to @bus, and call
355 * @fn for each. If @fn returns anything but 0, we break out
356 * and return it. If @start is not NULL, we use it as the head
357 * of the list.
358 *
359 * NOTE: we don't return the driver that returns a non-zero
360 * value, nor do we leave the reference count incremented for that
361 * driver. If the caller needs to know that info, it must set it
362 * in the callback. It must also be sure to increment the refcount
363 * so it doesn't disappear before returning to the caller.
364 */
365
366int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
367 void * data, int (*fn)(struct device_driver *, void *))
368{
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800369 struct klist_iter i;
370 struct device_driver * drv;
371 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800373 if (!bus)
374 return -EINVAL;
375
376 klist_iter_init_node(&bus->klist_drivers, &i,
377 start ? &start->knode_bus : NULL);
378 while ((drv = next_driver(&i)) && !error)
379 error = fn(drv, data);
380 klist_iter_exit(&i);
381 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
Andrew Morton4aca67e2007-02-13 22:39:26 -0800384static int device_add_attrs(struct bus_type *bus, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 int error = 0;
387 int i;
388
Andrew Morton4aca67e2007-02-13 22:39:26 -0800389 if (!bus->dev_attrs)
390 return 0;
391
392 for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
393 error = device_create_file(dev,&bus->dev_attrs[i]);
394 if (error) {
395 while (--i >= 0)
396 device_remove_file(dev, &bus->dev_attrs[i]);
397 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403static void device_remove_attrs(struct bus_type * bus, struct device * dev)
404{
405 int i;
406
407 if (bus->dev_attrs) {
408 for (i = 0; attr_name(bus->dev_attrs[i]); i++)
409 device_remove_file(dev,&bus->dev_attrs[i]);
410 }
411}
412
Kay Sieversb9cafc72006-09-14 11:23:28 +0200413#ifdef CONFIG_SYSFS_DEPRECATED
414static int make_deprecated_bus_links(struct device *dev)
415{
416 return sysfs_create_link(&dev->kobj,
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700417 &dev->bus->subsys.kobj, "bus");
Kay Sieversb9cafc72006-09-14 11:23:28 +0200418}
419
420static void remove_deprecated_bus_links(struct device *dev)
421{
422 sysfs_remove_link(&dev->kobj, "bus");
423}
424#else
425static inline int make_deprecated_bus_links(struct device *dev) { return 0; }
426static inline void remove_deprecated_bus_links(struct device *dev) { }
427#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429/**
430 * bus_add_device - add device to bus
431 * @dev: device being added
432 *
433 * - Add the device to its bus's list of devices.
Kay Sievers53877d02006-04-04 20:42:26 +0200434 * - Create link to device's bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 */
436int bus_add_device(struct device * dev)
437{
438 struct bus_type * bus = get_bus(dev->bus);
439 int error = 0;
440
441 if (bus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
Greg Kroah-Hartmand377e852005-06-22 16:09:05 -0700443 error = device_add_attrs(bus, dev);
Andrew Mortonf86db392006-08-14 22:43:20 -0700444 if (error)
Cornelia Huck513e7332006-09-22 11:37:08 +0200445 goto out_put;
Andrew Mortonf86db392006-08-14 22:43:20 -0700446 error = sysfs_create_link(&bus->devices.kobj,
447 &dev->kobj, dev->bus_id);
448 if (error)
Cornelia Huck513e7332006-09-22 11:37:08 +0200449 goto out_id;
Andrew Mortonf86db392006-08-14 22:43:20 -0700450 error = sysfs_create_link(&dev->kobj,
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700451 &dev->bus->subsys.kobj, "subsystem");
Andrew Mortonf86db392006-08-14 22:43:20 -0700452 if (error)
Cornelia Huck513e7332006-09-22 11:37:08 +0200453 goto out_subsys;
Kay Sieversb9cafc72006-09-14 11:23:28 +0200454 error = make_deprecated_bus_links(dev);
Cornelia Huck513e7332006-09-22 11:37:08 +0200455 if (error)
456 goto out_deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
Cornelia Huck513e7332006-09-22 11:37:08 +0200458 return 0;
459
460out_deprecated:
461 sysfs_remove_link(&dev->kobj, "subsystem");
462out_subsys:
463 sysfs_remove_link(&bus->devices.kobj, dev->bus_id);
464out_id:
465 device_remove_attrs(bus, dev);
466out_put:
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700467 bus_put(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return error;
469}
470
471/**
Kay Sievers53877d02006-04-04 20:42:26 +0200472 * bus_attach_device - add device to bus
473 * @dev: device tried to attach to a driver
474 *
Alan Sternf2eaae12006-09-18 16:22:34 -0400475 * - Add device to bus's list of devices.
Kay Sievers53877d02006-04-04 20:42:26 +0200476 * - Try to attach to driver.
477 */
Cornelia Huckc6a46692007-02-05 16:15:26 -0800478void bus_attach_device(struct device * dev)
Kay Sievers53877d02006-04-04 20:42:26 +0200479{
Andrew Mortonf86db392006-08-14 22:43:20 -0700480 struct bus_type *bus = dev->bus;
481 int ret = 0;
Kay Sievers53877d02006-04-04 20:42:26 +0200482
483 if (bus) {
Alan Sternf2eaae12006-09-18 16:22:34 -0400484 dev->is_registered = 1;
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100485 if (bus->drivers_autoprobe)
486 ret = device_attach(dev);
Cornelia Huckc6a46692007-02-05 16:15:26 -0800487 WARN_ON(ret < 0);
488 if (ret >= 0)
Andrew Mortonf86db392006-08-14 22:43:20 -0700489 klist_add_tail(&dev->knode_bus, &bus->klist_devices);
Cornelia Huckc6a46692007-02-05 16:15:26 -0800490 else
Alan Sternf2eaae12006-09-18 16:22:34 -0400491 dev->is_registered = 0;
Kay Sievers53877d02006-04-04 20:42:26 +0200492 }
493}
494
495/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * bus_remove_device - remove device from bus
497 * @dev: device to be removed
498 *
499 * - Remove symlink from bus's directory.
500 * - Delete device from bus's list.
501 * - Detach from its driver.
502 * - Drop reference taken in bus_add_device().
503 */
504void bus_remove_device(struct device * dev)
505{
506 if (dev->bus) {
Kay Sieversb9d9c822006-06-15 15:31:56 +0200507 sysfs_remove_link(&dev->kobj, "subsystem");
Kay Sieversb9cafc72006-09-14 11:23:28 +0200508 remove_deprecated_bus_links(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
510 device_remove_attrs(dev->bus, dev);
Alan Sternf70fa622006-10-05 17:03:24 -0400511 if (dev->is_registered) {
512 dev->is_registered = 0;
513 klist_del(&dev->knode_bus);
514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
516 device_release_driver(dev);
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700517 bus_put(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
519}
520
521static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
522{
523 int error = 0;
524 int i;
525
526 if (bus->drv_attrs) {
527 for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
528 error = driver_create_file(drv, &bus->drv_attrs[i]);
529 if (error)
530 goto Err;
531 }
532 }
533 Done:
534 return error;
535 Err:
536 while (--i >= 0)
537 driver_remove_file(drv, &bus->drv_attrs[i]);
538 goto Done;
539}
540
541
542static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
543{
544 int i;
545
546 if (bus->drv_attrs) {
547 for (i = 0; attr_name(bus->drv_attrs[i]); i++)
548 driver_remove_file(drv, &bus->drv_attrs[i]);
549 }
550}
551
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800552#ifdef CONFIG_HOTPLUG
553/*
554 * Thanks to drivers making their tables __devinit, we can't allow manual
555 * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
556 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700557static int __must_check add_bind_files(struct device_driver *drv)
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800558{
Andrew Mortonf86db392006-08-14 22:43:20 -0700559 int ret;
560
561 ret = driver_create_file(drv, &driver_attr_unbind);
562 if (ret == 0) {
563 ret = driver_create_file(drv, &driver_attr_bind);
564 if (ret)
565 driver_remove_file(drv, &driver_attr_unbind);
566 }
567 return ret;
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800568}
569
570static void remove_bind_files(struct device_driver *drv)
571{
572 driver_remove_file(drv, &driver_attr_bind);
573 driver_remove_file(drv, &driver_attr_unbind);
574}
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100575
Kay Sievers83807702007-07-30 02:28:56 +0200576static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
577static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
578 show_drivers_autoprobe, store_drivers_autoprobe);
579
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100580static int add_probe_files(struct bus_type *bus)
581{
582 int retval;
583
Kay Sievers83807702007-07-30 02:28:56 +0200584 retval = bus_create_file(bus, &bus_attr_drivers_probe);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100585 if (retval)
586 goto out;
587
Kay Sievers83807702007-07-30 02:28:56 +0200588 retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100589 if (retval)
Kay Sievers83807702007-07-30 02:28:56 +0200590 bus_remove_file(bus, &bus_attr_drivers_probe);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100591out:
592 return retval;
593}
594
595static void remove_probe_files(struct bus_type *bus)
596{
Kay Sievers83807702007-07-30 02:28:56 +0200597 bus_remove_file(bus, &bus_attr_drivers_autoprobe);
598 bus_remove_file(bus, &bus_attr_drivers_probe);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100599}
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800600#else
Yoichi Yuasa35acfdd2006-07-15 01:30:11 +0900601static inline int add_bind_files(struct device_driver *drv) { return 0; }
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800602static inline void remove_bind_files(struct device_driver *drv) {}
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100603static inline int add_probe_files(struct bus_type *bus) { return 0; }
604static inline void remove_probe_files(struct bus_type *bus) {}
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800605#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607/**
608 * bus_add_driver - Add a driver to the bus.
609 * @drv: driver.
610 *
611 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700612int bus_add_driver(struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 struct bus_type * bus = get_bus(drv->bus);
615 int error = 0;
616
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400617 if (!bus)
Greg Kroah-Hartman4f6e1942007-04-20 11:29:52 -0700618 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400620 pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
621 error = kobject_set_name(&drv->kobj, "%s", drv->name);
622 if (error)
623 goto out_put_bus;
624 drv->kobj.kset = &bus->drivers;
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700625 error = kobject_register(&drv->kobj);
626 if (error)
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400627 goto out_put_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100629 if (drv->bus->drivers_autoprobe) {
630 error = driver_attach(drv);
631 if (error)
632 goto out_unregister;
633 }
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400634 klist_add_tail(&drv->knode_bus, &bus->klist_drivers);
635 module_add_driver(drv->owner, drv);
636
637 error = driver_add_attrs(bus, drv);
638 if (error) {
639 /* How the hell do we get out of this pickle? Give up */
640 printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
641 __FUNCTION__, drv->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400643 error = add_bind_files(drv);
644 if (error) {
645 /* Ditto */
646 printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
647 __FUNCTION__, drv->name);
648 }
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return error;
Andrew Mortonf86db392006-08-14 22:43:20 -0700651out_unregister:
652 kobject_unregister(&drv->kobj);
653out_put_bus:
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700654 bus_put(bus);
Andrew Mortonf86db392006-08-14 22:43:20 -0700655 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658/**
659 * bus_remove_driver - delete driver from bus's knowledge.
660 * @drv: driver.
661 *
662 * Detach the driver from the devices it controls, and remove
663 * it from its bus's list of drivers. Finally, we drop the reference
664 * to the bus we took in bus_add_driver().
665 */
666
667void bus_remove_driver(struct device_driver * drv)
668{
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400669 if (!drv->bus)
670 return;
671
672 remove_bind_files(drv);
673 driver_remove_attrs(drv->bus, drv);
674 klist_remove(&drv->knode_bus);
675 pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
676 driver_detach(drv);
677 module_remove_driver(drv);
678 kobject_unregister(&drv->kobj);
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700679 bus_put(drv->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
682
683/* Helper for bus_rescan_devices's iter */
Andrew Mortonf86db392006-08-14 22:43:20 -0700684static int __must_check bus_rescan_devices_helper(struct device *dev,
685 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Andrew Mortonf86db392006-08-14 22:43:20 -0700687 int ret = 0;
688
Alan Sternbf74ad52005-11-17 16:54:12 -0500689 if (!dev->driver) {
690 if (dev->parent) /* Needed for USB */
691 down(&dev->parent->sem);
Andrew Mortonf86db392006-08-14 22:43:20 -0700692 ret = device_attach(dev);
Alan Sternbf74ad52005-11-17 16:54:12 -0500693 if (dev->parent)
694 up(&dev->parent->sem);
695 }
Andrew Mortonf86db392006-08-14 22:43:20 -0700696 return ret < 0 ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699/**
Greg Kroah-Hartman23d3d602005-06-22 16:09:05 -0700700 * bus_rescan_devices - rescan devices on the bus for possible drivers
701 * @bus: the bus to scan.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 *
Greg Kroah-Hartman23d3d602005-06-22 16:09:05 -0700703 * This function will look for devices on the bus with no driver
704 * attached and rescan it against existing drivers to see if it matches
705 * any by calling device_attach() for the unbound devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700707int bus_rescan_devices(struct bus_type * bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Andrew Mortonf86db392006-08-14 22:43:20 -0700709 return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Moore, Erice935d5d2006-03-14 09:18:18 -0700712/**
713 * device_reprobe - remove driver for a device and probe for a new driver
714 * @dev: the device to reprobe
715 *
716 * This function detaches the attached driver (if any) for the given
717 * device and restarts the driver probing process. It is intended
718 * to use if probing criteria changed during a devices lifetime and
719 * driver attachment should change accordingly.
720 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700721int device_reprobe(struct device *dev)
Moore, Erice935d5d2006-03-14 09:18:18 -0700722{
723 if (dev->driver) {
724 if (dev->parent) /* Needed for USB */
725 down(&dev->parent->sem);
726 device_release_driver(dev);
727 if (dev->parent)
728 up(&dev->parent->sem);
729 }
Andrew Mortonf86db392006-08-14 22:43:20 -0700730 return bus_rescan_devices_helper(dev, NULL);
Moore, Erice935d5d2006-03-14 09:18:18 -0700731}
732EXPORT_SYMBOL_GPL(device_reprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Andrew Mortonf86db392006-08-14 22:43:20 -0700734struct bus_type *get_bus(struct bus_type *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Greg Kroah-Hartman1ef4cfa2007-09-12 15:06:57 -0700736 return bus ? container_of(kset_get(&bus->subsys),
Andrew Mortonf86db392006-08-14 22:43:20 -0700737 struct bus_type, subsys) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740/**
741 * find_bus - locate bus by name.
742 * @name: name of bus.
743 *
744 * Call kset_find_obj() to iterate over list of buses to
745 * find a bus by name. Return bus if found.
746 *
747 * Note that kset_find_obj increments bus' reference count.
748 */
Adrian Bunk7e4ef082006-06-26 22:26:56 +0200749#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750struct bus_type * find_bus(char * name)
751{
752 struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
753 return k ? to_bus(k) : NULL;
754}
Adrian Bunk7e4ef082006-06-26 22:26:56 +0200755#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757
758/**
759 * bus_add_attrs - Add default attributes for this bus.
760 * @bus: Bus that has just been registered.
761 */
762
763static int bus_add_attrs(struct bus_type * bus)
764{
765 int error = 0;
766 int i;
767
768 if (bus->bus_attrs) {
769 for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700770 error = bus_create_file(bus,&bus->bus_attrs[i]);
771 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 goto Err;
773 }
774 }
775 Done:
776 return error;
777 Err:
778 while (--i >= 0)
779 bus_remove_file(bus,&bus->bus_attrs[i]);
780 goto Done;
781}
782
783static void bus_remove_attrs(struct bus_type * bus)
784{
785 int i;
786
787 if (bus->bus_attrs) {
788 for (i = 0; attr_name(bus->bus_attrs[i]); i++)
789 bus_remove_file(bus,&bus->bus_attrs[i]);
790 }
791}
792
James Bottomley34bb61f2005-09-06 16:56:51 -0700793static void klist_devices_get(struct klist_node *n)
794{
795 struct device *dev = container_of(n, struct device, knode_bus);
796
797 get_device(dev);
798}
799
800static void klist_devices_put(struct klist_node *n)
801{
802 struct device *dev = container_of(n, struct device, knode_bus);
803
804 put_device(dev);
805}
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807/**
808 * bus_register - register a bus with the system.
809 * @bus: bus.
810 *
811 * Once we have that, we registered the bus with the kobject
812 * infrastructure, then register the children subsystems it has:
813 * the devices and drivers that belong to the bus.
814 */
815int bus_register(struct bus_type * bus)
816{
817 int retval;
818
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000819 BLOCKING_INIT_NOTIFIER_HEAD(&bus->bus_notifier);
820
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700821 retval = kobject_set_name(&bus->subsys.kobj, "%s", bus->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (retval)
823 goto out;
824
Greg Kroah-Hartmand6b05b82007-09-12 15:06:57 -0700825 bus->subsys.kobj.kset = &bus_subsys;
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 retval = subsystem_register(&bus->subsys);
828 if (retval)
829 goto out;
830
831 kobject_set_name(&bus->devices.kobj, "devices");
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700832 bus->devices.kobj.parent = &bus->subsys.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 retval = kset_register(&bus->devices);
834 if (retval)
835 goto bus_devices_fail;
836
837 kobject_set_name(&bus->drivers.kobj, "drivers");
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700838 bus->drivers.kobj.parent = &bus->subsys.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 bus->drivers.ktype = &ktype_driver;
840 retval = kset_register(&bus->drivers);
841 if (retval)
842 goto bus_drivers_fail;
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800843
James Bottomley34bb61f2005-09-06 16:56:51 -0700844 klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put);
Alan Stern81107bf2006-09-18 16:24:28 -0400845 klist_init(&bus->klist_drivers, NULL, NULL);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100846
847 bus->drivers_autoprobe = 1;
848 retval = add_probe_files(bus);
849 if (retval)
850 goto bus_probe_files_fail;
851
Cornelia Huck1bb68812006-09-22 11:37:04 +0200852 retval = bus_add_attrs(bus);
853 if (retval)
854 goto bus_attrs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 pr_debug("bus type '%s' registered\n", bus->name);
857 return 0;
858
Cornelia Huck1bb68812006-09-22 11:37:04 +0200859bus_attrs_fail:
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100860 remove_probe_files(bus);
861bus_probe_files_fail:
Cornelia Huck1bb68812006-09-22 11:37:04 +0200862 kset_unregister(&bus->drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863bus_drivers_fail:
864 kset_unregister(&bus->devices);
865bus_devices_fail:
866 subsystem_unregister(&bus->subsys);
867out:
868 return retval;
869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/**
872 * bus_unregister - remove a bus from the system
873 * @bus: bus.
874 *
875 * Unregister the child subsystems and the bus itself.
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700876 * Finally, we call bus_put() to release the refcount
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 */
878void bus_unregister(struct bus_type * bus)
879{
880 pr_debug("bus %s: unregistering\n", bus->name);
881 bus_remove_attrs(bus);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100882 remove_probe_files(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 kset_unregister(&bus->drivers);
884 kset_unregister(&bus->devices);
885 subsystem_unregister(&bus->subsys);
886}
887
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000888int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
889{
890 return blocking_notifier_chain_register(&bus->bus_notifier, nb);
891}
892EXPORT_SYMBOL_GPL(bus_register_notifier);
893
894int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
895{
896 return blocking_notifier_chain_unregister(&bus->bus_notifier, nb);
897}
898EXPORT_SYMBOL_GPL(bus_unregister_notifier);
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900int __init buses_init(void)
901{
902 return subsystem_register(&bus_subsys);
903}
904
905
906EXPORT_SYMBOL_GPL(bus_for_each_dev);
Cornelia Huck0edb5862005-06-22 16:59:51 +0200907EXPORT_SYMBOL_GPL(bus_find_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908EXPORT_SYMBOL_GPL(bus_for_each_drv);
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910EXPORT_SYMBOL_GPL(bus_register);
911EXPORT_SYMBOL_GPL(bus_unregister);
912EXPORT_SYMBOL_GPL(bus_rescan_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914EXPORT_SYMBOL_GPL(bus_create_file);
915EXPORT_SYMBOL_GPL(bus_remove_file);