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