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