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