cx88-core.c

来自「linux 内核源代码」· C语言 代码 · 共 1,096 行 · 第 1/2 页

C
1,096
字号
/* * * device driver for Conexant 2388x based TV cards * driver core * * (c) 2003 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs] * * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org> *     - Multituner support *     - video_ioctl2 conversion *     - PAL/M fixes * *  This program is free software; you can redistribute it and/or modify *  it under the terms of the GNU General Public License as published by *  the Free Software Foundation; either version 2 of the License, or *  (at your option) any later version. * *  This program is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *  GNU General Public License for more details. * *  You should have received a copy of the GNU General Public License *  along with this program; if not, write to the Free Software *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include <linux/init.h>#include <linux/list.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/slab.h>#include <linux/kmod.h>#include <linux/sound.h>#include <linux/interrupt.h>#include <linux/pci.h>#include <linux/delay.h>#include <linux/videodev2.h>#include <linux/mutex.h>#include "cx88.h"#include <media/v4l2-common.h>MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");MODULE_LICENSE("GPL");/* ------------------------------------------------------------------ */static unsigned int core_debug = 0;module_param(core_debug,int,0644);MODULE_PARM_DESC(core_debug,"enable debug messages [core]");static unsigned int nicam = 0;module_param(nicam,int,0644);MODULE_PARM_DESC(nicam,"tv audio is nicam");static unsigned int nocomb = 0;module_param(nocomb,int,0644);MODULE_PARM_DESC(nocomb,"disable comb filter");#define dprintk(level,fmt, arg...)	if (core_debug >= level)	\	printk(KERN_DEBUG "%s: " fmt, core->name , ## arg)static unsigned int cx88_devcount;static LIST_HEAD(cx88_devlist);static DEFINE_MUTEX(devlist);#define NO_SYNC_LINE (-1U)/* @lpi: lines per IRQ, or 0 to not generate irqs. Note: IRQ to be	 generated _after_ lpi lines are transferred. */static u32* cx88_risc_field(u32 *rp, struct scatterlist *sglist,			    unsigned int offset, u32 sync_line,			    unsigned int bpl, unsigned int padding,			    unsigned int lines, unsigned int lpi){	struct scatterlist *sg;	unsigned int line,todo,sol;	/* sync instruction */	if (sync_line != NO_SYNC_LINE)		*(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);	/* scan lines */	sg = sglist;	for (line = 0; line < lines; line++) {		while (offset && offset >= sg_dma_len(sg)) {			offset -= sg_dma_len(sg);			sg++;		}		if (lpi && line>0 && !(line % lpi))			sol = RISC_SOL | RISC_IRQ1 | RISC_CNT_INC;		else			sol = RISC_SOL;		if (bpl <= sg_dma_len(sg)-offset) {			/* fits into current chunk */			*(rp++)=cpu_to_le32(RISC_WRITE|sol|RISC_EOL|bpl);			*(rp++)=cpu_to_le32(sg_dma_address(sg)+offset);			offset+=bpl;		} else {			/* scanline needs to be split */			todo = bpl;			*(rp++)=cpu_to_le32(RISC_WRITE|sol|					    (sg_dma_len(sg)-offset));			*(rp++)=cpu_to_le32(sg_dma_address(sg)+offset);			todo -= (sg_dma_len(sg)-offset);			offset = 0;			sg++;			while (todo > sg_dma_len(sg)) {				*(rp++)=cpu_to_le32(RISC_WRITE|						    sg_dma_len(sg));				*(rp++)=cpu_to_le32(sg_dma_address(sg));				todo -= sg_dma_len(sg);				sg++;			}			*(rp++)=cpu_to_le32(RISC_WRITE|RISC_EOL|todo);			*(rp++)=cpu_to_le32(sg_dma_address(sg));			offset += todo;		}		offset += padding;	}	return rp;}int cx88_risc_buffer(struct pci_dev *pci, struct btcx_riscmem *risc,		     struct scatterlist *sglist,		     unsigned int top_offset, unsigned int bottom_offset,		     unsigned int bpl, unsigned int padding, unsigned int lines){	u32 instructions,fields;	u32 *rp;	int rc;	fields = 0;	if (UNSET != top_offset)		fields++;	if (UNSET != bottom_offset)		fields++;	/* estimate risc mem: worst case is one write per page border +	   one write per scan line + syncs + jump (all 2 dwords).  Padding	   can cause next bpl to start close to a page border.  First DMA	   region may be smaller than PAGE_SIZE */	instructions  = fields * (1 + ((bpl + padding) * lines) / PAGE_SIZE + lines);	instructions += 2;	if ((rc = btcx_riscmem_alloc(pci,risc,instructions*8)) < 0)		return rc;	/* write risc instructions */	rp = risc->cpu;	if (UNSET != top_offset)		rp = cx88_risc_field(rp, sglist, top_offset, 0,				     bpl, padding, lines, 0);	if (UNSET != bottom_offset)		rp = cx88_risc_field(rp, sglist, bottom_offset, 0x200,				     bpl, padding, lines, 0);	/* save pointer to jmp instruction address */	risc->jmp = rp;	BUG_ON((risc->jmp - risc->cpu + 2) * sizeof (*risc->cpu) > risc->size);	return 0;}int cx88_risc_databuffer(struct pci_dev *pci, struct btcx_riscmem *risc,			 struct scatterlist *sglist, unsigned int bpl,			 unsigned int lines, unsigned int lpi){	u32 instructions;	u32 *rp;	int rc;	/* estimate risc mem: worst case is one write per page border +	   one write per scan line + syncs + jump (all 2 dwords).  Here	   there is no padding and no sync.  First DMA region may be smaller	   than PAGE_SIZE */	instructions  = 1 + (bpl * lines) / PAGE_SIZE + lines;	instructions += 1;	if ((rc = btcx_riscmem_alloc(pci,risc,instructions*8)) < 0)		return rc;	/* write risc instructions */	rp = risc->cpu;	rp = cx88_risc_field(rp, sglist, 0, NO_SYNC_LINE, bpl, 0, lines, lpi);	/* save pointer to jmp instruction address */	risc->jmp = rp;	BUG_ON((risc->jmp - risc->cpu + 2) * sizeof (*risc->cpu) > risc->size);	return 0;}int cx88_risc_stopper(struct pci_dev *pci, struct btcx_riscmem *risc,		      u32 reg, u32 mask, u32 value){	u32 *rp;	int rc;	if ((rc = btcx_riscmem_alloc(pci, risc, 4*16)) < 0)		return rc;	/* write risc instructions */	rp = risc->cpu;	*(rp++) = cpu_to_le32(RISC_WRITECR  | RISC_IRQ2 | RISC_IMM);	*(rp++) = cpu_to_le32(reg);	*(rp++) = cpu_to_le32(value);	*(rp++) = cpu_to_le32(mask);	*(rp++) = cpu_to_le32(RISC_JUMP);	*(rp++) = cpu_to_le32(risc->dma);	return 0;}voidcx88_free_buffer(struct videobuf_queue *q, struct cx88_buffer *buf){	struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);	BUG_ON(in_interrupt());	videobuf_waiton(&buf->vb,0,0);	videobuf_dma_unmap(q, dma);	videobuf_dma_free(dma);	btcx_riscmem_free((struct pci_dev *)q->dev, &buf->risc);	buf->vb.state = STATE_NEEDS_INIT;}/* ------------------------------------------------------------------ *//* our SRAM memory layout                                             *//* we are going to put all thr risc programs into host memory, so we * can use the whole SDRAM for the DMA fifos.  To simplify things, we * use a static memory layout.  That surely will waste memory in case * we don't use all DMA channels at the same time (which will be the * case most of the time).  But that still gives us enougth FIFO space * to be able to deal with insane long pci latencies ... * * FIFO space allocations: *    channel  21    (y video)  - 10.0k *    channel  22    (u video)  -  2.0k *    channel  23    (v video)  -  2.0k *    channel  24    (vbi)      -  4.0k *    channels 25+26 (audio)    -  4.0k *    channel  28    (mpeg)     -  4.0k *    TOTAL                     = 29.0k * * Every channel has 160 bytes control data (64 bytes instruction * queue and 6 CDT entries), which is close to 2k total. * * Address layout: *    0x0000 - 0x03ff    CMDs / reserved *    0x0400 - 0x0bff    instruction queues + CDs *    0x0c00 -           FIFOs */struct sram_channel cx88_sram_channels[] = {	[SRAM_CH21] = {		.name       = "video y / packed",		.cmds_start = 0x180040,		.ctrl_start = 0x180400,		.cdt        = 0x180400 + 64,		.fifo_start = 0x180c00,		.fifo_size  = 0x002800,		.ptr1_reg   = MO_DMA21_PTR1,		.ptr2_reg   = MO_DMA21_PTR2,		.cnt1_reg   = MO_DMA21_CNT1,		.cnt2_reg   = MO_DMA21_CNT2,	},	[SRAM_CH22] = {		.name       = "video u",		.cmds_start = 0x180080,		.ctrl_start = 0x1804a0,		.cdt        = 0x1804a0 + 64,		.fifo_start = 0x183400,		.fifo_size  = 0x000800,		.ptr1_reg   = MO_DMA22_PTR1,		.ptr2_reg   = MO_DMA22_PTR2,		.cnt1_reg   = MO_DMA22_CNT1,		.cnt2_reg   = MO_DMA22_CNT2,	},	[SRAM_CH23] = {		.name       = "video v",		.cmds_start = 0x1800c0,		.ctrl_start = 0x180540,		.cdt        = 0x180540 + 64,		.fifo_start = 0x183c00,		.fifo_size  = 0x000800,		.ptr1_reg   = MO_DMA23_PTR1,		.ptr2_reg   = MO_DMA23_PTR2,		.cnt1_reg   = MO_DMA23_CNT1,		.cnt2_reg   = MO_DMA23_CNT2,	},	[SRAM_CH24] = {		.name       = "vbi",		.cmds_start = 0x180100,		.ctrl_start = 0x1805e0,		.cdt        = 0x1805e0 + 64,		.fifo_start = 0x184400,		.fifo_size  = 0x001000,		.ptr1_reg   = MO_DMA24_PTR1,		.ptr2_reg   = MO_DMA24_PTR2,		.cnt1_reg   = MO_DMA24_CNT1,		.cnt2_reg   = MO_DMA24_CNT2,	},	[SRAM_CH25] = {		.name       = "audio from",		.cmds_start = 0x180140,		.ctrl_start = 0x180680,		.cdt        = 0x180680 + 64,		.fifo_start = 0x185400,		.fifo_size  = 0x001000,		.ptr1_reg   = MO_DMA25_PTR1,		.ptr2_reg   = MO_DMA25_PTR2,		.cnt1_reg   = MO_DMA25_CNT1,		.cnt2_reg   = MO_DMA25_CNT2,	},	[SRAM_CH26] = {		.name       = "audio to",		.cmds_start = 0x180180,		.ctrl_start = 0x180720,		.cdt        = 0x180680 + 64,  /* same as audio IN */		.fifo_start = 0x185400,       /* same as audio IN */		.fifo_size  = 0x001000,       /* same as audio IN */		.ptr1_reg   = MO_DMA26_PTR1,		.ptr2_reg   = MO_DMA26_PTR2,		.cnt1_reg   = MO_DMA26_CNT1,		.cnt2_reg   = MO_DMA26_CNT2,	},	[SRAM_CH28] = {		.name       = "mpeg",		.cmds_start = 0x180200,		.ctrl_start = 0x1807C0,		.cdt        = 0x1807C0 + 64,		.fifo_start = 0x186400,		.fifo_size  = 0x001000,		.ptr1_reg   = MO_DMA28_PTR1,		.ptr2_reg   = MO_DMA28_PTR2,		.cnt1_reg   = MO_DMA28_CNT1,		.cnt2_reg   = MO_DMA28_CNT2,	},};int cx88_sram_channel_setup(struct cx88_core *core,			    struct sram_channel *ch,			    unsigned int bpl, u32 risc){	unsigned int i,lines;	u32 cdt;	bpl   = (bpl + 7) & ~7; /* alignment */	cdt   = ch->cdt;	lines = ch->fifo_size / bpl;	if (lines > 6)		lines = 6;	BUG_ON(lines < 2);	/* write CDT */	for (i = 0; i < lines; i++)		cx_write(cdt + 16*i, ch->fifo_start + bpl*i);	/* write CMDS */	cx_write(ch->cmds_start +  0, risc);	cx_write(ch->cmds_start +  4, cdt);	cx_write(ch->cmds_start +  8, (lines*16) >> 3);	cx_write(ch->cmds_start + 12, ch->ctrl_start);	cx_write(ch->cmds_start + 16, 64 >> 2);	for (i = 20; i < 64; i += 4)		cx_write(ch->cmds_start + i, 0);	/* fill registers */	cx_write(ch->ptr1_reg, ch->fifo_start);	cx_write(ch->ptr2_reg, cdt);	cx_write(ch->cnt1_reg, (bpl >> 3) -1);	cx_write(ch->cnt2_reg, (lines*16) >> 3);	dprintk(2,"sram setup %s: bpl=%d lines=%d\n", ch->name, bpl, lines);	return 0;}/* ------------------------------------------------------------------ *//* debug helper code                                                  */static int cx88_risc_decode(u32 risc){	static char *instr[16] = {		[ RISC_SYNC    >> 28 ] = "sync",		[ RISC_WRITE   >> 28 ] = "write",		[ RISC_WRITEC  >> 28 ] = "writec",		[ RISC_READ    >> 28 ] = "read",		[ RISC_READC   >> 28 ] = "readc",		[ RISC_JUMP    >> 28 ] = "jump",		[ RISC_SKIP    >> 28 ] = "skip",		[ RISC_WRITERM >> 28 ] = "writerm",		[ RISC_WRITECM >> 28 ] = "writecm",		[ RISC_WRITECR >> 28 ] = "writecr",	};	static int incr[16] = {		[ RISC_WRITE   >> 28 ] = 2,		[ RISC_JUMP    >> 28 ] = 2,		[ RISC_WRITERM >> 28 ] = 3,		[ RISC_WRITECM >> 28 ] = 3,		[ RISC_WRITECR >> 28 ] = 4,	};	static char *bits[] = {		"12",   "13",   "14",   "resync",		"cnt0", "cnt1", "18",   "19",		"20",   "21",   "22",   "23",		"irq1", "irq2", "eol",  "sol",	};	int i;	printk("0x%08x [ %s", risc,	       instr[risc >> 28] ? instr[risc >> 28] : "INVALID");	for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)		if (risc & (1 << (i + 12)))			printk(" %s",bits[i]);	printk(" count=%d ]\n", risc & 0xfff);	return incr[risc >> 28] ? incr[risc >> 28] : 1;}void cx88_sram_channel_dump(struct cx88_core *core,			    struct sram_channel *ch){	static char *name[] = {		"initial risc",		"cdt base",		"cdt size",		"iq base",		"iq size",		"risc pc",		"iq wr ptr",		"iq rd ptr",		"cdt current",		"pci target",		"line / byte",	};	u32 risc;	unsigned int i,j,n;	printk("%s: %s - dma channel status dump\n",	       core->name,ch->name);	for (i = 0; i < ARRAY_SIZE(name); i++)		printk("%s:   cmds: %-12s: 0x%08x\n",		       core->name,name[i],		       cx_read(ch->cmds_start + 4*i));	for (n = 1, i = 0; i < 4; i++) {		risc = cx_read(ch->cmds_start + 4 * (i+11));		printk("%s:   risc%d: ", core->name, i);		if (--n)			printk("0x%08x [ arg #%d ]\n", risc, n);		else			n = cx88_risc_decode(risc);	}	for (i = 0; i < 16; i += n) {		risc = cx_read(ch->ctrl_start + 4 * i);		printk("%s:   iq %x: ", core->name, i);		n = cx88_risc_decode(risc);		for (j = 1; j < n; j++) {			risc = cx_read(ch->ctrl_start + 4 * (i+j));			printk("%s:   iq %x: 0x%08x [ arg #%d ]\n",			       core->name, i+j, risc, j);		}	}	printk("%s: fifo: 0x%08x -> 0x%x\n",	       core->name, ch->fifo_start, ch->fifo_start+ch->fifo_size);	printk("%s: ctrl: 0x%08x -> 0x%x\n",	       core->name, ch->ctrl_start, ch->ctrl_start+6*16);	printk("%s:   ptr1_reg: 0x%08x\n",	       core->name,cx_read(ch->ptr1_reg));	printk("%s:   ptr2_reg: 0x%08x\n",	       core->name,cx_read(ch->ptr2_reg));	printk("%s:   cnt1_reg: 0x%08x\n",	       core->name,cx_read(ch->cnt1_reg));	printk("%s:   cnt2_reg: 0x%08x\n",	       core->name,cx_read(ch->cnt2_reg));}static char *cx88_pci_irqs[32] = {	"vid", "aud", "ts", "vip", "hst", "5", "6", "tm1",	"src_dma", "dst_dma", "risc_rd_err", "risc_wr_err",	"brdg_err", "src_dma_err", "dst_dma_err", "ipb_dma_err",	"i2c", "i2c_rack", "ir_smp", "gpio0", "gpio1"};void cx88_print_irqbits(char *name, char *tag, char **strings,			int len, u32 bits, u32 mask){	unsigned int i;	printk(KERN_DEBUG "%s: %s [0x%x]", name, tag, bits);	for (i = 0; i < len; i++) {		if (!(bits & (1 << i)))			continue;		if (strings[i])			printk(" %s", strings[i]);		else			printk(" %d", i);		if (!(mask & (1 << i)))			continue;		printk("*");	}	printk("\n");}/* ------------------------------------------------------------------ */int cx88_core_irq(struct cx88_core *core, u32 status){	int handled = 0;	if (status & PCI_INT_IR_SMPINT) {		cx88_ir_irq(core);		handled++;	}	if (!handled)		cx88_print_irqbits(core->name, "irq pci",				   cx88_pci_irqs, ARRAY_SIZE(cx88_pci_irqs),				   status, core->pci_irqmask);	return handled;}void cx88_wakeup(struct cx88_core *core,		 struct cx88_dmaqueue *q, u32 count){	struct cx88_buffer *buf;	int bc;	for (bc = 0;; bc++) {		if (list_empty(&q->active))			break;		buf = list_entry(q->active.next,				 struct cx88_buffer, vb.queue);		/* count comes from the hw and is is 16bit wide --		 * this trick handles wrap-arounds correctly for		 * up to 32767 buffers in flight... */		if ((s16) (count - buf->count) < 0)			break;		do_gettimeofday(&buf->vb.ts);		dprintk(2,"[%p/%d] wakeup reg=%d buf=%d\n",buf,buf->vb.i,			count, buf->count);		buf->vb.state = STATE_DONE;		list_del(&buf->vb.queue);		wake_up(&buf->vb.done);	}	if (list_empty(&q->active)) {		del_timer(&q->timeout);	} else {		mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?