2017-11-07 13:58:43 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2008-11-05 04:29:31 +00:00
|
|
|
/*
|
2015-01-28 18:41:45 +00:00
|
|
|
* comedi/comedi_fops.c
|
|
|
|
* comedi kernel module
|
|
|
|
*
|
|
|
|
* COMEDI - Linux Control and Measurement Device Interface
|
2020-04-25 22:19:30 +00:00
|
|
|
* Copyright (C) 1997-2007 David A. Schleef <ds@schleef.org>
|
|
|
|
* compat ioctls:
|
|
|
|
* Author: Ian Abbott, MEV Ltd. <abbotti@mev.co.uk>
|
|
|
|
* Copyright (C) 2007 MEV Ltd. <http://www.mev.co.uk/>
|
2015-01-28 18:41:45 +00:00
|
|
|
*/
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2014-07-17 19:27:32 +00:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/kernel.h>
|
2017-02-02 18:15:33 +00:00
|
|
|
#include <linux/sched/signal.h>
|
2008-11-05 04:29:31 +00:00
|
|
|
#include <linux/fcntl.h>
|
|
|
|
#include <linux/delay.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/poll.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/fs.h>
|
2021-11-17 12:05:59 +00:00
|
|
|
#include <linux/comedi/comedidev.h>
|
2008-11-05 04:29:31 +00:00
|
|
|
#include <linux/cdev.h>
|
|
|
|
|
2008-11-14 01:05:58 +00:00
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/uaccess.h>
|
2020-04-25 22:19:30 +00:00
|
|
|
#include <linux/compat.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
|
|
|
|
2015-09-18 14:46:23 +00:00
|
|
|
/*
|
2015-04-21 12:18:11 +00:00
|
|
|
* comedi_subdevice "runflags"
|
2015-09-18 14:46:23 +00:00
|
|
|
* COMEDI_SRF_RT: DEPRECATED: command is running real-time
|
|
|
|
* COMEDI_SRF_ERROR: indicates an COMEDI_CB_ERROR event has occurred
|
2015-04-21 12:18:11 +00:00
|
|
|
* since the last command was started
|
2015-09-18 14:46:23 +00:00
|
|
|
* COMEDI_SRF_RUNNING: command is running
|
|
|
|
* COMEDI_SRF_FREE_SPRIV: free s->private on detach
|
2015-04-21 12:18:11 +00:00
|
|
|
*
|
2015-09-18 14:46:23 +00:00
|
|
|
* COMEDI_SRF_BUSY_MASK: runflags that indicate the subdevice is "busy"
|
2015-04-21 12:18:11 +00:00
|
|
|
*/
|
|
|
|
#define COMEDI_SRF_RT BIT(1)
|
|
|
|
#define COMEDI_SRF_ERROR BIT(2)
|
|
|
|
#define COMEDI_SRF_RUNNING BIT(27)
|
|
|
|
#define COMEDI_SRF_FREE_SPRIV BIT(31)
|
|
|
|
|
|
|
|
#define COMEDI_SRF_BUSY_MASK (COMEDI_SRF_ERROR | COMEDI_SRF_RUNNING)
|
|
|
|
|
2014-11-04 18:09:00 +00:00
|
|
|
/**
|
2015-09-23 15:33:27 +00:00
|
|
|
* struct comedi_file - Per-file private data for COMEDI device
|
|
|
|
* @dev: COMEDI device.
|
|
|
|
* @read_subdev: Current "read" subdevice.
|
|
|
|
* @write_subdev: Current "write" subdevice.
|
|
|
|
* @last_detach_count: Last known detach count.
|
|
|
|
* @last_attached: Last known attached/detached state.
|
2014-11-04 18:09:00 +00:00
|
|
|
*/
|
|
|
|
struct comedi_file {
|
|
|
|
struct comedi_device *dev;
|
|
|
|
struct comedi_subdevice *read_subdev;
|
|
|
|
struct comedi_subdevice *write_subdev;
|
|
|
|
unsigned int last_detach_count;
|
2018-06-12 14:50:28 +00:00
|
|
|
unsigned int last_attached:1;
|
2014-11-04 18:09:00 +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
|
|
|
|
2017-03-07 18:13:58 +00:00
|
|
|
static unsigned short comedi_num_legacy_minors;
|
|
|
|
module_param(comedi_num_legacy_minors, ushort, 0444);
|
2012-04-13 13:12:53 +00:00
|
|
|
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;
|
2016-09-12 13:18:59 +00:00
|
|
|
module_param(comedi_default_buf_size_kb, uint, 0644);
|
2012-04-13 13:12:53 +00:00
|
|
|
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
|
|
|
|
2018-07-14 15:44:58 +00:00
|
|
|
unsigned int comedi_default_buf_maxsize_kb =
|
|
|
|
CONFIG_COMEDI_DEFAULT_BUF_MAXSIZE_KB;
|
2016-09-12 13:18:59 +00:00
|
|
|
module_param(comedi_default_buf_maxsize_kb, uint, 0644);
|
2012-04-13 13:12:53 +00:00
|
|
|
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 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);
|
|
|
|
}
|
|
|
|
|
2015-01-28 18:41:46 +00:00
|
|
|
/**
|
2015-09-23 15:33:27 +00:00
|
|
|
* comedi_dev_put() - Release a use of a COMEDI device
|
|
|
|
* @dev: COMEDI device.
|
2015-01-28 18:41:46 +00:00
|
|
|
*
|
2015-09-23 15:33:27 +00:00
|
|
|
* Must be called when a user of a COMEDI device is finished with it.
|
|
|
|
* When the last user of the COMEDI device calls this function, the
|
|
|
|
* COMEDI device is destroyed.
|
2015-01-28 18:41:46 +00:00
|
|
|
*
|
2015-09-23 15:33:27 +00:00
|
|
|
* Return: 1 if the COMEDI device is destroyed by this call or @dev is
|
|
|
|
* NULL, otherwise return 0. Callers must not assume the COMEDI
|
2015-01-28 18:41:46 +00:00
|
|
|
* device is still valid if this function returns 0.
|
|
|
|
*/
|
2013-11-08 15:03:32 +00:00
|
|
|
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;
|
|
|
|
|
2015-03-04 19:15:28 +00:00
|
|
|
if (!dev)
|
2013-04-04 13:58:55 +00:00
|
|
|
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;
|
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
2013-04-04 13:59:18 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-03-21 13:18:35 +00:00
|
|
|
static struct comedi_device *comedi_clear_board_minor(unsigned int 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
|
|
|
}
|
|
|
|
|
2016-03-29 09:49:04 +00:00
|
|
|
static struct comedi_subdevice *
|
|
|
|
comedi_subdevice_from_minor(const struct comedi_device *dev, unsigned int 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;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-03-21 13:18:35 +00:00
|
|
|
static struct comedi_device *comedi_dev_get_from_board_minor(unsigned int minor)
|
2013-11-08 15:03:33 +00:00
|
|
|
{
|
|
|
|
struct comedi_device *dev;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-03-29 09:49:04 +00:00
|
|
|
static struct comedi_device *
|
|
|
|
comedi_dev_get_from_subdevice_minor(unsigned int minor)
|
2013-11-08 15:03:33 +00:00
|
|
|
{
|
|
|
|
struct comedi_device *dev;
|
|
|
|
struct comedi_subdevice *s;
|
|
|
|
unsigned int i = minor - COMEDI_NUM_BOARD_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;
|
|
|
|
}
|
|
|
|
|
2015-01-28 18:41:46 +00:00
|
|
|
/**
|
2015-09-23 15:33:27 +00:00
|
|
|
* comedi_dev_get_from_minor() - Get COMEDI device by minor device number
|
|
|
|
* @minor: Minor device number.
|
2015-01-28 18:41:46 +00:00
|
|
|
*
|
2015-09-23 15:33:27 +00:00
|
|
|
* Finds the COMEDI device associated with the minor device number, if any,
|
|
|
|
* and increments its reference count. The COMEDI device is prevented from
|
2015-01-28 18:41:46 +00:00
|
|
|
* being freed until a matching call is made to comedi_dev_put().
|
|
|
|
*
|
2015-09-23 15:33:27 +00:00
|
|
|
* Return: A pointer to the COMEDI device if it exists, with its usage
|
|
|
|
* reference incremented. Return NULL if no COMEDI device exists with the
|
2015-01-28 18:41:46 +00:00
|
|
|
* specified minor device number.
|
|
|
|
*/
|
2016-03-21 13:18:35 +00:00
|
|
|
struct comedi_device *comedi_dev_get_from_minor(unsigned int minor)
|
2013-11-08 15:03:33 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
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);
|
2015-03-04 19:15:28 +00:00
|
|
|
if (!s || (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
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
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);
|
2015-03-04 19:15:28 +00:00
|
|
|
if (!s || (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
|
|
|
}
|
|
|
|
|
2014-11-04 18:09:00 +00:00
|
|
|
static void comedi_file_reset(struct file *file)
|
|
|
|
{
|
|
|
|
struct comedi_file *cfp = file->private_data;
|
|
|
|
struct comedi_device *dev = cfp->dev;
|
|
|
|
struct comedi_subdevice *s, *read_s, *write_s;
|
|
|
|
unsigned int minor = iminor(file_inode(file));
|
|
|
|
|
|
|
|
read_s = dev->read_subdev;
|
|
|
|
write_s = dev->write_subdev;
|
|
|
|
if (minor >= COMEDI_NUM_BOARD_MINORS) {
|
|
|
|
s = comedi_subdevice_from_minor(dev, minor);
|
2015-03-04 19:15:28 +00:00
|
|
|
if (!s || s->subdev_flags & SDF_CMD_READ)
|
2014-11-04 18:09:00 +00:00
|
|
|
read_s = s;
|
2015-03-04 19:15:28 +00:00
|
|
|
if (!s || s->subdev_flags & SDF_CMD_WRITE)
|
2014-11-04 18:09:00 +00:00
|
|
|
write_s = s;
|
|
|
|
}
|
|
|
|
cfp->last_attached = dev->attached;
|
|
|
|
cfp->last_detach_count = dev->detach_count;
|
2016-05-10 15:51:07 +00:00
|
|
|
WRITE_ONCE(cfp->read_subdev, read_s);
|
|
|
|
WRITE_ONCE(cfp->write_subdev, write_s);
|
2014-11-04 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void comedi_file_check(struct file *file)
|
|
|
|
{
|
|
|
|
struct comedi_file *cfp = file->private_data;
|
|
|
|
struct comedi_device *dev = cfp->dev;
|
|
|
|
|
|
|
|
if (cfp->last_attached != dev->attached ||
|
|
|
|
cfp->last_detach_count != dev->detach_count)
|
|
|
|
comedi_file_reset(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct comedi_subdevice *comedi_file_read_subdevice(struct file *file)
|
|
|
|
{
|
|
|
|
struct comedi_file *cfp = file->private_data;
|
|
|
|
|
|
|
|
comedi_file_check(file);
|
2016-05-10 15:51:07 +00:00
|
|
|
return READ_ONCE(cfp->read_subdev);
|
2014-11-04 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct comedi_subdevice *comedi_file_write_subdevice(struct file *file)
|
|
|
|
{
|
|
|
|
struct comedi_file *cfp = file->private_data;
|
|
|
|
|
|
|
|
comedi_file_check(file);
|
2016-05-10 15:51:07 +00:00
|
|
|
return READ_ONCE(cfp->write_subdev);
|
2014-11-04 18:09:00 +00:00
|
|
|
}
|
|
|
|
|
2009-04-14 15:21:41 +00:00
|
|
|
static int resize_async_buffer(struct comedi_device *dev,
|
2016-03-29 09:49:04 +00:00
|
|
|
struct comedi_subdevice *s,
|
|
|
|
unsigned int 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;
|
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
|
|
|
|
2012-05-09 16:20:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-28 18:41:50 +00:00
|
|
|
/* make sure buffer is an integral number of pages (we round up) */
|
2012-05-09 16:20:08 +00:00
|
|
|
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);
|
2022-09-01 01:34:23 +00:00
|
|
|
return sysfs_emit(buf, "%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);
|
2022-09-01 01:34:23 +00:00
|
|
|
return sysfs_emit(buf, "%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);
|
2022-09-01 01:34:23 +00:00
|
|
|
return sysfs_emit(buf, "%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);
|
2022-09-01 01:34:23 +00:00
|
|
|
return sysfs_emit(buf, "%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
|
|
|
|
2023-06-20 14:41:38 +00:00
|
|
|
static const struct class comedi_class = {
|
|
|
|
.name = "comedi",
|
|
|
|
.dev_groups = comedi_dev_groups,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void comedi_free_board_dev(struct comedi_device *dev)
|
|
|
|
{
|
|
|
|
if (dev) {
|
|
|
|
comedi_device_cleanup(dev);
|
|
|
|
if (dev->class_dev) {
|
|
|
|
device_destroy(&comedi_class,
|
|
|
|
MKDEV(COMEDI_MAJOR, dev->minor));
|
|
|
|
}
|
|
|
|
comedi_dev_put(dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 15:13:06 +00:00
|
|
|
static void __comedi_clear_subdevice_runflags(struct comedi_subdevice *s,
|
2016-03-21 13:18:35 +00:00
|
|
|
unsigned int bits)
|
2015-03-27 15:13:06 +00:00
|
|
|
{
|
|
|
|
s->runflags &= ~bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __comedi_set_subdevice_runflags(struct comedi_subdevice *s,
|
2016-03-21 13:18:35 +00:00
|
|
|
unsigned int bits)
|
2015-03-27 15:13:06 +00:00
|
|
|
{
|
|
|
|
s->runflags |= bits;
|
|
|
|
}
|
|
|
|
|
2015-03-27 15:13:00 +00:00
|
|
|
static void comedi_update_subdevice_runflags(struct comedi_subdevice *s,
|
2016-03-29 09:49:04 +00:00
|
|
|
unsigned int mask,
|
|
|
|
unsigned int bits)
|
2012-12-19 22:31:57 +00:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&s->spin_lock, flags);
|
2015-03-27 15:13:06 +00:00
|
|
|
__comedi_clear_subdevice_runflags(s, mask);
|
|
|
|
__comedi_set_subdevice_runflags(s, bits & mask);
|
2012-12-19 22:31:57 +00:00
|
|
|
spin_unlock_irqrestore(&s->spin_lock, flags);
|
|
|
|
}
|
|
|
|
|
2016-03-21 13:18:35 +00:00
|
|
|
static unsigned int __comedi_get_subdevice_runflags(struct comedi_subdevice *s)
|
2015-03-27 15:13:06 +00:00
|
|
|
{
|
|
|
|
return s->runflags;
|
|
|
|
}
|
|
|
|
|
2016-03-21 13:18:35 +00:00
|
|
|
static unsigned int comedi_get_subdevice_runflags(struct comedi_subdevice *s)
|
2012-12-19 22:42:26 +00:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
2016-03-21 13:18:35 +00:00
|
|
|
unsigned int runflags;
|
2012-12-19 22:42:26 +00:00
|
|
|
|
|
|
|
spin_lock_irqsave(&s->spin_lock, flags);
|
2015-03-27 15:13:06 +00:00
|
|
|
runflags = __comedi_get_subdevice_runflags(s);
|
2012-12-19 22:42:26 +00:00
|
|
|
spin_unlock_irqrestore(&s->spin_lock, flags);
|
|
|
|
return runflags;
|
|
|
|
}
|
|
|
|
|
2016-03-21 13:18:35 +00:00
|
|
|
static bool comedi_is_runflags_running(unsigned int runflags)
|
2015-03-27 15:13:01 +00:00
|
|
|
{
|
|
|
|
return runflags & COMEDI_SRF_RUNNING;
|
|
|
|
}
|
|
|
|
|
2016-03-21 13:18:35 +00:00
|
|
|
static bool comedi_is_runflags_in_error(unsigned int runflags)
|
2015-03-27 15:13:01 +00:00
|
|
|
{
|
|
|
|
return runflags & COMEDI_SRF_ERROR;
|
|
|
|
}
|
|
|
|
|
2015-01-28 18:41:46 +00:00
|
|
|
/**
|
2015-09-23 15:33:27 +00:00
|
|
|
* comedi_is_subdevice_running() - Check if async command running on subdevice
|
|
|
|
* @s: COMEDI subdevice.
|
2015-01-28 18:41:46 +00:00
|
|
|
*
|
2015-09-23 15:33:27 +00:00
|
|
|
* Return: %true if an asynchronous COMEDI command is active on the
|
|
|
|
* subdevice, else %false.
|
2015-01-28 18:41:46 +00:00
|
|
|
*/
|
2012-12-19 22:42:47 +00:00
|
|
|
bool comedi_is_subdevice_running(struct comedi_subdevice *s)
|
|
|
|
{
|
2016-03-21 13:18:35 +00:00
|
|
|
unsigned int runflags = comedi_get_subdevice_runflags(s);
|
2012-12-19 22:42:47 +00:00
|
|
|
|
2015-03-27 15:13:01 +00:00
|
|
|
return comedi_is_runflags_running(runflags);
|
2012-12-19 22:42:47 +00:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(comedi_is_subdevice_running);
|
|
|
|
|
2015-03-27 15:13:06 +00:00
|
|
|
static bool __comedi_is_subdevice_running(struct comedi_subdevice *s)
|
|
|
|
{
|
2016-03-21 13:18:35 +00:00
|
|
|
unsigned int runflags = __comedi_get_subdevice_runflags(s);
|
2015-03-27 15:13:06 +00:00
|
|
|
|
|
|
|
return comedi_is_runflags_running(runflags);
|
|
|
|
}
|
|
|
|
|
2015-04-21 12:18:10 +00:00
|
|
|
bool comedi_can_auto_free_spriv(struct comedi_subdevice *s)
|
|
|
|
{
|
2016-03-21 13:18:35 +00:00
|
|
|
unsigned int runflags = __comedi_get_subdevice_runflags(s);
|
2015-04-21 12:18:10 +00:00
|
|
|
|
|
|
|
return runflags & COMEDI_SRF_FREE_SPRIV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-23 15:33:27 +00:00
|
|
|
* comedi_set_spriv_auto_free() - Mark subdevice private data as freeable
|
|
|
|
* @s: COMEDI subdevice.
|
2015-04-21 12:18:10 +00:00
|
|
|
*
|
|
|
|
* Mark the subdevice as having a pointer to private data that can be
|
2015-09-23 15:33:27 +00:00
|
|
|
* automatically freed when the COMEDI device is detached from the low-level
|
|
|
|
* driver.
|
2015-04-21 12:18:10 +00:00
|
|
|
*/
|
|
|
|
void comedi_set_spriv_auto_free(struct comedi_subdevice *s)
|
|
|
|
{
|
|
|
|
__comedi_set_subdevice_runflags(s, COMEDI_SRF_FREE_SPRIV);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(comedi_set_spriv_auto_free);
|
|
|
|
|
2013-06-11 18:32:29 +00:00
|
|
|
/**
|
2015-09-23 15:33:27 +00:00
|
|
|
* comedi_alloc_spriv - Allocate memory for the subdevice private data
|
|
|
|
* @s: COMEDI subdevice.
|
|
|
|
* @size: Size of the memory to allocate.
|
2013-06-11 18:32:29 +00:00
|
|
|
*
|
2015-09-23 15:33:27 +00:00
|
|
|
* Allocate memory for the subdevice private data and point @s->private
|
|
|
|
* to it. The memory will be freed automatically when the COMEDI device
|
|
|
|
* is detached from the low-level driver.
|
|
|
|
*
|
|
|
|
* Return: A pointer to the allocated memory @s->private on success.
|
|
|
|
* Return NULL on failure.
|
2013-06-11 18:32:29 +00:00
|
|
|
*/
|
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)
|
2015-04-21 12:18:10 +00:00
|
|
|
comedi_set_spriv_auto_free(s);
|
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
|
|
|
/*
|
2015-01-28 18:41:50 +00:00
|
|
|
* This function restores a subdevice to an idle state.
|
2012-12-19 22:31:57 +00:00
|
|
|
*/
|
|
|
|
static void do_become_nonbusy(struct comedi_device *dev,
|
|
|
|
struct comedi_subdevice *s)
|
|
|
|
{
|
|
|
|
struct comedi_async *async = s->async;
|
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
2015-03-27 15:13:00 +00:00
|
|
|
comedi_update_subdevice_runflags(s, COMEDI_SRF_RUNNING, 0);
|
2012-12-19 22:31:57 +00:00
|
|
|
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;
|
2015-03-27 15:13:03 +00:00
|
|
|
wake_up_interruptible_all(&async->wait_head);
|
2012-12-19 22:31:57 +00:00
|
|
|
} else {
|
|
|
|
dev_err(dev->class_dev,
|
2017-08-23 22:37:00 +00:00
|
|
|
"BUG: (?) %s called with async=NULL\n", __func__);
|
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;
|
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
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;
|
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
2013-11-08 15:03:28 +00:00
|
|
|
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;
|
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
2012-12-19 22:31:57 +00:00
|
|
|
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
|
|
|
/*
|
2015-01-28 18:41:47 +00:00
|
|
|
* COMEDI_DEVCONFIG ioctl
|
|
|
|
* attaches (and configures) or detaches a legacy device
|
|
|
|
*
|
|
|
|
* arg:
|
|
|
|
* pointer to comedi_devconfig structure (NULL if detaching)
|
|
|
|
*
|
|
|
|
* reads:
|
|
|
|
* comedi_devconfig structure (if attaching)
|
|
|
|
*
|
|
|
|
* writes:
|
|
|
|
* nothing
|
|
|
|
*/
|
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
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
2008-11-05 04:29:31 +00:00
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
|
2015-03-04 19:15:28 +00:00
|
|
|
if (!arg) {
|
2008-11-05 04:29:31 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-01-28 18:41:47 +00:00
|
|
|
* COMEDI_BUFCONFIG ioctl
|
|
|
|
* buffer configuration
|
|
|
|
*
|
|
|
|
* arg:
|
|
|
|
* pointer to comedi_bufconfig structure
|
|
|
|
*
|
|
|
|
* reads:
|
|
|
|
* comedi_bufconfig structure
|
|
|
|
*
|
|
|
|
* writes:
|
|
|
|
* modified comedi_bufconfig structure
|
|
|
|
*/
|
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
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-01-28 18:41:47 +00:00
|
|
|
* COMEDI_DEVINFO ioctl
|
|
|
|
* device info
|
|
|
|
*
|
|
|
|
* arg:
|
|
|
|
* pointer to comedi_devinfo structure
|
|
|
|
*
|
|
|
|
* reads:
|
|
|
|
* nothing
|
|
|
|
*
|
|
|
|
* writes:
|
|
|
|
* comedi_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
|
|
|
{
|
2012-12-19 22:39:18 +00:00
|
|
|
struct comedi_subdevice *s;
|
|
|
|
struct comedi_devinfo devinfo;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
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;
|
2021-01-31 17:28:22 +00:00
|
|
|
strscpy(devinfo.driver_name, dev->driver->driver_name, COMEDI_NAMELEN);
|
|
|
|
strscpy(devinfo.board_name, dev->board_name, COMEDI_NAMELEN);
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2014-11-04 18:09:00 +00:00
|
|
|
s = comedi_file_read_subdevice(file);
|
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
|
|
|
|
2014-11-04 18:09:00 +00:00
|
|
|
s = comedi_file_write_subdevice(file);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-01-28 18:41:47 +00:00
|
|
|
* COMEDI_SUBDINFO ioctl
|
|
|
|
* subdevices info
|
|
|
|
*
|
|
|
|
* arg:
|
|
|
|
* pointer to array of comedi_subdinfo structures
|
|
|
|
*
|
|
|
|
* reads:
|
|
|
|
* nothing
|
|
|
|
*
|
|
|
|
* writes:
|
|
|
|
* array of comedi_subdinfo structures
|
|
|
|
*/
|
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
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-01-28 18:41:47 +00:00
|
|
|
* COMEDI_CHANINFO ioctl
|
|
|
|
* subdevice channel info
|
|
|
|
*
|
|
|
|
* arg:
|
|
|
|
* pointer to comedi_chaninfo structure
|
|
|
|
*
|
|
|
|
* reads:
|
|
|
|
* comedi_chaninfo structure
|
|
|
|
*
|
|
|
|
* writes:
|
|
|
|
* array of maxdata values to chaninfo->maxdata_list if requested
|
|
|
|
* array of range table lengths to chaninfo->range_table_list if requested
|
|
|
|
*/
|
2009-06-08 15:34:41 +00:00
|
|
|
static int do_chaninfo_ioctl(struct comedi_device *dev,
|
2020-04-25 22:35:03 +00:00
|
|
|
struct comedi_chaninfo *it)
|
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
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2020-04-25 22:35:03 +00:00
|
|
|
if (it->subdev >= dev->n_subdevices)
|
2008-11-05 04:29:31 +00:00
|
|
|
return -EINVAL;
|
2020-04-25 22:35:03 +00:00
|
|
|
s = &dev->subdevices[it->subdev];
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2020-04-25 22:35:03 +00:00
|
|
|
if (it->maxdata_list) {
|
2008-11-05 04:29:31 +00:00
|
|
|
if (s->maxdata || !s->maxdata_list)
|
|
|
|
return -EINVAL;
|
2020-04-25 22:35:03 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-25 22:35:03 +00:00
|
|
|
if (it->flaglist)
|
2013-10-07 15:50:05 +00:00
|
|
|
return -EINVAL; /* flaglist not supported */
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2020-04-25 22:35:03 +00:00
|
|
|
if (it->rangelist) {
|
2008-11-05 04:29:31 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!s->range_table_list)
|
|
|
|
return -EINVAL;
|
|
|
|
for (i = 0; i < s->n_chan; i++) {
|
|
|
|
int x;
|
|
|
|
|
2020-04-25 22:35:03 +00:00
|
|
|
x = (dev->minor << 28) | (it->subdev << 24) | (i << 16) |
|
2008-11-14 01:05:58 +00:00
|
|
|
(s->range_table_list[i]->length);
|
2020-04-25 22:35:03 +00:00
|
|
|
if (put_user(x, it->rangelist + i))
|
2010-09-05 18:32:33 +00:00
|
|
|
return -EFAULT;
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-28 18:41:47 +00:00
|
|
|
/*
|
|
|
|
* COMEDI_BUFINFO ioctl
|
|
|
|
* buffer information
|
|
|
|
*
|
|
|
|
* arg:
|
|
|
|
* pointer to comedi_bufinfo structure
|
|
|
|
*
|
|
|
|
* reads:
|
|
|
|
* comedi_bufinfo structure
|
|
|
|
*
|
|
|
|
* writes:
|
|
|
|
* modified comedi_bufinfo structure
|
|
|
|
*/
|
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;
|
2016-02-19 16:13:56 +00:00
|
|
|
unsigned int runflags;
|
|
|
|
int retval = 0;
|
2016-02-19 16:13:52 +00:00
|
|
|
bool become_nonbusy = false;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
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;
|
|
|
|
|
2016-02-19 16:13:54 +00:00
|
|
|
if (!async || s->busy != file)
|
|
|
|
return -EINVAL;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2016-02-19 16:13:57 +00:00
|
|
|
runflags = comedi_get_subdevice_runflags(s);
|
2016-02-19 16:13:51 +00:00
|
|
|
if (!(async->cmd.flags & CMDF_WRITE)) {
|
|
|
|
/* command was set up in "read" direction */
|
|
|
|
if (bi.bytes_read) {
|
|
|
|
comedi_buf_read_alloc(s, bi.bytes_read);
|
|
|
|
bi.bytes_read = comedi_buf_read_free(s, bi.bytes_read);
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
2016-02-19 16:13:56 +00:00
|
|
|
/*
|
|
|
|
* If nothing left to read, and command has stopped, and
|
|
|
|
* {"read" position not updated or command stopped normally},
|
|
|
|
* then become non-busy.
|
|
|
|
*/
|
|
|
|
if (comedi_buf_read_n_available(s) == 0 &&
|
|
|
|
!comedi_is_runflags_running(runflags) &&
|
|
|
|
(bi.bytes_read == 0 ||
|
|
|
|
!comedi_is_runflags_in_error(runflags))) {
|
2016-02-19 16:13:55 +00:00
|
|
|
become_nonbusy = true;
|
2016-02-19 16:13:56 +00:00
|
|
|
if (comedi_is_runflags_in_error(runflags))
|
|
|
|
retval = -EPIPE;
|
|
|
|
}
|
2016-02-19 16:13:51 +00:00
|
|
|
bi.bytes_written = 0;
|
|
|
|
} else {
|
|
|
|
/* command was set up in "write" direction */
|
2016-02-19 16:13:57 +00:00
|
|
|
if (!comedi_is_runflags_running(runflags)) {
|
2016-02-19 16:13:53 +00:00
|
|
|
bi.bytes_written = 0;
|
2016-02-19 16:13:57 +00:00
|
|
|
become_nonbusy = true;
|
|
|
|
if (comedi_is_runflags_in_error(runflags))
|
|
|
|
retval = -EPIPE;
|
|
|
|
} else if (bi.bytes_written) {
|
2016-02-19 16:13:51 +00:00
|
|
|
comedi_buf_write_alloc(s, bi.bytes_written);
|
|
|
|
bi.bytes_written =
|
|
|
|
comedi_buf_write_free(s, bi.bytes_written);
|
|
|
|
}
|
|
|
|
bi.bytes_read = 0;
|
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;
|
|
|
|
|
2016-02-19 16:13:52 +00:00
|
|
|
if (become_nonbusy)
|
|
|
|
do_become_nonbusy(dev, s);
|
|
|
|
|
2016-02-19 16:13:56 +00:00
|
|
|
if (retval)
|
|
|
|
return retval;
|
|
|
|
|
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:
|
2023-01-03 15:11:27 +00:00
|
|
|
case INSN_CONFIG_GET_PWM_OUTPUT:
|
2008-11-05 04:29:31 +00:00
|
|
|
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:
|
2018-10-30 14:17:13 +00:00
|
|
|
case INSN_CONFIG_TIMER_1:
|
2008-11-05 04:29:31 +00:00
|
|
|
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;
|
2018-09-19 16:51:03 +00:00
|
|
|
case INSN_CONFIG_GET_CMD_TIMING_CONSTRAINTS:
|
|
|
|
if (insn->n >= 4)
|
|
|
|
return 0;
|
|
|
|
break;
|
2015-01-28 18:41:50 +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;
|
|
|
|
}
|
|
|
|
|
2018-10-03 20:56:02 +00:00
|
|
|
static int check_insn_device_config_length(struct comedi_insn *insn,
|
|
|
|
unsigned int *data)
|
|
|
|
{
|
|
|
|
if (insn->n < 1)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
switch (data[0]) {
|
|
|
|
case INSN_DEVICE_CONFIG_TEST_ROUTE:
|
|
|
|
case INSN_DEVICE_CONFIG_CONNECT_ROUTE:
|
|
|
|
case INSN_DEVICE_CONFIG_DISCONNECT_ROUTE:
|
|
|
|
if (insn->n == 3)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
case INSN_DEVICE_CONFIG_GET_ROUTES:
|
|
|
|
/*
|
|
|
|
* Big enough for config_id and the length of the userland
|
|
|
|
* memory buffer. Additional length should be in factors of 2
|
|
|
|
* to communicate any returned route pairs (source,destination).
|
|
|
|
*/
|
|
|
|
if (insn->n >= 2)
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get_valid_routes() - Calls low-level driver get_valid_routes function to
|
|
|
|
* either return a count of valid routes to user, or copy
|
|
|
|
* of list of all valid device routes to buffer in
|
|
|
|
* userspace.
|
|
|
|
* @dev: comedi device pointer
|
|
|
|
* @data: data from user insn call. The length of the data must be >= 2.
|
|
|
|
* data[0] must contain the INSN_DEVICE_CONFIG config_id.
|
|
|
|
* data[1](input) contains the number of _pairs_ for which memory is
|
|
|
|
* allotted from the user. If the user specifies '0', then only
|
|
|
|
* the number of pairs available is returned.
|
|
|
|
* data[1](output) returns either the number of pairs available (if none
|
|
|
|
* where requested) or the number of _pairs_ that are copied back
|
|
|
|
* to the user.
|
|
|
|
* data[2::2] returns each (source, destination) pair.
|
|
|
|
*
|
|
|
|
* Return: -EINVAL if low-level driver does not allocate and return routes as
|
|
|
|
* expected. Returns 0 otherwise.
|
|
|
|
*/
|
|
|
|
static int get_valid_routes(struct comedi_device *dev, unsigned int *data)
|
|
|
|
{
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
2018-10-03 20:56:02 +00:00
|
|
|
data[1] = dev->get_valid_routes(dev, data[1], data + 2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
2008-11-05 04:29:31 +00:00
|
|
|
if (insn->insn & INSN_MASK_SPECIAL) {
|
|
|
|
/* a non-subdevice instruction */
|
|
|
|
|
|
|
|
switch (insn->insn) {
|
|
|
|
case INSN_GTOD:
|
|
|
|
{
|
2016-06-17 19:56:15 +00:00
|
|
|
struct timespec64 tv;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
if (insn->n != 2) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-06-17 19:56:15 +00:00
|
|
|
ktime_get_real_ts64(&tv);
|
|
|
|
/* unsigned data safe until 2106 */
|
|
|
|
data[0] = (unsigned int)tv.tv_sec;
|
|
|
|
data[1] = tv.tv_nsec / NSEC_PER_USEC;
|
2008-11-05 04:29:31 +00:00
|
|
|
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;
|
2018-10-03 20:56:02 +00:00
|
|
|
case INSN_DEVICE_CONFIG:
|
|
|
|
ret = check_insn_device_config_length(insn, data);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (data[0] == INSN_DEVICE_CONFIG_GET_ROUTES) {
|
|
|
|
/*
|
|
|
|
* data[1] should be the number of _pairs_ that
|
|
|
|
* the memory can hold.
|
|
|
|
*/
|
|
|
|
data[1] = (insn->n - 2) / 2;
|
|
|
|
ret = get_valid_routes(dev, data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* other global device config instructions. */
|
|
|
|
ret = dev->insn_device_config(dev, insn, data);
|
|
|
|
break;
|
2008-11-05 04:29:31 +00:00
|
|
|
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. */
|
2015-10-13 15:37:47 +00:00
|
|
|
s->busy = parse_insn;
|
2008-11-05 04:29:31 +00:00
|
|
|
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 {
|
2015-01-28 18:41:50 +00:00
|
|
|
/*
|
|
|
|
* Most drivers ignore the base channel in
|
2011-01-18 17:44:33 +00:00
|
|
|
* insn->chanspec. Fix this here if
|
2015-01-28 18:41:50 +00:00
|
|
|
* 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
|
|
|
/*
|
2015-01-28 18:41:47 +00:00
|
|
|
* COMEDI_INSNLIST ioctl
|
|
|
|
* synchronous instruction list
|
2013-01-21 21:02:59 +00:00
|
|
|
*
|
2015-01-28 18:41:47 +00:00
|
|
|
* arg:
|
|
|
|
* pointer to comedi_insnlist structure
|
2013-01-21 21:02:59 +00:00
|
|
|
*
|
2015-01-28 18:41:47 +00:00
|
|
|
* reads:
|
|
|
|
* comedi_insnlist structure
|
|
|
|
* array of comedi_insn structures from insnlist->insns pointer
|
|
|
|
* data (for writes) from insns[].data pointers
|
2013-01-21 21:02:59 +00:00
|
|
|
*
|
2015-01-28 18:41:47 +00:00
|
|
|
* writes:
|
|
|
|
* data (for reads) to insns[].data pointers
|
2013-01-21 21:02:59 +00:00
|
|
|
*/
|
|
|
|
/* arbitrary limits */
|
2018-12-04 19:07:50 +00:00
|
|
|
#define MIN_SAMPLES 16
|
|
|
|
#define MAX_SAMPLES 65536
|
2013-01-21 21:02:59 +00:00
|
|
|
static int do_insnlist_ioctl(struct comedi_device *dev,
|
2020-04-26 00:33:04 +00:00
|
|
|
struct comedi_insn *insns,
|
|
|
|
unsigned int n_insns,
|
|
|
|
void *file)
|
2013-01-21 21:02:59 +00:00
|
|
|
{
|
|
|
|
unsigned int *data = NULL;
|
2018-12-04 19:07:50 +00:00
|
|
|
unsigned int max_n_data_required = MIN_SAMPLES;
|
2013-01-21 21:02:59 +00:00
|
|
|
int i = 0;
|
|
|
|
int ret = 0;
|
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
2013-01-21 21:02:59 +00:00
|
|
|
|
2018-12-04 19:07:50 +00:00
|
|
|
/* Determine maximum memory needed for all instructions. */
|
2020-04-26 00:33:04 +00:00
|
|
|
for (i = 0; i < n_insns; ++i) {
|
2013-01-21 21:02:59 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-12-04 19:07:50 +00:00
|
|
|
max_n_data_required = max(max_n_data_required, insns[i].n);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate scratch space for all instruction data. */
|
|
|
|
data = kmalloc_array(max_n_data_required, sizeof(unsigned int),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!data) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2020-04-26 00:33:04 +00:00
|
|
|
for (i = 0; i < n_insns; ++i) {
|
2013-01-21 21:02:59 +00:00
|
|
|
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(data);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
/*
|
2015-01-28 18:41:47 +00:00
|
|
|
* COMEDI_INSN ioctl
|
|
|
|
* synchronous instruction
|
2008-11-05 04:29:31 +00:00
|
|
|
*
|
2015-01-28 18:41:47 +00:00
|
|
|
* arg:
|
|
|
|
* pointer to comedi_insn structure
|
2008-11-05 04:29:31 +00:00
|
|
|
*
|
2015-01-28 18:41:47 +00:00
|
|
|
* reads:
|
|
|
|
* comedi_insn structure
|
|
|
|
* data (for writes) from insn->data pointer
|
2008-11-05 04:29:31 +00:00
|
|
|
*
|
2015-01-28 18:41:47 +00:00
|
|
|
* writes:
|
|
|
|
* data (for reads) to insn->data pointer
|
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,
|
2020-04-26 00:11:57 +00:00
|
|
|
struct comedi_insn *insn, void *file)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
2009-03-17 02:05:02 +00:00
|
|
|
unsigned int *data = NULL;
|
2018-12-04 19:07:50 +00:00
|
|
|
unsigned int n_data = MIN_SAMPLES;
|
2008-11-05 04:29:31 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
2019-01-15 15:36:56 +00:00
|
|
|
|
2020-04-26 00:11:57 +00:00
|
|
|
n_data = max(n_data, insn->n);
|
2018-12-04 19:07:50 +00:00
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
/* This is where the behavior of insn and insnlist deviate. */
|
2020-04-26 00:11:57 +00:00
|
|
|
if (insn->n > MAX_SAMPLES) {
|
|
|
|
insn->n = MAX_SAMPLES;
|
2018-12-04 19:07:50 +00:00
|
|
|
n_data = MAX_SAMPLES;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = kmalloc_array(n_data, sizeof(unsigned int), GFP_KERNEL);
|
|
|
|
if (!data) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2020-04-26 00:11:57 +00:00
|
|
|
if (insn->insn & INSN_MASK_WRITE) {
|
2010-05-13 09:44:39 +00:00
|
|
|
if (copy_from_user(data,
|
2020-04-26 00:11:57 +00:00
|
|
|
insn->data,
|
|
|
|
insn->n * sizeof(unsigned int))) {
|
2008-11-05 04:29:31 +00:00
|
|
|
ret = -EFAULT;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2020-04-26 00:11:57 +00:00
|
|
|
ret = parse_insn(dev, insn, data, file);
|
2008-11-05 04:29:31 +00:00
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
2020-04-26 00:11:57 +00:00
|
|
|
if (insn->insn & INSN_MASK_READ) {
|
|
|
|
if (copy_to_user(insn->data,
|
2010-05-13 09:44:39 +00:00
|
|
|
data,
|
2020-04-26 00:11:57 +00:00
|
|
|
insn->n * sizeof(unsigned int))) {
|
2008-11-05 04:29:31 +00:00
|
|
|
ret = -EFAULT;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2020-04-26 00:11:57 +00:00
|
|
|
ret = insn->n;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
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 *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
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
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;
|
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
2014-10-20 14:10:40 +00:00
|
|
|
cmd->chanlist = NULL;
|
2023-11-03 11:29:33 +00:00
|
|
|
chanlist = memdup_array_user(user_chanlist,
|
|
|
|
cmd->chanlist_len, sizeof(unsigned int));
|
2014-03-06 19:02:57 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-01-28 18:41:47 +00:00
|
|
|
/*
|
|
|
|
* COMEDI_CMD ioctl
|
|
|
|
* asynchronous acquisition command set-up
|
|
|
|
*
|
|
|
|
* arg:
|
|
|
|
* pointer to comedi_cmd structure
|
|
|
|
*
|
|
|
|
* reads:
|
|
|
|
* comedi_cmd structure
|
|
|
|
* channel/range list from cmd->chanlist pointer
|
|
|
|
*
|
|
|
|
* writes:
|
|
|
|
* possibly modified comedi_cmd structure (when -EAGAIN returned)
|
|
|
|
*/
|
2014-03-06 19:02:56 +00:00
|
|
|
static int do_cmd_ioctl(struct comedi_device *dev,
|
2020-04-26 13:01:49 +00:00
|
|
|
struct comedi_cmd *cmd, bool *copy, void *file)
|
2014-03-06 19:02:56 +00:00
|
|
|
{
|
|
|
|
struct comedi_subdevice *s;
|
|
|
|
struct comedi_async *async;
|
|
|
|
unsigned int __user *user_chanlist;
|
|
|
|
int ret;
|
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
|
|
|
|
2020-04-26 13:01:49 +00:00
|
|
|
/* do some simple cmd validation */
|
|
|
|
ret = __comedi_get_user_cmd(dev, cmd);
|
2014-03-06 19:02:56 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* save user's chanlist pointer so it can be restored later */
|
2020-04-26 13:01:49 +00:00
|
|
|
user_chanlist = (unsigned int __user *)cmd->chanlist;
|
2014-03-06 19:02:56 +00:00
|
|
|
|
2020-04-26 13:01:49 +00:00
|
|
|
s = &dev->subdevices[cmd->subdev];
|
2014-03-06 19:02:56 +00:00
|
|
|
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 */
|
2020-04-26 13:01:49 +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",
|
2020-04-26 13:01:49 +00:00
|
|
|
cmd->chanlist_len);
|
2013-07-05 15:49:34 +00:00
|
|
|
return -EINVAL;
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 13:01:49 +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);
|
2020-04-26 13:01:49 +00:00
|
|
|
*cmd = async->cmd;
|
2008-11-14 01:05:58 +00:00
|
|
|
/* restore chanlist pointer before copying back */
|
2020-04-26 13:01:49 +00:00
|
|
|
cmd->chanlist = (unsigned int __force *)user_chanlist;
|
|
|
|
cmd->data = NULL;
|
|
|
|
*copy = true;
|
2008-11-05 04:29:31 +00:00
|
|
|
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;
|
|
|
|
|
2015-03-27 15:13:00 +00:00
|
|
|
comedi_update_subdevice_runflags(s, COMEDI_SRF_BUSY_MASK,
|
|
|
|
COMEDI_SRF_RUNNING);
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2015-01-20 19:06:00 +00:00
|
|
|
/*
|
|
|
|
* Set s->busy _after_ setting COMEDI_SRF_RUNNING flag to avoid
|
|
|
|
* race with comedi_read() or comedi_write().
|
|
|
|
*/
|
2013-07-05 15:49:34 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-01-28 18:41:47 +00:00
|
|
|
* COMEDI_CMDTEST ioctl
|
2015-04-24 13:40:42 +00:00
|
|
|
* asynchronous acquisition command testing
|
2015-01-28 18:41:47 +00:00
|
|
|
*
|
|
|
|
* arg:
|
|
|
|
* pointer to comedi_cmd structure
|
|
|
|
*
|
|
|
|
* reads:
|
|
|
|
* comedi_cmd structure
|
|
|
|
* channel/range list from cmd->chanlist pointer
|
|
|
|
*
|
|
|
|
* writes:
|
|
|
|
* possibly modified comedi_cmd structure
|
|
|
|
*/
|
2010-05-03 23:32:28 +00:00
|
|
|
static int do_cmdtest_ioctl(struct comedi_device *dev,
|
2020-04-26 12:56:55 +00:00
|
|
|
struct comedi_cmd *cmd, bool *copy, void *file)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
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;
|
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
|
|
|
|
2020-04-26 12:56:55 +00:00
|
|
|
/* do some simple cmd validation */
|
|
|
|
ret = __comedi_get_user_cmd(dev, cmd);
|
2014-03-06 19:02:56 +00:00
|
|
|
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 */
|
2020-04-26 12:56:55 +00:00
|
|
|
user_chanlist = (unsigned int __user *)cmd->chanlist;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2020-04-26 12:56:55 +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 */
|
2020-04-26 12:56:55 +00:00
|
|
|
ret = __comedi_get_user_chanlist(dev, s, user_chanlist, cmd);
|
2014-10-08 15:09:14 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2020-04-26 12:56:55 +00:00
|
|
|
ret = s->do_cmdtest(dev, s, cmd);
|
2008-11-05 04:29:31 +00:00
|
|
|
|
2020-04-26 12:56:55 +00:00
|
|
|
kfree(cmd->chanlist); /* free kernel copy of user chanlist */
|
2014-10-20 14:10:40 +00:00
|
|
|
|
2008-11-14 01:05:58 +00:00
|
|
|
/* restore chanlist pointer before copying back */
|
2020-04-26 12:56:55 +00:00
|
|
|
cmd->chanlist = (unsigned int __force *)user_chanlist;
|
|
|
|
*copy = true;
|
2014-03-06 19:02:57 +00:00
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-01-28 18:41:47 +00:00
|
|
|
* COMEDI_LOCK ioctl
|
|
|
|
* lock subdevice
|
|
|
|
*
|
|
|
|
* arg:
|
|
|
|
* subdevice number
|
|
|
|
*
|
|
|
|
* reads:
|
|
|
|
* nothing
|
|
|
|
*
|
|
|
|
* writes:
|
|
|
|
* nothing
|
|
|
|
*/
|
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
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-01-28 18:41:47 +00:00
|
|
|
* COMEDI_UNLOCK ioctl
|
|
|
|
* unlock subdevice
|
|
|
|
*
|
|
|
|
* arg:
|
|
|
|
* subdevice number
|
|
|
|
*
|
|
|
|
* reads:
|
|
|
|
* nothing
|
|
|
|
*
|
|
|
|
* writes:
|
|
|
|
* nothing
|
|
|
|
*/
|
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
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-01-28 18:41:47 +00:00
|
|
|
* COMEDI_CANCEL ioctl
|
|
|
|
* cancel asynchronous acquisition
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
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];
|
2015-03-04 19:15:28 +00:00
|
|
|
if (!s->async)
|
2008-11-05 04:29:31 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-01-28 18:41:47 +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
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
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;
|
|
|
|
}
|
|
|
|
|
2014-11-04 18:09:01 +00:00
|
|
|
/*
|
|
|
|
* COMEDI_SETRSUBD ioctl
|
|
|
|
* sets the current "read" subdevice on a per-file basis
|
|
|
|
*
|
|
|
|
* arg:
|
|
|
|
* subdevice number
|
|
|
|
*
|
|
|
|
* reads:
|
|
|
|
* nothing
|
|
|
|
*
|
|
|
|
* writes:
|
|
|
|
* nothing
|
|
|
|
*/
|
|
|
|
static int do_setrsubd_ioctl(struct comedi_device *dev, unsigned long arg,
|
|
|
|
struct file *file)
|
|
|
|
{
|
|
|
|
struct comedi_file *cfp = file->private_data;
|
|
|
|
struct comedi_subdevice *s_old, *s_new;
|
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
2014-11-04 18:09:01 +00:00
|
|
|
if (arg >= dev->n_subdevices)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
s_new = &dev->subdevices[arg];
|
|
|
|
s_old = comedi_file_read_subdevice(file);
|
|
|
|
if (s_old == s_new)
|
|
|
|
return 0; /* no change */
|
|
|
|
|
|
|
|
if (!(s_new->subdev_flags & SDF_CMD_READ))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the file isn't still busy handling a "read" command on the
|
|
|
|
* old subdevice (if any).
|
|
|
|
*/
|
|
|
|
if (s_old && s_old->busy == file && s_old->async &&
|
|
|
|
!(s_old->async->cmd.flags & CMDF_WRITE))
|
|
|
|
return -EBUSY;
|
|
|
|
|
2016-05-10 15:51:07 +00:00
|
|
|
WRITE_ONCE(cfp->read_subdev, s_new);
|
2014-11-04 18:09:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* COMEDI_SETWSUBD ioctl
|
|
|
|
* sets the current "write" subdevice on a per-file basis
|
|
|
|
*
|
|
|
|
* arg:
|
|
|
|
* subdevice number
|
|
|
|
*
|
|
|
|
* reads:
|
|
|
|
* nothing
|
|
|
|
*
|
|
|
|
* writes:
|
|
|
|
* nothing
|
|
|
|
*/
|
|
|
|
static int do_setwsubd_ioctl(struct comedi_device *dev, unsigned long arg,
|
|
|
|
struct file *file)
|
|
|
|
{
|
|
|
|
struct comedi_file *cfp = file->private_data;
|
|
|
|
struct comedi_subdevice *s_old, *s_new;
|
|
|
|
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
2014-11-04 18:09:01 +00:00
|
|
|
if (arg >= dev->n_subdevices)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
s_new = &dev->subdevices[arg];
|
|
|
|
s_old = comedi_file_write_subdevice(file);
|
|
|
|
if (s_old == s_new)
|
|
|
|
return 0; /* no change */
|
|
|
|
|
|
|
|
if (!(s_new->subdev_flags & SDF_CMD_WRITE))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the file isn't still busy handling a "write" command on the
|
|
|
|
* old subdevice (if any).
|
|
|
|
*/
|
|
|
|
if (s_old && s_old->busy == file && s_old->async &&
|
|
|
|
(s_old->async->cmd.flags & CMDF_WRITE))
|
|
|
|
return -EBUSY;
|
|
|
|
|
2016-05-10 15:51:07 +00:00
|
|
|
WRITE_ONCE(cfp->write_subdev, s_new);
|
2014-11-04 18:09:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-06-08 00:05:27 +00:00
|
|
|
static long comedi_unlocked_ioctl(struct file *file, unsigned int cmd,
|
|
|
|
unsigned long arg)
|
|
|
|
{
|
2016-03-21 13:18:35 +00:00
|
|
|
unsigned int minor = iminor(file_inode(file));
|
2014-11-04 18:09:00 +00:00
|
|
|
struct comedi_file *cfp = file->private_data;
|
|
|
|
struct comedi_device *dev = cfp->dev;
|
2012-06-08 00:05:27 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
mutex_lock(&dev->mutex);
|
|
|
|
|
2015-01-28 18:41:50 +00:00
|
|
|
/*
|
|
|
|
* Device config is special, because it must work on
|
|
|
|
* an unconfigured device.
|
|
|
|
*/
|
2012-06-08 00:05:27 +00:00
|
|
|
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) {
|
2015-01-28 18:41:50 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2020-04-25 22:35:03 +00:00
|
|
|
case COMEDI_CHANINFO: {
|
|
|
|
struct comedi_chaninfo it;
|
2020-06-05 03:21:40 +00:00
|
|
|
|
2020-04-25 22:35:03 +00:00
|
|
|
if (copy_from_user(&it, (void __user *)arg, sizeof(it)))
|
|
|
|
rc = -EFAULT;
|
|
|
|
else
|
|
|
|
rc = do_chaninfo_ioctl(dev, &it);
|
2012-06-08 00:05:27 +00:00
|
|
|
break;
|
2020-04-25 22:35:03 +00:00
|
|
|
}
|
2020-04-25 22:44:30 +00:00
|
|
|
case COMEDI_RANGEINFO: {
|
|
|
|
struct comedi_rangeinfo it;
|
2020-06-05 03:21:40 +00:00
|
|
|
|
2020-04-25 22:44:30 +00:00
|
|
|
if (copy_from_user(&it, (void __user *)arg, sizeof(it)))
|
|
|
|
rc = -EFAULT;
|
|
|
|
else
|
|
|
|
rc = do_rangeinfo_ioctl(dev, &it);
|
2012-06-08 00:05:27 +00:00
|
|
|
break;
|
2020-04-25 22:44:30 +00:00
|
|
|
}
|
2012-06-08 00:05:27 +00:00
|
|
|
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;
|
2020-04-26 13:01:49 +00:00
|
|
|
case COMEDI_CMD: {
|
|
|
|
struct comedi_cmd cmd;
|
|
|
|
bool copy = false;
|
|
|
|
|
|
|
|
if (copy_from_user(&cmd, (void __user *)arg, sizeof(cmd))) {
|
|
|
|
rc = -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rc = do_cmd_ioctl(dev, &cmd, ©, file);
|
|
|
|
if (copy && copy_to_user((void __user *)arg, &cmd, sizeof(cmd)))
|
|
|
|
rc = -EFAULT;
|
2012-06-08 00:05:27 +00:00
|
|
|
break;
|
2020-04-26 13:01:49 +00:00
|
|
|
}
|
2020-04-26 12:56:55 +00:00
|
|
|
case COMEDI_CMDTEST: {
|
|
|
|
struct comedi_cmd cmd;
|
|
|
|
bool copy = false;
|
|
|
|
|
|
|
|
if (copy_from_user(&cmd, (void __user *)arg, sizeof(cmd))) {
|
|
|
|
rc = -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rc = do_cmdtest_ioctl(dev, &cmd, ©, file);
|
|
|
|
if (copy && copy_to_user((void __user *)arg, &cmd, sizeof(cmd)))
|
|
|
|
rc = -EFAULT;
|
2012-06-08 00:05:27 +00:00
|
|
|
break;
|
2020-04-26 12:56:55 +00:00
|
|
|
}
|
2020-04-26 00:33:04 +00:00
|
|
|
case COMEDI_INSNLIST: {
|
|
|
|
struct comedi_insnlist insnlist;
|
|
|
|
struct comedi_insn *insns = NULL;
|
|
|
|
|
|
|
|
if (copy_from_user(&insnlist, (void __user *)arg,
|
|
|
|
sizeof(insnlist))) {
|
|
|
|
rc = -EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
insns = kcalloc(insnlist.n_insns, sizeof(*insns), GFP_KERNEL);
|
|
|
|
if (!insns) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (copy_from_user(insns, insnlist.insns,
|
|
|
|
sizeof(*insns) * insnlist.n_insns)) {
|
|
|
|
rc = -EFAULT;
|
|
|
|
kfree(insns);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rc = do_insnlist_ioctl(dev, insns, insnlist.n_insns, file);
|
|
|
|
kfree(insns);
|
2012-06-08 00:05:27 +00:00
|
|
|
break;
|
2020-04-26 00:33:04 +00:00
|
|
|
}
|
2020-04-26 00:11:57 +00:00
|
|
|
case COMEDI_INSN: {
|
|
|
|
struct comedi_insn insn;
|
2020-06-05 03:21:40 +00:00
|
|
|
|
2020-04-26 00:11:57 +00:00
|
|
|
if (copy_from_user(&insn, (void __user *)arg, sizeof(insn)))
|
|
|
|
rc = -EFAULT;
|
|
|
|
else
|
|
|
|
rc = do_insn_ioctl(dev, &insn, file);
|
2012-06-08 00:05:27 +00:00
|
|
|
break;
|
2020-04-26 00:11:57 +00:00
|
|
|
}
|
2012-06-08 00:05:27 +00:00
|
|
|
case COMEDI_POLL:
|
|
|
|
rc = do_poll_ioctl(dev, arg, file);
|
|
|
|
break;
|
2014-11-04 18:09:01 +00:00
|
|
|
case COMEDI_SETRSUBD:
|
|
|
|
rc = do_setrsubd_ioctl(dev, arg, file);
|
|
|
|
break;
|
|
|
|
case COMEDI_SETWSUBD:
|
|
|
|
rc = do_setwsubd_ioctl(dev, arg, file);
|
|
|
|
break;
|
2012-06-08 00:05:27 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-20 18:05:14 +00:00
|
|
|
static int comedi_vm_access(struct vm_area_struct *vma, unsigned long addr,
|
|
|
|
void *buf, int len, int write)
|
|
|
|
{
|
|
|
|
struct comedi_buf_map *bm = vma->vm_private_data;
|
|
|
|
unsigned long offset =
|
|
|
|
addr - vma->vm_start + (vma->vm_pgoff << PAGE_SHIFT);
|
|
|
|
|
|
|
|
if (len < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
if (len > vma->vm_end - addr)
|
|
|
|
len = vma->vm_end - addr;
|
|
|
|
return comedi_buf_map_access(bm, offset, buf, len, write);
|
|
|
|
}
|
|
|
|
|
2015-09-09 22:39:26 +00:00
|
|
|
static const struct vm_operations_struct comedi_vm_ops = {
|
2011-10-29 07:45:39 +00:00
|
|
|
.open = comedi_vm_open,
|
|
|
|
.close = comedi_vm_close,
|
2017-04-20 18:05:14 +00:00
|
|
|
.access = comedi_vm_access,
|
2008-11-05 04:29:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int comedi_mmap(struct file *file, struct vm_area_struct *vma)
|
|
|
|
{
|
2014-11-04 18:09:00 +00:00
|
|
|
struct comedi_file *cfp = file->private_data;
|
|
|
|
struct comedi_device *dev = cfp->dev;
|
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;
|
staging: comedi: use dma_mmap_coherent for DMA-able buffer mmap
Comedi's acquisition buffer allocation code can allocate the buffer from
normal kernel memory or from DMA coherent memory depending on the
`dma_async_dir` value in the comedi subdevice. (A value of `DMA_NONE`
causes the buffer to be allocated from normal kernel memory. Other
values cause the buffer to be allocated from DMA coherent memory.) The
buffer currently consists of a bunch of page-sized blocks that are
vmap'ed into a contiguous range of virtual addresses. The pages are also
mmap'able into user address space. For DMA'able buffers, these
page-sized blocks are allocated by `dma_alloc_coherent()`.
For DMA-able buffers, the DMA API is currently abused in various ways,
the most serious abuse being the calling of `virt_to_page()` on the
blocks allocated by `dma_alloc_coherent()` and passing those pages to
`vmap()` (for mapping to the kernels vmalloc address space) and via
`page_to_pfn()` to `remap_pfn_range()` (for mmap'ing to user space). it
also uses the `__GFP_COMP` flag when allocating the blocks, and marks
the allocated pages as reserved (which is unnecessary for DMA coherent
allocations).
The code can be changed to get rid of the vmap'ed address altogether if
necessary, since there are only a few places in the comedi code that use
the vmap'ed address directly and we also keep a list of the kernel
addresses for the individual pages prior to the vmap operation. This
would add some run-time overhead to buffer accesses. The real killer is
the mmap operation.
For mmap, the address range specified in the VMA needs to be mmap'ed to
the individually allocated page-sized blocks. That is not a problem
when the pages are allocated from normal kernel memory as the individual
pages can be remapped by `remap_pfn_range()`, but it is a problem when
the page-sized blocks are allocated by `dma_alloc_coherent()` because
the DMA API currently has no support for splitting a VMA across multiple
blocks of DMA coherent memory (or rather, no support for mapping part of
a VMA range to a single block of DMA coherent memory).
In order to comply with the DMA API and allow the buffer to be mmap'ed,
the buffer needs to be allocated as a single block by a single call to
`dma_alloc_coherent()`, freed by a single call to `dma_free_coherent()`,
and mmap'ed to user space by a single call to `dma_mmap_coherent()`.
This patch changes the buffer allocation, freeing, and mmap'ing code to
do that, with the unfortunate consequence that buffer allocation is more
likely to fail. It also no longer uses the `__GFP_COMP` flag when
allocating DMA coherent memory, no longer marks the
allocated pages of DMA coherent memory as reserved, and no longer vmap's
the DMA coherent memory pages (since they are contiguous anyway).
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-06-25 11:26:59 +00:00
|
|
|
struct comedi_buf_page *buf;
|
2008-11-05 04:29:31 +00:00
|
|
|
unsigned long start = vma->vm_start;
|
|
|
|
unsigned long size;
|
|
|
|
int n_pages;
|
|
|
|
int i;
|
staging: comedi: use dma_mmap_coherent for DMA-able buffer mmap
Comedi's acquisition buffer allocation code can allocate the buffer from
normal kernel memory or from DMA coherent memory depending on the
`dma_async_dir` value in the comedi subdevice. (A value of `DMA_NONE`
causes the buffer to be allocated from normal kernel memory. Other
values cause the buffer to be allocated from DMA coherent memory.) The
buffer currently consists of a bunch of page-sized blocks that are
vmap'ed into a contiguous range of virtual addresses. The pages are also
mmap'able into user address space. For DMA'able buffers, these
page-sized blocks are allocated by `dma_alloc_coherent()`.
For DMA-able buffers, the DMA API is currently abused in various ways,
the most serious abuse being the calling of `virt_to_page()` on the
blocks allocated by `dma_alloc_coherent()` and passing those pages to
`vmap()` (for mapping to the kernels vmalloc address space) and via
`page_to_pfn()` to `remap_pfn_range()` (for mmap'ing to user space). it
also uses the `__GFP_COMP` flag when allocating the blocks, and marks
the allocated pages as reserved (which is unnecessary for DMA coherent
allocations).
The code can be changed to get rid of the vmap'ed address altogether if
necessary, since there are only a few places in the comedi code that use
the vmap'ed address directly and we also keep a list of the kernel
addresses for the individual pages prior to the vmap operation. This
would add some run-time overhead to buffer accesses. The real killer is
the mmap operation.
For mmap, the address range specified in the VMA needs to be mmap'ed to
the individually allocated page-sized blocks. That is not a problem
when the pages are allocated from normal kernel memory as the individual
pages can be remapped by `remap_pfn_range()`, but it is a problem when
the page-sized blocks are allocated by `dma_alloc_coherent()` because
the DMA API currently has no support for splitting a VMA across multiple
blocks of DMA coherent memory (or rather, no support for mapping part of
a VMA range to a single block of DMA coherent memory).
In order to comply with the DMA API and allow the buffer to be mmap'ed,
the buffer needs to be allocated as a single block by a single call to
`dma_alloc_coherent()`, freed by a single call to `dma_free_coherent()`,
and mmap'ed to user space by a single call to `dma_mmap_coherent()`.
This patch changes the buffer allocation, freeing, and mmap'ing code to
do that, with the unfortunate consequence that buffer allocation is more
likely to fail. It also no longer uses the `__GFP_COMP` flag when
allocating DMA coherent memory, no longer marks the
allocated pages of DMA coherent memory as reserved, and no longer vmap's
the DMA coherent memory pages (since they are contiguous anyway).
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-06-25 11:26:59 +00:00
|
|
|
int retval = 0;
|
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
|
|
|
/*
|
2020-06-09 04:33:54 +00:00
|
|
|
* 'trylock' avoids circular dependency with current->mm->mmap_lock
|
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
|
|
|
* 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)
|
2014-11-04 18:09:00 +00:00
|
|
|
s = comedi_file_write_subdevice(file);
|
2008-11-14 01:05:58 +00:00
|
|
|
else
|
2014-11-04 18:09:00 +00:00
|
|
|
s = comedi_file_read_subdevice(file);
|
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;
|
|
|
|
}
|
2016-03-06 07:05:29 +00:00
|
|
|
if (offset_in_page(size)) {
|
2008-11-05 04:29:31 +00:00
|
|
|
retval = -EFAULT;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2016-09-19 21:51:38 +00:00
|
|
|
n_pages = vma_pages(vma);
|
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;
|
|
|
|
}
|
staging: comedi: use dma_mmap_coherent for DMA-able buffer mmap
Comedi's acquisition buffer allocation code can allocate the buffer from
normal kernel memory or from DMA coherent memory depending on the
`dma_async_dir` value in the comedi subdevice. (A value of `DMA_NONE`
causes the buffer to be allocated from normal kernel memory. Other
values cause the buffer to be allocated from DMA coherent memory.) The
buffer currently consists of a bunch of page-sized blocks that are
vmap'ed into a contiguous range of virtual addresses. The pages are also
mmap'able into user address space. For DMA'able buffers, these
page-sized blocks are allocated by `dma_alloc_coherent()`.
For DMA-able buffers, the DMA API is currently abused in various ways,
the most serious abuse being the calling of `virt_to_page()` on the
blocks allocated by `dma_alloc_coherent()` and passing those pages to
`vmap()` (for mapping to the kernels vmalloc address space) and via
`page_to_pfn()` to `remap_pfn_range()` (for mmap'ing to user space). it
also uses the `__GFP_COMP` flag when allocating the blocks, and marks
the allocated pages as reserved (which is unnecessary for DMA coherent
allocations).
The code can be changed to get rid of the vmap'ed address altogether if
necessary, since there are only a few places in the comedi code that use
the vmap'ed address directly and we also keep a list of the kernel
addresses for the individual pages prior to the vmap operation. This
would add some run-time overhead to buffer accesses. The real killer is
the mmap operation.
For mmap, the address range specified in the VMA needs to be mmap'ed to
the individually allocated page-sized blocks. That is not a problem
when the pages are allocated from normal kernel memory as the individual
pages can be remapped by `remap_pfn_range()`, but it is a problem when
the page-sized blocks are allocated by `dma_alloc_coherent()` because
the DMA API currently has no support for splitting a VMA across multiple
blocks of DMA coherent memory (or rather, no support for mapping part of
a VMA range to a single block of DMA coherent memory).
In order to comply with the DMA API and allow the buffer to be mmap'ed,
the buffer needs to be allocated as a single block by a single call to
`dma_alloc_coherent()`, freed by a single call to `dma_free_coherent()`,
and mmap'ed to user space by a single call to `dma_mmap_coherent()`.
This patch changes the buffer allocation, freeing, and mmap'ing code to
do that, with the unfortunate consequence that buffer allocation is more
likely to fail. It also no longer uses the `__GFP_COMP` flag when
allocating DMA coherent memory, no longer marks the
allocated pages of DMA coherent memory as reserved, and no longer vmap's
the DMA coherent memory pages (since they are contiguous anyway).
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-06-25 11:26:59 +00:00
|
|
|
if (bm->dma_dir != DMA_NONE) {
|
|
|
|
/*
|
|
|
|
* DMA buffer was allocated as a single block.
|
|
|
|
* Address is in page_list[0].
|
|
|
|
*/
|
|
|
|
buf = &bm->page_list[0];
|
|
|
|
retval = dma_mmap_coherent(bm->dma_hw_dev, vma, buf->virt_addr,
|
|
|
|
buf->dma_addr, n_pages * PAGE_SIZE);
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < n_pages; ++i) {
|
|
|
|
unsigned long pfn;
|
|
|
|
|
|
|
|
buf = &bm->page_list[i];
|
|
|
|
pfn = page_to_pfn(virt_to_page(buf->virt_addr));
|
|
|
|
retval = remap_pfn_range(vma, start, pfn, PAGE_SIZE,
|
|
|
|
PAGE_SHARED);
|
|
|
|
if (retval)
|
|
|
|
break;
|
2012-12-19 22:45:12 +00:00
|
|
|
|
staging: comedi: use dma_mmap_coherent for DMA-able buffer mmap
Comedi's acquisition buffer allocation code can allocate the buffer from
normal kernel memory or from DMA coherent memory depending on the
`dma_async_dir` value in the comedi subdevice. (A value of `DMA_NONE`
causes the buffer to be allocated from normal kernel memory. Other
values cause the buffer to be allocated from DMA coherent memory.) The
buffer currently consists of a bunch of page-sized blocks that are
vmap'ed into a contiguous range of virtual addresses. The pages are also
mmap'able into user address space. For DMA'able buffers, these
page-sized blocks are allocated by `dma_alloc_coherent()`.
For DMA-able buffers, the DMA API is currently abused in various ways,
the most serious abuse being the calling of `virt_to_page()` on the
blocks allocated by `dma_alloc_coherent()` and passing those pages to
`vmap()` (for mapping to the kernels vmalloc address space) and via
`page_to_pfn()` to `remap_pfn_range()` (for mmap'ing to user space). it
also uses the `__GFP_COMP` flag when allocating the blocks, and marks
the allocated pages as reserved (which is unnecessary for DMA coherent
allocations).
The code can be changed to get rid of the vmap'ed address altogether if
necessary, since there are only a few places in the comedi code that use
the vmap'ed address directly and we also keep a list of the kernel
addresses for the individual pages prior to the vmap operation. This
would add some run-time overhead to buffer accesses. The real killer is
the mmap operation.
For mmap, the address range specified in the VMA needs to be mmap'ed to
the individually allocated page-sized blocks. That is not a problem
when the pages are allocated from normal kernel memory as the individual
pages can be remapped by `remap_pfn_range()`, but it is a problem when
the page-sized blocks are allocated by `dma_alloc_coherent()` because
the DMA API currently has no support for splitting a VMA across multiple
blocks of DMA coherent memory (or rather, no support for mapping part of
a VMA range to a single block of DMA coherent memory).
In order to comply with the DMA API and allow the buffer to be mmap'ed,
the buffer needs to be allocated as a single block by a single call to
`dma_alloc_coherent()`, freed by a single call to `dma_free_coherent()`,
and mmap'ed to user space by a single call to `dma_mmap_coherent()`.
This patch changes the buffer allocation, freeing, and mmap'ing code to
do that, with the unfortunate consequence that buffer allocation is more
likely to fail. It also no longer uses the `__GFP_COMP` flag when
allocating DMA coherent memory, no longer marks the
allocated pages of DMA coherent memory as reserved, and no longer vmap's
the DMA coherent memory pages (since they are contiguous anyway).
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-06-25 11:26:59 +00:00
|
|
|
start += PAGE_SIZE;
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
staging: comedi: use dma_mmap_coherent for DMA-able buffer mmap
Comedi's acquisition buffer allocation code can allocate the buffer from
normal kernel memory or from DMA coherent memory depending on the
`dma_async_dir` value in the comedi subdevice. (A value of `DMA_NONE`
causes the buffer to be allocated from normal kernel memory. Other
values cause the buffer to be allocated from DMA coherent memory.) The
buffer currently consists of a bunch of page-sized blocks that are
vmap'ed into a contiguous range of virtual addresses. The pages are also
mmap'able into user address space. For DMA'able buffers, these
page-sized blocks are allocated by `dma_alloc_coherent()`.
For DMA-able buffers, the DMA API is currently abused in various ways,
the most serious abuse being the calling of `virt_to_page()` on the
blocks allocated by `dma_alloc_coherent()` and passing those pages to
`vmap()` (for mapping to the kernels vmalloc address space) and via
`page_to_pfn()` to `remap_pfn_range()` (for mmap'ing to user space). it
also uses the `__GFP_COMP` flag when allocating the blocks, and marks
the allocated pages as reserved (which is unnecessary for DMA coherent
allocations).
The code can be changed to get rid of the vmap'ed address altogether if
necessary, since there are only a few places in the comedi code that use
the vmap'ed address directly and we also keep a list of the kernel
addresses for the individual pages prior to the vmap operation. This
would add some run-time overhead to buffer accesses. The real killer is
the mmap operation.
For mmap, the address range specified in the VMA needs to be mmap'ed to
the individually allocated page-sized blocks. That is not a problem
when the pages are allocated from normal kernel memory as the individual
pages can be remapped by `remap_pfn_range()`, but it is a problem when
the page-sized blocks are allocated by `dma_alloc_coherent()` because
the DMA API currently has no support for splitting a VMA across multiple
blocks of DMA coherent memory (or rather, no support for mapping part of
a VMA range to a single block of DMA coherent memory).
In order to comply with the DMA API and allow the buffer to be mmap'ed,
the buffer needs to be allocated as a single block by a single call to
`dma_alloc_coherent()`, freed by a single call to `dma_free_coherent()`,
and mmap'ed to user space by a single call to `dma_mmap_coherent()`.
This patch changes the buffer allocation, freeing, and mmap'ing code to
do that, with the unfortunate consequence that buffer allocation is more
likely to fail. It also no longer uses the `__GFP_COMP` flag when
allocating DMA coherent memory, no longer marks the
allocated pages of DMA coherent memory as reserved, and no longer vmap's
the DMA coherent memory pages (since they are contiguous anyway).
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-06-25 11:26:59 +00:00
|
|
|
if (retval == 0) {
|
|
|
|
vma->vm_ops = &comedi_vm_ops;
|
|
|
|
vma->vm_private_data = bm;
|
2008-11-05 04:29:31 +00:00
|
|
|
|
staging: comedi: use dma_mmap_coherent for DMA-able buffer mmap
Comedi's acquisition buffer allocation code can allocate the buffer from
normal kernel memory or from DMA coherent memory depending on the
`dma_async_dir` value in the comedi subdevice. (A value of `DMA_NONE`
causes the buffer to be allocated from normal kernel memory. Other
values cause the buffer to be allocated from DMA coherent memory.) The
buffer currently consists of a bunch of page-sized blocks that are
vmap'ed into a contiguous range of virtual addresses. The pages are also
mmap'able into user address space. For DMA'able buffers, these
page-sized blocks are allocated by `dma_alloc_coherent()`.
For DMA-able buffers, the DMA API is currently abused in various ways,
the most serious abuse being the calling of `virt_to_page()` on the
blocks allocated by `dma_alloc_coherent()` and passing those pages to
`vmap()` (for mapping to the kernels vmalloc address space) and via
`page_to_pfn()` to `remap_pfn_range()` (for mmap'ing to user space). it
also uses the `__GFP_COMP` flag when allocating the blocks, and marks
the allocated pages as reserved (which is unnecessary for DMA coherent
allocations).
The code can be changed to get rid of the vmap'ed address altogether if
necessary, since there are only a few places in the comedi code that use
the vmap'ed address directly and we also keep a list of the kernel
addresses for the individual pages prior to the vmap operation. This
would add some run-time overhead to buffer accesses. The real killer is
the mmap operation.
For mmap, the address range specified in the VMA needs to be mmap'ed to
the individually allocated page-sized blocks. That is not a problem
when the pages are allocated from normal kernel memory as the individual
pages can be remapped by `remap_pfn_range()`, but it is a problem when
the page-sized blocks are allocated by `dma_alloc_coherent()` because
the DMA API currently has no support for splitting a VMA across multiple
blocks of DMA coherent memory (or rather, no support for mapping part of
a VMA range to a single block of DMA coherent memory).
In order to comply with the DMA API and allow the buffer to be mmap'ed,
the buffer needs to be allocated as a single block by a single call to
`dma_alloc_coherent()`, freed by a single call to `dma_free_coherent()`,
and mmap'ed to user space by a single call to `dma_mmap_coherent()`.
This patch changes the buffer allocation, freeing, and mmap'ing code to
do that, with the unfortunate consequence that buffer allocation is more
likely to fail. It also no longer uses the `__GFP_COMP` flag when
allocating DMA coherent memory, no longer marks the
allocated pages of DMA coherent memory as reserved, and no longer vmap's
the DMA coherent memory pages (since they are contiguous anyway).
Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2019-06-25 11:26:59 +00:00
|
|
|
vma->vm_ops->open(vma);
|
|
|
|
}
|
2008-11-05 04:29:31 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-07-03 10:39:46 +00:00
|
|
|
static __poll_t comedi_poll(struct file *file, poll_table *wait)
|
2008-11-05 04:29:31 +00:00
|
|
|
{
|
2017-07-03 10:39:46 +00:00
|
|
|
__poll_t mask = 0;
|
2014-11-04 18:09:00 +00:00
|
|
|
struct comedi_file *cfp = file->private_data;
|
|
|
|
struct comedi_device *dev = cfp->dev;
|
2015-10-09 11:26:47 +00:00
|
|
|
struct comedi_subdevice *s, *s_read;
|
2011-11-08 21:23:03 +00:00
|
|
|
|
2015-10-09 11:26:52 +00:00
|
|
|
down_read(&dev->attach_lock);
|
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
|
|
|
}
|
|
|
|
|
2014-11-04 18:09:00 +00:00
|
|
|
s = comedi_file_read_subdevice(file);
|
2015-10-09 11:26:47 +00:00
|
|
|
s_read = s;
|
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);
|
2015-10-09 11:26:51 +00:00
|
|
|
if (s->busy != file || !comedi_is_subdevice_running(s) ||
|
2014-10-30 12:42:31 +00:00
|
|
|
(s->async->cmd.flags & CMDF_WRITE) ||
|
2014-05-06 12:12:09 +00:00
|
|
|
comedi_buf_read_n_available(s) > 0)
|
2018-02-11 22:34:03 +00:00
|
|
|
mask |= EPOLLIN | EPOLLRDNORM;
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
2012-12-19 22:39:44 +00:00
|
|
|
|
2014-11-04 18:09:00 +00:00
|
|
|
s = comedi_file_write_subdevice(file);
|
2013-02-05 12:50:40 +00:00
|
|
|
if (s && s->async) {
|
2014-10-31 19:04:28 +00:00
|
|
|
unsigned int bps = comedi_bytes_per_sample(s);
|
2012-12-19 22:39:44 +00:00
|
|
|
|
2015-10-09 11:26:47 +00:00
|
|
|
if (s != s_read)
|
|
|
|
poll_wait(file, &s->async->wait_head, wait);
|
2015-10-09 11:26:51 +00:00
|
|
|
if (s->busy != file || !comedi_is_subdevice_running(s) ||
|
2014-10-30 12:42:31 +00:00
|
|
|
!(s->async->cmd.flags & CMDF_WRITE) ||
|
2015-10-09 11:26:50 +00:00
|
|
|
comedi_buf_write_n_available(s) >= bps)
|
2018-02-11 22:34:03 +00:00
|
|
|
mask |= EPOLLOUT | EPOLLWRNORM;
|
2008-11-05 04:29:31 +00:00
|
|
|
}
|
|
|
|
|
2012-12-19 22:39:44 +00:00
|
|
|
done:
|
2015-10-09 11:26:52 +00:00
|
|
|
up_read(&dev->attach_lock);
|
2008-11-05 04:29:31 +00:00
|
|
|
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;
|
2015-11-18 17:55:06 +00:00
|
|
|
unsigned int n, m;
|
|
|
|
ssize_t count = 0;
|
|
|
|
int retval = 0;
|
2008-11-05 04:29:31 +00:00
|
|
|
DECLARE_WAITQUEUE(wait, current);
|
2014-11-04 18:09:00 +00:00
|
|
|
struct comedi_file *cfp = file->private_data;
|
|
|
|
struct comedi_device *dev = cfp->dev;
|
2015-11-18 17:55:04 +00:00
|
|
|
bool become_nonbusy = false;
|
2013-11-08 15:03:30 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-11-04 18:09:00 +00:00
|
|
|
s = comedi_file_write_subdevice(file);
|
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;
|
2015-11-18 17:55:10 +00:00
|
|
|
if (s->busy != file || !(async->cmd.flags & CMDF_WRITE)) {
|
2014-10-30 12:42:30 +00:00
|
|
|
retval = -EINVAL;
|
|
|
|
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);
|
2015-11-18 17:55:04 +00:00
|
|
|
while (count == 0 && !retval) {
|
2016-03-21 13:18:35 +00:00
|
|
|
unsigned int runflags;
|
2015-11-18 17:55:08 +00:00
|
|
|
unsigned int wp, n1, n2;
|
2015-03-27 15:13:01 +00:00
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
set_current_state(TASK_INTERRUPTIBLE);
|
|
|
|
|
2015-03-27 15:13:01 +00:00
|
|
|
runflags = comedi_get_subdevice_runflags(s);
|
|
|
|
if (!comedi_is_runflags_running(runflags)) {
|
2015-11-18 17:55:04 +00:00
|
|
|
if (comedi_is_runflags_in_error(runflags))
|
|
|
|
retval = -EPIPE;
|
2015-11-18 17:55:11 +00:00
|
|
|
if (retval || nbytes)
|
|
|
|
become_nonbusy = true;
|
2010-05-19 16:22:41 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-11-18 17:55:11 +00:00
|
|
|
if (nbytes == 0)
|
|
|
|
break;
|
2010-05-19 16:22:41 +00:00
|
|
|
|
2015-11-18 17:55:07 +00:00
|
|
|
/* Allocate all free buffer space. */
|
|
|
|
comedi_buf_write_alloc(s, async->prealloc_bufsz);
|
|
|
|
m = comedi_buf_write_n_allocated(s);
|
|
|
|
n = min_t(size_t, m, nbytes);
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2015-11-18 17:55:10 +00:00
|
|
|
if (s->busy != file ||
|
|
|
|
!(async->cmd.flags & CMDF_WRITE)) {
|
2014-10-30 12:42:30 +00:00
|
|
|
retval = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2008-11-05 04:29:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-07-28 15:22:31 +00:00
|
|
|
set_current_state(TASK_RUNNING);
|
2015-11-18 17:55:08 +00:00
|
|
|
wp = async->buf_write_ptr;
|
|
|
|
n1 = min(n, async->prealloc_bufsz - wp);
|
|
|
|
n2 = n - n1;
|
|
|
|
m = copy_from_user(async->prealloc_buf + wp, buf, n1);
|
|
|
|
if (m)
|
|
|
|
m += n2;
|
|
|
|
else if (n2)
|
|
|
|
m = copy_from_user(async->prealloc_buf, buf + n1, n2);
|
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;
|
|
|
|
}
|
2015-11-18 17:55:04 +00:00
|
|
|
remove_wait_queue(&async->wait_head, &wait);
|
2008-11-05 04:29:31 +00:00
|
|
|
set_current_state(TASK_RUNNING);
|
2015-11-18 17:55:04 +00:00
|
|
|
if (become_nonbusy && count == 0) {
|
|
|
|
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;
|
|
|
|
mutex_lock(&dev->mutex);
|
|
|
|
/*
|
|
|
|
* 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.
|
2015-11-18 17:55:05 +00:00
|
|
|
*
|
|
|
|
* Also check the subdevice is still in a suitable state to
|
|
|
|
* become non-busy in case it changed behind our back.
|
2015-11-18 17:55:04 +00:00
|
|
|
*/
|
|
|
|
new_s = comedi_file_write_subdevice(file);
|
|
|
|
if (dev->attached && old_detach_count == dev->detach_count &&
|
2015-11-18 17:55:05 +00:00
|
|
|
s == new_s && new_s->async == async && s->busy == file &&
|
|
|
|
(async->cmd.flags & CMDF_WRITE) &&
|
|
|
|
!comedi_is_subdevice_running(s))
|
2015-11-18 17:55:04 +00:00
|
|
|
do_become_nonbusy(dev, s);
|
|
|
|
mutex_unlock(&dev->mutex);
|
|
|
|
}
|
|
|
|
out:
|
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;
|
2015-10-12 16:21:23 +00:00
|
|
|
unsigned int n, m;
|
|
|
|
ssize_t count = 0;
|
|
|
|
int retval = 0;
|
2008-11-05 04:29:31 +00:00
|
|
|
DECLARE_WAITQUEUE(wait, current);
|
2014-11-04 18:09:00 +00:00
|
|
|
struct comedi_file *cfp = file->private_data;
|
|
|
|
struct comedi_device *dev = cfp->dev;
|
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
|
|
|
}
|
|
|
|
|
2014-11-04 18:09:00 +00:00
|
|
|
s = comedi_file_read_subdevice(file);
|
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;
|
2015-10-12 16:21:28 +00:00
|
|
|
if (s->busy != file || (async->cmd.flags & CMDF_WRITE)) {
|
2014-10-30 12:42:29 +00:00
|
|
|
retval = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
add_wait_queue(&async->wait_head, &wait);
|
2015-10-12 16:21:29 +00:00
|
|
|
while (count == 0 && !retval) {
|
2015-10-12 16:21:25 +00:00
|
|
|
unsigned int rp, n1, n2;
|
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
set_current_state(TASK_INTERRUPTIBLE);
|
|
|
|
|
2014-05-06 12:12:09 +00:00
|
|
|
m = comedi_buf_read_n_available(s);
|
2015-10-12 16:21:24 +00:00
|
|
|
n = min_t(size_t, m, nbytes);
|
2008-11-05 04:29:31 +00:00
|
|
|
|
|
|
|
if (n == 0) {
|
2016-03-29 09:49:04 +00:00
|
|
|
unsigned int runflags =
|
|
|
|
comedi_get_subdevice_runflags(s);
|
2015-03-27 15:13:01 +00:00
|
|
|
|
|
|
|
if (!comedi_is_runflags_running(runflags)) {
|
|
|
|
if (comedi_is_runflags_in_error(runflags))
|
2008-11-05 04:29:31 +00:00
|
|
|
retval = -EPIPE;
|
2015-10-12 16:21:29 +00:00
|
|
|
if (retval || nbytes)
|
|
|
|
become_nonbusy = true;
|
2008-11-05 04:29:31 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-10-12 16:21:29 +00:00
|
|
|
if (nbytes == 0)
|
|
|
|
break;
|
2008-11-05 04:29:31 +00:00
|
|
|
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;
|
|
|
|
}
|
2015-10-12 16:21:28 +00:00
|
|
|
if (s->busy != file ||
|
|
|
|
(async->cmd.flags & CMDF_WRITE)) {
|
2014-10-30 12:42:29 +00:00
|
|
|
retval = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2008-11-05 04:29:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-07-28 15:22:31 +00:00
|
|
|
|
|
|
|
set_current_state(TASK_RUNNING);
|
2015-10-12 16:21:25 +00:00
|
|
|
rp = async->buf_read_ptr;
|
|
|
|
n1 = min(n, async->prealloc_bufsz - rp);
|
|
|
|
n2 = n - n1;
|
|
|
|
m = copy_to_user(buf, async->prealloc_buf + rp, n1);
|
|
|
|
if (m)
|
|
|
|
m += n2;
|
|
|
|
else if (n2)
|
|
|
|
m = copy_to_user(buf + n1, async->prealloc_buf, n2);
|
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;
|
|
|
|
}
|
2013-11-08 15:03:31 +00:00
|
|
|
remove_wait_queue(&async->wait_head, &wait);
|
|
|
|
set_current_state(TASK_RUNNING);
|
2015-10-12 16:21:20 +00:00
|
|
|
if (become_nonbusy && count == 0) {
|
2013-11-08 15:03:31 +00:00
|
|
|
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.
|
2015-10-12 16:21:22 +00:00
|
|
|
*
|
|
|
|
* Also check the subdevice is still in a suitable state to
|
|
|
|
* become non-busy in case it changed behind our back.
|
2013-11-08 15:03:31 +00:00
|
|
|
*/
|
2014-11-04 18:09:00 +00:00
|
|
|
new_s = comedi_file_read_subdevice(file);
|
2013-11-08 15:03:31 +00:00
|
|
|
if (dev->attached && old_detach_count == dev->detach_count &&
|
2015-10-12 16:21:22 +00:00
|
|
|
s == new_s && new_s->async == async && s->busy == file &&
|
|
|
|
!(async->cmd.flags & CMDF_WRITE) &&
|
|
|
|
!comedi_is_subdevice_running(s) &&
|
|
|
|
comedi_buf_read_n_available(s) == 0)
|
|
|
|
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)
|
|
|
|
{
|
2016-03-21 13:18:35 +00:00
|
|
|
const unsigned int minor = iminor(inode);
|
2014-11-04 18:09:00 +00:00
|
|
|
struct comedi_file *cfp;
|
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;
|
|
|
|
}
|
|
|
|
|
2014-11-04 18:09:00 +00:00
|
|
|
cfp = kzalloc(sizeof(*cfp), GFP_KERNEL);
|
2020-04-20 05:44:16 +00:00
|
|
|
if (!cfp) {
|
|
|
|
comedi_dev_put(dev);
|
2014-11-04 18:09:00 +00:00
|
|
|
return -ENOMEM;
|
2020-04-20 05:44:16 +00:00
|
|
|
}
|
2014-11-04 18:09:00 +00:00
|
|
|
|
|
|
|
cfp->dev = dev;
|
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
mutex_lock(&dev->mutex);
|
2015-07-07 16:06:52 +00:00
|
|
|
if (!dev->attached && !capable(CAP_SYS_ADMIN)) {
|
|
|
|
dev_dbg(dev->class_dev, "not attached and not CAP_SYS_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)) {
|
2015-06-29 09:44:03 +00:00
|
|
|
rc = -ENXIO;
|
2013-11-08 15:03:34 +00:00
|
|
|
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++;
|
2014-11-04 18:09:00 +00:00
|
|
|
file->private_data = cfp;
|
|
|
|
comedi_file_reset(file);
|
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);
|
2014-11-04 18:09:00 +00:00
|
|
|
if (rc) {
|
2013-11-08 15:03:34 +00:00
|
|
|
comedi_dev_put(dev);
|
2014-11-04 18:09:00 +00:00
|
|
|
kfree(cfp);
|
|
|
|
}
|
2013-11-08 15:03:34 +00:00
|
|
|
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)
|
|
|
|
{
|
2014-11-04 18:09:00 +00:00
|
|
|
struct comedi_file *cfp = file->private_data;
|
|
|
|
struct comedi_device *dev = cfp->dev;
|
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
|
|
|
{
|
2014-11-04 18:09:00 +00:00
|
|
|
struct comedi_file *cfp = file->private_data;
|
|
|
|
struct comedi_device *dev = cfp->dev;
|
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);
|
2014-11-04 18:09:00 +00:00
|
|
|
kfree(cfp);
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2008-11-05 04:29:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-25 22:19:30 +00:00
|
|
|
#ifdef CONFIG_COMPAT
|
|
|
|
|
|
|
|
#define COMEDI32_CHANINFO _IOR(CIO, 3, struct comedi32_chaninfo_struct)
|
|
|
|
#define COMEDI32_RANGEINFO _IOR(CIO, 8, struct comedi32_rangeinfo_struct)
|
|
|
|
/*
|
|
|
|
* N.B. COMEDI32_CMD and COMEDI_CMD ought to use _IOWR, not _IOR.
|
|
|
|
* It's too late to change it now, but it only affects the command number.
|
|
|
|
*/
|
|
|
|
#define COMEDI32_CMD _IOR(CIO, 9, struct comedi32_cmd_struct)
|
|
|
|
/*
|
|
|
|
* N.B. COMEDI32_CMDTEST and COMEDI_CMDTEST ought to use _IOWR, not _IOR.
|
|
|
|
* It's too late to change it now, but it only affects the command number.
|
|
|
|
*/
|
|
|
|
#define COMEDI32_CMDTEST _IOR(CIO, 10, struct comedi32_cmd_struct)
|
|
|
|
#define COMEDI32_INSNLIST _IOR(CIO, 11, struct comedi32_insnlist_struct)
|
|
|
|
#define COMEDI32_INSN _IOR(CIO, 12, struct comedi32_insn_struct)
|
|
|
|
|
|
|
|
struct comedi32_chaninfo_struct {
|
|
|
|
unsigned int subdev;
|
|
|
|
compat_uptr_t maxdata_list; /* 32-bit 'unsigned int *' */
|
|
|
|
compat_uptr_t flaglist; /* 32-bit 'unsigned int *' */
|
|
|
|
compat_uptr_t rangelist; /* 32-bit 'unsigned int *' */
|
|
|
|
unsigned int unused[4];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct comedi32_rangeinfo_struct {
|
|
|
|
unsigned int range_type;
|
|
|
|
compat_uptr_t range_ptr; /* 32-bit 'void *' */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct comedi32_cmd_struct {
|
|
|
|
unsigned int subdev;
|
|
|
|
unsigned int flags;
|
|
|
|
unsigned int start_src;
|
|
|
|
unsigned int start_arg;
|
|
|
|
unsigned int scan_begin_src;
|
|
|
|
unsigned int scan_begin_arg;
|
|
|
|
unsigned int convert_src;
|
|
|
|
unsigned int convert_arg;
|
|
|
|
unsigned int scan_end_src;
|
|
|
|
unsigned int scan_end_arg;
|
|
|
|
unsigned int stop_src;
|
|
|
|
unsigned int stop_arg;
|
|
|
|
compat_uptr_t chanlist; /* 32-bit 'unsigned int *' */
|
|
|
|
unsigned int chanlist_len;
|
|
|
|
compat_uptr_t data; /* 32-bit 'short *' */
|
|
|
|
unsigned int data_len;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct comedi32_insn_struct {
|
|
|
|
unsigned int insn;
|
|
|
|
unsigned int n;
|
|
|
|
compat_uptr_t data; /* 32-bit 'unsigned int *' */
|
|
|
|
unsigned int subdev;
|
|
|
|
unsigned int chanspec;
|
|
|
|
unsigned int unused[3];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct comedi32_insnlist_struct {
|
|
|
|
unsigned int n_insns;
|
|
|
|
compat_uptr_t insns; /* 32-bit 'struct comedi_insn *' */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Handle 32-bit COMEDI_CHANINFO ioctl. */
|
|
|
|
static int compat_chaninfo(struct file *file, unsigned long arg)
|
|
|
|
{
|
2020-04-25 22:35:03 +00:00
|
|
|
struct comedi_file *cfp = file->private_data;
|
|
|
|
struct comedi_device *dev = cfp->dev;
|
|
|
|
struct comedi32_chaninfo_struct chaninfo32;
|
|
|
|
struct comedi_chaninfo chaninfo;
|
2020-04-25 22:19:30 +00:00
|
|
|
int err;
|
|
|
|
|
2020-04-25 22:35:03 +00:00
|
|
|
if (copy_from_user(&chaninfo32, compat_ptr(arg), sizeof(chaninfo32)))
|
2020-04-25 22:19:30 +00:00
|
|
|
return -EFAULT;
|
|
|
|
|
2020-04-25 22:35:03 +00:00
|
|
|
memset(&chaninfo, 0, sizeof(chaninfo));
|
|
|
|
chaninfo.subdev = chaninfo32.subdev;
|
|
|
|
chaninfo.maxdata_list = compat_ptr(chaninfo32.maxdata_list);
|
|
|
|
chaninfo.flaglist = compat_ptr(chaninfo32.flaglist);
|
|
|
|
chaninfo.rangelist = compat_ptr(chaninfo32.rangelist);
|
2020-04-25 22:19:30 +00:00
|
|
|
|
2020-04-25 22:35:03 +00:00
|
|
|
mutex_lock(&dev->mutex);
|
|
|
|
err = do_chaninfo_ioctl(dev, &chaninfo);
|
|
|
|
mutex_unlock(&dev->mutex);
|
|
|
|
return err;
|
2020-04-25 22:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle 32-bit COMEDI_RANGEINFO ioctl. */
|
|
|
|
static int compat_rangeinfo(struct file *file, unsigned long arg)
|
|
|
|
{
|
2020-04-25 22:44:30 +00:00
|
|
|
struct comedi_file *cfp = file->private_data;
|
|
|
|
struct comedi_device *dev = cfp->dev;
|
|
|
|
struct comedi32_rangeinfo_struct rangeinfo32;
|
|
|
|
struct comedi_rangeinfo rangeinfo;
|
2020-04-25 22:19:30 +00:00
|
|
|
int err;
|
|
|
|
|
2020-04-25 22:44:30 +00:00
|
|
|
if (copy_from_user(&rangeinfo32, compat_ptr(arg), sizeof(rangeinfo32)))
|
2020-04-25 22:19:30 +00:00
|
|
|
return -EFAULT;
|
2020-04-25 22:44:30 +00:00
|
|
|
memset(&rangeinfo, 0, sizeof(rangeinfo));
|
|
|
|
rangeinfo.range_type = rangeinfo32.range_type;
|
|
|
|
rangeinfo.range_ptr = compat_ptr(rangeinfo32.range_ptr);
|
2020-04-25 22:19:30 +00:00
|
|
|
|
2020-04-25 22:44:30 +00:00
|
|
|
mutex_lock(&dev->mutex);
|
|
|
|
err = do_rangeinfo_ioctl(dev, &rangeinfo);
|
|
|
|
mutex_unlock(&dev->mutex);
|
|
|
|
return err;
|
2020-04-25 22:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy 32-bit cmd structure to native cmd structure. */
|
2020-04-26 13:27:23 +00:00
|
|
|
static int get_compat_cmd(struct comedi_cmd *cmd,
|
2020-04-25 22:19:30 +00:00
|
|
|
struct comedi32_cmd_struct __user *cmd32)
|
|
|
|
{
|
2020-04-26 13:27:23 +00:00
|
|
|
struct comedi32_cmd_struct v32;
|
|
|
|
|
|
|
|
if (copy_from_user(&v32, cmd32, sizeof(v32)))
|
2020-04-25 22:19:30 +00:00
|
|
|
return -EFAULT;
|
|
|
|
|
2020-04-26 13:27:23 +00:00
|
|
|
cmd->subdev = v32.subdev;
|
|
|
|
cmd->flags = v32.flags;
|
|
|
|
cmd->start_src = v32.start_src;
|
|
|
|
cmd->start_arg = v32.start_arg;
|
|
|
|
cmd->scan_begin_src = v32.scan_begin_src;
|
|
|
|
cmd->scan_begin_arg = v32.scan_begin_arg;
|
|
|
|
cmd->convert_src = v32.convert_src;
|
|
|
|
cmd->convert_arg = v32.convert_arg;
|
|
|
|
cmd->scan_end_src = v32.scan_end_src;
|
|
|
|
cmd->scan_end_arg = v32.scan_end_arg;
|
|
|
|
cmd->stop_src = v32.stop_src;
|
|
|
|
cmd->stop_arg = v32.stop_arg;
|
2020-07-17 10:30:31 +00:00
|
|
|
cmd->chanlist = (unsigned int __force *)compat_ptr(v32.chanlist);
|
2020-04-26 13:27:23 +00:00
|
|
|
cmd->chanlist_len = v32.chanlist_len;
|
|
|
|
cmd->data = compat_ptr(v32.data);
|
|
|
|
cmd->data_len = v32.data_len;
|
|
|
|
return 0;
|
2020-04-25 22:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy native cmd structure to 32-bit cmd structure. */
|
|
|
|
static int put_compat_cmd(struct comedi32_cmd_struct __user *cmd32,
|
2020-04-26 13:27:23 +00:00
|
|
|
struct comedi_cmd *cmd)
|
|
|
|
{
|
|
|
|
struct comedi32_cmd_struct v32;
|
|
|
|
|
|
|
|
memset(&v32, 0, sizeof(v32));
|
|
|
|
v32.subdev = cmd->subdev;
|
|
|
|
v32.flags = cmd->flags;
|
|
|
|
v32.start_src = cmd->start_src;
|
|
|
|
v32.start_arg = cmd->start_arg;
|
|
|
|
v32.scan_begin_src = cmd->scan_begin_src;
|
|
|
|
v32.scan_begin_arg = cmd->scan_begin_arg;
|
|
|
|
v32.convert_src = cmd->convert_src;
|
|
|
|
v32.convert_arg = cmd->convert_arg;
|
|
|
|
v32.scan_end_src = cmd->scan_end_src;
|
|
|
|
v32.scan_end_arg = cmd->scan_end_arg;
|
|
|
|
v32.stop_src = cmd->stop_src;
|
|
|
|
v32.stop_arg = cmd->stop_arg;
|
2020-04-25 22:19:30 +00:00
|
|
|
/* Assume chanlist pointer is unchanged. */
|
2020-07-17 10:30:31 +00:00
|
|
|
v32.chanlist = ptr_to_compat((unsigned int __user *)cmd->chanlist);
|
2020-04-26 13:27:23 +00:00
|
|
|
v32.chanlist_len = cmd->chanlist_len;
|
|
|
|
v32.data = ptr_to_compat(cmd->data);
|
|
|
|
v32.data_len = cmd->data_len;
|
2020-12-02 06:43:49 +00:00
|
|
|
if (copy_to_user(cmd32, &v32, sizeof(v32)))
|
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
2020-04-25 22:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle 32-bit COMEDI_CMD ioctl. */
|
|
|
|
static int compat_cmd(struct file *file, unsigned long arg)
|
|
|
|
{
|
2020-04-26 13:27:23 +00:00
|
|
|
struct comedi_file *cfp = file->private_data;
|
|
|
|
struct comedi_device *dev = cfp->dev;
|
|
|
|
struct comedi_cmd cmd;
|
|
|
|
bool copy = false;
|
2020-04-25 22:19:30 +00:00
|
|
|
int rc, err;
|
|
|
|
|
2020-04-26 13:27:23 +00:00
|
|
|
rc = get_compat_cmd(&cmd, compat_ptr(arg));
|
2020-04-25 22:19:30 +00:00
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
2020-04-26 13:27:23 +00:00
|
|
|
mutex_lock(&dev->mutex);
|
|
|
|
rc = do_cmd_ioctl(dev, &cmd, ©, file);
|
|
|
|
mutex_unlock(&dev->mutex);
|
|
|
|
if (copy) {
|
2020-04-25 22:19:30 +00:00
|
|
|
/* Special case: copy cmd back to user. */
|
2020-04-26 13:27:23 +00:00
|
|
|
err = put_compat_cmd(compat_ptr(arg), &cmd);
|
2020-04-25 22:19:30 +00:00
|
|
|
if (err)
|
|
|
|
rc = err;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle 32-bit COMEDI_CMDTEST ioctl. */
|
|
|
|
static int compat_cmdtest(struct file *file, unsigned long arg)
|
|
|
|
{
|
2020-04-26 13:27:23 +00:00
|
|
|
struct comedi_file *cfp = file->private_data;
|
|
|
|
struct comedi_device *dev = cfp->dev;
|
|
|
|
struct comedi_cmd cmd;
|
|
|
|
bool copy = false;
|
2020-04-25 22:19:30 +00:00
|
|
|
int rc, err;
|
|
|
|
|
2020-04-26 13:27:23 +00:00
|
|
|
rc = get_compat_cmd(&cmd, compat_ptr(arg));
|
2020-04-25 22:19:30 +00:00
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
2020-04-26 13:27:23 +00:00
|
|
|
mutex_lock(&dev->mutex);
|
|
|
|
rc = do_cmdtest_ioctl(dev, &cmd, ©, file);
|
|
|
|
mutex_unlock(&dev->mutex);
|
|
|
|
if (copy) {
|
|
|
|
err = put_compat_cmd(compat_ptr(arg), &cmd);
|
|
|
|
if (err)
|
|
|
|
rc = err;
|
|
|
|
}
|
2020-04-25 22:19:30 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy 32-bit insn structure to native insn structure. */
|
2020-04-26 00:11:57 +00:00
|
|
|
static int get_compat_insn(struct comedi_insn *insn,
|
|
|
|
struct comedi32_insn_struct __user *insn32)
|
|
|
|
{
|
|
|
|
struct comedi32_insn_struct v32;
|
|
|
|
|
|
|
|
/* Copy insn structure. Ignore the unused members. */
|
|
|
|
if (copy_from_user(&v32, insn32, sizeof(v32)))
|
|
|
|
return -EFAULT;
|
|
|
|
memset(insn, 0, sizeof(*insn));
|
|
|
|
insn->insn = v32.insn;
|
|
|
|
insn->n = v32.n;
|
|
|
|
insn->data = compat_ptr(v32.data);
|
|
|
|
insn->subdev = v32.subdev;
|
|
|
|
insn->chanspec = v32.chanspec;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-25 22:19:30 +00:00
|
|
|
/* Handle 32-bit COMEDI_INSNLIST ioctl. */
|
|
|
|
static int compat_insnlist(struct file *file, unsigned long arg)
|
|
|
|
{
|
2020-04-26 00:33:04 +00:00
|
|
|
struct comedi_file *cfp = file->private_data;
|
|
|
|
struct comedi_device *dev = cfp->dev;
|
|
|
|
struct comedi32_insnlist_struct insnlist32;
|
2020-04-25 22:19:30 +00:00
|
|
|
struct comedi32_insn_struct __user *insn32;
|
2020-04-26 00:33:04 +00:00
|
|
|
struct comedi_insn *insns;
|
|
|
|
unsigned int n;
|
|
|
|
int rc;
|
2020-04-25 22:19:30 +00:00
|
|
|
|
2020-04-26 00:33:04 +00:00
|
|
|
if (copy_from_user(&insnlist32, compat_ptr(arg), sizeof(insnlist32)))
|
2020-04-25 22:19:30 +00:00
|
|
|
return -EFAULT;
|
|
|
|
|
2020-04-26 00:33:04 +00:00
|
|
|
insns = kcalloc(insnlist32.n_insns, sizeof(*insns), GFP_KERNEL);
|
|
|
|
if (!insns)
|
|
|
|
return -ENOMEM;
|
2020-04-25 22:19:30 +00:00
|
|
|
|
|
|
|
/* Copy insn structures. */
|
2020-04-26 00:33:04 +00:00
|
|
|
insn32 = compat_ptr(insnlist32.insns);
|
|
|
|
for (n = 0; n < insnlist32.n_insns; n++) {
|
|
|
|
rc = get_compat_insn(insns + n, insn32 + n);
|
|
|
|
if (rc) {
|
|
|
|
kfree(insns);
|
2020-04-25 22:19:30 +00:00
|
|
|
return rc;
|
2020-04-26 00:33:04 +00:00
|
|
|
}
|
2020-04-25 22:19:30 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 00:33:04 +00:00
|
|
|
mutex_lock(&dev->mutex);
|
|
|
|
rc = do_insnlist_ioctl(dev, insns, insnlist32.n_insns, file);
|
|
|
|
mutex_unlock(&dev->mutex);
|
2021-09-16 14:50:23 +00:00
|
|
|
kfree(insns);
|
2020-04-26 00:33:04 +00:00
|
|
|
return rc;
|
2020-04-25 22:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle 32-bit COMEDI_INSN ioctl. */
|
|
|
|
static int compat_insn(struct file *file, unsigned long arg)
|
|
|
|
{
|
2020-04-26 00:11:57 +00:00
|
|
|
struct comedi_file *cfp = file->private_data;
|
|
|
|
struct comedi_device *dev = cfp->dev;
|
|
|
|
struct comedi_insn insn;
|
2020-04-25 22:19:30 +00:00
|
|
|
int rc;
|
|
|
|
|
2020-04-26 00:11:57 +00:00
|
|
|
rc = get_compat_insn(&insn, (void __user *)arg);
|
2020-04-25 22:19:30 +00:00
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
2020-04-26 00:11:57 +00:00
|
|
|
mutex_lock(&dev->mutex);
|
|
|
|
rc = do_insn_ioctl(dev, &insn, file);
|
|
|
|
mutex_unlock(&dev->mutex);
|
|
|
|
return rc;
|
2020-04-25 22:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* compat_ioctl file operation.
|
|
|
|
*
|
|
|
|
* Returns -ENOIOCTLCMD for unrecognised ioctl codes.
|
|
|
|
*/
|
|
|
|
static long comedi_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case COMEDI_DEVCONFIG:
|
|
|
|
case COMEDI_DEVINFO:
|
|
|
|
case COMEDI_SUBDINFO:
|
|
|
|
case COMEDI_BUFCONFIG:
|
|
|
|
case COMEDI_BUFINFO:
|
|
|
|
/* Just need to translate the pointer argument. */
|
|
|
|
arg = (unsigned long)compat_ptr(arg);
|
2020-04-25 22:23:17 +00:00
|
|
|
rc = comedi_unlocked_ioctl(file, cmd, arg);
|
2020-04-25 22:19:30 +00:00
|
|
|
break;
|
|
|
|
case COMEDI_LOCK:
|
|
|
|
case COMEDI_UNLOCK:
|
|
|
|
case COMEDI_CANCEL:
|
|
|
|
case COMEDI_POLL:
|
|
|
|
case COMEDI_SETRSUBD:
|
|
|
|
case COMEDI_SETWSUBD:
|
|
|
|
/* No translation needed. */
|
2020-04-25 22:23:17 +00:00
|
|
|
rc = comedi_unlocked_ioctl(file, cmd, arg);
|
2020-04-25 22:19:30 +00:00
|
|
|
break;
|
|
|
|
case COMEDI32_CHANINFO:
|
|
|
|
rc = compat_chaninfo(file, arg);
|
|
|
|
break;
|
|
|
|
case COMEDI32_RANGEINFO:
|
|
|
|
rc = compat_rangeinfo(file, arg);
|
|
|
|
break;
|
|
|
|
case COMEDI32_CMD:
|
|
|
|
rc = compat_cmd(file, arg);
|
|
|
|
break;
|
|
|
|
case COMEDI32_CMDTEST:
|
|
|
|
rc = compat_cmdtest(file, arg);
|
|
|
|
break;
|
|
|
|
case COMEDI32_INSNLIST:
|
|
|
|
rc = compat_insnlist(file, arg);
|
|
|
|
break;
|
|
|
|
case COMEDI32_INSN:
|
|
|
|
rc = compat_insn(file, arg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rc = -ENOIOCTLCMD;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define comedi_compat_ioctl NULL
|
|
|
|
#endif
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2015-01-28 18:41:46 +00:00
|
|
|
/**
|
2015-09-23 15:33:27 +00:00
|
|
|
* comedi_event() - Handle events for asynchronous COMEDI command
|
|
|
|
* @dev: COMEDI device.
|
|
|
|
* @s: COMEDI subdevice.
|
|
|
|
* Context: in_interrupt() (usually), @s->spin_lock spin-lock not held.
|
2015-01-28 18:41:46 +00:00
|
|
|
*
|
2015-09-23 15:33:27 +00:00
|
|
|
* If an asynchronous COMEDI command is active on the subdevice, process
|
|
|
|
* any %COMEDI_CB_... event flags that have been set, usually by an
|
2015-01-28 18:41:46 +00:00
|
|
|
* interrupt handler. These may change the run state of the asynchronous
|
2015-09-23 15:33:27 +00:00
|
|
|
* command, wake a task, and/or send a %SIGIO signal.
|
2015-01-28 18:41:46 +00:00
|
|
|
*/
|
2012-05-09 16:20:08 +00:00
|
|
|
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;
|
2015-03-27 15:13:06 +00:00
|
|
|
unsigned int events;
|
|
|
|
int si_code = 0;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&s->spin_lock, flags);
|
2009-04-14 15:21:41 +00:00
|
|
|
|
2015-03-27 15:13:06 +00:00
|
|
|
events = async->events;
|
2015-03-27 15:13:04 +00:00
|
|
|
async->events = 0;
|
2015-03-27 15:13:06 +00:00
|
|
|
if (!__comedi_is_subdevice_running(s)) {
|
|
|
|
spin_unlock_irqrestore(&s->spin_lock, flags);
|
2012-05-09 16:20:08 +00:00
|
|
|
return;
|
2015-03-27 15:13:06 +00:00
|
|
|
}
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2015-03-27 15:13:04 +00:00
|
|
|
if (events & COMEDI_CB_CANCEL_MASK)
|
2015-03-27 15:13:06 +00:00
|
|
|
__comedi_clear_subdevice_runflags(s, COMEDI_SRF_RUNNING);
|
2014-10-13 16:56:08 +00:00
|
|
|
|
|
|
|
/*
|
2015-03-27 15:13:06 +00:00
|
|
|
* Remember if an error event has occurred, so an error can be
|
|
|
|
* returned the next time the user does a read() or write().
|
2014-10-13 16:56:08 +00:00
|
|
|
*/
|
2015-03-27 15:13:06 +00:00
|
|
|
if (events & COMEDI_CB_ERROR_MASK)
|
|
|
|
__comedi_set_subdevice_runflags(s, COMEDI_SRF_ERROR);
|
2009-04-14 15:21:41 +00:00
|
|
|
|
2015-03-27 15:13:04 +00:00
|
|
|
if (async->cb_mask & events) {
|
2013-11-08 15:03:22 +00:00
|
|
|
wake_up_interruptible(&async->wait_head);
|
2015-03-27 15:13:05 +00:00
|
|
|
si_code = async->cmd.flags & CMDF_WRITE ? POLL_OUT : POLL_IN;
|
2012-05-09 16:20:08 +00:00
|
|
|
}
|
2015-03-27 15:13:06 +00:00
|
|
|
|
|
|
|
spin_unlock_irqrestore(&s->spin_lock, flags);
|
|
|
|
|
|
|
|
if (si_code)
|
|
|
|
kill_fasync(&dev->async_queue, SIGIO, si_code);
|
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;
|
2016-03-21 13:18:35 +00:00
|
|
|
unsigned int i;
|
2012-05-09 16:20:08 +00:00
|
|
|
|
2014-07-18 21:28:14 +00:00
|
|
|
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
|
2015-03-04 19:15:28 +00:00
|
|
|
if (!dev)
|
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) {
|
2015-03-04 19:15:28 +00:00
|
|
|
if (!comedi_board_minor_table[i]) {
|
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);
|
2015-03-17 05:33:51 +00:00
|
|
|
dev_err(hardware_device,
|
|
|
|
"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;
|
2023-06-20 14:41:38 +00:00
|
|
|
csdev = device_create(&comedi_class, hardware_device,
|
2012-05-09 16:20:08 +00:00
|
|
|
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: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;
|
2016-03-21 13:18:35 +00:00
|
|
|
unsigned int 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) {
|
2015-03-04 19:15:28 +00:00
|
|
|
if (!comedi_subdevice_minor_table[i]) {
|
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) {
|
2015-03-17 05:33:51 +00:00
|
|
|
dev_err(dev->class_dev,
|
|
|
|
"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;
|
2023-06-20 14:41:38 +00:00
|
|
|
csdev = device_create(&comedi_class, dev->class_dev,
|
2012-05-09 16:20:08 +00:00
|
|
|
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
|
|
|
|
2015-03-04 19:15:28 +00:00
|
|
|
if (!s)
|
2012-05-09 16:20:08 +00:00
|
|
|
return;
|
2015-08-10 20:14:01 +00:00
|
|
|
if (s->minor < COMEDI_NUM_BOARD_MINORS ||
|
|
|
|
s->minor >= COMEDI_NUM_MINORS)
|
2012-05-09 16:20:08 +00:00
|
|
|
return;
|
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) {
|
2023-06-20 14:41:38 +00:00
|
|
|
device_destroy(&comedi_class, MKDEV(COMEDI_MAJOR, s->minor));
|
2012-05-09 16:20:08 +00:00
|
|
|
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
|
|
|
{
|
2015-08-10 20:13:59 +00:00
|
|
|
struct comedi_device *dev;
|
2016-03-21 13:18:35 +00:00
|
|
|
unsigned int i;
|
2012-12-19 22:41:42 +00:00
|
|
|
|
2015-08-10 20:13:59 +00:00
|
|
|
for (i = 0; i < COMEDI_NUM_BOARD_MINORS; i++) {
|
|
|
|
dev = comedi_clear_board_minor(i);
|
|
|
|
comedi_free_board_dev(dev);
|
|
|
|
}
|
2012-12-19 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-03-07 18:13:58 +00:00
|
|
|
if (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)
|
2017-06-16 18:35:35 +00:00
|
|
|
return retval;
|
|
|
|
|
2012-12-19 22:41:19 +00:00
|
|
|
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");
|
2017-06-16 18:35:35 +00:00
|
|
|
if (retval)
|
|
|
|
goto out_unregister_chrdev_region;
|
|
|
|
|
|
|
|
retval = cdev_add(&comedi_cdev, MKDEV(COMEDI_MAJOR, 0),
|
|
|
|
COMEDI_NUM_MINORS);
|
|
|
|
if (retval)
|
|
|
|
goto out_unregister_chrdev_region;
|
2014-07-09 05:12:37 +00:00
|
|
|
|
2023-06-20 14:41:38 +00:00
|
|
|
retval = class_register(&comedi_class);
|
|
|
|
if (retval) {
|
2014-07-17 19:27:32 +00:00
|
|
|
pr_err("failed to create class\n");
|
2017-06-16 18:35:35 +00:00
|
|
|
goto out_cdev_del;
|
2012-12-19 22:41:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)) {
|
2017-06-16 18:35:35 +00:00
|
|
|
retval = PTR_ERR(dev);
|
|
|
|
goto out_cleanup_board_minors;
|
2012-12-19 22:41:19 +00:00
|
|
|
}
|
2014-07-15 15:11:02 +00:00
|
|
|
/* comedi_alloc_board_minor() locked the mutex */
|
2019-04-17 14:39:29 +00:00
|
|
|
lockdep_assert_held(&dev->mutex);
|
2014-07-15 15:11:02 +00:00
|
|
|
mutex_unlock(&dev->mutex);
|
2012-12-19 22:41:19 +00:00
|
|
|
}
|
|
|
|
|
2016-12-30 11:25:52 +00:00
|
|
|
/* XXX requires /proc interface */
|
|
|
|
comedi_proc_init();
|
|
|
|
|
2012-12-19 22:41:19 +00:00
|
|
|
return 0;
|
2017-06-16 18:35:35 +00:00
|
|
|
|
|
|
|
out_cleanup_board_minors:
|
|
|
|
comedi_cleanup_board_minors();
|
2023-06-20 14:41:38 +00:00
|
|
|
class_unregister(&comedi_class);
|
2017-06-16 18:35:35 +00:00
|
|
|
out_cdev_del:
|
|
|
|
cdev_del(&comedi_cdev);
|
|
|
|
out_unregister_chrdev_region:
|
|
|
|
unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
|
|
|
|
return retval;
|
2012-12-19 22:41:19 +00:00
|
|
|
}
|
|
|
|
module_init(comedi_init);
|
|
|
|
|
|
|
|
static void __exit comedi_cleanup(void)
|
|
|
|
{
|
2013-01-28 17:07:39 +00:00
|
|
|
comedi_cleanup_board_minors();
|
2023-06-20 14:41:38 +00:00
|
|
|
class_unregister(&comedi_class);
|
2012-12-19 22:41:19 +00:00
|
|
|
cdev_del(&comedi_cdev);
|
|
|
|
unregister_chrdev_region(MKDEV(COMEDI_MAJOR, 0), COMEDI_NUM_MINORS);
|
|
|
|
|
|
|
|
comedi_proc_cleanup();
|
|
|
|
}
|
|
|
|
module_exit(comedi_cleanup);
|
|
|
|
|
2020-07-23 19:40:53 +00:00
|
|
|
MODULE_AUTHOR("https://www.comedi.org");
|
2012-12-19 22:40:59 +00:00
|
|
|
MODULE_DESCRIPTION("Comedi core module");
|
|
|
|
MODULE_LICENSE("GPL");
|