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