2011-02-11 21:09:10 +08:00
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/workqueue.h>
|
|
|
|
#include <linux/kfifo.h>
|
|
|
|
#include <linux/mutex.h>
|
2012-04-25 22:54:58 +08:00
|
|
|
#include <linux/iio/kfifo_buf.h>
|
2012-06-30 20:52:00 +08:00
|
|
|
#include <linux/sched.h>
|
2013-09-15 23:31:00 +08:00
|
|
|
#include <linux/poll.h>
|
2011-02-11 21:09:10 +08:00
|
|
|
|
2011-08-30 19:32:43 +08:00
|
|
|
struct iio_kfifo {
|
2011-09-21 18:15:57 +08:00
|
|
|
struct iio_buffer buffer;
|
2011-08-30 19:32:43 +08:00
|
|
|
struct kfifo kf;
|
|
|
|
int update_needed;
|
|
|
|
};
|
|
|
|
|
2011-09-21 18:15:57 +08:00
|
|
|
#define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer)
|
2011-05-18 21:42:24 +08:00
|
|
|
|
2011-02-11 21:09:10 +08:00
|
|
|
static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
|
|
|
|
int bytes_per_datum, int length)
|
|
|
|
{
|
|
|
|
if ((length == 0) || (bytes_per_datum == 0))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2012-06-30 20:52:00 +08:00
|
|
|
return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
|
|
|
|
bytes_per_datum, GFP_KERNEL);
|
2011-02-11 21:09:10 +08:00
|
|
|
}
|
|
|
|
|
2011-09-21 18:15:57 +08:00
|
|
|
static int iio_request_update_kfifo(struct iio_buffer *r)
|
2011-02-11 21:09:10 +08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
struct iio_kfifo *buf = iio_to_kfifo(r);
|
|
|
|
|
|
|
|
if (!buf->update_needed)
|
|
|
|
goto error_ret;
|
|
|
|
kfifo_free(&buf->kf);
|
2011-09-21 18:15:57 +08:00
|
|
|
ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum,
|
|
|
|
buf->buffer.length);
|
2012-06-30 20:52:00 +08:00
|
|
|
r->stufftoread = false;
|
2011-02-11 21:09:10 +08:00
|
|
|
error_ret:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-09-21 18:15:57 +08:00
|
|
|
static int iio_get_length_kfifo(struct iio_buffer *r)
|
2011-02-11 21:09:10 +08:00
|
|
|
{
|
|
|
|
return r->length;
|
|
|
|
}
|
|
|
|
|
2011-09-21 18:15:57 +08:00
|
|
|
static IIO_BUFFER_ENABLE_ATTR;
|
|
|
|
static IIO_BUFFER_LENGTH_ATTR;
|
2011-02-11 21:09:10 +08:00
|
|
|
|
|
|
|
static struct attribute *iio_kfifo_attributes[] = {
|
|
|
|
&dev_attr_length.attr,
|
|
|
|
&dev_attr_enable.attr,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct attribute_group iio_kfifo_attribute_group = {
|
|
|
|
.attrs = iio_kfifo_attributes,
|
2011-08-30 19:32:47 +08:00
|
|
|
.name = "buffer",
|
2011-02-11 21:09:10 +08:00
|
|
|
};
|
|
|
|
|
2011-09-21 18:15:57 +08:00
|
|
|
static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r)
|
2011-02-11 21:09:10 +08:00
|
|
|
{
|
|
|
|
return r->bytes_per_datum;
|
|
|
|
}
|
|
|
|
|
2011-12-19 22:23:48 +08:00
|
|
|
static int iio_mark_update_needed_kfifo(struct iio_buffer *r)
|
2011-02-11 21:09:10 +08:00
|
|
|
{
|
2011-12-19 22:23:48 +08:00
|
|
|
struct iio_kfifo *kf = iio_to_kfifo(r);
|
|
|
|
kf->update_needed = true;
|
2011-02-11 21:09:10 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-19 22:23:48 +08:00
|
|
|
static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd)
|
2011-02-11 21:09:10 +08:00
|
|
|
{
|
2011-12-19 22:23:48 +08:00
|
|
|
if (r->bytes_per_datum != bpd) {
|
|
|
|
r->bytes_per_datum = bpd;
|
|
|
|
iio_mark_update_needed_kfifo(r);
|
|
|
|
}
|
2011-02-11 21:09:10 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-21 18:15:57 +08:00
|
|
|
static int iio_set_length_kfifo(struct iio_buffer *r, int length)
|
2011-02-11 21:09:10 +08:00
|
|
|
{
|
2012-06-30 20:52:00 +08:00
|
|
|
/* Avoid an invalid state */
|
|
|
|
if (length < 2)
|
|
|
|
length = 2;
|
2011-02-11 21:09:10 +08:00
|
|
|
if (r->length != length) {
|
|
|
|
r->length = length;
|
2011-12-19 22:23:48 +08:00
|
|
|
iio_mark_update_needed_kfifo(r);
|
2011-02-11 21:09:10 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-21 18:15:57 +08:00
|
|
|
static int iio_store_to_kfifo(struct iio_buffer *r,
|
2013-09-16 00:50:00 +08:00
|
|
|
const void *data)
|
2011-02-11 21:09:10 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct iio_kfifo *kf = iio_to_kfifo(r);
|
2012-06-30 20:52:00 +08:00
|
|
|
ret = kfifo_in(&kf->kf, data, 1);
|
|
|
|
if (ret != 1)
|
2011-02-11 21:09:10 +08:00
|
|
|
return -EBUSY;
|
2012-06-30 20:52:00 +08:00
|
|
|
r->stufftoread = true;
|
2013-09-15 23:31:00 +08:00
|
|
|
wake_up_interruptible_poll(&r->pollq, POLLIN | POLLRDNORM);
|
2012-06-30 20:52:00 +08:00
|
|
|
|
2011-02-11 21:09:10 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-09-21 18:15:57 +08:00
|
|
|
static int iio_read_first_n_kfifo(struct iio_buffer *r,
|
2011-05-18 21:41:02 +08:00
|
|
|
size_t n, char __user *buf)
|
2011-02-11 21:09:10 +08:00
|
|
|
{
|
|
|
|
int ret, copied;
|
|
|
|
struct iio_kfifo *kf = iio_to_kfifo(r);
|
|
|
|
|
2012-08-28 01:40:04 +08:00
|
|
|
if (n < r->bytes_per_datum || r->bytes_per_datum == 0)
|
2011-12-12 16:33:14 +08:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
ret = kfifo_to_user(&kf->kf, buf, n, &copied);
|
2012-08-28 01:40:04 +08:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2011-02-11 21:09:10 +08:00
|
|
|
|
2012-06-30 20:52:00 +08:00
|
|
|
if (kfifo_is_empty(&kf->kf))
|
|
|
|
r->stufftoread = false;
|
|
|
|
/* verify it is still empty to avoid race */
|
|
|
|
if (!kfifo_is_empty(&kf->kf))
|
|
|
|
r->stufftoread = true;
|
|
|
|
|
2011-02-11 21:09:10 +08:00
|
|
|
return copied;
|
|
|
|
}
|
2011-05-18 21:42:24 +08:00
|
|
|
|
2013-10-04 19:06:00 +08:00
|
|
|
static void iio_kfifo_buffer_release(struct iio_buffer *buffer)
|
|
|
|
{
|
2013-10-15 16:30:00 +08:00
|
|
|
struct iio_kfifo *kf = iio_to_kfifo(buffer);
|
|
|
|
|
|
|
|
kfifo_free(&kf->kf);
|
|
|
|
kfree(kf);
|
2013-10-04 19:06:00 +08:00
|
|
|
}
|
|
|
|
|
2012-01-03 18:02:51 +08:00
|
|
|
static const struct iio_buffer_access_funcs kfifo_access_funcs = {
|
2011-05-18 21:42:24 +08:00
|
|
|
.store_to = &iio_store_to_kfifo,
|
|
|
|
.read_first_n = &iio_read_first_n_kfifo,
|
|
|
|
.request_update = &iio_request_update_kfifo,
|
|
|
|
.get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo,
|
|
|
|
.set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo,
|
|
|
|
.get_length = &iio_get_length_kfifo,
|
|
|
|
.set_length = &iio_set_length_kfifo,
|
2013-10-04 19:06:00 +08:00
|
|
|
.release = &iio_kfifo_buffer_release,
|
2011-05-18 21:42:24 +08:00
|
|
|
};
|
2012-01-03 18:02:51 +08:00
|
|
|
|
|
|
|
struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev)
|
|
|
|
{
|
|
|
|
struct iio_kfifo *kf;
|
|
|
|
|
|
|
|
kf = kzalloc(sizeof *kf, GFP_KERNEL);
|
|
|
|
if (!kf)
|
|
|
|
return NULL;
|
|
|
|
kf->update_needed = true;
|
|
|
|
iio_buffer_init(&kf->buffer);
|
|
|
|
kf->buffer.attrs = &iio_kfifo_attribute_group;
|
|
|
|
kf->buffer.access = &kfifo_access_funcs;
|
2012-06-30 20:52:00 +08:00
|
|
|
kf->buffer.length = 2;
|
2012-01-03 18:02:51 +08:00
|
|
|
return &kf->buffer;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(iio_kfifo_allocate);
|
|
|
|
|
|
|
|
void iio_kfifo_free(struct iio_buffer *r)
|
|
|
|
{
|
2013-10-04 19:06:00 +08:00
|
|
|
iio_buffer_put(r);
|
2012-01-03 18:02:51 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(iio_kfifo_free);
|
2011-05-18 21:42:24 +08:00
|
|
|
|
2011-02-11 21:09:10 +08:00
|
|
|
MODULE_LICENSE("GPL");
|