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 | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 15 | #include <linux/poll.h> |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 16 | #include "ring_sw.h" |
Jonathan Cameron | 59883ba | 2010-07-11 16:39:18 +0100 | [diff] [blame] | 17 | #include "trigger.h" |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 18 | |
Jonathan Cameron | 6f2dfb3 | 2010-03-02 13:35:35 +0000 | [diff] [blame] | 19 | static inline int __iio_allocate_sw_ring_buffer(struct iio_sw_ring_buffer *ring, |
| 20 | int bytes_per_datum, int length) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 21 | { |
| 22 | if ((length == 0) || (bytes_per_datum == 0)) |
| 23 | return -EINVAL; |
Jonathan Cameron | 6f2dfb3 | 2010-03-02 13:35:35 +0000 | [diff] [blame] | 24 | __iio_update_ring_buffer(&ring->buf, bytes_per_datum, length); |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 25 | ring->data = kmalloc(length*ring->buf.bytes_per_datum, GFP_ATOMIC); |
Greg Kroah-Hartman | 19ca92e | 2010-05-04 22:33:27 -0700 | [diff] [blame] | 26 | ring->read_p = NULL; |
| 27 | ring->write_p = NULL; |
| 28 | ring->last_written_p = NULL; |
| 29 | ring->half_p = NULL; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 30 | return ring->data ? 0 : -ENOMEM; |
| 31 | } |
| 32 | |
Jonathan Cameron | 6f2dfb3 | 2010-03-02 13:35:35 +0000 | [diff] [blame] | 33 | static inline void __iio_init_sw_ring_buffer(struct iio_sw_ring_buffer *ring) |
| 34 | { |
| 35 | spin_lock_init(&ring->use_lock); |
| 36 | } |
| 37 | |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 38 | static inline void __iio_free_sw_ring_buffer(struct iio_sw_ring_buffer *ring) |
| 39 | { |
| 40 | kfree(ring->data); |
| 41 | } |
| 42 | |
| 43 | void iio_mark_sw_rb_in_use(struct iio_ring_buffer *r) |
| 44 | { |
| 45 | struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r); |
| 46 | spin_lock(&ring->use_lock); |
| 47 | ring->use_count++; |
| 48 | spin_unlock(&ring->use_lock); |
| 49 | } |
| 50 | EXPORT_SYMBOL(iio_mark_sw_rb_in_use); |
| 51 | |
| 52 | void iio_unmark_sw_rb_in_use(struct iio_ring_buffer *r) |
| 53 | { |
| 54 | struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r); |
| 55 | spin_lock(&ring->use_lock); |
| 56 | ring->use_count--; |
| 57 | spin_unlock(&ring->use_lock); |
| 58 | } |
| 59 | EXPORT_SYMBOL(iio_unmark_sw_rb_in_use); |
| 60 | |
| 61 | |
| 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; |
| 71 | int code; |
| 72 | unsigned char *temp_ptr, *change_test_ptr; |
| 73 | |
| 74 | /* initial store */ |
Greg Kroah-Hartman | 19ca92e | 2010-05-04 22:33:27 -0700 | [diff] [blame] | 75 | if (unlikely(ring->write_p == NULL)) { |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 76 | ring->write_p = ring->data; |
| 77 | /* Doesn't actually matter if this is out of the set |
| 78 | * as long as the read pointer is valid before this |
| 79 | * passes it - guaranteed as set later in this function. |
| 80 | */ |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 81 | 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] | 82 | } |
| 83 | /* Copy data to where ever the current write pointer says */ |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 84 | memcpy(ring->write_p, data, ring->buf.bytes_per_datum); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 85 | barrier(); |
| 86 | /* Update the pointer used to get most recent value. |
| 87 | * Always valid as either points to latest or second latest value. |
| 88 | * Before this runs it is null and read attempts fail with -EAGAIN. |
| 89 | */ |
| 90 | ring->last_written_p = ring->write_p; |
| 91 | barrier(); |
| 92 | /* temp_ptr used to ensure we never have an invalid pointer |
| 93 | * it may be slightly lagging, but never invalid |
| 94 | */ |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 95 | temp_ptr = ring->write_p + ring->buf.bytes_per_datum; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 96 | /* End of ring, back to the beginning */ |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 97 | 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] | 98 | temp_ptr = ring->data; |
| 99 | /* Update the write pointer |
| 100 | * always valid as long as this is the only function able to write. |
| 101 | * Care needed with smp systems to ensure more than one ring fill |
| 102 | * is never scheduled. |
| 103 | */ |
| 104 | ring->write_p = temp_ptr; |
| 105 | |
Greg Kroah-Hartman | 19ca92e | 2010-05-04 22:33:27 -0700 | [diff] [blame] | 106 | if (ring->read_p == NULL) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 107 | ring->read_p = ring->data; |
| 108 | /* Buffer full - move the read pointer and create / escalate |
| 109 | * ring event */ |
| 110 | /* Tricky case - if the read pointer moves before we adjust it. |
| 111 | * Handle by not pushing if it has moved - may result in occasional |
| 112 | * unnecessary buffer full events when it wasn't quite true. |
| 113 | */ |
| 114 | else if (ring->write_p == ring->read_p) { |
| 115 | change_test_ptr = ring->read_p; |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 116 | temp_ptr = change_test_ptr + ring->buf.bytes_per_datum; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 117 | if (temp_ptr |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 118 | == ring->data + ring->buf.length*ring->buf.bytes_per_datum) { |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 119 | temp_ptr = ring->data; |
| 120 | } |
| 121 | /* We are moving pointer on one because the ring is full. Any |
| 122 | * change to the read pointer will be this or greater. |
| 123 | */ |
| 124 | if (change_test_ptr == ring->read_p) |
| 125 | ring->read_p = temp_ptr; |
| 126 | |
| 127 | spin_lock(&ring->buf.shared_ev_pointer.lock); |
| 128 | |
| 129 | ret = iio_push_or_escallate_ring_event(&ring->buf, |
Roel Van Nyen | c849d25 | 2010-04-29 19:27:31 +0200 | [diff] [blame] | 130 | IIO_EVENT_CODE_RING_100_FULL, timestamp); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 131 | spin_unlock(&ring->buf.shared_ev_pointer.lock); |
| 132 | if (ret) |
| 133 | goto error_ret; |
| 134 | } |
| 135 | /* investigate if our event barrier has been passed */ |
| 136 | /* There are definite 'issues' with this and chances of |
| 137 | * simultaneous read */ |
| 138 | /* Also need to use loop count to ensure this only happens once */ |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 139 | ring->half_p += ring->buf.bytes_per_datum; |
| 140 | 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] | 141 | ring->half_p = ring->data; |
| 142 | if (ring->half_p == ring->read_p) { |
| 143 | spin_lock(&ring->buf.shared_ev_pointer.lock); |
| 144 | code = IIO_EVENT_CODE_RING_50_FULL; |
| 145 | ret = __iio_push_event(&ring->buf.ev_int, |
| 146 | code, |
| 147 | timestamp, |
| 148 | &ring->buf.shared_ev_pointer); |
| 149 | spin_unlock(&ring->buf.shared_ev_pointer.lock); |
| 150 | } |
| 151 | error_ret: |
| 152 | return ret; |
| 153 | } |
| 154 | |
Jonathan Cameron | b428173 | 2011-04-15 18:55:55 +0100 | [diff] [blame^] | 155 | int iio_read_first_n_sw_rb(struct iio_ring_buffer *r, |
| 156 | size_t n, char __user *buf, int *dead_offset) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 157 | { |
| 158 | struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r); |
| 159 | |
| 160 | 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] | 161 | u8 *data; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 162 | int ret, max_copied; |
| 163 | int bytes_to_rip; |
| 164 | |
| 165 | /* A userspace program has probably made an error if it tries to |
| 166 | * read something that is not a whole number of bpds. |
| 167 | * Return an error. |
| 168 | */ |
Jonathan Cameron | b428173 | 2011-04-15 18:55:55 +0100 | [diff] [blame^] | 169 | if (n % ring->buf.bytes_per_datum) { |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 170 | ret = -EINVAL; |
| 171 | printk(KERN_INFO "Ring buffer read request not whole number of" |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 172 | "samples: Request bytes %zd, Current bytes per datum %d\n", |
Jonathan Cameron | b428173 | 2011-04-15 18:55:55 +0100 | [diff] [blame^] | 173 | n, ring->buf.bytes_per_datum); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 174 | goto error_ret; |
| 175 | } |
| 176 | /* Limit size to whole of ring buffer */ |
Jonathan Cameron | b428173 | 2011-04-15 18:55:55 +0100 | [diff] [blame^] | 177 | bytes_to_rip = min((size_t)(ring->buf.bytes_per_datum*ring->buf.length), |
| 178 | n); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 179 | |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 180 | data = kmalloc(bytes_to_rip, GFP_KERNEL); |
| 181 | if (data == NULL) { |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 182 | ret = -ENOMEM; |
| 183 | goto error_ret; |
| 184 | } |
| 185 | |
| 186 | /* build local copy */ |
| 187 | initial_read_p = ring->read_p; |
Greg Kroah-Hartman | 19ca92e | 2010-05-04 22:33:27 -0700 | [diff] [blame] | 188 | if (unlikely(initial_read_p == NULL)) { /* No data here as yet */ |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 189 | ret = 0; |
| 190 | goto error_free_data_cpy; |
| 191 | } |
| 192 | |
| 193 | initial_write_p = ring->write_p; |
| 194 | |
| 195 | /* Need a consistent pair */ |
| 196 | while ((initial_read_p != ring->read_p) |
| 197 | || (initial_write_p != ring->write_p)) { |
| 198 | initial_read_p = ring->read_p; |
| 199 | initial_write_p = ring->write_p; |
| 200 | } |
| 201 | if (initial_write_p == initial_read_p) { |
| 202 | /* No new data available.*/ |
| 203 | ret = 0; |
| 204 | goto error_free_data_cpy; |
| 205 | } |
| 206 | |
| 207 | if (initial_write_p >= initial_read_p + bytes_to_rip) { |
| 208 | /* write_p is greater than necessary, all is easy */ |
| 209 | max_copied = bytes_to_rip; |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 210 | memcpy(data, initial_read_p, max_copied); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 211 | end_read_p = initial_read_p + max_copied; |
| 212 | } else if (initial_write_p > initial_read_p) { |
| 213 | /*not enough data to cpy */ |
| 214 | max_copied = initial_write_p - initial_read_p; |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 215 | memcpy(data, initial_read_p, max_copied); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 216 | end_read_p = initial_write_p; |
| 217 | } else { |
| 218 | /* going through 'end' of ring buffer */ |
| 219 | max_copied = ring->data |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 220 | + ring->buf.length*ring->buf.bytes_per_datum - initial_read_p; |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 221 | memcpy(data, initial_read_p, max_copied); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 222 | /* possible we are done if we align precisely with end */ |
| 223 | if (max_copied == bytes_to_rip) |
| 224 | end_read_p = ring->data; |
| 225 | else if (initial_write_p |
| 226 | > ring->data + bytes_to_rip - max_copied) { |
| 227 | /* enough data to finish */ |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 228 | memcpy(data + max_copied, ring->data, |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 229 | bytes_to_rip - max_copied); |
| 230 | max_copied = bytes_to_rip; |
| 231 | end_read_p = ring->data + (bytes_to_rip - max_copied); |
| 232 | } else { /* not enough data */ |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 233 | memcpy(data + max_copied, ring->data, |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 234 | initial_write_p - ring->data); |
| 235 | max_copied += initial_write_p - ring->data; |
| 236 | end_read_p = initial_write_p; |
| 237 | } |
| 238 | } |
| 239 | /* Now to verify which section was cleanly copied - i.e. how far |
| 240 | * read pointer has been pushed */ |
| 241 | current_read_p = ring->read_p; |
| 242 | |
| 243 | if (initial_read_p <= current_read_p) |
| 244 | *dead_offset = current_read_p - initial_read_p; |
| 245 | else |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 246 | *dead_offset = ring->buf.length*ring->buf.bytes_per_datum |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 247 | - (initial_read_p - current_read_p); |
| 248 | |
| 249 | /* possible issue if the initial write has been lapped or indeed |
| 250 | * the point we were reading to has been passed */ |
| 251 | /* No valid data read. |
| 252 | * In this case the read pointer is already correct having been |
| 253 | * pushed further than we would look. */ |
| 254 | if (max_copied - *dead_offset < 0) { |
| 255 | ret = 0; |
| 256 | goto error_free_data_cpy; |
| 257 | } |
| 258 | |
| 259 | /* setup the next read position */ |
| 260 | /* Beware, this may fail due to concurrency fun and games. |
| 261 | * Possible that sufficient fill commands have run to push the read |
| 262 | * pointer past where we would be after the rip. If this occurs, leave |
| 263 | * it be. |
| 264 | */ |
| 265 | /* Tricky - deal with loops */ |
| 266 | |
| 267 | while (ring->read_p != end_read_p) |
| 268 | ring->read_p = end_read_p; |
| 269 | |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 270 | ret = max_copied - *dead_offset; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 271 | |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 272 | if (copy_to_user(buf, data + *dead_offset, ret)) { |
| 273 | ret = -EFAULT; |
| 274 | goto error_free_data_cpy; |
| 275 | } |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 276 | error_free_data_cpy: |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 277 | kfree(data); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 278 | error_ret: |
Jonathan Cameron | d5857d6 | 2011-02-11 13:09:09 +0000 | [diff] [blame] | 279 | |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 280 | return ret; |
| 281 | } |
Jonathan Cameron | b428173 | 2011-04-15 18:55:55 +0100 | [diff] [blame^] | 282 | EXPORT_SYMBOL(iio_read_first_n_sw_rb); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 283 | |
| 284 | int iio_store_to_sw_rb(struct iio_ring_buffer *r, u8 *data, s64 timestamp) |
| 285 | { |
| 286 | struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r); |
| 287 | return iio_store_to_sw_ring(ring, data, timestamp); |
| 288 | } |
| 289 | EXPORT_SYMBOL(iio_store_to_sw_rb); |
| 290 | |
Greg Kroah-Hartman | 19ca92e | 2010-05-04 22:33:27 -0700 | [diff] [blame] | 291 | static int iio_read_last_from_sw_ring(struct iio_sw_ring_buffer *ring, |
| 292 | unsigned char *data) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 293 | { |
| 294 | unsigned char *last_written_p_copy; |
| 295 | |
| 296 | iio_mark_sw_rb_in_use(&ring->buf); |
| 297 | again: |
| 298 | barrier(); |
| 299 | last_written_p_copy = ring->last_written_p; |
| 300 | barrier(); /*unnessecary? */ |
| 301 | /* Check there is anything here */ |
Greg Kroah-Hartman | 19ca92e | 2010-05-04 22:33:27 -0700 | [diff] [blame] | 302 | if (last_written_p_copy == NULL) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 303 | return -EAGAIN; |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 304 | memcpy(data, last_written_p_copy, ring->buf.bytes_per_datum); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 305 | |
Jonathan Cameron | 8474ddd | 2010-05-04 14:43:11 +0100 | [diff] [blame] | 306 | if (unlikely(ring->last_written_p != last_written_p_copy)) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 307 | goto again; |
| 308 | |
| 309 | iio_unmark_sw_rb_in_use(&ring->buf); |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | int iio_read_last_from_sw_rb(struct iio_ring_buffer *r, |
| 314 | unsigned char *data) |
| 315 | { |
| 316 | return iio_read_last_from_sw_ring(iio_to_sw_ring(r), data); |
| 317 | } |
| 318 | EXPORT_SYMBOL(iio_read_last_from_sw_rb); |
| 319 | |
| 320 | int iio_request_update_sw_rb(struct iio_ring_buffer *r) |
| 321 | { |
| 322 | int ret = 0; |
| 323 | struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r); |
| 324 | |
| 325 | spin_lock(&ring->use_lock); |
| 326 | if (!ring->update_needed) |
| 327 | goto error_ret; |
| 328 | if (ring->use_count) { |
| 329 | ret = -EAGAIN; |
| 330 | goto error_ret; |
| 331 | } |
| 332 | __iio_free_sw_ring_buffer(ring); |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 333 | ret = __iio_allocate_sw_ring_buffer(ring, ring->buf.bytes_per_datum, |
Jonathan Cameron | 6f2dfb3 | 2010-03-02 13:35:35 +0000 | [diff] [blame] | 334 | ring->buf.length); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 335 | error_ret: |
| 336 | spin_unlock(&ring->use_lock); |
| 337 | return ret; |
| 338 | } |
| 339 | EXPORT_SYMBOL(iio_request_update_sw_rb); |
| 340 | |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 341 | int iio_get_bytes_per_datum_sw_rb(struct iio_ring_buffer *r) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 342 | { |
| 343 | struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r); |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 344 | return ring->buf.bytes_per_datum; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 345 | } |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 346 | EXPORT_SYMBOL(iio_get_bytes_per_datum_sw_rb); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 347 | |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 348 | int iio_set_bytes_per_datum_sw_rb(struct iio_ring_buffer *r, size_t bpd) |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 349 | { |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 350 | if (r->bytes_per_datum != bpd) { |
| 351 | r->bytes_per_datum = bpd; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 352 | if (r->access.mark_param_change) |
| 353 | r->access.mark_param_change(r); |
| 354 | } |
| 355 | return 0; |
| 356 | } |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 357 | EXPORT_SYMBOL(iio_set_bytes_per_datum_sw_rb); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 358 | |
| 359 | int iio_get_length_sw_rb(struct iio_ring_buffer *r) |
| 360 | { |
| 361 | return r->length; |
| 362 | } |
| 363 | EXPORT_SYMBOL(iio_get_length_sw_rb); |
| 364 | |
| 365 | int iio_set_length_sw_rb(struct iio_ring_buffer *r, int length) |
| 366 | { |
| 367 | if (r->length != length) { |
| 368 | r->length = length; |
| 369 | if (r->access.mark_param_change) |
| 370 | r->access.mark_param_change(r); |
| 371 | } |
| 372 | return 0; |
| 373 | } |
| 374 | EXPORT_SYMBOL(iio_set_length_sw_rb); |
| 375 | |
| 376 | int iio_mark_update_needed_sw_rb(struct iio_ring_buffer *r) |
| 377 | { |
| 378 | struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r); |
| 379 | ring->update_needed = true; |
| 380 | return 0; |
| 381 | } |
| 382 | EXPORT_SYMBOL(iio_mark_update_needed_sw_rb); |
| 383 | |
| 384 | static void iio_sw_rb_release(struct device *dev) |
| 385 | { |
| 386 | struct iio_ring_buffer *r = to_iio_ring_buffer(dev); |
| 387 | kfree(iio_to_sw_ring(r)); |
| 388 | } |
| 389 | |
| 390 | static IIO_RING_ENABLE_ATTR; |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 391 | static IIO_RING_BYTES_PER_DATUM_ATTR; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 392 | static IIO_RING_LENGTH_ATTR; |
| 393 | |
| 394 | /* Standard set of ring buffer attributes */ |
| 395 | static struct attribute *iio_ring_attributes[] = { |
| 396 | &dev_attr_length.attr, |
Manuel Stahl | ffcab07 | 2010-08-31 11:32:50 +0200 | [diff] [blame] | 397 | &dev_attr_bytes_per_datum.attr, |
| 398 | &dev_attr_enable.attr, |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 399 | NULL, |
| 400 | }; |
| 401 | |
| 402 | static struct attribute_group iio_ring_attribute_group = { |
| 403 | .attrs = iio_ring_attributes, |
| 404 | }; |
| 405 | |
Alan Cox | 3860dc8 | 2009-08-19 16:59:31 +0100 | [diff] [blame] | 406 | static const struct attribute_group *iio_ring_attribute_groups[] = { |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 407 | &iio_ring_attribute_group, |
| 408 | NULL |
| 409 | }; |
| 410 | |
| 411 | static struct device_type iio_sw_ring_type = { |
| 412 | .release = iio_sw_rb_release, |
| 413 | .groups = iio_ring_attribute_groups, |
| 414 | }; |
| 415 | |
| 416 | struct iio_ring_buffer *iio_sw_rb_allocate(struct iio_dev *indio_dev) |
| 417 | { |
| 418 | struct iio_ring_buffer *buf; |
| 419 | struct iio_sw_ring_buffer *ring; |
| 420 | |
| 421 | ring = kzalloc(sizeof *ring, GFP_KERNEL); |
| 422 | if (!ring) |
Greg Kroah-Hartman | 19ca92e | 2010-05-04 22:33:27 -0700 | [diff] [blame] | 423 | return NULL; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 424 | buf = &ring->buf; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 425 | iio_ring_buffer_init(buf, indio_dev); |
Jonathan Cameron | 6f2dfb3 | 2010-03-02 13:35:35 +0000 | [diff] [blame] | 426 | __iio_init_sw_ring_buffer(ring); |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 427 | buf->dev.type = &iio_sw_ring_type; |
| 428 | device_initialize(&buf->dev); |
| 429 | buf->dev.parent = &indio_dev->dev; |
Jonathan Cameron | 5aaaeba | 2010-05-04 14:43:00 +0100 | [diff] [blame] | 430 | buf->dev.bus = &iio_bus_type; |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 431 | dev_set_drvdata(&buf->dev, (void *)buf); |
| 432 | |
| 433 | return buf; |
| 434 | } |
| 435 | EXPORT_SYMBOL(iio_sw_rb_allocate); |
| 436 | |
| 437 | void iio_sw_rb_free(struct iio_ring_buffer *r) |
| 438 | { |
| 439 | if (r) |
| 440 | iio_put_ring_buffer(r); |
| 441 | } |
| 442 | EXPORT_SYMBOL(iio_sw_rb_free); |
Barry Song | ad577f8 | 2010-07-11 16:39:16 +0100 | [diff] [blame] | 443 | |
| 444 | int iio_sw_ring_preenable(struct iio_dev *indio_dev) |
| 445 | { |
Manuel Stahl | bf32963 | 2010-08-31 11:32:52 +0200 | [diff] [blame] | 446 | struct iio_ring_buffer *ring = indio_dev->ring; |
Barry Song | ad577f8 | 2010-07-11 16:39:16 +0100 | [diff] [blame] | 447 | size_t size; |
| 448 | dev_dbg(&indio_dev->dev, "%s\n", __func__); |
| 449 | /* Check if there are any scan elements enabled, if not fail*/ |
Manuel Stahl | bf32963 | 2010-08-31 11:32:52 +0200 | [diff] [blame] | 450 | if (!(ring->scan_count || ring->scan_timestamp)) |
Barry Song | ad577f8 | 2010-07-11 16:39:16 +0100 | [diff] [blame] | 451 | return -EINVAL; |
Manuel Stahl | bf32963 | 2010-08-31 11:32:52 +0200 | [diff] [blame] | 452 | if (ring->scan_timestamp) |
| 453 | if (ring->scan_count) |
Barry Song | ad577f8 | 2010-07-11 16:39:16 +0100 | [diff] [blame] | 454 | /* Timestamp (aligned to s64) and data */ |
Manuel Stahl | bf32963 | 2010-08-31 11:32:52 +0200 | [diff] [blame] | 455 | size = (((ring->scan_count * ring->bpe) |
Barry Song | ad577f8 | 2010-07-11 16:39:16 +0100 | [diff] [blame] | 456 | + sizeof(s64) - 1) |
| 457 | & ~(sizeof(s64) - 1)) |
| 458 | + sizeof(s64); |
| 459 | else /* Timestamp only */ |
| 460 | size = sizeof(s64); |
| 461 | else /* Data only */ |
Manuel Stahl | bf32963 | 2010-08-31 11:32:52 +0200 | [diff] [blame] | 462 | size = ring->scan_count * ring->bpe; |
| 463 | ring->access.set_bytes_per_datum(ring, size); |
Barry Song | ad577f8 | 2010-07-11 16:39:16 +0100 | [diff] [blame] | 464 | |
| 465 | return 0; |
| 466 | } |
| 467 | EXPORT_SYMBOL(iio_sw_ring_preenable); |
| 468 | |
Jonathan Cameron | 59883ba | 2010-07-11 16:39:18 +0100 | [diff] [blame] | 469 | void iio_sw_trigger_bh_to_ring(struct work_struct *work_s) |
| 470 | { |
| 471 | struct iio_sw_ring_helper_state *st |
| 472 | = container_of(work_s, struct iio_sw_ring_helper_state, |
| 473 | work_trigger_to_ring); |
Manuel Stahl | bf32963 | 2010-08-31 11:32:52 +0200 | [diff] [blame] | 474 | struct iio_ring_buffer *ring = st->indio_dev->ring; |
Jonathan Cameron | 59883ba | 2010-07-11 16:39:18 +0100 | [diff] [blame] | 475 | int len = 0; |
Manuel Stahl | bf32963 | 2010-08-31 11:32:52 +0200 | [diff] [blame] | 476 | size_t datasize = ring->access.get_bytes_per_datum(ring); |
Jonathan Cameron | 59883ba | 2010-07-11 16:39:18 +0100 | [diff] [blame] | 477 | char *data = kmalloc(datasize, GFP_KERNEL); |
| 478 | |
| 479 | if (data == NULL) { |
| 480 | dev_err(st->indio_dev->dev.parent, |
| 481 | "memory alloc failed in ring bh"); |
| 482 | return; |
| 483 | } |
| 484 | |
Manuel Stahl | bf32963 | 2010-08-31 11:32:52 +0200 | [diff] [blame] | 485 | if (ring->scan_count) |
Jonathan Cameron | 59883ba | 2010-07-11 16:39:18 +0100 | [diff] [blame] | 486 | len = st->get_ring_element(st, data); |
| 487 | |
| 488 | /* Guaranteed to be aligned with 8 byte boundary */ |
Manuel Stahl | bf32963 | 2010-08-31 11:32:52 +0200 | [diff] [blame] | 489 | if (ring->scan_timestamp) |
Jonathan Cameron | 019415c | 2010-07-29 17:50:51 +0100 | [diff] [blame] | 490 | *(s64 *)(((phys_addr_t)data + len |
Jonathan Cameron | 59883ba | 2010-07-11 16:39:18 +0100 | [diff] [blame] | 491 | + sizeof(s64) - 1) & ~(sizeof(s64) - 1)) |
| 492 | = st->last_timestamp; |
Manuel Stahl | bf32963 | 2010-08-31 11:32:52 +0200 | [diff] [blame] | 493 | ring->access.store_to(ring, |
| 494 | (u8 *)data, |
Jonathan Cameron | 59883ba | 2010-07-11 16:39:18 +0100 | [diff] [blame] | 495 | st->last_timestamp); |
| 496 | |
| 497 | iio_trigger_notify_done(st->indio_dev->trig); |
| 498 | kfree(data); |
| 499 | |
| 500 | return; |
| 501 | } |
| 502 | EXPORT_SYMBOL(iio_sw_trigger_bh_to_ring); |
| 503 | |
| 504 | void iio_sw_poll_func_th(struct iio_dev *indio_dev, s64 time) |
| 505 | { struct iio_sw_ring_helper_state *h |
| 506 | = iio_dev_get_devdata(indio_dev); |
| 507 | h->last_timestamp = time; |
| 508 | schedule_work(&h->work_trigger_to_ring); |
| 509 | } |
| 510 | EXPORT_SYMBOL(iio_sw_poll_func_th); |
| 511 | |
Jonathan Cameron | 2235acb | 2009-08-18 18:06:27 +0100 | [diff] [blame] | 512 | MODULE_DESCRIPTION("Industrialio I/O software ring buffer"); |
| 513 | MODULE_LICENSE("GPL"); |