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>
44 #ifdef CONFIG_VIDEO_V4L1_COMPAT
45 /* Include V4L1 specific functions. Should be removed soon */
46 #include <linux/videodev.h>
49 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
50 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
51 MODULE_LICENSE("GPL");
53 /* ------------------------------------------------------------------ */
55 static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
56 static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
57 static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
59 module_param_array(video_nr, int, NULL, 0444);
60 module_param_array(vbi_nr, int, NULL, 0444);
61 module_param_array(radio_nr, int, NULL, 0444);
63 MODULE_PARM_DESC(video_nr,"video device numbers");
64 MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
65 MODULE_PARM_DESC(radio_nr,"radio device numbers");
67 static unsigned int video_debug;
68 module_param(video_debug,int,0644);
69 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
71 static unsigned int irq_debug;
72 module_param(irq_debug,int,0644);
73 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
75 static unsigned int vid_limit = 16;
76 module_param(vid_limit,int,0644);
77 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
79 #define dprintk(level,fmt, arg...) if (video_debug >= level) \
80 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
82 /* ------------------------------------------------------------------ */
84 static LIST_HEAD(cx8800_devlist);
86 /* ------------------------------------------------------------------- */
89 static struct cx8800_fmt formats[] = {
91 .name = "8 bpp, gray",
92 .fourcc = V4L2_PIX_FMT_GREY,
93 .cxformat = ColorFormatY8,
95 .flags = FORMAT_FLAGS_PACKED,
97 .name = "15 bpp RGB, le",
98 .fourcc = V4L2_PIX_FMT_RGB555,
99 .cxformat = ColorFormatRGB15,
101 .flags = FORMAT_FLAGS_PACKED,
103 .name = "15 bpp RGB, be",
104 .fourcc = V4L2_PIX_FMT_RGB555X,
105 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
107 .flags = FORMAT_FLAGS_PACKED,
109 .name = "16 bpp RGB, le",
110 .fourcc = V4L2_PIX_FMT_RGB565,
111 .cxformat = ColorFormatRGB16,
113 .flags = FORMAT_FLAGS_PACKED,
115 .name = "16 bpp RGB, be",
116 .fourcc = V4L2_PIX_FMT_RGB565X,
117 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
119 .flags = FORMAT_FLAGS_PACKED,
121 .name = "24 bpp RGB, le",
122 .fourcc = V4L2_PIX_FMT_BGR24,
123 .cxformat = ColorFormatRGB24,
125 .flags = FORMAT_FLAGS_PACKED,
127 .name = "32 bpp RGB, le",
128 .fourcc = V4L2_PIX_FMT_BGR32,
129 .cxformat = ColorFormatRGB32,
131 .flags = FORMAT_FLAGS_PACKED,
133 .name = "32 bpp RGB, be",
134 .fourcc = V4L2_PIX_FMT_RGB32,
135 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
137 .flags = FORMAT_FLAGS_PACKED,
139 .name = "4:2:2, packed, YUYV",
140 .fourcc = V4L2_PIX_FMT_YUYV,
141 .cxformat = ColorFormatYUY2,
143 .flags = FORMAT_FLAGS_PACKED,
145 .name = "4:2:2, packed, UYVY",
146 .fourcc = V4L2_PIX_FMT_UYVY,
147 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
149 .flags = FORMAT_FLAGS_PACKED,
153 static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
157 for (i = 0; i < ARRAY_SIZE(formats); i++)
158 if (formats[i].fourcc == fourcc)
163 /* ------------------------------------------------------------------- */
165 static const struct v4l2_queryctrl no_ctl = {
167 .flags = V4L2_CTRL_FLAG_DISABLED,
170 static struct cx88_ctrl cx8800_ctls[] = {
174 .id = V4L2_CID_BRIGHTNESS,
175 .name = "Brightness",
179 .default_value = 0x7f,
180 .type = V4L2_CTRL_TYPE_INTEGER,
183 .reg = MO_CONTR_BRIGHT,
188 .id = V4L2_CID_CONTRAST,
193 .default_value = 0x3f,
194 .type = V4L2_CTRL_TYPE_INTEGER,
197 .reg = MO_CONTR_BRIGHT,
207 .default_value = 0x7f,
208 .type = V4L2_CTRL_TYPE_INTEGER,
215 /* strictly, this only describes only U saturation.
216 * V saturation is handled specially through code.
219 .id = V4L2_CID_SATURATION,
220 .name = "Saturation",
224 .default_value = 0x7f,
225 .type = V4L2_CTRL_TYPE_INTEGER,
228 .reg = MO_UV_SATURATION,
233 .id = V4L2_CID_CHROMA_AGC,
234 .name = "Chroma AGC",
237 .default_value = 0x1,
238 .type = V4L2_CTRL_TYPE_BOOLEAN,
240 .reg = MO_INPUT_FORMAT,
245 .id = V4L2_CID_COLOR_KILLER,
246 .name = "Color killer",
249 .default_value = 0x1,
250 .type = V4L2_CTRL_TYPE_BOOLEAN,
252 .reg = MO_INPUT_FORMAT,
258 .id = V4L2_CID_AUDIO_MUTE,
263 .type = V4L2_CTRL_TYPE_BOOLEAN,
266 .sreg = SHADOW_AUD_VOL_CTL,
271 .id = V4L2_CID_AUDIO_VOLUME,
276 .default_value = 0x3f,
277 .type = V4L2_CTRL_TYPE_INTEGER,
280 .sreg = SHADOW_AUD_VOL_CTL,
285 .id = V4L2_CID_AUDIO_BALANCE,
290 .default_value = 0x40,
291 .type = V4L2_CTRL_TYPE_INTEGER,
294 .sreg = SHADOW_AUD_BAL_CTL,
299 static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
301 const u32 cx88_user_ctrls[] = {
307 V4L2_CID_AUDIO_VOLUME,
308 V4L2_CID_AUDIO_BALANCE,
311 V4L2_CID_COLOR_KILLER,
314 EXPORT_SYMBOL(cx88_user_ctrls);
316 static const u32 *ctrl_classes[] = {
321 int cx8800_ctrl_query(struct cx88_core *core, struct v4l2_queryctrl *qctrl)
325 if (qctrl->id < V4L2_CID_BASE ||
326 qctrl->id >= V4L2_CID_LASTP1)
328 for (i = 0; i < CX8800_CTLS; i++)
329 if (cx8800_ctls[i].v.id == qctrl->id)
331 if (i == CX8800_CTLS) {
335 *qctrl = cx8800_ctls[i].v;
336 /* Report chroma AGC as inactive when SECAM is selected */
337 if (cx8800_ctls[i].v.id == V4L2_CID_CHROMA_AGC &&
338 core->tvnorm & V4L2_STD_SECAM)
339 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
343 EXPORT_SYMBOL(cx8800_ctrl_query);
345 /* ------------------------------------------------------------------- */
346 /* resource management */
348 static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
350 struct cx88_core *core = dev->core;
351 if (fh->resources & bit)
352 /* have it already allocated */
356 mutex_lock(&core->lock);
357 if (dev->resources & bit) {
358 /* no, someone else uses it */
359 mutex_unlock(&core->lock);
362 /* it's free, grab it */
363 fh->resources |= bit;
364 dev->resources |= bit;
365 dprintk(1,"res: get %d\n",bit);
366 mutex_unlock(&core->lock);
371 int res_check(struct cx8800_fh *fh, unsigned int bit)
373 return (fh->resources & bit);
377 int res_locked(struct cx8800_dev *dev, unsigned int bit)
379 return (dev->resources & bit);
383 void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
385 struct cx88_core *core = dev->core;
386 BUG_ON((fh->resources & bits) != bits);
388 mutex_lock(&core->lock);
389 fh->resources &= ~bits;
390 dev->resources &= ~bits;
391 dprintk(1,"res: put %d\n",bits);
392 mutex_unlock(&core->lock);
395 /* ------------------------------------------------------------------ */
397 int cx88_video_mux(struct cx88_core *core, unsigned int input)
399 /* struct cx88_core *core = dev->core; */
401 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
402 input, INPUT(input).vmux,
403 INPUT(input).gpio0,INPUT(input).gpio1,
404 INPUT(input).gpio2,INPUT(input).gpio3);
406 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
407 cx_write(MO_GP3_IO, INPUT(input).gpio3);
408 cx_write(MO_GP0_IO, INPUT(input).gpio0);
409 cx_write(MO_GP1_IO, INPUT(input).gpio1);
410 cx_write(MO_GP2_IO, INPUT(input).gpio2);
412 switch (INPUT(input).type) {
413 case CX88_VMUX_SVIDEO:
414 cx_set(MO_AFECFG_IO, 0x00000001);
415 cx_set(MO_INPUT_FORMAT, 0x00010010);
416 cx_set(MO_FILTER_EVEN, 0x00002020);
417 cx_set(MO_FILTER_ODD, 0x00002020);
420 cx_clear(MO_AFECFG_IO, 0x00000001);
421 cx_clear(MO_INPUT_FORMAT, 0x00010010);
422 cx_clear(MO_FILTER_EVEN, 0x00002020);
423 cx_clear(MO_FILTER_ODD, 0x00002020);
427 /* if there are audioroutes defined, we have an external
428 ADC to deal with audio */
430 if (INPUT(input).audioroute) {
432 /* cx2388's C-ADC is connected to the tuner only.
433 When used with S-Video, that ADC is busy dealing with
434 chroma, so an external must be used for baseband audio */
436 if (INPUT(input).type != CX88_VMUX_TELEVISION &&
437 INPUT(input).type != CX88_RADIO) {
439 cx_write(AUD_I2SCNTL, 0x1);
440 cx_set(AUD_CTL, EN_I2SIN_ENABLE);
443 cx_write(AUD_I2SCNTL, 0x0);
444 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
447 /* The wm8775 module has the "2" route hardwired into
448 the initialization. Some boards may use different
449 routes for different inputs. HVR-1300 surely does */
450 if (core->board.audio_chip &&
451 core->board.audio_chip == V4L2_IDENT_WM8775) {
452 struct v4l2_routing route;
454 route.input = INPUT(input).audioroute;
455 cx88_call_i2c_clients(core,
456 VIDIOC_INT_S_AUDIO_ROUTING, &route);
464 EXPORT_SYMBOL(cx88_video_mux);
466 /* ------------------------------------------------------------------ */
468 static int start_video_dma(struct cx8800_dev *dev,
469 struct cx88_dmaqueue *q,
470 struct cx88_buffer *buf)
472 struct cx88_core *core = dev->core;
474 /* setup fifo + format */
475 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
476 buf->bpl, buf->risc.dma);
477 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
478 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
481 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
485 cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
487 /* Enables corresponding bits at PCI_INT_STAT:
488 bits 0 to 4: video, audio, transport stream, VIP, Host
490 bits 8 and 9: DMA complete for: SRC, DST
491 bits 10 and 11: BERR signal asserted for RISC: RD, WR
492 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
494 cx_set(MO_VID_INTMSK, 0x0f0011);
497 cx_set(VID_CAPTURE_CONTROL,0x06);
500 cx_set(MO_DEV_CNTRL2, (1<<5));
501 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
507 static int stop_video_dma(struct cx8800_dev *dev)
509 struct cx88_core *core = dev->core;
512 cx_clear(MO_VID_DMACNTRL, 0x11);
514 /* disable capture */
515 cx_clear(VID_CAPTURE_CONTROL,0x06);
518 cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
519 cx_clear(MO_VID_INTMSK, 0x0f0011);
524 static int restart_video_queue(struct cx8800_dev *dev,
525 struct cx88_dmaqueue *q)
527 struct cx88_core *core = dev->core;
528 struct cx88_buffer *buf, *prev;
530 if (!list_empty(&q->active)) {
531 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
532 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
534 start_video_dma(dev, q, buf);
535 list_for_each_entry(buf, &q->active, vb.queue)
536 buf->count = q->count++;
537 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
543 if (list_empty(&q->queued))
545 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
547 list_move_tail(&buf->vb.queue, &q->active);
548 start_video_dma(dev, q, buf);
549 buf->vb.state = VIDEOBUF_ACTIVE;
550 buf->count = q->count++;
551 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
552 dprintk(2,"[%p/%d] restart_queue - first active\n",
555 } else if (prev->vb.width == buf->vb.width &&
556 prev->vb.height == buf->vb.height &&
557 prev->fmt == buf->fmt) {
558 list_move_tail(&buf->vb.queue, &q->active);
559 buf->vb.state = VIDEOBUF_ACTIVE;
560 buf->count = q->count++;
561 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
562 dprintk(2,"[%p/%d] restart_queue - move to active\n",
571 /* ------------------------------------------------------------------ */
574 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
576 struct cx8800_fh *fh = q->priv_data;
578 *size = fh->fmt->depth*fh->width*fh->height >> 3;
581 while (*size * *count > vid_limit * 1024 * 1024)
587 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
588 enum v4l2_field field)
590 struct cx8800_fh *fh = q->priv_data;
591 struct cx8800_dev *dev = fh->dev;
592 struct cx88_core *core = dev->core;
593 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
594 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
595 int rc, init_buffer = 0;
597 BUG_ON(NULL == fh->fmt);
598 if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
599 fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
601 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
602 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
605 if (buf->fmt != fh->fmt ||
606 buf->vb.width != fh->width ||
607 buf->vb.height != fh->height ||
608 buf->vb.field != field) {
610 buf->vb.width = fh->width;
611 buf->vb.height = fh->height;
612 buf->vb.field = field;
616 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
618 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
623 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
624 switch (buf->vb.field) {
626 cx88_risc_buffer(dev->pci, &buf->risc,
627 dma->sglist, 0, UNSET,
628 buf->bpl, 0, buf->vb.height);
630 case V4L2_FIELD_BOTTOM:
631 cx88_risc_buffer(dev->pci, &buf->risc,
632 dma->sglist, UNSET, 0,
633 buf->bpl, 0, buf->vb.height);
635 case V4L2_FIELD_INTERLACED:
636 cx88_risc_buffer(dev->pci, &buf->risc,
637 dma->sglist, 0, buf->bpl,
639 buf->vb.height >> 1);
641 case V4L2_FIELD_SEQ_TB:
642 cx88_risc_buffer(dev->pci, &buf->risc,
644 0, buf->bpl * (buf->vb.height >> 1),
646 buf->vb.height >> 1);
648 case V4L2_FIELD_SEQ_BT:
649 cx88_risc_buffer(dev->pci, &buf->risc,
651 buf->bpl * (buf->vb.height >> 1), 0,
653 buf->vb.height >> 1);
659 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
661 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
662 (unsigned long)buf->risc.dma);
664 buf->vb.state = VIDEOBUF_PREPARED;
668 cx88_free_buffer(q,buf);
673 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
675 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
676 struct cx88_buffer *prev;
677 struct cx8800_fh *fh = vq->priv_data;
678 struct cx8800_dev *dev = fh->dev;
679 struct cx88_core *core = dev->core;
680 struct cx88_dmaqueue *q = &dev->vidq;
682 /* add jump to stopper */
683 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
684 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
686 if (!list_empty(&q->queued)) {
687 list_add_tail(&buf->vb.queue,&q->queued);
688 buf->vb.state = VIDEOBUF_QUEUED;
689 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
692 } else if (list_empty(&q->active)) {
693 list_add_tail(&buf->vb.queue,&q->active);
694 start_video_dma(dev, q, buf);
695 buf->vb.state = VIDEOBUF_ACTIVE;
696 buf->count = q->count++;
697 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
698 dprintk(2,"[%p/%d] buffer_queue - first active\n",
702 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
703 if (prev->vb.width == buf->vb.width &&
704 prev->vb.height == buf->vb.height &&
705 prev->fmt == buf->fmt) {
706 list_add_tail(&buf->vb.queue,&q->active);
707 buf->vb.state = VIDEOBUF_ACTIVE;
708 buf->count = q->count++;
709 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
710 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
714 list_add_tail(&buf->vb.queue,&q->queued);
715 buf->vb.state = VIDEOBUF_QUEUED;
716 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
722 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
724 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
726 cx88_free_buffer(q,buf);
729 static struct videobuf_queue_ops cx8800_video_qops = {
730 .buf_setup = buffer_setup,
731 .buf_prepare = buffer_prepare,
732 .buf_queue = buffer_queue,
733 .buf_release = buffer_release,
736 /* ------------------------------------------------------------------ */
739 /* ------------------------------------------------------------------ */
741 static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
744 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
746 case V4L2_BUF_TYPE_VBI_CAPTURE:
754 static int get_ressource(struct cx8800_fh *fh)
757 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
758 return RESOURCE_VIDEO;
759 case V4L2_BUF_TYPE_VBI_CAPTURE:
767 static int video_open(struct inode *inode, struct file *file)
769 int minor = iminor(inode);
770 struct cx8800_dev *h,*dev = NULL;
771 struct cx88_core *core;
772 struct cx8800_fh *fh;
773 enum v4l2_buf_type type = 0;
776 list_for_each_entry(h, &cx8800_devlist, devlist) {
777 if (h->video_dev->minor == minor) {
779 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
781 if (h->vbi_dev->minor == minor) {
783 type = V4L2_BUF_TYPE_VBI_CAPTURE;
786 h->radio_dev->minor == minor) {
796 dprintk(1,"open minor=%d radio=%d type=%s\n",
797 minor,radio,v4l2_type_names[type]);
799 /* allocate + initialize per filehandle data */
800 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
803 file->private_data = fh;
809 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
811 videobuf_queue_sg_init(&fh->vidq, &cx8800_video_qops,
812 &dev->pci->dev, &dev->slock,
813 V4L2_BUF_TYPE_VIDEO_CAPTURE,
814 V4L2_FIELD_INTERLACED,
815 sizeof(struct cx88_buffer),
817 videobuf_queue_sg_init(&fh->vbiq, &cx8800_vbi_qops,
818 &dev->pci->dev, &dev->slock,
819 V4L2_BUF_TYPE_VBI_CAPTURE,
821 sizeof(struct cx88_buffer),
825 dprintk(1,"video_open: setting radio device\n");
826 cx_write(MO_GP3_IO, core->board.radio.gpio3);
827 cx_write(MO_GP0_IO, core->board.radio.gpio0);
828 cx_write(MO_GP1_IO, core->board.radio.gpio1);
829 cx_write(MO_GP2_IO, core->board.radio.gpio2);
830 core->tvaudio = WW_FM;
831 cx88_set_tvaudio(core);
832 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
833 cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
840 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
842 struct cx8800_fh *fh = file->private_data;
845 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
846 if (res_locked(fh->dev,RESOURCE_VIDEO))
848 return videobuf_read_one(&fh->vidq, data, count, ppos,
849 file->f_flags & O_NONBLOCK);
850 case V4L2_BUF_TYPE_VBI_CAPTURE:
851 if (!res_get(fh->dev,fh,RESOURCE_VBI))
853 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
854 file->f_flags & O_NONBLOCK);
862 video_poll(struct file *file, struct poll_table_struct *wait)
864 struct cx8800_fh *fh = file->private_data;
865 struct cx88_buffer *buf;
867 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
868 if (!res_get(fh->dev,fh,RESOURCE_VBI))
870 return videobuf_poll_stream(file, &fh->vbiq, wait);
873 if (res_check(fh,RESOURCE_VIDEO)) {
874 /* streaming capture */
875 if (list_empty(&fh->vidq.stream))
877 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
880 buf = (struct cx88_buffer*)fh->vidq.read_buf;
884 poll_wait(file, &buf->vb.done, wait);
885 if (buf->vb.state == VIDEOBUF_DONE ||
886 buf->vb.state == VIDEOBUF_ERROR)
887 return POLLIN|POLLRDNORM;
891 static int video_release(struct inode *inode, struct file *file)
893 struct cx8800_fh *fh = file->private_data;
894 struct cx8800_dev *dev = fh->dev;
896 /* turn off overlay */
897 if (res_check(fh, RESOURCE_OVERLAY)) {
899 res_free(dev,fh,RESOURCE_OVERLAY);
902 /* stop video capture */
903 if (res_check(fh, RESOURCE_VIDEO)) {
904 videobuf_queue_cancel(&fh->vidq);
905 res_free(dev,fh,RESOURCE_VIDEO);
907 if (fh->vidq.read_buf) {
908 buffer_release(&fh->vidq,fh->vidq.read_buf);
909 kfree(fh->vidq.read_buf);
912 /* stop vbi capture */
913 if (res_check(fh, RESOURCE_VBI)) {
914 videobuf_stop(&fh->vbiq);
915 res_free(dev,fh,RESOURCE_VBI);
918 videobuf_mmap_free(&fh->vidq);
919 videobuf_mmap_free(&fh->vbiq);
920 file->private_data = NULL;
923 cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
929 video_mmap(struct file *file, struct vm_area_struct * vma)
931 struct cx8800_fh *fh = file->private_data;
933 return videobuf_mmap_mapper(get_queue(fh), vma);
936 /* ------------------------------------------------------------------ */
937 /* VIDEO CTRL IOCTLS */
939 int cx88_get_control (struct cx88_core *core, struct v4l2_control *ctl)
941 struct cx88_ctrl *c = NULL;
945 for (i = 0; i < CX8800_CTLS; i++)
946 if (cx8800_ctls[i].v.id == ctl->id)
948 if (unlikely(NULL == c))
951 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
953 case V4L2_CID_AUDIO_BALANCE:
954 ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
955 : (0x7f - (value & 0x7f));
957 case V4L2_CID_AUDIO_VOLUME:
958 ctl->value = 0x3f - (value & 0x3f);
961 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
964 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
965 ctl->id, c->v.name, ctl->value, c->reg,
966 value,c->mask, c->sreg ? " [shadowed]" : "");
969 EXPORT_SYMBOL(cx88_get_control);
971 int cx88_set_control(struct cx88_core *core, struct v4l2_control *ctl)
973 struct cx88_ctrl *c = NULL;
977 for (i = 0; i < CX8800_CTLS; i++) {
978 if (cx8800_ctls[i].v.id == ctl->id) {
982 if (unlikely(NULL == c))
985 if (ctl->value < c->v.minimum)
986 ctl->value = c->v.minimum;
987 if (ctl->value > c->v.maximum)
988 ctl->value = c->v.maximum;
991 case V4L2_CID_AUDIO_BALANCE:
992 value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
994 case V4L2_CID_AUDIO_VOLUME:
995 value = 0x3f - (ctl->value & 0x3f);
997 case V4L2_CID_SATURATION:
998 /* special v_sat handling */
1000 value = ((ctl->value - c->off) << c->shift) & c->mask;
1002 if (core->tvnorm & V4L2_STD_SECAM) {
1003 /* For SECAM, both U and V sat should be equal */
1004 value=value<<8|value;
1006 /* Keeps U Saturation proportional to V Sat */
1007 value=(value*0x5a)/0x7f<<8|value;
1011 case V4L2_CID_CHROMA_AGC:
1012 /* Do not allow chroma AGC to be enabled for SECAM */
1013 value = ((ctl->value - c->off) << c->shift) & c->mask;
1014 if (core->tvnorm & V4L2_STD_SECAM && value)
1018 value = ((ctl->value - c->off) << c->shift) & c->mask;
1021 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
1022 ctl->id, c->v.name, ctl->value, c->reg, value,
1023 mask, c->sreg ? " [shadowed]" : "");
1025 cx_sandor(c->sreg, c->reg, mask, value);
1027 cx_andor(c->reg, mask, value);
1031 EXPORT_SYMBOL(cx88_set_control);
1033 static void init_controls(struct cx88_core *core)
1035 struct v4l2_control ctrl;
1038 for (i = 0; i < CX8800_CTLS; i++) {
1039 ctrl.id=cx8800_ctls[i].v.id;
1040 ctrl.value=cx8800_ctls[i].v.default_value;
1042 cx88_set_control(core, &ctrl);
1046 /* ------------------------------------------------------------------ */
1049 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1050 struct v4l2_format *f)
1052 struct cx8800_fh *fh = priv;
1054 f->fmt.pix.width = fh->width;
1055 f->fmt.pix.height = fh->height;
1056 f->fmt.pix.field = fh->vidq.field;
1057 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1058 f->fmt.pix.bytesperline =
1059 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1060 f->fmt.pix.sizeimage =
1061 f->fmt.pix.height * f->fmt.pix.bytesperline;
1065 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1066 struct v4l2_format *f)
1068 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1069 struct cx8800_fmt *fmt;
1070 enum v4l2_field field;
1071 unsigned int maxw, maxh;
1073 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1077 field = f->fmt.pix.field;
1078 maxw = norm_maxw(core->tvnorm);
1079 maxh = norm_maxh(core->tvnorm);
1081 if (V4L2_FIELD_ANY == field) {
1082 field = (f->fmt.pix.height > maxh/2)
1083 ? V4L2_FIELD_INTERLACED
1084 : V4L2_FIELD_BOTTOM;
1088 case V4L2_FIELD_TOP:
1089 case V4L2_FIELD_BOTTOM:
1092 case V4L2_FIELD_INTERLACED:
1098 f->fmt.pix.field = field;
1099 if (f->fmt.pix.height < 32)
1100 f->fmt.pix.height = 32;
1101 if (f->fmt.pix.height > maxh)
1102 f->fmt.pix.height = maxh;
1103 if (f->fmt.pix.width < 48)
1104 f->fmt.pix.width = 48;
1105 if (f->fmt.pix.width > maxw)
1106 f->fmt.pix.width = maxw;
1107 f->fmt.pix.width &= ~0x03;
1108 f->fmt.pix.bytesperline =
1109 (f->fmt.pix.width * fmt->depth) >> 3;
1110 f->fmt.pix.sizeimage =
1111 f->fmt.pix.height * f->fmt.pix.bytesperline;
1116 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1117 struct v4l2_format *f)
1119 struct cx8800_fh *fh = priv;
1120 int err = vidioc_try_fmt_vid_cap (file,priv,f);
1124 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1125 fh->width = f->fmt.pix.width;
1126 fh->height = f->fmt.pix.height;
1127 fh->vidq.field = f->fmt.pix.field;
1131 static int vidioc_querycap (struct file *file, void *priv,
1132 struct v4l2_capability *cap)
1134 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
1135 struct cx88_core *core = dev->core;
1137 strcpy(cap->driver, "cx8800");
1138 strlcpy(cap->card, core->board.name, sizeof(cap->card));
1139 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1140 cap->version = CX88_VERSION_CODE;
1142 V4L2_CAP_VIDEO_CAPTURE |
1143 V4L2_CAP_READWRITE |
1144 V4L2_CAP_STREAMING |
1145 V4L2_CAP_VBI_CAPTURE;
1146 if (UNSET != core->board.tuner_type)
1147 cap->capabilities |= V4L2_CAP_TUNER;
1151 static int vidioc_enum_fmt_vid_cap (struct file *file, void *priv,
1152 struct v4l2_fmtdesc *f)
1154 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1157 strlcpy(f->description,formats[f->index].name,sizeof(f->description));
1158 f->pixelformat = formats[f->index].fourcc;
1163 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1164 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
1166 struct cx8800_fh *fh = priv;
1168 return videobuf_cgmbuf (get_queue(fh), mbuf, 8);
1172 static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
1174 struct cx8800_fh *fh = priv;
1175 return (videobuf_reqbufs(get_queue(fh), p));
1178 static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
1180 struct cx8800_fh *fh = priv;
1181 return (videobuf_querybuf(get_queue(fh), p));
1184 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1186 struct cx8800_fh *fh = priv;
1187 return (videobuf_qbuf(get_queue(fh), p));
1190 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1192 struct cx8800_fh *fh = priv;
1193 return (videobuf_dqbuf(get_queue(fh), p,
1194 file->f_flags & O_NONBLOCK));
1197 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1199 struct cx8800_fh *fh = priv;
1200 struct cx8800_dev *dev = fh->dev;
1202 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1204 if (unlikely(i != fh->type))
1207 if (unlikely(!res_get(dev,fh,get_ressource(fh))))
1209 return videobuf_streamon(get_queue(fh));
1212 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1214 struct cx8800_fh *fh = priv;
1215 struct cx8800_dev *dev = fh->dev;
1218 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1223 res = get_ressource(fh);
1224 err = videobuf_streamoff(get_queue(fh));
1227 res_free(dev,fh,res);
1231 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
1233 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1235 mutex_lock(&core->lock);
1236 cx88_set_tvnorm(core,*tvnorms);
1237 mutex_unlock(&core->lock);
1242 /* only one input in this sample driver */
1243 int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
1245 static const char *iname[] = {
1246 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1247 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1248 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1249 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1250 [ CX88_VMUX_SVIDEO ] = "S-Video",
1251 [ CX88_VMUX_TELEVISION ] = "Television",
1252 [ CX88_VMUX_CABLE ] = "Cable TV",
1253 [ CX88_VMUX_DVB ] = "DVB",
1254 [ CX88_VMUX_DEBUG ] = "for debug only",
1261 if (0 == INPUT(n).type)
1263 memset(i,0,sizeof(*i));
1265 i->type = V4L2_INPUT_TYPE_CAMERA;
1266 strcpy(i->name,iname[INPUT(n).type]);
1267 if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
1268 (CX88_VMUX_CABLE == INPUT(n).type))
1269 i->type = V4L2_INPUT_TYPE_TUNER;
1270 i->std = CX88_NORMS;
1273 EXPORT_SYMBOL(cx88_enum_input);
1275 static int vidioc_enum_input (struct file *file, void *priv,
1276 struct v4l2_input *i)
1278 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1279 return cx88_enum_input (core,i);
1282 static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1284 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1290 static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1292 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1297 mutex_lock(&core->lock);
1298 cx88_newstation(core);
1299 cx88_video_mux(core,i);
1300 mutex_unlock(&core->lock);
1306 static int vidioc_queryctrl (struct file *file, void *priv,
1307 struct v4l2_queryctrl *qctrl)
1309 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1311 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1312 if (unlikely(qctrl->id == 0))
1314 return cx8800_ctrl_query(core, qctrl);
1317 static int vidioc_g_ctrl (struct file *file, void *priv,
1318 struct v4l2_control *ctl)
1320 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1322 cx88_get_control(core,ctl);
1325 static int vidioc_s_ctrl (struct file *file, void *priv,
1326 struct v4l2_control *ctl)
1328 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1330 cx88_set_control(core,ctl);
1333 static int vidioc_g_tuner (struct file *file, void *priv,
1334 struct v4l2_tuner *t)
1336 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1339 if (unlikely(UNSET == core->board.tuner_type))
1344 strcpy(t->name, "Television");
1345 t->type = V4L2_TUNER_ANALOG_TV;
1346 t->capability = V4L2_TUNER_CAP_NORM;
1347 t->rangehigh = 0xffffffffUL;
1349 cx88_get_stereo(core ,t);
1350 reg = cx_read(MO_DEVICE_STATUS);
1351 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1355 static int vidioc_s_tuner (struct file *file, void *priv,
1356 struct v4l2_tuner *t)
1358 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1360 if (UNSET == core->board.tuner_type)
1365 cx88_set_stereo(core, t->audmode, 1);
1369 static int vidioc_g_frequency (struct file *file, void *priv,
1370 struct v4l2_frequency *f)
1372 struct cx8800_fh *fh = priv;
1373 struct cx88_core *core = fh->dev->core;
1375 if (unlikely(UNSET == core->board.tuner_type))
1378 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1379 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1380 f->frequency = core->freq;
1382 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1387 int cx88_set_freq (struct cx88_core *core,
1388 struct v4l2_frequency *f)
1390 if (unlikely(UNSET == core->board.tuner_type))
1392 if (unlikely(f->tuner != 0))
1395 mutex_lock(&core->lock);
1396 core->freq = f->frequency;
1397 cx88_newstation(core);
1398 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1400 /* When changing channels it is required to reset TVAUDIO */
1402 cx88_set_tvaudio(core);
1404 mutex_unlock(&core->lock);
1408 EXPORT_SYMBOL(cx88_set_freq);
1410 static int vidioc_s_frequency (struct file *file, void *priv,
1411 struct v4l2_frequency *f)
1413 struct cx8800_fh *fh = priv;
1414 struct cx88_core *core = fh->dev->core;
1416 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1418 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1422 cx88_set_freq (core,f);
1425 #ifdef CONFIG_VIDEO_ADV_DEBUG
1426 static int vidioc_g_register (struct file *file, void *fh,
1427 struct v4l2_register *reg)
1429 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1431 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1433 /* cx2388x has a 24-bit register space */
1434 reg->val = cx_read(reg->reg&0xffffff);
1438 static int vidioc_s_register (struct file *file, void *fh,
1439 struct v4l2_register *reg)
1441 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1443 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1445 cx_write(reg->reg&0xffffff, reg->val);
1450 /* ----------------------------------------------------------- */
1451 /* RADIO ESPECIFIC IOCTLS */
1452 /* ----------------------------------------------------------- */
1454 static int radio_querycap (struct file *file, void *priv,
1455 struct v4l2_capability *cap)
1457 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
1458 struct cx88_core *core = dev->core;
1460 strcpy(cap->driver, "cx8800");
1461 strlcpy(cap->card, core->board.name, sizeof(cap->card));
1462 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1463 cap->version = CX88_VERSION_CODE;
1464 cap->capabilities = V4L2_CAP_TUNER;
1468 static int radio_g_tuner (struct file *file, void *priv,
1469 struct v4l2_tuner *t)
1471 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1473 if (unlikely(t->index > 0))
1476 strcpy(t->name, "Radio");
1477 t->type = V4L2_TUNER_RADIO;
1479 cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
1483 static int radio_enum_input (struct file *file, void *priv,
1484 struct v4l2_input *i)
1488 strcpy(i->name,"Radio");
1489 i->type = V4L2_INPUT_TYPE_TUNER;
1494 static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
1496 if (unlikely(a->index))
1499 memset(a,0,sizeof(*a));
1500 strcpy(a->name,"Radio");
1504 /* FIXME: Should add a standard for radio */
1506 static int radio_s_tuner (struct file *file, void *priv,
1507 struct v4l2_tuner *t)
1509 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1514 cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1519 static int radio_s_audio (struct file *file, void *fh,
1520 struct v4l2_audio *a)
1525 static int radio_s_input (struct file *file, void *fh, unsigned int i)
1530 static int radio_queryctrl (struct file *file, void *priv,
1531 struct v4l2_queryctrl *c)
1535 if (c->id < V4L2_CID_BASE ||
1536 c->id >= V4L2_CID_LASTP1)
1538 if (c->id == V4L2_CID_AUDIO_MUTE) {
1539 for (i = 0; i < CX8800_CTLS; i++)
1540 if (cx8800_ctls[i].v.id == c->id)
1542 *c = cx8800_ctls[i].v;
1548 /* ----------------------------------------------------------- */
1550 static void cx8800_vid_timeout(unsigned long data)
1552 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1553 struct cx88_core *core = dev->core;
1554 struct cx88_dmaqueue *q = &dev->vidq;
1555 struct cx88_buffer *buf;
1556 unsigned long flags;
1558 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1560 cx_clear(MO_VID_DMACNTRL, 0x11);
1561 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1563 spin_lock_irqsave(&dev->slock,flags);
1564 while (!list_empty(&q->active)) {
1565 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1566 list_del(&buf->vb.queue);
1567 buf->vb.state = VIDEOBUF_ERROR;
1568 wake_up(&buf->vb.done);
1569 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1570 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1572 restart_video_queue(dev,q);
1573 spin_unlock_irqrestore(&dev->slock,flags);
1576 static char *cx88_vid_irqs[32] = {
1577 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1578 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1579 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1580 "y_sync", "u_sync", "v_sync", "vbi_sync",
1581 "opc_err", "par_err", "rip_err", "pci_abort",
1584 static void cx8800_vid_irq(struct cx8800_dev *dev)
1586 struct cx88_core *core = dev->core;
1587 u32 status, mask, count;
1589 status = cx_read(MO_VID_INTSTAT);
1590 mask = cx_read(MO_VID_INTMSK);
1591 if (0 == (status & mask))
1593 cx_write(MO_VID_INTSTAT, status);
1594 if (irq_debug || (status & mask & ~0xff))
1595 cx88_print_irqbits(core->name, "irq vid",
1596 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1599 /* risc op code error */
1600 if (status & (1 << 16)) {
1601 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1602 cx_clear(MO_VID_DMACNTRL, 0x11);
1603 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1604 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1608 if (status & 0x01) {
1609 spin_lock(&dev->slock);
1610 count = cx_read(MO_VIDY_GPCNT);
1611 cx88_wakeup(core, &dev->vidq, count);
1612 spin_unlock(&dev->slock);
1616 if (status & 0x08) {
1617 spin_lock(&dev->slock);
1618 count = cx_read(MO_VBI_GPCNT);
1619 cx88_wakeup(core, &dev->vbiq, count);
1620 spin_unlock(&dev->slock);
1624 if (status & 0x10) {
1625 dprintk(2,"stopper video\n");
1626 spin_lock(&dev->slock);
1627 restart_video_queue(dev,&dev->vidq);
1628 spin_unlock(&dev->slock);
1632 if (status & 0x80) {
1633 dprintk(2,"stopper vbi\n");
1634 spin_lock(&dev->slock);
1635 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1636 spin_unlock(&dev->slock);
1640 static irqreturn_t cx8800_irq(int irq, void *dev_id)
1642 struct cx8800_dev *dev = dev_id;
1643 struct cx88_core *core = dev->core;
1645 int loop, handled = 0;
1647 for (loop = 0; loop < 10; loop++) {
1648 status = cx_read(MO_PCI_INTSTAT) &
1649 (core->pci_irqmask | PCI_INT_VIDINT);
1652 cx_write(MO_PCI_INTSTAT, status);
1655 if (status & core->pci_irqmask)
1656 cx88_core_irq(core,status);
1657 if (status & PCI_INT_VIDINT)
1658 cx8800_vid_irq(dev);
1661 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1663 cx_write(MO_PCI_INTMSK,0);
1667 return IRQ_RETVAL(handled);
1670 /* ----------------------------------------------------------- */
1671 /* exported stuff */
1673 static const struct file_operations video_fops =
1675 .owner = THIS_MODULE,
1677 .release = video_release,
1681 .ioctl = video_ioctl2,
1682 .compat_ioctl = v4l_compat_ioctl32,
1683 .llseek = no_llseek,
1686 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1687 .vidioc_querycap = vidioc_querycap,
1688 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1689 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1690 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1691 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1692 .vidioc_g_fmt_vbi_cap = cx8800_vbi_fmt,
1693 .vidioc_try_fmt_vbi_cap = cx8800_vbi_fmt,
1694 .vidioc_s_fmt_vbi_cap = cx8800_vbi_fmt,
1695 .vidioc_reqbufs = vidioc_reqbufs,
1696 .vidioc_querybuf = vidioc_querybuf,
1697 .vidioc_qbuf = vidioc_qbuf,
1698 .vidioc_dqbuf = vidioc_dqbuf,
1699 .vidioc_s_std = vidioc_s_std,
1700 .vidioc_enum_input = vidioc_enum_input,
1701 .vidioc_g_input = vidioc_g_input,
1702 .vidioc_s_input = vidioc_s_input,
1703 .vidioc_queryctrl = vidioc_queryctrl,
1704 .vidioc_g_ctrl = vidioc_g_ctrl,
1705 .vidioc_s_ctrl = vidioc_s_ctrl,
1706 .vidioc_streamon = vidioc_streamon,
1707 .vidioc_streamoff = vidioc_streamoff,
1708 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1709 .vidiocgmbuf = vidiocgmbuf,
1711 .vidioc_g_tuner = vidioc_g_tuner,
1712 .vidioc_s_tuner = vidioc_s_tuner,
1713 .vidioc_g_frequency = vidioc_g_frequency,
1714 .vidioc_s_frequency = vidioc_s_frequency,
1715 #ifdef CONFIG_VIDEO_ADV_DEBUG
1716 .vidioc_g_register = vidioc_g_register,
1717 .vidioc_s_register = vidioc_s_register,
1721 static struct video_device cx8800_vbi_template;
1723 static struct video_device cx8800_video_template = {
1724 .name = "cx8800-video",
1725 .fops = &video_fops,
1727 .ioctl_ops = &video_ioctl_ops,
1728 .tvnorms = CX88_NORMS,
1729 .current_norm = V4L2_STD_NTSC_M,
1732 static const struct file_operations radio_fops =
1734 .owner = THIS_MODULE,
1736 .release = video_release,
1737 .ioctl = video_ioctl2,
1738 .compat_ioctl = v4l_compat_ioctl32,
1739 .llseek = no_llseek,
1742 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1743 .vidioc_querycap = radio_querycap,
1744 .vidioc_g_tuner = radio_g_tuner,
1745 .vidioc_enum_input = radio_enum_input,
1746 .vidioc_g_audio = radio_g_audio,
1747 .vidioc_s_tuner = radio_s_tuner,
1748 .vidioc_s_audio = radio_s_audio,
1749 .vidioc_s_input = radio_s_input,
1750 .vidioc_queryctrl = radio_queryctrl,
1751 .vidioc_g_ctrl = vidioc_g_ctrl,
1752 .vidioc_s_ctrl = vidioc_s_ctrl,
1753 .vidioc_g_frequency = vidioc_g_frequency,
1754 .vidioc_s_frequency = vidioc_s_frequency,
1755 #ifdef CONFIG_VIDEO_ADV_DEBUG
1756 .vidioc_g_register = vidioc_g_register,
1757 .vidioc_s_register = vidioc_s_register,
1761 static struct video_device cx8800_radio_template = {
1762 .name = "cx8800-radio",
1763 .fops = &radio_fops,
1765 .ioctl_ops = &radio_ioctl_ops,
1768 /* ----------------------------------------------------------- */
1770 static void cx8800_unregister_video(struct cx8800_dev *dev)
1772 if (dev->radio_dev) {
1773 if (-1 != dev->radio_dev->minor)
1774 video_unregister_device(dev->radio_dev);
1776 video_device_release(dev->radio_dev);
1777 dev->radio_dev = NULL;
1780 if (-1 != dev->vbi_dev->minor)
1781 video_unregister_device(dev->vbi_dev);
1783 video_device_release(dev->vbi_dev);
1784 dev->vbi_dev = NULL;
1786 if (dev->video_dev) {
1787 if (-1 != dev->video_dev->minor)
1788 video_unregister_device(dev->video_dev);
1790 video_device_release(dev->video_dev);
1791 dev->video_dev = NULL;
1795 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1796 const struct pci_device_id *pci_id)
1798 struct cx8800_dev *dev;
1799 struct cx88_core *core;
1803 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1809 if (pci_enable_device(pci_dev)) {
1813 core = cx88_core_get(dev->pci);
1820 /* print pci info */
1821 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
1822 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1823 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1824 "latency: %d, mmio: 0x%llx\n", core->name,
1825 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1826 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
1828 pci_set_master(pci_dev);
1829 if (!pci_dma_supported(pci_dev,DMA_32BIT_MASK)) {
1830 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1835 /* Initialize VBI template */
1836 memcpy( &cx8800_vbi_template, &cx8800_video_template,
1837 sizeof(cx8800_vbi_template) );
1838 strcpy(cx8800_vbi_template.name,"cx8800-vbi");
1840 /* initialize driver struct */
1841 spin_lock_init(&dev->slock);
1842 core->tvnorm = cx8800_video_template.current_norm;
1844 /* init video dma queues */
1845 INIT_LIST_HEAD(&dev->vidq.active);
1846 INIT_LIST_HEAD(&dev->vidq.queued);
1847 dev->vidq.timeout.function = cx8800_vid_timeout;
1848 dev->vidq.timeout.data = (unsigned long)dev;
1849 init_timer(&dev->vidq.timeout);
1850 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1851 MO_VID_DMACNTRL,0x11,0x00);
1853 /* init vbi dma queues */
1854 INIT_LIST_HEAD(&dev->vbiq.active);
1855 INIT_LIST_HEAD(&dev->vbiq.queued);
1856 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1857 dev->vbiq.timeout.data = (unsigned long)dev;
1858 init_timer(&dev->vbiq.timeout);
1859 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1860 MO_VID_DMACNTRL,0x88,0x00);
1863 err = request_irq(pci_dev->irq, cx8800_irq,
1864 IRQF_SHARED | IRQF_DISABLED, core->name, dev);
1866 printk(KERN_ERR "%s/0: can't get IRQ %d\n",
1867 core->name,pci_dev->irq);
1870 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1872 /* load and configure helper modules */
1874 if (core->board.audio_chip == V4L2_IDENT_WM8775)
1875 request_module("wm8775");
1877 switch (core->boardnr) {
1878 case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
1879 case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD:
1880 request_module("rtc-isl1208");
1881 /* break intentionally omitted */
1882 case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
1883 request_module("ir-kbd-i2c");
1886 /* register v4l devices */
1887 dev->video_dev = cx88_vdev_init(core,dev->pci,
1888 &cx8800_video_template,"video");
1889 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1890 video_nr[core->nr]);
1892 printk(KERN_ERR "%s/0: can't register video device\n",
1896 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1897 core->name,dev->video_dev->minor & 0x1f);
1899 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1900 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1903 printk(KERN_ERR "%s/0: can't register vbi device\n",
1907 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1908 core->name,dev->vbi_dev->minor & 0x1f);
1910 if (core->board.radio.type == CX88_RADIO) {
1911 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1912 &cx8800_radio_template,"radio");
1913 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1914 radio_nr[core->nr]);
1916 printk(KERN_ERR "%s/0: can't register radio device\n",
1920 printk(KERN_INFO "%s/0: registered device radio%d\n",
1921 core->name,dev->radio_dev->minor & 0x1f);
1924 /* everything worked */
1925 list_add_tail(&dev->devlist,&cx8800_devlist);
1926 pci_set_drvdata(pci_dev,dev);
1928 /* initial device configuration */
1929 mutex_lock(&core->lock);
1930 cx88_set_tvnorm(core,core->tvnorm);
1931 init_controls(core);
1932 cx88_video_mux(core,0);
1933 mutex_unlock(&core->lock);
1935 /* start tvaudio thread */
1936 if (core->board.tuner_type != TUNER_ABSENT) {
1937 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
1938 if (IS_ERR(core->kthread)) {
1939 err = PTR_ERR(core->kthread);
1940 printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
1947 cx8800_unregister_video(dev);
1948 free_irq(pci_dev->irq, dev);
1950 cx88_core_put(core,dev->pci);
1956 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1958 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1959 struct cx88_core *core = dev->core;
1962 if (core->kthread) {
1963 kthread_stop(core->kthread);
1964 core->kthread = NULL;
1968 cx88_ir_stop(core, core->ir);
1970 cx88_shutdown(core); /* FIXME */
1971 pci_disable_device(pci_dev);
1973 /* unregister stuff */
1975 free_irq(pci_dev->irq, dev);
1976 cx8800_unregister_video(dev);
1977 pci_set_drvdata(pci_dev, NULL);
1980 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1981 list_del(&dev->devlist);
1982 cx88_core_put(core,dev->pci);
1987 static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1989 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1990 struct cx88_core *core = dev->core;
1992 /* stop video+vbi capture */
1993 spin_lock(&dev->slock);
1994 if (!list_empty(&dev->vidq.active)) {
1995 printk("%s/0: suspend video\n", core->name);
1996 stop_video_dma(dev);
1997 del_timer(&dev->vidq.timeout);
1999 if (!list_empty(&dev->vbiq.active)) {
2000 printk("%s/0: suspend vbi\n", core->name);
2001 cx8800_stop_vbi_dma(dev);
2002 del_timer(&dev->vbiq.timeout);
2004 spin_unlock(&dev->slock);
2007 cx88_ir_stop(core, core->ir);
2008 /* FIXME -- shutdown device */
2009 cx88_shutdown(core);
2011 pci_save_state(pci_dev);
2012 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
2013 pci_disable_device(pci_dev);
2014 dev->state.disabled = 1;
2019 static int cx8800_resume(struct pci_dev *pci_dev)
2021 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2022 struct cx88_core *core = dev->core;
2025 if (dev->state.disabled) {
2026 err=pci_enable_device(pci_dev);
2028 printk(KERN_ERR "%s/0: can't enable device\n",
2033 dev->state.disabled = 0;
2035 err= pci_set_power_state(pci_dev, PCI_D0);
2037 printk(KERN_ERR "%s/0: can't set power state\n", core->name);
2038 pci_disable_device(pci_dev);
2039 dev->state.disabled = 1;
2043 pci_restore_state(pci_dev);
2045 /* FIXME: re-initialize hardware */
2048 cx88_ir_start(core, core->ir);
2050 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
2052 /* restart video+vbi capture */
2053 spin_lock(&dev->slock);
2054 if (!list_empty(&dev->vidq.active)) {
2055 printk("%s/0: resume video\n", core->name);
2056 restart_video_queue(dev,&dev->vidq);
2058 if (!list_empty(&dev->vbiq.active)) {
2059 printk("%s/0: resume vbi\n", core->name);
2060 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2062 spin_unlock(&dev->slock);
2068 /* ----------------------------------------------------------- */
2070 static struct pci_device_id cx8800_pci_tbl[] = {
2074 .subvendor = PCI_ANY_ID,
2075 .subdevice = PCI_ANY_ID,
2077 /* --- end of list --- */
2080 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2082 static struct pci_driver cx8800_pci_driver = {
2084 .id_table = cx8800_pci_tbl,
2085 .probe = cx8800_initdev,
2086 .remove = __devexit_p(cx8800_finidev),
2088 .suspend = cx8800_suspend,
2089 .resume = cx8800_resume,
2093 static int cx8800_init(void)
2095 printk(KERN_INFO "cx88/0: cx2388x v4l2 driver version %d.%d.%d loaded\n",
2096 (CX88_VERSION_CODE >> 16) & 0xff,
2097 (CX88_VERSION_CODE >> 8) & 0xff,
2098 CX88_VERSION_CODE & 0xff);
2100 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2101 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2103 return pci_register_driver(&cx8800_pci_driver);
2106 static void cx8800_fini(void)
2108 pci_unregister_driver(&cx8800_pci_driver);
2111 module_init(cx8800_init);
2112 module_exit(cx8800_fini);
2114 /* ----------------------------------------------------------- */
2119 * 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