2017-11-07 21:58:43 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2008-11-15 07:58:23 +08:00
|
|
|
/*
|
2015-09-04 01:44:35 +08:00
|
|
|
* kcomedilib/kcomedilib.c
|
|
|
|
* a comedlib interface for kernel modules
|
|
|
|
*
|
|
|
|
* COMEDI - Linux Control and Measurement Device Interface
|
|
|
|
* Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
|
|
|
|
*/
|
2008-11-15 07:58:23 +08:00
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/fcntl.h>
|
|
|
|
#include <linux/mm.h>
|
2010-05-02 04:08:06 +08:00
|
|
|
#include <linux/io.h>
|
2008-11-15 07:58:23 +08:00
|
|
|
|
2021-11-17 20:05:59 +08:00
|
|
|
#include <linux/comedi.h>
|
|
|
|
#include <linux/comedi/comedidev.h>
|
|
|
|
#include <linux/comedi/comedilib.h>
|
2008-11-15 07:58:23 +08:00
|
|
|
|
|
|
|
MODULE_AUTHOR("David Schleef <ds@schleef.org>");
|
|
|
|
MODULE_DESCRIPTION("Comedi kernel library");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
2010-05-04 06:01:50 +08:00
|
|
|
struct comedi_device *comedi_open(const char *filename)
|
2008-11-15 07:58:23 +08:00
|
|
|
{
|
2013-11-08 23:03:38 +08:00
|
|
|
struct comedi_device *dev, *retval = NULL;
|
2008-11-15 07:58:23 +08:00
|
|
|
unsigned int minor;
|
|
|
|
|
|
|
|
if (strncmp(filename, "/dev/comedi", 11) != 0)
|
|
|
|
return NULL;
|
|
|
|
|
2014-02-15 11:59:40 +08:00
|
|
|
if (kstrtouint(filename + 11, 0, &minor))
|
|
|
|
return NULL;
|
2008-11-15 07:58:23 +08:00
|
|
|
|
|
|
|
if (minor >= COMEDI_NUM_BOARD_MINORS)
|
|
|
|
return NULL;
|
|
|
|
|
2013-11-08 23:03:38 +08:00
|
|
|
dev = comedi_dev_get_from_minor(minor);
|
|
|
|
if (!dev)
|
2008-11-15 07:58:23 +08:00
|
|
|
return NULL;
|
|
|
|
|
2013-11-08 23:03:39 +08:00
|
|
|
down_read(&dev->attach_lock);
|
|
|
|
if (dev->attached)
|
2013-11-08 23:03:38 +08:00
|
|
|
retval = dev;
|
|
|
|
else
|
|
|
|
retval = NULL;
|
2013-11-08 23:03:39 +08:00
|
|
|
up_read(&dev->attach_lock);
|
2013-11-08 23:03:38 +08:00
|
|
|
|
2015-03-05 03:15:41 +08:00
|
|
|
if (!retval)
|
2013-11-08 23:03:38 +08:00
|
|
|
comedi_dev_put(dev);
|
2008-11-15 07:58:23 +08:00
|
|
|
|
2013-11-08 23:03:38 +08:00
|
|
|
return retval;
|
2008-11-15 07:58:23 +08:00
|
|
|
}
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(comedi_open);
|
2008-11-15 07:58:23 +08:00
|
|
|
|
2013-11-08 23:03:39 +08:00
|
|
|
int comedi_close(struct comedi_device *dev)
|
2008-11-15 07:58:23 +08:00
|
|
|
{
|
2013-11-08 23:03:38 +08:00
|
|
|
comedi_dev_put(dev);
|
2008-11-15 07:58:23 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(comedi_close);
|
2008-11-15 07:58:23 +08:00
|
|
|
|
2012-09-20 08:26:53 +08:00
|
|
|
static int comedi_do_insn(struct comedi_device *dev,
|
|
|
|
struct comedi_insn *insn,
|
|
|
|
unsigned int *data)
|
2008-11-15 07:58:23 +08:00
|
|
|
{
|
2009-03-17 10:05:14 +08:00
|
|
|
struct comedi_subdevice *s;
|
2013-11-08 23:03:39 +08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
mutex_lock(&dev->mutex);
|
|
|
|
|
|
|
|
if (!dev->attached) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto error;
|
|
|
|
}
|
2008-11-15 07:58:23 +08:00
|
|
|
|
2010-05-04 05:54:34 +08:00
|
|
|
/* a subdevice instruction */
|
|
|
|
if (insn->subdev >= dev->n_subdevices) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto error;
|
2008-11-15 07:58:23 +08:00
|
|
|
}
|
2012-09-06 09:59:26 +08:00
|
|
|
s = &dev->subdevices[insn->subdev];
|
2010-05-04 05:54:34 +08:00
|
|
|
|
|
|
|
if (s->type == COMEDI_SUBD_UNUSED) {
|
2012-10-05 08:07:00 +08:00
|
|
|
dev_err(dev->class_dev,
|
2015-01-12 05:46:26 +08:00
|
|
|
"%d not usable subdevice\n", insn->subdev);
|
2010-05-04 05:54:34 +08:00
|
|
|
ret = -EIO;
|
2008-11-15 07:58:23 +08:00
|
|
|
goto error;
|
2010-05-04 05:54:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX check lock */
|
|
|
|
|
|
|
|
ret = comedi_check_chanlist(s, 1, &insn->chanspec);
|
|
|
|
if (ret < 0) {
|
2012-10-05 08:07:00 +08:00
|
|
|
dev_err(dev->class_dev, "bad chanspec\n");
|
2008-11-15 07:58:23 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto error;
|
|
|
|
}
|
2010-05-04 05:54:34 +08:00
|
|
|
|
|
|
|
if (s->busy) {
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
s->busy = dev;
|
|
|
|
|
|
|
|
switch (insn->insn) {
|
|
|
|
case INSN_BITS:
|
2012-09-20 08:26:53 +08:00
|
|
|
ret = s->insn_bits(dev, s, insn, data);
|
2010-05-04 05:54:34 +08:00
|
|
|
break;
|
|
|
|
case INSN_CONFIG:
|
|
|
|
/* XXX should check instruction length */
|
2012-09-20 08:26:53 +08:00
|
|
|
ret = s->insn_config(dev, s, insn, data);
|
2010-05-04 05:54:34 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->busy = NULL;
|
2009-06-08 23:34:41 +08:00
|
|
|
error:
|
2008-11-15 07:58:23 +08:00
|
|
|
|
2013-11-08 23:03:39 +08:00
|
|
|
mutex_unlock(&dev->mutex);
|
2008-11-15 07:58:23 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-08-23 21:45:07 +08:00
|
|
|
int comedi_dio_get_config(struct comedi_device *dev, unsigned int subdev,
|
|
|
|
unsigned int chan, unsigned int *io)
|
|
|
|
{
|
|
|
|
struct comedi_insn insn;
|
|
|
|
unsigned int data[2];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
memset(&insn, 0, sizeof(insn));
|
|
|
|
insn.insn = INSN_CONFIG;
|
|
|
|
insn.n = 2;
|
|
|
|
insn.subdev = subdev;
|
|
|
|
insn.chanspec = CR_PACK(chan, 0, 0);
|
|
|
|
data[0] = INSN_CONFIG_DIO_QUERY;
|
|
|
|
data[1] = 0;
|
|
|
|
ret = comedi_do_insn(dev, &insn, data);
|
|
|
|
if (ret >= 0)
|
|
|
|
*io = data[1];
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(comedi_dio_get_config);
|
|
|
|
|
2010-05-04 06:01:50 +08:00
|
|
|
int comedi_dio_config(struct comedi_device *dev, unsigned int subdev,
|
|
|
|
unsigned int chan, unsigned int io)
|
2010-05-04 05:44:55 +08:00
|
|
|
{
|
|
|
|
struct comedi_insn insn;
|
|
|
|
|
|
|
|
memset(&insn, 0, sizeof(insn));
|
|
|
|
insn.insn = INSN_CONFIG;
|
|
|
|
insn.n = 1;
|
|
|
|
insn.subdev = subdev;
|
|
|
|
insn.chanspec = CR_PACK(chan, 0, 0);
|
|
|
|
|
2012-09-20 08:26:53 +08:00
|
|
|
return comedi_do_insn(dev, &insn, &io);
|
2010-05-04 05:44:55 +08:00
|
|
|
}
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(comedi_dio_config);
|
2010-05-04 05:44:55 +08:00
|
|
|
|
2013-08-23 21:45:08 +08:00
|
|
|
int comedi_dio_bitfield2(struct comedi_device *dev, unsigned int subdev,
|
|
|
|
unsigned int mask, unsigned int *bits,
|
|
|
|
unsigned int base_channel)
|
2010-05-04 05:44:55 +08:00
|
|
|
{
|
|
|
|
struct comedi_insn insn;
|
|
|
|
unsigned int data[2];
|
2013-08-23 21:45:08 +08:00
|
|
|
unsigned int n_chan;
|
|
|
|
unsigned int shift;
|
2010-05-04 05:44:55 +08:00
|
|
|
int ret;
|
|
|
|
|
2013-08-23 21:45:08 +08:00
|
|
|
base_channel = CR_CHAN(base_channel);
|
|
|
|
n_chan = comedi_get_n_channels(dev, subdev);
|
|
|
|
if (base_channel >= n_chan)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2010-05-04 05:44:55 +08:00
|
|
|
memset(&insn, 0, sizeof(insn));
|
|
|
|
insn.insn = INSN_BITS;
|
2013-08-23 21:45:08 +08:00
|
|
|
insn.chanspec = base_channel;
|
2010-05-04 05:44:55 +08:00
|
|
|
insn.n = 2;
|
|
|
|
insn.subdev = subdev;
|
|
|
|
|
|
|
|
data[0] = mask;
|
|
|
|
data[1] = *bits;
|
|
|
|
|
2013-08-23 21:45:08 +08:00
|
|
|
/*
|
|
|
|
* Most drivers ignore the base channel in insn->chanspec.
|
|
|
|
* Fix this here if the subdevice has <= 32 channels.
|
|
|
|
*/
|
|
|
|
if (n_chan <= 32) {
|
|
|
|
shift = base_channel;
|
|
|
|
if (shift) {
|
|
|
|
insn.chanspec = 0;
|
|
|
|
data[0] <<= shift;
|
|
|
|
data[1] <<= shift;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
shift = 0;
|
|
|
|
}
|
2010-05-04 05:44:55 +08:00
|
|
|
|
2013-08-23 21:45:08 +08:00
|
|
|
ret = comedi_do_insn(dev, &insn, data);
|
|
|
|
*bits = data[1] >> shift;
|
2010-05-04 05:44:55 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2013-08-23 21:45:08 +08:00
|
|
|
EXPORT_SYMBOL_GPL(comedi_dio_bitfield2);
|
2010-05-04 05:44:55 +08:00
|
|
|
|
2010-05-04 06:01:50 +08:00
|
|
|
int comedi_find_subdevice_by_type(struct comedi_device *dev, int type,
|
|
|
|
unsigned int subd)
|
2010-05-04 05:44:55 +08:00
|
|
|
{
|
2012-09-06 09:59:26 +08:00
|
|
|
struct comedi_subdevice *s;
|
2013-11-08 23:03:39 +08:00
|
|
|
int ret = -ENODEV;
|
|
|
|
|
|
|
|
down_read(&dev->attach_lock);
|
|
|
|
if (dev->attached)
|
|
|
|
for (; subd < dev->n_subdevices; subd++) {
|
|
|
|
s = &dev->subdevices[subd];
|
|
|
|
if (s->type == type) {
|
|
|
|
ret = subd;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
up_read(&dev->attach_lock);
|
|
|
|
return ret;
|
2010-05-04 05:44:55 +08:00
|
|
|
}
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(comedi_find_subdevice_by_type);
|
2010-05-04 05:44:55 +08:00
|
|
|
|
2010-05-04 06:01:50 +08:00
|
|
|
int comedi_get_n_channels(struct comedi_device *dev, unsigned int subdevice)
|
2010-05-04 05:44:55 +08:00
|
|
|
{
|
2013-11-08 23:03:39 +08:00
|
|
|
int n;
|
|
|
|
|
|
|
|
down_read(&dev->attach_lock);
|
|
|
|
if (!dev->attached || subdevice >= dev->n_subdevices)
|
|
|
|
n = 0;
|
|
|
|
else
|
|
|
|
n = dev->subdevices[subdevice].n_chan;
|
|
|
|
up_read(&dev->attach_lock);
|
2010-05-04 05:44:55 +08:00
|
|
|
|
2013-11-08 23:03:39 +08:00
|
|
|
return n;
|
2010-05-04 05:44:55 +08:00
|
|
|
}
|
2013-04-13 01:11:54 +08:00
|
|
|
EXPORT_SYMBOL_GPL(comedi_get_n_channels);
|
2016-12-06 00:57:28 +08:00
|
|
|
|
|
|
|
static int __init kcomedilib_module_init(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit kcomedilib_module_exit(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(kcomedilib_module_init);
|
|
|
|
module_exit(kcomedilib_module_exit);
|