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> |
Lars-Peter Clausen | 623c334 | 2013-09-15 16:31:00 +0100 | [diff] [blame] | 10 | #include <linux/poll.h> |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 11 | |
Jonathan Cameron | a710cc7 | 2011-08-30 12:32:43 +0100 | [diff] [blame] | 12 | struct iio_kfifo { |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 13 | struct iio_buffer buffer; |
Jonathan Cameron | a710cc7 | 2011-08-30 12:32:43 +0100 | [diff] [blame] | 14 | struct kfifo kf; |
Lars-Peter Clausen | 0894d80d | 2013-10-15 09:30:00 +0100 | [diff] [blame^] | 15 | struct mutex user_lock; |
Jonathan Cameron | a710cc7 | 2011-08-30 12:32:43 +0100 | [diff] [blame] | 16 | int update_needed; |
Jonathan Cameron | a710cc7 | 2011-08-30 12:32:43 +0100 | [diff] [blame] | 17 | }; |
| 18 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 19 | #define iio_to_kfifo(r) container_of(r, struct iio_kfifo, buffer) |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 20 | |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 21 | static inline int __iio_allocate_kfifo(struct iio_kfifo *buf, |
| 22 | int bytes_per_datum, int length) |
| 23 | { |
| 24 | if ((length == 0) || (bytes_per_datum == 0)) |
| 25 | return -EINVAL; |
| 26 | |
Jonathan Cameron | c559afb | 2012-06-30 13:52:00 +0100 | [diff] [blame] | 27 | return __kfifo_alloc((struct __kfifo *)&buf->kf, length, |
| 28 | bytes_per_datum, GFP_KERNEL); |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 31 | static int iio_request_update_kfifo(struct iio_buffer *r) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 32 | { |
| 33 | int ret = 0; |
| 34 | struct iio_kfifo *buf = iio_to_kfifo(r); |
| 35 | |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 36 | if (!buf->update_needed) |
| 37 | goto error_ret; |
Lars-Peter Clausen | 0894d80d | 2013-10-15 09:30:00 +0100 | [diff] [blame^] | 38 | mutex_lock(&buf->user_lock); |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 39 | kfifo_free(&buf->kf); |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 40 | ret = __iio_allocate_kfifo(buf, buf->buffer.bytes_per_datum, |
| 41 | buf->buffer.length); |
Jonathan Cameron | 7c388ec | 2012-06-30 13:52:00 +0100 | [diff] [blame] | 42 | r->stufftoread = false; |
Lars-Peter Clausen | 0894d80d | 2013-10-15 09:30:00 +0100 | [diff] [blame^] | 43 | mutex_unlock(&buf->user_lock); |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 44 | error_ret: |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 45 | return ret; |
| 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 int iio_get_length_kfifo(struct iio_buffer *r) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 49 | { |
| 50 | return r->length; |
| 51 | } |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 52 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 53 | static IIO_BUFFER_ENABLE_ATTR; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 54 | static IIO_BUFFER_LENGTH_ATTR; |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 55 | |
| 56 | static struct attribute *iio_kfifo_attributes[] = { |
| 57 | &dev_attr_length.attr, |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 58 | &dev_attr_enable.attr, |
| 59 | NULL, |
| 60 | }; |
| 61 | |
| 62 | static struct attribute_group iio_kfifo_attribute_group = { |
| 63 | .attrs = iio_kfifo_attributes, |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 64 | .name = "buffer", |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 67 | static int iio_get_bytes_per_datum_kfifo(struct iio_buffer *r) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 68 | { |
| 69 | return r->bytes_per_datum; |
| 70 | } |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 71 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 72 | static int iio_mark_update_needed_kfifo(struct iio_buffer *r) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 73 | { |
| 74 | struct iio_kfifo *kf = iio_to_kfifo(r); |
| 75 | kf->update_needed = true; |
| 76 | return 0; |
| 77 | } |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 78 | |
Lars-Peter Clausen | 869871b | 2011-12-19 15:23:48 +0100 | [diff] [blame] | 79 | static int iio_set_bytes_per_datum_kfifo(struct iio_buffer *r, size_t bpd) |
| 80 | { |
| 81 | if (r->bytes_per_datum != bpd) { |
| 82 | r->bytes_per_datum = bpd; |
| 83 | iio_mark_update_needed_kfifo(r); |
| 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 88 | static int iio_set_length_kfifo(struct iio_buffer *r, int length) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 89 | { |
Jonathan Cameron | 7c388ec | 2012-06-30 13:52:00 +0100 | [diff] [blame] | 90 | /* Avoid an invalid state */ |
| 91 | if (length < 2) |
| 92 | length = 2; |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 93 | if (r->length != length) { |
| 94 | r->length = length; |
Lars-Peter Clausen | 869871b | 2011-12-19 15:23:48 +0100 | [diff] [blame] | 95 | iio_mark_update_needed_kfifo(r); |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 96 | } |
| 97 | return 0; |
| 98 | } |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 99 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 100 | static int iio_store_to_kfifo(struct iio_buffer *r, |
Lars-Peter Clausen | 5d65d92 | 2013-09-15 17:50:00 +0100 | [diff] [blame] | 101 | const void *data) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 102 | { |
| 103 | int ret; |
| 104 | struct iio_kfifo *kf = iio_to_kfifo(r); |
Jonathan Cameron | c559afb | 2012-06-30 13:52:00 +0100 | [diff] [blame] | 105 | ret = kfifo_in(&kf->kf, data, 1); |
| 106 | if (ret != 1) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 107 | return -EBUSY; |
Jonathan Cameron | 7c388ec | 2012-06-30 13:52:00 +0100 | [diff] [blame] | 108 | r->stufftoread = true; |
Lars-Peter Clausen | 623c334 | 2013-09-15 16:31:00 +0100 | [diff] [blame] | 109 | wake_up_interruptible_poll(&r->pollq, POLLIN | POLLRDNORM); |
Jonathan Cameron | c559afb | 2012-06-30 13:52:00 +0100 | [diff] [blame] | 110 | |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 111 | return 0; |
| 112 | } |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 113 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 114 | static int iio_read_first_n_kfifo(struct iio_buffer *r, |
Jonathan Cameron | b26a218 | 2011-05-18 14:41:02 +0100 | [diff] [blame] | 115 | size_t n, char __user *buf) |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 116 | { |
| 117 | int ret, copied; |
| 118 | struct iio_kfifo *kf = iio_to_kfifo(r); |
| 119 | |
Lars-Peter Clausen | 0894d80d | 2013-10-15 09:30:00 +0100 | [diff] [blame^] | 120 | if (mutex_lock_interruptible(&kf->user_lock)) |
| 121 | return -ERESTARTSYS; |
Lars-Peter Clausen | 8fe6495 | 2011-12-12 09:33:14 +0100 | [diff] [blame] | 122 | |
Lars-Peter Clausen | 0894d80d | 2013-10-15 09:30:00 +0100 | [diff] [blame^] | 123 | if (!kfifo_initialized(&kf->kf) || n < kfifo_esize(&kf->kf)) |
| 124 | ret = -EINVAL; |
| 125 | else |
| 126 | ret = kfifo_to_user(&kf->kf, buf, n, &copied); |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 127 | |
Jonathan Cameron | 7c388ec | 2012-06-30 13:52:00 +0100 | [diff] [blame] | 128 | if (kfifo_is_empty(&kf->kf)) |
| 129 | r->stufftoread = false; |
| 130 | /* verify it is still empty to avoid race */ |
| 131 | if (!kfifo_is_empty(&kf->kf)) |
| 132 | r->stufftoread = true; |
| 133 | |
Lars-Peter Clausen | 0894d80d | 2013-10-15 09:30:00 +0100 | [diff] [blame^] | 134 | mutex_unlock(&kf->user_lock); |
| 135 | if (ret < 0) |
| 136 | return ret; |
| 137 | |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 138 | return copied; |
| 139 | } |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 140 | |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 141 | static void iio_kfifo_buffer_release(struct iio_buffer *buffer) |
| 142 | { |
Lars-Peter Clausen | f6c23f4 | 2013-10-15 09:30:00 +0100 | [diff] [blame] | 143 | struct iio_kfifo *kf = iio_to_kfifo(buffer); |
| 144 | |
Lars-Peter Clausen | 0894d80d | 2013-10-15 09:30:00 +0100 | [diff] [blame^] | 145 | mutex_destroy(&kf->user_lock); |
Lars-Peter Clausen | f6c23f4 | 2013-10-15 09:30:00 +0100 | [diff] [blame] | 146 | kfifo_free(&kf->kf); |
| 147 | kfree(kf); |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Lars-Peter Clausen | 7e63234 | 2012-01-03 11:02:51 +0100 | [diff] [blame] | 150 | static const struct iio_buffer_access_funcs kfifo_access_funcs = { |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 151 | .store_to = &iio_store_to_kfifo, |
| 152 | .read_first_n = &iio_read_first_n_kfifo, |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 153 | .request_update = &iio_request_update_kfifo, |
| 154 | .get_bytes_per_datum = &iio_get_bytes_per_datum_kfifo, |
| 155 | .set_bytes_per_datum = &iio_set_bytes_per_datum_kfifo, |
| 156 | .get_length = &iio_get_length_kfifo, |
| 157 | .set_length = &iio_set_length_kfifo, |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 158 | .release = &iio_kfifo_buffer_release, |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 159 | }; |
Lars-Peter Clausen | 7e63234 | 2012-01-03 11:02:51 +0100 | [diff] [blame] | 160 | |
| 161 | struct iio_buffer *iio_kfifo_allocate(struct iio_dev *indio_dev) |
| 162 | { |
| 163 | struct iio_kfifo *kf; |
| 164 | |
| 165 | kf = kzalloc(sizeof *kf, GFP_KERNEL); |
| 166 | if (!kf) |
| 167 | return NULL; |
| 168 | kf->update_needed = true; |
| 169 | iio_buffer_init(&kf->buffer); |
| 170 | kf->buffer.attrs = &iio_kfifo_attribute_group; |
| 171 | kf->buffer.access = &kfifo_access_funcs; |
Jonathan Cameron | 7c388ec | 2012-06-30 13:52:00 +0100 | [diff] [blame] | 172 | kf->buffer.length = 2; |
Lars-Peter Clausen | 0894d80d | 2013-10-15 09:30:00 +0100 | [diff] [blame^] | 173 | mutex_init(&kf->user_lock); |
Lars-Peter Clausen | 7e63234 | 2012-01-03 11:02:51 +0100 | [diff] [blame] | 174 | return &kf->buffer; |
| 175 | } |
| 176 | EXPORT_SYMBOL(iio_kfifo_allocate); |
| 177 | |
| 178 | void iio_kfifo_free(struct iio_buffer *r) |
| 179 | { |
Lars-Peter Clausen | 9e69c93 | 2013-10-04 12:06:00 +0100 | [diff] [blame] | 180 | iio_buffer_put(r); |
Lars-Peter Clausen | 7e63234 | 2012-01-03 11:02:51 +0100 | [diff] [blame] | 181 | } |
| 182 | EXPORT_SYMBOL(iio_kfifo_free); |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 183 | |
Jonathan Cameron | b174baf | 2011-02-11 13:09:10 +0000 | [diff] [blame] | 184 | MODULE_LICENSE("GPL"); |