driver.c 6.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 * driver.c - centralized device driver management
 *
 * Copyright (c) 2002-3 Patrick Mochel
 * Copyright (c) 2002-3 Open Source Development Labs
6 7
 * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
 * Copyright (c) 2007 Novell Inc.
L
Linus Torvalds 已提交
8 9 10 11 12 13 14 15 16 17 18
 *
 * This file is released under the GPLv2
 *
 */

#include <linux/device.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/string.h>
#include "base.h"

19
static struct device *next_device(struct klist_iter *i)
20
{
21
	struct klist_node *n = klist_next(i);
22 23 24 25 26 27 28 29
	struct device *dev = NULL;
	struct device_private *dev_prv;

	if (n) {
		dev_prv = to_device_private_driver(n);
		dev = dev_prv->device;
	}
	return dev;
30 31
}

32
/**
33 34 35 36 37
 * driver_for_each_device - Iterator for devices bound to a driver.
 * @drv: Driver we're iterating.
 * @start: Device to begin with
 * @data: Data to pass to the callback.
 * @fn: Function to call for each device.
38
 *
39
 * Iterate over the @drv's list of devices calling @fn for each one.
40
 */
41 42
int driver_for_each_device(struct device_driver *drv, struct device *start,
			   void *data, int (*fn)(struct device *, void *))
43
{
44
	struct klist_iter i;
45
	struct device *dev;
46 47
	int error = 0;

48 49 50
	if (!drv)
		return -EINVAL;

51
	klist_iter_init_node(&drv->p->klist_devices, &i,
52
			     start ? &start->p->knode_driver : NULL);
53
	while ((dev = next_device(&i)) && !error)
54
		error = fn(dev, data);
55
	klist_iter_exit(&i);
56 57
	return error;
}
58
EXPORT_SYMBOL_GPL(driver_for_each_device);
59

60 61
/**
 * driver_find_device - device iterator for locating a particular device.
62
 * @drv: The device's driver
63 64 65 66 67 68 69 70 71 72 73 74
 * @start: Device to begin with
 * @data: Data to pass to match function
 * @match: Callback function to check device
 *
 * This is similar to the driver_for_each_device() function above, but
 * it returns a reference to a device that is 'found' for later use, as
 * determined by the @match callback.
 *
 * The callback should return 0 if the device doesn't match and non-zero
 * if it does.  If the callback returns non-zero, this function will
 * return to the caller and not iterate over any more devices.
 */
75 76 77
struct device *driver_find_device(struct device_driver *drv,
				  struct device *start, void *data,
				  int (*match)(struct device *dev, void *data))
78 79 80 81 82 83 84
{
	struct klist_iter i;
	struct device *dev;

	if (!drv)
		return NULL;

85
	klist_iter_init_node(&drv->p->klist_devices, &i,
86
			     (start ? &start->p->knode_driver : NULL));
87 88 89 90 91 92 93 94
	while ((dev = next_device(&i)))
		if (match(dev, data) && get_device(dev))
			break;
	klist_iter_exit(&i);
	return dev;
}
EXPORT_SYMBOL_GPL(driver_find_device);

L
Linus Torvalds 已提交
95
/**
96 97 98
 * driver_create_file - create sysfs file for driver.
 * @drv: driver.
 * @attr: driver attribute descriptor.
L
Linus Torvalds 已提交
99
 */
100
int driver_create_file(struct device_driver *drv,
101
		       const struct driver_attribute *attr)
L
Linus Torvalds 已提交
102 103
{
	int error;
104
	if (drv)
105
		error = sysfs_create_file(&drv->p->kobj, &attr->attr);
106
	else
L
Linus Torvalds 已提交
107 108 109
		error = -EINVAL;
	return error;
}
110
EXPORT_SYMBOL_GPL(driver_create_file);
L
Linus Torvalds 已提交
111 112

/**
113 114 115
 * driver_remove_file - remove sysfs file for driver.
 * @drv: driver.
 * @attr: driver attribute descriptor.
L
Linus Torvalds 已提交
116
 */
117
void driver_remove_file(struct device_driver *drv,
118
			const struct driver_attribute *attr)
L
Linus Torvalds 已提交
119
{
120
	if (drv)
121
		sysfs_remove_file(&drv->p->kobj, &attr->attr);
L
Linus Torvalds 已提交
122
}
123
EXPORT_SYMBOL_GPL(driver_remove_file);
L
Linus Torvalds 已提交
124

125 126
/**
 * driver_add_kobj - add a kobject below the specified driver
127 128 129
 * @drv: requesting device driver
 * @kobj: kobject to add below this driver
 * @fmt: format string that names the kobject
130 131 132 133 134 135 136 137 138 139
 *
 * You really don't want to do this, this is only here due to one looney
 * iseries driver, go poke those developers if you are annoyed about
 * this...
 */
int driver_add_kobj(struct device_driver *drv, struct kobject *kobj,
		    const char *fmt, ...)
{
	va_list args;
	char *name;
140
	int ret;
141 142 143 144 145 146 147 148

	va_start(args, fmt);
	name = kvasprintf(GFP_KERNEL, fmt, args);
	va_end(args);

	if (!name)
		return -ENOMEM;

149 150 151
	ret = kobject_add(kobj, &drv->p->kobj, "%s", name);
	kfree(name);
	return ret;
152 153 154
}
EXPORT_SYMBOL_GPL(driver_add_kobj);

L
Linus Torvalds 已提交
155
/**
156 157
 * get_driver - increment driver reference count.
 * @drv: driver.
L
Linus Torvalds 已提交
158
 */
159
struct device_driver *get_driver(struct device_driver *drv)
L
Linus Torvalds 已提交
160
{
161 162 163 164 165 166 167 168 169
	if (drv) {
		struct driver_private *priv;
		struct kobject *kobj;

		kobj = kobject_get(&drv->p->kobj);
		priv = to_driver(kobj);
		return priv->driver;
	}
	return NULL;
L
Linus Torvalds 已提交
170
}
171
EXPORT_SYMBOL_GPL(get_driver);
L
Linus Torvalds 已提交
172 173

/**
174 175
 * put_driver - decrement driver's refcount.
 * @drv: driver.
L
Linus Torvalds 已提交
176
 */
177
void put_driver(struct device_driver *drv)
L
Linus Torvalds 已提交
178
{
179
	kobject_put(&drv->p->kobj);
L
Linus Torvalds 已提交
180
}
181
EXPORT_SYMBOL_GPL(put_driver);
L
Linus Torvalds 已提交
182

183
static int driver_add_groups(struct device_driver *drv,
184
			     const struct attribute_group **groups)
185 186 187 188 189 190
{
	int error = 0;
	int i;

	if (groups) {
		for (i = 0; groups[i]; i++) {
191
			error = sysfs_create_group(&drv->p->kobj, groups[i]);
192 193
			if (error) {
				while (--i >= 0)
194
					sysfs_remove_group(&drv->p->kobj,
195 196 197 198 199 200 201 202 203
							   groups[i]);
				break;
			}
		}
	}
	return error;
}

static void driver_remove_groups(struct device_driver *drv,
204
				 const struct attribute_group **groups)
205 206 207 208 209
{
	int i;

	if (groups)
		for (i = 0; groups[i]; i++)
210
			sysfs_remove_group(&drv->p->kobj, groups[i]);
211 212
}

L
Linus Torvalds 已提交
213
/**
214 215
 * driver_register - register driver with bus
 * @drv: driver to register
L
Linus Torvalds 已提交
216
 *
217 218 219
 * We pass off most of the work to the bus_add_driver() call,
 * since most of the things we have to do deal with the bus
 * structures.
L
Linus Torvalds 已提交
220
 */
221
int driver_register(struct device_driver *drv)
L
Linus Torvalds 已提交
222
{
223
	int ret;
224
	struct device_driver *other;
225

226 227
	BUG_ON(!drv->bus->p);

228 229
	if ((drv->bus->probe && drv->probe) ||
	    (drv->bus->remove && drv->remove) ||
230 231 232
	    (drv->bus->shutdown && drv->shutdown))
		printk(KERN_WARNING "Driver '%s' needs updating - please use "
			"bus_type methods\n", drv->name);
233 234 235 236 237 238

	other = driver_find(drv->name, drv->bus);
	if (other) {
		put_driver(other);
		printk(KERN_ERR "Error: Driver '%s' is already registered, "
			"aborting...\n", drv->name);
239
		return -EBUSY;
240 241
	}

242 243 244 245 246 247 248
	ret = bus_add_driver(drv);
	if (ret)
		return ret;
	ret = driver_add_groups(drv, drv->groups);
	if (ret)
		bus_remove_driver(drv);
	return ret;
L
Linus Torvalds 已提交
249
}
250
EXPORT_SYMBOL_GPL(driver_register);
L
Linus Torvalds 已提交
251 252

/**
253 254
 * driver_unregister - remove driver from system.
 * @drv: driver.
L
Linus Torvalds 已提交
255
 *
256
 * Again, we pass off most of the work to the bus-level call.
L
Linus Torvalds 已提交
257
 */
258
void driver_unregister(struct device_driver *drv)
L
Linus Torvalds 已提交
259
{
260 261 262 263
	if (!drv || !drv->p) {
		WARN(1, "Unexpected driver unregister!\n");
		return;
	}
264
	driver_remove_groups(drv, drv->groups);
L
Linus Torvalds 已提交
265 266
	bus_remove_driver(drv);
}
267
EXPORT_SYMBOL_GPL(driver_unregister);
L
Linus Torvalds 已提交
268 269

/**
270 271 272
 * driver_find - locate driver on a bus by its name.
 * @name: name of the driver.
 * @bus: bus to scan for the driver.
L
Linus Torvalds 已提交
273
 *
274 275
 * Call kset_find_obj() to iterate over list of drivers on
 * a bus to find driver by name. Return driver if found.
L
Linus Torvalds 已提交
276
 *
277
 * Note that kset_find_obj increments driver's reference count.
L
Linus Torvalds 已提交
278 279 280
 */
struct device_driver *driver_find(const char *name, struct bus_type *bus)
{
281
	struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
282 283 284 285 286 287
	struct driver_private *priv;

	if (k) {
		priv = to_driver(k);
		return priv->driver;
	}
L
Linus Torvalds 已提交
288 289 290
	return NULL;
}
EXPORT_SYMBOL_GPL(driver_find);