cx18-av-vbi.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * cx18 ADEC VBI functions
  4. *
  5. * Derived from cx25840-vbi.c
  6. *
  7. * Copyright (C) 2007 Hans Verkuil <[email protected]>
  8. */
  9. #include "cx18-driver.h"
  10. /*
  11. * For sliced VBI output, we set up to use VIP-1.1, 8-bit mode,
  12. * NN counts 1 byte Dwords, an IDID with the VBI line # in it.
  13. * Thus, according to the VIP-2 Spec, our VBI ancillary data lines
  14. * (should!) look like:
  15. * 4 byte EAV code: 0xff 0x00 0x00 0xRP
  16. * unknown number of possible idle bytes
  17. * 3 byte Anc data preamble: 0x00 0xff 0xff
  18. * 1 byte data identifier: ne010iii (parity bits, 010, DID bits)
  19. * 1 byte secondary data id: nessssss (parity bits, SDID bits)
  20. * 1 byte data word count: necccccc (parity bits, NN Dword count)
  21. * 2 byte Internal DID: VBI-line-# 0x80
  22. * NN data bytes
  23. * 1 byte checksum
  24. * Fill bytes needed to fil out to 4*NN bytes of payload
  25. *
  26. * The RP codes for EAVs when in VIP-1.1 mode, not in raw mode, &
  27. * in the vertical blanking interval are:
  28. * 0xb0 (Task 0 VerticalBlank HorizontalBlank 0 0 0 0)
  29. * 0xf0 (Task EvenField VerticalBlank HorizontalBlank 0 0 0 0)
  30. *
  31. * Since the V bit is only allowed to toggle in the EAV RP code, just
  32. * before the first active region line and for active lines, they are:
  33. * 0x90 (Task 0 0 HorizontalBlank 0 0 0 0)
  34. * 0xd0 (Task EvenField 0 HorizontalBlank 0 0 0 0)
  35. *
  36. * The user application DID bytes we care about are:
  37. * 0x91 (1 0 010 0 !ActiveLine AncDataPresent)
  38. * 0x55 (0 1 010 2ndField !ActiveLine AncDataPresent)
  39. *
  40. */
  41. static const u8 sliced_vbi_did[2] = { 0x91, 0x55 };
  42. struct vbi_anc_data {
  43. /* u8 eav[4]; */
  44. /* u8 idle[]; Variable number of idle bytes */
  45. u8 preamble[3];
  46. u8 did;
  47. u8 sdid;
  48. u8 data_count;
  49. u8 idid[2];
  50. u8 payload[1]; /* data_count of payload */
  51. /* u8 checksum; */
  52. /* u8 fill[]; Variable number of fill bytes */
  53. };
  54. static int odd_parity(u8 c)
  55. {
  56. c ^= (c >> 4);
  57. c ^= (c >> 2);
  58. c ^= (c >> 1);
  59. return c & 1;
  60. }
  61. static int decode_vps(u8 *dst, u8 *p)
  62. {
  63. static const u8 biphase_tbl[] = {
  64. 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
  65. 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
  66. 0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96,
  67. 0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2,
  68. 0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94,
  69. 0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0,
  70. 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
  71. 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
  72. 0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5,
  73. 0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1,
  74. 0xc3, 0x4b, 0x43, 0xc3, 0x87, 0x0f, 0x07, 0x87,
  75. 0x83, 0x0b, 0x03, 0x83, 0xc3, 0x4b, 0x43, 0xc3,
  76. 0xc1, 0x49, 0x41, 0xc1, 0x85, 0x0d, 0x05, 0x85,
  77. 0x81, 0x09, 0x01, 0x81, 0xc1, 0x49, 0x41, 0xc1,
  78. 0xe1, 0x69, 0x61, 0xe1, 0xa5, 0x2d, 0x25, 0xa5,
  79. 0xa1, 0x29, 0x21, 0xa1, 0xe1, 0x69, 0x61, 0xe1,
  80. 0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4,
  81. 0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0,
  82. 0xc2, 0x4a, 0x42, 0xc2, 0x86, 0x0e, 0x06, 0x86,
  83. 0x82, 0x0a, 0x02, 0x82, 0xc2, 0x4a, 0x42, 0xc2,
  84. 0xc0, 0x48, 0x40, 0xc0, 0x84, 0x0c, 0x04, 0x84,
  85. 0x80, 0x08, 0x00, 0x80, 0xc0, 0x48, 0x40, 0xc0,
  86. 0xe0, 0x68, 0x60, 0xe0, 0xa4, 0x2c, 0x24, 0xa4,
  87. 0xa0, 0x28, 0x20, 0xa0, 0xe0, 0x68, 0x60, 0xe0,
  88. 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
  89. 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
  90. 0xd2, 0x5a, 0x52, 0xd2, 0x96, 0x1e, 0x16, 0x96,
  91. 0x92, 0x1a, 0x12, 0x92, 0xd2, 0x5a, 0x52, 0xd2,
  92. 0xd0, 0x58, 0x50, 0xd0, 0x94, 0x1c, 0x14, 0x94,
  93. 0x90, 0x18, 0x10, 0x90, 0xd0, 0x58, 0x50, 0xd0,
  94. 0xf0, 0x78, 0x70, 0xf0, 0xb4, 0x3c, 0x34, 0xb4,
  95. 0xb0, 0x38, 0x30, 0xb0, 0xf0, 0x78, 0x70, 0xf0,
  96. };
  97. u8 c, err = 0;
  98. int i;
  99. for (i = 0; i < 2 * 13; i += 2) {
  100. err |= biphase_tbl[p[i]] | biphase_tbl[p[i + 1]];
  101. c = (biphase_tbl[p[i + 1]] & 0xf) |
  102. ((biphase_tbl[p[i]] & 0xf) << 4);
  103. dst[i / 2] = c;
  104. }
  105. return err & 0xf0;
  106. }
  107. int cx18_av_g_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
  108. {
  109. struct cx18 *cx = v4l2_get_subdevdata(sd);
  110. struct cx18_av_state *state = &cx->av_state;
  111. static const u16 lcr2vbi[] = {
  112. 0, V4L2_SLICED_TELETEXT_B, 0, /* 1 */
  113. 0, V4L2_SLICED_WSS_625, 0, /* 4 */
  114. V4L2_SLICED_CAPTION_525, /* 6 */
  115. 0, 0, V4L2_SLICED_VPS, 0, 0, /* 9 */
  116. 0, 0, 0, 0
  117. };
  118. int is_pal = !(state->std & V4L2_STD_525_60);
  119. int i;
  120. memset(svbi->service_lines, 0, sizeof(svbi->service_lines));
  121. svbi->service_set = 0;
  122. /* we're done if raw VBI is active */
  123. if ((cx18_av_read(cx, 0x404) & 0x10) == 0)
  124. return 0;
  125. if (is_pal) {
  126. for (i = 7; i <= 23; i++) {
  127. u8 v = cx18_av_read(cx, 0x424 + i - 7);
  128. svbi->service_lines[0][i] = lcr2vbi[v >> 4];
  129. svbi->service_lines[1][i] = lcr2vbi[v & 0xf];
  130. svbi->service_set |= svbi->service_lines[0][i] |
  131. svbi->service_lines[1][i];
  132. }
  133. } else {
  134. for (i = 10; i <= 21; i++) {
  135. u8 v = cx18_av_read(cx, 0x424 + i - 10);
  136. svbi->service_lines[0][i] = lcr2vbi[v >> 4];
  137. svbi->service_lines[1][i] = lcr2vbi[v & 0xf];
  138. svbi->service_set |= svbi->service_lines[0][i] |
  139. svbi->service_lines[1][i];
  140. }
  141. }
  142. return 0;
  143. }
  144. int cx18_av_s_raw_fmt(struct v4l2_subdev *sd, struct v4l2_vbi_format *fmt)
  145. {
  146. struct cx18 *cx = v4l2_get_subdevdata(sd);
  147. struct cx18_av_state *state = &cx->av_state;
  148. /* Setup standard */
  149. cx18_av_std_setup(cx);
  150. /* VBI Offset */
  151. cx18_av_write(cx, 0x47f, state->slicer_line_delay);
  152. cx18_av_write(cx, 0x404, 0x2e);
  153. return 0;
  154. }
  155. int cx18_av_s_sliced_fmt(struct v4l2_subdev *sd, struct v4l2_sliced_vbi_format *svbi)
  156. {
  157. struct cx18 *cx = v4l2_get_subdevdata(sd);
  158. struct cx18_av_state *state = &cx->av_state;
  159. int is_pal = !(state->std & V4L2_STD_525_60);
  160. int i, x;
  161. u8 lcr[24];
  162. for (x = 0; x <= 23; x++)
  163. lcr[x] = 0x00;
  164. /* Setup standard */
  165. cx18_av_std_setup(cx);
  166. /* Sliced VBI */
  167. cx18_av_write(cx, 0x404, 0x32); /* Ancillary data */
  168. cx18_av_write(cx, 0x406, 0x13);
  169. cx18_av_write(cx, 0x47f, state->slicer_line_delay);
  170. /* Force impossible lines to 0 */
  171. if (is_pal) {
  172. for (i = 0; i <= 6; i++)
  173. svbi->service_lines[0][i] =
  174. svbi->service_lines[1][i] = 0;
  175. } else {
  176. for (i = 0; i <= 9; i++)
  177. svbi->service_lines[0][i] =
  178. svbi->service_lines[1][i] = 0;
  179. for (i = 22; i <= 23; i++)
  180. svbi->service_lines[0][i] =
  181. svbi->service_lines[1][i] = 0;
  182. }
  183. /* Build register values for requested service lines */
  184. for (i = 7; i <= 23; i++) {
  185. for (x = 0; x <= 1; x++) {
  186. switch (svbi->service_lines[1-x][i]) {
  187. case V4L2_SLICED_TELETEXT_B:
  188. lcr[i] |= 1 << (4 * x);
  189. break;
  190. case V4L2_SLICED_WSS_625:
  191. lcr[i] |= 4 << (4 * x);
  192. break;
  193. case V4L2_SLICED_CAPTION_525:
  194. lcr[i] |= 6 << (4 * x);
  195. break;
  196. case V4L2_SLICED_VPS:
  197. lcr[i] |= 9 << (4 * x);
  198. break;
  199. }
  200. }
  201. }
  202. if (is_pal) {
  203. for (x = 1, i = 0x424; i <= 0x434; i++, x++)
  204. cx18_av_write(cx, i, lcr[6 + x]);
  205. } else {
  206. for (x = 1, i = 0x424; i <= 0x430; i++, x++)
  207. cx18_av_write(cx, i, lcr[9 + x]);
  208. for (i = 0x431; i <= 0x434; i++)
  209. cx18_av_write(cx, i, 0);
  210. }
  211. cx18_av_write(cx, 0x43c, 0x16);
  212. /* Should match vblank set in cx18_av_std_setup() */
  213. cx18_av_write(cx, 0x474, is_pal ? 38 : 26);
  214. return 0;
  215. }
  216. int cx18_av_decode_vbi_line(struct v4l2_subdev *sd,
  217. struct v4l2_decode_vbi_line *vbi)
  218. {
  219. struct cx18 *cx = v4l2_get_subdevdata(sd);
  220. struct cx18_av_state *state = &cx->av_state;
  221. struct vbi_anc_data *anc = (struct vbi_anc_data *)vbi->p;
  222. u8 *p;
  223. int did, sdid, l, err = 0;
  224. /*
  225. * Check for the ancillary data header for sliced VBI
  226. */
  227. if (anc->preamble[0] ||
  228. anc->preamble[1] != 0xff || anc->preamble[2] != 0xff ||
  229. (anc->did != sliced_vbi_did[0] &&
  230. anc->did != sliced_vbi_did[1])) {
  231. vbi->line = vbi->type = 0;
  232. return 0;
  233. }
  234. did = anc->did;
  235. sdid = anc->sdid & 0xf;
  236. l = anc->idid[0] & 0x3f;
  237. l += state->slicer_line_offset;
  238. p = anc->payload;
  239. /* Decode the SDID set by the slicer */
  240. switch (sdid) {
  241. case 1:
  242. sdid = V4L2_SLICED_TELETEXT_B;
  243. break;
  244. case 4:
  245. sdid = V4L2_SLICED_WSS_625;
  246. break;
  247. case 6:
  248. sdid = V4L2_SLICED_CAPTION_525;
  249. err = !odd_parity(p[0]) || !odd_parity(p[1]);
  250. break;
  251. case 9:
  252. sdid = V4L2_SLICED_VPS;
  253. if (decode_vps(p, p) != 0)
  254. err = 1;
  255. break;
  256. default:
  257. sdid = 0;
  258. err = 1;
  259. break;
  260. }
  261. vbi->type = err ? 0 : sdid;
  262. vbi->line = err ? 0 : l;
  263. vbi->is_second_field = err ? 0 : (did == sliced_vbi_did[1]);
  264. vbi->p = p;
  265. return 0;
  266. }