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_del(&buf
->vb
.queue
);
498 list_add_tail(&buf
->vb
.queue
,&q
->active
);
499 start_video_dma(dev
, q
, buf
);
500 buf
->vb
.state
= STATE_ACTIVE
;
501 buf
->count
= q
->count
++;
502 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
503 dprintk(2,"[%p/%d] restart_queue - first active\n",
506 } else if (prev
->vb
.width
== buf
->vb
.width
&&
507 prev
->vb
.height
== buf
->vb
.height
&&
508 prev
->fmt
== buf
->fmt
) {
509 list_del(&buf
->vb
.queue
);
510 list_add_tail(&buf
->vb
.queue
,&q
->active
);
511 buf
->vb
.state
= STATE_ACTIVE
;
512 buf
->count
= q
->count
++;
513 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
514 dprintk(2,"[%p/%d] restart_queue - move to active\n",
523 /* ------------------------------------------------------------------ */
526 buffer_setup(struct videobuf_queue
*q
, unsigned int *count
, unsigned int *size
)
528 struct cx8800_fh
*fh
= q
->priv_data
;
530 *size
= fh
->fmt
->depth
*fh
->width
*fh
->height
>> 3;
533 while (*size
* *count
> vid_limit
* 1024 * 1024)
539 buffer_prepare(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
540 enum v4l2_field field
)
542 struct cx8800_fh
*fh
= q
->priv_data
;
543 struct cx8800_dev
*dev
= fh
->dev
;
544 struct cx88_core
*core
= dev
->core
;
545 struct cx88_buffer
*buf
= container_of(vb
,struct cx88_buffer
,vb
);
546 int rc
, init_buffer
= 0;
548 BUG_ON(NULL
== fh
->fmt
);
549 if (fh
->width
< 48 || fh
->width
> norm_maxw(core
->tvnorm
) ||
550 fh
->height
< 32 || fh
->height
> norm_maxh(core
->tvnorm
))
552 buf
->vb
.size
= (fh
->width
* fh
->height
* fh
->fmt
->depth
) >> 3;
553 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
556 if (buf
->fmt
!= fh
->fmt
||
557 buf
->vb
.width
!= fh
->width
||
558 buf
->vb
.height
!= fh
->height
||
559 buf
->vb
.field
!= field
) {
561 buf
->vb
.width
= fh
->width
;
562 buf
->vb
.height
= fh
->height
;
563 buf
->vb
.field
= field
;
567 if (STATE_NEEDS_INIT
== buf
->vb
.state
) {
569 if (0 != (rc
= videobuf_iolock(q
,&buf
->vb
,NULL
)))
574 buf
->bpl
= buf
->vb
.width
* buf
->fmt
->depth
>> 3;
575 switch (buf
->vb
.field
) {
577 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
578 buf
->vb
.dma
.sglist
, 0, UNSET
,
579 buf
->bpl
, 0, buf
->vb
.height
);
581 case V4L2_FIELD_BOTTOM
:
582 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
583 buf
->vb
.dma
.sglist
, UNSET
, 0,
584 buf
->bpl
, 0, buf
->vb
.height
);
586 case V4L2_FIELD_INTERLACED
:
587 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
588 buf
->vb
.dma
.sglist
, 0, buf
->bpl
,
590 buf
->vb
.height
>> 1);
592 case V4L2_FIELD_SEQ_TB
:
593 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
595 0, buf
->bpl
* (buf
->vb
.height
>> 1),
597 buf
->vb
.height
>> 1);
599 case V4L2_FIELD_SEQ_BT
:
600 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
602 buf
->bpl
* (buf
->vb
.height
>> 1), 0,
604 buf
->vb
.height
>> 1);
610 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
612 fh
->width
, fh
->height
, fh
->fmt
->depth
, fh
->fmt
->name
,
613 (unsigned long)buf
->risc
.dma
);
615 buf
->vb
.state
= STATE_PREPARED
;
619 cx88_free_buffer(q
,buf
);
624 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
626 struct cx88_buffer
*buf
= container_of(vb
,struct cx88_buffer
,vb
);
627 struct cx88_buffer
*prev
;
628 struct cx8800_fh
*fh
= vq
->priv_data
;
629 struct cx8800_dev
*dev
= fh
->dev
;
630 struct cx88_core
*core
= dev
->core
;
631 struct cx88_dmaqueue
*q
= &dev
->vidq
;
633 /* add jump to stopper */
634 buf
->risc
.jmp
[0] = cpu_to_le32(RISC_JUMP
| RISC_IRQ1
| RISC_CNT_INC
);
635 buf
->risc
.jmp
[1] = cpu_to_le32(q
->stopper
.dma
);
637 if (!list_empty(&q
->queued
)) {
638 list_add_tail(&buf
->vb
.queue
,&q
->queued
);
639 buf
->vb
.state
= STATE_QUEUED
;
640 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
643 } else if (list_empty(&q
->active
)) {
644 list_add_tail(&buf
->vb
.queue
,&q
->active
);
645 start_video_dma(dev
, q
, buf
);
646 buf
->vb
.state
= STATE_ACTIVE
;
647 buf
->count
= q
->count
++;
648 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
649 dprintk(2,"[%p/%d] buffer_queue - first active\n",
653 prev
= list_entry(q
->active
.prev
, struct cx88_buffer
, vb
.queue
);
654 if (prev
->vb
.width
== buf
->vb
.width
&&
655 prev
->vb
.height
== buf
->vb
.height
&&
656 prev
->fmt
== buf
->fmt
) {
657 list_add_tail(&buf
->vb
.queue
,&q
->active
);
658 buf
->vb
.state
= STATE_ACTIVE
;
659 buf
->count
= q
->count
++;
660 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
661 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
665 list_add_tail(&buf
->vb
.queue
,&q
->queued
);
666 buf
->vb
.state
= STATE_QUEUED
;
667 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
673 static void buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
675 struct cx88_buffer
*buf
= container_of(vb
,struct cx88_buffer
,vb
);
677 cx88_free_buffer(q
,buf
);
680 static struct videobuf_queue_ops cx8800_video_qops
= {
681 .buf_setup
= buffer_setup
,
682 .buf_prepare
= buffer_prepare
,
683 .buf_queue
= buffer_queue
,
684 .buf_release
= buffer_release
,
687 /* ------------------------------------------------------------------ */
690 /* ------------------------------------------------------------------ */
692 static struct videobuf_queue
* get_queue(struct cx8800_fh
*fh
)
695 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
697 case V4L2_BUF_TYPE_VBI_CAPTURE
:
705 static int get_ressource(struct cx8800_fh
*fh
)
708 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
709 return RESOURCE_VIDEO
;
710 case V4L2_BUF_TYPE_VBI_CAPTURE
:
718 static int video_open(struct inode
*inode
, struct file
*file
)
720 int minor
= iminor(inode
);
721 struct cx8800_dev
*h
,*dev
= NULL
;
722 struct cx88_core
*core
;
723 struct cx8800_fh
*fh
;
724 struct list_head
*list
;
725 enum v4l2_buf_type type
= 0;
728 list_for_each(list
,&cx8800_devlist
) {
729 h
= list_entry(list
, struct cx8800_dev
, devlist
);
730 if (h
->video_dev
->minor
== minor
) {
732 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
734 if (h
->vbi_dev
->minor
== minor
) {
736 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
739 h
->radio_dev
->minor
== minor
) {
749 dprintk(1,"open minor=%d radio=%d type=%s\n",
750 minor
,radio
,v4l2_type_names
[type
]);
752 /* allocate + initialize per filehandle data */
753 fh
= kzalloc(sizeof(*fh
),GFP_KERNEL
);
756 file
->private_data
= fh
;
762 fh
->fmt
= format_by_fourcc(V4L2_PIX_FMT_BGR24
);
764 videobuf_queue_init(&fh
->vidq
, &cx8800_video_qops
,
765 dev
->pci
, &dev
->slock
,
766 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
767 V4L2_FIELD_INTERLACED
,
768 sizeof(struct cx88_buffer
),
770 videobuf_queue_init(&fh
->vbiq
, &cx8800_vbi_qops
,
771 dev
->pci
, &dev
->slock
,
772 V4L2_BUF_TYPE_VBI_CAPTURE
,
774 sizeof(struct cx88_buffer
),
778 int board
= core
->board
;
779 dprintk(1,"video_open: setting radio device\n");
780 cx_write(MO_GP3_IO
, cx88_boards
[board
].radio
.gpio3
);
781 cx_write(MO_GP0_IO
, cx88_boards
[board
].radio
.gpio0
);
782 cx_write(MO_GP1_IO
, cx88_boards
[board
].radio
.gpio1
);
783 cx_write(MO_GP2_IO
, cx88_boards
[board
].radio
.gpio2
);
784 core
->tvaudio
= WW_FM
;
785 cx88_set_tvaudio(core
);
786 cx88_set_stereo(core
,V4L2_TUNER_MODE_STEREO
,1);
787 cx88_call_i2c_clients(core
,AUDC_SET_RADIO
,NULL
);
794 video_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
796 struct cx8800_fh
*fh
= file
->private_data
;
799 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
800 if (res_locked(fh
->dev
,RESOURCE_VIDEO
))
802 return videobuf_read_one(&fh
->vidq
, data
, count
, ppos
,
803 file
->f_flags
& O_NONBLOCK
);
804 case V4L2_BUF_TYPE_VBI_CAPTURE
:
805 if (!res_get(fh
->dev
,fh
,RESOURCE_VBI
))
807 return videobuf_read_stream(&fh
->vbiq
, data
, count
, ppos
, 1,
808 file
->f_flags
& O_NONBLOCK
);
816 video_poll(struct file
*file
, struct poll_table_struct
*wait
)
818 struct cx8800_fh
*fh
= file
->private_data
;
819 struct cx88_buffer
*buf
;
821 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
) {
822 if (!res_get(fh
->dev
,fh
,RESOURCE_VBI
))
824 return videobuf_poll_stream(file
, &fh
->vbiq
, wait
);
827 if (res_check(fh
,RESOURCE_VIDEO
)) {
828 /* streaming capture */
829 if (list_empty(&fh
->vidq
.stream
))
831 buf
= list_entry(fh
->vidq
.stream
.next
,struct cx88_buffer
,vb
.stream
);
834 buf
= (struct cx88_buffer
*)fh
->vidq
.read_buf
;
838 poll_wait(file
, &buf
->vb
.done
, wait
);
839 if (buf
->vb
.state
== STATE_DONE
||
840 buf
->vb
.state
== STATE_ERROR
)
841 return POLLIN
|POLLRDNORM
;
845 static int video_release(struct inode
*inode
, struct file
*file
)
847 struct cx8800_fh
*fh
= file
->private_data
;
848 struct cx8800_dev
*dev
= fh
->dev
;
850 /* turn off overlay */
851 if (res_check(fh
, RESOURCE_OVERLAY
)) {
853 res_free(dev
,fh
,RESOURCE_OVERLAY
);
856 /* stop video capture */
857 if (res_check(fh
, RESOURCE_VIDEO
)) {
858 videobuf_queue_cancel(&fh
->vidq
);
859 res_free(dev
,fh
,RESOURCE_VIDEO
);
861 if (fh
->vidq
.read_buf
) {
862 buffer_release(&fh
->vidq
,fh
->vidq
.read_buf
);
863 kfree(fh
->vidq
.read_buf
);
866 /* stop vbi capture */
867 if (res_check(fh
, RESOURCE_VBI
)) {
868 if (fh
->vbiq
.streaming
)
869 videobuf_streamoff(&fh
->vbiq
);
870 if (fh
->vbiq
.reading
)
871 videobuf_read_stop(&fh
->vbiq
);
872 res_free(dev
,fh
,RESOURCE_VBI
);
875 videobuf_mmap_free(&fh
->vidq
);
876 videobuf_mmap_free(&fh
->vbiq
);
877 file
->private_data
= NULL
;
880 cx88_call_i2c_clients (dev
->core
, TUNER_SET_STANDBY
, NULL
);
886 video_mmap(struct file
*file
, struct vm_area_struct
* vma
)
888 struct cx8800_fh
*fh
= file
->private_data
;
890 return videobuf_mmap_mapper(get_queue(fh
), vma
);
893 /* ------------------------------------------------------------------ */
895 /* static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
896 static int get_control(struct cx88_core
*core
, struct v4l2_control
*ctl
)
898 /* struct cx88_core *core = dev->core; */
899 struct cx88_ctrl
*c
= NULL
;
903 for (i
= 0; i
< CX8800_CTLS
; i
++)
904 if (cx8800_ctls
[i
].v
.id
== ctl
->id
)
909 value
= c
->sreg
? cx_sread(c
->sreg
) : cx_read(c
->reg
);
911 case V4L2_CID_AUDIO_BALANCE
:
912 ctl
->value
= ((value
& 0x7f) < 0x40) ? ((value
& 0x7f) + 0x40)
913 : (0x7f - (value
& 0x7f));
915 case V4L2_CID_AUDIO_VOLUME
:
916 ctl
->value
= 0x3f - (value
& 0x3f);
919 ctl
->value
= ((value
+ (c
->off
<< c
->shift
)) & c
->mask
) >> c
->shift
;
922 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
923 ctl
->id
, c
->v
.name
, ctl
->value
, c
->reg
,
924 value
,c
->mask
, c
->sreg
? " [shadowed]" : "");
928 /* static int set_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
929 static int set_control(struct cx88_core
*core
, struct v4l2_control
*ctl
)
931 /* struct cx88_core *core = dev->core; */
932 struct cx88_ctrl
*c
= NULL
;
935 for (i
= 0; i
< CX8800_CTLS
; i
++) {
936 if (cx8800_ctls
[i
].v
.id
== ctl
->id
) {
943 if (ctl
->value
< c
->v
.minimum
)
944 ctl
->value
= c
->v
.minimum
;
945 if (ctl
->value
> c
->v
.maximum
)
946 ctl
->value
= c
->v
.maximum
;
949 case V4L2_CID_AUDIO_BALANCE
:
950 value
= (ctl
->value
< 0x40) ? (0x7f - ctl
->value
) : (ctl
->value
- 0x40);
952 case V4L2_CID_AUDIO_VOLUME
:
953 value
= 0x3f - (ctl
->value
& 0x3f);
955 case V4L2_CID_SATURATION
:
956 /* special v_sat handling */
958 value
= ((ctl
->value
- c
->off
) << c
->shift
) & c
->mask
;
960 if (core
->tvnorm
->id
& V4L2_STD_SECAM
) {
961 /* For SECAM, both U and V sat should be equal */
962 value
=value
<<8|value
;
964 /* Keeps U Saturation proportional to V Sat */
965 value
=(value
*0x5a)/0x7f<<8|value
;
970 value
= ((ctl
->value
- c
->off
) << c
->shift
) & c
->mask
;
973 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
974 ctl
->id
, c
->v
.name
, ctl
->value
, c
->reg
, value
,
975 mask
, c
->sreg
? " [shadowed]" : "");
977 cx_sandor(c
->sreg
, c
->reg
, mask
, value
);
979 cx_andor(c
->reg
, mask
, value
);
984 static void init_controls(struct cx88_core
*core
)
986 struct v4l2_control ctrl
;
989 for (i
= 0; i
< CX8800_CTLS
; i
++) {
990 ctrl
.id
=cx8800_ctls
[i
].v
.id
;
991 ctrl
.value
=cx8800_ctls
[i
].v
.default_value
;
992 set_control(core
, &ctrl
);
996 /* ------------------------------------------------------------------ */
998 static int cx8800_g_fmt(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
,
999 struct v4l2_format
*f
)
1002 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1003 memset(&f
->fmt
.pix
,0,sizeof(f
->fmt
.pix
));
1004 f
->fmt
.pix
.width
= fh
->width
;
1005 f
->fmt
.pix
.height
= fh
->height
;
1006 f
->fmt
.pix
.field
= fh
->vidq
.field
;
1007 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
1008 f
->fmt
.pix
.bytesperline
=
1009 (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
1010 f
->fmt
.pix
.sizeimage
=
1011 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1013 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1014 cx8800_vbi_fmt(dev
, f
);
1021 static int cx8800_try_fmt(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
,
1022 struct v4l2_format
*f
)
1024 struct cx88_core
*core
= dev
->core
;
1027 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1029 struct cx8800_fmt
*fmt
;
1030 enum v4l2_field field
;
1031 unsigned int maxw
, maxh
;
1033 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1037 field
= f
->fmt
.pix
.field
;
1038 maxw
= norm_maxw(core
->tvnorm
);
1039 maxh
= norm_maxh(core
->tvnorm
);
1041 if (V4L2_FIELD_ANY
== field
) {
1042 field
= (f
->fmt
.pix
.height
> maxh
/2)
1043 ? V4L2_FIELD_INTERLACED
1044 : V4L2_FIELD_BOTTOM
;
1048 case V4L2_FIELD_TOP
:
1049 case V4L2_FIELD_BOTTOM
:
1052 case V4L2_FIELD_INTERLACED
:
1058 f
->fmt
.pix
.field
= field
;
1059 if (f
->fmt
.pix
.height
< 32)
1060 f
->fmt
.pix
.height
= 32;
1061 if (f
->fmt
.pix
.height
> maxh
)
1062 f
->fmt
.pix
.height
= maxh
;
1063 if (f
->fmt
.pix
.width
< 48)
1064 f
->fmt
.pix
.width
= 48;
1065 if (f
->fmt
.pix
.width
> maxw
)
1066 f
->fmt
.pix
.width
= maxw
;
1067 f
->fmt
.pix
.width
&= ~0x03;
1068 f
->fmt
.pix
.bytesperline
=
1069 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
1070 f
->fmt
.pix
.sizeimage
=
1071 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1075 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1076 cx8800_vbi_fmt(dev
, f
);
1083 static int cx8800_s_fmt(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
,
1084 struct v4l2_format
*f
)
1089 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1090 err
= cx8800_try_fmt(dev
,fh
,f
);
1094 fh
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1095 fh
->width
= f
->fmt
.pix
.width
;
1096 fh
->height
= f
->fmt
.pix
.height
;
1097 fh
->vidq
.field
= f
->fmt
.pix
.field
;
1099 case V4L2_BUF_TYPE_VBI_CAPTURE
:
1100 cx8800_vbi_fmt(dev
, f
);
1108 * This function is _not_ called directly, but from
1109 * video_generic_ioctl (and maybe others). userspace
1110 * copying is done already, arg is a kernel pointer.
1112 static int video_do_ioctl(struct inode
*inode
, struct file
*file
,
1113 unsigned int cmd
, void *arg
)
1115 struct cx8800_fh
*fh
= file
->private_data
;
1116 struct cx8800_dev
*dev
= fh
->dev
;
1117 struct cx88_core
*core
= dev
->core
;
1120 if (video_debug
> 1)
1121 v4l_print_ioctl(core
->name
,cmd
);
1124 /* --- capabilities ------------------------------------------ */
1125 case VIDIOC_QUERYCAP
:
1127 struct v4l2_capability
*cap
= arg
;
1129 memset(cap
,0,sizeof(*cap
));
1130 strcpy(cap
->driver
, "cx8800");
1131 strlcpy(cap
->card
, cx88_boards
[core
->board
].name
,
1133 sprintf(cap
->bus_info
,"PCI:%s",pci_name(dev
->pci
));
1134 cap
->version
= CX88_VERSION_CODE
;
1136 V4L2_CAP_VIDEO_CAPTURE
|
1137 V4L2_CAP_READWRITE
|
1138 V4L2_CAP_STREAMING
|
1139 V4L2_CAP_VBI_CAPTURE
|
1140 V4L2_CAP_VIDEO_OVERLAY
|
1142 if (UNSET
!= core
->tuner_type
)
1143 cap
->capabilities
|= V4L2_CAP_TUNER
;
1147 /* --- capture ioctls ---------------------------------------- */
1148 case VIDIOC_ENUM_FMT
:
1150 struct v4l2_fmtdesc
*f
= arg
;
1151 enum v4l2_buf_type type
;
1157 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
1158 if (index
>= ARRAY_SIZE(formats
))
1160 memset(f
,0,sizeof(*f
));
1163 strlcpy(f
->description
,formats
[index
].name
,sizeof(f
->description
));
1164 f
->pixelformat
= formats
[index
].fourcc
;
1173 struct v4l2_format
*f
= arg
;
1174 return cx8800_g_fmt(dev
,fh
,f
);
1178 struct v4l2_format
*f
= arg
;
1179 return cx8800_s_fmt(dev
,fh
,f
);
1181 case VIDIOC_TRY_FMT
:
1183 struct v4l2_format
*f
= arg
;
1184 return cx8800_try_fmt(dev
,fh
,f
);
1187 /* --- streaming capture ------------------------------------- */
1190 struct video_mbuf
*mbuf
= arg
;
1191 struct videobuf_queue
*q
;
1192 struct v4l2_requestbuffers req
;
1196 memset(&req
,0,sizeof(req
));
1199 req
.memory
= V4L2_MEMORY_MMAP
;
1200 err
= videobuf_reqbufs(q
,&req
);
1203 memset(mbuf
,0,sizeof(*mbuf
));
1204 mbuf
->frames
= req
.count
;
1206 for (i
= 0; i
< mbuf
->frames
; i
++) {
1207 mbuf
->offsets
[i
] = q
->bufs
[i
]->boff
;
1208 mbuf
->size
+= q
->bufs
[i
]->bsize
;
1213 case VIDIOC_REQBUFS
:
1214 return videobuf_reqbufs(get_queue(fh
), arg
);
1216 case VIDIOC_QUERYBUF
:
1217 return videobuf_querybuf(get_queue(fh
), arg
);
1220 return videobuf_qbuf(get_queue(fh
), arg
);
1223 return videobuf_dqbuf(get_queue(fh
), arg
,
1224 file
->f_flags
& O_NONBLOCK
);
1226 case VIDIOC_STREAMON
:
1228 int res
= get_ressource(fh
);
1230 if (!res_get(dev
,fh
,res
))
1232 return videobuf_streamon(get_queue(fh
));
1234 case VIDIOC_STREAMOFF
:
1236 int res
= get_ressource(fh
);
1238 err
= videobuf_streamoff(get_queue(fh
));
1241 res_free(dev
,fh
,res
);
1245 return cx88_do_ioctl( inode
, file
, fh
->radio
, core
, cmd
, arg
, video_do_ioctl
);
1250 int cx88_do_ioctl(struct inode
*inode
, struct file
*file
, int radio
,
1251 struct cx88_core
*core
, unsigned int cmd
, void *arg
, v4l2_kioctl driver_ioctl
)
1256 if (video_debug
> 1) {
1257 if (_IOC_DIR(cmd
) & _IOC_WRITE
)
1258 v4l_printk_ioctl_arg("cx88(w)",cmd
, arg
);
1259 else if (!_IOC_DIR(cmd
) & _IOC_READ
) {
1260 v4l_print_ioctl("cx88", cmd
);
1263 v4l_print_ioctl(core
->name
,cmd
);
1268 /* ---------- tv norms ---------- */
1269 case VIDIOC_ENUMSTD
:
1271 struct v4l2_standard
*e
= arg
;
1275 if (i
>= ARRAY_SIZE(tvnorms
))
1277 err
= v4l2_video_std_construct(e
, tvnorms
[e
->index
].id
,
1278 tvnorms
[e
->index
].name
);
1286 v4l2_std_id
*id
= arg
;
1288 *id
= core
->tvnorm
->id
;
1293 v4l2_std_id
*id
= arg
;
1296 for(i
= 0; i
< ARRAY_SIZE(tvnorms
); i
++)
1297 if (*id
& tvnorms
[i
].id
)
1299 if (i
== ARRAY_SIZE(tvnorms
))
1302 mutex_lock(&core
->lock
);
1303 cx88_set_tvnorm(core
,&tvnorms
[i
]);
1304 mutex_unlock(&core
->lock
);
1308 /* ------ input switching ---------- */
1309 case VIDIOC_ENUMINPUT
:
1311 static const char *iname
[] = {
1312 [ CX88_VMUX_COMPOSITE1
] = "Composite1",
1313 [ CX88_VMUX_COMPOSITE2
] = "Composite2",
1314 [ CX88_VMUX_COMPOSITE3
] = "Composite3",
1315 [ CX88_VMUX_COMPOSITE4
] = "Composite4",
1316 [ CX88_VMUX_SVIDEO
] = "S-Video",
1317 [ CX88_VMUX_TELEVISION
] = "Television",
1318 [ CX88_VMUX_CABLE
] = "Cable TV",
1319 [ CX88_VMUX_DVB
] = "DVB",
1320 [ CX88_VMUX_DEBUG
] = "for debug only",
1322 struct v4l2_input
*i
= arg
;
1328 if (0 == INPUT(n
)->type
)
1330 memset(i
,0,sizeof(*i
));
1332 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1333 strcpy(i
->name
,iname
[INPUT(n
)->type
]);
1334 if ((CX88_VMUX_TELEVISION
== INPUT(n
)->type
) ||
1335 (CX88_VMUX_CABLE
== INPUT(n
)->type
))
1336 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1337 for (n
= 0; n
< ARRAY_SIZE(tvnorms
); n
++)
1338 i
->std
|= tvnorms
[n
].id
;
1341 case VIDIOC_G_INPUT
:
1343 unsigned int *i
= arg
;
1348 case VIDIOC_S_INPUT
:
1350 unsigned int *i
= arg
;
1354 mutex_lock(&core
->lock
);
1355 cx88_newstation(core
);
1357 mutex_unlock(&core
->lock
);
1363 /* --- controls ---------------------------------------------- */
1364 case VIDIOC_QUERYCTRL
:
1366 struct v4l2_queryctrl
*c
= arg
;
1369 if (c
->id
< V4L2_CID_BASE
||
1370 c
->id
>= V4L2_CID_LASTP1
)
1372 for (i
= 0; i
< CX8800_CTLS
; i
++)
1373 if (cx8800_ctls
[i
].v
.id
== c
->id
)
1375 if (i
== CX8800_CTLS
) {
1379 *c
= cx8800_ctls
[i
].v
;
1383 return get_control(core
,arg
);
1385 return set_control(core
,arg
);
1387 /* --- tuner ioctls ------------------------------------------ */
1388 case VIDIOC_G_TUNER
:
1390 struct v4l2_tuner
*t
= arg
;
1393 if (UNSET
== core
->tuner_type
)
1398 memset(t
,0,sizeof(*t
));
1399 strcpy(t
->name
, "Television");
1400 t
->type
= V4L2_TUNER_ANALOG_TV
;
1401 t
->capability
= V4L2_TUNER_CAP_NORM
;
1402 t
->rangehigh
= 0xffffffffUL
;
1404 cx88_get_stereo(core
,t
);
1405 reg
= cx_read(MO_DEVICE_STATUS
);
1406 t
->signal
= (reg
& (1<<5)) ? 0xffff : 0x0000;
1409 case VIDIOC_S_TUNER
:
1411 struct v4l2_tuner
*t
= arg
;
1413 if (UNSET
== core
->tuner_type
)
1417 cx88_set_stereo(core
, t
->audmode
, 1);
1420 case VIDIOC_G_FREQUENCY
:
1422 struct v4l2_frequency
*f
= arg
;
1424 memset(f
,0,sizeof(*f
));
1426 if (UNSET
== core
->tuner_type
)
1429 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1430 f
->type
= radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1431 f
->frequency
= core
->freq
;
1433 cx88_call_i2c_clients(core
,VIDIOC_G_FREQUENCY
,f
);
1437 case VIDIOC_S_FREQUENCY
:
1439 struct v4l2_frequency
*f
= arg
;
1441 if (UNSET
== core
->tuner_type
)
1445 if (0 == radio
&& f
->type
!= V4L2_TUNER_ANALOG_TV
)
1447 if (1 == radio
&& f
->type
!= V4L2_TUNER_RADIO
)
1449 mutex_lock(&core
->lock
);
1450 core
->freq
= f
->frequency
;
1451 cx88_newstation(core
);
1452 cx88_call_i2c_clients(core
,VIDIOC_S_FREQUENCY
,f
);
1454 /* When changing channels it is required to reset TVAUDIO */
1456 cx88_set_tvaudio(core
);
1458 mutex_unlock(&core
->lock
);
1463 return v4l_compat_translate_ioctl(inode
,file
,cmd
,arg
,
1469 static int video_ioctl(struct inode
*inode
, struct file
*file
,
1470 unsigned int cmd
, unsigned long arg
)
1474 retval
=video_usercopy(inode
, file
, cmd
, arg
, video_do_ioctl
);
1476 if (video_debug
> 1) {
1478 v4l_print_ioctl("cx88(err)", cmd
);
1479 printk(KERN_DEBUG
"cx88(err): errcode=%d\n",retval
);
1480 } else if (_IOC_DIR(cmd
) & _IOC_READ
)
1481 v4l_printk_ioctl_arg("cx88(r)",cmd
, (void *)arg
);
1487 /* ----------------------------------------------------------- */
1489 static int radio_do_ioctl(struct inode
*inode
, struct file
*file
,
1490 unsigned int cmd
, void *arg
)
1492 struct cx8800_fh
*fh
= file
->private_data
;
1493 struct cx8800_dev
*dev
= fh
->dev
;
1494 struct cx88_core
*core
= dev
->core
;
1496 if (video_debug
> 1)
1497 v4l_print_ioctl(core
->name
,cmd
);
1500 case VIDIOC_QUERYCAP
:
1502 struct v4l2_capability
*cap
= arg
;
1504 memset(cap
,0,sizeof(*cap
));
1505 strcpy(cap
->driver
, "cx8800");
1506 strlcpy(cap
->card
, cx88_boards
[core
->board
].name
,
1508 sprintf(cap
->bus_info
,"PCI:%s", pci_name(dev
->pci
));
1509 cap
->version
= CX88_VERSION_CODE
;
1510 cap
->capabilities
= V4L2_CAP_TUNER
;
1513 case VIDIOC_G_TUNER
:
1515 struct v4l2_tuner
*t
= arg
;
1520 memset(t
,0,sizeof(*t
));
1521 strcpy(t
->name
, "Radio");
1522 t
->type
= V4L2_TUNER_RADIO
;
1524 cx88_call_i2c_clients(core
,VIDIOC_G_TUNER
,t
);
1527 case VIDIOC_ENUMINPUT
:
1529 struct v4l2_input
*i
= arg
;
1533 strcpy(i
->name
,"Radio");
1534 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1537 case VIDIOC_G_INPUT
:
1543 case VIDIOC_G_AUDIO
:
1545 struct v4l2_audio
*a
= arg
;
1547 memset(a
,0,sizeof(*a
));
1548 strcpy(a
->name
,"Radio");
1553 v4l2_std_id
*id
= arg
;
1560 struct video_tuner
*v
= arg
;
1562 if (v
->tuner
) /* Only tuner 0 */
1565 cx88_call_i2c_clients(core
,VIDIOCSTUNER
,v
);
1569 case VIDIOC_S_TUNER
:
1571 struct v4l2_tuner
*t
= arg
;
1576 cx88_call_i2c_clients(core
,VIDIOC_S_TUNER
,t
);
1581 case VIDIOC_S_AUDIO
:
1582 case VIDIOC_S_INPUT
:
1586 case VIDIOC_QUERYCTRL
:
1588 struct v4l2_queryctrl
*c
= arg
;
1591 if (c
->id
< V4L2_CID_BASE
||
1592 c
->id
>= V4L2_CID_LASTP1
)
1594 if (c
->id
== V4L2_CID_AUDIO_MUTE
) {
1595 for (i
= 0; i
< CX8800_CTLS
; i
++)
1596 if (cx8800_ctls
[i
].v
.id
== c
->id
)
1598 *c
= cx8800_ctls
[i
].v
;
1607 case VIDIOC_G_FREQUENCY
:
1608 case VIDIOC_S_FREQUENCY
:
1609 return video_do_ioctl(inode
,file
,cmd
,arg
);
1612 return v4l_compat_translate_ioctl(inode
,file
,cmd
,arg
,
1618 static int radio_ioctl(struct inode
*inode
, struct file
*file
,
1619 unsigned int cmd
, unsigned long arg
)
1621 return video_usercopy(inode
, file
, cmd
, arg
, radio_do_ioctl
);
1624 /* ----------------------------------------------------------- */
1626 static void cx8800_vid_timeout(unsigned long data
)
1628 struct cx8800_dev
*dev
= (struct cx8800_dev
*)data
;
1629 struct cx88_core
*core
= dev
->core
;
1630 struct cx88_dmaqueue
*q
= &dev
->vidq
;
1631 struct cx88_buffer
*buf
;
1632 unsigned long flags
;
1634 cx88_sram_channel_dump(core
, &cx88_sram_channels
[SRAM_CH21
]);
1636 cx_clear(MO_VID_DMACNTRL
, 0x11);
1637 cx_clear(VID_CAPTURE_CONTROL
, 0x06);
1639 spin_lock_irqsave(&dev
->slock
,flags
);
1640 while (!list_empty(&q
->active
)) {
1641 buf
= list_entry(q
->active
.next
, struct cx88_buffer
, vb
.queue
);
1642 list_del(&buf
->vb
.queue
);
1643 buf
->vb
.state
= STATE_ERROR
;
1644 wake_up(&buf
->vb
.done
);
1645 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core
->name
,
1646 buf
, buf
->vb
.i
, (unsigned long)buf
->risc
.dma
);
1648 restart_video_queue(dev
,q
);
1649 spin_unlock_irqrestore(&dev
->slock
,flags
);
1652 static char *cx88_vid_irqs
[32] = {
1653 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1654 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1655 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1656 "y_sync", "u_sync", "v_sync", "vbi_sync",
1657 "opc_err", "par_err", "rip_err", "pci_abort",
1660 static void cx8800_vid_irq(struct cx8800_dev
*dev
)
1662 struct cx88_core
*core
= dev
->core
;
1663 u32 status
, mask
, count
;
1665 status
= cx_read(MO_VID_INTSTAT
);
1666 mask
= cx_read(MO_VID_INTMSK
);
1667 if (0 == (status
& mask
))
1669 cx_write(MO_VID_INTSTAT
, status
);
1670 if (irq_debug
|| (status
& mask
& ~0xff))
1671 cx88_print_irqbits(core
->name
, "irq vid",
1672 cx88_vid_irqs
, status
, mask
);
1674 /* risc op code error */
1675 if (status
& (1 << 16)) {
1676 printk(KERN_WARNING
"%s/0: video risc op code error\n",core
->name
);
1677 cx_clear(MO_VID_DMACNTRL
, 0x11);
1678 cx_clear(VID_CAPTURE_CONTROL
, 0x06);
1679 cx88_sram_channel_dump(core
, &cx88_sram_channels
[SRAM_CH21
]);
1683 if (status
& 0x01) {
1684 spin_lock(&dev
->slock
);
1685 count
= cx_read(MO_VIDY_GPCNT
);
1686 cx88_wakeup(core
, &dev
->vidq
, count
);
1687 spin_unlock(&dev
->slock
);
1691 if (status
& 0x08) {
1692 spin_lock(&dev
->slock
);
1693 count
= cx_read(MO_VBI_GPCNT
);
1694 cx88_wakeup(core
, &dev
->vbiq
, count
);
1695 spin_unlock(&dev
->slock
);
1699 if (status
& 0x10) {
1700 dprintk(2,"stopper video\n");
1701 spin_lock(&dev
->slock
);
1702 restart_video_queue(dev
,&dev
->vidq
);
1703 spin_unlock(&dev
->slock
);
1707 if (status
& 0x80) {
1708 dprintk(2,"stopper vbi\n");
1709 spin_lock(&dev
->slock
);
1710 cx8800_restart_vbi_queue(dev
,&dev
->vbiq
);
1711 spin_unlock(&dev
->slock
);
1715 static irqreturn_t
cx8800_irq(int irq
, void *dev_id
, struct pt_regs
*regs
)
1717 struct cx8800_dev
*dev
= dev_id
;
1718 struct cx88_core
*core
= dev
->core
;
1720 int loop
, handled
= 0;
1722 for (loop
= 0; loop
< 10; loop
++) {
1723 status
= cx_read(MO_PCI_INTSTAT
) & (core
->pci_irqmask
| 0x01);
1726 cx_write(MO_PCI_INTSTAT
, status
);
1729 if (status
& core
->pci_irqmask
)
1730 cx88_core_irq(core
,status
);
1732 cx8800_vid_irq(dev
);
1735 printk(KERN_WARNING
"%s/0: irq loop -- clearing mask\n",
1737 cx_write(MO_PCI_INTMSK
,0);
1741 return IRQ_RETVAL(handled
);
1744 /* ----------------------------------------------------------- */
1745 /* exported stuff */
1747 static struct file_operations video_fops
=
1749 .owner
= THIS_MODULE
,
1751 .release
= video_release
,
1755 .ioctl
= video_ioctl
,
1756 .compat_ioctl
= v4l_compat_ioctl32
,
1757 .llseek
= no_llseek
,
1760 static struct video_device cx8800_video_template
=
1762 .name
= "cx8800-video",
1763 .type
= VID_TYPE_CAPTURE
|VID_TYPE_TUNER
|VID_TYPE_SCALES
,
1765 .fops
= &video_fops
,
1769 static struct video_device cx8800_vbi_template
=
1771 .name
= "cx8800-vbi",
1772 .type
= VID_TYPE_TELETEXT
|VID_TYPE_TUNER
,
1774 .fops
= &video_fops
,
1778 static struct file_operations radio_fops
=
1780 .owner
= THIS_MODULE
,
1782 .release
= video_release
,
1783 .ioctl
= radio_ioctl
,
1784 .compat_ioctl
= v4l_compat_ioctl32
,
1785 .llseek
= no_llseek
,
1788 static struct video_device cx8800_radio_template
=
1790 .name
= "cx8800-radio",
1791 .type
= VID_TYPE_TUNER
,
1793 .fops
= &radio_fops
,
1797 /* ----------------------------------------------------------- */
1799 static void cx8800_unregister_video(struct cx8800_dev
*dev
)
1801 if (dev
->radio_dev
) {
1802 if (-1 != dev
->radio_dev
->minor
)
1803 video_unregister_device(dev
->radio_dev
);
1805 video_device_release(dev
->radio_dev
);
1806 dev
->radio_dev
= NULL
;
1809 if (-1 != dev
->vbi_dev
->minor
)
1810 video_unregister_device(dev
->vbi_dev
);
1812 video_device_release(dev
->vbi_dev
);
1813 dev
->vbi_dev
= NULL
;
1815 if (dev
->video_dev
) {
1816 if (-1 != dev
->video_dev
->minor
)
1817 video_unregister_device(dev
->video_dev
);
1819 video_device_release(dev
->video_dev
);
1820 dev
->video_dev
= NULL
;
1824 static int __devinit
cx8800_initdev(struct pci_dev
*pci_dev
,
1825 const struct pci_device_id
*pci_id
)
1827 struct cx8800_dev
*dev
;
1828 struct cx88_core
*core
;
1831 dev
= kzalloc(sizeof(*dev
),GFP_KERNEL
);
1837 if (pci_enable_device(pci_dev
)) {
1841 core
= cx88_core_get(dev
->pci
);
1848 /* print pci info */
1849 pci_read_config_byte(pci_dev
, PCI_CLASS_REVISION
, &dev
->pci_rev
);
1850 pci_read_config_byte(pci_dev
, PCI_LATENCY_TIMER
, &dev
->pci_lat
);
1851 printk(KERN_INFO
"%s/0: found at %s, rev: %d, irq: %d, "
1852 "latency: %d, mmio: 0x%lx\n", core
->name
,
1853 pci_name(pci_dev
), dev
->pci_rev
, pci_dev
->irq
,
1854 dev
->pci_lat
,pci_resource_start(pci_dev
,0));
1856 pci_set_master(pci_dev
);
1857 if (!pci_dma_supported(pci_dev
,0xffffffff)) {
1858 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core
->name
);
1863 /* initialize driver struct */
1864 spin_lock_init(&dev
->slock
);
1865 core
->tvnorm
= tvnorms
;
1867 /* init video dma queues */
1868 INIT_LIST_HEAD(&dev
->vidq
.active
);
1869 INIT_LIST_HEAD(&dev
->vidq
.queued
);
1870 dev
->vidq
.timeout
.function
= cx8800_vid_timeout
;
1871 dev
->vidq
.timeout
.data
= (unsigned long)dev
;
1872 init_timer(&dev
->vidq
.timeout
);
1873 cx88_risc_stopper(dev
->pci
,&dev
->vidq
.stopper
,
1874 MO_VID_DMACNTRL
,0x11,0x00);
1876 /* init vbi dma queues */
1877 INIT_LIST_HEAD(&dev
->vbiq
.active
);
1878 INIT_LIST_HEAD(&dev
->vbiq
.queued
);
1879 dev
->vbiq
.timeout
.function
= cx8800_vbi_timeout
;
1880 dev
->vbiq
.timeout
.data
= (unsigned long)dev
;
1881 init_timer(&dev
->vbiq
.timeout
);
1882 cx88_risc_stopper(dev
->pci
,&dev
->vbiq
.stopper
,
1883 MO_VID_DMACNTRL
,0x88,0x00);
1886 err
= request_irq(pci_dev
->irq
, cx8800_irq
,
1887 SA_SHIRQ
| SA_INTERRUPT
, core
->name
, dev
);
1889 printk(KERN_ERR
"%s: can't get IRQ %d\n",
1890 core
->name
,pci_dev
->irq
);
1893 cx_set(MO_PCI_INTMSK
, core
->pci_irqmask
);
1895 /* load and configure helper modules */
1896 if (TUNER_ABSENT
!= core
->tuner_type
)
1897 request_module("tuner");
1898 if (core
->tda9887_conf
)
1899 request_module("tda9887");
1901 /* register v4l devices */
1902 dev
->video_dev
= cx88_vdev_init(core
,dev
->pci
,
1903 &cx8800_video_template
,"video");
1904 err
= video_register_device(dev
->video_dev
,VFL_TYPE_GRABBER
,
1905 video_nr
[core
->nr
]);
1907 printk(KERN_INFO
"%s: can't register video device\n",
1911 printk(KERN_INFO
"%s/0: registered device video%d [v4l2]\n",
1912 core
->name
,dev
->video_dev
->minor
& 0x1f);
1914 dev
->vbi_dev
= cx88_vdev_init(core
,dev
->pci
,&cx8800_vbi_template
,"vbi");
1915 err
= video_register_device(dev
->vbi_dev
,VFL_TYPE_VBI
,
1918 printk(KERN_INFO
"%s/0: can't register vbi device\n",
1922 printk(KERN_INFO
"%s/0: registered device vbi%d\n",
1923 core
->name
,dev
->vbi_dev
->minor
& 0x1f);
1925 if (core
->has_radio
) {
1926 dev
->radio_dev
= cx88_vdev_init(core
,dev
->pci
,
1927 &cx8800_radio_template
,"radio");
1928 err
= video_register_device(dev
->radio_dev
,VFL_TYPE_RADIO
,
1929 radio_nr
[core
->nr
]);
1931 printk(KERN_INFO
"%s/0: can't register radio device\n",
1935 printk(KERN_INFO
"%s/0: registered device radio%d\n",
1936 core
->name
,dev
->radio_dev
->minor
& 0x1f);
1939 /* everything worked */
1940 list_add_tail(&dev
->devlist
,&cx8800_devlist
);
1941 pci_set_drvdata(pci_dev
,dev
);
1943 /* initial device configuration */
1944 mutex_lock(&core
->lock
);
1945 cx88_set_tvnorm(core
,tvnorms
);
1946 init_controls(core
);
1948 mutex_unlock(&core
->lock
);
1950 /* start tvaudio thread */
1951 if (core
->tuner_type
!= TUNER_ABSENT
)
1952 core
->kthread
= kthread_run(cx88_audio_thread
, core
, "cx88 tvaudio");
1956 cx8800_unregister_video(dev
);
1957 free_irq(pci_dev
->irq
, dev
);
1959 cx88_core_put(core
,dev
->pci
);
1965 static void __devexit
cx8800_finidev(struct pci_dev
*pci_dev
)
1967 struct cx8800_dev
*dev
= pci_get_drvdata(pci_dev
);
1968 struct cx88_core
*core
= dev
->core
;
1971 if (core
->kthread
) {
1972 kthread_stop(core
->kthread
);
1973 core
->kthread
= NULL
;
1976 cx88_shutdown(core
); /* FIXME */
1977 pci_disable_device(pci_dev
);
1979 /* unregister stuff */
1981 free_irq(pci_dev
->irq
, dev
);
1982 cx8800_unregister_video(dev
);
1983 pci_set_drvdata(pci_dev
, NULL
);
1986 btcx_riscmem_free(dev
->pci
,&dev
->vidq
.stopper
);
1987 list_del(&dev
->devlist
);
1988 cx88_core_put(core
,dev
->pci
);
1992 static int cx8800_suspend(struct pci_dev
*pci_dev
, pm_message_t state
)
1994 struct cx8800_dev
*dev
= pci_get_drvdata(pci_dev
);
1995 struct cx88_core
*core
= dev
->core
;
1997 /* stop video+vbi capture */
1998 spin_lock(&dev
->slock
);
1999 if (!list_empty(&dev
->vidq
.active
)) {
2000 printk("%s: suspend video\n", core
->name
);
2001 stop_video_dma(dev
);
2002 del_timer(&dev
->vidq
.timeout
);
2004 if (!list_empty(&dev
->vbiq
.active
)) {
2005 printk("%s: suspend vbi\n", core
->name
);
2006 cx8800_stop_vbi_dma(dev
);
2007 del_timer(&dev
->vbiq
.timeout
);
2009 spin_unlock(&dev
->slock
);
2011 /* FIXME -- shutdown device */
2012 cx88_shutdown(core
);
2014 pci_save_state(pci_dev
);
2015 if (0 != pci_set_power_state(pci_dev
, pci_choose_state(pci_dev
, state
))) {
2016 pci_disable_device(pci_dev
);
2017 dev
->state
.disabled
= 1;
2022 static int cx8800_resume(struct pci_dev
*pci_dev
)
2024 struct cx8800_dev
*dev
= pci_get_drvdata(pci_dev
);
2025 struct cx88_core
*core
= dev
->core
;
2028 if (dev
->state
.disabled
) {
2029 err
=pci_enable_device(pci_dev
);
2031 printk(KERN_ERR
"%s: can't enable device\n",
2036 dev
->state
.disabled
= 0;
2038 err
= pci_set_power_state(pci_dev
, PCI_D0
);
2040 printk(KERN_ERR
"%s: can't enable device\n",
2043 pci_disable_device(pci_dev
);
2044 dev
->state
.disabled
= 1;
2048 pci_restore_state(pci_dev
);
2050 /* FIXME: re-initialize hardware */
2053 /* restart video+vbi capture */
2054 spin_lock(&dev
->slock
);
2055 if (!list_empty(&dev
->vidq
.active
)) {
2056 printk("%s: resume video\n", core
->name
);
2057 restart_video_queue(dev
,&dev
->vidq
);
2059 if (!list_empty(&dev
->vbiq
.active
)) {
2060 printk("%s: resume vbi\n", core
->name
);
2061 cx8800_restart_vbi_queue(dev
,&dev
->vbiq
);
2063 spin_unlock(&dev
->slock
);
2068 /* ----------------------------------------------------------- */
2070 static struct pci_device_id cx8800_pci_tbl
[] = {
2074 .subvendor
= PCI_ANY_ID
,
2075 .subdevice
= PCI_ANY_ID
,
2077 /* --- end of list --- */
2080 MODULE_DEVICE_TABLE(pci
, cx8800_pci_tbl
);
2082 static struct pci_driver cx8800_pci_driver
= {
2084 .id_table
= cx8800_pci_tbl
,
2085 .probe
= cx8800_initdev
,
2086 .remove
= __devexit_p(cx8800_finidev
),
2088 .suspend
= cx8800_suspend
,
2089 .resume
= cx8800_resume
,
2092 static int cx8800_init(void)
2094 printk(KERN_INFO
"cx2388x v4l2 driver version %d.%d.%d loaded\n",
2095 (CX88_VERSION_CODE
>> 16) & 0xff,
2096 (CX88_VERSION_CODE
>> 8) & 0xff,
2097 CX88_VERSION_CODE
& 0xff);
2099 printk(KERN_INFO
"cx2388x: snapshot date %04d-%02d-%02d\n",
2100 SNAPSHOT
/10000, (SNAPSHOT
/100)%100, SNAPSHOT
%100);
2102 return pci_register_driver(&cx8800_pci_driver
);
2105 static void cx8800_fini(void)
2107 pci_unregister_driver(&cx8800_pci_driver
);
2110 module_init(cx8800_init
);
2111 module_exit(cx8800_fini
);
2113 EXPORT_SYMBOL(cx88_do_ioctl
);
2115 /* ----------------------------------------------------------- */
2120 * 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