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