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