Linux-2.6.12-rc2
[safe/jmp/linux-2.6] / drivers / base / driver.c
1 /*
2  * driver.c - centralized device 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/string.h>
16 #include "base.h"
17
18 #define to_dev(node) container_of(node, struct device, driver_list)
19 #define to_drv(obj) container_of(obj, struct device_driver, kobj)
20
21 /**
22  *      driver_create_file - create sysfs file for driver.
23  *      @drv:   driver.
24  *      @attr:  driver attribute descriptor.
25  */
26
27 int driver_create_file(struct device_driver * drv, struct driver_attribute * attr)
28 {
29         int error;
30         if (get_driver(drv)) {
31                 error = sysfs_create_file(&drv->kobj, &attr->attr);
32                 put_driver(drv);
33         } else
34                 error = -EINVAL;
35         return error;
36 }
37
38
39 /**
40  *      driver_remove_file - remove sysfs file for driver.
41  *      @drv:   driver.
42  *      @attr:  driver attribute descriptor.
43  */
44
45 void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr)
46 {
47         if (get_driver(drv)) {
48                 sysfs_remove_file(&drv->kobj, &attr->attr);
49                 put_driver(drv);
50         }
51 }
52
53
54 /**
55  *      get_driver - increment driver reference count.
56  *      @drv:   driver.
57  */
58 struct device_driver * get_driver(struct device_driver * drv)
59 {
60         return drv ? to_drv(kobject_get(&drv->kobj)) : NULL;
61 }
62
63
64 /**
65  *      put_driver - decrement driver's refcount.
66  *      @drv:   driver.
67  */
68 void put_driver(struct device_driver * drv)
69 {
70         kobject_put(&drv->kobj);
71 }
72
73
74 /**
75  *      driver_register - register driver with bus
76  *      @drv:   driver to register
77  *
78  *      We pass off most of the work to the bus_add_driver() call,
79  *      since most of the things we have to do deal with the bus
80  *      structures.
81  *
82  *      The one interesting aspect is that we setup @drv->unloaded
83  *      as a completion that gets complete when the driver reference
84  *      count reaches 0.
85  */
86 int driver_register(struct device_driver * drv)
87 {
88         INIT_LIST_HEAD(&drv->devices);
89         init_completion(&drv->unloaded);
90         return bus_add_driver(drv);
91 }
92
93
94 /**
95  *      driver_unregister - remove driver from system.
96  *      @drv:   driver.
97  *
98  *      Again, we pass off most of the work to the bus-level call.
99  *
100  *      Though, once that is done, we wait until @drv->unloaded is completed.
101  *      This will block until the driver refcount reaches 0, and it is
102  *      released. Only modular drivers will call this function, and we
103  *      have to guarantee that it won't complete, letting the driver
104  *      unload until all references are gone.
105  */
106
107 void driver_unregister(struct device_driver * drv)
108 {
109         bus_remove_driver(drv);
110         wait_for_completion(&drv->unloaded);
111 }
112
113 /**
114  *      driver_find - locate driver on a bus by its name.
115  *      @name:  name of the driver.
116  *      @bus:   bus to scan for the driver.
117  *
118  *      Call kset_find_obj() to iterate over list of drivers on
119  *      a bus to find driver by name. Return driver if found.
120  *
121  *      Note that kset_find_obj increments driver's reference count.
122  */
123 struct device_driver *driver_find(const char *name, struct bus_type *bus)
124 {
125         struct kobject *k = kset_find_obj(&bus->drivers, name);
126         if (k)
127                 return to_drv(k);
128         return NULL;
129 }
130
131 EXPORT_SYMBOL_GPL(driver_register);
132 EXPORT_SYMBOL_GPL(driver_unregister);
133 EXPORT_SYMBOL_GPL(get_driver);
134 EXPORT_SYMBOL_GPL(put_driver);
135 EXPORT_SYMBOL_GPL(driver_find);
136
137 EXPORT_SYMBOL_GPL(driver_create_file);
138 EXPORT_SYMBOL_GPL(driver_remove_file);