2017-11-07 13:58:43 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2008-11-05 04:29:31 +00:00
|
|
|
/*
|
2015-01-28 18:41:49 +00:00
|
|
|
* comedi/range.c
|
|
|
|
* comedi routines for voltage ranges
|
|
|
|
*
|
|
|
|
* COMEDI - Linux Control and Measurement Device Interface
|
|
|
|
* Copyright (C) 1997-8 David A. Schleef <ds@schleef.org>
|
|
|
|
*/
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2010-04-30 22:35:37 +00:00
|
|
|
#include <linux/uaccess.h>
|
2010-05-03 22:50:09 +00:00
|
|
|
#include "comedidev.h"
|
2012-06-19 09:17:44 +00:00
|
|
|
#include "comedi_internal.h"
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2009-03-17 02:05:31 +00:00
|
|
|
const struct comedi_lrange range_bipolar10 = { 1, {BIP_RANGE(10)} };
|
2013-04-12 17:11:54 +00:00
|
|
|
EXPORT_SYMBOL_GPL(range_bipolar10);
|
2010-05-03 09:39:09 +00:00
|
|
|
const struct comedi_lrange range_bipolar5 = { 1, {BIP_RANGE(5)} };
|
2013-04-12 17:11:54 +00:00
|
|
|
EXPORT_SYMBOL_GPL(range_bipolar5);
|
2010-05-03 09:39:09 +00:00
|
|
|
const struct comedi_lrange range_bipolar2_5 = { 1, {BIP_RANGE(2.5)} };
|
2013-04-12 17:11:54 +00:00
|
|
|
EXPORT_SYMBOL_GPL(range_bipolar2_5);
|
2010-05-03 09:39:09 +00:00
|
|
|
const struct comedi_lrange range_unipolar10 = { 1, {UNI_RANGE(10)} };
|
2013-04-12 17:11:54 +00:00
|
|
|
EXPORT_SYMBOL_GPL(range_unipolar10);
|
2010-05-03 09:39:09 +00:00
|
|
|
const struct comedi_lrange range_unipolar5 = { 1, {UNI_RANGE(5)} };
|
2013-04-12 17:11:54 +00:00
|
|
|
EXPORT_SYMBOL_GPL(range_unipolar5);
|
2013-04-03 20:38:26 +00:00
|
|
|
const struct comedi_lrange range_unipolar2_5 = { 1, {UNI_RANGE(2.5)} };
|
2013-04-12 17:11:54 +00:00
|
|
|
EXPORT_SYMBOL_GPL(range_unipolar2_5);
|
2013-04-03 20:40:13 +00:00
|
|
|
const struct comedi_lrange range_0_20mA = { 1, {RANGE_mA(0, 20)} };
|
2013-04-12 17:11:54 +00:00
|
|
|
EXPORT_SYMBOL_GPL(range_0_20mA);
|
2013-04-03 20:40:13 +00:00
|
|
|
const struct comedi_lrange range_4_20mA = { 1, {RANGE_mA(4, 20)} };
|
2013-04-12 17:11:54 +00:00
|
|
|
EXPORT_SYMBOL_GPL(range_4_20mA);
|
2013-04-03 20:40:13 +00:00
|
|
|
const struct comedi_lrange range_0_32mA = { 1, {RANGE_mA(0, 32)} };
|
2013-04-12 17:11:54 +00:00
|
|
|
EXPORT_SYMBOL_GPL(range_0_32mA);
|
2010-05-03 09:39:09 +00:00
|
|
|
const struct comedi_lrange range_unknown = { 1, {{0, 1000000, UNIT_none} } };
|
2013-04-12 17:11:54 +00:00
|
|
|
EXPORT_SYMBOL_GPL(range_unknown);
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
/*
|
2015-01-28 18:41:48 +00:00
|
|
|
* COMEDI_RANGEINFO ioctl
|
|
|
|
* range information
|
|
|
|
*
|
|
|
|
* arg:
|
|
|
|
* pointer to comedi_rangeinfo structure
|
|
|
|
*
|
|
|
|
* reads:
|
|
|
|
* comedi_rangeinfo structure
|
|
|
|
*
|
|
|
|
* writes:
|
|
|
|
* array of comedi_krange structures to rangeinfo->range_ptr pointer
|
|
|
|
*/
|
2010-05-03 22:50:09 +00:00
|
|
|
int do_rangeinfo_ioctl(struct comedi_device *dev,
|
2020-04-25 22:44:30 +00:00
|
|
|
struct comedi_rangeinfo *it)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
|
|
|
int subd, chan;
|
2009-03-17 02:05:31 +00:00
|
|
|
const struct comedi_lrange *lr;
|
2009-03-17 02:05:14 +00:00
|
|
|
struct comedi_subdevice *s;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2020-04-25 22:44:30 +00:00
|
|
|
subd = (it->range_type >> 24) & 0xf;
|
|
|
|
chan = (it->range_type >> 16) & 0xff;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
if (!dev->attached)
|
|
|
|
return -EINVAL;
|
|
|
|
if (subd >= dev->n_subdevices)
|
|
|
|
return -EINVAL;
|
2012-09-06 01:59:44 +00:00
|
|
|
s = &dev->subdevices[subd];
|
2008-11-05 04:29:31 +00:00
|
|
|
if (s->range_table) {
|
|
|
|
lr = s->range_table;
|
|
|
|
} else if (s->range_table_list) {
|
|
|
|
if (chan >= s->n_chan)
|
|
|
|
return -EINVAL;
|
|
|
|
lr = s->range_table_list[chan];
|
|
|
|
} else {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-04-25 22:44:30 +00:00
|
|
|
if (RANGE_LENGTH(it->range_type) != lr->length) {
|
2013-11-22 17:45:58 +00:00
|
|
|
dev_dbg(dev->class_dev,
|
|
|
|
"wrong length %d should be %d (0x%08x)\n",
|
2020-04-25 22:44:30 +00:00
|
|
|
RANGE_LENGTH(it->range_type),
|
|
|
|
lr->length, it->range_type);
|
2008-11-05 04:29:31 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-04-25 22:44:30 +00:00
|
|
|
if (copy_to_user(it->range_ptr, lr->range,
|
2009-06-08 15:34:41 +00:00
|
|
|
sizeof(struct comedi_krange) * lr->length))
|
2008-11-05 04:29:31 +00:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-24 17:00:45 +00:00
|
|
|
/**
|
|
|
|
* comedi_check_chanlist() - Validate each element in a chanlist.
|
|
|
|
* @s: comedi_subdevice struct
|
|
|
|
* @n: number of elements in the chanlist
|
|
|
|
* @chanlist: the chanlist to validate
|
2015-08-05 17:13:26 +00:00
|
|
|
*
|
|
|
|
* Each element consists of a channel number, a range index, an analog
|
|
|
|
* reference type and some flags, all packed into an unsigned int.
|
|
|
|
*
|
|
|
|
* This checks that the channel number and range index are supported by
|
|
|
|
* the comedi subdevice. It does not check whether the analog reference
|
|
|
|
* type and the flags are supported. Drivers that care should check those
|
|
|
|
* themselves.
|
|
|
|
*
|
|
|
|
* Return: %0 if all @chanlist elements are valid (success),
|
|
|
|
* %-EINVAL if one or more elements are invalid.
|
|
|
|
*/
|
2010-05-01 19:33:17 +00:00
|
|
|
int comedi_check_chanlist(struct comedi_subdevice *s, int n,
|
|
|
|
unsigned int *chanlist)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
2012-08-16 13:38:05 +00:00
|
|
|
struct comedi_device *dev = s->device;
|
2013-07-24 17:00:45 +00:00
|
|
|
unsigned int chanspec;
|
|
|
|
int chan, range_len, i;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2014-03-06 06:57:18 +00:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
chanspec = chanlist[i];
|
|
|
|
chan = CR_CHAN(chanspec);
|
|
|
|
if (s->range_table)
|
|
|
|
range_len = s->range_table->length;
|
|
|
|
else if (s->range_table_list && chan < s->n_chan)
|
|
|
|
range_len = s->range_table_list[chan]->length;
|
|
|
|
else
|
|
|
|
range_len = 0;
|
|
|
|
if (chan >= s->n_chan ||
|
2014-10-07 17:50:24 +00:00
|
|
|
CR_RANGE(chanspec) >= range_len) {
|
2014-03-06 06:57:18 +00:00
|
|
|
dev_warn(dev->class_dev,
|
|
|
|
"bad chanlist[%d]=0x%08x chan=%d range length=%d\n",
|
|
|
|
i, chanspec, chan, range_len);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
2008-11-05 04:29:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2013-04-12 17:11:54 +00:00
|
|
|
EXPORT_SYMBOL_GPL(comedi_check_chanlist);
|