cx88-mpeg.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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/moduleparam.h>
  26. #include <linux/init.h>
  27. #include <linux/device.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: " fmt, dev->core->name , ## arg)
  42. /* ------------------------------------------------------------------ */
  43. static int cx8802_start_dma(struct cx8802_dev *dev,
  44. struct cx88_dmaqueue *q,
  45. struct cx88_buffer *buf)
  46. {
  47. struct cx88_core *core = dev->core;
  48. dprintk(1, "cx8802_start_dma w: %d, h: %d, f: %d\n", dev->width, dev->height, buf->vb.field);
  49. /* setup fifo + format */
  50. cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH28],
  51. dev->ts_packet_size, buf->risc.dma);
  52. /* write TS length to chip */
  53. cx_write(MO_TS_LNGTH, buf->vb.width);
  54. /* FIXME: this needs a review.
  55. * also: move to cx88-blackbird + cx88-dvb source files? */
  56. if (cx88_boards[core->board].dvb) {
  57. /* negedge driven & software reset */
  58. cx_write(TS_GEN_CNTRL, 0x0040 | dev->ts_gen_cntrl);
  59. udelay(100);
  60. cx_write(MO_PINMUX_IO, 0x00);
  61. cx_write(TS_HW_SOP_CNTRL,0x47<<16|188<<4|0x01);
  62. switch (core->board) {
  63. case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q:
  64. case CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_T:
  65. case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
  66. case CX88_BOARD_PCHDTV_HD5500:
  67. cx_write(TS_SOP_STAT, 1<<13);
  68. break;
  69. case CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1:
  70. case CX88_BOARD_HAUPPAUGE_NOVASE2_S1:
  71. cx_write(MO_PINMUX_IO, 0x88); /* Enable MPEG parallel IO and video signal pins */
  72. udelay(100);
  73. break;
  74. default:
  75. cx_write(TS_SOP_STAT, 0x00);
  76. break;
  77. }
  78. cx_write(TS_GEN_CNTRL, dev->ts_gen_cntrl);
  79. udelay(100);
  80. }
  81. if (cx88_boards[core->board].blackbird) {
  82. cx_write(MO_PINMUX_IO, 0x88); /* enable MPEG parallel IO */
  83. cx_write(TS_GEN_CNTRL, 0x46); /* punctured clock TS & posedge driven & software reset */
  84. udelay(100);
  85. cx_write(TS_HW_SOP_CNTRL, 0x408); /* mpeg start byte */
  86. cx_write(TS_VALERR_CNTRL, 0x2000);
  87. cx_write(TS_GEN_CNTRL, 0x06); /* punctured clock TS & posedge driven */
  88. udelay(100);
  89. }
  90. /* reset counter */
  91. cx_write(MO_TS_GPCNTRL, GP_COUNT_CONTROL_RESET);
  92. q->count = 1;
  93. /* enable irqs */
  94. dprintk( 1, "setting the interrupt mask\n" );
  95. cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x04);
  96. cx_set(MO_TS_INTMSK, 0x1f0011);
  97. /* start dma */
  98. cx_set(MO_DEV_CNTRL2, (1<<5));
  99. cx_set(MO_TS_DMACNTRL, 0x11);
  100. return 0;
  101. }
  102. static int cx8802_stop_dma(struct cx8802_dev *dev)
  103. {
  104. struct cx88_core *core = dev->core;
  105. dprintk( 1, "cx8802_stop_dma\n" );
  106. /* stop dma */
  107. cx_clear(MO_TS_DMACNTRL, 0x11);
  108. /* disable irqs */
  109. cx_clear(MO_PCI_INTMSK, 0x000004);
  110. cx_clear(MO_TS_INTMSK, 0x1f0011);
  111. /* Reset the controller */
  112. cx_write(TS_GEN_CNTRL, 0xcd);
  113. return 0;
  114. }
  115. static int cx8802_restart_queue(struct cx8802_dev *dev,
  116. struct cx88_dmaqueue *q)
  117. {
  118. struct cx88_buffer *buf;
  119. struct list_head *item;
  120. dprintk( 1, "cx8802_restart_queue\n" );
  121. if (list_empty(&q->active))
  122. {
  123. struct cx88_buffer *prev;
  124. prev = NULL;
  125. dprintk(1, "cx8802_restart_queue: queue is empty\n" );
  126. for (;;) {
  127. if (list_empty(&q->queued))
  128. return 0;
  129. buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
  130. if (NULL == prev) {
  131. list_del(&buf->vb.queue);
  132. list_add_tail(&buf->vb.queue,&q->active);
  133. cx8802_start_dma(dev, q, buf);
  134. buf->vb.state = STATE_ACTIVE;
  135. buf->count = q->count++;
  136. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  137. dprintk(1,"[%p/%d] restart_queue - first active\n",
  138. buf,buf->vb.i);
  139. } else if (prev->vb.width == buf->vb.width &&
  140. prev->vb.height == buf->vb.height &&
  141. prev->fmt == buf->fmt) {
  142. list_del(&buf->vb.queue);
  143. list_add_tail(&buf->vb.queue,&q->active);
  144. buf->vb.state = STATE_ACTIVE;
  145. buf->count = q->count++;
  146. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  147. dprintk(1,"[%p/%d] restart_queue - move to active\n",
  148. buf,buf->vb.i);
  149. } else {
  150. return 0;
  151. }
  152. prev = buf;
  153. }
  154. return 0;
  155. }
  156. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  157. dprintk(2,"restart_queue [%p/%d]: restart dma\n",
  158. buf, buf->vb.i);
  159. cx8802_start_dma(dev, q, buf);
  160. list_for_each(item,&q->active) {
  161. buf = list_entry(item, struct cx88_buffer, vb.queue);
  162. buf->count = q->count++;
  163. }
  164. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  165. return 0;
  166. }
  167. /* ------------------------------------------------------------------ */
  168. int cx8802_buf_prepare(struct videobuf_queue *q, struct cx8802_dev *dev,
  169. struct cx88_buffer *buf, enum v4l2_field field)
  170. {
  171. int size = dev->ts_packet_size * dev->ts_packet_count;
  172. int rc;
  173. dprintk(1, "%s: %p\n", __FUNCTION__, buf);
  174. if (0 != buf->vb.baddr && buf->vb.bsize < size)
  175. return -EINVAL;
  176. if (STATE_NEEDS_INIT == buf->vb.state) {
  177. buf->vb.width = dev->ts_packet_size;
  178. buf->vb.height = dev->ts_packet_count;
  179. buf->vb.size = size;
  180. buf->vb.field = field /*V4L2_FIELD_TOP*/;
  181. if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
  182. goto fail;
  183. cx88_risc_databuffer(dev->pci, &buf->risc,
  184. buf->vb.dma.sglist,
  185. buf->vb.width, buf->vb.height);
  186. }
  187. buf->vb.state = STATE_PREPARED;
  188. return 0;
  189. fail:
  190. cx88_free_buffer(q,buf);
  191. return rc;
  192. }
  193. void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf)
  194. {
  195. struct cx88_buffer *prev;
  196. struct cx88_dmaqueue *cx88q = &dev->mpegq;
  197. dprintk( 1, "cx8802_buf_queue\n" );
  198. /* add jump to stopper */
  199. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  200. buf->risc.jmp[1] = cpu_to_le32(cx88q->stopper.dma);
  201. if (list_empty(&cx88q->active)) {
  202. dprintk( 1, "queue is empty - first active\n" );
  203. list_add_tail(&buf->vb.queue,&cx88q->active);
  204. cx8802_start_dma(dev, cx88q, buf);
  205. buf->vb.state = STATE_ACTIVE;
  206. buf->count = cx88q->count++;
  207. mod_timer(&cx88q->timeout, jiffies+BUFFER_TIMEOUT);
  208. dprintk(1,"[%p/%d] %s - first active\n",
  209. buf, buf->vb.i, __FUNCTION__);
  210. } else {
  211. dprintk( 1, "queue is not empty - append to active\n" );
  212. prev = list_entry(cx88q->active.prev, struct cx88_buffer, vb.queue);
  213. list_add_tail(&buf->vb.queue,&cx88q->active);
  214. buf->vb.state = STATE_ACTIVE;
  215. buf->count = cx88q->count++;
  216. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  217. dprintk( 1, "[%p/%d] %s - append to active\n",
  218. buf, buf->vb.i, __FUNCTION__);
  219. }
  220. }
  221. /* ----------------------------------------------------------- */
  222. static void do_cancel_buffers(struct cx8802_dev *dev, char *reason, int restart)
  223. {
  224. struct cx88_dmaqueue *q = &dev->mpegq;
  225. struct cx88_buffer *buf;
  226. unsigned long flags;
  227. spin_lock_irqsave(&dev->slock,flags);
  228. while (!list_empty(&q->active)) {
  229. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  230. list_del(&buf->vb.queue);
  231. buf->vb.state = STATE_ERROR;
  232. wake_up(&buf->vb.done);
  233. dprintk(1,"[%p/%d] %s - dma=0x%08lx\n",
  234. buf, buf->vb.i, reason, (unsigned long)buf->risc.dma);
  235. }
  236. if (restart)
  237. {
  238. dprintk(1, "restarting queue\n" );
  239. cx8802_restart_queue(dev,q);
  240. }
  241. spin_unlock_irqrestore(&dev->slock,flags);
  242. }
  243. void cx8802_cancel_buffers(struct cx8802_dev *dev)
  244. {
  245. struct cx88_dmaqueue *q = &dev->mpegq;
  246. dprintk( 1, "cx8802_cancel_buffers" );
  247. del_timer_sync(&q->timeout);
  248. cx8802_stop_dma(dev);
  249. do_cancel_buffers(dev,"cancel",0);
  250. }
  251. static void cx8802_timeout(unsigned long data)
  252. {
  253. struct cx8802_dev *dev = (struct cx8802_dev*)data;
  254. dprintk(0, "%s\n",__FUNCTION__);
  255. if (debug)
  256. cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH28]);
  257. cx8802_stop_dma(dev);
  258. do_cancel_buffers(dev,"timeout",1);
  259. }
  260. static char *cx88_mpeg_irqs[32] = {
  261. "ts_risci1", NULL, NULL, NULL,
  262. "ts_risci2", NULL, NULL, NULL,
  263. "ts_oflow", NULL, NULL, NULL,
  264. "ts_sync", NULL, NULL, NULL,
  265. "opc_err", "par_err", "rip_err", "pci_abort",
  266. "ts_err?",
  267. };
  268. static void cx8802_mpeg_irq(struct cx8802_dev *dev)
  269. {
  270. struct cx88_core *core = dev->core;
  271. u32 status, mask, count;
  272. dprintk( 1, "cx8802_mpeg_irq\n" );
  273. status = cx_read(MO_TS_INTSTAT);
  274. mask = cx_read(MO_TS_INTMSK);
  275. if (0 == (status & mask))
  276. return;
  277. cx_write(MO_TS_INTSTAT, status);
  278. if (debug || (status & mask & ~0xff))
  279. cx88_print_irqbits(core->name, "irq mpeg ",
  280. cx88_mpeg_irqs, status, mask);
  281. /* risc op code error */
  282. if (status & (1 << 16)) {
  283. printk(KERN_WARNING "%s: mpeg risc op code error\n",core->name);
  284. cx_clear(MO_TS_DMACNTRL, 0x11);
  285. cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH28]);
  286. }
  287. /* risc1 y */
  288. if (status & 0x01) {
  289. dprintk( 1, "wake up\n" );
  290. spin_lock(&dev->slock);
  291. count = cx_read(MO_TS_GPCNT);
  292. cx88_wakeup(dev->core, &dev->mpegq, count);
  293. spin_unlock(&dev->slock);
  294. }
  295. /* risc2 y */
  296. if (status & 0x10) {
  297. spin_lock(&dev->slock);
  298. cx8802_restart_queue(dev,&dev->mpegq);
  299. spin_unlock(&dev->slock);
  300. }
  301. /* other general errors */
  302. if (status & 0x1f0100) {
  303. dprintk( 0, "general errors: 0x%08x\n", status & 0x1f0100 );
  304. spin_lock(&dev->slock);
  305. cx8802_stop_dma(dev);
  306. cx8802_restart_queue(dev,&dev->mpegq);
  307. spin_unlock(&dev->slock);
  308. }
  309. }
  310. #define MAX_IRQ_LOOP 10
  311. static irqreturn_t cx8802_irq(int irq, void *dev_id, struct pt_regs *regs)
  312. {
  313. struct cx8802_dev *dev = dev_id;
  314. struct cx88_core *core = dev->core;
  315. u32 status;
  316. int loop, handled = 0;
  317. for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
  318. status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x04);
  319. if (0 == status)
  320. goto out;
  321. dprintk( 1, "cx8802_irq\n" );
  322. dprintk( 1, " loop: %d/%d\n", loop, MAX_IRQ_LOOP );
  323. dprintk( 1, " status: %d\n", status );
  324. handled = 1;
  325. cx_write(MO_PCI_INTSTAT, status);
  326. if (status & core->pci_irqmask)
  327. cx88_core_irq(core,status);
  328. if (status & 0x04)
  329. cx8802_mpeg_irq(dev);
  330. };
  331. if (MAX_IRQ_LOOP == loop) {
  332. dprintk( 0, "clearing mask\n" );
  333. printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
  334. core->name);
  335. cx_write(MO_PCI_INTMSK,0);
  336. }
  337. out:
  338. return IRQ_RETVAL(handled);
  339. }
  340. /* ----------------------------------------------------------- */
  341. /* exported stuff */
  342. int cx8802_init_common(struct cx8802_dev *dev)
  343. {
  344. struct cx88_core *core = dev->core;
  345. int err;
  346. /* pci init */
  347. if (pci_enable_device(dev->pci))
  348. return -EIO;
  349. pci_set_master(dev->pci);
  350. if (!pci_dma_supported(dev->pci,0xffffffff)) {
  351. printk("%s/2: Oops: no 32bit PCI DMA ???\n",dev->core->name);
  352. return -EIO;
  353. }
  354. pci_read_config_byte(dev->pci, PCI_CLASS_REVISION, &dev->pci_rev);
  355. pci_read_config_byte(dev->pci, PCI_LATENCY_TIMER, &dev->pci_lat);
  356. printk(KERN_INFO "%s/2: found at %s, rev: %d, irq: %d, "
  357. "latency: %d, mmio: 0x%llx\n", dev->core->name,
  358. pci_name(dev->pci), dev->pci_rev, dev->pci->irq,
  359. dev->pci_lat,(unsigned long long)pci_resource_start(dev->pci,0));
  360. /* initialize driver struct */
  361. spin_lock_init(&dev->slock);
  362. /* init dma queue */
  363. INIT_LIST_HEAD(&dev->mpegq.active);
  364. INIT_LIST_HEAD(&dev->mpegq.queued);
  365. dev->mpegq.timeout.function = cx8802_timeout;
  366. dev->mpegq.timeout.data = (unsigned long)dev;
  367. init_timer(&dev->mpegq.timeout);
  368. cx88_risc_stopper(dev->pci,&dev->mpegq.stopper,
  369. MO_TS_DMACNTRL,0x11,0x00);
  370. /* get irq */
  371. err = request_irq(dev->pci->irq, cx8802_irq,
  372. SA_SHIRQ | SA_INTERRUPT, dev->core->name, dev);
  373. if (err < 0) {
  374. printk(KERN_ERR "%s: can't get IRQ %d\n",
  375. dev->core->name, dev->pci->irq);
  376. return err;
  377. }
  378. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  379. /* everything worked */
  380. pci_set_drvdata(dev->pci,dev);
  381. return 0;
  382. }
  383. void cx8802_fini_common(struct cx8802_dev *dev)
  384. {
  385. dprintk( 2, "cx8802_fini_common\n" );
  386. cx8802_stop_dma(dev);
  387. pci_disable_device(dev->pci);
  388. /* unregister stuff */
  389. free_irq(dev->pci->irq, dev);
  390. pci_set_drvdata(dev->pci, NULL);
  391. /* free memory */
  392. btcx_riscmem_free(dev->pci,&dev->mpegq.stopper);
  393. }
  394. /* ----------------------------------------------------------- */
  395. int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state)
  396. {
  397. struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
  398. struct cx88_core *core = dev->core;
  399. /* stop mpeg dma */
  400. spin_lock(&dev->slock);
  401. if (!list_empty(&dev->mpegq.active)) {
  402. dprintk( 2, "suspend\n" );
  403. printk("%s: suspend mpeg\n", core->name);
  404. cx8802_stop_dma(dev);
  405. del_timer(&dev->mpegq.timeout);
  406. }
  407. spin_unlock(&dev->slock);
  408. /* FIXME -- shutdown device */
  409. cx88_shutdown(dev->core);
  410. pci_save_state(pci_dev);
  411. if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
  412. pci_disable_device(pci_dev);
  413. dev->state.disabled = 1;
  414. }
  415. return 0;
  416. }
  417. int cx8802_resume_common(struct pci_dev *pci_dev)
  418. {
  419. struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
  420. struct cx88_core *core = dev->core;
  421. int err;
  422. if (dev->state.disabled) {
  423. err=pci_enable_device(pci_dev);
  424. if (err) {
  425. printk(KERN_ERR "%s: can't enable device\n",
  426. dev->core->name);
  427. return err;
  428. }
  429. dev->state.disabled = 0;
  430. }
  431. err=pci_set_power_state(pci_dev, PCI_D0);
  432. if (err) {
  433. printk(KERN_ERR "%s: can't enable device\n",
  434. dev->core->name);
  435. pci_disable_device(pci_dev);
  436. dev->state.disabled = 1;
  437. return err;
  438. }
  439. pci_restore_state(pci_dev);
  440. /* FIXME: re-initialize hardware */
  441. cx88_reset(dev->core);
  442. /* restart video+vbi capture */
  443. spin_lock(&dev->slock);
  444. if (!list_empty(&dev->mpegq.active)) {
  445. printk("%s: resume mpeg\n", core->name);
  446. cx8802_restart_queue(dev,&dev->mpegq);
  447. }
  448. spin_unlock(&dev->slock);
  449. return 0;
  450. }
  451. /* ----------------------------------------------------------- */
  452. EXPORT_SYMBOL(cx8802_buf_prepare);
  453. EXPORT_SYMBOL(cx8802_buf_queue);
  454. EXPORT_SYMBOL(cx8802_cancel_buffers);
  455. EXPORT_SYMBOL(cx8802_init_common);
  456. EXPORT_SYMBOL(cx8802_fini_common);
  457. EXPORT_SYMBOL(cx8802_suspend_common);
  458. EXPORT_SYMBOL(cx8802_resume_common);
  459. /* ----------------------------------------------------------- */
  460. /*
  461. * Local variables:
  462. * c-basic-offset: 8
  463. * End:
  464. * 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
  465. */