cx88-video.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189
  1. /*
  2. *
  3. * device driver for Conexant 2388x based TV cards
  4. * video4linux video interface
  5. *
  6. * (c) 2003-04 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/moduleparam.h>
  26. #include <linux/kmod.h>
  27. #include <linux/kernel.h>
  28. #include <linux/slab.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/delay.h>
  31. #include <linux/kthread.h>
  32. #include <asm/div64.h>
  33. #include "cx88.h"
  34. #include <media/v4l2-common.h>
  35. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  36. /* Include V4L1 specific functions. Should be removed soon */
  37. #include <linux/videodev.h>
  38. #endif
  39. MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
  40. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
  41. MODULE_LICENSE("GPL");
  42. /* ------------------------------------------------------------------ */
  43. static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
  44. static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
  45. static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
  46. module_param_array(video_nr, int, NULL, 0444);
  47. module_param_array(vbi_nr, int, NULL, 0444);
  48. module_param_array(radio_nr, int, NULL, 0444);
  49. MODULE_PARM_DESC(video_nr,"video device numbers");
  50. MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
  51. MODULE_PARM_DESC(radio_nr,"radio device numbers");
  52. static unsigned int video_debug = 0;
  53. module_param(video_debug,int,0644);
  54. MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
  55. static unsigned int irq_debug = 0;
  56. module_param(irq_debug,int,0644);
  57. MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
  58. static unsigned int vid_limit = 16;
  59. module_param(vid_limit,int,0644);
  60. MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
  61. #define dprintk(level,fmt, arg...) if (video_debug >= level) \
  62. printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
  63. /* ------------------------------------------------------------------ */
  64. static LIST_HEAD(cx8800_devlist);
  65. /* ------------------------------------------------------------------- */
  66. /* static data */
  67. static struct cx88_tvnorm tvnorms[] = {
  68. {
  69. .name = "NTSC-M",
  70. .id = V4L2_STD_NTSC_M,
  71. .cxiformat = VideoFormatNTSC,
  72. .cxoformat = 0x181f0008,
  73. },{
  74. .name = "NTSC-JP",
  75. .id = V4L2_STD_NTSC_M_JP,
  76. .cxiformat = VideoFormatNTSCJapan,
  77. .cxoformat = 0x181f0008,
  78. },{
  79. .name = "PAL-BG",
  80. .id = V4L2_STD_PAL_BG,
  81. .cxiformat = VideoFormatPAL,
  82. .cxoformat = 0x181f0008,
  83. },{
  84. .name = "PAL-DK",
  85. .id = V4L2_STD_PAL_DK,
  86. .cxiformat = VideoFormatPAL,
  87. .cxoformat = 0x181f0008,
  88. },{
  89. .name = "PAL-I",
  90. .id = V4L2_STD_PAL_I,
  91. .cxiformat = VideoFormatPAL,
  92. .cxoformat = 0x181f0008,
  93. },{
  94. .name = "PAL-M",
  95. .id = V4L2_STD_PAL_M,
  96. .cxiformat = VideoFormatPALM,
  97. .cxoformat = 0x1c1f0008,
  98. },{
  99. .name = "PAL-N",
  100. .id = V4L2_STD_PAL_N,
  101. .cxiformat = VideoFormatPALN,
  102. .cxoformat = 0x1c1f0008,
  103. },{
  104. .name = "PAL-Nc",
  105. .id = V4L2_STD_PAL_Nc,
  106. .cxiformat = VideoFormatPALNC,
  107. .cxoformat = 0x1c1f0008,
  108. },{
  109. .name = "PAL-60",
  110. .id = V4L2_STD_PAL_60,
  111. .cxiformat = VideoFormatPAL60,
  112. .cxoformat = 0x181f0008,
  113. },{
  114. .name = "SECAM-L",
  115. .id = V4L2_STD_SECAM_L,
  116. .cxiformat = VideoFormatSECAM,
  117. .cxoformat = 0x181f0008,
  118. },{
  119. .name = "SECAM-DK",
  120. .id = V4L2_STD_SECAM_DK,
  121. .cxiformat = VideoFormatSECAM,
  122. .cxoformat = 0x181f0008,
  123. }
  124. };
  125. static struct cx8800_fmt formats[] = {
  126. {
  127. .name = "8 bpp, gray",
  128. .fourcc = V4L2_PIX_FMT_GREY,
  129. .cxformat = ColorFormatY8,
  130. .depth = 8,
  131. .flags = FORMAT_FLAGS_PACKED,
  132. },{
  133. .name = "15 bpp RGB, le",
  134. .fourcc = V4L2_PIX_FMT_RGB555,
  135. .cxformat = ColorFormatRGB15,
  136. .depth = 16,
  137. .flags = FORMAT_FLAGS_PACKED,
  138. },{
  139. .name = "15 bpp RGB, be",
  140. .fourcc = V4L2_PIX_FMT_RGB555X,
  141. .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
  142. .depth = 16,
  143. .flags = FORMAT_FLAGS_PACKED,
  144. },{
  145. .name = "16 bpp RGB, le",
  146. .fourcc = V4L2_PIX_FMT_RGB565,
  147. .cxformat = ColorFormatRGB16,
  148. .depth = 16,
  149. .flags = FORMAT_FLAGS_PACKED,
  150. },{
  151. .name = "16 bpp RGB, be",
  152. .fourcc = V4L2_PIX_FMT_RGB565X,
  153. .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
  154. .depth = 16,
  155. .flags = FORMAT_FLAGS_PACKED,
  156. },{
  157. .name = "24 bpp RGB, le",
  158. .fourcc = V4L2_PIX_FMT_BGR24,
  159. .cxformat = ColorFormatRGB24,
  160. .depth = 24,
  161. .flags = FORMAT_FLAGS_PACKED,
  162. },{
  163. .name = "32 bpp RGB, le",
  164. .fourcc = V4L2_PIX_FMT_BGR32,
  165. .cxformat = ColorFormatRGB32,
  166. .depth = 32,
  167. .flags = FORMAT_FLAGS_PACKED,
  168. },{
  169. .name = "32 bpp RGB, be",
  170. .fourcc = V4L2_PIX_FMT_RGB32,
  171. .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
  172. .depth = 32,
  173. .flags = FORMAT_FLAGS_PACKED,
  174. },{
  175. .name = "4:2:2, packed, YUYV",
  176. .fourcc = V4L2_PIX_FMT_YUYV,
  177. .cxformat = ColorFormatYUY2,
  178. .depth = 16,
  179. .flags = FORMAT_FLAGS_PACKED,
  180. },{
  181. .name = "4:2:2, packed, UYVY",
  182. .fourcc = V4L2_PIX_FMT_UYVY,
  183. .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
  184. .depth = 16,
  185. .flags = FORMAT_FLAGS_PACKED,
  186. },
  187. };
  188. static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
  189. {
  190. unsigned int i;
  191. for (i = 0; i < ARRAY_SIZE(formats); i++)
  192. if (formats[i].fourcc == fourcc)
  193. return formats+i;
  194. return NULL;
  195. }
  196. /* ------------------------------------------------------------------- */
  197. static const struct v4l2_queryctrl no_ctl = {
  198. .name = "42",
  199. .flags = V4L2_CTRL_FLAG_DISABLED,
  200. };
  201. static struct cx88_ctrl cx8800_ctls[] = {
  202. /* --- video --- */
  203. {
  204. .v = {
  205. .id = V4L2_CID_BRIGHTNESS,
  206. .name = "Brightness",
  207. .minimum = 0x00,
  208. .maximum = 0xff,
  209. .step = 1,
  210. .default_value = 0x7f,
  211. .type = V4L2_CTRL_TYPE_INTEGER,
  212. },
  213. .off = 128,
  214. .reg = MO_CONTR_BRIGHT,
  215. .mask = 0x00ff,
  216. .shift = 0,
  217. },{
  218. .v = {
  219. .id = V4L2_CID_CONTRAST,
  220. .name = "Contrast",
  221. .minimum = 0,
  222. .maximum = 0xff,
  223. .step = 1,
  224. .default_value = 0x3f,
  225. .type = V4L2_CTRL_TYPE_INTEGER,
  226. },
  227. .off = 0,
  228. .reg = MO_CONTR_BRIGHT,
  229. .mask = 0xff00,
  230. .shift = 8,
  231. },{
  232. .v = {
  233. .id = V4L2_CID_HUE,
  234. .name = "Hue",
  235. .minimum = 0,
  236. .maximum = 0xff,
  237. .step = 1,
  238. .default_value = 0x7f,
  239. .type = V4L2_CTRL_TYPE_INTEGER,
  240. },
  241. .off = 128,
  242. .reg = MO_HUE,
  243. .mask = 0x00ff,
  244. .shift = 0,
  245. },{
  246. /* strictly, this only describes only U saturation.
  247. * V saturation is handled specially through code.
  248. */
  249. .v = {
  250. .id = V4L2_CID_SATURATION,
  251. .name = "Saturation",
  252. .minimum = 0,
  253. .maximum = 0xff,
  254. .step = 1,
  255. .default_value = 0x7f,
  256. .type = V4L2_CTRL_TYPE_INTEGER,
  257. },
  258. .off = 0,
  259. .reg = MO_UV_SATURATION,
  260. .mask = 0x00ff,
  261. .shift = 0,
  262. },{
  263. /* --- audio --- */
  264. .v = {
  265. .id = V4L2_CID_AUDIO_MUTE,
  266. .name = "Mute",
  267. .minimum = 0,
  268. .maximum = 1,
  269. .default_value = 1,
  270. .type = V4L2_CTRL_TYPE_BOOLEAN,
  271. },
  272. .reg = AUD_VOL_CTL,
  273. .sreg = SHADOW_AUD_VOL_CTL,
  274. .mask = (1 << 6),
  275. .shift = 6,
  276. },{
  277. .v = {
  278. .id = V4L2_CID_AUDIO_VOLUME,
  279. .name = "Volume",
  280. .minimum = 0,
  281. .maximum = 0x3f,
  282. .step = 1,
  283. .default_value = 0x3f,
  284. .type = V4L2_CTRL_TYPE_INTEGER,
  285. },
  286. .reg = AUD_VOL_CTL,
  287. .sreg = SHADOW_AUD_VOL_CTL,
  288. .mask = 0x3f,
  289. .shift = 0,
  290. },{
  291. .v = {
  292. .id = V4L2_CID_AUDIO_BALANCE,
  293. .name = "Balance",
  294. .minimum = 0,
  295. .maximum = 0x7f,
  296. .step = 1,
  297. .default_value = 0x40,
  298. .type = V4L2_CTRL_TYPE_INTEGER,
  299. },
  300. .reg = AUD_BAL_CTL,
  301. .sreg = SHADOW_AUD_BAL_CTL,
  302. .mask = 0x7f,
  303. .shift = 0,
  304. }
  305. };
  306. static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
  307. const u32 cx88_user_ctrls[] = {
  308. V4L2_CID_USER_CLASS,
  309. V4L2_CID_BRIGHTNESS,
  310. V4L2_CID_CONTRAST,
  311. V4L2_CID_SATURATION,
  312. V4L2_CID_HUE,
  313. V4L2_CID_AUDIO_VOLUME,
  314. V4L2_CID_AUDIO_BALANCE,
  315. V4L2_CID_AUDIO_MUTE,
  316. 0
  317. };
  318. EXPORT_SYMBOL(cx88_user_ctrls);
  319. static const u32 *ctrl_classes[] = {
  320. cx88_user_ctrls,
  321. NULL
  322. };
  323. int cx8800_ctrl_query(struct v4l2_queryctrl *qctrl)
  324. {
  325. int i;
  326. if (qctrl->id < V4L2_CID_BASE ||
  327. qctrl->id >= V4L2_CID_LASTP1)
  328. return -EINVAL;
  329. for (i = 0; i < CX8800_CTLS; i++)
  330. if (cx8800_ctls[i].v.id == qctrl->id)
  331. break;
  332. if (i == CX8800_CTLS) {
  333. *qctrl = no_ctl;
  334. return 0;
  335. }
  336. *qctrl = cx8800_ctls[i].v;
  337. return 0;
  338. }
  339. EXPORT_SYMBOL(cx8800_ctrl_query);
  340. static int cx88_queryctrl(struct v4l2_queryctrl *qctrl)
  341. {
  342. qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
  343. if (qctrl->id == 0)
  344. return -EINVAL;
  345. return cx8800_ctrl_query(qctrl);
  346. }
  347. /* ------------------------------------------------------------------- */
  348. /* resource management */
  349. static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
  350. {
  351. struct cx88_core *core = dev->core;
  352. if (fh->resources & bit)
  353. /* have it already allocated */
  354. return 1;
  355. /* is it free? */
  356. mutex_lock(&core->lock);
  357. if (dev->resources & bit) {
  358. /* no, someone else uses it */
  359. mutex_unlock(&core->lock);
  360. return 0;
  361. }
  362. /* it's free, grab it */
  363. fh->resources |= bit;
  364. dev->resources |= bit;
  365. dprintk(1,"res: get %d\n",bit);
  366. mutex_unlock(&core->lock);
  367. return 1;
  368. }
  369. static
  370. int res_check(struct cx8800_fh *fh, unsigned int bit)
  371. {
  372. return (fh->resources & bit);
  373. }
  374. static
  375. int res_locked(struct cx8800_dev *dev, unsigned int bit)
  376. {
  377. return (dev->resources & bit);
  378. }
  379. static
  380. void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
  381. {
  382. struct cx88_core *core = dev->core;
  383. BUG_ON((fh->resources & bits) != bits);
  384. mutex_lock(&core->lock);
  385. fh->resources &= ~bits;
  386. dev->resources &= ~bits;
  387. dprintk(1,"res: put %d\n",bits);
  388. mutex_unlock(&core->lock);
  389. }
  390. /* ------------------------------------------------------------------ */
  391. /* static int video_mux(struct cx8800_dev *dev, unsigned int input) */
  392. static int video_mux(struct cx88_core *core, unsigned int input)
  393. {
  394. /* struct cx88_core *core = dev->core; */
  395. dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
  396. input, INPUT(input)->vmux,
  397. INPUT(input)->gpio0,INPUT(input)->gpio1,
  398. INPUT(input)->gpio2,INPUT(input)->gpio3);
  399. core->input = input;
  400. cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input)->vmux << 14);
  401. cx_write(MO_GP3_IO, INPUT(input)->gpio3);
  402. cx_write(MO_GP0_IO, INPUT(input)->gpio0);
  403. cx_write(MO_GP1_IO, INPUT(input)->gpio1);
  404. cx_write(MO_GP2_IO, INPUT(input)->gpio2);
  405. switch (INPUT(input)->type) {
  406. case CX88_VMUX_SVIDEO:
  407. cx_set(MO_AFECFG_IO, 0x00000001);
  408. cx_set(MO_INPUT_FORMAT, 0x00010010);
  409. cx_set(MO_FILTER_EVEN, 0x00002020);
  410. cx_set(MO_FILTER_ODD, 0x00002020);
  411. break;
  412. default:
  413. cx_clear(MO_AFECFG_IO, 0x00000001);
  414. cx_clear(MO_INPUT_FORMAT, 0x00010010);
  415. cx_clear(MO_FILTER_EVEN, 0x00002020);
  416. cx_clear(MO_FILTER_ODD, 0x00002020);
  417. break;
  418. }
  419. if (cx88_boards[core->board].mpeg & CX88_MPEG_BLACKBIRD) {
  420. /* sets sound input from external adc */
  421. if (INPUT(input)->extadc)
  422. cx_set(AUD_CTL, EN_I2SIN_ENABLE);
  423. else
  424. cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
  425. }
  426. return 0;
  427. }
  428. /* ------------------------------------------------------------------ */
  429. static int start_video_dma(struct cx8800_dev *dev,
  430. struct cx88_dmaqueue *q,
  431. struct cx88_buffer *buf)
  432. {
  433. struct cx88_core *core = dev->core;
  434. /* setup fifo + format */
  435. cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
  436. buf->bpl, buf->risc.dma);
  437. cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
  438. cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
  439. /* reset counter */
  440. cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
  441. q->count = 1;
  442. /* enable irqs */
  443. cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
  444. /* Enables corresponding bits at PCI_INT_STAT:
  445. bits 0 to 4: video, audio, transport stream, VIP, Host
  446. bit 7: timer
  447. bits 8 and 9: DMA complete for: SRC, DST
  448. bits 10 and 11: BERR signal asserted for RISC: RD, WR
  449. bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
  450. */
  451. cx_set(MO_VID_INTMSK, 0x0f0011);
  452. /* enable capture */
  453. cx_set(VID_CAPTURE_CONTROL,0x06);
  454. /* start dma */
  455. cx_set(MO_DEV_CNTRL2, (1<<5));
  456. cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
  457. return 0;
  458. }
  459. #ifdef CONFIG_PM
  460. static int stop_video_dma(struct cx8800_dev *dev)
  461. {
  462. struct cx88_core *core = dev->core;
  463. /* stop dma */
  464. cx_clear(MO_VID_DMACNTRL, 0x11);
  465. /* disable capture */
  466. cx_clear(VID_CAPTURE_CONTROL,0x06);
  467. /* disable irqs */
  468. cx_clear(MO_PCI_INTMSK, 0x000001);
  469. cx_clear(MO_VID_INTMSK, 0x0f0011);
  470. return 0;
  471. }
  472. #endif
  473. static int restart_video_queue(struct cx8800_dev *dev,
  474. struct cx88_dmaqueue *q)
  475. {
  476. struct cx88_core *core = dev->core;
  477. struct cx88_buffer *buf, *prev;
  478. struct list_head *item;
  479. if (!list_empty(&q->active)) {
  480. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  481. dprintk(2,"restart_queue [%p/%d]: restart dma\n",
  482. buf, buf->vb.i);
  483. start_video_dma(dev, q, buf);
  484. list_for_each(item,&q->active) {
  485. buf = list_entry(item, struct cx88_buffer, vb.queue);
  486. buf->count = q->count++;
  487. }
  488. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  489. return 0;
  490. }
  491. prev = NULL;
  492. for (;;) {
  493. if (list_empty(&q->queued))
  494. return 0;
  495. buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
  496. if (NULL == prev) {
  497. list_move_tail(&buf->vb.queue, &q->active);
  498. start_video_dma(dev, q, buf);
  499. buf->vb.state = STATE_ACTIVE;
  500. buf->count = q->count++;
  501. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  502. dprintk(2,"[%p/%d] restart_queue - first active\n",
  503. buf,buf->vb.i);
  504. } else if (prev->vb.width == buf->vb.width &&
  505. prev->vb.height == buf->vb.height &&
  506. prev->fmt == buf->fmt) {
  507. list_move_tail(&buf->vb.queue, &q->active);
  508. buf->vb.state = STATE_ACTIVE;
  509. buf->count = q->count++;
  510. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  511. dprintk(2,"[%p/%d] restart_queue - move to active\n",
  512. buf,buf->vb.i);
  513. } else {
  514. return 0;
  515. }
  516. prev = buf;
  517. }
  518. }
  519. /* ------------------------------------------------------------------ */
  520. static int
  521. buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  522. {
  523. struct cx8800_fh *fh = q->priv_data;
  524. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  525. if (0 == *count)
  526. *count = 32;
  527. while (*size * *count > vid_limit * 1024 * 1024)
  528. (*count)--;
  529. return 0;
  530. }
  531. static int
  532. buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  533. enum v4l2_field field)
  534. {
  535. struct cx8800_fh *fh = q->priv_data;
  536. struct cx8800_dev *dev = fh->dev;
  537. struct cx88_core *core = dev->core;
  538. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  539. int rc, init_buffer = 0;
  540. BUG_ON(NULL == fh->fmt);
  541. if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
  542. fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
  543. return -EINVAL;
  544. buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
  545. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  546. return -EINVAL;
  547. if (buf->fmt != fh->fmt ||
  548. buf->vb.width != fh->width ||
  549. buf->vb.height != fh->height ||
  550. buf->vb.field != field) {
  551. buf->fmt = fh->fmt;
  552. buf->vb.width = fh->width;
  553. buf->vb.height = fh->height;
  554. buf->vb.field = field;
  555. init_buffer = 1;
  556. }
  557. if (STATE_NEEDS_INIT == buf->vb.state) {
  558. init_buffer = 1;
  559. if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
  560. goto fail;
  561. }
  562. if (init_buffer) {
  563. buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
  564. switch (buf->vb.field) {
  565. case V4L2_FIELD_TOP:
  566. cx88_risc_buffer(dev->pci, &buf->risc,
  567. buf->vb.dma.sglist, 0, UNSET,
  568. buf->bpl, 0, buf->vb.height);
  569. break;
  570. case V4L2_FIELD_BOTTOM:
  571. cx88_risc_buffer(dev->pci, &buf->risc,
  572. buf->vb.dma.sglist, UNSET, 0,
  573. buf->bpl, 0, buf->vb.height);
  574. break;
  575. case V4L2_FIELD_INTERLACED:
  576. cx88_risc_buffer(dev->pci, &buf->risc,
  577. buf->vb.dma.sglist, 0, buf->bpl,
  578. buf->bpl, buf->bpl,
  579. buf->vb.height >> 1);
  580. break;
  581. case V4L2_FIELD_SEQ_TB:
  582. cx88_risc_buffer(dev->pci, &buf->risc,
  583. buf->vb.dma.sglist,
  584. 0, buf->bpl * (buf->vb.height >> 1),
  585. buf->bpl, 0,
  586. buf->vb.height >> 1);
  587. break;
  588. case V4L2_FIELD_SEQ_BT:
  589. cx88_risc_buffer(dev->pci, &buf->risc,
  590. buf->vb.dma.sglist,
  591. buf->bpl * (buf->vb.height >> 1), 0,
  592. buf->bpl, 0,
  593. buf->vb.height >> 1);
  594. break;
  595. default:
  596. BUG();
  597. }
  598. }
  599. dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
  600. buf, buf->vb.i,
  601. fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
  602. (unsigned long)buf->risc.dma);
  603. buf->vb.state = STATE_PREPARED;
  604. return 0;
  605. fail:
  606. cx88_free_buffer(q,buf);
  607. return rc;
  608. }
  609. static void
  610. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  611. {
  612. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  613. struct cx88_buffer *prev;
  614. struct cx8800_fh *fh = vq->priv_data;
  615. struct cx8800_dev *dev = fh->dev;
  616. struct cx88_core *core = dev->core;
  617. struct cx88_dmaqueue *q = &dev->vidq;
  618. /* add jump to stopper */
  619. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  620. buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
  621. if (!list_empty(&q->queued)) {
  622. list_add_tail(&buf->vb.queue,&q->queued);
  623. buf->vb.state = STATE_QUEUED;
  624. dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
  625. buf, buf->vb.i);
  626. } else if (list_empty(&q->active)) {
  627. list_add_tail(&buf->vb.queue,&q->active);
  628. start_video_dma(dev, q, buf);
  629. buf->vb.state = STATE_ACTIVE;
  630. buf->count = q->count++;
  631. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  632. dprintk(2,"[%p/%d] buffer_queue - first active\n",
  633. buf, buf->vb.i);
  634. } else {
  635. prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
  636. if (prev->vb.width == buf->vb.width &&
  637. prev->vb.height == buf->vb.height &&
  638. prev->fmt == buf->fmt) {
  639. list_add_tail(&buf->vb.queue,&q->active);
  640. buf->vb.state = STATE_ACTIVE;
  641. buf->count = q->count++;
  642. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  643. dprintk(2,"[%p/%d] buffer_queue - append to active\n",
  644. buf, buf->vb.i);
  645. } else {
  646. list_add_tail(&buf->vb.queue,&q->queued);
  647. buf->vb.state = STATE_QUEUED;
  648. dprintk(2,"[%p/%d] buffer_queue - first queued\n",
  649. buf, buf->vb.i);
  650. }
  651. }
  652. }
  653. static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  654. {
  655. struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
  656. cx88_free_buffer(q,buf);
  657. }
  658. static struct videobuf_queue_ops cx8800_video_qops = {
  659. .buf_setup = buffer_setup,
  660. .buf_prepare = buffer_prepare,
  661. .buf_queue = buffer_queue,
  662. .buf_release = buffer_release,
  663. };
  664. /* ------------------------------------------------------------------ */
  665. /* ------------------------------------------------------------------ */
  666. static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
  667. {
  668. switch (fh->type) {
  669. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  670. return &fh->vidq;
  671. case V4L2_BUF_TYPE_VBI_CAPTURE:
  672. return &fh->vbiq;
  673. default:
  674. BUG();
  675. return NULL;
  676. }
  677. }
  678. static int get_ressource(struct cx8800_fh *fh)
  679. {
  680. switch (fh->type) {
  681. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  682. return RESOURCE_VIDEO;
  683. case V4L2_BUF_TYPE_VBI_CAPTURE:
  684. return RESOURCE_VBI;
  685. default:
  686. BUG();
  687. return 0;
  688. }
  689. }
  690. static int video_open(struct inode *inode, struct file *file)
  691. {
  692. int minor = iminor(inode);
  693. struct cx8800_dev *h,*dev = NULL;
  694. struct cx88_core *core;
  695. struct cx8800_fh *fh;
  696. struct list_head *list;
  697. enum v4l2_buf_type type = 0;
  698. int radio = 0;
  699. list_for_each(list,&cx8800_devlist) {
  700. h = list_entry(list, struct cx8800_dev, devlist);
  701. if (h->video_dev->minor == minor) {
  702. dev = h;
  703. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  704. }
  705. if (h->vbi_dev->minor == minor) {
  706. dev = h;
  707. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  708. }
  709. if (h->radio_dev &&
  710. h->radio_dev->minor == minor) {
  711. radio = 1;
  712. dev = h;
  713. }
  714. }
  715. if (NULL == dev)
  716. return -ENODEV;
  717. core = dev->core;
  718. dprintk(1,"open minor=%d radio=%d type=%s\n",
  719. minor,radio,v4l2_type_names[type]);
  720. /* allocate + initialize per filehandle data */
  721. fh = kzalloc(sizeof(*fh),GFP_KERNEL);
  722. if (NULL == fh)
  723. return -ENOMEM;
  724. file->private_data = fh;
  725. fh->dev = dev;
  726. fh->radio = radio;
  727. fh->type = type;
  728. fh->width = 320;
  729. fh->height = 240;
  730. fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  731. videobuf_queue_init(&fh->vidq, &cx8800_video_qops,
  732. dev->pci, &dev->slock,
  733. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  734. V4L2_FIELD_INTERLACED,
  735. sizeof(struct cx88_buffer),
  736. fh);
  737. videobuf_queue_init(&fh->vbiq, &cx8800_vbi_qops,
  738. dev->pci, &dev->slock,
  739. V4L2_BUF_TYPE_VBI_CAPTURE,
  740. V4L2_FIELD_SEQ_TB,
  741. sizeof(struct cx88_buffer),
  742. fh);
  743. if (fh->radio) {
  744. int board = core->board;
  745. dprintk(1,"video_open: setting radio device\n");
  746. cx_write(MO_GP3_IO, cx88_boards[board].radio.gpio3);
  747. cx_write(MO_GP0_IO, cx88_boards[board].radio.gpio0);
  748. cx_write(MO_GP1_IO, cx88_boards[board].radio.gpio1);
  749. cx_write(MO_GP2_IO, cx88_boards[board].radio.gpio2);
  750. core->tvaudio = WW_FM;
  751. cx88_set_tvaudio(core);
  752. cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
  753. cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
  754. }
  755. return 0;
  756. }
  757. static ssize_t
  758. video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  759. {
  760. struct cx8800_fh *fh = file->private_data;
  761. switch (fh->type) {
  762. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  763. if (res_locked(fh->dev,RESOURCE_VIDEO))
  764. return -EBUSY;
  765. return videobuf_read_one(&fh->vidq, data, count, ppos,
  766. file->f_flags & O_NONBLOCK);
  767. case V4L2_BUF_TYPE_VBI_CAPTURE:
  768. if (!res_get(fh->dev,fh,RESOURCE_VBI))
  769. return -EBUSY;
  770. return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
  771. file->f_flags & O_NONBLOCK);
  772. default:
  773. BUG();
  774. return 0;
  775. }
  776. }
  777. static unsigned int
  778. video_poll(struct file *file, struct poll_table_struct *wait)
  779. {
  780. struct cx8800_fh *fh = file->private_data;
  781. struct cx88_buffer *buf;
  782. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  783. if (!res_get(fh->dev,fh,RESOURCE_VBI))
  784. return POLLERR;
  785. return videobuf_poll_stream(file, &fh->vbiq, wait);
  786. }
  787. if (res_check(fh,RESOURCE_VIDEO)) {
  788. /* streaming capture */
  789. if (list_empty(&fh->vidq.stream))
  790. return POLLERR;
  791. buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
  792. } else {
  793. /* read() capture */
  794. buf = (struct cx88_buffer*)fh->vidq.read_buf;
  795. if (NULL == buf)
  796. return POLLERR;
  797. }
  798. poll_wait(file, &buf->vb.done, wait);
  799. if (buf->vb.state == STATE_DONE ||
  800. buf->vb.state == STATE_ERROR)
  801. return POLLIN|POLLRDNORM;
  802. return 0;
  803. }
  804. static int video_release(struct inode *inode, struct file *file)
  805. {
  806. struct cx8800_fh *fh = file->private_data;
  807. struct cx8800_dev *dev = fh->dev;
  808. /* turn off overlay */
  809. if (res_check(fh, RESOURCE_OVERLAY)) {
  810. /* FIXME */
  811. res_free(dev,fh,RESOURCE_OVERLAY);
  812. }
  813. /* stop video capture */
  814. if (res_check(fh, RESOURCE_VIDEO)) {
  815. videobuf_queue_cancel(&fh->vidq);
  816. res_free(dev,fh,RESOURCE_VIDEO);
  817. }
  818. if (fh->vidq.read_buf) {
  819. buffer_release(&fh->vidq,fh->vidq.read_buf);
  820. kfree(fh->vidq.read_buf);
  821. }
  822. /* stop vbi capture */
  823. if (res_check(fh, RESOURCE_VBI)) {
  824. if (fh->vbiq.streaming)
  825. videobuf_streamoff(&fh->vbiq);
  826. if (fh->vbiq.reading)
  827. videobuf_read_stop(&fh->vbiq);
  828. res_free(dev,fh,RESOURCE_VBI);
  829. }
  830. videobuf_mmap_free(&fh->vidq);
  831. videobuf_mmap_free(&fh->vbiq);
  832. file->private_data = NULL;
  833. kfree(fh);
  834. cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
  835. return 0;
  836. }
  837. static int
  838. video_mmap(struct file *file, struct vm_area_struct * vma)
  839. {
  840. struct cx8800_fh *fh = file->private_data;
  841. return videobuf_mmap_mapper(get_queue(fh), vma);
  842. }
  843. /* ------------------------------------------------------------------ */
  844. /* static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
  845. static int get_control(struct cx88_core *core, struct v4l2_control *ctl)
  846. {
  847. /* struct cx88_core *core = dev->core; */
  848. struct cx88_ctrl *c = NULL;
  849. u32 value;
  850. int i;
  851. for (i = 0; i < CX8800_CTLS; i++)
  852. if (cx8800_ctls[i].v.id == ctl->id)
  853. c = &cx8800_ctls[i];
  854. if (NULL == c)
  855. return -EINVAL;
  856. value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
  857. switch (ctl->id) {
  858. case V4L2_CID_AUDIO_BALANCE:
  859. ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
  860. : (0x7f - (value & 0x7f));
  861. break;
  862. case V4L2_CID_AUDIO_VOLUME:
  863. ctl->value = 0x3f - (value & 0x3f);
  864. break;
  865. default:
  866. ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
  867. break;
  868. }
  869. dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
  870. ctl->id, c->v.name, ctl->value, c->reg,
  871. value,c->mask, c->sreg ? " [shadowed]" : "");
  872. return 0;
  873. }
  874. /* static int set_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
  875. static int set_control(struct cx88_core *core, struct v4l2_control *ctl)
  876. {
  877. /* struct cx88_core *core = dev->core; */
  878. struct cx88_ctrl *c = NULL;
  879. u32 value,mask;
  880. int i;
  881. for (i = 0; i < CX8800_CTLS; i++) {
  882. if (cx8800_ctls[i].v.id == ctl->id) {
  883. c = &cx8800_ctls[i];
  884. }
  885. }
  886. if (NULL == c)
  887. return -EINVAL;
  888. if (ctl->value < c->v.minimum)
  889. ctl->value = c->v.minimum;
  890. if (ctl->value > c->v.maximum)
  891. ctl->value = c->v.maximum;
  892. mask=c->mask;
  893. switch (ctl->id) {
  894. case V4L2_CID_AUDIO_BALANCE:
  895. value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
  896. break;
  897. case V4L2_CID_AUDIO_VOLUME:
  898. value = 0x3f - (ctl->value & 0x3f);
  899. break;
  900. case V4L2_CID_SATURATION:
  901. /* special v_sat handling */
  902. value = ((ctl->value - c->off) << c->shift) & c->mask;
  903. if (core->tvnorm->id & V4L2_STD_SECAM) {
  904. /* For SECAM, both U and V sat should be equal */
  905. value=value<<8|value;
  906. } else {
  907. /* Keeps U Saturation proportional to V Sat */
  908. value=(value*0x5a)/0x7f<<8|value;
  909. }
  910. mask=0xffff;
  911. break;
  912. default:
  913. value = ((ctl->value - c->off) << c->shift) & c->mask;
  914. break;
  915. }
  916. dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
  917. ctl->id, c->v.name, ctl->value, c->reg, value,
  918. mask, c->sreg ? " [shadowed]" : "");
  919. if (c->sreg) {
  920. cx_sandor(c->sreg, c->reg, mask, value);
  921. } else {
  922. cx_andor(c->reg, mask, value);
  923. }
  924. return 0;
  925. }
  926. static void init_controls(struct cx88_core *core)
  927. {
  928. struct v4l2_control ctrl;
  929. int i;
  930. for (i = 0; i < CX8800_CTLS; i++) {
  931. ctrl.id=cx8800_ctls[i].v.id;
  932. ctrl.value=cx8800_ctls[i].v.default_value;
  933. set_control(core, &ctrl);
  934. }
  935. }
  936. /* ------------------------------------------------------------------ */
  937. static int cx8800_g_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
  938. struct v4l2_format *f)
  939. {
  940. switch (f->type) {
  941. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  942. memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
  943. f->fmt.pix.width = fh->width;
  944. f->fmt.pix.height = fh->height;
  945. f->fmt.pix.field = fh->vidq.field;
  946. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  947. f->fmt.pix.bytesperline =
  948. (f->fmt.pix.width * fh->fmt->depth) >> 3;
  949. f->fmt.pix.sizeimage =
  950. f->fmt.pix.height * f->fmt.pix.bytesperline;
  951. return 0;
  952. case V4L2_BUF_TYPE_VBI_CAPTURE:
  953. cx8800_vbi_fmt(dev, f);
  954. return 0;
  955. default:
  956. return -EINVAL;
  957. }
  958. }
  959. static int cx8800_try_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
  960. struct v4l2_format *f)
  961. {
  962. struct cx88_core *core = dev->core;
  963. switch (f->type) {
  964. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  965. {
  966. struct cx8800_fmt *fmt;
  967. enum v4l2_field field;
  968. unsigned int maxw, maxh;
  969. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  970. if (NULL == fmt)
  971. return -EINVAL;
  972. field = f->fmt.pix.field;
  973. maxw = norm_maxw(core->tvnorm);
  974. maxh = norm_maxh(core->tvnorm);
  975. if (V4L2_FIELD_ANY == field) {
  976. field = (f->fmt.pix.height > maxh/2)
  977. ? V4L2_FIELD_INTERLACED
  978. : V4L2_FIELD_BOTTOM;
  979. }
  980. switch (field) {
  981. case V4L2_FIELD_TOP:
  982. case V4L2_FIELD_BOTTOM:
  983. maxh = maxh / 2;
  984. break;
  985. case V4L2_FIELD_INTERLACED:
  986. break;
  987. default:
  988. return -EINVAL;
  989. }
  990. f->fmt.pix.field = field;
  991. if (f->fmt.pix.height < 32)
  992. f->fmt.pix.height = 32;
  993. if (f->fmt.pix.height > maxh)
  994. f->fmt.pix.height = maxh;
  995. if (f->fmt.pix.width < 48)
  996. f->fmt.pix.width = 48;
  997. if (f->fmt.pix.width > maxw)
  998. f->fmt.pix.width = maxw;
  999. f->fmt.pix.width &= ~0x03;
  1000. f->fmt.pix.bytesperline =
  1001. (f->fmt.pix.width * fmt->depth) >> 3;
  1002. f->fmt.pix.sizeimage =
  1003. f->fmt.pix.height * f->fmt.pix.bytesperline;
  1004. return 0;
  1005. }
  1006. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1007. cx8800_vbi_fmt(dev, f);
  1008. return 0;
  1009. default:
  1010. return -EINVAL;
  1011. }
  1012. }
  1013. static int cx8800_s_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
  1014. struct v4l2_format *f)
  1015. {
  1016. int err;
  1017. switch (f->type) {
  1018. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1019. err = cx8800_try_fmt(dev,fh,f);
  1020. if (0 != err)
  1021. return err;
  1022. fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  1023. fh->width = f->fmt.pix.width;
  1024. fh->height = f->fmt.pix.height;
  1025. fh->vidq.field = f->fmt.pix.field;
  1026. return 0;
  1027. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1028. cx8800_vbi_fmt(dev, f);
  1029. return 0;
  1030. default:
  1031. return -EINVAL;
  1032. }
  1033. }
  1034. /*
  1035. * This function is _not_ called directly, but from
  1036. * video_generic_ioctl (and maybe others). userspace
  1037. * copying is done already, arg is a kernel pointer.
  1038. */
  1039. static int video_do_ioctl(struct inode *inode, struct file *file,
  1040. unsigned int cmd, void *arg)
  1041. {
  1042. struct cx8800_fh *fh = file->private_data;
  1043. struct cx8800_dev *dev = fh->dev;
  1044. struct cx88_core *core = dev->core;
  1045. int err;
  1046. if (video_debug > 1)
  1047. v4l_print_ioctl(core->name,cmd);
  1048. switch (cmd) {
  1049. /* --- capabilities ------------------------------------------ */
  1050. case VIDIOC_QUERYCAP:
  1051. {
  1052. struct v4l2_capability *cap = arg;
  1053. memset(cap,0,sizeof(*cap));
  1054. strcpy(cap->driver, "cx8800");
  1055. strlcpy(cap->card, cx88_boards[core->board].name,
  1056. sizeof(cap->card));
  1057. sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
  1058. cap->version = CX88_VERSION_CODE;
  1059. cap->capabilities =
  1060. V4L2_CAP_VIDEO_CAPTURE |
  1061. V4L2_CAP_READWRITE |
  1062. V4L2_CAP_STREAMING |
  1063. V4L2_CAP_VBI_CAPTURE |
  1064. 0;
  1065. if (UNSET != core->tuner_type)
  1066. cap->capabilities |= V4L2_CAP_TUNER;
  1067. return 0;
  1068. }
  1069. /* --- capture ioctls ---------------------------------------- */
  1070. case VIDIOC_ENUM_FMT:
  1071. {
  1072. struct v4l2_fmtdesc *f = arg;
  1073. enum v4l2_buf_type type;
  1074. unsigned int index;
  1075. index = f->index;
  1076. type = f->type;
  1077. switch (type) {
  1078. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1079. if (index >= ARRAY_SIZE(formats))
  1080. return -EINVAL;
  1081. memset(f,0,sizeof(*f));
  1082. f->index = index;
  1083. f->type = type;
  1084. strlcpy(f->description,formats[index].name,sizeof(f->description));
  1085. f->pixelformat = formats[index].fourcc;
  1086. break;
  1087. default:
  1088. return -EINVAL;
  1089. }
  1090. return 0;
  1091. }
  1092. case VIDIOC_G_FMT:
  1093. {
  1094. struct v4l2_format *f = arg;
  1095. return cx8800_g_fmt(dev,fh,f);
  1096. }
  1097. case VIDIOC_S_FMT:
  1098. {
  1099. struct v4l2_format *f = arg;
  1100. return cx8800_s_fmt(dev,fh,f);
  1101. }
  1102. case VIDIOC_TRY_FMT:
  1103. {
  1104. struct v4l2_format *f = arg;
  1105. return cx8800_try_fmt(dev,fh,f);
  1106. }
  1107. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1108. /* --- streaming capture ------------------------------------- */
  1109. case VIDIOCGMBUF:
  1110. {
  1111. struct video_mbuf *mbuf = arg;
  1112. struct videobuf_queue *q;
  1113. struct v4l2_requestbuffers req;
  1114. unsigned int i;
  1115. q = get_queue(fh);
  1116. memset(&req,0,sizeof(req));
  1117. req.type = q->type;
  1118. req.count = 8;
  1119. req.memory = V4L2_MEMORY_MMAP;
  1120. err = videobuf_reqbufs(q,&req);
  1121. if (err < 0)
  1122. return err;
  1123. memset(mbuf,0,sizeof(*mbuf));
  1124. mbuf->frames = req.count;
  1125. mbuf->size = 0;
  1126. for (i = 0; i < mbuf->frames; i++) {
  1127. mbuf->offsets[i] = q->bufs[i]->boff;
  1128. mbuf->size += q->bufs[i]->bsize;
  1129. }
  1130. return 0;
  1131. }
  1132. #endif
  1133. case VIDIOC_REQBUFS:
  1134. return videobuf_reqbufs(get_queue(fh), arg);
  1135. case VIDIOC_QUERYBUF:
  1136. return videobuf_querybuf(get_queue(fh), arg);
  1137. case VIDIOC_QBUF:
  1138. return videobuf_qbuf(get_queue(fh), arg);
  1139. case VIDIOC_DQBUF:
  1140. return videobuf_dqbuf(get_queue(fh), arg,
  1141. file->f_flags & O_NONBLOCK);
  1142. case VIDIOC_STREAMON:
  1143. {
  1144. int res = get_ressource(fh);
  1145. if (!res_get(dev,fh,res))
  1146. return -EBUSY;
  1147. return videobuf_streamon(get_queue(fh));
  1148. }
  1149. case VIDIOC_STREAMOFF:
  1150. {
  1151. int res = get_ressource(fh);
  1152. err = videobuf_streamoff(get_queue(fh));
  1153. if (err < 0)
  1154. return err;
  1155. res_free(dev,fh,res);
  1156. return 0;
  1157. }
  1158. default:
  1159. return cx88_do_ioctl( inode, file, fh->radio, core, cmd, arg, video_do_ioctl );
  1160. }
  1161. return 0;
  1162. }
  1163. int cx88_do_ioctl(struct inode *inode, struct file *file, int radio,
  1164. struct cx88_core *core, unsigned int cmd, void *arg, v4l2_kioctl driver_ioctl)
  1165. {
  1166. int err;
  1167. if (video_debug) {
  1168. if (video_debug > 1) {
  1169. if (_IOC_DIR(cmd) & _IOC_WRITE)
  1170. v4l_printk_ioctl_arg("cx88(w)",cmd, arg);
  1171. else if (!_IOC_DIR(cmd) & _IOC_READ) {
  1172. v4l_print_ioctl("cx88", cmd);
  1173. }
  1174. } else
  1175. v4l_print_ioctl(core->name,cmd);
  1176. }
  1177. switch (cmd) {
  1178. /* ---------- tv norms ---------- */
  1179. case VIDIOC_ENUMSTD:
  1180. {
  1181. struct v4l2_standard *e = arg;
  1182. unsigned int i;
  1183. i = e->index;
  1184. if (i >= ARRAY_SIZE(tvnorms))
  1185. return -EINVAL;
  1186. err = v4l2_video_std_construct(e, tvnorms[e->index].id,
  1187. tvnorms[e->index].name);
  1188. e->index = i;
  1189. if (err < 0)
  1190. return err;
  1191. return 0;
  1192. }
  1193. case VIDIOC_G_STD:
  1194. {
  1195. v4l2_std_id *id = arg;
  1196. *id = core->tvnorm->id;
  1197. return 0;
  1198. }
  1199. case VIDIOC_S_STD:
  1200. {
  1201. v4l2_std_id *id = arg;
  1202. unsigned int i;
  1203. for(i = 0; i < ARRAY_SIZE(tvnorms); i++)
  1204. if (*id & tvnorms[i].id)
  1205. break;
  1206. if (i == ARRAY_SIZE(tvnorms))
  1207. return -EINVAL;
  1208. mutex_lock(&core->lock);
  1209. cx88_set_tvnorm(core,&tvnorms[i]);
  1210. mutex_unlock(&core->lock);
  1211. return 0;
  1212. }
  1213. /* ------ input switching ---------- */
  1214. case VIDIOC_ENUMINPUT:
  1215. {
  1216. static const char *iname[] = {
  1217. [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
  1218. [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
  1219. [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
  1220. [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
  1221. [ CX88_VMUX_SVIDEO ] = "S-Video",
  1222. [ CX88_VMUX_TELEVISION ] = "Television",
  1223. [ CX88_VMUX_CABLE ] = "Cable TV",
  1224. [ CX88_VMUX_DVB ] = "DVB",
  1225. [ CX88_VMUX_DEBUG ] = "for debug only",
  1226. };
  1227. struct v4l2_input *i = arg;
  1228. unsigned int n;
  1229. n = i->index;
  1230. if (n >= 4)
  1231. return -EINVAL;
  1232. if (0 == INPUT(n)->type)
  1233. return -EINVAL;
  1234. memset(i,0,sizeof(*i));
  1235. i->index = n;
  1236. i->type = V4L2_INPUT_TYPE_CAMERA;
  1237. strcpy(i->name,iname[INPUT(n)->type]);
  1238. if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
  1239. (CX88_VMUX_CABLE == INPUT(n)->type))
  1240. i->type = V4L2_INPUT_TYPE_TUNER;
  1241. for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
  1242. i->std |= tvnorms[n].id;
  1243. return 0;
  1244. }
  1245. case VIDIOC_G_INPUT:
  1246. {
  1247. unsigned int *i = arg;
  1248. *i = core->input;
  1249. return 0;
  1250. }
  1251. case VIDIOC_S_INPUT:
  1252. {
  1253. unsigned int *i = arg;
  1254. if (*i >= 4)
  1255. return -EINVAL;
  1256. mutex_lock(&core->lock);
  1257. cx88_newstation(core);
  1258. video_mux(core,*i);
  1259. mutex_unlock(&core->lock);
  1260. return 0;
  1261. }
  1262. /* --- controls ---------------------------------------------- */
  1263. case VIDIOC_QUERYCTRL:
  1264. {
  1265. struct v4l2_queryctrl *c = arg;
  1266. return cx88_queryctrl(c);
  1267. }
  1268. case VIDIOC_G_CTRL:
  1269. return get_control(core,arg);
  1270. case VIDIOC_S_CTRL:
  1271. return set_control(core,arg);
  1272. /* --- tuner ioctls ------------------------------------------ */
  1273. case VIDIOC_G_TUNER:
  1274. {
  1275. struct v4l2_tuner *t = arg;
  1276. u32 reg;
  1277. if (UNSET == core->tuner_type)
  1278. return -EINVAL;
  1279. if (0 != t->index)
  1280. return -EINVAL;
  1281. memset(t,0,sizeof(*t));
  1282. strcpy(t->name, "Television");
  1283. t->type = V4L2_TUNER_ANALOG_TV;
  1284. t->capability = V4L2_TUNER_CAP_NORM;
  1285. t->rangehigh = 0xffffffffUL;
  1286. cx88_get_stereo(core ,t);
  1287. reg = cx_read(MO_DEVICE_STATUS);
  1288. t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
  1289. return 0;
  1290. }
  1291. case VIDIOC_S_TUNER:
  1292. {
  1293. struct v4l2_tuner *t = arg;
  1294. if (UNSET == core->tuner_type)
  1295. return -EINVAL;
  1296. if (0 != t->index)
  1297. return -EINVAL;
  1298. cx88_set_stereo(core, t->audmode, 1);
  1299. return 0;
  1300. }
  1301. case VIDIOC_G_FREQUENCY:
  1302. {
  1303. struct v4l2_frequency *f = arg;
  1304. memset(f,0,sizeof(*f));
  1305. if (UNSET == core->tuner_type)
  1306. return -EINVAL;
  1307. /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
  1308. f->type = radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1309. f->frequency = core->freq;
  1310. cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
  1311. return 0;
  1312. }
  1313. case VIDIOC_S_FREQUENCY:
  1314. {
  1315. struct v4l2_frequency *f = arg;
  1316. if (UNSET == core->tuner_type)
  1317. return -EINVAL;
  1318. if (f->tuner != 0)
  1319. return -EINVAL;
  1320. if (0 == radio && f->type != V4L2_TUNER_ANALOG_TV)
  1321. return -EINVAL;
  1322. if (1 == radio && f->type != V4L2_TUNER_RADIO)
  1323. return -EINVAL;
  1324. mutex_lock(&core->lock);
  1325. core->freq = f->frequency;
  1326. cx88_newstation(core);
  1327. cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
  1328. /* When changing channels it is required to reset TVAUDIO */
  1329. msleep (10);
  1330. cx88_set_tvaudio(core);
  1331. mutex_unlock(&core->lock);
  1332. return 0;
  1333. }
  1334. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1335. /* ioctls to allow direct acces to the cx2388x registers */
  1336. case VIDIOC_INT_G_REGISTER:
  1337. {
  1338. struct v4l2_register *reg = arg;
  1339. if (reg->i2c_id != 0)
  1340. return -EINVAL;
  1341. /* cx2388x has a 24-bit register space */
  1342. reg->val = cx_read(reg->reg&0xffffff);
  1343. return 0;
  1344. }
  1345. case VIDIOC_INT_S_REGISTER:
  1346. {
  1347. struct v4l2_register *reg = arg;
  1348. if (reg->i2c_id != 0)
  1349. return -EINVAL;
  1350. if (!capable(CAP_SYS_ADMIN))
  1351. return -EPERM;
  1352. cx_write(reg->reg&0xffffff, reg->val);
  1353. return 0;
  1354. }
  1355. #endif
  1356. default:
  1357. return v4l_compat_translate_ioctl(inode,file,cmd,arg,
  1358. driver_ioctl);
  1359. }
  1360. return 0;
  1361. }
  1362. static int video_ioctl(struct inode *inode, struct file *file,
  1363. unsigned int cmd, unsigned long arg)
  1364. {
  1365. int retval;
  1366. retval=video_usercopy(inode, file, cmd, arg, video_do_ioctl);
  1367. if (video_debug > 1) {
  1368. if (retval < 0) {
  1369. v4l_print_ioctl("cx88(err)", cmd);
  1370. printk(KERN_DEBUG "cx88(err): errcode=%d\n",retval);
  1371. } else if (_IOC_DIR(cmd) & _IOC_READ)
  1372. v4l_printk_ioctl_arg("cx88(r)",cmd, (void *)arg);
  1373. }
  1374. return retval;
  1375. }
  1376. /* ----------------------------------------------------------- */
  1377. static int radio_do_ioctl(struct inode *inode, struct file *file,
  1378. unsigned int cmd, void *arg)
  1379. {
  1380. struct cx8800_fh *fh = file->private_data;
  1381. struct cx8800_dev *dev = fh->dev;
  1382. struct cx88_core *core = dev->core;
  1383. if (video_debug > 1)
  1384. v4l_print_ioctl(core->name,cmd);
  1385. switch (cmd) {
  1386. case VIDIOC_QUERYCAP:
  1387. {
  1388. struct v4l2_capability *cap = arg;
  1389. memset(cap,0,sizeof(*cap));
  1390. strcpy(cap->driver, "cx8800");
  1391. strlcpy(cap->card, cx88_boards[core->board].name,
  1392. sizeof(cap->card));
  1393. sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
  1394. cap->version = CX88_VERSION_CODE;
  1395. cap->capabilities = V4L2_CAP_TUNER;
  1396. return 0;
  1397. }
  1398. case VIDIOC_G_TUNER:
  1399. {
  1400. struct v4l2_tuner *t = arg;
  1401. if (t->index > 0)
  1402. return -EINVAL;
  1403. memset(t,0,sizeof(*t));
  1404. strcpy(t->name, "Radio");
  1405. t->type = V4L2_TUNER_RADIO;
  1406. cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
  1407. return 0;
  1408. }
  1409. case VIDIOC_ENUMINPUT:
  1410. {
  1411. struct v4l2_input *i = arg;
  1412. if (i->index != 0)
  1413. return -EINVAL;
  1414. strcpy(i->name,"Radio");
  1415. i->type = V4L2_INPUT_TYPE_TUNER;
  1416. return 0;
  1417. }
  1418. case VIDIOC_G_INPUT:
  1419. {
  1420. int *i = arg;
  1421. *i = 0;
  1422. return 0;
  1423. }
  1424. case VIDIOC_G_AUDIO:
  1425. {
  1426. struct v4l2_audio *a = arg;
  1427. memset(a,0,sizeof(*a));
  1428. strcpy(a->name,"Radio");
  1429. return 0;
  1430. }
  1431. case VIDIOC_G_STD:
  1432. {
  1433. v4l2_std_id *id = arg;
  1434. *id = 0;
  1435. return 0;
  1436. }
  1437. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1438. case VIDIOCSTUNER:
  1439. {
  1440. struct video_tuner *v = arg;
  1441. if (v->tuner) /* Only tuner 0 */
  1442. return -EINVAL;
  1443. cx88_call_i2c_clients(core,VIDIOCSTUNER,v);
  1444. return 0;
  1445. }
  1446. #endif
  1447. case VIDIOC_S_TUNER:
  1448. {
  1449. struct v4l2_tuner *t = arg;
  1450. if (0 != t->index)
  1451. return -EINVAL;
  1452. cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
  1453. return 0;
  1454. }
  1455. case VIDIOC_S_AUDIO:
  1456. case VIDIOC_S_INPUT:
  1457. case VIDIOC_S_STD:
  1458. return 0;
  1459. case VIDIOC_QUERYCTRL:
  1460. {
  1461. struct v4l2_queryctrl *c = arg;
  1462. int i;
  1463. if (c->id < V4L2_CID_BASE ||
  1464. c->id >= V4L2_CID_LASTP1)
  1465. return -EINVAL;
  1466. if (c->id == V4L2_CID_AUDIO_MUTE) {
  1467. for (i = 0; i < CX8800_CTLS; i++)
  1468. if (cx8800_ctls[i].v.id == c->id)
  1469. break;
  1470. *c = cx8800_ctls[i].v;
  1471. } else
  1472. *c = no_ctl;
  1473. return 0;
  1474. }
  1475. case VIDIOC_G_CTRL:
  1476. case VIDIOC_S_CTRL:
  1477. case VIDIOC_G_FREQUENCY:
  1478. case VIDIOC_S_FREQUENCY:
  1479. return video_do_ioctl(inode,file,cmd,arg);
  1480. default:
  1481. return v4l_compat_translate_ioctl(inode,file,cmd,arg,
  1482. radio_do_ioctl);
  1483. }
  1484. return 0;
  1485. };
  1486. static int radio_ioctl(struct inode *inode, struct file *file,
  1487. unsigned int cmd, unsigned long arg)
  1488. {
  1489. return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
  1490. };
  1491. /* ----------------------------------------------------------- */
  1492. static void cx8800_vid_timeout(unsigned long data)
  1493. {
  1494. struct cx8800_dev *dev = (struct cx8800_dev*)data;
  1495. struct cx88_core *core = dev->core;
  1496. struct cx88_dmaqueue *q = &dev->vidq;
  1497. struct cx88_buffer *buf;
  1498. unsigned long flags;
  1499. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
  1500. cx_clear(MO_VID_DMACNTRL, 0x11);
  1501. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  1502. spin_lock_irqsave(&dev->slock,flags);
  1503. while (!list_empty(&q->active)) {
  1504. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  1505. list_del(&buf->vb.queue);
  1506. buf->vb.state = STATE_ERROR;
  1507. wake_up(&buf->vb.done);
  1508. printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
  1509. buf, buf->vb.i, (unsigned long)buf->risc.dma);
  1510. }
  1511. restart_video_queue(dev,q);
  1512. spin_unlock_irqrestore(&dev->slock,flags);
  1513. }
  1514. static char *cx88_vid_irqs[32] = {
  1515. "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
  1516. "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
  1517. "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
  1518. "y_sync", "u_sync", "v_sync", "vbi_sync",
  1519. "opc_err", "par_err", "rip_err", "pci_abort",
  1520. };
  1521. static void cx8800_vid_irq(struct cx8800_dev *dev)
  1522. {
  1523. struct cx88_core *core = dev->core;
  1524. u32 status, mask, count;
  1525. status = cx_read(MO_VID_INTSTAT);
  1526. mask = cx_read(MO_VID_INTMSK);
  1527. if (0 == (status & mask))
  1528. return;
  1529. cx_write(MO_VID_INTSTAT, status);
  1530. if (irq_debug || (status & mask & ~0xff))
  1531. cx88_print_irqbits(core->name, "irq vid",
  1532. cx88_vid_irqs, status, mask);
  1533. /* risc op code error */
  1534. if (status & (1 << 16)) {
  1535. printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
  1536. cx_clear(MO_VID_DMACNTRL, 0x11);
  1537. cx_clear(VID_CAPTURE_CONTROL, 0x06);
  1538. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
  1539. }
  1540. /* risc1 y */
  1541. if (status & 0x01) {
  1542. spin_lock(&dev->slock);
  1543. count = cx_read(MO_VIDY_GPCNT);
  1544. cx88_wakeup(core, &dev->vidq, count);
  1545. spin_unlock(&dev->slock);
  1546. }
  1547. /* risc1 vbi */
  1548. if (status & 0x08) {
  1549. spin_lock(&dev->slock);
  1550. count = cx_read(MO_VBI_GPCNT);
  1551. cx88_wakeup(core, &dev->vbiq, count);
  1552. spin_unlock(&dev->slock);
  1553. }
  1554. /* risc2 y */
  1555. if (status & 0x10) {
  1556. dprintk(2,"stopper video\n");
  1557. spin_lock(&dev->slock);
  1558. restart_video_queue(dev,&dev->vidq);
  1559. spin_unlock(&dev->slock);
  1560. }
  1561. /* risc2 vbi */
  1562. if (status & 0x80) {
  1563. dprintk(2,"stopper vbi\n");
  1564. spin_lock(&dev->slock);
  1565. cx8800_restart_vbi_queue(dev,&dev->vbiq);
  1566. spin_unlock(&dev->slock);
  1567. }
  1568. }
  1569. static irqreturn_t cx8800_irq(int irq, void *dev_id)
  1570. {
  1571. struct cx8800_dev *dev = dev_id;
  1572. struct cx88_core *core = dev->core;
  1573. u32 status;
  1574. int loop, handled = 0;
  1575. for (loop = 0; loop < 10; loop++) {
  1576. status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
  1577. if (0 == status)
  1578. goto out;
  1579. cx_write(MO_PCI_INTSTAT, status);
  1580. handled = 1;
  1581. if (status & core->pci_irqmask)
  1582. cx88_core_irq(core,status);
  1583. if (status & 0x01)
  1584. cx8800_vid_irq(dev);
  1585. };
  1586. if (10 == loop) {
  1587. printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
  1588. core->name);
  1589. cx_write(MO_PCI_INTMSK,0);
  1590. }
  1591. out:
  1592. return IRQ_RETVAL(handled);
  1593. }
  1594. /* ----------------------------------------------------------- */
  1595. /* exported stuff */
  1596. static const struct file_operations video_fops =
  1597. {
  1598. .owner = THIS_MODULE,
  1599. .open = video_open,
  1600. .release = video_release,
  1601. .read = video_read,
  1602. .poll = video_poll,
  1603. .mmap = video_mmap,
  1604. .ioctl = video_ioctl,
  1605. .compat_ioctl = v4l_compat_ioctl32,
  1606. .llseek = no_llseek,
  1607. };
  1608. static struct video_device cx8800_video_template =
  1609. {
  1610. .name = "cx8800-video",
  1611. .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
  1612. .hardware = 0,
  1613. .fops = &video_fops,
  1614. .minor = -1,
  1615. };
  1616. static struct video_device cx8800_vbi_template =
  1617. {
  1618. .name = "cx8800-vbi",
  1619. .type = VID_TYPE_TELETEXT|VID_TYPE_TUNER,
  1620. .hardware = 0,
  1621. .fops = &video_fops,
  1622. .minor = -1,
  1623. };
  1624. static const struct file_operations radio_fops =
  1625. {
  1626. .owner = THIS_MODULE,
  1627. .open = video_open,
  1628. .release = video_release,
  1629. .ioctl = radio_ioctl,
  1630. .compat_ioctl = v4l_compat_ioctl32,
  1631. .llseek = no_llseek,
  1632. };
  1633. static struct video_device cx8800_radio_template =
  1634. {
  1635. .name = "cx8800-radio",
  1636. .type = VID_TYPE_TUNER,
  1637. .hardware = 0,
  1638. .fops = &radio_fops,
  1639. .minor = -1,
  1640. };
  1641. /* ----------------------------------------------------------- */
  1642. static void cx8800_unregister_video(struct cx8800_dev *dev)
  1643. {
  1644. if (dev->radio_dev) {
  1645. if (-1 != dev->radio_dev->minor)
  1646. video_unregister_device(dev->radio_dev);
  1647. else
  1648. video_device_release(dev->radio_dev);
  1649. dev->radio_dev = NULL;
  1650. }
  1651. if (dev->vbi_dev) {
  1652. if (-1 != dev->vbi_dev->minor)
  1653. video_unregister_device(dev->vbi_dev);
  1654. else
  1655. video_device_release(dev->vbi_dev);
  1656. dev->vbi_dev = NULL;
  1657. }
  1658. if (dev->video_dev) {
  1659. if (-1 != dev->video_dev->minor)
  1660. video_unregister_device(dev->video_dev);
  1661. else
  1662. video_device_release(dev->video_dev);
  1663. dev->video_dev = NULL;
  1664. }
  1665. }
  1666. static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
  1667. const struct pci_device_id *pci_id)
  1668. {
  1669. struct cx8800_dev *dev;
  1670. struct cx88_core *core;
  1671. int err;
  1672. dev = kzalloc(sizeof(*dev),GFP_KERNEL);
  1673. if (NULL == dev)
  1674. return -ENOMEM;
  1675. /* pci init */
  1676. dev->pci = pci_dev;
  1677. if (pci_enable_device(pci_dev)) {
  1678. err = -EIO;
  1679. goto fail_free;
  1680. }
  1681. core = cx88_core_get(dev->pci);
  1682. if (NULL == core) {
  1683. err = -EINVAL;
  1684. goto fail_free;
  1685. }
  1686. dev->core = core;
  1687. /* print pci info */
  1688. pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
  1689. pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
  1690. printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
  1691. "latency: %d, mmio: 0x%llx\n", core->name,
  1692. pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
  1693. dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
  1694. pci_set_master(pci_dev);
  1695. if (!pci_dma_supported(pci_dev,0xffffffff)) {
  1696. printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
  1697. err = -EIO;
  1698. goto fail_core;
  1699. }
  1700. /* initialize driver struct */
  1701. spin_lock_init(&dev->slock);
  1702. core->tvnorm = tvnorms;
  1703. /* init video dma queues */
  1704. INIT_LIST_HEAD(&dev->vidq.active);
  1705. INIT_LIST_HEAD(&dev->vidq.queued);
  1706. dev->vidq.timeout.function = cx8800_vid_timeout;
  1707. dev->vidq.timeout.data = (unsigned long)dev;
  1708. init_timer(&dev->vidq.timeout);
  1709. cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
  1710. MO_VID_DMACNTRL,0x11,0x00);
  1711. /* init vbi dma queues */
  1712. INIT_LIST_HEAD(&dev->vbiq.active);
  1713. INIT_LIST_HEAD(&dev->vbiq.queued);
  1714. dev->vbiq.timeout.function = cx8800_vbi_timeout;
  1715. dev->vbiq.timeout.data = (unsigned long)dev;
  1716. init_timer(&dev->vbiq.timeout);
  1717. cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
  1718. MO_VID_DMACNTRL,0x88,0x00);
  1719. /* get irq */
  1720. err = request_irq(pci_dev->irq, cx8800_irq,
  1721. IRQF_SHARED | IRQF_DISABLED, core->name, dev);
  1722. if (err < 0) {
  1723. printk(KERN_ERR "%s: can't get IRQ %d\n",
  1724. core->name,pci_dev->irq);
  1725. goto fail_core;
  1726. }
  1727. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  1728. /* load and configure helper modules */
  1729. if (TUNER_ABSENT != core->tuner_type)
  1730. request_module("tuner");
  1731. if (cx88_boards[ core->board ].audio_chip == AUDIO_CHIP_WM8775)
  1732. request_module("wm8775");
  1733. /* register v4l devices */
  1734. dev->video_dev = cx88_vdev_init(core,dev->pci,
  1735. &cx8800_video_template,"video");
  1736. err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
  1737. video_nr[core->nr]);
  1738. if (err < 0) {
  1739. printk(KERN_INFO "%s: can't register video device\n",
  1740. core->name);
  1741. goto fail_unreg;
  1742. }
  1743. printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
  1744. core->name,dev->video_dev->minor & 0x1f);
  1745. dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
  1746. err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
  1747. vbi_nr[core->nr]);
  1748. if (err < 0) {
  1749. printk(KERN_INFO "%s/0: can't register vbi device\n",
  1750. core->name);
  1751. goto fail_unreg;
  1752. }
  1753. printk(KERN_INFO "%s/0: registered device vbi%d\n",
  1754. core->name,dev->vbi_dev->minor & 0x1f);
  1755. if (core->has_radio) {
  1756. dev->radio_dev = cx88_vdev_init(core,dev->pci,
  1757. &cx8800_radio_template,"radio");
  1758. err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
  1759. radio_nr[core->nr]);
  1760. if (err < 0) {
  1761. printk(KERN_INFO "%s/0: can't register radio device\n",
  1762. core->name);
  1763. goto fail_unreg;
  1764. }
  1765. printk(KERN_INFO "%s/0: registered device radio%d\n",
  1766. core->name,dev->radio_dev->minor & 0x1f);
  1767. }
  1768. /* everything worked */
  1769. list_add_tail(&dev->devlist,&cx8800_devlist);
  1770. pci_set_drvdata(pci_dev,dev);
  1771. /* initial device configuration */
  1772. mutex_lock(&core->lock);
  1773. cx88_set_tvnorm(core,tvnorms);
  1774. init_controls(core);
  1775. video_mux(core,0);
  1776. mutex_unlock(&core->lock);
  1777. /* start tvaudio thread */
  1778. if (core->tuner_type != TUNER_ABSENT)
  1779. core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
  1780. return 0;
  1781. fail_unreg:
  1782. cx8800_unregister_video(dev);
  1783. free_irq(pci_dev->irq, dev);
  1784. fail_core:
  1785. cx88_core_put(core,dev->pci);
  1786. fail_free:
  1787. kfree(dev);
  1788. return err;
  1789. }
  1790. static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
  1791. {
  1792. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1793. struct cx88_core *core = dev->core;
  1794. /* stop thread */
  1795. if (core->kthread) {
  1796. kthread_stop(core->kthread);
  1797. core->kthread = NULL;
  1798. }
  1799. cx88_shutdown(core); /* FIXME */
  1800. pci_disable_device(pci_dev);
  1801. /* unregister stuff */
  1802. free_irq(pci_dev->irq, dev);
  1803. cx8800_unregister_video(dev);
  1804. pci_set_drvdata(pci_dev, NULL);
  1805. /* free memory */
  1806. btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
  1807. list_del(&dev->devlist);
  1808. cx88_core_put(core,dev->pci);
  1809. kfree(dev);
  1810. }
  1811. #ifdef CONFIG_PM
  1812. static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
  1813. {
  1814. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1815. struct cx88_core *core = dev->core;
  1816. /* stop video+vbi capture */
  1817. spin_lock(&dev->slock);
  1818. if (!list_empty(&dev->vidq.active)) {
  1819. printk("%s: suspend video\n", core->name);
  1820. stop_video_dma(dev);
  1821. del_timer(&dev->vidq.timeout);
  1822. }
  1823. if (!list_empty(&dev->vbiq.active)) {
  1824. printk("%s: suspend vbi\n", core->name);
  1825. cx8800_stop_vbi_dma(dev);
  1826. del_timer(&dev->vbiq.timeout);
  1827. }
  1828. spin_unlock(&dev->slock);
  1829. /* FIXME -- shutdown device */
  1830. cx88_shutdown(core);
  1831. pci_save_state(pci_dev);
  1832. if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
  1833. pci_disable_device(pci_dev);
  1834. dev->state.disabled = 1;
  1835. }
  1836. return 0;
  1837. }
  1838. static int cx8800_resume(struct pci_dev *pci_dev)
  1839. {
  1840. struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
  1841. struct cx88_core *core = dev->core;
  1842. int err;
  1843. if (dev->state.disabled) {
  1844. err=pci_enable_device(pci_dev);
  1845. if (err) {
  1846. printk(KERN_ERR "%s: can't enable device\n",
  1847. core->name);
  1848. return err;
  1849. }
  1850. dev->state.disabled = 0;
  1851. }
  1852. err= pci_set_power_state(pci_dev, PCI_D0);
  1853. if (err) {
  1854. printk(KERN_ERR "%s: can't enable device\n",
  1855. core->name);
  1856. pci_disable_device(pci_dev);
  1857. dev->state.disabled = 1;
  1858. return err;
  1859. }
  1860. pci_restore_state(pci_dev);
  1861. /* FIXME: re-initialize hardware */
  1862. cx88_reset(core);
  1863. /* restart video+vbi capture */
  1864. spin_lock(&dev->slock);
  1865. if (!list_empty(&dev->vidq.active)) {
  1866. printk("%s: resume video\n", core->name);
  1867. restart_video_queue(dev,&dev->vidq);
  1868. }
  1869. if (!list_empty(&dev->vbiq.active)) {
  1870. printk("%s: resume vbi\n", core->name);
  1871. cx8800_restart_vbi_queue(dev,&dev->vbiq);
  1872. }
  1873. spin_unlock(&dev->slock);
  1874. return 0;
  1875. }
  1876. #endif
  1877. /* ----------------------------------------------------------- */
  1878. static struct pci_device_id cx8800_pci_tbl[] = {
  1879. {
  1880. .vendor = 0x14f1,
  1881. .device = 0x8800,
  1882. .subvendor = PCI_ANY_ID,
  1883. .subdevice = PCI_ANY_ID,
  1884. },{
  1885. /* --- end of list --- */
  1886. }
  1887. };
  1888. MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
  1889. static struct pci_driver cx8800_pci_driver = {
  1890. .name = "cx8800",
  1891. .id_table = cx8800_pci_tbl,
  1892. .probe = cx8800_initdev,
  1893. .remove = __devexit_p(cx8800_finidev),
  1894. #ifdef CONFIG_PM
  1895. .suspend = cx8800_suspend,
  1896. .resume = cx8800_resume,
  1897. #endif
  1898. };
  1899. static int cx8800_init(void)
  1900. {
  1901. printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
  1902. (CX88_VERSION_CODE >> 16) & 0xff,
  1903. (CX88_VERSION_CODE >> 8) & 0xff,
  1904. CX88_VERSION_CODE & 0xff);
  1905. #ifdef SNAPSHOT
  1906. printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
  1907. SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
  1908. #endif
  1909. return pci_register_driver(&cx8800_pci_driver);
  1910. }
  1911. static void cx8800_fini(void)
  1912. {
  1913. pci_unregister_driver(&cx8800_pci_driver);
  1914. }
  1915. module_init(cx8800_init);
  1916. module_exit(cx8800_fini);
  1917. EXPORT_SYMBOL(cx88_do_ioctl);
  1918. /* ----------------------------------------------------------- */
  1919. /*
  1920. * Local variables:
  1921. * c-basic-offset: 8
  1922. * End:
  1923. * kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off
  1924. */