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 const u32 cx88_user_ctrls
[] = {
336 V4L2_CID_AUDIO_VOLUME
,
337 V4L2_CID_AUDIO_BALANCE
,
341 EXPORT_SYMBOL(cx88_user_ctrls
);
343 static const u32
*ctrl_classes
[] = {
348 int cx8800_ctrl_query(struct v4l2_queryctrl
*qctrl
)
352 if (qctrl
->id
< V4L2_CID_BASE
||
353 qctrl
->id
>= V4L2_CID_LASTP1
)
355 for (i
= 0; i
< CX8800_CTLS
; i
++)
356 if (cx8800_ctls
[i
].v
.id
== qctrl
->id
)
358 if (i
== CX8800_CTLS
) {
362 *qctrl
= cx8800_ctls
[i
].v
;
365 EXPORT_SYMBOL(cx8800_ctrl_query
);
367 static int cx88_queryctrl(struct v4l2_queryctrl
*qctrl
)
369 qctrl
->id
= v4l2_ctrl_next(ctrl_classes
, qctrl
->id
);
372 return cx8800_ctrl_query(qctrl
);
375 /* ------------------------------------------------------------------- */
376 /* resource management */
378 static int res_get(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
, unsigned int bit
)
380 struct cx88_core
*core
= dev
->core
;
381 if (fh
->resources
& bit
)
382 /* have it already allocated */
386 mutex_lock(&core
->lock
);
387 if (dev
->resources
& bit
) {
388 /* no, someone else uses it */
389 mutex_unlock(&core
->lock
);
392 /* it's free, grab it */
393 fh
->resources
|= bit
;
394 dev
->resources
|= bit
;
395 dprintk(1,"res: get %d\n",bit
);
396 mutex_unlock(&core
->lock
);
401 int res_check(struct cx8800_fh
*fh
, unsigned int bit
)
403 return (fh
->resources
& bit
);
407 int res_locked(struct cx8800_dev
*dev
, unsigned int bit
)
409 return (dev
->resources
& bit
);
413 void res_free(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
, unsigned int bits
)
415 struct cx88_core
*core
= dev
->core
;
416 BUG_ON((fh
->resources
& bits
) != bits
);
418 mutex_lock(&core
->lock
);
419 fh
->resources
&= ~bits
;
420 dev
->resources
&= ~bits
;
421 dprintk(1,"res: put %d\n",bits
);
422 mutex_unlock(&core
->lock
);
425 /* ------------------------------------------------------------------ */
427 /* static int video_mux(struct cx8800_dev *dev, unsigned int input) */
428 static int video_mux(struct cx88_core
*core
, unsigned int input
)
430 /* struct cx88_core *core = dev->core; */
432 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
433 input
, INPUT(input
)->vmux
,
434 INPUT(input
)->gpio0
,INPUT(input
)->gpio1
,
435 INPUT(input
)->gpio2
,INPUT(input
)->gpio3
);
437 cx_andor(MO_INPUT_FORMAT
, 0x03 << 14, INPUT(input
)->vmux
<< 14);
438 cx_write(MO_GP3_IO
, INPUT(input
)->gpio3
);
439 cx_write(MO_GP0_IO
, INPUT(input
)->gpio0
);
440 cx_write(MO_GP1_IO
, INPUT(input
)->gpio1
);
441 cx_write(MO_GP2_IO
, INPUT(input
)->gpio2
);
443 switch (INPUT(input
)->type
) {
444 case CX88_VMUX_SVIDEO
:
445 cx_set(MO_AFECFG_IO
, 0x00000001);
446 cx_set(MO_INPUT_FORMAT
, 0x00010010);
447 cx_set(MO_FILTER_EVEN
, 0x00002020);
448 cx_set(MO_FILTER_ODD
, 0x00002020);
451 cx_clear(MO_AFECFG_IO
, 0x00000001);
452 cx_clear(MO_INPUT_FORMAT
, 0x00010010);
453 cx_clear(MO_FILTER_EVEN
, 0x00002020);
454 cx_clear(MO_FILTER_ODD
, 0x00002020);
460 /* ------------------------------------------------------------------ */
462 static int start_video_dma(struct cx8800_dev
*dev
,
463 struct cx88_dmaqueue
*q
,
464 struct cx88_buffer
*buf
)
466 struct cx88_core
*core
= dev
->core
;
468 /* setup fifo + format */
469 cx88_sram_channel_setup(core
, &cx88_sram_channels
[SRAM_CH21
],
470 buf
->bpl
, buf
->risc
.dma
);
471 cx88_set_scale(core
, buf
->vb
.width
, buf
->vb
.height
, buf
->vb
.field
);
472 cx_write(MO_COLOR_CTRL
, buf
->fmt
->cxformat
| ColorFormatGamma
);
475 cx_write(MO_VIDY_GPCNTRL
,GP_COUNT_CONTROL_RESET
);
479 cx_set(MO_PCI_INTMSK
, core
->pci_irqmask
| 0x01);
481 /* Enables corresponding bits at PCI_INT_STAT:
482 bits 0 to 4: video, audio, transport stream, VIP, Host
484 bits 8 and 9: DMA complete for: SRC, DST
485 bits 10 and 11: BERR signal asserted for RISC: RD, WR
486 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
488 cx_set(MO_VID_INTMSK
, 0x0f0011);
491 cx_set(VID_CAPTURE_CONTROL
,0x06);
494 cx_set(MO_DEV_CNTRL2
, (1<<5));
495 cx_set(MO_VID_DMACNTRL
, 0x11); /* Planar Y and packed FIFO and RISC enable */
501 static int stop_video_dma(struct cx8800_dev
*dev
)
503 struct cx88_core
*core
= dev
->core
;
506 cx_clear(MO_VID_DMACNTRL
, 0x11);
508 /* disable capture */
509 cx_clear(VID_CAPTURE_CONTROL
,0x06);
512 cx_clear(MO_PCI_INTMSK
, 0x000001);
513 cx_clear(MO_VID_INTMSK
, 0x0f0011);
518 static int restart_video_queue(struct cx8800_dev
*dev
,
519 struct cx88_dmaqueue
*q
)
521 struct cx88_core
*core
= dev
->core
;
522 struct cx88_buffer
*buf
, *prev
;
523 struct list_head
*item
;
525 if (!list_empty(&q
->active
)) {
526 buf
= list_entry(q
->active
.next
, struct cx88_buffer
, vb
.queue
);
527 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
529 start_video_dma(dev
, q
, buf
);
530 list_for_each(item
,&q
->active
) {
531 buf
= list_entry(item
, struct cx88_buffer
, vb
.queue
);
532 buf
->count
= q
->count
++;
534 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
540 if (list_empty(&q
->queued
))
542 buf
= list_entry(q
->queued
.next
, struct cx88_buffer
, vb
.queue
);
544 list_move_tail(&buf
->vb
.queue
, &q
->active
);
545 start_video_dma(dev
, q
, buf
);
546 buf
->vb
.state
= STATE_ACTIVE
;
547 buf
->count
= q
->count
++;
548 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
549 dprintk(2,"[%p/%d] restart_queue - first active\n",
552 } else if (prev
->vb
.width
== buf
->vb
.width
&&
553 prev
->vb
.height
== buf
->vb
.height
&&
554 prev
->fmt
== buf
->fmt
) {
555 list_move_tail(&buf
->vb
.queue
, &q
->active
);
556 buf
->vb
.state
= STATE_ACTIVE
;
557 buf
->count
= q
->count
++;
558 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
559 dprintk(2,"[%p/%d] restart_queue - move to active\n",
568 /* ------------------------------------------------------------------ */
571 buffer_setup(struct videobuf_queue
*q
, unsigned int *count
, unsigned int *size
)
573 struct cx8800_fh
*fh
= q
->priv_data
;
575 *size
= fh
->fmt
->depth
*fh
->width
*fh
->height
>> 3;
578 while (*size
* *count
> vid_limit
* 1024 * 1024)
584 buffer_prepare(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
585 enum v4l2_field field
)
587 struct cx8800_fh
*fh
= q
->priv_data
;
588 struct cx8800_dev
*dev
= fh
->dev
;
589 struct cx88_core
*core
= dev
->core
;
590 struct cx88_buffer
*buf
= container_of(vb
,struct cx88_buffer
,vb
);
591 int rc
, init_buffer
= 0;
593 BUG_ON(NULL
== fh
->fmt
);
594 if (fh
->width
< 48 || fh
->width
> norm_maxw(core
->tvnorm
) ||
595 fh
->height
< 32 || fh
->height
> norm_maxh(core
->tvnorm
))
597 buf
->vb
.size
= (fh
->width
* fh
->height
* fh
->fmt
->depth
) >> 3;
598 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
601 if (buf
->fmt
!= fh
->fmt
||
602 buf
->vb
.width
!= fh
->width
||
603 buf
->vb
.height
!= fh
->height
||
604 buf
->vb
.field
!= field
) {
606 buf
->vb
.width
= fh
->width
;
607 buf
->vb
.height
= fh
->height
;
608 buf
->vb
.field
= field
;
612 if (STATE_NEEDS_INIT
== buf
->vb
.state
) {
614 if (0 != (rc
= videobuf_iolock(q
,&buf
->vb
,NULL
)))
619 buf
->bpl
= buf
->vb
.width
* buf
->fmt
->depth
>> 3;
620 switch (buf
->vb
.field
) {
622 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
623 buf
->vb
.dma
.sglist
, 0, UNSET
,
624 buf
->bpl
, 0, buf
->vb
.height
);
626 case V4L2_FIELD_BOTTOM
:
627 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
628 buf
->vb
.dma
.sglist
, UNSET
, 0,
629 buf
->bpl
, 0, buf
->vb
.height
);
631 case V4L2_FIELD_INTERLACED
:
632 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
633 buf
->vb
.dma
.sglist
, 0, buf
->bpl
,
635 buf
->vb
.height
>> 1);
637 case V4L2_FIELD_SEQ_TB
:
638 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
640 0, buf
->bpl
* (buf
->vb
.height
>> 1),
642 buf
->vb
.height
>> 1);
644 case V4L2_FIELD_SEQ_BT
:
645 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
647 buf
->bpl
* (buf
->vb
.height
>> 1), 0,
649 buf
->vb
.height
>> 1);
655 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
657 fh
->width
, fh
->height
, fh
->fmt
->depth
, fh
->fmt
->name
,
658 (unsigned long)buf
->risc
.dma
);
660 buf
->vb
.state
= STATE_PREPARED
;
664 cx88_free_buffer(q
,buf
);
669 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
671 struct cx88_buffer
*buf
= container_of(vb
,struct cx88_buffer
,vb
);
672 struct cx88_buffer
*prev
;
673 struct cx8800_fh
*fh
= vq
->priv_data
;
674 struct cx8800_dev
*dev
= fh
->dev
;
675 struct cx88_core
*core
= dev
->core
;
676 struct cx88_dmaqueue
*q
= &dev
->vidq
;
678 /* add jump to stopper */
679 buf
->risc
.jmp
[0] = cpu_to_le32(RISC_JUMP
| RISC_IRQ1
| RISC_CNT_INC
);
680 buf
->risc
.jmp
[1] = cpu_to_le32(q
->stopper
.dma
);
682 if (!list_empty(&q
->queued
)) {
683 list_add_tail(&buf
->vb
.queue
,&q
->queued
);
684 buf
->vb
.state
= STATE_QUEUED
;
685 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
688 } else if (list_empty(&q
->active
)) {
689 list_add_tail(&buf
->vb
.queue
,&q
->active
);
690 start_video_dma(dev
, q
, buf
);
691 buf
->vb
.state
= STATE_ACTIVE
;
692 buf
->count
= q
->count
++;
693 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
694 dprintk(2,"[%p/%d] buffer_queue - first active\n",
698 prev
= list_entry(q
->active
.prev
, struct cx88_buffer
, vb
.queue
);
699 if (prev
->vb
.width
== buf
->vb
.width
&&
700 prev
->vb
.height
== buf
->vb
.height
&&
701 prev
->fmt
== buf
->fmt
) {
702 list_add_tail(&buf
->vb
.queue
,&q
->active
);
703 buf
->vb
.state
= STATE_ACTIVE
;
704 buf
->count
= q
->count
++;
705 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
706 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
710 list_add_tail(&buf
->vb
.queue
,&q
->queued
);
711 buf
->vb
.state
= STATE_QUEUED
;
712 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
718 static void buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
720 struct cx88_buffer
*buf
= container_of(vb
,struct cx88_buffer
,vb
);
722 cx88_free_buffer(q
,buf
);
725 static struct videobuf_queue_ops cx8800_video_qops
= {
726 .buf_setup
= buffer_setup
,
727 .buf_prepare
= buffer_prepare
,
728 .buf_queue
= buffer_queue
,
729 .buf_release
= buffer_release
,
732 /* ------------------------------------------------------------------ */
735 /* ------------------------------------------------------------------ */
737 static struct videobuf_queue
* get_queue(struct cx8800_fh
*fh
)
740 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
742 case V4L2_BUF_TYPE_VBI_CAPTURE
:
750 static int get_ressource(struct cx8800_fh
*fh
)
753 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
754 return RESOURCE_VIDEO
;
755 case V4L2_BUF_TYPE_VBI_CAPTURE
:
763 static int video_open(struct inode
*inode
, struct file
*file
)
765 int minor
= iminor(inode
);
766 struct cx8800_dev
*h
,*dev
= NULL
;
767 struct cx88_core
*core
;
768 struct cx8800_fh
*fh
;
769 struct list_head
*list
;
770 enum v4l2_buf_type type
= 0;
773 list_for_each(list
,&cx8800_devlist
) {
774 h
= list_entry(list
, struct cx8800_dev
, devlist
);
775 if (h
->video_dev
->minor
== minor
) {
777 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
779 if (h
->vbi_dev
->minor
== minor
) {
781 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
784 h
->radio_dev
->minor
== minor
) {
794 dprintk(1,"open minor=%d radio=%d type=%s\n",
795 minor
,radio
,v4l2_type_names
[type
]);
797 /* allocate + initialize per filehandle data */
798 fh
= kzalloc(sizeof(*fh
),GFP_KERNEL
);
801 file
->private_data
= fh
;
807 fh
->fmt
= format_by_fourcc(V4L2_PIX_FMT_BGR24
);
809 videobuf_queue_init(&fh
->vidq
, &cx8800_video_qops
,
810 dev
->pci
, &dev
->slock
,
811 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
812 V4L2_FIELD_INTERLACED
,
813 sizeof(struct cx88_buffer
),
815 videobuf_queue_init(&fh
->vbiq
, &cx8800_vbi_qops
,
816 dev
->pci
, &dev
->slock
,
817 V4L2_BUF_TYPE_VBI_CAPTURE
,
819 sizeof(struct cx88_buffer
),
823 int board
= core
->board
;
824 dprintk(1,"video_open: setting radio device\n");
825 cx_write(MO_GP3_IO
, cx88_boards
[board
].radio
.gpio3
);
826 cx_write(MO_GP0_IO
, cx88_boards
[board
].radio
.gpio0
);
827 cx_write(MO_GP1_IO
, cx88_boards
[board
].radio
.gpio1
);
828 cx_write(MO_GP2_IO
, cx88_boards
[board
].radio
.gpio2
);
829 core
->tvaudio
= WW_FM
;
830 cx88_set_tvaudio(core
);
831 cx88_set_stereo(core
,V4L2_TUNER_MODE_STEREO
,1);
832 cx88_call_i2c_clients(core
,AUDC_SET_RADIO
,NULL
);
839 video_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
841 struct cx8800_fh
*fh
= file
->private_data
;
844 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
845 if (res_locked(fh
->dev
,RESOURCE_VIDEO
))
847 return videobuf_read_one(&fh
->vidq
, data
, count
, ppos
,
848 file
->f_flags
& O_NONBLOCK
);
849 case V4L2_BUF_TYPE_VBI_CAPTURE
:
850 if (!res_get(fh
->dev
,fh
,RESOURCE_VBI
))
852 return videobuf_read_stream(&fh
->vbiq
, data
, count
, ppos
, 1,
853 file
->f_flags
& O_NONBLOCK
);
861 video_poll(struct file
*file
, struct poll_table_struct
*wait
)
863 struct cx8800_fh
*fh
= file
->private_data
;
864 struct cx88_buffer
*buf
;
866 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
) {
867 if (!res_get(fh
->dev
,fh
,RESOURCE_VBI
))
869 return videobuf_poll_stream(file
, &fh
->vbiq
, wait
);
872 if (res_check(fh
,RESOURCE_VIDEO
)) {
873 /* streaming capture */
874 if (list_empty(&fh
->vidq
.stream
))
876 buf
= list_entry(fh
->vidq
.stream
.next
,struct cx88_buffer
,vb
.stream
);
879 buf
= (struct cx88_buffer
*)fh
->vidq
.read_buf
;
883 poll_wait(file
, &buf
->vb
.done
, wait
);
884 if (buf
->vb
.state
== STATE_DONE
||
885 buf
->vb
.state
== STATE_ERROR
)
886 return POLLIN
|POLLRDNORM
;
890 static int video_release(struct inode
*inode
, struct file
*file
)
892 struct cx8800_fh
*fh
= file
->private_data
;
893 struct cx8800_dev
*dev
= fh
->dev
;
895 /* turn off overlay */
896 if (res_check(fh
, RESOURCE_OVERLAY
)) {
898 res_free(dev
,fh
,RESOURCE_OVERLAY
);
901 /* stop video capture */
902 if (res_check(fh
, RESOURCE_VIDEO
)) {
903 videobuf_queue_cancel(&fh
->vidq
);
904 res_free(dev
,fh
,RESOURCE_VIDEO
);
906 if (fh
->vidq
.read_buf
) {
907 buffer_release(&fh
->vidq
,fh
->vidq
.read_buf
);
908 kfree(fh
->vidq
.read_buf
);
911 /* stop vbi capture */
912 if (res_check(fh
, RESOURCE_VBI
)) {
913 if (fh
->vbiq
.streaming
)
914 videobuf_streamoff(&fh
->vbiq
);
915 if (fh
->vbiq
.reading
)
916 videobuf_read_stop(&fh
->vbiq
);
917 res_free(dev
,fh
,RESOURCE_VBI
);
920 videobuf_mmap_free(&fh
->vidq
);
921 videobuf_mmap_free(&fh
->vbiq
);
922 file
->private_data
= NULL
;
925 cx88_call_i2c_clients (dev
->core
, TUNER_SET_STANDBY
, NULL
);
931 video_mmap(struct file
*file
, struct vm_area_struct
* vma
)
933 struct cx8800_fh
*fh
= file
->private_data
;
935 return videobuf_mmap_mapper(get_queue(fh
), vma
);
938 /* ------------------------------------------------------------------ */
940 /* static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
941 static int get_control(struct cx88_core
*core
, struct v4l2_control
*ctl
)
943 /* struct cx88_core *core = dev->core; */
944 struct cx88_ctrl
*c
= NULL
;
948 for (i
= 0; i
< CX8800_CTLS
; i
++)
949 if (cx8800_ctls
[i
].v
.id
== ctl
->id
)
954 value
= c
->sreg
? cx_sread(c
->sreg
) : cx_read(c
->reg
);
956 case V4L2_CID_AUDIO_BALANCE
:
957 ctl
->value
= ((value
& 0x7f) < 0x40) ? ((value
& 0x7f) + 0x40)
958 : (0x7f - (value
& 0x7f));
960 case V4L2_CID_AUDIO_VOLUME
:
961 ctl
->value
= 0x3f - (value
& 0x3f);
964 ctl
->value
= ((value
+ (c
->off
<< c
->shift
)) & c
->mask
) >> c
->shift
;
967 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
968 ctl
->id
, c
->v
.name
, ctl
->value
, c
->reg
,
969 value
,c
->mask
, c
->sreg
? " [shadowed]" : "");
973 /* static int set_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
974 static int set_control(struct cx88_core
*core
, struct v4l2_control
*ctl
)
976 /* struct cx88_core *core = dev->core; */
977 struct cx88_ctrl
*c
= NULL
;
980 for (i
= 0; i
< CX8800_CTLS
; i
++) {
981 if (cx8800_ctls
[i
].v
.id
== ctl
->id
) {
988 if (ctl
->value
< c
->v
.minimum
)
989 ctl
->value
= c
->v
.minimum
;
990 if (ctl
->value
> c
->v
.maximum
)
991 ctl
->value
= c
->v
.maximum
;
994 case V4L2_CID_AUDIO_BALANCE
:
995 value
= (ctl
->value
< 0x40) ? (0x7f - ctl
->value
) : (ctl
->value
- 0x40);
997 case V4L2_CID_AUDIO_VOLUME
:
998 value
= 0x3f - (ctl
->value
& 0x3f);
1000 case V4L2_CID_SATURATION
:
1001 /* special v_sat handling */
1003 value
= ((ctl
->value
- c
->off
) << c
->shift
) & c
->mask
;
1005 if (core
->tvnorm
->id
& V4L2_STD_SECAM
) {
1006 /* For SECAM, both U and V sat should be equal */
1007 value
=value
<<8|value
;
1009 /* Keeps U Saturation proportional to V Sat */
1010 value
=(value
*0x5a)/0x7f<<8|value
;
1015 value
= ((ctl
->value
- c
->off
) << c
->shift
) & c
->mask
;
1018 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
1019 ctl
->id
, c
->v
.name
, ctl
->value
, c
->reg
, value
,
1020 mask
, c
->sreg
? " [shadowed]" : "");
1022 cx_sandor(c
->sreg
, c
->reg
, mask
, value
);
1024 cx_andor(c
->reg
, mask
, value
);
1029 static void init_controls(struct cx88_core
*core
)
1031 struct v4l2_control ctrl
;
1034 for (i
= 0; i
< CX8800_CTLS
; i
++) {
1035 ctrl
.id
=cx8800_ctls
[i
].v
.id
;
1036 ctrl
.value
=cx8800_ctls
[i
].v
.default_value
;
1037 set_control(core
, &ctrl
);
1041 /* ------------------------------------------------------------------ */
1043 static int cx8800_g_fmt(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
,
1044 struct v4l2_format
*f
)
1047 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1048 memset(&f
->fmt
.pix
,0,sizeof(f
->fmt
.pix
));
1049 f
->fmt
.pix
.width
= fh
->width
;
1050 f
->fmt
.pix
.height
= fh
->height
;
1051 f
->fmt
.pix
.field
= fh
->vidq
.field
;
1052 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
1053 f
->fmt
.pix
.bytesperline
=
1054 (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
1055 f
->fmt
.pix
.sizeimage
=
1056 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1058 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1059 cx8800_vbi_fmt(dev
, f
);
1066 static int cx8800_try_fmt(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
,
1067 struct v4l2_format
*f
)
1069 struct cx88_core
*core
= dev
->core
;
1072 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1074 struct cx8800_fmt
*fmt
;
1075 enum v4l2_field field
;
1076 unsigned int maxw
, maxh
;
1078 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1082 field
= f
->fmt
.pix
.field
;
1083 maxw
= norm_maxw(core
->tvnorm
);
1084 maxh
= norm_maxh(core
->tvnorm
);
1086 if (V4L2_FIELD_ANY
== field
) {
1087 field
= (f
->fmt
.pix
.height
> maxh
/2)
1088 ? V4L2_FIELD_INTERLACED
1089 : V4L2_FIELD_BOTTOM
;
1093 case V4L2_FIELD_TOP
:
1094 case V4L2_FIELD_BOTTOM
:
1097 case V4L2_FIELD_INTERLACED
:
1103 f
->fmt
.pix
.field
= field
;
1104 if (f
->fmt
.pix
.height
< 32)
1105 f
->fmt
.pix
.height
= 32;
1106 if (f
->fmt
.pix
.height
> maxh
)
1107 f
->fmt
.pix
.height
= maxh
;
1108 if (f
->fmt
.pix
.width
< 48)
1109 f
->fmt
.pix
.width
= 48;
1110 if (f
->fmt
.pix
.width
> maxw
)
1111 f
->fmt
.pix
.width
= maxw
;
1112 f
->fmt
.pix
.width
&= ~0x03;
1113 f
->fmt
.pix
.bytesperline
=
1114 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
1115 f
->fmt
.pix
.sizeimage
=
1116 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1120 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1121 cx8800_vbi_fmt(dev
, f
);
1128 static int cx8800_s_fmt(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
,
1129 struct v4l2_format
*f
)
1134 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1135 err
= cx8800_try_fmt(dev
,fh
,f
);
1139 fh
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1140 fh
->width
= f
->fmt
.pix
.width
;
1141 fh
->height
= f
->fmt
.pix
.height
;
1142 fh
->vidq
.field
= f
->fmt
.pix
.field
;
1144 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1145 cx8800_vbi_fmt(dev
, f
);
1153 * This function is _not_ called directly, but from
1154 * video_generic_ioctl (and maybe others). userspace
1155 * copying is done already, arg is a kernel pointer.
1157 static int video_do_ioctl(struct inode
*inode
, struct file
*file
,
1158 unsigned int cmd
, void *arg
)
1160 struct cx8800_fh
*fh
= file
->private_data
;
1161 struct cx8800_dev
*dev
= fh
->dev
;
1162 struct cx88_core
*core
= dev
->core
;
1165 if (video_debug
> 1)
1166 v4l_print_ioctl(core
->name
,cmd
);
1169 /* --- capabilities ------------------------------------------ */
1170 case VIDIOC_QUERYCAP
:
1172 struct v4l2_capability
*cap
= arg
;
1174 memset(cap
,0,sizeof(*cap
));
1175 strcpy(cap
->driver
, "cx8800");
1176 strlcpy(cap
->card
, cx88_boards
[core
->board
].name
,
1178 sprintf(cap
->bus_info
,"PCI:%s",pci_name(dev
->pci
));
1179 cap
->version
= CX88_VERSION_CODE
;
1181 V4L2_CAP_VIDEO_CAPTURE
|
1182 V4L2_CAP_READWRITE
|
1183 V4L2_CAP_STREAMING
|
1184 V4L2_CAP_VBI_CAPTURE
|
1186 if (UNSET
!= core
->tuner_type
)
1187 cap
->capabilities
|= V4L2_CAP_TUNER
;
1191 /* --- capture ioctls ---------------------------------------- */
1192 case VIDIOC_ENUM_FMT
:
1194 struct v4l2_fmtdesc
*f
= arg
;
1195 enum v4l2_buf_type type
;
1201 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1202 if (index
>= ARRAY_SIZE(formats
))
1204 memset(f
,0,sizeof(*f
));
1207 strlcpy(f
->description
,formats
[index
].name
,sizeof(f
->description
));
1208 f
->pixelformat
= formats
[index
].fourcc
;
1217 struct v4l2_format
*f
= arg
;
1218 return cx8800_g_fmt(dev
,fh
,f
);
1222 struct v4l2_format
*f
= arg
;
1223 return cx8800_s_fmt(dev
,fh
,f
);
1225 case VIDIOC_TRY_FMT
:
1227 struct v4l2_format
*f
= arg
;
1228 return cx8800_try_fmt(dev
,fh
,f
);
1230 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1231 /* --- streaming capture ------------------------------------- */
1234 struct video_mbuf
*mbuf
= arg
;
1235 struct videobuf_queue
*q
;
1236 struct v4l2_requestbuffers req
;
1240 memset(&req
,0,sizeof(req
));
1243 req
.memory
= V4L2_MEMORY_MMAP
;
1244 err
= videobuf_reqbufs(q
,&req
);
1247 memset(mbuf
,0,sizeof(*mbuf
));
1248 mbuf
->frames
= req
.count
;
1250 for (i
= 0; i
< mbuf
->frames
; i
++) {
1251 mbuf
->offsets
[i
] = q
->bufs
[i
]->boff
;
1252 mbuf
->size
+= q
->bufs
[i
]->bsize
;
1257 case VIDIOC_REQBUFS
:
1258 return videobuf_reqbufs(get_queue(fh
), arg
);
1260 case VIDIOC_QUERYBUF
:
1261 return videobuf_querybuf(get_queue(fh
), arg
);
1264 return videobuf_qbuf(get_queue(fh
), arg
);
1267 return videobuf_dqbuf(get_queue(fh
), arg
,
1268 file
->f_flags
& O_NONBLOCK
);
1270 case VIDIOC_STREAMON
:
1272 int res
= get_ressource(fh
);
1274 if (!res_get(dev
,fh
,res
))
1276 return videobuf_streamon(get_queue(fh
));
1278 case VIDIOC_STREAMOFF
:
1280 int res
= get_ressource(fh
);
1282 err
= videobuf_streamoff(get_queue(fh
));
1285 res_free(dev
,fh
,res
);
1289 return cx88_do_ioctl( inode
, file
, fh
->radio
, core
, cmd
, arg
, video_do_ioctl
);
1294 int cx88_do_ioctl(struct inode
*inode
, struct file
*file
, int radio
,
1295 struct cx88_core
*core
, unsigned int cmd
, void *arg
, v4l2_kioctl driver_ioctl
)
1300 if (video_debug
> 1) {
1301 if (_IOC_DIR(cmd
) & _IOC_WRITE
)
1302 v4l_printk_ioctl_arg("cx88(w)",cmd
, arg
);
1303 else if (!_IOC_DIR(cmd
) & _IOC_READ
) {
1304 v4l_print_ioctl("cx88", cmd
);
1307 v4l_print_ioctl(core
->name
,cmd
);
1312 /* ---------- tv norms ---------- */
1313 case VIDIOC_ENUMSTD
:
1315 struct v4l2_standard
*e
= arg
;
1319 if (i
>= ARRAY_SIZE(tvnorms
))
1321 err
= v4l2_video_std_construct(e
, tvnorms
[e
->index
].id
,
1322 tvnorms
[e
->index
].name
);
1330 v4l2_std_id
*id
= arg
;
1332 *id
= core
->tvnorm
->id
;
1337 v4l2_std_id
*id
= arg
;
1340 for(i
= 0; i
< ARRAY_SIZE(tvnorms
); i
++)
1341 if (*id
& tvnorms
[i
].id
)
1343 if (i
== ARRAY_SIZE(tvnorms
))
1346 mutex_lock(&core
->lock
);
1347 cx88_set_tvnorm(core
,&tvnorms
[i
]);
1348 mutex_unlock(&core
->lock
);
1352 /* ------ input switching ---------- */
1353 case VIDIOC_ENUMINPUT
:
1355 static const char *iname
[] = {
1356 [ CX88_VMUX_COMPOSITE1
] = "Composite1",
1357 [ CX88_VMUX_COMPOSITE2
] = "Composite2",
1358 [ CX88_VMUX_COMPOSITE3
] = "Composite3",
1359 [ CX88_VMUX_COMPOSITE4
] = "Composite4",
1360 [ CX88_VMUX_SVIDEO
] = "S-Video",
1361 [ CX88_VMUX_TELEVISION
] = "Television",
1362 [ CX88_VMUX_CABLE
] = "Cable TV",
1363 [ CX88_VMUX_DVB
] = "DVB",
1364 [ CX88_VMUX_DEBUG
] = "for debug only",
1366 struct v4l2_input
*i
= arg
;
1372 if (0 == INPUT(n
)->type
)
1374 memset(i
,0,sizeof(*i
));
1376 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1377 strcpy(i
->name
,iname
[INPUT(n
)->type
]);
1378 if ((CX88_VMUX_TELEVISION
== INPUT(n
)->type
) ||
1379 (CX88_VMUX_CABLE
== INPUT(n
)->type
))
1380 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1381 for (n
= 0; n
< ARRAY_SIZE(tvnorms
); n
++)
1382 i
->std
|= tvnorms
[n
].id
;
1385 case VIDIOC_G_INPUT
:
1387 unsigned int *i
= arg
;
1392 case VIDIOC_S_INPUT
:
1394 unsigned int *i
= arg
;
1398 mutex_lock(&core
->lock
);
1399 cx88_newstation(core
);
1401 mutex_unlock(&core
->lock
);
1407 /* --- controls ---------------------------------------------- */
1408 case VIDIOC_QUERYCTRL
:
1410 struct v4l2_queryctrl
*c
= arg
;
1412 return cx88_queryctrl(c
);
1415 return get_control(core
,arg
);
1417 return set_control(core
,arg
);
1419 /* --- tuner ioctls ------------------------------------------ */
1420 case VIDIOC_G_TUNER
:
1422 struct v4l2_tuner
*t
= arg
;
1425 if (UNSET
== core
->tuner_type
)
1430 memset(t
,0,sizeof(*t
));
1431 strcpy(t
->name
, "Television");
1432 t
->type
= V4L2_TUNER_ANALOG_TV
;
1433 t
->capability
= V4L2_TUNER_CAP_NORM
;
1434 t
->rangehigh
= 0xffffffffUL
;
1436 cx88_get_stereo(core
,t
);
1437 reg
= cx_read(MO_DEVICE_STATUS
);
1438 t
->signal
= (reg
& (1<<5)) ? 0xffff : 0x0000;
1441 case VIDIOC_S_TUNER
:
1443 struct v4l2_tuner
*t
= arg
;
1445 if (UNSET
== core
->tuner_type
)
1449 cx88_set_stereo(core
, t
->audmode
, 1);
1452 case VIDIOC_G_FREQUENCY
:
1454 struct v4l2_frequency
*f
= arg
;
1456 memset(f
,0,sizeof(*f
));
1458 if (UNSET
== core
->tuner_type
)
1461 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1462 f
->type
= radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1463 f
->frequency
= core
->freq
;
1465 cx88_call_i2c_clients(core
,VIDIOC_G_FREQUENCY
,f
);
1469 case VIDIOC_S_FREQUENCY
:
1471 struct v4l2_frequency
*f
= arg
;
1473 if (UNSET
== core
->tuner_type
)
1477 if (0 == radio
&& f
->type
!= V4L2_TUNER_ANALOG_TV
)
1479 if (1 == radio
&& f
->type
!= V4L2_TUNER_RADIO
)
1481 mutex_lock(&core
->lock
);
1482 core
->freq
= f
->frequency
;
1483 cx88_newstation(core
);
1484 cx88_call_i2c_clients(core
,VIDIOC_S_FREQUENCY
,f
);
1486 /* When changing channels it is required to reset TVAUDIO */
1488 cx88_set_tvaudio(core
);
1490 mutex_unlock(&core
->lock
);
1493 #ifdef CONFIG_VIDEO_ADV_DEBUG
1494 /* ioctls to allow direct acces to the cx2388x registers */
1495 case VIDIOC_INT_G_REGISTER
:
1497 struct v4l2_register
*reg
= arg
;
1499 if (reg
->i2c_id
!= 0)
1501 /* cx2388x has a 24-bit register space */
1502 reg
->val
= cx_read(reg
->reg
&0xffffff);
1505 case VIDIOC_INT_S_REGISTER
:
1507 struct v4l2_register
*reg
= arg
;
1509 if (reg
->i2c_id
!= 0)
1511 if (!capable(CAP_SYS_ADMIN
))
1513 cx_write(reg
->reg
&0xffffff, reg
->val
);
1519 return v4l_compat_translate_ioctl(inode
,file
,cmd
,arg
,
1525 static int video_ioctl(struct inode
*inode
, struct file
*file
,
1526 unsigned int cmd
, unsigned long arg
)
1530 retval
=video_usercopy(inode
, file
, cmd
, arg
, video_do_ioctl
);
1532 if (video_debug
> 1) {
1534 v4l_print_ioctl("cx88(err)", cmd
);
1535 printk(KERN_DEBUG
"cx88(err): errcode=%d\n",retval
);
1536 } else if (_IOC_DIR(cmd
) & _IOC_READ
)
1537 v4l_printk_ioctl_arg("cx88(r)",cmd
, (void *)arg
);
1543 /* ----------------------------------------------------------- */
1545 static int radio_do_ioctl(struct inode
*inode
, struct file
*file
,
1546 unsigned int cmd
, void *arg
)
1548 struct cx8800_fh
*fh
= file
->private_data
;
1549 struct cx8800_dev
*dev
= fh
->dev
;
1550 struct cx88_core
*core
= dev
->core
;
1552 if (video_debug
> 1)
1553 v4l_print_ioctl(core
->name
,cmd
);
1556 case VIDIOC_QUERYCAP
:
1558 struct v4l2_capability
*cap
= arg
;
1560 memset(cap
,0,sizeof(*cap
));
1561 strcpy(cap
->driver
, "cx8800");
1562 strlcpy(cap
->card
, cx88_boards
[core
->board
].name
,
1564 sprintf(cap
->bus_info
,"PCI:%s", pci_name(dev
->pci
));
1565 cap
->version
= CX88_VERSION_CODE
;
1566 cap
->capabilities
= V4L2_CAP_TUNER
;
1569 case VIDIOC_G_TUNER
:
1571 struct v4l2_tuner
*t
= arg
;
1576 memset(t
,0,sizeof(*t
));
1577 strcpy(t
->name
, "Radio");
1578 t
->type
= V4L2_TUNER_RADIO
;
1580 cx88_call_i2c_clients(core
,VIDIOC_G_TUNER
,t
);
1583 case VIDIOC_ENUMINPUT
:
1585 struct v4l2_input
*i
= arg
;
1589 strcpy(i
->name
,"Radio");
1590 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1593 case VIDIOC_G_INPUT
:
1599 case VIDIOC_G_AUDIO
:
1601 struct v4l2_audio
*a
= arg
;
1603 memset(a
,0,sizeof(*a
));
1604 strcpy(a
->name
,"Radio");
1609 v4l2_std_id
*id
= arg
;
1613 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1616 struct video_tuner
*v
= arg
;
1618 if (v
->tuner
) /* Only tuner 0 */
1621 cx88_call_i2c_clients(core
,VIDIOCSTUNER
,v
);
1625 case VIDIOC_S_TUNER
:
1627 struct v4l2_tuner
*t
= arg
;
1632 cx88_call_i2c_clients(core
,VIDIOC_S_TUNER
,t
);
1637 case VIDIOC_S_AUDIO
:
1638 case VIDIOC_S_INPUT
:
1642 case VIDIOC_QUERYCTRL
:
1644 struct v4l2_queryctrl
*c
= arg
;
1647 if (c
->id
< V4L2_CID_BASE
||
1648 c
->id
>= V4L2_CID_LASTP1
)
1650 if (c
->id
== V4L2_CID_AUDIO_MUTE
) {
1651 for (i
= 0; i
< CX8800_CTLS
; i
++)
1652 if (cx8800_ctls
[i
].v
.id
== c
->id
)
1654 *c
= cx8800_ctls
[i
].v
;
1663 case VIDIOC_G_FREQUENCY
:
1664 case VIDIOC_S_FREQUENCY
:
1665 return video_do_ioctl(inode
,file
,cmd
,arg
);
1668 return v4l_compat_translate_ioctl(inode
,file
,cmd
,arg
,
1674 static int radio_ioctl(struct inode
*inode
, struct file
*file
,
1675 unsigned int cmd
, unsigned long arg
)
1677 return video_usercopy(inode
, file
, cmd
, arg
, radio_do_ioctl
);
1680 /* ----------------------------------------------------------- */
1682 static void cx8800_vid_timeout(unsigned long data
)
1684 struct cx8800_dev
*dev
= (struct cx8800_dev
*)data
;
1685 struct cx88_core
*core
= dev
->core
;
1686 struct cx88_dmaqueue
*q
= &dev
->vidq
;
1687 struct cx88_buffer
*buf
;
1688 unsigned long flags
;
1690 cx88_sram_channel_dump(core
, &cx88_sram_channels
[SRAM_CH21
]);
1692 cx_clear(MO_VID_DMACNTRL
, 0x11);
1693 cx_clear(VID_CAPTURE_CONTROL
, 0x06);
1695 spin_lock_irqsave(&dev
->slock
,flags
);
1696 while (!list_empty(&q
->active
)) {
1697 buf
= list_entry(q
->active
.next
, struct cx88_buffer
, vb
.queue
);
1698 list_del(&buf
->vb
.queue
);
1699 buf
->vb
.state
= STATE_ERROR
;
1700 wake_up(&buf
->vb
.done
);
1701 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core
->name
,
1702 buf
, buf
->vb
.i
, (unsigned long)buf
->risc
.dma
);
1704 restart_video_queue(dev
,q
);
1705 spin_unlock_irqrestore(&dev
->slock
,flags
);
1708 static char *cx88_vid_irqs
[32] = {
1709 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1710 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1711 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1712 "y_sync", "u_sync", "v_sync", "vbi_sync",
1713 "opc_err", "par_err", "rip_err", "pci_abort",
1716 static void cx8800_vid_irq(struct cx8800_dev
*dev
)
1718 struct cx88_core
*core
= dev
->core
;
1719 u32 status
, mask
, count
;
1721 status
= cx_read(MO_VID_INTSTAT
);
1722 mask
= cx_read(MO_VID_INTMSK
);
1723 if (0 == (status
& mask
))
1725 cx_write(MO_VID_INTSTAT
, status
);
1726 if (irq_debug
|| (status
& mask
& ~0xff))
1727 cx88_print_irqbits(core
->name
, "irq vid",
1728 cx88_vid_irqs
, status
, mask
);
1730 /* risc op code error */
1731 if (status
& (1 << 16)) {
1732 printk(KERN_WARNING
"%s/0: video risc op code error\n",core
->name
);
1733 cx_clear(MO_VID_DMACNTRL
, 0x11);
1734 cx_clear(VID_CAPTURE_CONTROL
, 0x06);
1735 cx88_sram_channel_dump(core
, &cx88_sram_channels
[SRAM_CH21
]);
1739 if (status
& 0x01) {
1740 spin_lock(&dev
->slock
);
1741 count
= cx_read(MO_VIDY_GPCNT
);
1742 cx88_wakeup(core
, &dev
->vidq
, count
);
1743 spin_unlock(&dev
->slock
);
1747 if (status
& 0x08) {
1748 spin_lock(&dev
->slock
);
1749 count
= cx_read(MO_VBI_GPCNT
);
1750 cx88_wakeup(core
, &dev
->vbiq
, count
);
1751 spin_unlock(&dev
->slock
);
1755 if (status
& 0x10) {
1756 dprintk(2,"stopper video\n");
1757 spin_lock(&dev
->slock
);
1758 restart_video_queue(dev
,&dev
->vidq
);
1759 spin_unlock(&dev
->slock
);
1763 if (status
& 0x80) {
1764 dprintk(2,"stopper vbi\n");
1765 spin_lock(&dev
->slock
);
1766 cx8800_restart_vbi_queue(dev
,&dev
->vbiq
);
1767 spin_unlock(&dev
->slock
);
1771 static irqreturn_t
cx8800_irq(int irq
, void *dev_id
)
1773 struct cx8800_dev
*dev
= dev_id
;
1774 struct cx88_core
*core
= dev
->core
;
1776 int loop
, handled
= 0;
1778 for (loop
= 0; loop
< 10; loop
++) {
1779 status
= cx_read(MO_PCI_INTSTAT
) & (core
->pci_irqmask
| 0x01);
1782 cx_write(MO_PCI_INTSTAT
, status
);
1785 if (status
& core
->pci_irqmask
)
1786 cx88_core_irq(core
,status
);
1788 cx8800_vid_irq(dev
);
1791 printk(KERN_WARNING
"%s/0: irq loop -- clearing mask\n",
1793 cx_write(MO_PCI_INTMSK
,0);
1797 return IRQ_RETVAL(handled
);
1800 /* ----------------------------------------------------------- */
1801 /* exported stuff */
1803 static struct file_operations video_fops
=
1805 .owner
= THIS_MODULE
,
1807 .release
= video_release
,
1811 .ioctl
= video_ioctl
,
1812 .compat_ioctl
= v4l_compat_ioctl32
,
1813 .llseek
= no_llseek
,
1816 static struct video_device cx8800_video_template
=
1818 .name
= "cx8800-video",
1819 .type
= VID_TYPE_CAPTURE
|VID_TYPE_TUNER
|VID_TYPE_SCALES
,
1821 .fops
= &video_fops
,
1825 static struct video_device cx8800_vbi_template
=
1827 .name
= "cx8800-vbi",
1828 .type
= VID_TYPE_TELETEXT
|VID_TYPE_TUNER
,
1830 .fops
= &video_fops
,
1834 static struct file_operations radio_fops
=
1836 .owner
= THIS_MODULE
,
1838 .release
= video_release
,
1839 .ioctl
= radio_ioctl
,
1840 .compat_ioctl
= v4l_compat_ioctl32
,
1841 .llseek
= no_llseek
,
1844 static struct video_device cx8800_radio_template
=
1846 .name
= "cx8800-radio",
1847 .type
= VID_TYPE_TUNER
,
1849 .fops
= &radio_fops
,
1853 /* ----------------------------------------------------------- */
1855 static void cx8800_unregister_video(struct cx8800_dev
*dev
)
1857 if (dev
->radio_dev
) {
1858 if (-1 != dev
->radio_dev
->minor
)
1859 video_unregister_device(dev
->radio_dev
);
1861 video_device_release(dev
->radio_dev
);
1862 dev
->radio_dev
= NULL
;
1865 if (-1 != dev
->vbi_dev
->minor
)
1866 video_unregister_device(dev
->vbi_dev
);
1868 video_device_release(dev
->vbi_dev
);
1869 dev
->vbi_dev
= NULL
;
1871 if (dev
->video_dev
) {
1872 if (-1 != dev
->video_dev
->minor
)
1873 video_unregister_device(dev
->video_dev
);
1875 video_device_release(dev
->video_dev
);
1876 dev
->video_dev
= NULL
;
1880 static int __devinit
cx8800_initdev(struct pci_dev
*pci_dev
,
1881 const struct pci_device_id
*pci_id
)
1883 struct cx8800_dev
*dev
;
1884 struct cx88_core
*core
;
1887 dev
= kzalloc(sizeof(*dev
),GFP_KERNEL
);
1893 if (pci_enable_device(pci_dev
)) {
1897 core
= cx88_core_get(dev
->pci
);
1904 /* print pci info */
1905 pci_read_config_byte(pci_dev
, PCI_CLASS_REVISION
, &dev
->pci_rev
);
1906 pci_read_config_byte(pci_dev
, PCI_LATENCY_TIMER
, &dev
->pci_lat
);
1907 printk(KERN_INFO
"%s/0: found at %s, rev: %d, irq: %d, "
1908 "latency: %d, mmio: 0x%llx\n", core
->name
,
1909 pci_name(pci_dev
), dev
->pci_rev
, pci_dev
->irq
,
1910 dev
->pci_lat
,(unsigned long long)pci_resource_start(pci_dev
,0));
1912 pci_set_master(pci_dev
);
1913 if (!pci_dma_supported(pci_dev
,0xffffffff)) {
1914 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core
->name
);
1919 /* initialize driver struct */
1920 spin_lock_init(&dev
->slock
);
1921 core
->tvnorm
= tvnorms
;
1923 /* init video dma queues */
1924 INIT_LIST_HEAD(&dev
->vidq
.active
);
1925 INIT_LIST_HEAD(&dev
->vidq
.queued
);
1926 dev
->vidq
.timeout
.function
= cx8800_vid_timeout
;
1927 dev
->vidq
.timeout
.data
= (unsigned long)dev
;
1928 init_timer(&dev
->vidq
.timeout
);
1929 cx88_risc_stopper(dev
->pci
,&dev
->vidq
.stopper
,
1930 MO_VID_DMACNTRL
,0x11,0x00);
1932 /* init vbi dma queues */
1933 INIT_LIST_HEAD(&dev
->vbiq
.active
);
1934 INIT_LIST_HEAD(&dev
->vbiq
.queued
);
1935 dev
->vbiq
.timeout
.function
= cx8800_vbi_timeout
;
1936 dev
->vbiq
.timeout
.data
= (unsigned long)dev
;
1937 init_timer(&dev
->vbiq
.timeout
);
1938 cx88_risc_stopper(dev
->pci
,&dev
->vbiq
.stopper
,
1939 MO_VID_DMACNTRL
,0x88,0x00);
1942 err
= request_irq(pci_dev
->irq
, cx8800_irq
,
1943 IRQF_SHARED
| IRQF_DISABLED
, core
->name
, dev
);
1945 printk(KERN_ERR
"%s: can't get IRQ %d\n",
1946 core
->name
,pci_dev
->irq
);
1949 cx_set(MO_PCI_INTMSK
, core
->pci_irqmask
);
1951 /* load and configure helper modules */
1952 if (TUNER_ABSENT
!= core
->tuner_type
)
1953 request_module("tuner");
1955 if (cx88_boards
[ core
->board
].audio_chip
== AUDIO_CHIP_WM8775
)
1956 request_module("wm8775");
1958 /* register v4l devices */
1959 dev
->video_dev
= cx88_vdev_init(core
,dev
->pci
,
1960 &cx8800_video_template
,"video");
1961 err
= video_register_device(dev
->video_dev
,VFL_TYPE_GRABBER
,
1962 video_nr
[core
->nr
]);
1964 printk(KERN_INFO
"%s: can't register video device\n",
1968 printk(KERN_INFO
"%s/0: registered device video%d [v4l2]\n",
1969 core
->name
,dev
->video_dev
->minor
& 0x1f);
1971 dev
->vbi_dev
= cx88_vdev_init(core
,dev
->pci
,&cx8800_vbi_template
,"vbi");
1972 err
= video_register_device(dev
->vbi_dev
,VFL_TYPE_VBI
,
1975 printk(KERN_INFO
"%s/0: can't register vbi device\n",
1979 printk(KERN_INFO
"%s/0: registered device vbi%d\n",
1980 core
->name
,dev
->vbi_dev
->minor
& 0x1f);
1982 if (core
->has_radio
) {
1983 dev
->radio_dev
= cx88_vdev_init(core
,dev
->pci
,
1984 &cx8800_radio_template
,"radio");
1985 err
= video_register_device(dev
->radio_dev
,VFL_TYPE_RADIO
,
1986 radio_nr
[core
->nr
]);
1988 printk(KERN_INFO
"%s/0: can't register radio device\n",
1992 printk(KERN_INFO
"%s/0: registered device radio%d\n",
1993 core
->name
,dev
->radio_dev
->minor
& 0x1f);
1996 /* everything worked */
1997 list_add_tail(&dev
->devlist
,&cx8800_devlist
);
1998 pci_set_drvdata(pci_dev
,dev
);
2000 /* initial device configuration */
2001 mutex_lock(&core
->lock
);
2002 cx88_set_tvnorm(core
,tvnorms
);
2003 init_controls(core
);
2005 mutex_unlock(&core
->lock
);
2007 /* start tvaudio thread */
2008 if (core
->tuner_type
!= TUNER_ABSENT
)
2009 core
->kthread
= kthread_run(cx88_audio_thread
, core
, "cx88 tvaudio");
2013 cx8800_unregister_video(dev
);
2014 free_irq(pci_dev
->irq
, dev
);
2016 cx88_core_put(core
,dev
->pci
);
2022 static void __devexit
cx8800_finidev(struct pci_dev
*pci_dev
)
2024 struct cx8800_dev
*dev
= pci_get_drvdata(pci_dev
);
2025 struct cx88_core
*core
= dev
->core
;
2028 if (core
->kthread
) {
2029 kthread_stop(core
->kthread
);
2030 core
->kthread
= NULL
;
2033 cx88_shutdown(core
); /* FIXME */
2034 pci_disable_device(pci_dev
);
2036 /* unregister stuff */
2038 free_irq(pci_dev
->irq
, dev
);
2039 cx8800_unregister_video(dev
);
2040 pci_set_drvdata(pci_dev
, NULL
);
2043 btcx_riscmem_free(dev
->pci
,&dev
->vidq
.stopper
);
2044 list_del(&dev
->devlist
);
2045 cx88_core_put(core
,dev
->pci
);
2050 static int cx8800_suspend(struct pci_dev
*pci_dev
, pm_message_t state
)
2052 struct cx8800_dev
*dev
= pci_get_drvdata(pci_dev
);
2053 struct cx88_core
*core
= dev
->core
;
2055 /* stop video+vbi capture */
2056 spin_lock(&dev
->slock
);
2057 if (!list_empty(&dev
->vidq
.active
)) {
2058 printk("%s: suspend video\n", core
->name
);
2059 stop_video_dma(dev
);
2060 del_timer(&dev
->vidq
.timeout
);
2062 if (!list_empty(&dev
->vbiq
.active
)) {
2063 printk("%s: suspend vbi\n", core
->name
);
2064 cx8800_stop_vbi_dma(dev
);
2065 del_timer(&dev
->vbiq
.timeout
);
2067 spin_unlock(&dev
->slock
);
2069 /* FIXME -- shutdown device */
2070 cx88_shutdown(core
);
2072 pci_save_state(pci_dev
);
2073 if (0 != pci_set_power_state(pci_dev
, pci_choose_state(pci_dev
, state
))) {
2074 pci_disable_device(pci_dev
);
2075 dev
->state
.disabled
= 1;
2080 static int cx8800_resume(struct pci_dev
*pci_dev
)
2082 struct cx8800_dev
*dev
= pci_get_drvdata(pci_dev
);
2083 struct cx88_core
*core
= dev
->core
;
2086 if (dev
->state
.disabled
) {
2087 err
=pci_enable_device(pci_dev
);
2089 printk(KERN_ERR
"%s: can't enable device\n",
2094 dev
->state
.disabled
= 0;
2096 err
= pci_set_power_state(pci_dev
, PCI_D0
);
2098 printk(KERN_ERR
"%s: can't enable device\n",
2101 pci_disable_device(pci_dev
);
2102 dev
->state
.disabled
= 1;
2106 pci_restore_state(pci_dev
);
2108 /* FIXME: re-initialize hardware */
2111 /* restart video+vbi capture */
2112 spin_lock(&dev
->slock
);
2113 if (!list_empty(&dev
->vidq
.active
)) {
2114 printk("%s: resume video\n", core
->name
);
2115 restart_video_queue(dev
,&dev
->vidq
);
2117 if (!list_empty(&dev
->vbiq
.active
)) {
2118 printk("%s: resume vbi\n", core
->name
);
2119 cx8800_restart_vbi_queue(dev
,&dev
->vbiq
);
2121 spin_unlock(&dev
->slock
);
2127 /* ----------------------------------------------------------- */
2129 static struct pci_device_id cx8800_pci_tbl
[] = {
2133 .subvendor
= PCI_ANY_ID
,
2134 .subdevice
= PCI_ANY_ID
,
2136 /* --- end of list --- */
2139 MODULE_DEVICE_TABLE(pci
, cx8800_pci_tbl
);
2141 static struct pci_driver cx8800_pci_driver
= {
2143 .id_table
= cx8800_pci_tbl
,
2144 .probe
= cx8800_initdev
,
2145 .remove
= __devexit_p(cx8800_finidev
),
2147 .suspend
= cx8800_suspend
,
2148 .resume
= cx8800_resume
,
2152 static int cx8800_init(void)
2154 printk(KERN_INFO
"cx2388x v4l2 driver version %d.%d.%d loaded\n",
2155 (CX88_VERSION_CODE
>> 16) & 0xff,
2156 (CX88_VERSION_CODE
>> 8) & 0xff,
2157 CX88_VERSION_CODE
& 0xff);
2159 printk(KERN_INFO
"cx2388x: snapshot date %04d-%02d-%02d\n",
2160 SNAPSHOT
/10000, (SNAPSHOT
/100)%100, SNAPSHOT
%100);
2162 return pci_register_driver(&cx8800_pci_driver
);
2165 static void cx8800_fini(void)
2167 pci_unregister_driver(&cx8800_pci_driver
);
2170 module_init(cx8800_init
);
2171 module_exit(cx8800_fini
);
2173 EXPORT_SYMBOL(cx88_do_ioctl
);
2175 /* ----------------------------------------------------------- */
2180 * 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