3 * device driver for Conexant 2388x based TV cards
4 * video4linux video interface
6 * (c) 2003-04 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
8 * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
10 * - video_ioctl2 conversion
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/module.h>
31 #include <linux/kmod.h>
32 #include <linux/kernel.h>
33 #include <linux/slab.h>
34 #include <linux/interrupt.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/delay.h>
37 #include <linux/kthread.h>
38 #include <asm/div64.h>
41 #include <media/v4l2-common.h>
42 #include <media/v4l2-ioctl.h>
43 #include <media/wm8775.h>
45 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
46 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
47 MODULE_LICENSE("GPL");
48 MODULE_VERSION(CX88_VERSION);
50 /* ------------------------------------------------------------------ */
52 static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
53 static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
54 static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
56 module_param_array(video_nr, int, NULL, 0444);
57 module_param_array(vbi_nr, int, NULL, 0444);
58 module_param_array(radio_nr, int, NULL, 0444);
60 MODULE_PARM_DESC(video_nr,"video device numbers");
61 MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
62 MODULE_PARM_DESC(radio_nr,"radio device numbers");
64 static unsigned int video_debug;
65 module_param(video_debug,int,0644);
66 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
68 static unsigned int irq_debug;
69 module_param(irq_debug,int,0644);
70 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
72 static unsigned int vid_limit = 16;
73 module_param(vid_limit,int,0644);
74 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
76 #define dprintk(level,fmt, arg...) if (video_debug >= level) \
77 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
79 /* ------------------------------------------------------------------- */
82 static const struct cx8800_fmt formats[] = {
84 .name = "8 bpp, gray",
85 .fourcc = V4L2_PIX_FMT_GREY,
86 .cxformat = ColorFormatY8,
88 .flags = FORMAT_FLAGS_PACKED,
90 .name = "15 bpp RGB, le",
91 .fourcc = V4L2_PIX_FMT_RGB555,
92 .cxformat = ColorFormatRGB15,
94 .flags = FORMAT_FLAGS_PACKED,
96 .name = "15 bpp RGB, be",
97 .fourcc = V4L2_PIX_FMT_RGB555X,
98 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
100 .flags = FORMAT_FLAGS_PACKED,
102 .name = "16 bpp RGB, le",
103 .fourcc = V4L2_PIX_FMT_RGB565,
104 .cxformat = ColorFormatRGB16,
106 .flags = FORMAT_FLAGS_PACKED,
108 .name = "16 bpp RGB, be",
109 .fourcc = V4L2_PIX_FMT_RGB565X,
110 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
112 .flags = FORMAT_FLAGS_PACKED,
114 .name = "24 bpp RGB, le",
115 .fourcc = V4L2_PIX_FMT_BGR24,
116 .cxformat = ColorFormatRGB24,
118 .flags = FORMAT_FLAGS_PACKED,
120 .name = "32 bpp RGB, le",
121 .fourcc = V4L2_PIX_FMT_BGR32,
122 .cxformat = ColorFormatRGB32,
124 .flags = FORMAT_FLAGS_PACKED,
126 .name = "32 bpp RGB, be",
127 .fourcc = V4L2_PIX_FMT_RGB32,
128 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
130 .flags = FORMAT_FLAGS_PACKED,
132 .name = "4:2:2, packed, YUYV",
133 .fourcc = V4L2_PIX_FMT_YUYV,
134 .cxformat = ColorFormatYUY2,
136 .flags = FORMAT_FLAGS_PACKED,
138 .name = "4:2:2, packed, UYVY",
139 .fourcc = V4L2_PIX_FMT_UYVY,
140 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
142 .flags = FORMAT_FLAGS_PACKED,
146 static const struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
150 for (i = 0; i < ARRAY_SIZE(formats); i++)
151 if (formats[i].fourcc == fourcc)
156 /* ------------------------------------------------------------------- */
158 static const struct v4l2_queryctrl no_ctl = {
160 .flags = V4L2_CTRL_FLAG_DISABLED,
163 static const struct cx88_ctrl cx8800_ctls[] = {
167 .id = V4L2_CID_BRIGHTNESS,
168 .name = "Brightness",
172 .default_value = 0x7f,
173 .type = V4L2_CTRL_TYPE_INTEGER,
176 .reg = MO_CONTR_BRIGHT,
181 .id = V4L2_CID_CONTRAST,
186 .default_value = 0x3f,
187 .type = V4L2_CTRL_TYPE_INTEGER,
190 .reg = MO_CONTR_BRIGHT,
200 .default_value = 0x7f,
201 .type = V4L2_CTRL_TYPE_INTEGER,
208 /* strictly, this only describes only U saturation.
209 * V saturation is handled specially through code.
212 .id = V4L2_CID_SATURATION,
213 .name = "Saturation",
217 .default_value = 0x7f,
218 .type = V4L2_CTRL_TYPE_INTEGER,
221 .reg = MO_UV_SATURATION,
226 .id = V4L2_CID_SHARPNESS,
231 .default_value = 0x0,
232 .type = V4L2_CTRL_TYPE_INTEGER,
235 /* NOTE: the value is converted and written to both even
236 and odd registers in the code */
237 .reg = MO_FILTER_ODD,
242 .id = V4L2_CID_CHROMA_AGC,
243 .name = "Chroma AGC",
246 .default_value = 0x1,
247 .type = V4L2_CTRL_TYPE_BOOLEAN,
249 .reg = MO_INPUT_FORMAT,
254 .id = V4L2_CID_COLOR_KILLER,
255 .name = "Color killer",
258 .default_value = 0x1,
259 .type = V4L2_CTRL_TYPE_BOOLEAN,
261 .reg = MO_INPUT_FORMAT,
267 .id = V4L2_CID_AUDIO_MUTE,
272 .type = V4L2_CTRL_TYPE_BOOLEAN,
275 .sreg = SHADOW_AUD_VOL_CTL,
280 .id = V4L2_CID_AUDIO_VOLUME,
285 .default_value = 0x3f,
286 .type = V4L2_CTRL_TYPE_INTEGER,
289 .sreg = SHADOW_AUD_VOL_CTL,
294 .id = V4L2_CID_AUDIO_BALANCE,
299 .default_value = 0x40,
300 .type = V4L2_CTRL_TYPE_INTEGER,
303 .sreg = SHADOW_AUD_BAL_CTL,
308 enum { CX8800_CTLS = ARRAY_SIZE(cx8800_ctls) };
310 /* Must be sorted from low to high control ID! */
311 const u32 cx88_user_ctrls[] = {
317 V4L2_CID_AUDIO_VOLUME,
318 V4L2_CID_AUDIO_BALANCE,
322 V4L2_CID_COLOR_KILLER,
325 EXPORT_SYMBOL(cx88_user_ctrls);
327 static const u32 * const ctrl_classes[] = {
332 int cx8800_ctrl_query(struct cx88_core *core, struct v4l2_queryctrl *qctrl)
336 if (qctrl->id < V4L2_CID_BASE ||
337 qctrl->id >= V4L2_CID_LASTP1)
339 for (i = 0; i < CX8800_CTLS; i++)
340 if (cx8800_ctls[i].v.id == qctrl->id)
342 if (i == CX8800_CTLS) {
346 *qctrl = cx8800_ctls[i].v;
347 /* Report chroma AGC as inactive when SECAM is selected */
348 if (cx8800_ctls[i].v.id == V4L2_CID_CHROMA_AGC &&
349 core->tvnorm & V4L2_STD_SECAM)
350 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
354 EXPORT_SYMBOL(cx8800_ctrl_query);
356 /* ------------------------------------------------------------------- */
357 /* resource management */
359 static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
361 struct cx88_core *core = dev->core;
362 if (fh->resources & bit)
363 /* have it already allocated */
367 mutex_lock(&core->lock);
368 if (dev->resources & bit) {
369 /* no, someone else uses it */
370 mutex_unlock(&core->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(&core->lock);
382 int res_check(struct cx8800_fh *fh, unsigned int bit)
384 return (fh->resources & bit);
388 int res_locked(struct cx8800_dev *dev, unsigned int bit)
390 return (dev->resources & bit);
394 void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
396 struct cx88_core *core = dev->core;
397 BUG_ON((fh->resources & bits) != bits);
399 mutex_lock(&core->lock);
400 fh->resources &= ~bits;
401 dev->resources &= ~bits;
402 dprintk(1,"res: put %d\n",bits);
403 mutex_unlock(&core->lock);
406 /* ------------------------------------------------------------------ */
408 int cx88_video_mux(struct cx88_core *core, unsigned int input)
410 /* struct cx88_core *core = dev->core; */
412 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
413 input, INPUT(input).vmux,
414 INPUT(input).gpio0,INPUT(input).gpio1,
415 INPUT(input).gpio2,INPUT(input).gpio3);
417 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
418 cx_write(MO_GP3_IO, INPUT(input).gpio3);
419 cx_write(MO_GP0_IO, INPUT(input).gpio0);
420 cx_write(MO_GP1_IO, INPUT(input).gpio1);
421 cx_write(MO_GP2_IO, INPUT(input).gpio2);
423 switch (INPUT(input).type) {
424 case CX88_VMUX_SVIDEO:
425 cx_set(MO_AFECFG_IO, 0x00000001);
426 cx_set(MO_INPUT_FORMAT, 0x00010010);
427 cx_set(MO_FILTER_EVEN, 0x00002020);
428 cx_set(MO_FILTER_ODD, 0x00002020);
431 cx_clear(MO_AFECFG_IO, 0x00000001);
432 cx_clear(MO_INPUT_FORMAT, 0x00010010);
433 cx_clear(MO_FILTER_EVEN, 0x00002020);
434 cx_clear(MO_FILTER_ODD, 0x00002020);
438 /* if there are audioroutes defined, we have an external
439 ADC to deal with audio */
440 if (INPUT(input).audioroute) {
441 /* The wm8775 module has the "2" route hardwired into
442 the initialization. Some boards may use different
443 routes for different inputs. HVR-1300 surely does */
444 if (core->board.audio_chip &&
445 core->board.audio_chip == V4L2_IDENT_WM8775) {
446 call_all(core, audio, s_routing,
447 INPUT(input).audioroute, 0, 0);
449 /* cx2388's C-ADC is connected to the tuner only.
450 When used with S-Video, that ADC is busy dealing with
451 chroma, so an external must be used for baseband audio */
452 if (INPUT(input).type != CX88_VMUX_TELEVISION &&
453 INPUT(input).type != CX88_VMUX_CABLE) {
455 core->tvaudio = WW_I2SADC;
456 cx88_set_tvaudio(core);
459 cx_write(AUD_I2SCNTL, 0x0);
460 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
466 EXPORT_SYMBOL(cx88_video_mux);
468 /* ------------------------------------------------------------------ */
470 static int start_video_dma(struct cx8800_dev *dev,
471 struct cx88_dmaqueue *q,
472 struct cx88_buffer *buf)
474 struct cx88_core *core = dev->core;
476 /* setup fifo + format */
477 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
478 buf->bpl, buf->risc.dma);
479 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
480 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
483 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
487 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
489 /* Enables corresponding bits at PCI_INT_STAT:
490 bits 0 to 4: video, audio, transport stream, VIP, Host
492 bits 8 and 9: DMA complete for: SRC, DST
493 bits 10 and 11: BERR signal asserted for RISC: RD, WR
494 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
496 cx_set(MO_VID_INTMSK, 0x0f0011);
499 cx_set(VID_CAPTURE_CONTROL,0x06);
502 cx_set(MO_DEV_CNTRL2, (1<<5));
503 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
509 static int stop_video_dma(struct cx8800_dev *dev)
511 struct cx88_core *core = dev->core;
514 cx_clear(MO_VID_DMACNTRL, 0x11);
516 /* disable capture */
517 cx_clear(VID_CAPTURE_CONTROL,0x06);
520 cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
521 cx_clear(MO_VID_INTMSK, 0x0f0011);
526 static int restart_video_queue(struct cx8800_dev *dev,
527 struct cx88_dmaqueue *q)
529 struct cx88_core *core = dev->core;
530 struct cx88_buffer *buf, *prev;
532 if (!list_empty(&q->active)) {
533 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
534 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
536 start_video_dma(dev, q, buf);
537 list_for_each_entry(buf, &q->active, vb.queue)
538 buf->count = q->count++;
539 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
545 if (list_empty(&q->queued))
547 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
549 list_move_tail(&buf->vb.queue, &q->active);
550 start_video_dma(dev, q, buf);
551 buf->vb.state = VIDEOBUF_ACTIVE;
552 buf->count = q->count++;
553 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
554 dprintk(2,"[%p/%d] restart_queue - first active\n",
557 } else if (prev->vb.width == buf->vb.width &&
558 prev->vb.height == buf->vb.height &&
559 prev->fmt == buf->fmt) {
560 list_move_tail(&buf->vb.queue, &q->active);
561 buf->vb.state = VIDEOBUF_ACTIVE;
562 buf->count = q->count++;
563 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
564 dprintk(2,"[%p/%d] restart_queue - move to active\n",
573 /* ------------------------------------------------------------------ */
576 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
578 struct cx8800_fh *fh = q->priv_data;
580 *size = fh->fmt->depth*fh->width*fh->height >> 3;
583 if (*size * *count > vid_limit * 1024 * 1024)
584 *count = (vid_limit * 1024 * 1024) / *size;
589 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
590 enum v4l2_field field)
592 struct cx8800_fh *fh = q->priv_data;
593 struct cx8800_dev *dev = fh->dev;
594 struct cx88_core *core = dev->core;
595 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
596 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
597 int rc, init_buffer = 0;
599 BUG_ON(NULL == fh->fmt);
600 if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
601 fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
603 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
604 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
607 if (buf->fmt != fh->fmt ||
608 buf->vb.width != fh->width ||
609 buf->vb.height != fh->height ||
610 buf->vb.field != field) {
612 buf->vb.width = fh->width;
613 buf->vb.height = fh->height;
614 buf->vb.field = field;
618 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
620 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
625 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
626 switch (buf->vb.field) {
628 cx88_risc_buffer(dev->pci, &buf->risc,
629 dma->sglist, 0, UNSET,
630 buf->bpl, 0, buf->vb.height);
632 case V4L2_FIELD_BOTTOM:
633 cx88_risc_buffer(dev->pci, &buf->risc,
634 dma->sglist, UNSET, 0,
635 buf->bpl, 0, buf->vb.height);
637 case V4L2_FIELD_INTERLACED:
638 cx88_risc_buffer(dev->pci, &buf->risc,
639 dma->sglist, 0, buf->bpl,
641 buf->vb.height >> 1);
643 case V4L2_FIELD_SEQ_TB:
644 cx88_risc_buffer(dev->pci, &buf->risc,
646 0, buf->bpl * (buf->vb.height >> 1),
648 buf->vb.height >> 1);
650 case V4L2_FIELD_SEQ_BT:
651 cx88_risc_buffer(dev->pci, &buf->risc,
653 buf->bpl * (buf->vb.height >> 1), 0,
655 buf->vb.height >> 1);
661 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
663 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
664 (unsigned long)buf->risc.dma);
666 buf->vb.state = VIDEOBUF_PREPARED;
670 cx88_free_buffer(q,buf);
675 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
677 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
678 struct cx88_buffer *prev;
679 struct cx8800_fh *fh = vq->priv_data;
680 struct cx8800_dev *dev = fh->dev;
681 struct cx88_core *core = dev->core;
682 struct cx88_dmaqueue *q = &dev->vidq;
684 /* add jump to stopper */
685 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
686 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
688 if (!list_empty(&q->queued)) {
689 list_add_tail(&buf->vb.queue,&q->queued);
690 buf->vb.state = VIDEOBUF_QUEUED;
691 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
694 } else if (list_empty(&q->active)) {
695 list_add_tail(&buf->vb.queue,&q->active);
696 start_video_dma(dev, q, buf);
697 buf->vb.state = VIDEOBUF_ACTIVE;
698 buf->count = q->count++;
699 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
700 dprintk(2,"[%p/%d] buffer_queue - first active\n",
704 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
705 if (prev->vb.width == buf->vb.width &&
706 prev->vb.height == buf->vb.height &&
707 prev->fmt == buf->fmt) {
708 list_add_tail(&buf->vb.queue,&q->active);
709 buf->vb.state = VIDEOBUF_ACTIVE;
710 buf->count = q->count++;
711 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
712 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
716 list_add_tail(&buf->vb.queue,&q->queued);
717 buf->vb.state = VIDEOBUF_QUEUED;
718 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
724 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
726 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
728 cx88_free_buffer(q,buf);
731 static const struct videobuf_queue_ops cx8800_video_qops = {
732 .buf_setup = buffer_setup,
733 .buf_prepare = buffer_prepare,
734 .buf_queue = buffer_queue,
735 .buf_release = buffer_release,
738 /* ------------------------------------------------------------------ */
741 /* ------------------------------------------------------------------ */
743 static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
746 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
748 case V4L2_BUF_TYPE_VBI_CAPTURE:
756 static int get_ressource(struct cx8800_fh *fh)
759 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
760 return RESOURCE_VIDEO;
761 case V4L2_BUF_TYPE_VBI_CAPTURE:
769 static int video_open(struct file *file)
771 struct video_device *vdev = video_devdata(file);
772 struct cx8800_dev *dev = video_drvdata(file);
773 struct cx88_core *core = dev->core;
774 struct cx8800_fh *fh;
775 enum v4l2_buf_type type = 0;
778 switch (vdev->vfl_type) {
779 case VFL_TYPE_GRABBER:
780 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
783 type = V4L2_BUF_TYPE_VBI_CAPTURE;
790 dprintk(1, "open dev=%s radio=%d type=%s\n",
791 video_device_node_name(vdev), radio, v4l2_type_names[type]);
793 /* allocate + initialize per filehandle data */
794 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
798 file->private_data = fh;
804 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
806 mutex_lock(&core->lock);
808 videobuf_queue_sg_init(&fh->vidq, &cx8800_video_qops,
809 &dev->pci->dev, &dev->slock,
810 V4L2_BUF_TYPE_VIDEO_CAPTURE,
811 V4L2_FIELD_INTERLACED,
812 sizeof(struct cx88_buffer),
814 videobuf_queue_sg_init(&fh->vbiq, &cx8800_vbi_qops,
815 &dev->pci->dev, &dev->slock,
816 V4L2_BUF_TYPE_VBI_CAPTURE,
818 sizeof(struct cx88_buffer),
822 dprintk(1,"video_open: setting radio device\n");
823 cx_write(MO_GP3_IO, core->board.radio.gpio3);
824 cx_write(MO_GP0_IO, core->board.radio.gpio0);
825 cx_write(MO_GP1_IO, core->board.radio.gpio1);
826 cx_write(MO_GP2_IO, core->board.radio.gpio2);
827 if (core->board.radio.audioroute) {
828 if(core->board.audio_chip &&
829 core->board.audio_chip == V4L2_IDENT_WM8775) {
830 call_all(core, audio, s_routing,
831 core->board.radio.audioroute, 0, 0);
834 core->tvaudio = WW_I2SADC;
835 cx88_set_tvaudio(core);
838 core->tvaudio = WW_FM;
839 cx88_set_tvaudio(core);
840 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
842 call_all(core, tuner, s_radio);
846 mutex_unlock(&core->lock);
852 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
854 struct cx8800_fh *fh = file->private_data;
857 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
858 if (res_locked(fh->dev,RESOURCE_VIDEO))
860 return videobuf_read_one(&fh->vidq, data, count, ppos,
861 file->f_flags & O_NONBLOCK);
862 case V4L2_BUF_TYPE_VBI_CAPTURE:
863 if (!res_get(fh->dev,fh,RESOURCE_VBI))
865 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
866 file->f_flags & O_NONBLOCK);
874 video_poll(struct file *file, struct poll_table_struct *wait)
876 struct cx8800_fh *fh = file->private_data;
877 struct cx88_buffer *buf;
878 unsigned int rc = POLLERR;
880 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
881 if (!res_get(fh->dev,fh,RESOURCE_VBI))
883 return videobuf_poll_stream(file, &fh->vbiq, wait);
886 mutex_lock(&fh->vidq.vb_lock);
887 if (res_check(fh,RESOURCE_VIDEO)) {
888 /* streaming capture */
889 if (list_empty(&fh->vidq.stream))
891 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
894 buf = (struct cx88_buffer*)fh->vidq.read_buf;
898 poll_wait(file, &buf->vb.done, wait);
899 if (buf->vb.state == VIDEOBUF_DONE ||
900 buf->vb.state == VIDEOBUF_ERROR)
901 rc = POLLIN|POLLRDNORM;
905 mutex_unlock(&fh->vidq.vb_lock);
909 static int video_release(struct file *file)
911 struct cx8800_fh *fh = file->private_data;
912 struct cx8800_dev *dev = fh->dev;
914 /* turn off overlay */
915 if (res_check(fh, RESOURCE_OVERLAY)) {
917 res_free(dev,fh,RESOURCE_OVERLAY);
920 /* stop video capture */
921 if (res_check(fh, RESOURCE_VIDEO)) {
922 videobuf_queue_cancel(&fh->vidq);
923 res_free(dev,fh,RESOURCE_VIDEO);
925 if (fh->vidq.read_buf) {
926 buffer_release(&fh->vidq,fh->vidq.read_buf);
927 kfree(fh->vidq.read_buf);
930 /* stop vbi capture */
931 if (res_check(fh, RESOURCE_VBI)) {
932 videobuf_stop(&fh->vbiq);
933 res_free(dev,fh,RESOURCE_VBI);
936 videobuf_mmap_free(&fh->vidq);
937 videobuf_mmap_free(&fh->vbiq);
939 mutex_lock(&dev->core->lock);
940 file->private_data = NULL;
944 if (!dev->core->users)
945 call_all(dev->core, core, s_power, 0);
946 mutex_unlock(&dev->core->lock);
952 video_mmap(struct file *file, struct vm_area_struct * vma)
954 struct cx8800_fh *fh = file->private_data;
956 return videobuf_mmap_mapper(get_queue(fh), vma);
959 /* ------------------------------------------------------------------ */
960 /* VIDEO CTRL IOCTLS */
962 int cx88_get_control (struct cx88_core *core, struct v4l2_control *ctl)
964 const struct cx88_ctrl *c = NULL;
968 for (i = 0; i < CX8800_CTLS; i++)
969 if (cx8800_ctls[i].v.id == ctl->id)
971 if (unlikely(NULL == c))
974 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
976 case V4L2_CID_AUDIO_BALANCE:
977 ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
978 : (0x7f - (value & 0x7f));
980 case V4L2_CID_AUDIO_VOLUME:
981 ctl->value = 0x3f - (value & 0x3f);
983 case V4L2_CID_SHARPNESS:
984 ctl->value = ((value & 0x0200) ? (((value & 0x0180) >> 7) + 1)
988 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
991 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
992 ctl->id, c->v.name, ctl->value, c->reg,
993 value,c->mask, c->sreg ? " [shadowed]" : "");
996 EXPORT_SYMBOL(cx88_get_control);
998 int cx88_set_control(struct cx88_core *core, struct v4l2_control *ctl)
1000 const struct cx88_ctrl *c = NULL;
1004 for (i = 0; i < CX8800_CTLS; i++) {
1005 if (cx8800_ctls[i].v.id == ctl->id) {
1006 c = &cx8800_ctls[i];
1009 if (unlikely(NULL == c))
1012 if (ctl->value < c->v.minimum)
1013 ctl->value = c->v.minimum;
1014 if (ctl->value > c->v.maximum)
1015 ctl->value = c->v.maximum;
1017 /* Pass changes onto any WM8775 */
1018 if (core->board.audio_chip == V4L2_IDENT_WM8775) {
1019 struct v4l2_control client_ctl;
1020 memset(&client_ctl, 0, sizeof(client_ctl));
1021 client_ctl.id = ctl->id;
1024 case V4L2_CID_AUDIO_MUTE:
1025 client_ctl.value = ctl->value;
1027 case V4L2_CID_AUDIO_VOLUME:
1028 client_ctl.value = (ctl->value) ?
1029 (0x90 + ctl->value) << 8 : 0;
1031 case V4L2_CID_AUDIO_BALANCE:
1032 client_ctl.value = ctl->value << 9;
1039 call_hw(core, WM8775_GID, core, s_ctrl, &client_ctl);
1044 case V4L2_CID_AUDIO_BALANCE:
1045 value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
1047 case V4L2_CID_AUDIO_VOLUME:
1048 value = 0x3f - (ctl->value & 0x3f);
1050 case V4L2_CID_SATURATION:
1051 /* special v_sat handling */
1053 value = ((ctl->value - c->off) << c->shift) & c->mask;
1055 if (core->tvnorm & V4L2_STD_SECAM) {
1056 /* For SECAM, both U and V sat should be equal */
1057 value=value<<8|value;
1059 /* Keeps U Saturation proportional to V Sat */
1060 value=(value*0x5a)/0x7f<<8|value;
1064 case V4L2_CID_SHARPNESS:
1065 /* 0b000, 0b100, 0b101, 0b110, or 0b111 */
1066 value = (ctl->value < 1 ? 0 : ((ctl->value + 3) << 7));
1067 /* needs to be set for both fields */
1068 cx_andor(MO_FILTER_EVEN, mask, value);
1070 case V4L2_CID_CHROMA_AGC:
1071 /* Do not allow chroma AGC to be enabled for SECAM */
1072 value = ((ctl->value - c->off) << c->shift) & c->mask;
1073 if (core->tvnorm & V4L2_STD_SECAM && value)
1077 value = ((ctl->value - c->off) << c->shift) & c->mask;
1080 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
1081 ctl->id, c->v.name, ctl->value, c->reg, value,
1082 mask, c->sreg ? " [shadowed]" : "");
1084 cx_sandor(c->sreg, c->reg, mask, value);
1086 cx_andor(c->reg, mask, value);
1090 EXPORT_SYMBOL(cx88_set_control);
1092 static void init_controls(struct cx88_core *core)
1094 struct v4l2_control ctrl;
1097 for (i = 0; i < CX8800_CTLS; i++) {
1098 ctrl.id=cx8800_ctls[i].v.id;
1099 ctrl.value=cx8800_ctls[i].v.default_value;
1101 cx88_set_control(core, &ctrl);
1105 /* ------------------------------------------------------------------ */
1108 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1109 struct v4l2_format *f)
1111 struct cx8800_fh *fh = priv;
1113 f->fmt.pix.width = fh->width;
1114 f->fmt.pix.height = fh->height;
1115 f->fmt.pix.field = fh->vidq.field;
1116 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1117 f->fmt.pix.bytesperline =
1118 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1119 f->fmt.pix.sizeimage =
1120 f->fmt.pix.height * f->fmt.pix.bytesperline;
1124 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1125 struct v4l2_format *f)
1127 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1128 const struct cx8800_fmt *fmt;
1129 enum v4l2_field field;
1130 unsigned int maxw, maxh;
1132 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1136 field = f->fmt.pix.field;
1137 maxw = norm_maxw(core->tvnorm);
1138 maxh = norm_maxh(core->tvnorm);
1140 if (V4L2_FIELD_ANY == field) {
1141 field = (f->fmt.pix.height > maxh/2)
1142 ? V4L2_FIELD_INTERLACED
1143 : V4L2_FIELD_BOTTOM;
1147 case V4L2_FIELD_TOP:
1148 case V4L2_FIELD_BOTTOM:
1151 case V4L2_FIELD_INTERLACED:
1157 f->fmt.pix.field = field;
1158 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
1159 &f->fmt.pix.height, 32, maxh, 0, 0);
1160 f->fmt.pix.bytesperline =
1161 (f->fmt.pix.width * fmt->depth) >> 3;
1162 f->fmt.pix.sizeimage =
1163 f->fmt.pix.height * f->fmt.pix.bytesperline;
1168 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1169 struct v4l2_format *f)
1171 struct cx8800_fh *fh = priv;
1172 int err = vidioc_try_fmt_vid_cap (file,priv,f);
1176 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1177 fh->width = f->fmt.pix.width;
1178 fh->height = f->fmt.pix.height;
1179 fh->vidq.field = f->fmt.pix.field;
1183 static int vidioc_querycap (struct file *file, void *priv,
1184 struct v4l2_capability *cap)
1186 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
1187 struct cx88_core *core = dev->core;
1189 strcpy(cap->driver, "cx8800");
1190 strlcpy(cap->card, core->board.name, sizeof(cap->card));
1191 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1193 V4L2_CAP_VIDEO_CAPTURE |
1194 V4L2_CAP_READWRITE |
1195 V4L2_CAP_STREAMING |
1196 V4L2_CAP_VBI_CAPTURE;
1197 if (UNSET != core->board.tuner_type)
1198 cap->capabilities |= V4L2_CAP_TUNER;
1202 static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
1203 struct v4l2_fmtdesc *f)
1205 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1208 strlcpy(f->description,formats[f->index].name,sizeof(f->description));
1209 f->pixelformat = formats[f->index].fourcc;
1214 static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
1216 struct cx8800_fh *fh = priv;
1217 return (videobuf_reqbufs(get_queue(fh), p));
1220 static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
1222 struct cx8800_fh *fh = priv;
1223 return (videobuf_querybuf(get_queue(fh), p));
1226 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1228 struct cx8800_fh *fh = priv;
1229 return (videobuf_qbuf(get_queue(fh), p));
1232 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1234 struct cx8800_fh *fh = priv;
1235 return (videobuf_dqbuf(get_queue(fh), p,
1236 file->f_flags & O_NONBLOCK));
1239 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1241 struct cx8800_fh *fh = priv;
1242 struct cx8800_dev *dev = fh->dev;
1244 /* We should remember that this driver also supports teletext, */
1245 /* so we have to test if the v4l2_buf_type is VBI capture data. */
1246 if (unlikely((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1247 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)))
1250 if (unlikely(i != fh->type))
1253 if (unlikely(!res_get(dev,fh,get_ressource(fh))))
1255 return videobuf_streamon(get_queue(fh));
1258 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1260 struct cx8800_fh *fh = priv;
1261 struct cx8800_dev *dev = fh->dev;
1264 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1265 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
1271 res = get_ressource(fh);
1272 err = videobuf_streamoff(get_queue(fh));
1275 res_free(dev,fh,res);
1279 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
1281 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1283 mutex_lock(&core->lock);
1284 cx88_set_tvnorm(core,*tvnorms);
1285 mutex_unlock(&core->lock);
1290 /* only one input in this sample driver */
1291 int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
1293 static const char * const iname[] = {
1294 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1295 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1296 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1297 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1298 [ CX88_VMUX_SVIDEO ] = "S-Video",
1299 [ CX88_VMUX_TELEVISION ] = "Television",
1300 [ CX88_VMUX_CABLE ] = "Cable TV",
1301 [ CX88_VMUX_DVB ] = "DVB",
1302 [ CX88_VMUX_DEBUG ] = "for debug only",
1304 unsigned int n = i->index;
1308 if (0 == INPUT(n).type)
1310 i->type = V4L2_INPUT_TYPE_CAMERA;
1311 strcpy(i->name,iname[INPUT(n).type]);
1312 if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
1313 (CX88_VMUX_CABLE == INPUT(n).type)) {
1314 i->type = V4L2_INPUT_TYPE_TUNER;
1315 i->std = CX88_NORMS;
1319 EXPORT_SYMBOL(cx88_enum_input);
1321 static int vidioc_enum_input (struct file *file, void *priv,
1322 struct v4l2_input *i)
1324 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1325 return cx88_enum_input (core,i);
1328 static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1330 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1336 static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1338 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1343 mutex_lock(&core->lock);
1344 cx88_newstation(core);
1345 cx88_video_mux(core,i);
1346 mutex_unlock(&core->lock);
1352 static int vidioc_queryctrl (struct file *file, void *priv,
1353 struct v4l2_queryctrl *qctrl)
1355 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1357 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1358 if (unlikely(qctrl->id == 0))
1360 return cx8800_ctrl_query(core, qctrl);
1363 static int vidioc_g_ctrl (struct file *file, void *priv,
1364 struct v4l2_control *ctl)
1366 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1368 cx88_get_control(core,ctl);
1371 static int vidioc_s_ctrl (struct file *file, void *priv,
1372 struct v4l2_control *ctl)
1374 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1376 cx88_set_control(core,ctl);
1379 static int vidioc_g_tuner (struct file *file, void *priv,
1380 struct v4l2_tuner *t)
1382 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1385 if (unlikely(UNSET == core->board.tuner_type))
1390 strcpy(t->name, "Television");
1391 t->type = V4L2_TUNER_ANALOG_TV;
1392 t->capability = V4L2_TUNER_CAP_NORM;
1393 t->rangehigh = 0xffffffffUL;
1395 cx88_get_stereo(core ,t);
1396 reg = cx_read(MO_DEVICE_STATUS);
1397 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1401 static int vidioc_s_tuner (struct file *file, void *priv,
1402 struct v4l2_tuner *t)
1404 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1406 if (UNSET == core->board.tuner_type)
1411 cx88_set_stereo(core, t->audmode, 1);
1415 static int vidioc_g_frequency (struct file *file, void *priv,
1416 struct v4l2_frequency *f)
1418 struct cx8800_fh *fh = priv;
1419 struct cx88_core *core = fh->dev->core;
1421 if (unlikely(UNSET == core->board.tuner_type))
1424 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1425 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1426 f->frequency = core->freq;
1428 call_all(core, tuner, g_frequency, f);
1433 int cx88_set_freq (struct cx88_core *core,
1434 struct v4l2_frequency *f)
1436 if (unlikely(UNSET == core->board.tuner_type))
1438 if (unlikely(f->tuner != 0))
1441 mutex_lock(&core->lock);
1442 core->freq = f->frequency;
1443 cx88_newstation(core);
1444 call_all(core, tuner, s_frequency, f);
1446 /* When changing channels it is required to reset TVAUDIO */
1448 cx88_set_tvaudio(core);
1450 mutex_unlock(&core->lock);
1454 EXPORT_SYMBOL(cx88_set_freq);
1456 static int vidioc_s_frequency (struct file *file, void *priv,
1457 struct v4l2_frequency *f)
1459 struct cx8800_fh *fh = priv;
1460 struct cx88_core *core = fh->dev->core;
1462 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1464 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1468 cx88_set_freq (core,f);
1471 #ifdef CONFIG_VIDEO_ADV_DEBUG
1472 static int vidioc_g_register (struct file *file, void *fh,
1473 struct v4l2_dbg_register *reg)
1475 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1477 if (!v4l2_chip_match_host(®->match))
1479 /* cx2388x has a 24-bit register space */
1480 reg->val = cx_read(reg->reg & 0xffffff);
1485 static int vidioc_s_register (struct file *file, void *fh,
1486 struct v4l2_dbg_register *reg)
1488 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1490 if (!v4l2_chip_match_host(®->match))
1492 cx_write(reg->reg & 0xffffff, reg->val);
1497 /* ----------------------------------------------------------- */
1498 /* RADIO ESPECIFIC IOCTLS */
1499 /* ----------------------------------------------------------- */
1501 static int radio_querycap (struct file *file, void *priv,
1502 struct v4l2_capability *cap)
1504 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
1505 struct cx88_core *core = dev->core;
1507 strcpy(cap->driver, "cx8800");
1508 strlcpy(cap->card, core->board.name, sizeof(cap->card));
1509 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1510 cap->capabilities = V4L2_CAP_TUNER;
1514 static int radio_g_tuner (struct file *file, void *priv,
1515 struct v4l2_tuner *t)
1517 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1519 if (unlikely(t->index > 0))
1522 strcpy(t->name, "Radio");
1523 t->type = V4L2_TUNER_RADIO;
1525 call_all(core, tuner, g_tuner, t);
1529 static int radio_enum_input (struct file *file, void *priv,
1530 struct v4l2_input *i)
1534 strcpy(i->name,"Radio");
1535 i->type = V4L2_INPUT_TYPE_TUNER;
1540 static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
1542 if (unlikely(a->index))
1545 strcpy(a->name,"Radio");
1549 /* FIXME: Should add a standard for radio */
1551 static int radio_s_tuner (struct file *file, void *priv,
1552 struct v4l2_tuner *t)
1554 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1559 call_all(core, tuner, s_tuner, t);
1564 static int radio_s_audio (struct file *file, void *fh,
1565 struct v4l2_audio *a)
1570 static int radio_s_input (struct file *file, void *fh, unsigned int i)
1575 static int radio_queryctrl (struct file *file, void *priv,
1576 struct v4l2_queryctrl *c)
1580 if (c->id < V4L2_CID_BASE ||
1581 c->id >= V4L2_CID_LASTP1)
1583 if (c->id == V4L2_CID_AUDIO_MUTE ||
1584 c->id == V4L2_CID_AUDIO_VOLUME ||
1585 c->id == V4L2_CID_AUDIO_BALANCE) {
1586 for (i = 0; i < CX8800_CTLS; i++) {
1587 if (cx8800_ctls[i].v.id == c->id)
1590 if (i == CX8800_CTLS)
1592 *c = cx8800_ctls[i].v;
1598 /* ----------------------------------------------------------- */
1600 static void cx8800_vid_timeout(unsigned long data)
1602 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1603 struct cx88_core *core = dev->core;
1604 struct cx88_dmaqueue *q = &dev->vidq;
1605 struct cx88_buffer *buf;
1606 unsigned long flags;
1608 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1610 cx_clear(MO_VID_DMACNTRL, 0x11);
1611 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1613 spin_lock_irqsave(&dev->slock,flags);
1614 while (!list_empty(&q->active)) {
1615 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1616 list_del(&buf->vb.queue);
1617 buf->vb.state = VIDEOBUF_ERROR;
1618 wake_up(&buf->vb.done);
1619 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1620 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1622 restart_video_queue(dev,q);
1623 spin_unlock_irqrestore(&dev->slock,flags);
1626 static const char *cx88_vid_irqs[32] = {
1627 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1628 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1629 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1630 "y_sync", "u_sync", "v_sync", "vbi_sync",
1631 "opc_err", "par_err", "rip_err", "pci_abort",
1634 static void cx8800_vid_irq(struct cx8800_dev *dev)
1636 struct cx88_core *core = dev->core;
1637 u32 status, mask, count;
1639 status = cx_read(MO_VID_INTSTAT);
1640 mask = cx_read(MO_VID_INTMSK);
1641 if (0 == (status & mask))
1643 cx_write(MO_VID_INTSTAT, status);
1644 if (irq_debug || (status & mask & ~0xff))
1645 cx88_print_irqbits(core->name, "irq vid",
1646 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1649 /* risc op code error */
1650 if (status & (1 << 16)) {
1651 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1652 cx_clear(MO_VID_DMACNTRL, 0x11);
1653 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1654 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1658 if (status & 0x01) {
1659 spin_lock(&dev->slock);
1660 count = cx_read(MO_VIDY_GPCNT);
1661 cx88_wakeup(core, &dev->vidq, count);
1662 spin_unlock(&dev->slock);
1666 if (status & 0x08) {
1667 spin_lock(&dev->slock);
1668 count = cx_read(MO_VBI_GPCNT);
1669 cx88_wakeup(core, &dev->vbiq, count);
1670 spin_unlock(&dev->slock);
1674 if (status & 0x10) {
1675 dprintk(2,"stopper video\n");
1676 spin_lock(&dev->slock);
1677 restart_video_queue(dev,&dev->vidq);
1678 spin_unlock(&dev->slock);
1682 if (status & 0x80) {
1683 dprintk(2,"stopper vbi\n");
1684 spin_lock(&dev->slock);
1685 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1686 spin_unlock(&dev->slock);
1690 static irqreturn_t cx8800_irq(int irq, void *dev_id)
1692 struct cx8800_dev *dev = dev_id;
1693 struct cx88_core *core = dev->core;
1695 int loop, handled = 0;
1697 for (loop = 0; loop < 10; loop++) {
1698 status = cx_read(MO_PCI_INTSTAT) &
1699 (core->pci_irqmask | PCI_INT_VIDINT);
1702 cx_write(MO_PCI_INTSTAT, status);
1705 if (status & core->pci_irqmask)
1706 cx88_core_irq(core,status);
1707 if (status & PCI_INT_VIDINT)
1708 cx8800_vid_irq(dev);
1711 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1713 cx_write(MO_PCI_INTMSK,0);
1717 return IRQ_RETVAL(handled);
1720 /* ----------------------------------------------------------- */
1721 /* exported stuff */
1723 static const struct v4l2_file_operations video_fops =
1725 .owner = THIS_MODULE,
1727 .release = video_release,
1731 .unlocked_ioctl = video_ioctl2,
1734 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1735 .vidioc_querycap = vidioc_querycap,
1736 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1737 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1738 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1739 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1740 .vidioc_g_fmt_vbi_cap = cx8800_vbi_fmt,
1741 .vidioc_try_fmt_vbi_cap = cx8800_vbi_fmt,
1742 .vidioc_s_fmt_vbi_cap = cx8800_vbi_fmt,
1743 .vidioc_reqbufs = vidioc_reqbufs,
1744 .vidioc_querybuf = vidioc_querybuf,
1745 .vidioc_qbuf = vidioc_qbuf,
1746 .vidioc_dqbuf = vidioc_dqbuf,
1747 .vidioc_s_std = vidioc_s_std,
1748 .vidioc_enum_input = vidioc_enum_input,
1749 .vidioc_g_input = vidioc_g_input,
1750 .vidioc_s_input = vidioc_s_input,
1751 .vidioc_queryctrl = vidioc_queryctrl,
1752 .vidioc_g_ctrl = vidioc_g_ctrl,
1753 .vidioc_s_ctrl = vidioc_s_ctrl,
1754 .vidioc_streamon = vidioc_streamon,
1755 .vidioc_streamoff = vidioc_streamoff,
1756 .vidioc_g_tuner = vidioc_g_tuner,
1757 .vidioc_s_tuner = vidioc_s_tuner,
1758 .vidioc_g_frequency = vidioc_g_frequency,
1759 .vidioc_s_frequency = vidioc_s_frequency,
1760 #ifdef CONFIG_VIDEO_ADV_DEBUG
1761 .vidioc_g_register = vidioc_g_register,
1762 .vidioc_s_register = vidioc_s_register,
1766 static struct video_device cx8800_vbi_template;
1768 static const struct video_device cx8800_video_template = {
1769 .name = "cx8800-video",
1770 .fops = &video_fops,
1771 .ioctl_ops = &video_ioctl_ops,
1772 .tvnorms = CX88_NORMS,
1773 .current_norm = V4L2_STD_NTSC_M,
1776 static const struct v4l2_file_operations radio_fops =
1778 .owner = THIS_MODULE,
1780 .release = video_release,
1781 .unlocked_ioctl = video_ioctl2,
1784 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1785 .vidioc_querycap = radio_querycap,
1786 .vidioc_g_tuner = radio_g_tuner,
1787 .vidioc_enum_input = radio_enum_input,
1788 .vidioc_g_audio = radio_g_audio,
1789 .vidioc_s_tuner = radio_s_tuner,
1790 .vidioc_s_audio = radio_s_audio,
1791 .vidioc_s_input = radio_s_input,
1792 .vidioc_queryctrl = radio_queryctrl,
1793 .vidioc_g_ctrl = vidioc_g_ctrl,
1794 .vidioc_s_ctrl = vidioc_s_ctrl,
1795 .vidioc_g_frequency = vidioc_g_frequency,
1796 .vidioc_s_frequency = vidioc_s_frequency,
1797 #ifdef CONFIG_VIDEO_ADV_DEBUG
1798 .vidioc_g_register = vidioc_g_register,
1799 .vidioc_s_register = vidioc_s_register,
1803 static const struct video_device cx8800_radio_template = {
1804 .name = "cx8800-radio",
1805 .fops = &radio_fops,
1806 .ioctl_ops = &radio_ioctl_ops,
1809 /* ----------------------------------------------------------- */
1811 static void cx8800_unregister_video(struct cx8800_dev *dev)
1813 if (dev->radio_dev) {
1814 if (video_is_registered(dev->radio_dev))
1815 video_unregister_device(dev->radio_dev);
1817 video_device_release(dev->radio_dev);
1818 dev->radio_dev = NULL;
1821 if (video_is_registered(dev->vbi_dev))
1822 video_unregister_device(dev->vbi_dev);
1824 video_device_release(dev->vbi_dev);
1825 dev->vbi_dev = NULL;
1827 if (dev->video_dev) {
1828 if (video_is_registered(dev->video_dev))
1829 video_unregister_device(dev->video_dev);
1831 video_device_release(dev->video_dev);
1832 dev->video_dev = NULL;
1836 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1837 const struct pci_device_id *pci_id)
1839 struct cx8800_dev *dev;
1840 struct cx88_core *core;
1844 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1850 if (pci_enable_device(pci_dev)) {
1854 core = cx88_core_get(dev->pci);
1861 /* print pci info */
1862 dev->pci_rev = pci_dev->revision;
1863 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1864 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1865 "latency: %d, mmio: 0x%llx\n", core->name,
1866 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1867 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
1869 pci_set_master(pci_dev);
1870 if (!pci_dma_supported(pci_dev,DMA_BIT_MASK(32))) {
1871 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1876 /* Initialize VBI template */
1877 memcpy( &cx8800_vbi_template, &cx8800_video_template,
1878 sizeof(cx8800_vbi_template) );
1879 strcpy(cx8800_vbi_template.name,"cx8800-vbi");
1881 /* initialize driver struct */
1882 spin_lock_init(&dev->slock);
1883 core->tvnorm = cx8800_video_template.current_norm;
1885 /* init video dma queues */
1886 INIT_LIST_HEAD(&dev->vidq.active);
1887 INIT_LIST_HEAD(&dev->vidq.queued);
1888 dev->vidq.timeout.function = cx8800_vid_timeout;
1889 dev->vidq.timeout.data = (unsigned long)dev;
1890 init_timer(&dev->vidq.timeout);
1891 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1892 MO_VID_DMACNTRL,0x11,0x00);
1894 /* init vbi dma queues */
1895 INIT_LIST_HEAD(&dev->vbiq.active);
1896 INIT_LIST_HEAD(&dev->vbiq.queued);
1897 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1898 dev->vbiq.timeout.data = (unsigned long)dev;
1899 init_timer(&dev->vbiq.timeout);
1900 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1901 MO_VID_DMACNTRL,0x88,0x00);
1904 err = request_irq(pci_dev->irq, cx8800_irq,
1905 IRQF_SHARED | IRQF_DISABLED, core->name, dev);
1907 printk(KERN_ERR "%s/0: can't get IRQ %d\n",
1908 core->name,pci_dev->irq);
1911 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1913 /* load and configure helper modules */
1915 if (core->board.audio_chip == V4L2_IDENT_WM8775) {
1916 struct i2c_board_info wm8775_info = {
1919 .platform_data = &core->wm8775_data,
1921 struct v4l2_subdev *sd;
1923 if (core->boardnr == CX88_BOARD_HAUPPAUGE_NOVASPLUS_S1)
1924 core->wm8775_data.is_nova_s = true;
1926 core->wm8775_data.is_nova_s = false;
1928 sd = v4l2_i2c_new_subdev_board(&core->v4l2_dev, &core->i2c_adap,
1929 &wm8775_info, NULL);
1931 sd->grp_id = WM8775_GID;
1934 if (core->board.audio_chip == V4L2_IDENT_TVAUDIO) {
1935 /* This probes for a tda9874 as is used on some
1936 Pixelview Ultra boards. */
1937 v4l2_i2c_new_subdev(&core->v4l2_dev, &core->i2c_adap,
1938 "tvaudio", 0, I2C_ADDRS(0xb0 >> 1));
1941 switch (core->boardnr) {
1942 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
1943 case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD: {
1944 static const struct i2c_board_info rtc_info = {
1945 I2C_BOARD_INFO("isl1208", 0x6f)
1948 request_module("rtc-isl1208");
1949 core->i2c_rtc = i2c_new_device(&core->i2c_adap, &rtc_info);
1951 /* break intentionally omitted */
1952 case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
1953 request_module("ir-kbd-i2c");
1956 /* Sets device info at pci_dev */
1957 pci_set_drvdata(pci_dev, dev);
1959 /* initial device configuration */
1960 mutex_lock(&core->lock);
1961 cx88_set_tvnorm(core, core->tvnorm);
1962 init_controls(core);
1963 cx88_video_mux(core, 0);
1965 /* register v4l devices */
1966 dev->video_dev = cx88_vdev_init(core,dev->pci,
1967 &cx8800_video_template,"video");
1968 video_set_drvdata(dev->video_dev, dev);
1969 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1970 video_nr[core->nr]);
1972 printk(KERN_ERR "%s/0: can't register video device\n",
1976 printk(KERN_INFO "%s/0: registered device %s [v4l2]\n",
1977 core->name, video_device_node_name(dev->video_dev));
1979 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1980 video_set_drvdata(dev->vbi_dev, dev);
1981 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1984 printk(KERN_ERR "%s/0: can't register vbi device\n",
1988 printk(KERN_INFO "%s/0: registered device %s\n",
1989 core->name, video_device_node_name(dev->vbi_dev));
1991 if (core->board.radio.type == CX88_RADIO) {
1992 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1993 &cx8800_radio_template,"radio");
1994 video_set_drvdata(dev->radio_dev, dev);
1995 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1996 radio_nr[core->nr]);
1998 printk(KERN_ERR "%s/0: can't register radio device\n",
2002 printk(KERN_INFO "%s/0: registered device %s\n",
2003 core->name, video_device_node_name(dev->radio_dev));
2006 /* start tvaudio thread */
2007 if (core->board.tuner_type != TUNER_ABSENT) {
2008 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
2009 if (IS_ERR(core->kthread)) {
2010 err = PTR_ERR(core->kthread);
2011 printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
2015 mutex_unlock(&core->lock);
2020 cx8800_unregister_video(dev);
2021 free_irq(pci_dev->irq, dev);
2022 mutex_unlock(&core->lock);
2024 cx88_core_put(core,dev->pci);
2030 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
2032 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2033 struct cx88_core *core = dev->core;
2036 if (core->kthread) {
2037 kthread_stop(core->kthread);
2038 core->kthread = NULL;
2044 cx88_shutdown(core); /* FIXME */
2045 pci_disable_device(pci_dev);
2047 /* unregister stuff */
2049 free_irq(pci_dev->irq, dev);
2050 cx8800_unregister_video(dev);
2051 pci_set_drvdata(pci_dev, NULL);
2054 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
2055 cx88_core_put(core,dev->pci);
2060 static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
2062 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2063 struct cx88_core *core = dev->core;
2065 /* stop video+vbi capture */
2066 spin_lock(&dev->slock);
2067 if (!list_empty(&dev->vidq.active)) {
2068 printk("%s/0: suspend video\n", core->name);
2069 stop_video_dma(dev);
2070 del_timer(&dev->vidq.timeout);
2072 if (!list_empty(&dev->vbiq.active)) {
2073 printk("%s/0: suspend vbi\n", core->name);
2074 cx8800_stop_vbi_dma(dev);
2075 del_timer(&dev->vbiq.timeout);
2077 spin_unlock(&dev->slock);
2081 /* FIXME -- shutdown device */
2082 cx88_shutdown(core);
2084 pci_save_state(pci_dev);
2085 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
2086 pci_disable_device(pci_dev);
2087 dev->state.disabled = 1;
2092 static int cx8800_resume(struct pci_dev *pci_dev)
2094 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2095 struct cx88_core *core = dev->core;
2098 if (dev->state.disabled) {
2099 err=pci_enable_device(pci_dev);
2101 printk(KERN_ERR "%s/0: can't enable device\n",
2106 dev->state.disabled = 0;
2108 err= pci_set_power_state(pci_dev, PCI_D0);
2110 printk(KERN_ERR "%s/0: can't set power state\n", core->name);
2111 pci_disable_device(pci_dev);
2112 dev->state.disabled = 1;
2116 pci_restore_state(pci_dev);
2118 /* FIXME: re-initialize hardware */
2121 cx88_ir_start(core);
2123 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
2125 /* restart video+vbi capture */
2126 spin_lock(&dev->slock);
2127 if (!list_empty(&dev->vidq.active)) {
2128 printk("%s/0: resume video\n", core->name);
2129 restart_video_queue(dev,&dev->vidq);
2131 if (!list_empty(&dev->vbiq.active)) {
2132 printk("%s/0: resume vbi\n", core->name);
2133 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2135 spin_unlock(&dev->slock);
2141 /* ----------------------------------------------------------- */
2143 static const struct pci_device_id cx8800_pci_tbl[] = {
2147 .subvendor = PCI_ANY_ID,
2148 .subdevice = PCI_ANY_ID,
2150 /* --- end of list --- */
2153 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2155 static struct pci_driver cx8800_pci_driver = {
2157 .id_table = cx8800_pci_tbl,
2158 .probe = cx8800_initdev,
2159 .remove = __devexit_p(cx8800_finidev),
2161 .suspend = cx8800_suspend,
2162 .resume = cx8800_resume,
2166 static int __init cx8800_init(void)
2168 printk(KERN_INFO "cx88/0: cx2388x v4l2 driver version %s loaded\n",
2170 return pci_register_driver(&cx8800_pci_driver);
2173 static void __exit cx8800_fini(void)
2175 pci_unregister_driver(&cx8800_pci_driver);
2178 module_init(cx8800_init);
2179 module_exit(cx8800_fini);