saa7134-video.c 68 KB

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