saa7134-video.c 63 KB

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