1 /* The industrial I/O core - generic ring buffer interfaces.
3 * Copyright (c) 2008 Jonathan Cameron
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.
10 #ifndef _IIO_RING_GENERIC_H_
11 #define _IIO_RING_GENERIC_H_
14 #ifdef CONFIG_IIO_RING_BUFFER
16 struct iio_ring_buffer;
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
24 int iio_push_ring_event(struct iio_ring_buffer *ring_buf,
28 * 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
33 * Typical usecase is to escalate a 50% ring full to 75% full if noone has yet
34 * read the first event. Clearly the 50% full is no longer of interest in
37 int iio_push_or_escallate_ring_event(struct iio_ring_buffer *ring_buf,
42 * struct iio_ring_access_funcs - access functions for ring buffers.
43 * @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
51 * @request_update: if a parameter change has been marked, update underlying
53 * @get_bytes_per_datum:get current bytes per datum
54 * @set_bytes_per_datum:set number of bytes per datum
55 * @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
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
62 * different ring designs (space efficiency vs speed for example).
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
68 struct iio_ring_access_funcs {
69 void (*mark_in_use)(struct iio_ring_buffer *ring);
70 void (*unmark_in_use)(struct iio_ring_buffer *ring);
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,
79 int (*mark_param_change)(struct iio_ring_buffer *ring);
80 int (*request_update)(struct iio_ring_buffer *ring);
82 int (*get_bytes_per_datum)(struct iio_ring_buffer *ring);
83 int (*set_bytes_per_datum)(struct iio_ring_buffer *ring, size_t bpd);
84 int (*get_length)(struct iio_ring_buffer *ring);
85 int (*set_length)(struct iio_ring_buffer *ring, int length);
87 int (*is_enabled)(struct iio_ring_buffer *ring);
88 int (*enable)(struct iio_ring_buffer *ring);
92 * struct iio_ring_buffer - general ring buffer structure
93 * @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
100 * @bytes_per_datum: [DEVICE] size of individual datum including timestamp
101 * @bpe: [DEVICE] size of individual channel value
102 * @loopcount: [INTERN] number of times the ring has looped
103 * @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
108 * @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
112 * @access: [DRIVER] ring access functions associated with the
114 * @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
119 struct iio_ring_buffer {
121 struct device access_dev;
122 struct iio_dev *indio_dev;
123 struct module *owner;
130 struct attribute_group *scan_el_attrs;
134 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 *);
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
150 void iio_ring_buffer_init(struct iio_ring_buffer *ring,
151 struct iio_dev *dev_info);
154 * __iio_update_ring_buffer() - update common elements of ring buffers
155 * @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
159 static inline void __iio_update_ring_buffer(struct iio_ring_buffer *ring,
160 int bytes_per_datum, int length)
162 ring->bytes_per_datum = bytes_per_datum;
163 ring->length = length;
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 * @label: useful data for the scan el (often reg address)
172 * @set_state: for some devices datardy signals are generated
173 * for any enabled lines. This allows unwanted lines
174 * to be disabled and hence not get in the way.
177 struct device_attribute dev_attr;
181 int (*set_state)(struct iio_scan_el *scanel,
182 struct iio_dev *dev_info,
186 #define to_iio_scan_el(_dev_attr) \
187 container_of(_dev_attr, struct iio_scan_el, dev_attr);
190 * iio_scan_el_store() - sysfs scan element selection interface
191 * @dev: the target device
192 * @attr: the device attribute that is being processed
193 * @buf: input from userspace
194 * @len: length of input
196 * A generic function used to enable various scan elements. In some
197 * devices explicit read commands for each channel mean this is merely
198 * a software switch. In others this must actively disable the channel.
199 * Complexities occur when this interacts with data ready type triggers
200 * which may not reset unless every channel that is enabled is explicitly
203 ssize_t iio_scan_el_store(struct device *dev, struct device_attribute *attr,
204 const char *buf, size_t len);
206 * iio_scan_el_show() - sysfs interface to query whether a scan element
208 * @dev: the target device
209 * @attr: the device attribute that is being processed
210 * @buf: output buffer
212 ssize_t iio_scan_el_show(struct device *dev, struct device_attribute *attr,
216 * iio_scan_el_ts_store() - sysfs interface to set whether a timestamp is included
219 ssize_t iio_scan_el_ts_store(struct device *dev, struct device_attribute *attr,
220 const char *buf, size_t len);
222 * iio_scan_el_ts_show() - sysfs interface to query if a timestamp is included
225 ssize_t iio_scan_el_ts_show(struct device *dev, struct device_attribute *attr,
228 * IIO_SCAN_EL_C - declare and initialize a scan element with a control func
230 * @_name: identifying name. Resulting struct is iio_scan_el_##_name,
231 * sysfs element, _name##_en.
232 * @_number: unique id number for the scan element.
234 * @_label: indentification variable used by drivers. Often a reg address.
235 * @_controlfunc: function used to notify hardware of whether state changes
237 #define __IIO_SCAN_EL_C(_name, _number, _label, _controlfunc) \
238 struct iio_scan_el iio_scan_el_##_name = { \
239 .dev_attr = __ATTR(_name##_en, \
242 iio_scan_el_store), \
245 .set_state = _controlfunc, \
247 static IIO_CONST_ATTR(_name##_index, #_number)
249 #define IIO_SCAN_EL_C(_name, _number, _label, _controlfunc) \
250 __IIO_SCAN_EL_C(_name, _number, _label, _controlfunc)
252 #define __IIO_SCAN_NAMED_EL_C(_name, _string, _number, _label, _cf) \
253 struct iio_scan_el iio_scan_el_##_name = { \
254 .dev_attr = __ATTR(_string##_en, \
257 iio_scan_el_store), \
262 static struct iio_const_attr iio_const_attr_##_name##_index = { \
263 .string = #_number, \
264 .dev_attr = __ATTR(_string##_index, \
265 S_IRUGO, iio_read_const_attr, NULL) \
269 #define IIO_SCAN_NAMED_EL_C(_name, _string, _number, _label, _cf) \
270 __IIO_SCAN_NAMED_EL_C(_name, _string, _number, _label, _cf)
272 * IIO_SCAN_EL_TIMESTAMP - declare a special scan element for timestamps
273 * @number: specify where in the scan order this is stored.
275 * Odd one out. Handled slightly differently from other scan elements.
277 #define IIO_SCAN_EL_TIMESTAMP(number) \
278 struct iio_scan_el iio_scan_el_timestamp = { \
279 .dev_attr = __ATTR(_timestamp_en, \
281 iio_scan_el_ts_show, \
282 iio_scan_el_ts_store), \
284 static IIO_CONST_ATTR(timestamp_index, #number)
287 * IIO_CONST_ATTR_SCAN_EL_TYPE - attr to specify the data format of a scan el
288 * @name: the scan el name (may be more general and cover a set of scan elements
289 * @_sign: either s or u for signed or unsigned
290 * @_bits: number of actual bits occuplied by the value
291 * @_storagebits: number of bits _bits is padded to when read out of buffer
293 #define IIO_CONST_ATTR_SCAN_EL_TYPE(_name, _sign, _bits, _storagebits) \
294 IIO_CONST_ATTR(_name##_type, #_sign#_bits"/"#_storagebits);
296 * These are mainly provided to allow for a change of implementation if a device
297 * has a large number of scan elements
299 #define IIO_MAX_SCAN_LENGTH 31
301 /* note 0 used as error indicator as it doesn't make sense. */
302 static inline u32 iio_scan_mask_match(u32 *av_masks, u32 mask)
305 if (!(~*av_masks & mask))
312 static inline int iio_scan_mask_query(struct iio_ring_buffer *ring, int bit)
314 struct iio_dev *dev_info = ring->indio_dev;
317 if (bit > IIO_MAX_SCAN_LENGTH)
320 if (!ring->scan_mask)
323 if (dev_info->available_scan_masks)
324 mask = iio_scan_mask_match(dev_info->available_scan_masks,
327 mask = ring->scan_mask;
332 return !!(mask & (1 << bit));
336 * iio_scan_mask_set() - set particular bit in the scan mask
337 * @ring: the ring buffer whose scan mask we are interested in
338 * @bit: the bit to be set.
340 static inline int iio_scan_mask_set(struct iio_ring_buffer *ring, int bit)
342 struct iio_dev *dev_info = ring->indio_dev;
344 u32 trialmask = ring->scan_mask | (1 << bit);
346 if (bit > IIO_MAX_SCAN_LENGTH)
348 if (dev_info->available_scan_masks) {
349 mask = iio_scan_mask_match(dev_info->available_scan_masks,
354 ring->scan_mask = trialmask;
361 * iio_scan_mask_clear() - clear a particular element from the scan mask
362 * @ring: the ring buffer whose scan mask we are interested in
363 * @bit: the bit to clear
365 static inline int iio_scan_mask_clear(struct iio_ring_buffer *ring, int bit)
367 if (bit > IIO_MAX_SCAN_LENGTH)
369 ring->scan_mask &= ~(1 << bit);
375 * iio_scan_mask_count_to_right() - how many scan elements occur before here
376 * @ring: the ring buffer whose scan mask we interested in
377 * @bit: which number scan element is this
379 static inline int iio_scan_mask_count_to_right(struct iio_ring_buffer *ring,
383 int mask = (1 << bit);
384 if (bit > IIO_MAX_SCAN_LENGTH)
388 if (mask & ring->scan_mask)
396 * iio_put_ring_buffer() - notify done with buffer
397 * @ring: the buffer we are done with.
399 static inline void iio_put_ring_buffer(struct iio_ring_buffer *ring)
401 put_device(&ring->dev);
404 #define to_iio_ring_buffer(d) \
405 container_of(d, struct iio_ring_buffer, dev)
406 #define access_dev_to_iio_ring_buffer(d) \
407 container_of(d, struct iio_ring_buffer, access_dev)
410 * iio_ring_buffer_register() - register the buffer with IIO core
411 * @ring: the buffer to be registered
412 * @id: the id of the buffer (typically 0)
414 int iio_ring_buffer_register(struct iio_ring_buffer *ring, int id);
417 * iio_ring_buffer_unregister() - unregister the buffer from IIO core
418 * @ring: the buffer to be unregistered
420 void iio_ring_buffer_unregister(struct iio_ring_buffer *ring);
423 * iio_read_ring_length() - attr func to get number of datums in the buffer
425 ssize_t iio_read_ring_length(struct device *dev,
426 struct device_attribute *attr,
429 * iio_write_ring_length() - attr func to set number of datums in the buffer
431 ssize_t iio_write_ring_length(struct device *dev,
432 struct device_attribute *attr,
436 * iio_read_ring_bytes_per_datum() - attr for number of bytes in whole datum
438 ssize_t iio_read_ring_bytes_per_datum(struct device *dev,
439 struct device_attribute *attr,
442 * iio_store_ring_enable() - attr to turn the buffer on
444 ssize_t iio_store_ring_enable(struct device *dev,
445 struct device_attribute *attr,
449 * iio_show_ring_enable() - attr to see if the buffer is on
451 ssize_t iio_show_ring_enable(struct device *dev,
452 struct device_attribute *attr,
454 #define IIO_RING_LENGTH_ATTR DEVICE_ATTR(length, S_IRUGO | S_IWUSR, \
455 iio_read_ring_length, \
456 iio_write_ring_length)
457 #define IIO_RING_BYTES_PER_DATUM_ATTR DEVICE_ATTR(bytes_per_datum, S_IRUGO | S_IWUSR, \
458 iio_read_ring_bytes_per_datum, NULL)
459 #define IIO_RING_ENABLE_ATTR DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, \
460 iio_show_ring_enable, \
461 iio_store_ring_enable)
462 #else /* CONFIG_IIO_RING_BUFFER */
463 static inline int iio_ring_buffer_register(struct iio_ring_buffer *ring, int id)
467 static inline void iio_ring_buffer_unregister(struct iio_ring_buffer *ring)
470 #endif /* CONFIG_IIO_RING_BUFFER */
472 #endif /* _IIO_RING_GENERIC_H_ */