saa7134-video.c 67 KB

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