[PATCH 3/9] staging: comedi: core: introduce comedi_offset_munge()

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

 



The comedi core expects the data to/from analog subdevices to be
in offset binary coding. This means that a value of '0' represents
the most negative and a value of 's->maxdata' the most positive
signal of the analog subdevice.

Many comedi drivers require the data written to the analog outputs,
or returned from the analog inputs, be in two's complement format.

Introduce a helper function to munge the data for analog subdevices.
This function simply inverts the sign bit and masks the result by
's->maxdata' to keep the result in range for the subdevice. The
strange:

  return val ^ s->maxdata ^ (s->maxdata >> 1);

is equivalent to:

  return (val ^ ((s->maxdata + 1) >> 1) & s->maxdata;

as long as s->maxdata is a value of the form (1 << n) - 1. This
avoids the 32-bit unsigned overflow for a s->maxdata of 0xffffffff.

Signed-off-by: H Hartley Sweeten <hsweeten@xxxxxxxxxxxxxxxxxxx>
Cc: Ian Abbott <abbotti@xxxxxxxxx>
Cc: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
---
 drivers/staging/comedi/comedidev.h | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/staging/comedi/comedidev.h b/drivers/staging/comedi/comedidev.h
index 1d8e61a..bec2276 100644
--- a/drivers/staging/comedi/comedidev.h
+++ b/drivers/staging/comedi/comedidev.h
@@ -307,7 +307,12 @@ static inline bool comedi_range_is_unipolar(struct comedi_subdevice *s,
 	return s->range_table->range[range].min >= 0;
 }
 
-/* some silly little inline functions */
+/* munge between offset binary and two's complement values */
+static inline unsigned int comedi_offset_munge(struct comedi_subdevice *s,
+					       unsigned int val)
+{
+	return val ^ s->maxdata ^ (s->maxdata >> 1);
+}
 
 static inline unsigned int bytes_per_sample(const struct comedi_subdevice *subd)
 {
-- 
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