saa7134-video.c 59 KB

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