1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * vivid-core.h - core datastructures
5 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
12 #include <linux/workqueue.h>
13 #include <media/cec.h>
14 #include <media/videobuf2-v4l2.h>
15 #include <media/v4l2-device.h>
16 #include <media/v4l2-dev.h>
17 #include <media/v4l2-ctrls.h>
18 #include <media/tpg/v4l2-tpg.h>
19 #include "vivid-rds-gen.h"
20 #include "vivid-vbi-gen.h"
22 #define dprintk(dev, level, fmt, arg...) \
23 v4l2_dbg(level, vivid_debug, &dev->v4l2_dev, fmt, ## arg)
25 /* Maximum allowed frame rate
27 * vivid will allow setting timeperframe in [1/FPS_MAX - FPS_MAX/1] range.
29 * Ideally FPS_MAX should be infinity, i.e. practically UINT_MAX, but that
30 * might hit application errors when they manipulate these values.
32 * Besides, for tpf < 10ms image-generation logic should be changed, to avoid
33 * producing frames with equal content.
37 /* The maximum number of clip rectangles */
39 /* The maximum number of inputs */
41 /* The maximum number of outputs */
42 #define MAX_OUTPUTS 16
43 /* The maximum up or down scaling factor is 4 */
45 /* The maximum image width/height are set to 4K DMT */
46 #define MAX_WIDTH 4096
47 #define MAX_HEIGHT 2160
48 /* The minimum image width/height */
51 /* The data_offset of plane 0 for the multiplanar formats */
52 #define PLANE0_DATA_OFFSET 128
54 /* The supported TV frequency range in MHz */
55 #define MIN_TV_FREQ (44U * 16U)
56 #define MAX_TV_FREQ (958U * 16U)
58 /* The number of samples returned in every SDR buffer */
59 #define SDR_CAP_SAMPLES_PER_BUF 0x4000
61 /* used by the threads to know when to resync internal counters */
62 #define JIFFIES_PER_DAY (3600U * 24U * HZ)
63 #define JIFFIES_RESYNC (JIFFIES_PER_DAY * (0xf0000000U / JIFFIES_PER_DAY))
65 extern const struct v4l2_rect vivid_min_rect;
66 extern const struct v4l2_rect vivid_max_rect;
67 extern unsigned vivid_debug;
70 u32 fourcc; /* v4l2 format id */
71 enum tgp_color_enc color_enc;
73 u8 vdownsampling[TPG_MAX_PLANES];
77 u32 data_offset[TPG_MAX_PLANES];
78 u32 bit_depth[TPG_MAX_PLANES];
81 extern struct vivid_fmt vivid_formats[];
83 /* buffer for one video frame */
85 /* common v4l buffer stuff -- must be first */
86 struct vb2_v4l2_buffer vb;
87 struct list_head list;
97 enum vivid_signal_mode {
99 CURRENT_STD = CURRENT_DV_TIMINGS,
104 SELECTED_STD = SELECTED_DV_TIMINGS,
106 CYCLE_STD = CYCLE_DV_TIMINGS,
110 enum vivid_colorspace {
122 #define VIVID_INVALID_SIGNAL(mode) \
123 ((mode) == NO_SIGNAL || (mode) == NO_LOCK || (mode) == OUT_OF_RANGE)
125 struct vivid_cec_work {
126 struct list_head list;
127 struct delayed_work work;
128 struct cec_adapter *adap;
129 struct vivid_dev *dev;
131 unsigned int timeout_ms;
138 struct v4l2_device v4l2_dev;
139 #ifdef CONFIG_MEDIA_CONTROLLER
140 struct media_device mdev;
141 struct media_pad vid_cap_pad;
142 struct media_pad vid_out_pad;
143 struct media_pad vbi_cap_pad;
144 struct media_pad vbi_out_pad;
145 struct media_pad sdr_cap_pad;
147 struct v4l2_ctrl_handler ctrl_hdl_user_gen;
148 struct v4l2_ctrl_handler ctrl_hdl_user_vid;
149 struct v4l2_ctrl_handler ctrl_hdl_user_aud;
150 struct v4l2_ctrl_handler ctrl_hdl_streaming;
151 struct v4l2_ctrl_handler ctrl_hdl_sdtv_cap;
152 struct v4l2_ctrl_handler ctrl_hdl_loop_cap;
153 struct v4l2_ctrl_handler ctrl_hdl_fb;
154 struct video_device vid_cap_dev;
155 struct v4l2_ctrl_handler ctrl_hdl_vid_cap;
156 struct video_device vid_out_dev;
157 struct v4l2_ctrl_handler ctrl_hdl_vid_out;
158 struct video_device vbi_cap_dev;
159 struct v4l2_ctrl_handler ctrl_hdl_vbi_cap;
160 struct video_device vbi_out_dev;
161 struct v4l2_ctrl_handler ctrl_hdl_vbi_out;
162 struct video_device radio_rx_dev;
163 struct v4l2_ctrl_handler ctrl_hdl_radio_rx;
164 struct video_device radio_tx_dev;
165 struct v4l2_ctrl_handler ctrl_hdl_radio_tx;
166 struct video_device sdr_cap_dev;
167 struct v4l2_ctrl_handler ctrl_hdl_sdr_cap;
180 /* supported features */
183 u8 input_type[MAX_INPUTS];
184 u8 input_name_counter[MAX_INPUTS];
185 unsigned num_outputs;
186 u8 output_type[MAX_OUTPUTS];
187 u8 output_name_counter[MAX_OUTPUTS];
188 bool has_audio_inputs;
189 bool has_audio_outputs;
193 bool has_raw_vbi_cap;
194 bool has_sliced_vbi_cap;
196 bool has_raw_vbi_out;
197 bool has_sliced_vbi_out;
206 struct v4l2_ctrl *brightness;
207 struct v4l2_ctrl *contrast;
208 struct v4l2_ctrl *saturation;
209 struct v4l2_ctrl *hue;
211 /* autogain/gain cluster */
212 struct v4l2_ctrl *autogain;
213 struct v4l2_ctrl *gain;
215 struct v4l2_ctrl *volume;
216 struct v4l2_ctrl *mute;
217 struct v4l2_ctrl *alpha;
218 struct v4l2_ctrl *button;
219 struct v4l2_ctrl *boolean;
220 struct v4l2_ctrl *int32;
221 struct v4l2_ctrl *int64;
222 struct v4l2_ctrl *menu;
223 struct v4l2_ctrl *string;
224 struct v4l2_ctrl *bitmask;
225 struct v4l2_ctrl *int_menu;
226 struct v4l2_ctrl *test_pattern;
227 struct v4l2_ctrl *colorspace;
228 struct v4l2_ctrl *rgb_range_cap;
229 struct v4l2_ctrl *real_rgb_range_cap;
231 /* std_signal_mode/standard cluster */
232 struct v4l2_ctrl *ctrl_std_signal_mode;
233 struct v4l2_ctrl *ctrl_standard;
236 /* dv_timings_signal_mode/timings cluster */
237 struct v4l2_ctrl *ctrl_dv_timings_signal_mode;
238 struct v4l2_ctrl *ctrl_dv_timings;
240 struct v4l2_ctrl *ctrl_has_crop_cap;
241 struct v4l2_ctrl *ctrl_has_compose_cap;
242 struct v4l2_ctrl *ctrl_has_scaler_cap;
243 struct v4l2_ctrl *ctrl_has_crop_out;
244 struct v4l2_ctrl *ctrl_has_compose_out;
245 struct v4l2_ctrl *ctrl_has_scaler_out;
246 struct v4l2_ctrl *ctrl_tx_mode;
247 struct v4l2_ctrl *ctrl_tx_rgb_range;
249 struct v4l2_ctrl *radio_tx_rds_pi;
250 struct v4l2_ctrl *radio_tx_rds_pty;
251 struct v4l2_ctrl *radio_tx_rds_mono_stereo;
252 struct v4l2_ctrl *radio_tx_rds_art_head;
253 struct v4l2_ctrl *radio_tx_rds_compressed;
254 struct v4l2_ctrl *radio_tx_rds_dyn_pty;
255 struct v4l2_ctrl *radio_tx_rds_ta;
256 struct v4l2_ctrl *radio_tx_rds_tp;
257 struct v4l2_ctrl *radio_tx_rds_ms;
258 struct v4l2_ctrl *radio_tx_rds_psname;
259 struct v4l2_ctrl *radio_tx_rds_radiotext;
261 struct v4l2_ctrl *radio_rx_rds_pty;
262 struct v4l2_ctrl *radio_rx_rds_ta;
263 struct v4l2_ctrl *radio_rx_rds_tp;
264 struct v4l2_ctrl *radio_rx_rds_ms;
265 struct v4l2_ctrl *radio_rx_rds_psname;
266 struct v4l2_ctrl *radio_rx_rds_radiotext;
268 unsigned input_brightness[MAX_INPUTS];
270 unsigned button_pressed;
275 bool vbi_cap_interlaced;
280 unsigned long video_pbase;
282 u32 video_buffer_size;
285 int display_byte_stride;
288 struct fb_info fb_info;
289 struct fb_var_screeninfo fb_defined;
290 struct fb_fix_screeninfo fb_fix;
292 /* Error injection */
293 bool queue_setup_error;
294 bool buf_prepare_error;
295 bool start_streaming_error;
299 u64 time_wrap_offset;
300 unsigned perc_dropped_buffers;
301 enum vivid_signal_mode std_signal_mode;
302 unsigned query_std_last;
303 v4l2_std_id query_std;
304 enum tpg_video_aspect std_aspect_ratio;
306 enum vivid_signal_mode dv_timings_signal_mode;
307 char **query_dv_timings_qmenu;
308 unsigned query_dv_timings_size;
309 unsigned query_dv_timings_last;
310 unsigned query_dv_timings;
311 enum tpg_video_aspect dv_timings_aspect_ratio;
316 struct v4l2_dv_timings dv_timings_cap;
318 struct vivid_vbi_gen_data vbi_gen;
320 unsigned edid_blocks;
321 unsigned edid_max_blocks;
322 unsigned webcam_size_idx;
323 unsigned webcam_ival_idx;
326 unsigned tv_field_cap;
327 unsigned tv_audio_input;
329 /* Capture Overlay */
330 struct v4l2_framebuffer fb_cap;
331 struct v4l2_fh *overlay_cap_owner;
333 int overlay_cap_top, overlay_cap_left;
334 enum v4l2_field overlay_cap_field;
336 struct v4l2_clip clips_cap[MAX_CLIPS];
337 struct v4l2_clip try_clips_cap[MAX_CLIPS];
338 unsigned clipcount_cap;
343 struct v4l2_dv_timings dv_timings_out;
347 u32 quantization_out;
350 unsigned bytesperline_out[TPG_MAX_PLANES];
351 unsigned tv_field_out;
352 unsigned tv_audio_output;
353 bool vbi_out_have_wss;
355 bool vbi_out_have_cc[2];
360 unsigned cur_scaled_line;
364 bool overlay_out_enabled;
365 int overlay_out_top, overlay_out_left;
367 struct v4l2_clip clips_out[MAX_CLIPS];
368 struct v4l2_clip try_clips_out[MAX_CLIPS];
369 unsigned clipcount_out;
370 unsigned fbuf_out_flags;
377 bool must_blank[VIDEO_MAX_FRAME];
379 const struct vivid_fmt *fmt_cap;
380 struct v4l2_fract timeperframe_vid_cap;
381 enum v4l2_field field_cap;
382 struct v4l2_rect src_rect;
383 struct v4l2_rect fmt_cap_rect;
384 struct v4l2_rect crop_cap;
385 struct v4l2_rect compose_cap;
386 struct v4l2_rect crop_bounds_cap;
387 struct vb2_queue vb_vid_cap_q;
388 struct list_head vid_cap_active;
389 struct vb2_queue vb_vbi_cap_q;
390 struct list_head vbi_cap_active;
392 /* thread for generating video capture stream */
393 struct task_struct *kthread_vid_cap;
394 unsigned long jiffies_vid_cap;
398 u32 vid_cap_seq_start;
399 u32 vid_cap_seq_count;
400 bool vid_cap_streaming;
401 u32 vbi_cap_seq_start;
402 u32 vbi_cap_seq_count;
403 bool vbi_cap_streaming;
404 bool stream_sliced_vbi_cap;
407 const struct vivid_fmt *fmt_out;
408 struct v4l2_fract timeperframe_vid_out;
409 enum v4l2_field field_out;
410 struct v4l2_rect sink_rect;
411 struct v4l2_rect fmt_out_rect;
412 struct v4l2_rect crop_out;
413 struct v4l2_rect compose_out;
414 struct v4l2_rect compose_bounds_out;
415 struct vb2_queue vb_vid_out_q;
416 struct list_head vid_out_active;
417 struct vb2_queue vb_vbi_out_q;
418 struct list_head vbi_out_active;
420 /* video loop precalculated rectangles */
423 * Intersection between what the output side composes and the capture side
424 * crops. I.e., what actually needs to be copied from the output buffer to
425 * the capture buffer.
427 struct v4l2_rect loop_vid_copy;
428 /* The part of the output buffer that (after scaling) corresponds to loop_vid_copy. */
429 struct v4l2_rect loop_vid_out;
430 /* The part of the capture buffer that (after scaling) corresponds to loop_vid_copy. */
431 struct v4l2_rect loop_vid_cap;
433 * The intersection of the framebuffer, the overlay output window and
434 * loop_vid_copy. I.e., the part of the framebuffer that actually should be
435 * blended with the compose_out rectangle. This uses the framebuffer origin.
437 struct v4l2_rect loop_fb_copy;
438 /* The same as loop_fb_copy but with compose_out origin. */
439 struct v4l2_rect loop_vid_overlay;
441 * The part of the capture buffer that (after scaling) corresponds
442 * to loop_vid_overlay.
444 struct v4l2_rect loop_vid_overlay_cap;
446 /* thread for generating video output stream */
447 struct task_struct *kthread_vid_out;
448 unsigned long jiffies_vid_out;
452 u32 vid_out_seq_start;
453 u32 vid_out_seq_count;
454 bool vid_out_streaming;
455 u32 vbi_out_seq_start;
456 u32 vbi_out_seq_count;
457 bool vbi_out_streaming;
458 bool stream_sliced_vbi_out;
461 struct vb2_queue vb_sdr_cap_q;
462 struct list_head sdr_cap_active;
463 u32 sdr_pixelformat; /* v4l2 format id */
464 unsigned sdr_buffersize;
465 unsigned sdr_adc_freq;
466 unsigned sdr_fm_freq;
467 unsigned sdr_fm_deviation;
468 int sdr_fixp_src_phase;
469 int sdr_fixp_mod_phase;
471 bool tstamp_src_is_soe;
473 bool has_compose_cap;
476 bool has_compose_out;
479 /* thread for generating SDR stream */
480 struct task_struct *kthread_sdr_cap;
481 unsigned long jiffies_sdr_cap;
482 u32 sdr_cap_seq_offset;
483 u32 sdr_cap_seq_count;
484 bool sdr_cap_seq_resync;
487 struct vivid_rds_gen rds_gen;
490 unsigned radio_rx_freq;
491 unsigned radio_rx_audmode;
492 int radio_rx_sig_qual;
493 unsigned radio_rx_hw_seek_mode;
494 bool radio_rx_hw_seek_prog_lim;
495 bool radio_rx_rds_controls;
496 bool radio_rx_rds_enabled;
497 unsigned radio_rx_rds_use_alternates;
498 unsigned radio_rx_rds_last_block;
499 struct v4l2_fh *radio_rx_rds_owner;
501 /* Radio transmitter */
502 unsigned radio_tx_freq;
503 unsigned radio_tx_subchans;
504 bool radio_tx_rds_controls;
505 unsigned radio_tx_rds_last_block;
506 struct v4l2_fh *radio_tx_rds_owner;
508 /* Shared between radio receiver and transmitter */
510 ktime_t radio_rds_init_time;
513 struct cec_adapter *cec_rx_adap;
514 struct cec_adapter *cec_tx_adap[MAX_OUTPUTS];
515 struct workqueue_struct *cec_workqueue;
516 spinlock_t cec_slock;
517 struct list_head cec_work_list;
518 unsigned int cec_xfer_time_jiffies;
519 unsigned long cec_xfer_start_jiffies;
520 u8 cec_output2bus_map[MAX_OUTPUTS];
524 unsigned long osd_jiffies;
527 static inline bool vivid_is_webcam(const struct vivid_dev *dev)
529 return dev->input_type[dev->input] == WEBCAM;
532 static inline bool vivid_is_tv_cap(const struct vivid_dev *dev)
534 return dev->input_type[dev->input] == TV;
537 static inline bool vivid_is_svid_cap(const struct vivid_dev *dev)
539 return dev->input_type[dev->input] == SVID;
542 static inline bool vivid_is_hdmi_cap(const struct vivid_dev *dev)
544 return dev->input_type[dev->input] == HDMI;
547 static inline bool vivid_is_sdtv_cap(const struct vivid_dev *dev)
549 return vivid_is_tv_cap(dev) || vivid_is_svid_cap(dev);
552 static inline bool vivid_is_svid_out(const struct vivid_dev *dev)
554 return dev->output_type[dev->output] == SVID;
557 static inline bool vivid_is_hdmi_out(const struct vivid_dev *dev)
559 return dev->output_type[dev->output] == HDMI;