linux/drivers/staging/iio/industrialio-ring.c

562 lines
14 KiB
C
Raw Normal View History

/* The industrial I/O core
*
* Copyright (c) 2008 Jonathan Cameron
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*
* Handling of ring allocation / resizing.
*
*
* Things to look at here.
* - Better memory allocation techniques?
* - Alternative access techniques?
*/
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/poll.h>
#include <linux/cdev.h>
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h percpu.h is included by sched.h and module.h and thus ends up being included when building most .c files. percpu.h includes slab.h which in turn includes gfp.h making everything defined by the two files universally available and complicating inclusion dependencies. percpu.h -> slab.h dependency is about to be removed. Prepare for this change by updating users of gfp and slab facilities include those headers directly instead of assuming availability. As this conversion needs to touch large number of source files, the following script is used as the basis of conversion. http://userweb.kernel.org/~tj/misc/slabh-sweep.py The script does the followings. * Scan files for gfp and slab usages and update includes such that only the necessary includes are there. ie. if only gfp is used, gfp.h, if slab is used, slab.h. * When the script inserts a new include, it looks at the include blocks and try to put the new include such that its order conforms to its surrounding. It's put in the include block which contains core kernel includes, in the same order that the rest are ordered - alphabetical, Christmas tree, rev-Xmas-tree or at the end if there doesn't seem to be any matching order. * If the script can't find a place to put a new include (mostly because the file doesn't have fitting include block), it prints out an error message indicating which .h file needs to be added to the file. The conversion was done in the following steps. 1. The initial automatic conversion of all .c files updated slightly over 4000 files, deleting around 700 includes and adding ~480 gfp.h and ~3000 slab.h inclusions. The script emitted errors for ~400 files. 2. Each error was manually checked. Some didn't need the inclusion, some needed manual addition while adding it to implementation .h or embedding .c file was more appropriate for others. This step added inclusions to around 150 files. 3. The script was run again and the output was compared to the edits from #2 to make sure no file was left behind. 4. Several build tests were done and a couple of problems were fixed. e.g. lib/decompress_*.c used malloc/free() wrappers around slab APIs requiring slab.h to be added manually. 5. The script was run on all .h files but without automatically editing them as sprinkling gfp.h and slab.h inclusions around .h files could easily lead to inclusion dependency hell. Most gfp.h inclusion directives were ignored as stuff from gfp.h was usually wildly available and often used in preprocessor macros. Each slab.h inclusion directive was examined and added manually as necessary. 6. percpu.h was updated not to include slab.h. 7. Build test were done on the following configurations and failures were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my distributed build env didn't work with gcov compiles) and a few more options had to be turned off depending on archs to make things build (like ipr on powerpc/64 which failed due to missing writeq). * x86 and x86_64 UP and SMP allmodconfig and a custom test config. * powerpc and powerpc64 SMP allmodconfig * sparc and sparc64 SMP allmodconfig * ia64 SMP allmodconfig * s390 SMP allmodconfig * alpha SMP allmodconfig * um on x86_64 SMP allmodconfig 8. percpu.h modifications were reverted so that it could be applied as a separate patch and serve as bisection point. Given the fact that I had only a couple of failures from tests on step 6, I'm fairly confident about the coverage of this conversion patch. If there is a breakage, it's likely to be something in one of the arch headers which should be easily discoverable easily on most builds of the specific arch. Signed-off-by: Tejun Heo <tj@kernel.org> Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org> Cc: Ingo Molnar <mingo@redhat.com> Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 09:04:11 +01:00
#include <linux/slab.h>
#include "iio.h"
#include "ring_generic.h"
int iio_push_ring_event(struct iio_ring_buffer *ring_buf,
int event_code,
s64 timestamp)
{
return __iio_push_event(&ring_buf->ev_int,
event_code,
timestamp,
&ring_buf->shared_ev_pointer);
}
EXPORT_SYMBOL(iio_push_ring_event);
int iio_push_or_escallate_ring_event(struct iio_ring_buffer *ring_buf,
int event_code,
s64 timestamp)
{
if (ring_buf->shared_ev_pointer.ev_p)
__iio_change_event(ring_buf->shared_ev_pointer.ev_p,
event_code,
timestamp);
else
return iio_push_ring_event(ring_buf,
event_code,
timestamp);
return 0;
}
EXPORT_SYMBOL(iio_push_or_escallate_ring_event);
/**
* iio_ring_open() - chrdev file open for ring buffer access
*
* This function relies on all ring buffer implementations having an
* iio_ring_buffer as their first element.
**/
static int iio_ring_open(struct inode *inode, struct file *filp)
{
struct iio_handler *hand
= container_of(inode->i_cdev, struct iio_handler, chrdev);
struct iio_ring_buffer *rb = hand->private;
filp->private_data = hand->private;
if (rb->access.mark_in_use)
rb->access.mark_in_use(rb);
return 0;
}
/**
* iio_ring_release() - chrdev file close ring buffer access
*
* This function relies on all ring buffer implementations having an
* iio_ring_buffer as their first element.
**/
static int iio_ring_release(struct inode *inode, struct file *filp)
{
struct cdev *cd = inode->i_cdev;
struct iio_handler *hand = iio_cdev_to_handler(cd);
struct iio_ring_buffer *rb = hand->private;
clear_bit(IIO_BUSY_BIT_POS, &rb->access_handler.flags);
if (rb->access.unmark_in_use)
rb->access.unmark_in_use(rb);
return 0;
}
/**
* iio_ring_rip_outer() - chrdev read for ring buffer access
*
* This function relies on all ring buffer implementations having an
* iio_ring _bufer as their first element.
**/
static ssize_t iio_ring_rip_outer(struct file *filp, char __user *buf,
size_t count, loff_t *f_ps)
{
struct iio_ring_buffer *rb = filp->private_data;
int ret, dead_offset, copied;
u8 *data;
/* rip lots must exist. */
if (!rb->access.rip_lots)
return -EINVAL;
copied = rb->access.rip_lots(rb, count, &data, &dead_offset);
if (copied < 0) {
ret = copied;
goto error_ret;
}
if (copy_to_user(buf, data + dead_offset, copied)) {
ret = -EFAULT;
goto error_free_data_cpy;
}
/* In clever ring buffer designs this may not need to be freed.
* When such a design exists I'll add this to ring access funcs.
*/
kfree(data);
return copied;
error_free_data_cpy:
kfree(data);
error_ret:
return ret;
}
static const struct file_operations iio_ring_fileops = {
.read = iio_ring_rip_outer,
.release = iio_ring_release,
.open = iio_ring_open,
.owner = THIS_MODULE,
};
/**
* __iio_request_ring_buffer_event_chrdev() - allocate ring event chrdev
* @buf: ring buffer whose event chrdev we are allocating
* @id: id of this ring buffer (typically 0)
* @owner: the module who owns the ring buffer (for ref counting)
* @dev: device with which the chrdev is associated
**/
static inline int
__iio_request_ring_buffer_event_chrdev(struct iio_ring_buffer *buf,
int id,
struct module *owner,
struct device *dev)
{
int ret;
snprintf(buf->ev_int._name, sizeof(buf->ev_int._name),
"%s:event%d",
dev_name(&buf->dev),
id);
ret = iio_setup_ev_int(&(buf->ev_int),
buf->ev_int._name,
owner,
dev);
if (ret)
goto error_ret;
return 0;
error_ret:
return ret;
}
static inline void
__iio_free_ring_buffer_event_chrdev(struct iio_ring_buffer *buf)
{
iio_free_ev_int(&(buf->ev_int));
}
static void iio_ring_access_release(struct device *dev)
{
struct iio_ring_buffer *buf
= access_dev_to_iio_ring_buffer(dev);
cdev_del(&buf->access_handler.chrdev);
iio_device_free_chrdev_minor(MINOR(dev->devt));
}
static struct device_type iio_ring_access_type = {
.release = iio_ring_access_release,
};
static inline int
__iio_request_ring_buffer_access_chrdev(struct iio_ring_buffer *buf,
int id,
struct module *owner)
{
int ret, minor;
buf->access_handler.flags = 0;
buf->access_dev.parent = &buf->dev;
buf->access_dev.bus = &iio_bus_type;
buf->access_dev.type = &iio_ring_access_type;
device_initialize(&buf->access_dev);
minor = iio_device_get_chrdev_minor();
if (minor < 0) {
ret = minor;
goto error_device_put;
}
buf->access_dev.devt = MKDEV(MAJOR(iio_devt), minor);
buf->access_id = id;
dev_set_name(&buf->access_dev, "%s:access%d",
dev_name(&buf->dev),
buf->access_id);
ret = device_add(&buf->access_dev);
if (ret < 0) {
printk(KERN_ERR "failed to add the ring access dev\n");
goto error_device_put;
}
cdev_init(&buf->access_handler.chrdev, &iio_ring_fileops);
buf->access_handler.chrdev.owner = owner;
ret = cdev_add(&buf->access_handler.chrdev, buf->access_dev.devt, 1);
if (ret) {
printk(KERN_ERR "failed to allocate ring access chrdev\n");
goto error_device_unregister;
}
return 0;
error_device_unregister:
device_unregister(&buf->access_dev);
error_device_put:
put_device(&buf->access_dev);
return ret;
}
static void __iio_free_ring_buffer_access_chrdev(struct iio_ring_buffer *buf)
{
device_unregister(&buf->access_dev);
}
void iio_ring_buffer_init(struct iio_ring_buffer *ring,
struct iio_dev *dev_info)
{
if (ring->access.mark_param_change)
ring->access.mark_param_change(ring);
ring->indio_dev = dev_info;
ring->ev_int.private = ring;
ring->access_handler.private = ring;
ring->shared_ev_pointer.ev_p = NULL;
spin_lock_init(&ring->shared_ev_pointer.lock);
}
EXPORT_SYMBOL(iio_ring_buffer_init);
int iio_ring_buffer_register(struct iio_ring_buffer *ring, int id)
{
int ret;
ring->id = id;
dev_set_name(&ring->dev, "%s:buffer%d",
dev_name(ring->dev.parent),
ring->id);
ret = device_add(&ring->dev);
if (ret)
goto error_ret;
ret = __iio_request_ring_buffer_event_chrdev(ring,
0,
ring->owner,
&ring->dev);
if (ret)
goto error_remove_device;
ret = __iio_request_ring_buffer_access_chrdev(ring,
0,
ring->owner);
if (ret)
goto error_free_ring_buffer_event_chrdev;
if (ring->scan_el_attrs) {
ret = sysfs_create_group(&ring->dev.kobj,
ring->scan_el_attrs);
if (ret) {
dev_err(&ring->dev,
"Failed to add sysfs scan elements\n");
goto error_free_ring_buffer_event_chrdev;
}
}
return ret;
error_free_ring_buffer_event_chrdev:
__iio_free_ring_buffer_event_chrdev(ring);
error_remove_device:
device_del(&ring->dev);
error_ret:
return ret;
}
EXPORT_SYMBOL(iio_ring_buffer_register);
void iio_ring_buffer_unregister(struct iio_ring_buffer *ring)
{
if (ring->scan_el_attrs)
sysfs_remove_group(&ring->dev.kobj,
ring->scan_el_attrs);
__iio_free_ring_buffer_access_chrdev(ring);
__iio_free_ring_buffer_event_chrdev(ring);
device_del(&ring->dev);
}
EXPORT_SYMBOL(iio_ring_buffer_unregister);
ssize_t iio_read_ring_length(struct device *dev,
struct device_attribute *attr,
char *buf)
{
int len = 0;
struct iio_ring_buffer *ring = dev_get_drvdata(dev);
if (ring->access.get_length)
len = sprintf(buf, "%d\n",
ring->access.get_length(ring));
return len;
}
EXPORT_SYMBOL(iio_read_ring_length);
ssize_t iio_write_ring_length(struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t len)
{
int ret;
ulong val;
struct iio_ring_buffer *ring = dev_get_drvdata(dev);
ret = strict_strtoul(buf, 10, &val);
if (ret)
return ret;
if (ring->access.get_length)
if (val == ring->access.get_length(ring))
return len;
if (ring->access.set_length) {
ring->access.set_length(ring, val);
if (ring->access.mark_param_change)
ring->access.mark_param_change(ring);
}
return len;
}
EXPORT_SYMBOL(iio_write_ring_length);
ssize_t iio_read_ring_bytes_per_datum(struct device *dev,
struct device_attribute *attr,
char *buf)
{
int len = 0;
struct iio_ring_buffer *ring = dev_get_drvdata(dev);
if (ring->access.get_bytes_per_datum)
len = sprintf(buf, "%d\n",
ring->access.get_bytes_per_datum(ring));
return len;
}
EXPORT_SYMBOL(iio_read_ring_bytes_per_datum);
ssize_t iio_store_ring_enable(struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t len)
{
int ret;
bool requested_state, current_state;
int previous_mode;
struct iio_ring_buffer *ring = dev_get_drvdata(dev);
struct iio_dev *dev_info = ring->indio_dev;
mutex_lock(&dev_info->mlock);
previous_mode = dev_info->currentmode;
requested_state = !(buf[0] == '0');
current_state = !!(previous_mode & INDIO_ALL_RING_MODES);
if (current_state == requested_state) {
printk(KERN_INFO "iio-ring, current state requested again\n");
goto done;
}
if (requested_state) {
if (ring->preenable) {
ret = ring->preenable(dev_info);
if (ret) {
printk(KERN_ERR
"Buffer not started:"
"ring preenable failed\n");
goto error_ret;
}
}
if (ring->access.request_update) {
ret = ring->access.request_update(ring);
if (ret) {
printk(KERN_INFO
"Buffer not started:"
"ring parameter update failed\n");
goto error_ret;
}
}
if (ring->access.mark_in_use)
ring->access.mark_in_use(ring);
/* Definitely possible for devices to support both of these.*/
if (dev_info->modes & INDIO_RING_TRIGGERED) {
if (!dev_info->trig) {
printk(KERN_INFO
"Buffer not started: no trigger\n");
ret = -EINVAL;
if (ring->access.unmark_in_use)
ring->access.unmark_in_use(ring);
goto error_ret;
}
dev_info->currentmode = INDIO_RING_TRIGGERED;
} else if (dev_info->modes & INDIO_RING_HARDWARE_BUFFER)
dev_info->currentmode = INDIO_RING_HARDWARE_BUFFER;
else { /* should never be reached */
ret = -EINVAL;
goto error_ret;
}
if (ring->postenable) {
ret = ring->postenable(dev_info);
if (ret) {
printk(KERN_INFO
"Buffer not started:"
"postenable failed\n");
if (ring->access.unmark_in_use)
ring->access.unmark_in_use(ring);
dev_info->currentmode = previous_mode;
if (ring->postdisable)
ring->postdisable(dev_info);
goto error_ret;
}
}
} else {
if (ring->predisable) {
ret = ring->predisable(dev_info);
if (ret)
goto error_ret;
}
if (ring->access.unmark_in_use)
ring->access.unmark_in_use(ring);
dev_info->currentmode = INDIO_DIRECT_MODE;
if (ring->postdisable) {
ret = ring->postdisable(dev_info);
if (ret)
goto error_ret;
}
}
done:
mutex_unlock(&dev_info->mlock);
return len;
error_ret:
mutex_unlock(&dev_info->mlock);
return ret;
}
EXPORT_SYMBOL(iio_store_ring_enable);
ssize_t iio_show_ring_enable(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct iio_ring_buffer *ring = dev_get_drvdata(dev);
return sprintf(buf, "%d\n", !!(ring->indio_dev->currentmode
& INDIO_ALL_RING_MODES));
}
EXPORT_SYMBOL(iio_show_ring_enable);
ssize_t iio_scan_el_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
int ret;
struct iio_ring_buffer *ring = dev_get_drvdata(dev);
struct iio_scan_el *this_el = to_iio_scan_el(attr);
ret = iio_scan_mask_query(ring, this_el->number);
if (ret < 0)
return ret;
return sprintf(buf, "%d\n", ret);
}
EXPORT_SYMBOL(iio_scan_el_show);
ssize_t iio_scan_el_store(struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t len)
{
int ret = 0;
bool state;
struct iio_ring_buffer *ring = dev_get_drvdata(dev);
struct iio_dev *indio_dev = ring->indio_dev;
struct iio_scan_el *this_el = to_iio_scan_el(attr);
state = !(buf[0] == '0');
mutex_lock(&indio_dev->mlock);
if (indio_dev->currentmode == INDIO_RING_TRIGGERED) {
ret = -EBUSY;
goto error_ret;
}
ret = iio_scan_mask_query(ring, this_el->number);
if (ret < 0)
goto error_ret;
if (!state && ret) {
ret = iio_scan_mask_clear(ring, this_el->number);
if (ret)
goto error_ret;
} else if (state && !ret) {
ret = iio_scan_mask_set(ring, this_el->number);
if (ret)
goto error_ret;
}
if (this_el->set_state)
ret = this_el->set_state(this_el, indio_dev, state);
error_ret:
mutex_unlock(&indio_dev->mlock);
return ret ? ret : len;
}
EXPORT_SYMBOL(iio_scan_el_store);
ssize_t iio_scan_el_ts_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct iio_ring_buffer *ring = dev_get_drvdata(dev);
return sprintf(buf, "%d\n", ring->scan_timestamp);
}
EXPORT_SYMBOL(iio_scan_el_ts_show);
ssize_t iio_scan_el_ts_store(struct device *dev,
struct device_attribute *attr,
const char *buf,
size_t len)
{
int ret = 0;
struct iio_ring_buffer *ring = dev_get_drvdata(dev);
struct iio_dev *indio_dev = ring->indio_dev;
bool state;
state = !(buf[0] == '0');
mutex_lock(&indio_dev->mlock);
if (indio_dev->currentmode == INDIO_RING_TRIGGERED) {
ret = -EBUSY;
goto error_ret;
}
ring->scan_timestamp = state;
error_ret:
mutex_unlock(&indio_dev->mlock);
return ret ? ret : len;
}
EXPORT_SYMBOL(iio_scan_el_ts_store);