saa7134-video.c 67 KB

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