Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
23const struct comedi_lrange range_bipolar10 = { 1, {BIP_RANGE(10)} };
24EXPORT_SYMBOL_GPL(range_bipolar10);
25const struct comedi_lrange range_bipolar5 = { 1, {BIP_RANGE(5)} };
26EXPORT_SYMBOL_GPL(range_bipolar5);
27const struct comedi_lrange range_bipolar2_5 = { 1, {BIP_RANGE(2.5)} };
28EXPORT_SYMBOL_GPL(range_bipolar2_5);
29const struct comedi_lrange range_unipolar10 = { 1, {UNI_RANGE(10)} };
30EXPORT_SYMBOL_GPL(range_unipolar10);
31const struct comedi_lrange range_unipolar5 = { 1, {UNI_RANGE(5)} };
32EXPORT_SYMBOL_GPL(range_unipolar5);
33const struct comedi_lrange range_unipolar2_5 = { 1, {UNI_RANGE(2.5)} };
34EXPORT_SYMBOL_GPL(range_unipolar2_5);
35const struct comedi_lrange range_0_20mA = { 1, {RANGE_mA(0, 20)} };
36EXPORT_SYMBOL_GPL(range_0_20mA);
37const struct comedi_lrange range_4_20mA = { 1, {RANGE_mA(4, 20)} };
38EXPORT_SYMBOL_GPL(range_4_20mA);
39const struct comedi_lrange range_0_32mA = { 1, {RANGE_mA(0, 32)} };
40EXPORT_SYMBOL_GPL(range_0_32mA);
41const struct comedi_lrange range_unknown = { 1, {{0, 1000000, UNIT_none} } };
42EXPORT_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*/
57int 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 DPRINTK("wrong length %d should be %d (0x%08x)\n",
87 RANGE_LENGTH(it.range_type), lr->length, it.range_type);
88 return -EINVAL;
89 }
90
91 if (copy_to_user(it.range_ptr, lr->range,
92 sizeof(struct comedi_krange) * lr->length))
93 return -EFAULT;
94
95 return 0;
96}
97
98static int aref_invalid(struct comedi_subdevice *s, unsigned int chanspec)
99{
100 unsigned int aref;
101
102 /* disable reporting invalid arefs... maybe someday */
103 return 0;
104
105 aref = CR_AREF(chanspec);
106 switch (aref) {
107 case AREF_DIFF:
108 if (s->subdev_flags & SDF_DIFF)
109 return 0;
110 break;
111 case AREF_COMMON:
112 if (s->subdev_flags & SDF_COMMON)
113 return 0;
114 break;
115 case AREF_GROUND:
116 if (s->subdev_flags & SDF_GROUND)
117 return 0;
118 break;
119 case AREF_OTHER:
120 if (s->subdev_flags & SDF_OTHER)
121 return 0;
122 break;
123 default:
124 break;
125 }
126 DPRINTK("subdevice does not support aref %i", aref);
127 return 1;
128}
129
130/*
131 This function checks each element in a channel/gain list to make
132 make sure it is valid.
133*/
134int comedi_check_chanlist(struct comedi_subdevice *s, int n,
135 unsigned int *chanlist)
136{
137 struct comedi_device *dev = s->device;
138 int i;
139 int chan;
140
141 if (s->range_table) {
142 for (i = 0; i < n; i++)
143 if (CR_CHAN(chanlist[i]) >= s->n_chan ||
144 CR_RANGE(chanlist[i]) >= s->range_table->length
145 || aref_invalid(s, chanlist[i])) {
146 dev_warn(dev->class_dev,
147 "bad chanlist[%d]=0x%08x in_chan=%d range length=%d\n",
148 i, chanlist[i], s->n_chan,
149 s->range_table->length);
150 return -EINVAL;
151 }
152 } else if (s->range_table_list) {
153 for (i = 0; i < n; i++) {
154 chan = CR_CHAN(chanlist[i]);
155 if (chan >= s->n_chan ||
156 CR_RANGE(chanlist[i]) >=
157 s->range_table_list[chan]->length
158 || aref_invalid(s, chanlist[i])) {
159 dev_warn(dev->class_dev,
160 "bad chanlist[%d]=0x%08x\n",
161 i, chanlist[i]);
162 return -EINVAL;
163 }
164 }
165 } else {
166 dev_err(dev->class_dev, "(bug) no range type list!\n");
167 return -EINVAL;
168 }
169 return 0;
170}
171EXPORT_SYMBOL_GPL(comedi_check_chanlist);