driver.c 6.4 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 19 20
 *
 * 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"

#define to_dev(node) container_of(node, struct device, driver_list)

21

22 23 24 25 26 27
static struct device * next_device(struct klist_iter * i)
{
	struct klist_node * n = klist_next(i);
	return n ? container_of(n, struct device, knode_driver) : NULL;
}

28 29 30
/**
 *	driver_for_each_device - Iterator for devices bound to a driver.
 *	@drv:	Driver we're iterating.
31
 *	@start: Device to begin with
32 33 34
 *	@data:	Data to pass to the callback.
 *	@fn:	Function to call for each device.
 *
35
 *	Iterate over the @drv's list of devices calling @fn for each one.
36 37 38 39 40
 */

int driver_for_each_device(struct device_driver * drv, struct device * start, 
			   void * data, int (*fn)(struct device *, void *))
{
41
	struct klist_iter i;
42 43 44
	struct device * dev;
	int error = 0;

45 46 47
	if (!drv)
		return -EINVAL;

48
	klist_iter_init_node(&drv->p->klist_devices, &i,
49 50
			     start ? &start->knode_driver : NULL);
	while ((dev = next_device(&i)) && !error)
51
		error = fn(dev, data);
52
	klist_iter_exit(&i);
53 54 55
	return error;
}

56
EXPORT_SYMBOL_GPL(driver_for_each_device);
57 58


59 60
/**
 * driver_find_device - device iterator for locating a particular device.
61
 * @drv: The device's driver
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
 * @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.
 */
struct device * driver_find_device(struct device_driver *drv,
				   struct device * start, void * data,
				   int (*match)(struct device *, void *))
{
	struct klist_iter i;
	struct device *dev;

	if (!drv)
		return NULL;

84
	klist_iter_init_node(&drv->p->klist_devices, &i,
85 86 87 88 89 90 91 92 93
			     (start ? &start->knode_driver : NULL));
	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 已提交
94 95 96 97 98 99 100 101 102 103
/**
 *	driver_create_file - create sysfs file for driver.
 *	@drv:	driver.
 *	@attr:	driver attribute descriptor.
 */

int driver_create_file(struct device_driver * drv, struct driver_attribute * attr)
{
	int error;
	if (get_driver(drv)) {
104
		error = sysfs_create_file(&drv->p->kobj, &attr->attr);
L
Linus Torvalds 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
		put_driver(drv);
	} else
		error = -EINVAL;
	return error;
}


/**
 *	driver_remove_file - remove sysfs file for driver.
 *	@drv:	driver.
 *	@attr:	driver attribute descriptor.
 */

void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr)
{
	if (get_driver(drv)) {
121
		sysfs_remove_file(&drv->p->kobj, &attr->attr);
L
Linus Torvalds 已提交
122 123 124 125 126
		put_driver(drv);
	}
}


127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
/**
 * driver_add_kobj - add a kobject below the specified driver
 *
 * 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;

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

	if (!name)
		return -ENOMEM;

147
	return kobject_add(kobj, &drv->p->kobj, "%s", name);
148 149 150
}
EXPORT_SYMBOL_GPL(driver_add_kobj);

L
Linus Torvalds 已提交
151 152 153 154 155 156
/**
 *	get_driver - increment driver reference count.
 *	@drv:	driver.
 */
struct device_driver * get_driver(struct device_driver * drv)
{
157 158 159 160 161 162 163 164 165
	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 已提交
166 167 168 169 170 171 172 173 174
}


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

178 179 180 181 182 183 184 185
static int driver_add_groups(struct device_driver *drv,
			     struct attribute_group **groups)
{
	int error = 0;
	int i;

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

static void driver_remove_groups(struct device_driver *drv,
				 struct attribute_group **groups)
{
	int i;

	if (groups)
		for (i = 0; groups[i]; i++)
205
			sysfs_remove_group(&drv->p->kobj, groups[i]);
206 207 208
}


L
Linus Torvalds 已提交
209 210 211 212 213 214 215 216 217 218
/**
 *	driver_register - register driver with bus
 *	@drv:	driver to register
 *
 *	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.
 */
int driver_register(struct device_driver * drv)
{
219 220
	int ret;

221 222 223 224 225
	if ((drv->bus->probe && drv->probe) ||
	    (drv->bus->remove && drv->remove) ||
	    (drv->bus->shutdown && drv->shutdown)) {
		printk(KERN_WARNING "Driver '%s' needs updating - please use bus_type methods\n", drv->name);
	}
226 227 228 229 230 231 232
	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 已提交
233 234 235 236 237 238 239 240 241 242 243
}

/**
 *	driver_unregister - remove driver from system.
 *	@drv:	driver.
 *
 *	Again, we pass off most of the work to the bus-level call.
 */

void driver_unregister(struct device_driver * drv)
{
244
	driver_remove_groups(drv, drv->groups);
L
Linus Torvalds 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
	bus_remove_driver(drv);
}

/**
 *	driver_find - locate driver on a bus by its name.
 *	@name:	name of the driver.
 *	@bus:	bus to scan for the driver.
 *
 *	Call kset_find_obj() to iterate over list of drivers on
 *	a bus to find driver by name. Return driver if found.
 *
 *	Note that kset_find_obj increments driver's reference count.
 */
struct device_driver *driver_find(const char *name, struct bus_type *bus)
{
260
	struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
261 262 263 264 265 266
	struct driver_private *priv;

	if (k) {
		priv = to_driver(k);
		return priv->driver;
	}
L
Linus Torvalds 已提交
267 268 269 270 271 272 273 274 275 276 277
	return NULL;
}

EXPORT_SYMBOL_GPL(driver_register);
EXPORT_SYMBOL_GPL(driver_unregister);
EXPORT_SYMBOL_GPL(get_driver);
EXPORT_SYMBOL_GPL(put_driver);
EXPORT_SYMBOL_GPL(driver_find);

EXPORT_SYMBOL_GPL(driver_create_file);
EXPORT_SYMBOL_GPL(driver_remove_file);