2017-11-07 21:58:43 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2008-11-05 12:29:31 +08:00
|
|
|
/*
|
2015-01-29 02:41:49 +08: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 12:29:31 +08:00
|
|
|
|
2010-05-01 06:35:37 +08:00
|
|
|
#include <linux/uaccess.h>
|
2021-11-17 20:05:59 +08:00
|
|
|
#include <linux/comedi/comedidev.h>
|
2012-06-19 17:17:44 +08:00
|
|
|
#include "comedi_internal.h"
|
2008-11-05 12:29:31 +08:00
|
|
|
|
2009-03-17 10:05:31 +08:00
|
|
|
const struct comedi_lrange range_bipolar10 = { 1, {BIP_RANGE(10)} };
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(range_bipolar10);
|
2010-05-03 17:39:09 +08:00
|
|
|
const struct comedi_lrange range_bipolar5 = { 1, {BIP_RANGE(5)} };
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(range_bipolar5);
|
2010-05-03 17:39:09 +08:00
|
|
|
const struct comedi_lrange range_bipolar2_5 = { 1, {BIP_RANGE(2.5)} };
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(range_bipolar2_5);
|
2010-05-03 17:39:09 +08:00
|
|
|
const struct comedi_lrange range_unipolar10 = { 1, {UNI_RANGE(10)} };
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(range_unipolar10);
|
2010-05-03 17:39:09 +08:00
|
|
|
const struct comedi_lrange range_unipolar5 = { 1, {UNI_RANGE(5)} };
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(range_unipolar5);
|
2013-04-04 04:38:26 +08:00
|
|
|
const struct comedi_lrange range_unipolar2_5 = { 1, {UNI_RANGE(2.5)} };
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(range_unipolar2_5);
|
2013-04-04 04:40:13 +08:00
|
|
|
const struct comedi_lrange range_0_20mA = { 1, {RANGE_mA(0, 20)} };
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(range_0_20mA);
|
2013-04-04 04:40:13 +08:00
|
|
|
const struct comedi_lrange range_4_20mA = { 1, {RANGE_mA(4, 20)} };
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(range_4_20mA);
|
2013-04-04 04:40:13 +08:00
|
|
|
const struct comedi_lrange range_0_32mA = { 1, {RANGE_mA(0, 32)} };
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(range_0_32mA);
|
2010-05-03 17:39:09 +08:00
|
|
|
const struct comedi_lrange range_unknown = { 1, {{0, 1000000, UNIT_none} } };
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(range_unknown);
|
2008-11-05 12:29:31 +08:00
|
|
|
|
|
|
|
/*
|
2015-01-29 02:41:48 +08: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-04 06:50:09 +08:00
|
|
|
int do_rangeinfo_ioctl(struct comedi_device *dev,
|
2020-04-26 06:44:30 +08:00
|
|
|
struct comedi_rangeinfo *it)
|
2008-11-05 12:29:31 +08:00
|
|
|
{
|
|
|
|
int subd, chan;
|
2009-03-17 10:05:31 +08:00
|
|
|
const struct comedi_lrange *lr;
|
2009-03-17 10:05:14 +08:00
|
|
|
struct comedi_subdevice *s;
|
2008-11-05 12:29:31 +08:00
|
|
|
|
2020-04-26 06:44:30 +08:00
|
|
|
subd = (it->range_type >> 24) & 0xf;
|
|
|
|
chan = (it->range_type >> 16) & 0xff;
|
2008-11-05 12:29:31 +08:00
|
|
|
|
|
|
|
if (!dev->attached)
|
|
|
|
return -EINVAL;
|
|
|
|
if (subd >= dev->n_subdevices)
|
|
|
|
return -EINVAL;
|
2012-09-06 09:59:44 +08:00
|
|
|
s = &dev->subdevices[subd];
|
2008-11-05 12:29:31 +08: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-26 06:44:30 +08:00
|
|
|
if (RANGE_LENGTH(it->range_type) != lr->length) {
|
2013-11-23 01:45:58 +08:00
|
|
|
dev_dbg(dev->class_dev,
|
|
|
|
"wrong length %d should be %d (0x%08x)\n",
|
2020-04-26 06:44:30 +08:00
|
|
|
RANGE_LENGTH(it->range_type),
|
|
|
|
lr->length, it->range_type);
|
2008-11-05 12:29:31 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-04-26 06:44:30 +08:00
|
|
|
if (copy_to_user(it->range_ptr, lr->range,
|
2009-06-08 23:34:41 +08:00
|
|
|
sizeof(struct comedi_krange) * lr->length))
|
2008-11-05 12:29:31 +08:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-25 01:00:45 +08: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-06 01:13:26 +08: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-02 03:33:17 +08:00
|
|
|
int comedi_check_chanlist(struct comedi_subdevice *s, int n,
|
|
|
|
unsigned int *chanlist)
|
2008-11-05 12:29:31 +08:00
|
|
|
{
|
2012-08-16 21:38:05 +08:00
|
|
|
struct comedi_device *dev = s->device;
|
2013-07-25 01:00:45 +08:00
|
|
|
unsigned int chanspec;
|
|
|
|
int chan, range_len, i;
|
2008-11-05 12:29:31 +08:00
|
|
|
|
2014-03-06 14:57:18 +08: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-08 01:50:24 +08:00
|
|
|
CR_RANGE(chanspec) >= range_len) {
|
2014-03-06 14:57:18 +08: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 12:29:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(comedi_check_chanlist);
|