blob: ce3a58a7a1713b7d32c43fc50ce7c0045980ccbf [file] [log] [blame]
David Schleefed9eccb2008-11-04 20:29:31 -08001/*
Ian Abbott5824ec72015-01-28 18:41:49 +00002 * 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 */
David Schleefed9eccb2008-11-04 20:29:31 -080018
Greg Kroah-Hartmanecfe20d2010-04-30 15:35:37 -070019#include <linux/uaccess.h>
Greg Kroah-Hartman3b6b25b2010-05-03 15:50:09 -070020#include "comedidev.h"
Ian Abbott3a5fa272012-06-19 10:17:44 +010021#include "comedi_internal.h"
David Schleefed9eccb2008-11-04 20:29:31 -080022
Bill Pemberton9ced1de2009-03-16 22:05:31 -040023const struct comedi_lrange range_bipolar10 = { 1, {BIP_RANGE(10)} };
H Hartley Sweeten5660e742013-04-12 10:11:54 -070024EXPORT_SYMBOL_GPL(range_bipolar10);
Mark Rankilorf0f29182010-05-03 17:39:09 +080025const struct comedi_lrange range_bipolar5 = { 1, {BIP_RANGE(5)} };
H Hartley Sweeten5660e742013-04-12 10:11:54 -070026EXPORT_SYMBOL_GPL(range_bipolar5);
Mark Rankilorf0f29182010-05-03 17:39:09 +080027const struct comedi_lrange range_bipolar2_5 = { 1, {BIP_RANGE(2.5)} };
H Hartley Sweeten5660e742013-04-12 10:11:54 -070028EXPORT_SYMBOL_GPL(range_bipolar2_5);
Mark Rankilorf0f29182010-05-03 17:39:09 +080029const struct comedi_lrange range_unipolar10 = { 1, {UNI_RANGE(10)} };
H Hartley Sweeten5660e742013-04-12 10:11:54 -070030EXPORT_SYMBOL_GPL(range_unipolar10);
Mark Rankilorf0f29182010-05-03 17:39:09 +080031const struct comedi_lrange range_unipolar5 = { 1, {UNI_RANGE(5)} };
H Hartley Sweeten5660e742013-04-12 10:11:54 -070032EXPORT_SYMBOL_GPL(range_unipolar5);
H Hartley Sweeten5f8eb722013-04-03 13:38:26 -070033const struct comedi_lrange range_unipolar2_5 = { 1, {UNI_RANGE(2.5)} };
H Hartley Sweeten5660e742013-04-12 10:11:54 -070034EXPORT_SYMBOL_GPL(range_unipolar2_5);
H Hartley Sweeten2c71c4f2013-04-03 13:40:13 -070035const struct comedi_lrange range_0_20mA = { 1, {RANGE_mA(0, 20)} };
H Hartley Sweeten5660e742013-04-12 10:11:54 -070036EXPORT_SYMBOL_GPL(range_0_20mA);
H Hartley Sweeten2c71c4f2013-04-03 13:40:13 -070037const struct comedi_lrange range_4_20mA = { 1, {RANGE_mA(4, 20)} };
H Hartley Sweeten5660e742013-04-12 10:11:54 -070038EXPORT_SYMBOL_GPL(range_4_20mA);
H Hartley Sweeten2c71c4f2013-04-03 13:40:13 -070039const struct comedi_lrange range_0_32mA = { 1, {RANGE_mA(0, 32)} };
H Hartley Sweeten5660e742013-04-12 10:11:54 -070040EXPORT_SYMBOL_GPL(range_0_32mA);
Mark Rankilorf0f29182010-05-03 17:39:09 +080041const struct comedi_lrange range_unknown = { 1, {{0, 1000000, UNIT_none} } };
H Hartley Sweeten5660e742013-04-12 10:11:54 -070042EXPORT_SYMBOL_GPL(range_unknown);
David Schleefed9eccb2008-11-04 20:29:31 -080043
44/*
Ian Abbott1a4f01b2015-01-28 18:41:48 +000045 * 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 */
Greg Kroah-Hartman3b6b25b2010-05-03 15:50:09 -070057int do_rangeinfo_ioctl(struct comedi_device *dev,
58 struct comedi_rangeinfo __user *arg)
David Schleefed9eccb2008-11-04 20:29:31 -080059{
Bill Pembertond0a353f2009-03-16 22:06:26 -040060 struct comedi_rangeinfo it;
David Schleefed9eccb2008-11-04 20:29:31 -080061 int subd, chan;
Bill Pemberton9ced1de2009-03-16 22:05:31 -040062 const struct comedi_lrange *lr;
Bill Pemberton34c43922009-03-16 22:05:14 -040063 struct comedi_subdevice *s;
David Schleefed9eccb2008-11-04 20:29:31 -080064
Bill Pembertond0a353f2009-03-16 22:06:26 -040065 if (copy_from_user(&it, arg, sizeof(struct comedi_rangeinfo)))
David Schleefed9eccb2008-11-04 20:29:31 -080066 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;
H Hartley Sweetend08d6cf2012-09-05 18:59:44 -070074 s = &dev->subdevices[subd];
David Schleefed9eccb2008-11-04 20:29:31 -080075 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) {
H Hartley Sweeten2cde4762013-11-22 10:45:58 -070086 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);
David Schleefed9eccb2008-11-04 20:29:31 -080090 return -EINVAL;
91 }
92
93 if (copy_to_user(it.range_ptr, lr->range,
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +053094 sizeof(struct comedi_krange) * lr->length))
David Schleefed9eccb2008-11-04 20:29:31 -080095 return -EFAULT;
96
97 return 0;
98}
99
H Hartley Sweeten065b0912013-07-24 10:00:45 -0700100/**
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
Ian Abbottd27da4d2015-08-05 18:13:26 +0100105 *
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 */
Greg Kroah-Hartman0fd0ca72010-05-01 12:33:17 -0700117int comedi_check_chanlist(struct comedi_subdevice *s, int n,
118 unsigned int *chanlist)
David Schleefed9eccb2008-11-04 20:29:31 -0800119{
Ian Abbott4f870fe2012-08-16 14:38:05 +0100120 struct comedi_device *dev = s->device;
H Hartley Sweeten065b0912013-07-24 10:00:45 -0700121 unsigned int chanspec;
122 int chan, range_len, i;
David Schleefed9eccb2008-11-04 20:29:31 -0800123
Fred Akersafdc37e2014-03-06 01:57:18 -0500124 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 ||
Tapasweni Pathakc6c04f02014-10-07 23:20:24 +0530134 CR_RANGE(chanspec) >= range_len) {
Fred Akersafdc37e2014-03-06 01:57:18 -0500135 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 }
David Schleefed9eccb2008-11-04 20:29:31 -0800141 return 0;
142}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700143EXPORT_SYMBOL_GPL(comedi_check_chanlist);