2 * device driver for Conexant 2388x based TV cards
3 * video4linux video interface
5 * (c) 2003 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define __NO_VERSION__ 1
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/kmod.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/interrupt.h>
31 #include <asm/div64.h>
35 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
36 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
37 MODULE_LICENSE("GPL");
39 /* ------------------------------------------------------------------ */
41 static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
42 MODULE_PARM(video_nr,"1-" __stringify(CX88_MAXBOARDS) "i");
43 MODULE_PARM_DESC(video_nr,"video device numbers");
45 static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
46 MODULE_PARM(vbi_nr,"1-" __stringify(CX88_MAXBOARDS) "i");
47 MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
49 static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
50 MODULE_PARM(radio_nr,"1-" __stringify(CX88_MAXBOARDS) "i");
51 MODULE_PARM_DESC(radio_nr,"radio device numbers");
53 static unsigned int latency = UNSET;
54 MODULE_PARM(latency,"i");
55 MODULE_PARM_DESC(latency,"pci latency timer");
57 static unsigned int video_debug = 0;
58 MODULE_PARM(video_debug,"i");
59 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
61 static unsigned int irq_debug = 0;
62 MODULE_PARM(irq_debug,"i");
63 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
65 static unsigned int vid_limit = 16;
66 MODULE_PARM(vid_limit,"i");
67 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
69 static unsigned int tuner[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
70 MODULE_PARM(tuner,"1-" __stringify(CX88_MAXBOARDS) "i");
71 MODULE_PARM_DESC(tuner,"tuner type");
73 static unsigned int card[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
74 MODULE_PARM(card,"1-" __stringify(CX88_MAXBOARDS) "i");
75 MODULE_PARM_DESC(card,"card type");
77 static unsigned int nicam = 0;
78 MODULE_PARM(nicam,"i");
79 MODULE_PARM_DESC(nicam,"tv audio is nicam");
81 #define dprintk(level,fmt, arg...) if (video_debug >= level) \
82 printk(KERN_DEBUG "%s: " fmt, dev->name , ## arg)
84 /* ------------------------------------------------------------------ */
86 static struct list_head cx8800_devlist;
87 static unsigned int cx8800_devcount;
89 /* ------------------------------------------------------------------- */
92 static unsigned int inline norm_swidth(struct cx8800_tvnorm *norm)
94 return (norm->id & V4L2_STD_625_50) ? 922 : 754;
97 static unsigned int inline norm_hdelay(struct cx8800_tvnorm *norm)
99 return (norm->id & V4L2_STD_625_50) ? 186 : 135;
102 static unsigned int inline norm_vdelay(struct cx8800_tvnorm *norm)
104 return (norm->id & V4L2_STD_625_50) ? 0x24 : 0x18;
107 static unsigned int inline norm_maxw(struct cx8800_tvnorm *norm)
109 return (norm->id & V4L2_STD_625_50) ? 768 : 640;
110 // return (norm->id & V4L2_STD_625_50) ? 720 : 640;
113 static unsigned int inline norm_maxh(struct cx8800_tvnorm *norm)
115 return (norm->id & V4L2_STD_625_50) ? 576 : 480;
118 static unsigned int inline norm_fsc8(struct cx8800_tvnorm *norm)
120 static const unsigned int ntsc = 28636360;
121 static const unsigned int pal = 35468950;
123 return (norm->id & V4L2_STD_625_50) ? pal : ntsc;
126 static unsigned int inline norm_notchfilter(struct cx8800_tvnorm *norm)
128 return (norm->id & V4L2_STD_625_50)
129 ? HLNotchFilter135PAL
130 : HLNotchFilter135NTSC;
133 static unsigned int inline norm_htotal(struct cx8800_tvnorm *norm)
135 return (norm->id & V4L2_STD_625_50) ? 1135 : 910;
138 static unsigned int inline norm_vbipack(struct cx8800_tvnorm *norm)
140 return (norm->id & V4L2_STD_625_50) ? 511 : 288;
143 static struct cx8800_tvnorm tvnorms[] = {
146 .id = V4L2_STD_NTSC_M,
147 .cxiformat = VideoFormatNTSC,
148 .cxoformat = 0x181f0008,
151 .id = V4L2_STD_NTSC_M_JP,
152 .cxiformat = VideoFormatNTSCJapan,
153 .cxoformat = 0x181f0008,
158 .cxiformat = VideoFormatNTSC443,
159 .cxoformat = 0x181f0008,
163 .id = V4L2_STD_PAL_BG,
164 .cxiformat = VideoFormatPAL,
165 .cxoformat = 0x181f0008,
168 .id = V4L2_STD_PAL_DK,
169 .cxiformat = VideoFormatPAL,
170 .cxoformat = 0x181f0008,
173 .id = V4L2_STD_PAL_I,
174 .cxiformat = VideoFormatPAL,
175 .cxoformat = 0x181f0008,
178 .id = V4L2_STD_PAL_M,
179 .cxiformat = VideoFormatPALM,
180 .cxoformat = 0x1c1f0008,
183 .id = V4L2_STD_PAL_N,
184 .cxiformat = VideoFormatPALN,
185 .cxoformat = 0x1c1f0008,
188 .id = V4L2_STD_PAL_Nc,
189 .cxiformat = VideoFormatPALNC,
190 .cxoformat = 0x1c1f0008,
193 .id = V4L2_STD_PAL_60,
194 .cxiformat = VideoFormatPAL60,
195 .cxoformat = 0x181f0008,
198 .id = V4L2_STD_SECAM,
199 .cxiformat = VideoFormatSECAM,
200 .cxoformat = 0x181f0008,
204 static struct cx8800_fmt formats[] = {
206 .name = "8 bpp, gray",
207 .fourcc = V4L2_PIX_FMT_GREY,
208 .cxformat = ColorFormatY8,
210 .flags = FORMAT_FLAGS_PACKED,
212 .name = "15 bpp RGB, le",
213 .fourcc = V4L2_PIX_FMT_RGB555,
214 .cxformat = ColorFormatRGB15,
216 .flags = FORMAT_FLAGS_PACKED,
218 .name = "15 bpp RGB, be",
219 .fourcc = V4L2_PIX_FMT_RGB555X,
220 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
222 .flags = FORMAT_FLAGS_PACKED,
224 .name = "16 bpp RGB, le",
225 .fourcc = V4L2_PIX_FMT_RGB565,
226 .cxformat = ColorFormatRGB16,
228 .flags = FORMAT_FLAGS_PACKED,
230 .name = "16 bpp RGB, be",
231 .fourcc = V4L2_PIX_FMT_RGB565X,
232 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
234 .flags = FORMAT_FLAGS_PACKED,
236 .name = "24 bpp RGB, le",
237 .fourcc = V4L2_PIX_FMT_BGR24,
238 .cxformat = ColorFormatRGB24,
240 .flags = FORMAT_FLAGS_PACKED,
242 .name = "32 bpp RGB, le",
243 .fourcc = V4L2_PIX_FMT_BGR32,
244 .cxformat = ColorFormatRGB32,
246 .flags = FORMAT_FLAGS_PACKED,
248 .name = "32 bpp RGB, be",
249 .fourcc = V4L2_PIX_FMT_RGB32,
250 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
252 .flags = FORMAT_FLAGS_PACKED,
254 .name = "4:2:2, packed, YUYV",
255 .fourcc = V4L2_PIX_FMT_YUYV,
256 .cxformat = ColorFormatYUY2,
258 .flags = FORMAT_FLAGS_PACKED,
260 .name = "4:2:2, packed, UYVY",
261 .fourcc = V4L2_PIX_FMT_UYVY,
262 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
264 .flags = FORMAT_FLAGS_PACKED,
268 static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
272 for (i = 0; i < ARRAY_SIZE(formats); i++)
273 if (formats[i].fourcc == fourcc)
278 /* ------------------------------------------------------------------- */
280 static const struct v4l2_queryctrl no_ctl = {
282 .flags = V4L2_CTRL_FLAG_DISABLED,
285 static struct cx88_ctrl cx8800_ctls[] = {
289 .id = V4L2_CID_BRIGHTNESS,
290 .name = "Brightness",
295 .type = V4L2_CTRL_TYPE_INTEGER,
298 .reg = MO_CONTR_BRIGHT,
303 .id = V4L2_CID_CONTRAST,
309 .type = V4L2_CTRL_TYPE_INTEGER,
311 .reg = MO_CONTR_BRIGHT,
322 .type = V4L2_CTRL_TYPE_INTEGER,
329 /* strictly, this only describes only U saturation.
330 * V saturation is handled specially through code.
333 .id = V4L2_CID_SATURATION,
334 .name = "Saturation",
339 .type = V4L2_CTRL_TYPE_INTEGER,
342 .reg = MO_UV_SATURATION,
348 .id = V4L2_CID_AUDIO_MUTE,
352 .type = V4L2_CTRL_TYPE_BOOLEAN,
355 .sreg = SHADOW_AUD_VOL_CTL,
360 .id = V4L2_CID_AUDIO_VOLUME,
366 .type = V4L2_CTRL_TYPE_INTEGER,
369 .sreg = SHADOW_AUD_VOL_CTL,
374 .id = V4L2_CID_AUDIO_BALANCE,
379 .default_value = 0x40,
380 .type = V4L2_CTRL_TYPE_INTEGER,
383 .sreg = SHADOW_AUD_BAL_CTL,
388 const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
390 /* ------------------------------------------------------------------- */
391 /* resource management */
393 static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
395 if (fh->resources & bit)
396 /* have it already allocated */
401 if (dev->resources & bit) {
402 /* no, someone else uses it */
406 /* it's free, grab it */
407 fh->resources |= bit;
408 dev->resources |= bit;
409 dprintk(1,"res: get %d\n",bit);
415 int res_check(struct cx8800_fh *fh, unsigned int bit)
417 return (fh->resources & bit);
421 int res_locked(struct cx8800_dev *dev, unsigned int bit)
423 return (dev->resources & bit);
427 void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
429 if ((fh->resources & bits) != bits)
433 fh->resources &= ~bits;
434 dev->resources &= ~bits;
435 dprintk(1,"res: put %d\n",bits);
439 /* ------------------------------------------------------------------ */
441 static const u32 xtal = 28636363;
443 static int set_pll(struct cx8800_dev *dev, int prescale, u32 ofreq)
445 static u32 pre[] = { 0, 0, 0, 3, 2, 1 };
455 pll = ofreq * 8 * prescale * (u64)(1 << 20);
457 reg = (pll & 0x3ffffff) | (pre[prescale] << 26);
458 if (((reg >> 20) & 0x3f) < 14) {
459 printk("%s: pll out of range\n",dev->name);
463 dprintk(1,"set_pll: MO_PLL_REG 0x%08x [old=0x%08x,freq=%d]\n",
464 reg, cx_read(MO_PLL_REG), ofreq);
465 cx_write(MO_PLL_REG, reg);
466 for (i = 0; i < 10; i++) {
467 reg = cx_read(MO_DEVICE_STATUS);
469 dprintk(1,"pll locked [pre=%d,ofreq=%d]\n",
473 dprintk(1,"pll not locked yet, waiting ...\n");
474 set_current_state(TASK_INTERRUPTIBLE);
475 schedule_timeout(HZ/10);
477 dprintk(1,"pll NOT locked [pre=%d,ofreq=%d]\n",prescale,ofreq);
481 static int set_tvaudio(struct cx8800_dev *dev)
483 if (CX88_VMUX_TELEVISION != INPUT(dev->input)->type)
486 switch (dev->tvnorm->id) {
487 case V4L2_STD_PAL_BG:
488 dev->tvaudio = nicam ? WW_NICAM_BGDKL : WW_A2_BG;
490 case V4L2_STD_PAL_DK:
491 dev->tvaudio = nicam ? WW_NICAM_BGDKL : WW_A2_DK;
494 dev->tvaudio = WW_NICAM_I;
497 dev->tvaudio = WW_SYSTEM_L_AM; /* FIXME: fr != ru */
499 case V4L2_STD_NTSC_M:
500 dev->tvaudio = WW_BTSC;
502 case V4L2_STD_NTSC_M_JP:
503 dev->tvaudio = WW_EIAJ;
506 dprintk(1,"tvaudio support needs work for this tv norm [%s], sorry\n",
512 cx_andor(MO_AFECFG_IO, 0x1f, 0x0);
513 cx88_set_tvaudio(dev);
514 cx88_set_stereo(dev,V4L2_TUNER_MODE_STEREO);
516 cx_write(MO_AUDD_LNGTH, 128/8); /* fifo size */
517 cx_write(MO_AUDR_LNGTH, 128/8); /* fifo size */
518 cx_write(MO_AUD_DMACNTRL, 0x03); /* need audio fifo */
522 static int set_tvnorm(struct cx8800_dev *dev, struct cx8800_tvnorm *norm)
528 u32 bdelay,agcdelay,htotal;
529 struct video_channel c;
532 fsc8 = norm_fsc8(norm);
536 dprintk(1,"set_tvnorm: \"%s\" fsc8=%d adc=%d vdec=%d\n",
537 norm->name, fsc8, adc_clock, vdec_clock);
538 set_pll(dev,2,vdec_clock);
540 dprintk(1,"set_tvnorm: MO_INPUT_FORMAT 0x%08x [old=0x%08x]\n",
541 norm->cxiformat, cx_read(MO_INPUT_FORMAT) & 0x0f);
542 cx_andor(MO_INPUT_FORMAT, 0xf, norm->cxiformat);
545 // FIXME: as-is from DScaler
546 dprintk(1,"set_tvnorm: MO_OUTPUT_FORMAT 0x%08x [old=0x%08x]\n",
547 norm->cxoformat, cx_read(MO_OUTPUT_FORMAT));
548 cx_write(MO_OUTPUT_FORMAT, norm->cxoformat);
551 // MO_SCONV_REG = adc clock / video dec clock * 2^17
552 tmp64 = adc_clock * (u64)(1 << 17);
553 do_div(tmp64, vdec_clock);
554 dprintk(1,"set_tvnorm: MO_SCONV_REG 0x%08x [old=0x%08x]\n",
555 (u32)tmp64, cx_read(MO_SCONV_REG));
556 cx_write(MO_SCONV_REG, (u32)tmp64);
558 // MO_SUB_STEP = 8 * fsc / video dec clock * 2^22
559 tmp64 = fsc8 * (u64)(1 << 22);
560 do_div(tmp64, vdec_clock);
561 dprintk(1,"set_tvnorm: MO_SUB_STEP 0x%08x [old=0x%08x]\n",
562 (u32)tmp64, cx_read(MO_SUB_STEP));
563 cx_write(MO_SUB_STEP, (u32)tmp64);
565 // MO_SUB_STEP_DR = 8 * 4406250 / video dec clock * 2^22
566 tmp64 = 4406250 * 8 * (u64)(1 << 22);
567 do_div(tmp64, vdec_clock);
568 dprintk(1,"set_tvnorm: MO_SUB_STEP_DR 0x%08x [old=0x%08x]\n",
569 (u32)tmp64, cx_read(MO_SUB_STEP_DR));
570 cx_write(MO_SUB_STEP_DR, (u32)tmp64);
573 bdelay = vdec_clock * 65 / 20000000 + 21;
574 agcdelay = vdec_clock * 68 / 20000000 + 15;
575 dprintk(1,"set_tvnorm: MO_AGC_BURST 0x%08x [old=0x%08x,bdelay=%d,agcdelay=%d]\n",
576 (bdelay << 8) | agcdelay, cx_read(MO_AGC_BURST), bdelay, agcdelay);
577 cx_write(MO_AGC_BURST, (bdelay << 8) | agcdelay);
580 tmp64 = norm_htotal(norm) * (u64)vdec_clock;
582 htotal = (u32)tmp64 | (norm_notchfilter(norm) << 11);
583 dprintk(1,"set_tvnorm: MO_HTOTAL 0x%08x [old=0x%08x,htotal=%d]\n",
584 htotal, cx_read(MO_HTOTAL), (u32)tmp64);
585 cx_write(MO_HTOTAL, htotal);
588 cx_write(MO_VBI_PACKET, ((1 << 11) | /* (norm_vdelay(norm) << 11) | */
589 norm_vbipack(norm)));
595 memset(&c,0,sizeof(c));
596 c.channel = dev->input;
597 c.norm = VIDEO_MODE_PAL;
598 if ((norm->id & (V4L2_STD_NTSC_M|V4L2_STD_NTSC_M_JP)))
599 c.norm = VIDEO_MODE_NTSC;
600 if (norm->id & V4L2_STD_SECAM)
601 c.norm = VIDEO_MODE_SECAM;
602 cx8800_call_i2c_clients(dev,VIDIOCSCHAN,&c);
608 static int set_scale(struct cx8800_dev *dev, unsigned int width, unsigned int height,
611 unsigned int swidth = norm_swidth(dev->tvnorm);
612 unsigned int sheight = norm_maxh(dev->tvnorm);
615 dprintk(1,"set_scale: %dx%d [%s]\n", width, height, dev->tvnorm->name);
617 // recalc H delay and scale registers
618 value = (width * norm_hdelay(dev->tvnorm)) / swidth;
619 cx_write(MO_HDELAY_EVEN, value);
620 cx_write(MO_HDELAY_ODD, value);
621 dprintk(1,"set_scale: hdelay 0x%04x\n", value);
623 value = (swidth * 4096 / width) - 4096;
624 cx_write(MO_HSCALE_EVEN, value);
625 cx_write(MO_HSCALE_ODD, value);
626 dprintk(1,"set_scale: hscale 0x%04x\n", value);
628 cx_write(MO_HACTIVE_EVEN, width);
629 cx_write(MO_HACTIVE_ODD, width);
630 dprintk(1,"set_scale: hactive 0x%04x\n", width);
632 // recalc V scale Register (delay is constant)
633 cx_write(MO_VDELAY_EVEN, norm_vdelay(dev->tvnorm));
634 cx_write(MO_VDELAY_ODD, norm_vdelay(dev->tvnorm));
635 dprintk(1,"set_scale: vdelay 0x%04x\n", norm_vdelay(dev->tvnorm));
637 value = (0x10000 - (sheight * 512 / height - 512)) & 0x1fff;
638 cx_write(MO_VSCALE_EVEN, value);
639 cx_write(MO_VSCALE_ODD, value);
640 dprintk(1,"set_scale: vscale 0x%04x\n", value);
642 cx_write(MO_VACTIVE_EVEN, sheight);
643 cx_write(MO_VACTIVE_ODD, sheight);
644 dprintk(1,"set_scale: vactive 0x%04x\n", sheight);
648 value |= (1 << 19); // CFILT (default)
650 value |= (1 << 3); // VINT (interlaced vertical scaling)
652 value |= (1 << 0); // 3-tap interpolation
654 value |= (1 << 1); // 5-tap interpolation
656 cx_write(MO_FILTER_EVEN, value);
657 cx_write(MO_FILTER_ODD, value);
658 dprintk(1,"set_scale: filter 0x%04x\n", value);
663 static int video_mux(struct cx8800_dev *dev, unsigned int input)
665 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
666 input, INPUT(input)->vmux,
667 INPUT(input)->gpio0,INPUT(input)->gpio1,
668 INPUT(input)->gpio2,INPUT(input)->gpio3);
670 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input)->vmux << 14);
671 cx_write(MO_GP0_IO, INPUT(input)->gpio0);
672 cx_write(MO_GP1_IO, INPUT(input)->gpio1);
673 cx_write(MO_GP2_IO, INPUT(input)->gpio2);
674 cx_write(MO_GP3_IO, INPUT(input)->gpio3);
676 switch (INPUT(input)->type) {
677 case CX88_VMUX_SVIDEO:
678 cx_andor(MO_AFECFG_IO, 0x01, 0x01);
681 cx_andor(MO_AFECFG_IO, 0x01, 0x00);
687 /* ------------------------------------------------------------------ */
689 static int start_video_dma(struct cx8800_dev *dev,
690 struct cx88_dmaqueue *q,
691 struct cx88_buffer *buf)
693 /* setup fifo + format */
694 cx88_sram_channel_setup(dev, &cx88_sram_channels[SRAM_CH21],
695 buf->bpl, buf->risc.dma);
696 set_scale(dev, buf->vb.width, buf->vb.height, 1);
697 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
700 cx_write(MO_VIDY_GPCNTRL,0x3);
704 cx_set(MO_PCI_INTMSK, 0x00fc01);
705 cx_set(MO_VID_INTMSK, 0x0f0011);
708 cx_set(VID_CAPTURE_CONTROL,0x06);
711 cx_set(MO_DEV_CNTRL2, (1<<5));
712 cx_set(MO_VID_DMACNTRL, 0x11);
717 static int restart_video_queue(struct cx8800_dev *dev,
718 struct cx88_dmaqueue *q)
720 struct cx88_buffer *buf, *prev;
721 struct list_head *item;
723 if (!list_empty(&q->active)) {
724 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
725 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
727 start_video_dma(dev, q, buf);
728 list_for_each(item,&q->active) {
729 buf = list_entry(item, struct cx88_buffer, vb.queue);
730 buf->count = q->count++;
732 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
738 if (list_empty(&q->queued))
740 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
742 list_del(&buf->vb.queue);
743 list_add_tail(&buf->vb.queue,&q->active);
744 start_video_dma(dev, q, buf);
745 buf->vb.state = STATE_ACTIVE;
746 buf->count = q->count++;
747 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
748 dprintk(2,"[%p/%d] restart_queue - first active\n",
751 } else if (prev->vb.width == buf->vb.width &&
752 prev->vb.height == buf->vb.height &&
753 prev->fmt == buf->fmt) {
754 list_del(&buf->vb.queue);
755 list_add_tail(&buf->vb.queue,&q->active);
756 buf->vb.state = STATE_ACTIVE;
757 buf->count = q->count++;
758 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
759 dprintk(2,"[%p/%d] restart_queue - move to active\n",
768 /* ------------------------------------------------------------------ */
771 buffer_setup(struct file *file, unsigned int *count, unsigned int *size)
773 struct cx8800_fh *fh = file->private_data;
775 *size = fh->fmt->depth*fh->width*fh->height >> 3;
778 while (*size * *count > vid_limit * 1024 * 1024)
784 buffer_prepare(struct file *file, struct videobuf_buffer *vb,
785 enum v4l2_field field)
787 struct cx8800_fh *fh = file->private_data;
788 struct cx8800_dev *dev = fh->dev;
789 struct cx88_buffer *buf = (struct cx88_buffer*)vb;
790 int rc, init_buffer = 0;
792 BUG_ON(NULL == fh->fmt);
793 if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
794 fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
796 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
797 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
800 if (buf->fmt != fh->fmt ||
801 buf->vb.width != fh->width ||
802 buf->vb.height != fh->height ||
803 buf->vb.field != field) {
805 buf->vb.width = fh->width;
806 buf->vb.height = fh->height;
807 buf->vb.field = field;
811 if (STATE_NEEDS_INIT == buf->vb.state) {
813 if (0 != (rc = videobuf_iolock(dev->pci,&buf->vb,NULL)))
818 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
819 switch (buf->vb.field) {
821 cx88_risc_buffer(dev->pci, &buf->risc,
822 buf->vb.dma.sglist, 0, UNSET,
823 buf->bpl, 0, buf->vb.height);
825 case V4L2_FIELD_BOTTOM:
826 cx88_risc_buffer(dev->pci, &buf->risc,
827 buf->vb.dma.sglist, UNSET, 0,
828 buf->bpl, 0, buf->vb.height);
830 case V4L2_FIELD_INTERLACED:
831 cx88_risc_buffer(dev->pci, &buf->risc,
832 buf->vb.dma.sglist, 0, buf->bpl,
834 buf->vb.height >> 1);
836 case V4L2_FIELD_SEQ_TB:
837 cx88_risc_buffer(dev->pci, &buf->risc,
839 0, buf->bpl * (buf->vb.height >> 1),
841 buf->vb.height >> 1);
843 case V4L2_FIELD_SEQ_BT:
844 cx88_risc_buffer(dev->pci, &buf->risc,
846 buf->bpl * (buf->vb.height >> 1), 0,
848 buf->vb.height >> 1);
854 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
856 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
857 (unsigned long)buf->risc.dma);
859 buf->vb.state = STATE_PREPARED;
863 cx88_free_buffer(dev->pci,buf);
868 buffer_queue(struct file *file, struct videobuf_buffer *vb)
870 struct cx88_buffer *buf = (struct cx88_buffer*)vb;
871 struct cx88_buffer *prev;
872 struct cx8800_fh *fh = file->private_data;
873 struct cx8800_dev *dev = fh->dev;
874 struct cx88_dmaqueue *q = &dev->vidq;
876 /* add jump to stopper */
877 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | 0x10000);
878 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
880 if (!list_empty(&q->queued)) {
881 list_add_tail(&buf->vb.queue,&q->queued);
882 buf->vb.state = STATE_QUEUED;
883 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
886 } else if (list_empty(&q->active)) {
887 list_add_tail(&buf->vb.queue,&q->active);
888 start_video_dma(dev, q, buf);
889 buf->vb.state = STATE_ACTIVE;
890 buf->count = q->count++;
891 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
892 dprintk(2,"[%p/%d] buffer_queue - first active\n",
896 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
897 if (prev->vb.width == buf->vb.width &&
898 prev->vb.height == buf->vb.height &&
899 prev->fmt == buf->fmt) {
900 list_add_tail(&buf->vb.queue,&q->active);
901 buf->vb.state = STATE_ACTIVE;
902 buf->count = q->count++;
903 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
904 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
908 list_add_tail(&buf->vb.queue,&q->queued);
909 buf->vb.state = STATE_QUEUED;
910 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
916 static void buffer_release(struct file *file, struct videobuf_buffer *vb)
918 struct cx88_buffer *buf = (struct cx88_buffer*)vb;
919 struct cx8800_fh *fh = file->private_data;
921 cx88_free_buffer(fh->dev->pci,buf);
924 struct videobuf_queue_ops cx8800_video_qops = {
925 .buf_setup = buffer_setup,
926 .buf_prepare = buffer_prepare,
927 .buf_queue = buffer_queue,
928 .buf_release = buffer_release,
931 /* ------------------------------------------------------------------ */
933 #if 0 /* overlay support not finished yet */
934 static u32* ov_risc_field(struct cx8800_dev *dev, struct cx8800_fh *fh,
935 u32 *rp, struct btcx_skiplist *skips,
936 u32 sync_line, int skip_even, int skip_odd)
938 int line,maxy,start,end,skip,nskips;
942 /* sync instruction */
943 *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
945 addr = (unsigned long)dev->fbuf.base;
946 addr += dev->fbuf.fmt.bytesperline * fh->win.w.top;
947 addr += (fh->fmt->depth >> 3) * fh->win.w.left;
950 for (maxy = -1, line = 0; line < fh->win.w.height;
951 line++, addr += dev->fbuf.fmt.bytesperline) {
952 if ((line%2) == 0 && skip_even)
954 if ((line%2) == 1 && skip_odd)
957 /* calculate clipping */
959 btcx_calc_skips(line, fh->win.w.width, &maxy,
960 skips, &nskips, fh->clips, fh->nclips);
962 /* write out risc code */
963 for (start = 0, skip = 0; start < fh->win.w.width; start = end) {
964 if (skip >= nskips) {
966 end = fh->win.w.width;
967 } else if (start < skips[skip].start) {
969 end = skips[skip].start;
972 end = skips[skip].end;
975 if (RISC_WRITE == ri)
976 ra = addr + (fh->fmt->depth>>3)*start;
982 if (fh->win.w.width == end)
984 ri |= (fh->fmt->depth>>3) * (end-start);
986 *(rp++)=cpu_to_le32(ri);
988 *(rp++)=cpu_to_le32(ra);
995 static int ov_risc_frame(struct cx8800_dev *dev, struct cx8800_fh *fh,
996 struct cx88_buffer *buf)
998 struct btcx_skiplist *skips;
999 u32 instructions,fields;
1003 /* skip list for window clipping */
1004 if (NULL == (skips = kmalloc(sizeof(*skips) * fh->nclips,GFP_KERNEL)))
1008 if (V4L2_FIELD_HAS_TOP(fh->win.field))
1010 if (V4L2_FIELD_HAS_BOTTOM(fh->win.field))
1013 /* estimate risc mem: worst case is (clip+1) * lines instructions
1014 + syncs + jump (all 2 dwords) */
1015 instructions = (fh->nclips+1) * fh->win.w.height;
1016 instructions += 3 + 4;
1017 if ((rc = btcx_riscmem_alloc(dev->pci,&buf->risc,instructions*8)) < 0) {
1022 /* write risc instructions */
1024 switch (fh->win.field) {
1025 case V4L2_FIELD_TOP:
1026 rp = ov_risc_field(dev, fh, rp, skips, 0, 0, 0);
1028 case V4L2_FIELD_BOTTOM:
1029 rp = ov_risc_field(dev, fh, rp, skips, 0x200, 0, 0);
1031 case V4L2_FIELD_INTERLACED:
1032 rp = ov_risc_field(dev, fh, rp, skips, 0, 0, 1);
1033 rp = ov_risc_field(dev, fh, rp, skips, 0x200, 1, 0);
1039 /* save pointer to jmp instruction address */
1045 static int verify_window(struct cx8800_dev *dev, struct v4l2_window *win)
1047 enum v4l2_field field;
1050 if (NULL == dev->fbuf.base)
1052 if (win->w.width < 48 || win->w.height < 32)
1054 if (win->clipcount > 2048)
1058 maxw = norm_maxw(dev->tvnorm);
1059 maxh = norm_maxh(dev->tvnorm);
1061 if (V4L2_FIELD_ANY == field) {
1062 field = (win->w.height > maxh/2)
1063 ? V4L2_FIELD_INTERLACED
1067 case V4L2_FIELD_TOP:
1068 case V4L2_FIELD_BOTTOM:
1071 case V4L2_FIELD_INTERLACED:
1078 if (win->w.width > maxw)
1079 win->w.width = maxw;
1080 if (win->w.height > maxh)
1081 win->w.height = maxh;
1085 static int setup_window(struct cx8800_dev *dev, struct cx8800_fh *fh,
1086 struct v4l2_window *win)
1088 struct v4l2_clip *clips = NULL;
1089 int n,size,retval = 0;
1091 if (NULL == fh->fmt)
1093 retval = verify_window(dev,win);
1097 /* copy clips -- luckily v4l1 + v4l2 are binary
1098 compatible here ...*/
1100 size = sizeof(*clips)*(n+4);
1101 clips = kmalloc(size,GFP_KERNEL);
1105 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
1111 /* clip against screen */
1112 if (NULL != dev->fbuf.base)
1113 n = btcx_screen_clips(dev->fbuf.fmt.width, dev->fbuf.fmt.height,
1115 btcx_sort_clips(clips,n);
1117 /* 4-byte alignments */
1118 switch (fh->fmt->depth) {
1121 btcx_align(&win->w, clips, n, 3);
1124 btcx_align(&win->w, clips, n, 1);
1127 /* no alignment fixups needed */
1133 down(&fh->vidq.lock);
1140 fh->ov.setup_ok = 1;
1143 /* update overlay if needed */
1146 if (check_btres(fh, RESOURCE_OVERLAY)) {
1147 struct bttv_buffer *new;
1149 new = videobuf_alloc(sizeof(*new));
1150 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
1151 retval = bttv_switch_overlay(btv,fh,new);
1159 /* ------------------------------------------------------------------ */
1161 static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
1164 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1166 case V4L2_BUF_TYPE_VBI_CAPTURE:
1174 static int get_ressource(struct cx8800_fh *fh)
1177 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1178 return RESOURCE_VIDEO;
1179 case V4L2_BUF_TYPE_VBI_CAPTURE:
1180 return RESOURCE_VBI;
1187 static int video_open(struct inode *inode, struct file *file)
1189 int minor = iminor(inode);
1190 struct cx8800_dev *h,*dev = NULL;
1191 struct cx8800_fh *fh;
1192 struct list_head *list;
1193 enum v4l2_buf_type type = 0;
1196 list_for_each(list,&cx8800_devlist) {
1197 h = list_entry(list, struct cx8800_dev, devlist);
1198 if (h->video_dev->minor == minor) {
1200 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1202 if (h->vbi_dev->minor == minor) {
1204 type = V4L2_BUF_TYPE_VBI_CAPTURE;
1207 h->radio_dev->minor == minor) {
1215 dprintk(1,"open minor=%d radio=%d type=%s\n",
1216 minor,radio,v4l2_type_names[type]);
1218 /* allocate + initialize per filehandle data */
1219 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
1222 memset(fh,0,sizeof(*fh));
1223 file->private_data = fh;
1229 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1231 videobuf_queue_init(&fh->vidq, &cx8800_video_qops,
1232 dev->pci, &dev->slock,
1233 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1234 V4L2_FIELD_INTERLACED,
1235 sizeof(struct cx88_buffer));
1236 videobuf_queue_init(&fh->vbiq, &cx8800_vbi_qops,
1237 dev->pci, &dev->slock,
1238 V4L2_BUF_TYPE_VBI_CAPTURE,
1240 sizeof(struct cx88_buffer));
1241 init_MUTEX(&fh->vidq.lock);
1242 init_MUTEX(&fh->vbiq.lock);
1245 dprintk(1,"video_open: setting radio device\n");
1246 cx_write(MO_GP0_IO, cx88_boards[dev->board].radio.gpio0);
1247 cx_write(MO_GP1_IO, cx88_boards[dev->board].radio.gpio1);
1248 cx_write(MO_GP2_IO, cx88_boards[dev->board].radio.gpio2);
1249 cx_write(MO_GP3_IO, cx88_boards[dev->board].radio.gpio3);
1250 dev->tvaudio = WW_FM;
1251 cx88_set_tvaudio(dev);
1252 cx88_set_stereo(dev,V4L2_TUNER_MODE_STEREO);
1253 cx8800_call_i2c_clients(dev,AUDC_SET_RADIO,NULL);
1260 video_read(struct file *file, char *data, size_t count, loff_t *ppos)
1262 struct cx8800_fh *fh = file->private_data;
1265 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1266 if (res_locked(fh->dev,RESOURCE_VIDEO))
1268 return videobuf_read_one(file, &fh->vidq, data, count, ppos);
1269 case V4L2_BUF_TYPE_VBI_CAPTURE:
1270 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1272 return videobuf_read_stream(file, &fh->vbiq, data, count, ppos, 1);
1280 video_poll(struct file *file, struct poll_table_struct *wait)
1282 struct cx8800_fh *fh = file->private_data;
1284 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1285 return videobuf_poll_stream(file, &fh->vbiq, wait);
1291 static int video_release(struct inode *inode, struct file *file)
1293 struct cx8800_fh *fh = file->private_data;
1294 struct cx8800_dev *dev = fh->dev;
1296 /* turn off overlay */
1297 if (res_check(fh, RESOURCE_OVERLAY)) {
1299 res_free(dev,fh,RESOURCE_OVERLAY);
1302 /* stop video capture */
1303 if (res_check(fh, RESOURCE_VIDEO)) {
1304 videobuf_queue_cancel(file,&fh->vidq);
1305 res_free(dev,fh,RESOURCE_VIDEO);
1307 if (fh->vidq.read_buf) {
1308 buffer_release(file,fh->vidq.read_buf);
1309 kfree(fh->vidq.read_buf);
1312 /* stop vbi capture */
1313 if (res_check(fh, RESOURCE_VBI)) {
1314 if (fh->vbiq.streaming)
1315 videobuf_streamoff(file,&fh->vbiq);
1316 if (fh->vbiq.reading)
1317 videobuf_read_stop(file,&fh->vbiq);
1318 res_free(dev,fh,RESOURCE_VBI);
1321 file->private_data = NULL;
1327 video_mmap(struct file *file, struct vm_area_struct * vma)
1329 struct cx8800_fh *fh = file->private_data;
1331 return videobuf_mmap_mapper(vma, get_queue(fh));
1334 /* ------------------------------------------------------------------ */
1336 static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl)
1338 struct cx88_ctrl *c = NULL;
1342 for (i = 0; i < CX8800_CTLS; i++)
1343 if (cx8800_ctls[i].v.id == ctl->id)
1344 c = &cx8800_ctls[i];
1348 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
1350 case V4L2_CID_AUDIO_BALANCE:
1351 ctl->value = (value & 0x40) ? (value & 0x3f) : (0x40 - (value & 0x3f));
1354 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
1360 static int set_control(struct cx8800_dev *dev, struct v4l2_control *ctl)
1362 struct cx88_ctrl *c = NULL;
1367 for (i = 0; i < CX8800_CTLS; i++)
1368 if (cx8800_ctls[i].v.id == ctl->id)
1369 c = &cx8800_ctls[i];
1373 if (ctl->value < c->v.minimum)
1375 if (ctl->value > c->v.maximum)
1378 case V4L2_CID_AUDIO_BALANCE:
1379 value = (ctl->value < 0x40) ? (0x40 - ctl->value) : ctl->value;
1381 case V4L2_CID_SATURATION:
1382 /* special v_sat handling */
1383 v_sat_value = ctl->value - (0x7f - 0x5a);
1384 if (v_sat_value > 0xff)
1386 if (v_sat_value < 0x00)
1388 cx_andor(MO_UV_SATURATION, 0xff00, v_sat_value << 8);
1389 /* fall through to default route for u_sat */
1391 value = ((ctl->value - c->off) << c->shift) & c->mask;
1394 dprintk(1,"set_control id=0x%X reg=0x%x val=0x%x%s\n",
1395 ctl->id, c->reg, value, c->sreg ? " [shadowed]" : "");
1397 cx_sandor(c->sreg, c->reg, c->mask, value);
1399 cx_andor(c->reg, c->mask, value);
1404 static void init_controls(struct cx8800_dev *dev)
1406 static struct v4l2_control mute = {
1407 .id = V4L2_CID_AUDIO_MUTE,
1410 static struct v4l2_control volume = {
1411 .id = V4L2_CID_AUDIO_VOLUME,
1415 set_control(dev,&mute);
1416 set_control(dev,&volume);
1419 /* ------------------------------------------------------------------ */
1421 static int cx8800_g_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1422 struct v4l2_format *f)
1425 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1426 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
1427 f->fmt.pix.width = fh->width;
1428 f->fmt.pix.height = fh->height;
1429 f->fmt.pix.field = fh->vidq.field;
1430 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1431 f->fmt.pix.bytesperline =
1432 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1433 f->fmt.pix.sizeimage =
1434 f->fmt.pix.height * f->fmt.pix.bytesperline;
1436 case V4L2_BUF_TYPE_VBI_CAPTURE:
1437 cx8800_vbi_fmt(dev, f);
1444 static int cx8800_try_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1445 struct v4l2_format *f)
1448 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1450 struct cx8800_fmt *fmt;
1451 enum v4l2_field field;
1452 unsigned int maxw, maxh;
1454 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1458 field = f->fmt.pix.field;
1459 maxw = norm_maxw(dev->tvnorm);
1460 maxh = norm_maxh(dev->tvnorm);
1463 if (V4L2_FIELD_ANY == field) {
1464 field = (f->fmt.pix.height > maxh/2)
1465 ? V4L2_FIELD_INTERLACED
1466 : V4L2_FIELD_BOTTOM;
1469 field = V4L2_FIELD_INTERLACED;
1472 case V4L2_FIELD_TOP:
1473 case V4L2_FIELD_BOTTOM:
1476 case V4L2_FIELD_INTERLACED:
1482 f->fmt.pix.field = field;
1483 if (f->fmt.pix.width < 48)
1484 f->fmt.pix.width = 48;
1485 if (f->fmt.pix.height < 32)
1486 f->fmt.pix.height = 32;
1487 if (f->fmt.pix.width > maxw)
1488 f->fmt.pix.width = maxw;
1489 if (f->fmt.pix.height > maxh)
1490 f->fmt.pix.height = maxh;
1491 f->fmt.pix.bytesperline =
1492 (f->fmt.pix.width * fmt->depth) >> 3;
1493 f->fmt.pix.sizeimage =
1494 f->fmt.pix.height * f->fmt.pix.bytesperline;
1498 case V4L2_BUF_TYPE_VBI_CAPTURE:
1499 cx8800_vbi_fmt(dev, f);
1506 static int cx8800_s_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1507 struct v4l2_format *f)
1512 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1513 err = cx8800_try_fmt(dev,fh,f);
1517 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1518 fh->width = f->fmt.pix.width;
1519 fh->height = f->fmt.pix.height;
1520 fh->vidq.field = f->fmt.pix.field;
1522 case V4L2_BUF_TYPE_VBI_CAPTURE:
1523 cx8800_vbi_fmt(dev, f);
1531 * This function is _not_ called directly, but from
1532 * video_generic_ioctl (and maybe others). userspace
1533 * copying is done already, arg is a kernel pointer.
1535 static int video_do_ioctl(struct inode *inode, struct file *file,
1536 unsigned int cmd, void *arg)
1538 struct cx8800_fh *fh = file->private_data;
1539 struct cx8800_dev *dev = fh->dev;
1541 unsigned long flags;
1545 if (video_debug > 1)
1546 cx88_print_ioctl(dev->name,cmd);
1548 case VIDIOC_QUERYCAP:
1550 struct v4l2_capability *cap = arg;
1552 memset(cap,0,sizeof(*cap));
1553 strcpy(cap->driver, "cx8800");
1554 strlcpy(cap->card, cx88_boards[dev->board].name,
1556 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1557 cap->version = CX88_VERSION_CODE;
1559 V4L2_CAP_VIDEO_CAPTURE |
1560 V4L2_CAP_READWRITE |
1561 V4L2_CAP_STREAMING |
1562 V4L2_CAP_VBI_CAPTURE |
1564 V4L2_CAP_VIDEO_OVERLAY |
1567 if (UNSET != dev->tuner_type)
1568 cap->capabilities |= V4L2_CAP_TUNER;
1573 /* ---------- tv norms ---------- */
1574 case VIDIOC_ENUMSTD:
1576 struct v4l2_standard *e = arg;
1580 if (i >= ARRAY_SIZE(tvnorms))
1582 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1583 tvnorms[e->index].name);
1591 v4l2_std_id *id = arg;
1593 *id = dev->tvnorm->id;
1598 v4l2_std_id *id = arg;
1601 for(i = 0; i < ARRAY_SIZE(tvnorms); i++)
1602 if (*id & tvnorms[i].id)
1604 if (i == ARRAY_SIZE(tvnorms))
1608 set_tvnorm(dev,&tvnorms[i]);
1613 /* ------ input switching ---------- */
1614 case VIDIOC_ENUMINPUT:
1616 static const char *iname[] = {
1617 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1618 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1619 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1620 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1621 [ CX88_VMUX_TELEVISION ] = "Television",
1622 [ CX88_VMUX_SVIDEO ] = "S-Video",
1623 [ CX88_VMUX_DEBUG ] = "for debug only",
1625 struct v4l2_input *i = arg;
1631 if (0 == INPUT(n)->type)
1633 memset(i,0,sizeof(*i));
1635 i->type = V4L2_INPUT_TYPE_CAMERA;
1636 strcpy(i->name,iname[INPUT(n)->type]);
1637 if (CX88_VMUX_TELEVISION == INPUT(n)->type)
1638 i->type = V4L2_INPUT_TYPE_TUNER;
1639 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1640 i->std |= tvnorms[n].id;
1643 case VIDIOC_G_INPUT:
1645 unsigned int *i = arg;
1650 case VIDIOC_S_INPUT:
1652 unsigned int *i = arg;
1662 /* --- capture ioctls ---------------------------------------- */
1663 case VIDIOC_ENUM_FMT:
1665 struct v4l2_fmtdesc *f = arg;
1666 enum v4l2_buf_type type;
1672 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1673 if (index >= ARRAY_SIZE(formats))
1675 memset(f,0,sizeof(*f));
1678 strlcpy(f->description,formats[index].name,sizeof(f->description));
1679 f->pixelformat = formats[index].fourcc;
1688 struct v4l2_format *f = arg;
1689 return cx8800_g_fmt(dev,fh,f);
1693 struct v4l2_format *f = arg;
1694 return cx8800_s_fmt(dev,fh,f);
1696 case VIDIOC_TRY_FMT:
1698 struct v4l2_format *f = arg;
1699 return cx8800_try_fmt(dev,fh,f);
1702 /* --- controls ---------------------------------------------- */
1703 case VIDIOC_QUERYCTRL:
1705 struct v4l2_queryctrl *c = arg;
1708 if (c->id < V4L2_CID_BASE ||
1709 c->id >= V4L2_CID_LASTP1)
1711 for (i = 0; i < CX8800_CTLS; i++)
1712 if (cx8800_ctls[i].v.id == c->id)
1714 if (i == CX8800_CTLS) {
1718 *c = cx8800_ctls[i].v;
1722 return get_control(dev,arg);
1724 return set_control(dev,arg);
1726 /* --- tuner ioctls ------------------------------------------ */
1727 case VIDIOC_G_TUNER:
1729 struct v4l2_tuner *t = arg;
1732 if (UNSET == dev->tuner_type)
1737 memset(t,0,sizeof(*t));
1738 strcpy(t->name, "Television");
1739 t->type = V4L2_TUNER_ANALOG_TV;
1740 t->capability = V4L2_TUNER_CAP_NORM;
1741 t->rangehigh = 0xffffffffUL;
1743 cx88_get_stereo(dev ,t);
1744 reg = cx_read(MO_DEVICE_STATUS);
1745 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1748 case VIDIOC_S_TUNER:
1750 struct v4l2_tuner *t = arg;
1752 if (UNSET == dev->tuner_type)
1756 cx88_set_stereo(dev,t->audmode);
1759 case VIDIOC_G_FREQUENCY:
1761 struct v4l2_frequency *f = arg;
1763 if (UNSET == dev->tuner_type)
1767 memset(f,0,sizeof(*f));
1768 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1769 f->frequency = dev->freq;
1772 case VIDIOC_S_FREQUENCY:
1774 struct v4l2_frequency *f = arg;
1776 if (UNSET == dev->tuner_type)
1780 if (0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV)
1782 if (1 == fh->radio && f->type != V4L2_TUNER_RADIO)
1785 dev->freq = f->frequency;
1786 cx8800_call_i2c_clients(dev,VIDIOCSFREQ,&dev->freq);
1791 /* --- streaming capture ------------------------------------- */
1794 struct video_mbuf *mbuf = arg;
1795 struct videobuf_queue *q;
1796 struct v4l2_requestbuffers req;
1800 memset(&req,0,sizeof(req));
1803 req.memory = V4L2_MEMORY_MMAP;
1804 err = videobuf_reqbufs(file,q,&req);
1807 memset(mbuf,0,sizeof(*mbuf));
1808 mbuf->frames = req.count;
1810 for (i = 0; i < mbuf->frames; i++) {
1811 mbuf->offsets[i] = q->bufs[i]->boff;
1812 mbuf->size += q->bufs[i]->bsize;
1816 case VIDIOC_REQBUFS:
1817 return videobuf_reqbufs(file, get_queue(fh), arg);
1819 case VIDIOC_QUERYBUF:
1820 return videobuf_querybuf(get_queue(fh), arg);
1823 return videobuf_qbuf(file, get_queue(fh), arg);
1826 return videobuf_dqbuf(file, get_queue(fh), arg);
1828 case VIDIOC_STREAMON:
1830 int res = get_ressource(fh);
1832 if (!res_get(dev,fh,res))
1834 return videobuf_streamon(file, get_queue(fh));
1836 case VIDIOC_STREAMOFF:
1838 int res = get_ressource(fh);
1840 err = videobuf_streamoff(file, get_queue(fh));
1843 res_free(dev,fh,res);
1848 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1854 static int video_ioctl(struct inode *inode, struct file *file,
1855 unsigned int cmd, unsigned long arg)
1857 return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
1860 /* ----------------------------------------------------------- */
1862 static int radio_do_ioctl(struct inode *inode, struct file *file,
1863 unsigned int cmd, void *arg)
1865 struct cx8800_fh *fh = file->private_data;
1866 struct cx8800_dev *dev = fh->dev;
1868 if (video_debug > 1)
1869 cx88_print_ioctl(dev->name,cmd);
1872 case VIDIOC_QUERYCAP:
1874 struct v4l2_capability *cap = arg;
1876 memset(cap,0,sizeof(*cap));
1877 strcpy(cap->driver, "cx8800");
1878 strlcpy(cap->card, cx88_boards[dev->board].name,
1880 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1881 cap->version = CX88_VERSION_CODE;
1882 cap->capabilities = V4L2_CAP_TUNER;
1885 case VIDIOC_G_TUNER:
1887 struct v4l2_tuner *t = arg;
1888 struct video_tuner vt;
1893 memset(t,0,sizeof(*t));
1894 strcpy(t->name, "Radio");
1895 t->rangelow = (int)(65*16);
1896 t->rangehigh = (int)(108*16);
1898 memset(&vt,0,sizeof(vt));
1899 cx8800_call_i2c_clients(dev,VIDIOCGTUNER,&vt);
1900 t->signal = vt.signal;
1903 case VIDIOC_ENUMINPUT:
1905 struct v4l2_input *i = arg;
1909 strcpy(i->name,"Radio");
1910 i->type = V4L2_INPUT_TYPE_TUNER;
1913 case VIDIOC_G_INPUT:
1919 case VIDIOC_G_AUDIO:
1921 struct v4l2_audio *a = arg;
1923 memset(a,0,sizeof(*a));
1924 strcpy(a->name,"Radio");
1929 v4l2_std_id *id = arg;
1933 case VIDIOC_S_AUDIO:
1934 case VIDIOC_S_TUNER:
1935 case VIDIOC_S_INPUT:
1939 case VIDIOC_QUERYCTRL:
1941 struct v4l2_queryctrl *c = arg;
1944 if (c->id < V4L2_CID_BASE ||
1945 c->id >= V4L2_CID_LASTP1)
1947 if (c->id == V4L2_CID_AUDIO_MUTE) {
1948 for (i = 0; i < CX8800_CTLS; i++)
1949 if (cx8800_ctls[i].v.id == c->id)
1951 *c = cx8800_ctls[i].v;
1960 case VIDIOC_G_FREQUENCY:
1961 case VIDIOC_S_FREQUENCY:
1962 return video_do_ioctl(inode,file,cmd,arg);
1965 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1971 static int radio_ioctl(struct inode *inode, struct file *file,
1972 unsigned int cmd, unsigned long arg)
1974 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
1977 /* ----------------------------------------------------------- */
1979 static void cx8800_vid_timeout(unsigned long data)
1981 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1982 struct cx88_dmaqueue *q = &dev->vidq;
1983 struct cx88_buffer *buf;
1984 unsigned long flags;
1986 cx88_sram_channel_dump(dev, &cx88_sram_channels[SRAM_CH21]);
1987 //cx88_risc_disasm(dev,&dev->vidq.stopper);
1989 cx_clear(MO_VID_DMACNTRL, 0x11);
1990 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1992 spin_lock_irqsave(&dev->slock,flags);
1993 while (!list_empty(&q->active)) {
1994 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1995 list_del(&buf->vb.queue);
1996 buf->vb.state = STATE_ERROR;
1997 wake_up(&buf->vb.done);
1998 printk("%s: [%p/%d] timeout - dma=0x%08lx\n", dev->name,
1999 buf, buf->vb.i, (unsigned long)buf->risc.dma);
2001 restart_video_queue(dev,q);
2002 spin_unlock_irqrestore(&dev->slock,flags);
2005 static void cx8800_wakeup(struct cx8800_dev *dev,
2006 struct cx88_dmaqueue *q, u32 count)
2008 struct cx88_buffer *buf;
2011 if (list_empty(&q->active))
2013 buf = list_entry(q->active.next,
2014 struct cx88_buffer, vb.queue);
2015 if (buf->count > count)
2017 do_gettimeofday(&buf->vb.ts);
2018 dprintk(2,"[%p/%d] wakeup reg=%d buf=%d\n",buf,buf->vb.i,
2020 buf->vb.state = STATE_DONE;
2021 list_del(&buf->vb.queue);
2022 wake_up(&buf->vb.done);
2024 if (list_empty(&q->active)) {
2025 del_timer(&q->timeout);
2027 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
2031 static void cx8800_vid_irq(struct cx8800_dev *dev)
2033 u32 status, mask, count;
2035 status = cx_read(MO_VID_INTSTAT);
2036 mask = cx_read(MO_VID_INTMSK);
2037 if (0 == (status & mask))
2039 cx_write(MO_VID_INTSTAT, status);
2040 if (irq_debug || (status & mask & ~0xff))
2041 cx88_print_irqbits(dev->name, "irq vid",
2042 cx88_vid_irqs, status, mask);
2044 /* risc op code error */
2045 if (status & (1 << 16)) {
2046 printk(KERN_WARNING "%s: video risc op code error\n",dev->name);
2047 cx_clear(MO_VID_DMACNTRL, 0x11);
2048 cx_clear(VID_CAPTURE_CONTROL, 0x06);
2049 cx88_sram_channel_dump(dev, &cx88_sram_channels[SRAM_CH21]);
2053 if (status & 0x01) {
2054 spin_lock(&dev->slock);
2055 count = cx_read(MO_VIDY_GPCNT);
2056 cx8800_wakeup(dev, &dev->vidq, count);
2057 spin_unlock(&dev->slock);
2061 if (status & 0x08) {
2062 spin_lock(&dev->slock);
2063 count = cx_read(MO_VBI_GPCNT);
2064 cx8800_wakeup(dev, &dev->vbiq, count);
2065 spin_unlock(&dev->slock);
2069 if (status & 0x10) {
2070 dprintk(2,"stopper video\n");
2071 spin_lock(&dev->slock);
2072 restart_video_queue(dev,&dev->vidq);
2073 spin_unlock(&dev->slock);
2077 if (status & 0x80) {
2078 dprintk(2,"stopper vbi\n");
2079 spin_lock(&dev->slock);
2080 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2081 spin_unlock(&dev->slock);
2085 static irqreturn_t cx8800_irq(int irq, void *dev_id, struct pt_regs *regs)
2087 struct cx8800_dev *dev = dev_id;
2089 int loop, handled = 0;
2091 for (loop = 0; loop < 10; loop++) {
2092 status = cx_read(MO_PCI_INTSTAT);
2093 mask = cx_read(MO_PCI_INTMSK);
2094 if (0 == (status & mask))
2097 cx_write(MO_PCI_INTSTAT, status);
2098 if (irq_debug || (status & mask & ~0x1f))
2099 cx88_print_irqbits(dev->name, "irq pci",
2100 cx88_pci_irqs, status, mask);
2103 cx8800_vid_irq(dev);
2106 printk(KERN_WARNING "%s: irq loop -- clearing mask\n",
2108 cx_write(MO_PCI_INTMSK,0);
2112 return IRQ_RETVAL(handled);
2115 /* ----------------------------------------------------------- */
2116 /* exported stuff */
2118 static struct file_operations video_fops =
2120 .owner = THIS_MODULE,
2122 .release = video_release,
2126 .ioctl = video_ioctl,
2127 .llseek = no_llseek,
2130 struct video_device cx8800_video_template =
2132 .name = "cx8800-video",
2133 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
2135 .fops = &video_fops,
2139 struct video_device cx8800_vbi_template =
2141 .name = "cx8800-vbi",
2142 .type = VID_TYPE_TELETEXT|VID_TYPE_TUNER,
2144 .fops = &video_fops,
2148 static struct file_operations radio_fops =
2150 .owner = THIS_MODULE,
2152 .release = video_release,
2153 .ioctl = radio_ioctl,
2154 .llseek = no_llseek,
2157 struct video_device cx8800_radio_template =
2159 .name = "cx8800-radio",
2160 .type = VID_TYPE_TUNER,
2162 .fops = &radio_fops,
2166 /* ----------------------------------------------------------- */
2168 static void cx8800_shutdown(struct cx8800_dev *dev)
2170 /* disable RISC controller + IRQs */
2171 cx_write(MO_DEV_CNTRL2, 0);
2173 /* stop dma transfers */
2174 cx_write(MO_VID_DMACNTRL, 0x0);
2175 cx_write(MO_AUD_DMACNTRL, 0x0);
2176 cx_write(MO_TS_DMACNTRL, 0x0);
2177 cx_write(MO_VIP_DMACNTRL, 0x0);
2178 cx_write(MO_GPHST_DMACNTRL, 0x0);
2180 /* stop interupts */
2181 cx_write(MO_PCI_INTMSK, 0x0);
2182 cx_write(MO_VID_INTMSK, 0x0);
2183 cx_write(MO_AUD_INTMSK, 0x0);
2184 cx_write(MO_TS_INTMSK, 0x0);
2185 cx_write(MO_VIP_INTMSK, 0x0);
2186 cx_write(MO_GPHST_INTMSK, 0x0);
2188 /* stop capturing */
2189 cx_write(VID_CAPTURE_CONTROL, 0);
2192 static int cx8800_reset(struct cx8800_dev *dev)
2194 dprintk(1,"cx8800_reset\n");
2196 cx8800_shutdown(dev);
2198 /* clear irq status */
2199 cx_write(MO_VID_INTSTAT, 0xFFFFFFFF); // Clear PIV int
2200 cx_write(MO_PCI_INTSTAT, 0xFFFFFFFF); // Clear PCI int
2201 cx_write(MO_INT1_STAT, 0xFFFFFFFF); // Clear RISC int
2204 set_current_state(TASK_INTERRUPTIBLE);
2205 schedule_timeout(HZ/10);
2208 cx88_sram_channel_setup(dev, &cx88_sram_channels[SRAM_CH21], 720*4, 0);
2209 cx88_sram_channel_setup(dev, &cx88_sram_channels[SRAM_CH22], 128, 0);
2210 cx88_sram_channel_setup(dev, &cx88_sram_channels[SRAM_CH23], 128, 0);
2211 cx88_sram_channel_setup(dev, &cx88_sram_channels[SRAM_CH24], 128, 0);
2212 cx88_sram_channel_setup(dev, &cx88_sram_channels[SRAM_CH25], 128, 0);
2213 cx88_sram_channel_setup(dev, &cx88_sram_channels[SRAM_CH26], 128, 0);
2216 cx_write(MO_INPUT_FORMAT, ((1 << 13) | // agc enable
2217 (1 << 12) | // agc gain
2218 (1 << 11) | // adaptibe agc
2219 (0 << 10) | // chroma agc
2220 (0 << 9) | // ckillen
2223 /* setup image format */
2224 cx_andor(MO_COLOR_CTRL, 0x4000, 0x4000);
2226 /* setup FIFO Threshholds */
2227 cx_write(MO_PDMA_STHRSH, 0x0807);
2228 cx_write(MO_PDMA_DTHRSH, 0x0807);
2230 /* fixes flashing of image */
2231 cx_write(MO_AGC_SYNC_TIP1, 0x0380000F);
2232 cx_write(MO_AGC_BACK_VBI, 0x00E00555);
2234 cx_write(MO_VID_INTSTAT, 0xFFFFFFFF); // Clear PIV int
2235 cx_write(MO_PCI_INTSTAT, 0xFFFFFFFF); // Clear PCI int
2236 cx_write(MO_INT1_STAT, 0xFFFFFFFF); // Clear RISC int
2241 static struct video_device *vdev_init(struct cx8800_dev *dev,
2242 struct video_device *template,
2245 struct video_device *vfd;
2247 vfd = video_device_alloc();
2252 vfd->dev = &dev->pci->dev;
2253 vfd->release = video_device_release;
2254 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
2255 dev->name, type, cx88_boards[dev->board].name);
2259 static void cx8800_unregister_video(struct cx8800_dev *dev)
2261 if (dev->radio_dev) {
2262 if (-1 != dev->radio_dev->minor)
2263 video_unregister_device(dev->radio_dev);
2265 video_device_release(dev->radio_dev);
2266 dev->radio_dev = NULL;
2269 if (-1 != dev->vbi_dev->minor)
2270 video_unregister_device(dev->vbi_dev);
2272 video_device_release(dev->vbi_dev);
2273 dev->vbi_dev = NULL;
2275 if (dev->video_dev) {
2276 if (-1 != dev->video_dev->minor)
2277 video_unregister_device(dev->video_dev);
2279 video_device_release(dev->video_dev);
2280 dev->video_dev = NULL;
2284 /* debug that damn oops ... */
2285 static unsigned int oops = 0;
2286 MODULE_PARM(oops,"i");
2287 #define OOPS(msg) if (oops) printk("%s: %s\n",__FUNCTION__,msg);
2289 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
2290 const struct pci_device_id *pci_id)
2292 struct cx8800_dev *dev;
2296 dev = kmalloc(sizeof(*dev),GFP_KERNEL);
2299 memset(dev,0,sizeof(*dev));
2304 if (pci_enable_device(pci_dev)) {
2308 sprintf(dev->name,"cx%x[%d]",pci_dev->device,cx8800_devcount);
2312 cx88_pci_quirks(dev->name, dev->pci, &latency);
2313 if (UNSET != latency) {
2314 printk(KERN_INFO "%s: setting pci latency timer to %d\n",
2316 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
2319 /* print pci info */
2321 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
2322 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
2323 printk(KERN_INFO "%s: found at %s, rev: %d, irq: %d, "
2324 "latency: %d, mmio: 0x%lx\n", dev->name,
2325 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
2326 dev->pci_lat,pci_resource_start(pci_dev,0));
2328 pci_set_master(pci_dev);
2329 if (!pci_dma_supported(pci_dev,0xffffffff)) {
2330 printk("%s: Oops: no 32bit PCI DMA ???\n",dev->name);
2336 OOPS("board config");
2337 dev->board = card[cx8800_devcount];
2338 for (i = 0; UNSET == dev->board && i < cx88_idcount; i++)
2339 if (pci_dev->subsystem_vendor == cx88_subids[i].subvendor &&
2340 pci_dev->subsystem_device == cx88_subids[i].subdevice)
2341 dev->board = cx88_subids[i].card;
2342 if (UNSET == dev->board)
2343 dev->board = CX88_BOARD_UNKNOWN;
2344 printk(KERN_INFO "%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
2345 dev->name,pci_dev->subsystem_vendor,
2346 pci_dev->subsystem_device,cx88_boards[dev->board].name,
2347 dev->board, card[cx8800_devcount] == dev->board ?
2348 "insmod option" : "autodetected");
2350 dev->tuner_type = tuner[cx8800_devcount];
2351 if (UNSET == dev->tuner_type)
2352 dev->tuner_type = cx88_boards[dev->board].tuner_type;
2356 if (!request_mem_region(pci_resource_start(pci_dev,0),
2357 pci_resource_len(pci_dev,0),
2360 printk(KERN_ERR "%s: can't get MMIO memory @ 0x%lx\n",
2361 dev->name,pci_resource_start(pci_dev,0));
2364 dev->lmmio = ioremap(pci_resource_start(pci_dev,0),
2365 pci_resource_len(pci_dev,0));
2366 dev->bmmio = (u8*)dev->lmmio;
2368 /* initialize driver struct */
2369 OOPS("init structs");
2370 init_MUTEX(&dev->lock);
2371 dev->slock = SPIN_LOCK_UNLOCKED;
2372 dev->tvnorm = tvnorms;
2374 /* init video dma queues */
2375 INIT_LIST_HEAD(&dev->vidq.active);
2376 INIT_LIST_HEAD(&dev->vidq.queued);
2377 dev->vidq.timeout.function = cx8800_vid_timeout;
2378 dev->vidq.timeout.data = (unsigned long)dev;
2379 init_timer(&dev->vidq.timeout);
2380 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
2381 MO_VID_DMACNTRL,0x11,0x00);
2383 /* init vbi dma queues */
2384 INIT_LIST_HEAD(&dev->vbiq.active);
2385 INIT_LIST_HEAD(&dev->vbiq.queued);
2386 dev->vbiq.timeout.function = cx8800_vbi_timeout;
2387 dev->vbiq.timeout.data = (unsigned long)dev;
2388 init_timer(&dev->vbiq.timeout);
2389 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
2390 MO_VID_DMACNTRL,0x88,0x00);
2392 /* initialize hardware */
2393 OOPS("reset hardware");
2397 OOPS("install irq handler");
2398 err = request_irq(pci_dev->irq, cx8800_irq,
2399 SA_SHIRQ | SA_INTERRUPT, dev->name, dev);
2401 printk(KERN_ERR "%s: can't get IRQ %d\n",
2402 dev->name,pci_dev->irq);
2406 /* register i2c bus + load i2c helpers */
2408 cx8800_i2c_init(dev);
2410 cx88_card_setup(dev);
2412 /* load and configure helper modules */
2413 OOPS("configure i2c clients");
2414 if (TUNER_ABSENT != dev->tuner_type)
2415 request_module("tuner");
2416 if (cx88_boards[dev->board].needs_tda9887)
2417 request_module("tda9887");
2418 if (dev->tuner_type != UNSET)
2419 cx8800_call_i2c_clients(dev,TUNER_SET_TYPE,&dev->tuner_type);
2421 /* register v4l devices */
2422 OOPS("register video");
2423 dev->video_dev = vdev_init(dev,&cx8800_video_template,"video");
2424 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
2425 video_nr[cx8800_devcount]);
2427 printk(KERN_INFO "%s: can't register video device\n",
2431 printk(KERN_INFO "%s: registered device video%d [v4l2]\n",
2432 dev->name,dev->video_dev->minor & 0x1f);
2434 OOPS("register vbi");
2435 dev->vbi_dev = vdev_init(dev,&cx8800_vbi_template,"vbi");
2436 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
2437 vbi_nr[cx8800_devcount]);
2439 printk(KERN_INFO "%s: can't register vbi device\n",
2443 printk(KERN_INFO "%s: registered device vbi%d\n",
2444 dev->name,dev->vbi_dev->minor & 0x1f);
2446 if (dev->has_radio) {
2447 OOPS("register radio");
2448 dev->radio_dev = vdev_init(dev,&cx8800_radio_template,"radio");
2449 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
2450 radio_nr[cx8800_devcount]);
2452 printk(KERN_INFO "%s: can't register radio device\n",
2456 printk(KERN_INFO "%s: registered device radio%d\n",
2457 dev->name,dev->radio_dev->minor & 0x1f);
2460 /* everything worked */
2462 list_add_tail(&dev->devlist,&cx8800_devlist);
2463 pci_set_drvdata(pci_dev,dev);
2466 /* initial device configuration */
2467 OOPS("init device");
2470 set_tvnorm(dev,tvnorms);
2477 cx8800_unregister_video(dev);
2478 if (0 == dev->i2c_rc)
2479 i2c_bit_del_bus(&dev->i2c_adap);
2480 free_irq(pci_dev->irq, dev);
2483 release_mem_region(pci_resource_start(pci_dev,0),
2484 pci_resource_len(pci_dev,0));
2491 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
2493 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2495 cx8800_shutdown(dev);
2496 pci_disable_device(pci_dev);
2498 /* unregister stuff */
2499 if (0 == dev->i2c_rc)
2500 i2c_bit_del_bus(&dev->i2c_adap);
2502 free_irq(pci_dev->irq, dev);
2503 release_mem_region(pci_resource_start(pci_dev,0),
2504 pci_resource_len(pci_dev,0));
2506 cx8800_unregister_video(dev);
2507 pci_set_drvdata(pci_dev, NULL);
2510 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
2511 list_del(&dev->devlist);
2516 static int cx8800_suspend(struct pci_dev *pci_dev, u32 state)
2518 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2520 printk("%s: suspend %d\n", dev->name, state);
2522 cx8800_shutdown(dev);
2523 del_timer(&dev->vidq.timeout);
2525 pci_save_state(pci_dev, dev->state.pci_cfg);
2526 if (0 != pci_set_power_state(pci_dev, state)) {
2527 pci_disable_device(pci_dev);
2528 dev->state.disabled = 1;
2533 static int cx8800_resume(struct pci_dev *pci_dev)
2535 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2537 printk("%s: resume\n", dev->name);
2539 if (dev->state.disabled) {
2540 pci_enable_device(pci_dev);
2541 dev->state.disabled = 0;
2543 pci_set_power_state(pci_dev, 0);
2544 pci_restore_state(pci_dev, dev->state.pci_cfg);
2546 /* re-initialize hardware */
2549 /* restart video capture */
2550 spin_lock(&dev->slock);
2551 restart_video_queue(dev,&dev->vidq);
2552 spin_unlock(&dev->slock);
2557 /* ----------------------------------------------------------- */
2559 struct pci_device_id cx8800_pci_tbl[] = {
2563 .subvendor = PCI_ANY_ID,
2564 .subdevice = PCI_ANY_ID,
2566 /* --- end of list --- */
2569 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2571 static struct pci_driver cx8800_pci_driver = {
2573 .id_table = cx8800_pci_tbl,
2574 .probe = cx8800_initdev,
2575 .remove = cx8800_finidev,
2577 .suspend = cx8800_suspend,
2578 .resume = cx8800_resume,
2581 static int cx8800_init(void)
2583 INIT_LIST_HEAD(&cx8800_devlist);
2584 printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
2585 (CX88_VERSION_CODE >> 16) & 0xff,
2586 (CX88_VERSION_CODE >> 8) & 0xff,
2587 CX88_VERSION_CODE & 0xff);
2589 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2590 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2592 return pci_module_init(&cx8800_pci_driver);
2595 static void cx8800_fini(void)
2597 pci_unregister_driver(&cx8800_pci_driver);
2600 module_init(cx8800_init);
2601 module_exit(cx8800_fini);
2603 /* ----------------------------------------------------------- */