saa7134-video.c 66 KB

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