@@ -324,6 +324,9 @@ ATTRIBUTE_GROUPS(fpga_bridge);
324324 * @br_ops: pointer to structure of fpga bridge ops
325325 * @priv: FPGA bridge private data
326326 *
327+ * The caller of this function is responsible for freeing the bridge with
328+ * fpga_bridge_free(). Using devm_fpga_bridge_create() instead is recommended.
329+ *
327330 * Return: struct fpga_bridge or NULL
328331 */
329332struct fpga_bridge * fpga_bridge_create (struct device * dev , const char * name ,
@@ -378,8 +381,8 @@ struct fpga_bridge *fpga_bridge_create(struct device *dev, const char *name,
378381EXPORT_SYMBOL_GPL (fpga_bridge_create );
379382
380383/**
381- * fpga_bridge_free - free a fpga bridge and its id
382- * @bridge: FPGA bridge struct created by fpga_bridge_create
384+ * fpga_bridge_free - free a fpga bridge created by fpga_bridge_create()
385+ * @bridge: FPGA bridge struct
383386 */
384387void fpga_bridge_free (struct fpga_bridge * bridge )
385388{
@@ -388,9 +391,56 @@ void fpga_bridge_free(struct fpga_bridge *bridge)
388391}
389392EXPORT_SYMBOL_GPL (fpga_bridge_free );
390393
394+ static void devm_fpga_bridge_release (struct device * dev , void * res )
395+ {
396+ struct fpga_bridge * bridge = * (struct fpga_bridge * * )res ;
397+
398+ fpga_bridge_free (bridge );
399+ }
400+
391401/**
392- * fpga_bridge_register - register a fpga bridge
393- * @bridge: FPGA bridge struct created by fpga_bridge_create
402+ * devm_fpga_bridge_create - create and init a managed struct fpga_bridge
403+ * @dev: FPGA bridge device from pdev
404+ * @name: FPGA bridge name
405+ * @br_ops: pointer to structure of fpga bridge ops
406+ * @priv: FPGA bridge private data
407+ *
408+ * This function is intended for use in a FPGA bridge driver's probe function.
409+ * After the bridge driver creates the struct with devm_fpga_bridge_create(), it
410+ * should register the bridge with fpga_bridge_register(). The bridge driver's
411+ * remove function should call fpga_bridge_unregister(). The bridge struct
412+ * allocated with this function will be freed automatically on driver detach.
413+ * This includes the case of a probe function returning error before calling
414+ * fpga_bridge_register(), the struct will still get cleaned up.
415+ *
416+ * Return: struct fpga_bridge or NULL
417+ */
418+ struct fpga_bridge
419+ * devm_fpga_bridge_create (struct device * dev , const char * name ,
420+ const struct fpga_bridge_ops * br_ops , void * priv )
421+ {
422+ struct fpga_bridge * * ptr , * bridge ;
423+
424+ ptr = devres_alloc (devm_fpga_bridge_release , sizeof (* ptr ), GFP_KERNEL );
425+ if (!ptr )
426+ return NULL ;
427+
428+ bridge = fpga_bridge_create (dev , name , br_ops , priv );
429+ if (!bridge ) {
430+ devres_free (ptr );
431+ } else {
432+ * ptr = bridge ;
433+ devres_add (dev , ptr );
434+ }
435+
436+ return bridge ;
437+ }
438+ EXPORT_SYMBOL_GPL (devm_fpga_bridge_create );
439+
440+ /**
441+ * fpga_bridge_register - register a FPGA bridge
442+ *
443+ * @bridge: FPGA bridge struct
394444 *
395445 * Return: 0 for success, error code otherwise.
396446 */
@@ -412,8 +462,11 @@ int fpga_bridge_register(struct fpga_bridge *bridge)
412462EXPORT_SYMBOL_GPL (fpga_bridge_register );
413463
414464/**
415- * fpga_bridge_unregister - unregister and free a fpga bridge
416- * @bridge: FPGA bridge struct created by fpga_bridge_create
465+ * fpga_bridge_unregister - unregister a FPGA bridge
466+ *
467+ * @bridge: FPGA bridge struct
468+ *
469+ * This function is intended for use in a FPGA bridge driver's remove function.
417470 */
418471void fpga_bridge_unregister (struct fpga_bridge * bridge )
419472{
@@ -430,9 +483,6 @@ EXPORT_SYMBOL_GPL(fpga_bridge_unregister);
430483
431484static void fpga_bridge_dev_release (struct device * dev )
432485{
433- struct fpga_bridge * bridge = to_fpga_bridge (dev );
434-
435- fpga_bridge_free (bridge );
436486}
437487
438488static int __init fpga_bridge_dev_init (void )
0 commit comments