saa7134-video.c 67 KB

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