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 #ifdef CONFIG_VIDEO_V4L1_COMPAT
39 /* Include V4L1 specific functions. Should be removed soon */
40 #include <linux/videodev.h>
43 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
44 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
45 MODULE_LICENSE("GPL");
47 /* ------------------------------------------------------------------ */
49 static unsigned int video_nr
[] = {[0 ... (CX88_MAXBOARDS
- 1)] = UNSET
};
50 static unsigned int vbi_nr
[] = {[0 ... (CX88_MAXBOARDS
- 1)] = UNSET
};
51 static unsigned int radio_nr
[] = {[0 ... (CX88_MAXBOARDS
- 1)] = UNSET
};
53 module_param_array(video_nr
, int, NULL
, 0444);
54 module_param_array(vbi_nr
, int, NULL
, 0444);
55 module_param_array(radio_nr
, int, NULL
, 0444);
57 MODULE_PARM_DESC(video_nr
,"video device numbers");
58 MODULE_PARM_DESC(vbi_nr
,"vbi device numbers");
59 MODULE_PARM_DESC(radio_nr
,"radio device numbers");
61 static unsigned int video_debug
= 0;
62 module_param(video_debug
,int,0644);
63 MODULE_PARM_DESC(video_debug
,"enable debug messages [video]");
65 static unsigned int irq_debug
= 0;
66 module_param(irq_debug
,int,0644);
67 MODULE_PARM_DESC(irq_debug
,"enable debug messages [IRQ handler]");
69 static unsigned int vid_limit
= 16;
70 module_param(vid_limit
,int,0644);
71 MODULE_PARM_DESC(vid_limit
,"capture memory limit in megabytes");
73 #define dprintk(level,fmt, arg...) if (video_debug >= level) \
74 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
76 /* ------------------------------------------------------------------ */
78 static LIST_HEAD(cx8800_devlist
);
80 /* ------------------------------------------------------------------- */
83 static struct cx88_tvnorm tvnorms
[] = {
86 .id
= V4L2_STD_NTSC_M
,
87 .cxiformat
= VideoFormatNTSC
,
88 .cxoformat
= 0x181f0008,
91 .id
= V4L2_STD_NTSC_M_JP
,
92 .cxiformat
= VideoFormatNTSCJapan
,
93 .cxoformat
= 0x181f0008,
96 .id
= V4L2_STD_PAL_BG
,
97 .cxiformat
= VideoFormatPAL
,
98 .cxoformat
= 0x181f0008,
101 .id
= V4L2_STD_PAL_DK
,
102 .cxiformat
= VideoFormatPAL
,
103 .cxoformat
= 0x181f0008,
106 .id
= V4L2_STD_PAL_I
,
107 .cxiformat
= VideoFormatPAL
,
108 .cxoformat
= 0x181f0008,
111 .id
= V4L2_STD_PAL_M
,
112 .cxiformat
= VideoFormatPALM
,
113 .cxoformat
= 0x1c1f0008,
116 .id
= V4L2_STD_PAL_N
,
117 .cxiformat
= VideoFormatPALN
,
118 .cxoformat
= 0x1c1f0008,
121 .id
= V4L2_STD_PAL_Nc
,
122 .cxiformat
= VideoFormatPALNC
,
123 .cxoformat
= 0x1c1f0008,
126 .id
= V4L2_STD_PAL_60
,
127 .cxiformat
= VideoFormatPAL60
,
128 .cxoformat
= 0x181f0008,
131 .id
= V4L2_STD_SECAM_L
,
132 .cxiformat
= VideoFormatSECAM
,
133 .cxoformat
= 0x181f0008,
136 .id
= V4L2_STD_SECAM_DK
,
137 .cxiformat
= VideoFormatSECAM
,
138 .cxoformat
= 0x181f0008,
142 static struct cx8800_fmt formats
[] = {
144 .name
= "8 bpp, gray",
145 .fourcc
= V4L2_PIX_FMT_GREY
,
146 .cxformat
= ColorFormatY8
,
148 .flags
= FORMAT_FLAGS_PACKED
,
150 .name
= "15 bpp RGB, le",
151 .fourcc
= V4L2_PIX_FMT_RGB555
,
152 .cxformat
= ColorFormatRGB15
,
154 .flags
= FORMAT_FLAGS_PACKED
,
156 .name
= "15 bpp RGB, be",
157 .fourcc
= V4L2_PIX_FMT_RGB555X
,
158 .cxformat
= ColorFormatRGB15
| ColorFormatBSWAP
,
160 .flags
= FORMAT_FLAGS_PACKED
,
162 .name
= "16 bpp RGB, le",
163 .fourcc
= V4L2_PIX_FMT_RGB565
,
164 .cxformat
= ColorFormatRGB16
,
166 .flags
= FORMAT_FLAGS_PACKED
,
168 .name
= "16 bpp RGB, be",
169 .fourcc
= V4L2_PIX_FMT_RGB565X
,
170 .cxformat
= ColorFormatRGB16
| ColorFormatBSWAP
,
172 .flags
= FORMAT_FLAGS_PACKED
,
174 .name
= "24 bpp RGB, le",
175 .fourcc
= V4L2_PIX_FMT_BGR24
,
176 .cxformat
= ColorFormatRGB24
,
178 .flags
= FORMAT_FLAGS_PACKED
,
180 .name
= "32 bpp RGB, le",
181 .fourcc
= V4L2_PIX_FMT_BGR32
,
182 .cxformat
= ColorFormatRGB32
,
184 .flags
= FORMAT_FLAGS_PACKED
,
186 .name
= "32 bpp RGB, be",
187 .fourcc
= V4L2_PIX_FMT_RGB32
,
188 .cxformat
= ColorFormatRGB32
| ColorFormatBSWAP
| ColorFormatWSWAP
,
190 .flags
= FORMAT_FLAGS_PACKED
,
192 .name
= "4:2:2, packed, YUYV",
193 .fourcc
= V4L2_PIX_FMT_YUYV
,
194 .cxformat
= ColorFormatYUY2
,
196 .flags
= FORMAT_FLAGS_PACKED
,
198 .name
= "4:2:2, packed, UYVY",
199 .fourcc
= V4L2_PIX_FMT_UYVY
,
200 .cxformat
= ColorFormatYUY2
| ColorFormatBSWAP
,
202 .flags
= FORMAT_FLAGS_PACKED
,
206 static struct cx8800_fmt
* format_by_fourcc(unsigned int fourcc
)
210 for (i
= 0; i
< ARRAY_SIZE(formats
); i
++)
211 if (formats
[i
].fourcc
== fourcc
)
216 /* ------------------------------------------------------------------- */
218 static const struct v4l2_queryctrl no_ctl
= {
220 .flags
= V4L2_CTRL_FLAG_DISABLED
,
223 static struct cx88_ctrl cx8800_ctls
[] = {
227 .id
= V4L2_CID_BRIGHTNESS
,
228 .name
= "Brightness",
232 .default_value
= 0x7f,
233 .type
= V4L2_CTRL_TYPE_INTEGER
,
236 .reg
= MO_CONTR_BRIGHT
,
241 .id
= V4L2_CID_CONTRAST
,
246 .default_value
= 0x3f,
247 .type
= V4L2_CTRL_TYPE_INTEGER
,
250 .reg
= MO_CONTR_BRIGHT
,
260 .default_value
= 0x7f,
261 .type
= V4L2_CTRL_TYPE_INTEGER
,
268 /* strictly, this only describes only U saturation.
269 * V saturation is handled specially through code.
272 .id
= V4L2_CID_SATURATION
,
273 .name
= "Saturation",
277 .default_value
= 0x7f,
278 .type
= V4L2_CTRL_TYPE_INTEGER
,
281 .reg
= MO_UV_SATURATION
,
287 .id
= V4L2_CID_AUDIO_MUTE
,
292 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
295 .sreg
= SHADOW_AUD_VOL_CTL
,
300 .id
= V4L2_CID_AUDIO_VOLUME
,
305 .default_value
= 0x3f,
306 .type
= V4L2_CTRL_TYPE_INTEGER
,
309 .sreg
= SHADOW_AUD_VOL_CTL
,
314 .id
= V4L2_CID_AUDIO_BALANCE
,
319 .default_value
= 0x40,
320 .type
= V4L2_CTRL_TYPE_INTEGER
,
323 .sreg
= SHADOW_AUD_BAL_CTL
,
328 static const int CX8800_CTLS
= ARRAY_SIZE(cx8800_ctls
);
330 /* ------------------------------------------------------------------- */
331 /* resource management */
333 static int res_get(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
, unsigned int bit
)
335 struct cx88_core
*core
= dev
->core
;
336 if (fh
->resources
& bit
)
337 /* have it already allocated */
341 mutex_lock(&core
->lock
);
342 if (dev
->resources
& bit
) {
343 /* no, someone else uses it */
344 mutex_unlock(&core
->lock
);
347 /* it's free, grab it */
348 fh
->resources
|= bit
;
349 dev
->resources
|= bit
;
350 dprintk(1,"res: get %d\n",bit
);
351 mutex_unlock(&core
->lock
);
356 int res_check(struct cx8800_fh
*fh
, unsigned int bit
)
358 return (fh
->resources
& bit
);
362 int res_locked(struct cx8800_dev
*dev
, unsigned int bit
)
364 return (dev
->resources
& bit
);
368 void res_free(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
, unsigned int bits
)
370 struct cx88_core
*core
= dev
->core
;
371 BUG_ON((fh
->resources
& bits
) != bits
);
373 mutex_lock(&core
->lock
);
374 fh
->resources
&= ~bits
;
375 dev
->resources
&= ~bits
;
376 dprintk(1,"res: put %d\n",bits
);
377 mutex_unlock(&core
->lock
);
380 /* ------------------------------------------------------------------ */
382 /* static int video_mux(struct cx8800_dev *dev, unsigned int input) */
383 static int video_mux(struct cx88_core
*core
, unsigned int input
)
385 /* struct cx88_core *core = dev->core; */
387 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
388 input
, INPUT(input
)->vmux
,
389 INPUT(input
)->gpio0
,INPUT(input
)->gpio1
,
390 INPUT(input
)->gpio2
,INPUT(input
)->gpio3
);
392 cx_andor(MO_INPUT_FORMAT
, 0x03 << 14, INPUT(input
)->vmux
<< 14);
393 cx_write(MO_GP3_IO
, INPUT(input
)->gpio3
);
394 cx_write(MO_GP0_IO
, INPUT(input
)->gpio0
);
395 cx_write(MO_GP1_IO
, INPUT(input
)->gpio1
);
396 cx_write(MO_GP2_IO
, INPUT(input
)->gpio2
);
398 switch (INPUT(input
)->type
) {
399 case CX88_VMUX_SVIDEO
:
400 cx_set(MO_AFECFG_IO
, 0x00000001);
401 cx_set(MO_INPUT_FORMAT
, 0x00010010);
402 cx_set(MO_FILTER_EVEN
, 0x00002020);
403 cx_set(MO_FILTER_ODD
, 0x00002020);
406 cx_clear(MO_AFECFG_IO
, 0x00000001);
407 cx_clear(MO_INPUT_FORMAT
, 0x00010010);
408 cx_clear(MO_FILTER_EVEN
, 0x00002020);
409 cx_clear(MO_FILTER_ODD
, 0x00002020);
415 /* ------------------------------------------------------------------ */
417 static int start_video_dma(struct cx8800_dev
*dev
,
418 struct cx88_dmaqueue
*q
,
419 struct cx88_buffer
*buf
)
421 struct cx88_core
*core
= dev
->core
;
423 /* setup fifo + format */
424 cx88_sram_channel_setup(core
, &cx88_sram_channels
[SRAM_CH21
],
425 buf
->bpl
, buf
->risc
.dma
);
426 cx88_set_scale(core
, buf
->vb
.width
, buf
->vb
.height
, buf
->vb
.field
);
427 cx_write(MO_COLOR_CTRL
, buf
->fmt
->cxformat
| ColorFormatGamma
);
430 cx_write(MO_VIDY_GPCNTRL
,GP_COUNT_CONTROL_RESET
);
434 cx_set(MO_PCI_INTMSK
, core
->pci_irqmask
| 0x01);
436 /* Enables corresponding bits at PCI_INT_STAT:
437 bits 0 to 4: video, audio, transport stream, VIP, Host
439 bits 8 and 9: DMA complete for: SRC, DST
440 bits 10 and 11: BERR signal asserted for RISC: RD, WR
441 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
443 cx_set(MO_VID_INTMSK
, 0x0f0011);
446 cx_set(VID_CAPTURE_CONTROL
,0x06);
449 cx_set(MO_DEV_CNTRL2
, (1<<5));
450 cx_set(MO_VID_DMACNTRL
, 0x11); /* Planar Y and packed FIFO and RISC enable */
455 static int stop_video_dma(struct cx8800_dev
*dev
)
457 struct cx88_core
*core
= dev
->core
;
460 cx_clear(MO_VID_DMACNTRL
, 0x11);
462 /* disable capture */
463 cx_clear(VID_CAPTURE_CONTROL
,0x06);
466 cx_clear(MO_PCI_INTMSK
, 0x000001);
467 cx_clear(MO_VID_INTMSK
, 0x0f0011);
471 static int restart_video_queue(struct cx8800_dev
*dev
,
472 struct cx88_dmaqueue
*q
)
474 struct cx88_core
*core
= dev
->core
;
475 struct cx88_buffer
*buf
, *prev
;
476 struct list_head
*item
;
478 if (!list_empty(&q
->active
)) {
479 buf
= list_entry(q
->active
.next
, struct cx88_buffer
, vb
.queue
);
480 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
482 start_video_dma(dev
, q
, buf
);
483 list_for_each(item
,&q
->active
) {
484 buf
= list_entry(item
, struct cx88_buffer
, vb
.queue
);
485 buf
->count
= q
->count
++;
487 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
493 if (list_empty(&q
->queued
))
495 buf
= list_entry(q
->queued
.next
, struct cx88_buffer
, vb
.queue
);
497 list_move_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_move_tail(&buf
->vb
.queue
, &q
->active
);
509 buf
->vb
.state
= STATE_ACTIVE
;
510 buf
->count
= q
->count
++;
511 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
512 dprintk(2,"[%p/%d] restart_queue - move to active\n",
521 /* ------------------------------------------------------------------ */
524 buffer_setup(struct videobuf_queue
*q
, unsigned int *count
, unsigned int *size
)
526 struct cx8800_fh
*fh
= q
->priv_data
;
528 *size
= fh
->fmt
->depth
*fh
->width
*fh
->height
>> 3;
531 while (*size
* *count
> vid_limit
* 1024 * 1024)
537 buffer_prepare(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
538 enum v4l2_field field
)
540 struct cx8800_fh
*fh
= q
->priv_data
;
541 struct cx8800_dev
*dev
= fh
->dev
;
542 struct cx88_core
*core
= dev
->core
;
543 struct cx88_buffer
*buf
= container_of(vb
,struct cx88_buffer
,vb
);
544 int rc
, init_buffer
= 0;
546 BUG_ON(NULL
== fh
->fmt
);
547 if (fh
->width
< 48 || fh
->width
> norm_maxw(core
->tvnorm
) ||
548 fh
->height
< 32 || fh
->height
> norm_maxh(core
->tvnorm
))
550 buf
->vb
.size
= (fh
->width
* fh
->height
* fh
->fmt
->depth
) >> 3;
551 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
554 if (buf
->fmt
!= fh
->fmt
||
555 buf
->vb
.width
!= fh
->width
||
556 buf
->vb
.height
!= fh
->height
||
557 buf
->vb
.field
!= field
) {
559 buf
->vb
.width
= fh
->width
;
560 buf
->vb
.height
= fh
->height
;
561 buf
->vb
.field
= field
;
565 if (STATE_NEEDS_INIT
== buf
->vb
.state
) {
567 if (0 != (rc
= videobuf_iolock(q
,&buf
->vb
,NULL
)))
572 buf
->bpl
= buf
->vb
.width
* buf
->fmt
->depth
>> 3;
573 switch (buf
->vb
.field
) {
575 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
576 buf
->vb
.dma
.sglist
, 0, UNSET
,
577 buf
->bpl
, 0, buf
->vb
.height
);
579 case V4L2_FIELD_BOTTOM
:
580 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
581 buf
->vb
.dma
.sglist
, UNSET
, 0,
582 buf
->bpl
, 0, buf
->vb
.height
);
584 case V4L2_FIELD_INTERLACED
:
585 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
586 buf
->vb
.dma
.sglist
, 0, buf
->bpl
,
588 buf
->vb
.height
>> 1);
590 case V4L2_FIELD_SEQ_TB
:
591 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
593 0, buf
->bpl
* (buf
->vb
.height
>> 1),
595 buf
->vb
.height
>> 1);
597 case V4L2_FIELD_SEQ_BT
:
598 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
600 buf
->bpl
* (buf
->vb
.height
>> 1), 0,
602 buf
->vb
.height
>> 1);
608 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
610 fh
->width
, fh
->height
, fh
->fmt
->depth
, fh
->fmt
->name
,
611 (unsigned long)buf
->risc
.dma
);
613 buf
->vb
.state
= STATE_PREPARED
;
617 cx88_free_buffer(q
,buf
);
622 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
624 struct cx88_buffer
*buf
= container_of(vb
,struct cx88_buffer
,vb
);
625 struct cx88_buffer
*prev
;
626 struct cx8800_fh
*fh
= vq
->priv_data
;
627 struct cx8800_dev
*dev
= fh
->dev
;
628 struct cx88_core
*core
= dev
->core
;
629 struct cx88_dmaqueue
*q
= &dev
->vidq
;
631 /* add jump to stopper */
632 buf
->risc
.jmp
[0] = cpu_to_le32(RISC_JUMP
| RISC_IRQ1
| RISC_CNT_INC
);
633 buf
->risc
.jmp
[1] = cpu_to_le32(q
->stopper
.dma
);
635 if (!list_empty(&q
->queued
)) {
636 list_add_tail(&buf
->vb
.queue
,&q
->queued
);
637 buf
->vb
.state
= STATE_QUEUED
;
638 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
641 } else if (list_empty(&q
->active
)) {
642 list_add_tail(&buf
->vb
.queue
,&q
->active
);
643 start_video_dma(dev
, q
, buf
);
644 buf
->vb
.state
= STATE_ACTIVE
;
645 buf
->count
= q
->count
++;
646 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
647 dprintk(2,"[%p/%d] buffer_queue - first active\n",
651 prev
= list_entry(q
->active
.prev
, struct cx88_buffer
, vb
.queue
);
652 if (prev
->vb
.width
== buf
->vb
.width
&&
653 prev
->vb
.height
== buf
->vb
.height
&&
654 prev
->fmt
== buf
->fmt
) {
655 list_add_tail(&buf
->vb
.queue
,&q
->active
);
656 buf
->vb
.state
= STATE_ACTIVE
;
657 buf
->count
= q
->count
++;
658 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
659 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
663 list_add_tail(&buf
->vb
.queue
,&q
->queued
);
664 buf
->vb
.state
= STATE_QUEUED
;
665 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
671 static void buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
673 struct cx88_buffer
*buf
= container_of(vb
,struct cx88_buffer
,vb
);
675 cx88_free_buffer(q
,buf
);
678 static struct videobuf_queue_ops cx8800_video_qops
= {
679 .buf_setup
= buffer_setup
,
680 .buf_prepare
= buffer_prepare
,
681 .buf_queue
= buffer_queue
,
682 .buf_release
= buffer_release
,
685 /* ------------------------------------------------------------------ */
688 /* ------------------------------------------------------------------ */
690 static struct videobuf_queue
* get_queue(struct cx8800_fh
*fh
)
693 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
695 case V4L2_BUF_TYPE_VBI_CAPTURE
:
703 static int get_ressource(struct cx8800_fh
*fh
)
706 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
707 return RESOURCE_VIDEO
;
708 case V4L2_BUF_TYPE_VBI_CAPTURE
:
716 static int video_open(struct inode
*inode
, struct file
*file
)
718 int minor
= iminor(inode
);
719 struct cx8800_dev
*h
,*dev
= NULL
;
720 struct cx88_core
*core
;
721 struct cx8800_fh
*fh
;
722 struct list_head
*list
;
723 enum v4l2_buf_type type
= 0;
726 list_for_each(list
,&cx8800_devlist
) {
727 h
= list_entry(list
, struct cx8800_dev
, devlist
);
728 if (h
->video_dev
->minor
== minor
) {
730 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
732 if (h
->vbi_dev
->minor
== minor
) {
734 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
737 h
->radio_dev
->minor
== minor
) {
747 dprintk(1,"open minor=%d radio=%d type=%s\n",
748 minor
,radio
,v4l2_type_names
[type
]);
750 /* allocate + initialize per filehandle data */
751 fh
= kzalloc(sizeof(*fh
),GFP_KERNEL
);
754 file
->private_data
= fh
;
760 fh
->fmt
= format_by_fourcc(V4L2_PIX_FMT_BGR24
);
762 videobuf_queue_init(&fh
->vidq
, &cx8800_video_qops
,
763 dev
->pci
, &dev
->slock
,
764 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
765 V4L2_FIELD_INTERLACED
,
766 sizeof(struct cx88_buffer
),
768 videobuf_queue_init(&fh
->vbiq
, &cx8800_vbi_qops
,
769 dev
->pci
, &dev
->slock
,
770 V4L2_BUF_TYPE_VBI_CAPTURE
,
772 sizeof(struct cx88_buffer
),
776 int board
= core
->board
;
777 dprintk(1,"video_open: setting radio device\n");
778 cx_write(MO_GP3_IO
, cx88_boards
[board
].radio
.gpio3
);
779 cx_write(MO_GP0_IO
, cx88_boards
[board
].radio
.gpio0
);
780 cx_write(MO_GP1_IO
, cx88_boards
[board
].radio
.gpio1
);
781 cx_write(MO_GP2_IO
, cx88_boards
[board
].radio
.gpio2
);
782 core
->tvaudio
= WW_FM
;
783 cx88_set_tvaudio(core
);
784 cx88_set_stereo(core
,V4L2_TUNER_MODE_STEREO
,1);
785 cx88_call_i2c_clients(core
,AUDC_SET_RADIO
,NULL
);
792 video_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
794 struct cx8800_fh
*fh
= file
->private_data
;
797 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
798 if (res_locked(fh
->dev
,RESOURCE_VIDEO
))
800 return videobuf_read_one(&fh
->vidq
, data
, count
, ppos
,
801 file
->f_flags
& O_NONBLOCK
);
802 case V4L2_BUF_TYPE_VBI_CAPTURE
:
803 if (!res_get(fh
->dev
,fh
,RESOURCE_VBI
))
805 return videobuf_read_stream(&fh
->vbiq
, data
, count
, ppos
, 1,
806 file
->f_flags
& O_NONBLOCK
);
814 video_poll(struct file
*file
, struct poll_table_struct
*wait
)
816 struct cx8800_fh
*fh
= file
->private_data
;
817 struct cx88_buffer
*buf
;
819 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
) {
820 if (!res_get(fh
->dev
,fh
,RESOURCE_VBI
))
822 return videobuf_poll_stream(file
, &fh
->vbiq
, wait
);
825 if (res_check(fh
,RESOURCE_VIDEO
)) {
826 /* streaming capture */
827 if (list_empty(&fh
->vidq
.stream
))
829 buf
= list_entry(fh
->vidq
.stream
.next
,struct cx88_buffer
,vb
.stream
);
832 buf
= (struct cx88_buffer
*)fh
->vidq
.read_buf
;
836 poll_wait(file
, &buf
->vb
.done
, wait
);
837 if (buf
->vb
.state
== STATE_DONE
||
838 buf
->vb
.state
== STATE_ERROR
)
839 return POLLIN
|POLLRDNORM
;
843 static int video_release(struct inode
*inode
, struct file
*file
)
845 struct cx8800_fh
*fh
= file
->private_data
;
846 struct cx8800_dev
*dev
= fh
->dev
;
848 /* turn off overlay */
849 if (res_check(fh
, RESOURCE_OVERLAY
)) {
851 res_free(dev
,fh
,RESOURCE_OVERLAY
);
854 /* stop video capture */
855 if (res_check(fh
, RESOURCE_VIDEO
)) {
856 videobuf_queue_cancel(&fh
->vidq
);
857 res_free(dev
,fh
,RESOURCE_VIDEO
);
859 if (fh
->vidq
.read_buf
) {
860 buffer_release(&fh
->vidq
,fh
->vidq
.read_buf
);
861 kfree(fh
->vidq
.read_buf
);
864 /* stop vbi capture */
865 if (res_check(fh
, RESOURCE_VBI
)) {
866 if (fh
->vbiq
.streaming
)
867 videobuf_streamoff(&fh
->vbiq
);
868 if (fh
->vbiq
.reading
)
869 videobuf_read_stop(&fh
->vbiq
);
870 res_free(dev
,fh
,RESOURCE_VBI
);
873 videobuf_mmap_free(&fh
->vidq
);
874 videobuf_mmap_free(&fh
->vbiq
);
875 file
->private_data
= NULL
;
878 cx88_call_i2c_clients (dev
->core
, TUNER_SET_STANDBY
, NULL
);
884 video_mmap(struct file
*file
, struct vm_area_struct
* vma
)
886 struct cx8800_fh
*fh
= file
->private_data
;
888 return videobuf_mmap_mapper(get_queue(fh
), vma
);
891 /* ------------------------------------------------------------------ */
893 /* static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
894 static int get_control(struct cx88_core
*core
, struct v4l2_control
*ctl
)
896 /* struct cx88_core *core = dev->core; */
897 struct cx88_ctrl
*c
= NULL
;
901 for (i
= 0; i
< CX8800_CTLS
; i
++)
902 if (cx8800_ctls
[i
].v
.id
== ctl
->id
)
907 value
= c
->sreg
? cx_sread(c
->sreg
) : cx_read(c
->reg
);
909 case V4L2_CID_AUDIO_BALANCE
:
910 ctl
->value
= ((value
& 0x7f) < 0x40) ? ((value
& 0x7f) + 0x40)
911 : (0x7f - (value
& 0x7f));
913 case V4L2_CID_AUDIO_VOLUME
:
914 ctl
->value
= 0x3f - (value
& 0x3f);
917 ctl
->value
= ((value
+ (c
->off
<< c
->shift
)) & c
->mask
) >> c
->shift
;
920 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
921 ctl
->id
, c
->v
.name
, ctl
->value
, c
->reg
,
922 value
,c
->mask
, c
->sreg
? " [shadowed]" : "");
926 /* static int set_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
927 static int set_control(struct cx88_core
*core
, struct v4l2_control
*ctl
)
929 /* struct cx88_core *core = dev->core; */
930 struct cx88_ctrl
*c
= NULL
;
933 for (i
= 0; i
< CX8800_CTLS
; i
++) {
934 if (cx8800_ctls
[i
].v
.id
== ctl
->id
) {
941 if (ctl
->value
< c
->v
.minimum
)
942 ctl
->value
= c
->v
.minimum
;
943 if (ctl
->value
> c
->v
.maximum
)
944 ctl
->value
= c
->v
.maximum
;
947 case V4L2_CID_AUDIO_BALANCE
:
948 value
= (ctl
->value
< 0x40) ? (0x7f - ctl
->value
) : (ctl
->value
- 0x40);
950 case V4L2_CID_AUDIO_VOLUME
:
951 value
= 0x3f - (ctl
->value
& 0x3f);
953 case V4L2_CID_SATURATION
:
954 /* special v_sat handling */
956 value
= ((ctl
->value
- c
->off
) << c
->shift
) & c
->mask
;
958 if (core
->tvnorm
->id
& V4L2_STD_SECAM
) {
959 /* For SECAM, both U and V sat should be equal */
960 value
=value
<<8|value
;
962 /* Keeps U Saturation proportional to V Sat */
963 value
=(value
*0x5a)/0x7f<<8|value
;
968 value
= ((ctl
->value
- c
->off
) << c
->shift
) & c
->mask
;
971 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
972 ctl
->id
, c
->v
.name
, ctl
->value
, c
->reg
, value
,
973 mask
, c
->sreg
? " [shadowed]" : "");
975 cx_sandor(c
->sreg
, c
->reg
, mask
, value
);
977 cx_andor(c
->reg
, mask
, value
);
982 static void init_controls(struct cx88_core
*core
)
984 struct v4l2_control ctrl
;
987 for (i
= 0; i
< CX8800_CTLS
; i
++) {
988 ctrl
.id
=cx8800_ctls
[i
].v
.id
;
989 ctrl
.value
=cx8800_ctls
[i
].v
.default_value
;
990 set_control(core
, &ctrl
);
994 /* ------------------------------------------------------------------ */
996 static int cx8800_g_fmt(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
,
997 struct v4l2_format
*f
)
1000 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1001 memset(&f
->fmt
.pix
,0,sizeof(f
->fmt
.pix
));
1002 f
->fmt
.pix
.width
= fh
->width
;
1003 f
->fmt
.pix
.height
= fh
->height
;
1004 f
->fmt
.pix
.field
= fh
->vidq
.field
;
1005 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
1006 f
->fmt
.pix
.bytesperline
=
1007 (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
1008 f
->fmt
.pix
.sizeimage
=
1009 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1011 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1012 cx8800_vbi_fmt(dev
, f
);
1019 static int cx8800_try_fmt(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
,
1020 struct v4l2_format
*f
)
1022 struct cx88_core
*core
= dev
->core
;
1025 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1027 struct cx8800_fmt
*fmt
;
1028 enum v4l2_field field
;
1029 unsigned int maxw
, maxh
;
1031 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1035 field
= f
->fmt
.pix
.field
;
1036 maxw
= norm_maxw(core
->tvnorm
);
1037 maxh
= norm_maxh(core
->tvnorm
);
1039 if (V4L2_FIELD_ANY
== field
) {
1040 field
= (f
->fmt
.pix
.height
> maxh
/2)
1041 ? V4L2_FIELD_INTERLACED
1042 : V4L2_FIELD_BOTTOM
;
1046 case V4L2_FIELD_TOP
:
1047 case V4L2_FIELD_BOTTOM
:
1050 case V4L2_FIELD_INTERLACED
:
1056 f
->fmt
.pix
.field
= field
;
1057 if (f
->fmt
.pix
.height
< 32)
1058 f
->fmt
.pix
.height
= 32;
1059 if (f
->fmt
.pix
.height
> maxh
)
1060 f
->fmt
.pix
.height
= maxh
;
1061 if (f
->fmt
.pix
.width
< 48)
1062 f
->fmt
.pix
.width
= 48;
1063 if (f
->fmt
.pix
.width
> maxw
)
1064 f
->fmt
.pix
.width
= maxw
;
1065 f
->fmt
.pix
.width
&= ~0x03;
1066 f
->fmt
.pix
.bytesperline
=
1067 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
1068 f
->fmt
.pix
.sizeimage
=
1069 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1073 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1074 cx8800_vbi_fmt(dev
, f
);
1081 static int cx8800_s_fmt(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
,
1082 struct v4l2_format
*f
)
1087 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1088 err
= cx8800_try_fmt(dev
,fh
,f
);
1092 fh
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1093 fh
->width
= f
->fmt
.pix
.width
;
1094 fh
->height
= f
->fmt
.pix
.height
;
1095 fh
->vidq
.field
= f
->fmt
.pix
.field
;
1097 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1098 cx8800_vbi_fmt(dev
, f
);
1106 * This function is _not_ called directly, but from
1107 * video_generic_ioctl (and maybe others). userspace
1108 * copying is done already, arg is a kernel pointer.
1110 static int video_do_ioctl(struct inode
*inode
, struct file
*file
,
1111 unsigned int cmd
, void *arg
)
1113 struct cx8800_fh
*fh
= file
->private_data
;
1114 struct cx8800_dev
*dev
= fh
->dev
;
1115 struct cx88_core
*core
= dev
->core
;
1118 if (video_debug
> 1)
1119 v4l_print_ioctl(core
->name
,cmd
);
1122 /* --- capabilities ------------------------------------------ */
1123 case VIDIOC_QUERYCAP
:
1125 struct v4l2_capability
*cap
= arg
;
1127 memset(cap
,0,sizeof(*cap
));
1128 strcpy(cap
->driver
, "cx8800");
1129 strlcpy(cap
->card
, cx88_boards
[core
->board
].name
,
1131 sprintf(cap
->bus_info
,"PCI:%s",pci_name(dev
->pci
));
1132 cap
->version
= CX88_VERSION_CODE
;
1134 V4L2_CAP_VIDEO_CAPTURE
|
1135 V4L2_CAP_READWRITE
|
1136 V4L2_CAP_STREAMING
|
1137 V4L2_CAP_VBI_CAPTURE
|
1138 V4L2_CAP_VIDEO_OVERLAY
|
1140 if (UNSET
!= core
->tuner_type
)
1141 cap
->capabilities
|= V4L2_CAP_TUNER
;
1145 /* --- capture ioctls ---------------------------------------- */
1146 case VIDIOC_ENUM_FMT
:
1148 struct v4l2_fmtdesc
*f
= arg
;
1149 enum v4l2_buf_type type
;
1155 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1156 if (index
>= ARRAY_SIZE(formats
))
1158 memset(f
,0,sizeof(*f
));
1161 strlcpy(f
->description
,formats
[index
].name
,sizeof(f
->description
));
1162 f
->pixelformat
= formats
[index
].fourcc
;
1171 struct v4l2_format
*f
= arg
;
1172 return cx8800_g_fmt(dev
,fh
,f
);
1176 struct v4l2_format
*f
= arg
;
1177 return cx8800_s_fmt(dev
,fh
,f
);
1179 case VIDIOC_TRY_FMT
:
1181 struct v4l2_format
*f
= arg
;
1182 return cx8800_try_fmt(dev
,fh
,f
);
1185 /* --- streaming capture ------------------------------------- */
1188 struct video_mbuf
*mbuf
= arg
;
1189 struct videobuf_queue
*q
;
1190 struct v4l2_requestbuffers req
;
1194 memset(&req
,0,sizeof(req
));
1197 req
.memory
= V4L2_MEMORY_MMAP
;
1198 err
= videobuf_reqbufs(q
,&req
);
1201 memset(mbuf
,0,sizeof(*mbuf
));
1202 mbuf
->frames
= req
.count
;
1204 for (i
= 0; i
< mbuf
->frames
; i
++) {
1205 mbuf
->offsets
[i
] = q
->bufs
[i
]->boff
;
1206 mbuf
->size
+= q
->bufs
[i
]->bsize
;
1211 case VIDIOC_REQBUFS
:
1212 return videobuf_reqbufs(get_queue(fh
), arg
);
1214 case VIDIOC_QUERYBUF
:
1215 return videobuf_querybuf(get_queue(fh
), arg
);
1218 return videobuf_qbuf(get_queue(fh
), arg
);
1221 return videobuf_dqbuf(get_queue(fh
), arg
,
1222 file
->f_flags
& O_NONBLOCK
);
1224 case VIDIOC_STREAMON
:
1226 int res
= get_ressource(fh
);
1228 if (!res_get(dev
,fh
,res
))
1230 return videobuf_streamon(get_queue(fh
));
1232 case VIDIOC_STREAMOFF
:
1234 int res
= get_ressource(fh
);
1236 err
= videobuf_streamoff(get_queue(fh
));
1239 res_free(dev
,fh
,res
);
1243 return cx88_do_ioctl( inode
, file
, fh
->radio
, core
, cmd
, arg
, video_do_ioctl
);
1248 int cx88_do_ioctl(struct inode
*inode
, struct file
*file
, int radio
,
1249 struct cx88_core
*core
, unsigned int cmd
, void *arg
, v4l2_kioctl driver_ioctl
)
1254 if (video_debug
> 1) {
1255 if (_IOC_DIR(cmd
) & _IOC_WRITE
)
1256 v4l_printk_ioctl_arg("cx88(w)",cmd
, arg
);
1257 else if (!_IOC_DIR(cmd
) & _IOC_READ
) {
1258 v4l_print_ioctl("cx88", cmd
);
1261 v4l_print_ioctl(core
->name
,cmd
);
1266 /* ---------- tv norms ---------- */
1267 case VIDIOC_ENUMSTD
:
1269 struct v4l2_standard
*e
= arg
;
1273 if (i
>= ARRAY_SIZE(tvnorms
))
1275 err
= v4l2_video_std_construct(e
, tvnorms
[e
->index
].id
,
1276 tvnorms
[e
->index
].name
);
1284 v4l2_std_id
*id
= arg
;
1286 *id
= core
->tvnorm
->id
;
1291 v4l2_std_id
*id
= arg
;
1294 for(i
= 0; i
< ARRAY_SIZE(tvnorms
); i
++)
1295 if (*id
& tvnorms
[i
].id
)
1297 if (i
== ARRAY_SIZE(tvnorms
))
1300 mutex_lock(&core
->lock
);
1301 cx88_set_tvnorm(core
,&tvnorms
[i
]);
1302 mutex_unlock(&core
->lock
);
1306 /* ------ input switching ---------- */
1307 case VIDIOC_ENUMINPUT
:
1309 static const char *iname
[] = {
1310 [ CX88_VMUX_COMPOSITE1
] = "Composite1",
1311 [ CX88_VMUX_COMPOSITE2
] = "Composite2",
1312 [ CX88_VMUX_COMPOSITE3
] = "Composite3",
1313 [ CX88_VMUX_COMPOSITE4
] = "Composite4",
1314 [ CX88_VMUX_SVIDEO
] = "S-Video",
1315 [ CX88_VMUX_TELEVISION
] = "Television",
1316 [ CX88_VMUX_CABLE
] = "Cable TV",
1317 [ CX88_VMUX_DVB
] = "DVB",
1318 [ CX88_VMUX_DEBUG
] = "for debug only",
1320 struct v4l2_input
*i
= arg
;
1326 if (0 == INPUT(n
)->type
)
1328 memset(i
,0,sizeof(*i
));
1330 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1331 strcpy(i
->name
,iname
[INPUT(n
)->type
]);
1332 if ((CX88_VMUX_TELEVISION
== INPUT(n
)->type
) ||
1333 (CX88_VMUX_CABLE
== INPUT(n
)->type
))
1334 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1335 for (n
= 0; n
< ARRAY_SIZE(tvnorms
); n
++)
1336 i
->std
|= tvnorms
[n
].id
;
1339 case VIDIOC_G_INPUT
:
1341 unsigned int *i
= arg
;
1346 case VIDIOC_S_INPUT
:
1348 unsigned int *i
= arg
;
1352 mutex_lock(&core
->lock
);
1353 cx88_newstation(core
);
1355 mutex_unlock(&core
->lock
);
1361 /* --- controls ---------------------------------------------- */
1362 case VIDIOC_QUERYCTRL
:
1364 struct v4l2_queryctrl
*c
= arg
;
1367 if (c
->id
< V4L2_CID_BASE
||
1368 c
->id
>= V4L2_CID_LASTP1
)
1370 for (i
= 0; i
< CX8800_CTLS
; i
++)
1371 if (cx8800_ctls
[i
].v
.id
== c
->id
)
1373 if (i
== CX8800_CTLS
) {
1377 *c
= cx8800_ctls
[i
].v
;
1381 return get_control(core
,arg
);
1383 return set_control(core
,arg
);
1385 /* --- tuner ioctls ------------------------------------------ */
1386 case VIDIOC_G_TUNER
:
1388 struct v4l2_tuner
*t
= arg
;
1391 if (UNSET
== core
->tuner_type
)
1396 memset(t
,0,sizeof(*t
));
1397 strcpy(t
->name
, "Television");
1398 t
->type
= V4L2_TUNER_ANALOG_TV
;
1399 t
->capability
= V4L2_TUNER_CAP_NORM
;
1400 t
->rangehigh
= 0xffffffffUL
;
1402 cx88_get_stereo(core
,t
);
1403 reg
= cx_read(MO_DEVICE_STATUS
);
1404 t
->signal
= (reg
& (1<<5)) ? 0xffff : 0x0000;
1407 case VIDIOC_S_TUNER
:
1409 struct v4l2_tuner
*t
= arg
;
1411 if (UNSET
== core
->tuner_type
)
1415 cx88_set_stereo(core
, t
->audmode
, 1);
1418 case VIDIOC_G_FREQUENCY
:
1420 struct v4l2_frequency
*f
= arg
;
1422 memset(f
,0,sizeof(*f
));
1424 if (UNSET
== core
->tuner_type
)
1427 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1428 f
->type
= radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1429 f
->frequency
= core
->freq
;
1431 cx88_call_i2c_clients(core
,VIDIOC_G_FREQUENCY
,f
);
1435 case VIDIOC_S_FREQUENCY
:
1437 struct v4l2_frequency
*f
= arg
;
1439 if (UNSET
== core
->tuner_type
)
1443 if (0 == radio
&& f
->type
!= V4L2_TUNER_ANALOG_TV
)
1445 if (1 == radio
&& f
->type
!= V4L2_TUNER_RADIO
)
1447 mutex_lock(&core
->lock
);
1448 core
->freq
= f
->frequency
;
1449 cx88_newstation(core
);
1450 cx88_call_i2c_clients(core
,VIDIOC_S_FREQUENCY
,f
);
1452 /* When changing channels it is required to reset TVAUDIO */
1454 cx88_set_tvaudio(core
);
1456 mutex_unlock(&core
->lock
);
1461 return v4l_compat_translate_ioctl(inode
,file
,cmd
,arg
,
1467 static int video_ioctl(struct inode
*inode
, struct file
*file
,
1468 unsigned int cmd
, unsigned long arg
)
1472 retval
=video_usercopy(inode
, file
, cmd
, arg
, video_do_ioctl
);
1474 if (video_debug
> 1) {
1476 v4l_print_ioctl("cx88(err)", cmd
);
1477 printk(KERN_DEBUG
"cx88(err): errcode=%d\n",retval
);
1478 } else if (_IOC_DIR(cmd
) & _IOC_READ
)
1479 v4l_printk_ioctl_arg("cx88(r)",cmd
, (void *)arg
);
1485 /* ----------------------------------------------------------- */
1487 static int radio_do_ioctl(struct inode
*inode
, struct file
*file
,
1488 unsigned int cmd
, void *arg
)
1490 struct cx8800_fh
*fh
= file
->private_data
;
1491 struct cx8800_dev
*dev
= fh
->dev
;
1492 struct cx88_core
*core
= dev
->core
;
1494 if (video_debug
> 1)
1495 v4l_print_ioctl(core
->name
,cmd
);
1498 case VIDIOC_QUERYCAP
:
1500 struct v4l2_capability
*cap
= arg
;
1502 memset(cap
,0,sizeof(*cap
));
1503 strcpy(cap
->driver
, "cx8800");
1504 strlcpy(cap
->card
, cx88_boards
[core
->board
].name
,
1506 sprintf(cap
->bus_info
,"PCI:%s", pci_name(dev
->pci
));
1507 cap
->version
= CX88_VERSION_CODE
;
1508 cap
->capabilities
= V4L2_CAP_TUNER
;
1511 case VIDIOC_G_TUNER
:
1513 struct v4l2_tuner
*t
= arg
;
1518 memset(t
,0,sizeof(*t
));
1519 strcpy(t
->name
, "Radio");
1520 t
->type
= V4L2_TUNER_RADIO
;
1522 cx88_call_i2c_clients(core
,VIDIOC_G_TUNER
,t
);
1525 case VIDIOC_ENUMINPUT
:
1527 struct v4l2_input
*i
= arg
;
1531 strcpy(i
->name
,"Radio");
1532 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1535 case VIDIOC_G_INPUT
:
1541 case VIDIOC_G_AUDIO
:
1543 struct v4l2_audio
*a
= arg
;
1545 memset(a
,0,sizeof(*a
));
1546 strcpy(a
->name
,"Radio");
1551 v4l2_std_id
*id
= arg
;
1558 struct video_tuner
*v
= arg
;
1560 if (v
->tuner
) /* Only tuner 0 */
1563 cx88_call_i2c_clients(core
,VIDIOCSTUNER
,v
);
1567 case VIDIOC_S_TUNER
:
1569 struct v4l2_tuner
*t
= arg
;
1574 cx88_call_i2c_clients(core
,VIDIOC_S_TUNER
,t
);
1579 case VIDIOC_S_AUDIO
:
1580 case VIDIOC_S_INPUT
:
1584 case VIDIOC_QUERYCTRL
:
1586 struct v4l2_queryctrl
*c
= arg
;
1589 if (c
->id
< V4L2_CID_BASE
||
1590 c
->id
>= V4L2_CID_LASTP1
)
1592 if (c
->id
== V4L2_CID_AUDIO_MUTE
) {
1593 for (i
= 0; i
< CX8800_CTLS
; i
++)
1594 if (cx8800_ctls
[i
].v
.id
== c
->id
)
1596 *c
= cx8800_ctls
[i
].v
;
1605 case VIDIOC_G_FREQUENCY
:
1606 case VIDIOC_S_FREQUENCY
:
1607 return video_do_ioctl(inode
,file
,cmd
,arg
);
1610 return v4l_compat_translate_ioctl(inode
,file
,cmd
,arg
,
1616 static int radio_ioctl(struct inode
*inode
, struct file
*file
,
1617 unsigned int cmd
, unsigned long arg
)
1619 return video_usercopy(inode
, file
, cmd
, arg
, radio_do_ioctl
);
1622 /* ----------------------------------------------------------- */
1624 static void cx8800_vid_timeout(unsigned long data
)
1626 struct cx8800_dev
*dev
= (struct cx8800_dev
*)data
;
1627 struct cx88_core
*core
= dev
->core
;
1628 struct cx88_dmaqueue
*q
= &dev
->vidq
;
1629 struct cx88_buffer
*buf
;
1630 unsigned long flags
;
1632 cx88_sram_channel_dump(core
, &cx88_sram_channels
[SRAM_CH21
]);
1634 cx_clear(MO_VID_DMACNTRL
, 0x11);
1635 cx_clear(VID_CAPTURE_CONTROL
, 0x06);
1637 spin_lock_irqsave(&dev
->slock
,flags
);
1638 while (!list_empty(&q
->active
)) {
1639 buf
= list_entry(q
->active
.next
, struct cx88_buffer
, vb
.queue
);
1640 list_del(&buf
->vb
.queue
);
1641 buf
->vb
.state
= STATE_ERROR
;
1642 wake_up(&buf
->vb
.done
);
1643 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core
->name
,
1644 buf
, buf
->vb
.i
, (unsigned long)buf
->risc
.dma
);
1646 restart_video_queue(dev
,q
);
1647 spin_unlock_irqrestore(&dev
->slock
,flags
);
1650 static char *cx88_vid_irqs
[32] = {
1651 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1652 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1653 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1654 "y_sync", "u_sync", "v_sync", "vbi_sync",
1655 "opc_err", "par_err", "rip_err", "pci_abort",
1658 static void cx8800_vid_irq(struct cx8800_dev
*dev
)
1660 struct cx88_core
*core
= dev
->core
;
1661 u32 status
, mask
, count
;
1663 status
= cx_read(MO_VID_INTSTAT
);
1664 mask
= cx_read(MO_VID_INTMSK
);
1665 if (0 == (status
& mask
))
1667 cx_write(MO_VID_INTSTAT
, status
);
1668 if (irq_debug
|| (status
& mask
& ~0xff))
1669 cx88_print_irqbits(core
->name
, "irq vid",
1670 cx88_vid_irqs
, status
, mask
);
1672 /* risc op code error */
1673 if (status
& (1 << 16)) {
1674 printk(KERN_WARNING
"%s/0: video risc op code error\n",core
->name
);
1675 cx_clear(MO_VID_DMACNTRL
, 0x11);
1676 cx_clear(VID_CAPTURE_CONTROL
, 0x06);
1677 cx88_sram_channel_dump(core
, &cx88_sram_channels
[SRAM_CH21
]);
1681 if (status
& 0x01) {
1682 spin_lock(&dev
->slock
);
1683 count
= cx_read(MO_VIDY_GPCNT
);
1684 cx88_wakeup(core
, &dev
->vidq
, count
);
1685 spin_unlock(&dev
->slock
);
1689 if (status
& 0x08) {
1690 spin_lock(&dev
->slock
);
1691 count
= cx_read(MO_VBI_GPCNT
);
1692 cx88_wakeup(core
, &dev
->vbiq
, count
);
1693 spin_unlock(&dev
->slock
);
1697 if (status
& 0x10) {
1698 dprintk(2,"stopper video\n");
1699 spin_lock(&dev
->slock
);
1700 restart_video_queue(dev
,&dev
->vidq
);
1701 spin_unlock(&dev
->slock
);
1705 if (status
& 0x80) {
1706 dprintk(2,"stopper vbi\n");
1707 spin_lock(&dev
->slock
);
1708 cx8800_restart_vbi_queue(dev
,&dev
->vbiq
);
1709 spin_unlock(&dev
->slock
);
1713 static irqreturn_t
cx8800_irq(int irq
, void *dev_id
, struct pt_regs
*regs
)
1715 struct cx8800_dev
*dev
= dev_id
;
1716 struct cx88_core
*core
= dev
->core
;
1718 int loop
, handled
= 0;
1720 for (loop
= 0; loop
< 10; loop
++) {
1721 status
= cx_read(MO_PCI_INTSTAT
) & (core
->pci_irqmask
| 0x01);
1724 cx_write(MO_PCI_INTSTAT
, status
);
1727 if (status
& core
->pci_irqmask
)
1728 cx88_core_irq(core
,status
);
1730 cx8800_vid_irq(dev
);
1733 printk(KERN_WARNING
"%s/0: irq loop -- clearing mask\n",
1735 cx_write(MO_PCI_INTMSK
,0);
1739 return IRQ_RETVAL(handled
);
1742 /* ----------------------------------------------------------- */
1743 /* exported stuff */
1745 static struct file_operations video_fops
=
1747 .owner
= THIS_MODULE
,
1749 .release
= video_release
,
1753 .ioctl
= video_ioctl
,
1754 .compat_ioctl
= v4l_compat_ioctl32
,
1755 .llseek
= no_llseek
,
1758 static struct video_device cx8800_video_template
=
1760 .name
= "cx8800-video",
1761 .type
= VID_TYPE_CAPTURE
|VID_TYPE_TUNER
|VID_TYPE_SCALES
,
1763 .fops
= &video_fops
,
1767 static struct video_device cx8800_vbi_template
=
1769 .name
= "cx8800-vbi",
1770 .type
= VID_TYPE_TELETEXT
|VID_TYPE_TUNER
,
1772 .fops
= &video_fops
,
1776 static struct file_operations radio_fops
=
1778 .owner
= THIS_MODULE
,
1780 .release
= video_release
,
1781 .ioctl
= radio_ioctl
,
1782 .compat_ioctl
= v4l_compat_ioctl32
,
1783 .llseek
= no_llseek
,
1786 static struct video_device cx8800_radio_template
=
1788 .name
= "cx8800-radio",
1789 .type
= VID_TYPE_TUNER
,
1791 .fops
= &radio_fops
,
1795 /* ----------------------------------------------------------- */
1797 static void cx8800_unregister_video(struct cx8800_dev
*dev
)
1799 if (dev
->radio_dev
) {
1800 if (-1 != dev
->radio_dev
->minor
)
1801 video_unregister_device(dev
->radio_dev
);
1803 video_device_release(dev
->radio_dev
);
1804 dev
->radio_dev
= NULL
;
1807 if (-1 != dev
->vbi_dev
->minor
)
1808 video_unregister_device(dev
->vbi_dev
);
1810 video_device_release(dev
->vbi_dev
);
1811 dev
->vbi_dev
= NULL
;
1813 if (dev
->video_dev
) {
1814 if (-1 != dev
->video_dev
->minor
)
1815 video_unregister_device(dev
->video_dev
);
1817 video_device_release(dev
->video_dev
);
1818 dev
->video_dev
= NULL
;
1822 static int __devinit
cx8800_initdev(struct pci_dev
*pci_dev
,
1823 const struct pci_device_id
*pci_id
)
1825 struct cx8800_dev
*dev
;
1826 struct cx88_core
*core
;
1829 dev
= kzalloc(sizeof(*dev
),GFP_KERNEL
);
1835 if (pci_enable_device(pci_dev
)) {
1839 core
= cx88_core_get(dev
->pci
);
1846 /* print pci info */
1847 pci_read_config_byte(pci_dev
, PCI_CLASS_REVISION
, &dev
->pci_rev
);
1848 pci_read_config_byte(pci_dev
, PCI_LATENCY_TIMER
, &dev
->pci_lat
);
1849 printk(KERN_INFO
"%s/0: found at %s, rev: %d, irq: %d, "
1850 "latency: %d, mmio: 0x%lx\n", core
->name
,
1851 pci_name(pci_dev
), dev
->pci_rev
, pci_dev
->irq
,
1852 dev
->pci_lat
,pci_resource_start(pci_dev
,0));
1854 pci_set_master(pci_dev
);
1855 if (!pci_dma_supported(pci_dev
,0xffffffff)) {
1856 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core
->name
);
1861 /* initialize driver struct */
1862 spin_lock_init(&dev
->slock
);
1863 core
->tvnorm
= tvnorms
;
1865 /* init video dma queues */
1866 INIT_LIST_HEAD(&dev
->vidq
.active
);
1867 INIT_LIST_HEAD(&dev
->vidq
.queued
);
1868 dev
->vidq
.timeout
.function
= cx8800_vid_timeout
;
1869 dev
->vidq
.timeout
.data
= (unsigned long)dev
;
1870 init_timer(&dev
->vidq
.timeout
);
1871 cx88_risc_stopper(dev
->pci
,&dev
->vidq
.stopper
,
1872 MO_VID_DMACNTRL
,0x11,0x00);
1874 /* init vbi dma queues */
1875 INIT_LIST_HEAD(&dev
->vbiq
.active
);
1876 INIT_LIST_HEAD(&dev
->vbiq
.queued
);
1877 dev
->vbiq
.timeout
.function
= cx8800_vbi_timeout
;
1878 dev
->vbiq
.timeout
.data
= (unsigned long)dev
;
1879 init_timer(&dev
->vbiq
.timeout
);
1880 cx88_risc_stopper(dev
->pci
,&dev
->vbiq
.stopper
,
1881 MO_VID_DMACNTRL
,0x88,0x00);
1884 err
= request_irq(pci_dev
->irq
, cx8800_irq
,
1885 SA_SHIRQ
| SA_INTERRUPT
, core
->name
, dev
);
1887 printk(KERN_ERR
"%s: can't get IRQ %d\n",
1888 core
->name
,pci_dev
->irq
);
1891 cx_set(MO_PCI_INTMSK
, core
->pci_irqmask
);
1893 /* load and configure helper modules */
1894 if (TUNER_ABSENT
!= core
->tuner_type
)
1895 request_module("tuner");
1896 if (core
->tda9887_conf
)
1897 request_module("tda9887");
1899 /* register v4l devices */
1900 dev
->video_dev
= cx88_vdev_init(core
,dev
->pci
,
1901 &cx8800_video_template
,"video");
1902 err
= video_register_device(dev
->video_dev
,VFL_TYPE_GRABBER
,
1903 video_nr
[core
->nr
]);
1905 printk(KERN_INFO
"%s: can't register video device\n",
1909 printk(KERN_INFO
"%s/0: registered device video%d [v4l2]\n",
1910 core
->name
,dev
->video_dev
->minor
& 0x1f);
1912 dev
->vbi_dev
= cx88_vdev_init(core
,dev
->pci
,&cx8800_vbi_template
,"vbi");
1913 err
= video_register_device(dev
->vbi_dev
,VFL_TYPE_VBI
,
1916 printk(KERN_INFO
"%s/0: can't register vbi device\n",
1920 printk(KERN_INFO
"%s/0: registered device vbi%d\n",
1921 core
->name
,dev
->vbi_dev
->minor
& 0x1f);
1923 if (core
->has_radio
) {
1924 dev
->radio_dev
= cx88_vdev_init(core
,dev
->pci
,
1925 &cx8800_radio_template
,"radio");
1926 err
= video_register_device(dev
->radio_dev
,VFL_TYPE_RADIO
,
1927 radio_nr
[core
->nr
]);
1929 printk(KERN_INFO
"%s/0: can't register radio device\n",
1933 printk(KERN_INFO
"%s/0: registered device radio%d\n",
1934 core
->name
,dev
->radio_dev
->minor
& 0x1f);
1937 /* everything worked */
1938 list_add_tail(&dev
->devlist
,&cx8800_devlist
);
1939 pci_set_drvdata(pci_dev
,dev
);
1941 /* initial device configuration */
1942 mutex_lock(&core
->lock
);
1943 cx88_set_tvnorm(core
,tvnorms
);
1944 init_controls(core
);
1946 mutex_unlock(&core
->lock
);
1948 /* start tvaudio thread */
1949 if (core
->tuner_type
!= TUNER_ABSENT
)
1950 core
->kthread
= kthread_run(cx88_audio_thread
, core
, "cx88 tvaudio");
1954 cx8800_unregister_video(dev
);
1955 free_irq(pci_dev
->irq
, dev
);
1957 cx88_core_put(core
,dev
->pci
);
1963 static void __devexit
cx8800_finidev(struct pci_dev
*pci_dev
)
1965 struct cx8800_dev
*dev
= pci_get_drvdata(pci_dev
);
1966 struct cx88_core
*core
= dev
->core
;
1969 if (core
->kthread
) {
1970 kthread_stop(core
->kthread
);
1971 core
->kthread
= NULL
;
1974 cx88_shutdown(core
); /* FIXME */
1975 pci_disable_device(pci_dev
);
1977 /* unregister stuff */
1979 free_irq(pci_dev
->irq
, dev
);
1980 cx8800_unregister_video(dev
);
1981 pci_set_drvdata(pci_dev
, NULL
);
1984 btcx_riscmem_free(dev
->pci
,&dev
->vidq
.stopper
);
1985 list_del(&dev
->devlist
);
1986 cx88_core_put(core
,dev
->pci
);
1990 static int cx8800_suspend(struct pci_dev
*pci_dev
, pm_message_t state
)
1992 struct cx8800_dev
*dev
= pci_get_drvdata(pci_dev
);
1993 struct cx88_core
*core
= dev
->core
;
1995 /* stop video+vbi capture */
1996 spin_lock(&dev
->slock
);
1997 if (!list_empty(&dev
->vidq
.active
)) {
1998 printk("%s: suspend video\n", core
->name
);
1999 stop_video_dma(dev
);
2000 del_timer(&dev
->vidq
.timeout
);
2002 if (!list_empty(&dev
->vbiq
.active
)) {
2003 printk("%s: suspend vbi\n", core
->name
);
2004 cx8800_stop_vbi_dma(dev
);
2005 del_timer(&dev
->vbiq
.timeout
);
2007 spin_unlock(&dev
->slock
);
2009 /* FIXME -- shutdown device */
2010 cx88_shutdown(core
);
2012 pci_save_state(pci_dev
);
2013 if (0 != pci_set_power_state(pci_dev
, pci_choose_state(pci_dev
, state
))) {
2014 pci_disable_device(pci_dev
);
2015 dev
->state
.disabled
= 1;
2020 static int cx8800_resume(struct pci_dev
*pci_dev
)
2022 struct cx8800_dev
*dev
= pci_get_drvdata(pci_dev
);
2023 struct cx88_core
*core
= dev
->core
;
2026 if (dev
->state
.disabled
) {
2027 err
=pci_enable_device(pci_dev
);
2029 printk(KERN_ERR
"%s: can't enable device\n",
2034 dev
->state
.disabled
= 0;
2036 err
= pci_set_power_state(pci_dev
, PCI_D0
);
2038 printk(KERN_ERR
"%s: can't enable device\n",
2041 pci_disable_device(pci_dev
);
2042 dev
->state
.disabled
= 1;
2046 pci_restore_state(pci_dev
);
2048 /* FIXME: re-initialize hardware */
2051 /* restart video+vbi capture */
2052 spin_lock(&dev
->slock
);
2053 if (!list_empty(&dev
->vidq
.active
)) {
2054 printk("%s: resume video\n", core
->name
);
2055 restart_video_queue(dev
,&dev
->vidq
);
2057 if (!list_empty(&dev
->vbiq
.active
)) {
2058 printk("%s: resume vbi\n", core
->name
);
2059 cx8800_restart_vbi_queue(dev
,&dev
->vbiq
);
2061 spin_unlock(&dev
->slock
);
2066 /* ----------------------------------------------------------- */
2068 static struct pci_device_id cx8800_pci_tbl
[] = {
2072 .subvendor
= PCI_ANY_ID
,
2073 .subdevice
= PCI_ANY_ID
,
2075 /* --- end of list --- */
2078 MODULE_DEVICE_TABLE(pci
, cx8800_pci_tbl
);
2080 static struct pci_driver cx8800_pci_driver
= {
2082 .id_table
= cx8800_pci_tbl
,
2083 .probe
= cx8800_initdev
,
2084 .remove
= __devexit_p(cx8800_finidev
),
2086 .suspend
= cx8800_suspend
,
2087 .resume
= cx8800_resume
,
2090 static int cx8800_init(void)
2092 printk(KERN_INFO
"cx2388x v4l2 driver version %d.%d.%d loaded\n",
2093 (CX88_VERSION_CODE
>> 16) & 0xff,
2094 (CX88_VERSION_CODE
>> 8) & 0xff,
2095 CX88_VERSION_CODE
& 0xff);
2097 printk(KERN_INFO
"cx2388x: snapshot date %04d-%02d-%02d\n",
2098 SNAPSHOT
/10000, (SNAPSHOT
/100)%100, SNAPSHOT
%100);
2100 return pci_register_driver(&cx8800_pci_driver
);
2103 static void cx8800_fini(void)
2105 pci_unregister_driver(&cx8800_pci_driver
);
2108 module_init(cx8800_init
);
2109 module_exit(cx8800_fini
);
2111 EXPORT_SYMBOL(cx88_do_ioctl
);
2113 /* ----------------------------------------------------------- */
2118 * 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