2 * Driver for the Conexant CX23885 PCIe bridge
4 * Copyright (c) 2007 Steven Toth <stoth@hauppauge.com>
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>
37 #ifdef CONFIG_VIDEO_V4L1_COMPAT
38 /* Include V4L1 specific functions. Should be removed soon */
39 #include <linux/videodev.h>
42 MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
43 MODULE_AUTHOR("Steven Toth <stoth@hauppauge.com>");
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 = 0x7f,
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 = 0x7f,
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 const u32 cx23885_user_ctrls[] = {
252 V4L2_CID_AUDIO_VOLUME,
256 EXPORT_SYMBOL(cx23885_user_ctrls);
258 static const u32 *ctrl_classes[] = {
263 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);
294 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
298 int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
300 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
303 v4l2_norm_to_name(norm));
307 /* Tell the analog tuner/demods */
308 cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_STD, &norm);
310 /* Tell the internal A/V decoder */
311 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_STD, &norm);
316 struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
318 struct video_device *template,
321 struct video_device *vfd;
322 dprintk(1, "%s()\n", __func__);
324 vfd = video_device_alloc();
329 vfd->dev = &pci->dev;
330 vfd->release = video_device_release;
331 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
332 dev->name, type, cx23885_boards[dev->board].name);
336 int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
340 if (qctrl->id < V4L2_CID_BASE ||
341 qctrl->id >= V4L2_CID_LASTP1)
343 for (i = 0; i < CX23885_CTLS; i++)
344 if (cx23885_ctls[i].v.id == qctrl->id)
346 if (i == CX23885_CTLS) {
350 *qctrl = cx23885_ctls[i].v;
353 EXPORT_SYMBOL(cx23885_ctrl_query);
355 /* ------------------------------------------------------------------- */
356 /* resource management */
358 static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
361 dprintk(1, "%s()\n", __func__);
362 if (fh->resources & bit)
363 /* have it already allocated */
367 mutex_lock(&dev->lock);
368 if (dev->resources & bit) {
369 /* no, someone else uses it */
370 mutex_unlock(&dev->lock);
373 /* it's free, grab it */
374 fh->resources |= bit;
375 dev->resources |= bit;
376 dprintk(1, "res: get %d\n", bit);
377 mutex_unlock(&dev->lock);
381 static int res_check(struct cx23885_fh *fh, unsigned int bit)
383 return (fh->resources & bit);
386 static int res_locked(struct cx23885_dev *dev, unsigned int bit)
388 return (dev->resources & bit);
391 static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
394 BUG_ON((fh->resources & bits) != bits);
395 dprintk(1, "%s()\n", __func__);
397 mutex_lock(&dev->lock);
398 fh->resources &= ~bits;
399 dev->resources &= ~bits;
400 dprintk(1, "res: put %d\n", bits);
401 mutex_unlock(&dev->lock);
404 int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
406 struct v4l2_routing route;
407 memset(&route, 0, sizeof(route));
409 dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
411 input, INPUT(input)->vmux,
412 INPUT(input)->gpio0, INPUT(input)->gpio1,
413 INPUT(input)->gpio2, INPUT(input)->gpio3);
416 route.input = INPUT(input)->vmux;
418 /* Tell the internal A/V decoder */
419 cx23885_call_i2c_clients(&dev->i2c_bus[2],
420 VIDIOC_INT_S_VIDEO_ROUTING, &route);
424 EXPORT_SYMBOL(cx23885_video_mux);
426 /* ------------------------------------------------------------------ */
427 int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
428 unsigned int height, enum v4l2_field field)
430 dprintk(1, "%s()\n", __func__);
434 static int cx23885_start_video_dma(struct cx23885_dev *dev,
435 struct cx23885_dmaqueue *q,
436 struct cx23885_buffer *buf)
438 dprintk(1, "%s()\n", __func__);
440 /* setup fifo + format */
441 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
442 buf->bpl, buf->risc.dma);
443 cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
446 cx_write(VID_A_GPCNT_CTL, 3);
450 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
451 cx_set(VID_A_INT_MSK, 0x000011);
454 cx_set(DEV_CNTRL2, (1<<5));
455 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
461 static int cx23885_restart_video_queue(struct cx23885_dev *dev,
462 struct cx23885_dmaqueue *q)
464 struct cx23885_buffer *buf, *prev;
465 struct list_head *item;
466 dprintk(1, "%s()\n", __func__);
468 if (!list_empty(&q->active)) {
469 buf = list_entry(q->active.next, struct cx23885_buffer,
471 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
473 cx23885_start_video_dma(dev, q, buf);
474 list_for_each(item, &q->active) {
475 buf = list_entry(item, struct cx23885_buffer,
477 buf->count = q->count++;
479 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
485 if (list_empty(&q->queued))
487 buf = list_entry(q->queued.next, struct cx23885_buffer,
490 list_move_tail(&buf->vb.queue, &q->active);
491 cx23885_start_video_dma(dev, q, buf);
492 buf->vb.state = VIDEOBUF_ACTIVE;
493 buf->count = q->count++;
494 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
495 dprintk(2, "[%p/%d] restart_queue - first active\n",
498 } else if (prev->vb.width == buf->vb.width &&
499 prev->vb.height == buf->vb.height &&
500 prev->fmt == buf->fmt) {
501 list_move_tail(&buf->vb.queue, &q->active);
502 buf->vb.state = VIDEOBUF_ACTIVE;
503 buf->count = q->count++;
504 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
505 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
506 dprintk(2, "[%p/%d] restart_queue - move to active\n",
515 static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
518 struct cx23885_fh *fh = q->priv_data;
520 *size = fh->fmt->depth*fh->width*fh->height >> 3;
523 while (*size * *count > vid_limit * 1024 * 1024)
528 static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
529 enum v4l2_field field)
531 struct cx23885_fh *fh = q->priv_data;
532 struct cx23885_dev *dev = fh->dev;
533 struct cx23885_buffer *buf =
534 container_of(vb, struct cx23885_buffer, vb);
535 int rc, init_buffer = 0;
536 u32 line0_offset, line1_offset;
537 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
539 BUG_ON(NULL == fh->fmt);
540 if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
541 fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
543 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
544 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
547 if (buf->fmt != fh->fmt ||
548 buf->vb.width != fh->width ||
549 buf->vb.height != fh->height ||
550 buf->vb.field != field) {
552 buf->vb.width = fh->width;
553 buf->vb.height = fh->height;
554 buf->vb.field = field;
558 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
560 rc = videobuf_iolock(q, &buf->vb, NULL);
566 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
567 switch (buf->vb.field) {
569 cx23885_risc_buffer(dev->pci, &buf->risc,
570 dma->sglist, 0, UNSET,
571 buf->bpl, 0, buf->vb.height);
573 case V4L2_FIELD_BOTTOM:
574 cx23885_risc_buffer(dev->pci, &buf->risc,
575 dma->sglist, UNSET, 0,
576 buf->bpl, 0, buf->vb.height);
578 case V4L2_FIELD_INTERLACED:
579 if (dev->tvnorm & V4L2_STD_NTSC) {
580 /* cx25840 transmits NTSC bottom field first */
581 dprintk(1, "%s() Creating NTSC risc\n",
583 line0_offset = buf->bpl;
586 /* All other formats are top field first */
587 dprintk(1, "%s() Creating PAL/SECAM risc\n",
590 line1_offset = buf->bpl;
592 cx23885_risc_buffer(dev->pci, &buf->risc,
593 dma->sglist, line0_offset,
596 buf->vb.height >> 1);
598 case V4L2_FIELD_SEQ_TB:
599 cx23885_risc_buffer(dev->pci, &buf->risc,
601 0, buf->bpl * (buf->vb.height >> 1),
603 buf->vb.height >> 1);
605 case V4L2_FIELD_SEQ_BT:
606 cx23885_risc_buffer(dev->pci, &buf->risc,
608 buf->bpl * (buf->vb.height >> 1), 0,
610 buf->vb.height >> 1);
616 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
618 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
619 (unsigned long)buf->risc.dma);
621 buf->vb.state = VIDEOBUF_PREPARED;
625 cx23885_free_buffer(q, buf);
629 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
631 struct cx23885_buffer *buf = container_of(vb,
632 struct cx23885_buffer, vb);
633 struct cx23885_buffer *prev;
634 struct cx23885_fh *fh = vq->priv_data;
635 struct cx23885_dev *dev = fh->dev;
636 struct cx23885_dmaqueue *q = &dev->vidq;
638 /* add jump to stopper */
639 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
640 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
641 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
643 if (!list_empty(&q->queued)) {
644 list_add_tail(&buf->vb.queue, &q->queued);
645 buf->vb.state = VIDEOBUF_QUEUED;
646 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
649 } else if (list_empty(&q->active)) {
650 list_add_tail(&buf->vb.queue, &q->active);
651 cx23885_start_video_dma(dev, q, buf);
652 buf->vb.state = VIDEOBUF_ACTIVE;
653 buf->count = q->count++;
654 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
655 dprintk(2, "[%p/%d] buffer_queue - first active\n",
659 prev = list_entry(q->active.prev, struct cx23885_buffer,
661 if (prev->vb.width == buf->vb.width &&
662 prev->vb.height == buf->vb.height &&
663 prev->fmt == buf->fmt) {
664 list_add_tail(&buf->vb.queue, &q->active);
665 buf->vb.state = VIDEOBUF_ACTIVE;
666 buf->count = q->count++;
667 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
668 /* 64 bit bits 63-32 */
669 prev->risc.jmp[2] = cpu_to_le32(0);
670 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
674 list_add_tail(&buf->vb.queue, &q->queued);
675 buf->vb.state = VIDEOBUF_QUEUED;
676 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
682 static void buffer_release(struct videobuf_queue *q,
683 struct videobuf_buffer *vb)
685 struct cx23885_buffer *buf = container_of(vb,
686 struct cx23885_buffer, vb);
688 cx23885_free_buffer(q, buf);
691 static struct videobuf_queue_ops cx23885_video_qops = {
692 .buf_setup = buffer_setup,
693 .buf_prepare = buffer_prepare,
694 .buf_queue = buffer_queue,
695 .buf_release = buffer_release,
698 static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
701 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
703 case V4L2_BUF_TYPE_VBI_CAPTURE:
711 static int get_resource(struct cx23885_fh *fh)
714 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
715 return RESOURCE_VIDEO;
716 case V4L2_BUF_TYPE_VBI_CAPTURE:
724 static int video_open(struct inode *inode, struct file *file)
726 int minor = iminor(inode);
727 struct cx23885_dev *h, *dev = NULL;
728 struct cx23885_fh *fh;
729 struct list_head *list;
730 enum v4l2_buf_type type = 0;
733 list_for_each(list, &cx23885_devlist) {
734 h = list_entry(list, struct cx23885_dev, devlist);
735 if (h->video_dev->minor == minor) {
737 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
740 h->vbi_dev->minor == minor) {
742 type = V4L2_BUF_TYPE_VBI_CAPTURE;
745 h->radio_dev->minor == minor) {
753 dprintk(1, "open minor=%d radio=%d type=%s\n",
754 minor, radio, v4l2_type_names[type]);
756 /* allocate + initialize per filehandle data */
757 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
760 file->private_data = fh;
766 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
768 videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
769 &dev->pci->dev, &dev->slock,
770 V4L2_BUF_TYPE_VIDEO_CAPTURE,
771 V4L2_FIELD_INTERLACED,
772 sizeof(struct cx23885_buffer),
775 dprintk(1, "post videobuf_queue_init()\n");
781 static ssize_t video_read(struct file *file, char __user *data,
782 size_t count, loff_t *ppos)
784 struct cx23885_fh *fh = file->private_data;
787 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
788 if (res_locked(fh->dev, RESOURCE_VIDEO))
790 return videobuf_read_one(&fh->vidq, data, count, ppos,
791 file->f_flags & O_NONBLOCK);
792 case V4L2_BUF_TYPE_VBI_CAPTURE:
793 if (!res_get(fh->dev, fh, RESOURCE_VBI))
795 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
796 file->f_flags & O_NONBLOCK);
803 static unsigned int video_poll(struct file *file,
804 struct poll_table_struct *wait)
806 struct cx23885_fh *fh = file->private_data;
807 struct cx23885_buffer *buf;
809 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
810 if (!res_get(fh->dev, fh, RESOURCE_VBI))
812 return videobuf_poll_stream(file, &fh->vbiq, wait);
815 if (res_check(fh, RESOURCE_VIDEO)) {
816 /* streaming capture */
817 if (list_empty(&fh->vidq.stream))
819 buf = list_entry(fh->vidq.stream.next,
820 struct cx23885_buffer, vb.stream);
823 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
827 poll_wait(file, &buf->vb.done, wait);
828 if (buf->vb.state == VIDEOBUF_DONE ||
829 buf->vb.state == VIDEOBUF_ERROR)
830 return POLLIN|POLLRDNORM;
834 static int video_release(struct inode *inode, struct file *file)
836 struct cx23885_fh *fh = file->private_data;
837 struct cx23885_dev *dev = fh->dev;
839 /* turn off overlay */
840 if (res_check(fh, RESOURCE_OVERLAY)) {
842 res_free(dev, fh, RESOURCE_OVERLAY);
845 /* stop video capture */
846 if (res_check(fh, RESOURCE_VIDEO)) {
847 videobuf_queue_cancel(&fh->vidq);
848 res_free(dev, fh, RESOURCE_VIDEO);
850 if (fh->vidq.read_buf) {
851 buffer_release(&fh->vidq, fh->vidq.read_buf);
852 kfree(fh->vidq.read_buf);
855 /* stop vbi capture */
856 if (res_check(fh, RESOURCE_VBI)) {
857 if (fh->vbiq.streaming)
858 videobuf_streamoff(&fh->vbiq);
859 if (fh->vbiq.reading)
860 videobuf_read_stop(&fh->vbiq);
861 res_free(dev, fh, RESOURCE_VBI);
864 videobuf_mmap_free(&fh->vidq);
865 file->private_data = NULL;
868 /* We are not putting the tuner to sleep here on exit, because
869 * we want to use the mpeg encoder in another session to capture
870 * tuner video. Closing this will result in no video to the encoder.
876 static int video_mmap(struct file *file, struct vm_area_struct *vma)
878 struct cx23885_fh *fh = file->private_data;
880 return videobuf_mmap_mapper(get_queue(fh), vma);
883 /* ------------------------------------------------------------------ */
884 /* VIDEO CTRL IOCTLS */
886 int cx23885_get_control(struct cx23885_dev *dev, struct v4l2_control *ctl)
888 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
889 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_G_CTRL, ctl);
892 EXPORT_SYMBOL(cx23885_get_control);
894 int cx23885_set_control(struct cx23885_dev *dev, struct v4l2_control *ctl)
896 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
897 " (disabled - no action)\n", __func__);
900 EXPORT_SYMBOL(cx23885_set_control);
902 static void init_controls(struct cx23885_dev *dev)
904 struct v4l2_control ctrl;
907 for (i = 0; i < CX23885_CTLS; i++) {
908 ctrl.id = cx23885_ctls[i].v.id;
909 ctrl.value = cx23885_ctls[i].v.default_value;
911 cx23885_set_control(dev, &ctrl);
915 /* ------------------------------------------------------------------ */
918 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
919 struct v4l2_format *f)
921 struct cx23885_fh *fh = priv;
923 f->fmt.pix.width = fh->width;
924 f->fmt.pix.height = fh->height;
925 f->fmt.pix.field = fh->vidq.field;
926 f->fmt.pix.pixelformat = fh->fmt->fourcc;
927 f->fmt.pix.bytesperline =
928 (f->fmt.pix.width * fh->fmt->depth) >> 3;
929 f->fmt.pix.sizeimage =
930 f->fmt.pix.height * f->fmt.pix.bytesperline;
935 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
936 struct v4l2_format *f)
938 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
939 struct cx23885_fmt *fmt;
940 enum v4l2_field field;
941 unsigned int maxw, maxh;
943 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
947 field = f->fmt.pix.field;
948 maxw = norm_maxw(dev->tvnorm);
949 maxh = norm_maxh(dev->tvnorm);
951 if (V4L2_FIELD_ANY == field) {
952 field = (f->fmt.pix.height > maxh/2)
953 ? V4L2_FIELD_INTERLACED
959 case V4L2_FIELD_BOTTOM:
962 case V4L2_FIELD_INTERLACED:
968 f->fmt.pix.field = field;
969 if (f->fmt.pix.height < 32)
970 f->fmt.pix.height = 32;
971 if (f->fmt.pix.height > maxh)
972 f->fmt.pix.height = maxh;
973 if (f->fmt.pix.width < 48)
974 f->fmt.pix.width = 48;
975 if (f->fmt.pix.width > maxw)
976 f->fmt.pix.width = maxw;
977 f->fmt.pix.width &= ~0x03;
978 f->fmt.pix.bytesperline =
979 (f->fmt.pix.width * fmt->depth) >> 3;
980 f->fmt.pix.sizeimage =
981 f->fmt.pix.height * f->fmt.pix.bytesperline;
986 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
987 struct v4l2_format *f)
989 struct cx23885_fh *fh = priv;
990 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
993 dprintk(2, "%s()\n", __func__);
994 err = vidioc_try_fmt_vid_cap(file, priv, f);
998 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
999 fh->width = f->fmt.pix.width;
1000 fh->height = f->fmt.pix.height;
1001 fh->vidq.field = f->fmt.pix.field;
1002 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
1003 fh->width, fh->height, fh->vidq.field);
1004 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_FMT, f);
1008 static int vidioc_querycap(struct file *file, void *priv,
1009 struct v4l2_capability *cap)
1011 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1013 strcpy(cap->driver, "cx23885");
1014 strlcpy(cap->card, cx23885_boards[dev->board].name,
1016 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1017 cap->version = CX23885_VERSION_CODE;
1019 V4L2_CAP_VIDEO_CAPTURE |
1020 V4L2_CAP_READWRITE |
1021 V4L2_CAP_STREAMING |
1022 V4L2_CAP_VBI_CAPTURE;
1023 if (UNSET != dev->tuner_type)
1024 cap->capabilities |= V4L2_CAP_TUNER;
1028 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1029 struct v4l2_fmtdesc *f)
1031 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1034 strlcpy(f->description, formats[f->index].name,
1035 sizeof(f->description));
1036 f->pixelformat = formats[f->index].fourcc;
1041 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1042 static int vidiocgmbuf(struct file *file, void *priv,
1043 struct video_mbuf *mbuf)
1045 struct cx23885_fh *fh = priv;
1046 struct videobuf_queue *q;
1047 struct v4l2_requestbuffers req;
1052 memset(&req, 0, sizeof(req));
1055 req.memory = V4L2_MEMORY_MMAP;
1056 err = videobuf_reqbufs(q, &req);
1060 mbuf->frames = req.count;
1062 for (i = 0; i < mbuf->frames; i++) {
1063 mbuf->offsets[i] = q->bufs[i]->boff;
1064 mbuf->size += q->bufs[i]->bsize;
1070 static int vidioc_reqbufs(struct file *file, void *priv,
1071 struct v4l2_requestbuffers *p)
1073 struct cx23885_fh *fh = priv;
1074 return (videobuf_reqbufs(get_queue(fh), p));
1077 static int vidioc_querybuf(struct file *file, void *priv,
1078 struct v4l2_buffer *p)
1080 struct cx23885_fh *fh = priv;
1081 return (videobuf_querybuf(get_queue(fh), p));
1084 static int vidioc_qbuf(struct file *file, void *priv,
1085 struct v4l2_buffer *p)
1087 struct cx23885_fh *fh = priv;
1088 return (videobuf_qbuf(get_queue(fh), p));
1091 static int vidioc_dqbuf(struct file *file, void *priv,
1092 struct v4l2_buffer *p)
1094 struct cx23885_fh *fh = priv;
1095 return (videobuf_dqbuf(get_queue(fh), p,
1096 file->f_flags & O_NONBLOCK));
1099 static int vidioc_streamon(struct file *file, void *priv,
1100 enum v4l2_buf_type i)
1102 struct cx23885_fh *fh = priv;
1103 struct cx23885_dev *dev = fh->dev;
1104 dprintk(1, "%s()\n", __func__);
1106 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1108 if (unlikely(i != fh->type))
1111 if (unlikely(!res_get(dev, fh, get_resource(fh))))
1113 return videobuf_streamon(get_queue(fh));
1116 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1118 struct cx23885_fh *fh = priv;
1119 struct cx23885_dev *dev = fh->dev;
1121 dprintk(1, "%s()\n", __func__);
1123 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1128 res = get_resource(fh);
1129 err = videobuf_streamoff(get_queue(fh));
1132 res_free(dev, fh, res);
1136 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1138 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1139 dprintk(1, "%s()\n", __func__);
1141 mutex_lock(&dev->lock);
1142 cx23885_set_tvnorm(dev, *tvnorms);
1143 mutex_unlock(&dev->lock);
1148 int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
1150 static const char *iname[] = {
1151 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1152 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1153 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1154 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1155 [CX23885_VMUX_SVIDEO] = "S-Video",
1156 [CX23885_VMUX_TELEVISION] = "Television",
1157 [CX23885_VMUX_CABLE] = "Cable TV",
1158 [CX23885_VMUX_DVB] = "DVB",
1159 [CX23885_VMUX_DEBUG] = "for debug only",
1162 dprintk(1, "%s()\n", __func__);
1168 if (0 == INPUT(n)->type)
1171 memset(i, 0, sizeof(*i));
1173 i->type = V4L2_INPUT_TYPE_CAMERA;
1174 strcpy(i->name, iname[INPUT(n)->type]);
1175 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
1176 (CX23885_VMUX_CABLE == INPUT(n)->type))
1177 i->type = V4L2_INPUT_TYPE_TUNER;
1178 i->std = CX23885_NORMS;
1181 EXPORT_SYMBOL(cx23885_enum_input);
1183 static int vidioc_enum_input(struct file *file, void *priv,
1184 struct v4l2_input *i)
1186 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1187 dprintk(1, "%s()\n", __func__);
1188 return cx23885_enum_input(dev, i);
1191 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1193 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1196 dprintk(1, "%s() returns %d\n", __func__, *i);
1200 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1202 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1204 dprintk(1, "%s(%d)\n", __func__, i);
1207 dprintk(1, "%s() -EINVAL\n", __func__);
1211 mutex_lock(&dev->lock);
1212 cx23885_video_mux(dev, i);
1213 mutex_unlock(&dev->lock);
1217 static int vidioc_queryctrl(struct file *file, void *priv,
1218 struct v4l2_queryctrl *qctrl)
1220 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1221 if (unlikely(qctrl->id == 0))
1223 return cx23885_ctrl_query(qctrl);
1226 static int vidioc_g_ctrl(struct file *file, void *priv,
1227 struct v4l2_control *ctl)
1229 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1231 return cx23885_get_control(dev, ctl);
1234 static int vidioc_s_ctrl(struct file *file, void *priv,
1235 struct v4l2_control *ctl)
1237 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1239 return cx23885_set_control(dev, ctl);
1242 static int vidioc_g_tuner(struct file *file, void *priv,
1243 struct v4l2_tuner *t)
1245 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1247 if (unlikely(UNSET == dev->tuner_type))
1252 strcpy(t->name, "Television");
1253 t->type = V4L2_TUNER_ANALOG_TV;
1254 t->capability = V4L2_TUNER_CAP_NORM;
1255 t->rangehigh = 0xffffffffUL;
1256 t->signal = 0xffff ; /* LOCKED */
1260 static int vidioc_s_tuner(struct file *file, void *priv,
1261 struct v4l2_tuner *t)
1263 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1265 if (UNSET == dev->tuner_type)
1272 static int vidioc_g_frequency(struct file *file, void *priv,
1273 struct v4l2_frequency *f)
1275 struct cx23885_fh *fh = priv;
1276 struct cx23885_dev *dev = fh->dev;
1278 if (unlikely(UNSET == dev->tuner_type))
1281 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1282 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1283 f->frequency = dev->freq;
1285 cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_G_FREQUENCY, f);
1290 int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
1292 if (unlikely(UNSET == dev->tuner_type))
1294 if (unlikely(f->tuner != 0))
1297 mutex_lock(&dev->lock);
1298 dev->freq = f->frequency;
1300 cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, f);
1302 /* When changing channels it is required to reset TVAUDIO */
1305 mutex_unlock(&dev->lock);
1309 EXPORT_SYMBOL(cx23885_set_freq);
1311 static int vidioc_s_frequency(struct file *file, void *priv,
1312 struct v4l2_frequency *f)
1314 struct cx23885_fh *fh = priv;
1315 struct cx23885_dev *dev = fh->dev;
1317 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1319 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1323 cx23885_set_freq(dev, f);
1326 #ifdef CONFIG_VIDEO_ADV_DEBUG
1327 static int vidioc_g_register(struct file *file, void *fh,
1328 struct v4l2_register *reg)
1330 struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1332 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1335 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_G_REGISTER, reg);
1340 static int vidioc_s_register(struct file *file, void *fh,
1341 struct v4l2_register *reg)
1343 struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1345 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1348 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_S_REGISTER, reg);
1354 /* ----------------------------------------------------------- */
1356 static void cx23885_vid_timeout(unsigned long data)
1358 struct cx23885_dev *dev = (struct cx23885_dev *)data;
1359 struct cx23885_dmaqueue *q = &dev->vidq;
1360 struct cx23885_buffer *buf;
1361 unsigned long flags;
1363 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1365 cx_clear(VID_A_DMA_CTL, 0x11);
1367 spin_lock_irqsave(&dev->slock, flags);
1368 while (!list_empty(&q->active)) {
1369 buf = list_entry(q->active.next,
1370 struct cx23885_buffer, vb.queue);
1371 list_del(&buf->vb.queue);
1372 buf->vb.state = VIDEOBUF_ERROR;
1373 wake_up(&buf->vb.done);
1374 printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
1375 dev->name, buf, buf->vb.i,
1376 (unsigned long)buf->risc.dma);
1378 cx23885_restart_video_queue(dev, q);
1379 spin_unlock_irqrestore(&dev->slock, flags);
1382 int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1387 mask = cx_read(VID_A_INT_MSK);
1388 if (0 == (status & mask))
1390 cx_write(VID_A_INT_STAT, status);
1392 dprintk(2, "%s() status = 0x%08x\n", __func__, status);
1393 /* risc op code error */
1394 if (status & (1 << 16)) {
1395 printk(KERN_WARNING "%s/0: video risc op code error\n",
1397 cx_clear(VID_A_DMA_CTL, 0x11);
1398 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1402 if (status & 0x01) {
1403 spin_lock(&dev->slock);
1404 count = cx_read(VID_A_GPCNT);
1405 cx23885_video_wakeup(dev, &dev->vidq, count);
1406 spin_unlock(&dev->slock);
1410 if (status & 0x10) {
1411 dprintk(2, "stopper video\n");
1412 spin_lock(&dev->slock);
1413 cx23885_restart_video_queue(dev, &dev->vidq);
1414 spin_unlock(&dev->slock);
1421 /* ----------------------------------------------------------- */
1422 /* exported stuff */
1424 static const struct file_operations video_fops = {
1425 .owner = THIS_MODULE,
1427 .release = video_release,
1431 .ioctl = video_ioctl2,
1432 .compat_ioctl = v4l_compat_ioctl32,
1433 .llseek = no_llseek,
1436 static struct video_device cx23885_vbi_template;
1437 static struct video_device cx23885_video_template = {
1438 .name = "cx23885-video",
1439 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1440 .fops = &video_fops,
1442 .vidioc_querycap = vidioc_querycap,
1443 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1444 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1445 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1446 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1447 .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
1448 .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
1449 .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
1450 .vidioc_reqbufs = vidioc_reqbufs,
1451 .vidioc_querybuf = vidioc_querybuf,
1452 .vidioc_qbuf = vidioc_qbuf,
1453 .vidioc_dqbuf = vidioc_dqbuf,
1454 .vidioc_s_std = vidioc_s_std,
1455 .vidioc_enum_input = vidioc_enum_input,
1456 .vidioc_g_input = vidioc_g_input,
1457 .vidioc_s_input = vidioc_s_input,
1458 .vidioc_queryctrl = vidioc_queryctrl,
1459 .vidioc_g_ctrl = vidioc_g_ctrl,
1460 .vidioc_s_ctrl = vidioc_s_ctrl,
1461 .vidioc_streamon = vidioc_streamon,
1462 .vidioc_streamoff = vidioc_streamoff,
1463 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1464 .vidiocgmbuf = vidiocgmbuf,
1466 .vidioc_g_tuner = vidioc_g_tuner,
1467 .vidioc_s_tuner = vidioc_s_tuner,
1468 .vidioc_g_frequency = vidioc_g_frequency,
1469 .vidioc_s_frequency = vidioc_s_frequency,
1470 #ifdef CONFIG_VIDEO_ADV_DEBUG
1471 .vidioc_g_register = vidioc_g_register,
1472 .vidioc_s_register = vidioc_s_register,
1474 .tvnorms = CX23885_NORMS,
1475 .current_norm = V4L2_STD_NTSC_M,
1478 static const struct file_operations radio_fops = {
1479 .owner = THIS_MODULE,
1481 .release = video_release,
1482 .ioctl = video_ioctl2,
1483 .compat_ioctl = v4l_compat_ioctl32,
1484 .llseek = no_llseek,
1488 void cx23885_video_unregister(struct cx23885_dev *dev)
1490 dprintk(1, "%s()\n", __func__);
1491 cx_clear(PCI_INT_MSK, 1);
1493 if (dev->video_dev) {
1494 if (-1 != dev->video_dev->minor)
1495 video_unregister_device(dev->video_dev);
1497 video_device_release(dev->video_dev);
1498 dev->video_dev = NULL;
1500 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1504 int cx23885_video_register(struct cx23885_dev *dev)
1508 dprintk(1, "%s()\n", __func__);
1509 spin_lock_init(&dev->slock);
1511 /* Initialize VBI template */
1512 memcpy(&cx23885_vbi_template, &cx23885_video_template,
1513 sizeof(cx23885_vbi_template));
1514 strcpy(cx23885_vbi_template.name, "cx23885-vbi");
1515 cx23885_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
1517 dev->tvnorm = cx23885_video_template.current_norm;
1519 /* init video dma queues */
1520 INIT_LIST_HEAD(&dev->vidq.active);
1521 INIT_LIST_HEAD(&dev->vidq.queued);
1522 dev->vidq.timeout.function = cx23885_vid_timeout;
1523 dev->vidq.timeout.data = (unsigned long)dev;
1524 init_timer(&dev->vidq.timeout);
1525 cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1526 VID_A_DMA_CTL, 0x11, 0x00);
1528 /* Don't enable VBI yet */
1529 cx_set(PCI_INT_MSK, 1);
1532 /* register v4l devices */
1533 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1534 &cx23885_video_template, "video");
1535 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1538 printk(KERN_INFO "%s: can't register video device\n",
1542 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1543 dev->name, dev->video_dev->minor & 0x1f);
1544 /* initial device configuration */
1545 mutex_lock(&dev->lock);
1546 cx23885_set_tvnorm(dev, dev->tvnorm);
1548 cx23885_video_mux(dev, 0);
1549 mutex_unlock(&dev->lock);
1554 cx23885_video_unregister(dev);