3 * device driver for Conexant 2388x based TV cards
4 * video4linux video interface
6 * (c) 2003-04 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/kmod.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/interrupt.h>
31 #include <linux/delay.h>
32 #include <linux/kthread.h>
33 #include <asm/div64.h>
36 #include <media/v4l2-common.h>
38 /* Include V4L1 specific functions. Should be removed soon */
39 #include <linux/videodev.h>
41 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
42 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
43 MODULE_LICENSE("GPL");
45 /* ------------------------------------------------------------------ */
47 static unsigned int video_nr
[] = {[0 ... (CX88_MAXBOARDS
- 1)] = UNSET
};
48 static unsigned int vbi_nr
[] = {[0 ... (CX88_MAXBOARDS
- 1)] = UNSET
};
49 static unsigned int radio_nr
[] = {[0 ... (CX88_MAXBOARDS
- 1)] = UNSET
};
51 module_param_array(video_nr
, int, NULL
, 0444);
52 module_param_array(vbi_nr
, int, NULL
, 0444);
53 module_param_array(radio_nr
, int, NULL
, 0444);
55 MODULE_PARM_DESC(video_nr
,"video device numbers");
56 MODULE_PARM_DESC(vbi_nr
,"vbi device numbers");
57 MODULE_PARM_DESC(radio_nr
,"radio device numbers");
59 static unsigned int video_debug
= 0;
60 module_param(video_debug
,int,0644);
61 MODULE_PARM_DESC(video_debug
,"enable debug messages [video]");
63 static unsigned int irq_debug
= 0;
64 module_param(irq_debug
,int,0644);
65 MODULE_PARM_DESC(irq_debug
,"enable debug messages [IRQ handler]");
67 static unsigned int vid_limit
= 16;
68 module_param(vid_limit
,int,0644);
69 MODULE_PARM_DESC(vid_limit
,"capture memory limit in megabytes");
71 #define dprintk(level,fmt, arg...) if (video_debug >= level) \
72 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
74 /* ------------------------------------------------------------------ */
76 static LIST_HEAD(cx8800_devlist
);
78 /* ------------------------------------------------------------------- */
81 static struct cx88_tvnorm tvnorms
[] = {
84 .id
= V4L2_STD_NTSC_M
,
85 .cxiformat
= VideoFormatNTSC
,
86 .cxoformat
= 0x181f0008,
89 .id
= V4L2_STD_NTSC_M_JP
,
90 .cxiformat
= VideoFormatNTSCJapan
,
91 .cxoformat
= 0x181f0008,
94 .id
= V4L2_STD_PAL_BG
,
95 .cxiformat
= VideoFormatPAL
,
96 .cxoformat
= 0x181f0008,
99 .id
= V4L2_STD_PAL_DK
,
100 .cxiformat
= VideoFormatPAL
,
101 .cxoformat
= 0x181f0008,
104 .id
= V4L2_STD_PAL_I
,
105 .cxiformat
= VideoFormatPAL
,
106 .cxoformat
= 0x181f0008,
109 .id
= V4L2_STD_PAL_M
,
110 .cxiformat
= VideoFormatPALM
,
111 .cxoformat
= 0x1c1f0008,
114 .id
= V4L2_STD_PAL_N
,
115 .cxiformat
= VideoFormatPALN
,
116 .cxoformat
= 0x1c1f0008,
119 .id
= V4L2_STD_PAL_Nc
,
120 .cxiformat
= VideoFormatPALNC
,
121 .cxoformat
= 0x1c1f0008,
124 .id
= V4L2_STD_PAL_60
,
125 .cxiformat
= VideoFormatPAL60
,
126 .cxoformat
= 0x181f0008,
129 .id
= V4L2_STD_SECAM_L
,
130 .cxiformat
= VideoFormatSECAM
,
131 .cxoformat
= 0x181f0008,
134 .id
= V4L2_STD_SECAM_DK
,
135 .cxiformat
= VideoFormatSECAM
,
136 .cxoformat
= 0x181f0008,
140 static struct cx8800_fmt formats
[] = {
142 .name
= "8 bpp, gray",
143 .fourcc
= V4L2_PIX_FMT_GREY
,
144 .cxformat
= ColorFormatY8
,
146 .flags
= FORMAT_FLAGS_PACKED
,
148 .name
= "15 bpp RGB, le",
149 .fourcc
= V4L2_PIX_FMT_RGB555
,
150 .cxformat
= ColorFormatRGB15
,
152 .flags
= FORMAT_FLAGS_PACKED
,
154 .name
= "15 bpp RGB, be",
155 .fourcc
= V4L2_PIX_FMT_RGB555X
,
156 .cxformat
= ColorFormatRGB15
| ColorFormatBSWAP
,
158 .flags
= FORMAT_FLAGS_PACKED
,
160 .name
= "16 bpp RGB, le",
161 .fourcc
= V4L2_PIX_FMT_RGB565
,
162 .cxformat
= ColorFormatRGB16
,
164 .flags
= FORMAT_FLAGS_PACKED
,
166 .name
= "16 bpp RGB, be",
167 .fourcc
= V4L2_PIX_FMT_RGB565X
,
168 .cxformat
= ColorFormatRGB16
| ColorFormatBSWAP
,
170 .flags
= FORMAT_FLAGS_PACKED
,
172 .name
= "24 bpp RGB, le",
173 .fourcc
= V4L2_PIX_FMT_BGR24
,
174 .cxformat
= ColorFormatRGB24
,
176 .flags
= FORMAT_FLAGS_PACKED
,
178 .name
= "32 bpp RGB, le",
179 .fourcc
= V4L2_PIX_FMT_BGR32
,
180 .cxformat
= ColorFormatRGB32
,
182 .flags
= FORMAT_FLAGS_PACKED
,
184 .name
= "32 bpp RGB, be",
185 .fourcc
= V4L2_PIX_FMT_RGB32
,
186 .cxformat
= ColorFormatRGB32
| ColorFormatBSWAP
| ColorFormatWSWAP
,
188 .flags
= FORMAT_FLAGS_PACKED
,
190 .name
= "4:2:2, packed, YUYV",
191 .fourcc
= V4L2_PIX_FMT_YUYV
,
192 .cxformat
= ColorFormatYUY2
,
194 .flags
= FORMAT_FLAGS_PACKED
,
196 .name
= "4:2:2, packed, UYVY",
197 .fourcc
= V4L2_PIX_FMT_UYVY
,
198 .cxformat
= ColorFormatYUY2
| ColorFormatBSWAP
,
200 .flags
= FORMAT_FLAGS_PACKED
,
204 static struct cx8800_fmt
* format_by_fourcc(unsigned int fourcc
)
208 for (i
= 0; i
< ARRAY_SIZE(formats
); i
++)
209 if (formats
[i
].fourcc
== fourcc
)
214 /* ------------------------------------------------------------------- */
216 static const struct v4l2_queryctrl no_ctl
= {
218 .flags
= V4L2_CTRL_FLAG_DISABLED
,
221 static struct cx88_ctrl cx8800_ctls
[] = {
225 .id
= V4L2_CID_BRIGHTNESS
,
226 .name
= "Brightness",
231 .type
= V4L2_CTRL_TYPE_INTEGER
,
234 .reg
= MO_CONTR_BRIGHT
,
239 .id
= V4L2_CID_CONTRAST
,
244 .default_value
= 0x3f,
245 .type
= V4L2_CTRL_TYPE_INTEGER
,
248 .reg
= MO_CONTR_BRIGHT
,
259 .type
= V4L2_CTRL_TYPE_INTEGER
,
266 /* strictly, this only describes only U saturation.
267 * V saturation is handled specially through code.
270 .id
= V4L2_CID_SATURATION
,
271 .name
= "Saturation",
275 .default_value
= 0x7f,
276 .type
= V4L2_CTRL_TYPE_INTEGER
,
279 .reg
= MO_UV_SATURATION
,
285 .id
= V4L2_CID_AUDIO_MUTE
,
290 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
293 .sreg
= SHADOW_AUD_VOL_CTL
,
298 .id
= V4L2_CID_AUDIO_VOLUME
,
303 .default_value
= 0x1f,
304 .type
= V4L2_CTRL_TYPE_INTEGER
,
307 .sreg
= SHADOW_AUD_VOL_CTL
,
312 .id
= V4L2_CID_AUDIO_BALANCE
,
317 .default_value
= 0x40,
318 .type
= V4L2_CTRL_TYPE_INTEGER
,
321 .sreg
= SHADOW_AUD_BAL_CTL
,
326 static const int CX8800_CTLS
= ARRAY_SIZE(cx8800_ctls
);
328 /* ------------------------------------------------------------------- */
329 /* resource management */
331 static int res_get(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
, unsigned int bit
)
333 struct cx88_core
*core
= dev
->core
;
334 if (fh
->resources
& bit
)
335 /* have it already allocated */
340 if (dev
->resources
& bit
) {
341 /* no, someone else uses it */
345 /* it's free, grab it */
346 fh
->resources
|= bit
;
347 dev
->resources
|= bit
;
348 dprintk(1,"res: get %d\n",bit
);
354 int res_check(struct cx8800_fh
*fh
, unsigned int bit
)
356 return (fh
->resources
& bit
);
360 int res_locked(struct cx8800_dev
*dev
, unsigned int bit
)
362 return (dev
->resources
& bit
);
366 void res_free(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
, unsigned int bits
)
368 struct cx88_core
*core
= dev
->core
;
369 if ((fh
->resources
& bits
) != bits
)
373 fh
->resources
&= ~bits
;
374 dev
->resources
&= ~bits
;
375 dprintk(1,"res: put %d\n",bits
);
379 /* ------------------------------------------------------------------ */
381 /* static int video_mux(struct cx8800_dev *dev, unsigned int input) */
382 static int video_mux(struct cx88_core
*core
, unsigned int input
)
384 /* struct cx88_core *core = dev->core; */
386 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
387 input
, INPUT(input
)->vmux
,
388 INPUT(input
)->gpio0
,INPUT(input
)->gpio1
,
389 INPUT(input
)->gpio2
,INPUT(input
)->gpio3
);
391 cx_andor(MO_INPUT_FORMAT
, 0x03 << 14, INPUT(input
)->vmux
<< 14);
392 cx_write(MO_GP3_IO
, INPUT(input
)->gpio3
);
393 cx_write(MO_GP0_IO
, INPUT(input
)->gpio0
);
394 cx_write(MO_GP1_IO
, INPUT(input
)->gpio1
);
395 cx_write(MO_GP2_IO
, INPUT(input
)->gpio2
);
397 switch (INPUT(input
)->type
) {
398 case CX88_VMUX_SVIDEO
:
399 cx_set(MO_AFECFG_IO
, 0x00000001);
400 cx_set(MO_INPUT_FORMAT
, 0x00010010);
401 cx_set(MO_FILTER_EVEN
, 0x00002020);
402 cx_set(MO_FILTER_ODD
, 0x00002020);
405 cx_clear(MO_AFECFG_IO
, 0x00000001);
406 cx_clear(MO_INPUT_FORMAT
, 0x00010010);
407 cx_clear(MO_FILTER_EVEN
, 0x00002020);
408 cx_clear(MO_FILTER_ODD
, 0x00002020);
414 /* ------------------------------------------------------------------ */
416 static int start_video_dma(struct cx8800_dev
*dev
,
417 struct cx88_dmaqueue
*q
,
418 struct cx88_buffer
*buf
)
420 struct cx88_core
*core
= dev
->core
;
422 /* setup fifo + format */
423 cx88_sram_channel_setup(core
, &cx88_sram_channels
[SRAM_CH21
],
424 buf
->bpl
, buf
->risc
.dma
);
425 cx88_set_scale(core
, buf
->vb
.width
, buf
->vb
.height
, buf
->vb
.field
);
426 cx_write(MO_COLOR_CTRL
, buf
->fmt
->cxformat
| ColorFormatGamma
);
429 cx_write(MO_VIDY_GPCNTRL
,GP_COUNT_CONTROL_RESET
);
433 cx_set(MO_PCI_INTMSK
, core
->pci_irqmask
| 0x01);
435 /* Enables corresponding bits at PCI_INT_STAT:
436 bits 0 to 4: video, audio, transport stream, VIP, Host
438 bits 8 and 9: DMA complete for: SRC, DST
439 bits 10 and 11: BERR signal asserted for RISC: RD, WR
440 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
442 cx_set(MO_VID_INTMSK
, 0x0f0011);
445 cx_set(VID_CAPTURE_CONTROL
,0x06);
448 cx_set(MO_DEV_CNTRL2
, (1<<5));
449 cx_set(MO_VID_DMACNTRL
, 0x11); /* Planar Y and packed FIFO and RISC enable */
454 static int stop_video_dma(struct cx8800_dev
*dev
)
456 struct cx88_core
*core
= dev
->core
;
459 cx_clear(MO_VID_DMACNTRL
, 0x11);
461 /* disable capture */
462 cx_clear(VID_CAPTURE_CONTROL
,0x06);
465 cx_clear(MO_PCI_INTMSK
, 0x000001);
466 cx_clear(MO_VID_INTMSK
, 0x0f0011);
470 static int restart_video_queue(struct cx8800_dev
*dev
,
471 struct cx88_dmaqueue
*q
)
473 struct cx88_core
*core
= dev
->core
;
474 struct cx88_buffer
*buf
, *prev
;
475 struct list_head
*item
;
477 if (!list_empty(&q
->active
)) {
478 buf
= list_entry(q
->active
.next
, struct cx88_buffer
, vb
.queue
);
479 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
481 start_video_dma(dev
, q
, buf
);
482 list_for_each(item
,&q
->active
) {
483 buf
= list_entry(item
, struct cx88_buffer
, vb
.queue
);
484 buf
->count
= q
->count
++;
486 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
492 if (list_empty(&q
->queued
))
494 buf
= list_entry(q
->queued
.next
, struct cx88_buffer
, vb
.queue
);
496 list_del(&buf
->vb
.queue
);
497 list_add_tail(&buf
->vb
.queue
,&q
->active
);
498 start_video_dma(dev
, q
, buf
);
499 buf
->vb
.state
= STATE_ACTIVE
;
500 buf
->count
= q
->count
++;
501 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
502 dprintk(2,"[%p/%d] restart_queue - first active\n",
505 } else if (prev
->vb
.width
== buf
->vb
.width
&&
506 prev
->vb
.height
== buf
->vb
.height
&&
507 prev
->fmt
== buf
->fmt
) {
508 list_del(&buf
->vb
.queue
);
509 list_add_tail(&buf
->vb
.queue
,&q
->active
);
510 buf
->vb
.state
= STATE_ACTIVE
;
511 buf
->count
= q
->count
++;
512 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
513 dprintk(2,"[%p/%d] restart_queue - move to active\n",
522 /* ------------------------------------------------------------------ */
525 buffer_setup(struct videobuf_queue
*q
, unsigned int *count
, unsigned int *size
)
527 struct cx8800_fh
*fh
= q
->priv_data
;
529 *size
= fh
->fmt
->depth
*fh
->width
*fh
->height
>> 3;
532 while (*size
* *count
> vid_limit
* 1024 * 1024)
538 buffer_prepare(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
539 enum v4l2_field field
)
541 struct cx8800_fh
*fh
= q
->priv_data
;
542 struct cx8800_dev
*dev
= fh
->dev
;
543 struct cx88_core
*core
= dev
->core
;
544 struct cx88_buffer
*buf
= container_of(vb
,struct cx88_buffer
,vb
);
545 int rc
, init_buffer
= 0;
547 BUG_ON(NULL
== fh
->fmt
);
548 if (fh
->width
< 48 || fh
->width
> norm_maxw(core
->tvnorm
) ||
549 fh
->height
< 32 || fh
->height
> norm_maxh(core
->tvnorm
))
551 buf
->vb
.size
= (fh
->width
* fh
->height
* fh
->fmt
->depth
) >> 3;
552 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
555 if (buf
->fmt
!= fh
->fmt
||
556 buf
->vb
.width
!= fh
->width
||
557 buf
->vb
.height
!= fh
->height
||
558 buf
->vb
.field
!= field
) {
560 buf
->vb
.width
= fh
->width
;
561 buf
->vb
.height
= fh
->height
;
562 buf
->vb
.field
= field
;
566 if (STATE_NEEDS_INIT
== buf
->vb
.state
) {
568 if (0 != (rc
= videobuf_iolock(dev
->pci
,&buf
->vb
,NULL
)))
573 buf
->bpl
= buf
->vb
.width
* buf
->fmt
->depth
>> 3;
574 switch (buf
->vb
.field
) {
576 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
577 buf
->vb
.dma
.sglist
, 0, UNSET
,
578 buf
->bpl
, 0, buf
->vb
.height
);
580 case V4L2_FIELD_BOTTOM
:
581 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
582 buf
->vb
.dma
.sglist
, UNSET
, 0,
583 buf
->bpl
, 0, buf
->vb
.height
);
585 case V4L2_FIELD_INTERLACED
:
586 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
587 buf
->vb
.dma
.sglist
, 0, buf
->bpl
,
589 buf
->vb
.height
>> 1);
591 case V4L2_FIELD_SEQ_TB
:
592 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
594 0, buf
->bpl
* (buf
->vb
.height
>> 1),
596 buf
->vb
.height
>> 1);
598 case V4L2_FIELD_SEQ_BT
:
599 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
601 buf
->bpl
* (buf
->vb
.height
>> 1), 0,
603 buf
->vb
.height
>> 1);
609 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
611 fh
->width
, fh
->height
, fh
->fmt
->depth
, fh
->fmt
->name
,
612 (unsigned long)buf
->risc
.dma
);
614 buf
->vb
.state
= STATE_PREPARED
;
618 cx88_free_buffer(dev
->pci
,buf
);
623 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
625 struct cx88_buffer
*buf
= container_of(vb
,struct cx88_buffer
,vb
);
626 struct cx88_buffer
*prev
;
627 struct cx8800_fh
*fh
= vq
->priv_data
;
628 struct cx8800_dev
*dev
= fh
->dev
;
629 struct cx88_core
*core
= dev
->core
;
630 struct cx88_dmaqueue
*q
= &dev
->vidq
;
632 /* add jump to stopper */
633 buf
->risc
.jmp
[0] = cpu_to_le32(RISC_JUMP
| RISC_IRQ1
| RISC_CNT_INC
);
634 buf
->risc
.jmp
[1] = cpu_to_le32(q
->stopper
.dma
);
636 if (!list_empty(&q
->queued
)) {
637 list_add_tail(&buf
->vb
.queue
,&q
->queued
);
638 buf
->vb
.state
= STATE_QUEUED
;
639 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
642 } else if (list_empty(&q
->active
)) {
643 list_add_tail(&buf
->vb
.queue
,&q
->active
);
644 start_video_dma(dev
, q
, buf
);
645 buf
->vb
.state
= STATE_ACTIVE
;
646 buf
->count
= q
->count
++;
647 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
648 dprintk(2,"[%p/%d] buffer_queue - first active\n",
652 prev
= list_entry(q
->active
.prev
, struct cx88_buffer
, vb
.queue
);
653 if (prev
->vb
.width
== buf
->vb
.width
&&
654 prev
->vb
.height
== buf
->vb
.height
&&
655 prev
->fmt
== buf
->fmt
) {
656 list_add_tail(&buf
->vb
.queue
,&q
->active
);
657 buf
->vb
.state
= STATE_ACTIVE
;
658 buf
->count
= q
->count
++;
659 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
660 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
664 list_add_tail(&buf
->vb
.queue
,&q
->queued
);
665 buf
->vb
.state
= STATE_QUEUED
;
666 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
672 static void buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
674 struct cx88_buffer
*buf
= container_of(vb
,struct cx88_buffer
,vb
);
675 struct cx8800_fh
*fh
= q
->priv_data
;
677 cx88_free_buffer(fh
->dev
->pci
,buf
);
680 static struct videobuf_queue_ops cx8800_video_qops
= {
681 .buf_setup
= buffer_setup
,
682 .buf_prepare
= buffer_prepare
,
683 .buf_queue
= buffer_queue
,
684 .buf_release
= buffer_release
,
687 /* ------------------------------------------------------------------ */
690 /* ------------------------------------------------------------------ */
692 static struct videobuf_queue
* get_queue(struct cx8800_fh
*fh
)
695 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
697 case V4L2_BUF_TYPE_VBI_CAPTURE
:
705 static int get_ressource(struct cx8800_fh
*fh
)
708 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
709 return RESOURCE_VIDEO
;
710 case V4L2_BUF_TYPE_VBI_CAPTURE
:
718 static int video_open(struct inode
*inode
, struct file
*file
)
720 int minor
= iminor(inode
);
721 struct cx8800_dev
*h
,*dev
= NULL
;
722 struct cx88_core
*core
;
723 struct cx8800_fh
*fh
;
724 struct list_head
*list
;
725 enum v4l2_buf_type type
= 0;
728 list_for_each(list
,&cx8800_devlist
) {
729 h
= list_entry(list
, struct cx8800_dev
, devlist
);
730 if (h
->video_dev
->minor
== minor
) {
732 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
734 if (h
->vbi_dev
->minor
== minor
) {
736 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
739 h
->radio_dev
->minor
== minor
) {
749 dprintk(1,"open minor=%d radio=%d type=%s\n",
750 minor
,radio
,v4l2_type_names
[type
]);
752 /* allocate + initialize per filehandle data */
753 fh
= kzalloc(sizeof(*fh
),GFP_KERNEL
);
756 file
->private_data
= fh
;
762 fh
->fmt
= format_by_fourcc(V4L2_PIX_FMT_BGR24
);
764 videobuf_queue_init(&fh
->vidq
, &cx8800_video_qops
,
765 dev
->pci
, &dev
->slock
,
766 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
767 V4L2_FIELD_INTERLACED
,
768 sizeof(struct cx88_buffer
),
770 videobuf_queue_init(&fh
->vbiq
, &cx8800_vbi_qops
,
771 dev
->pci
, &dev
->slock
,
772 V4L2_BUF_TYPE_VBI_CAPTURE
,
774 sizeof(struct cx88_buffer
),
778 int board
= core
->board
;
779 dprintk(1,"video_open: setting radio device\n");
780 cx_write(MO_GP3_IO
, cx88_boards
[board
].radio
.gpio3
);
781 cx_write(MO_GP0_IO
, cx88_boards
[board
].radio
.gpio0
);
782 cx_write(MO_GP1_IO
, cx88_boards
[board
].radio
.gpio1
);
783 cx_write(MO_GP2_IO
, cx88_boards
[board
].radio
.gpio2
);
784 core
->tvaudio
= WW_FM
;
785 cx88_set_tvaudio(core
);
786 cx88_set_stereo(core
,V4L2_TUNER_MODE_STEREO
,1);
787 cx88_call_i2c_clients(core
,AUDC_SET_RADIO
,NULL
);
794 video_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
796 struct cx8800_fh
*fh
= file
->private_data
;
799 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
800 if (res_locked(fh
->dev
,RESOURCE_VIDEO
))
802 return videobuf_read_one(&fh
->vidq
, data
, count
, ppos
,
803 file
->f_flags
& O_NONBLOCK
);
804 case V4L2_BUF_TYPE_VBI_CAPTURE
:
805 if (!res_get(fh
->dev
,fh
,RESOURCE_VBI
))
807 return videobuf_read_stream(&fh
->vbiq
, data
, count
, ppos
, 1,
808 file
->f_flags
& O_NONBLOCK
);
816 video_poll(struct file
*file
, struct poll_table_struct
*wait
)
818 struct cx8800_fh
*fh
= file
->private_data
;
819 struct cx88_buffer
*buf
;
821 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
) {
822 if (!res_get(fh
->dev
,fh
,RESOURCE_VBI
))
824 return videobuf_poll_stream(file
, &fh
->vbiq
, wait
);
827 if (res_check(fh
,RESOURCE_VIDEO
)) {
828 /* streaming capture */
829 if (list_empty(&fh
->vidq
.stream
))
831 buf
= list_entry(fh
->vidq
.stream
.next
,struct cx88_buffer
,vb
.stream
);
834 buf
= (struct cx88_buffer
*)fh
->vidq
.read_buf
;
838 poll_wait(file
, &buf
->vb
.done
, wait
);
839 if (buf
->vb
.state
== STATE_DONE
||
840 buf
->vb
.state
== STATE_ERROR
)
841 return POLLIN
|POLLRDNORM
;
845 static int video_release(struct inode
*inode
, struct file
*file
)
847 struct cx8800_fh
*fh
= file
->private_data
;
848 struct cx8800_dev
*dev
= fh
->dev
;
850 /* turn off overlay */
851 if (res_check(fh
, RESOURCE_OVERLAY
)) {
853 res_free(dev
,fh
,RESOURCE_OVERLAY
);
856 /* stop video capture */
857 if (res_check(fh
, RESOURCE_VIDEO
)) {
858 videobuf_queue_cancel(&fh
->vidq
);
859 res_free(dev
,fh
,RESOURCE_VIDEO
);
861 if (fh
->vidq
.read_buf
) {
862 buffer_release(&fh
->vidq
,fh
->vidq
.read_buf
);
863 kfree(fh
->vidq
.read_buf
);
866 /* stop vbi capture */
867 if (res_check(fh
, RESOURCE_VBI
)) {
868 if (fh
->vbiq
.streaming
)
869 videobuf_streamoff(&fh
->vbiq
);
870 if (fh
->vbiq
.reading
)
871 videobuf_read_stop(&fh
->vbiq
);
872 res_free(dev
,fh
,RESOURCE_VBI
);
875 videobuf_mmap_free(&fh
->vidq
);
876 videobuf_mmap_free(&fh
->vbiq
);
877 file
->private_data
= NULL
;
880 cx88_call_i2c_clients (dev
->core
, TUNER_SET_STANDBY
, NULL
);
886 video_mmap(struct file
*file
, struct vm_area_struct
* vma
)
888 struct cx8800_fh
*fh
= file
->private_data
;
890 return videobuf_mmap_mapper(get_queue(fh
), vma
);
893 /* ------------------------------------------------------------------ */
895 /* static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
896 static int get_control(struct cx88_core
*core
, struct v4l2_control
*ctl
)
898 /* struct cx88_core *core = dev->core; */
899 struct cx88_ctrl
*c
= NULL
;
903 for (i
= 0; i
< CX8800_CTLS
; i
++)
904 if (cx8800_ctls
[i
].v
.id
== ctl
->id
)
909 value
= c
->sreg
? cx_sread(c
->sreg
) : cx_read(c
->reg
);
911 case V4L2_CID_AUDIO_BALANCE
:
912 ctl
->value
= (value
& 0x40) ? (value
& 0x3f) : (0x40 - (value
& 0x3f));
914 case V4L2_CID_AUDIO_VOLUME
:
915 ctl
->value
= 0x3f - (value
& 0x3f);
918 ctl
->value
= ((value
+ (c
->off
<< c
->shift
)) & c
->mask
) >> c
->shift
;
921 printk("get_control id=0x%X reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
922 ctl
->id
, c
->reg
, ctl
->value
,
923 c
->mask
, c
->sreg
? " [shadowed]" : "");
927 /* static int set_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
928 static int set_control(struct cx88_core
*core
, struct v4l2_control
*ctl
)
930 /* struct cx88_core *core = dev->core; */
931 struct cx88_ctrl
*c
= NULL
;
934 for (i
= 0; i
< CX8800_CTLS
; i
++) {
935 if (cx8800_ctls
[i
].v
.id
== ctl
->id
) {
942 if (ctl
->value
< c
->v
.minimum
)
943 ctl
->value
= c
->v
.minimum
;
944 if (ctl
->value
> c
->v
.maximum
)
945 ctl
->value
= c
->v
.maximum
;
948 case V4L2_CID_AUDIO_BALANCE
:
949 value
= (ctl
->value
< 0x40) ? (0x40 - ctl
->value
) : ctl
->value
;
951 case V4L2_CID_AUDIO_VOLUME
:
952 value
= 0x3f - (ctl
->value
& 0x3f);
954 case V4L2_CID_SATURATION
:
955 /* special v_sat handling */
957 value
= ((ctl
->value
- c
->off
) << c
->shift
) & c
->mask
;
959 if (core
->tvnorm
->id
& V4L2_STD_SECAM
) {
960 /* For SECAM, both U and V sat should be equal */
961 value
=value
<<8|value
;
963 /* Keeps U Saturation proportional to V Sat */
964 value
=(value
*0x5a)/0x7f<<8|value
;
969 value
= ((ctl
->value
- c
->off
) << c
->shift
) & c
->mask
;
972 printk("set_control id=0x%X reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
973 ctl
->id
, c
->reg
, value
,
974 mask
, c
->sreg
? " [shadowed]" : "");
976 cx_sandor(c
->sreg
, c
->reg
, mask
, value
);
978 cx_andor(c
->reg
, mask
, value
);
983 static void init_controls(struct cx88_core
*core
)
985 struct v4l2_control ctrl
;
988 for (i
= 0; i
< CX8800_CTLS
; i
++) {
989 ctrl
.id
=cx8800_ctls
[i
].v
.id
;
990 ctrl
.value
=cx8800_ctls
[i
].v
.default_value
992 set_control(core
, &ctrl
);
996 /* ------------------------------------------------------------------ */
998 static int cx8800_g_fmt(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
,
999 struct v4l2_format
*f
)
1002 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1003 memset(&f
->fmt
.pix
,0,sizeof(f
->fmt
.pix
));
1004 f
->fmt
.pix
.width
= fh
->width
;
1005 f
->fmt
.pix
.height
= fh
->height
;
1006 f
->fmt
.pix
.field
= fh
->vidq
.field
;
1007 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
1008 f
->fmt
.pix
.bytesperline
=
1009 (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
1010 f
->fmt
.pix
.sizeimage
=
1011 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1013 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1014 cx8800_vbi_fmt(dev
, f
);
1021 static int cx8800_try_fmt(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
,
1022 struct v4l2_format
*f
)
1024 struct cx88_core
*core
= dev
->core
;
1027 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1029 struct cx8800_fmt
*fmt
;
1030 enum v4l2_field field
;
1031 unsigned int maxw
, maxh
;
1033 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1037 field
= f
->fmt
.pix
.field
;
1038 maxw
= norm_maxw(core
->tvnorm
);
1039 maxh
= norm_maxh(core
->tvnorm
);
1041 if (V4L2_FIELD_ANY
== field
) {
1042 field
= (f
->fmt
.pix
.height
> maxh
/2)
1043 ? V4L2_FIELD_INTERLACED
1044 : V4L2_FIELD_BOTTOM
;
1048 case V4L2_FIELD_TOP
:
1049 case V4L2_FIELD_BOTTOM
:
1052 case V4L2_FIELD_INTERLACED
:
1058 f
->fmt
.pix
.field
= field
;
1059 if (f
->fmt
.pix
.height
< 32)
1060 f
->fmt
.pix
.height
= 32;
1061 if (f
->fmt
.pix
.height
> maxh
)
1062 f
->fmt
.pix
.height
= maxh
;
1063 if (f
->fmt
.pix
.width
< 48)
1064 f
->fmt
.pix
.width
= 48;
1065 if (f
->fmt
.pix
.width
> maxw
)
1066 f
->fmt
.pix
.width
= maxw
;
1067 f
->fmt
.pix
.width
&= ~0x03;
1068 f
->fmt
.pix
.bytesperline
=
1069 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
1070 f
->fmt
.pix
.sizeimage
=
1071 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1075 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1076 cx8800_vbi_fmt(dev
, f
);
1083 static int cx8800_s_fmt(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
,
1084 struct v4l2_format
*f
)
1089 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1090 err
= cx8800_try_fmt(dev
,fh
,f
);
1094 fh
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1095 fh
->width
= f
->fmt
.pix
.width
;
1096 fh
->height
= f
->fmt
.pix
.height
;
1097 fh
->vidq
.field
= f
->fmt
.pix
.field
;
1099 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1100 cx8800_vbi_fmt(dev
, f
);
1108 * This function is _not_ called directly, but from
1109 * video_generic_ioctl (and maybe others). userspace
1110 * copying is done already, arg is a kernel pointer.
1112 static int video_do_ioctl(struct inode
*inode
, struct file
*file
,
1113 unsigned int cmd
, void *arg
)
1115 struct cx8800_fh
*fh
= file
->private_data
;
1116 struct cx8800_dev
*dev
= fh
->dev
;
1117 struct cx88_core
*core
= dev
->core
;
1120 if (video_debug
> 1)
1121 v4l_print_ioctl(core
->name
,cmd
);
1124 /* --- capabilities ------------------------------------------ */
1125 case VIDIOC_QUERYCAP
:
1127 struct v4l2_capability
*cap
= arg
;
1129 memset(cap
,0,sizeof(*cap
));
1130 strcpy(cap
->driver
, "cx8800");
1131 strlcpy(cap
->card
, cx88_boards
[core
->board
].name
,
1133 sprintf(cap
->bus_info
,"PCI:%s",pci_name(dev
->pci
));
1134 cap
->version
= CX88_VERSION_CODE
;
1136 V4L2_CAP_VIDEO_CAPTURE
|
1137 V4L2_CAP_READWRITE
|
1138 V4L2_CAP_STREAMING
|
1139 V4L2_CAP_VBI_CAPTURE
|
1140 V4L2_CAP_VIDEO_OVERLAY
|
1142 if (UNSET
!= core
->tuner_type
)
1143 cap
->capabilities
|= V4L2_CAP_TUNER
;
1147 /* --- capture ioctls ---------------------------------------- */
1148 case VIDIOC_ENUM_FMT
:
1150 struct v4l2_fmtdesc
*f
= arg
;
1151 enum v4l2_buf_type type
;
1157 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1158 if (index
>= ARRAY_SIZE(formats
))
1160 memset(f
,0,sizeof(*f
));
1163 strlcpy(f
->description
,formats
[index
].name
,sizeof(f
->description
));
1164 f
->pixelformat
= formats
[index
].fourcc
;
1173 struct v4l2_format
*f
= arg
;
1174 return cx8800_g_fmt(dev
,fh
,f
);
1178 struct v4l2_format
*f
= arg
;
1179 return cx8800_s_fmt(dev
,fh
,f
);
1181 case VIDIOC_TRY_FMT
:
1183 struct v4l2_format
*f
= arg
;
1184 return cx8800_try_fmt(dev
,fh
,f
);
1187 /* --- streaming capture ------------------------------------- */
1190 struct video_mbuf
*mbuf
= arg
;
1191 struct videobuf_queue
*q
;
1192 struct v4l2_requestbuffers req
;
1196 memset(&req
,0,sizeof(req
));
1199 req
.memory
= V4L2_MEMORY_MMAP
;
1200 err
= videobuf_reqbufs(q
,&req
);
1203 memset(mbuf
,0,sizeof(*mbuf
));
1204 mbuf
->frames
= req
.count
;
1206 for (i
= 0; i
< mbuf
->frames
; i
++) {
1207 mbuf
->offsets
[i
] = q
->bufs
[i
]->boff
;
1208 mbuf
->size
+= q
->bufs
[i
]->bsize
;
1213 case VIDIOC_REQBUFS
:
1214 return videobuf_reqbufs(get_queue(fh
), arg
);
1216 case VIDIOC_QUERYBUF
:
1217 return videobuf_querybuf(get_queue(fh
), arg
);
1220 return videobuf_qbuf(get_queue(fh
), arg
);
1223 return videobuf_dqbuf(get_queue(fh
), arg
,
1224 file
->f_flags
& O_NONBLOCK
);
1226 case VIDIOC_STREAMON
:
1228 int res
= get_ressource(fh
);
1230 if (!res_get(dev
,fh
,res
))
1232 return videobuf_streamon(get_queue(fh
));
1234 case VIDIOC_STREAMOFF
:
1236 int res
= get_ressource(fh
);
1238 err
= videobuf_streamoff(get_queue(fh
));
1241 res_free(dev
,fh
,res
);
1245 return cx88_do_ioctl( inode
, file
, fh
->radio
, core
, cmd
, arg
, video_do_ioctl
);
1250 int cx88_do_ioctl(struct inode
*inode
, struct file
*file
, int radio
,
1251 struct cx88_core
*core
, unsigned int cmd
, void *arg
, v4l2_kioctl driver_ioctl
)
1255 dprintk( 1, "CORE IOCTL: 0x%x\n", cmd
);
1256 if (video_debug
> 1)
1257 v4l_print_ioctl(core
->name
,cmd
);
1260 /* ---------- tv norms ---------- */
1261 case VIDIOC_ENUMSTD
:
1263 struct v4l2_standard
*e
= arg
;
1267 if (i
>= ARRAY_SIZE(tvnorms
))
1269 err
= v4l2_video_std_construct(e
, tvnorms
[e
->index
].id
,
1270 tvnorms
[e
->index
].name
);
1278 v4l2_std_id
*id
= arg
;
1280 *id
= core
->tvnorm
->id
;
1285 v4l2_std_id
*id
= arg
;
1288 for(i
= 0; i
< ARRAY_SIZE(tvnorms
); i
++)
1289 if (*id
& tvnorms
[i
].id
)
1291 if (i
== ARRAY_SIZE(tvnorms
))
1295 cx88_set_tvnorm(core
,&tvnorms
[i
]);
1300 /* ------ input switching ---------- */
1301 case VIDIOC_ENUMINPUT
:
1303 static const char *iname
[] = {
1304 [ CX88_VMUX_COMPOSITE1
] = "Composite1",
1305 [ CX88_VMUX_COMPOSITE2
] = "Composite2",
1306 [ CX88_VMUX_COMPOSITE3
] = "Composite3",
1307 [ CX88_VMUX_COMPOSITE4
] = "Composite4",
1308 [ CX88_VMUX_SVIDEO
] = "S-Video",
1309 [ CX88_VMUX_TELEVISION
] = "Television",
1310 [ CX88_VMUX_CABLE
] = "Cable TV",
1311 [ CX88_VMUX_DVB
] = "DVB",
1312 [ CX88_VMUX_DEBUG
] = "for debug only",
1314 struct v4l2_input
*i
= arg
;
1320 if (0 == INPUT(n
)->type
)
1322 memset(i
,0,sizeof(*i
));
1324 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1325 strcpy(i
->name
,iname
[INPUT(n
)->type
]);
1326 if ((CX88_VMUX_TELEVISION
== INPUT(n
)->type
) ||
1327 (CX88_VMUX_CABLE
== INPUT(n
)->type
))
1328 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1329 for (n
= 0; n
< ARRAY_SIZE(tvnorms
); n
++)
1330 i
->std
|= tvnorms
[n
].id
;
1333 case VIDIOC_G_INPUT
:
1335 unsigned int *i
= arg
;
1340 case VIDIOC_S_INPUT
:
1342 unsigned int *i
= arg
;
1347 cx88_newstation(core
);
1355 /* --- controls ---------------------------------------------- */
1356 case VIDIOC_QUERYCTRL
:
1358 struct v4l2_queryctrl
*c
= arg
;
1361 if (c
->id
< V4L2_CID_BASE
||
1362 c
->id
>= V4L2_CID_LASTP1
)
1364 for (i
= 0; i
< CX8800_CTLS
; i
++)
1365 if (cx8800_ctls
[i
].v
.id
== c
->id
)
1367 if (i
== CX8800_CTLS
) {
1371 *c
= cx8800_ctls
[i
].v
;
1375 return get_control(core
,arg
);
1377 return set_control(core
,arg
);
1379 /* --- tuner ioctls ------------------------------------------ */
1380 case VIDIOC_G_TUNER
:
1382 struct v4l2_tuner
*t
= arg
;
1385 if (UNSET
== core
->tuner_type
)
1390 memset(t
,0,sizeof(*t
));
1391 strcpy(t
->name
, "Television");
1392 t
->type
= V4L2_TUNER_ANALOG_TV
;
1393 t
->capability
= V4L2_TUNER_CAP_NORM
;
1394 t
->rangehigh
= 0xffffffffUL
;
1396 cx88_get_stereo(core
,t
);
1397 reg
= cx_read(MO_DEVICE_STATUS
);
1398 t
->signal
= (reg
& (1<<5)) ? 0xffff : 0x0000;
1401 case VIDIOC_S_TUNER
:
1403 struct v4l2_tuner
*t
= arg
;
1405 if (UNSET
== core
->tuner_type
)
1409 cx88_set_stereo(core
, t
->audmode
, 1);
1412 case VIDIOC_G_FREQUENCY
:
1414 struct v4l2_frequency
*f
= arg
;
1416 memset(f
,0,sizeof(*f
));
1418 if (UNSET
== core
->tuner_type
)
1421 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1422 f
->type
= radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1423 f
->frequency
= core
->freq
;
1425 cx88_call_i2c_clients(core
,VIDIOC_G_FREQUENCY
,f
);
1429 case VIDIOC_S_FREQUENCY
:
1431 struct v4l2_frequency
*f
= arg
;
1433 if (UNSET
== core
->tuner_type
)
1437 if (0 == radio
&& f
->type
!= V4L2_TUNER_ANALOG_TV
)
1439 if (1 == radio
&& f
->type
!= V4L2_TUNER_RADIO
)
1442 core
->freq
= f
->frequency
;
1443 cx88_newstation(core
);
1444 cx88_call_i2c_clients(core
,VIDIOC_S_FREQUENCY
,f
);
1446 /* When changing channels it is required to reset TVAUDIO */
1448 cx88_set_tvaudio(core
);
1455 return v4l_compat_translate_ioctl(inode
,file
,cmd
,arg
,
1461 static int video_ioctl(struct inode
*inode
, struct file
*file
,
1462 unsigned int cmd
, unsigned long arg
)
1464 return video_usercopy(inode
, file
, cmd
, arg
, video_do_ioctl
);
1467 /* ----------------------------------------------------------- */
1469 static int radio_do_ioctl(struct inode
*inode
, struct file
*file
,
1470 unsigned int cmd
, void *arg
)
1472 struct cx8800_fh
*fh
= file
->private_data
;
1473 struct cx8800_dev
*dev
= fh
->dev
;
1474 struct cx88_core
*core
= dev
->core
;
1476 if (video_debug
> 1)
1477 v4l_print_ioctl(core
->name
,cmd
);
1480 case VIDIOC_QUERYCAP
:
1482 struct v4l2_capability
*cap
= arg
;
1484 memset(cap
,0,sizeof(*cap
));
1485 strcpy(cap
->driver
, "cx8800");
1486 strlcpy(cap
->card
, cx88_boards
[core
->board
].name
,
1488 sprintf(cap
->bus_info
,"PCI:%s", pci_name(dev
->pci
));
1489 cap
->version
= CX88_VERSION_CODE
;
1490 cap
->capabilities
= V4L2_CAP_TUNER
;
1493 case VIDIOC_G_TUNER
:
1495 struct v4l2_tuner
*t
= arg
;
1500 memset(t
,0,sizeof(*t
));
1501 strcpy(t
->name
, "Radio");
1502 t
->type
= V4L2_TUNER_RADIO
;
1504 cx88_call_i2c_clients(core
,VIDIOC_G_TUNER
,t
);
1507 case VIDIOC_ENUMINPUT
:
1509 struct v4l2_input
*i
= arg
;
1513 strcpy(i
->name
,"Radio");
1514 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1517 case VIDIOC_G_INPUT
:
1523 case VIDIOC_G_AUDIO
:
1525 struct v4l2_audio
*a
= arg
;
1527 memset(a
,0,sizeof(*a
));
1528 strcpy(a
->name
,"Radio");
1533 v4l2_std_id
*id
= arg
;
1540 struct video_tuner
*v
= arg
;
1542 if (v
->tuner
) /* Only tuner 0 */
1545 cx88_call_i2c_clients(core
,VIDIOCSTUNER
,v
);
1549 case VIDIOC_S_TUNER
:
1551 struct v4l2_tuner
*t
= arg
;
1556 cx88_call_i2c_clients(core
,VIDIOC_S_TUNER
,t
);
1561 case VIDIOC_S_AUDIO
:
1562 case VIDIOC_S_INPUT
:
1566 case VIDIOC_QUERYCTRL
:
1568 struct v4l2_queryctrl
*c
= arg
;
1571 if (c
->id
< V4L2_CID_BASE
||
1572 c
->id
>= V4L2_CID_LASTP1
)
1574 if (c
->id
== V4L2_CID_AUDIO_MUTE
) {
1575 for (i
= 0; i
< CX8800_CTLS
; i
++)
1576 if (cx8800_ctls
[i
].v
.id
== c
->id
)
1578 *c
= cx8800_ctls
[i
].v
;
1587 case VIDIOC_G_FREQUENCY
:
1588 case VIDIOC_S_FREQUENCY
:
1589 return video_do_ioctl(inode
,file
,cmd
,arg
);
1592 return v4l_compat_translate_ioctl(inode
,file
,cmd
,arg
,
1598 static int radio_ioctl(struct inode
*inode
, struct file
*file
,
1599 unsigned int cmd
, unsigned long arg
)
1601 return video_usercopy(inode
, file
, cmd
, arg
, radio_do_ioctl
);
1604 /* ----------------------------------------------------------- */
1606 static void cx8800_vid_timeout(unsigned long data
)
1608 struct cx8800_dev
*dev
= (struct cx8800_dev
*)data
;
1609 struct cx88_core
*core
= dev
->core
;
1610 struct cx88_dmaqueue
*q
= &dev
->vidq
;
1611 struct cx88_buffer
*buf
;
1612 unsigned long flags
;
1614 cx88_sram_channel_dump(core
, &cx88_sram_channels
[SRAM_CH21
]);
1616 cx_clear(MO_VID_DMACNTRL
, 0x11);
1617 cx_clear(VID_CAPTURE_CONTROL
, 0x06);
1619 spin_lock_irqsave(&dev
->slock
,flags
);
1620 while (!list_empty(&q
->active
)) {
1621 buf
= list_entry(q
->active
.next
, struct cx88_buffer
, vb
.queue
);
1622 list_del(&buf
->vb
.queue
);
1623 buf
->vb
.state
= STATE_ERROR
;
1624 wake_up(&buf
->vb
.done
);
1625 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core
->name
,
1626 buf
, buf
->vb
.i
, (unsigned long)buf
->risc
.dma
);
1628 restart_video_queue(dev
,q
);
1629 spin_unlock_irqrestore(&dev
->slock
,flags
);
1632 static char *cx88_vid_irqs
[32] = {
1633 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1634 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1635 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1636 "y_sync", "u_sync", "v_sync", "vbi_sync",
1637 "opc_err", "par_err", "rip_err", "pci_abort",
1640 static void cx8800_vid_irq(struct cx8800_dev
*dev
)
1642 struct cx88_core
*core
= dev
->core
;
1643 u32 status
, mask
, count
;
1645 status
= cx_read(MO_VID_INTSTAT
);
1646 mask
= cx_read(MO_VID_INTMSK
);
1647 if (0 == (status
& mask
))
1649 cx_write(MO_VID_INTSTAT
, status
);
1650 if (irq_debug
|| (status
& mask
& ~0xff))
1651 cx88_print_irqbits(core
->name
, "irq vid",
1652 cx88_vid_irqs
, status
, mask
);
1654 /* risc op code error */
1655 if (status
& (1 << 16)) {
1656 printk(KERN_WARNING
"%s/0: video risc op code error\n",core
->name
);
1657 cx_clear(MO_VID_DMACNTRL
, 0x11);
1658 cx_clear(VID_CAPTURE_CONTROL
, 0x06);
1659 cx88_sram_channel_dump(core
, &cx88_sram_channels
[SRAM_CH21
]);
1663 if (status
& 0x01) {
1664 spin_lock(&dev
->slock
);
1665 count
= cx_read(MO_VIDY_GPCNT
);
1666 cx88_wakeup(core
, &dev
->vidq
, count
);
1667 spin_unlock(&dev
->slock
);
1671 if (status
& 0x08) {
1672 spin_lock(&dev
->slock
);
1673 count
= cx_read(MO_VBI_GPCNT
);
1674 cx88_wakeup(core
, &dev
->vbiq
, count
);
1675 spin_unlock(&dev
->slock
);
1679 if (status
& 0x10) {
1680 dprintk(2,"stopper video\n");
1681 spin_lock(&dev
->slock
);
1682 restart_video_queue(dev
,&dev
->vidq
);
1683 spin_unlock(&dev
->slock
);
1687 if (status
& 0x80) {
1688 dprintk(2,"stopper vbi\n");
1689 spin_lock(&dev
->slock
);
1690 cx8800_restart_vbi_queue(dev
,&dev
->vbiq
);
1691 spin_unlock(&dev
->slock
);
1695 static irqreturn_t
cx8800_irq(int irq
, void *dev_id
, struct pt_regs
*regs
)
1697 struct cx8800_dev
*dev
= dev_id
;
1698 struct cx88_core
*core
= dev
->core
;
1700 int loop
, handled
= 0;
1702 for (loop
= 0; loop
< 10; loop
++) {
1703 status
= cx_read(MO_PCI_INTSTAT
) & (core
->pci_irqmask
| 0x01);
1706 cx_write(MO_PCI_INTSTAT
, status
);
1709 if (status
& core
->pci_irqmask
)
1710 cx88_core_irq(core
,status
);
1712 cx8800_vid_irq(dev
);
1715 printk(KERN_WARNING
"%s/0: irq loop -- clearing mask\n",
1717 cx_write(MO_PCI_INTMSK
,0);
1721 return IRQ_RETVAL(handled
);
1724 /* ----------------------------------------------------------- */
1725 /* exported stuff */
1727 static struct file_operations video_fops
=
1729 .owner
= THIS_MODULE
,
1731 .release
= video_release
,
1735 .ioctl
= video_ioctl
,
1736 .compat_ioctl
= v4l_compat_ioctl32
,
1737 .llseek
= no_llseek
,
1740 static struct video_device cx8800_video_template
=
1742 .name
= "cx8800-video",
1743 .type
= VID_TYPE_CAPTURE
|VID_TYPE_TUNER
|VID_TYPE_SCALES
,
1745 .fops
= &video_fops
,
1749 static struct video_device cx8800_vbi_template
=
1751 .name
= "cx8800-vbi",
1752 .type
= VID_TYPE_TELETEXT
|VID_TYPE_TUNER
,
1754 .fops
= &video_fops
,
1758 static struct file_operations radio_fops
=
1760 .owner
= THIS_MODULE
,
1762 .release
= video_release
,
1763 .ioctl
= radio_ioctl
,
1764 .compat_ioctl
= v4l_compat_ioctl32
,
1765 .llseek
= no_llseek
,
1768 static struct video_device cx8800_radio_template
=
1770 .name
= "cx8800-radio",
1771 .type
= VID_TYPE_TUNER
,
1773 .fops
= &radio_fops
,
1777 /* ----------------------------------------------------------- */
1779 static void cx8800_unregister_video(struct cx8800_dev
*dev
)
1781 if (dev
->radio_dev
) {
1782 if (-1 != dev
->radio_dev
->minor
)
1783 video_unregister_device(dev
->radio_dev
);
1785 video_device_release(dev
->radio_dev
);
1786 dev
->radio_dev
= NULL
;
1789 if (-1 != dev
->vbi_dev
->minor
)
1790 video_unregister_device(dev
->vbi_dev
);
1792 video_device_release(dev
->vbi_dev
);
1793 dev
->vbi_dev
= NULL
;
1795 if (dev
->video_dev
) {
1796 if (-1 != dev
->video_dev
->minor
)
1797 video_unregister_device(dev
->video_dev
);
1799 video_device_release(dev
->video_dev
);
1800 dev
->video_dev
= NULL
;
1804 static int __devinit
cx8800_initdev(struct pci_dev
*pci_dev
,
1805 const struct pci_device_id
*pci_id
)
1807 struct cx8800_dev
*dev
;
1808 struct cx88_core
*core
;
1811 dev
= kzalloc(sizeof(*dev
),GFP_KERNEL
);
1817 if (pci_enable_device(pci_dev
)) {
1821 core
= cx88_core_get(dev
->pci
);
1828 /* print pci info */
1829 pci_read_config_byte(pci_dev
, PCI_CLASS_REVISION
, &dev
->pci_rev
);
1830 pci_read_config_byte(pci_dev
, PCI_LATENCY_TIMER
, &dev
->pci_lat
);
1831 printk(KERN_INFO
"%s/0: found at %s, rev: %d, irq: %d, "
1832 "latency: %d, mmio: 0x%lx\n", core
->name
,
1833 pci_name(pci_dev
), dev
->pci_rev
, pci_dev
->irq
,
1834 dev
->pci_lat
,pci_resource_start(pci_dev
,0));
1836 pci_set_master(pci_dev
);
1837 if (!pci_dma_supported(pci_dev
,0xffffffff)) {
1838 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core
->name
);
1843 /* initialize driver struct */
1844 spin_lock_init(&dev
->slock
);
1845 core
->tvnorm
= tvnorms
;
1847 /* init video dma queues */
1848 INIT_LIST_HEAD(&dev
->vidq
.active
);
1849 INIT_LIST_HEAD(&dev
->vidq
.queued
);
1850 dev
->vidq
.timeout
.function
= cx8800_vid_timeout
;
1851 dev
->vidq
.timeout
.data
= (unsigned long)dev
;
1852 init_timer(&dev
->vidq
.timeout
);
1853 cx88_risc_stopper(dev
->pci
,&dev
->vidq
.stopper
,
1854 MO_VID_DMACNTRL
,0x11,0x00);
1856 /* init vbi dma queues */
1857 INIT_LIST_HEAD(&dev
->vbiq
.active
);
1858 INIT_LIST_HEAD(&dev
->vbiq
.queued
);
1859 dev
->vbiq
.timeout
.function
= cx8800_vbi_timeout
;
1860 dev
->vbiq
.timeout
.data
= (unsigned long)dev
;
1861 init_timer(&dev
->vbiq
.timeout
);
1862 cx88_risc_stopper(dev
->pci
,&dev
->vbiq
.stopper
,
1863 MO_VID_DMACNTRL
,0x88,0x00);
1866 err
= request_irq(pci_dev
->irq
, cx8800_irq
,
1867 SA_SHIRQ
| SA_INTERRUPT
, core
->name
, dev
);
1869 printk(KERN_ERR
"%s: can't get IRQ %d\n",
1870 core
->name
,pci_dev
->irq
);
1873 cx_set(MO_PCI_INTMSK
, core
->pci_irqmask
);
1875 /* load and configure helper modules */
1876 if (TUNER_ABSENT
!= core
->tuner_type
)
1877 request_module("tuner");
1878 if (core
->tda9887_conf
)
1879 request_module("tda9887");
1881 /* register v4l devices */
1882 dev
->video_dev
= cx88_vdev_init(core
,dev
->pci
,
1883 &cx8800_video_template
,"video");
1884 err
= video_register_device(dev
->video_dev
,VFL_TYPE_GRABBER
,
1885 video_nr
[core
->nr
]);
1887 printk(KERN_INFO
"%s: can't register video device\n",
1891 printk(KERN_INFO
"%s/0: registered device video%d [v4l2]\n",
1892 core
->name
,dev
->video_dev
->minor
& 0x1f);
1894 dev
->vbi_dev
= cx88_vdev_init(core
,dev
->pci
,&cx8800_vbi_template
,"vbi");
1895 err
= video_register_device(dev
->vbi_dev
,VFL_TYPE_VBI
,
1898 printk(KERN_INFO
"%s/0: can't register vbi device\n",
1902 printk(KERN_INFO
"%s/0: registered device vbi%d\n",
1903 core
->name
,dev
->vbi_dev
->minor
& 0x1f);
1905 if (core
->has_radio
) {
1906 dev
->radio_dev
= cx88_vdev_init(core
,dev
->pci
,
1907 &cx8800_radio_template
,"radio");
1908 err
= video_register_device(dev
->radio_dev
,VFL_TYPE_RADIO
,
1909 radio_nr
[core
->nr
]);
1911 printk(KERN_INFO
"%s/0: can't register radio device\n",
1915 printk(KERN_INFO
"%s/0: registered device radio%d\n",
1916 core
->name
,dev
->radio_dev
->minor
& 0x1f);
1919 /* everything worked */
1920 list_add_tail(&dev
->devlist
,&cx8800_devlist
);
1921 pci_set_drvdata(pci_dev
,dev
);
1923 /* initial device configuration */
1925 cx88_set_tvnorm(core
,tvnorms
);
1926 init_controls(core
);
1930 /* start tvaudio thread */
1931 if (core
->tuner_type
!= TUNER_ABSENT
)
1932 core
->kthread
= kthread_run(cx88_audio_thread
, core
, "cx88 tvaudio");
1936 cx8800_unregister_video(dev
);
1937 free_irq(pci_dev
->irq
, dev
);
1939 cx88_core_put(core
,dev
->pci
);
1945 static void __devexit
cx8800_finidev(struct pci_dev
*pci_dev
)
1947 struct cx8800_dev
*dev
= pci_get_drvdata(pci_dev
);
1948 struct cx88_core
*core
= dev
->core
;
1951 if (core
->kthread
) {
1952 kthread_stop(core
->kthread
);
1953 core
->kthread
= NULL
;
1956 cx88_shutdown(core
); /* FIXME */
1957 pci_disable_device(pci_dev
);
1959 /* unregister stuff */
1961 free_irq(pci_dev
->irq
, dev
);
1962 cx8800_unregister_video(dev
);
1963 pci_set_drvdata(pci_dev
, NULL
);
1966 btcx_riscmem_free(dev
->pci
,&dev
->vidq
.stopper
);
1967 list_del(&dev
->devlist
);
1968 cx88_core_put(core
,dev
->pci
);
1972 static int cx8800_suspend(struct pci_dev
*pci_dev
, pm_message_t state
)
1974 struct cx8800_dev
*dev
= pci_get_drvdata(pci_dev
);
1975 struct cx88_core
*core
= dev
->core
;
1977 /* stop video+vbi capture */
1978 spin_lock(&dev
->slock
);
1979 if (!list_empty(&dev
->vidq
.active
)) {
1980 printk("%s: suspend video\n", core
->name
);
1981 stop_video_dma(dev
);
1982 del_timer(&dev
->vidq
.timeout
);
1984 if (!list_empty(&dev
->vbiq
.active
)) {
1985 printk("%s: suspend vbi\n", core
->name
);
1986 cx8800_stop_vbi_dma(dev
);
1987 del_timer(&dev
->vbiq
.timeout
);
1989 spin_unlock(&dev
->slock
);
1991 /* FIXME -- shutdown device */
1992 cx88_shutdown(core
);
1994 pci_save_state(pci_dev
);
1995 if (0 != pci_set_power_state(pci_dev
, pci_choose_state(pci_dev
, state
))) {
1996 pci_disable_device(pci_dev
);
1997 dev
->state
.disabled
= 1;
2002 static int cx8800_resume(struct pci_dev
*pci_dev
)
2004 struct cx8800_dev
*dev
= pci_get_drvdata(pci_dev
);
2005 struct cx88_core
*core
= dev
->core
;
2008 if (dev
->state
.disabled
) {
2009 err
=pci_enable_device(pci_dev
);
2011 printk(KERN_ERR
"%s: can't enable device\n",
2016 dev
->state
.disabled
= 0;
2018 err
= pci_set_power_state(pci_dev
, PCI_D0
);
2020 printk(KERN_ERR
"%s: can't enable device\n",
2023 pci_disable_device(pci_dev
);
2024 dev
->state
.disabled
= 1;
2028 pci_restore_state(pci_dev
);
2030 /* FIXME: re-initialize hardware */
2033 /* restart video+vbi capture */
2034 spin_lock(&dev
->slock
);
2035 if (!list_empty(&dev
->vidq
.active
)) {
2036 printk("%s: resume video\n", core
->name
);
2037 restart_video_queue(dev
,&dev
->vidq
);
2039 if (!list_empty(&dev
->vbiq
.active
)) {
2040 printk("%s: resume vbi\n", core
->name
);
2041 cx8800_restart_vbi_queue(dev
,&dev
->vbiq
);
2043 spin_unlock(&dev
->slock
);
2048 /* ----------------------------------------------------------- */
2050 static struct pci_device_id cx8800_pci_tbl
[] = {
2054 .subvendor
= PCI_ANY_ID
,
2055 .subdevice
= PCI_ANY_ID
,
2057 /* --- end of list --- */
2060 MODULE_DEVICE_TABLE(pci
, cx8800_pci_tbl
);
2062 static struct pci_driver cx8800_pci_driver
= {
2064 .id_table
= cx8800_pci_tbl
,
2065 .probe
= cx8800_initdev
,
2066 .remove
= __devexit_p(cx8800_finidev
),
2068 .suspend
= cx8800_suspend
,
2069 .resume
= cx8800_resume
,
2072 static int cx8800_init(void)
2074 printk(KERN_INFO
"cx2388x v4l2 driver version %d.%d.%d loaded\n",
2075 (CX88_VERSION_CODE
>> 16) & 0xff,
2076 (CX88_VERSION_CODE
>> 8) & 0xff,
2077 CX88_VERSION_CODE
& 0xff);
2079 printk(KERN_INFO
"cx2388x: snapshot date %04d-%02d-%02d\n",
2080 SNAPSHOT
/10000, (SNAPSHOT
/100)%100, SNAPSHOT
%100);
2082 return pci_register_driver(&cx8800_pci_driver
);
2085 static void cx8800_fini(void)
2087 pci_unregister_driver(&cx8800_pci_driver
);
2090 module_init(cx8800_init
);
2091 module_exit(cx8800_fini
);
2093 EXPORT_SYMBOL(cx88_do_ioctl
);
2095 /* ----------------------------------------------------------- */
2100 * kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off