hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/fpga/fpga-bridge.c
....@@ -19,11 +19,6 @@
1919 /* Lock for adding/removing bridges to linked lists*/
2020 static spinlock_t bridge_list_lock;
2121
22
-static int fpga_bridge_of_node_match(struct device *dev, const void *data)
23
-{
24
- return dev->of_node == data;
25
-}
26
-
2722 /**
2823 * fpga_bridge_enable - Enable transactions on the bridge
2924 *
....@@ -104,8 +99,7 @@
10499 {
105100 struct device *dev;
106101
107
- dev = class_find_device(fpga_bridge_class, NULL, np,
108
- fpga_bridge_of_node_match);
102
+ dev = class_find_device_by_of_node(fpga_bridge_class, np);
109103 if (!dev)
110104 return ERR_PTR(-ENODEV);
111105
....@@ -121,7 +115,7 @@
121115 /**
122116 * fpga_bridge_get - get an exclusive reference to a fpga bridge
123117 * @dev: parent device that fpga bridge was registered with
124
- * @info: fpga manager info
118
+ * @info: fpga image specific information
125119 *
126120 * Given a device, get an exclusive reference to a fpga bridge.
127121 *
....@@ -324,6 +318,9 @@
324318 * @br_ops: pointer to structure of fpga bridge ops
325319 * @priv: FPGA bridge private data
326320 *
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
+ *
327324 * Return: struct fpga_bridge or NULL
328325 */
329326 struct fpga_bridge *fpga_bridge_create(struct device *dev, const char *name,
....@@ -331,7 +328,7 @@
331328 void *priv)
332329 {
333330 struct fpga_bridge *bridge;
334
- int id, ret = 0;
331
+ int id, ret;
335332
336333 if (!name || !strlen(name)) {
337334 dev_err(dev, "Attempt to register with no name!\n");
....@@ -343,10 +340,8 @@
343340 return NULL;
344341
345342 id = ida_simple_get(&fpga_bridge_ida, 0, 0, GFP_KERNEL);
346
- if (id < 0) {
347
- ret = id;
343
+ if (id < 0)
348344 goto error_kfree;
349
- }
350345
351346 mutex_init(&bridge->mutex);
352347 INIT_LIST_HEAD(&bridge->node);
....@@ -378,8 +373,8 @@
378373 EXPORT_SYMBOL_GPL(fpga_bridge_create);
379374
380375 /**
381
- * fpga_bridge_free - free a fpga bridge and its id
382
- * @bridge: FPGA bridge struct created by fpga_bridge_create
376
+ * fpga_bridge_free - free a fpga bridge created by fpga_bridge_create()
377
+ * @bridge: FPGA bridge struct
383378 */
384379 void fpga_bridge_free(struct fpga_bridge *bridge)
385380 {
....@@ -388,9 +383,56 @@
388383 }
389384 EXPORT_SYMBOL_GPL(fpga_bridge_free);
390385
386
+static void devm_fpga_bridge_release(struct device *dev, void *res)
387
+{
388
+ struct fpga_bridge *bridge = *(struct fpga_bridge **)res;
389
+
390
+ fpga_bridge_free(bridge);
391
+}
392
+
391393 /**
392
- * fpga_bridge_register - register a fpga bridge
393
- * @bridge: FPGA bridge struct created by fpga_bridge_create
394
+ * devm_fpga_bridge_create - create and init a managed struct fpga_bridge
395
+ * @dev: FPGA bridge device from pdev
396
+ * @name: FPGA bridge name
397
+ * @br_ops: pointer to structure of fpga bridge ops
398
+ * @priv: FPGA bridge private data
399
+ *
400
+ * This function is intended for use in a FPGA bridge driver's probe function.
401
+ * After the bridge driver creates the struct with devm_fpga_bridge_create(), it
402
+ * should register the bridge with fpga_bridge_register(). The bridge driver's
403
+ * remove function should call fpga_bridge_unregister(). The bridge struct
404
+ * allocated with this function will be freed automatically on driver detach.
405
+ * This includes the case of a probe function returning error before calling
406
+ * fpga_bridge_register(), the struct will still get cleaned up.
407
+ *
408
+ * Return: struct fpga_bridge or NULL
409
+ */
410
+struct fpga_bridge
411
+*devm_fpga_bridge_create(struct device *dev, const char *name,
412
+ const struct fpga_bridge_ops *br_ops, void *priv)
413
+{
414
+ struct fpga_bridge **ptr, *bridge;
415
+
416
+ ptr = devres_alloc(devm_fpga_bridge_release, sizeof(*ptr), GFP_KERNEL);
417
+ if (!ptr)
418
+ return NULL;
419
+
420
+ bridge = fpga_bridge_create(dev, name, br_ops, priv);
421
+ if (!bridge) {
422
+ devres_free(ptr);
423
+ } else {
424
+ *ptr = bridge;
425
+ devres_add(dev, ptr);
426
+ }
427
+
428
+ return bridge;
429
+}
430
+EXPORT_SYMBOL_GPL(devm_fpga_bridge_create);
431
+
432
+/**
433
+ * fpga_bridge_register - register a FPGA bridge
434
+ *
435
+ * @bridge: FPGA bridge struct
394436 *
395437 * Return: 0 for success, error code otherwise.
396438 */
....@@ -412,8 +454,11 @@
412454 EXPORT_SYMBOL_GPL(fpga_bridge_register);
413455
414456 /**
415
- * fpga_bridge_unregister - unregister and free a fpga bridge
416
- * @bridge: FPGA bridge struct created by fpga_bridge_create
457
+ * fpga_bridge_unregister - unregister a FPGA bridge
458
+ *
459
+ * @bridge: FPGA bridge struct
460
+ *
461
+ * This function is intended for use in a FPGA bridge driver's remove function.
417462 */
418463 void fpga_bridge_unregister(struct fpga_bridge *bridge)
419464 {
....@@ -430,9 +475,6 @@
430475
431476 static void fpga_bridge_dev_release(struct device *dev)
432477 {
433
- struct fpga_bridge *bridge = to_fpga_bridge(dev);
434
-
435
- fpga_bridge_free(bridge);
436478 }
437479
438480 static int __init fpga_bridge_dev_init(void)