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
= 65535,
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 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)",
348 cx23885_boards
[dev
->board
].name
, type
);
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 dev
->board
== CX23885_BOARD_MYGICA_X8507
) {
497 /* Select Analog TV */
498 if (INPUT(input
)->type
== CX23885_VMUX_TELEVISION
)
499 cx23885_gpio_clear(dev
, GPIO_0
);
502 /* Tell the internal A/V decoder */
503 v4l2_subdev_call(dev
->sd_cx25840
, video
, s_routing
,
504 INPUT(input
)->vmux
, 0, 0);
506 if ((dev
->board
== CX23885_BOARD_HAUPPAUGE_HVR1800
) ||
507 (dev
->board
== CX23885_BOARD_MPX885
) ||
508 (dev
->board
== CX23885_BOARD_HAUPPAUGE_HVR1850
)) {
509 /* Configure audio routing */
510 v4l2_subdev_call(dev
->sd_cx25840
, audio
, s_routing
,
511 INPUT(input
)->amux
, 0, 0);
513 if (INPUT(input
)->amux
== CX25840_AUDIO7
)
514 cx23885_flatiron_mux(dev
, 1);
515 else if (INPUT(input
)->amux
== CX25840_AUDIO6
)
516 cx23885_flatiron_mux(dev
, 2);
522 static int cx23885_audio_mux(struct cx23885_dev
*dev
, unsigned int input
)
524 dprintk(1, "%s(input=%d)\n", __func__
, input
);
526 /* The baseband video core of the cx23885 has two audio inputs.
527 * LR1 and LR2. In almost every single case so far only HVR1xxx
528 * cards we've only ever supported LR1. Time to support LR2,
529 * which is available via the optional white breakout header on
531 * We'll use a could of existing enums in the card struct to allow
532 * devs to specify which baseband input they need, or just default
533 * to what we've always used.
535 if (INPUT(input
)->amux
== CX25840_AUDIO7
)
536 cx23885_flatiron_mux(dev
, 1);
537 else if (INPUT(input
)->amux
== CX25840_AUDIO6
)
538 cx23885_flatiron_mux(dev
, 2);
540 /* Not specifically defined, assume the default. */
541 cx23885_flatiron_mux(dev
, 1);
547 /* ------------------------------------------------------------------ */
548 static int cx23885_start_video_dma(struct cx23885_dev
*dev
,
549 struct cx23885_dmaqueue
*q
,
550 struct cx23885_buffer
*buf
)
552 dprintk(1, "%s()\n", __func__
);
554 /* Stop the dma/fifo before we tamper with it's risc programs */
555 cx_clear(VID_A_DMA_CTL
, 0x11);
557 /* setup fifo + format */
558 cx23885_sram_channel_setup(dev
, &dev
->sram_channels
[SRAM_CH01
],
559 buf
->bpl
, buf
->risc
.dma
);
562 cx_write(VID_A_GPCNT_CTL
, 3);
566 cx23885_irq_add_enable(dev
, 0x01);
567 cx_set(VID_A_INT_MSK
, 0x000011);
570 cx_set(DEV_CNTRL2
, (1<<5));
571 cx_set(VID_A_DMA_CTL
, 0x11); /* FIFO and RISC enable */
577 static int cx23885_restart_video_queue(struct cx23885_dev
*dev
,
578 struct cx23885_dmaqueue
*q
)
580 struct cx23885_buffer
*buf
, *prev
;
581 struct list_head
*item
;
582 dprintk(1, "%s()\n", __func__
);
584 if (!list_empty(&q
->active
)) {
585 buf
= list_entry(q
->active
.next
, struct cx23885_buffer
,
587 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
589 cx23885_start_video_dma(dev
, q
, buf
);
590 list_for_each(item
, &q
->active
) {
591 buf
= list_entry(item
, struct cx23885_buffer
,
593 buf
->count
= q
->count
++;
595 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
601 if (list_empty(&q
->queued
))
603 buf
= list_entry(q
->queued
.next
, struct cx23885_buffer
,
606 list_move_tail(&buf
->vb
.queue
, &q
->active
);
607 cx23885_start_video_dma(dev
, q
, buf
);
608 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
609 buf
->count
= q
->count
++;
610 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
611 dprintk(2, "[%p/%d] restart_queue - first active\n",
614 } else if (prev
->vb
.width
== buf
->vb
.width
&&
615 prev
->vb
.height
== buf
->vb
.height
&&
616 prev
->fmt
== buf
->fmt
) {
617 list_move_tail(&buf
->vb
.queue
, &q
->active
);
618 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
619 buf
->count
= q
->count
++;
620 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
621 prev
->risc
.jmp
[2] = cpu_to_le32(0); /* Bits 63 - 32 */
622 dprintk(2, "[%p/%d] restart_queue - move to active\n",
631 static int buffer_setup(struct videobuf_queue
*q
, unsigned int *count
,
634 struct cx23885_fh
*fh
= q
->priv_data
;
636 *size
= fh
->fmt
->depth
*fh
->width
*fh
->height
>> 3;
639 if (*size
* *count
> vid_limit
* 1024 * 1024)
640 *count
= (vid_limit
* 1024 * 1024) / *size
;
644 static int buffer_prepare(struct videobuf_queue
*q
, struct videobuf_buffer
*vb
,
645 enum v4l2_field field
)
647 struct cx23885_fh
*fh
= q
->priv_data
;
648 struct cx23885_dev
*dev
= fh
->dev
;
649 struct cx23885_buffer
*buf
=
650 container_of(vb
, struct cx23885_buffer
, vb
);
651 int rc
, init_buffer
= 0;
652 u32 line0_offset
, line1_offset
;
653 struct videobuf_dmabuf
*dma
= videobuf_to_dma(&buf
->vb
);
656 BUG_ON(NULL
== fh
->fmt
);
657 if (fh
->width
< 48 || fh
->width
> norm_maxw(dev
->tvnorm
) ||
658 fh
->height
< 32 || fh
->height
> norm_maxh(dev
->tvnorm
))
660 buf
->vb
.size
= (fh
->width
* fh
->height
* fh
->fmt
->depth
) >> 3;
661 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
664 if (buf
->fmt
!= fh
->fmt
||
665 buf
->vb
.width
!= fh
->width
||
666 buf
->vb
.height
!= fh
->height
||
667 buf
->vb
.field
!= field
) {
669 buf
->vb
.width
= fh
->width
;
670 buf
->vb
.height
= fh
->height
;
671 buf
->vb
.field
= field
;
675 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
677 rc
= videobuf_iolock(q
, &buf
->vb
, NULL
);
683 buf
->bpl
= buf
->vb
.width
* buf
->fmt
->depth
>> 3;
684 switch (buf
->vb
.field
) {
686 cx23885_risc_buffer(dev
->pci
, &buf
->risc
,
687 dma
->sglist
, 0, UNSET
,
688 buf
->bpl
, 0, buf
->vb
.height
);
690 case V4L2_FIELD_BOTTOM
:
691 cx23885_risc_buffer(dev
->pci
, &buf
->risc
,
692 dma
->sglist
, UNSET
, 0,
693 buf
->bpl
, 0, buf
->vb
.height
);
695 case V4L2_FIELD_INTERLACED
:
696 if (dev
->tvnorm
& V4L2_STD_NTSC
)
702 if (cx23885_boards
[dev
->board
].force_bff
)
703 /* PAL / SECAM OR 888 in NTSC MODE */
707 /* cx25840 transmits NTSC bottom field first */
708 dprintk(1, "%s() Creating TFF/NTSC risc\n",
710 line0_offset
= buf
->bpl
;
713 /* All other formats are top field first */
714 dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
717 line1_offset
= buf
->bpl
;
719 cx23885_risc_buffer(dev
->pci
, &buf
->risc
,
720 dma
->sglist
, line0_offset
,
723 buf
->vb
.height
>> 1);
725 case V4L2_FIELD_SEQ_TB
:
726 cx23885_risc_buffer(dev
->pci
, &buf
->risc
,
728 0, buf
->bpl
* (buf
->vb
.height
>> 1),
730 buf
->vb
.height
>> 1);
732 case V4L2_FIELD_SEQ_BT
:
733 cx23885_risc_buffer(dev
->pci
, &buf
->risc
,
735 buf
->bpl
* (buf
->vb
.height
>> 1), 0,
737 buf
->vb
.height
>> 1);
743 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
745 fh
->width
, fh
->height
, fh
->fmt
->depth
, fh
->fmt
->name
,
746 (unsigned long)buf
->risc
.dma
);
748 buf
->vb
.state
= VIDEOBUF_PREPARED
;
752 cx23885_free_buffer(q
, buf
);
756 static void buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
758 struct cx23885_buffer
*buf
= container_of(vb
,
759 struct cx23885_buffer
, vb
);
760 struct cx23885_buffer
*prev
;
761 struct cx23885_fh
*fh
= vq
->priv_data
;
762 struct cx23885_dev
*dev
= fh
->dev
;
763 struct cx23885_dmaqueue
*q
= &dev
->vidq
;
765 /* add jump to stopper */
766 buf
->risc
.jmp
[0] = cpu_to_le32(RISC_JUMP
| RISC_IRQ1
| RISC_CNT_INC
);
767 buf
->risc
.jmp
[1] = cpu_to_le32(q
->stopper
.dma
);
768 buf
->risc
.jmp
[2] = cpu_to_le32(0); /* bits 63-32 */
770 if (!list_empty(&q
->queued
)) {
771 list_add_tail(&buf
->vb
.queue
, &q
->queued
);
772 buf
->vb
.state
= VIDEOBUF_QUEUED
;
773 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
776 } else if (list_empty(&q
->active
)) {
777 list_add_tail(&buf
->vb
.queue
, &q
->active
);
778 cx23885_start_video_dma(dev
, q
, buf
);
779 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
780 buf
->count
= q
->count
++;
781 mod_timer(&q
->timeout
, jiffies
+BUFFER_TIMEOUT
);
782 dprintk(2, "[%p/%d] buffer_queue - first active\n",
786 prev
= list_entry(q
->active
.prev
, struct cx23885_buffer
,
788 if (prev
->vb
.width
== buf
->vb
.width
&&
789 prev
->vb
.height
== buf
->vb
.height
&&
790 prev
->fmt
== buf
->fmt
) {
791 list_add_tail(&buf
->vb
.queue
, &q
->active
);
792 buf
->vb
.state
= VIDEOBUF_ACTIVE
;
793 buf
->count
= q
->count
++;
794 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
795 /* 64 bit bits 63-32 */
796 prev
->risc
.jmp
[2] = cpu_to_le32(0);
797 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
801 list_add_tail(&buf
->vb
.queue
, &q
->queued
);
802 buf
->vb
.state
= VIDEOBUF_QUEUED
;
803 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
809 static void buffer_release(struct videobuf_queue
*q
,
810 struct videobuf_buffer
*vb
)
812 struct cx23885_buffer
*buf
= container_of(vb
,
813 struct cx23885_buffer
, vb
);
815 cx23885_free_buffer(q
, buf
);
818 static struct videobuf_queue_ops cx23885_video_qops
= {
819 .buf_setup
= buffer_setup
,
820 .buf_prepare
= buffer_prepare
,
821 .buf_queue
= buffer_queue
,
822 .buf_release
= buffer_release
,
825 static struct videobuf_queue
*get_queue(struct cx23885_fh
*fh
)
828 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
830 case V4L2_BUF_TYPE_VBI_CAPTURE
:
838 static int get_resource(struct cx23885_fh
*fh
)
841 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
842 return RESOURCE_VIDEO
;
843 case V4L2_BUF_TYPE_VBI_CAPTURE
:
851 static int video_open(struct file
*file
)
853 struct video_device
*vdev
= video_devdata(file
);
854 struct cx23885_dev
*dev
= video_drvdata(file
);
855 struct cx23885_fh
*fh
;
856 enum v4l2_buf_type type
= 0;
859 switch (vdev
->vfl_type
) {
860 case VFL_TYPE_GRABBER
:
861 type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
864 type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
871 dprintk(1, "open dev=%s radio=%d type=%s\n",
872 video_device_node_name(vdev
), radio
, v4l2_type_names
[type
]);
874 /* allocate + initialize per filehandle data */
875 fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
879 file
->private_data
= fh
;
885 fh
->fmt
= format_by_fourcc(V4L2_PIX_FMT_YUYV
);
887 videobuf_queue_sg_init(&fh
->vidq
, &cx23885_video_qops
,
888 &dev
->pci
->dev
, &dev
->slock
,
889 V4L2_BUF_TYPE_VIDEO_CAPTURE
,
890 V4L2_FIELD_INTERLACED
,
891 sizeof(struct cx23885_buffer
),
894 videobuf_queue_sg_init(&fh
->vbiq
, &cx23885_vbi_qops
,
895 &dev
->pci
->dev
, &dev
->slock
,
896 V4L2_BUF_TYPE_VBI_CAPTURE
,
898 sizeof(struct cx23885_buffer
),
902 dprintk(1, "post videobuf_queue_init()\n");
907 static ssize_t
video_read(struct file
*file
, char __user
*data
,
908 size_t count
, loff_t
*ppos
)
910 struct cx23885_fh
*fh
= file
->private_data
;
913 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
914 if (res_locked(fh
->dev
, RESOURCE_VIDEO
))
916 return videobuf_read_one(&fh
->vidq
, data
, count
, ppos
,
917 file
->f_flags
& O_NONBLOCK
);
918 case V4L2_BUF_TYPE_VBI_CAPTURE
:
919 if (!res_get(fh
->dev
, fh
, RESOURCE_VBI
))
921 return videobuf_read_stream(&fh
->vbiq
, data
, count
, ppos
, 1,
922 file
->f_flags
& O_NONBLOCK
);
929 static unsigned int video_poll(struct file
*file
,
930 struct poll_table_struct
*wait
)
932 struct cx23885_fh
*fh
= file
->private_data
;
933 struct cx23885_buffer
*buf
;
934 unsigned int rc
= POLLERR
;
936 if (V4L2_BUF_TYPE_VBI_CAPTURE
== fh
->type
) {
937 if (!res_get(fh
->dev
, fh
, RESOURCE_VBI
))
939 return videobuf_poll_stream(file
, &fh
->vbiq
, wait
);
942 mutex_lock(&fh
->vidq
.vb_lock
);
943 if (res_check(fh
, RESOURCE_VIDEO
)) {
944 /* streaming capture */
945 if (list_empty(&fh
->vidq
.stream
))
947 buf
= list_entry(fh
->vidq
.stream
.next
,
948 struct cx23885_buffer
, vb
.stream
);
951 buf
= (struct cx23885_buffer
*)fh
->vidq
.read_buf
;
955 poll_wait(file
, &buf
->vb
.done
, wait
);
956 if (buf
->vb
.state
== VIDEOBUF_DONE
||
957 buf
->vb
.state
== VIDEOBUF_ERROR
)
958 rc
= POLLIN
|POLLRDNORM
;
962 mutex_unlock(&fh
->vidq
.vb_lock
);
966 static int video_release(struct file
*file
)
968 struct cx23885_fh
*fh
= file
->private_data
;
969 struct cx23885_dev
*dev
= fh
->dev
;
971 /* turn off overlay */
972 if (res_check(fh
, RESOURCE_OVERLAY
)) {
974 res_free(dev
, fh
, RESOURCE_OVERLAY
);
977 /* stop video capture */
978 if (res_check(fh
, RESOURCE_VIDEO
)) {
979 videobuf_queue_cancel(&fh
->vidq
);
980 res_free(dev
, fh
, RESOURCE_VIDEO
);
982 if (fh
->vidq
.read_buf
) {
983 buffer_release(&fh
->vidq
, fh
->vidq
.read_buf
);
984 kfree(fh
->vidq
.read_buf
);
987 /* stop vbi capture */
988 if (res_check(fh
, RESOURCE_VBI
)) {
989 if (fh
->vbiq
.streaming
)
990 videobuf_streamoff(&fh
->vbiq
);
991 if (fh
->vbiq
.reading
)
992 videobuf_read_stop(&fh
->vbiq
);
993 res_free(dev
, fh
, RESOURCE_VBI
);
996 videobuf_mmap_free(&fh
->vidq
);
997 videobuf_mmap_free(&fh
->vbiq
);
999 file
->private_data
= NULL
;
1002 /* We are not putting the tuner to sleep here on exit, because
1003 * we want to use the mpeg encoder in another session to capture
1004 * tuner video. Closing this will result in no video to the encoder.
1010 static int video_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1012 struct cx23885_fh
*fh
= file
->private_data
;
1014 return videobuf_mmap_mapper(get_queue(fh
), vma
);
1017 /* ------------------------------------------------------------------ */
1018 /* VIDEO CTRL IOCTLS */
1020 int cx23885_get_control(struct cx23885_dev
*dev
,
1021 struct v4l2_control
*ctl
)
1023 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__
);
1024 call_all(dev
, core
, g_ctrl
, ctl
);
1028 int cx23885_set_control(struct cx23885_dev
*dev
,
1029 struct v4l2_control
*ctl
)
1031 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)\n", __func__
);
1032 call_all(dev
, core
, s_ctrl
, ctl
);
1037 static void init_controls(struct cx23885_dev
*dev
)
1039 struct v4l2_control ctrl
;
1042 for (i
= 0; i
< CX23885_CTLS
; i
++) {
1043 ctrl
.id
= cx23885_ctls
[i
].v
.id
;
1044 ctrl
.value
= cx23885_ctls
[i
].v
.default_value
;
1046 cx23885_set_control(dev
, &ctrl
);
1050 /* ------------------------------------------------------------------ */
1053 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
1054 struct v4l2_format
*f
)
1056 struct cx23885_fh
*fh
= priv
;
1058 f
->fmt
.pix
.width
= fh
->width
;
1059 f
->fmt
.pix
.height
= fh
->height
;
1060 f
->fmt
.pix
.field
= fh
->vidq
.field
;
1061 f
->fmt
.pix
.pixelformat
= fh
->fmt
->fourcc
;
1062 f
->fmt
.pix
.bytesperline
=
1063 (f
->fmt
.pix
.width
* fh
->fmt
->depth
) >> 3;
1064 f
->fmt
.pix
.sizeimage
=
1065 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1070 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
1071 struct v4l2_format
*f
)
1073 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1074 struct cx23885_fmt
*fmt
;
1075 enum v4l2_field field
;
1076 unsigned int maxw
, maxh
;
1078 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1082 field
= f
->fmt
.pix
.field
;
1083 maxw
= norm_maxw(dev
->tvnorm
);
1084 maxh
= norm_maxh(dev
->tvnorm
);
1086 if (V4L2_FIELD_ANY
== field
) {
1087 field
= (f
->fmt
.pix
.height
> maxh
/2)
1088 ? V4L2_FIELD_INTERLACED
1089 : V4L2_FIELD_BOTTOM
;
1093 case V4L2_FIELD_TOP
:
1094 case V4L2_FIELD_BOTTOM
:
1097 case V4L2_FIELD_INTERLACED
:
1103 f
->fmt
.pix
.field
= field
;
1104 v4l_bound_align_image(&f
->fmt
.pix
.width
, 48, maxw
, 2,
1105 &f
->fmt
.pix
.height
, 32, maxh
, 0, 0);
1106 f
->fmt
.pix
.bytesperline
=
1107 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
1108 f
->fmt
.pix
.sizeimage
=
1109 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
1114 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
1115 struct v4l2_format
*f
)
1117 struct cx23885_fh
*fh
= priv
;
1118 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1119 struct v4l2_mbus_framefmt mbus_fmt
;
1122 dprintk(2, "%s()\n", __func__
);
1123 err
= vidioc_try_fmt_vid_cap(file
, priv
, f
);
1127 fh
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
1128 fh
->width
= f
->fmt
.pix
.width
;
1129 fh
->height
= f
->fmt
.pix
.height
;
1130 fh
->vidq
.field
= f
->fmt
.pix
.field
;
1131 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__
,
1132 fh
->width
, fh
->height
, fh
->vidq
.field
);
1133 v4l2_fill_mbus_format(&mbus_fmt
, &f
->fmt
.pix
, V4L2_MBUS_FMT_FIXED
);
1134 call_all(dev
, video
, s_mbus_fmt
, &mbus_fmt
);
1135 v4l2_fill_pix_format(&f
->fmt
.pix
, &mbus_fmt
);
1139 static int vidioc_querycap(struct file
*file
, void *priv
,
1140 struct v4l2_capability
*cap
)
1142 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1144 strcpy(cap
->driver
, "cx23885");
1145 strlcpy(cap
->card
, cx23885_boards
[dev
->board
].name
,
1147 sprintf(cap
->bus_info
, "PCIe:%s", pci_name(dev
->pci
));
1149 V4L2_CAP_VIDEO_CAPTURE
|
1150 V4L2_CAP_READWRITE
|
1151 V4L2_CAP_STREAMING
|
1152 V4L2_CAP_VBI_CAPTURE
;
1153 if (UNSET
!= dev
->tuner_type
)
1154 cap
->capabilities
|= V4L2_CAP_TUNER
;
1158 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
1159 struct v4l2_fmtdesc
*f
)
1161 if (unlikely(f
->index
>= ARRAY_SIZE(formats
)))
1164 strlcpy(f
->description
, formats
[f
->index
].name
,
1165 sizeof(f
->description
));
1166 f
->pixelformat
= formats
[f
->index
].fourcc
;
1171 static int vidioc_reqbufs(struct file
*file
, void *priv
,
1172 struct v4l2_requestbuffers
*p
)
1174 struct cx23885_fh
*fh
= priv
;
1175 return videobuf_reqbufs(get_queue(fh
), p
);
1178 static int vidioc_querybuf(struct file
*file
, void *priv
,
1179 struct v4l2_buffer
*p
)
1181 struct cx23885_fh
*fh
= priv
;
1182 return videobuf_querybuf(get_queue(fh
), p
);
1185 static int vidioc_qbuf(struct file
*file
, void *priv
,
1186 struct v4l2_buffer
*p
)
1188 struct cx23885_fh
*fh
= priv
;
1189 return videobuf_qbuf(get_queue(fh
), p
);
1192 static int vidioc_dqbuf(struct file
*file
, void *priv
,
1193 struct v4l2_buffer
*p
)
1195 struct cx23885_fh
*fh
= priv
;
1196 return videobuf_dqbuf(get_queue(fh
), p
,
1197 file
->f_flags
& O_NONBLOCK
);
1200 static int vidioc_streamon(struct file
*file
, void *priv
,
1201 enum v4l2_buf_type i
)
1203 struct cx23885_fh
*fh
= priv
;
1204 struct cx23885_dev
*dev
= fh
->dev
;
1205 dprintk(1, "%s()\n", __func__
);
1207 if ((fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) &&
1208 (fh
->type
!= V4L2_BUF_TYPE_VBI_CAPTURE
))
1210 if (unlikely(i
!= fh
->type
))
1213 if (unlikely(!res_get(dev
, fh
, get_resource(fh
))))
1216 /* Don't start VBI streaming unless vida streaming
1217 * has already started.
1219 if ((fh
->type
== V4L2_BUF_TYPE_VBI_CAPTURE
) &&
1220 ((cx_read(VID_A_DMA_CTL
) & 0x11) == 0))
1223 return videobuf_streamon(get_queue(fh
));
1226 static int vidioc_streamoff(struct file
*file
, void *priv
, enum v4l2_buf_type i
)
1228 struct cx23885_fh
*fh
= priv
;
1229 struct cx23885_dev
*dev
= fh
->dev
;
1231 dprintk(1, "%s()\n", __func__
);
1233 if ((fh
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) &&
1234 (fh
->type
!= V4L2_BUF_TYPE_VBI_CAPTURE
))
1239 res
= get_resource(fh
);
1240 err
= videobuf_streamoff(get_queue(fh
));
1243 res_free(dev
, fh
, res
);
1247 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
1249 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1250 dprintk(1, "%s()\n", __func__
);
1252 call_all(dev
, core
, g_std
, id
);
1257 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id
*tvnorms
)
1259 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1260 dprintk(1, "%s()\n", __func__
);
1262 mutex_lock(&dev
->lock
);
1263 cx23885_set_tvnorm(dev
, *tvnorms
);
1264 mutex_unlock(&dev
->lock
);
1269 int cx23885_enum_input(struct cx23885_dev
*dev
, struct v4l2_input
*i
)
1271 static const char *iname
[] = {
1272 [CX23885_VMUX_COMPOSITE1
] = "Composite1",
1273 [CX23885_VMUX_COMPOSITE2
] = "Composite2",
1274 [CX23885_VMUX_COMPOSITE3
] = "Composite3",
1275 [CX23885_VMUX_COMPOSITE4
] = "Composite4",
1276 [CX23885_VMUX_SVIDEO
] = "S-Video",
1277 [CX23885_VMUX_COMPONENT
] = "Component",
1278 [CX23885_VMUX_TELEVISION
] = "Television",
1279 [CX23885_VMUX_CABLE
] = "Cable TV",
1280 [CX23885_VMUX_DVB
] = "DVB",
1281 [CX23885_VMUX_DEBUG
] = "for debug only",
1284 dprintk(1, "%s()\n", __func__
);
1287 if (n
>= MAX_CX23885_INPUT
)
1290 if (0 == INPUT(n
)->type
)
1294 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
1295 strcpy(i
->name
, iname
[INPUT(n
)->type
]);
1296 if ((CX23885_VMUX_TELEVISION
== INPUT(n
)->type
) ||
1297 (CX23885_VMUX_CABLE
== INPUT(n
)->type
)) {
1298 i
->type
= V4L2_INPUT_TYPE_TUNER
;
1299 i
->std
= CX23885_NORMS
;
1302 /* Two selectable audio inputs for non-tv inputs */
1303 if (INPUT(n
)->type
!= CX23885_VMUX_TELEVISION
)
1306 if (dev
->input
== n
) {
1307 /* enum'd input matches our configured input.
1308 * Ask the video decoder to process the call
1309 * and give it an oppertunity to update the
1312 call_all(dev
, video
, g_input_status
, &i
->status
);
1318 static int vidioc_enum_input(struct file
*file
, void *priv
,
1319 struct v4l2_input
*i
)
1321 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1322 dprintk(1, "%s()\n", __func__
);
1323 return cx23885_enum_input(dev
, i
);
1326 int cx23885_get_input(struct file
*file
, void *priv
, unsigned int *i
)
1328 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1331 dprintk(1, "%s() returns %d\n", __func__
, *i
);
1335 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
1337 return cx23885_get_input(file
, priv
, i
);
1340 int cx23885_set_input(struct file
*file
, void *priv
, unsigned int i
)
1342 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1344 dprintk(1, "%s(%d)\n", __func__
, i
);
1346 if (i
>= MAX_CX23885_INPUT
) {
1347 dprintk(1, "%s() -EINVAL\n", __func__
);
1351 if (INPUT(i
)->type
== 0)
1354 mutex_lock(&dev
->lock
);
1355 cx23885_video_mux(dev
, i
);
1357 /* By default establish the default audio input for the card also */
1358 /* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
1359 cx23885_audio_mux(dev
, i
);
1360 mutex_unlock(&dev
->lock
);
1364 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
1366 return cx23885_set_input(file
, priv
, i
);
1369 static int vidioc_log_status(struct file
*file
, void *priv
)
1371 struct cx23885_fh
*fh
= priv
;
1372 struct cx23885_dev
*dev
= fh
->dev
;
1375 "%s/0: ============ START LOG STATUS ============\n",
1377 call_all(dev
, core
, log_status
);
1379 "%s/0: ============= END LOG STATUS =============\n",
1384 static int cx23885_query_audinput(struct file
*file
, void *priv
,
1385 struct v4l2_audio
*i
)
1387 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1388 static const char *iname
[] = {
1389 [0] = "Baseband L/R 1",
1390 [1] = "Baseband L/R 2",
1393 dprintk(1, "%s()\n", __func__
);
1399 memset(i
, 0, sizeof(*i
));
1401 strcpy(i
->name
, iname
[n
]);
1402 i
->capability
= V4L2_AUDCAP_STEREO
;
1403 i
->mode
= V4L2_AUDMODE_AVL
;
1408 static int vidioc_enum_audinput(struct file
*file
, void *priv
,
1409 struct v4l2_audio
*i
)
1411 return cx23885_query_audinput(file
, priv
, i
);
1414 static int vidioc_g_audinput(struct file
*file
, void *priv
,
1415 struct v4l2_audio
*i
)
1417 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1419 i
->index
= dev
->audinput
;
1420 dprintk(1, "%s(input=%d)\n", __func__
, i
->index
);
1422 return cx23885_query_audinput(file
, priv
, i
);
1425 static int vidioc_s_audinput(struct file
*file
, void *priv
,
1426 struct v4l2_audio
*i
)
1428 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1432 dprintk(1, "%s(%d)\n", __func__
, i
->index
);
1434 dev
->audinput
= i
->index
;
1436 /* Skip the audio defaults from the cards struct, caller wants
1437 * directly touch the audio mux hardware. */
1438 cx23885_flatiron_mux(dev
, dev
->audinput
+ 1);
1442 static int vidioc_queryctrl(struct file
*file
, void *priv
,
1443 struct v4l2_queryctrl
*qctrl
)
1445 qctrl
->id
= v4l2_ctrl_next(ctrl_classes
, qctrl
->id
);
1446 if (unlikely(qctrl
->id
== 0))
1448 return cx23885_ctrl_query(qctrl
);
1451 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
1452 struct v4l2_control
*ctl
)
1454 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1456 return cx23885_get_control(dev
, ctl
);
1459 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
1460 struct v4l2_control
*ctl
)
1462 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1464 return cx23885_set_control(dev
, ctl
);
1467 static int vidioc_g_tuner(struct file
*file
, void *priv
,
1468 struct v4l2_tuner
*t
)
1470 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1472 if (unlikely(UNSET
== dev
->tuner_type
))
1477 strcpy(t
->name
, "Television");
1479 call_all(dev
, tuner
, g_tuner
, t
);
1483 static int vidioc_s_tuner(struct file
*file
, void *priv
,
1484 struct v4l2_tuner
*t
)
1486 struct cx23885_dev
*dev
= ((struct cx23885_fh
*)priv
)->dev
;
1488 if (UNSET
== dev
->tuner_type
)
1492 /* Update the A/V core */
1493 call_all(dev
, tuner
, s_tuner
, t
);
1498 static int vidioc_g_frequency(struct file
*file
, void *priv
,
1499 struct v4l2_frequency
*f
)
1501 struct cx23885_fh
*fh
= priv
;
1502 struct cx23885_dev
*dev
= fh
->dev
;
1504 if (unlikely(UNSET
== dev
->tuner_type
))
1507 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1508 f
->type
= fh
->radio
? V4L2_TUNER_RADIO
: V4L2_TUNER_ANALOG_TV
;
1509 f
->frequency
= dev
->freq
;
1511 call_all(dev
, tuner
, g_frequency
, f
);
1516 static int cx23885_set_freq(struct cx23885_dev
*dev
, struct v4l2_frequency
*f
)
1518 struct v4l2_control ctrl
;
1520 if (unlikely(UNSET
== dev
->tuner_type
))
1522 if (unlikely(f
->tuner
!= 0))
1525 mutex_lock(&dev
->lock
);
1526 dev
->freq
= f
->frequency
;
1528 /* I need to mute audio here */
1529 ctrl
.id
= V4L2_CID_AUDIO_MUTE
;
1531 cx23885_set_control(dev
, &ctrl
);
1533 call_all(dev
, tuner
, s_frequency
, f
);
1535 /* When changing channels it is required to reset TVAUDIO */
1538 /* I need to unmute audio here */
1540 cx23885_set_control(dev
, &ctrl
);
1542 mutex_unlock(&dev
->lock
);
1547 static int cx23885_set_freq_via_ops(struct cx23885_dev
*dev
,
1548 struct v4l2_frequency
*f
)
1550 struct v4l2_control ctrl
;
1551 struct videobuf_dvb_frontend
*vfe
;
1552 struct dvb_frontend
*fe
;
1554 struct analog_parameters params
= {
1555 .mode
= V4L2_TUNER_ANALOG_TV
,
1556 .audmode
= V4L2_TUNER_MODE_STEREO
,
1558 .frequency
= f
->frequency
1561 mutex_lock(&dev
->lock
);
1562 dev
->freq
= f
->frequency
;
1564 /* I need to mute audio here */
1565 ctrl
.id
= V4L2_CID_AUDIO_MUTE
;
1567 cx23885_set_control(dev
, &ctrl
);
1570 dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__
,
1571 params
.frequency
, f
->tuner
, params
.std
);
1573 vfe
= videobuf_dvb_get_frontend(&dev
->ts2
.frontends
, 1);
1575 mutex_unlock(&dev
->lock
);
1579 fe
= vfe
->dvb
.frontend
;
1581 if (dev
->board
== CX23885_BOARD_HAUPPAUGE_HVR1850
)
1582 fe
= &dev
->ts1
.analog_fe
;
1584 if (fe
&& fe
->ops
.tuner_ops
.set_analog_params
) {
1585 call_all(dev
, core
, s_std
, dev
->tvnorm
);
1586 fe
->ops
.tuner_ops
.set_analog_params(fe
, ¶ms
);
1589 printk(KERN_ERR
"%s() No analog tuner, aborting\n", __func__
);
1591 /* When changing channels it is required to reset TVAUDIO */
1594 /* I need to unmute audio here */
1596 cx23885_set_control(dev
, &ctrl
);
1598 mutex_unlock(&dev
->lock
);
1603 int cx23885_set_frequency(struct file
*file
, void *priv
,
1604 struct v4l2_frequency
*f
)
1606 struct cx23885_fh
*fh
= priv
;
1607 struct cx23885_dev
*dev
= fh
->dev
;
1610 switch (dev
->board
) {
1611 case CX23885_BOARD_HAUPPAUGE_HVR1850
:
1612 ret
= cx23885_set_freq_via_ops(dev
, f
);
1615 ret
= cx23885_set_freq(dev
, f
);
1621 static int vidioc_s_frequency(struct file
*file
, void *priv
,
1622 struct v4l2_frequency
*f
)
1624 return cx23885_set_frequency(file
, priv
, f
);
1627 /* ----------------------------------------------------------- */
1629 static void cx23885_vid_timeout(unsigned long data
)
1631 struct cx23885_dev
*dev
= (struct cx23885_dev
*)data
;
1632 struct cx23885_dmaqueue
*q
= &dev
->vidq
;
1633 struct cx23885_buffer
*buf
;
1634 unsigned long flags
;
1636 spin_lock_irqsave(&dev
->slock
, flags
);
1637 while (!list_empty(&q
->active
)) {
1638 buf
= list_entry(q
->active
.next
,
1639 struct cx23885_buffer
, vb
.queue
);
1640 list_del(&buf
->vb
.queue
);
1641 buf
->vb
.state
= VIDEOBUF_ERROR
;
1642 wake_up(&buf
->vb
.done
);
1643 printk(KERN_ERR
"%s: [%p/%d] timeout - dma=0x%08lx\n",
1644 dev
->name
, buf
, buf
->vb
.i
,
1645 (unsigned long)buf
->risc
.dma
);
1647 cx23885_restart_video_queue(dev
, q
);
1648 spin_unlock_irqrestore(&dev
->slock
, flags
);
1651 int cx23885_video_irq(struct cx23885_dev
*dev
, u32 status
)
1656 mask
= cx_read(VID_A_INT_MSK
);
1657 if (0 == (status
& mask
))
1660 cx_write(VID_A_INT_STAT
, status
);
1662 /* risc op code error, fifo overflow or line sync detection error */
1663 if ((status
& VID_BC_MSK_OPC_ERR
) ||
1664 (status
& VID_BC_MSK_SYNC
) ||
1665 (status
& VID_BC_MSK_OF
)) {
1667 if (status
& VID_BC_MSK_OPC_ERR
) {
1668 dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1669 VID_BC_MSK_OPC_ERR
);
1670 printk(KERN_WARNING
"%s: video risc op code error\n",
1672 cx23885_sram_channel_dump(dev
,
1673 &dev
->sram_channels
[SRAM_CH01
]);
1676 if (status
& VID_BC_MSK_SYNC
)
1677 dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) "
1678 "video lines miss-match\n",
1681 if (status
& VID_BC_MSK_OF
)
1682 dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
1688 if (status
& VID_BC_MSK_RISCI1
) {
1689 spin_lock(&dev
->slock
);
1690 count
= cx_read(VID_A_GPCNT
);
1691 cx23885_video_wakeup(dev
, &dev
->vidq
, count
);
1692 spin_unlock(&dev
->slock
);
1695 if (status
& VID_BC_MSK_RISCI2
) {
1696 dprintk(2, "stopper video\n");
1697 spin_lock(&dev
->slock
);
1698 cx23885_restart_video_queue(dev
, &dev
->vidq
);
1699 spin_unlock(&dev
->slock
);
1703 /* Allow the VBI framework to process it's payload */
1704 handled
+= cx23885_vbi_irq(dev
, status
);
1709 /* ----------------------------------------------------------- */
1710 /* exported stuff */
1712 static const struct v4l2_file_operations video_fops
= {
1713 .owner
= THIS_MODULE
,
1715 .release
= video_release
,
1719 .ioctl
= video_ioctl2
,
1722 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
1723 .vidioc_querycap
= vidioc_querycap
,
1724 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1725 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1726 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1727 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1728 .vidioc_g_fmt_vbi_cap
= cx23885_vbi_fmt
,
1729 .vidioc_try_fmt_vbi_cap
= cx23885_vbi_fmt
,
1730 .vidioc_s_fmt_vbi_cap
= cx23885_vbi_fmt
,
1731 .vidioc_reqbufs
= vidioc_reqbufs
,
1732 .vidioc_querybuf
= vidioc_querybuf
,
1733 .vidioc_qbuf
= vidioc_qbuf
,
1734 .vidioc_dqbuf
= vidioc_dqbuf
,
1735 .vidioc_s_std
= vidioc_s_std
,
1736 .vidioc_g_std
= vidioc_g_std
,
1737 .vidioc_querystd
= vidioc_g_std
,
1738 .vidioc_enum_input
= vidioc_enum_input
,
1739 .vidioc_g_input
= vidioc_g_input
,
1740 .vidioc_s_input
= vidioc_s_input
,
1741 .vidioc_log_status
= vidioc_log_status
,
1742 .vidioc_queryctrl
= vidioc_queryctrl
,
1743 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1744 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1745 .vidioc_streamon
= vidioc_streamon
,
1746 .vidioc_streamoff
= vidioc_streamoff
,
1747 .vidioc_g_tuner
= vidioc_g_tuner
,
1748 .vidioc_s_tuner
= vidioc_s_tuner
,
1749 .vidioc_g_frequency
= vidioc_g_frequency
,
1750 .vidioc_s_frequency
= vidioc_s_frequency
,
1751 .vidioc_g_chip_ident
= cx23885_g_chip_ident
,
1752 #ifdef CONFIG_VIDEO_ADV_DEBUG
1753 .vidioc_g_register
= cx23885_g_register
,
1754 .vidioc_s_register
= cx23885_s_register
,
1756 .vidioc_enumaudio
= vidioc_enum_audinput
,
1757 .vidioc_g_audio
= vidioc_g_audinput
,
1758 .vidioc_s_audio
= vidioc_s_audinput
,
1761 static struct video_device cx23885_vbi_template
;
1762 static struct video_device cx23885_video_template
= {
1763 .name
= "cx23885-video",
1764 .fops
= &video_fops
,
1765 .ioctl_ops
= &video_ioctl_ops
,
1766 .tvnorms
= CX23885_NORMS
,
1767 .current_norm
= V4L2_STD_NTSC_M
,
1770 static const struct v4l2_file_operations radio_fops
= {
1771 .owner
= THIS_MODULE
,
1773 .release
= video_release
,
1774 .ioctl
= video_ioctl2
,
1778 void cx23885_video_unregister(struct cx23885_dev
*dev
)
1780 dprintk(1, "%s()\n", __func__
);
1781 cx23885_irq_remove(dev
, 0x01);
1784 if (video_is_registered(dev
->vbi_dev
))
1785 video_unregister_device(dev
->vbi_dev
);
1787 video_device_release(dev
->vbi_dev
);
1788 dev
->vbi_dev
= NULL
;
1789 btcx_riscmem_free(dev
->pci
, &dev
->vbiq
.stopper
);
1791 if (dev
->video_dev
) {
1792 if (video_is_registered(dev
->video_dev
))
1793 video_unregister_device(dev
->video_dev
);
1795 video_device_release(dev
->video_dev
);
1796 dev
->video_dev
= NULL
;
1798 btcx_riscmem_free(dev
->pci
, &dev
->vidq
.stopper
);
1802 cx23885_audio_unregister(dev
);
1805 int cx23885_video_register(struct cx23885_dev
*dev
)
1809 dprintk(1, "%s()\n", __func__
);
1810 spin_lock_init(&dev
->slock
);
1812 /* Initialize VBI template */
1813 memcpy(&cx23885_vbi_template
, &cx23885_video_template
,
1814 sizeof(cx23885_vbi_template
));
1815 strcpy(cx23885_vbi_template
.name
, "cx23885-vbi");
1817 dev
->tvnorm
= cx23885_video_template
.current_norm
;
1819 /* init video dma queues */
1820 INIT_LIST_HEAD(&dev
->vidq
.active
);
1821 INIT_LIST_HEAD(&dev
->vidq
.queued
);
1822 dev
->vidq
.timeout
.function
= cx23885_vid_timeout
;
1823 dev
->vidq
.timeout
.data
= (unsigned long)dev
;
1824 init_timer(&dev
->vidq
.timeout
);
1825 cx23885_risc_stopper(dev
->pci
, &dev
->vidq
.stopper
,
1826 VID_A_DMA_CTL
, 0x11, 0x00);
1828 /* init vbi dma queues */
1829 INIT_LIST_HEAD(&dev
->vbiq
.active
);
1830 INIT_LIST_HEAD(&dev
->vbiq
.queued
);
1831 dev
->vbiq
.timeout
.function
= cx23885_vbi_timeout
;
1832 dev
->vbiq
.timeout
.data
= (unsigned long)dev
;
1833 init_timer(&dev
->vbiq
.timeout
);
1834 cx23885_risc_stopper(dev
->pci
, &dev
->vbiq
.stopper
,
1835 VID_A_DMA_CTL
, 0x22, 0x00);
1837 cx23885_irq_add_enable(dev
, 0x01);
1839 if ((TUNER_ABSENT
!= dev
->tuner_type
) &&
1840 ((dev
->tuner_bus
== 0) || (dev
->tuner_bus
== 1))) {
1841 struct v4l2_subdev
*sd
= NULL
;
1843 if (dev
->tuner_addr
)
1844 sd
= v4l2_i2c_new_subdev(&dev
->v4l2_dev
,
1845 &dev
->i2c_bus
[dev
->tuner_bus
].i2c_adap
,
1846 "tuner", dev
->tuner_addr
, NULL
);
1848 sd
= v4l2_i2c_new_subdev(&dev
->v4l2_dev
,
1849 &dev
->i2c_bus
[dev
->tuner_bus
].i2c_adap
,
1850 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV
));
1852 struct tuner_setup tun_setup
;
1854 memset(&tun_setup
, 0, sizeof(tun_setup
));
1855 tun_setup
.mode_mask
= T_ANALOG_TV
;
1856 tun_setup
.type
= dev
->tuner_type
;
1857 tun_setup
.addr
= v4l2_i2c_subdev_addr(sd
);
1858 tun_setup
.tuner_callback
= cx23885_tuner_callback
;
1860 v4l2_subdev_call(sd
, tuner
, s_type_addr
, &tun_setup
);
1862 if (dev
->board
== CX23885_BOARD_LEADTEK_WINFAST_PXTV1200
) {
1863 struct xc2028_ctrl ctrl
= {
1864 .fname
= XC2028_DEFAULT_FIRMWARE
,
1867 struct v4l2_priv_tun_config cfg
= {
1868 .tuner
= dev
->tuner_type
,
1871 v4l2_subdev_call(sd
, tuner
, s_config
, &cfg
);
1876 /* register Video device */
1877 dev
->video_dev
= cx23885_vdev_init(dev
, dev
->pci
,
1878 &cx23885_video_template
, "video");
1879 err
= video_register_device(dev
->video_dev
, VFL_TYPE_GRABBER
,
1882 printk(KERN_INFO
"%s: can't register video device\n",
1886 printk(KERN_INFO
"%s: registered device %s [v4l2]\n",
1887 dev
->name
, video_device_node_name(dev
->video_dev
));
1889 /* register VBI device */
1890 dev
->vbi_dev
= cx23885_vdev_init(dev
, dev
->pci
,
1891 &cx23885_vbi_template
, "vbi");
1892 err
= video_register_device(dev
->vbi_dev
, VFL_TYPE_VBI
,
1895 printk(KERN_INFO
"%s: can't register vbi device\n",
1899 printk(KERN_INFO
"%s: registered device %s\n",
1900 dev
->name
, video_device_node_name(dev
->vbi_dev
));
1902 /* Register ALSA audio device */
1903 dev
->audio_dev
= cx23885_audio_register(dev
);
1905 /* initial device configuration */
1906 mutex_lock(&dev
->lock
);
1907 cx23885_set_tvnorm(dev
, dev
->tvnorm
);
1909 cx23885_video_mux(dev
, 0);
1910 cx23885_audio_mux(dev
, 0);
1911 mutex_unlock(&dev
->lock
);
1916 cx23885_video_unregister(dev
);