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