cx88-mpeg.c 24 KB

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