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 struct v4l2_ctrl_handler ctrl_hdl_user_gen;
140 struct v4l2_ctrl_handler ctrl_hdl_user_vid;
141 struct v4l2_ctrl_handler ctrl_hdl_user_aud;
142 struct v4l2_ctrl_handler ctrl_hdl_streaming;
143 struct v4l2_ctrl_handler ctrl_hdl_sdtv_cap;
144 struct v4l2_ctrl_handler ctrl_hdl_loop_cap;
145 struct v4l2_ctrl_handler ctrl_hdl_fb;
146 struct video_device vid_cap_dev;
147 struct v4l2_ctrl_handler ctrl_hdl_vid_cap;
148 struct video_device vid_out_dev;
149 struct v4l2_ctrl_handler ctrl_hdl_vid_out;
150 struct video_device vbi_cap_dev;
151 struct v4l2_ctrl_handler ctrl_hdl_vbi_cap;
152 struct video_device vbi_out_dev;
153 struct v4l2_ctrl_handler ctrl_hdl_vbi_out;
154 struct video_device radio_rx_dev;
155 struct v4l2_ctrl_handler ctrl_hdl_radio_rx;
156 struct video_device radio_tx_dev;
157 struct v4l2_ctrl_handler ctrl_hdl_radio_tx;
158 struct video_device sdr_cap_dev;
159 struct v4l2_ctrl_handler ctrl_hdl_sdr_cap;
172 /* supported features */
175 u8 input_type[MAX_INPUTS];
176 u8 input_name_counter[MAX_INPUTS];
177 unsigned num_outputs;
178 u8 output_type[MAX_OUTPUTS];
179 u8 output_name_counter[MAX_OUTPUTS];
180 bool has_audio_inputs;
181 bool has_audio_outputs;
185 bool has_raw_vbi_cap;
186 bool has_sliced_vbi_cap;
188 bool has_raw_vbi_out;
189 bool has_sliced_vbi_out;
198 struct v4l2_ctrl *brightness;
199 struct v4l2_ctrl *contrast;
200 struct v4l2_ctrl *saturation;
201 struct v4l2_ctrl *hue;
203 /* autogain/gain cluster */
204 struct v4l2_ctrl *autogain;
205 struct v4l2_ctrl *gain;
207 struct v4l2_ctrl *volume;
208 struct v4l2_ctrl *mute;
209 struct v4l2_ctrl *alpha;
210 struct v4l2_ctrl *button;
211 struct v4l2_ctrl *boolean;
212 struct v4l2_ctrl *int32;
213 struct v4l2_ctrl *int64;
214 struct v4l2_ctrl *menu;
215 struct v4l2_ctrl *string;
216 struct v4l2_ctrl *bitmask;
217 struct v4l2_ctrl *int_menu;
218 struct v4l2_ctrl *test_pattern;
219 struct v4l2_ctrl *colorspace;
220 struct v4l2_ctrl *rgb_range_cap;
221 struct v4l2_ctrl *real_rgb_range_cap;
223 /* std_signal_mode/standard cluster */
224 struct v4l2_ctrl *ctrl_std_signal_mode;
225 struct v4l2_ctrl *ctrl_standard;
228 /* dv_timings_signal_mode/timings cluster */
229 struct v4l2_ctrl *ctrl_dv_timings_signal_mode;
230 struct v4l2_ctrl *ctrl_dv_timings;
232 struct v4l2_ctrl *ctrl_has_crop_cap;
233 struct v4l2_ctrl *ctrl_has_compose_cap;
234 struct v4l2_ctrl *ctrl_has_scaler_cap;
235 struct v4l2_ctrl *ctrl_has_crop_out;
236 struct v4l2_ctrl *ctrl_has_compose_out;
237 struct v4l2_ctrl *ctrl_has_scaler_out;
238 struct v4l2_ctrl *ctrl_tx_mode;
239 struct v4l2_ctrl *ctrl_tx_rgb_range;
241 struct v4l2_ctrl *radio_tx_rds_pi;
242 struct v4l2_ctrl *radio_tx_rds_pty;
243 struct v4l2_ctrl *radio_tx_rds_mono_stereo;
244 struct v4l2_ctrl *radio_tx_rds_art_head;
245 struct v4l2_ctrl *radio_tx_rds_compressed;
246 struct v4l2_ctrl *radio_tx_rds_dyn_pty;
247 struct v4l2_ctrl *radio_tx_rds_ta;
248 struct v4l2_ctrl *radio_tx_rds_tp;
249 struct v4l2_ctrl *radio_tx_rds_ms;
250 struct v4l2_ctrl *radio_tx_rds_psname;
251 struct v4l2_ctrl *radio_tx_rds_radiotext;
253 struct v4l2_ctrl *radio_rx_rds_pty;
254 struct v4l2_ctrl *radio_rx_rds_ta;
255 struct v4l2_ctrl *radio_rx_rds_tp;
256 struct v4l2_ctrl *radio_rx_rds_ms;
257 struct v4l2_ctrl *radio_rx_rds_psname;
258 struct v4l2_ctrl *radio_rx_rds_radiotext;
260 unsigned input_brightness[MAX_INPUTS];
262 unsigned button_pressed;
267 bool vbi_cap_interlaced;
272 unsigned long video_pbase;
274 u32 video_buffer_size;
277 int display_byte_stride;
280 struct fb_info fb_info;
281 struct fb_var_screeninfo fb_defined;
282 struct fb_fix_screeninfo fb_fix;
284 /* Error injection */
285 bool queue_setup_error;
286 bool buf_prepare_error;
287 bool start_streaming_error;
291 u64 time_wrap_offset;
292 unsigned perc_dropped_buffers;
293 enum vivid_signal_mode std_signal_mode;
294 unsigned query_std_last;
295 v4l2_std_id query_std;
296 enum tpg_video_aspect std_aspect_ratio;
298 enum vivid_signal_mode dv_timings_signal_mode;
299 char **query_dv_timings_qmenu;
300 unsigned query_dv_timings_size;
301 unsigned query_dv_timings_last;
302 unsigned query_dv_timings;
303 enum tpg_video_aspect dv_timings_aspect_ratio;
308 struct v4l2_dv_timings dv_timings_cap;
310 struct vivid_vbi_gen_data vbi_gen;
312 unsigned edid_blocks;
313 unsigned edid_max_blocks;
314 unsigned webcam_size_idx;
315 unsigned webcam_ival_idx;
318 unsigned tv_field_cap;
319 unsigned tv_audio_input;
321 /* Capture Overlay */
322 struct v4l2_framebuffer fb_cap;
323 struct v4l2_fh *overlay_cap_owner;
325 int overlay_cap_top, overlay_cap_left;
326 enum v4l2_field overlay_cap_field;
328 struct v4l2_clip clips_cap[MAX_CLIPS];
329 struct v4l2_clip try_clips_cap[MAX_CLIPS];
330 unsigned clipcount_cap;
335 struct v4l2_dv_timings dv_timings_out;
339 u32 quantization_out;
342 unsigned bytesperline_out[TPG_MAX_PLANES];
343 unsigned tv_field_out;
344 unsigned tv_audio_output;
345 bool vbi_out_have_wss;
347 bool vbi_out_have_cc[2];
352 unsigned cur_scaled_line;
356 bool overlay_out_enabled;
357 int overlay_out_top, overlay_out_left;
359 struct v4l2_clip clips_out[MAX_CLIPS];
360 struct v4l2_clip try_clips_out[MAX_CLIPS];
361 unsigned clipcount_out;
362 unsigned fbuf_out_flags;
369 bool must_blank[VIDEO_MAX_FRAME];
371 const struct vivid_fmt *fmt_cap;
372 struct v4l2_fract timeperframe_vid_cap;
373 enum v4l2_field field_cap;
374 struct v4l2_rect src_rect;
375 struct v4l2_rect fmt_cap_rect;
376 struct v4l2_rect crop_cap;
377 struct v4l2_rect compose_cap;
378 struct v4l2_rect crop_bounds_cap;
379 struct vb2_queue vb_vid_cap_q;
380 struct list_head vid_cap_active;
381 struct vb2_queue vb_vbi_cap_q;
382 struct list_head vbi_cap_active;
384 /* thread for generating video capture stream */
385 struct task_struct *kthread_vid_cap;
386 unsigned long jiffies_vid_cap;
390 u32 vid_cap_seq_start;
391 u32 vid_cap_seq_count;
392 bool vid_cap_streaming;
393 u32 vbi_cap_seq_start;
394 u32 vbi_cap_seq_count;
395 bool vbi_cap_streaming;
396 bool stream_sliced_vbi_cap;
399 const struct vivid_fmt *fmt_out;
400 struct v4l2_fract timeperframe_vid_out;
401 enum v4l2_field field_out;
402 struct v4l2_rect sink_rect;
403 struct v4l2_rect fmt_out_rect;
404 struct v4l2_rect crop_out;
405 struct v4l2_rect compose_out;
406 struct v4l2_rect compose_bounds_out;
407 struct vb2_queue vb_vid_out_q;
408 struct list_head vid_out_active;
409 struct vb2_queue vb_vbi_out_q;
410 struct list_head vbi_out_active;
412 /* video loop precalculated rectangles */
415 * Intersection between what the output side composes and the capture side
416 * crops. I.e., what actually needs to be copied from the output buffer to
417 * the capture buffer.
419 struct v4l2_rect loop_vid_copy;
420 /* The part of the output buffer that (after scaling) corresponds to loop_vid_copy. */
421 struct v4l2_rect loop_vid_out;
422 /* The part of the capture buffer that (after scaling) corresponds to loop_vid_copy. */
423 struct v4l2_rect loop_vid_cap;
425 * The intersection of the framebuffer, the overlay output window and
426 * loop_vid_copy. I.e., the part of the framebuffer that actually should be
427 * blended with the compose_out rectangle. This uses the framebuffer origin.
429 struct v4l2_rect loop_fb_copy;
430 /* The same as loop_fb_copy but with compose_out origin. */
431 struct v4l2_rect loop_vid_overlay;
433 * The part of the capture buffer that (after scaling) corresponds
434 * to loop_vid_overlay.
436 struct v4l2_rect loop_vid_overlay_cap;
438 /* thread for generating video output stream */
439 struct task_struct *kthread_vid_out;
440 unsigned long jiffies_vid_out;
444 u32 vid_out_seq_start;
445 u32 vid_out_seq_count;
446 bool vid_out_streaming;
447 u32 vbi_out_seq_start;
448 u32 vbi_out_seq_count;
449 bool vbi_out_streaming;
450 bool stream_sliced_vbi_out;
453 struct vb2_queue vb_sdr_cap_q;
454 struct list_head sdr_cap_active;
455 u32 sdr_pixelformat; /* v4l2 format id */
456 unsigned sdr_buffersize;
457 unsigned sdr_adc_freq;
458 unsigned sdr_fm_freq;
459 unsigned sdr_fm_deviation;
460 int sdr_fixp_src_phase;
461 int sdr_fixp_mod_phase;
463 bool tstamp_src_is_soe;
465 bool has_compose_cap;
468 bool has_compose_out;
471 /* thread for generating SDR stream */
472 struct task_struct *kthread_sdr_cap;
473 unsigned long jiffies_sdr_cap;
474 u32 sdr_cap_seq_offset;
475 u32 sdr_cap_seq_count;
476 bool sdr_cap_seq_resync;
479 struct vivid_rds_gen rds_gen;
482 unsigned radio_rx_freq;
483 unsigned radio_rx_audmode;
484 int radio_rx_sig_qual;
485 unsigned radio_rx_hw_seek_mode;
486 bool radio_rx_hw_seek_prog_lim;
487 bool radio_rx_rds_controls;
488 bool radio_rx_rds_enabled;
489 unsigned radio_rx_rds_use_alternates;
490 unsigned radio_rx_rds_last_block;
491 struct v4l2_fh *radio_rx_rds_owner;
493 /* Radio transmitter */
494 unsigned radio_tx_freq;
495 unsigned radio_tx_subchans;
496 bool radio_tx_rds_controls;
497 unsigned radio_tx_rds_last_block;
498 struct v4l2_fh *radio_tx_rds_owner;
500 /* Shared between radio receiver and transmitter */
502 ktime_t radio_rds_init_time;
505 struct cec_adapter *cec_rx_adap;
506 struct cec_adapter *cec_tx_adap[MAX_OUTPUTS];
507 struct workqueue_struct *cec_workqueue;
508 spinlock_t cec_slock;
509 struct list_head cec_work_list;
510 unsigned int cec_xfer_time_jiffies;
511 unsigned long cec_xfer_start_jiffies;
512 u8 cec_output2bus_map[MAX_OUTPUTS];
516 unsigned long osd_jiffies;
519 static inline bool vivid_is_webcam(const struct vivid_dev *dev)
521 return dev->input_type[dev->input] == WEBCAM;
524 static inline bool vivid_is_tv_cap(const struct vivid_dev *dev)
526 return dev->input_type[dev->input] == TV;
529 static inline bool vivid_is_svid_cap(const struct vivid_dev *dev)
531 return dev->input_type[dev->input] == SVID;
534 static inline bool vivid_is_hdmi_cap(const struct vivid_dev *dev)
536 return dev->input_type[dev->input] == HDMI;
539 static inline bool vivid_is_sdtv_cap(const struct vivid_dev *dev)
541 return vivid_is_tv_cap(dev) || vivid_is_svid_cap(dev);
544 static inline bool vivid_is_svid_out(const struct vivid_dev *dev)
546 return dev->output_type[dev->output] == SVID;
549 static inline bool vivid_is_hdmi_out(const struct vivid_dev *dev)
551 return dev->output_type[dev->output] == HDMI;
554 bool vivid_validate_fb(const struct v4l2_framebuffer *a);