blob: ea0015eee13e85a560ce05cfe03c8fb153a99dc5 [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 Camerond5857d62011-02-11 13:09:09 +000015#include <linux/poll.h>
Jonathan Cameron2235acb2009-08-18 18:06:27 +010016#include "ring_sw.h"
Jonathan Cameron59883ba2010-07-11 16:39:18 +010017#include "trigger.h"
Jonathan Cameron2235acb2009-08-18 18:06:27 +010018
Jonathan Cameron6f2dfb32010-03-02 13:35:35 +000019static inline int __iio_allocate_sw_ring_buffer(struct iio_sw_ring_buffer *ring,
20 int bytes_per_datum, int length)
Jonathan Cameron2235acb2009-08-18 18:06:27 +010021{
22 if ((length == 0) || (bytes_per_datum == 0))
23 return -EINVAL;
Jonathan Cameron6f2dfb32010-03-02 13:35:35 +000024 __iio_update_ring_buffer(&ring->buf, bytes_per_datum, length);
Manuel Stahlffcab072010-08-31 11:32:50 +020025 ring->data = kmalloc(length*ring->buf.bytes_per_datum, GFP_ATOMIC);
Greg Kroah-Hartman19ca92e2010-05-04 22:33:27 -070026 ring->read_p = NULL;
27 ring->write_p = NULL;
28 ring->last_written_p = NULL;
29 ring->half_p = NULL;
Jonathan Cameron2235acb2009-08-18 18:06:27 +010030 return ring->data ? 0 : -ENOMEM;
31}
32
Jonathan Cameron6f2dfb32010-03-02 13:35:35 +000033static inline void __iio_init_sw_ring_buffer(struct iio_sw_ring_buffer *ring)
34{
35 spin_lock_init(&ring->use_lock);
36}
37
Jonathan Cameron2235acb2009-08-18 18:06:27 +010038static inline void __iio_free_sw_ring_buffer(struct iio_sw_ring_buffer *ring)
39{
40 kfree(ring->data);
41}
42
43void 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}
50EXPORT_SYMBOL(iio_mark_sw_rb_in_use);
51
52void 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}
59EXPORT_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-Hartman19ca92e2010-05-04 22:33:27 -070067static int iio_store_to_sw_ring(struct iio_sw_ring_buffer *ring,
68 unsigned char *data, s64 timestamp)
Jonathan Cameron2235acb2009-08-18 18:06:27 +010069{
70 int ret = 0;
71 int code;
72 unsigned char *temp_ptr, *change_test_ptr;
73
74 /* initial store */
Greg Kroah-Hartman19ca92e2010-05-04 22:33:27 -070075 if (unlikely(ring->write_p == NULL)) {
Jonathan Cameron2235acb2009-08-18 18:06:27 +010076 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 Stahlffcab072010-08-31 11:32:50 +020081 ring->half_p = ring->data - ring->buf.length*ring->buf.bytes_per_datum/2;
Jonathan Cameron2235acb2009-08-18 18:06:27 +010082 }
83 /* Copy data to where ever the current write pointer says */
Manuel Stahlffcab072010-08-31 11:32:50 +020084 memcpy(ring->write_p, data, ring->buf.bytes_per_datum);
Jonathan Cameron2235acb2009-08-18 18:06:27 +010085 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 Stahlffcab072010-08-31 11:32:50 +020095 temp_ptr = ring->write_p + ring->buf.bytes_per_datum;
Jonathan Cameron2235acb2009-08-18 18:06:27 +010096 /* End of ring, back to the beginning */
Manuel Stahlffcab072010-08-31 11:32:50 +020097 if (temp_ptr == ring->data + ring->buf.length*ring->buf.bytes_per_datum)
Jonathan Cameron2235acb2009-08-18 18:06:27 +010098 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-Hartman19ca92e2010-05-04 22:33:27 -0700106 if (ring->read_p == NULL)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100107 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 Stahlffcab072010-08-31 11:32:50 +0200116 temp_ptr = change_test_ptr + ring->buf.bytes_per_datum;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100117 if (temp_ptr
Manuel Stahlffcab072010-08-31 11:32:50 +0200118 == ring->data + ring->buf.length*ring->buf.bytes_per_datum) {
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100119 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 Nyenc849d252010-04-29 19:27:31 +0200130 IIO_EVENT_CODE_RING_100_FULL, timestamp);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100131 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 Stahlffcab072010-08-31 11:32:50 +0200139 ring->half_p += ring->buf.bytes_per_datum;
140 if (ring->half_p == ring->data + ring->buf.length*ring->buf.bytes_per_datum)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100141 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 }
151error_ret:
152 return ret;
153}
154
Jonathan Cameronb4281732011-04-15 18:55:55 +0100155int iio_read_first_n_sw_rb(struct iio_ring_buffer *r,
156 size_t n, char __user *buf, int *dead_offset)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100157{
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 Camerond5857d62011-02-11 13:09:09 +0000161 u8 *data;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100162 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 Cameronb4281732011-04-15 18:55:55 +0100169 if (n % ring->buf.bytes_per_datum) {
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100170 ret = -EINVAL;
171 printk(KERN_INFO "Ring buffer read request not whole number of"
Manuel Stahlffcab072010-08-31 11:32:50 +0200172 "samples: Request bytes %zd, Current bytes per datum %d\n",
Jonathan Cameronb4281732011-04-15 18:55:55 +0100173 n, ring->buf.bytes_per_datum);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100174 goto error_ret;
175 }
176 /* Limit size to whole of ring buffer */
Jonathan Cameronb4281732011-04-15 18:55:55 +0100177 bytes_to_rip = min((size_t)(ring->buf.bytes_per_datum*ring->buf.length),
178 n);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100179
Jonathan Camerond5857d62011-02-11 13:09:09 +0000180 data = kmalloc(bytes_to_rip, GFP_KERNEL);
181 if (data == NULL) {
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100182 ret = -ENOMEM;
183 goto error_ret;
184 }
185
186 /* build local copy */
187 initial_read_p = ring->read_p;
Greg Kroah-Hartman19ca92e2010-05-04 22:33:27 -0700188 if (unlikely(initial_read_p == NULL)) { /* No data here as yet */
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100189 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 Camerond5857d62011-02-11 13:09:09 +0000210 memcpy(data, initial_read_p, max_copied);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100211 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 Camerond5857d62011-02-11 13:09:09 +0000215 memcpy(data, initial_read_p, max_copied);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100216 end_read_p = initial_write_p;
217 } else {
218 /* going through 'end' of ring buffer */
219 max_copied = ring->data
Manuel Stahlffcab072010-08-31 11:32:50 +0200220 + ring->buf.length*ring->buf.bytes_per_datum - initial_read_p;
Jonathan Camerond5857d62011-02-11 13:09:09 +0000221 memcpy(data, initial_read_p, max_copied);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100222 /* 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 Camerond5857d62011-02-11 13:09:09 +0000228 memcpy(data + max_copied, ring->data,
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100229 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 Camerond5857d62011-02-11 13:09:09 +0000233 memcpy(data + max_copied, ring->data,
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100234 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 Stahlffcab072010-08-31 11:32:50 +0200246 *dead_offset = ring->buf.length*ring->buf.bytes_per_datum
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100247 - (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 Camerond5857d62011-02-11 13:09:09 +0000270 ret = max_copied - *dead_offset;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100271
Jonathan Camerond5857d62011-02-11 13:09:09 +0000272 if (copy_to_user(buf, data + *dead_offset, ret)) {
273 ret = -EFAULT;
274 goto error_free_data_cpy;
275 }
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100276error_free_data_cpy:
Jonathan Camerond5857d62011-02-11 13:09:09 +0000277 kfree(data);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100278error_ret:
Jonathan Camerond5857d62011-02-11 13:09:09 +0000279
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100280 return ret;
281}
Jonathan Cameronb4281732011-04-15 18:55:55 +0100282EXPORT_SYMBOL(iio_read_first_n_sw_rb);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100283
284int 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}
289EXPORT_SYMBOL(iio_store_to_sw_rb);
290
Greg Kroah-Hartman19ca92e2010-05-04 22:33:27 -0700291static int iio_read_last_from_sw_ring(struct iio_sw_ring_buffer *ring,
292 unsigned char *data)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100293{
294 unsigned char *last_written_p_copy;
295
296 iio_mark_sw_rb_in_use(&ring->buf);
297again:
298 barrier();
299 last_written_p_copy = ring->last_written_p;
300 barrier(); /*unnessecary? */
301 /* Check there is anything here */
Greg Kroah-Hartman19ca92e2010-05-04 22:33:27 -0700302 if (last_written_p_copy == NULL)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100303 return -EAGAIN;
Manuel Stahlffcab072010-08-31 11:32:50 +0200304 memcpy(data, last_written_p_copy, ring->buf.bytes_per_datum);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100305
Jonathan Cameron8474ddd2010-05-04 14:43:11 +0100306 if (unlikely(ring->last_written_p != last_written_p_copy))
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100307 goto again;
308
309 iio_unmark_sw_rb_in_use(&ring->buf);
310 return 0;
311}
312
313int 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}
318EXPORT_SYMBOL(iio_read_last_from_sw_rb);
319
320int 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 Stahlffcab072010-08-31 11:32:50 +0200333 ret = __iio_allocate_sw_ring_buffer(ring, ring->buf.bytes_per_datum,
Jonathan Cameron6f2dfb32010-03-02 13:35:35 +0000334 ring->buf.length);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100335error_ret:
336 spin_unlock(&ring->use_lock);
337 return ret;
338}
339EXPORT_SYMBOL(iio_request_update_sw_rb);
340
Manuel Stahlffcab072010-08-31 11:32:50 +0200341int iio_get_bytes_per_datum_sw_rb(struct iio_ring_buffer *r)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100342{
343 struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r);
Manuel Stahlffcab072010-08-31 11:32:50 +0200344 return ring->buf.bytes_per_datum;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100345}
Manuel Stahlffcab072010-08-31 11:32:50 +0200346EXPORT_SYMBOL(iio_get_bytes_per_datum_sw_rb);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100347
Manuel Stahlffcab072010-08-31 11:32:50 +0200348int iio_set_bytes_per_datum_sw_rb(struct iio_ring_buffer *r, size_t bpd)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100349{
Manuel Stahlffcab072010-08-31 11:32:50 +0200350 if (r->bytes_per_datum != bpd) {
351 r->bytes_per_datum = bpd;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100352 if (r->access.mark_param_change)
353 r->access.mark_param_change(r);
354 }
355 return 0;
356}
Manuel Stahlffcab072010-08-31 11:32:50 +0200357EXPORT_SYMBOL(iio_set_bytes_per_datum_sw_rb);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100358
359int iio_get_length_sw_rb(struct iio_ring_buffer *r)
360{
361 return r->length;
362}
363EXPORT_SYMBOL(iio_get_length_sw_rb);
364
365int 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}
374EXPORT_SYMBOL(iio_set_length_sw_rb);
375
376int 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}
382EXPORT_SYMBOL(iio_mark_update_needed_sw_rb);
383
384static 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
390static IIO_RING_ENABLE_ATTR;
Manuel Stahlffcab072010-08-31 11:32:50 +0200391static IIO_RING_BYTES_PER_DATUM_ATTR;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100392static IIO_RING_LENGTH_ATTR;
393
394/* Standard set of ring buffer attributes */
395static struct attribute *iio_ring_attributes[] = {
396 &dev_attr_length.attr,
Manuel Stahlffcab072010-08-31 11:32:50 +0200397 &dev_attr_bytes_per_datum.attr,
398 &dev_attr_enable.attr,
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100399 NULL,
400};
401
402static struct attribute_group iio_ring_attribute_group = {
403 .attrs = iio_ring_attributes,
404};
405
Alan Cox3860dc82009-08-19 16:59:31 +0100406static const struct attribute_group *iio_ring_attribute_groups[] = {
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100407 &iio_ring_attribute_group,
408 NULL
409};
410
411static struct device_type iio_sw_ring_type = {
412 .release = iio_sw_rb_release,
413 .groups = iio_ring_attribute_groups,
414};
415
416struct 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-Hartman19ca92e2010-05-04 22:33:27 -0700423 return NULL;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100424 buf = &ring->buf;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100425 iio_ring_buffer_init(buf, indio_dev);
Jonathan Cameron6f2dfb32010-03-02 13:35:35 +0000426 __iio_init_sw_ring_buffer(ring);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100427 buf->dev.type = &iio_sw_ring_type;
428 device_initialize(&buf->dev);
429 buf->dev.parent = &indio_dev->dev;
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100430 buf->dev.bus = &iio_bus_type;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100431 dev_set_drvdata(&buf->dev, (void *)buf);
432
433 return buf;
434}
435EXPORT_SYMBOL(iio_sw_rb_allocate);
436
437void iio_sw_rb_free(struct iio_ring_buffer *r)
438{
439 if (r)
440 iio_put_ring_buffer(r);
441}
442EXPORT_SYMBOL(iio_sw_rb_free);
Barry Songad577f82010-07-11 16:39:16 +0100443
444int iio_sw_ring_preenable(struct iio_dev *indio_dev)
445{
Manuel Stahlbf329632010-08-31 11:32:52 +0200446 struct iio_ring_buffer *ring = indio_dev->ring;
Barry Songad577f82010-07-11 16:39:16 +0100447 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 Stahlbf329632010-08-31 11:32:52 +0200450 if (!(ring->scan_count || ring->scan_timestamp))
Barry Songad577f82010-07-11 16:39:16 +0100451 return -EINVAL;
Manuel Stahlbf329632010-08-31 11:32:52 +0200452 if (ring->scan_timestamp)
453 if (ring->scan_count)
Barry Songad577f82010-07-11 16:39:16 +0100454 /* Timestamp (aligned to s64) and data */
Manuel Stahlbf329632010-08-31 11:32:52 +0200455 size = (((ring->scan_count * ring->bpe)
Barry Songad577f82010-07-11 16:39:16 +0100456 + sizeof(s64) - 1)
457 & ~(sizeof(s64) - 1))
458 + sizeof(s64);
459 else /* Timestamp only */
460 size = sizeof(s64);
461 else /* Data only */
Manuel Stahlbf329632010-08-31 11:32:52 +0200462 size = ring->scan_count * ring->bpe;
463 ring->access.set_bytes_per_datum(ring, size);
Barry Songad577f82010-07-11 16:39:16 +0100464
465 return 0;
466}
467EXPORT_SYMBOL(iio_sw_ring_preenable);
468
Jonathan Cameron59883ba2010-07-11 16:39:18 +0100469void 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 Stahlbf329632010-08-31 11:32:52 +0200474 struct iio_ring_buffer *ring = st->indio_dev->ring;
Jonathan Cameron59883ba2010-07-11 16:39:18 +0100475 int len = 0;
Manuel Stahlbf329632010-08-31 11:32:52 +0200476 size_t datasize = ring->access.get_bytes_per_datum(ring);
Jonathan Cameron59883ba2010-07-11 16:39:18 +0100477 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 Stahlbf329632010-08-31 11:32:52 +0200485 if (ring->scan_count)
Jonathan Cameron59883ba2010-07-11 16:39:18 +0100486 len = st->get_ring_element(st, data);
487
488 /* Guaranteed to be aligned with 8 byte boundary */
Manuel Stahlbf329632010-08-31 11:32:52 +0200489 if (ring->scan_timestamp)
Jonathan Cameron019415c2010-07-29 17:50:51 +0100490 *(s64 *)(((phys_addr_t)data + len
Jonathan Cameron59883ba2010-07-11 16:39:18 +0100491 + sizeof(s64) - 1) & ~(sizeof(s64) - 1))
492 = st->last_timestamp;
Manuel Stahlbf329632010-08-31 11:32:52 +0200493 ring->access.store_to(ring,
494 (u8 *)data,
Jonathan Cameron59883ba2010-07-11 16:39:18 +0100495 st->last_timestamp);
496
497 iio_trigger_notify_done(st->indio_dev->trig);
498 kfree(data);
499
500 return;
501}
502EXPORT_SYMBOL(iio_sw_trigger_bh_to_ring);
503
504void 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}
510EXPORT_SYMBOL(iio_sw_poll_func_th);
511
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100512MODULE_DESCRIPTION("Industrialio I/O software ring buffer");
513MODULE_LICENSE("GPL");