[PATCH] staging: dgnc: Encapsulate global variables in a structure

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This commit binds global variables of dgnc driver in a structure so
that it is logically consistent. The structure is accessed via getter
function and as a result the externing of globals is removed. The names
of the variables are also changed to be more eye friendly.

Signed-off-by: Konrad Zapalowicz <bergo.torino@xxxxxxxxx>
---

This patch applies on top of my two previous patch series. In case it
is an issue I can resubmit the whole series for dgnc driver as one
patch set - just let me know.

 drivers/staging/dgnc/dgnc_driver.c | 82 +++++++++++++++++++-------------------
 drivers/staging/dgnc/dgnc_driver.h | 20 ++++++----
 drivers/staging/dgnc/dgnc_mgmt.c   | 48 ++++++++++++----------
 drivers/staging/dgnc/dgnc_sysfs.c  | 31 ++++++++++----
 4 files changed, 102 insertions(+), 79 deletions(-)

diff --git a/drivers/staging/dgnc/dgnc_driver.c b/drivers/staging/dgnc/dgnc_driver.c
index 724e4ab..9a23e9a 100644
--- a/drivers/staging/dgnc/dgnc_driver.c
+++ b/drivers/staging/dgnc/dgnc_driver.c
@@ -83,17 +83,11 @@ static const struct file_operations dgnc_BoardFops = {
 	.release	=	dgnc_mgmt_close
 };
 
-
-/*
- * Globals
- */
-uint			dgnc_NumBoards;
-struct dgnc_board		*dgnc_Board[MAXBOARDS];
-DEFINE_SPINLOCK(dgnc_global_lock);
-int			dgnc_driver_state = DRIVER_INITIALIZED;
-ulong			dgnc_poll_counter;
-uint			dgnc_Major;
-int			dgnc_poll_tick = 20;	/* Poll interval - 20 ms */
+static struct dgnc_driver driver = {
+	.lock = __SPIN_LOCK_UNLOCKED(driver.lock),
+	.state = DRIVER_INITIALIZED,
+	.poll_tick = 20,
+};
 
 /*
  * Static vars.
@@ -207,20 +201,20 @@ static void __exit dgnc_cleanup_module(void)
 	dgnc_remove_driver_sysfiles(&dgnc_driver);
 
 	if (dgnc_Major_Control_Registered) {
-		device_destroy(dgnc_class, MKDEV(dgnc_Major, 0));
+		device_destroy(dgnc_class, MKDEV(driver.major, 0));
 		class_destroy(dgnc_class);
-		unregister_chrdev(dgnc_Major, "dgnc");
+		unregister_chrdev(driver.major, "dgnc");
 	}
 
-	for (i = 0; i < dgnc_NumBoards; ++i) {
-		dgnc_remove_ports_sysfiles(dgnc_Board[i]);
-		dgnc_tty_uninit(dgnc_Board[i]);
-		dgnc_cleanup_board(dgnc_Board[i]);
+	for (i = 0; i < driver.num_boards; ++i) {
+		dgnc_remove_ports_sysfiles(driver.board[i]);
+		dgnc_tty_uninit(driver.board[i]);
+		dgnc_cleanup_board(driver.board[i]);
 	}
 
 	dgnc_tty_post_uninit();
 
-	if (dgnc_NumBoards)
+	if (driver.num_boards)
 		pci_unregister_driver(&dgnc_driver);
 }
 
@@ -253,7 +247,7 @@ static int __init dgnc_init_module(void)
 	 */
 	if (rc < 0) {
 		/* Only unregister the pci driver if it was actually registered. */
-		if (dgnc_NumBoards)
+		if (driver.num_boards)
 			pci_unregister_driver(&dgnc_driver);
 		else
 			pr_warn("WARNING: dgnc driver load failed.  No Digi Neo or Classic boards found.\n");
@@ -301,11 +295,11 @@ static int dgnc_start(void)
 			APR(("Can't register dgnc driver device (%d)\n", rc));
 			return -ENXIO;
 		}
-		dgnc_Major = rc;
+		driver.major = rc;
 
 		dgnc_class = class_create(THIS_MODULE, "dgnc_mgmt");
 		device_create(dgnc_class, NULL,
-			MKDEV(dgnc_Major, 0),
+			MKDEV(driver.major, 0),
 			NULL, "dgnc_mgmt");
 		dgnc_Major_Control_Registered = TRUE;
 	}
@@ -325,13 +319,13 @@ static int dgnc_start(void)
 	init_timer(&dgnc_poll_timer);
 	dgnc_poll_timer.function = dgnc_poll_handler;
 	dgnc_poll_timer.data = 0;
-	dgnc_poll_time = jiffies + dgnc_jiffies_from_ms(dgnc_poll_tick);
+	dgnc_poll_time = jiffies + dgnc_jiffies_from_ms(driver.poll_tick);
 	dgnc_poll_timer.expires = dgnc_poll_time;
 	DGNC_UNLOCK(dgnc_poll_lock, flags);
 
 	add_timer(&dgnc_poll_timer);
 
-	dgnc_driver_state = DRIVER_READY;
+	driver.state = DRIVER_READY;
 
 	return rc;
 }
@@ -349,8 +343,9 @@ static int dgnc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
 	} else {
 		rc = dgnc_found_board(pdev, ent->driver_data);
 		if (rc == 0) {
-			dgnc_NumBoards++;
-			DPR_INIT(("Incrementing numboards to %d\n", dgnc_NumBoards));
+			driver.num_boards++;
+			DPR_INIT(("Incrementing numboards to %d\n",
+				driver.num_boards));
 		}
 	}
 	return rc;
@@ -395,12 +390,12 @@ static void dgnc_cleanup_board(struct dgnc_board *brd)
 	if (brd->msgbuf_head) {
 		unsigned long flags;
 
-		DGNC_LOCK(dgnc_global_lock, flags);
+		DGNC_LOCK(driver.lock, flags);
 		brd->msgbuf = NULL;
 		printk("%s", brd->msgbuf_head);
 		kfree(brd->msgbuf_head);
 		brd->msgbuf_head = NULL;
-		DGNC_UNLOCK(dgnc_global_lock, flags);
+		DGNC_UNLOCK(driver.lock, flags);
 	}
 
 	/* Free all allocated channels structs */
@@ -416,7 +411,7 @@ static void dgnc_cleanup_board(struct dgnc_board *brd)
 
 	kfree(brd->flipbuf);
 
-	dgnc_Board[brd->boardnum] = NULL;
+	driver.board[brd->boardnum] = NULL;
 
 	kfree(brd);
 }
@@ -436,7 +431,7 @@ static int dgnc_found_board(struct pci_dev *pdev, int id)
 	unsigned long flags;
 
 	/* get the board structure and prep it */
-	brd = dgnc_Board[dgnc_NumBoards] =
+	brd = driver.board[driver.num_boards] =
 		kzalloc(sizeof(*brd), GFP_KERNEL);
 	if (!brd)
 		return -ENOMEM;
@@ -451,7 +446,7 @@ static int dgnc_found_board(struct pci_dev *pdev, int id)
 
 	/* store the info for the board we've found */
 	brd->magic = DGNC_BOARD_MAGIC;
-	brd->boardnum = dgnc_NumBoards;
+	brd->boardnum = driver.num_boards;
 	brd->vendor = dgnc_pci_tbl[id].vendor;
 	brd->device = dgnc_pci_tbl[id].device;
 	brd->pdev = pdev;
@@ -642,12 +637,12 @@ static int dgnc_found_board(struct pci_dev *pdev, int id)
 	tasklet_init(&brd->helper_tasklet, brd->bd_ops->tasklet, (unsigned long) brd);
 
 	DPR_INIT(("dgnc_scan(%d) - printing out the msgbuf\n", i));
-	DGNC_LOCK(dgnc_global_lock, flags);
+	DGNC_LOCK(driver.lock, flags);
 	brd->msgbuf = NULL;
 	printk("%s", brd->msgbuf_head);
 	kfree(brd->msgbuf_head);
 	brd->msgbuf_head = NULL;
-	DGNC_UNLOCK(dgnc_global_lock, flags);
+	DGNC_UNLOCK(driver.lock, flags);
 
 	/*
 	 * allocate flip buffer for board.
@@ -745,19 +740,19 @@ static void dgnc_poll_handler(ulong dummy)
 	int i;
 	unsigned long new_time;
 
-	dgnc_poll_counter++;
+	driver.poll_counter++;
 
 	/*
 	 * Do not start the board state machine until
 	 * driver tells us its up and running, and has
 	 * everything it needs.
 	 */
-	if (dgnc_driver_state != DRIVER_READY)
+	if (driver.state != DRIVER_READY)
 		goto schedule_poller;
 
 	/* Go thru each board, kicking off a tasklet for each if needed */
-	for (i = 0; i < dgnc_NumBoards; i++) {
-		brd = dgnc_Board[i];
+	for (i = 0; i < driver.num_boards; i++) {
+		brd = driver.board[i];
 
 		DGNC_LOCK(brd->bd_lock, lock_flags);
 
@@ -779,12 +774,13 @@ schedule_poller:
 	 * Schedule ourself back at the nominal wakeup interval.
 	 */
 	DGNC_LOCK(dgnc_poll_lock, lock_flags);
-	dgnc_poll_time += dgnc_jiffies_from_ms(dgnc_poll_tick);
+	dgnc_poll_time += dgnc_jiffies_from_ms(driver.poll_tick);
 
 	new_time = dgnc_poll_time - jiffies;
 
-	if ((ulong) new_time >= 2 * dgnc_poll_tick)
-		dgnc_poll_time = jiffies +  dgnc_jiffies_from_ms(dgnc_poll_tick);
+	if ((ulong) new_time >= 2 * driver.poll_tick)
+		dgnc_poll_time =
+			jiffies + dgnc_jiffies_from_ms(driver.poll_tick);
 
 	init_timer(&dgnc_poll_timer);
 	dgnc_poll_timer.function = dgnc_poll_handler;
@@ -810,11 +806,15 @@ static void dgnc_init_globals(void)
 	dgnc_rawreadok		= rawreadok;
 	dgnc_trcbuf_size	= trcbuf_size;
 	dgnc_debug		= debug;
-	dgnc_NumBoards		= 0;
+	driver.num_boards	= 0;
 
 	for (i = 0; i < MAXBOARDS; i++)
-		dgnc_Board[i] = NULL;
+		driver.board[i] = NULL;
 
 	init_timer(&dgnc_poll_timer);
 }
 
+struct dgnc_driver *get_driver(void)
+{
+	return &driver;
+}
diff --git a/drivers/staging/dgnc/dgnc_driver.h b/drivers/staging/dgnc/dgnc_driver.h
index eae0494..712986a 100644
--- a/drivers/staging/dgnc/dgnc_driver.h
+++ b/drivers/staging/dgnc/dgnc_driver.h
@@ -481,20 +481,24 @@ struct channel_t {
 	wait_queue_head_t ch_sniff_wait;
 };
 
+struct dgnc_driver {
+	uint8_t num_boards;
+	struct dgnc_board *board[MAXBOARDS];
+	spinlock_t lock;
+	int8_t state;
+	unsigned long poll_counter;
+	unsigned int major;
+	int poll_tick;
+};
+
+struct dgnc_driver *get_driver(void);
+
 /*
  * Our Global Variables.
  */
-extern int		dgnc_driver_state;	/* The state of the driver	*/
-extern uint		dgnc_Major;		/* Our driver/mgmt major	*/
 extern int		dgnc_debug;		/* Debug variable		*/
 extern int		dgnc_rawreadok;		/* Set if user wants rawreads	*/
-extern int		dgnc_poll_tick;		/* Poll interval - 20 ms	*/
 extern int		dgnc_trcbuf_size;	/* Size of the ringbuffer	*/
-extern spinlock_t	dgnc_global_lock;	/* Driver global spinlock	*/
-extern uint		dgnc_NumBoards;		/* Total number of boards	*/
-extern struct dgnc_board	*dgnc_Board[MAXBOARDS];	/* Array of board structs	*/
-extern ulong		dgnc_poll_counter;	/* Times the poller has run	*/
 extern char		*dgnc_state_text[];	/* Array of state text		*/
 extern char		*dgnc_driver_state_text[];/* Array of driver state text */
-
 #endif
diff --git a/drivers/staging/dgnc/dgnc_mgmt.c b/drivers/staging/dgnc/dgnc_mgmt.c
index c5b425b..c8e62b1 100644
--- a/drivers/staging/dgnc/dgnc_mgmt.c
+++ b/drivers/staging/dgnc/dgnc_mgmt.c
@@ -64,25 +64,26 @@ int dgnc_mgmt_open(struct inode *inode, struct file *file)
 {
 	unsigned long lock_flags;
 	unsigned int minor = iminor(inode);
+	struct dgnc_driver *driver = get_driver();
 
 	DPR_MGMT(("dgnc_mgmt_open start.\n"));
 
-	DGNC_LOCK(dgnc_global_lock, lock_flags);
+	DGNC_LOCK(driver->lock, lock_flags);
 
 	/* mgmt device */
 	if (minor < MAXMGMTDEVICES) {
 		/* Only allow 1 open at a time on mgmt device */
 		if (dgnc_mgmt_in_use[minor]) {
-			DGNC_UNLOCK(dgnc_global_lock, lock_flags);
+			DGNC_UNLOCK(driver->lock, lock_flags);
 			return -EBUSY;
 		}
 		dgnc_mgmt_in_use[minor]++;
 	} else {
-		DGNC_UNLOCK(dgnc_global_lock, lock_flags);
+		DGNC_UNLOCK(driver->lock, lock_flags);
 		return -ENXIO;
 	}
 
-	DGNC_UNLOCK(dgnc_global_lock, lock_flags);
+	DGNC_UNLOCK(driver->lock, lock_flags);
 
 	DPR_MGMT(("dgnc_mgmt_open finish.\n"));
 
@@ -99,17 +100,18 @@ int dgnc_mgmt_close(struct inode *inode, struct file *file)
 {
 	unsigned long lock_flags;
 	unsigned int minor = iminor(inode);
+	struct dgnc_driver *driver = get_driver();
 
 	DPR_MGMT(("dgnc_mgmt_close start.\n"));
 
-	DGNC_LOCK(dgnc_global_lock, lock_flags);
+	DGNC_LOCK(driver->lock, lock_flags);
 
 	/* mgmt device */
 	if (minor < MAXMGMTDEVICES) {
 		if (dgnc_mgmt_in_use[minor])
 			dgnc_mgmt_in_use[minor] = 0;
 	}
-	DGNC_UNLOCK(dgnc_global_lock, lock_flags);
+	DGNC_UNLOCK(driver->lock, lock_flags);
 
 	DPR_MGMT(("dgnc_mgmt_close finish.\n"));
 
@@ -127,6 +129,7 @@ long dgnc_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
 	unsigned long lock_flags;
 	void __user *uarg = (void __user *) arg;
+	struct dgnc_driver *driver = get_driver();
 
 	DPR_MGMT(("dgnc_mgmt_ioctl start.\n"));
 
@@ -141,12 +144,12 @@ long dgnc_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 		 */
 		struct digi_dinfo ddi;
 
-		DGNC_LOCK(dgnc_global_lock, lock_flags);
+		DGNC_LOCK(driver->lock, lock_flags);
 
-		ddi.dinfo_nboards = dgnc_NumBoards;
+		ddi.dinfo_nboards = driver->num_boards;
 		sprintf(ddi.dinfo_version, "%s", DG_PART);
 
-		DGNC_UNLOCK(dgnc_global_lock, lock_flags);
+		DGNC_UNLOCK(driver->lock, lock_flags);
 
 		DPR_MGMT(("DIGI_GETDD returning numboards: %d version: %s\n",
 			ddi.dinfo_nboards, ddi.dinfo_version));
@@ -168,27 +171,28 @@ long dgnc_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 
 		DPR_MGMT(("DIGI_GETBD asking about board: %d\n", brd));
 
-		if ((brd < 0) || (brd > dgnc_NumBoards) ||
-		    (dgnc_NumBoards == 0))
+		if ((brd < 0) || (brd > driver->num_boards) ||
+		    (driver->num_boards == 0))
 			return -ENODEV;
 
 		memset(&di, 0, sizeof(di));
 
 		di.info_bdnum = brd;
 
-		DGNC_LOCK(dgnc_Board[brd]->bd_lock, lock_flags);
+		DGNC_LOCK(driver->board[brd]->bd_lock, lock_flags);
 
-		di.info_bdtype = dgnc_Board[brd]->dpatype;
-		di.info_bdstate = dgnc_Board[brd]->dpastatus;
+		di.info_bdtype = driver->board[brd]->dpatype;
+		di.info_bdstate = driver->board[brd]->dpastatus;
 		di.info_ioport = 0;
-		di.info_physaddr = (ulong) dgnc_Board[brd]->membase;
-		di.info_physsize = (ulong) dgnc_Board[brd]->membase - dgnc_Board[brd]->membase_end;
-		if (dgnc_Board[brd]->state != BOARD_FAILED)
-			di.info_nports = dgnc_Board[brd]->nasync;
+		di.info_physaddr = (ulong) driver->board[brd]->membase;
+		di.info_physsize = (ulong) driver->board[brd]->membase -
+					driver->board[brd]->membase_end;
+		if (driver->board[brd]->state != BOARD_FAILED)
+			di.info_nports = driver->board[brd]->nasync;
 		else
 			di.info_nports = 0;
 
-		DGNC_UNLOCK(dgnc_Board[brd]->bd_lock, lock_flags);
+		DGNC_UNLOCK(driver->board[brd]->bd_lock, lock_flags);
 
 		DPR_MGMT(("DIGI_GETBD returning type: %x state: %x ports: %x size: %x\n",
 			di.info_bdtype, di.info_bdstate, di.info_nports, di.info_physsize));
@@ -217,14 +221,14 @@ long dgnc_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 		channel = ni.channel;
 
 		/* Verify boundaries on board */
-		if ((board > dgnc_NumBoards) || (dgnc_NumBoards == 0))
+		if ((board > driver->num_boards) || (driver->num_boards == 0))
 			return -ENODEV;
 
 		/* Verify boundaries on channel */
-		if ((channel < 0) || (channel > dgnc_Board[board]->nasync))
+		if ((channel < 0) || (channel > driver->board[board]->nasync))
 			return -ENODEV;
 
-		ch = dgnc_Board[board]->channels[channel];
+		ch = driver->board[board]->channels[channel];
 
 		if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
 			return -ENODEV;
diff --git a/drivers/staging/dgnc/dgnc_sysfs.c b/drivers/staging/dgnc/dgnc_sysfs.c
index 3f321bb..08a54c9 100644
--- a/drivers/staging/dgnc/dgnc_sysfs.c
+++ b/drivers/staging/dgnc/dgnc_sysfs.c
@@ -52,7 +52,7 @@ static DRIVER_ATTR(version, S_IRUSR, dgnc_driver_version_show, NULL);
 
 static ssize_t dgnc_driver_boards_show(struct device_driver *ddp, char *buf)
 {
-	return snprintf(buf, PAGE_SIZE, "%d\n", dgnc_NumBoards);
+	return snprintf(buf, PAGE_SIZE, "%d\n", get_driver()->num_boards);
 }
 static DRIVER_ATTR(boards, S_IRUSR, dgnc_driver_boards_show, NULL);
 
@@ -66,14 +66,17 @@ static DRIVER_ATTR(maxboards, S_IRUSR, dgnc_driver_maxboards_show, NULL);
 
 static ssize_t dgnc_driver_pollcounter_show(struct device_driver *ddp, char *buf)
 {
-	return snprintf(buf, PAGE_SIZE, "%ld\n", dgnc_poll_counter);
+	return snprintf(buf, PAGE_SIZE, "%ld\n", get_driver()->poll_counter);
 }
 static DRIVER_ATTR(pollcounter, S_IRUSR, dgnc_driver_pollcounter_show, NULL);
 
 
 static ssize_t dgnc_driver_state_show(struct device_driver *ddp, char *buf)
 {
-	return snprintf(buf, PAGE_SIZE, "%s\n", dgnc_driver_state_text[dgnc_driver_state]);
+	return snprintf(buf,
+			PAGE_SIZE,
+			"%s\n",
+			dgnc_driver_state_text[get_driver()->state]);
 }
 static DRIVER_ATTR(state, S_IRUSR, dgnc_driver_state_show, NULL);
 
@@ -85,8 +88,14 @@ static ssize_t dgnc_driver_debug_show(struct device_driver *ddp, char *buf)
 
 static ssize_t dgnc_driver_debug_store(struct device_driver *ddp, const char *buf, size_t count)
 {
-	sscanf(buf, "0x%x\n", &dgnc_debug);
-	return count;
+	int retval = sscanf(buf, "0x%x\n", &dgnc_debug);
+
+	if (retval == 1)
+		retval = count;
+	else
+		retval = -EINVAL;
+
+	return retval;
 }
 static DRIVER_ATTR(debug, (S_IRUSR | S_IWUSR), dgnc_driver_debug_show, dgnc_driver_debug_store);
 
@@ -106,13 +115,19 @@ static DRIVER_ATTR(rawreadok, (S_IRUSR | S_IWUSR), dgnc_driver_rawreadok_show, d
 
 static ssize_t dgnc_driver_pollrate_show(struct device_driver *ddp, char *buf)
 {
-	return snprintf(buf, PAGE_SIZE, "%dms\n", dgnc_poll_tick);
+	return snprintf(buf, PAGE_SIZE, "%dms\n", get_driver()->poll_tick);
 }
 
 static ssize_t dgnc_driver_pollrate_store(struct device_driver *ddp, const char *buf, size_t count)
 {
-	sscanf(buf, "%d\n", &dgnc_poll_tick);
-	return count;
+	int retval = sscanf(buf, "%d\n", &(get_driver()->poll_tick));
+
+	if (retval == 1)
+		retval = count;
+	else
+		retval = -EINVAL;
+
+	return retval;
 }
 static DRIVER_ATTR(pollrate, (S_IRUSR | S_IWUSR), dgnc_driver_pollrate_show, dgnc_driver_pollrate_store);
 
-- 
1.8.1.2

_______________________________________________
devel mailing list
devel@xxxxxxxxxxxxxxxxxxxxxx
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel




[Index of Archives]     [Linux Driver Backports]     [DMA Engine]     [Linux GPIO]     [Linux SPI]     [Video for Linux]     [Linux USB Devel]     [Linux Coverity]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]
  Powered by Linux