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