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 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/kmod.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/interrupt.h>
31 #include <linux/delay.h>
32 #include <linux/kthread.h>
33 #include <asm/div64.h>
36 #include <media/v4l2-common.h>
38 #ifdef CONFIG_VIDEO_V4L1_COMPAT
39 /* Include V4L1 specific functions. Should be removed soon */
40 #include <linux/videodev.h>
43 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
44 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
45 MODULE_LICENSE("GPL");
47 /* ------------------------------------------------------------------ */
49 static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
50 static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
51 static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
53 module_param_array(video_nr, int, NULL, 0444);
54 module_param_array(vbi_nr, int, NULL, 0444);
55 module_param_array(radio_nr, int, NULL, 0444);
57 MODULE_PARM_DESC(video_nr,"video device numbers");
58 MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
59 MODULE_PARM_DESC(radio_nr,"radio device numbers");
61 static unsigned int video_debug = 0;
62 module_param(video_debug,int,0644);
63 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
65 static unsigned int irq_debug = 0;
66 module_param(irq_debug,int,0644);
67 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
69 static unsigned int vid_limit = 16;
70 module_param(vid_limit,int,0644);
71 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
73 #define dprintk(level,fmt, arg...) if (video_debug >= level) \
74 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
76 /* ------------------------------------------------------------------ */
78 static LIST_HEAD(cx8800_devlist);
80 /* ------------------------------------------------------------------- */
83 static struct cx88_tvnorm tvnorms[] = {
86 .id = V4L2_STD_NTSC_M,
87 .cxiformat = VideoFormatNTSC,
88 .cxoformat = 0x181f0008,
91 .id = V4L2_STD_NTSC_M_JP,
92 .cxiformat = VideoFormatNTSCJapan,
93 .cxoformat = 0x181f0008,
96 .id = V4L2_STD_PAL_BG,
97 .cxiformat = VideoFormatPAL,
98 .cxoformat = 0x181f0008,
101 .id = V4L2_STD_PAL_DK,
102 .cxiformat = VideoFormatPAL,
103 .cxoformat = 0x181f0008,
106 .id = V4L2_STD_PAL_I,
107 .cxiformat = VideoFormatPAL,
108 .cxoformat = 0x181f0008,
111 .id = V4L2_STD_PAL_M,
112 .cxiformat = VideoFormatPALM,
113 .cxoformat = 0x1c1f0008,
116 .id = V4L2_STD_PAL_N,
117 .cxiformat = VideoFormatPALN,
118 .cxoformat = 0x1c1f0008,
121 .id = V4L2_STD_PAL_Nc,
122 .cxiformat = VideoFormatPALNC,
123 .cxoformat = 0x1c1f0008,
126 .id = V4L2_STD_PAL_60,
127 .cxiformat = VideoFormatPAL60,
128 .cxoformat = 0x181f0008,
131 .id = V4L2_STD_SECAM_L,
132 .cxiformat = VideoFormatSECAM,
133 .cxoformat = 0x181f0008,
136 .id = V4L2_STD_SECAM_DK,
137 .cxiformat = VideoFormatSECAM,
138 .cxoformat = 0x181f0008,
142 static struct cx8800_fmt formats[] = {
144 .name = "8 bpp, gray",
145 .fourcc = V4L2_PIX_FMT_GREY,
146 .cxformat = ColorFormatY8,
148 .flags = FORMAT_FLAGS_PACKED,
150 .name = "15 bpp RGB, le",
151 .fourcc = V4L2_PIX_FMT_RGB555,
152 .cxformat = ColorFormatRGB15,
154 .flags = FORMAT_FLAGS_PACKED,
156 .name = "15 bpp RGB, be",
157 .fourcc = V4L2_PIX_FMT_RGB555X,
158 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
160 .flags = FORMAT_FLAGS_PACKED,
162 .name = "16 bpp RGB, le",
163 .fourcc = V4L2_PIX_FMT_RGB565,
164 .cxformat = ColorFormatRGB16,
166 .flags = FORMAT_FLAGS_PACKED,
168 .name = "16 bpp RGB, be",
169 .fourcc = V4L2_PIX_FMT_RGB565X,
170 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
172 .flags = FORMAT_FLAGS_PACKED,
174 .name = "24 bpp RGB, le",
175 .fourcc = V4L2_PIX_FMT_BGR24,
176 .cxformat = ColorFormatRGB24,
178 .flags = FORMAT_FLAGS_PACKED,
180 .name = "32 bpp RGB, le",
181 .fourcc = V4L2_PIX_FMT_BGR32,
182 .cxformat = ColorFormatRGB32,
184 .flags = FORMAT_FLAGS_PACKED,
186 .name = "32 bpp RGB, be",
187 .fourcc = V4L2_PIX_FMT_RGB32,
188 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
190 .flags = FORMAT_FLAGS_PACKED,
192 .name = "4:2:2, packed, YUYV",
193 .fourcc = V4L2_PIX_FMT_YUYV,
194 .cxformat = ColorFormatYUY2,
196 .flags = FORMAT_FLAGS_PACKED,
198 .name = "4:2:2, packed, UYVY",
199 .fourcc = V4L2_PIX_FMT_UYVY,
200 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
202 .flags = FORMAT_FLAGS_PACKED,
206 static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
210 for (i = 0; i < ARRAY_SIZE(formats); i++)
211 if (formats[i].fourcc == fourcc)
216 /* ------------------------------------------------------------------- */
218 static const struct v4l2_queryctrl no_ctl = {
220 .flags = V4L2_CTRL_FLAG_DISABLED,
223 static struct cx88_ctrl cx8800_ctls[] = {
227 .id = V4L2_CID_BRIGHTNESS,
228 .name = "Brightness",
232 .default_value = 0x7f,
233 .type = V4L2_CTRL_TYPE_INTEGER,
236 .reg = MO_CONTR_BRIGHT,
241 .id = V4L2_CID_CONTRAST,
246 .default_value = 0x3f,
247 .type = V4L2_CTRL_TYPE_INTEGER,
250 .reg = MO_CONTR_BRIGHT,
260 .default_value = 0x7f,
261 .type = V4L2_CTRL_TYPE_INTEGER,
268 /* strictly, this only describes only U saturation.
269 * V saturation is handled specially through code.
272 .id = V4L2_CID_SATURATION,
273 .name = "Saturation",
277 .default_value = 0x7f,
278 .type = V4L2_CTRL_TYPE_INTEGER,
281 .reg = MO_UV_SATURATION,
287 .id = V4L2_CID_AUDIO_MUTE,
292 .type = V4L2_CTRL_TYPE_BOOLEAN,
295 .sreg = SHADOW_AUD_VOL_CTL,
300 .id = V4L2_CID_AUDIO_VOLUME,
305 .default_value = 0x3f,
306 .type = V4L2_CTRL_TYPE_INTEGER,
309 .sreg = SHADOW_AUD_VOL_CTL,
314 .id = V4L2_CID_AUDIO_BALANCE,
319 .default_value = 0x40,
320 .type = V4L2_CTRL_TYPE_INTEGER,
323 .sreg = SHADOW_AUD_BAL_CTL,
328 static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
330 const u32 cx88_user_ctrls[] = {
336 V4L2_CID_AUDIO_VOLUME,
337 V4L2_CID_AUDIO_BALANCE,
341 EXPORT_SYMBOL(cx88_user_ctrls);
343 static const u32 *ctrl_classes[] = {
348 int cx8800_ctrl_query(struct v4l2_queryctrl *qctrl)
352 if (qctrl->id < V4L2_CID_BASE ||
353 qctrl->id >= V4L2_CID_LASTP1)
355 for (i = 0; i < CX8800_CTLS; i++)
356 if (cx8800_ctls[i].v.id == qctrl->id)
358 if (i == CX8800_CTLS) {
362 *qctrl = cx8800_ctls[i].v;
365 EXPORT_SYMBOL(cx8800_ctrl_query);
367 static int cx88_queryctrl(struct v4l2_queryctrl *qctrl)
369 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
372 return cx8800_ctrl_query(qctrl);
375 /* ------------------------------------------------------------------- */
376 /* resource management */
378 static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
380 struct cx88_core *core = dev->core;
381 if (fh->resources & bit)
382 /* have it already allocated */
386 mutex_lock(&core->lock);
387 if (dev->resources & bit) {
388 /* no, someone else uses it */
389 mutex_unlock(&core->lock);
392 /* it's free, grab it */
393 fh->resources |= bit;
394 dev->resources |= bit;
395 dprintk(1,"res: get %d\n",bit);
396 mutex_unlock(&core->lock);
401 int res_check(struct cx8800_fh *fh, unsigned int bit)
403 return (fh->resources & bit);
407 int res_locked(struct cx8800_dev *dev, unsigned int bit)
409 return (dev->resources & bit);
413 void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
415 struct cx88_core *core = dev->core;
416 BUG_ON((fh->resources & bits) != bits);
418 mutex_lock(&core->lock);
419 fh->resources &= ~bits;
420 dev->resources &= ~bits;
421 dprintk(1,"res: put %d\n",bits);
422 mutex_unlock(&core->lock);
425 /* ------------------------------------------------------------------ */
427 /* static int video_mux(struct cx8800_dev *dev, unsigned int input) */
428 static int video_mux(struct cx88_core *core, unsigned int input)
430 /* struct cx88_core *core = dev->core; */
432 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
433 input, INPUT(input)->vmux,
434 INPUT(input)->gpio0,INPUT(input)->gpio1,
435 INPUT(input)->gpio2,INPUT(input)->gpio3);
437 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input)->vmux << 14);
438 cx_write(MO_GP3_IO, INPUT(input)->gpio3);
439 cx_write(MO_GP0_IO, INPUT(input)->gpio0);
440 cx_write(MO_GP1_IO, INPUT(input)->gpio1);
441 cx_write(MO_GP2_IO, INPUT(input)->gpio2);
443 switch (INPUT(input)->type) {
444 case CX88_VMUX_SVIDEO:
445 cx_set(MO_AFECFG_IO, 0x00000001);
446 cx_set(MO_INPUT_FORMAT, 0x00010010);
447 cx_set(MO_FILTER_EVEN, 0x00002020);
448 cx_set(MO_FILTER_ODD, 0x00002020);
451 cx_clear(MO_AFECFG_IO, 0x00000001);
452 cx_clear(MO_INPUT_FORMAT, 0x00010010);
453 cx_clear(MO_FILTER_EVEN, 0x00002020);
454 cx_clear(MO_FILTER_ODD, 0x00002020);
458 if (cx88_boards[core->board].mpeg & CX88_MPEG_BLACKBIRD) {
459 /* sets sound input from external adc */
460 if (INPUT(input)->extadc)
461 cx_set(AUD_CTL, EN_I2SIN_ENABLE);
463 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
468 /* ------------------------------------------------------------------ */
470 static int start_video_dma(struct cx8800_dev *dev,
471 struct cx88_dmaqueue *q,
472 struct cx88_buffer *buf)
474 struct cx88_core *core = dev->core;
476 /* setup fifo + format */
477 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
478 buf->bpl, buf->risc.dma);
479 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
480 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
483 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
487 cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
489 /* Enables corresponding bits at PCI_INT_STAT:
490 bits 0 to 4: video, audio, transport stream, VIP, Host
492 bits 8 and 9: DMA complete for: SRC, DST
493 bits 10 and 11: BERR signal asserted for RISC: RD, WR
494 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
496 cx_set(MO_VID_INTMSK, 0x0f0011);
499 cx_set(VID_CAPTURE_CONTROL,0x06);
502 cx_set(MO_DEV_CNTRL2, (1<<5));
503 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
509 static int stop_video_dma(struct cx8800_dev *dev)
511 struct cx88_core *core = dev->core;
514 cx_clear(MO_VID_DMACNTRL, 0x11);
516 /* disable capture */
517 cx_clear(VID_CAPTURE_CONTROL,0x06);
520 cx_clear(MO_PCI_INTMSK, 0x000001);
521 cx_clear(MO_VID_INTMSK, 0x0f0011);
526 static int restart_video_queue(struct cx8800_dev *dev,
527 struct cx88_dmaqueue *q)
529 struct cx88_core *core = dev->core;
530 struct cx88_buffer *buf, *prev;
531 struct list_head *item;
533 if (!list_empty(&q->active)) {
534 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
535 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
537 start_video_dma(dev, q, buf);
538 list_for_each(item,&q->active) {
539 buf = list_entry(item, struct cx88_buffer, vb.queue);
540 buf->count = q->count++;
542 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
548 if (list_empty(&q->queued))
550 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
552 list_move_tail(&buf->vb.queue, &q->active);
553 start_video_dma(dev, q, buf);
554 buf->vb.state = STATE_ACTIVE;
555 buf->count = q->count++;
556 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
557 dprintk(2,"[%p/%d] restart_queue - first active\n",
560 } else if (prev->vb.width == buf->vb.width &&
561 prev->vb.height == buf->vb.height &&
562 prev->fmt == buf->fmt) {
563 list_move_tail(&buf->vb.queue, &q->active);
564 buf->vb.state = STATE_ACTIVE;
565 buf->count = q->count++;
566 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
567 dprintk(2,"[%p/%d] restart_queue - move to active\n",
576 /* ------------------------------------------------------------------ */
579 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
581 struct cx8800_fh *fh = q->priv_data;
583 *size = fh->fmt->depth*fh->width*fh->height >> 3;
586 while (*size * *count > vid_limit * 1024 * 1024)
592 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
593 enum v4l2_field field)
595 struct cx8800_fh *fh = q->priv_data;
596 struct cx8800_dev *dev = fh->dev;
597 struct cx88_core *core = dev->core;
598 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
599 int rc, init_buffer = 0;
601 BUG_ON(NULL == fh->fmt);
602 if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
603 fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
605 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
606 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
609 if (buf->fmt != fh->fmt ||
610 buf->vb.width != fh->width ||
611 buf->vb.height != fh->height ||
612 buf->vb.field != field) {
614 buf->vb.width = fh->width;
615 buf->vb.height = fh->height;
616 buf->vb.field = field;
620 if (STATE_NEEDS_INIT == buf->vb.state) {
622 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
627 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
628 switch (buf->vb.field) {
630 cx88_risc_buffer(dev->pci, &buf->risc,
631 buf->vb.dma.sglist, 0, UNSET,
632 buf->bpl, 0, buf->vb.height);
634 case V4L2_FIELD_BOTTOM:
635 cx88_risc_buffer(dev->pci, &buf->risc,
636 buf->vb.dma.sglist, UNSET, 0,
637 buf->bpl, 0, buf->vb.height);
639 case V4L2_FIELD_INTERLACED:
640 cx88_risc_buffer(dev->pci, &buf->risc,
641 buf->vb.dma.sglist, 0, buf->bpl,
643 buf->vb.height >> 1);
645 case V4L2_FIELD_SEQ_TB:
646 cx88_risc_buffer(dev->pci, &buf->risc,
648 0, buf->bpl * (buf->vb.height >> 1),
650 buf->vb.height >> 1);
652 case V4L2_FIELD_SEQ_BT:
653 cx88_risc_buffer(dev->pci, &buf->risc,
655 buf->bpl * (buf->vb.height >> 1), 0,
657 buf->vb.height >> 1);
663 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
665 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
666 (unsigned long)buf->risc.dma);
668 buf->vb.state = STATE_PREPARED;
672 cx88_free_buffer(q,buf);
677 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
679 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
680 struct cx88_buffer *prev;
681 struct cx8800_fh *fh = vq->priv_data;
682 struct cx8800_dev *dev = fh->dev;
683 struct cx88_core *core = dev->core;
684 struct cx88_dmaqueue *q = &dev->vidq;
686 /* add jump to stopper */
687 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
688 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
690 if (!list_empty(&q->queued)) {
691 list_add_tail(&buf->vb.queue,&q->queued);
692 buf->vb.state = STATE_QUEUED;
693 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
696 } else if (list_empty(&q->active)) {
697 list_add_tail(&buf->vb.queue,&q->active);
698 start_video_dma(dev, q, buf);
699 buf->vb.state = STATE_ACTIVE;
700 buf->count = q->count++;
701 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
702 dprintk(2,"[%p/%d] buffer_queue - first active\n",
706 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
707 if (prev->vb.width == buf->vb.width &&
708 prev->vb.height == buf->vb.height &&
709 prev->fmt == buf->fmt) {
710 list_add_tail(&buf->vb.queue,&q->active);
711 buf->vb.state = STATE_ACTIVE;
712 buf->count = q->count++;
713 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
714 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
718 list_add_tail(&buf->vb.queue,&q->queued);
719 buf->vb.state = STATE_QUEUED;
720 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
726 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
728 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
730 cx88_free_buffer(q,buf);
733 static struct videobuf_queue_ops cx8800_video_qops = {
734 .buf_setup = buffer_setup,
735 .buf_prepare = buffer_prepare,
736 .buf_queue = buffer_queue,
737 .buf_release = buffer_release,
740 /* ------------------------------------------------------------------ */
743 /* ------------------------------------------------------------------ */
745 static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
748 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
750 case V4L2_BUF_TYPE_VBI_CAPTURE:
758 static int get_ressource(struct cx8800_fh *fh)
761 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
762 return RESOURCE_VIDEO;
763 case V4L2_BUF_TYPE_VBI_CAPTURE:
771 static int video_open(struct inode *inode, struct file *file)
773 int minor = iminor(inode);
774 struct cx8800_dev *h,*dev = NULL;
775 struct cx88_core *core;
776 struct cx8800_fh *fh;
777 struct list_head *list;
778 enum v4l2_buf_type type = 0;
781 list_for_each(list,&cx8800_devlist) {
782 h = list_entry(list, struct cx8800_dev, devlist);
783 if (h->video_dev->minor == minor) {
785 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
787 if (h->vbi_dev->minor == minor) {
789 type = V4L2_BUF_TYPE_VBI_CAPTURE;
792 h->radio_dev->minor == minor) {
802 dprintk(1,"open minor=%d radio=%d type=%s\n",
803 minor,radio,v4l2_type_names[type]);
805 /* allocate + initialize per filehandle data */
806 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
809 file->private_data = fh;
815 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
817 videobuf_queue_init(&fh->vidq, &cx8800_video_qops,
818 dev->pci, &dev->slock,
819 V4L2_BUF_TYPE_VIDEO_CAPTURE,
820 V4L2_FIELD_INTERLACED,
821 sizeof(struct cx88_buffer),
823 videobuf_queue_init(&fh->vbiq, &cx8800_vbi_qops,
824 dev->pci, &dev->slock,
825 V4L2_BUF_TYPE_VBI_CAPTURE,
827 sizeof(struct cx88_buffer),
831 int board = core->board;
832 dprintk(1,"video_open: setting radio device\n");
833 cx_write(MO_GP3_IO, cx88_boards[board].radio.gpio3);
834 cx_write(MO_GP0_IO, cx88_boards[board].radio.gpio0);
835 cx_write(MO_GP1_IO, cx88_boards[board].radio.gpio1);
836 cx_write(MO_GP2_IO, cx88_boards[board].radio.gpio2);
837 core->tvaudio = WW_FM;
838 cx88_set_tvaudio(core);
839 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
840 cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
847 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
849 struct cx8800_fh *fh = file->private_data;
852 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
853 if (res_locked(fh->dev,RESOURCE_VIDEO))
855 return videobuf_read_one(&fh->vidq, data, count, ppos,
856 file->f_flags & O_NONBLOCK);
857 case V4L2_BUF_TYPE_VBI_CAPTURE:
858 if (!res_get(fh->dev,fh,RESOURCE_VBI))
860 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
861 file->f_flags & O_NONBLOCK);
869 video_poll(struct file *file, struct poll_table_struct *wait)
871 struct cx8800_fh *fh = file->private_data;
872 struct cx88_buffer *buf;
874 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
875 if (!res_get(fh->dev,fh,RESOURCE_VBI))
877 return videobuf_poll_stream(file, &fh->vbiq, wait);
880 if (res_check(fh,RESOURCE_VIDEO)) {
881 /* streaming capture */
882 if (list_empty(&fh->vidq.stream))
884 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
887 buf = (struct cx88_buffer*)fh->vidq.read_buf;
891 poll_wait(file, &buf->vb.done, wait);
892 if (buf->vb.state == STATE_DONE ||
893 buf->vb.state == STATE_ERROR)
894 return POLLIN|POLLRDNORM;
898 static int video_release(struct inode *inode, struct file *file)
900 struct cx8800_fh *fh = file->private_data;
901 struct cx8800_dev *dev = fh->dev;
903 /* turn off overlay */
904 if (res_check(fh, RESOURCE_OVERLAY)) {
906 res_free(dev,fh,RESOURCE_OVERLAY);
909 /* stop video capture */
910 if (res_check(fh, RESOURCE_VIDEO)) {
911 videobuf_queue_cancel(&fh->vidq);
912 res_free(dev,fh,RESOURCE_VIDEO);
914 if (fh->vidq.read_buf) {
915 buffer_release(&fh->vidq,fh->vidq.read_buf);
916 kfree(fh->vidq.read_buf);
919 /* stop vbi capture */
920 if (res_check(fh, RESOURCE_VBI)) {
921 if (fh->vbiq.streaming)
922 videobuf_streamoff(&fh->vbiq);
923 if (fh->vbiq.reading)
924 videobuf_read_stop(&fh->vbiq);
925 res_free(dev,fh,RESOURCE_VBI);
928 videobuf_mmap_free(&fh->vidq);
929 videobuf_mmap_free(&fh->vbiq);
930 file->private_data = NULL;
933 cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
939 video_mmap(struct file *file, struct vm_area_struct * vma)
941 struct cx8800_fh *fh = file->private_data;
943 return videobuf_mmap_mapper(get_queue(fh), vma);
946 /* ------------------------------------------------------------------ */
948 /* static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
949 static int get_control(struct cx88_core *core, struct v4l2_control *ctl)
951 /* struct cx88_core *core = dev->core; */
952 struct cx88_ctrl *c = NULL;
956 for (i = 0; i < CX8800_CTLS; i++)
957 if (cx8800_ctls[i].v.id == ctl->id)
962 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
964 case V4L2_CID_AUDIO_BALANCE:
965 ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
966 : (0x7f - (value & 0x7f));
968 case V4L2_CID_AUDIO_VOLUME:
969 ctl->value = 0x3f - (value & 0x3f);
972 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
975 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
976 ctl->id, c->v.name, ctl->value, c->reg,
977 value,c->mask, c->sreg ? " [shadowed]" : "");
981 /* static int set_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
982 static int set_control(struct cx88_core *core, struct v4l2_control *ctl)
984 /* struct cx88_core *core = dev->core; */
985 struct cx88_ctrl *c = NULL;
988 for (i = 0; i < CX8800_CTLS; i++) {
989 if (cx8800_ctls[i].v.id == ctl->id) {
996 if (ctl->value < c->v.minimum)
997 ctl->value = c->v.minimum;
998 if (ctl->value > c->v.maximum)
999 ctl->value = c->v.maximum;
1002 case V4L2_CID_AUDIO_BALANCE:
1003 value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
1005 case V4L2_CID_AUDIO_VOLUME:
1006 value = 0x3f - (ctl->value & 0x3f);
1008 case V4L2_CID_SATURATION:
1009 /* special v_sat handling */
1011 value = ((ctl->value - c->off) << c->shift) & c->mask;
1013 if (core->tvnorm->id & V4L2_STD_SECAM) {
1014 /* For SECAM, both U and V sat should be equal */
1015 value=value<<8|value;
1017 /* Keeps U Saturation proportional to V Sat */
1018 value=(value*0x5a)/0x7f<<8|value;
1023 value = ((ctl->value - c->off) << c->shift) & c->mask;
1026 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
1027 ctl->id, c->v.name, ctl->value, c->reg, value,
1028 mask, c->sreg ? " [shadowed]" : "");
1030 cx_sandor(c->sreg, c->reg, mask, value);
1032 cx_andor(c->reg, mask, value);
1037 static void init_controls(struct cx88_core *core)
1039 struct v4l2_control ctrl;
1042 for (i = 0; i < CX8800_CTLS; i++) {
1043 ctrl.id=cx8800_ctls[i].v.id;
1044 ctrl.value=cx8800_ctls[i].v.default_value;
1045 set_control(core, &ctrl);
1049 /* ------------------------------------------------------------------ */
1051 static int cx8800_g_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1052 struct v4l2_format *f)
1055 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1056 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
1057 f->fmt.pix.width = fh->width;
1058 f->fmt.pix.height = fh->height;
1059 f->fmt.pix.field = fh->vidq.field;
1060 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1061 f->fmt.pix.bytesperline =
1062 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1063 f->fmt.pix.sizeimage =
1064 f->fmt.pix.height * f->fmt.pix.bytesperline;
1066 case V4L2_BUF_TYPE_VBI_CAPTURE:
1067 cx8800_vbi_fmt(dev, f);
1074 static int cx8800_try_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1075 struct v4l2_format *f)
1077 struct cx88_core *core = dev->core;
1080 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1082 struct cx8800_fmt *fmt;
1083 enum v4l2_field field;
1084 unsigned int maxw, maxh;
1086 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1090 field = f->fmt.pix.field;
1091 maxw = norm_maxw(core->tvnorm);
1092 maxh = norm_maxh(core->tvnorm);
1094 if (V4L2_FIELD_ANY == field) {
1095 field = (f->fmt.pix.height > maxh/2)
1096 ? V4L2_FIELD_INTERLACED
1097 : V4L2_FIELD_BOTTOM;
1101 case V4L2_FIELD_TOP:
1102 case V4L2_FIELD_BOTTOM:
1105 case V4L2_FIELD_INTERLACED:
1111 f->fmt.pix.field = field;
1112 if (f->fmt.pix.height < 32)
1113 f->fmt.pix.height = 32;
1114 if (f->fmt.pix.height > maxh)
1115 f->fmt.pix.height = maxh;
1116 if (f->fmt.pix.width < 48)
1117 f->fmt.pix.width = 48;
1118 if (f->fmt.pix.width > maxw)
1119 f->fmt.pix.width = maxw;
1120 f->fmt.pix.width &= ~0x03;
1121 f->fmt.pix.bytesperline =
1122 (f->fmt.pix.width * fmt->depth) >> 3;
1123 f->fmt.pix.sizeimage =
1124 f->fmt.pix.height * f->fmt.pix.bytesperline;
1128 case V4L2_BUF_TYPE_VBI_CAPTURE:
1129 cx8800_vbi_fmt(dev, f);
1136 static int cx8800_s_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1137 struct v4l2_format *f)
1142 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1143 err = cx8800_try_fmt(dev,fh,f);
1147 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1148 fh->width = f->fmt.pix.width;
1149 fh->height = f->fmt.pix.height;
1150 fh->vidq.field = f->fmt.pix.field;
1152 case V4L2_BUF_TYPE_VBI_CAPTURE:
1153 cx8800_vbi_fmt(dev, f);
1161 * This function is _not_ called directly, but from
1162 * video_generic_ioctl (and maybe others). userspace
1163 * copying is done already, arg is a kernel pointer.
1165 static int video_do_ioctl(struct inode *inode, struct file *file,
1166 unsigned int cmd, void *arg)
1168 struct cx8800_fh *fh = file->private_data;
1169 struct cx8800_dev *dev = fh->dev;
1170 struct cx88_core *core = dev->core;
1173 if (video_debug > 1)
1174 v4l_print_ioctl(core->name,cmd);
1177 /* --- capabilities ------------------------------------------ */
1178 case VIDIOC_QUERYCAP:
1180 struct v4l2_capability *cap = arg;
1182 memset(cap,0,sizeof(*cap));
1183 strcpy(cap->driver, "cx8800");
1184 strlcpy(cap->card, cx88_boards[core->board].name,
1186 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1187 cap->version = CX88_VERSION_CODE;
1189 V4L2_CAP_VIDEO_CAPTURE |
1190 V4L2_CAP_READWRITE |
1191 V4L2_CAP_STREAMING |
1192 V4L2_CAP_VBI_CAPTURE |
1194 if (UNSET != core->tuner_type)
1195 cap->capabilities |= V4L2_CAP_TUNER;
1199 /* --- capture ioctls ---------------------------------------- */
1200 case VIDIOC_ENUM_FMT:
1202 struct v4l2_fmtdesc *f = arg;
1203 enum v4l2_buf_type type;
1209 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1210 if (index >= ARRAY_SIZE(formats))
1212 memset(f,0,sizeof(*f));
1215 strlcpy(f->description,formats[index].name,sizeof(f->description));
1216 f->pixelformat = formats[index].fourcc;
1225 struct v4l2_format *f = arg;
1226 return cx8800_g_fmt(dev,fh,f);
1230 struct v4l2_format *f = arg;
1231 return cx8800_s_fmt(dev,fh,f);
1233 case VIDIOC_TRY_FMT:
1235 struct v4l2_format *f = arg;
1236 return cx8800_try_fmt(dev,fh,f);
1238 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1239 /* --- streaming capture ------------------------------------- */
1242 struct video_mbuf *mbuf = arg;
1243 struct videobuf_queue *q;
1244 struct v4l2_requestbuffers req;
1248 memset(&req,0,sizeof(req));
1251 req.memory = V4L2_MEMORY_MMAP;
1252 err = videobuf_reqbufs(q,&req);
1255 memset(mbuf,0,sizeof(*mbuf));
1256 mbuf->frames = req.count;
1258 for (i = 0; i < mbuf->frames; i++) {
1259 mbuf->offsets[i] = q->bufs[i]->boff;
1260 mbuf->size += q->bufs[i]->bsize;
1265 case VIDIOC_REQBUFS:
1266 return videobuf_reqbufs(get_queue(fh), arg);
1268 case VIDIOC_QUERYBUF:
1269 return videobuf_querybuf(get_queue(fh), arg);
1272 return videobuf_qbuf(get_queue(fh), arg);
1275 return videobuf_dqbuf(get_queue(fh), arg,
1276 file->f_flags & O_NONBLOCK);
1278 case VIDIOC_STREAMON:
1280 int res = get_ressource(fh);
1282 if (!res_get(dev,fh,res))
1284 return videobuf_streamon(get_queue(fh));
1286 case VIDIOC_STREAMOFF:
1288 int res = get_ressource(fh);
1290 err = videobuf_streamoff(get_queue(fh));
1293 res_free(dev,fh,res);
1297 return cx88_do_ioctl( inode, file, fh->radio, core, cmd, arg, video_do_ioctl );
1302 int cx88_do_ioctl(struct inode *inode, struct file *file, int radio,
1303 struct cx88_core *core, unsigned int cmd, void *arg, v4l2_kioctl driver_ioctl)
1308 if (video_debug > 1) {
1309 if (_IOC_DIR(cmd) & _IOC_WRITE)
1310 v4l_printk_ioctl_arg("cx88(w)",cmd, arg);
1311 else if (!_IOC_DIR(cmd) & _IOC_READ) {
1312 v4l_print_ioctl("cx88", cmd);
1315 v4l_print_ioctl(core->name,cmd);
1320 /* ---------- tv norms ---------- */
1321 case VIDIOC_ENUMSTD:
1323 struct v4l2_standard *e = arg;
1327 if (i >= ARRAY_SIZE(tvnorms))
1329 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1330 tvnorms[e->index].name);
1338 v4l2_std_id *id = arg;
1340 *id = core->tvnorm->id;
1345 v4l2_std_id *id = arg;
1348 for(i = 0; i < ARRAY_SIZE(tvnorms); i++)
1349 if (*id & tvnorms[i].id)
1351 if (i == ARRAY_SIZE(tvnorms))
1354 mutex_lock(&core->lock);
1355 cx88_set_tvnorm(core,&tvnorms[i]);
1356 mutex_unlock(&core->lock);
1360 /* ------ input switching ---------- */
1361 case VIDIOC_ENUMINPUT:
1363 static const char *iname[] = {
1364 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1365 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1366 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1367 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1368 [ CX88_VMUX_SVIDEO ] = "S-Video",
1369 [ CX88_VMUX_TELEVISION ] = "Television",
1370 [ CX88_VMUX_CABLE ] = "Cable TV",
1371 [ CX88_VMUX_DVB ] = "DVB",
1372 [ CX88_VMUX_DEBUG ] = "for debug only",
1374 struct v4l2_input *i = arg;
1380 if (0 == INPUT(n)->type)
1382 memset(i,0,sizeof(*i));
1384 i->type = V4L2_INPUT_TYPE_CAMERA;
1385 strcpy(i->name,iname[INPUT(n)->type]);
1386 if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
1387 (CX88_VMUX_CABLE == INPUT(n)->type))
1388 i->type = V4L2_INPUT_TYPE_TUNER;
1389 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1390 i->std |= tvnorms[n].id;
1393 case VIDIOC_G_INPUT:
1395 unsigned int *i = arg;
1400 case VIDIOC_S_INPUT:
1402 unsigned int *i = arg;
1406 mutex_lock(&core->lock);
1407 cx88_newstation(core);
1409 mutex_unlock(&core->lock);
1415 /* --- controls ---------------------------------------------- */
1416 case VIDIOC_QUERYCTRL:
1418 struct v4l2_queryctrl *c = arg;
1420 return cx88_queryctrl(c);
1423 return get_control(core,arg);
1425 return set_control(core,arg);
1427 /* --- tuner ioctls ------------------------------------------ */
1428 case VIDIOC_G_TUNER:
1430 struct v4l2_tuner *t = arg;
1433 if (UNSET == core->tuner_type)
1438 memset(t,0,sizeof(*t));
1439 strcpy(t->name, "Television");
1440 t->type = V4L2_TUNER_ANALOG_TV;
1441 t->capability = V4L2_TUNER_CAP_NORM;
1442 t->rangehigh = 0xffffffffUL;
1444 cx88_get_stereo(core ,t);
1445 reg = cx_read(MO_DEVICE_STATUS);
1446 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1449 case VIDIOC_S_TUNER:
1451 struct v4l2_tuner *t = arg;
1453 if (UNSET == core->tuner_type)
1457 cx88_set_stereo(core, t->audmode, 1);
1460 case VIDIOC_G_FREQUENCY:
1462 struct v4l2_frequency *f = arg;
1464 memset(f,0,sizeof(*f));
1466 if (UNSET == core->tuner_type)
1469 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1470 f->type = radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1471 f->frequency = core->freq;
1473 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1477 case VIDIOC_S_FREQUENCY:
1479 struct v4l2_frequency *f = arg;
1481 if (UNSET == core->tuner_type)
1485 if (0 == radio && f->type != V4L2_TUNER_ANALOG_TV)
1487 if (1 == radio && f->type != V4L2_TUNER_RADIO)
1489 mutex_lock(&core->lock);
1490 core->freq = f->frequency;
1491 cx88_newstation(core);
1492 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1494 /* When changing channels it is required to reset TVAUDIO */
1496 cx88_set_tvaudio(core);
1498 mutex_unlock(&core->lock);
1501 #ifdef CONFIG_VIDEO_ADV_DEBUG
1502 /* ioctls to allow direct acces to the cx2388x registers */
1503 case VIDIOC_INT_G_REGISTER:
1505 struct v4l2_register *reg = arg;
1507 if (reg->i2c_id != 0)
1509 /* cx2388x has a 24-bit register space */
1510 reg->val = cx_read(reg->reg&0xffffff);
1513 case VIDIOC_INT_S_REGISTER:
1515 struct v4l2_register *reg = arg;
1517 if (reg->i2c_id != 0)
1519 if (!capable(CAP_SYS_ADMIN))
1521 cx_write(reg->reg&0xffffff, reg->val);
1527 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1533 static int video_ioctl(struct inode *inode, struct file *file,
1534 unsigned int cmd, unsigned long arg)
1538 retval=video_usercopy(inode, file, cmd, arg, video_do_ioctl);
1540 if (video_debug > 1) {
1542 v4l_print_ioctl("cx88(err)", cmd);
1543 printk(KERN_DEBUG "cx88(err): errcode=%d\n",retval);
1544 } else if (_IOC_DIR(cmd) & _IOC_READ)
1545 v4l_printk_ioctl_arg("cx88(r)",cmd, (void *)arg);
1551 /* ----------------------------------------------------------- */
1553 static int radio_do_ioctl(struct inode *inode, struct file *file,
1554 unsigned int cmd, void *arg)
1556 struct cx8800_fh *fh = file->private_data;
1557 struct cx8800_dev *dev = fh->dev;
1558 struct cx88_core *core = dev->core;
1560 if (video_debug > 1)
1561 v4l_print_ioctl(core->name,cmd);
1564 case VIDIOC_QUERYCAP:
1566 struct v4l2_capability *cap = arg;
1568 memset(cap,0,sizeof(*cap));
1569 strcpy(cap->driver, "cx8800");
1570 strlcpy(cap->card, cx88_boards[core->board].name,
1572 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1573 cap->version = CX88_VERSION_CODE;
1574 cap->capabilities = V4L2_CAP_TUNER;
1577 case VIDIOC_G_TUNER:
1579 struct v4l2_tuner *t = arg;
1584 memset(t,0,sizeof(*t));
1585 strcpy(t->name, "Radio");
1586 t->type = V4L2_TUNER_RADIO;
1588 cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
1591 case VIDIOC_ENUMINPUT:
1593 struct v4l2_input *i = arg;
1597 strcpy(i->name,"Radio");
1598 i->type = V4L2_INPUT_TYPE_TUNER;
1601 case VIDIOC_G_INPUT:
1607 case VIDIOC_G_AUDIO:
1609 struct v4l2_audio *a = arg;
1611 memset(a,0,sizeof(*a));
1612 strcpy(a->name,"Radio");
1617 v4l2_std_id *id = arg;
1621 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1624 struct video_tuner *v = arg;
1626 if (v->tuner) /* Only tuner 0 */
1629 cx88_call_i2c_clients(core,VIDIOCSTUNER,v);
1633 case VIDIOC_S_TUNER:
1635 struct v4l2_tuner *t = arg;
1640 cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1645 case VIDIOC_S_AUDIO:
1646 case VIDIOC_S_INPUT:
1650 case VIDIOC_QUERYCTRL:
1652 struct v4l2_queryctrl *c = arg;
1655 if (c->id < V4L2_CID_BASE ||
1656 c->id >= V4L2_CID_LASTP1)
1658 if (c->id == V4L2_CID_AUDIO_MUTE) {
1659 for (i = 0; i < CX8800_CTLS; i++)
1660 if (cx8800_ctls[i].v.id == c->id)
1662 *c = cx8800_ctls[i].v;
1671 case VIDIOC_G_FREQUENCY:
1672 case VIDIOC_S_FREQUENCY:
1673 return video_do_ioctl(inode,file,cmd,arg);
1676 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1682 static int radio_ioctl(struct inode *inode, struct file *file,
1683 unsigned int cmd, unsigned long arg)
1685 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
1688 /* ----------------------------------------------------------- */
1690 static void cx8800_vid_timeout(unsigned long data)
1692 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1693 struct cx88_core *core = dev->core;
1694 struct cx88_dmaqueue *q = &dev->vidq;
1695 struct cx88_buffer *buf;
1696 unsigned long flags;
1698 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1700 cx_clear(MO_VID_DMACNTRL, 0x11);
1701 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1703 spin_lock_irqsave(&dev->slock,flags);
1704 while (!list_empty(&q->active)) {
1705 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1706 list_del(&buf->vb.queue);
1707 buf->vb.state = STATE_ERROR;
1708 wake_up(&buf->vb.done);
1709 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1710 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1712 restart_video_queue(dev,q);
1713 spin_unlock_irqrestore(&dev->slock,flags);
1716 static char *cx88_vid_irqs[32] = {
1717 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1718 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1719 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1720 "y_sync", "u_sync", "v_sync", "vbi_sync",
1721 "opc_err", "par_err", "rip_err", "pci_abort",
1724 static void cx8800_vid_irq(struct cx8800_dev *dev)
1726 struct cx88_core *core = dev->core;
1727 u32 status, mask, count;
1729 status = cx_read(MO_VID_INTSTAT);
1730 mask = cx_read(MO_VID_INTMSK);
1731 if (0 == (status & mask))
1733 cx_write(MO_VID_INTSTAT, status);
1734 if (irq_debug || (status & mask & ~0xff))
1735 cx88_print_irqbits(core->name, "irq vid",
1736 cx88_vid_irqs, status, mask);
1738 /* risc op code error */
1739 if (status & (1 << 16)) {
1740 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1741 cx_clear(MO_VID_DMACNTRL, 0x11);
1742 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1743 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1747 if (status & 0x01) {
1748 spin_lock(&dev->slock);
1749 count = cx_read(MO_VIDY_GPCNT);
1750 cx88_wakeup(core, &dev->vidq, count);
1751 spin_unlock(&dev->slock);
1755 if (status & 0x08) {
1756 spin_lock(&dev->slock);
1757 count = cx_read(MO_VBI_GPCNT);
1758 cx88_wakeup(core, &dev->vbiq, count);
1759 spin_unlock(&dev->slock);
1763 if (status & 0x10) {
1764 dprintk(2,"stopper video\n");
1765 spin_lock(&dev->slock);
1766 restart_video_queue(dev,&dev->vidq);
1767 spin_unlock(&dev->slock);
1771 if (status & 0x80) {
1772 dprintk(2,"stopper vbi\n");
1773 spin_lock(&dev->slock);
1774 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1775 spin_unlock(&dev->slock);
1779 static irqreturn_t cx8800_irq(int irq, void *dev_id)
1781 struct cx8800_dev *dev = dev_id;
1782 struct cx88_core *core = dev->core;
1784 int loop, handled = 0;
1786 for (loop = 0; loop < 10; loop++) {
1787 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
1790 cx_write(MO_PCI_INTSTAT, status);
1793 if (status & core->pci_irqmask)
1794 cx88_core_irq(core,status);
1796 cx8800_vid_irq(dev);
1799 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1801 cx_write(MO_PCI_INTMSK,0);
1805 return IRQ_RETVAL(handled);
1808 /* ----------------------------------------------------------- */
1809 /* exported stuff */
1811 static const struct file_operations video_fops =
1813 .owner = THIS_MODULE,
1815 .release = video_release,
1819 .ioctl = video_ioctl,
1820 .compat_ioctl = v4l_compat_ioctl32,
1821 .llseek = no_llseek,
1824 static struct video_device cx8800_video_template =
1826 .name = "cx8800-video",
1827 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1829 .fops = &video_fops,
1833 static struct video_device cx8800_vbi_template =
1835 .name = "cx8800-vbi",
1836 .type = VID_TYPE_TELETEXT|VID_TYPE_TUNER,
1838 .fops = &video_fops,
1842 static const struct file_operations radio_fops =
1844 .owner = THIS_MODULE,
1846 .release = video_release,
1847 .ioctl = radio_ioctl,
1848 .compat_ioctl = v4l_compat_ioctl32,
1849 .llseek = no_llseek,
1852 static struct video_device cx8800_radio_template =
1854 .name = "cx8800-radio",
1855 .type = VID_TYPE_TUNER,
1857 .fops = &radio_fops,
1861 /* ----------------------------------------------------------- */
1863 static void cx8800_unregister_video(struct cx8800_dev *dev)
1865 if (dev->radio_dev) {
1866 if (-1 != dev->radio_dev->minor)
1867 video_unregister_device(dev->radio_dev);
1869 video_device_release(dev->radio_dev);
1870 dev->radio_dev = NULL;
1873 if (-1 != dev->vbi_dev->minor)
1874 video_unregister_device(dev->vbi_dev);
1876 video_device_release(dev->vbi_dev);
1877 dev->vbi_dev = NULL;
1879 if (dev->video_dev) {
1880 if (-1 != dev->video_dev->minor)
1881 video_unregister_device(dev->video_dev);
1883 video_device_release(dev->video_dev);
1884 dev->video_dev = NULL;
1888 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1889 const struct pci_device_id *pci_id)
1891 struct cx8800_dev *dev;
1892 struct cx88_core *core;
1895 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1901 if (pci_enable_device(pci_dev)) {
1905 core = cx88_core_get(dev->pci);
1912 /* print pci info */
1913 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
1914 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1915 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1916 "latency: %d, mmio: 0x%llx\n", core->name,
1917 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1918 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
1920 pci_set_master(pci_dev);
1921 if (!pci_dma_supported(pci_dev,0xffffffff)) {
1922 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1927 /* initialize driver struct */
1928 spin_lock_init(&dev->slock);
1929 core->tvnorm = tvnorms;
1931 /* init video dma queues */
1932 INIT_LIST_HEAD(&dev->vidq.active);
1933 INIT_LIST_HEAD(&dev->vidq.queued);
1934 dev->vidq.timeout.function = cx8800_vid_timeout;
1935 dev->vidq.timeout.data = (unsigned long)dev;
1936 init_timer(&dev->vidq.timeout);
1937 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1938 MO_VID_DMACNTRL,0x11,0x00);
1940 /* init vbi dma queues */
1941 INIT_LIST_HEAD(&dev->vbiq.active);
1942 INIT_LIST_HEAD(&dev->vbiq.queued);
1943 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1944 dev->vbiq.timeout.data = (unsigned long)dev;
1945 init_timer(&dev->vbiq.timeout);
1946 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1947 MO_VID_DMACNTRL,0x88,0x00);
1950 err = request_irq(pci_dev->irq, cx8800_irq,
1951 IRQF_SHARED | IRQF_DISABLED, core->name, dev);
1953 printk(KERN_ERR "%s: can't get IRQ %d\n",
1954 core->name,pci_dev->irq);
1957 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1959 /* load and configure helper modules */
1960 if (TUNER_ABSENT != core->tuner_type)
1961 request_module("tuner");
1963 if (cx88_boards[ core->board ].audio_chip == AUDIO_CHIP_WM8775)
1964 request_module("wm8775");
1966 /* register v4l devices */
1967 dev->video_dev = cx88_vdev_init(core,dev->pci,
1968 &cx8800_video_template,"video");
1969 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1970 video_nr[core->nr]);
1972 printk(KERN_INFO "%s: can't register video device\n",
1976 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1977 core->name,dev->video_dev->minor & 0x1f);
1979 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1980 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1983 printk(KERN_INFO "%s/0: can't register vbi device\n",
1987 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1988 core->name,dev->vbi_dev->minor & 0x1f);
1990 if (core->has_radio) {
1991 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1992 &cx8800_radio_template,"radio");
1993 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1994 radio_nr[core->nr]);
1996 printk(KERN_INFO "%s/0: can't register radio device\n",
2000 printk(KERN_INFO "%s/0: registered device radio%d\n",
2001 core->name,dev->radio_dev->minor & 0x1f);
2004 /* everything worked */
2005 list_add_tail(&dev->devlist,&cx8800_devlist);
2006 pci_set_drvdata(pci_dev,dev);
2008 /* initial device configuration */
2009 mutex_lock(&core->lock);
2010 cx88_set_tvnorm(core,tvnorms);
2011 init_controls(core);
2013 mutex_unlock(&core->lock);
2015 /* start tvaudio thread */
2016 if (core->tuner_type != TUNER_ABSENT)
2017 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
2021 cx8800_unregister_video(dev);
2022 free_irq(pci_dev->irq, dev);
2024 cx88_core_put(core,dev->pci);
2030 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
2032 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2033 struct cx88_core *core = dev->core;
2036 if (core->kthread) {
2037 kthread_stop(core->kthread);
2038 core->kthread = NULL;
2041 cx88_shutdown(core); /* FIXME */
2042 pci_disable_device(pci_dev);
2044 /* unregister stuff */
2046 free_irq(pci_dev->irq, dev);
2047 cx8800_unregister_video(dev);
2048 pci_set_drvdata(pci_dev, NULL);
2051 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
2052 list_del(&dev->devlist);
2053 cx88_core_put(core,dev->pci);
2058 static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
2060 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2061 struct cx88_core *core = dev->core;
2063 /* stop video+vbi capture */
2064 spin_lock(&dev->slock);
2065 if (!list_empty(&dev->vidq.active)) {
2066 printk("%s: suspend video\n", core->name);
2067 stop_video_dma(dev);
2068 del_timer(&dev->vidq.timeout);
2070 if (!list_empty(&dev->vbiq.active)) {
2071 printk("%s: suspend vbi\n", core->name);
2072 cx8800_stop_vbi_dma(dev);
2073 del_timer(&dev->vbiq.timeout);
2075 spin_unlock(&dev->slock);
2077 /* FIXME -- shutdown device */
2078 cx88_shutdown(core);
2080 pci_save_state(pci_dev);
2081 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
2082 pci_disable_device(pci_dev);
2083 dev->state.disabled = 1;
2088 static int cx8800_resume(struct pci_dev *pci_dev)
2090 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2091 struct cx88_core *core = dev->core;
2094 if (dev->state.disabled) {
2095 err=pci_enable_device(pci_dev);
2097 printk(KERN_ERR "%s: can't enable device\n",
2102 dev->state.disabled = 0;
2104 err= pci_set_power_state(pci_dev, PCI_D0);
2106 printk(KERN_ERR "%s: can't enable device\n",
2109 pci_disable_device(pci_dev);
2110 dev->state.disabled = 1;
2114 pci_restore_state(pci_dev);
2116 /* FIXME: re-initialize hardware */
2119 /* restart video+vbi capture */
2120 spin_lock(&dev->slock);
2121 if (!list_empty(&dev->vidq.active)) {
2122 printk("%s: resume video\n", core->name);
2123 restart_video_queue(dev,&dev->vidq);
2125 if (!list_empty(&dev->vbiq.active)) {
2126 printk("%s: resume vbi\n", core->name);
2127 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2129 spin_unlock(&dev->slock);
2135 /* ----------------------------------------------------------- */
2137 static struct pci_device_id cx8800_pci_tbl[] = {
2141 .subvendor = PCI_ANY_ID,
2142 .subdevice = PCI_ANY_ID,
2144 /* --- end of list --- */
2147 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2149 static struct pci_driver cx8800_pci_driver = {
2151 .id_table = cx8800_pci_tbl,
2152 .probe = cx8800_initdev,
2153 .remove = __devexit_p(cx8800_finidev),
2155 .suspend = cx8800_suspend,
2156 .resume = cx8800_resume,
2160 static int cx8800_init(void)
2162 printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
2163 (CX88_VERSION_CODE >> 16) & 0xff,
2164 (CX88_VERSION_CODE >> 8) & 0xff,
2165 CX88_VERSION_CODE & 0xff);
2167 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2168 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2170 return pci_register_driver(&cx8800_pci_driver);
2173 static void cx8800_fini(void)
2175 pci_unregister_driver(&cx8800_pci_driver);
2178 module_init(cx8800_init);
2179 module_exit(cx8800_fini);
2181 EXPORT_SYMBOL(cx88_do_ioctl);
2183 /* ----------------------------------------------------------- */
2188 * 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