1
2
3
4
5
6
7
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
23
24
25
26
27int 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
41
42
43
44
45void 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
56
57
58struct device_driver * get_driver(struct device_driver * drv)
59{
60 return drv ? to_drv(kobject_get(&drv->kobj)) : NULL;
61}
62
63
64
65
66
67
68void put_driver(struct device_driver * drv)
69{
70 kobject_put(&drv->kobj);
71}
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86int driver_register(struct device_driver * drv)
87{
88 INIT_LIST_HEAD(&drv->devices);
89 init_MUTEX_LOCKED(&drv->unload_sem);
90 return bus_add_driver(drv);
91}
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107void driver_unregister(struct device_driver * drv)
108{
109 bus_remove_driver(drv);
110 down(&drv->unload_sem);
111 up(&drv->unload_sem);
112}
113
114
115
116
117
118
119
120
121
122
123
124struct device_driver *driver_find(const char *name, struct bus_type *bus)
125{
126 struct kobject *k = kset_find_obj(&bus->drivers, name);
127 if (k)
128 return to_drv(k);
129 return NULL;
130}
131
132EXPORT_SYMBOL_GPL(driver_register);
133EXPORT_SYMBOL_GPL(driver_unregister);
134EXPORT_SYMBOL_GPL(get_driver);
135EXPORT_SYMBOL_GPL(put_driver);
136EXPORT_SYMBOL_GPL(driver_find);
137
138EXPORT_SYMBOL_GPL(driver_create_file);
139EXPORT_SYMBOL_GPL(driver_remove_file);
140