saa7134-video.c 60 KB

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