saa7134-video.c 59 KB

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