cx88-mpeg.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /*
  2. *
  3. * Support for the mpeg transport stream transfers
  4. * PCI function #2 of the cx2388x.
  5. *
  6. * (c) 2004 Jelle Foks <jelle@foks.8m.com>
  7. * (c) 2004 Chris Pascoe <c.pascoe@itee.uq.edu.au>
  8. * (c) 2004 Gerd Knorr <kraxel@bytesex.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/device.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/interrupt.h>
  29. #include <asm/delay.h>
  30. #include "cx88.h"
  31. /* ------------------------------------------------------------------ */
  32. MODULE_DESCRIPTION("mpeg driver for cx2388x based TV cards");
  33. MODULE_AUTHOR("Jelle Foks <jelle@foks.8m.com>");
  34. MODULE_AUTHOR("Chris Pascoe <c.pascoe@itee.uq.edu.au>");
  35. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
  36. MODULE_LICENSE("GPL");
  37. static unsigned int debug = 0;
  38. module_param(debug,int,0644);
  39. MODULE_PARM_DESC(debug,"enable debug messages [mpeg]");
  40. #define dprintk(level,fmt, arg...) if (debug >= level) \
  41. printk(KERN_DEBUG "%s/2-mpeg: " fmt, dev->core->name, ## arg)
  42. #define mpeg_dbg(level,fmt, arg...) if (debug >= level) \
  43. printk(KERN_DEBUG "%s/2-mpeg: " fmt, core->name, ## arg)
  44. #if defined(CONFIG_MODULES) && defined(MODULE)
  45. static void request_module_async(struct work_struct *work)
  46. {
  47. struct cx8802_dev *dev=container_of(work, struct cx8802_dev, request_module_wk);
  48. if (dev->core->board.mpeg & CX88_MPEG_DVB)
  49. request_module("cx88-dvb");
  50. if (dev->core->board.mpeg & CX88_MPEG_BLACKBIRD)
  51. request_module("cx88-blackbird");
  52. }
  53. static void request_modules(struct cx8802_dev *dev)
  54. {
  55. INIT_WORK(&dev->request_module_wk, request_module_async);
  56. schedule_work(&dev->request_module_wk);
  57. }
  58. #else
  59. #define request_modules(dev)
  60. #endif /* CONFIG_MODULES */
  61. static LIST_HEAD(cx8802_devlist);
  62. /* ------------------------------------------------------------------ */
  63. static int cx8802_start_dma(struct cx8802_dev *dev,
  64. struct cx88_dmaqueue *q,
  65. struct cx88_buffer *buf)
  66. {
  67. struct cx88_core *core = dev->core;
  68. dprintk(1, "cx8802_start_dma w: %d, h: %d, f: %d\n",
  69. buf->vb.width, buf->vb.height, buf->vb.field);
  70. /* setup fifo + format */
  71. cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH28],
  72. dev->ts_packet_size, buf->risc.dma);
  73. /* write TS length to chip */
  74. cx_write(MO_TS_LNGTH, buf->vb.width);
  75. /* FIXME: this needs a review.
  76. * also: move to cx88-blackbird + cx88-dvb source files? */
  77. dprintk( 1, "core->active_type_id = 0x%08x\n", core->active_type_id);
  78. if ( (core->active_type_id == CX88_MPEG_DVB) &&
  79. (core->board.mpeg & CX88_MPEG_DVB) ) {
  80. dprintk( 1, "cx8802_start_dma doing .dvb\n");
  81. /* negedge driven & software reset */
  82. cx_write(TS_GEN_CNTRL, 0x0040 | dev->ts_gen_cntrl);
  83. udelay(100);
  84. cx_write(MO_PINMUX_IO, 0x00);
  85. cx_write(TS_HW_SOP_CNTRL, 0x47<<16|188<<4|0x01);
  86. switch (core->boardnr) {
  87. case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q:
  88. case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T:
  89. case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
  90. case CX88_BOARD_PCHDTV_HD5500:
  91. cx_write(TS_SOP_STAT, 1<<13);
  92. break;
  93. case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
  94. case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
  95. cx_write(MO_PINMUX_IO, 0x88); /* Enable MPEG parallel IO and video signal pins */
  96. udelay(100);
  97. break;
  98. case CX88_BOARD_HAUPPAUGE_HVR1300:
  99. break;
  100. case CX88_BOARD_PINNACLE_PCTV_HD_800i:
  101. /* Enable MPEG parallel port */
  102. cx_write(MO_PINMUX_IO, 0x80);
  103. udelay(100);
  104. break;
  105. default:
  106. cx_write(TS_SOP_STAT, 0x00);
  107. break;
  108. }
  109. cx_write(TS_GEN_CNTRL, dev->ts_gen_cntrl);
  110. udelay(100);
  111. } else if ( (core->active_type_id == CX88_MPEG_BLACKBIRD) &&
  112. (core->board.mpeg & CX88_MPEG_BLACKBIRD) ) {
  113. dprintk( 1, "cx8802_start_dma doing .blackbird\n");
  114. cx_write(MO_PINMUX_IO, 0x88); /* enable MPEG parallel IO */
  115. cx_write(TS_GEN_CNTRL, 0x46); /* punctured clock TS & posedge driven & software reset */
  116. udelay(100);
  117. cx_write(TS_HW_SOP_CNTRL, 0x408); /* mpeg start byte */
  118. cx_write(TS_VALERR_CNTRL, 0x2000);
  119. cx_write(TS_GEN_CNTRL, 0x06); /* punctured clock TS & posedge driven */
  120. udelay(100);
  121. } else {
  122. printk( "%s() Failed. Unsupported value in .mpeg (0x%08x)\n", __FUNCTION__,
  123. core->board.mpeg );
  124. return -EINVAL;
  125. }
  126. /* reset counter */
  127. cx_write(MO_TS_GPCNTRL, GP_COUNT_CONTROL_RESET);
  128. q->count = 1;
  129. /* enable irqs */
  130. dprintk( 1, "setting the interrupt mask\n" );
  131. cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_TSINT);
  132. cx_set(MO_TS_INTMSK, 0x1f0011);
  133. /* start dma */
  134. cx_set(MO_DEV_CNTRL2, (1<<5));
  135. cx_set(MO_TS_DMACNTRL, 0x11);
  136. return 0;
  137. }
  138. static int cx8802_stop_dma(struct cx8802_dev *dev)
  139. {
  140. struct cx88_core *core = dev->core;
  141. dprintk( 1, "cx8802_stop_dma\n" );
  142. /* stop dma */
  143. cx_clear(MO_TS_DMACNTRL, 0x11);
  144. /* disable irqs */
  145. cx_clear(MO_PCI_INTMSK, PCI_INT_TSINT);
  146. cx_clear(MO_TS_INTMSK, 0x1f0011);
  147. /* Reset the controller */
  148. cx_write(TS_GEN_CNTRL, 0xcd);
  149. return 0;
  150. }
  151. static int cx8802_restart_queue(struct cx8802_dev *dev,
  152. struct cx88_dmaqueue *q)
  153. {
  154. struct cx88_buffer *buf;
  155. dprintk( 1, "cx8802_restart_queue\n" );
  156. if (list_empty(&q->active))
  157. {
  158. struct cx88_buffer *prev;
  159. prev = NULL;
  160. dprintk(1, "cx8802_restart_queue: queue is empty\n" );
  161. for (;;) {
  162. if (list_empty(&q->queued))
  163. return 0;
  164. buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
  165. if (NULL == prev) {
  166. list_del(&buf->vb.queue);
  167. list_add_tail(&buf->vb.queue,&q->active);
  168. cx8802_start_dma(dev, q, buf);
  169. buf->vb.state = VIDEOBUF_ACTIVE;
  170. buf->count = q->count++;
  171. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  172. dprintk(1,"[%p/%d] restart_queue - first active\n",
  173. buf,buf->vb.i);
  174. } else if (prev->vb.width == buf->vb.width &&
  175. prev->vb.height == buf->vb.height &&
  176. prev->fmt == buf->fmt) {
  177. list_del(&buf->vb.queue);
  178. list_add_tail(&buf->vb.queue,&q->active);
  179. buf->vb.state = VIDEOBUF_ACTIVE;
  180. buf->count = q->count++;
  181. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  182. dprintk(1,"[%p/%d] restart_queue - move to active\n",
  183. buf,buf->vb.i);
  184. } else {
  185. return 0;
  186. }
  187. prev = buf;
  188. }
  189. return 0;
  190. }
  191. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  192. dprintk(2,"restart_queue [%p/%d]: restart dma\n",
  193. buf, buf->vb.i);
  194. cx8802_start_dma(dev, q, buf);
  195. list_for_each_entry(buf, &q->active, vb.queue)
  196. buf->count = q->count++;
  197. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  198. return 0;
  199. }
  200. /* ------------------------------------------------------------------ */
  201. int cx8802_buf_prepare(struct videobuf_queue *q, struct cx8802_dev *dev,
  202. struct cx88_buffer *buf, enum v4l2_field field)
  203. {
  204. int size = dev->ts_packet_size * dev->ts_packet_count;
  205. struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
  206. int rc;
  207. dprintk(1, "%s: %p\n", __FUNCTION__, buf);
  208. if (0 != buf->vb.baddr && buf->vb.bsize < size)
  209. return -EINVAL;
  210. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  211. buf->vb.width = dev->ts_packet_size;
  212. buf->vb.height = dev->ts_packet_count;
  213. buf->vb.size = size;
  214. buf->vb.field = field /*V4L2_FIELD_TOP*/;
  215. if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
  216. goto fail;
  217. cx88_risc_databuffer(dev->pci, &buf->risc,
  218. dma->sglist,
  219. buf->vb.width, buf->vb.height, 0);
  220. }
  221. buf->vb.state = VIDEOBUF_PREPARED;
  222. return 0;
  223. fail:
  224. cx88_free_buffer(q,buf);
  225. return rc;
  226. }
  227. void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf)
  228. {
  229. struct cx88_buffer *prev;
  230. struct cx88_dmaqueue *cx88q = &dev->mpegq;
  231. dprintk( 1, "cx8802_buf_queue\n" );
  232. /* add jump to stopper */
  233. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  234. buf->risc.jmp[1] = cpu_to_le32(cx88q->stopper.dma);
  235. if (list_empty(&cx88q->active)) {
  236. dprintk( 1, "queue is empty - first active\n" );
  237. list_add_tail(&buf->vb.queue,&cx88q->active);
  238. cx8802_start_dma(dev, cx88q, buf);
  239. buf->vb.state = VIDEOBUF_ACTIVE;
  240. buf->count = cx88q->count++;
  241. mod_timer(&cx88q->timeout, jiffies+BUFFER_TIMEOUT);
  242. dprintk(1,"[%p/%d] %s - first active\n",
  243. buf, buf->vb.i, __FUNCTION__);
  244. } else {
  245. dprintk( 1, "queue is not empty - append to active\n" );
  246. prev = list_entry(cx88q->active.prev, struct cx88_buffer, vb.queue);
  247. list_add_tail(&buf->vb.queue,&cx88q->active);
  248. buf->vb.state = VIDEOBUF_ACTIVE;
  249. buf->count = cx88q->count++;
  250. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  251. dprintk( 1, "[%p/%d] %s - append to active\n",
  252. buf, buf->vb.i, __FUNCTION__);
  253. }
  254. }
  255. /* ----------------------------------------------------------- */
  256. static void do_cancel_buffers(struct cx8802_dev *dev, char *reason, int restart)
  257. {
  258. struct cx88_dmaqueue *q = &dev->mpegq;
  259. struct cx88_buffer *buf;
  260. unsigned long flags;
  261. spin_lock_irqsave(&dev->slock,flags);
  262. while (!list_empty(&q->active)) {
  263. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  264. list_del(&buf->vb.queue);
  265. buf->vb.state = VIDEOBUF_ERROR;
  266. wake_up(&buf->vb.done);
  267. dprintk(1,"[%p/%d] %s - dma=0x%08lx\n",
  268. buf, buf->vb.i, reason, (unsigned long)buf->risc.dma);
  269. }
  270. if (restart)
  271. {
  272. dprintk(1, "restarting queue\n" );
  273. cx8802_restart_queue(dev,q);
  274. }
  275. spin_unlock_irqrestore(&dev->slock,flags);
  276. }
  277. void cx8802_cancel_buffers(struct cx8802_dev *dev)
  278. {
  279. struct cx88_dmaqueue *q = &dev->mpegq;
  280. dprintk( 1, "cx8802_cancel_buffers" );
  281. del_timer_sync(&q->timeout);
  282. cx8802_stop_dma(dev);
  283. do_cancel_buffers(dev,"cancel",0);
  284. }
  285. static void cx8802_timeout(unsigned long data)
  286. {
  287. struct cx8802_dev *dev = (struct cx8802_dev*)data;
  288. dprintk(1, "%s\n",__FUNCTION__);
  289. if (debug)
  290. cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH28]);
  291. cx8802_stop_dma(dev);
  292. do_cancel_buffers(dev,"timeout",1);
  293. }
  294. static char *cx88_mpeg_irqs[32] = {
  295. "ts_risci1", NULL, NULL, NULL,
  296. "ts_risci2", NULL, NULL, NULL,
  297. "ts_oflow", NULL, NULL, NULL,
  298. "ts_sync", NULL, NULL, NULL,
  299. "opc_err", "par_err", "rip_err", "pci_abort",
  300. "ts_err?",
  301. };
  302. static void cx8802_mpeg_irq(struct cx8802_dev *dev)
  303. {
  304. struct cx88_core *core = dev->core;
  305. u32 status, mask, count;
  306. dprintk( 1, "cx8802_mpeg_irq\n" );
  307. status = cx_read(MO_TS_INTSTAT);
  308. mask = cx_read(MO_TS_INTMSK);
  309. if (0 == (status & mask))
  310. return;
  311. cx_write(MO_TS_INTSTAT, status);
  312. if (debug || (status & mask & ~0xff))
  313. cx88_print_irqbits(core->name, "irq mpeg ",
  314. cx88_mpeg_irqs, ARRAY_SIZE(cx88_mpeg_irqs),
  315. status, mask);
  316. /* risc op code error */
  317. if (status & (1 << 16)) {
  318. printk(KERN_WARNING "%s: mpeg risc op code error\n",core->name);
  319. cx_clear(MO_TS_DMACNTRL, 0x11);
  320. cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH28]);
  321. }
  322. /* risc1 y */
  323. if (status & 0x01) {
  324. dprintk( 1, "wake up\n" );
  325. spin_lock(&dev->slock);
  326. count = cx_read(MO_TS_GPCNT);
  327. cx88_wakeup(dev->core, &dev->mpegq, count);
  328. spin_unlock(&dev->slock);
  329. }
  330. /* risc2 y */
  331. if (status & 0x10) {
  332. spin_lock(&dev->slock);
  333. cx8802_restart_queue(dev,&dev->mpegq);
  334. spin_unlock(&dev->slock);
  335. }
  336. /* other general errors */
  337. if (status & 0x1f0100) {
  338. dprintk( 0, "general errors: 0x%08x\n", status & 0x1f0100 );
  339. spin_lock(&dev->slock);
  340. cx8802_stop_dma(dev);
  341. cx8802_restart_queue(dev,&dev->mpegq);
  342. spin_unlock(&dev->slock);
  343. }
  344. }
  345. #define MAX_IRQ_LOOP 10
  346. static irqreturn_t cx8802_irq(int irq, void *dev_id)
  347. {
  348. struct cx8802_dev *dev = dev_id;
  349. struct cx88_core *core = dev->core;
  350. u32 status;
  351. int loop, handled = 0;
  352. for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
  353. status = cx_read(MO_PCI_INTSTAT) &
  354. (core->pci_irqmask | PCI_INT_TSINT);
  355. if (0 == status)
  356. goto out;
  357. dprintk( 1, "cx8802_irq\n" );
  358. dprintk( 1, " loop: %d/%d\n", loop, MAX_IRQ_LOOP );
  359. dprintk( 1, " status: %d\n", status );
  360. handled = 1;
  361. cx_write(MO_PCI_INTSTAT, status);
  362. if (status & core->pci_irqmask)
  363. cx88_core_irq(core,status);
  364. if (status & PCI_INT_TSINT)
  365. cx8802_mpeg_irq(dev);
  366. };
  367. if (MAX_IRQ_LOOP == loop) {
  368. dprintk( 0, "clearing mask\n" );
  369. printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
  370. core->name);
  371. cx_write(MO_PCI_INTMSK,0);
  372. }
  373. out:
  374. return IRQ_RETVAL(handled);
  375. }
  376. static int cx8802_init_common(struct cx8802_dev *dev)
  377. {
  378. struct cx88_core *core = dev->core;
  379. int err;
  380. /* pci init */
  381. if (pci_enable_device(dev->pci))
  382. return -EIO;
  383. pci_set_master(dev->pci);
  384. if (!pci_dma_supported(dev->pci,DMA_32BIT_MASK)) {
  385. printk("%s/2: Oops: no 32bit PCI DMA ???\n",dev->core->name);
  386. return -EIO;
  387. }
  388. pci_read_config_byte(dev->pci, PCI_CLASS_REVISION, &dev->pci_rev);
  389. pci_read_config_byte(dev->pci, PCI_LATENCY_TIMER, &dev->pci_lat);
  390. printk(KERN_INFO "%s/2: found at %s, rev: %d, irq: %d, "
  391. "latency: %d, mmio: 0x%llx\n", dev->core->name,
  392. pci_name(dev->pci), dev->pci_rev, dev->pci->irq,
  393. dev->pci_lat,(unsigned long long)pci_resource_start(dev->pci,0));
  394. /* initialize driver struct */
  395. spin_lock_init(&dev->slock);
  396. /* init dma queue */
  397. INIT_LIST_HEAD(&dev->mpegq.active);
  398. INIT_LIST_HEAD(&dev->mpegq.queued);
  399. dev->mpegq.timeout.function = cx8802_timeout;
  400. dev->mpegq.timeout.data = (unsigned long)dev;
  401. init_timer(&dev->mpegq.timeout);
  402. cx88_risc_stopper(dev->pci,&dev->mpegq.stopper,
  403. MO_TS_DMACNTRL,0x11,0x00);
  404. /* get irq */
  405. err = request_irq(dev->pci->irq, cx8802_irq,
  406. IRQF_SHARED | IRQF_DISABLED, dev->core->name, dev);
  407. if (err < 0) {
  408. printk(KERN_ERR "%s: can't get IRQ %d\n",
  409. dev->core->name, dev->pci->irq);
  410. return err;
  411. }
  412. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  413. /* everything worked */
  414. pci_set_drvdata(dev->pci,dev);
  415. return 0;
  416. }
  417. static void cx8802_fini_common(struct cx8802_dev *dev)
  418. {
  419. dprintk( 2, "cx8802_fini_common\n" );
  420. cx8802_stop_dma(dev);
  421. pci_disable_device(dev->pci);
  422. /* unregister stuff */
  423. free_irq(dev->pci->irq, dev);
  424. pci_set_drvdata(dev->pci, NULL);
  425. /* free memory */
  426. btcx_riscmem_free(dev->pci,&dev->mpegq.stopper);
  427. }
  428. /* ----------------------------------------------------------- */
  429. static int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state)
  430. {
  431. struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
  432. struct cx88_core *core = dev->core;
  433. /* stop mpeg dma */
  434. spin_lock(&dev->slock);
  435. if (!list_empty(&dev->mpegq.active)) {
  436. dprintk( 2, "suspend\n" );
  437. printk("%s: suspend mpeg\n", core->name);
  438. cx8802_stop_dma(dev);
  439. del_timer(&dev->mpegq.timeout);
  440. }
  441. spin_unlock(&dev->slock);
  442. /* FIXME -- shutdown device */
  443. cx88_shutdown(dev->core);
  444. pci_save_state(pci_dev);
  445. if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
  446. pci_disable_device(pci_dev);
  447. dev->state.disabled = 1;
  448. }
  449. return 0;
  450. }
  451. static int cx8802_resume_common(struct pci_dev *pci_dev)
  452. {
  453. struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
  454. struct cx88_core *core = dev->core;
  455. int err;
  456. if (dev->state.disabled) {
  457. err=pci_enable_device(pci_dev);
  458. if (err) {
  459. printk(KERN_ERR "%s: can't enable device\n",
  460. dev->core->name);
  461. return err;
  462. }
  463. dev->state.disabled = 0;
  464. }
  465. err=pci_set_power_state(pci_dev, PCI_D0);
  466. if (err) {
  467. printk(KERN_ERR "%s: can't enable device\n",
  468. dev->core->name);
  469. pci_disable_device(pci_dev);
  470. dev->state.disabled = 1;
  471. return err;
  472. }
  473. pci_restore_state(pci_dev);
  474. /* FIXME: re-initialize hardware */
  475. cx88_reset(dev->core);
  476. /* restart video+vbi capture */
  477. spin_lock(&dev->slock);
  478. if (!list_empty(&dev->mpegq.active)) {
  479. printk("%s: resume mpeg\n", core->name);
  480. cx8802_restart_queue(dev,&dev->mpegq);
  481. }
  482. spin_unlock(&dev->slock);
  483. return 0;
  484. }
  485. #if defined(CONFIG_VIDEO_CX88_BLACKBIRD) || \
  486. defined(CONFIG_VIDEO_CX88_BLACKBIRD_MODULE)
  487. struct cx8802_dev * cx8802_get_device(struct inode *inode)
  488. {
  489. int minor = iminor(inode);
  490. struct cx8802_dev *dev;
  491. list_for_each_entry(dev, &cx8802_devlist, devlist)
  492. if (dev->mpeg_dev && dev->mpeg_dev->minor == minor)
  493. return dev;
  494. return NULL;
  495. }
  496. EXPORT_SYMBOL(cx8802_get_device);
  497. #endif
  498. struct cx8802_driver * cx8802_get_driver(struct cx8802_dev *dev, enum cx88_board_type btype)
  499. {
  500. struct cx8802_driver *d;
  501. list_for_each_entry(d, &dev->drvlist, drvlist)
  502. if (d->type_id == btype)
  503. return d;
  504. return NULL;
  505. }
  506. /* Driver asked for hardware access. */
  507. static int cx8802_request_acquire(struct cx8802_driver *drv)
  508. {
  509. struct cx88_core *core = drv->core;
  510. /* Fail a request for hardware if the device is busy. */
  511. if (core->active_type_id != CX88_BOARD_NONE)
  512. return -EBUSY;
  513. if (drv->advise_acquire)
  514. {
  515. core->active_type_id = drv->type_id;
  516. drv->advise_acquire(drv);
  517. mpeg_dbg(1,"%s() Post acquire GPIO=%x\n", __FUNCTION__, cx_read(MO_GP0_IO));
  518. }
  519. return 0;
  520. }
  521. /* Driver asked to release hardware. */
  522. static int cx8802_request_release(struct cx8802_driver *drv)
  523. {
  524. struct cx88_core *core = drv->core;
  525. if (drv->advise_release)
  526. {
  527. drv->advise_release(drv);
  528. core->active_type_id = CX88_BOARD_NONE;
  529. mpeg_dbg(1,"%s() Post release GPIO=%x\n", __FUNCTION__, cx_read(MO_GP0_IO));
  530. }
  531. return 0;
  532. }
  533. static int cx8802_check_driver(struct cx8802_driver *drv)
  534. {
  535. if (drv == NULL)
  536. return -ENODEV;
  537. if ((drv->type_id != CX88_MPEG_DVB) &&
  538. (drv->type_id != CX88_MPEG_BLACKBIRD))
  539. return -EINVAL;
  540. if ((drv->hw_access != CX8802_DRVCTL_SHARED) &&
  541. (drv->hw_access != CX8802_DRVCTL_EXCLUSIVE))
  542. return -EINVAL;
  543. if ((drv->probe == NULL) ||
  544. (drv->remove == NULL) ||
  545. (drv->advise_acquire == NULL) ||
  546. (drv->advise_release == NULL))
  547. return -EINVAL;
  548. return 0;
  549. }
  550. int cx8802_register_driver(struct cx8802_driver *drv)
  551. {
  552. struct cx8802_dev *dev;
  553. struct cx8802_driver *driver;
  554. int err, i = 0;
  555. printk(KERN_INFO
  556. "cx88/2: registering cx8802 driver, type: %s access: %s\n",
  557. drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird",
  558. drv->hw_access == CX8802_DRVCTL_SHARED ? "shared" : "exclusive");
  559. if ((err = cx8802_check_driver(drv)) != 0) {
  560. printk(KERN_ERR "cx88/2: cx8802_driver is invalid\n");
  561. return err;
  562. }
  563. list_for_each_entry(dev, &cx8802_devlist, devlist) {
  564. printk(KERN_INFO
  565. "%s/2: subsystem: %04x:%04x, board: %s [card=%d]\n",
  566. dev->core->name, dev->pci->subsystem_vendor,
  567. dev->pci->subsystem_device, dev->core->board.name,
  568. dev->core->boardnr);
  569. /* Bring up a new struct for each driver instance */
  570. driver = kzalloc(sizeof(*drv),GFP_KERNEL);
  571. if (driver == NULL)
  572. return -ENOMEM;
  573. /* Snapshot of the driver registration data */
  574. drv->core = dev->core;
  575. drv->suspend = cx8802_suspend_common;
  576. drv->resume = cx8802_resume_common;
  577. drv->request_acquire = cx8802_request_acquire;
  578. drv->request_release = cx8802_request_release;
  579. memcpy(driver, drv, sizeof(*driver));
  580. err = drv->probe(driver);
  581. if (err == 0) {
  582. i++;
  583. mutex_lock(&drv->core->lock);
  584. list_add_tail(&driver->drvlist, &dev->drvlist);
  585. mutex_unlock(&drv->core->lock);
  586. } else {
  587. printk(KERN_ERR
  588. "%s/2: cx8802 probe failed, err = %d\n",
  589. dev->core->name, err);
  590. }
  591. }
  592. return i ? 0 : -ENODEV;
  593. }
  594. int cx8802_unregister_driver(struct cx8802_driver *drv)
  595. {
  596. struct cx8802_dev *dev;
  597. struct cx8802_driver *d, *dtmp;
  598. int err = 0;
  599. printk(KERN_INFO
  600. "cx88/2: unregistering cx8802 driver, type: %s access: %s\n",
  601. drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird",
  602. drv->hw_access == CX8802_DRVCTL_SHARED ? "shared" : "exclusive");
  603. list_for_each_entry(dev, &cx8802_devlist, devlist) {
  604. printk(KERN_INFO
  605. "%s/2: subsystem: %04x:%04x, board: %s [card=%d]\n",
  606. dev->core->name, dev->pci->subsystem_vendor,
  607. dev->pci->subsystem_device, dev->core->board.name,
  608. dev->core->boardnr);
  609. list_for_each_entry_safe(d, dtmp, &dev->drvlist, drvlist) {
  610. /* only unregister the correct driver type */
  611. if (d->type_id != drv->type_id)
  612. continue;
  613. err = d->remove(d);
  614. if (err == 0) {
  615. mutex_lock(&drv->core->lock);
  616. list_del(&d->drvlist);
  617. mutex_unlock(&drv->core->lock);
  618. kfree(d);
  619. } else
  620. printk(KERN_ERR "%s/2: cx8802 driver remove "
  621. "failed (%d)\n", dev->core->name, err);
  622. }
  623. }
  624. return err;
  625. }
  626. /* ----------------------------------------------------------- */
  627. static int __devinit cx8802_probe(struct pci_dev *pci_dev,
  628. const struct pci_device_id *pci_id)
  629. {
  630. struct cx8802_dev *dev;
  631. struct cx88_core *core;
  632. int err;
  633. /* general setup */
  634. core = cx88_core_get(pci_dev);
  635. if (NULL == core)
  636. return -EINVAL;
  637. printk("%s/2: cx2388x 8802 Driver Manager\n", core->name);
  638. err = -ENODEV;
  639. if (!core->board.mpeg)
  640. goto fail_core;
  641. err = -ENOMEM;
  642. dev = kzalloc(sizeof(*dev),GFP_KERNEL);
  643. if (NULL == dev)
  644. goto fail_core;
  645. dev->pci = pci_dev;
  646. dev->core = core;
  647. err = cx8802_init_common(dev);
  648. if (err != 0)
  649. goto fail_free;
  650. INIT_LIST_HEAD(&dev->drvlist);
  651. list_add_tail(&dev->devlist,&cx8802_devlist);
  652. /* Maintain a reference so cx88-video can query the 8802 device. */
  653. core->dvbdev = dev;
  654. /* now autoload cx88-dvb or cx88-blackbird */
  655. request_modules(dev);
  656. return 0;
  657. fail_free:
  658. kfree(dev);
  659. fail_core:
  660. cx88_core_put(core,pci_dev);
  661. return err;
  662. }
  663. static void __devexit cx8802_remove(struct pci_dev *pci_dev)
  664. {
  665. struct cx8802_dev *dev;
  666. dev = pci_get_drvdata(pci_dev);
  667. dprintk( 1, "%s\n", __FUNCTION__);
  668. if (!list_empty(&dev->drvlist)) {
  669. struct cx8802_driver *drv, *tmp;
  670. int err;
  671. printk(KERN_WARNING "%s/2: Trying to remove cx8802 driver "
  672. "while cx8802 sub-drivers still loaded?!\n",
  673. dev->core->name);
  674. list_for_each_entry_safe(drv, tmp, &dev->drvlist, drvlist) {
  675. err = drv->remove(drv);
  676. if (err == 0) {
  677. mutex_lock(&drv->core->lock);
  678. list_del(&drv->drvlist);
  679. mutex_unlock(&drv->core->lock);
  680. } else
  681. printk(KERN_ERR "%s/2: cx8802 driver remove "
  682. "failed (%d)\n", dev->core->name, err);
  683. kfree(drv);
  684. }
  685. }
  686. /* Destroy any 8802 reference. */
  687. dev->core->dvbdev = NULL;
  688. /* common */
  689. cx8802_fini_common(dev);
  690. cx88_core_put(dev->core,dev->pci);
  691. kfree(dev);
  692. }
  693. static struct pci_device_id cx8802_pci_tbl[] = {
  694. {
  695. .vendor = 0x14f1,
  696. .device = 0x8802,
  697. .subvendor = PCI_ANY_ID,
  698. .subdevice = PCI_ANY_ID,
  699. },{
  700. /* --- end of list --- */
  701. }
  702. };
  703. MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
  704. static struct pci_driver cx8802_pci_driver = {
  705. .name = "cx88-mpeg driver manager",
  706. .id_table = cx8802_pci_tbl,
  707. .probe = cx8802_probe,
  708. .remove = __devexit_p(cx8802_remove),
  709. };
  710. static int cx8802_init(void)
  711. {
  712. printk(KERN_INFO "cx88/2: cx2388x MPEG-TS Driver Manager version %d.%d.%d loaded\n",
  713. (CX88_VERSION_CODE >> 16) & 0xff,
  714. (CX88_VERSION_CODE >> 8) & 0xff,
  715. CX88_VERSION_CODE & 0xff);
  716. #ifdef SNAPSHOT
  717. printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
  718. SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
  719. #endif
  720. return pci_register_driver(&cx8802_pci_driver);
  721. }
  722. static void cx8802_fini(void)
  723. {
  724. pci_unregister_driver(&cx8802_pci_driver);
  725. }
  726. module_init(cx8802_init);
  727. module_exit(cx8802_fini);
  728. EXPORT_SYMBOL(cx8802_buf_prepare);
  729. EXPORT_SYMBOL(cx8802_buf_queue);
  730. EXPORT_SYMBOL(cx8802_cancel_buffers);
  731. EXPORT_SYMBOL(cx8802_register_driver);
  732. EXPORT_SYMBOL(cx8802_unregister_driver);
  733. EXPORT_SYMBOL(cx8802_get_driver);
  734. /* ----------------------------------------------------------- */
  735. /*
  736. * Local variables:
  737. * c-basic-offset: 8
  738. * End:
  739. * 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
  740. */