staging: comedi: drivers: use comedi_dio_update_state() for simple cases
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / comedi / range.c
CommitLineData
ed9eccbe
DS
1/*
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
ecfe20db 24#include <linux/uaccess.h>
3b6b25b5 25#include "comedidev.h"
3a5fa275 26#include "comedi_internal.h"
ed9eccbe 27
9ced1de6 28const struct comedi_lrange range_bipolar10 = { 1, {BIP_RANGE(10)} };
5660e742 29EXPORT_SYMBOL_GPL(range_bipolar10);
f0f29184 30const struct comedi_lrange range_bipolar5 = { 1, {BIP_RANGE(5)} };
5660e742 31EXPORT_SYMBOL_GPL(range_bipolar5);
f0f29184 32const struct comedi_lrange range_bipolar2_5 = { 1, {BIP_RANGE(2.5)} };
5660e742 33EXPORT_SYMBOL_GPL(range_bipolar2_5);
f0f29184 34const struct comedi_lrange range_unipolar10 = { 1, {UNI_RANGE(10)} };
5660e742 35EXPORT_SYMBOL_GPL(range_unipolar10);
f0f29184 36const struct comedi_lrange range_unipolar5 = { 1, {UNI_RANGE(5)} };
5660e742 37EXPORT_SYMBOL_GPL(range_unipolar5);
5f8eb72c 38const struct comedi_lrange range_unipolar2_5 = { 1, {UNI_RANGE(2.5)} };
5660e742 39EXPORT_SYMBOL_GPL(range_unipolar2_5);
2c71c4ff 40const struct comedi_lrange range_0_20mA = { 1, {RANGE_mA(0, 20)} };
5660e742 41EXPORT_SYMBOL_GPL(range_0_20mA);
2c71c4ff 42const struct comedi_lrange range_4_20mA = { 1, {RANGE_mA(4, 20)} };
5660e742 43EXPORT_SYMBOL_GPL(range_4_20mA);
2c71c4ff 44const struct comedi_lrange range_0_32mA = { 1, {RANGE_mA(0, 32)} };
5660e742 45EXPORT_SYMBOL_GPL(range_0_32mA);
f0f29184 46const struct comedi_lrange range_unknown = { 1, {{0, 1000000, UNIT_none} } };
5660e742 47EXPORT_SYMBOL_GPL(range_unknown);
ed9eccbe
DS
48
49/*
356cdbcb 50 COMEDI_RANGEINFO
ed9eccbe
DS
51 range information ioctl
52
53 arg:
54 pointer to rangeinfo structure
55
56 reads:
57 range info structure
58
59 writes:
1f6325d6 60 n struct comedi_krange structures to rangeinfo->range_ptr
ed9eccbe 61*/
3b6b25b5
GKH
62int do_rangeinfo_ioctl(struct comedi_device *dev,
63 struct comedi_rangeinfo __user *arg)
ed9eccbe 64{
d0a353f6 65 struct comedi_rangeinfo it;
ed9eccbe 66 int subd, chan;
9ced1de6 67 const struct comedi_lrange *lr;
34c43922 68 struct comedi_subdevice *s;
ed9eccbe 69
d0a353f6 70 if (copy_from_user(&it, arg, sizeof(struct comedi_rangeinfo)))
ed9eccbe
DS
71 return -EFAULT;
72 subd = (it.range_type >> 24) & 0xf;
73 chan = (it.range_type >> 16) & 0xff;
74
75 if (!dev->attached)
76 return -EINVAL;
77 if (subd >= dev->n_subdevices)
78 return -EINVAL;
d08d6cfe 79 s = &dev->subdevices[subd];
ed9eccbe
DS
80 if (s->range_table) {
81 lr = s->range_table;
82 } else if (s->range_table_list) {
83 if (chan >= s->n_chan)
84 return -EINVAL;
85 lr = s->range_table_list[chan];
86 } else {
87 return -EINVAL;
88 }
89
90 if (RANGE_LENGTH(it.range_type) != lr->length) {
91 DPRINTK("wrong length %d should be %d (0x%08x)\n",
92 RANGE_LENGTH(it.range_type), lr->length, it.range_type);
93 return -EINVAL;
94 }
95
96 if (copy_to_user(it.range_ptr, lr->range,
0a85b6f0 97 sizeof(struct comedi_krange) * lr->length))
ed9eccbe
DS
98 return -EFAULT;
99
100 return 0;
101}
102
34c43922 103static int aref_invalid(struct comedi_subdevice *s, unsigned int chanspec)
ed9eccbe
DS
104{
105 unsigned int aref;
106
b6c77757 107 /* disable reporting invalid arefs... maybe someday */
ed9eccbe
DS
108 return 0;
109
110 aref = CR_AREF(chanspec);
111 switch (aref) {
112 case AREF_DIFF:
113 if (s->subdev_flags & SDF_DIFF)
114 return 0;
115 break;
116 case AREF_COMMON:
117 if (s->subdev_flags & SDF_COMMON)
118 return 0;
119 break;
120 case AREF_GROUND:
121 if (s->subdev_flags & SDF_GROUND)
122 return 0;
123 break;
124 case AREF_OTHER:
125 if (s->subdev_flags & SDF_OTHER)
126 return 0;
127 break;
128 default:
129 break;
130 }
131 DPRINTK("subdevice does not support aref %i", aref);
132 return 1;
133}
134
135/*
136 This function checks each element in a channel/gain list to make
137 make sure it is valid.
138*/
0fd0ca75
GKH
139int comedi_check_chanlist(struct comedi_subdevice *s, int n,
140 unsigned int *chanlist)
ed9eccbe 141{
4f870fe6 142 struct comedi_device *dev = s->device;
ed9eccbe
DS
143 int i;
144 int chan;
145
146 if (s->range_table) {
147 for (i = 0; i < n; i++)
148 if (CR_CHAN(chanlist[i]) >= s->n_chan ||
0a85b6f0
MT
149 CR_RANGE(chanlist[i]) >= s->range_table->length
150 || aref_invalid(s, chanlist[i])) {
4f870fe6
IA
151 dev_warn(dev->class_dev,
152 "bad chanlist[%d]=0x%08x in_chan=%d range length=%d\n",
153 i, chanlist[i], s->n_chan,
154 s->range_table->length);
ed9eccbe
DS
155 return -EINVAL;
156 }
157 } else if (s->range_table_list) {
158 for (i = 0; i < n; i++) {
159 chan = CR_CHAN(chanlist[i]);
160 if (chan >= s->n_chan ||
0a85b6f0
MT
161 CR_RANGE(chanlist[i]) >=
162 s->range_table_list[chan]->length
163 || aref_invalid(s, chanlist[i])) {
4f870fe6
IA
164 dev_warn(dev->class_dev,
165 "bad chanlist[%d]=0x%08x\n",
166 i, chanlist[i]);
ed9eccbe
DS
167 return -EINVAL;
168 }
169 }
170 } else {
4f870fe6 171 dev_err(dev->class_dev, "(bug) no range type list!\n");
ed9eccbe
DS
172 return -EINVAL;
173 }
174 return 0;
175}
5660e742 176EXPORT_SYMBOL_GPL(comedi_check_chanlist);