2008-11-04 21:29:31 -07:00
|
|
|
/*
|
|
|
|
module/range.c
|
|
|
|
comedi routines for voltage ranges
|
|
|
|
|
|
|
|
COMEDI - Linux Control and Measurement Device Interface
|
|
|
|
Copyright (C) 1997-8 David A. Schleef <ds@schleef.org>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2010-04-30 16:35:37 -06:00
|
|
|
#include <linux/uaccess.h>
|
2010-05-03 16:50:09 -06:00
|
|
|
#include "comedidev.h"
|
2012-06-19 03:17:44 -06:00
|
|
|
#include "comedi_internal.h"
|
2008-11-04 21:29:31 -07:00
|
|
|
|
2009-03-16 20:05:31 -06:00
|
|
|
const struct comedi_lrange range_bipolar10 = { 1, {BIP_RANGE(10)} };
|
2013-04-12 11:11:54 -06:00
|
|
|
EXPORT_SYMBOL_GPL(range_bipolar10);
|
2010-05-03 03:39:09 -06:00
|
|
|
const struct comedi_lrange range_bipolar5 = { 1, {BIP_RANGE(5)} };
|
2013-04-12 11:11:54 -06:00
|
|
|
EXPORT_SYMBOL_GPL(range_bipolar5);
|
2010-05-03 03:39:09 -06:00
|
|
|
const struct comedi_lrange range_bipolar2_5 = { 1, {BIP_RANGE(2.5)} };
|
2013-04-12 11:11:54 -06:00
|
|
|
EXPORT_SYMBOL_GPL(range_bipolar2_5);
|
2010-05-03 03:39:09 -06:00
|
|
|
const struct comedi_lrange range_unipolar10 = { 1, {UNI_RANGE(10)} };
|
2013-04-12 11:11:54 -06:00
|
|
|
EXPORT_SYMBOL_GPL(range_unipolar10);
|
2010-05-03 03:39:09 -06:00
|
|
|
const struct comedi_lrange range_unipolar5 = { 1, {UNI_RANGE(5)} };
|
2013-04-12 11:11:54 -06:00
|
|
|
EXPORT_SYMBOL_GPL(range_unipolar5);
|
2013-04-03 14:38:26 -06:00
|
|
|
const struct comedi_lrange range_unipolar2_5 = { 1, {UNI_RANGE(2.5)} };
|
2013-04-12 11:11:54 -06:00
|
|
|
EXPORT_SYMBOL_GPL(range_unipolar2_5);
|
2013-04-03 14:40:13 -06:00
|
|
|
const struct comedi_lrange range_0_20mA = { 1, {RANGE_mA(0, 20)} };
|
2013-04-12 11:11:54 -06:00
|
|
|
EXPORT_SYMBOL_GPL(range_0_20mA);
|
2013-04-03 14:40:13 -06:00
|
|
|
const struct comedi_lrange range_4_20mA = { 1, {RANGE_mA(4, 20)} };
|
2013-04-12 11:11:54 -06:00
|
|
|
EXPORT_SYMBOL_GPL(range_4_20mA);
|
2013-04-03 14:40:13 -06:00
|
|
|
const struct comedi_lrange range_0_32mA = { 1, {RANGE_mA(0, 32)} };
|
2013-04-12 11:11:54 -06:00
|
|
|
EXPORT_SYMBOL_GPL(range_0_32mA);
|
2010-05-03 03:39:09 -06:00
|
|
|
const struct comedi_lrange range_unknown = { 1, {{0, 1000000, UNIT_none} } };
|
2013-04-12 11:11:54 -06:00
|
|
|
EXPORT_SYMBOL_GPL(range_unknown);
|
2008-11-04 21:29:31 -07:00
|
|
|
|
|
|
|
/*
|
2009-04-09 14:07:27 -06:00
|
|
|
COMEDI_RANGEINFO
|
2008-11-04 21:29:31 -07:00
|
|
|
range information ioctl
|
|
|
|
|
|
|
|
arg:
|
|
|
|
pointer to rangeinfo structure
|
|
|
|
|
|
|
|
reads:
|
|
|
|
range info structure
|
|
|
|
|
|
|
|
writes:
|
2009-03-16 20:06:31 -06:00
|
|
|
n struct comedi_krange structures to rangeinfo->range_ptr
|
2008-11-04 21:29:31 -07:00
|
|
|
*/
|
2010-05-03 16:50:09 -06:00
|
|
|
int do_rangeinfo_ioctl(struct comedi_device *dev,
|
|
|
|
struct comedi_rangeinfo __user *arg)
|
2008-11-04 21:29:31 -07:00
|
|
|
{
|
2009-03-16 20:06:26 -06:00
|
|
|
struct comedi_rangeinfo it;
|
2008-11-04 21:29:31 -07:00
|
|
|
int subd, chan;
|
2009-03-16 20:05:31 -06:00
|
|
|
const struct comedi_lrange *lr;
|
2009-03-16 20:05:14 -06:00
|
|
|
struct comedi_subdevice *s;
|
2008-11-04 21:29:31 -07:00
|
|
|
|
2009-03-16 20:06:26 -06:00
|
|
|
if (copy_from_user(&it, arg, sizeof(struct comedi_rangeinfo)))
|
2008-11-04 21:29:31 -07:00
|
|
|
return -EFAULT;
|
|
|
|
subd = (it.range_type >> 24) & 0xf;
|
|
|
|
chan = (it.range_type >> 16) & 0xff;
|
|
|
|
|
|
|
|
if (!dev->attached)
|
|
|
|
return -EINVAL;
|
|
|
|
if (subd >= dev->n_subdevices)
|
|
|
|
return -EINVAL;
|
2012-09-05 19:59:44 -06:00
|
|
|
s = &dev->subdevices[subd];
|
2008-11-04 21:29:31 -07: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RANGE_LENGTH(it.range_type) != lr->length) {
|
2013-11-22 10:45:58 -07:00
|
|
|
dev_dbg(dev->class_dev,
|
|
|
|
"wrong length %d should be %d (0x%08x)\n",
|
|
|
|
RANGE_LENGTH(it.range_type),
|
|
|
|
lr->length, it.range_type);
|
2008-11-04 21:29:31 -07:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (copy_to_user(it.range_ptr, lr->range,
|
2009-06-08 09:34:41 -06:00
|
|
|
sizeof(struct comedi_krange) * lr->length))
|
2008-11-04 21:29:31 -07:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-16 20:05:14 -06:00
|
|
|
static int aref_invalid(struct comedi_subdevice *s, unsigned int chanspec)
|
2008-11-04 21:29:31 -07:00
|
|
|
{
|
|
|
|
unsigned int aref;
|
|
|
|
|
2009-03-16 20:03:24 -06:00
|
|
|
/* disable reporting invalid arefs... maybe someday */
|
2008-11-04 21:29:31 -07:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
aref = CR_AREF(chanspec);
|
|
|
|
switch (aref) {
|
|
|
|
case AREF_DIFF:
|
|
|
|
if (s->subdev_flags & SDF_DIFF)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case AREF_COMMON:
|
|
|
|
if (s->subdev_flags & SDF_COMMON)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case AREF_GROUND:
|
|
|
|
if (s->subdev_flags & SDF_GROUND)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case AREF_OTHER:
|
|
|
|
if (s->subdev_flags & SDF_OTHER)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-11-22 10:45:58 -07:00
|
|
|
dev_dbg(s->device->class_dev, "subdevice does not support aref %i",
|
|
|
|
aref);
|
2008-11-04 21:29:31 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-07-24 11:00:45 -06: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
|
2008-11-04 21:29:31 -07:00
|
|
|
*/
|
2010-05-01 13:33:17 -06:00
|
|
|
int comedi_check_chanlist(struct comedi_subdevice *s, int n,
|
|
|
|
unsigned int *chanlist)
|
2008-11-04 21:29:31 -07:00
|
|
|
{
|
2012-08-16 07:38:05 -06:00
|
|
|
struct comedi_device *dev = s->device;
|
2013-07-24 11:00:45 -06:00
|
|
|
unsigned int chanspec;
|
|
|
|
int chan, range_len, i;
|
2008-11-04 21:29:31 -07:00
|
|
|
|
2013-07-24 11:00:45 -06:00
|
|
|
if (s->range_table || s->range_table_list) {
|
2008-11-04 21:29:31 -07:00
|
|
|
for (i = 0; i < n; i++) {
|
2013-07-24 11:00:45 -06:00
|
|
|
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;
|
2008-11-04 21:29:31 -07:00
|
|
|
if (chan >= s->n_chan ||
|
2013-07-24 11:00:45 -06:00
|
|
|
CR_RANGE(chanspec) >= range_len ||
|
|
|
|
aref_invalid(s, chanspec)) {
|
2012-08-16 07:38:05 -06:00
|
|
|
dev_warn(dev->class_dev,
|
2013-07-24 11:00:45 -06:00
|
|
|
"bad chanlist[%d]=0x%08x chan=%d range length=%d\n",
|
|
|
|
i, chanspec, chan, range_len);
|
2008-11-04 21:29:31 -07:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2012-08-16 07:38:05 -06:00
|
|
|
dev_err(dev->class_dev, "(bug) no range type list!\n");
|
2008-11-04 21:29:31 -07:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2013-04-12 11:11:54 -06:00
|
|
|
EXPORT_SYMBOL_GPL(comedi_check_chanlist);
|