cx88-mpeg.c 24 KB

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