hc
2024-01-05 071106ecf68c401173c58808b1cf5f68cc50d390
kernel/drivers/fpga/fpga-region.c
....@@ -1,6 +1,6 @@
11 // SPDX-License-Identifier: GPL-2.0
22 /*
3
- * FPGA Region - Device Tree support for FPGA programming under Linux
3
+ * FPGA Region - Support for FPGA programming under Linux
44 *
55 * Copyright (C) 2013-2016 Altera Corporation
66 * Copyright (C) 2017 Intel Corporation
....@@ -185,6 +185,10 @@
185185 * @mgr: manager that programs this region
186186 * @get_bridges: optional function to get bridges to a list
187187 *
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
+ *
188192 * Return: struct fpga_region or NULL
189193 */
190194 struct fpga_region
....@@ -230,8 +234,8 @@
230234 EXPORT_SYMBOL_GPL(fpga_region_create);
231235
232236 /**
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
235239 */
236240 void fpga_region_free(struct fpga_region *region)
237241 {
....@@ -240,21 +244,69 @@
240244 }
241245 EXPORT_SYMBOL_GPL(fpga_region_free);
242246
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
+
243293 /**
244294 * fpga_region_register - register a FPGA region
245
- * @region: FPGA region created by fpga_region_create
295
+ * @region: FPGA region
296
+ *
246297 * Return: 0 or -errno
247298 */
248299 int fpga_region_register(struct fpga_region *region)
249300 {
250301 return device_add(&region->dev);
251
-
252302 }
253303 EXPORT_SYMBOL_GPL(fpga_region_register);
254304
255305 /**
256
- * fpga_region_unregister - unregister and free a FPGA region
306
+ * fpga_region_unregister - unregister a FPGA region
257307 * @region: FPGA region
308
+ *
309
+ * This function is intended for use in a FPGA region driver's remove function.
258310 */
259311 void fpga_region_unregister(struct fpga_region *region)
260312 {
....@@ -264,9 +316,6 @@
264316
265317 static void fpga_region_dev_release(struct device *dev)
266318 {
267
- struct fpga_region *region = to_fpga_region(dev);
268
-
269
- fpga_region_free(region);
270319 }
271320
272321 /**