1 /*
2 * vivid-core.h - core datastructures
3 *
4 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20 #ifndef _VIVID_CORE_H_
21 #define _VIVID_CORE_H_
22
23 #include <linux/fb.h>
24 #include <media/videobuf2-core.h>
25 #include <media/v4l2-device.h>
26 #include <media/v4l2-dev.h>
27 #include <media/v4l2-ctrls.h>
28 #include "vivid-tpg.h"
29 #include "vivid-rds-gen.h"
30 #include "vivid-vbi-gen.h"
31
32 #define dprintk(dev, level, fmt, arg...) \
33 v4l2_dbg(level, vivid_debug, &dev->v4l2_dev, fmt, ## arg)
34
35 /* Maximum allowed frame rate
36 *
37 * vivid will allow setting timeperframe in [1/FPS_MAX - FPS_MAX/1] range.
38 *
39 * Ideally FPS_MAX should be infinity, i.e. practically UINT_MAX, but that
40 * might hit application errors when they manipulate these values.
41 *
42 * Besides, for tpf < 10ms image-generation logic should be changed, to avoid
43 * producing frames with equal content.
44 */
45 #define FPS_MAX 100
46
47 /* The maximum number of clip rectangles */
48 #define MAX_CLIPS 16
49 /* The maximum number of inputs */
50 #define MAX_INPUTS 16
51 /* The maximum number of outputs */
52 #define MAX_OUTPUTS 16
53 /* The maximum up or down scaling factor is 4 */
54 #define MAX_ZOOM 4
55 /* The maximum image width/height are set to 4K DMT */
56 #define MAX_WIDTH 4096
57 #define MAX_HEIGHT 2160
58 /* The minimum image width/height */
59 #define MIN_WIDTH 16
60 #define MIN_HEIGHT 16
61 /* The data_offset of plane 0 for the multiplanar formats */
62 #define PLANE0_DATA_OFFSET 128
63
64 /* The supported TV frequency range in MHz */
65 #define MIN_TV_FREQ (44U * 16U)
66 #define MAX_TV_FREQ (958U * 16U)
67
68 /* The number of samples returned in every SDR buffer */
69 #define SDR_CAP_SAMPLES_PER_BUF 0x4000
70
71 /* used by the threads to know when to resync internal counters */
72 #define JIFFIES_PER_DAY (3600U * 24U * HZ)
73 #define JIFFIES_RESYNC (JIFFIES_PER_DAY * (0xf0000000U / JIFFIES_PER_DAY))
74
75 extern const struct v4l2_rect vivid_min_rect;
76 extern const struct v4l2_rect vivid_max_rect;
77 extern unsigned vivid_debug;
78
79 struct vivid_fmt {
80 const char *name;
81 u32 fourcc; /* v4l2 format id */
82 u8 depth;
83 bool is_yuv;
84 bool can_do_overlay;
85 u32 alpha_mask;
86 u8 planes;
87 u32 data_offset[2];
88 };
89
90 extern struct vivid_fmt vivid_formats[];
91
92 /* buffer for one video frame */
93 struct vivid_buffer {
94 /* common v4l buffer stuff -- must be first */
95 struct vb2_buffer vb;
96 struct list_head list;
97 };
98
99 enum vivid_input {
100 WEBCAM,
101 TV,
102 SVID,
103 HDMI,
104 };
105
106 enum vivid_signal_mode {
107 CURRENT_DV_TIMINGS,
108 CURRENT_STD = CURRENT_DV_TIMINGS,
109 NO_SIGNAL,
110 NO_LOCK,
111 OUT_OF_RANGE,
112 SELECTED_DV_TIMINGS,
113 SELECTED_STD = SELECTED_DV_TIMINGS,
114 CYCLE_DV_TIMINGS,
115 CYCLE_STD = CYCLE_DV_TIMINGS,
116 CUSTOM_DV_TIMINGS,
117 };
118
119 #define VIVID_INVALID_SIGNAL(mode) \
120 ((mode) == NO_SIGNAL || (mode) == NO_LOCK || (mode) == OUT_OF_RANGE)
121
122 struct vivid_dev {
123 unsigned inst;
124 struct v4l2_device v4l2_dev;
125 struct v4l2_ctrl_handler ctrl_hdl_user_gen;
126 struct v4l2_ctrl_handler ctrl_hdl_user_vid;
127 struct v4l2_ctrl_handler ctrl_hdl_user_aud;
128 struct v4l2_ctrl_handler ctrl_hdl_streaming;
129 struct v4l2_ctrl_handler ctrl_hdl_sdtv_cap;
130 struct v4l2_ctrl_handler ctrl_hdl_loop_out;
131 struct video_device vid_cap_dev;
132 struct v4l2_ctrl_handler ctrl_hdl_vid_cap;
133 struct video_device vid_out_dev;
134 struct v4l2_ctrl_handler ctrl_hdl_vid_out;
135 struct video_device vbi_cap_dev;
136 struct v4l2_ctrl_handler ctrl_hdl_vbi_cap;
137 struct video_device vbi_out_dev;
138 struct v4l2_ctrl_handler ctrl_hdl_vbi_out;
139 struct video_device radio_rx_dev;
140 struct v4l2_ctrl_handler ctrl_hdl_radio_rx;
141 struct video_device radio_tx_dev;
142 struct v4l2_ctrl_handler ctrl_hdl_radio_tx;
143 struct video_device sdr_cap_dev;
144 struct v4l2_ctrl_handler ctrl_hdl_sdr_cap;
145 spinlock_t slock;
146 struct mutex mutex;
147
148 /* capabilities */
149 u32 vid_cap_caps;
150 u32 vid_out_caps;
151 u32 vbi_cap_caps;
152 u32 vbi_out_caps;
153 u32 sdr_cap_caps;
154 u32 radio_rx_caps;
155 u32 radio_tx_caps;
156
157 /* supported features */
158 bool multiplanar;
159 unsigned num_inputs;
160 u8 input_type[MAX_INPUTS];
161 u8 input_name_counter[MAX_INPUTS];
162 unsigned num_outputs;
163 u8 output_type[MAX_OUTPUTS];
164 u8 output_name_counter[MAX_OUTPUTS];
165 bool has_audio_inputs;
166 bool has_audio_outputs;
167 bool has_vid_cap;
168 bool has_vid_out;
169 bool has_vbi_cap;
170 bool has_raw_vbi_cap;
171 bool has_sliced_vbi_cap;
172 bool has_vbi_out;
173 bool has_raw_vbi_out;
174 bool has_sliced_vbi_out;
175 bool has_radio_rx;
176 bool has_radio_tx;
177 bool has_sdr_cap;
178 bool has_fb;
179
180 bool can_loop_video;
181
182 /* controls */
183 struct v4l2_ctrl *brightness;
184 struct v4l2_ctrl *contrast;
185 struct v4l2_ctrl *saturation;
186 struct v4l2_ctrl *hue;
187 struct {
188 /* autogain/gain cluster */
189 struct v4l2_ctrl *autogain;
190 struct v4l2_ctrl *gain;
191 };
192 struct v4l2_ctrl *volume;
193 struct v4l2_ctrl *mute;
194 struct v4l2_ctrl *alpha;
195 struct v4l2_ctrl *button;
196 struct v4l2_ctrl *boolean;
197 struct v4l2_ctrl *int32;
198 struct v4l2_ctrl *int64;
199 struct v4l2_ctrl *menu;
200 struct v4l2_ctrl *string;
201 struct v4l2_ctrl *bitmask;
202 struct v4l2_ctrl *int_menu;
203 struct v4l2_ctrl *test_pattern;
204 struct v4l2_ctrl *colorspace;
205 struct v4l2_ctrl *rgb_range_cap;
206 struct v4l2_ctrl *real_rgb_range_cap;
207 struct {
208 /* std_signal_mode/standard cluster */
209 struct v4l2_ctrl *ctrl_std_signal_mode;
210 struct v4l2_ctrl *ctrl_standard;
211 };
212 struct {
213 /* dv_timings_signal_mode/timings cluster */
214 struct v4l2_ctrl *ctrl_dv_timings_signal_mode;
215 struct v4l2_ctrl *ctrl_dv_timings;
216 };
217 struct v4l2_ctrl *ctrl_has_crop_cap;
218 struct v4l2_ctrl *ctrl_has_compose_cap;
219 struct v4l2_ctrl *ctrl_has_scaler_cap;
220 struct v4l2_ctrl *ctrl_has_crop_out;
221 struct v4l2_ctrl *ctrl_has_compose_out;
222 struct v4l2_ctrl *ctrl_has_scaler_out;
223 struct v4l2_ctrl *ctrl_tx_mode;
224 struct v4l2_ctrl *ctrl_tx_rgb_range;
225
226 struct v4l2_ctrl *radio_tx_rds_pi;
227 struct v4l2_ctrl *radio_tx_rds_pty;
228 struct v4l2_ctrl *radio_tx_rds_mono_stereo;
229 struct v4l2_ctrl *radio_tx_rds_art_head;
230 struct v4l2_ctrl *radio_tx_rds_compressed;
231 struct v4l2_ctrl *radio_tx_rds_dyn_pty;
232 struct v4l2_ctrl *radio_tx_rds_ta;
233 struct v4l2_ctrl *radio_tx_rds_tp;
234 struct v4l2_ctrl *radio_tx_rds_ms;
235 struct v4l2_ctrl *radio_tx_rds_psname;
236 struct v4l2_ctrl *radio_tx_rds_radiotext;
237
238 struct v4l2_ctrl *radio_rx_rds_pty;
239 struct v4l2_ctrl *radio_rx_rds_ta;
240 struct v4l2_ctrl *radio_rx_rds_tp;
241 struct v4l2_ctrl *radio_rx_rds_ms;
242 struct v4l2_ctrl *radio_rx_rds_psname;
243 struct v4l2_ctrl *radio_rx_rds_radiotext;
244
245 unsigned input_brightness[MAX_INPUTS];
246 unsigned osd_mode;
247 unsigned button_pressed;
248 bool sensor_hflip;
249 bool sensor_vflip;
250 bool hflip;
251 bool vflip;
252 bool vbi_cap_interlaced;
253 bool loop_video;
254
255 /* Framebuffer */
256 unsigned long video_pbase;
257 void *video_vbase;
258 u32 video_buffer_size;
259 int display_width;
260 int display_height;
261 int display_byte_stride;
262 int bits_per_pixel;
263 int bytes_per_pixel;
264 struct fb_info fb_info;
265 struct fb_var_screeninfo fb_defined;
266 struct fb_fix_screeninfo fb_fix;
267
268 /* Error injection */
269 bool queue_setup_error;
270 bool buf_prepare_error;
271 bool start_streaming_error;
272 bool dqbuf_error;
273 bool seq_wrap;
274 bool time_wrap;
275 __kernel_time_t time_wrap_offset;
276 unsigned perc_dropped_buffers;
277 enum vivid_signal_mode std_signal_mode;
278 unsigned query_std_last;
279 v4l2_std_id query_std;
280 enum tpg_video_aspect std_aspect_ratio;
281
282 enum vivid_signal_mode dv_timings_signal_mode;
283 char **query_dv_timings_qmenu;
284 unsigned query_dv_timings_size;
285 unsigned query_dv_timings_last;
286 unsigned query_dv_timings;
287 enum tpg_video_aspect dv_timings_aspect_ratio;
288
289 /* Input */
290 unsigned input;
291 v4l2_std_id std_cap;
292 struct v4l2_dv_timings dv_timings_cap;
293 u32 service_set_cap;
294 struct vivid_vbi_gen_data vbi_gen;
295 u8 *edid;
296 unsigned edid_blocks;
297 unsigned edid_max_blocks;
298 unsigned webcam_size_idx;
299 unsigned webcam_ival_idx;
300 unsigned tv_freq;
301 unsigned tv_audmode;
302 unsigned tv_field_cap;
303 unsigned tv_audio_input;
304
305 /* Capture Overlay */
306 struct v4l2_framebuffer fb_cap;
307 struct v4l2_fh *overlay_cap_owner;
308 void *fb_vbase_cap;
309 int overlay_cap_top, overlay_cap_left;
310 enum v4l2_field overlay_cap_field;
311 void *bitmap_cap;
312 struct v4l2_clip clips_cap[MAX_CLIPS];
313 struct v4l2_clip try_clips_cap[MAX_CLIPS];
314 unsigned clipcount_cap;
315
316 /* Output */
317 unsigned output;
318 v4l2_std_id std_out;
319 struct v4l2_dv_timings dv_timings_out;
320 u32 colorspace_out;
321 u32 service_set_out;
322 u32 bytesperline_out[2];
323 unsigned tv_field_out;
324 unsigned tv_audio_output;
325 bool vbi_out_have_wss;
326 u8 vbi_out_wss[2];
327 bool vbi_out_have_cc[2];
328 u8 vbi_out_cc[2][2];
329 bool dvi_d_out;
330 u8 *scaled_line;
331 u8 *blended_line;
332 unsigned cur_scaled_line;
333
334 /* Output Overlay */
335 void *fb_vbase_out;
336 bool overlay_out_enabled;
337 int overlay_out_top, overlay_out_left;
338 void *bitmap_out;
339 struct v4l2_clip clips_out[MAX_CLIPS];
340 struct v4l2_clip try_clips_out[MAX_CLIPS];
341 unsigned clipcount_out;
342 unsigned fbuf_out_flags;
343 u32 chromakey_out;
344 u8 global_alpha_out;
345
346 /* video capture */
347 struct tpg_data tpg;
348 unsigned ms_vid_cap;
349 bool must_blank[VIDEO_MAX_FRAME];
350
351 const struct vivid_fmt *fmt_cap;
352 struct v4l2_fract timeperframe_vid_cap;
353 enum v4l2_field field_cap;
354 struct v4l2_rect src_rect;
355 struct v4l2_rect fmt_cap_rect;
356 struct v4l2_rect crop_cap;
357 struct v4l2_rect compose_cap;
358 struct v4l2_rect crop_bounds_cap;
359 struct vb2_queue vb_vid_cap_q;
360 struct list_head vid_cap_active;
361 struct vb2_queue vb_vbi_cap_q;
362 struct list_head vbi_cap_active;
363
364 /* thread for generating video capture stream */
365 struct task_struct *kthread_vid_cap;
366 unsigned long jiffies_vid_cap;
367 u32 cap_seq_offset;
368 u32 cap_seq_count;
369 bool cap_seq_resync;
370 u32 vid_cap_seq_start;
371 u32 vid_cap_seq_count;
372 bool vid_cap_streaming;
373 u32 vbi_cap_seq_start;
374 u32 vbi_cap_seq_count;
375 bool vbi_cap_streaming;
376 bool stream_sliced_vbi_cap;
377
378 /* video output */
379 const struct vivid_fmt *fmt_out;
380 struct v4l2_fract timeperframe_vid_out;
381 enum v4l2_field field_out;
382 struct v4l2_rect sink_rect;
383 struct v4l2_rect fmt_out_rect;
384 struct v4l2_rect crop_out;
385 struct v4l2_rect compose_out;
386 struct v4l2_rect compose_bounds_out;
387 struct vb2_queue vb_vid_out_q;
388 struct list_head vid_out_active;
389 struct vb2_queue vb_vbi_out_q;
390 struct list_head vbi_out_active;
391
392 /* video loop precalculated rectangles */
393
394 /*
395 * Intersection between what the output side composes and the capture side
396 * crops. I.e., what actually needs to be copied from the output buffer to
397 * the capture buffer.
398 */
399 struct v4l2_rect loop_vid_copy;
400 /* The part of the output buffer that (after scaling) corresponds to loop_vid_copy. */
401 struct v4l2_rect loop_vid_out;
402 /* The part of the capture buffer that (after scaling) corresponds to loop_vid_copy. */
403 struct v4l2_rect loop_vid_cap;
404 /*
405 * The intersection of the framebuffer, the overlay output window and
406 * loop_vid_copy. I.e., the part of the framebuffer that actually should be
407 * blended with the compose_out rectangle. This uses the framebuffer origin.
408 */
409 struct v4l2_rect loop_fb_copy;
410 /* The same as loop_fb_copy but with compose_out origin. */
411 struct v4l2_rect loop_vid_overlay;
412 /*
413 * The part of the capture buffer that (after scaling) corresponds
414 * to loop_vid_overlay.
415 */
416 struct v4l2_rect loop_vid_overlay_cap;
417
418 /* thread for generating video output stream */
419 struct task_struct *kthread_vid_out;
420 unsigned long jiffies_vid_out;
421 u32 out_seq_offset;
422 u32 out_seq_count;
423 bool out_seq_resync;
424 u32 vid_out_seq_start;
425 u32 vid_out_seq_count;
426 bool vid_out_streaming;
427 u32 vbi_out_seq_start;
428 u32 vbi_out_seq_count;
429 bool vbi_out_streaming;
430 bool stream_sliced_vbi_out;
431
432 /* SDR capture */
433 struct vb2_queue vb_sdr_cap_q;
434 struct list_head sdr_cap_active;
435 unsigned sdr_adc_freq;
436 unsigned sdr_fm_freq;
437 int sdr_fixp_src_phase;
438 int sdr_fixp_mod_phase;
439
440 bool tstamp_src_is_soe;
441 bool has_crop_cap;
442 bool has_compose_cap;
443 bool has_scaler_cap;
444 bool has_crop_out;
445 bool has_compose_out;
446 bool has_scaler_out;
447
448 /* thread for generating SDR stream */
449 struct task_struct *kthread_sdr_cap;
450 unsigned long jiffies_sdr_cap;
451 u32 sdr_cap_seq_offset;
452 u32 sdr_cap_seq_count;
453 bool sdr_cap_seq_resync;
454
455 /* RDS generator */
456 struct vivid_rds_gen rds_gen;
457
458 /* Radio receiver */
459 unsigned radio_rx_freq;
460 unsigned radio_rx_audmode;
461 int radio_rx_sig_qual;
462 unsigned radio_rx_hw_seek_mode;
463 bool radio_rx_hw_seek_prog_lim;
464 bool radio_rx_rds_controls;
465 bool radio_rx_rds_enabled;
466 unsigned radio_rx_rds_use_alternates;
467 unsigned radio_rx_rds_last_block;
468 struct v4l2_fh *radio_rx_rds_owner;
469
470 /* Radio transmitter */
471 unsigned radio_tx_freq;
472 unsigned radio_tx_subchans;
473 bool radio_tx_rds_controls;
474 unsigned radio_tx_rds_last_block;
475 struct v4l2_fh *radio_tx_rds_owner;
476
477 /* Shared between radio receiver and transmitter */
478 bool radio_rds_loop;
479 struct timespec radio_rds_init_ts;
480 };
481
vivid_is_webcam(const struct vivid_dev * dev)482 static inline bool vivid_is_webcam(const struct vivid_dev *dev)
483 {
484 return dev->input_type[dev->input] == WEBCAM;
485 }
486
vivid_is_tv_cap(const struct vivid_dev * dev)487 static inline bool vivid_is_tv_cap(const struct vivid_dev *dev)
488 {
489 return dev->input_type[dev->input] == TV;
490 }
491
vivid_is_svid_cap(const struct vivid_dev * dev)492 static inline bool vivid_is_svid_cap(const struct vivid_dev *dev)
493 {
494 return dev->input_type[dev->input] == SVID;
495 }
496
vivid_is_hdmi_cap(const struct vivid_dev * dev)497 static inline bool vivid_is_hdmi_cap(const struct vivid_dev *dev)
498 {
499 return dev->input_type[dev->input] == HDMI;
500 }
501
vivid_is_sdtv_cap(const struct vivid_dev * dev)502 static inline bool vivid_is_sdtv_cap(const struct vivid_dev *dev)
503 {
504 return vivid_is_tv_cap(dev) || vivid_is_svid_cap(dev);
505 }
506
vivid_is_svid_out(const struct vivid_dev * dev)507 static inline bool vivid_is_svid_out(const struct vivid_dev *dev)
508 {
509 return dev->output_type[dev->output] == SVID;
510 }
511
vivid_is_hdmi_out(const struct vivid_dev * dev)512 static inline bool vivid_is_hdmi_out(const struct vivid_dev *dev)
513 {
514 return dev->output_type[dev->output] == HDMI;
515 }
516
517 void vivid_lock(struct vb2_queue *vq);
518 void vivid_unlock(struct vb2_queue *vq);
519
520 #endif
521