Commit fea82b7f authored by Alan Tull's avatar Alan Tull Committed by Greg Kroah-Hartman

fpga: add devm_fpga_region_create

Add devm_fpga_region_create() which is the
managed version of fpga_region_create().

Change current region drivers to use
devm_fpga_region_create().
Signed-off-by: default avatarAlan Tull <atull@kernel.org>
Suggested-by: default avatarFederico Vaga <federico.vaga@cern.ch>
Acked-by: default avatarMoritz Fischer <mdf@kernel.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 213befe0
...@@ -89,6 +89,9 @@ API to add a new FPGA region ...@@ -89,6 +89,9 @@ API to add a new FPGA region
.. kernel-doc:: include/linux/fpga/fpga-region.h .. kernel-doc:: include/linux/fpga/fpga-region.h
:functions: fpga_region :functions: fpga_region
.. kernel-doc:: drivers/fpga/fpga-region.c
:functions: devm_fpga_region_create
.. kernel-doc:: drivers/fpga/fpga-region.c .. kernel-doc:: drivers/fpga/fpga-region.c
:functions: fpga_region_create :functions: fpga_region_create
......
...@@ -39,7 +39,7 @@ static int fme_region_probe(struct platform_device *pdev) ...@@ -39,7 +39,7 @@ static int fme_region_probe(struct platform_device *pdev)
if (IS_ERR(mgr)) if (IS_ERR(mgr))
return -EPROBE_DEFER; return -EPROBE_DEFER;
region = fpga_region_create(dev, mgr, fme_region_get_bridges); region = devm_fpga_region_create(dev, mgr, fme_region_get_bridges);
if (!region) { if (!region) {
ret = -ENOMEM; ret = -ENOMEM;
goto eprobe_mgr_put; goto eprobe_mgr_put;
...@@ -51,14 +51,12 @@ static int fme_region_probe(struct platform_device *pdev) ...@@ -51,14 +51,12 @@ static int fme_region_probe(struct platform_device *pdev)
ret = fpga_region_register(region); ret = fpga_region_register(region);
if (ret) if (ret)
goto region_free; goto eprobe_mgr_put;
dev_dbg(dev, "DFL FME FPGA Region probed\n"); dev_dbg(dev, "DFL FME FPGA Region probed\n");
return 0; return 0;
region_free:
fpga_region_free(region);
eprobe_mgr_put: eprobe_mgr_put:
fpga_mgr_put(mgr); fpga_mgr_put(mgr);
return ret; return ret;
......
...@@ -899,7 +899,7 @@ dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info) ...@@ -899,7 +899,7 @@ dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info)
if (!cdev) if (!cdev)
return ERR_PTR(-ENOMEM); return ERR_PTR(-ENOMEM);
cdev->region = fpga_region_create(info->dev, NULL, NULL); cdev->region = devm_fpga_region_create(info->dev, NULL, NULL);
if (!cdev->region) { if (!cdev->region) {
ret = -ENOMEM; ret = -ENOMEM;
goto free_cdev_exit; goto free_cdev_exit;
...@@ -911,7 +911,7 @@ dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info) ...@@ -911,7 +911,7 @@ dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info)
ret = fpga_region_register(cdev->region); ret = fpga_region_register(cdev->region);
if (ret) if (ret)
goto free_region_exit; goto free_cdev_exit;
/* create and init build info for enumeration */ /* create and init build info for enumeration */
binfo = devm_kzalloc(info->dev, sizeof(*binfo), GFP_KERNEL); binfo = devm_kzalloc(info->dev, sizeof(*binfo), GFP_KERNEL);
...@@ -942,8 +942,6 @@ dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info) ...@@ -942,8 +942,6 @@ dfl_fpga_feature_devs_enumerate(struct dfl_fpga_enum_info *info)
unregister_region_exit: unregister_region_exit:
fpga_region_unregister(cdev->region); fpga_region_unregister(cdev->region);
free_region_exit:
fpga_region_free(cdev->region);
free_cdev_exit: free_cdev_exit:
devm_kfree(info->dev, cdev); devm_kfree(info->dev, cdev);
return ERR_PTR(ret); return ERR_PTR(ret);
......
...@@ -185,6 +185,10 @@ ATTRIBUTE_GROUPS(fpga_region); ...@@ -185,6 +185,10 @@ ATTRIBUTE_GROUPS(fpga_region);
* @mgr: manager that programs this region * @mgr: manager that programs this region
* @get_bridges: optional function to get bridges to a list * @get_bridges: optional function to get bridges to a list
* *
* The caller of this function is responsible for freeing the resulting region
* struct with fpga_region_free(). Using devm_fpga_region_create() instead is
* recommended.
*
* Return: struct fpga_region or NULL * Return: struct fpga_region or NULL
*/ */
struct fpga_region struct fpga_region
...@@ -230,8 +234,8 @@ struct fpga_region ...@@ -230,8 +234,8 @@ struct fpga_region
EXPORT_SYMBOL_GPL(fpga_region_create); EXPORT_SYMBOL_GPL(fpga_region_create);
/** /**
* fpga_region_free - free a struct fpga_region * fpga_region_free - free a FPGA region created by fpga_region_create()
* @region: FPGA region created by fpga_region_create * @region: FPGA region
*/ */
void fpga_region_free(struct fpga_region *region) void fpga_region_free(struct fpga_region *region)
{ {
...@@ -240,21 +244,69 @@ void fpga_region_free(struct fpga_region *region) ...@@ -240,21 +244,69 @@ void fpga_region_free(struct fpga_region *region)
} }
EXPORT_SYMBOL_GPL(fpga_region_free); EXPORT_SYMBOL_GPL(fpga_region_free);
static void devm_fpga_region_release(struct device *dev, void *res)
{
struct fpga_region *region = *(struct fpga_region **)res;
fpga_region_free(region);
}
/**
* devm_fpga_region_create - create and initialize a managed FPGA region struct
* @dev: device parent
* @mgr: manager that programs this region
* @get_bridges: optional function to get bridges to a list
*
* This function is intended for use in a FPGA region driver's probe function.
* After the region driver creates the region struct with
* devm_fpga_region_create(), it should register it with fpga_region_register().
* The region driver's remove function should call fpga_region_unregister().
* The region struct allocated with this function will be freed automatically on
* driver detach. This includes the case of a probe function returning error
* before calling fpga_region_register(), the struct will still get cleaned up.
*
* Return: struct fpga_region or NULL
*/
struct fpga_region
*devm_fpga_region_create(struct device *dev,
struct fpga_manager *mgr,
int (*get_bridges)(struct fpga_region *))
{
struct fpga_region **ptr, *region;
ptr = devres_alloc(devm_fpga_region_release, sizeof(*ptr), GFP_KERNEL);
if (!ptr)
return NULL;
region = fpga_region_create(dev, mgr, get_bridges);
if (!region) {
devres_free(ptr);
} else {
*ptr = region;
devres_add(dev, ptr);
}
return region;
}
EXPORT_SYMBOL_GPL(devm_fpga_region_create);
/** /**
* fpga_region_register - register a FPGA region * fpga_region_register - register a FPGA region
* @region: FPGA region created by fpga_region_create * @region: FPGA region
*
* Return: 0 or -errno * Return: 0 or -errno
*/ */
int fpga_region_register(struct fpga_region *region) int fpga_region_register(struct fpga_region *region)
{ {
return device_add(&region->dev); return device_add(&region->dev);
} }
EXPORT_SYMBOL_GPL(fpga_region_register); EXPORT_SYMBOL_GPL(fpga_region_register);
/** /**
* fpga_region_unregister - unregister and free a FPGA region * fpga_region_unregister - unregister a FPGA region
* @region: FPGA region * @region: FPGA region
*
* This function is intended for use in a FPGA region driver's remove function.
*/ */
void fpga_region_unregister(struct fpga_region *region) void fpga_region_unregister(struct fpga_region *region)
{ {
...@@ -264,9 +316,6 @@ EXPORT_SYMBOL_GPL(fpga_region_unregister); ...@@ -264,9 +316,6 @@ EXPORT_SYMBOL_GPL(fpga_region_unregister);
static void fpga_region_dev_release(struct device *dev) static void fpga_region_dev_release(struct device *dev)
{ {
struct fpga_region *region = to_fpga_region(dev);
fpga_region_free(region);
} }
/** /**
......
...@@ -410,7 +410,7 @@ static int of_fpga_region_probe(struct platform_device *pdev) ...@@ -410,7 +410,7 @@ static int of_fpga_region_probe(struct platform_device *pdev)
if (IS_ERR(mgr)) if (IS_ERR(mgr))
return -EPROBE_DEFER; return -EPROBE_DEFER;
region = fpga_region_create(dev, mgr, of_fpga_region_get_bridges); region = devm_fpga_region_create(dev, mgr, of_fpga_region_get_bridges);
if (!region) { if (!region) {
ret = -ENOMEM; ret = -ENOMEM;
goto eprobe_mgr_put; goto eprobe_mgr_put;
...@@ -418,7 +418,7 @@ static int of_fpga_region_probe(struct platform_device *pdev) ...@@ -418,7 +418,7 @@ static int of_fpga_region_probe(struct platform_device *pdev)
ret = fpga_region_register(region); ret = fpga_region_register(region);
if (ret) if (ret)
goto eprobe_free; goto eprobe_mgr_put;
of_platform_populate(np, fpga_region_of_match, NULL, &region->dev); of_platform_populate(np, fpga_region_of_match, NULL, &region->dev);
dev_set_drvdata(dev, region); dev_set_drvdata(dev, region);
...@@ -427,8 +427,6 @@ static int of_fpga_region_probe(struct platform_device *pdev) ...@@ -427,8 +427,6 @@ static int of_fpga_region_probe(struct platform_device *pdev)
return 0; return 0;
eprobe_free:
fpga_region_free(region);
eprobe_mgr_put: eprobe_mgr_put:
fpga_mgr_put(mgr); fpga_mgr_put(mgr);
return ret; return ret;
......
...@@ -44,4 +44,8 @@ void fpga_region_free(struct fpga_region *region); ...@@ -44,4 +44,8 @@ void fpga_region_free(struct fpga_region *region);
int fpga_region_register(struct fpga_region *region); int fpga_region_register(struct fpga_region *region);
void fpga_region_unregister(struct fpga_region *region); void fpga_region_unregister(struct fpga_region *region);
struct fpga_region
*devm_fpga_region_create(struct device *dev, struct fpga_manager *mgr,
int (*get_bridges)(struct fpga_region *));
#endif /* _FPGA_REGION_H */ #endif /* _FPGA_REGION_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment