saa7134-video.c 67 KB

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