cx88-mpeg.c 24 KB

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