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