blob: b71ce390064971a4905fac4531cec10bcd9a5177 [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
155int iio_rip_sw_rb(struct iio_ring_buffer *r,
Jonathan Camerond5857d62011-02-11 13:09:09 +0000156 size_t count, 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 */
Manuel Stahlffcab072010-08-31 11:32:50 +0200169 if (count % 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",
173 count, 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 */
Manuel Stahlffcab072010-08-31 11:32:50 +0200177 bytes_to_rip = min((size_t)(ring->buf.bytes_per_datum*ring->buf.length), count);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100178
Jonathan Camerond5857d62011-02-11 13:09:09 +0000179 data = kmalloc(bytes_to_rip, GFP_KERNEL);
180 if (data == NULL) {
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100181 ret = -ENOMEM;
182 goto error_ret;
183 }
184
185 /* build local copy */
186 initial_read_p = ring->read_p;
Greg Kroah-Hartman19ca92e2010-05-04 22:33:27 -0700187 if (unlikely(initial_read_p == NULL)) { /* No data here as yet */
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100188 ret = 0;
189 goto error_free_data_cpy;
190 }
191
192 initial_write_p = ring->write_p;
193
194 /* Need a consistent pair */
195 while ((initial_read_p != ring->read_p)
196 || (initial_write_p != ring->write_p)) {
197 initial_read_p = ring->read_p;
198 initial_write_p = ring->write_p;
199 }
200 if (initial_write_p == initial_read_p) {
201 /* No new data available.*/
202 ret = 0;
203 goto error_free_data_cpy;
204 }
205
206 if (initial_write_p >= initial_read_p + bytes_to_rip) {
207 /* write_p is greater than necessary, all is easy */
208 max_copied = bytes_to_rip;
Jonathan Camerond5857d62011-02-11 13:09:09 +0000209 memcpy(data, initial_read_p, max_copied);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100210 end_read_p = initial_read_p + max_copied;
211 } else if (initial_write_p > initial_read_p) {
212 /*not enough data to cpy */
213 max_copied = initial_write_p - initial_read_p;
Jonathan Camerond5857d62011-02-11 13:09:09 +0000214 memcpy(data, initial_read_p, max_copied);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100215 end_read_p = initial_write_p;
216 } else {
217 /* going through 'end' of ring buffer */
218 max_copied = ring->data
Manuel Stahlffcab072010-08-31 11:32:50 +0200219 + ring->buf.length*ring->buf.bytes_per_datum - initial_read_p;
Jonathan Camerond5857d62011-02-11 13:09:09 +0000220 memcpy(data, initial_read_p, max_copied);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100221 /* possible we are done if we align precisely with end */
222 if (max_copied == bytes_to_rip)
223 end_read_p = ring->data;
224 else if (initial_write_p
225 > ring->data + bytes_to_rip - max_copied) {
226 /* enough data to finish */
Jonathan Camerond5857d62011-02-11 13:09:09 +0000227 memcpy(data + max_copied, ring->data,
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100228 bytes_to_rip - max_copied);
229 max_copied = bytes_to_rip;
230 end_read_p = ring->data + (bytes_to_rip - max_copied);
231 } else { /* not enough data */
Jonathan Camerond5857d62011-02-11 13:09:09 +0000232 memcpy(data + max_copied, ring->data,
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100233 initial_write_p - ring->data);
234 max_copied += initial_write_p - ring->data;
235 end_read_p = initial_write_p;
236 }
237 }
238 /* Now to verify which section was cleanly copied - i.e. how far
239 * read pointer has been pushed */
240 current_read_p = ring->read_p;
241
242 if (initial_read_p <= current_read_p)
243 *dead_offset = current_read_p - initial_read_p;
244 else
Manuel Stahlffcab072010-08-31 11:32:50 +0200245 *dead_offset = ring->buf.length*ring->buf.bytes_per_datum
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100246 - (initial_read_p - current_read_p);
247
248 /* possible issue if the initial write has been lapped or indeed
249 * the point we were reading to has been passed */
250 /* No valid data read.
251 * In this case the read pointer is already correct having been
252 * pushed further than we would look. */
253 if (max_copied - *dead_offset < 0) {
254 ret = 0;
255 goto error_free_data_cpy;
256 }
257
258 /* setup the next read position */
259 /* Beware, this may fail due to concurrency fun and games.
260 * Possible that sufficient fill commands have run to push the read
261 * pointer past where we would be after the rip. If this occurs, leave
262 * it be.
263 */
264 /* Tricky - deal with loops */
265
266 while (ring->read_p != end_read_p)
267 ring->read_p = end_read_p;
268
Jonathan Camerond5857d62011-02-11 13:09:09 +0000269 ret = max_copied - *dead_offset;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100270
Jonathan Camerond5857d62011-02-11 13:09:09 +0000271 if (copy_to_user(buf, data + *dead_offset, ret)) {
272 ret = -EFAULT;
273 goto error_free_data_cpy;
274 }
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100275error_free_data_cpy:
Jonathan Camerond5857d62011-02-11 13:09:09 +0000276 kfree(data);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100277error_ret:
Jonathan Camerond5857d62011-02-11 13:09:09 +0000278
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100279 return ret;
280}
281EXPORT_SYMBOL(iio_rip_sw_rb);
282
283int iio_store_to_sw_rb(struct iio_ring_buffer *r, u8 *data, s64 timestamp)
284{
285 struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r);
286 return iio_store_to_sw_ring(ring, data, timestamp);
287}
288EXPORT_SYMBOL(iio_store_to_sw_rb);
289
Greg Kroah-Hartman19ca92e2010-05-04 22:33:27 -0700290static int iio_read_last_from_sw_ring(struct iio_sw_ring_buffer *ring,
291 unsigned char *data)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100292{
293 unsigned char *last_written_p_copy;
294
295 iio_mark_sw_rb_in_use(&ring->buf);
296again:
297 barrier();
298 last_written_p_copy = ring->last_written_p;
299 barrier(); /*unnessecary? */
300 /* Check there is anything here */
Greg Kroah-Hartman19ca92e2010-05-04 22:33:27 -0700301 if (last_written_p_copy == NULL)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100302 return -EAGAIN;
Manuel Stahlffcab072010-08-31 11:32:50 +0200303 memcpy(data, last_written_p_copy, ring->buf.bytes_per_datum);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100304
Jonathan Cameron8474ddd2010-05-04 14:43:11 +0100305 if (unlikely(ring->last_written_p != last_written_p_copy))
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100306 goto again;
307
308 iio_unmark_sw_rb_in_use(&ring->buf);
309 return 0;
310}
311
312int iio_read_last_from_sw_rb(struct iio_ring_buffer *r,
313 unsigned char *data)
314{
315 return iio_read_last_from_sw_ring(iio_to_sw_ring(r), data);
316}
317EXPORT_SYMBOL(iio_read_last_from_sw_rb);
318
319int iio_request_update_sw_rb(struct iio_ring_buffer *r)
320{
321 int ret = 0;
322 struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r);
323
324 spin_lock(&ring->use_lock);
325 if (!ring->update_needed)
326 goto error_ret;
327 if (ring->use_count) {
328 ret = -EAGAIN;
329 goto error_ret;
330 }
331 __iio_free_sw_ring_buffer(ring);
Manuel Stahlffcab072010-08-31 11:32:50 +0200332 ret = __iio_allocate_sw_ring_buffer(ring, ring->buf.bytes_per_datum,
Jonathan Cameron6f2dfb32010-03-02 13:35:35 +0000333 ring->buf.length);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100334error_ret:
335 spin_unlock(&ring->use_lock);
336 return ret;
337}
338EXPORT_SYMBOL(iio_request_update_sw_rb);
339
Manuel Stahlffcab072010-08-31 11:32:50 +0200340int iio_get_bytes_per_datum_sw_rb(struct iio_ring_buffer *r)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100341{
342 struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r);
Manuel Stahlffcab072010-08-31 11:32:50 +0200343 return ring->buf.bytes_per_datum;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100344}
Manuel Stahlffcab072010-08-31 11:32:50 +0200345EXPORT_SYMBOL(iio_get_bytes_per_datum_sw_rb);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100346
Manuel Stahlffcab072010-08-31 11:32:50 +0200347int iio_set_bytes_per_datum_sw_rb(struct iio_ring_buffer *r, size_t bpd)
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100348{
Manuel Stahlffcab072010-08-31 11:32:50 +0200349 if (r->bytes_per_datum != bpd) {
350 r->bytes_per_datum = bpd;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100351 if (r->access.mark_param_change)
352 r->access.mark_param_change(r);
353 }
354 return 0;
355}
Manuel Stahlffcab072010-08-31 11:32:50 +0200356EXPORT_SYMBOL(iio_set_bytes_per_datum_sw_rb);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100357
358int iio_get_length_sw_rb(struct iio_ring_buffer *r)
359{
360 return r->length;
361}
362EXPORT_SYMBOL(iio_get_length_sw_rb);
363
364int iio_set_length_sw_rb(struct iio_ring_buffer *r, int length)
365{
366 if (r->length != length) {
367 r->length = length;
368 if (r->access.mark_param_change)
369 r->access.mark_param_change(r);
370 }
371 return 0;
372}
373EXPORT_SYMBOL(iio_set_length_sw_rb);
374
375int iio_mark_update_needed_sw_rb(struct iio_ring_buffer *r)
376{
377 struct iio_sw_ring_buffer *ring = iio_to_sw_ring(r);
378 ring->update_needed = true;
379 return 0;
380}
381EXPORT_SYMBOL(iio_mark_update_needed_sw_rb);
382
383static void iio_sw_rb_release(struct device *dev)
384{
385 struct iio_ring_buffer *r = to_iio_ring_buffer(dev);
386 kfree(iio_to_sw_ring(r));
387}
388
389static IIO_RING_ENABLE_ATTR;
Manuel Stahlffcab072010-08-31 11:32:50 +0200390static IIO_RING_BYTES_PER_DATUM_ATTR;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100391static IIO_RING_LENGTH_ATTR;
392
393/* Standard set of ring buffer attributes */
394static struct attribute *iio_ring_attributes[] = {
395 &dev_attr_length.attr,
Manuel Stahlffcab072010-08-31 11:32:50 +0200396 &dev_attr_bytes_per_datum.attr,
397 &dev_attr_enable.attr,
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100398 NULL,
399};
400
401static struct attribute_group iio_ring_attribute_group = {
402 .attrs = iio_ring_attributes,
403};
404
Alan Cox3860dc82009-08-19 16:59:31 +0100405static const struct attribute_group *iio_ring_attribute_groups[] = {
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100406 &iio_ring_attribute_group,
407 NULL
408};
409
410static struct device_type iio_sw_ring_type = {
411 .release = iio_sw_rb_release,
412 .groups = iio_ring_attribute_groups,
413};
414
415struct iio_ring_buffer *iio_sw_rb_allocate(struct iio_dev *indio_dev)
416{
417 struct iio_ring_buffer *buf;
418 struct iio_sw_ring_buffer *ring;
419
420 ring = kzalloc(sizeof *ring, GFP_KERNEL);
421 if (!ring)
Greg Kroah-Hartman19ca92e2010-05-04 22:33:27 -0700422 return NULL;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100423 buf = &ring->buf;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100424 iio_ring_buffer_init(buf, indio_dev);
Jonathan Cameron6f2dfb32010-03-02 13:35:35 +0000425 __iio_init_sw_ring_buffer(ring);
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100426 buf->dev.type = &iio_sw_ring_type;
427 device_initialize(&buf->dev);
428 buf->dev.parent = &indio_dev->dev;
Jonathan Cameron5aaaeba2010-05-04 14:43:00 +0100429 buf->dev.bus = &iio_bus_type;
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100430 dev_set_drvdata(&buf->dev, (void *)buf);
431
432 return buf;
433}
434EXPORT_SYMBOL(iio_sw_rb_allocate);
435
436void iio_sw_rb_free(struct iio_ring_buffer *r)
437{
438 if (r)
439 iio_put_ring_buffer(r);
440}
441EXPORT_SYMBOL(iio_sw_rb_free);
Barry Songad577f82010-07-11 16:39:16 +0100442
443int iio_sw_ring_preenable(struct iio_dev *indio_dev)
444{
Manuel Stahlbf329632010-08-31 11:32:52 +0200445 struct iio_ring_buffer *ring = indio_dev->ring;
Barry Songad577f82010-07-11 16:39:16 +0100446 size_t size;
447 dev_dbg(&indio_dev->dev, "%s\n", __func__);
448 /* Check if there are any scan elements enabled, if not fail*/
Manuel Stahlbf329632010-08-31 11:32:52 +0200449 if (!(ring->scan_count || ring->scan_timestamp))
Barry Songad577f82010-07-11 16:39:16 +0100450 return -EINVAL;
Manuel Stahlbf329632010-08-31 11:32:52 +0200451 if (ring->scan_timestamp)
452 if (ring->scan_count)
Barry Songad577f82010-07-11 16:39:16 +0100453 /* Timestamp (aligned to s64) and data */
Manuel Stahlbf329632010-08-31 11:32:52 +0200454 size = (((ring->scan_count * ring->bpe)
Barry Songad577f82010-07-11 16:39:16 +0100455 + sizeof(s64) - 1)
456 & ~(sizeof(s64) - 1))
457 + sizeof(s64);
458 else /* Timestamp only */
459 size = sizeof(s64);
460 else /* Data only */
Manuel Stahlbf329632010-08-31 11:32:52 +0200461 size = ring->scan_count * ring->bpe;
462 ring->access.set_bytes_per_datum(ring, size);
Barry Songad577f82010-07-11 16:39:16 +0100463
464 return 0;
465}
466EXPORT_SYMBOL(iio_sw_ring_preenable);
467
Jonathan Cameron59883ba2010-07-11 16:39:18 +0100468void iio_sw_trigger_bh_to_ring(struct work_struct *work_s)
469{
470 struct iio_sw_ring_helper_state *st
471 = container_of(work_s, struct iio_sw_ring_helper_state,
472 work_trigger_to_ring);
Manuel Stahlbf329632010-08-31 11:32:52 +0200473 struct iio_ring_buffer *ring = st->indio_dev->ring;
Jonathan Cameron59883ba2010-07-11 16:39:18 +0100474 int len = 0;
Manuel Stahlbf329632010-08-31 11:32:52 +0200475 size_t datasize = ring->access.get_bytes_per_datum(ring);
Jonathan Cameron59883ba2010-07-11 16:39:18 +0100476 char *data = kmalloc(datasize, GFP_KERNEL);
477
478 if (data == NULL) {
479 dev_err(st->indio_dev->dev.parent,
480 "memory alloc failed in ring bh");
481 return;
482 }
483
Manuel Stahlbf329632010-08-31 11:32:52 +0200484 if (ring->scan_count)
Jonathan Cameron59883ba2010-07-11 16:39:18 +0100485 len = st->get_ring_element(st, data);
486
487 /* Guaranteed to be aligned with 8 byte boundary */
Manuel Stahlbf329632010-08-31 11:32:52 +0200488 if (ring->scan_timestamp)
Jonathan Cameron019415c2010-07-29 17:50:51 +0100489 *(s64 *)(((phys_addr_t)data + len
Jonathan Cameron59883ba2010-07-11 16:39:18 +0100490 + sizeof(s64) - 1) & ~(sizeof(s64) - 1))
491 = st->last_timestamp;
Manuel Stahlbf329632010-08-31 11:32:52 +0200492 ring->access.store_to(ring,
493 (u8 *)data,
Jonathan Cameron59883ba2010-07-11 16:39:18 +0100494 st->last_timestamp);
495
496 iio_trigger_notify_done(st->indio_dev->trig);
497 kfree(data);
498
499 return;
500}
501EXPORT_SYMBOL(iio_sw_trigger_bh_to_ring);
502
503void iio_sw_poll_func_th(struct iio_dev *indio_dev, s64 time)
504{ struct iio_sw_ring_helper_state *h
505 = iio_dev_get_devdata(indio_dev);
506 h->last_timestamp = time;
507 schedule_work(&h->work_trigger_to_ring);
508}
509EXPORT_SYMBOL(iio_sw_poll_func_th);
510
Jonathan Cameron2235acb2009-08-18 18:06:27 +0100511MODULE_DESCRIPTION("Industrialio I/O software ring buffer");
512MODULE_LICENSE("GPL");