driver core: bus: bus_add/probe/remove_device() cleanups
[linux-2.6-block.git] / drivers / base / bus.c
CommitLineData
989d42e8 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * bus.c - bus driver management
4 *
5 * Copyright (c) 2002-3 Patrick Mochel
6 * Copyright (c) 2002-3 Open Source Development Labs
e5dd1278
GKH
7 * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
8 * Copyright (c) 2007 Novell Inc.
1da177e4
LT
9 */
10
765230b5 11#include <linux/async.h>
5aee2bf2 12#include <linux/device/bus.h>
1da177e4
LT
13#include <linux/device.h>
14#include <linux/module.h>
15#include <linux/errno.h>
5a0e3ad6 16#include <linux/slab.h>
1da177e4
LT
17#include <linux/init.h>
18#include <linux/string.h>
ca22e56d 19#include <linux/mutex.h>
63967685 20#include <linux/sysfs.h>
1da177e4
LT
21#include "base.h"
22#include "power/power.h"
23
ca22e56d 24/* /sys/devices/system */
97ec448a 25static struct kset *system_kset;
ca22e56d 26
273afac6
GKH
27/* /sys/bus */
28static struct kset *bus_kset;
29
1da177e4 30#define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
1da177e4
LT
31
32/*
33 * sysfs bindings for drivers
34 */
35
36#define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
1da177e4 37
4f4b3743
DV
38#define DRIVER_ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store) \
39 struct driver_attribute driver_attr_##_name = \
40 __ATTR_IGNORE_LOCKDEP(_name, _mode, _show, _store)
1da177e4 41
b8c5cec2
KS
42static int __must_check bus_rescan_devices_helper(struct device *dev,
43 void *data);
44
273afac6
GKH
45/**
46 * bus_to_subsys - Turn a struct bus_type into a struct subsys_private
47 *
48 * @bus: pointer to the struct bus_type to look up
49 *
50 * The driver core internals needs to work on the subsys_private structure, not
51 * the external struct bus_type pointer. This function walks the list of
52 * registered busses in the system and finds the matching one and returns the
53 * internal struct subsys_private that relates to that bus.
54 *
55 * Note, the reference count of the return value is INCREMENTED if it is not
56 * NULL. A call to subsys_put() must be done when finished with the pointer in
57 * order for it to be properly freed.
58 */
59static struct subsys_private *bus_to_subsys(const struct bus_type *bus)
60{
61 struct subsys_private *sp = NULL;
62 struct kobject *kobj;
63
64 if (!bus)
65 return NULL;
66
67 spin_lock(&bus_kset->list_lock);
68
69 if (list_empty(&bus_kset->list))
70 goto done;
71
72 list_for_each_entry(kobj, &bus_kset->list, entry) {
73 struct kset *kset = container_of(kobj, struct kset, kobj);
74
75 sp = container_of_const(kset, struct subsys_private, subsys);
76 if (sp->bus == bus)
77 goto done;
78 }
79 sp = NULL;
80done:
81 sp = subsys_get(sp);
82 spin_unlock(&bus_kset->list_lock);
83 return sp;
84}
85
5901d014
GKH
86static struct bus_type *bus_get(struct bus_type *bus)
87{
273afac6
GKH
88 struct subsys_private *sp = bus_to_subsys(bus);
89
90 if (sp)
c6f7e72a 91 return bus;
c6f7e72a 92 return NULL;
5901d014
GKH
93}
94
273afac6 95static void bus_put(const struct bus_type *bus)
fc1ede58 96{
273afac6
GKH
97 struct subsys_private *sp = bus_to_subsys(bus);
98
99 /* two puts are required as the call to bus_to_subsys incremented it again */
100 subsys_put(sp);
101 subsys_put(sp);
fc1ede58
GKH
102}
103
4a3ad20c
GKH
104static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr,
105 char *buf)
1da177e4 106{
4a3ad20c 107 struct driver_attribute *drv_attr = to_drv_attr(attr);
e5dd1278 108 struct driver_private *drv_priv = to_driver(kobj);
4a0c20bf 109 ssize_t ret = -EIO;
1da177e4
LT
110
111 if (drv_attr->show)
e5dd1278 112 ret = drv_attr->show(drv_priv->driver, buf);
1da177e4
LT
113 return ret;
114}
115
4a3ad20c
GKH
116static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr,
117 const char *buf, size_t count)
1da177e4 118{
4a3ad20c 119 struct driver_attribute *drv_attr = to_drv_attr(attr);
e5dd1278 120 struct driver_private *drv_priv = to_driver(kobj);
4a0c20bf 121 ssize_t ret = -EIO;
1da177e4
LT
122
123 if (drv_attr->store)
e5dd1278 124 ret = drv_attr->store(drv_priv->driver, buf, count);
1da177e4
LT
125 return ret;
126}
127
52cf25d0 128static const struct sysfs_ops driver_sysfs_ops = {
1da177e4
LT
129 .show = drv_attr_show,
130 .store = drv_attr_store,
131};
132
e5dd1278 133static void driver_release(struct kobject *kobj)
1da177e4 134{
e5dd1278
GKH
135 struct driver_private *drv_priv = to_driver(kobj);
136
2b3a302a 137 pr_debug("driver: '%s': %s\n", kobject_name(kobj), __func__);
e5dd1278 138 kfree(drv_priv);
1da177e4
LT
139}
140
c83d9ab4 141static const struct kobj_type driver_ktype = {
1da177e4
LT
142 .sysfs_ops = &driver_sysfs_ops,
143 .release = driver_release,
144};
145
1da177e4
LT
146/*
147 * sysfs bindings for buses
148 */
4a3ad20c
GKH
149static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr,
150 char *buf)
1da177e4 151{
4a3ad20c 152 struct bus_attribute *bus_attr = to_bus_attr(attr);
6b6e39a6 153 struct subsys_private *subsys_priv = to_subsys_private(kobj);
1da177e4
LT
154 ssize_t ret = 0;
155
156 if (bus_attr->show)
6b6e39a6 157 ret = bus_attr->show(subsys_priv->bus, buf);
1da177e4
LT
158 return ret;
159}
160
4a3ad20c
GKH
161static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr,
162 const char *buf, size_t count)
1da177e4 163{
4a3ad20c 164 struct bus_attribute *bus_attr = to_bus_attr(attr);
6b6e39a6 165 struct subsys_private *subsys_priv = to_subsys_private(kobj);
1da177e4
LT
166 ssize_t ret = 0;
167
168 if (bus_attr->store)
6b6e39a6 169 ret = bus_attr->store(subsys_priv->bus, buf, count);
1da177e4
LT
170 return ret;
171}
172
52cf25d0 173static const struct sysfs_ops bus_sysfs_ops = {
1da177e4
LT
174 .show = bus_attr_show,
175 .store = bus_attr_store,
176};
177
0396f286 178int bus_create_file(const struct bus_type *bus, struct bus_attribute *attr)
1da177e4 179{
0396f286 180 struct subsys_private *sp = bus_to_subsys(bus);
1da177e4 181 int error;
0396f286
GKH
182
183 if (!sp)
184 return -EINVAL;
185
186 error = sysfs_create_file(&sp->subsys.kobj, &attr->attr);
187
188 subsys_put(sp);
1da177e4
LT
189 return error;
190}
4a3ad20c 191EXPORT_SYMBOL_GPL(bus_create_file);
1da177e4 192
0396f286 193void bus_remove_file(const struct bus_type *bus, struct bus_attribute *attr)
1da177e4 194{
0396f286
GKH
195 struct subsys_private *sp = bus_to_subsys(bus);
196
197 if (!sp)
198 return;
199
200 sysfs_remove_file(&sp->subsys.kobj, &attr->attr);
201 subsys_put(sp);
1da177e4 202}
4a3ad20c 203EXPORT_SYMBOL_GPL(bus_remove_file);
1da177e4 204
174be70b
BVA
205static void bus_release(struct kobject *kobj)
206{
371fd7a2 207 struct subsys_private *priv = to_subsys_private(kobj);
174be70b
BVA
208 struct bus_type *bus = priv->bus;
209
37e98d9b 210 lockdep_unregister_key(&priv->lock_key);
174be70b
BVA
211 kfree(priv);
212 bus->p = NULL;
213}
214
c83d9ab4 215static const struct kobj_type bus_ktype = {
1da177e4 216 .sysfs_ops = &bus_sysfs_ops,
174be70b 217 .release = bus_release,
80f03e34
KS
218};
219
c45a88bb 220static int bus_uevent_filter(const struct kobject *kobj)
80f03e34 221{
ee6d3dd4 222 const struct kobj_type *ktype = get_ktype(kobj);
80f03e34
KS
223
224 if (ktype == &bus_ktype)
225 return 1;
226 return 0;
227}
1da177e4 228
9cd43611 229static const struct kset_uevent_ops bus_uevent_ops = {
80f03e34 230 .filter = bus_uevent_filter,
1da177e4
LT
231};
232
2b08c8d0 233/* Manually detach a device from its associated driver. */
2581c9cc
GKH
234static ssize_t unbind_store(struct device_driver *drv, const char *buf,
235 size_t count)
151ef38f 236{
5901d014 237 struct bus_type *bus = bus_get(drv->bus);
151ef38f
GKH
238 struct device *dev;
239 int err = -ENODEV;
240
1f9ffc04 241 dev = bus_find_device_by_name(bus, NULL, buf);
2b08c8d0 242 if (dev && dev->driver == drv) {
ed88747c 243 device_driver_detach(dev);
151ef38f
GKH
244 err = count;
245 }
2b08c8d0 246 put_device(dev);
fc1ede58 247 bus_put(bus);
2b08c8d0 248 return err;
151ef38f 249}
16b0dd40 250static DRIVER_ATTR_IGNORE_LOCKDEP(unbind, 0200, NULL, unbind_store);
151ef38f 251
afdce75f
GKH
252/*
253 * Manually attach a device to a driver.
254 * Note: the driver must want to bind to the device,
255 * it is not possible to override the driver's id table.
256 */
2581c9cc
GKH
257static ssize_t bind_store(struct device_driver *drv, const char *buf,
258 size_t count)
afdce75f 259{
5901d014 260 struct bus_type *bus = bus_get(drv->bus);
afdce75f
GKH
261 struct device *dev;
262 int err = -ENODEV;
263
1f9ffc04 264 dev = bus_find_device_by_name(bus, NULL, buf);
204db60c 265 if (dev && driver_match_device(drv, dev)) {
ed88747c 266 err = device_driver_attach(drv, dev);
ef6dcbdd 267 if (!err) {
4a3ad20c 268 /* success */
37225401 269 err = count;
4a3ad20c 270 }
afdce75f 271 }
2b08c8d0 272 put_device(dev);
fc1ede58 273 bus_put(bus);
2b08c8d0 274 return err;
afdce75f 275}
16b0dd40 276static DRIVER_ATTR_IGNORE_LOCKDEP(bind, 0200, NULL, bind_store);
afdce75f 277
2e7189b6 278static ssize_t drivers_autoprobe_show(struct bus_type *bus, char *buf)
b8c5cec2 279{
a00fdb98
GKH
280 struct subsys_private *sp = bus_to_subsys(bus);
281 int ret;
282
283 if (!sp)
284 return -EINVAL;
285
286 ret = sysfs_emit(buf, "%d\n", sp->drivers_autoprobe);
287 subsys_put(sp);
288 return ret;
b8c5cec2
KS
289}
290
2e7189b6 291static ssize_t drivers_autoprobe_store(struct bus_type *bus,
b8c5cec2
KS
292 const char *buf, size_t count)
293{
a00fdb98
GKH
294 struct subsys_private *sp = bus_to_subsys(bus);
295
296 if (!sp)
297 return -EINVAL;
298
b8c5cec2 299 if (buf[0] == '0')
a00fdb98 300 sp->drivers_autoprobe = 0;
b8c5cec2 301 else
a00fdb98
GKH
302 sp->drivers_autoprobe = 1;
303
304 subsys_put(sp);
b8c5cec2
KS
305 return count;
306}
307
2e7189b6 308static ssize_t drivers_probe_store(struct bus_type *bus,
b8c5cec2
KS
309 const char *buf, size_t count)
310{
311 struct device *dev;
0372ffb3 312 int err = -EINVAL;
b8c5cec2 313
1f9ffc04 314 dev = bus_find_device_by_name(bus, NULL, buf);
b8c5cec2
KS
315 if (!dev)
316 return -ENODEV;
0372ffb3
AW
317 if (bus_rescan_devices_helper(dev, NULL) == 0)
318 err = count;
319 put_device(dev);
320 return err;
b8c5cec2 321}
151ef38f 322
4a3ad20c 323static struct device *next_device(struct klist_iter *i)
465c7a3a 324{
4a3ad20c 325 struct klist_node *n = klist_next(i);
ae1b4171
GKH
326 struct device *dev = NULL;
327 struct device_private *dev_prv;
328
329 if (n) {
330 dev_prv = to_device_private_bus(n);
331 dev = dev_prv->device;
332 }
333 return dev;
465c7a3a 334}
335
1da177e4 336/**
4a3ad20c
GKH
337 * bus_for_each_dev - device iterator.
338 * @bus: bus type.
339 * @start: device to start iterating from.
340 * @data: data for the callback.
341 * @fn: function to be called for each device.
1da177e4 342 *
4a3ad20c
GKH
343 * Iterate over @bus's list of devices, and call @fn for each,
344 * passing it @data. If @start is not NULL, we use that device to
345 * begin iterating from.
1da177e4 346 *
4a3ad20c
GKH
347 * We check the return of @fn each time. If it returns anything
348 * other than 0, we break out and return that value.
1da177e4 349 *
4a3ad20c
GKH
350 * NOTE: The device that returns a non-zero value is not retained
351 * in any way, nor is its refcount incremented. If the caller needs
0fa1b0a1 352 * to retain this data, it should do so, and increment the reference
4a3ad20c 353 * count in the supplied callback.
1da177e4 354 */
e0766ea4 355int bus_for_each_dev(const struct bus_type *bus, struct device *start,
4a3ad20c 356 void *data, int (*fn)(struct device *, void *))
1da177e4 357{
465c7a3a 358 struct klist_iter i;
4a3ad20c 359 struct device *dev;
465c7a3a 360 int error = 0;
1da177e4 361
4fa3e78b 362 if (!bus || !bus->p)
465c7a3a 363 return -EINVAL;
364
7cd9c9bb
GKH
365 klist_iter_init_node(&bus->p->klist_devices, &i,
366 (start ? &start->p->knode_bus : NULL));
93ead7c9 367 while (!error && (dev = next_device(&i)))
7cd9c9bb
GKH
368 error = fn(dev, data);
369 klist_iter_exit(&i);
465c7a3a 370 return error;
1da177e4 371}
4a3ad20c 372EXPORT_SYMBOL_GPL(bus_for_each_dev);
1da177e4 373
0edb5860
CH
374/**
375 * bus_find_device - device iterator for locating a particular device.
376 * @bus: bus type
377 * @start: Device to begin with
378 * @data: Data to pass to match function
379 * @match: Callback function to check device
380 *
381 * This is similar to the bus_for_each_dev() function above, but it
382 * returns a reference to a device that is 'found' for later use, as
383 * determined by the @match callback.
384 *
385 * The callback should return 0 if the device doesn't match and non-zero
386 * if it does. If the callback returns non-zero, this function will
387 * return to the caller and not iterate over any more devices.
388 */
e0766ea4 389struct device *bus_find_device(const struct bus_type *bus,
418e3ea1
SP
390 struct device *start, const void *data,
391 int (*match)(struct device *dev, const void *data))
0edb5860
CH
392{
393 struct klist_iter i;
394 struct device *dev;
395
4fa3e78b 396 if (!bus || !bus->p)
0edb5860
CH
397 return NULL;
398
7cd9c9bb
GKH
399 klist_iter_init_node(&bus->p->klist_devices, &i,
400 (start ? &start->p->knode_bus : NULL));
0edb5860
CH
401 while ((dev = next_device(&i)))
402 if (match(dev, data) && get_device(dev))
403 break;
404 klist_iter_exit(&i);
405 return dev;
406}
4a3ad20c 407EXPORT_SYMBOL_GPL(bus_find_device);
38fdac3c 408
4a3ad20c 409static struct device_driver *next_driver(struct klist_iter *i)
38fdac3c 410{
4a3ad20c 411 struct klist_node *n = klist_next(i);
e5dd1278
GKH
412 struct driver_private *drv_priv;
413
414 if (n) {
415 drv_priv = container_of(n, struct driver_private, knode_bus);
416 return drv_priv->driver;
417 }
418 return NULL;
38fdac3c 419}
420
1da177e4 421/**
4a3ad20c
GKH
422 * bus_for_each_drv - driver iterator
423 * @bus: bus we're dealing with.
424 * @start: driver to start iterating on.
425 * @data: data to pass to the callback.
426 * @fn: function to call for each driver.
1da177e4 427 *
4a3ad20c
GKH
428 * This is nearly identical to the device iterator above.
429 * We iterate over each driver that belongs to @bus, and call
430 * @fn for each. If @fn returns anything but 0, we break out
431 * and return it. If @start is not NULL, we use it as the head
432 * of the list.
1da177e4 433 *
4a3ad20c
GKH
434 * NOTE: we don't return the driver that returns a non-zero
435 * value, nor do we leave the reference count incremented for that
436 * driver. If the caller needs to know that info, it must set it
437 * in the callback. It must also be sure to increment the refcount
438 * so it doesn't disappear before returning to the caller.
1da177e4 439 */
e0766ea4 440int bus_for_each_drv(const struct bus_type *bus, struct device_driver *start,
4a3ad20c 441 void *data, int (*fn)(struct device_driver *, void *))
1da177e4 442{
38fdac3c 443 struct klist_iter i;
4a3ad20c 444 struct device_driver *drv;
38fdac3c 445 int error = 0;
1da177e4 446
38fdac3c 447 if (!bus)
448 return -EINVAL;
449
7cd9c9bb
GKH
450 klist_iter_init_node(&bus->p->klist_drivers, &i,
451 start ? &start->p->knode_bus : NULL);
452 while ((drv = next_driver(&i)) && !error)
453 error = fn(drv, data);
454 klist_iter_exit(&i);
38fdac3c 455 return error;
1da177e4 456}
4a3ad20c 457EXPORT_SYMBOL_GPL(bus_for_each_drv);
1da177e4 458
1da177e4 459/**
4a3ad20c
GKH
460 * bus_add_device - add device to bus
461 * @dev: device being added
1da177e4 462 *
2023c610
AS
463 * - Add device's bus attributes.
464 * - Create links to device's bus.
4a3ad20c 465 * - Add the device to its bus's list of devices.
1da177e4 466 */
4a3ad20c 467int bus_add_device(struct device *dev)
1da177e4 468{
5221b82d
GKH
469 struct subsys_private *sp = bus_to_subsys(dev->bus);
470 int error;
1da177e4 471
5221b82d
GKH
472 if (!sp) {
473 /*
474 * This is a normal operation for many devices that do not
475 * have a bus assigned to them, just say that all went
476 * well.
477 */
478 return 0;
1da177e4 479 }
5221b82d
GKH
480
481 /*
482 * Reference in sp is now incremented and will be dropped when
483 * the device is removed from the bus
484 */
485
486 pr_debug("bus: '%s': add device %s\n", sp->bus->name, dev_name(dev));
487
488 error = device_add_groups(dev, sp->bus->dev_groups);
489 if (error)
490 goto out_put;
491
492 error = sysfs_create_link(&sp->devices_kset->kobj, &dev->kobj, dev_name(dev));
493 if (error)
494 goto out_groups;
495
496 error = sysfs_create_link(&dev->kobj, &sp->subsys.kobj, "subsystem");
497 if (error)
498 goto out_subsys;
499
500 klist_add_tail(&dev->p->knode_bus, &sp->klist_devices);
513e7337
CH
501 return 0;
502
513e7337 503out_subsys:
5221b82d 504 sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev));
fa6fdb33 505out_groups:
5221b82d 506 device_remove_groups(dev, sp->bus->dev_groups);
513e7337 507out_put:
5221b82d 508 subsys_put(sp);
1da177e4
LT
509 return error;
510}
511
53877d06 512/**
2023c610
AS
513 * bus_probe_device - probe drivers for a new device
514 * @dev: device to probe
53877d06 515 *
2023c610 516 * - Automatically probe for a driver if the bus allows it.
53877d06 517 */
2023c610 518void bus_probe_device(struct device *dev)
53877d06 519{
5221b82d 520 struct subsys_private *sp = bus_to_subsys(dev->bus);
ca22e56d 521 struct subsys_interface *sif;
53877d06 522
5221b82d 523 if (!sp)
ca22e56d
KS
524 return;
525
5221b82d 526 if (sp->drivers_autoprobe)
765230b5 527 device_initial_probe(dev);
ca22e56d 528
5221b82d
GKH
529 mutex_lock(&sp->mutex);
530 list_for_each_entry(sif, &sp->interfaces, node)
ca22e56d
KS
531 if (sif->add_dev)
532 sif->add_dev(dev, sif);
5221b82d
GKH
533 mutex_unlock(&sp->mutex);
534 subsys_put(sp);
53877d06
KS
535}
536
1da177e4 537/**
4a3ad20c
GKH
538 * bus_remove_device - remove device from bus
539 * @dev: device to be removed
1da177e4 540 *
ca22e56d
KS
541 * - Remove device from all interfaces.
542 * - Remove symlink from bus' directory.
4a3ad20c
GKH
543 * - Delete device from bus's list.
544 * - Detach from its driver.
545 * - Drop reference taken in bus_add_device().
1da177e4 546 */
4a3ad20c 547void bus_remove_device(struct device *dev)
1da177e4 548{
5221b82d 549 struct subsys_private *sp = bus_to_subsys(dev->bus);
ca22e56d
KS
550 struct subsys_interface *sif;
551
5221b82d 552 if (!sp)
ca22e56d
KS
553 return;
554
5221b82d
GKH
555 mutex_lock(&sp->mutex);
556 list_for_each_entry(sif, &sp->interfaces, node)
ca22e56d
KS
557 if (sif->remove_dev)
558 sif->remove_dev(dev, sif);
5221b82d 559 mutex_unlock(&sp->mutex);
ca22e56d
KS
560
561 sysfs_remove_link(&dev->kobj, "subsystem");
5221b82d 562 sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev));
fa6fdb33 563 device_remove_groups(dev, dev->bus->dev_groups);
ca22e56d
KS
564 if (klist_node_attached(&dev->p->knode_bus))
565 klist_del(&dev->p->knode_bus);
566
567 pr_debug("bus: '%s': remove device %s\n",
568 dev->bus->name, dev_name(dev));
569 device_release_driver(dev);
5221b82d
GKH
570
571 /*
572 * Decrement the reference count twice, once for the bus_to_subsys()
573 * call in the start of this function, and the second one from the
574 * reference increment in bus_add_device()
575 */
576 subsys_put(sp);
577 subsys_put(sp);
1da177e4
LT
578}
579
f86db396 580static int __must_check add_bind_files(struct device_driver *drv)
874c6241 581{
f86db396
AM
582 int ret;
583
584 ret = driver_create_file(drv, &driver_attr_unbind);
585 if (ret == 0) {
586 ret = driver_create_file(drv, &driver_attr_bind);
587 if (ret)
588 driver_remove_file(drv, &driver_attr_unbind);
589 }
590 return ret;
874c6241
GKH
591}
592
593static void remove_bind_files(struct device_driver *drv)
594{
595 driver_remove_file(drv, &driver_attr_bind);
596 driver_remove_file(drv, &driver_attr_unbind);
597}
b8c5cec2 598
2e7189b6
GKH
599static BUS_ATTR_WO(drivers_probe);
600static BUS_ATTR_RW(drivers_autoprobe);
8380770c 601
b8c5cec2
KS
602static int add_probe_files(struct bus_type *bus)
603{
604 int retval;
605
8380770c 606 retval = bus_create_file(bus, &bus_attr_drivers_probe);
b8c5cec2
KS
607 if (retval)
608 goto out;
609
8380770c 610 retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
b8c5cec2 611 if (retval)
8380770c 612 bus_remove_file(bus, &bus_attr_drivers_probe);
b8c5cec2
KS
613out:
614 return retval;
615}
616
617static void remove_probe_files(struct bus_type *bus)
618{
8380770c
KS
619 bus_remove_file(bus, &bus_attr_drivers_autoprobe);
620 bus_remove_file(bus, &bus_attr_drivers_probe);
b8c5cec2 621}
1da177e4 622
2581c9cc
GKH
623static ssize_t uevent_store(struct device_driver *drv, const char *buf,
624 size_t count)
7ac1cf4a 625{
df44b479
PR
626 int rc;
627
628 rc = kobject_synth_uevent(&drv->p->kobj, buf, count);
629 return rc ? rc : count;
7ac1cf4a 630}
2581c9cc 631static DRIVER_ATTR_WO(uevent);
7ac1cf4a 632
1da177e4 633/**
4a3ad20c
GKH
634 * bus_add_driver - Add a driver to the bus.
635 * @drv: driver.
1da177e4 636 */
f86db396 637int bus_add_driver(struct device_driver *drv)
1da177e4 638{
e5dd1278
GKH
639 struct bus_type *bus;
640 struct driver_private *priv;
1da177e4
LT
641 int error = 0;
642
e5dd1278 643 bus = bus_get(drv->bus);
d9fd4d3b 644 if (!bus)
4f6e1945 645 return -EINVAL;
d9fd4d3b 646
7dc72b28 647 pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);
e5dd1278
GKH
648
649 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
07634464
CH
650 if (!priv) {
651 error = -ENOMEM;
652 goto out_put_bus;
653 }
e5dd1278
GKH
654 klist_init(&priv->klist_devices, NULL, NULL);
655 priv->driver = drv;
656 drv->p = priv;
c8e90d82
GKH
657 priv->kobj.kset = bus->p->drivers_kset;
658 error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
659 "%s", drv->name);
dc0afa83 660 if (error)
07634464 661 goto out_unregister;
d9fd4d3b 662
190888ac 663 klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
c6f7e72a 664 if (drv->bus->p->drivers_autoprobe) {
ef0ff683
AD
665 error = driver_attach(drv);
666 if (error)
310862e5 667 goto out_del_list;
b8c5cec2 668 }
d9fd4d3b
JG
669 module_add_driver(drv->owner, drv);
670
7ac1cf4a
KS
671 error = driver_create_file(drv, &driver_attr_uevent);
672 if (error) {
673 printk(KERN_ERR "%s: uevent attr (%s) failed\n",
2b3a302a 674 __func__, drv->name);
7ac1cf4a 675 }
e18945b1 676 error = driver_add_groups(drv, bus->drv_groups);
d9fd4d3b
JG
677 if (error) {
678 /* How the hell do we get out of this pickle? Give up */
4044b2fc 679 printk(KERN_ERR "%s: driver_add_groups(%s) failed\n",
ed0617b5 680 __func__, drv->name);
e18945b1 681 }
1a6f2a75
DT
682
683 if (!drv->suppress_bind_attrs) {
684 error = add_bind_files(drv);
685 if (error) {
686 /* Ditto */
687 printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
688 __func__, drv->name);
689 }
1da177e4 690 }
d9fd4d3b 691
5c8563d7 692 return 0;
1a6f2a75 693
310862e5
SS
694out_del_list:
695 klist_del(&priv->knode_bus);
f86db396 696out_unregister:
99b28f1b 697 kobject_put(&priv->kobj);
0f9b011d 698 /* drv->p is freed in driver_release() */
5c8563d7 699 drv->p = NULL;
f86db396 700out_put_bus:
fc1ede58 701 bus_put(bus);
f86db396 702 return error;
1da177e4
LT
703}
704
1da177e4 705/**
4a3ad20c
GKH
706 * bus_remove_driver - delete driver from bus's knowledge.
707 * @drv: driver.
1da177e4 708 *
4a3ad20c
GKH
709 * Detach the driver from the devices it controls, and remove
710 * it from its bus's list of drivers. Finally, we drop the reference
711 * to the bus we took in bus_add_driver().
1da177e4 712 */
4a3ad20c 713void bus_remove_driver(struct device_driver *drv)
1da177e4 714{
d9fd4d3b
JG
715 if (!drv->bus)
716 return;
717
1a6f2a75
DT
718 if (!drv->suppress_bind_attrs)
719 remove_bind_files(drv);
ed0617b5 720 driver_remove_groups(drv, drv->bus->drv_groups);
7ac1cf4a 721 driver_remove_file(drv, &driver_attr_uevent);
e5dd1278 722 klist_remove(&drv->p->knode_bus);
7dc72b28 723 pr_debug("bus: '%s': remove driver %s\n", drv->bus->name, drv->name);
d9fd4d3b
JG
724 driver_detach(drv);
725 module_remove_driver(drv);
c10997f6 726 kobject_put(&drv->p->kobj);
fc1ede58 727 bus_put(drv->bus);
1da177e4
LT
728}
729
1da177e4 730/* Helper for bus_rescan_devices's iter */
f86db396 731static int __must_check bus_rescan_devices_helper(struct device *dev,
4a3ad20c 732 void *data)
1da177e4 733{
f86db396
AM
734 int ret = 0;
735
bf74ad5b 736 if (!dev->driver) {
8c97a46a 737 if (dev->parent && dev->bus->need_parent_lock)
8e9394ce 738 device_lock(dev->parent);
f86db396 739 ret = device_attach(dev);
8c97a46a 740 if (dev->parent && dev->bus->need_parent_lock)
8e9394ce 741 device_unlock(dev->parent);
bf74ad5b 742 }
f86db396 743 return ret < 0 ? ret : 0;
1da177e4
LT
744}
745
1da177e4 746/**
23d3d602
GKH
747 * bus_rescan_devices - rescan devices on the bus for possible drivers
748 * @bus: the bus to scan.
1da177e4 749 *
23d3d602
GKH
750 * This function will look for devices on the bus with no driver
751 * attached and rescan it against existing drivers to see if it matches
752 * any by calling device_attach() for the unbound devices.
1da177e4 753 */
4a3ad20c 754int bus_rescan_devices(struct bus_type *bus)
1da177e4 755{
f86db396 756 return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
1da177e4 757}
4a3ad20c 758EXPORT_SYMBOL_GPL(bus_rescan_devices);
1da177e4 759
e935d5da
ME
760/**
761 * device_reprobe - remove driver for a device and probe for a new driver
762 * @dev: the device to reprobe
763 *
764 * This function detaches the attached driver (if any) for the given
765 * device and restarts the driver probing process. It is intended
766 * to use if probing criteria changed during a devices lifetime and
767 * driver attachment should change accordingly.
768 */
f86db396 769int device_reprobe(struct device *dev)
e935d5da 770{
ed88747c
AD
771 if (dev->driver)
772 device_driver_detach(dev);
f86db396 773 return bus_rescan_devices_helper(dev, NULL);
e935d5da
ME
774}
775EXPORT_SYMBOL_GPL(device_reprobe);
1da177e4 776
12478ba0
GKH
777static int bus_add_groups(struct bus_type *bus,
778 const struct attribute_group **groups)
779{
3e9b2bae 780 return sysfs_create_groups(&bus->p->subsys.kobj, groups);
12478ba0
GKH
781}
782
783static void bus_remove_groups(struct bus_type *bus,
784 const struct attribute_group **groups)
785{
3e9b2bae 786 sysfs_remove_groups(&bus->p->subsys.kobj, groups);
12478ba0
GKH
787}
788
34bb61f9
JB
789static void klist_devices_get(struct klist_node *n)
790{
ae1b4171
GKH
791 struct device_private *dev_prv = to_device_private_bus(n);
792 struct device *dev = dev_prv->device;
34bb61f9
JB
793
794 get_device(dev);
795}
796
797static void klist_devices_put(struct klist_node *n)
798{
ae1b4171
GKH
799 struct device_private *dev_prv = to_device_private_bus(n);
800 struct device *dev = dev_prv->device;
34bb61f9
JB
801
802 put_device(dev);
803}
804
7ac1cf4a
KS
805static ssize_t bus_uevent_store(struct bus_type *bus,
806 const char *buf, size_t count)
807{
a00fdb98
GKH
808 struct subsys_private *sp = bus_to_subsys(bus);
809 int ret;
df44b479 810
a00fdb98
GKH
811 if (!sp)
812 return -EINVAL;
813
814 ret = kobject_synth_uevent(&sp->subsys.kobj, buf, count);
815 subsys_put(sp);
816
817 if (ret)
818 return ret;
819 return count;
7ac1cf4a 820}
a4723041
GKH
821/*
822 * "open code" the old BUS_ATTR() macro here. We want to use BUS_ATTR_WO()
823 * here, but can not use it as earlier in the file we have
824 * DEVICE_ATTR_WO(uevent), which would cause a clash with the with the store
825 * function name.
826 */
16b0dd40 827static struct bus_attribute bus_attr_uevent = __ATTR(uevent, 0200, NULL,
a4723041 828 bus_uevent_store);
7ac1cf4a 829
1da177e4 830/**
be871b7e 831 * bus_register - register a driver-core subsystem
78d79559 832 * @bus: bus to register
1da177e4 833 *
78d79559 834 * Once we have that, we register the bus with the kobject
4a3ad20c 835 * infrastructure, then register the children subsystems it has:
ca22e56d 836 * the devices and drivers that belong to the subsystem.
1da177e4 837 */
be871b7e 838int bus_register(struct bus_type *bus)
1da177e4
LT
839{
840 int retval;
6b6e39a6 841 struct subsys_private *priv;
37e98d9b 842 struct lock_class_key *key;
c6f7e72a 843
6b6e39a6 844 priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL);
c6f7e72a
GKH
845 if (!priv)
846 return -ENOMEM;
847
848 priv->bus = bus;
849 bus->p = priv;
1da177e4 850
c6f7e72a 851 BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
116af378 852
c6f7e72a 853 retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name);
1da177e4
LT
854 if (retval)
855 goto out;
856
c6f7e72a
GKH
857 priv->subsys.kobj.kset = bus_kset;
858 priv->subsys.kobj.ktype = &bus_ktype;
859 priv->drivers_autoprobe = 1;
d6b05b84 860
c6f7e72a 861 retval = kset_register(&priv->subsys);
1da177e4
LT
862 if (retval)
863 goto out;
864
7ac1cf4a
KS
865 retval = bus_create_file(bus, &bus_attr_uevent);
866 if (retval)
867 goto bus_uevent_fail;
868
c6f7e72a
GKH
869 priv->devices_kset = kset_create_and_add("devices", NULL,
870 &priv->subsys.kobj);
871 if (!priv->devices_kset) {
3d899596 872 retval = -ENOMEM;
1da177e4 873 goto bus_devices_fail;
3d899596 874 }
1da177e4 875
c6f7e72a
GKH
876 priv->drivers_kset = kset_create_and_add("drivers", NULL,
877 &priv->subsys.kobj);
878 if (!priv->drivers_kset) {
6dcec251 879 retval = -ENOMEM;
1da177e4 880 goto bus_drivers_fail;
6dcec251 881 }
465c7a3a 882
ca22e56d 883 INIT_LIST_HEAD(&priv->interfaces);
37e98d9b
GKH
884 key = &priv->lock_key;
885 lockdep_register_key(key);
ca22e56d 886 __mutex_init(&priv->mutex, "subsys mutex", key);
c6f7e72a
GKH
887 klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
888 klist_init(&priv->klist_drivers, NULL, NULL);
b8c5cec2 889
b8c5cec2
KS
890 retval = add_probe_files(bus);
891 if (retval)
892 goto bus_probe_files_fail;
893
12478ba0
GKH
894 retval = bus_add_groups(bus, bus->bus_groups);
895 if (retval)
896 goto bus_groups_fail;
1da177e4 897
7dc72b28 898 pr_debug("bus: '%s': registered\n", bus->name);
1da177e4
LT
899 return 0;
900
12478ba0 901bus_groups_fail:
b8c5cec2
KS
902 remove_probe_files(bus);
903bus_probe_files_fail:
c6f7e72a 904 kset_unregister(bus->p->drivers_kset);
1da177e4 905bus_drivers_fail:
c6f7e72a 906 kset_unregister(bus->p->devices_kset);
1da177e4 907bus_devices_fail:
7ac1cf4a
KS
908 bus_remove_file(bus, &bus_attr_uevent);
909bus_uevent_fail:
c6f7e72a 910 kset_unregister(&bus->p->subsys);
1da177e4 911out:
600c20f3 912 kfree(bus->p);
f48f3feb 913 bus->p = NULL;
1da177e4
LT
914 return retval;
915}
be871b7e 916EXPORT_SYMBOL_GPL(bus_register);
1da177e4 917
1da177e4 918/**
4a3ad20c
GKH
919 * bus_unregister - remove a bus from the system
920 * @bus: bus.
1da177e4 921 *
4a3ad20c
GKH
922 * Unregister the child subsystems and the bus itself.
923 * Finally, we call bus_put() to release the refcount
1da177e4 924 */
4a3ad20c 925void bus_unregister(struct bus_type *bus)
1da177e4 926{
7dc72b28 927 pr_debug("bus: '%s': unregistering\n", bus->name);
ca22e56d
KS
928 if (bus->dev_root)
929 device_unregister(bus->dev_root);
12478ba0 930 bus_remove_groups(bus, bus->bus_groups);
b8c5cec2 931 remove_probe_files(bus);
c6f7e72a
GKH
932 kset_unregister(bus->p->drivers_kset);
933 kset_unregister(bus->p->devices_kset);
7ac1cf4a 934 bus_remove_file(bus, &bus_attr_uevent);
c6f7e72a 935 kset_unregister(&bus->p->subsys);
1da177e4 936}
4a3ad20c 937EXPORT_SYMBOL_GPL(bus_unregister);
1da177e4 938
116af378
BH
939int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
940{
c6f7e72a 941 return blocking_notifier_chain_register(&bus->p->bus_notifier, nb);
116af378
BH
942}
943EXPORT_SYMBOL_GPL(bus_register_notifier);
944
945int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
946{
c6f7e72a 947 return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb);
116af378
BH
948}
949EXPORT_SYMBOL_GPL(bus_unregister_notifier);
950
ed9f9181
GKH
951void bus_notify(struct device *dev, enum bus_notifier_event value)
952{
953 struct bus_type *bus = dev->bus;
954
955 if (bus)
956 blocking_notifier_call_chain(&bus->p->bus_notifier, value, dev);
957}
958
0fed80f7
GKH
959struct kset *bus_get_kset(struct bus_type *bus)
960{
c6f7e72a 961 return &bus->p->subsys;
0fed80f7
GKH
962}
963EXPORT_SYMBOL_GPL(bus_get_kset);
964
8afbb427 965static struct klist *bus_get_device_klist(struct bus_type *bus)
b249072e 966{
c6f7e72a 967 return &bus->p->klist_devices;
b249072e 968}
b249072e 969
99178b03 970/*
dca25ebd 971 * Yes, this forcibly breaks the klist abstraction temporarily. It
99178b03
GKH
972 * just wants to sort the klist, not change reference counts and
973 * take/drop locks rapidly in the process. It does all this while
974 * holding the lock for the list, so objects can't otherwise be
975 * added/removed while we're swizzling.
976 */
977static void device_insertion_sort_klist(struct device *a, struct list_head *list,
978 int (*compare)(const struct device *a,
979 const struct device *b))
980{
99178b03 981 struct klist_node *n;
ae1b4171 982 struct device_private *dev_prv;
99178b03
GKH
983 struct device *b;
984
4c62785e 985 list_for_each_entry(n, list, n_node) {
ae1b4171
GKH
986 dev_prv = to_device_private_bus(n);
987 b = dev_prv->device;
99178b03 988 if (compare(a, b) <= 0) {
ae1b4171
GKH
989 list_move_tail(&a->p->knode_bus.n_node,
990 &b->p->knode_bus.n_node);
99178b03
GKH
991 return;
992 }
993 }
ae1b4171 994 list_move_tail(&a->p->knode_bus.n_node, list);
99178b03
GKH
995}
996
997void bus_sort_breadthfirst(struct bus_type *bus,
998 int (*compare)(const struct device *a,
999 const struct device *b))
1000{
1001 LIST_HEAD(sorted_devices);
4c62785e 1002 struct klist_node *n, *tmp;
ae1b4171 1003 struct device_private *dev_prv;
99178b03
GKH
1004 struct device *dev;
1005 struct klist *device_klist;
1006
1007 device_klist = bus_get_device_klist(bus);
1008
1009 spin_lock(&device_klist->k_lock);
4c62785e 1010 list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) {
ae1b4171
GKH
1011 dev_prv = to_device_private_bus(n);
1012 dev = dev_prv->device;
99178b03
GKH
1013 device_insertion_sort_klist(dev, &sorted_devices, compare);
1014 }
1015 list_splice(&sorted_devices, &device_klist->k_list);
1016 spin_unlock(&device_klist->k_lock);
1017}
1018EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
1019
b0a8a59a
GKH
1020struct subsys_dev_iter {
1021 struct klist_iter ki;
1022 const struct device_type *type;
1023};
1024
ca22e56d
KS
1025/**
1026 * subsys_dev_iter_init - initialize subsys device iterator
1027 * @iter: subsys iterator to initialize
1028 * @subsys: the subsys we wanna iterate over
1029 * @start: the device to start iterating from, if any
1030 * @type: device_type of the devices to iterate over, NULL for all
1031 *
1032 * Initialize subsys iterator @iter such that it iterates over devices
1033 * of @subsys. If @start is set, the list iteration will start there,
1034 * otherwise if it is NULL, the iteration starts at the beginning of
1035 * the list.
1036 */
2e45fc55
GKH
1037static void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys,
1038 struct device *start, const struct device_type *type)
ca22e56d
KS
1039{
1040 struct klist_node *start_knode = NULL;
1041
1042 if (start)
1043 start_knode = &start->p->knode_bus;
7cd9c9bb
GKH
1044 klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode);
1045 iter->type = type;
ca22e56d 1046}
ca22e56d
KS
1047
1048/**
1049 * subsys_dev_iter_next - iterate to the next device
1050 * @iter: subsys iterator to proceed
1051 *
1052 * Proceed @iter to the next device and return it. Returns NULL if
1053 * iteration is complete.
1054 *
1055 * The returned device is referenced and won't be released till
1056 * iterator is proceed to the next device or exited. The caller is
1057 * free to do whatever it wants to do with the device including
1058 * calling back into subsys code.
1059 */
38cdadef 1060static struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
ca22e56d
KS
1061{
1062 struct klist_node *knode;
1063 struct device *dev;
1064
1065 for (;;) {
1066 knode = klist_next(&iter->ki);
1067 if (!knode)
1068 return NULL;
371fd7a2 1069 dev = to_device_private_bus(knode)->device;
ca22e56d
KS
1070 if (!iter->type || iter->type == dev->type)
1071 return dev;
1072 }
1073}
ca22e56d
KS
1074
1075/**
1076 * subsys_dev_iter_exit - finish iteration
1077 * @iter: subsys iterator to finish
1078 *
1079 * Finish an iteration. Always call this function after iteration is
1080 * complete whether the iteration ran till the end or not.
1081 */
af6d0743 1082static void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
ca22e56d
KS
1083{
1084 klist_iter_exit(&iter->ki);
1085}
ca22e56d
KS
1086
1087int subsys_interface_register(struct subsys_interface *sif)
1088{
1089 struct bus_type *subsys;
1090 struct subsys_dev_iter iter;
1091 struct device *dev;
1092
1093 if (!sif || !sif->subsys)
1094 return -ENODEV;
1095
1096 subsys = bus_get(sif->subsys);
1097 if (!subsys)
1098 return -EINVAL;
1099
1100 mutex_lock(&subsys->p->mutex);
1101 list_add_tail(&sif->node, &subsys->p->interfaces);
1102 if (sif->add_dev) {
1103 subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1104 while ((dev = subsys_dev_iter_next(&iter)))
1105 sif->add_dev(dev, sif);
1106 subsys_dev_iter_exit(&iter);
1107 }
1108 mutex_unlock(&subsys->p->mutex);
1109
1110 return 0;
1111}
1112EXPORT_SYMBOL_GPL(subsys_interface_register);
1113
1114void subsys_interface_unregister(struct subsys_interface *sif)
1115{
2b31594a 1116 struct bus_type *subsys;
ca22e56d
KS
1117 struct subsys_dev_iter iter;
1118 struct device *dev;
1119
2b31594a 1120 if (!sif || !sif->subsys)
ca22e56d
KS
1121 return;
1122
2b31594a
JC
1123 subsys = sif->subsys;
1124
ca22e56d
KS
1125 mutex_lock(&subsys->p->mutex);
1126 list_del_init(&sif->node);
1127 if (sif->remove_dev) {
1128 subsys_dev_iter_init(&iter, subsys, NULL, NULL);
1129 while ((dev = subsys_dev_iter_next(&iter)))
1130 sif->remove_dev(dev, sif);
1131 subsys_dev_iter_exit(&iter);
1132 }
1133 mutex_unlock(&subsys->p->mutex);
1134
1135 bus_put(subsys);
1136}
1137EXPORT_SYMBOL_GPL(subsys_interface_unregister);
1138
1139static void system_root_device_release(struct device *dev)
1140{
1141 kfree(dev);
1142}
d73ce004
TH
1143
1144static int subsys_register(struct bus_type *subsys,
1145 const struct attribute_group **groups,
1146 struct kobject *parent_of_root)
ca22e56d
KS
1147{
1148 struct device *dev;
1149 int err;
1150
1151 err = bus_register(subsys);
1152 if (err < 0)
1153 return err;
1154
1155 dev = kzalloc(sizeof(struct device), GFP_KERNEL);
1156 if (!dev) {
1157 err = -ENOMEM;
1158 goto err_dev;
1159 }
1160
1161 err = dev_set_name(dev, "%s", subsys->name);
1162 if (err < 0)
1163 goto err_name;
1164
d73ce004 1165 dev->kobj.parent = parent_of_root;
ca22e56d
KS
1166 dev->groups = groups;
1167 dev->release = system_root_device_release;
1168
1169 err = device_register(dev);
1170 if (err < 0)
1171 goto err_dev_reg;
1172
1173 subsys->dev_root = dev;
1174 return 0;
1175
1176err_dev_reg:
1177 put_device(dev);
1178 dev = NULL;
1179err_name:
1180 kfree(dev);
1181err_dev:
1182 bus_unregister(subsys);
1183 return err;
1184}
d73ce004
TH
1185
1186/**
1187 * subsys_system_register - register a subsystem at /sys/devices/system/
1188 * @subsys: system subsystem
1189 * @groups: default attributes for the root device
1190 *
1191 * All 'system' subsystems have a /sys/devices/system/<name> root device
1192 * with the name of the subsystem. The root device can carry subsystem-
1193 * wide attributes. All registered devices are below this single root
1194 * device and are named after the subsystem with a simple enumeration
e227867f 1195 * number appended. The registered devices are not explicitly named;
d73ce004
TH
1196 * only 'id' in the device needs to be set.
1197 *
1198 * Do not use this interface for anything new, it exists for compatibility
1199 * with bad ideas only. New subsystems should use plain subsystems; and
1200 * add the subsystem-wide attributes should be added to the subsystem
1201 * directory itself and not some create fake root-device placed in
1202 * /sys/devices/system/<name>.
1203 */
1204int subsys_system_register(struct bus_type *subsys,
1205 const struct attribute_group **groups)
1206{
1207 return subsys_register(subsys, groups, &system_kset->kobj);
1208}
ca22e56d
KS
1209EXPORT_SYMBOL_GPL(subsys_system_register);
1210
d73ce004
TH
1211/**
1212 * subsys_virtual_register - register a subsystem at /sys/devices/virtual/
1213 * @subsys: virtual subsystem
1214 * @groups: default attributes for the root device
1215 *
1216 * All 'virtual' subsystems have a /sys/devices/system/<name> root device
1217 * with the name of the subystem. The root device can carry subsystem-wide
1218 * attributes. All registered devices are below this single root device.
1219 * There's no restriction on device naming. This is for kernel software
1220 * constructs which need sysfs interface.
1221 */
1222int subsys_virtual_register(struct bus_type *subsys,
1223 const struct attribute_group **groups)
1224{
1225 struct kobject *virtual_dir;
1226
1227 virtual_dir = virtual_device_parent(NULL);
1228 if (!virtual_dir)
1229 return -ENOMEM;
1230
1231 return subsys_register(subsys, groups, virtual_dir);
1232}
1c04fc35 1233EXPORT_SYMBOL_GPL(subsys_virtual_register);
d73ce004 1234
1da177e4
LT
1235int __init buses_init(void)
1236{
59a54833
GKH
1237 bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
1238 if (!bus_kset)
1239 return -ENOMEM;
ca22e56d
KS
1240
1241 system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
1242 if (!system_kset)
1243 return -ENOMEM;
1244
59a54833 1245 return 0;
1da177e4 1246}