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