Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 1 | /* The industrial I/O simple minimally locked ring buffer. |
| 2 | * |
| 3 | * Copyright (c) 2008 Jonathan Cameron |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published by |
| 7 | * the Free Software Foundation. |
| 8 | */ |
| 9 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 11 | #include <linux/kernel.h> |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/workqueue.h> |
Jonathan Cameron | a734834 | 2011-05-18 14:40:55 +0100 | [diff] [blame] | 15 | #include <linux/sched.h> |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 16 | #include <linux/poll.h> |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 17 | #include "ring_sw.h" |
Jonathan Cameron | 59883ba | 2010-07-11 16:39:18 +0100 | [diff] [blame] | 18 | #include "trigger.h" |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 19 | |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 20 | /** |
| 21 | * struct iio_sw_ring_buffer - software ring buffer |
| 22 | * @buf: generic ring buffer elements |
| 23 | * @data: the ring buffer memory |
| 24 | * @read_p: read pointer (oldest available) |
| 25 | * @write_p: write pointer |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 26 | * @half_p: half buffer length behind write_p (event generation) |
| 27 | * @use_count: reference count to prevent resizing when in use |
| 28 | * @update_needed: flag to indicated change in size requested |
| 29 | * @use_lock: lock to prevent change in size when in use |
| 30 | * |
| 31 | * Note that the first element of all ring buffers must be a |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 32 | * struct iio_buffer. |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 33 | **/ |
| 34 | struct iio_sw_ring_buffer { |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 35 | struct iio_buffer buf; |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 36 | unsigned char *data; |
| 37 | unsigned char *read_p; |
| 38 | unsigned char *write_p; |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 39 | /* used to act as a point at which to signal an event */ |
| 40 | unsigned char *half_p; |
| 41 | int use_count; |
| 42 | int update_needed; |
| 43 | spinlock_t use_lock; |
| 44 | }; |
| 45 | |
| 46 | #define iio_to_sw_ring(r) container_of(r, struct iio_sw_ring_buffer, buf) |
| 47 | |
Jonathan Cameron | 6f2dfb3 | 2010-03-02 13:35:35 +0000 | [diff] [blame] | 48 | static inline int __iio_allocate_sw_ring_buffer(struct iio_sw_ring_buffer *ring, |
| 49 | int bytes_per_datum, int length) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 50 | { |
| 51 | if ((length == 0) || (bytes_per_datum == 0)) |
| 52 | return -EINVAL; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 53 | __iio_update_buffer(&ring->buf, bytes_per_datum, length); |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 54 | ring->data = kmalloc(length*ring->buf.bytes_per_datum, GFP_ATOMIC); |
Greg Kroah-Hartman | 19ca92e | 2010-05-04 22:33:27 -0700 | [diff] [blame] | 55 | ring->read_p = NULL; |
| 56 | ring->write_p = NULL; |
Greg Kroah-Hartman | 19ca92e | 2010-05-04 22:33:27 -0700 | [diff] [blame] | 57 | ring->half_p = NULL; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 58 | return ring->data ? 0 : -ENOMEM; |
| 59 | } |
| 60 | |
Jonathan Cameron | 6f2dfb3 | 2010-03-02 13:35:35 +0000 | [diff] [blame] | 61 | static inline void __iio_init_sw_ring_buffer(struct iio_sw_ring_buffer *ring) |
| 62 | { |
| 63 | spin_lock_init(&ring->use_lock); |
| 64 | } |
| 65 | |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 66 | static inline void __iio_free_sw_ring_buffer(struct iio_sw_ring_buffer *ring) |
| 67 | { |
| 68 | kfree(ring->data); |
| 69 | } |
| 70 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 71 | static void iio_mark_sw_rb_in_use(struct iio_buffer *r) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 72 | { |
| 73 | struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r); |
| 74 | spin_lock(&ring->use_lock); |
| 75 | ring->use_count++; |
| 76 | spin_unlock(&ring->use_lock); |
| 77 | } |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 78 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 79 | static void iio_unmark_sw_rb_in_use(struct iio_buffer *r) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 80 | { |
| 81 | struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r); |
| 82 | spin_lock(&ring->use_lock); |
| 83 | ring->use_count--; |
| 84 | spin_unlock(&ring->use_lock); |
| 85 | } |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 86 | |
| 87 | |
| 88 | /* Ring buffer related functionality */ |
| 89 | /* Store to ring is typically called in the bh of a data ready interrupt handler |
| 90 | * in the device driver */ |
| 91 | /* Lock always held if their is a chance this may be called */ |
| 92 | /* Only one of these per ring may run concurrently - enforced by drivers */ |
Greg Kroah-Hartman | 19ca92e | 2010-05-04 22:33:27 -0700 | [diff] [blame] | 93 | static int iio_store_to_sw_ring(struct iio_sw_ring_buffer *ring, |
| 94 | unsigned char *data, s64 timestamp) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 95 | { |
| 96 | int ret = 0; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 97 | unsigned char *temp_ptr, *change_test_ptr; |
| 98 | |
| 99 | /* initial store */ |
Greg Kroah-Hartman | 19ca92e | 2010-05-04 22:33:27 -0700 | [diff] [blame] | 100 | if (unlikely(ring->write_p == NULL)) { |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 101 | ring->write_p = ring->data; |
| 102 | /* Doesn't actually matter if this is out of the set |
| 103 | * as long as the read pointer is valid before this |
| 104 | * passes it - guaranteed as set later in this function. |
| 105 | */ |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 106 | ring->half_p = ring->data - ring->buf.length*ring->buf.bytes_per_datum/2; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 107 | } |
| 108 | /* Copy data to where ever the current write pointer says */ |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 109 | memcpy(ring->write_p, data, ring->buf.bytes_per_datum); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 110 | barrier(); |
| 111 | /* Update the pointer used to get most recent value. |
| 112 | * Always valid as either points to latest or second latest value. |
| 113 | * Before this runs it is null and read attempts fail with -EAGAIN. |
| 114 | */ |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 115 | barrier(); |
| 116 | /* temp_ptr used to ensure we never have an invalid pointer |
| 117 | * it may be slightly lagging, but never invalid |
| 118 | */ |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 119 | temp_ptr = ring->write_p + ring->buf.bytes_per_datum; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 120 | /* End of ring, back to the beginning */ |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 121 | if (temp_ptr == ring->data + ring->buf.length*ring->buf.bytes_per_datum) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 122 | temp_ptr = ring->data; |
| 123 | /* Update the write pointer |
| 124 | * always valid as long as this is the only function able to write. |
| 125 | * Care needed with smp systems to ensure more than one ring fill |
| 126 | * is never scheduled. |
| 127 | */ |
| 128 | ring->write_p = temp_ptr; |
| 129 | |
Greg Kroah-Hartman | 19ca92e | 2010-05-04 22:33:27 -0700 | [diff] [blame] | 130 | if (ring->read_p == NULL) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 131 | ring->read_p = ring->data; |
| 132 | /* Buffer full - move the read pointer and create / escalate |
| 133 | * ring event */ |
| 134 | /* Tricky case - if the read pointer moves before we adjust it. |
| 135 | * Handle by not pushing if it has moved - may result in occasional |
| 136 | * unnecessary buffer full events when it wasn't quite true. |
| 137 | */ |
| 138 | else if (ring->write_p == ring->read_p) { |
| 139 | change_test_ptr = ring->read_p; |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 140 | temp_ptr = change_test_ptr + ring->buf.bytes_per_datum; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 141 | if (temp_ptr |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 142 | == ring->data + ring->buf.length*ring->buf.bytes_per_datum) { |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 143 | temp_ptr = ring->data; |
| 144 | } |
| 145 | /* We are moving pointer on one because the ring is full. Any |
| 146 | * change to the read pointer will be this or greater. |
| 147 | */ |
| 148 | if (change_test_ptr == ring->read_p) |
| 149 | ring->read_p = temp_ptr; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 150 | } |
| 151 | /* investigate if our event barrier has been passed */ |
| 152 | /* There are definite 'issues' with this and chances of |
| 153 | * simultaneous read */ |
| 154 | /* Also need to use loop count to ensure this only happens once */ |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 155 | ring->half_p += ring->buf.bytes_per_datum; |
| 156 | if (ring->half_p == ring->data + ring->buf.length*ring->buf.bytes_per_datum) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 157 | ring->half_p = ring->data; |
| 158 | if (ring->half_p == ring->read_p) { |
Jonathan Cameron | a734834 | 2011-05-18 14:40:55 +0100 | [diff] [blame] | 159 | ring->buf.stufftoread = true; |
| 160 | wake_up_interruptible(&ring->buf.pollq); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 161 | } |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 162 | return ret; |
| 163 | } |
| 164 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 165 | static int iio_read_first_n_sw_rb(struct iio_buffer *r, |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 166 | size_t n, char __user *buf) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 167 | { |
| 168 | struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r); |
| 169 | |
| 170 | u8 *initial_read_p, *initial_write_p, *current_read_p, *end_read_p; |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 171 | u8 *data; |
Jonathan Cameron | b26a218 | 2011-05-18 14:41:02 +0100 | [diff] [blame] | 172 | int ret, max_copied, bytes_to_rip, dead_offset; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 173 | |
| 174 | /* A userspace program has probably made an error if it tries to |
| 175 | * read something that is not a whole number of bpds. |
| 176 | * Return an error. |
| 177 | */ |
Jonathan Cameron | b428173 | 2011-04-15 18:55:55 +0100 | [diff] [blame] | 178 | if (n % ring->buf.bytes_per_datum) { |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 179 | ret = -EINVAL; |
| 180 | printk(KERN_INFO "Ring buffer read request not whole number of" |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 181 | "samples: Request bytes %zd, Current bytes per datum %d\n", |
Jonathan Cameron | b428173 | 2011-04-15 18:55:55 +0100 | [diff] [blame] | 182 | n, ring->buf.bytes_per_datum); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 183 | goto error_ret; |
| 184 | } |
| 185 | /* Limit size to whole of ring buffer */ |
Jonathan Cameron | b428173 | 2011-04-15 18:55:55 +0100 | [diff] [blame] | 186 | bytes_to_rip = min((size_t)(ring->buf.bytes_per_datum*ring->buf.length), |
| 187 | n); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 188 | |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 189 | data = kmalloc(bytes_to_rip, GFP_KERNEL); |
| 190 | if (data == NULL) { |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 191 | ret = -ENOMEM; |
| 192 | goto error_ret; |
| 193 | } |
| 194 | |
| 195 | /* build local copy */ |
| 196 | initial_read_p = ring->read_p; |
Greg Kroah-Hartman | 19ca92e | 2010-05-04 22:33:27 -0700 | [diff] [blame] | 197 | if (unlikely(initial_read_p == NULL)) { /* No data here as yet */ |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 198 | ret = 0; |
| 199 | goto error_free_data_cpy; |
| 200 | } |
| 201 | |
| 202 | initial_write_p = ring->write_p; |
| 203 | |
| 204 | /* Need a consistent pair */ |
| 205 | while ((initial_read_p != ring->read_p) |
| 206 | || (initial_write_p != ring->write_p)) { |
| 207 | initial_read_p = ring->read_p; |
| 208 | initial_write_p = ring->write_p; |
| 209 | } |
| 210 | if (initial_write_p == initial_read_p) { |
| 211 | /* No new data available.*/ |
| 212 | ret = 0; |
| 213 | goto error_free_data_cpy; |
| 214 | } |
| 215 | |
| 216 | if (initial_write_p >= initial_read_p + bytes_to_rip) { |
| 217 | /* write_p is greater than necessary, all is easy */ |
| 218 | max_copied = bytes_to_rip; |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 219 | memcpy(data, initial_read_p, max_copied); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 220 | end_read_p = initial_read_p + max_copied; |
| 221 | } else if (initial_write_p > initial_read_p) { |
| 222 | /*not enough data to cpy */ |
| 223 | max_copied = initial_write_p - initial_read_p; |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 224 | memcpy(data, initial_read_p, max_copied); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 225 | end_read_p = initial_write_p; |
| 226 | } else { |
| 227 | /* going through 'end' of ring buffer */ |
| 228 | max_copied = ring->data |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 229 | + ring->buf.length*ring->buf.bytes_per_datum - initial_read_p; |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 230 | memcpy(data, initial_read_p, max_copied); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 231 | /* possible we are done if we align precisely with end */ |
| 232 | if (max_copied == bytes_to_rip) |
| 233 | end_read_p = ring->data; |
| 234 | else if (initial_write_p |
| 235 | > ring->data + bytes_to_rip - max_copied) { |
| 236 | /* enough data to finish */ |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 237 | memcpy(data + max_copied, ring->data, |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 238 | bytes_to_rip - max_copied); |
| 239 | max_copied = bytes_to_rip; |
| 240 | end_read_p = ring->data + (bytes_to_rip - max_copied); |
| 241 | } else { /* not enough data */ |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 242 | memcpy(data + max_copied, ring->data, |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 243 | initial_write_p - ring->data); |
| 244 | max_copied += initial_write_p - ring->data; |
| 245 | end_read_p = initial_write_p; |
| 246 | } |
| 247 | } |
| 248 | /* Now to verify which section was cleanly copied - i.e. how far |
| 249 | * read pointer has been pushed */ |
| 250 | current_read_p = ring->read_p; |
| 251 | |
| 252 | if (initial_read_p <= current_read_p) |
Jonathan Cameron | b26a218 | 2011-05-18 14:41:02 +0100 | [diff] [blame] | 253 | dead_offset = current_read_p - initial_read_p; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 254 | else |
Jonathan Cameron | b26a218 | 2011-05-18 14:41:02 +0100 | [diff] [blame] | 255 | dead_offset = ring->buf.length*ring->buf.bytes_per_datum |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 256 | - (initial_read_p - current_read_p); |
| 257 | |
| 258 | /* possible issue if the initial write has been lapped or indeed |
| 259 | * the point we were reading to has been passed */ |
| 260 | /* No valid data read. |
| 261 | * In this case the read pointer is already correct having been |
| 262 | * pushed further than we would look. */ |
Jonathan Cameron | b26a218 | 2011-05-18 14:41:02 +0100 | [diff] [blame] | 263 | if (max_copied - dead_offset < 0) { |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 264 | ret = 0; |
| 265 | goto error_free_data_cpy; |
| 266 | } |
| 267 | |
| 268 | /* setup the next read position */ |
| 269 | /* Beware, this may fail due to concurrency fun and games. |
| 270 | * Possible that sufficient fill commands have run to push the read |
| 271 | * pointer past where we would be after the rip. If this occurs, leave |
| 272 | * it be. |
| 273 | */ |
| 274 | /* Tricky - deal with loops */ |
| 275 | |
| 276 | while (ring->read_p != end_read_p) |
| 277 | ring->read_p = end_read_p; |
| 278 | |
Jonathan Cameron | b26a218 | 2011-05-18 14:41:02 +0100 | [diff] [blame] | 279 | ret = max_copied - dead_offset; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 280 | |
Jonathan Cameron | b26a218 | 2011-05-18 14:41:02 +0100 | [diff] [blame] | 281 | if (copy_to_user(buf, data + dead_offset, ret)) { |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 282 | ret = -EFAULT; |
| 283 | goto error_free_data_cpy; |
| 284 | } |
Jonathan Cameron | a734834 | 2011-05-18 14:40:55 +0100 | [diff] [blame] | 285 | |
| 286 | if (bytes_to_rip >= ring->buf.length*ring->buf.bytes_per_datum/2) |
| 287 | ring->buf.stufftoread = 0; |
| 288 | |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 289 | error_free_data_cpy: |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 290 | kfree(data); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 291 | error_ret: |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 292 | |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 293 | return ret; |
| 294 | } |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 295 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 296 | static int iio_store_to_sw_rb(struct iio_buffer *r, |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 297 | u8 *data, |
| 298 | s64 timestamp) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 299 | { |
| 300 | struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r); |
| 301 | return iio_store_to_sw_ring(ring, data, timestamp); |
| 302 | } |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 303 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 304 | static int iio_request_update_sw_rb(struct iio_buffer *r) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 305 | { |
| 306 | int ret = 0; |
| 307 | struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r); |
| 308 | |
Jonathan Cameron | a734834 | 2011-05-18 14:40:55 +0100 | [diff] [blame] | 309 | r->stufftoread = false; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 310 | spin_lock(&ring->use_lock); |
| 311 | if (!ring->update_needed) |
| 312 | goto error_ret; |
| 313 | if (ring->use_count) { |
| 314 | ret = -EAGAIN; |
| 315 | goto error_ret; |
| 316 | } |
| 317 | __iio_free_sw_ring_buffer(ring); |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 318 | ret = __iio_allocate_sw_ring_buffer(ring, ring->buf.bytes_per_datum, |
Jonathan Cameron | 6f2dfb3 | 2010-03-02 13:35:35 +0000 | [diff] [blame] | 319 | ring->buf.length); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 320 | error_ret: |
| 321 | spin_unlock(&ring->use_lock); |
| 322 | return ret; |
| 323 | } |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 324 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 325 | static int iio_get_bytes_per_datum_sw_rb(struct iio_buffer *r) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 326 | { |
| 327 | struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r); |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 328 | return ring->buf.bytes_per_datum; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 329 | } |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 330 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 331 | static int iio_set_bytes_per_datum_sw_rb(struct iio_buffer *r, size_t bpd) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 332 | { |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 333 | if (r->bytes_per_datum != bpd) { |
| 334 | r->bytes_per_datum = bpd; |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 335 | if (r->access->mark_param_change) |
| 336 | r->access->mark_param_change(r); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 337 | } |
| 338 | return 0; |
| 339 | } |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 340 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 341 | static int iio_get_length_sw_rb(struct iio_buffer *r) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 342 | { |
| 343 | return r->length; |
| 344 | } |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 345 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 346 | static int iio_set_length_sw_rb(struct iio_buffer *r, int length) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 347 | { |
| 348 | if (r->length != length) { |
| 349 | r->length = length; |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 350 | if (r->access->mark_param_change) |
| 351 | r->access->mark_param_change(r); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 352 | } |
| 353 | return 0; |
| 354 | } |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 355 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 356 | static int iio_mark_update_needed_sw_rb(struct iio_buffer *r) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 357 | { |
| 358 | struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r); |
| 359 | ring->update_needed = true; |
| 360 | return 0; |
| 361 | } |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 362 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 363 | static IIO_BUFFER_ENABLE_ATTR; |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 364 | static IIO_BUFFER_LENGTH_ATTR; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 365 | |
| 366 | /* Standard set of ring buffer attributes */ |
| 367 | static struct attribute *iio_ring_attributes[] = { |
| 368 | &dev_attr_length.attr, |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 369 | &dev_attr_enable.attr, |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 370 | NULL, |
| 371 | }; |
| 372 | |
| 373 | static struct attribute_group iio_ring_attribute_group = { |
| 374 | .attrs = iio_ring_attributes, |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 375 | .name = "buffer", |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 376 | }; |
| 377 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 378 | struct iio_buffer *iio_sw_rb_allocate(struct iio_dev *indio_dev) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 379 | { |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 380 | struct iio_buffer *buf; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 381 | struct iio_sw_ring_buffer *ring; |
| 382 | |
| 383 | ring = kzalloc(sizeof *ring, GFP_KERNEL); |
| 384 | if (!ring) |
Greg Kroah-Hartman | 19ca92e | 2010-05-04 22:33:27 -0700 | [diff] [blame] | 385 | return NULL; |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 386 | ring->update_needed = true; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 387 | buf = &ring->buf; |
Jonathan Cameron | f79a909 | 2011-12-05 22:18:29 +0000 | [diff] [blame^] | 388 | iio_buffer_init(buf); |
Jonathan Cameron | 6f2dfb3 | 2010-03-02 13:35:35 +0000 | [diff] [blame] | 389 | __iio_init_sw_ring_buffer(ring); |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 390 | buf->attrs = &iio_ring_attribute_group; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 391 | |
| 392 | return buf; |
| 393 | } |
| 394 | EXPORT_SYMBOL(iio_sw_rb_allocate); |
| 395 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 396 | void iio_sw_rb_free(struct iio_buffer *r) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 397 | { |
Jonathan Cameron | 1aa0427 | 2011-08-30 12:32:47 +0100 | [diff] [blame] | 398 | kfree(iio_to_sw_ring(r)); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 399 | } |
| 400 | EXPORT_SYMBOL(iio_sw_rb_free); |
Barry Song | ad577f8 | 2010-07-11 16:39:16 +0100 | [diff] [blame] | 401 | |
Jonathan Cameron | 14555b1 | 2011-09-21 11:15:57 +0100 | [diff] [blame] | 402 | const struct iio_buffer_access_funcs ring_sw_access_funcs = { |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 403 | .mark_in_use = &iio_mark_sw_rb_in_use, |
| 404 | .unmark_in_use = &iio_unmark_sw_rb_in_use, |
| 405 | .store_to = &iio_store_to_sw_rb, |
Jonathan Cameron | 5565a45 | 2011-05-18 14:42:24 +0100 | [diff] [blame] | 406 | .read_first_n = &iio_read_first_n_sw_rb, |
| 407 | .mark_param_change = &iio_mark_update_needed_sw_rb, |
| 408 | .request_update = &iio_request_update_sw_rb, |
| 409 | .get_bytes_per_datum = &iio_get_bytes_per_datum_sw_rb, |
| 410 | .set_bytes_per_datum = &iio_set_bytes_per_datum_sw_rb, |
| 411 | .get_length = &iio_get_length_sw_rb, |
| 412 | .set_length = &iio_set_length_sw_rb, |
| 413 | }; |
| 414 | EXPORT_SYMBOL(ring_sw_access_funcs); |
| 415 | |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 416 | MODULE_DESCRIPTION("Industrialio I/O software ring buffer"); |
| 417 | MODULE_LICENSE("GPL"); |