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