blob: df9bba1b69eded250eaea794e7a2bb4d0e6547ed [file] [log] [blame]
Greg Kroah-Hartmane184e2b2017-11-07 14:58:43 +01001// SPDX-License-Identifier: GPL-2.0+
David Schleefb79a7a22008-11-14 15:58:23 -08002/*
Nayeemahmed Badebade6b32fcc2015-09-03 23:14:35 +05303 * 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 Badebade6b32fcc2015-09-03 23:14:35 +05308 */
David Schleefb79a7a22008-11-14 15:58:23 -08009
David Schleefb79a7a22008-11-14 15:58:23 -080010#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 Schleefb79a7a22008-11-14 15:58:23 -080016#include <linux/mm.h>
Greg Kroah-Hartman7382e572010-05-01 13:08:06 -070017#include <linux/io.h>
David Schleefb79a7a22008-11-14 15:58:23 -080018
19#include "../comedi.h"
20#include "../comedilib.h"
21#include "../comedidev.h"
22
23MODULE_AUTHOR("David Schleef <ds@schleef.org>");
24MODULE_DESCRIPTION("Comedi kernel library");
25MODULE_LICENSE("GPL");
26
Greg Kroah-Hartman472dfe72010-05-03 15:01:50 -070027struct comedi_device *comedi_open(const char *filename)
David Schleefb79a7a22008-11-14 15:58:23 -080028{
Ian Abbott799a66b2013-11-08 15:03:38 +000029 struct comedi_device *dev, *retval = NULL;
David Schleefb79a7a22008-11-14 15:58:23 -080030 unsigned int minor;
31
32 if (strncmp(filename, "/dev/comedi", 11) != 0)
33 return NULL;
34
Chase Southwoode6bed032014-02-14 21:59:40 -060035 if (kstrtouint(filename + 11, 0, &minor))
36 return NULL;
David Schleefb79a7a22008-11-14 15:58:23 -080037
38 if (minor >= COMEDI_NUM_BOARD_MINORS)
39 return NULL;
40
Ian Abbott799a66b2013-11-08 15:03:38 +000041 dev = comedi_dev_get_from_minor(minor);
42 if (!dev)
David Schleefb79a7a22008-11-14 15:58:23 -080043 return NULL;
44
Ian Abbott69e2387f2013-11-08 15:03:39 +000045 down_read(&dev->attach_lock);
46 if (dev->attached)
Ian Abbott799a66b2013-11-08 15:03:38 +000047 retval = dev;
48 else
49 retval = NULL;
Ian Abbott69e2387f2013-11-08 15:03:39 +000050 up_read(&dev->attach_lock);
David Schleefb79a7a22008-11-14 15:58:23 -080051
H Hartley Sweeten4c36fdd2015-03-04 12:15:41 -070052 if (!retval)
Ian Abbott799a66b2013-11-08 15:03:38 +000053 comedi_dev_put(dev);
54
55 return retval;
David Schleefb79a7a22008-11-14 15:58:23 -080056}
H Hartley Sweeten5660e742013-04-12 10:11:54 -070057EXPORT_SYMBOL_GPL(comedi_open);
David Schleefb79a7a22008-11-14 15:58:23 -080058
Ian Abbott69e2387f2013-11-08 15:03:39 +000059int comedi_close(struct comedi_device *dev)
David Schleefb79a7a22008-11-14 15:58:23 -080060{
Ian Abbott799a66b2013-11-08 15:03:38 +000061 comedi_dev_put(dev);
David Schleefb79a7a22008-11-14 15:58:23 -080062 return 0;
63}
H Hartley Sweeten5660e742013-04-12 10:11:54 -070064EXPORT_SYMBOL_GPL(comedi_close);
David Schleefb79a7a22008-11-14 15:58:23 -080065
H Hartley Sweeten1f5cc352012-09-19 17:26:53 -070066static int comedi_do_insn(struct comedi_device *dev,
67 struct comedi_insn *insn,
68 unsigned int *data)
David Schleefb79a7a22008-11-14 15:58:23 -080069{
Bill Pemberton34c43922009-03-16 22:05:14 -040070 struct comedi_subdevice *s;
Ian Abbott69e2387f2013-11-08 15:03:39 +000071 int ret;
72
73 mutex_lock(&dev->mutex);
74
75 if (!dev->attached) {
76 ret = -EINVAL;
77 goto error;
78 }
David Schleefb79a7a22008-11-14 15:58:23 -080079
Greg Kroah-Hartman3781bc52010-05-03 14:54:34 -070080 /* a subdevice instruction */
81 if (insn->subdev >= dev->n_subdevices) {
David Schleefb79a7a22008-11-14 15:58:23 -080082 ret = -EINVAL;
83 goto error;
84 }
H Hartley Sweeten5818e702012-09-05 18:59:26 -070085 s = &dev->subdevices[insn->subdev];
Greg Kroah-Hartman3781bc52010-05-03 14:54:34 -070086
87 if (s->type == COMEDI_SUBD_UNUSED) {
YAMANE Toshiaki49e8e442012-10-05 09:07:00 +090088 dev_err(dev->class_dev,
Gustavo A. R. Silva53d484b2015-01-11 15:46:26 -060089 "%d not usable subdevice\n", insn->subdev);
Greg Kroah-Hartman3781bc52010-05-03 14:54:34 -070090 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 Toshiaki49e8e442012-10-05 09:07:00 +090098 dev_err(dev->class_dev, "bad chanspec\n");
Greg Kroah-Hartman3781bc52010-05-03 14:54:34 -070099 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 Sweeten1f5cc352012-09-19 17:26:53 -0700111 ret = s->insn_bits(dev, s, insn, data);
Greg Kroah-Hartman3781bc52010-05-03 14:54:34 -0700112 break;
113 case INSN_CONFIG:
114 /* XXX should check instruction length */
H Hartley Sweeten1f5cc352012-09-19 17:26:53 -0700115 ret = s->insn_config(dev, s, insn, data);
Greg Kroah-Hartman3781bc52010-05-03 14:54:34 -0700116 break;
117 default:
118 ret = -EINVAL;
119 break;
120 }
121
122 s->busy = NULL;
Mithlesh Thukral0a85b6f2009-06-08 21:04:41 +0530123error:
David Schleefb79a7a22008-11-14 15:58:23 -0800124
Ian Abbott69e2387f2013-11-08 15:03:39 +0000125 mutex_unlock(&dev->mutex);
David Schleefb79a7a22008-11-14 15:58:23 -0800126 return ret;
127}
128
Ian Abbott16d2d3c2013-08-23 14:45:07 +0100129int 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}
148EXPORT_SYMBOL_GPL(comedi_dio_get_config);
149
Greg Kroah-Hartman472dfe72010-05-03 15:01:50 -0700150int comedi_dio_config(struct comedi_device *dev, unsigned int subdev,
151 unsigned int chan, unsigned int io)
Greg Kroah-Hartmana1525752010-05-03 14:44:55 -0700152{
153 struct comedi_insn insn;
154
155 memset(&insn, 0, sizeof(insn));
156 insn.insn = INSN_CONFIG;
157 insn.n = 1;
Greg Kroah-Hartmana1525752010-05-03 14:44:55 -0700158 insn.subdev = subdev;
159 insn.chanspec = CR_PACK(chan, 0, 0);
160
H Hartley Sweeten1f5cc352012-09-19 17:26:53 -0700161 return comedi_do_insn(dev, &insn, &io);
Greg Kroah-Hartmana1525752010-05-03 14:44:55 -0700162}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700163EXPORT_SYMBOL_GPL(comedi_dio_config);
Greg Kroah-Hartmana1525752010-05-03 14:44:55 -0700164
Ian Abbott0f3ce1a2013-08-23 14:45:08 +0100165int comedi_dio_bitfield2(struct comedi_device *dev, unsigned int subdev,
166 unsigned int mask, unsigned int *bits,
167 unsigned int base_channel)
Greg Kroah-Hartmana1525752010-05-03 14:44:55 -0700168{
169 struct comedi_insn insn;
170 unsigned int data[2];
Ian Abbott0f3ce1a2013-08-23 14:45:08 +0100171 unsigned int n_chan;
172 unsigned int shift;
Greg Kroah-Hartmana1525752010-05-03 14:44:55 -0700173 int ret;
174
Ian Abbott0f3ce1a2013-08-23 14:45:08 +0100175 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-Hartmana1525752010-05-03 14:44:55 -0700180 memset(&insn, 0, sizeof(insn));
181 insn.insn = INSN_BITS;
Ian Abbott0f3ce1a2013-08-23 14:45:08 +0100182 insn.chanspec = base_channel;
Greg Kroah-Hartmana1525752010-05-03 14:44:55 -0700183 insn.n = 2;
Greg Kroah-Hartmana1525752010-05-03 14:44:55 -0700184 insn.subdev = subdev;
185
186 data[0] = mask;
187 data[1] = *bits;
188
Ian Abbott0f3ce1a2013-08-23 14:45:08 +0100189 /*
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 Sweeten1f5cc352012-09-19 17:26:53 -0700204 ret = comedi_do_insn(dev, &insn, data);
Ian Abbott0f3ce1a2013-08-23 14:45:08 +0100205 *bits = data[1] >> shift;
Greg Kroah-Hartmana1525752010-05-03 14:44:55 -0700206 return ret;
207}
Ian Abbott0f3ce1a2013-08-23 14:45:08 +0100208EXPORT_SYMBOL_GPL(comedi_dio_bitfield2);
Greg Kroah-Hartmana1525752010-05-03 14:44:55 -0700209
Greg Kroah-Hartman472dfe72010-05-03 15:01:50 -0700210int comedi_find_subdevice_by_type(struct comedi_device *dev, int type,
211 unsigned int subd)
Greg Kroah-Hartmana1525752010-05-03 14:44:55 -0700212{
H Hartley Sweeten5818e702012-09-05 18:59:26 -0700213 struct comedi_subdevice *s;
Ian Abbott69e2387f2013-11-08 15:03:39 +0000214 int ret = -ENODEV;
H Hartley Sweeten5818e702012-09-05 18:59:26 -0700215
Ian Abbott69e2387f2013-11-08 15:03:39 +0000216 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-Hartmana1525752010-05-03 14:44:55 -0700227}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700228EXPORT_SYMBOL_GPL(comedi_find_subdevice_by_type);
Greg Kroah-Hartmana1525752010-05-03 14:44:55 -0700229
Greg Kroah-Hartman472dfe72010-05-03 15:01:50 -0700230int comedi_get_n_channels(struct comedi_device *dev, unsigned int subdevice)
Greg Kroah-Hartmana1525752010-05-03 14:44:55 -0700231{
Ian Abbott69e2387f2013-11-08 15:03:39 +0000232 int n;
Greg Kroah-Hartmana1525752010-05-03 14:44:55 -0700233
Ian Abbott69e2387f2013-11-08 15:03:39 +0000234 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-Hartmana1525752010-05-03 14:44:55 -0700242}
H Hartley Sweeten5660e742013-04-12 10:11:54 -0700243EXPORT_SYMBOL_GPL(comedi_get_n_channels);
Cheah Kok Cheongc7ea8b52016-12-06 00:57:28 +0800244
245static int __init kcomedilib_module_init(void)
246{
247 return 0;
248}
249
250static void __exit kcomedilib_module_exit(void)
251{
252}
253
254module_init(kcomedilib_module_init);
255module_exit(kcomedilib_module_exit);