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