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 * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
10 * - video_ioctl2 conversion
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/module.h>
31 #include <linux/kmod.h>
32 #include <linux/kernel.h>
33 #include <linux/slab.h>
34 #include <linux/interrupt.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/delay.h>
37 #include <linux/kthread.h>
38 #include <asm/div64.h>
41 #include <media/v4l2-common.h>
43 #ifdef CONFIG_VIDEO_V4L1_COMPAT
44 /* Include V4L1 specific functions. Should be removed soon */
45 #include <linux/videodev.h>
48 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
49 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
50 MODULE_LICENSE("GPL");
52 /* ------------------------------------------------------------------ */
54 static unsigned int video_nr
[] = {[0 ... (CX88_MAXBOARDS
- 1)] = UNSET
};
55 static unsigned int vbi_nr
[] = {[0 ... (CX88_MAXBOARDS
- 1)] = UNSET
};
56 static unsigned int radio_nr
[] = {[0 ... (CX88_MAXBOARDS
- 1)] = UNSET
};
58 module_param_array(video_nr
, int, NULL
, 0444);
59 module_param_array(vbi_nr
, int, NULL
, 0444);
60 module_param_array(radio_nr
, int, NULL
, 0444);
62 MODULE_PARM_DESC(video_nr
,"video device numbers");
63 MODULE_PARM_DESC(vbi_nr
,"vbi device numbers");
64 MODULE_PARM_DESC(radio_nr
,"radio device numbers");
66 static unsigned int video_debug
= 0;
67 module_param(video_debug
,int,0644);
68 MODULE_PARM_DESC(video_debug
,"enable debug messages [video]");
70 static unsigned int irq_debug
= 0;
71 module_param(irq_debug
,int,0644);
72 MODULE_PARM_DESC(irq_debug
,"enable debug messages [IRQ handler]");
74 static unsigned int vid_limit
= 16;
75 module_param(vid_limit
,int,0644);
76 MODULE_PARM_DESC(vid_limit
,"capture memory limit in megabytes");
78 #define dprintk(level,fmt, arg...) if (video_debug >= level) \
79 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
81 /* ------------------------------------------------------------------ */
83 static LIST_HEAD(cx8800_devlist
);
85 /* ------------------------------------------------------------------- */
88 static struct cx8800_fmt formats
[] = {
90 .name
= "8 bpp, gray",
91 .fourcc
= V4L2_PIX_FMT_GREY
,
92 .cxformat
= ColorFormatY8
,
94 .flags
= FORMAT_FLAGS_PACKED
,
96 .name
= "15 bpp RGB, le",
97 .fourcc
= V4L2_PIX_FMT_RGB555
,
98 .cxformat
= ColorFormatRGB15
,
100 .flags
= FORMAT_FLAGS_PACKED
,
102 .name
= "15 bpp RGB, be",
103 .fourcc
= V4L2_PIX_FMT_RGB555X
,
104 .cxformat
= ColorFormatRGB15
| ColorFormatBSWAP
,
106 .flags
= FORMAT_FLAGS_PACKED
,
108 .name
= "16 bpp RGB, le",
109 .fourcc
= V4L2_PIX_FMT_RGB565
,
110 .cxformat
= ColorFormatRGB16
,
112 .flags
= FORMAT_FLAGS_PACKED
,
114 .name
= "16 bpp RGB, be",
115 .fourcc
= V4L2_PIX_FMT_RGB565X
,
116 .cxformat
= ColorFormatRGB16
| ColorFormatBSWAP
,
118 .flags
= FORMAT_FLAGS_PACKED
,
120 .name
= "24 bpp RGB, le",
121 .fourcc
= V4L2_PIX_FMT_BGR24
,
122 .cxformat
= ColorFormatRGB24
,
124 .flags
= FORMAT_FLAGS_PACKED
,
126 .name
= "32 bpp RGB, le",
127 .fourcc
= V4L2_PIX_FMT_BGR32
,
128 .cxformat
= ColorFormatRGB32
,
130 .flags
= FORMAT_FLAGS_PACKED
,
132 .name
= "32 bpp RGB, be",
133 .fourcc
= V4L2_PIX_FMT_RGB32
,
134 .cxformat
= ColorFormatRGB32
| ColorFormatBSWAP
| ColorFormatWSWAP
,
136 .flags
= FORMAT_FLAGS_PACKED
,
138 .name
= "4:2:2, packed, YUYV",
139 .fourcc
= V4L2_PIX_FMT_YUYV
,
140 .cxformat
= ColorFormatYUY2
,
142 .flags
= FORMAT_FLAGS_PACKED
,
144 .name
= "4:2:2, packed, UYVY",
145 .fourcc
= V4L2_PIX_FMT_UYVY
,
146 .cxformat
= ColorFormatYUY2
| ColorFormatBSWAP
,
148 .flags
= FORMAT_FLAGS_PACKED
,
152 static struct cx8800_fmt
* format_by_fourcc(unsigned int fourcc
)
156 for (i
= 0; i
< ARRAY_SIZE(formats
); i
++)
157 if (formats
[i
].fourcc
== fourcc
)
162 /* ------------------------------------------------------------------- */
164 static const struct v4l2_queryctrl no_ctl
= {
166 .flags
= V4L2_CTRL_FLAG_DISABLED
,
169 static struct cx88_ctrl cx8800_ctls
[] = {
173 .id
= V4L2_CID_BRIGHTNESS
,
174 .name
= "Brightness",
178 .default_value
= 0x7f,
179 .type
= V4L2_CTRL_TYPE_INTEGER
,
182 .reg
= MO_CONTR_BRIGHT
,
187 .id
= V4L2_CID_CONTRAST
,
192 .default_value
= 0x3f,
193 .type
= V4L2_CTRL_TYPE_INTEGER
,
196 .reg
= MO_CONTR_BRIGHT
,
206 .default_value
= 0x7f,
207 .type
= V4L2_CTRL_TYPE_INTEGER
,
214 /* strictly, this only describes only U saturation.
215 * V saturation is handled specially through code.
218 .id
= V4L2_CID_SATURATION
,
219 .name
= "Saturation",
223 .default_value
= 0x7f,
224 .type
= V4L2_CTRL_TYPE_INTEGER
,
227 .reg
= MO_UV_SATURATION
,
233 .id
= V4L2_CID_AUDIO_MUTE
,
238 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
241 .sreg
= SHADOW_AUD_VOL_CTL
,
246 .id
= V4L2_CID_AUDIO_VOLUME
,
251 .default_value
= 0x3f,
252 .type
= V4L2_CTRL_TYPE_INTEGER
,
255 .sreg
= SHADOW_AUD_VOL_CTL
,
260 .id
= V4L2_CID_AUDIO_BALANCE
,
265 .default_value
= 0x40,
266 .type
= V4L2_CTRL_TYPE_INTEGER
,
269 .sreg
= SHADOW_AUD_BAL_CTL
,
274 static const int CX8800_CTLS
= ARRAY_SIZE(cx8800_ctls
);
276 const u32 cx88_user_ctrls
[] = {
282 V4L2_CID_AUDIO_VOLUME
,
283 V4L2_CID_AUDIO_BALANCE
,
287 EXPORT_SYMBOL(cx88_user_ctrls
);
289 static const u32
*ctrl_classes
[] = {
294 int cx8800_ctrl_query(struct v4l2_queryctrl
*qctrl
)
298 if (qctrl
->id
< V4L2_CID_BASE
||
299 qctrl
->id
>= V4L2_CID_LASTP1
)
301 for (i
= 0; i
< CX8800_CTLS
; i
++)
302 if (cx8800_ctls
[i
].v
.id
== qctrl
->id
)
304 if (i
== CX8800_CTLS
) {
308 *qctrl
= cx8800_ctls
[i
].v
;
311 EXPORT_SYMBOL(cx8800_ctrl_query
);
313 /* ------------------------------------------------------------------- */
314 /* resource management */
316 static int res_get(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
, unsigned int bit
)
318 struct cx88_core
*core
= dev
->core
;
319 if (fh
->resources
& bit
)
320 /* have it already allocated */
324 mutex_lock(&core
->lock
);
325 if (dev
->resources
& bit
) {
326 /* no, someone else uses it */
327 mutex_unlock(&core
->lock
);
330 /* it's free, grab it */
331 fh
->resources
|= bit
;
332 dev
->resources
|= bit
;
333 dprintk(1,"res: get %d\n",bit
);
334 mutex_unlock(&core
->lock
);
339 int res_check(struct cx8800_fh
*fh
, unsigned int bit
)
341 return (fh
->resources
& bit
);
345 int res_locked(struct cx8800_dev
*dev
, unsigned int bit
)
347 return (dev
->resources
& bit
);
351 void res_free(struct cx8800_dev
*dev
, struct cx8800_fh
*fh
, unsigned int bits
)
353 struct cx88_core
*core
= dev
->core
;
354 BUG_ON((fh
->resources
& bits
) != bits
);
356 mutex_lock(&core
->lock
);
357 fh
->resources
&= ~bits
;
358 dev
->resources
&= ~bits
;
359 dprintk(1,"res: put %d\n",bits
);
360 mutex_unlock(&core
->lock
);
363 /* ------------------------------------------------------------------ */
365 int cx88_video_mux(struct cx88_core
*core
, unsigned int input
)
367 /* struct cx88_core *core = dev->core; */
369 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
370 input
, INPUT(input
).vmux
,
371 INPUT(input
).gpio0
,INPUT(input
).gpio1
,
372 INPUT(input
).gpio2
,INPUT(input
).gpio3
);
374 cx_andor(MO_INPUT_FORMAT
, 0x03 << 14, INPUT(input
).vmux
<< 14);
375 cx_write(MO_GP3_IO
, INPUT(input
).gpio3
);
376 cx_write(MO_GP0_IO
, INPUT(input
).gpio0
);
377 cx_write(MO_GP1_IO
, INPUT(input
).gpio1
);
378 cx_write(MO_GP2_IO
, INPUT(input
).gpio2
);
380 switch (INPUT(input
).type
) {
381 case CX88_VMUX_SVIDEO
:
382 cx_set(MO_AFECFG_IO
, 0x00000001);
383 cx_set(MO_INPUT_FORMAT
, 0x00010010);
384 cx_set(MO_FILTER_EVEN
, 0x00002020);
385 cx_set(MO_FILTER_ODD
, 0x00002020);
388 cx_clear(MO_AFECFG_IO
, 0x00000001);
389 cx_clear(MO_INPUT_FORMAT
, 0x00010010);
390 cx_clear(MO_FILTER_EVEN
, 0x00002020);
391 cx_clear(MO_FILTER_ODD
, 0x00002020);
395 if (core
->board
.mpeg
& CX88_MPEG_BLACKBIRD
) {
396 /* sets sound input from external adc */
397 if (INPUT(input
).extadc
)
398 cx_set(AUD_CTL
, EN_I2SIN_ENABLE
);
400 cx_clear(AUD_CTL
, EN_I2SIN_ENABLE
);
404 EXPORT_SYMBOL(cx88_video_mux
);
406 /* ------------------------------------------------------------------ */
408 static int start_video_dma(struct cx8800_dev
*dev
,
409 struct cx88_dmaqueue
*q
,
410 struct cx88_buffer
*buf
)
412 struct cx88_core
*core
= dev
->core
;
414 /* setup fifo + format */
415 cx88_sram_channel_setup(core
, &cx88_sram_channels
[SRAM_CH21
],
416 buf
->bpl
, buf
->risc
.dma
);
417 cx88_set_scale(core
, buf
->vb
.width
, buf
->vb
.height
, buf
->vb
.field
);
418 cx_write(MO_COLOR_CTRL
, buf
->fmt
->cxformat
| ColorFormatGamma
);
421 cx_write(MO_VIDY_GPCNTRL
,GP_COUNT_CONTROL_RESET
);
425 cx_set(MO_PCI_INTMSK
, core
->pci_irqmask
| PCI_INT_VIDINT
);
427 /* Enables corresponding bits at PCI_INT_STAT:
428 bits 0 to 4: video, audio, transport stream, VIP, Host
430 bits 8 and 9: DMA complete for: SRC, DST
431 bits 10 and 11: BERR signal asserted for RISC: RD, WR
432 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
434 cx_set(MO_VID_INTMSK
, 0x0f0011);
437 cx_set(VID_CAPTURE_CONTROL
,0x06);
440 cx_set(MO_DEV_CNTRL2
, (1<<5));
441 cx_set(MO_VID_DMACNTRL
, 0x11); /* Planar Y and packed FIFO and RISC enable */
447 static int stop_video_dma(struct cx8800_dev
*dev
)
449 struct cx88_core
*core
= dev
->core
;
452 cx_clear(MO_VID_DMACNTRL
, 0x11);
454 /* disable capture */
455 cx_clear(VID_CAPTURE_CONTROL
,0x06);
458 cx_clear(MO_PCI_INTMSK
, PCI_INT_VIDINT
);
459 cx_clear(MO_VID_INTMSK
, 0x0f0011);
464 static int restart_video_queue(struct cx8800_dev
*dev
,
465 struct cx88_dmaqueue
*q
)
467 struct cx88_core
*core
= dev
->core
;
468 struct cx88_buffer
*buf
, *prev
;
470 if (!list_empty(&q
->active
)) {
471 buf
= list_entry(q
->active
.next
, struct cx88_buffer
, vb
.queue
);
472 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
474 start_video_dma(dev
, q
, buf
);
475 list_for_each_entry(buf
, &q
->active
, vb
.queue
)
476 buf
->count
= q
->count
++;
477 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
483 if (list_empty(&q
->queued
))
485 buf
= list_entry(q
->queued
.next
, struct cx88_buffer
, vb
.queue
);
487 list_move_tail(&buf
->vb
.queue
, &q
->active
);
488 start_video_dma(dev
, q
, buf
);
489 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
490 buf
->count
= q
->count
++;
491 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
492 dprintk(2,"[%p/%d] restart_queue - first active\n",
495 } else if (prev
->vb
.width
== buf
->vb
.width
&&
496 prev
->vb
.height
== buf
->vb
.height
&&
497 prev
->fmt
== buf
->fmt
) {
498 list_move_tail(&buf
->vb
.queue
, &q
->active
);
499 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
500 buf
->count
= q
->count
++;
501 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
502 dprintk(2,"[%p/%d] restart_queue - move to active\n",
511 /* ------------------------------------------------------------------ */
514 buffer_setup(struct videobuf_queue
*q
, unsigned int *count
, unsigned int *size
)
516 struct cx8800_fh
*fh
= q
->priv_data
;
518 *size
= fh
->fmt
->depth
*fh
->width
*fh
->height
>> 3;
521 while (*size
* *count
> vid_limit
* 1024 * 1024)
527 buffer_prepare(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
528 enum v4l2_field field
)
530 struct cx8800_fh
*fh
= q
->priv_data
;
531 struct cx8800_dev
*dev
= fh
->dev
;
532 struct cx88_core
*core
= dev
->core
;
533 struct cx88_buffer
*buf
= container_of(vb
,struct cx88_buffer
,vb
);
534 struct videobuf_dmabuf
*dma
=videobuf_to_dma(&buf
->vb
);
535 int rc
, init_buffer
= 0;
537 BUG_ON(NULL
== fh
->fmt
);
538 if (fh
->width
< 48 || fh
->width
> norm_maxw(core
->tvnorm
) ||
539 fh
->height
< 32 || fh
->height
> norm_maxh(core
->tvnorm
))
541 buf
->vb
.size
= (fh
->width
* fh
->height
* fh
->fmt
->depth
) >> 3;
542 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
545 if (buf
->fmt
!= fh
->fmt
||
546 buf
->vb
.width
!= fh
->width
||
547 buf
->vb
.height
!= fh
->height
||
548 buf
->vb
.field
!= field
) {
550 buf
->vb
.width
= fh
->width
;
551 buf
->vb
.height
= fh
->height
;
552 buf
->vb
.field
= field
;
556 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
558 if (0 != (rc
= videobuf_iolock(q
,&buf
->vb
,NULL
)))
563 buf
->bpl
= buf
->vb
.width
* buf
->fmt
->depth
>> 3;
564 switch (buf
->vb
.field
) {
566 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
567 dma
->sglist
, 0, UNSET
,
568 buf
->bpl
, 0, buf
->vb
.height
);
570 case V4L2_FIELD_BOTTOM
:
571 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
572 dma
->sglist
, UNSET
, 0,
573 buf
->bpl
, 0, buf
->vb
.height
);
575 case V4L2_FIELD_INTERLACED
:
576 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
577 dma
->sglist
, 0, buf
->bpl
,
579 buf
->vb
.height
>> 1);
581 case V4L2_FIELD_SEQ_TB
:
582 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
584 0, buf
->bpl
* (buf
->vb
.height
>> 1),
586 buf
->vb
.height
>> 1);
588 case V4L2_FIELD_SEQ_BT
:
589 cx88_risc_buffer(dev
->pci
, &buf
->risc
,
591 buf
->bpl
* (buf
->vb
.height
>> 1), 0,
593 buf
->vb
.height
>> 1);
599 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
601 fh
->width
, fh
->height
, fh
->fmt
->depth
, fh
->fmt
->name
,
602 (unsigned long)buf
->risc
.dma
);
604 buf
->vb
.state
= VIDEOBUF_PREPARED
;
608 cx88_free_buffer(q
,buf
);
613 buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
615 struct cx88_buffer
*buf
= container_of(vb
,struct cx88_buffer
,vb
);
616 struct cx88_buffer
*prev
;
617 struct cx8800_fh
*fh
= vq
->priv_data
;
618 struct cx8800_dev
*dev
= fh
->dev
;
619 struct cx88_core
*core
= dev
->core
;
620 struct cx88_dmaqueue
*q
= &dev
->vidq
;
622 /* add jump to stopper */
623 buf
->risc
.jmp
[0] = cpu_to_le32(RISC_JUMP
| RISC_IRQ1
| RISC_CNT_INC
);
624 buf
->risc
.jmp
[1] = cpu_to_le32(q
->stopper
.dma
);
626 if (!list_empty(&q
->queued
)) {
627 list_add_tail(&buf
->vb
.queue
,&q
->queued
);
628 buf
->vb
.state
= VIDEOBUF_QUEUED
;
629 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
632 } else if (list_empty(&q
->active
)) {
633 list_add_tail(&buf
->vb
.queue
,&q
->active
);
634 start_video_dma(dev
, q
, buf
);
635 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
636 buf
->count
= q
->count
++;
637 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
638 dprintk(2,"[%p/%d] buffer_queue - first active\n",
642 prev
= list_entry(q
->active
.prev
, struct cx88_buffer
, vb
.queue
);
643 if (prev
->vb
.width
== buf
->vb
.width
&&
644 prev
->vb
.height
== buf
->vb
.height
&&
645 prev
->fmt
== buf
->fmt
) {
646 list_add_tail(&buf
->vb
.queue
,&q
->active
);
647 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
648 buf
->count
= q
->count
++;
649 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
650 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
654 list_add_tail(&buf
->vb
.queue
,&q
->queued
);
655 buf
->vb
.state
= VIDEOBUF_QUEUED
;
656 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
662 static void buffer_release(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
)
664 struct cx88_buffer
*buf
= container_of(vb
,struct cx88_buffer
,vb
);
666 cx88_free_buffer(q
,buf
);
669 static struct videobuf_queue_ops cx8800_video_qops
= {
670 .buf_setup
= buffer_setup
,
671 .buf_prepare
= buffer_prepare
,
672 .buf_queue
= buffer_queue
,
673 .buf_release
= buffer_release
,
676 /* ------------------------------------------------------------------ */
679 /* ------------------------------------------------------------------ */
681 static struct videobuf_queue
* get_queue(struct cx8800_fh
*fh
)
684 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
686 case V4L2_BUF_TYPE_VBI_CAPTURE
:
694 static int get_ressource(struct cx8800_fh
*fh
)
697 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
698 return RESOURCE_VIDEO
;
699 case V4L2_BUF_TYPE_VBI_CAPTURE
:
707 static int video_open(struct inode
*inode
, struct file
*file
)
709 int minor
= iminor(inode
);
710 struct cx8800_dev
*h
,*dev
= NULL
;
711 struct cx88_core
*core
;
712 struct cx8800_fh
*fh
;
713 enum v4l2_buf_type type
= 0;
716 list_for_each_entry(h
, &cx8800_devlist
, devlist
) {
717 if (h
->video_dev
->minor
== minor
) {
719 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
721 if (h
->vbi_dev
->minor
== minor
) {
723 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
726 h
->radio_dev
->minor
== minor
) {
736 dprintk(1,"open minor=%d radio=%d type=%s\n",
737 minor
,radio
,v4l2_type_names
[type
]);
739 /* allocate + initialize per filehandle data */
740 fh
= kzalloc(sizeof(*fh
),GFP_KERNEL
);
743 file
->private_data
= fh
;
749 fh
->fmt
= format_by_fourcc(V4L2_PIX_FMT_BGR24
);
751 videobuf_queue_pci_init(&fh
->vidq
, &cx8800_video_qops
,
752 dev
->pci
, &dev
->slock
,
753 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
754 V4L2_FIELD_INTERLACED
,
755 sizeof(struct cx88_buffer
),
757 videobuf_queue_pci_init(&fh
->vbiq
, &cx8800_vbi_qops
,
758 dev
->pci
, &dev
->slock
,
759 V4L2_BUF_TYPE_VBI_CAPTURE
,
761 sizeof(struct cx88_buffer
),
765 dprintk(1,"video_open: setting radio device\n");
766 cx_write(MO_GP3_IO
, core
->board
.radio
.gpio3
);
767 cx_write(MO_GP0_IO
, core
->board
.radio
.gpio0
);
768 cx_write(MO_GP1_IO
, core
->board
.radio
.gpio1
);
769 cx_write(MO_GP2_IO
, core
->board
.radio
.gpio2
);
770 core
->tvaudio
= WW_FM
;
771 cx88_set_tvaudio(core
);
772 cx88_set_stereo(core
,V4L2_TUNER_MODE_STEREO
,1);
773 cx88_call_i2c_clients(core
,AUDC_SET_RADIO
,NULL
);
780 video_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
782 struct cx8800_fh
*fh
= file
->private_data
;
785 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
786 if (res_locked(fh
->dev
,RESOURCE_VIDEO
))
788 return videobuf_read_one(&fh
->vidq
, data
, count
, ppos
,
789 file
->f_flags
& O_NONBLOCK
);
790 case V4L2_BUF_TYPE_VBI_CAPTURE
:
791 if (!res_get(fh
->dev
,fh
,RESOURCE_VBI
))
793 return videobuf_read_stream(&fh
->vbiq
, data
, count
, ppos
, 1,
794 file
->f_flags
& O_NONBLOCK
);
802 video_poll(struct file
*file
, struct poll_table_struct
*wait
)
804 struct cx8800_fh
*fh
= file
->private_data
;
805 struct cx88_buffer
*buf
;
807 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
) {
808 if (!res_get(fh
->dev
,fh
,RESOURCE_VBI
))
810 return videobuf_poll_stream(file
, &fh
->vbiq
, wait
);
813 if (res_check(fh
,RESOURCE_VIDEO
)) {
814 /* streaming capture */
815 if (list_empty(&fh
->vidq
.stream
))
817 buf
= list_entry(fh
->vidq
.stream
.next
,struct cx88_buffer
,vb
.stream
);
820 buf
= (struct cx88_buffer
*)fh
->vidq
.read_buf
;
824 poll_wait(file
, &buf
->vb
.done
, wait
);
825 if (buf
->vb
.state
== VIDEOBUF_DONE
||
826 buf
->vb
.state
== VIDEOBUF_ERROR
)
827 return POLLIN
|POLLRDNORM
;
831 static int video_release(struct inode
*inode
, struct file
*file
)
833 struct cx8800_fh
*fh
= file
->private_data
;
834 struct cx8800_dev
*dev
= fh
->dev
;
836 /* turn off overlay */
837 if (res_check(fh
, RESOURCE_OVERLAY
)) {
839 res_free(dev
,fh
,RESOURCE_OVERLAY
);
842 /* stop video capture */
843 if (res_check(fh
, RESOURCE_VIDEO
)) {
844 videobuf_queue_cancel(&fh
->vidq
);
845 res_free(dev
,fh
,RESOURCE_VIDEO
);
847 if (fh
->vidq
.read_buf
) {
848 buffer_release(&fh
->vidq
,fh
->vidq
.read_buf
);
849 kfree(fh
->vidq
.read_buf
);
852 /* stop vbi capture */
853 if (res_check(fh
, RESOURCE_VBI
)) {
854 videobuf_stop(&fh
->vbiq
);
855 res_free(dev
,fh
,RESOURCE_VBI
);
858 videobuf_mmap_free(&fh
->vidq
);
859 videobuf_mmap_free(&fh
->vbiq
);
860 file
->private_data
= NULL
;
863 cx88_call_i2c_clients (dev
->core
, TUNER_SET_STANDBY
, NULL
);
869 video_mmap(struct file
*file
, struct vm_area_struct
* vma
)
871 struct cx8800_fh
*fh
= file
->private_data
;
873 return videobuf_mmap_mapper(get_queue(fh
), vma
);
876 /* ------------------------------------------------------------------ */
877 /* VIDEO CTRL IOCTLS */
879 int cx88_get_control (struct cx88_core
*core
, struct v4l2_control
*ctl
)
881 struct cx88_ctrl
*c
= NULL
;
885 for (i
= 0; i
< CX8800_CTLS
; i
++)
886 if (cx8800_ctls
[i
].v
.id
== ctl
->id
)
888 if (unlikely(NULL
== c
))
891 value
= c
->sreg
? cx_sread(c
->sreg
) : cx_read(c
->reg
);
893 case V4L2_CID_AUDIO_BALANCE
:
894 ctl
->value
= ((value
& 0x7f) < 0x40) ? ((value
& 0x7f) + 0x40)
895 : (0x7f - (value
& 0x7f));
897 case V4L2_CID_AUDIO_VOLUME
:
898 ctl
->value
= 0x3f - (value
& 0x3f);
901 ctl
->value
= ((value
+ (c
->off
<< c
->shift
)) & c
->mask
) >> c
->shift
;
904 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
905 ctl
->id
, c
->v
.name
, ctl
->value
, c
->reg
,
906 value
,c
->mask
, c
->sreg
? " [shadowed]" : "");
909 EXPORT_SYMBOL(cx88_get_control
);
911 int cx88_set_control(struct cx88_core
*core
, struct v4l2_control
*ctl
)
913 struct cx88_ctrl
*c
= NULL
;
917 for (i
= 0; i
< CX8800_CTLS
; i
++) {
918 if (cx8800_ctls
[i
].v
.id
== ctl
->id
) {
922 if (unlikely(NULL
== c
))
925 if (ctl
->value
< c
->v
.minimum
)
926 ctl
->value
= c
->v
.minimum
;
927 if (ctl
->value
> c
->v
.maximum
)
928 ctl
->value
= c
->v
.maximum
;
931 case V4L2_CID_AUDIO_BALANCE
:
932 value
= (ctl
->value
< 0x40) ? (0x7f - ctl
->value
) : (ctl
->value
- 0x40);
934 case V4L2_CID_AUDIO_VOLUME
:
935 value
= 0x3f - (ctl
->value
& 0x3f);
937 case V4L2_CID_SATURATION
:
938 /* special v_sat handling */
940 value
= ((ctl
->value
- c
->off
) << c
->shift
) & c
->mask
;
942 if (core
->tvnorm
& V4L2_STD_SECAM
) {
943 /* For SECAM, both U and V sat should be equal */
944 value
=value
<<8|value
;
946 /* Keeps U Saturation proportional to V Sat */
947 value
=(value
*0x5a)/0x7f<<8|value
;
952 value
= ((ctl
->value
- c
->off
) << c
->shift
) & c
->mask
;
955 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
956 ctl
->id
, c
->v
.name
, ctl
->value
, c
->reg
, value
,
957 mask
, c
->sreg
? " [shadowed]" : "");
959 cx_sandor(c
->sreg
, c
->reg
, mask
, value
);
961 cx_andor(c
->reg
, mask
, value
);
965 EXPORT_SYMBOL(cx88_set_control
);
967 static void init_controls(struct cx88_core
*core
)
969 struct v4l2_control ctrl
;
972 for (i
= 0; i
< CX8800_CTLS
; i
++) {
973 ctrl
.id
=cx8800_ctls
[i
].v
.id
;
974 ctrl
.value
=cx8800_ctls
[i
].v
.default_value
;
976 cx88_set_control(core
, &ctrl
);
980 /* ------------------------------------------------------------------ */
983 static int vidioc_g_fmt_cap (struct file
*file
, void *priv
,
984 struct v4l2_format
*f
)
986 struct cx8800_fh
*fh
= priv
;
988 f
->fmt
.pix
.width
= fh
->width
;
989 f
->fmt
.pix
.height
= fh
->height
;
990 f
->fmt
.pix
.field
= fh
->vidq
.field
;
991 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
992 f
->fmt
.pix
.bytesperline
=
993 (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
994 f
->fmt
.pix
.sizeimage
=
995 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
999 static int vidioc_try_fmt_cap (struct file
*file
, void *priv
,
1000 struct v4l2_format
*f
)
1002 struct cx88_core
*core
= ((struct cx8800_fh
*)priv
)->dev
->core
;
1003 struct cx8800_fmt
*fmt
;
1004 enum v4l2_field field
;
1005 unsigned int maxw
, maxh
;
1007 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1011 field
= f
->fmt
.pix
.field
;
1012 maxw
= norm_maxw(core
->tvnorm
);
1013 maxh
= norm_maxh(core
->tvnorm
);
1015 if (V4L2_FIELD_ANY
== field
) {
1016 field
= (f
->fmt
.pix
.height
> maxh
/2)
1017 ? V4L2_FIELD_INTERLACED
1018 : V4L2_FIELD_BOTTOM
;
1022 case V4L2_FIELD_TOP
:
1023 case V4L2_FIELD_BOTTOM
:
1026 case V4L2_FIELD_INTERLACED
:
1032 f
->fmt
.pix
.field
= field
;
1033 if (f
->fmt
.pix
.height
< 32)
1034 f
->fmt
.pix
.height
= 32;
1035 if (f
->fmt
.pix
.height
> maxh
)
1036 f
->fmt
.pix
.height
= maxh
;
1037 if (f
->fmt
.pix
.width
< 48)
1038 f
->fmt
.pix
.width
= 48;
1039 if (f
->fmt
.pix
.width
> maxw
)
1040 f
->fmt
.pix
.width
= maxw
;
1041 f
->fmt
.pix
.width
&= ~0x03;
1042 f
->fmt
.pix
.bytesperline
=
1043 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
1044 f
->fmt
.pix
.sizeimage
=
1045 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1050 static int vidioc_s_fmt_cap (struct file
*file
, void *priv
,
1051 struct v4l2_format
*f
)
1053 struct cx8800_fh
*fh
= priv
;
1054 int err
= vidioc_try_fmt_cap (file
,priv
,f
);
1058 fh
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1059 fh
->width
= f
->fmt
.pix
.width
;
1060 fh
->height
= f
->fmt
.pix
.height
;
1061 fh
->vidq
.field
= f
->fmt
.pix
.field
;
1065 static int vidioc_querycap (struct file
*file
, void *priv
,
1066 struct v4l2_capability
*cap
)
1068 struct cx8800_dev
*dev
= ((struct cx8800_fh
*)priv
)->dev
;
1069 struct cx88_core
*core
= dev
->core
;
1071 strcpy(cap
->driver
, "cx8800");
1072 strlcpy(cap
->card
, core
->board
.name
, sizeof(cap
->card
));
1073 sprintf(cap
->bus_info
,"PCI:%s",pci_name(dev
->pci
));
1074 cap
->version
= CX88_VERSION_CODE
;
1076 V4L2_CAP_VIDEO_CAPTURE
|
1077 V4L2_CAP_READWRITE
|
1078 V4L2_CAP_STREAMING
|
1079 V4L2_CAP_VBI_CAPTURE
;
1080 if (UNSET
!= core
->board
.tuner_type
)
1081 cap
->capabilities
|= V4L2_CAP_TUNER
;
1085 static int vidioc_enum_fmt_cap (struct file
*file
, void *priv
,
1086 struct v4l2_fmtdesc
*f
)
1088 if (unlikely(f
->index
>= ARRAY_SIZE(formats
)))
1091 strlcpy(f
->description
,formats
[f
->index
].name
,sizeof(f
->description
));
1092 f
->pixelformat
= formats
[f
->index
].fourcc
;
1097 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1098 static int vidiocgmbuf (struct file
*file
, void *priv
, struct video_mbuf
*mbuf
)
1100 struct cx8800_fh
*fh
= priv
;
1102 return videobuf_cgmbuf (get_queue(fh
), mbuf
, 8);
1106 static int vidioc_reqbufs (struct file
*file
, void *priv
, struct v4l2_requestbuffers
*p
)
1108 struct cx8800_fh
*fh
= priv
;
1109 return (videobuf_reqbufs(get_queue(fh
), p
));
1112 static int vidioc_querybuf (struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1114 struct cx8800_fh
*fh
= priv
;
1115 return (videobuf_querybuf(get_queue(fh
), p
));
1118 static int vidioc_qbuf (struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1120 struct cx8800_fh
*fh
= priv
;
1121 return (videobuf_qbuf(get_queue(fh
), p
));
1124 static int vidioc_dqbuf (struct file
*file
, void *priv
, struct v4l2_buffer
*p
)
1126 struct cx8800_fh
*fh
= priv
;
1127 return (videobuf_dqbuf(get_queue(fh
), p
,
1128 file
->f_flags
& O_NONBLOCK
));
1131 static int vidioc_streamon(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1133 struct cx8800_fh
*fh
= priv
;
1134 struct cx8800_dev
*dev
= fh
->dev
;
1136 if (unlikely(fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
))
1138 if (unlikely(i
!= fh
->type
))
1141 if (unlikely(!res_get(dev
,fh
,get_ressource(fh
))))
1143 return videobuf_streamon(get_queue(fh
));
1146 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1148 struct cx8800_fh
*fh
= priv
;
1149 struct cx8800_dev
*dev
= fh
->dev
;
1152 if (fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1157 res
= get_ressource(fh
);
1158 err
= videobuf_streamoff(get_queue(fh
));
1161 res_free(dev
,fh
,res
);
1165 static int vidioc_s_std (struct file
*file
, void *priv
, v4l2_std_id
*tvnorms
)
1167 struct cx88_core
*core
= ((struct cx8800_fh
*)priv
)->dev
->core
;
1169 mutex_lock(&core
->lock
);
1170 cx88_set_tvnorm(core
,*tvnorms
);
1171 mutex_unlock(&core
->lock
);
1176 /* only one input in this sample driver */
1177 int cx88_enum_input (struct cx88_core
*core
,struct v4l2_input
*i
)
1179 static const char *iname
[] = {
1180 [ CX88_VMUX_COMPOSITE1
] = "Composite1",
1181 [ CX88_VMUX_COMPOSITE2
] = "Composite2",
1182 [ CX88_VMUX_COMPOSITE3
] = "Composite3",
1183 [ CX88_VMUX_COMPOSITE4
] = "Composite4",
1184 [ CX88_VMUX_SVIDEO
] = "S-Video",
1185 [ CX88_VMUX_TELEVISION
] = "Television",
1186 [ CX88_VMUX_CABLE
] = "Cable TV",
1187 [ CX88_VMUX_DVB
] = "DVB",
1188 [ CX88_VMUX_DEBUG
] = "for debug only",
1195 if (0 == INPUT(n
).type
)
1197 memset(i
,0,sizeof(*i
));
1199 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1200 strcpy(i
->name
,iname
[INPUT(n
).type
]);
1201 if ((CX88_VMUX_TELEVISION
== INPUT(n
).type
) ||
1202 (CX88_VMUX_CABLE
== INPUT(n
).type
))
1203 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1204 i
->std
= CX88_NORMS
;
1207 EXPORT_SYMBOL(cx88_enum_input
);
1209 static int vidioc_enum_input (struct file
*file
, void *priv
,
1210 struct v4l2_input
*i
)
1212 struct cx88_core
*core
= ((struct cx8800_fh
*)priv
)->dev
->core
;
1213 return cx88_enum_input (core
,i
);
1216 static int vidioc_g_input (struct file
*file
, void *priv
, unsigned int *i
)
1218 struct cx88_core
*core
= ((struct cx8800_fh
*)priv
)->dev
->core
;
1224 static int vidioc_s_input (struct file
*file
, void *priv
, unsigned int i
)
1226 struct cx88_core
*core
= ((struct cx8800_fh
*)priv
)->dev
->core
;
1231 mutex_lock(&core
->lock
);
1232 cx88_newstation(core
);
1233 cx88_video_mux(core
,i
);
1234 mutex_unlock(&core
->lock
);
1240 static int vidioc_queryctrl (struct file
*file
, void *priv
,
1241 struct v4l2_queryctrl
*qctrl
)
1243 qctrl
->id
= v4l2_ctrl_next(ctrl_classes
, qctrl
->id
);
1244 if (unlikely(qctrl
->id
== 0))
1246 return cx8800_ctrl_query(qctrl
);
1249 static int vidioc_g_ctrl (struct file
*file
, void *priv
,
1250 struct v4l2_control
*ctl
)
1252 struct cx88_core
*core
= ((struct cx8800_fh
*)priv
)->dev
->core
;
1254 cx88_get_control(core
,ctl
);
1257 static int vidioc_s_ctrl (struct file
*file
, void *priv
,
1258 struct v4l2_control
*ctl
)
1260 struct cx88_core
*core
= ((struct cx8800_fh
*)priv
)->dev
->core
;
1262 cx88_set_control(core
,ctl
);
1265 static int vidioc_g_tuner (struct file
*file
, void *priv
,
1266 struct v4l2_tuner
*t
)
1268 struct cx88_core
*core
= ((struct cx8800_fh
*)priv
)->dev
->core
;
1271 if (unlikely(UNSET
== core
->board
.tuner_type
))
1276 strcpy(t
->name
, "Television");
1277 t
->type
= V4L2_TUNER_ANALOG_TV
;
1278 t
->capability
= V4L2_TUNER_CAP_NORM
;
1279 t
->rangehigh
= 0xffffffffUL
;
1281 cx88_get_stereo(core
,t
);
1282 reg
= cx_read(MO_DEVICE_STATUS
);
1283 t
->signal
= (reg
& (1<<5)) ? 0xffff : 0x0000;
1287 static int vidioc_s_tuner (struct file
*file
, void *priv
,
1288 struct v4l2_tuner
*t
)
1290 struct cx88_core
*core
= ((struct cx8800_fh
*)priv
)->dev
->core
;
1292 if (UNSET
== core
->board
.tuner_type
)
1297 cx88_set_stereo(core
, t
->audmode
, 1);
1301 static int vidioc_g_frequency (struct file
*file
, void *priv
,
1302 struct v4l2_frequency
*f
)
1304 struct cx8800_fh
*fh
= priv
;
1305 struct cx88_core
*core
= fh
->dev
->core
;
1307 if (unlikely(UNSET
== core
->board
.tuner_type
))
1310 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1311 f
->type
= fh
->radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1312 f
->frequency
= core
->freq
;
1314 cx88_call_i2c_clients(core
,VIDIOC_G_FREQUENCY
,f
);
1319 int cx88_set_freq (struct cx88_core
*core
,
1320 struct v4l2_frequency
*f
)
1322 if (unlikely(UNSET
== core
->board
.tuner_type
))
1324 if (unlikely(f
->tuner
!= 0))
1327 mutex_lock(&core
->lock
);
1328 core
->freq
= f
->frequency
;
1329 cx88_newstation(core
);
1330 cx88_call_i2c_clients(core
,VIDIOC_S_FREQUENCY
,f
);
1332 /* When changing channels it is required to reset TVAUDIO */
1334 cx88_set_tvaudio(core
);
1336 mutex_unlock(&core
->lock
);
1340 EXPORT_SYMBOL(cx88_set_freq
);
1342 static int vidioc_s_frequency (struct file
*file
, void *priv
,
1343 struct v4l2_frequency
*f
)
1345 struct cx8800_fh
*fh
= priv
;
1346 struct cx88_core
*core
= fh
->dev
->core
;
1348 if (unlikely(0 == fh
->radio
&& f
->type
!= V4L2_TUNER_ANALOG_TV
))
1350 if (unlikely(1 == fh
->radio
&& f
->type
!= V4L2_TUNER_RADIO
))
1354 cx88_set_freq (core
,f
);
1357 #ifdef CONFIG_VIDEO_ADV_DEBUG
1358 static int vidioc_g_register (struct file
*file
, void *fh
,
1359 struct v4l2_register
*reg
)
1361 struct cx88_core
*core
= ((struct cx8800_fh
*)fh
)->dev
->core
;
1363 if (!v4l2_chip_match_host(reg
->match_type
, reg
->match_chip
))
1365 /* cx2388x has a 24-bit register space */
1366 reg
->val
= cx_read(reg
->reg
&0xffffff);
1370 static int vidioc_s_register (struct file
*file
, void *fh
,
1371 struct v4l2_register
*reg
)
1373 struct cx88_core
*core
= ((struct cx8800_fh
*)fh
)->dev
->core
;
1375 if (!v4l2_chip_match_host(reg
->match_type
, reg
->match_chip
))
1377 cx_write(reg
->reg
&0xffffff, reg
->val
);
1382 /* ----------------------------------------------------------- */
1383 /* RADIO ESPECIFIC IOCTLS */
1384 /* ----------------------------------------------------------- */
1386 static int radio_querycap (struct file
*file
, void *priv
,
1387 struct v4l2_capability
*cap
)
1389 struct cx8800_dev
*dev
= ((struct cx8800_fh
*)priv
)->dev
;
1390 struct cx88_core
*core
= dev
->core
;
1392 strcpy(cap
->driver
, "cx8800");
1393 strlcpy(cap
->card
, core
->board
.name
, sizeof(cap
->card
));
1394 sprintf(cap
->bus_info
,"PCI:%s", pci_name(dev
->pci
));
1395 cap
->version
= CX88_VERSION_CODE
;
1396 cap
->capabilities
= V4L2_CAP_TUNER
;
1400 static int radio_g_tuner (struct file
*file
, void *priv
,
1401 struct v4l2_tuner
*t
)
1403 struct cx88_core
*core
= ((struct cx8800_fh
*)priv
)->dev
->core
;
1405 if (unlikely(t
->index
> 0))
1408 strcpy(t
->name
, "Radio");
1409 t
->type
= V4L2_TUNER_RADIO
;
1411 cx88_call_i2c_clients(core
,VIDIOC_G_TUNER
,t
);
1415 static int radio_enum_input (struct file
*file
, void *priv
,
1416 struct v4l2_input
*i
)
1420 strcpy(i
->name
,"Radio");
1421 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1426 static int radio_g_audio (struct file
*file
, void *priv
, struct v4l2_audio
*a
)
1428 if (unlikely(a
->index
))
1431 memset(a
,0,sizeof(*a
));
1432 strcpy(a
->name
,"Radio");
1436 /* FIXME: Should add a standard for radio */
1438 static int radio_s_tuner (struct file
*file
, void *priv
,
1439 struct v4l2_tuner
*t
)
1441 struct cx88_core
*core
= ((struct cx8800_fh
*)priv
)->dev
->core
;
1446 cx88_call_i2c_clients(core
,VIDIOC_S_TUNER
,t
);
1451 static int radio_s_audio (struct file
*file
, void *fh
,
1452 struct v4l2_audio
*a
)
1457 static int radio_s_input (struct file
*file
, void *fh
, unsigned int i
)
1462 static int radio_queryctrl (struct file
*file
, void *priv
,
1463 struct v4l2_queryctrl
*c
)
1467 if (c
->id
< V4L2_CID_BASE
||
1468 c
->id
>= V4L2_CID_LASTP1
)
1470 if (c
->id
== V4L2_CID_AUDIO_MUTE
) {
1471 for (i
= 0; i
< CX8800_CTLS
; i
++)
1472 if (cx8800_ctls
[i
].v
.id
== c
->id
)
1474 *c
= cx8800_ctls
[i
].v
;
1480 /* ----------------------------------------------------------- */
1482 static void cx8800_vid_timeout(unsigned long data
)
1484 struct cx8800_dev
*dev
= (struct cx8800_dev
*)data
;
1485 struct cx88_core
*core
= dev
->core
;
1486 struct cx88_dmaqueue
*q
= &dev
->vidq
;
1487 struct cx88_buffer
*buf
;
1488 unsigned long flags
;
1490 cx88_sram_channel_dump(core
, &cx88_sram_channels
[SRAM_CH21
]);
1492 cx_clear(MO_VID_DMACNTRL
, 0x11);
1493 cx_clear(VID_CAPTURE_CONTROL
, 0x06);
1495 spin_lock_irqsave(&dev
->slock
,flags
);
1496 while (!list_empty(&q
->active
)) {
1497 buf
= list_entry(q
->active
.next
, struct cx88_buffer
, vb
.queue
);
1498 list_del(&buf
->vb
.queue
);
1499 buf
->vb
.state
= VIDEOBUF_ERROR
;
1500 wake_up(&buf
->vb
.done
);
1501 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core
->name
,
1502 buf
, buf
->vb
.i
, (unsigned long)buf
->risc
.dma
);
1504 restart_video_queue(dev
,q
);
1505 spin_unlock_irqrestore(&dev
->slock
,flags
);
1508 static char *cx88_vid_irqs
[32] = {
1509 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1510 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1511 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1512 "y_sync", "u_sync", "v_sync", "vbi_sync",
1513 "opc_err", "par_err", "rip_err", "pci_abort",
1516 static void cx8800_vid_irq(struct cx8800_dev
*dev
)
1518 struct cx88_core
*core
= dev
->core
;
1519 u32 status
, mask
, count
;
1521 status
= cx_read(MO_VID_INTSTAT
);
1522 mask
= cx_read(MO_VID_INTMSK
);
1523 if (0 == (status
& mask
))
1525 cx_write(MO_VID_INTSTAT
, status
);
1526 if (irq_debug
|| (status
& mask
& ~0xff))
1527 cx88_print_irqbits(core
->name
, "irq vid",
1528 cx88_vid_irqs
, ARRAY_SIZE(cx88_vid_irqs
),
1531 /* risc op code error */
1532 if (status
& (1 << 16)) {
1533 printk(KERN_WARNING
"%s/0: video risc op code error\n",core
->name
);
1534 cx_clear(MO_VID_DMACNTRL
, 0x11);
1535 cx_clear(VID_CAPTURE_CONTROL
, 0x06);
1536 cx88_sram_channel_dump(core
, &cx88_sram_channels
[SRAM_CH21
]);
1540 if (status
& 0x01) {
1541 spin_lock(&dev
->slock
);
1542 count
= cx_read(MO_VIDY_GPCNT
);
1543 cx88_wakeup(core
, &dev
->vidq
, count
);
1544 spin_unlock(&dev
->slock
);
1548 if (status
& 0x08) {
1549 spin_lock(&dev
->slock
);
1550 count
= cx_read(MO_VBI_GPCNT
);
1551 cx88_wakeup(core
, &dev
->vbiq
, count
);
1552 spin_unlock(&dev
->slock
);
1556 if (status
& 0x10) {
1557 dprintk(2,"stopper video\n");
1558 spin_lock(&dev
->slock
);
1559 restart_video_queue(dev
,&dev
->vidq
);
1560 spin_unlock(&dev
->slock
);
1564 if (status
& 0x80) {
1565 dprintk(2,"stopper vbi\n");
1566 spin_lock(&dev
->slock
);
1567 cx8800_restart_vbi_queue(dev
,&dev
->vbiq
);
1568 spin_unlock(&dev
->slock
);
1572 static irqreturn_t
cx8800_irq(int irq
, void *dev_id
)
1574 struct cx8800_dev
*dev
= dev_id
;
1575 struct cx88_core
*core
= dev
->core
;
1577 int loop
, handled
= 0;
1579 for (loop
= 0; loop
< 10; loop
++) {
1580 status
= cx_read(MO_PCI_INTSTAT
) &
1581 (core
->pci_irqmask
| PCI_INT_VIDINT
);
1584 cx_write(MO_PCI_INTSTAT
, status
);
1587 if (status
& core
->pci_irqmask
)
1588 cx88_core_irq(core
,status
);
1589 if (status
& PCI_INT_VIDINT
)
1590 cx8800_vid_irq(dev
);
1593 printk(KERN_WARNING
"%s/0: irq loop -- clearing mask\n",
1595 cx_write(MO_PCI_INTMSK
,0);
1599 return IRQ_RETVAL(handled
);
1602 /* ----------------------------------------------------------- */
1603 /* exported stuff */
1605 static const struct file_operations video_fops
=
1607 .owner
= THIS_MODULE
,
1609 .release
= video_release
,
1613 .ioctl
= video_ioctl2
,
1614 .compat_ioctl
= v4l_compat_ioctl32
,
1615 .llseek
= no_llseek
,
1618 static struct video_device cx8800_vbi_template
;
1619 static struct video_device cx8800_video_template
=
1621 .name
= "cx8800-video",
1622 .type
= VID_TYPE_CAPTURE
|VID_TYPE_TUNER
|VID_TYPE_SCALES
,
1623 .fops
= &video_fops
,
1625 .vidioc_querycap
= vidioc_querycap
,
1626 .vidioc_enum_fmt_cap
= vidioc_enum_fmt_cap
,
1627 .vidioc_g_fmt_cap
= vidioc_g_fmt_cap
,
1628 .vidioc_try_fmt_cap
= vidioc_try_fmt_cap
,
1629 .vidioc_s_fmt_cap
= vidioc_s_fmt_cap
,
1630 .vidioc_g_fmt_vbi
= cx8800_vbi_fmt
,
1631 .vidioc_try_fmt_vbi
= cx8800_vbi_fmt
,
1632 .vidioc_s_fmt_vbi
= cx8800_vbi_fmt
,
1633 .vidioc_reqbufs
= vidioc_reqbufs
,
1634 .vidioc_querybuf
= vidioc_querybuf
,
1635 .vidioc_qbuf
= vidioc_qbuf
,
1636 .vidioc_dqbuf
= vidioc_dqbuf
,
1637 .vidioc_s_std
= vidioc_s_std
,
1638 .vidioc_enum_input
= vidioc_enum_input
,
1639 .vidioc_g_input
= vidioc_g_input
,
1640 .vidioc_s_input
= vidioc_s_input
,
1641 .vidioc_queryctrl
= vidioc_queryctrl
,
1642 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1643 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1644 .vidioc_streamon
= vidioc_streamon
,
1645 .vidioc_streamoff
= vidioc_streamoff
,
1646 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1647 .vidiocgmbuf
= vidiocgmbuf
,
1649 .vidioc_g_tuner
= vidioc_g_tuner
,
1650 .vidioc_s_tuner
= vidioc_s_tuner
,
1651 .vidioc_g_frequency
= vidioc_g_frequency
,
1652 .vidioc_s_frequency
= vidioc_s_frequency
,
1653 #ifdef CONFIG_VIDEO_ADV_DEBUG
1654 .vidioc_g_register
= vidioc_g_register
,
1655 .vidioc_s_register
= vidioc_s_register
,
1657 .tvnorms
= CX88_NORMS
,
1658 .current_norm
= V4L2_STD_NTSC_M
,
1661 static const struct file_operations radio_fops
=
1663 .owner
= THIS_MODULE
,
1665 .release
= video_release
,
1666 .ioctl
= video_ioctl2
,
1667 .compat_ioctl
= v4l_compat_ioctl32
,
1668 .llseek
= no_llseek
,
1671 static struct video_device cx8800_radio_template
=
1673 .name
= "cx8800-radio",
1674 .type
= VID_TYPE_TUNER
,
1675 .fops
= &radio_fops
,
1677 .vidioc_querycap
= radio_querycap
,
1678 .vidioc_g_tuner
= radio_g_tuner
,
1679 .vidioc_enum_input
= radio_enum_input
,
1680 .vidioc_g_audio
= radio_g_audio
,
1681 .vidioc_s_tuner
= radio_s_tuner
,
1682 .vidioc_s_audio
= radio_s_audio
,
1683 .vidioc_s_input
= radio_s_input
,
1684 .vidioc_queryctrl
= radio_queryctrl
,
1685 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1686 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1687 .vidioc_g_frequency
= vidioc_g_frequency
,
1688 .vidioc_s_frequency
= vidioc_s_frequency
,
1689 #ifdef CONFIG_VIDEO_ADV_DEBUG
1690 .vidioc_g_register
= vidioc_g_register
,
1691 .vidioc_s_register
= vidioc_s_register
,
1695 /* ----------------------------------------------------------- */
1697 static void cx8800_unregister_video(struct cx8800_dev
*dev
)
1699 if (dev
->radio_dev
) {
1700 if (-1 != dev
->radio_dev
->minor
)
1701 video_unregister_device(dev
->radio_dev
);
1703 video_device_release(dev
->radio_dev
);
1704 dev
->radio_dev
= NULL
;
1707 if (-1 != dev
->vbi_dev
->minor
)
1708 video_unregister_device(dev
->vbi_dev
);
1710 video_device_release(dev
->vbi_dev
);
1711 dev
->vbi_dev
= NULL
;
1713 if (dev
->video_dev
) {
1714 if (-1 != dev
->video_dev
->minor
)
1715 video_unregister_device(dev
->video_dev
);
1717 video_device_release(dev
->video_dev
);
1718 dev
->video_dev
= NULL
;
1722 static int __devinit
cx8800_initdev(struct pci_dev
*pci_dev
,
1723 const struct pci_device_id
*pci_id
)
1725 struct cx8800_dev
*dev
;
1726 struct cx88_core
*core
;
1730 dev
= kzalloc(sizeof(*dev
),GFP_KERNEL
);
1736 if (pci_enable_device(pci_dev
)) {
1740 core
= cx88_core_get(dev
->pci
);
1747 /* print pci info */
1748 pci_read_config_byte(pci_dev
, PCI_CLASS_REVISION
, &dev
->pci_rev
);
1749 pci_read_config_byte(pci_dev
, PCI_LATENCY_TIMER
, &dev
->pci_lat
);
1750 printk(KERN_INFO
"%s/0: found at %s, rev: %d, irq: %d, "
1751 "latency: %d, mmio: 0x%llx\n", core
->name
,
1752 pci_name(pci_dev
), dev
->pci_rev
, pci_dev
->irq
,
1753 dev
->pci_lat
,(unsigned long long)pci_resource_start(pci_dev
,0));
1755 pci_set_master(pci_dev
);
1756 if (!pci_dma_supported(pci_dev
,DMA_32BIT_MASK
)) {
1757 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core
->name
);
1762 /* Initialize VBI template */
1763 memcpy( &cx8800_vbi_template
, &cx8800_video_template
,
1764 sizeof(cx8800_vbi_template
) );
1765 strcpy(cx8800_vbi_template
.name
,"cx8800-vbi");
1766 cx8800_vbi_template
.type
= VID_TYPE_TELETEXT
|VID_TYPE_TUNER
;
1768 /* initialize driver struct */
1769 spin_lock_init(&dev
->slock
);
1770 core
->tvnorm
= cx8800_video_template
.current_norm
;
1772 /* init video dma queues */
1773 INIT_LIST_HEAD(&dev
->vidq
.active
);
1774 INIT_LIST_HEAD(&dev
->vidq
.queued
);
1775 dev
->vidq
.timeout
.function
= cx8800_vid_timeout
;
1776 dev
->vidq
.timeout
.data
= (unsigned long)dev
;
1777 init_timer(&dev
->vidq
.timeout
);
1778 cx88_risc_stopper(dev
->pci
,&dev
->vidq
.stopper
,
1779 MO_VID_DMACNTRL
,0x11,0x00);
1781 /* init vbi dma queues */
1782 INIT_LIST_HEAD(&dev
->vbiq
.active
);
1783 INIT_LIST_HEAD(&dev
->vbiq
.queued
);
1784 dev
->vbiq
.timeout
.function
= cx8800_vbi_timeout
;
1785 dev
->vbiq
.timeout
.data
= (unsigned long)dev
;
1786 init_timer(&dev
->vbiq
.timeout
);
1787 cx88_risc_stopper(dev
->pci
,&dev
->vbiq
.stopper
,
1788 MO_VID_DMACNTRL
,0x88,0x00);
1791 err
= request_irq(pci_dev
->irq
, cx8800_irq
,
1792 IRQF_SHARED
| IRQF_DISABLED
, core
->name
, dev
);
1794 printk(KERN_ERR
"%s/0: can't get IRQ %d\n",
1795 core
->name
,pci_dev
->irq
);
1798 cx_set(MO_PCI_INTMSK
, core
->pci_irqmask
);
1800 /* load and configure helper modules */
1801 if (TUNER_ABSENT
!= core
->board
.tuner_type
)
1802 request_module("tuner");
1804 if (core
->board
.audio_chip
== AUDIO_CHIP_WM8775
)
1805 request_module("wm8775");
1807 switch (core
->boardnr
) {
1808 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD
:
1809 request_module("ir-kbd-i2c");
1810 request_module("rtc-isl1208");
1813 /* register v4l devices */
1814 dev
->video_dev
= cx88_vdev_init(core
,dev
->pci
,
1815 &cx8800_video_template
,"video");
1816 err
= video_register_device(dev
->video_dev
,VFL_TYPE_GRABBER
,
1817 video_nr
[core
->nr
]);
1819 printk(KERN_ERR
"%s/0: can't register video device\n",
1823 printk(KERN_INFO
"%s/0: registered device video%d [v4l2]\n",
1824 core
->name
,dev
->video_dev
->minor
& 0x1f);
1826 dev
->vbi_dev
= cx88_vdev_init(core
,dev
->pci
,&cx8800_vbi_template
,"vbi");
1827 err
= video_register_device(dev
->vbi_dev
,VFL_TYPE_VBI
,
1830 printk(KERN_ERR
"%s/0: can't register vbi device\n",
1834 printk(KERN_INFO
"%s/0: registered device vbi%d\n",
1835 core
->name
,dev
->vbi_dev
->minor
& 0x1f);
1837 if (core
->board
.radio
.type
== CX88_RADIO
) {
1838 dev
->radio_dev
= cx88_vdev_init(core
,dev
->pci
,
1839 &cx8800_radio_template
,"radio");
1840 err
= video_register_device(dev
->radio_dev
,VFL_TYPE_RADIO
,
1841 radio_nr
[core
->nr
]);
1843 printk(KERN_ERR
"%s/0: can't register radio device\n",
1847 printk(KERN_INFO
"%s/0: registered device radio%d\n",
1848 core
->name
,dev
->radio_dev
->minor
& 0x1f);
1851 /* everything worked */
1852 list_add_tail(&dev
->devlist
,&cx8800_devlist
);
1853 pci_set_drvdata(pci_dev
,dev
);
1855 /* initial device configuration */
1856 mutex_lock(&core
->lock
);
1857 cx88_set_tvnorm(core
,core
->tvnorm
);
1858 init_controls(core
);
1859 cx88_video_mux(core
,0);
1860 mutex_unlock(&core
->lock
);
1862 /* start tvaudio thread */
1863 if (core
->board
.tuner_type
!= TUNER_ABSENT
) {
1864 core
->kthread
= kthread_run(cx88_audio_thread
, core
, "cx88 tvaudio");
1865 if (IS_ERR(core
->kthread
)) {
1866 err
= PTR_ERR(core
->kthread
);
1867 printk(KERN_ERR
"%s/0: failed to create cx88 audio thread, err=%d\n",
1874 cx8800_unregister_video(dev
);
1875 free_irq(pci_dev
->irq
, dev
);
1877 cx88_core_put(core
,dev
->pci
);
1883 static void __devexit
cx8800_finidev(struct pci_dev
*pci_dev
)
1885 struct cx8800_dev
*dev
= pci_get_drvdata(pci_dev
);
1886 struct cx88_core
*core
= dev
->core
;
1889 if (core
->kthread
) {
1890 kthread_stop(core
->kthread
);
1891 core
->kthread
= NULL
;
1894 cx88_shutdown(core
); /* FIXME */
1895 pci_disable_device(pci_dev
);
1897 /* unregister stuff */
1899 free_irq(pci_dev
->irq
, dev
);
1900 cx8800_unregister_video(dev
);
1901 pci_set_drvdata(pci_dev
, NULL
);
1904 btcx_riscmem_free(dev
->pci
,&dev
->vidq
.stopper
);
1905 list_del(&dev
->devlist
);
1906 cx88_core_put(core
,dev
->pci
);
1911 static int cx8800_suspend(struct pci_dev
*pci_dev
, pm_message_t state
)
1913 struct cx8800_dev
*dev
= pci_get_drvdata(pci_dev
);
1914 struct cx88_core
*core
= dev
->core
;
1916 /* stop video+vbi capture */
1917 spin_lock(&dev
->slock
);
1918 if (!list_empty(&dev
->vidq
.active
)) {
1919 printk("%s/0: suspend video\n", core
->name
);
1920 stop_video_dma(dev
);
1921 del_timer(&dev
->vidq
.timeout
);
1923 if (!list_empty(&dev
->vbiq
.active
)) {
1924 printk("%s/0: suspend vbi\n", core
->name
);
1925 cx8800_stop_vbi_dma(dev
);
1926 del_timer(&dev
->vbiq
.timeout
);
1928 spin_unlock(&dev
->slock
);
1931 cx88_ir_stop(core
, core
->ir
);
1932 /* FIXME -- shutdown device */
1933 cx88_shutdown(core
);
1935 pci_save_state(pci_dev
);
1936 if (0 != pci_set_power_state(pci_dev
, pci_choose_state(pci_dev
, state
))) {
1937 pci_disable_device(pci_dev
);
1938 dev
->state
.disabled
= 1;
1943 static int cx8800_resume(struct pci_dev
*pci_dev
)
1945 struct cx8800_dev
*dev
= pci_get_drvdata(pci_dev
);
1946 struct cx88_core
*core
= dev
->core
;
1949 if (dev
->state
.disabled
) {
1950 err
=pci_enable_device(pci_dev
);
1952 printk(KERN_ERR
"%s/0: can't enable device\n",
1957 dev
->state
.disabled
= 0;
1959 err
= pci_set_power_state(pci_dev
, PCI_D0
);
1961 printk(KERN_ERR
"%s/0: can't set power state\n", core
->name
);
1962 pci_disable_device(pci_dev
);
1963 dev
->state
.disabled
= 1;
1967 pci_restore_state(pci_dev
);
1969 /* FIXME: re-initialize hardware */
1972 cx88_ir_start(core
, core
->ir
);
1974 cx_set(MO_PCI_INTMSK
, core
->pci_irqmask
);
1976 /* restart video+vbi capture */
1977 spin_lock(&dev
->slock
);
1978 if (!list_empty(&dev
->vidq
.active
)) {
1979 printk("%s/0: resume video\n", core
->name
);
1980 restart_video_queue(dev
,&dev
->vidq
);
1982 if (!list_empty(&dev
->vbiq
.active
)) {
1983 printk("%s/0: resume vbi\n", core
->name
);
1984 cx8800_restart_vbi_queue(dev
,&dev
->vbiq
);
1986 spin_unlock(&dev
->slock
);
1992 /* ----------------------------------------------------------- */
1994 static struct pci_device_id cx8800_pci_tbl
[] = {
1998 .subvendor
= PCI_ANY_ID
,
1999 .subdevice
= PCI_ANY_ID
,
2001 /* --- end of list --- */
2004 MODULE_DEVICE_TABLE(pci
, cx8800_pci_tbl
);
2006 static struct pci_driver cx8800_pci_driver
= {
2008 .id_table
= cx8800_pci_tbl
,
2009 .probe
= cx8800_initdev
,
2010 .remove
= __devexit_p(cx8800_finidev
),
2012 .suspend
= cx8800_suspend
,
2013 .resume
= cx8800_resume
,
2017 static int cx8800_init(void)
2019 printk(KERN_INFO
"cx88/0: cx2388x v4l2 driver version %d.%d.%d loaded\n",
2020 (CX88_VERSION_CODE
>> 16) & 0xff,
2021 (CX88_VERSION_CODE
>> 8) & 0xff,
2022 CX88_VERSION_CODE
& 0xff);
2024 printk(KERN_INFO
"cx2388x: snapshot date %04d-%02d-%02d\n",
2025 SNAPSHOT
/10000, (SNAPSHOT
/100)%100, SNAPSHOT
%100);
2027 return pci_register_driver(&cx8800_pci_driver
);
2030 static void cx8800_fini(void)
2032 pci_unregister_driver(&cx8800_pci_driver
);
2035 module_init(cx8800_init
);
2036 module_exit(cx8800_fini
);
2038 /* ----------------------------------------------------------- */
2043 * 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