cx88-video.c 53 KB

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