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