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> |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 8 | #include <linux/iio/kfifo_buf.h> |
Jonathan Cameron | 7c388ec | 2012-06-30 13:52:00 +0100 | [diff] [blame] | 9 | #include <linux/sched.h> |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 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 | c559afb | 2012-06-30 13:52:00 +0100 | [diff] [blame] | 25 | return __kfifo_alloc((struct __kfifo *)&buf->kf, length, |
| 26 | bytes_per_datum, GFP_KERNEL); |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 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 | 7c388ec | 2012-06-30 13:52:00 +0100 | [diff] [blame] | 39 | r->stufftoread = false; |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 40 | error_ret: |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 41 | return ret; |
| 42 | } |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 43 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 44 | static int iio_get_length_kfifo(struct iio_buffer *r) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 45 | { |
| 46 | return r->length; |
| 47 | } |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 48 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 49 | static IIO_BUFFER_ENABLE_ATTR; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 50 | static IIO_BUFFER_LENGTH_ATTR; |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 51 | |
| 52 | static struct attribute *iio_kfifo_attributes[] = { |
| 53 | &dev_attr_length.attr, |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 54 | &dev_attr_enable.attr, |
| 55 | NULL, |
| 56 | }; |
| 57 | |
| 58 | static struct attribute_group iio_kfifo_attribute_group = { |
| 59 | .attrs = iio_kfifo_attributes, |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 60 | .name = "buffer", |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 63 | static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 64 | { |
| 65 | return r->bytes_per_datum; |
| 66 | } |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 67 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 68 | static int iio_mark_update_needed_kfifo(struct iio_buffer *r) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 69 | { |
| 70 | struct iio_kfifo *kf = iio_to_kfifo(r); |
| 71 | kf->update_needed = true; |
| 72 | return 0; |
| 73 | } |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 74 | |
Lars-Peter Clausen | 869871b | 2011-12-19 15:23:48 +0100 | [diff] [blame] | 75 | static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd) |
| 76 | { |
| 77 | if (r->bytes_per_datum != bpd) { |
| 78 | r->bytes_per_datum = bpd; |
| 79 | iio_mark_update_needed_kfifo(r); |
| 80 | } |
| 81 | return 0; |
| 82 | } |
| 83 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 84 | static int iio_set_length_kfifo(struct iio_buffer *r, int length) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 85 | { |
Jonathan Cameron | 7c388ec | 2012-06-30 13:52:00 +0100 | [diff] [blame] | 86 | /* Avoid an invalid state */ |
| 87 | if (length < 2) |
| 88 | length = 2; |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 89 | if (r->length != length) { |
| 90 | r->length = length; |
Lars-Peter Clausen | 869871b | 2011-12-19 15:23:48 +0100 | [diff] [blame] | 91 | iio_mark_update_needed_kfifo(r); |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 92 | } |
| 93 | return 0; |
| 94 | } |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 95 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 96 | static int iio_store_to_kfifo(struct iio_buffer *r, |
Lars-Peter Clausen | ce56ade | 2012-09-04 13:38:00 +0100 | [diff] [blame] | 97 | u8 *data) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 98 | { |
| 99 | int ret; |
| 100 | struct iio_kfifo *kf = iio_to_kfifo(r); |
Jonathan Cameron | c559afb | 2012-06-30 13:52:00 +0100 | [diff] [blame] | 101 | ret = kfifo_in(&kf->kf, data, 1); |
| 102 | if (ret != 1) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 103 | return -EBUSY; |
Jonathan Cameron | 7c388ec | 2012-06-30 13:52:00 +0100 | [diff] [blame] | 104 | r->stufftoread = true; |
| 105 | wake_up_interruptible(&r->pollq); |
Jonathan Cameron | c559afb | 2012-06-30 13:52:00 +0100 | [diff] [blame] | 106 | |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 107 | return 0; |
| 108 | } |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 109 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 110 | static int iio_read_first_n_kfifo(struct iio_buffer *r, |
Jonathan Cameron | b26a218 | 2011-05-18 14:41:02 +0100 | [diff] [blame] | 111 | size_t n, char __user *buf) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 112 | { |
| 113 | int ret, copied; |
| 114 | struct iio_kfifo *kf = iio_to_kfifo(r); |
| 115 | |
Jonathan Cameron | 08ce9b4 | 2012-08-27 18:40:04 +0100 | [diff] [blame] | 116 | if (n < r->bytes_per_datum || r->bytes_per_datum == 0) |
Lars-Peter Clausen | 8fe6495 | 2011-12-12 09:33:14 +0100 | [diff] [blame] | 117 | return -EINVAL; |
| 118 | |
Lars-Peter Clausen | 8fe6495 | 2011-12-12 09:33:14 +0100 | [diff] [blame] | 119 | ret = kfifo_to_user(&kf->kf, buf, n, &copied); |
Jonathan Cameron | 08ce9b4 | 2012-08-27 18:40:04 +0100 | [diff] [blame] | 120 | if (ret < 0) |
| 121 | return ret; |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 122 | |
Jonathan Cameron | 7c388ec | 2012-06-30 13:52:00 +0100 | [diff] [blame] | 123 | if (kfifo_is_empty(&kf->kf)) |
| 124 | r->stufftoread = false; |
| 125 | /* verify it is still empty to avoid race */ |
| 126 | if (!kfifo_is_empty(&kf->kf)) |
| 127 | r->stufftoread = true; |
| 128 | |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 129 | return copied; |
| 130 | } |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 131 | |
Lars-Peter Clausen | 7e63234 | 2012-01-03 11:02:51 +0100 | [diff] [blame] | 132 | static const struct iio_buffer_access_funcs kfifo_access_funcs = { |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 133 | .store_to = &iio_store_to_kfifo, |
| 134 | .read_first_n = &iio_read_first_n_kfifo, |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 135 | .request_update = &iio_request_update_kfifo, |
| 136 | .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo, |
| 137 | .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo, |
| 138 | .get_length = &iio_get_length_kfifo, |
| 139 | .set_length = &iio_set_length_kfifo, |
| 140 | }; |
Lars-Peter Clausen | 7e63234 | 2012-01-03 11:02:51 +0100 | [diff] [blame] | 141 | |
| 142 | struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev) |
| 143 | { |
| 144 | struct iio_kfifo *kf; |
| 145 | |
| 146 | kf = kzalloc(sizeof *kf, GFP_KERNEL); |
| 147 | if (!kf) |
| 148 | return NULL; |
| 149 | kf->update_needed = true; |
| 150 | iio_buffer_init(&kf->buffer); |
| 151 | kf->buffer.attrs = &iio_kfifo_attribute_group; |
| 152 | kf->buffer.access = &kfifo_access_funcs; |
Jonathan Cameron | 7c388ec | 2012-06-30 13:52:00 +0100 | [diff] [blame] | 153 | kf->buffer.length = 2; |
Lars-Peter Clausen | 7e63234 | 2012-01-03 11:02:51 +0100 | [diff] [blame] | 154 | return &kf->buffer; |
| 155 | } |
| 156 | EXPORT_SYMBOL(iio_kfifo_allocate); |
| 157 | |
| 158 | void iio_kfifo_free(struct iio_buffer *r) |
| 159 | { |
| 160 | kfree(iio_to_kfifo(r)); |
| 161 | } |
| 162 | EXPORT_SYMBOL(iio_kfifo_free); |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 163 | |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 164 | MODULE_LICENSE("GPL"); |