blob: efec9022a7aec0ed019e0563cec0ae8e296bc9eb [file] [log] [blame]
David Schleefed9eccb2008-11-04 20:29:31 -08001/*
2 module/range.c
3 comedi routines for voltage ranges
4
5 COMEDI - Linux Control and Measurement Device Interface
6 Copyright (C) 1997-8 David A. Schleef <ds@schleef.org>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22*/
23
Greg Kroah-Hartmanecfe20d2010-04-30 15:35:37 -070024#include <linux/uaccess.h>
Greg Kroah-Hartman3b6b25b2010-05-03 15:50:09 -070025#include "comedidev.h"
Ian Abbott3a5fa272012-06-19 10:17:44 +010026#include "comedi_internal.h"
David Schleefed9eccb2008-11-04 20:29:31 -080027
Bill Pemberton9ced1de2009-03-16 22:05:31 -040028const struct comedi_lrange range_bipolar10 = { 1, {BIP_RANGE(10)} };
Greg Kroah-Hartmane4231512010-05-01 12:12:02 -070029EXPORT_SYMBOL(range_bipolar10);
Mark Rankilorf0f29182010-05-03 17:39:09 +080030const struct comedi_lrange range_bipolar5 = { 1, {BIP_RANGE(5)} };
Greg Kroah-Hartmane4231512010-05-01 12:12:02 -070031EXPORT_SYMBOL(range_bipolar5);
Mark Rankilorf0f29182010-05-03 17:39:09 +080032const struct comedi_lrange range_bipolar2_5 = { 1, {BIP_RANGE(2.5)} };
Greg Kroah-Hartmane4231512010-05-01 12:12:02 -070033EXPORT_SYMBOL(range_bipolar2_5);
Mark Rankilorf0f29182010-05-03 17:39:09 +080034const struct comedi_lrange range_unipolar10 = { 1, {UNI_RANGE(10)} };
Greg Kroah-Hartmane4231512010-05-01 12:12:02 -070035EXPORT_SYMBOL(range_unipolar10);
Mark Rankilorf0f29182010-05-03 17:39:09 +080036const struct comedi_lrange range_unipolar5 = { 1, {UNI_RANGE(5)} };
Greg Kroah-Hartmane4231512010-05-01 12:12:02 -070037EXPORT_SYMBOL(range_unipolar5);
H Hartley Sweeten5f8eb722013-04-03 13:38:26 -070038const struct comedi_lrange range_unipolar2_5 = { 1, {UNI_RANGE(2.5)} };
39EXPORT_SYMBOL(range_unipolar2_5);
Mark Rankilorf0f29182010-05-03 17:39:09 +080040const struct comedi_lrange range_unknown = { 1, {{0, 1000000, UNIT_none} } };
Greg Kroah-Hartmane4231512010-05-01 12:12:02 -070041EXPORT_SYMBOL(range_unknown);
David Schleefed9eccb2008-11-04 20:29:31 -080042
43/*
Bill Pemberton356cdbc2009-04-09 16:07:27 -040044 COMEDI_RANGEINFO
David Schleefed9eccb2008-11-04 20:29:31 -080045 range information ioctl
46
47 arg:
48 pointer to rangeinfo structure
49
50 reads:
51 range info structure
52
53 writes:
Bill Pemberton1f6325d2009-03-16 22:06:31 -040054 n struct comedi_krange structures to rangeinfo->range_ptr
David Schleefed9eccb2008-11-04 20:29:31 -080055*/
Greg Kroah-Hartman3b6b25b2010-05-03 15:50:09 -070056int do_rangeinfo_ioctl(struct comedi_device *dev,
57 struct comedi_rangeinfo __user *arg)
David Schleefed9eccb2008-11-04 20:29:31 -080058{
Bill Pembertond0a353f2009-03-16 22:06:26 -040059 struct comedi_rangeinfo it;
David Schleefed9eccb2008-11-04 20:29:31 -080060 int subd, chan;
Bill Pemberton9ced1de2009-03-16 22:05:31 -040061 const struct comedi_lrange *lr;
Bill Pemberton34c43922009-03-16 22:05:14 -040062 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -080063
Bill Pembertond0a353f2009-03-16 22:06:26 -040064 if (copy_from_user(&it, arg, sizeof(struct comedi_rangeinfo)))
David Schleefed9eccb2008-11-04 20:29:31 -080065 return -EFAULT;
66 subd = (it.range_type >> 24) & 0xf;
67 chan = (it.range_type >> 16) & 0xff;
68
69 if (!dev->attached)
70 return -EINVAL;
71 if (subd >= dev->n_subdevices)
72 return -EINVAL;
H Hartley Sweetend08d6cf2012-09-05 18:59:44 -070073 s = &dev->subdevices[subd];
David Schleefed9eccb2008-11-04 20:29:31 -080074 if (s->range_table) {
75 lr = s->range_table;
76 } else if (s->range_table_list) {
77 if (chan >= s->n_chan)
78 return -EINVAL;
79 lr = s->range_table_list[chan];
80 } else {
81 return -EINVAL;
82 }
83
84 if (RANGE_LENGTH(it.range_type) != lr->length) {
85 DPRINTK("wrong length %d should be %d (0x%08x)\n",
86 RANGE_LENGTH(it.range_type), lr->length, it.range_type);
87 return -EINVAL;
88 }
89
90 if (copy_to_user(it.range_ptr, lr->range,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053091 sizeof(struct comedi_krange) * lr->length))
David Schleefed9eccb2008-11-04 20:29:31 -080092 return -EFAULT;
93
94 return 0;
95}
96
Bill Pemberton34c43922009-03-16 22:05:14 -040097static int aref_invalid(struct comedi_subdevice *s, unsigned int chanspec)
David Schleefed9eccb2008-11-04 20:29:31 -080098{
99 unsigned int aref;
100
Bill Pembertonb6c77752009-03-16 22:03:24 -0400101 /* disable reporting invalid arefs... maybe someday */
David Schleefed9eccb2008-11-04 20:29:31 -0800102 return 0;
103
104 aref = CR_AREF(chanspec);
105 switch (aref) {
106 case AREF_DIFF:
107 if (s->subdev_flags & SDF_DIFF)
108 return 0;
109 break;
110 case AREF_COMMON:
111 if (s->subdev_flags & SDF_COMMON)
112 return 0;
113 break;
114 case AREF_GROUND:
115 if (s->subdev_flags & SDF_GROUND)
116 return 0;
117 break;
118 case AREF_OTHER:
119 if (s->subdev_flags & SDF_OTHER)
120 return 0;
121 break;
122 default:
123 break;
124 }
125 DPRINTK("subdevice does not support aref %i", aref);
126 return 1;
127}
128
129/*
130 This function checks each element in a channel/gain list to make
131 make sure it is valid.
132*/
Greg Kroah-Hartman0fd0ca72010-05-01 12:33:17 -0700133int comedi_check_chanlist(struct comedi_subdevice *s, int n,
134 unsigned int *chanlist)
David Schleefed9eccb2008-11-04 20:29:31 -0800135{
Ian Abbott4f870fe2012-08-16 14:38:05 +0100136 struct comedi_device *dev = s->device;
David Schleefed9eccb2008-11-04 20:29:31 -0800137 int i;
138 int chan;
139
140 if (s->range_table) {
141 for (i = 0; i < n; i++)
142 if (CR_CHAN(chanlist[i]) >= s->n_chan ||
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530143 CR_RANGE(chanlist[i]) >= s->range_table->length
144 || aref_invalid(s, chanlist[i])) {
Ian Abbott4f870fe2012-08-16 14:38:05 +0100145 dev_warn(dev->class_dev,
146 "bad chanlist[%d]=0x%08x in_chan=%d range length=%d\n",
147 i, chanlist[i], s->n_chan,
148 s->range_table->length);
David Schleefed9eccb2008-11-04 20:29:31 -0800149 return -EINVAL;
150 }
151 } else if (s->range_table_list) {
152 for (i = 0; i < n; i++) {
153 chan = CR_CHAN(chanlist[i]);
154 if (chan >= s->n_chan ||
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530155 CR_RANGE(chanlist[i]) >=
156 s->range_table_list[chan]->length
157 || aref_invalid(s, chanlist[i])) {
Ian Abbott4f870fe2012-08-16 14:38:05 +0100158 dev_warn(dev->class_dev,
159 "bad chanlist[%d]=0x%08x\n",
160 i, chanlist[i]);
David Schleefed9eccb2008-11-04 20:29:31 -0800161 return -EINVAL;
162 }
163 }
164 } else {
Ian Abbott4f870fe2012-08-16 14:38:05 +0100165 dev_err(dev->class_dev, "(bug) no range type list!\n");
David Schleefed9eccb2008-11-04 20:29:31 -0800166 return -EINVAL;
167 }
168 return 0;
169}
Greg Kroah-Hartman0fd0ca72010-05-01 12:33:17 -0700170EXPORT_SYMBOL(comedi_check_chanlist);