ivtv-yuv.c 40 KB

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