Blame view
drivers/base/driver.c
5.78 KB
989d42e85 driver core: add ... |
1 |
// SPDX-License-Identifier: GPL-2.0 |
1da177e4c Linux-2.6.12-rc2 |
2 3 4 5 6 |
/* * driver.c - centralized device driver management * * Copyright (c) 2002-3 Patrick Mochel * Copyright (c) 2002-3 Open Source Development Labs |
e5dd12784 Driver core: move... |
7 8 |
* Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de> * Copyright (c) 2007 Novell Inc. |
1da177e4c Linux-2.6.12-rc2 |
9 |
*/ |
4c002c978 device.h: move 's... |
10 |
#include <linux/device/driver.h> |
1da177e4c Linux-2.6.12-rc2 |
11 12 13 |
#include <linux/device.h> #include <linux/module.h> #include <linux/errno.h> |
5a0e3ad6a include cleanup: ... |
14 |
#include <linux/slab.h> |
1da177e4c Linux-2.6.12-rc2 |
15 |
#include <linux/string.h> |
639676856 driver core: add ... |
16 |
#include <linux/sysfs.h> |
1da177e4c Linux-2.6.12-rc2 |
17 |
#include "base.h" |
4a3ad20cc Driver core: codi... |
18 |
static struct device *next_device(struct klist_iter *i) |
94e7b1c5f [PATCH] Add a kli... |
19 |
{ |
4a3ad20cc Driver core: codi... |
20 |
struct klist_node *n = klist_next(i); |
8940b4f31 driver core: move... |
21 22 23 24 25 26 27 28 |
struct device *dev = NULL; struct device_private *dev_prv; if (n) { dev_prv = to_device_private_driver(n); dev = dev_prv->device; } return dev; |
94e7b1c5f [PATCH] Add a kli... |
29 |
} |
fae3cd002 [PATCH] Add drive... |
30 |
/** |
4a3ad20cc Driver core: codi... |
31 32 33 34 35 |
* 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. |
fae3cd002 [PATCH] Add drive... |
36 |
* |
4a3ad20cc Driver core: codi... |
37 |
* Iterate over the @drv's list of devices calling @fn for each one. |
fae3cd002 [PATCH] Add drive... |
38 |
*/ |
4a3ad20cc Driver core: codi... |
39 40 |
int driver_for_each_device(struct device_driver *drv, struct device *start, void *data, int (*fn)(struct device *, void *)) |
fae3cd002 [PATCH] Add drive... |
41 |
{ |
94e7b1c5f [PATCH] Add a kli... |
42 |
struct klist_iter i; |
4a3ad20cc Driver core: codi... |
43 |
struct device *dev; |
fae3cd002 [PATCH] Add drive... |
44 |
int error = 0; |
94e7b1c5f [PATCH] Add a kli... |
45 46 |
if (!drv) return -EINVAL; |
7cd9c9bb5 Revert "driver co... |
47 48 |
klist_iter_init_node(&drv->p->klist_devices, &i, start ? &start->p->knode_driver : NULL); |
93ead7c94 drivers: base: om... |
49 |
while (!error && (dev = next_device(&i))) |
7cd9c9bb5 Revert "driver co... |
50 51 |
error = fn(dev, data); klist_iter_exit(&i); |
fae3cd002 [PATCH] Add drive... |
52 53 |
return error; } |
126eddfbf [PATCH] driver co... |
54 |
EXPORT_SYMBOL_GPL(driver_for_each_device); |
fae3cd002 [PATCH] Add drive... |
55 |
|
1da177e4c Linux-2.6.12-rc2 |
56 |
/** |
0edb58604 [PATCH] driver co... |
57 |
* driver_find_device - device iterator for locating a particular device. |
c41455fba [PATCH] kernel-do... |
58 |
* @drv: The device's driver |
0edb58604 [PATCH] driver co... |
59 60 61 62 63 64 65 66 67 68 69 70 |
* @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. */ |
4a3ad20cc Driver core: codi... |
71 |
struct device *driver_find_device(struct device_driver *drv, |
92ce7e83b driver_find_devic... |
72 73 |
struct device *start, const void *data, int (*match)(struct device *dev, const void *data)) |
0edb58604 [PATCH] driver co... |
74 75 76 |
{ struct klist_iter i; struct device *dev; |
094e47e9f Driver Core: don'... |
77 |
if (!drv || !drv->p) |
0edb58604 [PATCH] driver co... |
78 |
return NULL; |
7cd9c9bb5 Revert "driver co... |
79 80 |
klist_iter_init_node(&drv->p->klist_devices, &i, (start ? &start->p->knode_driver : NULL)); |
0edb58604 [PATCH] driver co... |
81 82 83 84 85 86 87 88 89 |
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); /** |
4a3ad20cc Driver core: codi... |
90 91 92 |
* driver_create_file - create sysfs file for driver. * @drv: driver. * @attr: driver attribute descriptor. |
1da177e4c Linux-2.6.12-rc2 |
93 |
*/ |
4a3ad20cc Driver core: codi... |
94 |
int driver_create_file(struct device_driver *drv, |
099c2f21d Driver core: driv... |
95 |
const struct driver_attribute *attr) |
1da177e4c Linux-2.6.12-rc2 |
96 97 |
{ int error; |
74642c6cd driver core: add ... |
98 |
|
0c98b19fe Driver core: Remo... |
99 |
if (drv) |
e5dd12784 Driver core: move... |
100 |
error = sysfs_create_file(&drv->p->kobj, &attr->attr); |
0c98b19fe Driver core: Remo... |
101 |
else |
1da177e4c Linux-2.6.12-rc2 |
102 103 104 |
error = -EINVAL; return error; } |
4a3ad20cc Driver core: codi... |
105 |
EXPORT_SYMBOL_GPL(driver_create_file); |
1da177e4c Linux-2.6.12-rc2 |
106 107 |
/** |
4a3ad20cc Driver core: codi... |
108 109 110 |
* driver_remove_file - remove sysfs file for driver. * @drv: driver. * @attr: driver attribute descriptor. |
1da177e4c Linux-2.6.12-rc2 |
111 |
*/ |
4a3ad20cc Driver core: codi... |
112 |
void driver_remove_file(struct device_driver *drv, |
099c2f21d Driver core: driv... |
113 |
const struct driver_attribute *attr) |
1da177e4c Linux-2.6.12-rc2 |
114 |
{ |
0c98b19fe Driver core: Remo... |
115 |
if (drv) |
e5dd12784 Driver core: move... |
116 |
sysfs_remove_file(&drv->p->kobj, &attr->attr); |
1da177e4c Linux-2.6.12-rc2 |
117 |
} |
4a3ad20cc Driver core: codi... |
118 |
EXPORT_SYMBOL_GPL(driver_remove_file); |
1da177e4c Linux-2.6.12-rc2 |
119 |
|
ed0617b5c driver core: bus_... |
120 121 |
int driver_add_groups(struct device_driver *drv, const struct attribute_group **groups) |
57c745340 driver core: Intr... |
122 |
{ |
3e9b2bae8 sysfs: add sysfs_... |
123 |
return sysfs_create_groups(&drv->p->kobj, groups); |
57c745340 driver core: Intr... |
124 |
} |
ed0617b5c driver core: bus_... |
125 126 |
void driver_remove_groups(struct device_driver *drv, const struct attribute_group **groups) |
57c745340 driver core: Intr... |
127 |
{ |
3e9b2bae8 sysfs: add sysfs_... |
128 |
sysfs_remove_groups(&drv->p->kobj, groups); |
57c745340 driver core: Intr... |
129 |
} |
1da177e4c Linux-2.6.12-rc2 |
130 |
/** |
4a3ad20cc Driver core: codi... |
131 132 |
* driver_register - register driver with bus * @drv: driver to register |
1da177e4c Linux-2.6.12-rc2 |
133 |
* |
4a3ad20cc Driver core: codi... |
134 135 136 |
* 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. |
1da177e4c Linux-2.6.12-rc2 |
137 |
*/ |
4a3ad20cc Driver core: codi... |
138 |
int driver_register(struct device_driver *drv) |
1da177e4c Linux-2.6.12-rc2 |
139 |
{ |
57c745340 driver core: Intr... |
140 |
int ret; |
16dc42e01 driver core: warn... |
141 |
struct device_driver *other; |
57c745340 driver core: Intr... |
142 |
|
0dda2bb62 driver-core: retu... |
143 144 145 146 147 148 |
if (!drv->bus->p) { pr_err("Driver '%s' was unable to register with bus_type '%s' because the bus was not initialized. ", drv->name, drv->bus->name); return -EINVAL; } |
f48f3febb driver-core: do n... |
149 |
|
594c8281f [PATCH] Add bus_t... |
150 151 |
if ((drv->bus->probe && drv->probe) || (drv->bus->remove && drv->remove) || |
4a3ad20cc Driver core: codi... |
152 |
(drv->bus->shutdown && drv->shutdown)) |
5962b8b27 drivers: base: Co... |
153 |
pr_warn("Driver '%s' needs updating - please use " |
4a3ad20cc Driver core: codi... |
154 155 |
"bus_type methods ", drv->name); |
16dc42e01 driver core: warn... |
156 157 158 |
other = driver_find(drv->name, drv->bus); if (other) { |
5962b8b27 drivers: base: Co... |
159 |
pr_err("Error: Driver '%s' is already registered, " |
16dc42e01 driver core: warn... |
160 161 |
"aborting... ", drv->name); |
39acbc12a Driver core: fix ... |
162 |
return -EBUSY; |
16dc42e01 driver core: warn... |
163 |
} |
57c745340 driver core: Intr... |
164 165 166 167 |
ret = bus_add_driver(drv); if (ret) return ret; ret = driver_add_groups(drv, drv->groups); |
a14af3256 driver core: don'... |
168 |
if (ret) { |
57c745340 driver core: Intr... |
169 |
bus_remove_driver(drv); |
a14af3256 driver core: don'... |
170 171 |
return ret; } |
5a7689fd5 driver core: move... |
172 |
kobject_uevent(&drv->p->kobj, KOBJ_ADD); |
57c745340 driver core: Intr... |
173 |
return ret; |
1da177e4c Linux-2.6.12-rc2 |
174 |
} |
4a3ad20cc Driver core: codi... |
175 |
EXPORT_SYMBOL_GPL(driver_register); |
1da177e4c Linux-2.6.12-rc2 |
176 |
|
1da177e4c Linux-2.6.12-rc2 |
177 |
/** |
4a3ad20cc Driver core: codi... |
178 179 |
* driver_unregister - remove driver from system. * @drv: driver. |
1da177e4c Linux-2.6.12-rc2 |
180 |
* |
4a3ad20cc Driver core: codi... |
181 |
* Again, we pass off most of the work to the bus-level call. |
1da177e4c Linux-2.6.12-rc2 |
182 |
*/ |
4a3ad20cc Driver core: codi... |
183 |
void driver_unregister(struct device_driver *drv) |
1da177e4c Linux-2.6.12-rc2 |
184 |
{ |
5c8563d77 Driver Core: do n... |
185 186 187 188 189 |
if (!drv || !drv->p) { WARN(1, "Unexpected driver unregister! "); return; } |
57c745340 driver core: Intr... |
190 |
driver_remove_groups(drv, drv->groups); |
1da177e4c Linux-2.6.12-rc2 |
191 |
bus_remove_driver(drv); |
1da177e4c Linux-2.6.12-rc2 |
192 |
} |
4a3ad20cc Driver core: codi... |
193 |
EXPORT_SYMBOL_GPL(driver_unregister); |
1da177e4c Linux-2.6.12-rc2 |
194 195 |
/** |
4a3ad20cc Driver core: codi... |
196 197 198 |
* driver_find - locate driver on a bus by its name. * @name: name of the driver. * @bus: bus to scan for the driver. |
1da177e4c Linux-2.6.12-rc2 |
199 |
* |
4a3ad20cc Driver core: codi... |
200 201 |
* Call kset_find_obj() to iterate over list of drivers on * a bus to find driver by name. Return driver if found. |
1da177e4c Linux-2.6.12-rc2 |
202 |
* |
fde25a9b6 Driver core: driv... |
203 204 205 |
* 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. |
1da177e4c Linux-2.6.12-rc2 |
206 207 208 |
*/ struct device_driver *driver_find(const char *name, struct bus_type *bus) { |
c6f7e72a3 driver core: remo... |
209 |
struct kobject *k = kset_find_obj(bus->p->drivers_kset, name); |
e5dd12784 Driver core: move... |
210 211 212 |
struct driver_private *priv; if (k) { |
fde25a9b6 Driver core: driv... |
213 214 |
/* Drop reference added by kset_find_obj() */ kobject_put(k); |
e5dd12784 Driver core: move... |
215 216 217 |
priv = to_driver(k); return priv->driver; } |
1da177e4c Linux-2.6.12-rc2 |
218 219 |
return NULL; } |
1da177e4c Linux-2.6.12-rc2 |
220 |
EXPORT_SYMBOL_GPL(driver_find); |