saa7134-video.c 59 KB

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