blob: 04d3850ff4b7f642125d267b13c6828846ad9a0c [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-Hartmanc6f7e722007-11-01 19:41:16 -070020#define to_bus(obj) container_of(obj, struct bus_type_private, 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-Hartman5901d012007-09-13 02:53:13 -070033static struct bus_type *bus_get(struct bus_type *bus)
34{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -070035 if (bus) {
36 kset_get(&bus->p->subsys);
37 return bus;
38 }
39 return NULL;
Greg Kroah-Hartman5901d012007-09-13 02:53:13 -070040}
41
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -070042static void bus_put(struct bus_type *bus)
43{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -070044 if (bus)
45 kset_put(&bus->p->subsys);
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -070046}
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static ssize_t
49drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
50{
51 struct driver_attribute * drv_attr = to_drv_attr(attr);
52 struct device_driver * drv = to_driver(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050053 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 if (drv_attr->show)
56 ret = drv_attr->show(drv, buf);
57 return ret;
58}
59
60static ssize_t
61drv_attr_store(struct kobject * kobj, struct attribute * attr,
62 const char * buf, size_t count)
63{
64 struct driver_attribute * drv_attr = to_drv_attr(attr);
65 struct device_driver * drv = to_driver(kobj);
Dmitry Torokhov4a0c20b2005-04-29 01:23:47 -050066 ssize_t ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 if (drv_attr->store)
69 ret = drv_attr->store(drv, buf, count);
70 return ret;
71}
72
73static struct sysfs_ops driver_sysfs_ops = {
74 .show = drv_attr_show,
75 .store = drv_attr_store,
76};
77
78
79static void driver_release(struct kobject * kobj)
80{
Greg Kroah-Hartman74e9f5f2002-04-09 12:14:34 -070081 /*
82 * Yes this is an empty release function, it is this way because struct
83 * device is always a static object, not a dynamic one. Yes, this is
84 * not nice and bad, but remember, drivers are code, reference counted
85 * by the module count, not a device, which is really data. And yes,
86 * in the future I do want to have all drivers be created dynamically,
87 * and am working toward that goal, but it will take a bit longer...
88 *
89 * But do not let this example give _anyone_ the idea that they can
90 * create a release function without any code in it at all, to do that
91 * is almost always wrong. If you have any questions about this,
92 * please send an email to <greg@kroah.com>
93 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Greg Kroah-Hartmana1148fb2007-10-11 10:47:49 -060096static struct kobj_type driver_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 .sysfs_ops = &driver_sysfs_ops,
98 .release = driver_release,
99};
100
101
102/*
103 * sysfs bindings for buses
104 */
105
106
107static ssize_t
108bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
109{
110 struct bus_attribute * bus_attr = to_bus_attr(attr);
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700111 struct bus_type_private *bus_priv = to_bus(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 ssize_t ret = 0;
113
114 if (bus_attr->show)
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700115 ret = bus_attr->show(bus_priv->bus, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return ret;
117}
118
119static ssize_t
120bus_attr_store(struct kobject * kobj, struct attribute * attr,
121 const char * buf, size_t count)
122{
123 struct bus_attribute * bus_attr = to_bus_attr(attr);
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700124 struct bus_type_private *bus_priv = to_bus(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 ssize_t ret = 0;
126
127 if (bus_attr->store)
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700128 ret = bus_attr->store(bus_priv->bus, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return ret;
130}
131
132static struct sysfs_ops bus_sysfs_ops = {
133 .show = bus_attr_show,
134 .store = bus_attr_store,
135};
136
137int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
138{
139 int error;
Greg Kroah-Hartman5901d012007-09-13 02:53:13 -0700140 if (bus_get(bus)) {
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700141 error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr);
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700142 bus_put(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 } else
144 error = -EINVAL;
145 return error;
146}
147
148void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
149{
Greg Kroah-Hartman5901d012007-09-13 02:53:13 -0700150 if (bus_get(bus)) {
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700151 sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr);
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700152 bus_put(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154}
155
Kay Sievers80f03e32007-05-26 11:21:36 +0200156static struct kobj_type bus_ktype = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 .sysfs_ops = &bus_sysfs_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158};
159
Kay Sievers80f03e32007-05-26 11:21:36 +0200160static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
161{
162 struct kobj_type *ktype = get_ktype(kobj);
163
164 if (ktype == &bus_ktype)
165 return 1;
166 return 0;
167}
168
169static struct kset_uevent_ops bus_uevent_ops = {
170 .filter = bus_uevent_filter,
171};
172
Greg Kroah-Hartman59a54832007-10-29 23:22:26 -0500173static struct kset *bus_kset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Russell King2139bdd2006-02-07 12:58:20 -0800176#ifdef CONFIG_HOTPLUG
Alan Stern2b08c8d2005-11-23 15:43:50 -0800177/* Manually detach a device from its associated driver. */
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700178static int driver_helper(struct device *dev, void *data)
179{
180 const char *name = data;
181
182 if (strcmp(name, dev->bus_id) == 0)
183 return 1;
184 return 0;
185}
186
187static ssize_t driver_unbind(struct device_driver *drv,
188 const char *buf, size_t count)
189{
Greg Kroah-Hartman5901d012007-09-13 02:53:13 -0700190 struct bus_type *bus = bus_get(drv->bus);
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700191 struct device *dev;
192 int err = -ENODEV;
193
194 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
Alan Stern2b08c8d2005-11-23 15:43:50 -0800195 if (dev && dev->driver == drv) {
Alan Sternbf74ad52005-11-17 16:54:12 -0500196 if (dev->parent) /* Needed for USB */
197 down(&dev->parent->sem);
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700198 device_release_driver(dev);
Alan Sternbf74ad52005-11-17 16:54:12 -0500199 if (dev->parent)
200 up(&dev->parent->sem);
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700201 err = count;
202 }
Alan Stern2b08c8d2005-11-23 15:43:50 -0800203 put_device(dev);
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700204 bus_put(bus);
Alan Stern2b08c8d2005-11-23 15:43:50 -0800205 return err;
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700206}
207static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
208
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700209/*
210 * Manually attach a device to a driver.
211 * Note: the driver must want to bind to the device,
212 * it is not possible to override the driver's id table.
213 */
214static ssize_t driver_bind(struct device_driver *drv,
215 const char *buf, size_t count)
216{
Greg Kroah-Hartman5901d012007-09-13 02:53:13 -0700217 struct bus_type *bus = bus_get(drv->bus);
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700218 struct device *dev;
219 int err = -ENODEV;
220
221 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
Alan Stern2b08c8d2005-11-23 15:43:50 -0800222 if (dev && dev->driver == NULL) {
Alan Sternbf74ad52005-11-17 16:54:12 -0500223 if (dev->parent) /* Needed for USB */
224 down(&dev->parent->sem);
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700225 down(&dev->sem);
226 err = driver_probe_device(drv, dev);
227 up(&dev->sem);
Alan Sternbf74ad52005-11-17 16:54:12 -0500228 if (dev->parent)
229 up(&dev->parent->sem);
Ryan Wilson37225402006-03-22 16:26:25 -0500230
231 if (err > 0) /* success */
232 err = count;
233 else if (err == 0) /* driver didn't accept device */
234 err = -ENODEV;
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700235 }
Alan Stern2b08c8d2005-11-23 15:43:50 -0800236 put_device(dev);
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700237 bus_put(bus);
Alan Stern2b08c8d2005-11-23 15:43:50 -0800238 return err;
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700239}
240static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
241
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100242static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
243{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700244 return sprintf(buf, "%d\n", bus->p->drivers_autoprobe);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100245}
246
247static ssize_t store_drivers_autoprobe(struct bus_type *bus,
248 const char *buf, size_t count)
249{
250 if (buf[0] == '0')
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700251 bus->p->drivers_autoprobe = 0;
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100252 else
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700253 bus->p->drivers_autoprobe = 1;
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100254 return count;
255}
256
257static ssize_t store_drivers_probe(struct bus_type *bus,
258 const char *buf, size_t count)
259{
260 struct device *dev;
261
262 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
263 if (!dev)
264 return -ENODEV;
265 if (bus_rescan_devices_helper(dev, NULL) != 0)
266 return -EINVAL;
267 return count;
268}
Russell King2139bdd2006-02-07 12:58:20 -0800269#endif
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700270
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800271static struct device * next_device(struct klist_iter * i)
272{
273 struct klist_node * n = klist_next(i);
274 return n ? container_of(n, struct device, knode_bus) : NULL;
275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/**
278 * bus_for_each_dev - device iterator.
279 * @bus: bus type.
280 * @start: device to start iterating from.
281 * @data: data for the callback.
282 * @fn: function to be called for each device.
283 *
284 * Iterate over @bus's list of devices, and call @fn for each,
285 * passing it @data. If @start is not NULL, we use that device to
286 * begin iterating from.
287 *
288 * We check the return of @fn each time. If it returns anything
289 * other than 0, we break out and return that value.
290 *
291 * NOTE: The device that returns a non-zero value is not retained
292 * in any way, nor is its refcount incremented. If the caller needs
293 * to retain this data, it should do, and increment the reference
294 * count in the supplied callback.
295 */
296
297int bus_for_each_dev(struct bus_type * bus, struct device * start,
298 void * data, int (*fn)(struct device *, void *))
299{
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800300 struct klist_iter i;
301 struct device * dev;
302 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800304 if (!bus)
305 return -EINVAL;
306
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700307 klist_iter_init_node(&bus->p->klist_devices, &i,
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800308 (start ? &start->knode_bus : NULL));
309 while ((dev = next_device(&i)) && !error)
310 error = fn(dev, data);
311 klist_iter_exit(&i);
312 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
Cornelia Huck0edb5862005-06-22 16:59:51 +0200315/**
316 * bus_find_device - device iterator for locating a particular device.
317 * @bus: bus type
318 * @start: Device to begin with
319 * @data: Data to pass to match function
320 * @match: Callback function to check device
321 *
322 * This is similar to the bus_for_each_dev() function above, but it
323 * returns a reference to a device that is 'found' for later use, as
324 * determined by the @match callback.
325 *
326 * The callback should return 0 if the device doesn't match and non-zero
327 * if it does. If the callback returns non-zero, this function will
328 * return to the caller and not iterate over any more devices.
329 */
330struct device * bus_find_device(struct bus_type *bus,
331 struct device *start, void *data,
332 int (*match)(struct device *, void *))
333{
334 struct klist_iter i;
335 struct device *dev;
336
337 if (!bus)
338 return NULL;
339
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700340 klist_iter_init_node(&bus->p->klist_devices, &i,
Cornelia Huck0edb5862005-06-22 16:59:51 +0200341 (start ? &start->knode_bus : NULL));
342 while ((dev = next_device(&i)))
343 if (match(dev, data) && get_device(dev))
344 break;
345 klist_iter_exit(&i);
346 return dev;
347}
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800348
349
350static struct device_driver * next_driver(struct klist_iter * i)
351{
352 struct klist_node * n = klist_next(i);
353 return n ? container_of(n, struct device_driver, knode_bus) : NULL;
354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/**
357 * bus_for_each_drv - driver iterator
358 * @bus: bus we're dealing with.
359 * @start: driver to start iterating on.
360 * @data: data to pass to the callback.
361 * @fn: function to call for each driver.
362 *
363 * This is nearly identical to the device iterator above.
364 * We iterate over each driver that belongs to @bus, and call
365 * @fn for each. If @fn returns anything but 0, we break out
366 * and return it. If @start is not NULL, we use it as the head
367 * of the list.
368 *
369 * NOTE: we don't return the driver that returns a non-zero
370 * value, nor do we leave the reference count incremented for that
371 * driver. If the caller needs to know that info, it must set it
372 * in the callback. It must also be sure to increment the refcount
373 * so it doesn't disappear before returning to the caller.
374 */
375
376int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
377 void * data, int (*fn)(struct device_driver *, void *))
378{
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800379 struct klist_iter i;
380 struct device_driver * drv;
381 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800383 if (!bus)
384 return -EINVAL;
385
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700386 klist_iter_init_node(&bus->p->klist_drivers, &i,
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800387 start ? &start->knode_bus : NULL);
388 while ((drv = next_driver(&i)) && !error)
389 error = fn(drv, data);
390 klist_iter_exit(&i);
391 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
Andrew Morton4aca67e2007-02-13 22:39:26 -0800394static int device_add_attrs(struct bus_type *bus, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 int error = 0;
397 int i;
398
Andrew Morton4aca67e2007-02-13 22:39:26 -0800399 if (!bus->dev_attrs)
400 return 0;
401
402 for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
403 error = device_create_file(dev,&bus->dev_attrs[i]);
404 if (error) {
405 while (--i >= 0)
406 device_remove_file(dev, &bus->dev_attrs[i]);
407 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413static void device_remove_attrs(struct bus_type * bus, struct device * dev)
414{
415 int i;
416
417 if (bus->dev_attrs) {
418 for (i = 0; attr_name(bus->dev_attrs[i]); i++)
419 device_remove_file(dev,&bus->dev_attrs[i]);
420 }
421}
422
Kay Sieversb9cafc72006-09-14 11:23:28 +0200423#ifdef CONFIG_SYSFS_DEPRECATED
424static int make_deprecated_bus_links(struct device *dev)
425{
426 return sysfs_create_link(&dev->kobj,
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700427 &dev->bus->p->subsys.kobj, "bus");
Kay Sieversb9cafc72006-09-14 11:23:28 +0200428}
429
430static void remove_deprecated_bus_links(struct device *dev)
431{
432 sysfs_remove_link(&dev->kobj, "bus");
433}
434#else
435static inline int make_deprecated_bus_links(struct device *dev) { return 0; }
436static inline void remove_deprecated_bus_links(struct device *dev) { }
437#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439/**
440 * bus_add_device - add device to bus
441 * @dev: device being added
442 *
443 * - Add the device to its bus's list of devices.
Kay Sievers53877d02006-04-04 20:42:26 +0200444 * - Create link to device's bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 */
446int bus_add_device(struct device * dev)
447{
Greg Kroah-Hartman5901d012007-09-13 02:53:13 -0700448 struct bus_type * bus = bus_get(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 int error = 0;
450
451 if (bus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
Greg Kroah-Hartmand377e852005-06-22 16:09:05 -0700453 error = device_add_attrs(bus, dev);
Andrew Mortonf86db392006-08-14 22:43:20 -0700454 if (error)
Cornelia Huck513e7332006-09-22 11:37:08 +0200455 goto out_put;
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700456 error = sysfs_create_link(&bus->p->devices_kset->kobj,
Andrew Mortonf86db392006-08-14 22:43:20 -0700457 &dev->kobj, dev->bus_id);
458 if (error)
Cornelia Huck513e7332006-09-22 11:37:08 +0200459 goto out_id;
Andrew Mortonf86db392006-08-14 22:43:20 -0700460 error = sysfs_create_link(&dev->kobj,
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700461 &dev->bus->p->subsys.kobj, "subsystem");
Andrew Mortonf86db392006-08-14 22:43:20 -0700462 if (error)
Cornelia Huck513e7332006-09-22 11:37:08 +0200463 goto out_subsys;
Kay Sieversb9cafc72006-09-14 11:23:28 +0200464 error = make_deprecated_bus_links(dev);
Cornelia Huck513e7332006-09-22 11:37:08 +0200465 if (error)
466 goto out_deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
Cornelia Huck513e7332006-09-22 11:37:08 +0200468 return 0;
469
470out_deprecated:
471 sysfs_remove_link(&dev->kobj, "subsystem");
472out_subsys:
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700473 sysfs_remove_link(&bus->p->devices_kset->kobj, dev->bus_id);
Cornelia Huck513e7332006-09-22 11:37:08 +0200474out_id:
475 device_remove_attrs(bus, dev);
476out_put:
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700477 bus_put(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return error;
479}
480
481/**
Kay Sievers53877d02006-04-04 20:42:26 +0200482 * bus_attach_device - add device to bus
483 * @dev: device tried to attach to a driver
484 *
Alan Sternf2eaae12006-09-18 16:22:34 -0400485 * - Add device to bus's list of devices.
Kay Sievers53877d02006-04-04 20:42:26 +0200486 * - Try to attach to driver.
487 */
Cornelia Huckc6a46692007-02-05 16:15:26 -0800488void bus_attach_device(struct device * dev)
Kay Sievers53877d02006-04-04 20:42:26 +0200489{
Andrew Mortonf86db392006-08-14 22:43:20 -0700490 struct bus_type *bus = dev->bus;
491 int ret = 0;
Kay Sievers53877d02006-04-04 20:42:26 +0200492
493 if (bus) {
Alan Sternf2eaae12006-09-18 16:22:34 -0400494 dev->is_registered = 1;
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700495 if (bus->p->drivers_autoprobe)
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100496 ret = device_attach(dev);
Cornelia Huckc6a46692007-02-05 16:15:26 -0800497 WARN_ON(ret < 0);
498 if (ret >= 0)
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700499 klist_add_tail(&dev->knode_bus, &bus->p->klist_devices);
Cornelia Huckc6a46692007-02-05 16:15:26 -0800500 else
Alan Sternf2eaae12006-09-18 16:22:34 -0400501 dev->is_registered = 0;
Kay Sievers53877d02006-04-04 20:42:26 +0200502 }
503}
504
505/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 * bus_remove_device - remove device from bus
507 * @dev: device to be removed
508 *
509 * - Remove symlink from bus's directory.
510 * - Delete device from bus's list.
511 * - Detach from its driver.
512 * - Drop reference taken in bus_add_device().
513 */
514void bus_remove_device(struct device * dev)
515{
516 if (dev->bus) {
Kay Sieversb9d9c822006-06-15 15:31:56 +0200517 sysfs_remove_link(&dev->kobj, "subsystem");
Kay Sieversb9cafc72006-09-14 11:23:28 +0200518 remove_deprecated_bus_links(dev);
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700519 sysfs_remove_link(&dev->bus->p->devices_kset->kobj, dev->bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 device_remove_attrs(dev->bus, dev);
Alan Sternf70fa622006-10-05 17:03:24 -0400521 if (dev->is_registered) {
522 dev->is_registered = 0;
523 klist_del(&dev->knode_bus);
524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
526 device_release_driver(dev);
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700527 bus_put(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529}
530
531static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
532{
533 int error = 0;
534 int i;
535
536 if (bus->drv_attrs) {
537 for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
538 error = driver_create_file(drv, &bus->drv_attrs[i]);
539 if (error)
540 goto Err;
541 }
542 }
543 Done:
544 return error;
545 Err:
546 while (--i >= 0)
547 driver_remove_file(drv, &bus->drv_attrs[i]);
548 goto Done;
549}
550
551
552static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
553{
554 int i;
555
556 if (bus->drv_attrs) {
557 for (i = 0; attr_name(bus->drv_attrs[i]); i++)
558 driver_remove_file(drv, &bus->drv_attrs[i]);
559 }
560}
561
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800562#ifdef CONFIG_HOTPLUG
563/*
564 * Thanks to drivers making their tables __devinit, we can't allow manual
565 * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
566 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700567static int __must_check add_bind_files(struct device_driver *drv)
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800568{
Andrew Mortonf86db392006-08-14 22:43:20 -0700569 int ret;
570
571 ret = driver_create_file(drv, &driver_attr_unbind);
572 if (ret == 0) {
573 ret = driver_create_file(drv, &driver_attr_bind);
574 if (ret)
575 driver_remove_file(drv, &driver_attr_unbind);
576 }
577 return ret;
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800578}
579
580static void remove_bind_files(struct device_driver *drv)
581{
582 driver_remove_file(drv, &driver_attr_bind);
583 driver_remove_file(drv, &driver_attr_unbind);
584}
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100585
Kay Sievers83807702007-07-30 02:28:56 +0200586static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe);
587static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO,
588 show_drivers_autoprobe, store_drivers_autoprobe);
589
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100590static int add_probe_files(struct bus_type *bus)
591{
592 int retval;
593
Kay Sievers83807702007-07-30 02:28:56 +0200594 retval = bus_create_file(bus, &bus_attr_drivers_probe);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100595 if (retval)
596 goto out;
597
Kay Sievers83807702007-07-30 02:28:56 +0200598 retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100599 if (retval)
Kay Sievers83807702007-07-30 02:28:56 +0200600 bus_remove_file(bus, &bus_attr_drivers_probe);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100601out:
602 return retval;
603}
604
605static void remove_probe_files(struct bus_type *bus)
606{
Kay Sievers83807702007-07-30 02:28:56 +0200607 bus_remove_file(bus, &bus_attr_drivers_autoprobe);
608 bus_remove_file(bus, &bus_attr_drivers_probe);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100609}
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800610#else
Yoichi Yuasa35acfdd2006-07-15 01:30:11 +0900611static inline int add_bind_files(struct device_driver *drv) { return 0; }
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800612static inline void remove_bind_files(struct device_driver *drv) {}
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100613static inline int add_probe_files(struct bus_type *bus) { return 0; }
614static inline void remove_probe_files(struct bus_type *bus) {}
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800615#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200617static ssize_t driver_uevent_store(struct device_driver *drv,
618 const char *buf, size_t count)
619{
620 enum kobject_action action;
621
622 if (kobject_action_type(buf, count, &action) == 0)
623 kobject_uevent(&drv->kobj, action);
624 return count;
625}
626static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store);
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628/**
629 * bus_add_driver - Add a driver to the bus.
630 * @drv: driver.
631 *
632 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700633int bus_add_driver(struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Greg Kroah-Hartman5901d012007-09-13 02:53:13 -0700635 struct bus_type * bus = bus_get(drv->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 int error = 0;
637
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400638 if (!bus)
Greg Kroah-Hartman4f6e1942007-04-20 11:29:52 -0700639 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400641 pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
642 error = kobject_set_name(&drv->kobj, "%s", drv->name);
643 if (error)
644 goto out_put_bus;
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700645 drv->kobj.kset = bus->p->drivers_kset;
Greg Kroah-Hartman3514fac2007-10-16 10:11:44 -0600646 drv->kobj.ktype = &driver_ktype;
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700647 error = kobject_register(&drv->kobj);
648 if (error)
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400649 goto out_put_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700651 if (drv->bus->p->drivers_autoprobe) {
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100652 error = driver_attach(drv);
653 if (error)
654 goto out_unregister;
655 }
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700656 klist_add_tail(&drv->knode_bus, &bus->p->klist_drivers);
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400657 module_add_driver(drv->owner, drv);
658
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200659 error = driver_create_file(drv, &driver_attr_uevent);
660 if (error) {
661 printk(KERN_ERR "%s: uevent attr (%s) failed\n",
662 __FUNCTION__, drv->name);
663 }
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400664 error = driver_add_attrs(bus, drv);
665 if (error) {
666 /* How the hell do we get out of this pickle? Give up */
667 printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
668 __FUNCTION__, drv->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400670 error = add_bind_files(drv);
671 if (error) {
672 /* Ditto */
673 printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
674 __FUNCTION__, drv->name);
675 }
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return error;
Andrew Mortonf86db392006-08-14 22:43:20 -0700678out_unregister:
679 kobject_unregister(&drv->kobj);
680out_put_bus:
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700681 bus_put(bus);
Andrew Mortonf86db392006-08-14 22:43:20 -0700682 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685/**
686 * bus_remove_driver - delete driver from bus's knowledge.
687 * @drv: driver.
688 *
689 * Detach the driver from the devices it controls, and remove
690 * it from its bus's list of drivers. Finally, we drop the reference
691 * to the bus we took in bus_add_driver().
692 */
693
694void bus_remove_driver(struct device_driver * drv)
695{
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400696 if (!drv->bus)
697 return;
698
699 remove_bind_files(drv);
700 driver_remove_attrs(drv->bus, drv);
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200701 driver_remove_file(drv, &driver_attr_uevent);
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400702 klist_remove(&drv->knode_bus);
703 pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
704 driver_detach(drv);
705 module_remove_driver(drv);
706 kobject_unregister(&drv->kobj);
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700707 bus_put(drv->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710
711/* Helper for bus_rescan_devices's iter */
Andrew Mortonf86db392006-08-14 22:43:20 -0700712static int __must_check bus_rescan_devices_helper(struct device *dev,
713 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Andrew Mortonf86db392006-08-14 22:43:20 -0700715 int ret = 0;
716
Alan Sternbf74ad52005-11-17 16:54:12 -0500717 if (!dev->driver) {
718 if (dev->parent) /* Needed for USB */
719 down(&dev->parent->sem);
Andrew Mortonf86db392006-08-14 22:43:20 -0700720 ret = device_attach(dev);
Alan Sternbf74ad52005-11-17 16:54:12 -0500721 if (dev->parent)
722 up(&dev->parent->sem);
723 }
Andrew Mortonf86db392006-08-14 22:43:20 -0700724 return ret < 0 ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727/**
Greg Kroah-Hartman23d3d602005-06-22 16:09:05 -0700728 * bus_rescan_devices - rescan devices on the bus for possible drivers
729 * @bus: the bus to scan.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 *
Greg Kroah-Hartman23d3d602005-06-22 16:09:05 -0700731 * This function will look for devices on the bus with no driver
732 * attached and rescan it against existing drivers to see if it matches
733 * any by calling device_attach() for the unbound devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700735int bus_rescan_devices(struct bus_type * bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Andrew Mortonf86db392006-08-14 22:43:20 -0700737 return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
Moore, Erice935d5d2006-03-14 09:18:18 -0700740/**
741 * device_reprobe - remove driver for a device and probe for a new driver
742 * @dev: the device to reprobe
743 *
744 * This function detaches the attached driver (if any) for the given
745 * device and restarts the driver probing process. It is intended
746 * to use if probing criteria changed during a devices lifetime and
747 * driver attachment should change accordingly.
748 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700749int device_reprobe(struct device *dev)
Moore, Erice935d5d2006-03-14 09:18:18 -0700750{
751 if (dev->driver) {
752 if (dev->parent) /* Needed for USB */
753 down(&dev->parent->sem);
754 device_release_driver(dev);
755 if (dev->parent)
756 up(&dev->parent->sem);
757 }
Andrew Mortonf86db392006-08-14 22:43:20 -0700758 return bus_rescan_devices_helper(dev, NULL);
Moore, Erice935d5d2006-03-14 09:18:18 -0700759}
760EXPORT_SYMBOL_GPL(device_reprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762/**
763 * find_bus - locate bus by name.
764 * @name: name of bus.
765 *
766 * Call kset_find_obj() to iterate over list of buses to
767 * find a bus by name. Return bus if found.
768 *
769 * Note that kset_find_obj increments bus' reference count.
770 */
Adrian Bunk7e4ef082006-06-26 22:26:56 +0200771#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772struct bus_type * find_bus(char * name)
773{
Greg Kroah-Hartman59a54832007-10-29 23:22:26 -0500774 struct kobject * k = kset_find_obj(bus_kset, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return k ? to_bus(k) : NULL;
776}
Adrian Bunk7e4ef082006-06-26 22:26:56 +0200777#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779
780/**
781 * bus_add_attrs - Add default attributes for this bus.
782 * @bus: Bus that has just been registered.
783 */
784
785static int bus_add_attrs(struct bus_type * bus)
786{
787 int error = 0;
788 int i;
789
790 if (bus->bus_attrs) {
791 for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
Cornelia Huckdc0afa82007-07-09 11:39:18 -0700792 error = bus_create_file(bus,&bus->bus_attrs[i]);
793 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 goto Err;
795 }
796 }
797 Done:
798 return error;
799 Err:
800 while (--i >= 0)
801 bus_remove_file(bus,&bus->bus_attrs[i]);
802 goto Done;
803}
804
805static void bus_remove_attrs(struct bus_type * bus)
806{
807 int i;
808
809 if (bus->bus_attrs) {
810 for (i = 0; attr_name(bus->bus_attrs[i]); i++)
811 bus_remove_file(bus,&bus->bus_attrs[i]);
812 }
813}
814
James Bottomley34bb61f2005-09-06 16:56:51 -0700815static void klist_devices_get(struct klist_node *n)
816{
817 struct device *dev = container_of(n, struct device, knode_bus);
818
819 get_device(dev);
820}
821
822static void klist_devices_put(struct klist_node *n)
823{
824 struct device *dev = container_of(n, struct device, knode_bus);
825
826 put_device(dev);
827}
828
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200829static ssize_t bus_uevent_store(struct bus_type *bus,
830 const char *buf, size_t count)
831{
832 enum kobject_action action;
833
834 if (kobject_action_type(buf, count, &action) == 0)
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700835 kobject_uevent(&bus->p->subsys.kobj, action);
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200836 return count;
837}
838static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store);
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840/**
841 * bus_register - register a bus with the system.
842 * @bus: bus.
843 *
844 * Once we have that, we registered the bus with the kobject
845 * infrastructure, then register the children subsystems it has:
846 * the devices and drivers that belong to the bus.
847 */
848int bus_register(struct bus_type * bus)
849{
850 int retval;
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700851 struct bus_type_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700853 priv = kzalloc(sizeof(struct bus_type_private), GFP_KERNEL);
854 if (!priv)
855 return -ENOMEM;
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000856
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700857 priv->bus = bus;
858 bus->p = priv;
859
860 BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
861
862 retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (retval)
864 goto out;
865
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700866 priv->subsys.kobj.kset = bus_kset;
867 priv->subsys.kobj.ktype = &bus_ktype;
868 priv->drivers_autoprobe = 1;
Greg Kroah-Hartmand6b05b82007-09-12 15:06:57 -0700869
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700870 retval = kset_register(&priv->subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (retval)
872 goto out;
873
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200874 retval = bus_create_file(bus, &bus_attr_uevent);
875 if (retval)
876 goto bus_uevent_fail;
877
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700878 priv->devices_kset = kset_create_and_add("devices", NULL,
879 &priv->subsys.kobj);
880 if (!priv->devices_kset) {
Greg Kroah-Hartman3d899592007-11-01 13:31:26 -0700881 retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 goto bus_devices_fail;
Greg Kroah-Hartman3d899592007-11-01 13:31:26 -0700883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700885 priv->drivers_kset = kset_create_and_add("drivers", NULL,
886 &priv->subsys.kobj);
887 if (!priv->drivers_kset) {
Greg Kroah-Hartman6dcec252007-11-01 13:31:26 -0700888 retval = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 goto bus_drivers_fail;
Greg Kroah-Hartman6dcec252007-11-01 13:31:26 -0700890 }
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800891
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700892 klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
893 klist_init(&priv->klist_drivers, NULL, NULL);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100894
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100895 retval = add_probe_files(bus);
896 if (retval)
897 goto bus_probe_files_fail;
898
Cornelia Huck1bb68812006-09-22 11:37:04 +0200899 retval = bus_add_attrs(bus);
900 if (retval)
901 goto bus_attrs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 pr_debug("bus type '%s' registered\n", bus->name);
904 return 0;
905
Cornelia Huck1bb68812006-09-22 11:37:04 +0200906bus_attrs_fail:
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100907 remove_probe_files(bus);
908bus_probe_files_fail:
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700909 kset_unregister(bus->p->drivers_kset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910bus_drivers_fail:
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700911 kset_unregister(bus->p->devices_kset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912bus_devices_fail:
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200913 bus_remove_file(bus, &bus_attr_uevent);
914bus_uevent_fail:
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700915 kset_unregister(&bus->p->subsys);
916 kfree(bus->p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917out:
918 return retval;
919}
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921/**
922 * bus_unregister - remove a bus from the system
923 * @bus: bus.
924 *
925 * Unregister the child subsystems and the bus itself.
Greg Kroah-Hartmanfc1ede52007-09-13 02:53:13 -0700926 * Finally, we call bus_put() to release the refcount
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 */
928void bus_unregister(struct bus_type * bus)
929{
930 pr_debug("bus %s: unregistering\n", bus->name);
931 bus_remove_attrs(bus);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100932 remove_probe_files(bus);
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700933 kset_unregister(bus->p->drivers_kset);
934 kset_unregister(bus->p->devices_kset);
Kay Sievers7ac1cf42007-08-12 20:43:55 +0200935 bus_remove_file(bus, &bus_attr_uevent);
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700936 kset_unregister(&bus->p->subsys);
937 kfree(bus->p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000940int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
941{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700942 return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000943}
944EXPORT_SYMBOL_GPL(bus_register_notifier);
945
946int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
947{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700948 return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000949}
950EXPORT_SYMBOL_GPL(bus_unregister_notifier);
951
Greg Kroah-Hartman0fed80f2007-11-01 19:41:16 -0700952struct kset *bus_get_kset(struct bus_type *bus)
953{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700954 return &bus->p->subsys;
Greg Kroah-Hartman0fed80f2007-11-01 19:41:16 -0700955}
956EXPORT_SYMBOL_GPL(bus_get_kset);
957
Greg Kroah-Hartmanb2490722007-11-01 19:41:16 -0700958struct klist *bus_get_device_klist(struct bus_type *bus)
959{
Greg Kroah-Hartmanc6f7e722007-11-01 19:41:16 -0700960 return &bus->p->klist_devices;
Greg Kroah-Hartmanb2490722007-11-01 19:41:16 -0700961}
962EXPORT_SYMBOL_GPL(bus_get_device_klist);
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964int __init buses_init(void)
965{
Greg Kroah-Hartman59a54832007-10-29 23:22:26 -0500966 bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
967 if (!bus_kset)
968 return -ENOMEM;
969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970}
971
972
973EXPORT_SYMBOL_GPL(bus_for_each_dev);
Cornelia Huck0edb5862005-06-22 16:59:51 +0200974EXPORT_SYMBOL_GPL(bus_find_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975EXPORT_SYMBOL_GPL(bus_for_each_drv);
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977EXPORT_SYMBOL_GPL(bus_register);
978EXPORT_SYMBOL_GPL(bus_unregister);
979EXPORT_SYMBOL_GPL(bus_rescan_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981EXPORT_SYMBOL_GPL(bus_create_file);
982EXPORT_SYMBOL_GPL(bus_remove_file);