blob: dbf11e180629c529650fdefb9228bfa06f55d27a [file] [log] [blame]
Jonathan Cameron7026ea42009-08-18 18:06:24 +01001/* The industrial I/O core - generic ring buffer interfaces.
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
10#ifndef _IIO_RING_GENERIC_H_
11#define _IIO_RING_GENERIC_H_
12#include "iio.h"
13
Jonathan Cameron26620512010-07-11 16:39:14 +010014#ifdef CONFIG_IIO_RING_BUFFER
15
Jonathan Cameron7026ea42009-08-18 18:06:24 +010016struct iio_handler;
17struct iio_ring_buffer;
18struct iio_dev;
19
20/**
21 * iio_push_ring_event() - ring buffer specific push to event chrdev
22 * @ring_buf: ring buffer that is the event source
23 * @event_code: event indentification code
24 * @timestamp: time of event
25 **/
26int iio_push_ring_event(struct iio_ring_buffer *ring_buf,
27 int event_code,
28 s64 timestamp);
29/**
Randy Dunlap4c572602009-10-04 19:34:02 -070030 * iio_push_or_escallate_ring_event() - escalate or add as appropriate
31 * @ring_buf: ring buffer that is the event source
32 * @event_code: event indentification code
33 * @timestamp: time of event
Jonathan Cameron7026ea42009-08-18 18:06:24 +010034 *
Randy Dunlap4c572602009-10-04 19:34:02 -070035 * Typical usecase is to escalate a 50% ring full to 75% full if noone has yet
Jonathan Cameron7026ea42009-08-18 18:06:24 +010036 * read the first event. Clearly the 50% full is no longer of interest in
37 * typical use case.
38 **/
39int iio_push_or_escallate_ring_event(struct iio_ring_buffer *ring_buf,
40 int event_code,
41 s64 timestamp);
42
43/**
44 * struct iio_ring_access_funcs - access functions for ring buffers.
Jonathan Cameron7026ea42009-08-18 18:06:24 +010045 * @mark_in_use: reference counting, typically to prevent module removal
46 * @unmark_in_use: reduce reference count when no longer using ring buffer
47 * @store_to: actually store stuff to the ring buffer
48 * @read_last: get the last element stored
49 * @rip_lots: try to get a specified number of elements (must exist)
50 * @mark_param_change: notify ring that some relevant parameter has changed
51 * Often this means the underlying storage may need to
52 * change.
53 * @request_update: if a parameter change has been marked, update underlying
54 * storage.
55 * @get_bpd: get current bytes per datum
56 * @set_bpd: set number of bytes per datum
57 * @get_length: get number of datums in ring
58 * @set_length: set number of datums in ring
59 * @is_enabled: query if ring is currently being used
60 * @enable: enable the ring
61 *
62 * The purpose of this structure is to make the ring buffer element
63 * modular as event for a given driver, different usecases may require
Randy Dunlap4c572602009-10-04 19:34:02 -070064 * different ring designs (space efficiency vs speed for example).
Jonathan Cameron7026ea42009-08-18 18:06:24 +010065 *
66 * It is worth noting that a given ring implementation may only support a small
67 * proportion of these functions. The core code 'should' cope fine with any of
68 * them not existing.
69 **/
70struct iio_ring_access_funcs {
71 void (*mark_in_use)(struct iio_ring_buffer *ring);
72 void (*unmark_in_use)(struct iio_ring_buffer *ring);
73
74 int (*store_to)(struct iio_ring_buffer *ring, u8 *data, s64 timestamp);
75 int (*read_last)(struct iio_ring_buffer *ring, u8 *data);
76 int (*rip_lots)(struct iio_ring_buffer *ring,
77 size_t count,
78 u8 **data,
79 int *dead_offset);
80
81 int (*mark_param_change)(struct iio_ring_buffer *ring);
82 int (*request_update)(struct iio_ring_buffer *ring);
83
84 int (*get_bpd)(struct iio_ring_buffer *ring);
85 int (*set_bpd)(struct iio_ring_buffer *ring, size_t bpd);
86 int (*get_length)(struct iio_ring_buffer *ring);
87 int (*set_length)(struct iio_ring_buffer *ring, int length);
88
89 int (*is_enabled)(struct iio_ring_buffer *ring);
90 int (*enable)(struct iio_ring_buffer *ring);
91};
92
93/**
94 * struct iio_ring_buffer - general ring buffer structure
Randy Dunlap4c572602009-10-04 19:34:02 -070095 * @dev: ring buffer device struct
96 * @access_dev: system device struct for the chrdev
97 * @indio_dev: industrial I/O device structure
98 * @owner: module that owns the ring buffer (for ref counting)
99 * @id: unique id number
100 * @access_id: device id number
101 * @length: [DEVICE] number of datums in ring
102 * @bpd: [DEVICE] size of individual datum including timestamp
103 * @loopcount: [INTERN] number of times the ring has looped
104 * @access_handler: [INTERN] chrdev access handling
105 * @ev_int: [INTERN] chrdev interface for the event chrdev
106 * @shared_ev_pointer: [INTERN] the shared event pointer to allow escalation of
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100107 * events
Randy Dunlap4c572602009-10-04 19:34:02 -0700108 * @access: [DRIVER] ring access functions associated with the
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100109 * implementation.
Randy Dunlap4c572602009-10-04 19:34:02 -0700110 * @preenable: [DRIVER] function to run prior to marking ring enabled
111 * @postenable: [DRIVER] function to run after marking ring enabled
112 * @predisable: [DRIVER] function to run prior to marking ring disabled
113 * @postdisable: [DRIVER] function to run after marking ring disabled
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100114 **/
115struct iio_ring_buffer {
116 struct device dev;
117 struct device access_dev;
118 struct iio_dev *indio_dev;
119 struct module *owner;
120 int id;
121 int access_id;
122 int length;
123 int bpd;
124 int loopcount;
125 struct iio_handler access_handler;
126 struct iio_event_interface ev_int;
127 struct iio_shared_ev_pointer shared_ev_pointer;
128 struct iio_ring_access_funcs access;
129 int (*preenable)(struct iio_dev *);
130 int (*postenable)(struct iio_dev *);
131 int (*predisable)(struct iio_dev *);
132 int (*postdisable)(struct iio_dev *);
133
134};
135void iio_ring_buffer_init(struct iio_ring_buffer *ring,
136 struct iio_dev *dev_info);
137
138/**
Jonathan Cameron6f2dfb32010-03-02 13:35:35 +0000139 * __iio_update_ring_buffer() - update common elements of ring buffers
Randy Dunlap4c572602009-10-04 19:34:02 -0700140 * @ring: ring buffer that is the event source
141 * @bytes_per_datum: size of individual datum including timestamp
142 * @length: number of datums in ring
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100143 **/
Jonathan Cameron6f2dfb32010-03-02 13:35:35 +0000144static inline void __iio_update_ring_buffer(struct iio_ring_buffer *ring,
145 int bytes_per_datum, int length)
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100146{
147 ring->bpd = bytes_per_datum;
148 ring->length = length;
149 ring->loopcount = 0;
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100150}
151
152/**
153 * struct iio_scan_el - an individual element of a scan
154 * @dev_attr: control attribute (if directly controllable)
155 * @number: unique identifier of element (used for bit mask)
156 * @bit_count: number of bits in scan element
157 * @label: useful data for the scan el (often reg address)
158 * @set_state: for some devices datardy signals are generated
159 * for any enabled lines. This allows unwanted lines
160 * to be disabled and hence not get in the way.
161 **/
162struct iio_scan_el {
163 struct device_attribute dev_attr;
164 unsigned int number;
165 int bit_count;
166 unsigned int label;
167
168 int (*set_state)(struct iio_scan_el *scanel,
169 struct iio_dev *dev_info,
170 bool state);
171};
172
173#define to_iio_scan_el(_dev_attr) \
174 container_of(_dev_attr, struct iio_scan_el, dev_attr);
175
176/**
Randy Dunlap4c572602009-10-04 19:34:02 -0700177 * iio_scan_el_store() - sysfs scan element selection interface
178 * @dev: the target device
179 * @attr: the device attribute that is being processed
180 * @buf: input from userspace
181 * @len: length of input
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100182 *
183 * A generic function used to enable various scan elements. In some
184 * devices explicit read commands for each channel mean this is merely
185 * a software switch. In others this must actively disable the channel.
186 * Complexities occur when this interacts with data ready type triggers
187 * which may not reset unless every channel that is enabled is explicitly
188 * read.
189 **/
190ssize_t iio_scan_el_store(struct device *dev, struct device_attribute *attr,
191 const char *buf, size_t len);
192/**
193 * iio_scal_el_show() - sysfs interface to query whether a scan element is
Randy Dunlap4c572602009-10-04 19:34:02 -0700194 * is enabled or not
195 * @dev: the target device
196 * @attr: the device attribute that is being processed
197 * @buf: output buffer
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100198 **/
199ssize_t iio_scan_el_show(struct device *dev, struct device_attribute *attr,
200 char *buf);
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100201
202ssize_t iio_scan_el_ts_store(struct device *dev, struct device_attribute *attr,
203 const char *buf, size_t len);
204
205ssize_t iio_scan_el_ts_show(struct device *dev, struct device_attribute *attr,
206 char *buf);
207/**
Randy Dunlap4c572602009-10-04 19:34:02 -0700208 * IIO_SCAN_EL_C - declare and initialize a scan element with a control func
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100209 *
Randy Dunlap4c572602009-10-04 19:34:02 -0700210 * @_name: identifying name. Resulting struct is iio_scan_el_##_name,
Jonathan Cameron17227622010-05-04 14:43:02 +0100211 * sysfs element, _name##_en.
Randy Dunlap4c572602009-10-04 19:34:02 -0700212 * @_number: unique id number for the scan element.
213 * @_bits: number of bits in the scan element result (used in mixed bit
214 * length devices).
215 * @_label: indentification variable used by drivers. Often a reg address.
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100216 * @_controlfunc: function used to notify hardware of whether state changes
217 **/
218#define IIO_SCAN_EL_C(_name, _number, _bits, _label, _controlfunc) \
219 struct iio_scan_el iio_scan_el_##_name = { \
Jonathan Cameron17227622010-05-04 14:43:02 +0100220 .dev_attr = __ATTR(_number##_##_name##_en, \
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100221 S_IRUGO | S_IWUSR, \
222 iio_scan_el_show, \
223 iio_scan_el_store), \
224 .number = _number, \
225 .bit_count = _bits, \
226 .label = _label, \
227 .set_state = _controlfunc, \
228 }
Jonathan Cameron17227622010-05-04 14:43:02 +0100229
230#define IIO_SCAN_NAMED_EL_C(_name, _string, _number, _bits, _label, _cf) \
231 struct iio_scan_el iio_scan_el_##_name = { \
232 .dev_attr = __ATTR(_number##_##_string##_en, \
233 S_IRUGO | S_IWUSR, \
234 iio_scan_el_show, \
235 iio_scan_el_store), \
236 .number = _number, \
237 .bit_count = _bits, \
238 .label = _label, \
239 .set_state = _cf, \
240 }
241
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100242/**
Randy Dunlap4c572602009-10-04 19:34:02 -0700243 * IIO_SCAN_EL_TIMESTAMP - declare a special scan element for timestamps
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100244 *
245 * Odd one out. Handled slightly differently from other scan elements.
246 **/
Jonathan Cameronf3fb0012010-05-04 14:42:58 +0100247#define IIO_SCAN_EL_TIMESTAMP(number) \
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100248 struct iio_scan_el iio_scan_el_timestamp = { \
Jonathan Cameronf3fb0012010-05-04 14:42:58 +0100249 .dev_attr = __ATTR(number##_timestamp_en, \
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100250 S_IRUGO | S_IWUSR, \
251 iio_scan_el_ts_show, \
252 iio_scan_el_ts_store), \
253 }
254
255static inline void iio_put_ring_buffer(struct iio_ring_buffer *ring)
256{
257 put_device(&ring->dev);
258};
259
260#define to_iio_ring_buffer(d) \
261 container_of(d, struct iio_ring_buffer, dev)
262#define access_dev_to_iio_ring_buffer(d) \
263 container_of(d, struct iio_ring_buffer, access_dev)
Jonathan Cameron758d9882010-05-04 14:43:08 +0100264int iio_ring_buffer_register(struct iio_ring_buffer *ring, int id);
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100265void iio_ring_buffer_unregister(struct iio_ring_buffer *ring);
266
267ssize_t iio_read_ring_length(struct device *dev,
268 struct device_attribute *attr,
269 char *buf);
270ssize_t iio_write_ring_length(struct device *dev,
271 struct device_attribute *attr,
272 const char *buf,
273 size_t len);
274ssize_t iio_read_ring_bps(struct device *dev,
275 struct device_attribute *attr,
276 char *buf);
277ssize_t iio_store_ring_enable(struct device *dev,
278 struct device_attribute *attr,
279 const char *buf,
280 size_t len);
281ssize_t iio_show_ring_enable(struct device *dev,
282 struct device_attribute *attr,
283 char *buf);
284#define IIO_RING_LENGTH_ATTR DEVICE_ATTR(length, S_IRUGO | S_IWUSR, \
285 iio_read_ring_length, \
286 iio_write_ring_length)
287#define IIO_RING_BPS_ATTR DEVICE_ATTR(bps, S_IRUGO | S_IWUSR, \
288 iio_read_ring_bps, NULL)
289#define IIO_RING_ENABLE_ATTR DEVICE_ATTR(ring_enable, S_IRUGO | S_IWUSR, \
290 iio_show_ring_enable, \
291 iio_store_ring_enable)
Jonathan Cameron26620512010-07-11 16:39:14 +0100292#else /* CONFIG_IIO_RING_BUFFER */
293static inline int iio_ring_buffer_register(struct iio_ring_buffer *ring, int id)
294{
295 return 0;
296};
297static inline void iio_ring_buffer_unregister(struct iio_ring_buffer *ring)
298{};
299
300#endif /* CONFIG_IIO_RING_BUFFER */
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100301
302#endif /* _IIO_RING_GENERIC_H_ */