1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3 * vivid-core.h - core datastructures
4 *
5 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6 */
7
8 #ifndef _VIVID_CORE_H_
9 #define _VIVID_CORE_H_
10
11 #include <linux/fb.h>
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"
21
22 #define dprintk(dev, level, fmt, arg...) \
23 v4l2_dbg(level, vivid_debug, &dev->v4l2_dev, fmt, ## arg)
24
25 /* The maximum number of inputs */
26 #define MAX_INPUTS 16
27 /* The maximum number of outputs */
28 #define MAX_OUTPUTS 16
29 /* The maximum up or down scaling factor is 4 */
30 #define MAX_ZOOM 4
31 /* The maximum image width/height are set to 4K DMT */
32 #define MAX_WIDTH 4096
33 #define MAX_HEIGHT 2160
34 /* The minimum image width/height */
35 #define MIN_WIDTH 16
36 #define MIN_HEIGHT MIN_WIDTH
37 /* Pixel Array control divider */
38 #define PIXEL_ARRAY_DIV MIN_WIDTH
39 /* The data_offset of plane 0 for the multiplanar formats */
40 #define PLANE0_DATA_OFFSET 128
41
42 /* The supported TV frequency range in MHz */
43 #define MIN_TV_FREQ (44U * 16U)
44 #define MAX_TV_FREQ (958U * 16U)
45
46 /* The number of samples returned in every SDR buffer */
47 #define SDR_CAP_SAMPLES_PER_BUF 0x4000
48
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))
52
53 extern const struct v4l2_rect vivid_min_rect;
54 extern const struct v4l2_rect vivid_max_rect;
55 extern unsigned vivid_debug;
56
57 struct vivid_fmt {
58 u32 fourcc; /* v4l2 format id */
59 enum tgp_color_enc color_enc;
60 bool can_do_overlay;
61 u8 vdownsampling[TPG_MAX_PLANES];
62 u32 alpha_mask;
63 u8 planes;
64 u8 buffers;
65 u32 data_offset[TPG_MAX_PLANES];
66 u32 bit_depth[TPG_MAX_PLANES];
67 };
68
69 extern struct vivid_fmt vivid_formats[];
70
71 /* buffer for one video frame */
72 struct vivid_buffer {
73 /* common v4l buffer stuff -- must be first */
74 struct vb2_v4l2_buffer vb;
75 struct list_head list;
76 };
77
78 enum vivid_input {
79 WEBCAM,
80 TV,
81 SVID,
82 HDMI,
83 };
84
85 enum vivid_signal_mode {
86 CURRENT_DV_TIMINGS,
87 CURRENT_STD = CURRENT_DV_TIMINGS,
88 NO_SIGNAL,
89 NO_LOCK,
90 OUT_OF_RANGE,
91 SELECTED_DV_TIMINGS,
92 SELECTED_STD = SELECTED_DV_TIMINGS,
93 CYCLE_DV_TIMINGS,
94 CYCLE_STD = CYCLE_DV_TIMINGS,
95 CUSTOM_DV_TIMINGS,
96 };
97
98 enum vivid_colorspace {
99 VIVID_CS_170M,
100 VIVID_CS_709,
101 VIVID_CS_SRGB,
102 VIVID_CS_OPRGB,
103 VIVID_CS_2020,
104 VIVID_CS_DCI_P3,
105 VIVID_CS_240M,
106 VIVID_CS_SYS_M,
107 VIVID_CS_SYS_BG,
108 };
109
110 #define VIVID_INVALID_SIGNAL(mode) \
111 ((mode) == NO_SIGNAL || (mode) == NO_LOCK || (mode) == OUT_OF_RANGE)
112
113 struct vivid_cec_xfer {
114 struct cec_adapter *adap;
115 u8 msg[CEC_MAX_MSG_SIZE];
116 u32 len;
117 u32 sft;
118 };
119
120 struct vivid_dev {
121 unsigned inst;
122 struct v4l2_device v4l2_dev;
123 #ifdef CONFIG_MEDIA_CONTROLLER
124 struct media_device mdev;
125 struct media_pad vid_cap_pad;
126 struct media_pad vid_out_pad;
127 struct media_pad vbi_cap_pad;
128 struct media_pad vbi_out_pad;
129 struct media_pad sdr_cap_pad;
130 struct media_pad meta_cap_pad;
131 struct media_pad meta_out_pad;
132 struct media_pad touch_cap_pad;
133 #endif
134 struct v4l2_ctrl_handler ctrl_hdl_user_gen;
135 struct v4l2_ctrl_handler ctrl_hdl_user_vid;
136 struct v4l2_ctrl_handler ctrl_hdl_user_aud;
137 struct v4l2_ctrl_handler ctrl_hdl_streaming;
138 struct v4l2_ctrl_handler ctrl_hdl_sdtv_cap;
139 struct v4l2_ctrl_handler ctrl_hdl_loop_cap;
140 struct v4l2_ctrl_handler ctrl_hdl_fb;
141 struct video_device vid_cap_dev;
142 struct v4l2_ctrl_handler ctrl_hdl_vid_cap;
143 struct video_device vid_out_dev;
144 struct v4l2_ctrl_handler ctrl_hdl_vid_out;
145 struct video_device vbi_cap_dev;
146 struct v4l2_ctrl_handler ctrl_hdl_vbi_cap;
147 struct video_device vbi_out_dev;
148 struct v4l2_ctrl_handler ctrl_hdl_vbi_out;
149 struct video_device radio_rx_dev;
150 struct v4l2_ctrl_handler ctrl_hdl_radio_rx;
151 struct video_device radio_tx_dev;
152 struct v4l2_ctrl_handler ctrl_hdl_radio_tx;
153 struct video_device sdr_cap_dev;
154 struct v4l2_ctrl_handler ctrl_hdl_sdr_cap;
155 struct video_device meta_cap_dev;
156 struct v4l2_ctrl_handler ctrl_hdl_meta_cap;
157 struct video_device meta_out_dev;
158 struct v4l2_ctrl_handler ctrl_hdl_meta_out;
159 struct video_device touch_cap_dev;
160 struct v4l2_ctrl_handler ctrl_hdl_touch_cap;
161
162 spinlock_t slock;
163 struct mutex mutex;
164
165 /* capabilities */
166 u32 vid_cap_caps;
167 u32 vid_out_caps;
168 u32 vbi_cap_caps;
169 u32 vbi_out_caps;
170 u32 sdr_cap_caps;
171 u32 radio_rx_caps;
172 u32 radio_tx_caps;
173 u32 meta_cap_caps;
174 u32 meta_out_caps;
175 u32 touch_cap_caps;
176
177 /* supported features */
178 bool multiplanar;
179 unsigned num_inputs;
180 unsigned int num_hdmi_inputs;
181 u8 input_type[MAX_INPUTS];
182 u8 input_name_counter[MAX_INPUTS];
183 unsigned num_outputs;
184 unsigned int num_hdmi_outputs;
185 u8 output_type[MAX_OUTPUTS];
186 u8 output_name_counter[MAX_OUTPUTS];
187 bool has_audio_inputs;
188 bool has_audio_outputs;
189 bool has_vid_cap;
190 bool has_vid_out;
191 bool has_vbi_cap;
192 bool has_raw_vbi_cap;
193 bool has_sliced_vbi_cap;
194 bool has_vbi_out;
195 bool has_raw_vbi_out;
196 bool has_sliced_vbi_out;
197 bool has_radio_rx;
198 bool has_radio_tx;
199 bool has_sdr_cap;
200 bool has_fb;
201 bool has_meta_cap;
202 bool has_meta_out;
203 bool has_tv_tuner;
204 bool has_touch_cap;
205
206 bool can_loop_video;
207
208 /* controls */
209 struct v4l2_ctrl *brightness;
210 struct v4l2_ctrl *contrast;
211 struct v4l2_ctrl *saturation;
212 struct v4l2_ctrl *hue;
213 struct {
214 /* autogain/gain cluster */
215 struct v4l2_ctrl *autogain;
216 struct v4l2_ctrl *gain;
217 };
218 struct v4l2_ctrl *volume;
219 struct v4l2_ctrl *mute;
220 struct v4l2_ctrl *alpha;
221 struct v4l2_ctrl *button;
222 struct v4l2_ctrl *boolean;
223 struct v4l2_ctrl *int32;
224 struct v4l2_ctrl *int64;
225 struct v4l2_ctrl *menu;
226 struct v4l2_ctrl *string;
227 struct v4l2_ctrl *bitmask;
228 struct v4l2_ctrl *int_menu;
229 struct v4l2_ctrl *ro_int32;
230 struct v4l2_ctrl *pixel_array;
231 struct v4l2_ctrl *test_pattern;
232 struct v4l2_ctrl *colorspace;
233 struct v4l2_ctrl *rgb_range_cap;
234 struct v4l2_ctrl *real_rgb_range_cap;
235 struct {
236 /* std_signal_mode/standard cluster */
237 struct v4l2_ctrl *ctrl_std_signal_mode;
238 struct v4l2_ctrl *ctrl_standard;
239 };
240 struct {
241 /* dv_timings_signal_mode/timings cluster */
242 struct v4l2_ctrl *ctrl_dv_timings_signal_mode;
243 struct v4l2_ctrl *ctrl_dv_timings;
244 };
245 struct v4l2_ctrl *ctrl_display_present;
246 struct v4l2_ctrl *ctrl_has_crop_cap;
247 struct v4l2_ctrl *ctrl_has_compose_cap;
248 struct v4l2_ctrl *ctrl_has_scaler_cap;
249 struct v4l2_ctrl *ctrl_has_crop_out;
250 struct v4l2_ctrl *ctrl_has_compose_out;
251 struct v4l2_ctrl *ctrl_has_scaler_out;
252 struct v4l2_ctrl *ctrl_tx_mode;
253 struct v4l2_ctrl *ctrl_tx_rgb_range;
254 struct v4l2_ctrl *ctrl_tx_edid_present;
255 struct v4l2_ctrl *ctrl_tx_hotplug;
256 struct v4l2_ctrl *ctrl_tx_rxsense;
257
258 struct v4l2_ctrl *ctrl_rx_power_present;
259
260 struct v4l2_ctrl *radio_tx_rds_pi;
261 struct v4l2_ctrl *radio_tx_rds_pty;
262 struct v4l2_ctrl *radio_tx_rds_mono_stereo;
263 struct v4l2_ctrl *radio_tx_rds_art_head;
264 struct v4l2_ctrl *radio_tx_rds_compressed;
265 struct v4l2_ctrl *radio_tx_rds_dyn_pty;
266 struct v4l2_ctrl *radio_tx_rds_ta;
267 struct v4l2_ctrl *radio_tx_rds_tp;
268 struct v4l2_ctrl *radio_tx_rds_ms;
269 struct v4l2_ctrl *radio_tx_rds_psname;
270 struct v4l2_ctrl *radio_tx_rds_radiotext;
271
272 struct v4l2_ctrl *radio_rx_rds_pty;
273 struct v4l2_ctrl *radio_rx_rds_ta;
274 struct v4l2_ctrl *radio_rx_rds_tp;
275 struct v4l2_ctrl *radio_rx_rds_ms;
276 struct v4l2_ctrl *radio_rx_rds_psname;
277 struct v4l2_ctrl *radio_rx_rds_radiotext;
278
279 unsigned input_brightness[MAX_INPUTS];
280 unsigned osd_mode;
281 unsigned button_pressed;
282 bool sensor_hflip;
283 bool sensor_vflip;
284 bool hflip;
285 bool vflip;
286 bool vbi_cap_interlaced;
287 bool loop_video;
288 bool reduced_fps;
289
290 /* Framebuffer */
291 unsigned long video_pbase;
292 void *video_vbase;
293 u32 video_buffer_size;
294 int display_width;
295 int display_height;
296 int display_byte_stride;
297 int bits_per_pixel;
298 int bytes_per_pixel;
299 struct fb_info fb_info;
300 struct fb_var_screeninfo fb_defined;
301 struct fb_fix_screeninfo fb_fix;
302
303 /* Error injection */
304 bool disconnect_error;
305 bool queue_setup_error;
306 bool buf_prepare_error;
307 bool start_streaming_error;
308 bool dqbuf_error;
309 bool req_validate_error;
310 bool seq_wrap;
311 u64 time_wrap;
312 u64 time_wrap_offset;
313 unsigned perc_dropped_buffers;
314 enum vivid_signal_mode std_signal_mode[MAX_INPUTS];
315 unsigned int query_std_last[MAX_INPUTS];
316 v4l2_std_id query_std[MAX_INPUTS];
317 enum tpg_video_aspect std_aspect_ratio[MAX_INPUTS];
318
319 enum vivid_signal_mode dv_timings_signal_mode[MAX_INPUTS];
320 char **query_dv_timings_qmenu;
321 char *query_dv_timings_qmenu_strings;
322 unsigned query_dv_timings_size;
323 unsigned int query_dv_timings_last[MAX_INPUTS];
324 unsigned int query_dv_timings[MAX_INPUTS];
325 enum tpg_video_aspect dv_timings_aspect_ratio[MAX_INPUTS];
326
327 /* Input */
328 unsigned input;
329 v4l2_std_id std_cap[MAX_INPUTS];
330 struct v4l2_dv_timings dv_timings_cap[MAX_INPUTS];
331 int dv_timings_cap_sel[MAX_INPUTS];
332 u32 service_set_cap;
333 struct vivid_vbi_gen_data vbi_gen;
334 u8 *edid;
335 unsigned edid_blocks;
336 unsigned edid_max_blocks;
337 unsigned webcam_size_idx;
338 unsigned webcam_ival_idx;
339 unsigned tv_freq;
340 unsigned tv_audmode;
341 unsigned tv_field_cap;
342 unsigned tv_audio_input;
343
344 u32 power_present;
345
346 /* Output */
347 unsigned output;
348 v4l2_std_id std_out;
349 struct v4l2_dv_timings dv_timings_out;
350 u32 colorspace_out;
351 u32 ycbcr_enc_out;
352 u32 hsv_enc_out;
353 u32 quantization_out;
354 u32 xfer_func_out;
355 u32 service_set_out;
356 unsigned bytesperline_out[TPG_MAX_PLANES];
357 unsigned tv_field_out;
358 unsigned tv_audio_output;
359 bool vbi_out_have_wss;
360 u8 vbi_out_wss[2];
361 bool vbi_out_have_cc[2];
362 u8 vbi_out_cc[2][2];
363 bool dvi_d_out;
364 u8 *scaled_line;
365 u8 *blended_line;
366 unsigned cur_scaled_line;
367 bool display_present[MAX_OUTPUTS];
368
369 /* Output Overlay */
370 void *fb_vbase_out;
371 bool overlay_out_enabled;
372 int overlay_out_top, overlay_out_left;
373 unsigned fbuf_out_flags;
374 u32 chromakey_out;
375 u8 global_alpha_out;
376
377 /* video capture */
378 struct tpg_data tpg;
379 unsigned ms_vid_cap;
380 bool must_blank[VIDEO_MAX_FRAME];
381
382 const struct vivid_fmt *fmt_cap;
383 struct v4l2_fract timeperframe_vid_cap;
384 enum v4l2_field field_cap;
385 struct v4l2_rect src_rect;
386 struct v4l2_rect fmt_cap_rect;
387 struct v4l2_rect crop_cap;
388 struct v4l2_rect compose_cap;
389 struct v4l2_rect crop_bounds_cap;
390 struct vb2_queue vb_vid_cap_q;
391 struct list_head vid_cap_active;
392 struct vb2_queue vb_vbi_cap_q;
393 struct list_head vbi_cap_active;
394 struct vb2_queue vb_meta_cap_q;
395 struct list_head meta_cap_active;
396 struct vb2_queue vb_touch_cap_q;
397 struct list_head touch_cap_active;
398
399 /* thread for generating video capture stream */
400 struct task_struct *kthread_vid_cap;
401 unsigned long jiffies_vid_cap;
402 u64 cap_stream_start;
403 u64 cap_frame_period;
404 u64 cap_frame_eof_offset;
405 u32 cap_seq_offset;
406 u32 cap_seq_count;
407 bool cap_seq_resync;
408 u32 vid_cap_seq_start;
409 u32 vid_cap_seq_count;
410 bool vid_cap_streaming;
411 u32 vbi_cap_seq_start;
412 u32 vbi_cap_seq_count;
413 bool vbi_cap_streaming;
414 u32 meta_cap_seq_start;
415 u32 meta_cap_seq_count;
416 bool meta_cap_streaming;
417
418 /* Touch capture */
419 struct task_struct *kthread_touch_cap;
420 unsigned long jiffies_touch_cap;
421 u64 touch_cap_stream_start;
422 u32 touch_cap_seq_offset;
423 bool touch_cap_seq_resync;
424 u32 touch_cap_seq_start;
425 u32 touch_cap_seq_count;
426 u32 touch_cap_with_seq_wrap_count;
427 bool touch_cap_streaming;
428 struct v4l2_fract timeperframe_tch_cap;
429 struct v4l2_pix_format tch_format;
430 int tch_pat_random;
431
432 /* video output */
433 const struct vivid_fmt *fmt_out;
434 struct v4l2_fract timeperframe_vid_out;
435 enum v4l2_field field_out;
436 struct v4l2_rect sink_rect;
437 struct v4l2_rect fmt_out_rect;
438 struct v4l2_rect crop_out;
439 struct v4l2_rect compose_out;
440 struct v4l2_rect compose_bounds_out;
441 struct vb2_queue vb_vid_out_q;
442 struct list_head vid_out_active;
443 struct vb2_queue vb_vbi_out_q;
444 struct list_head vbi_out_active;
445 struct vb2_queue vb_meta_out_q;
446 struct list_head meta_out_active;
447
448 /* video loop precalculated rectangles */
449
450 /*
451 * Intersection between what the output side composes and the capture side
452 * crops. I.e., what actually needs to be copied from the output buffer to
453 * the capture buffer.
454 */
455 struct v4l2_rect loop_vid_copy;
456 /* The part of the output buffer that (after scaling) corresponds to loop_vid_copy. */
457 struct v4l2_rect loop_vid_out;
458 /* The part of the capture buffer that (after scaling) corresponds to loop_vid_copy. */
459 struct v4l2_rect loop_vid_cap;
460 /*
461 * The intersection of the framebuffer, the overlay output window and
462 * loop_vid_copy. I.e., the part of the framebuffer that actually should be
463 * blended with the compose_out rectangle. This uses the framebuffer origin.
464 */
465 struct v4l2_rect loop_fb_copy;
466 /* The same as loop_fb_copy but with compose_out origin. */
467 struct v4l2_rect loop_vid_overlay;
468 /*
469 * The part of the capture buffer that (after scaling) corresponds
470 * to loop_vid_overlay.
471 */
472 struct v4l2_rect loop_vid_overlay_cap;
473
474 /* thread for generating video output stream */
475 struct task_struct *kthread_vid_out;
476 unsigned long jiffies_vid_out;
477 u32 out_seq_offset;
478 u32 out_seq_count;
479 bool out_seq_resync;
480 u32 vid_out_seq_start;
481 u32 vid_out_seq_count;
482 bool vid_out_streaming;
483 u32 vbi_out_seq_start;
484 u32 vbi_out_seq_count;
485 bool vbi_out_streaming;
486 bool stream_sliced_vbi_out;
487 u32 meta_out_seq_start;
488 u32 meta_out_seq_count;
489 bool meta_out_streaming;
490
491 /* SDR capture */
492 struct vb2_queue vb_sdr_cap_q;
493 struct list_head sdr_cap_active;
494 u32 sdr_pixelformat; /* v4l2 format id */
495 unsigned sdr_buffersize;
496 unsigned sdr_adc_freq;
497 unsigned sdr_fm_freq;
498 unsigned sdr_fm_deviation;
499 int sdr_fixp_src_phase;
500 int sdr_fixp_mod_phase;
501
502 bool tstamp_src_is_soe;
503 bool has_crop_cap;
504 bool has_compose_cap;
505 bool has_scaler_cap;
506 bool has_crop_out;
507 bool has_compose_out;
508 bool has_scaler_out;
509
510 /* thread for generating SDR stream */
511 struct task_struct *kthread_sdr_cap;
512 unsigned long jiffies_sdr_cap;
513 u32 sdr_cap_seq_offset;
514 u32 sdr_cap_seq_start;
515 u32 sdr_cap_seq_count;
516 u32 sdr_cap_with_seq_wrap_count;
517 bool sdr_cap_seq_resync;
518
519 /* RDS generator */
520 struct vivid_rds_gen rds_gen;
521
522 /* Radio receiver */
523 unsigned radio_rx_freq;
524 unsigned radio_rx_audmode;
525 int radio_rx_sig_qual;
526 unsigned radio_rx_hw_seek_mode;
527 bool radio_rx_hw_seek_prog_lim;
528 bool radio_rx_rds_controls;
529 bool radio_rx_rds_enabled;
530 unsigned radio_rx_rds_use_alternates;
531 unsigned radio_rx_rds_last_block;
532 struct v4l2_fh *radio_rx_rds_owner;
533
534 /* Radio transmitter */
535 unsigned radio_tx_freq;
536 unsigned radio_tx_subchans;
537 bool radio_tx_rds_controls;
538 unsigned radio_tx_rds_last_block;
539 struct v4l2_fh *radio_tx_rds_owner;
540
541 /* Shared between radio receiver and transmitter */
542 bool radio_rds_loop;
543 ktime_t radio_rds_init_time;
544
545 /* CEC */
546 struct cec_adapter *cec_rx_adap;
547 struct cec_adapter *cec_tx_adap[MAX_OUTPUTS];
548 u8 cec_output2bus_map[MAX_OUTPUTS];
549 struct task_struct *kthread_cec;
550 wait_queue_head_t kthread_waitq_cec;
551 struct vivid_cec_xfer xfers[MAX_OUTPUTS];
552 spinlock_t cec_xfers_slock; /* read and write cec messages */
553 u32 cec_sft; /* bus signal free time, in bit periods */
554 u8 last_initiator;
555
556 /* CEC OSD String */
557 char osd[14];
558 unsigned long osd_jiffies;
559
560 bool meta_pts;
561 bool meta_scr;
562 };
563
vivid_is_webcam(const struct vivid_dev * dev)564 static inline bool vivid_is_webcam(const struct vivid_dev *dev)
565 {
566 return dev->input_type[dev->input] == WEBCAM;
567 }
568
vivid_is_tv_cap(const struct vivid_dev * dev)569 static inline bool vivid_is_tv_cap(const struct vivid_dev *dev)
570 {
571 return dev->input_type[dev->input] == TV;
572 }
573
vivid_is_svid_cap(const struct vivid_dev * dev)574 static inline bool vivid_is_svid_cap(const struct vivid_dev *dev)
575 {
576 return dev->input_type[dev->input] == SVID;
577 }
578
vivid_is_hdmi_cap(const struct vivid_dev * dev)579 static inline bool vivid_is_hdmi_cap(const struct vivid_dev *dev)
580 {
581 return dev->input_type[dev->input] == HDMI;
582 }
583
vivid_is_sdtv_cap(const struct vivid_dev * dev)584 static inline bool vivid_is_sdtv_cap(const struct vivid_dev *dev)
585 {
586 return vivid_is_tv_cap(dev) || vivid_is_svid_cap(dev);
587 }
588
vivid_is_svid_out(const struct vivid_dev * dev)589 static inline bool vivid_is_svid_out(const struct vivid_dev *dev)
590 {
591 return dev->output_type[dev->output] == SVID;
592 }
593
vivid_is_hdmi_out(const struct vivid_dev * dev)594 static inline bool vivid_is_hdmi_out(const struct vivid_dev *dev)
595 {
596 return dev->output_type[dev->output] == HDMI;
597 }
598
599 #endif
600