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.
19 #include "cx23885-video.h"
21 #include <linux/init.h>
22 #include <linux/list.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/kmod.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <linux/delay.h>
30 #include <linux/kthread.h>
31 #include <asm/div64.h>
33 #include <media/v4l2-common.h>
34 #include <media/v4l2-ioctl.h>
35 #include <media/v4l2-event.h>
36 #include "cx23885-ioctl.h"
37 #include "tuner-xc2028.h"
39 #include <media/drv-intf/cx25840.h>
41 MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
42 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
43 MODULE_LICENSE("GPL");
45 /* ------------------------------------------------------------------ */
47 static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
48 static unsigned int vbi_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
50 module_param_array(video_nr, int, NULL, 0444);
51 module_param_array(vbi_nr, int, NULL, 0444);
53 MODULE_PARM_DESC(video_nr, "video device numbers");
54 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
56 static unsigned int video_debug;
57 module_param(video_debug, int, 0644);
58 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
60 static unsigned int irq_debug;
61 module_param(irq_debug, int, 0644);
62 MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
64 static unsigned int vid_limit = 16;
65 module_param(vid_limit, int, 0644);
66 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
68 #define dprintk(level, fmt, arg...)\
69 do { if (video_debug >= level)\
70 printk(KERN_DEBUG pr_fmt("%s: video:" fmt), \
74 /* ------------------------------------------------------------------- */
77 #define FORMAT_FLAGS_PACKED 0x01
78 static struct cx23885_fmt formats[] = {
80 .name = "4:2:2, packed, YUYV",
81 .fourcc = V4L2_PIX_FMT_YUYV,
83 .flags = FORMAT_FLAGS_PACKED,
87 static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
91 for (i = 0; i < ARRAY_SIZE(formats); i++)
92 if (formats[i].fourcc == fourcc)
97 /* ------------------------------------------------------------------- */
99 void cx23885_video_wakeup(struct cx23885_dev *dev,
100 struct cx23885_dmaqueue *q, u32 count)
102 struct cx23885_buffer *buf;
104 if (list_empty(&q->active))
106 buf = list_entry(q->active.next,
107 struct cx23885_buffer, queue);
109 buf->vb.sequence = q->count++;
110 buf->vb.vb2_buf.timestamp = ktime_get_ns();
111 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf,
112 buf->vb.vb2_buf.index, count, q->count);
113 list_del(&buf->queue);
114 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
117 int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
119 struct v4l2_subdev_format format = {
120 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
121 .format.code = MEDIA_BUS_FMT_FIXED,
124 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
127 v4l2_norm_to_name(norm));
129 if (dev->tvnorm == norm)
132 if (dev->tvnorm != norm) {
133 if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
134 vb2_is_busy(&dev->vb2_mpegq))
140 dev->height = norm_maxh(norm);
141 dev->field = V4L2_FIELD_INTERLACED;
143 call_all(dev, video, s_std, norm);
145 format.format.width = dev->width;
146 format.format.height = dev->height;
147 format.format.field = dev->field;
148 call_all(dev, pad, set_fmt, NULL, &format);
153 static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
155 struct video_device *template,
158 struct video_device *vfd;
159 dprintk(1, "%s()\n", __func__);
161 vfd = video_device_alloc();
165 vfd->v4l2_dev = &dev->v4l2_dev;
166 vfd->release = video_device_release;
167 vfd->lock = &dev->lock;
168 snprintf(vfd->name, sizeof(vfd->name), "%s (%s)",
169 cx23885_boards[dev->board].name, type);
170 video_set_drvdata(vfd, dev);
174 int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data)
176 /* 8 bit registers, 8 bit values */
177 u8 buf[] = { reg, data };
179 struct i2c_msg msg = { .addr = 0x98 >> 1,
180 .flags = 0, .buf = buf, .len = 2 };
182 return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
185 u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg)
187 /* 8 bit registers, 8 bit values */
192 struct i2c_msg msg[] = {
193 { .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 },
194 { .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 }
197 ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2);
199 pr_err("%s() error\n", __func__);
204 static void cx23885_flatiron_dump(struct cx23885_dev *dev)
207 dprintk(1, "Flatiron dump\n");
208 for (i = 0; i < 0x24; i++) {
209 dprintk(1, "FI[%02x] = %02x\n", i,
210 cx23885_flatiron_read(dev, i));
214 static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input)
217 dprintk(1, "%s(input = %d)\n", __func__, input);
220 val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL;
222 val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL;
226 val |= 0x20; /* Enable clock to delta-sigma and dec filter */
228 cx23885_flatiron_write(dev, CH_PWR_CTRL1, val);
231 cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0);
234 cx23885_flatiron_dump(dev);
239 static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
241 dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
243 input, INPUT(input)->vmux,
244 INPUT(input)->gpio0, INPUT(input)->gpio1,
245 INPUT(input)->gpio2, INPUT(input)->gpio3);
248 if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
249 dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2 ||
250 dev->board == CX23885_BOARD_MYGICA_X8507) {
251 /* Select Analog TV */
252 if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
253 cx23885_gpio_clear(dev, GPIO_0);
256 /* Tell the internal A/V decoder */
257 v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
258 INPUT(input)->vmux, 0, 0);
260 if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) ||
261 (dev->board == CX23885_BOARD_MPX885) ||
262 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1250) ||
263 (dev->board == CX23885_BOARD_HAUPPAUGE_IMPACTVCBE) ||
264 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
265 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
266 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1265_K4) ||
267 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
268 (dev->board == CX23885_BOARD_MYGICA_X8507) ||
269 (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) ||
270 (dev->board == CX23885_BOARD_VIEWCAST_260E) ||
271 (dev->board == CX23885_BOARD_VIEWCAST_460E)) {
272 /* Configure audio routing */
273 v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
274 INPUT(input)->amux, 0, 0);
276 if (INPUT(input)->amux == CX25840_AUDIO7)
277 cx23885_flatiron_mux(dev, 1);
278 else if (INPUT(input)->amux == CX25840_AUDIO6)
279 cx23885_flatiron_mux(dev, 2);
285 static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
287 dprintk(1, "%s(input=%d)\n", __func__, input);
289 /* The baseband video core of the cx23885 has two audio inputs.
290 * LR1 and LR2. In almost every single case so far only HVR1xxx
291 * cards we've only ever supported LR1. Time to support LR2,
292 * which is available via the optional white breakout header on
294 * We'll use a could of existing enums in the card struct to allow
295 * devs to specify which baseband input they need, or just default
296 * to what we've always used.
298 if (INPUT(input)->amux == CX25840_AUDIO7)
299 cx23885_flatiron_mux(dev, 1);
300 else if (INPUT(input)->amux == CX25840_AUDIO6)
301 cx23885_flatiron_mux(dev, 2);
303 /* Not specifically defined, assume the default. */
304 cx23885_flatiron_mux(dev, 1);
310 /* ------------------------------------------------------------------ */
311 static int cx23885_start_video_dma(struct cx23885_dev *dev,
312 struct cx23885_dmaqueue *q,
313 struct cx23885_buffer *buf)
315 dprintk(1, "%s()\n", __func__);
317 /* Stop the dma/fifo before we tamper with it's risc programs */
318 cx_clear(VID_A_DMA_CTL, 0x11);
320 /* setup fifo + format */
321 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
322 buf->bpl, buf->risc.dma);
325 cx_write(VID_A_GPCNT_CTL, 3);
329 cx23885_irq_add_enable(dev, 0x01);
330 cx_set(VID_A_INT_MSK, 0x000011);
333 cx_set(DEV_CNTRL2, (1<<5));
334 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
339 static int queue_setup(struct vb2_queue *q,
340 unsigned int *num_buffers, unsigned int *num_planes,
341 unsigned int sizes[], struct device *alloc_devs[])
343 struct cx23885_dev *dev = q->drv_priv;
346 sizes[0] = (dev->fmt->depth * dev->width * dev->height) >> 3;
350 static int buffer_prepare(struct vb2_buffer *vb)
352 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
353 struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
354 struct cx23885_buffer *buf =
355 container_of(vbuf, struct cx23885_buffer, vb);
356 u32 line0_offset, line1_offset;
357 struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
360 buf->bpl = (dev->width * dev->fmt->depth) >> 3;
362 if (vb2_plane_size(vb, 0) < dev->height * buf->bpl)
364 vb2_set_plane_payload(vb, 0, dev->height * buf->bpl);
366 switch (dev->field) {
368 cx23885_risc_buffer(dev->pci, &buf->risc,
370 buf->bpl, 0, dev->height);
372 case V4L2_FIELD_BOTTOM:
373 cx23885_risc_buffer(dev->pci, &buf->risc,
375 buf->bpl, 0, dev->height);
377 case V4L2_FIELD_INTERLACED:
378 if (dev->tvnorm & V4L2_STD_525_60)
384 if (cx23885_boards[dev->board].force_bff)
385 /* PAL / SECAM OR 888 in NTSC MODE */
389 /* cx25840 transmits NTSC bottom field first */
390 dprintk(1, "%s() Creating TFF/NTSC risc\n",
392 line0_offset = buf->bpl;
395 /* All other formats are top field first */
396 dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
399 line1_offset = buf->bpl;
401 cx23885_risc_buffer(dev->pci, &buf->risc,
402 sgt->sgl, line0_offset,
407 case V4L2_FIELD_SEQ_TB:
408 cx23885_risc_buffer(dev->pci, &buf->risc,
410 0, buf->bpl * (dev->height >> 1),
414 case V4L2_FIELD_SEQ_BT:
415 cx23885_risc_buffer(dev->pci, &buf->risc,
417 buf->bpl * (dev->height >> 1), 0,
424 dprintk(2, "[%p/%d] buffer_init - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
425 buf, buf->vb.vb2_buf.index,
426 dev->width, dev->height, dev->fmt->depth, dev->fmt->name,
427 (unsigned long)buf->risc.dma);
431 static void buffer_finish(struct vb2_buffer *vb)
433 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
434 struct cx23885_buffer *buf = container_of(vbuf,
435 struct cx23885_buffer, vb);
437 cx23885_free_buffer(vb->vb2_queue->drv_priv, buf);
441 * The risc program for each buffer works as follows: it starts with a simple
442 * 'JUMP to addr + 12', which is effectively a NOP. Then the code to DMA the
443 * buffer follows and at the end we have a JUMP back to the start + 12 (skipping
446 * This is the risc program of the first buffer to be queued if the active list
447 * is empty and it just keeps DMAing this buffer without generating any
450 * If a new buffer is added then the initial JUMP in the code for that buffer
451 * will generate an interrupt which signals that the previous buffer has been
452 * DMAed successfully and that it can be returned to userspace.
454 * It also sets the final jump of the previous buffer to the start of the new
455 * buffer, thus chaining the new buffer into the DMA chain. This is a single
456 * atomic u32 write, so there is no race condition.
458 * The end-result of all this that you only get an interrupt when a buffer
459 * is ready, so the control flow is very easy.
461 static void buffer_queue(struct vb2_buffer *vb)
463 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
464 struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
465 struct cx23885_buffer *buf = container_of(vbuf,
466 struct cx23885_buffer, vb);
467 struct cx23885_buffer *prev;
468 struct cx23885_dmaqueue *q = &dev->vidq;
471 /* add jump to start */
472 buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
473 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
474 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
475 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
477 spin_lock_irqsave(&dev->slock, flags);
478 if (list_empty(&q->active)) {
479 list_add_tail(&buf->queue, &q->active);
480 dprintk(2, "[%p/%d] buffer_queue - first active\n",
481 buf, buf->vb.vb2_buf.index);
483 buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
484 prev = list_entry(q->active.prev, struct cx23885_buffer,
486 list_add_tail(&buf->queue, &q->active);
487 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
488 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
489 buf, buf->vb.vb2_buf.index);
491 spin_unlock_irqrestore(&dev->slock, flags);
494 static int cx23885_start_streaming(struct vb2_queue *q, unsigned int count)
496 struct cx23885_dev *dev = q->drv_priv;
497 struct cx23885_dmaqueue *dmaq = &dev->vidq;
498 struct cx23885_buffer *buf = list_entry(dmaq->active.next,
499 struct cx23885_buffer, queue);
501 cx23885_start_video_dma(dev, dmaq, buf);
505 static void cx23885_stop_streaming(struct vb2_queue *q)
507 struct cx23885_dev *dev = q->drv_priv;
508 struct cx23885_dmaqueue *dmaq = &dev->vidq;
511 cx_clear(VID_A_DMA_CTL, 0x11);
512 spin_lock_irqsave(&dev->slock, flags);
513 while (!list_empty(&dmaq->active)) {
514 struct cx23885_buffer *buf = list_entry(dmaq->active.next,
515 struct cx23885_buffer, queue);
517 list_del(&buf->queue);
518 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
520 spin_unlock_irqrestore(&dev->slock, flags);
523 static const struct vb2_ops cx23885_video_qops = {
524 .queue_setup = queue_setup,
525 .buf_prepare = buffer_prepare,
526 .buf_finish = buffer_finish,
527 .buf_queue = buffer_queue,
528 .wait_prepare = vb2_ops_wait_prepare,
529 .wait_finish = vb2_ops_wait_finish,
530 .start_streaming = cx23885_start_streaming,
531 .stop_streaming = cx23885_stop_streaming,
534 /* ------------------------------------------------------------------ */
537 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
538 struct v4l2_format *f)
540 struct cx23885_dev *dev = video_drvdata(file);
542 f->fmt.pix.width = dev->width;
543 f->fmt.pix.height = dev->height;
544 f->fmt.pix.field = dev->field;
545 f->fmt.pix.pixelformat = dev->fmt->fourcc;
546 f->fmt.pix.bytesperline =
547 (f->fmt.pix.width * dev->fmt->depth) >> 3;
548 f->fmt.pix.sizeimage =
549 f->fmt.pix.height * f->fmt.pix.bytesperline;
550 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
555 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
556 struct v4l2_format *f)
558 struct cx23885_dev *dev = video_drvdata(file);
559 struct cx23885_fmt *fmt;
560 enum v4l2_field field;
561 unsigned int maxw, maxh;
563 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
567 field = f->fmt.pix.field;
569 maxh = norm_maxh(dev->tvnorm);
571 if (V4L2_FIELD_ANY == field) {
572 field = (f->fmt.pix.height > maxh/2)
573 ? V4L2_FIELD_INTERLACED
579 case V4L2_FIELD_BOTTOM:
582 case V4L2_FIELD_INTERLACED:
583 case V4L2_FIELD_SEQ_TB:
584 case V4L2_FIELD_SEQ_BT:
587 field = V4L2_FIELD_INTERLACED;
591 f->fmt.pix.field = field;
592 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
593 &f->fmt.pix.height, 32, maxh, 0, 0);
594 f->fmt.pix.bytesperline =
595 (f->fmt.pix.width * fmt->depth) >> 3;
596 f->fmt.pix.sizeimage =
597 f->fmt.pix.height * f->fmt.pix.bytesperline;
598 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
603 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
604 struct v4l2_format *f)
606 struct cx23885_dev *dev = video_drvdata(file);
607 struct v4l2_subdev_format format = {
608 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
612 dprintk(2, "%s()\n", __func__);
613 err = vidioc_try_fmt_vid_cap(file, priv, f);
618 if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
619 vb2_is_busy(&dev->vb2_mpegq))
622 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
623 dev->width = f->fmt.pix.width;
624 dev->height = f->fmt.pix.height;
625 dev->field = f->fmt.pix.field;
626 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
627 dev->width, dev->height, dev->field);
628 v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
629 call_all(dev, pad, set_fmt, NULL, &format);
630 v4l2_fill_pix_format(&f->fmt.pix, &format.format);
631 /* set_fmt overwrites f->fmt.pix.field, restore it */
632 f->fmt.pix.field = dev->field;
636 static int vidioc_querycap(struct file *file, void *priv,
637 struct v4l2_capability *cap)
639 struct cx23885_dev *dev = video_drvdata(file);
640 struct video_device *vdev = video_devdata(file);
642 strscpy(cap->driver, "cx23885", sizeof(cap->driver));
643 strscpy(cap->card, cx23885_boards[dev->board].name,
645 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
646 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING | V4L2_CAP_AUDIO;
647 if (dev->tuner_type != TUNER_ABSENT)
648 cap->device_caps |= V4L2_CAP_TUNER;
649 if (vdev->vfl_type == VFL_TYPE_VBI)
650 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
652 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
653 cap->capabilities = cap->device_caps | V4L2_CAP_VBI_CAPTURE |
654 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_DEVICE_CAPS;
658 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
659 struct v4l2_fmtdesc *f)
661 if (unlikely(f->index >= ARRAY_SIZE(formats)))
664 strscpy(f->description, formats[f->index].name,
665 sizeof(f->description));
666 f->pixelformat = formats[f->index].fourcc;
671 static int vidioc_g_pixelaspect(struct file *file, void *priv,
672 int type, struct v4l2_fract *f)
674 struct cx23885_dev *dev = video_drvdata(file);
675 bool is_50hz = dev->tvnorm & V4L2_STD_625_50;
677 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
680 f->numerator = is_50hz ? 54 : 11;
681 f->denominator = is_50hz ? 59 : 10;
686 static int vidioc_g_selection(struct file *file, void *fh,
687 struct v4l2_selection *sel)
689 struct cx23885_dev *dev = video_drvdata(file);
691 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
694 switch (sel->target) {
695 case V4L2_SEL_TGT_CROP_BOUNDS:
696 case V4L2_SEL_TGT_CROP_DEFAULT:
700 sel->r.height = norm_maxh(dev->tvnorm);
708 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
710 struct cx23885_dev *dev = video_drvdata(file);
711 dprintk(1, "%s()\n", __func__);
717 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
719 struct cx23885_dev *dev = video_drvdata(file);
720 dprintk(1, "%s()\n", __func__);
722 return cx23885_set_tvnorm(dev, tvnorms);
725 int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
727 static const char *iname[] = {
728 [CX23885_VMUX_COMPOSITE1] = "Composite1",
729 [CX23885_VMUX_COMPOSITE2] = "Composite2",
730 [CX23885_VMUX_COMPOSITE3] = "Composite3",
731 [CX23885_VMUX_COMPOSITE4] = "Composite4",
732 [CX23885_VMUX_SVIDEO] = "S-Video",
733 [CX23885_VMUX_COMPONENT] = "Component",
734 [CX23885_VMUX_TELEVISION] = "Television",
735 [CX23885_VMUX_CABLE] = "Cable TV",
736 [CX23885_VMUX_DVB] = "DVB",
737 [CX23885_VMUX_DEBUG] = "for debug only",
740 dprintk(1, "%s()\n", __func__);
743 if (n >= MAX_CX23885_INPUT)
746 if (0 == INPUT(n)->type)
750 i->type = V4L2_INPUT_TYPE_CAMERA;
751 strscpy(i->name, iname[INPUT(n)->type], sizeof(i->name));
752 i->std = CX23885_NORMS;
753 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
754 (CX23885_VMUX_CABLE == INPUT(n)->type)) {
755 i->type = V4L2_INPUT_TYPE_TUNER;
758 /* Two selectable audio inputs for non-tv inputs */
762 if (dev->input == n) {
763 /* enum'd input matches our configured input.
764 * Ask the video decoder to process the call
765 * and give it an oppertunity to update the
768 call_all(dev, video, g_input_status, &i->status);
774 static int vidioc_enum_input(struct file *file, void *priv,
775 struct v4l2_input *i)
777 struct cx23885_dev *dev = video_drvdata(file);
778 dprintk(1, "%s()\n", __func__);
779 return cx23885_enum_input(dev, i);
782 int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
784 struct cx23885_dev *dev = video_drvdata(file);
787 dprintk(1, "%s() returns %d\n", __func__, *i);
791 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
793 return cx23885_get_input(file, priv, i);
796 int cx23885_set_input(struct file *file, void *priv, unsigned int i)
798 struct cx23885_dev *dev = video_drvdata(file);
800 dprintk(1, "%s(%d)\n", __func__, i);
802 if (i >= MAX_CX23885_INPUT) {
803 dprintk(1, "%s() -EINVAL\n", __func__);
807 if (INPUT(i)->type == 0)
810 cx23885_video_mux(dev, i);
812 /* By default establish the default audio input for the card also */
813 /* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
814 cx23885_audio_mux(dev, i);
818 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
820 return cx23885_set_input(file, priv, i);
823 static int vidioc_log_status(struct file *file, void *priv)
825 struct cx23885_dev *dev = video_drvdata(file);
827 call_all(dev, core, log_status);
831 static int cx23885_query_audinput(struct file *file, void *priv,
832 struct v4l2_audio *i)
834 static const char *iname[] = {
835 [0] = "Baseband L/R 1",
836 [1] = "Baseband L/R 2",
840 dprintk(1, "%s()\n", __func__);
846 memset(i, 0, sizeof(*i));
848 strscpy(i->name, iname[n], sizeof(i->name));
849 i->capability = V4L2_AUDCAP_STEREO;
854 static int vidioc_enum_audinput(struct file *file, void *priv,
855 struct v4l2_audio *i)
857 return cx23885_query_audinput(file, priv, i);
860 static int vidioc_g_audinput(struct file *file, void *priv,
861 struct v4l2_audio *i)
863 struct cx23885_dev *dev = video_drvdata(file);
865 if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
866 (CX23885_VMUX_CABLE == INPUT(dev->input)->type))
869 i->index = dev->audinput;
870 dprintk(1, "%s(input=%d)\n", __func__, i->index);
872 return cx23885_query_audinput(file, priv, i);
875 static int vidioc_s_audinput(struct file *file, void *priv,
876 const struct v4l2_audio *i)
878 struct cx23885_dev *dev = video_drvdata(file);
880 if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
881 (CX23885_VMUX_CABLE == INPUT(dev->input)->type)) {
882 return i->index != 2 ? -EINVAL : 0;
887 dprintk(1, "%s(%d)\n", __func__, i->index);
889 dev->audinput = i->index;
891 /* Skip the audio defaults from the cards struct, caller wants
892 * directly touch the audio mux hardware. */
893 cx23885_flatiron_mux(dev, dev->audinput + 1);
897 static int vidioc_g_tuner(struct file *file, void *priv,
898 struct v4l2_tuner *t)
900 struct cx23885_dev *dev = video_drvdata(file);
902 if (dev->tuner_type == TUNER_ABSENT)
907 strscpy(t->name, "Television", sizeof(t->name));
909 call_all(dev, tuner, g_tuner, t);
913 static int vidioc_s_tuner(struct file *file, void *priv,
914 const struct v4l2_tuner *t)
916 struct cx23885_dev *dev = video_drvdata(file);
918 if (dev->tuner_type == TUNER_ABSENT)
922 /* Update the A/V core */
923 call_all(dev, tuner, s_tuner, t);
928 static int vidioc_g_frequency(struct file *file, void *priv,
929 struct v4l2_frequency *f)
931 struct cx23885_dev *dev = video_drvdata(file);
933 if (dev->tuner_type == TUNER_ABSENT)
936 f->type = V4L2_TUNER_ANALOG_TV;
937 f->frequency = dev->freq;
939 call_all(dev, tuner, g_frequency, f);
944 static int cx23885_set_freq(struct cx23885_dev *dev, const struct v4l2_frequency *f)
946 struct v4l2_ctrl *mute;
947 int old_mute_val = 1;
949 if (dev->tuner_type == TUNER_ABSENT)
951 if (unlikely(f->tuner != 0))
954 dev->freq = f->frequency;
956 /* I need to mute audio here */
957 mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
959 old_mute_val = v4l2_ctrl_g_ctrl(mute);
961 v4l2_ctrl_s_ctrl(mute, 1);
964 call_all(dev, tuner, s_frequency, f);
966 /* When changing channels it is required to reset TVAUDIO */
969 /* I need to unmute audio here */
970 if (old_mute_val == 0)
971 v4l2_ctrl_s_ctrl(mute, old_mute_val);
976 static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
977 const struct v4l2_frequency *f)
979 struct v4l2_ctrl *mute;
980 int old_mute_val = 1;
981 struct vb2_dvb_frontend *vfe;
982 struct dvb_frontend *fe;
984 struct analog_parameters params = {
985 .mode = V4L2_TUNER_ANALOG_TV,
986 .audmode = V4L2_TUNER_MODE_STEREO,
988 .frequency = f->frequency
991 dev->freq = f->frequency;
993 /* I need to mute audio here */
994 mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
996 old_mute_val = v4l2_ctrl_g_ctrl(mute);
998 v4l2_ctrl_s_ctrl(mute, 1);
1002 dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
1003 params.frequency, f->tuner, params.std);
1005 vfe = vb2_dvb_get_frontend(&dev->ts2.frontends, 1);
1010 fe = vfe->dvb.frontend;
1012 if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
1013 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
1014 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
1015 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1265_K4))
1016 fe = &dev->ts1.analog_fe;
1018 if (fe && fe->ops.tuner_ops.set_analog_params) {
1019 call_all(dev, video, s_std, dev->tvnorm);
1020 fe->ops.tuner_ops.set_analog_params(fe, ¶ms);
1023 pr_err("%s() No analog tuner, aborting\n", __func__);
1025 /* When changing channels it is required to reset TVAUDIO */
1028 /* I need to unmute audio here */
1029 if (old_mute_val == 0)
1030 v4l2_ctrl_s_ctrl(mute, old_mute_val);
1035 int cx23885_set_frequency(struct file *file, void *priv,
1036 const struct v4l2_frequency *f)
1038 struct cx23885_dev *dev = video_drvdata(file);
1041 switch (dev->board) {
1042 case CX23885_BOARD_HAUPPAUGE_HVR1255:
1043 case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
1044 case CX23885_BOARD_HAUPPAUGE_HVR1265_K4:
1045 case CX23885_BOARD_HAUPPAUGE_HVR1850:
1046 ret = cx23885_set_freq_via_ops(dev, f);
1049 ret = cx23885_set_freq(dev, f);
1055 static int vidioc_s_frequency(struct file *file, void *priv,
1056 const struct v4l2_frequency *f)
1058 return cx23885_set_frequency(file, priv, f);
1061 /* ----------------------------------------------------------- */
1063 int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1068 mask = cx_read(VID_A_INT_MSK);
1069 if (0 == (status & mask))
1072 cx_write(VID_A_INT_STAT, status);
1074 /* risc op code error, fifo overflow or line sync detection error */
1075 if ((status & VID_BC_MSK_OPC_ERR) ||
1076 (status & VID_BC_MSK_SYNC) ||
1077 (status & VID_BC_MSK_OF)) {
1079 if (status & VID_BC_MSK_OPC_ERR) {
1080 dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1081 VID_BC_MSK_OPC_ERR);
1082 pr_warn("%s: video risc op code error\n",
1084 cx23885_sram_channel_dump(dev,
1085 &dev->sram_channels[SRAM_CH01]);
1088 if (status & VID_BC_MSK_SYNC)
1089 dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) video lines miss-match\n",
1092 if (status & VID_BC_MSK_OF)
1093 dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
1099 if (status & VID_BC_MSK_RISCI1) {
1100 spin_lock(&dev->slock);
1101 count = cx_read(VID_A_GPCNT);
1102 cx23885_video_wakeup(dev, &dev->vidq, count);
1103 spin_unlock(&dev->slock);
1107 /* Allow the VBI framework to process it's payload */
1108 handled += cx23885_vbi_irq(dev, status);
1113 /* ----------------------------------------------------------- */
1114 /* exported stuff */
1116 static const struct v4l2_file_operations video_fops = {
1117 .owner = THIS_MODULE,
1118 .open = v4l2_fh_open,
1119 .release = vb2_fop_release,
1120 .read = vb2_fop_read,
1121 .poll = vb2_fop_poll,
1122 .unlocked_ioctl = video_ioctl2,
1123 .mmap = vb2_fop_mmap,
1126 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1127 .vidioc_querycap = vidioc_querycap,
1128 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1129 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1130 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1131 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1132 .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
1133 .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
1134 .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
1135 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1136 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1137 .vidioc_querybuf = vb2_ioctl_querybuf,
1138 .vidioc_qbuf = vb2_ioctl_qbuf,
1139 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1140 .vidioc_streamon = vb2_ioctl_streamon,
1141 .vidioc_streamoff = vb2_ioctl_streamoff,
1142 .vidioc_g_pixelaspect = vidioc_g_pixelaspect,
1143 .vidioc_g_selection = vidioc_g_selection,
1144 .vidioc_s_std = vidioc_s_std,
1145 .vidioc_g_std = vidioc_g_std,
1146 .vidioc_enum_input = vidioc_enum_input,
1147 .vidioc_g_input = vidioc_g_input,
1148 .vidioc_s_input = vidioc_s_input,
1149 .vidioc_log_status = vidioc_log_status,
1150 .vidioc_g_tuner = vidioc_g_tuner,
1151 .vidioc_s_tuner = vidioc_s_tuner,
1152 .vidioc_g_frequency = vidioc_g_frequency,
1153 .vidioc_s_frequency = vidioc_s_frequency,
1154 #ifdef CONFIG_VIDEO_ADV_DEBUG
1155 .vidioc_g_chip_info = cx23885_g_chip_info,
1156 .vidioc_g_register = cx23885_g_register,
1157 .vidioc_s_register = cx23885_s_register,
1159 .vidioc_enumaudio = vidioc_enum_audinput,
1160 .vidioc_g_audio = vidioc_g_audinput,
1161 .vidioc_s_audio = vidioc_s_audinput,
1162 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1163 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1166 static struct video_device cx23885_vbi_template;
1167 static struct video_device cx23885_video_template = {
1168 .name = "cx23885-video",
1169 .fops = &video_fops,
1170 .ioctl_ops = &video_ioctl_ops,
1171 .tvnorms = CX23885_NORMS,
1174 void cx23885_video_unregister(struct cx23885_dev *dev)
1176 dprintk(1, "%s()\n", __func__);
1177 cx23885_irq_remove(dev, 0x01);
1180 if (video_is_registered(dev->vbi_dev))
1181 video_unregister_device(dev->vbi_dev);
1183 video_device_release(dev->vbi_dev);
1184 dev->vbi_dev = NULL;
1186 if (dev->video_dev) {
1187 if (video_is_registered(dev->video_dev))
1188 video_unregister_device(dev->video_dev);
1190 video_device_release(dev->video_dev);
1191 dev->video_dev = NULL;
1195 cx23885_audio_unregister(dev);
1198 int cx23885_video_register(struct cx23885_dev *dev)
1200 struct vb2_queue *q;
1203 dprintk(1, "%s()\n", __func__);
1205 /* Initialize VBI template */
1206 cx23885_vbi_template = cx23885_video_template;
1207 strscpy(cx23885_vbi_template.name, "cx23885-vbi",
1208 sizeof(cx23885_vbi_template.name));
1210 dev->tvnorm = V4L2_STD_NTSC_M;
1211 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
1212 dev->field = V4L2_FIELD_INTERLACED;
1214 dev->height = norm_maxh(dev->tvnorm);
1216 /* init video dma queues */
1217 INIT_LIST_HEAD(&dev->vidq.active);
1219 /* init vbi dma queues */
1220 INIT_LIST_HEAD(&dev->vbiq.active);
1222 cx23885_irq_add_enable(dev, 0x01);
1224 if ((TUNER_ABSENT != dev->tuner_type) &&
1225 ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
1226 struct v4l2_subdev *sd = NULL;
1228 if (dev->tuner_addr)
1229 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1230 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
1231 "tuner", dev->tuner_addr, NULL);
1233 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1234 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
1235 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
1237 struct tuner_setup tun_setup;
1239 memset(&tun_setup, 0, sizeof(tun_setup));
1240 tun_setup.mode_mask = T_ANALOG_TV;
1241 tun_setup.type = dev->tuner_type;
1242 tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1243 tun_setup.tuner_callback = cx23885_tuner_callback;
1245 v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1247 if ((dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) ||
1248 (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXPVR2200)) {
1249 struct xc2028_ctrl ctrl = {
1250 .fname = XC2028_DEFAULT_FIRMWARE,
1253 struct v4l2_priv_tun_config cfg = {
1254 .tuner = dev->tuner_type,
1257 v4l2_subdev_call(sd, tuner, s_config, &cfg);
1260 if (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) {
1261 struct xc2028_ctrl ctrl = {
1262 .fname = "xc3028L-v36.fw",
1265 struct v4l2_priv_tun_config cfg = {
1266 .tuner = dev->tuner_type,
1269 v4l2_subdev_call(sd, tuner, s_config, &cfg);
1274 /* initial device configuration */
1275 mutex_lock(&dev->lock);
1276 cx23885_set_tvnorm(dev, dev->tvnorm);
1277 cx23885_video_mux(dev, 0);
1278 cx23885_audio_mux(dev, 0);
1279 mutex_unlock(&dev->lock);
1282 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1283 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1284 q->gfp_flags = GFP_DMA32;
1285 q->min_buffers_needed = 2;
1287 q->buf_struct_size = sizeof(struct cx23885_buffer);
1288 q->ops = &cx23885_video_qops;
1289 q->mem_ops = &vb2_dma_sg_memops;
1290 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1291 q->lock = &dev->lock;
1292 q->dev = &dev->pci->dev;
1294 err = vb2_queue_init(q);
1299 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1300 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1301 q->gfp_flags = GFP_DMA32;
1302 q->min_buffers_needed = 2;
1304 q->buf_struct_size = sizeof(struct cx23885_buffer);
1305 q->ops = &cx23885_vbi_qops;
1306 q->mem_ops = &vb2_dma_sg_memops;
1307 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1308 q->lock = &dev->lock;
1309 q->dev = &dev->pci->dev;
1311 err = vb2_queue_init(q);
1315 /* register Video device */
1316 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1317 &cx23885_video_template, "video");
1318 dev->video_dev->queue = &dev->vb2_vidq;
1319 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1322 pr_info("%s: can't register video device\n",
1326 pr_info("%s: registered device %s [v4l2]\n",
1327 dev->name, video_device_node_name(dev->video_dev));
1329 /* register VBI device */
1330 dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
1331 &cx23885_vbi_template, "vbi");
1332 dev->vbi_dev->queue = &dev->vb2_vbiq;
1333 err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1336 pr_info("%s: can't register vbi device\n",
1340 pr_info("%s: registered device %s\n",
1341 dev->name, video_device_node_name(dev->vbi_dev));
1343 /* Register ALSA audio device */
1344 dev->audio_dev = cx23885_audio_register(dev);
1349 cx23885_video_unregister(dev);