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