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 clip rectangles */
26 #define MAX_CLIPS 16
27 /* The maximum number of inputs */
28 #define MAX_INPUTS 16
29 /* The maximum number of outputs */
30 #define MAX_OUTPUTS 16
31 /* The maximum up or down scaling factor is 4 */
32 #define MAX_ZOOM 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 */
37 #define MIN_WIDTH 16
38 #define MIN_HEIGHT 16
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_work {
114 struct list_head list;
115 struct delayed_work work;
116 struct cec_adapter *adap;
117 struct vivid_dev *dev;
118 unsigned int usecs;
119 unsigned int timeout_ms;
120 u8 tx_status;
121 struct cec_msg msg;
122 };
123
124 struct vivid_dev {
125 unsigned inst;
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;
134 #endif
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;
156 spinlock_t slock;
157 struct mutex mutex;
158
159 /* capabilities */
160 u32 vid_cap_caps;
161 u32 vid_out_caps;
162 u32 vbi_cap_caps;
163 u32 vbi_out_caps;
164 u32 sdr_cap_caps;
165 u32 radio_rx_caps;
166 u32 radio_tx_caps;
167
168 /* supported features */
169 bool multiplanar;
170 unsigned num_inputs;
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;
180 bool has_vid_cap;
181 bool has_vid_out;
182 bool has_vbi_cap;
183 bool has_raw_vbi_cap;
184 bool has_sliced_vbi_cap;
185 bool has_vbi_out;
186 bool has_raw_vbi_out;
187 bool has_sliced_vbi_out;
188 bool has_radio_rx;
189 bool has_radio_tx;
190 bool has_sdr_cap;
191 bool has_fb;
192
193 bool can_loop_video;
194
195 /* controls */
196 struct v4l2_ctrl *brightness;
197 struct v4l2_ctrl *contrast;
198 struct v4l2_ctrl *saturation;
199 struct v4l2_ctrl *hue;
200 struct {
201 /* autogain/gain cluster */
202 struct v4l2_ctrl *autogain;
203 struct v4l2_ctrl *gain;
204 };
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;
220 struct {
221 /* std_signal_mode/standard cluster */
222 struct v4l2_ctrl *ctrl_std_signal_mode;
223 struct v4l2_ctrl *ctrl_standard;
224 };
225 struct {
226 /* dv_timings_signal_mode/timings cluster */
227 struct v4l2_ctrl *ctrl_dv_timings_signal_mode;
228 struct v4l2_ctrl *ctrl_dv_timings;
229 };
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;
242
243 struct v4l2_ctrl *ctrl_rx_power_present;
244
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;
256
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;
263
264 unsigned input_brightness[MAX_INPUTS];
265 unsigned osd_mode;
266 unsigned button_pressed;
267 bool sensor_hflip;
268 bool sensor_vflip;
269 bool hflip;
270 bool vflip;
271 bool vbi_cap_interlaced;
272 bool loop_video;
273 bool reduced_fps;
274
275 /* Framebuffer */
276 unsigned long video_pbase;
277 void *video_vbase;
278 u32 video_buffer_size;
279 int display_width;
280 int display_height;
281 int display_byte_stride;
282 int bits_per_pixel;
283 int bytes_per_pixel;
284 struct fb_info fb_info;
285 struct fb_var_screeninfo fb_defined;
286 struct fb_fix_screeninfo fb_fix;
287
288 /* Error injection */
289 bool queue_setup_error;
290 bool buf_prepare_error;
291 bool start_streaming_error;
292 bool dqbuf_error;
293 bool req_validate_error;
294 bool seq_wrap;
295 bool time_wrap;
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];
302
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];
310
311 /* Input */
312 unsigned input;
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];
316 u32 service_set_cap;
317 struct vivid_vbi_gen_data vbi_gen;
318 u8 *edid;
319 unsigned edid_blocks;
320 unsigned edid_max_blocks;
321 unsigned webcam_size_idx;
322 unsigned webcam_ival_idx;
323 unsigned tv_freq;
324 unsigned tv_audmode;
325 unsigned tv_field_cap;
326 unsigned tv_audio_input;
327
328 u32 power_present;
329
330 /* Capture Overlay */
331 struct v4l2_framebuffer fb_cap;
332 struct v4l2_fh *overlay_cap_owner;
333 void *fb_vbase_cap;
334 int overlay_cap_top, overlay_cap_left;
335 enum v4l2_field overlay_cap_field;
336 void *bitmap_cap;
337 struct v4l2_clip clips_cap[MAX_CLIPS];
338 struct v4l2_clip try_clips_cap[MAX_CLIPS];
339 unsigned clipcount_cap;
340
341 /* Output */
342 unsigned output;
343 v4l2_std_id std_out;
344 struct v4l2_dv_timings dv_timings_out;
345 u32 colorspace_out;
346 u32 ycbcr_enc_out;
347 u32 hsv_enc_out;
348 u32 quantization_out;
349 u32 xfer_func_out;
350 u32 service_set_out;
351 unsigned bytesperline_out[TPG_MAX_PLANES];
352 unsigned tv_field_out;
353 unsigned tv_audio_output;
354 bool vbi_out_have_wss;
355 u8 vbi_out_wss[2];
356 bool vbi_out_have_cc[2];
357 u8 vbi_out_cc[2][2];
358 bool dvi_d_out;
359 u8 *scaled_line;
360 u8 *blended_line;
361 unsigned cur_scaled_line;
362 bool display_present[MAX_OUTPUTS];
363
364 /* Output Overlay */
365 void *fb_vbase_out;
366 bool overlay_out_enabled;
367 int overlay_out_top, overlay_out_left;
368 void *bitmap_out;
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;
373 u32 chromakey_out;
374 u8 global_alpha_out;
375
376 /* video capture */
377 struct tpg_data tpg;
378 unsigned ms_vid_cap;
379 bool must_blank[VIDEO_MAX_FRAME];
380
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;
393
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;
400 u32 cap_seq_offset;
401 u32 cap_seq_count;
402 bool cap_seq_resync;
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;
410
411 /* video output */
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;
424
425 /* video loop precalculated rectangles */
426
427 /*
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.
431 */
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;
437 /*
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.
441 */
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;
445 /*
446 * The part of the capture buffer that (after scaling) corresponds
447 * to loop_vid_overlay.
448 */
449 struct v4l2_rect loop_vid_overlay_cap;
450
451 /* thread for generating video output stream */
452 struct task_struct *kthread_vid_out;
453 unsigned long jiffies_vid_out;
454 u32 out_seq_offset;
455 u32 out_seq_count;
456 bool out_seq_resync;
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;
464
465 /* SDR capture */
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;
475
476 bool tstamp_src_is_soe;
477 bool has_crop_cap;
478 bool has_compose_cap;
479 bool has_scaler_cap;
480 bool has_crop_out;
481 bool has_compose_out;
482 bool has_scaler_out;
483
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;
490
491 /* RDS generator */
492 struct vivid_rds_gen rds_gen;
493
494 /* Radio receiver */
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;
505
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;
512
513 /* Shared between radio receiver and transmitter */
514 bool radio_rds_loop;
515 ktime_t radio_rds_init_time;
516
517 /* CEC */
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];
526
527 /* CEC OSD String */
528 char osd[14];
529 unsigned long osd_jiffies;
530 };
531
vivid_is_webcam(const struct vivid_dev * dev)532 static inline bool vivid_is_webcam(const struct vivid_dev *dev)
533 {
534 return dev->input_type[dev->input] == WEBCAM;
535 }
536
vivid_is_tv_cap(const struct vivid_dev * dev)537 static inline bool vivid_is_tv_cap(const struct vivid_dev *dev)
538 {
539 return dev->input_type[dev->input] == TV;
540 }
541
vivid_is_svid_cap(const struct vivid_dev * dev)542 static inline bool vivid_is_svid_cap(const struct vivid_dev *dev)
543 {
544 return dev->input_type[dev->input] == SVID;
545 }
546
vivid_is_hdmi_cap(const struct vivid_dev * dev)547 static inline bool vivid_is_hdmi_cap(const struct vivid_dev *dev)
548 {
549 return dev->input_type[dev->input] == HDMI;
550 }
551
vivid_is_sdtv_cap(const struct vivid_dev * dev)552 static inline bool vivid_is_sdtv_cap(const struct vivid_dev *dev)
553 {
554 return vivid_is_tv_cap(dev) || vivid_is_svid_cap(dev);
555 }
556
vivid_is_svid_out(const struct vivid_dev * dev)557 static inline bool vivid_is_svid_out(const struct vivid_dev *dev)
558 {
559 return dev->output_type[dev->output] == SVID;
560 }
561
vivid_is_hdmi_out(const struct vivid_dev * dev)562 static inline bool vivid_is_hdmi_out(const struct vivid_dev *dev)
563 {
564 return dev->output_type[dev->output] == HDMI;
565 }
566
567 #endif
568