saa7134-video.c 67 KB

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