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/0: " 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 *format_by_fourcc(unsigned int fourcc)
140 for (i = 0; i < ARRAY_SIZE(formats); i++)
141 if (formats[i].fourcc == fourcc)
144 printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __func__, fourcc);
148 /* ------------------------------------------------------------------- */
150 static const struct v4l2_queryctrl no_ctl = {
152 .flags = V4L2_CTRL_FLAG_DISABLED,
155 static struct cx23885_ctrl cx23885_ctls[] = {
159 .id = V4L2_CID_BRIGHTNESS,
160 .name = "Brightness",
164 .default_value = 0x7f,
165 .type = V4L2_CTRL_TYPE_INTEGER,
173 .id = V4L2_CID_CONTRAST,
178 .default_value = 0x3f,
179 .type = V4L2_CTRL_TYPE_INTEGER,
192 .default_value = 0x0,
193 .type = V4L2_CTRL_TYPE_INTEGER,
200 /* strictly, this only describes only U saturation.
201 * V saturation is handled specially through code.
204 .id = V4L2_CID_SATURATION,
205 .name = "Saturation",
209 .default_value = 0x3f,
210 .type = V4L2_CTRL_TYPE_INTEGER,
219 .id = V4L2_CID_AUDIO_MUTE,
224 .type = V4L2_CTRL_TYPE_BOOLEAN,
227 .mask = (0x1f << 24),
231 .id = V4L2_CID_AUDIO_VOLUME,
236 .default_value = 0x3f,
237 .type = V4L2_CTRL_TYPE_INTEGER,
239 .reg = PATH1_VOL_CTL,
244 static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
246 /* Must be sorted from low to high control ID! */
247 static const u32 cx23885_user_ctrls[] = {
253 V4L2_CID_AUDIO_VOLUME,
258 static const u32 *ctrl_classes[] = {
263 static void cx23885_video_wakeup(struct cx23885_dev *dev,
264 struct cx23885_dmaqueue *q, u32 count)
266 struct cx23885_buffer *buf;
269 for (bc = 0;; bc++) {
270 if (list_empty(&q->active))
272 buf = list_entry(q->active.next,
273 struct cx23885_buffer, vb.queue);
275 /* count comes from the hw and is is 16bit wide --
276 * this trick handles wrap-arounds correctly for
277 * up to 32767 buffers in flight... */
278 if ((s16) (count - buf->count) < 0)
281 do_gettimeofday(&buf->vb.ts);
282 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
284 buf->vb.state = VIDEOBUF_DONE;
285 list_del(&buf->vb.queue);
286 wake_up(&buf->vb.done);
288 if (list_empty(&q->active))
289 del_timer(&q->timeout);
291 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
293 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
297 static int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
299 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
302 v4l2_norm_to_name(norm));
306 call_all(dev, core, s_std, norm);
311 static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
313 struct video_device *template,
316 struct video_device *vfd;
317 dprintk(1, "%s()\n", __func__);
319 vfd = video_device_alloc();
323 vfd->v4l2_dev = &dev->v4l2_dev;
324 vfd->release = video_device_release;
325 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
326 dev->name, type, cx23885_boards[dev->board].name);
327 video_set_drvdata(vfd, dev);
331 static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
335 if (qctrl->id < V4L2_CID_BASE ||
336 qctrl->id >= V4L2_CID_LASTP1)
338 for (i = 0; i < CX23885_CTLS; i++)
339 if (cx23885_ctls[i].v.id == qctrl->id)
341 if (i == CX23885_CTLS) {
345 *qctrl = cx23885_ctls[i].v;
349 /* ------------------------------------------------------------------- */
350 /* resource management */
352 static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
355 dprintk(1, "%s()\n", __func__);
356 if (fh->resources & bit)
357 /* have it already allocated */
361 mutex_lock(&dev->lock);
362 if (dev->resources & bit) {
363 /* no, someone else uses it */
364 mutex_unlock(&dev->lock);
367 /* it's free, grab it */
368 fh->resources |= bit;
369 dev->resources |= bit;
370 dprintk(1, "res: get %d\n", bit);
371 mutex_unlock(&dev->lock);
375 static int res_check(struct cx23885_fh *fh, unsigned int bit)
377 return fh->resources & bit;
380 static int res_locked(struct cx23885_dev *dev, unsigned int bit)
382 return dev->resources & bit;
385 static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
388 BUG_ON((fh->resources & bits) != bits);
389 dprintk(1, "%s()\n", __func__);
391 mutex_lock(&dev->lock);
392 fh->resources &= ~bits;
393 dev->resources &= ~bits;
394 dprintk(1, "res: put %d\n", bits);
395 mutex_unlock(&dev->lock);
398 static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
400 dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
402 input, INPUT(input)->vmux,
403 INPUT(input)->gpio0, INPUT(input)->gpio1,
404 INPUT(input)->gpio2, INPUT(input)->gpio3);
407 if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
408 dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2) {
409 /* Select Analog TV */
410 if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
411 cx23885_gpio_clear(dev, GPIO_0);
414 /* Tell the internal A/V decoder */
415 v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
416 INPUT(input)->vmux, 0, 0);
421 /* ------------------------------------------------------------------ */
422 static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
423 unsigned int height, enum v4l2_field field)
425 dprintk(1, "%s()\n", __func__);
429 static int cx23885_start_video_dma(struct cx23885_dev *dev,
430 struct cx23885_dmaqueue *q,
431 struct cx23885_buffer *buf)
433 dprintk(1, "%s()\n", __func__);
435 /* setup fifo + format */
436 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
437 buf->bpl, buf->risc.dma);
438 cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
441 cx_write(VID_A_GPCNT_CTL, 3);
445 cx23885_irq_add_enable(dev, 0x01);
446 cx_set(VID_A_INT_MSK, 0x000011);
449 cx_set(DEV_CNTRL2, (1<<5));
450 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
456 static int cx23885_restart_video_queue(struct cx23885_dev *dev,
457 struct cx23885_dmaqueue *q)
459 struct cx23885_buffer *buf, *prev;
460 struct list_head *item;
461 dprintk(1, "%s()\n", __func__);
463 if (!list_empty(&q->active)) {
464 buf = list_entry(q->active.next, struct cx23885_buffer,
466 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
468 cx23885_start_video_dma(dev, q, buf);
469 list_for_each(item, &q->active) {
470 buf = list_entry(item, struct cx23885_buffer,
472 buf->count = q->count++;
474 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
480 if (list_empty(&q->queued))
482 buf = list_entry(q->queued.next, struct cx23885_buffer,
485 list_move_tail(&buf->vb.queue, &q->active);
486 cx23885_start_video_dma(dev, q, buf);
487 buf->vb.state = VIDEOBUF_ACTIVE;
488 buf->count = q->count++;
489 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
490 dprintk(2, "[%p/%d] restart_queue - first active\n",
493 } else if (prev->vb.width == buf->vb.width &&
494 prev->vb.height == buf->vb.height &&
495 prev->fmt == buf->fmt) {
496 list_move_tail(&buf->vb.queue, &q->active);
497 buf->vb.state = VIDEOBUF_ACTIVE;
498 buf->count = q->count++;
499 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
500 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
501 dprintk(2, "[%p/%d] restart_queue - move to active\n",
510 static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
513 struct cx23885_fh *fh = q->priv_data;
515 *size = fh->fmt->depth*fh->width*fh->height >> 3;
518 if (*size * *count > vid_limit * 1024 * 1024)
519 *count = (vid_limit * 1024 * 1024) / *size;
523 static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
524 enum v4l2_field field)
526 struct cx23885_fh *fh = q->priv_data;
527 struct cx23885_dev *dev = fh->dev;
528 struct cx23885_buffer *buf =
529 container_of(vb, struct cx23885_buffer, vb);
530 int rc, init_buffer = 0;
531 u32 line0_offset, line1_offset;
532 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
534 BUG_ON(NULL == fh->fmt);
535 if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
536 fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
538 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
539 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
542 if (buf->fmt != fh->fmt ||
543 buf->vb.width != fh->width ||
544 buf->vb.height != fh->height ||
545 buf->vb.field != field) {
547 buf->vb.width = fh->width;
548 buf->vb.height = fh->height;
549 buf->vb.field = field;
553 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
555 rc = videobuf_iolock(q, &buf->vb, NULL);
561 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
562 switch (buf->vb.field) {
564 cx23885_risc_buffer(dev->pci, &buf->risc,
565 dma->sglist, 0, UNSET,
566 buf->bpl, 0, buf->vb.height);
568 case V4L2_FIELD_BOTTOM:
569 cx23885_risc_buffer(dev->pci, &buf->risc,
570 dma->sglist, UNSET, 0,
571 buf->bpl, 0, buf->vb.height);
573 case V4L2_FIELD_INTERLACED:
574 if (dev->tvnorm & V4L2_STD_NTSC) {
575 /* cx25840 transmits NTSC bottom field first */
576 dprintk(1, "%s() Creating NTSC risc\n",
578 line0_offset = buf->bpl;
581 /* All other formats are top field first */
582 dprintk(1, "%s() Creating PAL/SECAM risc\n",
585 line1_offset = buf->bpl;
587 cx23885_risc_buffer(dev->pci, &buf->risc,
588 dma->sglist, line0_offset,
591 buf->vb.height >> 1);
593 case V4L2_FIELD_SEQ_TB:
594 cx23885_risc_buffer(dev->pci, &buf->risc,
596 0, buf->bpl * (buf->vb.height >> 1),
598 buf->vb.height >> 1);
600 case V4L2_FIELD_SEQ_BT:
601 cx23885_risc_buffer(dev->pci, &buf->risc,
603 buf->bpl * (buf->vb.height >> 1), 0,
605 buf->vb.height >> 1);
611 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
613 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
614 (unsigned long)buf->risc.dma);
616 buf->vb.state = VIDEOBUF_PREPARED;
620 cx23885_free_buffer(q, buf);
624 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
626 struct cx23885_buffer *buf = container_of(vb,
627 struct cx23885_buffer, vb);
628 struct cx23885_buffer *prev;
629 struct cx23885_fh *fh = vq->priv_data;
630 struct cx23885_dev *dev = fh->dev;
631 struct cx23885_dmaqueue *q = &dev->vidq;
633 /* add jump to stopper */
634 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
635 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
636 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
638 if (!list_empty(&q->queued)) {
639 list_add_tail(&buf->vb.queue, &q->queued);
640 buf->vb.state = VIDEOBUF_QUEUED;
641 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
644 } else if (list_empty(&q->active)) {
645 list_add_tail(&buf->vb.queue, &q->active);
646 cx23885_start_video_dma(dev, q, buf);
647 buf->vb.state = VIDEOBUF_ACTIVE;
648 buf->count = q->count++;
649 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
650 dprintk(2, "[%p/%d] buffer_queue - first active\n",
654 prev = list_entry(q->active.prev, struct cx23885_buffer,
656 if (prev->vb.width == buf->vb.width &&
657 prev->vb.height == buf->vb.height &&
658 prev->fmt == buf->fmt) {
659 list_add_tail(&buf->vb.queue, &q->active);
660 buf->vb.state = VIDEOBUF_ACTIVE;
661 buf->count = q->count++;
662 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
663 /* 64 bit bits 63-32 */
664 prev->risc.jmp[2] = cpu_to_le32(0);
665 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
669 list_add_tail(&buf->vb.queue, &q->queued);
670 buf->vb.state = VIDEOBUF_QUEUED;
671 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
677 static void buffer_release(struct videobuf_queue *q,
678 struct videobuf_buffer *vb)
680 struct cx23885_buffer *buf = container_of(vb,
681 struct cx23885_buffer, vb);
683 cx23885_free_buffer(q, buf);
686 static struct videobuf_queue_ops cx23885_video_qops = {
687 .buf_setup = buffer_setup,
688 .buf_prepare = buffer_prepare,
689 .buf_queue = buffer_queue,
690 .buf_release = buffer_release,
693 static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
696 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
698 case V4L2_BUF_TYPE_VBI_CAPTURE:
706 static int get_resource(struct cx23885_fh *fh)
709 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
710 return RESOURCE_VIDEO;
711 case V4L2_BUF_TYPE_VBI_CAPTURE:
719 static int video_open(struct file *file)
721 struct video_device *vdev = video_devdata(file);
722 struct cx23885_dev *dev = video_drvdata(file);
723 struct cx23885_fh *fh;
724 enum v4l2_buf_type type = 0;
727 switch (vdev->vfl_type) {
728 case VFL_TYPE_GRABBER:
729 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
732 type = V4L2_BUF_TYPE_VBI_CAPTURE;
739 dprintk(1, "open dev=%s radio=%d type=%s\n",
740 video_device_node_name(vdev), radio, v4l2_type_names[type]);
742 /* allocate + initialize per filehandle data */
743 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
747 file->private_data = fh;
753 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
755 videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
756 &dev->pci->dev, &dev->slock,
757 V4L2_BUF_TYPE_VIDEO_CAPTURE,
758 V4L2_FIELD_INTERLACED,
759 sizeof(struct cx23885_buffer),
762 dprintk(1, "post videobuf_queue_init()\n");
767 static ssize_t video_read(struct file *file, char __user *data,
768 size_t count, loff_t *ppos)
770 struct cx23885_fh *fh = file->private_data;
773 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
774 if (res_locked(fh->dev, RESOURCE_VIDEO))
776 return videobuf_read_one(&fh->vidq, data, count, ppos,
777 file->f_flags & O_NONBLOCK);
778 case V4L2_BUF_TYPE_VBI_CAPTURE:
779 if (!res_get(fh->dev, fh, RESOURCE_VBI))
781 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
782 file->f_flags & O_NONBLOCK);
789 static unsigned int video_poll(struct file *file,
790 struct poll_table_struct *wait)
792 struct cx23885_fh *fh = file->private_data;
793 struct cx23885_buffer *buf;
794 unsigned int rc = POLLERR;
796 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
797 if (!res_get(fh->dev, fh, RESOURCE_VBI))
799 return videobuf_poll_stream(file, &fh->vbiq, wait);
802 mutex_lock(&fh->vidq.vb_lock);
803 if (res_check(fh, RESOURCE_VIDEO)) {
804 /* streaming capture */
805 if (list_empty(&fh->vidq.stream))
807 buf = list_entry(fh->vidq.stream.next,
808 struct cx23885_buffer, vb.stream);
811 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
815 poll_wait(file, &buf->vb.done, wait);
816 if (buf->vb.state == VIDEOBUF_DONE ||
817 buf->vb.state == VIDEOBUF_ERROR)
818 rc = POLLIN|POLLRDNORM;
822 mutex_unlock(&fh->vidq.vb_lock);
826 static int video_release(struct file *file)
828 struct cx23885_fh *fh = file->private_data;
829 struct cx23885_dev *dev = fh->dev;
831 /* turn off overlay */
832 if (res_check(fh, RESOURCE_OVERLAY)) {
834 res_free(dev, fh, RESOURCE_OVERLAY);
837 /* stop video capture */
838 if (res_check(fh, RESOURCE_VIDEO)) {
839 videobuf_queue_cancel(&fh->vidq);
840 res_free(dev, fh, RESOURCE_VIDEO);
842 if (fh->vidq.read_buf) {
843 buffer_release(&fh->vidq, fh->vidq.read_buf);
844 kfree(fh->vidq.read_buf);
847 /* stop vbi capture */
848 if (res_check(fh, RESOURCE_VBI)) {
849 if (fh->vbiq.streaming)
850 videobuf_streamoff(&fh->vbiq);
851 if (fh->vbiq.reading)
852 videobuf_read_stop(&fh->vbiq);
853 res_free(dev, fh, RESOURCE_VBI);
856 videobuf_mmap_free(&fh->vidq);
857 file->private_data = NULL;
860 /* We are not putting the tuner to sleep here on exit, because
861 * we want to use the mpeg encoder in another session to capture
862 * tuner video. Closing this will result in no video to the encoder.
868 static int video_mmap(struct file *file, struct vm_area_struct *vma)
870 struct cx23885_fh *fh = file->private_data;
872 return videobuf_mmap_mapper(get_queue(fh), vma);
875 /* ------------------------------------------------------------------ */
876 /* VIDEO CTRL IOCTLS */
878 static int cx23885_get_control(struct cx23885_dev *dev,
879 struct v4l2_control *ctl)
881 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
882 call_all(dev, core, g_ctrl, ctl);
886 static int cx23885_set_control(struct cx23885_dev *dev,
887 struct v4l2_control *ctl)
889 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)\n", __func__);
890 call_all(dev, core, s_ctrl, ctl);
895 static void init_controls(struct cx23885_dev *dev)
897 struct v4l2_control ctrl;
900 for (i = 0; i < CX23885_CTLS; i++) {
901 ctrl.id = cx23885_ctls[i].v.id;
902 ctrl.value = cx23885_ctls[i].v.default_value;
904 cx23885_set_control(dev, &ctrl);
908 /* ------------------------------------------------------------------ */
911 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
912 struct v4l2_format *f)
914 struct cx23885_fh *fh = priv;
916 f->fmt.pix.width = fh->width;
917 f->fmt.pix.height = fh->height;
918 f->fmt.pix.field = fh->vidq.field;
919 f->fmt.pix.pixelformat = fh->fmt->fourcc;
920 f->fmt.pix.bytesperline =
921 (f->fmt.pix.width * fh->fmt->depth) >> 3;
922 f->fmt.pix.sizeimage =
923 f->fmt.pix.height * f->fmt.pix.bytesperline;
928 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
929 struct v4l2_format *f)
931 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
932 struct cx23885_fmt *fmt;
933 enum v4l2_field field;
934 unsigned int maxw, maxh;
936 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
940 field = f->fmt.pix.field;
941 maxw = norm_maxw(dev->tvnorm);
942 maxh = norm_maxh(dev->tvnorm);
944 if (V4L2_FIELD_ANY == field) {
945 field = (f->fmt.pix.height > maxh/2)
946 ? V4L2_FIELD_INTERLACED
952 case V4L2_FIELD_BOTTOM:
955 case V4L2_FIELD_INTERLACED:
961 f->fmt.pix.field = field;
962 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
963 &f->fmt.pix.height, 32, maxh, 0, 0);
964 f->fmt.pix.bytesperline =
965 (f->fmt.pix.width * fmt->depth) >> 3;
966 f->fmt.pix.sizeimage =
967 f->fmt.pix.height * f->fmt.pix.bytesperline;
972 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
973 struct v4l2_format *f)
975 struct cx23885_fh *fh = priv;
976 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
977 struct v4l2_mbus_framefmt mbus_fmt;
980 dprintk(2, "%s()\n", __func__);
981 err = vidioc_try_fmt_vid_cap(file, priv, f);
985 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
986 fh->width = f->fmt.pix.width;
987 fh->height = f->fmt.pix.height;
988 fh->vidq.field = f->fmt.pix.field;
989 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
990 fh->width, fh->height, fh->vidq.field);
991 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
992 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
993 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
997 static int vidioc_querycap(struct file *file, void *priv,
998 struct v4l2_capability *cap)
1000 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1002 strcpy(cap->driver, "cx23885");
1003 strlcpy(cap->card, cx23885_boards[dev->board].name,
1005 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1007 V4L2_CAP_VIDEO_CAPTURE |
1008 V4L2_CAP_READWRITE |
1009 V4L2_CAP_STREAMING |
1010 V4L2_CAP_VBI_CAPTURE;
1011 if (UNSET != dev->tuner_type)
1012 cap->capabilities |= V4L2_CAP_TUNER;
1016 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1017 struct v4l2_fmtdesc *f)
1019 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1022 strlcpy(f->description, formats[f->index].name,
1023 sizeof(f->description));
1024 f->pixelformat = formats[f->index].fourcc;
1029 static int vidioc_reqbufs(struct file *file, void *priv,
1030 struct v4l2_requestbuffers *p)
1032 struct cx23885_fh *fh = priv;
1033 return videobuf_reqbufs(get_queue(fh), p);
1036 static int vidioc_querybuf(struct file *file, void *priv,
1037 struct v4l2_buffer *p)
1039 struct cx23885_fh *fh = priv;
1040 return videobuf_querybuf(get_queue(fh), p);
1043 static int vidioc_qbuf(struct file *file, void *priv,
1044 struct v4l2_buffer *p)
1046 struct cx23885_fh *fh = priv;
1047 return videobuf_qbuf(get_queue(fh), p);
1050 static int vidioc_dqbuf(struct file *file, void *priv,
1051 struct v4l2_buffer *p)
1053 struct cx23885_fh *fh = priv;
1054 return videobuf_dqbuf(get_queue(fh), p,
1055 file->f_flags & O_NONBLOCK);
1058 static int vidioc_streamon(struct file *file, void *priv,
1059 enum v4l2_buf_type i)
1061 struct cx23885_fh *fh = priv;
1062 struct cx23885_dev *dev = fh->dev;
1063 dprintk(1, "%s()\n", __func__);
1065 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1067 if (unlikely(i != fh->type))
1070 if (unlikely(!res_get(dev, fh, get_resource(fh))))
1072 return videobuf_streamon(get_queue(fh));
1075 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1077 struct cx23885_fh *fh = priv;
1078 struct cx23885_dev *dev = fh->dev;
1080 dprintk(1, "%s()\n", __func__);
1082 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1087 res = get_resource(fh);
1088 err = videobuf_streamoff(get_queue(fh));
1091 res_free(dev, fh, res);
1095 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1097 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1098 dprintk(1, "%s()\n", __func__);
1100 mutex_lock(&dev->lock);
1101 cx23885_set_tvnorm(dev, *tvnorms);
1102 mutex_unlock(&dev->lock);
1107 static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
1109 static const char *iname[] = {
1110 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1111 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1112 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1113 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1114 [CX23885_VMUX_SVIDEO] = "S-Video",
1115 [CX23885_VMUX_COMPONENT] = "Component",
1116 [CX23885_VMUX_TELEVISION] = "Television",
1117 [CX23885_VMUX_CABLE] = "Cable TV",
1118 [CX23885_VMUX_DVB] = "DVB",
1119 [CX23885_VMUX_DEBUG] = "for debug only",
1122 dprintk(1, "%s()\n", __func__);
1128 if (0 == INPUT(n)->type)
1132 i->type = V4L2_INPUT_TYPE_CAMERA;
1133 strcpy(i->name, iname[INPUT(n)->type]);
1134 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
1135 (CX23885_VMUX_CABLE == INPUT(n)->type)) {
1136 i->type = V4L2_INPUT_TYPE_TUNER;
1137 i->std = CX23885_NORMS;
1142 static int vidioc_enum_input(struct file *file, void *priv,
1143 struct v4l2_input *i)
1145 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1146 dprintk(1, "%s()\n", __func__);
1147 return cx23885_enum_input(dev, i);
1150 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1152 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1155 dprintk(1, "%s() returns %d\n", __func__, *i);
1159 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1161 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1163 dprintk(1, "%s(%d)\n", __func__, i);
1166 dprintk(1, "%s() -EINVAL\n", __func__);
1170 mutex_lock(&dev->lock);
1171 cx23885_video_mux(dev, i);
1172 mutex_unlock(&dev->lock);
1176 static int vidioc_log_status(struct file *file, void *priv)
1178 struct cx23885_fh *fh = priv;
1179 struct cx23885_dev *dev = fh->dev;
1182 "%s/0: ============ START LOG STATUS ============\n",
1184 call_all(dev, core, log_status);
1186 "%s/0: ============= END LOG STATUS =============\n",
1191 static int vidioc_queryctrl(struct file *file, void *priv,
1192 struct v4l2_queryctrl *qctrl)
1194 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1195 if (unlikely(qctrl->id == 0))
1197 return cx23885_ctrl_query(qctrl);
1200 static int vidioc_g_ctrl(struct file *file, void *priv,
1201 struct v4l2_control *ctl)
1203 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1205 return cx23885_get_control(dev, ctl);
1208 static int vidioc_s_ctrl(struct file *file, void *priv,
1209 struct v4l2_control *ctl)
1211 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1213 return cx23885_set_control(dev, ctl);
1216 static int vidioc_g_tuner(struct file *file, void *priv,
1217 struct v4l2_tuner *t)
1219 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1221 if (unlikely(UNSET == dev->tuner_type))
1226 strcpy(t->name, "Television");
1228 call_all(dev, tuner, g_tuner, t);
1232 static int vidioc_s_tuner(struct file *file, void *priv,
1233 struct v4l2_tuner *t)
1235 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1237 if (UNSET == dev->tuner_type)
1241 /* Update the A/V core */
1242 call_all(dev, tuner, s_tuner, t);
1247 static int vidioc_g_frequency(struct file *file, void *priv,
1248 struct v4l2_frequency *f)
1250 struct cx23885_fh *fh = priv;
1251 struct cx23885_dev *dev = fh->dev;
1253 if (unlikely(UNSET == dev->tuner_type))
1256 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1257 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1258 f->frequency = dev->freq;
1260 call_all(dev, tuner, g_frequency, f);
1265 static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
1267 if (unlikely(UNSET == dev->tuner_type))
1269 if (unlikely(f->tuner != 0))
1272 mutex_lock(&dev->lock);
1273 dev->freq = f->frequency;
1275 call_all(dev, tuner, s_frequency, f);
1277 /* When changing channels it is required to reset TVAUDIO */
1280 mutex_unlock(&dev->lock);
1285 static int vidioc_s_frequency(struct file *file, void *priv,
1286 struct v4l2_frequency *f)
1288 struct cx23885_fh *fh = priv;
1289 struct cx23885_dev *dev = fh->dev;
1291 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1293 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1297 cx23885_set_freq(dev, f);
1300 /* ----------------------------------------------------------- */
1302 static void cx23885_vid_timeout(unsigned long data)
1304 struct cx23885_dev *dev = (struct cx23885_dev *)data;
1305 struct cx23885_dmaqueue *q = &dev->vidq;
1306 struct cx23885_buffer *buf;
1307 unsigned long flags;
1309 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1311 cx_clear(VID_A_DMA_CTL, 0x11);
1313 spin_lock_irqsave(&dev->slock, flags);
1314 while (!list_empty(&q->active)) {
1315 buf = list_entry(q->active.next,
1316 struct cx23885_buffer, vb.queue);
1317 list_del(&buf->vb.queue);
1318 buf->vb.state = VIDEOBUF_ERROR;
1319 wake_up(&buf->vb.done);
1320 printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
1321 dev->name, buf, buf->vb.i,
1322 (unsigned long)buf->risc.dma);
1324 cx23885_restart_video_queue(dev, q);
1325 spin_unlock_irqrestore(&dev->slock, flags);
1328 int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1333 mask = cx_read(VID_A_INT_MSK);
1334 if (0 == (status & mask))
1336 cx_write(VID_A_INT_STAT, status);
1338 dprintk(2, "%s() status = 0x%08x\n", __func__, status);
1339 /* risc op code error */
1340 if (status & (1 << 16)) {
1341 printk(KERN_WARNING "%s/0: video risc op code error\n",
1343 cx_clear(VID_A_DMA_CTL, 0x11);
1344 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1348 if (status & 0x01) {
1349 spin_lock(&dev->slock);
1350 count = cx_read(VID_A_GPCNT);
1351 cx23885_video_wakeup(dev, &dev->vidq, count);
1352 spin_unlock(&dev->slock);
1356 if (status & 0x10) {
1357 dprintk(2, "stopper video\n");
1358 spin_lock(&dev->slock);
1359 cx23885_restart_video_queue(dev, &dev->vidq);
1360 spin_unlock(&dev->slock);
1367 /* ----------------------------------------------------------- */
1368 /* exported stuff */
1370 static const struct v4l2_file_operations video_fops = {
1371 .owner = THIS_MODULE,
1373 .release = video_release,
1377 .ioctl = video_ioctl2,
1380 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1381 .vidioc_querycap = vidioc_querycap,
1382 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1383 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1384 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1385 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1386 .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
1387 .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
1388 .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
1389 .vidioc_reqbufs = vidioc_reqbufs,
1390 .vidioc_querybuf = vidioc_querybuf,
1391 .vidioc_qbuf = vidioc_qbuf,
1392 .vidioc_dqbuf = vidioc_dqbuf,
1393 .vidioc_s_std = vidioc_s_std,
1394 .vidioc_enum_input = vidioc_enum_input,
1395 .vidioc_g_input = vidioc_g_input,
1396 .vidioc_s_input = vidioc_s_input,
1397 .vidioc_log_status = vidioc_log_status,
1398 .vidioc_queryctrl = vidioc_queryctrl,
1399 .vidioc_g_ctrl = vidioc_g_ctrl,
1400 .vidioc_s_ctrl = vidioc_s_ctrl,
1401 .vidioc_streamon = vidioc_streamon,
1402 .vidioc_streamoff = vidioc_streamoff,
1403 .vidioc_g_tuner = vidioc_g_tuner,
1404 .vidioc_s_tuner = vidioc_s_tuner,
1405 .vidioc_g_frequency = vidioc_g_frequency,
1406 .vidioc_s_frequency = vidioc_s_frequency,
1407 .vidioc_g_chip_ident = cx23885_g_chip_ident,
1408 #ifdef CONFIG_VIDEO_ADV_DEBUG
1409 .vidioc_g_register = cx23885_g_register,
1410 .vidioc_s_register = cx23885_s_register,
1414 static struct video_device cx23885_vbi_template;
1415 static struct video_device cx23885_video_template = {
1416 .name = "cx23885-video",
1417 .fops = &video_fops,
1418 .ioctl_ops = &video_ioctl_ops,
1419 .tvnorms = CX23885_NORMS,
1420 .current_norm = V4L2_STD_NTSC_M,
1423 static const struct v4l2_file_operations radio_fops = {
1424 .owner = THIS_MODULE,
1426 .release = video_release,
1427 .ioctl = video_ioctl2,
1431 void cx23885_video_unregister(struct cx23885_dev *dev)
1433 dprintk(1, "%s()\n", __func__);
1434 cx23885_irq_remove(dev, 0x01);
1436 if (dev->video_dev) {
1437 if (video_is_registered(dev->video_dev))
1438 video_unregister_device(dev->video_dev);
1440 video_device_release(dev->video_dev);
1441 dev->video_dev = NULL;
1443 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1447 cx23885_audio_finidev(dev);
1450 int cx23885_video_register(struct cx23885_dev *dev)
1454 dprintk(1, "%s()\n", __func__);
1455 spin_lock_init(&dev->slock);
1457 /* Initialize VBI template */
1458 memcpy(&cx23885_vbi_template, &cx23885_video_template,
1459 sizeof(cx23885_vbi_template));
1460 strcpy(cx23885_vbi_template.name, "cx23885-vbi");
1462 dev->tvnorm = cx23885_video_template.current_norm;
1464 /* init video dma queues */
1465 INIT_LIST_HEAD(&dev->vidq.active);
1466 INIT_LIST_HEAD(&dev->vidq.queued);
1467 dev->vidq.timeout.function = cx23885_vid_timeout;
1468 dev->vidq.timeout.data = (unsigned long)dev;
1469 init_timer(&dev->vidq.timeout);
1470 cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1471 VID_A_DMA_CTL, 0x11, 0x00);
1473 /* Don't enable VBI yet */
1475 cx23885_irq_add_enable(dev, 0x01);
1477 if ((TUNER_ABSENT != dev->tuner_type) &&
1478 ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
1479 struct v4l2_subdev *sd = NULL;
1481 if (dev->tuner_addr)
1482 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1483 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
1484 "tuner", dev->tuner_addr, NULL);
1486 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1487 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
1488 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
1490 struct tuner_setup tun_setup;
1492 memset(&tun_setup, 0, sizeof(tun_setup));
1493 tun_setup.mode_mask = T_ANALOG_TV;
1494 tun_setup.type = dev->tuner_type;
1495 tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1496 tun_setup.tuner_callback = cx23885_tuner_callback;
1498 v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1500 if (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) {
1501 struct xc2028_ctrl ctrl = {
1502 .fname = XC2028_DEFAULT_FIRMWARE,
1505 struct v4l2_priv_tun_config cfg = {
1506 .tuner = dev->tuner_type,
1509 v4l2_subdev_call(sd, tuner, s_config, &cfg);
1514 /* register v4l devices */
1515 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1516 &cx23885_video_template, "video");
1517 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1520 printk(KERN_INFO "%s: can't register video device\n",
1524 printk(KERN_INFO "%s/0: registered device %s [v4l2]\n",
1525 dev->name, video_device_node_name(dev->video_dev));
1527 /* Register ALSA audio device */
1528 dev->audio_dev = cx23885_audio_initdev(dev);
1530 /* initial device configuration */
1531 mutex_lock(&dev->lock);
1532 cx23885_set_tvnorm(dev, dev->tvnorm);
1534 cx23885_video_mux(dev, 0);
1535 mutex_unlock(&dev->lock);
1540 cx23885_video_unregister(dev);