[PATCH 09/13] staging: comedi: pcmuio: add inline helpers to get the 'iobase', 'asic', and 'port'

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

 



To reduce the potential for bugs, introduce a couple inline helper
functions to consolidate the calculations needed to get the 'iobase'
for a given asic and the 'asic' and 'port' associated with a given
subdevice.

Signed-off-by: H Hartley Sweeten <hsweeten@xxxxxxxxxxxxxxxxxxx>
Cc: Ian Abbott <abbotti@xxxxxxxxx>
Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
---
 drivers/staging/comedi/drivers/pcmuio.c | 49 ++++++++++++++++++++++++---------
 1 file changed, 36 insertions(+), 13 deletions(-)

diff --git a/drivers/staging/comedi/drivers/pcmuio.c b/drivers/staging/comedi/drivers/pcmuio.c
index 087911d..9e746a5 100644
--- a/drivers/staging/comedi/drivers/pcmuio.c
+++ b/drivers/staging/comedi/drivers/pcmuio.c
@@ -138,11 +138,35 @@ struct pcmuio_private {
 	} asics[PCMUIO_MAX_ASICS];
 };
 
+static inline unsigned long pcmuio_asic_iobase(struct comedi_device *dev,
+					       int asic)
+{
+	return dev->iobase + (asic * PCMUIO_ASIC_IOSIZE);
+}
+
+static inline int pcmuio_subdevice_to_asic(struct comedi_subdevice *s)
+{
+	/*
+	 * subdevice 0 and 1 are handled by the first asic
+	 * subdevice 2 and 3 are handled by the second asic
+	 */
+	return s->index / 2;
+}
+
+static inline int pcmuio_subdevice_to_port(struct comedi_subdevice *s)
+{
+	/*
+	 * subdevice 0 and 2 use port registers 0-2
+	 * subdevice 1 and 3 use port registers 3-5
+	 */
+	return (s->index % 2) ? 3 : 0;
+}
+
 static void pcmuio_write(struct comedi_device *dev, unsigned int val,
 			 int asic, int page, int port)
 {
 	struct pcmuio_private *devpriv = dev->private;
-	unsigned long iobase = dev->iobase + (asic * PCMUIO_ASIC_IOSIZE);
+	unsigned long iobase = pcmuio_asic_iobase(dev, asic);
 	unsigned long flags;
 
 	spin_lock_irqsave(&devpriv->asics[asic].pagelock, flags);
@@ -164,7 +188,7 @@ static unsigned int pcmuio_read(struct comedi_device *dev,
 				int asic, int page, int port)
 {
 	struct pcmuio_private *devpriv = dev->private;
-	unsigned long iobase = dev->iobase + (asic * PCMUIO_ASIC_IOSIZE);
+	unsigned long iobase = pcmuio_asic_iobase(dev, asic);
 	unsigned long flags;
 	unsigned int val;
 
@@ -199,10 +223,10 @@ static int pcmuio_dio_insn_bits(struct comedi_device *dev,
 				struct comedi_subdevice *s,
 				struct comedi_insn *insn, unsigned int *data)
 {
+	int asic = pcmuio_subdevice_to_asic(s);
+	int port = pcmuio_subdevice_to_port(s);
 	unsigned int mask = data[0] & s->io_bits;	/* outputs only */
 	unsigned int bits = data[1];
-	int asic = s->index / 2;
-	int port = (s->index % 2) ? 3 : 0;
 	unsigned int val;
 
 	/* get inverted state of the channels from the port */
@@ -235,9 +259,9 @@ static int pcmuio_dio_insn_config(struct comedi_device *dev,
 				  struct comedi_subdevice *s,
 				  struct comedi_insn *insn, unsigned int *data)
 {
+	int asic = pcmuio_subdevice_to_asic(s);
+	int port = pcmuio_subdevice_to_port(s);
 	unsigned int chan_mask = 1 << CR_CHAN(insn->chanspec);
-	int asic = s->index / 2;
-	int port = (s->index % 2) ? 3 : 0;
 
 	switch (data[0]) {
 	case INSN_CONFIG_DIO_OUTPUT:
@@ -279,7 +303,7 @@ static void pcmuio_stop_intr(struct comedi_device *dev,
 			     struct comedi_subdevice *s)
 {
 	struct pcmuio_private *devpriv = dev->private;
-	int asic = s->index / 2;
+	int asic = pcmuio_subdevice_to_asic(s);
 
 	devpriv->asics[asic].enabled_mask = 0;
 	devpriv->asics[asic].active = 0;
@@ -346,7 +370,7 @@ done:
 
 static int pcmuio_handle_asic_interrupt(struct comedi_device *dev, int asic)
 {
-	unsigned long iobase = dev->iobase + (asic * PCMUIO_ASIC_IOSIZE);
+	unsigned long iobase = pcmuio_asic_iobase(dev, asic);
 	unsigned int val;
 
 	/* check if there are any interrupts pending */
@@ -381,7 +405,7 @@ static int pcmuio_start_intr(struct comedi_device *dev,
 			     struct comedi_subdevice *s)
 {
 	struct pcmuio_private *devpriv = dev->private;
-	int asic = s->index / 2;
+	int asic = pcmuio_subdevice_to_asic(s);
 
 	if (!devpriv->asics[asic].continuous &&
 	    devpriv->asics[asic].stop_count == 0) {
@@ -391,7 +415,6 @@ static int pcmuio_start_intr(struct comedi_device *dev,
 		return 1;
 	} else {
 		unsigned bits = 0, pol_bits = 0, n;
-		int asic = s->index / 2;
 		struct comedi_cmd *cmd = &s->async->cmd;
 
 		devpriv->asics[asic].enabled_mask = 0;
@@ -418,7 +441,7 @@ static int pcmuio_start_intr(struct comedi_device *dev,
 static int pcmuio_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
 {
 	struct pcmuio_private *devpriv = dev->private;
-	int asic = s->index / 2;
+	int asic = pcmuio_subdevice_to_asic(s);
 	unsigned long flags;
 
 	spin_lock_irqsave(&devpriv->asics[asic].spinlock, flags);
@@ -437,7 +460,7 @@ pcmuio_inttrig_start_intr(struct comedi_device *dev, struct comedi_subdevice *s,
 			  unsigned int trignum)
 {
 	struct pcmuio_private *devpriv = dev->private;
-	int asic = s->index / 2;
+	int asic = pcmuio_subdevice_to_asic(s);
 	unsigned long flags;
 	int event = 0;
 
@@ -464,7 +487,7 @@ static int pcmuio_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
 {
 	struct pcmuio_private *devpriv = dev->private;
 	struct comedi_cmd *cmd = &s->async->cmd;
-	int asic = s->index / 2;
+	int asic = pcmuio_subdevice_to_asic(s);
 	unsigned long flags;
 	int event = 0;
 
-- 
1.8.3.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