cx88-mpeg.c 24 KB

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