cx88-mpeg.c 23 KB

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