cx88-mpeg.c 21 KB

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