blob: 8f43cf88d784bf3ecaf9a0d0f98438868469d2c4 [file] [log] [blame]
Greg Kroah-Hartmane184e2b2017-11-07 14:58:43 +01001// SPDX-License-Identifier: GPL-2.0+
David Schleefed9eccb2008-11-04 20:29:31 -08002/*
Ian Abbott5824ec72015-01-28 18:41:49 +00003 * comedi/range.c
4 * comedi routines for voltage ranges
5 *
6 * COMEDI - Linux Control and Measurement Device Interface
7 * Copyright (C) 1997-8 David A. Schleef <ds@schleef.org>
Ian Abbott5824ec72015-01-28 18:41:49 +00008 */
David Schleefed9eccb2008-11-04 20:29:31 -08009
Greg Kroah-Hartmanecfe20d2010-04-30 15:35:37 -070010#include <linux/uaccess.h>
Ian Abbottdf0e68c12021-11-17 12:05:59 +000011#include <linux/comedi/comedidev.h>
Ian Abbott3a5fa272012-06-19 10:17:44 +010012#include "comedi_internal.h"
David Schleefed9eccb2008-11-04 20:29:31 -080013
Bill Pemberton9ced1de2009-03-16 22:05:31 -040014const struct comedi_lrange range_bipolar10 = { 1, {BIP_RANGE(10)} };
H Hartley Sweeten5660e742013-04-12 10:11:54 -070015EXPORT_SYMBOL_GPL(range_bipolar10);
Mark Rankilorf0f29182010-05-03 17:39:09 +080016const struct comedi_lrange range_bipolar5 = { 1, {BIP_RANGE(5)} };
H Hartley Sweeten5660e742013-04-12 10:11:54 -070017EXPORT_SYMBOL_GPL(range_bipolar5);
Mark Rankilorf0f29182010-05-03 17:39:09 +080018const struct comedi_lrange range_bipolar2_5 = { 1, {BIP_RANGE(2.5)} };
H Hartley Sweeten5660e742013-04-12 10:11:54 -070019EXPORT_SYMBOL_GPL(range_bipolar2_5);
Mark Rankilorf0f29182010-05-03 17:39:09 +080020const struct comedi_lrange range_unipolar10 = { 1, {UNI_RANGE(10)} };
H Hartley Sweeten5660e742013-04-12 10:11:54 -070021EXPORT_SYMBOL_GPL(range_unipolar10);
Mark Rankilorf0f29182010-05-03 17:39:09 +080022const struct comedi_lrange range_unipolar5 = { 1, {UNI_RANGE(5)} };
H Hartley Sweeten5660e742013-04-12 10:11:54 -070023EXPORT_SYMBOL_GPL(range_unipolar5);
H Hartley Sweeten5f8eb722013-04-03 13:38:26 -070024const struct comedi_lrange range_unipolar2_5 = { 1, {UNI_RANGE(2.5)} };
H Hartley Sweeten5660e742013-04-12 10:11:54 -070025EXPORT_SYMBOL_GPL(range_unipolar2_5);
H Hartley Sweeten2c71c4f2013-04-03 13:40:13 -070026const struct comedi_lrange range_0_20mA = { 1, {RANGE_mA(0, 20)} };
H Hartley Sweeten5660e742013-04-12 10:11:54 -070027EXPORT_SYMBOL_GPL(range_0_20mA);
H Hartley Sweeten2c71c4f2013-04-03 13:40:13 -070028const struct comedi_lrange range_4_20mA = { 1, {RANGE_mA(4, 20)} };
H Hartley Sweeten5660e742013-04-12 10:11:54 -070029EXPORT_SYMBOL_GPL(range_4_20mA);
H Hartley Sweeten2c71c4f2013-04-03 13:40:13 -070030const struct comedi_lrange range_0_32mA = { 1, {RANGE_mA(0, 32)} };
H Hartley Sweeten5660e742013-04-12 10:11:54 -070031EXPORT_SYMBOL_GPL(range_0_32mA);
Mark Rankilorf0f29182010-05-03 17:39:09 +080032const struct comedi_lrange range_unknown = { 1, {{0, 1000000, UNIT_none} } };
H Hartley Sweeten5660e742013-04-12 10:11:54 -070033EXPORT_SYMBOL_GPL(range_unknown);
David Schleefed9eccb2008-11-04 20:29:31 -080034
35/*
Ian Abbott1a4f01b2015-01-28 18:41:48 +000036 * COMEDI_RANGEINFO ioctl
37 * range information
38 *
39 * arg:
40 * pointer to comedi_rangeinfo structure
41 *
42 * reads:
43 * comedi_rangeinfo structure
44 *
45 * writes:
46 * array of comedi_krange structures to rangeinfo->range_ptr pointer
47 */
Greg Kroah-Hartman3b6b25b2010-05-03 15:50:09 -070048int do_rangeinfo_ioctl(struct comedi_device *dev,
Al Viro38813872020-04-25 18:44:30 -040049 struct comedi_rangeinfo *it)
David Schleefed9eccb2008-11-04 20:29:31 -080050{
David Schleefed9eccb2008-11-04 20:29:31 -080051 int subd, chan;
Bill Pemberton9ced1de2009-03-16 22:05:31 -040052 const struct comedi_lrange *lr;
Bill Pemberton34c43922009-03-16 22:05:14 -040053 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -080054
Al Viro38813872020-04-25 18:44:30 -040055 subd = (it->range_type >> 24) & 0xf;
56 chan = (it->range_type >> 16) & 0xff;
David Schleefed9eccb2008-11-04 20:29:31 -080057
58 if (!dev->attached)
59 return -EINVAL;
60 if (subd >= dev->n_subdevices)
61 return -EINVAL;
H Hartley Sweetend08d6cf2012-09-05 18:59:44 -070062 s = &dev->subdevices[subd];
David Schleefed9eccb2008-11-04 20:29:31 -080063 if (s->range_table) {
64 lr = s->range_table;
65 } else if (s->range_table_list) {
66 if (chan >= s->n_chan)
67 return -EINVAL;
68 lr = s->range_table_list[chan];
69 } else {
70 return -EINVAL;
71 }
72
Al Viro38813872020-04-25 18:44:30 -040073 if (RANGE_LENGTH(it->range_type) != lr->length) {
H Hartley Sweeten2cde4762013-11-22 10:45:58 -070074 dev_dbg(dev->class_dev,
75 "wrong length %d should be %d (0x%08x)\n",
Al Viro38813872020-04-25 18:44:30 -040076 RANGE_LENGTH(it->range_type),
77 lr->length, it->range_type);
David Schleefed9eccb2008-11-04 20:29:31 -080078 return -EINVAL;
79 }
80
Al Viro38813872020-04-25 18:44:30 -040081 if (copy_to_user(it->range_ptr, lr->range,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053082 sizeof(struct comedi_krange) * lr->length))
David Schleefed9eccb2008-11-04 20:29:31 -080083 return -EFAULT;
84
85 return 0;
86}
87
H Hartley Sweeten065b0912013-07-24 10:00:45 -070088/**
89 * comedi_check_chanlist() - Validate each element in a chanlist.
90 * @s: comedi_subdevice struct
91 * @n: number of elements in the chanlist
92 * @chanlist: the chanlist to validate
Ian Abbottd27da4d2015-08-05 18:13:26 +010093 *
94 * Each element consists of a channel number, a range index, an analog
95 * reference type and some flags, all packed into an unsigned int.
96 *
97 * This checks that the channel number and range index are supported by
98 * the comedi subdevice. It does not check whether the analog reference
99 * type and the flags are supported. Drivers that care should check those
100 * themselves.
101 *
102 * Return: %0 if all @chanlist elements are valid (success),
103 * %-EINVAL if one or more elements are invalid.
104 */
Greg Kroah-Hartman0fd0ca72010-05-01 12:33:17 -0700105int comedi_check_chanlist(struct comedi_subdevice *s, int n,
106 unsigned int *chanlist)
David Schleefed9eccb2008-11-04 20:29:31 -0800107{
Ian Abbott4f870fe2012-08-16 14:38:05 +0100108 struct comedi_device *dev = s->device;
H Hartley Sweeten065b0912013-07-24 10:00:45 -0700109 unsigned int chanspec;
110 int chan, range_len, i;
David Schleefed9eccb2008-11-04 20:29:31 -0800111
Fred Akersafdc37e2014-03-06 01:57:18 -0500112 for (i = 0; i < n; i++) {
113 chanspec = chanlist[i];
114 chan = CR_CHAN(chanspec);
115 if (s->range_table)
116 range_len = s->range_table->length;
117 else if (s->range_table_list && chan < s->n_chan)
118 range_len = s->range_table_list[chan]->length;
119 else
120 range_len = 0;
121 if (chan >= s->n_chan ||
Tapasweni Pathakc6c04f02014-10-07 23:20:24 +0530122 CR_RANGE(chanspec) >= range_len) {
Fred Akersafdc37e2014-03-06 01:57:18 -0500123 dev_warn(dev->class_dev,
124 "bad chanlist[%d]=0x%08x chan=%d range length=%d\n",
125 i, chanspec, chan, range_len);
126 return -EINVAL;
127 }
128 }
David Schleefed9eccb2008-11-04 20:29:31 -0800129 return 0;
130}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700131EXPORT_SYMBOL_GPL(comedi_check_chanlist);