David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | kcomedilib/kcomedilib.c |
| 3 | a comedlib interface for kernel modules |
| 4 | |
| 5 | COMEDI - Linux Control and Measurement Device Interface |
| 6 | Copyright (C) 1997-2000 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. |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 17 | */ |
| 18 | |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 19 | #include <linux/module.h> |
| 20 | |
| 21 | #include <linux/errno.h> |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/sched.h> |
| 24 | #include <linux/fcntl.h> |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 25 | #include <linux/mm.h> |
Greg Kroah-Hartman | 7382e57 | 2010-05-01 13:08:06 -0700 | [diff] [blame] | 26 | #include <linux/io.h> |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 27 | |
| 28 | #include "../comedi.h" |
| 29 | #include "../comedilib.h" |
| 30 | #include "../comedidev.h" |
| 31 | |
| 32 | MODULE_AUTHOR("David Schleef <ds@schleef.org>"); |
| 33 | MODULE_DESCRIPTION("Comedi kernel library"); |
| 34 | MODULE_LICENSE("GPL"); |
| 35 | |
Greg Kroah-Hartman | 472dfe7 | 2010-05-03 15:01:50 -0700 | [diff] [blame] | 36 | struct comedi_device *comedi_open(const char *filename) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 37 | { |
Ian Abbott | 799a66b | 2013-11-08 15:03:38 +0000 | [diff] [blame] | 38 | struct comedi_device *dev, *retval = NULL; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 39 | unsigned int minor; |
| 40 | |
| 41 | if (strncmp(filename, "/dev/comedi", 11) != 0) |
| 42 | return NULL; |
| 43 | |
Chase Southwood | e6bed03 | 2014-02-14 21:59:40 -0600 | [diff] [blame] | 44 | if (kstrtouint(filename + 11, 0, &minor)) |
| 45 | return NULL; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 46 | |
| 47 | if (minor >= COMEDI_NUM_BOARD_MINORS) |
| 48 | return NULL; |
| 49 | |
Ian Abbott | 799a66b | 2013-11-08 15:03:38 +0000 | [diff] [blame] | 50 | dev = comedi_dev_get_from_minor(minor); |
| 51 | if (!dev) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 52 | return NULL; |
| 53 | |
Ian Abbott | 69e2387f | 2013-11-08 15:03:39 +0000 | [diff] [blame] | 54 | down_read(&dev->attach_lock); |
| 55 | if (dev->attached) |
Ian Abbott | 799a66b | 2013-11-08 15:03:38 +0000 | [diff] [blame] | 56 | retval = dev; |
| 57 | else |
| 58 | retval = NULL; |
Ian Abbott | 69e2387f | 2013-11-08 15:03:39 +0000 | [diff] [blame] | 59 | up_read(&dev->attach_lock); |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 60 | |
H Hartley Sweeten | 4c36fdd | 2015-03-04 12:15:41 -0700 | [diff] [blame] | 61 | if (!retval) |
Ian Abbott | 799a66b | 2013-11-08 15:03:38 +0000 | [diff] [blame] | 62 | comedi_dev_put(dev); |
| 63 | |
| 64 | return retval; |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 65 | } |
H Hartley Sweeten | 5660e74 | 2013-04-12 10:11:54 -0700 | [diff] [blame] | 66 | EXPORT_SYMBOL_GPL(comedi_open); |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 67 | |
Ian Abbott | 69e2387f | 2013-11-08 15:03:39 +0000 | [diff] [blame] | 68 | int comedi_close(struct comedi_device *dev) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 69 | { |
Ian Abbott | 799a66b | 2013-11-08 15:03:38 +0000 | [diff] [blame] | 70 | comedi_dev_put(dev); |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 71 | return 0; |
| 72 | } |
H Hartley Sweeten | 5660e74 | 2013-04-12 10:11:54 -0700 | [diff] [blame] | 73 | EXPORT_SYMBOL_GPL(comedi_close); |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 74 | |
H Hartley Sweeten | 1f5cc35 | 2012-09-19 17:26:53 -0700 | [diff] [blame] | 75 | static int comedi_do_insn(struct comedi_device *dev, |
| 76 | struct comedi_insn *insn, |
| 77 | unsigned int *data) |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 78 | { |
Bill Pemberton | 34c4392 | 2009-03-16 22:05:14 -0400 | [diff] [blame] | 79 | struct comedi_subdevice *s; |
Ian Abbott | 69e2387f | 2013-11-08 15:03:39 +0000 | [diff] [blame] | 80 | int ret; |
| 81 | |
| 82 | mutex_lock(&dev->mutex); |
| 83 | |
| 84 | if (!dev->attached) { |
| 85 | ret = -EINVAL; |
| 86 | goto error; |
| 87 | } |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 88 | |
Greg Kroah-Hartman | 3781bc5 | 2010-05-03 14:54:34 -0700 | [diff] [blame] | 89 | /* a subdevice instruction */ |
| 90 | if (insn->subdev >= dev->n_subdevices) { |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 91 | ret = -EINVAL; |
| 92 | goto error; |
| 93 | } |
H Hartley Sweeten | 5818e70 | 2012-09-05 18:59:26 -0700 | [diff] [blame] | 94 | s = &dev->subdevices[insn->subdev]; |
Greg Kroah-Hartman | 3781bc5 | 2010-05-03 14:54:34 -0700 | [diff] [blame] | 95 | |
| 96 | if (s->type == COMEDI_SUBD_UNUSED) { |
YAMANE Toshiaki | 49e8e44 | 2012-10-05 09:07:00 +0900 | [diff] [blame] | 97 | dev_err(dev->class_dev, |
Gustavo A. R. Silva | 53d484b | 2015-01-11 15:46:26 -0600 | [diff] [blame] | 98 | "%d not usable subdevice\n", insn->subdev); |
Greg Kroah-Hartman | 3781bc5 | 2010-05-03 14:54:34 -0700 | [diff] [blame] | 99 | ret = -EIO; |
| 100 | goto error; |
| 101 | } |
| 102 | |
| 103 | /* XXX check lock */ |
| 104 | |
| 105 | ret = comedi_check_chanlist(s, 1, &insn->chanspec); |
| 106 | if (ret < 0) { |
YAMANE Toshiaki | 49e8e44 | 2012-10-05 09:07:00 +0900 | [diff] [blame] | 107 | dev_err(dev->class_dev, "bad chanspec\n"); |
Greg Kroah-Hartman | 3781bc5 | 2010-05-03 14:54:34 -0700 | [diff] [blame] | 108 | ret = -EINVAL; |
| 109 | goto error; |
| 110 | } |
| 111 | |
| 112 | if (s->busy) { |
| 113 | ret = -EBUSY; |
| 114 | goto error; |
| 115 | } |
| 116 | s->busy = dev; |
| 117 | |
| 118 | switch (insn->insn) { |
| 119 | case INSN_BITS: |
H Hartley Sweeten | 1f5cc35 | 2012-09-19 17:26:53 -0700 | [diff] [blame] | 120 | ret = s->insn_bits(dev, s, insn, data); |
Greg Kroah-Hartman | 3781bc5 | 2010-05-03 14:54:34 -0700 | [diff] [blame] | 121 | break; |
| 122 | case INSN_CONFIG: |
| 123 | /* XXX should check instruction length */ |
H Hartley Sweeten | 1f5cc35 | 2012-09-19 17:26:53 -0700 | [diff] [blame] | 124 | ret = s->insn_config(dev, s, insn, data); |
Greg Kroah-Hartman | 3781bc5 | 2010-05-03 14:54:34 -0700 | [diff] [blame] | 125 | break; |
| 126 | default: |
| 127 | ret = -EINVAL; |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | s->busy = NULL; |
Mithlesh Thukral | 0a85b6f | 2009-06-08 21:04:41 +0530 | [diff] [blame] | 132 | error: |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 133 | |
Ian Abbott | 69e2387f | 2013-11-08 15:03:39 +0000 | [diff] [blame] | 134 | mutex_unlock(&dev->mutex); |
David Schleef | b79a7a2 | 2008-11-14 15:58:23 -0800 | [diff] [blame] | 135 | return ret; |
| 136 | } |
| 137 | |
Ian Abbott | 16d2d3c | 2013-08-23 14:45:07 +0100 | [diff] [blame] | 138 | int comedi_dio_get_config(struct comedi_device *dev, unsigned int subdev, |
| 139 | unsigned int chan, unsigned int *io) |
| 140 | { |
| 141 | struct comedi_insn insn; |
| 142 | unsigned int data[2]; |
| 143 | int ret; |
| 144 | |
| 145 | memset(&insn, 0, sizeof(insn)); |
| 146 | insn.insn = INSN_CONFIG; |
| 147 | insn.n = 2; |
| 148 | insn.subdev = subdev; |
| 149 | insn.chanspec = CR_PACK(chan, 0, 0); |
| 150 | data[0] = INSN_CONFIG_DIO_QUERY; |
| 151 | data[1] = 0; |
| 152 | ret = comedi_do_insn(dev, &insn, data); |
| 153 | if (ret >= 0) |
| 154 | *io = data[1]; |
| 155 | return ret; |
| 156 | } |
| 157 | EXPORT_SYMBOL_GPL(comedi_dio_get_config); |
| 158 | |
Greg Kroah-Hartman | 472dfe7 | 2010-05-03 15:01:50 -0700 | [diff] [blame] | 159 | int comedi_dio_config(struct comedi_device *dev, unsigned int subdev, |
| 160 | unsigned int chan, unsigned int io) |
Greg Kroah-Hartman | a152575 | 2010-05-03 14:44:55 -0700 | [diff] [blame] | 161 | { |
| 162 | struct comedi_insn insn; |
| 163 | |
| 164 | memset(&insn, 0, sizeof(insn)); |
| 165 | insn.insn = INSN_CONFIG; |
| 166 | insn.n = 1; |
Greg Kroah-Hartman | a152575 | 2010-05-03 14:44:55 -0700 | [diff] [blame] | 167 | insn.subdev = subdev; |
| 168 | insn.chanspec = CR_PACK(chan, 0, 0); |
| 169 | |
H Hartley Sweeten | 1f5cc35 | 2012-09-19 17:26:53 -0700 | [diff] [blame] | 170 | return comedi_do_insn(dev, &insn, &io); |
Greg Kroah-Hartman | a152575 | 2010-05-03 14:44:55 -0700 | [diff] [blame] | 171 | } |
H Hartley Sweeten | 5660e74 | 2013-04-12 10:11:54 -0700 | [diff] [blame] | 172 | EXPORT_SYMBOL_GPL(comedi_dio_config); |
Greg Kroah-Hartman | a152575 | 2010-05-03 14:44:55 -0700 | [diff] [blame] | 173 | |
Ian Abbott | 0f3ce1a | 2013-08-23 14:45:08 +0100 | [diff] [blame] | 174 | int comedi_dio_bitfield2(struct comedi_device *dev, unsigned int subdev, |
| 175 | unsigned int mask, unsigned int *bits, |
| 176 | unsigned int base_channel) |
Greg Kroah-Hartman | a152575 | 2010-05-03 14:44:55 -0700 | [diff] [blame] | 177 | { |
| 178 | struct comedi_insn insn; |
| 179 | unsigned int data[2]; |
Ian Abbott | 0f3ce1a | 2013-08-23 14:45:08 +0100 | [diff] [blame] | 180 | unsigned int n_chan; |
| 181 | unsigned int shift; |
Greg Kroah-Hartman | a152575 | 2010-05-03 14:44:55 -0700 | [diff] [blame] | 182 | int ret; |
| 183 | |
Ian Abbott | 0f3ce1a | 2013-08-23 14:45:08 +0100 | [diff] [blame] | 184 | base_channel = CR_CHAN(base_channel); |
| 185 | n_chan = comedi_get_n_channels(dev, subdev); |
| 186 | if (base_channel >= n_chan) |
| 187 | return -EINVAL; |
| 188 | |
Greg Kroah-Hartman | a152575 | 2010-05-03 14:44:55 -0700 | [diff] [blame] | 189 | memset(&insn, 0, sizeof(insn)); |
| 190 | insn.insn = INSN_BITS; |
Ian Abbott | 0f3ce1a | 2013-08-23 14:45:08 +0100 | [diff] [blame] | 191 | insn.chanspec = base_channel; |
Greg Kroah-Hartman | a152575 | 2010-05-03 14:44:55 -0700 | [diff] [blame] | 192 | insn.n = 2; |
Greg Kroah-Hartman | a152575 | 2010-05-03 14:44:55 -0700 | [diff] [blame] | 193 | insn.subdev = subdev; |
| 194 | |
| 195 | data[0] = mask; |
| 196 | data[1] = *bits; |
| 197 | |
Ian Abbott | 0f3ce1a | 2013-08-23 14:45:08 +0100 | [diff] [blame] | 198 | /* |
| 199 | * Most drivers ignore the base channel in insn->chanspec. |
| 200 | * Fix this here if the subdevice has <= 32 channels. |
| 201 | */ |
| 202 | if (n_chan <= 32) { |
| 203 | shift = base_channel; |
| 204 | if (shift) { |
| 205 | insn.chanspec = 0; |
| 206 | data[0] <<= shift; |
| 207 | data[1] <<= shift; |
| 208 | } |
| 209 | } else { |
| 210 | shift = 0; |
| 211 | } |
| 212 | |
H Hartley Sweeten | 1f5cc35 | 2012-09-19 17:26:53 -0700 | [diff] [blame] | 213 | ret = comedi_do_insn(dev, &insn, data); |
Ian Abbott | 0f3ce1a | 2013-08-23 14:45:08 +0100 | [diff] [blame] | 214 | *bits = data[1] >> shift; |
Greg Kroah-Hartman | a152575 | 2010-05-03 14:44:55 -0700 | [diff] [blame] | 215 | return ret; |
| 216 | } |
Ian Abbott | 0f3ce1a | 2013-08-23 14:45:08 +0100 | [diff] [blame] | 217 | EXPORT_SYMBOL_GPL(comedi_dio_bitfield2); |
Greg Kroah-Hartman | a152575 | 2010-05-03 14:44:55 -0700 | [diff] [blame] | 218 | |
Greg Kroah-Hartman | 472dfe7 | 2010-05-03 15:01:50 -0700 | [diff] [blame] | 219 | int comedi_find_subdevice_by_type(struct comedi_device *dev, int type, |
| 220 | unsigned int subd) |
Greg Kroah-Hartman | a152575 | 2010-05-03 14:44:55 -0700 | [diff] [blame] | 221 | { |
H Hartley Sweeten | 5818e70 | 2012-09-05 18:59:26 -0700 | [diff] [blame] | 222 | struct comedi_subdevice *s; |
Ian Abbott | 69e2387f | 2013-11-08 15:03:39 +0000 | [diff] [blame] | 223 | int ret = -ENODEV; |
H Hartley Sweeten | 5818e70 | 2012-09-05 18:59:26 -0700 | [diff] [blame] | 224 | |
Ian Abbott | 69e2387f | 2013-11-08 15:03:39 +0000 | [diff] [blame] | 225 | down_read(&dev->attach_lock); |
| 226 | if (dev->attached) |
| 227 | for (; subd < dev->n_subdevices; subd++) { |
| 228 | s = &dev->subdevices[subd]; |
| 229 | if (s->type == type) { |
| 230 | ret = subd; |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | up_read(&dev->attach_lock); |
| 235 | return ret; |
Greg Kroah-Hartman | a152575 | 2010-05-03 14:44:55 -0700 | [diff] [blame] | 236 | } |
H Hartley Sweeten | 5660e74 | 2013-04-12 10:11:54 -0700 | [diff] [blame] | 237 | EXPORT_SYMBOL_GPL(comedi_find_subdevice_by_type); |
Greg Kroah-Hartman | a152575 | 2010-05-03 14:44:55 -0700 | [diff] [blame] | 238 | |
Greg Kroah-Hartman | 472dfe7 | 2010-05-03 15:01:50 -0700 | [diff] [blame] | 239 | int comedi_get_n_channels(struct comedi_device *dev, unsigned int subdevice) |
Greg Kroah-Hartman | a152575 | 2010-05-03 14:44:55 -0700 | [diff] [blame] | 240 | { |
Ian Abbott | 69e2387f | 2013-11-08 15:03:39 +0000 | [diff] [blame] | 241 | int n; |
Greg Kroah-Hartman | a152575 | 2010-05-03 14:44:55 -0700 | [diff] [blame] | 242 | |
Ian Abbott | 69e2387f | 2013-11-08 15:03:39 +0000 | [diff] [blame] | 243 | down_read(&dev->attach_lock); |
| 244 | if (!dev->attached || subdevice >= dev->n_subdevices) |
| 245 | n = 0; |
| 246 | else |
| 247 | n = dev->subdevices[subdevice].n_chan; |
| 248 | up_read(&dev->attach_lock); |
| 249 | |
| 250 | return n; |
Greg Kroah-Hartman | a152575 | 2010-05-03 14:44:55 -0700 | [diff] [blame] | 251 | } |
H Hartley Sweeten | 5660e74 | 2013-04-12 10:11:54 -0700 | [diff] [blame] | 252 | EXPORT_SYMBOL_GPL(comedi_get_n_channels); |