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 * This callback is DEPRECATED. New drivers should instead implement
454 * &v4l2_subdev_pad_ops.enable_streams and
455 * &v4l2_subdev_pad_ops.disable_streams operations, and use
456 * v4l2_subdev_s_stream_helper for the &v4l2_subdev_video_ops.s_stream
457 * operation to support legacy users.
458 *
459 * Drivers should also not call the .s_stream() subdev operation directly,
460 * but use the v4l2_subdev_enable_streams() and
461 * v4l2_subdev_disable_streams() helpers.
462 *
463 * @g_pixelaspect: callback to return the pixelaspect ratio.
464 *
465 * @s_rx_buffer: set a host allocated memory buffer for the subdev. The subdev
466 * can adjust @size to a lower value and must not write more data to the
467 * buffer starting at @data than the original value of @size.
468 *
469 * @pre_streamon: May be called before streaming is actually started, to help
470 * initialising the bus. Current usage is to set a CSI-2 transmitter to
471 * LP-11 or LP-111 mode before streaming. See &enum
472 * v4l2_subdev_pre_streamon_flags.
473 *
474 * pre_streamon shall return error if it cannot perform the operation as
475 * indicated by the flags argument. In particular, -EACCES indicates lack
476 * of support for the operation. The caller shall call post_streamoff for
477 * each successful call of pre_streamon.
478 *
479 * @post_streamoff: Called after streaming is stopped, but if and only if
480 * pre_streamon was called earlier.
481 */
482 struct v4l2_subdev_video_ops {
483 int (*s_routing)(struct v4l2_subdev *sd, u32 input, u32 output, u32 config);
484 int (*s_crystal_freq)(struct v4l2_subdev *sd, u32 freq, u32 flags);
485 int (*g_std)(struct v4l2_subdev *sd, v4l2_std_id *norm);
486 int (*s_std)(struct v4l2_subdev *sd, v4l2_std_id norm);
487 int (*s_std_output)(struct v4l2_subdev *sd, v4l2_std_id std);
488 int (*g_std_output)(struct v4l2_subdev *sd, v4l2_std_id *std);
489 int (*querystd)(struct v4l2_subdev *sd, v4l2_std_id *std);
490 int (*g_tvnorms)(struct v4l2_subdev *sd, v4l2_std_id *std);
491 int (*g_tvnorms_output)(struct v4l2_subdev *sd, v4l2_std_id *std);
492 int (*g_input_status)(struct v4l2_subdev *sd, u32 *status);
493 int (*s_stream)(struct v4l2_subdev *sd, int enable);
494 int (*g_pixelaspect)(struct v4l2_subdev *sd, struct v4l2_fract *aspect);
495 int (*s_rx_buffer)(struct v4l2_subdev *sd, void *buf,
496 unsigned int *size);
497 int (*pre_streamon)(struct v4l2_subdev *sd, u32 flags);
498 int (*post_streamoff)(struct v4l2_subdev *sd);
499 };
500
501 /**
502 * struct v4l2_subdev_vbi_ops - Callbacks used when v4l device was opened
503 * in video mode via the vbi device node.
504 *
505 * @decode_vbi_line: video decoders that support sliced VBI need to implement
506 * this ioctl. Field p of the &struct v4l2_decode_vbi_line is set to the
507 * start of the VBI data that was generated by the decoder. The driver
508 * then parses the sliced VBI data and sets the other fields in the
509 * struct accordingly. The pointer p is updated to point to the start of
510 * the payload which can be copied verbatim into the data field of the
511 * &struct v4l2_sliced_vbi_data. If no valid VBI data was found, then the
512 * type field is set to 0 on return.
513 *
514 * @s_vbi_data: used to generate VBI signals on a video signal.
515 * &struct v4l2_sliced_vbi_data is filled with the data packets that
516 * should be output. Note that if you set the line field to 0, then that
517 * VBI signal is disabled. If no valid VBI data was found, then the type
518 * field is set to 0 on return.
519 *
520 * @g_vbi_data: used to obtain the sliced VBI packet from a readback register.
521 * Not all video decoders support this. If no data is available because
522 * the readback register contains invalid or erroneous data %-EIO is
523 * returned. Note that you must fill in the 'id' member and the 'field'
524 * member (to determine whether CC data from the first or second field
525 * should be obtained).
526 *
527 * @g_sliced_vbi_cap: callback for VIDIOC_G_SLICED_VBI_CAP() ioctl handler
528 * code.
529 *
530 * @s_raw_fmt: setup the video encoder/decoder for raw VBI.
531 *
532 * @g_sliced_fmt: retrieve the current sliced VBI settings.
533 *
534 * @s_sliced_fmt: setup the sliced VBI settings.
535 */
536 struct v4l2_subdev_vbi_ops {
537 int (*decode_vbi_line)(struct v4l2_subdev *sd, struct v4l2_decode_vbi_line *vbi_line);
538 int (*s_vbi_data)(struct v4l2_subdev *sd, const struct v4l2_sliced_vbi_data *vbi_data);
539 int (*g_vbi_data)(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_data *vbi_data);
540 int (*g_sliced_vbi_cap)(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_cap *cap);
541 int (*s_raw_fmt)(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt);
542 int (*g_sliced_fmt)(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *fmt);
543 int (*s_sliced_fmt)(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *fmt);
544 };
545
546 /**
547 * struct v4l2_subdev_sensor_ops - v4l2-subdev sensor operations
548 * @g_skip_top_lines: number of lines at the top of the image to be skipped.
549 * This is needed for some sensors, which always corrupt
550 * several top lines of the output image, or which send their
551 * metadata in them.
552 * @g_skip_frames: number of frames to skip at stream start. This is needed for
553 * buggy sensors that generate faulty frames when they are
554 * turned on.
555 */
556 struct v4l2_subdev_sensor_ops {
557 int (*g_skip_top_lines)(struct v4l2_subdev *sd, u32 *lines);
558 int (*g_skip_frames)(struct v4l2_subdev *sd, u32 *frames);
559 };
560
561 /**
562 * enum v4l2_subdev_ir_mode- describes the type of IR supported
563 *
564 * @V4L2_SUBDEV_IR_MODE_PULSE_WIDTH: IR uses struct ir_raw_event records
565 */
566 enum v4l2_subdev_ir_mode {
567 V4L2_SUBDEV_IR_MODE_PULSE_WIDTH,
568 };
569
570 /**
571 * struct v4l2_subdev_ir_parameters - Parameters for IR TX or TX
572 *
573 * @bytes_per_data_element: bytes per data element of data in read or
574 * write call.
575 * @mode: IR mode as defined by &enum v4l2_subdev_ir_mode.
576 * @enable: device is active if true
577 * @interrupt_enable: IR interrupts are enabled if true
578 * @shutdown: if true: set hardware to low/no power, false: normal mode
579 *
580 * @modulation: if true, it uses carrier, if false: baseband
581 * @max_pulse_width: maximum pulse width in ns, valid only for baseband signal
582 * @carrier_freq: carrier frequency in Hz, valid only for modulated signal
583 * @duty_cycle: duty cycle percentage, valid only for modulated signal
584 * @invert_level: invert signal level
585 *
586 * @invert_carrier_sense: Send 0/space as a carrier burst. used only in TX.
587 *
588 * @noise_filter_min_width: min time of a valid pulse, in ns. Used only for RX.
589 * @carrier_range_lower: Lower carrier range, in Hz, valid only for modulated
590 * signal. Used only for RX.
591 * @carrier_range_upper: Upper carrier range, in Hz, valid only for modulated
592 * signal. Used only for RX.
593 * @resolution: The receive resolution, in ns . Used only for RX.
594 */
595 struct v4l2_subdev_ir_parameters {
596 unsigned int bytes_per_data_element;
597 enum v4l2_subdev_ir_mode mode;
598
599 bool enable;
600 bool interrupt_enable;
601 bool shutdown;
602
603 bool modulation;
604 u32 max_pulse_width;
605 unsigned int carrier_freq;
606 unsigned int duty_cycle;
607 bool invert_level;
608
609 /* Tx only */
610 bool invert_carrier_sense;
611
612 /* Rx only */
613 u32 noise_filter_min_width;
614 unsigned int carrier_range_lower;
615 unsigned int carrier_range_upper;
616 u32 resolution;
617 };
618
619 /**
620 * struct v4l2_subdev_ir_ops - operations for IR subdevices
621 *
622 * @rx_read: Reads received codes or pulse width data.
623 * The semantics are similar to a non-blocking read() call.
624 * @rx_g_parameters: Get the current operating parameters and state of
625 * the IR receiver.
626 * @rx_s_parameters: Set the current operating parameters and state of
627 * the IR receiver. It is recommended to call
628 * [rt]x_g_parameters first to fill out the current state, and only change
629 * the fields that need to be changed. Upon return, the actual device
630 * operating parameters and state will be returned. Note that hardware
631 * limitations may prevent the actual settings from matching the requested
632 * settings - e.g. an actual carrier setting of 35,904 Hz when 36,000 Hz
633 * was requested. An exception is when the shutdown parameter is true.
634 * The last used operational parameters will be returned, but the actual
635 * state of the hardware be different to minimize power consumption and
636 * processing when shutdown is true.
637 *
638 * @tx_write: Writes codes or pulse width data for transmission.
639 * The semantics are similar to a non-blocking write() call.
640 * @tx_g_parameters: Get the current operating parameters and state of
641 * the IR transmitter.
642 * @tx_s_parameters: Set the current operating parameters and state of
643 * the IR transmitter. It is recommended to call
644 * [rt]x_g_parameters first to fill out the current state, and only change
645 * the fields that need to be changed. Upon return, the actual device
646 * operating parameters and state will be returned. Note that hardware
647 * limitations may prevent the actual settings from matching the requested
648 * settings - e.g. an actual carrier setting of 35,904 Hz when 36,000 Hz
649 * was requested. An exception is when the shutdown parameter is true.
650 * The last used operational parameters will be returned, but the actual
651 * state of the hardware be different to minimize power consumption and
652 * processing when shutdown is true.
653 */
654 struct v4l2_subdev_ir_ops {
655 /* Receiver */
656 int (*rx_read)(struct v4l2_subdev *sd, u8 *buf, size_t count,
657 ssize_t *num);
658
659 int (*rx_g_parameters)(struct v4l2_subdev *sd,
660 struct v4l2_subdev_ir_parameters *params);
661 int (*rx_s_parameters)(struct v4l2_subdev *sd,
662 struct v4l2_subdev_ir_parameters *params);
663
664 /* Transmitter */
665 int (*tx_write)(struct v4l2_subdev *sd, u8 *buf, size_t count,
666 ssize_t *num);
667
668 int (*tx_g_parameters)(struct v4l2_subdev *sd,
669 struct v4l2_subdev_ir_parameters *params);
670 int (*tx_s_parameters)(struct v4l2_subdev *sd,
671 struct v4l2_subdev_ir_parameters *params);
672 };
673
674 /**
675 * struct v4l2_subdev_pad_config - Used for storing subdev pad information.
676 *
677 * @format: &struct v4l2_mbus_framefmt
678 * @crop: &struct v4l2_rect to be used for crop
679 * @compose: &struct v4l2_rect to be used for compose
680 * @interval: frame interval
681 */
682 struct v4l2_subdev_pad_config {
683 struct v4l2_mbus_framefmt format;
684 struct v4l2_rect crop;
685 struct v4l2_rect compose;
686 struct v4l2_fract interval;
687 };
688
689 /**
690 * struct v4l2_subdev_stream_config - Used for storing stream configuration.
691 *
692 * @pad: pad number
693 * @stream: stream number
694 * @enabled: has the stream been enabled with v4l2_subdev_enable_streams()
695 * @fmt: &struct v4l2_mbus_framefmt
696 * @crop: &struct v4l2_rect to be used for crop
697 * @compose: &struct v4l2_rect to be used for compose
698 * @interval: frame interval
699 *
700 * This structure stores configuration for a stream.
701 */
702 struct v4l2_subdev_stream_config {
703 u32 pad;
704 u32 stream;
705 bool enabled;
706
707 struct v4l2_mbus_framefmt fmt;
708 struct v4l2_rect crop;
709 struct v4l2_rect compose;
710 struct v4l2_fract interval;
711 };
712
713 /**
714 * struct v4l2_subdev_stream_configs - A collection of stream configs.
715 *
716 * @num_configs: number of entries in @config.
717 * @configs: an array of &struct v4l2_subdev_stream_configs.
718 */
719 struct v4l2_subdev_stream_configs {
720 u32 num_configs;
721 struct v4l2_subdev_stream_config *configs;
722 };
723
724 /**
725 * struct v4l2_subdev_krouting - subdev routing table
726 *
727 * @len_routes: length of routes array, in routes
728 * @num_routes: number of routes
729 * @routes: &struct v4l2_subdev_route
730 *
731 * This structure contains the routing table for a subdev.
732 */
733 struct v4l2_subdev_krouting {
734 unsigned int len_routes;
735 unsigned int num_routes;
736 struct v4l2_subdev_route *routes;
737 };
738
739 /**
740 * struct v4l2_subdev_state - Used for storing subdev state information.
741 *
742 * @_lock: default for 'lock'
743 * @lock: mutex for the state. May be replaced by the user.
744 * @sd: the sub-device which the state is related to
745 * @pads: &struct v4l2_subdev_pad_config array
746 * @routing: routing table for the subdev
747 * @stream_configs: stream configurations (only for V4L2_SUBDEV_FL_STREAMS)
748 *
749 * This structure only needs to be passed to the pad op if the 'which' field
750 * of the main argument is set to %V4L2_SUBDEV_FORMAT_TRY. For
751 * %V4L2_SUBDEV_FORMAT_ACTIVE it is safe to pass %NULL.
752 */
753 struct v4l2_subdev_state {
754 /* lock for the struct v4l2_subdev_state fields */
755 struct mutex _lock;
756 struct mutex *lock;
757 struct v4l2_subdev *sd;
758 struct v4l2_subdev_pad_config *pads;
759 struct v4l2_subdev_krouting routing;
760 struct v4l2_subdev_stream_configs stream_configs;
761 };
762
763 /**
764 * struct v4l2_subdev_pad_ops - v4l2-subdev pad level operations
765 *
766 * @enum_mbus_code: callback for VIDIOC_SUBDEV_ENUM_MBUS_CODE() ioctl handler
767 * code.
768 * @enum_frame_size: callback for VIDIOC_SUBDEV_ENUM_FRAME_SIZE() ioctl handler
769 * code.
770 *
771 * @enum_frame_interval: callback for VIDIOC_SUBDEV_ENUM_FRAME_INTERVAL() ioctl
772 * handler code.
773 *
774 * @get_fmt: callback for VIDIOC_SUBDEV_G_FMT() ioctl handler code.
775 *
776 * @set_fmt: callback for VIDIOC_SUBDEV_S_FMT() ioctl handler code.
777 *
778 * @get_selection: callback for VIDIOC_SUBDEV_G_SELECTION() ioctl handler code.
779 *
780 * @set_selection: callback for VIDIOC_SUBDEV_S_SELECTION() ioctl handler code.
781 *
782 * @get_frame_interval: callback for VIDIOC_SUBDEV_G_FRAME_INTERVAL()
783 * ioctl handler code.
784 *
785 * @set_frame_interval: callback for VIDIOC_SUBDEV_S_FRAME_INTERVAL()
786 * ioctl handler code.
787 *
788 * @get_edid: callback for VIDIOC_SUBDEV_G_EDID() ioctl handler code.
789 *
790 * @set_edid: callback for VIDIOC_SUBDEV_S_EDID() ioctl handler code.
791 *
792 * @s_dv_timings: Set custom dv timings in the sub device. This is used
793 * when sub device is capable of setting detailed timing information
794 * in the hardware to generate/detect the video signal.
795 *
796 * @g_dv_timings: Get custom dv timings in the sub device.
797 *
798 * @query_dv_timings: callback for VIDIOC_QUERY_DV_TIMINGS() ioctl handler code.
799 *
800 * @dv_timings_cap: callback for VIDIOC_SUBDEV_DV_TIMINGS_CAP() ioctl handler
801 * code.
802 *
803 * @enum_dv_timings: callback for VIDIOC_SUBDEV_ENUM_DV_TIMINGS() ioctl handler
804 * code.
805 *
806 * @link_validate: used by the media controller code to check if the links
807 * that belongs to a pipeline can be used for stream.
808 *
809 * @get_frame_desc: get the current low level media bus frame parameters.
810 *
811 * @set_frame_desc: set the low level media bus frame parameters, @fd array
812 * may be adjusted by the subdev driver to device capabilities.
813 *
814 * @get_mbus_config: get the media bus configuration of a remote sub-device.
815 * The media bus configuration is usually retrieved from the
816 * firmware interface at sub-device probe time, immediately
817 * applied to the hardware and eventually adjusted by the
818 * driver. Remote sub-devices (usually video receivers) shall
819 * use this operation to query the transmitting end bus
820 * configuration in order to adjust their own one accordingly.
821 * Callers should make sure they get the most up-to-date as
822 * possible configuration from the remote end, likely calling
823 * this operation as close as possible to stream on time. The
824 * operation shall fail if the pad index it has been called on
825 * is not valid or in case of unrecoverable failures.
826 *
827 * @set_routing: Enable or disable data connection routes described in the
828 * subdevice routing table. Subdevs that implement this operation
829 * must set the V4L2_SUBDEV_FL_STREAMS flag.
830 *
831 * @enable_streams: Enable 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_enable_streams() instead.
836 *
837 * Drivers that support only a single stream without setting the
838 * V4L2_SUBDEV_CAP_STREAMS sub-device capability flag can ignore the mask
839 * argument.
840 *
841 * @disable_streams: Disable the streams defined in streams_mask on the given
842 * source pad. Subdevs that implement this operation must use the active
843 * state management provided by the subdev core (enabled through a call to
844 * v4l2_subdev_init_finalize() at initialization time). Do not call
845 * directly, use v4l2_subdev_disable_streams() instead.
846 *
847 * Drivers that support only a single stream without setting the
848 * V4L2_SUBDEV_CAP_STREAMS sub-device capability flag can ignore the mask
849 * argument.
850 */
851 struct v4l2_subdev_pad_ops {
852 int (*enum_mbus_code)(struct v4l2_subdev *sd,
853 struct v4l2_subdev_state *state,
854 struct v4l2_subdev_mbus_code_enum *code);
855 int (*enum_frame_size)(struct v4l2_subdev *sd,
856 struct v4l2_subdev_state *state,
857 struct v4l2_subdev_frame_size_enum *fse);
858 int (*enum_frame_interval)(struct v4l2_subdev *sd,
859 struct v4l2_subdev_state *state,
860 struct v4l2_subdev_frame_interval_enum *fie);
861 int (*get_fmt)(struct v4l2_subdev *sd,
862 struct v4l2_subdev_state *state,
863 struct v4l2_subdev_format *format);
864 int (*set_fmt)(struct v4l2_subdev *sd,
865 struct v4l2_subdev_state *state,
866 struct v4l2_subdev_format *format);
867 int (*get_selection)(struct v4l2_subdev *sd,
868 struct v4l2_subdev_state *state,
869 struct v4l2_subdev_selection *sel);
870 int (*set_selection)(struct v4l2_subdev *sd,
871 struct v4l2_subdev_state *state,
872 struct v4l2_subdev_selection *sel);
873 int (*get_frame_interval)(struct v4l2_subdev *sd,
874 struct v4l2_subdev_state *state,
875 struct v4l2_subdev_frame_interval *interval);
876 int (*set_frame_interval)(struct v4l2_subdev *sd,
877 struct v4l2_subdev_state *state,
878 struct v4l2_subdev_frame_interval *interval);
879 int (*get_edid)(struct v4l2_subdev *sd, struct v4l2_edid *edid);
880 int (*set_edid)(struct v4l2_subdev *sd, struct v4l2_edid *edid);
881 int (*s_dv_timings)(struct v4l2_subdev *sd, unsigned int pad,
882 struct v4l2_dv_timings *timings);
883 int (*g_dv_timings)(struct v4l2_subdev *sd, unsigned int pad,
884 struct v4l2_dv_timings *timings);
885 int (*query_dv_timings)(struct v4l2_subdev *sd, unsigned int pad,
886 struct v4l2_dv_timings *timings);
887 int (*dv_timings_cap)(struct v4l2_subdev *sd,
888 struct v4l2_dv_timings_cap *cap);
889 int (*enum_dv_timings)(struct v4l2_subdev *sd,
890 struct v4l2_enum_dv_timings *timings);
891 #ifdef CONFIG_MEDIA_CONTROLLER
892 int (*link_validate)(struct v4l2_subdev *sd, struct media_link *link,
893 struct v4l2_subdev_format *source_fmt,
894 struct v4l2_subdev_format *sink_fmt);
895 #endif /* CONFIG_MEDIA_CONTROLLER */
896 int (*get_frame_desc)(struct v4l2_subdev *sd, unsigned int pad,
897 struct v4l2_mbus_frame_desc *fd);
898 int (*set_frame_desc)(struct v4l2_subdev *sd, unsigned int pad,
899 struct v4l2_mbus_frame_desc *fd);
900 int (*get_mbus_config)(struct v4l2_subdev *sd, unsigned int pad,
901 struct v4l2_mbus_config *config);
902 int (*set_routing)(struct v4l2_subdev *sd,
903 struct v4l2_subdev_state *state,
904 enum v4l2_subdev_format_whence which,
905 struct v4l2_subdev_krouting *route);
906 int (*enable_streams)(struct v4l2_subdev *sd,
907 struct v4l2_subdev_state *state, u32 pad,
908 u64 streams_mask);
909 int (*disable_streams)(struct v4l2_subdev *sd,
910 struct v4l2_subdev_state *state, u32 pad,
911 u64 streams_mask);
912 };
913
914 /**
915 * struct v4l2_subdev_ops - Subdev operations
916 *
917 * @core: pointer to &struct v4l2_subdev_core_ops. Can be %NULL
918 * @tuner: pointer to &struct v4l2_subdev_tuner_ops. Can be %NULL
919 * @audio: pointer to &struct v4l2_subdev_audio_ops. Can be %NULL
920 * @video: pointer to &struct v4l2_subdev_video_ops. Can be %NULL
921 * @vbi: pointer to &struct v4l2_subdev_vbi_ops. Can be %NULL
922 * @ir: pointer to &struct v4l2_subdev_ir_ops. Can be %NULL
923 * @sensor: pointer to &struct v4l2_subdev_sensor_ops. Can be %NULL
924 * @pad: pointer to &struct v4l2_subdev_pad_ops. Can be %NULL
925 */
926 struct v4l2_subdev_ops {
927 const struct v4l2_subdev_core_ops *core;
928 const struct v4l2_subdev_tuner_ops *tuner;
929 const struct v4l2_subdev_audio_ops *audio;
930 const struct v4l2_subdev_video_ops *video;
931 const struct v4l2_subdev_vbi_ops *vbi;
932 const struct v4l2_subdev_ir_ops *ir;
933 const struct v4l2_subdev_sensor_ops *sensor;
934 const struct v4l2_subdev_pad_ops *pad;
935 };
936
937 /**
938 * struct v4l2_subdev_internal_ops - V4L2 subdev internal ops
939 *
940 * @init_state: initialize the subdev state to default values
941 *
942 * @registered: called when this subdev is registered. When called the v4l2_dev
943 * field is set to the correct v4l2_device.
944 *
945 * @unregistered: called when this subdev is unregistered. When called the
946 * v4l2_dev field is still set to the correct v4l2_device.
947 *
948 * @open: called when the subdev device node is opened by an application.
949 *
950 * @close: called when the subdev device node is closed. Please note that
951 * it is possible for @close to be called after @unregistered!
952 *
953 * @release: called when the last user of the subdev device is gone. This
954 * happens after the @unregistered callback and when the last open
955 * filehandle to the v4l-subdevX device node was closed. If no device
956 * node was created for this sub-device, then the @release callback
957 * is called right after the @unregistered callback.
958 * The @release callback is typically used to free the memory containing
959 * the v4l2_subdev structure. It is almost certainly required for any
960 * sub-device that sets the V4L2_SUBDEV_FL_HAS_DEVNODE flag.
961 *
962 * .. note::
963 * Never call this from drivers, only the v4l2 framework can call
964 * these ops.
965 */
966 struct v4l2_subdev_internal_ops {
967 int (*init_state)(struct v4l2_subdev *sd,
968 struct v4l2_subdev_state *state);
969 int (*registered)(struct v4l2_subdev *sd);
970 void (*unregistered)(struct v4l2_subdev *sd);
971 int (*open)(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh);
972 int (*close)(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh);
973 void (*release)(struct v4l2_subdev *sd);
974 };
975
976 /* Set this flag if this subdev is a i2c device. */
977 #define V4L2_SUBDEV_FL_IS_I2C (1U << 0)
978 /* Set this flag if this subdev is a spi device. */
979 #define V4L2_SUBDEV_FL_IS_SPI (1U << 1)
980 /* Set this flag if this subdev needs a device node. */
981 #define V4L2_SUBDEV_FL_HAS_DEVNODE (1U << 2)
982 /*
983 * Set this flag if this subdev generates events.
984 * Note controls can send events, thus drivers exposing controls
985 * should set this flag.
986 */
987 #define V4L2_SUBDEV_FL_HAS_EVENTS (1U << 3)
988 /*
989 * Set this flag if this subdev supports multiplexed streams. This means
990 * that the driver supports routing and handles the stream parameter in its
991 * v4l2_subdev_pad_ops handlers. More specifically, this means:
992 *
993 * - Centrally managed subdev active state is enabled
994 * - Legacy pad config is _not_ supported (state->pads is NULL)
995 * - Routing ioctls are available
996 * - Multiple streams per pad are supported
997 */
998 #define V4L2_SUBDEV_FL_STREAMS (1U << 4)
999
1000 struct regulator_bulk_data;
1001
1002 /**
1003 * struct v4l2_subdev_platform_data - regulators config struct
1004 *
1005 * @regulators: Optional regulators used to power on/off the subdevice
1006 * @num_regulators: Number of regululators
1007 * @host_priv: Per-subdevice data, specific for a certain video host device
1008 */
1009 struct v4l2_subdev_platform_data {
1010 struct regulator_bulk_data *regulators;
1011 int num_regulators;
1012
1013 void *host_priv;
1014 };
1015
1016 /**
1017 * struct v4l2_subdev - describes a V4L2 sub-device
1018 *
1019 * @entity: pointer to &struct media_entity
1020 * @list: List of sub-devices
1021 * @owner: The owner is the same as the driver's &struct device owner.
1022 * @owner_v4l2_dev: true if the &sd->owner matches the owner of @v4l2_dev->dev
1023 * owner. Initialized by v4l2_device_register_subdev().
1024 * @flags: subdev flags. Can be:
1025 * %V4L2_SUBDEV_FL_IS_I2C - Set this flag if this subdev is a i2c device;
1026 * %V4L2_SUBDEV_FL_IS_SPI - Set this flag if this subdev is a spi device;
1027 * %V4L2_SUBDEV_FL_HAS_DEVNODE - Set this flag if this subdev needs a
1028 * device node;
1029 * %V4L2_SUBDEV_FL_HAS_EVENTS - Set this flag if this subdev generates
1030 * events.
1031 *
1032 * @v4l2_dev: pointer to struct &v4l2_device
1033 * @ops: pointer to struct &v4l2_subdev_ops
1034 * @internal_ops: pointer to struct &v4l2_subdev_internal_ops.
1035 * Never call these internal ops from within a driver!
1036 * @ctrl_handler: The control handler of this subdev. May be NULL.
1037 * @name: Name of the sub-device. Please notice that the name must be unique.
1038 * @grp_id: can be used to group similar subdevs. Value is driver-specific
1039 * @dev_priv: pointer to private data
1040 * @host_priv: pointer to private data used by the device where the subdev
1041 * is attached.
1042 * @devnode: subdev device node
1043 * @dev: pointer to the physical device, if any
1044 * @fwnode: The fwnode_handle of the subdev, usually the same as
1045 * either dev->of_node->fwnode or dev->fwnode (whichever is non-NULL).
1046 * @async_list: Links this subdev to a global subdev_list or
1047 * @notifier->done_list list.
1048 * @async_subdev_endpoint_list: List entry in async_subdev_endpoint_entry of
1049 * &struct v4l2_async_subdev_endpoint.
1050 * @subdev_notifier: A sub-device notifier implicitly registered for the sub-
1051 * device using v4l2_async_register_subdev_sensor().
1052 * @asc_list: Async connection list, of &struct
1053 * v4l2_async_connection.subdev_entry.
1054 * @pdata: common part of subdevice platform data
1055 * @state_lock: A pointer to a lock used for all the subdev's states, set by the
1056 * driver. This is optional. If NULL, each state instance will get
1057 * a lock of its own.
1058 * @privacy_led: Optional pointer to a LED classdev for the privacy LED for sensors.
1059 * @active_state: Active state for the subdev (NULL for subdevs tracking the
1060 * state internally). Initialized by calling
1061 * v4l2_subdev_init_finalize().
1062 * @enabled_pads: Bitmask of enabled pads used by v4l2_subdev_enable_streams()
1063 * and v4l2_subdev_disable_streams() helper functions for
1064 * fallback cases.
1065 * @s_stream_enabled: Tracks whether streaming has been enabled with s_stream.
1066 * This is only for call_s_stream() internal use.
1067 *
1068 * Each instance of a subdev driver should create this struct, either
1069 * stand-alone or embedded in a larger struct.
1070 *
1071 * This structure should be initialized by v4l2_subdev_init() or one of
1072 * its variants: v4l2_spi_subdev_init(), v4l2_i2c_subdev_init().
1073 */
1074 struct v4l2_subdev {
1075 #if defined(CONFIG_MEDIA_CONTROLLER)
1076 struct media_entity entity;
1077 #endif
1078 struct list_head list;
1079 struct module *owner;
1080 bool owner_v4l2_dev;
1081 u32 flags;
1082 struct v4l2_device *v4l2_dev;
1083 const struct v4l2_subdev_ops *ops;
1084 const struct v4l2_subdev_internal_ops *internal_ops;
1085 struct v4l2_ctrl_handler *ctrl_handler;
1086 char name[52];
1087 u32 grp_id;
1088 void *dev_priv;
1089 void *host_priv;
1090 struct video_device *devnode;
1091 struct device *dev;
1092 struct fwnode_handle *fwnode;
1093 struct list_head async_list;
1094 struct list_head async_subdev_endpoint_list;
1095 struct v4l2_async_notifier *subdev_notifier;
1096 struct list_head asc_list;
1097 struct v4l2_subdev_platform_data *pdata;
1098 struct mutex *state_lock;
1099
1100 /*
1101 * The fields below are private, and should only be accessed via
1102 * appropriate functions.
1103 */
1104
1105 struct led_classdev *privacy_led;
1106
1107 /*
1108 * TODO: active_state should most likely be changed from a pointer to an
1109 * embedded field. For the time being it's kept as a pointer to more
1110 * easily catch uses of active_state in the cases where the driver
1111 * doesn't support it.
1112 */
1113 struct v4l2_subdev_state *active_state;
1114 u64 enabled_pads;
1115 bool s_stream_enabled;
1116 };
1117
1118
1119 /**
1120 * media_entity_to_v4l2_subdev - Returns a &struct v4l2_subdev from
1121 * the &struct media_entity embedded in it.
1122 *
1123 * @ent: pointer to &struct media_entity.
1124 */
1125 #define media_entity_to_v4l2_subdev(ent) \
1126 ({ \
1127 typeof(ent) __me_sd_ent = (ent); \
1128 \
1129 __me_sd_ent ? \
1130 container_of(__me_sd_ent, struct v4l2_subdev, entity) : \
1131 NULL; \
1132 })
1133
1134 /**
1135 * vdev_to_v4l2_subdev - Returns a &struct v4l2_subdev from
1136 * the &struct video_device embedded on it.
1137 *
1138 * @vdev: pointer to &struct video_device
1139 */
1140 #define vdev_to_v4l2_subdev(vdev) \
1141 ((struct v4l2_subdev *)video_get_drvdata(vdev))
1142
1143 /**
1144 * struct v4l2_subdev_fh - Used for storing subdev information per file handle
1145 *
1146 * @vfh: pointer to &struct v4l2_fh
1147 * @state: pointer to &struct v4l2_subdev_state
1148 * @owner: module pointer to the owner of this file handle
1149 * @client_caps: bitmask of ``V4L2_SUBDEV_CLIENT_CAP_*``
1150 */
1151 struct v4l2_subdev_fh {
1152 struct v4l2_fh vfh;
1153 struct module *owner;
1154 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1155 struct v4l2_subdev_state *state;
1156 u64 client_caps;
1157 #endif
1158 };
1159
1160 /**
1161 * to_v4l2_subdev_fh - Returns a &struct v4l2_subdev_fh from
1162 * the &struct v4l2_fh embedded on it.
1163 *
1164 * @fh: pointer to &struct v4l2_fh
1165 */
1166 #define to_v4l2_subdev_fh(fh) \
1167 container_of(fh, struct v4l2_subdev_fh, vfh)
1168
1169 extern const struct v4l2_file_operations v4l2_subdev_fops;
1170
1171 /**
1172 * v4l2_set_subdevdata - Sets V4L2 dev private device data
1173 *
1174 * @sd: pointer to &struct v4l2_subdev
1175 * @p: pointer to the private device data to be stored.
1176 */
v4l2_set_subdevdata(struct v4l2_subdev * sd,void * p)1177 static inline void v4l2_set_subdevdata(struct v4l2_subdev *sd, void *p)
1178 {
1179 sd->dev_priv = p;
1180 }
1181
1182 /**
1183 * v4l2_get_subdevdata - Gets V4L2 dev private device data
1184 *
1185 * @sd: pointer to &struct v4l2_subdev
1186 *
1187 * Returns the pointer to the private device data to be stored.
1188 */
v4l2_get_subdevdata(const struct v4l2_subdev * sd)1189 static inline void *v4l2_get_subdevdata(const struct v4l2_subdev *sd)
1190 {
1191 return sd->dev_priv;
1192 }
1193
1194 /**
1195 * v4l2_set_subdev_hostdata - Sets V4L2 dev private host data
1196 *
1197 * @sd: pointer to &struct v4l2_subdev
1198 * @p: pointer to the private data to be stored.
1199 */
v4l2_set_subdev_hostdata(struct v4l2_subdev * sd,void * p)1200 static inline void v4l2_set_subdev_hostdata(struct v4l2_subdev *sd, void *p)
1201 {
1202 sd->host_priv = p;
1203 }
1204
1205 /**
1206 * v4l2_get_subdev_hostdata - Gets V4L2 dev private data
1207 *
1208 * @sd: pointer to &struct v4l2_subdev
1209 *
1210 * Returns the pointer to the private host data to be stored.
1211 */
v4l2_get_subdev_hostdata(const struct v4l2_subdev * sd)1212 static inline void *v4l2_get_subdev_hostdata(const struct v4l2_subdev *sd)
1213 {
1214 return sd->host_priv;
1215 }
1216
1217 #ifdef CONFIG_MEDIA_CONTROLLER
1218
1219 /**
1220 * v4l2_subdev_get_fwnode_pad_1_to_1 - Get pad number from a subdev fwnode
1221 * endpoint, assuming 1:1 port:pad
1222 *
1223 * @entity: Pointer to the subdev entity
1224 * @endpoint: Pointer to a parsed fwnode endpoint
1225 *
1226 * This function can be used as the .get_fwnode_pad operation for
1227 * subdevices that map port numbers and pad indexes 1:1. If the endpoint
1228 * is owned by the subdevice, the function returns the endpoint port
1229 * number.
1230 *
1231 * Returns the endpoint port number on success or a negative error code.
1232 */
1233 int v4l2_subdev_get_fwnode_pad_1_to_1(struct media_entity *entity,
1234 struct fwnode_endpoint *endpoint);
1235
1236 /**
1237 * v4l2_subdev_link_validate_default - validates a media link
1238 *
1239 * @sd: pointer to &struct v4l2_subdev
1240 * @link: pointer to &struct media_link
1241 * @source_fmt: pointer to &struct v4l2_subdev_format
1242 * @sink_fmt: pointer to &struct v4l2_subdev_format
1243 *
1244 * This function ensures that width, height and the media bus pixel
1245 * code are equal on both source and sink of the link.
1246 */
1247 int v4l2_subdev_link_validate_default(struct v4l2_subdev *sd,
1248 struct media_link *link,
1249 struct v4l2_subdev_format *source_fmt,
1250 struct v4l2_subdev_format *sink_fmt);
1251
1252 /**
1253 * v4l2_subdev_link_validate - validates a media link
1254 *
1255 * @link: pointer to &struct media_link
1256 *
1257 * This function calls the subdev's link_validate ops to validate
1258 * if a media link is valid for streaming. It also internally
1259 * calls v4l2_subdev_link_validate_default() to ensure that
1260 * width, height and the media bus pixel code are equal on both
1261 * source and sink of the link.
1262 *
1263 * The function can be used as a drop-in &media_entity_ops.link_validate
1264 * implementation for v4l2_subdev instances. It supports all links between
1265 * subdevs, as well as links between subdevs and video devices, provided that
1266 * the video devices also implement their &media_entity_ops.link_validate
1267 * operation.
1268 */
1269 int v4l2_subdev_link_validate(struct media_link *link);
1270
1271 /**
1272 * v4l2_subdev_has_pad_interdep - MC has_pad_interdep implementation for subdevs
1273 *
1274 * @entity: pointer to &struct media_entity
1275 * @pad0: pad number for the first pad
1276 * @pad1: pad number for the second pad
1277 *
1278 * This function is an implementation of the
1279 * media_entity_operations.has_pad_interdep operation for subdevs that
1280 * implement the multiplexed streams API (as indicated by the
1281 * V4L2_SUBDEV_FL_STREAMS subdev flag).
1282 *
1283 * It considers two pads interdependent if there is an active route between pad0
1284 * and pad1.
1285 */
1286 bool v4l2_subdev_has_pad_interdep(struct media_entity *entity,
1287 unsigned int pad0, unsigned int pad1);
1288
1289 /**
1290 * __v4l2_subdev_state_alloc - allocate v4l2_subdev_state
1291 *
1292 * @sd: pointer to &struct v4l2_subdev for which the state is being allocated.
1293 * @lock_name: name of the state lock
1294 * @key: lock_class_key for the lock
1295 *
1296 * Must call __v4l2_subdev_state_free() when state is no longer needed.
1297 *
1298 * Not to be called directly by the drivers.
1299 */
1300 struct v4l2_subdev_state *__v4l2_subdev_state_alloc(struct v4l2_subdev *sd,
1301 const char *lock_name,
1302 struct lock_class_key *key);
1303
1304 /**
1305 * __v4l2_subdev_state_free - free a v4l2_subdev_state
1306 *
1307 * @state: v4l2_subdev_state to be freed.
1308 *
1309 * Not to be called directly by the drivers.
1310 */
1311 void __v4l2_subdev_state_free(struct v4l2_subdev_state *state);
1312
1313 /**
1314 * v4l2_subdev_init_finalize() - Finalizes the initialization of the subdevice
1315 * @sd: The subdev
1316 *
1317 * This function finalizes the initialization of the subdev, including
1318 * allocation of the active state for the subdev.
1319 *
1320 * This function must be called by the subdev drivers that use the centralized
1321 * active state, after the subdev struct has been initialized and
1322 * media_entity_pads_init() has been called, but before registering the
1323 * subdev.
1324 *
1325 * The user must call v4l2_subdev_cleanup() when the subdev is being removed.
1326 */
1327 #define v4l2_subdev_init_finalize(sd) \
1328 ({ \
1329 static struct lock_class_key __key; \
1330 const char *name = KBUILD_BASENAME \
1331 ":" __stringify(__LINE__) ":sd->active_state->lock"; \
1332 __v4l2_subdev_init_finalize(sd, name, &__key); \
1333 })
1334
1335 int __v4l2_subdev_init_finalize(struct v4l2_subdev *sd, const char *name,
1336 struct lock_class_key *key);
1337
1338 /**
1339 * v4l2_subdev_cleanup() - Releases the resources allocated by the subdevice
1340 * @sd: The subdevice
1341 *
1342 * Clean up a V4L2 async sub-device. Must be called for a sub-device as part of
1343 * its release if resources have been associated with it using
1344 * v4l2_async_subdev_endpoint_add() or v4l2_subdev_init_finalize().
1345 */
1346 void v4l2_subdev_cleanup(struct v4l2_subdev *sd);
1347
1348 /*
1349 * A macro to generate the macro or function name for sub-devices state access
1350 * wrapper macros below.
1351 */
1352 #define __v4l2_subdev_state_gen_call(NAME, _1, ARG, ...) \
1353 __v4l2_subdev_state_get_ ## NAME ## ARG
1354
1355 /*
1356 * A macro to constify the return value of the state accessors when the state
1357 * parameter is const.
1358 */
1359 #define __v4l2_subdev_state_constify_ret(state, value) \
1360 _Generic(state, \
1361 const struct v4l2_subdev_state *: (const typeof(*(value)) *)(value), \
1362 struct v4l2_subdev_state *: (value) \
1363 )
1364
1365 /**
1366 * v4l2_subdev_state_get_format() - Get pointer to a stream format
1367 * @state: subdevice state
1368 * @pad: pad id
1369 * @...: stream id (optional argument)
1370 *
1371 * This returns a pointer to &struct v4l2_mbus_framefmt for the given pad +
1372 * stream in the subdev state.
1373 *
1374 * For stream-unaware drivers the format for the corresponding pad is returned.
1375 * If the pad does not exist, NULL is returned.
1376 */
1377 /*
1378 * Wrap v4l2_subdev_state_get_format(), allowing the function to be called with
1379 * two or three arguments. The purpose of the __v4l2_subdev_state_gen_call()
1380 * macro is to come up with the name of the function or macro to call, using
1381 * the last two arguments (_stream and _pad). The selected function or macro is
1382 * then called using the arguments specified by the caller. The
1383 * __v4l2_subdev_state_constify_ret() macro constifies the returned pointer
1384 * when the state is const, allowing the state accessors to guarantee
1385 * const-correctness in all cases.
1386 *
1387 * A similar arrangement is used for v4l2_subdev_state_crop(),
1388 * v4l2_subdev_state_compose() and v4l2_subdev_state_get_interval() below.
1389 */
1390 #define v4l2_subdev_state_get_format(state, pad, ...) \
1391 __v4l2_subdev_state_constify_ret(state, \
1392 __v4l2_subdev_state_gen_call(format, ##__VA_ARGS__, , _pad) \
1393 ((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
1394 #define __v4l2_subdev_state_get_format_pad(state, pad) \
1395 __v4l2_subdev_state_get_format(state, pad, 0)
1396 struct v4l2_mbus_framefmt *
1397 __v4l2_subdev_state_get_format(struct v4l2_subdev_state *state,
1398 unsigned int pad, u32 stream);
1399
1400 /**
1401 * v4l2_subdev_state_get_crop() - Get pointer to a stream crop rectangle
1402 * @state: subdevice state
1403 * @pad: pad id
1404 * @...: stream id (optional argument)
1405 *
1406 * This returns a pointer to crop rectangle for the given pad + stream in the
1407 * subdev state.
1408 *
1409 * For stream-unaware drivers the crop rectangle for the corresponding pad is
1410 * returned. If the pad does not exist, NULL is returned.
1411 */
1412 #define v4l2_subdev_state_get_crop(state, pad, ...) \
1413 __v4l2_subdev_state_constify_ret(state, \
1414 __v4l2_subdev_state_gen_call(crop, ##__VA_ARGS__, , _pad) \
1415 ((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
1416 #define __v4l2_subdev_state_get_crop_pad(state, pad) \
1417 __v4l2_subdev_state_get_crop(state, pad, 0)
1418 struct v4l2_rect *
1419 __v4l2_subdev_state_get_crop(struct v4l2_subdev_state *state, unsigned int pad,
1420 u32 stream);
1421
1422 /**
1423 * v4l2_subdev_state_get_compose() - Get pointer to a stream compose rectangle
1424 * @state: subdevice state
1425 * @pad: pad id
1426 * @...: stream id (optional argument)
1427 *
1428 * This returns a pointer to compose rectangle for the given pad + stream in the
1429 * subdev state.
1430 *
1431 * For stream-unaware drivers the compose rectangle for the corresponding pad is
1432 * returned. If the pad does not exist, NULL is returned.
1433 */
1434 #define v4l2_subdev_state_get_compose(state, pad, ...) \
1435 __v4l2_subdev_state_constify_ret(state, \
1436 __v4l2_subdev_state_gen_call(compose, ##__VA_ARGS__, , _pad) \
1437 ((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
1438 #define __v4l2_subdev_state_get_compose_pad(state, pad) \
1439 __v4l2_subdev_state_get_compose(state, pad, 0)
1440 struct v4l2_rect *
1441 __v4l2_subdev_state_get_compose(struct v4l2_subdev_state *state,
1442 unsigned int pad, u32 stream);
1443
1444 /**
1445 * v4l2_subdev_state_get_interval() - Get pointer to a stream frame interval
1446 * @state: subdevice state
1447 * @pad: pad id
1448 * @...: stream id (optional argument)
1449 *
1450 * This returns a pointer to the frame interval for the given pad + stream in
1451 * the subdev state.
1452 *
1453 * For stream-unaware drivers the frame interval for the corresponding pad is
1454 * returned. If the pad does not exist, NULL is returned.
1455 */
1456 #define v4l2_subdev_state_get_interval(state, pad, ...) \
1457 __v4l2_subdev_state_constify_ret(state, \
1458 __v4l2_subdev_state_gen_call(interval, ##__VA_ARGS__, , _pad) \
1459 ((struct v4l2_subdev_state *)state, pad, ##__VA_ARGS__))
1460 #define __v4l2_subdev_state_get_interval_pad(state, pad) \
1461 __v4l2_subdev_state_get_interval(state, pad, 0)
1462 struct v4l2_fract *
1463 __v4l2_subdev_state_get_interval(struct v4l2_subdev_state *state,
1464 unsigned int pad, u32 stream);
1465
1466 #if defined(CONFIG_VIDEO_V4L2_SUBDEV_API)
1467
1468 /**
1469 * v4l2_subdev_get_fmt() - Fill format based on state
1470 * @sd: subdevice
1471 * @state: subdevice state
1472 * @format: pointer to &struct v4l2_subdev_format
1473 *
1474 * Fill @format->format field based on the information in the @format struct.
1475 *
1476 * This function can be used by the subdev drivers which support active state to
1477 * implement v4l2_subdev_pad_ops.get_fmt if the subdev driver does not need to
1478 * do anything special in their get_fmt op.
1479 *
1480 * Returns 0 on success, error value otherwise.
1481 */
1482 int v4l2_subdev_get_fmt(struct v4l2_subdev *sd, struct v4l2_subdev_state *state,
1483 struct v4l2_subdev_format *format);
1484
1485 /**
1486 * v4l2_subdev_get_frame_interval() - Fill frame interval based on state
1487 * @sd: subdevice
1488 * @state: subdevice state
1489 * @fi: pointer to &struct v4l2_subdev_frame_interval
1490 *
1491 * Fill @fi->interval field based on the information in the @fi struct.
1492 *
1493 * This function can be used by the subdev drivers which support active state to
1494 * implement v4l2_subdev_pad_ops.get_frame_interval if the subdev driver does
1495 * not need to do anything special in their get_frame_interval op.
1496 *
1497 * Returns 0 on success, error value otherwise.
1498 */
1499 int v4l2_subdev_get_frame_interval(struct v4l2_subdev *sd,
1500 struct v4l2_subdev_state *state,
1501 struct v4l2_subdev_frame_interval *fi);
1502
1503 /**
1504 * v4l2_subdev_set_routing() - Set given routing to subdev state
1505 * @sd: The subdevice
1506 * @state: The subdevice state
1507 * @routing: Routing that will be copied to subdev state
1508 *
1509 * This will release old routing table (if any) from the state, allocate
1510 * enough space for the given routing, and copy the routing.
1511 *
1512 * This can be used from the subdev driver's set_routing op, after validating
1513 * the routing.
1514 */
1515 int v4l2_subdev_set_routing(struct v4l2_subdev *sd,
1516 struct v4l2_subdev_state *state,
1517 const struct v4l2_subdev_krouting *routing);
1518
1519 struct v4l2_subdev_route *
1520 __v4l2_subdev_next_active_route(const struct v4l2_subdev_krouting *routing,
1521 struct v4l2_subdev_route *route);
1522
1523 /**
1524 * for_each_active_route - iterate on all active routes of a routing table
1525 * @routing: The routing table
1526 * @route: The route iterator
1527 */
1528 #define for_each_active_route(routing, route) \
1529 for ((route) = NULL; \
1530 ((route) = __v4l2_subdev_next_active_route((routing), (route)));)
1531
1532 /**
1533 * v4l2_subdev_set_routing_with_fmt() - Set given routing and format to subdev
1534 * state
1535 * @sd: The subdevice
1536 * @state: The subdevice state
1537 * @routing: Routing that will be copied to subdev state
1538 * @fmt: Format used to initialize all the streams
1539 *
1540 * This is the same as v4l2_subdev_set_routing, but additionally initializes
1541 * all the streams using the given format.
1542 */
1543 int v4l2_subdev_set_routing_with_fmt(struct v4l2_subdev *sd,
1544 struct v4l2_subdev_state *state,
1545 const struct v4l2_subdev_krouting *routing,
1546 const struct v4l2_mbus_framefmt *fmt);
1547
1548 /**
1549 * v4l2_subdev_routing_find_opposite_end() - Find the opposite stream
1550 * @routing: routing used to find the opposite side
1551 * @pad: pad id
1552 * @stream: stream id
1553 * @other_pad: pointer used to return the opposite pad
1554 * @other_stream: pointer used to return the opposite stream
1555 *
1556 * This function uses the routing table to find the pad + stream which is
1557 * opposite the given pad + stream.
1558 *
1559 * @other_pad and/or @other_stream can be NULL if the caller does not need the
1560 * value.
1561 *
1562 * Returns 0 on success, or -EINVAL if no matching route is found.
1563 */
1564 int v4l2_subdev_routing_find_opposite_end(const struct v4l2_subdev_krouting *routing,
1565 u32 pad, u32 stream, u32 *other_pad,
1566 u32 *other_stream);
1567
1568 /**
1569 * v4l2_subdev_state_get_opposite_stream_format() - Get pointer to opposite
1570 * stream format
1571 * @state: subdevice state
1572 * @pad: pad id
1573 * @stream: stream id
1574 *
1575 * This returns a pointer to &struct v4l2_mbus_framefmt for the pad + stream
1576 * that is opposite the given pad + stream in the subdev state.
1577 *
1578 * If the state does not contain the given pad + stream, NULL is returned.
1579 */
1580 struct v4l2_mbus_framefmt *
1581 v4l2_subdev_state_get_opposite_stream_format(struct v4l2_subdev_state *state,
1582 u32 pad, u32 stream);
1583
1584 /**
1585 * v4l2_subdev_state_xlate_streams() - Translate streams from one pad to another
1586 *
1587 * @state: Subdevice state
1588 * @pad0: The first pad
1589 * @pad1: The second pad
1590 * @streams: Streams bitmask on the first pad
1591 *
1592 * Streams on sink pads of a subdev are routed to source pads as expressed in
1593 * the subdev state routing table. Stream numbers don't necessarily match on
1594 * the sink and source side of a route. This function translates stream numbers
1595 * on @pad0, expressed as a bitmask in @streams, to the corresponding streams
1596 * on @pad1 using the routing table from the @state. It returns the stream mask
1597 * on @pad1, and updates @streams with the streams that have been found in the
1598 * routing table.
1599 *
1600 * @pad0 and @pad1 must be a sink and a source, in any order.
1601 *
1602 * Return: The bitmask of streams of @pad1 that are routed to @streams on @pad0.
1603 */
1604 u64 v4l2_subdev_state_xlate_streams(const struct v4l2_subdev_state *state,
1605 u32 pad0, u32 pad1, u64 *streams);
1606
1607 /**
1608 * enum v4l2_subdev_routing_restriction - Subdevice internal routing restrictions
1609 *
1610 * @V4L2_SUBDEV_ROUTING_NO_1_TO_N:
1611 * an input stream shall not be routed to multiple output streams (stream
1612 * duplication)
1613 * @V4L2_SUBDEV_ROUTING_NO_N_TO_1:
1614 * multiple input streams shall not be routed to the same output stream
1615 * (stream merging)
1616 * @V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX:
1617 * all streams from a sink pad must be routed to a single source pad
1618 * @V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX:
1619 * all streams on a source pad must originate from a single sink pad
1620 * @V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING:
1621 * source pads shall not contain multiplexed streams
1622 * @V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING:
1623 * sink pads shall not contain multiplexed streams
1624 * @V4L2_SUBDEV_ROUTING_ONLY_1_TO_1:
1625 * only non-overlapping 1-to-1 stream routing is allowed (a combination of
1626 * @V4L2_SUBDEV_ROUTING_NO_1_TO_N and @V4L2_SUBDEV_ROUTING_NO_N_TO_1)
1627 * @V4L2_SUBDEV_ROUTING_NO_STREAM_MIX:
1628 * all streams from a sink pad must be routed to a single source pad, and
1629 * that source pad shall not get routes from any other sink pad
1630 * (a combination of @V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX and
1631 * @V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX)
1632 * @V4L2_SUBDEV_ROUTING_NO_MULTIPLEXING:
1633 * no multiplexed streams allowed on either source or sink sides.
1634 */
1635 enum v4l2_subdev_routing_restriction {
1636 V4L2_SUBDEV_ROUTING_NO_1_TO_N = BIT(0),
1637 V4L2_SUBDEV_ROUTING_NO_N_TO_1 = BIT(1),
1638 V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX = BIT(2),
1639 V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX = BIT(3),
1640 V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING = BIT(4),
1641 V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING = BIT(5),
1642 V4L2_SUBDEV_ROUTING_ONLY_1_TO_1 =
1643 V4L2_SUBDEV_ROUTING_NO_1_TO_N |
1644 V4L2_SUBDEV_ROUTING_NO_N_TO_1,
1645 V4L2_SUBDEV_ROUTING_NO_STREAM_MIX =
1646 V4L2_SUBDEV_ROUTING_NO_SINK_STREAM_MIX |
1647 V4L2_SUBDEV_ROUTING_NO_SOURCE_STREAM_MIX,
1648 V4L2_SUBDEV_ROUTING_NO_MULTIPLEXING =
1649 V4L2_SUBDEV_ROUTING_NO_SINK_MULTIPLEXING |
1650 V4L2_SUBDEV_ROUTING_NO_SOURCE_MULTIPLEXING,
1651 };
1652
1653 /**
1654 * v4l2_subdev_routing_validate() - Verify that routes comply with driver
1655 * constraints
1656 * @sd: The subdevice
1657 * @routing: Routing to verify
1658 * @disallow: Restrictions on routes
1659 *
1660 * This verifies that the given routing complies with the @disallow constraints.
1661 *
1662 * Returns 0 on success, error value otherwise.
1663 */
1664 int v4l2_subdev_routing_validate(struct v4l2_subdev *sd,
1665 const struct v4l2_subdev_krouting *routing,
1666 enum v4l2_subdev_routing_restriction disallow);
1667
1668 /**
1669 * v4l2_subdev_enable_streams() - Enable streams on a pad
1670 * @sd: The subdevice
1671 * @pad: The pad
1672 * @streams_mask: Bitmask of streams to enable
1673 *
1674 * This function enables streams on a source @pad of a subdevice. The pad is
1675 * identified by its index, while the streams are identified by the
1676 * @streams_mask bitmask. This allows enabling multiple streams on a pad at
1677 * once.
1678 *
1679 * Enabling a stream that is already enabled isn't allowed. If @streams_mask
1680 * contains an already enabled stream, this function returns -EALREADY without
1681 * performing any operation.
1682 *
1683 * Per-stream enable is only available for subdevs that implement the
1684 * .enable_streams() and .disable_streams() operations. For other subdevs, this
1685 * function implements a best-effort compatibility by calling the .s_stream()
1686 * operation, limited to subdevs that have a single source pad.
1687 *
1688 * Drivers that are not stream-aware shall set @streams_mask to BIT_ULL(0).
1689 *
1690 * Return:
1691 * * 0: Success
1692 * * -EALREADY: One of the streams in streams_mask is already enabled
1693 * * -EINVAL: The pad index is invalid, or doesn't correspond to a source pad
1694 * * -EOPNOTSUPP: Falling back to the legacy .s_stream() operation is
1695 * impossible because the subdev has multiple source pads
1696 */
1697 int v4l2_subdev_enable_streams(struct v4l2_subdev *sd, u32 pad,
1698 u64 streams_mask);
1699
1700 /**
1701 * v4l2_subdev_disable_streams() - Disable streams on a pad
1702 * @sd: The subdevice
1703 * @pad: The pad
1704 * @streams_mask: Bitmask of streams to disable
1705 *
1706 * This function disables streams on a source @pad of a subdevice. The pad is
1707 * identified by its index, while the streams are identified by the
1708 * @streams_mask bitmask. This allows disabling multiple streams on a pad at
1709 * once.
1710 *
1711 * Disabling a streams that is not enabled isn't allowed. If @streams_mask
1712 * contains a disabled stream, this function returns -EALREADY without
1713 * performing any operation.
1714 *
1715 * Per-stream disable is only available for subdevs that implement the
1716 * .enable_streams() and .disable_streams() operations. For other subdevs, this
1717 * function implements a best-effort compatibility by calling the .s_stream()
1718 * operation, limited to subdevs that have a single source pad.
1719 *
1720 * Drivers that are not stream-aware shall set @streams_mask to BIT_ULL(0).
1721 *
1722 * Return:
1723 * * 0: Success
1724 * * -EALREADY: One of the streams in streams_mask is not enabled
1725 * * -EINVAL: The pad index is invalid, or doesn't correspond to a source pad
1726 * * -EOPNOTSUPP: Falling back to the legacy .s_stream() operation is
1727 * impossible because the subdev has multiple source pads
1728 */
1729 int v4l2_subdev_disable_streams(struct v4l2_subdev *sd, u32 pad,
1730 u64 streams_mask);
1731
1732 /**
1733 * v4l2_subdev_s_stream_helper() - Helper to implement the subdev s_stream
1734 * operation using enable_streams and disable_streams
1735 * @sd: The subdevice
1736 * @enable: Enable or disable streaming
1737 *
1738 * Subdevice drivers that implement the streams-aware
1739 * &v4l2_subdev_pad_ops.enable_streams and &v4l2_subdev_pad_ops.disable_streams
1740 * operations can use this helper to implement the legacy
1741 * &v4l2_subdev_video_ops.s_stream operation.
1742 *
1743 * This helper can only be used by subdevs that have a single source pad.
1744 *
1745 * Return: 0 on success, or a negative error code otherwise.
1746 */
1747 int v4l2_subdev_s_stream_helper(struct v4l2_subdev *sd, int enable);
1748
1749 #endif /* CONFIG_VIDEO_V4L2_SUBDEV_API */
1750
1751 #endif /* CONFIG_MEDIA_CONTROLLER */
1752
1753 /**
1754 * v4l2_subdev_lock_state() - Locks the subdev state
1755 * @state: The subdevice state
1756 *
1757 * Locks the given subdev state.
1758 *
1759 * The state must be unlocked with v4l2_subdev_unlock_state() after use.
1760 */
v4l2_subdev_lock_state(struct v4l2_subdev_state * state)1761 static inline void v4l2_subdev_lock_state(struct v4l2_subdev_state *state)
1762 {
1763 mutex_lock(state->lock);
1764 }
1765
1766 /**
1767 * v4l2_subdev_unlock_state() - Unlocks the subdev state
1768 * @state: The subdevice state
1769 *
1770 * Unlocks the given subdev state.
1771 */
v4l2_subdev_unlock_state(struct v4l2_subdev_state * state)1772 static inline void v4l2_subdev_unlock_state(struct v4l2_subdev_state *state)
1773 {
1774 mutex_unlock(state->lock);
1775 }
1776
1777 /**
1778 * v4l2_subdev_lock_states - Lock two sub-device states
1779 * @state1: One subdevice state
1780 * @state2: The other subdevice state
1781 *
1782 * Locks the state of two sub-devices.
1783 *
1784 * The states must be unlocked with v4l2_subdev_unlock_states() after use.
1785 *
1786 * This differs from calling v4l2_subdev_lock_state() on both states so that if
1787 * the states share the same lock, the lock is acquired only once (so no
1788 * deadlock occurs). The caller is responsible for ensuring the locks will
1789 * always be acquired in the same order.
1790 */
v4l2_subdev_lock_states(struct v4l2_subdev_state * state1,struct v4l2_subdev_state * state2)1791 static inline void v4l2_subdev_lock_states(struct v4l2_subdev_state *state1,
1792 struct v4l2_subdev_state *state2)
1793 {
1794 mutex_lock(state1->lock);
1795 if (state1->lock != state2->lock)
1796 mutex_lock(state2->lock);
1797 }
1798
1799 /**
1800 * v4l2_subdev_unlock_states() - Unlock two sub-device states
1801 * @state1: One subdevice state
1802 * @state2: The other subdevice state
1803 *
1804 * Unlocks the state of two sub-devices.
1805 *
1806 * This differs from calling v4l2_subdev_unlock_state() on both states so that
1807 * if the states share the same lock, the lock is released only once.
1808 */
v4l2_subdev_unlock_states(struct v4l2_subdev_state * state1,struct v4l2_subdev_state * state2)1809 static inline void v4l2_subdev_unlock_states(struct v4l2_subdev_state *state1,
1810 struct v4l2_subdev_state *state2)
1811 {
1812 mutex_unlock(state1->lock);
1813 if (state1->lock != state2->lock)
1814 mutex_unlock(state2->lock);
1815 }
1816
1817 /**
1818 * v4l2_subdev_get_unlocked_active_state() - Checks that the active subdev state
1819 * is unlocked and returns it
1820 * @sd: The subdevice
1821 *
1822 * Returns the active state for the subdevice, or NULL if the subdev does not
1823 * support active state. If the state is not NULL, calls
1824 * lockdep_assert_not_held() to issue a warning if the state is locked.
1825 *
1826 * This function is to be used e.g. when getting the active state for the sole
1827 * purpose of passing it forward, without accessing the state fields.
1828 */
1829 static inline struct v4l2_subdev_state *
v4l2_subdev_get_unlocked_active_state(struct v4l2_subdev * sd)1830 v4l2_subdev_get_unlocked_active_state(struct v4l2_subdev *sd)
1831 {
1832 if (sd->active_state)
1833 lockdep_assert_not_held(sd->active_state->lock);
1834 return sd->active_state;
1835 }
1836
1837 /**
1838 * v4l2_subdev_get_locked_active_state() - Checks that the active subdev state
1839 * is locked and returns it
1840 *
1841 * @sd: The subdevice
1842 *
1843 * Returns the active state for the subdevice, or NULL if the subdev does not
1844 * support active state. If the state is not NULL, calls lockdep_assert_held()
1845 * to issue a warning if the state is not locked.
1846 *
1847 * This function is to be used when the caller knows that the active state is
1848 * already locked.
1849 */
1850 static inline struct v4l2_subdev_state *
v4l2_subdev_get_locked_active_state(struct v4l2_subdev * sd)1851 v4l2_subdev_get_locked_active_state(struct v4l2_subdev *sd)
1852 {
1853 if (sd->active_state)
1854 lockdep_assert_held(sd->active_state->lock);
1855 return sd->active_state;
1856 }
1857
1858 /**
1859 * v4l2_subdev_lock_and_get_active_state() - Locks and returns the active subdev
1860 * state for the subdevice
1861 * @sd: The subdevice
1862 *
1863 * Returns the locked active state for the subdevice, or NULL if the subdev
1864 * does not support active state.
1865 *
1866 * The state must be unlocked with v4l2_subdev_unlock_state() after use.
1867 */
1868 static inline struct v4l2_subdev_state *
v4l2_subdev_lock_and_get_active_state(struct v4l2_subdev * sd)1869 v4l2_subdev_lock_and_get_active_state(struct v4l2_subdev *sd)
1870 {
1871 if (sd->active_state)
1872 v4l2_subdev_lock_state(sd->active_state);
1873 return sd->active_state;
1874 }
1875
1876 /**
1877 * v4l2_subdev_init - initializes the sub-device struct
1878 *
1879 * @sd: pointer to the &struct v4l2_subdev to be initialized
1880 * @ops: pointer to &struct v4l2_subdev_ops.
1881 */
1882 void v4l2_subdev_init(struct v4l2_subdev *sd,
1883 const struct v4l2_subdev_ops *ops);
1884
1885 extern const struct v4l2_subdev_ops v4l2_subdev_call_wrappers;
1886
1887 /**
1888 * v4l2_subdev_call - call an operation of a v4l2_subdev.
1889 *
1890 * @sd: pointer to the &struct v4l2_subdev
1891 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
1892 * Each element there groups a set of callbacks functions.
1893 * @f: callback function to be called.
1894 * The callback functions are defined in groups, according to
1895 * each element at &struct v4l2_subdev_ops.
1896 * @args: arguments for @f.
1897 *
1898 * Example: err = v4l2_subdev_call(sd, video, s_std, norm);
1899 */
1900 #define v4l2_subdev_call(sd, o, f, args...) \
1901 ({ \
1902 struct v4l2_subdev *__sd = (sd); \
1903 int __result; \
1904 if (!__sd) \
1905 __result = -ENODEV; \
1906 else if (!(__sd->ops->o && __sd->ops->o->f)) \
1907 __result = -ENOIOCTLCMD; \
1908 else if (v4l2_subdev_call_wrappers.o && \
1909 v4l2_subdev_call_wrappers.o->f) \
1910 __result = v4l2_subdev_call_wrappers.o->f( \
1911 __sd, ##args); \
1912 else \
1913 __result = __sd->ops->o->f(__sd, ##args); \
1914 __result; \
1915 })
1916
1917 /**
1918 * v4l2_subdev_call_state_active - call an operation of a v4l2_subdev which
1919 * takes state as a parameter, passing the
1920 * subdev its active state.
1921 *
1922 * @sd: pointer to the &struct v4l2_subdev
1923 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
1924 * Each element there groups a set of callbacks functions.
1925 * @f: callback function to be called.
1926 * The callback functions are defined in groups, according to
1927 * each element at &struct v4l2_subdev_ops.
1928 * @args: arguments for @f.
1929 *
1930 * This is similar to v4l2_subdev_call(), except that this version can only be
1931 * used for ops that take a subdev state as a parameter. The macro will get the
1932 * active state, lock it before calling the op and unlock it after the call.
1933 */
1934 #define v4l2_subdev_call_state_active(sd, o, f, args...) \
1935 ({ \
1936 int __result; \
1937 struct v4l2_subdev_state *state; \
1938 state = v4l2_subdev_get_unlocked_active_state(sd); \
1939 if (state) \
1940 v4l2_subdev_lock_state(state); \
1941 __result = v4l2_subdev_call(sd, o, f, state, ##args); \
1942 if (state) \
1943 v4l2_subdev_unlock_state(state); \
1944 __result; \
1945 })
1946
1947 /**
1948 * v4l2_subdev_call_state_try - call an operation of a v4l2_subdev which
1949 * takes state as a parameter, passing the
1950 * subdev a newly allocated try state.
1951 *
1952 * @sd: pointer to the &struct v4l2_subdev
1953 * @o: name of the element at &struct v4l2_subdev_ops that contains @f.
1954 * Each element there groups a set of callbacks functions.
1955 * @f: callback function to be called.
1956 * The callback functions are defined in groups, according to
1957 * each element at &struct v4l2_subdev_ops.
1958 * @args: arguments for @f.
1959 *
1960 * This is similar to v4l2_subdev_call_state_active(), except that as this
1961 * version allocates a new state, this is only usable for
1962 * V4L2_SUBDEV_FORMAT_TRY use cases.
1963 *
1964 * Note: only legacy non-MC drivers may need this macro.
1965 */
1966 #define v4l2_subdev_call_state_try(sd, o, f, args...) \
1967 ({ \
1968 int __result; \
1969 static struct lock_class_key __key; \
1970 const char *name = KBUILD_BASENAME \
1971 ":" __stringify(__LINE__) ":state->lock"; \
1972 struct v4l2_subdev_state *state = \
1973 __v4l2_subdev_state_alloc(sd, name, &__key); \
1974 v4l2_subdev_lock_state(state); \
1975 __result = v4l2_subdev_call(sd, o, f, state, ##args); \
1976 v4l2_subdev_unlock_state(state); \
1977 __v4l2_subdev_state_free(state); \
1978 __result; \
1979 })
1980
1981 /**
1982 * v4l2_subdev_has_op - Checks if a subdev defines a certain operation.
1983 *
1984 * @sd: pointer to the &struct v4l2_subdev
1985 * @o: The group of callback functions in &struct v4l2_subdev_ops
1986 * which @f is a part of.
1987 * @f: callback function to be checked for its existence.
1988 */
1989 #define v4l2_subdev_has_op(sd, o, f) \
1990 ((sd)->ops->o && (sd)->ops->o->f)
1991
1992 /**
1993 * v4l2_subdev_notify_event() - Delivers event notification for subdevice
1994 * @sd: The subdev for which to deliver the event
1995 * @ev: The event to deliver
1996 *
1997 * Will deliver the specified event to all userspace event listeners which are
1998 * subscribed to the v42l subdev event queue as well as to the bridge driver
1999 * using the notify callback. The notification type for the notify callback
2000 * will be %V4L2_DEVICE_NOTIFY_EVENT.
2001 */
2002 void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
2003 const struct v4l2_event *ev);
2004
2005 /**
2006 * v4l2_subdev_is_streaming() - Returns if the subdevice is streaming
2007 * @sd: The subdevice
2008 *
2009 * v4l2_subdev_is_streaming() tells if the subdevice is currently streaming.
2010 * "Streaming" here means whether .s_stream() or .enable_streams() has been
2011 * successfully called, and the streaming has not yet been disabled.
2012 *
2013 * If the subdevice implements .enable_streams() this function must be called
2014 * while holding the active state lock.
2015 */
2016 bool v4l2_subdev_is_streaming(struct v4l2_subdev *sd);
2017
2018 #endif /* _V4L2_SUBDEV_H */
2019