2005-04-16 15:20:36 -07:00
|
|
|
/*
|
|
|
|
* driver.c - centralized device driver management
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002-3 Patrick Mochel
|
|
|
|
* Copyright (c) 2002-3 Open Source Development Labs
|
2007-11-28 16:59:15 -07:00
|
|
|
* Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
|
|
|
|
* Copyright (c) 2007 Novell Inc.
|
2005-04-16 15:20:36 -07:00
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
|
2005-03-21 11:59:56 -07:00
|
|
|
|
2008-01-24 23:50:12 -07:00
|
|
|
static struct device *next_device(struct klist_iter *i)
|
2005-03-21 13:25:36 -07:00
|
|
|
{
|
2008-01-24 23:50:12 -07:00
|
|
|
struct klist_node *n = klist_next(i);
|
2005-03-21 13:25:36 -07:00
|
|
|
return n ? container_of(n, struct device, knode_driver) : NULL;
|
|
|
|
}
|
|
|
|
|
2005-03-21 11:59:56 -07:00
|
|
|
/**
|
2008-01-24 23:50:12 -07:00
|
|
|
* 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.
|
2005-03-21 11:59:56 -07:00
|
|
|
*
|
2008-01-24 23:50:12 -07:00
|
|
|
* Iterate over the @drv's list of devices calling @fn for each one.
|
2005-03-21 11:59:56 -07:00
|
|
|
*/
|
2008-01-24 23:50:12 -07:00
|
|
|
int driver_for_each_device(struct device_driver *drv, struct device *start,
|
|
|
|
void *data, int (*fn)(struct device *, void *))
|
2005-03-21 11:59:56 -07:00
|
|
|
{
|
2005-03-21 13:25:36 -07:00
|
|
|
struct klist_iter i;
|
2008-01-24 23:50:12 -07:00
|
|
|
struct device *dev;
|
2005-03-21 11:59:56 -07:00
|
|
|
int error = 0;
|
|
|
|
|
2005-03-21 13:25:36 -07:00
|
|
|
if (!drv)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2007-11-28 16:59:15 -07:00
|
|
|
klist_iter_init_node(&drv->p->klist_devices, &i,
|
2005-03-21 13:25:36 -07:00
|
|
|
start ? &start->knode_driver : NULL);
|
|
|
|
while ((dev = next_device(&i)) && !error)
|
2005-03-21 11:59:56 -07:00
|
|
|
error = fn(dev, data);
|
2005-03-21 13:25:36 -07:00
|
|
|
klist_iter_exit(&i);
|
2005-03-21 11:59:56 -07:00
|
|
|
return error;
|
|
|
|
}
|
2005-03-22 13:17:13 -07:00
|
|
|
EXPORT_SYMBOL_GPL(driver_for_each_device);
|
2005-03-21 11:59:56 -07:00
|
|
|
|
2005-06-22 07:59:51 -07:00
|
|
|
/**
|
|
|
|
* driver_find_device - device iterator for locating a particular device.
|
2005-10-23 11:59:14 -07:00
|
|
|
* @drv: The device's driver
|
2005-06-22 07:59:51 -07:00
|
|
|
* @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.
|
|
|
|
*/
|
2008-01-24 23:50:12 -07:00
|
|
|
struct device *driver_find_device(struct device_driver *drv,
|
|
|
|
struct device *start, void *data,
|
|
|
|
int (*match)(struct device *dev, void *data))
|
2005-06-22 07:59:51 -07:00
|
|
|
{
|
|
|
|
struct klist_iter i;
|
|
|
|
struct device *dev;
|
|
|
|
|
|
|
|
if (!drv)
|
|
|
|
return NULL;
|
|
|
|
|
2007-11-28 16:59:15 -07:00
|
|
|
klist_iter_init_node(&drv->p->klist_devices, &i,
|
2005-06-22 07:59:51 -07:00
|
|
|
(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);
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
/**
|
2008-01-24 23:50:12 -07:00
|
|
|
* driver_create_file - create sysfs file for driver.
|
|
|
|
* @drv: driver.
|
|
|
|
* @attr: driver attribute descriptor.
|
2005-04-16 15:20:36 -07:00
|
|
|
*/
|
2008-01-24 23:50:12 -07:00
|
|
|
int driver_create_file(struct device_driver *drv,
|
|
|
|
struct driver_attribute *attr)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
|
|
|
int error;
|
2008-01-31 02:39:38 -07:00
|
|
|
if (drv)
|
2007-11-28 16:59:15 -07:00
|
|
|
error = sysfs_create_file(&drv->p->kobj, &attr->attr);
|
2008-01-31 02:39:38 -07:00
|
|
|
else
|
2005-04-16 15:20:36 -07:00
|
|
|
error = -EINVAL;
|
|
|
|
return error;
|
|
|
|
}
|
2008-01-24 23:50:12 -07:00
|
|
|
EXPORT_SYMBOL_GPL(driver_create_file);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
/**
|
2008-01-24 23:50:12 -07:00
|
|
|
* driver_remove_file - remove sysfs file for driver.
|
|
|
|
* @drv: driver.
|
|
|
|
* @attr: driver attribute descriptor.
|
2005-04-16 15:20:36 -07:00
|
|
|
*/
|
2008-01-24 23:50:12 -07:00
|
|
|
void driver_remove_file(struct device_driver *drv,
|
|
|
|
struct driver_attribute *attr)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2008-01-31 02:39:38 -07:00
|
|
|
if (drv)
|
2007-11-28 16:59:15 -07:00
|
|
|
sysfs_remove_file(&drv->p->kobj, &attr->attr);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2008-01-24 23:50:12 -07:00
|
|
|
EXPORT_SYMBOL_GPL(driver_remove_file);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2007-12-19 12:54:39 -07:00
|
|
|
/**
|
|
|
|
* driver_add_kobj - add a kobject below the specified driver
|
2008-02-18 14:09:03 -07:00
|
|
|
* @drv: requesting device driver
|
|
|
|
* @kobj: kobject to add below this driver
|
|
|
|
* @fmt: format string that names the kobject
|
2007-12-19 12:54:39 -07:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
2007-12-17 23:05:35 -07:00
|
|
|
return kobject_add(kobj, &drv->p->kobj, "%s", name);
|
2007-12-19 12:54:39 -07:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(driver_add_kobj);
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
/**
|
2008-01-24 23:50:12 -07:00
|
|
|
* get_driver - increment driver reference count.
|
|
|
|
* @drv: driver.
|
2005-04-16 15:20:36 -07:00
|
|
|
*/
|
2008-01-24 23:50:12 -07:00
|
|
|
struct device_driver *get_driver(struct device_driver *drv)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2007-11-28 16:59:15 -07:00
|
|
|
if (drv) {
|
|
|
|
struct driver_private *priv;
|
|
|
|
struct kobject *kobj;
|
|
|
|
|
|
|
|
kobj = kobject_get(&drv->p->kobj);
|
|
|
|
priv = to_driver(kobj);
|
|
|
|
return priv->driver;
|
|
|
|
}
|
|
|
|
return NULL;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2008-01-24 23:50:12 -07:00
|
|
|
EXPORT_SYMBOL_GPL(get_driver);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
/**
|
2008-01-24 23:50:12 -07:00
|
|
|
* put_driver - decrement driver's refcount.
|
|
|
|
* @drv: driver.
|
2005-04-16 15:20:36 -07:00
|
|
|
*/
|
2008-01-24 23:50:12 -07:00
|
|
|
void put_driver(struct device_driver *drv)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2007-11-28 16:59:15 -07:00
|
|
|
kobject_put(&drv->p->kobj);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2008-01-24 23:50:12 -07:00
|
|
|
EXPORT_SYMBOL_GPL(put_driver);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2007-12-05 04:50:23 -07:00
|
|
|
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++) {
|
2007-11-28 16:59:15 -07:00
|
|
|
error = sysfs_create_group(&drv->p->kobj, groups[i]);
|
2007-12-05 04:50:23 -07:00
|
|
|
if (error) {
|
|
|
|
while (--i >= 0)
|
2007-11-28 16:59:15 -07:00
|
|
|
sysfs_remove_group(&drv->p->kobj,
|
2007-12-05 04:50:23 -07:00
|
|
|
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++)
|
2007-11-28 16:59:15 -07:00
|
|
|
sysfs_remove_group(&drv->p->kobj, groups[i]);
|
2007-12-05 04:50:23 -07:00
|
|
|
}
|
|
|
|
|
2005-04-16 15:20:36 -07:00
|
|
|
/**
|
2008-01-24 23:50:12 -07:00
|
|
|
* driver_register - register driver with bus
|
|
|
|
* @drv: driver to register
|
2005-04-16 15:20:36 -07:00
|
|
|
*
|
2008-01-24 23:50:12 -07:00
|
|
|
* 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.
|
2005-04-16 15:20:36 -07:00
|
|
|
*/
|
2008-01-24 23:50:12 -07:00
|
|
|
int driver_register(struct device_driver *drv)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2007-12-05 04:50:23 -07:00
|
|
|
int ret;
|
|
|
|
|
2006-01-05 07:29:51 -07:00
|
|
|
if ((drv->bus->probe && drv->probe) ||
|
|
|
|
(drv->bus->remove && drv->remove) ||
|
2008-01-24 23:50:12 -07:00
|
|
|
(drv->bus->shutdown && drv->shutdown))
|
|
|
|
printk(KERN_WARNING "Driver '%s' needs updating - please use "
|
|
|
|
"bus_type methods\n", drv->name);
|
2007-12-05 04:50:23 -07:00
|
|
|
ret = bus_add_driver(drv);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
ret = driver_add_groups(drv, drv->groups);
|
|
|
|
if (ret)
|
|
|
|
bus_remove_driver(drv);
|
|
|
|
return ret;
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2008-01-24 23:50:12 -07:00
|
|
|
EXPORT_SYMBOL_GPL(driver_register);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
/**
|
2008-01-24 23:50:12 -07:00
|
|
|
* driver_unregister - remove driver from system.
|
|
|
|
* @drv: driver.
|
2005-04-16 15:20:36 -07:00
|
|
|
*
|
2008-01-24 23:50:12 -07:00
|
|
|
* Again, we pass off most of the work to the bus-level call.
|
2005-04-16 15:20:36 -07:00
|
|
|
*/
|
2008-01-24 23:50:12 -07:00
|
|
|
void driver_unregister(struct device_driver *drv)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2007-12-05 04:50:23 -07:00
|
|
|
driver_remove_groups(drv, drv->groups);
|
2005-04-16 15:20:36 -07:00
|
|
|
bus_remove_driver(drv);
|
|
|
|
}
|
2008-01-24 23:50:12 -07:00
|
|
|
EXPORT_SYMBOL_GPL(driver_unregister);
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
/**
|
2008-01-24 23:50:12 -07:00
|
|
|
* driver_find - locate driver on a bus by its name.
|
|
|
|
* @name: name of the driver.
|
|
|
|
* @bus: bus to scan for the driver.
|
2005-04-16 15:20:36 -07:00
|
|
|
*
|
2008-01-24 23:50:12 -07:00
|
|
|
* Call kset_find_obj() to iterate over list of drivers on
|
|
|
|
* a bus to find driver by name. Return driver if found.
|
2005-04-16 15:20:36 -07:00
|
|
|
*
|
2008-01-24 23:50:12 -07:00
|
|
|
* Note that kset_find_obj increments driver's reference count.
|
2005-04-16 15:20:36 -07:00
|
|
|
*/
|
|
|
|
struct device_driver *driver_find(const char *name, struct bus_type *bus)
|
|
|
|
{
|
2007-11-01 19:41:16 -07:00
|
|
|
struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
|
2007-11-28 16:59:15 -07:00
|
|
|
struct driver_private *priv;
|
|
|
|
|
|
|
|
if (k) {
|
|
|
|
priv = to_driver(k);
|
|
|
|
return priv->driver;
|
|
|
|
}
|
2005-04-16 15:20:36 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(driver_find);
|