xref: /linux/drivers/base/bus.c (revision 9ce7677cfd7cd871adb457c80bea3b581b839641)
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/config.h>
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/string.h>
17 #include "base.h"
18 #include "power/power.h"
19 
20 #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
21 #define to_bus(obj) container_of(obj, struct bus_type, subsys.kset.kobj)
22 
23 /*
24  * sysfs bindings for drivers
25  */
26 
27 #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
28 #define to_driver(obj) container_of(obj, struct device_driver, kobj)
29 
30 
31 static ssize_t
32 drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
33 {
34 	struct driver_attribute * drv_attr = to_drv_attr(attr);
35 	struct device_driver * drv = to_driver(kobj);
36 	ssize_t ret = -EIO;
37 
38 	if (drv_attr->show)
39 		ret = drv_attr->show(drv, buf);
40 	return ret;
41 }
42 
43 static ssize_t
44 drv_attr_store(struct kobject * kobj, struct attribute * attr,
45 	       const char * buf, size_t count)
46 {
47 	struct driver_attribute * drv_attr = to_drv_attr(attr);
48 	struct device_driver * drv = to_driver(kobj);
49 	ssize_t ret = -EIO;
50 
51 	if (drv_attr->store)
52 		ret = drv_attr->store(drv, buf, count);
53 	return ret;
54 }
55 
56 static struct sysfs_ops driver_sysfs_ops = {
57 	.show	= drv_attr_show,
58 	.store	= drv_attr_store,
59 };
60 
61 
62 static void driver_release(struct kobject * kobj)
63 {
64 	struct device_driver * drv = to_driver(kobj);
65 	complete(&drv->unloaded);
66 }
67 
68 static struct kobj_type ktype_driver = {
69 	.sysfs_ops	= &driver_sysfs_ops,
70 	.release	= driver_release,
71 };
72 
73 
74 /*
75  * sysfs bindings for buses
76  */
77 
78 
79 static ssize_t
80 bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
81 {
82 	struct bus_attribute * bus_attr = to_bus_attr(attr);
83 	struct bus_type * bus = to_bus(kobj);
84 	ssize_t ret = 0;
85 
86 	if (bus_attr->show)
87 		ret = bus_attr->show(bus, buf);
88 	return ret;
89 }
90 
91 static ssize_t
92 bus_attr_store(struct kobject * kobj, struct attribute * attr,
93 	       const char * buf, size_t count)
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->store)
100 		ret = bus_attr->store(bus, buf, count);
101 	return ret;
102 }
103 
104 static struct sysfs_ops bus_sysfs_ops = {
105 	.show	= bus_attr_show,
106 	.store	= bus_attr_store,
107 };
108 
109 int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
110 {
111 	int error;
112 	if (get_bus(bus)) {
113 		error = sysfs_create_file(&bus->subsys.kset.kobj, &attr->attr);
114 		put_bus(bus);
115 	} else
116 		error = -EINVAL;
117 	return error;
118 }
119 
120 void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
121 {
122 	if (get_bus(bus)) {
123 		sysfs_remove_file(&bus->subsys.kset.kobj, &attr->attr);
124 		put_bus(bus);
125 	}
126 }
127 
128 static struct kobj_type ktype_bus = {
129 	.sysfs_ops	= &bus_sysfs_ops,
130 
131 };
132 
133 decl_subsys(bus, &ktype_bus, NULL);
134 
135 
136 /* Manually detach a device from its associated driver. */
137 static int driver_helper(struct device *dev, void *data)
138 {
139 	const char *name = data;
140 
141 	if (strcmp(name, dev->bus_id) == 0)
142 		return 1;
143 	return 0;
144 }
145 
146 static ssize_t driver_unbind(struct device_driver *drv,
147 			     const char *buf, size_t count)
148 {
149 	struct bus_type *bus = get_bus(drv->bus);
150 	struct device *dev;
151 	int err = -ENODEV;
152 
153 	dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
154 	if (dev && dev->driver == drv) {
155 		device_release_driver(dev);
156 		err = count;
157 	}
158 	put_device(dev);
159 	put_bus(bus);
160 	return err;
161 }
162 static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind);
163 
164 /*
165  * Manually attach a device to a driver.
166  * Note: the driver must want to bind to the device,
167  * it is not possible to override the driver's id table.
168  */
169 static ssize_t driver_bind(struct device_driver *drv,
170 			   const char *buf, size_t count)
171 {
172 	struct bus_type *bus = get_bus(drv->bus);
173 	struct device *dev;
174 	int err = -ENODEV;
175 
176 	dev = bus_find_device(bus, NULL, (void *)buf, driver_helper);
177 	if (dev && dev->driver == NULL) {
178 		down(&dev->sem);
179 		err = driver_probe_device(drv, dev);
180 		up(&dev->sem);
181 	}
182 	put_device(dev);
183 	put_bus(bus);
184 	return err;
185 }
186 static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind);
187 
188 
189 static struct device * next_device(struct klist_iter * i)
190 {
191 	struct klist_node * n = klist_next(i);
192 	return n ? container_of(n, struct device, knode_bus) : NULL;
193 }
194 
195 /**
196  *	bus_for_each_dev - device iterator.
197  *	@bus:	bus type.
198  *	@start:	device to start iterating from.
199  *	@data:	data for the callback.
200  *	@fn:	function to be called for each device.
201  *
202  *	Iterate over @bus's list of devices, and call @fn for each,
203  *	passing it @data. If @start is not NULL, we use that device to
204  *	begin iterating from.
205  *
206  *	We check the return of @fn each time. If it returns anything
207  *	other than 0, we break out and return that value.
208  *
209  *	NOTE: The device that returns a non-zero value is not retained
210  *	in any way, nor is its refcount incremented. If the caller needs
211  *	to retain this data, it should do, and increment the reference
212  *	count in the supplied callback.
213  */
214 
215 int bus_for_each_dev(struct bus_type * bus, struct device * start,
216 		     void * data, int (*fn)(struct device *, void *))
217 {
218 	struct klist_iter i;
219 	struct device * dev;
220 	int error = 0;
221 
222 	if (!bus)
223 		return -EINVAL;
224 
225 	klist_iter_init_node(&bus->klist_devices, &i,
226 			     (start ? &start->knode_bus : NULL));
227 	while ((dev = next_device(&i)) && !error)
228 		error = fn(dev, data);
229 	klist_iter_exit(&i);
230 	return error;
231 }
232 
233 /**
234  * bus_find_device - device iterator for locating a particular device.
235  * @bus: bus type
236  * @start: Device to begin with
237  * @data: Data to pass to match function
238  * @match: Callback function to check device
239  *
240  * This is similar to the bus_for_each_dev() function above, but it
241  * returns a reference to a device that is 'found' for later use, as
242  * determined by the @match callback.
243  *
244  * The callback should return 0 if the device doesn't match and non-zero
245  * if it does.  If the callback returns non-zero, this function will
246  * return to the caller and not iterate over any more devices.
247  */
248 struct device * bus_find_device(struct bus_type *bus,
249 				struct device *start, void *data,
250 				int (*match)(struct device *, void *))
251 {
252 	struct klist_iter i;
253 	struct device *dev;
254 
255 	if (!bus)
256 		return NULL;
257 
258 	klist_iter_init_node(&bus->klist_devices, &i,
259 			     (start ? &start->knode_bus : NULL));
260 	while ((dev = next_device(&i)))
261 		if (match(dev, data) && get_device(dev))
262 			break;
263 	klist_iter_exit(&i);
264 	return dev;
265 }
266 
267 
268 static struct device_driver * next_driver(struct klist_iter * i)
269 {
270 	struct klist_node * n = klist_next(i);
271 	return n ? container_of(n, struct device_driver, knode_bus) : NULL;
272 }
273 
274 /**
275  *	bus_for_each_drv - driver iterator
276  *	@bus:	bus we're dealing with.
277  *	@start:	driver to start iterating on.
278  *	@data:	data to pass to the callback.
279  *	@fn:	function to call for each driver.
280  *
281  *	This is nearly identical to the device iterator above.
282  *	We iterate over each driver that belongs to @bus, and call
283  *	@fn for each. If @fn returns anything but 0, we break out
284  *	and return it. If @start is not NULL, we use it as the head
285  *	of the list.
286  *
287  *	NOTE: we don't return the driver that returns a non-zero
288  *	value, nor do we leave the reference count incremented for that
289  *	driver. If the caller needs to know that info, it must set it
290  *	in the callback. It must also be sure to increment the refcount
291  *	so it doesn't disappear before returning to the caller.
292  */
293 
294 int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
295 		     void * data, int (*fn)(struct device_driver *, void *))
296 {
297 	struct klist_iter i;
298 	struct device_driver * drv;
299 	int error = 0;
300 
301 	if (!bus)
302 		return -EINVAL;
303 
304 	klist_iter_init_node(&bus->klist_drivers, &i,
305 			     start ? &start->knode_bus : NULL);
306 	while ((drv = next_driver(&i)) && !error)
307 		error = fn(drv, data);
308 	klist_iter_exit(&i);
309 	return error;
310 }
311 
312 static int device_add_attrs(struct bus_type * bus, struct device * dev)
313 {
314 	int error = 0;
315 	int i;
316 
317 	if (bus->dev_attrs) {
318 		for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
319 			error = device_create_file(dev,&bus->dev_attrs[i]);
320 			if (error)
321 				goto Err;
322 		}
323 	}
324  Done:
325 	return error;
326  Err:
327 	while (--i >= 0)
328 		device_remove_file(dev,&bus->dev_attrs[i]);
329 	goto Done;
330 }
331 
332 
333 static void device_remove_attrs(struct bus_type * bus, struct device * dev)
334 {
335 	int i;
336 
337 	if (bus->dev_attrs) {
338 		for (i = 0; attr_name(bus->dev_attrs[i]); i++)
339 			device_remove_file(dev,&bus->dev_attrs[i]);
340 	}
341 }
342 
343 
344 /**
345  *	bus_add_device - add device to bus
346  *	@dev:	device being added
347  *
348  *	- Add the device to its bus's list of devices.
349  *	- Try to attach to driver.
350  *	- Create link to device's physical location.
351  */
352 int bus_add_device(struct device * dev)
353 {
354 	struct bus_type * bus = get_bus(dev->bus);
355 	int error = 0;
356 
357 	if (bus) {
358 		pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
359 		device_attach(dev);
360 		klist_add_tail(&dev->knode_bus, &bus->klist_devices);
361 		error = device_add_attrs(bus, dev);
362 		if (!error) {
363 			sysfs_create_link(&bus->devices.kobj, &dev->kobj, dev->bus_id);
364 			sysfs_create_link(&dev->kobj, &dev->bus->subsys.kset.kobj, "bus");
365 		}
366 	}
367 	return error;
368 }
369 
370 /**
371  *	bus_remove_device - remove device from bus
372  *	@dev:	device to be removed
373  *
374  *	- Remove symlink from bus's directory.
375  *	- Delete device from bus's list.
376  *	- Detach from its driver.
377  *	- Drop reference taken in bus_add_device().
378  */
379 void bus_remove_device(struct device * dev)
380 {
381 	if (dev->bus) {
382 		sysfs_remove_link(&dev->kobj, "bus");
383 		sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
384 		device_remove_attrs(dev->bus, dev);
385 		klist_remove(&dev->knode_bus);
386 		pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
387 		device_release_driver(dev);
388 		put_bus(dev->bus);
389 	}
390 }
391 
392 static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
393 {
394 	int error = 0;
395 	int i;
396 
397 	if (bus->drv_attrs) {
398 		for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
399 			error = driver_create_file(drv, &bus->drv_attrs[i]);
400 			if (error)
401 				goto Err;
402 		}
403 	}
404  Done:
405 	return error;
406  Err:
407 	while (--i >= 0)
408 		driver_remove_file(drv, &bus->drv_attrs[i]);
409 	goto Done;
410 }
411 
412 
413 static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
414 {
415 	int i;
416 
417 	if (bus->drv_attrs) {
418 		for (i = 0; attr_name(bus->drv_attrs[i]); i++)
419 			driver_remove_file(drv, &bus->drv_attrs[i]);
420 	}
421 }
422 
423 
424 /**
425  *	bus_add_driver - Add a driver to the bus.
426  *	@drv:	driver.
427  *
428  */
429 int bus_add_driver(struct device_driver * drv)
430 {
431 	struct bus_type * bus = get_bus(drv->bus);
432 	int error = 0;
433 
434 	if (bus) {
435 		pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
436 		error = kobject_set_name(&drv->kobj, "%s", drv->name);
437 		if (error) {
438 			put_bus(bus);
439 			return error;
440 		}
441 		drv->kobj.kset = &bus->drivers;
442 		if ((error = kobject_register(&drv->kobj))) {
443 			put_bus(bus);
444 			return error;
445 		}
446 
447 		driver_attach(drv);
448 		klist_add_tail(&drv->knode_bus, &bus->klist_drivers);
449 		module_add_driver(drv->owner, drv);
450 
451 		driver_add_attrs(bus, drv);
452 		driver_create_file(drv, &driver_attr_unbind);
453 		driver_create_file(drv, &driver_attr_bind);
454 	}
455 	return error;
456 }
457 
458 
459 /**
460  *	bus_remove_driver - delete driver from bus's knowledge.
461  *	@drv:	driver.
462  *
463  *	Detach the driver from the devices it controls, and remove
464  *	it from its bus's list of drivers. Finally, we drop the reference
465  *	to the bus we took in bus_add_driver().
466  */
467 
468 void bus_remove_driver(struct device_driver * drv)
469 {
470 	if (drv->bus) {
471 		driver_remove_file(drv, &driver_attr_bind);
472 		driver_remove_file(drv, &driver_attr_unbind);
473 		driver_remove_attrs(drv->bus, drv);
474 		klist_remove(&drv->knode_bus);
475 		pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
476 		driver_detach(drv);
477 		module_remove_driver(drv);
478 		kobject_unregister(&drv->kobj);
479 		put_bus(drv->bus);
480 	}
481 }
482 
483 
484 /* Helper for bus_rescan_devices's iter */
485 static int bus_rescan_devices_helper(struct device *dev, void *data)
486 {
487 	if (!dev->driver)
488 		device_attach(dev);
489 	return 0;
490 }
491 
492 /**
493  * bus_rescan_devices - rescan devices on the bus for possible drivers
494  * @bus: the bus to scan.
495  *
496  * This function will look for devices on the bus with no driver
497  * attached and rescan it against existing drivers to see if it matches
498  * any by calling device_attach() for the unbound devices.
499  */
500 void bus_rescan_devices(struct bus_type * bus)
501 {
502 	bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper);
503 }
504 
505 
506 struct bus_type * get_bus(struct bus_type * bus)
507 {
508 	return bus ? container_of(subsys_get(&bus->subsys), struct bus_type, subsys) : NULL;
509 }
510 
511 void put_bus(struct bus_type * bus)
512 {
513 	subsys_put(&bus->subsys);
514 }
515 
516 
517 /**
518  *	find_bus - locate bus by name.
519  *	@name:	name of bus.
520  *
521  *	Call kset_find_obj() to iterate over list of buses to
522  *	find a bus by name. Return bus if found.
523  *
524  *	Note that kset_find_obj increments bus' reference count.
525  */
526 
527 struct bus_type * find_bus(char * name)
528 {
529 	struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
530 	return k ? to_bus(k) : NULL;
531 }
532 
533 
534 /**
535  *	bus_add_attrs - Add default attributes for this bus.
536  *	@bus:	Bus that has just been registered.
537  */
538 
539 static int bus_add_attrs(struct bus_type * bus)
540 {
541 	int error = 0;
542 	int i;
543 
544 	if (bus->bus_attrs) {
545 		for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
546 			if ((error = bus_create_file(bus,&bus->bus_attrs[i])))
547 				goto Err;
548 		}
549 	}
550  Done:
551 	return error;
552  Err:
553 	while (--i >= 0)
554 		bus_remove_file(bus,&bus->bus_attrs[i]);
555 	goto Done;
556 }
557 
558 static void bus_remove_attrs(struct bus_type * bus)
559 {
560 	int i;
561 
562 	if (bus->bus_attrs) {
563 		for (i = 0; attr_name(bus->bus_attrs[i]); i++)
564 			bus_remove_file(bus,&bus->bus_attrs[i]);
565 	}
566 }
567 
568 static void klist_devices_get(struct klist_node *n)
569 {
570 	struct device *dev = container_of(n, struct device, knode_bus);
571 
572 	get_device(dev);
573 }
574 
575 static void klist_devices_put(struct klist_node *n)
576 {
577 	struct device *dev = container_of(n, struct device, knode_bus);
578 
579 	put_device(dev);
580 }
581 
582 static void klist_drivers_get(struct klist_node *n)
583 {
584 	struct device_driver *drv = container_of(n, struct device_driver,
585 						 knode_bus);
586 
587 	get_driver(drv);
588 }
589 
590 static void klist_drivers_put(struct klist_node *n)
591 {
592 	struct device_driver *drv = container_of(n, struct device_driver,
593 						 knode_bus);
594 
595 	put_driver(drv);
596 }
597 
598 /**
599  *	bus_register - register a bus with the system.
600  *	@bus:	bus.
601  *
602  *	Once we have that, we registered the bus with the kobject
603  *	infrastructure, then register the children subsystems it has:
604  *	the devices and drivers that belong to the bus.
605  */
606 int bus_register(struct bus_type * bus)
607 {
608 	int retval;
609 
610 	retval = kobject_set_name(&bus->subsys.kset.kobj, "%s", bus->name);
611 	if (retval)
612 		goto out;
613 
614 	subsys_set_kset(bus, bus_subsys);
615 	retval = subsystem_register(&bus->subsys);
616 	if (retval)
617 		goto out;
618 
619 	kobject_set_name(&bus->devices.kobj, "devices");
620 	bus->devices.subsys = &bus->subsys;
621 	retval = kset_register(&bus->devices);
622 	if (retval)
623 		goto bus_devices_fail;
624 
625 	kobject_set_name(&bus->drivers.kobj, "drivers");
626 	bus->drivers.subsys = &bus->subsys;
627 	bus->drivers.ktype = &ktype_driver;
628 	retval = kset_register(&bus->drivers);
629 	if (retval)
630 		goto bus_drivers_fail;
631 
632 	klist_init(&bus->klist_devices, klist_devices_get, klist_devices_put);
633 	klist_init(&bus->klist_drivers, klist_drivers_get, klist_drivers_put);
634 	bus_add_attrs(bus);
635 
636 	pr_debug("bus type '%s' registered\n", bus->name);
637 	return 0;
638 
639 bus_drivers_fail:
640 	kset_unregister(&bus->devices);
641 bus_devices_fail:
642 	subsystem_unregister(&bus->subsys);
643 out:
644 	return retval;
645 }
646 
647 
648 /**
649  *	bus_unregister - remove a bus from the system
650  *	@bus:	bus.
651  *
652  *	Unregister the child subsystems and the bus itself.
653  *	Finally, we call put_bus() to release the refcount
654  */
655 void bus_unregister(struct bus_type * bus)
656 {
657 	pr_debug("bus %s: unregistering\n", bus->name);
658 	bus_remove_attrs(bus);
659 	kset_unregister(&bus->drivers);
660 	kset_unregister(&bus->devices);
661 	subsystem_unregister(&bus->subsys);
662 }
663 
664 int __init buses_init(void)
665 {
666 	return subsystem_register(&bus_subsys);
667 }
668 
669 
670 EXPORT_SYMBOL_GPL(bus_for_each_dev);
671 EXPORT_SYMBOL_GPL(bus_find_device);
672 EXPORT_SYMBOL_GPL(bus_for_each_drv);
673 
674 EXPORT_SYMBOL_GPL(bus_add_device);
675 EXPORT_SYMBOL_GPL(bus_remove_device);
676 EXPORT_SYMBOL_GPL(bus_register);
677 EXPORT_SYMBOL_GPL(bus_unregister);
678 EXPORT_SYMBOL_GPL(bus_rescan_devices);
679 EXPORT_SYMBOL_GPL(get_bus);
680 EXPORT_SYMBOL_GPL(put_bus);
681 EXPORT_SYMBOL_GPL(find_bus);
682 
683 EXPORT_SYMBOL_GPL(bus_create_file);
684 EXPORT_SYMBOL_GPL(bus_remove_file);
685