xref: /linux-6.15/include/linux/iio/buffer.h (revision 217a5cf0)
1 /* The industrial I/O core - generic 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_BUFFER_GENERIC_H_
11 #define _IIO_BUFFER_GENERIC_H_
12 #include <linux/sysfs.h>
13 #include <linux/iio/iio.h>
14 #include <linux/kref.h>
15 
16 #ifdef CONFIG_IIO_BUFFER
17 
18 struct iio_buffer;
19 
20 /**
21  * struct iio_buffer_access_funcs - access functions for buffers.
22  * @store_to:		actually store stuff to the buffer
23  * @read_first_n:	try to get a specified number of bytes (must exist)
24  * @data_available:	indicates whether data for reading from the buffer is
25  *			available.
26  * @request_update:	if a parameter change has been marked, update underlying
27  *			storage.
28  * @get_bytes_per_datum:get current bytes per datum
29  * @set_bytes_per_datum:set number of bytes per datum
30  * @get_length:		get number of datums in buffer
31  * @set_length:		set number of datums in buffer
32  * @release:		called when the last reference to the buffer is dropped,
33  *			should free all resources allocated by the buffer.
34  *
35  * The purpose of this structure is to make the buffer element
36  * modular as event for a given driver, different usecases may require
37  * different buffer designs (space efficiency vs speed for example).
38  *
39  * It is worth noting that a given buffer implementation may only support a
40  * small proportion of these functions.  The core code 'should' cope fine with
41  * any of them not existing.
42  **/
43 struct iio_buffer_access_funcs {
44 	int (*store_to)(struct iio_buffer *buffer, const void *data);
45 	int (*read_first_n)(struct iio_buffer *buffer,
46 			    size_t n,
47 			    char __user *buf);
48 	bool (*data_available)(struct iio_buffer *buffer);
49 
50 	int (*request_update)(struct iio_buffer *buffer);
51 
52 	int (*get_bytes_per_datum)(struct iio_buffer *buffer);
53 	int (*set_bytes_per_datum)(struct iio_buffer *buffer, size_t bpd);
54 	int (*get_length)(struct iio_buffer *buffer);
55 	int (*set_length)(struct iio_buffer *buffer, int length);
56 
57 	void (*release)(struct iio_buffer *buffer);
58 };
59 
60 /**
61  * struct iio_buffer - general buffer structure
62  * @length:		[DEVICE] number of datums in buffer
63  * @bytes_per_datum:	[DEVICE] size of individual datum including timestamp
64  * @scan_el_attrs:	[DRIVER] control of scan elements if that scan mode
65  *			control method is used
66  * @scan_mask:		[INTERN] bitmask used in masking scan mode elements
67  * @scan_timestamp:	[INTERN] does the scan mode include a timestamp
68  * @access:		[DRIVER] buffer access functions associated with the
69  *			implementation.
70  * @scan_el_dev_attr_list:[INTERN] list of scan element related attributes.
71  * @scan_el_group:	[DRIVER] attribute group for those attributes not
72  *			created from the iio_chan_info array.
73  * @pollq:		[INTERN] wait queue to allow for polling on the buffer.
74  * @stufftoread:	[INTERN] flag to indicate new data.
75  * @demux_list:		[INTERN] list of operations required to demux the scan.
76  * @demux_bounce:	[INTERN] buffer for doing gather from incoming scan.
77  * @buffer_list:	[INTERN] entry in the devices list of current buffers.
78  * @ref:		[INTERN] reference count of the buffer.
79  */
80 struct iio_buffer {
81 	int					length;
82 	int					bytes_per_datum;
83 	struct attribute_group			*scan_el_attrs;
84 	long					*scan_mask;
85 	bool					scan_timestamp;
86 	const struct iio_buffer_access_funcs	*access;
87 	struct list_head			scan_el_dev_attr_list;
88 	struct attribute_group			scan_el_group;
89 	wait_queue_head_t			pollq;
90 	bool					stufftoread;
91 	const struct attribute_group *attrs;
92 	struct list_head			demux_list;
93 	void					*demux_bounce;
94 	struct list_head			buffer_list;
95 	struct kref				ref;
96 };
97 
98 /**
99  * iio_update_buffers() - add or remove buffer from active list
100  * @indio_dev:		device to add buffer to
101  * @insert_buffer:	buffer to insert
102  * @remove_buffer:	buffer_to_remove
103  *
104  * Note this will tear down the all buffering and build it up again
105  */
106 int iio_update_buffers(struct iio_dev *indio_dev,
107 		       struct iio_buffer *insert_buffer,
108 		       struct iio_buffer *remove_buffer);
109 
110 /**
111  * iio_buffer_init() - Initialize the buffer structure
112  * @buffer:		buffer to be initialized
113  **/
114 void iio_buffer_init(struct iio_buffer *buffer);
115 
116 int iio_scan_mask_query(struct iio_dev *indio_dev,
117 			struct iio_buffer *buffer, int bit);
118 
119 /**
120  * iio_push_to_buffers() - push to a registered buffer.
121  * @indio_dev:		iio_dev structure for device.
122  * @data:		Full scan.
123  */
124 int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data);
125 
126 /*
127  * iio_push_to_buffers_with_timestamp() - push data and timestamp to buffers
128  * @indio_dev:		iio_dev structure for device.
129  * @data:		sample data
130  * @timestamp:		timestamp for the sample data
131  *
132  * Pushes data to the IIO device's buffers. If timestamps are enabled for the
133  * device the function will store the supplied timestamp as the last element in
134  * the sample data buffer before pushing it to the device buffers. The sample
135  * data buffer needs to be large enough to hold the additional timestamp
136  * (usually the buffer should be indio->scan_bytes bytes large).
137  *
138  * Returns 0 on success, a negative error code otherwise.
139  */
140 static inline int iio_push_to_buffers_with_timestamp(struct iio_dev *indio_dev,
141 	void *data, int64_t timestamp)
142 {
143 	if (indio_dev->scan_timestamp) {
144 		size_t ts_offset = indio_dev->scan_bytes / sizeof(int64_t) - 1;
145 		((int64_t *)data)[ts_offset] = timestamp;
146 	}
147 
148 	return iio_push_to_buffers(indio_dev, data);
149 }
150 
151 int iio_update_demux(struct iio_dev *indio_dev);
152 
153 /**
154  * iio_buffer_register() - register the buffer with IIO core
155  * @indio_dev:		device with the buffer to be registered
156  * @channels:		the channel descriptions used to construct buffer
157  * @num_channels:	the number of channels
158  **/
159 int iio_buffer_register(struct iio_dev *indio_dev,
160 			const struct iio_chan_spec *channels,
161 			int num_channels);
162 
163 /**
164  * iio_buffer_unregister() - unregister the buffer from IIO core
165  * @indio_dev:		the device with the buffer to be unregistered
166  **/
167 void iio_buffer_unregister(struct iio_dev *indio_dev);
168 
169 /**
170  * iio_buffer_read_length() - attr func to get number of datums in the buffer
171  **/
172 ssize_t iio_buffer_read_length(struct device *dev,
173 			       struct device_attribute *attr,
174 			       char *buf);
175 /**
176  * iio_buffer_write_length() - attr func to set number of datums in the buffer
177  **/
178 ssize_t iio_buffer_write_length(struct device *dev,
179 			      struct device_attribute *attr,
180 			      const char *buf,
181 			      size_t len);
182 /**
183  * iio_buffer_store_enable() - attr to turn the buffer on
184  **/
185 ssize_t iio_buffer_store_enable(struct device *dev,
186 				struct device_attribute *attr,
187 				const char *buf,
188 				size_t len);
189 /**
190  * iio_buffer_show_enable() - attr to see if the buffer is on
191  **/
192 ssize_t iio_buffer_show_enable(struct device *dev,
193 			       struct device_attribute *attr,
194 			       char *buf);
195 #define IIO_BUFFER_LENGTH_ATTR DEVICE_ATTR(length, S_IRUGO | S_IWUSR,	\
196 					   iio_buffer_read_length,	\
197 					   iio_buffer_write_length)
198 
199 #define IIO_BUFFER_ENABLE_ATTR DEVICE_ATTR(enable, S_IRUGO | S_IWUSR,	\
200 					   iio_buffer_show_enable,	\
201 					   iio_buffer_store_enable)
202 
203 bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev,
204 	const unsigned long *mask);
205 
206 struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer);
207 void iio_buffer_put(struct iio_buffer *buffer);
208 
209 /**
210  * iio_device_attach_buffer - Attach a buffer to a IIO device
211  * @indio_dev: The device the buffer should be attached to
212  * @buffer: The buffer to attach to the device
213  *
214  * This function attaches a buffer to a IIO device. The buffer stays attached to
215  * the device until the device is freed. The function should only be called at
216  * most once per device.
217  */
218 static inline void iio_device_attach_buffer(struct iio_dev *indio_dev,
219 	struct iio_buffer *buffer)
220 {
221 	indio_dev->buffer = iio_buffer_get(buffer);
222 }
223 
224 #else /* CONFIG_IIO_BUFFER */
225 
226 static inline int iio_buffer_register(struct iio_dev *indio_dev,
227 					   const struct iio_chan_spec *channels,
228 					   int num_channels)
229 {
230 	return 0;
231 }
232 
233 static inline void iio_buffer_unregister(struct iio_dev *indio_dev)
234 {}
235 
236 static inline void iio_buffer_get(struct iio_buffer *buffer) {}
237 static inline void iio_buffer_put(struct iio_buffer *buffer) {}
238 
239 #endif /* CONFIG_IIO_BUFFER */
240 
241 #endif /* _IIO_BUFFER_GENERIC_H_ */
242