2 * Driver for the Conexant CX23885 PCIe bridge
4 * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/kmod.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <linux/delay.h>
31 #include <linux/kthread.h>
32 #include <asm/div64.h>
35 #include <media/v4l2-common.h>
36 #include <media/v4l2-ioctl.h>
37 #include "cx23885-ioctl.h"
38 #include "tuner-xc2028.h"
40 #include <media/cx25840.h>
42 MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
43 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
44 MODULE_LICENSE("GPL");
46 /* ------------------------------------------------------------------ */
48 static unsigned int video_nr
[] = {[0 ... (CX23885_MAXBOARDS
- 1)] = UNSET
};
49 static unsigned int vbi_nr
[] = {[0 ... (CX23885_MAXBOARDS
- 1)] = UNSET
};
50 static unsigned int radio_nr
[] = {[0 ... (CX23885_MAXBOARDS
- 1)] = UNSET
};
52 module_param_array(video_nr
, int, NULL
, 0444);
53 module_param_array(vbi_nr
, int, NULL
, 0444);
54 module_param_array(radio_nr
, int, NULL
, 0444);
56 MODULE_PARM_DESC(video_nr
, "video device numbers");
57 MODULE_PARM_DESC(vbi_nr
, "vbi device numbers");
58 MODULE_PARM_DESC(radio_nr
, "radio device numbers");
60 static unsigned int video_debug
;
61 module_param(video_debug
, int, 0644);
62 MODULE_PARM_DESC(video_debug
, "enable debug messages [video]");
64 static unsigned int irq_debug
;
65 module_param(irq_debug
, int, 0644);
66 MODULE_PARM_DESC(irq_debug
, "enable debug messages [IRQ handler]");
68 static unsigned int vid_limit
= 16;
69 module_param(vid_limit
, int, 0644);
70 MODULE_PARM_DESC(vid_limit
, "capture memory limit in megabytes");
72 #define dprintk(level, fmt, arg...)\
73 do { if (video_debug >= level)\
74 printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
77 /* ------------------------------------------------------------------- */
80 #define FORMAT_FLAGS_PACKED 0x01
82 static struct cx23885_fmt formats
[] = {
84 .name
= "8 bpp, gray",
85 .fourcc
= V4L2_PIX_FMT_GREY
,
87 .flags
= FORMAT_FLAGS_PACKED
,
89 .name
= "15 bpp RGB, le",
90 .fourcc
= V4L2_PIX_FMT_RGB555
,
92 .flags
= FORMAT_FLAGS_PACKED
,
94 .name
= "15 bpp RGB, be",
95 .fourcc
= V4L2_PIX_FMT_RGB555X
,
97 .flags
= FORMAT_FLAGS_PACKED
,
99 .name
= "16 bpp RGB, le",
100 .fourcc
= V4L2_PIX_FMT_RGB565
,
102 .flags
= FORMAT_FLAGS_PACKED
,
104 .name
= "16 bpp RGB, be",
105 .fourcc
= V4L2_PIX_FMT_RGB565X
,
107 .flags
= FORMAT_FLAGS_PACKED
,
109 .name
= "24 bpp RGB, le",
110 .fourcc
= V4L2_PIX_FMT_BGR24
,
112 .flags
= FORMAT_FLAGS_PACKED
,
114 .name
= "32 bpp RGB, le",
115 .fourcc
= V4L2_PIX_FMT_BGR32
,
117 .flags
= FORMAT_FLAGS_PACKED
,
119 .name
= "32 bpp RGB, be",
120 .fourcc
= V4L2_PIX_FMT_RGB32
,
122 .flags
= FORMAT_FLAGS_PACKED
,
124 .name
= "4:2:2, packed, YUYV",
125 .fourcc
= V4L2_PIX_FMT_YUYV
,
127 .flags
= FORMAT_FLAGS_PACKED
,
129 .name
= "4:2:2, packed, UYVY",
130 .fourcc
= V4L2_PIX_FMT_UYVY
,
132 .flags
= FORMAT_FLAGS_PACKED
,
136 static struct cx23885_fmt formats
[] = {
139 .name
= "4:2:2, packed, UYVY",
140 .fourcc
= V4L2_PIX_FMT_UYVY
,
142 .flags
= FORMAT_FLAGS_PACKED
,
145 .name
= "4:2:2, packed, YUYV",
146 .fourcc
= V4L2_PIX_FMT_YUYV
,
148 .flags
= FORMAT_FLAGS_PACKED
,
153 static struct cx23885_fmt
*format_by_fourcc(unsigned int fourcc
)
157 for (i
= 0; i
< ARRAY_SIZE(formats
); i
++)
158 if (formats
[i
].fourcc
== fourcc
)
161 printk(KERN_ERR
"%s(%c%c%c%c) NOT FOUND\n", __func__
,
163 ((fourcc
>> 8) & 0xff),
164 ((fourcc
>> 16) & 0xff),
165 ((fourcc
>> 24) & 0xff)
170 /* ------------------------------------------------------------------- */
172 static const struct v4l2_queryctrl no_ctl
= {
174 .flags
= V4L2_CTRL_FLAG_DISABLED
,
177 static struct cx23885_ctrl cx23885_ctls
[] = {
181 .id
= V4L2_CID_BRIGHTNESS
,
182 .name
= "Brightness",
186 .default_value
= 0x7f,
187 .type
= V4L2_CTRL_TYPE_INTEGER
,
195 .id
= V4L2_CID_CONTRAST
,
200 .default_value
= 0x3f,
201 .type
= V4L2_CTRL_TYPE_INTEGER
,
214 .default_value
= 0x0,
215 .type
= V4L2_CTRL_TYPE_INTEGER
,
222 /* strictly, this only describes only U saturation.
223 * V saturation is handled specially through code.
226 .id
= V4L2_CID_SATURATION
,
227 .name
= "Saturation",
231 .default_value
= 0x3f,
232 .type
= V4L2_CTRL_TYPE_INTEGER
,
241 .id
= V4L2_CID_AUDIO_MUTE
,
246 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
249 .mask
= (0x1f << 24),
253 .id
= V4L2_CID_AUDIO_VOLUME
,
258 .default_value
= 0x3f,
259 .type
= V4L2_CTRL_TYPE_INTEGER
,
261 .reg
= PATH1_VOL_CTL
,
266 static const int CX23885_CTLS
= ARRAY_SIZE(cx23885_ctls
);
268 /* Must be sorted from low to high control ID! */
269 static const u32 cx23885_user_ctrls
[] = {
275 V4L2_CID_AUDIO_VOLUME
,
280 static const u32
*ctrl_classes
[] = {
285 void cx23885_video_wakeup(struct cx23885_dev
*dev
,
286 struct cx23885_dmaqueue
*q
, u32 count
)
288 struct cx23885_buffer
*buf
;
291 for (bc
= 0;; bc
++) {
292 if (list_empty(&q
->active
))
294 buf
= list_entry(q
->active
.next
,
295 struct cx23885_buffer
, vb
.queue
);
297 /* count comes from the hw and is is 16bit wide --
298 * this trick handles wrap-arounds correctly for
299 * up to 32767 buffers in flight... */
300 if ((s16
) (count
- buf
->count
) < 0)
303 do_gettimeofday(&buf
->vb
.ts
);
304 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf
, buf
->vb
.i
,
306 buf
->vb
.state
= VIDEOBUF_DONE
;
307 list_del(&buf
->vb
.queue
);
308 wake_up(&buf
->vb
.done
);
310 if (list_empty(&q
->active
))
311 del_timer(&q
->timeout
);
313 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
315 printk(KERN_ERR
"%s: %d buffers handled (should be 1)\n",
319 static int cx23885_set_tvnorm(struct cx23885_dev
*dev
, v4l2_std_id norm
)
321 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
324 v4l2_norm_to_name(norm
));
328 call_all(dev
, core
, s_std
, norm
);
333 static struct video_device
*cx23885_vdev_init(struct cx23885_dev
*dev
,
335 struct video_device
*template,
338 struct video_device
*vfd
;
339 dprintk(1, "%s()\n", __func__
);
341 vfd
= video_device_alloc();
345 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
346 vfd
->release
= video_device_release
;
347 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s %s (%s)",
348 dev
->name
, type
, cx23885_boards
[dev
->board
].name
);
349 video_set_drvdata(vfd
, dev
);
353 static int cx23885_ctrl_query(struct v4l2_queryctrl
*qctrl
)
357 if (qctrl
->id
< V4L2_CID_BASE
||
358 qctrl
->id
>= V4L2_CID_LASTP1
)
360 for (i
= 0; i
< CX23885_CTLS
; i
++)
361 if (cx23885_ctls
[i
].v
.id
== qctrl
->id
)
363 if (i
== CX23885_CTLS
) {
367 *qctrl
= cx23885_ctls
[i
].v
;
371 /* ------------------------------------------------------------------- */
372 /* resource management */
374 static int res_get(struct cx23885_dev
*dev
, struct cx23885_fh
*fh
,
377 dprintk(1, "%s()\n", __func__
);
378 if (fh
->resources
& bit
)
379 /* have it already allocated */
383 mutex_lock(&dev
->lock
);
384 if (dev
->resources
& bit
) {
385 /* no, someone else uses it */
386 mutex_unlock(&dev
->lock
);
389 /* it's free, grab it */
390 fh
->resources
|= bit
;
391 dev
->resources
|= bit
;
392 dprintk(1, "res: get %d\n", bit
);
393 mutex_unlock(&dev
->lock
);
397 static int res_check(struct cx23885_fh
*fh
, unsigned int bit
)
399 return fh
->resources
& bit
;
402 static int res_locked(struct cx23885_dev
*dev
, unsigned int bit
)
404 return dev
->resources
& bit
;
407 static void res_free(struct cx23885_dev
*dev
, struct cx23885_fh
*fh
,
410 BUG_ON((fh
->resources
& bits
) != bits
);
411 dprintk(1, "%s()\n", __func__
);
413 mutex_lock(&dev
->lock
);
414 fh
->resources
&= ~bits
;
415 dev
->resources
&= ~bits
;
416 dprintk(1, "res: put %d\n", bits
);
417 mutex_unlock(&dev
->lock
);
420 static int cx23885_flatiron_write(struct cx23885_dev
*dev
, u8 reg
, u8 data
)
422 /* 8 bit registers, 8 bit values */
423 u8 buf
[] = { reg
, data
};
425 struct i2c_msg msg
= { .addr
= 0x98 >> 1,
426 .flags
= 0, .buf
= buf
, .len
= 2 };
428 return i2c_transfer(&dev
->i2c_bus
[2].i2c_adap
, &msg
, 1);
431 static u8
cx23885_flatiron_read(struct cx23885_dev
*dev
, u8 reg
)
433 /* 8 bit registers, 8 bit values */
438 struct i2c_msg msg
[] = {
439 { .addr
= 0x98 >> 1, .flags
= 0, .buf
= b0
, .len
= 1 },
440 { .addr
= 0x98 >> 1, .flags
= I2C_M_RD
, .buf
= b1
, .len
= 1 }
443 ret
= i2c_transfer(&dev
->i2c_bus
[2].i2c_adap
, &msg
[0], 2);
445 printk(KERN_ERR
"%s() error\n", __func__
);
450 static void cx23885_flatiron_dump(struct cx23885_dev
*dev
)
453 dprintk(1, "Flatiron dump\n");
454 for (i
= 0; i
< 0x24; i
++) {
455 dprintk(1, "FI[%02x] = %02x\n", i
,
456 cx23885_flatiron_read(dev
, i
));
460 static int cx23885_flatiron_mux(struct cx23885_dev
*dev
, int input
)
463 dprintk(1, "%s(input = %d)\n", __func__
, input
);
466 val
= cx23885_flatiron_read(dev
, CH_PWR_CTRL1
) & ~FLD_CH_SEL
;
468 val
= cx23885_flatiron_read(dev
, CH_PWR_CTRL1
) | FLD_CH_SEL
;
472 val
|= 0x20; /* Enable clock to delta-sigma and dec filter */
474 cx23885_flatiron_write(dev
, CH_PWR_CTRL1
, val
);
477 cx23885_flatiron_write(dev
, CH_PWR_CTRL2
, 0);
480 cx23885_flatiron_dump(dev
);
485 static int cx23885_video_mux(struct cx23885_dev
*dev
, unsigned int input
)
487 dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
489 input
, INPUT(input
)->vmux
,
490 INPUT(input
)->gpio0
, INPUT(input
)->gpio1
,
491 INPUT(input
)->gpio2
, INPUT(input
)->gpio3
);
494 if (dev
->board
== CX23885_BOARD_MYGICA_X8506
||
495 dev
->board
== CX23885_BOARD_MAGICPRO_PROHDTVE2
) {
496 /* Select Analog TV */
497 if (INPUT(input
)->type
== CX23885_VMUX_TELEVISION
)
498 cx23885_gpio_clear(dev
, GPIO_0
);
501 /* Tell the internal A/V decoder */
502 v4l2_subdev_call(dev
->sd_cx25840
, video
, s_routing
,
503 INPUT(input
)->vmux
, 0, 0);
505 if ((dev
->board
== CX23885_BOARD_HAUPPAUGE_HVR1800
) ||
506 (dev
->board
== CX23885_BOARD_MPX885
)) {
507 /* Configure audio routing */
508 v4l2_subdev_call(dev
->sd_cx25840
, audio
, s_routing
,
509 INPUT(input
)->amux
, 0, 0);
511 if (INPUT(input
)->amux
== CX25840_AUDIO7
)
512 cx23885_flatiron_mux(dev
, 1);
513 else if (INPUT(input
)->amux
== CX25840_AUDIO6
)
514 cx23885_flatiron_mux(dev
, 2);
520 static int cx23885_audio_mux(struct cx23885_dev
*dev
, unsigned int input
)
522 dprintk(1, "%s(input=%d)\n", __func__
, input
);
524 /* The baseband video core of the cx23885 has two audio inputs.
525 * LR1 and LR2. In almost every single case so far only HVR1xxx
526 * cards we've only ever supported LR1. Time to support LR2,
527 * which is available via the optional white breakout header on
529 * We'll use a could of existing enums in the card struct to allow
530 * devs to specify which baseband input they need, or just default
531 * to what we've always used.
533 if (INPUT(input
)->amux
== CX25840_AUDIO7
)
534 cx23885_flatiron_mux(dev
, 1);
535 else if (INPUT(input
)->amux
== CX25840_AUDIO6
)
536 cx23885_flatiron_mux(dev
, 2);
538 /* Not specifically defined, assume the default. */
539 cx23885_flatiron_mux(dev
, 1);
545 /* ------------------------------------------------------------------ */
546 static int cx23885_set_scale(struct cx23885_dev
*dev
, unsigned int width
,
547 unsigned int height
, enum v4l2_field field
)
549 dprintk(1, "%s()\n", __func__
);
553 static int cx23885_start_video_dma(struct cx23885_dev
*dev
,
554 struct cx23885_dmaqueue
*q
,
555 struct cx23885_buffer
*buf
)
557 dprintk(1, "%s()\n", __func__
);
559 /* setup fifo + format */
560 cx23885_sram_channel_setup(dev
, &dev
->sram_channels
[SRAM_CH01
],
561 buf
->bpl
, buf
->risc
.dma
);
562 cx23885_set_scale(dev
, buf
->vb
.width
, buf
->vb
.height
, buf
->vb
.field
);
565 cx_write(VID_A_GPCNT_CTL
, 3);
569 cx23885_irq_add_enable(dev
, 0x01);
570 cx_set(VID_A_INT_MSK
, 0x000011);
573 cx_set(DEV_CNTRL2
, (1<<5));
574 cx_set(VID_A_DMA_CTL
, 0x11); /* FIFO and RISC enable */
580 static int cx23885_restart_video_queue(struct cx23885_dev
*dev
,
581 struct cx23885_dmaqueue
*q
)
583 struct cx23885_buffer
*buf
, *prev
;
584 struct list_head
*item
;
585 dprintk(1, "%s()\n", __func__
);
587 if (!list_empty(&q
->active
)) {
588 buf
= list_entry(q
->active
.next
, struct cx23885_buffer
,
590 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
592 cx23885_start_video_dma(dev
, q
, buf
);
593 list_for_each(item
, &q
->active
) {
594 buf
= list_entry(item
, struct cx23885_buffer
,
596 buf
->count
= q
->count
++;
598 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
604 if (list_empty(&q
->queued
))
606 buf
= list_entry(q
->queued
.next
, struct cx23885_buffer
,
609 list_move_tail(&buf
->vb
.queue
, &q
->active
);
610 cx23885_start_video_dma(dev
, q
, buf
);
611 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
612 buf
->count
= q
->count
++;
613 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
614 dprintk(2, "[%p/%d] restart_queue - first active\n",
617 } else if (prev
->vb
.width
== buf
->vb
.width
&&
618 prev
->vb
.height
== buf
->vb
.height
&&
619 prev
->fmt
== buf
->fmt
) {
620 list_move_tail(&buf
->vb
.queue
, &q
->active
);
621 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
622 buf
->count
= q
->count
++;
623 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
624 prev
->risc
.jmp
[2] = cpu_to_le32(0); /* Bits 63 - 32 */
625 dprintk(2, "[%p/%d] restart_queue - move to active\n",
634 static int buffer_setup(struct videobuf_queue
*q
, unsigned int *count
,
637 struct cx23885_fh
*fh
= q
->priv_data
;
639 *size
= fh
->fmt
->depth
*fh
->width
*fh
->height
>> 3;
642 if (*size
* *count
> vid_limit
* 1024 * 1024)
643 *count
= (vid_limit
* 1024 * 1024) / *size
;
647 static int buffer_prepare(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
648 enum v4l2_field field
)
650 struct cx23885_fh
*fh
= q
->priv_data
;
651 struct cx23885_dev
*dev
= fh
->dev
;
652 struct cx23885_buffer
*buf
=
653 container_of(vb
, struct cx23885_buffer
, vb
);
654 int rc
, init_buffer
= 0;
655 u32 line0_offset
, line1_offset
;
656 struct videobuf_dmabuf
*dma
= videobuf_to_dma(&buf
->vb
);
658 BUG_ON(NULL
== fh
->fmt
);
659 if (fh
->width
< 48 || fh
->width
> norm_maxw(dev
->tvnorm
) ||
660 fh
->height
< 32 || fh
->height
> norm_maxh(dev
->tvnorm
))
662 buf
->vb
.size
= (fh
->width
* fh
->height
* fh
->fmt
->depth
) >> 3;
663 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
666 if (buf
->fmt
!= fh
->fmt
||
667 buf
->vb
.width
!= fh
->width
||
668 buf
->vb
.height
!= fh
->height
||
669 buf
->vb
.field
!= field
) {
671 buf
->vb
.width
= fh
->width
;
672 buf
->vb
.height
= fh
->height
;
673 buf
->vb
.field
= field
;
677 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
679 rc
= videobuf_iolock(q
, &buf
->vb
, NULL
);
685 buf
->bpl
= buf
->vb
.width
* buf
->fmt
->depth
>> 3;
686 switch (buf
->vb
.field
) {
688 cx23885_risc_buffer(dev
->pci
, &buf
->risc
,
689 dma
->sglist
, 0, UNSET
,
690 buf
->bpl
, 0, buf
->vb
.height
);
692 case V4L2_FIELD_BOTTOM
:
693 cx23885_risc_buffer(dev
->pci
, &buf
->risc
,
694 dma
->sglist
, UNSET
, 0,
695 buf
->bpl
, 0, buf
->vb
.height
);
697 case V4L2_FIELD_INTERLACED
:
698 if (dev
->tvnorm
& V4L2_STD_NTSC
) {
699 /* cx25840 transmits NTSC bottom field first */
700 dprintk(1, "%s() Creating NTSC risc\n",
702 line0_offset
= buf
->bpl
;
705 /* All other formats are top field first */
706 dprintk(1, "%s() Creating PAL/SECAM risc\n",
709 line1_offset
= buf
->bpl
;
711 cx23885_risc_buffer(dev
->pci
, &buf
->risc
,
712 dma
->sglist
, line0_offset
,
715 buf
->vb
.height
>> 1);
717 case V4L2_FIELD_SEQ_TB
:
718 cx23885_risc_buffer(dev
->pci
, &buf
->risc
,
720 0, buf
->bpl
* (buf
->vb
.height
>> 1),
722 buf
->vb
.height
>> 1);
724 case V4L2_FIELD_SEQ_BT
:
725 cx23885_risc_buffer(dev
->pci
, &buf
->risc
,
727 buf
->bpl
* (buf
->vb
.height
>> 1), 0,
729 buf
->vb
.height
>> 1);
735 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
737 fh
->width
, fh
->height
, fh
->fmt
->depth
, fh
->fmt
->name
,
738 (unsigned long)buf
->risc
.dma
);
740 buf
->vb
.state
= VIDEOBUF_PREPARED
;
744 cx23885_free_buffer(q
, buf
);
748 static void buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
750 struct cx23885_buffer
*buf
= container_of(vb
,
751 struct cx23885_buffer
, vb
);
752 struct cx23885_buffer
*prev
;
753 struct cx23885_fh
*fh
= vq
->priv_data
;
754 struct cx23885_dev
*dev
= fh
->dev
;
755 struct cx23885_dmaqueue
*q
= &dev
->vidq
;
757 /* add jump to stopper */
758 buf
->risc
.jmp
[0] = cpu_to_le32(RISC_JUMP
| RISC_IRQ1
| RISC_CNT_INC
);
759 buf
->risc
.jmp
[1] = cpu_to_le32(q
->stopper
.dma
);
760 buf
->risc
.jmp
[2] = cpu_to_le32(0); /* bits 63-32 */
762 if (!list_empty(&q
->queued
)) {
763 list_add_tail(&buf
->vb
.queue
, &q
->queued
);
764 buf
->vb
.state
= VIDEOBUF_QUEUED
;
765 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
768 } else if (list_empty(&q
->active
)) {
769 list_add_tail(&buf
->vb
.queue
, &q
->active
);
770 cx23885_start_video_dma(dev
, q
, buf
);
771 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
772 buf
->count
= q
->count
++;
773 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
774 dprintk(2, "[%p/%d] buffer_queue - first active\n",
778 prev
= list_entry(q
->active
.prev
, struct cx23885_buffer
,
780 if (prev
->vb
.width
== buf
->vb
.width
&&
781 prev
->vb
.height
== buf
->vb
.height
&&
782 prev
->fmt
== buf
->fmt
) {
783 list_add_tail(&buf
->vb
.queue
, &q
->active
);
784 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
785 buf
->count
= q
->count
++;
786 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
787 /* 64 bit bits 63-32 */
788 prev
->risc
.jmp
[2] = cpu_to_le32(0);
789 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
793 list_add_tail(&buf
->vb
.queue
, &q
->queued
);
794 buf
->vb
.state
= VIDEOBUF_QUEUED
;
795 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
801 static void buffer_release(struct videobuf_queue
*q
,
802 struct videobuf_buffer
*vb
)
804 struct cx23885_buffer
*buf
= container_of(vb
,
805 struct cx23885_buffer
, vb
);
807 cx23885_free_buffer(q
, buf
);
810 static struct videobuf_queue_ops cx23885_video_qops
= {
811 .buf_setup
= buffer_setup
,
812 .buf_prepare
= buffer_prepare
,
813 .buf_queue
= buffer_queue
,
814 .buf_release
= buffer_release
,
817 static struct videobuf_queue
*get_queue(struct cx23885_fh
*fh
)
820 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
822 case V4L2_BUF_TYPE_VBI_CAPTURE
:
830 static int get_resource(struct cx23885_fh
*fh
)
833 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
834 return RESOURCE_VIDEO
;
835 case V4L2_BUF_TYPE_VBI_CAPTURE
:
843 static int video_open(struct file
*file
)
845 struct video_device
*vdev
= video_devdata(file
);
846 struct cx23885_dev
*dev
= video_drvdata(file
);
847 struct cx23885_fh
*fh
;
848 enum v4l2_buf_type type
= 0;
851 switch (vdev
->vfl_type
) {
852 case VFL_TYPE_GRABBER
:
853 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
856 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
863 dprintk(1, "open dev=%s radio=%d type=%s\n",
864 video_device_node_name(vdev
), radio
, v4l2_type_names
[type
]);
866 /* allocate + initialize per filehandle data */
867 fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
871 file
->private_data
= fh
;
877 fh
->fmt
= format_by_fourcc(V4L2_PIX_FMT_YUYV
);
879 videobuf_queue_sg_init(&fh
->vidq
, &cx23885_video_qops
,
880 &dev
->pci
->dev
, &dev
->slock
,
881 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
882 V4L2_FIELD_INTERLACED
,
883 sizeof(struct cx23885_buffer
),
886 videobuf_queue_sg_init(&fh
->vbiq
, &cx23885_vbi_qops
,
887 &dev
->pci
->dev
, &dev
->slock
,
888 V4L2_BUF_TYPE_VBI_CAPTURE
,
890 sizeof(struct cx23885_buffer
),
894 dprintk(1, "post videobuf_queue_init()\n");
899 static ssize_t
video_read(struct file
*file
, char __user
*data
,
900 size_t count
, loff_t
*ppos
)
902 struct cx23885_fh
*fh
= file
->private_data
;
905 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
906 if (res_locked(fh
->dev
, RESOURCE_VIDEO
))
908 return videobuf_read_one(&fh
->vidq
, data
, count
, ppos
,
909 file
->f_flags
& O_NONBLOCK
);
910 case V4L2_BUF_TYPE_VBI_CAPTURE
:
911 if (!res_get(fh
->dev
, fh
, RESOURCE_VBI
))
913 return videobuf_read_stream(&fh
->vbiq
, data
, count
, ppos
, 1,
914 file
->f_flags
& O_NONBLOCK
);
921 static unsigned int video_poll(struct file
*file
,
922 struct poll_table_struct
*wait
)
924 struct cx23885_fh
*fh
= file
->private_data
;
925 struct cx23885_buffer
*buf
;
926 unsigned int rc
= POLLERR
;
928 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
) {
929 if (!res_get(fh
->dev
, fh
, RESOURCE_VBI
))
931 return videobuf_poll_stream(file
, &fh
->vbiq
, wait
);
934 mutex_lock(&fh
->vidq
.vb_lock
);
935 if (res_check(fh
, RESOURCE_VIDEO
)) {
936 /* streaming capture */
937 if (list_empty(&fh
->vidq
.stream
))
939 buf
= list_entry(fh
->vidq
.stream
.next
,
940 struct cx23885_buffer
, vb
.stream
);
943 buf
= (struct cx23885_buffer
*)fh
->vidq
.read_buf
;
947 poll_wait(file
, &buf
->vb
.done
, wait
);
948 if (buf
->vb
.state
== VIDEOBUF_DONE
||
949 buf
->vb
.state
== VIDEOBUF_ERROR
)
950 rc
= POLLIN
|POLLRDNORM
;
954 mutex_unlock(&fh
->vidq
.vb_lock
);
958 static int video_release(struct file
*file
)
960 struct cx23885_fh
*fh
= file
->private_data
;
961 struct cx23885_dev
*dev
= fh
->dev
;
963 /* turn off overlay */
964 if (res_check(fh
, RESOURCE_OVERLAY
)) {
966 res_free(dev
, fh
, RESOURCE_OVERLAY
);
969 /* stop video capture */
970 if (res_check(fh
, RESOURCE_VIDEO
)) {
971 videobuf_queue_cancel(&fh
->vidq
);
972 res_free(dev
, fh
, RESOURCE_VIDEO
);
974 if (fh
->vidq
.read_buf
) {
975 buffer_release(&fh
->vidq
, fh
->vidq
.read_buf
);
976 kfree(fh
->vidq
.read_buf
);
979 /* stop vbi capture */
980 if (res_check(fh
, RESOURCE_VBI
)) {
981 if (fh
->vbiq
.streaming
)
982 videobuf_streamoff(&fh
->vbiq
);
983 if (fh
->vbiq
.reading
)
984 videobuf_read_stop(&fh
->vbiq
);
985 res_free(dev
, fh
, RESOURCE_VBI
);
988 videobuf_mmap_free(&fh
->vidq
);
989 file
->private_data
= NULL
;
992 /* We are not putting the tuner to sleep here on exit, because
993 * we want to use the mpeg encoder in another session to capture
994 * tuner video. Closing this will result in no video to the encoder.
1000 static int video_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1002 struct cx23885_fh
*fh
= file
->private_data
;
1004 return videobuf_mmap_mapper(get_queue(fh
), vma
);
1007 /* ------------------------------------------------------------------ */
1008 /* VIDEO CTRL IOCTLS */
1010 static int cx23885_get_control(struct cx23885_dev
*dev
,
1011 struct v4l2_control
*ctl
)
1013 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__
);
1014 call_all(dev
, core
, g_ctrl
, ctl
);
1018 static int cx23885_set_control(struct cx23885_dev
*dev
,
1019 struct v4l2_control
*ctl
)
1021 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)\n", __func__
);
1022 call_all(dev
, core
, s_ctrl
, ctl
);
1027 static void init_controls(struct cx23885_dev
*dev
)
1029 struct v4l2_control ctrl
;
1032 for (i
= 0; i
< CX23885_CTLS
; i
++) {
1033 ctrl
.id
= cx23885_ctls
[i
].v
.id
;
1034 ctrl
.value
= cx23885_ctls
[i
].v
.default_value
;
1036 cx23885_set_control(dev
, &ctrl
);
1040 /* ------------------------------------------------------------------ */
1043 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
1044 struct v4l2_format
*f
)
1046 struct cx23885_fh
*fh
= priv
;
1048 f
->fmt
.pix
.width
= fh
->width
;
1049 f
->fmt
.pix
.height
= fh
->height
;
1050 f
->fmt
.pix
.field
= fh
->vidq
.field
;
1051 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
1052 f
->fmt
.pix
.bytesperline
=
1053 (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
1054 f
->fmt
.pix
.sizeimage
=
1055 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1060 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
1061 struct v4l2_format
*f
)
1063 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1064 struct cx23885_fmt
*fmt
;
1065 enum v4l2_field field
;
1066 unsigned int maxw
, maxh
;
1068 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1072 field
= f
->fmt
.pix
.field
;
1073 maxw
= norm_maxw(dev
->tvnorm
);
1074 maxh
= norm_maxh(dev
->tvnorm
);
1076 if (V4L2_FIELD_ANY
== field
) {
1077 field
= (f
->fmt
.pix
.height
> maxh
/2)
1078 ? V4L2_FIELD_INTERLACED
1079 : V4L2_FIELD_BOTTOM
;
1083 case V4L2_FIELD_TOP
:
1084 case V4L2_FIELD_BOTTOM
:
1087 case V4L2_FIELD_INTERLACED
:
1093 f
->fmt
.pix
.field
= field
;
1094 v4l_bound_align_image(&f
->fmt
.pix
.width
, 48, maxw
, 2,
1095 &f
->fmt
.pix
.height
, 32, maxh
, 0, 0);
1096 f
->fmt
.pix
.bytesperline
=
1097 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
1098 f
->fmt
.pix
.sizeimage
=
1099 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1104 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
1105 struct v4l2_format
*f
)
1107 struct cx23885_fh
*fh
= priv
;
1108 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1109 struct v4l2_mbus_framefmt mbus_fmt
;
1112 dprintk(2, "%s()\n", __func__
);
1113 err
= vidioc_try_fmt_vid_cap(file
, priv
, f
);
1117 fh
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1118 fh
->width
= f
->fmt
.pix
.width
;
1119 fh
->height
= f
->fmt
.pix
.height
;
1120 fh
->vidq
.field
= f
->fmt
.pix
.field
;
1121 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__
,
1122 fh
->width
, fh
->height
, fh
->vidq
.field
);
1123 v4l2_fill_mbus_format(&mbus_fmt
, &f
->fmt
.pix
, V4L2_MBUS_FMT_FIXED
);
1124 call_all(dev
, video
, s_mbus_fmt
, &mbus_fmt
);
1125 v4l2_fill_pix_format(&f
->fmt
.pix
, &mbus_fmt
);
1129 static int vidioc_querycap(struct file
*file
, void *priv
,
1130 struct v4l2_capability
*cap
)
1132 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1134 strcpy(cap
->driver
, "cx23885");
1135 strlcpy(cap
->card
, cx23885_boards
[dev
->board
].name
,
1137 sprintf(cap
->bus_info
, "PCIe:%s", pci_name(dev
->pci
));
1139 V4L2_CAP_VIDEO_CAPTURE
|
1140 V4L2_CAP_READWRITE
|
1141 V4L2_CAP_STREAMING
|
1142 V4L2_CAP_VBI_CAPTURE
;
1143 if (UNSET
!= dev
->tuner_type
)
1144 cap
->capabilities
|= V4L2_CAP_TUNER
;
1148 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1149 struct v4l2_fmtdesc
*f
)
1151 if (unlikely(f
->index
>= ARRAY_SIZE(formats
)))
1154 strlcpy(f
->description
, formats
[f
->index
].name
,
1155 sizeof(f
->description
));
1156 f
->pixelformat
= formats
[f
->index
].fourcc
;
1161 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1162 struct v4l2_requestbuffers
*p
)
1164 struct cx23885_fh
*fh
= priv
;
1165 return videobuf_reqbufs(get_queue(fh
), p
);
1168 static int vidioc_querybuf(struct file
*file
, void *priv
,
1169 struct v4l2_buffer
*p
)
1171 struct cx23885_fh
*fh
= priv
;
1172 return videobuf_querybuf(get_queue(fh
), p
);
1175 static int vidioc_qbuf(struct file
*file
, void *priv
,
1176 struct v4l2_buffer
*p
)
1178 struct cx23885_fh
*fh
= priv
;
1179 return videobuf_qbuf(get_queue(fh
), p
);
1182 static int vidioc_dqbuf(struct file
*file
, void *priv
,
1183 struct v4l2_buffer
*p
)
1185 struct cx23885_fh
*fh
= priv
;
1186 return videobuf_dqbuf(get_queue(fh
), p
,
1187 file
->f_flags
& O_NONBLOCK
);
1190 static int vidioc_streamon(struct file
*file
, void *priv
,
1191 enum v4l2_buf_type i
)
1193 struct cx23885_fh
*fh
= priv
;
1194 struct cx23885_dev
*dev
= fh
->dev
;
1195 dprintk(1, "%s()\n", __func__
);
1197 if ((fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) &&
1198 (fh
->type
!= V4L2_BUF_TYPE_VBI_CAPTURE
))
1200 if (unlikely(i
!= fh
->type
))
1203 if (unlikely(!res_get(dev
, fh
, get_resource(fh
))))
1206 /* Don't start VBI streaming unless vida streaming
1207 * has already started.
1209 if ((fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) &&
1210 ((cx_read(VID_A_DMA_CTL
) & 0x11) == 0))
1213 return videobuf_streamon(get_queue(fh
));
1216 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1218 struct cx23885_fh
*fh
= priv
;
1219 struct cx23885_dev
*dev
= fh
->dev
;
1221 dprintk(1, "%s()\n", __func__
);
1223 if ((fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) &&
1224 (fh
->type
!= V4L2_BUF_TYPE_VBI_CAPTURE
))
1229 res
= get_resource(fh
);
1230 err
= videobuf_streamoff(get_queue(fh
));
1233 res_free(dev
, fh
, res
);
1237 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*tvnorms
)
1239 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1240 dprintk(1, "%s()\n", __func__
);
1242 mutex_lock(&dev
->lock
);
1243 cx23885_set_tvnorm(dev
, *tvnorms
);
1244 mutex_unlock(&dev
->lock
);
1249 static int cx23885_enum_input(struct cx23885_dev
*dev
, struct v4l2_input
*i
)
1251 static const char *iname
[] = {
1252 [CX23885_VMUX_COMPOSITE1
] = "Composite1",
1253 [CX23885_VMUX_COMPOSITE2
] = "Composite2",
1254 [CX23885_VMUX_COMPOSITE3
] = "Composite3",
1255 [CX23885_VMUX_COMPOSITE4
] = "Composite4",
1256 [CX23885_VMUX_SVIDEO
] = "S-Video",
1257 [CX23885_VMUX_COMPONENT
] = "Component",
1258 [CX23885_VMUX_TELEVISION
] = "Television",
1259 [CX23885_VMUX_CABLE
] = "Cable TV",
1260 [CX23885_VMUX_DVB
] = "DVB",
1261 [CX23885_VMUX_DEBUG
] = "for debug only",
1264 dprintk(1, "%s()\n", __func__
);
1267 if (n
>= MAX_CX23885_INPUT
)
1270 if (0 == INPUT(n
)->type
)
1274 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1275 strcpy(i
->name
, iname
[INPUT(n
)->type
]);
1276 if ((CX23885_VMUX_TELEVISION
== INPUT(n
)->type
) ||
1277 (CX23885_VMUX_CABLE
== INPUT(n
)->type
)) {
1278 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1279 i
->std
= CX23885_NORMS
;
1284 static int vidioc_enum_input(struct file
*file
, void *priv
,
1285 struct v4l2_input
*i
)
1287 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1288 dprintk(1, "%s()\n", __func__
);
1289 return cx23885_enum_input(dev
, i
);
1292 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1294 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1297 dprintk(1, "%s() returns %d\n", __func__
, *i
);
1301 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1303 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1305 dprintk(1, "%s(%d)\n", __func__
, i
);
1307 if (i
>= MAX_CX23885_INPUT
) {
1308 dprintk(1, "%s() -EINVAL\n", __func__
);
1312 if (INPUT(i
)->type
== 0)
1315 mutex_lock(&dev
->lock
);
1316 cx23885_video_mux(dev
, i
);
1318 /* By default establish the default audio input for the card also */
1319 /* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
1320 cx23885_audio_mux(dev
, i
);
1321 mutex_unlock(&dev
->lock
);
1325 static int vidioc_log_status(struct file
*file
, void *priv
)
1327 struct cx23885_fh
*fh
= priv
;
1328 struct cx23885_dev
*dev
= fh
->dev
;
1331 "%s/0: ============ START LOG STATUS ============\n",
1333 call_all(dev
, core
, log_status
);
1335 "%s/0: ============= END LOG STATUS =============\n",
1340 static int cx23885_query_audinput(struct file
*file
, void *priv
,
1341 struct v4l2_audio
*i
)
1343 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1344 static const char *iname
[] = {
1345 [0] = "Baseband L/R 1",
1346 [1] = "Baseband L/R 2",
1349 dprintk(1, "%s()\n", __func__
);
1355 memset(i
, 0, sizeof(*i
));
1357 strcpy(i
->name
, iname
[n
]);
1358 i
->capability
= V4L2_AUDCAP_STEREO
;
1359 i
->mode
= V4L2_AUDMODE_AVL
;
1364 static int vidioc_enum_audinput(struct file
*file
, void *priv
,
1365 struct v4l2_audio
*i
)
1367 return cx23885_query_audinput(file
, priv
, i
);
1370 static int vidioc_g_audinput(struct file
*file
, void *priv
,
1371 struct v4l2_audio
*i
)
1373 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1375 i
->index
= dev
->audinput
;
1376 dprintk(1, "%s(input=%d)\n", __func__
, i
->index
);
1378 return cx23885_query_audinput(file
, priv
, i
);
1381 static int vidioc_s_audinput(struct file
*file
, void *priv
,
1382 struct v4l2_audio
*i
)
1384 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1388 dprintk(1, "%s(%d)\n", __func__
, i
->index
);
1390 dev
->audinput
= i
->index
;
1392 /* Skip the audio defaults from the cards struct, caller wants
1393 * directly touch the audio mux hardware. */
1394 cx23885_flatiron_mux(dev
, dev
->audinput
+ 1);
1398 static int vidioc_queryctrl(struct file
*file
, void *priv
,
1399 struct v4l2_queryctrl
*qctrl
)
1401 qctrl
->id
= v4l2_ctrl_next(ctrl_classes
, qctrl
->id
);
1402 if (unlikely(qctrl
->id
== 0))
1404 return cx23885_ctrl_query(qctrl
);
1407 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1408 struct v4l2_control
*ctl
)
1410 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1412 return cx23885_get_control(dev
, ctl
);
1415 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1416 struct v4l2_control
*ctl
)
1418 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1420 return cx23885_set_control(dev
, ctl
);
1423 static int vidioc_g_tuner(struct file
*file
, void *priv
,
1424 struct v4l2_tuner
*t
)
1426 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1428 if (unlikely(UNSET
== dev
->tuner_type
))
1433 strcpy(t
->name
, "Television");
1435 call_all(dev
, tuner
, g_tuner
, t
);
1439 static int vidioc_s_tuner(struct file
*file
, void *priv
,
1440 struct v4l2_tuner
*t
)
1442 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1444 if (UNSET
== dev
->tuner_type
)
1448 /* Update the A/V core */
1449 call_all(dev
, tuner
, s_tuner
, t
);
1454 static int vidioc_g_frequency(struct file
*file
, void *priv
,
1455 struct v4l2_frequency
*f
)
1457 struct cx23885_fh
*fh
= priv
;
1458 struct cx23885_dev
*dev
= fh
->dev
;
1460 if (unlikely(UNSET
== dev
->tuner_type
))
1463 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1464 f
->type
= fh
->radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1465 f
->frequency
= dev
->freq
;
1467 call_all(dev
, tuner
, g_frequency
, f
);
1472 static int cx23885_set_freq(struct cx23885_dev
*dev
, struct v4l2_frequency
*f
)
1474 if (unlikely(UNSET
== dev
->tuner_type
))
1476 if (unlikely(f
->tuner
!= 0))
1479 mutex_lock(&dev
->lock
);
1480 dev
->freq
= f
->frequency
;
1482 call_all(dev
, tuner
, s_frequency
, f
);
1484 /* When changing channels it is required to reset TVAUDIO */
1487 mutex_unlock(&dev
->lock
);
1492 static int vidioc_s_frequency(struct file
*file
, void *priv
,
1493 struct v4l2_frequency
*f
)
1495 struct cx23885_fh
*fh
= priv
;
1496 struct cx23885_dev
*dev
= fh
->dev
;
1498 if (unlikely(0 == fh
->radio
&& f
->type
!= V4L2_TUNER_ANALOG_TV
))
1500 if (unlikely(1 == fh
->radio
&& f
->type
!= V4L2_TUNER_RADIO
))
1504 cx23885_set_freq(dev
, f
);
1507 /* ----------------------------------------------------------- */
1509 static void cx23885_vid_timeout(unsigned long data
)
1511 struct cx23885_dev
*dev
= (struct cx23885_dev
*)data
;
1512 struct cx23885_dmaqueue
*q
= &dev
->vidq
;
1513 struct cx23885_buffer
*buf
;
1514 unsigned long flags
;
1516 cx23885_sram_channel_dump(dev
, &dev
->sram_channels
[SRAM_CH01
]);
1518 cx_clear(VID_A_DMA_CTL
, 0x11);
1520 spin_lock_irqsave(&dev
->slock
, flags
);
1521 while (!list_empty(&q
->active
)) {
1522 buf
= list_entry(q
->active
.next
,
1523 struct cx23885_buffer
, vb
.queue
);
1524 list_del(&buf
->vb
.queue
);
1525 buf
->vb
.state
= VIDEOBUF_ERROR
;
1526 wake_up(&buf
->vb
.done
);
1527 printk(KERN_ERR
"%s: [%p/%d] timeout - dma=0x%08lx\n",
1528 dev
->name
, buf
, buf
->vb
.i
,
1529 (unsigned long)buf
->risc
.dma
);
1531 cx23885_restart_video_queue(dev
, q
);
1532 spin_unlock_irqrestore(&dev
->slock
, flags
);
1535 int cx23885_video_irq(struct cx23885_dev
*dev
, u32 status
)
1540 mask
= cx_read(VID_A_INT_MSK
);
1541 if (0 == (status
& mask
))
1544 cx_write(VID_A_INT_STAT
, status
);
1546 /* risc op code error */
1547 if ((status
& VID_BC_MSK_OPC_ERR
) ||
1548 (status
& VID_BC_MSK_SYNC
) ||
1549 (status
& VID_BC_MSK_OF
)) {
1551 if (status
& VID_BC_MSK_OPC_ERR
)
1552 dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1553 VID_BC_MSK_OPC_ERR
);
1555 if (status
& VID_BC_MSK_SYNC
)
1556 dprintk(7, " (VID_BC_MSK_SYNC 0x%08x)\n",
1559 if (status
& VID_BC_MSK_OF
)
1560 dprintk(7, " (VID_BC_MSK_OF 0x%08x)\n",
1563 printk(KERN_WARNING
"%s: video risc op code error\n",
1566 cx_clear(VID_A_DMA_CTL
, 0x11);
1567 cx23885_sram_channel_dump(dev
, &dev
->sram_channels
[SRAM_CH01
]);
1572 if (status
& VID_BC_MSK_RISCI1
) {
1573 spin_lock(&dev
->slock
);
1574 count
= cx_read(VID_A_GPCNT
);
1575 cx23885_video_wakeup(dev
, &dev
->vidq
, count
);
1576 spin_unlock(&dev
->slock
);
1579 if (status
& VID_BC_MSK_RISCI2
) {
1580 dprintk(2, "stopper video\n");
1581 spin_lock(&dev
->slock
);
1582 cx23885_restart_video_queue(dev
, &dev
->vidq
);
1583 spin_unlock(&dev
->slock
);
1587 /* Allow the VBI framework to process it's payload */
1588 handled
+= cx23885_vbi_irq(dev
, status
);
1593 /* ----------------------------------------------------------- */
1594 /* exported stuff */
1596 static const struct v4l2_file_operations video_fops
= {
1597 .owner
= THIS_MODULE
,
1599 .release
= video_release
,
1603 .ioctl
= video_ioctl2
,
1606 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
1607 .vidioc_querycap
= vidioc_querycap
,
1608 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1609 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1610 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1611 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1612 .vidioc_g_fmt_vbi_cap
= cx23885_vbi_fmt
,
1613 .vidioc_try_fmt_vbi_cap
= cx23885_vbi_fmt
,
1614 .vidioc_s_fmt_vbi_cap
= cx23885_vbi_fmt
,
1615 .vidioc_reqbufs
= vidioc_reqbufs
,
1616 .vidioc_querybuf
= vidioc_querybuf
,
1617 .vidioc_qbuf
= vidioc_qbuf
,
1618 .vidioc_dqbuf
= vidioc_dqbuf
,
1619 .vidioc_s_std
= vidioc_s_std
,
1620 .vidioc_enum_input
= vidioc_enum_input
,
1621 .vidioc_g_input
= vidioc_g_input
,
1622 .vidioc_s_input
= vidioc_s_input
,
1623 .vidioc_log_status
= vidioc_log_status
,
1624 .vidioc_queryctrl
= vidioc_queryctrl
,
1625 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1626 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1627 .vidioc_streamon
= vidioc_streamon
,
1628 .vidioc_streamoff
= vidioc_streamoff
,
1629 .vidioc_g_tuner
= vidioc_g_tuner
,
1630 .vidioc_s_tuner
= vidioc_s_tuner
,
1631 .vidioc_g_frequency
= vidioc_g_frequency
,
1632 .vidioc_s_frequency
= vidioc_s_frequency
,
1633 .vidioc_g_chip_ident
= cx23885_g_chip_ident
,
1634 #ifdef CONFIG_VIDEO_ADV_DEBUG
1635 .vidioc_g_register
= cx23885_g_register
,
1636 .vidioc_s_register
= cx23885_s_register
,
1638 .vidioc_enumaudio
= vidioc_enum_audinput
,
1639 .vidioc_g_audio
= vidioc_g_audinput
,
1640 .vidioc_s_audio
= vidioc_s_audinput
,
1643 static struct video_device cx23885_vbi_template
;
1644 static struct video_device cx23885_video_template
= {
1645 .name
= "cx23885-video",
1646 .fops
= &video_fops
,
1647 .ioctl_ops
= &video_ioctl_ops
,
1648 .tvnorms
= CX23885_NORMS
,
1649 .current_norm
= V4L2_STD_NTSC_M
,
1652 static const struct v4l2_file_operations radio_fops
= {
1653 .owner
= THIS_MODULE
,
1655 .release
= video_release
,
1656 .ioctl
= video_ioctl2
,
1660 void cx23885_video_unregister(struct cx23885_dev
*dev
)
1662 dprintk(1, "%s()\n", __func__
);
1663 cx23885_irq_remove(dev
, 0x01);
1666 if (video_is_registered(dev
->vbi_dev
))
1667 video_unregister_device(dev
->vbi_dev
);
1669 video_device_release(dev
->vbi_dev
);
1670 dev
->vbi_dev
= NULL
;
1671 btcx_riscmem_free(dev
->pci
, &dev
->vbiq
.stopper
);
1673 if (dev
->video_dev
) {
1674 if (video_is_registered(dev
->video_dev
))
1675 video_unregister_device(dev
->video_dev
);
1677 video_device_release(dev
->video_dev
);
1678 dev
->video_dev
= NULL
;
1680 btcx_riscmem_free(dev
->pci
, &dev
->vidq
.stopper
);
1684 cx23885_audio_unregister(dev
);
1687 int cx23885_video_register(struct cx23885_dev
*dev
)
1691 dprintk(1, "%s()\n", __func__
);
1692 spin_lock_init(&dev
->slock
);
1694 /* Initialize VBI template */
1695 memcpy(&cx23885_vbi_template
, &cx23885_video_template
,
1696 sizeof(cx23885_vbi_template
));
1697 strcpy(cx23885_vbi_template
.name
, "cx23885-vbi");
1699 dev
->tvnorm
= cx23885_video_template
.current_norm
;
1701 /* init video dma queues */
1702 INIT_LIST_HEAD(&dev
->vidq
.active
);
1703 INIT_LIST_HEAD(&dev
->vidq
.queued
);
1704 dev
->vidq
.timeout
.function
= cx23885_vid_timeout
;
1705 dev
->vidq
.timeout
.data
= (unsigned long)dev
;
1706 init_timer(&dev
->vidq
.timeout
);
1707 cx23885_risc_stopper(dev
->pci
, &dev
->vidq
.stopper
,
1708 VID_A_DMA_CTL
, 0x11, 0x00);
1710 /* init vbi dma queues */
1711 INIT_LIST_HEAD(&dev
->vbiq
.active
);
1712 INIT_LIST_HEAD(&dev
->vbiq
.queued
);
1713 dev
->vbiq
.timeout
.function
= cx23885_vbi_timeout
;
1714 dev
->vbiq
.timeout
.data
= (unsigned long)dev
;
1715 init_timer(&dev
->vbiq
.timeout
);
1716 cx23885_risc_stopper(dev
->pci
, &dev
->vbiq
.stopper
,
1717 VID_A_DMA_CTL
, 0x22, 0x00);
1719 cx23885_irq_add_enable(dev
, 0x01);
1721 if ((TUNER_ABSENT
!= dev
->tuner_type
) &&
1722 ((dev
->tuner_bus
== 0) || (dev
->tuner_bus
== 1))) {
1723 struct v4l2_subdev
*sd
= NULL
;
1725 if (dev
->tuner_addr
)
1726 sd
= v4l2_i2c_new_subdev(&dev
->v4l2_dev
,
1727 &dev
->i2c_bus
[dev
->tuner_bus
].i2c_adap
,
1728 "tuner", dev
->tuner_addr
, NULL
);
1730 sd
= v4l2_i2c_new_subdev(&dev
->v4l2_dev
,
1731 &dev
->i2c_bus
[dev
->tuner_bus
].i2c_adap
,
1732 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV
));
1734 struct tuner_setup tun_setup
;
1736 memset(&tun_setup
, 0, sizeof(tun_setup
));
1737 tun_setup
.mode_mask
= T_ANALOG_TV
;
1738 tun_setup
.type
= dev
->tuner_type
;
1739 tun_setup
.addr
= v4l2_i2c_subdev_addr(sd
);
1740 tun_setup
.tuner_callback
= cx23885_tuner_callback
;
1742 v4l2_subdev_call(sd
, tuner
, s_type_addr
, &tun_setup
);
1744 if (dev
->board
== CX23885_BOARD_LEADTEK_WINFAST_PXTV1200
) {
1745 struct xc2028_ctrl ctrl
= {
1746 .fname
= XC2028_DEFAULT_FIRMWARE
,
1749 struct v4l2_priv_tun_config cfg
= {
1750 .tuner
= dev
->tuner_type
,
1753 v4l2_subdev_call(sd
, tuner
, s_config
, &cfg
);
1758 /* register Video device */
1759 dev
->video_dev
= cx23885_vdev_init(dev
, dev
->pci
,
1760 &cx23885_video_template
, "video");
1761 err
= video_register_device(dev
->video_dev
, VFL_TYPE_GRABBER
,
1764 printk(KERN_INFO
"%s: can't register video device\n",
1768 printk(KERN_INFO
"%s: registered device %s [v4l2]\n",
1769 dev
->name
, video_device_node_name(dev
->video_dev
));
1771 /* register VBI device */
1772 dev
->vbi_dev
= cx23885_vdev_init(dev
, dev
->pci
,
1773 &cx23885_vbi_template
, "vbi");
1774 err
= video_register_device(dev
->vbi_dev
, VFL_TYPE_VBI
,
1777 printk(KERN_INFO
"%s: can't register vbi device\n",
1781 printk(KERN_INFO
"%s: registered device %s\n",
1782 dev
->name
, video_device_node_name(dev
->vbi_dev
));
1784 /* Register ALSA audio device */
1785 dev
->audio_dev
= cx23885_audio_register(dev
);
1787 /* initial device configuration */
1788 mutex_lock(&dev
->lock
);
1789 cx23885_set_tvnorm(dev
, dev
->tvnorm
);
1791 cx23885_video_mux(dev
, 0);
1792 cx23885_audio_mux(dev
, 0);
1793 mutex_unlock(&dev
->lock
);
1798 cx23885_video_unregister(dev
);