cx88-mpeg.c 24 KB

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