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