saa7134-video.c 63 KB

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