saa7134-video.c 66 KB

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