ssb: Add export.h to files using EXPORT_SYMBOL/THIS_MODULE
[deliverable/linux.git] / drivers / staging / iio / iio_simple_dummy_buffer.c
CommitLineData
9ad2e2e1
JC
1/**
2 * Copyright (c) 2011 Jonathan Cameron
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
7 *
8 * Buffer handling elements of industrial I/O reference driver.
9 * Uses the kfifo buffer.
10 *
11 * To test without hardware use the sysfs trigger.
12 */
13
14#include <linux/kernel.h>
15#include <linux/slab.h>
16#include <linux/interrupt.h>
17#include <linux/irq.h>
18#include <linux/bitmap.h>
19
20#include "iio.h"
21#include "trigger_consumer.h"
22#include "kfifo_buf.h"
23
24#include "iio_simple_dummy.h"
25
26/* Some fake data */
27
28static const s16 fakedata[] = {
29 [voltage0] = 7,
30 [diffvoltage1m2] = -33,
31 [diffvoltage3m4] = -2,
32 [accelx] = 344,
33};
34/**
35 * iio_simple_dummy_trigger_h() - the trigger handler function
36 * @irq: the interrupt number
37 * @p: private data - always a pointer to the poll func.
38 *
39 * This is the guts of buffered capture. On a trigger event occuring,
40 * if the pollfunc is attached then this handler is called as a threaded
41 * interrupt (and hence may sleep). It is responsible for grabbing data
42 * from the device and pushing it into the associated buffer.
43 */
44static irqreturn_t iio_simple_dummy_trigger_h(int irq, void *p)
45{
46 struct iio_poll_func *pf = p;
47 struct iio_dev *indio_dev = pf->indio_dev;
48 struct iio_buffer *buffer = indio_dev->buffer;
49 int len = 0;
50 /*
51 * The datasize is obtained from the buffer. It was stored when
52 * the preenable setup function was called.
53 */
54 size_t datasize = buffer->access->get_bytes_per_datum(buffer);
55 u16 *data = kmalloc(datasize, GFP_KERNEL);
56 if (data == NULL)
57 return -ENOMEM;
58
59 if (buffer->scan_count) {
60 /*
61 * Three common options here:
62 * hardware scans: certain combinations of channels make
63 * up a fast read. The capture will consist of all of them.
64 * Hence we just call the grab data function and fill the
65 * buffer without processing.
66 * sofware scans: can be considered to be random access
67 * so efficient reading is just a case of minimal bus
68 * transactions.
69 * software culled hardware scans:
70 * occasionally a driver may process the nearest hardware
71 * scan to avoid storing elements that are not desired. This
72 * is the fidliest option by far.
73 * Here lets pretend we have random access. And the values are
74 * in the constant table fakedata.
75 */
76 int i, j;
77 for (i = 0, j = 0; i < buffer->scan_count; i++) {
78 j = find_next_bit(buffer->scan_mask,
79 indio_dev->masklength, j + 1);
80 /* random access read form the 'device' */
81 data[i] = fakedata[j];
82 len += 2;
83 }
84 }
85 /* Store a timestampe at an 8 byte boundary */
86 if (buffer->scan_timestamp)
87 *(s64 *)(((phys_addr_t)data + len
88 + sizeof(s64) - 1) & ~(sizeof(s64) - 1))
89 = iio_get_time_ns();
90 buffer->access->store_to(buffer, (u8 *)data, pf->timestamp);
91
92 kfree(data);
93
94 /*
95 * Tell the core we are done with this trigger and ready for the
96 * next one.
97 */
98 iio_trigger_notify_done(indio_dev->trig);
99
100 return IRQ_HANDLED;
101}
102
103static const struct iio_buffer_setup_ops iio_simple_dummy_buffer_setup_ops = {
104 /*
105 * iio_sw_buffer_preenable:
106 * Generic function for equal sized ring elements + 64 bit timestamp
107 * Assumes that any combination of channels can be enabled.
108 * Typically replaced to implement restrictions on what combinations
109 * can be captured (hardware scan modes).
110 */
111 .preenable = &iio_sw_buffer_preenable,
112 /*
113 * iio_triggered_buffer_postenable:
114 * Generic function that simply attaches the pollfunc to the trigger.
115 * Replace this to mess with hardware state before we attach the
116 * trigger.
117 */
118 .postenable = &iio_triggered_buffer_postenable,
119 /*
120 * iio_triggered_buffer_predisable:
121 * Generic function that simple detaches the pollfunc from the trigger.
122 * Replace this to put hardware state back again after the trigger is
123 * detached but before userspace knows we have disabled the ring.
124 */
125 .predisable = &iio_triggered_buffer_predisable,
126};
127
128int iio_simple_dummy_configure_buffer(struct iio_dev *indio_dev)
129{
130 int ret;
131 struct iio_buffer *buffer;
132
133 /* Allocate a buffer to use - here a kfifo */
134 buffer = iio_kfifo_allocate(indio_dev);
135 if (buffer == NULL) {
136 ret = -ENOMEM;
137 goto error_ret;
138 }
139
140 indio_dev->buffer = buffer;
141 /* Tell the core how to access the buffer */
142 buffer->access = &kfifo_access_funcs;
143
144 /* Number of bytes per element */
145 buffer->bpe = 2;
146 /* Enable timestamps by default */
147 buffer->scan_timestamp = true;
148
149 /*
150 * Tell the core what device type specific functions should
151 * be run on either side of buffer capture enable / disable.
152 */
153 buffer->setup_ops = &iio_simple_dummy_buffer_setup_ops;
154 buffer->owner = THIS_MODULE;
155
156 /*
157 * Configure a polling function.
158 * When a trigger event with this polling function connected
159 * occurs, this function is run. Typically this grabs data
160 * from the device.
161 *
162 * NULL for the top half. This is normally implemented only if we
163 * either want to ping a capture now pin (no sleeping) or grab
164 * a timestamp as close as possible to a data ready trigger firing.
165 *
166 * IRQF_ONESHOT ensures irqs are masked such that only one instance
167 * of the handler can run at a time.
168 *
169 * "iio_simple_dummy_consumer%d" formatting string for the irq 'name'
170 * as seen under /proc/interrupts. Remaining parameters as per printk.
171 */
172 indio_dev->pollfunc = iio_alloc_pollfunc(NULL,
173 &iio_simple_dummy_trigger_h,
174 IRQF_ONESHOT,
175 indio_dev,
176 "iio_simple_dummy_consumer%d",
177 indio_dev->id);
178
179 if (indio_dev->pollfunc == NULL) {
180 ret = -ENOMEM;
181 goto error_free_buffer;
182 }
183
184 /*
185 * Notify the core that this device is capable of buffered capture
186 * driven by a trigger.
187 */
188 indio_dev->modes |= INDIO_BUFFER_TRIGGERED;
189 return 0;
190
191error_free_buffer:
192 iio_kfifo_free(indio_dev->buffer);
193error_ret:
194 return ret;
195
196}
197
198/**
199 * iio_simple_dummy_unconfigure_buffer() - release buffer resources
200 * @indo_dev: device instance state
201 */
202void iio_simple_dummy_unconfigure_buffer(struct iio_dev *indio_dev)
203{
204 iio_dealloc_pollfunc(indio_dev->pollfunc);
205 iio_kfifo_free(indio_dev->buffer);
206}
This page took 0.037969 seconds and 5 git commands to generate.