1
2
3
4
5
6
7
8#include <linux/device.h>
9#include <linux/slab.h>
10
11#include <linux/surface_aggregator/controller.h>
12#include <linux/surface_aggregator/device.h>
13
14#include "bus.h"
15#include "controller.h"
16
17static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
18 char *buf)
19{
20 struct ssam_device *sdev = to_ssam_device(dev);
21
22 return sysfs_emit(buf, "ssam:d%02Xc%02Xt%02Xi%02Xf%02X\n",
23 sdev->uid.domain, sdev->uid.category, sdev->uid.target,
24 sdev->uid.instance, sdev->uid.function);
25}
26static DEVICE_ATTR_RO(modalias);
27
28static struct attribute *ssam_device_attrs[] = {
29 &dev_attr_modalias.attr,
30 NULL,
31};
32ATTRIBUTE_GROUPS(ssam_device);
33
34static int ssam_device_uevent(struct device *dev, struct kobj_uevent_env *env)
35{
36 struct ssam_device *sdev = to_ssam_device(dev);
37
38 return add_uevent_var(env, "MODALIAS=ssam:d%02Xc%02Xt%02Xi%02Xf%02X",
39 sdev->uid.domain, sdev->uid.category,
40 sdev->uid.target, sdev->uid.instance,
41 sdev->uid.function);
42}
43
44static void ssam_device_release(struct device *dev)
45{
46 struct ssam_device *sdev = to_ssam_device(dev);
47
48 ssam_controller_put(sdev->ctrl);
49 kfree(sdev);
50}
51
52const struct device_type ssam_device_type = {
53 .name = "surface_aggregator_device",
54 .groups = ssam_device_groups,
55 .uevent = ssam_device_uevent,
56 .release = ssam_device_release,
57};
58EXPORT_SYMBOL_GPL(ssam_device_type);
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73struct ssam_device *ssam_device_alloc(struct ssam_controller *ctrl,
74 struct ssam_device_uid uid)
75{
76 struct ssam_device *sdev;
77
78 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
79 if (!sdev)
80 return NULL;
81
82 device_initialize(&sdev->dev);
83 sdev->dev.bus = &ssam_bus_type;
84 sdev->dev.type = &ssam_device_type;
85 sdev->dev.parent = ssam_controller_device(ctrl);
86 sdev->ctrl = ssam_controller_get(ctrl);
87 sdev->uid = uid;
88
89 dev_set_name(&sdev->dev, "%02x:%02x:%02x:%02x:%02x",
90 sdev->uid.domain, sdev->uid.category, sdev->uid.target,
91 sdev->uid.instance, sdev->uid.function);
92
93 return sdev;
94}
95EXPORT_SYMBOL_GPL(ssam_device_alloc);
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122int ssam_device_add(struct ssam_device *sdev)
123{
124 int status;
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143 ssam_controller_statelock(sdev->ctrl);
144
145 if (sdev->ctrl->state != SSAM_CONTROLLER_STARTED) {
146 ssam_controller_stateunlock(sdev->ctrl);
147 return -ENODEV;
148 }
149
150 status = device_add(&sdev->dev);
151
152 ssam_controller_stateunlock(sdev->ctrl);
153 return status;
154}
155EXPORT_SYMBOL_GPL(ssam_device_add);
156
157
158
159
160
161
162
163void ssam_device_remove(struct ssam_device *sdev)
164{
165 device_unregister(&sdev->dev);
166}
167EXPORT_SYMBOL_GPL(ssam_device_remove);
168
169
170
171
172
173
174
175
176
177
178
179
180
181static bool ssam_device_id_compatible(const struct ssam_device_id *id,
182 struct ssam_device_uid uid)
183{
184 if (id->domain != uid.domain || id->category != uid.category)
185 return false;
186
187 if ((id->match_flags & SSAM_MATCH_TARGET) && id->target != uid.target)
188 return false;
189
190 if ((id->match_flags & SSAM_MATCH_INSTANCE) && id->instance != uid.instance)
191 return false;
192
193 if ((id->match_flags & SSAM_MATCH_FUNCTION) && id->function != uid.function)
194 return false;
195
196 return true;
197}
198
199
200
201
202
203
204
205
206
207
208
209static bool ssam_device_id_is_null(const struct ssam_device_id *id)
210{
211 return id->match_flags == 0 &&
212 id->domain == 0 &&
213 id->category == 0 &&
214 id->target == 0 &&
215 id->instance == 0 &&
216 id->function == 0 &&
217 id->driver_data == 0;
218}
219
220
221
222
223
224
225
226
227
228const struct ssam_device_id *ssam_device_id_match(const struct ssam_device_id *table,
229 const struct ssam_device_uid uid)
230{
231 const struct ssam_device_id *id;
232
233 for (id = table; !ssam_device_id_is_null(id); ++id)
234 if (ssam_device_id_compatible(id, uid))
235 return id;
236
237 return NULL;
238}
239EXPORT_SYMBOL_GPL(ssam_device_id_match);
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257const struct ssam_device_id *ssam_device_get_match(const struct ssam_device *dev)
258{
259 const struct ssam_device_driver *sdrv;
260
261 sdrv = to_ssam_device_driver(dev->dev.driver);
262 if (!sdrv)
263 return NULL;
264
265 if (!sdrv->match_table)
266 return NULL;
267
268 return ssam_device_id_match(sdrv->match_table, dev->uid);
269}
270EXPORT_SYMBOL_GPL(ssam_device_get_match);
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290const void *ssam_device_get_match_data(const struct ssam_device *dev)
291{
292 const struct ssam_device_id *id;
293
294 id = ssam_device_get_match(dev);
295 if (!id)
296 return NULL;
297
298 return (const void *)id->driver_data;
299}
300EXPORT_SYMBOL_GPL(ssam_device_get_match_data);
301
302static int ssam_bus_match(struct device *dev, struct device_driver *drv)
303{
304 struct ssam_device_driver *sdrv = to_ssam_device_driver(drv);
305 struct ssam_device *sdev = to_ssam_device(dev);
306
307 if (!is_ssam_device(dev))
308 return 0;
309
310 return !!ssam_device_id_match(sdrv->match_table, sdev->uid);
311}
312
313static int ssam_bus_probe(struct device *dev)
314{
315 return to_ssam_device_driver(dev->driver)
316 ->probe(to_ssam_device(dev));
317}
318
319static void ssam_bus_remove(struct device *dev)
320{
321 struct ssam_device_driver *sdrv = to_ssam_device_driver(dev->driver);
322
323 if (sdrv->remove)
324 sdrv->remove(to_ssam_device(dev));
325}
326
327struct bus_type ssam_bus_type = {
328 .name = "surface_aggregator",
329 .match = ssam_bus_match,
330 .probe = ssam_bus_probe,
331 .remove = ssam_bus_remove,
332};
333EXPORT_SYMBOL_GPL(ssam_bus_type);
334
335
336
337
338
339
340
341
342
343int __ssam_device_driver_register(struct ssam_device_driver *sdrv,
344 struct module *owner)
345{
346 sdrv->driver.owner = owner;
347 sdrv->driver.bus = &ssam_bus_type;
348
349
350 sdrv->driver.probe_type = PROBE_PREFER_ASYNCHRONOUS;
351
352 return driver_register(&sdrv->driver);
353}
354EXPORT_SYMBOL_GPL(__ssam_device_driver_register);
355
356
357
358
359
360void ssam_device_driver_unregister(struct ssam_device_driver *sdrv)
361{
362 driver_unregister(&sdrv->driver);
363}
364EXPORT_SYMBOL_GPL(ssam_device_driver_unregister);
365
366static int ssam_remove_device(struct device *dev, void *_data)
367{
368 struct ssam_device *sdev = to_ssam_device(dev);
369
370 if (is_ssam_device(dev))
371 ssam_device_remove(sdev);
372
373 return 0;
374}
375
376
377
378
379
380
381
382
383
384
385void ssam_remove_clients(struct device *dev)
386{
387 device_for_each_child_reverse(dev, NULL, ssam_remove_device);
388}
389EXPORT_SYMBOL_GPL(ssam_remove_clients);
390
391
392
393
394int ssam_bus_register(void)
395{
396 return bus_register(&ssam_bus_type);
397}
398
399
400
401
402void ssam_bus_unregister(void)
403{
404 return bus_unregister(&ssam_bus_type);
405}
406