[PATCH] staging: comedi: comedi_buf: introduce comedi_buf_n_bytes_ready()

H Hartley Sweeten hsweeten at visionengravers.com
Thu May 29 17:38:12 UTC 2014


Introduce a helper function to return the number of bytes that are
ready to read/write from/to the comedi_async buffer. The "write to"
use doesn't really make much sense but is handled for completeness.

Use the helper function in the comedi drivers that currently do the
calculation as part of the (*poll) operation.

Also, use the helper in comedi_fops where the calculation is used as
part of the subdevice going nonbusy.

Signed-off-by: H Hartley Sweeten <hsweeten at visionengravers.com>
Cc: Ian Abbott <abbotti at mev.co.uk>
Cc: Greg Kroah-Hartman <gregk at linuxfoundation.org>
---
 drivers/staging/comedi/comedi_buf.c            | 18 ++++++++++++++++++
 drivers/staging/comedi/comedi_fops.c           |  5 ++---
 drivers/staging/comedi/comedidev.h             |  2 ++
 drivers/staging/comedi/drivers/das16m1.c       |  2 +-
 drivers/staging/comedi/drivers/das1800.c       |  2 +-
 drivers/staging/comedi/drivers/ni_mio_common.c |  2 +-
 drivers/staging/comedi/drivers/ni_pcidio.c     |  2 +-
 drivers/staging/comedi/drivers/pcl812.c        |  2 +-
 drivers/staging/comedi/drivers/pcl816.c        |  2 +-
 9 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/drivers/staging/comedi/comedi_buf.c b/drivers/staging/comedi/comedi_buf.c
index df4a9c4..52fc634 100644
--- a/drivers/staging/comedi/comedi_buf.c
+++ b/drivers/staging/comedi/comedi_buf.c
@@ -243,6 +243,24 @@ void comedi_buf_reset(struct comedi_subdevice *s)
 	async->events = 0;
 }
 
+/**
+ * comedi_buf_n_bytes_ready() - Returns the number of bytes ready to read/write
+ * @s: comedi_subdevice struct
+ */
+unsigned int comedi_buf_n_bytes_ready(struct comedi_subdevice *s)
+{
+	struct comedi_async *async = s->async;
+
+	if (async->buf_write_count == async->buf_read_count)
+		return 0;
+
+	if (async->buf_write_count > async->buf_read_count)
+		return async->buf_write_count - async->buf_read_count;
+	else
+		return async->buf_read_count - async->buf_write_count;
+}
+EXPORT_SYMBOL_GPL(comedi_buf_n_bytes_ready);
+
 static unsigned int comedi_buf_write_n_available(struct comedi_subdevice *s)
 {
 	struct comedi_async *async = s->async;
diff --git a/drivers/staging/comedi/comedi_fops.c b/drivers/staging/comedi/comedi_fops.c
index 9d99fb3..f5eda5f 100644
--- a/drivers/staging/comedi/comedi_fops.c
+++ b/drivers/staging/comedi/comedi_fops.c
@@ -997,7 +997,7 @@ static int do_bufinfo_ioctl(struct comedi_device *dev,
 		comedi_buf_read_free(s, bi.bytes_read);
 
 		if (comedi_is_subdevice_idle(s) &&
-		    async->buf_write_count == async->buf_read_count) {
+		    comedi_buf_n_bytes_ready(s) == 0) {
 			do_become_nonbusy(dev, s);
 		}
 	}
@@ -2303,8 +2303,7 @@ static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
 		new_s = comedi_read_subdevice(dev, minor);
 		if (dev->attached && old_detach_count == dev->detach_count &&
 		    s == new_s && new_s->async == async) {
-			if (become_nonbusy ||
-			    async->buf_read_count - async->buf_write_count == 0)
+			if (become_nonbusy || comedi_buf_n_bytes_ready(s) == 0)
 				do_become_nonbusy(dev, s);
 		}
 		mutex_unlock(&dev->mutex);
diff --git a/drivers/staging/comedi/comedidev.h b/drivers/staging/comedi/comedidev.h
index 8f4e44b..789b0ea 100644
--- a/drivers/staging/comedi/comedidev.h
+++ b/drivers/staging/comedi/comedidev.h
@@ -333,6 +333,8 @@ static inline unsigned int bytes_per_sample(const struct comedi_subdevice *subd)
  */
 int comedi_set_hw_dev(struct comedi_device *dev, struct device *hw_dev);
 
+unsigned int comedi_buf_n_bytes_ready(struct comedi_subdevice *);
+
 unsigned int comedi_buf_write_alloc(struct comedi_subdevice *s, unsigned int n);
 unsigned int comedi_buf_write_free(struct comedi_subdevice *s, unsigned int n);
 
diff --git a/drivers/staging/comedi/drivers/das16m1.c b/drivers/staging/comedi/drivers/das16m1.c
index ec039fb..c252ad2 100644
--- a/drivers/staging/comedi/drivers/das16m1.c
+++ b/drivers/staging/comedi/drivers/das16m1.c
@@ -477,7 +477,7 @@ static int das16m1_poll(struct comedi_device *dev, struct comedi_subdevice *s)
 	das16m1_handler(dev, status);
 	spin_unlock_irqrestore(&dev->spinlock, flags);
 
-	return s->async->buf_write_count - s->async->buf_read_count;
+	return comedi_buf_n_bytes_ready(s);
 }
 
 static irqreturn_t das16m1_interrupt(int irq, void *d)
diff --git a/drivers/staging/comedi/drivers/das1800.c b/drivers/staging/comedi/drivers/das1800.c
index 8595190..b2f8391d 100644
--- a/drivers/staging/comedi/drivers/das1800.c
+++ b/drivers/staging/comedi/drivers/das1800.c
@@ -696,7 +696,7 @@ static int das1800_ai_poll(struct comedi_device *dev,
 	das1800_ai_handler(dev);
 	spin_unlock_irqrestore(&dev->spinlock, flags);
 
-	return s->async->buf_write_count - s->async->buf_read_count;
+	return comedi_buf_n_bytes_ready(s);
 }
 
 static irqreturn_t das1800_interrupt(int irq, void *d)
diff --git a/drivers/staging/comedi/drivers/ni_mio_common.c b/drivers/staging/comedi/drivers/ni_mio_common.c
index 68cd92b..332ca0a 100644
--- a/drivers/staging/comedi/drivers/ni_mio_common.c
+++ b/drivers/staging/comedi/drivers/ni_mio_common.c
@@ -1502,7 +1502,7 @@ static int ni_ai_poll(struct comedi_device *dev, struct comedi_subdevice *s)
 #else
 	ni_sync_ai_dma(dev);
 #endif
-	count = s->async->buf_write_count - s->async->buf_read_count;
+	count = comedi_buf_n_bytes_ready(s);
 	spin_unlock_irqrestore(&dev->spinlock, flags);
 
 	return count;
diff --git a/drivers/staging/comedi/drivers/ni_pcidio.c b/drivers/staging/comedi/drivers/ni_pcidio.c
index 5fc74d6..b03105a 100644
--- a/drivers/staging/comedi/drivers/ni_pcidio.c
+++ b/drivers/staging/comedi/drivers/ni_pcidio.c
@@ -361,7 +361,7 @@ static int ni_pcidio_poll(struct comedi_device *dev, struct comedi_subdevice *s)
 	if (devpriv->di_mite_chan)
 		mite_sync_input_dma(devpriv->di_mite_chan, s);
 	spin_unlock(&devpriv->mite_channel_lock);
-	count = s->async->buf_write_count - s->async->buf_read_count;
+	count = comedi_buf_n_bytes_ready(s);
 	spin_unlock_irqrestore(&dev->spinlock, irq_flags);
 	return count;
 }
diff --git a/drivers/staging/comedi/drivers/pcl812.c b/drivers/staging/comedi/drivers/pcl812.c
index 4c1b947..803e779 100644
--- a/drivers/staging/comedi/drivers/pcl812.c
+++ b/drivers/staging/comedi/drivers/pcl812.c
@@ -985,7 +985,7 @@ static int pcl812_ai_poll(struct comedi_device *dev, struct comedi_subdevice *s)
 
 	spin_unlock_irqrestore(&dev->spinlock, flags);
 
-	return s->async->buf_write_count - s->async->buf_read_count;
+	return comedi_buf_n_bytes_ready(s);
 }
 
 static int pcl812_ai_cancel(struct comedi_device *dev,
diff --git a/drivers/staging/comedi/drivers/pcl816.c b/drivers/staging/comedi/drivers/pcl816.c
index d9ca7fe..eda9986 100644
--- a/drivers/staging/comedi/drivers/pcl816.c
+++ b/drivers/staging/comedi/drivers/pcl816.c
@@ -516,7 +516,7 @@ static int pcl816_ai_poll(struct comedi_device *dev, struct comedi_subdevice *s)
 
 	cfc_handle_events(dev, s);
 
-	return s->async->buf_write_count - s->async->buf_read_count;
+	return comedi_buf_n_bytes_ready(s);
 }
 
 static int pcl816_ai_cancel(struct comedi_device *dev,
-- 
1.9.2



More information about the devel mailing list