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