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