Skip to content

Commit 2a4d15a

Browse files
Ojaswin Mujoogregkh
authored andcommitted
staging: vchiq: Refactor vchiq cdev code
Move the vchiq cdev initialization code to its own function for better code organization. Call the initialization function during probe, thus shifting the whole cdev creation logic (which was earlier split in vchiq_probe() and vchiq_driver_init()) to vchiq_probe(). Reviewed-by: Stefan Wahren <stefan.wahren@i2se.com> Signed-off-by: Ojaswin Mujoo <ojaswin98@gmail.com> Link: https://lore.kernel.org/r/c0e538eb0644292a52267d39edd85ab2af9f9a4e.1626882325.git.ojaswin98@gmail.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 050cbd9 commit 2a4d15a

File tree

1 file changed

+92
-50
lines changed
  • drivers/staging/vc04_services/interface/vchiq_arm

1 file changed

+92
-50
lines changed

drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c

Lines changed: 92 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,6 +2134,81 @@ vchiq_fops = {
21342134
.read = vchiq_read
21352135
};
21362136

2137+
/**
2138+
* vchiq_register_chrdev - Register the char driver for vchiq
2139+
* and create the necessary class and
2140+
* device files in userspace.
2141+
* @parent The parent of the char device.
2142+
*
2143+
* Returns 0 on success else returns the error code.
2144+
*/
2145+
static int vchiq_register_chrdev(struct device *parent)
2146+
{
2147+
struct device *vchiq_dev;
2148+
int ret;
2149+
2150+
vchiq_class = class_create(THIS_MODULE, DEVICE_NAME);
2151+
if (IS_ERR(vchiq_class)) {
2152+
pr_err("Failed to create vchiq class\n");
2153+
ret = PTR_ERR(vchiq_class);
2154+
goto error_exit;
2155+
}
2156+
2157+
ret = alloc_chrdev_region(&vchiq_devid, 0, 1, DEVICE_NAME);
2158+
if (ret) {
2159+
pr_err("vchiq: Failed to allocate vchiq's chrdev region\n");
2160+
goto alloc_region_error;
2161+
}
2162+
2163+
cdev_init(&vchiq_cdev, &vchiq_fops);
2164+
vchiq_cdev.owner = THIS_MODULE;
2165+
ret = cdev_add(&vchiq_cdev, vchiq_devid, 1);
2166+
if (ret) {
2167+
vchiq_log_error(vchiq_arm_log_level,
2168+
"Unable to register vchiq char device");
2169+
goto cdev_add_error;
2170+
}
2171+
2172+
vchiq_dev = device_create(vchiq_class, parent, vchiq_devid, NULL,
2173+
DEVICE_NAME);
2174+
if (IS_ERR(vchiq_dev)) {
2175+
vchiq_log_error(vchiq_arm_log_level,
2176+
"Failed to create vchiq char device node");
2177+
ret = PTR_ERR(vchiq_dev);
2178+
goto device_create_error;
2179+
}
2180+
2181+
vchiq_log_info(vchiq_arm_log_level,
2182+
"vchiq char dev initialised successfully - device %d.%d",
2183+
MAJOR(vchiq_devid), MINOR(vchiq_devid));
2184+
2185+
return 0;
2186+
2187+
device_create_error:
2188+
cdev_del(&vchiq_cdev);
2189+
2190+
cdev_add_error:
2191+
unregister_chrdev_region(vchiq_devid, 1);
2192+
2193+
alloc_region_error:
2194+
class_destroy(vchiq_class);
2195+
2196+
error_exit:
2197+
return ret;
2198+
}
2199+
2200+
/**
2201+
* vchiq_deregister_chrdev - Deregister and cleanup the vchiq char
2202+
* driver and device files
2203+
*/
2204+
static void vchiq_deregister_chrdev(void)
2205+
{
2206+
device_destroy(vchiq_class, vchiq_devid);
2207+
cdev_del(&vchiq_cdev);
2208+
unregister_chrdev_region(vchiq_devid, 1);
2209+
class_destroy(vchiq_class);
2210+
}
2211+
21372212
/*
21382213
* Autosuspend related functionality
21392214
*/
@@ -2644,7 +2719,6 @@ static int vchiq_probe(struct platform_device *pdev)
26442719
struct device_node *fw_node;
26452720
const struct of_device_id *of_id;
26462721
struct vchiq_drvdata *drvdata;
2647-
struct device *vchiq_dev;
26482722
int err;
26492723

26502724
of_id = of_match_node(vchiq_of_match, pdev->dev.of_node);
@@ -2670,38 +2744,31 @@ static int vchiq_probe(struct platform_device *pdev)
26702744
if (err)
26712745
goto failed_platform_init;
26722746

2673-
cdev_init(&vchiq_cdev, &vchiq_fops);
2674-
vchiq_cdev.owner = THIS_MODULE;
2675-
err = cdev_add(&vchiq_cdev, vchiq_devid, 1);
2676-
if (err) {
2677-
vchiq_log_error(vchiq_arm_log_level,
2678-
"Unable to register device");
2679-
goto failed_platform_init;
2680-
}
2681-
2682-
vchiq_dev = device_create(vchiq_class, &pdev->dev, vchiq_devid, NULL,
2683-
"vchiq");
2684-
if (IS_ERR(vchiq_dev)) {
2685-
err = PTR_ERR(vchiq_dev);
2686-
goto failed_device_create;
2687-
}
2688-
26892747
vchiq_debugfs_init();
26902748

26912749
vchiq_log_info(vchiq_arm_log_level,
2692-
"vchiq: initialised - version %d (min %d), device %d.%d",
2693-
VCHIQ_VERSION, VCHIQ_VERSION_MIN,
2694-
MAJOR(vchiq_devid), MINOR(vchiq_devid));
2750+
"vchiq: platform initialised - version %d (min %d)",
2751+
VCHIQ_VERSION, VCHIQ_VERSION_MIN);
2752+
2753+
/*
2754+
* Simply exit on error since the function handles cleanup in
2755+
* cases of failure.
2756+
*/
2757+
err = vchiq_register_chrdev(&pdev->dev);
2758+
if (err) {
2759+
vchiq_log_warning(vchiq_arm_log_level,
2760+
"Failed to initialize vchiq cdev");
2761+
goto error_exit;
2762+
}
26952763

26962764
bcm2835_camera = vchiq_register_child(pdev, "bcm2835-camera");
26972765
bcm2835_audio = vchiq_register_child(pdev, "bcm2835_audio");
26982766

26992767
return 0;
27002768

2701-
failed_device_create:
2702-
cdev_del(&vchiq_cdev);
27032769
failed_platform_init:
2704-
vchiq_log_warning(vchiq_arm_log_level, "could not load vchiq");
2770+
vchiq_log_warning(vchiq_arm_log_level, "could not initialize vchiq platform");
2771+
error_exit:
27052772
return err;
27062773
}
27072774

@@ -2710,8 +2777,7 @@ static int vchiq_remove(struct platform_device *pdev)
27102777
platform_device_unregister(bcm2835_audio);
27112778
platform_device_unregister(bcm2835_camera);
27122779
vchiq_debugfs_deinit();
2713-
device_destroy(vchiq_class, vchiq_devid);
2714-
cdev_del(&vchiq_cdev);
2780+
vchiq_deregister_chrdev();
27152781

27162782
return 0;
27172783
}
@@ -2729,31 +2795,9 @@ static int __init vchiq_driver_init(void)
27292795
{
27302796
int ret;
27312797

2732-
vchiq_class = class_create(THIS_MODULE, DEVICE_NAME);
2733-
if (IS_ERR(vchiq_class)) {
2734-
pr_err("Failed to create vchiq class\n");
2735-
return PTR_ERR(vchiq_class);
2736-
}
2737-
2738-
ret = alloc_chrdev_region(&vchiq_devid, 0, 1, DEVICE_NAME);
2739-
if (ret) {
2740-
pr_err("Failed to allocate vchiq's chrdev region\n");
2741-
goto class_destroy;
2742-
}
2743-
27442798
ret = platform_driver_register(&vchiq_driver);
2745-
if (ret) {
2799+
if (ret)
27462800
pr_err("Failed to register vchiq driver\n");
2747-
goto region_unregister;
2748-
}
2749-
2750-
return 0;
2751-
2752-
region_unregister:
2753-
unregister_chrdev_region(vchiq_devid, 1);
2754-
2755-
class_destroy:
2756-
class_destroy(vchiq_class);
27572801

27582802
return ret;
27592803
}
@@ -2762,8 +2806,6 @@ module_init(vchiq_driver_init);
27622806
static void __exit vchiq_driver_exit(void)
27632807
{
27642808
platform_driver_unregister(&vchiq_driver);
2765-
unregister_chrdev_region(vchiq_devid, 1);
2766-
class_destroy(vchiq_class);
27672809
}
27682810
module_exit(vchiq_driver_exit);
27692811

0 commit comments

Comments
 (0)