xref: /openbmc/linux/drivers/base/bus.c (revision e868d61272caa648214046a096e5a6bfc068dc8c)
1 /*
2  * bus.c - bus driver management
3  *
4  * Copyright (c) 2002-3 Patrick Mochel
5  * Copyright (c) 2002-3 Open Source Development Labs
6  *
7  * This file is released under the GPLv2
8  *
9  */
10 
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/errno.h>
14 #include <linux/init.h>
15 #include <linux/string.h>
16 #include "base.h"
17 #include "power/power.h"
18 
19 #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
20 #define to_bus(obj) container_of(obj, struct bus_type, subsys.kobj)
21 
22 /*
23  * sysfs bindings for drivers
24  */
25 
26 #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
27 #define to_driver(obj) container_of(obj, struct device_driver, kobj)
28 
29 
30 static int __must_check bus_rescan_devices_helper(struct device *dev,
31 						void *data);
32 
33 static ssize_t
34 drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
35 {
36 	struct driver_attribute * drv_attr = to_drv_attr(attr);
37 	struct device_driver * drv = to_driver(kobj);
38 	ssize_t ret = -EIO;
39 
40 	if (drv_attr->show)
41 		ret = drv_attr->show(drv, buf);
42 	return ret;
43 }
44 
45 static ssize_t
46 drv_attr_store(struct kobject * kobj, struct attribute * attr,
47 	       const char * buf, size_t count)
48 {
49 	struct driver_attribute * drv_attr = to_drv_attr(attr);
50 	struct device_driver * drv = to_driver(kobj);
51 	ssize_t ret = -EIO;
52 
53 	if (drv_attr->store)
54 		ret = drv_attr->store(drv, buf, count);
55 	return ret;
56 }
57 
58 static struct sysfs_ops driver_sysfs_ops = {
59 	.show	= drv_attr_show,
60 	.store	= drv_attr_store,
61 };
62 
63 
64 static void driver_release(struct kobject * kobj)
65 {
66 	/*
67 	 * Yes this is an empty release function, it is this way because struct
68 	 * device is always a static object, not a dynamic one.  Yes, this is
69 	 * not nice and bad, but remember, drivers are code, reference counted
70 	 * by the module count, not a device, which is really data.  And yes,
71 	 * in the future I do want to have all drivers be created dynamically,
72 	 * and am working toward that goal, but it will take a bit longer...
73 	 *
74 	 * But do not let this example give _anyone_ the idea that they can
75 	 * create a release function without any code in it at all, to do that
76 	 * is almost always wrong.  If you have any questions about this,
77 	 * please send an email to <greg@kroah.com>
78 	 */
79 }
80 
81 static struct kobj_type ktype_driver = {
82 	.sysfs_ops	= &driver_sysfs_ops,
83 	.release	= driver_release,
84 };
85 
86 
87 /*
88  * sysfs bindings for buses
89  */
90 
91 
92 static ssize_t
93 bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
94 {
95 	struct bus_attribute * bus_attr = to_bus_attr(attr);
96 	struct bus_type * bus = to_bus(kobj);
97 	ssize_t ret = 0;
98 
99 	if (bus_attr->show)
100 		ret = bus_attr->show(bus, buf);
101 	return ret;
102 }
103 
104 static ssize_t
105 bus_attr_store(struct kobject * kobj, struct attribute * attr,
106 	       const char * buf, size_t count)
107 {
108 	struct bus_attribute * bus_attr = to_bus_attr(attr);
109 	struct bus_type * bus = to_bus(kobj);
110 	ssize_t ret = 0;
111 
112 	if (bus_attr->store)
113 		ret = bus_attr->store(bus, buf, count);
114 	return ret;
115 }
116 
117 static struct sysfs_ops bus_sysfs_ops = {
118 	.show	= bus_attr_show,
119 	.store	= bus_attr_store,
120 };
121 
122 int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
123 {
124 	int error;
125 	if (get_bus(bus)) {
126 		error = sysfs_create_file(&bus->subsys.kobj, &attr->attr);
127 		put_bus(bus);
128 	} else
129 		error = -EINVAL;
130 	return error;
131 }
132 
133 void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
134 {
135 	if (get_bus(bus)) {
136 		sysfs_remove_file(&bus->subsys.kobj, &attr->attr);
137 		put_bus(bus);
138 	}
139 }
140 
141 static struct kobj_type ktype_bus = {
142 	.sysfs_ops	= &bus_sysfs_ops,
143 
144 };
145 
146 static decl_subsys(bus, &ktype_bus, NULL);
147 
148 
149 #ifdef CONFIG_HOTPLUG
150 /* Manually detach a device from its associated driver. */
151 static int driver_helper(struct device *dev, void *data)
152 {
153 	const char *name = data;
154 
155 	if (strcmp(name, dev->bus_id) == 0)
156 		return 1;
157 	return 0;
158 }
159 
160 static ssize_t driver_unbind(struct device_driver *drv,
161 			     const char *buf, size_t count)
162 {
163 	struct bus_type *bus = get_bus(drv->bus);
164 	struct device *dev;
165 	int err = -ENODEV;
166 
167 	dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
168 	if (dev && dev->driver == drv) {
169 		if (dev->parent)	/* Needed for USB */
170 			down(&dev->parent->sem);
171 		device_release_driver(dev);
172 		if (dev->parent)
173 			up(&dev->parent->sem);
174 		err = count;
175 	}
176 	put_device(dev);
177 	put_bus(bus);
178 	return err;
179 }
180 static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
181 
182 /*
183  * Manually attach a device to a driver.
184  * Note: the driver must want to bind to the device,
185  * it is not possible to override the driver's id table.
186  */
187 static ssize_t driver_bind(struct device_driver *drv,
188 			   const char *buf, size_t count)
189 {
190 	struct bus_type *bus = get_bus(drv->bus);
191 	struct device *dev;
192 	int err = -ENODEV;
193 
194 	dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
195 	if (dev && dev->driver == NULL) {
196 		if (dev->parent)	/* Needed for USB */
197 			down(&dev->parent->sem);
198 		down(&dev->sem);
199 		err = driver_probe_device(drv, dev);
200 		up(&dev->sem);
201 		if (dev->parent)
202 			up(&dev->parent->sem);
203 
204 		if (err > 0) 		/* success */
205 			err = count;
206 		else if (err == 0)	/* driver didn't accept device */
207 			err = -ENODEV;
208 	}
209 	put_device(dev);
210 	put_bus(bus);
211 	return err;
212 }
213 static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
214 
215 static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf)
216 {
217 	return sprintf(buf, "%d\n", bus->drivers_autoprobe);
218 }
219 
220 static ssize_t store_drivers_autoprobe(struct bus_type *bus,
221 				       const char *buf, size_t count)
222 {
223 	if (buf[0] == '0')
224 		bus->drivers_autoprobe = 0;
225 	else
226 		bus->drivers_autoprobe = 1;
227 	return count;
228 }
229 
230 static ssize_t store_drivers_probe(struct bus_type *bus,
231 				   const char *buf, size_t count)
232 {
233 	struct device *dev;
234 
235 	dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
236 	if (!dev)
237 		return -ENODEV;
238 	if (bus_rescan_devices_helper(dev, NULL) != 0)
239 		return -EINVAL;
240 	return count;
241 }
242 #endif
243 
244 static struct device * next_device(struct klist_iter * i)
245 {
246 	struct klist_node * n = klist_next(i);
247 	return n ? container_of(n, struct device, knode_bus) : NULL;
248 }
249 
250 /**
251  *	bus_for_each_dev - device iterator.
252  *	@bus:	bus type.
253  *	@start:	device to start iterating from.
254  *	@data:	data for the callback.
255  *	@fn:	function to be called for each device.
256  *
257  *	Iterate over @bus's list of devices, and call @fn for each,
258  *	passing it @data. If @start is not NULL, we use that device to
259  *	begin iterating from.
260  *
261  *	We check the return of @fn each time. If it returns anything
262  *	other than 0, we break out and return that value.
263  *
264  *	NOTE: The device that returns a non-zero value is not retained
265  *	in any way, nor is its refcount incremented. If the caller needs
266  *	to retain this data, it should do, and increment the reference
267  *	count in the supplied callback.
268  */
269 
270 int bus_for_each_dev(struct bus_type * bus, struct device * start,
271 		     void * data, int (*fn)(struct device *, void *))
272 {
273 	struct klist_iter i;
274 	struct device * dev;
275 	int error = 0;
276 
277 	if (!bus)
278 		return -EINVAL;
279 
280 	klist_iter_init_node(&bus->klist_devices, &i,
281 			     (start ? &start->knode_bus : NULL));
282 	while ((dev = next_device(&i)) && !error)
283 		error = fn(dev, data);
284 	klist_iter_exit(&i);
285 	return error;
286 }
287 
288 /**
289  * bus_find_device - device iterator for locating a particular device.
290  * @bus: bus type
291  * @start: Device to begin with
292  * @data: Data to pass to match function
293  * @match: Callback function to check device
294  *
295  * This is similar to the bus_for_each_dev() function above, but it
296  * returns a reference to a device that is 'found' for later use, as
297  * determined by the @match callback.
298  *
299  * The callback should return 0 if the device doesn't match and non-zero
300  * if it does.  If the callback returns non-zero, this function will
301  * return to the caller and not iterate over any more devices.
302  */
303 struct device * bus_find_device(struct bus_type *bus,
304 				struct device *start, void *data,
305 				int (*match)(struct device *, void *))
306 {
307 	struct klist_iter i;
308 	struct device *dev;
309 
310 	if (!bus)
311 		return NULL;
312 
313 	klist_iter_init_node(&bus->klist_devices, &i,
314 			     (start ? &start->knode_bus : NULL));
315 	while ((dev = next_device(&i)))
316 		if (match(dev, data) && get_device(dev))
317 			break;
318 	klist_iter_exit(&i);
319 	return dev;
320 }
321 
322 
323 static struct device_driver * next_driver(struct klist_iter * i)
324 {
325 	struct klist_node * n = klist_next(i);
326 	return n ? container_of(n, struct device_driver, knode_bus) : NULL;
327 }
328 
329 /**
330  *	bus_for_each_drv - driver iterator
331  *	@bus:	bus we're dealing with.
332  *	@start:	driver to start iterating on.
333  *	@data:	data to pass to the callback.
334  *	@fn:	function to call for each driver.
335  *
336  *	This is nearly identical to the device iterator above.
337  *	We iterate over each driver that belongs to @bus, and call
338  *	@fn for each. If @fn returns anything but 0, we break out
339  *	and return it. If @start is not NULL, we use it as the head
340  *	of the list.
341  *
342  *	NOTE: we don't return the driver that returns a non-zero
343  *	value, nor do we leave the reference count incremented for that
344  *	driver. If the caller needs to know that info, it must set it
345  *	in the callback. It must also be sure to increment the refcount
346  *	so it doesn't disappear before returning to the caller.
347  */
348 
349 int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
350 		     void * data, int (*fn)(struct device_driver *, void *))
351 {
352 	struct klist_iter i;
353 	struct device_driver * drv;
354 	int error = 0;
355 
356 	if (!bus)
357 		return -EINVAL;
358 
359 	klist_iter_init_node(&bus->klist_drivers, &i,
360 			     start ? &start->knode_bus : NULL);
361 	while ((drv = next_driver(&i)) && !error)
362 		error = fn(drv, data);
363 	klist_iter_exit(&i);
364 	return error;
365 }
366 
367 static int device_add_attrs(struct bus_type *bus, struct device *dev)
368 {
369 	int error = 0;
370 	int i;
371 
372 	if (!bus->dev_attrs)
373 		return 0;
374 
375 	for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
376 		error = device_create_file(dev,&bus->dev_attrs[i]);
377 		if (error) {
378 			while (--i >= 0)
379 				device_remove_file(dev, &bus->dev_attrs[i]);
380 			break;
381 		}
382 	}
383 	return error;
384 }
385 
386 static void device_remove_attrs(struct bus_type * bus, struct device * dev)
387 {
388 	int i;
389 
390 	if (bus->dev_attrs) {
391 		for (i = 0; attr_name(bus->dev_attrs[i]); i++)
392 			device_remove_file(dev,&bus->dev_attrs[i]);
393 	}
394 }
395 
396 #ifdef CONFIG_SYSFS_DEPRECATED
397 static int make_deprecated_bus_links(struct device *dev)
398 {
399 	return sysfs_create_link(&dev->kobj,
400 				 &dev->bus->subsys.kobj, "bus");
401 }
402 
403 static void remove_deprecated_bus_links(struct device *dev)
404 {
405 	sysfs_remove_link(&dev->kobj, "bus");
406 }
407 #else
408 static inline int make_deprecated_bus_links(struct device *dev) { return 0; }
409 static inline void remove_deprecated_bus_links(struct device *dev) { }
410 #endif
411 
412 /**
413  *	bus_add_device - add device to bus
414  *	@dev:	device being added
415  *
416  *	- Add the device to its bus's list of devices.
417  *	- Create link to device's bus.
418  */
419 int bus_add_device(struct device * dev)
420 {
421 	struct bus_type * bus = get_bus(dev->bus);
422 	int error = 0;
423 
424 	if (bus) {
425 		pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
426 		error = device_add_attrs(bus, dev);
427 		if (error)
428 			goto out_put;
429 		error = sysfs_create_link(&bus->devices.kobj,
430 						&dev->kobj, dev->bus_id);
431 		if (error)
432 			goto out_id;
433 		error = sysfs_create_link(&dev->kobj,
434 				&dev->bus->subsys.kobj, "subsystem");
435 		if (error)
436 			goto out_subsys;
437 		error = make_deprecated_bus_links(dev);
438 		if (error)
439 			goto out_deprecated;
440 	}
441 	return 0;
442 
443 out_deprecated:
444 	sysfs_remove_link(&dev->kobj, "subsystem");
445 out_subsys:
446 	sysfs_remove_link(&bus->devices.kobj, dev->bus_id);
447 out_id:
448 	device_remove_attrs(bus, dev);
449 out_put:
450 	put_bus(dev->bus);
451 	return error;
452 }
453 
454 /**
455  *	bus_attach_device - add device to bus
456  *	@dev:	device tried to attach to a driver
457  *
458  *	- Add device to bus's list of devices.
459  *	- Try to attach to driver.
460  */
461 void bus_attach_device(struct device * dev)
462 {
463 	struct bus_type *bus = dev->bus;
464 	int ret = 0;
465 
466 	if (bus) {
467 		dev->is_registered = 1;
468 		if (bus->drivers_autoprobe)
469 			ret = device_attach(dev);
470 		WARN_ON(ret < 0);
471 		if (ret >= 0)
472 			klist_add_tail(&dev->knode_bus, &bus->klist_devices);
473 		else
474 			dev->is_registered = 0;
475 	}
476 }
477 
478 /**
479  *	bus_remove_device - remove device from bus
480  *	@dev:	device to be removed
481  *
482  *	- Remove symlink from bus's directory.
483  *	- Delete device from bus's list.
484  *	- Detach from its driver.
485  *	- Drop reference taken in bus_add_device().
486  */
487 void bus_remove_device(struct device * dev)
488 {
489 	if (dev->bus) {
490 		sysfs_remove_link(&dev->kobj, "subsystem");
491 		remove_deprecated_bus_links(dev);
492 		sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
493 		device_remove_attrs(dev->bus, dev);
494 		if (dev->is_registered) {
495 			dev->is_registered = 0;
496 			klist_del(&dev->knode_bus);
497 		}
498 		pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
499 		device_release_driver(dev);
500 		put_bus(dev->bus);
501 	}
502 }
503 
504 static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
505 {
506 	int error = 0;
507 	int i;
508 
509 	if (bus->drv_attrs) {
510 		for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
511 			error = driver_create_file(drv, &bus->drv_attrs[i]);
512 			if (error)
513 				goto Err;
514 		}
515 	}
516  Done:
517 	return error;
518  Err:
519 	while (--i >= 0)
520 		driver_remove_file(drv, &bus->drv_attrs[i]);
521 	goto Done;
522 }
523 
524 
525 static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
526 {
527 	int i;
528 
529 	if (bus->drv_attrs) {
530 		for (i = 0; attr_name(bus->drv_attrs[i]); i++)
531 			driver_remove_file(drv, &bus->drv_attrs[i]);
532 	}
533 }
534 
535 #ifdef CONFIG_HOTPLUG
536 /*
537  * Thanks to drivers making their tables __devinit, we can't allow manual
538  * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled.
539  */
540 static int __must_check add_bind_files(struct device_driver *drv)
541 {
542 	int ret;
543 
544 	ret = driver_create_file(drv, &driver_attr_unbind);
545 	if (ret == 0) {
546 		ret = driver_create_file(drv, &driver_attr_bind);
547 		if (ret)
548 			driver_remove_file(drv, &driver_attr_unbind);
549 	}
550 	return ret;
551 }
552 
553 static void remove_bind_files(struct device_driver *drv)
554 {
555 	driver_remove_file(drv, &driver_attr_bind);
556 	driver_remove_file(drv, &driver_attr_unbind);
557 }
558 
559 static int add_probe_files(struct bus_type *bus)
560 {
561 	int retval;
562 
563 	bus->drivers_probe_attr.attr.name = "drivers_probe";
564 	bus->drivers_probe_attr.attr.mode = S_IWUSR;
565 	bus->drivers_probe_attr.attr.owner = bus->owner;
566 	bus->drivers_probe_attr.store = store_drivers_probe;
567 	retval = bus_create_file(bus, &bus->drivers_probe_attr);
568 	if (retval)
569 		goto out;
570 
571 	bus->drivers_autoprobe_attr.attr.name = "drivers_autoprobe";
572 	bus->drivers_autoprobe_attr.attr.mode = S_IWUSR | S_IRUGO;
573 	bus->drivers_autoprobe_attr.attr.owner = bus->owner;
574 	bus->drivers_autoprobe_attr.show = show_drivers_autoprobe;
575 	bus->drivers_autoprobe_attr.store = store_drivers_autoprobe;
576 	retval = bus_create_file(bus, &bus->drivers_autoprobe_attr);
577 	if (retval)
578 		bus_remove_file(bus, &bus->drivers_probe_attr);
579 out:
580 	return retval;
581 }
582 
583 static void remove_probe_files(struct bus_type *bus)
584 {
585 	bus_remove_file(bus, &bus->drivers_autoprobe_attr);
586 	bus_remove_file(bus, &bus->drivers_probe_attr);
587 }
588 #else
589 static inline int add_bind_files(struct device_driver *drv) { return 0; }
590 static inline void remove_bind_files(struct device_driver *drv) {}
591 static inline int add_probe_files(struct bus_type *bus) { return 0; }
592 static inline void remove_probe_files(struct bus_type *bus) {}
593 #endif
594 
595 /**
596  *	bus_add_driver - Add a driver to the bus.
597  *	@drv:	driver.
598  *
599  */
600 int bus_add_driver(struct device_driver *drv)
601 {
602 	struct bus_type * bus = get_bus(drv->bus);
603 	int error = 0;
604 
605 	if (!bus)
606 		return -EINVAL;
607 
608 	pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
609 	error = kobject_set_name(&drv->kobj, "%s", drv->name);
610 	if (error)
611 		goto out_put_bus;
612 	drv->kobj.kset = &bus->drivers;
613 	if ((error = kobject_register(&drv->kobj)))
614 		goto out_put_bus;
615 
616 	if (drv->bus->drivers_autoprobe) {
617 		error = driver_attach(drv);
618 		if (error)
619 			goto out_unregister;
620 	}
621 	klist_add_tail(&drv->knode_bus, &bus->klist_drivers);
622 	module_add_driver(drv->owner, drv);
623 
624 	error = driver_add_attrs(bus, drv);
625 	if (error) {
626 		/* How the hell do we get out of this pickle? Give up */
627 		printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",
628 			__FUNCTION__, drv->name);
629 	}
630 	error = add_bind_files(drv);
631 	if (error) {
632 		/* Ditto */
633 		printk(KERN_ERR "%s: add_bind_files(%s) failed\n",
634 			__FUNCTION__, drv->name);
635 	}
636 
637 	return error;
638 out_unregister:
639 	kobject_unregister(&drv->kobj);
640 out_put_bus:
641 	put_bus(bus);
642 	return error;
643 }
644 
645 /**
646  *	bus_remove_driver - delete driver from bus's knowledge.
647  *	@drv:	driver.
648  *
649  *	Detach the driver from the devices it controls, and remove
650  *	it from its bus's list of drivers. Finally, we drop the reference
651  *	to the bus we took in bus_add_driver().
652  */
653 
654 void bus_remove_driver(struct device_driver * drv)
655 {
656 	if (!drv->bus)
657 		return;
658 
659 	remove_bind_files(drv);
660 	driver_remove_attrs(drv->bus, drv);
661 	klist_remove(&drv->knode_bus);
662 	pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
663 	driver_detach(drv);
664 	module_remove_driver(drv);
665 	kobject_unregister(&drv->kobj);
666 	put_bus(drv->bus);
667 }
668 
669 
670 /* Helper for bus_rescan_devices's iter */
671 static int __must_check bus_rescan_devices_helper(struct device *dev,
672 						void *data)
673 {
674 	int ret = 0;
675 
676 	if (!dev->driver) {
677 		if (dev->parent)	/* Needed for USB */
678 			down(&dev->parent->sem);
679 		ret = device_attach(dev);
680 		if (dev->parent)
681 			up(&dev->parent->sem);
682 	}
683 	return ret < 0 ? ret : 0;
684 }
685 
686 /**
687  * bus_rescan_devices - rescan devices on the bus for possible drivers
688  * @bus: the bus to scan.
689  *
690  * This function will look for devices on the bus with no driver
691  * attached and rescan it against existing drivers to see if it matches
692  * any by calling device_attach() for the unbound devices.
693  */
694 int bus_rescan_devices(struct bus_type * bus)
695 {
696 	return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
697 }
698 
699 /**
700  * device_reprobe - remove driver for a device and probe for a new driver
701  * @dev: the device to reprobe
702  *
703  * This function detaches the attached driver (if any) for the given
704  * device and restarts the driver probing process.  It is intended
705  * to use if probing criteria changed during a devices lifetime and
706  * driver attachment should change accordingly.
707  */
708 int device_reprobe(struct device *dev)
709 {
710 	if (dev->driver) {
711 		if (dev->parent)        /* Needed for USB */
712 			down(&dev->parent->sem);
713 		device_release_driver(dev);
714 		if (dev->parent)
715 			up(&dev->parent->sem);
716 	}
717 	return bus_rescan_devices_helper(dev, NULL);
718 }
719 EXPORT_SYMBOL_GPL(device_reprobe);
720 
721 struct bus_type *get_bus(struct bus_type *bus)
722 {
723 	return bus ? container_of(subsys_get(&bus->subsys),
724 				struct bus_type, subsys) : NULL;
725 }
726 
727 void put_bus(struct bus_type * bus)
728 {
729 	subsys_put(&bus->subsys);
730 }
731 
732 
733 /**
734  *	find_bus - locate bus by name.
735  *	@name:	name of bus.
736  *
737  *	Call kset_find_obj() to iterate over list of buses to
738  *	find a bus by name. Return bus if found.
739  *
740  *	Note that kset_find_obj increments bus' reference count.
741  */
742 #if 0
743 struct bus_type * find_bus(char * name)
744 {
745 	struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
746 	return k ? to_bus(k) : NULL;
747 }
748 #endif  /*  0  */
749 
750 
751 /**
752  *	bus_add_attrs - Add default attributes for this bus.
753  *	@bus:	Bus that has just been registered.
754  */
755 
756 static int bus_add_attrs(struct bus_type * bus)
757 {
758 	int error = 0;
759 	int i;
760 
761 	if (bus->bus_attrs) {
762 		for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
763 			if ((error = bus_create_file(bus,&bus->bus_attrs[i])))
764 				goto Err;
765 		}
766 	}
767  Done:
768 	return error;
769  Err:
770 	while (--i >= 0)
771 		bus_remove_file(bus,&bus->bus_attrs[i]);
772 	goto Done;
773 }
774 
775 static void bus_remove_attrs(struct bus_type * bus)
776 {
777 	int i;
778 
779 	if (bus->bus_attrs) {
780 		for (i = 0; attr_name(bus->bus_attrs[i]); i++)
781 			bus_remove_file(bus,&bus->bus_attrs[i]);
782 	}
783 }
784 
785 static void klist_devices_get(struct klist_node *n)
786 {
787 	struct device *dev = container_of(n, struct device, knode_bus);
788 
789 	get_device(dev);
790 }
791 
792 static void klist_devices_put(struct klist_node *n)
793 {
794 	struct device *dev = container_of(n, struct device, knode_bus);
795 
796 	put_device(dev);
797 }
798 
799 /**
800  *	bus_register - register a bus with the system.
801  *	@bus:	bus.
802  *
803  *	Once we have that, we registered the bus with the kobject
804  *	infrastructure, then register the children subsystems it has:
805  *	the devices and drivers that belong to the bus.
806  */
807 int bus_register(struct bus_type * bus)
808 {
809 	int retval;
810 
811 	BLOCKING_INIT_NOTIFIER_HEAD(&bus->bus_notifier);
812 
813 	retval = kobject_set_name(&bus->subsys.kobj, "%s", bus->name);
814 	if (retval)
815 		goto out;
816 
817 	subsys_set_kset(bus, bus_subsys);
818 	retval = subsystem_register(&bus->subsys);
819 	if (retval)
820 		goto out;
821 
822 	kobject_set_name(&bus->devices.kobj, "devices");
823 	bus->devices.kobj.parent = &bus->subsys.kobj;
824 	retval = kset_register(&bus->devices);
825 	if (retval)
826 		goto bus_devices_fail;
827 
828 	kobject_set_name(&bus->drivers.kobj, "drivers");
829 	bus->drivers.kobj.parent = &bus->subsys.kobj;
830 	bus->drivers.ktype = &ktype_driver;
831 	retval = kset_register(&bus->drivers);
832 	if (retval)
833 		goto bus_drivers_fail;
834 
835 	klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put);
836 	klist_init(&bus->klist_drivers, NULL, NULL);
837 
838 	bus->drivers_autoprobe = 1;
839 	retval = add_probe_files(bus);
840 	if (retval)
841 		goto bus_probe_files_fail;
842 
843 	retval = bus_add_attrs(bus);
844 	if (retval)
845 		goto bus_attrs_fail;
846 
847 	pr_debug("bus type '%s' registered\n", bus->name);
848 	return 0;
849 
850 bus_attrs_fail:
851 	remove_probe_files(bus);
852 bus_probe_files_fail:
853 	kset_unregister(&bus->drivers);
854 bus_drivers_fail:
855 	kset_unregister(&bus->devices);
856 bus_devices_fail:
857 	subsystem_unregister(&bus->subsys);
858 out:
859 	return retval;
860 }
861 
862 /**
863  *	bus_unregister - remove a bus from the system
864  *	@bus:	bus.
865  *
866  *	Unregister the child subsystems and the bus itself.
867  *	Finally, we call put_bus() to release the refcount
868  */
869 void bus_unregister(struct bus_type * bus)
870 {
871 	pr_debug("bus %s: unregistering\n", bus->name);
872 	bus_remove_attrs(bus);
873 	remove_probe_files(bus);
874 	kset_unregister(&bus->drivers);
875 	kset_unregister(&bus->devices);
876 	subsystem_unregister(&bus->subsys);
877 }
878 
879 int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb)
880 {
881 	return blocking_notifier_chain_register(&bus->bus_notifier, nb);
882 }
883 EXPORT_SYMBOL_GPL(bus_register_notifier);
884 
885 int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb)
886 {
887 	return blocking_notifier_chain_unregister(&bus->bus_notifier, nb);
888 }
889 EXPORT_SYMBOL_GPL(bus_unregister_notifier);
890 
891 int __init buses_init(void)
892 {
893 	return subsystem_register(&bus_subsys);
894 }
895 
896 
897 EXPORT_SYMBOL_GPL(bus_for_each_dev);
898 EXPORT_SYMBOL_GPL(bus_find_device);
899 EXPORT_SYMBOL_GPL(bus_for_each_drv);
900 
901 EXPORT_SYMBOL_GPL(bus_register);
902 EXPORT_SYMBOL_GPL(bus_unregister);
903 EXPORT_SYMBOL_GPL(bus_rescan_devices);
904 
905 EXPORT_SYMBOL_GPL(bus_create_file);
906 EXPORT_SYMBOL_GPL(bus_remove_file);
907