1 // SPDX-License-Identifier: GPL-2.0+
4 * comedi routines for voltage ranges
6 * COMEDI - Linux Control and Measurement Device Interface
7 * Copyright (C) 1997-8 David A. Schleef <ds@schleef.org>
10 #include <linux/uaccess.h>
11 #include "comedidev.h"
12 #include "comedi_internal.h"
14 const struct comedi_lrange range_bipolar10
= { 1, {BIP_RANGE(10)} };
15 EXPORT_SYMBOL_GPL(range_bipolar10
);
16 const struct comedi_lrange range_bipolar5
= { 1, {BIP_RANGE(5)} };
17 EXPORT_SYMBOL_GPL(range_bipolar5
);
18 const struct comedi_lrange range_bipolar2_5
= { 1, {BIP_RANGE(2.5)} };
19 EXPORT_SYMBOL_GPL(range_bipolar2_5
);
20 const struct comedi_lrange range_unipolar10
= { 1, {UNI_RANGE(10)} };
21 EXPORT_SYMBOL_GPL(range_unipolar10
);
22 const struct comedi_lrange range_unipolar5
= { 1, {UNI_RANGE(5)} };
23 EXPORT_SYMBOL_GPL(range_unipolar5
);
24 const struct comedi_lrange range_unipolar2_5
= { 1, {UNI_RANGE(2.5)} };
25 EXPORT_SYMBOL_GPL(range_unipolar2_5
);
26 const struct comedi_lrange range_0_20mA
= { 1, {RANGE_mA(0, 20)} };
27 EXPORT_SYMBOL_GPL(range_0_20mA
);
28 const struct comedi_lrange range_4_20mA
= { 1, {RANGE_mA(4, 20)} };
29 EXPORT_SYMBOL_GPL(range_4_20mA
);
30 const struct comedi_lrange range_0_32mA
= { 1, {RANGE_mA(0, 32)} };
31 EXPORT_SYMBOL_GPL(range_0_32mA
);
32 const struct comedi_lrange range_unknown
= { 1, {{0, 1000000, UNIT_none
} } };
33 EXPORT_SYMBOL_GPL(range_unknown
);
36 * COMEDI_RANGEINFO ioctl
40 * pointer to comedi_rangeinfo structure
43 * comedi_rangeinfo structure
46 * array of comedi_krange structures to rangeinfo->range_ptr pointer
48 int do_rangeinfo_ioctl(struct comedi_device
*dev
,
49 struct comedi_rangeinfo __user
*arg
)
51 struct comedi_rangeinfo it
;
53 const struct comedi_lrange
*lr
;
54 struct comedi_subdevice
*s
;
56 if (copy_from_user(&it
, arg
, sizeof(struct comedi_rangeinfo
)))
58 subd
= (it
.range_type
>> 24) & 0xf;
59 chan
= (it
.range_type
>> 16) & 0xff;
63 if (subd
>= dev
->n_subdevices
)
65 s
= &dev
->subdevices
[subd
];
68 } else if (s
->range_table_list
) {
69 if (chan
>= s
->n_chan
)
71 lr
= s
->range_table_list
[chan
];
76 if (RANGE_LENGTH(it
.range_type
) != lr
->length
) {
77 dev_dbg(dev
->class_dev
,
78 "wrong length %d should be %d (0x%08x)\n",
79 RANGE_LENGTH(it
.range_type
),
80 lr
->length
, it
.range_type
);
84 if (copy_to_user(it
.range_ptr
, lr
->range
,
85 sizeof(struct comedi_krange
) * lr
->length
))
92 * comedi_check_chanlist() - Validate each element in a chanlist.
93 * @s: comedi_subdevice struct
94 * @n: number of elements in the chanlist
95 * @chanlist: the chanlist to validate
97 * Each element consists of a channel number, a range index, an analog
98 * reference type and some flags, all packed into an unsigned int.
100 * This checks that the channel number and range index are supported by
101 * the comedi subdevice. It does not check whether the analog reference
102 * type and the flags are supported. Drivers that care should check those
105 * Return: %0 if all @chanlist elements are valid (success),
106 * %-EINVAL if one or more elements are invalid.
108 int comedi_check_chanlist(struct comedi_subdevice
*s
, int n
,
109 unsigned int *chanlist
)
111 struct comedi_device
*dev
= s
->device
;
112 unsigned int chanspec
;
113 int chan
, range_len
, i
;
115 for (i
= 0; i
< n
; i
++) {
116 chanspec
= chanlist
[i
];
117 chan
= CR_CHAN(chanspec
);
119 range_len
= s
->range_table
->length
;
120 else if (s
->range_table_list
&& chan
< s
->n_chan
)
121 range_len
= s
->range_table_list
[chan
]->length
;
124 if (chan
>= s
->n_chan
||
125 CR_RANGE(chanspec
) >= range_len
) {
126 dev_warn(dev
->class_dev
,
127 "bad chanlist[%d]=0x%08x chan=%d range length=%d\n",
128 i
, chanspec
, chan
, range_len
);
134 EXPORT_SYMBOL_GPL(comedi_check_chanlist
);