saa7134-video.c 67 KB

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