]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/base/bus.c
[PATCH] driver core: add bus_find_device & driver_find_device functions
[mirror_ubuntu-eoan-kernel.git] / drivers / base / bus.c
CommitLineData
1da177e4
LT
1/*
2 * bus.c - bus driver management
3 *
4 * Copyright (c) 2002-3 Patrick Mochel
5 * Copyright (c) 2002-3 Open Source Development Labs
6 *
7 * This file is released under the GPLv2
8 *
9 */
10
11#include <linux/config.h>
12#include <linux/device.h>
13#include <linux/module.h>
14#include <linux/errno.h>
15#include <linux/init.h>
16#include <linux/string.h>
17#include "base.h"
18#include "power/power.h"
19
1da177e4
LT
20#define to_bus_attr(_attr) container_of(_attr, struct bus_attribute, attr)
21#define to_bus(obj) container_of(obj, struct bus_type, subsys.kset.kobj)
22
23/*
24 * sysfs bindings for drivers
25 */
26
27#define to_drv_attr(_attr) container_of(_attr, struct driver_attribute, attr)
28#define to_driver(obj) container_of(obj, struct device_driver, kobj)
29
30
31static ssize_t
32drv_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
33{
34 struct driver_attribute * drv_attr = to_drv_attr(attr);
35 struct device_driver * drv = to_driver(kobj);
4a0c20bf 36 ssize_t ret = -EIO;
1da177e4
LT
37
38 if (drv_attr->show)
39 ret = drv_attr->show(drv, buf);
40 return ret;
41}
42
43static ssize_t
44drv_attr_store(struct kobject * kobj, struct attribute * attr,
45 const char * buf, size_t count)
46{
47 struct driver_attribute * drv_attr = to_drv_attr(attr);
48 struct device_driver * drv = to_driver(kobj);
4a0c20bf 49 ssize_t ret = -EIO;
1da177e4
LT
50
51 if (drv_attr->store)
52 ret = drv_attr->store(drv, buf, count);
53 return ret;
54}
55
56static struct sysfs_ops driver_sysfs_ops = {
57 .show = drv_attr_show,
58 .store = drv_attr_store,
59};
60
61
62static void driver_release(struct kobject * kobj)
63{
64 struct device_driver * drv = to_driver(kobj);
65 complete(&drv->unloaded);
66}
67
68static struct kobj_type ktype_driver = {
69 .sysfs_ops = &driver_sysfs_ops,
70 .release = driver_release,
71};
72
73
74/*
75 * sysfs bindings for buses
76 */
77
78
79static ssize_t
80bus_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
81{
82 struct bus_attribute * bus_attr = to_bus_attr(attr);
83 struct bus_type * bus = to_bus(kobj);
84 ssize_t ret = 0;
85
86 if (bus_attr->show)
87 ret = bus_attr->show(bus, buf);
88 return ret;
89}
90
91static ssize_t
92bus_attr_store(struct kobject * kobj, struct attribute * attr,
93 const char * buf, size_t count)
94{
95 struct bus_attribute * bus_attr = to_bus_attr(attr);
96 struct bus_type * bus = to_bus(kobj);
97 ssize_t ret = 0;
98
99 if (bus_attr->store)
100 ret = bus_attr->store(bus, buf, count);
101 return ret;
102}
103
104static struct sysfs_ops bus_sysfs_ops = {
105 .show = bus_attr_show,
106 .store = bus_attr_store,
107};
108
109int bus_create_file(struct bus_type * bus, struct bus_attribute * attr)
110{
111 int error;
112 if (get_bus(bus)) {
113 error = sysfs_create_file(&bus->subsys.kset.kobj, &attr->attr);
114 put_bus(bus);
115 } else
116 error = -EINVAL;
117 return error;
118}
119
120void bus_remove_file(struct bus_type * bus, struct bus_attribute * attr)
121{
122 if (get_bus(bus)) {
123 sysfs_remove_file(&bus->subsys.kset.kobj, &attr->attr);
124 put_bus(bus);
125 }
126}
127
128static struct kobj_type ktype_bus = {
129 .sysfs_ops = &bus_sysfs_ops,
130
131};
132
133decl_subsys(bus, &ktype_bus, NULL);
134
1da177e4 135
465c7a3a
PM
136static struct device * next_device(struct klist_iter * i)
137{
138 struct klist_node * n = klist_next(i);
139 return n ? container_of(n, struct device, knode_bus) : NULL;
140}
141
1da177e4
LT
142/**
143 * bus_for_each_dev - device iterator.
144 * @bus: bus type.
145 * @start: device to start iterating from.
146 * @data: data for the callback.
147 * @fn: function to be called for each device.
148 *
149 * Iterate over @bus's list of devices, and call @fn for each,
150 * passing it @data. If @start is not NULL, we use that device to
151 * begin iterating from.
152 *
153 * We check the return of @fn each time. If it returns anything
154 * other than 0, we break out and return that value.
155 *
156 * NOTE: The device that returns a non-zero value is not retained
157 * in any way, nor is its refcount incremented. If the caller needs
158 * to retain this data, it should do, and increment the reference
159 * count in the supplied callback.
160 */
161
162int bus_for_each_dev(struct bus_type * bus, struct device * start,
163 void * data, int (*fn)(struct device *, void *))
164{
465c7a3a
PM
165 struct klist_iter i;
166 struct device * dev;
167 int error = 0;
1da177e4 168
465c7a3a
PM
169 if (!bus)
170 return -EINVAL;
171
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;
1da177e4
LT
178}
179
0edb5860
CH
180/**
181 * bus_find_device - device iterator for locating a particular device.
182 * @bus: bus type
183 * @start: Device to begin with
184 * @data: Data to pass to match function
185 * @match: Callback function to check device
186 *
187 * This is similar to the bus_for_each_dev() function above, but it
188 * returns a reference to a device that is 'found' for later use, as
189 * determined by the @match callback.
190 *
191 * The callback should return 0 if the device doesn't match and non-zero
192 * if it does. If the callback returns non-zero, this function will
193 * return to the caller and not iterate over any more devices.
194 */
195struct device * bus_find_device(struct bus_type *bus,
196 struct device *start, void *data,
197 int (*match)(struct device *, void *))
198{
199 struct klist_iter i;
200 struct device *dev;
201
202 if (!bus)
203 return NULL;
204
205 klist_iter_init_node(&bus->klist_devices, &i,
206 (start ? &start->knode_bus : NULL));
207 while ((dev = next_device(&i)))
208 if (match(dev, data) && get_device(dev))
209 break;
210 klist_iter_exit(&i);
211 return dev;
212}
38fdac3c
PM
213
214
215static struct device_driver * next_driver(struct klist_iter * i)
216{
217 struct klist_node * n = klist_next(i);
218 return n ? container_of(n, struct device_driver, knode_bus) : NULL;
219}
220
1da177e4
LT
221/**
222 * bus_for_each_drv - driver iterator
223 * @bus: bus we're dealing with.
224 * @start: driver to start iterating on.
225 * @data: data to pass to the callback.
226 * @fn: function to call for each driver.
227 *
228 * This is nearly identical to the device iterator above.
229 * We iterate over each driver that belongs to @bus, and call
230 * @fn for each. If @fn returns anything but 0, we break out
231 * and return it. If @start is not NULL, we use it as the head
232 * of the list.
233 *
234 * NOTE: we don't return the driver that returns a non-zero
235 * value, nor do we leave the reference count incremented for that
236 * driver. If the caller needs to know that info, it must set it
237 * in the callback. It must also be sure to increment the refcount
238 * so it doesn't disappear before returning to the caller.
239 */
240
241int bus_for_each_drv(struct bus_type * bus, struct device_driver * start,
242 void * data, int (*fn)(struct device_driver *, void *))
243{
38fdac3c
PM
244 struct klist_iter i;
245 struct device_driver * drv;
246 int error = 0;
1da177e4 247
38fdac3c
PM
248 if (!bus)
249 return -EINVAL;
250
251 klist_iter_init_node(&bus->klist_drivers, &i,
252 start ? &start->knode_bus : NULL);
253 while ((drv = next_driver(&i)) && !error)
254 error = fn(drv, data);
255 klist_iter_exit(&i);
256 return error;
1da177e4
LT
257}
258
1da177e4
LT
259static int device_add_attrs(struct bus_type * bus, struct device * dev)
260{
261 int error = 0;
262 int i;
263
264 if (bus->dev_attrs) {
265 for (i = 0; attr_name(bus->dev_attrs[i]); i++) {
266 error = device_create_file(dev,&bus->dev_attrs[i]);
267 if (error)
268 goto Err;
269 }
270 }
271 Done:
272 return error;
273 Err:
274 while (--i >= 0)
275 device_remove_file(dev,&bus->dev_attrs[i]);
276 goto Done;
277}
278
279
280static void device_remove_attrs(struct bus_type * bus, struct device * dev)
281{
282 int i;
283
284 if (bus->dev_attrs) {
285 for (i = 0; attr_name(bus->dev_attrs[i]); i++)
286 device_remove_file(dev,&bus->dev_attrs[i]);
287 }
288}
289
290
291/**
292 * bus_add_device - add device to bus
293 * @dev: device being added
294 *
295 * - Add the device to its bus's list of devices.
296 * - Try to attach to driver.
297 * - Create link to device's physical location.
298 */
299int bus_add_device(struct device * dev)
300{
301 struct bus_type * bus = get_bus(dev->bus);
302 int error = 0;
303
304 if (bus) {
1da177e4 305 pr_debug("bus %s: add device %s\n", bus->name, dev->bus_id);
d377e85b 306 device_attach(dev);
465c7a3a 307 klist_add_tail(&bus->klist_devices, &dev->knode_bus);
d377e85b 308 error = device_add_attrs(bus, dev);
ca2b94ba
HR
309 if (!error) {
310 sysfs_create_link(&bus->devices.kobj, &dev->kobj, dev->bus_id);
311 sysfs_create_link(&dev->kobj, &dev->bus->subsys.kset.kobj, "bus");
312 }
1da177e4
LT
313 }
314 return error;
315}
316
317/**
318 * bus_remove_device - remove device from bus
319 * @dev: device to be removed
320 *
321 * - Remove symlink from bus's directory.
322 * - Delete device from bus's list.
323 * - Detach from its driver.
324 * - Drop reference taken in bus_add_device().
325 */
326void bus_remove_device(struct device * dev)
327{
328 if (dev->bus) {
329 sysfs_remove_link(&dev->kobj, "bus");
330 sysfs_remove_link(&dev->bus->devices.kobj, dev->bus_id);
331 device_remove_attrs(dev->bus, dev);
465c7a3a 332 klist_remove(&dev->knode_bus);
1da177e4
LT
333 pr_debug("bus %s: remove device %s\n", dev->bus->name, dev->bus_id);
334 device_release_driver(dev);
1da177e4
LT
335 put_bus(dev->bus);
336 }
337}
338
339static int driver_add_attrs(struct bus_type * bus, struct device_driver * drv)
340{
341 int error = 0;
342 int i;
343
344 if (bus->drv_attrs) {
345 for (i = 0; attr_name(bus->drv_attrs[i]); i++) {
346 error = driver_create_file(drv, &bus->drv_attrs[i]);
347 if (error)
348 goto Err;
349 }
350 }
351 Done:
352 return error;
353 Err:
354 while (--i >= 0)
355 driver_remove_file(drv, &bus->drv_attrs[i]);
356 goto Done;
357}
358
359
360static void driver_remove_attrs(struct bus_type * bus, struct device_driver * drv)
361{
362 int i;
363
364 if (bus->drv_attrs) {
365 for (i = 0; attr_name(bus->drv_attrs[i]); i++)
366 driver_remove_file(drv, &bus->drv_attrs[i]);
367 }
368}
369
370
371/**
372 * bus_add_driver - Add a driver to the bus.
373 * @drv: driver.
374 *
375 */
376int bus_add_driver(struct device_driver * drv)
377{
378 struct bus_type * bus = get_bus(drv->bus);
379 int error = 0;
380
381 if (bus) {
382 pr_debug("bus %s: add driver %s\n", bus->name, drv->name);
383 error = kobject_set_name(&drv->kobj, "%s", drv->name);
384 if (error) {
385 put_bus(bus);
386 return error;
387 }
388 drv->kobj.kset = &bus->drivers;
389 if ((error = kobject_register(&drv->kobj))) {
390 put_bus(bus);
391 return error;
392 }
393
1da177e4 394 driver_attach(drv);
38fdac3c 395 klist_add_tail(&bus->klist_drivers, &drv->knode_bus);
1da177e4
LT
396 module_add_driver(drv->owner, drv);
397
398 driver_add_attrs(bus, drv);
399 }
400 return error;
401}
402
403
404/**
405 * bus_remove_driver - delete driver from bus's knowledge.
406 * @drv: driver.
407 *
408 * Detach the driver from the devices it controls, and remove
409 * it from its bus's list of drivers. Finally, we drop the reference
410 * to the bus we took in bus_add_driver().
411 */
412
413void bus_remove_driver(struct device_driver * drv)
414{
415 if (drv->bus) {
416 driver_remove_attrs(drv->bus, drv);
38fdac3c 417 klist_remove(&drv->knode_bus);
1da177e4
LT
418 pr_debug("bus %s: remove driver %s\n", drv->bus->name, drv->name);
419 driver_detach(drv);
1da177e4
LT
420 module_remove_driver(drv);
421 kobject_unregister(&drv->kobj);
422 put_bus(drv->bus);
423 }
424}
425
426
427/* Helper for bus_rescan_devices's iter */
428static int bus_rescan_devices_helper(struct device *dev, void *data)
429{
430 int *count = data;
431
ca2b94ba 432 if (!dev->driver && (device_attach(dev) > 0))
1da177e4
LT
433 (*count)++;
434
435 return 0;
436}
437
438
439/**
440 * bus_rescan_devices - rescan devices on the bus for possible drivers
441 * @bus: the bus to scan.
442 *
443 * This function will look for devices on the bus with no driver
444 * attached and rescan it against existing drivers to see if it
445 * matches any. Calls device_attach(). Returns the number of devices
446 * that were sucessfully bound to a driver.
447 */
448int bus_rescan_devices(struct bus_type * bus)
449{
450 int count = 0;
451
465c7a3a 452 bus_for_each_dev(bus, NULL, &count, bus_rescan_devices_helper);
1da177e4
LT
453
454 return count;
455}
456
457
458struct bus_type * get_bus(struct bus_type * bus)
459{
460 return bus ? container_of(subsys_get(&bus->subsys), struct bus_type, subsys) : NULL;
461}
462
463void put_bus(struct bus_type * bus)
464{
465 subsys_put(&bus->subsys);
466}
467
468
469/**
470 * find_bus - locate bus by name.
471 * @name: name of bus.
472 *
473 * Call kset_find_obj() to iterate over list of buses to
474 * find a bus by name. Return bus if found.
475 *
476 * Note that kset_find_obj increments bus' reference count.
477 */
478
479struct bus_type * find_bus(char * name)
480{
481 struct kobject * k = kset_find_obj(&bus_subsys.kset, name);
482 return k ? to_bus(k) : NULL;
483}
484
485
486/**
487 * bus_add_attrs - Add default attributes for this bus.
488 * @bus: Bus that has just been registered.
489 */
490
491static int bus_add_attrs(struct bus_type * bus)
492{
493 int error = 0;
494 int i;
495
496 if (bus->bus_attrs) {
497 for (i = 0; attr_name(bus->bus_attrs[i]); i++) {
498 if ((error = bus_create_file(bus,&bus->bus_attrs[i])))
499 goto Err;
500 }
501 }
502 Done:
503 return error;
504 Err:
505 while (--i >= 0)
506 bus_remove_file(bus,&bus->bus_attrs[i]);
507 goto Done;
508}
509
510static void bus_remove_attrs(struct bus_type * bus)
511{
512 int i;
513
514 if (bus->bus_attrs) {
515 for (i = 0; attr_name(bus->bus_attrs[i]); i++)
516 bus_remove_file(bus,&bus->bus_attrs[i]);
517 }
518}
519
520/**
521 * bus_register - register a bus with the system.
522 * @bus: bus.
523 *
524 * Once we have that, we registered the bus with the kobject
525 * infrastructure, then register the children subsystems it has:
526 * the devices and drivers that belong to the bus.
527 */
528int bus_register(struct bus_type * bus)
529{
530 int retval;
531
532 retval = kobject_set_name(&bus->subsys.kset.kobj, "%s", bus->name);
533 if (retval)
534 goto out;
535
536 subsys_set_kset(bus, bus_subsys);
537 retval = subsystem_register(&bus->subsys);
538 if (retval)
539 goto out;
540
541 kobject_set_name(&bus->devices.kobj, "devices");
542 bus->devices.subsys = &bus->subsys;
543 retval = kset_register(&bus->devices);
544 if (retval)
545 goto bus_devices_fail;
546
547 kobject_set_name(&bus->drivers.kobj, "drivers");
548 bus->drivers.subsys = &bus->subsys;
549 bus->drivers.ktype = &ktype_driver;
550 retval = kset_register(&bus->drivers);
551 if (retval)
552 goto bus_drivers_fail;
465c7a3a
PM
553
554 klist_init(&bus->klist_devices);
38fdac3c 555 klist_init(&bus->klist_drivers);
1da177e4
LT
556 bus_add_attrs(bus);
557
558 pr_debug("bus type '%s' registered\n", bus->name);
559 return 0;
560
561bus_drivers_fail:
562 kset_unregister(&bus->devices);
563bus_devices_fail:
564 subsystem_unregister(&bus->subsys);
565out:
566 return retval;
567}
568
569
570/**
571 * bus_unregister - remove a bus from the system
572 * @bus: bus.
573 *
574 * Unregister the child subsystems and the bus itself.
575 * Finally, we call put_bus() to release the refcount
576 */
577void bus_unregister(struct bus_type * bus)
578{
579 pr_debug("bus %s: unregistering\n", bus->name);
580 bus_remove_attrs(bus);
581 kset_unregister(&bus->drivers);
582 kset_unregister(&bus->devices);
583 subsystem_unregister(&bus->subsys);
584}
585
586int __init buses_init(void)
587{
588 return subsystem_register(&bus_subsys);
589}
590
591
592EXPORT_SYMBOL_GPL(bus_for_each_dev);
0edb5860 593EXPORT_SYMBOL_GPL(bus_find_device);
1da177e4
LT
594EXPORT_SYMBOL_GPL(bus_for_each_drv);
595
1da177e4
LT
596EXPORT_SYMBOL_GPL(bus_add_device);
597EXPORT_SYMBOL_GPL(bus_remove_device);
598EXPORT_SYMBOL_GPL(bus_register);
599EXPORT_SYMBOL_GPL(bus_unregister);
600EXPORT_SYMBOL_GPL(bus_rescan_devices);
601EXPORT_SYMBOL_GPL(get_bus);
602EXPORT_SYMBOL_GPL(put_bus);
603EXPORT_SYMBOL_GPL(find_bus);
604
605EXPORT_SYMBOL_GPL(bus_create_file);
606EXPORT_SYMBOL_GPL(bus_remove_file);