2014-02-23 20:22:26

by Alexey Khoroshilov

[permalink] [raw]
Subject: [PATCH 1/3] staging: dgap: remove unneeded status variables

dgap_driver_start and dgap_Major_Control_Registered are used
to keep status of initialization of the driver as a whole and its "Major Control".
But the code that checks them is executed once on module init/unload.
That makes no sense in these variables as far as their values are predictable
at any time.

Signed-off-by: Alexey Khoroshilov <[email protected]>
---
drivers/staging/dgap/dgap_driver.c | 105 ++++++++++++++++---------------------
1 file changed, 46 insertions(+), 59 deletions(-)

diff --git a/drivers/staging/dgap/dgap_driver.c b/drivers/staging/dgap/dgap_driver.c
index 089d017fc291..d7f1e999aaa4 100644
--- a/drivers/staging/dgap/dgap_driver.c
+++ b/drivers/staging/dgap/dgap_driver.c
@@ -117,9 +117,6 @@ int dgap_poll_tick = 20; /* Poll interval - 20 ms */
/*
* Static vars.
*/
-static int dgap_Major_Control_Registered = FALSE;
-static uint dgap_driver_start = FALSE;
-
static struct class * dgap_class;

/*
@@ -283,65 +280,57 @@ static int dgap_start(void)
int rc = 0;
unsigned long flags;

- if (dgap_driver_start == FALSE) {
-
- dgap_driver_start = TRUE;
+ /* make sure that the globals are init'd before we do anything else */
+ dgap_init_globals();

- /* make sure that the globals are init'd before we do anything else */
- dgap_init_globals();
+ dgap_NumBoards = 0;

- dgap_NumBoards = 0;
+ APR(("For the tools package or updated drivers please visit http://www.digi.com\n"));

- APR(("For the tools package or updated drivers please visit http://www.digi.com\n"));
-
- /*
- * Register our base character device into the kernel.
- * This allows the download daemon to connect to the downld device
- * before any of the boards are init'ed.
- */
- if (!dgap_Major_Control_Registered) {
- /*
- * Register management/dpa devices
- */
- rc = register_chrdev(DIGI_DGAP_MAJOR, "dgap", &DgapBoardFops);
- if (rc < 0) {
- APR(("Can't register dgap driver device (%d)\n", rc));
- return (rc);
- }
+ /*
+ * Register our base character device into the kernel.
+ * This allows the download daemon to connect to the downld device
+ * before any of the boards are init'ed.
+ */

- dgap_class = class_create(THIS_MODULE, "dgap_mgmt");
- device_create(dgap_class, NULL,
- MKDEV(DIGI_DGAP_MAJOR, 0),
- NULL, "dgap_mgmt");
- device_create(dgap_class, NULL,
- MKDEV(DIGI_DGAP_MAJOR, 1),
- NULL, "dgap_downld");
- dgap_Major_Control_Registered = TRUE;
- }
+ /*
+ * Register management/dpa devices
+ */
+ rc = register_chrdev(DIGI_DGAP_MAJOR, "dgap", &DgapBoardFops);
+ if (rc < 0) {
+ APR(("Can't register dgap driver device (%d)\n", rc));
+ return (rc);
+ }

- /*
- * Init any global tty stuff.
- */
- rc = dgap_tty_preinit();
+ dgap_class = class_create(THIS_MODULE, "dgap_mgmt");
+ device_create(dgap_class, NULL,
+ MKDEV(DIGI_DGAP_MAJOR, 0),
+ NULL, "dgap_mgmt");
+ device_create(dgap_class, NULL,
+ MKDEV(DIGI_DGAP_MAJOR, 1),
+ NULL, "dgap_downld");

- if (rc < 0) {
- APR(("tty preinit - not enough memory (%d)\n", rc));
- return(rc);
- }
+ /*
+ * Init any global tty stuff.
+ */
+ rc = dgap_tty_preinit();
+ if (rc < 0) {
+ APR(("tty preinit - not enough memory (%d)\n", rc));
+ return(rc);
+ }

- /* Start the poller */
- DGAP_LOCK(dgap_poll_lock, flags);
- init_timer(&dgap_poll_timer);
- dgap_poll_timer.function = dgap_poll_handler;
- dgap_poll_timer.data = 0;
- dgap_poll_time = jiffies + dgap_jiffies_from_ms(dgap_poll_tick);
- dgap_poll_timer.expires = dgap_poll_time;
- DGAP_UNLOCK(dgap_poll_lock, flags);
+ /* Start the poller */
+ DGAP_LOCK(dgap_poll_lock, flags);
+ init_timer(&dgap_poll_timer);
+ dgap_poll_timer.function = dgap_poll_handler;
+ dgap_poll_timer.data = 0;
+ dgap_poll_time = jiffies + dgap_jiffies_from_ms(dgap_poll_tick);
+ dgap_poll_timer.expires = dgap_poll_time;
+ DGAP_UNLOCK(dgap_poll_lock, flags);

- add_timer(&dgap_poll_timer);
+ add_timer(&dgap_poll_timer);

- dgap_driver_state = DRIVER_NEED_CONFIG_LOAD;
- }
+ dgap_driver_state = DRIVER_NEED_CONFIG_LOAD;

return (rc);
}
@@ -409,12 +398,10 @@ void dgap_cleanup_module(void)
dgap_remove_driver_sysfiles(&dgap_driver);


- if (dgap_Major_Control_Registered) {
- device_destroy(dgap_class, MKDEV(DIGI_DGAP_MAJOR, 0));
- device_destroy(dgap_class, MKDEV(DIGI_DGAP_MAJOR, 1));
- class_destroy(dgap_class);
- unregister_chrdev(DIGI_DGAP_MAJOR, "dgap");
- }
+ device_destroy(dgap_class, MKDEV(DIGI_DGAP_MAJOR, 0));
+ device_destroy(dgap_class, MKDEV(DIGI_DGAP_MAJOR, 1));
+ class_destroy(dgap_class);
+ unregister_chrdev(DIGI_DGAP_MAJOR, "dgap");

kfree(dgap_config_buf);

--
1.8.3.2


2014-02-23 20:22:25

by Alexey Khoroshilov

[permalink] [raw]
Subject: [PATCH 2/3] staging: dgap: implement proper error handling in dgap_start()

dgap_start() ignored errors in class_create() and device_create().
The patch implements proper error handling.

Found by Linux Driver Verification project (linuxtesting.org).

Signed-off-by: Alexey Khoroshilov <[email protected]>
---
drivers/staging/dgap/dgap_driver.c | 42 +++++++++++++++++++++++++++++++-------
1 file changed, 35 insertions(+), 7 deletions(-)

diff --git a/drivers/staging/dgap/dgap_driver.c b/drivers/staging/dgap/dgap_driver.c
index d7f1e999aaa4..136c879b560c 100644
--- a/drivers/staging/dgap/dgap_driver.c
+++ b/drivers/staging/dgap/dgap_driver.c
@@ -279,6 +279,7 @@ static int dgap_start(void)
{
int rc = 0;
unsigned long flags;
+ struct device *device;

/* make sure that the globals are init'd before we do anything else */
dgap_init_globals();
@@ -303,12 +304,29 @@ static int dgap_start(void)
}

dgap_class = class_create(THIS_MODULE, "dgap_mgmt");
- device_create(dgap_class, NULL,
- MKDEV(DIGI_DGAP_MAJOR, 0),
- NULL, "dgap_mgmt");
- device_create(dgap_class, NULL,
- MKDEV(DIGI_DGAP_MAJOR, 1),
- NULL, "dgap_downld");
+ if (IS_ERR(dgap_class)) {
+ rc = PTR_ERR(dgap_class);
+ APR(("Can't create dgap_mgmt class (%d)\n", rc));
+ goto failed_class;
+ }
+
+ device = device_create(dgap_class, NULL,
+ MKDEV(DIGI_DGAP_MAJOR, 0),
+ NULL, "dgap_mgmt");
+ if (IS_ERR(device)) {
+ rc = PTR_ERR(device);
+ APR(("Can't create device (%d)\n", rc));
+ goto failed_device0;
+ }
+
+ device = device_create(dgap_class, NULL,
+ MKDEV(DIGI_DGAP_MAJOR, 1),
+ NULL, "dgap_downld");
+ if (IS_ERR(device)) {
+ rc = PTR_ERR(device);
+ APR(("Can't create device (%d)\n", rc));
+ goto failed_device1;
+ }

/*
* Init any global tty stuff.
@@ -316,7 +334,7 @@ static int dgap_start(void)
rc = dgap_tty_preinit();
if (rc < 0) {
APR(("tty preinit - not enough memory (%d)\n", rc));
- return(rc);
+ goto failed_tty;
}

/* Start the poller */
@@ -333,6 +351,16 @@ static int dgap_start(void)
dgap_driver_state = DRIVER_NEED_CONFIG_LOAD;

return (rc);
+
+failed_tty:
+ device_destroy(dgap_class, MKDEV(DIGI_DGAP_MAJOR, 1));
+failed_device1:
+ device_destroy(dgap_class, MKDEV(DIGI_DGAP_MAJOR, 0));
+failed_device0:
+ class_destroy(dgap_class);
+failed_class:
+ unregister_chrdev(DIGI_DGAP_MAJOR, "dgap");
+ return (rc);
}


--
1.8.3.2

2014-02-23 20:22:56

by Alexey Khoroshilov

[permalink] [raw]
Subject: [PATCH 3/3] staging: dgap: fix error handling in dgap_init_module()

No need to call pci_unregister_driver() if pci_register_driver() failed.

Signed-off-by: Alexey Khoroshilov <[email protected]>
---
drivers/staging/dgap/dgap_driver.c | 12 ++----------
1 file changed, 2 insertions(+), 10 deletions(-)

diff --git a/drivers/staging/dgap/dgap_driver.c b/drivers/staging/dgap/dgap_driver.c
index 136c879b560c..cc6933db6feb 100644
--- a/drivers/staging/dgap/dgap_driver.c
+++ b/drivers/staging/dgap/dgap_driver.c
@@ -254,18 +254,10 @@ int dgap_init_module(void)
/*
* If something went wrong in the scan, bail out of driver.
*/
- if (rc < 0) {
- /* Only unregister the pci driver if it was actually registered. */
- if (dgap_NumBoards)
- pci_unregister_driver(&dgap_driver);
- else
- printk("WARNING: dgap driver load failed. No DGAP boards found.\n");
-
+ if (rc < 0)
dgap_cleanup_module();
- }
- else {
+ else
dgap_create_driver_sysfiles(&dgap_driver);
- }

DPR_INIT(("Finished init_module. Returning %d\n", rc));
return (rc);
--
1.8.3.2

2014-02-25 00:50:22

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [PATCH 1/3] staging: dgap: remove unneeded status variables

On Mon, Feb 24, 2014 at 12:21:51AM +0400, Alexey Khoroshilov wrote:
> dgap_driver_start and dgap_Major_Control_Registered are used
> to keep status of initialization of the driver as a whole and its "Major Control".
> But the code that checks them is executed once on module init/unload.
> That makes no sense in these variables as far as their values are predictable
> at any time.
>
> Signed-off-by: Alexey Khoroshilov <[email protected]>
> ---
> drivers/staging/dgap/dgap_driver.c | 105 ++++++++++++++++---------------------
> 1 file changed, 46 insertions(+), 59 deletions(-)

This file is no longer in my tree, as the driver has been merged to one
file. Can you please redo it against my latest staging-next branch of
my staging.git tree and resend the series so that I can apply them?

thanks,

greg k-h