David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 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 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | |
| 22 | */ |
| 23 | |
| 24 | #include "comedidev.h" |
| 25 | #include <asm/uaccess.h> |
| 26 | |
Bill Pemberton | 9ced1de | 2009-03-16 22:05:31 -0400 | [diff] [blame] | 27 | const struct comedi_lrange range_bipolar10 = { 1, {BIP_RANGE(10)} }; |
| 28 | const struct comedi_lrange range_bipolar5 = { 1, {BIP_RANGE(5)} }; |
| 29 | const struct comedi_lrange range_bipolar2_5 = { 1, {BIP_RANGE(2.5)} }; |
| 30 | const struct comedi_lrange range_unipolar10 = { 1, {UNI_RANGE(10)} }; |
| 31 | const struct comedi_lrange range_unipolar5 = { 1, {UNI_RANGE(5)} }; |
| 32 | const struct comedi_lrange range_unknown = { 1, {{0, 1000000, UNIT_none}} }; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | COMEDI_RANGEINFO |
| 36 | range information ioctl |
| 37 | |
| 38 | arg: |
| 39 | pointer to rangeinfo structure |
| 40 | |
| 41 | reads: |
| 42 | range info structure |
| 43 | |
| 44 | writes: |
Bill Pemberton | 1f6325d | 2009-03-16 22:06:31 -0400 | [diff] [blame] | 45 | n struct comedi_krange structures to rangeinfo->range_ptr |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 46 | */ |
Bill Pemberton | d0a353f | 2009-03-16 22:06:26 -0400 | [diff] [blame] | 47 | int do_rangeinfo_ioctl(struct comedi_device *dev, struct comedi_rangeinfo *arg) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 48 | { |
Bill Pemberton | d0a353f | 2009-03-16 22:06:26 -0400 | [diff] [blame] | 49 | struct comedi_rangeinfo it; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 50 | int subd, chan; |
Bill Pemberton | 9ced1de | 2009-03-16 22:05:31 -0400 | [diff] [blame] | 51 | const struct comedi_lrange *lr; |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 52 | struct comedi_subdevice *s; |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 53 | |
Bill Pemberton | d0a353f | 2009-03-16 22:06:26 -0400 | [diff] [blame] | 54 | if (copy_from_user(&it, arg, sizeof(struct comedi_rangeinfo))) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 55 | return -EFAULT; |
| 56 | subd = (it.range_type >> 24) & 0xf; |
| 57 | chan = (it.range_type >> 16) & 0xff; |
| 58 | |
| 59 | if (!dev->attached) |
| 60 | return -EINVAL; |
| 61 | if (subd >= dev->n_subdevices) |
| 62 | return -EINVAL; |
| 63 | s = dev->subdevices + subd; |
| 64 | if (s->range_table) { |
| 65 | lr = s->range_table; |
| 66 | } else if (s->range_table_list) { |
| 67 | if (chan >= s->n_chan) |
| 68 | return -EINVAL; |
| 69 | lr = s->range_table_list[chan]; |
| 70 | } else { |
| 71 | return -EINVAL; |
| 72 | } |
| 73 | |
| 74 | if (RANGE_LENGTH(it.range_type) != lr->length) { |
| 75 | DPRINTK("wrong length %d should be %d (0x%08x)\n", |
| 76 | RANGE_LENGTH(it.range_type), lr->length, it.range_type); |
| 77 | return -EINVAL; |
| 78 | } |
| 79 | |
| 80 | if (copy_to_user(it.range_ptr, lr->range, |
Bill Pemberton | 1f6325d | 2009-03-16 22:06:31 -0400 | [diff] [blame] | 81 | sizeof(struct comedi_krange) * lr->length)) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 82 | return -EFAULT; |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 87 | static int aref_invalid(struct comedi_subdevice *s, unsigned int chanspec) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 88 | { |
| 89 | unsigned int aref; |
| 90 | |
Bill Pemberton | b6c7775 | 2009-03-16 22:03:24 -0400 | [diff] [blame] | 91 | /* disable reporting invalid arefs... maybe someday */ |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 92 | return 0; |
| 93 | |
| 94 | aref = CR_AREF(chanspec); |
| 95 | switch (aref) { |
| 96 | case AREF_DIFF: |
| 97 | if (s->subdev_flags & SDF_DIFF) |
| 98 | return 0; |
| 99 | break; |
| 100 | case AREF_COMMON: |
| 101 | if (s->subdev_flags & SDF_COMMON) |
| 102 | return 0; |
| 103 | break; |
| 104 | case AREF_GROUND: |
| 105 | if (s->subdev_flags & SDF_GROUND) |
| 106 | return 0; |
| 107 | break; |
| 108 | case AREF_OTHER: |
| 109 | if (s->subdev_flags & SDF_OTHER) |
| 110 | return 0; |
| 111 | break; |
| 112 | default: |
| 113 | break; |
| 114 | } |
| 115 | DPRINTK("subdevice does not support aref %i", aref); |
| 116 | return 1; |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | This function checks each element in a channel/gain list to make |
| 121 | make sure it is valid. |
| 122 | */ |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 123 | int check_chanlist(struct comedi_subdevice *s, int n, unsigned int *chanlist) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 124 | { |
| 125 | int i; |
| 126 | int chan; |
| 127 | |
| 128 | if (s->range_table) { |
| 129 | for (i = 0; i < n; i++) |
| 130 | if (CR_CHAN(chanlist[i]) >= s->n_chan || |
| 131 | CR_RANGE(chanlist[i]) >= s->range_table->length |
| 132 | || aref_invalid(s, chanlist[i])) { |
| 133 | rt_printk |
| 134 | ("bad chanlist[%d]=0x%08x n_chan=%d range length=%d\n", |
| 135 | i, chanlist[i], s->n_chan, |
| 136 | s->range_table->length); |
| 137 | #if 0 |
Bill Pemberton | 82675f3 | 2009-03-16 22:04:23 -0400 | [diff] [blame] | 138 | for (i = 0; i < n; i++) |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 139 | printk("[%d]=0x%08x\n", i, chanlist[i]); |
David Schleef | ed9eccb | 2008-11-04 20:29:31 -0800 | [diff] [blame] | 140 | #endif |
| 141 | return -EINVAL; |
| 142 | } |
| 143 | } else if (s->range_table_list) { |
| 144 | for (i = 0; i < n; i++) { |
| 145 | chan = CR_CHAN(chanlist[i]); |
| 146 | if (chan >= s->n_chan || |
| 147 | CR_RANGE(chanlist[i]) >= |
| 148 | s->range_table_list[chan]->length |
| 149 | || aref_invalid(s, chanlist[i])) { |
| 150 | rt_printk("bad chanlist[%d]=0x%08x\n", i, |
| 151 | chanlist[i]); |
| 152 | return -EINVAL; |
| 153 | } |
| 154 | } |
| 155 | } else { |
| 156 | rt_printk("comedi: (bug) no range type list!\n"); |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | return 0; |
| 160 | } |