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