cx88-mpeg.c 24 KB

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