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/smp_lock.h>
30 #include <linux/interrupt.h>
31 #include <linux/delay.h>
32 #include <linux/kthread.h>
33 #include <asm/div64.h>
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
38 #include "cx23885-ioctl.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 };
48 static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
50 module_param_array(video_nr, int, NULL, 0444);
51 module_param_array(vbi_nr, int, NULL, 0444);
52 module_param_array(radio_nr, int, NULL, 0444);
54 MODULE_PARM_DESC(video_nr, "video device numbers");
55 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
56 MODULE_PARM_DESC(radio_nr, "radio device numbers");
58 static unsigned int video_debug;
59 module_param(video_debug, int, 0644);
60 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
62 static unsigned int irq_debug;
63 module_param(irq_debug, int, 0644);
64 MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
66 static unsigned int vid_limit = 16;
67 module_param(vid_limit, int, 0644);
68 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
70 #define dprintk(level, fmt, arg...)\
71 do { if (video_debug >= level)\
72 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
75 /* ------------------------------------------------------------------- */
78 #define FORMAT_FLAGS_PACKED 0x01
80 static struct cx23885_fmt formats[] = {
82 .name = "8 bpp, gray",
83 .fourcc = V4L2_PIX_FMT_GREY,
85 .flags = FORMAT_FLAGS_PACKED,
87 .name = "15 bpp RGB, le",
88 .fourcc = V4L2_PIX_FMT_RGB555,
90 .flags = FORMAT_FLAGS_PACKED,
92 .name = "15 bpp RGB, be",
93 .fourcc = V4L2_PIX_FMT_RGB555X,
95 .flags = FORMAT_FLAGS_PACKED,
97 .name = "16 bpp RGB, le",
98 .fourcc = V4L2_PIX_FMT_RGB565,
100 .flags = FORMAT_FLAGS_PACKED,
102 .name = "16 bpp RGB, be",
103 .fourcc = V4L2_PIX_FMT_RGB565X,
105 .flags = FORMAT_FLAGS_PACKED,
107 .name = "24 bpp RGB, le",
108 .fourcc = V4L2_PIX_FMT_BGR24,
110 .flags = FORMAT_FLAGS_PACKED,
112 .name = "32 bpp RGB, le",
113 .fourcc = V4L2_PIX_FMT_BGR32,
115 .flags = FORMAT_FLAGS_PACKED,
117 .name = "32 bpp RGB, be",
118 .fourcc = V4L2_PIX_FMT_RGB32,
120 .flags = FORMAT_FLAGS_PACKED,
122 .name = "4:2:2, packed, YUYV",
123 .fourcc = V4L2_PIX_FMT_YUYV,
125 .flags = FORMAT_FLAGS_PACKED,
127 .name = "4:2:2, packed, UYVY",
128 .fourcc = V4L2_PIX_FMT_UYVY,
130 .flags = FORMAT_FLAGS_PACKED,
134 static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
138 for (i = 0; i < ARRAY_SIZE(formats); i++)
139 if (formats[i].fourcc == fourcc)
142 printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __func__, fourcc);
146 /* ------------------------------------------------------------------- */
148 static const struct v4l2_queryctrl no_ctl = {
150 .flags = V4L2_CTRL_FLAG_DISABLED,
153 static struct cx23885_ctrl cx23885_ctls[] = {
157 .id = V4L2_CID_BRIGHTNESS,
158 .name = "Brightness",
162 .default_value = 0x7f,
163 .type = V4L2_CTRL_TYPE_INTEGER,
171 .id = V4L2_CID_CONTRAST,
176 .default_value = 0x3f,
177 .type = V4L2_CTRL_TYPE_INTEGER,
190 .default_value = 0x7f,
191 .type = V4L2_CTRL_TYPE_INTEGER,
198 /* strictly, this only describes only U saturation.
199 * V saturation is handled specially through code.
202 .id = V4L2_CID_SATURATION,
203 .name = "Saturation",
207 .default_value = 0x7f,
208 .type = V4L2_CTRL_TYPE_INTEGER,
217 .id = V4L2_CID_AUDIO_MUTE,
222 .type = V4L2_CTRL_TYPE_BOOLEAN,
225 .mask = (0x1f << 24),
229 .id = V4L2_CID_AUDIO_VOLUME,
234 .default_value = 0x3f,
235 .type = V4L2_CTRL_TYPE_INTEGER,
237 .reg = PATH1_VOL_CTL,
242 static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
244 /* Must be sorted from low to high control ID! */
245 static const u32 cx23885_user_ctrls[] = {
251 V4L2_CID_AUDIO_VOLUME,
256 static const u32 *ctrl_classes[] = {
261 static void cx23885_video_wakeup(struct cx23885_dev *dev,
262 struct cx23885_dmaqueue *q, u32 count)
264 struct cx23885_buffer *buf;
267 for (bc = 0;; bc++) {
268 if (list_empty(&q->active))
270 buf = list_entry(q->active.next,
271 struct cx23885_buffer, vb.queue);
273 /* count comes from the hw and is is 16bit wide --
274 * this trick handles wrap-arounds correctly for
275 * up to 32767 buffers in flight... */
276 if ((s16) (count - buf->count) < 0)
279 do_gettimeofday(&buf->vb.ts);
280 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
282 buf->vb.state = VIDEOBUF_DONE;
283 list_del(&buf->vb.queue);
284 wake_up(&buf->vb.done);
286 if (list_empty(&q->active))
287 del_timer(&q->timeout);
289 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
291 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
295 static int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
297 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
300 v4l2_norm_to_name(norm));
304 call_all(dev, core, s_std, norm);
309 static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
311 struct video_device *template,
314 struct video_device *vfd;
315 dprintk(1, "%s()\n", __func__);
317 vfd = video_device_alloc();
321 vfd->v4l2_dev = &dev->v4l2_dev;
322 vfd->release = video_device_release;
323 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
324 dev->name, type, cx23885_boards[dev->board].name);
325 video_set_drvdata(vfd, dev);
329 static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
333 if (qctrl->id < V4L2_CID_BASE ||
334 qctrl->id >= V4L2_CID_LASTP1)
336 for (i = 0; i < CX23885_CTLS; i++)
337 if (cx23885_ctls[i].v.id == qctrl->id)
339 if (i == CX23885_CTLS) {
343 *qctrl = cx23885_ctls[i].v;
347 /* ------------------------------------------------------------------- */
348 /* resource management */
350 static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
353 dprintk(1, "%s()\n", __func__);
354 if (fh->resources & bit)
355 /* have it already allocated */
359 mutex_lock(&dev->lock);
360 if (dev->resources & bit) {
361 /* no, someone else uses it */
362 mutex_unlock(&dev->lock);
365 /* it's free, grab it */
366 fh->resources |= bit;
367 dev->resources |= bit;
368 dprintk(1, "res: get %d\n", bit);
369 mutex_unlock(&dev->lock);
373 static int res_check(struct cx23885_fh *fh, unsigned int bit)
375 return fh->resources & bit;
378 static int res_locked(struct cx23885_dev *dev, unsigned int bit)
380 return dev->resources & bit;
383 static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
386 BUG_ON((fh->resources & bits) != bits);
387 dprintk(1, "%s()\n", __func__);
389 mutex_lock(&dev->lock);
390 fh->resources &= ~bits;
391 dev->resources &= ~bits;
392 dprintk(1, "res: put %d\n", bits);
393 mutex_unlock(&dev->lock);
396 static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
398 dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
400 input, INPUT(input)->vmux,
401 INPUT(input)->gpio0, INPUT(input)->gpio1,
402 INPUT(input)->gpio2, INPUT(input)->gpio3);
405 if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
406 dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2) {
407 /* Select Analog TV */
408 if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
409 cx23885_gpio_clear(dev, GPIO_0);
412 /* Tell the internal A/V decoder */
413 v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
414 INPUT(input)->vmux, 0, 0);
419 /* ------------------------------------------------------------------ */
420 static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
421 unsigned int height, enum v4l2_field field)
423 dprintk(1, "%s()\n", __func__);
427 static int cx23885_start_video_dma(struct cx23885_dev *dev,
428 struct cx23885_dmaqueue *q,
429 struct cx23885_buffer *buf)
431 dprintk(1, "%s()\n", __func__);
433 /* setup fifo + format */
434 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
435 buf->bpl, buf->risc.dma);
436 cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
439 cx_write(VID_A_GPCNT_CTL, 3);
443 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
444 cx_set(VID_A_INT_MSK, 0x000011);
447 cx_set(DEV_CNTRL2, (1<<5));
448 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
454 static int cx23885_restart_video_queue(struct cx23885_dev *dev,
455 struct cx23885_dmaqueue *q)
457 struct cx23885_buffer *buf, *prev;
458 struct list_head *item;
459 dprintk(1, "%s()\n", __func__);
461 if (!list_empty(&q->active)) {
462 buf = list_entry(q->active.next, struct cx23885_buffer,
464 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
466 cx23885_start_video_dma(dev, q, buf);
467 list_for_each(item, &q->active) {
468 buf = list_entry(item, struct cx23885_buffer,
470 buf->count = q->count++;
472 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
478 if (list_empty(&q->queued))
480 buf = list_entry(q->queued.next, struct cx23885_buffer,
483 list_move_tail(&buf->vb.queue, &q->active);
484 cx23885_start_video_dma(dev, q, buf);
485 buf->vb.state = VIDEOBUF_ACTIVE;
486 buf->count = q->count++;
487 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
488 dprintk(2, "[%p/%d] restart_queue - first active\n",
491 } else if (prev->vb.width == buf->vb.width &&
492 prev->vb.height == buf->vb.height &&
493 prev->fmt == buf->fmt) {
494 list_move_tail(&buf->vb.queue, &q->active);
495 buf->vb.state = VIDEOBUF_ACTIVE;
496 buf->count = q->count++;
497 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
498 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
499 dprintk(2, "[%p/%d] restart_queue - move to active\n",
508 static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
511 struct cx23885_fh *fh = q->priv_data;
513 *size = fh->fmt->depth*fh->width*fh->height >> 3;
516 while (*size * *count > vid_limit * 1024 * 1024)
521 static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
522 enum v4l2_field field)
524 struct cx23885_fh *fh = q->priv_data;
525 struct cx23885_dev *dev = fh->dev;
526 struct cx23885_buffer *buf =
527 container_of(vb, struct cx23885_buffer, vb);
528 int rc, init_buffer = 0;
529 u32 line0_offset, line1_offset;
530 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
532 BUG_ON(NULL == fh->fmt);
533 if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
534 fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
536 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
537 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
540 if (buf->fmt != fh->fmt ||
541 buf->vb.width != fh->width ||
542 buf->vb.height != fh->height ||
543 buf->vb.field != field) {
545 buf->vb.width = fh->width;
546 buf->vb.height = fh->height;
547 buf->vb.field = field;
551 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
553 rc = videobuf_iolock(q, &buf->vb, NULL);
559 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
560 switch (buf->vb.field) {
562 cx23885_risc_buffer(dev->pci, &buf->risc,
563 dma->sglist, 0, UNSET,
564 buf->bpl, 0, buf->vb.height);
566 case V4L2_FIELD_BOTTOM:
567 cx23885_risc_buffer(dev->pci, &buf->risc,
568 dma->sglist, UNSET, 0,
569 buf->bpl, 0, buf->vb.height);
571 case V4L2_FIELD_INTERLACED:
572 if (dev->tvnorm & V4L2_STD_NTSC) {
573 /* cx25840 transmits NTSC bottom field first */
574 dprintk(1, "%s() Creating NTSC risc\n",
576 line0_offset = buf->bpl;
579 /* All other formats are top field first */
580 dprintk(1, "%s() Creating PAL/SECAM risc\n",
583 line1_offset = buf->bpl;
585 cx23885_risc_buffer(dev->pci, &buf->risc,
586 dma->sglist, line0_offset,
589 buf->vb.height >> 1);
591 case V4L2_FIELD_SEQ_TB:
592 cx23885_risc_buffer(dev->pci, &buf->risc,
594 0, buf->bpl * (buf->vb.height >> 1),
596 buf->vb.height >> 1);
598 case V4L2_FIELD_SEQ_BT:
599 cx23885_risc_buffer(dev->pci, &buf->risc,
601 buf->bpl * (buf->vb.height >> 1), 0,
603 buf->vb.height >> 1);
609 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
611 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
612 (unsigned long)buf->risc.dma);
614 buf->vb.state = VIDEOBUF_PREPARED;
618 cx23885_free_buffer(q, buf);
622 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
624 struct cx23885_buffer *buf = container_of(vb,
625 struct cx23885_buffer, vb);
626 struct cx23885_buffer *prev;
627 struct cx23885_fh *fh = vq->priv_data;
628 struct cx23885_dev *dev = fh->dev;
629 struct cx23885_dmaqueue *q = &dev->vidq;
631 /* add jump to stopper */
632 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
633 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
634 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
636 if (!list_empty(&q->queued)) {
637 list_add_tail(&buf->vb.queue, &q->queued);
638 buf->vb.state = VIDEOBUF_QUEUED;
639 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
642 } else if (list_empty(&q->active)) {
643 list_add_tail(&buf->vb.queue, &q->active);
644 cx23885_start_video_dma(dev, q, buf);
645 buf->vb.state = VIDEOBUF_ACTIVE;
646 buf->count = q->count++;
647 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
648 dprintk(2, "[%p/%d] buffer_queue - first active\n",
652 prev = list_entry(q->active.prev, struct cx23885_buffer,
654 if (prev->vb.width == buf->vb.width &&
655 prev->vb.height == buf->vb.height &&
656 prev->fmt == buf->fmt) {
657 list_add_tail(&buf->vb.queue, &q->active);
658 buf->vb.state = VIDEOBUF_ACTIVE;
659 buf->count = q->count++;
660 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
661 /* 64 bit bits 63-32 */
662 prev->risc.jmp[2] = cpu_to_le32(0);
663 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
667 list_add_tail(&buf->vb.queue, &q->queued);
668 buf->vb.state = VIDEOBUF_QUEUED;
669 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
675 static void buffer_release(struct videobuf_queue *q,
676 struct videobuf_buffer *vb)
678 struct cx23885_buffer *buf = container_of(vb,
679 struct cx23885_buffer, vb);
681 cx23885_free_buffer(q, buf);
684 static struct videobuf_queue_ops cx23885_video_qops = {
685 .buf_setup = buffer_setup,
686 .buf_prepare = buffer_prepare,
687 .buf_queue = buffer_queue,
688 .buf_release = buffer_release,
691 static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
694 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
696 case V4L2_BUF_TYPE_VBI_CAPTURE:
704 static int get_resource(struct cx23885_fh *fh)
707 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
708 return RESOURCE_VIDEO;
709 case V4L2_BUF_TYPE_VBI_CAPTURE:
717 static int video_open(struct file *file)
719 struct video_device *vdev = video_devdata(file);
720 struct cx23885_dev *dev = video_drvdata(file);
721 struct cx23885_fh *fh;
722 enum v4l2_buf_type type = 0;
725 switch (vdev->vfl_type) {
726 case VFL_TYPE_GRABBER:
727 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
730 type = V4L2_BUF_TYPE_VBI_CAPTURE;
737 dprintk(1, "open dev=%s radio=%d type=%s\n",
738 video_device_node_name(vdev), radio, v4l2_type_names[type]);
740 /* allocate + initialize per filehandle data */
741 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");
769 static ssize_t video_read(struct file *file, char __user *data,
770 size_t count, loff_t *ppos)
772 struct cx23885_fh *fh = file->private_data;
775 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
776 if (res_locked(fh->dev, RESOURCE_VIDEO))
778 return videobuf_read_one(&fh->vidq, data, count, ppos,
779 file->f_flags & O_NONBLOCK);
780 case V4L2_BUF_TYPE_VBI_CAPTURE:
781 if (!res_get(fh->dev, fh, RESOURCE_VBI))
783 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
784 file->f_flags & O_NONBLOCK);
791 static unsigned int video_poll(struct file *file,
792 struct poll_table_struct *wait)
794 struct cx23885_fh *fh = file->private_data;
795 struct cx23885_buffer *buf;
796 unsigned int rc = POLLERR;
798 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
799 if (!res_get(fh->dev, fh, RESOURCE_VBI))
801 return videobuf_poll_stream(file, &fh->vbiq, wait);
804 mutex_lock(&fh->vidq.vb_lock);
805 if (res_check(fh, RESOURCE_VIDEO)) {
806 /* streaming capture */
807 if (list_empty(&fh->vidq.stream))
809 buf = list_entry(fh->vidq.stream.next,
810 struct cx23885_buffer, vb.stream);
813 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
817 poll_wait(file, &buf->vb.done, wait);
818 if (buf->vb.state == VIDEOBUF_DONE ||
819 buf->vb.state == VIDEOBUF_ERROR)
820 rc = POLLIN|POLLRDNORM;
824 mutex_unlock(&fh->vidq.vb_lock);
828 static int video_release(struct file *file)
830 struct cx23885_fh *fh = file->private_data;
831 struct cx23885_dev *dev = fh->dev;
833 /* turn off overlay */
834 if (res_check(fh, RESOURCE_OVERLAY)) {
836 res_free(dev, fh, RESOURCE_OVERLAY);
839 /* stop video capture */
840 if (res_check(fh, RESOURCE_VIDEO)) {
841 videobuf_queue_cancel(&fh->vidq);
842 res_free(dev, fh, RESOURCE_VIDEO);
844 if (fh->vidq.read_buf) {
845 buffer_release(&fh->vidq, fh->vidq.read_buf);
846 kfree(fh->vidq.read_buf);
849 /* stop vbi capture */
850 if (res_check(fh, RESOURCE_VBI)) {
851 if (fh->vbiq.streaming)
852 videobuf_streamoff(&fh->vbiq);
853 if (fh->vbiq.reading)
854 videobuf_read_stop(&fh->vbiq);
855 res_free(dev, fh, RESOURCE_VBI);
858 videobuf_mmap_free(&fh->vidq);
859 file->private_data = NULL;
862 /* We are not putting the tuner to sleep here on exit, because
863 * we want to use the mpeg encoder in another session to capture
864 * tuner video. Closing this will result in no video to the encoder.
870 static int video_mmap(struct file *file, struct vm_area_struct *vma)
872 struct cx23885_fh *fh = file->private_data;
874 return videobuf_mmap_mapper(get_queue(fh), vma);
877 /* ------------------------------------------------------------------ */
878 /* VIDEO CTRL IOCTLS */
880 static int cx23885_get_control(struct cx23885_dev *dev,
881 struct v4l2_control *ctl)
883 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
884 call_all(dev, core, g_ctrl, ctl);
888 static int cx23885_set_control(struct cx23885_dev *dev,
889 struct v4l2_control *ctl)
891 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
892 " (disabled - no action)\n", __func__);
896 static void init_controls(struct cx23885_dev *dev)
898 struct v4l2_control ctrl;
901 for (i = 0; i < CX23885_CTLS; i++) {
902 ctrl.id = cx23885_ctls[i].v.id;
903 ctrl.value = cx23885_ctls[i].v.default_value;
905 cx23885_set_control(dev, &ctrl);
909 /* ------------------------------------------------------------------ */
912 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
913 struct v4l2_format *f)
915 struct cx23885_fh *fh = priv;
917 f->fmt.pix.width = fh->width;
918 f->fmt.pix.height = fh->height;
919 f->fmt.pix.field = fh->vidq.field;
920 f->fmt.pix.pixelformat = fh->fmt->fourcc;
921 f->fmt.pix.bytesperline =
922 (f->fmt.pix.width * fh->fmt->depth) >> 3;
923 f->fmt.pix.sizeimage =
924 f->fmt.pix.height * f->fmt.pix.bytesperline;
929 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
930 struct v4l2_format *f)
932 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
933 struct cx23885_fmt *fmt;
934 enum v4l2_field field;
935 unsigned int maxw, maxh;
937 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
941 field = f->fmt.pix.field;
942 maxw = norm_maxw(dev->tvnorm);
943 maxh = norm_maxh(dev->tvnorm);
945 if (V4L2_FIELD_ANY == field) {
946 field = (f->fmt.pix.height > maxh/2)
947 ? V4L2_FIELD_INTERLACED
953 case V4L2_FIELD_BOTTOM:
956 case V4L2_FIELD_INTERLACED:
962 f->fmt.pix.field = field;
963 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
964 &f->fmt.pix.height, 32, maxh, 0, 0);
965 f->fmt.pix.bytesperline =
966 (f->fmt.pix.width * fmt->depth) >> 3;
967 f->fmt.pix.sizeimage =
968 f->fmt.pix.height * f->fmt.pix.bytesperline;
973 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
974 struct v4l2_format *f)
976 struct cx23885_fh *fh = priv;
977 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
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 call_all(dev, video, s_fmt, f);
995 static int vidioc_querycap(struct file *file, void *priv,
996 struct v4l2_capability *cap)
998 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1000 strcpy(cap->driver, "cx23885");
1001 strlcpy(cap->card, cx23885_boards[dev->board].name,
1003 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1004 cap->version = CX23885_VERSION_CODE;
1006 V4L2_CAP_VIDEO_CAPTURE |
1007 V4L2_CAP_READWRITE |
1008 V4L2_CAP_STREAMING |
1009 V4L2_CAP_VBI_CAPTURE;
1010 if (UNSET != dev->tuner_type)
1011 cap->capabilities |= V4L2_CAP_TUNER;
1015 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1016 struct v4l2_fmtdesc *f)
1018 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1021 strlcpy(f->description, formats[f->index].name,
1022 sizeof(f->description));
1023 f->pixelformat = formats[f->index].fourcc;
1028 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1029 static int vidiocgmbuf(struct file *file, void *priv,
1030 struct video_mbuf *mbuf)
1032 struct cx23885_fh *fh = priv;
1033 struct videobuf_queue *q;
1034 struct v4l2_requestbuffers req;
1039 memset(&req, 0, sizeof(req));
1042 req.memory = V4L2_MEMORY_MMAP;
1043 err = videobuf_reqbufs(q, &req);
1047 mbuf->frames = req.count;
1049 for (i = 0; i < mbuf->frames; i++) {
1050 mbuf->offsets[i] = q->bufs[i]->boff;
1051 mbuf->size += q->bufs[i]->bsize;
1057 static int vidioc_reqbufs(struct file *file, void *priv,
1058 struct v4l2_requestbuffers *p)
1060 struct cx23885_fh *fh = priv;
1061 return videobuf_reqbufs(get_queue(fh), p);
1064 static int vidioc_querybuf(struct file *file, void *priv,
1065 struct v4l2_buffer *p)
1067 struct cx23885_fh *fh = priv;
1068 return videobuf_querybuf(get_queue(fh), p);
1071 static int vidioc_qbuf(struct file *file, void *priv,
1072 struct v4l2_buffer *p)
1074 struct cx23885_fh *fh = priv;
1075 return videobuf_qbuf(get_queue(fh), p);
1078 static int vidioc_dqbuf(struct file *file, void *priv,
1079 struct v4l2_buffer *p)
1081 struct cx23885_fh *fh = priv;
1082 return videobuf_dqbuf(get_queue(fh), p,
1083 file->f_flags & O_NONBLOCK);
1086 static int vidioc_streamon(struct file *file, void *priv,
1087 enum v4l2_buf_type i)
1089 struct cx23885_fh *fh = priv;
1090 struct cx23885_dev *dev = fh->dev;
1091 dprintk(1, "%s()\n", __func__);
1093 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1095 if (unlikely(i != fh->type))
1098 if (unlikely(!res_get(dev, fh, get_resource(fh))))
1100 return videobuf_streamon(get_queue(fh));
1103 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1105 struct cx23885_fh *fh = priv;
1106 struct cx23885_dev *dev = fh->dev;
1108 dprintk(1, "%s()\n", __func__);
1110 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1115 res = get_resource(fh);
1116 err = videobuf_streamoff(get_queue(fh));
1119 res_free(dev, fh, res);
1123 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1125 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1126 dprintk(1, "%s()\n", __func__);
1128 mutex_lock(&dev->lock);
1129 cx23885_set_tvnorm(dev, *tvnorms);
1130 mutex_unlock(&dev->lock);
1135 static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
1137 static const char *iname[] = {
1138 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1139 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1140 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1141 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1142 [CX23885_VMUX_SVIDEO] = "S-Video",
1143 [CX23885_VMUX_COMPONENT] = "Component",
1144 [CX23885_VMUX_TELEVISION] = "Television",
1145 [CX23885_VMUX_CABLE] = "Cable TV",
1146 [CX23885_VMUX_DVB] = "DVB",
1147 [CX23885_VMUX_DEBUG] = "for debug only",
1150 dprintk(1, "%s()\n", __func__);
1156 if (0 == INPUT(n)->type)
1159 memset(i, 0, sizeof(*i));
1161 i->type = V4L2_INPUT_TYPE_CAMERA;
1162 strcpy(i->name, iname[INPUT(n)->type]);
1163 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
1164 (CX23885_VMUX_CABLE == INPUT(n)->type))
1165 i->type = V4L2_INPUT_TYPE_TUNER;
1166 i->std = CX23885_NORMS;
1170 static int vidioc_enum_input(struct file *file, void *priv,
1171 struct v4l2_input *i)
1173 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1174 dprintk(1, "%s()\n", __func__);
1175 return cx23885_enum_input(dev, i);
1178 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1180 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1183 dprintk(1, "%s() returns %d\n", __func__, *i);
1187 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1189 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1191 dprintk(1, "%s(%d)\n", __func__, i);
1194 dprintk(1, "%s() -EINVAL\n", __func__);
1198 mutex_lock(&dev->lock);
1199 cx23885_video_mux(dev, i);
1200 mutex_unlock(&dev->lock);
1204 static int vidioc_queryctrl(struct file *file, void *priv,
1205 struct v4l2_queryctrl *qctrl)
1207 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1208 if (unlikely(qctrl->id == 0))
1210 return cx23885_ctrl_query(qctrl);
1213 static int vidioc_g_ctrl(struct file *file, void *priv,
1214 struct v4l2_control *ctl)
1216 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1218 return cx23885_get_control(dev, ctl);
1221 static int vidioc_s_ctrl(struct file *file, void *priv,
1222 struct v4l2_control *ctl)
1224 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1226 return cx23885_set_control(dev, ctl);
1229 static int vidioc_g_tuner(struct file *file, void *priv,
1230 struct v4l2_tuner *t)
1232 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1234 if (unlikely(UNSET == dev->tuner_type))
1239 strcpy(t->name, "Television");
1240 t->type = V4L2_TUNER_ANALOG_TV;
1241 t->capability = V4L2_TUNER_CAP_NORM;
1242 t->rangehigh = 0xffffffffUL;
1243 t->signal = 0xffff ; /* LOCKED */
1247 static int vidioc_s_tuner(struct file *file, void *priv,
1248 struct v4l2_tuner *t)
1250 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1252 if (UNSET == dev->tuner_type)
1259 static int vidioc_g_frequency(struct file *file, void *priv,
1260 struct v4l2_frequency *f)
1262 struct cx23885_fh *fh = priv;
1263 struct cx23885_dev *dev = fh->dev;
1265 if (unlikely(UNSET == dev->tuner_type))
1268 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1269 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1270 f->frequency = dev->freq;
1272 call_all(dev, tuner, g_frequency, f);
1277 static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
1279 if (unlikely(UNSET == dev->tuner_type))
1281 if (unlikely(f->tuner != 0))
1284 mutex_lock(&dev->lock);
1285 dev->freq = f->frequency;
1287 call_all(dev, tuner, s_frequency, f);
1289 /* When changing channels it is required to reset TVAUDIO */
1292 mutex_unlock(&dev->lock);
1297 static int vidioc_s_frequency(struct file *file, void *priv,
1298 struct v4l2_frequency *f)
1300 struct cx23885_fh *fh = priv;
1301 struct cx23885_dev *dev = fh->dev;
1303 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1305 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1309 cx23885_set_freq(dev, f);
1312 /* ----------------------------------------------------------- */
1314 static void cx23885_vid_timeout(unsigned long data)
1316 struct cx23885_dev *dev = (struct cx23885_dev *)data;
1317 struct cx23885_dmaqueue *q = &dev->vidq;
1318 struct cx23885_buffer *buf;
1319 unsigned long flags;
1321 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1323 cx_clear(VID_A_DMA_CTL, 0x11);
1325 spin_lock_irqsave(&dev->slock, flags);
1326 while (!list_empty(&q->active)) {
1327 buf = list_entry(q->active.next,
1328 struct cx23885_buffer, vb.queue);
1329 list_del(&buf->vb.queue);
1330 buf->vb.state = VIDEOBUF_ERROR;
1331 wake_up(&buf->vb.done);
1332 printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
1333 dev->name, buf, buf->vb.i,
1334 (unsigned long)buf->risc.dma);
1336 cx23885_restart_video_queue(dev, q);
1337 spin_unlock_irqrestore(&dev->slock, flags);
1340 int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1345 mask = cx_read(VID_A_INT_MSK);
1346 if (0 == (status & mask))
1348 cx_write(VID_A_INT_STAT, status);
1350 dprintk(2, "%s() status = 0x%08x\n", __func__, status);
1351 /* risc op code error */
1352 if (status & (1 << 16)) {
1353 printk(KERN_WARNING "%s/0: video risc op code error\n",
1355 cx_clear(VID_A_DMA_CTL, 0x11);
1356 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1360 if (status & 0x01) {
1361 spin_lock(&dev->slock);
1362 count = cx_read(VID_A_GPCNT);
1363 cx23885_video_wakeup(dev, &dev->vidq, count);
1364 spin_unlock(&dev->slock);
1368 if (status & 0x10) {
1369 dprintk(2, "stopper video\n");
1370 spin_lock(&dev->slock);
1371 cx23885_restart_video_queue(dev, &dev->vidq);
1372 spin_unlock(&dev->slock);
1379 /* ----------------------------------------------------------- */
1380 /* exported stuff */
1382 static const struct v4l2_file_operations video_fops = {
1383 .owner = THIS_MODULE,
1385 .release = video_release,
1389 .ioctl = video_ioctl2,
1392 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1393 .vidioc_querycap = vidioc_querycap,
1394 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1395 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1396 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1397 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1398 .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
1399 .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
1400 .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
1401 .vidioc_reqbufs = vidioc_reqbufs,
1402 .vidioc_querybuf = vidioc_querybuf,
1403 .vidioc_qbuf = vidioc_qbuf,
1404 .vidioc_dqbuf = vidioc_dqbuf,
1405 .vidioc_s_std = vidioc_s_std,
1406 .vidioc_enum_input = vidioc_enum_input,
1407 .vidioc_g_input = vidioc_g_input,
1408 .vidioc_s_input = vidioc_s_input,
1409 .vidioc_queryctrl = vidioc_queryctrl,
1410 .vidioc_g_ctrl = vidioc_g_ctrl,
1411 .vidioc_s_ctrl = vidioc_s_ctrl,
1412 .vidioc_streamon = vidioc_streamon,
1413 .vidioc_streamoff = vidioc_streamoff,
1414 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1415 .vidiocgmbuf = vidiocgmbuf,
1417 .vidioc_g_tuner = vidioc_g_tuner,
1418 .vidioc_s_tuner = vidioc_s_tuner,
1419 .vidioc_g_frequency = vidioc_g_frequency,
1420 .vidioc_s_frequency = vidioc_s_frequency,
1421 .vidioc_g_chip_ident = cx23885_g_chip_ident,
1422 #ifdef CONFIG_VIDEO_ADV_DEBUG
1423 .vidioc_g_register = cx23885_g_register,
1424 .vidioc_s_register = cx23885_s_register,
1428 static struct video_device cx23885_vbi_template;
1429 static struct video_device cx23885_video_template = {
1430 .name = "cx23885-video",
1431 .fops = &video_fops,
1432 .ioctl_ops = &video_ioctl_ops,
1433 .tvnorms = CX23885_NORMS,
1434 .current_norm = V4L2_STD_NTSC_M,
1437 static const struct v4l2_file_operations radio_fops = {
1438 .owner = THIS_MODULE,
1440 .release = video_release,
1441 .ioctl = video_ioctl2,
1445 void cx23885_video_unregister(struct cx23885_dev *dev)
1447 dprintk(1, "%s()\n", __func__);
1448 cx_clear(PCI_INT_MSK, 1);
1450 if (dev->video_dev) {
1451 if (video_is_registered(dev->video_dev))
1452 video_unregister_device(dev->video_dev);
1454 video_device_release(dev->video_dev);
1455 dev->video_dev = NULL;
1457 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1461 int cx23885_video_register(struct cx23885_dev *dev)
1465 dprintk(1, "%s()\n", __func__);
1466 spin_lock_init(&dev->slock);
1468 /* Initialize VBI template */
1469 memcpy(&cx23885_vbi_template, &cx23885_video_template,
1470 sizeof(cx23885_vbi_template));
1471 strcpy(cx23885_vbi_template.name, "cx23885-vbi");
1473 dev->tvnorm = cx23885_video_template.current_norm;
1475 /* init video dma queues */
1476 INIT_LIST_HEAD(&dev->vidq.active);
1477 INIT_LIST_HEAD(&dev->vidq.queued);
1478 dev->vidq.timeout.function = cx23885_vid_timeout;
1479 dev->vidq.timeout.data = (unsigned long)dev;
1480 init_timer(&dev->vidq.timeout);
1481 cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1482 VID_A_DMA_CTL, 0x11, 0x00);
1484 /* Don't enable VBI yet */
1485 cx_set(PCI_INT_MSK, 1);
1487 if (TUNER_ABSENT != dev->tuner_type) {
1488 struct v4l2_subdev *sd = NULL;
1490 if (dev->tuner_addr)
1491 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1492 &dev->i2c_bus[1].i2c_adap,
1493 "tuner", "tuner", dev->tuner_addr, NULL);
1495 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1496 &dev->i2c_bus[1].i2c_adap,
1497 "tuner", "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
1499 struct tuner_setup tun_setup;
1501 memset(&tun_setup, 0, sizeof(tun_setup));
1502 tun_setup.mode_mask = T_ANALOG_TV;
1503 tun_setup.type = dev->tuner_type;
1504 tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1505 tun_setup.tuner_callback = cx23885_tuner_callback;
1507 v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1512 /* register v4l devices */
1513 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1514 &cx23885_video_template, "video");
1515 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1518 printk(KERN_INFO "%s: can't register video device\n",
1522 printk(KERN_INFO "%s/0: registered device %s [v4l2]\n",
1523 dev->name, video_device_node_name(dev->video_dev));
1524 /* initial device configuration */
1525 mutex_lock(&dev->lock);
1526 cx23885_set_tvnorm(dev, dev->tvnorm);
1528 cx23885_video_mux(dev, 0);
1529 mutex_unlock(&dev->lock);
1534 cx23885_video_unregister(dev);