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