vivid-core.h 15 KB

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