cx88-mpeg.c 20 KB

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