saa7134-video.c 66 KB

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