saa7134-video.c 67 KB

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