saa7134-video.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244
  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_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 saa7134_buf *buf = container_of(vb2, struct saa7134_buf, vb2);
  761. dmaq->curr = NULL;
  762. buf->activate = buffer_activate;
  763. return 0;
  764. }
  765. static int buffer_prepare(struct vb2_buffer *vb2)
  766. {
  767. struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
  768. struct saa7134_dev *dev = dmaq->dev;
  769. struct saa7134_buf *buf = container_of(vb2, struct saa7134_buf, vb2);
  770. struct sg_table *dma = vb2_dma_sg_plane_desc(&buf->vb2, 0);
  771. unsigned int size;
  772. if (dma->sgl->offset) {
  773. pr_err("The buffer is not page-aligned\n");
  774. return -EINVAL;
  775. }
  776. size = (dev->width * dev->height * dev->fmt->depth) >> 3;
  777. if (vb2_plane_size(vb2, 0) < size)
  778. return -EINVAL;
  779. vb2_set_plane_payload(vb2, 0, size);
  780. vb2->v4l2_buf.field = dev->field;
  781. return saa7134_pgtable_build(dev->pci, &dmaq->pt, dma->sgl, dma->nents,
  782. saa7134_buffer_startpage(buf));
  783. }
  784. static int queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
  785. unsigned int *nbuffers, unsigned int *nplanes,
  786. unsigned int sizes[], void *alloc_ctxs[])
  787. {
  788. struct saa7134_dmaqueue *dmaq = q->drv_priv;
  789. struct saa7134_dev *dev = dmaq->dev;
  790. int size = dev->fmt->depth * dev->width * dev->height >> 3;
  791. if (dev->width < 48 ||
  792. dev->height < 32 ||
  793. dev->width/4 > dev->crop_current.width ||
  794. dev->height/4 > dev->crop_current.height ||
  795. dev->width > dev->crop_bounds.width ||
  796. dev->height > dev->crop_bounds.height)
  797. return -EINVAL;
  798. *nbuffers = saa7134_buffer_count(size, *nbuffers);
  799. *nplanes = 1;
  800. sizes[0] = size;
  801. alloc_ctxs[0] = dev->alloc_ctx;
  802. return 0;
  803. }
  804. /*
  805. * move buffer to hardware queue
  806. */
  807. void saa7134_vb2_buffer_queue(struct vb2_buffer *vb)
  808. {
  809. struct saa7134_dmaqueue *dmaq = vb->vb2_queue->drv_priv;
  810. struct saa7134_dev *dev = dmaq->dev;
  811. struct saa7134_buf *buf = container_of(vb, struct saa7134_buf, vb2);
  812. saa7134_buffer_queue(dev, dmaq, buf);
  813. }
  814. EXPORT_SYMBOL_GPL(saa7134_vb2_buffer_queue);
  815. int saa7134_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
  816. {
  817. struct saa7134_dmaqueue *dmaq = vq->drv_priv;
  818. struct saa7134_dev *dev = dmaq->dev;
  819. /*
  820. * Planar video capture and TS share the same DMA channel,
  821. * so only one can be active at a time.
  822. */
  823. if (card_is_empress(dev) && vb2_is_busy(&dev->empress_vbq) &&
  824. dmaq == &dev->video_q && dev->fmt->planar) {
  825. struct saa7134_buf *buf, *tmp;
  826. list_for_each_entry_safe(buf, tmp, &dmaq->queue, entry) {
  827. list_del(&buf->entry);
  828. vb2_buffer_done(&buf->vb2, VB2_BUF_STATE_QUEUED);
  829. }
  830. if (dmaq->curr) {
  831. vb2_buffer_done(&dmaq->curr->vb2, VB2_BUF_STATE_QUEUED);
  832. dmaq->curr = NULL;
  833. }
  834. return -EBUSY;
  835. }
  836. /* The SAA7134 has a 1K FIFO; the datasheet suggests that when
  837. * configured conservatively, there's 22 usec of buffering for video.
  838. * We therefore request a DMA latency of 20 usec, giving us 2 usec of
  839. * margin in case the FIFO is configured differently to the datasheet.
  840. * Unfortunately, I lack register-level documentation to check the
  841. * Linux FIFO setup and confirm the perfect value.
  842. */
  843. if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
  844. (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
  845. pm_qos_add_request(&dev->qos_request,
  846. PM_QOS_CPU_DMA_LATENCY, 20);
  847. dmaq->seq_nr = 0;
  848. return 0;
  849. }
  850. void saa7134_vb2_stop_streaming(struct vb2_queue *vq)
  851. {
  852. struct saa7134_dmaqueue *dmaq = vq->drv_priv;
  853. struct saa7134_dev *dev = dmaq->dev;
  854. saa7134_stop_streaming(dev, dmaq);
  855. if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
  856. (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
  857. pm_qos_remove_request(&dev->qos_request);
  858. }
  859. static struct vb2_ops vb2_qops = {
  860. .queue_setup = queue_setup,
  861. .buf_init = buffer_init,
  862. .buf_prepare = buffer_prepare,
  863. .buf_queue = saa7134_vb2_buffer_queue,
  864. .wait_prepare = vb2_ops_wait_prepare,
  865. .wait_finish = vb2_ops_wait_finish,
  866. .start_streaming = saa7134_vb2_start_streaming,
  867. .stop_streaming = saa7134_vb2_stop_streaming,
  868. };
  869. /* ------------------------------------------------------------------ */
  870. static int saa7134_s_ctrl(struct v4l2_ctrl *ctrl)
  871. {
  872. struct saa7134_dev *dev = container_of(ctrl->handler, struct saa7134_dev, ctrl_handler);
  873. unsigned long flags;
  874. int restart_overlay = 0;
  875. switch (ctrl->id) {
  876. case V4L2_CID_BRIGHTNESS:
  877. dev->ctl_bright = ctrl->val;
  878. saa_writeb(SAA7134_DEC_LUMA_BRIGHT, ctrl->val);
  879. break;
  880. case V4L2_CID_HUE:
  881. dev->ctl_hue = ctrl->val;
  882. saa_writeb(SAA7134_DEC_CHROMA_HUE, ctrl->val);
  883. break;
  884. case V4L2_CID_CONTRAST:
  885. dev->ctl_contrast = ctrl->val;
  886. saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
  887. dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
  888. break;
  889. case V4L2_CID_SATURATION:
  890. dev->ctl_saturation = ctrl->val;
  891. saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
  892. dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
  893. break;
  894. case V4L2_CID_AUDIO_MUTE:
  895. dev->ctl_mute = ctrl->val;
  896. saa7134_tvaudio_setmute(dev);
  897. break;
  898. case V4L2_CID_AUDIO_VOLUME:
  899. dev->ctl_volume = ctrl->val;
  900. saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
  901. break;
  902. case V4L2_CID_PRIVATE_INVERT:
  903. dev->ctl_invert = ctrl->val;
  904. saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
  905. dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
  906. saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
  907. dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
  908. break;
  909. case V4L2_CID_HFLIP:
  910. dev->ctl_mirror = ctrl->val;
  911. restart_overlay = 1;
  912. break;
  913. case V4L2_CID_PRIVATE_Y_EVEN:
  914. dev->ctl_y_even = ctrl->val;
  915. restart_overlay = 1;
  916. break;
  917. case V4L2_CID_PRIVATE_Y_ODD:
  918. dev->ctl_y_odd = ctrl->val;
  919. restart_overlay = 1;
  920. break;
  921. case V4L2_CID_PRIVATE_AUTOMUTE:
  922. {
  923. struct v4l2_priv_tun_config tda9887_cfg;
  924. tda9887_cfg.tuner = TUNER_TDA9887;
  925. tda9887_cfg.priv = &dev->tda9887_conf;
  926. dev->ctl_automute = ctrl->val;
  927. if (dev->tda9887_conf) {
  928. if (dev->ctl_automute)
  929. dev->tda9887_conf |= TDA9887_AUTOMUTE;
  930. else
  931. dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
  932. saa_call_all(dev, tuner, s_config, &tda9887_cfg);
  933. }
  934. break;
  935. }
  936. default:
  937. return -EINVAL;
  938. }
  939. if (restart_overlay && dev->overlay_owner) {
  940. spin_lock_irqsave(&dev->slock, flags);
  941. stop_preview(dev);
  942. start_preview(dev);
  943. spin_unlock_irqrestore(&dev->slock, flags);
  944. }
  945. return 0;
  946. }
  947. /* ------------------------------------------------------------------ */
  948. static int video_open(struct file *file)
  949. {
  950. struct video_device *vdev = video_devdata(file);
  951. struct saa7134_dev *dev = video_drvdata(file);
  952. int ret = v4l2_fh_open(file);
  953. if (ret < 0)
  954. return ret;
  955. mutex_lock(&dev->lock);
  956. if (vdev->vfl_type == VFL_TYPE_RADIO) {
  957. /* switch to radio mode */
  958. saa7134_tvaudio_setinput(dev, &card(dev).radio);
  959. saa_call_all(dev, tuner, s_radio);
  960. } else {
  961. /* switch to video/vbi mode */
  962. video_mux(dev, dev->ctl_input);
  963. }
  964. mutex_unlock(&dev->lock);
  965. return 0;
  966. }
  967. static int video_release(struct file *file)
  968. {
  969. struct video_device *vdev = video_devdata(file);
  970. struct saa7134_dev *dev = video_drvdata(file);
  971. struct v4l2_fh *fh = file->private_data;
  972. struct saa6588_command cmd;
  973. unsigned long flags;
  974. mutex_lock(&dev->lock);
  975. saa7134_tvaudio_close(dev);
  976. /* turn off overlay */
  977. if (fh == dev->overlay_owner) {
  978. spin_lock_irqsave(&dev->slock,flags);
  979. stop_preview(dev);
  980. spin_unlock_irqrestore(&dev->slock,flags);
  981. dev->overlay_owner = NULL;
  982. }
  983. if (vdev->vfl_type == VFL_TYPE_RADIO)
  984. v4l2_fh_release(file);
  985. else
  986. _vb2_fop_release(file, NULL);
  987. /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
  988. saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
  989. saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
  990. saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
  991. saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
  992. saa_call_all(dev, core, s_power, 0);
  993. if (vdev->vfl_type == VFL_TYPE_RADIO)
  994. saa_call_all(dev, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
  995. mutex_unlock(&dev->lock);
  996. return 0;
  997. }
  998. static ssize_t radio_read(struct file *file, char __user *data,
  999. size_t count, loff_t *ppos)
  1000. {
  1001. struct saa7134_dev *dev = video_drvdata(file);
  1002. struct saa6588_command cmd;
  1003. cmd.block_count = count/3;
  1004. cmd.nonblocking = file->f_flags & O_NONBLOCK;
  1005. cmd.buffer = data;
  1006. cmd.instance = file;
  1007. cmd.result = -ENODEV;
  1008. mutex_lock(&dev->lock);
  1009. saa_call_all(dev, core, ioctl, SAA6588_CMD_READ, &cmd);
  1010. mutex_unlock(&dev->lock);
  1011. return cmd.result;
  1012. }
  1013. static unsigned int radio_poll(struct file *file, poll_table *wait)
  1014. {
  1015. struct saa7134_dev *dev = video_drvdata(file);
  1016. struct saa6588_command cmd;
  1017. unsigned int rc = v4l2_ctrl_poll(file, wait);
  1018. cmd.instance = file;
  1019. cmd.event_list = wait;
  1020. cmd.result = 0;
  1021. mutex_lock(&dev->lock);
  1022. saa_call_all(dev, core, ioctl, SAA6588_CMD_POLL, &cmd);
  1023. mutex_unlock(&dev->lock);
  1024. return rc | cmd.result;
  1025. }
  1026. /* ------------------------------------------------------------------ */
  1027. static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
  1028. struct v4l2_format *f)
  1029. {
  1030. struct saa7134_dev *dev = video_drvdata(file);
  1031. struct saa7134_tvnorm *norm = dev->tvnorm;
  1032. memset(&f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
  1033. f->fmt.vbi.sampling_rate = 6750000 * 4;
  1034. f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
  1035. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1036. f->fmt.vbi.offset = 64 * 4;
  1037. f->fmt.vbi.start[0] = norm->vbi_v_start_0;
  1038. f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
  1039. f->fmt.vbi.start[1] = norm->vbi_v_start_1;
  1040. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1041. f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
  1042. return 0;
  1043. }
  1044. static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
  1045. struct v4l2_format *f)
  1046. {
  1047. struct saa7134_dev *dev = video_drvdata(file);
  1048. f->fmt.pix.width = dev->width;
  1049. f->fmt.pix.height = dev->height;
  1050. f->fmt.pix.field = dev->field;
  1051. f->fmt.pix.pixelformat = dev->fmt->fourcc;
  1052. f->fmt.pix.bytesperline =
  1053. (f->fmt.pix.width * dev->fmt->depth) >> 3;
  1054. f->fmt.pix.sizeimage =
  1055. f->fmt.pix.height * f->fmt.pix.bytesperline;
  1056. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1057. return 0;
  1058. }
  1059. static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
  1060. struct v4l2_format *f)
  1061. {
  1062. struct saa7134_dev *dev = video_drvdata(file);
  1063. struct v4l2_clip __user *clips = f->fmt.win.clips;
  1064. u32 clipcount = f->fmt.win.clipcount;
  1065. int err = 0;
  1066. int i;
  1067. if (saa7134_no_overlay > 0) {
  1068. pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  1069. return -EINVAL;
  1070. }
  1071. f->fmt.win = dev->win;
  1072. f->fmt.win.clips = clips;
  1073. if (clips == NULL)
  1074. clipcount = 0;
  1075. if (dev->nclips < clipcount)
  1076. clipcount = dev->nclips;
  1077. f->fmt.win.clipcount = clipcount;
  1078. for (i = 0; !err && i < clipcount; i++) {
  1079. if (copy_to_user(&f->fmt.win.clips[i].c, &dev->clips[i].c,
  1080. sizeof(struct v4l2_rect)))
  1081. err = -EFAULT;
  1082. }
  1083. return err;
  1084. }
  1085. static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
  1086. struct v4l2_format *f)
  1087. {
  1088. struct saa7134_dev *dev = video_drvdata(file);
  1089. struct saa7134_format *fmt;
  1090. enum v4l2_field field;
  1091. unsigned int maxw, maxh;
  1092. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  1093. if (NULL == fmt)
  1094. return -EINVAL;
  1095. field = f->fmt.pix.field;
  1096. maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
  1097. maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
  1098. if (V4L2_FIELD_ANY == field) {
  1099. field = (f->fmt.pix.height > maxh/2)
  1100. ? V4L2_FIELD_INTERLACED
  1101. : V4L2_FIELD_BOTTOM;
  1102. }
  1103. switch (field) {
  1104. case V4L2_FIELD_TOP:
  1105. case V4L2_FIELD_BOTTOM:
  1106. maxh = maxh / 2;
  1107. break;
  1108. default:
  1109. field = V4L2_FIELD_INTERLACED;
  1110. break;
  1111. }
  1112. f->fmt.pix.field = field;
  1113. if (f->fmt.pix.width < 48)
  1114. f->fmt.pix.width = 48;
  1115. if (f->fmt.pix.height < 32)
  1116. f->fmt.pix.height = 32;
  1117. if (f->fmt.pix.width > maxw)
  1118. f->fmt.pix.width = maxw;
  1119. if (f->fmt.pix.height > maxh)
  1120. f->fmt.pix.height = maxh;
  1121. f->fmt.pix.width &= ~0x03;
  1122. f->fmt.pix.bytesperline =
  1123. (f->fmt.pix.width * fmt->depth) >> 3;
  1124. f->fmt.pix.sizeimage =
  1125. f->fmt.pix.height * f->fmt.pix.bytesperline;
  1126. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1127. return 0;
  1128. }
  1129. static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
  1130. struct v4l2_format *f)
  1131. {
  1132. struct saa7134_dev *dev = video_drvdata(file);
  1133. if (saa7134_no_overlay > 0) {
  1134. pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  1135. return -EINVAL;
  1136. }
  1137. if (f->fmt.win.clips == NULL)
  1138. f->fmt.win.clipcount = 0;
  1139. return verify_preview(dev, &f->fmt.win, true);
  1140. }
  1141. static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
  1142. struct v4l2_format *f)
  1143. {
  1144. struct saa7134_dev *dev = video_drvdata(file);
  1145. int err;
  1146. err = saa7134_try_fmt_vid_cap(file, priv, f);
  1147. if (0 != err)
  1148. return err;
  1149. dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  1150. dev->width = f->fmt.pix.width;
  1151. dev->height = f->fmt.pix.height;
  1152. dev->field = f->fmt.pix.field;
  1153. return 0;
  1154. }
  1155. static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
  1156. struct v4l2_format *f)
  1157. {
  1158. struct saa7134_dev *dev = video_drvdata(file);
  1159. int err;
  1160. unsigned long flags;
  1161. if (saa7134_no_overlay > 0) {
  1162. pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  1163. return -EINVAL;
  1164. }
  1165. if (f->fmt.win.clips == NULL)
  1166. f->fmt.win.clipcount = 0;
  1167. err = verify_preview(dev, &f->fmt.win, true);
  1168. if (0 != err)
  1169. return err;
  1170. dev->win = f->fmt.win;
  1171. dev->nclips = f->fmt.win.clipcount;
  1172. if (copy_from_user(dev->clips, f->fmt.win.clips,
  1173. sizeof(struct v4l2_clip) * dev->nclips))
  1174. return -EFAULT;
  1175. if (priv == dev->overlay_owner) {
  1176. spin_lock_irqsave(&dev->slock, flags);
  1177. stop_preview(dev);
  1178. start_preview(dev);
  1179. spin_unlock_irqrestore(&dev->slock, flags);
  1180. }
  1181. return 0;
  1182. }
  1183. int saa7134_enum_input(struct file *file, void *priv, struct v4l2_input *i)
  1184. {
  1185. struct saa7134_dev *dev = video_drvdata(file);
  1186. unsigned int n;
  1187. n = i->index;
  1188. if (n >= SAA7134_INPUT_MAX)
  1189. return -EINVAL;
  1190. if (NULL == card_in(dev, i->index).name)
  1191. return -EINVAL;
  1192. i->index = n;
  1193. i->type = V4L2_INPUT_TYPE_CAMERA;
  1194. strcpy(i->name, card_in(dev, n).name);
  1195. if (card_in(dev, n).tv)
  1196. i->type = V4L2_INPUT_TYPE_TUNER;
  1197. if (n == dev->ctl_input) {
  1198. int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
  1199. int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
  1200. if (0 != (v1 & 0x40))
  1201. i->status |= V4L2_IN_ST_NO_H_LOCK;
  1202. if (0 != (v2 & 0x40))
  1203. i->status |= V4L2_IN_ST_NO_SIGNAL;
  1204. if (0 != (v2 & 0x0e))
  1205. i->status |= V4L2_IN_ST_MACROVISION;
  1206. }
  1207. i->std = SAA7134_NORMS;
  1208. return 0;
  1209. }
  1210. EXPORT_SYMBOL_GPL(saa7134_enum_input);
  1211. int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
  1212. {
  1213. struct saa7134_dev *dev = video_drvdata(file);
  1214. *i = dev->ctl_input;
  1215. return 0;
  1216. }
  1217. EXPORT_SYMBOL_GPL(saa7134_g_input);
  1218. int saa7134_s_input(struct file *file, void *priv, unsigned int i)
  1219. {
  1220. struct saa7134_dev *dev = video_drvdata(file);
  1221. if (i >= SAA7134_INPUT_MAX)
  1222. return -EINVAL;
  1223. if (NULL == card_in(dev, i).name)
  1224. return -EINVAL;
  1225. video_mux(dev, i);
  1226. return 0;
  1227. }
  1228. EXPORT_SYMBOL_GPL(saa7134_s_input);
  1229. int saa7134_querycap(struct file *file, void *priv,
  1230. struct v4l2_capability *cap)
  1231. {
  1232. struct saa7134_dev *dev = video_drvdata(file);
  1233. struct video_device *vdev = video_devdata(file);
  1234. u32 radio_caps, video_caps, vbi_caps;
  1235. unsigned int tuner_type = dev->tuner_type;
  1236. strcpy(cap->driver, "saa7134");
  1237. strlcpy(cap->card, saa7134_boards[dev->board].name,
  1238. sizeof(cap->card));
  1239. sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
  1240. cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1241. if ((tuner_type != TUNER_ABSENT) && (tuner_type != UNSET))
  1242. cap->device_caps |= V4L2_CAP_TUNER;
  1243. radio_caps = V4L2_CAP_RADIO;
  1244. if (dev->has_rds)
  1245. radio_caps |= V4L2_CAP_RDS_CAPTURE;
  1246. video_caps = V4L2_CAP_VIDEO_CAPTURE;
  1247. if (saa7134_no_overlay <= 0 && !is_empress(file))
  1248. video_caps |= V4L2_CAP_VIDEO_OVERLAY;
  1249. vbi_caps = V4L2_CAP_VBI_CAPTURE;
  1250. switch (vdev->vfl_type) {
  1251. case VFL_TYPE_RADIO:
  1252. cap->device_caps |= radio_caps;
  1253. break;
  1254. case VFL_TYPE_GRABBER:
  1255. cap->device_caps |= video_caps;
  1256. break;
  1257. case VFL_TYPE_VBI:
  1258. cap->device_caps |= vbi_caps;
  1259. break;
  1260. }
  1261. cap->capabilities = radio_caps | video_caps | vbi_caps |
  1262. cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  1263. if (vdev->vfl_type == VFL_TYPE_RADIO) {
  1264. cap->device_caps &= ~V4L2_CAP_STREAMING;
  1265. if (!dev->has_rds)
  1266. cap->device_caps &= ~V4L2_CAP_READWRITE;
  1267. }
  1268. return 0;
  1269. }
  1270. EXPORT_SYMBOL_GPL(saa7134_querycap);
  1271. int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id)
  1272. {
  1273. struct saa7134_dev *dev = video_drvdata(file);
  1274. struct v4l2_fh *fh = priv;
  1275. unsigned long flags;
  1276. unsigned int i;
  1277. v4l2_std_id fixup;
  1278. if (is_empress(file) && dev->overlay_owner) {
  1279. /* Don't change the std from the mpeg device
  1280. if overlay is active. */
  1281. return -EBUSY;
  1282. }
  1283. for (i = 0; i < TVNORMS; i++)
  1284. if (id == tvnorms[i].id)
  1285. break;
  1286. if (i == TVNORMS)
  1287. for (i = 0; i < TVNORMS; i++)
  1288. if (id & tvnorms[i].id)
  1289. break;
  1290. if (i == TVNORMS)
  1291. return -EINVAL;
  1292. if ((id & V4L2_STD_SECAM) && (secam[0] != '-')) {
  1293. if (secam[0] == 'L' || secam[0] == 'l') {
  1294. if (secam[1] == 'C' || secam[1] == 'c')
  1295. fixup = V4L2_STD_SECAM_LC;
  1296. else
  1297. fixup = V4L2_STD_SECAM_L;
  1298. } else {
  1299. if (secam[0] == 'D' || secam[0] == 'd')
  1300. fixup = V4L2_STD_SECAM_DK;
  1301. else
  1302. fixup = V4L2_STD_SECAM;
  1303. }
  1304. for (i = 0; i < TVNORMS; i++) {
  1305. if (fixup == tvnorms[i].id)
  1306. break;
  1307. }
  1308. if (i == TVNORMS)
  1309. return -EINVAL;
  1310. }
  1311. id = tvnorms[i].id;
  1312. if (!is_empress(file) && fh == dev->overlay_owner) {
  1313. spin_lock_irqsave(&dev->slock, flags);
  1314. stop_preview(dev);
  1315. spin_unlock_irqrestore(&dev->slock, flags);
  1316. set_tvnorm(dev, &tvnorms[i]);
  1317. spin_lock_irqsave(&dev->slock, flags);
  1318. start_preview(dev);
  1319. spin_unlock_irqrestore(&dev->slock, flags);
  1320. } else
  1321. set_tvnorm(dev, &tvnorms[i]);
  1322. saa7134_tvaudio_do_scan(dev);
  1323. return 0;
  1324. }
  1325. EXPORT_SYMBOL_GPL(saa7134_s_std);
  1326. int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
  1327. {
  1328. struct saa7134_dev *dev = video_drvdata(file);
  1329. *id = dev->tvnorm->id;
  1330. return 0;
  1331. }
  1332. EXPORT_SYMBOL_GPL(saa7134_g_std);
  1333. static v4l2_std_id saa7134_read_std(struct saa7134_dev *dev)
  1334. {
  1335. static v4l2_std_id stds[] = {
  1336. V4L2_STD_UNKNOWN,
  1337. V4L2_STD_NTSC,
  1338. V4L2_STD_PAL,
  1339. V4L2_STD_SECAM };
  1340. v4l2_std_id result = 0;
  1341. u8 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
  1342. u8 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
  1343. if (!(st2 & 0x1)) /* RDCAP == 0 */
  1344. result = V4L2_STD_UNKNOWN;
  1345. else
  1346. result = stds[st1 & 0x03];
  1347. return result;
  1348. }
  1349. int saa7134_querystd(struct file *file, void *priv, v4l2_std_id *std)
  1350. {
  1351. struct saa7134_dev *dev = video_drvdata(file);
  1352. *std &= saa7134_read_std(dev);
  1353. return 0;
  1354. }
  1355. EXPORT_SYMBOL_GPL(saa7134_querystd);
  1356. static int saa7134_cropcap(struct file *file, void *priv,
  1357. struct v4l2_cropcap *cap)
  1358. {
  1359. struct saa7134_dev *dev = video_drvdata(file);
  1360. if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1361. cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
  1362. return -EINVAL;
  1363. cap->bounds = dev->crop_bounds;
  1364. cap->defrect = dev->crop_defrect;
  1365. cap->pixelaspect.numerator = 1;
  1366. cap->pixelaspect.denominator = 1;
  1367. if (dev->tvnorm->id & V4L2_STD_525_60) {
  1368. cap->pixelaspect.numerator = 11;
  1369. cap->pixelaspect.denominator = 10;
  1370. }
  1371. if (dev->tvnorm->id & V4L2_STD_625_50) {
  1372. cap->pixelaspect.numerator = 54;
  1373. cap->pixelaspect.denominator = 59;
  1374. }
  1375. return 0;
  1376. }
  1377. static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
  1378. {
  1379. struct saa7134_dev *dev = video_drvdata(file);
  1380. if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1381. crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
  1382. return -EINVAL;
  1383. crop->c = dev->crop_current;
  1384. return 0;
  1385. }
  1386. static int saa7134_s_crop(struct file *file, void *f, const struct v4l2_crop *crop)
  1387. {
  1388. struct saa7134_dev *dev = video_drvdata(file);
  1389. struct v4l2_rect *b = &dev->crop_bounds;
  1390. struct v4l2_rect *c = &dev->crop_current;
  1391. if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1392. crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
  1393. return -EINVAL;
  1394. if (dev->overlay_owner)
  1395. return -EBUSY;
  1396. if (vb2_is_streaming(&dev->video_vbq))
  1397. return -EBUSY;
  1398. *c = crop->c;
  1399. if (c->top < b->top)
  1400. c->top = b->top;
  1401. if (c->top > b->top + b->height)
  1402. c->top = b->top + b->height;
  1403. if (c->height > b->top - c->top + b->height)
  1404. c->height = b->top - c->top + b->height;
  1405. if (c->left < b->left)
  1406. c->left = b->left;
  1407. if (c->left > b->left + b->width)
  1408. c->left = b->left + b->width;
  1409. if (c->width > b->left - c->left + b->width)
  1410. c->width = b->left - c->left + b->width;
  1411. return 0;
  1412. }
  1413. int saa7134_g_tuner(struct file *file, void *priv,
  1414. struct v4l2_tuner *t)
  1415. {
  1416. struct saa7134_dev *dev = video_drvdata(file);
  1417. int n;
  1418. if (0 != t->index)
  1419. return -EINVAL;
  1420. memset(t, 0, sizeof(*t));
  1421. for (n = 0; n < SAA7134_INPUT_MAX; n++) {
  1422. if (card_in(dev, n).tv)
  1423. break;
  1424. }
  1425. if (n == SAA7134_INPUT_MAX)
  1426. return -EINVAL;
  1427. if (NULL != card_in(dev, n).name) {
  1428. strcpy(t->name, "Television");
  1429. t->type = V4L2_TUNER_ANALOG_TV;
  1430. saa_call_all(dev, tuner, g_tuner, t);
  1431. t->capability = V4L2_TUNER_CAP_NORM |
  1432. V4L2_TUNER_CAP_STEREO |
  1433. V4L2_TUNER_CAP_LANG1 |
  1434. V4L2_TUNER_CAP_LANG2;
  1435. t->rxsubchans = saa7134_tvaudio_getstereo(dev);
  1436. t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
  1437. }
  1438. if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
  1439. t->signal = 0xffff;
  1440. return 0;
  1441. }
  1442. EXPORT_SYMBOL_GPL(saa7134_g_tuner);
  1443. int saa7134_s_tuner(struct file *file, void *priv,
  1444. const struct v4l2_tuner *t)
  1445. {
  1446. struct saa7134_dev *dev = video_drvdata(file);
  1447. int rx, mode;
  1448. if (0 != t->index)
  1449. return -EINVAL;
  1450. mode = dev->thread.mode;
  1451. if (UNSET == mode) {
  1452. rx = saa7134_tvaudio_getstereo(dev);
  1453. mode = saa7134_tvaudio_rx2mode(rx);
  1454. }
  1455. if (mode != t->audmode)
  1456. dev->thread.mode = t->audmode;
  1457. return 0;
  1458. }
  1459. EXPORT_SYMBOL_GPL(saa7134_s_tuner);
  1460. int saa7134_g_frequency(struct file *file, void *priv,
  1461. struct v4l2_frequency *f)
  1462. {
  1463. struct saa7134_dev *dev = video_drvdata(file);
  1464. if (0 != f->tuner)
  1465. return -EINVAL;
  1466. saa_call_all(dev, tuner, g_frequency, f);
  1467. return 0;
  1468. }
  1469. EXPORT_SYMBOL_GPL(saa7134_g_frequency);
  1470. int saa7134_s_frequency(struct file *file, void *priv,
  1471. const struct v4l2_frequency *f)
  1472. {
  1473. struct saa7134_dev *dev = video_drvdata(file);
  1474. if (0 != f->tuner)
  1475. return -EINVAL;
  1476. saa_call_all(dev, tuner, s_frequency, f);
  1477. saa7134_tvaudio_do_scan(dev);
  1478. return 0;
  1479. }
  1480. EXPORT_SYMBOL_GPL(saa7134_s_frequency);
  1481. static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
  1482. struct v4l2_fmtdesc *f)
  1483. {
  1484. if (f->index >= FORMATS)
  1485. return -EINVAL;
  1486. strlcpy(f->description, formats[f->index].name,
  1487. sizeof(f->description));
  1488. f->pixelformat = formats[f->index].fourcc;
  1489. return 0;
  1490. }
  1491. static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
  1492. struct v4l2_fmtdesc *f)
  1493. {
  1494. if (saa7134_no_overlay > 0) {
  1495. pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  1496. return -EINVAL;
  1497. }
  1498. if ((f->index >= FORMATS) || formats[f->index].planar)
  1499. return -EINVAL;
  1500. strlcpy(f->description, formats[f->index].name,
  1501. sizeof(f->description));
  1502. f->pixelformat = formats[f->index].fourcc;
  1503. return 0;
  1504. }
  1505. static int saa7134_g_fbuf(struct file *file, void *f,
  1506. struct v4l2_framebuffer *fb)
  1507. {
  1508. struct saa7134_dev *dev = video_drvdata(file);
  1509. *fb = dev->ovbuf;
  1510. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  1511. return 0;
  1512. }
  1513. static int saa7134_s_fbuf(struct file *file, void *f,
  1514. const struct v4l2_framebuffer *fb)
  1515. {
  1516. struct saa7134_dev *dev = video_drvdata(file);
  1517. struct saa7134_format *fmt;
  1518. if (!capable(CAP_SYS_ADMIN) &&
  1519. !capable(CAP_SYS_RAWIO))
  1520. return -EPERM;
  1521. /* check args */
  1522. fmt = format_by_fourcc(fb->fmt.pixelformat);
  1523. if (NULL == fmt)
  1524. return -EINVAL;
  1525. /* ok, accept it */
  1526. dev->ovbuf = *fb;
  1527. dev->ovfmt = fmt;
  1528. if (0 == dev->ovbuf.fmt.bytesperline)
  1529. dev->ovbuf.fmt.bytesperline =
  1530. dev->ovbuf.fmt.width*fmt->depth/8;
  1531. return 0;
  1532. }
  1533. static int saa7134_overlay(struct file *file, void *priv, unsigned int on)
  1534. {
  1535. struct saa7134_dev *dev = video_drvdata(file);
  1536. unsigned long flags;
  1537. if (on) {
  1538. if (saa7134_no_overlay > 0) {
  1539. video_dbg("no_overlay\n");
  1540. return -EINVAL;
  1541. }
  1542. if (dev->overlay_owner && priv != dev->overlay_owner)
  1543. return -EBUSY;
  1544. dev->overlay_owner = priv;
  1545. spin_lock_irqsave(&dev->slock, flags);
  1546. start_preview(dev);
  1547. spin_unlock_irqrestore(&dev->slock, flags);
  1548. }
  1549. if (!on) {
  1550. if (priv != dev->overlay_owner)
  1551. return -EINVAL;
  1552. spin_lock_irqsave(&dev->slock, flags);
  1553. stop_preview(dev);
  1554. spin_unlock_irqrestore(&dev->slock, flags);
  1555. dev->overlay_owner = NULL;
  1556. }
  1557. return 0;
  1558. }
  1559. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1560. static int vidioc_g_register (struct file *file, void *priv,
  1561. struct v4l2_dbg_register *reg)
  1562. {
  1563. struct saa7134_dev *dev = video_drvdata(file);
  1564. reg->val = saa_readb(reg->reg & 0xffffff);
  1565. reg->size = 1;
  1566. return 0;
  1567. }
  1568. static int vidioc_s_register (struct file *file, void *priv,
  1569. const struct v4l2_dbg_register *reg)
  1570. {
  1571. struct saa7134_dev *dev = video_drvdata(file);
  1572. saa_writeb(reg->reg & 0xffffff, reg->val);
  1573. return 0;
  1574. }
  1575. #endif
  1576. static int radio_g_tuner(struct file *file, void *priv,
  1577. struct v4l2_tuner *t)
  1578. {
  1579. struct saa7134_dev *dev = video_drvdata(file);
  1580. if (0 != t->index)
  1581. return -EINVAL;
  1582. strcpy(t->name, "Radio");
  1583. saa_call_all(dev, tuner, g_tuner, t);
  1584. t->audmode &= V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO;
  1585. if (dev->input->amux == TV) {
  1586. t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
  1587. t->rxsubchans = (saa_readb(0x529) & 0x08) ?
  1588. V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
  1589. }
  1590. return 0;
  1591. }
  1592. static int radio_s_tuner(struct file *file, void *priv,
  1593. const struct v4l2_tuner *t)
  1594. {
  1595. struct saa7134_dev *dev = video_drvdata(file);
  1596. if (0 != t->index)
  1597. return -EINVAL;
  1598. saa_call_all(dev, tuner, s_tuner, t);
  1599. return 0;
  1600. }
  1601. static const struct v4l2_file_operations video_fops =
  1602. {
  1603. .owner = THIS_MODULE,
  1604. .open = video_open,
  1605. .release = video_release,
  1606. .read = vb2_fop_read,
  1607. .poll = vb2_fop_poll,
  1608. .mmap = vb2_fop_mmap,
  1609. .unlocked_ioctl = video_ioctl2,
  1610. };
  1611. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1612. .vidioc_querycap = saa7134_querycap,
  1613. .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
  1614. .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
  1615. .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
  1616. .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
  1617. .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
  1618. .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
  1619. .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
  1620. .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
  1621. .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
  1622. .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
  1623. .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
  1624. .vidioc_cropcap = saa7134_cropcap,
  1625. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1626. .vidioc_querybuf = vb2_ioctl_querybuf,
  1627. .vidioc_qbuf = vb2_ioctl_qbuf,
  1628. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1629. .vidioc_s_std = saa7134_s_std,
  1630. .vidioc_g_std = saa7134_g_std,
  1631. .vidioc_querystd = saa7134_querystd,
  1632. .vidioc_enum_input = saa7134_enum_input,
  1633. .vidioc_g_input = saa7134_g_input,
  1634. .vidioc_s_input = saa7134_s_input,
  1635. .vidioc_streamon = vb2_ioctl_streamon,
  1636. .vidioc_streamoff = vb2_ioctl_streamoff,
  1637. .vidioc_g_tuner = saa7134_g_tuner,
  1638. .vidioc_s_tuner = saa7134_s_tuner,
  1639. .vidioc_g_crop = saa7134_g_crop,
  1640. .vidioc_s_crop = saa7134_s_crop,
  1641. .vidioc_g_fbuf = saa7134_g_fbuf,
  1642. .vidioc_s_fbuf = saa7134_s_fbuf,
  1643. .vidioc_overlay = saa7134_overlay,
  1644. .vidioc_g_frequency = saa7134_g_frequency,
  1645. .vidioc_s_frequency = saa7134_s_frequency,
  1646. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1647. .vidioc_g_register = vidioc_g_register,
  1648. .vidioc_s_register = vidioc_s_register,
  1649. #endif
  1650. .vidioc_log_status = v4l2_ctrl_log_status,
  1651. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1652. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1653. };
  1654. static const struct v4l2_file_operations radio_fops = {
  1655. .owner = THIS_MODULE,
  1656. .open = video_open,
  1657. .read = radio_read,
  1658. .release = video_release,
  1659. .unlocked_ioctl = video_ioctl2,
  1660. .poll = radio_poll,
  1661. };
  1662. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1663. .vidioc_querycap = saa7134_querycap,
  1664. .vidioc_g_tuner = radio_g_tuner,
  1665. .vidioc_s_tuner = radio_s_tuner,
  1666. .vidioc_g_frequency = saa7134_g_frequency,
  1667. .vidioc_s_frequency = saa7134_s_frequency,
  1668. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1669. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1670. };
  1671. /* ----------------------------------------------------------- */
  1672. /* exported stuff */
  1673. struct video_device saa7134_video_template = {
  1674. .name = "saa7134-video",
  1675. .fops = &video_fops,
  1676. .ioctl_ops = &video_ioctl_ops,
  1677. .tvnorms = SAA7134_NORMS,
  1678. };
  1679. struct video_device saa7134_radio_template = {
  1680. .name = "saa7134-radio",
  1681. .fops = &radio_fops,
  1682. .ioctl_ops = &radio_ioctl_ops,
  1683. };
  1684. static const struct v4l2_ctrl_ops saa7134_ctrl_ops = {
  1685. .s_ctrl = saa7134_s_ctrl,
  1686. };
  1687. static const struct v4l2_ctrl_config saa7134_ctrl_invert = {
  1688. .ops = &saa7134_ctrl_ops,
  1689. .id = V4L2_CID_PRIVATE_INVERT,
  1690. .name = "Invert",
  1691. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1692. .min = 0,
  1693. .max = 1,
  1694. .step = 1,
  1695. };
  1696. static const struct v4l2_ctrl_config saa7134_ctrl_y_odd = {
  1697. .ops = &saa7134_ctrl_ops,
  1698. .id = V4L2_CID_PRIVATE_Y_ODD,
  1699. .name = "Y Offset Odd Field",
  1700. .type = V4L2_CTRL_TYPE_INTEGER,
  1701. .min = 0,
  1702. .max = 128,
  1703. .step = 1,
  1704. };
  1705. static const struct v4l2_ctrl_config saa7134_ctrl_y_even = {
  1706. .ops = &saa7134_ctrl_ops,
  1707. .id = V4L2_CID_PRIVATE_Y_EVEN,
  1708. .name = "Y Offset Even Field",
  1709. .type = V4L2_CTRL_TYPE_INTEGER,
  1710. .min = 0,
  1711. .max = 128,
  1712. .step = 1,
  1713. };
  1714. static const struct v4l2_ctrl_config saa7134_ctrl_automute = {
  1715. .ops = &saa7134_ctrl_ops,
  1716. .id = V4L2_CID_PRIVATE_AUTOMUTE,
  1717. .name = "Automute",
  1718. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1719. .min = 0,
  1720. .max = 1,
  1721. .step = 1,
  1722. .def = 1,
  1723. };
  1724. int saa7134_video_init1(struct saa7134_dev *dev)
  1725. {
  1726. struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
  1727. struct vb2_queue *q;
  1728. int ret;
  1729. /* sanitycheck insmod options */
  1730. if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
  1731. gbuffers = 2;
  1732. if (gbufsize > gbufsize_max)
  1733. gbufsize = gbufsize_max;
  1734. gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
  1735. v4l2_ctrl_handler_init(hdl, 11);
  1736. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1737. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  1738. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1739. V4L2_CID_CONTRAST, 0, 127, 1, 68);
  1740. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1741. V4L2_CID_SATURATION, 0, 127, 1, 64);
  1742. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1743. V4L2_CID_HUE, -128, 127, 1, 0);
  1744. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1745. V4L2_CID_HFLIP, 0, 1, 1, 0);
  1746. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1747. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
  1748. v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
  1749. V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
  1750. v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_invert, NULL);
  1751. v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_odd, NULL);
  1752. v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_even, NULL);
  1753. v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_automute, NULL);
  1754. if (hdl->error)
  1755. return hdl->error;
  1756. if (card_has_radio(dev)) {
  1757. hdl = &dev->radio_ctrl_handler;
  1758. v4l2_ctrl_handler_init(hdl, 2);
  1759. v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler,
  1760. v4l2_ctrl_radio_filter);
  1761. if (hdl->error)
  1762. return hdl->error;
  1763. }
  1764. dev->ctl_mute = 1;
  1765. if (dev->tda9887_conf && saa7134_ctrl_automute.def)
  1766. dev->tda9887_conf |= TDA9887_AUTOMUTE;
  1767. dev->automute = 0;
  1768. INIT_LIST_HEAD(&dev->video_q.queue);
  1769. init_timer(&dev->video_q.timeout);
  1770. dev->video_q.timeout.function = saa7134_buffer_timeout;
  1771. dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
  1772. dev->video_q.dev = dev;
  1773. dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  1774. dev->width = 720;
  1775. dev->height = 576;
  1776. dev->field = V4L2_FIELD_INTERLACED;
  1777. dev->win.w.width = dev->width;
  1778. dev->win.w.height = dev->height;
  1779. dev->win.field = V4L2_FIELD_INTERLACED;
  1780. dev->ovbuf.fmt.width = dev->width;
  1781. dev->ovbuf.fmt.height = dev->height;
  1782. dev->ovbuf.fmt.pixelformat = dev->fmt->fourcc;
  1783. dev->ovbuf.fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1784. if (saa7134_boards[dev->board].video_out)
  1785. saa7134_videoport_init(dev);
  1786. q = &dev->video_vbq;
  1787. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1788. /*
  1789. * Do not add VB2_USERPTR unless explicitly requested: the saa7134 DMA
  1790. * engine cannot handle transfers that do not start at the beginning
  1791. * of a page. A user-provided pointer can start anywhere in a page, so
  1792. * USERPTR support is a no-go unless the application knows about these
  1793. * limitations and has special support for this.
  1794. */
  1795. q->io_modes = VB2_MMAP | VB2_READ;
  1796. if (saa7134_userptr)
  1797. q->io_modes |= VB2_USERPTR;
  1798. q->drv_priv = &dev->video_q;
  1799. q->ops = &vb2_qops;
  1800. q->gfp_flags = GFP_DMA32;
  1801. q->mem_ops = &vb2_dma_sg_memops;
  1802. q->buf_struct_size = sizeof(struct saa7134_buf);
  1803. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1804. q->lock = &dev->lock;
  1805. ret = vb2_queue_init(q);
  1806. if (ret)
  1807. return ret;
  1808. saa7134_pgtable_alloc(dev->pci, &dev->video_q.pt);
  1809. q = &dev->vbi_vbq;
  1810. q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1811. /* Don't add VB2_USERPTR, see comment above */
  1812. q->io_modes = VB2_MMAP | VB2_READ;
  1813. if (saa7134_userptr)
  1814. q->io_modes |= VB2_USERPTR;
  1815. q->drv_priv = &dev->vbi_q;
  1816. q->ops = &saa7134_vbi_qops;
  1817. q->gfp_flags = GFP_DMA32;
  1818. q->mem_ops = &vb2_dma_sg_memops;
  1819. q->buf_struct_size = sizeof(struct saa7134_buf);
  1820. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1821. q->lock = &dev->lock;
  1822. ret = vb2_queue_init(q);
  1823. if (ret)
  1824. return ret;
  1825. saa7134_pgtable_alloc(dev->pci, &dev->vbi_q.pt);
  1826. return 0;
  1827. }
  1828. void saa7134_video_fini(struct saa7134_dev *dev)
  1829. {
  1830. /* free stuff */
  1831. vb2_queue_release(&dev->video_vbq);
  1832. saa7134_pgtable_free(dev->pci, &dev->video_q.pt);
  1833. vb2_queue_release(&dev->vbi_vbq);
  1834. saa7134_pgtable_free(dev->pci, &dev->vbi_q.pt);
  1835. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  1836. if (card_has_radio(dev))
  1837. v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
  1838. }
  1839. int saa7134_videoport_init(struct saa7134_dev *dev)
  1840. {
  1841. /* enable video output */
  1842. int vo = saa7134_boards[dev->board].video_out;
  1843. int video_reg;
  1844. unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
  1845. /* Configure videoport */
  1846. saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
  1847. video_reg = video_out[vo][1];
  1848. if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
  1849. video_reg &= ~VP_T_CODE_P_INVERTED;
  1850. saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
  1851. saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
  1852. saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
  1853. video_reg = video_out[vo][5];
  1854. if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
  1855. video_reg &= ~VP_CLK_CTRL2_DELAYED;
  1856. if (vid_port_opts & SET_CLOCK_INVERTED)
  1857. video_reg |= VP_CLK_CTRL1_INVERTED;
  1858. saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
  1859. video_reg = video_out[vo][6];
  1860. if (vid_port_opts & SET_VSYNC_OFF) {
  1861. video_reg &= ~VP_VS_TYPE_MASK;
  1862. video_reg |= VP_VS_TYPE_OFF;
  1863. }
  1864. saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
  1865. saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
  1866. saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
  1867. /* Start videoport */
  1868. saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
  1869. return 0;
  1870. }
  1871. int saa7134_video_init2(struct saa7134_dev *dev)
  1872. {
  1873. /* init video hw */
  1874. set_tvnorm(dev,&tvnorms[0]);
  1875. video_mux(dev,0);
  1876. v4l2_ctrl_handler_setup(&dev->ctrl_handler);
  1877. saa7134_tvaudio_setmute(dev);
  1878. saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
  1879. return 0;
  1880. }
  1881. void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
  1882. {
  1883. static const char *st[] = {
  1884. "(no signal)", "NTSC", "PAL", "SECAM" };
  1885. u32 st1,st2;
  1886. st1 = saa_readb(SAA7134_STATUS_VIDEO1);
  1887. st2 = saa_readb(SAA7134_STATUS_VIDEO2);
  1888. video_dbg("DCSDT: pll: %s, sync: %s, norm: %s\n",
  1889. (st1 & 0x40) ? "not locked" : "locked",
  1890. (st2 & 0x40) ? "no" : "yes",
  1891. st[st1 & 0x03]);
  1892. dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
  1893. if (dev->nosignal) {
  1894. /* no video signal -> mute audio */
  1895. if (dev->ctl_automute)
  1896. dev->automute = 1;
  1897. saa7134_tvaudio_setmute(dev);
  1898. } else {
  1899. /* wake up tvaudio audio carrier scan thread */
  1900. saa7134_tvaudio_do_scan(dev);
  1901. }
  1902. if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
  1903. saa_clearb(SAA7134_SYNC_CTRL, 0x20);
  1904. else
  1905. saa_setb(SAA7134_SYNC_CTRL, 0x20);
  1906. if (dev->mops && dev->mops->signal_change)
  1907. dev->mops->signal_change(dev);
  1908. }
  1909. void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
  1910. {
  1911. enum v4l2_field field;
  1912. spin_lock(&dev->slock);
  1913. if (dev->video_q.curr) {
  1914. field = dev->field;
  1915. if (V4L2_FIELD_HAS_BOTH(field)) {
  1916. /* make sure we have seen both fields */
  1917. if ((status & 0x10) == 0x00) {
  1918. dev->video_q.curr->top_seen = 1;
  1919. goto done;
  1920. }
  1921. if (!dev->video_q.curr->top_seen)
  1922. goto done;
  1923. } else if (field == V4L2_FIELD_TOP) {
  1924. if ((status & 0x10) != 0x10)
  1925. goto done;
  1926. } else if (field == V4L2_FIELD_BOTTOM) {
  1927. if ((status & 0x10) != 0x00)
  1928. goto done;
  1929. }
  1930. saa7134_buffer_finish(dev, &dev->video_q, VB2_BUF_STATE_DONE);
  1931. }
  1932. saa7134_buffer_next(dev, &dev->video_q);
  1933. done:
  1934. spin_unlock(&dev->slock);
  1935. }