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_
15 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() - escallate or add as appropriate
30 * Typical usecase is to escallate a 50% ring full to 75% full if noone has yet
31 * read the first event. Clearly the 50% full is no longer of interest in
34 int iio_push_or_escallate_ring_event(struct iio_ring_buffer
*ring_buf
,
39 * struct iio_ring_access_funcs - access functions for ring buffers.
40 * @create: perform allocation
41 * @init: get ring buffer ready for use
42 * @_exit: reverse steps in init
43 * @_free: deallocate ring buffer
44 * @mark_in_use: reference counting, typically to prevent module removal
45 * @unmark_in_use: reduce reference count when no longer using ring buffer
46 * @store_to: actually store stuff to the ring buffer
47 * @read_last: get the last element stored
48 * @rip_lots: try to get a specified number of elements (must exist)
49 * @mark_param_change: notify ring that some relevant parameter has changed
50 * Often this means the underlying storage may need to
52 * @request_update: if a parameter change has been marked, update underlying
54 * @get_bpd: get current bytes per datum
55 * @set_bpd: set number of bytes per datum
56 * @get_length: get number of datums in ring
57 * @set_length: set number of datums in ring
58 * @is_enabled: query if ring is currently being used
59 * @enable: enable the ring
61 * The purpose of this structure is to make the ring buffer element
62 * modular as event for a given driver, different usecases may require
63 * different ring designs (space efficiency vs speed for example.
65 * It is worth noting that a given ring implementation may only support a small
66 * proportion of these functions. The core code 'should' cope fine with any of
69 struct iio_ring_access_funcs
{
70 void (*mark_in_use
)(struct iio_ring_buffer
*ring
);
71 void (*unmark_in_use
)(struct iio_ring_buffer
*ring
);
73 int (*store_to
)(struct iio_ring_buffer
*ring
, u8
*data
, s64 timestamp
);
74 int (*read_last
)(struct iio_ring_buffer
*ring
, u8
*data
);
75 int (*rip_lots
)(struct iio_ring_buffer
*ring
,
80 int (*mark_param_change
)(struct iio_ring_buffer
*ring
);
81 int (*request_update
)(struct iio_ring_buffer
*ring
);
83 int (*get_bpd
)(struct iio_ring_buffer
*ring
);
84 int (*set_bpd
)(struct iio_ring_buffer
*ring
, size_t bpd
);
85 int (*get_length
)(struct iio_ring_buffer
*ring
);
86 int (*set_length
)(struct iio_ring_buffer
*ring
, int length
);
88 int (*is_enabled
)(struct iio_ring_buffer
*ring
);
89 int (*enable
)(struct iio_ring_buffer
*ring
);
93 * struct iio_ring_buffer - general ring buffer structure
94 * @length: [DEVICE]number of datums in ring
95 * @bpd: [DEVICE]size of individual datum including timestamp
96 * @loopcount: [INTERN]number of times the ring has looped
97 * @access_minor_name: [INTERN]store of name of the access chrdev minor number
99 * @access_handler: [INTERN]chrdev access handling
100 * @event_minor_name: [INTERN]store of name of the event chrdev minor number
102 * @ev_int: [INTERN]chrdev interface for the event chrdev
103 * @shared_ev_pointer: [INTERN]the shared event pointer to allow escalation of
105 * @ring_access: [DRIVER]ring access functions associated with the
107 * @ring_prenable: [DRIVER] function to run prior to marking ring enabled
108 * @ring_postenable: [DRIVER] function to run after marking ring enabled
109 * @ring_predisable: [DRIVER] function to run prior to marking ring disabled
110 * @ring_postdisable: [DRIVER] function to run after marking ring disabled
112 struct iio_ring_buffer
{
114 struct device access_dev
;
115 struct iio_dev
*indio_dev
;
116 struct module
*owner
;
122 struct iio_handler access_handler
;
123 struct iio_event_interface ev_int
;
124 struct iio_shared_ev_pointer shared_ev_pointer
;
125 struct iio_ring_access_funcs access
;
126 int (*preenable
)(struct iio_dev
*);
127 int (*postenable
)(struct iio_dev
*);
128 int (*predisable
)(struct iio_dev
*);
129 int (*postdisable
)(struct iio_dev
*);
132 void iio_ring_buffer_init(struct iio_ring_buffer
*ring
,
133 struct iio_dev
*dev_info
);
136 * __iio_init_ring_buffer() - initialize common elements of ring buffers.
138 static inline void __iio_init_ring_buffer(struct iio_ring_buffer
*ring
,
139 int bytes_per_datum
, int length
)
141 ring
->bpd
= bytes_per_datum
;
142 ring
->length
= length
;
144 ring
->shared_ev_pointer
.ev_p
= 0;
145 ring
->shared_ev_pointer
.lock
=
146 __SPIN_LOCK_UNLOCKED(ring
->shared_ev_pointer
->loc
);
150 * struct iio_scan_el - an individual element of a scan
151 * @dev_attr: control attribute (if directly controllable)
152 * @number: unique identifier of element (used for bit mask)
153 * @bit_count: number of bits in scan element
154 * @label: useful data for the scan el (often reg address)
155 * @set_state: for some devices datardy signals are generated
156 * for any enabled lines. This allows unwanted lines
157 * to be disabled and hence not get in the way.
160 struct device_attribute dev_attr
;
165 int (*set_state
)(struct iio_scan_el
*scanel
,
166 struct iio_dev
*dev_info
,
170 #define to_iio_scan_el(_dev_attr) \
171 container_of(_dev_attr, struct iio_scan_el, dev_attr);
174 * iio_scan_el_store() - sysfs scan element selection interface.
176 * A generic function used to enable various scan elements. In some
177 * devices explicit read commands for each channel mean this is merely
178 * a software switch. In others this must actively disable the channel.
179 * Complexities occur when this interacts with data ready type triggers
180 * which may not reset unless every channel that is enabled is explicitly
183 ssize_t
iio_scan_el_store(struct device
*dev
, struct device_attribute
*attr
,
184 const char *buf
, size_t len
);
186 * iio_scal_el_show() - sysfs interface to query whether a scan element is
189 ssize_t
iio_scan_el_show(struct device
*dev
, struct device_attribute
*attr
,
192 * IIO_SCAN_EL: - declare and initialize a scan element without control func
193 * @_name: identifying name. Resulting struct is iio_scan_el_##_name,
194 * sysfs element, scan_en_##_name.
195 * @_number: unique id number for the scan element.
196 * @_bits: number of bits in the scan element result (used in mixed bit
198 * @_label: indentification variable used by drivers. Often a reg address.
200 #define IIO_SCAN_EL(_name, _number, _bits, _label) \
201 struct iio_scan_el iio_scan_el_##_name = { \
202 .dev_attr = __ATTR(scan_en_##_name, \
205 iio_scan_el_store), \
206 .mask = (1 << _number), \
207 .bit_count = _bits, \
211 ssize_t
iio_scan_el_ts_store(struct device
*dev
, struct device_attribute
*attr
,
212 const char *buf
, size_t len
);
214 ssize_t
iio_scan_el_ts_show(struct device
*dev
, struct device_attribute
*attr
,
217 * IIO_SCAN_EL_C: - declare and initialize a scan element with a control func
219 * @_controlfunc: function used to notify hardware of whether state changes
221 #define IIO_SCAN_EL_C(_name, _number, _bits, _label, _controlfunc) \
222 struct iio_scan_el iio_scan_el_##_name = { \
223 .dev_attr = __ATTR(scan_en_##_name, \
226 iio_scan_el_store), \
228 .bit_count = _bits, \
230 .set_state = _controlfunc, \
233 * IIO_SCAN_EL_TIMESTAMP: - declare a special scan element for timestamps
235 * Odd one out. Handled slightly differently from other scan elements.
237 #define IIO_SCAN_EL_TIMESTAMP \
238 struct iio_scan_el iio_scan_el_timestamp = { \
239 .dev_attr = __ATTR(scan_en_timestamp, \
241 iio_scan_el_ts_show, \
242 iio_scan_el_ts_store), \
245 static inline void iio_put_ring_buffer(struct iio_ring_buffer
*ring
)
247 put_device(&ring
->dev
);
250 #define to_iio_ring_buffer(d) \
251 container_of(d, struct iio_ring_buffer, dev)
252 #define access_dev_to_iio_ring_buffer(d) \
253 container_of(d, struct iio_ring_buffer, access_dev)
254 int iio_ring_buffer_register(struct iio_ring_buffer
*ring
);
255 void iio_ring_buffer_unregister(struct iio_ring_buffer
*ring
);
257 ssize_t
iio_read_ring_length(struct device
*dev
,
258 struct device_attribute
*attr
,
260 ssize_t
iio_write_ring_length(struct device
*dev
,
261 struct device_attribute
*attr
,
264 ssize_t
iio_read_ring_bps(struct device
*dev
,
265 struct device_attribute
*attr
,
267 ssize_t
iio_store_ring_enable(struct device
*dev
,
268 struct device_attribute
*attr
,
271 ssize_t
iio_show_ring_enable(struct device
*dev
,
272 struct device_attribute
*attr
,
274 #define IIO_RING_LENGTH_ATTR DEVICE_ATTR(length, S_IRUGO | S_IWUSR, \
275 iio_read_ring_length, \
276 iio_write_ring_length)
277 #define IIO_RING_BPS_ATTR DEVICE_ATTR(bps, S_IRUGO | S_IWUSR, \
278 iio_read_ring_bps, NULL)
279 #define IIO_RING_ENABLE_ATTR DEVICE_ATTR(ring_enable, S_IRUGO | S_IWUSR, \
280 iio_show_ring_enable, \
281 iio_store_ring_enable)
283 #endif /* _IIO_RING_GENERIC_H_ */