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>
38 MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
39 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
40 MODULE_LICENSE("GPL");
42 /* ------------------------------------------------------------------ */
44 static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
45 static unsigned int vbi_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
46 static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
48 module_param_array(video_nr, int, NULL, 0444);
49 module_param_array(vbi_nr, int, NULL, 0444);
50 module_param_array(radio_nr, int, NULL, 0444);
52 MODULE_PARM_DESC(video_nr, "video device numbers");
53 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
54 MODULE_PARM_DESC(radio_nr, "radio 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 "%s/0: " fmt, dev->name, ## arg);\
73 /* ------------------------------------------------------------------- */
76 #define FORMAT_FLAGS_PACKED 0x01
78 static struct cx23885_fmt formats[] = {
80 .name = "8 bpp, gray",
81 .fourcc = V4L2_PIX_FMT_GREY,
83 .flags = FORMAT_FLAGS_PACKED,
85 .name = "15 bpp RGB, le",
86 .fourcc = V4L2_PIX_FMT_RGB555,
88 .flags = FORMAT_FLAGS_PACKED,
90 .name = "15 bpp RGB, be",
91 .fourcc = V4L2_PIX_FMT_RGB555X,
93 .flags = FORMAT_FLAGS_PACKED,
95 .name = "16 bpp RGB, le",
96 .fourcc = V4L2_PIX_FMT_RGB565,
98 .flags = FORMAT_FLAGS_PACKED,
100 .name = "16 bpp RGB, be",
101 .fourcc = V4L2_PIX_FMT_RGB565X,
103 .flags = FORMAT_FLAGS_PACKED,
105 .name = "24 bpp RGB, le",
106 .fourcc = V4L2_PIX_FMT_BGR24,
108 .flags = FORMAT_FLAGS_PACKED,
110 .name = "32 bpp RGB, le",
111 .fourcc = V4L2_PIX_FMT_BGR32,
113 .flags = FORMAT_FLAGS_PACKED,
115 .name = "32 bpp RGB, be",
116 .fourcc = V4L2_PIX_FMT_RGB32,
118 .flags = FORMAT_FLAGS_PACKED,
120 .name = "4:2:2, packed, YUYV",
121 .fourcc = V4L2_PIX_FMT_YUYV,
123 .flags = FORMAT_FLAGS_PACKED,
125 .name = "4:2:2, packed, UYVY",
126 .fourcc = V4L2_PIX_FMT_UYVY,
128 .flags = FORMAT_FLAGS_PACKED,
132 static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
136 for (i = 0; i < ARRAY_SIZE(formats); i++)
137 if (formats[i].fourcc == fourcc)
140 printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __func__, fourcc);
144 /* ------------------------------------------------------------------- */
146 static const struct v4l2_queryctrl no_ctl = {
148 .flags = V4L2_CTRL_FLAG_DISABLED,
151 static struct cx23885_ctrl cx23885_ctls[] = {
155 .id = V4L2_CID_BRIGHTNESS,
156 .name = "Brightness",
160 .default_value = 0x7f,
161 .type = V4L2_CTRL_TYPE_INTEGER,
169 .id = V4L2_CID_CONTRAST,
174 .default_value = 0x3f,
175 .type = V4L2_CTRL_TYPE_INTEGER,
188 .default_value = 0x7f,
189 .type = V4L2_CTRL_TYPE_INTEGER,
196 /* strictly, this only describes only U saturation.
197 * V saturation is handled specially through code.
200 .id = V4L2_CID_SATURATION,
201 .name = "Saturation",
205 .default_value = 0x7f,
206 .type = V4L2_CTRL_TYPE_INTEGER,
215 .id = V4L2_CID_AUDIO_MUTE,
220 .type = V4L2_CTRL_TYPE_BOOLEAN,
223 .mask = (0x1f << 24),
227 .id = V4L2_CID_AUDIO_VOLUME,
232 .default_value = 0x3f,
233 .type = V4L2_CTRL_TYPE_INTEGER,
235 .reg = PATH1_VOL_CTL,
240 static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
242 /* Must be sorted from low to high control ID! */
243 static const u32 cx23885_user_ctrls[] = {
249 V4L2_CID_AUDIO_VOLUME,
254 static const u32 *ctrl_classes[] = {
259 static void cx23885_video_wakeup(struct cx23885_dev *dev,
260 struct cx23885_dmaqueue *q, u32 count)
262 struct cx23885_buffer *buf;
265 for (bc = 0;; bc++) {
266 if (list_empty(&q->active))
268 buf = list_entry(q->active.next,
269 struct cx23885_buffer, vb.queue);
271 /* count comes from the hw and is is 16bit wide --
272 * this trick handles wrap-arounds correctly for
273 * up to 32767 buffers in flight... */
274 if ((s16) (count - buf->count) < 0)
277 do_gettimeofday(&buf->vb.ts);
278 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
280 buf->vb.state = VIDEOBUF_DONE;
281 list_del(&buf->vb.queue);
282 wake_up(&buf->vb.done);
284 if (list_empty(&q->active))
285 del_timer(&q->timeout);
287 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
289 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
293 static int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
295 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
298 v4l2_norm_to_name(norm));
302 call_all(dev, tuner, s_std, norm);
307 static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
309 struct video_device *template,
312 struct video_device *vfd;
313 dprintk(1, "%s()\n", __func__);
315 vfd = video_device_alloc();
320 vfd->v4l2_dev = &dev->v4l2_dev;
321 vfd->release = video_device_release;
322 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
323 dev->name, type, cx23885_boards[dev->board].name);
327 static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
331 if (qctrl->id < V4L2_CID_BASE ||
332 qctrl->id >= V4L2_CID_LASTP1)
334 for (i = 0; i < CX23885_CTLS; i++)
335 if (cx23885_ctls[i].v.id == qctrl->id)
337 if (i == CX23885_CTLS) {
341 *qctrl = cx23885_ctls[i].v;
345 /* ------------------------------------------------------------------- */
346 /* resource management */
348 static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
351 dprintk(1, "%s()\n", __func__);
352 if (fh->resources & bit)
353 /* have it already allocated */
357 mutex_lock(&dev->lock);
358 if (dev->resources & bit) {
359 /* no, someone else uses it */
360 mutex_unlock(&dev->lock);
363 /* it's free, grab it */
364 fh->resources |= bit;
365 dev->resources |= bit;
366 dprintk(1, "res: get %d\n", bit);
367 mutex_unlock(&dev->lock);
371 static int res_check(struct cx23885_fh *fh, unsigned int bit)
373 return fh->resources & bit;
376 static int res_locked(struct cx23885_dev *dev, unsigned int bit)
378 return dev->resources & bit;
381 static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
384 BUG_ON((fh->resources & bits) != bits);
385 dprintk(1, "%s()\n", __func__);
387 mutex_lock(&dev->lock);
388 fh->resources &= ~bits;
389 dev->resources &= ~bits;
390 dprintk(1, "res: put %d\n", bits);
391 mutex_unlock(&dev->lock);
394 static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
396 struct v4l2_routing route;
397 memset(&route, 0, sizeof(route));
399 dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
401 input, INPUT(input)->vmux,
402 INPUT(input)->gpio0, INPUT(input)->gpio1,
403 INPUT(input)->gpio2, INPUT(input)->gpio3);
406 route.input = INPUT(input)->vmux;
408 /* Tell the internal A/V decoder */
409 v4l2_subdev_call(dev->sd_cx25840, video, s_routing, &route);
414 /* ------------------------------------------------------------------ */
415 static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
416 unsigned int height, enum v4l2_field field)
418 dprintk(1, "%s()\n", __func__);
422 static int cx23885_start_video_dma(struct cx23885_dev *dev,
423 struct cx23885_dmaqueue *q,
424 struct cx23885_buffer *buf)
426 dprintk(1, "%s()\n", __func__);
428 /* setup fifo + format */
429 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
430 buf->bpl, buf->risc.dma);
431 cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
434 cx_write(VID_A_GPCNT_CTL, 3);
438 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
439 cx_set(VID_A_INT_MSK, 0x000011);
442 cx_set(DEV_CNTRL2, (1<<5));
443 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
449 static int cx23885_restart_video_queue(struct cx23885_dev *dev,
450 struct cx23885_dmaqueue *q)
452 struct cx23885_buffer *buf, *prev;
453 struct list_head *item;
454 dprintk(1, "%s()\n", __func__);
456 if (!list_empty(&q->active)) {
457 buf = list_entry(q->active.next, struct cx23885_buffer,
459 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
461 cx23885_start_video_dma(dev, q, buf);
462 list_for_each(item, &q->active) {
463 buf = list_entry(item, struct cx23885_buffer,
465 buf->count = q->count++;
467 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
473 if (list_empty(&q->queued))
475 buf = list_entry(q->queued.next, struct cx23885_buffer,
478 list_move_tail(&buf->vb.queue, &q->active);
479 cx23885_start_video_dma(dev, q, buf);
480 buf->vb.state = VIDEOBUF_ACTIVE;
481 buf->count = q->count++;
482 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
483 dprintk(2, "[%p/%d] restart_queue - first active\n",
486 } else if (prev->vb.width == buf->vb.width &&
487 prev->vb.height == buf->vb.height &&
488 prev->fmt == buf->fmt) {
489 list_move_tail(&buf->vb.queue, &q->active);
490 buf->vb.state = VIDEOBUF_ACTIVE;
491 buf->count = q->count++;
492 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
493 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
494 dprintk(2, "[%p/%d] restart_queue - move to active\n",
503 static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
506 struct cx23885_fh *fh = q->priv_data;
508 *size = fh->fmt->depth*fh->width*fh->height >> 3;
511 while (*size * *count > vid_limit * 1024 * 1024)
516 static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
517 enum v4l2_field field)
519 struct cx23885_fh *fh = q->priv_data;
520 struct cx23885_dev *dev = fh->dev;
521 struct cx23885_buffer *buf =
522 container_of(vb, struct cx23885_buffer, vb);
523 int rc, init_buffer = 0;
524 u32 line0_offset, line1_offset;
525 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
527 BUG_ON(NULL == fh->fmt);
528 if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
529 fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
531 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
532 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
535 if (buf->fmt != fh->fmt ||
536 buf->vb.width != fh->width ||
537 buf->vb.height != fh->height ||
538 buf->vb.field != field) {
540 buf->vb.width = fh->width;
541 buf->vb.height = fh->height;
542 buf->vb.field = field;
546 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
548 rc = videobuf_iolock(q, &buf->vb, NULL);
554 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
555 switch (buf->vb.field) {
557 cx23885_risc_buffer(dev->pci, &buf->risc,
558 dma->sglist, 0, UNSET,
559 buf->bpl, 0, buf->vb.height);
561 case V4L2_FIELD_BOTTOM:
562 cx23885_risc_buffer(dev->pci, &buf->risc,
563 dma->sglist, UNSET, 0,
564 buf->bpl, 0, buf->vb.height);
566 case V4L2_FIELD_INTERLACED:
567 if (dev->tvnorm & V4L2_STD_NTSC) {
568 /* cx25840 transmits NTSC bottom field first */
569 dprintk(1, "%s() Creating NTSC risc\n",
571 line0_offset = buf->bpl;
574 /* All other formats are top field first */
575 dprintk(1, "%s() Creating PAL/SECAM risc\n",
578 line1_offset = buf->bpl;
580 cx23885_risc_buffer(dev->pci, &buf->risc,
581 dma->sglist, line0_offset,
584 buf->vb.height >> 1);
586 case V4L2_FIELD_SEQ_TB:
587 cx23885_risc_buffer(dev->pci, &buf->risc,
589 0, buf->bpl * (buf->vb.height >> 1),
591 buf->vb.height >> 1);
593 case V4L2_FIELD_SEQ_BT:
594 cx23885_risc_buffer(dev->pci, &buf->risc,
596 buf->bpl * (buf->vb.height >> 1), 0,
598 buf->vb.height >> 1);
604 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
606 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
607 (unsigned long)buf->risc.dma);
609 buf->vb.state = VIDEOBUF_PREPARED;
613 cx23885_free_buffer(q, buf);
617 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
619 struct cx23885_buffer *buf = container_of(vb,
620 struct cx23885_buffer, vb);
621 struct cx23885_buffer *prev;
622 struct cx23885_fh *fh = vq->priv_data;
623 struct cx23885_dev *dev = fh->dev;
624 struct cx23885_dmaqueue *q = &dev->vidq;
626 /* add jump to stopper */
627 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
628 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
629 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
631 if (!list_empty(&q->queued)) {
632 list_add_tail(&buf->vb.queue, &q->queued);
633 buf->vb.state = VIDEOBUF_QUEUED;
634 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
637 } else if (list_empty(&q->active)) {
638 list_add_tail(&buf->vb.queue, &q->active);
639 cx23885_start_video_dma(dev, q, buf);
640 buf->vb.state = VIDEOBUF_ACTIVE;
641 buf->count = q->count++;
642 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
643 dprintk(2, "[%p/%d] buffer_queue - first active\n",
647 prev = list_entry(q->active.prev, struct cx23885_buffer,
649 if (prev->vb.width == buf->vb.width &&
650 prev->vb.height == buf->vb.height &&
651 prev->fmt == buf->fmt) {
652 list_add_tail(&buf->vb.queue, &q->active);
653 buf->vb.state = VIDEOBUF_ACTIVE;
654 buf->count = q->count++;
655 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
656 /* 64 bit bits 63-32 */
657 prev->risc.jmp[2] = cpu_to_le32(0);
658 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
662 list_add_tail(&buf->vb.queue, &q->queued);
663 buf->vb.state = VIDEOBUF_QUEUED;
664 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
670 static void buffer_release(struct videobuf_queue *q,
671 struct videobuf_buffer *vb)
673 struct cx23885_buffer *buf = container_of(vb,
674 struct cx23885_buffer, vb);
676 cx23885_free_buffer(q, buf);
679 static struct videobuf_queue_ops cx23885_video_qops = {
680 .buf_setup = buffer_setup,
681 .buf_prepare = buffer_prepare,
682 .buf_queue = buffer_queue,
683 .buf_release = buffer_release,
686 static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
689 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
691 case V4L2_BUF_TYPE_VBI_CAPTURE:
699 static int get_resource(struct cx23885_fh *fh)
702 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
703 return RESOURCE_VIDEO;
704 case V4L2_BUF_TYPE_VBI_CAPTURE:
712 static int video_open(struct file *file)
714 int minor = video_devdata(file)->minor;
715 struct cx23885_dev *h, *dev = NULL;
716 struct cx23885_fh *fh;
717 struct list_head *list;
718 enum v4l2_buf_type type = 0;
722 list_for_each(list, &cx23885_devlist) {
723 h = list_entry(list, struct cx23885_dev, devlist);
725 h->video_dev->minor == minor) {
727 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
730 h->vbi_dev->minor == minor) {
732 type = V4L2_BUF_TYPE_VBI_CAPTURE;
735 h->radio_dev->minor == minor) {
745 dprintk(1, "open minor=%d radio=%d type=%s\n",
746 minor, radio, v4l2_type_names[type]);
748 /* allocate + initialize per filehandle data */
749 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
754 file->private_data = fh;
760 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
762 videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
763 &dev->pci->dev, &dev->slock,
764 V4L2_BUF_TYPE_VIDEO_CAPTURE,
765 V4L2_FIELD_INTERLACED,
766 sizeof(struct cx23885_buffer),
769 dprintk(1, "post videobuf_queue_init()\n");
776 static ssize_t video_read(struct file *file, char __user *data,
777 size_t count, loff_t *ppos)
779 struct cx23885_fh *fh = file->private_data;
782 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
783 if (res_locked(fh->dev, RESOURCE_VIDEO))
785 return videobuf_read_one(&fh->vidq, data, count, ppos,
786 file->f_flags & O_NONBLOCK);
787 case V4L2_BUF_TYPE_VBI_CAPTURE:
788 if (!res_get(fh->dev, fh, RESOURCE_VBI))
790 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
791 file->f_flags & O_NONBLOCK);
798 static unsigned int video_poll(struct file *file,
799 struct poll_table_struct *wait)
801 struct cx23885_fh *fh = file->private_data;
802 struct cx23885_buffer *buf;
804 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
805 if (!res_get(fh->dev, fh, RESOURCE_VBI))
807 return videobuf_poll_stream(file, &fh->vbiq, wait);
810 if (res_check(fh, RESOURCE_VIDEO)) {
811 /* streaming capture */
812 if (list_empty(&fh->vidq.stream))
814 buf = list_entry(fh->vidq.stream.next,
815 struct cx23885_buffer, vb.stream);
818 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
822 poll_wait(file, &buf->vb.done, wait);
823 if (buf->vb.state == VIDEOBUF_DONE ||
824 buf->vb.state == VIDEOBUF_ERROR)
825 return POLLIN|POLLRDNORM;
829 static int video_release(struct file *file)
831 struct cx23885_fh *fh = file->private_data;
832 struct cx23885_dev *dev = fh->dev;
834 /* turn off overlay */
835 if (res_check(fh, RESOURCE_OVERLAY)) {
837 res_free(dev, fh, RESOURCE_OVERLAY);
840 /* stop video capture */
841 if (res_check(fh, RESOURCE_VIDEO)) {
842 videobuf_queue_cancel(&fh->vidq);
843 res_free(dev, fh, RESOURCE_VIDEO);
845 if (fh->vidq.read_buf) {
846 buffer_release(&fh->vidq, fh->vidq.read_buf);
847 kfree(fh->vidq.read_buf);
850 /* stop vbi capture */
851 if (res_check(fh, RESOURCE_VBI)) {
852 if (fh->vbiq.streaming)
853 videobuf_streamoff(&fh->vbiq);
854 if (fh->vbiq.reading)
855 videobuf_read_stop(&fh->vbiq);
856 res_free(dev, fh, RESOURCE_VBI);
859 videobuf_mmap_free(&fh->vidq);
860 file->private_data = NULL;
863 /* We are not putting the tuner to sleep here on exit, because
864 * we want to use the mpeg encoder in another session to capture
865 * tuner video. Closing this will result in no video to the encoder.
871 static int video_mmap(struct file *file, struct vm_area_struct *vma)
873 struct cx23885_fh *fh = file->private_data;
875 return videobuf_mmap_mapper(get_queue(fh), vma);
878 /* ------------------------------------------------------------------ */
879 /* VIDEO CTRL IOCTLS */
881 static int cx23885_get_control(struct cx23885_dev *dev,
882 struct v4l2_control *ctl)
884 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
885 call_all(dev, core, g_ctrl, ctl);
889 static int cx23885_set_control(struct cx23885_dev *dev,
890 struct v4l2_control *ctl)
892 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
893 " (disabled - no action)\n", __func__);
897 static void init_controls(struct cx23885_dev *dev)
899 struct v4l2_control ctrl;
902 for (i = 0; i < CX23885_CTLS; i++) {
903 ctrl.id = cx23885_ctls[i].v.id;
904 ctrl.value = cx23885_ctls[i].v.default_value;
906 cx23885_set_control(dev, &ctrl);
910 /* ------------------------------------------------------------------ */
913 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
914 struct v4l2_format *f)
916 struct cx23885_fh *fh = priv;
918 f->fmt.pix.width = fh->width;
919 f->fmt.pix.height = fh->height;
920 f->fmt.pix.field = fh->vidq.field;
921 f->fmt.pix.pixelformat = fh->fmt->fourcc;
922 f->fmt.pix.bytesperline =
923 (f->fmt.pix.width * fh->fmt->depth) >> 3;
924 f->fmt.pix.sizeimage =
925 f->fmt.pix.height * f->fmt.pix.bytesperline;
930 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
931 struct v4l2_format *f)
933 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
934 struct cx23885_fmt *fmt;
935 enum v4l2_field field;
936 unsigned int maxw, maxh;
938 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
942 field = f->fmt.pix.field;
943 maxw = norm_maxw(dev->tvnorm);
944 maxh = norm_maxh(dev->tvnorm);
946 if (V4L2_FIELD_ANY == field) {
947 field = (f->fmt.pix.height > maxh/2)
948 ? V4L2_FIELD_INTERLACED
954 case V4L2_FIELD_BOTTOM:
957 case V4L2_FIELD_INTERLACED:
963 f->fmt.pix.field = field;
964 if (f->fmt.pix.height < 32)
965 f->fmt.pix.height = 32;
966 if (f->fmt.pix.height > maxh)
967 f->fmt.pix.height = maxh;
968 if (f->fmt.pix.width < 48)
969 f->fmt.pix.width = 48;
970 if (f->fmt.pix.width > maxw)
971 f->fmt.pix.width = maxw;
972 f->fmt.pix.width &= ~0x03;
973 f->fmt.pix.bytesperline =
974 (f->fmt.pix.width * fmt->depth) >> 3;
975 f->fmt.pix.sizeimage =
976 f->fmt.pix.height * f->fmt.pix.bytesperline;
981 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
982 struct v4l2_format *f)
984 struct cx23885_fh *fh = priv;
985 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
988 dprintk(2, "%s()\n", __func__);
989 err = vidioc_try_fmt_vid_cap(file, priv, f);
993 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
994 fh->width = f->fmt.pix.width;
995 fh->height = f->fmt.pix.height;
996 fh->vidq.field = f->fmt.pix.field;
997 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
998 fh->width, fh->height, fh->vidq.field);
999 call_all(dev, video, s_fmt, f);
1003 static int vidioc_querycap(struct file *file, void *priv,
1004 struct v4l2_capability *cap)
1006 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1008 strcpy(cap->driver, "cx23885");
1009 strlcpy(cap->card, cx23885_boards[dev->board].name,
1011 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1012 cap->version = CX23885_VERSION_CODE;
1014 V4L2_CAP_VIDEO_CAPTURE |
1015 V4L2_CAP_READWRITE |
1016 V4L2_CAP_STREAMING |
1017 V4L2_CAP_VBI_CAPTURE;
1018 if (UNSET != dev->tuner_type)
1019 cap->capabilities |= V4L2_CAP_TUNER;
1023 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1024 struct v4l2_fmtdesc *f)
1026 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1029 strlcpy(f->description, formats[f->index].name,
1030 sizeof(f->description));
1031 f->pixelformat = formats[f->index].fourcc;
1036 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1037 static int vidiocgmbuf(struct file *file, void *priv,
1038 struct video_mbuf *mbuf)
1040 struct cx23885_fh *fh = priv;
1041 struct videobuf_queue *q;
1042 struct v4l2_requestbuffers req;
1047 memset(&req, 0, sizeof(req));
1050 req.memory = V4L2_MEMORY_MMAP;
1051 err = videobuf_reqbufs(q, &req);
1055 mbuf->frames = req.count;
1057 for (i = 0; i < mbuf->frames; i++) {
1058 mbuf->offsets[i] = q->bufs[i]->boff;
1059 mbuf->size += q->bufs[i]->bsize;
1065 static int vidioc_reqbufs(struct file *file, void *priv,
1066 struct v4l2_requestbuffers *p)
1068 struct cx23885_fh *fh = priv;
1069 return videobuf_reqbufs(get_queue(fh), p);
1072 static int vidioc_querybuf(struct file *file, void *priv,
1073 struct v4l2_buffer *p)
1075 struct cx23885_fh *fh = priv;
1076 return videobuf_querybuf(get_queue(fh), p);
1079 static int vidioc_qbuf(struct file *file, void *priv,
1080 struct v4l2_buffer *p)
1082 struct cx23885_fh *fh = priv;
1083 return videobuf_qbuf(get_queue(fh), p);
1086 static int vidioc_dqbuf(struct file *file, void *priv,
1087 struct v4l2_buffer *p)
1089 struct cx23885_fh *fh = priv;
1090 return videobuf_dqbuf(get_queue(fh), p,
1091 file->f_flags & O_NONBLOCK);
1094 static int vidioc_streamon(struct file *file, void *priv,
1095 enum v4l2_buf_type i)
1097 struct cx23885_fh *fh = priv;
1098 struct cx23885_dev *dev = fh->dev;
1099 dprintk(1, "%s()\n", __func__);
1101 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1103 if (unlikely(i != fh->type))
1106 if (unlikely(!res_get(dev, fh, get_resource(fh))))
1108 return videobuf_streamon(get_queue(fh));
1111 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1113 struct cx23885_fh *fh = priv;
1114 struct cx23885_dev *dev = fh->dev;
1116 dprintk(1, "%s()\n", __func__);
1118 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1123 res = get_resource(fh);
1124 err = videobuf_streamoff(get_queue(fh));
1127 res_free(dev, fh, res);
1131 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1133 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1134 dprintk(1, "%s()\n", __func__);
1136 mutex_lock(&dev->lock);
1137 cx23885_set_tvnorm(dev, *tvnorms);
1138 mutex_unlock(&dev->lock);
1143 static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
1145 static const char *iname[] = {
1146 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1147 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1148 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1149 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1150 [CX23885_VMUX_SVIDEO] = "S-Video",
1151 [CX23885_VMUX_TELEVISION] = "Television",
1152 [CX23885_VMUX_CABLE] = "Cable TV",
1153 [CX23885_VMUX_DVB] = "DVB",
1154 [CX23885_VMUX_DEBUG] = "for debug only",
1157 dprintk(1, "%s()\n", __func__);
1163 if (0 == INPUT(n)->type)
1166 memset(i, 0, sizeof(*i));
1168 i->type = V4L2_INPUT_TYPE_CAMERA;
1169 strcpy(i->name, iname[INPUT(n)->type]);
1170 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
1171 (CX23885_VMUX_CABLE == INPUT(n)->type))
1172 i->type = V4L2_INPUT_TYPE_TUNER;
1173 i->std = CX23885_NORMS;
1177 static int vidioc_enum_input(struct file *file, void *priv,
1178 struct v4l2_input *i)
1180 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1181 dprintk(1, "%s()\n", __func__);
1182 return cx23885_enum_input(dev, i);
1185 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1187 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1190 dprintk(1, "%s() returns %d\n", __func__, *i);
1194 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1196 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1198 dprintk(1, "%s(%d)\n", __func__, i);
1201 dprintk(1, "%s() -EINVAL\n", __func__);
1205 mutex_lock(&dev->lock);
1206 cx23885_video_mux(dev, i);
1207 mutex_unlock(&dev->lock);
1211 static int vidioc_queryctrl(struct file *file, void *priv,
1212 struct v4l2_queryctrl *qctrl)
1214 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1215 if (unlikely(qctrl->id == 0))
1217 return cx23885_ctrl_query(qctrl);
1220 static int vidioc_g_ctrl(struct file *file, void *priv,
1221 struct v4l2_control *ctl)
1223 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1225 return cx23885_get_control(dev, ctl);
1228 static int vidioc_s_ctrl(struct file *file, void *priv,
1229 struct v4l2_control *ctl)
1231 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1233 return cx23885_set_control(dev, ctl);
1236 static int vidioc_g_tuner(struct file *file, void *priv,
1237 struct v4l2_tuner *t)
1239 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1241 if (unlikely(UNSET == dev->tuner_type))
1246 strcpy(t->name, "Television");
1247 t->type = V4L2_TUNER_ANALOG_TV;
1248 t->capability = V4L2_TUNER_CAP_NORM;
1249 t->rangehigh = 0xffffffffUL;
1250 t->signal = 0xffff ; /* LOCKED */
1254 static int vidioc_s_tuner(struct file *file, void *priv,
1255 struct v4l2_tuner *t)
1257 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1259 if (UNSET == dev->tuner_type)
1266 static int vidioc_g_frequency(struct file *file, void *priv,
1267 struct v4l2_frequency *f)
1269 struct cx23885_fh *fh = priv;
1270 struct cx23885_dev *dev = fh->dev;
1272 if (unlikely(UNSET == dev->tuner_type))
1275 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1276 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1277 f->frequency = dev->freq;
1279 call_all(dev, tuner, g_frequency, f);
1284 static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
1286 if (unlikely(UNSET == dev->tuner_type))
1288 if (unlikely(f->tuner != 0))
1291 mutex_lock(&dev->lock);
1292 dev->freq = f->frequency;
1294 call_all(dev, tuner, s_frequency, f);
1296 /* When changing channels it is required to reset TVAUDIO */
1299 mutex_unlock(&dev->lock);
1304 static int vidioc_s_frequency(struct file *file, void *priv,
1305 struct v4l2_frequency *f)
1307 struct cx23885_fh *fh = priv;
1308 struct cx23885_dev *dev = fh->dev;
1310 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1312 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1316 cx23885_set_freq(dev, f);
1319 #ifdef CONFIG_VIDEO_ADV_DEBUG
1320 static int vidioc_g_register(struct file *file, void *fh,
1321 struct v4l2_dbg_register *reg)
1323 struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1325 if (!v4l2_chip_match_host(®->match))
1328 call_all(dev, core, g_register, reg);
1333 static int vidioc_s_register(struct file *file, void *fh,
1334 struct v4l2_dbg_register *reg)
1336 struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1338 if (!v4l2_chip_match_host(®->match))
1341 call_all(dev, core, s_register, reg);
1347 /* ----------------------------------------------------------- */
1349 static void cx23885_vid_timeout(unsigned long data)
1351 struct cx23885_dev *dev = (struct cx23885_dev *)data;
1352 struct cx23885_dmaqueue *q = &dev->vidq;
1353 struct cx23885_buffer *buf;
1354 unsigned long flags;
1356 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1358 cx_clear(VID_A_DMA_CTL, 0x11);
1360 spin_lock_irqsave(&dev->slock, flags);
1361 while (!list_empty(&q->active)) {
1362 buf = list_entry(q->active.next,
1363 struct cx23885_buffer, vb.queue);
1364 list_del(&buf->vb.queue);
1365 buf->vb.state = VIDEOBUF_ERROR;
1366 wake_up(&buf->vb.done);
1367 printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
1368 dev->name, buf, buf->vb.i,
1369 (unsigned long)buf->risc.dma);
1371 cx23885_restart_video_queue(dev, q);
1372 spin_unlock_irqrestore(&dev->slock, flags);
1375 int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1380 mask = cx_read(VID_A_INT_MSK);
1381 if (0 == (status & mask))
1383 cx_write(VID_A_INT_STAT, status);
1385 dprintk(2, "%s() status = 0x%08x\n", __func__, status);
1386 /* risc op code error */
1387 if (status & (1 << 16)) {
1388 printk(KERN_WARNING "%s/0: video risc op code error\n",
1390 cx_clear(VID_A_DMA_CTL, 0x11);
1391 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1395 if (status & 0x01) {
1396 spin_lock(&dev->slock);
1397 count = cx_read(VID_A_GPCNT);
1398 cx23885_video_wakeup(dev, &dev->vidq, count);
1399 spin_unlock(&dev->slock);
1403 if (status & 0x10) {
1404 dprintk(2, "stopper video\n");
1405 spin_lock(&dev->slock);
1406 cx23885_restart_video_queue(dev, &dev->vidq);
1407 spin_unlock(&dev->slock);
1414 /* ----------------------------------------------------------- */
1415 /* exported stuff */
1417 static const struct v4l2_file_operations video_fops = {
1418 .owner = THIS_MODULE,
1420 .release = video_release,
1424 .ioctl = video_ioctl2,
1427 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1428 .vidioc_querycap = vidioc_querycap,
1429 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1430 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1431 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1432 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1433 .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
1434 .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
1435 .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
1436 .vidioc_reqbufs = vidioc_reqbufs,
1437 .vidioc_querybuf = vidioc_querybuf,
1438 .vidioc_qbuf = vidioc_qbuf,
1439 .vidioc_dqbuf = vidioc_dqbuf,
1440 .vidioc_s_std = vidioc_s_std,
1441 .vidioc_enum_input = vidioc_enum_input,
1442 .vidioc_g_input = vidioc_g_input,
1443 .vidioc_s_input = vidioc_s_input,
1444 .vidioc_queryctrl = vidioc_queryctrl,
1445 .vidioc_g_ctrl = vidioc_g_ctrl,
1446 .vidioc_s_ctrl = vidioc_s_ctrl,
1447 .vidioc_streamon = vidioc_streamon,
1448 .vidioc_streamoff = vidioc_streamoff,
1449 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1450 .vidiocgmbuf = vidiocgmbuf,
1452 .vidioc_g_tuner = vidioc_g_tuner,
1453 .vidioc_s_tuner = vidioc_s_tuner,
1454 .vidioc_g_frequency = vidioc_g_frequency,
1455 .vidioc_s_frequency = vidioc_s_frequency,
1456 #ifdef CONFIG_VIDEO_ADV_DEBUG
1457 .vidioc_g_register = vidioc_g_register,
1458 .vidioc_s_register = vidioc_s_register,
1462 static struct video_device cx23885_vbi_template;
1463 static struct video_device cx23885_video_template = {
1464 .name = "cx23885-video",
1465 .fops = &video_fops,
1467 .ioctl_ops = &video_ioctl_ops,
1468 .tvnorms = CX23885_NORMS,
1469 .current_norm = V4L2_STD_NTSC_M,
1472 static const struct v4l2_file_operations radio_fops = {
1473 .owner = THIS_MODULE,
1475 .release = video_release,
1476 .ioctl = video_ioctl2,
1480 void cx23885_video_unregister(struct cx23885_dev *dev)
1482 dprintk(1, "%s()\n", __func__);
1483 cx_clear(PCI_INT_MSK, 1);
1485 if (dev->video_dev) {
1486 if (-1 != dev->video_dev->minor)
1487 video_unregister_device(dev->video_dev);
1489 video_device_release(dev->video_dev);
1490 dev->video_dev = NULL;
1492 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1496 int cx23885_video_register(struct cx23885_dev *dev)
1500 dprintk(1, "%s()\n", __func__);
1501 spin_lock_init(&dev->slock);
1503 /* Initialize VBI template */
1504 memcpy(&cx23885_vbi_template, &cx23885_video_template,
1505 sizeof(cx23885_vbi_template));
1506 strcpy(cx23885_vbi_template.name, "cx23885-vbi");
1508 dev->tvnorm = cx23885_video_template.current_norm;
1510 /* init video dma queues */
1511 INIT_LIST_HEAD(&dev->vidq.active);
1512 INIT_LIST_HEAD(&dev->vidq.queued);
1513 dev->vidq.timeout.function = cx23885_vid_timeout;
1514 dev->vidq.timeout.data = (unsigned long)dev;
1515 init_timer(&dev->vidq.timeout);
1516 cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1517 VID_A_DMA_CTL, 0x11, 0x00);
1519 /* Don't enable VBI yet */
1520 cx_set(PCI_INT_MSK, 1);
1522 if (TUNER_ABSENT != dev->tuner_type) {
1523 struct v4l2_subdev *sd = NULL;
1525 if (dev->tuner_addr)
1526 sd = v4l2_i2c_new_subdev(&dev->i2c_bus[1].i2c_adap,
1527 "tuner", "tuner", dev->tuner_addr);
1529 sd = v4l2_i2c_new_probed_subdev(&dev->i2c_bus[1].i2c_adap,
1530 "tuner", "tuner", v4l2_i2c_tuner_addrs(ADDRS_TV));
1532 struct tuner_setup tun_setup;
1534 tun_setup.mode_mask = T_ANALOG_TV;
1535 tun_setup.type = dev->tuner_type;
1536 tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1538 v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1543 /* register v4l devices */
1544 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1545 &cx23885_video_template, "video");
1546 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1549 printk(KERN_INFO "%s: can't register video device\n",
1553 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1554 dev->name, dev->video_dev->num);
1555 /* initial device configuration */
1556 mutex_lock(&dev->lock);
1557 cx23885_set_tvnorm(dev, dev->tvnorm);
1559 cx23885_video_mux(dev, 0);
1560 mutex_unlock(&dev->lock);
1565 cx23885_video_unregister(dev);