cx88-mpeg.c 16 KB

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