]>
Commit | Line | Data |
---|---|---|
ed9eccbe | 1 | /* |
5824ec7f IA |
2 | * comedi/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 | */ | |
ed9eccbe | 18 | |
ecfe20db | 19 | #include <linux/uaccess.h> |
3b6b25b5 | 20 | #include "comedidev.h" |
3a5fa275 | 21 | #include "comedi_internal.h" |
ed9eccbe | 22 | |
9ced1de6 | 23 | const struct comedi_lrange range_bipolar10 = { 1, {BIP_RANGE(10)} }; |
5660e742 | 24 | EXPORT_SYMBOL_GPL(range_bipolar10); |
f0f29184 | 25 | const struct comedi_lrange range_bipolar5 = { 1, {BIP_RANGE(5)} }; |
5660e742 | 26 | EXPORT_SYMBOL_GPL(range_bipolar5); |
f0f29184 | 27 | const struct comedi_lrange range_bipolar2_5 = { 1, {BIP_RANGE(2.5)} }; |
5660e742 | 28 | EXPORT_SYMBOL_GPL(range_bipolar2_5); |
f0f29184 | 29 | const struct comedi_lrange range_unipolar10 = { 1, {UNI_RANGE(10)} }; |
5660e742 | 30 | EXPORT_SYMBOL_GPL(range_unipolar10); |
f0f29184 | 31 | const struct comedi_lrange range_unipolar5 = { 1, {UNI_RANGE(5)} }; |
5660e742 | 32 | EXPORT_SYMBOL_GPL(range_unipolar5); |
5f8eb72c | 33 | const struct comedi_lrange range_unipolar2_5 = { 1, {UNI_RANGE(2.5)} }; |
5660e742 | 34 | EXPORT_SYMBOL_GPL(range_unipolar2_5); |
2c71c4ff | 35 | const struct comedi_lrange range_0_20mA = { 1, {RANGE_mA(0, 20)} }; |
5660e742 | 36 | EXPORT_SYMBOL_GPL(range_0_20mA); |
2c71c4ff | 37 | const struct comedi_lrange range_4_20mA = { 1, {RANGE_mA(4, 20)} }; |
5660e742 | 38 | EXPORT_SYMBOL_GPL(range_4_20mA); |
2c71c4ff | 39 | const struct comedi_lrange range_0_32mA = { 1, {RANGE_mA(0, 32)} }; |
5660e742 | 40 | EXPORT_SYMBOL_GPL(range_0_32mA); |
f0f29184 | 41 | const struct comedi_lrange range_unknown = { 1, {{0, 1000000, UNIT_none} } }; |
5660e742 | 42 | EXPORT_SYMBOL_GPL(range_unknown); |
ed9eccbe DS |
43 | |
44 | /* | |
1a4f01b7 IA |
45 | * COMEDI_RANGEINFO ioctl |
46 | * range information | |
47 | * | |
48 | * arg: | |
49 | * pointer to comedi_rangeinfo structure | |
50 | * | |
51 | * reads: | |
52 | * comedi_rangeinfo structure | |
53 | * | |
54 | * writes: | |
55 | * array of comedi_krange structures to rangeinfo->range_ptr pointer | |
56 | */ | |
3b6b25b5 GKH |
57 | int do_rangeinfo_ioctl(struct comedi_device *dev, |
58 | struct comedi_rangeinfo __user *arg) | |
ed9eccbe | 59 | { |
d0a353f6 | 60 | struct comedi_rangeinfo it; |
ed9eccbe | 61 | int subd, chan; |
9ced1de6 | 62 | const struct comedi_lrange *lr; |
34c43922 | 63 | struct comedi_subdevice *s; |
ed9eccbe | 64 | |
d0a353f6 | 65 | if (copy_from_user(&it, arg, sizeof(struct comedi_rangeinfo))) |
ed9eccbe DS |
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; | |
d08d6cfe | 74 | s = &dev->subdevices[subd]; |
ed9eccbe DS |
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) { | |
2cde476d HS |
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); | |
ed9eccbe DS |
90 | return -EINVAL; |
91 | } | |
92 | ||
93 | if (copy_to_user(it.range_ptr, lr->range, | |
0a85b6f0 | 94 | sizeof(struct comedi_krange) * lr->length)) |
ed9eccbe DS |
95 | return -EFAULT; |
96 | ||
97 | return 0; | |
98 | } | |
99 | ||
065b0912 HS |
100 | /** |
101 | * comedi_check_chanlist() - Validate each element in a chanlist. | |
102 | * @s: comedi_subdevice struct | |
103 | * @n: number of elements in the chanlist | |
104 | * @chanlist: the chanlist to validate | |
d27da4da IA |
105 | * |
106 | * Each element consists of a channel number, a range index, an analog | |
107 | * reference type and some flags, all packed into an unsigned int. | |
108 | * | |
109 | * This checks that the channel number and range index are supported by | |
110 | * the comedi subdevice. It does not check whether the analog reference | |
111 | * type and the flags are supported. Drivers that care should check those | |
112 | * themselves. | |
113 | * | |
114 | * Return: %0 if all @chanlist elements are valid (success), | |
115 | * %-EINVAL if one or more elements are invalid. | |
116 | */ | |
0fd0ca75 GKH |
117 | int comedi_check_chanlist(struct comedi_subdevice *s, int n, |
118 | unsigned int *chanlist) | |
ed9eccbe | 119 | { |
4f870fe6 | 120 | struct comedi_device *dev = s->device; |
065b0912 HS |
121 | unsigned int chanspec; |
122 | int chan, range_len, i; | |
ed9eccbe | 123 | |
afdc37ee FA |
124 | for (i = 0; i < n; i++) { |
125 | chanspec = chanlist[i]; | |
126 | chan = CR_CHAN(chanspec); | |
127 | if (s->range_table) | |
128 | range_len = s->range_table->length; | |
129 | else if (s->range_table_list && chan < s->n_chan) | |
130 | range_len = s->range_table_list[chan]->length; | |
131 | else | |
132 | range_len = 0; | |
133 | if (chan >= s->n_chan || | |
c6c04f05 | 134 | CR_RANGE(chanspec) >= range_len) { |
afdc37ee FA |
135 | dev_warn(dev->class_dev, |
136 | "bad chanlist[%d]=0x%08x chan=%d range length=%d\n", | |
137 | i, chanspec, chan, range_len); | |
138 | return -EINVAL; | |
139 | } | |
140 | } | |
ed9eccbe DS |
141 | return 0; |
142 | } | |
5660e742 | 143 | EXPORT_SYMBOL_GPL(comedi_check_chanlist); |