saa7134-video.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334
  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, core, s_power, 0);
  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 unsigned int radio_poll(struct file *file, poll_table *wait)
  1063. {
  1064. struct saa7134_dev *dev = video_drvdata(file);
  1065. struct saa6588_command cmd;
  1066. unsigned int rc = v4l2_ctrl_poll(file, wait);
  1067. cmd.instance = file;
  1068. cmd.event_list = wait;
  1069. cmd.result = 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.result;
  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. }
  1322. cap->capabilities = radio_caps | video_caps | vbi_caps |
  1323. cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1324. if (vdev->vfl_type == VFL_TYPE_RADIO) {
  1325. cap->device_caps &= ~V4L2_CAP_STREAMING;
  1326. if (!dev->has_rds)
  1327. cap->device_caps &= ~V4L2_CAP_READWRITE;
  1328. }
  1329. return 0;
  1330. }
  1331. EXPORT_SYMBOL_GPL(saa7134_querycap);
  1332. int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id)
  1333. {
  1334. struct saa7134_dev *dev = video_drvdata(file);
  1335. struct v4l2_fh *fh = priv;
  1336. unsigned long flags;
  1337. unsigned int i;
  1338. v4l2_std_id fixup;
  1339. if (is_empress(file) && dev->overlay_owner) {
  1340. /* Don't change the std from the mpeg device
  1341. if overlay is active. */
  1342. return -EBUSY;
  1343. }
  1344. for (i = 0; i < TVNORMS; i++)
  1345. if (id == tvnorms[i].id)
  1346. break;
  1347. if (i == TVNORMS)
  1348. for (i = 0; i < TVNORMS; i++)
  1349. if (id & tvnorms[i].id)
  1350. break;
  1351. if (i == TVNORMS)
  1352. return -EINVAL;
  1353. if ((id & V4L2_STD_SECAM) && (secam[0] != '-')) {
  1354. if (secam[0] == 'L' || secam[0] == 'l') {
  1355. if (secam[1] == 'C' || secam[1] == 'c')
  1356. fixup = V4L2_STD_SECAM_LC;
  1357. else
  1358. fixup = V4L2_STD_SECAM_L;
  1359. } else {
  1360. if (secam[0] == 'D' || secam[0] == 'd')
  1361. fixup = V4L2_STD_SECAM_DK;
  1362. else
  1363. fixup = V4L2_STD_SECAM;
  1364. }
  1365. for (i = 0; i < TVNORMS; i++) {
  1366. if (fixup == tvnorms[i].id)
  1367. break;
  1368. }
  1369. if (i == TVNORMS)
  1370. return -EINVAL;
  1371. }
  1372. id = tvnorms[i].id;
  1373. if (!is_empress(file) && fh == dev->overlay_owner) {
  1374. spin_lock_irqsave(&dev->slock, flags);
  1375. stop_preview(dev);
  1376. spin_unlock_irqrestore(&dev->slock, flags);
  1377. set_tvnorm(dev, &tvnorms[i]);
  1378. spin_lock_irqsave(&dev->slock, flags);
  1379. start_preview(dev);
  1380. spin_unlock_irqrestore(&dev->slock, flags);
  1381. } else
  1382. set_tvnorm(dev, &tvnorms[i]);
  1383. saa7134_tvaudio_do_scan(dev);
  1384. return 0;
  1385. }
  1386. EXPORT_SYMBOL_GPL(saa7134_s_std);
  1387. int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
  1388. {
  1389. struct saa7134_dev *dev = video_drvdata(file);
  1390. *id = dev->tvnorm->id;
  1391. return 0;
  1392. }
  1393. EXPORT_SYMBOL_GPL(saa7134_g_std);
  1394. static v4l2_std_id saa7134_read_std(struct saa7134_dev *dev)
  1395. {
  1396. static v4l2_std_id stds[] = {
  1397. V4L2_STD_UNKNOWN,
  1398. V4L2_STD_NTSC,
  1399. V4L2_STD_PAL,
  1400. V4L2_STD_SECAM };
  1401. v4l2_std_id result = 0;
  1402. u8 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
  1403. u8 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
  1404. if (!(st2 & 0x1)) /* RDCAP == 0 */
  1405. result = V4L2_STD_UNKNOWN;
  1406. else
  1407. result = stds[st1 & 0x03];
  1408. return result;
  1409. }
  1410. int saa7134_querystd(struct file *file, void *priv, v4l2_std_id *std)
  1411. {
  1412. struct saa7134_dev *dev = video_drvdata(file);
  1413. *std &= saa7134_read_std(dev);
  1414. return 0;
  1415. }
  1416. EXPORT_SYMBOL_GPL(saa7134_querystd);
  1417. static int saa7134_cropcap(struct file *file, void *priv,
  1418. struct v4l2_cropcap *cap)
  1419. {
  1420. struct saa7134_dev *dev = video_drvdata(file);
  1421. if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1422. cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
  1423. return -EINVAL;
  1424. cap->pixelaspect.numerator = 1;
  1425. cap->pixelaspect.denominator = 1;
  1426. if (dev->tvnorm->id & V4L2_STD_525_60) {
  1427. cap->pixelaspect.numerator = 11;
  1428. cap->pixelaspect.denominator = 10;
  1429. }
  1430. if (dev->tvnorm->id & V4L2_STD_625_50) {
  1431. cap->pixelaspect.numerator = 54;
  1432. cap->pixelaspect.denominator = 59;
  1433. }
  1434. return 0;
  1435. }
  1436. static int saa7134_g_selection(struct file *file, void *f, struct v4l2_selection *sel)
  1437. {
  1438. struct saa7134_dev *dev = video_drvdata(file);
  1439. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1440. sel->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
  1441. return -EINVAL;
  1442. switch (sel->target) {
  1443. case V4L2_SEL_TGT_CROP:
  1444. sel->r = dev->crop_current;
  1445. break;
  1446. case V4L2_SEL_TGT_CROP_DEFAULT:
  1447. sel->r = dev->crop_defrect;
  1448. break;
  1449. case V4L2_SEL_TGT_CROP_BOUNDS:
  1450. sel->r = dev->crop_bounds;
  1451. break;
  1452. default:
  1453. return -EINVAL;
  1454. }
  1455. return 0;
  1456. }
  1457. static int saa7134_s_selection(struct file *file, void *f, struct v4l2_selection *sel)
  1458. {
  1459. struct saa7134_dev *dev = video_drvdata(file);
  1460. struct v4l2_rect *b = &dev->crop_bounds;
  1461. struct v4l2_rect *c = &dev->crop_current;
  1462. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1463. sel->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
  1464. return -EINVAL;
  1465. if (sel->target != V4L2_SEL_TGT_CROP)
  1466. return -EINVAL;
  1467. if (dev->overlay_owner)
  1468. return -EBUSY;
  1469. if (vb2_is_streaming(&dev->video_vbq))
  1470. return -EBUSY;
  1471. *c = sel->r;
  1472. if (c->top < b->top)
  1473. c->top = b->top;
  1474. if (c->top > b->top + b->height)
  1475. c->top = b->top + b->height;
  1476. if (c->height > b->top - c->top + b->height)
  1477. c->height = b->top - c->top + b->height;
  1478. if (c->left < b->left)
  1479. c->left = b->left;
  1480. if (c->left > b->left + b->width)
  1481. c->left = b->left + b->width;
  1482. if (c->width > b->left - c->left + b->width)
  1483. c->width = b->left - c->left + b->width;
  1484. sel->r = *c;
  1485. return 0;
  1486. }
  1487. int saa7134_g_tuner(struct file *file, void *priv,
  1488. struct v4l2_tuner *t)
  1489. {
  1490. struct saa7134_dev *dev = video_drvdata(file);
  1491. int n;
  1492. if (0 != t->index)
  1493. return -EINVAL;
  1494. memset(t, 0, sizeof(*t));
  1495. for (n = 0; n < SAA7134_INPUT_MAX; n++) {
  1496. if (card_in(dev, n).type == SAA7134_INPUT_TV ||
  1497. card_in(dev, n).type == SAA7134_INPUT_TV_MONO)
  1498. break;
  1499. }
  1500. if (n == SAA7134_INPUT_MAX)
  1501. return -EINVAL;
  1502. if (card_in(dev, n).type != SAA7134_NO_INPUT) {
  1503. strcpy(t->name, "Television");
  1504. t->type = V4L2_TUNER_ANALOG_TV;
  1505. saa_call_all(dev, tuner, g_tuner, t);
  1506. t->capability = V4L2_TUNER_CAP_NORM |
  1507. V4L2_TUNER_CAP_STEREO |
  1508. V4L2_TUNER_CAP_LANG1 |
  1509. V4L2_TUNER_CAP_LANG2;
  1510. t->rxsubchans = saa7134_tvaudio_getstereo(dev);
  1511. t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
  1512. }
  1513. if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
  1514. t->signal = 0xffff;
  1515. return 0;
  1516. }
  1517. EXPORT_SYMBOL_GPL(saa7134_g_tuner);
  1518. int saa7134_s_tuner(struct file *file, void *priv,
  1519. const struct v4l2_tuner *t)
  1520. {
  1521. struct saa7134_dev *dev = video_drvdata(file);
  1522. int rx, mode;
  1523. if (0 != t->index)
  1524. return -EINVAL;
  1525. mode = dev->thread.mode;
  1526. if (UNSET == mode) {
  1527. rx = saa7134_tvaudio_getstereo(dev);
  1528. mode = saa7134_tvaudio_rx2mode(rx);
  1529. }
  1530. if (mode != t->audmode)
  1531. dev->thread.mode = t->audmode;
  1532. return 0;
  1533. }
  1534. EXPORT_SYMBOL_GPL(saa7134_s_tuner);
  1535. int saa7134_g_frequency(struct file *file, void *priv,
  1536. struct v4l2_frequency *f)
  1537. {
  1538. struct saa7134_dev *dev = video_drvdata(file);
  1539. if (0 != f->tuner)
  1540. return -EINVAL;
  1541. saa_call_all(dev, tuner, g_frequency, f);
  1542. return 0;
  1543. }
  1544. EXPORT_SYMBOL_GPL(saa7134_g_frequency);
  1545. int saa7134_s_frequency(struct file *file, void *priv,
  1546. const struct v4l2_frequency *f)
  1547. {
  1548. struct saa7134_dev *dev = video_drvdata(file);
  1549. if (0 != f->tuner)
  1550. return -EINVAL;
  1551. saa_call_all(dev, tuner, s_frequency, f);
  1552. saa7134_tvaudio_do_scan(dev);
  1553. return 0;
  1554. }
  1555. EXPORT_SYMBOL_GPL(saa7134_s_frequency);
  1556. static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
  1557. struct v4l2_fmtdesc *f)
  1558. {
  1559. if (f->index >= FORMATS)
  1560. return -EINVAL;
  1561. strlcpy(f->description, formats[f->index].name,
  1562. sizeof(f->description));
  1563. f->pixelformat = formats[f->index].fourcc;
  1564. return 0;
  1565. }
  1566. static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
  1567. struct v4l2_fmtdesc *f)
  1568. {
  1569. if (saa7134_no_overlay > 0) {
  1570. pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  1571. return -EINVAL;
  1572. }
  1573. if ((f->index >= FORMATS) || formats[f->index].planar)
  1574. return -EINVAL;
  1575. strlcpy(f->description, formats[f->index].name,
  1576. sizeof(f->description));
  1577. f->pixelformat = formats[f->index].fourcc;
  1578. return 0;
  1579. }
  1580. static int saa7134_g_fbuf(struct file *file, void *f,
  1581. struct v4l2_framebuffer *fb)
  1582. {
  1583. struct saa7134_dev *dev = video_drvdata(file);
  1584. *fb = dev->ovbuf;
  1585. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  1586. return 0;
  1587. }
  1588. static int saa7134_s_fbuf(struct file *file, void *f,
  1589. const struct v4l2_framebuffer *fb)
  1590. {
  1591. struct saa7134_dev *dev = video_drvdata(file);
  1592. struct saa7134_format *fmt;
  1593. if (!capable(CAP_SYS_ADMIN) &&
  1594. !capable(CAP_SYS_RAWIO))
  1595. return -EPERM;
  1596. /* check args */
  1597. fmt = format_by_fourcc(fb->fmt.pixelformat);
  1598. if (NULL == fmt)
  1599. return -EINVAL;
  1600. /* ok, accept it */
  1601. dev->ovbuf = *fb;
  1602. dev->ovfmt = fmt;
  1603. if (0 == dev->ovbuf.fmt.bytesperline)
  1604. dev->ovbuf.fmt.bytesperline =
  1605. dev->ovbuf.fmt.width*fmt->depth/8;
  1606. return 0;
  1607. }
  1608. static int saa7134_overlay(struct file *file, void *priv, unsigned int on)
  1609. {
  1610. struct saa7134_dev *dev = video_drvdata(file);
  1611. unsigned long flags;
  1612. if (on) {
  1613. if (saa7134_no_overlay > 0) {
  1614. video_dbg("no_overlay\n");
  1615. return -EINVAL;
  1616. }
  1617. if (dev->overlay_owner && priv != dev->overlay_owner)
  1618. return -EBUSY;
  1619. dev->overlay_owner = priv;
  1620. spin_lock_irqsave(&dev->slock, flags);
  1621. start_preview(dev);
  1622. spin_unlock_irqrestore(&dev->slock, flags);
  1623. }
  1624. if (!on) {
  1625. if (priv != dev->overlay_owner)
  1626. return -EINVAL;
  1627. spin_lock_irqsave(&dev->slock, flags);
  1628. stop_preview(dev);
  1629. spin_unlock_irqrestore(&dev->slock, flags);
  1630. dev->overlay_owner = NULL;
  1631. }
  1632. return 0;
  1633. }
  1634. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1635. static int vidioc_g_register (struct file *file, void *priv,
  1636. struct v4l2_dbg_register *reg)
  1637. {
  1638. struct saa7134_dev *dev = video_drvdata(file);
  1639. reg->val = saa_readb(reg->reg & 0xffffff);
  1640. reg->size = 1;
  1641. return 0;
  1642. }
  1643. static int vidioc_s_register (struct file *file, void *priv,
  1644. const struct v4l2_dbg_register *reg)
  1645. {
  1646. struct saa7134_dev *dev = video_drvdata(file);
  1647. saa_writeb(reg->reg & 0xffffff, reg->val);
  1648. return 0;
  1649. }
  1650. #endif
  1651. static int radio_g_tuner(struct file *file, void *priv,
  1652. struct v4l2_tuner *t)
  1653. {
  1654. struct saa7134_dev *dev = video_drvdata(file);
  1655. if (0 != t->index)
  1656. return -EINVAL;
  1657. strcpy(t->name, "Radio");
  1658. saa_call_all(dev, tuner, g_tuner, t);
  1659. t->audmode &= V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO;
  1660. if (dev->input->amux == TV) {
  1661. t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
  1662. t->rxsubchans = (saa_readb(0x529) & 0x08) ?
  1663. V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
  1664. }
  1665. return 0;
  1666. }
  1667. static int radio_s_tuner(struct file *file, void *priv,
  1668. const struct v4l2_tuner *t)
  1669. {
  1670. struct saa7134_dev *dev = video_drvdata(file);
  1671. if (0 != t->index)
  1672. return -EINVAL;
  1673. saa_call_all(dev, tuner, s_tuner, t);
  1674. return 0;
  1675. }
  1676. static const struct v4l2_file_operations video_fops =
  1677. {
  1678. .owner = THIS_MODULE,
  1679. .open = video_open,
  1680. .release = video_release,
  1681. .read = vb2_fop_read,
  1682. .poll = vb2_fop_poll,
  1683. .mmap = vb2_fop_mmap,
  1684. .unlocked_ioctl = video_ioctl2,
  1685. };
  1686. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1687. .vidioc_querycap = saa7134_querycap,
  1688. .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
  1689. .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
  1690. .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
  1691. .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
  1692. .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
  1693. .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
  1694. .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
  1695. .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
  1696. .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
  1697. .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
  1698. .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
  1699. .vidioc_cropcap = saa7134_cropcap,
  1700. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1701. .vidioc_querybuf = vb2_ioctl_querybuf,
  1702. .vidioc_qbuf = vb2_ioctl_qbuf,
  1703. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1704. .vidioc_expbuf = vb2_ioctl_expbuf,
  1705. .vidioc_s_std = saa7134_s_std,
  1706. .vidioc_g_std = saa7134_g_std,
  1707. .vidioc_querystd = saa7134_querystd,
  1708. .vidioc_enum_input = saa7134_enum_input,
  1709. .vidioc_g_input = saa7134_g_input,
  1710. .vidioc_s_input = saa7134_s_input,
  1711. .vidioc_streamon = vb2_ioctl_streamon,
  1712. .vidioc_streamoff = vb2_ioctl_streamoff,
  1713. .vidioc_g_tuner = saa7134_g_tuner,
  1714. .vidioc_s_tuner = saa7134_s_tuner,
  1715. .vidioc_g_selection = saa7134_g_selection,
  1716. .vidioc_s_selection = saa7134_s_selection,
  1717. .vidioc_g_fbuf = saa7134_g_fbuf,
  1718. .vidioc_s_fbuf = saa7134_s_fbuf,
  1719. .vidioc_overlay = saa7134_overlay,
  1720. .vidioc_g_frequency = saa7134_g_frequency,
  1721. .vidioc_s_frequency = saa7134_s_frequency,
  1722. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1723. .vidioc_g_register = vidioc_g_register,
  1724. .vidioc_s_register = vidioc_s_register,
  1725. #endif
  1726. .vidioc_log_status = v4l2_ctrl_log_status,
  1727. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1728. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1729. };
  1730. static const struct v4l2_file_operations radio_fops = {
  1731. .owner = THIS_MODULE,
  1732. .open = video_open,
  1733. .read = radio_read,
  1734. .release = video_release,
  1735. .unlocked_ioctl = video_ioctl2,
  1736. .poll = radio_poll,
  1737. };
  1738. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1739. .vidioc_querycap = saa7134_querycap,
  1740. .vidioc_g_tuner = radio_g_tuner,
  1741. .vidioc_s_tuner = radio_s_tuner,
  1742. .vidioc_g_frequency = saa7134_g_frequency,
  1743. .vidioc_s_frequency = saa7134_s_frequency,
  1744. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1745. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1746. };
  1747. /* ----------------------------------------------------------- */
  1748. /* exported stuff */
  1749. struct video_device saa7134_video_template = {
  1750. .name = "saa7134-video",
  1751. .fops = &video_fops,
  1752. .ioctl_ops = &video_ioctl_ops,
  1753. .tvnorms = SAA7134_NORMS,
  1754. };
  1755. struct video_device saa7134_radio_template = {
  1756. .name = "saa7134-radio",
  1757. .fops = &radio_fops,
  1758. .ioctl_ops = &radio_ioctl_ops,
  1759. };
  1760. static const struct v4l2_ctrl_ops saa7134_ctrl_ops = {
  1761. .s_ctrl = saa7134_s_ctrl,
  1762. };
  1763. static const struct v4l2_ctrl_config saa7134_ctrl_invert = {
  1764. .ops = &saa7134_ctrl_ops,
  1765. .id = V4L2_CID_PRIVATE_INVERT,
  1766. .name = "Invert",
  1767. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1768. .min = 0,
  1769. .max = 1,
  1770. .step = 1,
  1771. };
  1772. static const struct v4l2_ctrl_config saa7134_ctrl_y_odd = {
  1773. .ops = &saa7134_ctrl_ops,
  1774. .id = V4L2_CID_PRIVATE_Y_ODD,
  1775. .name = "Y Offset Odd Field",
  1776. .type = V4L2_CTRL_TYPE_INTEGER,
  1777. .min = 0,
  1778. .max = 128,
  1779. .step = 1,
  1780. };
  1781. static const struct v4l2_ctrl_config saa7134_ctrl_y_even = {
  1782. .ops = &saa7134_ctrl_ops,
  1783. .id = V4L2_CID_PRIVATE_Y_EVEN,
  1784. .name = "Y Offset Even Field",
  1785. .type = V4L2_CTRL_TYPE_INTEGER,
  1786. .min = 0,
  1787. .max = 128,
  1788. .step = 1,
  1789. };
  1790. static const struct v4l2_ctrl_config saa7134_ctrl_automute = {
  1791. .ops = &saa7134_ctrl_ops,
  1792. .id = V4L2_CID_PRIVATE_AUTOMUTE,
  1793. .name = "Automute",
  1794. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1795. .min = 0,
  1796. .max = 1,
  1797. .step = 1,
  1798. .def = 1,
  1799. };
  1800. int saa7134_video_init1(struct saa7134_dev *dev)
  1801. {
  1802. struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
  1803. struct vb2_queue *q;
  1804. int ret;
  1805. /* sanitycheck insmod options */
  1806. if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
  1807. gbuffers = 2;
  1808. if (gbufsize > gbufsize_max)
  1809. gbufsize = gbufsize_max;
  1810. gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
  1811. v4l2_ctrl_handler_init(hdl, 11);
  1812. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1813. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  1814. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1815. V4L2_CID_CONTRAST, 0, 127, 1, 68);
  1816. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1817. V4L2_CID_SATURATION, 0, 127, 1, 64);
  1818. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1819. V4L2_CID_HUE, -128, 127, 1, 0);
  1820. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1821. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1822. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1823. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
  1824. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1825. V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
  1826. v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_invert, NULL);
  1827. v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_odd, NULL);
  1828. v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_even, NULL);
  1829. v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_automute, NULL);
  1830. if (hdl->error)
  1831. return hdl->error;
  1832. if (card_has_radio(dev)) {
  1833. hdl = &dev->radio_ctrl_handler;
  1834. v4l2_ctrl_handler_init(hdl, 2);
  1835. v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler,
  1836. v4l2_ctrl_radio_filter);
  1837. if (hdl->error)
  1838. return hdl->error;
  1839. }
  1840. dev->ctl_mute = 1;
  1841. if (dev->tda9887_conf && saa7134_ctrl_automute.def)
  1842. dev->tda9887_conf |= TDA9887_AUTOMUTE;
  1843. dev->automute = 0;
  1844. INIT_LIST_HEAD(&dev->video_q.queue);
  1845. setup_timer(&dev->video_q.timeout, saa7134_buffer_timeout,
  1846. (unsigned long)(&dev->video_q));
  1847. dev->video_q.dev = dev;
  1848. dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  1849. dev->width = 720;
  1850. dev->height = 576;
  1851. dev->field = V4L2_FIELD_INTERLACED;
  1852. dev->win.w.width = dev->width;
  1853. dev->win.w.height = dev->height;
  1854. dev->win.field = V4L2_FIELD_INTERLACED;
  1855. dev->ovbuf.fmt.width = dev->width;
  1856. dev->ovbuf.fmt.height = dev->height;
  1857. dev->ovbuf.fmt.pixelformat = dev->fmt->fourcc;
  1858. dev->ovbuf.fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1859. if (saa7134_boards[dev->board].video_out)
  1860. saa7134_videoport_init(dev);
  1861. q = &dev->video_vbq;
  1862. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1863. /*
  1864. * Do not add VB2_USERPTR unless explicitly requested: the saa7134 DMA
  1865. * engine cannot handle transfers that do not start at the beginning
  1866. * of a page. A user-provided pointer can start anywhere in a page, so
  1867. * USERPTR support is a no-go unless the application knows about these
  1868. * limitations and has special support for this.
  1869. */
  1870. q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
  1871. if (saa7134_userptr)
  1872. q->io_modes |= VB2_USERPTR;
  1873. q->drv_priv = &dev->video_q;
  1874. q->ops = &vb2_qops;
  1875. q->gfp_flags = GFP_DMA32;
  1876. q->mem_ops = &vb2_dma_sg_memops;
  1877. q->buf_struct_size = sizeof(struct saa7134_buf);
  1878. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1879. q->lock = &dev->lock;
  1880. q->dev = &dev->pci->dev;
  1881. ret = vb2_queue_init(q);
  1882. if (ret)
  1883. return ret;
  1884. saa7134_pgtable_alloc(dev->pci, &dev->video_q.pt);
  1885. q = &dev->vbi_vbq;
  1886. q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1887. /* Don't add VB2_USERPTR, see comment above */
  1888. q->io_modes = VB2_MMAP | VB2_READ;
  1889. if (saa7134_userptr)
  1890. q->io_modes |= VB2_USERPTR;
  1891. q->drv_priv = &dev->vbi_q;
  1892. q->ops = &saa7134_vbi_qops;
  1893. q->gfp_flags = GFP_DMA32;
  1894. q->mem_ops = &vb2_dma_sg_memops;
  1895. q->buf_struct_size = sizeof(struct saa7134_buf);
  1896. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1897. q->lock = &dev->lock;
  1898. q->dev = &dev->pci->dev;
  1899. ret = vb2_queue_init(q);
  1900. if (ret)
  1901. return ret;
  1902. saa7134_pgtable_alloc(dev->pci, &dev->vbi_q.pt);
  1903. return 0;
  1904. }
  1905. void saa7134_video_fini(struct saa7134_dev *dev)
  1906. {
  1907. /* free stuff */
  1908. vb2_queue_release(&dev->video_vbq);
  1909. saa7134_pgtable_free(dev->pci, &dev->video_q.pt);
  1910. vb2_queue_release(&dev->vbi_vbq);
  1911. saa7134_pgtable_free(dev->pci, &dev->vbi_q.pt);
  1912. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  1913. if (card_has_radio(dev))
  1914. v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
  1915. }
  1916. int saa7134_videoport_init(struct saa7134_dev *dev)
  1917. {
  1918. /* enable video output */
  1919. int vo = saa7134_boards[dev->board].video_out;
  1920. int video_reg;
  1921. unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
  1922. /* Configure videoport */
  1923. saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
  1924. video_reg = video_out[vo][1];
  1925. if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
  1926. video_reg &= ~VP_T_CODE_P_INVERTED;
  1927. saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
  1928. saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
  1929. saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
  1930. video_reg = video_out[vo][5];
  1931. if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
  1932. video_reg &= ~VP_CLK_CTRL2_DELAYED;
  1933. if (vid_port_opts & SET_CLOCK_INVERTED)
  1934. video_reg |= VP_CLK_CTRL1_INVERTED;
  1935. saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
  1936. video_reg = video_out[vo][6];
  1937. if (vid_port_opts & SET_VSYNC_OFF) {
  1938. video_reg &= ~VP_VS_TYPE_MASK;
  1939. video_reg |= VP_VS_TYPE_OFF;
  1940. }
  1941. saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
  1942. saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
  1943. saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
  1944. /* Start videoport */
  1945. saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
  1946. return 0;
  1947. }
  1948. int saa7134_video_init2(struct saa7134_dev *dev)
  1949. {
  1950. /* init video hw */
  1951. set_tvnorm(dev,&tvnorms[0]);
  1952. video_mux(dev,0);
  1953. v4l2_ctrl_handler_setup(&dev->ctrl_handler);
  1954. saa7134_tvaudio_setmute(dev);
  1955. saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
  1956. return 0;
  1957. }
  1958. void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
  1959. {
  1960. static const char *st[] = {
  1961. "(no signal)", "NTSC", "PAL", "SECAM" };
  1962. u32 st1,st2;
  1963. st1 = saa_readb(SAA7134_STATUS_VIDEO1);
  1964. st2 = saa_readb(SAA7134_STATUS_VIDEO2);
  1965. video_dbg("DCSDT: pll: %s, sync: %s, norm: %s\n",
  1966. (st1 & 0x40) ? "not locked" : "locked",
  1967. (st2 & 0x40) ? "no" : "yes",
  1968. st[st1 & 0x03]);
  1969. dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
  1970. if (dev->nosignal) {
  1971. /* no video signal -> mute audio */
  1972. if (dev->ctl_automute)
  1973. dev->automute = 1;
  1974. saa7134_tvaudio_setmute(dev);
  1975. } else {
  1976. /* wake up tvaudio audio carrier scan thread */
  1977. saa7134_tvaudio_do_scan(dev);
  1978. }
  1979. if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
  1980. saa_clearb(SAA7134_SYNC_CTRL, 0x20);
  1981. else
  1982. saa_setb(SAA7134_SYNC_CTRL, 0x20);
  1983. if (dev->mops && dev->mops->signal_change)
  1984. dev->mops->signal_change(dev);
  1985. }
  1986. void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
  1987. {
  1988. enum v4l2_field field;
  1989. spin_lock(&dev->slock);
  1990. if (dev->video_q.curr) {
  1991. field = dev->field;
  1992. if (V4L2_FIELD_HAS_BOTH(field)) {
  1993. /* make sure we have seen both fields */
  1994. if ((status & 0x10) == 0x00) {
  1995. dev->video_q.curr->top_seen = 1;
  1996. goto done;
  1997. }
  1998. if (!dev->video_q.curr->top_seen)
  1999. goto done;
  2000. } else if (field == V4L2_FIELD_TOP) {
  2001. if ((status & 0x10) != 0x10)
  2002. goto done;
  2003. } else if (field == V4L2_FIELD_BOTTOM) {
  2004. if ((status & 0x10) != 0x00)
  2005. goto done;
  2006. }
  2007. saa7134_buffer_finish(dev, &dev->video_q, VB2_BUF_STATE_DONE);
  2008. }
  2009. saa7134_buffer_next(dev, &dev->video_q);
  2010. done:
  2011. spin_unlock(&dev->slock);
  2012. }