diff options
Diffstat (limited to 'drivers/base/bus.c')
-rw-r--r-- | drivers/base/bus.c | 299 |
1 files changed, 52 insertions, 247 deletions
diff --git a/drivers/base/bus.c b/drivers/base/bus.c index 3cb04bb04c2b..43722af90bdd 100644 --- a/drivers/base/bus.c +++ b/drivers/base/bus.c | |||
@@ -17,9 +17,6 @@ | |||
17 | #include "base.h" | 17 | #include "base.h" |
18 | #include "power/power.h" | 18 | #include "power/power.h" |
19 | 19 | ||
20 | #define to_dev(node) container_of(node, struct device, bus_list) | ||
21 | #define to_drv(node) container_of(node, struct device_driver, kobj.entry) | ||
22 | |||
23 | #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr) | 20 | #define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr) |
24 | #define to_bus(obj) container_of(obj, struct bus_type, subsys.kset.kobj) | 21 | #define to_bus(obj) container_of(obj, struct bus_type, subsys.kset.kobj) |
25 | 22 | ||
@@ -36,7 +33,7 @@ drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf) | |||
36 | { | 33 | { |
37 | struct driver_attribute * drv_attr = to_drv_attr(attr); | 34 | struct driver_attribute * drv_attr = to_drv_attr(attr); |
38 | struct device_driver * drv = to_driver(kobj); | 35 | struct device_driver * drv = to_driver(kobj); |
39 | ssize_t ret = 0; | 36 | ssize_t ret = -EIO; |
40 | 37 | ||
41 | if (drv_attr->show) | 38 | if (drv_attr->show) |
42 | ret = drv_attr->show(drv, buf); | 39 | ret = drv_attr->show(drv, buf); |
@@ -49,7 +46,7 @@ drv_attr_store(struct kobject * kobj, struct attribute * attr, | |||
49 | { | 46 | { |
50 | struct driver_attribute * drv_attr = to_drv_attr(attr); | 47 | struct driver_attribute * drv_attr = to_drv_attr(attr); |
51 | struct device_driver * drv = to_driver(kobj); | 48 | struct device_driver * drv = to_driver(kobj); |
52 | ssize_t ret = 0; | 49 | ssize_t ret = -EIO; |
53 | 50 | ||
54 | if (drv_attr->store) | 51 | if (drv_attr->store) |
55 | ret = drv_attr->store(drv, buf, count); | 52 | ret = drv_attr->store(drv, buf, count); |
@@ -135,50 +132,11 @@ static struct kobj_type ktype_bus = { | |||
135 | 132 | ||
136 | decl_subsys(bus, &ktype_bus, NULL); | 133 | decl_subsys(bus, &ktype_bus, NULL); |
137 | 134 | ||
138 | static int __bus_for_each_dev(struct bus_type *bus, struct device *start, | ||
139 | void *data, int (*fn)(struct device *, void *)) | ||
140 | { | ||
141 | struct list_head *head; | ||
142 | struct device *dev; | ||
143 | int error = 0; | ||
144 | |||
145 | if (!(bus = get_bus(bus))) | ||
146 | return -EINVAL; | ||
147 | |||
148 | head = &bus->devices.list; | ||
149 | dev = list_prepare_entry(start, head, bus_list); | ||
150 | list_for_each_entry_continue(dev, head, bus_list) { | ||
151 | get_device(dev); | ||
152 | error = fn(dev, data); | ||
153 | put_device(dev); | ||
154 | if (error) | ||
155 | break; | ||
156 | } | ||
157 | put_bus(bus); | ||
158 | return error; | ||
159 | } | ||
160 | 135 | ||
161 | static int __bus_for_each_drv(struct bus_type *bus, struct device_driver *start, | 136 | static struct device * next_device(struct klist_iter * i) |
162 | void * data, int (*fn)(struct device_driver *, void *)) | ||
163 | { | 137 | { |
164 | struct list_head *head; | 138 | struct klist_node * n = klist_next(i); |
165 | struct device_driver *drv; | 139 | return n ? container_of(n, struct device, knode_bus) : NULL; |
166 | int error = 0; | ||
167 | |||
168 | if (!(bus = get_bus(bus))) | ||
169 | return -EINVAL; | ||
170 | |||
171 | head = &bus->drivers.list; | ||
172 | drv = list_prepare_entry(start, head, kobj.entry); | ||
173 | list_for_each_entry_continue(drv, head, kobj.entry) { | ||
174 | get_driver(drv); | ||
175 | error = fn(drv, data); | ||
176 | put_driver(drv); | ||
177 | if (error) | ||
178 | break; | ||
179 | } | ||
180 | put_bus(bus); | ||
181 | return error; | ||
182 | } | 140 | } |
183 | 141 | ||
184 | /** | 142 | /** |
@@ -204,12 +162,27 @@ static int __bus_for_each_drv(struct bus_type *bus, struct device_driver *start, | |||
204 | int bus_for_each_dev(struct bus_type * bus, struct device * start, | 162 | int bus_for_each_dev(struct bus_type * bus, struct device * start, |
205 | void * data, int (*fn)(struct device *, void *)) | 163 | void * data, int (*fn)(struct device *, void *)) |
206 | { | 164 | { |
207 | int ret; | 165 | struct klist_iter i; |
166 | struct device * dev; | ||
167 | int error = 0; | ||
208 | 168 | ||
209 | down_read(&bus->subsys.rwsem); | 169 | if (!bus) |
210 | ret = __bus_for_each_dev(bus, start, data, fn); | 170 | return -EINVAL; |
211 | up_read(&bus->subsys.rwsem); | 171 | |
212 | return ret; | 172 | klist_iter_init_node(&bus->klist_devices, &i, |
173 | (start ? &start->knode_bus : NULL)); | ||
174 | while ((dev = next_device(&i)) && !error) | ||
175 | error = fn(dev, data); | ||
176 | klist_iter_exit(&i); | ||
177 | return error; | ||
178 | } | ||
179 | |||
180 | |||
181 | |||
182 | static struct device_driver * next_driver(struct klist_iter * i) | ||
183 | { | ||
184 | struct klist_node * n = klist_next(i); | ||
185 | return n ? container_of(n, struct device_driver, knode_bus) : NULL; | ||
213 | } | 186 | } |
214 | 187 | ||
215 | /** | 188 | /** |
@@ -235,179 +208,19 @@ int bus_for_each_dev(struct bus_type * bus, struct device * start, | |||
235 | int bus_for_each_drv(struct bus_type * bus, struct device_driver * start, | 208 | int bus_for_each_drv(struct bus_type * bus, struct device_driver * start, |
236 | void * data, int (*fn)(struct device_driver *, void *)) | 209 | void * data, int (*fn)(struct device_driver *, void *)) |
237 | { | 210 | { |
238 | int ret; | 211 | struct klist_iter i; |
239 | 212 | struct device_driver * drv; | |
240 | down_read(&bus->subsys.rwsem); | 213 | int error = 0; |
241 | ret = __bus_for_each_drv(bus, start, data, fn); | ||
242 | up_read(&bus->subsys.rwsem); | ||
243 | return ret; | ||
244 | } | ||
245 | |||
246 | /** | ||
247 | * device_bind_driver - bind a driver to one device. | ||
248 | * @dev: device. | ||
249 | * | ||
250 | * Allow manual attachment of a driver to a device. | ||
251 | * Caller must have already set @dev->driver. | ||
252 | * | ||
253 | * Note that this does not modify the bus reference count | ||
254 | * nor take the bus's rwsem. Please verify those are accounted | ||
255 | * for before calling this. (It is ok to call with no other effort | ||
256 | * from a driver's probe() method.) | ||
257 | */ | ||
258 | |||
259 | void device_bind_driver(struct device * dev) | ||
260 | { | ||
261 | pr_debug("bound device '%s' to driver '%s'\n", | ||
262 | dev->bus_id, dev->driver->name); | ||
263 | list_add_tail(&dev->driver_list, &dev->driver->devices); | ||
264 | sysfs_create_link(&dev->driver->kobj, &dev->kobj, | ||
265 | kobject_name(&dev->kobj)); | ||
266 | sysfs_create_link(&dev->kobj, &dev->driver->kobj, "driver"); | ||
267 | } | ||
268 | |||
269 | |||
270 | /** | ||
271 | * driver_probe_device - attempt to bind device & driver. | ||
272 | * @drv: driver. | ||
273 | * @dev: device. | ||
274 | * | ||
275 | * First, we call the bus's match function, if one present, which | ||
276 | * should compare the device IDs the driver supports with the | ||
277 | * device IDs of the device. Note we don't do this ourselves | ||
278 | * because we don't know the format of the ID structures, nor what | ||
279 | * is to be considered a match and what is not. | ||
280 | * | ||
281 | * If we find a match, we call @drv->probe(@dev) if it exists, and | ||
282 | * call device_bind_driver() above. | ||
283 | */ | ||
284 | int driver_probe_device(struct device_driver * drv, struct device * dev) | ||
285 | { | ||
286 | if (drv->bus->match && !drv->bus->match(dev, drv)) | ||
287 | return -ENODEV; | ||
288 | |||
289 | dev->driver = drv; | ||
290 | if (drv->probe) { | ||
291 | int error = drv->probe(dev); | ||
292 | if (error) { | ||
293 | dev->driver = NULL; | ||
294 | return error; | ||
295 | } | ||
296 | } | ||
297 | |||
298 | device_bind_driver(dev); | ||
299 | return 0; | ||
300 | } | ||
301 | |||
302 | |||
303 | /** | ||
304 | * device_attach - try to attach device to a driver. | ||
305 | * @dev: device. | ||
306 | * | ||
307 | * Walk the list of drivers that the bus has and call | ||
308 | * driver_probe_device() for each pair. If a compatible | ||
309 | * pair is found, break out and return. | ||
310 | */ | ||
311 | int device_attach(struct device * dev) | ||
312 | { | ||
313 | struct bus_type * bus = dev->bus; | ||
314 | struct list_head * entry; | ||
315 | int error; | ||
316 | |||
317 | if (dev->driver) { | ||
318 | device_bind_driver(dev); | ||
319 | return 1; | ||
320 | } | ||
321 | |||
322 | if (bus->match) { | ||
323 | list_for_each(entry, &bus->drivers.list) { | ||
324 | struct device_driver * drv = to_drv(entry); | ||
325 | error = driver_probe_device(drv, dev); | ||
326 | if (!error) | ||
327 | /* success, driver matched */ | ||
328 | return 1; | ||
329 | if (error != -ENODEV && error != -ENXIO) | ||
330 | /* driver matched but the probe failed */ | ||
331 | printk(KERN_WARNING | ||
332 | "%s: probe of %s failed with error %d\n", | ||
333 | drv->name, dev->bus_id, error); | ||
334 | } | ||
335 | } | ||
336 | |||
337 | return 0; | ||
338 | } | ||
339 | |||
340 | |||
341 | /** | ||
342 | * driver_attach - try to bind driver to devices. | ||
343 | * @drv: driver. | ||
344 | * | ||
345 | * Walk the list of devices that the bus has on it and try to | ||
346 | * match the driver with each one. If driver_probe_device() | ||
347 | * returns 0 and the @dev->driver is set, we've found a | ||
348 | * compatible pair. | ||
349 | * | ||
350 | * Note that we ignore the -ENODEV error from driver_probe_device(), | ||
351 | * since it's perfectly valid for a driver not to bind to any devices. | ||
352 | */ | ||
353 | void driver_attach(struct device_driver * drv) | ||
354 | { | ||
355 | struct bus_type * bus = drv->bus; | ||
356 | struct list_head * entry; | ||
357 | int error; | ||
358 | |||
359 | if (!bus->match) | ||
360 | return; | ||
361 | |||
362 | list_for_each(entry, &bus->devices.list) { | ||
363 | struct device * dev = container_of(entry, struct device, bus_list); | ||
364 | if (!dev->driver) { | ||
365 | error = driver_probe_device(drv, dev); | ||
366 | if (error && (error != -ENODEV)) | ||
367 | /* driver matched but the probe failed */ | ||
368 | printk(KERN_WARNING | ||
369 | "%s: probe of %s failed with error %d\n", | ||
370 | drv->name, dev->bus_id, error); | ||
371 | } | ||
372 | } | ||
373 | } | ||
374 | |||
375 | |||
376 | /** | ||
377 | * device_release_driver - manually detach device from driver. | ||
378 | * @dev: device. | ||
379 | * | ||
380 | * Manually detach device from driver. | ||
381 | * Note that this is called without incrementing the bus | ||
382 | * reference count nor taking the bus's rwsem. Be sure that | ||
383 | * those are accounted for before calling this function. | ||
384 | */ | ||
385 | |||
386 | void device_release_driver(struct device * dev) | ||
387 | { | ||
388 | struct device_driver * drv = dev->driver; | ||
389 | if (drv) { | ||
390 | sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj)); | ||
391 | sysfs_remove_link(&dev->kobj, "driver"); | ||
392 | list_del_init(&dev->driver_list); | ||
393 | if (drv->remove) | ||
394 | drv->remove(dev); | ||
395 | dev->driver = NULL; | ||
396 | } | ||
397 | } | ||
398 | |||
399 | 214 | ||
400 | /** | 215 | if (!bus) |
401 | * driver_detach - detach driver from all devices it controls. | 216 | return -EINVAL; |
402 | * @drv: driver. | ||
403 | */ | ||
404 | 217 | ||
405 | static void driver_detach(struct device_driver * drv) | 218 | klist_iter_init_node(&bus->klist_drivers, &i, |
406 | { | 219 | start ? &start->knode_bus : NULL); |
407 | while (!list_empty(&drv->devices)) { | 220 | while ((drv = next_driver(&i)) && !error) |
408 | struct device * dev = container_of(drv->devices.next, struct device, driver_list); | 221 | error = fn(drv, data); |
409 | device_release_driver(dev); | 222 | klist_iter_exit(&i); |
410 | } | 223 | return error; |
411 | } | 224 | } |
412 | 225 | ||
413 | static int device_add_attrs(struct bus_type * bus, struct device * dev) | 226 | static int device_add_attrs(struct bus_type * bus, struct device * dev) |
@@ -456,14 +269,15 @@ int bus_add_device(struct device * dev) | |||
456 | int error = 0; | 269 | int error = 0; |
457 | 270 | ||
458 | if (bus) { | 271 | if (bus) { |
459 | down_write(&dev->bus->subsys.rwsem); | ||
460 | pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id); | 272 | pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id); |
461 | list_add_tail(&dev->bus_list, &dev->bus->devices.list); | 273 | error = device_attach(dev); |
462 | device_attach(dev); | 274 | klist_add_tail(&bus->klist_devices, &dev->knode_bus); |
463 | up_write(&dev->bus->subsys.rwsem); | 275 | if (error >= 0) |
464 | device_add_attrs(bus, dev); | 276 | error = device_add_attrs(bus, dev); |
465 | sysfs_create_link(&bus->devices.kobj, &dev->kobj, dev->bus_id); | 277 | if (!error) { |
466 | sysfs_create_link(&dev->kobj, &dev->bus->subsys.kset.kobj, "bus"); | 278 | sysfs_create_link(&bus->devices.kobj, &dev->kobj, dev->bus_id); |
279 | sysfs_create_link(&dev->kobj, &dev->bus->subsys.kset.kobj, "bus"); | ||
280 | } | ||
467 | } | 281 | } |
468 | return error; | 282 | return error; |
469 | } | 283 | } |
@@ -483,11 +297,9 @@ void bus_remove_device(struct device * dev) | |||
483 | sysfs_remove_link(&dev->kobj, "bus"); | 297 | sysfs_remove_link(&dev->kobj, "bus"); |
484 | sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id); | 298 | sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id); |
485 | device_remove_attrs(dev->bus, dev); | 299 | device_remove_attrs(dev->bus, dev); |
486 | down_write(&dev->bus->subsys.rwsem); | 300 | klist_remove(&dev->knode_bus); |
487 | pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id); | 301 | pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id); |
488 | device_release_driver(dev); | 302 | device_release_driver(dev); |
489 | list_del_init(&dev->bus_list); | ||
490 | up_write(&dev->bus->subsys.rwsem); | ||
491 | put_bus(dev->bus); | 303 | put_bus(dev->bus); |
492 | } | 304 | } |
493 | } | 305 | } |
@@ -547,9 +359,8 @@ int bus_add_driver(struct device_driver * drv) | |||
547 | return error; | 359 | return error; |
548 | } | 360 | } |
549 | 361 | ||
550 | down_write(&bus->subsys.rwsem); | ||
551 | driver_attach(drv); | 362 | driver_attach(drv); |
552 | up_write(&bus->subsys.rwsem); | 363 | klist_add_tail(&bus->klist_drivers, &drv->knode_bus); |
553 | module_add_driver(drv->owner, drv); | 364 | module_add_driver(drv->owner, drv); |
554 | 365 | ||
555 | driver_add_attrs(bus, drv); | 366 | driver_add_attrs(bus, drv); |
@@ -571,10 +382,9 @@ void bus_remove_driver(struct device_driver * drv) | |||
571 | { | 382 | { |
572 | if (drv->bus) { | 383 | if (drv->bus) { |
573 | driver_remove_attrs(drv->bus, drv); | 384 | driver_remove_attrs(drv->bus, drv); |
574 | down_write(&drv->bus->subsys.rwsem); | 385 | klist_remove(&drv->knode_bus); |
575 | pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name); | 386 | pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name); |
576 | driver_detach(drv); | 387 | driver_detach(drv); |
577 | up_write(&drv->bus->subsys.rwsem); | ||
578 | module_remove_driver(drv); | 388 | module_remove_driver(drv); |
579 | kobject_unregister(&drv->kobj); | 389 | kobject_unregister(&drv->kobj); |
580 | put_bus(drv->bus); | 390 | put_bus(drv->bus); |
@@ -587,7 +397,7 @@ static int bus_rescan_devices_helper(struct device *dev, void *data) | |||
587 | { | 397 | { |
588 | int *count = data; | 398 | int *count = data; |
589 | 399 | ||
590 | if (!dev->driver && device_attach(dev)) | 400 | if (!dev->driver && (device_attach(dev) > 0)) |
591 | (*count)++; | 401 | (*count)++; |
592 | 402 | ||
593 | return 0; | 403 | return 0; |
@@ -607,9 +417,7 @@ int bus_rescan_devices(struct bus_type * bus) | |||
607 | { | 417 | { |
608 | int count = 0; | 418 | int count = 0; |
609 | 419 | ||
610 | down_write(&bus->subsys.rwsem); | 420 | bus_for_each_dev(bus, NULL, &count, bus_rescan_devices_helper); |
611 | __bus_for_each_dev(bus, NULL, &count, bus_rescan_devices_helper); | ||
612 | up_write(&bus->subsys.rwsem); | ||
613 | 421 | ||
614 | return count; | 422 | return count; |
615 | } | 423 | } |
@@ -710,6 +518,9 @@ int bus_register(struct bus_type * bus) | |||
710 | retval = kset_register(&bus->drivers); | 518 | retval = kset_register(&bus->drivers); |
711 | if (retval) | 519 | if (retval) |
712 | goto bus_drivers_fail; | 520 | goto bus_drivers_fail; |
521 | |||
522 | klist_init(&bus->klist_devices); | ||
523 | klist_init(&bus->klist_drivers); | ||
713 | bus_add_attrs(bus); | 524 | bus_add_attrs(bus); |
714 | 525 | ||
715 | pr_debug("bus type '%s' registered\n", bus->name); | 526 | pr_debug("bus type '%s' registered\n", bus->name); |
@@ -749,12 +560,6 @@ int __init buses_init(void) | |||
749 | EXPORT_SYMBOL_GPL(bus_for_each_dev); | 560 | EXPORT_SYMBOL_GPL(bus_for_each_dev); |
750 | EXPORT_SYMBOL_GPL(bus_for_each_drv); | 561 | EXPORT_SYMBOL_GPL(bus_for_each_drv); |
751 | 562 | ||
752 | EXPORT_SYMBOL_GPL(driver_probe_device); | ||
753 | EXPORT_SYMBOL_GPL(device_bind_driver); | ||
754 | EXPORT_SYMBOL_GPL(device_release_driver); | ||
755 | EXPORT_SYMBOL_GPL(device_attach); | ||
756 | EXPORT_SYMBOL_GPL(driver_attach); | ||
757 | |||
758 | EXPORT_SYMBOL_GPL(bus_add_device); | 563 | EXPORT_SYMBOL_GPL(bus_add_device); |
759 | EXPORT_SYMBOL_GPL(bus_remove_device); | 564 | EXPORT_SYMBOL_GPL(bus_remove_device); |
760 | EXPORT_SYMBOL_GPL(bus_register); | 565 | EXPORT_SYMBOL_GPL(bus_register); |