Source
178
178
NULL,
179
179
};
180
180
ATTRIBUTE_GROUPS(fpga_region);
181
181
182
182
/**
183
183
* fpga_region_create - alloc and init a struct fpga_region
184
184
* @dev: device parent
185
185
* @mgr: manager that programs this region
186
186
* @get_bridges: optional function to get bridges to a list
187
187
*
188
+
* The caller of this function is responsible for freeing the resulting region
189
+
* struct with fpga_region_free(). Using devm_fpga_region_create() instead is
190
+
* recommended.
191
+
*
188
192
* Return: struct fpga_region or NULL
189
193
*/
190
194
struct fpga_region
191
195
*fpga_region_create(struct device *dev,
192
196
struct fpga_manager *mgr,
193
197
int (*get_bridges)(struct fpga_region *))
194
198
{
195
199
struct fpga_region *region;
196
200
int id, ret = 0;
197
201
223
227
err_remove:
224
228
ida_simple_remove(&fpga_region_ida, id);
225
229
err_free:
226
230
kfree(region);
227
231
228
232
return NULL;
229
233
}
230
234
EXPORT_SYMBOL_GPL(fpga_region_create);
231
235
232
236
/**
233
-
* fpga_region_free - free a struct fpga_region
234
-
* @region: FPGA region created by fpga_region_create
237
+
* fpga_region_free - free a FPGA region created by fpga_region_create()
238
+
* @region: FPGA region
235
239
*/
236
240
void fpga_region_free(struct fpga_region *region)
237
241
{
238
242
ida_simple_remove(&fpga_region_ida, region->dev.id);
239
243
kfree(region);
240
244
}
241
245
EXPORT_SYMBOL_GPL(fpga_region_free);
242
246
247
+
static void devm_fpga_region_release(struct device *dev, void *res)
248
+
{
249
+
struct fpga_region *region = *(struct fpga_region **)res;
250
+
251
+
fpga_region_free(region);
252
+
}
253
+
254
+
/**
255
+
* devm_fpga_region_create - create and initialize a managed FPGA region struct
256
+
* @dev: device parent
257
+
* @mgr: manager that programs this region
258
+
* @get_bridges: optional function to get bridges to a list
259
+
*
260
+
* This function is intended for use in a FPGA region driver's probe function.
261
+
* After the region driver creates the region struct with
262
+
* devm_fpga_region_create(), it should register it with fpga_region_register().
263
+
* The region driver's remove function should call fpga_region_unregister().
264
+
* The region struct allocated with this function will be freed automatically on
265
+
* driver detach. This includes the case of a probe function returning error
266
+
* before calling fpga_region_register(), the struct will still get cleaned up.
267
+
*
268
+
* Return: struct fpga_region or NULL
269
+
*/
270
+
struct fpga_region
271
+
*devm_fpga_region_create(struct device *dev,
272
+
struct fpga_manager *mgr,
273
+
int (*get_bridges)(struct fpga_region *))
274
+
{
275
+
struct fpga_region **ptr, *region;
276
+
277
+
ptr = devres_alloc(devm_fpga_region_release, sizeof(*ptr), GFP_KERNEL);
278
+
if (!ptr)
279
+
return NULL;
280
+
281
+
region = fpga_region_create(dev, mgr, get_bridges);
282
+
if (!region) {
283
+
devres_free(ptr);
284
+
} else {
285
+
*ptr = region;
286
+
devres_add(dev, ptr);
287
+
}
288
+
289
+
return region;
290
+
}
291
+
EXPORT_SYMBOL_GPL(devm_fpga_region_create);
292
+
243
293
/**
244
294
* fpga_region_register - register a FPGA region
245
-
* @region: FPGA region created by fpga_region_create
295
+
* @region: FPGA region
296
+
*
246
297
* Return: 0 or -errno
247
298
*/
248
299
int fpga_region_register(struct fpga_region *region)
249
300
{
250
301
return device_add(®ion->dev);
251
-
252
302
}
253
303
EXPORT_SYMBOL_GPL(fpga_region_register);
254
304
255
305
/**
256
-
* fpga_region_unregister - unregister and free a FPGA region
306
+
* fpga_region_unregister - unregister a FPGA region
257
307
* @region: FPGA region
308
+
*
309
+
* This function is intended for use in a FPGA region driver's remove function.
258
310
*/
259
311
void fpga_region_unregister(struct fpga_region *region)
260
312
{
261
313
device_unregister(®ion->dev);
262
314
}
263
315
EXPORT_SYMBOL_GPL(fpga_region_unregister);
264
316
265
317
static void fpga_region_dev_release(struct device *dev)
266
318
{
267
-
struct fpga_region *region = to_fpga_region(dev);
268
-
269
-
fpga_region_free(region);
270
319
}
271
320
272
321
/**
273
322
* fpga_region_init - init function for fpga_region class
274
323
* Creates the fpga_region class and registers a reconfig notifier.
275
324
*/
276
325
static int __init fpga_region_init(void)
277
326
{
278
327
fpga_region_class = class_create(THIS_MODULE, "fpga_region");
279
328
if (IS_ERR(fpga_region_class))