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 /* The maximum number of clip rectangles */
27 /* The maximum number of inputs */
29 /* The maximum number of outputs */
30 #define MAX_OUTPUTS 16
31 /* The maximum up or down scaling factor is 4 */
33 /* The maximum image width/height are set to 4K DMT */
34 #define MAX_WIDTH 4096
35 #define MAX_HEIGHT 2160
36 /* The minimum image width/height */
39 /* The data_offset of plane 0 for the multiplanar formats */
40 #define PLANE0_DATA_OFFSET 128
42 /* The supported TV frequency range in MHz */
43 #define MIN_TV_FREQ (44U * 16U)
44 #define MAX_TV_FREQ (958U * 16U)
46 /* The number of samples returned in every SDR buffer */
47 #define SDR_CAP_SAMPLES_PER_BUF 0x4000
49 /* used by the threads to know when to resync internal counters */
50 #define JIFFIES_PER_DAY (3600U * 24U * HZ)
51 #define JIFFIES_RESYNC (JIFFIES_PER_DAY * (0xf0000000U / JIFFIES_PER_DAY))
53 extern const struct v4l2_rect vivid_min_rect;
54 extern const struct v4l2_rect vivid_max_rect;
55 extern unsigned vivid_debug;
58 u32 fourcc; /* v4l2 format id */
59 enum tgp_color_enc color_enc;
61 u8 vdownsampling[TPG_MAX_PLANES];
65 u32 data_offset[TPG_MAX_PLANES];
66 u32 bit_depth[TPG_MAX_PLANES];
69 extern struct vivid_fmt vivid_formats[];
71 /* buffer for one video frame */
73 /* common v4l buffer stuff -- must be first */
74 struct vb2_v4l2_buffer vb;
75 struct list_head list;
85 enum vivid_signal_mode {
87 CURRENT_STD = CURRENT_DV_TIMINGS,
92 SELECTED_STD = SELECTED_DV_TIMINGS,
94 CYCLE_STD = CYCLE_DV_TIMINGS,
98 enum vivid_colorspace {
110 #define VIVID_INVALID_SIGNAL(mode) \
111 ((mode) == NO_SIGNAL || (mode) == NO_LOCK || (mode) == OUT_OF_RANGE)
113 struct vivid_cec_work {
114 struct list_head list;
115 struct delayed_work work;
116 struct cec_adapter *adap;
117 struct vivid_dev *dev;
119 unsigned int timeout_ms;
126 struct v4l2_device v4l2_dev;
127 #ifdef CONFIG_MEDIA_CONTROLLER
128 struct media_device mdev;
129 struct media_pad vid_cap_pad;
130 struct media_pad vid_out_pad;
131 struct media_pad vbi_cap_pad;
132 struct media_pad vbi_out_pad;
133 struct media_pad sdr_cap_pad;
135 struct v4l2_ctrl_handler ctrl_hdl_user_gen;
136 struct v4l2_ctrl_handler ctrl_hdl_user_vid;
137 struct v4l2_ctrl_handler ctrl_hdl_user_aud;
138 struct v4l2_ctrl_handler ctrl_hdl_streaming;
139 struct v4l2_ctrl_handler ctrl_hdl_sdtv_cap;
140 struct v4l2_ctrl_handler ctrl_hdl_loop_cap;
141 struct v4l2_ctrl_handler ctrl_hdl_fb;
142 struct video_device vid_cap_dev;
143 struct v4l2_ctrl_handler ctrl_hdl_vid_cap;
144 struct video_device vid_out_dev;
145 struct v4l2_ctrl_handler ctrl_hdl_vid_out;
146 struct video_device vbi_cap_dev;
147 struct v4l2_ctrl_handler ctrl_hdl_vbi_cap;
148 struct video_device vbi_out_dev;
149 struct v4l2_ctrl_handler ctrl_hdl_vbi_out;
150 struct video_device radio_rx_dev;
151 struct v4l2_ctrl_handler ctrl_hdl_radio_rx;
152 struct video_device radio_tx_dev;
153 struct v4l2_ctrl_handler ctrl_hdl_radio_tx;
154 struct video_device sdr_cap_dev;
155 struct v4l2_ctrl_handler ctrl_hdl_sdr_cap;
168 /* supported features */
171 unsigned int num_hdmi_inputs;
172 u8 input_type[MAX_INPUTS];
173 u8 input_name_counter[MAX_INPUTS];
174 unsigned num_outputs;
175 unsigned int num_hdmi_outputs;
176 u8 output_type[MAX_OUTPUTS];
177 u8 output_name_counter[MAX_OUTPUTS];
178 bool has_audio_inputs;
179 bool has_audio_outputs;
183 bool has_raw_vbi_cap;
184 bool has_sliced_vbi_cap;
186 bool has_raw_vbi_out;
187 bool has_sliced_vbi_out;
196 struct v4l2_ctrl *brightness;
197 struct v4l2_ctrl *contrast;
198 struct v4l2_ctrl *saturation;
199 struct v4l2_ctrl *hue;
201 /* autogain/gain cluster */
202 struct v4l2_ctrl *autogain;
203 struct v4l2_ctrl *gain;
205 struct v4l2_ctrl *volume;
206 struct v4l2_ctrl *mute;
207 struct v4l2_ctrl *alpha;
208 struct v4l2_ctrl *button;
209 struct v4l2_ctrl *boolean;
210 struct v4l2_ctrl *int32;
211 struct v4l2_ctrl *int64;
212 struct v4l2_ctrl *menu;
213 struct v4l2_ctrl *string;
214 struct v4l2_ctrl *bitmask;
215 struct v4l2_ctrl *int_menu;
216 struct v4l2_ctrl *test_pattern;
217 struct v4l2_ctrl *colorspace;
218 struct v4l2_ctrl *rgb_range_cap;
219 struct v4l2_ctrl *real_rgb_range_cap;
221 /* std_signal_mode/standard cluster */
222 struct v4l2_ctrl *ctrl_std_signal_mode;
223 struct v4l2_ctrl *ctrl_standard;
226 /* dv_timings_signal_mode/timings cluster */
227 struct v4l2_ctrl *ctrl_dv_timings_signal_mode;
228 struct v4l2_ctrl *ctrl_dv_timings;
230 struct v4l2_ctrl *ctrl_display_present;
231 struct v4l2_ctrl *ctrl_has_crop_cap;
232 struct v4l2_ctrl *ctrl_has_compose_cap;
233 struct v4l2_ctrl *ctrl_has_scaler_cap;
234 struct v4l2_ctrl *ctrl_has_crop_out;
235 struct v4l2_ctrl *ctrl_has_compose_out;
236 struct v4l2_ctrl *ctrl_has_scaler_out;
237 struct v4l2_ctrl *ctrl_tx_mode;
238 struct v4l2_ctrl *ctrl_tx_rgb_range;
239 struct v4l2_ctrl *ctrl_tx_edid_present;
240 struct v4l2_ctrl *ctrl_tx_hotplug;
241 struct v4l2_ctrl *ctrl_tx_rxsense;
243 struct v4l2_ctrl *ctrl_rx_power_present;
245 struct v4l2_ctrl *radio_tx_rds_pi;
246 struct v4l2_ctrl *radio_tx_rds_pty;
247 struct v4l2_ctrl *radio_tx_rds_mono_stereo;
248 struct v4l2_ctrl *radio_tx_rds_art_head;
249 struct v4l2_ctrl *radio_tx_rds_compressed;
250 struct v4l2_ctrl *radio_tx_rds_dyn_pty;
251 struct v4l2_ctrl *radio_tx_rds_ta;
252 struct v4l2_ctrl *radio_tx_rds_tp;
253 struct v4l2_ctrl *radio_tx_rds_ms;
254 struct v4l2_ctrl *radio_tx_rds_psname;
255 struct v4l2_ctrl *radio_tx_rds_radiotext;
257 struct v4l2_ctrl *radio_rx_rds_pty;
258 struct v4l2_ctrl *radio_rx_rds_ta;
259 struct v4l2_ctrl *radio_rx_rds_tp;
260 struct v4l2_ctrl *radio_rx_rds_ms;
261 struct v4l2_ctrl *radio_rx_rds_psname;
262 struct v4l2_ctrl *radio_rx_rds_radiotext;
264 unsigned input_brightness[MAX_INPUTS];
266 unsigned button_pressed;
271 bool vbi_cap_interlaced;
276 unsigned long video_pbase;
278 u32 video_buffer_size;
281 int display_byte_stride;
284 struct fb_info fb_info;
285 struct fb_var_screeninfo fb_defined;
286 struct fb_fix_screeninfo fb_fix;
288 /* Error injection */
289 bool queue_setup_error;
290 bool buf_prepare_error;
291 bool start_streaming_error;
293 bool req_validate_error;
296 u64 time_wrap_offset;
297 unsigned perc_dropped_buffers;
298 enum vivid_signal_mode std_signal_mode[MAX_INPUTS];
299 unsigned int query_std_last[MAX_INPUTS];
300 v4l2_std_id query_std[MAX_INPUTS];
301 enum tpg_video_aspect std_aspect_ratio[MAX_INPUTS];
303 enum vivid_signal_mode dv_timings_signal_mode[MAX_INPUTS];
304 char **query_dv_timings_qmenu;
305 char *query_dv_timings_qmenu_strings;
306 unsigned query_dv_timings_size;
307 unsigned int query_dv_timings_last[MAX_INPUTS];
308 unsigned int query_dv_timings[MAX_INPUTS];
309 enum tpg_video_aspect dv_timings_aspect_ratio[MAX_INPUTS];
313 v4l2_std_id std_cap[MAX_INPUTS];
314 struct v4l2_dv_timings dv_timings_cap[MAX_INPUTS];
315 int dv_timings_cap_sel[MAX_INPUTS];
317 struct vivid_vbi_gen_data vbi_gen;
319 unsigned edid_blocks;
320 unsigned edid_max_blocks;
321 unsigned webcam_size_idx;
322 unsigned webcam_ival_idx;
325 unsigned tv_field_cap;
326 unsigned tv_audio_input;
330 /* Capture Overlay */
331 struct v4l2_framebuffer fb_cap;
332 struct v4l2_fh *overlay_cap_owner;
334 int overlay_cap_top, overlay_cap_left;
335 enum v4l2_field overlay_cap_field;
337 struct v4l2_clip clips_cap[MAX_CLIPS];
338 struct v4l2_clip try_clips_cap[MAX_CLIPS];
339 unsigned clipcount_cap;
344 struct v4l2_dv_timings dv_timings_out;
348 u32 quantization_out;
351 unsigned bytesperline_out[TPG_MAX_PLANES];
352 unsigned tv_field_out;
353 unsigned tv_audio_output;
354 bool vbi_out_have_wss;
356 bool vbi_out_have_cc[2];
361 unsigned cur_scaled_line;
362 bool display_present[MAX_OUTPUTS];
366 bool overlay_out_enabled;
367 int overlay_out_top, overlay_out_left;
369 struct v4l2_clip clips_out[MAX_CLIPS];
370 struct v4l2_clip try_clips_out[MAX_CLIPS];
371 unsigned clipcount_out;
372 unsigned fbuf_out_flags;
379 bool must_blank[VIDEO_MAX_FRAME];
381 const struct vivid_fmt *fmt_cap;
382 struct v4l2_fract timeperframe_vid_cap;
383 enum v4l2_field field_cap;
384 struct v4l2_rect src_rect;
385 struct v4l2_rect fmt_cap_rect;
386 struct v4l2_rect crop_cap;
387 struct v4l2_rect compose_cap;
388 struct v4l2_rect crop_bounds_cap;
389 struct vb2_queue vb_vid_cap_q;
390 struct list_head vid_cap_active;
391 struct vb2_queue vb_vbi_cap_q;
392 struct list_head vbi_cap_active;
394 /* thread for generating video capture stream */
395 struct task_struct *kthread_vid_cap;
396 unsigned long jiffies_vid_cap;
397 u64 cap_stream_start;
398 u64 cap_frame_period;
399 u64 cap_frame_eof_offset;
403 u32 vid_cap_seq_start;
404 u32 vid_cap_seq_count;
405 bool vid_cap_streaming;
406 u32 vbi_cap_seq_start;
407 u32 vbi_cap_seq_count;
408 bool vbi_cap_streaming;
409 bool stream_sliced_vbi_cap;
412 const struct vivid_fmt *fmt_out;
413 struct v4l2_fract timeperframe_vid_out;
414 enum v4l2_field field_out;
415 struct v4l2_rect sink_rect;
416 struct v4l2_rect fmt_out_rect;
417 struct v4l2_rect crop_out;
418 struct v4l2_rect compose_out;
419 struct v4l2_rect compose_bounds_out;
420 struct vb2_queue vb_vid_out_q;
421 struct list_head vid_out_active;
422 struct vb2_queue vb_vbi_out_q;
423 struct list_head vbi_out_active;
425 /* video loop precalculated rectangles */
428 * Intersection between what the output side composes and the capture side
429 * crops. I.e., what actually needs to be copied from the output buffer to
430 * the capture buffer.
432 struct v4l2_rect loop_vid_copy;
433 /* The part of the output buffer that (after scaling) corresponds to loop_vid_copy. */
434 struct v4l2_rect loop_vid_out;
435 /* The part of the capture buffer that (after scaling) corresponds to loop_vid_copy. */
436 struct v4l2_rect loop_vid_cap;
438 * The intersection of the framebuffer, the overlay output window and
439 * loop_vid_copy. I.e., the part of the framebuffer that actually should be
440 * blended with the compose_out rectangle. This uses the framebuffer origin.
442 struct v4l2_rect loop_fb_copy;
443 /* The same as loop_fb_copy but with compose_out origin. */
444 struct v4l2_rect loop_vid_overlay;
446 * The part of the capture buffer that (after scaling) corresponds
447 * to loop_vid_overlay.
449 struct v4l2_rect loop_vid_overlay_cap;
451 /* thread for generating video output stream */
452 struct task_struct *kthread_vid_out;
453 unsigned long jiffies_vid_out;
457 u32 vid_out_seq_start;
458 u32 vid_out_seq_count;
459 bool vid_out_streaming;
460 u32 vbi_out_seq_start;
461 u32 vbi_out_seq_count;
462 bool vbi_out_streaming;
463 bool stream_sliced_vbi_out;
466 struct vb2_queue vb_sdr_cap_q;
467 struct list_head sdr_cap_active;
468 u32 sdr_pixelformat; /* v4l2 format id */
469 unsigned sdr_buffersize;
470 unsigned sdr_adc_freq;
471 unsigned sdr_fm_freq;
472 unsigned sdr_fm_deviation;
473 int sdr_fixp_src_phase;
474 int sdr_fixp_mod_phase;
476 bool tstamp_src_is_soe;
478 bool has_compose_cap;
481 bool has_compose_out;
484 /* thread for generating SDR stream */
485 struct task_struct *kthread_sdr_cap;
486 unsigned long jiffies_sdr_cap;
487 u32 sdr_cap_seq_offset;
488 u32 sdr_cap_seq_count;
489 bool sdr_cap_seq_resync;
492 struct vivid_rds_gen rds_gen;
495 unsigned radio_rx_freq;
496 unsigned radio_rx_audmode;
497 int radio_rx_sig_qual;
498 unsigned radio_rx_hw_seek_mode;
499 bool radio_rx_hw_seek_prog_lim;
500 bool radio_rx_rds_controls;
501 bool radio_rx_rds_enabled;
502 unsigned radio_rx_rds_use_alternates;
503 unsigned radio_rx_rds_last_block;
504 struct v4l2_fh *radio_rx_rds_owner;
506 /* Radio transmitter */
507 unsigned radio_tx_freq;
508 unsigned radio_tx_subchans;
509 bool radio_tx_rds_controls;
510 unsigned radio_tx_rds_last_block;
511 struct v4l2_fh *radio_tx_rds_owner;
513 /* Shared between radio receiver and transmitter */
515 ktime_t radio_rds_init_time;
518 struct cec_adapter *cec_rx_adap;
519 struct cec_adapter *cec_tx_adap[MAX_OUTPUTS];
520 struct workqueue_struct *cec_workqueue;
521 spinlock_t cec_slock;
522 struct list_head cec_work_list;
523 unsigned int cec_xfer_time_jiffies;
524 unsigned long cec_xfer_start_jiffies;
525 u8 cec_output2bus_map[MAX_OUTPUTS];
529 unsigned long osd_jiffies;
532 static inline bool vivid_is_webcam(const struct vivid_dev *dev)
534 return dev->input_type[dev->input] == WEBCAM;
537 static inline bool vivid_is_tv_cap(const struct vivid_dev *dev)
539 return dev->input_type[dev->input] == TV;
542 static inline bool vivid_is_svid_cap(const struct vivid_dev *dev)
544 return dev->input_type[dev->input] == SVID;
547 static inline bool vivid_is_hdmi_cap(const struct vivid_dev *dev)
549 return dev->input_type[dev->input] == HDMI;
552 static inline bool vivid_is_sdtv_cap(const struct vivid_dev *dev)
554 return vivid_is_tv_cap(dev) || vivid_is_svid_cap(dev);
557 static inline bool vivid_is_svid_out(const struct vivid_dev *dev)
559 return dev->output_type[dev->output] == SVID;
562 static inline bool vivid_is_hdmi_out(const struct vivid_dev *dev)
564 return dev->output_type[dev->output] == HDMI;