cx88-mpeg.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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. * 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 <jelle@foks.us>");
  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. 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. static 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. buf->vb.width, buf->vb.height, buf->vb.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, buf->vb.width);
  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 = 1;
  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. {
  181. struct cx88_buffer *prev;
  182. prev = NULL;
  183. dprintk(1, "cx8802_restart_queue: queue is empty\n" );
  184. for (;;) {
  185. if (list_empty(&q->queued))
  186. return 0;
  187. buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
  188. if (NULL == prev) {
  189. list_move_tail(&buf->vb.queue, &q->active);
  190. cx8802_start_dma(dev, q, buf);
  191. buf->vb.state = VIDEOBUF_ACTIVE;
  192. buf->count = q->count++;
  193. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  194. dprintk(1,"[%p/%d] restart_queue - first active\n",
  195. buf,buf->vb.i);
  196. } else if (prev->vb.width == buf->vb.width &&
  197. prev->vb.height == buf->vb.height &&
  198. prev->fmt == buf->fmt) {
  199. list_move_tail(&buf->vb.queue, &q->active);
  200. buf->vb.state = VIDEOBUF_ACTIVE;
  201. buf->count = q->count++;
  202. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  203. dprintk(1,"[%p/%d] restart_queue - move to active\n",
  204. buf,buf->vb.i);
  205. } else {
  206. return 0;
  207. }
  208. prev = buf;
  209. }
  210. return 0;
  211. }
  212. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  213. dprintk(2,"restart_queue [%p/%d]: restart dma\n",
  214. buf, buf->vb.i);
  215. cx8802_start_dma(dev, q, buf);
  216. list_for_each_entry(buf, &q->active, vb.queue)
  217. buf->count = q->count++;
  218. mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
  219. return 0;
  220. }
  221. /* ------------------------------------------------------------------ */
  222. int cx8802_buf_prepare(struct videobuf_queue *q, struct cx8802_dev *dev,
  223. struct cx88_buffer *buf, enum v4l2_field field)
  224. {
  225. int size = dev->ts_packet_size * dev->ts_packet_count;
  226. struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
  227. int rc;
  228. dprintk(1, "%s: %p\n", __func__, buf);
  229. if (0 != buf->vb.baddr && buf->vb.bsize < size)
  230. return -EINVAL;
  231. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  232. buf->vb.width = dev->ts_packet_size;
  233. buf->vb.height = dev->ts_packet_count;
  234. buf->vb.size = size;
  235. buf->vb.field = field /*V4L2_FIELD_TOP*/;
  236. if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
  237. goto fail;
  238. cx88_risc_databuffer(dev->pci, &buf->risc,
  239. dma->sglist,
  240. buf->vb.width, buf->vb.height, 0);
  241. }
  242. buf->vb.state = VIDEOBUF_PREPARED;
  243. return 0;
  244. fail:
  245. cx88_free_buffer(q,buf);
  246. return rc;
  247. }
  248. void cx8802_buf_queue(struct cx8802_dev *dev, struct cx88_buffer *buf)
  249. {
  250. struct cx88_buffer *prev;
  251. struct cx88_dmaqueue *cx88q = &dev->mpegq;
  252. dprintk( 1, "cx8802_buf_queue\n" );
  253. /* add jump to stopper */
  254. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
  255. buf->risc.jmp[1] = cpu_to_le32(cx88q->stopper.dma);
  256. if (list_empty(&cx88q->active)) {
  257. dprintk( 1, "queue is empty - first active\n" );
  258. list_add_tail(&buf->vb.queue,&cx88q->active);
  259. cx8802_start_dma(dev, cx88q, buf);
  260. buf->vb.state = VIDEOBUF_ACTIVE;
  261. buf->count = cx88q->count++;
  262. mod_timer(&cx88q->timeout, jiffies+BUFFER_TIMEOUT);
  263. dprintk(1,"[%p/%d] %s - first active\n",
  264. buf, buf->vb.i, __func__);
  265. } else {
  266. dprintk( 1, "queue is not empty - append to active\n" );
  267. prev = list_entry(cx88q->active.prev, struct cx88_buffer, vb.queue);
  268. list_add_tail(&buf->vb.queue,&cx88q->active);
  269. buf->vb.state = VIDEOBUF_ACTIVE;
  270. buf->count = cx88q->count++;
  271. prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  272. dprintk( 1, "[%p/%d] %s - append to active\n",
  273. buf, buf->vb.i, __func__);
  274. }
  275. }
  276. /* ----------------------------------------------------------- */
  277. static void do_cancel_buffers(struct cx8802_dev *dev, const char *reason, int restart)
  278. {
  279. struct cx88_dmaqueue *q = &dev->mpegq;
  280. struct cx88_buffer *buf;
  281. unsigned long flags;
  282. spin_lock_irqsave(&dev->slock,flags);
  283. while (!list_empty(&q->active)) {
  284. buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
  285. list_del(&buf->vb.queue);
  286. buf->vb.state = VIDEOBUF_ERROR;
  287. wake_up(&buf->vb.done);
  288. dprintk(1,"[%p/%d] %s - dma=0x%08lx\n",
  289. buf, buf->vb.i, reason, (unsigned long)buf->risc.dma);
  290. }
  291. if (restart)
  292. {
  293. dprintk(1, "restarting queue\n" );
  294. cx8802_restart_queue(dev,q);
  295. }
  296. spin_unlock_irqrestore(&dev->slock,flags);
  297. }
  298. void cx8802_cancel_buffers(struct cx8802_dev *dev)
  299. {
  300. struct cx88_dmaqueue *q = &dev->mpegq;
  301. dprintk( 1, "cx8802_cancel_buffers" );
  302. del_timer_sync(&q->timeout);
  303. cx8802_stop_dma(dev);
  304. do_cancel_buffers(dev,"cancel",0);
  305. }
  306. static void cx8802_timeout(unsigned long data)
  307. {
  308. struct cx8802_dev *dev = (struct cx8802_dev*)data;
  309. dprintk(1, "%s\n",__func__);
  310. if (debug)
  311. cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH28]);
  312. cx8802_stop_dma(dev);
  313. do_cancel_buffers(dev,"timeout",1);
  314. }
  315. static const char * cx88_mpeg_irqs[32] = {
  316. "ts_risci1", NULL, NULL, NULL,
  317. "ts_risci2", NULL, NULL, NULL,
  318. "ts_oflow", NULL, NULL, NULL,
  319. "ts_sync", NULL, NULL, NULL,
  320. "opc_err", "par_err", "rip_err", "pci_abort",
  321. "ts_err?",
  322. };
  323. static void cx8802_mpeg_irq(struct cx8802_dev *dev)
  324. {
  325. struct cx88_core *core = dev->core;
  326. u32 status, mask, count;
  327. dprintk( 1, "cx8802_mpeg_irq\n" );
  328. status = cx_read(MO_TS_INTSTAT);
  329. mask = cx_read(MO_TS_INTMSK);
  330. if (0 == (status & mask))
  331. return;
  332. cx_write(MO_TS_INTSTAT, status);
  333. if (debug || (status & mask & ~0xff))
  334. cx88_print_irqbits(core->name, "irq mpeg ",
  335. cx88_mpeg_irqs, ARRAY_SIZE(cx88_mpeg_irqs),
  336. status, mask);
  337. /* risc op code error */
  338. if (status & (1 << 16)) {
  339. printk(KERN_WARNING "%s: mpeg risc op code error\n",core->name);
  340. cx_clear(MO_TS_DMACNTRL, 0x11);
  341. cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH28]);
  342. }
  343. /* risc1 y */
  344. if (status & 0x01) {
  345. dprintk( 1, "wake up\n" );
  346. spin_lock(&dev->slock);
  347. count = cx_read(MO_TS_GPCNT);
  348. cx88_wakeup(dev->core, &dev->mpegq, count);
  349. spin_unlock(&dev->slock);
  350. }
  351. /* risc2 y */
  352. if (status & 0x10) {
  353. spin_lock(&dev->slock);
  354. cx8802_restart_queue(dev,&dev->mpegq);
  355. spin_unlock(&dev->slock);
  356. }
  357. /* other general errors */
  358. if (status & 0x1f0100) {
  359. dprintk( 0, "general errors: 0x%08x\n", status & 0x1f0100 );
  360. spin_lock(&dev->slock);
  361. cx8802_stop_dma(dev);
  362. cx8802_restart_queue(dev,&dev->mpegq);
  363. spin_unlock(&dev->slock);
  364. }
  365. }
  366. #define MAX_IRQ_LOOP 10
  367. static irqreturn_t cx8802_irq(int irq, void *dev_id)
  368. {
  369. struct cx8802_dev *dev = dev_id;
  370. struct cx88_core *core = dev->core;
  371. u32 status;
  372. int loop, handled = 0;
  373. for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
  374. status = cx_read(MO_PCI_INTSTAT) &
  375. (core->pci_irqmask | PCI_INT_TSINT);
  376. if (0 == status)
  377. goto out;
  378. dprintk( 1, "cx8802_irq\n" );
  379. dprintk( 1, " loop: %d/%d\n", loop, MAX_IRQ_LOOP );
  380. dprintk( 1, " status: %d\n", status );
  381. handled = 1;
  382. cx_write(MO_PCI_INTSTAT, status);
  383. if (status & core->pci_irqmask)
  384. cx88_core_irq(core,status);
  385. if (status & PCI_INT_TSINT)
  386. cx8802_mpeg_irq(dev);
  387. }
  388. if (MAX_IRQ_LOOP == loop) {
  389. dprintk( 0, "clearing mask\n" );
  390. printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
  391. core->name);
  392. cx_write(MO_PCI_INTMSK,0);
  393. }
  394. out:
  395. return IRQ_RETVAL(handled);
  396. }
  397. static int cx8802_init_common(struct cx8802_dev *dev)
  398. {
  399. struct cx88_core *core = dev->core;
  400. int err;
  401. /* pci init */
  402. if (pci_enable_device(dev->pci))
  403. return -EIO;
  404. pci_set_master(dev->pci);
  405. if (!pci_dma_supported(dev->pci,DMA_BIT_MASK(32))) {
  406. printk("%s/2: Oops: no 32bit PCI DMA ???\n",dev->core->name);
  407. return -EIO;
  408. }
  409. dev->pci_rev = dev->pci->revision;
  410. pci_read_config_byte(dev->pci, PCI_LATENCY_TIMER, &dev->pci_lat);
  411. printk(KERN_INFO "%s/2: found at %s, rev: %d, irq: %d, "
  412. "latency: %d, mmio: 0x%llx\n", dev->core->name,
  413. pci_name(dev->pci), dev->pci_rev, dev->pci->irq,
  414. dev->pci_lat,(unsigned long long)pci_resource_start(dev->pci,0));
  415. /* initialize driver struct */
  416. spin_lock_init(&dev->slock);
  417. /* init dma queue */
  418. INIT_LIST_HEAD(&dev->mpegq.active);
  419. INIT_LIST_HEAD(&dev->mpegq.queued);
  420. dev->mpegq.timeout.function = cx8802_timeout;
  421. dev->mpegq.timeout.data = (unsigned long)dev;
  422. init_timer(&dev->mpegq.timeout);
  423. cx88_risc_stopper(dev->pci,&dev->mpegq.stopper,
  424. MO_TS_DMACNTRL,0x11,0x00);
  425. /* get irq */
  426. err = request_irq(dev->pci->irq, cx8802_irq,
  427. IRQF_SHARED | IRQF_DISABLED, dev->core->name, dev);
  428. if (err < 0) {
  429. printk(KERN_ERR "%s: can't get IRQ %d\n",
  430. dev->core->name, dev->pci->irq);
  431. return err;
  432. }
  433. cx_set(MO_PCI_INTMSK, core->pci_irqmask);
  434. /* everything worked */
  435. pci_set_drvdata(dev->pci,dev);
  436. return 0;
  437. }
  438. static void cx8802_fini_common(struct cx8802_dev *dev)
  439. {
  440. dprintk( 2, "cx8802_fini_common\n" );
  441. cx8802_stop_dma(dev);
  442. pci_disable_device(dev->pci);
  443. /* unregister stuff */
  444. free_irq(dev->pci->irq, dev);
  445. pci_set_drvdata(dev->pci, NULL);
  446. /* free memory */
  447. btcx_riscmem_free(dev->pci,&dev->mpegq.stopper);
  448. }
  449. /* ----------------------------------------------------------- */
  450. static int cx8802_suspend_common(struct pci_dev *pci_dev, pm_message_t state)
  451. {
  452. struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
  453. struct cx88_core *core = dev->core;
  454. /* stop mpeg dma */
  455. spin_lock(&dev->slock);
  456. if (!list_empty(&dev->mpegq.active)) {
  457. dprintk( 2, "suspend\n" );
  458. printk("%s: suspend mpeg\n", core->name);
  459. cx8802_stop_dma(dev);
  460. del_timer(&dev->mpegq.timeout);
  461. }
  462. spin_unlock(&dev->slock);
  463. /* FIXME -- shutdown device */
  464. cx88_shutdown(dev->core);
  465. pci_save_state(pci_dev);
  466. if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
  467. pci_disable_device(pci_dev);
  468. dev->state.disabled = 1;
  469. }
  470. return 0;
  471. }
  472. static int cx8802_resume_common(struct pci_dev *pci_dev)
  473. {
  474. struct cx8802_dev *dev = pci_get_drvdata(pci_dev);
  475. struct cx88_core *core = dev->core;
  476. int err;
  477. if (dev->state.disabled) {
  478. err=pci_enable_device(pci_dev);
  479. if (err) {
  480. printk(KERN_ERR "%s: can't enable device\n",
  481. dev->core->name);
  482. return err;
  483. }
  484. dev->state.disabled = 0;
  485. }
  486. err=pci_set_power_state(pci_dev, PCI_D0);
  487. if (err) {
  488. printk(KERN_ERR "%s: can't enable device\n",
  489. dev->core->name);
  490. pci_disable_device(pci_dev);
  491. dev->state.disabled = 1;
  492. return err;
  493. }
  494. pci_restore_state(pci_dev);
  495. /* FIXME: re-initialize hardware */
  496. cx88_reset(dev->core);
  497. /* restart video+vbi capture */
  498. spin_lock(&dev->slock);
  499. if (!list_empty(&dev->mpegq.active)) {
  500. printk("%s: resume mpeg\n", core->name);
  501. cx8802_restart_queue(dev,&dev->mpegq);
  502. }
  503. spin_unlock(&dev->slock);
  504. return 0;
  505. }
  506. struct cx8802_driver * cx8802_get_driver(struct cx8802_dev *dev, enum cx88_board_type btype)
  507. {
  508. struct cx8802_driver *d;
  509. list_for_each_entry(d, &dev->drvlist, drvlist)
  510. if (d->type_id == btype)
  511. return d;
  512. return NULL;
  513. }
  514. /* Driver asked for hardware access. */
  515. static int cx8802_request_acquire(struct cx8802_driver *drv)
  516. {
  517. struct cx88_core *core = drv->core;
  518. unsigned int i;
  519. /* Fail a request for hardware if the device is busy. */
  520. if (core->active_type_id != CX88_BOARD_NONE &&
  521. core->active_type_id != drv->type_id)
  522. return -EBUSY;
  523. if (drv->type_id == CX88_MPEG_DVB) {
  524. /* When switching to DVB, always set the input to the tuner */
  525. core->last_analog_input = core->input;
  526. core->input = 0;
  527. for (i = 0;
  528. i < (sizeof(core->board.input) / sizeof(struct cx88_input));
  529. i++) {
  530. if (core->board.input[i].type == CX88_VMUX_DVB) {
  531. core->input = i;
  532. break;
  533. }
  534. }
  535. }
  536. if (drv->advise_acquire)
  537. {
  538. core->active_ref++;
  539. if (core->active_type_id == CX88_BOARD_NONE) {
  540. core->active_type_id = drv->type_id;
  541. drv->advise_acquire(drv);
  542. }
  543. mpeg_dbg(1,"%s() Post acquire GPIO=%x\n", __func__, cx_read(MO_GP0_IO));
  544. }
  545. return 0;
  546. }
  547. /* Driver asked to release hardware. */
  548. static int cx8802_request_release(struct cx8802_driver *drv)
  549. {
  550. struct cx88_core *core = drv->core;
  551. if (drv->advise_release && --core->active_ref == 0)
  552. {
  553. if (drv->type_id == CX88_MPEG_DVB) {
  554. /* If the DVB driver is releasing, reset the input
  555. state to the last configured analog input */
  556. core->input = core->last_analog_input;
  557. }
  558. drv->advise_release(drv);
  559. core->active_type_id = CX88_BOARD_NONE;
  560. mpeg_dbg(1,"%s() Post release GPIO=%x\n", __func__, cx_read(MO_GP0_IO));
  561. }
  562. return 0;
  563. }
  564. static int cx8802_check_driver(struct cx8802_driver *drv)
  565. {
  566. if (drv == NULL)
  567. return -ENODEV;
  568. if ((drv->type_id != CX88_MPEG_DVB) &&
  569. (drv->type_id != CX88_MPEG_BLACKBIRD))
  570. return -EINVAL;
  571. if ((drv->hw_access != CX8802_DRVCTL_SHARED) &&
  572. (drv->hw_access != CX8802_DRVCTL_EXCLUSIVE))
  573. return -EINVAL;
  574. if ((drv->probe == NULL) ||
  575. (drv->remove == NULL) ||
  576. (drv->advise_acquire == NULL) ||
  577. (drv->advise_release == NULL))
  578. return -EINVAL;
  579. return 0;
  580. }
  581. int cx8802_register_driver(struct cx8802_driver *drv)
  582. {
  583. struct cx8802_dev *dev;
  584. struct cx8802_driver *driver;
  585. int err, i = 0;
  586. printk(KERN_INFO
  587. "cx88/2: registering cx8802 driver, type: %s access: %s\n",
  588. drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird",
  589. drv->hw_access == CX8802_DRVCTL_SHARED ? "shared" : "exclusive");
  590. if ((err = cx8802_check_driver(drv)) != 0) {
  591. printk(KERN_ERR "cx88/2: cx8802_driver is invalid\n");
  592. return err;
  593. }
  594. mutex_lock(&cx8802_mutex);
  595. list_for_each_entry(dev, &cx8802_devlist, devlist) {
  596. printk(KERN_INFO
  597. "%s/2: subsystem: %04x:%04x, board: %s [card=%d]\n",
  598. dev->core->name, dev->pci->subsystem_vendor,
  599. dev->pci->subsystem_device, dev->core->board.name,
  600. dev->core->boardnr);
  601. /* Bring up a new struct for each driver instance */
  602. driver = kzalloc(sizeof(*drv),GFP_KERNEL);
  603. if (driver == NULL) {
  604. err = -ENOMEM;
  605. goto out;
  606. }
  607. /* Snapshot of the driver registration data */
  608. drv->core = dev->core;
  609. drv->suspend = cx8802_suspend_common;
  610. drv->resume = cx8802_resume_common;
  611. drv->request_acquire = cx8802_request_acquire;
  612. drv->request_release = cx8802_request_release;
  613. memcpy(driver, drv, sizeof(*driver));
  614. mutex_lock(&drv->core->lock);
  615. err = drv->probe(driver);
  616. if (err == 0) {
  617. i++;
  618. list_add_tail(&driver->drvlist, &dev->drvlist);
  619. } else {
  620. printk(KERN_ERR
  621. "%s/2: cx8802 probe failed, err = %d\n",
  622. dev->core->name, err);
  623. }
  624. mutex_unlock(&drv->core->lock);
  625. }
  626. err = i ? 0 : -ENODEV;
  627. out:
  628. mutex_unlock(&cx8802_mutex);
  629. return err;
  630. }
  631. int cx8802_unregister_driver(struct cx8802_driver *drv)
  632. {
  633. struct cx8802_dev *dev;
  634. struct cx8802_driver *d, *dtmp;
  635. int err = 0;
  636. printk(KERN_INFO
  637. "cx88/2: unregistering cx8802 driver, type: %s access: %s\n",
  638. drv->type_id == CX88_MPEG_DVB ? "dvb" : "blackbird",
  639. drv->hw_access == CX8802_DRVCTL_SHARED ? "shared" : "exclusive");
  640. mutex_lock(&cx8802_mutex);
  641. list_for_each_entry(dev, &cx8802_devlist, devlist) {
  642. printk(KERN_INFO
  643. "%s/2: subsystem: %04x:%04x, board: %s [card=%d]\n",
  644. dev->core->name, dev->pci->subsystem_vendor,
  645. dev->pci->subsystem_device, dev->core->board.name,
  646. dev->core->boardnr);
  647. mutex_lock(&dev->core->lock);
  648. list_for_each_entry_safe(d, dtmp, &dev->drvlist, drvlist) {
  649. /* only unregister the correct driver type */
  650. if (d->type_id != drv->type_id)
  651. continue;
  652. err = d->remove(d);
  653. if (err == 0) {
  654. list_del(&d->drvlist);
  655. kfree(d);
  656. } else
  657. printk(KERN_ERR "%s/2: cx8802 driver remove "
  658. "failed (%d)\n", dev->core->name, err);
  659. }
  660. mutex_unlock(&dev->core->lock);
  661. }
  662. mutex_unlock(&cx8802_mutex);
  663. return err;
  664. }
  665. /* ----------------------------------------------------------- */
  666. static int cx8802_probe(struct pci_dev *pci_dev,
  667. const struct pci_device_id *pci_id)
  668. {
  669. struct cx8802_dev *dev;
  670. struct cx88_core *core;
  671. int err;
  672. /* general setup */
  673. core = cx88_core_get(pci_dev);
  674. if (NULL == core)
  675. return -EINVAL;
  676. printk("%s/2: cx2388x 8802 Driver Manager\n", core->name);
  677. err = -ENODEV;
  678. if (!core->board.mpeg)
  679. goto fail_core;
  680. err = -ENOMEM;
  681. dev = kzalloc(sizeof(*dev),GFP_KERNEL);
  682. if (NULL == dev)
  683. goto fail_core;
  684. dev->pci = pci_dev;
  685. dev->core = core;
  686. /* Maintain a reference so cx88-video can query the 8802 device. */
  687. core->dvbdev = dev;
  688. err = cx8802_init_common(dev);
  689. if (err != 0)
  690. goto fail_free;
  691. INIT_LIST_HEAD(&dev->drvlist);
  692. mutex_lock(&cx8802_mutex);
  693. list_add_tail(&dev->devlist,&cx8802_devlist);
  694. mutex_unlock(&cx8802_mutex);
  695. /* now autoload cx88-dvb or cx88-blackbird */
  696. request_modules(dev);
  697. return 0;
  698. fail_free:
  699. kfree(dev);
  700. fail_core:
  701. core->dvbdev = NULL;
  702. cx88_core_put(core,pci_dev);
  703. return err;
  704. }
  705. static void cx8802_remove(struct pci_dev *pci_dev)
  706. {
  707. struct cx8802_dev *dev;
  708. dev = pci_get_drvdata(pci_dev);
  709. dprintk( 1, "%s\n", __func__);
  710. flush_request_modules(dev);
  711. mutex_lock(&dev->core->lock);
  712. if (!list_empty(&dev->drvlist)) {
  713. struct cx8802_driver *drv, *tmp;
  714. int err;
  715. printk(KERN_WARNING "%s/2: Trying to remove cx8802 driver "
  716. "while cx8802 sub-drivers still loaded?!\n",
  717. dev->core->name);
  718. list_for_each_entry_safe(drv, tmp, &dev->drvlist, drvlist) {
  719. err = drv->remove(drv);
  720. if (err == 0) {
  721. list_del(&drv->drvlist);
  722. } else
  723. printk(KERN_ERR "%s/2: cx8802 driver remove "
  724. "failed (%d)\n", dev->core->name, err);
  725. kfree(drv);
  726. }
  727. }
  728. mutex_unlock(&dev->core->lock);
  729. /* Destroy any 8802 reference. */
  730. dev->core->dvbdev = NULL;
  731. /* common */
  732. cx8802_fini_common(dev);
  733. cx88_core_put(dev->core,dev->pci);
  734. kfree(dev);
  735. }
  736. static const struct pci_device_id cx8802_pci_tbl[] = {
  737. {
  738. .vendor = 0x14f1,
  739. .device = 0x8802,
  740. .subvendor = PCI_ANY_ID,
  741. .subdevice = PCI_ANY_ID,
  742. },{
  743. /* --- end of list --- */
  744. }
  745. };
  746. MODULE_DEVICE_TABLE(pci, cx8802_pci_tbl);
  747. static struct pci_driver cx8802_pci_driver = {
  748. .name = "cx88-mpeg driver manager",
  749. .id_table = cx8802_pci_tbl,
  750. .probe = cx8802_probe,
  751. .remove = cx8802_remove,
  752. };
  753. static int __init cx8802_init(void)
  754. {
  755. printk(KERN_INFO "cx88/2: cx2388x MPEG-TS Driver Manager version %s loaded\n",
  756. CX88_VERSION);
  757. return pci_register_driver(&cx8802_pci_driver);
  758. }
  759. static void __exit cx8802_fini(void)
  760. {
  761. pci_unregister_driver(&cx8802_pci_driver);
  762. }
  763. module_init(cx8802_init);
  764. module_exit(cx8802_fini);
  765. EXPORT_SYMBOL(cx8802_buf_prepare);
  766. EXPORT_SYMBOL(cx8802_buf_queue);
  767. EXPORT_SYMBOL(cx8802_cancel_buffers);
  768. EXPORT_SYMBOL(cx8802_register_driver);
  769. EXPORT_SYMBOL(cx8802_unregister_driver);
  770. EXPORT_SYMBOL(cx8802_get_driver);
  771. /* ----------------------------------------------------------- */
  772. /*
  773. * Local variables:
  774. * c-basic-offset: 8
  775. * End:
  776. * 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
  777. */