blob: 20b6dc8706fa6764197bd07930ea624e90dbbba1 [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)
20#define to_bus(obj) container_of(obj, struct bus_type, subsys.kset.kobj)
21
22/*
23 * sysfs bindings for drivers
24 */
25
26#define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
27#define to_driver(obj) container_of(obj, struct device_driver, kobj)
28
29
Kay 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{
66 struct device_driver * drv = to_driver(kobj);
67 complete(&drv->unloaded);
68}
69
70static struct kobj_type ktype_driver = {
71 .sysfs_ops = &driver_sysfs_ops,
72 .release = driver_release,
73};
74
75
76/*
77 * sysfs bindings for buses
78 */
79
80
81static ssize_t
82bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
83{
84 struct bus_attribute * bus_attr = to_bus_attr(attr);
85 struct bus_type * bus = to_bus(kobj);
86 ssize_t ret = 0;
87
88 if (bus_attr->show)
89 ret = bus_attr->show(bus, buf);
90 return ret;
91}
92
93static ssize_t
94bus_attr_store(struct kobject * kobj, struct attribute * attr,
95 const char * buf, size_t count)
96{
97 struct bus_attribute * bus_attr = to_bus_attr(attr);
98 struct bus_type * bus = to_bus(kobj);
99 ssize_t ret = 0;
100
101 if (bus_attr->store)
102 ret = bus_attr->store(bus, buf, count);
103 return ret;
104}
105
106static struct sysfs_ops bus_sysfs_ops = {
107 .show = bus_attr_show,
108 .store = bus_attr_store,
109};
110
111int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
112{
113 int error;
114 if (get_bus(bus)) {
115 error = sysfs_create_file(&bus->subsys.kset.kobj, &attr->attr);
116 put_bus(bus);
117 } else
118 error = -EINVAL;
119 return error;
120}
121
122void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
123{
124 if (get_bus(bus)) {
125 sysfs_remove_file(&bus->subsys.kset.kobj, &attr->attr);
126 put_bus(bus);
127 }
128}
129
130static struct kobj_type ktype_bus = {
131 .sysfs_ops = &bus_sysfs_ops,
132
133};
134
Adrian Bunk7e4ef082006-06-26 22:26:56 +0200135static decl_subsys(bus, &ktype_bus, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Russell King2139bdd2006-02-07 12:58:20 -0800138#ifdef CONFIG_HOTPLUG
Alan Stern2b08c8d2005-11-23 15:43:50 -0800139/* Manually detach a device from its associated driver. */
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700140static int driver_helper(struct device *dev, void *data)
141{
142 const char *name = data;
143
144 if (strcmp(name, dev->bus_id) == 0)
145 return 1;
146 return 0;
147}
148
149static ssize_t driver_unbind(struct device_driver *drv,
150 const char *buf, size_t count)
151{
152 struct bus_type *bus = get_bus(drv->bus);
153 struct device *dev;
154 int err = -ENODEV;
155
156 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
Alan Stern2b08c8d2005-11-23 15:43:50 -0800157 if (dev && dev->driver == drv) {
Alan Sternbf74ad52005-11-17 16:54:12 -0500158 if (dev->parent) /* Needed for USB */
159 down(&dev->parent->sem);
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700160 device_release_driver(dev);
Alan Sternbf74ad52005-11-17 16:54:12 -0500161 if (dev->parent)
162 up(&dev->parent->sem);
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700163 err = count;
164 }
Alan Stern2b08c8d2005-11-23 15:43:50 -0800165 put_device(dev);
166 put_bus(bus);
167 return err;
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700168}
169static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
170
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700171/*
172 * Manually attach a device to a driver.
173 * Note: the driver must want to bind to the device,
174 * it is not possible to override the driver's id table.
175 */
176static ssize_t driver_bind(struct device_driver *drv,
177 const char *buf, size_t count)
178{
179 struct bus_type *bus = get_bus(drv->bus);
180 struct device *dev;
181 int err = -ENODEV;
182
183 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
Alan Stern2b08c8d2005-11-23 15:43:50 -0800184 if (dev && dev->driver == NULL) {
Alan Sternbf74ad52005-11-17 16:54:12 -0500185 if (dev->parent) /* Needed for USB */
186 down(&dev->parent->sem);
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700187 down(&dev->sem);
188 err = driver_probe_device(drv, dev);
189 up(&dev->sem);
Alan Sternbf74ad52005-11-17 16:54:12 -0500190 if (dev->parent)
191 up(&dev->parent->sem);
Ryan Wilson37225402006-03-22 16:26:25 -0500192
193 if (err > 0) /* success */
194 err = count;
195 else if (err == 0) /* driver didn't accept device */
196 err = -ENODEV;
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700197 }
Alan Stern2b08c8d2005-11-23 15:43:50 -0800198 put_device(dev);
199 put_bus(bus);
200 return err;
Greg Kroah-Hartmanafdce752005-06-22 16:09:05 -0700201}
202static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
203
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100204static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
205{
206 return sprintf(buf, "%d\n", bus->drivers_autoprobe);
207}
208
209static ssize_t store_drivers_autoprobe(struct bus_type *bus,
210 const char *buf, size_t count)
211{
212 if (buf[0] == '0')
213 bus->drivers_autoprobe = 0;
214 else
215 bus->drivers_autoprobe = 1;
216 return count;
217}
218
219static ssize_t store_drivers_probe(struct bus_type *bus,
220 const char *buf, size_t count)
221{
222 struct device *dev;
223
224 dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
225 if (!dev)
226 return -ENODEV;
227 if (bus_rescan_devices_helper(dev, NULL) != 0)
228 return -EINVAL;
229 return count;
230}
Russell King2139bdd2006-02-07 12:58:20 -0800231#endif
Greg Kroah-Hartman151ef382005-06-22 16:09:05 -0700232
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800233static struct device * next_device(struct klist_iter * i)
234{
235 struct klist_node * n = klist_next(i);
236 return n ? container_of(n, struct device, knode_bus) : NULL;
237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/**
240 * bus_for_each_dev - device iterator.
241 * @bus: bus type.
242 * @start: device to start iterating from.
243 * @data: data for the callback.
244 * @fn: function to be called for each device.
245 *
246 * Iterate over @bus's list of devices, and call @fn for each,
247 * passing it @data. If @start is not NULL, we use that device to
248 * begin iterating from.
249 *
250 * We check the return of @fn each time. If it returns anything
251 * other than 0, we break out and return that value.
252 *
253 * NOTE: The device that returns a non-zero value is not retained
254 * in any way, nor is its refcount incremented. If the caller needs
255 * to retain this data, it should do, and increment the reference
256 * count in the supplied callback.
257 */
258
259int bus_for_each_dev(struct bus_type * bus, struct device * start,
260 void * data, int (*fn)(struct device *, void *))
261{
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800262 struct klist_iter i;
263 struct device * dev;
264 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800266 if (!bus)
267 return -EINVAL;
268
269 klist_iter_init_node(&bus->klist_devices, &i,
270 (start ? &start->knode_bus : NULL));
271 while ((dev = next_device(&i)) && !error)
272 error = fn(dev, data);
273 klist_iter_exit(&i);
274 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Cornelia Huck0edb5862005-06-22 16:59:51 +0200277/**
278 * bus_find_device - device iterator for locating a particular device.
279 * @bus: bus type
280 * @start: Device to begin with
281 * @data: Data to pass to match function
282 * @match: Callback function to check device
283 *
284 * This is similar to the bus_for_each_dev() function above, but it
285 * returns a reference to a device that is 'found' for later use, as
286 * determined by the @match callback.
287 *
288 * The callback should return 0 if the device doesn't match and non-zero
289 * if it does. If the callback returns non-zero, this function will
290 * return to the caller and not iterate over any more devices.
291 */
292struct device * bus_find_device(struct bus_type *bus,
293 struct device *start, void *data,
294 int (*match)(struct device *, void *))
295{
296 struct klist_iter i;
297 struct device *dev;
298
299 if (!bus)
300 return NULL;
301
302 klist_iter_init_node(&bus->klist_devices, &i,
303 (start ? &start->knode_bus : NULL));
304 while ((dev = next_device(&i)))
305 if (match(dev, data) && get_device(dev))
306 break;
307 klist_iter_exit(&i);
308 return dev;
309}
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800310
311
312static struct device_driver * next_driver(struct klist_iter * i)
313{
314 struct klist_node * n = klist_next(i);
315 return n ? container_of(n, struct device_driver, knode_bus) : NULL;
316}
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318/**
319 * bus_for_each_drv - driver iterator
320 * @bus: bus we're dealing with.
321 * @start: driver to start iterating on.
322 * @data: data to pass to the callback.
323 * @fn: function to call for each driver.
324 *
325 * This is nearly identical to the device iterator above.
326 * We iterate over each driver that belongs to @bus, and call
327 * @fn for each. If @fn returns anything but 0, we break out
328 * and return it. If @start is not NULL, we use it as the head
329 * of the list.
330 *
331 * NOTE: we don't return the driver that returns a non-zero
332 * value, nor do we leave the reference count incremented for that
333 * driver. If the caller needs to know that info, it must set it
334 * in the callback. It must also be sure to increment the refcount
335 * so it doesn't disappear before returning to the caller.
336 */
337
338int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
339 void * data, int (*fn)(struct device_driver *, void *))
340{
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800341 struct klist_iter i;
342 struct device_driver * drv;
343 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
mochel@digitalimplant.org38fdac32005-03-21 12:00:18 -0800345 if (!bus)
346 return -EINVAL;
347
348 klist_iter_init_node(&bus->klist_drivers, &i,
349 start ? &start->knode_bus : NULL);
350 while ((drv = next_driver(&i)) && !error)
351 error = fn(drv, data);
352 klist_iter_exit(&i);
353 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Andrew Morton4aca67e2007-02-13 22:39:26 -0800356static int device_add_attrs(struct bus_type *bus, struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 int error = 0;
359 int i;
360
Andrew Morton4aca67e2007-02-13 22:39:26 -0800361 if (!bus->dev_attrs)
362 return 0;
363
364 for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
365 error = device_create_file(dev,&bus->dev_attrs[i]);
366 if (error) {
367 while (--i >= 0)
368 device_remove_file(dev, &bus->dev_attrs[i]);
369 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375static void device_remove_attrs(struct bus_type * bus, struct device * dev)
376{
377 int i;
378
379 if (bus->dev_attrs) {
380 for (i = 0; attr_name(bus->dev_attrs[i]); i++)
381 device_remove_file(dev,&bus->dev_attrs[i]);
382 }
383}
384
Kay Sieversb9cafc72006-09-14 11:23:28 +0200385#ifdef CONFIG_SYSFS_DEPRECATED
386static int make_deprecated_bus_links(struct device *dev)
387{
388 return sysfs_create_link(&dev->kobj,
389 &dev->bus->subsys.kset.kobj, "bus");
390}
391
392static void remove_deprecated_bus_links(struct device *dev)
393{
394 sysfs_remove_link(&dev->kobj, "bus");
395}
396#else
397static inline int make_deprecated_bus_links(struct device *dev) { return 0; }
398static inline void remove_deprecated_bus_links(struct device *dev) { }
399#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401/**
402 * bus_add_device - add device to bus
403 * @dev: device being added
404 *
405 * - Add the device to its bus's list of devices.
Kay Sievers53877d02006-04-04 20:42:26 +0200406 * - Create link to device's bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 */
408int bus_add_device(struct device * dev)
409{
410 struct bus_type * bus = get_bus(dev->bus);
411 int error = 0;
412
413 if (bus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
Greg Kroah-Hartmand377e852005-06-22 16:09:05 -0700415 error = device_add_attrs(bus, dev);
Andrew Mortonf86db392006-08-14 22:43:20 -0700416 if (error)
Cornelia Huck513e7332006-09-22 11:37:08 +0200417 goto out_put;
Andrew Mortonf86db392006-08-14 22:43:20 -0700418 error = sysfs_create_link(&bus->devices.kobj,
419 &dev->kobj, dev->bus_id);
420 if (error)
Cornelia Huck513e7332006-09-22 11:37:08 +0200421 goto out_id;
Andrew Mortonf86db392006-08-14 22:43:20 -0700422 error = sysfs_create_link(&dev->kobj,
423 &dev->bus->subsys.kset.kobj, "subsystem");
424 if (error)
Cornelia Huck513e7332006-09-22 11:37:08 +0200425 goto out_subsys;
Kay Sieversb9cafc72006-09-14 11:23:28 +0200426 error = make_deprecated_bus_links(dev);
Cornelia Huck513e7332006-09-22 11:37:08 +0200427 if (error)
428 goto out_deprecated;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
Cornelia Huck513e7332006-09-22 11:37:08 +0200430 return 0;
431
432out_deprecated:
433 sysfs_remove_link(&dev->kobj, "subsystem");
434out_subsys:
435 sysfs_remove_link(&bus->devices.kobj, dev->bus_id);
436out_id:
437 device_remove_attrs(bus, dev);
438out_put:
439 put_bus(dev->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return error;
441}
442
443/**
Kay Sievers53877d02006-04-04 20:42:26 +0200444 * bus_attach_device - add device to bus
445 * @dev: device tried to attach to a driver
446 *
Alan Sternf2eaae12006-09-18 16:22:34 -0400447 * - Add device to bus's list of devices.
Kay Sievers53877d02006-04-04 20:42:26 +0200448 * - Try to attach to driver.
449 */
Cornelia Huckc6a46692007-02-05 16:15:26 -0800450void bus_attach_device(struct device * dev)
Kay Sievers53877d02006-04-04 20:42:26 +0200451{
Andrew Mortonf86db392006-08-14 22:43:20 -0700452 struct bus_type *bus = dev->bus;
453 int ret = 0;
Kay Sievers53877d02006-04-04 20:42:26 +0200454
455 if (bus) {
Alan Sternf2eaae12006-09-18 16:22:34 -0400456 dev->is_registered = 1;
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100457 if (bus->drivers_autoprobe)
458 ret = device_attach(dev);
Cornelia Huckc6a46692007-02-05 16:15:26 -0800459 WARN_ON(ret < 0);
460 if (ret >= 0)
Andrew Mortonf86db392006-08-14 22:43:20 -0700461 klist_add_tail(&dev->knode_bus, &bus->klist_devices);
Cornelia Huckc6a46692007-02-05 16:15:26 -0800462 else
Alan Sternf2eaae12006-09-18 16:22:34 -0400463 dev->is_registered = 0;
Kay Sievers53877d02006-04-04 20:42:26 +0200464 }
465}
466
467/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 * bus_remove_device - remove device from bus
469 * @dev: device to be removed
470 *
471 * - Remove symlink from bus's directory.
472 * - Delete device from bus's list.
473 * - Detach from its driver.
474 * - Drop reference taken in bus_add_device().
475 */
476void bus_remove_device(struct device * dev)
477{
478 if (dev->bus) {
Kay Sieversb9d9c822006-06-15 15:31:56 +0200479 sysfs_remove_link(&dev->kobj, "subsystem");
Kay Sieversb9cafc72006-09-14 11:23:28 +0200480 remove_deprecated_bus_links(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
482 device_remove_attrs(dev->bus, dev);
Alan Sternf70fa622006-10-05 17:03:24 -0400483 if (dev->is_registered) {
484 dev->is_registered = 0;
485 klist_del(&dev->knode_bus);
486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
488 device_release_driver(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 put_bus(dev->bus);
490 }
491}
492
493static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
494{
495 int error = 0;
496 int i;
497
498 if (bus->drv_attrs) {
499 for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
500 error = driver_create_file(drv, &bus->drv_attrs[i]);
501 if (error)
502 goto Err;
503 }
504 }
505 Done:
506 return error;
507 Err:
508 while (--i >= 0)
509 driver_remove_file(drv, &bus->drv_attrs[i]);
510 goto Done;
511}
512
513
514static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
515{
516 int i;
517
518 if (bus->drv_attrs) {
519 for (i = 0; attr_name(bus->drv_attrs[i]); i++)
520 driver_remove_file(drv, &bus->drv_attrs[i]);
521 }
522}
523
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800524#ifdef CONFIG_HOTPLUG
525/*
526 * Thanks to drivers making their tables __devinit, we can't allow manual
527 * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
528 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700529static int __must_check add_bind_files(struct device_driver *drv)
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800530{
Andrew Mortonf86db392006-08-14 22:43:20 -0700531 int ret;
532
533 ret = driver_create_file(drv, &driver_attr_unbind);
534 if (ret == 0) {
535 ret = driver_create_file(drv, &driver_attr_bind);
536 if (ret)
537 driver_remove_file(drv, &driver_attr_unbind);
538 }
539 return ret;
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800540}
541
542static void remove_bind_files(struct device_driver *drv)
543{
544 driver_remove_file(drv, &driver_attr_bind);
545 driver_remove_file(drv, &driver_attr_unbind);
546}
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100547
548static int add_probe_files(struct bus_type *bus)
549{
550 int retval;
551
552 bus->drivers_probe_attr.attr.name = "drivers_probe";
553 bus->drivers_probe_attr.attr.mode = S_IWUSR;
554 bus->drivers_probe_attr.attr.owner = bus->owner;
555 bus->drivers_probe_attr.store = store_drivers_probe;
556 retval = bus_create_file(bus, &bus->drivers_probe_attr);
557 if (retval)
558 goto out;
559
560 bus->drivers_autoprobe_attr.attr.name = "drivers_autoprobe";
561 bus->drivers_autoprobe_attr.attr.mode = S_IWUSR | S_IRUGO;
562 bus->drivers_autoprobe_attr.attr.owner = bus->owner;
563 bus->drivers_autoprobe_attr.show = show_drivers_autoprobe;
564 bus->drivers_autoprobe_attr.store = store_drivers_autoprobe;
565 retval = bus_create_file(bus, &bus->drivers_autoprobe_attr);
566 if (retval)
567 bus_remove_file(bus, &bus->drivers_probe_attr);
568out:
569 return retval;
570}
571
572static void remove_probe_files(struct bus_type *bus)
573{
574 bus_remove_file(bus, &bus->drivers_autoprobe_attr);
575 bus_remove_file(bus, &bus->drivers_probe_attr);
576}
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800577#else
Yoichi Yuasa35acfdd2006-07-15 01:30:11 +0900578static inline int add_bind_files(struct device_driver *drv) { return 0; }
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800579static inline void remove_bind_files(struct device_driver *drv) {}
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100580static inline int add_probe_files(struct bus_type *bus) { return 0; }
581static inline void remove_probe_files(struct bus_type *bus) {}
Greg Kroah-Hartman874c6242005-12-13 15:17:34 -0800582#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584/**
585 * bus_add_driver - Add a driver to the bus.
586 * @drv: driver.
587 *
588 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700589int bus_add_driver(struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 struct bus_type * bus = get_bus(drv->bus);
592 int error = 0;
593
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400594 if (!bus)
595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400597 pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
598 error = kobject_set_name(&drv->kobj, "%s", drv->name);
599 if (error)
600 goto out_put_bus;
601 drv->kobj.kset = &bus->drivers;
602 if ((error = kobject_register(&drv->kobj)))
603 goto out_put_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100605 if (drv->bus->drivers_autoprobe) {
606 error = driver_attach(drv);
607 if (error)
608 goto out_unregister;
609 }
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400610 klist_add_tail(&drv->knode_bus, &bus->klist_drivers);
611 module_add_driver(drv->owner, drv);
612
613 error = driver_add_attrs(bus, drv);
614 if (error) {
615 /* How the hell do we get out of this pickle? Give up */
616 printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
617 __FUNCTION__, drv->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400619 error = add_bind_files(drv);
620 if (error) {
621 /* Ditto */
622 printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
623 __FUNCTION__, drv->name);
624 }
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return error;
Andrew Mortonf86db392006-08-14 22:43:20 -0700627out_unregister:
628 kobject_unregister(&drv->kobj);
629out_put_bus:
630 put_bus(bus);
631 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634/**
635 * bus_remove_driver - delete driver from bus's knowledge.
636 * @drv: driver.
637 *
638 * Detach the driver from the devices it controls, and remove
639 * it from its bus's list of drivers. Finally, we drop the reference
640 * to the bus we took in bus_add_driver().
641 */
642
643void bus_remove_driver(struct device_driver * drv)
644{
Jeff Garzikd9fd4d3b32006-10-04 07:48:03 -0400645 if (!drv->bus)
646 return;
647
648 remove_bind_files(drv);
649 driver_remove_attrs(drv->bus, drv);
650 klist_remove(&drv->knode_bus);
651 pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
652 driver_detach(drv);
653 module_remove_driver(drv);
654 kobject_unregister(&drv->kobj);
655 put_bus(drv->bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
658
659/* Helper for bus_rescan_devices's iter */
Andrew Mortonf86db392006-08-14 22:43:20 -0700660static int __must_check bus_rescan_devices_helper(struct device *dev,
661 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Andrew Mortonf86db392006-08-14 22:43:20 -0700663 int ret = 0;
664
Alan Sternbf74ad52005-11-17 16:54:12 -0500665 if (!dev->driver) {
666 if (dev->parent) /* Needed for USB */
667 down(&dev->parent->sem);
Andrew Mortonf86db392006-08-14 22:43:20 -0700668 ret = device_attach(dev);
Alan Sternbf74ad52005-11-17 16:54:12 -0500669 if (dev->parent)
670 up(&dev->parent->sem);
671 }
Andrew Mortonf86db392006-08-14 22:43:20 -0700672 return ret < 0 ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675/**
Greg Kroah-Hartman23d3d602005-06-22 16:09:05 -0700676 * bus_rescan_devices - rescan devices on the bus for possible drivers
677 * @bus: the bus to scan.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 *
Greg Kroah-Hartman23d3d602005-06-22 16:09:05 -0700679 * This function will look for devices on the bus with no driver
680 * attached and rescan it against existing drivers to see if it matches
681 * any by calling device_attach() for the unbound devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700683int bus_rescan_devices(struct bus_type * bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Andrew Mortonf86db392006-08-14 22:43:20 -0700685 return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
Moore, Erice935d5d2006-03-14 09:18:18 -0700688/**
689 * device_reprobe - remove driver for a device and probe for a new driver
690 * @dev: the device to reprobe
691 *
692 * This function detaches the attached driver (if any) for the given
693 * device and restarts the driver probing process. It is intended
694 * to use if probing criteria changed during a devices lifetime and
695 * driver attachment should change accordingly.
696 */
Andrew Mortonf86db392006-08-14 22:43:20 -0700697int device_reprobe(struct device *dev)
Moore, Erice935d5d2006-03-14 09:18:18 -0700698{
699 if (dev->driver) {
700 if (dev->parent) /* Needed for USB */
701 down(&dev->parent->sem);
702 device_release_driver(dev);
703 if (dev->parent)
704 up(&dev->parent->sem);
705 }
Andrew Mortonf86db392006-08-14 22:43:20 -0700706 return bus_rescan_devices_helper(dev, NULL);
Moore, Erice935d5d2006-03-14 09:18:18 -0700707}
708EXPORT_SYMBOL_GPL(device_reprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Andrew Mortonf86db392006-08-14 22:43:20 -0700710struct bus_type *get_bus(struct bus_type *bus)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Andrew Mortonf86db392006-08-14 22:43:20 -0700712 return bus ? container_of(subsys_get(&bus->subsys),
713 struct bus_type, subsys) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716void put_bus(struct bus_type * bus)
717{
718 subsys_put(&bus->subsys);
719}
720
721
722/**
723 * find_bus - locate bus by name.
724 * @name: name of bus.
725 *
726 * Call kset_find_obj() to iterate over list of buses to
727 * find a bus by name. Return bus if found.
728 *
729 * Note that kset_find_obj increments bus' reference count.
730 */
Adrian Bunk7e4ef082006-06-26 22:26:56 +0200731#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732struct bus_type * find_bus(char * name)
733{
734 struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
735 return k ? to_bus(k) : NULL;
736}
Adrian Bunk7e4ef082006-06-26 22:26:56 +0200737#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739
740/**
741 * bus_add_attrs - Add default attributes for this bus.
742 * @bus: Bus that has just been registered.
743 */
744
745static int bus_add_attrs(struct bus_type * bus)
746{
747 int error = 0;
748 int i;
749
750 if (bus->bus_attrs) {
751 for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
752 if ((error = bus_create_file(bus,&bus->bus_attrs[i])))
753 goto Err;
754 }
755 }
756 Done:
757 return error;
758 Err:
759 while (--i >= 0)
760 bus_remove_file(bus,&bus->bus_attrs[i]);
761 goto Done;
762}
763
764static void bus_remove_attrs(struct bus_type * bus)
765{
766 int i;
767
768 if (bus->bus_attrs) {
769 for (i = 0; attr_name(bus->bus_attrs[i]); i++)
770 bus_remove_file(bus,&bus->bus_attrs[i]);
771 }
772}
773
James Bottomley34bb61f2005-09-06 16:56:51 -0700774static void klist_devices_get(struct klist_node *n)
775{
776 struct device *dev = container_of(n, struct device, knode_bus);
777
778 get_device(dev);
779}
780
781static void klist_devices_put(struct klist_node *n)
782{
783 struct device *dev = container_of(n, struct device, knode_bus);
784
785 put_device(dev);
786}
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788/**
789 * bus_register - register a bus with the system.
790 * @bus: bus.
791 *
792 * Once we have that, we registered the bus with the kobject
793 * infrastructure, then register the children subsystems it has:
794 * the devices and drivers that belong to the bus.
795 */
796int bus_register(struct bus_type * bus)
797{
798 int retval;
799
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000800 BLOCKING_INIT_NOTIFIER_HEAD(&bus->bus_notifier);
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 retval = kobject_set_name(&bus->subsys.kset.kobj, "%s", bus->name);
803 if (retval)
804 goto out;
805
806 subsys_set_kset(bus, bus_subsys);
807 retval = subsystem_register(&bus->subsys);
808 if (retval)
809 goto out;
810
811 kobject_set_name(&bus->devices.kobj, "devices");
812 bus->devices.subsys = &bus->subsys;
813 retval = kset_register(&bus->devices);
814 if (retval)
815 goto bus_devices_fail;
816
817 kobject_set_name(&bus->drivers.kobj, "drivers");
818 bus->drivers.subsys = &bus->subsys;
819 bus->drivers.ktype = &ktype_driver;
820 retval = kset_register(&bus->drivers);
821 if (retval)
822 goto bus_drivers_fail;
mochel@digitalimplant.org465c7a32005-03-21 11:49:14 -0800823
James Bottomley34bb61f2005-09-06 16:56:51 -0700824 klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put);
Alan Stern81107bf2006-09-18 16:24:28 -0400825 klist_init(&bus->klist_drivers, NULL, NULL);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100826
827 bus->drivers_autoprobe = 1;
828 retval = add_probe_files(bus);
829 if (retval)
830 goto bus_probe_files_fail;
831
Cornelia Huck1bb68812006-09-22 11:37:04 +0200832 retval = bus_add_attrs(bus);
833 if (retval)
834 goto bus_attrs_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 pr_debug("bus type '%s' registered\n", bus->name);
837 return 0;
838
Cornelia Huck1bb68812006-09-22 11:37:04 +0200839bus_attrs_fail:
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100840 remove_probe_files(bus);
841bus_probe_files_fail:
Cornelia Huck1bb68812006-09-22 11:37:04 +0200842 kset_unregister(&bus->drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843bus_drivers_fail:
844 kset_unregister(&bus->devices);
845bus_devices_fail:
846 subsystem_unregister(&bus->subsys);
847out:
848 return retval;
849}
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851/**
852 * bus_unregister - remove a bus from the system
853 * @bus: bus.
854 *
855 * Unregister the child subsystems and the bus itself.
856 * Finally, we call put_bus() to release the refcount
857 */
858void bus_unregister(struct bus_type * bus)
859{
860 pr_debug("bus %s: unregistering\n", bus->name);
861 bus_remove_attrs(bus);
Kay Sieversb8c5cec2007-02-16 17:33:36 +0100862 remove_probe_files(bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 kset_unregister(&bus->drivers);
864 kset_unregister(&bus->devices);
865 subsystem_unregister(&bus->subsys);
866}
867
Benjamin Herrenschmidt116af372006-10-25 13:44:59 +1000868int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
869{
870 return blocking_notifier_chain_register(&bus->bus_notifier, nb);
871}
872EXPORT_SYMBOL_GPL(bus_register_notifier);
873
874int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
875{
876 return blocking_notifier_chain_unregister(&bus->bus_notifier, nb);
877}
878EXPORT_SYMBOL_GPL(bus_unregister_notifier);
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880int __init buses_init(void)
881{
882 return subsystem_register(&bus_subsys);
883}
884
885
886EXPORT_SYMBOL_GPL(bus_for_each_dev);
Cornelia Huck0edb5862005-06-22 16:59:51 +0200887EXPORT_SYMBOL_GPL(bus_find_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888EXPORT_SYMBOL_GPL(bus_for_each_drv);
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890EXPORT_SYMBOL_GPL(bus_register);
891EXPORT_SYMBOL_GPL(bus_unregister);
892EXPORT_SYMBOL_GPL(bus_rescan_devices);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894EXPORT_SYMBOL_GPL(bus_create_file);
895EXPORT_SYMBOL_GPL(bus_remove_file);