xref: /linux/include/media/v4l2-subdev.h (revision 07fdad3a93756b872da7b53647715c48d0f4a2d0)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *  V4L2 sub-device support header.
4  *
5  *  Copyright (C) 2008  Hans Verkuil <hverkuil@kernel.org>
6  */
7 
8 #ifndef _V4L2_SUBDEV_H
9 #define _V4L2_SUBDEV_H
10 
11 #include <linux/types.h>
12 #include <linux/v4l2-subdev.h>
13 #include <media/media-entity.h>
14 #include <media/v4l2-async.h>
15 #include <media/v4l2-common.h>
16 #include <media/v4l2-dev.h>
17 #include <media/v4l2-fh.h>
18 #include <media/v4l2-mediabus.h>
19 
20 /* generic v4l2_device notify callback notification values */
21 #define V4L2_SUBDEV_IR_RX_NOTIFY		_IOW('v', 0, u32)
22 #define V4L2_SUBDEV_IR_RX_FIFO_SERVICE_REQ	0x00000001
23 #define V4L2_SUBDEV_IR_RX_END_OF_RX_DETECTED	0x00000002
24 #define V4L2_SUBDEV_IR_RX_HW_FIFO_OVERRUN	0x00000004
25 #define V4L2_SUBDEV_IR_RX_SW_FIFO_OVERRUN	0x00000008
26 
27 #define V4L2_SUBDEV_IR_TX_NOTIFY		_IOW('v', 1, u32)
28 #define V4L2_SUBDEV_IR_TX_FIFO_SERVICE_REQ	0x00000001
29 
30 #define	V4L2_DEVICE_NOTIFY_EVENT		_IOW('v', 2, struct v4l2_event)
31 
32 struct v4l2_device;
33 struct v4l2_ctrl_handler;
34 struct v4l2_event;
35 struct v4l2_event_subscription;
36 struct v4l2_fh;
37 struct v4l2_subdev;
38 struct v4l2_subdev_fh;
39 struct v4l2_subdev_stream_config;
40 struct tuner_setup;
41 struct v4l2_mbus_frame_desc;
42 struct led_classdev;
43 
44 /**
45  * struct v4l2_decode_vbi_line - used to decode_vbi_line
46  *
47  * @is_second_field: Set to 0 for the first (odd) field;
48  *	set to 1 for the second (even) field.
49  * @p: Pointer to the sliced VBI data from the decoder. On exit, points to
50  *	the start of the payload.
51  * @line: Line number of the sliced VBI data (1-23)
52  * @type: VBI service type (V4L2_SLICED_*). 0 if no service found
53  */
54 struct v4l2_decode_vbi_line {
55 	u32 is_second_field;
56 	u8 *p;
57 	u32 line;
58 	u32 type;
59 };
60 
61 /*
62  * Sub-devices are devices that are connected somehow to the main bridge
63  * device. These devices are usually audio/video muxers/encoders/decoders or
64  * sensors and webcam controllers.
65  *
66  * Usually these devices are controlled through an i2c bus, but other buses
67  * may also be used.
68  *
69  * The v4l2_subdev struct provides a way of accessing these devices in a
70  * generic manner. Most operations that these sub-devices support fall in
71  * a few categories: core ops, audio ops, video ops and tuner ops.
72  *
73  * More categories can be added if needed, although this should remain a
74  * limited set (no more than approx. 8 categories).
75  *
76  * Each category has its own set of ops that subdev drivers can implement.
77  *
78  * A subdev driver can leave the pointer to the category ops NULL if
79  * it does not implement them (e.g. an audio subdev will generally not
80  * implement the video category ops). The exception is the core category:
81  * this must always be present.
82  *
83  * These ops are all used internally so it is no problem to change, remove
84  * or add ops or move ops from one to another category. Currently these
85  * ops are based on the original ioctls, but since ops are not limited to
86  * one argument there is room for improvement here once all i2c subdev
87  * drivers are converted to use these ops.
88  */
89 
90 /*
91  * Core ops: it is highly recommended to implement at least these ops:
92  *
93  * log_status
94  * g_register
95  * s_register
96  *
97  * This provides basic debugging support.
98  *
99  * The ioctl ops is meant for generic ioctl-like commands. Depending on
100  * the use-case it might be better to use subdev-specific ops (currently
101  * not yet implemented) since ops provide proper type-checking.
102  */
103 
104 /**
105  * enum v4l2_subdev_io_pin_bits - Subdevice external IO pin configuration
106  *	bits
107  *
108  * @V4L2_SUBDEV_IO_PIN_DISABLE: disables a pin config. ENABLE assumed.
109  * @V4L2_SUBDEV_IO_PIN_OUTPUT: set it if pin is an output.
110  * @V4L2_SUBDEV_IO_PIN_INPUT: set it if pin is an input.
111  * @V4L2_SUBDEV_IO_PIN_SET_VALUE: to set the output value via
112  *				  &struct v4l2_subdev_io_pin_config->value.
113  * @V4L2_SUBDEV_IO_PIN_ACTIVE_LOW: pin active is bit 0.
114  *				   Otherwise, ACTIVE HIGH is assumed.
115  */
116 enum v4l2_subdev_io_pin_bits {
117 	V4L2_SUBDEV_IO_PIN_DISABLE	= 0,
118 	V4L2_SUBDEV_IO_PIN_OUTPUT	= 1,
119 	V4L2_SUBDEV_IO_PIN_INPUT	= 2,
120 	V4L2_SUBDEV_IO_PIN_SET_VALUE	= 3,
121 	V4L2_SUBDEV_IO_PIN_ACTIVE_LOW	= 4,
122 };
123 
124 /**
125  * struct v4l2_subdev_io_pin_config - Subdevice external IO pin configuration
126  *
127  * @flags: bitmask with flags for this pin's config, whose bits are defined by
128  *	   &enum v4l2_subdev_io_pin_bits.
129  * @pin: Chip external IO pin to configure
130  * @function: Internal signal pad/function to route to IO pin
131  * @value: Initial value for pin - e.g. GPIO output value
132  * @strength: Pin drive strength
133  */
134 struct v4l2_subdev_io_pin_config {
135 	u32 flags;
136 	u8 pin;
137 	u8 function;
138 	u8 value;
139 	u8 strength;
140 };
141 
142 /**
143  * struct v4l2_subdev_core_ops - Define core ops callbacks for subdevs
144  *
145  * @log_status: callback for VIDIOC_LOG_STATUS() ioctl handler code.
146  *
147  * @s_io_pin_config: configure one or more chip I/O pins for chips that
148  *	multiplex different internal signal pads out to IO pins.  This function
149  *	takes a pointer to an array of 'n' pin configuration entries, one for
150  *	each pin being configured.  This function could be called at times
151  *	other than just subdevice initialization.
152  *
153  * @init: initialize the sensor registers to some sort of reasonable default
154  *	values. Do not use for new drivers and should be removed in existing
155  *	drivers.
156  *
157  * @load_fw: load firmware.
158  *
159  * @reset: generic reset command. The argument selects which subsystems to
160  *	reset. Passing 0 will always reset the whole chip. Do not use for new
161  *	drivers without discussing this first on the linux-media mailinglist.
162  *	There should be no reason normally to reset a device.
163  *
164  * @s_gpio: set GPIO pins. Very simple right now, might need to be extended with
165  *	a direction argument if needed.
166  *
167  * @command: called by in-kernel drivers in order to call functions internal
168  *	   to subdev drivers driver that have a separate callback.
169  *
170  * @ioctl: called at the end of ioctl() syscall handler at the V4L2 core.
171  *	   used to provide support for private ioctls used on the driver.
172  *
173  * @compat_ioctl32: called when a 32 bits application uses a 64 bits Kernel,
174  *		    in order to fix data passed from/to userspace.
175  *
176  * @g_register: callback for VIDIOC_DBG_G_REGISTER() ioctl handler code.
177  *
178  * @s_register: callback for VIDIOC_DBG_S_REGISTER() ioctl handler code.
179  *
180  * @s_power: puts subdevice in power saving mode (on == 0) or normal operation
181  *	mode (on == 1). DEPRECATED. See
182  *	Documentation/driver-api/media/camera-sensor.rst . pre_streamon and
183  *	post_streamoff callbacks can be used for e.g. setting the bus to LP-11
184  *	mode before s_stream is called.
185  *
186  * @interrupt_service_routine: Called by the bridge chip's interrupt service
187  *	handler, when an interrupt status has be raised due to this subdev,
188  *	so that this subdev can handle the details.  It may schedule work to be
189  *	performed later.  It must not sleep. **Called from an IRQ context**.
190  *
191  * @subscribe_event: used by the drivers to request the control framework that
192  *		     for it to be warned when the value of a control changes.
193  *
194  * @unsubscribe_event: remove event subscription from the control framework.
195  */
196 struct v4l2_subdev_core_ops {
197 	int (*log_status)(struct v4l2_subdev *sd);
198 	int (*s_io_pin_config)(struct v4l2_subdev *sd, size_t n,
199 				      struct v4l2_subdev_io_pin_config *pincfg);
200 	int (*init)(struct v4l2_subdev *sd, u32 val);
201 	int (*load_fw)(struct v4l2_subdev *sd);
202 	int (*reset)(struct v4l2_subdev *sd, u32 val);
203 	int (*s_gpio)(struct v4l2_subdev *sd, u32 val);
204 	long (*command)(struct v4l2_subdev *sd, unsigned int cmd, void *arg);
205 	long (*ioctl)(struct v4l2_subdev *sd, unsigned int cmd, void *arg);
206 #ifdef CONFIG_COMPAT
207 	long (*compat_ioctl32)(struct v4l2_subdev *sd, unsigned int cmd,
208 			       unsigned long arg);
209 #endif
210 #ifdef CONFIG_VIDEO_ADV_DEBUG
211 	int (*g_register)(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg);
212 	int (*s_register)(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg);
213 #endif
214 	int (*s_power)(struct v4l2_subdev *sd, int on);
215 	int (*interrupt_service_routine)(struct v4l2_subdev *sd,
216 						u32 status, bool *handled);
217 	int (*subscribe_event)(struct v4l2_subdev *sd, struct v4l2_fh *fh,
218 			       struct v4l2_event_subscription *sub);
219 	int (*unsubscribe_event)(struct v4l2_subdev *sd, struct v4l2_fh *fh,
220 				 struct v4l2_event_subscription *sub);
221 };
222 
223 /**
224  * struct v4l2_subdev_tuner_ops - Callbacks used when v4l device was opened
225  *	in radio mode.
226  *
227  * @standby: puts the tuner in standby mode. It will be woken up
228  *	     automatically the next time it is used.
229  *
230  * @s_radio: callback that switches the tuner to radio mode.
231  *	     drivers should explicitly call it when a tuner ops should
232  *	     operate on radio mode, before being able to handle it.
233  *	     Used on devices that have both AM/FM radio receiver and TV.
234  *
235  * @s_frequency: callback for VIDIOC_S_FREQUENCY() ioctl handler code.
236  *
237  * @g_frequency: callback for VIDIOC_G_FREQUENCY() ioctl handler code.
238  *		 freq->type must be filled in. Normally done by video_ioctl2()
239  *		 or the bridge driver.
240  *
241  * @enum_freq_bands: callback for VIDIOC_ENUM_FREQ_BANDS() ioctl handler code.
242  *
243  * @g_tuner: callback for VIDIOC_G_TUNER() ioctl handler code.
244  *
245  * @s_tuner: callback for VIDIOC_S_TUNER() ioctl handler code. @vt->type must be
246  *	     filled in. Normally done by video_ioctl2 or the
247  *	     bridge driver.
248  *
249  * @g_modulator: callback for VIDIOC_G_MODULATOR() ioctl handler code.
250  *
251  * @s_modulator: callback for VIDIOC_S_MODULATOR() ioctl handler code.
252  *
253  * @s_type_addr: sets tuner type and its I2C addr.
254  *
255  * @s_config: sets tda9887 specific stuff, like port1, port2 and qss
256  *
257  * .. note::
258  *
259  *	On devices that have both AM/FM and TV, it is up to the driver
260  *	to explicitly call s_radio when the tuner should be switched to
261  *	radio mode, before handling other &struct v4l2_subdev_tuner_ops
262  *	that would require it. An example of such usage is::
263  *
264  *	  static void s_frequency(void *priv, const struct v4l2_frequency *f)
265  *	  {
266  *		...
267  *		if (f.type == V4L2_TUNER_RADIO)
268  *			v4l2_device_call_all(v4l2_dev, 0, tuner, s_radio);
269  *		...
270  *		v4l2_device_call_all(v4l2_dev, 0, tuner, s_frequency);
271  *	  }
272  */
273 struct v4l2_subdev_tuner_ops {
274 	int (*standby)(struct v4l2_subdev *sd);
275 	int (*s_radio)(struct v4l2_subdev *sd);
276 	int (*s_frequency)(struct v4l2_subdev *sd, const struct v4l2_frequency *freq);
277 	int (*g_frequency)(struct v4l2_subdev *sd, struct v4l2_frequency *freq);
278 	int (*enum_freq_bands)(struct v4l2_subdev *sd, struct v4l2_frequency_band *band);
279 	int (*g_tuner)(struct v4l2_subdev *sd, struct v4l2_tuner *vt);
280 	int (*s_tuner)(struct v4l2_subdev *sd, const struct v4l2_tuner *vt);
281 	int (*g_modulator)(struct v4l2_subdev *sd, struct v4l2_modulator *vm);
282 	int (*s_modulator)(struct v4l2_subdev *sd, const struct v4l2_modulator *vm);
283 	int (*s_type_addr)(struct v4l2_subdev *sd, struct tuner_setup *type);
284 	int (*s_config)(struct v4l2_subdev *sd, const struct v4l2_priv_tun_config *config);
285 };
286 
287 /**
288  * struct v4l2_subdev_audio_ops - Callbacks used for audio-related settings
289  *
290  * @s_clock_freq: set the frequency (in Hz) of the audio clock output.
291  *	Used to slave an audio processor to the video decoder, ensuring that
292  *	audio and video remain synchronized. Usual values for the frequency
293  *	are 48000, 44100 or 32000 Hz. If the frequency is not supported, then
294  *	-EINVAL is returned.
295  *
296  * @s_i2s_clock_freq: sets I2S speed in bps. This is used to provide a standard
297  *	way to select I2S clock used by driving digital audio streams at some
298  *	board designs. Usual values for the frequency are 1024000 and 2048000.
299  *	If the frequency is not supported, then %-EINVAL is returned.
300  *
301  * @s_routing: used to define the input and/or output pins of an audio chip,
302  *	and any additional configuration data.
303  *	Never attempt to use user-level input IDs (e.g. Composite, S-Video,
304  *	Tuner) at this level. An i2c device shouldn't know about whether an
305  *	input pin is connected to a Composite connector, become on another
306  *	board or platform it might be connected to something else entirely.
307  *	The calling driver is responsible for mapping a user-level input to
308  *	the right pins on the i2c device.
309  *
310  * @s_stream: used to notify the audio code that stream will start or has
311  *	stopped.
312  */
313 struct v4l2_subdev_audio_ops {
314 	int (*s_clock_freq)(struct v4l2_subdev *sd, u32 freq);
315 	int (*s_i2s_clock_freq)(struct v4l2_subdev *sd, u32 freq);
316 	int (*s_routing)(struct v4l2_subdev *sd, u32 input, u32 output, u32 config);
317 	int (*s_stream)(struct v4l2_subdev *sd, int enable);
318 };
319 
320 /**
321  * struct v4l2_mbus_frame_desc_entry_csi2
322  *
323  * @vc: CSI-2 virtual channel
324  * @dt: CSI-2 data type ID
325  */
326 struct v4l2_mbus_frame_desc_entry_csi2 {
327 	u8 vc;
328 	u8 dt;
329 };
330 
331 /**
332  * enum v4l2_mbus_frame_desc_flags - media bus frame description flags
333  *
334  * @V4L2_MBUS_FRAME_DESC_FL_LEN_MAX:
335  *	Indicates that &struct v4l2_mbus_frame_desc_entry->length field
336  *	specifies maximum data length.
337  * @V4L2_MBUS_FRAME_DESC_FL_BLOB:
338  *	Indicates that the format does not have line offsets, i.e.
339  *	the receiver should use 1D DMA.
340  */
341 enum v4l2_mbus_frame_desc_flags {
342 	V4L2_MBUS_FRAME_DESC_FL_LEN_MAX	= BIT(0),
343 	V4L2_MBUS_FRAME_DESC_FL_BLOB	= BIT(1),
344 };
345 
346 /**
347  * struct v4l2_mbus_frame_desc_entry - media bus frame description structure
348  *
349  * @flags:	bitmask flags, as defined by &enum v4l2_mbus_frame_desc_flags.
350  * @stream:	stream in routing configuration
351  * @pixelcode:	media bus pixel code, valid if @flags
352  *		%FRAME_DESC_FL_BLOB is not set.
353  * @length:	number of octets per frame, valid if @flags
354  *		%V4L2_MBUS_FRAME_DESC_FL_LEN_MAX is set.
355  * @bus:	Bus-specific frame descriptor parameters
356  * @bus.csi2:	CSI-2-specific bus configuration
357  */
358 struct v4l2_mbus_frame_desc_entry {
359 	enum v4l2_mbus_frame_desc_flags flags;
360 	u32 stream;
361 	u32 pixelcode;
362 	u32 length;
363 	union {
364 		struct v4l2_mbus_frame_desc_entry_csi2 csi2;
365 	} bus;
366 };
367 
368  /*
369   * If this number is too small, it should be dropped altogether and the
370   * API switched to a dynamic number of frame descriptor entries.
371   */
372 #define V4L2_FRAME_DESC_ENTRY_MAX	8
373 
374 /**
375  * enum v4l2_mbus_frame_desc_type - media bus frame description type
376  *
377  * @V4L2_MBUS_FRAME_DESC_TYPE_UNDEFINED:
378  *	Undefined frame desc type. Drivers should not use this, it is
379  *	for backwards compatibility.
380  * @V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL:
381  *	Parallel media bus.
382  * @V4L2_MBUS_FRAME_DESC_TYPE_CSI2:
383  *	CSI-2 media bus. Frame desc parameters must be set in
384  *	&struct v4l2_mbus_frame_desc_entry->csi2.
385  */
386 enum v4l2_mbus_frame_desc_type {
387 	V4L2_MBUS_FRAME_DESC_TYPE_UNDEFINED = 0,
388 	V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL,
389 	V4L2_MBUS_FRAME_DESC_TYPE_CSI2,
390 };
391 
392 /**
393  * struct v4l2_mbus_frame_desc - media bus data frame description
394  * @type: type of the bus (enum v4l2_mbus_frame_desc_type)
395  * @entry: frame descriptors array
396  * @num_entries: number of entries in @entry array
397  */
398 struct v4l2_mbus_frame_desc {
399 	enum v4l2_mbus_frame_desc_type type;
400 	struct v4l2_mbus_frame_desc_entry entry[V4L2_FRAME_DESC_ENTRY_MAX];
401 	unsigned short num_entries;
402 };
403 
404 /**
405  * enum v4l2_subdev_pre_streamon_flags - Flags for pre_streamon subdev core op
406  *
407  * @V4L2_SUBDEV_PRE_STREAMON_FL_MANUAL_LP: Set the transmitter to either LP-11
408  *	or LP-111 mode before call to s_stream().
409  */
410 enum v4l2_subdev_pre_streamon_flags {
411 	V4L2_SUBDEV_PRE_STREAMON_FL_MANUAL_LP = BIT(0),
412 };
413 
414 /**
415  * struct v4l2_subdev_video_ops - Callbacks used when v4l device was opened
416  *				  in video mode.
417  *
418  * @s_routing: see s_routing in audio_ops, except this version is for video
419  *	devices.
420  *
421  * @s_crystal_freq: sets the frequency of the crystal used to generate the
422  *	clocks in Hz. An extra flags field allows device specific configuration
423  *	regarding clock frequency dividers, etc. If not used, then set flags
424  *	to 0. If the frequency is not supported, then -EINVAL is returned.
425  *
426  * @g_std: callback for VIDIOC_G_STD() ioctl handler code.
427  *
428  * @s_std: callback for VIDIOC_S_STD() ioctl handler code.
429  *
430  * @s_std_output: set v4l2_std_id for video OUTPUT devices. This is ignored by
431  *	video input devices.
432  *
433  * @g_std_output: get current standard for video OUTPUT devices. This is ignored
434  *	by video input devices.
435  *
436  * @querystd: callback for VIDIOC_QUERYSTD() ioctl handler code.
437  *
438  * @g_tvnorms: get &v4l2_std_id with all standards supported by the video
439  *	CAPTURE device. This is ignored by video output devices.
440  *
441  * @g_tvnorms_output: get v4l2_std_id with all standards supported by the video
442  *	OUTPUT device. This is ignored by video capture devices.
443  *
444  * @g_input_status: get input status. Same as the status field in the
445  *	&struct v4l2_input
446  *
447  * @s_stream: start (enabled == 1) or stop (enabled == 0) streaming on the
448  *	sub-device. Failure on stop will remove any resources acquired in
449  *	streaming start, while the error code is still returned by the driver.
450  *	The caller shall track the subdev state, and shall not start or stop an
451  *	already started or stopped subdev. Also see call_s_stream wrapper in
452  *	v4l2-subdev.c.
453  *
454  *	This callback is DEPRECATED. New drivers should instead implement
455  *	&v4l2_subdev_pad_ops.enable_streams and
456  *	&v4l2_subdev_pad_ops.disable_streams operations, and use
457  *	v4l2_subdev_s_stream_helper for the &v4l2_subdev_video_ops.s_stream
458  *	operation to support legacy users.
459  *
460  *	Drivers should also not call the .s_stream() subdev operation directly,
461  *	but use the v4l2_subdev_enable_streams() and
462  *	v4l2_subdev_disable_streams() helpers.
463  *
464  * @s_rx_buffer: set a host allocated memory buffer for the subdev. The subdev
465  *	can adjust @size to a lower value and must not write more data to the
466  *	buffer starting at @data than the original value of @size.
467  *
468  * @pre_streamon: May be called before streaming is actually started, to help
469  *	initialising the bus. Current usage is to set a CSI-2 transmitter to
470  *	LP-11 or LP-111 mode before streaming. See &enum
471  *	v4l2_subdev_pre_streamon_flags.
472  *
473  *	pre_streamon shall return error if it cannot perform the operation as
474  *	indicated by the flags argument. In particular, -EACCES indicates lack
475  *	of support for the operation. The caller shall call post_streamoff for
476  *	each successful call of pre_streamon.
477  *
478  * @post_streamoff: Called after streaming is stopped, but if and only if
479  *	pre_streamon was called earlier.
480  */
481 struct v4l2_subdev_video_ops {
482 	int (*s_routing)(struct v4l2_subdev *sd, u32 input, u32 output, u32 config);
483 	int (*s_crystal_freq)(struct v4l2_subdev *sd, u32 freq, u32 flags);
484 	int (*g_std)(struct v4l2_subdev *sd, v4l2_std_id *norm);
485 	int (*s_std)(struct v4l2_subdev *sd, v4l2_std_id norm);
486 	int (*s_std_output)(struct v4l2_subdev *sd, v4l2_std_id std);
487 	int (*g_std_output)(struct v4l2_subdev *sd, v4l2_std_id *std);
488 	int (*querystd)(struct v4l2_subdev *sd, v4l2_std_id *std);
489 	int (*g_tvnorms)(struct v4l2_subdev *sd, v4l2_std_id *std);
490 	int (*g_tvnorms_output)(struct v4l2_subdev *sd, v4l2_std_id *std);
491 	int (*g_input_status)(struct v4l2_subdev *sd, u32 *status);
492 	int (*s_stream)(struct v4l2_subdev *sd, int enable);
493 	int (*s_rx_buffer)(struct v4l2_subdev *sd, void *buf,
494 			   unsigned int *size);
495 	int (*pre_streamon)(struct v4l2_subdev *sd, u32 flags);
496 	int (*post_streamoff)(struct v4l2_subdev *sd);
497 };
498 
499 /**
500  * struct v4l2_subdev_vbi_ops - Callbacks used when v4l device was opened
501  *				  in video mode via the vbi device node.
502  *
503  *  @decode_vbi_line: video decoders that support sliced VBI need to implement
504  *	this ioctl. Field p of the &struct v4l2_decode_vbi_line is set to the
505  *	start of the VBI data that was generated by the decoder. The driver
506  *	then parses the sliced VBI data and sets the other fields in the
507  *	struct accordingly. The pointer p is updated to point to the start of
508  *	the payload which can be copied verbatim into the data field of the
509  *	&struct v4l2_sliced_vbi_data. If no valid VBI data was found, then the
510  *	type field is set to 0 on return.
511  *
512  * @s_vbi_data: used to generate VBI signals on a video signal.
513  *	&struct v4l2_sliced_vbi_data is filled with the data packets that
514  *	should be output. Note that if you set the line field to 0, then that
515  *	VBI signal is disabled. If no valid VBI data was found, then the type
516  *	field is set to 0 on return.
517  *
518  * @g_vbi_data: used to obtain the sliced VBI packet from a readback register.
519  *	Not all video decoders support this. If no data is available because
520  *	the readback register contains invalid or erroneous data %-EIO is
521  *	returned. Note that you must fill in the 'id' member and the 'field'
522  *	member (to determine whether CC data from the first or second field
523  *	should be obtained).
524  *
525  * @g_sliced_vbi_cap: callback for VIDIOC_G_SLICED_VBI_CAP() ioctl handler
526  *		      code.
527  *
528  * @s_raw_fmt: setup the video encoder/decoder for raw VBI.
529  *
530  * @g_sliced_fmt: retrieve the current sliced VBI settings.
531  *
532  * @s_sliced_fmt: setup the sliced VBI settings.
533  */
534 struct v4l2_subdev_vbi_ops {
535 	int (*decode_vbi_line)(struct v4l2_subdev *sd, struct v4l2_decode_vbi_line *vbi_line);
536 	int (*s_vbi_data)(struct v4l2_subdev *sd, const struct v4l2_sliced_vbi_data *vbi_data);
537 	int (*g_vbi_data)(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_data *vbi_data);
538 	int (*g_sliced_vbi_cap)(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_cap *cap);
539 	int (*s_raw_fmt)(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt);
540 	int (*g_sliced_fmt)(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *fmt);
541 	int (*s_sliced_fmt)(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *fmt);
542 };
543 
544 /**
545  * struct v4l2_subdev_sensor_ops - v4l2-subdev sensor operations
546  * @g_skip_top_lines: number of lines at the top of the image to be skipped.
547  *		      This is needed for some sensors, which always corrupt
548  *		      several top lines of the output image, or which send their
549  *		      metadata in them.
550  * @g_skip_frames: number of frames to skip at stream start. This is needed for
551  *		   buggy sensors that generate faulty frames when they are
552  *		   turned on.
553  */
554 struct v4l2_subdev_sensor_ops {
555 	int (*g_skip_top_lines)(struct v4l2_subdev *sd, u32 *lines);
556 	int (*g_skip_frames)(struct v4l2_subdev *sd, u32 *frames);
557 };
558 
559 /**
560  * enum v4l2_subdev_ir_mode- describes the type of IR supported
561  *
562  * @V4L2_SUBDEV_IR_MODE_PULSE_WIDTH: IR uses struct ir_raw_event records
563  */
564 enum v4l2_subdev_ir_mode {
565 	V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
566 };
567 
568 /**
569  * struct v4l2_subdev_ir_parameters - Parameters for IR TX or TX
570  *
571  * @bytes_per_data_element: bytes per data element of data in read or
572  *	write call.
573  * @mode: IR mode as defined by &enum v4l2_subdev_ir_mode.
574  * @enable: device is active if true
575  * @interrupt_enable: IR interrupts are enabled if true
576  * @shutdown: if true: set hardware to low/no power, false: normal mode
577  *
578  * @modulation: if true, it uses carrier, if false: baseband
579  * @max_pulse_width:  maximum pulse width in ns, valid only for baseband signal
580  * @carrier_freq: carrier frequency in Hz, valid only for modulated signal
581  * @duty_cycle: duty cycle percentage, valid only for modulated signal
582  * @invert_level: invert signal level
583  *
584  * @invert_carrier_sense: Send 0/space as a carrier burst. used only in TX.
585  *
586  * @noise_filter_min_width: min time of a valid pulse, in ns. Used only for RX.
587  * @carrier_range_lower: Lower carrier range, in Hz, valid only for modulated
588  *	signal. Used only for RX.
589  * @carrier_range_upper: Upper carrier range, in Hz, valid only for modulated
590  *	signal. Used only for RX.
591  * @resolution: The receive resolution, in ns . Used only for RX.
592  */
593 struct v4l2_subdev_ir_parameters {
594 	unsigned int bytes_per_data_element;
595 	enum v4l2_subdev_ir_mode mode;
596 
597 	bool enable;
598 	bool interrupt_enable;
599 	bool shutdown;
600 
601 	bool modulation;
602 	u32 max_pulse_width;
603 	unsigned int carrier_freq;
604 	unsigned int duty_cycle;
605 	bool invert_level;
606 
607 	/* Tx only */
608 	bool invert_carrier_sense;
609 
610 	/* Rx only */
611 	u32 noise_filter_min_width;
612 	unsigned int carrier_range_lower;
613 	unsigned int carrier_range_upper;
614 	u32 resolution;
615 };
616 
617 /**
618  * struct v4l2_subdev_ir_ops - operations for IR subdevices
619  *
620  * @rx_read: Reads received codes or pulse width data.
621  *	The semantics are similar to a non-blocking read() call.
622  * @rx_g_parameters: Get the current operating parameters and state of
623  *	the IR receiver.
624  * @rx_s_parameters: Set the current operating parameters and state of
625  *	the IR receiver.  It is recommended to call
626  *	[rt]x_g_parameters first to fill out the current state, and only change
627  *	the fields that need to be changed.  Upon return, the actual device
628  *	operating parameters and state will be returned.  Note that hardware
629  *	limitations may prevent the actual settings from matching the requested
630  *	settings - e.g. an actual carrier setting of 35,904 Hz when 36,000 Hz
631  *	was requested.  An exception is when the shutdown parameter is true.
632  *	The last used operational parameters will be returned, but the actual
633  *	state of the hardware be different to minimize power consumption and
634  *	processing when shutdown is true.
635  *
636  * @tx_write: Writes codes or pulse width data for transmission.
637  *	The semantics are similar to a non-blocking write() call.
638  * @tx_g_parameters: Get the current operating parameters and state of
639  *	the IR transmitter.
640  * @tx_s_parameters: Set the current operating parameters and state of
641  *	the IR transmitter.  It is recommended to call
642  *	[rt]x_g_parameters first to fill out the current state, and only change
643  *	the fields that need to be changed.  Upon return, the actual device
644  *	operating parameters and state will be returned.  Note that hardware
645  *	limitations may prevent the actual settings from matching the requested
646  *	settings - e.g. an actual carrier setting of 35,904 Hz when 36,000 Hz
647  *	was requested.  An exception is when the shutdown parameter is true.
648  *	The last used operational parameters will be returned, but the actual
649  *	state of the hardware be different to minimize power consumption and
650  *	processing when shutdown is true.
651  */
652 struct v4l2_subdev_ir_ops {
653 	/* Receiver */
654 	int (*rx_read)(struct v4l2_subdev *sd, u8 *buf, size_t count,
655 				ssize_t *num);
656 
657 	int (*rx_g_parameters)(struct v4l2_subdev *sd,
658 				struct v4l2_subdev_ir_parameters *params);
659 	int (*rx_s_parameters)(struct v4l2_subdev *sd,
660 				struct v4l2_subdev_ir_parameters *params);
661 
662 	/* Transmitter */
663 	int (*tx_write)(struct v4l2_subdev *sd, u8 *buf, size_t count,
664 				ssize_t *num);
665 
666 	int (*tx_g_parameters)(struct v4l2_subdev *sd,
667 				struct v4l2_subdev_ir_parameters *params);
668 	int (*tx_s_parameters)(struct v4l2_subdev *sd,
669 				struct v4l2_subdev_ir_parameters *params);
670 };
671 
672 /**
673  * struct v4l2_subdev_pad_config - Used for storing subdev pad information.
674  *
675  * @format: &struct v4l2_mbus_framefmt
676  * @crop: &struct v4l2_rect to be used for crop
677  * @compose: &struct v4l2_rect to be used for compose
678  * @interval: frame interval
679  */
680 struct v4l2_subdev_pad_config {
681 	struct v4l2_mbus_framefmt format;
682 	struct v4l2_rect crop;
683 	struct v4l2_rect compose;
684 	struct v4l2_fract interval;
685 };
686 
687 /**
688  * struct v4l2_subdev_stream_configs - A collection of stream configs.
689  *
690  * @num_configs: number of entries in @config.
691  * @configs: an array of &struct v4l2_subdev_stream_configs.
692  */
693 struct v4l2_subdev_stream_configs {
694 	u32 num_configs;
695 	struct v4l2_subdev_stream_config *configs;
696 };
697 
698 /**
699  * struct v4l2_subdev_krouting - subdev routing table
700  *
701  * @len_routes: length of routes array, in routes
702  * @num_routes: number of routes
703  * @routes: &struct v4l2_subdev_route
704  *
705  * This structure contains the routing table for a subdev.
706  */
707 struct v4l2_subdev_krouting {
708 	unsigned int len_routes;
709 	unsigned int num_routes;
710 	struct v4l2_subdev_route *routes;
711 };
712 
713 /**
714  * struct v4l2_subdev_state - Used for storing subdev state information.
715  *
716  * @_lock: default for 'lock'
717  * @lock: mutex for the state. May be replaced by the user.
718  * @sd: the sub-device which the state is related to
719  * @pads: &struct v4l2_subdev_pad_config array
720  * @routing: routing table for the subdev
721  * @stream_configs: stream configurations (only for V4L2_SUBDEV_FL_STREAMS)
722  *
723  * This structure only needs to be passed to the pad op if the 'which' field
724  * of the main argument is set to %V4L2_SUBDEV_FORMAT_TRY. For
725  * %V4L2_SUBDEV_FORMAT_ACTIVE it is safe to pass %NULL.
726  */
727 struct v4l2_subdev_state {
728 	/* lock for the struct v4l2_subdev_state fields */
729 	struct mutex _lock;
730 	struct mutex *lock;
731 	struct v4l2_subdev *sd;
732 	struct v4l2_subdev_pad_config *pads;
733 	struct v4l2_subdev_krouting routing;
734 	struct v4l2_subdev_stream_configs stream_configs;
735 };
736 
737 /**
738  * struct v4l2_subdev_pad_ops - v4l2-subdev pad level operations
739  *
740  * @enum_mbus_code: callback for VIDIOC_SUBDEV_ENUM_MBUS_CODE() ioctl handler
741  *		    code.
742  * @enum_frame_size: callback for VIDIOC_SUBDEV_ENUM_FRAME_SIZE() ioctl handler
743  *		     code.
744  *
745  * @enum_frame_interval: callback for VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL() ioctl
746  *			 handler code.
747  *
748  * @get_fmt: callback for VIDIOC_SUBDEV_G_FMT() ioctl handler code.
749  *
750  * @set_fmt: callback for VIDIOC_SUBDEV_S_FMT() ioctl handler code.
751  *
752  * @get_selection: callback for VIDIOC_SUBDEV_G_SELECTION() ioctl handler code.
753  *
754  * @set_selection: callback for VIDIOC_SUBDEV_S_SELECTION() ioctl handler code.
755  *
756  * @get_frame_interval: callback for VIDIOC_SUBDEV_G_FRAME_INTERVAL()
757  *			ioctl handler code.
758  *
759  * @set_frame_interval: callback for VIDIOC_SUBDEV_S_FRAME_INTERVAL()
760  *			ioctl handler code.
761  *
762  * @get_edid: callback for VIDIOC_SUBDEV_G_EDID() ioctl handler code.
763  *
764  * @set_edid: callback for VIDIOC_SUBDEV_S_EDID() ioctl handler code.
765  *
766  * @s_dv_timings: Set custom dv timings in the sub device. This is used
767  *	when sub device is capable of setting detailed timing information
768  *	in the hardware to generate/detect the video signal.
769  *
770  * @g_dv_timings: Get custom dv timings in the sub device.
771  *
772  * @query_dv_timings: callback for VIDIOC_QUERY_DV_TIMINGS() ioctl handler code.
773  *
774  * @dv_timings_cap: callback for VIDIOC_SUBDEV_DV_TIMINGS_CAP() ioctl handler
775  *		    code.
776  *
777  * @enum_dv_timings: callback for VIDIOC_SUBDEV_ENUM_DV_TIMINGS() ioctl handler
778  *		     code.
779  *
780  * @link_validate: used by the media controller code to check if the links
781  *		   that belongs to a pipeline can be used for stream.
782  *
783  * @get_frame_desc: get the current low level media bus frame parameters.
784  *
785  * @set_frame_desc: set the low level media bus frame parameters, @fd array
786  *                  may be adjusted by the subdev driver to device capabilities.
787  *
788  * @get_mbus_config: get the media bus configuration of a remote sub-device.
789  *		     The media bus configuration is usually retrieved from the
790  *		     firmware interface at sub-device probe time, immediately
791  *		     applied to the hardware and eventually adjusted by the
792  *		     driver. Remote sub-devices (usually video receivers) shall
793  *		     use this operation to query the transmitting end bus
794  *		     configuration in order to adjust their own one accordingly.
795  *		     Callers should make sure they get the most up-to-date as
796  *		     possible configuration from the remote end, likely calling
797  *		     this operation as close as possible to stream on time. The
798  *		     operation shall fail if the pad index it has been called on
799  *		     is not valid or in case of unrecoverable failures. The
800  *		     config argument has been memset to 0 just before calling
801  *		     the op.
802  *
803  * @set_routing: Enable or disable data connection routes described in the
804  *		 subdevice routing table. Subdevs that implement this operation
805  *		 must set the V4L2_SUBDEV_FL_STREAMS flag.
806  *
807  * @enable_streams: Enable the streams defined in streams_mask on the given
808  *	source pad. Subdevs that implement this operation must use the active
809  *	state management provided by the subdev core (enabled through a call to
810  *	v4l2_subdev_init_finalize() at initialization time). Do not call
811  *	directly, use v4l2_subdev_enable_streams() instead.
812  *
813  *	Drivers that support only a single stream without setting the
814  *	V4L2_SUBDEV_CAP_STREAMS sub-device capability flag can ignore the mask
815  *	argument.
816  *
817  * @disable_streams: Disable the streams defined in streams_mask on the given
818  *	source pad. Subdevs that implement this operation must use the active
819  *	state management provided by the subdev core (enabled through a call to
820  *	v4l2_subdev_init_finalize() at initialization time). Do not call
821  *	directly, use v4l2_subdev_disable_streams() instead.
822  *
823  *	Drivers that support only a single stream without setting the
824  *	V4L2_SUBDEV_CAP_STREAMS sub-device capability flag can ignore the mask
825  *	argument.
826  */
827 struct v4l2_subdev_pad_ops {
828 	int (*enum_mbus_code)(struct v4l2_subdev *sd,
829 			      struct v4l2_subdev_state *state,
830 			      struct v4l2_subdev_mbus_code_enum *code);
831 	int (*enum_frame_size)(struct v4l2_subdev *sd,
832 			       struct v4l2_subdev_state *state,
833 			       struct v4l2_subdev_frame_size_enum *fse);
834 	int (*enum_frame_interval)(struct v4l2_subdev *sd,
835 				   struct v4l2_subdev_state *state,
836 				   struct v4l2_subdev_frame_interval_enum *fie);
837 	int (*get_fmt)(struct v4l2_subdev *sd,
838 		       struct v4l2_subdev_state *state,
839 		       struct v4l2_subdev_format *format);
840 	int (*set_fmt)(struct v4l2_subdev *sd,
841 		       struct v4l2_subdev_state *state,
842 		       struct v4l2_subdev_format *format);
843 	int (*get_selection)(struct v4l2_subdev *sd,
844 			     struct v4l2_subdev_state *state,
845 			     struct v4l2_subdev_selection *sel);
846 	int (*set_selection)(struct v4l2_subdev *sd,
847 			     struct v4l2_subdev_state *state,
848 			     struct v4l2_subdev_selection *sel);
849 	int (*get_frame_interval)(struct v4l2_subdev *sd,
850 				  struct v4l2_subdev_state *state,
851 				  struct v4l2_subdev_frame_interval *interval);
852 	int (*set_frame_interval)(struct v4l2_subdev *sd,
853 				  struct v4l2_subdev_state *state,
854 				  struct v4l2_subdev_frame_interval *interval);
855 	int (*get_edid)(struct v4l2_subdev *sd, struct v4l2_edid *edid);
856 	int (*set_edid)(struct v4l2_subdev *sd, struct v4l2_edid *edid);
857 	int (*s_dv_timings)(struct v4l2_subdev *sd, unsigned int pad,
858 			    struct v4l2_dv_timings *timings);
859 	int (*g_dv_timings)(struct v4l2_subdev *sd, unsigned int pad,
860 			    struct v4l2_dv_timings *timings);
861 	int (*query_dv_timings)(struct v4l2_subdev *sd, unsigned int pad,
862 				struct v4l2_dv_timings *timings);
863 	int (*dv_timings_cap)(struct v4l2_subdev *sd,
864 			      struct v4l2_dv_timings_cap *cap);
865 	int (*enum_dv_timings)(struct v4l2_subdev *sd,
866 			       struct v4l2_enum_dv_timings *timings);
867 #ifdef CONFIG_MEDIA_CONTROLLER
868 	int (*link_validate)(struct v4l2_subdev *sd, struct media_link *link,
869 			     struct v4l2_subdev_format *source_fmt,
870 			     struct v4l2_subdev_format *sink_fmt);
871 #endif /* CONFIG_MEDIA_CONTROLLER */
872 	int (*get_frame_desc)(struct v4l2_subdev *sd, unsigned int pad,
873 			      struct v4l2_mbus_frame_desc *fd);
874 	int (*set_frame_desc)(struct v4l2_subdev *sd, unsigned int pad,
875 			      struct v4l2_mbus_frame_desc *fd);
876 	int (*get_mbus_config)(struct v4l2_subdev *sd, unsigned int pad,
877 			       struct v4l2_mbus_config *config);
878 	int (*set_routing)(struct v4l2_subdev *sd,
879 			   struct v4l2_subdev_state *state,
880 			   enum v4l2_subdev_format_whence which,
881 			   struct v4l2_subdev_krouting *route);
882 	int (*enable_streams)(struct v4l2_subdev *sd,
883 			      struct v4l2_subdev_state *state, u32 pad,
884 			      u64 streams_mask);
885 	int (*disable_streams)(struct v4l2_subdev *sd,
886 			       struct v4l2_subdev_state *state, u32 pad,
887 			       u64 streams_mask);
888 };
889 
890 /**
891  * struct v4l2_subdev_ops - Subdev operations
892  *
893  * @core: pointer to &struct v4l2_subdev_core_ops. Can be %NULL
894  * @tuner: pointer to &struct v4l2_subdev_tuner_ops. Can be %NULL
895  * @audio: pointer to &struct v4l2_subdev_audio_ops. Can be %NULL
896  * @video: pointer to &struct v4l2_subdev_video_ops. Can be %NULL
897  * @vbi: pointer to &struct v4l2_subdev_vbi_ops. Can be %NULL
898  * @ir: pointer to &struct v4l2_subdev_ir_ops. Can be %NULL
899  * @sensor: pointer to &struct v4l2_subdev_sensor_ops. Can be %NULL
900  * @pad: pointer to &struct v4l2_subdev_pad_ops. Can be %NULL
901  */
902 struct v4l2_subdev_ops {
903 	const struct v4l2_subdev_core_ops	*core;
904 	const struct v4l2_subdev_tuner_ops	*tuner;
905 	const struct v4l2_subdev_audio_ops	*audio;
906 	const struct v4l2_subdev_video_ops	*video;
907 	const struct v4l2_subdev_vbi_ops	*vbi;
908 	const struct v4l2_subdev_ir_ops		*ir;
909 	const struct v4l2_subdev_sensor_ops	*sensor;
910 	const struct v4l2_subdev_pad_ops	*pad;
911 };
912 
913 /**
914  * struct v4l2_subdev_internal_ops - V4L2 subdev internal ops
915  *
916  * @init_state: initialize the subdev state to default values
917  *
918  * @registered: called when this subdev is registered. When called the v4l2_dev
919  *	field is set to the correct v4l2_device.
920  *
921  * @unregistered: called when this subdev is unregistered. When called the
922  *	v4l2_dev field is still set to the correct v4l2_device.
923  *
924  * @open: called when the subdev device node is opened by an application.
925  *
926  * @close: called when the subdev device node is closed. Please note that
927  *	it is possible for @close to be called after @unregistered!
928  *
929  * @release: called when the last user of the subdev device is gone. This
930  *	happens after the @unregistered callback and when the last open
931  *	filehandle to the v4l-subdevX device node was closed. If no device
932  *	node was created for this sub-device, then the @release callback
933  *	is called right after the @unregistered callback.
934  *	The @release callback is typically used to free the memory containing
935  *	the v4l2_subdev structure. It is almost certainly required for any
936  *	sub-device that sets the V4L2_SUBDEV_FL_HAS_DEVNODE flag.
937  *
938  * .. note::
939  *	Never call this from drivers, only the v4l2 framework can call
940  *	these ops.
941  */
942 struct v4l2_subdev_internal_ops {
943 	int (*init_state)(struct v4l2_subdev *sd,
944 			  struct v4l2_subdev_state *state);
945 	int (*registered)(struct v4l2_subdev *sd);
946 	void (*unregistered)(struct v4l2_subdev *sd);
947 	int (*open)(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh);
948 	int (*close)(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh);
949 	void (*release)(struct v4l2_subdev *sd);
950 };
951 
952 /* Set this flag if this subdev is a i2c device. */
953 #define V4L2_SUBDEV_FL_IS_I2C			(1U << 0)
954 /* Set this flag if this subdev is a spi device. */
955 #define V4L2_SUBDEV_FL_IS_SPI			(1U << 1)
956 /* Set this flag if this subdev needs a device node. */
957 #define V4L2_SUBDEV_FL_HAS_DEVNODE		(1U << 2)
958 /*
959  * Set this flag if this subdev generates events.
960  * Note controls can send events, thus drivers exposing controls
961  * should set this flag.
962  */
963 #define V4L2_SUBDEV_FL_HAS_EVENTS		(1U << 3)
964 /*
965  * Set this flag if this subdev supports multiplexed streams. This means
966  * that the driver supports routing and handles the stream parameter in its
967  * v4l2_subdev_pad_ops handlers. More specifically, this means:
968  *
969  * - Centrally managed subdev active state is enabled
970  * - Legacy pad config is _not_ supported (state->pads is NULL)
971  * - Routing ioctls are available
972  * - Multiple streams per pad are supported
973  */
974 #define V4L2_SUBDEV_FL_STREAMS			(1U << 4)
975 
976 struct regulator_bulk_data;
977 
978 /**
979  * struct v4l2_subdev_platform_data - regulators config struct
980  *
981  * @regulators: Optional regulators used to power on/off the subdevice
982  * @num_regulators: Number of regululators
983  * @host_priv: Per-subdevice data, specific for a certain video host device
984  */
985 struct v4l2_subdev_platform_data {
986 	struct regulator_bulk_data *regulators;
987 	int num_regulators;
988 
989 	void *host_priv;
990 };
991 
992 /**
993  * struct v4l2_subdev - describes a V4L2 sub-device
994  *
995  * @entity: pointer to &struct media_entity
996  * @list: List of sub-devices
997  * @owner: The owner is the same as the driver's &struct device owner.
998  * @owner_v4l2_dev: true if the &sd->owner matches the owner of @v4l2_dev->dev
999  *	owner. Initialized by v4l2_device_register_subdev().
1000  * @flags: subdev flags. Can be:
1001  *   %V4L2_SUBDEV_FL_IS_I2C - Set this flag if this subdev is a i2c device;
1002  *   %V4L2_SUBDEV_FL_IS_SPI - Set this flag if this subdev is a spi device;
1003  *   %V4L2_SUBDEV_FL_HAS_DEVNODE - Set this flag if this subdev needs a
1004  *   device node;
1005  *   %V4L2_SUBDEV_FL_HAS_EVENTS -  Set this flag if this subdev generates
1006  *   events.
1007  *
1008  * @v4l2_dev: pointer to struct &v4l2_device
1009  * @ops: pointer to struct &v4l2_subdev_ops
1010  * @internal_ops: pointer to struct &v4l2_subdev_internal_ops.
1011  *	Never call these internal ops from within a driver!
1012  * @ctrl_handler: The control handler of this subdev. May be NULL.
1013  * @name: Name of the sub-device. Please notice that the name must be unique.
1014  * @grp_id: can be used to group similar subdevs. Value is driver-specific
1015  * @dev_priv: pointer to private data
1016  * @host_priv: pointer to private data used by the device where the subdev
1017  *	is attached.
1018  * @devnode: subdev device node
1019  * @dev: pointer to the physical device, if any
1020  * @fwnode: The fwnode_handle of the subdev, usually the same as
1021  *	    either dev->of_node->fwnode or dev->fwnode (whichever is non-NULL).
1022  * @async_list: Links this subdev to a global subdev_list or
1023  *		@notifier->done_list list.
1024  * @async_subdev_endpoint_list: List entry in async_subdev_endpoint_entry of
1025  *				&struct v4l2_async_subdev_endpoint.
1026  * @subdev_notifier: A sub-device notifier implicitly registered for the sub-
1027  *		     device using v4l2_async_register_subdev_sensor().
1028  * @asc_list: Async connection list, of &struct
1029  *	      v4l2_async_connection.subdev_entry.
1030  * @pdata: common part of subdevice platform data
1031  * @state_lock: A pointer to a lock used for all the subdev's states, set by the
1032  *		driver. This is	optional. If NULL, each state instance will get
1033  *		a lock of its own.
1034  * @privacy_led: Optional pointer to a LED classdev for the privacy LED for sensors.
1035  * @active_state: Active state for the subdev (NULL for subdevs tracking the
1036  *		  state internally). Initialized by calling
1037  *		  v4l2_subdev_init_finalize().
1038  * @enabled_pads: Bitmask of enabled pads used by v4l2_subdev_enable_streams()
1039  *		  and v4l2_subdev_disable_streams() helper functions for
1040  *		  fallback cases.
1041  * @s_stream_enabled: Tracks whether streaming has been enabled with s_stream.
1042  *                    This is only for call_s_stream() internal use.
1043  *
1044  * Each instance of a subdev driver should create this struct, either
1045  * stand-alone or embedded in a larger struct.
1046  *
1047  * This structure should be initialized by v4l2_subdev_init() or one of
1048  * its variants: v4l2_spi_subdev_init(), v4l2_i2c_subdev_init().
1049  */
1050 struct v4l2_subdev {
1051 #if defined(CONFIG_MEDIA_CONTROLLER)
1052 	struct media_entity entity;
1053 #endif
1054 	struct list_head list;
1055 	struct module *owner;
1056 	bool owner_v4l2_dev;
1057 	u32 flags;
1058 	struct v4l2_device *v4l2_dev;
1059 	const struct v4l2_subdev_ops *ops;
1060 	const struct v4l2_subdev_internal_ops *internal_ops;
1061 	struct v4l2_ctrl_handler *ctrl_handler;
1062 	char name[52];
1063 	u32 grp_id;
1064 	void *dev_priv;
1065 	void *host_priv;
1066 	struct video_device *devnode;
1067 	struct device *dev;
1068 	struct fwnode_handle *fwnode;
1069 	struct list_head async_list;
1070 	struct list_head async_subdev_endpoint_list;
1071 	struct v4l2_async_notifier *subdev_notifier;
1072 	struct list_head asc_list;
1073 	struct v4l2_subdev_platform_data *pdata;
1074 	struct mutex *state_lock;
1075 
1076 	/*
1077 	 * The fields below are private, and should only be accessed via
1078 	 * appropriate functions.
1079 	 */
1080 
1081 	struct led_classdev *privacy_led;
1082 
1083 	/*
1084 	 * TODO: active_state should most likely be changed from a pointer to an
1085 	 * embedded field. For the time being it's kept as a pointer to more
1086 	 * easily catch uses of active_state in the cases where the driver
1087 	 * doesn't support it.
1088 	 */
1089 	struct v4l2_subdev_state *active_state;
1090 	u64 enabled_pads;
1091 	bool s_stream_enabled;
1092 };
1093 
1094 
1095 /**
1096  * media_entity_to_v4l2_subdev - Returns a &struct v4l2_subdev from
1097  *    the &struct media_entity embedded in it.
1098  *
1099  * @ent: pointer to &struct media_entity.
1100  */
1101 #define media_entity_to_v4l2_subdev(ent)				\
1102 ({									\
1103 	typeof(ent) __me_sd_ent = (ent);				\
1104 									\
1105 	__me_sd_ent ?							\
1106 		container_of(__me_sd_ent, struct v4l2_subdev, entity) :	\
1107 		NULL;							\
1108 })
1109 
1110 /**
1111  * vdev_to_v4l2_subdev - Returns a &struct v4l2_subdev from
1112  *	the &struct video_device embedded on it.
1113  *
1114  * @vdev: pointer to &struct video_device
1115  */
1116 #define vdev_to_v4l2_subdev(vdev) \
1117 	((struct v4l2_subdev *)video_get_drvdata(vdev))
1118 
1119 /**
1120  * struct v4l2_subdev_fh - Used for storing subdev information per file handle
1121  *
1122  * @vfh: pointer to &struct v4l2_fh
1123  * @state: pointer to &struct v4l2_subdev_state
1124  * @owner: module pointer to the owner of this file handle
1125  * @client_caps: bitmask of ``V4L2_SUBDEV_CLIENT_CAP_*``
1126  */
1127 struct v4l2_subdev_fh {
1128 	struct v4l2_fh vfh;
1129 	struct module *owner;
1130 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1131 	struct v4l2_subdev_state *state;
1132 	u64 client_caps;
1133 #endif
1134 };
1135 
1136 /**
1137  * to_v4l2_subdev_fh - Returns a &struct v4l2_subdev_fh from
1138  *	the &struct v4l2_fh embedded on it.
1139  *
1140  * @fh: pointer to &struct v4l2_fh
1141  */
1142 #define to_v4l2_subdev_fh(fh)	\
1143 	container_of(fh, struct v4l2_subdev_fh, vfh)
1144 
1145 extern const struct v4l2_file_operations v4l2_subdev_fops;
1146 
1147 /**
1148  * v4l2_set_subdevdata - Sets V4L2 dev private device data
1149  *
1150  * @sd: pointer to &struct v4l2_subdev
1151  * @p: pointer to the private device data to be stored.
1152  */
1153 static inline void v4l2_set_subdevdata(struct v4l2_subdev *sd, void *p)
1154 {
1155 	sd->dev_priv = p;
1156 }
1157 
1158 /**
1159  * v4l2_get_subdevdata - Gets V4L2 dev private device data
1160  *
1161  * @sd: pointer to &struct v4l2_subdev
1162  *
1163  * Returns the pointer to the private device data to be stored.
1164  */
1165 static inline void *v4l2_get_subdevdata(const struct v4l2_subdev *sd)
1166 {
1167 	return sd->dev_priv;
1168 }
1169 
1170 /**
1171  * v4l2_set_subdev_hostdata - Sets V4L2 dev private host data
1172  *
1173  * @sd: pointer to &struct v4l2_subdev
1174  * @p: pointer to the private data to be stored.
1175  */
1176 static inline void v4l2_set_subdev_hostdata(struct v4l2_subdev *sd, void *p)
1177 {
1178 	sd->host_priv = p;
1179 }
1180 
1181 /**
1182  * v4l2_get_subdev_hostdata - Gets V4L2 dev private data
1183  *
1184  * @sd: pointer to &struct v4l2_subdev
1185  *
1186  * Returns the pointer to the private host data to be stored.
1187  */
1188 static inline void *v4l2_get_subdev_hostdata(const struct v4l2_subdev *sd)
1189 {
1190 	return sd->host_priv;
1191 }
1192 
1193 #ifdef CONFIG_MEDIA_CONTROLLER
1194 
1195 /**
1196  * v4l2_subdev_get_fwnode_pad_1_to_1 - Get pad number from a subdev fwnode
1197  *                                     endpoint, assuming 1:1 port:pad
1198  *
1199  * @entity: Pointer to the subdev entity
1200  * @endpoint: Pointer to a parsed fwnode endpoint
1201  *
1202  * This function can be used as the .get_fwnode_pad operation for
1203  * subdevices that map port numbers and pad indexes 1:1. If the endpoint
1204  * is owned by the subdevice, the function returns the endpoint port
1205  * number.
1206  *
1207  * Returns the endpoint port number on success or a negative error code.
1208  */
1209 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
1210 				      struct fwnode_endpoint *endpoint);
1211 
1212 /**
1213  * v4l2_subdev_link_validate_default - validates a media link
1214  *
1215  * @sd: pointer to &struct v4l2_subdev
1216  * @link: pointer to &struct media_link
1217  * @source_fmt: pointer to &struct v4l2_subdev_format
1218  * @sink_fmt: pointer to &struct v4l2_subdev_format
1219  *
1220  * This function ensures that width, height and the media bus pixel
1221  * code are equal on both source and sink of the link.
1222  */
1223 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
1224 				      struct media_link *link,
1225 				      struct v4l2_subdev_format *source_fmt,
1226 				      struct v4l2_subdev_format *sink_fmt);
1227 
1228 /**
1229  * v4l2_subdev_link_validate - validates a media link
1230  *
1231  * @link: pointer to &struct media_link
1232  *
1233  * This function calls the subdev's link_validate ops to validate
1234  * if a media link is valid for streaming. It also internally
1235  * calls v4l2_subdev_link_validate_default() to ensure that
1236  * width, height and the media bus pixel code are equal on both
1237  * source and sink of the link.
1238  *
1239  * The function can be used as a drop-in &media_entity_ops.link_validate
1240  * implementation for v4l2_subdev instances. It supports all links between
1241  * subdevs, as well as links between subdevs and video devices, provided that
1242  * the video devices also implement their &media_entity_ops.link_validate
1243  * operation.
1244  */
1245 int v4l2_subdev_link_validate(struct media_link *link);
1246 
1247 /**
1248  * v4l2_subdev_has_pad_interdep - MC has_pad_interdep implementation for subdevs
1249  *
1250  * @entity: pointer to &struct media_entity
1251  * @pad0: pad number for the first pad
1252  * @pad1: pad number for the second pad
1253  *
1254  * This function is an implementation of the
1255  * media_entity_operations.has_pad_interdep operation for subdevs that
1256  * implement the multiplexed streams API (as indicated by the
1257  * V4L2_SUBDEV_FL_STREAMS subdev flag).
1258  *
1259  * It considers two pads interdependent if there is an active route between pad0
1260  * and pad1.
1261  */
1262 bool v4l2_subdev_has_pad_interdep(struct media_entity *entity,
1263 				  unsigned int pad0, unsigned int pad1);
1264 
1265 /**
1266  * __v4l2_subdev_state_alloc - allocate v4l2_subdev_state
1267  *
1268  * @sd: pointer to &struct v4l2_subdev for which the state is being allocated.
1269  * @lock_name: name of the state lock
1270  * @key: lock_class_key for the lock
1271  *
1272  * Must call __v4l2_subdev_state_free() when state is no longer needed.
1273  *
1274  * Not to be called directly by the drivers.
1275  */
1276 struct v4l2_subdev_state *__v4l2_subdev_state_alloc(struct v4l2_subdev *sd,
1277 						    const char *lock_name,
1278 						    struct lock_class_key *key);
1279 
1280 /**
1281  * __v4l2_subdev_state_free - free a v4l2_subdev_state
1282  *
1283  * @state: v4l2_subdev_state to be freed.
1284  *
1285  * Not to be called directly by the drivers.
1286  */
1287 void __v4l2_subdev_state_free(struct v4l2_subdev_state *state);
1288 
1289 /**
1290  * v4l2_subdev_init_finalize() - Finalizes the initialization of the subdevice
1291  * @sd: The subdev
1292  *
1293  * This function finalizes the initialization of the subdev, including
1294  * allocation of the active state for the subdev.
1295  *
1296  * This function must be called by the subdev drivers that use the centralized
1297  * active state, after the subdev struct has been initialized and
1298  * media_entity_pads_init() has been called, but before registering the
1299  * subdev.
1300  *
1301  * The user must call v4l2_subdev_cleanup() when the subdev is being removed.
1302  */
1303 #define v4l2_subdev_init_finalize(sd)                                          \
1304 	({                                                                     \
1305 		static struct lock_class_key __key;                            \
1306 		const char *name = KBUILD_BASENAME                             \
1307 			":" __stringify(__LINE__) ":sd->active_state->lock";   \
1308 		__v4l2_subdev_init_finalize(sd, name, &__key);                 \
1309 	})
1310 
1311 int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
1312 				struct lock_class_key *key);
1313 
1314 /**
1315  * v4l2_subdev_cleanup() - Releases the resources allocated by the subdevice
1316  * @sd: The subdevice
1317  *
1318  * Clean up a V4L2 async sub-device. Must be called for a sub-device as part of
1319  * its release if resources have been associated with it using
1320  * v4l2_async_subdev_endpoint_add() or v4l2_subdev_init_finalize().
1321  */
1322 void v4l2_subdev_cleanup(struct v4l2_subdev *sd);
1323 
1324 /*
1325  * A macro to generate the macro or function name for sub-devices state access
1326  * wrapper macros below.
1327  */
1328 #define __v4l2_subdev_state_gen_call(NAME, _1, ARG, ...)	\
1329 	__v4l2_subdev_state_get_ ## NAME ## ARG
1330 
1331 /*
1332  * A macro to constify the return value of the state accessors when the state
1333  * parameter is const.
1334  */
1335 #define __v4l2_subdev_state_constify_ret(state, value)				\
1336 	_Generic(state,								\
1337 		const struct v4l2_subdev_state *: (const typeof(*(value)) *)(value), \
1338 		struct v4l2_subdev_state *: (value)				\
1339 	)
1340 
1341 /**
1342  * v4l2_subdev_state_get_format() - Get pointer to a stream format
1343  * @state: subdevice state
1344  * @pad: pad id
1345  * @...: stream id (optional argument)
1346  *
1347  * This returns a pointer to &struct v4l2_mbus_framefmt for the given pad +
1348  * stream in the subdev state.
1349  *
1350  * For stream-unaware drivers the format for the corresponding pad is returned.
1351  * If the pad does not exist, NULL is returned.
1352  */
1353 /*
1354  * Wrap v4l2_subdev_state_get_format(), allowing the function to be called with
1355  * two or three arguments. The purpose of the __v4l2_subdev_state_gen_call()
1356  * macro is to come up with the name of the function or macro to call, using
1357  * the last two arguments (_stream and _pad). The selected function or macro is
1358  * then called using the arguments specified by the caller. The
1359  * __v4l2_subdev_state_constify_ret() macro constifies the returned pointer
1360  * when the state is const, allowing the state accessors to guarantee
1361  * const-correctness in all cases.
1362  *
1363  * A similar arrangement is used for v4l2_subdev_state_crop(),
1364  * v4l2_subdev_state_compose() and v4l2_subdev_state_get_interval() below.
1365  */
1366 #define v4l2_subdev_state_get_format(state, pad, ...)				\
1367 	__v4l2_subdev_state_constify_ret(state,					\
1368 		__v4l2_subdev_state_gen_call(format, ##__VA_ARGS__, , _pad)	\
1369 			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
1370 #define __v4l2_subdev_state_get_format_pad(state, pad)	\
1371 	__v4l2_subdev_state_get_format(state, pad, 0)
1372 struct v4l2_mbus_framefmt *
1373 __v4l2_subdev_state_get_format(struct v4l2_subdev_state *state,
1374 			       unsigned int pad, u32 stream);
1375 
1376 /**
1377  * v4l2_subdev_state_get_crop() - Get pointer to a stream crop rectangle
1378  * @state: subdevice state
1379  * @pad: pad id
1380  * @...: stream id (optional argument)
1381  *
1382  * This returns a pointer to crop rectangle for the given pad + stream in the
1383  * subdev state.
1384  *
1385  * For stream-unaware drivers the crop rectangle for the corresponding pad is
1386  * returned. If the pad does not exist, NULL is returned.
1387  */
1388 #define v4l2_subdev_state_get_crop(state, pad, ...)				\
1389 	__v4l2_subdev_state_constify_ret(state,					\
1390 		__v4l2_subdev_state_gen_call(crop, ##__VA_ARGS__, , _pad)	\
1391 			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
1392 #define __v4l2_subdev_state_get_crop_pad(state, pad)	\
1393 	__v4l2_subdev_state_get_crop(state, pad, 0)
1394 struct v4l2_rect *
1395 __v4l2_subdev_state_get_crop(struct v4l2_subdev_state *state, unsigned int pad,
1396 			     u32 stream);
1397 
1398 /**
1399  * v4l2_subdev_state_get_compose() - Get pointer to a stream compose rectangle
1400  * @state: subdevice state
1401  * @pad: pad id
1402  * @...: stream id (optional argument)
1403  *
1404  * This returns a pointer to compose rectangle for the given pad + stream in the
1405  * subdev state.
1406  *
1407  * For stream-unaware drivers the compose rectangle for the corresponding pad is
1408  * returned. If the pad does not exist, NULL is returned.
1409  */
1410 #define v4l2_subdev_state_get_compose(state, pad, ...)				\
1411 	__v4l2_subdev_state_constify_ret(state,					\
1412 		__v4l2_subdev_state_gen_call(compose, ##__VA_ARGS__, , _pad)	\
1413 			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
1414 #define __v4l2_subdev_state_get_compose_pad(state, pad)	\
1415 	__v4l2_subdev_state_get_compose(state, pad, 0)
1416 struct v4l2_rect *
1417 __v4l2_subdev_state_get_compose(struct v4l2_subdev_state *state,
1418 				unsigned int pad, u32 stream);
1419 
1420 /**
1421  * v4l2_subdev_state_get_interval() - Get pointer to a stream frame interval
1422  * @state: subdevice state
1423  * @pad: pad id
1424  * @...: stream id (optional argument)
1425  *
1426  * This returns a pointer to the frame interval for the given pad + stream in
1427  * the subdev state.
1428  *
1429  * For stream-unaware drivers the frame interval for the corresponding pad is
1430  * returned. If the pad does not exist, NULL is returned.
1431  */
1432 #define v4l2_subdev_state_get_interval(state, pad, ...)				\
1433 	__v4l2_subdev_state_constify_ret(state,					\
1434 		__v4l2_subdev_state_gen_call(interval, ##__VA_ARGS__, , _pad)	\
1435 			((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
1436 #define __v4l2_subdev_state_get_interval_pad(state, pad)	\
1437 	__v4l2_subdev_state_get_interval(state, pad, 0)
1438 struct v4l2_fract *
1439 __v4l2_subdev_state_get_interval(struct v4l2_subdev_state *state,
1440 				 unsigned int pad, u32 stream);
1441 
1442 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1443 
1444 /**
1445  * v4l2_subdev_get_fmt() - Fill format based on state
1446  * @sd: subdevice
1447  * @state: subdevice state
1448  * @format: pointer to &struct v4l2_subdev_format
1449  *
1450  * Fill @format->format field based on the information in the @format struct.
1451  *
1452  * This function can be used by the subdev drivers which support active state to
1453  * implement v4l2_subdev_pad_ops.get_fmt if the subdev driver does not need to
1454  * do anything special in their get_fmt op.
1455  *
1456  * Returns 0 on success, error value otherwise.
1457  */
1458 int v4l2_subdev_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
1459 			struct v4l2_subdev_format *format);
1460 
1461 /**
1462  * v4l2_subdev_get_frame_interval() - Fill frame interval based on state
1463  * @sd: subdevice
1464  * @state: subdevice state
1465  * @fi: pointer to &struct v4l2_subdev_frame_interval
1466  *
1467  * Fill @fi->interval field based on the information in the @fi struct.
1468  *
1469  * This function can be used by the subdev drivers which support active state to
1470  * implement v4l2_subdev_pad_ops.get_frame_interval if the subdev driver does
1471  * not need to do anything special in their get_frame_interval op.
1472  *
1473  * Returns 0 on success, error value otherwise.
1474  */
1475 int v4l2_subdev_get_frame_interval(struct v4l2_subdev *sd,
1476 				   struct v4l2_subdev_state *state,
1477 				   struct v4l2_subdev_frame_interval *fi);
1478 
1479 /**
1480  * v4l2_subdev_set_routing() - Set given routing to subdev state
1481  * @sd: The subdevice
1482  * @state: The subdevice state
1483  * @routing: Routing that will be copied to subdev state
1484  *
1485  * This will release old routing table (if any) from the state, allocate
1486  * enough space for the given routing, and copy the routing.
1487  *
1488  * This can be used from the subdev driver's set_routing op, after validating
1489  * the routing.
1490  */
1491 int v4l2_subdev_set_routing(struct v4l2_subdev *sd,
1492 			    struct v4l2_subdev_state *state,
1493 			    const struct v4l2_subdev_krouting *routing);
1494 
1495 struct v4l2_subdev_route *
1496 __v4l2_subdev_next_active_route(const struct v4l2_subdev_krouting *routing,
1497 				struct v4l2_subdev_route *route);
1498 
1499 /**
1500  * for_each_active_route - iterate on all active routes of a routing table
1501  * @routing: The routing table
1502  * @route: The route iterator
1503  */
1504 #define for_each_active_route(routing, route) \
1505 	for ((route) = NULL;                  \
1506 	     ((route) = __v4l2_subdev_next_active_route((routing), (route)));)
1507 
1508 /**
1509  * v4l2_subdev_set_routing_with_fmt() - Set given routing and format to subdev
1510  *					state
1511  * @sd: The subdevice
1512  * @state: The subdevice state
1513  * @routing: Routing that will be copied to subdev state
1514  * @fmt: Format used to initialize all the streams
1515  *
1516  * This is the same as v4l2_subdev_set_routing, but additionally initializes
1517  * all the streams using the given format.
1518  */
1519 int v4l2_subdev_set_routing_with_fmt(struct v4l2_subdev *sd,
1520 				     struct v4l2_subdev_state *state,
1521 				     const struct v4l2_subdev_krouting *routing,
1522 				     const struct v4l2_mbus_framefmt *fmt);
1523 
1524 /**
1525  * v4l2_subdev_routing_find_opposite_end() - Find the opposite stream
1526  * @routing: routing used to find the opposite side
1527  * @pad: pad id
1528  * @stream: stream id
1529  * @other_pad: pointer used to return the opposite pad
1530  * @other_stream: pointer used to return the opposite stream
1531  *
1532  * This function uses the routing table to find the pad + stream which is
1533  * opposite the given pad + stream.
1534  *
1535  * @other_pad and/or @other_stream can be NULL if the caller does not need the
1536  * value.
1537  *
1538  * Returns 0 on success, or -EINVAL if no matching route is found.
1539  */
1540 int v4l2_subdev_routing_find_opposite_end(const struct v4l2_subdev_krouting *routing,
1541 					  u32 pad, u32 stream, u32 *other_pad,
1542 					  u32 *other_stream);
1543 
1544 /**
1545  * v4l2_subdev_state_get_opposite_stream_format() - Get pointer to opposite
1546  *                                                  stream format
1547  * @state: subdevice state
1548  * @pad: pad id
1549  * @stream: stream id
1550  *
1551  * This returns a pointer to &struct v4l2_mbus_framefmt for the pad + stream
1552  * that is opposite the given pad + stream in the subdev state.
1553  *
1554  * If the state does not contain the given pad + stream, NULL is returned.
1555  */
1556 struct v4l2_mbus_framefmt *
1557 v4l2_subdev_state_get_opposite_stream_format(struct v4l2_subdev_state *state,
1558 					     u32 pad, u32 stream);
1559 
1560 /**
1561  * v4l2_subdev_state_xlate_streams() - Translate streams from one pad to another
1562  *
1563  * @state: Subdevice state
1564  * @pad0: The first pad
1565  * @pad1: The second pad
1566  * @streams: Streams bitmask on the first pad
1567  *
1568  * Streams on sink pads of a subdev are routed to source pads as expressed in
1569  * the subdev state routing table. Stream numbers don't necessarily match on
1570  * the sink and source side of a route. This function translates stream numbers
1571  * on @pad0, expressed as a bitmask in @streams, to the corresponding streams
1572  * on @pad1 using the routing table from the @state. It returns the stream mask
1573  * on @pad1, and updates @streams with the streams that have been found in the
1574  * routing table.
1575  *
1576  * @pad0 and @pad1 must be a sink and a source, in any order.
1577  *
1578  * Return: The bitmask of streams of @pad1 that are routed to @streams on @pad0.
1579  */
1580 u64 v4l2_subdev_state_xlate_streams(const struct v4l2_subdev_state *state,
1581 				    u32 pad0, u32 pad1, u64 *streams);
1582 
1583 /**
1584  * enum v4l2_subdev_routing_restriction - Subdevice internal routing restrictions
1585  *
1586  * @V4L2_SUBDEV_ROUTING_NO_1_TO_N:
1587  *	an input stream shall not be routed to multiple output streams (stream
1588  *	duplication)
1589  * @V4L2_SUBDEV_ROUTING_NO_N_TO_1:
1590  *	multiple input streams shall not be routed to the same output stream
1591  *	(stream merging)
1592  * @V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX:
1593  *	all streams from a sink pad must be routed to a single source pad
1594  * @V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX:
1595  *	all streams on a source pad must originate from a single sink pad
1596  * @V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING:
1597  *	source pads shall not contain multiplexed streams
1598  * @V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING:
1599  *	sink pads shall not contain multiplexed streams
1600  * @V4L2_SUBDEV_ROUTING_ONLY_1_TO_1:
1601  *	only non-overlapping 1-to-1 stream routing is allowed (a combination of
1602  *	@V4L2_SUBDEV_ROUTING_NO_1_TO_N and @V4L2_SUBDEV_ROUTING_NO_N_TO_1)
1603  * @V4L2_SUBDEV_ROUTING_NO_STREAM_MIX:
1604  *	all streams from a sink pad must be routed to a single source pad, and
1605  *	that source pad shall not get routes from any other sink pad
1606  *	(a combination of @V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX and
1607  *	@V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX)
1608  * @V4L2_SUBDEV_ROUTING_NO_MULTIPLEXING:
1609  *	no multiplexed streams allowed on either source or sink sides.
1610  */
1611 enum v4l2_subdev_routing_restriction {
1612 	V4L2_SUBDEV_ROUTING_NO_1_TO_N = BIT(0),
1613 	V4L2_SUBDEV_ROUTING_NO_N_TO_1 = BIT(1),
1614 	V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX = BIT(2),
1615 	V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX = BIT(3),
1616 	V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING = BIT(4),
1617 	V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING = BIT(5),
1618 	V4L2_SUBDEV_ROUTING_ONLY_1_TO_1 =
1619 		V4L2_SUBDEV_ROUTING_NO_1_TO_N |
1620 		V4L2_SUBDEV_ROUTING_NO_N_TO_1,
1621 	V4L2_SUBDEV_ROUTING_NO_STREAM_MIX =
1622 		V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX |
1623 		V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX,
1624 	V4L2_SUBDEV_ROUTING_NO_MULTIPLEXING =
1625 		V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING |
1626 		V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING,
1627 };
1628 
1629 /**
1630  * v4l2_subdev_routing_validate() - Verify that routes comply with driver
1631  *				    constraints
1632  * @sd: The subdevice
1633  * @routing: Routing to verify
1634  * @disallow: Restrictions on routes
1635  *
1636  * This verifies that the given routing complies with the @disallow constraints.
1637  *
1638  * Returns 0 on success, error value otherwise.
1639  */
1640 int v4l2_subdev_routing_validate(struct v4l2_subdev *sd,
1641 				 const struct v4l2_subdev_krouting *routing,
1642 				 enum v4l2_subdev_routing_restriction disallow);
1643 
1644 /**
1645  * v4l2_subdev_enable_streams() - Enable streams on a pad
1646  * @sd: The subdevice
1647  * @pad: The pad
1648  * @streams_mask: Bitmask of streams to enable
1649  *
1650  * This function enables streams on a source @pad of a subdevice. The pad is
1651  * identified by its index, while the streams are identified by the
1652  * @streams_mask bitmask. This allows enabling multiple streams on a pad at
1653  * once.
1654  *
1655  * Enabling a stream that is already enabled isn't allowed. If @streams_mask
1656  * contains an already enabled stream, this function returns -EALREADY without
1657  * performing any operation.
1658  *
1659  * Per-stream enable is only available for subdevs that implement the
1660  * .enable_streams() and .disable_streams() operations. For other subdevs, this
1661  * function implements a best-effort compatibility by calling the .s_stream()
1662  * operation, limited to subdevs that have a single source pad.
1663  *
1664  * Drivers that are not stream-aware shall set @streams_mask to BIT_ULL(0).
1665  *
1666  * Return:
1667  * * 0: Success
1668  * * -EALREADY: One of the streams in streams_mask is already enabled
1669  * * -EINVAL: The pad index is invalid, or doesn't correspond to a source pad
1670  * * -EOPNOTSUPP: Falling back to the legacy .s_stream() operation is
1671  *   impossible because the subdev has multiple source pads
1672  */
1673 int v4l2_subdev_enable_streams(struct v4l2_subdev *sd, u32 pad,
1674 			       u64 streams_mask);
1675 
1676 /**
1677  * v4l2_subdev_disable_streams() - Disable streams on a pad
1678  * @sd: The subdevice
1679  * @pad: The pad
1680  * @streams_mask: Bitmask of streams to disable
1681  *
1682  * This function disables streams on a source @pad of a subdevice. The pad is
1683  * identified by its index, while the streams are identified by the
1684  * @streams_mask bitmask. This allows disabling multiple streams on a pad at
1685  * once.
1686  *
1687  * Disabling a streams that is not enabled isn't allowed. If @streams_mask
1688  * contains a disabled stream, this function returns -EALREADY without
1689  * performing any operation.
1690  *
1691  * Per-stream disable is only available for subdevs that implement the
1692  * .enable_streams() and .disable_streams() operations. For other subdevs, this
1693  * function implements a best-effort compatibility by calling the .s_stream()
1694  * operation, limited to subdevs that have a single source pad.
1695  *
1696  * Drivers that are not stream-aware shall set @streams_mask to BIT_ULL(0).
1697  *
1698  * Return:
1699  * * 0: Success
1700  * * -EALREADY: One of the streams in streams_mask is not enabled
1701  * * -EINVAL: The pad index is invalid, or doesn't correspond to a source pad
1702  * * -EOPNOTSUPP: Falling back to the legacy .s_stream() operation is
1703  *   impossible because the subdev has multiple source pads
1704  */
1705 int v4l2_subdev_disable_streams(struct v4l2_subdev *sd, u32 pad,
1706 				u64 streams_mask);
1707 
1708 /**
1709  * v4l2_subdev_s_stream_helper() - Helper to implement the subdev s_stream
1710  *	operation using enable_streams and disable_streams
1711  * @sd: The subdevice
1712  * @enable: Enable or disable streaming
1713  *
1714  * Subdevice drivers that implement the streams-aware
1715  * &v4l2_subdev_pad_ops.enable_streams and &v4l2_subdev_pad_ops.disable_streams
1716  * operations can use this helper to implement the legacy
1717  * &v4l2_subdev_video_ops.s_stream operation.
1718  *
1719  * This helper can only be used by subdevs that have a single source pad.
1720  *
1721  * Return: 0 on success, or a negative error code otherwise.
1722  */
1723 int v4l2_subdev_s_stream_helper(struct v4l2_subdev *sd, int enable);
1724 
1725 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1726 
1727 #endif /* CONFIG_MEDIA_CONTROLLER */
1728 
1729 /**
1730  * v4l2_subdev_lock_state() - Locks the subdev state
1731  * @state: The subdevice state
1732  *
1733  * Locks the given subdev state.
1734  *
1735  * The state must be unlocked with v4l2_subdev_unlock_state() after use.
1736  */
1737 static inline void v4l2_subdev_lock_state(struct v4l2_subdev_state *state)
1738 {
1739 	mutex_lock(state->lock);
1740 }
1741 
1742 /**
1743  * v4l2_subdev_unlock_state() - Unlocks the subdev state
1744  * @state: The subdevice state
1745  *
1746  * Unlocks the given subdev state.
1747  */
1748 static inline void v4l2_subdev_unlock_state(struct v4l2_subdev_state *state)
1749 {
1750 	mutex_unlock(state->lock);
1751 }
1752 
1753 /**
1754  * v4l2_subdev_lock_states - Lock two sub-device states
1755  * @state1: One subdevice state
1756  * @state2: The other subdevice state
1757  *
1758  * Locks the state of two sub-devices.
1759  *
1760  * The states must be unlocked with v4l2_subdev_unlock_states() after use.
1761  *
1762  * This differs from calling v4l2_subdev_lock_state() on both states so that if
1763  * the states share the same lock, the lock is acquired only once (so no
1764  * deadlock occurs). The caller is responsible for ensuring the locks will
1765  * always be acquired in the same order.
1766  */
1767 static inline void v4l2_subdev_lock_states(struct v4l2_subdev_state *state1,
1768 					   struct v4l2_subdev_state *state2)
1769 {
1770 	mutex_lock(state1->lock);
1771 	if (state1->lock != state2->lock)
1772 		mutex_lock(state2->lock);
1773 }
1774 
1775 /**
1776  * v4l2_subdev_unlock_states() - Unlock two sub-device states
1777  * @state1: One subdevice state
1778  * @state2: The other subdevice state
1779  *
1780  * Unlocks the state of two sub-devices.
1781  *
1782  * This differs from calling v4l2_subdev_unlock_state() on both states so that
1783  * if the states share the same lock, the lock is released only once.
1784  */
1785 static inline void v4l2_subdev_unlock_states(struct v4l2_subdev_state *state1,
1786 					     struct v4l2_subdev_state *state2)
1787 {
1788 	mutex_unlock(state1->lock);
1789 	if (state1->lock != state2->lock)
1790 		mutex_unlock(state2->lock);
1791 }
1792 
1793 /**
1794  * v4l2_subdev_get_unlocked_active_state() - Checks that the active subdev state
1795  *					     is unlocked and returns it
1796  * @sd: The subdevice
1797  *
1798  * Returns the active state for the subdevice, or NULL if the subdev does not
1799  * support active state. If the state is not NULL, calls
1800  * lockdep_assert_not_held() to issue a warning if the state is locked.
1801  *
1802  * This function is to be used e.g. when getting the active state for the sole
1803  * purpose of passing it forward, without accessing the state fields.
1804  */
1805 static inline struct v4l2_subdev_state *
1806 v4l2_subdev_get_unlocked_active_state(struct v4l2_subdev *sd)
1807 {
1808 	if (sd->active_state)
1809 		lockdep_assert_not_held(sd->active_state->lock);
1810 	return sd->active_state;
1811 }
1812 
1813 /**
1814  * v4l2_subdev_get_locked_active_state() - Checks that the active subdev state
1815  *					   is locked and returns it
1816  *
1817  * @sd: The subdevice
1818  *
1819  * Returns the active state for the subdevice, or NULL if the subdev does not
1820  * support active state. If the state is not NULL, calls lockdep_assert_held()
1821  * to issue a warning if the state is not locked.
1822  *
1823  * This function is to be used when the caller knows that the active state is
1824  * already locked.
1825  */
1826 static inline struct v4l2_subdev_state *
1827 v4l2_subdev_get_locked_active_state(struct v4l2_subdev *sd)
1828 {
1829 	if (sd->active_state)
1830 		lockdep_assert_held(sd->active_state->lock);
1831 	return sd->active_state;
1832 }
1833 
1834 /**
1835  * v4l2_subdev_lock_and_get_active_state() - Locks and returns the active subdev
1836  *					     state for the subdevice
1837  * @sd: The subdevice
1838  *
1839  * Returns the locked active state for the subdevice, or NULL if the subdev
1840  * does not support active state.
1841  *
1842  * The state must be unlocked with v4l2_subdev_unlock_state() after use.
1843  */
1844 static inline struct v4l2_subdev_state *
1845 v4l2_subdev_lock_and_get_active_state(struct v4l2_subdev *sd)
1846 {
1847 	if (sd->active_state)
1848 		v4l2_subdev_lock_state(sd->active_state);
1849 	return sd->active_state;
1850 }
1851 
1852 /**
1853  * v4l2_subdev_init - initializes the sub-device struct
1854  *
1855  * @sd: pointer to the &struct v4l2_subdev to be initialized
1856  * @ops: pointer to &struct v4l2_subdev_ops.
1857  */
1858 void v4l2_subdev_init(struct v4l2_subdev *sd,
1859 		      const struct v4l2_subdev_ops *ops);
1860 
1861 extern const struct v4l2_subdev_ops v4l2_subdev_call_wrappers;
1862 
1863 /**
1864  * v4l2_subdev_call - call an operation of a v4l2_subdev.
1865  *
1866  * @sd: pointer to the &struct v4l2_subdev
1867  * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
1868  *     Each element there groups a set of callbacks functions.
1869  * @f: callback function to be called.
1870  *     The callback functions are defined in groups, according to
1871  *     each element at &struct v4l2_subdev_ops.
1872  * @args: arguments for @f.
1873  *
1874  * Example: err = v4l2_subdev_call(sd, video, s_std, norm);
1875  */
1876 #define v4l2_subdev_call(sd, o, f, args...)				\
1877 	({								\
1878 		struct v4l2_subdev *__sd = (sd);			\
1879 		int __result;						\
1880 		if (!__sd)						\
1881 			__result = -ENODEV;				\
1882 		else if (!(__sd->ops->o && __sd->ops->o->f))		\
1883 			__result = -ENOIOCTLCMD;			\
1884 		else if (v4l2_subdev_call_wrappers.o &&			\
1885 			 v4l2_subdev_call_wrappers.o->f)		\
1886 			__result = v4l2_subdev_call_wrappers.o->f(	\
1887 							__sd, ##args);	\
1888 		else							\
1889 			__result = __sd->ops->o->f(__sd, ##args);	\
1890 		__result;						\
1891 	})
1892 
1893 /**
1894  * v4l2_subdev_call_state_active - call an operation of a v4l2_subdev which
1895  *				   takes state as a parameter, passing the
1896  *				   subdev its active state.
1897  *
1898  * @sd: pointer to the &struct v4l2_subdev
1899  * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
1900  *     Each element there groups a set of callbacks functions.
1901  * @f: callback function to be called.
1902  *     The callback functions are defined in groups, according to
1903  *     each element at &struct v4l2_subdev_ops.
1904  * @args: arguments for @f.
1905  *
1906  * This is similar to v4l2_subdev_call(), except that this version can only be
1907  * used for ops that take a subdev state as a parameter. The macro will get the
1908  * active state, lock it before calling the op and unlock it after the call.
1909  */
1910 #define v4l2_subdev_call_state_active(sd, o, f, args...)		\
1911 	({								\
1912 		int __result;						\
1913 		struct v4l2_subdev_state *state;			\
1914 		state = v4l2_subdev_get_unlocked_active_state(sd);	\
1915 		if (state)						\
1916 			v4l2_subdev_lock_state(state);			\
1917 		__result = v4l2_subdev_call(sd, o, f, state, ##args);	\
1918 		if (state)						\
1919 			v4l2_subdev_unlock_state(state);		\
1920 		__result;						\
1921 	})
1922 
1923 /**
1924  * v4l2_subdev_call_state_try - call an operation of a v4l2_subdev which
1925  *				takes state as a parameter, passing the
1926  *				subdev a newly allocated try state.
1927  *
1928  * @sd: pointer to the &struct v4l2_subdev
1929  * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
1930  *     Each element there groups a set of callbacks functions.
1931  * @f: callback function to be called.
1932  *     The callback functions are defined in groups, according to
1933  *     each element at &struct v4l2_subdev_ops.
1934  * @args: arguments for @f.
1935  *
1936  * This is similar to v4l2_subdev_call_state_active(), except that as this
1937  * version allocates a new state, this is only usable for
1938  * V4L2_SUBDEV_FORMAT_TRY use cases.
1939  *
1940  * Note: only legacy non-MC drivers may need this macro.
1941  */
1942 #define v4l2_subdev_call_state_try(sd, o, f, args...)                         \
1943 	({                                                                    \
1944 		int __result;                                                 \
1945 		static struct lock_class_key __key;                           \
1946 		const char *name = KBUILD_BASENAME                            \
1947 			":" __stringify(__LINE__) ":state->lock";             \
1948 		struct v4l2_subdev_state *state =                             \
1949 			__v4l2_subdev_state_alloc(sd, name, &__key);          \
1950 		if (IS_ERR(state)) {                                          \
1951 			__result = PTR_ERR(state);                            \
1952 		} else {                                                      \
1953 			v4l2_subdev_lock_state(state);                        \
1954 			__result = v4l2_subdev_call(sd, o, f, state, ##args); \
1955 			v4l2_subdev_unlock_state(state);                      \
1956 			__v4l2_subdev_state_free(state);                      \
1957 		}                                                             \
1958 		__result;                                                     \
1959 	})
1960 
1961 /**
1962  * v4l2_subdev_has_op - Checks if a subdev defines a certain operation.
1963  *
1964  * @sd: pointer to the &struct v4l2_subdev
1965  * @o: The group of callback functions in &struct v4l2_subdev_ops
1966  * which @f is a part of.
1967  * @f: callback function to be checked for its existence.
1968  */
1969 #define v4l2_subdev_has_op(sd, o, f) \
1970 	((sd)->ops->o && (sd)->ops->o->f)
1971 
1972 /**
1973  * v4l2_subdev_notify_event() - Delivers event notification for subdevice
1974  * @sd: The subdev for which to deliver the event
1975  * @ev: The event to deliver
1976  *
1977  * Will deliver the specified event to all userspace event listeners which are
1978  * subscribed to the v42l subdev event queue as well as to the bridge driver
1979  * using the notify callback. The notification type for the notify callback
1980  * will be %V4L2_DEVICE_NOTIFY_EVENT.
1981  */
1982 void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
1983 			      const struct v4l2_event *ev);
1984 
1985 /**
1986  * v4l2_subdev_is_streaming() - Returns if the subdevice is streaming
1987  * @sd: The subdevice
1988  *
1989  * v4l2_subdev_is_streaming() tells if the subdevice is currently streaming.
1990  * "Streaming" here means whether .s_stream() or .enable_streams() has been
1991  * successfully called, and the streaming has not yet been disabled.
1992  *
1993  * If the subdevice implements .enable_streams() this function must be called
1994  * while holding the active state lock.
1995  */
1996 bool v4l2_subdev_is_streaming(struct v4l2_subdev *sd);
1997 
1998 #endif /* _V4L2_SUBDEV_H */
1999