projects/linux·blob master · refs

1489 lines · 36.6 KiB

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