2017-11-07 16:30:07 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* driver.c - centralized device driver management
|
|
|
|
*
|
|
|
|
* Copyright (c) 2002-3 Patrick Mochel
|
|
|
|
* Copyright (c) 2002-3 Open Source Development Labs
|
2007-11-28 23:59:15 +00:00
|
|
|
* Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
|
|
|
|
* Copyright (c) 2007 Novell Inc.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
|
2019-12-09 19:33:03 +00:00
|
|
|
#include <linux/device/driver.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/errno.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/string.h>
|
2013-08-27 17:24:15 +00:00
|
|
|
#include <linux/sysfs.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include "base.h"
|
|
|
|
|
2008-01-25 06:50:12 +00:00
|
|
|
static struct device *next_device(struct klist_iter *i)
|
2005-03-21 20:25:36 +00:00
|
|
|
{
|
2008-01-25 06:50:12 +00:00
|
|
|
struct klist_node *n = klist_next(i);
|
2008-12-16 20:25:49 +00:00
|
|
|
struct device *dev = NULL;
|
|
|
|
struct device_private *dev_prv;
|
|
|
|
|
|
|
|
if (n) {
|
|
|
|
dev_prv = to_device_private_driver(n);
|
|
|
|
dev = dev_prv->device;
|
|
|
|
}
|
|
|
|
return dev;
|
2005-03-21 20:25:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 11:34:24 +00:00
|
|
|
/**
|
|
|
|
* driver_set_override() - Helper to set or clear driver override.
|
|
|
|
* @dev: Device to change
|
|
|
|
* @override: Address of string to change (e.g. &device->driver_override);
|
|
|
|
* The contents will be freed and hold newly allocated override.
|
|
|
|
* @s: NUL-terminated string, new driver name to force a match, pass empty
|
|
|
|
* string to clear it ("" or "\n", where the latter is only for sysfs
|
|
|
|
* interface).
|
|
|
|
* @len: length of @s
|
|
|
|
*
|
|
|
|
* Helper to set or clear driver override in a device, intended for the cases
|
|
|
|
* when the driver_override field is allocated by driver/bus code.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success or a negative error code on failure.
|
|
|
|
*/
|
|
|
|
int driver_set_override(struct device *dev, const char **override,
|
|
|
|
const char *s, size_t len)
|
|
|
|
{
|
|
|
|
const char *new, *old;
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
if (!override || !s)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The stored value will be used in sysfs show callback (sysfs_emit()),
|
|
|
|
* which has a length limit of PAGE_SIZE and adds a trailing newline.
|
|
|
|
* Thus we can store one character less to avoid truncation during sysfs
|
|
|
|
* show.
|
|
|
|
*/
|
|
|
|
if (len >= (PAGE_SIZE - 1))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!len) {
|
|
|
|
/* Empty string passed - clear override */
|
|
|
|
device_lock(dev);
|
|
|
|
old = *override;
|
|
|
|
*override = NULL;
|
|
|
|
device_unlock(dev);
|
|
|
|
kfree(old);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cp = strnchr(s, len, '\n');
|
|
|
|
if (cp)
|
|
|
|
len = cp - s;
|
|
|
|
|
|
|
|
new = kstrndup(s, len, GFP_KERNEL);
|
|
|
|
if (!new)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
device_lock(dev);
|
|
|
|
old = *override;
|
|
|
|
if (cp != s) {
|
|
|
|
*override = new;
|
|
|
|
} else {
|
|
|
|
/* "\n" passed - clear override */
|
|
|
|
kfree(new);
|
|
|
|
*override = NULL;
|
|
|
|
}
|
|
|
|
device_unlock(dev);
|
|
|
|
|
|
|
|
kfree(old);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(driver_set_override);
|
|
|
|
|
2005-03-21 18:59:56 +00:00
|
|
|
/**
|
2008-01-25 06:50:12 +00: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 18:59:56 +00:00
|
|
|
*
|
2008-01-25 06:50:12 +00:00
|
|
|
* Iterate over the @drv's list of devices calling @fn for each one.
|
2005-03-21 18:59:56 +00:00
|
|
|
*/
|
2008-01-25 06:50:12 +00:00
|
|
|
int driver_for_each_device(struct device_driver *drv, struct device *start,
|
|
|
|
void *data, int (*fn)(struct device *, void *))
|
2005-03-21 18:59:56 +00:00
|
|
|
{
|
2005-03-21 20:25:36 +00:00
|
|
|
struct klist_iter i;
|
2008-01-25 06:50:12 +00:00
|
|
|
struct device *dev;
|
2005-03-21 18:59:56 +00:00
|
|
|
int error = 0;
|
|
|
|
|
2005-03-21 20:25:36 +00:00
|
|
|
if (!drv)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2012-04-20 02:17:30 +00:00
|
|
|
klist_iter_init_node(&drv->p->klist_devices, &i,
|
|
|
|
start ? &start->p->knode_driver : NULL);
|
2017-11-11 05:52:54 +00:00
|
|
|
while (!error && (dev = next_device(&i)))
|
2012-04-20 02:17:30 +00:00
|
|
|
error = fn(dev, data);
|
|
|
|
klist_iter_exit(&i);
|
2005-03-21 18:59:56 +00:00
|
|
|
return error;
|
|
|
|
}
|
2005-03-22 20:17:13 +00:00
|
|
|
EXPORT_SYMBOL_GPL(driver_for_each_device);
|
2005-03-21 18:59:56 +00:00
|
|
|
|
2005-06-22 14:59:51 +00:00
|
|
|
/**
|
|
|
|
* driver_find_device - device iterator for locating a particular device.
|
2005-10-23 18:59:14 +00:00
|
|
|
* @drv: The device's driver
|
2005-06-22 14:59:51 +00: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-25 06:50:12 +00:00
|
|
|
struct device *driver_find_device(struct device_driver *drv,
|
2019-06-14 17:54:00 +00:00
|
|
|
struct device *start, const void *data,
|
|
|
|
int (*match)(struct device *dev, const void *data))
|
2005-06-22 14:59:51 +00:00
|
|
|
{
|
|
|
|
struct klist_iter i;
|
|
|
|
struct device *dev;
|
|
|
|
|
2012-05-11 08:03:09 +00:00
|
|
|
if (!drv || !drv->p)
|
2005-06-22 14:59:51 +00:00
|
|
|
return NULL;
|
|
|
|
|
2012-04-20 02:17:30 +00:00
|
|
|
klist_iter_init_node(&drv->p->klist_devices, &i,
|
|
|
|
(start ? &start->p->knode_driver : NULL));
|
2005-06-22 14:59:51 +00:00
|
|
|
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 22:20:36 +00:00
|
|
|
/**
|
2008-01-25 06:50:12 +00:00
|
|
|
* driver_create_file - create sysfs file for driver.
|
|
|
|
* @drv: driver.
|
|
|
|
* @attr: driver attribute descriptor.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2008-01-25 06:50:12 +00:00
|
|
|
int driver_create_file(struct device_driver *drv,
|
2009-12-18 13:34:21 +00:00
|
|
|
const struct driver_attribute *attr)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
int error;
|
2015-03-08 10:48:55 +00:00
|
|
|
|
2008-01-31 09:39:38 +00:00
|
|
|
if (drv)
|
2007-11-28 23:59:15 +00:00
|
|
|
error = sysfs_create_file(&drv->p->kobj, &attr->attr);
|
2008-01-31 09:39:38 +00:00
|
|
|
else
|
2005-04-16 22:20:36 +00:00
|
|
|
error = -EINVAL;
|
|
|
|
return error;
|
|
|
|
}
|
2008-01-25 06:50:12 +00:00
|
|
|
EXPORT_SYMBOL_GPL(driver_create_file);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/**
|
2008-01-25 06:50:12 +00:00
|
|
|
* driver_remove_file - remove sysfs file for driver.
|
|
|
|
* @drv: driver.
|
|
|
|
* @attr: driver attribute descriptor.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2008-01-25 06:50:12 +00:00
|
|
|
void driver_remove_file(struct device_driver *drv,
|
2009-12-18 13:34:21 +00:00
|
|
|
const struct driver_attribute *attr)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-01-31 09:39:38 +00:00
|
|
|
if (drv)
|
2007-11-28 23:59:15 +00:00
|
|
|
sysfs_remove_file(&drv->p->kobj, &attr->attr);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2008-01-25 06:50:12 +00:00
|
|
|
EXPORT_SYMBOL_GPL(driver_remove_file);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-08-08 22:22:56 +00:00
|
|
|
int driver_add_groups(struct device_driver *drv,
|
|
|
|
const struct attribute_group **groups)
|
2007-12-05 11:50:23 +00:00
|
|
|
{
|
2013-08-21 20:47:50 +00:00
|
|
|
return sysfs_create_groups(&drv->p->kobj, groups);
|
2007-12-05 11:50:23 +00:00
|
|
|
}
|
|
|
|
|
2013-08-08 22:22:56 +00:00
|
|
|
void driver_remove_groups(struct device_driver *drv,
|
|
|
|
const struct attribute_group **groups)
|
2007-12-05 11:50:23 +00:00
|
|
|
{
|
2013-08-21 20:47:50 +00:00
|
|
|
sysfs_remove_groups(&drv->p->kobj, groups);
|
2007-12-05 11:50:23 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/**
|
2008-01-25 06:50:12 +00:00
|
|
|
* driver_register - register driver with bus
|
|
|
|
* @drv: driver to register
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2008-01-25 06:50:12 +00: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 22:20:36 +00:00
|
|
|
*/
|
2008-01-25 06:50:12 +00:00
|
|
|
int driver_register(struct device_driver *drv)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-12-05 11:50:23 +00:00
|
|
|
int ret;
|
2008-04-26 15:52:35 +00:00
|
|
|
struct device_driver *other;
|
2007-12-05 11:50:23 +00:00
|
|
|
|
2018-05-23 15:59:11 +00:00
|
|
|
if (!drv->bus->p) {
|
|
|
|
pr_err("Driver '%s' was unable to register with bus_type '%s' because the bus was not initialized.\n",
|
|
|
|
drv->name, drv->bus->name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2009-02-14 13:23:22 +00:00
|
|
|
|
2006-01-05 14:29:51 +00:00
|
|
|
if ((drv->bus->probe && drv->probe) ||
|
|
|
|
(drv->bus->remove && drv->remove) ||
|
2008-01-25 06:50:12 +00:00
|
|
|
(drv->bus->shutdown && drv->shutdown))
|
2020-06-08 09:52:17 +00:00
|
|
|
pr_warn("Driver '%s' needs updating - please use "
|
2008-01-25 06:50:12 +00:00
|
|
|
"bus_type methods\n", drv->name);
|
2008-04-26 15:52:35 +00:00
|
|
|
|
|
|
|
other = driver_find(drv->name, drv->bus);
|
|
|
|
if (other) {
|
2020-06-08 09:52:17 +00:00
|
|
|
pr_err("Error: Driver '%s' is already registered, "
|
2008-04-26 15:52:35 +00:00
|
|
|
"aborting...\n", drv->name);
|
2009-10-17 20:31:38 +00:00
|
|
|
return -EBUSY;
|
2008-04-26 15:52:35 +00:00
|
|
|
}
|
|
|
|
|
2007-12-05 11:50:23 +00:00
|
|
|
ret = bus_add_driver(drv);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
ret = driver_add_groups(drv, drv->groups);
|
2012-07-17 08:39:10 +00:00
|
|
|
if (ret) {
|
2007-12-05 11:50:23 +00:00
|
|
|
bus_remove_driver(drv);
|
2012-07-17 08:39:10 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2012-07-02 17:08:15 +00:00
|
|
|
kobject_uevent(&drv->p->kobj, KOBJ_ADD);
|
2022-04-29 22:09:32 +00:00
|
|
|
deferred_probe_extend_timeout();
|
2012-07-02 17:08:15 +00:00
|
|
|
|
2007-12-05 11:50:23 +00:00
|
|
|
return ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2008-01-25 06:50:12 +00:00
|
|
|
EXPORT_SYMBOL_GPL(driver_register);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/**
|
2008-01-25 06:50:12 +00:00
|
|
|
* driver_unregister - remove driver from system.
|
|
|
|
* @drv: driver.
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2008-01-25 06:50:12 +00:00
|
|
|
* Again, we pass off most of the work to the bus-level call.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2008-01-25 06:50:12 +00:00
|
|
|
void driver_unregister(struct device_driver *drv)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2009-05-28 21:24:07 +00:00
|
|
|
if (!drv || !drv->p) {
|
|
|
|
WARN(1, "Unexpected driver unregister!\n");
|
|
|
|
return;
|
|
|
|
}
|
2007-12-05 11:50:23 +00:00
|
|
|
driver_remove_groups(drv, drv->groups);
|
2005-04-16 22:20:36 +00:00
|
|
|
bus_remove_driver(drv);
|
|
|
|
}
|
2008-01-25 06:50:12 +00:00
|
|
|
EXPORT_SYMBOL_GPL(driver_unregister);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/**
|
2008-01-25 06:50:12 +00: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 22:20:36 +00:00
|
|
|
*
|
2008-01-25 06:50:12 +00: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 22:20:36 +00:00
|
|
|
*
|
2012-01-24 18:34:24 +00:00
|
|
|
* This routine provides no locking to prevent the driver it returns
|
|
|
|
* from being unregistered or unloaded while the caller is using it.
|
|
|
|
* The caller is responsible for preventing this.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
struct device_driver *driver_find(const char *name, struct bus_type *bus)
|
|
|
|
{
|
2007-11-02 02:41:16 +00:00
|
|
|
struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
|
2007-11-28 23:59:15 +00:00
|
|
|
struct driver_private *priv;
|
|
|
|
|
|
|
|
if (k) {
|
2012-01-24 18:34:24 +00:00
|
|
|
/* Drop reference added by kset_find_obj() */
|
|
|
|
kobject_put(k);
|
2007-11-28 23:59:15 +00:00
|
|
|
priv = to_driver(k);
|
|
|
|
return priv->driver;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(driver_find);
|