2008-11-05 04:29:31 +00:00
|
|
|
/*
|
|
|
|
comedi/comedi_fops.c
|
|
|
|
comedi kernel module
|
|
|
|
|
|
|
|
COMEDI - Linux Control and Measurement Device Interface
|
|
|
|
Copyright (C) 1997-2000 David A. Schleef <ds@schleef.org>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2014-07-17 19:27:32 +00:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
#include "comedi_compat32.h"
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/fcntl.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/kmod.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include "comedidev.h"
|
|
|
|
#include <linux/cdev.h>
|
2009-04-14 15:21:41 +00:00
|
|
|
#include <linux/stat.h>
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2008-11-14 01:05:58 +00:00
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/uaccess.h>
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2012-06-19 09:17:44 +00:00
|
|
|
#include "comedi_internal.h"
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2013-04-04 13:59:02 +00:00
|
|
|
#define COMEDI_NUM_MINORS 0x100
|
2013-04-04 13:59:04 +00:00
|
|
|
#define COMEDI_NUM_SUBDEVICE_MINORS \
|
|
|
|
(COMEDI_NUM_MINORS - COMEDI_NUM_BOARD_MINORS)
|
2013-04-04 13:59:02 +00:00
|
|
|
|
2010-05-03 23:32:28 +00:00
|
|
|
static int comedi_num_legacy_minors;
|
2012-04-13 13:12:53 +00:00
|
|
|
module_param(comedi_num_legacy_minors, int, S_IRUGO);
|
|
|
|
MODULE_PARM_DESC(comedi_num_legacy_minors,
|
|
|
|
"number of comedi minor devices to reserve for non-auto-configured devices (default 0)"
|
|
|
|
);
|
|
|
|
|
2012-04-13 13:12:54 +00:00
|
|
|
unsigned int comedi_default_buf_size_kb = CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB;
|
2012-04-13 13:12:53 +00:00
|
|
|
module_param(comedi_default_buf_size_kb, uint, S_IRUGO | S_IWUSR);
|
|
|
|
MODULE_PARM_DESC(comedi_default_buf_size_kb,
|
|
|
|
"default asynchronous buffer size in KiB (default "
|
2012-04-13 13:12:54 +00:00
|
|
|
__MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_SIZE_KB) ")");
|
2012-04-13 13:12:53 +00:00
|
|
|
|
2012-04-13 13:12:54 +00:00
|
|
|
unsigned int comedi_default_buf_maxsize_kb
|
|
|
|
= CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
|
2012-04-13 13:12:53 +00:00
|
|
|
module_param(comedi_default_buf_maxsize_kb, uint, S_IRUGO | S_IWUSR);
|
|
|
|
MODULE_PARM_DESC(comedi_default_buf_maxsize_kb,
|
|
|
|
"default maximum size of asynchronous buffer in KiB (default "
|
2012-04-13 13:12:54 +00:00
|
|
|
__MODULE_STRING(CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB) ")");
|
2008-12-08 23:30:13 +00:00
|
|
|
|
2013-04-04 13:59:04 +00:00
|
|
|
static DEFINE_MUTEX(comedi_board_minor_table_lock);
|
2013-04-04 13:59:16 +00:00
|
|
|
static struct comedi_device
|
2013-04-04 13:59:04 +00:00
|
|
|
*comedi_board_minor_table[COMEDI_NUM_BOARD_MINORS];
|
|
|
|
|
|
|
|
static DEFINE_MUTEX(comedi_subdevice_minor_table_lock);
|
|
|
|
/* Note: indexed by minor - COMEDI_NUM_BOARD_MINORS. */
|
2013-04-04 13:59:15 +00:00
|
|
|
static struct comedi_subdevice
|
2013-04-04 13:59:04 +00:00
|
|
|
*comedi_subdevice_minor_table[COMEDI_NUM_SUBDEVICE_MINORS];
|
2008-11-14 01:05:58 +00:00
|
|
|
|
2013-04-04 13:58:55 +00:00
|
|
|
static struct class *comedi_class;
|
|
|
|
static struct cdev comedi_cdev;
|
|
|
|
|
|
|
|
static void comedi_device_init(struct comedi_device *dev)
|
|
|
|
{
|
2013-11-08 15:03:32 +00:00
|
|
|
kref_init(&dev->refcount);
|
2013-04-04 13:58:55 +00:00
|
|
|
spin_lock_init(&dev->spinlock);
|
|
|
|
mutex_init(&dev->mutex);
|
2013-11-08 15:03:24 +00:00
|
|
|
init_rwsem(&dev->attach_lock);
|
2013-04-04 13:58:55 +00:00
|
|
|
dev->minor = -1;
|
|
|
|
}
|
|
|
|
|
2013-11-08 15:03:32 +00:00
|
|
|
static void comedi_dev_kref_release(struct kref *kref)
|
|
|
|
{
|
|
|
|
struct comedi_device *dev =
|
|
|
|
container_of(kref, struct comedi_device, refcount);
|
|
|
|
|
|
|
|
mutex_destroy(&dev->mutex);
|
2013-12-11 14:51:02 +00:00
|
|
|
put_device(dev->class_dev);
|
2013-11-08 15:03:32 +00:00
|
|
|
kfree(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
int comedi_dev_put(struct comedi_device *dev)
|
|
|
|
{
|
|
|
|
if (dev)
|
|
|
|
return kref_put(&dev->refcount, comedi_dev_kref_release);
|
|
|
|
return 1;
|
|
|
|
}
|
2013-11-08 15:03:33 +00:00
|
|
|
EXPORT_SYMBOL_GPL(comedi_dev_put);
|
|
|
|
|
|
|
|
static struct comedi_device *comedi_dev_get(struct comedi_device *dev)
|
|
|
|
{
|
|
|
|
if (dev)
|
|
|
|
kref_get(&dev->refcount);
|
|
|
|
return dev;
|
|
|
|
}
|
2013-11-08 15:03:32 +00:00
|
|
|
|
2013-04-04 13:58:55 +00:00
|
|
|
static void comedi_device_cleanup(struct comedi_device *dev)
|
|
|
|
{
|
|
|
|
struct module *driver_module = NULL;
|
|
|
|
|
|
|
|
if (dev == NULL)
|
|
|
|
return;
|
|
|
|
mutex_lock(&dev->mutex);
|
|
|
|
if (dev->attached)
|
|
|
|
driver_module = dev->driver->module;
|
|
|
|
comedi_device_detach(dev);
|
2013-12-11 14:51:03 +00:00
|
|
|
if (driver_module && dev->use_count)
|
|
|
|
module_put(driver_module);
|
2013-04-04 13:58:55 +00:00
|
|
|
mutex_unlock(&dev->mutex);
|
|
|
|
}
|
|
|
|
|
2013-04-04 13:59:18 +00:00
|
|
|
static bool comedi_clear_board_dev(struct comedi_device *dev)
|
|
|
|
{
|
|
|
|
unsigned int i = dev->minor;
|
|
|
|
bool cleared = false;
|
|
|
|
|
|
|
|
mutex_lock(&comedi_board_minor_table_lock);
|
|
|
|
if (dev == comedi_board_minor_table[i]) {
|
|
|
|
comedi_board_minor_table[i] = NULL;
|
|
|
|
cleared = true;
|
|
|
|
}
|
|
|
|
mutex_unlock(&comedi_board_minor_table_lock);
|
|
|
|
return cleared;
|
|
|
|
}
|
|
|
|
|
2013-04-04 13:59:16 +00:00
|
|
|
static struct comedi_device *comedi_clear_board_minor(unsigned minor)
|
2013-04-04 13:58:55 +00:00
|
|
|
{
|
2013-04-04 13:59:16 +00:00
|
|
|
struct comedi_device *dev;
|
2013-04-04 13:58:55 +00:00
|
|
|
|
2013-04-04 13:59:04 +00:00
|
|
|
mutex_lock(&comedi_board_minor_table_lock);
|
2013-04-04 13:59:16 +00:00
|
|
|
dev = comedi_board_minor_table[minor];
|
2013-04-04 13:59:04 +00:00
|
|
|
comedi_board_minor_table[minor] = NULL;
|
|
|
|
mutex_unlock(&comedi_board_minor_table_lock);
|
2013-04-04 13:59:16 +00:00
|
|
|
return dev;
|
2013-04-04 13:58:55 +00:00
|
|
|
}
|
|
|
|
|
2013-04-04 13:59:16 +00:00
|
|
|
static void comedi_free_board_dev(struct comedi_device *dev)
|
2013-04-04 13:58:55 +00:00
|
|
|
{
|
2013-04-04 13:59:16 +00:00
|
|
|
if (dev) {
|
2014-01-07 12:38:32 +00:00
|
|
|
comedi_device_cleanup(dev);
|
2013-04-04 13:59:16 +00:00
|
|
|
if (dev->class_dev) {
|
|
|
|
device_destroy(comedi_class,
|
|
|
|
MKDEV(COMEDI_MAJOR, dev->minor));
|
2013-04-04 13:58:55 +00:00
|
|
|
}
|
2013-11-08 15:03:32 +00:00
|
|
|
comedi_dev_put(dev);
|
2013-04-04 13:58:55 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-04 13:58:54 +00:00
|
|
|
|
2013-04-04 13:59:15 +00:00
|
|
|
static struct comedi_subdevice
|
2013-11-08 15:03:42 +00:00
|
|
|
*comedi_subdevice_from_minor(const struct comedi_device *dev, unsigned minor)
|
2013-04-04 13:59:04 +00:00
|
|
|
{
|
2013-04-04 13:59:15 +00:00
|
|
|
struct comedi_subdevice *s;
|
2013-04-04 13:59:04 +00:00
|
|
|
unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
|
|
|
|
|
|
|
|
BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
|
|
|
|
mutex_lock(&comedi_subdevice_minor_table_lock);
|
2013-04-04 13:59:15 +00:00
|
|
|
s = comedi_subdevice_minor_table[i];
|
2013-11-08 15:03:42 +00:00
|
|
|
if (s && s->device != dev)
|
|
|
|
s = NULL;
|
2013-04-04 13:59:04 +00:00
|
|
|
mutex_unlock(&comedi_subdevice_minor_table_lock);
|
2013-04-04 13:59:15 +00:00
|
|
|
return s;
|
2013-04-04 13:59:04 +00:00
|
|
|
}
|
|
|
|
|
2013-11-08 15:03:33 +00:00
|
|
|
static struct comedi_device *comedi_dev_get_from_board_minor(unsigned minor)
|
|
|
|
{
|
|
|
|
struct comedi_device *dev;
|
|
|
|
|
|
|
|
BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
|
|
|
|
mutex_lock(&comedi_board_minor_table_lock);
|
|
|
|
dev = comedi_dev_get(comedi_board_minor_table[minor]);
|
|
|
|
mutex_unlock(&comedi_board_minor_table_lock);
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct comedi_device *comedi_dev_get_from_subdevice_minor(unsigned minor)
|
|
|
|
{
|
|
|
|
struct comedi_device *dev;
|
|
|
|
struct comedi_subdevice *s;
|
|
|
|
unsigned int i = minor - COMEDI_NUM_BOARD_MINORS;
|
|
|
|
|
|
|
|
BUG_ON(i >= COMEDI_NUM_SUBDEVICE_MINORS);
|
|
|
|
mutex_lock(&comedi_subdevice_minor_table_lock);
|
|
|
|
s = comedi_subdevice_minor_table[i];
|
|
|
|
dev = comedi_dev_get(s ? s->device : NULL);
|
|
|
|
mutex_unlock(&comedi_subdevice_minor_table_lock);
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct comedi_device *comedi_dev_get_from_minor(unsigned minor)
|
|
|
|
{
|
|
|
|
if (minor < COMEDI_NUM_BOARD_MINORS)
|
|
|
|
return comedi_dev_get_from_board_minor(minor);
|
2014-07-15 15:11:02 +00:00
|
|
|
|
|
|
|
return comedi_dev_get_from_subdevice_minor(minor);
|
2013-11-08 15:03:33 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(comedi_dev_get_from_minor);
|
|
|
|
|
2012-12-19 22:33:29 +00:00
|
|
|
static struct comedi_subdevice *
|
2013-04-04 13:59:10 +00:00
|
|
|
comedi_read_subdevice(const struct comedi_device *dev, unsigned int minor)
|
2012-12-19 22:33:29 +00:00
|
|
|
{
|
2013-04-04 13:59:15 +00:00
|
|
|
struct comedi_subdevice *s;
|
2013-04-04 13:59:10 +00:00
|
|
|
|
|
|
|
if (minor >= COMEDI_NUM_BOARD_MINORS) {
|
2013-11-08 15:03:42 +00:00
|
|
|
s = comedi_subdevice_from_minor(dev, minor);
|
|
|
|
if (s == NULL || (s->subdev_flags & SDF_CMD_READ))
|
2013-04-04 13:59:15 +00:00
|
|
|
return s;
|
2013-04-04 13:59:10 +00:00
|
|
|
}
|
|
|
|
return dev->read_subdev;
|
2012-12-19 22:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct comedi_subdevice *
|
2013-04-04 13:59:10 +00:00
|
|
|
comedi_write_subdevice(const struct comedi_device *dev, unsigned int minor)
|
2012-12-19 22:33:29 +00:00
|
|
|
{
|
2013-04-04 13:59:15 +00:00
|
|
|
struct comedi_subdevice *s;
|
2013-04-04 13:59:10 +00:00
|
|
|
|
|
|
|
if (minor >= COMEDI_NUM_BOARD_MINORS) {
|
2013-11-08 15:03:42 +00:00
|
|
|
s = comedi_subdevice_from_minor(dev, minor);
|
|
|
|
if (s == NULL || (s->subdev_flags & SDF_CMD_WRITE))
|
2013-04-04 13:59:15 +00:00
|
|
|
return s;
|
2013-04-04 13:59:10 +00:00
|
|
|
}
|
|
|
|
return dev->write_subdev;
|
2012-12-19 22:33:29 +00:00
|
|
|
}
|
|
|
|
|
2009-04-14 15:21:41 +00:00
|
|
|
static int resize_async_buffer(struct comedi_device *dev,
|
2014-05-02 12:50:14 +00:00
|
|
|
struct comedi_subdevice *s, unsigned new_size)
|
2012-05-09 16:20:08 +00:00
|
|
|
{
|
2014-05-02 12:50:14 +00:00
|
|
|
struct comedi_async *async = s->async;
|
2012-05-09 16:20:08 +00:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (new_size > async->max_bufsize)
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
if (s->busy) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev,
|
|
|
|
"subdevice is busy, cannot resize buffer\n");
|
2012-05-09 16:20:08 +00:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
2014-05-06 12:12:11 +00:00
|
|
|
if (comedi_buf_is_mmapped(s)) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev,
|
|
|
|
"subdevice is mmapped, cannot resize buffer\n");
|
2012-05-09 16:20:08 +00:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure buffer is an integral number of pages
|
|
|
|
* (we round up) */
|
|
|
|
new_size = (new_size + PAGE_SIZE - 1) & PAGE_MASK;
|
|
|
|
|
|
|
|
retval = comedi_buf_alloc(dev, s, new_size);
|
|
|
|
if (retval < 0)
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
if (s->buf_change) {
|
2014-07-21 18:48:32 +00:00
|
|
|
retval = s->buf_change(dev, s);
|
2012-05-09 16:20:08 +00:00
|
|
|
if (retval < 0)
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "subd %d buffer resized to %i bytes\n",
|
|
|
|
s->index, async->prealloc_bufsz);
|
2012-05-09 16:20:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sysfs attribute files */
|
|
|
|
|
2013-07-24 22:05:25 +00:00
|
|
|
static ssize_t max_read_buffer_kb_show(struct device *csdev,
|
2012-05-09 16:20:08 +00:00
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
2013-04-04 13:59:09 +00:00
|
|
|
unsigned int minor = MINOR(csdev->devt);
|
2013-04-04 13:59:08 +00:00
|
|
|
struct comedi_device *dev;
|
|
|
|
struct comedi_subdevice *s;
|
2012-06-05 17:18:02 +00:00
|
|
|
unsigned int size = 0;
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2013-11-08 15:03:37 +00:00
|
|
|
dev = comedi_dev_get_from_minor(minor);
|
2013-04-04 13:59:11 +00:00
|
|
|
if (!dev)
|
2013-04-04 13:59:09 +00:00
|
|
|
return -ENODEV;
|
|
|
|
|
2013-04-04 13:59:08 +00:00
|
|
|
mutex_lock(&dev->mutex);
|
2013-04-04 13:59:10 +00:00
|
|
|
s = comedi_read_subdevice(dev, minor);
|
2012-06-05 17:18:02 +00:00
|
|
|
if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
|
|
|
|
size = s->async->max_bufsize / 1024;
|
2013-04-04 13:59:08 +00:00
|
|
|
mutex_unlock(&dev->mutex);
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2013-11-08 15:03:37 +00:00
|
|
|
comedi_dev_put(dev);
|
2014-02-12 08:28:35 +00:00
|
|
|
return snprintf(buf, PAGE_SIZE, "%u\n", size);
|
2012-05-09 16:20:08 +00:00
|
|
|
}
|
|
|
|
|
2013-07-24 22:05:25 +00:00
|
|
|
static ssize_t max_read_buffer_kb_store(struct device *csdev,
|
2012-05-09 16:20:08 +00:00
|
|
|
struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
2013-04-04 13:59:09 +00:00
|
|
|
unsigned int minor = MINOR(csdev->devt);
|
2013-04-04 13:59:08 +00:00
|
|
|
struct comedi_device *dev;
|
|
|
|
struct comedi_subdevice *s;
|
2012-06-05 17:18:02 +00:00
|
|
|
unsigned int size;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = kstrtouint(buf, 10, &size);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
if (size > (UINT_MAX / 1024))
|
2012-05-09 16:20:08 +00:00
|
|
|
return -EINVAL;
|
2012-06-05 17:18:02 +00:00
|
|
|
size *= 1024;
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2013-11-08 15:03:37 +00:00
|
|
|
dev = comedi_dev_get_from_minor(minor);
|
2013-04-04 13:59:11 +00:00
|
|
|
if (!dev)
|
2013-04-04 13:59:09 +00:00
|
|
|
return -ENODEV;
|
|
|
|
|
2013-04-04 13:59:08 +00:00
|
|
|
mutex_lock(&dev->mutex);
|
2013-04-04 13:59:10 +00:00
|
|
|
s = comedi_read_subdevice(dev, minor);
|
2012-06-05 17:18:02 +00:00
|
|
|
if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
|
|
|
|
s->async->max_bufsize = size;
|
|
|
|
else
|
|
|
|
err = -EINVAL;
|
2013-04-04 13:59:08 +00:00
|
|
|
mutex_unlock(&dev->mutex);
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2013-11-08 15:03:37 +00:00
|
|
|
comedi_dev_put(dev);
|
2012-06-05 17:18:02 +00:00
|
|
|
return err ? err : count;
|
2012-05-09 16:20:08 +00:00
|
|
|
}
|
2013-07-24 22:05:25 +00:00
|
|
|
static DEVICE_ATTR_RW(max_read_buffer_kb);
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2013-07-24 22:05:25 +00:00
|
|
|
static ssize_t read_buffer_kb_show(struct device *csdev,
|
2012-05-09 16:20:08 +00:00
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
2013-04-04 13:59:09 +00:00
|
|
|
unsigned int minor = MINOR(csdev->devt);
|
2013-04-04 13:59:08 +00:00
|
|
|
struct comedi_device *dev;
|
|
|
|
struct comedi_subdevice *s;
|
2012-06-05 17:18:02 +00:00
|
|
|
unsigned int size = 0;
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2013-11-08 15:03:37 +00:00
|
|
|
dev = comedi_dev_get_from_minor(minor);
|
2013-04-04 13:59:11 +00:00
|
|
|
if (!dev)
|
2013-04-04 13:59:09 +00:00
|
|
|
return -ENODEV;
|
|
|
|
|
2013-04-04 13:59:08 +00:00
|
|
|
mutex_lock(&dev->mutex);
|
2013-04-04 13:59:10 +00:00
|
|
|
s = comedi_read_subdevice(dev, minor);
|
2012-06-05 17:18:02 +00:00
|
|
|
if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
|
|
|
|
size = s->async->prealloc_bufsz / 1024;
|
2013-04-04 13:59:08 +00:00
|
|
|
mutex_unlock(&dev->mutex);
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2013-11-08 15:03:37 +00:00
|
|
|
comedi_dev_put(dev);
|
2014-02-12 08:28:35 +00:00
|
|
|
return snprintf(buf, PAGE_SIZE, "%u\n", size);
|
2012-05-09 16:20:08 +00:00
|
|
|
}
|
|
|
|
|
2013-07-24 22:05:25 +00:00
|
|
|
static ssize_t read_buffer_kb_store(struct device *csdev,
|
2012-05-09 16:20:08 +00:00
|
|
|
struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
2013-04-04 13:59:09 +00:00
|
|
|
unsigned int minor = MINOR(csdev->devt);
|
2013-04-04 13:59:08 +00:00
|
|
|
struct comedi_device *dev;
|
|
|
|
struct comedi_subdevice *s;
|
2012-06-05 17:18:02 +00:00
|
|
|
unsigned int size;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = kstrtouint(buf, 10, &size);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
if (size > (UINT_MAX / 1024))
|
2012-05-09 16:20:08 +00:00
|
|
|
return -EINVAL;
|
2012-06-05 17:18:02 +00:00
|
|
|
size *= 1024;
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2013-11-08 15:03:37 +00:00
|
|
|
dev = comedi_dev_get_from_minor(minor);
|
2013-04-04 13:59:11 +00:00
|
|
|
if (!dev)
|
2013-04-04 13:59:09 +00:00
|
|
|
return -ENODEV;
|
|
|
|
|
2013-04-04 13:59:08 +00:00
|
|
|
mutex_lock(&dev->mutex);
|
2013-04-04 13:59:10 +00:00
|
|
|
s = comedi_read_subdevice(dev, minor);
|
2012-06-05 17:18:02 +00:00
|
|
|
if (s && (s->subdev_flags & SDF_CMD_READ) && s->async)
|
2014-05-02 12:50:14 +00:00
|
|
|
err = resize_async_buffer(dev, s, size);
|
2012-06-05 17:18:02 +00:00
|
|
|
else
|
|
|
|
err = -EINVAL;
|
2013-04-04 13:59:08 +00:00
|
|
|
mutex_unlock(&dev->mutex);
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2013-11-08 15:03:37 +00:00
|
|
|
comedi_dev_put(dev);
|
2012-06-05 17:18:02 +00:00
|
|
|
return err ? err : count;
|
2012-05-09 16:20:08 +00:00
|
|
|
}
|
2013-07-24 22:05:25 +00:00
|
|
|
static DEVICE_ATTR_RW(read_buffer_kb);
|
2009-04-14 15:21:41 +00:00
|
|
|
|
2013-07-24 22:05:25 +00:00
|
|
|
static ssize_t max_write_buffer_kb_show(struct device *csdev,
|
2012-05-09 16:20:08 +00:00
|
|
|
struct device_attribute *attr,
|
|
|
|
char *buf)
|
|
|
|
{
|
2013-04-04 13:59:09 +00:00
|
|
|
unsigned int minor = MINOR(csdev->devt);
|
2013-04-04 13:59:08 +00:00
|
|
|
struct comedi_device *dev;
|
|
|
|
struct comedi_subdevice *s;
|
2012-06-05 17:18:02 +00:00
|
|
|
unsigned int size = 0;
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2013-11-08 15:03:37 +00:00
|
|
|
dev = comedi_dev_get_from_minor(minor);
|
2013-04-04 13:59:11 +00:00
|
|
|
if (!dev)
|
2013-04-04 13:59:09 +00:00
|
|
|
return -ENODEV;
|
|
|
|
|
2013-04-04 13:59:08 +00:00
|
|
|
mutex_lock(&dev->mutex);
|
2013-04-04 13:59:10 +00:00
|
|
|
s = comedi_write_subdevice(dev, minor);
|
2012-06-05 17:18:02 +00:00
|
|
|
if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
|
|
|
|
size = s->async->max_bufsize / 1024;
|
2013-04-04 13:59:08 +00:00
|
|
|
mutex_unlock(&dev->mutex);
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2013-11-08 15:03:37 +00:00
|
|
|
comedi_dev_put(dev);
|
2014-02-12 08:28:35 +00:00
|
|
|
return snprintf(buf, PAGE_SIZE, "%u\n", size);
|
2012-05-09 16:20:08 +00:00
|
|
|
}
|
|
|
|
|
2013-07-24 22:05:25 +00:00
|
|
|
static ssize_t max_write_buffer_kb_store(struct device *csdev,
|
2012-05-09 16:20:08 +00:00
|
|
|
struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
2013-04-04 13:59:09 +00:00
|
|
|
unsigned int minor = MINOR(csdev->devt);
|
2013-04-04 13:59:08 +00:00
|
|
|
struct comedi_device *dev;
|
|
|
|
struct comedi_subdevice *s;
|
2012-06-05 17:18:02 +00:00
|
|
|
unsigned int size;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = kstrtouint(buf, 10, &size);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
if (size > (UINT_MAX / 1024))
|
2012-05-09 16:20:08 +00:00
|
|
|
return -EINVAL;
|
2012-06-05 17:18:02 +00:00
|
|
|
size *= 1024;
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2013-11-08 15:03:37 +00:00
|
|
|
dev = comedi_dev_get_from_minor(minor);
|
2013-04-04 13:59:11 +00:00
|
|
|
if (!dev)
|
2013-04-04 13:59:09 +00:00
|
|
|
return -ENODEV;
|
|
|
|
|
2013-04-04 13:59:08 +00:00
|
|
|
mutex_lock(&dev->mutex);
|
2013-04-04 13:59:10 +00:00
|
|
|
s = comedi_write_subdevice(dev, minor);
|
2012-06-05 17:18:02 +00:00
|
|
|
if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
|
|
|
|
s->async->max_bufsize = size;
|
|
|
|
else
|
|
|
|
err = -EINVAL;
|
2013-04-04 13:59:08 +00:00
|
|
|
mutex_unlock(&dev->mutex);
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2013-11-08 15:03:37 +00:00
|
|
|
comedi_dev_put(dev);
|
2012-06-05 17:18:02 +00:00
|
|
|
return err ? err : count;
|
2012-05-09 16:20:08 +00:00
|
|
|
}
|
2013-07-24 22:05:25 +00:00
|
|
|
static DEVICE_ATTR_RW(max_write_buffer_kb);
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2013-07-24 22:05:25 +00:00
|
|
|
static ssize_t write_buffer_kb_show(struct device *csdev,
|
2012-05-09 16:20:08 +00:00
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
2013-04-04 13:59:09 +00:00
|
|
|
unsigned int minor = MINOR(csdev->devt);
|
2013-04-04 13:59:08 +00:00
|
|
|
struct comedi_device *dev;
|
|
|
|
struct comedi_subdevice *s;
|
2012-06-05 17:18:02 +00:00
|
|
|
unsigned int size = 0;
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2013-11-08 15:03:37 +00:00
|
|
|
dev = comedi_dev_get_from_minor(minor);
|
2013-04-04 13:59:11 +00:00
|
|
|
if (!dev)
|
2013-04-04 13:59:09 +00:00
|
|
|
return -ENODEV;
|
|
|
|
|
2013-04-04 13:59:08 +00:00
|
|
|
mutex_lock(&dev->mutex);
|
2013-04-04 13:59:10 +00:00
|
|
|
s = comedi_write_subdevice(dev, minor);
|
2012-06-05 17:18:02 +00:00
|
|
|
if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
|
|
|
|
size = s->async->prealloc_bufsz / 1024;
|
2013-04-04 13:59:08 +00:00
|
|
|
mutex_unlock(&dev->mutex);
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2013-11-08 15:03:37 +00:00
|
|
|
comedi_dev_put(dev);
|
2014-02-12 08:28:35 +00:00
|
|
|
return snprintf(buf, PAGE_SIZE, "%u\n", size);
|
2012-05-09 16:20:08 +00:00
|
|
|
}
|
|
|
|
|
2013-07-24 22:05:25 +00:00
|
|
|
static ssize_t write_buffer_kb_store(struct device *csdev,
|
2012-05-09 16:20:08 +00:00
|
|
|
struct device_attribute *attr,
|
|
|
|
const char *buf, size_t count)
|
|
|
|
{
|
2013-04-04 13:59:09 +00:00
|
|
|
unsigned int minor = MINOR(csdev->devt);
|
2013-04-04 13:59:08 +00:00
|
|
|
struct comedi_device *dev;
|
|
|
|
struct comedi_subdevice *s;
|
2012-06-05 17:18:02 +00:00
|
|
|
unsigned int size;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = kstrtouint(buf, 10, &size);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
if (size > (UINT_MAX / 1024))
|
2012-05-09 16:20:08 +00:00
|
|
|
return -EINVAL;
|
2012-06-05 17:18:02 +00:00
|
|
|
size *= 1024;
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2013-11-08 15:03:37 +00:00
|
|
|
dev = comedi_dev_get_from_minor(minor);
|
2013-04-04 13:59:11 +00:00
|
|
|
if (!dev)
|
2013-04-04 13:59:09 +00:00
|
|
|
return -ENODEV;
|
|
|
|
|
2013-04-04 13:59:08 +00:00
|
|
|
mutex_lock(&dev->mutex);
|
2013-04-04 13:59:10 +00:00
|
|
|
s = comedi_write_subdevice(dev, minor);
|
2012-06-05 17:18:02 +00:00
|
|
|
if (s && (s->subdev_flags & SDF_CMD_WRITE) && s->async)
|
2014-05-02 12:50:14 +00:00
|
|
|
err = resize_async_buffer(dev, s, size);
|
2012-06-05 17:18:02 +00:00
|
|
|
else
|
|
|
|
err = -EINVAL;
|
2013-04-04 13:59:08 +00:00
|
|
|
mutex_unlock(&dev->mutex);
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2013-11-08 15:03:37 +00:00
|
|
|
comedi_dev_put(dev);
|
2012-06-05 17:18:02 +00:00
|
|
|
return err ? err : count;
|
2012-05-09 16:20:08 +00:00
|
|
|
}
|
2013-07-24 22:05:25 +00:00
|
|
|
static DEVICE_ATTR_RW(write_buffer_kb);
|
|
|
|
|
|
|
|
static struct attribute *comedi_dev_attrs[] = {
|
|
|
|
&dev_attr_max_read_buffer_kb.attr,
|
|
|
|
&dev_attr_read_buffer_kb.attr,
|
|
|
|
&dev_attr_max_write_buffer_kb.attr,
|
|
|
|
&dev_attr_write_buffer_kb.attr,
|
|
|
|
NULL,
|
2012-05-09 16:20:08 +00:00
|
|
|
};
|
2013-07-24 22:05:25 +00:00
|
|
|
ATTRIBUTE_GROUPS(comedi_dev);
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2012-12-19 22:31:57 +00:00
|
|
|
static void comedi_set_subdevice_runflags(struct comedi_subdevice *s,
|
|
|
|
unsigned mask, unsigned bits)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&s->spin_lock, flags);
|
|
|
|
s->runflags &= ~mask;
|
|
|
|
s->runflags |= (bits & mask);
|
|
|
|
spin_unlock_irqrestore(&s->spin_lock, flags);
|
|
|
|
}
|
|
|
|
|
2012-12-19 22:43:40 +00:00
|
|
|
static unsigned comedi_get_subdevice_runflags(struct comedi_subdevice *s)
|
2012-12-19 22:42:26 +00:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
unsigned runflags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&s->spin_lock, flags);
|
|
|
|
runflags = s->runflags;
|
|
|
|
spin_unlock_irqrestore(&s->spin_lock, flags);
|
|
|
|
return runflags;
|
|
|
|
}
|
|
|
|
|
2012-12-19 22:42:47 +00:00
|
|
|
bool comedi_is_subdevice_running(struct comedi_subdevice *s)
|
|
|
|
{
|
|
|
|
unsigned runflags = comedi_get_subdevice_runflags(s);
|
|
|
|
|
|
|
|
return (runflags & SRF_RUNNING) ? true : false;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
|
|
|
|
|
2012-12-19 22:44:02 +00:00
|
|
|
static bool comedi_is_subdevice_in_error(struct comedi_subdevice *s)
|
|
|
|
{
|
|
|
|
unsigned runflags = comedi_get_subdevice_runflags(s);
|
|
|
|
|
|
|
|
return (runflags & SRF_ERROR) ? true : false;
|
|
|
|
}
|
|
|
|
|
2012-12-19 22:44:24 +00:00
|
|
|
static bool comedi_is_subdevice_idle(struct comedi_subdevice *s)
|
|
|
|
{
|
|
|
|
unsigned runflags = comedi_get_subdevice_runflags(s);
|
|
|
|
|
|
|
|
return (runflags & (SRF_ERROR | SRF_RUNNING)) ? false : true;
|
|
|
|
}
|
|
|
|
|
2013-06-11 18:32:29 +00:00
|
|
|
/**
|
2013-06-19 22:24:36 +00:00
|
|
|
* comedi_alloc_spriv() - Allocate memory for the subdevice private data.
|
2013-06-11 18:32:29 +00:00
|
|
|
* @s: comedi_subdevice struct
|
2013-06-19 22:24:36 +00:00
|
|
|
* @size: size of the memory to allocate
|
2013-06-11 18:32:29 +00:00
|
|
|
*
|
|
|
|
* This also sets the subdevice runflags to allow the core to automatically
|
|
|
|
* free the private data during the detach.
|
|
|
|
*/
|
2013-06-19 22:24:36 +00:00
|
|
|
void *comedi_alloc_spriv(struct comedi_subdevice *s, size_t size)
|
2013-06-11 18:32:29 +00:00
|
|
|
{
|
2013-06-19 22:24:36 +00:00
|
|
|
s->private = kzalloc(size, GFP_KERNEL);
|
|
|
|
if (s->private)
|
2013-10-07 14:51:58 +00:00
|
|
|
s->runflags |= SRF_FREE_SPRIV;
|
2013-06-19 22:24:36 +00:00
|
|
|
return s->private;
|
2013-06-11 18:32:29 +00:00
|
|
|
}
|
2013-06-19 22:24:36 +00:00
|
|
|
EXPORT_SYMBOL_GPL(comedi_alloc_spriv);
|
2013-06-11 18:32:29 +00:00
|
|
|
|
2012-12-19 22:31:57 +00:00
|
|
|
/*
|
|
|
|
This function restores a subdevice to an idle state.
|
|
|
|
*/
|
|
|
|
static void do_become_nonbusy(struct comedi_device *dev,
|
|
|
|
struct comedi_subdevice *s)
|
|
|
|
{
|
|
|
|
struct comedi_async *async = s->async;
|
|
|
|
|
|
|
|
comedi_set_subdevice_runflags(s, SRF_RUNNING, 0);
|
|
|
|
if (async) {
|
2014-05-06 12:12:10 +00:00
|
|
|
comedi_buf_reset(s);
|
2012-12-19 22:31:57 +00:00
|
|
|
async->inttrig = NULL;
|
|
|
|
kfree(async->cmd.chanlist);
|
|
|
|
async->cmd.chanlist = NULL;
|
2013-11-08 15:03:27 +00:00
|
|
|
s->busy = NULL;
|
|
|
|
wake_up_interruptible_all(&s->async->wait_head);
|
2012-12-19 22:31:57 +00:00
|
|
|
} else {
|
|
|
|
dev_err(dev->class_dev,
|
|
|
|
"BUG: (?) do_become_nonbusy called with async=NULL\n");
|
2013-11-08 15:03:27 +00:00
|
|
|
s->busy = NULL;
|
2012-12-19 22:31:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2012-12-19 22:43:18 +00:00
|
|
|
if (comedi_is_subdevice_running(s) && s->cancel)
|
2012-12-19 22:31:57 +00:00
|
|
|
ret = s->cancel(dev, s);
|
|
|
|
|
|
|
|
do_become_nonbusy(dev, s);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-11-08 15:03:28 +00:00
|
|
|
void comedi_device_cancel_all(struct comedi_device *dev)
|
|
|
|
{
|
|
|
|
struct comedi_subdevice *s;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!dev->attached)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < dev->n_subdevices; i++) {
|
|
|
|
s = &dev->subdevices[i];
|
|
|
|
if (s->async)
|
|
|
|
do_cancel(dev, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-19 22:31:57 +00:00
|
|
|
static int is_device_busy(struct comedi_device *dev)
|
|
|
|
{
|
|
|
|
struct comedi_subdevice *s;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!dev->attached)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < dev->n_subdevices; i++) {
|
|
|
|
s = &dev->subdevices[i];
|
|
|
|
if (s->busy)
|
|
|
|
return 1;
|
2014-05-06 12:12:11 +00:00
|
|
|
if (s->async && comedi_buf_is_mmapped(s))
|
2012-12-19 22:31:57 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
/*
|
|
|
|
COMEDI_DEVCONFIG
|
|
|
|
device config ioctl
|
|
|
|
|
|
|
|
arg:
|
|
|
|
pointer to devconfig structure
|
|
|
|
|
|
|
|
reads:
|
|
|
|
devconfig structure at arg
|
|
|
|
|
|
|
|
writes:
|
|
|
|
none
|
|
|
|
*/
|
2009-06-08 15:34:41 +00:00
|
|
|
static int do_devconfig_ioctl(struct comedi_device *dev,
|
2010-05-03 23:32:28 +00:00
|
|
|
struct comedi_devconfig __user *arg)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
2009-03-17 02:06:20 +00:00
|
|
|
struct comedi_devconfig it;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
if (arg == NULL) {
|
|
|
|
if (is_device_busy(dev))
|
|
|
|
return -EBUSY;
|
2008-11-14 01:05:58 +00:00
|
|
|
if (dev->attached) {
|
2008-11-05 04:29:31 +00:00
|
|
|
struct module *driver_module = dev->driver->module;
|
2014-05-01 08:23:12 +00:00
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
comedi_device_detach(dev);
|
|
|
|
module_put(driver_module);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-19 22:42:02 +00:00
|
|
|
if (copy_from_user(&it, arg, sizeof(it)))
|
2008-11-05 04:29:31 +00:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
it.board_name[COMEDI_NAMELEN - 1] = 0;
|
|
|
|
|
2013-01-09 16:46:10 +00:00
|
|
|
if (it.options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
|
|
|
|
dev_warn(dev->class_dev,
|
|
|
|
"comedi_config --init_data is deprecated\n");
|
|
|
|
return -EINVAL;
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
|
|
|
|
2013-04-04 13:58:54 +00:00
|
|
|
if (dev->minor >= comedi_num_legacy_minors)
|
|
|
|
/* don't re-use dynamically allocated comedi devices */
|
|
|
|
return -EBUSY;
|
|
|
|
|
2013-04-04 13:58:56 +00:00
|
|
|
/* This increments the driver module count on success. */
|
|
|
|
return comedi_device_attach(dev, &it);
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
COMEDI_BUFCONFIG
|
|
|
|
buffer configuration ioctl
|
|
|
|
|
|
|
|
arg:
|
|
|
|
pointer to bufconfig structure
|
|
|
|
|
|
|
|
reads:
|
|
|
|
bufconfig at arg
|
|
|
|
|
|
|
|
writes:
|
|
|
|
modified bufconfig at arg
|
|
|
|
|
|
|
|
*/
|
2010-05-03 23:32:28 +00:00
|
|
|
static int do_bufconfig_ioctl(struct comedi_device *dev,
|
|
|
|
struct comedi_bufconfig __user *arg)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
2009-03-17 02:06:37 +00:00
|
|
|
struct comedi_bufconfig bc;
|
2009-03-17 02:05:20 +00:00
|
|
|
struct comedi_async *async;
|
2009-03-17 02:05:14 +00:00
|
|
|
struct comedi_subdevice *s;
|
2009-04-14 15:21:41 +00:00
|
|
|
int retval = 0;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2012-12-19 22:42:02 +00:00
|
|
|
if (copy_from_user(&bc, arg, sizeof(bc)))
|
2008-11-05 04:29:31 +00:00
|
|
|
return -EFAULT;
|
|
|
|
|
2013-06-07 18:29:49 +00:00
|
|
|
if (bc.subdevice >= dev->n_subdevices)
|
2008-11-05 04:29:31 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2012-09-06 01:20:58 +00:00
|
|
|
s = &dev->subdevices[bc.subdevice];
|
2008-11-05 04:29:31 +00:00
|
|
|
async = s->async;
|
|
|
|
|
|
|
|
if (!async) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev,
|
|
|
|
"subdevice does not have async capability\n");
|
2008-11-05 04:29:31 +00:00
|
|
|
bc.size = 0;
|
|
|
|
bc.maximum_size = 0;
|
|
|
|
goto copyback;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bc.maximum_size) {
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
async->max_bufsize = bc.maximum_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bc.size) {
|
2014-05-02 12:50:14 +00:00
|
|
|
retval = resize_async_buffer(dev, s, bc.size);
|
2009-04-14 15:21:41 +00:00
|
|
|
if (retval < 0)
|
|
|
|
return retval;
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bc.size = async->prealloc_bufsz;
|
|
|
|
bc.maximum_size = async->max_bufsize;
|
|
|
|
|
2008-11-14 01:05:58 +00:00
|
|
|
copyback:
|
2012-12-19 22:42:02 +00:00
|
|
|
if (copy_to_user(arg, &bc, sizeof(bc)))
|
2008-11-05 04:29:31 +00:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
COMEDI_DEVINFO
|
|
|
|
device info ioctl
|
|
|
|
|
|
|
|
arg:
|
|
|
|
pointer to devinfo structure
|
|
|
|
|
|
|
|
reads:
|
|
|
|
none
|
|
|
|
|
|
|
|
writes:
|
|
|
|
devinfo structure
|
|
|
|
|
|
|
|
*/
|
2009-06-08 15:34:41 +00:00
|
|
|
static int do_devinfo_ioctl(struct comedi_device *dev,
|
2010-05-03 23:32:28 +00:00
|
|
|
struct comedi_devinfo __user *arg,
|
|
|
|
struct file *file)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
2013-02-27 21:59:05 +00:00
|
|
|
const unsigned minor = iminor(file_inode(file));
|
2012-12-19 22:39:18 +00:00
|
|
|
struct comedi_subdevice *s;
|
|
|
|
struct comedi_devinfo devinfo;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
memset(&devinfo, 0, sizeof(devinfo));
|
|
|
|
|
|
|
|
/* fill devinfo structure */
|
|
|
|
devinfo.version_code = COMEDI_VERSION_CODE;
|
|
|
|
devinfo.n_subdevs = dev->n_subdevices;
|
2011-06-26 08:56:22 +00:00
|
|
|
strlcpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
|
|
|
|
strlcpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2013-04-04 13:59:10 +00:00
|
|
|
s = comedi_read_subdevice(dev, minor);
|
2012-12-19 22:39:18 +00:00
|
|
|
if (s)
|
2012-12-20 00:27:02 +00:00
|
|
|
devinfo.read_subdevice = s->index;
|
2008-11-14 01:05:58 +00:00
|
|
|
else
|
2008-11-05 04:29:31 +00:00
|
|
|
devinfo.read_subdevice = -1;
|
2008-11-14 01:05:58 +00:00
|
|
|
|
2013-04-04 13:59:10 +00:00
|
|
|
s = comedi_write_subdevice(dev, minor);
|
2012-12-19 22:39:18 +00:00
|
|
|
if (s)
|
2012-12-20 00:27:02 +00:00
|
|
|
devinfo.write_subdevice = s->index;
|
2008-11-14 01:05:58 +00:00
|
|
|
else
|
2008-11-05 04:29:31 +00:00
|
|
|
devinfo.write_subdevice = -1;
|
|
|
|
|
2012-12-19 22:42:02 +00:00
|
|
|
if (copy_to_user(arg, &devinfo, sizeof(devinfo)))
|
2008-11-05 04:29:31 +00:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
COMEDI_SUBDINFO
|
|
|
|
subdevice info ioctl
|
|
|
|
|
|
|
|
arg:
|
|
|
|
pointer to array of subdevice info structures
|
|
|
|
|
|
|
|
reads:
|
|
|
|
none
|
|
|
|
|
|
|
|
writes:
|
|
|
|
array of subdevice info structures at arg
|
|
|
|
|
|
|
|
*/
|
2009-06-08 15:34:41 +00:00
|
|
|
static int do_subdinfo_ioctl(struct comedi_device *dev,
|
2010-05-03 23:32:28 +00:00
|
|
|
struct comedi_subdinfo __user *arg, void *file)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
|
|
|
int ret, i;
|
2009-03-17 02:06:09 +00:00
|
|
|
struct comedi_subdinfo *tmp, *us;
|
2009-03-17 02:05:14 +00:00
|
|
|
struct comedi_subdevice *s;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2012-12-19 22:42:02 +00:00
|
|
|
tmp = kcalloc(dev->n_subdevices, sizeof(*tmp), GFP_KERNEL);
|
2008-11-05 04:29:31 +00:00
|
|
|
if (!tmp)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* fill subdinfo structs */
|
|
|
|
for (i = 0; i < dev->n_subdevices; i++) {
|
2012-09-06 01:20:58 +00:00
|
|
|
s = &dev->subdevices[i];
|
2008-11-05 04:29:31 +00:00
|
|
|
us = tmp + i;
|
|
|
|
|
|
|
|
us->type = s->type;
|
|
|
|
us->n_chan = s->n_chan;
|
|
|
|
us->subd_flags = s->subdev_flags;
|
2012-12-19 22:43:18 +00:00
|
|
|
if (comedi_is_subdevice_running(s))
|
2008-11-05 04:29:31 +00:00
|
|
|
us->subd_flags |= SDF_RUNNING;
|
|
|
|
#define TIMER_nanosec 5 /* backwards compatibility */
|
|
|
|
us->timer_type = TIMER_nanosec;
|
|
|
|
us->len_chanlist = s->len_chanlist;
|
|
|
|
us->maxdata = s->maxdata;
|
|
|
|
if (s->range_table) {
|
|
|
|
us->range_type =
|
2008-11-14 01:05:58 +00:00
|
|
|
(i << 24) | (0 << 16) | (s->range_table->length);
|
2008-11-05 04:29:31 +00:00
|
|
|
} else {
|
|
|
|
us->range_type = 0; /* XXX */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->busy)
|
|
|
|
us->subd_flags |= SDF_BUSY;
|
|
|
|
if (s->busy == file)
|
|
|
|
us->subd_flags |= SDF_BUSY_OWNER;
|
|
|
|
if (s->lock)
|
|
|
|
us->subd_flags |= SDF_LOCKED;
|
|
|
|
if (s->lock == file)
|
|
|
|
us->subd_flags |= SDF_LOCK_OWNER;
|
|
|
|
if (!s->maxdata && s->maxdata_list)
|
|
|
|
us->subd_flags |= SDF_MAXDATA;
|
|
|
|
if (s->range_table_list)
|
|
|
|
us->subd_flags |= SDF_RANGETYPE;
|
|
|
|
if (s->do_cmd)
|
|
|
|
us->subd_flags |= SDF_CMD;
|
|
|
|
|
|
|
|
if (s->insn_bits != &insn_inval)
|
|
|
|
us->insn_bits_support = COMEDI_SUPPORTED;
|
|
|
|
else
|
|
|
|
us->insn_bits_support = COMEDI_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
|
2012-12-19 22:42:02 +00:00
|
|
|
ret = copy_to_user(arg, tmp, dev->n_subdevices * sizeof(*tmp));
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
kfree(tmp);
|
|
|
|
|
|
|
|
return ret ? -EFAULT : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
COMEDI_CHANINFO
|
|
|
|
subdevice info ioctl
|
|
|
|
|
|
|
|
arg:
|
|
|
|
pointer to chaninfo structure
|
|
|
|
|
|
|
|
reads:
|
|
|
|
chaninfo structure at arg
|
|
|
|
|
|
|
|
writes:
|
|
|
|
arrays at elements of chaninfo structure
|
|
|
|
|
|
|
|
*/
|
2009-06-08 15:34:41 +00:00
|
|
|
static int do_chaninfo_ioctl(struct comedi_device *dev,
|
2010-05-03 23:32:28 +00:00
|
|
|
struct comedi_chaninfo __user *arg)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
2009-03-17 02:05:14 +00:00
|
|
|
struct comedi_subdevice *s;
|
2009-03-17 02:06:04 +00:00
|
|
|
struct comedi_chaninfo it;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2012-12-19 22:42:02 +00:00
|
|
|
if (copy_from_user(&it, arg, sizeof(it)))
|
2008-11-05 04:29:31 +00:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (it.subdev >= dev->n_subdevices)
|
|
|
|
return -EINVAL;
|
2012-09-06 01:20:58 +00:00
|
|
|
s = &dev->subdevices[it.subdev];
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
if (it.maxdata_list) {
|
|
|
|
if (s->maxdata || !s->maxdata_list)
|
|
|
|
return -EINVAL;
|
|
|
|
if (copy_to_user(it.maxdata_list, s->maxdata_list,
|
2009-03-17 02:05:02 +00:00
|
|
|
s->n_chan * sizeof(unsigned int)))
|
2008-11-05 04:29:31 +00:00
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
2013-10-07 15:50:05 +00:00
|
|
|
if (it.flaglist)
|
|
|
|
return -EINVAL; /* flaglist not supported */
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
if (it.rangelist) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!s->range_table_list)
|
|
|
|
return -EINVAL;
|
|
|
|
for (i = 0; i < s->n_chan; i++) {
|
|
|
|
int x;
|
|
|
|
|
|
|
|
x = (dev->minor << 28) | (it.subdev << 24) | (i << 16) |
|
2008-11-14 01:05:58 +00:00
|
|
|
(s->range_table_list[i]->length);
|
2010-09-05 18:32:33 +00:00
|
|
|
if (put_user(x, it.rangelist + i))
|
|
|
|
return -EFAULT;
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
2008-11-14 01:05:58 +00:00
|
|
|
#if 0
|
|
|
|
if (copy_to_user(it.rangelist, s->range_type_list,
|
2009-06-08 15:34:41 +00:00
|
|
|
s->n_chan * sizeof(unsigned int)))
|
2008-11-14 01:05:58 +00:00
|
|
|
return -EFAULT;
|
|
|
|
#endif
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
COMEDI_BUFINFO
|
|
|
|
buffer information ioctl
|
|
|
|
|
|
|
|
arg:
|
|
|
|
pointer to bufinfo structure
|
|
|
|
|
|
|
|
reads:
|
|
|
|
bufinfo at arg
|
|
|
|
|
|
|
|
writes:
|
|
|
|
modified bufinfo at arg
|
|
|
|
|
|
|
|
*/
|
2010-05-03 23:32:28 +00:00
|
|
|
static int do_bufinfo_ioctl(struct comedi_device *dev,
|
2010-05-19 17:09:50 +00:00
|
|
|
struct comedi_bufinfo __user *arg, void *file)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
2009-03-17 02:06:42 +00:00
|
|
|
struct comedi_bufinfo bi;
|
2009-03-17 02:05:14 +00:00
|
|
|
struct comedi_subdevice *s;
|
2009-03-17 02:05:20 +00:00
|
|
|
struct comedi_async *async;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2012-12-19 22:42:02 +00:00
|
|
|
if (copy_from_user(&bi, arg, sizeof(bi)))
|
2008-11-05 04:29:31 +00:00
|
|
|
return -EFAULT;
|
|
|
|
|
2013-06-07 18:29:50 +00:00
|
|
|
if (bi.subdevice >= dev->n_subdevices)
|
2008-11-05 04:29:31 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
2012-09-06 01:20:58 +00:00
|
|
|
s = &dev->subdevices[bi.subdevice];
|
2010-05-19 17:09:50 +00:00
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
async = s->async;
|
|
|
|
|
|
|
|
if (!async) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev,
|
|
|
|
"subdevice does not have async capability\n");
|
2008-11-05 04:29:31 +00:00
|
|
|
bi.buf_write_ptr = 0;
|
|
|
|
bi.buf_read_ptr = 0;
|
|
|
|
bi.buf_write_count = 0;
|
|
|
|
bi.buf_read_count = 0;
|
2010-05-19 17:09:49 +00:00
|
|
|
bi.bytes_read = 0;
|
|
|
|
bi.bytes_written = 0;
|
2008-11-05 04:29:31 +00:00
|
|
|
goto copyback;
|
|
|
|
}
|
2010-05-19 17:09:50 +00:00
|
|
|
if (!s->busy) {
|
|
|
|
bi.bytes_read = 0;
|
|
|
|
bi.bytes_written = 0;
|
|
|
|
goto copyback_position;
|
|
|
|
}
|
|
|
|
if (s->busy != file)
|
|
|
|
return -EACCES;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
if (bi.bytes_read && (s->subdev_flags & SDF_CMD_READ)) {
|
2014-05-06 12:12:07 +00:00
|
|
|
bi.bytes_read = comedi_buf_read_alloc(s, bi.bytes_read);
|
2014-05-06 12:12:08 +00:00
|
|
|
comedi_buf_read_free(s, bi.bytes_read);
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2012-12-19 22:44:24 +00:00
|
|
|
if (comedi_is_subdevice_idle(s) &&
|
2014-06-20 17:58:28 +00:00
|
|
|
comedi_buf_n_bytes_ready(s) == 0) {
|
2008-11-05 04:29:31 +00:00
|
|
|
do_become_nonbusy(dev, s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bi.bytes_written && (s->subdev_flags & SDF_CMD_WRITE)) {
|
|
|
|
bi.bytes_written =
|
2014-05-06 12:12:04 +00:00
|
|
|
comedi_buf_write_alloc(s, bi.bytes_written);
|
2014-05-06 12:12:05 +00:00
|
|
|
comedi_buf_write_free(s, bi.bytes_written);
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-19 17:09:50 +00:00
|
|
|
copyback_position:
|
2008-11-05 04:29:31 +00:00
|
|
|
bi.buf_write_count = async->buf_write_count;
|
|
|
|
bi.buf_write_ptr = async->buf_write_ptr;
|
|
|
|
bi.buf_read_count = async->buf_read_count;
|
|
|
|
bi.buf_read_ptr = async->buf_read_ptr;
|
|
|
|
|
2008-11-14 01:05:58 +00:00
|
|
|
copyback:
|
2012-12-19 22:42:02 +00:00
|
|
|
if (copy_to_user(arg, &bi, sizeof(bi)))
|
2008-11-05 04:29:31 +00:00
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-08 15:34:41 +00:00
|
|
|
static int check_insn_config_length(struct comedi_insn *insn,
|
|
|
|
unsigned int *data)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
2008-11-14 01:05:58 +00:00
|
|
|
if (insn->n < 1)
|
|
|
|
return -EINVAL;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
switch (data[0]) {
|
|
|
|
case INSN_CONFIG_DIO_OUTPUT:
|
|
|
|
case INSN_CONFIG_DIO_INPUT:
|
|
|
|
case INSN_CONFIG_DISARM:
|
|
|
|
case INSN_CONFIG_RESET:
|
|
|
|
if (insn->n == 1)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case INSN_CONFIG_ARM:
|
|
|
|
case INSN_CONFIG_DIO_QUERY:
|
|
|
|
case INSN_CONFIG_BLOCK_SIZE:
|
|
|
|
case INSN_CONFIG_FILTER:
|
|
|
|
case INSN_CONFIG_SERIAL_CLOCK:
|
|
|
|
case INSN_CONFIG_BIDIRECTIONAL_DATA:
|
|
|
|
case INSN_CONFIG_ALT_SOURCE:
|
|
|
|
case INSN_CONFIG_SET_COUNTER_MODE:
|
|
|
|
case INSN_CONFIG_8254_READ_STATUS:
|
|
|
|
case INSN_CONFIG_SET_ROUTING:
|
|
|
|
case INSN_CONFIG_GET_ROUTING:
|
|
|
|
case INSN_CONFIG_GET_PWM_STATUS:
|
|
|
|
case INSN_CONFIG_PWM_SET_PERIOD:
|
|
|
|
case INSN_CONFIG_PWM_GET_PERIOD:
|
|
|
|
if (insn->n == 2)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case INSN_CONFIG_SET_GATE_SRC:
|
|
|
|
case INSN_CONFIG_GET_GATE_SRC:
|
|
|
|
case INSN_CONFIG_SET_CLOCK_SRC:
|
|
|
|
case INSN_CONFIG_GET_CLOCK_SRC:
|
|
|
|
case INSN_CONFIG_SET_OTHER_SRC:
|
|
|
|
case INSN_CONFIG_GET_COUNTER_STATUS:
|
|
|
|
case INSN_CONFIG_PWM_SET_H_BRIDGE:
|
|
|
|
case INSN_CONFIG_PWM_GET_H_BRIDGE:
|
|
|
|
case INSN_CONFIG_GET_HARDWARE_BUFFER_SIZE:
|
|
|
|
if (insn->n == 3)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case INSN_CONFIG_PWM_OUTPUT:
|
|
|
|
case INSN_CONFIG_ANALOG_TRIG:
|
|
|
|
if (insn->n == 5)
|
|
|
|
return 0;
|
|
|
|
break;
|
2012-11-14 11:22:57 +00:00
|
|
|
case INSN_CONFIG_DIGITAL_TRIG:
|
|
|
|
if (insn->n == 6)
|
|
|
|
return 0;
|
|
|
|
break;
|
2009-06-08 15:34:41 +00:00
|
|
|
/* by default we allow the insn since we don't have checks for
|
|
|
|
* all possible cases yet */
|
2008-11-05 04:29:31 +00:00
|
|
|
default:
|
2014-07-17 19:27:32 +00:00
|
|
|
pr_warn("No check for data length of config insn id %i is implemented\n",
|
2012-08-16 13:38:05 +00:00
|
|
|
data[0]);
|
2014-07-17 19:27:32 +00:00
|
|
|
pr_warn("Add a check to %s in %s\n", __func__, __FILE__);
|
|
|
|
pr_warn("Assuming n=%i is correct\n", insn->n);
|
2008-11-05 04:29:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2009-06-08 15:34:41 +00:00
|
|
|
static int parse_insn(struct comedi_device *dev, struct comedi_insn *insn,
|
|
|
|
unsigned int *data, void *file)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
2009-03-17 02:05:14 +00:00
|
|
|
struct comedi_subdevice *s;
|
2008-11-05 04:29:31 +00:00
|
|
|
int ret = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (insn->insn & INSN_MASK_SPECIAL) {
|
|
|
|
/* a non-subdevice instruction */
|
|
|
|
|
|
|
|
switch (insn->insn) {
|
|
|
|
case INSN_GTOD:
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
if (insn->n != 2) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
do_gettimeofday(&tv);
|
|
|
|
data[0] = tv.tv_sec;
|
|
|
|
data[1] = tv.tv_usec;
|
|
|
|
ret = 2;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case INSN_WAIT:
|
|
|
|
if (insn->n != 1 || data[0] >= 100000) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
udelay(data[0] / 1000);
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
case INSN_INTTRIG:
|
|
|
|
if (insn->n != 1) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (insn->subdev >= dev->n_subdevices) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev,
|
|
|
|
"%d not usable subdevice\n",
|
2008-11-05 04:29:31 +00:00
|
|
|
insn->subdev);
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2012-09-06 01:20:58 +00:00
|
|
|
s = &dev->subdevices[insn->subdev];
|
2008-11-05 04:29:31 +00:00
|
|
|
if (!s->async) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "no async\n");
|
2008-11-05 04:29:31 +00:00
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!s->async->inttrig) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "no inttrig\n");
|
2008-11-05 04:29:31 +00:00
|
|
|
ret = -EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
2012-09-18 18:46:58 +00:00
|
|
|
ret = s->async->inttrig(dev, s, data[0]);
|
2008-11-05 04:29:31 +00:00
|
|
|
if (ret >= 0)
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
default:
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "invalid insn\n");
|
2008-11-05 04:29:31 +00:00
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* a subdevice instruction */
|
2009-03-17 02:05:02 +00:00
|
|
|
unsigned int maxdata;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
if (insn->subdev >= dev->n_subdevices) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "subdevice %d out of range\n",
|
|
|
|
insn->subdev);
|
2008-11-05 04:29:31 +00:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-09-06 01:20:58 +00:00
|
|
|
s = &dev->subdevices[insn->subdev];
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
if (s->type == COMEDI_SUBD_UNUSED) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "%d not usable subdevice\n",
|
|
|
|
insn->subdev);
|
2008-11-05 04:29:31 +00:00
|
|
|
ret = -EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* are we locked? (ioctl lock) */
|
|
|
|
if (s->lock && s->lock != file) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "device locked\n");
|
2008-11-05 04:29:31 +00:00
|
|
|
ret = -EACCES;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2010-05-01 19:33:17 +00:00
|
|
|
ret = comedi_check_chanlist(s, 1, &insn->chanspec);
|
2008-11-14 01:05:58 +00:00
|
|
|
if (ret < 0) {
|
2008-11-05 04:29:31 +00:00
|
|
|
ret = -EINVAL;
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "bad chanspec\n");
|
2008-11-05 04:29:31 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->busy) {
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* This looks arbitrary. It is. */
|
|
|
|
s->busy = &parse_insn;
|
|
|
|
switch (insn->insn) {
|
|
|
|
case INSN_READ:
|
|
|
|
ret = s->insn_read(dev, s, insn, data);
|
2014-02-10 18:49:45 +00:00
|
|
|
if (ret == -ETIMEDOUT) {
|
|
|
|
dev_dbg(dev->class_dev,
|
|
|
|
"subdevice %d read instruction timed out\n",
|
|
|
|
s->index);
|
|
|
|
}
|
2008-11-05 04:29:31 +00:00
|
|
|
break;
|
|
|
|
case INSN_WRITE:
|
|
|
|
maxdata = s->maxdata_list
|
2008-11-14 01:05:58 +00:00
|
|
|
? s->maxdata_list[CR_CHAN(insn->chanspec)]
|
|
|
|
: s->maxdata;
|
2008-11-05 04:29:31 +00:00
|
|
|
for (i = 0; i < insn->n; ++i) {
|
|
|
|
if (data[i] > maxdata) {
|
|
|
|
ret = -EINVAL;
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev,
|
|
|
|
"bad data value(s)\n");
|
2008-11-05 04:29:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 18:49:45 +00:00
|
|
|
if (ret == 0) {
|
2008-11-05 04:29:31 +00:00
|
|
|
ret = s->insn_write(dev, s, insn, data);
|
2014-02-10 18:49:45 +00:00
|
|
|
if (ret == -ETIMEDOUT) {
|
|
|
|
dev_dbg(dev->class_dev,
|
|
|
|
"subdevice %d write instruction timed out\n",
|
|
|
|
s->index);
|
|
|
|
}
|
|
|
|
}
|
2008-11-05 04:29:31 +00:00
|
|
|
break;
|
|
|
|
case INSN_BITS:
|
|
|
|
if (insn->n != 2) {
|
|
|
|
ret = -EINVAL;
|
2011-01-18 17:44:33 +00:00
|
|
|
} else {
|
|
|
|
/* Most drivers ignore the base channel in
|
|
|
|
* insn->chanspec. Fix this here if
|
|
|
|
* the subdevice has <= 32 channels. */
|
2014-07-18 21:28:14 +00:00
|
|
|
unsigned int orig_mask = data[0];
|
|
|
|
unsigned int shift = 0;
|
2011-01-18 17:44:33 +00:00
|
|
|
|
|
|
|
if (s->n_chan <= 32) {
|
|
|
|
shift = CR_CHAN(insn->chanspec);
|
|
|
|
if (shift > 0) {
|
|
|
|
insn->chanspec = 0;
|
|
|
|
data[0] <<= shift;
|
|
|
|
data[1] <<= shift;
|
|
|
|
}
|
2014-07-18 21:28:14 +00:00
|
|
|
}
|
2011-01-18 17:44:33 +00:00
|
|
|
ret = s->insn_bits(dev, s, insn, data);
|
|
|
|
data[0] = orig_mask;
|
|
|
|
if (shift > 0)
|
|
|
|
data[1] >>= shift;
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case INSN_CONFIG:
|
|
|
|
ret = check_insn_config_length(insn, data);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
ret = s->insn_config(dev, s, insn, data);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->busy = NULL;
|
|
|
|
}
|
|
|
|
|
2008-11-14 01:05:58 +00:00
|
|
|
out:
|
2008-11-05 04:29:31 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-01-21 21:02:59 +00:00
|
|
|
/*
|
|
|
|
* COMEDI_INSNLIST
|
|
|
|
* synchronous instructions
|
|
|
|
*
|
|
|
|
* arg:
|
|
|
|
* pointer to sync cmd structure
|
|
|
|
*
|
|
|
|
* reads:
|
|
|
|
* sync cmd struct at arg
|
|
|
|
* instruction list
|
|
|
|
* data (for writes)
|
|
|
|
*
|
|
|
|
* writes:
|
|
|
|
* data (for reads)
|
|
|
|
*/
|
|
|
|
/* arbitrary limits */
|
|
|
|
#define MAX_SAMPLES 256
|
|
|
|
static int do_insnlist_ioctl(struct comedi_device *dev,
|
|
|
|
struct comedi_insnlist __user *arg, void *file)
|
|
|
|
{
|
|
|
|
struct comedi_insnlist insnlist;
|
|
|
|
struct comedi_insn *insns = NULL;
|
|
|
|
unsigned int *data = NULL;
|
|
|
|
int i = 0;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (copy_from_user(&insnlist, arg, sizeof(insnlist)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
2014-06-02 20:48:06 +00:00
|
|
|
data = kmalloc_array(MAX_SAMPLES, sizeof(unsigned int), GFP_KERNEL);
|
2013-01-21 21:02:59 +00:00
|
|
|
if (!data) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
|
|
|
|
if (!insns) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (copy_from_user(insns, insnlist.insns,
|
|
|
|
sizeof(*insns) * insnlist.n_insns)) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "copy_from_user failed\n");
|
2013-01-21 21:02:59 +00:00
|
|
|
ret = -EFAULT;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < insnlist.n_insns; i++) {
|
|
|
|
if (insns[i].n > MAX_SAMPLES) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev,
|
|
|
|
"number of samples too large\n");
|
2013-01-21 21:02:59 +00:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (insns[i].insn & INSN_MASK_WRITE) {
|
|
|
|
if (copy_from_user(data, insns[i].data,
|
|
|
|
insns[i].n * sizeof(unsigned int))) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev,
|
|
|
|
"copy_from_user failed\n");
|
2013-01-21 21:02:59 +00:00
|
|
|
ret = -EFAULT;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = parse_insn(dev, insns + i, data, file);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
if (insns[i].insn & INSN_MASK_READ) {
|
|
|
|
if (copy_to_user(insns[i].data, data,
|
|
|
|
insns[i].n * sizeof(unsigned int))) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev,
|
|
|
|
"copy_to_user failed\n");
|
2013-01-21 21:02:59 +00:00
|
|
|
ret = -EFAULT;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (need_resched())
|
|
|
|
schedule();
|
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
kfree(insns);
|
|
|
|
kfree(data);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
/*
|
2010-03-10 08:47:44 +00:00
|
|
|
* COMEDI_INSN
|
|
|
|
* synchronous instructions
|
2008-11-05 04:29:31 +00:00
|
|
|
*
|
2010-03-10 08:47:44 +00:00
|
|
|
* arg:
|
|
|
|
* pointer to insn
|
2008-11-05 04:29:31 +00:00
|
|
|
*
|
2010-03-10 08:47:44 +00:00
|
|
|
* reads:
|
|
|
|
* struct comedi_insn struct at arg
|
|
|
|
* data (for writes)
|
2008-11-05 04:29:31 +00:00
|
|
|
*
|
2010-03-10 08:47:44 +00:00
|
|
|
* writes:
|
|
|
|
* data (for reads)
|
2008-11-05 04:29:31 +00:00
|
|
|
*/
|
2010-05-03 23:32:28 +00:00
|
|
|
static int do_insn_ioctl(struct comedi_device *dev,
|
|
|
|
struct comedi_insn __user *arg, void *file)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
2009-03-17 02:05:53 +00:00
|
|
|
struct comedi_insn insn;
|
2009-03-17 02:05:02 +00:00
|
|
|
unsigned int *data = NULL;
|
2008-11-05 04:29:31 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2014-06-02 20:48:06 +00:00
|
|
|
data = kmalloc_array(MAX_SAMPLES, sizeof(unsigned int), GFP_KERNEL);
|
2008-11-05 04:29:31 +00:00
|
|
|
if (!data) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2012-12-19 22:42:02 +00:00
|
|
|
if (copy_from_user(&insn, arg, sizeof(insn))) {
|
2008-11-05 04:29:31 +00:00
|
|
|
ret = -EFAULT;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is where the behavior of insn and insnlist deviate. */
|
|
|
|
if (insn.n > MAX_SAMPLES)
|
|
|
|
insn.n = MAX_SAMPLES;
|
|
|
|
if (insn.insn & INSN_MASK_WRITE) {
|
2010-05-13 09:44:39 +00:00
|
|
|
if (copy_from_user(data,
|
|
|
|
insn.data,
|
|
|
|
insn.n * sizeof(unsigned int))) {
|
2008-11-05 04:29:31 +00:00
|
|
|
ret = -EFAULT;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = parse_insn(dev, &insn, data, file);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
if (insn.insn & INSN_MASK_READ) {
|
2010-05-13 09:44:39 +00:00
|
|
|
if (copy_to_user(insn.data,
|
|
|
|
data,
|
|
|
|
insn.n * sizeof(unsigned int))) {
|
2008-11-05 04:29:31 +00:00
|
|
|
ret = -EFAULT;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = insn.n;
|
|
|
|
|
2008-11-14 01:05:58 +00:00
|
|
|
error:
|
|
|
|
kfree(data);
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-03-06 19:02:56 +00:00
|
|
|
static int __comedi_get_user_cmd(struct comedi_device *dev,
|
|
|
|
struct comedi_cmd __user *arg,
|
|
|
|
struct comedi_cmd *cmd)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
2009-03-17 02:05:14 +00:00
|
|
|
struct comedi_subdevice *s;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2014-03-06 19:02:56 +00:00
|
|
|
if (copy_from_user(cmd, arg, sizeof(*cmd))) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "bad cmd address\n");
|
2008-11-05 04:29:31 +00:00
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
2014-03-06 19:02:56 +00:00
|
|
|
if (cmd->subdev >= dev->n_subdevices) {
|
|
|
|
dev_dbg(dev->class_dev, "%d no such subdevice\n", cmd->subdev);
|
2008-11-05 04:29:31 +00:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2014-03-06 19:02:56 +00:00
|
|
|
s = &dev->subdevices[cmd->subdev];
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
if (s->type == COMEDI_SUBD_UNUSED) {
|
2014-03-31 20:55:39 +00:00
|
|
|
dev_dbg(dev->class_dev, "%d not valid subdevice\n",
|
|
|
|
cmd->subdev);
|
2008-11-05 04:29:31 +00:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!s->do_cmd || !s->do_cmdtest || !s->async) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev,
|
2014-03-31 20:55:39 +00:00
|
|
|
"subdevice %d does not support commands\n",
|
|
|
|
cmd->subdev);
|
2008-11-05 04:29:31 +00:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2014-03-06 19:02:56 +00:00
|
|
|
/* make sure channel/gain list isn't too long */
|
|
|
|
if (cmd->chanlist_len > s->len_chanlist) {
|
|
|
|
dev_dbg(dev->class_dev, "channel/gain list too long %d > %d\n",
|
|
|
|
cmd->chanlist_len, s->len_chanlist);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2014-10-30 12:42:26 +00:00
|
|
|
/*
|
|
|
|
* Set the CMDF_WRITE flag to the correct state if the subdevice
|
|
|
|
* supports only "read" commands or only "write" commands.
|
|
|
|
*/
|
|
|
|
switch (s->subdev_flags & (SDF_CMD_READ | SDF_CMD_WRITE)) {
|
|
|
|
case SDF_CMD_READ:
|
|
|
|
cmd->flags &= ~CMDF_WRITE;
|
|
|
|
break;
|
|
|
|
case SDF_CMD_WRITE:
|
|
|
|
cmd->flags |= CMDF_WRITE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-03-06 19:02:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-06 19:02:57 +00:00
|
|
|
static int __comedi_get_user_chanlist(struct comedi_device *dev,
|
|
|
|
struct comedi_subdevice *s,
|
|
|
|
unsigned int __user *user_chanlist,
|
|
|
|
struct comedi_cmd *cmd)
|
|
|
|
{
|
|
|
|
unsigned int *chanlist;
|
|
|
|
int ret;
|
|
|
|
|
2014-10-20 14:10:40 +00:00
|
|
|
cmd->chanlist = NULL;
|
2014-03-06 19:02:57 +00:00
|
|
|
chanlist = memdup_user(user_chanlist,
|
|
|
|
cmd->chanlist_len * sizeof(unsigned int));
|
|
|
|
if (IS_ERR(chanlist))
|
|
|
|
return PTR_ERR(chanlist);
|
|
|
|
|
|
|
|
/* make sure each element in channel/gain list is valid */
|
|
|
|
ret = comedi_check_chanlist(s, cmd->chanlist_len, chanlist);
|
|
|
|
if (ret < 0) {
|
|
|
|
kfree(chanlist);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd->chanlist = chanlist;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-06 19:02:56 +00:00
|
|
|
static int do_cmd_ioctl(struct comedi_device *dev,
|
|
|
|
struct comedi_cmd __user *arg, void *file)
|
|
|
|
{
|
|
|
|
struct comedi_cmd cmd;
|
|
|
|
struct comedi_subdevice *s;
|
|
|
|
struct comedi_async *async;
|
|
|
|
unsigned int __user *user_chanlist;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* get the user's cmd and do some simple validation */
|
|
|
|
ret = __comedi_get_user_cmd(dev, arg, &cmd);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* save user's chanlist pointer so it can be restored later */
|
|
|
|
user_chanlist = (unsigned int __user *)cmd.chanlist;
|
|
|
|
|
|
|
|
s = &dev->subdevices[cmd.subdev];
|
|
|
|
async = s->async;
|
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
/* are we locked? (ioctl lock) */
|
|
|
|
if (s->lock && s->lock != file) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "subdevice locked\n");
|
2008-11-05 04:29:31 +00:00
|
|
|
return -EACCES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* are we busy? */
|
|
|
|
if (s->busy) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "subdevice busy\n");
|
2008-11-05 04:29:31 +00:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure channel/gain list isn't too short */
|
2012-09-18 18:42:37 +00:00
|
|
|
if (cmd.chanlist_len < 1) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "channel/gain list too short %u < 1\n",
|
2012-09-18 18:42:37 +00:00
|
|
|
cmd.chanlist_len);
|
2013-07-05 15:49:34 +00:00
|
|
|
return -EINVAL;
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
|
|
|
|
2012-09-18 18:42:37 +00:00
|
|
|
async->cmd = cmd;
|
2008-11-05 04:29:31 +00:00
|
|
|
async->cmd.data = NULL;
|
|
|
|
|
2014-03-06 19:02:57 +00:00
|
|
|
/* load channel/gain list */
|
|
|
|
ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &async->cmd);
|
|
|
|
if (ret)
|
2008-11-05 04:29:31 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = s->do_cmdtest(dev, s, &async->cmd);
|
|
|
|
|
2014-09-03 12:45:44 +00:00
|
|
|
if (async->cmd.flags & CMDF_BOGUS || ret) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "test returned %d\n", ret);
|
2012-09-18 18:42:37 +00:00
|
|
|
cmd = async->cmd;
|
2008-11-14 01:05:58 +00:00
|
|
|
/* restore chanlist pointer before copying back */
|
2012-09-18 18:43:52 +00:00
|
|
|
cmd.chanlist = (unsigned int __force *)user_chanlist;
|
2012-09-18 18:42:37 +00:00
|
|
|
cmd.data = NULL;
|
2012-12-19 22:42:02 +00:00
|
|
|
if (copy_to_user(arg, &cmd, sizeof(cmd))) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "fault writing cmd\n");
|
2008-11-05 04:29:31 +00:00
|
|
|
ret = -EFAULT;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
ret = -EAGAIN;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!async->prealloc_bufsz) {
|
|
|
|
ret = -ENOMEM;
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "no buffer (?)\n");
|
2008-11-05 04:29:31 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2014-05-06 12:12:10 +00:00
|
|
|
comedi_buf_reset(s);
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2014-10-13 16:56:08 +00:00
|
|
|
async->cb_mask = COMEDI_CB_BLOCK | COMEDI_CB_CANCEL_MASK;
|
2014-09-03 12:45:30 +00:00
|
|
|
if (async->cmd.flags & CMDF_WAKE_EOS)
|
2008-11-05 04:29:31 +00:00
|
|
|
async->cb_mask |= COMEDI_CB_EOS;
|
|
|
|
|
2013-11-08 15:03:22 +00:00
|
|
|
comedi_set_subdevice_runflags(s, SRF_ERROR | SRF_RUNNING, SRF_RUNNING);
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2013-07-05 15:49:34 +00:00
|
|
|
/* set s->busy _after_ setting SRF_RUNNING flag to avoid race with
|
|
|
|
* comedi_read() or comedi_write() */
|
|
|
|
s->busy = file;
|
2008-11-05 04:29:31 +00:00
|
|
|
ret = s->do_cmd(dev, s);
|
|
|
|
if (ret == 0)
|
|
|
|
return 0;
|
|
|
|
|
2008-11-14 01:05:58 +00:00
|
|
|
cleanup:
|
2008-11-05 04:29:31 +00:00
|
|
|
do_become_nonbusy(dev, s);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
COMEDI_CMDTEST
|
|
|
|
command testing ioctl
|
|
|
|
|
|
|
|
arg:
|
|
|
|
pointer to cmd structure
|
|
|
|
|
|
|
|
reads:
|
|
|
|
cmd structure at arg
|
|
|
|
channel/range list
|
|
|
|
|
|
|
|
writes:
|
|
|
|
modified cmd structure at arg
|
|
|
|
|
|
|
|
*/
|
2010-05-03 23:32:28 +00:00
|
|
|
static int do_cmdtest_ioctl(struct comedi_device *dev,
|
|
|
|
struct comedi_cmd __user *arg, void *file)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
2012-09-18 18:43:13 +00:00
|
|
|
struct comedi_cmd cmd;
|
2009-03-17 02:05:14 +00:00
|
|
|
struct comedi_subdevice *s;
|
2012-09-18 18:43:52 +00:00
|
|
|
unsigned int __user *user_chanlist;
|
2014-03-06 19:02:56 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* get the user's cmd and do some simple validation */
|
|
|
|
ret = __comedi_get_user_cmd(dev, arg, &cmd);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2008-11-14 01:05:58 +00:00
|
|
|
/* save user's chanlist pointer so it can be restored later */
|
2012-09-18 18:43:52 +00:00
|
|
|
user_chanlist = (unsigned int __user *)cmd.chanlist;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2012-09-18 18:43:13 +00:00
|
|
|
s = &dev->subdevices[cmd.subdev];
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2014-10-08 15:09:14 +00:00
|
|
|
/* user_chanlist can be NULL for COMEDI_CMDTEST ioctl */
|
|
|
|
if (user_chanlist) {
|
|
|
|
/* load channel/gain list */
|
|
|
|
ret = __comedi_get_user_chanlist(dev, s, user_chanlist, &cmd);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2012-09-18 18:43:13 +00:00
|
|
|
ret = s->do_cmdtest(dev, s, &cmd);
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2014-10-20 14:10:40 +00:00
|
|
|
kfree(cmd.chanlist); /* free kernel copy of user chanlist */
|
|
|
|
|
2008-11-14 01:05:58 +00:00
|
|
|
/* restore chanlist pointer before copying back */
|
2012-09-18 18:43:52 +00:00
|
|
|
cmd.chanlist = (unsigned int __force *)user_chanlist;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2012-12-19 22:42:02 +00:00
|
|
|
if (copy_to_user(arg, &cmd, sizeof(cmd))) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "bad cmd address\n");
|
2008-11-05 04:29:31 +00:00
|
|
|
ret = -EFAULT;
|
|
|
|
}
|
2014-03-06 19:02:57 +00:00
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
COMEDI_LOCK
|
|
|
|
lock subdevice
|
|
|
|
|
|
|
|
arg:
|
|
|
|
subdevice number
|
|
|
|
|
|
|
|
reads:
|
|
|
|
none
|
|
|
|
|
|
|
|
writes:
|
|
|
|
none
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-10-28 17:15:47 +00:00
|
|
|
static int do_lock_ioctl(struct comedi_device *dev, unsigned long arg,
|
2009-06-08 15:34:41 +00:00
|
|
|
void *file)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
unsigned long flags;
|
2009-03-17 02:05:14 +00:00
|
|
|
struct comedi_subdevice *s;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
if (arg >= dev->n_subdevices)
|
|
|
|
return -EINVAL;
|
2012-09-06 01:20:58 +00:00
|
|
|
s = &dev->subdevices[arg];
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2009-04-27 21:44:31 +00:00
|
|
|
spin_lock_irqsave(&s->spin_lock, flags);
|
2008-11-14 01:05:58 +00:00
|
|
|
if (s->busy || s->lock)
|
2008-11-05 04:29:31 +00:00
|
|
|
ret = -EBUSY;
|
2008-11-14 01:05:58 +00:00
|
|
|
else
|
2008-11-05 04:29:31 +00:00
|
|
|
s->lock = file;
|
2009-04-27 21:44:31 +00:00
|
|
|
spin_unlock_irqrestore(&s->spin_lock, flags);
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
COMEDI_UNLOCK
|
|
|
|
unlock subdevice
|
|
|
|
|
|
|
|
arg:
|
|
|
|
subdevice number
|
|
|
|
|
|
|
|
reads:
|
|
|
|
none
|
|
|
|
|
|
|
|
writes:
|
|
|
|
none
|
|
|
|
|
|
|
|
This function isn't protected by the semaphore, since
|
|
|
|
we already own the lock.
|
|
|
|
*/
|
2014-10-28 17:15:47 +00:00
|
|
|
static int do_unlock_ioctl(struct comedi_device *dev, unsigned long arg,
|
2009-06-08 15:34:41 +00:00
|
|
|
void *file)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
2009-03-17 02:05:14 +00:00
|
|
|
struct comedi_subdevice *s;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
if (arg >= dev->n_subdevices)
|
|
|
|
return -EINVAL;
|
2012-09-06 01:20:58 +00:00
|
|
|
s = &dev->subdevices[arg];
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
if (s->busy)
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
if (s->lock && s->lock != file)
|
|
|
|
return -EACCES;
|
|
|
|
|
2014-07-21 18:01:26 +00:00
|
|
|
if (s->lock == file)
|
2008-11-05 04:29:31 +00:00
|
|
|
s->lock = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
COMEDI_CANCEL
|
|
|
|
cancel acquisition ioctl
|
|
|
|
|
|
|
|
arg:
|
|
|
|
subdevice number
|
|
|
|
|
|
|
|
reads:
|
|
|
|
nothing
|
|
|
|
|
|
|
|
writes:
|
|
|
|
nothing
|
|
|
|
|
|
|
|
*/
|
2014-10-28 17:15:47 +00:00
|
|
|
static int do_cancel_ioctl(struct comedi_device *dev, unsigned long arg,
|
2009-06-08 15:34:41 +00:00
|
|
|
void *file)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
2009-03-17 02:05:14 +00:00
|
|
|
struct comedi_subdevice *s;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
if (arg >= dev->n_subdevices)
|
|
|
|
return -EINVAL;
|
2012-09-06 01:20:58 +00:00
|
|
|
s = &dev->subdevices[arg];
|
2008-11-05 04:29:31 +00:00
|
|
|
if (s->async == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (!s->busy)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (s->busy != file)
|
|
|
|
return -EBUSY;
|
|
|
|
|
2014-10-06 08:16:09 +00:00
|
|
|
return do_cancel(dev, s);
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
COMEDI_POLL ioctl
|
|
|
|
instructs driver to synchronize buffers
|
|
|
|
|
|
|
|
arg:
|
|
|
|
subdevice number
|
|
|
|
|
|
|
|
reads:
|
|
|
|
nothing
|
|
|
|
|
|
|
|
writes:
|
|
|
|
nothing
|
|
|
|
|
|
|
|
*/
|
2014-10-28 17:15:47 +00:00
|
|
|
static int do_poll_ioctl(struct comedi_device *dev, unsigned long arg,
|
2009-06-08 15:34:41 +00:00
|
|
|
void *file)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
2009-03-17 02:05:14 +00:00
|
|
|
struct comedi_subdevice *s;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
if (arg >= dev->n_subdevices)
|
|
|
|
return -EINVAL;
|
2012-09-06 01:20:58 +00:00
|
|
|
s = &dev->subdevices[arg];
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
if (!s->busy)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (s->busy != file)
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
if (s->poll)
|
|
|
|
return s->poll(dev, s);
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2012-06-08 00:05:27 +00:00
|
|
|
static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
|
|
|
|
unsigned long arg)
|
|
|
|
{
|
2013-02-27 21:59:05 +00:00
|
|
|
const unsigned minor = iminor(file_inode(file));
|
2013-11-08 15:03:40 +00:00
|
|
|
struct comedi_device *dev = file->private_data;
|
2012-06-08 00:05:27 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
mutex_lock(&dev->mutex);
|
|
|
|
|
|
|
|
/* Device config is special, because it must work on
|
|
|
|
* an unconfigured device. */
|
|
|
|
if (cmd == COMEDI_DEVCONFIG) {
|
2013-01-28 16:14:31 +00:00
|
|
|
if (minor >= COMEDI_NUM_BOARD_MINORS) {
|
|
|
|
/* Device config not appropriate on non-board minors. */
|
|
|
|
rc = -ENOTTY;
|
|
|
|
goto done;
|
|
|
|
}
|
2012-06-08 00:05:27 +00:00
|
|
|
rc = do_devconfig_ioctl(dev,
|
|
|
|
(struct comedi_devconfig __user *)arg);
|
2013-04-04 13:58:54 +00:00
|
|
|
if (rc == 0) {
|
|
|
|
if (arg == 0 &&
|
|
|
|
dev->minor >= comedi_num_legacy_minors) {
|
|
|
|
/* Successfully unconfigured a dynamically
|
|
|
|
* allocated device. Try and remove it. */
|
2013-04-04 13:59:18 +00:00
|
|
|
if (comedi_clear_board_dev(dev)) {
|
2013-04-04 13:58:54 +00:00
|
|
|
mutex_unlock(&dev->mutex);
|
2013-04-04 13:59:16 +00:00
|
|
|
comedi_free_board_dev(dev);
|
2013-04-04 13:58:54 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-08 00:05:27 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dev->attached) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "no driver attached\n");
|
2012-06-08 00:05:27 +00:00
|
|
|
rc = -ENODEV;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case COMEDI_BUFCONFIG:
|
|
|
|
rc = do_bufconfig_ioctl(dev,
|
|
|
|
(struct comedi_bufconfig __user *)arg);
|
|
|
|
break;
|
|
|
|
case COMEDI_DEVINFO:
|
|
|
|
rc = do_devinfo_ioctl(dev, (struct comedi_devinfo __user *)arg,
|
|
|
|
file);
|
|
|
|
break;
|
|
|
|
case COMEDI_SUBDINFO:
|
|
|
|
rc = do_subdinfo_ioctl(dev,
|
|
|
|
(struct comedi_subdinfo __user *)arg,
|
|
|
|
file);
|
|
|
|
break;
|
|
|
|
case COMEDI_CHANINFO:
|
|
|
|
rc = do_chaninfo_ioctl(dev, (void __user *)arg);
|
|
|
|
break;
|
|
|
|
case COMEDI_RANGEINFO:
|
|
|
|
rc = do_rangeinfo_ioctl(dev, (void __user *)arg);
|
|
|
|
break;
|
|
|
|
case COMEDI_BUFINFO:
|
|
|
|
rc = do_bufinfo_ioctl(dev,
|
|
|
|
(struct comedi_bufinfo __user *)arg,
|
|
|
|
file);
|
|
|
|
break;
|
|
|
|
case COMEDI_LOCK:
|
|
|
|
rc = do_lock_ioctl(dev, arg, file);
|
|
|
|
break;
|
|
|
|
case COMEDI_UNLOCK:
|
|
|
|
rc = do_unlock_ioctl(dev, arg, file);
|
|
|
|
break;
|
|
|
|
case COMEDI_CANCEL:
|
|
|
|
rc = do_cancel_ioctl(dev, arg, file);
|
|
|
|
break;
|
|
|
|
case COMEDI_CMD:
|
|
|
|
rc = do_cmd_ioctl(dev, (struct comedi_cmd __user *)arg, file);
|
|
|
|
break;
|
|
|
|
case COMEDI_CMDTEST:
|
|
|
|
rc = do_cmdtest_ioctl(dev, (struct comedi_cmd __user *)arg,
|
|
|
|
file);
|
|
|
|
break;
|
|
|
|
case COMEDI_INSNLIST:
|
|
|
|
rc = do_insnlist_ioctl(dev,
|
|
|
|
(struct comedi_insnlist __user *)arg,
|
|
|
|
file);
|
|
|
|
break;
|
|
|
|
case COMEDI_INSN:
|
|
|
|
rc = do_insn_ioctl(dev, (struct comedi_insn __user *)arg,
|
|
|
|
file);
|
|
|
|
break;
|
|
|
|
case COMEDI_POLL:
|
|
|
|
rc = do_poll_ioctl(dev, arg, file);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rc = -ENOTTY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
mutex_unlock(&dev->mutex);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2011-10-29 07:45:39 +00:00
|
|
|
static void comedi_vm_open(struct vm_area_struct *area)
|
|
|
|
{
|
2013-11-08 15:03:43 +00:00
|
|
|
struct comedi_buf_map *bm;
|
2011-10-29 07:45:39 +00:00
|
|
|
|
2013-11-08 15:03:43 +00:00
|
|
|
bm = area->vm_private_data;
|
|
|
|
comedi_buf_map_get(bm);
|
2011-10-29 07:45:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void comedi_vm_close(struct vm_area_struct *area)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
2013-11-08 15:03:43 +00:00
|
|
|
struct comedi_buf_map *bm;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2013-11-08 15:03:43 +00:00
|
|
|
bm = area->vm_private_data;
|
|
|
|
comedi_buf_map_put(bm);
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct vm_operations_struct comedi_vm_ops = {
|
2011-10-29 07:45:39 +00:00
|
|
|
.open = comedi_vm_open,
|
|
|
|
.close = comedi_vm_close,
|
2008-11-05 04:29:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
|
|
|
|
{
|
2013-02-27 21:59:05 +00:00
|
|
|
const unsigned minor = iminor(file_inode(file));
|
2013-11-08 15:03:40 +00:00
|
|
|
struct comedi_device *dev = file->private_data;
|
2012-12-19 22:45:12 +00:00
|
|
|
struct comedi_subdevice *s;
|
|
|
|
struct comedi_async *async;
|
staging: comedi: fix circular locking dependency in comedi_mmap()
Mmapping a comedi data buffer with lockdep checking enabled produced the
following kernel debug messages:
======================================================
[ INFO: possible circular locking dependency detected ]
3.5.0-rc3-ija1+ #9 Tainted: G C
-------------------------------------------------------
comedi_test/4160 is trying to acquire lock:
(&dev->mutex#2){+.+.+.}, at: [<ffffffffa00313f4>] comedi_mmap+0x57/0x1d9 [comedi]
but task is already holding lock:
(&mm->mmap_sem){++++++}, at: [<ffffffff810c96fe>] vm_mmap_pgoff+0x41/0x76
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (&mm->mmap_sem){++++++}:
[<ffffffff8106d0e8>] lock_acquire+0x97/0x105
[<ffffffff810ce3bc>] might_fault+0x6d/0x90
[<ffffffffa0031ffb>] do_devinfo_ioctl.isra.7+0x11e/0x14c [comedi]
[<ffffffffa003227f>] comedi_unlocked_ioctl+0x256/0xe48 [comedi]
[<ffffffff810f7fcd>] vfs_ioctl+0x18/0x34
[<ffffffff810f87fd>] do_vfs_ioctl+0x382/0x43c
[<ffffffff810f88f9>] sys_ioctl+0x42/0x65
[<ffffffff81415c62>] system_call_fastpath+0x16/0x1b
-> #0 (&dev->mutex#2){+.+.+.}:
[<ffffffff8106c528>] __lock_acquire+0x101d/0x1591
[<ffffffff8106d0e8>] lock_acquire+0x97/0x105
[<ffffffff8140c894>] mutex_lock_nested+0x46/0x2a4
[<ffffffffa00313f4>] comedi_mmap+0x57/0x1d9 [comedi]
[<ffffffff810d5816>] mmap_region+0x281/0x492
[<ffffffff810d5c92>] do_mmap_pgoff+0x26b/0x2a7
[<ffffffff810c971a>] vm_mmap_pgoff+0x5d/0x76
[<ffffffff810d493f>] sys_mmap_pgoff+0xc7/0x10d
[<ffffffff81004d36>] sys_mmap+0x16/0x20
[<ffffffff81415c62>] system_call_fastpath+0x16/0x1b
other info that might help us debug this:
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(&mm->mmap_sem);
lock(&dev->mutex#2);
lock(&mm->mmap_sem);
lock(&dev->mutex#2);
*** DEADLOCK ***
To avoid the circular dependency, just try to get the lock in
`comedi_mmap()` instead of blocking. Since the comedi device's main mutex
is heavily used, do a down-read of its `attach_lock` rwsemaphore
instead. Trying to down-read `attach_lock` should only fail if
some task has down-write locked it, and that is only done while the
comedi device is being attached to or detached from a low-level hardware
device.
Unfortunately, acquiring the `attach_lock` doesn't prevent another
task replacing the comedi data buffer we are trying to mmap. The
details of the buffer are held in a `struct comedi_buf_map` and pointed
to by `s->async->buf_map` where `s` is the comedi subdevice whose buffer
we are trying to map. The `struct comedi_buf_map` is already reference
counted with a `struct kref`, so we can stop it being freed prematurely.
Modify `comedi_mmap()` to call new function
`comedi_buf_map_from_subdev_get()` to read the subdevice's current
buffer map pointer and increment its reference instead of accessing
`async->buf_map` directly. Call `comedi_buf_map_put()` to decrement the
reference once the buffer map structure has been dealt with. (Note that
`comedi_buf_map_put()` does nothing if passed a NULL pointer.)
`comedi_buf_map_from_subdev_get()` checks the subdevice's buffer map
pointer has been set and the buffer map has been initialized enough for
`comedi_mmap()` to deal with it (specifically, check the `n_pages`
member has been set to a non-zero value). If all is well, the buffer
map's reference is incremented and a pointer to it is returned. The
comedi subdevice's spin-lock is used to protect the checks. Also use
the spin-lock in `__comedi_buf_alloc()` and `__comedi_buf_free()` to
protect changes to the subdevice's buffer map structure pointer and the
buffer map structure's `n_pages` member. (This checking of `n_pages` is
a bit clunky and I [Ian Abbott] plan to deal with it in the future.)
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Cc: <stable@vger.kernel.org> # 3.14.x, 3.15.x
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-04-10 18:41:57 +00:00
|
|
|
struct comedi_buf_map *bm = NULL;
|
2008-11-05 04:29:31 +00:00
|
|
|
unsigned long start = vma->vm_start;
|
|
|
|
unsigned long size;
|
|
|
|
int n_pages;
|
|
|
|
int i;
|
|
|
|
int retval;
|
2011-11-08 21:23:03 +00:00
|
|
|
|
staging: comedi: fix circular locking dependency in comedi_mmap()
Mmapping a comedi data buffer with lockdep checking enabled produced the
following kernel debug messages:
======================================================
[ INFO: possible circular locking dependency detected ]
3.5.0-rc3-ija1+ #9 Tainted: G C
-------------------------------------------------------
comedi_test/4160 is trying to acquire lock:
(&dev->mutex#2){+.+.+.}, at: [<ffffffffa00313f4>] comedi_mmap+0x57/0x1d9 [comedi]
but task is already holding lock:
(&mm->mmap_sem){++++++}, at: [<ffffffff810c96fe>] vm_mmap_pgoff+0x41/0x76
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (&mm->mmap_sem){++++++}:
[<ffffffff8106d0e8>] lock_acquire+0x97/0x105
[<ffffffff810ce3bc>] might_fault+0x6d/0x90
[<ffffffffa0031ffb>] do_devinfo_ioctl.isra.7+0x11e/0x14c [comedi]
[<ffffffffa003227f>] comedi_unlocked_ioctl+0x256/0xe48 [comedi]
[<ffffffff810f7fcd>] vfs_ioctl+0x18/0x34
[<ffffffff810f87fd>] do_vfs_ioctl+0x382/0x43c
[<ffffffff810f88f9>] sys_ioctl+0x42/0x65
[<ffffffff81415c62>] system_call_fastpath+0x16/0x1b
-> #0 (&dev->mutex#2){+.+.+.}:
[<ffffffff8106c528>] __lock_acquire+0x101d/0x1591
[<ffffffff8106d0e8>] lock_acquire+0x97/0x105
[<ffffffff8140c894>] mutex_lock_nested+0x46/0x2a4
[<ffffffffa00313f4>] comedi_mmap+0x57/0x1d9 [comedi]
[<ffffffff810d5816>] mmap_region+0x281/0x492
[<ffffffff810d5c92>] do_mmap_pgoff+0x26b/0x2a7
[<ffffffff810c971a>] vm_mmap_pgoff+0x5d/0x76
[<ffffffff810d493f>] sys_mmap_pgoff+0xc7/0x10d
[<ffffffff81004d36>] sys_mmap+0x16/0x20
[<ffffffff81415c62>] system_call_fastpath+0x16/0x1b
other info that might help us debug this:
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(&mm->mmap_sem);
lock(&dev->mutex#2);
lock(&mm->mmap_sem);
lock(&dev->mutex#2);
*** DEADLOCK ***
To avoid the circular dependency, just try to get the lock in
`comedi_mmap()` instead of blocking. Since the comedi device's main mutex
is heavily used, do a down-read of its `attach_lock` rwsemaphore
instead. Trying to down-read `attach_lock` should only fail if
some task has down-write locked it, and that is only done while the
comedi device is being attached to or detached from a low-level hardware
device.
Unfortunately, acquiring the `attach_lock` doesn't prevent another
task replacing the comedi data buffer we are trying to mmap. The
details of the buffer are held in a `struct comedi_buf_map` and pointed
to by `s->async->buf_map` where `s` is the comedi subdevice whose buffer
we are trying to map. The `struct comedi_buf_map` is already reference
counted with a `struct kref`, so we can stop it being freed prematurely.
Modify `comedi_mmap()` to call new function
`comedi_buf_map_from_subdev_get()` to read the subdevice's current
buffer map pointer and increment its reference instead of accessing
`async->buf_map` directly. Call `comedi_buf_map_put()` to decrement the
reference once the buffer map structure has been dealt with. (Note that
`comedi_buf_map_put()` does nothing if passed a NULL pointer.)
`comedi_buf_map_from_subdev_get()` checks the subdevice's buffer map
pointer has been set and the buffer map has been initialized enough for
`comedi_mmap()` to deal with it (specifically, check the `n_pages`
member has been set to a non-zero value). If all is well, the buffer
map's reference is incremented and a pointer to it is returned. The
comedi subdevice's spin-lock is used to protect the checks. Also use
the spin-lock in `__comedi_buf_alloc()` and `__comedi_buf_free()` to
protect changes to the subdevice's buffer map structure pointer and the
buffer map structure's `n_pages` member. (This checking of `n_pages` is
a bit clunky and I [Ian Abbott] plan to deal with it in the future.)
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Cc: <stable@vger.kernel.org> # 3.14.x, 3.15.x
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-04-10 18:41:57 +00:00
|
|
|
/*
|
|
|
|
* 'trylock' avoids circular dependency with current->mm->mmap_sem
|
|
|
|
* and down-reading &dev->attach_lock should normally succeed without
|
|
|
|
* contention unless the device is in the process of being attached
|
|
|
|
* or detached.
|
|
|
|
*/
|
|
|
|
if (!down_read_trylock(&dev->attach_lock))
|
|
|
|
return -EAGAIN;
|
2012-12-19 22:45:12 +00:00
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
if (!dev->attached) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "no driver attached\n");
|
2008-11-05 04:29:31 +00:00
|
|
|
retval = -ENODEV;
|
|
|
|
goto done;
|
|
|
|
}
|
2012-12-19 22:45:12 +00:00
|
|
|
|
2008-11-14 01:05:58 +00:00
|
|
|
if (vma->vm_flags & VM_WRITE)
|
2013-04-04 13:59:10 +00:00
|
|
|
s = comedi_write_subdevice(dev, minor);
|
2008-11-14 01:05:58 +00:00
|
|
|
else
|
2013-04-04 13:59:10 +00:00
|
|
|
s = comedi_read_subdevice(dev, minor);
|
2012-12-19 22:45:12 +00:00
|
|
|
if (!s) {
|
2008-11-05 04:29:31 +00:00
|
|
|
retval = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2012-12-19 22:45:12 +00:00
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
async = s->async;
|
2012-12-19 22:45:12 +00:00
|
|
|
if (!async) {
|
2008-11-05 04:29:31 +00:00
|
|
|
retval = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vma->vm_pgoff != 0) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "mmap() offset must be 0.\n");
|
2008-11-05 04:29:31 +00:00
|
|
|
retval = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = vma->vm_end - vma->vm_start;
|
|
|
|
if (size > async->prealloc_bufsz) {
|
|
|
|
retval = -EFAULT;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (size & (~PAGE_MASK)) {
|
|
|
|
retval = -EFAULT;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
n_pages = size >> PAGE_SHIFT;
|
staging: comedi: fix circular locking dependency in comedi_mmap()
Mmapping a comedi data buffer with lockdep checking enabled produced the
following kernel debug messages:
======================================================
[ INFO: possible circular locking dependency detected ]
3.5.0-rc3-ija1+ #9 Tainted: G C
-------------------------------------------------------
comedi_test/4160 is trying to acquire lock:
(&dev->mutex#2){+.+.+.}, at: [<ffffffffa00313f4>] comedi_mmap+0x57/0x1d9 [comedi]
but task is already holding lock:
(&mm->mmap_sem){++++++}, at: [<ffffffff810c96fe>] vm_mmap_pgoff+0x41/0x76
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (&mm->mmap_sem){++++++}:
[<ffffffff8106d0e8>] lock_acquire+0x97/0x105
[<ffffffff810ce3bc>] might_fault+0x6d/0x90
[<ffffffffa0031ffb>] do_devinfo_ioctl.isra.7+0x11e/0x14c [comedi]
[<ffffffffa003227f>] comedi_unlocked_ioctl+0x256/0xe48 [comedi]
[<ffffffff810f7fcd>] vfs_ioctl+0x18/0x34
[<ffffffff810f87fd>] do_vfs_ioctl+0x382/0x43c
[<ffffffff810f88f9>] sys_ioctl+0x42/0x65
[<ffffffff81415c62>] system_call_fastpath+0x16/0x1b
-> #0 (&dev->mutex#2){+.+.+.}:
[<ffffffff8106c528>] __lock_acquire+0x101d/0x1591
[<ffffffff8106d0e8>] lock_acquire+0x97/0x105
[<ffffffff8140c894>] mutex_lock_nested+0x46/0x2a4
[<ffffffffa00313f4>] comedi_mmap+0x57/0x1d9 [comedi]
[<ffffffff810d5816>] mmap_region+0x281/0x492
[<ffffffff810d5c92>] do_mmap_pgoff+0x26b/0x2a7
[<ffffffff810c971a>] vm_mmap_pgoff+0x5d/0x76
[<ffffffff810d493f>] sys_mmap_pgoff+0xc7/0x10d
[<ffffffff81004d36>] sys_mmap+0x16/0x20
[<ffffffff81415c62>] system_call_fastpath+0x16/0x1b
other info that might help us debug this:
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(&mm->mmap_sem);
lock(&dev->mutex#2);
lock(&mm->mmap_sem);
lock(&dev->mutex#2);
*** DEADLOCK ***
To avoid the circular dependency, just try to get the lock in
`comedi_mmap()` instead of blocking. Since the comedi device's main mutex
is heavily used, do a down-read of its `attach_lock` rwsemaphore
instead. Trying to down-read `attach_lock` should only fail if
some task has down-write locked it, and that is only done while the
comedi device is being attached to or detached from a low-level hardware
device.
Unfortunately, acquiring the `attach_lock` doesn't prevent another
task replacing the comedi data buffer we are trying to mmap. The
details of the buffer are held in a `struct comedi_buf_map` and pointed
to by `s->async->buf_map` where `s` is the comedi subdevice whose buffer
we are trying to map. The `struct comedi_buf_map` is already reference
counted with a `struct kref`, so we can stop it being freed prematurely.
Modify `comedi_mmap()` to call new function
`comedi_buf_map_from_subdev_get()` to read the subdevice's current
buffer map pointer and increment its reference instead of accessing
`async->buf_map` directly. Call `comedi_buf_map_put()` to decrement the
reference once the buffer map structure has been dealt with. (Note that
`comedi_buf_map_put()` does nothing if passed a NULL pointer.)
`comedi_buf_map_from_subdev_get()` checks the subdevice's buffer map
pointer has been set and the buffer map has been initialized enough for
`comedi_mmap()` to deal with it (specifically, check the `n_pages`
member has been set to a non-zero value). If all is well, the buffer
map's reference is incremented and a pointer to it is returned. The
comedi subdevice's spin-lock is used to protect the checks. Also use
the spin-lock in `__comedi_buf_alloc()` and `__comedi_buf_free()` to
protect changes to the subdevice's buffer map structure pointer and the
buffer map structure's `n_pages` member. (This checking of `n_pages` is
a bit clunky and I [Ian Abbott] plan to deal with it in the future.)
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Cc: <stable@vger.kernel.org> # 3.14.x, 3.15.x
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-04-10 18:41:57 +00:00
|
|
|
|
|
|
|
/* get reference to current buf map (if any) */
|
|
|
|
bm = comedi_buf_map_from_subdev_get(s);
|
2013-11-08 15:03:43 +00:00
|
|
|
if (!bm || n_pages > bm->n_pages) {
|
|
|
|
retval = -EINVAL;
|
|
|
|
goto done;
|
|
|
|
}
|
2008-11-05 04:29:31 +00:00
|
|
|
for (i = 0; i < n_pages; ++i) {
|
2013-11-08 15:03:43 +00:00
|
|
|
struct comedi_buf_page *buf = &bm->page_list[i];
|
2012-12-19 22:45:12 +00:00
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
if (remap_pfn_range(vma, start,
|
2012-12-19 22:45:12 +00:00
|
|
|
page_to_pfn(virt_to_page(buf->virt_addr)),
|
|
|
|
PAGE_SIZE, PAGE_SHARED)) {
|
2008-11-05 04:29:31 +00:00
|
|
|
retval = -EAGAIN;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
start += PAGE_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
vma->vm_ops = &comedi_vm_ops;
|
2013-11-08 15:03:43 +00:00
|
|
|
vma->vm_private_data = bm;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2013-11-08 15:03:43 +00:00
|
|
|
vma->vm_ops->open(vma);
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
retval = 0;
|
2008-11-14 01:05:58 +00:00
|
|
|
done:
|
staging: comedi: fix circular locking dependency in comedi_mmap()
Mmapping a comedi data buffer with lockdep checking enabled produced the
following kernel debug messages:
======================================================
[ INFO: possible circular locking dependency detected ]
3.5.0-rc3-ija1+ #9 Tainted: G C
-------------------------------------------------------
comedi_test/4160 is trying to acquire lock:
(&dev->mutex#2){+.+.+.}, at: [<ffffffffa00313f4>] comedi_mmap+0x57/0x1d9 [comedi]
but task is already holding lock:
(&mm->mmap_sem){++++++}, at: [<ffffffff810c96fe>] vm_mmap_pgoff+0x41/0x76
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (&mm->mmap_sem){++++++}:
[<ffffffff8106d0e8>] lock_acquire+0x97/0x105
[<ffffffff810ce3bc>] might_fault+0x6d/0x90
[<ffffffffa0031ffb>] do_devinfo_ioctl.isra.7+0x11e/0x14c [comedi]
[<ffffffffa003227f>] comedi_unlocked_ioctl+0x256/0xe48 [comedi]
[<ffffffff810f7fcd>] vfs_ioctl+0x18/0x34
[<ffffffff810f87fd>] do_vfs_ioctl+0x382/0x43c
[<ffffffff810f88f9>] sys_ioctl+0x42/0x65
[<ffffffff81415c62>] system_call_fastpath+0x16/0x1b
-> #0 (&dev->mutex#2){+.+.+.}:
[<ffffffff8106c528>] __lock_acquire+0x101d/0x1591
[<ffffffff8106d0e8>] lock_acquire+0x97/0x105
[<ffffffff8140c894>] mutex_lock_nested+0x46/0x2a4
[<ffffffffa00313f4>] comedi_mmap+0x57/0x1d9 [comedi]
[<ffffffff810d5816>] mmap_region+0x281/0x492
[<ffffffff810d5c92>] do_mmap_pgoff+0x26b/0x2a7
[<ffffffff810c971a>] vm_mmap_pgoff+0x5d/0x76
[<ffffffff810d493f>] sys_mmap_pgoff+0xc7/0x10d
[<ffffffff81004d36>] sys_mmap+0x16/0x20
[<ffffffff81415c62>] system_call_fastpath+0x16/0x1b
other info that might help us debug this:
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(&mm->mmap_sem);
lock(&dev->mutex#2);
lock(&mm->mmap_sem);
lock(&dev->mutex#2);
*** DEADLOCK ***
To avoid the circular dependency, just try to get the lock in
`comedi_mmap()` instead of blocking. Since the comedi device's main mutex
is heavily used, do a down-read of its `attach_lock` rwsemaphore
instead. Trying to down-read `attach_lock` should only fail if
some task has down-write locked it, and that is only done while the
comedi device is being attached to or detached from a low-level hardware
device.
Unfortunately, acquiring the `attach_lock` doesn't prevent another
task replacing the comedi data buffer we are trying to mmap. The
details of the buffer are held in a `struct comedi_buf_map` and pointed
to by `s->async->buf_map` where `s` is the comedi subdevice whose buffer
we are trying to map. The `struct comedi_buf_map` is already reference
counted with a `struct kref`, so we can stop it being freed prematurely.
Modify `comedi_mmap()` to call new function
`comedi_buf_map_from_subdev_get()` to read the subdevice's current
buffer map pointer and increment its reference instead of accessing
`async->buf_map` directly. Call `comedi_buf_map_put()` to decrement the
reference once the buffer map structure has been dealt with. (Note that
`comedi_buf_map_put()` does nothing if passed a NULL pointer.)
`comedi_buf_map_from_subdev_get()` checks the subdevice's buffer map
pointer has been set and the buffer map has been initialized enough for
`comedi_mmap()` to deal with it (specifically, check the `n_pages`
member has been set to a non-zero value). If all is well, the buffer
map's reference is incremented and a pointer to it is returned. The
comedi subdevice's spin-lock is used to protect the checks. Also use
the spin-lock in `__comedi_buf_alloc()` and `__comedi_buf_free()` to
protect changes to the subdevice's buffer map structure pointer and the
buffer map structure's `n_pages` member. (This checking of `n_pages` is
a bit clunky and I [Ian Abbott] plan to deal with it in the future.)
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Cc: <stable@vger.kernel.org> # 3.14.x, 3.15.x
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2014-04-10 18:41:57 +00:00
|
|
|
up_read(&dev->attach_lock);
|
|
|
|
comedi_buf_map_put(bm); /* put reference to buf map - okay if NULL */
|
2008-11-05 04:29:31 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2012-04-16 10:40:22 +00:00
|
|
|
static unsigned int comedi_poll(struct file *file, poll_table *wait)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
|
|
|
unsigned int mask = 0;
|
2013-02-27 21:59:05 +00:00
|
|
|
const unsigned minor = iminor(file_inode(file));
|
2013-11-08 15:03:40 +00:00
|
|
|
struct comedi_device *dev = file->private_data;
|
2012-12-19 22:39:44 +00:00
|
|
|
struct comedi_subdevice *s;
|
2011-11-08 21:23:03 +00:00
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
mutex_lock(&dev->mutex);
|
2012-12-19 22:39:44 +00:00
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
if (!dev->attached) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "no driver attached\n");
|
2012-12-19 22:39:44 +00:00
|
|
|
goto done;
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
|
|
|
|
2013-04-04 13:59:10 +00:00
|
|
|
s = comedi_read_subdevice(dev, minor);
|
2013-02-05 12:50:40 +00:00
|
|
|
if (s && s->async) {
|
2012-12-19 22:39:44 +00:00
|
|
|
poll_wait(file, &s->async->wait_head, wait);
|
2012-12-19 22:43:18 +00:00
|
|
|
if (!s->busy || !comedi_is_subdevice_running(s) ||
|
2014-05-06 12:12:09 +00:00
|
|
|
comedi_buf_read_n_available(s) > 0)
|
2008-11-05 04:29:31 +00:00
|
|
|
mask |= POLLIN | POLLRDNORM;
|
|
|
|
}
|
2012-12-19 22:39:44 +00:00
|
|
|
|
2013-04-04 13:59:10 +00:00
|
|
|
s = comedi_write_subdevice(dev, minor);
|
2013-02-05 12:50:40 +00:00
|
|
|
if (s && s->async) {
|
2014-05-02 12:50:13 +00:00
|
|
|
unsigned int bps = bytes_per_sample(s);
|
2012-12-19 22:39:44 +00:00
|
|
|
|
|
|
|
poll_wait(file, &s->async->wait_head, wait);
|
2014-05-06 12:12:04 +00:00
|
|
|
comedi_buf_write_alloc(s, s->async->prealloc_bufsz);
|
2012-12-19 22:43:18 +00:00
|
|
|
if (!s->busy || !comedi_is_subdevice_running(s) ||
|
2014-05-06 12:12:06 +00:00
|
|
|
comedi_buf_write_n_allocated(s) >= bps)
|
2008-11-05 04:29:31 +00:00
|
|
|
mask |= POLLOUT | POLLWRNORM;
|
|
|
|
}
|
|
|
|
|
2012-12-19 22:39:44 +00:00
|
|
|
done:
|
2008-11-05 04:29:31 +00:00
|
|
|
mutex_unlock(&dev->mutex);
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
2010-05-03 23:32:28 +00:00
|
|
|
static ssize_t comedi_write(struct file *file, const char __user *buf,
|
|
|
|
size_t nbytes, loff_t *offset)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
2009-03-17 02:05:14 +00:00
|
|
|
struct comedi_subdevice *s;
|
2009-03-17 02:05:20 +00:00
|
|
|
struct comedi_async *async;
|
2008-11-05 04:29:31 +00:00
|
|
|
int n, m, count = 0, retval = 0;
|
|
|
|
DECLARE_WAITQUEUE(wait, current);
|
2013-02-27 21:59:05 +00:00
|
|
|
const unsigned minor = iminor(file_inode(file));
|
2013-11-08 15:03:40 +00:00
|
|
|
struct comedi_device *dev = file->private_data;
|
2013-11-08 15:03:30 +00:00
|
|
|
bool on_wait_queue = false;
|
|
|
|
bool attach_locked;
|
|
|
|
unsigned int old_detach_count;
|
2011-11-08 21:23:03 +00:00
|
|
|
|
2013-11-08 15:03:30 +00:00
|
|
|
/* Protect against device detachment during operation. */
|
|
|
|
down_read(&dev->attach_lock);
|
|
|
|
attach_locked = true;
|
|
|
|
old_detach_count = dev->detach_count;
|
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
if (!dev->attached) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "no driver attached\n");
|
2013-11-08 15:03:30 +00:00
|
|
|
retval = -ENODEV;
|
|
|
|
goto out;
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
|
|
|
|
2013-04-04 13:59:10 +00:00
|
|
|
s = comedi_write_subdevice(dev, minor);
|
2013-11-08 15:03:30 +00:00
|
|
|
if (!s || !s->async) {
|
|
|
|
retval = -EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-12-19 22:40:34 +00:00
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
async = s->async;
|
|
|
|
|
2012-12-19 22:40:34 +00:00
|
|
|
if (!s->busy || !nbytes)
|
2013-11-08 15:03:30 +00:00
|
|
|
goto out;
|
|
|
|
if (s->busy != file) {
|
|
|
|
retval = -EACCES;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-12-19 22:40:34 +00:00
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
add_wait_queue(&async->wait_head, &wait);
|
2013-11-08 15:03:30 +00:00
|
|
|
on_wait_queue = true;
|
2008-11-05 04:29:31 +00:00
|
|
|
while (nbytes > 0 && !retval) {
|
|
|
|
set_current_state(TASK_INTERRUPTIBLE);
|
|
|
|
|
2012-12-19 22:43:18 +00:00
|
|
|
if (!comedi_is_subdevice_running(s)) {
|
2010-05-19 16:22:41 +00:00
|
|
|
if (count == 0) {
|
2013-11-08 15:03:30 +00:00
|
|
|
struct comedi_subdevice *new_s;
|
|
|
|
|
2012-12-19 22:44:02 +00:00
|
|
|
if (comedi_is_subdevice_in_error(s))
|
2010-05-19 16:22:41 +00:00
|
|
|
retval = -EPIPE;
|
2012-12-19 22:44:02 +00:00
|
|
|
else
|
2010-05-19 16:22:41 +00:00
|
|
|
retval = 0;
|
2013-11-08 15:03:30 +00:00
|
|
|
/*
|
|
|
|
* To avoid deadlock, cannot acquire dev->mutex
|
|
|
|
* while dev->attach_lock is held. Need to
|
|
|
|
* remove task from the async wait queue before
|
|
|
|
* releasing dev->attach_lock, as it might not
|
|
|
|
* be valid afterwards.
|
|
|
|
*/
|
|
|
|
remove_wait_queue(&async->wait_head, &wait);
|
|
|
|
on_wait_queue = false;
|
|
|
|
up_read(&dev->attach_lock);
|
|
|
|
attach_locked = false;
|
|
|
|
mutex_lock(&dev->mutex);
|
|
|
|
/*
|
|
|
|
* Become non-busy unless things have changed
|
|
|
|
* behind our back. Checking dev->detach_count
|
|
|
|
* is unchanged ought to be sufficient (unless
|
|
|
|
* there have been 2**32 detaches in the
|
|
|
|
* meantime!), but check the subdevice pointer
|
|
|
|
* as well just in case.
|
|
|
|
*/
|
|
|
|
new_s = comedi_write_subdevice(dev, minor);
|
|
|
|
if (dev->attached &&
|
|
|
|
old_detach_count == dev->detach_count &&
|
|
|
|
s == new_s && new_s->async == async)
|
|
|
|
do_become_nonbusy(dev, s);
|
2013-07-05 15:49:34 +00:00
|
|
|
mutex_unlock(&dev->mutex);
|
2010-05-19 16:22:41 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
n = nbytes;
|
|
|
|
|
|
|
|
m = n;
|
2008-11-14 01:05:58 +00:00
|
|
|
if (async->buf_write_ptr + m > async->prealloc_bufsz)
|
2008-11-05 04:29:31 +00:00
|
|
|
m = async->prealloc_bufsz - async->buf_write_ptr;
|
2014-05-06 12:12:04 +00:00
|
|
|
comedi_buf_write_alloc(s, async->prealloc_bufsz);
|
2014-05-06 12:12:06 +00:00
|
|
|
if (m > comedi_buf_write_n_allocated(s))
|
|
|
|
m = comedi_buf_write_n_allocated(s);
|
2008-11-05 04:29:31 +00:00
|
|
|
if (m < n)
|
|
|
|
n = m;
|
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
if (file->f_flags & O_NONBLOCK) {
|
|
|
|
retval = -EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
2011-10-29 07:47:39 +00:00
|
|
|
schedule();
|
2008-11-05 04:29:31 +00:00
|
|
|
if (signal_pending(current)) {
|
|
|
|
retval = -ERESTARTSYS;
|
|
|
|
break;
|
|
|
|
}
|
2008-11-14 01:05:58 +00:00
|
|
|
if (!s->busy)
|
2008-11-05 04:29:31 +00:00
|
|
|
break;
|
|
|
|
if (s->busy != file) {
|
|
|
|
retval = -EACCES;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
m = copy_from_user(async->prealloc_buf + async->buf_write_ptr,
|
2008-11-14 01:05:58 +00:00
|
|
|
buf, n);
|
2008-11-05 04:29:31 +00:00
|
|
|
if (m) {
|
|
|
|
n -= m;
|
|
|
|
retval = -EFAULT;
|
|
|
|
}
|
2014-05-06 12:12:05 +00:00
|
|
|
comedi_buf_write_free(s, n);
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
count += n;
|
|
|
|
nbytes -= n;
|
|
|
|
|
|
|
|
buf += n;
|
|
|
|
break; /* makes device work like a pipe */
|
|
|
|
}
|
2013-11-08 15:03:30 +00:00
|
|
|
out:
|
|
|
|
if (on_wait_queue)
|
|
|
|
remove_wait_queue(&async->wait_head, &wait);
|
2008-11-05 04:29:31 +00:00
|
|
|
set_current_state(TASK_RUNNING);
|
2013-11-08 15:03:30 +00:00
|
|
|
if (attach_locked)
|
|
|
|
up_read(&dev->attach_lock);
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2008-11-14 01:05:58 +00:00
|
|
|
return count ? count : retval;
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
|
|
|
|
2010-05-03 23:32:28 +00:00
|
|
|
static ssize_t comedi_read(struct file *file, char __user *buf, size_t nbytes,
|
2014-07-18 21:28:14 +00:00
|
|
|
loff_t *offset)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
2009-03-17 02:05:14 +00:00
|
|
|
struct comedi_subdevice *s;
|
2009-03-17 02:05:20 +00:00
|
|
|
struct comedi_async *async;
|
2008-11-05 04:29:31 +00:00
|
|
|
int n, m, count = 0, retval = 0;
|
|
|
|
DECLARE_WAITQUEUE(wait, current);
|
2013-02-27 21:59:05 +00:00
|
|
|
const unsigned minor = iminor(file_inode(file));
|
2013-11-08 15:03:40 +00:00
|
|
|
struct comedi_device *dev = file->private_data;
|
2013-11-08 15:03:31 +00:00
|
|
|
unsigned int old_detach_count;
|
|
|
|
bool become_nonbusy = false;
|
|
|
|
bool attach_locked;
|
2011-11-08 21:23:03 +00:00
|
|
|
|
2013-11-08 15:03:31 +00:00
|
|
|
/* Protect against device detachment during operation. */
|
|
|
|
down_read(&dev->attach_lock);
|
|
|
|
attach_locked = true;
|
|
|
|
old_detach_count = dev->detach_count;
|
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
if (!dev->attached) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "no driver attached\n");
|
2013-11-08 15:03:31 +00:00
|
|
|
retval = -ENODEV;
|
|
|
|
goto out;
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
|
|
|
|
2013-04-04 13:59:10 +00:00
|
|
|
s = comedi_read_subdevice(dev, minor);
|
2013-11-08 15:03:31 +00:00
|
|
|
if (!s || !s->async) {
|
|
|
|
retval = -EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-12-19 22:40:08 +00:00
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
async = s->async;
|
2012-12-19 22:40:08 +00:00
|
|
|
if (!s->busy || !nbytes)
|
2013-11-08 15:03:31 +00:00
|
|
|
goto out;
|
|
|
|
if (s->busy != file) {
|
|
|
|
retval = -EACCES;
|
|
|
|
goto out;
|
|
|
|
}
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
add_wait_queue(&async->wait_head, &wait);
|
|
|
|
while (nbytes > 0 && !retval) {
|
|
|
|
set_current_state(TASK_INTERRUPTIBLE);
|
|
|
|
|
|
|
|
n = nbytes;
|
|
|
|
|
2014-05-06 12:12:09 +00:00
|
|
|
m = comedi_buf_read_n_available(s);
|
2008-11-14 01:05:58 +00:00
|
|
|
if (async->buf_read_ptr + m > async->prealloc_bufsz)
|
2008-11-05 04:29:31 +00:00
|
|
|
m = async->prealloc_bufsz - async->buf_read_ptr;
|
|
|
|
if (m < n)
|
|
|
|
n = m;
|
|
|
|
|
|
|
|
if (n == 0) {
|
2012-12-19 22:43:18 +00:00
|
|
|
if (!comedi_is_subdevice_running(s)) {
|
2012-12-19 22:44:02 +00:00
|
|
|
if (comedi_is_subdevice_in_error(s))
|
2008-11-05 04:29:31 +00:00
|
|
|
retval = -EPIPE;
|
2012-12-19 22:44:02 +00:00
|
|
|
else
|
2008-11-05 04:29:31 +00:00
|
|
|
retval = 0;
|
2013-11-08 15:03:31 +00:00
|
|
|
become_nonbusy = true;
|
2008-11-05 04:29:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (file->f_flags & O_NONBLOCK) {
|
|
|
|
retval = -EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
2011-10-29 07:47:39 +00:00
|
|
|
schedule();
|
2008-11-05 04:29:31 +00:00
|
|
|
if (signal_pending(current)) {
|
|
|
|
retval = -ERESTARTSYS;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!s->busy) {
|
|
|
|
retval = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (s->busy != file) {
|
|
|
|
retval = -EACCES;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
m = copy_to_user(buf, async->prealloc_buf +
|
2008-11-14 01:05:58 +00:00
|
|
|
async->buf_read_ptr, n);
|
2008-11-05 04:29:31 +00:00
|
|
|
if (m) {
|
|
|
|
n -= m;
|
|
|
|
retval = -EFAULT;
|
|
|
|
}
|
|
|
|
|
2014-05-06 12:12:07 +00:00
|
|
|
comedi_buf_read_alloc(s, n);
|
2014-05-06 12:12:08 +00:00
|
|
|
comedi_buf_read_free(s, n);
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
count += n;
|
|
|
|
nbytes -= n;
|
|
|
|
|
|
|
|
buf += n;
|
|
|
|
break; /* makes device work like a pipe */
|
|
|
|
}
|
2013-11-08 15:03:31 +00:00
|
|
|
remove_wait_queue(&async->wait_head, &wait);
|
|
|
|
set_current_state(TASK_RUNNING);
|
|
|
|
if (become_nonbusy || comedi_is_subdevice_idle(s)) {
|
|
|
|
struct comedi_subdevice *new_s;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* To avoid deadlock, cannot acquire dev->mutex
|
|
|
|
* while dev->attach_lock is held.
|
|
|
|
*/
|
|
|
|
up_read(&dev->attach_lock);
|
|
|
|
attach_locked = false;
|
2013-07-05 15:49:34 +00:00
|
|
|
mutex_lock(&dev->mutex);
|
2013-11-08 15:03:31 +00:00
|
|
|
/*
|
|
|
|
* Check device hasn't become detached behind our back.
|
|
|
|
* Checking dev->detach_count is unchanged ought to be
|
|
|
|
* sufficient (unless there have been 2**32 detaches in the
|
|
|
|
* meantime!), but check the subdevice pointer as well just in
|
|
|
|
* case.
|
|
|
|
*/
|
|
|
|
new_s = comedi_read_subdevice(dev, minor);
|
|
|
|
if (dev->attached && old_detach_count == dev->detach_count &&
|
|
|
|
s == new_s && new_s->async == async) {
|
2014-06-20 17:58:28 +00:00
|
|
|
if (become_nonbusy || comedi_buf_n_bytes_ready(s) == 0)
|
2013-11-08 15:03:31 +00:00
|
|
|
do_become_nonbusy(dev, s);
|
|
|
|
}
|
2013-07-05 15:49:34 +00:00
|
|
|
mutex_unlock(&dev->mutex);
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
2013-11-08 15:03:31 +00:00
|
|
|
out:
|
|
|
|
if (attach_locked)
|
|
|
|
up_read(&dev->attach_lock);
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2008-11-14 01:05:58 +00:00
|
|
|
return count ? count : retval;
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int comedi_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
const unsigned minor = iminor(inode);
|
2013-11-08 15:03:34 +00:00
|
|
|
struct comedi_device *dev = comedi_dev_get_from_minor(minor);
|
|
|
|
int rc;
|
2009-02-09 16:51:38 +00:00
|
|
|
|
2012-12-19 22:35:23 +00:00
|
|
|
if (!dev) {
|
2013-11-26 17:21:11 +00:00
|
|
|
pr_debug("invalid minor number\n");
|
2008-11-05 04:29:31 +00:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_lock(&dev->mutex);
|
2009-08-13 13:44:51 +00:00
|
|
|
if (!dev->attached && !capable(CAP_NET_ADMIN)) {
|
2013-11-26 17:21:11 +00:00
|
|
|
dev_dbg(dev->class_dev, "not attached and not CAP_NET_ADMIN\n");
|
2013-11-08 15:03:34 +00:00
|
|
|
rc = -ENODEV;
|
|
|
|
goto out;
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
2013-12-11 14:51:03 +00:00
|
|
|
if (dev->attached && dev->use_count == 0) {
|
2008-11-05 04:29:31 +00:00
|
|
|
if (!try_module_get(dev->driver->module)) {
|
2013-11-08 15:03:34 +00:00
|
|
|
rc = -ENOSYS;
|
|
|
|
goto out;
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
2013-12-11 14:51:03 +00:00
|
|
|
if (dev->open) {
|
|
|
|
rc = dev->open(dev);
|
|
|
|
if (rc < 0) {
|
|
|
|
module_put(dev->driver->module);
|
|
|
|
goto out;
|
|
|
|
}
|
2010-05-19 13:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
dev->use_count++;
|
2013-11-08 15:03:40 +00:00
|
|
|
file->private_data = dev;
|
2013-11-08 15:03:34 +00:00
|
|
|
rc = 0;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2013-11-08 15:03:34 +00:00
|
|
|
out:
|
2012-05-09 16:20:08 +00:00
|
|
|
mutex_unlock(&dev->mutex);
|
2013-11-08 15:03:34 +00:00
|
|
|
if (rc)
|
|
|
|
comedi_dev_put(dev);
|
|
|
|
return rc;
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
|
|
|
|
2012-12-19 22:31:57 +00:00
|
|
|
static int comedi_fasync(int fd, struct file *file, int on)
|
|
|
|
{
|
2013-11-08 15:03:40 +00:00
|
|
|
struct comedi_device *dev = file->private_data;
|
2012-12-19 22:31:57 +00:00
|
|
|
|
|
|
|
return fasync_helper(fd, file, on, &dev->async_queue);
|
|
|
|
}
|
|
|
|
|
2012-05-09 16:20:08 +00:00
|
|
|
static int comedi_close(struct inode *inode, struct file *file)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
2013-11-08 15:03:40 +00:00
|
|
|
struct comedi_device *dev = file->private_data;
|
2012-05-09 16:20:08 +00:00
|
|
|
struct comedi_subdevice *s = NULL;
|
2008-11-05 04:29:31 +00:00
|
|
|
int i;
|
|
|
|
|
2012-05-09 16:20:08 +00:00
|
|
|
mutex_lock(&dev->mutex);
|
|
|
|
|
|
|
|
if (dev->subdevices) {
|
|
|
|
for (i = 0; i < dev->n_subdevices; i++) {
|
2012-09-06 01:20:58 +00:00
|
|
|
s = &dev->subdevices[i];
|
2012-05-09 16:20:08 +00:00
|
|
|
|
|
|
|
if (s->busy == file)
|
|
|
|
do_cancel(dev, s);
|
|
|
|
if (s->lock == file)
|
|
|
|
s->lock = NULL;
|
|
|
|
}
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
2013-12-11 14:51:03 +00:00
|
|
|
if (dev->attached && dev->use_count == 1) {
|
|
|
|
if (dev->close)
|
|
|
|
dev->close(dev);
|
2012-05-09 16:20:08 +00:00
|
|
|
module_put(dev->driver->module);
|
2013-12-11 14:51:03 +00:00
|
|
|
}
|
2012-05-09 16:20:08 +00:00
|
|
|
|
|
|
|
dev->use_count--;
|
|
|
|
|
|
|
|
mutex_unlock(&dev->mutex);
|
2013-11-08 15:03:34 +00:00
|
|
|
comedi_dev_put(dev);
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-19 09:17:43 +00:00
|
|
|
static const struct file_operations comedi_fops = {
|
2012-05-09 16:20:08 +00:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.unlocked_ioctl = comedi_unlocked_ioctl,
|
|
|
|
.compat_ioctl = comedi_compat_ioctl,
|
|
|
|
.open = comedi_open,
|
|
|
|
.release = comedi_close,
|
|
|
|
.read = comedi_read,
|
|
|
|
.write = comedi_write,
|
|
|
|
.mmap = comedi_mmap,
|
|
|
|
.poll = comedi_poll,
|
|
|
|
.fasync = comedi_fasync,
|
|
|
|
.llseek = noop_llseek,
|
|
|
|
};
|
|
|
|
|
|
|
|
void comedi_event(struct comedi_device *dev, struct comedi_subdevice *s)
|
2009-04-14 15:21:41 +00:00
|
|
|
{
|
2012-05-09 16:20:08 +00:00
|
|
|
struct comedi_async *async = s->async;
|
|
|
|
unsigned runflags = 0;
|
|
|
|
unsigned runflags_mask = 0;
|
2009-04-14 15:21:41 +00:00
|
|
|
|
2012-12-19 22:43:18 +00:00
|
|
|
if (!comedi_is_subdevice_running(s))
|
2012-05-09 16:20:08 +00:00
|
|
|
return;
|
|
|
|
|
2014-10-13 16:56:08 +00:00
|
|
|
if (s->async->events & COMEDI_CB_CANCEL_MASK)
|
2012-05-09 16:20:08 +00:00
|
|
|
runflags_mask |= SRF_RUNNING;
|
2014-10-13 16:56:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Remember if an error event has occurred, so an error
|
|
|
|
* can be returned the next time the user does a read().
|
|
|
|
*/
|
|
|
|
if (s->async->events & COMEDI_CB_ERROR_MASK) {
|
2012-05-09 16:20:08 +00:00
|
|
|
runflags_mask |= SRF_ERROR;
|
|
|
|
runflags |= SRF_ERROR;
|
2009-04-14 15:21:41 +00:00
|
|
|
}
|
2012-05-09 16:20:08 +00:00
|
|
|
if (runflags_mask) {
|
|
|
|
/*sets SRF_ERROR and SRF_RUNNING together atomically */
|
|
|
|
comedi_set_subdevice_runflags(s, runflags_mask, runflags);
|
2009-04-14 15:21:41 +00:00
|
|
|
}
|
|
|
|
|
2012-05-09 16:20:08 +00:00
|
|
|
if (async->cb_mask & s->async->events) {
|
2013-11-08 15:03:22 +00:00
|
|
|
wake_up_interruptible(&async->wait_head);
|
|
|
|
if (s->subdev_flags & SDF_CMD_READ)
|
|
|
|
kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
|
|
|
|
if (s->subdev_flags & SDF_CMD_WRITE)
|
|
|
|
kill_fasync(&dev->async_queue, SIGIO, POLL_OUT);
|
2012-05-09 16:20:08 +00:00
|
|
|
}
|
|
|
|
s->async->events = 0;
|
2009-04-14 15:21:41 +00:00
|
|
|
}
|
2013-04-12 17:11:54 +00:00
|
|
|
EXPORT_SYMBOL_GPL(comedi_event);
|
2009-04-14 15:21:41 +00:00
|
|
|
|
2013-04-04 13:58:51 +00:00
|
|
|
/* Note: the ->mutex is pre-locked on successful return */
|
2013-04-04 13:58:50 +00:00
|
|
|
struct comedi_device *comedi_alloc_board_minor(struct device *hardware_device)
|
2012-05-09 16:20:08 +00:00
|
|
|
{
|
2013-04-04 13:58:50 +00:00
|
|
|
struct comedi_device *dev;
|
2012-05-09 16:20:08 +00:00
|
|
|
struct device *csdev;
|
|
|
|
unsigned i;
|
|
|
|
|
2014-07-18 21:28:14 +00:00
|
|
|
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
|
2013-04-04 13:59:16 +00:00
|
|
|
if (dev == NULL)
|
2013-04-04 13:58:50 +00:00
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
comedi_device_init(dev);
|
2013-04-04 13:59:00 +00:00
|
|
|
comedi_set_hw_dev(dev, hardware_device);
|
2013-04-04 13:58:51 +00:00
|
|
|
mutex_lock(&dev->mutex);
|
2013-04-04 13:59:04 +00:00
|
|
|
mutex_lock(&comedi_board_minor_table_lock);
|
2013-04-04 13:58:52 +00:00
|
|
|
for (i = hardware_device ? comedi_num_legacy_minors : 0;
|
|
|
|
i < COMEDI_NUM_BOARD_MINORS; ++i) {
|
2013-04-04 13:59:04 +00:00
|
|
|
if (comedi_board_minor_table[i] == NULL) {
|
2013-04-04 13:59:16 +00:00
|
|
|
comedi_board_minor_table[i] = dev;
|
2012-05-09 16:20:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-04-04 13:59:04 +00:00
|
|
|
mutex_unlock(&comedi_board_minor_table_lock);
|
2012-05-09 16:20:08 +00:00
|
|
|
if (i == COMEDI_NUM_BOARD_MINORS) {
|
2013-04-04 13:58:51 +00:00
|
|
|
mutex_unlock(&dev->mutex);
|
2013-04-04 13:58:50 +00:00
|
|
|
comedi_device_cleanup(dev);
|
2013-11-08 15:03:32 +00:00
|
|
|
comedi_dev_put(dev);
|
2014-07-17 19:27:32 +00:00
|
|
|
pr_err("ran out of minor numbers for board device files\n");
|
2013-04-04 13:58:50 +00:00
|
|
|
return ERR_PTR(-EBUSY);
|
2012-05-09 16:20:08 +00:00
|
|
|
}
|
2013-04-04 13:58:50 +00:00
|
|
|
dev->minor = i;
|
2012-05-09 16:20:08 +00:00
|
|
|
csdev = device_create(comedi_class, hardware_device,
|
|
|
|
MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i", i);
|
|
|
|
if (!IS_ERR(csdev))
|
2013-12-11 14:51:02 +00:00
|
|
|
dev->class_dev = get_device(csdev);
|
2009-04-14 15:21:41 +00:00
|
|
|
|
2013-04-04 13:58:51 +00:00
|
|
|
/* Note: dev->mutex needs to be unlocked by the caller. */
|
2013-04-04 13:58:50 +00:00
|
|
|
return dev;
|
2009-04-14 15:21:41 +00:00
|
|
|
}
|
|
|
|
|
2013-04-04 13:58:49 +00:00
|
|
|
static void comedi_free_board_minor(unsigned minor)
|
2013-04-04 13:58:46 +00:00
|
|
|
{
|
|
|
|
BUG_ON(minor >= COMEDI_NUM_BOARD_MINORS);
|
2013-04-04 13:59:16 +00:00
|
|
|
comedi_free_board_dev(comedi_clear_board_minor(minor));
|
2013-04-04 13:58:46 +00:00
|
|
|
}
|
|
|
|
|
2013-04-04 13:58:47 +00:00
|
|
|
void comedi_release_hardware_device(struct device *hardware_device)
|
2009-04-14 15:21:41 +00:00
|
|
|
{
|
2012-05-09 16:20:08 +00:00
|
|
|
int minor;
|
2013-04-04 13:59:16 +00:00
|
|
|
struct comedi_device *dev;
|
2009-04-14 15:21:41 +00:00
|
|
|
|
2013-04-04 13:58:52 +00:00
|
|
|
for (minor = comedi_num_legacy_minors; minor < COMEDI_NUM_BOARD_MINORS;
|
|
|
|
minor++) {
|
2013-04-04 13:59:04 +00:00
|
|
|
mutex_lock(&comedi_board_minor_table_lock);
|
2013-04-04 13:59:16 +00:00
|
|
|
dev = comedi_board_minor_table[minor];
|
|
|
|
if (dev && dev->hw_dev == hardware_device) {
|
2013-04-04 13:59:04 +00:00
|
|
|
comedi_board_minor_table[minor] = NULL;
|
|
|
|
mutex_unlock(&comedi_board_minor_table_lock);
|
2013-04-04 13:59:16 +00:00
|
|
|
comedi_free_board_dev(dev);
|
2013-04-04 13:58:47 +00:00
|
|
|
break;
|
2012-05-09 16:20:08 +00:00
|
|
|
}
|
2013-04-04 13:59:04 +00:00
|
|
|
mutex_unlock(&comedi_board_minor_table_lock);
|
2009-04-14 15:21:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-01 10:20:30 +00:00
|
|
|
int comedi_alloc_subdevice_minor(struct comedi_subdevice *s)
|
2009-04-14 15:21:41 +00:00
|
|
|
{
|
2013-02-01 10:20:30 +00:00
|
|
|
struct comedi_device *dev = s->device;
|
2012-05-09 16:20:08 +00:00
|
|
|
struct device *csdev;
|
|
|
|
unsigned i;
|
2009-04-14 15:21:41 +00:00
|
|
|
|
2013-04-04 13:59:04 +00:00
|
|
|
mutex_lock(&comedi_subdevice_minor_table_lock);
|
|
|
|
for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i) {
|
|
|
|
if (comedi_subdevice_minor_table[i] == NULL) {
|
2013-04-04 13:59:15 +00:00
|
|
|
comedi_subdevice_minor_table[i] = s;
|
2012-05-09 16:20:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-04-04 13:59:04 +00:00
|
|
|
mutex_unlock(&comedi_subdevice_minor_table_lock);
|
|
|
|
if (i == COMEDI_NUM_SUBDEVICE_MINORS) {
|
2014-07-17 19:27:32 +00:00
|
|
|
pr_err("ran out of minor numbers for subdevice files\n");
|
2012-05-09 16:20:08 +00:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
2013-04-04 13:59:04 +00:00
|
|
|
i += COMEDI_NUM_BOARD_MINORS;
|
2012-05-09 16:20:08 +00:00
|
|
|
s->minor = i;
|
|
|
|
csdev = device_create(comedi_class, dev->class_dev,
|
|
|
|
MKDEV(COMEDI_MAJOR, i), NULL, "comedi%i_subd%i",
|
2012-12-20 00:27:02 +00:00
|
|
|
dev->minor, s->index);
|
2012-05-09 16:20:08 +00:00
|
|
|
if (!IS_ERR(csdev))
|
|
|
|
s->class_dev = csdev;
|
2009-04-14 15:21:41 +00:00
|
|
|
|
2013-02-07 16:03:00 +00:00
|
|
|
return 0;
|
2009-04-14 15:21:41 +00:00
|
|
|
}
|
|
|
|
|
2012-05-09 16:20:08 +00:00
|
|
|
void comedi_free_subdevice_minor(struct comedi_subdevice *s)
|
2009-04-14 15:21:41 +00:00
|
|
|
{
|
2013-04-04 13:59:13 +00:00
|
|
|
unsigned int i;
|
2009-04-14 15:21:41 +00:00
|
|
|
|
2012-05-09 16:20:08 +00:00
|
|
|
if (s == NULL)
|
|
|
|
return;
|
|
|
|
if (s->minor < 0)
|
|
|
|
return;
|
2009-04-14 15:21:41 +00:00
|
|
|
|
2012-05-09 16:20:08 +00:00
|
|
|
BUG_ON(s->minor >= COMEDI_NUM_MINORS);
|
2013-04-04 13:59:03 +00:00
|
|
|
BUG_ON(s->minor < COMEDI_NUM_BOARD_MINORS);
|
2009-04-14 15:21:41 +00:00
|
|
|
|
2013-04-04 13:59:13 +00:00
|
|
|
i = s->minor - COMEDI_NUM_BOARD_MINORS;
|
|
|
|
mutex_lock(&comedi_subdevice_minor_table_lock);
|
2013-04-04 13:59:15 +00:00
|
|
|
if (s == comedi_subdevice_minor_table[i])
|
|
|
|
comedi_subdevice_minor_table[i] = NULL;
|
2013-04-04 13:59:13 +00:00
|
|
|
mutex_unlock(&comedi_subdevice_minor_table_lock);
|
2012-05-09 16:20:08 +00:00
|
|
|
if (s->class_dev) {
|
|
|
|
device_destroy(comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
|
|
|
|
s->class_dev = NULL;
|
2009-04-14 15:21:41 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-19 22:40:59 +00:00
|
|
|
|
2013-01-28 17:07:39 +00:00
|
|
|
static void comedi_cleanup_board_minors(void)
|
2012-12-19 22:41:42 +00:00
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
2013-01-28 17:07:39 +00:00
|
|
|
for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++)
|
2012-12-19 22:41:42 +00:00
|
|
|
comedi_free_board_minor(i);
|
|
|
|
}
|
|
|
|
|
2012-12-19 22:41:19 +00:00
|
|
|
static int __init comedi_init(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int retval;
|
|
|
|
|
2014-07-17 19:27:32 +00:00
|
|
|
pr_info("version " COMEDI_RELEASE " - http://www.comedi.org\n");
|
2012-12-19 22:41:19 +00:00
|
|
|
|
|
|
|
if (comedi_num_legacy_minors < 0 ||
|
|
|
|
comedi_num_legacy_minors > COMEDI_NUM_BOARD_MINORS) {
|
2014-07-17 19:27:32 +00:00
|
|
|
pr_err("invalid value for module parameter \"comedi_num_legacy_minors\". Valid values are 0 through %i.\n",
|
2012-12-19 22:41:19 +00:00
|
|
|
COMEDI_NUM_BOARD_MINORS);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = register_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
|
|
|
|
COMEDI_NUM_MINORS, "comedi");
|
|
|
|
if (retval)
|
|
|
|
return -EIO;
|
|
|
|
cdev_init(&comedi_cdev, &comedi_fops);
|
|
|
|
comedi_cdev.owner = THIS_MODULE;
|
2014-07-09 05:12:37 +00:00
|
|
|
|
|
|
|
retval = kobject_set_name(&comedi_cdev.kobj, "comedi");
|
|
|
|
if (retval) {
|
|
|
|
unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
|
|
|
|
COMEDI_NUM_MINORS);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2012-12-19 22:41:19 +00:00
|
|
|
if (cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS)) {
|
|
|
|
unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
|
|
|
|
COMEDI_NUM_MINORS);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
comedi_class = class_create(THIS_MODULE, "comedi");
|
|
|
|
if (IS_ERR(comedi_class)) {
|
2014-07-17 19:27:32 +00:00
|
|
|
pr_err("failed to create class\n");
|
2012-12-19 22:41:19 +00:00
|
|
|
cdev_del(&comedi_cdev);
|
|
|
|
unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
|
|
|
|
COMEDI_NUM_MINORS);
|
|
|
|
return PTR_ERR(comedi_class);
|
|
|
|
}
|
|
|
|
|
2013-07-24 22:05:25 +00:00
|
|
|
comedi_class->dev_groups = comedi_dev_groups;
|
2012-12-19 22:41:19 +00:00
|
|
|
|
|
|
|
/* XXX requires /proc interface */
|
|
|
|
comedi_proc_init();
|
|
|
|
|
|
|
|
/* create devices files for legacy/manual use */
|
|
|
|
for (i = 0; i < comedi_num_legacy_minors; i++) {
|
2013-04-04 13:58:50 +00:00
|
|
|
struct comedi_device *dev;
|
2014-05-01 08:23:12 +00:00
|
|
|
|
2013-04-04 13:58:50 +00:00
|
|
|
dev = comedi_alloc_board_minor(NULL);
|
|
|
|
if (IS_ERR(dev)) {
|
2013-01-28 17:07:39 +00:00
|
|
|
comedi_cleanup_board_minors();
|
2012-12-19 22:41:19 +00:00
|
|
|
cdev_del(&comedi_cdev);
|
|
|
|
unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0),
|
|
|
|
COMEDI_NUM_MINORS);
|
2013-04-04 13:58:50 +00:00
|
|
|
return PTR_ERR(dev);
|
2012-12-19 22:41:19 +00:00
|
|
|
}
|
2014-07-15 15:11:02 +00:00
|
|
|
/* comedi_alloc_board_minor() locked the mutex */
|
|
|
|
mutex_unlock(&dev->mutex);
|
2012-12-19 22:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
module_init(comedi_init);
|
|
|
|
|
|
|
|
static void __exit comedi_cleanup(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2013-01-28 17:07:39 +00:00
|
|
|
comedi_cleanup_board_minors();
|
2013-04-04 13:59:04 +00:00
|
|
|
for (i = 0; i < COMEDI_NUM_BOARD_MINORS; ++i)
|
|
|
|
BUG_ON(comedi_board_minor_table[i]);
|
|
|
|
for (i = 0; i < COMEDI_NUM_SUBDEVICE_MINORS; ++i)
|
|
|
|
BUG_ON(comedi_subdevice_minor_table[i]);
|
2012-12-19 22:41:19 +00:00
|
|
|
|
|
|
|
class_destroy(comedi_class);
|
|
|
|
cdev_del(&comedi_cdev);
|
|
|
|
unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
|
|
|
|
|
|
|
|
comedi_proc_cleanup();
|
|
|
|
}
|
|
|
|
module_exit(comedi_cleanup);
|
|
|
|
|
2012-12-19 22:40:59 +00:00
|
|
|
MODULE_AUTHOR("http://www.comedi.org");
|
|
|
|
MODULE_DESCRIPTION("Comedi core module");
|
|
|
|
MODULE_LICENSE("GPL");
|