blob: 7b29cd5a91d2f171d6609a9818bb7f5ac1c08c1a [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_ring_buffer;
Jonathan Cameron7026ea42009-08-18 18:06:24 +010017
18/**
19 * iio_push_ring_event() - ring buffer specific push to event chrdev
20 * @ring_buf: ring buffer that is the event source
21 * @event_code: event indentification code
22 * @timestamp: time of event
23 **/
24int iio_push_ring_event(struct iio_ring_buffer *ring_buf,
25 int event_code,
26 s64 timestamp);
27/**
Randy Dunlap4c572602009-10-04 19:34:02 -070028 * iio_push_or_escallate_ring_event() - escalate or add as appropriate
29 * @ring_buf: ring buffer that is the event source
30 * @event_code: event indentification code
31 * @timestamp: time of event
Jonathan Cameron7026ea42009-08-18 18:06:24 +010032 *
Randy Dunlap4c572602009-10-04 19:34:02 -070033 * Typical usecase is to escalate a 50% ring full to 75% full if noone has yet
Jonathan Cameron7026ea42009-08-18 18:06:24 +010034 * read the first event. Clearly the 50% full is no longer of interest in
35 * typical use case.
36 **/
37int iio_push_or_escallate_ring_event(struct iio_ring_buffer *ring_buf,
38 int event_code,
39 s64 timestamp);
40
41/**
42 * struct iio_ring_access_funcs - access functions for ring buffers.
Jonathan Cameron7026ea42009-08-18 18:06:24 +010043 * @mark_in_use: reference counting, typically to prevent module removal
44 * @unmark_in_use: reduce reference count when no longer using ring buffer
45 * @store_to: actually store stuff to the ring buffer
46 * @read_last: get the last element stored
47 * @rip_lots: try to get a specified number of elements (must exist)
48 * @mark_param_change: notify ring that some relevant parameter has changed
49 * Often this means the underlying storage may need to
50 * change.
51 * @request_update: if a parameter change has been marked, update underlying
52 * storage.
Jonathan Cameronc3e5d412010-09-04 17:54:45 +010053 * @get_bytes_per_datum:get current bytes per datum
54 * @set_bytes_per_datum:set number of bytes per datum
Jonathan Cameron7026ea42009-08-18 18:06:24 +010055 * @get_length: get number of datums in ring
56 * @set_length: set number of datums in ring
57 * @is_enabled: query if ring is currently being used
58 * @enable: enable the ring
59 *
60 * The purpose of this structure is to make the ring buffer element
61 * modular as event for a given driver, different usecases may require
Randy Dunlap4c572602009-10-04 19:34:02 -070062 * different ring designs (space efficiency vs speed for example).
Jonathan Cameron7026ea42009-08-18 18:06:24 +010063 *
64 * It is worth noting that a given ring implementation may only support a small
65 * proportion of these functions. The core code 'should' cope fine with any of
66 * them not existing.
67 **/
68struct iio_ring_access_funcs {
69 void (*mark_in_use)(struct iio_ring_buffer *ring);
70 void (*unmark_in_use)(struct iio_ring_buffer *ring);
71
72 int (*store_to)(struct iio_ring_buffer *ring, u8 *data, s64 timestamp);
73 int (*read_last)(struct iio_ring_buffer *ring, u8 *data);
74 int (*rip_lots)(struct iio_ring_buffer *ring,
75 size_t count,
76 u8 **data,
77 int *dead_offset);
78
79 int (*mark_param_change)(struct iio_ring_buffer *ring);
80 int (*request_update)(struct iio_ring_buffer *ring);
81
Manuel Stahlffcab072010-08-31 11:32:50 +020082 int (*get_bytes_per_datum)(struct iio_ring_buffer *ring);
83 int (*set_bytes_per_datum)(struct iio_ring_buffer *ring, size_t bpd);
Jonathan Cameron7026ea42009-08-18 18:06:24 +010084 int (*get_length)(struct iio_ring_buffer *ring);
85 int (*set_length)(struct iio_ring_buffer *ring, int length);
86
87 int (*is_enabled)(struct iio_ring_buffer *ring);
88 int (*enable)(struct iio_ring_buffer *ring);
89};
90
91/**
92 * struct iio_ring_buffer - general ring buffer structure
Randy Dunlap4c572602009-10-04 19:34:02 -070093 * @dev: ring buffer device struct
94 * @access_dev: system device struct for the chrdev
95 * @indio_dev: industrial I/O device structure
96 * @owner: module that owns the ring buffer (for ref counting)
97 * @id: unique id number
98 * @access_id: device id number
99 * @length: [DEVICE] number of datums in ring
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100100 * @bytes_per_datum: [DEVICE] size of individual datum including timestamp
Barry Songad577f82010-07-11 16:39:16 +0100101 * @bpe: [DEVICE] size of individual channel value
Randy Dunlap4c572602009-10-04 19:34:02 -0700102 * @loopcount: [INTERN] number of times the ring has looped
Manuel Stahlbf329632010-08-31 11:32:52 +0200103 * @scan_el_attrs: [DRIVER] control of scan elements if that scan mode
104 * control method is used
105 * @scan_count: [INTERN] the number of elements in the current scan mode
106 * @scan_mask: [INTERN] bitmask used in masking scan mode elements
107 * @scan_timestamp: [INTERN] does the scan mode include a timestamp
Randy Dunlap4c572602009-10-04 19:34:02 -0700108 * @access_handler: [INTERN] chrdev access handling
109 * @ev_int: [INTERN] chrdev interface for the event chrdev
110 * @shared_ev_pointer: [INTERN] the shared event pointer to allow escalation of
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100111 * events
Randy Dunlap4c572602009-10-04 19:34:02 -0700112 * @access: [DRIVER] ring access functions associated with the
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100113 * implementation.
Randy Dunlap4c572602009-10-04 19:34:02 -0700114 * @preenable: [DRIVER] function to run prior to marking ring enabled
115 * @postenable: [DRIVER] function to run after marking ring enabled
116 * @predisable: [DRIVER] function to run prior to marking ring disabled
117 * @postdisable: [DRIVER] function to run after marking ring disabled
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100118 **/
119struct iio_ring_buffer {
120 struct device dev;
121 struct device access_dev;
122 struct iio_dev *indio_dev;
123 struct module *owner;
124 int id;
125 int access_id;
126 int length;
Manuel Stahlffcab072010-08-31 11:32:50 +0200127 int bytes_per_datum;
Barry Songad577f82010-07-11 16:39:16 +0100128 int bpe;
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100129 int loopcount;
Manuel Stahlbf329632010-08-31 11:32:52 +0200130 struct attribute_group *scan_el_attrs;
131 int scan_count;
132 u32 scan_mask;
133 bool scan_timestamp;
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100134 struct iio_handler access_handler;
135 struct iio_event_interface ev_int;
136 struct iio_shared_ev_pointer shared_ev_pointer;
137 struct iio_ring_access_funcs access;
138 int (*preenable)(struct iio_dev *);
139 int (*postenable)(struct iio_dev *);
140 int (*predisable)(struct iio_dev *);
141 int (*postdisable)(struct iio_dev *);
142
143};
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100144
145/**
146 * iio_ring_buffer_init() - Initialize the buffer structure
147 * @ring: buffer to be initialized
148 * @dev_info: the iio device the buffer is assocated with
149 **/
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100150void iio_ring_buffer_init(struct iio_ring_buffer *ring,
151 struct iio_dev *dev_info);
152
153/**
Jonathan Cameron6f2dfb32010-03-02 13:35:35 +0000154 * __iio_update_ring_buffer() - update common elements of ring buffers
Randy Dunlap4c572602009-10-04 19:34:02 -0700155 * @ring: ring buffer that is the event source
156 * @bytes_per_datum: size of individual datum including timestamp
157 * @length: number of datums in ring
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100158 **/
Jonathan Cameron6f2dfb32010-03-02 13:35:35 +0000159static inline void __iio_update_ring_buffer(struct iio_ring_buffer *ring,
160 int bytes_per_datum, int length)
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100161{
Manuel Stahlffcab072010-08-31 11:32:50 +0200162 ring->bytes_per_datum = bytes_per_datum;
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100163 ring->length = length;
164 ring->loopcount = 0;
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100165}
166
167/**
168 * struct iio_scan_el - an individual element of a scan
169 * @dev_attr: control attribute (if directly controllable)
170 * @number: unique identifier of element (used for bit mask)
171 * @bit_count: number of bits in scan element
172 * @label: useful data for the scan el (often reg address)
173 * @set_state: for some devices datardy signals are generated
174 * for any enabled lines. This allows unwanted lines
175 * to be disabled and hence not get in the way.
176 **/
177struct iio_scan_el {
178 struct device_attribute dev_attr;
179 unsigned int number;
180 int bit_count;
181 unsigned int label;
182
183 int (*set_state)(struct iio_scan_el *scanel,
184 struct iio_dev *dev_info,
185 bool state);
186};
187
188#define to_iio_scan_el(_dev_attr) \
189 container_of(_dev_attr, struct iio_scan_el, dev_attr);
190
191/**
Randy Dunlap4c572602009-10-04 19:34:02 -0700192 * iio_scan_el_store() - sysfs scan element selection interface
193 * @dev: the target device
194 * @attr: the device attribute that is being processed
195 * @buf: input from userspace
196 * @len: length of input
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100197 *
198 * A generic function used to enable various scan elements. In some
199 * devices explicit read commands for each channel mean this is merely
200 * a software switch. In others this must actively disable the channel.
201 * Complexities occur when this interacts with data ready type triggers
202 * which may not reset unless every channel that is enabled is explicitly
203 * read.
204 **/
205ssize_t iio_scan_el_store(struct device *dev, struct device_attribute *attr,
206 const char *buf, size_t len);
207/**
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100208 * iio_scan_el_show() - sysfs interface to query whether a scan element
Randy Dunlap4c572602009-10-04 19:34:02 -0700209 * is enabled or not
210 * @dev: the target device
211 * @attr: the device attribute that is being processed
212 * @buf: output buffer
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100213 **/
214ssize_t iio_scan_el_show(struct device *dev, struct device_attribute *attr,
215 char *buf);
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100216
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100217/**
218 * iio_scan_el_ts_store() - sysfs interface to set whether a timestamp is included
219 * in the scan.
220 **/
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100221ssize_t iio_scan_el_ts_store(struct device *dev, struct device_attribute *attr,
222 const char *buf, size_t len);
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100223/**
224 * iio_scan_el_ts_show() - sysfs interface to query if a timestamp is included
225 * in the scan.
226 **/
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100227ssize_t iio_scan_el_ts_show(struct device *dev, struct device_attribute *attr,
228 char *buf);
229/**
Randy Dunlap4c572602009-10-04 19:34:02 -0700230 * IIO_SCAN_EL_C - declare and initialize a scan element with a control func
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100231 *
Randy Dunlap4c572602009-10-04 19:34:02 -0700232 * @_name: identifying name. Resulting struct is iio_scan_el_##_name,
Jonathan Cameron17227622010-05-04 14:43:02 +0100233 * sysfs element, _name##_en.
Randy Dunlap4c572602009-10-04 19:34:02 -0700234 * @_number: unique id number for the scan element.
235 * @_bits: number of bits in the scan element result (used in mixed bit
236 * length devices).
237 * @_label: indentification variable used by drivers. Often a reg address.
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100238 * @_controlfunc: function used to notify hardware of whether state changes
239 **/
Jonathan Cameron69584bd2010-07-11 16:39:15 +0100240#define __IIO_SCAN_EL_C(_name, _number, _bits, _label, _controlfunc) \
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100241 struct iio_scan_el iio_scan_el_##_name = { \
Jonathan Cameron17227622010-05-04 14:43:02 +0100242 .dev_attr = __ATTR(_number##_##_name##_en, \
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100243 S_IRUGO | S_IWUSR, \
244 iio_scan_el_show, \
245 iio_scan_el_store), \
246 .number = _number, \
247 .bit_count = _bits, \
248 .label = _label, \
249 .set_state = _controlfunc, \
250 }
Jonathan Cameron17227622010-05-04 14:43:02 +0100251
Jonathan Cameron69584bd2010-07-11 16:39:15 +0100252#define IIO_SCAN_EL_C(_name, _number, _bits, _label, _controlfunc) \
253 __IIO_SCAN_EL_C(_name, _number, _bits, _label, _controlfunc)
254
255#define __IIO_SCAN_NAMED_EL_C(_name, _string, _number, _bits, _label, _cf) \
Jonathan Cameron17227622010-05-04 14:43:02 +0100256 struct iio_scan_el iio_scan_el_##_name = { \
257 .dev_attr = __ATTR(_number##_##_string##_en, \
258 S_IRUGO | S_IWUSR, \
259 iio_scan_el_show, \
260 iio_scan_el_store), \
261 .number = _number, \
262 .bit_count = _bits, \
263 .label = _label, \
264 .set_state = _cf, \
265 }
Jonathan Cameron69584bd2010-07-11 16:39:15 +0100266#define IIO_SCAN_NAMED_EL_C(_name, _string, _number, _bits, _label, _cf) \
267 __IIO_SCAN_NAMED_EL_C(_name, _string, _number, _bits, _label, _cf)
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100268/**
Randy Dunlap4c572602009-10-04 19:34:02 -0700269 * IIO_SCAN_EL_TIMESTAMP - declare a special scan element for timestamps
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100270 * @number: specify where in the scan order this is stored.
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100271 *
272 * Odd one out. Handled slightly differently from other scan elements.
273 **/
Jonathan Cameronf3fb0012010-05-04 14:42:58 +0100274#define IIO_SCAN_EL_TIMESTAMP(number) \
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100275 struct iio_scan_el iio_scan_el_timestamp = { \
Jonathan Cameronf3fb0012010-05-04 14:42:58 +0100276 .dev_attr = __ATTR(number##_timestamp_en, \
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100277 S_IRUGO | S_IWUSR, \
278 iio_scan_el_ts_show, \
279 iio_scan_el_ts_store), \
280 }
281
Manuel Stahlbf329632010-08-31 11:32:52 +0200282/*
283 * These are mainly provided to allow for a change of implementation if a device
284 * has a large number of scan elements
285 */
286#define IIO_MAX_SCAN_LENGTH 31
287
288/* note 0 used as error indicator as it doesn't make sense. */
289static inline u32 iio_scan_mask_match(u32 *av_masks, u32 mask)
290{
291 while (*av_masks) {
292 if (!(~*av_masks & mask))
293 return *av_masks;
294 av_masks++;
295 }
296 return 0;
297}
298
299static inline int iio_scan_mask_query(struct iio_ring_buffer *ring, int bit)
300{
301 struct iio_dev *dev_info = ring->indio_dev;
302 u32 mask;
303
304 if (bit > IIO_MAX_SCAN_LENGTH)
305 return -EINVAL;
306
307 if (!ring->scan_mask)
308 return 0;
309
310 if (dev_info->available_scan_masks)
311 mask = iio_scan_mask_match(dev_info->available_scan_masks,
312 ring->scan_mask);
313 else
314 mask = ring->scan_mask;
315
316 if (!mask)
317 return -EINVAL;
318
319 return !!(mask & (1 << bit));
320};
321
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100322/**
323 * iio_scan_mask_set() - set particular bit in the scan mask
324 * @ring: the ring buffer whose scan mask we are interested in
325 * @bit: the bit to be set.
326 **/
Manuel Stahlbf329632010-08-31 11:32:52 +0200327static inline int iio_scan_mask_set(struct iio_ring_buffer *ring, int bit)
328{
329 struct iio_dev *dev_info = ring->indio_dev;
330 u32 mask;
331 u32 trialmask = ring->scan_mask | (1 << bit);
332
333 if (bit > IIO_MAX_SCAN_LENGTH)
334 return -EINVAL;
335 if (dev_info->available_scan_masks) {
336 mask = iio_scan_mask_match(dev_info->available_scan_masks,
337 trialmask);
338 if (!mask)
339 return -EINVAL;
340 }
341 ring->scan_mask = trialmask;
342 ring->scan_count++;
343
344 return 0;
345};
346
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100347/**
348 * iio_scan_mask_clear() - clear a particular element from the scan mask
349 * @ring: the ring buffer whose scan mask we are interested in
350 * @bit: the bit to clear
351 **/
Manuel Stahlbf329632010-08-31 11:32:52 +0200352static inline int iio_scan_mask_clear(struct iio_ring_buffer *ring, int bit)
353{
354 if (bit > IIO_MAX_SCAN_LENGTH)
355 return -EINVAL;
356 ring->scan_mask &= ~(1 << bit);
357 ring->scan_count--;
358 return 0;
359};
360
361/**
362 * iio_scan_mask_count_to_right() - how many scan elements occur before here
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100363 * @ring: the ring buffer whose scan mask we interested in
Manuel Stahlbf329632010-08-31 11:32:52 +0200364 * @bit: which number scan element is this
365 **/
366static inline int iio_scan_mask_count_to_right(struct iio_ring_buffer *ring,
367 int bit)
368{
369 int count = 0;
370 int mask = (1 << bit);
371 if (bit > IIO_MAX_SCAN_LENGTH)
372 return -EINVAL;
373 while (mask) {
374 mask >>= 1;
375 if (mask & ring->scan_mask)
376 count++;
377 }
378
379 return count;
380}
381
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100382/**
383 * iio_put_ring_buffer() - notify done with buffer
384 * @ring: the buffer we are done with.
385 **/
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100386static inline void iio_put_ring_buffer(struct iio_ring_buffer *ring)
387{
388 put_device(&ring->dev);
389};
390
391#define to_iio_ring_buffer(d) \
392 container_of(d, struct iio_ring_buffer, dev)
393#define access_dev_to_iio_ring_buffer(d) \
394 container_of(d, struct iio_ring_buffer, access_dev)
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100395
396/**
397 * iio_ring_buffer_register() - register the buffer with IIO core
398 * @ring: the buffer to be registered
399 * @id: the id of the buffer (typically 0)
400 **/
Jonathan Cameron758d9882010-05-04 14:43:08 +0100401int iio_ring_buffer_register(struct iio_ring_buffer *ring, int id);
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100402
403/**
404 * iio_ring_buffer_unregister() - unregister the buffer from IIO core
405 * @ring: the buffer to be unregistered
406 **/
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100407void iio_ring_buffer_unregister(struct iio_ring_buffer *ring);
408
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100409/**
410 * iio_read_ring_length() - attr func to get number of datums in the buffer
411 **/
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100412ssize_t iio_read_ring_length(struct device *dev,
413 struct device_attribute *attr,
414 char *buf);
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100415/**
416 * iio_write_ring_length() - attr func to set number of datums in the buffer
417 **/
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100418ssize_t iio_write_ring_length(struct device *dev,
419 struct device_attribute *attr,
420 const char *buf,
421 size_t len);
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100422/**
423 * iio_read_ring_bytes_per_datum() - attr for number of bytes in whole datum
424 **/
Manuel Stahlffcab072010-08-31 11:32:50 +0200425ssize_t iio_read_ring_bytes_per_datum(struct device *dev,
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100426 struct device_attribute *attr,
427 char *buf);
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100428/**
429 * iio_store_ring_enable() - attr to turn the buffer on
430 **/
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100431ssize_t iio_store_ring_enable(struct device *dev,
432 struct device_attribute *attr,
433 const char *buf,
434 size_t len);
Jonathan Cameronc3e5d412010-09-04 17:54:45 +0100435/**
436 * iio_show_ring_enable() - attr to see if the buffer is on
437 **/
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100438ssize_t iio_show_ring_enable(struct device *dev,
439 struct device_attribute *attr,
440 char *buf);
441#define IIO_RING_LENGTH_ATTR DEVICE_ATTR(length, S_IRUGO | S_IWUSR, \
442 iio_read_ring_length, \
443 iio_write_ring_length)
Manuel Stahlffcab072010-08-31 11:32:50 +0200444#define IIO_RING_BYTES_PER_DATUM_ATTR DEVICE_ATTR(bytes_per_datum, S_IRUGO | S_IWUSR, \
445 iio_read_ring_bytes_per_datum, NULL)
446#define IIO_RING_ENABLE_ATTR DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, \
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100447 iio_show_ring_enable, \
448 iio_store_ring_enable)
Jonathan Cameron26620512010-07-11 16:39:14 +0100449#else /* CONFIG_IIO_RING_BUFFER */
450static inline int iio_ring_buffer_register(struct iio_ring_buffer *ring, int id)
451{
452 return 0;
453};
454static inline void iio_ring_buffer_unregister(struct iio_ring_buffer *ring)
455{};
456
457#endif /* CONFIG_IIO_RING_BUFFER */
Jonathan Cameron7026ea42009-08-18 18:06:24 +0100458
459#endif /* _IIO_RING_GENERIC_H_ */