saa7134-video.c 61 KB

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