2 * device driver for Conexant 2388x based TV cards
3 * video4linux video interface
5 * (c) 2003-04 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 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/kmod.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <asm/div64.h>
33 #define V4L2_I2C_CLIENTS 1
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_L
,
199 .cxiformat
= VideoFormatSECAM
,
200 .cxoformat
= 0x181f0008,
203 .id
= V4L2_STD_SECAM_DK
,
204 .cxiformat
= VideoFormatSECAM
,
205 .cxoformat
= 0x181f0008,
209 static struct cx8800_fmt formats
[] = {
211 .name
= "8 bpp, gray",
212 .fourcc
= V4L2_PIX_FMT_GREY
,
213 .cxformat
= ColorFormatY8
,
215 .flags
= FORMAT_FLAGS_PACKED
,
217 .name
= "15 bpp RGB, le",
218 .fourcc
= V4L2_PIX_FMT_RGB555
,
219 .cxformat
= ColorFormatRGB15
,
221 .flags
= FORMAT_FLAGS_PACKED
,
223 .name
= "15 bpp RGB, be",
224 .fourcc
= V4L2_PIX_FMT_RGB555X
,
225 .cxformat
= ColorFormatRGB15
| ColorFormatBSWAP
,
227 .flags
= FORMAT_FLAGS_PACKED
,
229 .name
= "16 bpp RGB, le",
230 .fourcc
= V4L2_PIX_FMT_RGB565
,
231 .cxformat
= ColorFormatRGB16
,
233 .flags
= FORMAT_FLAGS_PACKED
,
235 .name
= "16 bpp RGB, be",
236 .fourcc
= V4L2_PIX_FMT_RGB565X
,
237 .cxformat
= ColorFormatRGB16
| ColorFormatBSWAP
,
239 .flags
= FORMAT_FLAGS_PACKED
,
241 .name
= "24 bpp RGB, le",
242 .fourcc
= V4L2_PIX_FMT_BGR24
,
243 .cxformat
= ColorFormatRGB24
,
245 .flags
= FORMAT_FLAGS_PACKED
,
247 .name
= "32 bpp RGB, le",
248 .fourcc
= V4L2_PIX_FMT_BGR32
,
249 .cxformat
= ColorFormatRGB32
,
251 .flags
= FORMAT_FLAGS_PACKED
,
253 .name
= "32 bpp RGB, be",
254 .fourcc
= V4L2_PIX_FMT_RGB32
,
255 .cxformat
= ColorFormatRGB32
| ColorFormatBSWAP
| ColorFormatWSWAP
,
257 .flags
= FORMAT_FLAGS_PACKED
,
259 .name
= "4:2:2, packed, YUYV",
260 .fourcc
= V4L2_PIX_FMT_YUYV
,
261 .cxformat
= ColorFormatYUY2
,
263 .flags
= FORMAT_FLAGS_PACKED
,
265 .name
= "4:2:2, packed, UYVY",
266 .fourcc
= V4L2_PIX_FMT_UYVY
,
267 .cxformat
= ColorFormatYUY2
| ColorFormatBSWAP
,
269 .flags
= FORMAT_FLAGS_PACKED
,
273 static struct cx8800_fmt
* format_by_fourcc(unsigned int fourcc
)
277 for (i
= 0; i
< ARRAY_SIZE(formats
); i
++)
278 if (formats
[i
].fourcc
== fourcc
)
283 /* ------------------------------------------------------------------- */
285 static const struct v4l2_queryctrl no_ctl
= {
287 .flags
= V4L2_CTRL_FLAG_DISABLED
,
290 static struct cx88_ctrl cx8800_ctls
[] = {
294 .id
= V4L2_CID_BRIGHTNESS
,
295 .name
= "Brightness",
300 .type
= V4L2_CTRL_TYPE_INTEGER
,
303 .reg
= MO_CONTR_BRIGHT
,
308 .id
= V4L2_CID_CONTRAST
,
314 .type
= V4L2_CTRL_TYPE_INTEGER
,
316 .reg
= MO_CONTR_BRIGHT
,
327 .type
= V4L2_CTRL_TYPE_INTEGER
,
334 /* strictly, this only describes only U saturation.
335 * V saturation is handled specially through code.
338 .id
= V4L2_CID_SATURATION
,
339 .name
= "Saturation",
344 .type
= V4L2_CTRL_TYPE_INTEGER
,
347 .reg
= MO_UV_SATURATION
,
353 .id
= V4L2_CID_AUDIO_MUTE
,
357 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
360 .sreg
= SHADOW_AUD_VOL_CTL
,
365 .id
= V4L2_CID_AUDIO_VOLUME
,
371 .type
= V4L2_CTRL_TYPE_INTEGER
,
374 .sreg
= SHADOW_AUD_VOL_CTL
,
379 .id
= V4L2_CID_AUDIO_BALANCE
,
384 .default_value
= 0x40,
385 .type
= V4L2_CTRL_TYPE_INTEGER
,
388 .sreg
= SHADOW_AUD_BAL_CTL
,
393 const int CX8800_CTLS
= ARRAY_SIZE(cx8800_ctls
);
395 /* ------------------------------------------------------------------- */
396 /* resource management */
398 static int res_get(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
, unsigned int bit
)
400 if (fh
->resources
& bit
)
401 /* have it already allocated */
406 if (dev
->resources
& bit
) {
407 /* no, someone else uses it */
411 /* it's free, grab it */
412 fh
->resources
|= bit
;
413 dev
->resources
|= bit
;
414 dprintk(1,"res: get %d\n",bit
);
420 int res_check(struct cx8800_fh
*fh
, unsigned int bit
)
422 return (fh
->resources
& bit
);
426 int res_locked(struct cx8800_dev
*dev
, unsigned int bit
)
428 return (dev
->resources
& bit
);
432 void res_free(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
, unsigned int bits
)
434 if ((fh
->resources
& bits
) != bits
)
438 fh
->resources
&= ~bits
;
439 dev
->resources
&= ~bits
;
440 dprintk(1,"res: put %d\n",bits
);
444 /* ------------------------------------------------------------------ */
446 static const u32 xtal
= 28636363;
448 static int set_pll(struct cx8800_dev
*dev
, int prescale
, u32 ofreq
)
450 static u32 pre
[] = { 0, 0, 0, 3, 2, 1 };
460 pll
= ofreq
* 8 * prescale
* (u64
)(1 << 20);
462 reg
= (pll
& 0x3ffffff) | (pre
[prescale
] << 26);
463 if (((reg
>> 20) & 0x3f) < 14) {
464 printk("%s: pll out of range\n",dev
->name
);
468 dprintk(1,"set_pll: MO_PLL_REG 0x%08x [old=0x%08x,freq=%d]\n",
469 reg
, cx_read(MO_PLL_REG
), ofreq
);
470 cx_write(MO_PLL_REG
, reg
);
471 for (i
= 0; i
< 10; i
++) {
472 reg
= cx_read(MO_DEVICE_STATUS
);
474 dprintk(1,"pll locked [pre=%d,ofreq=%d]\n",
478 dprintk(1,"pll not locked yet, waiting ...\n");
479 set_current_state(TASK_INTERRUPTIBLE
);
480 schedule_timeout(HZ
/10);
482 dprintk(1,"pll NOT locked [pre=%d,ofreq=%d]\n",prescale
,ofreq
);
486 static int set_tvaudio(struct cx8800_dev
*dev
)
488 if (CX88_VMUX_TELEVISION
!= INPUT(dev
->input
)->type
)
491 if (V4L2_STD_PAL_BG
& dev
->tvnorm
->id
) {
492 dev
->tvaudio
= nicam
? WW_NICAM_BGDKL
: WW_A2_BG
;
494 } else if (V4L2_STD_PAL_DK
& dev
->tvnorm
->id
) {
495 dev
->tvaudio
= nicam
? WW_NICAM_BGDKL
: WW_A2_DK
;
497 } else if (V4L2_STD_PAL_I
& dev
->tvnorm
->id
) {
498 dev
->tvaudio
= WW_NICAM_I
;
500 } else if (V4L2_STD_SECAM_L
& dev
->tvnorm
->id
) {
501 dev
->tvaudio
= WW_SYSTEM_L_AM
;
503 } else if (V4L2_STD_SECAM_DK
& dev
->tvnorm
->id
) {
504 dev
->tvaudio
= WW_A2_DK
;
506 } else if ((V4L2_STD_NTSC_M
& dev
->tvnorm
->id
) ||
507 (V4L2_STD_PAL_M
& dev
->tvnorm
->id
)) {
508 dev
->tvaudio
= WW_BTSC
;
510 } else if (V4L2_STD_NTSC_M_JP
& dev
->tvnorm
->id
) {
511 dev
->tvaudio
= WW_EIAJ
;
514 printk("%s: tvaudio support needs work for this tv norm [%s], sorry\n",
515 dev
->name
, dev
->tvnorm
->name
);
520 cx_andor(MO_AFECFG_IO
, 0x1f, 0x0);
521 cx88_set_tvaudio(dev
);
522 // cx88_set_stereo(dev,V4L2_TUNER_MODE_STEREO);
524 cx_write(MO_AUDD_LNGTH
, 128/8); /* fifo size */
525 cx_write(MO_AUDR_LNGTH
, 128/8); /* fifo size */
526 cx_write(MO_AUD_DMACNTRL
, 0x03); /* need audio fifo */
530 static int set_tvnorm(struct cx8800_dev
*dev
, struct cx8800_tvnorm
*norm
)
536 u32 bdelay
,agcdelay
,htotal
;
539 fsc8
= norm_fsc8(norm
);
543 dprintk(1,"set_tvnorm: \"%s\" fsc8=%d adc=%d vdec=%d\n",
544 norm
->name
, fsc8
, adc_clock
, vdec_clock
);
545 set_pll(dev
,2,vdec_clock
);
547 dprintk(1,"set_tvnorm: MO_INPUT_FORMAT 0x%08x [old=0x%08x]\n",
548 norm
->cxiformat
, cx_read(MO_INPUT_FORMAT
) & 0x0f);
549 cx_andor(MO_INPUT_FORMAT
, 0xf, norm
->cxiformat
);
552 // FIXME: as-is from DScaler
553 dprintk(1,"set_tvnorm: MO_OUTPUT_FORMAT 0x%08x [old=0x%08x]\n",
554 norm
->cxoformat
, cx_read(MO_OUTPUT_FORMAT
));
555 cx_write(MO_OUTPUT_FORMAT
, norm
->cxoformat
);
558 // MO_SCONV_REG = adc clock / video dec clock * 2^17
559 tmp64
= adc_clock
* (u64
)(1 << 17);
560 do_div(tmp64
, vdec_clock
);
561 dprintk(1,"set_tvnorm: MO_SCONV_REG 0x%08x [old=0x%08x]\n",
562 (u32
)tmp64
, cx_read(MO_SCONV_REG
));
563 cx_write(MO_SCONV_REG
, (u32
)tmp64
);
565 // MO_SUB_STEP = 8 * fsc / video dec clock * 2^22
566 tmp64
= fsc8
* (u64
)(1 << 22);
567 do_div(tmp64
, vdec_clock
);
568 dprintk(1,"set_tvnorm: MO_SUB_STEP 0x%08x [old=0x%08x]\n",
569 (u32
)tmp64
, cx_read(MO_SUB_STEP
));
570 cx_write(MO_SUB_STEP
, (u32
)tmp64
);
572 // MO_SUB_STEP_DR = 8 * 4406250 / video dec clock * 2^22
573 tmp64
= 4406250 * 8 * (u64
)(1 << 22);
574 do_div(tmp64
, vdec_clock
);
575 dprintk(1,"set_tvnorm: MO_SUB_STEP_DR 0x%08x [old=0x%08x]\n",
576 (u32
)tmp64
, cx_read(MO_SUB_STEP_DR
));
577 cx_write(MO_SUB_STEP_DR
, (u32
)tmp64
);
580 bdelay
= vdec_clock
* 65 / 20000000 + 21;
581 agcdelay
= vdec_clock
* 68 / 20000000 + 15;
582 dprintk(1,"set_tvnorm: MO_AGC_BURST 0x%08x [old=0x%08x,bdelay=%d,agcdelay=%d]\n",
583 (bdelay
<< 8) | agcdelay
, cx_read(MO_AGC_BURST
), bdelay
, agcdelay
);
584 cx_write(MO_AGC_BURST
, (bdelay
<< 8) | agcdelay
);
587 tmp64
= norm_htotal(norm
) * (u64
)vdec_clock
;
589 htotal
= (u32
)tmp64
| (norm_notchfilter(norm
) << 11);
590 dprintk(1,"set_tvnorm: MO_HTOTAL 0x%08x [old=0x%08x,htotal=%d]\n",
591 htotal
, cx_read(MO_HTOTAL
), (u32
)tmp64
);
592 cx_write(MO_HTOTAL
, htotal
);
595 cx_write(MO_VBI_PACKET
, ((1 << 11) | /* (norm_vdelay(norm) << 11) | */
596 norm_vbipack(norm
)));
602 #ifdef V4L2_I2C_CLIENTS
603 cx8800_call_i2c_clients(dev
,VIDIOC_S_STD
,&norm
->id
);
606 struct video_channel c
;
607 memset(&c
,0,sizeof(c
));
608 c
.channel
= dev
->input
;
609 c
.norm
= VIDEO_MODE_PAL
;
610 if ((norm
->id
& (V4L2_STD_NTSC_M
|V4L2_STD_NTSC_M_JP
)))
611 c
.norm
= VIDEO_MODE_NTSC
;
612 if (norm
->id
& V4L2_STD_SECAM
)
613 c
.norm
= VIDEO_MODE_SECAM
;
614 cx8800_call_i2c_clients(dev
,VIDIOCSCHAN
,&c
);
622 static int set_scale(struct cx8800_dev
*dev
, unsigned int width
, unsigned int height
,
623 enum v4l2_field field
)
625 unsigned int swidth
= norm_swidth(dev
->tvnorm
);
626 unsigned int sheight
= norm_maxh(dev
->tvnorm
);
629 dprintk(1,"set_scale: %dx%d [%s%s,%s]\n", width
, height
,
630 V4L2_FIELD_HAS_TOP(field
) ? "T" : "",
631 V4L2_FIELD_HAS_BOTTOM(field
) ? "B" : "",
633 if (!V4L2_FIELD_HAS_BOTH(field
))
636 // recalc H delay and scale registers
637 value
= (width
* norm_hdelay(dev
->tvnorm
)) / swidth
;
639 cx_write(MO_HDELAY_EVEN
, value
);
640 cx_write(MO_HDELAY_ODD
, value
);
641 dprintk(1,"set_scale: hdelay 0x%04x\n", value
);
643 value
= (swidth
* 4096 / width
) - 4096;
644 cx_write(MO_HSCALE_EVEN
, value
);
645 cx_write(MO_HSCALE_ODD
, value
);
646 dprintk(1,"set_scale: hscale 0x%04x\n", value
);
648 cx_write(MO_HACTIVE_EVEN
, width
);
649 cx_write(MO_HACTIVE_ODD
, width
);
650 dprintk(1,"set_scale: hactive 0x%04x\n", width
);
652 // recalc V scale Register (delay is constant)
653 cx_write(MO_VDELAY_EVEN
, norm_vdelay(dev
->tvnorm
));
654 cx_write(MO_VDELAY_ODD
, norm_vdelay(dev
->tvnorm
));
655 dprintk(1,"set_scale: vdelay 0x%04x\n", norm_vdelay(dev
->tvnorm
));
657 value
= (0x10000 - (sheight
* 512 / height
- 512)) & 0x1fff;
658 cx_write(MO_VSCALE_EVEN
, value
);
659 cx_write(MO_VSCALE_ODD
, value
);
660 dprintk(1,"set_scale: vscale 0x%04x\n", value
);
662 cx_write(MO_VACTIVE_EVEN
, sheight
);
663 cx_write(MO_VACTIVE_ODD
, sheight
);
664 dprintk(1,"set_scale: vactive 0x%04x\n", sheight
);
668 value
|= (1 << 19); // CFILT (default)
669 if (V4L2_FIELD_INTERLACED
== field
)
670 value
|= (1 << 3); // VINT (interlaced vertical scaling)
672 value
|= (1 << 0); // 3-tap interpolation
674 value
|= (1 << 1); // 5-tap interpolation
676 cx_write(MO_FILTER_EVEN
, value
);
677 cx_write(MO_FILTER_ODD
, value
);
678 dprintk(1,"set_scale: filter 0x%04x\n", value
);
683 static int video_mux(struct cx8800_dev
*dev
, unsigned int input
)
685 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
686 input
, INPUT(input
)->vmux
,
687 INPUT(input
)->gpio0
,INPUT(input
)->gpio1
,
688 INPUT(input
)->gpio2
,INPUT(input
)->gpio3
);
690 cx_andor(MO_INPUT_FORMAT
, 0x03 << 14, INPUT(input
)->vmux
<< 14);
691 cx_write(MO_GP0_IO
, INPUT(input
)->gpio0
);
692 cx_write(MO_GP1_IO
, INPUT(input
)->gpio1
);
693 cx_write(MO_GP2_IO
, INPUT(input
)->gpio2
);
694 cx_write(MO_GP3_IO
, INPUT(input
)->gpio3
);
696 switch (INPUT(input
)->type
) {
697 case CX88_VMUX_SVIDEO
:
698 cx_set(MO_AFECFG_IO
, 0x00000001);
699 cx_set(MO_INPUT_FORMAT
, 0x00010010);
702 cx_clear(MO_AFECFG_IO
, 0x00000001);
703 cx_clear(MO_INPUT_FORMAT
, 0x00010010);
709 /* ------------------------------------------------------------------ */
711 static int start_video_dma(struct cx8800_dev
*dev
,
712 struct cx88_dmaqueue
*q
,
713 struct cx88_buffer
*buf
)
715 /* setup fifo + format */
716 cx88_sram_channel_setup(dev
, &cx88_sram_channels
[SRAM_CH21
],
717 buf
->bpl
, buf
->risc
.dma
);
718 set_scale(dev
, buf
->vb
.width
, buf
->vb
.height
, buf
->vb
.field
);
719 cx_write(MO_COLOR_CTRL
, buf
->fmt
->cxformat
| ColorFormatGamma
);
722 cx_write(MO_VIDY_GPCNTRL
,0x3);
726 cx_set(MO_PCI_INTMSK
, 0x00fc01);
727 cx_set(MO_VID_INTMSK
, 0x0f0011);
730 cx_set(VID_CAPTURE_CONTROL
,0x06);
733 cx_set(MO_DEV_CNTRL2
, (1<<5));
734 cx_set(MO_VID_DMACNTRL
, 0x11);
739 static int restart_video_queue(struct cx8800_dev
*dev
,
740 struct cx88_dmaqueue
*q
)
742 struct cx88_buffer
*buf
, *prev
;
743 struct list_head
*item
;
745 if (!list_empty(&q
->active
)) {
746 buf
= list_entry(q
->active
.next
, struct cx88_buffer
, vb
.queue
);
747 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
749 start_video_dma(dev
, q
, buf
);
750 list_for_each(item
,&q
->active
) {
751 buf
= list_entry(item
, struct cx88_buffer
, vb
.queue
);
752 buf
->count
= q
->count
++;
754 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
760 if (list_empty(&q
->queued
))
762 buf
= list_entry(q
->queued
.next
, struct cx88_buffer
, vb
.queue
);
764 list_del(&buf
->vb
.queue
);
765 list_add_tail(&buf
->vb
.queue
,&q
->active
);
766 start_video_dma(dev
, q
, buf
);
767 buf
->vb
.state
= STATE_ACTIVE
;
768 buf
->count
= q
->count
++;
769 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
770 dprintk(2,"[%p/%d] restart_queue - first active\n",
773 } else if (prev
->vb
.width
== buf
->vb
.width
&&
774 prev
->vb
.height
== buf
->vb
.height
&&
775 prev
->fmt
== buf
->fmt
) {
776 list_del(&buf
->vb
.queue
);
777 list_add_tail(&buf
->vb
.queue
,&q
->active
);
778 buf
->vb
.state
= STATE_ACTIVE
;
779 buf
->count
= q
->count
++;
780 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
781 dprintk(2,"[%p/%d] restart_queue - move to active\n",
790 /* ------------------------------------------------------------------ */
793 buffer_setup(struct file
*file
, unsigned int *count
, unsigned int *size
)
795 struct cx8800_fh
*fh
= file
->private_data
;
797 *size
= fh
->fmt
->depth
*fh
->width
*fh
->height
>> 3;
800 while (*size
* *count
> vid_limit
* 1024 * 1024)
806 buffer_prepare(struct file
*file
, struct videobuf_buffer
*vb
,
807 enum v4l2_field field
)
809 struct cx8800_fh
*fh
= file
->private_data
;
810 struct cx8800_dev
*dev
= fh
->dev
;
811 struct cx88_buffer
*buf
= (struct cx88_buffer
*)vb
;
812 int rc
, init_buffer
= 0;
814 BUG_ON(NULL
== fh
->fmt
);
815 if (fh
->width
< 48 || fh
->width
> norm_maxw(dev
->tvnorm
) ||
816 fh
->height
< 32 || fh
->height
> norm_maxh(dev
->tvnorm
))
818 buf
->vb
.size
= (fh
->width
* fh
->height
* fh
->fmt
->depth
) >> 3;
819 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
822 if (buf
->fmt
!= fh
->fmt
||
823 buf
->vb
.width
!= fh
->width
||
824 buf
->vb
.height
!= fh
->height
||
825 buf
->vb
.field
!= field
) {
827 buf
->vb
.width
= fh
->width
;
828 buf
->vb
.height
= fh
->height
;
829 buf
->vb
.field
= field
;
833 if (STATE_NEEDS_INIT
== buf
->vb
.state
) {
835 if (0 != (rc
= videobuf_iolock(dev
->pci
,&buf
->vb
,NULL
)))
840 buf
->bpl
= buf
->vb
.width
* buf
->fmt
->depth
>> 3;
841 switch (buf
->vb
.field
) {
843 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
844 buf
->vb
.dma
.sglist
, 0, UNSET
,
845 buf
->bpl
, 0, buf
->vb
.height
);
847 case V4L2_FIELD_BOTTOM
:
848 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
849 buf
->vb
.dma
.sglist
, UNSET
, 0,
850 buf
->bpl
, 0, buf
->vb
.height
);
852 case V4L2_FIELD_INTERLACED
:
853 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
854 buf
->vb
.dma
.sglist
, 0, buf
->bpl
,
856 buf
->vb
.height
>> 1);
858 case V4L2_FIELD_SEQ_TB
:
859 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
861 0, buf
->bpl
* (buf
->vb
.height
>> 1),
863 buf
->vb
.height
>> 1);
865 case V4L2_FIELD_SEQ_BT
:
866 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
868 buf
->bpl
* (buf
->vb
.height
>> 1), 0,
870 buf
->vb
.height
>> 1);
876 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
878 fh
->width
, fh
->height
, fh
->fmt
->depth
, fh
->fmt
->name
,
879 (unsigned long)buf
->risc
.dma
);
881 buf
->vb
.state
= STATE_PREPARED
;
885 cx88_free_buffer(dev
->pci
,buf
);
890 buffer_queue(struct file
*file
, struct videobuf_buffer
*vb
)
892 struct cx88_buffer
*buf
= (struct cx88_buffer
*)vb
;
893 struct cx88_buffer
*prev
;
894 struct cx8800_fh
*fh
= file
->private_data
;
895 struct cx8800_dev
*dev
= fh
->dev
;
896 struct cx88_dmaqueue
*q
= &dev
->vidq
;
898 /* add jump to stopper */
899 buf
->risc
.jmp
[0] = cpu_to_le32(RISC_JUMP
| RISC_IRQ1
| 0x10000);
900 buf
->risc
.jmp
[1] = cpu_to_le32(q
->stopper
.dma
);
902 if (!list_empty(&q
->queued
)) {
903 list_add_tail(&buf
->vb
.queue
,&q
->queued
);
904 buf
->vb
.state
= STATE_QUEUED
;
905 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
908 } else if (list_empty(&q
->active
)) {
909 list_add_tail(&buf
->vb
.queue
,&q
->active
);
910 start_video_dma(dev
, q
, buf
);
911 buf
->vb
.state
= STATE_ACTIVE
;
912 buf
->count
= q
->count
++;
913 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
914 dprintk(2,"[%p/%d] buffer_queue - first active\n",
918 prev
= list_entry(q
->active
.prev
, struct cx88_buffer
, vb
.queue
);
919 if (prev
->vb
.width
== buf
->vb
.width
&&
920 prev
->vb
.height
== buf
->vb
.height
&&
921 prev
->fmt
== buf
->fmt
) {
922 list_add_tail(&buf
->vb
.queue
,&q
->active
);
923 buf
->vb
.state
= STATE_ACTIVE
;
924 buf
->count
= q
->count
++;
925 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
926 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
930 list_add_tail(&buf
->vb
.queue
,&q
->queued
);
931 buf
->vb
.state
= STATE_QUEUED
;
932 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
938 static void buffer_release(struct file
*file
, struct videobuf_buffer
*vb
)
940 struct cx88_buffer
*buf
= (struct cx88_buffer
*)vb
;
941 struct cx8800_fh
*fh
= file
->private_data
;
943 cx88_free_buffer(fh
->dev
->pci
,buf
);
946 struct videobuf_queue_ops cx8800_video_qops
= {
947 .buf_setup
= buffer_setup
,
948 .buf_prepare
= buffer_prepare
,
949 .buf_queue
= buffer_queue
,
950 .buf_release
= buffer_release
,
953 /* ------------------------------------------------------------------ */
955 #if 0 /* overlay support not finished yet */
956 static u32
* ov_risc_field(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
,
957 u32
*rp
, struct btcx_skiplist
*skips
,
958 u32 sync_line
, int skip_even
, int skip_odd
)
960 int line
,maxy
,start
,end
,skip
,nskips
;
964 /* sync instruction */
965 *(rp
++) = cpu_to_le32(RISC_RESYNC
| sync_line
);
967 addr
= (unsigned long)dev
->fbuf
.base
;
968 addr
+= dev
->fbuf
.fmt
.bytesperline
* fh
->win
.w
.top
;
969 addr
+= (fh
->fmt
->depth
>> 3) * fh
->win
.w
.left
;
972 for (maxy
= -1, line
= 0; line
< fh
->win
.w
.height
;
973 line
++, addr
+= dev
->fbuf
.fmt
.bytesperline
) {
974 if ((line
%2) == 0 && skip_even
)
976 if ((line
%2) == 1 && skip_odd
)
979 /* calculate clipping */
981 btcx_calc_skips(line
, fh
->win
.w
.width
, &maxy
,
982 skips
, &nskips
, fh
->clips
, fh
->nclips
);
984 /* write out risc code */
985 for (start
= 0, skip
= 0; start
< fh
->win
.w
.width
; start
= end
) {
986 if (skip
>= nskips
) {
988 end
= fh
->win
.w
.width
;
989 } else if (start
< skips
[skip
].start
) {
991 end
= skips
[skip
].start
;
994 end
= skips
[skip
].end
;
997 if (RISC_WRITE
== ri
)
998 ra
= addr
+ (fh
->fmt
->depth
>>3)*start
;
1004 if (fh
->win
.w
.width
== end
)
1006 ri
|= (fh
->fmt
->depth
>>3) * (end
-start
);
1008 *(rp
++)=cpu_to_le32(ri
);
1010 *(rp
++)=cpu_to_le32(ra
);
1017 static int ov_risc_frame(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
,
1018 struct cx88_buffer
*buf
)
1020 struct btcx_skiplist
*skips
;
1021 u32 instructions
,fields
;
1025 /* skip list for window clipping */
1026 if (NULL
== (skips
= kmalloc(sizeof(*skips
) * fh
->nclips
,GFP_KERNEL
)))
1030 if (V4L2_FIELD_HAS_TOP(fh
->win
.field
))
1032 if (V4L2_FIELD_HAS_BOTTOM(fh
->win
.field
))
1035 /* estimate risc mem: worst case is (clip+1) * lines instructions
1036 + syncs + jump (all 2 dwords) */
1037 instructions
= (fh
->nclips
+1) * fh
->win
.w
.height
;
1038 instructions
+= 3 + 4;
1039 if ((rc
= btcx_riscmem_alloc(dev
->pci
,&buf
->risc
,instructions
*8)) < 0) {
1044 /* write risc instructions */
1046 switch (fh
->win
.field
) {
1047 case V4L2_FIELD_TOP
:
1048 rp
= ov_risc_field(dev
, fh
, rp
, skips
, 0, 0, 0);
1050 case V4L2_FIELD_BOTTOM
:
1051 rp
= ov_risc_field(dev
, fh
, rp
, skips
, 0x200, 0, 0);
1053 case V4L2_FIELD_INTERLACED
:
1054 rp
= ov_risc_field(dev
, fh
, rp
, skips
, 0, 0, 1);
1055 rp
= ov_risc_field(dev
, fh
, rp
, skips
, 0x200, 1, 0);
1061 /* save pointer to jmp instruction address */
1067 static int verify_window(struct cx8800_dev
*dev
, struct v4l2_window
*win
)
1069 enum v4l2_field field
;
1072 if (NULL
== dev
->fbuf
.base
)
1074 if (win
->w
.width
< 48 || win
->w
.height
< 32)
1076 if (win
->clipcount
> 2048)
1080 maxw
= norm_maxw(dev
->tvnorm
);
1081 maxh
= norm_maxh(dev
->tvnorm
);
1083 if (V4L2_FIELD_ANY
== field
) {
1084 field
= (win
->w
.height
> maxh
/2)
1085 ? V4L2_FIELD_INTERLACED
1089 case V4L2_FIELD_TOP
:
1090 case V4L2_FIELD_BOTTOM
:
1093 case V4L2_FIELD_INTERLACED
:
1100 if (win
->w
.width
> maxw
)
1101 win
->w
.width
= maxw
;
1102 if (win
->w
.height
> maxh
)
1103 win
->w
.height
= maxh
;
1107 static int setup_window(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
,
1108 struct v4l2_window
*win
)
1110 struct v4l2_clip
*clips
= NULL
;
1111 int n
,size
,retval
= 0;
1113 if (NULL
== fh
->fmt
)
1115 retval
= verify_window(dev
,win
);
1119 /* copy clips -- luckily v4l1 + v4l2 are binary
1120 compatible here ...*/
1122 size
= sizeof(*clips
)*(n
+4);
1123 clips
= kmalloc(size
,GFP_KERNEL
);
1127 if (copy_from_user(clips
,win
->clips
,sizeof(struct v4l2_clip
)*n
)) {
1133 /* clip against screen */
1134 if (NULL
!= dev
->fbuf
.base
)
1135 n
= btcx_screen_clips(dev
->fbuf
.fmt
.width
, dev
->fbuf
.fmt
.height
,
1137 btcx_sort_clips(clips
,n
);
1139 /* 4-byte alignments */
1140 switch (fh
->fmt
->depth
) {
1143 btcx_align(&win
->w
, clips
, n
, 3);
1146 btcx_align(&win
->w
, clips
, n
, 1);
1149 /* no alignment fixups needed */
1155 down(&fh
->vidq
.lock
);
1162 fh
->ov
.setup_ok
= 1;
1165 /* update overlay if needed */
1168 if (check_btres(fh
, RESOURCE_OVERLAY
)) {
1169 struct bttv_buffer
*new;
1171 new = videobuf_alloc(sizeof(*new));
1172 bttv_overlay_risc(btv
, &fh
->ov
, fh
->ovfmt
, new);
1173 retval
= bttv_switch_overlay(btv
,fh
,new);
1181 /* ------------------------------------------------------------------ */
1183 static struct videobuf_queue
* get_queue(struct cx8800_fh
*fh
)
1186 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1188 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1196 static int get_ressource(struct cx8800_fh
*fh
)
1199 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1200 return RESOURCE_VIDEO
;
1201 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1202 return RESOURCE_VBI
;
1209 static int video_open(struct inode
*inode
, struct file
*file
)
1211 int minor
= iminor(inode
);
1212 struct cx8800_dev
*h
,*dev
= NULL
;
1213 struct cx8800_fh
*fh
;
1214 struct list_head
*list
;
1215 enum v4l2_buf_type type
= 0;
1218 list_for_each(list
,&cx8800_devlist
) {
1219 h
= list_entry(list
, struct cx8800_dev
, devlist
);
1220 if (h
->video_dev
->minor
== minor
) {
1222 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1224 if (h
->vbi_dev
->minor
== minor
) {
1226 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
1229 h
->radio_dev
->minor
== minor
) {
1237 dprintk(1,"open minor=%d radio=%d type=%s\n",
1238 minor
,radio
,v4l2_type_names
[type
]);
1240 /* allocate + initialize per filehandle data */
1241 fh
= kmalloc(sizeof(*fh
),GFP_KERNEL
);
1244 memset(fh
,0,sizeof(*fh
));
1245 file
->private_data
= fh
;
1251 fh
->fmt
= format_by_fourcc(V4L2_PIX_FMT_BGR24
);
1253 videobuf_queue_init(&fh
->vidq
, &cx8800_video_qops
,
1254 dev
->pci
, &dev
->slock
,
1255 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
1256 V4L2_FIELD_INTERLACED
,
1257 sizeof(struct cx88_buffer
));
1258 videobuf_queue_init(&fh
->vbiq
, &cx8800_vbi_qops
,
1259 dev
->pci
, &dev
->slock
,
1260 V4L2_BUF_TYPE_VBI_CAPTURE
,
1262 sizeof(struct cx88_buffer
));
1263 init_MUTEX(&fh
->vidq
.lock
);
1264 init_MUTEX(&fh
->vbiq
.lock
);
1267 dprintk(1,"video_open: setting radio device\n");
1268 cx_write(MO_GP0_IO
, cx88_boards
[dev
->board
].radio
.gpio0
);
1269 cx_write(MO_GP1_IO
, cx88_boards
[dev
->board
].radio
.gpio1
);
1270 cx_write(MO_GP2_IO
, cx88_boards
[dev
->board
].radio
.gpio2
);
1271 cx_write(MO_GP3_IO
, cx88_boards
[dev
->board
].radio
.gpio3
);
1272 dev
->tvaudio
= WW_FM
;
1273 cx88_set_tvaudio(dev
);
1274 cx88_set_stereo(dev
,V4L2_TUNER_MODE_STEREO
);
1275 cx8800_call_i2c_clients(dev
,AUDC_SET_RADIO
,NULL
);
1282 video_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
1284 struct cx8800_fh
*fh
= file
->private_data
;
1287 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1288 if (res_locked(fh
->dev
,RESOURCE_VIDEO
))
1290 return videobuf_read_one(file
, &fh
->vidq
, data
, count
, ppos
);
1291 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1292 if (!res_get(fh
->dev
,fh
,RESOURCE_VBI
))
1294 return videobuf_read_stream(file
, &fh
->vbiq
, data
, count
, ppos
, 1);
1302 video_poll(struct file
*file
, struct poll_table_struct
*wait
)
1304 struct cx8800_fh
*fh
= file
->private_data
;
1306 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
)
1307 return videobuf_poll_stream(file
, &fh
->vbiq
, wait
);
1313 static int video_release(struct inode
*inode
, struct file
*file
)
1315 struct cx8800_fh
*fh
= file
->private_data
;
1316 struct cx8800_dev
*dev
= fh
->dev
;
1318 /* turn off overlay */
1319 if (res_check(fh
, RESOURCE_OVERLAY
)) {
1321 res_free(dev
,fh
,RESOURCE_OVERLAY
);
1324 /* stop video capture */
1325 if (res_check(fh
, RESOURCE_VIDEO
)) {
1326 videobuf_queue_cancel(file
,&fh
->vidq
);
1327 res_free(dev
,fh
,RESOURCE_VIDEO
);
1329 if (fh
->vidq
.read_buf
) {
1330 buffer_release(file
,fh
->vidq
.read_buf
);
1331 kfree(fh
->vidq
.read_buf
);
1334 /* stop vbi capture */
1335 if (res_check(fh
, RESOURCE_VBI
)) {
1336 if (fh
->vbiq
.streaming
)
1337 videobuf_streamoff(file
,&fh
->vbiq
);
1338 if (fh
->vbiq
.reading
)
1339 videobuf_read_stop(file
,&fh
->vbiq
);
1340 res_free(dev
,fh
,RESOURCE_VBI
);
1343 file
->private_data
= NULL
;
1349 video_mmap(struct file
*file
, struct vm_area_struct
* vma
)
1351 struct cx8800_fh
*fh
= file
->private_data
;
1353 return videobuf_mmap_mapper(vma
, get_queue(fh
));
1356 /* ------------------------------------------------------------------ */
1358 static int get_control(struct cx8800_dev
*dev
, struct v4l2_control
*ctl
)
1360 struct cx88_ctrl
*c
= NULL
;
1364 for (i
= 0; i
< CX8800_CTLS
; i
++)
1365 if (cx8800_ctls
[i
].v
.id
== ctl
->id
)
1366 c
= &cx8800_ctls
[i
];
1370 value
= c
->sreg
? cx_sread(c
->sreg
) : cx_read(c
->reg
);
1372 case V4L2_CID_AUDIO_BALANCE
:
1373 ctl
->value
= (value
& 0x40) ? (value
& 0x3f) : (0x40 - (value
& 0x3f));
1375 case V4L2_CID_AUDIO_VOLUME
:
1376 ctl
->value
= 0x3f - (value
& 0x3f);
1379 ctl
->value
= ((value
+ (c
->off
<< c
->shift
)) & c
->mask
) >> c
->shift
;
1385 static int set_control(struct cx8800_dev
*dev
, struct v4l2_control
*ctl
)
1387 struct cx88_ctrl
*c
= NULL
;
1392 for (i
= 0; i
< CX8800_CTLS
; i
++)
1393 if (cx8800_ctls
[i
].v
.id
== ctl
->id
)
1394 c
= &cx8800_ctls
[i
];
1398 if (ctl
->value
< c
->v
.minimum
)
1400 if (ctl
->value
> c
->v
.maximum
)
1403 case V4L2_CID_AUDIO_BALANCE
:
1404 value
= (ctl
->value
< 0x40) ? (0x40 - ctl
->value
) : ctl
->value
;
1406 case V4L2_CID_AUDIO_VOLUME
:
1407 value
= 0x3f - (ctl
->value
& 0x3f);
1409 case V4L2_CID_SATURATION
:
1410 /* special v_sat handling */
1411 v_sat_value
= ctl
->value
- (0x7f - 0x5a);
1412 if (v_sat_value
> 0xff)
1414 if (v_sat_value
< 0x00)
1416 cx_andor(MO_UV_SATURATION
, 0xff00, v_sat_value
<< 8);
1417 /* fall through to default route for u_sat */
1419 value
= ((ctl
->value
- c
->off
) << c
->shift
) & c
->mask
;
1422 dprintk(1,"set_control id=0x%X reg=0x%x val=0x%x%s\n",
1423 ctl
->id
, c
->reg
, value
, c
->sreg
? " [shadowed]" : "");
1425 cx_sandor(c
->sreg
, c
->reg
, c
->mask
, value
);
1427 cx_andor(c
->reg
, c
->mask
, value
);
1432 static void init_controls(struct cx8800_dev
*dev
)
1434 static struct v4l2_control mute
= {
1435 .id
= V4L2_CID_AUDIO_MUTE
,
1438 static struct v4l2_control volume
= {
1439 .id
= V4L2_CID_AUDIO_VOLUME
,
1443 set_control(dev
,&mute
);
1444 set_control(dev
,&volume
);
1447 /* ------------------------------------------------------------------ */
1449 static int cx8800_g_fmt(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
,
1450 struct v4l2_format
*f
)
1453 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1454 memset(&f
->fmt
.pix
,0,sizeof(f
->fmt
.pix
));
1455 f
->fmt
.pix
.width
= fh
->width
;
1456 f
->fmt
.pix
.height
= fh
->height
;
1457 f
->fmt
.pix
.field
= fh
->vidq
.field
;
1458 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
1459 f
->fmt
.pix
.bytesperline
=
1460 (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
1461 f
->fmt
.pix
.sizeimage
=
1462 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1464 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1465 cx8800_vbi_fmt(dev
, f
);
1472 static int cx8800_try_fmt(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
,
1473 struct v4l2_format
*f
)
1476 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1478 struct cx8800_fmt
*fmt
;
1479 enum v4l2_field field
;
1480 unsigned int maxw
, maxh
;
1482 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1486 field
= f
->fmt
.pix
.field
;
1487 maxw
= norm_maxw(dev
->tvnorm
);
1488 maxh
= norm_maxh(dev
->tvnorm
);
1490 if (V4L2_FIELD_ANY
== field
) {
1491 field
= (f
->fmt
.pix
.height
> maxh
/2)
1492 ? V4L2_FIELD_INTERLACED
1493 : V4L2_FIELD_BOTTOM
;
1497 case V4L2_FIELD_TOP
:
1498 case V4L2_FIELD_BOTTOM
:
1501 case V4L2_FIELD_INTERLACED
:
1507 f
->fmt
.pix
.field
= field
;
1508 if (f
->fmt
.pix
.height
< 32)
1509 f
->fmt
.pix
.height
= 32;
1510 if (f
->fmt
.pix
.height
> maxh
)
1511 f
->fmt
.pix
.height
= maxh
;
1512 if (f
->fmt
.pix
.width
< 48)
1513 f
->fmt
.pix
.width
= 48;
1514 if (f
->fmt
.pix
.width
> maxw
)
1515 f
->fmt
.pix
.width
= maxw
;
1516 f
->fmt
.pix
.width
&= ~0x03;
1517 f
->fmt
.pix
.bytesperline
=
1518 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
1519 f
->fmt
.pix
.sizeimage
=
1520 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1524 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1525 cx8800_vbi_fmt(dev
, f
);
1532 static int cx8800_s_fmt(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
,
1533 struct v4l2_format
*f
)
1538 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1539 err
= cx8800_try_fmt(dev
,fh
,f
);
1543 fh
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1544 fh
->width
= f
->fmt
.pix
.width
;
1545 fh
->height
= f
->fmt
.pix
.height
;
1546 fh
->vidq
.field
= f
->fmt
.pix
.field
;
1548 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1549 cx8800_vbi_fmt(dev
, f
);
1557 * This function is _not_ called directly, but from
1558 * video_generic_ioctl (and maybe others). userspace
1559 * copying is done already, arg is a kernel pointer.
1561 static int video_do_ioctl(struct inode
*inode
, struct file
*file
,
1562 unsigned int cmd
, void *arg
)
1564 struct cx8800_fh
*fh
= file
->private_data
;
1565 struct cx8800_dev
*dev
= fh
->dev
;
1567 unsigned long flags
;
1571 if (video_debug
> 1)
1572 cx88_print_ioctl(dev
->name
,cmd
);
1574 case VIDIOC_QUERYCAP
:
1576 struct v4l2_capability
*cap
= arg
;
1578 memset(cap
,0,sizeof(*cap
));
1579 strcpy(cap
->driver
, "cx8800");
1580 strlcpy(cap
->card
, cx88_boards
[dev
->board
].name
,
1582 sprintf(cap
->bus_info
,"PCI:%s",pci_name(dev
->pci
));
1583 cap
->version
= CX88_VERSION_CODE
;
1585 V4L2_CAP_VIDEO_CAPTURE
|
1586 V4L2_CAP_READWRITE
|
1587 V4L2_CAP_STREAMING
|
1588 V4L2_CAP_VBI_CAPTURE
|
1590 V4L2_CAP_VIDEO_OVERLAY
|
1593 if (UNSET
!= dev
->tuner_type
)
1594 cap
->capabilities
|= V4L2_CAP_TUNER
;
1599 /* ---------- tv norms ---------- */
1600 case VIDIOC_ENUMSTD
:
1602 struct v4l2_standard
*e
= arg
;
1606 if (i
>= ARRAY_SIZE(tvnorms
))
1608 err
= v4l2_video_std_construct(e
, tvnorms
[e
->index
].id
,
1609 tvnorms
[e
->index
].name
);
1617 v4l2_std_id
*id
= arg
;
1619 *id
= dev
->tvnorm
->id
;
1624 v4l2_std_id
*id
= arg
;
1627 for(i
= 0; i
< ARRAY_SIZE(tvnorms
); i
++)
1628 if (*id
& tvnorms
[i
].id
)
1630 if (i
== ARRAY_SIZE(tvnorms
))
1634 set_tvnorm(dev
,&tvnorms
[i
]);
1639 /* ------ input switching ---------- */
1640 case VIDIOC_ENUMINPUT
:
1642 static const char *iname
[] = {
1643 [ CX88_VMUX_COMPOSITE1
] = "Composite1",
1644 [ CX88_VMUX_COMPOSITE2
] = "Composite2",
1645 [ CX88_VMUX_COMPOSITE3
] = "Composite3",
1646 [ CX88_VMUX_COMPOSITE4
] = "Composite4",
1647 [ CX88_VMUX_TELEVISION
] = "Television",
1648 [ CX88_VMUX_SVIDEO
] = "S-Video",
1649 [ CX88_VMUX_DEBUG
] = "for debug only",
1651 struct v4l2_input
*i
= arg
;
1657 if (0 == INPUT(n
)->type
)
1659 memset(i
,0,sizeof(*i
));
1661 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1662 strcpy(i
->name
,iname
[INPUT(n
)->type
]);
1663 if (CX88_VMUX_TELEVISION
== INPUT(n
)->type
)
1664 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1665 for (n
= 0; n
< ARRAY_SIZE(tvnorms
); n
++)
1666 i
->std
|= tvnorms
[n
].id
;
1669 case VIDIOC_G_INPUT
:
1671 unsigned int *i
= arg
;
1676 case VIDIOC_S_INPUT
:
1678 unsigned int *i
= arg
;
1688 /* --- capture ioctls ---------------------------------------- */
1689 case VIDIOC_ENUM_FMT
:
1691 struct v4l2_fmtdesc
*f
= arg
;
1692 enum v4l2_buf_type type
;
1698 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1699 if (index
>= ARRAY_SIZE(formats
))
1701 memset(f
,0,sizeof(*f
));
1704 strlcpy(f
->description
,formats
[index
].name
,sizeof(f
->description
));
1705 f
->pixelformat
= formats
[index
].fourcc
;
1714 struct v4l2_format
*f
= arg
;
1715 return cx8800_g_fmt(dev
,fh
,f
);
1719 struct v4l2_format
*f
= arg
;
1720 return cx8800_s_fmt(dev
,fh
,f
);
1722 case VIDIOC_TRY_FMT
:
1724 struct v4l2_format
*f
= arg
;
1725 return cx8800_try_fmt(dev
,fh
,f
);
1728 /* --- controls ---------------------------------------------- */
1729 case VIDIOC_QUERYCTRL
:
1731 struct v4l2_queryctrl
*c
= arg
;
1734 if (c
->id
< V4L2_CID_BASE
||
1735 c
->id
>= V4L2_CID_LASTP1
)
1737 for (i
= 0; i
< CX8800_CTLS
; i
++)
1738 if (cx8800_ctls
[i
].v
.id
== c
->id
)
1740 if (i
== CX8800_CTLS
) {
1744 *c
= cx8800_ctls
[i
].v
;
1748 return get_control(dev
,arg
);
1750 return set_control(dev
,arg
);
1752 /* --- tuner ioctls ------------------------------------------ */
1753 case VIDIOC_G_TUNER
:
1755 struct v4l2_tuner
*t
= arg
;
1758 if (UNSET
== dev
->tuner_type
)
1763 memset(t
,0,sizeof(*t
));
1764 strcpy(t
->name
, "Television");
1765 t
->type
= V4L2_TUNER_ANALOG_TV
;
1766 t
->capability
= V4L2_TUNER_CAP_NORM
;
1767 t
->rangehigh
= 0xffffffffUL
;
1769 cx88_get_stereo(dev
,t
);
1770 reg
= cx_read(MO_DEVICE_STATUS
);
1771 t
->signal
= (reg
& (1<<5)) ? 0xffff : 0x0000;
1774 case VIDIOC_S_TUNER
:
1776 struct v4l2_tuner
*t
= arg
;
1778 if (UNSET
== dev
->tuner_type
)
1782 cx88_set_stereo(dev
,t
->audmode
);
1785 case VIDIOC_G_FREQUENCY
:
1787 struct v4l2_frequency
*f
= arg
;
1789 if (UNSET
== dev
->tuner_type
)
1793 memset(f
,0,sizeof(*f
));
1794 f
->type
= fh
->radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1795 f
->frequency
= dev
->freq
;
1798 case VIDIOC_S_FREQUENCY
:
1800 struct v4l2_frequency
*f
= arg
;
1802 if (UNSET
== dev
->tuner_type
)
1806 if (0 == fh
->radio
&& f
->type
!= V4L2_TUNER_ANALOG_TV
)
1808 if (1 == fh
->radio
&& f
->type
!= V4L2_TUNER_RADIO
)
1811 dev
->freq
= f
->frequency
;
1812 #ifdef V4L2_I2C_CLIENTS
1813 cx8800_call_i2c_clients(dev
,VIDIOC_S_FREQUENCY
,f
);
1815 cx8800_call_i2c_clients(dev
,VIDIOCSFREQ
,&dev
->freq
);
1821 /* --- streaming capture ------------------------------------- */
1824 struct video_mbuf
*mbuf
= arg
;
1825 struct videobuf_queue
*q
;
1826 struct v4l2_requestbuffers req
;
1830 memset(&req
,0,sizeof(req
));
1833 req
.memory
= V4L2_MEMORY_MMAP
;
1834 err
= videobuf_reqbufs(file
,q
,&req
);
1837 memset(mbuf
,0,sizeof(*mbuf
));
1838 mbuf
->frames
= req
.count
;
1840 for (i
= 0; i
< mbuf
->frames
; i
++) {
1841 mbuf
->offsets
[i
] = q
->bufs
[i
]->boff
;
1842 mbuf
->size
+= q
->bufs
[i
]->bsize
;
1846 case VIDIOC_REQBUFS
:
1847 return videobuf_reqbufs(file
, get_queue(fh
), arg
);
1849 case VIDIOC_QUERYBUF
:
1850 return videobuf_querybuf(get_queue(fh
), arg
);
1853 return videobuf_qbuf(file
, get_queue(fh
), arg
);
1856 return videobuf_dqbuf(file
, get_queue(fh
), arg
);
1858 case VIDIOC_STREAMON
:
1860 int res
= get_ressource(fh
);
1862 if (!res_get(dev
,fh
,res
))
1864 return videobuf_streamon(file
, get_queue(fh
));
1866 case VIDIOC_STREAMOFF
:
1868 int res
= get_ressource(fh
);
1870 err
= videobuf_streamoff(file
, get_queue(fh
));
1873 res_free(dev
,fh
,res
);
1878 return v4l_compat_translate_ioctl(inode
,file
,cmd
,arg
,
1884 static int video_ioctl(struct inode
*inode
, struct file
*file
,
1885 unsigned int cmd
, unsigned long arg
)
1887 return video_usercopy(inode
, file
, cmd
, arg
, video_do_ioctl
);
1890 /* ----------------------------------------------------------- */
1892 static int radio_do_ioctl(struct inode
*inode
, struct file
*file
,
1893 unsigned int cmd
, void *arg
)
1895 struct cx8800_fh
*fh
= file
->private_data
;
1896 struct cx8800_dev
*dev
= fh
->dev
;
1898 if (video_debug
> 1)
1899 cx88_print_ioctl(dev
->name
,cmd
);
1902 case VIDIOC_QUERYCAP
:
1904 struct v4l2_capability
*cap
= arg
;
1906 memset(cap
,0,sizeof(*cap
));
1907 strcpy(cap
->driver
, "cx8800");
1908 strlcpy(cap
->card
, cx88_boards
[dev
->board
].name
,
1910 sprintf(cap
->bus_info
,"PCI:%s", pci_name(dev
->pci
));
1911 cap
->version
= CX88_VERSION_CODE
;
1912 cap
->capabilities
= V4L2_CAP_TUNER
;
1915 case VIDIOC_G_TUNER
:
1917 struct v4l2_tuner
*t
= arg
;
1922 memset(t
,0,sizeof(*t
));
1923 strcpy(t
->name
, "Radio");
1924 t
->rangelow
= (int)(65*16);
1925 t
->rangehigh
= (int)(108*16);
1927 #ifdef V4L2_I2C_CLIENTS
1928 cx8800_call_i2c_clients(dev
,VIDIOC_G_TUNER
,t
);
1931 struct video_tuner vt
;
1932 memset(&vt
,0,sizeof(vt
));
1933 cx8800_call_i2c_clients(dev
,VIDIOCGTUNER
,&vt
);
1934 t
->signal
= vt
.signal
;
1939 case VIDIOC_ENUMINPUT
:
1941 struct v4l2_input
*i
= arg
;
1945 strcpy(i
->name
,"Radio");
1946 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1949 case VIDIOC_G_INPUT
:
1955 case VIDIOC_G_AUDIO
:
1957 struct v4l2_audio
*a
= arg
;
1959 memset(a
,0,sizeof(*a
));
1960 strcpy(a
->name
,"Radio");
1965 v4l2_std_id
*id
= arg
;
1969 case VIDIOC_S_AUDIO
:
1970 case VIDIOC_S_TUNER
:
1971 case VIDIOC_S_INPUT
:
1975 case VIDIOC_QUERYCTRL
:
1977 struct v4l2_queryctrl
*c
= arg
;
1980 if (c
->id
< V4L2_CID_BASE
||
1981 c
->id
>= V4L2_CID_LASTP1
)
1983 if (c
->id
== V4L2_CID_AUDIO_MUTE
) {
1984 for (i
= 0; i
< CX8800_CTLS
; i
++)
1985 if (cx8800_ctls
[i
].v
.id
== c
->id
)
1987 *c
= cx8800_ctls
[i
].v
;
1996 case VIDIOC_G_FREQUENCY
:
1997 case VIDIOC_S_FREQUENCY
:
1998 return video_do_ioctl(inode
,file
,cmd
,arg
);
2001 return v4l_compat_translate_ioctl(inode
,file
,cmd
,arg
,
2007 static int radio_ioctl(struct inode
*inode
, struct file
*file
,
2008 unsigned int cmd
, unsigned long arg
)
2010 return video_usercopy(inode
, file
, cmd
, arg
, radio_do_ioctl
);
2013 /* ----------------------------------------------------------- */
2015 static void cx8800_vid_timeout(unsigned long data
)
2017 struct cx8800_dev
*dev
= (struct cx8800_dev
*)data
;
2018 struct cx88_dmaqueue
*q
= &dev
->vidq
;
2019 struct cx88_buffer
*buf
;
2020 unsigned long flags
;
2022 cx88_sram_channel_dump(dev
, &cx88_sram_channels
[SRAM_CH21
]);
2023 //cx88_risc_disasm(dev,&dev->vidq.stopper);
2025 cx_clear(MO_VID_DMACNTRL
, 0x11);
2026 cx_clear(VID_CAPTURE_CONTROL
, 0x06);
2028 spin_lock_irqsave(&dev
->slock
,flags
);
2029 while (!list_empty(&q
->active
)) {
2030 buf
= list_entry(q
->active
.next
, struct cx88_buffer
, vb
.queue
);
2031 list_del(&buf
->vb
.queue
);
2032 buf
->vb
.state
= STATE_ERROR
;
2033 wake_up(&buf
->vb
.done
);
2034 printk("%s: [%p/%d] timeout - dma=0x%08lx\n", dev
->name
,
2035 buf
, buf
->vb
.i
, (unsigned long)buf
->risc
.dma
);
2037 restart_video_queue(dev
,q
);
2038 spin_unlock_irqrestore(&dev
->slock
,flags
);
2041 static void cx8800_wakeup(struct cx8800_dev
*dev
,
2042 struct cx88_dmaqueue
*q
, u32 count
)
2044 struct cx88_buffer
*buf
;
2047 if (list_empty(&q
->active
))
2049 buf
= list_entry(q
->active
.next
,
2050 struct cx88_buffer
, vb
.queue
);
2051 if (buf
->count
> count
)
2053 do_gettimeofday(&buf
->vb
.ts
);
2054 dprintk(2,"[%p/%d] wakeup reg=%d buf=%d\n",buf
,buf
->vb
.i
,
2056 buf
->vb
.state
= STATE_DONE
;
2057 list_del(&buf
->vb
.queue
);
2058 wake_up(&buf
->vb
.done
);
2060 if (list_empty(&q
->active
)) {
2061 del_timer(&q
->timeout
);
2063 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
2067 static void cx8800_vid_irq(struct cx8800_dev
*dev
)
2069 u32 status
, mask
, count
;
2071 status
= cx_read(MO_VID_INTSTAT
);
2072 mask
= cx_read(MO_VID_INTMSK
);
2073 if (0 == (status
& mask
))
2075 cx_write(MO_VID_INTSTAT
, status
);
2076 if (irq_debug
|| (status
& mask
& ~0xff))
2077 cx88_print_irqbits(dev
->name
, "irq vid",
2078 cx88_vid_irqs
, status
, mask
);
2080 /* risc op code error */
2081 if (status
& (1 << 16)) {
2082 printk(KERN_WARNING
"%s: video risc op code error\n",dev
->name
);
2083 cx_clear(MO_VID_DMACNTRL
, 0x11);
2084 cx_clear(VID_CAPTURE_CONTROL
, 0x06);
2085 cx88_sram_channel_dump(dev
, &cx88_sram_channels
[SRAM_CH21
]);
2089 if (status
& 0x01) {
2090 spin_lock(&dev
->slock
);
2091 count
= cx_read(MO_VIDY_GPCNT
);
2092 cx8800_wakeup(dev
, &dev
->vidq
, count
);
2093 spin_unlock(&dev
->slock
);
2097 if (status
& 0x08) {
2098 spin_lock(&dev
->slock
);
2099 count
= cx_read(MO_VBI_GPCNT
);
2100 cx8800_wakeup(dev
, &dev
->vbiq
, count
);
2101 spin_unlock(&dev
->slock
);
2105 if (status
& 0x10) {
2106 dprintk(2,"stopper video\n");
2107 spin_lock(&dev
->slock
);
2108 restart_video_queue(dev
,&dev
->vidq
);
2109 spin_unlock(&dev
->slock
);
2113 if (status
& 0x80) {
2114 dprintk(2,"stopper vbi\n");
2115 spin_lock(&dev
->slock
);
2116 cx8800_restart_vbi_queue(dev
,&dev
->vbiq
);
2117 spin_unlock(&dev
->slock
);
2121 static irqreturn_t
cx8800_irq(int irq
, void *dev_id
, struct pt_regs
*regs
)
2123 struct cx8800_dev
*dev
= dev_id
;
2125 int loop
, handled
= 0;
2127 for (loop
= 0; loop
< 10; loop
++) {
2128 status
= cx_read(MO_PCI_INTSTAT
);
2129 mask
= cx_read(MO_PCI_INTMSK
);
2130 if (0 == (status
& mask
))
2133 cx_write(MO_PCI_INTSTAT
, status
);
2134 if (irq_debug
|| (status
& mask
& ~0x1f))
2135 cx88_print_irqbits(dev
->name
, "irq pci",
2136 cx88_pci_irqs
, status
, mask
);
2139 cx8800_vid_irq(dev
);
2142 printk(KERN_WARNING
"%s: irq loop -- clearing mask\n",
2144 cx_write(MO_PCI_INTMSK
,0);
2148 return IRQ_RETVAL(handled
);
2151 /* ----------------------------------------------------------- */
2152 /* exported stuff */
2154 static struct file_operations video_fops
=
2156 .owner
= THIS_MODULE
,
2158 .release
= video_release
,
2162 .ioctl
= video_ioctl
,
2163 .llseek
= no_llseek
,
2166 struct video_device cx8800_video_template
=
2168 .name
= "cx8800-video",
2169 .type
= VID_TYPE_CAPTURE
|VID_TYPE_TUNER
|VID_TYPE_SCALES
,
2171 .fops
= &video_fops
,
2175 struct video_device cx8800_vbi_template
=
2177 .name
= "cx8800-vbi",
2178 .type
= VID_TYPE_TELETEXT
|VID_TYPE_TUNER
,
2180 .fops
= &video_fops
,
2184 static struct file_operations radio_fops
=
2186 .owner
= THIS_MODULE
,
2188 .release
= video_release
,
2189 .ioctl
= radio_ioctl
,
2190 .llseek
= no_llseek
,
2193 struct video_device cx8800_radio_template
=
2195 .name
= "cx8800-radio",
2196 .type
= VID_TYPE_TUNER
,
2198 .fops
= &radio_fops
,
2202 /* ----------------------------------------------------------- */
2204 static void cx8800_shutdown(struct cx8800_dev
*dev
)
2206 /* disable RISC controller + IRQs */
2207 cx_write(MO_DEV_CNTRL2
, 0);
2209 /* stop dma transfers */
2210 cx_write(MO_VID_DMACNTRL
, 0x0);
2211 cx_write(MO_AUD_DMACNTRL
, 0x0);
2212 cx_write(MO_TS_DMACNTRL
, 0x0);
2213 cx_write(MO_VIP_DMACNTRL
, 0x0);
2214 cx_write(MO_GPHST_DMACNTRL
, 0x0);
2216 /* stop interupts */
2217 cx_write(MO_PCI_INTMSK
, 0x0);
2218 cx_write(MO_VID_INTMSK
, 0x0);
2219 cx_write(MO_AUD_INTMSK
, 0x0);
2220 cx_write(MO_TS_INTMSK
, 0x0);
2221 cx_write(MO_VIP_INTMSK
, 0x0);
2222 cx_write(MO_GPHST_INTMSK
, 0x0);
2224 /* stop capturing */
2225 cx_write(VID_CAPTURE_CONTROL
, 0);
2228 static int cx8800_reset(struct cx8800_dev
*dev
)
2230 dprintk(1,"cx8800_reset\n");
2232 cx8800_shutdown(dev
);
2234 /* clear irq status */
2235 cx_write(MO_VID_INTSTAT
, 0xFFFFFFFF); // Clear PIV int
2236 cx_write(MO_PCI_INTSTAT
, 0xFFFFFFFF); // Clear PCI int
2237 cx_write(MO_INT1_STAT
, 0xFFFFFFFF); // Clear RISC int
2240 set_current_state(TASK_INTERRUPTIBLE
);
2241 schedule_timeout(HZ
/10);
2244 cx88_sram_channel_setup(dev
, &cx88_sram_channels
[SRAM_CH21
], 720*4, 0);
2245 cx88_sram_channel_setup(dev
, &cx88_sram_channels
[SRAM_CH22
], 128, 0);
2246 cx88_sram_channel_setup(dev
, &cx88_sram_channels
[SRAM_CH23
], 128, 0);
2247 cx88_sram_channel_setup(dev
, &cx88_sram_channels
[SRAM_CH24
], 128, 0);
2248 cx88_sram_channel_setup(dev
, &cx88_sram_channels
[SRAM_CH25
], 128, 0);
2249 cx88_sram_channel_setup(dev
, &cx88_sram_channels
[SRAM_CH26
], 128, 0);
2252 cx_write(MO_INPUT_FORMAT
, ((1 << 13) | // agc enable
2253 (1 << 12) | // agc gain
2254 (1 << 11) | // adaptibe agc
2255 (0 << 10) | // chroma agc
2256 (0 << 9) | // ckillen
2259 /* setup image format */
2260 cx_andor(MO_COLOR_CTRL
, 0x4000, 0x4000);
2262 /* setup FIFO Threshholds */
2263 cx_write(MO_PDMA_STHRSH
, 0x0807);
2264 cx_write(MO_PDMA_DTHRSH
, 0x0807);
2266 /* fixes flashing of image */
2267 cx_write(MO_AGC_SYNC_TIP1
, 0x0380000F);
2268 cx_write(MO_AGC_BACK_VBI
, 0x00E00555);
2270 cx_write(MO_VID_INTSTAT
, 0xFFFFFFFF); // Clear PIV int
2271 cx_write(MO_PCI_INTSTAT
, 0xFFFFFFFF); // Clear PCI int
2272 cx_write(MO_INT1_STAT
, 0xFFFFFFFF); // Clear RISC int
2277 static struct video_device
*vdev_init(struct cx8800_dev
*dev
,
2278 struct video_device
*template,
2281 struct video_device
*vfd
;
2283 vfd
= video_device_alloc();
2288 vfd
->dev
= &dev
->pci
->dev
;
2289 vfd
->release
= video_device_release
;
2290 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s %s (%s)",
2291 dev
->name
, type
, cx88_boards
[dev
->board
].name
);
2295 static void cx8800_unregister_video(struct cx8800_dev
*dev
)
2297 if (dev
->radio_dev
) {
2298 if (-1 != dev
->radio_dev
->minor
)
2299 video_unregister_device(dev
->radio_dev
);
2301 video_device_release(dev
->radio_dev
);
2302 dev
->radio_dev
= NULL
;
2305 if (-1 != dev
->vbi_dev
->minor
)
2306 video_unregister_device(dev
->vbi_dev
);
2308 video_device_release(dev
->vbi_dev
);
2309 dev
->vbi_dev
= NULL
;
2311 if (dev
->video_dev
) {
2312 if (-1 != dev
->video_dev
->minor
)
2313 video_unregister_device(dev
->video_dev
);
2315 video_device_release(dev
->video_dev
);
2316 dev
->video_dev
= NULL
;
2320 static int __devinit
cx8800_initdev(struct pci_dev
*pci_dev
,
2321 const struct pci_device_id
*pci_id
)
2323 struct cx8800_dev
*dev
;
2327 dev
= kmalloc(sizeof(*dev
),GFP_KERNEL
);
2330 memset(dev
,0,sizeof(*dev
));
2334 if (pci_enable_device(pci_dev
)) {
2338 sprintf(dev
->name
,"cx%x[%d]",pci_dev
->device
,cx8800_devcount
);
2341 cx88_pci_quirks(dev
->name
, dev
->pci
, &latency
);
2342 if (UNSET
!= latency
) {
2343 printk(KERN_INFO
"%s: setting pci latency timer to %d\n",
2345 pci_write_config_byte(pci_dev
, PCI_LATENCY_TIMER
, latency
);
2348 /* print pci info */
2349 pci_read_config_byte(pci_dev
, PCI_CLASS_REVISION
, &dev
->pci_rev
);
2350 pci_read_config_byte(pci_dev
, PCI_LATENCY_TIMER
, &dev
->pci_lat
);
2351 printk(KERN_INFO
"%s: found at %s, rev: %d, irq: %d, "
2352 "latency: %d, mmio: 0x%lx\n", dev
->name
,
2353 pci_name(pci_dev
), dev
->pci_rev
, pci_dev
->irq
,
2354 dev
->pci_lat
,pci_resource_start(pci_dev
,0));
2356 pci_set_master(pci_dev
);
2357 if (!pci_dma_supported(pci_dev
,0xffffffff)) {
2358 printk("%s: Oops: no 32bit PCI DMA ???\n",dev
->name
);
2364 dev
->board
= card
[cx8800_devcount
];
2365 for (i
= 0; UNSET
== dev
->board
&& i
< cx88_idcount
; i
++)
2366 if (pci_dev
->subsystem_vendor
== cx88_subids
[i
].subvendor
&&
2367 pci_dev
->subsystem_device
== cx88_subids
[i
].subdevice
)
2368 dev
->board
= cx88_subids
[i
].card
;
2369 if (UNSET
== dev
->board
) {
2370 dev
->board
= CX88_BOARD_UNKNOWN
;
2371 cx88_card_list(dev
);
2373 printk(KERN_INFO
"%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
2374 dev
->name
,pci_dev
->subsystem_vendor
,
2375 pci_dev
->subsystem_device
,cx88_boards
[dev
->board
].name
,
2376 dev
->board
, card
[cx8800_devcount
] == dev
->board
?
2377 "insmod option" : "autodetected");
2379 dev
->tuner_type
= tuner
[cx8800_devcount
];
2380 if (UNSET
== dev
->tuner_type
)
2381 dev
->tuner_type
= cx88_boards
[dev
->board
].tuner_type
;
2384 if (!request_mem_region(pci_resource_start(pci_dev
,0),
2385 pci_resource_len(pci_dev
,0),
2388 printk(KERN_ERR
"%s: can't get MMIO memory @ 0x%lx\n",
2389 dev
->name
,pci_resource_start(pci_dev
,0));
2392 dev
->lmmio
= ioremap(pci_resource_start(pci_dev
,0),
2393 pci_resource_len(pci_dev
,0));
2394 dev
->bmmio
= (u8
*)dev
->lmmio
;
2396 /* initialize driver struct */
2397 init_MUTEX(&dev
->lock
);
2398 dev
->slock
= SPIN_LOCK_UNLOCKED
;
2399 dev
->tvnorm
= tvnorms
;
2401 /* init video dma queues */
2402 INIT_LIST_HEAD(&dev
->vidq
.active
);
2403 INIT_LIST_HEAD(&dev
->vidq
.queued
);
2404 dev
->vidq
.timeout
.function
= cx8800_vid_timeout
;
2405 dev
->vidq
.timeout
.data
= (unsigned long)dev
;
2406 init_timer(&dev
->vidq
.timeout
);
2407 cx88_risc_stopper(dev
->pci
,&dev
->vidq
.stopper
,
2408 MO_VID_DMACNTRL
,0x11,0x00);
2410 /* init vbi dma queues */
2411 INIT_LIST_HEAD(&dev
->vbiq
.active
);
2412 INIT_LIST_HEAD(&dev
->vbiq
.queued
);
2413 dev
->vbiq
.timeout
.function
= cx8800_vbi_timeout
;
2414 dev
->vbiq
.timeout
.data
= (unsigned long)dev
;
2415 init_timer(&dev
->vbiq
.timeout
);
2416 cx88_risc_stopper(dev
->pci
,&dev
->vbiq
.stopper
,
2417 MO_VID_DMACNTRL
,0x88,0x00);
2419 /* initialize hardware */
2423 err
= request_irq(pci_dev
->irq
, cx8800_irq
,
2424 SA_SHIRQ
| SA_INTERRUPT
, dev
->name
, dev
);
2426 printk(KERN_ERR
"%s: can't get IRQ %d\n",
2427 dev
->name
,pci_dev
->irq
);
2431 /* register i2c bus + load i2c helpers */
2432 cx8800_i2c_init(dev
);
2433 cx88_card_setup(dev
);
2435 /* load and configure helper modules */
2436 if (TUNER_ABSENT
!= dev
->tuner_type
)
2437 request_module("tuner");
2438 if (cx88_boards
[dev
->board
].needs_tda9887
)
2439 request_module("tda9887");
2440 if (dev
->tuner_type
!= UNSET
)
2441 cx8800_call_i2c_clients(dev
,TUNER_SET_TYPE
,&dev
->tuner_type
);
2443 /* register v4l devices */
2444 dev
->video_dev
= vdev_init(dev
,&cx8800_video_template
,"video");
2445 err
= video_register_device(dev
->video_dev
,VFL_TYPE_GRABBER
,
2446 video_nr
[cx8800_devcount
]);
2448 printk(KERN_INFO
"%s: can't register video device\n",
2452 printk(KERN_INFO
"%s: registered device video%d [v4l2]\n",
2453 dev
->name
,dev
->video_dev
->minor
& 0x1f);
2455 dev
->vbi_dev
= vdev_init(dev
,&cx8800_vbi_template
,"vbi");
2456 err
= video_register_device(dev
->vbi_dev
,VFL_TYPE_VBI
,
2457 vbi_nr
[cx8800_devcount
]);
2459 printk(KERN_INFO
"%s: can't register vbi device\n",
2463 printk(KERN_INFO
"%s: registered device vbi%d\n",
2464 dev
->name
,dev
->vbi_dev
->minor
& 0x1f);
2466 if (dev
->has_radio
) {
2467 dev
->radio_dev
= vdev_init(dev
,&cx8800_radio_template
,"radio");
2468 err
= video_register_device(dev
->radio_dev
,VFL_TYPE_RADIO
,
2469 radio_nr
[cx8800_devcount
]);
2471 printk(KERN_INFO
"%s: can't register radio device\n",
2475 printk(KERN_INFO
"%s: registered device radio%d\n",
2476 dev
->name
,dev
->radio_dev
->minor
& 0x1f);
2479 /* everything worked */
2480 list_add_tail(&dev
->devlist
,&cx8800_devlist
);
2481 pci_set_drvdata(pci_dev
,dev
);
2484 /* initial device configuration */
2487 set_tvnorm(dev
,tvnorms
);
2491 /* start tvaudio thread */
2492 init_completion(&dev
->texit
);
2493 dev
->tpid
= kernel_thread(cx88_audio_thread
, dev
, 0);
2497 cx8800_unregister_video(dev
);
2498 if (0 == dev
->i2c_rc
)
2499 i2c_bit_del_bus(&dev
->i2c_adap
);
2500 free_irq(pci_dev
->irq
, dev
);
2502 release_mem_region(pci_resource_start(pci_dev
,0),
2503 pci_resource_len(pci_dev
,0));
2509 static void __devexit
cx8800_finidev(struct pci_dev
*pci_dev
)
2511 struct cx8800_dev
*dev
= pci_get_drvdata(pci_dev
);
2516 wait_for_completion(&dev
->texit
);
2518 cx8800_shutdown(dev
);
2519 pci_disable_device(pci_dev
);
2521 /* unregister stuff */
2522 if (0 == dev
->i2c_rc
)
2523 i2c_bit_del_bus(&dev
->i2c_adap
);
2525 free_irq(pci_dev
->irq
, dev
);
2526 release_mem_region(pci_resource_start(pci_dev
,0),
2527 pci_resource_len(pci_dev
,0));
2529 cx8800_unregister_video(dev
);
2530 pci_set_drvdata(pci_dev
, NULL
);
2533 btcx_riscmem_free(dev
->pci
,&dev
->vidq
.stopper
);
2534 list_del(&dev
->devlist
);
2539 static int cx8800_suspend(struct pci_dev
*pci_dev
, u32 state
)
2541 struct cx8800_dev
*dev
= pci_get_drvdata(pci_dev
);
2543 printk("%s: suspend %d\n", dev
->name
, state
);
2545 cx8800_shutdown(dev
);
2546 del_timer(&dev
->vidq
.timeout
);
2548 pci_save_state(pci_dev
, dev
->state
.pci_cfg
);
2549 if (0 != pci_set_power_state(pci_dev
, state
)) {
2550 pci_disable_device(pci_dev
);
2551 dev
->state
.disabled
= 1;
2556 static int cx8800_resume(struct pci_dev
*pci_dev
)
2558 struct cx8800_dev
*dev
= pci_get_drvdata(pci_dev
);
2560 printk("%s: resume\n", dev
->name
);
2562 if (dev
->state
.disabled
) {
2563 pci_enable_device(pci_dev
);
2564 dev
->state
.disabled
= 0;
2566 pci_set_power_state(pci_dev
, 0);
2567 pci_restore_state(pci_dev
, dev
->state
.pci_cfg
);
2569 /* re-initialize hardware */
2572 /* restart video capture */
2573 spin_lock(&dev
->slock
);
2574 restart_video_queue(dev
,&dev
->vidq
);
2575 spin_unlock(&dev
->slock
);
2580 /* ----------------------------------------------------------- */
2582 struct pci_device_id cx8800_pci_tbl
[] = {
2586 .subvendor
= PCI_ANY_ID
,
2587 .subdevice
= PCI_ANY_ID
,
2589 /* --- end of list --- */
2592 MODULE_DEVICE_TABLE(pci
, cx8800_pci_tbl
);
2594 static struct pci_driver cx8800_pci_driver
= {
2596 .id_table
= cx8800_pci_tbl
,
2597 .probe
= cx8800_initdev
,
2598 .remove
= cx8800_finidev
,
2600 .suspend
= cx8800_suspend
,
2601 .resume
= cx8800_resume
,
2604 static int cx8800_init(void)
2606 INIT_LIST_HEAD(&cx8800_devlist
);
2607 printk(KERN_INFO
"cx2388x v4l2 driver version %d.%d.%d loaded\n",
2608 (CX88_VERSION_CODE
>> 16) & 0xff,
2609 (CX88_VERSION_CODE
>> 8) & 0xff,
2610 CX88_VERSION_CODE
& 0xff);
2612 printk(KERN_INFO
"cx2388x: snapshot date %04d-%02d-%02d\n",
2613 SNAPSHOT
/10000, (SNAPSHOT
/100)%100, SNAPSHOT
%100);
2615 return pci_module_init(&cx8800_pci_driver
);
2618 static void cx8800_fini(void)
2620 pci_unregister_driver(&cx8800_pci_driver
);
2623 module_init(cx8800_init
);
2624 module_exit(cx8800_fini
);
2626 /* ----------------------------------------------------------- */