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>
43 #ifdef CONFIG_VIDEO_V4L1_COMPAT
44 /* Include V4L1 specific functions. Should be removed soon */
45 #include <linux/videodev.h>
48 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
49 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
50 MODULE_LICENSE("GPL");
52 /* ------------------------------------------------------------------ */
54 static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
55 static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
56 static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
58 module_param_array(video_nr, int, NULL, 0444);
59 module_param_array(vbi_nr, int, NULL, 0444);
60 module_param_array(radio_nr, int, NULL, 0444);
62 MODULE_PARM_DESC(video_nr,"video device numbers");
63 MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
64 MODULE_PARM_DESC(radio_nr,"radio device numbers");
66 static unsigned int video_debug;
67 module_param(video_debug,int,0644);
68 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
70 static unsigned int irq_debug;
71 module_param(irq_debug,int,0644);
72 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
74 static unsigned int vid_limit = 16;
75 module_param(vid_limit,int,0644);
76 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
78 #define dprintk(level,fmt, arg...) if (video_debug >= level) \
79 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
81 /* ------------------------------------------------------------------ */
83 static LIST_HEAD(cx8800_devlist);
85 /* ------------------------------------------------------------------- */
88 static struct cx8800_fmt formats[] = {
90 .name = "8 bpp, gray",
91 .fourcc = V4L2_PIX_FMT_GREY,
92 .cxformat = ColorFormatY8,
94 .flags = FORMAT_FLAGS_PACKED,
96 .name = "15 bpp RGB, le",
97 .fourcc = V4L2_PIX_FMT_RGB555,
98 .cxformat = ColorFormatRGB15,
100 .flags = FORMAT_FLAGS_PACKED,
102 .name = "15 bpp RGB, be",
103 .fourcc = V4L2_PIX_FMT_RGB555X,
104 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
106 .flags = FORMAT_FLAGS_PACKED,
108 .name = "16 bpp RGB, le",
109 .fourcc = V4L2_PIX_FMT_RGB565,
110 .cxformat = ColorFormatRGB16,
112 .flags = FORMAT_FLAGS_PACKED,
114 .name = "16 bpp RGB, be",
115 .fourcc = V4L2_PIX_FMT_RGB565X,
116 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
118 .flags = FORMAT_FLAGS_PACKED,
120 .name = "24 bpp RGB, le",
121 .fourcc = V4L2_PIX_FMT_BGR24,
122 .cxformat = ColorFormatRGB24,
124 .flags = FORMAT_FLAGS_PACKED,
126 .name = "32 bpp RGB, le",
127 .fourcc = V4L2_PIX_FMT_BGR32,
128 .cxformat = ColorFormatRGB32,
130 .flags = FORMAT_FLAGS_PACKED,
132 .name = "32 bpp RGB, be",
133 .fourcc = V4L2_PIX_FMT_RGB32,
134 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
136 .flags = FORMAT_FLAGS_PACKED,
138 .name = "4:2:2, packed, YUYV",
139 .fourcc = V4L2_PIX_FMT_YUYV,
140 .cxformat = ColorFormatYUY2,
142 .flags = FORMAT_FLAGS_PACKED,
144 .name = "4:2:2, packed, UYVY",
145 .fourcc = V4L2_PIX_FMT_UYVY,
146 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
148 .flags = FORMAT_FLAGS_PACKED,
152 static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
156 for (i = 0; i < ARRAY_SIZE(formats); i++)
157 if (formats[i].fourcc == fourcc)
162 /* ------------------------------------------------------------------- */
164 static const struct v4l2_queryctrl no_ctl = {
166 .flags = V4L2_CTRL_FLAG_DISABLED,
169 static struct cx88_ctrl cx8800_ctls[] = {
173 .id = V4L2_CID_BRIGHTNESS,
174 .name = "Brightness",
178 .default_value = 0x7f,
179 .type = V4L2_CTRL_TYPE_INTEGER,
182 .reg = MO_CONTR_BRIGHT,
187 .id = V4L2_CID_CONTRAST,
192 .default_value = 0x3f,
193 .type = V4L2_CTRL_TYPE_INTEGER,
196 .reg = MO_CONTR_BRIGHT,
206 .default_value = 0x7f,
207 .type = V4L2_CTRL_TYPE_INTEGER,
214 /* strictly, this only describes only U saturation.
215 * V saturation is handled specially through code.
218 .id = V4L2_CID_SATURATION,
219 .name = "Saturation",
223 .default_value = 0x7f,
224 .type = V4L2_CTRL_TYPE_INTEGER,
227 .reg = MO_UV_SATURATION,
232 .id = V4L2_CID_CHROMA_AGC,
233 .name = "Chroma AGC",
236 .default_value = 0x1,
237 .type = V4L2_CTRL_TYPE_BOOLEAN,
239 .reg = MO_INPUT_FORMAT,
244 .id = V4L2_CID_COLOR_KILLER,
245 .name = "Color killer",
248 .default_value = 0x1,
249 .type = V4L2_CTRL_TYPE_BOOLEAN,
251 .reg = MO_INPUT_FORMAT,
257 .id = V4L2_CID_AUDIO_MUTE,
262 .type = V4L2_CTRL_TYPE_BOOLEAN,
265 .sreg = SHADOW_AUD_VOL_CTL,
270 .id = V4L2_CID_AUDIO_VOLUME,
275 .default_value = 0x3f,
276 .type = V4L2_CTRL_TYPE_INTEGER,
279 .sreg = SHADOW_AUD_VOL_CTL,
284 .id = V4L2_CID_AUDIO_BALANCE,
289 .default_value = 0x40,
290 .type = V4L2_CTRL_TYPE_INTEGER,
293 .sreg = SHADOW_AUD_BAL_CTL,
298 static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
300 const u32 cx88_user_ctrls[] = {
306 V4L2_CID_AUDIO_VOLUME,
307 V4L2_CID_AUDIO_BALANCE,
310 V4L2_CID_COLOR_KILLER,
313 EXPORT_SYMBOL(cx88_user_ctrls);
315 static const u32 *ctrl_classes[] = {
320 int cx8800_ctrl_query(struct cx88_core *core, struct v4l2_queryctrl *qctrl)
324 if (qctrl->id < V4L2_CID_BASE ||
325 qctrl->id >= V4L2_CID_LASTP1)
327 for (i = 0; i < CX8800_CTLS; i++)
328 if (cx8800_ctls[i].v.id == qctrl->id)
330 if (i == CX8800_CTLS) {
334 *qctrl = cx8800_ctls[i].v;
335 /* Report chroma AGC as inactive when SECAM is selected */
336 if (cx8800_ctls[i].v.id == V4L2_CID_CHROMA_AGC &&
337 core->tvnorm & V4L2_STD_SECAM)
338 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
342 EXPORT_SYMBOL(cx8800_ctrl_query);
344 /* ------------------------------------------------------------------- */
345 /* resource management */
347 static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
349 struct cx88_core *core = dev->core;
350 if (fh->resources & bit)
351 /* have it already allocated */
355 mutex_lock(&core->lock);
356 if (dev->resources & bit) {
357 /* no, someone else uses it */
358 mutex_unlock(&core->lock);
361 /* it's free, grab it */
362 fh->resources |= bit;
363 dev->resources |= bit;
364 dprintk(1,"res: get %d\n",bit);
365 mutex_unlock(&core->lock);
370 int res_check(struct cx8800_fh *fh, unsigned int bit)
372 return (fh->resources & bit);
376 int res_locked(struct cx8800_dev *dev, unsigned int bit)
378 return (dev->resources & bit);
382 void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
384 struct cx88_core *core = dev->core;
385 BUG_ON((fh->resources & bits) != bits);
387 mutex_lock(&core->lock);
388 fh->resources &= ~bits;
389 dev->resources &= ~bits;
390 dprintk(1,"res: put %d\n",bits);
391 mutex_unlock(&core->lock);
394 /* ------------------------------------------------------------------ */
396 int cx88_video_mux(struct cx88_core *core, unsigned int input)
398 /* struct cx88_core *core = dev->core; */
400 dprintk(1,"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);
405 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
406 cx_write(MO_GP3_IO, INPUT(input).gpio3);
407 cx_write(MO_GP0_IO, INPUT(input).gpio0);
408 cx_write(MO_GP1_IO, INPUT(input).gpio1);
409 cx_write(MO_GP2_IO, INPUT(input).gpio2);
411 switch (INPUT(input).type) {
412 case CX88_VMUX_SVIDEO:
413 cx_set(MO_AFECFG_IO, 0x00000001);
414 cx_set(MO_INPUT_FORMAT, 0x00010010);
415 cx_set(MO_FILTER_EVEN, 0x00002020);
416 cx_set(MO_FILTER_ODD, 0x00002020);
419 cx_clear(MO_AFECFG_IO, 0x00000001);
420 cx_clear(MO_INPUT_FORMAT, 0x00010010);
421 cx_clear(MO_FILTER_EVEN, 0x00002020);
422 cx_clear(MO_FILTER_ODD, 0x00002020);
426 /* if there are audioroutes defined, we have an external
427 ADC to deal with audio */
429 if (INPUT(input).audioroute) {
431 /* cx2388's C-ADC is connected to the tuner only.
432 When used with S-Video, that ADC is busy dealing with
433 chroma, so an external must be used for baseband audio */
435 if (INPUT(input).type != CX88_VMUX_TELEVISION &&
436 INPUT(input).type != CX88_RADIO) {
438 cx_write(AUD_I2SCNTL, 0x1);
439 cx_set(AUD_CTL, EN_I2SIN_ENABLE);
442 cx_write(AUD_I2SCNTL, 0x0);
443 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
446 /* The wm8775 module has the "2" route hardwired into
447 the initialization. Some boards may use different
448 routes for different inputs. HVR-1300 surely does */
449 if (core->board.audio_chip &&
450 core->board.audio_chip == AUDIO_CHIP_WM8775) {
451 struct v4l2_routing route;
453 route.input = INPUT(input).audioroute;
454 cx88_call_i2c_clients(core,
455 VIDIOC_INT_S_AUDIO_ROUTING, &route);
463 EXPORT_SYMBOL(cx88_video_mux);
465 /* ------------------------------------------------------------------ */
467 static int start_video_dma(struct cx8800_dev *dev,
468 struct cx88_dmaqueue *q,
469 struct cx88_buffer *buf)
471 struct cx88_core *core = dev->core;
473 /* setup fifo + format */
474 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
475 buf->bpl, buf->risc.dma);
476 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
477 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
480 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
484 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
486 /* Enables corresponding bits at PCI_INT_STAT:
487 bits 0 to 4: video, audio, transport stream, VIP, Host
489 bits 8 and 9: DMA complete for: SRC, DST
490 bits 10 and 11: BERR signal asserted for RISC: RD, WR
491 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
493 cx_set(MO_VID_INTMSK, 0x0f0011);
496 cx_set(VID_CAPTURE_CONTROL,0x06);
499 cx_set(MO_DEV_CNTRL2, (1<<5));
500 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
506 static int stop_video_dma(struct cx8800_dev *dev)
508 struct cx88_core *core = dev->core;
511 cx_clear(MO_VID_DMACNTRL, 0x11);
513 /* disable capture */
514 cx_clear(VID_CAPTURE_CONTROL,0x06);
517 cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
518 cx_clear(MO_VID_INTMSK, 0x0f0011);
523 static int restart_video_queue(struct cx8800_dev *dev,
524 struct cx88_dmaqueue *q)
526 struct cx88_core *core = dev->core;
527 struct cx88_buffer *buf, *prev;
529 if (!list_empty(&q->active)) {
530 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
531 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
533 start_video_dma(dev, q, buf);
534 list_for_each_entry(buf, &q->active, vb.queue)
535 buf->count = q->count++;
536 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
542 if (list_empty(&q->queued))
544 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
546 list_move_tail(&buf->vb.queue, &q->active);
547 start_video_dma(dev, q, buf);
548 buf->vb.state = VIDEOBUF_ACTIVE;
549 buf->count = q->count++;
550 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
551 dprintk(2,"[%p/%d] restart_queue - first active\n",
554 } else if (prev->vb.width == buf->vb.width &&
555 prev->vb.height == buf->vb.height &&
556 prev->fmt == buf->fmt) {
557 list_move_tail(&buf->vb.queue, &q->active);
558 buf->vb.state = VIDEOBUF_ACTIVE;
559 buf->count = q->count++;
560 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
561 dprintk(2,"[%p/%d] restart_queue - move to active\n",
570 /* ------------------------------------------------------------------ */
573 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
575 struct cx8800_fh *fh = q->priv_data;
577 *size = fh->fmt->depth*fh->width*fh->height >> 3;
580 while (*size * *count > vid_limit * 1024 * 1024)
586 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
587 enum v4l2_field field)
589 struct cx8800_fh *fh = q->priv_data;
590 struct cx8800_dev *dev = fh->dev;
591 struct cx88_core *core = dev->core;
592 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
593 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
594 int rc, init_buffer = 0;
596 BUG_ON(NULL == fh->fmt);
597 if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
598 fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
600 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
601 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
604 if (buf->fmt != fh->fmt ||
605 buf->vb.width != fh->width ||
606 buf->vb.height != fh->height ||
607 buf->vb.field != field) {
609 buf->vb.width = fh->width;
610 buf->vb.height = fh->height;
611 buf->vb.field = field;
615 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
617 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
622 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
623 switch (buf->vb.field) {
625 cx88_risc_buffer(dev->pci, &buf->risc,
626 dma->sglist, 0, UNSET,
627 buf->bpl, 0, buf->vb.height);
629 case V4L2_FIELD_BOTTOM:
630 cx88_risc_buffer(dev->pci, &buf->risc,
631 dma->sglist, UNSET, 0,
632 buf->bpl, 0, buf->vb.height);
634 case V4L2_FIELD_INTERLACED:
635 cx88_risc_buffer(dev->pci, &buf->risc,
636 dma->sglist, 0, buf->bpl,
638 buf->vb.height >> 1);
640 case V4L2_FIELD_SEQ_TB:
641 cx88_risc_buffer(dev->pci, &buf->risc,
643 0, buf->bpl * (buf->vb.height >> 1),
645 buf->vb.height >> 1);
647 case V4L2_FIELD_SEQ_BT:
648 cx88_risc_buffer(dev->pci, &buf->risc,
650 buf->bpl * (buf->vb.height >> 1), 0,
652 buf->vb.height >> 1);
658 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
660 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
661 (unsigned long)buf->risc.dma);
663 buf->vb.state = VIDEOBUF_PREPARED;
667 cx88_free_buffer(q,buf);
672 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
674 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
675 struct cx88_buffer *prev;
676 struct cx8800_fh *fh = vq->priv_data;
677 struct cx8800_dev *dev = fh->dev;
678 struct cx88_core *core = dev->core;
679 struct cx88_dmaqueue *q = &dev->vidq;
681 /* add jump to stopper */
682 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
683 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
685 if (!list_empty(&q->queued)) {
686 list_add_tail(&buf->vb.queue,&q->queued);
687 buf->vb.state = VIDEOBUF_QUEUED;
688 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
691 } else if (list_empty(&q->active)) {
692 list_add_tail(&buf->vb.queue,&q->active);
693 start_video_dma(dev, q, buf);
694 buf->vb.state = VIDEOBUF_ACTIVE;
695 buf->count = q->count++;
696 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
697 dprintk(2,"[%p/%d] buffer_queue - first active\n",
701 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
702 if (prev->vb.width == buf->vb.width &&
703 prev->vb.height == buf->vb.height &&
704 prev->fmt == buf->fmt) {
705 list_add_tail(&buf->vb.queue,&q->active);
706 buf->vb.state = VIDEOBUF_ACTIVE;
707 buf->count = q->count++;
708 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
709 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
713 list_add_tail(&buf->vb.queue,&q->queued);
714 buf->vb.state = VIDEOBUF_QUEUED;
715 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
721 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
723 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
725 cx88_free_buffer(q,buf);
728 static struct videobuf_queue_ops cx8800_video_qops = {
729 .buf_setup = buffer_setup,
730 .buf_prepare = buffer_prepare,
731 .buf_queue = buffer_queue,
732 .buf_release = buffer_release,
735 /* ------------------------------------------------------------------ */
738 /* ------------------------------------------------------------------ */
740 static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
743 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
745 case V4L2_BUF_TYPE_VBI_CAPTURE:
753 static int get_ressource(struct cx8800_fh *fh)
756 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
757 return RESOURCE_VIDEO;
758 case V4L2_BUF_TYPE_VBI_CAPTURE:
766 static int video_open(struct inode *inode, struct file *file)
768 int minor = iminor(inode);
769 struct cx8800_dev *h,*dev = NULL;
770 struct cx88_core *core;
771 struct cx8800_fh *fh;
772 enum v4l2_buf_type type = 0;
775 list_for_each_entry(h, &cx8800_devlist, devlist) {
776 if (h->video_dev->minor == minor) {
778 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
780 if (h->vbi_dev->minor == minor) {
782 type = V4L2_BUF_TYPE_VBI_CAPTURE;
785 h->radio_dev->minor == minor) {
795 dprintk(1,"open minor=%d radio=%d type=%s\n",
796 minor,radio,v4l2_type_names[type]);
798 /* allocate + initialize per filehandle data */
799 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
802 file->private_data = fh;
808 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
810 videobuf_queue_sg_init(&fh->vidq, &cx8800_video_qops,
811 &dev->pci->dev, &dev->slock,
812 V4L2_BUF_TYPE_VIDEO_CAPTURE,
813 V4L2_FIELD_INTERLACED,
814 sizeof(struct cx88_buffer),
816 videobuf_queue_sg_init(&fh->vbiq, &cx8800_vbi_qops,
817 &dev->pci->dev, &dev->slock,
818 V4L2_BUF_TYPE_VBI_CAPTURE,
820 sizeof(struct cx88_buffer),
824 dprintk(1,"video_open: setting radio device\n");
825 cx_write(MO_GP3_IO, core->board.radio.gpio3);
826 cx_write(MO_GP0_IO, core->board.radio.gpio0);
827 cx_write(MO_GP1_IO, core->board.radio.gpio1);
828 cx_write(MO_GP2_IO, core->board.radio.gpio2);
829 core->tvaudio = WW_FM;
830 cx88_set_tvaudio(core);
831 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
832 cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
839 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
841 struct cx8800_fh *fh = file->private_data;
844 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
845 if (res_locked(fh->dev,RESOURCE_VIDEO))
847 return videobuf_read_one(&fh->vidq, data, count, ppos,
848 file->f_flags & O_NONBLOCK);
849 case V4L2_BUF_TYPE_VBI_CAPTURE:
850 if (!res_get(fh->dev,fh,RESOURCE_VBI))
852 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
853 file->f_flags & O_NONBLOCK);
861 video_poll(struct file *file, struct poll_table_struct *wait)
863 struct cx8800_fh *fh = file->private_data;
864 struct cx88_buffer *buf;
866 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
867 if (!res_get(fh->dev,fh,RESOURCE_VBI))
869 return videobuf_poll_stream(file, &fh->vbiq, wait);
872 if (res_check(fh,RESOURCE_VIDEO)) {
873 /* streaming capture */
874 if (list_empty(&fh->vidq.stream))
876 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
879 buf = (struct cx88_buffer*)fh->vidq.read_buf;
883 poll_wait(file, &buf->vb.done, wait);
884 if (buf->vb.state == VIDEOBUF_DONE ||
885 buf->vb.state == VIDEOBUF_ERROR)
886 return POLLIN|POLLRDNORM;
890 static int video_release(struct inode *inode, struct file *file)
892 struct cx8800_fh *fh = file->private_data;
893 struct cx8800_dev *dev = fh->dev;
895 /* turn off overlay */
896 if (res_check(fh, RESOURCE_OVERLAY)) {
898 res_free(dev,fh,RESOURCE_OVERLAY);
901 /* stop video capture */
902 if (res_check(fh, RESOURCE_VIDEO)) {
903 videobuf_queue_cancel(&fh->vidq);
904 res_free(dev,fh,RESOURCE_VIDEO);
906 if (fh->vidq.read_buf) {
907 buffer_release(&fh->vidq,fh->vidq.read_buf);
908 kfree(fh->vidq.read_buf);
911 /* stop vbi capture */
912 if (res_check(fh, RESOURCE_VBI)) {
913 videobuf_stop(&fh->vbiq);
914 res_free(dev,fh,RESOURCE_VBI);
917 videobuf_mmap_free(&fh->vidq);
918 videobuf_mmap_free(&fh->vbiq);
919 file->private_data = NULL;
922 cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
928 video_mmap(struct file *file, struct vm_area_struct * vma)
930 struct cx8800_fh *fh = file->private_data;
932 return videobuf_mmap_mapper(get_queue(fh), vma);
935 /* ------------------------------------------------------------------ */
936 /* VIDEO CTRL IOCTLS */
938 int cx88_get_control (struct cx88_core *core, struct v4l2_control *ctl)
940 struct cx88_ctrl *c = NULL;
944 for (i = 0; i < CX8800_CTLS; i++)
945 if (cx8800_ctls[i].v.id == ctl->id)
947 if (unlikely(NULL == c))
950 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
952 case V4L2_CID_AUDIO_BALANCE:
953 ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
954 : (0x7f - (value & 0x7f));
956 case V4L2_CID_AUDIO_VOLUME:
957 ctl->value = 0x3f - (value & 0x3f);
960 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
963 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
964 ctl->id, c->v.name, ctl->value, c->reg,
965 value,c->mask, c->sreg ? " [shadowed]" : "");
968 EXPORT_SYMBOL(cx88_get_control);
970 int cx88_set_control(struct cx88_core *core, struct v4l2_control *ctl)
972 struct cx88_ctrl *c = NULL;
976 for (i = 0; i < CX8800_CTLS; i++) {
977 if (cx8800_ctls[i].v.id == ctl->id) {
981 if (unlikely(NULL == c))
984 if (ctl->value < c->v.minimum)
985 ctl->value = c->v.minimum;
986 if (ctl->value > c->v.maximum)
987 ctl->value = c->v.maximum;
990 case V4L2_CID_AUDIO_BALANCE:
991 value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
993 case V4L2_CID_AUDIO_VOLUME:
994 value = 0x3f - (ctl->value & 0x3f);
996 case V4L2_CID_SATURATION:
997 /* special v_sat handling */
999 value = ((ctl->value - c->off) << c->shift) & c->mask;
1001 if (core->tvnorm & V4L2_STD_SECAM) {
1002 /* For SECAM, both U and V sat should be equal */
1003 value=value<<8|value;
1005 /* Keeps U Saturation proportional to V Sat */
1006 value=(value*0x5a)/0x7f<<8|value;
1010 case V4L2_CID_CHROMA_AGC:
1011 /* Do not allow chroma AGC to be enabled for SECAM */
1012 value = ((ctl->value - c->off) << c->shift) & c->mask;
1013 if (core->tvnorm & V4L2_STD_SECAM && value)
1017 value = ((ctl->value - c->off) << c->shift) & c->mask;
1020 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
1021 ctl->id, c->v.name, ctl->value, c->reg, value,
1022 mask, c->sreg ? " [shadowed]" : "");
1024 cx_sandor(c->sreg, c->reg, mask, value);
1026 cx_andor(c->reg, mask, value);
1030 EXPORT_SYMBOL(cx88_set_control);
1032 static void init_controls(struct cx88_core *core)
1034 struct v4l2_control ctrl;
1037 for (i = 0; i < CX8800_CTLS; i++) {
1038 ctrl.id=cx8800_ctls[i].v.id;
1039 ctrl.value=cx8800_ctls[i].v.default_value;
1041 cx88_set_control(core, &ctrl);
1045 /* ------------------------------------------------------------------ */
1048 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1049 struct v4l2_format *f)
1051 struct cx8800_fh *fh = priv;
1053 f->fmt.pix.width = fh->width;
1054 f->fmt.pix.height = fh->height;
1055 f->fmt.pix.field = fh->vidq.field;
1056 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1057 f->fmt.pix.bytesperline =
1058 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1059 f->fmt.pix.sizeimage =
1060 f->fmt.pix.height * f->fmt.pix.bytesperline;
1064 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1065 struct v4l2_format *f)
1067 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1068 struct cx8800_fmt *fmt;
1069 enum v4l2_field field;
1070 unsigned int maxw, maxh;
1072 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1076 field = f->fmt.pix.field;
1077 maxw = norm_maxw(core->tvnorm);
1078 maxh = norm_maxh(core->tvnorm);
1080 if (V4L2_FIELD_ANY == field) {
1081 field = (f->fmt.pix.height > maxh/2)
1082 ? V4L2_FIELD_INTERLACED
1083 : V4L2_FIELD_BOTTOM;
1087 case V4L2_FIELD_TOP:
1088 case V4L2_FIELD_BOTTOM:
1091 case V4L2_FIELD_INTERLACED:
1097 f->fmt.pix.field = field;
1098 if (f->fmt.pix.height < 32)
1099 f->fmt.pix.height = 32;
1100 if (f->fmt.pix.height > maxh)
1101 f->fmt.pix.height = maxh;
1102 if (f->fmt.pix.width < 48)
1103 f->fmt.pix.width = 48;
1104 if (f->fmt.pix.width > maxw)
1105 f->fmt.pix.width = maxw;
1106 f->fmt.pix.width &= ~0x03;
1107 f->fmt.pix.bytesperline =
1108 (f->fmt.pix.width * fmt->depth) >> 3;
1109 f->fmt.pix.sizeimage =
1110 f->fmt.pix.height * f->fmt.pix.bytesperline;
1115 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1116 struct v4l2_format *f)
1118 struct cx8800_fh *fh = priv;
1119 int err = vidioc_try_fmt_vid_cap (file,priv,f);
1123 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1124 fh->width = f->fmt.pix.width;
1125 fh->height = f->fmt.pix.height;
1126 fh->vidq.field = f->fmt.pix.field;
1130 static int vidioc_querycap (struct file *file, void *priv,
1131 struct v4l2_capability *cap)
1133 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
1134 struct cx88_core *core = dev->core;
1136 strcpy(cap->driver, "cx8800");
1137 strlcpy(cap->card, core->board.name, sizeof(cap->card));
1138 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1139 cap->version = CX88_VERSION_CODE;
1141 V4L2_CAP_VIDEO_CAPTURE |
1142 V4L2_CAP_READWRITE |
1143 V4L2_CAP_STREAMING |
1144 V4L2_CAP_VBI_CAPTURE;
1145 if (UNSET != core->board.tuner_type)
1146 cap->capabilities |= V4L2_CAP_TUNER;
1150 static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
1151 struct v4l2_fmtdesc *f)
1153 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1156 strlcpy(f->description,formats[f->index].name,sizeof(f->description));
1157 f->pixelformat = formats[f->index].fourcc;
1162 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1163 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
1165 struct cx8800_fh *fh = priv;
1167 return videobuf_cgmbuf (get_queue(fh), mbuf, 8);
1171 static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
1173 struct cx8800_fh *fh = priv;
1174 return (videobuf_reqbufs(get_queue(fh), p));
1177 static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
1179 struct cx8800_fh *fh = priv;
1180 return (videobuf_querybuf(get_queue(fh), p));
1183 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1185 struct cx8800_fh *fh = priv;
1186 return (videobuf_qbuf(get_queue(fh), p));
1189 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1191 struct cx8800_fh *fh = priv;
1192 return (videobuf_dqbuf(get_queue(fh), p,
1193 file->f_flags & O_NONBLOCK));
1196 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1198 struct cx8800_fh *fh = priv;
1199 struct cx8800_dev *dev = fh->dev;
1201 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1203 if (unlikely(i != fh->type))
1206 if (unlikely(!res_get(dev,fh,get_ressource(fh))))
1208 return videobuf_streamon(get_queue(fh));
1211 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1213 struct cx8800_fh *fh = priv;
1214 struct cx8800_dev *dev = fh->dev;
1217 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1222 res = get_ressource(fh);
1223 err = videobuf_streamoff(get_queue(fh));
1226 res_free(dev,fh,res);
1230 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
1232 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1234 mutex_lock(&core->lock);
1235 cx88_set_tvnorm(core,*tvnorms);
1236 mutex_unlock(&core->lock);
1241 /* only one input in this sample driver */
1242 int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
1244 static const char *iname[] = {
1245 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1246 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1247 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1248 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1249 [ CX88_VMUX_SVIDEO ] = "S-Video",
1250 [ CX88_VMUX_TELEVISION ] = "Television",
1251 [ CX88_VMUX_CABLE ] = "Cable TV",
1252 [ CX88_VMUX_DVB ] = "DVB",
1253 [ CX88_VMUX_DEBUG ] = "for debug only",
1260 if (0 == INPUT(n).type)
1262 memset(i,0,sizeof(*i));
1264 i->type = V4L2_INPUT_TYPE_CAMERA;
1265 strcpy(i->name,iname[INPUT(n).type]);
1266 if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
1267 (CX88_VMUX_CABLE == INPUT(n).type))
1268 i->type = V4L2_INPUT_TYPE_TUNER;
1269 i->std = CX88_NORMS;
1272 EXPORT_SYMBOL(cx88_enum_input);
1274 static int vidioc_enum_input (struct file *file, void *priv,
1275 struct v4l2_input *i)
1277 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1278 return cx88_enum_input (core,i);
1281 static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1283 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1289 static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1291 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1296 mutex_lock(&core->lock);
1297 cx88_newstation(core);
1298 cx88_video_mux(core,i);
1299 mutex_unlock(&core->lock);
1305 static int vidioc_queryctrl (struct file *file, void *priv,
1306 struct v4l2_queryctrl *qctrl)
1308 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1310 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1311 if (unlikely(qctrl->id == 0))
1313 return cx8800_ctrl_query(core, qctrl);
1316 static int vidioc_g_ctrl (struct file *file, void *priv,
1317 struct v4l2_control *ctl)
1319 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1321 cx88_get_control(core,ctl);
1324 static int vidioc_s_ctrl (struct file *file, void *priv,
1325 struct v4l2_control *ctl)
1327 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1329 cx88_set_control(core,ctl);
1332 static int vidioc_g_tuner (struct file *file, void *priv,
1333 struct v4l2_tuner *t)
1335 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1338 if (unlikely(UNSET == core->board.tuner_type))
1343 strcpy(t->name, "Television");
1344 t->type = V4L2_TUNER_ANALOG_TV;
1345 t->capability = V4L2_TUNER_CAP_NORM;
1346 t->rangehigh = 0xffffffffUL;
1348 cx88_get_stereo(core ,t);
1349 reg = cx_read(MO_DEVICE_STATUS);
1350 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1354 static int vidioc_s_tuner (struct file *file, void *priv,
1355 struct v4l2_tuner *t)
1357 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1359 if (UNSET == core->board.tuner_type)
1364 cx88_set_stereo(core, t->audmode, 1);
1368 static int vidioc_g_frequency (struct file *file, void *priv,
1369 struct v4l2_frequency *f)
1371 struct cx8800_fh *fh = priv;
1372 struct cx88_core *core = fh->dev->core;
1374 if (unlikely(UNSET == core->board.tuner_type))
1377 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1378 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1379 f->frequency = core->freq;
1381 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1386 int cx88_set_freq (struct cx88_core *core,
1387 struct v4l2_frequency *f)
1389 if (unlikely(UNSET == core->board.tuner_type))
1391 if (unlikely(f->tuner != 0))
1394 mutex_lock(&core->lock);
1395 core->freq = f->frequency;
1396 cx88_newstation(core);
1397 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1399 /* When changing channels it is required to reset TVAUDIO */
1401 cx88_set_tvaudio(core);
1403 mutex_unlock(&core->lock);
1407 EXPORT_SYMBOL(cx88_set_freq);
1409 static int vidioc_s_frequency (struct file *file, void *priv,
1410 struct v4l2_frequency *f)
1412 struct cx8800_fh *fh = priv;
1413 struct cx88_core *core = fh->dev->core;
1415 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1417 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1421 cx88_set_freq (core,f);
1424 #ifdef CONFIG_VIDEO_ADV_DEBUG
1425 static int vidioc_g_register (struct file *file, void *fh,
1426 struct v4l2_register *reg)
1428 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1430 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1432 /* cx2388x has a 24-bit register space */
1433 reg->val = cx_read(reg->reg&0xffffff);
1437 static int vidioc_s_register (struct file *file, void *fh,
1438 struct v4l2_register *reg)
1440 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1442 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1444 cx_write(reg->reg&0xffffff, reg->val);
1449 /* ----------------------------------------------------------- */
1450 /* RADIO ESPECIFIC IOCTLS */
1451 /* ----------------------------------------------------------- */
1453 static int radio_querycap (struct file *file, void *priv,
1454 struct v4l2_capability *cap)
1456 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
1457 struct cx88_core *core = dev->core;
1459 strcpy(cap->driver, "cx8800");
1460 strlcpy(cap->card, core->board.name, sizeof(cap->card));
1461 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1462 cap->version = CX88_VERSION_CODE;
1463 cap->capabilities = V4L2_CAP_TUNER;
1467 static int radio_g_tuner (struct file *file, void *priv,
1468 struct v4l2_tuner *t)
1470 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1472 if (unlikely(t->index > 0))
1475 strcpy(t->name, "Radio");
1476 t->type = V4L2_TUNER_RADIO;
1478 cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
1482 static int radio_enum_input (struct file *file, void *priv,
1483 struct v4l2_input *i)
1487 strcpy(i->name,"Radio");
1488 i->type = V4L2_INPUT_TYPE_TUNER;
1493 static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
1495 if (unlikely(a->index))
1498 memset(a,0,sizeof(*a));
1499 strcpy(a->name,"Radio");
1503 /* FIXME: Should add a standard for radio */
1505 static int radio_s_tuner (struct file *file, void *priv,
1506 struct v4l2_tuner *t)
1508 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1513 cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1518 static int radio_s_audio (struct file *file, void *fh,
1519 struct v4l2_audio *a)
1524 static int radio_s_input (struct file *file, void *fh, unsigned int i)
1529 static int radio_queryctrl (struct file *file, void *priv,
1530 struct v4l2_queryctrl *c)
1534 if (c->id < V4L2_CID_BASE ||
1535 c->id >= V4L2_CID_LASTP1)
1537 if (c->id == V4L2_CID_AUDIO_MUTE) {
1538 for (i = 0; i < CX8800_CTLS; i++)
1539 if (cx8800_ctls[i].v.id == c->id)
1541 *c = cx8800_ctls[i].v;
1547 /* ----------------------------------------------------------- */
1549 static void cx8800_vid_timeout(unsigned long data)
1551 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1552 struct cx88_core *core = dev->core;
1553 struct cx88_dmaqueue *q = &dev->vidq;
1554 struct cx88_buffer *buf;
1555 unsigned long flags;
1557 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1559 cx_clear(MO_VID_DMACNTRL, 0x11);
1560 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1562 spin_lock_irqsave(&dev->slock,flags);
1563 while (!list_empty(&q->active)) {
1564 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1565 list_del(&buf->vb.queue);
1566 buf->vb.state = VIDEOBUF_ERROR;
1567 wake_up(&buf->vb.done);
1568 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1569 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1571 restart_video_queue(dev,q);
1572 spin_unlock_irqrestore(&dev->slock,flags);
1575 static char *cx88_vid_irqs[32] = {
1576 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1577 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1578 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1579 "y_sync", "u_sync", "v_sync", "vbi_sync",
1580 "opc_err", "par_err", "rip_err", "pci_abort",
1583 static void cx8800_vid_irq(struct cx8800_dev *dev)
1585 struct cx88_core *core = dev->core;
1586 u32 status, mask, count;
1588 status = cx_read(MO_VID_INTSTAT);
1589 mask = cx_read(MO_VID_INTMSK);
1590 if (0 == (status & mask))
1592 cx_write(MO_VID_INTSTAT, status);
1593 if (irq_debug || (status & mask & ~0xff))
1594 cx88_print_irqbits(core->name, "irq vid",
1595 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1598 /* risc op code error */
1599 if (status & (1 << 16)) {
1600 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1601 cx_clear(MO_VID_DMACNTRL, 0x11);
1602 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1603 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1607 if (status & 0x01) {
1608 spin_lock(&dev->slock);
1609 count = cx_read(MO_VIDY_GPCNT);
1610 cx88_wakeup(core, &dev->vidq, count);
1611 spin_unlock(&dev->slock);
1615 if (status & 0x08) {
1616 spin_lock(&dev->slock);
1617 count = cx_read(MO_VBI_GPCNT);
1618 cx88_wakeup(core, &dev->vbiq, count);
1619 spin_unlock(&dev->slock);
1623 if (status & 0x10) {
1624 dprintk(2,"stopper video\n");
1625 spin_lock(&dev->slock);
1626 restart_video_queue(dev,&dev->vidq);
1627 spin_unlock(&dev->slock);
1631 if (status & 0x80) {
1632 dprintk(2,"stopper vbi\n");
1633 spin_lock(&dev->slock);
1634 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1635 spin_unlock(&dev->slock);
1639 static irqreturn_t cx8800_irq(int irq, void *dev_id)
1641 struct cx8800_dev *dev = dev_id;
1642 struct cx88_core *core = dev->core;
1644 int loop, handled = 0;
1646 for (loop = 0; loop < 10; loop++) {
1647 status = cx_read(MO_PCI_INTSTAT) &
1648 (core->pci_irqmask | PCI_INT_VIDINT);
1651 cx_write(MO_PCI_INTSTAT, status);
1654 if (status & core->pci_irqmask)
1655 cx88_core_irq(core,status);
1656 if (status & PCI_INT_VIDINT)
1657 cx8800_vid_irq(dev);
1660 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1662 cx_write(MO_PCI_INTMSK,0);
1666 return IRQ_RETVAL(handled);
1669 /* ----------------------------------------------------------- */
1670 /* exported stuff */
1672 static const struct file_operations video_fops =
1674 .owner = THIS_MODULE,
1676 .release = video_release,
1680 .ioctl = video_ioctl2,
1681 .compat_ioctl = v4l_compat_ioctl32,
1682 .llseek = no_llseek,
1685 static struct video_device cx8800_vbi_template;
1686 static struct video_device cx8800_video_template =
1688 .name = "cx8800-video",
1689 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1690 .fops = &video_fops,
1692 .vidioc_querycap = vidioc_querycap,
1693 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1694 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1695 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1696 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1697 .vidioc_g_fmt_vbi_cap = cx8800_vbi_fmt,
1698 .vidioc_try_fmt_vbi_cap = cx8800_vbi_fmt,
1699 .vidioc_s_fmt_vbi_cap = cx8800_vbi_fmt,
1700 .vidioc_reqbufs = vidioc_reqbufs,
1701 .vidioc_querybuf = vidioc_querybuf,
1702 .vidioc_qbuf = vidioc_qbuf,
1703 .vidioc_dqbuf = vidioc_dqbuf,
1704 .vidioc_s_std = vidioc_s_std,
1705 .vidioc_enum_input = vidioc_enum_input,
1706 .vidioc_g_input = vidioc_g_input,
1707 .vidioc_s_input = vidioc_s_input,
1708 .vidioc_queryctrl = vidioc_queryctrl,
1709 .vidioc_g_ctrl = vidioc_g_ctrl,
1710 .vidioc_s_ctrl = vidioc_s_ctrl,
1711 .vidioc_streamon = vidioc_streamon,
1712 .vidioc_streamoff = vidioc_streamoff,
1713 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1714 .vidiocgmbuf = vidiocgmbuf,
1716 .vidioc_g_tuner = vidioc_g_tuner,
1717 .vidioc_s_tuner = vidioc_s_tuner,
1718 .vidioc_g_frequency = vidioc_g_frequency,
1719 .vidioc_s_frequency = vidioc_s_frequency,
1720 #ifdef CONFIG_VIDEO_ADV_DEBUG
1721 .vidioc_g_register = vidioc_g_register,
1722 .vidioc_s_register = vidioc_s_register,
1724 .tvnorms = CX88_NORMS,
1725 .current_norm = V4L2_STD_NTSC_M,
1728 static const struct file_operations radio_fops =
1730 .owner = THIS_MODULE,
1732 .release = video_release,
1733 .ioctl = video_ioctl2,
1734 .compat_ioctl = v4l_compat_ioctl32,
1735 .llseek = no_llseek,
1738 static struct video_device cx8800_radio_template =
1740 .name = "cx8800-radio",
1741 .type = VID_TYPE_TUNER,
1742 .fops = &radio_fops,
1744 .vidioc_querycap = radio_querycap,
1745 .vidioc_g_tuner = radio_g_tuner,
1746 .vidioc_enum_input = radio_enum_input,
1747 .vidioc_g_audio = radio_g_audio,
1748 .vidioc_s_tuner = radio_s_tuner,
1749 .vidioc_s_audio = radio_s_audio,
1750 .vidioc_s_input = radio_s_input,
1751 .vidioc_queryctrl = radio_queryctrl,
1752 .vidioc_g_ctrl = vidioc_g_ctrl,
1753 .vidioc_s_ctrl = vidioc_s_ctrl,
1754 .vidioc_g_frequency = vidioc_g_frequency,
1755 .vidioc_s_frequency = vidioc_s_frequency,
1756 #ifdef CONFIG_VIDEO_ADV_DEBUG
1757 .vidioc_g_register = vidioc_g_register,
1758 .vidioc_s_register = vidioc_s_register,
1762 /* ----------------------------------------------------------- */
1764 static void cx8800_unregister_video(struct cx8800_dev *dev)
1766 if (dev->radio_dev) {
1767 if (-1 != dev->radio_dev->minor)
1768 video_unregister_device(dev->radio_dev);
1770 video_device_release(dev->radio_dev);
1771 dev->radio_dev = NULL;
1774 if (-1 != dev->vbi_dev->minor)
1775 video_unregister_device(dev->vbi_dev);
1777 video_device_release(dev->vbi_dev);
1778 dev->vbi_dev = NULL;
1780 if (dev->video_dev) {
1781 if (-1 != dev->video_dev->minor)
1782 video_unregister_device(dev->video_dev);
1784 video_device_release(dev->video_dev);
1785 dev->video_dev = NULL;
1789 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1790 const struct pci_device_id *pci_id)
1792 struct cx8800_dev *dev;
1793 struct cx88_core *core;
1797 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1803 if (pci_enable_device(pci_dev)) {
1807 core = cx88_core_get(dev->pci);
1814 /* print pci info */
1815 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
1816 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1817 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1818 "latency: %d, mmio: 0x%llx\n", core->name,
1819 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1820 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
1822 pci_set_master(pci_dev);
1823 if (!pci_dma_supported(pci_dev,DMA_32BIT_MASK)) {
1824 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1829 /* Initialize VBI template */
1830 memcpy( &cx8800_vbi_template, &cx8800_video_template,
1831 sizeof(cx8800_vbi_template) );
1832 strcpy(cx8800_vbi_template.name,"cx8800-vbi");
1833 cx8800_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
1835 /* initialize driver struct */
1836 spin_lock_init(&dev->slock);
1837 core->tvnorm = cx8800_video_template.current_norm;
1839 /* init video dma queues */
1840 INIT_LIST_HEAD(&dev->vidq.active);
1841 INIT_LIST_HEAD(&dev->vidq.queued);
1842 dev->vidq.timeout.function = cx8800_vid_timeout;
1843 dev->vidq.timeout.data = (unsigned long)dev;
1844 init_timer(&dev->vidq.timeout);
1845 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1846 MO_VID_DMACNTRL,0x11,0x00);
1848 /* init vbi dma queues */
1849 INIT_LIST_HEAD(&dev->vbiq.active);
1850 INIT_LIST_HEAD(&dev->vbiq.queued);
1851 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1852 dev->vbiq.timeout.data = (unsigned long)dev;
1853 init_timer(&dev->vbiq.timeout);
1854 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1855 MO_VID_DMACNTRL,0x88,0x00);
1858 err = request_irq(pci_dev->irq, cx8800_irq,
1859 IRQF_SHARED | IRQF_DISABLED, core->name, dev);
1861 printk(KERN_ERR "%s/0: can't get IRQ %d\n",
1862 core->name,pci_dev->irq);
1865 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1867 /* load and configure helper modules */
1869 if (core->board.audio_chip == AUDIO_CHIP_WM8775)
1870 request_module("wm8775");
1872 switch (core->boardnr) {
1873 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
1874 case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD:
1875 request_module("rtc-isl1208");
1876 /* break intentionally omitted */
1877 case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
1878 request_module("ir-kbd-i2c");
1881 /* register v4l devices */
1882 dev->video_dev = cx88_vdev_init(core,dev->pci,
1883 &cx8800_video_template,"video");
1884 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1885 video_nr[core->nr]);
1887 printk(KERN_ERR "%s/0: can't register video device\n",
1891 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1892 core->name,dev->video_dev->minor & 0x1f);
1894 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1895 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1898 printk(KERN_ERR "%s/0: can't register vbi device\n",
1902 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1903 core->name,dev->vbi_dev->minor & 0x1f);
1905 if (core->board.radio.type == CX88_RADIO) {
1906 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1907 &cx8800_radio_template,"radio");
1908 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1909 radio_nr[core->nr]);
1911 printk(KERN_ERR "%s/0: can't register radio device\n",
1915 printk(KERN_INFO "%s/0: registered device radio%d\n",
1916 core->name,dev->radio_dev->minor & 0x1f);
1919 /* everything worked */
1920 list_add_tail(&dev->devlist,&cx8800_devlist);
1921 pci_set_drvdata(pci_dev,dev);
1923 /* initial device configuration */
1924 mutex_lock(&core->lock);
1925 cx88_set_tvnorm(core,core->tvnorm);
1926 init_controls(core);
1927 cx88_video_mux(core,0);
1928 mutex_unlock(&core->lock);
1930 /* start tvaudio thread */
1931 if (core->board.tuner_type != TUNER_ABSENT) {
1932 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
1933 if (IS_ERR(core->kthread)) {
1934 err = PTR_ERR(core->kthread);
1935 printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
1942 cx8800_unregister_video(dev);
1943 free_irq(pci_dev->irq, dev);
1945 cx88_core_put(core,dev->pci);
1951 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1953 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1954 struct cx88_core *core = dev->core;
1957 if (core->kthread) {
1958 kthread_stop(core->kthread);
1959 core->kthread = NULL;
1963 cx88_ir_stop(core, core->ir);
1965 cx88_shutdown(core); /* FIXME */
1966 pci_disable_device(pci_dev);
1968 /* unregister stuff */
1970 free_irq(pci_dev->irq, dev);
1971 cx8800_unregister_video(dev);
1972 pci_set_drvdata(pci_dev, NULL);
1975 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1976 list_del(&dev->devlist);
1977 cx88_core_put(core,dev->pci);
1982 static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1984 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1985 struct cx88_core *core = dev->core;
1987 /* stop video+vbi capture */
1988 spin_lock(&dev->slock);
1989 if (!list_empty(&dev->vidq.active)) {
1990 printk("%s/0: suspend video\n", core->name);
1991 stop_video_dma(dev);
1992 del_timer(&dev->vidq.timeout);
1994 if (!list_empty(&dev->vbiq.active)) {
1995 printk("%s/0: suspend vbi\n", core->name);
1996 cx8800_stop_vbi_dma(dev);
1997 del_timer(&dev->vbiq.timeout);
1999 spin_unlock(&dev->slock);
2002 cx88_ir_stop(core, core->ir);
2003 /* FIXME -- shutdown device */
2004 cx88_shutdown(core);
2006 pci_save_state(pci_dev);
2007 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
2008 pci_disable_device(pci_dev);
2009 dev->state.disabled = 1;
2014 static int cx8800_resume(struct pci_dev *pci_dev)
2016 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2017 struct cx88_core *core = dev->core;
2020 if (dev->state.disabled) {
2021 err=pci_enable_device(pci_dev);
2023 printk(KERN_ERR "%s/0: can't enable device\n",
2028 dev->state.disabled = 0;
2030 err= pci_set_power_state(pci_dev, PCI_D0);
2032 printk(KERN_ERR "%s/0: can't set power state\n", core->name);
2033 pci_disable_device(pci_dev);
2034 dev->state.disabled = 1;
2038 pci_restore_state(pci_dev);
2040 /* FIXME: re-initialize hardware */
2043 cx88_ir_start(core, core->ir);
2045 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
2047 /* restart video+vbi capture */
2048 spin_lock(&dev->slock);
2049 if (!list_empty(&dev->vidq.active)) {
2050 printk("%s/0: resume video\n", core->name);
2051 restart_video_queue(dev,&dev->vidq);
2053 if (!list_empty(&dev->vbiq.active)) {
2054 printk("%s/0: resume vbi\n", core->name);
2055 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2057 spin_unlock(&dev->slock);
2063 /* ----------------------------------------------------------- */
2065 static struct pci_device_id cx8800_pci_tbl[] = {
2069 .subvendor = PCI_ANY_ID,
2070 .subdevice = PCI_ANY_ID,
2072 /* --- end of list --- */
2075 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2077 static struct pci_driver cx8800_pci_driver = {
2079 .id_table = cx8800_pci_tbl,
2080 .probe = cx8800_initdev,
2081 .remove = __devexit_p(cx8800_finidev),
2083 .suspend = cx8800_suspend,
2084 .resume = cx8800_resume,
2088 static int cx8800_init(void)
2090 printk(KERN_INFO "cx88/0: cx2388x v4l2 driver version %d.%d.%d loaded\n",
2091 (CX88_VERSION_CODE >> 16) & 0xff,
2092 (CX88_VERSION_CODE >> 8) & 0xff,
2093 CX88_VERSION_CODE & 0xff);
2095 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2096 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2098 return pci_register_driver(&cx8800_pci_driver);
2101 static void cx8800_fini(void)
2103 pci_unregister_driver(&cx8800_pci_driver);
2106 module_init(cx8800_init);
2107 module_exit(cx8800_fini);
2109 /* ----------------------------------------------------------- */
2114 * kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off