cx88-video.c 50 KB

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