cx88-mpeg.c 24 KB

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