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