xref: /linux/include/linux/iio/buffer_impl.h (revision 83bd89291f5cc866f60d32c34e268896c7ba8a3d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _IIO_BUFFER_GENERIC_IMPL_H_
3 #define _IIO_BUFFER_GENERIC_IMPL_H_
4 #include <linux/sysfs.h>
5 #include <linux/kref.h>
6 
7 #ifdef CONFIG_IIO_BUFFER
8 
9 #include <uapi/linux/iio/buffer.h>
10 #include <linux/iio/buffer.h>
11 
12 struct dma_buf_attachment;
13 struct dma_fence;
14 struct iio_dev;
15 struct iio_dma_buffer_block;
16 struct iio_buffer;
17 struct sg_table;
18 
19 /**
20  * INDIO_BUFFER_FLAG_FIXED_WATERMARK - Watermark level of the buffer can not be
21  *   configured. It has a fixed value which will be buffer specific.
22  */
23 #define INDIO_BUFFER_FLAG_FIXED_WATERMARK BIT(0)
24 
25 /**
26  * struct iio_buffer_access_funcs - access functions for buffers.
27  * @store_to:		actually store stuff to the buffer - must be safe to
28  *			call from any context (e.g. must not sleep).
29  * @read:		try to get a specified number of bytes (must exist)
30  * @data_available:	indicates how much data is available for reading from
31  *			the buffer.
32  * @remove_from:	remove scan from buffer. Drivers should calls this to
33  *			remove a scan from a buffer.
34  * @write:		try to write a number of bytes
35  * @space_available:	returns the amount of bytes available in a buffer
36  * @request_update:	if a parameter change has been marked, update underlying
37  *			storage.
38  * @set_bytes_per_datum:set number of bytes per datum
39  * @set_length:		set number of datums in buffer
40  * @enable:             called if the buffer is attached to a device and the
41  *                      device starts sampling. Calls are balanced with
42  *                      @disable.
43  * @disable:            called if the buffer is attached to a device and the
44  *                      device stops sampling. Calles are balanced with @enable.
45  * @release:		called when the last reference to the buffer is dropped,
46  *			should free all resources allocated by the buffer.
47  * @attach_dmabuf:	called from userspace via ioctl to attach one external
48  *			DMABUF.
49  * @detach_dmabuf:	called from userspace via ioctl to detach one previously
50  *			attached DMABUF.
51  * @enqueue_dmabuf:	called from userspace via ioctl to queue this DMABUF
52  *			object to this buffer. Requires a valid DMABUF fd, that
53  *			was previouly attached to this buffer.
54  * @get_dma_dev:	called to get the DMA channel associated with this buffer.
55  * @lock_queue:		called when the core needs to lock the buffer queue;
56  *                      it is used when enqueueing DMABUF objects.
57  * @unlock_queue:       used to unlock a previously locked buffer queue
58  * @modes:		Supported operating modes by this buffer type
59  * @flags:		A bitmask combination of INDIO_BUFFER_FLAG_*
60  *
61  * The purpose of this structure is to make the buffer element
62  * modular as event for a given driver, different usecases may require
63  * different buffer designs (space efficiency vs speed for example).
64  *
65  * It is worth noting that a given buffer implementation may only support a
66  * small proportion of these functions.  The core code 'should' cope fine with
67  * any of them not existing.
68  **/
69 struct iio_buffer_access_funcs {
70 	int (*store_to)(struct iio_buffer *buffer, const void *data);
71 	int (*read)(struct iio_buffer *buffer, size_t n, char __user *buf);
72 	size_t (*data_available)(struct iio_buffer *buffer);
73 	int (*remove_from)(struct iio_buffer *buffer, void *data);
74 	int (*write)(struct iio_buffer *buffer, size_t n, const char __user *buf);
75 	size_t (*space_available)(struct iio_buffer *buffer);
76 
77 	int (*request_update)(struct iio_buffer *buffer);
78 
79 	int (*set_bytes_per_datum)(struct iio_buffer *buffer, size_t bpd);
80 	int (*set_length)(struct iio_buffer *buffer, unsigned int length);
81 
82 	int (*enable)(struct iio_buffer *buffer, struct iio_dev *indio_dev);
83 	int (*disable)(struct iio_buffer *buffer, struct iio_dev *indio_dev);
84 
85 	void (*release)(struct iio_buffer *buffer);
86 
87 	struct iio_dma_buffer_block * (*attach_dmabuf)(struct iio_buffer *buffer,
88 						       struct dma_buf_attachment *attach);
89 	void (*detach_dmabuf)(struct iio_buffer *buffer,
90 			      struct iio_dma_buffer_block *block);
91 	int (*enqueue_dmabuf)(struct iio_buffer *buffer,
92 			      struct iio_dma_buffer_block *block,
93 			      struct dma_fence *fence, struct sg_table *sgt,
94 			      size_t size, bool cyclic);
95 	struct device * (*get_dma_dev)(struct iio_buffer *buffer);
96 	void (*lock_queue)(struct iio_buffer *buffer);
97 	void (*unlock_queue)(struct iio_buffer *buffer);
98 
99 	unsigned int modes;
100 	unsigned int flags;
101 };
102 
103 /**
104  * struct iio_buffer - general buffer structure
105  *
106  * Note that the internals of this structure should only be of interest to
107  * those writing new buffer implementations.
108  */
109 struct iio_buffer {
110 	/** @length: Number of datums in buffer. */
111 	unsigned int length;
112 
113 	/** @flags: File ops flags including busy flag. */
114 	unsigned long flags;
115 
116 	/**  @bytes_per_datum: Size of individual datum including timestamp. */
117 	size_t bytes_per_datum;
118 
119 	/* @direction: Direction of the data stream (in/out). */
120 	enum iio_buffer_direction direction;
121 
122 	/**
123 	 * @access: Buffer access functions associated with the
124 	 * implementation.
125 	 */
126 	const struct iio_buffer_access_funcs *access;
127 
128 	/** @scan_mask: Bitmask used in masking scan mode elements. */
129 	long *scan_mask;
130 
131 	/** @demux_list: List of operations required to demux the scan. */
132 	struct list_head demux_list;
133 
134 	/** @pollq: Wait queue to allow for polling on the buffer. */
135 	wait_queue_head_t pollq;
136 
137 	/** @watermark: Number of datums to wait for poll/read. */
138 	unsigned int watermark;
139 
140 	/* private: */
141 	/* @scan_timestamp: Does the scan mode include a timestamp. */
142 	bool scan_timestamp;
143 
144 	/* @buffer_attr_list: List of buffer attributes. */
145 	struct list_head buffer_attr_list;
146 
147 	/*
148 	 * @buffer_group: Attributes of the new buffer group.
149 	 * Includes scan elements attributes.
150 	 */
151 	struct attribute_group buffer_group;
152 
153 	/* @attrs: Standard attributes of the buffer. */
154 	const struct iio_dev_attr **attrs;
155 
156 	/* @demux_bounce: Buffer for doing gather from incoming scan. */
157 	void *demux_bounce;
158 
159 	/* @attached_entry: Entry in the devices list of buffers attached by the driver. */
160 	struct list_head attached_entry;
161 
162 	/* @buffer_list: Entry in the devices list of current buffers. */
163 	struct list_head buffer_list;
164 
165 	/* @ref: Reference count of the buffer. */
166 	struct kref ref;
167 
168 	/* @dmabufs: List of DMABUF attachments */
169 	struct list_head dmabufs; /* P: dmabufs_mutex */
170 
171 	/* @dmabufs_mutex: Protects dmabufs */
172 	struct mutex dmabufs_mutex;
173 };
174 
175 /**
176  * iio_update_buffers() - add or remove buffer from active list
177  * @indio_dev:		device to add buffer to
178  * @insert_buffer:	buffer to insert
179  * @remove_buffer:	buffer_to_remove
180  *
181  * Note this will tear down the all buffering and build it up again
182  */
183 int iio_update_buffers(struct iio_dev *indio_dev,
184 		       struct iio_buffer *insert_buffer,
185 		       struct iio_buffer *remove_buffer);
186 
187 /**
188  * iio_buffer_init() - Initialize the buffer structure
189  * @buffer:		buffer to be initialized
190  **/
191 void iio_buffer_init(struct iio_buffer *buffer);
192 
193 struct iio_buffer *iio_buffer_get(struct iio_buffer *buffer);
194 void iio_buffer_put(struct iio_buffer *buffer);
195 
196 void iio_buffer_signal_dmabuf_done(struct dma_fence *fence, int ret);
197 
198 #else /* CONFIG_IIO_BUFFER */
199 
iio_buffer_get(struct iio_buffer * buffer)200 static inline void iio_buffer_get(struct iio_buffer *buffer) {}
iio_buffer_put(struct iio_buffer * buffer)201 static inline void iio_buffer_put(struct iio_buffer *buffer) {}
202 
203 #endif /* CONFIG_IIO_BUFFER */
204 #endif /* _IIO_BUFFER_GENERIC_IMPL_H_ */
205