blob: dca734819e50cc670439f22806eac6198e8c29eb [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
141static struct kobj_type ktype_bus = {
142 .sysfs_ops = &bus_sysfs_ops,
143
144};
145
Adrian Bunk7e4ef082006-06-26 22:26:56 +0200146static decl_subsys(bus, &ktype_bus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Russell King2139bdd2006-02-07 12:58:20 -0800149#ifdef CONFIG_HOTPLUG
Alan Stern2b08c8d2005-11-23 15:43:50 -0800150/* Manually detach a device from its associated driver. */
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700151static int driver_helper(struct device *dev, void *data)
152{
153 const char *name = data;
154
155 if (strcmp(name, dev->bus_id) == 0)
156 return 1;
157 return 0;
158}
159
160static ssize_t driver_unbind(struct device_driver *drv,
161 const char *buf, size_t count)
162{
163 struct bus_type *bus = get_bus(drv->bus);
164 struct device *dev;
165 int err = -ENODEV;
166
167 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
Alan Stern2b08c8d2005-11-23 15:43:50 -0800168 if (dev && dev->driver == drv) {
Alan Sternbf74ad52005-11-17 16:54:12 -0500169 if (dev->parent) /* Needed for USB */
170 down(&dev->parent->sem);
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700171 device_release_driver(dev);
Alan Sternbf74ad52005-11-17 16:54:12 -0500172 if (dev->parent)
173 up(&dev->parent->sem);
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700174 err = count;
175 }
Alan Stern2b08c8d2005-11-23 15:43:50 -0800176 put_device(dev);
177 put_bus(bus);
178 return err;
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700179}
180static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
181
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700182/*
183 * Manually attach a device to a driver.
184 * Note: the driver must want to bind to the device,
185 * it is not possible to override the driver's id table.
186 */
187static ssize_t driver_bind(struct device_driver *drv,
188 const char *buf, size_t count)
189{
190 struct bus_type *bus = get_bus(drv->bus);
191 struct device *dev;
192 int err = -ENODEV;
193
194 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
Alan Stern2b08c8d2005-11-23 15:43:50 -0800195 if (dev && dev->driver == NULL) {
Alan Sternbf74ad52005-11-17 16:54:12 -0500196 if (dev->parent) /* Needed for USB */
197 down(&dev->parent->sem);
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700198 down(&dev->sem);
199 err = driver_probe_device(drv, dev);
200 up(&dev->sem);
Alan Sternbf74ad52005-11-17 16:54:12 -0500201 if (dev->parent)
202 up(&dev->parent->sem);
Ryan Wilson37225402006-03-22 16:26:25 -0500203
204 if (err > 0) /* success */
205 err = count;
206 else if (err == 0) /* driver didn't accept device */
207 err = -ENODEV;
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700208 }
Alan Stern2b08c8d2005-11-23 15:43:50 -0800209 put_device(dev);
210 put_bus(bus);
211 return err;
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700212}
213static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
214
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100215static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
216{
217 return sprintf(buf, "%d\n", bus->drivers_autoprobe);
218}
219
220static ssize_t store_drivers_autoprobe(struct bus_type *bus,
221 const char *buf, size_t count)
222{
223 if (buf[0] == '0')
224 bus->drivers_autoprobe = 0;
225 else
226 bus->drivers_autoprobe = 1;
227 return count;
228}
229
230static ssize_t store_drivers_probe(struct bus_type *bus,
231 const char *buf, size_t count)
232{
233 struct device *dev;
234
235 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
236 if (!dev)
237 return -ENODEV;
238 if (bus_rescan_devices_helper(dev, NULL) != 0)
239 return -EINVAL;
240 return count;
241}
Russell King2139bdd2006-02-07 12:58:20 -0800242#endif
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700243
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800244static struct device * next_device(struct klist_iter * i)
245{
246 struct klist_node * n = klist_next(i);
247 return n ? container_of(n, struct device, knode_bus) : NULL;
248}
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/**
251 * bus_for_each_dev - device iterator.
252 * @bus: bus type.
253 * @start: device to start iterating from.
254 * @data: data for the callback.
255 * @fn: function to be called for each device.
256 *
257 * Iterate over @bus's list of devices, and call @fn for each,
258 * passing it @data. If @start is not NULL, we use that device to
259 * begin iterating from.
260 *
261 * We check the return of @fn each time. If it returns anything
262 * other than 0, we break out and return that value.
263 *
264 * NOTE: The device that returns a non-zero value is not retained
265 * in any way, nor is its refcount incremented. If the caller needs
266 * to retain this data, it should do, and increment the reference
267 * count in the supplied callback.
268 */
269
270int bus_for_each_dev(struct bus_type * bus, struct device * start,
271 void * data, int (*fn)(struct device *, void *))
272{
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800273 struct klist_iter i;
274 struct device * dev;
275 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800277 if (!bus)
278 return -EINVAL;
279
280 klist_iter_init_node(&bus->klist_devices, &i,
281 (start ? &start->knode_bus : NULL));
282 while ((dev = next_device(&i)) && !error)
283 error = fn(dev, data);
284 klist_iter_exit(&i);
285 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
Cornelia Huck0edb5862005-06-22 16:59:51 +0200288/**
289 * bus_find_device - device iterator for locating a particular device.
290 * @bus: bus type
291 * @start: Device to begin with
292 * @data: Data to pass to match function
293 * @match: Callback function to check device
294 *
295 * This is similar to the bus_for_each_dev() function above, but it
296 * returns a reference to a device that is 'found' for later use, as
297 * determined by the @match callback.
298 *
299 * The callback should return 0 if the device doesn't match and non-zero
300 * if it does. If the callback returns non-zero, this function will
301 * return to the caller and not iterate over any more devices.
302 */
303struct device * bus_find_device(struct bus_type *bus,
304 struct device *start, void *data,
305 int (*match)(struct device *, void *))
306{
307 struct klist_iter i;
308 struct device *dev;
309
310 if (!bus)
311 return NULL;
312
313 klist_iter_init_node(&bus->klist_devices, &i,
314 (start ? &start->knode_bus : NULL));
315 while ((dev = next_device(&i)))
316 if (match(dev, data) && get_device(dev))
317 break;
318 klist_iter_exit(&i);
319 return dev;
320}
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800321
322
323static struct device_driver * next_driver(struct klist_iter * i)
324{
325 struct klist_node * n = klist_next(i);
326 return n ? container_of(n, struct device_driver, knode_bus) : NULL;
327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329/**
330 * bus_for_each_drv - driver iterator
331 * @bus: bus we're dealing with.
332 * @start: driver to start iterating on.
333 * @data: data to pass to the callback.
334 * @fn: function to call for each driver.
335 *
336 * This is nearly identical to the device iterator above.
337 * We iterate over each driver that belongs to @bus, and call
338 * @fn for each. If @fn returns anything but 0, we break out
339 * and return it. If @start is not NULL, we use it as the head
340 * of the list.
341 *
342 * NOTE: we don't return the driver that returns a non-zero
343 * value, nor do we leave the reference count incremented for that
344 * driver. If the caller needs to know that info, it must set it
345 * in the callback. It must also be sure to increment the refcount
346 * so it doesn't disappear before returning to the caller.
347 */
348
349int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
350 void * data, int (*fn)(struct device_driver *, void *))
351{
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800352 struct klist_iter i;
353 struct device_driver * drv;
354 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800356 if (!bus)
357 return -EINVAL;
358
359 klist_iter_init_node(&bus->klist_drivers, &i,
360 start ? &start->knode_bus : NULL);
361 while ((drv = next_driver(&i)) && !error)
362 error = fn(drv, data);
363 klist_iter_exit(&i);
364 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Andrew Morton4aca67e2007-02-13 22:39:26 -0800367static int device_add_attrs(struct bus_type *bus, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 int error = 0;
370 int i;
371
Andrew Morton4aca67e2007-02-13 22:39:26 -0800372 if (!bus->dev_attrs)
373 return 0;
374
375 for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
376 error = device_create_file(dev,&bus->dev_attrs[i]);
377 if (error) {
378 while (--i >= 0)
379 device_remove_file(dev, &bus->dev_attrs[i]);
380 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386static void device_remove_attrs(struct bus_type * bus, struct device * dev)
387{
388 int i;
389
390 if (bus->dev_attrs) {
391 for (i = 0; attr_name(bus->dev_attrs[i]); i++)
392 device_remove_file(dev,&bus->dev_attrs[i]);
393 }
394}
395
Kay Sieversb9cafc72006-09-14 11:23:28 +0200396#ifdef CONFIG_SYSFS_DEPRECATED
397static int make_deprecated_bus_links(struct device *dev)
398{
399 return sysfs_create_link(&dev->kobj,
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700400 &dev->bus->subsys.kobj, "bus");
Kay Sieversb9cafc72006-09-14 11:23:28 +0200401}
402
403static void remove_deprecated_bus_links(struct device *dev)
404{
405 sysfs_remove_link(&dev->kobj, "bus");
406}
407#else
408static inline int make_deprecated_bus_links(struct device *dev) { return 0; }
409static inline void remove_deprecated_bus_links(struct device *dev) { }
410#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412/**
413 * bus_add_device - add device to bus
414 * @dev: device being added
415 *
416 * - Add the device to its bus's list of devices.
Kay Sievers53877d02006-04-04 20:42:26 +0200417 * - Create link to device's bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 */
419int bus_add_device(struct device * dev)
420{
421 struct bus_type * bus = get_bus(dev->bus);
422 int error = 0;
423
424 if (bus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
Greg Kroah-Hartmand377e852005-06-22 16:09:05 -0700426 error = device_add_attrs(bus, dev);
Andrew Mortonf86db392006-08-14 22:43:20 -0700427 if (error)
Cornelia Huck513e7332006-09-22 11:37:08 +0200428 goto out_put;
Andrew Mortonf86db392006-08-14 22:43:20 -0700429 error = sysfs_create_link(&bus->devices.kobj,
430 &dev->kobj, dev->bus_id);
431 if (error)
Cornelia Huck513e7332006-09-22 11:37:08 +0200432 goto out_id;
Andrew Mortonf86db392006-08-14 22:43:20 -0700433 error = sysfs_create_link(&dev->kobj,
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700434 &dev->bus->subsys.kobj, "subsystem");
Andrew Mortonf86db392006-08-14 22:43:20 -0700435 if (error)
Cornelia Huck513e7332006-09-22 11:37:08 +0200436 goto out_subsys;
Kay Sieversb9cafc72006-09-14 11:23:28 +0200437 error = make_deprecated_bus_links(dev);
Cornelia Huck513e7332006-09-22 11:37:08 +0200438 if (error)
439 goto out_deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
Cornelia Huck513e7332006-09-22 11:37:08 +0200441 return 0;
442
443out_deprecated:
444 sysfs_remove_link(&dev->kobj, "subsystem");
445out_subsys:
446 sysfs_remove_link(&bus->devices.kobj, dev->bus_id);
447out_id:
448 device_remove_attrs(bus, dev);
449out_put:
450 put_bus(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return error;
452}
453
454/**
Kay Sievers53877d02006-04-04 20:42:26 +0200455 * bus_attach_device - add device to bus
456 * @dev: device tried to attach to a driver
457 *
Alan Sternf2eaae12006-09-18 16:22:34 -0400458 * - Add device to bus's list of devices.
Kay Sievers53877d02006-04-04 20:42:26 +0200459 * - Try to attach to driver.
460 */
Cornelia Huckc6a46692007-02-05 16:15:26 -0800461void bus_attach_device(struct device * dev)
Kay Sievers53877d02006-04-04 20:42:26 +0200462{
Andrew Mortonf86db392006-08-14 22:43:20 -0700463 struct bus_type *bus = dev->bus;
464 int ret = 0;
Kay Sievers53877d02006-04-04 20:42:26 +0200465
466 if (bus) {
Alan Sternf2eaae12006-09-18 16:22:34 -0400467 dev->is_registered = 1;
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100468 if (bus->drivers_autoprobe)
469 ret = device_attach(dev);
Cornelia Huckc6a46692007-02-05 16:15:26 -0800470 WARN_ON(ret < 0);
471 if (ret >= 0)
Andrew Mortonf86db392006-08-14 22:43:20 -0700472 klist_add_tail(&dev->knode_bus, &bus->klist_devices);
Cornelia Huckc6a46692007-02-05 16:15:26 -0800473 else
Alan Sternf2eaae12006-09-18 16:22:34 -0400474 dev->is_registered = 0;
Kay Sievers53877d02006-04-04 20:42:26 +0200475 }
476}
477
478/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 * bus_remove_device - remove device from bus
480 * @dev: device to be removed
481 *
482 * - Remove symlink from bus's directory.
483 * - Delete device from bus's list.
484 * - Detach from its driver.
485 * - Drop reference taken in bus_add_device().
486 */
487void bus_remove_device(struct device * dev)
488{
489 if (dev->bus) {
Kay Sieversb9d9c822006-06-15 15:31:56 +0200490 sysfs_remove_link(&dev->kobj, "subsystem");
Kay Sieversb9cafc72006-09-14 11:23:28 +0200491 remove_deprecated_bus_links(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
493 device_remove_attrs(dev->bus, dev);
Alan Sternf70fa622006-10-05 17:03:24 -0400494 if (dev->is_registered) {
495 dev->is_registered = 0;
496 klist_del(&dev->knode_bus);
497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
499 device_release_driver(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 put_bus(dev->bus);
501 }
502}
503
504static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
505{
506 int error = 0;
507 int i;
508
509 if (bus->drv_attrs) {
510 for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
511 error = driver_create_file(drv, &bus->drv_attrs[i]);
512 if (error)
513 goto Err;
514 }
515 }
516 Done:
517 return error;
518 Err:
519 while (--i >= 0)
520 driver_remove_file(drv, &bus->drv_attrs[i]);
521 goto Done;
522}
523
524
525static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
526{
527 int i;
528
529 if (bus->drv_attrs) {
530 for (i = 0; attr_name(bus->drv_attrs[i]); i++)
531 driver_remove_file(drv, &bus->drv_attrs[i]);
532 }
533}
534
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800535#ifdef CONFIG_HOTPLUG
536/*
537 * Thanks to drivers making their tables __devinit, we can't allow manual
538 * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
539 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700540static int __must_check add_bind_files(struct device_driver *drv)
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800541{
Andrew Mortonf86db392006-08-14 22:43:20 -0700542 int ret;
543
544 ret = driver_create_file(drv, &driver_attr_unbind);
545 if (ret == 0) {
546 ret = driver_create_file(drv, &driver_attr_bind);
547 if (ret)
548 driver_remove_file(drv, &driver_attr_unbind);
549 }
550 return ret;
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800551}
552
553static void remove_bind_files(struct device_driver *drv)
554{
555 driver_remove_file(drv, &driver_attr_bind);
556 driver_remove_file(drv, &driver_attr_unbind);
557}
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100558
559static int add_probe_files(struct bus_type *bus)
560{
561 int retval;
562
563 bus->drivers_probe_attr.attr.name = "drivers_probe";
564 bus->drivers_probe_attr.attr.mode = S_IWUSR;
565 bus->drivers_probe_attr.attr.owner = bus->owner;
566 bus->drivers_probe_attr.store = store_drivers_probe;
567 retval = bus_create_file(bus, &bus->drivers_probe_attr);
568 if (retval)
569 goto out;
570
571 bus->drivers_autoprobe_attr.attr.name = "drivers_autoprobe";
572 bus->drivers_autoprobe_attr.attr.mode = S_IWUSR | S_IRUGO;
573 bus->drivers_autoprobe_attr.attr.owner = bus->owner;
574 bus->drivers_autoprobe_attr.show = show_drivers_autoprobe;
575 bus->drivers_autoprobe_attr.store = store_drivers_autoprobe;
576 retval = bus_create_file(bus, &bus->drivers_autoprobe_attr);
577 if (retval)
578 bus_remove_file(bus, &bus->drivers_probe_attr);
579out:
580 return retval;
581}
582
583static void remove_probe_files(struct bus_type *bus)
584{
585 bus_remove_file(bus, &bus->drivers_autoprobe_attr);
586 bus_remove_file(bus, &bus->drivers_probe_attr);
587}
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800588#else
Yoichi Yuasa35acfdd2006-07-15 01:30:11 +0900589static inline int add_bind_files(struct device_driver *drv) { return 0; }
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800590static inline void remove_bind_files(struct device_driver *drv) {}
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100591static inline int add_probe_files(struct bus_type *bus) { return 0; }
592static inline void remove_probe_files(struct bus_type *bus) {}
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800593#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595/**
596 * bus_add_driver - Add a driver to the bus.
597 * @drv: driver.
598 *
599 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700600int bus_add_driver(struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 struct bus_type * bus = get_bus(drv->bus);
603 int error = 0;
604
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400605 if (!bus)
Greg Kroah-Hartman4f6e1942007-04-20 11:29:52 -0700606 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400608 pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
609 error = kobject_set_name(&drv->kobj, "%s", drv->name);
610 if (error)
611 goto out_put_bus;
612 drv->kobj.kset = &bus->drivers;
613 if ((error = kobject_register(&drv->kobj)))
614 goto out_put_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100616 if (drv->bus->drivers_autoprobe) {
617 error = driver_attach(drv);
618 if (error)
619 goto out_unregister;
620 }
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400621 klist_add_tail(&drv->knode_bus, &bus->klist_drivers);
622 module_add_driver(drv->owner, drv);
623
624 error = driver_add_attrs(bus, drv);
625 if (error) {
626 /* How the hell do we get out of this pickle? Give up */
627 printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
628 __FUNCTION__, drv->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400630 error = add_bind_files(drv);
631 if (error) {
632 /* Ditto */
633 printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
634 __FUNCTION__, drv->name);
635 }
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return error;
Andrew Mortonf86db392006-08-14 22:43:20 -0700638out_unregister:
639 kobject_unregister(&drv->kobj);
640out_put_bus:
641 put_bus(bus);
642 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645/**
646 * bus_remove_driver - delete driver from bus's knowledge.
647 * @drv: driver.
648 *
649 * Detach the driver from the devices it controls, and remove
650 * it from its bus's list of drivers. Finally, we drop the reference
651 * to the bus we took in bus_add_driver().
652 */
653
654void bus_remove_driver(struct device_driver * drv)
655{
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400656 if (!drv->bus)
657 return;
658
659 remove_bind_files(drv);
660 driver_remove_attrs(drv->bus, drv);
661 klist_remove(&drv->knode_bus);
662 pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
663 driver_detach(drv);
664 module_remove_driver(drv);
665 kobject_unregister(&drv->kobj);
666 put_bus(drv->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669
670/* Helper for bus_rescan_devices's iter */
Andrew Mortonf86db392006-08-14 22:43:20 -0700671static int __must_check bus_rescan_devices_helper(struct device *dev,
672 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Andrew Mortonf86db392006-08-14 22:43:20 -0700674 int ret = 0;
675
Alan Sternbf74ad52005-11-17 16:54:12 -0500676 if (!dev->driver) {
677 if (dev->parent) /* Needed for USB */
678 down(&dev->parent->sem);
Andrew Mortonf86db392006-08-14 22:43:20 -0700679 ret = device_attach(dev);
Alan Sternbf74ad52005-11-17 16:54:12 -0500680 if (dev->parent)
681 up(&dev->parent->sem);
682 }
Andrew Mortonf86db392006-08-14 22:43:20 -0700683 return ret < 0 ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686/**
Greg Kroah-Hartman23d3d602005-06-22 16:09:05 -0700687 * bus_rescan_devices - rescan devices on the bus for possible drivers
688 * @bus: the bus to scan.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 *
Greg Kroah-Hartman23d3d602005-06-22 16:09:05 -0700690 * This function will look for devices on the bus with no driver
691 * attached and rescan it against existing drivers to see if it matches
692 * any by calling device_attach() for the unbound devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700694int bus_rescan_devices(struct bus_type * bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Andrew Mortonf86db392006-08-14 22:43:20 -0700696 return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Moore, Erice935d5d2006-03-14 09:18:18 -0700699/**
700 * device_reprobe - remove driver for a device and probe for a new driver
701 * @dev: the device to reprobe
702 *
703 * This function detaches the attached driver (if any) for the given
704 * device and restarts the driver probing process. It is intended
705 * to use if probing criteria changed during a devices lifetime and
706 * driver attachment should change accordingly.
707 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700708int device_reprobe(struct device *dev)
Moore, Erice935d5d2006-03-14 09:18:18 -0700709{
710 if (dev->driver) {
711 if (dev->parent) /* Needed for USB */
712 down(&dev->parent->sem);
713 device_release_driver(dev);
714 if (dev->parent)
715 up(&dev->parent->sem);
716 }
Andrew Mortonf86db392006-08-14 22:43:20 -0700717 return bus_rescan_devices_helper(dev, NULL);
Moore, Erice935d5d2006-03-14 09:18:18 -0700718}
719EXPORT_SYMBOL_GPL(device_reprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Andrew Mortonf86db392006-08-14 22:43:20 -0700721struct bus_type *get_bus(struct bus_type *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Andrew Mortonf86db392006-08-14 22:43:20 -0700723 return bus ? container_of(subsys_get(&bus->subsys),
724 struct bus_type, subsys) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
727void put_bus(struct bus_type * bus)
728{
729 subsys_put(&bus->subsys);
730}
731
732
733/**
734 * find_bus - locate bus by name.
735 * @name: name of bus.
736 *
737 * Call kset_find_obj() to iterate over list of buses to
738 * find a bus by name. Return bus if found.
739 *
740 * Note that kset_find_obj increments bus' reference count.
741 */
Adrian Bunk7e4ef082006-06-26 22:26:56 +0200742#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743struct bus_type * find_bus(char * name)
744{
745 struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
746 return k ? to_bus(k) : NULL;
747}
Adrian Bunk7e4ef082006-06-26 22:26:56 +0200748#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750
751/**
752 * bus_add_attrs - Add default attributes for this bus.
753 * @bus: Bus that has just been registered.
754 */
755
756static int bus_add_attrs(struct bus_type * bus)
757{
758 int error = 0;
759 int i;
760
761 if (bus->bus_attrs) {
762 for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
763 if ((error = bus_create_file(bus,&bus->bus_attrs[i])))
764 goto Err;
765 }
766 }
767 Done:
768 return error;
769 Err:
770 while (--i >= 0)
771 bus_remove_file(bus,&bus->bus_attrs[i]);
772 goto Done;
773}
774
775static void bus_remove_attrs(struct bus_type * bus)
776{
777 int i;
778
779 if (bus->bus_attrs) {
780 for (i = 0; attr_name(bus->bus_attrs[i]); i++)
781 bus_remove_file(bus,&bus->bus_attrs[i]);
782 }
783}
784
James Bottomley34bb61f2005-09-06 16:56:51 -0700785static void klist_devices_get(struct klist_node *n)
786{
787 struct device *dev = container_of(n, struct device, knode_bus);
788
789 get_device(dev);
790}
791
792static void klist_devices_put(struct klist_node *n)
793{
794 struct device *dev = container_of(n, struct device, knode_bus);
795
796 put_device(dev);
797}
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799/**
800 * bus_register - register a bus with the system.
801 * @bus: bus.
802 *
803 * Once we have that, we registered the bus with the kobject
804 * infrastructure, then register the children subsystems it has:
805 * the devices and drivers that belong to the bus.
806 */
807int bus_register(struct bus_type * bus)
808{
809 int retval;
810
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000811 BLOCKING_INIT_NOTIFIER_HEAD(&bus->bus_notifier);
812
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700813 retval = kobject_set_name(&bus->subsys.kobj, "%s", bus->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (retval)
815 goto out;
816
817 subsys_set_kset(bus, bus_subsys);
818 retval = subsystem_register(&bus->subsys);
819 if (retval)
820 goto out;
821
822 kobject_set_name(&bus->devices.kobj, "devices");
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700823 bus->devices.kobj.parent = &bus->subsys.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 retval = kset_register(&bus->devices);
825 if (retval)
826 goto bus_devices_fail;
827
828 kobject_set_name(&bus->drivers.kobj, "drivers");
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700829 bus->drivers.kobj.parent = &bus->subsys.kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 bus->drivers.ktype = &ktype_driver;
831 retval = kset_register(&bus->drivers);
832 if (retval)
833 goto bus_drivers_fail;
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800834
James Bottomley34bb61f2005-09-06 16:56:51 -0700835 klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put);
Alan Stern81107bf2006-09-18 16:24:28 -0400836 klist_init(&bus->klist_drivers, NULL, NULL);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100837
838 bus->drivers_autoprobe = 1;
839 retval = add_probe_files(bus);
840 if (retval)
841 goto bus_probe_files_fail;
842
Cornelia Huck1bb68812006-09-22 11:37:04 +0200843 retval = bus_add_attrs(bus);
844 if (retval)
845 goto bus_attrs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 pr_debug("bus type '%s' registered\n", bus->name);
848 return 0;
849
Cornelia Huck1bb68812006-09-22 11:37:04 +0200850bus_attrs_fail:
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100851 remove_probe_files(bus);
852bus_probe_files_fail:
Cornelia Huck1bb68812006-09-22 11:37:04 +0200853 kset_unregister(&bus->drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854bus_drivers_fail:
855 kset_unregister(&bus->devices);
856bus_devices_fail:
857 subsystem_unregister(&bus->subsys);
858out:
859 return retval;
860}
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862/**
863 * bus_unregister - remove a bus from the system
864 * @bus: bus.
865 *
866 * Unregister the child subsystems and the bus itself.
867 * Finally, we call put_bus() to release the refcount
868 */
869void bus_unregister(struct bus_type * bus)
870{
871 pr_debug("bus %s: unregistering\n", bus->name);
872 bus_remove_attrs(bus);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100873 remove_probe_files(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 kset_unregister(&bus->drivers);
875 kset_unregister(&bus->devices);
876 subsystem_unregister(&bus->subsys);
877}
878
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000879int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
880{
881 return blocking_notifier_chain_register(&bus->bus_notifier, nb);
882}
883EXPORT_SYMBOL_GPL(bus_register_notifier);
884
885int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
886{
887 return blocking_notifier_chain_unregister(&bus->bus_notifier, nb);
888}
889EXPORT_SYMBOL_GPL(bus_unregister_notifier);
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891int __init buses_init(void)
892{
893 return subsystem_register(&bus_subsys);
894}
895
896
897EXPORT_SYMBOL_GPL(bus_for_each_dev);
Cornelia Huck0edb5862005-06-22 16:59:51 +0200898EXPORT_SYMBOL_GPL(bus_find_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899EXPORT_SYMBOL_GPL(bus_for_each_drv);
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901EXPORT_SYMBOL_GPL(bus_register);
902EXPORT_SYMBOL_GPL(bus_unregister);
903EXPORT_SYMBOL_GPL(bus_rescan_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905EXPORT_SYMBOL_GPL(bus_create_file);
906EXPORT_SYMBOL_GPL(bus_remove_file);