Blame view
drivers/base/bus.c
31.7 KB
1da177e4c Linux-2.6.12-rc2 |
1 2 3 4 5 |
/* * bus.c - bus driver management * * Copyright (c) 2002-3 Patrick Mochel * Copyright (c) 2002-3 Open Source Development Labs |
e5dd12784 Driver core: move... |
6 7 |
* Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de> * Copyright (c) 2007 Novell Inc. |
1da177e4c Linux-2.6.12-rc2 |
8 9 10 11 |
* * This file is released under the GPLv2 * */ |
1da177e4c Linux-2.6.12-rc2 |
12 13 14 |
#include <linux/device.h> #include <linux/module.h> #include <linux/errno.h> |
5a0e3ad6a include cleanup: ... |
15 |
#include <linux/slab.h> |
1da177e4c Linux-2.6.12-rc2 |
16 17 |
#include <linux/init.h> #include <linux/string.h> |
ca22e56de driver-core: impl... |
18 |
#include <linux/mutex.h> |
1da177e4c Linux-2.6.12-rc2 |
19 20 |
#include "base.h" #include "power/power.h" |
ca22e56de driver-core: impl... |
21 22 23 |
/* /sys/devices/system */ /* FIXME: make static after drivers/base/sys.c is deleted */ struct kset *system_kset; |
1da177e4c Linux-2.6.12-rc2 |
24 |
#define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr) |
1da177e4c Linux-2.6.12-rc2 |
25 26 27 28 29 30 |
/* * sysfs bindings for drivers */ #define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr) |
1da177e4c Linux-2.6.12-rc2 |
31 |
|
b8c5cec23 Driver core: udev... |
32 33 |
static int __must_check bus_rescan_devices_helper(struct device *dev, void *data); |
5901d0145 Driver core: remo... |
34 35 |
static struct bus_type *bus_get(struct bus_type *bus) { |
c6f7e72a3 driver core: remo... |
36 37 38 39 40 |
if (bus) { kset_get(&bus->p->subsys); return bus; } return NULL; |
5901d0145 Driver core: remo... |
41 |
} |
fc1ede588 Driver core: remo... |
42 43 |
static void bus_put(struct bus_type *bus) { |
c6f7e72a3 driver core: remo... |
44 45 |
if (bus) kset_put(&bus->p->subsys); |
fc1ede588 Driver core: remo... |
46 |
} |
4a3ad20cc Driver core: codi... |
47 48 |
static ssize_t drv_attr_show(struct kobject *kobj, struct attribute *attr, char *buf) |
1da177e4c Linux-2.6.12-rc2 |
49 |
{ |
4a3ad20cc Driver core: codi... |
50 |
struct driver_attribute *drv_attr = to_drv_attr(attr); |
e5dd12784 Driver core: move... |
51 |
struct driver_private *drv_priv = to_driver(kobj); |
4a0c20bf8 [PATCH] sysfs: (d... |
52 |
ssize_t ret = -EIO; |
1da177e4c Linux-2.6.12-rc2 |
53 54 |
if (drv_attr->show) |
e5dd12784 Driver core: move... |
55 |
ret = drv_attr->show(drv_priv->driver, buf); |
1da177e4c Linux-2.6.12-rc2 |
56 57 |
return ret; } |
4a3ad20cc Driver core: codi... |
58 59 |
static ssize_t drv_attr_store(struct kobject *kobj, struct attribute *attr, const char *buf, size_t count) |
1da177e4c Linux-2.6.12-rc2 |
60 |
{ |
4a3ad20cc Driver core: codi... |
61 |
struct driver_attribute *drv_attr = to_drv_attr(attr); |
e5dd12784 Driver core: move... |
62 |
struct driver_private *drv_priv = to_driver(kobj); |
4a0c20bf8 [PATCH] sysfs: (d... |
63 |
ssize_t ret = -EIO; |
1da177e4c Linux-2.6.12-rc2 |
64 65 |
if (drv_attr->store) |
e5dd12784 Driver core: move... |
66 |
ret = drv_attr->store(drv_priv->driver, buf, count); |
1da177e4c Linux-2.6.12-rc2 |
67 68 |
return ret; } |
52cf25d0a Driver core: Cons... |
69 |
static const struct sysfs_ops driver_sysfs_ops = { |
1da177e4c Linux-2.6.12-rc2 |
70 71 72 |
.show = drv_attr_show, .store = drv_attr_store, }; |
e5dd12784 Driver core: move... |
73 |
static void driver_release(struct kobject *kobj) |
1da177e4c Linux-2.6.12-rc2 |
74 |
{ |
e5dd12784 Driver core: move... |
75 |
struct driver_private *drv_priv = to_driver(kobj); |
2b3a302a0 driver core: repl... |
76 77 |
pr_debug("driver: '%s': %s ", kobject_name(kobj), __func__); |
e5dd12784 Driver core: move... |
78 |
kfree(drv_priv); |
1da177e4c Linux-2.6.12-rc2 |
79 |
} |
a1148fb03 Driver core: rena... |
80 |
static struct kobj_type driver_ktype = { |
1da177e4c Linux-2.6.12-rc2 |
81 82 83 |
.sysfs_ops = &driver_sysfs_ops, .release = driver_release, }; |
1da177e4c Linux-2.6.12-rc2 |
84 85 86 |
/* * sysfs bindings for buses */ |
4a3ad20cc Driver core: codi... |
87 88 |
static ssize_t bus_attr_show(struct kobject *kobj, struct attribute *attr, char *buf) |
1da177e4c Linux-2.6.12-rc2 |
89 |
{ |
4a3ad20cc Driver core: codi... |
90 |
struct bus_attribute *bus_attr = to_bus_attr(attr); |
6b6e39a6a driver-core: merg... |
91 |
struct subsys_private *subsys_priv = to_subsys_private(kobj); |
1da177e4c Linux-2.6.12-rc2 |
92 93 94 |
ssize_t ret = 0; if (bus_attr->show) |
6b6e39a6a driver-core: merg... |
95 |
ret = bus_attr->show(subsys_priv->bus, buf); |
1da177e4c Linux-2.6.12-rc2 |
96 97 |
return ret; } |
4a3ad20cc Driver core: codi... |
98 99 |
static ssize_t bus_attr_store(struct kobject *kobj, struct attribute *attr, const char *buf, size_t count) |
1da177e4c Linux-2.6.12-rc2 |
100 |
{ |
4a3ad20cc Driver core: codi... |
101 |
struct bus_attribute *bus_attr = to_bus_attr(attr); |
6b6e39a6a driver-core: merg... |
102 |
struct subsys_private *subsys_priv = to_subsys_private(kobj); |
1da177e4c Linux-2.6.12-rc2 |
103 104 105 |
ssize_t ret = 0; if (bus_attr->store) |
6b6e39a6a driver-core: merg... |
106 |
ret = bus_attr->store(subsys_priv->bus, buf, count); |
1da177e4c Linux-2.6.12-rc2 |
107 108 |
return ret; } |
52cf25d0a Driver core: Cons... |
109 |
static const struct sysfs_ops bus_sysfs_ops = { |
1da177e4c Linux-2.6.12-rc2 |
110 111 112 |
.show = bus_attr_show, .store = bus_attr_store, }; |
4a3ad20cc Driver core: codi... |
113 |
int bus_create_file(struct bus_type *bus, struct bus_attribute *attr) |
1da177e4c Linux-2.6.12-rc2 |
114 115 |
{ int error; |
5901d0145 Driver core: remo... |
116 |
if (bus_get(bus)) { |
c6f7e72a3 driver core: remo... |
117 |
error = sysfs_create_file(&bus->p->subsys.kobj, &attr->attr); |
fc1ede588 Driver core: remo... |
118 |
bus_put(bus); |
1da177e4c Linux-2.6.12-rc2 |
119 120 121 122 |
} else error = -EINVAL; return error; } |
4a3ad20cc Driver core: codi... |
123 |
EXPORT_SYMBOL_GPL(bus_create_file); |
1da177e4c Linux-2.6.12-rc2 |
124 |
|
4a3ad20cc Driver core: codi... |
125 |
void bus_remove_file(struct bus_type *bus, struct bus_attribute *attr) |
1da177e4c Linux-2.6.12-rc2 |
126 |
{ |
5901d0145 Driver core: remo... |
127 |
if (bus_get(bus)) { |
c6f7e72a3 driver core: remo... |
128 |
sysfs_remove_file(&bus->p->subsys.kobj, &attr->attr); |
fc1ede588 Driver core: remo... |
129 |
bus_put(bus); |
1da177e4c Linux-2.6.12-rc2 |
130 131 |
} } |
4a3ad20cc Driver core: codi... |
132 |
EXPORT_SYMBOL_GPL(bus_remove_file); |
1da177e4c Linux-2.6.12-rc2 |
133 |
|
80f03e349 Driver core: add ... |
134 |
static struct kobj_type bus_ktype = { |
1da177e4c Linux-2.6.12-rc2 |
135 |
.sysfs_ops = &bus_sysfs_ops, |
80f03e349 Driver core: add ... |
136 137 138 139 140 141 142 143 144 145 |
}; static int bus_uevent_filter(struct kset *kset, struct kobject *kobj) { struct kobj_type *ktype = get_ktype(kobj); if (ktype == &bus_ktype) return 1; return 0; } |
1da177e4c Linux-2.6.12-rc2 |
146 |
|
9cd43611c kobject: Constify... |
147 |
static const struct kset_uevent_ops bus_uevent_ops = { |
80f03e349 Driver core: add ... |
148 |
.filter = bus_uevent_filter, |
1da177e4c Linux-2.6.12-rc2 |
149 |
}; |
59a548338 kset: convert dri... |
150 |
static struct kset *bus_kset; |
1da177e4c Linux-2.6.12-rc2 |
151 |
|
1da177e4c Linux-2.6.12-rc2 |
152 |
|
2139bdd5b [PATCH] drivers/b... |
153 |
#ifdef CONFIG_HOTPLUG |
2b08c8d04 [PATCH] Small fix... |
154 |
/* Manually detach a device from its associated driver. */ |
151ef38f7 [PATCH] driver co... |
155 156 157 |
static ssize_t driver_unbind(struct device_driver *drv, const char *buf, size_t count) { |
5901d0145 Driver core: remo... |
158 |
struct bus_type *bus = bus_get(drv->bus); |
151ef38f7 [PATCH] driver co... |
159 160 |
struct device *dev; int err = -ENODEV; |
1f9ffc049 Driver core: add ... |
161 |
dev = bus_find_device_by_name(bus, NULL, buf); |
2b08c8d04 [PATCH] Small fix... |
162 |
if (dev && dev->driver == drv) { |
bf74ad5bc [PATCH] Hold the ... |
163 |
if (dev->parent) /* Needed for USB */ |
8e9394ce2 Driver core: crea... |
164 |
device_lock(dev->parent); |
151ef38f7 [PATCH] driver co... |
165 |
device_release_driver(dev); |
bf74ad5bc [PATCH] Hold the ... |
166 |
if (dev->parent) |
8e9394ce2 Driver core: crea... |
167 |
device_unlock(dev->parent); |
151ef38f7 [PATCH] driver co... |
168 169 |
err = count; } |
2b08c8d04 [PATCH] Small fix... |
170 |
put_device(dev); |
fc1ede588 Driver core: remo... |
171 |
bus_put(bus); |
2b08c8d04 [PATCH] Small fix... |
172 |
return err; |
151ef38f7 [PATCH] driver co... |
173 174 |
} static DRIVER_ATTR(unbind, S_IWUSR, NULL, driver_unbind); |
afdce75f1 [PATCH] driver co... |
175 176 177 178 179 180 181 182 |
/* * Manually attach a device to a driver. * Note: the driver must want to bind to the device, * it is not possible to override the driver's id table. */ static ssize_t driver_bind(struct device_driver *drv, const char *buf, size_t count) { |
5901d0145 Driver core: remo... |
183 |
struct bus_type *bus = bus_get(drv->bus); |
afdce75f1 [PATCH] driver co... |
184 185 |
struct device *dev; int err = -ENODEV; |
1f9ffc049 Driver core: add ... |
186 |
dev = bus_find_device_by_name(bus, NULL, buf); |
49b420a13 driver core: chec... |
187 |
if (dev && dev->driver == NULL && driver_match_device(drv, dev)) { |
bf74ad5bc [PATCH] Hold the ... |
188 |
if (dev->parent) /* Needed for USB */ |
8e9394ce2 Driver core: crea... |
189 190 |
device_lock(dev->parent); device_lock(dev); |
afdce75f1 [PATCH] driver co... |
191 |
err = driver_probe_device(drv, dev); |
8e9394ce2 Driver core: crea... |
192 |
device_unlock(dev); |
bf74ad5bc [PATCH] Hold the ... |
193 |
if (dev->parent) |
8e9394ce2 Driver core: crea... |
194 |
device_unlock(dev->parent); |
372254018 [PATCH] driver co... |
195 |
|
4a3ad20cc Driver core: codi... |
196 197 |
if (err > 0) { /* success */ |
372254018 [PATCH] driver co... |
198 |
err = count; |
4a3ad20cc Driver core: codi... |
199 200 |
} else if (err == 0) { /* driver didn't accept device */ |
372254018 [PATCH] driver co... |
201 |
err = -ENODEV; |
4a3ad20cc Driver core: codi... |
202 |
} |
afdce75f1 [PATCH] driver co... |
203 |
} |
2b08c8d04 [PATCH] Small fix... |
204 |
put_device(dev); |
fc1ede588 Driver core: remo... |
205 |
bus_put(bus); |
2b08c8d04 [PATCH] Small fix... |
206 |
return err; |
afdce75f1 [PATCH] driver co... |
207 208 |
} static DRIVER_ATTR(bind, S_IWUSR, NULL, driver_bind); |
b8c5cec23 Driver core: udev... |
209 210 |
static ssize_t show_drivers_autoprobe(struct bus_type *bus, char *buf) { |
c6f7e72a3 driver core: remo... |
211 212 |
return sprintf(buf, "%d ", bus->p->drivers_autoprobe); |
b8c5cec23 Driver core: udev... |
213 214 215 216 217 218 |
} static ssize_t store_drivers_autoprobe(struct bus_type *bus, const char *buf, size_t count) { if (buf[0] == '0') |
c6f7e72a3 driver core: remo... |
219 |
bus->p->drivers_autoprobe = 0; |
b8c5cec23 Driver core: udev... |
220 |
else |
c6f7e72a3 driver core: remo... |
221 |
bus->p->drivers_autoprobe = 1; |
b8c5cec23 Driver core: udev... |
222 223 224 225 226 227 228 |
return count; } static ssize_t store_drivers_probe(struct bus_type *bus, const char *buf, size_t count) { struct device *dev; |
1f9ffc049 Driver core: add ... |
229 |
dev = bus_find_device_by_name(bus, NULL, buf); |
b8c5cec23 Driver core: udev... |
230 231 232 233 234 235 |
if (!dev) return -ENODEV; if (bus_rescan_devices_helper(dev, NULL) != 0) return -EINVAL; return count; } |
2139bdd5b [PATCH] drivers/b... |
236 |
#endif |
151ef38f7 [PATCH] driver co... |
237 |
|
4a3ad20cc Driver core: codi... |
238 |
static struct device *next_device(struct klist_iter *i) |
465c7a3a3 [PATCH] Add a kli... |
239 |
{ |
4a3ad20cc Driver core: codi... |
240 |
struct klist_node *n = klist_next(i); |
ae1b41715 driver core: move... |
241 242 243 244 245 246 247 248 |
struct device *dev = NULL; struct device_private *dev_prv; if (n) { dev_prv = to_device_private_bus(n); dev = dev_prv->device; } return dev; |
465c7a3a3 [PATCH] Add a kli... |
249 |
} |
1da177e4c Linux-2.6.12-rc2 |
250 |
/** |
4a3ad20cc Driver core: codi... |
251 252 253 254 255 |
* bus_for_each_dev - device iterator. * @bus: bus type. * @start: device to start iterating from. * @data: data for the callback. * @fn: function to be called for each device. |
1da177e4c Linux-2.6.12-rc2 |
256 |
* |
4a3ad20cc Driver core: codi... |
257 258 259 |
* Iterate over @bus's list of devices, and call @fn for each, * passing it @data. If @start is not NULL, we use that device to * begin iterating from. |
1da177e4c Linux-2.6.12-rc2 |
260 |
* |
4a3ad20cc Driver core: codi... |
261 262 |
* We check the return of @fn each time. If it returns anything * other than 0, we break out and return that value. |
1da177e4c Linux-2.6.12-rc2 |
263 |
* |
4a3ad20cc Driver core: codi... |
264 265 |
* NOTE: The device that returns a non-zero value is not retained * in any way, nor is its refcount incremented. If the caller needs |
0fa1b0a14 trivial: fix gram... |
266 |
* to retain this data, it should do so, and increment the reference |
4a3ad20cc Driver core: codi... |
267 |
* count in the supplied callback. |
1da177e4c Linux-2.6.12-rc2 |
268 |
*/ |
4a3ad20cc Driver core: codi... |
269 270 |
int bus_for_each_dev(struct bus_type *bus, struct device *start, void *data, int (*fn)(struct device *, void *)) |
1da177e4c Linux-2.6.12-rc2 |
271 |
{ |
465c7a3a3 [PATCH] Add a kli... |
272 |
struct klist_iter i; |
4a3ad20cc Driver core: codi... |
273 |
struct device *dev; |
465c7a3a3 [PATCH] Add a kli... |
274 |
int error = 0; |
1da177e4c Linux-2.6.12-rc2 |
275 |
|
465c7a3a3 [PATCH] Add a kli... |
276 277 |
if (!bus) return -EINVAL; |
c6f7e72a3 driver core: remo... |
278 |
klist_iter_init_node(&bus->p->klist_devices, &i, |
ae1b41715 driver core: move... |
279 |
(start ? &start->p->knode_bus : NULL)); |
465c7a3a3 [PATCH] Add a kli... |
280 281 282 283 |
while ((dev = next_device(&i)) && !error) error = fn(dev, data); klist_iter_exit(&i); return error; |
1da177e4c Linux-2.6.12-rc2 |
284 |
} |
4a3ad20cc Driver core: codi... |
285 |
EXPORT_SYMBOL_GPL(bus_for_each_dev); |
1da177e4c Linux-2.6.12-rc2 |
286 |
|
0edb58604 [PATCH] driver co... |
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
/** * bus_find_device - device iterator for locating a particular device. * @bus: bus type * @start: Device to begin with * @data: Data to pass to match function * @match: Callback function to check device * * This is similar to the bus_for_each_dev() 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... |
302 303 304 |
struct device *bus_find_device(struct bus_type *bus, struct device *start, void *data, int (*match)(struct device *dev, void *data)) |
0edb58604 [PATCH] driver co... |
305 306 307 308 309 310 |
{ struct klist_iter i; struct device *dev; if (!bus) return NULL; |
c6f7e72a3 driver core: remo... |
311 |
klist_iter_init_node(&bus->p->klist_devices, &i, |
ae1b41715 driver core: move... |
312 |
(start ? &start->p->knode_bus : NULL)); |
0edb58604 [PATCH] driver co... |
313 314 315 316 317 318 |
while ((dev = next_device(&i))) if (match(dev, data) && get_device(dev)) break; klist_iter_exit(&i); return dev; } |
4a3ad20cc Driver core: codi... |
319 |
EXPORT_SYMBOL_GPL(bus_find_device); |
38fdac3cd [PATCH] Add a kli... |
320 |
|
1f9ffc049 Driver core: add ... |
321 322 323 |
static int match_name(struct device *dev, void *data) { const char *name = data; |
1e0b2cf93 driver core: stru... |
324 |
return sysfs_streq(name, dev_name(dev)); |
1f9ffc049 Driver core: add ... |
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
} /** * bus_find_device_by_name - device iterator for locating a particular device of a specific name * @bus: bus type * @start: Device to begin with * @name: name of the device to match * * This is similar to the bus_find_device() function above, but it handles * searching by a name automatically, no need to write another strcmp matching * function. */ struct device *bus_find_device_by_name(struct bus_type *bus, struct device *start, const char *name) { return bus_find_device(bus, start, (void *)name, match_name); } EXPORT_SYMBOL_GPL(bus_find_device_by_name); |
ca22e56de driver-core: impl... |
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
/** * subsys_find_device_by_id - find a device with a specific enumeration number * @subsys: subsystem * @id: index 'id' in struct device * @hint: device to check first * * Check the hint's next object and if it is a match return it directly, * otherwise, fall back to a full list search. Either way a reference for * the returned object is taken. */ struct device *subsys_find_device_by_id(struct bus_type *subsys, unsigned int id, struct device *hint) { struct klist_iter i; struct device *dev; if (!subsys) return NULL; if (hint) { klist_iter_init_node(&subsys->p->klist_devices, &i, &hint->p->knode_bus); dev = next_device(&i); if (dev && dev->id == id && get_device(dev)) { klist_iter_exit(&i); return dev; } klist_iter_exit(&i); } klist_iter_init_node(&subsys->p->klist_devices, &i, NULL); while ((dev = next_device(&i))) { if (dev->id == id && get_device(dev)) { klist_iter_exit(&i); return dev; } } klist_iter_exit(&i); return NULL; } EXPORT_SYMBOL_GPL(subsys_find_device_by_id); |
4a3ad20cc Driver core: codi... |
383 |
static struct device_driver *next_driver(struct klist_iter *i) |
38fdac3cd [PATCH] Add a kli... |
384 |
{ |
4a3ad20cc Driver core: codi... |
385 |
struct klist_node *n = klist_next(i); |
e5dd12784 Driver core: move... |
386 387 388 389 390 391 392 |
struct driver_private *drv_priv; if (n) { drv_priv = container_of(n, struct driver_private, knode_bus); return drv_priv->driver; } return NULL; |
38fdac3cd [PATCH] Add a kli... |
393 |
} |
1da177e4c Linux-2.6.12-rc2 |
394 |
/** |
4a3ad20cc Driver core: codi... |
395 396 397 398 399 |
* bus_for_each_drv - driver iterator * @bus: bus we're dealing with. * @start: driver to start iterating on. * @data: data to pass to the callback. * @fn: function to call for each driver. |
1da177e4c Linux-2.6.12-rc2 |
400 |
* |
4a3ad20cc Driver core: codi... |
401 402 403 404 405 |
* This is nearly identical to the device iterator above. * We iterate over each driver that belongs to @bus, and call * @fn for each. If @fn returns anything but 0, we break out * and return it. If @start is not NULL, we use it as the head * of the list. |
1da177e4c Linux-2.6.12-rc2 |
406 |
* |
4a3ad20cc Driver core: codi... |
407 408 409 410 411 |
* NOTE: we don't return the driver that returns a non-zero * value, nor do we leave the reference count incremented for that * driver. If the caller needs to know that info, it must set it * in the callback. It must also be sure to increment the refcount * so it doesn't disappear before returning to the caller. |
1da177e4c Linux-2.6.12-rc2 |
412 |
*/ |
4a3ad20cc Driver core: codi... |
413 414 |
int bus_for_each_drv(struct bus_type *bus, struct device_driver *start, void *data, int (*fn)(struct device_driver *, void *)) |
1da177e4c Linux-2.6.12-rc2 |
415 |
{ |
38fdac3cd [PATCH] Add a kli... |
416 |
struct klist_iter i; |
4a3ad20cc Driver core: codi... |
417 |
struct device_driver *drv; |
38fdac3cd [PATCH] Add a kli... |
418 |
int error = 0; |
1da177e4c Linux-2.6.12-rc2 |
419 |
|
38fdac3cd [PATCH] Add a kli... |
420 421 |
if (!bus) return -EINVAL; |
c6f7e72a3 driver core: remo... |
422 |
klist_iter_init_node(&bus->p->klist_drivers, &i, |
e5dd12784 Driver core: move... |
423 |
start ? &start->p->knode_bus : NULL); |
38fdac3cd [PATCH] Add a kli... |
424 425 426 427 |
while ((drv = next_driver(&i)) && !error) error = fn(drv, data); klist_iter_exit(&i); return error; |
1da177e4c Linux-2.6.12-rc2 |
428 |
} |
4a3ad20cc Driver core: codi... |
429 |
EXPORT_SYMBOL_GPL(bus_for_each_drv); |
1da177e4c Linux-2.6.12-rc2 |
430 |
|
4aca67e5f Driver core: devi... |
431 |
static int device_add_attrs(struct bus_type *bus, struct device *dev) |
1da177e4c Linux-2.6.12-rc2 |
432 433 434 |
{ int error = 0; int i; |
4aca67e5f Driver core: devi... |
435 436 437 438 |
if (!bus->dev_attrs) return 0; for (i = 0; attr_name(bus->dev_attrs[i]); i++) { |
4a3ad20cc Driver core: codi... |
439 |
error = device_create_file(dev, &bus->dev_attrs[i]); |
4aca67e5f Driver core: devi... |
440 441 442 443 |
if (error) { while (--i >= 0) device_remove_file(dev, &bus->dev_attrs[i]); break; |
1da177e4c Linux-2.6.12-rc2 |
444 445 |
} } |
1da177e4c Linux-2.6.12-rc2 |
446 |
return error; |
1da177e4c Linux-2.6.12-rc2 |
447 |
} |
4a3ad20cc Driver core: codi... |
448 |
static void device_remove_attrs(struct bus_type *bus, struct device *dev) |
1da177e4c Linux-2.6.12-rc2 |
449 450 451 452 453 |
{ int i; if (bus->dev_attrs) { for (i = 0; attr_name(bus->dev_attrs[i]); i++) |
4a3ad20cc Driver core: codi... |
454 |
device_remove_file(dev, &bus->dev_attrs[i]); |
1da177e4c Linux-2.6.12-rc2 |
455 456 |
} } |
1da177e4c Linux-2.6.12-rc2 |
457 |
/** |
4a3ad20cc Driver core: codi... |
458 459 |
* bus_add_device - add device to bus * @dev: device being added |
1da177e4c Linux-2.6.12-rc2 |
460 |
* |
2023c610d Driver core: add ... |
461 462 |
* - Add device's bus attributes. * - Create links to device's bus. |
4a3ad20cc Driver core: codi... |
463 |
* - Add the device to its bus's list of devices. |
1da177e4c Linux-2.6.12-rc2 |
464 |
*/ |
4a3ad20cc Driver core: codi... |
465 |
int bus_add_device(struct device *dev) |
1da177e4c Linux-2.6.12-rc2 |
466 |
{ |
4a3ad20cc Driver core: codi... |
467 |
struct bus_type *bus = bus_get(dev->bus); |
1da177e4c Linux-2.6.12-rc2 |
468 469 470 |
int error = 0; if (bus) { |
1e0b2cf93 driver core: stru... |
471 472 |
pr_debug("bus: '%s': add device %s ", bus->name, dev_name(dev)); |
d377e85b5 [PATCH] driver co... |
473 |
error = device_add_attrs(bus, dev); |
f86db396f drivers/base: che... |
474 |
if (error) |
513e7337a driver core fixes... |
475 |
goto out_put; |
c6f7e72a3 driver core: remo... |
476 |
error = sysfs_create_link(&bus->p->devices_kset->kobj, |
1e0b2cf93 driver core: stru... |
477 |
&dev->kobj, dev_name(dev)); |
f86db396f drivers/base: che... |
478 |
if (error) |
513e7337a driver core fixes... |
479 |
goto out_id; |
f86db396f drivers/base: che... |
480 |
error = sysfs_create_link(&dev->kobj, |
c6f7e72a3 driver core: remo... |
481 |
&dev->bus->p->subsys.kobj, "subsystem"); |
f86db396f drivers/base: che... |
482 |
if (error) |
513e7337a driver core fixes... |
483 |
goto out_subsys; |
2023c610d Driver core: add ... |
484 |
klist_add_tail(&dev->p->knode_bus, &bus->p->klist_devices); |
1da177e4c Linux-2.6.12-rc2 |
485 |
} |
513e7337a driver core fixes... |
486 |
return 0; |
513e7337a driver core fixes... |
487 |
out_subsys: |
1e0b2cf93 driver core: stru... |
488 |
sysfs_remove_link(&bus->p->devices_kset->kobj, dev_name(dev)); |
513e7337a driver core fixes... |
489 490 491 |
out_id: device_remove_attrs(bus, dev); out_put: |
fc1ede588 Driver core: remo... |
492 |
bus_put(dev->bus); |
1da177e4c Linux-2.6.12-rc2 |
493 494 495 496 |
return error; } /** |
2023c610d Driver core: add ... |
497 498 |
* bus_probe_device - probe drivers for a new device * @dev: device to probe |
53877d06d [PATCH] Driver co... |
499 |
* |
2023c610d Driver core: add ... |
500 |
* - Automatically probe for a driver if the bus allows it. |
53877d06d [PATCH] Driver co... |
501 |
*/ |
2023c610d Driver core: add ... |
502 |
void bus_probe_device(struct device *dev) |
53877d06d [PATCH] Driver co... |
503 |
{ |
f86db396f drivers/base: che... |
504 |
struct bus_type *bus = dev->bus; |
ca22e56de driver-core: impl... |
505 |
struct subsys_interface *sif; |
2023c610d Driver core: add ... |
506 |
int ret; |
53877d06d [PATCH] Driver co... |
507 |
|
ca22e56de driver-core: impl... |
508 509 510 511 |
if (!bus) return; if (bus->p->drivers_autoprobe) { |
2023c610d Driver core: add ... |
512 |
ret = device_attach(dev); |
c6a46696f driver core: don'... |
513 |
WARN_ON(ret < 0); |
53877d06d [PATCH] Driver co... |
514 |
} |
ca22e56de driver-core: impl... |
515 516 517 518 519 520 |
mutex_lock(&bus->p->mutex); list_for_each_entry(sif, &bus->p->interfaces, node) if (sif->add_dev) sif->add_dev(dev, sif); mutex_unlock(&bus->p->mutex); |
53877d06d [PATCH] Driver co... |
521 522 523 |
} /** |
4a3ad20cc Driver core: codi... |
524 525 |
* bus_remove_device - remove device from bus * @dev: device to be removed |
1da177e4c Linux-2.6.12-rc2 |
526 |
* |
ca22e56de driver-core: impl... |
527 528 |
* - Remove device from all interfaces. * - Remove symlink from bus' directory. |
4a3ad20cc Driver core: codi... |
529 530 531 |
* - Delete device from bus's list. * - Detach from its driver. * - Drop reference taken in bus_add_device(). |
1da177e4c Linux-2.6.12-rc2 |
532 |
*/ |
4a3ad20cc Driver core: codi... |
533 |
void bus_remove_device(struct device *dev) |
1da177e4c Linux-2.6.12-rc2 |
534 |
{ |
ca22e56de driver-core: impl... |
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 |
struct bus_type *bus = dev->bus; struct subsys_interface *sif; if (!bus) return; mutex_lock(&bus->p->mutex); list_for_each_entry(sif, &bus->p->interfaces, node) if (sif->remove_dev) sif->remove_dev(dev, sif); mutex_unlock(&bus->p->mutex); sysfs_remove_link(&dev->kobj, "subsystem"); sysfs_remove_link(&dev->bus->p->devices_kset->kobj, dev_name(dev)); device_remove_attrs(dev->bus, dev); if (klist_node_attached(&dev->p->knode_bus)) klist_del(&dev->p->knode_bus); pr_debug("bus: '%s': remove device %s ", dev->bus->name, dev_name(dev)); device_release_driver(dev); bus_put(dev->bus); |
1da177e4c Linux-2.6.12-rc2 |
559 |
} |
4a3ad20cc Driver core: codi... |
560 |
static int driver_add_attrs(struct bus_type *bus, struct device_driver *drv) |
1da177e4c Linux-2.6.12-rc2 |
561 562 563 564 565 566 567 568 |
{ int error = 0; int i; if (bus->drv_attrs) { for (i = 0; attr_name(bus->drv_attrs[i]); i++) { error = driver_create_file(drv, &bus->drv_attrs[i]); if (error) |
4a3ad20cc Driver core: codi... |
569 |
goto err; |
1da177e4c Linux-2.6.12-rc2 |
570 571 |
} } |
4a3ad20cc Driver core: codi... |
572 |
done: |
1da177e4c Linux-2.6.12-rc2 |
573 |
return error; |
4a3ad20cc Driver core: codi... |
574 |
err: |
1da177e4c Linux-2.6.12-rc2 |
575 576 |
while (--i >= 0) driver_remove_file(drv, &bus->drv_attrs[i]); |
4a3ad20cc Driver core: codi... |
577 |
goto done; |
1da177e4c Linux-2.6.12-rc2 |
578 |
} |
4a3ad20cc Driver core: codi... |
579 580 |
static void driver_remove_attrs(struct bus_type *bus, struct device_driver *drv) |
1da177e4c Linux-2.6.12-rc2 |
581 582 583 584 585 586 587 588 |
{ int i; if (bus->drv_attrs) { for (i = 0; attr_name(bus->drv_attrs[i]); i++) driver_remove_file(drv, &bus->drv_attrs[i]); } } |
874c6241b [PATCH] Driver co... |
589 590 591 592 593 |
#ifdef CONFIG_HOTPLUG /* * Thanks to drivers making their tables __devinit, we can't allow manual * bind and unbind from userspace unless CONFIG_HOTPLUG is enabled. */ |
f86db396f drivers/base: che... |
594 |
static int __must_check add_bind_files(struct device_driver *drv) |
874c6241b [PATCH] Driver co... |
595 |
{ |
f86db396f drivers/base: che... |
596 597 598 599 600 601 602 603 604 |
int ret; ret = driver_create_file(drv, &driver_attr_unbind); if (ret == 0) { ret = driver_create_file(drv, &driver_attr_bind); if (ret) driver_remove_file(drv, &driver_attr_unbind); } return ret; |
874c6241b [PATCH] Driver co... |
605 606 607 608 609 610 611 |
} static void remove_bind_files(struct device_driver *drv) { driver_remove_file(drv, &driver_attr_bind); driver_remove_file(drv, &driver_attr_unbind); } |
b8c5cec23 Driver core: udev... |
612 |
|
8380770c8 Driver core: make... |
613 614 615 |
static BUS_ATTR(drivers_probe, S_IWUSR, NULL, store_drivers_probe); static BUS_ATTR(drivers_autoprobe, S_IWUSR | S_IRUGO, show_drivers_autoprobe, store_drivers_autoprobe); |
b8c5cec23 Driver core: udev... |
616 617 618 |
static int add_probe_files(struct bus_type *bus) { int retval; |
8380770c8 Driver core: make... |
619 |
retval = bus_create_file(bus, &bus_attr_drivers_probe); |
b8c5cec23 Driver core: udev... |
620 621 |
if (retval) goto out; |
8380770c8 Driver core: make... |
622 |
retval = bus_create_file(bus, &bus_attr_drivers_autoprobe); |
b8c5cec23 Driver core: udev... |
623 |
if (retval) |
8380770c8 Driver core: make... |
624 |
bus_remove_file(bus, &bus_attr_drivers_probe); |
b8c5cec23 Driver core: udev... |
625 626 627 628 629 630 |
out: return retval; } static void remove_probe_files(struct bus_type *bus) { |
8380770c8 Driver core: make... |
631 632 |
bus_remove_file(bus, &bus_attr_drivers_autoprobe); bus_remove_file(bus, &bus_attr_drivers_probe); |
b8c5cec23 Driver core: udev... |
633 |
} |
874c6241b [PATCH] Driver co... |
634 |
#else |
35acfdd72 Driver core: fixe... |
635 |
static inline int add_bind_files(struct device_driver *drv) { return 0; } |
874c6241b [PATCH] Driver co... |
636 |
static inline void remove_bind_files(struct device_driver *drv) {} |
b8c5cec23 Driver core: udev... |
637 638 |
static inline int add_probe_files(struct bus_type *bus) { return 0; } static inline void remove_probe_files(struct bus_type *bus) {} |
874c6241b [PATCH] Driver co... |
639 |
#endif |
1da177e4c Linux-2.6.12-rc2 |
640 |
|
7ac1cf4a8 Driver core: add ... |
641 642 643 644 645 646 |
static ssize_t driver_uevent_store(struct device_driver *drv, const char *buf, size_t count) { enum kobject_action action; if (kobject_action_type(buf, count, &action) == 0) |
e5dd12784 Driver core: move... |
647 |
kobject_uevent(&drv->p->kobj, action); |
7ac1cf4a8 Driver core: add ... |
648 649 650 |
return count; } static DRIVER_ATTR(uevent, S_IWUSR, NULL, driver_uevent_store); |
1da177e4c Linux-2.6.12-rc2 |
651 |
/** |
4a3ad20cc Driver core: codi... |
652 653 |
* bus_add_driver - Add a driver to the bus. * @drv: driver. |
1da177e4c Linux-2.6.12-rc2 |
654 |
*/ |
f86db396f drivers/base: che... |
655 |
int bus_add_driver(struct device_driver *drv) |
1da177e4c Linux-2.6.12-rc2 |
656 |
{ |
e5dd12784 Driver core: move... |
657 658 |
struct bus_type *bus; struct driver_private *priv; |
1da177e4c Linux-2.6.12-rc2 |
659 |
int error = 0; |
e5dd12784 Driver core: move... |
660 |
bus = bus_get(drv->bus); |
d9fd4d3b3 Driver core: bus:... |
661 |
if (!bus) |
4f6e1945f driver core: bus_... |
662 |
return -EINVAL; |
d9fd4d3b3 Driver core: bus:... |
663 |
|
7dc72b284 Driver core: clea... |
664 665 |
pr_debug("bus: '%s': add driver %s ", bus->name, drv->name); |
e5dd12784 Driver core: move... |
666 667 |
priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
076344642 Driver core: Fix ... |
668 669 670 671 |
if (!priv) { error = -ENOMEM; goto out_put_bus; } |
e5dd12784 Driver core: move... |
672 673 674 |
klist_init(&priv->klist_devices, NULL, NULL); priv->driver = drv; drv->p = priv; |
c8e90d822 Kobject: change d... |
675 676 677 |
priv->kobj.kset = bus->p->drivers_kset; error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL, "%s", drv->name); |
dc0afa838 Driver core: codi... |
678 |
if (error) |
076344642 Driver core: Fix ... |
679 |
goto out_unregister; |
d9fd4d3b3 Driver core: bus:... |
680 |
|
c6f7e72a3 driver core: remo... |
681 |
if (drv->bus->p->drivers_autoprobe) { |
b8c5cec23 Driver core: udev... |
682 683 684 685 |
error = driver_attach(drv); if (error) goto out_unregister; } |
e5dd12784 Driver core: move... |
686 |
klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers); |
d9fd4d3b3 Driver core: bus:... |
687 |
module_add_driver(drv->owner, drv); |
7ac1cf4a8 Driver core: add ... |
688 689 690 691 |
error = driver_create_file(drv, &driver_attr_uevent); if (error) { printk(KERN_ERR "%s: uevent attr (%s) failed ", |
2b3a302a0 driver core: repl... |
692 |
__func__, drv->name); |
7ac1cf4a8 Driver core: add ... |
693 |
} |
d9fd4d3b3 Driver core: bus:... |
694 695 696 697 698 |
error = driver_add_attrs(bus, drv); if (error) { /* How the hell do we get out of this pickle? Give up */ printk(KERN_ERR "%s: driver_add_attrs(%s) failed ", |
2b3a302a0 driver core: repl... |
699 |
__func__, drv->name); |
d9fd4d3b3 Driver core: bus:... |
700 |
} |
1a6f2a751 Driver core: allo... |
701 702 703 704 705 706 707 708 709 |
if (!drv->suppress_bind_attrs) { error = add_bind_files(drv); if (error) { /* Ditto */ printk(KERN_ERR "%s: add_bind_files(%s) failed ", __func__, drv->name); } |
1da177e4c Linux-2.6.12-rc2 |
710 |
} |
d9fd4d3b3 Driver core: bus:... |
711 |
|
c8e90d822 Kobject: change d... |
712 |
kobject_uevent(&priv->kobj, KOBJ_ADD); |
5c8563d77 Driver Core: do n... |
713 |
return 0; |
1a6f2a751 Driver core: allo... |
714 |
|
f86db396f drivers/base: che... |
715 |
out_unregister: |
99b28f1b4 driver core: Prev... |
716 |
kobject_put(&priv->kobj); |
5c8563d77 Driver Core: do n... |
717 718 |
kfree(drv->p); drv->p = NULL; |
f86db396f drivers/base: che... |
719 |
out_put_bus: |
fc1ede588 Driver core: remo... |
720 |
bus_put(bus); |
f86db396f drivers/base: che... |
721 |
return error; |
1da177e4c Linux-2.6.12-rc2 |
722 |
} |
1da177e4c Linux-2.6.12-rc2 |
723 |
/** |
4a3ad20cc Driver core: codi... |
724 725 |
* bus_remove_driver - delete driver from bus's knowledge. * @drv: driver. |
1da177e4c Linux-2.6.12-rc2 |
726 |
* |
4a3ad20cc Driver core: codi... |
727 728 729 |
* Detach the driver from the devices it controls, and remove * it from its bus's list of drivers. Finally, we drop the reference * to the bus we took in bus_add_driver(). |
1da177e4c Linux-2.6.12-rc2 |
730 |
*/ |
4a3ad20cc Driver core: codi... |
731 |
void bus_remove_driver(struct device_driver *drv) |
1da177e4c Linux-2.6.12-rc2 |
732 |
{ |
d9fd4d3b3 Driver core: bus:... |
733 734 |
if (!drv->bus) return; |
1a6f2a751 Driver core: allo... |
735 736 |
if (!drv->suppress_bind_attrs) remove_bind_files(drv); |
d9fd4d3b3 Driver core: bus:... |
737 |
driver_remove_attrs(drv->bus, drv); |
7ac1cf4a8 Driver core: add ... |
738 |
driver_remove_file(drv, &driver_attr_uevent); |
e5dd12784 Driver core: move... |
739 |
klist_remove(&drv->p->knode_bus); |
7dc72b284 Driver core: clea... |
740 741 |
pr_debug("bus: '%s': remove driver %s ", drv->bus->name, drv->name); |
d9fd4d3b3 Driver core: bus:... |
742 743 |
driver_detach(drv); module_remove_driver(drv); |
c10997f65 Kobject: convert ... |
744 |
kobject_put(&drv->p->kobj); |
fc1ede588 Driver core: remo... |
745 |
bus_put(drv->bus); |
1da177e4c Linux-2.6.12-rc2 |
746 |
} |
1da177e4c Linux-2.6.12-rc2 |
747 |
/* Helper for bus_rescan_devices's iter */ |
f86db396f drivers/base: che... |
748 |
static int __must_check bus_rescan_devices_helper(struct device *dev, |
4a3ad20cc Driver core: codi... |
749 |
void *data) |
1da177e4c Linux-2.6.12-rc2 |
750 |
{ |
f86db396f drivers/base: che... |
751 |
int ret = 0; |
bf74ad5bc [PATCH] Hold the ... |
752 753 |
if (!dev->driver) { if (dev->parent) /* Needed for USB */ |
8e9394ce2 Driver core: crea... |
754 |
device_lock(dev->parent); |
f86db396f drivers/base: che... |
755 |
ret = device_attach(dev); |
bf74ad5bc [PATCH] Hold the ... |
756 |
if (dev->parent) |
8e9394ce2 Driver core: crea... |
757 |
device_unlock(dev->parent); |
bf74ad5bc [PATCH] Hold the ... |
758 |
} |
f86db396f drivers/base: che... |
759 |
return ret < 0 ? ret : 0; |
1da177e4c Linux-2.6.12-rc2 |
760 |
} |
1da177e4c Linux-2.6.12-rc2 |
761 |
/** |
23d3d602c [PATCH] driver co... |
762 763 |
* bus_rescan_devices - rescan devices on the bus for possible drivers * @bus: the bus to scan. |
1da177e4c Linux-2.6.12-rc2 |
764 |
* |
23d3d602c [PATCH] driver co... |
765 766 767 |
* This function will look for devices on the bus with no driver * attached and rescan it against existing drivers to see if it matches * any by calling device_attach() for the unbound devices. |
1da177e4c Linux-2.6.12-rc2 |
768 |
*/ |
4a3ad20cc Driver core: codi... |
769 |
int bus_rescan_devices(struct bus_type *bus) |
1da177e4c Linux-2.6.12-rc2 |
770 |
{ |
f86db396f drivers/base: che... |
771 |
return bus_for_each_dev(bus, NULL, NULL, bus_rescan_devices_helper); |
1da177e4c Linux-2.6.12-rc2 |
772 |
} |
4a3ad20cc Driver core: codi... |
773 |
EXPORT_SYMBOL_GPL(bus_rescan_devices); |
1da177e4c Linux-2.6.12-rc2 |
774 |
|
e935d5da8 [SCSI] drivers/ba... |
775 776 777 778 779 780 781 782 783 |
/** * device_reprobe - remove driver for a device and probe for a new driver * @dev: the device to reprobe * * This function detaches the attached driver (if any) for the given * device and restarts the driver probing process. It is intended * to use if probing criteria changed during a devices lifetime and * driver attachment should change accordingly. */ |
f86db396f drivers/base: che... |
784 |
int device_reprobe(struct device *dev) |
e935d5da8 [SCSI] drivers/ba... |
785 786 787 |
{ if (dev->driver) { if (dev->parent) /* Needed for USB */ |
8e9394ce2 Driver core: crea... |
788 |
device_lock(dev->parent); |
e935d5da8 [SCSI] drivers/ba... |
789 790 |
device_release_driver(dev); if (dev->parent) |
8e9394ce2 Driver core: crea... |
791 |
device_unlock(dev->parent); |
e935d5da8 [SCSI] drivers/ba... |
792 |
} |
f86db396f drivers/base: che... |
793 |
return bus_rescan_devices_helper(dev, NULL); |
e935d5da8 [SCSI] drivers/ba... |
794 795 |
} EXPORT_SYMBOL_GPL(device_reprobe); |
1da177e4c Linux-2.6.12-rc2 |
796 |
|
1da177e4c Linux-2.6.12-rc2 |
797 |
/** |
4a3ad20cc Driver core: codi... |
798 799 |
* find_bus - locate bus by name. * @name: name of bus. |
1da177e4c Linux-2.6.12-rc2 |
800 |
* |
4a3ad20cc Driver core: codi... |
801 802 |
* Call kset_find_obj() to iterate over list of buses to * find a bus by name. Return bus if found. |
1da177e4c Linux-2.6.12-rc2 |
803 |
* |
4a3ad20cc Driver core: codi... |
804 |
* Note that kset_find_obj increments bus' reference count. |
1da177e4c Linux-2.6.12-rc2 |
805 |
*/ |
7e4ef085e [PATCH] Driver co... |
806 |
#if 0 |
4a3ad20cc Driver core: codi... |
807 |
struct bus_type *find_bus(char *name) |
1da177e4c Linux-2.6.12-rc2 |
808 |
{ |
4a3ad20cc Driver core: codi... |
809 |
struct kobject *k = kset_find_obj(bus_kset, name); |
1da177e4c Linux-2.6.12-rc2 |
810 811 |
return k ? to_bus(k) : NULL; } |
7e4ef085e [PATCH] Driver co... |
812 |
#endif /* 0 */ |
1da177e4c Linux-2.6.12-rc2 |
813 814 815 |
/** |
4a3ad20cc Driver core: codi... |
816 817 |
* bus_add_attrs - Add default attributes for this bus. * @bus: Bus that has just been registered. |
1da177e4c Linux-2.6.12-rc2 |
818 |
*/ |
4a3ad20cc Driver core: codi... |
819 |
static int bus_add_attrs(struct bus_type *bus) |
1da177e4c Linux-2.6.12-rc2 |
820 821 822 823 824 825 |
{ int error = 0; int i; if (bus->bus_attrs) { for (i = 0; attr_name(bus->bus_attrs[i]); i++) { |
4a3ad20cc Driver core: codi... |
826 |
error = bus_create_file(bus, &bus->bus_attrs[i]); |
dc0afa838 Driver core: codi... |
827 |
if (error) |
4a3ad20cc Driver core: codi... |
828 |
goto err; |
1da177e4c Linux-2.6.12-rc2 |
829 830 |
} } |
4a3ad20cc Driver core: codi... |
831 |
done: |
1da177e4c Linux-2.6.12-rc2 |
832 |
return error; |
4a3ad20cc Driver core: codi... |
833 |
err: |
1da177e4c Linux-2.6.12-rc2 |
834 |
while (--i >= 0) |
4a3ad20cc Driver core: codi... |
835 836 |
bus_remove_file(bus, &bus->bus_attrs[i]); goto done; |
1da177e4c Linux-2.6.12-rc2 |
837 |
} |
4a3ad20cc Driver core: codi... |
838 |
static void bus_remove_attrs(struct bus_type *bus) |
1da177e4c Linux-2.6.12-rc2 |
839 840 841 842 843 |
{ int i; if (bus->bus_attrs) { for (i = 0; attr_name(bus->bus_attrs[i]); i++) |
4a3ad20cc Driver core: codi... |
844 |
bus_remove_file(bus, &bus->bus_attrs[i]); |
1da177e4c Linux-2.6.12-rc2 |
845 846 |
} } |
34bb61f9d [PATCH] fix klist... |
847 848 |
static void klist_devices_get(struct klist_node *n) { |
ae1b41715 driver core: move... |
849 850 |
struct device_private *dev_prv = to_device_private_bus(n); struct device *dev = dev_prv->device; |
34bb61f9d [PATCH] fix klist... |
851 852 853 854 855 856 |
get_device(dev); } static void klist_devices_put(struct klist_node *n) { |
ae1b41715 driver core: move... |
857 858 |
struct device_private *dev_prv = to_device_private_bus(n); struct device *dev = dev_prv->device; |
34bb61f9d [PATCH] fix klist... |
859 860 861 |
put_device(dev); } |
7ac1cf4a8 Driver core: add ... |
862 863 864 865 866 867 |
static ssize_t bus_uevent_store(struct bus_type *bus, const char *buf, size_t count) { enum kobject_action action; if (kobject_action_type(buf, count, &action) == 0) |
c6f7e72a3 driver core: remo... |
868 |
kobject_uevent(&bus->p->subsys.kobj, action); |
7ac1cf4a8 Driver core: add ... |
869 870 871 |
return count; } static BUS_ATTR(uevent, S_IWUSR, NULL, bus_uevent_store); |
1da177e4c Linux-2.6.12-rc2 |
872 |
/** |
ca22e56de driver-core: impl... |
873 |
* __bus_register - register a driver-core subsystem |
4a3ad20cc Driver core: codi... |
874 |
* @bus: bus. |
1da177e4c Linux-2.6.12-rc2 |
875 |
* |
4a3ad20cc Driver core: codi... |
876 877 |
* Once we have that, we registered the bus with the kobject * infrastructure, then register the children subsystems it has: |
ca22e56de driver-core: impl... |
878 |
* the devices and drivers that belong to the subsystem. |
1da177e4c Linux-2.6.12-rc2 |
879 |
*/ |
ca22e56de driver-core: impl... |
880 |
int __bus_register(struct bus_type *bus, struct lock_class_key *key) |
1da177e4c Linux-2.6.12-rc2 |
881 882 |
{ int retval; |
6b6e39a6a driver-core: merg... |
883 |
struct subsys_private *priv; |
c6f7e72a3 driver core: remo... |
884 |
|
6b6e39a6a driver-core: merg... |
885 |
priv = kzalloc(sizeof(struct subsys_private), GFP_KERNEL); |
c6f7e72a3 driver core: remo... |
886 887 888 889 890 |
if (!priv) return -ENOMEM; priv->bus = bus; bus->p = priv; |
1da177e4c Linux-2.6.12-rc2 |
891 |
|
c6f7e72a3 driver core: remo... |
892 |
BLOCKING_INIT_NOTIFIER_HEAD(&priv->bus_notifier); |
116af3782 Driver core: add ... |
893 |
|
c6f7e72a3 driver core: remo... |
894 |
retval = kobject_set_name(&priv->subsys.kobj, "%s", bus->name); |
1da177e4c Linux-2.6.12-rc2 |
895 896 |
if (retval) goto out; |
c6f7e72a3 driver core: remo... |
897 898 899 |
priv->subsys.kobj.kset = bus_kset; priv->subsys.kobj.ktype = &bus_ktype; priv->drivers_autoprobe = 1; |
d6b05b84e Driver core: remo... |
900 |
|
c6f7e72a3 driver core: remo... |
901 |
retval = kset_register(&priv->subsys); |
1da177e4c Linux-2.6.12-rc2 |
902 903 |
if (retval) goto out; |
7ac1cf4a8 Driver core: add ... |
904 905 906 |
retval = bus_create_file(bus, &bus_attr_uevent); if (retval) goto bus_uevent_fail; |
c6f7e72a3 driver core: remo... |
907 908 909 |
priv->devices_kset = kset_create_and_add("devices", NULL, &priv->subsys.kobj); if (!priv->devices_kset) { |
3d8995963 kset: convert str... |
910 |
retval = -ENOMEM; |
1da177e4c Linux-2.6.12-rc2 |
911 |
goto bus_devices_fail; |
3d8995963 kset: convert str... |
912 |
} |
1da177e4c Linux-2.6.12-rc2 |
913 |
|
c6f7e72a3 driver core: remo... |
914 915 916 |
priv->drivers_kset = kset_create_and_add("drivers", NULL, &priv->subsys.kobj); if (!priv->drivers_kset) { |
6dcec2511 kset: convert str... |
917 |
retval = -ENOMEM; |
1da177e4c Linux-2.6.12-rc2 |
918 |
goto bus_drivers_fail; |
6dcec2511 kset: convert str... |
919 |
} |
465c7a3a3 [PATCH] Add a kli... |
920 |
|
ca22e56de driver-core: impl... |
921 922 |
INIT_LIST_HEAD(&priv->interfaces); __mutex_init(&priv->mutex, "subsys mutex", key); |
c6f7e72a3 driver core: remo... |
923 924 |
klist_init(&priv->klist_devices, klist_devices_get, klist_devices_put); klist_init(&priv->klist_drivers, NULL, NULL); |
b8c5cec23 Driver core: udev... |
925 |
|
b8c5cec23 Driver core: udev... |
926 927 928 |
retval = add_probe_files(bus); if (retval) goto bus_probe_files_fail; |
1bb6881ac driver core fixes... |
929 930 931 |
retval = bus_add_attrs(bus); if (retval) goto bus_attrs_fail; |
1da177e4c Linux-2.6.12-rc2 |
932 |
|
7dc72b284 Driver core: clea... |
933 934 |
pr_debug("bus: '%s': registered ", bus->name); |
1da177e4c Linux-2.6.12-rc2 |
935 |
return 0; |
1bb6881ac driver core fixes... |
936 |
bus_attrs_fail: |
b8c5cec23 Driver core: udev... |
937 938 |
remove_probe_files(bus); bus_probe_files_fail: |
c6f7e72a3 driver core: remo... |
939 |
kset_unregister(bus->p->drivers_kset); |
1da177e4c Linux-2.6.12-rc2 |
940 |
bus_drivers_fail: |
c6f7e72a3 driver core: remo... |
941 |
kset_unregister(bus->p->devices_kset); |
1da177e4c Linux-2.6.12-rc2 |
942 |
bus_devices_fail: |
7ac1cf4a8 Driver core: add ... |
943 944 |
bus_remove_file(bus, &bus_attr_uevent); bus_uevent_fail: |
c6f7e72a3 driver core: remo... |
945 |
kset_unregister(&bus->p->subsys); |
1da177e4c Linux-2.6.12-rc2 |
946 |
out: |
600c20f34 driver core: fix ... |
947 |
kfree(bus->p); |
f48f3febb driver-core: do n... |
948 |
bus->p = NULL; |
1da177e4c Linux-2.6.12-rc2 |
949 950 |
return retval; } |
ca22e56de driver-core: impl... |
951 |
EXPORT_SYMBOL_GPL(__bus_register); |
1da177e4c Linux-2.6.12-rc2 |
952 |
|
1da177e4c Linux-2.6.12-rc2 |
953 |
/** |
4a3ad20cc Driver core: codi... |
954 955 |
* bus_unregister - remove a bus from the system * @bus: bus. |
1da177e4c Linux-2.6.12-rc2 |
956 |
* |
4a3ad20cc Driver core: codi... |
957 958 |
* Unregister the child subsystems and the bus itself. * Finally, we call bus_put() to release the refcount |
1da177e4c Linux-2.6.12-rc2 |
959 |
*/ |
4a3ad20cc Driver core: codi... |
960 |
void bus_unregister(struct bus_type *bus) |
1da177e4c Linux-2.6.12-rc2 |
961 |
{ |
7dc72b284 Driver core: clea... |
962 963 |
pr_debug("bus: '%s': unregistering ", bus->name); |
ca22e56de driver-core: impl... |
964 965 |
if (bus->dev_root) device_unregister(bus->dev_root); |
1da177e4c Linux-2.6.12-rc2 |
966 |
bus_remove_attrs(bus); |
b8c5cec23 Driver core: udev... |
967 |
remove_probe_files(bus); |
c6f7e72a3 driver core: remo... |
968 969 |
kset_unregister(bus->p->drivers_kset); kset_unregister(bus->p->devices_kset); |
7ac1cf4a8 Driver core: add ... |
970 |
bus_remove_file(bus, &bus_attr_uevent); |
c6f7e72a3 driver core: remo... |
971 972 |
kset_unregister(&bus->p->subsys); kfree(bus->p); |
f48f3febb driver-core: do n... |
973 |
bus->p = NULL; |
1da177e4c Linux-2.6.12-rc2 |
974 |
} |
4a3ad20cc Driver core: codi... |
975 |
EXPORT_SYMBOL_GPL(bus_unregister); |
1da177e4c Linux-2.6.12-rc2 |
976 |
|
116af3782 Driver core: add ... |
977 978 |
int bus_register_notifier(struct bus_type *bus, struct notifier_block *nb) { |
c6f7e72a3 driver core: remo... |
979 |
return blocking_notifier_chain_register(&bus->p->bus_notifier, nb); |
116af3782 Driver core: add ... |
980 981 982 983 984 |
} EXPORT_SYMBOL_GPL(bus_register_notifier); int bus_unregister_notifier(struct bus_type *bus, struct notifier_block *nb) { |
c6f7e72a3 driver core: remo... |
985 |
return blocking_notifier_chain_unregister(&bus->p->bus_notifier, nb); |
116af3782 Driver core: add ... |
986 987 |
} EXPORT_SYMBOL_GPL(bus_unregister_notifier); |
0fed80f7a driver core: add ... |
988 989 |
struct kset *bus_get_kset(struct bus_type *bus) { |
c6f7e72a3 driver core: remo... |
990 |
return &bus->p->subsys; |
0fed80f7a driver core: add ... |
991 992 |
} EXPORT_SYMBOL_GPL(bus_get_kset); |
b249072ee driver core: add ... |
993 994 |
struct klist *bus_get_device_klist(struct bus_type *bus) { |
c6f7e72a3 driver core: remo... |
995 |
return &bus->p->klist_devices; |
b249072ee driver core: add ... |
996 997 |
} EXPORT_SYMBOL_GPL(bus_get_device_klist); |
99178b036 Driver core: add ... |
998 |
/* |
dca25ebdd Fix "forcably" co... |
999 |
* Yes, this forcibly breaks the klist abstraction temporarily. It |
99178b036 Driver core: add ... |
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 |
* just wants to sort the klist, not change reference counts and * take/drop locks rapidly in the process. It does all this while * holding the lock for the list, so objects can't otherwise be * added/removed while we're swizzling. */ static void device_insertion_sort_klist(struct device *a, struct list_head *list, int (*compare)(const struct device *a, const struct device *b)) { struct list_head *pos; struct klist_node *n; |
ae1b41715 driver core: move... |
1011 |
struct device_private *dev_prv; |
99178b036 Driver core: add ... |
1012 1013 1014 1015 |
struct device *b; list_for_each(pos, list) { n = container_of(pos, struct klist_node, n_node); |
ae1b41715 driver core: move... |
1016 1017 |
dev_prv = to_device_private_bus(n); b = dev_prv->device; |
99178b036 Driver core: add ... |
1018 |
if (compare(a, b) <= 0) { |
ae1b41715 driver core: move... |
1019 1020 |
list_move_tail(&a->p->knode_bus.n_node, &b->p->knode_bus.n_node); |
99178b036 Driver core: add ... |
1021 1022 1023 |
return; } } |
ae1b41715 driver core: move... |
1024 |
list_move_tail(&a->p->knode_bus.n_node, list); |
99178b036 Driver core: add ... |
1025 1026 1027 1028 1029 1030 1031 1032 1033 |
} void bus_sort_breadthfirst(struct bus_type *bus, int (*compare)(const struct device *a, const struct device *b)) { LIST_HEAD(sorted_devices); struct list_head *pos, *tmp; struct klist_node *n; |
ae1b41715 driver core: move... |
1034 |
struct device_private *dev_prv; |
99178b036 Driver core: add ... |
1035 1036 1037 1038 1039 1040 1041 1042 |
struct device *dev; struct klist *device_klist; device_klist = bus_get_device_klist(bus); spin_lock(&device_klist->k_lock); list_for_each_safe(pos, tmp, &device_klist->k_list) { n = container_of(pos, struct klist_node, n_node); |
ae1b41715 driver core: move... |
1043 1044 |
dev_prv = to_device_private_bus(n); dev = dev_prv->device; |
99178b036 Driver core: add ... |
1045 1046 1047 1048 1049 1050 |
device_insertion_sort_klist(dev, &sorted_devices, compare); } list_splice(&sorted_devices, &device_klist->k_list); spin_unlock(&device_klist->k_lock); } EXPORT_SYMBOL_GPL(bus_sort_breadthfirst); |
ca22e56de driver-core: impl... |
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 |
/** * subsys_dev_iter_init - initialize subsys device iterator * @iter: subsys iterator to initialize * @subsys: the subsys we wanna iterate over * @start: the device to start iterating from, if any * @type: device_type of the devices to iterate over, NULL for all * * Initialize subsys iterator @iter such that it iterates over devices * of @subsys. If @start is set, the list iteration will start there, * otherwise if it is NULL, the iteration starts at the beginning of * the list. */ void subsys_dev_iter_init(struct subsys_dev_iter *iter, struct bus_type *subsys, struct device *start, const struct device_type *type) { struct klist_node *start_knode = NULL; if (start) start_knode = &start->p->knode_bus; klist_iter_init_node(&subsys->p->klist_devices, &iter->ki, start_knode); iter->type = type; } EXPORT_SYMBOL_GPL(subsys_dev_iter_init); /** * subsys_dev_iter_next - iterate to the next device * @iter: subsys iterator to proceed * * Proceed @iter to the next device and return it. Returns NULL if * iteration is complete. * * The returned device is referenced and won't be released till * iterator is proceed to the next device or exited. The caller is * free to do whatever it wants to do with the device including * calling back into subsys code. */ struct device *subsys_dev_iter_next(struct subsys_dev_iter *iter) { struct klist_node *knode; struct device *dev; for (;;) { knode = klist_next(&iter->ki); if (!knode) return NULL; dev = container_of(knode, struct device_private, knode_bus)->device; if (!iter->type || iter->type == dev->type) return dev; } } EXPORT_SYMBOL_GPL(subsys_dev_iter_next); /** * subsys_dev_iter_exit - finish iteration * @iter: subsys iterator to finish * * Finish an iteration. Always call this function after iteration is * complete whether the iteration ran till the end or not. */ void subsys_dev_iter_exit(struct subsys_dev_iter *iter) { klist_iter_exit(&iter->ki); } EXPORT_SYMBOL_GPL(subsys_dev_iter_exit); int subsys_interface_register(struct subsys_interface *sif) { struct bus_type *subsys; struct subsys_dev_iter iter; struct device *dev; if (!sif || !sif->subsys) return -ENODEV; subsys = bus_get(sif->subsys); if (!subsys) return -EINVAL; mutex_lock(&subsys->p->mutex); list_add_tail(&sif->node, &subsys->p->interfaces); if (sif->add_dev) { subsys_dev_iter_init(&iter, subsys, NULL, NULL); while ((dev = subsys_dev_iter_next(&iter))) sif->add_dev(dev, sif); subsys_dev_iter_exit(&iter); } mutex_unlock(&subsys->p->mutex); return 0; } EXPORT_SYMBOL_GPL(subsys_interface_register); void subsys_interface_unregister(struct subsys_interface *sif) { struct bus_type *subsys = sif->subsys; struct subsys_dev_iter iter; struct device *dev; if (!sif) return; mutex_lock(&subsys->p->mutex); list_del_init(&sif->node); if (sif->remove_dev) { subsys_dev_iter_init(&iter, subsys, NULL, NULL); while ((dev = subsys_dev_iter_next(&iter))) sif->remove_dev(dev, sif); subsys_dev_iter_exit(&iter); } mutex_unlock(&subsys->p->mutex); bus_put(subsys); } EXPORT_SYMBOL_GPL(subsys_interface_unregister); static void system_root_device_release(struct device *dev) { kfree(dev); } /** * subsys_system_register - register a subsystem at /sys/devices/system/ * @subsys - system subsystem * @groups - default attributes for the root device * * All 'system' subsystems have a /sys/devices/system/<name> root device * with the name of the subsystem. The root device can carry subsystem- * wide attributes. All registered devices are below this single root * device and are named after the subsystem with a simple enumeration * number appended. The registered devices are not explicitely named; * only 'id' in the device needs to be set. * * Do not use this interface for anything new, it exists for compatibility * with bad ideas only. New subsystems should use plain subsystems; and * add the subsystem-wide attributes should be added to the subsystem * directory itself and not some create fake root-device placed in * /sys/devices/system/<name>. */ int subsys_system_register(struct bus_type *subsys, const struct attribute_group **groups) { struct device *dev; int err; err = bus_register(subsys); if (err < 0) return err; dev = kzalloc(sizeof(struct device), GFP_KERNEL); if (!dev) { err = -ENOMEM; goto err_dev; } err = dev_set_name(dev, "%s", subsys->name); if (err < 0) goto err_name; dev->kobj.parent = &system_kset->kobj; dev->groups = groups; dev->release = system_root_device_release; err = device_register(dev); if (err < 0) goto err_dev_reg; subsys->dev_root = dev; return 0; err_dev_reg: put_device(dev); dev = NULL; err_name: kfree(dev); err_dev: bus_unregister(subsys); return err; } EXPORT_SYMBOL_GPL(subsys_system_register); |
1da177e4c Linux-2.6.12-rc2 |
1229 1230 |
int __init buses_init(void) { |
59a548338 kset: convert dri... |
1231 1232 1233 |
bus_kset = kset_create_and_add("bus", &bus_uevent_ops, NULL); if (!bus_kset) return -ENOMEM; |
ca22e56de driver-core: impl... |
1234 1235 1236 1237 |
system_kset = kset_create_and_add("system", NULL, &devices_kset->kobj); if (!system_kset) return -ENOMEM; |
59a548338 kset: convert dri... |
1238 |
return 0; |
1da177e4c Linux-2.6.12-rc2 |
1239 |
} |