Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * FPGA Bridge Framework Driver
4 *
5 * Copyright (C) 2013-2016 Altera Corporation, All Rights Reserved.
6 * Copyright (C) 2017 Intel Corporation
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
16static DEFINE_IDA(fpga_bridge_ida);
17static struct class *fpga_bridge_class;
18
19/* Lock for adding/removing bridges to linked lists*/
20static spinlock_t bridge_list_lock;
21
22/**
23 * fpga_bridge_enable - Enable transactions on the bridge
24 *
25 * @bridge: FPGA bridge
26 *
27 * Return: 0 for success, error code otherwise.
28 */
29int 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}
38EXPORT_SYMBOL_GPL(fpga_bridge_enable);
39
40/**
41 * fpga_bridge_disable - Disable transactions on the bridge
42 *
43 * @bridge: FPGA bridge
44 *
45 * Return: 0 for success, error code otherwise.
46 */
47int 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}
56EXPORT_SYMBOL_GPL(fpga_bridge_disable);
57
58static 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
80err_ll_mod:
81 mutex_unlock(&bridge->mutex);
82err_dev:
83 put_device(dev);
84 return ERR_PTR(ret);
85}
86
87/**
88 * of_fpga_bridge_get - get an exclusive reference to a fpga bridge
89 *
90 * @np: node pointer of a FPGA bridge
91 * @info: fpga image specific information
92 *
93 * Return fpga_bridge struct if successful.
94 * Return -EBUSY if someone already has a reference to the bridge.
95 * Return -ENODEV if @np is not a FPGA Bridge.
96 */
97struct 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}
108EXPORT_SYMBOL_GPL(of_fpga_bridge_get);
109
110static int fpga_bridge_dev_match(struct device *dev, const void *data)
111{
112 return dev->parent == data;
113}
114
115/**
116 * fpga_bridge_get - get an exclusive reference to a fpga bridge
117 * @dev: parent device that fpga bridge was registered with
118 * @info: fpga manager info
119 *
120 * Given a device, get an exclusive reference to a fpga bridge.
121 *
122 * Return: fpga bridge struct or IS_ERR() condition containing error code.
123 */
124struct 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}
136EXPORT_SYMBOL_GPL(fpga_bridge_get);
137
138/**
139 * fpga_bridge_put - release a reference to a bridge
140 *
141 * @bridge: FPGA bridge
142 */
143void 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}
152EXPORT_SYMBOL_GPL(fpga_bridge_put);
153
154/**
155 * fpga_bridges_enable - enable bridges in a list
156 * @bridge_list: list of FPGA bridges
157 *
158 * Enable each bridge in the list. If list is empty, do nothing.
159 *
160 * Return 0 for success or empty bridge list; return error code otherwise.
161 */
162int 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}
175EXPORT_SYMBOL_GPL(fpga_bridges_enable);
176
177/**
178 * fpga_bridges_disable - disable bridges in a list
179 *
180 * @bridge_list: list of FPGA bridges
181 *
182 * Disable each bridge in the list. If list is empty, do nothing.
183 *
184 * Return 0 for success or empty bridge list; return error code otherwise.
185 */
186int 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}
199EXPORT_SYMBOL_GPL(fpga_bridges_disable);
200
201/**
202 * fpga_bridges_put - put bridges
203 *
204 * @bridge_list: list of FPGA bridges
205 *
206 * For each bridge in the list, put the bridge and remove it from the list.
207 * If list is empty, do nothing.
208 */
209void 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}
222EXPORT_SYMBOL_GPL(fpga_bridges_put);
223
224/**
225 * of_fpga_bridge_get_to_list - get a bridge, add it to a list
226 *
227 * @np: node pointer of a FPGA bridge
228 * @info: fpga image specific information
229 * @bridge_list: list of FPGA bridges
230 *
231 * Get an exclusive reference to the bridge and and it to the list.
232 *
233 * Return 0 for success, error code from of_fpga_bridge_get() othewise.
234 */
235int 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}
252EXPORT_SYMBOL_GPL(of_fpga_bridge_get_to_list);
253
254/**
255 * fpga_bridge_get_to_list - given device, get a bridge, add it to a list
256 *
257 * @dev: FPGA bridge device
258 * @info: fpga image specific information
259 * @bridge_list: list of FPGA bridges
260 *
261 * Get an exclusive reference to the bridge and and it to the list.
262 *
263 * Return 0 for success, error code from fpga_bridge_get() othewise.
264 */
265int 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}
282EXPORT_SYMBOL_GPL(fpga_bridge_get_to_list);
283
284static 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
292static 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
304static DEVICE_ATTR_RO(name);
305static DEVICE_ATTR_RO(state);
306
307static struct attribute *fpga_bridge_attrs[] = {
308 &dev_attr_name.attr,
309 &dev_attr_state.attr,
310 NULL,
311};
312ATTRIBUTE_GROUPS(fpga_bridge);
313
314/**
315 * fpga_bridge_create - create and initialize a struct fpga_bridge
316 * @dev: FPGA bridge device from pdev
317 * @name: FPGA bridge name
318 * @br_ops: pointer to structure of fpga bridge ops
319 * @priv: FPGA bridge private data
320 *
321 * The caller of this function is responsible for freeing the bridge with
322 * fpga_bridge_free(). Using devm_fpga_bridge_create() instead is recommended.
323 *
324 * Return: struct fpga_bridge or NULL
325 */
326struct 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
368error_device:
369 ida_simple_remove(&fpga_bridge_ida, id);
370error_kfree:
371 kfree(bridge);
372
373 return NULL;
374}
375EXPORT_SYMBOL_GPL(fpga_bridge_create);
376
377/**
378 * fpga_bridge_free - free a fpga bridge created by fpga_bridge_create()
379 * @bridge: FPGA bridge struct
380 */
381void fpga_bridge_free(struct fpga_bridge *bridge)
382{
383 ida_simple_remove(&fpga_bridge_ida, bridge->dev.id);
384 kfree(bridge);
385}
386EXPORT_SYMBOL_GPL(fpga_bridge_free);
387
388static 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 * devm_fpga_bridge_create - create and init a managed struct fpga_bridge
397 * @dev: FPGA bridge device from pdev
398 * @name: FPGA bridge name
399 * @br_ops: pointer to structure of fpga bridge ops
400 * @priv: FPGA bridge private data
401 *
402 * This function is intended for use in a FPGA bridge driver's probe function.
403 * After the bridge driver creates the struct with devm_fpga_bridge_create(), it
404 * should register the bridge with fpga_bridge_register(). The bridge driver's
405 * remove function should call fpga_bridge_unregister(). The bridge struct
406 * allocated with this function will be freed automatically on driver detach.
407 * This includes the case of a probe function returning error before calling
408 * fpga_bridge_register(), the struct will still get cleaned up.
409 *
410 * Return: struct fpga_bridge or NULL
411 */
412struct 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}
432EXPORT_SYMBOL_GPL(devm_fpga_bridge_create);
433
434/**
435 * fpga_bridge_register - register a FPGA bridge
436 *
437 * @bridge: FPGA bridge struct
438 *
439 * Return: 0 for success, error code otherwise.
440 */
441int 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}
456EXPORT_SYMBOL_GPL(fpga_bridge_register);
457
458/**
459 * fpga_bridge_unregister - unregister a FPGA bridge
460 *
461 * @bridge: FPGA bridge struct
462 *
463 * This function is intended for use in a FPGA bridge driver's remove function.
464 */
465void fpga_bridge_unregister(struct fpga_bridge *bridge)
466{
467 /*
468 * If the low level driver provides a method for putting bridge into
469 * a desired state upon unregister, do it.
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}
476EXPORT_SYMBOL_GPL(fpga_bridge_unregister);
477
478static void fpga_bridge_dev_release(struct device *dev)
479{
480}
481
482static 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
496static void __exit fpga_bridge_dev_exit(void)
497{
498 class_destroy(fpga_bridge_class);
499 ida_destroy(&fpga_bridge_ida);
500}
501
502MODULE_DESCRIPTION("FPGA Bridge Driver");
503MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
504MODULE_LICENSE("GPL v2");
505
506subsys_initcall(fpga_bridge_dev_init);
507module_exit(fpga_bridge_dev_exit);
1/*
2 * FPGA Bridge Framework Driver
3 *
4 * Copyright (C) 2013-2016 Altera Corporation, All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18#include <linux/fpga/fpga-bridge.h>
19#include <linux/idr.h>
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/of_platform.h>
23#include <linux/slab.h>
24#include <linux/spinlock.h>
25
26static DEFINE_IDA(fpga_bridge_ida);
27static struct class *fpga_bridge_class;
28
29/* Lock for adding/removing bridges to linked lists*/
30spinlock_t bridge_list_lock;
31
32static int fpga_bridge_of_node_match(struct device *dev, const void *data)
33{
34 return dev->of_node == data;
35}
36
37/**
38 * fpga_bridge_enable - Enable transactions on the bridge
39 *
40 * @bridge: FPGA bridge
41 *
42 * Return: 0 for success, error code otherwise.
43 */
44int fpga_bridge_enable(struct fpga_bridge *bridge)
45{
46 dev_dbg(&bridge->dev, "enable\n");
47
48 if (bridge->br_ops && bridge->br_ops->enable_set)
49 return bridge->br_ops->enable_set(bridge, 1);
50
51 return 0;
52}
53EXPORT_SYMBOL_GPL(fpga_bridge_enable);
54
55/**
56 * fpga_bridge_disable - Disable transactions on the bridge
57 *
58 * @bridge: FPGA bridge
59 *
60 * Return: 0 for success, error code otherwise.
61 */
62int fpga_bridge_disable(struct fpga_bridge *bridge)
63{
64 dev_dbg(&bridge->dev, "disable\n");
65
66 if (bridge->br_ops && bridge->br_ops->enable_set)
67 return bridge->br_ops->enable_set(bridge, 0);
68
69 return 0;
70}
71EXPORT_SYMBOL_GPL(fpga_bridge_disable);
72
73/**
74 * of_fpga_bridge_get - get an exclusive reference to a fpga bridge
75 *
76 * @np: node pointer of a FPGA bridge
77 * @info: fpga image specific information
78 *
79 * Return fpga_bridge struct if successful.
80 * Return -EBUSY if someone already has a reference to the bridge.
81 * Return -ENODEV if @np is not a FPGA Bridge.
82 */
83struct fpga_bridge *of_fpga_bridge_get(struct device_node *np,
84 struct fpga_image_info *info)
85
86{
87 struct device *dev;
88 struct fpga_bridge *bridge;
89 int ret = -ENODEV;
90
91 dev = class_find_device(fpga_bridge_class, NULL, np,
92 fpga_bridge_of_node_match);
93 if (!dev)
94 goto err_dev;
95
96 bridge = to_fpga_bridge(dev);
97 if (!bridge)
98 goto err_dev;
99
100 bridge->info = info;
101
102 if (!mutex_trylock(&bridge->mutex)) {
103 ret = -EBUSY;
104 goto err_dev;
105 }
106
107 if (!try_module_get(dev->parent->driver->owner))
108 goto err_ll_mod;
109
110 dev_dbg(&bridge->dev, "get\n");
111
112 return bridge;
113
114err_ll_mod:
115 mutex_unlock(&bridge->mutex);
116err_dev:
117 put_device(dev);
118 return ERR_PTR(ret);
119}
120EXPORT_SYMBOL_GPL(of_fpga_bridge_get);
121
122/**
123 * fpga_bridge_put - release a reference to a bridge
124 *
125 * @bridge: FPGA bridge
126 */
127void fpga_bridge_put(struct fpga_bridge *bridge)
128{
129 dev_dbg(&bridge->dev, "put\n");
130
131 bridge->info = NULL;
132 module_put(bridge->dev.parent->driver->owner);
133 mutex_unlock(&bridge->mutex);
134 put_device(&bridge->dev);
135}
136EXPORT_SYMBOL_GPL(fpga_bridge_put);
137
138/**
139 * fpga_bridges_enable - enable bridges in a list
140 * @bridge_list: list of FPGA bridges
141 *
142 * Enable each bridge in the list. If list is empty, do nothing.
143 *
144 * Return 0 for success or empty bridge list; return error code otherwise.
145 */
146int fpga_bridges_enable(struct list_head *bridge_list)
147{
148 struct fpga_bridge *bridge;
149 struct list_head *node;
150 int ret;
151
152 list_for_each(node, bridge_list) {
153 bridge = list_entry(node, struct fpga_bridge, node);
154 ret = fpga_bridge_enable(bridge);
155 if (ret)
156 return ret;
157 }
158
159 return 0;
160}
161EXPORT_SYMBOL_GPL(fpga_bridges_enable);
162
163/**
164 * fpga_bridges_disable - disable bridges in a list
165 *
166 * @bridge_list: list of FPGA bridges
167 *
168 * Disable each bridge in the list. If list is empty, do nothing.
169 *
170 * Return 0 for success or empty bridge list; return error code otherwise.
171 */
172int fpga_bridges_disable(struct list_head *bridge_list)
173{
174 struct fpga_bridge *bridge;
175 struct list_head *node;
176 int ret;
177
178 list_for_each(node, bridge_list) {
179 bridge = list_entry(node, struct fpga_bridge, node);
180 ret = fpga_bridge_disable(bridge);
181 if (ret)
182 return ret;
183 }
184
185 return 0;
186}
187EXPORT_SYMBOL_GPL(fpga_bridges_disable);
188
189/**
190 * fpga_bridges_put - put bridges
191 *
192 * @bridge_list: list of FPGA bridges
193 *
194 * For each bridge in the list, put the bridge and remove it from the list.
195 * If list is empty, do nothing.
196 */
197void fpga_bridges_put(struct list_head *bridge_list)
198{
199 struct fpga_bridge *bridge;
200 struct list_head *node, *next;
201 unsigned long flags;
202
203 list_for_each_safe(node, next, bridge_list) {
204 bridge = list_entry(node, struct fpga_bridge, node);
205
206 fpga_bridge_put(bridge);
207
208 spin_lock_irqsave(&bridge_list_lock, flags);
209 list_del(&bridge->node);
210 spin_unlock_irqrestore(&bridge_list_lock, flags);
211 }
212}
213EXPORT_SYMBOL_GPL(fpga_bridges_put);
214
215/**
216 * fpga_bridges_get_to_list - get a bridge, add it to a list
217 *
218 * @np: node pointer of a FPGA bridge
219 * @info: fpga image specific information
220 * @bridge_list: list of FPGA bridges
221 *
222 * Get an exclusive reference to the bridge and and it to the list.
223 *
224 * Return 0 for success, error code from of_fpga_bridge_get() othewise.
225 */
226int fpga_bridge_get_to_list(struct device_node *np,
227 struct fpga_image_info *info,
228 struct list_head *bridge_list)
229{
230 struct fpga_bridge *bridge;
231 unsigned long flags;
232
233 bridge = of_fpga_bridge_get(np, info);
234 if (IS_ERR(bridge))
235 return PTR_ERR(bridge);
236
237 spin_lock_irqsave(&bridge_list_lock, flags);
238 list_add(&bridge->node, bridge_list);
239 spin_unlock_irqrestore(&bridge_list_lock, flags);
240
241 return 0;
242}
243EXPORT_SYMBOL_GPL(fpga_bridge_get_to_list);
244
245static ssize_t name_show(struct device *dev,
246 struct device_attribute *attr, char *buf)
247{
248 struct fpga_bridge *bridge = to_fpga_bridge(dev);
249
250 return sprintf(buf, "%s\n", bridge->name);
251}
252
253static ssize_t state_show(struct device *dev,
254 struct device_attribute *attr, char *buf)
255{
256 struct fpga_bridge *bridge = to_fpga_bridge(dev);
257 int enable = 1;
258
259 if (bridge->br_ops && bridge->br_ops->enable_show)
260 enable = bridge->br_ops->enable_show(bridge);
261
262 return sprintf(buf, "%s\n", enable ? "enabled" : "disabled");
263}
264
265static DEVICE_ATTR_RO(name);
266static DEVICE_ATTR_RO(state);
267
268static struct attribute *fpga_bridge_attrs[] = {
269 &dev_attr_name.attr,
270 &dev_attr_state.attr,
271 NULL,
272};
273ATTRIBUTE_GROUPS(fpga_bridge);
274
275/**
276 * fpga_bridge_register - register a fpga bridge driver
277 * @dev: FPGA bridge device from pdev
278 * @name: FPGA bridge name
279 * @br_ops: pointer to structure of fpga bridge ops
280 * @priv: FPGA bridge private data
281 *
282 * Return: 0 for success, error code otherwise.
283 */
284int fpga_bridge_register(struct device *dev, const char *name,
285 const struct fpga_bridge_ops *br_ops, void *priv)
286{
287 struct fpga_bridge *bridge;
288 int id, ret = 0;
289
290 if (!name || !strlen(name)) {
291 dev_err(dev, "Attempt to register with no name!\n");
292 return -EINVAL;
293 }
294
295 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
296 if (!bridge)
297 return -ENOMEM;
298
299 id = ida_simple_get(&fpga_bridge_ida, 0, 0, GFP_KERNEL);
300 if (id < 0) {
301 ret = id;
302 goto error_kfree;
303 }
304
305 mutex_init(&bridge->mutex);
306 INIT_LIST_HEAD(&bridge->node);
307
308 bridge->name = name;
309 bridge->br_ops = br_ops;
310 bridge->priv = priv;
311
312 device_initialize(&bridge->dev);
313 bridge->dev.class = fpga_bridge_class;
314 bridge->dev.parent = dev;
315 bridge->dev.of_node = dev->of_node;
316 bridge->dev.id = id;
317 dev_set_drvdata(dev, bridge);
318
319 ret = dev_set_name(&bridge->dev, "br%d", id);
320 if (ret)
321 goto error_device;
322
323 ret = device_add(&bridge->dev);
324 if (ret)
325 goto error_device;
326
327 of_platform_populate(dev->of_node, NULL, NULL, dev);
328
329 dev_info(bridge->dev.parent, "fpga bridge [%s] registered\n",
330 bridge->name);
331
332 return 0;
333
334error_device:
335 ida_simple_remove(&fpga_bridge_ida, id);
336error_kfree:
337 kfree(bridge);
338
339 return ret;
340}
341EXPORT_SYMBOL_GPL(fpga_bridge_register);
342
343/**
344 * fpga_bridge_unregister - unregister a fpga bridge driver
345 * @dev: FPGA bridge device from pdev
346 */
347void fpga_bridge_unregister(struct device *dev)
348{
349 struct fpga_bridge *bridge = dev_get_drvdata(dev);
350
351 /*
352 * If the low level driver provides a method for putting bridge into
353 * a desired state upon unregister, do it.
354 */
355 if (bridge->br_ops && bridge->br_ops->fpga_bridge_remove)
356 bridge->br_ops->fpga_bridge_remove(bridge);
357
358 device_unregister(&bridge->dev);
359}
360EXPORT_SYMBOL_GPL(fpga_bridge_unregister);
361
362static void fpga_bridge_dev_release(struct device *dev)
363{
364 struct fpga_bridge *bridge = to_fpga_bridge(dev);
365
366 ida_simple_remove(&fpga_bridge_ida, bridge->dev.id);
367 kfree(bridge);
368}
369
370static int __init fpga_bridge_dev_init(void)
371{
372 spin_lock_init(&bridge_list_lock);
373
374 fpga_bridge_class = class_create(THIS_MODULE, "fpga_bridge");
375 if (IS_ERR(fpga_bridge_class))
376 return PTR_ERR(fpga_bridge_class);
377
378 fpga_bridge_class->dev_groups = fpga_bridge_groups;
379 fpga_bridge_class->dev_release = fpga_bridge_dev_release;
380
381 return 0;
382}
383
384static void __exit fpga_bridge_dev_exit(void)
385{
386 class_destroy(fpga_bridge_class);
387 ida_destroy(&fpga_bridge_ida);
388}
389
390MODULE_DESCRIPTION("FPGA Bridge Driver");
391MODULE_AUTHOR("Alan Tull <atull@opensource.altera.com>");
392MODULE_LICENSE("GPL v2");
393
394subsys_initcall(fpga_bridge_dev_init);
395module_exit(fpga_bridge_dev_exit);