1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/rio.h>
16#include <linux/rio_ids.h>
17
18#include "rio.h"
19
20
21
22
23
24
25
26
27
28
29static const struct rio_device_id *rio_match_device(const struct rio_device_id
30 *id,
31 const struct rio_dev *rdev)
32{
33 while (id->vid || id->asm_vid) {
34 if (((id->vid == RIO_ANY_ID) || (id->vid == rdev->vid)) &&
35 ((id->did == RIO_ANY_ID) || (id->did == rdev->did)) &&
36 ((id->asm_vid == RIO_ANY_ID)
37 || (id->asm_vid == rdev->asm_vid))
38 && ((id->asm_did == RIO_ANY_ID)
39 || (id->asm_did == rdev->asm_did)))
40 return id;
41 id++;
42 }
43 return NULL;
44}
45
46
47
48
49
50
51
52
53
54
55
56
57struct rio_dev *rio_dev_get(struct rio_dev *rdev)
58{
59 if (rdev)
60 get_device(&rdev->dev);
61
62 return rdev;
63}
64
65
66
67
68
69
70
71
72
73
74void rio_dev_put(struct rio_dev *rdev)
75{
76 if (rdev)
77 put_device(&rdev->dev);
78}
79
80
81
82
83
84
85
86static int rio_device_probe(struct device *dev)
87{
88 struct rio_driver *rdrv = to_rio_driver(dev->driver);
89 struct rio_dev *rdev = to_rio_dev(dev);
90 int error = -ENODEV;
91 const struct rio_device_id *id;
92
93 if (!rdev->driver && rdrv->probe) {
94 if (!rdrv->id_table)
95 return error;
96 id = rio_match_device(rdrv->id_table, rdev);
97 rio_dev_get(rdev);
98 if (id)
99 error = rdrv->probe(rdev, id);
100 if (error >= 0) {
101 rdev->driver = rdrv;
102 error = 0;
103 } else
104 rio_dev_put(rdev);
105 }
106 return error;
107}
108
109
110
111
112
113
114
115
116
117
118static int rio_device_remove(struct device *dev)
119{
120 struct rio_dev *rdev = to_rio_dev(dev);
121 struct rio_driver *rdrv = rdev->driver;
122
123 if (rdrv) {
124 if (rdrv->remove)
125 rdrv->remove(rdev);
126 rdev->driver = NULL;
127 }
128
129 rio_dev_put(rdev);
130
131 return 0;
132}
133
134
135
136
137
138
139
140
141
142
143int rio_register_driver(struct rio_driver *rdrv)
144{
145
146 rdrv->driver.name = rdrv->name;
147 rdrv->driver.bus = &rio_bus_type;
148
149
150 return driver_register(&rdrv->driver);
151}
152
153
154
155
156
157
158
159
160
161
162void rio_unregister_driver(struct rio_driver *rdrv)
163{
164 driver_unregister(&rdrv->driver);
165}
166
167void rio_attach_device(struct rio_dev *rdev)
168{
169 rdev->dev.bus = &rio_bus_type;
170}
171EXPORT_SYMBOL_GPL(rio_attach_device);
172
173
174
175
176
177
178
179
180
181
182
183static int rio_match_bus(struct device *dev, struct device_driver *drv)
184{
185 struct rio_dev *rdev = to_rio_dev(dev);
186 struct rio_driver *rdrv = to_rio_driver(drv);
187 const struct rio_device_id *id = rdrv->id_table;
188 const struct rio_device_id *found_id;
189
190 if (!id)
191 goto out;
192
193 found_id = rio_match_device(id, rdev);
194
195 if (found_id)
196 return 1;
197
198 out:return 0;
199}
200
201static int rio_uevent(struct device *dev, struct kobj_uevent_env *env)
202{
203 struct rio_dev *rdev;
204
205 if (!dev)
206 return -ENODEV;
207
208 rdev = to_rio_dev(dev);
209 if (!rdev)
210 return -ENODEV;
211
212 if (add_uevent_var(env, "MODALIAS=rapidio:v%04Xd%04Xav%04Xad%04X",
213 rdev->vid, rdev->did, rdev->asm_vid, rdev->asm_did))
214 return -ENOMEM;
215 return 0;
216}
217
218struct class rio_mport_class = {
219 .name = "rapidio_port",
220 .owner = THIS_MODULE,
221 .dev_groups = rio_mport_groups,
222};
223EXPORT_SYMBOL_GPL(rio_mport_class);
224
225struct bus_type rio_bus_type = {
226 .name = "rapidio",
227 .match = rio_match_bus,
228 .dev_groups = rio_dev_groups,
229 .bus_groups = rio_bus_groups,
230 .probe = rio_device_probe,
231 .remove = rio_device_remove,
232 .uevent = rio_uevent,
233};
234
235
236
237
238
239
240
241static int __init rio_bus_init(void)
242{
243 int ret;
244
245 ret = class_register(&rio_mport_class);
246 if (!ret) {
247 ret = bus_register(&rio_bus_type);
248 if (ret)
249 class_unregister(&rio_mport_class);
250 }
251 return ret;
252}
253
254postcore_initcall(rio_bus_init);
255
256EXPORT_SYMBOL_GPL(rio_register_driver);
257EXPORT_SYMBOL_GPL(rio_unregister_driver);
258EXPORT_SYMBOL_GPL(rio_bus_type);
259EXPORT_SYMBOL_GPL(rio_dev_get);
260EXPORT_SYMBOL_GPL(rio_dev_put);
261