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 const struct class fpga_bridge_class;
18
19/* Lock for adding/removing bridges to linked lists*/
20static DEFINE_SPINLOCK(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 an fpga bridge
89 *
90 * @np: node pointer of an FPGA bridge.
91 * @info: fpga image specific information.
92 *
93 * Return:
94 * * fpga_bridge struct pointer if successful.
95 * * -EBUSY if someone already has a reference to the bridge.
96 * * -ENODEV if @np is not an FPGA Bridge or can't take parent driver refcount.
97 */
98struct fpga_bridge *of_fpga_bridge_get(struct device_node *np,
99 struct fpga_image_info *info)
100{
101 struct device *dev;
102
103 dev = class_find_device_by_of_node(&fpga_bridge_class, np);
104 if (!dev)
105 return ERR_PTR(-ENODEV);
106
107 return __fpga_bridge_get(dev, info);
108}
109EXPORT_SYMBOL_GPL(of_fpga_bridge_get);
110
111static int fpga_bridge_dev_match(struct device *dev, const void *data)
112{
113 return dev->parent == data;
114}
115
116/**
117 * fpga_bridge_get - get an exclusive reference to an fpga bridge
118 * @dev: parent device that fpga bridge was registered with
119 * @info: fpga image specific information
120 *
121 * Given a device, get an exclusive reference to an fpga bridge.
122 *
123 * Return: fpga bridge struct or IS_ERR() condition containing error code.
124 */
125struct fpga_bridge *fpga_bridge_get(struct device *dev,
126 struct fpga_image_info *info)
127{
128 struct device *bridge_dev;
129
130 bridge_dev = class_find_device(&fpga_bridge_class, NULL, dev,
131 fpga_bridge_dev_match);
132 if (!bridge_dev)
133 return ERR_PTR(-ENODEV);
134
135 return __fpga_bridge_get(bridge_dev, info);
136}
137EXPORT_SYMBOL_GPL(fpga_bridge_get);
138
139/**
140 * fpga_bridge_put - release a reference to a bridge
141 *
142 * @bridge: FPGA bridge
143 */
144void fpga_bridge_put(struct fpga_bridge *bridge)
145{
146 dev_dbg(&bridge->dev, "put\n");
147
148 bridge->info = NULL;
149 module_put(bridge->dev.parent->driver->owner);
150 mutex_unlock(&bridge->mutex);
151 put_device(&bridge->dev);
152}
153EXPORT_SYMBOL_GPL(fpga_bridge_put);
154
155/**
156 * fpga_bridges_enable - enable bridges in a list
157 * @bridge_list: list of FPGA bridges
158 *
159 * Enable each bridge in the list. If list is empty, do nothing.
160 *
161 * Return: 0 for success or empty bridge list or an error code otherwise.
162 */
163int fpga_bridges_enable(struct list_head *bridge_list)
164{
165 struct fpga_bridge *bridge;
166 int ret;
167
168 list_for_each_entry(bridge, bridge_list, node) {
169 ret = fpga_bridge_enable(bridge);
170 if (ret)
171 return ret;
172 }
173
174 return 0;
175}
176EXPORT_SYMBOL_GPL(fpga_bridges_enable);
177
178/**
179 * fpga_bridges_disable - disable bridges in a list
180 *
181 * @bridge_list: list of FPGA bridges
182 *
183 * Disable each bridge in the list. If list is empty, do nothing.
184 *
185 * Return: 0 for success or empty bridge list or an error code otherwise.
186 */
187int fpga_bridges_disable(struct list_head *bridge_list)
188{
189 struct fpga_bridge *bridge;
190 int ret;
191
192 list_for_each_entry(bridge, bridge_list, node) {
193 ret = fpga_bridge_disable(bridge);
194 if (ret)
195 return ret;
196 }
197
198 return 0;
199}
200EXPORT_SYMBOL_GPL(fpga_bridges_disable);
201
202/**
203 * fpga_bridges_put - put bridges
204 *
205 * @bridge_list: list of FPGA bridges
206 *
207 * For each bridge in the list, put the bridge and remove it from the list.
208 * If list is empty, do nothing.
209 */
210void fpga_bridges_put(struct list_head *bridge_list)
211{
212 struct fpga_bridge *bridge, *next;
213 unsigned long flags;
214
215 list_for_each_entry_safe(bridge, next, bridge_list, node) {
216 fpga_bridge_put(bridge);
217
218 spin_lock_irqsave(&bridge_list_lock, flags);
219 list_del(&bridge->node);
220 spin_unlock_irqrestore(&bridge_list_lock, flags);
221 }
222}
223EXPORT_SYMBOL_GPL(fpga_bridges_put);
224
225/**
226 * of_fpga_bridge_get_to_list - get a bridge, add it to a list
227 *
228 * @np: node pointer of an FPGA bridge
229 * @info: fpga image specific information
230 * @bridge_list: list of FPGA bridges
231 *
232 * Get an exclusive reference to the bridge and it to the list.
233 *
234 * Return: 0 for success, error code from of_fpga_bridge_get() otherwise.
235 */
236int of_fpga_bridge_get_to_list(struct device_node *np,
237 struct fpga_image_info *info,
238 struct list_head *bridge_list)
239{
240 struct fpga_bridge *bridge;
241 unsigned long flags;
242
243 bridge = of_fpga_bridge_get(np, info);
244 if (IS_ERR(bridge))
245 return PTR_ERR(bridge);
246
247 spin_lock_irqsave(&bridge_list_lock, flags);
248 list_add(&bridge->node, bridge_list);
249 spin_unlock_irqrestore(&bridge_list_lock, flags);
250
251 return 0;
252}
253EXPORT_SYMBOL_GPL(of_fpga_bridge_get_to_list);
254
255/**
256 * fpga_bridge_get_to_list - given device, get a bridge, add it to a list
257 *
258 * @dev: FPGA bridge device
259 * @info: fpga image specific information
260 * @bridge_list: list of FPGA bridges
261 *
262 * Get an exclusive reference to the bridge and it to the list.
263 *
264 * Return: 0 for success, error code from fpga_bridge_get() otherwise.
265 */
266int fpga_bridge_get_to_list(struct device *dev,
267 struct fpga_image_info *info,
268 struct list_head *bridge_list)
269{
270 struct fpga_bridge *bridge;
271 unsigned long flags;
272
273 bridge = fpga_bridge_get(dev, info);
274 if (IS_ERR(bridge))
275 return PTR_ERR(bridge);
276
277 spin_lock_irqsave(&bridge_list_lock, flags);
278 list_add(&bridge->node, bridge_list);
279 spin_unlock_irqrestore(&bridge_list_lock, flags);
280
281 return 0;
282}
283EXPORT_SYMBOL_GPL(fpga_bridge_get_to_list);
284
285static ssize_t name_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
287{
288 struct fpga_bridge *bridge = to_fpga_bridge(dev);
289
290 return sprintf(buf, "%s\n", bridge->name);
291}
292
293static ssize_t state_show(struct device *dev,
294 struct device_attribute *attr, char *buf)
295{
296 struct fpga_bridge *bridge = to_fpga_bridge(dev);
297 int state = 1;
298
299 if (bridge->br_ops && bridge->br_ops->enable_show) {
300 state = bridge->br_ops->enable_show(bridge);
301 if (state < 0)
302 return state;
303 }
304
305 return sysfs_emit(buf, "%s\n", state ? "enabled" : "disabled");
306}
307
308static DEVICE_ATTR_RO(name);
309static DEVICE_ATTR_RO(state);
310
311static struct attribute *fpga_bridge_attrs[] = {
312 &dev_attr_name.attr,
313 &dev_attr_state.attr,
314 NULL,
315};
316ATTRIBUTE_GROUPS(fpga_bridge);
317
318/**
319 * fpga_bridge_register - create and register an FPGA Bridge device
320 * @parent: FPGA bridge device from pdev
321 * @name: FPGA bridge name
322 * @br_ops: pointer to structure of fpga bridge ops
323 * @priv: FPGA bridge private data
324 *
325 * Return: struct fpga_bridge pointer or ERR_PTR()
326 */
327struct fpga_bridge *
328fpga_bridge_register(struct device *parent, const char *name,
329 const struct fpga_bridge_ops *br_ops,
330 void *priv)
331{
332 struct fpga_bridge *bridge;
333 int id, ret;
334
335 if (!br_ops) {
336 dev_err(parent, "Attempt to register without fpga_bridge_ops\n");
337 return ERR_PTR(-EINVAL);
338 }
339
340 if (!name || !strlen(name)) {
341 dev_err(parent, "Attempt to register with no name!\n");
342 return ERR_PTR(-EINVAL);
343 }
344
345 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
346 if (!bridge)
347 return ERR_PTR(-ENOMEM);
348
349 id = ida_alloc(&fpga_bridge_ida, GFP_KERNEL);
350 if (id < 0) {
351 ret = id;
352 goto error_kfree;
353 }
354
355 mutex_init(&bridge->mutex);
356 INIT_LIST_HEAD(&bridge->node);
357
358 bridge->name = name;
359 bridge->br_ops = br_ops;
360 bridge->priv = priv;
361
362 bridge->dev.groups = br_ops->groups;
363 bridge->dev.class = &fpga_bridge_class;
364 bridge->dev.parent = parent;
365 bridge->dev.of_node = parent->of_node;
366 bridge->dev.id = id;
367
368 ret = dev_set_name(&bridge->dev, "br%d", id);
369 if (ret)
370 goto error_device;
371
372 ret = device_register(&bridge->dev);
373 if (ret) {
374 put_device(&bridge->dev);
375 return ERR_PTR(ret);
376 }
377
378 of_platform_populate(bridge->dev.of_node, NULL, NULL, &bridge->dev);
379
380 return bridge;
381
382error_device:
383 ida_free(&fpga_bridge_ida, id);
384error_kfree:
385 kfree(bridge);
386
387 return ERR_PTR(ret);
388}
389EXPORT_SYMBOL_GPL(fpga_bridge_register);
390
391/**
392 * fpga_bridge_unregister - unregister an FPGA bridge
393 *
394 * @bridge: FPGA bridge struct
395 *
396 * This function is intended for use in an FPGA bridge driver's remove function.
397 */
398void fpga_bridge_unregister(struct fpga_bridge *bridge)
399{
400 /*
401 * If the low level driver provides a method for putting bridge into
402 * a desired state upon unregister, do it.
403 */
404 if (bridge->br_ops && bridge->br_ops->fpga_bridge_remove)
405 bridge->br_ops->fpga_bridge_remove(bridge);
406
407 device_unregister(&bridge->dev);
408}
409EXPORT_SYMBOL_GPL(fpga_bridge_unregister);
410
411static void fpga_bridge_dev_release(struct device *dev)
412{
413 struct fpga_bridge *bridge = to_fpga_bridge(dev);
414
415 ida_free(&fpga_bridge_ida, bridge->dev.id);
416 kfree(bridge);
417}
418
419static const struct class fpga_bridge_class = {
420 .name = "fpga_bridge",
421 .dev_groups = fpga_bridge_groups,
422 .dev_release = fpga_bridge_dev_release,
423};
424
425static int __init fpga_bridge_dev_init(void)
426{
427 return class_register(&fpga_bridge_class);
428}
429
430static void __exit fpga_bridge_dev_exit(void)
431{
432 class_unregister(&fpga_bridge_class);
433 ida_destroy(&fpga_bridge_ida);
434}
435
436MODULE_DESCRIPTION("FPGA Bridge Driver");
437MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
438MODULE_LICENSE("GPL v2");
439
440subsys_initcall(fpga_bridge_dev_init);
441module_exit(fpga_bridge_dev_exit);
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 DEFINE_SPINLOCK(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 an fpga bridge
89 *
90 * @np: node pointer of an 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 an 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 an 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 an 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 an 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 it to the list.
232 *
233 * Return 0 for success, error code from of_fpga_bridge_get() otherwise.
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 it to the list.
262 *
263 * Return 0 for success, error code from fpga_bridge_get() otherwise.
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_register - create and register an FPGA Bridge device
316 * @parent: 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 * Return: struct fpga_bridge pointer or ERR_PTR()
322 */
323struct fpga_bridge *
324fpga_bridge_register(struct device *parent, const char *name,
325 const struct fpga_bridge_ops *br_ops,
326 void *priv)
327{
328 struct fpga_bridge *bridge;
329 int id, ret;
330
331 if (!br_ops) {
332 dev_err(parent, "Attempt to register without fpga_bridge_ops\n");
333 return ERR_PTR(-EINVAL);
334 }
335
336 if (!name || !strlen(name)) {
337 dev_err(parent, "Attempt to register with no name!\n");
338 return ERR_PTR(-EINVAL);
339 }
340
341 bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
342 if (!bridge)
343 return ERR_PTR(-ENOMEM);
344
345 id = ida_alloc(&fpga_bridge_ida, GFP_KERNEL);
346 if (id < 0) {
347 ret = id;
348 goto error_kfree;
349 }
350
351 mutex_init(&bridge->mutex);
352 INIT_LIST_HEAD(&bridge->node);
353
354 bridge->name = name;
355 bridge->br_ops = br_ops;
356 bridge->priv = priv;
357
358 bridge->dev.groups = br_ops->groups;
359 bridge->dev.class = fpga_bridge_class;
360 bridge->dev.parent = parent;
361 bridge->dev.of_node = parent->of_node;
362 bridge->dev.id = id;
363 of_platform_populate(bridge->dev.of_node, NULL, NULL, &bridge->dev);
364
365 ret = dev_set_name(&bridge->dev, "br%d", id);
366 if (ret)
367 goto error_device;
368
369 ret = device_register(&bridge->dev);
370 if (ret) {
371 put_device(&bridge->dev);
372 return ERR_PTR(ret);
373 }
374
375 return bridge;
376
377error_device:
378 ida_free(&fpga_bridge_ida, id);
379error_kfree:
380 kfree(bridge);
381
382 return ERR_PTR(ret);
383}
384EXPORT_SYMBOL_GPL(fpga_bridge_register);
385
386/**
387 * fpga_bridge_unregister - unregister an FPGA bridge
388 *
389 * @bridge: FPGA bridge struct
390 *
391 * This function is intended for use in an FPGA bridge driver's remove function.
392 */
393void fpga_bridge_unregister(struct fpga_bridge *bridge)
394{
395 /*
396 * If the low level driver provides a method for putting bridge into
397 * a desired state upon unregister, do it.
398 */
399 if (bridge->br_ops && bridge->br_ops->fpga_bridge_remove)
400 bridge->br_ops->fpga_bridge_remove(bridge);
401
402 device_unregister(&bridge->dev);
403}
404EXPORT_SYMBOL_GPL(fpga_bridge_unregister);
405
406static void fpga_bridge_dev_release(struct device *dev)
407{
408 struct fpga_bridge *bridge = to_fpga_bridge(dev);
409
410 ida_free(&fpga_bridge_ida, bridge->dev.id);
411 kfree(bridge);
412}
413
414static int __init fpga_bridge_dev_init(void)
415{
416 fpga_bridge_class = class_create(THIS_MODULE, "fpga_bridge");
417 if (IS_ERR(fpga_bridge_class))
418 return PTR_ERR(fpga_bridge_class);
419
420 fpga_bridge_class->dev_groups = fpga_bridge_groups;
421 fpga_bridge_class->dev_release = fpga_bridge_dev_release;
422
423 return 0;
424}
425
426static void __exit fpga_bridge_dev_exit(void)
427{
428 class_destroy(fpga_bridge_class);
429 ida_destroy(&fpga_bridge_ida);
430}
431
432MODULE_DESCRIPTION("FPGA Bridge Driver");
433MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
434MODULE_LICENSE("GPL v2");
435
436subsys_initcall(fpga_bridge_dev_init);
437module_exit(fpga_bridge_dev_exit);