blob: 1905e033c41d76f9b0062bf07a0a6e5dd57f5aa1 [file] [log] [blame]
Jonathan Cameron2235acb2009-08-18 18:06:27 +01001/* 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 Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Jonathan Cameron2235acb2009-08-18 18:06:27 +010011#include <linux/kernel.h>
Jonathan Cameron2235acb2009-08-18 18:06:27 +010012#include <linux/module.h>
13#include <linux/device.h>
14#include <linux/workqueue.h>
Jonathan Camerona7348342011-05-18 14:40:55 +010015#include <linux/sched.h>
Jonathan Camerond5857d62011-02-11 13:09:09 +000016#include <linux/poll.h>
Jonathan Cameron2235acb2009-08-18 18:06:27 +010017#include "ring_sw.h"
Jonathan Cameron59883ba2010-07-11 16:39:18 +010018#include "trigger.h"
Jonathan Cameron2235acb2009-08-18 18:06:27 +010019
Jonathan Cameron5565a452011-05-18 14:42:24 +010020/**
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
26 * @last_written_p: read pointer (newest available)
27 * @half_p: half buffer length behind write_p (event generation)
28 * @use_count: reference count to prevent resizing when in use
29 * @update_needed: flag to indicated change in size requested
30 * @use_lock: lock to prevent change in size when in use
31 *
32 * Note that the first element of all ring buffers must be a
33 * struct iio_ring_buffer.
34**/
35struct iio_sw_ring_buffer {
36 struct iio_ring_buffer buf;
37 unsigned char *data;
38 unsigned char *read_p;
39 unsigned char *write_p;
40 unsigned char *last_written_p;
41 /* used to act as a point at which to signal an event */
42 unsigned char *half_p;
43 int use_count;
44 int update_needed;
45 spinlock_t use_lock;
46};
47
48#define iio_to_sw_ring(r) container_of(r, struct iio_sw_ring_buffer, buf)
49
Jonathan Cameron6f2dfb32010-03-02 13:35:35 +000050static inline int __iio_allocate_sw_ring_buffer(struct iio_sw_ring_buffer *ring,
51 int bytes_per_datum, int length)
Jonathan Cameron2235acb2009-08-18 18:06:27 +010052{
53 if ((length == 0) || (bytes_per_datum == 0))
54 return -EINVAL;
Jonathan Cameron6f2dfb32010-03-02 13:35:35 +000055 __iio_update_ring_buffer(&ring->buf, bytes_per_datum, length);
Manuel Stahlffcab072010-08-31 11:32:50 +020056 ring->data = kmalloc(length*ring->buf.bytes_per_datum, GFP_ATOMIC);
Greg Kroah-Hartman19ca92e2010-05-04 22:33:27 -070057 ring->read_p = NULL;
58 ring->write_p = NULL;
59 ring->last_written_p = NULL;
60 ring->half_p = NULL;
Jonathan Cameron2235acb2009-08-18 18:06:27 +010061 return ring->data ? 0 : -ENOMEM;
62}
63
Jonathan Cameron6f2dfb32010-03-02 13:35:35 +000064static inline void __iio_init_sw_ring_buffer(struct iio_sw_ring_buffer *ring)
65{
66 spin_lock_init(&ring->use_lock);
67}
68
Jonathan Cameron2235acb2009-08-18 18:06:27 +010069static inline void __iio_free_sw_ring_buffer(struct iio_sw_ring_buffer *ring)
70{
71 kfree(ring->data);
72}
73
Jonathan Cameron5565a452011-05-18 14:42:24 +010074static void iio_mark_sw_rb_in_use(struct iio_ring_buffer *r)
Jonathan Cameron2235acb2009-08-18 18:06:27 +010075{
76 struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r);
77 spin_lock(&ring->use_lock);
78 ring->use_count++;
79 spin_unlock(&ring->use_lock);
80}
Jonathan Cameron2235acb2009-08-18 18:06:27 +010081
Jonathan Cameron5565a452011-05-18 14:42:24 +010082static void iio_unmark_sw_rb_in_use(struct iio_ring_buffer *r)
Jonathan Cameron2235acb2009-08-18 18:06:27 +010083{
84 struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r);
85 spin_lock(&ring->use_lock);
86 ring->use_count--;
87 spin_unlock(&ring->use_lock);
88}
Jonathan Cameron2235acb2009-08-18 18:06:27 +010089
90
91/* Ring buffer related functionality */
92/* Store to ring is typically called in the bh of a data ready interrupt handler
93 * in the device driver */
94/* Lock always held if their is a chance this may be called */
95/* Only one of these per ring may run concurrently - enforced by drivers */
Greg Kroah-Hartman19ca92e2010-05-04 22:33:27 -070096static int iio_store_to_sw_ring(struct iio_sw_ring_buffer *ring,
97 unsigned char *data, s64 timestamp)
Jonathan Cameron2235acb2009-08-18 18:06:27 +010098{
99 int ret = 0;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100100 unsigned char *temp_ptr, *change_test_ptr;
101
102 /* initial store */
Greg Kroah-Hartman19ca92e2010-05-04 22:33:27 -0700103 if (unlikely(ring->write_p == NULL)) {
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100104 ring->write_p = ring->data;
105 /* Doesn't actually matter if this is out of the set
106 * as long as the read pointer is valid before this
107 * passes it - guaranteed as set later in this function.
108 */
Manuel Stahlffcab072010-08-31 11:32:50 +0200109 ring->half_p = ring->data - ring->buf.length*ring->buf.bytes_per_datum/2;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100110 }
111 /* Copy data to where ever the current write pointer says */
Manuel Stahlffcab072010-08-31 11:32:50 +0200112 memcpy(ring->write_p, data, ring->buf.bytes_per_datum);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100113 barrier();
114 /* Update the pointer used to get most recent value.
115 * Always valid as either points to latest or second latest value.
116 * Before this runs it is null and read attempts fail with -EAGAIN.
117 */
118 ring->last_written_p = ring->write_p;
119 barrier();
120 /* temp_ptr used to ensure we never have an invalid pointer
121 * it may be slightly lagging, but never invalid
122 */
Manuel Stahlffcab072010-08-31 11:32:50 +0200123 temp_ptr = ring->write_p + ring->buf.bytes_per_datum;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100124 /* End of ring, back to the beginning */
Manuel Stahlffcab072010-08-31 11:32:50 +0200125 if (temp_ptr == ring->data + ring->buf.length*ring->buf.bytes_per_datum)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100126 temp_ptr = ring->data;
127 /* Update the write pointer
128 * always valid as long as this is the only function able to write.
129 * Care needed with smp systems to ensure more than one ring fill
130 * is never scheduled.
131 */
132 ring->write_p = temp_ptr;
133
Greg Kroah-Hartman19ca92e2010-05-04 22:33:27 -0700134 if (ring->read_p == NULL)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100135 ring->read_p = ring->data;
136 /* Buffer full - move the read pointer and create / escalate
137 * ring event */
138 /* Tricky case - if the read pointer moves before we adjust it.
139 * Handle by not pushing if it has moved - may result in occasional
140 * unnecessary buffer full events when it wasn't quite true.
141 */
142 else if (ring->write_p == ring->read_p) {
143 change_test_ptr = ring->read_p;
Manuel Stahlffcab072010-08-31 11:32:50 +0200144 temp_ptr = change_test_ptr + ring->buf.bytes_per_datum;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100145 if (temp_ptr
Manuel Stahlffcab072010-08-31 11:32:50 +0200146 == ring->data + ring->buf.length*ring->buf.bytes_per_datum) {
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100147 temp_ptr = ring->data;
148 }
149 /* We are moving pointer on one because the ring is full. Any
150 * change to the read pointer will be this or greater.
151 */
152 if (change_test_ptr == ring->read_p)
153 ring->read_p = temp_ptr;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100154 }
155 /* investigate if our event barrier has been passed */
156 /* There are definite 'issues' with this and chances of
157 * simultaneous read */
158 /* Also need to use loop count to ensure this only happens once */
Manuel Stahlffcab072010-08-31 11:32:50 +0200159 ring->half_p += ring->buf.bytes_per_datum;
160 if (ring->half_p == ring->data + ring->buf.length*ring->buf.bytes_per_datum)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100161 ring->half_p = ring->data;
162 if (ring->half_p == ring->read_p) {
Jonathan Camerona7348342011-05-18 14:40:55 +0100163 ring->buf.stufftoread = true;
164 wake_up_interruptible(&ring->buf.pollq);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100165 }
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100166 return ret;
167}
168
Jonathan Cameron5565a452011-05-18 14:42:24 +0100169static int iio_read_first_n_sw_rb(struct iio_ring_buffer *r,
170 size_t n, char __user *buf)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100171{
172 struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r);
173
174 u8 *initial_read_p, *initial_write_p, *current_read_p, *end_read_p;
Jonathan Camerond5857d62011-02-11 13:09:09 +0000175 u8 *data;
Jonathan Cameronb26a2182011-05-18 14:41:02 +0100176 int ret, max_copied, bytes_to_rip, dead_offset;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100177
178 /* A userspace program has probably made an error if it tries to
179 * read something that is not a whole number of bpds.
180 * Return an error.
181 */
Jonathan Cameronb4281732011-04-15 18:55:55 +0100182 if (n % ring->buf.bytes_per_datum) {
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100183 ret = -EINVAL;
184 printk(KERN_INFO "Ring buffer read request not whole number of"
Manuel Stahlffcab072010-08-31 11:32:50 +0200185 "samples: Request bytes %zd, Current bytes per datum %d\n",
Jonathan Cameronb4281732011-04-15 18:55:55 +0100186 n, ring->buf.bytes_per_datum);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100187 goto error_ret;
188 }
189 /* Limit size to whole of ring buffer */
Jonathan Cameronb4281732011-04-15 18:55:55 +0100190 bytes_to_rip = min((size_t)(ring->buf.bytes_per_datum*ring->buf.length),
191 n);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100192
Jonathan Camerond5857d62011-02-11 13:09:09 +0000193 data = kmalloc(bytes_to_rip, GFP_KERNEL);
194 if (data == NULL) {
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100195 ret = -ENOMEM;
196 goto error_ret;
197 }
198
199 /* build local copy */
200 initial_read_p = ring->read_p;
Greg Kroah-Hartman19ca92e2010-05-04 22:33:27 -0700201 if (unlikely(initial_read_p == NULL)) { /* No data here as yet */
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100202 ret = 0;
203 goto error_free_data_cpy;
204 }
205
206 initial_write_p = ring->write_p;
207
208 /* Need a consistent pair */
209 while ((initial_read_p != ring->read_p)
210 || (initial_write_p != ring->write_p)) {
211 initial_read_p = ring->read_p;
212 initial_write_p = ring->write_p;
213 }
214 if (initial_write_p == initial_read_p) {
215 /* No new data available.*/
216 ret = 0;
217 goto error_free_data_cpy;
218 }
219
220 if (initial_write_p >= initial_read_p + bytes_to_rip) {
221 /* write_p is greater than necessary, all is easy */
222 max_copied = bytes_to_rip;
Jonathan Camerond5857d62011-02-11 13:09:09 +0000223 memcpy(data, initial_read_p, max_copied);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100224 end_read_p = initial_read_p + max_copied;
225 } else if (initial_write_p > initial_read_p) {
226 /*not enough data to cpy */
227 max_copied = initial_write_p - initial_read_p;
Jonathan Camerond5857d62011-02-11 13:09:09 +0000228 memcpy(data, initial_read_p, max_copied);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100229 end_read_p = initial_write_p;
230 } else {
231 /* going through 'end' of ring buffer */
232 max_copied = ring->data
Manuel Stahlffcab072010-08-31 11:32:50 +0200233 + ring->buf.length*ring->buf.bytes_per_datum - initial_read_p;
Jonathan Camerond5857d62011-02-11 13:09:09 +0000234 memcpy(data, initial_read_p, max_copied);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100235 /* possible we are done if we align precisely with end */
236 if (max_copied == bytes_to_rip)
237 end_read_p = ring->data;
238 else if (initial_write_p
239 > ring->data + bytes_to_rip - max_copied) {
240 /* enough data to finish */
Jonathan Camerond5857d62011-02-11 13:09:09 +0000241 memcpy(data + max_copied, ring->data,
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100242 bytes_to_rip - max_copied);
243 max_copied = bytes_to_rip;
244 end_read_p = ring->data + (bytes_to_rip - max_copied);
245 } else { /* not enough data */
Jonathan Camerond5857d62011-02-11 13:09:09 +0000246 memcpy(data + max_copied, ring->data,
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100247 initial_write_p - ring->data);
248 max_copied += initial_write_p - ring->data;
249 end_read_p = initial_write_p;
250 }
251 }
252 /* Now to verify which section was cleanly copied - i.e. how far
253 * read pointer has been pushed */
254 current_read_p = ring->read_p;
255
256 if (initial_read_p <= current_read_p)
Jonathan Cameronb26a2182011-05-18 14:41:02 +0100257 dead_offset = current_read_p - initial_read_p;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100258 else
Jonathan Cameronb26a2182011-05-18 14:41:02 +0100259 dead_offset = ring->buf.length*ring->buf.bytes_per_datum
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100260 - (initial_read_p - current_read_p);
261
262 /* possible issue if the initial write has been lapped or indeed
263 * the point we were reading to has been passed */
264 /* No valid data read.
265 * In this case the read pointer is already correct having been
266 * pushed further than we would look. */
Jonathan Cameronb26a2182011-05-18 14:41:02 +0100267 if (max_copied - dead_offset < 0) {
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100268 ret = 0;
269 goto error_free_data_cpy;
270 }
271
272 /* setup the next read position */
273 /* Beware, this may fail due to concurrency fun and games.
274 * Possible that sufficient fill commands have run to push the read
275 * pointer past where we would be after the rip. If this occurs, leave
276 * it be.
277 */
278 /* Tricky - deal with loops */
279
280 while (ring->read_p != end_read_p)
281 ring->read_p = end_read_p;
282
Jonathan Cameronb26a2182011-05-18 14:41:02 +0100283 ret = max_copied - dead_offset;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100284
Jonathan Cameronb26a2182011-05-18 14:41:02 +0100285 if (copy_to_user(buf, data + dead_offset, ret)) {
Jonathan Camerond5857d62011-02-11 13:09:09 +0000286 ret = -EFAULT;
287 goto error_free_data_cpy;
288 }
Jonathan Camerona7348342011-05-18 14:40:55 +0100289
290 if (bytes_to_rip >= ring->buf.length*ring->buf.bytes_per_datum/2)
291 ring->buf.stufftoread = 0;
292
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100293error_free_data_cpy:
Jonathan Camerond5857d62011-02-11 13:09:09 +0000294 kfree(data);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100295error_ret:
Jonathan Camerond5857d62011-02-11 13:09:09 +0000296
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100297 return ret;
298}
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100299
Jonathan Cameron5565a452011-05-18 14:42:24 +0100300static int iio_store_to_sw_rb(struct iio_ring_buffer *r,
301 u8 *data,
302 s64 timestamp)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100303{
304 struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r);
305 return iio_store_to_sw_ring(ring, data, timestamp);
306}
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100307
Greg Kroah-Hartman19ca92e2010-05-04 22:33:27 -0700308static int iio_read_last_from_sw_ring(struct iio_sw_ring_buffer *ring,
309 unsigned char *data)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100310{
311 unsigned char *last_written_p_copy;
312
313 iio_mark_sw_rb_in_use(&ring->buf);
314again:
315 barrier();
316 last_written_p_copy = ring->last_written_p;
317 barrier(); /*unnessecary? */
318 /* Check there is anything here */
Greg Kroah-Hartman19ca92e2010-05-04 22:33:27 -0700319 if (last_written_p_copy == NULL)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100320 return -EAGAIN;
Manuel Stahlffcab072010-08-31 11:32:50 +0200321 memcpy(data, last_written_p_copy, ring->buf.bytes_per_datum);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100322
Jonathan Cameron8474ddd2010-05-04 14:43:11 +0100323 if (unlikely(ring->last_written_p != last_written_p_copy))
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100324 goto again;
325
326 iio_unmark_sw_rb_in_use(&ring->buf);
327 return 0;
328}
329
Jonathan Cameron5565a452011-05-18 14:42:24 +0100330static int iio_read_last_from_sw_rb(struct iio_ring_buffer *r,
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100331 unsigned char *data)
332{
333 return iio_read_last_from_sw_ring(iio_to_sw_ring(r), data);
334}
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100335
Jonathan Cameron5565a452011-05-18 14:42:24 +0100336static int iio_request_update_sw_rb(struct iio_ring_buffer *r)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100337{
338 int ret = 0;
339 struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r);
340
Jonathan Camerona7348342011-05-18 14:40:55 +0100341 r->stufftoread = false;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100342 spin_lock(&ring->use_lock);
343 if (!ring->update_needed)
344 goto error_ret;
345 if (ring->use_count) {
346 ret = -EAGAIN;
347 goto error_ret;
348 }
349 __iio_free_sw_ring_buffer(ring);
Manuel Stahlffcab072010-08-31 11:32:50 +0200350 ret = __iio_allocate_sw_ring_buffer(ring, ring->buf.bytes_per_datum,
Jonathan Cameron6f2dfb32010-03-02 13:35:35 +0000351 ring->buf.length);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100352error_ret:
353 spin_unlock(&ring->use_lock);
354 return ret;
355}
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100356
Jonathan Cameron5565a452011-05-18 14:42:24 +0100357static int iio_get_bytes_per_datum_sw_rb(struct iio_ring_buffer *r)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100358{
359 struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r);
Manuel Stahlffcab072010-08-31 11:32:50 +0200360 return ring->buf.bytes_per_datum;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100361}
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100362
Jonathan Cameron5565a452011-05-18 14:42:24 +0100363static int iio_set_bytes_per_datum_sw_rb(struct iio_ring_buffer *r, size_t bpd)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100364{
Manuel Stahlffcab072010-08-31 11:32:50 +0200365 if (r->bytes_per_datum != bpd) {
366 r->bytes_per_datum = bpd;
Jonathan Cameron5565a452011-05-18 14:42:24 +0100367 if (r->access->mark_param_change)
368 r->access->mark_param_change(r);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100369 }
370 return 0;
371}
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100372
Jonathan Cameron5565a452011-05-18 14:42:24 +0100373static int iio_get_length_sw_rb(struct iio_ring_buffer *r)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100374{
375 return r->length;
376}
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100377
Jonathan Cameron5565a452011-05-18 14:42:24 +0100378static int iio_set_length_sw_rb(struct iio_ring_buffer *r, int length)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100379{
380 if (r->length != length) {
381 r->length = length;
Jonathan Cameron5565a452011-05-18 14:42:24 +0100382 if (r->access->mark_param_change)
383 r->access->mark_param_change(r);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100384 }
385 return 0;
386}
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100387
Jonathan Cameron5565a452011-05-18 14:42:24 +0100388static int iio_mark_update_needed_sw_rb(struct iio_ring_buffer *r)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100389{
390 struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r);
391 ring->update_needed = true;
392 return 0;
393}
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100394
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100395static IIO_RING_ENABLE_ATTR;
Manuel Stahlffcab072010-08-31 11:32:50 +0200396static IIO_RING_BYTES_PER_DATUM_ATTR;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100397static IIO_RING_LENGTH_ATTR;
398
399/* Standard set of ring buffer attributes */
400static struct attribute *iio_ring_attributes[] = {
401 &dev_attr_length.attr,
Manuel Stahlffcab072010-08-31 11:32:50 +0200402 &dev_attr_bytes_per_datum.attr,
403 &dev_attr_enable.attr,
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100404 NULL,
405};
406
407static struct attribute_group iio_ring_attribute_group = {
408 .attrs = iio_ring_attributes,
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100409 .name = "buffer",
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100410};
411
412struct iio_ring_buffer *iio_sw_rb_allocate(struct iio_dev *indio_dev)
413{
414 struct iio_ring_buffer *buf;
415 struct iio_sw_ring_buffer *ring;
416
417 ring = kzalloc(sizeof *ring, GFP_KERNEL);
418 if (!ring)
Greg Kroah-Hartman19ca92e2010-05-04 22:33:27 -0700419 return NULL;
Jonathan Cameron5565a452011-05-18 14:42:24 +0100420 ring->update_needed = true;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100421 buf = &ring->buf;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100422 iio_ring_buffer_init(buf, indio_dev);
Jonathan Cameron6f2dfb32010-03-02 13:35:35 +0000423 __iio_init_sw_ring_buffer(ring);
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100424 buf->attrs = &iio_ring_attribute_group;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100425
426 return buf;
427}
428EXPORT_SYMBOL(iio_sw_rb_allocate);
429
430void iio_sw_rb_free(struct iio_ring_buffer *r)
431{
Jonathan Cameron1aa04272011-08-30 12:32:47 +0100432 kfree(iio_to_sw_ring(r));
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100433}
434EXPORT_SYMBOL(iio_sw_rb_free);
Barry Songad577f82010-07-11 16:39:16 +0100435
Jonathan Cameron5565a452011-05-18 14:42:24 +0100436const struct iio_ring_access_funcs ring_sw_access_funcs = {
437 .mark_in_use = &iio_mark_sw_rb_in_use,
438 .unmark_in_use = &iio_unmark_sw_rb_in_use,
439 .store_to = &iio_store_to_sw_rb,
440 .read_last = &iio_read_last_from_sw_rb,
441 .read_first_n = &iio_read_first_n_sw_rb,
442 .mark_param_change = &iio_mark_update_needed_sw_rb,
443 .request_update = &iio_request_update_sw_rb,
444 .get_bytes_per_datum = &iio_get_bytes_per_datum_sw_rb,
445 .set_bytes_per_datum = &iio_set_bytes_per_datum_sw_rb,
446 .get_length = &iio_get_length_sw_rb,
447 .set_length = &iio_set_length_sw_rb,
448};
449EXPORT_SYMBOL(ring_sw_access_funcs);
450
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100451MODULE_DESCRIPTION("Industrialio I/O software ring buffer");
452MODULE_LICENSE("GPL");