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
17 struct iio_ring_buffer;
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
26 int iio_push_ring_event(struct iio_ring_buffer *ring_buf,
30 * 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
35 * Typical usecase is to escalate a 50% ring full to 75% full if noone has yet
36 * read the first event. Clearly the 50% full is no longer of interest in
39 int iio_push_or_escallate_ring_event(struct iio_ring_buffer *ring_buf,
44 * struct iio_ring_access_funcs - access functions for ring buffers.
45 * @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
53 * @request_update: if a parameter change has been marked, update underlying
55 * @get_bytes_per_datum: get current bytes per datum
56 * @set_bytes_per_datum: 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
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
64 * different ring designs (space efficiency vs speed for example).
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
70 struct iio_ring_access_funcs {
71 void (*mark_in_use)(struct iio_ring_buffer *ring);
72 void (*unmark_in_use)(struct iio_ring_buffer *ring);
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,
81 int (*mark_param_change)(struct iio_ring_buffer *ring);
82 int (*request_update)(struct iio_ring_buffer *ring);
84 int (*get_bytes_per_datum)(struct iio_ring_buffer *ring);
85 int (*set_bytes_per_datum)(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);
89 int (*is_enabled)(struct iio_ring_buffer *ring);
90 int (*enable)(struct iio_ring_buffer *ring);
94 * struct iio_ring_buffer - general ring buffer structure
95 * @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 * @bytes_per_datum [DEVICE] size of individual datum including timestamp
103 * @bpe: [DEVICE] size of individual channel value
104 * @loopcount: [INTERN] number of times the ring has looped
105 * @scan_el_attrs: [DRIVER] control of scan elements if that scan mode
106 * control method is used
107 * @scan_count: [INTERN] the number of elements in the current scan mode
108 * @scan_mask: [INTERN] bitmask used in masking scan mode elements
109 * @scan_timestamp: [INTERN] does the scan mode include a timestamp
110 * @access_handler: [INTERN] chrdev access handling
111 * @ev_int: [INTERN] chrdev interface for the event chrdev
112 * @shared_ev_pointer: [INTERN] the shared event pointer to allow escalation of
114 * @access: [DRIVER] ring access functions associated with the
116 * @preenable: [DRIVER] function to run prior to marking ring enabled
117 * @postenable: [DRIVER] function to run after marking ring enabled
118 * @predisable: [DRIVER] function to run prior to marking ring disabled
119 * @postdisable: [DRIVER] function to run after marking ring disabled
121 struct iio_ring_buffer {
123 struct device access_dev;
124 struct iio_dev *indio_dev;
125 struct module *owner;
132 struct attribute_group *scan_el_attrs;
136 struct iio_handler access_handler;
137 struct iio_event_interface ev_int;
138 struct iio_shared_ev_pointer shared_ev_pointer;
139 struct iio_ring_access_funcs access;
140 int (*preenable)(struct iio_dev *);
141 int (*postenable)(struct iio_dev *);
142 int (*predisable)(struct iio_dev *);
143 int (*postdisable)(struct iio_dev *);
146 void iio_ring_buffer_init(struct iio_ring_buffer *ring,
147 struct iio_dev *dev_info);
150 * __iio_update_ring_buffer() - update common elements of ring buffers
151 * @ring: ring buffer that is the event source
152 * @bytes_per_datum: size of individual datum including timestamp
153 * @length: number of datums in ring
155 static inline void __iio_update_ring_buffer(struct iio_ring_buffer *ring,
156 int bytes_per_datum, int length)
158 ring->bytes_per_datum = bytes_per_datum;
159 ring->length = length;
164 * struct iio_scan_el - an individual element of a scan
165 * @dev_attr: control attribute (if directly controllable)
166 * @number: unique identifier of element (used for bit mask)
167 * @bit_count: number of bits in scan element
168 * @label: useful data for the scan el (often reg address)
169 * @set_state: for some devices datardy signals are generated
170 * for any enabled lines. This allows unwanted lines
171 * to be disabled and hence not get in the way.
174 struct device_attribute dev_attr;
179 int (*set_state)(struct iio_scan_el *scanel,
180 struct iio_dev *dev_info,
184 #define to_iio_scan_el(_dev_attr) \
185 container_of(_dev_attr, struct iio_scan_el, dev_attr);
188 * iio_scan_el_store() - sysfs scan element selection interface
189 * @dev: the target device
190 * @attr: the device attribute that is being processed
191 * @buf: input from userspace
192 * @len: length of input
194 * A generic function used to enable various scan elements. In some
195 * devices explicit read commands for each channel mean this is merely
196 * a software switch. In others this must actively disable the channel.
197 * Complexities occur when this interacts with data ready type triggers
198 * which may not reset unless every channel that is enabled is explicitly
201 ssize_t iio_scan_el_store(struct device *dev, struct device_attribute *attr,
202 const char *buf, size_t len);
204 * iio_scal_el_show() - sysfs interface to query whether a scan element is
206 * @dev: the target device
207 * @attr: the device attribute that is being processed
208 * @buf: output buffer
210 ssize_t iio_scan_el_show(struct device *dev, struct device_attribute *attr,
213 ssize_t iio_scan_el_ts_store(struct device *dev, struct device_attribute *attr,
214 const char *buf, size_t len);
216 ssize_t iio_scan_el_ts_show(struct device *dev, struct device_attribute *attr,
219 * IIO_SCAN_EL_C - declare and initialize a scan element with a control func
221 * @_name: identifying name. Resulting struct is iio_scan_el_##_name,
222 * sysfs element, _name##_en.
223 * @_number: unique id number for the scan element.
224 * @_bits: number of bits in the scan element result (used in mixed bit
226 * @_label: indentification variable used by drivers. Often a reg address.
227 * @_controlfunc: function used to notify hardware of whether state changes
229 #define __IIO_SCAN_EL_C(_name, _number, _bits, _label, _controlfunc) \
230 struct iio_scan_el iio_scan_el_##_name = { \
231 .dev_attr = __ATTR(_number##_##_name##_en, \
234 iio_scan_el_store), \
236 .bit_count = _bits, \
238 .set_state = _controlfunc, \
241 #define IIO_SCAN_EL_C(_name, _number, _bits, _label, _controlfunc) \
242 __IIO_SCAN_EL_C(_name, _number, _bits, _label, _controlfunc)
244 #define __IIO_SCAN_NAMED_EL_C(_name, _string, _number, _bits, _label, _cf) \
245 struct iio_scan_el iio_scan_el_##_name = { \
246 .dev_attr = __ATTR(_number##_##_string##_en, \
249 iio_scan_el_store), \
251 .bit_count = _bits, \
255 #define IIO_SCAN_NAMED_EL_C(_name, _string, _number, _bits, _label, _cf) \
256 __IIO_SCAN_NAMED_EL_C(_name, _string, _number, _bits, _label, _cf)
258 * IIO_SCAN_EL_TIMESTAMP - declare a special scan element for timestamps
260 * Odd one out. Handled slightly differently from other scan elements.
262 #define IIO_SCAN_EL_TIMESTAMP(number) \
263 struct iio_scan_el iio_scan_el_timestamp = { \
264 .dev_attr = __ATTR(number##_timestamp_en, \
266 iio_scan_el_ts_show, \
267 iio_scan_el_ts_store), \
271 * These are mainly provided to allow for a change of implementation if a device
272 * has a large number of scan elements
274 #define IIO_MAX_SCAN_LENGTH 31
276 /* note 0 used as error indicator as it doesn't make sense. */
277 static inline u32 iio_scan_mask_match(u32 *av_masks, u32 mask)
280 if (!(~*av_masks & mask))
287 static inline int iio_scan_mask_query(struct iio_ring_buffer *ring, int bit)
289 struct iio_dev *dev_info = ring->indio_dev;
292 if (bit > IIO_MAX_SCAN_LENGTH)
295 if (!ring->scan_mask)
298 if (dev_info->available_scan_masks)
299 mask = iio_scan_mask_match(dev_info->available_scan_masks,
302 mask = ring->scan_mask;
307 return !!(mask & (1 << bit));
310 static inline int iio_scan_mask_set(struct iio_ring_buffer *ring, int bit)
312 struct iio_dev *dev_info = ring->indio_dev;
314 u32 trialmask = ring->scan_mask | (1 << bit);
316 if (bit > IIO_MAX_SCAN_LENGTH)
318 if (dev_info->available_scan_masks) {
319 mask = iio_scan_mask_match(dev_info->available_scan_masks,
324 ring->scan_mask = trialmask;
330 static inline int iio_scan_mask_clear(struct iio_ring_buffer *ring, int bit)
332 if (bit > IIO_MAX_SCAN_LENGTH)
334 ring->scan_mask &= ~(1 << bit);
340 * iio_scan_mask_count_to_right() - how many scan elements occur before here
341 * @dev_info: the iio_device whose scan mode we are querying
342 * @bit: which number scan element is this
344 static inline int iio_scan_mask_count_to_right(struct iio_ring_buffer *ring,
348 int mask = (1 << bit);
349 if (bit > IIO_MAX_SCAN_LENGTH)
353 if (mask & ring->scan_mask)
361 static inline void iio_put_ring_buffer(struct iio_ring_buffer *ring)
363 put_device(&ring->dev);
366 #define to_iio_ring_buffer(d) \
367 container_of(d, struct iio_ring_buffer, dev)
368 #define access_dev_to_iio_ring_buffer(d) \
369 container_of(d, struct iio_ring_buffer, access_dev)
370 int iio_ring_buffer_register(struct iio_ring_buffer *ring, int id);
371 void iio_ring_buffer_unregister(struct iio_ring_buffer *ring);
373 ssize_t iio_read_ring_length(struct device *dev,
374 struct device_attribute *attr,
376 ssize_t iio_write_ring_length(struct device *dev,
377 struct device_attribute *attr,
380 ssize_t iio_read_ring_bytes_per_datum(struct device *dev,
381 struct device_attribute *attr,
383 ssize_t iio_store_ring_enable(struct device *dev,
384 struct device_attribute *attr,
387 ssize_t iio_show_ring_enable(struct device *dev,
388 struct device_attribute *attr,
390 #define IIO_RING_LENGTH_ATTR DEVICE_ATTR(length, S_IRUGO | S_IWUSR, \
391 iio_read_ring_length, \
392 iio_write_ring_length)
393 #define IIO_RING_BYTES_PER_DATUM_ATTR DEVICE_ATTR(bytes_per_datum, S_IRUGO | S_IWUSR, \
394 iio_read_ring_bytes_per_datum, NULL)
395 #define IIO_RING_ENABLE_ATTR DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, \
396 iio_show_ring_enable, \
397 iio_store_ring_enable)
398 #else /* CONFIG_IIO_RING_BUFFER */
399 static inline int iio_ring_buffer_register(struct iio_ring_buffer *ring, int id)
403 static inline void iio_ring_buffer_unregister(struct iio_ring_buffer *ring)
406 #endif /* CONFIG_IIO_RING_BUFFER */
408 #endif /* _IIO_RING_GENERIC_H_ */