ivtv-yuv.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  1. /*
  2. yuv support
  3. Copyright (C) 2007 Ian Armstrong <ian@iarmst.demon.co.uk>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #include "ivtv-driver.h"
  17. #include "ivtv-udma.h"
  18. #include "ivtv-yuv.h"
  19. /* YUV buffer offsets */
  20. const u32 yuv_offset[IVTV_YUV_BUFFERS] = {
  21. 0x001a8600,
  22. 0x00240400,
  23. 0x002d8200,
  24. 0x00370000,
  25. 0x00029000,
  26. 0x000C0E00,
  27. 0x006B0400,
  28. 0x00748200
  29. };
  30. static int ivtv_yuv_prep_user_dma(struct ivtv *itv, struct ivtv_user_dma *dma,
  31. struct ivtv_dma_frame *args)
  32. {
  33. struct ivtv_dma_page_info y_dma;
  34. struct ivtv_dma_page_info uv_dma;
  35. struct yuv_playback_info *yi = &itv->yuv_info;
  36. u8 frame = yi->draw_frame;
  37. struct yuv_frame_info *f = &yi->new_frame_info[frame];
  38. int i;
  39. int y_pages, uv_pages;
  40. unsigned long y_buffer_offset, uv_buffer_offset;
  41. int y_decode_height, uv_decode_height, y_size;
  42. y_buffer_offset = IVTV_DECODER_OFFSET + yuv_offset[frame];
  43. uv_buffer_offset = y_buffer_offset + IVTV_YUV_BUFFER_UV_OFFSET;
  44. y_decode_height = uv_decode_height = f->src_h + f->src_y;
  45. if (f->offset_y)
  46. y_buffer_offset += 720 * 16;
  47. if (y_decode_height & 15)
  48. y_decode_height = (y_decode_height + 16) & ~15;
  49. if (uv_decode_height & 31)
  50. uv_decode_height = (uv_decode_height + 32) & ~31;
  51. y_size = 720 * y_decode_height;
  52. /* Still in USE */
  53. if (dma->SG_length || dma->page_count) {
  54. IVTV_DEBUG_WARN
  55. ("prep_user_dma: SG_length %d page_count %d still full?\n",
  56. dma->SG_length, dma->page_count);
  57. return -EBUSY;
  58. }
  59. ivtv_udma_get_page_info (&y_dma, (unsigned long)args->y_source, 720 * y_decode_height);
  60. ivtv_udma_get_page_info (&uv_dma, (unsigned long)args->uv_source, 360 * uv_decode_height);
  61. /* Get user pages for DMA Xfer */
  62. y_pages = get_user_pages_unlocked(y_dma.uaddr,
  63. y_dma.page_count, &dma->map[0], FOLL_FORCE);
  64. uv_pages = 0; /* silence gcc. value is set and consumed only if: */
  65. if (y_pages == y_dma.page_count) {
  66. uv_pages = get_user_pages_unlocked(uv_dma.uaddr,
  67. uv_dma.page_count, &dma->map[y_pages],
  68. FOLL_FORCE);
  69. }
  70. if (y_pages != y_dma.page_count || uv_pages != uv_dma.page_count) {
  71. int rc = -EFAULT;
  72. if (y_pages == y_dma.page_count) {
  73. IVTV_DEBUG_WARN
  74. ("failed to map uv user pages, returned %d expecting %d\n",
  75. uv_pages, uv_dma.page_count);
  76. if (uv_pages >= 0) {
  77. for (i = 0; i < uv_pages; i++)
  78. put_page(dma->map[y_pages + i]);
  79. rc = -EFAULT;
  80. } else {
  81. rc = uv_pages;
  82. }
  83. } else {
  84. IVTV_DEBUG_WARN
  85. ("failed to map y user pages, returned %d expecting %d\n",
  86. y_pages, y_dma.page_count);
  87. }
  88. if (y_pages >= 0) {
  89. for (i = 0; i < y_pages; i++)
  90. put_page(dma->map[i]);
  91. /*
  92. * Inherit the -EFAULT from rc's
  93. * initialization, but allow it to be
  94. * overriden by uv_pages above if it was an
  95. * actual errno.
  96. */
  97. } else {
  98. rc = y_pages;
  99. }
  100. return rc;
  101. }
  102. dma->page_count = y_pages + uv_pages;
  103. /* Fill & map SG List */
  104. if (ivtv_udma_fill_sg_list (dma, &uv_dma, ivtv_udma_fill_sg_list (dma, &y_dma, 0)) < 0) {
  105. IVTV_DEBUG_WARN("could not allocate bounce buffers for highmem userspace buffers\n");
  106. for (i = 0; i < dma->page_count; i++) {
  107. put_page(dma->map[i]);
  108. }
  109. dma->page_count = 0;
  110. return -ENOMEM;
  111. }
  112. dma->SG_length = pci_map_sg(itv->pdev, dma->SGlist, dma->page_count, PCI_DMA_TODEVICE);
  113. /* Fill SG Array with new values */
  114. ivtv_udma_fill_sg_array(dma, y_buffer_offset, uv_buffer_offset, y_size);
  115. /* If we've offset the y plane, ensure top area is blanked */
  116. if (f->offset_y && yi->blanking_dmaptr) {
  117. dma->SGarray[dma->SG_length].size = cpu_to_le32(720*16);
  118. dma->SGarray[dma->SG_length].src = cpu_to_le32(yi->blanking_dmaptr);
  119. dma->SGarray[dma->SG_length].dst = cpu_to_le32(IVTV_DECODER_OFFSET + yuv_offset[frame]);
  120. dma->SG_length++;
  121. }
  122. /* Tag SG Array with Interrupt Bit */
  123. dma->SGarray[dma->SG_length - 1].size |= cpu_to_le32(0x80000000);
  124. ivtv_udma_sync_for_device(itv);
  125. return 0;
  126. }
  127. /* We rely on a table held in the firmware - Quick check. */
  128. int ivtv_yuv_filter_check(struct ivtv *itv)
  129. {
  130. int i, y, uv;
  131. for (i = 0, y = 16, uv = 4; i < 16; i++, y += 24, uv += 12) {
  132. if ((read_dec(IVTV_YUV_HORIZONTAL_FILTER_OFFSET + y) != i << 16) ||
  133. (read_dec(IVTV_YUV_VERTICAL_FILTER_OFFSET + uv) != i << 16)) {
  134. IVTV_WARN ("YUV filter table not found in firmware.\n");
  135. return -1;
  136. }
  137. }
  138. return 0;
  139. }
  140. static void ivtv_yuv_filter(struct ivtv *itv, int h_filter, int v_filter_1, int v_filter_2)
  141. {
  142. u32 i, line;
  143. /* If any filter is -1, then don't update it */
  144. if (h_filter > -1) {
  145. if (h_filter > 4)
  146. h_filter = 4;
  147. i = IVTV_YUV_HORIZONTAL_FILTER_OFFSET + (h_filter * 384);
  148. for (line = 0; line < 16; line++) {
  149. write_reg(read_dec(i), 0x02804);
  150. write_reg(read_dec(i), 0x0281c);
  151. i += 4;
  152. write_reg(read_dec(i), 0x02808);
  153. write_reg(read_dec(i), 0x02820);
  154. i += 4;
  155. write_reg(read_dec(i), 0x0280c);
  156. write_reg(read_dec(i), 0x02824);
  157. i += 4;
  158. write_reg(read_dec(i), 0x02810);
  159. write_reg(read_dec(i), 0x02828);
  160. i += 4;
  161. write_reg(read_dec(i), 0x02814);
  162. write_reg(read_dec(i), 0x0282c);
  163. i += 8;
  164. write_reg(0, 0x02818);
  165. write_reg(0, 0x02830);
  166. }
  167. IVTV_DEBUG_YUV("h_filter -> %d\n", h_filter);
  168. }
  169. if (v_filter_1 > -1) {
  170. if (v_filter_1 > 4)
  171. v_filter_1 = 4;
  172. i = IVTV_YUV_VERTICAL_FILTER_OFFSET + (v_filter_1 * 192);
  173. for (line = 0; line < 16; line++) {
  174. write_reg(read_dec(i), 0x02900);
  175. i += 4;
  176. write_reg(read_dec(i), 0x02904);
  177. i += 8;
  178. write_reg(0, 0x02908);
  179. }
  180. IVTV_DEBUG_YUV("v_filter_1 -> %d\n", v_filter_1);
  181. }
  182. if (v_filter_2 > -1) {
  183. if (v_filter_2 > 4)
  184. v_filter_2 = 4;
  185. i = IVTV_YUV_VERTICAL_FILTER_OFFSET + (v_filter_2 * 192);
  186. for (line = 0; line < 16; line++) {
  187. write_reg(read_dec(i), 0x0290c);
  188. i += 4;
  189. write_reg(read_dec(i), 0x02910);
  190. i += 8;
  191. write_reg(0, 0x02914);
  192. }
  193. IVTV_DEBUG_YUV("v_filter_2 -> %d\n", v_filter_2);
  194. }
  195. }
  196. static void ivtv_yuv_handle_horizontal(struct ivtv *itv, struct yuv_frame_info *f)
  197. {
  198. struct yuv_playback_info *yi = &itv->yuv_info;
  199. u32 reg_2834, reg_2838, reg_283c;
  200. u32 reg_2844, reg_2854, reg_285c;
  201. u32 reg_2864, reg_2874, reg_2890;
  202. u32 reg_2870, reg_2870_base, reg_2870_offset;
  203. int x_cutoff;
  204. int h_filter;
  205. u32 master_width;
  206. IVTV_DEBUG_WARN
  207. ("Adjust to width %d src_w %d dst_w %d src_x %d dst_x %d\n",
  208. f->tru_w, f->src_w, f->dst_w, f->src_x, f->dst_x);
  209. /* How wide is the src image */
  210. x_cutoff = f->src_w + f->src_x;
  211. /* Set the display width */
  212. reg_2834 = f->dst_w;
  213. reg_2838 = reg_2834;
  214. /* Set the display position */
  215. reg_2890 = f->dst_x;
  216. /* Index into the image horizontally */
  217. reg_2870 = 0;
  218. /* 2870 is normally fudged to align video coords with osd coords.
  219. If running full screen, it causes an unwanted left shift
  220. Remove the fudge if we almost fill the screen.
  221. Gradually adjust the offset to avoid the video 'snapping'
  222. left/right if it gets dragged through this region.
  223. Only do this if osd is full width. */
  224. if (f->vis_w == 720) {
  225. if ((f->tru_x - f->pan_x > -1) && (f->tru_x - f->pan_x <= 40) && (f->dst_w >= 680))
  226. reg_2870 = 10 - (f->tru_x - f->pan_x) / 4;
  227. else if ((f->tru_x - f->pan_x < 0) && (f->tru_x - f->pan_x >= -20) && (f->dst_w >= 660))
  228. reg_2870 = (10 + (f->tru_x - f->pan_x) / 2);
  229. if (f->dst_w >= f->src_w)
  230. reg_2870 = reg_2870 << 16 | reg_2870;
  231. else
  232. reg_2870 = ((reg_2870 & ~1) << 15) | (reg_2870 & ~1);
  233. }
  234. if (f->dst_w < f->src_w)
  235. reg_2870 = 0x000d000e - reg_2870;
  236. else
  237. reg_2870 = 0x0012000e - reg_2870;
  238. /* We're also using 2870 to shift the image left (src_x & negative dst_x) */
  239. reg_2870_offset = (f->src_x * ((f->dst_w << 21) / f->src_w)) >> 19;
  240. if (f->dst_w >= f->src_w) {
  241. x_cutoff &= ~1;
  242. master_width = (f->src_w * 0x00200000) / (f->dst_w);
  243. if (master_width * f->dst_w != f->src_w * 0x00200000)
  244. master_width++;
  245. reg_2834 = (reg_2834 << 16) | x_cutoff;
  246. reg_2838 = (reg_2838 << 16) | x_cutoff;
  247. reg_283c = master_width >> 2;
  248. reg_2844 = master_width >> 2;
  249. reg_2854 = master_width;
  250. reg_285c = master_width >> 1;
  251. reg_2864 = master_width >> 1;
  252. /* We also need to factor in the scaling
  253. (src_w - dst_w) / (src_w / 4) */
  254. if (f->dst_w > f->src_w)
  255. reg_2870_base = ((f->dst_w - f->src_w)<<16) / (f->src_w <<14);
  256. else
  257. reg_2870_base = 0;
  258. reg_2870 += (((reg_2870_offset << 14) & 0xFFFF0000) | reg_2870_offset >> 2) + (reg_2870_base << 17 | reg_2870_base);
  259. reg_2874 = 0;
  260. } else if (f->dst_w < f->src_w / 2) {
  261. master_width = (f->src_w * 0x00080000) / f->dst_w;
  262. if (master_width * f->dst_w != f->src_w * 0x00080000)
  263. master_width++;
  264. reg_2834 = (reg_2834 << 16) | x_cutoff;
  265. reg_2838 = (reg_2838 << 16) | x_cutoff;
  266. reg_283c = master_width >> 2;
  267. reg_2844 = master_width >> 1;
  268. reg_2854 = master_width;
  269. reg_285c = master_width >> 1;
  270. reg_2864 = master_width >> 1;
  271. reg_2870 += ((reg_2870_offset << 15) & 0xFFFF0000) | reg_2870_offset;
  272. reg_2870 += (5 - (((f->src_w + f->src_w / 2) - 1) / f->dst_w)) << 16;
  273. reg_2874 = 0x00000012;
  274. } else {
  275. master_width = (f->src_w * 0x00100000) / f->dst_w;
  276. if (master_width * f->dst_w != f->src_w * 0x00100000)
  277. master_width++;
  278. reg_2834 = (reg_2834 << 16) | x_cutoff;
  279. reg_2838 = (reg_2838 << 16) | x_cutoff;
  280. reg_283c = master_width >> 2;
  281. reg_2844 = master_width >> 1;
  282. reg_2854 = master_width;
  283. reg_285c = master_width >> 1;
  284. reg_2864 = master_width >> 1;
  285. reg_2870 += ((reg_2870_offset << 14) & 0xFFFF0000) | reg_2870_offset >> 1;
  286. reg_2870 += (5 - (((f->src_w * 3) - 1) / f->dst_w)) << 16;
  287. reg_2874 = 0x00000001;
  288. }
  289. /* Select the horizontal filter */
  290. if (f->src_w == f->dst_w) {
  291. /* An exact size match uses filter 0 */
  292. h_filter = 0;
  293. } else {
  294. /* Figure out which filter to use */
  295. h_filter = ((f->src_w << 16) / f->dst_w) >> 15;
  296. h_filter = (h_filter >> 1) + (h_filter & 1);
  297. /* Only an exact size match can use filter 0 */
  298. h_filter += !h_filter;
  299. }
  300. write_reg(reg_2834, 0x02834);
  301. write_reg(reg_2838, 0x02838);
  302. IVTV_DEBUG_YUV("Update reg 0x2834 %08x->%08x 0x2838 %08x->%08x\n",
  303. yi->reg_2834, reg_2834, yi->reg_2838, reg_2838);
  304. write_reg(reg_283c, 0x0283c);
  305. write_reg(reg_2844, 0x02844);
  306. IVTV_DEBUG_YUV("Update reg 0x283c %08x->%08x 0x2844 %08x->%08x\n",
  307. yi->reg_283c, reg_283c, yi->reg_2844, reg_2844);
  308. write_reg(0x00080514, 0x02840);
  309. write_reg(0x00100514, 0x02848);
  310. IVTV_DEBUG_YUV("Update reg 0x2840 %08x->%08x 0x2848 %08x->%08x\n",
  311. yi->reg_2840, 0x00080514, yi->reg_2848, 0x00100514);
  312. write_reg(reg_2854, 0x02854);
  313. IVTV_DEBUG_YUV("Update reg 0x2854 %08x->%08x \n",
  314. yi->reg_2854, reg_2854);
  315. write_reg(reg_285c, 0x0285c);
  316. write_reg(reg_2864, 0x02864);
  317. IVTV_DEBUG_YUV("Update reg 0x285c %08x->%08x 0x2864 %08x->%08x\n",
  318. yi->reg_285c, reg_285c, yi->reg_2864, reg_2864);
  319. write_reg(reg_2874, 0x02874);
  320. IVTV_DEBUG_YUV("Update reg 0x2874 %08x->%08x\n",
  321. yi->reg_2874, reg_2874);
  322. write_reg(reg_2870, 0x02870);
  323. IVTV_DEBUG_YUV("Update reg 0x2870 %08x->%08x\n",
  324. yi->reg_2870, reg_2870);
  325. write_reg(reg_2890, 0x02890);
  326. IVTV_DEBUG_YUV("Update reg 0x2890 %08x->%08x\n",
  327. yi->reg_2890, reg_2890);
  328. /* Only update the filter if we really need to */
  329. if (h_filter != yi->h_filter) {
  330. ivtv_yuv_filter(itv, h_filter, -1, -1);
  331. yi->h_filter = h_filter;
  332. }
  333. }
  334. static void ivtv_yuv_handle_vertical(struct ivtv *itv, struct yuv_frame_info *f)
  335. {
  336. struct yuv_playback_info *yi = &itv->yuv_info;
  337. u32 master_height;
  338. u32 reg_2918, reg_291c, reg_2920, reg_2928;
  339. u32 reg_2930, reg_2934, reg_293c;
  340. u32 reg_2940, reg_2944, reg_294c;
  341. u32 reg_2950, reg_2954, reg_2958, reg_295c;
  342. u32 reg_2960, reg_2964, reg_2968, reg_296c;
  343. u32 reg_289c;
  344. u32 src_major_y, src_minor_y;
  345. u32 src_major_uv, src_minor_uv;
  346. u32 reg_2964_base, reg_2968_base;
  347. int v_filter_1, v_filter_2;
  348. IVTV_DEBUG_WARN
  349. ("Adjust to height %d src_h %d dst_h %d src_y %d dst_y %d\n",
  350. f->tru_h, f->src_h, f->dst_h, f->src_y, f->dst_y);
  351. /* What scaling mode is being used... */
  352. IVTV_DEBUG_YUV("Scaling mode Y: %s\n",
  353. f->interlaced_y ? "Interlaced" : "Progressive");
  354. IVTV_DEBUG_YUV("Scaling mode UV: %s\n",
  355. f->interlaced_uv ? "Interlaced" : "Progressive");
  356. /* What is the source video being treated as... */
  357. IVTV_DEBUG_WARN("Source video: %s\n",
  358. f->interlaced ? "Interlaced" : "Progressive");
  359. /* We offset into the image using two different index methods, so split
  360. the y source coord into two parts. */
  361. if (f->src_y < 8) {
  362. src_minor_uv = f->src_y;
  363. src_major_uv = 0;
  364. } else {
  365. src_minor_uv = 8;
  366. src_major_uv = f->src_y - 8;
  367. }
  368. src_minor_y = src_minor_uv;
  369. src_major_y = src_major_uv;
  370. if (f->offset_y)
  371. src_minor_y += 16;
  372. if (f->interlaced_y)
  373. reg_2918 = (f->dst_h << 16) | (f->src_h + src_minor_y);
  374. else
  375. reg_2918 = (f->dst_h << 16) | ((f->src_h + src_minor_y) << 1);
  376. if (f->interlaced_uv)
  377. reg_291c = (f->dst_h << 16) | ((f->src_h + src_minor_uv) >> 1);
  378. else
  379. reg_291c = (f->dst_h << 16) | (f->src_h + src_minor_uv);
  380. reg_2964_base = (src_minor_y * ((f->dst_h << 16) / f->src_h)) >> 14;
  381. reg_2968_base = (src_minor_uv * ((f->dst_h << 16) / f->src_h)) >> 14;
  382. if (f->dst_h / 2 >= f->src_h && !f->interlaced_y) {
  383. master_height = (f->src_h * 0x00400000) / f->dst_h;
  384. if ((f->src_h * 0x00400000) - (master_height * f->dst_h) >= f->dst_h / 2)
  385. master_height++;
  386. reg_2920 = master_height >> 2;
  387. reg_2928 = master_height >> 3;
  388. reg_2930 = master_height;
  389. reg_2940 = master_height >> 1;
  390. reg_2964_base >>= 3;
  391. reg_2968_base >>= 3;
  392. reg_296c = 0x00000000;
  393. } else if (f->dst_h >= f->src_h) {
  394. master_height = (f->src_h * 0x00400000) / f->dst_h;
  395. master_height = (master_height >> 1) + (master_height & 1);
  396. reg_2920 = master_height >> 2;
  397. reg_2928 = master_height >> 2;
  398. reg_2930 = master_height;
  399. reg_2940 = master_height >> 1;
  400. reg_296c = 0x00000000;
  401. if (f->interlaced_y) {
  402. reg_2964_base >>= 3;
  403. } else {
  404. reg_296c++;
  405. reg_2964_base >>= 2;
  406. }
  407. if (f->interlaced_uv)
  408. reg_2928 >>= 1;
  409. reg_2968_base >>= 3;
  410. } else if (f->dst_h >= f->src_h / 2) {
  411. master_height = (f->src_h * 0x00200000) / f->dst_h;
  412. master_height = (master_height >> 1) + (master_height & 1);
  413. reg_2920 = master_height >> 2;
  414. reg_2928 = master_height >> 2;
  415. reg_2930 = master_height;
  416. reg_2940 = master_height;
  417. reg_296c = 0x00000101;
  418. if (f->interlaced_y) {
  419. reg_2964_base >>= 2;
  420. } else {
  421. reg_296c++;
  422. reg_2964_base >>= 1;
  423. }
  424. if (f->interlaced_uv)
  425. reg_2928 >>= 1;
  426. reg_2968_base >>= 2;
  427. } else {
  428. master_height = (f->src_h * 0x00100000) / f->dst_h;
  429. master_height = (master_height >> 1) + (master_height & 1);
  430. reg_2920 = master_height >> 2;
  431. reg_2928 = master_height >> 2;
  432. reg_2930 = master_height;
  433. reg_2940 = master_height;
  434. reg_2964_base >>= 1;
  435. reg_2968_base >>= 2;
  436. reg_296c = 0x00000102;
  437. }
  438. /* FIXME These registers change depending on scaled / unscaled output
  439. We really need to work out what they should be */
  440. if (f->src_h == f->dst_h) {
  441. reg_2934 = 0x00020000;
  442. reg_293c = 0x00100000;
  443. reg_2944 = 0x00040000;
  444. reg_294c = 0x000b0000;
  445. } else {
  446. reg_2934 = 0x00000FF0;
  447. reg_293c = 0x00000FF0;
  448. reg_2944 = 0x00000FF0;
  449. reg_294c = 0x00000FF0;
  450. }
  451. /* The first line to be displayed */
  452. reg_2950 = 0x00010000 + src_major_y;
  453. if (f->interlaced_y)
  454. reg_2950 += 0x00010000;
  455. reg_2954 = reg_2950 + 1;
  456. reg_2958 = 0x00010000 + (src_major_y >> 1);
  457. if (f->interlaced_uv)
  458. reg_2958 += 0x00010000;
  459. reg_295c = reg_2958 + 1;
  460. if (yi->decode_height == 480)
  461. reg_289c = 0x011e0017;
  462. else
  463. reg_289c = 0x01500017;
  464. if (f->dst_y < 0)
  465. reg_289c = (reg_289c - ((f->dst_y & ~1)<<15))-(f->dst_y >>1);
  466. else
  467. reg_289c = (reg_289c + ((f->dst_y & ~1)<<15))+(f->dst_y >>1);
  468. /* How much of the source to decode.
  469. Take into account the source offset */
  470. reg_2960 = ((src_minor_y + f->src_h + src_major_y) - 1) |
  471. (((src_minor_uv + f->src_h + src_major_uv - 1) & ~1) << 15);
  472. /* Calculate correct value for register 2964 */
  473. if (f->src_h == f->dst_h) {
  474. reg_2964 = 1;
  475. } else {
  476. reg_2964 = 2 + ((f->dst_h << 1) / f->src_h);
  477. reg_2964 = (reg_2964 >> 1) + (reg_2964 & 1);
  478. }
  479. reg_2968 = (reg_2964 << 16) + reg_2964 + (reg_2964 >> 1);
  480. reg_2964 = (reg_2964 << 16) + reg_2964 + (reg_2964 * 46 / 94);
  481. /* Okay, we've wasted time working out the correct value,
  482. but if we use it, it fouls the the window alignment.
  483. Fudge it to what we want... */
  484. reg_2964 = 0x00010001 + ((reg_2964 & 0x0000FFFF) - (reg_2964 >> 16));
  485. reg_2968 = 0x00010001 + ((reg_2968 & 0x0000FFFF) - (reg_2968 >> 16));
  486. /* Deviate further from what it should be. I find the flicker headache
  487. inducing so try to reduce it slightly. Leave 2968 as-is otherwise
  488. colours foul. */
  489. if ((reg_2964 != 0x00010001) && (f->dst_h / 2 <= f->src_h))
  490. reg_2964 = (reg_2964 & 0xFFFF0000) + ((reg_2964 & 0x0000FFFF) / 2);
  491. if (!f->interlaced_y)
  492. reg_2964 -= 0x00010001;
  493. if (!f->interlaced_uv)
  494. reg_2968 -= 0x00010001;
  495. reg_2964 += ((reg_2964_base << 16) | reg_2964_base);
  496. reg_2968 += ((reg_2968_base << 16) | reg_2968_base);
  497. /* Select the vertical filter */
  498. if (f->src_h == f->dst_h) {
  499. /* An exact size match uses filter 0/1 */
  500. v_filter_1 = 0;
  501. v_filter_2 = 1;
  502. } else {
  503. /* Figure out which filter to use */
  504. v_filter_1 = ((f->src_h << 16) / f->dst_h) >> 15;
  505. v_filter_1 = (v_filter_1 >> 1) + (v_filter_1 & 1);
  506. /* Only an exact size match can use filter 0 */
  507. v_filter_1 += !v_filter_1;
  508. v_filter_2 = v_filter_1;
  509. }
  510. write_reg(reg_2934, 0x02934);
  511. write_reg(reg_293c, 0x0293c);
  512. IVTV_DEBUG_YUV("Update reg 0x2934 %08x->%08x 0x293c %08x->%08x\n",
  513. yi->reg_2934, reg_2934, yi->reg_293c, reg_293c);
  514. write_reg(reg_2944, 0x02944);
  515. write_reg(reg_294c, 0x0294c);
  516. IVTV_DEBUG_YUV("Update reg 0x2944 %08x->%08x 0x294c %08x->%08x\n",
  517. yi->reg_2944, reg_2944, yi->reg_294c, reg_294c);
  518. /* Ensure 2970 is 0 (does it ever change ?) */
  519. /* write_reg(0,0x02970); */
  520. /* IVTV_DEBUG_YUV("Update reg 0x2970 %08x->%08x\n", yi->reg_2970, 0); */
  521. write_reg(reg_2930, 0x02938);
  522. write_reg(reg_2930, 0x02930);
  523. IVTV_DEBUG_YUV("Update reg 0x2930 %08x->%08x 0x2938 %08x->%08x\n",
  524. yi->reg_2930, reg_2930, yi->reg_2938, reg_2930);
  525. write_reg(reg_2928, 0x02928);
  526. write_reg(reg_2928 + 0x514, 0x0292C);
  527. IVTV_DEBUG_YUV("Update reg 0x2928 %08x->%08x 0x292c %08x->%08x\n",
  528. yi->reg_2928, reg_2928, yi->reg_292c, reg_2928 + 0x514);
  529. write_reg(reg_2920, 0x02920);
  530. write_reg(reg_2920 + 0x514, 0x02924);
  531. IVTV_DEBUG_YUV("Update reg 0x2920 %08x->%08x 0x2924 %08x->%08x\n",
  532. yi->reg_2920, reg_2920, yi->reg_2924, reg_2920 + 0x514);
  533. write_reg(reg_2918, 0x02918);
  534. write_reg(reg_291c, 0x0291C);
  535. IVTV_DEBUG_YUV("Update reg 0x2918 %08x->%08x 0x291C %08x->%08x\n",
  536. yi->reg_2918, reg_2918, yi->reg_291c, reg_291c);
  537. write_reg(reg_296c, 0x0296c);
  538. IVTV_DEBUG_YUV("Update reg 0x296c %08x->%08x\n",
  539. yi->reg_296c, reg_296c);
  540. write_reg(reg_2940, 0x02948);
  541. write_reg(reg_2940, 0x02940);
  542. IVTV_DEBUG_YUV("Update reg 0x2940 %08x->%08x 0x2948 %08x->%08x\n",
  543. yi->reg_2940, reg_2940, yi->reg_2948, reg_2940);
  544. write_reg(reg_2950, 0x02950);
  545. write_reg(reg_2954, 0x02954);
  546. IVTV_DEBUG_YUV("Update reg 0x2950 %08x->%08x 0x2954 %08x->%08x\n",
  547. yi->reg_2950, reg_2950, yi->reg_2954, reg_2954);
  548. write_reg(reg_2958, 0x02958);
  549. write_reg(reg_295c, 0x0295C);
  550. IVTV_DEBUG_YUV("Update reg 0x2958 %08x->%08x 0x295C %08x->%08x\n",
  551. yi->reg_2958, reg_2958, yi->reg_295c, reg_295c);
  552. write_reg(reg_2960, 0x02960);
  553. IVTV_DEBUG_YUV("Update reg 0x2960 %08x->%08x \n",
  554. yi->reg_2960, reg_2960);
  555. write_reg(reg_2964, 0x02964);
  556. write_reg(reg_2968, 0x02968);
  557. IVTV_DEBUG_YUV("Update reg 0x2964 %08x->%08x 0x2968 %08x->%08x\n",
  558. yi->reg_2964, reg_2964, yi->reg_2968, reg_2968);
  559. write_reg(reg_289c, 0x0289c);
  560. IVTV_DEBUG_YUV("Update reg 0x289c %08x->%08x\n",
  561. yi->reg_289c, reg_289c);
  562. /* Only update filter 1 if we really need to */
  563. if (v_filter_1 != yi->v_filter_1) {
  564. ivtv_yuv_filter(itv, -1, v_filter_1, -1);
  565. yi->v_filter_1 = v_filter_1;
  566. }
  567. /* Only update filter 2 if we really need to */
  568. if (v_filter_2 != yi->v_filter_2) {
  569. ivtv_yuv_filter(itv, -1, -1, v_filter_2);
  570. yi->v_filter_2 = v_filter_2;
  571. }
  572. }
  573. /* Modify the supplied coordinate information to fit the visible osd area */
  574. static u32 ivtv_yuv_window_setup(struct ivtv *itv, struct yuv_frame_info *f)
  575. {
  576. struct yuv_frame_info *of = &itv->yuv_info.old_frame_info;
  577. int osd_crop;
  578. u32 osd_scale;
  579. u32 yuv_update = 0;
  580. /* Sorry, but no negative coords for src */
  581. if (f->src_x < 0)
  582. f->src_x = 0;
  583. if (f->src_y < 0)
  584. f->src_y = 0;
  585. /* Can only reduce width down to 1/4 original size */
  586. if ((osd_crop = f->src_w - 4 * f->dst_w) > 0) {
  587. f->src_x += osd_crop / 2;
  588. f->src_w = (f->src_w - osd_crop) & ~3;
  589. f->dst_w = f->src_w / 4;
  590. f->dst_w += f->dst_w & 1;
  591. }
  592. /* Can only reduce height down to 1/4 original size */
  593. if (f->src_h / f->dst_h >= 2) {
  594. /* Overflow may be because we're running progressive,
  595. so force mode switch */
  596. f->interlaced_y = 1;
  597. /* Make sure we're still within limits for interlace */
  598. if ((osd_crop = f->src_h - 4 * f->dst_h) > 0) {
  599. /* If we reach here we'll have to force the height. */
  600. f->src_y += osd_crop / 2;
  601. f->src_h = (f->src_h - osd_crop) & ~3;
  602. f->dst_h = f->src_h / 4;
  603. f->dst_h += f->dst_h & 1;
  604. }
  605. }
  606. /* If there's nothing to safe to display, we may as well stop now */
  607. if ((int)f->dst_w <= 2 || (int)f->dst_h <= 2 ||
  608. (int)f->src_w <= 2 || (int)f->src_h <= 2) {
  609. return IVTV_YUV_UPDATE_INVALID;
  610. }
  611. /* Ensure video remains inside OSD area */
  612. osd_scale = (f->src_h << 16) / f->dst_h;
  613. if ((osd_crop = f->pan_y - f->dst_y) > 0) {
  614. /* Falls off the upper edge - crop */
  615. f->src_y += (osd_scale * osd_crop) >> 16;
  616. f->src_h -= (osd_scale * osd_crop) >> 16;
  617. f->dst_h -= osd_crop;
  618. f->dst_y = 0;
  619. } else {
  620. f->dst_y -= f->pan_y;
  621. }
  622. if ((osd_crop = f->dst_h + f->dst_y - f->vis_h) > 0) {
  623. /* Falls off the lower edge - crop */
  624. f->dst_h -= osd_crop;
  625. f->src_h -= (osd_scale * osd_crop) >> 16;
  626. }
  627. osd_scale = (f->src_w << 16) / f->dst_w;
  628. if ((osd_crop = f->pan_x - f->dst_x) > 0) {
  629. /* Fall off the left edge - crop */
  630. f->src_x += (osd_scale * osd_crop) >> 16;
  631. f->src_w -= (osd_scale * osd_crop) >> 16;
  632. f->dst_w -= osd_crop;
  633. f->dst_x = 0;
  634. } else {
  635. f->dst_x -= f->pan_x;
  636. }
  637. if ((osd_crop = f->dst_w + f->dst_x - f->vis_w) > 0) {
  638. /* Falls off the right edge - crop */
  639. f->dst_w -= osd_crop;
  640. f->src_w -= (osd_scale * osd_crop) >> 16;
  641. }
  642. if (itv->yuv_info.track_osd) {
  643. /* The OSD can be moved. Track to it */
  644. f->dst_x += itv->yuv_info.osd_x_offset;
  645. f->dst_y += itv->yuv_info.osd_y_offset;
  646. }
  647. /* Width & height for both src & dst must be even.
  648. Same for coordinates. */
  649. f->dst_w &= ~1;
  650. f->dst_x &= ~1;
  651. f->src_w += f->src_x & 1;
  652. f->src_x &= ~1;
  653. f->src_w &= ~1;
  654. f->dst_w &= ~1;
  655. f->dst_h &= ~1;
  656. f->dst_y &= ~1;
  657. f->src_h += f->src_y & 1;
  658. f->src_y &= ~1;
  659. f->src_h &= ~1;
  660. f->dst_h &= ~1;
  661. /* Due to rounding, we may have reduced the output size to <1/4 of
  662. the source. Check again, but this time just resize. Don't change
  663. source coordinates */
  664. if (f->dst_w < f->src_w / 4) {
  665. f->src_w &= ~3;
  666. f->dst_w = f->src_w / 4;
  667. f->dst_w += f->dst_w & 1;
  668. }
  669. if (f->dst_h < f->src_h / 4) {
  670. f->src_h &= ~3;
  671. f->dst_h = f->src_h / 4;
  672. f->dst_h += f->dst_h & 1;
  673. }
  674. /* Check again. If there's nothing to safe to display, stop now */
  675. if ((int)f->dst_w <= 2 || (int)f->dst_h <= 2 ||
  676. (int)f->src_w <= 2 || (int)f->src_h <= 2) {
  677. return IVTV_YUV_UPDATE_INVALID;
  678. }
  679. /* Both x offset & width are linked, so they have to be done together */
  680. if ((of->dst_w != f->dst_w) || (of->src_w != f->src_w) ||
  681. (of->dst_x != f->dst_x) || (of->src_x != f->src_x) ||
  682. (of->pan_x != f->pan_x) || (of->vis_w != f->vis_w)) {
  683. yuv_update |= IVTV_YUV_UPDATE_HORIZONTAL;
  684. }
  685. if ((of->src_h != f->src_h) || (of->dst_h != f->dst_h) ||
  686. (of->dst_y != f->dst_y) || (of->src_y != f->src_y) ||
  687. (of->pan_y != f->pan_y) || (of->vis_h != f->vis_h) ||
  688. (of->lace_mode != f->lace_mode) ||
  689. (of->interlaced_y != f->interlaced_y) ||
  690. (of->interlaced_uv != f->interlaced_uv)) {
  691. yuv_update |= IVTV_YUV_UPDATE_VERTICAL;
  692. }
  693. return yuv_update;
  694. }
  695. /* Update the scaling register to the requested value */
  696. void ivtv_yuv_work_handler(struct ivtv *itv)
  697. {
  698. struct yuv_playback_info *yi = &itv->yuv_info;
  699. struct yuv_frame_info f;
  700. int frame = yi->update_frame;
  701. u32 yuv_update;
  702. IVTV_DEBUG_YUV("Update yuv registers for frame %d\n", frame);
  703. f = yi->new_frame_info[frame];
  704. if (yi->track_osd) {
  705. /* Snapshot the osd pan info */
  706. f.pan_x = yi->osd_x_pan;
  707. f.pan_y = yi->osd_y_pan;
  708. f.vis_w = yi->osd_vis_w;
  709. f.vis_h = yi->osd_vis_h;
  710. } else {
  711. /* Not tracking the osd, so assume full screen */
  712. f.pan_x = 0;
  713. f.pan_y = 0;
  714. f.vis_w = 720;
  715. f.vis_h = yi->decode_height;
  716. }
  717. /* Calculate the display window coordinates. Exit if nothing left */
  718. if (!(yuv_update = ivtv_yuv_window_setup(itv, &f)))
  719. return;
  720. if (yuv_update & IVTV_YUV_UPDATE_INVALID) {
  721. write_reg(0x01008080, 0x2898);
  722. } else if (yuv_update) {
  723. write_reg(0x00108080, 0x2898);
  724. if (yuv_update & IVTV_YUV_UPDATE_HORIZONTAL)
  725. ivtv_yuv_handle_horizontal(itv, &f);
  726. if (yuv_update & IVTV_YUV_UPDATE_VERTICAL)
  727. ivtv_yuv_handle_vertical(itv, &f);
  728. }
  729. yi->old_frame_info = f;
  730. }
  731. static void ivtv_yuv_init(struct ivtv *itv)
  732. {
  733. struct yuv_playback_info *yi = &itv->yuv_info;
  734. IVTV_DEBUG_YUV("ivtv_yuv_init\n");
  735. /* Take a snapshot of the current register settings */
  736. yi->reg_2834 = read_reg(0x02834);
  737. yi->reg_2838 = read_reg(0x02838);
  738. yi->reg_283c = read_reg(0x0283c);
  739. yi->reg_2840 = read_reg(0x02840);
  740. yi->reg_2844 = read_reg(0x02844);
  741. yi->reg_2848 = read_reg(0x02848);
  742. yi->reg_2854 = read_reg(0x02854);
  743. yi->reg_285c = read_reg(0x0285c);
  744. yi->reg_2864 = read_reg(0x02864);
  745. yi->reg_2870 = read_reg(0x02870);
  746. yi->reg_2874 = read_reg(0x02874);
  747. yi->reg_2898 = read_reg(0x02898);
  748. yi->reg_2890 = read_reg(0x02890);
  749. yi->reg_289c = read_reg(0x0289c);
  750. yi->reg_2918 = read_reg(0x02918);
  751. yi->reg_291c = read_reg(0x0291c);
  752. yi->reg_2920 = read_reg(0x02920);
  753. yi->reg_2924 = read_reg(0x02924);
  754. yi->reg_2928 = read_reg(0x02928);
  755. yi->reg_292c = read_reg(0x0292c);
  756. yi->reg_2930 = read_reg(0x02930);
  757. yi->reg_2934 = read_reg(0x02934);
  758. yi->reg_2938 = read_reg(0x02938);
  759. yi->reg_293c = read_reg(0x0293c);
  760. yi->reg_2940 = read_reg(0x02940);
  761. yi->reg_2944 = read_reg(0x02944);
  762. yi->reg_2948 = read_reg(0x02948);
  763. yi->reg_294c = read_reg(0x0294c);
  764. yi->reg_2950 = read_reg(0x02950);
  765. yi->reg_2954 = read_reg(0x02954);
  766. yi->reg_2958 = read_reg(0x02958);
  767. yi->reg_295c = read_reg(0x0295c);
  768. yi->reg_2960 = read_reg(0x02960);
  769. yi->reg_2964 = read_reg(0x02964);
  770. yi->reg_2968 = read_reg(0x02968);
  771. yi->reg_296c = read_reg(0x0296c);
  772. yi->reg_2970 = read_reg(0x02970);
  773. yi->v_filter_1 = -1;
  774. yi->v_filter_2 = -1;
  775. yi->h_filter = -1;
  776. /* Set some valid size info */
  777. yi->osd_x_offset = read_reg(0x02a04) & 0x00000FFF;
  778. yi->osd_y_offset = (read_reg(0x02a04) >> 16) & 0x00000FFF;
  779. /* Bit 2 of reg 2878 indicates current decoder output format
  780. 0 : NTSC 1 : PAL */
  781. if (read_reg(0x2878) & 4)
  782. yi->decode_height = 576;
  783. else
  784. yi->decode_height = 480;
  785. if (!itv->osd_info) {
  786. yi->osd_vis_w = 720 - yi->osd_x_offset;
  787. yi->osd_vis_h = yi->decode_height - yi->osd_y_offset;
  788. } else {
  789. /* If no visible size set, assume full size */
  790. if (!yi->osd_vis_w)
  791. yi->osd_vis_w = 720 - yi->osd_x_offset;
  792. if (!yi->osd_vis_h) {
  793. yi->osd_vis_h = yi->decode_height - yi->osd_y_offset;
  794. } else if (yi->osd_vis_h + yi->osd_y_offset > yi->decode_height) {
  795. /* If output video standard has changed, requested height may
  796. not be legal */
  797. IVTV_DEBUG_WARN("Clipping yuv output - fb size (%d) exceeds video standard limit (%d)\n",
  798. yi->osd_vis_h + yi->osd_y_offset,
  799. yi->decode_height);
  800. yi->osd_vis_h = yi->decode_height - yi->osd_y_offset;
  801. }
  802. }
  803. /* We need a buffer for blanking when Y plane is offset - non-fatal if we can't get one */
  804. yi->blanking_ptr = kzalloc(720 * 16, GFP_ATOMIC|__GFP_NOWARN);
  805. if (yi->blanking_ptr) {
  806. yi->blanking_dmaptr = pci_map_single(itv->pdev, yi->blanking_ptr, 720*16, PCI_DMA_TODEVICE);
  807. } else {
  808. yi->blanking_dmaptr = 0;
  809. IVTV_DEBUG_WARN("Failed to allocate yuv blanking buffer\n");
  810. }
  811. /* Enable YUV decoder output */
  812. write_reg_sync(0x01, IVTV_REG_VDM);
  813. set_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags);
  814. atomic_set(&yi->next_dma_frame, 0);
  815. }
  816. /* Get next available yuv buffer on PVR350 */
  817. static void ivtv_yuv_next_free(struct ivtv *itv)
  818. {
  819. int draw, display;
  820. struct yuv_playback_info *yi = &itv->yuv_info;
  821. if (atomic_read(&yi->next_dma_frame) == -1)
  822. ivtv_yuv_init(itv);
  823. draw = atomic_read(&yi->next_fill_frame);
  824. display = atomic_read(&yi->next_dma_frame);
  825. if (display > draw)
  826. display -= IVTV_YUV_BUFFERS;
  827. if (draw - display >= yi->max_frames_buffered)
  828. draw = (u8)(draw - 1) % IVTV_YUV_BUFFERS;
  829. else
  830. yi->new_frame_info[draw].update = 0;
  831. yi->draw_frame = draw;
  832. }
  833. /* Set up frame according to ivtv_dma_frame parameters */
  834. static void ivtv_yuv_setup_frame(struct ivtv *itv, struct ivtv_dma_frame *args)
  835. {
  836. struct yuv_playback_info *yi = &itv->yuv_info;
  837. u8 frame = yi->draw_frame;
  838. u8 last_frame = (u8)(frame - 1) % IVTV_YUV_BUFFERS;
  839. struct yuv_frame_info *nf = &yi->new_frame_info[frame];
  840. struct yuv_frame_info *of = &yi->new_frame_info[last_frame];
  841. int lace_threshold = yi->lace_threshold;
  842. /* Preserve old update flag in case we're overwriting a queued frame */
  843. int update = nf->update;
  844. /* Take a snapshot of the yuv coordinate information */
  845. nf->src_x = args->src.left;
  846. nf->src_y = args->src.top;
  847. nf->src_w = args->src.width;
  848. nf->src_h = args->src.height;
  849. nf->dst_x = args->dst.left;
  850. nf->dst_y = args->dst.top;
  851. nf->dst_w = args->dst.width;
  852. nf->dst_h = args->dst.height;
  853. nf->tru_x = args->dst.left;
  854. nf->tru_w = args->src_width;
  855. nf->tru_h = args->src_height;
  856. /* Are we going to offset the Y plane */
  857. nf->offset_y = (nf->tru_h + nf->src_x < 512 - 16) ? 1 : 0;
  858. nf->update = 0;
  859. nf->interlaced_y = 0;
  860. nf->interlaced_uv = 0;
  861. nf->delay = 0;
  862. nf->sync_field = 0;
  863. nf->lace_mode = yi->lace_mode & IVTV_YUV_MODE_MASK;
  864. if (lace_threshold < 0)
  865. lace_threshold = yi->decode_height - 1;
  866. /* Work out the lace settings */
  867. switch (nf->lace_mode) {
  868. case IVTV_YUV_MODE_PROGRESSIVE: /* Progressive mode */
  869. nf->interlaced = 0;
  870. if (nf->tru_h < 512 || (nf->tru_h > 576 && nf->tru_h < 1021))
  871. nf->interlaced_y = 0;
  872. else
  873. nf->interlaced_y = 1;
  874. if (nf->tru_h < 1021 && (nf->dst_h >= nf->src_h / 2))
  875. nf->interlaced_uv = 0;
  876. else
  877. nf->interlaced_uv = 1;
  878. break;
  879. case IVTV_YUV_MODE_AUTO:
  880. if (nf->tru_h <= lace_threshold || nf->tru_h > 576 || nf->tru_w > 720) {
  881. nf->interlaced = 0;
  882. if ((nf->tru_h < 512) ||
  883. (nf->tru_h > 576 && nf->tru_h < 1021) ||
  884. (nf->tru_w > 720 && nf->tru_h < 1021))
  885. nf->interlaced_y = 0;
  886. else
  887. nf->interlaced_y = 1;
  888. if (nf->tru_h < 1021 && (nf->dst_h >= nf->src_h / 2))
  889. nf->interlaced_uv = 0;
  890. else
  891. nf->interlaced_uv = 1;
  892. } else {
  893. nf->interlaced = 1;
  894. nf->interlaced_y = 1;
  895. nf->interlaced_uv = 1;
  896. }
  897. break;
  898. case IVTV_YUV_MODE_INTERLACED: /* Interlace mode */
  899. default:
  900. nf->interlaced = 1;
  901. nf->interlaced_y = 1;
  902. nf->interlaced_uv = 1;
  903. break;
  904. }
  905. if (memcmp(&yi->old_frame_info_args, nf, sizeof(*nf))) {
  906. yi->old_frame_info_args = *nf;
  907. nf->update = 1;
  908. IVTV_DEBUG_YUV("Requesting reg update for frame %d\n", frame);
  909. }
  910. nf->update |= update;
  911. nf->sync_field = yi->lace_sync_field;
  912. nf->delay = nf->sync_field != of->sync_field;
  913. }
  914. /* Frame is complete & ready for display */
  915. void ivtv_yuv_frame_complete(struct ivtv *itv)
  916. {
  917. atomic_set(&itv->yuv_info.next_fill_frame,
  918. (itv->yuv_info.draw_frame + 1) % IVTV_YUV_BUFFERS);
  919. }
  920. static int ivtv_yuv_udma_frame(struct ivtv *itv, struct ivtv_dma_frame *args)
  921. {
  922. DEFINE_WAIT(wait);
  923. int rc = 0;
  924. int got_sig = 0;
  925. /* DMA the frame */
  926. mutex_lock(&itv->udma.lock);
  927. if ((rc = ivtv_yuv_prep_user_dma(itv, &itv->udma, args)) != 0) {
  928. mutex_unlock(&itv->udma.lock);
  929. return rc;
  930. }
  931. ivtv_udma_prepare(itv);
  932. prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
  933. /* if no UDMA is pending and no UDMA is in progress, then the DMA
  934. is finished */
  935. while (test_bit(IVTV_F_I_UDMA_PENDING, &itv->i_flags) ||
  936. test_bit(IVTV_F_I_UDMA, &itv->i_flags)) {
  937. /* don't interrupt if the DMA is in progress but break off
  938. a still pending DMA. */
  939. got_sig = signal_pending(current);
  940. if (got_sig && test_and_clear_bit(IVTV_F_I_UDMA_PENDING, &itv->i_flags))
  941. break;
  942. got_sig = 0;
  943. schedule();
  944. }
  945. finish_wait(&itv->dma_waitq, &wait);
  946. /* Unmap Last DMA Xfer */
  947. ivtv_udma_unmap(itv);
  948. if (got_sig) {
  949. IVTV_DEBUG_INFO("User stopped YUV UDMA\n");
  950. mutex_unlock(&itv->udma.lock);
  951. return -EINTR;
  952. }
  953. ivtv_yuv_frame_complete(itv);
  954. mutex_unlock(&itv->udma.lock);
  955. return rc;
  956. }
  957. /* Setup frame according to V4L2 parameters */
  958. void ivtv_yuv_setup_stream_frame(struct ivtv *itv)
  959. {
  960. struct yuv_playback_info *yi = &itv->yuv_info;
  961. struct ivtv_dma_frame dma_args;
  962. ivtv_yuv_next_free(itv);
  963. /* Copy V4L2 parameters to an ivtv_dma_frame struct... */
  964. dma_args.y_source = NULL;
  965. dma_args.uv_source = NULL;
  966. dma_args.src.left = 0;
  967. dma_args.src.top = 0;
  968. dma_args.src.width = yi->v4l2_src_w;
  969. dma_args.src.height = yi->v4l2_src_h;
  970. dma_args.dst = yi->main_rect;
  971. dma_args.src_width = yi->v4l2_src_w;
  972. dma_args.src_height = yi->v4l2_src_h;
  973. /* ... and use the same setup routine as ivtv_yuv_prep_frame */
  974. ivtv_yuv_setup_frame(itv, &dma_args);
  975. if (!itv->dma_data_req_offset)
  976. itv->dma_data_req_offset = yuv_offset[yi->draw_frame];
  977. }
  978. /* Attempt to dma a frame from a user buffer */
  979. int ivtv_yuv_udma_stream_frame(struct ivtv *itv, void __user *src)
  980. {
  981. struct yuv_playback_info *yi = &itv->yuv_info;
  982. struct ivtv_dma_frame dma_args;
  983. int res;
  984. ivtv_yuv_setup_stream_frame(itv);
  985. /* We only need to supply source addresses for this */
  986. dma_args.y_source = src;
  987. dma_args.uv_source = src + 720 * ((yi->v4l2_src_h + 31) & ~31);
  988. /* Wait for frame DMA. Note that serialize_lock is locked,
  989. so to allow other processes to access the driver while
  990. we are waiting unlock first and later lock again. */
  991. mutex_unlock(&itv->serialize_lock);
  992. res = ivtv_yuv_udma_frame(itv, &dma_args);
  993. mutex_lock(&itv->serialize_lock);
  994. return res;
  995. }
  996. /* IVTV_IOC_DMA_FRAME ioctl handler */
  997. int ivtv_yuv_prep_frame(struct ivtv *itv, struct ivtv_dma_frame *args)
  998. {
  999. int res;
  1000. /* IVTV_DEBUG_INFO("yuv_prep_frame\n"); */
  1001. ivtv_yuv_next_free(itv);
  1002. ivtv_yuv_setup_frame(itv, args);
  1003. /* Wait for frame DMA. Note that serialize_lock is locked,
  1004. so to allow other processes to access the driver while
  1005. we are waiting unlock first and later lock again. */
  1006. mutex_unlock(&itv->serialize_lock);
  1007. res = ivtv_yuv_udma_frame(itv, args);
  1008. mutex_lock(&itv->serialize_lock);
  1009. return res;
  1010. }
  1011. void ivtv_yuv_close(struct ivtv *itv)
  1012. {
  1013. struct yuv_playback_info *yi = &itv->yuv_info;
  1014. int h_filter, v_filter_1, v_filter_2;
  1015. IVTV_DEBUG_YUV("ivtv_yuv_close\n");
  1016. mutex_unlock(&itv->serialize_lock);
  1017. ivtv_waitq(&itv->vsync_waitq);
  1018. mutex_lock(&itv->serialize_lock);
  1019. yi->running = 0;
  1020. atomic_set(&yi->next_dma_frame, -1);
  1021. atomic_set(&yi->next_fill_frame, 0);
  1022. /* Reset registers we have changed so mpeg playback works */
  1023. /* If we fully restore this register, the display may remain active.
  1024. Restore, but set one bit to blank the video. Firmware will always
  1025. clear this bit when needed, so not a problem. */
  1026. write_reg(yi->reg_2898 | 0x01000000, 0x2898);
  1027. write_reg(yi->reg_2834, 0x02834);
  1028. write_reg(yi->reg_2838, 0x02838);
  1029. write_reg(yi->reg_283c, 0x0283c);
  1030. write_reg(yi->reg_2840, 0x02840);
  1031. write_reg(yi->reg_2844, 0x02844);
  1032. write_reg(yi->reg_2848, 0x02848);
  1033. write_reg(yi->reg_2854, 0x02854);
  1034. write_reg(yi->reg_285c, 0x0285c);
  1035. write_reg(yi->reg_2864, 0x02864);
  1036. write_reg(yi->reg_2870, 0x02870);
  1037. write_reg(yi->reg_2874, 0x02874);
  1038. write_reg(yi->reg_2890, 0x02890);
  1039. write_reg(yi->reg_289c, 0x0289c);
  1040. write_reg(yi->reg_2918, 0x02918);
  1041. write_reg(yi->reg_291c, 0x0291c);
  1042. write_reg(yi->reg_2920, 0x02920);
  1043. write_reg(yi->reg_2924, 0x02924);
  1044. write_reg(yi->reg_2928, 0x02928);
  1045. write_reg(yi->reg_292c, 0x0292c);
  1046. write_reg(yi->reg_2930, 0x02930);
  1047. write_reg(yi->reg_2934, 0x02934);
  1048. write_reg(yi->reg_2938, 0x02938);
  1049. write_reg(yi->reg_293c, 0x0293c);
  1050. write_reg(yi->reg_2940, 0x02940);
  1051. write_reg(yi->reg_2944, 0x02944);
  1052. write_reg(yi->reg_2948, 0x02948);
  1053. write_reg(yi->reg_294c, 0x0294c);
  1054. write_reg(yi->reg_2950, 0x02950);
  1055. write_reg(yi->reg_2954, 0x02954);
  1056. write_reg(yi->reg_2958, 0x02958);
  1057. write_reg(yi->reg_295c, 0x0295c);
  1058. write_reg(yi->reg_2960, 0x02960);
  1059. write_reg(yi->reg_2964, 0x02964);
  1060. write_reg(yi->reg_2968, 0x02968);
  1061. write_reg(yi->reg_296c, 0x0296c);
  1062. write_reg(yi->reg_2970, 0x02970);
  1063. /* Prepare to restore filters */
  1064. /* First the horizontal filter */
  1065. if ((yi->reg_2834 & 0x0000FFFF) == (yi->reg_2834 >> 16)) {
  1066. /* An exact size match uses filter 0 */
  1067. h_filter = 0;
  1068. } else {
  1069. /* Figure out which filter to use */
  1070. h_filter = ((yi->reg_2834 << 16) / (yi->reg_2834 >> 16)) >> 15;
  1071. h_filter = (h_filter >> 1) + (h_filter & 1);
  1072. /* Only an exact size match can use filter 0. */
  1073. h_filter += !h_filter;
  1074. }
  1075. /* Now the vertical filter */
  1076. if ((yi->reg_2918 & 0x0000FFFF) == (yi->reg_2918 >> 16)) {
  1077. /* An exact size match uses filter 0/1 */
  1078. v_filter_1 = 0;
  1079. v_filter_2 = 1;
  1080. } else {
  1081. /* Figure out which filter to use */
  1082. v_filter_1 = ((yi->reg_2918 << 16) / (yi->reg_2918 >> 16)) >> 15;
  1083. v_filter_1 = (v_filter_1 >> 1) + (v_filter_1 & 1);
  1084. /* Only an exact size match can use filter 0 */
  1085. v_filter_1 += !v_filter_1;
  1086. v_filter_2 = v_filter_1;
  1087. }
  1088. /* Now restore the filters */
  1089. ivtv_yuv_filter(itv, h_filter, v_filter_1, v_filter_2);
  1090. /* and clear a few registers */
  1091. write_reg(0, 0x02814);
  1092. write_reg(0, 0x0282c);
  1093. write_reg(0, 0x02904);
  1094. write_reg(0, 0x02910);
  1095. /* Release the blanking buffer */
  1096. if (yi->blanking_ptr) {
  1097. kfree(yi->blanking_ptr);
  1098. yi->blanking_ptr = NULL;
  1099. pci_unmap_single(itv->pdev, yi->blanking_dmaptr, 720*16, PCI_DMA_TODEVICE);
  1100. }
  1101. /* Invalidate the old dimension information */
  1102. yi->old_frame_info.src_w = 0;
  1103. yi->old_frame_info.src_h = 0;
  1104. yi->old_frame_info_args.src_w = 0;
  1105. yi->old_frame_info_args.src_h = 0;
  1106. /* All done. */
  1107. clear_bit(IVTV_F_I_DECODING_YUV, &itv->i_flags);
  1108. }