saa7134-video.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336
  1. /*
  2. *
  3. * device driver for philips saa7134 based TV cards
  4. * video4linux video interface
  5. *
  6. * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include "saa7134.h"
  19. #include "saa7134-reg.h"
  20. #include <linux/init.h>
  21. #include <linux/list.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/slab.h>
  25. #include <linux/sort.h>
  26. #include <media/v4l2-common.h>
  27. #include <media/v4l2-event.h>
  28. #include <media/i2c/saa6588.h>
  29. /* ------------------------------------------------------------------ */
  30. unsigned int video_debug;
  31. static unsigned int gbuffers = 8;
  32. static unsigned int noninterlaced; /* 0 */
  33. static unsigned int gbufsize = 720*576*4;
  34. static unsigned int gbufsize_max = 720*576*4;
  35. static char secam[] = "--";
  36. module_param(video_debug, int, 0644);
  37. MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
  38. module_param(gbuffers, int, 0444);
  39. MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
  40. module_param(noninterlaced, int, 0644);
  41. MODULE_PARM_DESC(noninterlaced,"capture non interlaced video");
  42. module_param_string(secam, secam, sizeof(secam), 0644);
  43. MODULE_PARM_DESC(secam, "force SECAM variant, either DK,L or Lc");
  44. #define video_dbg(fmt, arg...) do { \
  45. if (video_debug & 0x04) \
  46. printk(KERN_DEBUG pr_fmt("video: " fmt), ## arg); \
  47. } while (0)
  48. /* ------------------------------------------------------------------ */
  49. /* Defines for Video Output Port Register at address 0x191 */
  50. /* Bit 0: VIP code T bit polarity */
  51. #define VP_T_CODE_P_NON_INVERTED 0x00
  52. #define VP_T_CODE_P_INVERTED 0x01
  53. /* ------------------------------------------------------------------ */
  54. /* Defines for Video Output Port Register at address 0x195 */
  55. /* Bit 2: Video output clock delay control */
  56. #define VP_CLK_CTRL2_NOT_DELAYED 0x00
  57. #define VP_CLK_CTRL2_DELAYED 0x04
  58. /* Bit 1: Video output clock invert control */
  59. #define VP_CLK_CTRL1_NON_INVERTED 0x00
  60. #define VP_CLK_CTRL1_INVERTED 0x02
  61. /* ------------------------------------------------------------------ */
  62. /* Defines for Video Output Port Register at address 0x196 */
  63. /* Bits 2 to 0: VSYNC pin video vertical sync type */
  64. #define VP_VS_TYPE_MASK 0x07
  65. #define VP_VS_TYPE_OFF 0x00
  66. #define VP_VS_TYPE_V123 0x01
  67. #define VP_VS_TYPE_V_ITU 0x02
  68. #define VP_VS_TYPE_VGATE_L 0x03
  69. #define VP_VS_TYPE_RESERVED1 0x04
  70. #define VP_VS_TYPE_RESERVED2 0x05
  71. #define VP_VS_TYPE_F_ITU 0x06
  72. #define VP_VS_TYPE_SC_FID 0x07
  73. /* ------------------------------------------------------------------ */
  74. /* data structs for video */
  75. static int video_out[][9] = {
  76. [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
  77. };
  78. static struct saa7134_format formats[] = {
  79. {
  80. .name = "8 bpp gray",
  81. .fourcc = V4L2_PIX_FMT_GREY,
  82. .depth = 8,
  83. .pm = 0x06,
  84. },{
  85. .name = "15 bpp RGB, le",
  86. .fourcc = V4L2_PIX_FMT_RGB555,
  87. .depth = 16,
  88. .pm = 0x13 | 0x80,
  89. },{
  90. .name = "15 bpp RGB, be",
  91. .fourcc = V4L2_PIX_FMT_RGB555X,
  92. .depth = 16,
  93. .pm = 0x13 | 0x80,
  94. .bswap = 1,
  95. },{
  96. .name = "16 bpp RGB, le",
  97. .fourcc = V4L2_PIX_FMT_RGB565,
  98. .depth = 16,
  99. .pm = 0x10 | 0x80,
  100. },{
  101. .name = "16 bpp RGB, be",
  102. .fourcc = V4L2_PIX_FMT_RGB565X,
  103. .depth = 16,
  104. .pm = 0x10 | 0x80,
  105. .bswap = 1,
  106. },{
  107. .name = "24 bpp RGB, le",
  108. .fourcc = V4L2_PIX_FMT_BGR24,
  109. .depth = 24,
  110. .pm = 0x11,
  111. },{
  112. .name = "24 bpp RGB, be",
  113. .fourcc = V4L2_PIX_FMT_RGB24,
  114. .depth = 24,
  115. .pm = 0x11,
  116. .bswap = 1,
  117. },{
  118. .name = "32 bpp RGB, le",
  119. .fourcc = V4L2_PIX_FMT_BGR32,
  120. .depth = 32,
  121. .pm = 0x12,
  122. },{
  123. .name = "32 bpp RGB, be",
  124. .fourcc = V4L2_PIX_FMT_RGB32,
  125. .depth = 32,
  126. .pm = 0x12,
  127. .bswap = 1,
  128. .wswap = 1,
  129. },{
  130. .name = "4:2:2 packed, YUYV",
  131. .fourcc = V4L2_PIX_FMT_YUYV,
  132. .depth = 16,
  133. .pm = 0x00,
  134. .bswap = 1,
  135. .yuv = 1,
  136. },{
  137. .name = "4:2:2 packed, UYVY",
  138. .fourcc = V4L2_PIX_FMT_UYVY,
  139. .depth = 16,
  140. .pm = 0x00,
  141. .yuv = 1,
  142. },{
  143. .name = "4:2:2 planar, Y-Cb-Cr",
  144. .fourcc = V4L2_PIX_FMT_YUV422P,
  145. .depth = 16,
  146. .pm = 0x09,
  147. .yuv = 1,
  148. .planar = 1,
  149. .hshift = 1,
  150. .vshift = 0,
  151. },{
  152. .name = "4:2:0 planar, Y-Cb-Cr",
  153. .fourcc = V4L2_PIX_FMT_YUV420,
  154. .depth = 12,
  155. .pm = 0x0a,
  156. .yuv = 1,
  157. .planar = 1,
  158. .hshift = 1,
  159. .vshift = 1,
  160. },{
  161. .name = "4:2:0 planar, Y-Cb-Cr",
  162. .fourcc = V4L2_PIX_FMT_YVU420,
  163. .depth = 12,
  164. .pm = 0x0a,
  165. .yuv = 1,
  166. .planar = 1,
  167. .uvswap = 1,
  168. .hshift = 1,
  169. .vshift = 1,
  170. }
  171. };
  172. #define FORMATS ARRAY_SIZE(formats)
  173. #define NORM_625_50 \
  174. .h_start = 0, \
  175. .h_stop = 719, \
  176. .video_v_start = 24, \
  177. .video_v_stop = 311, \
  178. .vbi_v_start_0 = 7, \
  179. .vbi_v_stop_0 = 23, \
  180. .vbi_v_start_1 = 319, \
  181. .src_timing = 4
  182. #define NORM_525_60 \
  183. .h_start = 0, \
  184. .h_stop = 719, \
  185. .video_v_start = 23, \
  186. .video_v_stop = 262, \
  187. .vbi_v_start_0 = 10, \
  188. .vbi_v_stop_0 = 21, \
  189. .vbi_v_start_1 = 273, \
  190. .src_timing = 7
  191. static struct saa7134_tvnorm tvnorms[] = {
  192. {
  193. .name = "PAL", /* autodetect */
  194. .id = V4L2_STD_PAL,
  195. NORM_625_50,
  196. .sync_control = 0x18,
  197. .luma_control = 0x40,
  198. .chroma_ctrl1 = 0x81,
  199. .chroma_gain = 0x2a,
  200. .chroma_ctrl2 = 0x06,
  201. .vgate_misc = 0x1c,
  202. },{
  203. .name = "PAL-BG",
  204. .id = V4L2_STD_PAL_BG,
  205. NORM_625_50,
  206. .sync_control = 0x18,
  207. .luma_control = 0x40,
  208. .chroma_ctrl1 = 0x81,
  209. .chroma_gain = 0x2a,
  210. .chroma_ctrl2 = 0x06,
  211. .vgate_misc = 0x1c,
  212. },{
  213. .name = "PAL-I",
  214. .id = V4L2_STD_PAL_I,
  215. NORM_625_50,
  216. .sync_control = 0x18,
  217. .luma_control = 0x40,
  218. .chroma_ctrl1 = 0x81,
  219. .chroma_gain = 0x2a,
  220. .chroma_ctrl2 = 0x06,
  221. .vgate_misc = 0x1c,
  222. },{
  223. .name = "PAL-DK",
  224. .id = V4L2_STD_PAL_DK,
  225. NORM_625_50,
  226. .sync_control = 0x18,
  227. .luma_control = 0x40,
  228. .chroma_ctrl1 = 0x81,
  229. .chroma_gain = 0x2a,
  230. .chroma_ctrl2 = 0x06,
  231. .vgate_misc = 0x1c,
  232. },{
  233. .name = "NTSC",
  234. .id = V4L2_STD_NTSC,
  235. NORM_525_60,
  236. .sync_control = 0x59,
  237. .luma_control = 0x40,
  238. .chroma_ctrl1 = 0x89,
  239. .chroma_gain = 0x2a,
  240. .chroma_ctrl2 = 0x0e,
  241. .vgate_misc = 0x18,
  242. },{
  243. .name = "SECAM",
  244. .id = V4L2_STD_SECAM,
  245. NORM_625_50,
  246. .sync_control = 0x18,
  247. .luma_control = 0x1b,
  248. .chroma_ctrl1 = 0xd1,
  249. .chroma_gain = 0x80,
  250. .chroma_ctrl2 = 0x00,
  251. .vgate_misc = 0x1c,
  252. },{
  253. .name = "SECAM-DK",
  254. .id = V4L2_STD_SECAM_DK,
  255. NORM_625_50,
  256. .sync_control = 0x18,
  257. .luma_control = 0x1b,
  258. .chroma_ctrl1 = 0xd1,
  259. .chroma_gain = 0x80,
  260. .chroma_ctrl2 = 0x00,
  261. .vgate_misc = 0x1c,
  262. },{
  263. .name = "SECAM-L",
  264. .id = V4L2_STD_SECAM_L,
  265. NORM_625_50,
  266. .sync_control = 0x18,
  267. .luma_control = 0x1b,
  268. .chroma_ctrl1 = 0xd1,
  269. .chroma_gain = 0x80,
  270. .chroma_ctrl2 = 0x00,
  271. .vgate_misc = 0x1c,
  272. },{
  273. .name = "SECAM-Lc",
  274. .id = V4L2_STD_SECAM_LC,
  275. NORM_625_50,
  276. .sync_control = 0x18,
  277. .luma_control = 0x1b,
  278. .chroma_ctrl1 = 0xd1,
  279. .chroma_gain = 0x80,
  280. .chroma_ctrl2 = 0x00,
  281. .vgate_misc = 0x1c,
  282. },{
  283. .name = "PAL-M",
  284. .id = V4L2_STD_PAL_M,
  285. NORM_525_60,
  286. .sync_control = 0x59,
  287. .luma_control = 0x40,
  288. .chroma_ctrl1 = 0xb9,
  289. .chroma_gain = 0x2a,
  290. .chroma_ctrl2 = 0x0e,
  291. .vgate_misc = 0x18,
  292. },{
  293. .name = "PAL-Nc",
  294. .id = V4L2_STD_PAL_Nc,
  295. NORM_625_50,
  296. .sync_control = 0x18,
  297. .luma_control = 0x40,
  298. .chroma_ctrl1 = 0xa1,
  299. .chroma_gain = 0x2a,
  300. .chroma_ctrl2 = 0x06,
  301. .vgate_misc = 0x1c,
  302. },{
  303. .name = "PAL-60",
  304. .id = V4L2_STD_PAL_60,
  305. .h_start = 0,
  306. .h_stop = 719,
  307. .video_v_start = 23,
  308. .video_v_stop = 262,
  309. .vbi_v_start_0 = 10,
  310. .vbi_v_stop_0 = 21,
  311. .vbi_v_start_1 = 273,
  312. .src_timing = 7,
  313. .sync_control = 0x18,
  314. .luma_control = 0x40,
  315. .chroma_ctrl1 = 0x81,
  316. .chroma_gain = 0x2a,
  317. .chroma_ctrl2 = 0x06,
  318. .vgate_misc = 0x1c,
  319. }
  320. };
  321. #define TVNORMS ARRAY_SIZE(tvnorms)
  322. static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
  323. {
  324. unsigned int i;
  325. for (i = 0; i < FORMATS; i++)
  326. if (formats[i].fourcc == fourcc)
  327. return formats+i;
  328. return NULL;
  329. }
  330. /* ------------------------------------------------------------------ */
  331. static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
  332. {
  333. video_dbg("set tv norm = %s\n", norm->name);
  334. dev->tvnorm = norm;
  335. /* setup cropping */
  336. dev->crop_bounds.left = norm->h_start;
  337. dev->crop_defrect.left = norm->h_start;
  338. dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
  339. dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
  340. dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
  341. dev->crop_defrect.top = norm->video_v_start*2;
  342. dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
  343. - dev->crop_bounds.top;
  344. dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
  345. dev->crop_current = dev->crop_defrect;
  346. saa7134_set_tvnorm_hw(dev);
  347. }
  348. static void video_mux(struct saa7134_dev *dev, int input)
  349. {
  350. video_dbg("video input = %d [%s]\n",
  351. input, saa7134_input_name[card_in(dev, input).type]);
  352. dev->ctl_input = input;
  353. set_tvnorm(dev, dev->tvnorm);
  354. saa7134_tvaudio_setinput(dev, &card_in(dev, input));
  355. }
  356. static void saa7134_set_decoder(struct saa7134_dev *dev)
  357. {
  358. int luma_control, sync_control, chroma_ctrl1, mux;
  359. struct saa7134_tvnorm *norm = dev->tvnorm;
  360. mux = card_in(dev, dev->ctl_input).vmux;
  361. luma_control = norm->luma_control;
  362. sync_control = norm->sync_control;
  363. chroma_ctrl1 = norm->chroma_ctrl1;
  364. if (mux > 5)
  365. luma_control |= 0x80; /* svideo */
  366. if (noninterlaced || dev->nosignal)
  367. sync_control |= 0x20;
  368. /* switch on auto standard detection */
  369. sync_control |= SAA7134_SYNC_CTRL_AUFD;
  370. chroma_ctrl1 |= SAA7134_CHROMA_CTRL1_AUTO0;
  371. chroma_ctrl1 &= ~SAA7134_CHROMA_CTRL1_FCTC;
  372. luma_control &= ~SAA7134_LUMA_CTRL_LDEL;
  373. /* setup video decoder */
  374. saa_writeb(SAA7134_INCR_DELAY, 0x08);
  375. saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
  376. saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
  377. saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
  378. saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
  379. saa_writeb(SAA7134_HSYNC_START, 0xeb);
  380. saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
  381. saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
  382. saa_writeb(SAA7134_SYNC_CTRL, sync_control);
  383. saa_writeb(SAA7134_LUMA_CTRL, luma_control);
  384. saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
  385. saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
  386. dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
  387. saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
  388. dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
  389. saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
  390. saa_writeb(SAA7134_CHROMA_CTRL1, chroma_ctrl1);
  391. saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
  392. saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
  393. saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
  394. saa_writeb(SAA7134_ANALOG_ADC, 0x01);
  395. saa_writeb(SAA7134_VGATE_START, 0x11);
  396. saa_writeb(SAA7134_VGATE_STOP, 0xfe);
  397. saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
  398. saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
  399. saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
  400. }
  401. void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
  402. {
  403. saa7134_set_decoder(dev);
  404. saa_call_all(dev, video, s_std, dev->tvnorm->id);
  405. /* Set the correct norm for the saa6752hs. This function
  406. does nothing if there is no saa6752hs. */
  407. saa_call_empress(dev, video, s_std, dev->tvnorm->id);
  408. }
  409. static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
  410. {
  411. static const struct {
  412. int xpsc;
  413. int xacl;
  414. int xc2_1;
  415. int xdcg;
  416. int vpfy;
  417. } vals[] = {
  418. /* XPSC XACL XC2_1 XDCG VPFY */
  419. { 1, 0, 0, 0, 0 },
  420. { 2, 2, 1, 2, 2 },
  421. { 3, 4, 1, 3, 2 },
  422. { 4, 8, 1, 4, 2 },
  423. { 5, 8, 1, 4, 2 },
  424. { 6, 8, 1, 4, 3 },
  425. { 7, 8, 1, 4, 3 },
  426. { 8, 15, 0, 4, 3 },
  427. { 9, 15, 0, 4, 3 },
  428. { 10, 16, 1, 5, 3 },
  429. };
  430. static const int count = ARRAY_SIZE(vals);
  431. int i;
  432. for (i = 0; i < count; i++)
  433. if (vals[i].xpsc == prescale)
  434. break;
  435. if (i == count)
  436. return;
  437. saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
  438. saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
  439. saa_writeb(SAA7134_LEVEL_CTRL(task),
  440. (vals[i].xc2_1 << 3) | (vals[i].xdcg));
  441. saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
  442. (vals[i].vpfy << 2) | vals[i].vpfy);
  443. }
  444. static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
  445. {
  446. int val,mirror;
  447. saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
  448. saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
  449. mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
  450. if (yscale < 2048) {
  451. /* LPI */
  452. video_dbg("yscale LPI yscale=%d\n", yscale);
  453. saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
  454. saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
  455. saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
  456. } else {
  457. /* ACM */
  458. val = 0x40 * 1024 / yscale;
  459. video_dbg("yscale ACM yscale=%d val=0x%x\n", yscale, val);
  460. saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
  461. saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
  462. saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
  463. }
  464. saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
  465. }
  466. static void set_size(struct saa7134_dev *dev, int task,
  467. int width, int height, int interlace)
  468. {
  469. int prescale,xscale,yscale,y_even,y_odd;
  470. int h_start, h_stop, v_start, v_stop;
  471. int div = interlace ? 2 : 1;
  472. /* setup video scaler */
  473. h_start = dev->crop_current.left;
  474. v_start = dev->crop_current.top/2;
  475. h_stop = (dev->crop_current.left + dev->crop_current.width -1);
  476. v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
  477. saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
  478. saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
  479. saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
  480. saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
  481. saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
  482. saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
  483. saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
  484. saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
  485. prescale = dev->crop_current.width / width;
  486. if (0 == prescale)
  487. prescale = 1;
  488. xscale = 1024 * dev->crop_current.width / prescale / width;
  489. yscale = 512 * div * dev->crop_current.height / height;
  490. video_dbg("prescale=%d xscale=%d yscale=%d\n",
  491. prescale, xscale, yscale);
  492. set_h_prescale(dev,task,prescale);
  493. saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
  494. saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
  495. set_v_scale(dev,task,yscale);
  496. saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
  497. saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
  498. saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
  499. saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
  500. /* deinterlace y offsets */
  501. y_odd = dev->ctl_y_odd;
  502. y_even = dev->ctl_y_even;
  503. saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
  504. saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
  505. saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
  506. saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
  507. }
  508. /* ------------------------------------------------------------------ */
  509. struct cliplist {
  510. __u16 position;
  511. __u8 enable;
  512. __u8 disable;
  513. };
  514. static void set_cliplist(struct saa7134_dev *dev, int reg,
  515. struct cliplist *cl, int entries, char *name)
  516. {
  517. __u8 winbits = 0;
  518. int i;
  519. for (i = 0; i < entries; i++) {
  520. winbits |= cl[i].enable;
  521. winbits &= ~cl[i].disable;
  522. if (i < 15 && cl[i].position == cl[i+1].position)
  523. continue;
  524. saa_writeb(reg + 0, winbits);
  525. saa_writeb(reg + 2, cl[i].position & 0xff);
  526. saa_writeb(reg + 3, cl[i].position >> 8);
  527. video_dbg("clip: %s winbits=%02x pos=%d\n",
  528. name,winbits,cl[i].position);
  529. reg += 8;
  530. }
  531. for (; reg < 0x400; reg += 8) {
  532. saa_writeb(reg+ 0, 0);
  533. saa_writeb(reg + 1, 0);
  534. saa_writeb(reg + 2, 0);
  535. saa_writeb(reg + 3, 0);
  536. }
  537. }
  538. static int clip_range(int val)
  539. {
  540. if (val < 0)
  541. val = 0;
  542. return val;
  543. }
  544. /* Sort into smallest position first order */
  545. static int cliplist_cmp(const void *a, const void *b)
  546. {
  547. const struct cliplist *cla = a;
  548. const struct cliplist *clb = b;
  549. if (cla->position < clb->position)
  550. return -1;
  551. if (cla->position > clb->position)
  552. return 1;
  553. return 0;
  554. }
  555. static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
  556. int nclips, int interlace)
  557. {
  558. struct cliplist col[16], row[16];
  559. int cols = 0, rows = 0, i;
  560. int div = interlace ? 2 : 1;
  561. memset(col, 0, sizeof(col));
  562. memset(row, 0, sizeof(row));
  563. for (i = 0; i < nclips && i < 8; i++) {
  564. col[cols].position = clip_range(clips[i].c.left);
  565. col[cols].enable = (1 << i);
  566. cols++;
  567. col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
  568. col[cols].disable = (1 << i);
  569. cols++;
  570. row[rows].position = clip_range(clips[i].c.top / div);
  571. row[rows].enable = (1 << i);
  572. rows++;
  573. row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
  574. / div);
  575. row[rows].disable = (1 << i);
  576. rows++;
  577. }
  578. sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
  579. sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
  580. set_cliplist(dev,0x380,col,cols,"cols");
  581. set_cliplist(dev,0x384,row,rows,"rows");
  582. return 0;
  583. }
  584. static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win, bool try)
  585. {
  586. enum v4l2_field field;
  587. int maxw, maxh;
  588. if (!try && (dev->ovbuf.base == NULL || dev->ovfmt == NULL))
  589. return -EINVAL;
  590. if (win->w.width < 48)
  591. win->w.width = 48;
  592. if (win->w.height < 32)
  593. win->w.height = 32;
  594. if (win->clipcount > 8)
  595. win->clipcount = 8;
  596. win->chromakey = 0;
  597. win->global_alpha = 0;
  598. field = win->field;
  599. maxw = dev->crop_current.width;
  600. maxh = dev->crop_current.height;
  601. if (V4L2_FIELD_ANY == field) {
  602. field = (win->w.height > maxh/2)
  603. ? V4L2_FIELD_INTERLACED
  604. : V4L2_FIELD_TOP;
  605. }
  606. switch (field) {
  607. case V4L2_FIELD_TOP:
  608. case V4L2_FIELD_BOTTOM:
  609. maxh = maxh / 2;
  610. break;
  611. default:
  612. field = V4L2_FIELD_INTERLACED;
  613. break;
  614. }
  615. win->field = field;
  616. if (win->w.width > maxw)
  617. win->w.width = maxw;
  618. if (win->w.height > maxh)
  619. win->w.height = maxh;
  620. return 0;
  621. }
  622. static int start_preview(struct saa7134_dev *dev)
  623. {
  624. unsigned long base,control,bpl;
  625. int err;
  626. err = verify_preview(dev, &dev->win, false);
  627. if (0 != err)
  628. return err;
  629. dev->ovfield = dev->win.field;
  630. video_dbg("start_preview %dx%d+%d+%d %s field=%s\n",
  631. dev->win.w.width, dev->win.w.height,
  632. dev->win.w.left, dev->win.w.top,
  633. dev->ovfmt->name, v4l2_field_names[dev->ovfield]);
  634. /* setup window + clipping */
  635. set_size(dev, TASK_B, dev->win.w.width, dev->win.w.height,
  636. V4L2_FIELD_HAS_BOTH(dev->ovfield));
  637. setup_clipping(dev, dev->clips, dev->nclips,
  638. V4L2_FIELD_HAS_BOTH(dev->ovfield));
  639. if (dev->ovfmt->yuv)
  640. saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
  641. else
  642. saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
  643. saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
  644. /* dma: setup channel 1 (= Video Task B) */
  645. base = (unsigned long)dev->ovbuf.base;
  646. base += dev->ovbuf.fmt.bytesperline * dev->win.w.top;
  647. base += dev->ovfmt->depth/8 * dev->win.w.left;
  648. bpl = dev->ovbuf.fmt.bytesperline;
  649. control = SAA7134_RS_CONTROL_BURST_16;
  650. if (dev->ovfmt->bswap)
  651. control |= SAA7134_RS_CONTROL_BSWAP;
  652. if (dev->ovfmt->wswap)
  653. control |= SAA7134_RS_CONTROL_WSWAP;
  654. if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
  655. saa_writel(SAA7134_RS_BA1(1),base);
  656. saa_writel(SAA7134_RS_BA2(1),base+bpl);
  657. saa_writel(SAA7134_RS_PITCH(1),bpl*2);
  658. saa_writel(SAA7134_RS_CONTROL(1),control);
  659. } else {
  660. saa_writel(SAA7134_RS_BA1(1),base);
  661. saa_writel(SAA7134_RS_BA2(1),base);
  662. saa_writel(SAA7134_RS_PITCH(1),bpl);
  663. saa_writel(SAA7134_RS_CONTROL(1),control);
  664. }
  665. /* start dma */
  666. dev->ovenable = 1;
  667. saa7134_set_dmabits(dev);
  668. return 0;
  669. }
  670. static int stop_preview(struct saa7134_dev *dev)
  671. {
  672. dev->ovenable = 0;
  673. saa7134_set_dmabits(dev);
  674. return 0;
  675. }
  676. /*
  677. * Media Controller helper functions
  678. */
  679. static int saa7134_enable_analog_tuner(struct saa7134_dev *dev)
  680. {
  681. #ifdef CONFIG_MEDIA_CONTROLLER
  682. struct media_device *mdev = dev->media_dev;
  683. struct media_entity *source;
  684. struct media_link *link, *found_link = NULL;
  685. int ret, active_links = 0;
  686. if (!mdev || !dev->decoder)
  687. return 0;
  688. /*
  689. * This will find the tuner that is connected into the decoder.
  690. * Technically, this is not 100% correct, as the device may be
  691. * using an analog input instead of the tuner. However, as we can't
  692. * do DVB streaming while the DMA engine is being used for V4L2,
  693. * this should be enough for the actual needs.
  694. */
  695. list_for_each_entry(link, &dev->decoder->links, list) {
  696. if (link->sink->entity == dev->decoder) {
  697. found_link = link;
  698. if (link->flags & MEDIA_LNK_FL_ENABLED)
  699. active_links++;
  700. break;
  701. }
  702. }
  703. if (active_links == 1 || !found_link)
  704. return 0;
  705. source = found_link->source->entity;
  706. list_for_each_entry(link, &source->links, list) {
  707. struct media_entity *sink;
  708. int flags = 0;
  709. sink = link->sink->entity;
  710. if (sink == dev->decoder)
  711. flags = MEDIA_LNK_FL_ENABLED;
  712. ret = media_entity_setup_link(link, flags);
  713. if (ret) {
  714. pr_err("Couldn't change link %s->%s to %s. Error %d\n",
  715. source->name, sink->name,
  716. flags ? "enabled" : "disabled",
  717. ret);
  718. return ret;
  719. }
  720. }
  721. #endif
  722. return 0;
  723. }
  724. /* ------------------------------------------------------------------ */
  725. static int buffer_activate(struct saa7134_dev *dev,
  726. struct saa7134_buf *buf,
  727. struct saa7134_buf *next)
  728. {
  729. struct saa7134_dmaqueue *dmaq = buf->vb2.vb2_buf.vb2_queue->drv_priv;
  730. unsigned long base,control,bpl;
  731. unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
  732. video_dbg("buffer_activate buf=%p\n", buf);
  733. buf->top_seen = 0;
  734. set_size(dev, TASK_A, dev->width, dev->height,
  735. V4L2_FIELD_HAS_BOTH(dev->field));
  736. if (dev->fmt->yuv)
  737. saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
  738. else
  739. saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
  740. saa_writeb(SAA7134_OFMT_VIDEO_A, dev->fmt->pm);
  741. /* DMA: setup channel 0 (= Video Task A0) */
  742. base = saa7134_buffer_base(buf);
  743. if (dev->fmt->planar)
  744. bpl = dev->width;
  745. else
  746. bpl = (dev->width * dev->fmt->depth) / 8;
  747. control = SAA7134_RS_CONTROL_BURST_16 |
  748. SAA7134_RS_CONTROL_ME |
  749. (dmaq->pt.dma >> 12);
  750. if (dev->fmt->bswap)
  751. control |= SAA7134_RS_CONTROL_BSWAP;
  752. if (dev->fmt->wswap)
  753. control |= SAA7134_RS_CONTROL_WSWAP;
  754. if (V4L2_FIELD_HAS_BOTH(dev->field)) {
  755. /* interlaced */
  756. saa_writel(SAA7134_RS_BA1(0),base);
  757. saa_writel(SAA7134_RS_BA2(0),base+bpl);
  758. saa_writel(SAA7134_RS_PITCH(0),bpl*2);
  759. } else {
  760. /* non-interlaced */
  761. saa_writel(SAA7134_RS_BA1(0),base);
  762. saa_writel(SAA7134_RS_BA2(0),base);
  763. saa_writel(SAA7134_RS_PITCH(0),bpl);
  764. }
  765. saa_writel(SAA7134_RS_CONTROL(0),control);
  766. if (dev->fmt->planar) {
  767. /* DMA: setup channel 4+5 (= planar task A) */
  768. bpl_uv = bpl >> dev->fmt->hshift;
  769. lines_uv = dev->height >> dev->fmt->vshift;
  770. base2 = base + bpl * dev->height;
  771. base3 = base2 + bpl_uv * lines_uv;
  772. if (dev->fmt->uvswap)
  773. tmp = base2, base2 = base3, base3 = tmp;
  774. video_dbg("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
  775. bpl_uv,lines_uv,base2,base3);
  776. if (V4L2_FIELD_HAS_BOTH(dev->field)) {
  777. /* interlaced */
  778. saa_writel(SAA7134_RS_BA1(4),base2);
  779. saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
  780. saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
  781. saa_writel(SAA7134_RS_BA1(5),base3);
  782. saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
  783. saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
  784. } else {
  785. /* non-interlaced */
  786. saa_writel(SAA7134_RS_BA1(4),base2);
  787. saa_writel(SAA7134_RS_BA2(4),base2);
  788. saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
  789. saa_writel(SAA7134_RS_BA1(5),base3);
  790. saa_writel(SAA7134_RS_BA2(5),base3);
  791. saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
  792. }
  793. saa_writel(SAA7134_RS_CONTROL(4),control);
  794. saa_writel(SAA7134_RS_CONTROL(5),control);
  795. }
  796. /* start DMA */
  797. saa7134_set_dmabits(dev);
  798. mod_timer(&dmaq->timeout, jiffies + BUFFER_TIMEOUT);
  799. return 0;
  800. }
  801. static int buffer_init(struct vb2_buffer *vb2)
  802. {
  803. struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
  804. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
  805. struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
  806. dmaq->curr = NULL;
  807. buf->activate = buffer_activate;
  808. return 0;
  809. }
  810. static int buffer_prepare(struct vb2_buffer *vb2)
  811. {
  812. struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
  813. struct saa7134_dev *dev = dmaq->dev;
  814. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
  815. struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
  816. struct sg_table *dma = vb2_dma_sg_plane_desc(vb2, 0);
  817. unsigned int size;
  818. if (dma->sgl->offset) {
  819. pr_err("The buffer is not page-aligned\n");
  820. return -EINVAL;
  821. }
  822. size = (dev->width * dev->height * dev->fmt->depth) >> 3;
  823. if (vb2_plane_size(vb2, 0) < size)
  824. return -EINVAL;
  825. vb2_set_plane_payload(vb2, 0, size);
  826. vbuf->field = dev->field;
  827. return saa7134_pgtable_build(dev->pci, &dmaq->pt, dma->sgl, dma->nents,
  828. saa7134_buffer_startpage(buf));
  829. }
  830. static int queue_setup(struct vb2_queue *q,
  831. unsigned int *nbuffers, unsigned int *nplanes,
  832. unsigned int sizes[], struct device *alloc_devs[])
  833. {
  834. struct saa7134_dmaqueue *dmaq = q->drv_priv;
  835. struct saa7134_dev *dev = dmaq->dev;
  836. int size = dev->fmt->depth * dev->width * dev->height >> 3;
  837. if (dev->width < 48 ||
  838. dev->height < 32 ||
  839. dev->width/4 > dev->crop_current.width ||
  840. dev->height/4 > dev->crop_current.height ||
  841. dev->width > dev->crop_bounds.width ||
  842. dev->height > dev->crop_bounds.height)
  843. return -EINVAL;
  844. *nbuffers = saa7134_buffer_count(size, *nbuffers);
  845. *nplanes = 1;
  846. sizes[0] = size;
  847. saa7134_enable_analog_tuner(dev);
  848. return 0;
  849. }
  850. /*
  851. * move buffer to hardware queue
  852. */
  853. void saa7134_vb2_buffer_queue(struct vb2_buffer *vb)
  854. {
  855. struct saa7134_dmaqueue *dmaq = vb->vb2_queue->drv_priv;
  856. struct saa7134_dev *dev = dmaq->dev;
  857. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  858. struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
  859. saa7134_buffer_queue(dev, dmaq, buf);
  860. }
  861. EXPORT_SYMBOL_GPL(saa7134_vb2_buffer_queue);
  862. int saa7134_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
  863. {
  864. struct saa7134_dmaqueue *dmaq = vq->drv_priv;
  865. struct saa7134_dev *dev = dmaq->dev;
  866. /*
  867. * Planar video capture and TS share the same DMA channel,
  868. * so only one can be active at a time.
  869. */
  870. if (card_is_empress(dev) && vb2_is_busy(&dev->empress_vbq) &&
  871. dmaq == &dev->video_q && dev->fmt->planar) {
  872. struct saa7134_buf *buf, *tmp;
  873. list_for_each_entry_safe(buf, tmp, &dmaq->queue, entry) {
  874. list_del(&buf->entry);
  875. vb2_buffer_done(&buf->vb2.vb2_buf,
  876. VB2_BUF_STATE_QUEUED);
  877. }
  878. if (dmaq->curr) {
  879. vb2_buffer_done(&dmaq->curr->vb2.vb2_buf,
  880. VB2_BUF_STATE_QUEUED);
  881. dmaq->curr = NULL;
  882. }
  883. return -EBUSY;
  884. }
  885. /* The SAA7134 has a 1K FIFO; the datasheet suggests that when
  886. * configured conservatively, there's 22 usec of buffering for video.
  887. * We therefore request a DMA latency of 20 usec, giving us 2 usec of
  888. * margin in case the FIFO is configured differently to the datasheet.
  889. * Unfortunately, I lack register-level documentation to check the
  890. * Linux FIFO setup and confirm the perfect value.
  891. */
  892. if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
  893. (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
  894. pm_qos_add_request(&dev->qos_request,
  895. PM_QOS_CPU_DMA_LATENCY, 20);
  896. dmaq->seq_nr = 0;
  897. return 0;
  898. }
  899. void saa7134_vb2_stop_streaming(struct vb2_queue *vq)
  900. {
  901. struct saa7134_dmaqueue *dmaq = vq->drv_priv;
  902. struct saa7134_dev *dev = dmaq->dev;
  903. saa7134_stop_streaming(dev, dmaq);
  904. if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
  905. (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
  906. pm_qos_remove_request(&dev->qos_request);
  907. }
  908. static const struct vb2_ops vb2_qops = {
  909. .queue_setup = queue_setup,
  910. .buf_init = buffer_init,
  911. .buf_prepare = buffer_prepare,
  912. .buf_queue = saa7134_vb2_buffer_queue,
  913. .wait_prepare = vb2_ops_wait_prepare,
  914. .wait_finish = vb2_ops_wait_finish,
  915. .start_streaming = saa7134_vb2_start_streaming,
  916. .stop_streaming = saa7134_vb2_stop_streaming,
  917. };
  918. /* ------------------------------------------------------------------ */
  919. static int saa7134_s_ctrl(struct v4l2_ctrl *ctrl)
  920. {
  921. struct saa7134_dev *dev = container_of(ctrl->handler, struct saa7134_dev, ctrl_handler);
  922. unsigned long flags;
  923. int restart_overlay = 0;
  924. switch (ctrl->id) {
  925. case V4L2_CID_BRIGHTNESS:
  926. dev->ctl_bright = ctrl->val;
  927. saa_writeb(SAA7134_DEC_LUMA_BRIGHT, ctrl->val);
  928. break;
  929. case V4L2_CID_HUE:
  930. dev->ctl_hue = ctrl->val;
  931. saa_writeb(SAA7134_DEC_CHROMA_HUE, ctrl->val);
  932. break;
  933. case V4L2_CID_CONTRAST:
  934. dev->ctl_contrast = ctrl->val;
  935. saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
  936. dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
  937. break;
  938. case V4L2_CID_SATURATION:
  939. dev->ctl_saturation = ctrl->val;
  940. saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
  941. dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
  942. break;
  943. case V4L2_CID_AUDIO_MUTE:
  944. dev->ctl_mute = ctrl->val;
  945. saa7134_tvaudio_setmute(dev);
  946. break;
  947. case V4L2_CID_AUDIO_VOLUME:
  948. dev->ctl_volume = ctrl->val;
  949. saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
  950. break;
  951. case V4L2_CID_PRIVATE_INVERT:
  952. dev->ctl_invert = ctrl->val;
  953. saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
  954. dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
  955. saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
  956. dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
  957. break;
  958. case V4L2_CID_HFLIP:
  959. dev->ctl_mirror = ctrl->val;
  960. restart_overlay = 1;
  961. break;
  962. case V4L2_CID_PRIVATE_Y_EVEN:
  963. dev->ctl_y_even = ctrl->val;
  964. restart_overlay = 1;
  965. break;
  966. case V4L2_CID_PRIVATE_Y_ODD:
  967. dev->ctl_y_odd = ctrl->val;
  968. restart_overlay = 1;
  969. break;
  970. case V4L2_CID_PRIVATE_AUTOMUTE:
  971. {
  972. struct v4l2_priv_tun_config tda9887_cfg;
  973. tda9887_cfg.tuner = TUNER_TDA9887;
  974. tda9887_cfg.priv = &dev->tda9887_conf;
  975. dev->ctl_automute = ctrl->val;
  976. if (dev->tda9887_conf) {
  977. if (dev->ctl_automute)
  978. dev->tda9887_conf |= TDA9887_AUTOMUTE;
  979. else
  980. dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
  981. saa_call_all(dev, tuner, s_config, &tda9887_cfg);
  982. }
  983. break;
  984. }
  985. default:
  986. return -EINVAL;
  987. }
  988. if (restart_overlay && dev->overlay_owner) {
  989. spin_lock_irqsave(&dev->slock, flags);
  990. stop_preview(dev);
  991. start_preview(dev);
  992. spin_unlock_irqrestore(&dev->slock, flags);
  993. }
  994. return 0;
  995. }
  996. /* ------------------------------------------------------------------ */
  997. static int video_open(struct file *file)
  998. {
  999. struct video_device *vdev = video_devdata(file);
  1000. struct saa7134_dev *dev = video_drvdata(file);
  1001. int ret = v4l2_fh_open(file);
  1002. if (ret < 0)
  1003. return ret;
  1004. mutex_lock(&dev->lock);
  1005. if (vdev->vfl_type == VFL_TYPE_RADIO) {
  1006. /* switch to radio mode */
  1007. saa7134_tvaudio_setinput(dev, &card(dev).radio);
  1008. saa_call_all(dev, tuner, s_radio);
  1009. } else {
  1010. /* switch to video/vbi mode */
  1011. video_mux(dev, dev->ctl_input);
  1012. }
  1013. mutex_unlock(&dev->lock);
  1014. return 0;
  1015. }
  1016. static int video_release(struct file *file)
  1017. {
  1018. struct video_device *vdev = video_devdata(file);
  1019. struct saa7134_dev *dev = video_drvdata(file);
  1020. struct v4l2_fh *fh = file->private_data;
  1021. struct saa6588_command cmd;
  1022. unsigned long flags;
  1023. mutex_lock(&dev->lock);
  1024. saa7134_tvaudio_close(dev);
  1025. /* turn off overlay */
  1026. if (fh == dev->overlay_owner) {
  1027. spin_lock_irqsave(&dev->slock,flags);
  1028. stop_preview(dev);
  1029. spin_unlock_irqrestore(&dev->slock,flags);
  1030. dev->overlay_owner = NULL;
  1031. }
  1032. if (vdev->vfl_type == VFL_TYPE_RADIO)
  1033. v4l2_fh_release(file);
  1034. else
  1035. _vb2_fop_release(file, NULL);
  1036. /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
  1037. saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
  1038. saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
  1039. saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
  1040. saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
  1041. saa_call_all(dev, tuner, standby);
  1042. if (vdev->vfl_type == VFL_TYPE_RADIO)
  1043. saa_call_all(dev, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
  1044. mutex_unlock(&dev->lock);
  1045. return 0;
  1046. }
  1047. static ssize_t radio_read(struct file *file, char __user *data,
  1048. size_t count, loff_t *ppos)
  1049. {
  1050. struct saa7134_dev *dev = video_drvdata(file);
  1051. struct saa6588_command cmd;
  1052. cmd.block_count = count/3;
  1053. cmd.nonblocking = file->f_flags & O_NONBLOCK;
  1054. cmd.buffer = data;
  1055. cmd.instance = file;
  1056. cmd.result = -ENODEV;
  1057. mutex_lock(&dev->lock);
  1058. saa_call_all(dev, core, ioctl, SAA6588_CMD_READ, &cmd);
  1059. mutex_unlock(&dev->lock);
  1060. return cmd.result;
  1061. }
  1062. static __poll_t radio_poll(struct file *file, poll_table *wait)
  1063. {
  1064. struct saa7134_dev *dev = video_drvdata(file);
  1065. struct saa6588_command cmd;
  1066. __poll_t rc = v4l2_ctrl_poll(file, wait);
  1067. cmd.instance = file;
  1068. cmd.event_list = wait;
  1069. cmd.poll_mask = 0;
  1070. mutex_lock(&dev->lock);
  1071. saa_call_all(dev, core, ioctl, SAA6588_CMD_POLL, &cmd);
  1072. mutex_unlock(&dev->lock);
  1073. return rc | cmd.poll_mask;
  1074. }
  1075. /* ------------------------------------------------------------------ */
  1076. static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
  1077. struct v4l2_format *f)
  1078. {
  1079. struct saa7134_dev *dev = video_drvdata(file);
  1080. struct saa7134_tvnorm *norm = dev->tvnorm;
  1081. memset(&f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
  1082. f->fmt.vbi.sampling_rate = 6750000 * 4;
  1083. f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
  1084. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1085. f->fmt.vbi.offset = 64 * 4;
  1086. f->fmt.vbi.start[0] = norm->vbi_v_start_0;
  1087. f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
  1088. f->fmt.vbi.start[1] = norm->vbi_v_start_1;
  1089. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1090. f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
  1091. return 0;
  1092. }
  1093. static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
  1094. struct v4l2_format *f)
  1095. {
  1096. struct saa7134_dev *dev = video_drvdata(file);
  1097. f->fmt.pix.width = dev->width;
  1098. f->fmt.pix.height = dev->height;
  1099. f->fmt.pix.field = dev->field;
  1100. f->fmt.pix.pixelformat = dev->fmt->fourcc;
  1101. if (dev->fmt->planar)
  1102. f->fmt.pix.bytesperline = f->fmt.pix.width;
  1103. else
  1104. f->fmt.pix.bytesperline =
  1105. (f->fmt.pix.width * dev->fmt->depth) / 8;
  1106. f->fmt.pix.sizeimage =
  1107. (f->fmt.pix.height * f->fmt.pix.width * dev->fmt->depth) / 8;
  1108. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1109. return 0;
  1110. }
  1111. static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
  1112. struct v4l2_format *f)
  1113. {
  1114. struct saa7134_dev *dev = video_drvdata(file);
  1115. struct v4l2_clip __user *clips = f->fmt.win.clips;
  1116. u32 clipcount = f->fmt.win.clipcount;
  1117. int err = 0;
  1118. int i;
  1119. if (saa7134_no_overlay > 0) {
  1120. pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  1121. return -EINVAL;
  1122. }
  1123. f->fmt.win = dev->win;
  1124. f->fmt.win.clips = clips;
  1125. if (clips == NULL)
  1126. clipcount = 0;
  1127. if (dev->nclips < clipcount)
  1128. clipcount = dev->nclips;
  1129. f->fmt.win.clipcount = clipcount;
  1130. for (i = 0; !err && i < clipcount; i++) {
  1131. if (copy_to_user(&f->fmt.win.clips[i].c, &dev->clips[i].c,
  1132. sizeof(struct v4l2_rect)))
  1133. err = -EFAULT;
  1134. }
  1135. return err;
  1136. }
  1137. static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
  1138. struct v4l2_format *f)
  1139. {
  1140. struct saa7134_dev *dev = video_drvdata(file);
  1141. struct saa7134_format *fmt;
  1142. enum v4l2_field field;
  1143. unsigned int maxw, maxh;
  1144. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  1145. if (NULL == fmt)
  1146. return -EINVAL;
  1147. field = f->fmt.pix.field;
  1148. maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
  1149. maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
  1150. if (V4L2_FIELD_ANY == field) {
  1151. field = (f->fmt.pix.height > maxh/2)
  1152. ? V4L2_FIELD_INTERLACED
  1153. : V4L2_FIELD_BOTTOM;
  1154. }
  1155. switch (field) {
  1156. case V4L2_FIELD_TOP:
  1157. case V4L2_FIELD_BOTTOM:
  1158. maxh = maxh / 2;
  1159. break;
  1160. default:
  1161. field = V4L2_FIELD_INTERLACED;
  1162. break;
  1163. }
  1164. f->fmt.pix.field = field;
  1165. if (f->fmt.pix.width < 48)
  1166. f->fmt.pix.width = 48;
  1167. if (f->fmt.pix.height < 32)
  1168. f->fmt.pix.height = 32;
  1169. if (f->fmt.pix.width > maxw)
  1170. f->fmt.pix.width = maxw;
  1171. if (f->fmt.pix.height > maxh)
  1172. f->fmt.pix.height = maxh;
  1173. f->fmt.pix.width &= ~0x03;
  1174. if (fmt->planar)
  1175. f->fmt.pix.bytesperline = f->fmt.pix.width;
  1176. else
  1177. f->fmt.pix.bytesperline =
  1178. (f->fmt.pix.width * fmt->depth) / 8;
  1179. f->fmt.pix.sizeimage =
  1180. (f->fmt.pix.height * f->fmt.pix.width * fmt->depth) / 8;
  1181. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1182. return 0;
  1183. }
  1184. static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
  1185. struct v4l2_format *f)
  1186. {
  1187. struct saa7134_dev *dev = video_drvdata(file);
  1188. if (saa7134_no_overlay > 0) {
  1189. pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  1190. return -EINVAL;
  1191. }
  1192. if (f->fmt.win.clips == NULL)
  1193. f->fmt.win.clipcount = 0;
  1194. return verify_preview(dev, &f->fmt.win, true);
  1195. }
  1196. static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
  1197. struct v4l2_format *f)
  1198. {
  1199. struct saa7134_dev *dev = video_drvdata(file);
  1200. int err;
  1201. err = saa7134_try_fmt_vid_cap(file, priv, f);
  1202. if (0 != err)
  1203. return err;
  1204. dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  1205. dev->width = f->fmt.pix.width;
  1206. dev->height = f->fmt.pix.height;
  1207. dev->field = f->fmt.pix.field;
  1208. return 0;
  1209. }
  1210. static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
  1211. struct v4l2_format *f)
  1212. {
  1213. struct saa7134_dev *dev = video_drvdata(file);
  1214. int err;
  1215. unsigned long flags;
  1216. if (saa7134_no_overlay > 0) {
  1217. pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  1218. return -EINVAL;
  1219. }
  1220. if (f->fmt.win.clips == NULL)
  1221. f->fmt.win.clipcount = 0;
  1222. err = verify_preview(dev, &f->fmt.win, true);
  1223. if (0 != err)
  1224. return err;
  1225. dev->win = f->fmt.win;
  1226. dev->nclips = f->fmt.win.clipcount;
  1227. if (copy_from_user(dev->clips, f->fmt.win.clips,
  1228. sizeof(struct v4l2_clip) * dev->nclips))
  1229. return -EFAULT;
  1230. if (priv == dev->overlay_owner) {
  1231. spin_lock_irqsave(&dev->slock, flags);
  1232. stop_preview(dev);
  1233. start_preview(dev);
  1234. spin_unlock_irqrestore(&dev->slock, flags);
  1235. }
  1236. return 0;
  1237. }
  1238. int saa7134_enum_input(struct file *file, void *priv, struct v4l2_input *i)
  1239. {
  1240. struct saa7134_dev *dev = video_drvdata(file);
  1241. unsigned int n;
  1242. n = i->index;
  1243. if (n >= SAA7134_INPUT_MAX)
  1244. return -EINVAL;
  1245. if (card_in(dev, i->index).type == SAA7134_NO_INPUT)
  1246. return -EINVAL;
  1247. i->index = n;
  1248. strcpy(i->name, saa7134_input_name[card_in(dev, n).type]);
  1249. switch (card_in(dev, n).type) {
  1250. case SAA7134_INPUT_TV:
  1251. case SAA7134_INPUT_TV_MONO:
  1252. i->type = V4L2_INPUT_TYPE_TUNER;
  1253. break;
  1254. default:
  1255. i->type = V4L2_INPUT_TYPE_CAMERA;
  1256. break;
  1257. }
  1258. if (n == dev->ctl_input) {
  1259. int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
  1260. int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
  1261. if (0 != (v1 & 0x40))
  1262. i->status |= V4L2_IN_ST_NO_H_LOCK;
  1263. if (0 != (v2 & 0x40))
  1264. i->status |= V4L2_IN_ST_NO_SIGNAL;
  1265. if (0 != (v2 & 0x0e))
  1266. i->status |= V4L2_IN_ST_MACROVISION;
  1267. }
  1268. i->std = SAA7134_NORMS;
  1269. return 0;
  1270. }
  1271. EXPORT_SYMBOL_GPL(saa7134_enum_input);
  1272. int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
  1273. {
  1274. struct saa7134_dev *dev = video_drvdata(file);
  1275. *i = dev->ctl_input;
  1276. return 0;
  1277. }
  1278. EXPORT_SYMBOL_GPL(saa7134_g_input);
  1279. int saa7134_s_input(struct file *file, void *priv, unsigned int i)
  1280. {
  1281. struct saa7134_dev *dev = video_drvdata(file);
  1282. if (i >= SAA7134_INPUT_MAX)
  1283. return -EINVAL;
  1284. if (card_in(dev, i).type == SAA7134_NO_INPUT)
  1285. return -EINVAL;
  1286. video_mux(dev, i);
  1287. return 0;
  1288. }
  1289. EXPORT_SYMBOL_GPL(saa7134_s_input);
  1290. int saa7134_querycap(struct file *file, void *priv,
  1291. struct v4l2_capability *cap)
  1292. {
  1293. struct saa7134_dev *dev = video_drvdata(file);
  1294. struct video_device *vdev = video_devdata(file);
  1295. u32 radio_caps, video_caps, vbi_caps;
  1296. unsigned int tuner_type = dev->tuner_type;
  1297. strcpy(cap->driver, "saa7134");
  1298. strlcpy(cap->card, saa7134_boards[dev->board].name,
  1299. sizeof(cap->card));
  1300. sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
  1301. cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1302. if ((tuner_type != TUNER_ABSENT) && (tuner_type != UNSET))
  1303. cap->device_caps |= V4L2_CAP_TUNER;
  1304. radio_caps = V4L2_CAP_RADIO;
  1305. if (dev->has_rds)
  1306. radio_caps |= V4L2_CAP_RDS_CAPTURE;
  1307. video_caps = V4L2_CAP_VIDEO_CAPTURE;
  1308. if (saa7134_no_overlay <= 0 && !is_empress(file))
  1309. video_caps |= V4L2_CAP_VIDEO_OVERLAY;
  1310. vbi_caps = V4L2_CAP_VBI_CAPTURE;
  1311. switch (vdev->vfl_type) {
  1312. case VFL_TYPE_RADIO:
  1313. cap->device_caps |= radio_caps;
  1314. break;
  1315. case VFL_TYPE_GRABBER:
  1316. cap->device_caps |= video_caps;
  1317. break;
  1318. case VFL_TYPE_VBI:
  1319. cap->device_caps |= vbi_caps;
  1320. break;
  1321. default:
  1322. return -EINVAL;
  1323. }
  1324. cap->capabilities = radio_caps | video_caps | vbi_caps |
  1325. cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1326. if (vdev->vfl_type == VFL_TYPE_RADIO) {
  1327. cap->device_caps &= ~V4L2_CAP_STREAMING;
  1328. if (!dev->has_rds)
  1329. cap->device_caps &= ~V4L2_CAP_READWRITE;
  1330. }
  1331. return 0;
  1332. }
  1333. EXPORT_SYMBOL_GPL(saa7134_querycap);
  1334. int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id)
  1335. {
  1336. struct saa7134_dev *dev = video_drvdata(file);
  1337. struct v4l2_fh *fh = priv;
  1338. unsigned long flags;
  1339. unsigned int i;
  1340. v4l2_std_id fixup;
  1341. if (is_empress(file) && dev->overlay_owner) {
  1342. /* Don't change the std from the mpeg device
  1343. if overlay is active. */
  1344. return -EBUSY;
  1345. }
  1346. for (i = 0; i < TVNORMS; i++)
  1347. if (id == tvnorms[i].id)
  1348. break;
  1349. if (i == TVNORMS)
  1350. for (i = 0; i < TVNORMS; i++)
  1351. if (id & tvnorms[i].id)
  1352. break;
  1353. if (i == TVNORMS)
  1354. return -EINVAL;
  1355. if ((id & V4L2_STD_SECAM) && (secam[0] != '-')) {
  1356. if (secam[0] == 'L' || secam[0] == 'l') {
  1357. if (secam[1] == 'C' || secam[1] == 'c')
  1358. fixup = V4L2_STD_SECAM_LC;
  1359. else
  1360. fixup = V4L2_STD_SECAM_L;
  1361. } else {
  1362. if (secam[0] == 'D' || secam[0] == 'd')
  1363. fixup = V4L2_STD_SECAM_DK;
  1364. else
  1365. fixup = V4L2_STD_SECAM;
  1366. }
  1367. for (i = 0; i < TVNORMS; i++) {
  1368. if (fixup == tvnorms[i].id)
  1369. break;
  1370. }
  1371. if (i == TVNORMS)
  1372. return -EINVAL;
  1373. }
  1374. id = tvnorms[i].id;
  1375. if (!is_empress(file) && fh == dev->overlay_owner) {
  1376. spin_lock_irqsave(&dev->slock, flags);
  1377. stop_preview(dev);
  1378. spin_unlock_irqrestore(&dev->slock, flags);
  1379. set_tvnorm(dev, &tvnorms[i]);
  1380. spin_lock_irqsave(&dev->slock, flags);
  1381. start_preview(dev);
  1382. spin_unlock_irqrestore(&dev->slock, flags);
  1383. } else
  1384. set_tvnorm(dev, &tvnorms[i]);
  1385. saa7134_tvaudio_do_scan(dev);
  1386. return 0;
  1387. }
  1388. EXPORT_SYMBOL_GPL(saa7134_s_std);
  1389. int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
  1390. {
  1391. struct saa7134_dev *dev = video_drvdata(file);
  1392. *id = dev->tvnorm->id;
  1393. return 0;
  1394. }
  1395. EXPORT_SYMBOL_GPL(saa7134_g_std);
  1396. static v4l2_std_id saa7134_read_std(struct saa7134_dev *dev)
  1397. {
  1398. static v4l2_std_id stds[] = {
  1399. V4L2_STD_UNKNOWN,
  1400. V4L2_STD_NTSC,
  1401. V4L2_STD_PAL,
  1402. V4L2_STD_SECAM };
  1403. v4l2_std_id result = 0;
  1404. u8 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
  1405. u8 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
  1406. if (!(st2 & 0x1)) /* RDCAP == 0 */
  1407. result = V4L2_STD_UNKNOWN;
  1408. else
  1409. result = stds[st1 & 0x03];
  1410. return result;
  1411. }
  1412. int saa7134_querystd(struct file *file, void *priv, v4l2_std_id *std)
  1413. {
  1414. struct saa7134_dev *dev = video_drvdata(file);
  1415. *std &= saa7134_read_std(dev);
  1416. return 0;
  1417. }
  1418. EXPORT_SYMBOL_GPL(saa7134_querystd);
  1419. static int saa7134_cropcap(struct file *file, void *priv,
  1420. struct v4l2_cropcap *cap)
  1421. {
  1422. struct saa7134_dev *dev = video_drvdata(file);
  1423. if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1424. cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
  1425. return -EINVAL;
  1426. cap->pixelaspect.numerator = 1;
  1427. cap->pixelaspect.denominator = 1;
  1428. if (dev->tvnorm->id & V4L2_STD_525_60) {
  1429. cap->pixelaspect.numerator = 11;
  1430. cap->pixelaspect.denominator = 10;
  1431. }
  1432. if (dev->tvnorm->id & V4L2_STD_625_50) {
  1433. cap->pixelaspect.numerator = 54;
  1434. cap->pixelaspect.denominator = 59;
  1435. }
  1436. return 0;
  1437. }
  1438. static int saa7134_g_selection(struct file *file, void *f, struct v4l2_selection *sel)
  1439. {
  1440. struct saa7134_dev *dev = video_drvdata(file);
  1441. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1442. sel->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
  1443. return -EINVAL;
  1444. switch (sel->target) {
  1445. case V4L2_SEL_TGT_CROP:
  1446. sel->r = dev->crop_current;
  1447. break;
  1448. case V4L2_SEL_TGT_CROP_DEFAULT:
  1449. sel->r = dev->crop_defrect;
  1450. break;
  1451. case V4L2_SEL_TGT_CROP_BOUNDS:
  1452. sel->r = dev->crop_bounds;
  1453. break;
  1454. default:
  1455. return -EINVAL;
  1456. }
  1457. return 0;
  1458. }
  1459. static int saa7134_s_selection(struct file *file, void *f, struct v4l2_selection *sel)
  1460. {
  1461. struct saa7134_dev *dev = video_drvdata(file);
  1462. struct v4l2_rect *b = &dev->crop_bounds;
  1463. struct v4l2_rect *c = &dev->crop_current;
  1464. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1465. sel->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
  1466. return -EINVAL;
  1467. if (sel->target != V4L2_SEL_TGT_CROP)
  1468. return -EINVAL;
  1469. if (dev->overlay_owner)
  1470. return -EBUSY;
  1471. if (vb2_is_streaming(&dev->video_vbq))
  1472. return -EBUSY;
  1473. *c = sel->r;
  1474. if (c->top < b->top)
  1475. c->top = b->top;
  1476. if (c->top > b->top + b->height)
  1477. c->top = b->top + b->height;
  1478. if (c->height > b->top - c->top + b->height)
  1479. c->height = b->top - c->top + b->height;
  1480. if (c->left < b->left)
  1481. c->left = b->left;
  1482. if (c->left > b->left + b->width)
  1483. c->left = b->left + b->width;
  1484. if (c->width > b->left - c->left + b->width)
  1485. c->width = b->left - c->left + b->width;
  1486. sel->r = *c;
  1487. return 0;
  1488. }
  1489. int saa7134_g_tuner(struct file *file, void *priv,
  1490. struct v4l2_tuner *t)
  1491. {
  1492. struct saa7134_dev *dev = video_drvdata(file);
  1493. int n;
  1494. if (0 != t->index)
  1495. return -EINVAL;
  1496. memset(t, 0, sizeof(*t));
  1497. for (n = 0; n < SAA7134_INPUT_MAX; n++) {
  1498. if (card_in(dev, n).type == SAA7134_INPUT_TV ||
  1499. card_in(dev, n).type == SAA7134_INPUT_TV_MONO)
  1500. break;
  1501. }
  1502. if (n == SAA7134_INPUT_MAX)
  1503. return -EINVAL;
  1504. if (card_in(dev, n).type != SAA7134_NO_INPUT) {
  1505. strcpy(t->name, "Television");
  1506. t->type = V4L2_TUNER_ANALOG_TV;
  1507. saa_call_all(dev, tuner, g_tuner, t);
  1508. t->capability = V4L2_TUNER_CAP_NORM |
  1509. V4L2_TUNER_CAP_STEREO |
  1510. V4L2_TUNER_CAP_LANG1 |
  1511. V4L2_TUNER_CAP_LANG2;
  1512. t->rxsubchans = saa7134_tvaudio_getstereo(dev);
  1513. t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
  1514. }
  1515. if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
  1516. t->signal = 0xffff;
  1517. return 0;
  1518. }
  1519. EXPORT_SYMBOL_GPL(saa7134_g_tuner);
  1520. int saa7134_s_tuner(struct file *file, void *priv,
  1521. const struct v4l2_tuner *t)
  1522. {
  1523. struct saa7134_dev *dev = video_drvdata(file);
  1524. int rx, mode;
  1525. if (0 != t->index)
  1526. return -EINVAL;
  1527. mode = dev->thread.mode;
  1528. if (UNSET == mode) {
  1529. rx = saa7134_tvaudio_getstereo(dev);
  1530. mode = saa7134_tvaudio_rx2mode(rx);
  1531. }
  1532. if (mode != t->audmode)
  1533. dev->thread.mode = t->audmode;
  1534. return 0;
  1535. }
  1536. EXPORT_SYMBOL_GPL(saa7134_s_tuner);
  1537. int saa7134_g_frequency(struct file *file, void *priv,
  1538. struct v4l2_frequency *f)
  1539. {
  1540. struct saa7134_dev *dev = video_drvdata(file);
  1541. if (0 != f->tuner)
  1542. return -EINVAL;
  1543. saa_call_all(dev, tuner, g_frequency, f);
  1544. return 0;
  1545. }
  1546. EXPORT_SYMBOL_GPL(saa7134_g_frequency);
  1547. int saa7134_s_frequency(struct file *file, void *priv,
  1548. const struct v4l2_frequency *f)
  1549. {
  1550. struct saa7134_dev *dev = video_drvdata(file);
  1551. if (0 != f->tuner)
  1552. return -EINVAL;
  1553. saa_call_all(dev, tuner, s_frequency, f);
  1554. saa7134_tvaudio_do_scan(dev);
  1555. return 0;
  1556. }
  1557. EXPORT_SYMBOL_GPL(saa7134_s_frequency);
  1558. static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
  1559. struct v4l2_fmtdesc *f)
  1560. {
  1561. if (f->index >= FORMATS)
  1562. return -EINVAL;
  1563. strlcpy(f->description, formats[f->index].name,
  1564. sizeof(f->description));
  1565. f->pixelformat = formats[f->index].fourcc;
  1566. return 0;
  1567. }
  1568. static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
  1569. struct v4l2_fmtdesc *f)
  1570. {
  1571. if (saa7134_no_overlay > 0) {
  1572. pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  1573. return -EINVAL;
  1574. }
  1575. if ((f->index >= FORMATS) || formats[f->index].planar)
  1576. return -EINVAL;
  1577. strlcpy(f->description, formats[f->index].name,
  1578. sizeof(f->description));
  1579. f->pixelformat = formats[f->index].fourcc;
  1580. return 0;
  1581. }
  1582. static int saa7134_g_fbuf(struct file *file, void *f,
  1583. struct v4l2_framebuffer *fb)
  1584. {
  1585. struct saa7134_dev *dev = video_drvdata(file);
  1586. *fb = dev->ovbuf;
  1587. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  1588. return 0;
  1589. }
  1590. static int saa7134_s_fbuf(struct file *file, void *f,
  1591. const struct v4l2_framebuffer *fb)
  1592. {
  1593. struct saa7134_dev *dev = video_drvdata(file);
  1594. struct saa7134_format *fmt;
  1595. if (!capable(CAP_SYS_ADMIN) &&
  1596. !capable(CAP_SYS_RAWIO))
  1597. return -EPERM;
  1598. /* check args */
  1599. fmt = format_by_fourcc(fb->fmt.pixelformat);
  1600. if (NULL == fmt)
  1601. return -EINVAL;
  1602. /* ok, accept it */
  1603. dev->ovbuf = *fb;
  1604. dev->ovfmt = fmt;
  1605. if (0 == dev->ovbuf.fmt.bytesperline)
  1606. dev->ovbuf.fmt.bytesperline =
  1607. dev->ovbuf.fmt.width*fmt->depth/8;
  1608. return 0;
  1609. }
  1610. static int saa7134_overlay(struct file *file, void *priv, unsigned int on)
  1611. {
  1612. struct saa7134_dev *dev = video_drvdata(file);
  1613. unsigned long flags;
  1614. if (on) {
  1615. if (saa7134_no_overlay > 0) {
  1616. video_dbg("no_overlay\n");
  1617. return -EINVAL;
  1618. }
  1619. if (dev->overlay_owner && priv != dev->overlay_owner)
  1620. return -EBUSY;
  1621. dev->overlay_owner = priv;
  1622. spin_lock_irqsave(&dev->slock, flags);
  1623. start_preview(dev);
  1624. spin_unlock_irqrestore(&dev->slock, flags);
  1625. }
  1626. if (!on) {
  1627. if (priv != dev->overlay_owner)
  1628. return -EINVAL;
  1629. spin_lock_irqsave(&dev->slock, flags);
  1630. stop_preview(dev);
  1631. spin_unlock_irqrestore(&dev->slock, flags);
  1632. dev->overlay_owner = NULL;
  1633. }
  1634. return 0;
  1635. }
  1636. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1637. static int vidioc_g_register (struct file *file, void *priv,
  1638. struct v4l2_dbg_register *reg)
  1639. {
  1640. struct saa7134_dev *dev = video_drvdata(file);
  1641. reg->val = saa_readb(reg->reg & 0xffffff);
  1642. reg->size = 1;
  1643. return 0;
  1644. }
  1645. static int vidioc_s_register (struct file *file, void *priv,
  1646. const struct v4l2_dbg_register *reg)
  1647. {
  1648. struct saa7134_dev *dev = video_drvdata(file);
  1649. saa_writeb(reg->reg & 0xffffff, reg->val);
  1650. return 0;
  1651. }
  1652. #endif
  1653. static int radio_g_tuner(struct file *file, void *priv,
  1654. struct v4l2_tuner *t)
  1655. {
  1656. struct saa7134_dev *dev = video_drvdata(file);
  1657. if (0 != t->index)
  1658. return -EINVAL;
  1659. strcpy(t->name, "Radio");
  1660. saa_call_all(dev, tuner, g_tuner, t);
  1661. t->audmode &= V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO;
  1662. if (dev->input->amux == TV) {
  1663. t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
  1664. t->rxsubchans = (saa_readb(0x529) & 0x08) ?
  1665. V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
  1666. }
  1667. return 0;
  1668. }
  1669. static int radio_s_tuner(struct file *file, void *priv,
  1670. const struct v4l2_tuner *t)
  1671. {
  1672. struct saa7134_dev *dev = video_drvdata(file);
  1673. if (0 != t->index)
  1674. return -EINVAL;
  1675. saa_call_all(dev, tuner, s_tuner, t);
  1676. return 0;
  1677. }
  1678. static const struct v4l2_file_operations video_fops =
  1679. {
  1680. .owner = THIS_MODULE,
  1681. .open = video_open,
  1682. .release = video_release,
  1683. .read = vb2_fop_read,
  1684. .poll = vb2_fop_poll,
  1685. .mmap = vb2_fop_mmap,
  1686. .unlocked_ioctl = video_ioctl2,
  1687. };
  1688. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1689. .vidioc_querycap = saa7134_querycap,
  1690. .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
  1691. .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
  1692. .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
  1693. .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
  1694. .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
  1695. .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
  1696. .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
  1697. .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
  1698. .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
  1699. .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
  1700. .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
  1701. .vidioc_cropcap = saa7134_cropcap,
  1702. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1703. .vidioc_querybuf = vb2_ioctl_querybuf,
  1704. .vidioc_qbuf = vb2_ioctl_qbuf,
  1705. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1706. .vidioc_expbuf = vb2_ioctl_expbuf,
  1707. .vidioc_s_std = saa7134_s_std,
  1708. .vidioc_g_std = saa7134_g_std,
  1709. .vidioc_querystd = saa7134_querystd,
  1710. .vidioc_enum_input = saa7134_enum_input,
  1711. .vidioc_g_input = saa7134_g_input,
  1712. .vidioc_s_input = saa7134_s_input,
  1713. .vidioc_streamon = vb2_ioctl_streamon,
  1714. .vidioc_streamoff = vb2_ioctl_streamoff,
  1715. .vidioc_g_tuner = saa7134_g_tuner,
  1716. .vidioc_s_tuner = saa7134_s_tuner,
  1717. .vidioc_g_selection = saa7134_g_selection,
  1718. .vidioc_s_selection = saa7134_s_selection,
  1719. .vidioc_g_fbuf = saa7134_g_fbuf,
  1720. .vidioc_s_fbuf = saa7134_s_fbuf,
  1721. .vidioc_overlay = saa7134_overlay,
  1722. .vidioc_g_frequency = saa7134_g_frequency,
  1723. .vidioc_s_frequency = saa7134_s_frequency,
  1724. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1725. .vidioc_g_register = vidioc_g_register,
  1726. .vidioc_s_register = vidioc_s_register,
  1727. #endif
  1728. .vidioc_log_status = v4l2_ctrl_log_status,
  1729. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1730. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1731. };
  1732. static const struct v4l2_file_operations radio_fops = {
  1733. .owner = THIS_MODULE,
  1734. .open = video_open,
  1735. .read = radio_read,
  1736. .release = video_release,
  1737. .unlocked_ioctl = video_ioctl2,
  1738. .poll = radio_poll,
  1739. };
  1740. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1741. .vidioc_querycap = saa7134_querycap,
  1742. .vidioc_g_tuner = radio_g_tuner,
  1743. .vidioc_s_tuner = radio_s_tuner,
  1744. .vidioc_g_frequency = saa7134_g_frequency,
  1745. .vidioc_s_frequency = saa7134_s_frequency,
  1746. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1747. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1748. };
  1749. /* ----------------------------------------------------------- */
  1750. /* exported stuff */
  1751. struct video_device saa7134_video_template = {
  1752. .name = "saa7134-video",
  1753. .fops = &video_fops,
  1754. .ioctl_ops = &video_ioctl_ops,
  1755. .tvnorms = SAA7134_NORMS,
  1756. };
  1757. struct video_device saa7134_radio_template = {
  1758. .name = "saa7134-radio",
  1759. .fops = &radio_fops,
  1760. .ioctl_ops = &radio_ioctl_ops,
  1761. };
  1762. static const struct v4l2_ctrl_ops saa7134_ctrl_ops = {
  1763. .s_ctrl = saa7134_s_ctrl,
  1764. };
  1765. static const struct v4l2_ctrl_config saa7134_ctrl_invert = {
  1766. .ops = &saa7134_ctrl_ops,
  1767. .id = V4L2_CID_PRIVATE_INVERT,
  1768. .name = "Invert",
  1769. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1770. .min = 0,
  1771. .max = 1,
  1772. .step = 1,
  1773. };
  1774. static const struct v4l2_ctrl_config saa7134_ctrl_y_odd = {
  1775. .ops = &saa7134_ctrl_ops,
  1776. .id = V4L2_CID_PRIVATE_Y_ODD,
  1777. .name = "Y Offset Odd Field",
  1778. .type = V4L2_CTRL_TYPE_INTEGER,
  1779. .min = 0,
  1780. .max = 128,
  1781. .step = 1,
  1782. };
  1783. static const struct v4l2_ctrl_config saa7134_ctrl_y_even = {
  1784. .ops = &saa7134_ctrl_ops,
  1785. .id = V4L2_CID_PRIVATE_Y_EVEN,
  1786. .name = "Y Offset Even Field",
  1787. .type = V4L2_CTRL_TYPE_INTEGER,
  1788. .min = 0,
  1789. .max = 128,
  1790. .step = 1,
  1791. };
  1792. static const struct v4l2_ctrl_config saa7134_ctrl_automute = {
  1793. .ops = &saa7134_ctrl_ops,
  1794. .id = V4L2_CID_PRIVATE_AUTOMUTE,
  1795. .name = "Automute",
  1796. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1797. .min = 0,
  1798. .max = 1,
  1799. .step = 1,
  1800. .def = 1,
  1801. };
  1802. int saa7134_video_init1(struct saa7134_dev *dev)
  1803. {
  1804. struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
  1805. struct vb2_queue *q;
  1806. int ret;
  1807. /* sanitycheck insmod options */
  1808. if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
  1809. gbuffers = 2;
  1810. if (gbufsize > gbufsize_max)
  1811. gbufsize = gbufsize_max;
  1812. gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
  1813. v4l2_ctrl_handler_init(hdl, 11);
  1814. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1815. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  1816. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1817. V4L2_CID_CONTRAST, 0, 127, 1, 68);
  1818. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1819. V4L2_CID_SATURATION, 0, 127, 1, 64);
  1820. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1821. V4L2_CID_HUE, -128, 127, 1, 0);
  1822. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1823. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1824. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1825. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
  1826. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1827. V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
  1828. v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_invert, NULL);
  1829. v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_odd, NULL);
  1830. v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_even, NULL);
  1831. v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_automute, NULL);
  1832. if (hdl->error)
  1833. return hdl->error;
  1834. if (card_has_radio(dev)) {
  1835. hdl = &dev->radio_ctrl_handler;
  1836. v4l2_ctrl_handler_init(hdl, 2);
  1837. v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler,
  1838. v4l2_ctrl_radio_filter);
  1839. if (hdl->error)
  1840. return hdl->error;
  1841. }
  1842. dev->ctl_mute = 1;
  1843. if (dev->tda9887_conf && saa7134_ctrl_automute.def)
  1844. dev->tda9887_conf |= TDA9887_AUTOMUTE;
  1845. dev->automute = 0;
  1846. INIT_LIST_HEAD(&dev->video_q.queue);
  1847. timer_setup(&dev->video_q.timeout, saa7134_buffer_timeout, 0);
  1848. dev->video_q.dev = dev;
  1849. dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  1850. dev->width = 720;
  1851. dev->height = 576;
  1852. dev->field = V4L2_FIELD_INTERLACED;
  1853. dev->win.w.width = dev->width;
  1854. dev->win.w.height = dev->height;
  1855. dev->win.field = V4L2_FIELD_INTERLACED;
  1856. dev->ovbuf.fmt.width = dev->width;
  1857. dev->ovbuf.fmt.height = dev->height;
  1858. dev->ovbuf.fmt.pixelformat = dev->fmt->fourcc;
  1859. dev->ovbuf.fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1860. if (saa7134_boards[dev->board].video_out)
  1861. saa7134_videoport_init(dev);
  1862. q = &dev->video_vbq;
  1863. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1864. /*
  1865. * Do not add VB2_USERPTR unless explicitly requested: the saa7134 DMA
  1866. * engine cannot handle transfers that do not start at the beginning
  1867. * of a page. A user-provided pointer can start anywhere in a page, so
  1868. * USERPTR support is a no-go unless the application knows about these
  1869. * limitations and has special support for this.
  1870. */
  1871. q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
  1872. if (saa7134_userptr)
  1873. q->io_modes |= VB2_USERPTR;
  1874. q->drv_priv = &dev->video_q;
  1875. q->ops = &vb2_qops;
  1876. q->gfp_flags = GFP_DMA32;
  1877. q->mem_ops = &vb2_dma_sg_memops;
  1878. q->buf_struct_size = sizeof(struct saa7134_buf);
  1879. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1880. q->lock = &dev->lock;
  1881. q->dev = &dev->pci->dev;
  1882. ret = vb2_queue_init(q);
  1883. if (ret)
  1884. return ret;
  1885. saa7134_pgtable_alloc(dev->pci, &dev->video_q.pt);
  1886. q = &dev->vbi_vbq;
  1887. q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1888. /* Don't add VB2_USERPTR, see comment above */
  1889. q->io_modes = VB2_MMAP | VB2_READ;
  1890. if (saa7134_userptr)
  1891. q->io_modes |= VB2_USERPTR;
  1892. q->drv_priv = &dev->vbi_q;
  1893. q->ops = &saa7134_vbi_qops;
  1894. q->gfp_flags = GFP_DMA32;
  1895. q->mem_ops = &vb2_dma_sg_memops;
  1896. q->buf_struct_size = sizeof(struct saa7134_buf);
  1897. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1898. q->lock = &dev->lock;
  1899. q->dev = &dev->pci->dev;
  1900. ret = vb2_queue_init(q);
  1901. if (ret)
  1902. return ret;
  1903. saa7134_pgtable_alloc(dev->pci, &dev->vbi_q.pt);
  1904. return 0;
  1905. }
  1906. void saa7134_video_fini(struct saa7134_dev *dev)
  1907. {
  1908. /* free stuff */
  1909. vb2_queue_release(&dev->video_vbq);
  1910. saa7134_pgtable_free(dev->pci, &dev->video_q.pt);
  1911. vb2_queue_release(&dev->vbi_vbq);
  1912. saa7134_pgtable_free(dev->pci, &dev->vbi_q.pt);
  1913. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  1914. if (card_has_radio(dev))
  1915. v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
  1916. }
  1917. int saa7134_videoport_init(struct saa7134_dev *dev)
  1918. {
  1919. /* enable video output */
  1920. int vo = saa7134_boards[dev->board].video_out;
  1921. int video_reg;
  1922. unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
  1923. /* Configure videoport */
  1924. saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
  1925. video_reg = video_out[vo][1];
  1926. if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
  1927. video_reg &= ~VP_T_CODE_P_INVERTED;
  1928. saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
  1929. saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
  1930. saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
  1931. video_reg = video_out[vo][5];
  1932. if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
  1933. video_reg &= ~VP_CLK_CTRL2_DELAYED;
  1934. if (vid_port_opts & SET_CLOCK_INVERTED)
  1935. video_reg |= VP_CLK_CTRL1_INVERTED;
  1936. saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
  1937. video_reg = video_out[vo][6];
  1938. if (vid_port_opts & SET_VSYNC_OFF) {
  1939. video_reg &= ~VP_VS_TYPE_MASK;
  1940. video_reg |= VP_VS_TYPE_OFF;
  1941. }
  1942. saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
  1943. saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
  1944. saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
  1945. /* Start videoport */
  1946. saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
  1947. return 0;
  1948. }
  1949. int saa7134_video_init2(struct saa7134_dev *dev)
  1950. {
  1951. /* init video hw */
  1952. set_tvnorm(dev,&tvnorms[0]);
  1953. video_mux(dev,0);
  1954. v4l2_ctrl_handler_setup(&dev->ctrl_handler);
  1955. saa7134_tvaudio_setmute(dev);
  1956. saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
  1957. return 0;
  1958. }
  1959. void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
  1960. {
  1961. static const char *st[] = {
  1962. "(no signal)", "NTSC", "PAL", "SECAM" };
  1963. u32 st1,st2;
  1964. st1 = saa_readb(SAA7134_STATUS_VIDEO1);
  1965. st2 = saa_readb(SAA7134_STATUS_VIDEO2);
  1966. video_dbg("DCSDT: pll: %s, sync: %s, norm: %s\n",
  1967. (st1 & 0x40) ? "not locked" : "locked",
  1968. (st2 & 0x40) ? "no" : "yes",
  1969. st[st1 & 0x03]);
  1970. dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
  1971. if (dev->nosignal) {
  1972. /* no video signal -> mute audio */
  1973. if (dev->ctl_automute)
  1974. dev->automute = 1;
  1975. saa7134_tvaudio_setmute(dev);
  1976. } else {
  1977. /* wake up tvaudio audio carrier scan thread */
  1978. saa7134_tvaudio_do_scan(dev);
  1979. }
  1980. if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
  1981. saa_clearb(SAA7134_SYNC_CTRL, 0x20);
  1982. else
  1983. saa_setb(SAA7134_SYNC_CTRL, 0x20);
  1984. if (dev->mops && dev->mops->signal_change)
  1985. dev->mops->signal_change(dev);
  1986. }
  1987. void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
  1988. {
  1989. enum v4l2_field field;
  1990. spin_lock(&dev->slock);
  1991. if (dev->video_q.curr) {
  1992. field = dev->field;
  1993. if (V4L2_FIELD_HAS_BOTH(field)) {
  1994. /* make sure we have seen both fields */
  1995. if ((status & 0x10) == 0x00) {
  1996. dev->video_q.curr->top_seen = 1;
  1997. goto done;
  1998. }
  1999. if (!dev->video_q.curr->top_seen)
  2000. goto done;
  2001. } else if (field == V4L2_FIELD_TOP) {
  2002. if ((status & 0x10) != 0x10)
  2003. goto done;
  2004. } else if (field == V4L2_FIELD_BOTTOM) {
  2005. if ((status & 0x10) != 0x00)
  2006. goto done;
  2007. }
  2008. saa7134_buffer_finish(dev, &dev->video_q, VB2_BUF_STATE_DONE);
  2009. }
  2010. saa7134_buffer_next(dev, &dev->video_q);
  2011. done:
  2012. spin_unlock(&dev->slock);
  2013. }