This source file includes following definitions.
- fpga_bridge_enable
- fpga_bridge_disable
- __fpga_bridge_get
- of_fpga_bridge_get
- fpga_bridge_dev_match
- fpga_bridge_get
- fpga_bridge_put
- fpga_bridges_enable
- fpga_bridges_disable
- fpga_bridges_put
- of_fpga_bridge_get_to_list
- fpga_bridge_get_to_list
- name_show
- state_show
- fpga_bridge_create
- fpga_bridge_free
- devm_fpga_bridge_release
- devm_fpga_bridge_create
- fpga_bridge_register
- fpga_bridge_unregister
- fpga_bridge_dev_release
- fpga_bridge_dev_init
- fpga_bridge_dev_exit
1
2
3
4
5
6
7
8 #include <linux/fpga/fpga-bridge.h>
9 #include <linux/idr.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of_platform.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
15
16 static DEFINE_IDA(fpga_bridge_ida);
17 static struct class *fpga_bridge_class;
18
19
20 static spinlock_t bridge_list_lock;
21
22
23
24
25
26
27
28
29 int fpga_bridge_enable(struct fpga_bridge *bridge)
30 {
31 dev_dbg(&bridge->dev, "enable\n");
32
33 if (bridge->br_ops && bridge->br_ops->enable_set)
34 return bridge->br_ops->enable_set(bridge, 1);
35
36 return 0;
37 }
38 EXPORT_SYMBOL_GPL(fpga_bridge_enable);
39
40
41
42
43
44
45
46
47 int fpga_bridge_disable(struct fpga_bridge *bridge)
48 {
49 dev_dbg(&bridge->dev, "disable\n");
50
51 if (bridge->br_ops && bridge->br_ops->enable_set)
52 return bridge->br_ops->enable_set(bridge, 0);
53
54 return 0;
55 }
56 EXPORT_SYMBOL_GPL(fpga_bridge_disable);
57
58 static struct fpga_bridge *__fpga_bridge_get(struct device *dev,
59 struct fpga_image_info *info)
60 {
61 struct fpga_bridge *bridge;
62 int ret = -ENODEV;
63
64 bridge = to_fpga_bridge(dev);
65
66 bridge->info = info;
67
68 if (!mutex_trylock(&bridge->mutex)) {
69 ret = -EBUSY;
70 goto err_dev;
71 }
72
73 if (!try_module_get(dev->parent->driver->owner))
74 goto err_ll_mod;
75
76 dev_dbg(&bridge->dev, "get\n");
77
78 return bridge;
79
80 err_ll_mod:
81 mutex_unlock(&bridge->mutex);
82 err_dev:
83 put_device(dev);
84 return ERR_PTR(ret);
85 }
86
87
88
89
90
91
92
93
94
95
96
97 struct fpga_bridge *of_fpga_bridge_get(struct device_node *np,
98 struct fpga_image_info *info)
99 {
100 struct device *dev;
101
102 dev = class_find_device_by_of_node(fpga_bridge_class, np);
103 if (!dev)
104 return ERR_PTR(-ENODEV);
105
106 return __fpga_bridge_get(dev, info);
107 }
108 EXPORT_SYMBOL_GPL(of_fpga_bridge_get);
109
110 static int fpga_bridge_dev_match(struct device *dev, const void *data)
111 {
112 return dev->parent == data;
113 }
114
115
116
117
118
119
120
121
122
123
124 struct fpga_bridge *fpga_bridge_get(struct device *dev,
125 struct fpga_image_info *info)
126 {
127 struct device *bridge_dev;
128
129 bridge_dev = class_find_device(fpga_bridge_class, NULL, dev,
130 fpga_bridge_dev_match);
131 if (!bridge_dev)
132 return ERR_PTR(-ENODEV);
133
134 return __fpga_bridge_get(bridge_dev, info);
135 }
136 EXPORT_SYMBOL_GPL(fpga_bridge_get);
137
138
139
140
141
142
143 void fpga_bridge_put(struct fpga_bridge *bridge)
144 {
145 dev_dbg(&bridge->dev, "put\n");
146
147 bridge->info = NULL;
148 module_put(bridge->dev.parent->driver->owner);
149 mutex_unlock(&bridge->mutex);
150 put_device(&bridge->dev);
151 }
152 EXPORT_SYMBOL_GPL(fpga_bridge_put);
153
154
155
156
157
158
159
160
161
162 int fpga_bridges_enable(struct list_head *bridge_list)
163 {
164 struct fpga_bridge *bridge;
165 int ret;
166
167 list_for_each_entry(bridge, bridge_list, node) {
168 ret = fpga_bridge_enable(bridge);
169 if (ret)
170 return ret;
171 }
172
173 return 0;
174 }
175 EXPORT_SYMBOL_GPL(fpga_bridges_enable);
176
177
178
179
180
181
182
183
184
185
186 int fpga_bridges_disable(struct list_head *bridge_list)
187 {
188 struct fpga_bridge *bridge;
189 int ret;
190
191 list_for_each_entry(bridge, bridge_list, node) {
192 ret = fpga_bridge_disable(bridge);
193 if (ret)
194 return ret;
195 }
196
197 return 0;
198 }
199 EXPORT_SYMBOL_GPL(fpga_bridges_disable);
200
201
202
203
204
205
206
207
208
209 void fpga_bridges_put(struct list_head *bridge_list)
210 {
211 struct fpga_bridge *bridge, *next;
212 unsigned long flags;
213
214 list_for_each_entry_safe(bridge, next, bridge_list, node) {
215 fpga_bridge_put(bridge);
216
217 spin_lock_irqsave(&bridge_list_lock, flags);
218 list_del(&bridge->node);
219 spin_unlock_irqrestore(&bridge_list_lock, flags);
220 }
221 }
222 EXPORT_SYMBOL_GPL(fpga_bridges_put);
223
224
225
226
227
228
229
230
231
232
233
234
235 int of_fpga_bridge_get_to_list(struct device_node *np,
236 struct fpga_image_info *info,
237 struct list_head *bridge_list)
238 {
239 struct fpga_bridge *bridge;
240 unsigned long flags;
241
242 bridge = of_fpga_bridge_get(np, info);
243 if (IS_ERR(bridge))
244 return PTR_ERR(bridge);
245
246 spin_lock_irqsave(&bridge_list_lock, flags);
247 list_add(&bridge->node, bridge_list);
248 spin_unlock_irqrestore(&bridge_list_lock, flags);
249
250 return 0;
251 }
252 EXPORT_SYMBOL_GPL(of_fpga_bridge_get_to_list);
253
254
255
256
257
258
259
260
261
262
263
264
265 int fpga_bridge_get_to_list(struct device *dev,
266 struct fpga_image_info *info,
267 struct list_head *bridge_list)
268 {
269 struct fpga_bridge *bridge;
270 unsigned long flags;
271
272 bridge = fpga_bridge_get(dev, info);
273 if (IS_ERR(bridge))
274 return PTR_ERR(bridge);
275
276 spin_lock_irqsave(&bridge_list_lock, flags);
277 list_add(&bridge->node, bridge_list);
278 spin_unlock_irqrestore(&bridge_list_lock, flags);
279
280 return 0;
281 }
282 EXPORT_SYMBOL_GPL(fpga_bridge_get_to_list);
283
284 static ssize_t name_show(struct device *dev,
285 struct device_attribute *attr, char *buf)
286 {
287 struct fpga_bridge *bridge = to_fpga_bridge(dev);
288
289 return sprintf(buf, "%s\n", bridge->name);
290 }
291
292 static ssize_t state_show(struct device *dev,
293 struct device_attribute *attr, char *buf)
294 {
295 struct fpga_bridge *bridge = to_fpga_bridge(dev);
296 int enable = 1;
297
298 if (bridge->br_ops && bridge->br_ops->enable_show)
299 enable = bridge->br_ops->enable_show(bridge);
300
301 return sprintf(buf, "%s\n", enable ? "enabled" : "disabled");
302 }
303
304 static DEVICE_ATTR_RO(name);
305 static DEVICE_ATTR_RO(state);
306
307 static struct attribute *fpga_bridge_attrs[] = {
308 &dev_attr_name.attr,
309 &dev_attr_state.attr,
310 NULL,
311 };
312 ATTRIBUTE_GROUPS(fpga_bridge);
313
314
315
316
317
318
319
320
321
322
323
324
325
326 struct fpga_bridge *fpga_bridge_create(struct device *dev, const char *name,
327 const struct fpga_bridge_ops *br_ops,
328 void *priv)
329 {
330 struct fpga_bridge *bridge;
331 int id, ret = 0;
332
333 if (!name || !strlen(name)) {
334 dev_err(dev, "Attempt to register with no name!\n");
335 return NULL;
336 }
337
338 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
339 if (!bridge)
340 return NULL;
341
342 id = ida_simple_get(&fpga_bridge_ida, 0, 0, GFP_KERNEL);
343 if (id < 0) {
344 ret = id;
345 goto error_kfree;
346 }
347
348 mutex_init(&bridge->mutex);
349 INIT_LIST_HEAD(&bridge->node);
350
351 bridge->name = name;
352 bridge->br_ops = br_ops;
353 bridge->priv = priv;
354
355 device_initialize(&bridge->dev);
356 bridge->dev.groups = br_ops->groups;
357 bridge->dev.class = fpga_bridge_class;
358 bridge->dev.parent = dev;
359 bridge->dev.of_node = dev->of_node;
360 bridge->dev.id = id;
361
362 ret = dev_set_name(&bridge->dev, "br%d", id);
363 if (ret)
364 goto error_device;
365
366 return bridge;
367
368 error_device:
369 ida_simple_remove(&fpga_bridge_ida, id);
370 error_kfree:
371 kfree(bridge);
372
373 return NULL;
374 }
375 EXPORT_SYMBOL_GPL(fpga_bridge_create);
376
377
378
379
380
381 void fpga_bridge_free(struct fpga_bridge *bridge)
382 {
383 ida_simple_remove(&fpga_bridge_ida, bridge->dev.id);
384 kfree(bridge);
385 }
386 EXPORT_SYMBOL_GPL(fpga_bridge_free);
387
388 static void devm_fpga_bridge_release(struct device *dev, void *res)
389 {
390 struct fpga_bridge *bridge = *(struct fpga_bridge **)res;
391
392 fpga_bridge_free(bridge);
393 }
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412 struct fpga_bridge
413 *devm_fpga_bridge_create(struct device *dev, const char *name,
414 const struct fpga_bridge_ops *br_ops, void *priv)
415 {
416 struct fpga_bridge **ptr, *bridge;
417
418 ptr = devres_alloc(devm_fpga_bridge_release, sizeof(*ptr), GFP_KERNEL);
419 if (!ptr)
420 return NULL;
421
422 bridge = fpga_bridge_create(dev, name, br_ops, priv);
423 if (!bridge) {
424 devres_free(ptr);
425 } else {
426 *ptr = bridge;
427 devres_add(dev, ptr);
428 }
429
430 return bridge;
431 }
432 EXPORT_SYMBOL_GPL(devm_fpga_bridge_create);
433
434
435
436
437
438
439
440
441 int fpga_bridge_register(struct fpga_bridge *bridge)
442 {
443 struct device *dev = &bridge->dev;
444 int ret;
445
446 ret = device_add(dev);
447 if (ret)
448 return ret;
449
450 of_platform_populate(dev->of_node, NULL, NULL, dev);
451
452 dev_info(dev->parent, "fpga bridge [%s] registered\n", bridge->name);
453
454 return 0;
455 }
456 EXPORT_SYMBOL_GPL(fpga_bridge_register);
457
458
459
460
461
462
463
464
465 void fpga_bridge_unregister(struct fpga_bridge *bridge)
466 {
467
468
469
470
471 if (bridge->br_ops && bridge->br_ops->fpga_bridge_remove)
472 bridge->br_ops->fpga_bridge_remove(bridge);
473
474 device_unregister(&bridge->dev);
475 }
476 EXPORT_SYMBOL_GPL(fpga_bridge_unregister);
477
478 static void fpga_bridge_dev_release(struct device *dev)
479 {
480 }
481
482 static int __init fpga_bridge_dev_init(void)
483 {
484 spin_lock_init(&bridge_list_lock);
485
486 fpga_bridge_class = class_create(THIS_MODULE, "fpga_bridge");
487 if (IS_ERR(fpga_bridge_class))
488 return PTR_ERR(fpga_bridge_class);
489
490 fpga_bridge_class->dev_groups = fpga_bridge_groups;
491 fpga_bridge_class->dev_release = fpga_bridge_dev_release;
492
493 return 0;
494 }
495
496 static void __exit fpga_bridge_dev_exit(void)
497 {
498 class_destroy(fpga_bridge_class);
499 ida_destroy(&fpga_bridge_ida);
500 }
501
502 MODULE_DESCRIPTION("FPGA Bridge Driver");
503 MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
504 MODULE_LICENSE("GPL v2");
505
506 subsys_initcall(fpga_bridge_dev_init);
507 module_exit(fpga_bridge_dev_exit);