| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * driver.c - centralized device 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/string.h> | 
 | 16 | #include "base.h" | 
 | 17 |  | 
 | 18 | #define to_dev(node) container_of(node, struct device, driver_list) | 
 | 19 | #define to_drv(obj) container_of(obj, struct device_driver, kobj) | 
 | 20 |  | 
| mochel@digitalimplant.org | fae3cd0 | 2005-03-21 10:59:56 -0800 | [diff] [blame] | 21 |  | 
| mochel@digitalimplant.org | 94e7b1c | 2005-03-21 12:25:36 -0800 | [diff] [blame] | 22 | static struct device * next_device(struct klist_iter * i) | 
 | 23 | { | 
 | 24 | 	struct klist_node * n = klist_next(i); | 
 | 25 | 	return n ? container_of(n, struct device, knode_driver) : NULL; | 
 | 26 | } | 
 | 27 |  | 
| mochel@digitalimplant.org | fae3cd0 | 2005-03-21 10:59:56 -0800 | [diff] [blame] | 28 | /** | 
 | 29 |  *	driver_for_each_device - Iterator for devices bound to a driver. | 
 | 30 |  *	@drv:	Driver we're iterating. | 
 | 31 |  *	@data:	Data to pass to the callback. | 
 | 32 |  *	@fn:	Function to call for each device. | 
 | 33 |  * | 
| mochel@digitalimplant.org | 4d12d2d | 2005-03-24 20:08:04 -0800 | [diff] [blame] | 34 |  *	Iterate over the @drv's list of devices calling @fn for each one. | 
| mochel@digitalimplant.org | fae3cd0 | 2005-03-21 10:59:56 -0800 | [diff] [blame] | 35 |  */ | 
 | 36 |  | 
 | 37 | int driver_for_each_device(struct device_driver * drv, struct device * start,  | 
 | 38 | 			   void * data, int (*fn)(struct device *, void *)) | 
 | 39 | { | 
| mochel@digitalimplant.org | 94e7b1c | 2005-03-21 12:25:36 -0800 | [diff] [blame] | 40 | 	struct klist_iter i; | 
| mochel@digitalimplant.org | fae3cd0 | 2005-03-21 10:59:56 -0800 | [diff] [blame] | 41 | 	struct device * dev; | 
 | 42 | 	int error = 0; | 
 | 43 |  | 
| mochel@digitalimplant.org | 94e7b1c | 2005-03-21 12:25:36 -0800 | [diff] [blame] | 44 | 	if (!drv) | 
 | 45 | 		return -EINVAL; | 
 | 46 |  | 
 | 47 | 	klist_iter_init_node(&drv->klist_devices, &i, | 
 | 48 | 			     start ? &start->knode_driver : NULL); | 
 | 49 | 	while ((dev = next_device(&i)) && !error) | 
| mochel@digitalimplant.org | fae3cd0 | 2005-03-21 10:59:56 -0800 | [diff] [blame] | 50 | 		error = fn(dev, data); | 
| mochel@digitalimplant.org | 94e7b1c | 2005-03-21 12:25:36 -0800 | [diff] [blame] | 51 | 	klist_iter_exit(&i); | 
| mochel@digitalimplant.org | fae3cd0 | 2005-03-21 10:59:56 -0800 | [diff] [blame] | 52 | 	return error; | 
 | 53 | } | 
 | 54 |  | 
| gregkh@suse.de | 126eddf | 2005-03-22 12:17:13 -0800 | [diff] [blame] | 55 | EXPORT_SYMBOL_GPL(driver_for_each_device); | 
| mochel@digitalimplant.org | fae3cd0 | 2005-03-21 10:59:56 -0800 | [diff] [blame] | 56 |  | 
 | 57 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | /** | 
 | 59 |  *	driver_create_file - create sysfs file for driver. | 
 | 60 |  *	@drv:	driver. | 
 | 61 |  *	@attr:	driver attribute descriptor. | 
 | 62 |  */ | 
 | 63 |  | 
 | 64 | int driver_create_file(struct device_driver * drv, struct driver_attribute * attr) | 
 | 65 | { | 
 | 66 | 	int error; | 
 | 67 | 	if (get_driver(drv)) { | 
 | 68 | 		error = sysfs_create_file(&drv->kobj, &attr->attr); | 
 | 69 | 		put_driver(drv); | 
 | 70 | 	} else | 
 | 71 | 		error = -EINVAL; | 
 | 72 | 	return error; | 
 | 73 | } | 
 | 74 |  | 
 | 75 |  | 
 | 76 | /** | 
 | 77 |  *	driver_remove_file - remove sysfs file for driver. | 
 | 78 |  *	@drv:	driver. | 
 | 79 |  *	@attr:	driver attribute descriptor. | 
 | 80 |  */ | 
 | 81 |  | 
 | 82 | void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr) | 
 | 83 | { | 
 | 84 | 	if (get_driver(drv)) { | 
 | 85 | 		sysfs_remove_file(&drv->kobj, &attr->attr); | 
 | 86 | 		put_driver(drv); | 
 | 87 | 	} | 
 | 88 | } | 
 | 89 |  | 
 | 90 |  | 
 | 91 | /** | 
 | 92 |  *	get_driver - increment driver reference count. | 
 | 93 |  *	@drv:	driver. | 
 | 94 |  */ | 
 | 95 | struct device_driver * get_driver(struct device_driver * drv) | 
 | 96 | { | 
 | 97 | 	return drv ? to_drv(kobject_get(&drv->kobj)) : NULL; | 
 | 98 | } | 
 | 99 |  | 
 | 100 |  | 
 | 101 | /** | 
 | 102 |  *	put_driver - decrement driver's refcount. | 
 | 103 |  *	@drv:	driver. | 
 | 104 |  */ | 
 | 105 | void put_driver(struct device_driver * drv) | 
 | 106 | { | 
 | 107 | 	kobject_put(&drv->kobj); | 
 | 108 | } | 
 | 109 |  | 
 | 110 |  | 
 | 111 | /** | 
 | 112 |  *	driver_register - register driver with bus | 
 | 113 |  *	@drv:	driver to register | 
 | 114 |  * | 
 | 115 |  *	We pass off most of the work to the bus_add_driver() call, | 
 | 116 |  *	since most of the things we have to do deal with the bus | 
 | 117 |  *	structures. | 
 | 118 |  * | 
 | 119 |  *	The one interesting aspect is that we setup @drv->unloaded | 
 | 120 |  *	as a completion that gets complete when the driver reference | 
 | 121 |  *	count reaches 0. | 
 | 122 |  */ | 
 | 123 | int driver_register(struct device_driver * drv) | 
 | 124 | { | 
| mochel@digitalimplant.org | 94e7b1c | 2005-03-21 12:25:36 -0800 | [diff] [blame] | 125 | 	klist_init(&drv->klist_devices); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | 	init_completion(&drv->unloaded); | 
 | 127 | 	return bus_add_driver(drv); | 
 | 128 | } | 
 | 129 |  | 
 | 130 |  | 
 | 131 | /** | 
 | 132 |  *	driver_unregister - remove driver from system. | 
 | 133 |  *	@drv:	driver. | 
 | 134 |  * | 
 | 135 |  *	Again, we pass off most of the work to the bus-level call. | 
 | 136 |  * | 
 | 137 |  *	Though, once that is done, we wait until @drv->unloaded is completed. | 
 | 138 |  *	This will block until the driver refcount reaches 0, and it is | 
 | 139 |  *	released. Only modular drivers will call this function, and we | 
 | 140 |  *	have to guarantee that it won't complete, letting the driver | 
 | 141 |  *	unload until all references are gone. | 
 | 142 |  */ | 
 | 143 |  | 
 | 144 | void driver_unregister(struct device_driver * drv) | 
 | 145 | { | 
 | 146 | 	bus_remove_driver(drv); | 
 | 147 | 	wait_for_completion(&drv->unloaded); | 
 | 148 | } | 
 | 149 |  | 
 | 150 | /** | 
 | 151 |  *	driver_find - locate driver on a bus by its name. | 
 | 152 |  *	@name:	name of the driver. | 
 | 153 |  *	@bus:	bus to scan for the driver. | 
 | 154 |  * | 
 | 155 |  *	Call kset_find_obj() to iterate over list of drivers on | 
 | 156 |  *	a bus to find driver by name. Return driver if found. | 
 | 157 |  * | 
 | 158 |  *	Note that kset_find_obj increments driver's reference count. | 
 | 159 |  */ | 
 | 160 | struct device_driver *driver_find(const char *name, struct bus_type *bus) | 
 | 161 | { | 
 | 162 | 	struct kobject *k = kset_find_obj(&bus->drivers, name); | 
 | 163 | 	if (k) | 
 | 164 | 		return to_drv(k); | 
 | 165 | 	return NULL; | 
 | 166 | } | 
 | 167 |  | 
 | 168 | EXPORT_SYMBOL_GPL(driver_register); | 
 | 169 | EXPORT_SYMBOL_GPL(driver_unregister); | 
 | 170 | EXPORT_SYMBOL_GPL(get_driver); | 
 | 171 | EXPORT_SYMBOL_GPL(put_driver); | 
 | 172 | EXPORT_SYMBOL_GPL(driver_find); | 
 | 173 |  | 
 | 174 | EXPORT_SYMBOL_GPL(driver_create_file); | 
 | 175 | EXPORT_SYMBOL_GPL(driver_remove_file); |