cx88-mpeg.c 24 KB

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