Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 1 | #include <linux/slab.h> |
| 2 | #include <linux/kernel.h> |
| 3 | #include <linux/module.h> |
| 4 | #include <linux/device.h> |
| 5 | #include <linux/workqueue.h> |
| 6 | #include <linux/kfifo.h> |
| 7 | #include <linux/mutex.h> |
| 8 | |
| 9 | #include "kfifo_buf.h" |
| 10 | |
Jonathan Cameron | a710cc7 | 2011-08-30 12:32:43 +0100 | [diff] [blame] | 11 | struct iio_kfifo { |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 12 | struct iio_buffer buffer; |
Jonathan Cameron | a710cc7 | 2011-08-30 12:32:43 +0100 | [diff] [blame] | 13 | struct kfifo kf; |
Jonathan Cameron | a710cc7 | 2011-08-30 12:32:43 +0100 | [diff] [blame] | 14 | int update_needed; |
Jonathan Cameron | a710cc7 | 2011-08-30 12:32:43 +0100 | [diff] [blame] | 15 | }; |
| 16 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 17 | #define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer) |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 18 | |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 19 | static inline int __iio_allocate_kfifo(struct iio_kfifo *buf, |
| 20 | int bytes_per_datum, int length) |
| 21 | { |
| 22 | if ((length == 0) || (bytes_per_datum == 0)) |
| 23 | return -EINVAL; |
| 24 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 25 | __iio_update_buffer(&buf->buffer, bytes_per_datum, length); |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 26 | return kfifo_alloc(&buf->kf, bytes_per_datum*length, GFP_KERNEL); |
| 27 | } |
| 28 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 29 | static int iio_request_update_kfifo(struct iio_buffer *r) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 30 | { |
| 31 | int ret = 0; |
| 32 | struct iio_kfifo *buf = iio_to_kfifo(r); |
| 33 | |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 34 | if (!buf->update_needed) |
| 35 | goto error_ret; |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 36 | kfifo_free(&buf->kf); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 37 | ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum, |
| 38 | buf->buffer.length); |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 39 | error_ret: |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 40 | return ret; |
| 41 | } |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 42 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 43 | static int iio_get_length_kfifo(struct iio_buffer *r) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 44 | { |
| 45 | return r->length; |
| 46 | } |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 47 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 48 | static IIO_BUFFER_ENABLE_ATTR; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 49 | static IIO_BUFFER_LENGTH_ATTR; |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 50 | |
| 51 | static struct attribute *iio_kfifo_attributes[] = { |
| 52 | &dev_attr_length.attr, |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 53 | &dev_attr_enable.attr, |
| 54 | NULL, |
| 55 | }; |
| 56 | |
| 57 | static struct attribute_group iio_kfifo_attribute_group = { |
| 58 | .attrs = iio_kfifo_attributes, |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 59 | .name = "buffer", |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 62 | static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 63 | { |
| 64 | return r->bytes_per_datum; |
| 65 | } |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 66 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 67 | static int iio_mark_update_needed_kfifo(struct iio_buffer *r) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 68 | { |
| 69 | struct iio_kfifo *kf = iio_to_kfifo(r); |
| 70 | kf->update_needed = true; |
| 71 | return 0; |
| 72 | } |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 73 | |
Lars-Peter Clausen | 869871b | 2011-12-19 15:23:48 +0100 | [diff] [blame] | 74 | static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd) |
| 75 | { |
| 76 | if (r->bytes_per_datum != bpd) { |
| 77 | r->bytes_per_datum = bpd; |
| 78 | iio_mark_update_needed_kfifo(r); |
| 79 | } |
| 80 | return 0; |
| 81 | } |
| 82 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 83 | static int iio_set_length_kfifo(struct iio_buffer *r, int length) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 84 | { |
| 85 | if (r->length != length) { |
| 86 | r->length = length; |
Lars-Peter Clausen | 869871b | 2011-12-19 15:23:48 +0100 | [diff] [blame] | 87 | iio_mark_update_needed_kfifo(r); |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 88 | } |
| 89 | return 0; |
| 90 | } |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 91 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 92 | static int iio_store_to_kfifo(struct iio_buffer *r, |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 93 | u8 *data, |
| 94 | s64 timestamp) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 95 | { |
| 96 | int ret; |
| 97 | struct iio_kfifo *kf = iio_to_kfifo(r); |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 98 | ret = kfifo_in(&kf->kf, data, r->bytes_per_datum); |
Jonathan Cameron | 4c3d153 | 2011-10-26 17:27:40 +0100 | [diff] [blame] | 99 | if (ret != r->bytes_per_datum) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 100 | return -EBUSY; |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 101 | return 0; |
| 102 | } |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 103 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 104 | static int iio_read_first_n_kfifo(struct iio_buffer *r, |
Jonathan Cameron | b26a218 | 2011-05-18 14:41:02 +0100 | [diff] [blame] | 105 | size_t n, char __user *buf) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 106 | { |
| 107 | int ret, copied; |
| 108 | struct iio_kfifo *kf = iio_to_kfifo(r); |
| 109 | |
Lars-Peter Clausen | 8fe6495 | 2011-12-12 09:33:14 +0100 | [diff] [blame] | 110 | if (n < r->bytes_per_datum) |
| 111 | return -EINVAL; |
| 112 | |
| 113 | n = rounddown(n, r->bytes_per_datum); |
| 114 | ret = kfifo_to_user(&kf->kf, buf, n, &copied); |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 115 | |
| 116 | return copied; |
| 117 | } |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 118 | |
Lars-Peter Clausen | 7e63234 | 2012-01-03 11:02:51 +0100 | [diff] [blame] | 119 | static const struct iio_buffer_access_funcs kfifo_access_funcs = { |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 120 | .store_to = &iio_store_to_kfifo, |
| 121 | .read_first_n = &iio_read_first_n_kfifo, |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 122 | .request_update = &iio_request_update_kfifo, |
| 123 | .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo, |
| 124 | .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo, |
| 125 | .get_length = &iio_get_length_kfifo, |
| 126 | .set_length = &iio_set_length_kfifo, |
| 127 | }; |
Lars-Peter Clausen | 7e63234 | 2012-01-03 11:02:51 +0100 | [diff] [blame] | 128 | |
| 129 | struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev) |
| 130 | { |
| 131 | struct iio_kfifo *kf; |
| 132 | |
| 133 | kf = kzalloc(sizeof *kf, GFP_KERNEL); |
| 134 | if (!kf) |
| 135 | return NULL; |
| 136 | kf->update_needed = true; |
| 137 | iio_buffer_init(&kf->buffer); |
| 138 | kf->buffer.attrs = &iio_kfifo_attribute_group; |
| 139 | kf->buffer.access = &kfifo_access_funcs; |
| 140 | |
| 141 | return &kf->buffer; |
| 142 | } |
| 143 | EXPORT_SYMBOL(iio_kfifo_allocate); |
| 144 | |
| 145 | void iio_kfifo_free(struct iio_buffer *r) |
| 146 | { |
| 147 | kfree(iio_to_kfifo(r)); |
| 148 | } |
| 149 | EXPORT_SYMBOL(iio_kfifo_free); |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 150 | |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 151 | MODULE_LICENSE("GPL"); |