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.
18 #include <linux/init.h>
19 #include <linux/list.h>
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/kmod.h>
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
27 #include <linux/kthread.h>
28 #include <asm/div64.h>
31 #include "cx23885-video.h"
32 #include <media/v4l2-common.h>
33 #include <media/v4l2-ioctl.h>
34 #include <media/v4l2-event.h>
35 #include "cx23885-ioctl.h"
36 #include "tuner-xc2028.h"
38 #include <media/cx25840.h>
40 MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
41 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
42 MODULE_LICENSE("GPL");
44 /* ------------------------------------------------------------------ */
46 static unsigned int video_nr
[] = {[0 ... (CX23885_MAXBOARDS
- 1)] = UNSET
};
47 static unsigned int vbi_nr
[] = {[0 ... (CX23885_MAXBOARDS
- 1)] = UNSET
};
49 module_param_array(video_nr
, int, NULL
, 0444);
50 module_param_array(vbi_nr
, int, NULL
, 0444);
52 MODULE_PARM_DESC(video_nr
, "video device numbers");
53 MODULE_PARM_DESC(vbi_nr
, "vbi device numbers");
55 static unsigned int video_debug
;
56 module_param(video_debug
, int, 0644);
57 MODULE_PARM_DESC(video_debug
, "enable debug messages [video]");
59 static unsigned int irq_debug
;
60 module_param(irq_debug
, int, 0644);
61 MODULE_PARM_DESC(irq_debug
, "enable debug messages [IRQ handler]");
63 static unsigned int vid_limit
= 16;
64 module_param(vid_limit
, int, 0644);
65 MODULE_PARM_DESC(vid_limit
, "capture memory limit in megabytes");
67 #define dprintk(level, fmt, arg...)\
68 do { if (video_debug >= level)\
69 printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
72 /* ------------------------------------------------------------------- */
75 #define FORMAT_FLAGS_PACKED 0x01
76 static struct cx23885_fmt formats
[] = {
78 .name
= "4:2:2, packed, YUYV",
79 .fourcc
= V4L2_PIX_FMT_YUYV
,
81 .flags
= FORMAT_FLAGS_PACKED
,
85 static struct cx23885_fmt
*format_by_fourcc(unsigned int fourcc
)
89 for (i
= 0; i
< ARRAY_SIZE(formats
); i
++)
90 if (formats
[i
].fourcc
== fourcc
)
95 /* ------------------------------------------------------------------- */
97 void cx23885_video_wakeup(struct cx23885_dev
*dev
,
98 struct cx23885_dmaqueue
*q
, u32 count
)
100 struct cx23885_buffer
*buf
;
102 if (list_empty(&q
->active
))
104 buf
= list_entry(q
->active
.next
,
105 struct cx23885_buffer
, queue
);
107 buf
->vb
.sequence
= q
->count
++;
108 v4l2_get_timestamp(&buf
->vb
.timestamp
);
109 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf
,
110 buf
->vb
.vb2_buf
.index
, count
, q
->count
);
111 list_del(&buf
->queue
);
112 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_DONE
);
115 int cx23885_set_tvnorm(struct cx23885_dev
*dev
, v4l2_std_id norm
)
117 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
120 v4l2_norm_to_name(norm
));
122 if (dev
->tvnorm
!= norm
) {
123 if (vb2_is_busy(&dev
->vb2_vidq
) || vb2_is_busy(&dev
->vb2_vbiq
) ||
124 vb2_is_busy(&dev
->vb2_mpegq
))
130 call_all(dev
, video
, s_std
, norm
);
135 static struct video_device
*cx23885_vdev_init(struct cx23885_dev
*dev
,
137 struct video_device
*template,
140 struct video_device
*vfd
;
141 dprintk(1, "%s()\n", __func__
);
143 vfd
= video_device_alloc();
147 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
148 vfd
->release
= video_device_release
;
149 vfd
->lock
= &dev
->lock
;
150 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s (%s)",
151 cx23885_boards
[dev
->board
].name
, type
);
152 video_set_drvdata(vfd
, dev
);
156 int cx23885_flatiron_write(struct cx23885_dev
*dev
, u8 reg
, u8 data
)
158 /* 8 bit registers, 8 bit values */
159 u8 buf
[] = { reg
, data
};
161 struct i2c_msg msg
= { .addr
= 0x98 >> 1,
162 .flags
= 0, .buf
= buf
, .len
= 2 };
164 return i2c_transfer(&dev
->i2c_bus
[2].i2c_adap
, &msg
, 1);
167 u8
cx23885_flatiron_read(struct cx23885_dev
*dev
, u8 reg
)
169 /* 8 bit registers, 8 bit values */
174 struct i2c_msg msg
[] = {
175 { .addr
= 0x98 >> 1, .flags
= 0, .buf
= b0
, .len
= 1 },
176 { .addr
= 0x98 >> 1, .flags
= I2C_M_RD
, .buf
= b1
, .len
= 1 }
179 ret
= i2c_transfer(&dev
->i2c_bus
[2].i2c_adap
, &msg
[0], 2);
181 printk(KERN_ERR
"%s() error\n", __func__
);
186 static void cx23885_flatiron_dump(struct cx23885_dev
*dev
)
189 dprintk(1, "Flatiron dump\n");
190 for (i
= 0; i
< 0x24; i
++) {
191 dprintk(1, "FI[%02x] = %02x\n", i
,
192 cx23885_flatiron_read(dev
, i
));
196 static int cx23885_flatiron_mux(struct cx23885_dev
*dev
, int input
)
199 dprintk(1, "%s(input = %d)\n", __func__
, input
);
202 val
= cx23885_flatiron_read(dev
, CH_PWR_CTRL1
) & ~FLD_CH_SEL
;
204 val
= cx23885_flatiron_read(dev
, CH_PWR_CTRL1
) | FLD_CH_SEL
;
208 val
|= 0x20; /* Enable clock to delta-sigma and dec filter */
210 cx23885_flatiron_write(dev
, CH_PWR_CTRL1
, val
);
213 cx23885_flatiron_write(dev
, CH_PWR_CTRL2
, 0);
216 cx23885_flatiron_dump(dev
);
221 static int cx23885_video_mux(struct cx23885_dev
*dev
, unsigned int input
)
223 dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
225 input
, INPUT(input
)->vmux
,
226 INPUT(input
)->gpio0
, INPUT(input
)->gpio1
,
227 INPUT(input
)->gpio2
, INPUT(input
)->gpio3
);
230 if (dev
->board
== CX23885_BOARD_MYGICA_X8506
||
231 dev
->board
== CX23885_BOARD_MAGICPRO_PROHDTVE2
||
232 dev
->board
== CX23885_BOARD_MYGICA_X8507
) {
233 /* Select Analog TV */
234 if (INPUT(input
)->type
== CX23885_VMUX_TELEVISION
)
235 cx23885_gpio_clear(dev
, GPIO_0
);
238 /* Tell the internal A/V decoder */
239 v4l2_subdev_call(dev
->sd_cx25840
, video
, s_routing
,
240 INPUT(input
)->vmux
, 0, 0);
242 if ((dev
->board
== CX23885_BOARD_HAUPPAUGE_HVR1800
) ||
243 (dev
->board
== CX23885_BOARD_MPX885
) ||
244 (dev
->board
== CX23885_BOARD_HAUPPAUGE_HVR1250
) ||
245 (dev
->board
== CX23885_BOARD_HAUPPAUGE_IMPACTVCBE
) ||
246 (dev
->board
== CX23885_BOARD_HAUPPAUGE_HVR1255
) ||
247 (dev
->board
== CX23885_BOARD_HAUPPAUGE_HVR1255_22111
) ||
248 (dev
->board
== CX23885_BOARD_HAUPPAUGE_HVR1850
) ||
249 (dev
->board
== CX23885_BOARD_MYGICA_X8507
) ||
250 (dev
->board
== CX23885_BOARD_AVERMEDIA_HC81R
)) {
251 /* Configure audio routing */
252 v4l2_subdev_call(dev
->sd_cx25840
, audio
, s_routing
,
253 INPUT(input
)->amux
, 0, 0);
255 if (INPUT(input
)->amux
== CX25840_AUDIO7
)
256 cx23885_flatiron_mux(dev
, 1);
257 else if (INPUT(input
)->amux
== CX25840_AUDIO6
)
258 cx23885_flatiron_mux(dev
, 2);
264 static int cx23885_audio_mux(struct cx23885_dev
*dev
, unsigned int input
)
266 dprintk(1, "%s(input=%d)\n", __func__
, input
);
268 /* The baseband video core of the cx23885 has two audio inputs.
269 * LR1 and LR2. In almost every single case so far only HVR1xxx
270 * cards we've only ever supported LR1. Time to support LR2,
271 * which is available via the optional white breakout header on
273 * We'll use a could of existing enums in the card struct to allow
274 * devs to specify which baseband input they need, or just default
275 * to what we've always used.
277 if (INPUT(input
)->amux
== CX25840_AUDIO7
)
278 cx23885_flatiron_mux(dev
, 1);
279 else if (INPUT(input
)->amux
== CX25840_AUDIO6
)
280 cx23885_flatiron_mux(dev
, 2);
282 /* Not specifically defined, assume the default. */
283 cx23885_flatiron_mux(dev
, 1);
289 /* ------------------------------------------------------------------ */
290 static int cx23885_start_video_dma(struct cx23885_dev
*dev
,
291 struct cx23885_dmaqueue
*q
,
292 struct cx23885_buffer
*buf
)
294 dprintk(1, "%s()\n", __func__
);
296 /* Stop the dma/fifo before we tamper with it's risc programs */
297 cx_clear(VID_A_DMA_CTL
, 0x11);
299 /* setup fifo + format */
300 cx23885_sram_channel_setup(dev
, &dev
->sram_channels
[SRAM_CH01
],
301 buf
->bpl
, buf
->risc
.dma
);
304 cx_write(VID_A_GPCNT_CTL
, 3);
308 cx23885_irq_add_enable(dev
, 0x01);
309 cx_set(VID_A_INT_MSK
, 0x000011);
312 cx_set(DEV_CNTRL2
, (1<<5));
313 cx_set(VID_A_DMA_CTL
, 0x11); /* FIFO and RISC enable */
318 static int queue_setup(struct vb2_queue
*q
, const void *parg
,
319 unsigned int *num_buffers
, unsigned int *num_planes
,
320 unsigned int sizes
[], void *alloc_ctxs
[])
322 struct cx23885_dev
*dev
= q
->drv_priv
;
325 sizes
[0] = (dev
->fmt
->depth
* dev
->width
* dev
->height
) >> 3;
326 alloc_ctxs
[0] = dev
->alloc_ctx
;
330 static int buffer_prepare(struct vb2_buffer
*vb
)
332 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
333 struct cx23885_dev
*dev
= vb
->vb2_queue
->drv_priv
;
334 struct cx23885_buffer
*buf
=
335 container_of(vbuf
, struct cx23885_buffer
, vb
);
336 u32 line0_offset
, line1_offset
;
337 struct sg_table
*sgt
= vb2_dma_sg_plane_desc(vb
, 0);
340 buf
->bpl
= (dev
->width
* dev
->fmt
->depth
) >> 3;
342 if (vb2_plane_size(vb
, 0) < dev
->height
* buf
->bpl
)
344 vb2_set_plane_payload(vb
, 0, dev
->height
* buf
->bpl
);
346 switch (dev
->field
) {
348 cx23885_risc_buffer(dev
->pci
, &buf
->risc
,
350 buf
->bpl
, 0, dev
->height
);
352 case V4L2_FIELD_BOTTOM
:
353 cx23885_risc_buffer(dev
->pci
, &buf
->risc
,
355 buf
->bpl
, 0, dev
->height
);
357 case V4L2_FIELD_INTERLACED
:
358 if (dev
->tvnorm
& V4L2_STD_525_60
)
364 if (cx23885_boards
[dev
->board
].force_bff
)
365 /* PAL / SECAM OR 888 in NTSC MODE */
369 /* cx25840 transmits NTSC bottom field first */
370 dprintk(1, "%s() Creating TFF/NTSC risc\n",
372 line0_offset
= buf
->bpl
;
375 /* All other formats are top field first */
376 dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
379 line1_offset
= buf
->bpl
;
381 cx23885_risc_buffer(dev
->pci
, &buf
->risc
,
382 sgt
->sgl
, line0_offset
,
387 case V4L2_FIELD_SEQ_TB
:
388 cx23885_risc_buffer(dev
->pci
, &buf
->risc
,
390 0, buf
->bpl
* (dev
->height
>> 1),
394 case V4L2_FIELD_SEQ_BT
:
395 cx23885_risc_buffer(dev
->pci
, &buf
->risc
,
397 buf
->bpl
* (dev
->height
>> 1), 0,
404 dprintk(2, "[%p/%d] buffer_init - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
405 buf
, buf
->vb
.vb2_buf
.index
,
406 dev
->width
, dev
->height
, dev
->fmt
->depth
, dev
->fmt
->name
,
407 (unsigned long)buf
->risc
.dma
);
411 static void buffer_finish(struct vb2_buffer
*vb
)
413 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
414 struct cx23885_buffer
*buf
= container_of(vbuf
,
415 struct cx23885_buffer
, vb
);
417 cx23885_free_buffer(vb
->vb2_queue
->drv_priv
, buf
);
421 * The risc program for each buffer works as follows: it starts with a simple
422 * 'JUMP to addr + 12', which is effectively a NOP. Then the code to DMA the
423 * buffer follows and at the end we have a JUMP back to the start + 12 (skipping
426 * This is the risc program of the first buffer to be queued if the active list
427 * is empty and it just keeps DMAing this buffer without generating any
430 * If a new buffer is added then the initial JUMP in the code for that buffer
431 * will generate an interrupt which signals that the previous buffer has been
432 * DMAed successfully and that it can be returned to userspace.
434 * It also sets the final jump of the previous buffer to the start of the new
435 * buffer, thus chaining the new buffer into the DMA chain. This is a single
436 * atomic u32 write, so there is no race condition.
438 * The end-result of all this that you only get an interrupt when a buffer
439 * is ready, so the control flow is very easy.
441 static void buffer_queue(struct vb2_buffer
*vb
)
443 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
444 struct cx23885_dev
*dev
= vb
->vb2_queue
->drv_priv
;
445 struct cx23885_buffer
*buf
= container_of(vbuf
,
446 struct cx23885_buffer
, vb
);
447 struct cx23885_buffer
*prev
;
448 struct cx23885_dmaqueue
*q
= &dev
->vidq
;
451 /* add jump to start */
452 buf
->risc
.cpu
[1] = cpu_to_le32(buf
->risc
.dma
+ 12);
453 buf
->risc
.jmp
[0] = cpu_to_le32(RISC_JUMP
| RISC_CNT_INC
);
454 buf
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
+ 12);
455 buf
->risc
.jmp
[2] = cpu_to_le32(0); /* bits 63-32 */
457 spin_lock_irqsave(&dev
->slock
, flags
);
458 if (list_empty(&q
->active
)) {
459 list_add_tail(&buf
->queue
, &q
->active
);
460 dprintk(2, "[%p/%d] buffer_queue - first active\n",
461 buf
, buf
->vb
.vb2_buf
.index
);
463 buf
->risc
.cpu
[0] |= cpu_to_le32(RISC_IRQ1
);
464 prev
= list_entry(q
->active
.prev
, struct cx23885_buffer
,
466 list_add_tail(&buf
->queue
, &q
->active
);
467 prev
->risc
.jmp
[1] = cpu_to_le32(buf
->risc
.dma
);
468 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
469 buf
, buf
->vb
.vb2_buf
.index
);
471 spin_unlock_irqrestore(&dev
->slock
, flags
);
474 static int cx23885_start_streaming(struct vb2_queue
*q
, unsigned int count
)
476 struct cx23885_dev
*dev
= q
->drv_priv
;
477 struct cx23885_dmaqueue
*dmaq
= &dev
->vidq
;
478 struct cx23885_buffer
*buf
= list_entry(dmaq
->active
.next
,
479 struct cx23885_buffer
, queue
);
481 cx23885_start_video_dma(dev
, dmaq
, buf
);
485 static void cx23885_stop_streaming(struct vb2_queue
*q
)
487 struct cx23885_dev
*dev
= q
->drv_priv
;
488 struct cx23885_dmaqueue
*dmaq
= &dev
->vidq
;
491 cx_clear(VID_A_DMA_CTL
, 0x11);
492 spin_lock_irqsave(&dev
->slock
, flags
);
493 while (!list_empty(&dmaq
->active
)) {
494 struct cx23885_buffer
*buf
= list_entry(dmaq
->active
.next
,
495 struct cx23885_buffer
, queue
);
497 list_del(&buf
->queue
);
498 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
500 spin_unlock_irqrestore(&dev
->slock
, flags
);
503 static struct vb2_ops cx23885_video_qops
= {
504 .queue_setup
= queue_setup
,
505 .buf_prepare
= buffer_prepare
,
506 .buf_finish
= buffer_finish
,
507 .buf_queue
= buffer_queue
,
508 .wait_prepare
= vb2_ops_wait_prepare
,
509 .wait_finish
= vb2_ops_wait_finish
,
510 .start_streaming
= cx23885_start_streaming
,
511 .stop_streaming
= cx23885_stop_streaming
,
514 /* ------------------------------------------------------------------ */
517 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
518 struct v4l2_format
*f
)
520 struct cx23885_dev
*dev
= video_drvdata(file
);
522 f
->fmt
.pix
.width
= dev
->width
;
523 f
->fmt
.pix
.height
= dev
->height
;
524 f
->fmt
.pix
.field
= dev
->field
;
525 f
->fmt
.pix
.pixelformat
= dev
->fmt
->fourcc
;
526 f
->fmt
.pix
.bytesperline
=
527 (f
->fmt
.pix
.width
* dev
->fmt
->depth
) >> 3;
528 f
->fmt
.pix
.sizeimage
=
529 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
530 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
535 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
536 struct v4l2_format
*f
)
538 struct cx23885_dev
*dev
= video_drvdata(file
);
539 struct cx23885_fmt
*fmt
;
540 enum v4l2_field field
;
541 unsigned int maxw
, maxh
;
543 fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
547 field
= f
->fmt
.pix
.field
;
548 maxw
= norm_maxw(dev
->tvnorm
);
549 maxh
= norm_maxh(dev
->tvnorm
);
551 if (V4L2_FIELD_ANY
== field
) {
552 field
= (f
->fmt
.pix
.height
> maxh
/2)
553 ? V4L2_FIELD_INTERLACED
559 case V4L2_FIELD_BOTTOM
:
562 case V4L2_FIELD_INTERLACED
:
563 case V4L2_FIELD_SEQ_TB
:
564 case V4L2_FIELD_SEQ_BT
:
567 field
= V4L2_FIELD_INTERLACED
;
571 f
->fmt
.pix
.field
= field
;
572 v4l_bound_align_image(&f
->fmt
.pix
.width
, 48, maxw
, 2,
573 &f
->fmt
.pix
.height
, 32, maxh
, 0, 0);
574 f
->fmt
.pix
.bytesperline
=
575 (f
->fmt
.pix
.width
* fmt
->depth
) >> 3;
576 f
->fmt
.pix
.sizeimage
=
577 f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
578 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
583 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
584 struct v4l2_format
*f
)
586 struct cx23885_dev
*dev
= video_drvdata(file
);
587 struct v4l2_subdev_format format
= {
588 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
592 dprintk(2, "%s()\n", __func__
);
593 err
= vidioc_try_fmt_vid_cap(file
, priv
, f
);
598 if (vb2_is_busy(&dev
->vb2_vidq
) || vb2_is_busy(&dev
->vb2_vbiq
) ||
599 vb2_is_busy(&dev
->vb2_mpegq
))
602 dev
->fmt
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
603 dev
->width
= f
->fmt
.pix
.width
;
604 dev
->height
= f
->fmt
.pix
.height
;
605 dev
->field
= f
->fmt
.pix
.field
;
606 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__
,
607 dev
->width
, dev
->height
, dev
->field
);
608 v4l2_fill_mbus_format(&format
.format
, &f
->fmt
.pix
, MEDIA_BUS_FMT_FIXED
);
609 call_all(dev
, pad
, set_fmt
, NULL
, &format
);
610 v4l2_fill_pix_format(&f
->fmt
.pix
, &format
.format
);
611 /* set_fmt overwrites f->fmt.pix.field, restore it */
612 f
->fmt
.pix
.field
= dev
->field
;
616 static int vidioc_querycap(struct file
*file
, void *priv
,
617 struct v4l2_capability
*cap
)
619 struct cx23885_dev
*dev
= video_drvdata(file
);
620 struct video_device
*vdev
= video_devdata(file
);
622 strcpy(cap
->driver
, "cx23885");
623 strlcpy(cap
->card
, cx23885_boards
[dev
->board
].name
,
625 sprintf(cap
->bus_info
, "PCIe:%s", pci_name(dev
->pci
));
626 cap
->device_caps
= V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
| V4L2_CAP_AUDIO
;
627 if (dev
->tuner_type
!= TUNER_ABSENT
)
628 cap
->device_caps
|= V4L2_CAP_TUNER
;
629 if (vdev
->vfl_type
== VFL_TYPE_VBI
)
630 cap
->device_caps
|= V4L2_CAP_VBI_CAPTURE
;
632 cap
->device_caps
|= V4L2_CAP_VIDEO_CAPTURE
;
633 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_VBI_CAPTURE
|
634 V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_DEVICE_CAPS
;
638 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
639 struct v4l2_fmtdesc
*f
)
641 if (unlikely(f
->index
>= ARRAY_SIZE(formats
)))
644 strlcpy(f
->description
, formats
[f
->index
].name
,
645 sizeof(f
->description
));
646 f
->pixelformat
= formats
[f
->index
].fourcc
;
651 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
653 struct cx23885_dev
*dev
= video_drvdata(file
);
654 dprintk(1, "%s()\n", __func__
);
660 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id tvnorms
)
662 struct cx23885_dev
*dev
= video_drvdata(file
);
663 dprintk(1, "%s()\n", __func__
);
665 return cx23885_set_tvnorm(dev
, tvnorms
);
668 int cx23885_enum_input(struct cx23885_dev
*dev
, struct v4l2_input
*i
)
670 static const char *iname
[] = {
671 [CX23885_VMUX_COMPOSITE1
] = "Composite1",
672 [CX23885_VMUX_COMPOSITE2
] = "Composite2",
673 [CX23885_VMUX_COMPOSITE3
] = "Composite3",
674 [CX23885_VMUX_COMPOSITE4
] = "Composite4",
675 [CX23885_VMUX_SVIDEO
] = "S-Video",
676 [CX23885_VMUX_COMPONENT
] = "Component",
677 [CX23885_VMUX_TELEVISION
] = "Television",
678 [CX23885_VMUX_CABLE
] = "Cable TV",
679 [CX23885_VMUX_DVB
] = "DVB",
680 [CX23885_VMUX_DEBUG
] = "for debug only",
683 dprintk(1, "%s()\n", __func__
);
686 if (n
>= MAX_CX23885_INPUT
)
689 if (0 == INPUT(n
)->type
)
693 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
694 strcpy(i
->name
, iname
[INPUT(n
)->type
]);
695 i
->std
= CX23885_NORMS
;
696 if ((CX23885_VMUX_TELEVISION
== INPUT(n
)->type
) ||
697 (CX23885_VMUX_CABLE
== INPUT(n
)->type
)) {
698 i
->type
= V4L2_INPUT_TYPE_TUNER
;
701 /* Two selectable audio inputs for non-tv inputs */
705 if (dev
->input
== n
) {
706 /* enum'd input matches our configured input.
707 * Ask the video decoder to process the call
708 * and give it an oppertunity to update the
711 call_all(dev
, video
, g_input_status
, &i
->status
);
717 static int vidioc_enum_input(struct file
*file
, void *priv
,
718 struct v4l2_input
*i
)
720 struct cx23885_dev
*dev
= video_drvdata(file
);
721 dprintk(1, "%s()\n", __func__
);
722 return cx23885_enum_input(dev
, i
);
725 int cx23885_get_input(struct file
*file
, void *priv
, unsigned int *i
)
727 struct cx23885_dev
*dev
= video_drvdata(file
);
730 dprintk(1, "%s() returns %d\n", __func__
, *i
);
734 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
736 return cx23885_get_input(file
, priv
, i
);
739 int cx23885_set_input(struct file
*file
, void *priv
, unsigned int i
)
741 struct cx23885_dev
*dev
= video_drvdata(file
);
743 dprintk(1, "%s(%d)\n", __func__
, i
);
745 if (i
>= MAX_CX23885_INPUT
) {
746 dprintk(1, "%s() -EINVAL\n", __func__
);
750 if (INPUT(i
)->type
== 0)
753 cx23885_video_mux(dev
, i
);
755 /* By default establish the default audio input for the card also */
756 /* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
757 cx23885_audio_mux(dev
, i
);
761 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
763 return cx23885_set_input(file
, priv
, i
);
766 static int vidioc_log_status(struct file
*file
, void *priv
)
768 struct cx23885_dev
*dev
= video_drvdata(file
);
770 call_all(dev
, core
, log_status
);
774 static int cx23885_query_audinput(struct file
*file
, void *priv
,
775 struct v4l2_audio
*i
)
777 struct cx23885_dev
*dev
= video_drvdata(file
);
778 static const char *iname
[] = {
779 [0] = "Baseband L/R 1",
780 [1] = "Baseband L/R 2",
784 dprintk(1, "%s()\n", __func__
);
790 memset(i
, 0, sizeof(*i
));
792 strcpy(i
->name
, iname
[n
]);
793 i
->capability
= V4L2_AUDCAP_STEREO
;
798 static int vidioc_enum_audinput(struct file
*file
, void *priv
,
799 struct v4l2_audio
*i
)
801 return cx23885_query_audinput(file
, priv
, i
);
804 static int vidioc_g_audinput(struct file
*file
, void *priv
,
805 struct v4l2_audio
*i
)
807 struct cx23885_dev
*dev
= video_drvdata(file
);
809 if ((CX23885_VMUX_TELEVISION
== INPUT(dev
->input
)->type
) ||
810 (CX23885_VMUX_CABLE
== INPUT(dev
->input
)->type
))
813 i
->index
= dev
->audinput
;
814 dprintk(1, "%s(input=%d)\n", __func__
, i
->index
);
816 return cx23885_query_audinput(file
, priv
, i
);
819 static int vidioc_s_audinput(struct file
*file
, void *priv
,
820 const struct v4l2_audio
*i
)
822 struct cx23885_dev
*dev
= video_drvdata(file
);
824 if ((CX23885_VMUX_TELEVISION
== INPUT(dev
->input
)->type
) ||
825 (CX23885_VMUX_CABLE
== INPUT(dev
->input
)->type
)) {
826 return i
->index
!= 2 ? -EINVAL
: 0;
831 dprintk(1, "%s(%d)\n", __func__
, i
->index
);
833 dev
->audinput
= i
->index
;
835 /* Skip the audio defaults from the cards struct, caller wants
836 * directly touch the audio mux hardware. */
837 cx23885_flatiron_mux(dev
, dev
->audinput
+ 1);
841 static int vidioc_g_tuner(struct file
*file
, void *priv
,
842 struct v4l2_tuner
*t
)
844 struct cx23885_dev
*dev
= video_drvdata(file
);
846 if (dev
->tuner_type
== TUNER_ABSENT
)
851 strcpy(t
->name
, "Television");
853 call_all(dev
, tuner
, g_tuner
, t
);
857 static int vidioc_s_tuner(struct file
*file
, void *priv
,
858 const struct v4l2_tuner
*t
)
860 struct cx23885_dev
*dev
= video_drvdata(file
);
862 if (dev
->tuner_type
== TUNER_ABSENT
)
866 /* Update the A/V core */
867 call_all(dev
, tuner
, s_tuner
, t
);
872 static int vidioc_g_frequency(struct file
*file
, void *priv
,
873 struct v4l2_frequency
*f
)
875 struct cx23885_dev
*dev
= video_drvdata(file
);
877 if (dev
->tuner_type
== TUNER_ABSENT
)
880 f
->type
= V4L2_TUNER_ANALOG_TV
;
881 f
->frequency
= dev
->freq
;
883 call_all(dev
, tuner
, g_frequency
, f
);
888 static int cx23885_set_freq(struct cx23885_dev
*dev
, const struct v4l2_frequency
*f
)
890 struct v4l2_ctrl
*mute
;
891 int old_mute_val
= 1;
893 if (dev
->tuner_type
== TUNER_ABSENT
)
895 if (unlikely(f
->tuner
!= 0))
898 dev
->freq
= f
->frequency
;
900 /* I need to mute audio here */
901 mute
= v4l2_ctrl_find(&dev
->ctrl_handler
, V4L2_CID_AUDIO_MUTE
);
903 old_mute_val
= v4l2_ctrl_g_ctrl(mute
);
905 v4l2_ctrl_s_ctrl(mute
, 1);
908 call_all(dev
, tuner
, s_frequency
, f
);
910 /* When changing channels it is required to reset TVAUDIO */
913 /* I need to unmute audio here */
914 if (old_mute_val
== 0)
915 v4l2_ctrl_s_ctrl(mute
, old_mute_val
);
920 static int cx23885_set_freq_via_ops(struct cx23885_dev
*dev
,
921 const struct v4l2_frequency
*f
)
923 struct v4l2_ctrl
*mute
;
924 int old_mute_val
= 1;
925 struct vb2_dvb_frontend
*vfe
;
926 struct dvb_frontend
*fe
;
928 struct analog_parameters params
= {
929 .mode
= V4L2_TUNER_ANALOG_TV
,
930 .audmode
= V4L2_TUNER_MODE_STEREO
,
932 .frequency
= f
->frequency
935 dev
->freq
= f
->frequency
;
937 /* I need to mute audio here */
938 mute
= v4l2_ctrl_find(&dev
->ctrl_handler
, V4L2_CID_AUDIO_MUTE
);
940 old_mute_val
= v4l2_ctrl_g_ctrl(mute
);
942 v4l2_ctrl_s_ctrl(mute
, 1);
946 dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__
,
947 params
.frequency
, f
->tuner
, params
.std
);
949 vfe
= vb2_dvb_get_frontend(&dev
->ts2
.frontends
, 1);
954 fe
= vfe
->dvb
.frontend
;
956 if ((dev
->board
== CX23885_BOARD_HAUPPAUGE_HVR1850
) ||
957 (dev
->board
== CX23885_BOARD_HAUPPAUGE_HVR1255
) ||
958 (dev
->board
== CX23885_BOARD_HAUPPAUGE_HVR1255_22111
))
959 fe
= &dev
->ts1
.analog_fe
;
961 if (fe
&& fe
->ops
.tuner_ops
.set_analog_params
) {
962 call_all(dev
, video
, s_std
, dev
->tvnorm
);
963 fe
->ops
.tuner_ops
.set_analog_params(fe
, ¶ms
);
966 printk(KERN_ERR
"%s() No analog tuner, aborting\n", __func__
);
968 /* When changing channels it is required to reset TVAUDIO */
971 /* I need to unmute audio here */
972 if (old_mute_val
== 0)
973 v4l2_ctrl_s_ctrl(mute
, old_mute_val
);
978 int cx23885_set_frequency(struct file
*file
, void *priv
,
979 const struct v4l2_frequency
*f
)
981 struct cx23885_dev
*dev
= video_drvdata(file
);
984 switch (dev
->board
) {
985 case CX23885_BOARD_HAUPPAUGE_HVR1255
:
986 case CX23885_BOARD_HAUPPAUGE_HVR1255_22111
:
987 case CX23885_BOARD_HAUPPAUGE_HVR1850
:
988 ret
= cx23885_set_freq_via_ops(dev
, f
);
991 ret
= cx23885_set_freq(dev
, f
);
997 static int vidioc_s_frequency(struct file
*file
, void *priv
,
998 const struct v4l2_frequency
*f
)
1000 return cx23885_set_frequency(file
, priv
, f
);
1003 /* ----------------------------------------------------------- */
1005 int cx23885_video_irq(struct cx23885_dev
*dev
, u32 status
)
1010 mask
= cx_read(VID_A_INT_MSK
);
1011 if (0 == (status
& mask
))
1014 cx_write(VID_A_INT_STAT
, status
);
1016 /* risc op code error, fifo overflow or line sync detection error */
1017 if ((status
& VID_BC_MSK_OPC_ERR
) ||
1018 (status
& VID_BC_MSK_SYNC
) ||
1019 (status
& VID_BC_MSK_OF
)) {
1021 if (status
& VID_BC_MSK_OPC_ERR
) {
1022 dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1023 VID_BC_MSK_OPC_ERR
);
1024 printk(KERN_WARNING
"%s: video risc op code error\n",
1026 cx23885_sram_channel_dump(dev
,
1027 &dev
->sram_channels
[SRAM_CH01
]);
1030 if (status
& VID_BC_MSK_SYNC
)
1031 dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) "
1032 "video lines miss-match\n",
1035 if (status
& VID_BC_MSK_OF
)
1036 dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
1042 if (status
& VID_BC_MSK_RISCI1
) {
1043 spin_lock(&dev
->slock
);
1044 count
= cx_read(VID_A_GPCNT
);
1045 cx23885_video_wakeup(dev
, &dev
->vidq
, count
);
1046 spin_unlock(&dev
->slock
);
1050 /* Allow the VBI framework to process it's payload */
1051 handled
+= cx23885_vbi_irq(dev
, status
);
1056 /* ----------------------------------------------------------- */
1057 /* exported stuff */
1059 static const struct v4l2_file_operations video_fops
= {
1060 .owner
= THIS_MODULE
,
1061 .open
= v4l2_fh_open
,
1062 .release
= vb2_fop_release
,
1063 .read
= vb2_fop_read
,
1064 .poll
= vb2_fop_poll
,
1065 .unlocked_ioctl
= video_ioctl2
,
1066 .mmap
= vb2_fop_mmap
,
1069 static const struct v4l2_ioctl_ops video_ioctl_ops
= {
1070 .vidioc_querycap
= vidioc_querycap
,
1071 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1072 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1073 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1074 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1075 .vidioc_g_fmt_vbi_cap
= cx23885_vbi_fmt
,
1076 .vidioc_try_fmt_vbi_cap
= cx23885_vbi_fmt
,
1077 .vidioc_s_fmt_vbi_cap
= cx23885_vbi_fmt
,
1078 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
1079 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
1080 .vidioc_querybuf
= vb2_ioctl_querybuf
,
1081 .vidioc_qbuf
= vb2_ioctl_qbuf
,
1082 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
1083 .vidioc_streamon
= vb2_ioctl_streamon
,
1084 .vidioc_streamoff
= vb2_ioctl_streamoff
,
1085 .vidioc_s_std
= vidioc_s_std
,
1086 .vidioc_g_std
= vidioc_g_std
,
1087 .vidioc_enum_input
= vidioc_enum_input
,
1088 .vidioc_g_input
= vidioc_g_input
,
1089 .vidioc_s_input
= vidioc_s_input
,
1090 .vidioc_log_status
= vidioc_log_status
,
1091 .vidioc_g_tuner
= vidioc_g_tuner
,
1092 .vidioc_s_tuner
= vidioc_s_tuner
,
1093 .vidioc_g_frequency
= vidioc_g_frequency
,
1094 .vidioc_s_frequency
= vidioc_s_frequency
,
1095 #ifdef CONFIG_VIDEO_ADV_DEBUG
1096 .vidioc_g_chip_info
= cx23885_g_chip_info
,
1097 .vidioc_g_register
= cx23885_g_register
,
1098 .vidioc_s_register
= cx23885_s_register
,
1100 .vidioc_enumaudio
= vidioc_enum_audinput
,
1101 .vidioc_g_audio
= vidioc_g_audinput
,
1102 .vidioc_s_audio
= vidioc_s_audinput
,
1103 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1104 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1107 static struct video_device cx23885_vbi_template
;
1108 static struct video_device cx23885_video_template
= {
1109 .name
= "cx23885-video",
1110 .fops
= &video_fops
,
1111 .ioctl_ops
= &video_ioctl_ops
,
1112 .tvnorms
= CX23885_NORMS
,
1115 void cx23885_video_unregister(struct cx23885_dev
*dev
)
1117 dprintk(1, "%s()\n", __func__
);
1118 cx23885_irq_remove(dev
, 0x01);
1121 if (video_is_registered(dev
->vbi_dev
))
1122 video_unregister_device(dev
->vbi_dev
);
1124 video_device_release(dev
->vbi_dev
);
1125 dev
->vbi_dev
= NULL
;
1127 if (dev
->video_dev
) {
1128 if (video_is_registered(dev
->video_dev
))
1129 video_unregister_device(dev
->video_dev
);
1131 video_device_release(dev
->video_dev
);
1132 dev
->video_dev
= NULL
;
1136 cx23885_audio_unregister(dev
);
1139 int cx23885_video_register(struct cx23885_dev
*dev
)
1141 struct vb2_queue
*q
;
1144 dprintk(1, "%s()\n", __func__
);
1146 /* Initialize VBI template */
1147 cx23885_vbi_template
= cx23885_video_template
;
1148 strcpy(cx23885_vbi_template
.name
, "cx23885-vbi");
1150 dev
->tvnorm
= V4L2_STD_NTSC_M
;
1151 dev
->fmt
= format_by_fourcc(V4L2_PIX_FMT_YUYV
);
1152 dev
->field
= V4L2_FIELD_INTERLACED
;
1154 dev
->height
= norm_maxh(dev
->tvnorm
);
1156 /* init video dma queues */
1157 INIT_LIST_HEAD(&dev
->vidq
.active
);
1159 /* init vbi dma queues */
1160 INIT_LIST_HEAD(&dev
->vbiq
.active
);
1162 cx23885_irq_add_enable(dev
, 0x01);
1164 if ((TUNER_ABSENT
!= dev
->tuner_type
) &&
1165 ((dev
->tuner_bus
== 0) || (dev
->tuner_bus
== 1))) {
1166 struct v4l2_subdev
*sd
= NULL
;
1168 if (dev
->tuner_addr
)
1169 sd
= v4l2_i2c_new_subdev(&dev
->v4l2_dev
,
1170 &dev
->i2c_bus
[dev
->tuner_bus
].i2c_adap
,
1171 "tuner", dev
->tuner_addr
, NULL
);
1173 sd
= v4l2_i2c_new_subdev(&dev
->v4l2_dev
,
1174 &dev
->i2c_bus
[dev
->tuner_bus
].i2c_adap
,
1175 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV
));
1177 struct tuner_setup tun_setup
;
1179 memset(&tun_setup
, 0, sizeof(tun_setup
));
1180 tun_setup
.mode_mask
= T_ANALOG_TV
;
1181 tun_setup
.type
= dev
->tuner_type
;
1182 tun_setup
.addr
= v4l2_i2c_subdev_addr(sd
);
1183 tun_setup
.tuner_callback
= cx23885_tuner_callback
;
1185 v4l2_subdev_call(sd
, tuner
, s_type_addr
, &tun_setup
);
1187 if ((dev
->board
== CX23885_BOARD_LEADTEK_WINFAST_PXTV1200
) ||
1188 (dev
->board
== CX23885_BOARD_LEADTEK_WINFAST_PXPVR2200
)) {
1189 struct xc2028_ctrl ctrl
= {
1190 .fname
= XC2028_DEFAULT_FIRMWARE
,
1193 struct v4l2_priv_tun_config cfg
= {
1194 .tuner
= dev
->tuner_type
,
1197 v4l2_subdev_call(sd
, tuner
, s_config
, &cfg
);
1200 if (dev
->board
== CX23885_BOARD_AVERMEDIA_HC81R
) {
1201 struct xc2028_ctrl ctrl
= {
1202 .fname
= "xc3028L-v36.fw",
1205 struct v4l2_priv_tun_config cfg
= {
1206 .tuner
= dev
->tuner_type
,
1209 v4l2_subdev_call(sd
, tuner
, s_config
, &cfg
);
1214 /* initial device configuration */
1215 mutex_lock(&dev
->lock
);
1216 cx23885_set_tvnorm(dev
, dev
->tvnorm
);
1217 cx23885_video_mux(dev
, 0);
1218 cx23885_audio_mux(dev
, 0);
1219 mutex_unlock(&dev
->lock
);
1222 q
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1223 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
| VB2_READ
;
1224 q
->gfp_flags
= GFP_DMA32
;
1225 q
->min_buffers_needed
= 2;
1227 q
->buf_struct_size
= sizeof(struct cx23885_buffer
);
1228 q
->ops
= &cx23885_video_qops
;
1229 q
->mem_ops
= &vb2_dma_sg_memops
;
1230 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1231 q
->lock
= &dev
->lock
;
1233 err
= vb2_queue_init(q
);
1238 q
->type
= V4L2_BUF_TYPE_VBI_CAPTURE
;
1239 q
->io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
| VB2_READ
;
1240 q
->gfp_flags
= GFP_DMA32
;
1241 q
->min_buffers_needed
= 2;
1243 q
->buf_struct_size
= sizeof(struct cx23885_buffer
);
1244 q
->ops
= &cx23885_vbi_qops
;
1245 q
->mem_ops
= &vb2_dma_sg_memops
;
1246 q
->timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1247 q
->lock
= &dev
->lock
;
1249 err
= vb2_queue_init(q
);
1253 /* register Video device */
1254 dev
->video_dev
= cx23885_vdev_init(dev
, dev
->pci
,
1255 &cx23885_video_template
, "video");
1256 dev
->video_dev
->queue
= &dev
->vb2_vidq
;
1257 err
= video_register_device(dev
->video_dev
, VFL_TYPE_GRABBER
,
1260 printk(KERN_INFO
"%s: can't register video device\n",
1264 printk(KERN_INFO
"%s: registered device %s [v4l2]\n",
1265 dev
->name
, video_device_node_name(dev
->video_dev
));
1267 /* register VBI device */
1268 dev
->vbi_dev
= cx23885_vdev_init(dev
, dev
->pci
,
1269 &cx23885_vbi_template
, "vbi");
1270 dev
->vbi_dev
->queue
= &dev
->vb2_vbiq
;
1271 err
= video_register_device(dev
->vbi_dev
, VFL_TYPE_VBI
,
1274 printk(KERN_INFO
"%s: can't register vbi device\n",
1278 printk(KERN_INFO
"%s: registered device %s\n",
1279 dev
->name
, video_device_node_name(dev
->vbi_dev
));
1281 /* Register ALSA audio device */
1282 dev
->audio_dev
= cx23885_audio_register(dev
);
1287 cx23885_video_unregister(dev
);