saa7134-video.c 66 KB

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