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
driver_override_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)507 static ssize_t driver_override_store(struct device *dev,
508 struct device_attribute *attr,
509 const char *buf, size_t count)
510 {
511 int ret;
512
513 ret = __device_set_driver_override(dev, buf, count);
514 if (ret)
515 return ret;
516
517 return count;
518 }
519
driver_override_show(struct device * dev,struct device_attribute * attr,char * buf)520 static ssize_t driver_override_show(struct device *dev,
521 struct device_attribute *attr, char *buf)
522 {
523 guard(spinlock)(&dev->driver_override.lock);
524 return sysfs_emit(buf, "%s\n", dev->driver_override.name);
525 }
526 static DEVICE_ATTR_RW(driver_override);
527
528 static struct attribute *driver_override_dev_attrs[] = {
529 &dev_attr_driver_override.attr,
530 NULL,
531 };
532
533 static const struct attribute_group driver_override_dev_group = {
534 .attrs = driver_override_dev_attrs,
535 };
536
537 /**
538 * bus_add_device - add device to bus
539 * @dev: device being added
540 *
541 * - Add device's bus attributes.
542 * - Create links to device's bus.
543 * - Add the device to its bus's list of devices.
544 */
bus_add_device(struct device * dev)545 int bus_add_device(struct device *dev)
546 {
547 struct subsys_private *sp = bus_to_subsys(dev->bus);
548 int error;
549
550 if (!sp) {
551 /*
552 * This is a normal operation for many devices that do not
553 * have a bus assigned to them, just say that all went
554 * well.
555 */
556 return 0;
557 }
558
559 /*
560 * Reference in sp is now incremented and will be dropped when
561 * the device is removed from the bus
562 */
563
564 pr_debug("bus: '%s': add device %s\n", sp->bus->name, dev_name(dev));
565
566 error = device_add_groups(dev, sp->bus->dev_groups);
567 if (error)
568 goto out_put;
569
570 if (dev->bus->driver_override) {
571 error = device_add_group(dev, &driver_override_dev_group);
572 if (error)
573 goto out_groups;
574 }
575
576 error = sysfs_create_link(&sp->devices_kset->kobj, &dev->kobj, dev_name(dev));
577 if (error)
578 goto out_override;
579
580 error = sysfs_create_link(&dev->kobj, &sp->subsys.kobj, "subsystem");
581 if (error)
582 goto out_subsys;
583
584 klist_add_tail(&dev->p->knode_bus, &sp->klist_devices);
585 return 0;
586
587 out_subsys:
588 sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev));
589 out_override:
590 if (dev->bus->driver_override)
591 device_remove_group(dev, &driver_override_dev_group);
592 out_groups:
593 device_remove_groups(dev, sp->bus->dev_groups);
594 out_put:
595 subsys_put(sp);
596 return error;
597 }
598
599 /**
600 * bus_probe_device - probe drivers for a new device
601 * @dev: device to probe
602 *
603 * - Automatically probe for a driver if the bus allows it.
604 */
bus_probe_device(struct device * dev)605 void bus_probe_device(struct device *dev)
606 {
607 struct subsys_private *sp = bus_to_subsys(dev->bus);
608 struct subsys_interface *sif;
609
610 if (!sp)
611 return;
612
613 device_initial_probe(dev);
614
615 mutex_lock(&sp->mutex);
616 list_for_each_entry(sif, &sp->interfaces, node)
617 if (sif->add_dev)
618 sif->add_dev(dev, sif);
619 mutex_unlock(&sp->mutex);
620 subsys_put(sp);
621 }
622
623 /**
624 * bus_remove_device - remove device from bus
625 * @dev: device to be removed
626 *
627 * - Remove device from all interfaces.
628 * - Remove symlink from bus' directory.
629 * - Delete device from bus's list.
630 * - Detach from its driver.
631 * - Drop reference taken in bus_add_device().
632 */
bus_remove_device(struct device * dev)633 void bus_remove_device(struct device *dev)
634 {
635 struct subsys_private *sp = bus_to_subsys(dev->bus);
636 struct subsys_interface *sif;
637
638 if (!sp)
639 return;
640
641 mutex_lock(&sp->mutex);
642 list_for_each_entry(sif, &sp->interfaces, node)
643 if (sif->remove_dev)
644 sif->remove_dev(dev, sif);
645 mutex_unlock(&sp->mutex);
646
647 sysfs_remove_link(&dev->kobj, "subsystem");
648 sysfs_remove_link(&sp->devices_kset->kobj, dev_name(dev));
649 if (dev->bus->driver_override)
650 device_remove_group(dev, &driver_override_dev_group);
651 device_remove_groups(dev, dev->bus->dev_groups);
652 if (klist_node_attached(&dev->p->knode_bus))
653 klist_del(&dev->p->knode_bus);
654
655 pr_debug("bus: '%s': remove device %s\n",
656 dev->bus->name, dev_name(dev));
657 device_release_driver(dev);
658
659 /*
660 * Decrement the reference count twice, once for the bus_to_subsys()
661 * call in the start of this function, and the second one from the
662 * reference increment in bus_add_device()
663 */
664 subsys_put(sp);
665 subsys_put(sp);
666 }
667
add_bind_files(struct device_driver * drv)668 static int __must_check add_bind_files(struct device_driver *drv)
669 {
670 int ret;
671
672 ret = driver_create_file(drv, &driver_attr_unbind);
673 if (ret == 0) {
674 ret = driver_create_file(drv, &driver_attr_bind);
675 if (ret)
676 driver_remove_file(drv, &driver_attr_unbind);
677 }
678 return ret;
679 }
680
remove_bind_files(struct device_driver * drv)681 static void remove_bind_files(struct device_driver *drv)
682 {
683 driver_remove_file(drv, &driver_attr_bind);
684 driver_remove_file(drv, &driver_attr_unbind);
685 }
686
687 static BUS_ATTR_WO(drivers_probe);
688 static BUS_ATTR_RW(drivers_autoprobe);
689
add_probe_files(const struct bus_type * bus)690 static int add_probe_files(const struct bus_type *bus)
691 {
692 int retval;
693
694 retval = bus_create_file(bus, &bus_attr_drivers_probe);
695 if (retval)
696 goto out;
697
698 retval = bus_create_file(bus, &bus_attr_drivers_autoprobe);
699 if (retval)
700 bus_remove_file(bus, &bus_attr_drivers_probe);
701 out:
702 return retval;
703 }
704
remove_probe_files(const struct bus_type * bus)705 static void remove_probe_files(const struct bus_type *bus)
706 {
707 bus_remove_file(bus, &bus_attr_drivers_autoprobe);
708 bus_remove_file(bus, &bus_attr_drivers_probe);
709 }
710
uevent_store(struct device_driver * drv,const char * buf,size_t count)711 static ssize_t uevent_store(struct device_driver *drv, const char *buf,
712 size_t count)
713 {
714 int rc;
715
716 rc = kobject_synth_uevent(&drv->p->kobj, buf, count);
717 return rc ? rc : count;
718 }
719 static DRIVER_ATTR_WO(uevent);
720
721 /**
722 * bus_add_driver - Add a driver to the bus.
723 * @drv: driver.
724 */
bus_add_driver(struct device_driver * drv)725 int bus_add_driver(struct device_driver *drv)
726 {
727 struct subsys_private *sp = bus_to_subsys(drv->bus);
728 struct driver_private *priv;
729 int error = 0;
730
731 if (!sp)
732 return -EINVAL;
733
734 /*
735 * Reference in sp is now incremented and will be dropped when
736 * the driver is removed from the bus
737 */
738 pr_debug("bus: '%s': add driver %s\n", sp->bus->name, drv->name);
739
740 priv = kzalloc_obj(*priv);
741 if (!priv) {
742 error = -ENOMEM;
743 goto out_put_bus;
744 }
745 klist_init(&priv->klist_devices, NULL, NULL);
746 priv->driver = drv;
747 drv->p = priv;
748 priv->kobj.kset = sp->drivers_kset;
749 error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
750 "%s", drv->name);
751 if (error)
752 goto out_unregister;
753
754 klist_add_tail(&priv->knode_bus, &sp->klist_drivers);
755 if (sp->drivers_autoprobe) {
756 error = driver_attach(drv);
757 if (error)
758 goto out_del_list;
759 }
760 error = module_add_driver(drv->owner, drv);
761 if (error) {
762 printk(KERN_ERR "%s: failed to create module links for %s\n",
763 __func__, drv->name);
764 goto out_detach;
765 }
766
767 error = driver_create_file(drv, &driver_attr_uevent);
768 if (error) {
769 printk(KERN_ERR "%s: uevent attr (%s) failed\n",
770 __func__, drv->name);
771 }
772 error = driver_add_groups(drv, sp->bus->drv_groups);
773 if (error) {
774 /* How the hell do we get out of this pickle? Give up */
775 printk(KERN_ERR "%s: driver_add_groups(%s) failed\n",
776 __func__, drv->name);
777 }
778
779 if (!drv->suppress_bind_attrs) {
780 error = add_bind_files(drv);
781 if (error) {
782 /* Ditto */
783 printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
784 __func__, drv->name);
785 }
786 }
787
788 return 0;
789
790 out_detach:
791 driver_detach(drv);
792 out_del_list:
793 klist_del(&priv->knode_bus);
794 out_unregister:
795 kobject_put(&priv->kobj);
796 /* drv->p is freed in driver_release() */
797 drv->p = NULL;
798 out_put_bus:
799 subsys_put(sp);
800 return error;
801 }
802
803 /**
804 * bus_remove_driver - delete driver from bus's knowledge.
805 * @drv: driver.
806 *
807 * Detach the driver from the devices it controls, and remove
808 * it from its bus's list of drivers. Finally, we drop the reference
809 * to the bus we took in bus_add_driver().
810 */
bus_remove_driver(struct device_driver * drv)811 void bus_remove_driver(struct device_driver *drv)
812 {
813 struct subsys_private *sp = bus_to_subsys(drv->bus);
814
815 if (!sp)
816 return;
817
818 pr_debug("bus: '%s': remove driver %s\n", sp->bus->name, drv->name);
819
820 if (!drv->suppress_bind_attrs)
821 remove_bind_files(drv);
822 driver_remove_groups(drv, sp->bus->drv_groups);
823 driver_remove_file(drv, &driver_attr_uevent);
824 klist_remove(&drv->p->knode_bus);
825 driver_detach(drv);
826 module_remove_driver(drv);
827 kobject_put(&drv->p->kobj);
828
829 /*
830 * Decrement the reference count twice, once for the bus_to_subsys()
831 * call in the start of this function, and the second one from the
832 * reference increment in bus_add_driver()
833 */
834 subsys_put(sp);
835 subsys_put(sp);
836 }
837
838 /* Helper for bus_rescan_devices's iter */
bus_rescan_devices_helper(struct device * dev,void * data)839 static int __must_check bus_rescan_devices_helper(struct device *dev,
840 void *data)
841 {
842 int ret = 0;
843
844 if (!dev->driver) {
845 if (dev->parent && dev->bus->need_parent_lock)
846 device_lock(dev->parent);
847 ret = device_attach(dev);
848 if (dev->parent && dev->bus->need_parent_lock)
849 device_unlock(dev->parent);
850 }
851 return ret < 0 ? ret : 0;
852 }
853
854 /**
855 * bus_rescan_devices - rescan devices on the bus for possible drivers
856 * @bus: the bus to scan.
857 *
858 * This function will look for devices on the bus with no driver
859 * attached and rescan it against existing drivers to see if it matches
860 * any by calling device_attach() for the unbound devices.
861 */
bus_rescan_devices(const struct bus_type * bus)862 int bus_rescan_devices(const struct bus_type *bus)
863 {
864 return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
865 }
866 EXPORT_SYMBOL_GPL(bus_rescan_devices);
867
868 /**
869 * device_reprobe - remove driver for a device and probe for a new driver
870 * @dev: the device to reprobe
871 *
872 * This function detaches the attached driver (if any) for the given
873 * device and restarts the driver probing process. It is intended
874 * to use if probing criteria changed during a devices lifetime and
875 * driver attachment should change accordingly.
876 */
device_reprobe(struct device * dev)877 int device_reprobe(struct device *dev)
878 {
879 if (dev->driver)
880 device_driver_detach(dev);
881 return bus_rescan_devices_helper(dev, NULL);
882 }
883 EXPORT_SYMBOL_GPL(device_reprobe);
884
klist_devices_get(struct klist_node * n)885 static void klist_devices_get(struct klist_node *n)
886 {
887 struct device_private *dev_prv = to_device_private_bus(n);
888 struct device *dev = dev_prv->device;
889
890 get_device(dev);
891 }
892
klist_devices_put(struct klist_node * n)893 static void klist_devices_put(struct klist_node *n)
894 {
895 struct device_private *dev_prv = to_device_private_bus(n);
896 struct device *dev = dev_prv->device;
897
898 put_device(dev);
899 }
900
bus_uevent_store(const struct bus_type * bus,const char * buf,size_t count)901 static ssize_t bus_uevent_store(const struct bus_type *bus,
902 const char *buf, size_t count)
903 {
904 struct subsys_private *sp = bus_to_subsys(bus);
905 int ret;
906
907 if (!sp)
908 return -EINVAL;
909
910 ret = kobject_synth_uevent(&sp->subsys.kobj, buf, count);
911 subsys_put(sp);
912
913 if (ret)
914 return ret;
915 return count;
916 }
917 /*
918 * "open code" the old BUS_ATTR() macro here. We want to use BUS_ATTR_WO()
919 * here, but can not use it as earlier in the file we have
920 * DEVICE_ATTR_WO(uevent), which would cause a clash with the with the store
921 * function name.
922 */
923 static struct bus_attribute bus_attr_uevent = __ATTR(uevent, 0200, NULL,
924 bus_uevent_store);
925
926 /**
927 * bus_register - register a driver-core subsystem
928 * @bus: bus to register
929 *
930 * Once we have that, we register the bus with the kobject
931 * infrastructure, then register the children subsystems it has:
932 * the devices and drivers that belong to the subsystem.
933 */
bus_register(const struct bus_type * bus)934 int bus_register(const struct bus_type *bus)
935 {
936 int retval;
937 struct subsys_private *priv;
938 struct kobject *bus_kobj;
939 struct lock_class_key *key;
940
941 priv = kzalloc_obj(struct subsys_private);
942 if (!priv)
943 return -ENOMEM;
944
945 priv->bus = bus;
946
947 BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier);
948
949 bus_kobj = &priv->subsys.kobj;
950 retval = kobject_set_name(bus_kobj, "%s", bus->name);
951 if (retval)
952 goto out;
953
954 bus_kobj->kset = bus_kset;
955 bus_kobj->ktype = &bus_ktype;
956 priv->drivers_autoprobe = 1;
957
958 retval = kset_register(&priv->subsys);
959 if (retval)
960 goto out;
961
962 retval = bus_create_file(bus, &bus_attr_uevent);
963 if (retval)
964 goto bus_uevent_fail;
965
966 priv->devices_kset = kset_create_and_add("devices", NULL, bus_kobj);
967 if (!priv->devices_kset) {
968 retval = -ENOMEM;
969 goto bus_devices_fail;
970 }
971
972 priv->drivers_kset = kset_create_and_add("drivers", NULL, bus_kobj);
973 if (!priv->drivers_kset) {
974 retval = -ENOMEM;
975 goto bus_drivers_fail;
976 }
977
978 INIT_LIST_HEAD(&priv->interfaces);
979 key = &priv->lock_key;
980 lockdep_register_key(key);
981 __mutex_init(&priv->mutex, "subsys mutex", key);
982 klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put);
983 klist_init(&priv->klist_drivers, NULL, NULL);
984
985 retval = add_probe_files(bus);
986 if (retval)
987 goto bus_probe_files_fail;
988
989 retval = sysfs_create_groups(bus_kobj, bus->bus_groups);
990 if (retval)
991 goto bus_groups_fail;
992
993 pr_debug("bus: '%s': registered\n", bus->name);
994 return 0;
995
996 bus_groups_fail:
997 remove_probe_files(bus);
998 bus_probe_files_fail:
999 kset_unregister(priv->drivers_kset);
1000 bus_drivers_fail:
1001 kset_unregister(priv->devices_kset);
1002 bus_devices_fail:
1003 bus_remove_file(bus, &bus_attr_uevent);
1004 bus_uevent_fail:
1005 kset_unregister(&priv->subsys);
1006 /* Above kset_unregister() will kfree @priv */
1007 priv = NULL;
1008 out:
1009 kfree(priv);
1010 return retval;
1011 }
1012 EXPORT_SYMBOL_GPL(bus_register);
1013
1014 /**
1015 * bus_unregister - remove a bus from the system
1016 * @bus: bus.
1017 *
1018 * Unregister the child subsystems and the bus itself.
1019 * Finally, we call bus_put() to release the refcount
1020 */
bus_unregister(const struct bus_type * bus)1021 void bus_unregister(const struct bus_type *bus)
1022 {
1023 struct subsys_private *sp = bus_to_subsys(bus);
1024 struct kobject *bus_kobj;
1025
1026 if (!sp)
1027 return;
1028
1029 pr_debug("bus: '%s': unregistering\n", bus->name);
1030 if (sp->dev_root)
1031 device_unregister(sp->dev_root);
1032
1033 bus_kobj = &sp->subsys.kobj;
1034 sysfs_remove_groups(bus_kobj, bus->bus_groups);
1035 remove_probe_files(bus);
1036 bus_remove_file(bus, &bus_attr_uevent);
1037
1038 kset_unregister(sp->drivers_kset);
1039 kset_unregister(sp->devices_kset);
1040 kset_unregister(&sp->subsys);
1041 subsys_put(sp);
1042 }
1043 EXPORT_SYMBOL_GPL(bus_unregister);
1044
bus_register_notifier(const struct bus_type * bus,struct notifier_block * nb)1045 int bus_register_notifier(const struct bus_type *bus, struct notifier_block *nb)
1046 {
1047 struct subsys_private *sp = bus_to_subsys(bus);
1048 int retval;
1049
1050 if (!sp)
1051 return -EINVAL;
1052
1053 retval = blocking_notifier_chain_register(&sp->bus_notifier, nb);
1054 subsys_put(sp);
1055 return retval;
1056 }
1057 EXPORT_SYMBOL_GPL(bus_register_notifier);
1058
bus_unregister_notifier(const struct bus_type * bus,struct notifier_block * nb)1059 int bus_unregister_notifier(const struct bus_type *bus, struct notifier_block *nb)
1060 {
1061 struct subsys_private *sp = bus_to_subsys(bus);
1062 int retval;
1063
1064 if (!sp)
1065 return -EINVAL;
1066 retval = blocking_notifier_chain_unregister(&sp->bus_notifier, nb);
1067 subsys_put(sp);
1068 return retval;
1069 }
1070 EXPORT_SYMBOL_GPL(bus_unregister_notifier);
1071
bus_notify(struct device * dev,enum bus_notifier_event value)1072 void bus_notify(struct device *dev, enum bus_notifier_event value)
1073 {
1074 struct subsys_private *sp = bus_to_subsys(dev->bus);
1075
1076 if (!sp)
1077 return;
1078
1079 blocking_notifier_call_chain(&sp->bus_notifier, value, dev);
1080 subsys_put(sp);
1081 }
1082
bus_get_kset(const struct bus_type * bus)1083 struct kset *bus_get_kset(const struct bus_type *bus)
1084 {
1085 struct subsys_private *sp = bus_to_subsys(bus);
1086 struct kset *kset;
1087
1088 if (!sp)
1089 return NULL;
1090
1091 kset = &sp->subsys;
1092 subsys_put(sp);
1093
1094 return kset;
1095 }
1096 EXPORT_SYMBOL_GPL(bus_get_kset);
1097
1098 /*
1099 * Yes, this forcibly breaks the klist abstraction temporarily. It
1100 * just wants to sort the klist, not change reference counts and
1101 * take/drop locks rapidly in the process. It does all this while
1102 * holding the lock for the list, so objects can't otherwise be
1103 * added/removed while we're swizzling.
1104 */
device_insertion_sort_klist(struct device * a,struct list_head * list,int (* compare)(const struct device * a,const struct device * b))1105 static void device_insertion_sort_klist(struct device *a, struct list_head *list,
1106 int (*compare)(const struct device *a,
1107 const struct device *b))
1108 {
1109 struct klist_node *n;
1110 struct device_private *dev_prv;
1111 struct device *b;
1112
1113 list_for_each_entry(n, list, n_node) {
1114 dev_prv = to_device_private_bus(n);
1115 b = dev_prv->device;
1116 if (compare(a, b) <= 0) {
1117 list_move_tail(&a->p->knode_bus.n_node,
1118 &b->p->knode_bus.n_node);
1119 return;
1120 }
1121 }
1122 list_move_tail(&a->p->knode_bus.n_node, list);
1123 }
1124
bus_sort_breadthfirst(const struct bus_type * bus,int (* compare)(const struct device * a,const struct device * b))1125 void bus_sort_breadthfirst(const struct bus_type *bus,
1126 int (*compare)(const struct device *a,
1127 const struct device *b))
1128 {
1129 struct subsys_private *sp = bus_to_subsys(bus);
1130 LIST_HEAD(sorted_devices);
1131 struct klist_node *n, *tmp;
1132 struct device_private *dev_prv;
1133 struct device *dev;
1134 struct klist *device_klist;
1135
1136 if (!sp)
1137 return;
1138 device_klist = &sp->klist_devices;
1139
1140 spin_lock(&device_klist->k_lock);
1141 list_for_each_entry_safe(n, tmp, &device_klist->k_list, n_node) {
1142 dev_prv = to_device_private_bus(n);
1143 dev = dev_prv->device;
1144 device_insertion_sort_klist(dev, &sorted_devices, compare);
1145 }
1146 list_splice(&sorted_devices, &device_klist->k_list);
1147 spin_unlock(&device_klist->k_lock);
1148 subsys_put(sp);
1149 }
1150 EXPORT_SYMBOL_GPL(bus_sort_breadthfirst);
1151
1152 struct subsys_dev_iter {
1153 struct klist_iter ki;
1154 const struct device_type *type;
1155 };
1156
1157 /**
1158 * subsys_dev_iter_init - initialize subsys device iterator
1159 * @iter: subsys iterator to initialize
1160 * @sp: the subsys private (i.e. bus) we wanna iterate over
1161 * @start: the device to start iterating from, if any
1162 * @type: device_type of the devices to iterate over, NULL for all
1163 *
1164 * Initialize subsys iterator @iter such that it iterates over devices
1165 * of @subsys. If @start is set, the list iteration will start there,
1166 * otherwise if it is NULL, the iteration starts at the beginning of
1167 * the list.
1168 */
subsys_dev_iter_init(struct subsys_dev_iter * iter,struct subsys_private * sp,struct device * start,const struct device_type * type)1169 static void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct subsys_private *sp,
1170 struct device *start, const struct device_type *type)
1171 {
1172 struct klist_node *start_knode = NULL;
1173
1174 if (start)
1175 start_knode = &start->p->knode_bus;
1176 klist_iter_init_node(&sp->klist_devices, &iter->ki, start_knode);
1177 iter->type = type;
1178 }
1179
1180 /**
1181 * subsys_dev_iter_next - iterate to the next device
1182 * @iter: subsys iterator to proceed
1183 *
1184 * Proceed @iter to the next device and return it. Returns NULL if
1185 * iteration is complete.
1186 *
1187 * The returned device is referenced and won't be released till
1188 * iterator is proceed to the next device or exited. The caller is
1189 * free to do whatever it wants to do with the device including
1190 * calling back into subsys code.
1191 */
subsys_dev_iter_next(struct subsys_dev_iter * iter)1192 static struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter)
1193 {
1194 struct klist_node *knode;
1195 struct device *dev;
1196
1197 for (;;) {
1198 knode = klist_next(&iter->ki);
1199 if (!knode)
1200 return NULL;
1201 dev = to_device_private_bus(knode)->device;
1202 if (!iter->type || iter->type == dev->type)
1203 return dev;
1204 }
1205 }
1206
1207 /**
1208 * subsys_dev_iter_exit - finish iteration
1209 * @iter: subsys iterator to finish
1210 *
1211 * Finish an iteration. Always call this function after iteration is
1212 * complete whether the iteration ran till the end or not.
1213 */
subsys_dev_iter_exit(struct subsys_dev_iter * iter)1214 static void subsys_dev_iter_exit(struct subsys_dev_iter *iter)
1215 {
1216 klist_iter_exit(&iter->ki);
1217 }
1218
subsys_interface_register(struct subsys_interface * sif)1219 int subsys_interface_register(struct subsys_interface *sif)
1220 {
1221 struct subsys_private *sp;
1222 struct subsys_dev_iter iter;
1223 struct device *dev;
1224
1225 if (!sif || !sif->subsys)
1226 return -ENODEV;
1227
1228 sp = bus_to_subsys(sif->subsys);
1229 if (!sp)
1230 return -EINVAL;
1231
1232 /*
1233 * Reference in sp is now incremented and will be dropped when
1234 * the interface is removed from the bus
1235 */
1236
1237 mutex_lock(&sp->mutex);
1238 list_add_tail(&sif->node, &sp->interfaces);
1239 if (sif->add_dev) {
1240 subsys_dev_iter_init(&iter, sp, NULL, NULL);
1241 while ((dev = subsys_dev_iter_next(&iter)))
1242 sif->add_dev(dev, sif);
1243 subsys_dev_iter_exit(&iter);
1244 }
1245 mutex_unlock(&sp->mutex);
1246
1247 return 0;
1248 }
1249 EXPORT_SYMBOL_GPL(subsys_interface_register);
1250
subsys_interface_unregister(struct subsys_interface * sif)1251 void subsys_interface_unregister(struct subsys_interface *sif)
1252 {
1253 struct subsys_private *sp;
1254 struct subsys_dev_iter iter;
1255 struct device *dev;
1256
1257 if (!sif || !sif->subsys)
1258 return;
1259
1260 sp = bus_to_subsys(sif->subsys);
1261 if (!sp)
1262 return;
1263
1264 mutex_lock(&sp->mutex);
1265 list_del_init(&sif->node);
1266 if (sif->remove_dev) {
1267 subsys_dev_iter_init(&iter, sp, NULL, NULL);
1268 while ((dev = subsys_dev_iter_next(&iter)))
1269 sif->remove_dev(dev, sif);
1270 subsys_dev_iter_exit(&iter);
1271 }
1272 mutex_unlock(&sp->mutex);
1273
1274 /*
1275 * Decrement the reference count twice, once for the bus_to_subsys()
1276 * call in the start of this function, and the second one from the
1277 * reference increment in subsys_interface_register()
1278 */
1279 subsys_put(sp);
1280 subsys_put(sp);
1281 }
1282 EXPORT_SYMBOL_GPL(subsys_interface_unregister);
1283
system_root_device_release(struct device * dev)1284 static void system_root_device_release(struct device *dev)
1285 {
1286 kfree(dev);
1287 }
1288
subsys_register(const struct bus_type * subsys,const struct attribute_group ** groups,struct kobject * parent_of_root)1289 static int subsys_register(const struct bus_type *subsys,
1290 const struct attribute_group **groups,
1291 struct kobject *parent_of_root)
1292 {
1293 struct subsys_private *sp;
1294 struct device *dev;
1295 int err;
1296
1297 err = bus_register(subsys);
1298 if (err < 0)
1299 return err;
1300
1301 sp = bus_to_subsys(subsys);
1302 if (!sp) {
1303 err = -EINVAL;
1304 goto err_sp;
1305 }
1306
1307 dev = kzalloc_obj(struct device);
1308 if (!dev) {
1309 err = -ENOMEM;
1310 goto err_dev;
1311 }
1312
1313 err = dev_set_name(dev, "%s", subsys->name);
1314 if (err < 0)
1315 goto err_name;
1316
1317 dev->kobj.parent = parent_of_root;
1318 dev->groups = groups;
1319 dev->release = system_root_device_release;
1320
1321 err = device_register(dev);
1322 if (err < 0)
1323 goto err_dev_reg;
1324
1325 sp->dev_root = dev;
1326 subsys_put(sp);
1327 return 0;
1328
1329 err_dev_reg:
1330 put_device(dev);
1331 dev = NULL;
1332 err_name:
1333 kfree(dev);
1334 err_dev:
1335 subsys_put(sp);
1336 err_sp:
1337 bus_unregister(subsys);
1338 return err;
1339 }
1340
1341 /**
1342 * subsys_system_register - register a subsystem at /sys/devices/system/
1343 * @subsys: system subsystem
1344 * @groups: default attributes for the root device
1345 *
1346 * All 'system' subsystems have a /sys/devices/system/<name> root device
1347 * with the name of the subsystem. The root device can carry subsystem-
1348 * wide attributes. All registered devices are below this single root
1349 * device and are named after the subsystem with a simple enumeration
1350 * number appended. The registered devices are not explicitly named;
1351 * only 'id' in the device needs to be set.
1352 *
1353 * Do not use this interface for anything new, it exists for compatibility
1354 * with bad ideas only. New subsystems should use plain subsystems; and
1355 * add the subsystem-wide attributes should be added to the subsystem
1356 * directory itself and not some create fake root-device placed in
1357 * /sys/devices/system/<name>.
1358 */
subsys_system_register(const struct bus_type * subsys,const struct attribute_group ** groups)1359 int subsys_system_register(const struct bus_type *subsys,
1360 const struct attribute_group **groups)
1361 {
1362 return subsys_register(subsys, groups, &system_kset->kobj);
1363 }
1364 EXPORT_SYMBOL_GPL(subsys_system_register);
1365
1366 /**
1367 * subsys_virtual_register - register a subsystem at /sys/devices/virtual/
1368 * @subsys: virtual subsystem
1369 * @groups: default attributes for the root device
1370 *
1371 * All 'virtual' subsystems have a /sys/devices/system/<name> root device
1372 * with the name of the subsystem. The root device can carry subsystem-wide
1373 * attributes. All registered devices are below this single root device.
1374 * There's no restriction on device naming. This is for kernel software
1375 * constructs which need sysfs interface.
1376 */
subsys_virtual_register(const struct bus_type * subsys,const struct attribute_group ** groups)1377 int subsys_virtual_register(const struct bus_type *subsys,
1378 const struct attribute_group **groups)
1379 {
1380 struct kobject *virtual_dir;
1381
1382 virtual_dir = virtual_device_parent();
1383 if (!virtual_dir)
1384 return -ENOMEM;
1385
1386 return subsys_register(subsys, groups, virtual_dir);
1387 }
1388 EXPORT_SYMBOL_GPL(subsys_virtual_register);
1389
1390 /**
1391 * driver_find - locate driver on a bus by its name.
1392 * @name: name of the driver.
1393 * @bus: bus to scan for the driver.
1394 *
1395 * Call kset_find_obj() to iterate over list of drivers on
1396 * a bus to find driver by name. Return driver if found.
1397 *
1398 * This routine provides no locking to prevent the driver it returns
1399 * from being unregistered or unloaded while the caller is using it.
1400 * The caller is responsible for preventing this.
1401 */
driver_find(const char * name,const struct bus_type * bus)1402 struct device_driver *driver_find(const char *name, const struct bus_type *bus)
1403 {
1404 struct subsys_private *sp = bus_to_subsys(bus);
1405 struct kobject *k;
1406 struct driver_private *priv;
1407
1408 if (!sp)
1409 return NULL;
1410
1411 k = kset_find_obj(sp->drivers_kset, name);
1412 subsys_put(sp);
1413 if (!k)
1414 return NULL;
1415
1416 priv = to_driver(k);
1417
1418 /* Drop reference added by kset_find_obj() */
1419 kobject_put(k);
1420 return priv->driver;
1421 }
1422 EXPORT_SYMBOL_GPL(driver_find);
1423
1424 /*
1425 * Warning, the value could go to "removed" instantly after calling this function, so be very
1426 * careful when calling it...
1427 */
bus_is_registered(const struct bus_type * bus)1428 bool bus_is_registered(const struct bus_type *bus)
1429 {
1430 struct subsys_private *sp = bus_to_subsys(bus);
1431 bool is_initialized = false;
1432
1433 if (sp) {
1434 is_initialized = true;
1435 subsys_put(sp);
1436 }
1437 return is_initialized;
1438 }
1439
1440 /**
1441 * bus_get_dev_root - return a pointer to the "device root" of a bus
1442 * @bus: bus to return the device root of.
1443 *
1444 * If a bus has a "device root" structure, return it, WITH THE REFERENCE
1445 * COUNT INCREMENTED.
1446 *
1447 * Note, when finished with the device, a call to put_device() is required.
1448 *
1449 * If the device root is not present (or bus is not a valid pointer), NULL
1450 * will be returned.
1451 */
bus_get_dev_root(const struct bus_type * bus)1452 struct device *bus_get_dev_root(const struct bus_type *bus)
1453 {
1454 struct subsys_private *sp = bus_to_subsys(bus);
1455 struct device *dev_root;
1456
1457 if (!sp)
1458 return NULL;
1459
1460 dev_root = get_device(sp->dev_root);
1461 subsys_put(sp);
1462 return dev_root;
1463 }
1464 EXPORT_SYMBOL_GPL(bus_get_dev_root);
1465
buses_init(void)1466 int __init buses_init(void)
1467 {
1468 bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL);
1469 if (!bus_kset)
1470 return -ENOMEM;
1471
1472 system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj);
1473 if (!system_kset) {
1474 /* Do error handling here as devices_init() do */
1475 kset_unregister(bus_kset);
1476 bus_kset = NULL;
1477 pr_err("%s: failed to create and add kset 'bus'\n", __func__);
1478 return -ENOMEM;
1479 }
1480
1481 return 0;
1482 }
1483