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);
460 /* ------------------------------------------------------------------ */
462 static int start_video_dma(struct cx8800_dev *dev,
463 struct cx88_dmaqueue *q,
464 struct cx88_buffer *buf)
466 struct cx88_core *core = dev->core;
468 /* setup fifo + format */
469 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
470 buf->bpl, buf->risc.dma);
471 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
472 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
475 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
479 cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
481 /* Enables corresponding bits at PCI_INT_STAT:
482 bits 0 to 4: video, audio, transport stream, VIP, Host
484 bits 8 and 9: DMA complete for: SRC, DST
485 bits 10 and 11: BERR signal asserted for RISC: RD, WR
486 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
488 cx_set(MO_VID_INTMSK, 0x0f0011);
491 cx_set(VID_CAPTURE_CONTROL,0x06);
494 cx_set(MO_DEV_CNTRL2, (1<<5));
495 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
500 static int stop_video_dma(struct cx8800_dev *dev)
502 struct cx88_core *core = dev->core;
505 cx_clear(MO_VID_DMACNTRL, 0x11);
507 /* disable capture */
508 cx_clear(VID_CAPTURE_CONTROL,0x06);
511 cx_clear(MO_PCI_INTMSK, 0x000001);
512 cx_clear(MO_VID_INTMSK, 0x0f0011);
516 static int restart_video_queue(struct cx8800_dev *dev,
517 struct cx88_dmaqueue *q)
519 struct cx88_core *core = dev->core;
520 struct cx88_buffer *buf, *prev;
521 struct list_head *item;
523 if (!list_empty(&q->active)) {
524 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
525 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
527 start_video_dma(dev, q, buf);
528 list_for_each(item,&q->active) {
529 buf = list_entry(item, struct cx88_buffer, vb.queue);
530 buf->count = q->count++;
532 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
538 if (list_empty(&q->queued))
540 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
542 list_move_tail(&buf->vb.queue, &q->active);
543 start_video_dma(dev, q, buf);
544 buf->vb.state = STATE_ACTIVE;
545 buf->count = q->count++;
546 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
547 dprintk(2,"[%p/%d] restart_queue - first active\n",
550 } else if (prev->vb.width == buf->vb.width &&
551 prev->vb.height == buf->vb.height &&
552 prev->fmt == buf->fmt) {
553 list_move_tail(&buf->vb.queue, &q->active);
554 buf->vb.state = STATE_ACTIVE;
555 buf->count = q->count++;
556 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
557 dprintk(2,"[%p/%d] restart_queue - move to active\n",
566 /* ------------------------------------------------------------------ */
569 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
571 struct cx8800_fh *fh = q->priv_data;
573 *size = fh->fmt->depth*fh->width*fh->height >> 3;
576 while (*size * *count > vid_limit * 1024 * 1024)
582 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
583 enum v4l2_field field)
585 struct cx8800_fh *fh = q->priv_data;
586 struct cx8800_dev *dev = fh->dev;
587 struct cx88_core *core = dev->core;
588 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
589 int rc, init_buffer = 0;
591 BUG_ON(NULL == fh->fmt);
592 if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
593 fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
595 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
596 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
599 if (buf->fmt != fh->fmt ||
600 buf->vb.width != fh->width ||
601 buf->vb.height != fh->height ||
602 buf->vb.field != field) {
604 buf->vb.width = fh->width;
605 buf->vb.height = fh->height;
606 buf->vb.field = field;
610 if (STATE_NEEDS_INIT == buf->vb.state) {
612 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
617 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
618 switch (buf->vb.field) {
620 cx88_risc_buffer(dev->pci, &buf->risc,
621 buf->vb.dma.sglist, 0, UNSET,
622 buf->bpl, 0, buf->vb.height);
624 case V4L2_FIELD_BOTTOM:
625 cx88_risc_buffer(dev->pci, &buf->risc,
626 buf->vb.dma.sglist, UNSET, 0,
627 buf->bpl, 0, buf->vb.height);
629 case V4L2_FIELD_INTERLACED:
630 cx88_risc_buffer(dev->pci, &buf->risc,
631 buf->vb.dma.sglist, 0, buf->bpl,
633 buf->vb.height >> 1);
635 case V4L2_FIELD_SEQ_TB:
636 cx88_risc_buffer(dev->pci, &buf->risc,
638 0, buf->bpl * (buf->vb.height >> 1),
640 buf->vb.height >> 1);
642 case V4L2_FIELD_SEQ_BT:
643 cx88_risc_buffer(dev->pci, &buf->risc,
645 buf->bpl * (buf->vb.height >> 1), 0,
647 buf->vb.height >> 1);
653 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
655 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
656 (unsigned long)buf->risc.dma);
658 buf->vb.state = STATE_PREPARED;
662 cx88_free_buffer(q,buf);
667 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
669 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
670 struct cx88_buffer *prev;
671 struct cx8800_fh *fh = vq->priv_data;
672 struct cx8800_dev *dev = fh->dev;
673 struct cx88_core *core = dev->core;
674 struct cx88_dmaqueue *q = &dev->vidq;
676 /* add jump to stopper */
677 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
678 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
680 if (!list_empty(&q->queued)) {
681 list_add_tail(&buf->vb.queue,&q->queued);
682 buf->vb.state = STATE_QUEUED;
683 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
686 } else if (list_empty(&q->active)) {
687 list_add_tail(&buf->vb.queue,&q->active);
688 start_video_dma(dev, q, buf);
689 buf->vb.state = STATE_ACTIVE;
690 buf->count = q->count++;
691 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
692 dprintk(2,"[%p/%d] buffer_queue - first active\n",
696 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
697 if (prev->vb.width == buf->vb.width &&
698 prev->vb.height == buf->vb.height &&
699 prev->fmt == buf->fmt) {
700 list_add_tail(&buf->vb.queue,&q->active);
701 buf->vb.state = STATE_ACTIVE;
702 buf->count = q->count++;
703 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
704 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
708 list_add_tail(&buf->vb.queue,&q->queued);
709 buf->vb.state = STATE_QUEUED;
710 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
716 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
718 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
720 cx88_free_buffer(q,buf);
723 static struct videobuf_queue_ops cx8800_video_qops = {
724 .buf_setup = buffer_setup,
725 .buf_prepare = buffer_prepare,
726 .buf_queue = buffer_queue,
727 .buf_release = buffer_release,
730 /* ------------------------------------------------------------------ */
733 /* ------------------------------------------------------------------ */
735 static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
738 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
740 case V4L2_BUF_TYPE_VBI_CAPTURE:
748 static int get_ressource(struct cx8800_fh *fh)
751 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
752 return RESOURCE_VIDEO;
753 case V4L2_BUF_TYPE_VBI_CAPTURE:
761 static int video_open(struct inode *inode, struct file *file)
763 int minor = iminor(inode);
764 struct cx8800_dev *h,*dev = NULL;
765 struct cx88_core *core;
766 struct cx8800_fh *fh;
767 struct list_head *list;
768 enum v4l2_buf_type type = 0;
771 list_for_each(list,&cx8800_devlist) {
772 h = list_entry(list, struct cx8800_dev, devlist);
773 if (h->video_dev->minor == minor) {
775 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
777 if (h->vbi_dev->minor == minor) {
779 type = V4L2_BUF_TYPE_VBI_CAPTURE;
782 h->radio_dev->minor == minor) {
792 dprintk(1,"open minor=%d radio=%d type=%s\n",
793 minor,radio,v4l2_type_names[type]);
795 /* allocate + initialize per filehandle data */
796 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
799 file->private_data = fh;
805 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
807 videobuf_queue_init(&fh->vidq, &cx8800_video_qops,
808 dev->pci, &dev->slock,
809 V4L2_BUF_TYPE_VIDEO_CAPTURE,
810 V4L2_FIELD_INTERLACED,
811 sizeof(struct cx88_buffer),
813 videobuf_queue_init(&fh->vbiq, &cx8800_vbi_qops,
814 dev->pci, &dev->slock,
815 V4L2_BUF_TYPE_VBI_CAPTURE,
817 sizeof(struct cx88_buffer),
821 int board = core->board;
822 dprintk(1,"video_open: setting radio device\n");
823 cx_write(MO_GP3_IO, cx88_boards[board].radio.gpio3);
824 cx_write(MO_GP0_IO, cx88_boards[board].radio.gpio0);
825 cx_write(MO_GP1_IO, cx88_boards[board].radio.gpio1);
826 cx_write(MO_GP2_IO, cx88_boards[board].radio.gpio2);
827 core->tvaudio = WW_FM;
828 cx88_set_tvaudio(core);
829 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
830 cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
837 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
839 struct cx8800_fh *fh = file->private_data;
842 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
843 if (res_locked(fh->dev,RESOURCE_VIDEO))
845 return videobuf_read_one(&fh->vidq, data, count, ppos,
846 file->f_flags & O_NONBLOCK);
847 case V4L2_BUF_TYPE_VBI_CAPTURE:
848 if (!res_get(fh->dev,fh,RESOURCE_VBI))
850 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
851 file->f_flags & O_NONBLOCK);
859 video_poll(struct file *file, struct poll_table_struct *wait)
861 struct cx8800_fh *fh = file->private_data;
862 struct cx88_buffer *buf;
864 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
865 if (!res_get(fh->dev,fh,RESOURCE_VBI))
867 return videobuf_poll_stream(file, &fh->vbiq, wait);
870 if (res_check(fh,RESOURCE_VIDEO)) {
871 /* streaming capture */
872 if (list_empty(&fh->vidq.stream))
874 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
877 buf = (struct cx88_buffer*)fh->vidq.read_buf;
881 poll_wait(file, &buf->vb.done, wait);
882 if (buf->vb.state == STATE_DONE ||
883 buf->vb.state == STATE_ERROR)
884 return POLLIN|POLLRDNORM;
888 static int video_release(struct inode *inode, struct file *file)
890 struct cx8800_fh *fh = file->private_data;
891 struct cx8800_dev *dev = fh->dev;
893 /* turn off overlay */
894 if (res_check(fh, RESOURCE_OVERLAY)) {
896 res_free(dev,fh,RESOURCE_OVERLAY);
899 /* stop video capture */
900 if (res_check(fh, RESOURCE_VIDEO)) {
901 videobuf_queue_cancel(&fh->vidq);
902 res_free(dev,fh,RESOURCE_VIDEO);
904 if (fh->vidq.read_buf) {
905 buffer_release(&fh->vidq,fh->vidq.read_buf);
906 kfree(fh->vidq.read_buf);
909 /* stop vbi capture */
910 if (res_check(fh, RESOURCE_VBI)) {
911 if (fh->vbiq.streaming)
912 videobuf_streamoff(&fh->vbiq);
913 if (fh->vbiq.reading)
914 videobuf_read_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 /* ------------------------------------------------------------------ */
938 /* static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
939 static int get_control(struct cx88_core *core, struct v4l2_control *ctl)
941 /* struct cx88_core *core = dev->core; */
942 struct cx88_ctrl *c = NULL;
946 for (i = 0; i < CX8800_CTLS; i++)
947 if (cx8800_ctls[i].v.id == ctl->id)
952 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
954 case V4L2_CID_AUDIO_BALANCE:
955 ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
956 : (0x7f - (value & 0x7f));
958 case V4L2_CID_AUDIO_VOLUME:
959 ctl->value = 0x3f - (value & 0x3f);
962 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
965 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
966 ctl->id, c->v.name, ctl->value, c->reg,
967 value,c->mask, c->sreg ? " [shadowed]" : "");
971 /* static int set_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
972 static int set_control(struct cx88_core *core, struct v4l2_control *ctl)
974 /* struct cx88_core *core = dev->core; */
975 struct cx88_ctrl *c = NULL;
978 for (i = 0; i < CX8800_CTLS; i++) {
979 if (cx8800_ctls[i].v.id == ctl->id) {
986 if (ctl->value < c->v.minimum)
987 ctl->value = c->v.minimum;
988 if (ctl->value > c->v.maximum)
989 ctl->value = c->v.maximum;
992 case V4L2_CID_AUDIO_BALANCE:
993 value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
995 case V4L2_CID_AUDIO_VOLUME:
996 value = 0x3f - (ctl->value & 0x3f);
998 case V4L2_CID_SATURATION:
999 /* special v_sat handling */
1001 value = ((ctl->value - c->off) << c->shift) & c->mask;
1003 if (core->tvnorm->id & V4L2_STD_SECAM) {
1004 /* For SECAM, both U and V sat should be equal */
1005 value=value<<8|value;
1007 /* Keeps U Saturation proportional to V Sat */
1008 value=(value*0x5a)/0x7f<<8|value;
1013 value = ((ctl->value - c->off) << c->shift) & c->mask;
1016 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
1017 ctl->id, c->v.name, ctl->value, c->reg, value,
1018 mask, c->sreg ? " [shadowed]" : "");
1020 cx_sandor(c->sreg, c->reg, mask, value);
1022 cx_andor(c->reg, mask, value);
1027 static void init_controls(struct cx88_core *core)
1029 struct v4l2_control ctrl;
1032 for (i = 0; i < CX8800_CTLS; i++) {
1033 ctrl.id=cx8800_ctls[i].v.id;
1034 ctrl.value=cx8800_ctls[i].v.default_value;
1035 set_control(core, &ctrl);
1039 /* ------------------------------------------------------------------ */
1041 static int cx8800_g_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1042 struct v4l2_format *f)
1045 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1046 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
1047 f->fmt.pix.width = fh->width;
1048 f->fmt.pix.height = fh->height;
1049 f->fmt.pix.field = fh->vidq.field;
1050 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1051 f->fmt.pix.bytesperline =
1052 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1053 f->fmt.pix.sizeimage =
1054 f->fmt.pix.height * f->fmt.pix.bytesperline;
1056 case V4L2_BUF_TYPE_VBI_CAPTURE:
1057 cx8800_vbi_fmt(dev, f);
1064 static int cx8800_try_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1065 struct v4l2_format *f)
1067 struct cx88_core *core = dev->core;
1070 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1072 struct cx8800_fmt *fmt;
1073 enum v4l2_field field;
1074 unsigned int maxw, maxh;
1076 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1080 field = f->fmt.pix.field;
1081 maxw = norm_maxw(core->tvnorm);
1082 maxh = norm_maxh(core->tvnorm);
1084 if (V4L2_FIELD_ANY == field) {
1085 field = (f->fmt.pix.height > maxh/2)
1086 ? V4L2_FIELD_INTERLACED
1087 : V4L2_FIELD_BOTTOM;
1091 case V4L2_FIELD_TOP:
1092 case V4L2_FIELD_BOTTOM:
1095 case V4L2_FIELD_INTERLACED:
1101 f->fmt.pix.field = field;
1102 if (f->fmt.pix.height < 32)
1103 f->fmt.pix.height = 32;
1104 if (f->fmt.pix.height > maxh)
1105 f->fmt.pix.height = maxh;
1106 if (f->fmt.pix.width < 48)
1107 f->fmt.pix.width = 48;
1108 if (f->fmt.pix.width > maxw)
1109 f->fmt.pix.width = maxw;
1110 f->fmt.pix.width &= ~0x03;
1111 f->fmt.pix.bytesperline =
1112 (f->fmt.pix.width * fmt->depth) >> 3;
1113 f->fmt.pix.sizeimage =
1114 f->fmt.pix.height * f->fmt.pix.bytesperline;
1118 case V4L2_BUF_TYPE_VBI_CAPTURE:
1119 cx8800_vbi_fmt(dev, f);
1126 static int cx8800_s_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1127 struct v4l2_format *f)
1132 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1133 err = cx8800_try_fmt(dev,fh,f);
1137 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1138 fh->width = f->fmt.pix.width;
1139 fh->height = f->fmt.pix.height;
1140 fh->vidq.field = f->fmt.pix.field;
1142 case V4L2_BUF_TYPE_VBI_CAPTURE:
1143 cx8800_vbi_fmt(dev, f);
1151 * This function is _not_ called directly, but from
1152 * video_generic_ioctl (and maybe others). userspace
1153 * copying is done already, arg is a kernel pointer.
1155 static int video_do_ioctl(struct inode *inode, struct file *file,
1156 unsigned int cmd, void *arg)
1158 struct cx8800_fh *fh = file->private_data;
1159 struct cx8800_dev *dev = fh->dev;
1160 struct cx88_core *core = dev->core;
1163 if (video_debug > 1)
1164 v4l_print_ioctl(core->name,cmd);
1167 /* --- capabilities ------------------------------------------ */
1168 case VIDIOC_QUERYCAP:
1170 struct v4l2_capability *cap = arg;
1172 memset(cap,0,sizeof(*cap));
1173 strcpy(cap->driver, "cx8800");
1174 strlcpy(cap->card, cx88_boards[core->board].name,
1176 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1177 cap->version = CX88_VERSION_CODE;
1179 V4L2_CAP_VIDEO_CAPTURE |
1180 V4L2_CAP_READWRITE |
1181 V4L2_CAP_STREAMING |
1182 V4L2_CAP_VBI_CAPTURE |
1183 V4L2_CAP_VIDEO_OVERLAY |
1185 if (UNSET != core->tuner_type)
1186 cap->capabilities |= V4L2_CAP_TUNER;
1190 /* --- capture ioctls ---------------------------------------- */
1191 case VIDIOC_ENUM_FMT:
1193 struct v4l2_fmtdesc *f = arg;
1194 enum v4l2_buf_type type;
1200 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1201 if (index >= ARRAY_SIZE(formats))
1203 memset(f,0,sizeof(*f));
1206 strlcpy(f->description,formats[index].name,sizeof(f->description));
1207 f->pixelformat = formats[index].fourcc;
1216 struct v4l2_format *f = arg;
1217 return cx8800_g_fmt(dev,fh,f);
1221 struct v4l2_format *f = arg;
1222 return cx8800_s_fmt(dev,fh,f);
1224 case VIDIOC_TRY_FMT:
1226 struct v4l2_format *f = arg;
1227 return cx8800_try_fmt(dev,fh,f);
1230 /* --- streaming capture ------------------------------------- */
1233 struct video_mbuf *mbuf = arg;
1234 struct videobuf_queue *q;
1235 struct v4l2_requestbuffers req;
1239 memset(&req,0,sizeof(req));
1242 req.memory = V4L2_MEMORY_MMAP;
1243 err = videobuf_reqbufs(q,&req);
1246 memset(mbuf,0,sizeof(*mbuf));
1247 mbuf->frames = req.count;
1249 for (i = 0; i < mbuf->frames; i++) {
1250 mbuf->offsets[i] = q->bufs[i]->boff;
1251 mbuf->size += q->bufs[i]->bsize;
1256 case VIDIOC_REQBUFS:
1257 return videobuf_reqbufs(get_queue(fh), arg);
1259 case VIDIOC_QUERYBUF:
1260 return videobuf_querybuf(get_queue(fh), arg);
1263 return videobuf_qbuf(get_queue(fh), arg);
1266 return videobuf_dqbuf(get_queue(fh), arg,
1267 file->f_flags & O_NONBLOCK);
1269 case VIDIOC_STREAMON:
1271 int res = get_ressource(fh);
1273 if (!res_get(dev,fh,res))
1275 return videobuf_streamon(get_queue(fh));
1277 case VIDIOC_STREAMOFF:
1279 int res = get_ressource(fh);
1281 err = videobuf_streamoff(get_queue(fh));
1284 res_free(dev,fh,res);
1288 return cx88_do_ioctl( inode, file, fh->radio, core, cmd, arg, video_do_ioctl );
1293 int cx88_do_ioctl(struct inode *inode, struct file *file, int radio,
1294 struct cx88_core *core, unsigned int cmd, void *arg, v4l2_kioctl driver_ioctl)
1299 if (video_debug > 1) {
1300 if (_IOC_DIR(cmd) & _IOC_WRITE)
1301 v4l_printk_ioctl_arg("cx88(w)",cmd, arg);
1302 else if (!_IOC_DIR(cmd) & _IOC_READ) {
1303 v4l_print_ioctl("cx88", cmd);
1306 v4l_print_ioctl(core->name,cmd);
1311 /* ---------- tv norms ---------- */
1312 case VIDIOC_ENUMSTD:
1314 struct v4l2_standard *e = arg;
1318 if (i >= ARRAY_SIZE(tvnorms))
1320 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1321 tvnorms[e->index].name);
1329 v4l2_std_id *id = arg;
1331 *id = core->tvnorm->id;
1336 v4l2_std_id *id = arg;
1339 for(i = 0; i < ARRAY_SIZE(tvnorms); i++)
1340 if (*id & tvnorms[i].id)
1342 if (i == ARRAY_SIZE(tvnorms))
1345 mutex_lock(&core->lock);
1346 cx88_set_tvnorm(core,&tvnorms[i]);
1347 mutex_unlock(&core->lock);
1351 /* ------ input switching ---------- */
1352 case VIDIOC_ENUMINPUT:
1354 static const char *iname[] = {
1355 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1356 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1357 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1358 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1359 [ CX88_VMUX_SVIDEO ] = "S-Video",
1360 [ CX88_VMUX_TELEVISION ] = "Television",
1361 [ CX88_VMUX_CABLE ] = "Cable TV",
1362 [ CX88_VMUX_DVB ] = "DVB",
1363 [ CX88_VMUX_DEBUG ] = "for debug only",
1365 struct v4l2_input *i = arg;
1371 if (0 == INPUT(n)->type)
1373 memset(i,0,sizeof(*i));
1375 i->type = V4L2_INPUT_TYPE_CAMERA;
1376 strcpy(i->name,iname[INPUT(n)->type]);
1377 if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
1378 (CX88_VMUX_CABLE == INPUT(n)->type))
1379 i->type = V4L2_INPUT_TYPE_TUNER;
1380 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1381 i->std |= tvnorms[n].id;
1384 case VIDIOC_G_INPUT:
1386 unsigned int *i = arg;
1391 case VIDIOC_S_INPUT:
1393 unsigned int *i = arg;
1397 mutex_lock(&core->lock);
1398 cx88_newstation(core);
1400 mutex_unlock(&core->lock);
1406 /* --- controls ---------------------------------------------- */
1407 case VIDIOC_QUERYCTRL:
1409 struct v4l2_queryctrl *c = arg;
1411 return cx88_queryctrl(c);
1414 return get_control(core,arg);
1416 return set_control(core,arg);
1418 /* --- tuner ioctls ------------------------------------------ */
1419 case VIDIOC_G_TUNER:
1421 struct v4l2_tuner *t = arg;
1424 if (UNSET == core->tuner_type)
1429 memset(t,0,sizeof(*t));
1430 strcpy(t->name, "Television");
1431 t->type = V4L2_TUNER_ANALOG_TV;
1432 t->capability = V4L2_TUNER_CAP_NORM;
1433 t->rangehigh = 0xffffffffUL;
1435 cx88_get_stereo(core ,t);
1436 reg = cx_read(MO_DEVICE_STATUS);
1437 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1440 case VIDIOC_S_TUNER:
1442 struct v4l2_tuner *t = arg;
1444 if (UNSET == core->tuner_type)
1448 cx88_set_stereo(core, t->audmode, 1);
1451 case VIDIOC_G_FREQUENCY:
1453 struct v4l2_frequency *f = arg;
1455 memset(f,0,sizeof(*f));
1457 if (UNSET == core->tuner_type)
1460 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1461 f->type = radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1462 f->frequency = core->freq;
1464 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1468 case VIDIOC_S_FREQUENCY:
1470 struct v4l2_frequency *f = arg;
1472 if (UNSET == core->tuner_type)
1476 if (0 == radio && f->type != V4L2_TUNER_ANALOG_TV)
1478 if (1 == radio && f->type != V4L2_TUNER_RADIO)
1480 mutex_lock(&core->lock);
1481 core->freq = f->frequency;
1482 cx88_newstation(core);
1483 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1485 /* When changing channels it is required to reset TVAUDIO */
1487 cx88_set_tvaudio(core);
1489 mutex_unlock(&core->lock);
1494 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1500 static int video_ioctl(struct inode *inode, struct file *file,
1501 unsigned int cmd, unsigned long arg)
1505 retval=video_usercopy(inode, file, cmd, arg, video_do_ioctl);
1507 if (video_debug > 1) {
1509 v4l_print_ioctl("cx88(err)", cmd);
1510 printk(KERN_DEBUG "cx88(err): errcode=%d\n",retval);
1511 } else if (_IOC_DIR(cmd) & _IOC_READ)
1512 v4l_printk_ioctl_arg("cx88(r)",cmd, (void *)arg);
1518 /* ----------------------------------------------------------- */
1520 static int radio_do_ioctl(struct inode *inode, struct file *file,
1521 unsigned int cmd, void *arg)
1523 struct cx8800_fh *fh = file->private_data;
1524 struct cx8800_dev *dev = fh->dev;
1525 struct cx88_core *core = dev->core;
1527 if (video_debug > 1)
1528 v4l_print_ioctl(core->name,cmd);
1531 case VIDIOC_QUERYCAP:
1533 struct v4l2_capability *cap = arg;
1535 memset(cap,0,sizeof(*cap));
1536 strcpy(cap->driver, "cx8800");
1537 strlcpy(cap->card, cx88_boards[core->board].name,
1539 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1540 cap->version = CX88_VERSION_CODE;
1541 cap->capabilities = V4L2_CAP_TUNER;
1544 case VIDIOC_G_TUNER:
1546 struct v4l2_tuner *t = arg;
1551 memset(t,0,sizeof(*t));
1552 strcpy(t->name, "Radio");
1553 t->type = V4L2_TUNER_RADIO;
1555 cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
1558 case VIDIOC_ENUMINPUT:
1560 struct v4l2_input *i = arg;
1564 strcpy(i->name,"Radio");
1565 i->type = V4L2_INPUT_TYPE_TUNER;
1568 case VIDIOC_G_INPUT:
1574 case VIDIOC_G_AUDIO:
1576 struct v4l2_audio *a = arg;
1578 memset(a,0,sizeof(*a));
1579 strcpy(a->name,"Radio");
1584 v4l2_std_id *id = arg;
1591 struct video_tuner *v = arg;
1593 if (v->tuner) /* Only tuner 0 */
1596 cx88_call_i2c_clients(core,VIDIOCSTUNER,v);
1600 case VIDIOC_S_TUNER:
1602 struct v4l2_tuner *t = arg;
1607 cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1612 case VIDIOC_S_AUDIO:
1613 case VIDIOC_S_INPUT:
1617 case VIDIOC_QUERYCTRL:
1619 struct v4l2_queryctrl *c = arg;
1622 if (c->id < V4L2_CID_BASE ||
1623 c->id >= V4L2_CID_LASTP1)
1625 if (c->id == V4L2_CID_AUDIO_MUTE) {
1626 for (i = 0; i < CX8800_CTLS; i++)
1627 if (cx8800_ctls[i].v.id == c->id)
1629 *c = cx8800_ctls[i].v;
1638 case VIDIOC_G_FREQUENCY:
1639 case VIDIOC_S_FREQUENCY:
1640 return video_do_ioctl(inode,file,cmd,arg);
1643 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1649 static int radio_ioctl(struct inode *inode, struct file *file,
1650 unsigned int cmd, unsigned long arg)
1652 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
1655 /* ----------------------------------------------------------- */
1657 static void cx8800_vid_timeout(unsigned long data)
1659 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1660 struct cx88_core *core = dev->core;
1661 struct cx88_dmaqueue *q = &dev->vidq;
1662 struct cx88_buffer *buf;
1663 unsigned long flags;
1665 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1667 cx_clear(MO_VID_DMACNTRL, 0x11);
1668 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1670 spin_lock_irqsave(&dev->slock,flags);
1671 while (!list_empty(&q->active)) {
1672 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1673 list_del(&buf->vb.queue);
1674 buf->vb.state = STATE_ERROR;
1675 wake_up(&buf->vb.done);
1676 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1677 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1679 restart_video_queue(dev,q);
1680 spin_unlock_irqrestore(&dev->slock,flags);
1683 static char *cx88_vid_irqs[32] = {
1684 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1685 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1686 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1687 "y_sync", "u_sync", "v_sync", "vbi_sync",
1688 "opc_err", "par_err", "rip_err", "pci_abort",
1691 static void cx8800_vid_irq(struct cx8800_dev *dev)
1693 struct cx88_core *core = dev->core;
1694 u32 status, mask, count;
1696 status = cx_read(MO_VID_INTSTAT);
1697 mask = cx_read(MO_VID_INTMSK);
1698 if (0 == (status & mask))
1700 cx_write(MO_VID_INTSTAT, status);
1701 if (irq_debug || (status & mask & ~0xff))
1702 cx88_print_irqbits(core->name, "irq vid",
1703 cx88_vid_irqs, status, mask);
1705 /* risc op code error */
1706 if (status & (1 << 16)) {
1707 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1708 cx_clear(MO_VID_DMACNTRL, 0x11);
1709 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1710 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1714 if (status & 0x01) {
1715 spin_lock(&dev->slock);
1716 count = cx_read(MO_VIDY_GPCNT);
1717 cx88_wakeup(core, &dev->vidq, count);
1718 spin_unlock(&dev->slock);
1722 if (status & 0x08) {
1723 spin_lock(&dev->slock);
1724 count = cx_read(MO_VBI_GPCNT);
1725 cx88_wakeup(core, &dev->vbiq, count);
1726 spin_unlock(&dev->slock);
1730 if (status & 0x10) {
1731 dprintk(2,"stopper video\n");
1732 spin_lock(&dev->slock);
1733 restart_video_queue(dev,&dev->vidq);
1734 spin_unlock(&dev->slock);
1738 if (status & 0x80) {
1739 dprintk(2,"stopper vbi\n");
1740 spin_lock(&dev->slock);
1741 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1742 spin_unlock(&dev->slock);
1746 static irqreturn_t cx8800_irq(int irq, void *dev_id, struct pt_regs *regs)
1748 struct cx8800_dev *dev = dev_id;
1749 struct cx88_core *core = dev->core;
1751 int loop, handled = 0;
1753 for (loop = 0; loop < 10; loop++) {
1754 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
1757 cx_write(MO_PCI_INTSTAT, status);
1760 if (status & core->pci_irqmask)
1761 cx88_core_irq(core,status);
1763 cx8800_vid_irq(dev);
1766 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1768 cx_write(MO_PCI_INTMSK,0);
1772 return IRQ_RETVAL(handled);
1775 /* ----------------------------------------------------------- */
1776 /* exported stuff */
1778 static struct file_operations video_fops =
1780 .owner = THIS_MODULE,
1782 .release = video_release,
1786 .ioctl = video_ioctl,
1787 .compat_ioctl = v4l_compat_ioctl32,
1788 .llseek = no_llseek,
1791 static struct video_device cx8800_video_template =
1793 .name = "cx8800-video",
1794 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1796 .fops = &video_fops,
1800 static struct video_device cx8800_vbi_template =
1802 .name = "cx8800-vbi",
1803 .type = VID_TYPE_TELETEXT|VID_TYPE_TUNER,
1805 .fops = &video_fops,
1809 static struct file_operations radio_fops =
1811 .owner = THIS_MODULE,
1813 .release = video_release,
1814 .ioctl = radio_ioctl,
1815 .compat_ioctl = v4l_compat_ioctl32,
1816 .llseek = no_llseek,
1819 static struct video_device cx8800_radio_template =
1821 .name = "cx8800-radio",
1822 .type = VID_TYPE_TUNER,
1824 .fops = &radio_fops,
1828 /* ----------------------------------------------------------- */
1830 static void cx8800_unregister_video(struct cx8800_dev *dev)
1832 if (dev->radio_dev) {
1833 if (-1 != dev->radio_dev->minor)
1834 video_unregister_device(dev->radio_dev);
1836 video_device_release(dev->radio_dev);
1837 dev->radio_dev = NULL;
1840 if (-1 != dev->vbi_dev->minor)
1841 video_unregister_device(dev->vbi_dev);
1843 video_device_release(dev->vbi_dev);
1844 dev->vbi_dev = NULL;
1846 if (dev->video_dev) {
1847 if (-1 != dev->video_dev->minor)
1848 video_unregister_device(dev->video_dev);
1850 video_device_release(dev->video_dev);
1851 dev->video_dev = NULL;
1855 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1856 const struct pci_device_id *pci_id)
1858 struct cx8800_dev *dev;
1859 struct cx88_core *core;
1862 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1868 if (pci_enable_device(pci_dev)) {
1872 core = cx88_core_get(dev->pci);
1879 /* print pci info */
1880 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
1881 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1882 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1883 "latency: %d, mmio: 0x%llx\n", core->name,
1884 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1885 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
1887 pci_set_master(pci_dev);
1888 if (!pci_dma_supported(pci_dev,0xffffffff)) {
1889 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1894 /* initialize driver struct */
1895 spin_lock_init(&dev->slock);
1896 core->tvnorm = tvnorms;
1898 /* init video dma queues */
1899 INIT_LIST_HEAD(&dev->vidq.active);
1900 INIT_LIST_HEAD(&dev->vidq.queued);
1901 dev->vidq.timeout.function = cx8800_vid_timeout;
1902 dev->vidq.timeout.data = (unsigned long)dev;
1903 init_timer(&dev->vidq.timeout);
1904 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1905 MO_VID_DMACNTRL,0x11,0x00);
1907 /* init vbi dma queues */
1908 INIT_LIST_HEAD(&dev->vbiq.active);
1909 INIT_LIST_HEAD(&dev->vbiq.queued);
1910 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1911 dev->vbiq.timeout.data = (unsigned long)dev;
1912 init_timer(&dev->vbiq.timeout);
1913 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1914 MO_VID_DMACNTRL,0x88,0x00);
1917 err = request_irq(pci_dev->irq, cx8800_irq,
1918 SA_SHIRQ | SA_INTERRUPT, core->name, dev);
1920 printk(KERN_ERR "%s: can't get IRQ %d\n",
1921 core->name,pci_dev->irq);
1924 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1926 /* load and configure helper modules */
1927 if (TUNER_ABSENT != core->tuner_type)
1928 request_module("tuner");
1930 /* register v4l devices */
1931 dev->video_dev = cx88_vdev_init(core,dev->pci,
1932 &cx8800_video_template,"video");
1933 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1934 video_nr[core->nr]);
1936 printk(KERN_INFO "%s: can't register video device\n",
1940 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1941 core->name,dev->video_dev->minor & 0x1f);
1943 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1944 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1947 printk(KERN_INFO "%s/0: can't register vbi device\n",
1951 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1952 core->name,dev->vbi_dev->minor & 0x1f);
1954 if (core->has_radio) {
1955 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1956 &cx8800_radio_template,"radio");
1957 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1958 radio_nr[core->nr]);
1960 printk(KERN_INFO "%s/0: can't register radio device\n",
1964 printk(KERN_INFO "%s/0: registered device radio%d\n",
1965 core->name,dev->radio_dev->minor & 0x1f);
1968 /* everything worked */
1969 list_add_tail(&dev->devlist,&cx8800_devlist);
1970 pci_set_drvdata(pci_dev,dev);
1972 /* initial device configuration */
1973 mutex_lock(&core->lock);
1974 cx88_set_tvnorm(core,tvnorms);
1975 init_controls(core);
1977 mutex_unlock(&core->lock);
1979 /* start tvaudio thread */
1980 if (core->tuner_type != TUNER_ABSENT)
1981 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
1985 cx8800_unregister_video(dev);
1986 free_irq(pci_dev->irq, dev);
1988 cx88_core_put(core,dev->pci);
1994 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1996 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1997 struct cx88_core *core = dev->core;
2000 if (core->kthread) {
2001 kthread_stop(core->kthread);
2002 core->kthread = NULL;
2005 cx88_shutdown(core); /* FIXME */
2006 pci_disable_device(pci_dev);
2008 /* unregister stuff */
2010 free_irq(pci_dev->irq, dev);
2011 cx8800_unregister_video(dev);
2012 pci_set_drvdata(pci_dev, NULL);
2015 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
2016 list_del(&dev->devlist);
2017 cx88_core_put(core,dev->pci);
2021 static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
2023 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2024 struct cx88_core *core = dev->core;
2026 /* stop video+vbi capture */
2027 spin_lock(&dev->slock);
2028 if (!list_empty(&dev->vidq.active)) {
2029 printk("%s: suspend video\n", core->name);
2030 stop_video_dma(dev);
2031 del_timer(&dev->vidq.timeout);
2033 if (!list_empty(&dev->vbiq.active)) {
2034 printk("%s: suspend vbi\n", core->name);
2035 cx8800_stop_vbi_dma(dev);
2036 del_timer(&dev->vbiq.timeout);
2038 spin_unlock(&dev->slock);
2040 /* FIXME -- shutdown device */
2041 cx88_shutdown(core);
2043 pci_save_state(pci_dev);
2044 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
2045 pci_disable_device(pci_dev);
2046 dev->state.disabled = 1;
2051 static int cx8800_resume(struct pci_dev *pci_dev)
2053 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2054 struct cx88_core *core = dev->core;
2057 if (dev->state.disabled) {
2058 err=pci_enable_device(pci_dev);
2060 printk(KERN_ERR "%s: can't enable device\n",
2065 dev->state.disabled = 0;
2067 err= pci_set_power_state(pci_dev, PCI_D0);
2069 printk(KERN_ERR "%s: can't enable device\n",
2072 pci_disable_device(pci_dev);
2073 dev->state.disabled = 1;
2077 pci_restore_state(pci_dev);
2079 /* FIXME: re-initialize hardware */
2082 /* restart video+vbi capture */
2083 spin_lock(&dev->slock);
2084 if (!list_empty(&dev->vidq.active)) {
2085 printk("%s: resume video\n", core->name);
2086 restart_video_queue(dev,&dev->vidq);
2088 if (!list_empty(&dev->vbiq.active)) {
2089 printk("%s: resume vbi\n", core->name);
2090 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2092 spin_unlock(&dev->slock);
2097 /* ----------------------------------------------------------- */
2099 static struct pci_device_id cx8800_pci_tbl[] = {
2103 .subvendor = PCI_ANY_ID,
2104 .subdevice = PCI_ANY_ID,
2106 /* --- end of list --- */
2109 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2111 static struct pci_driver cx8800_pci_driver = {
2113 .id_table = cx8800_pci_tbl,
2114 .probe = cx8800_initdev,
2115 .remove = __devexit_p(cx8800_finidev),
2117 .suspend = cx8800_suspend,
2118 .resume = cx8800_resume,
2121 static int cx8800_init(void)
2123 printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
2124 (CX88_VERSION_CODE >> 16) & 0xff,
2125 (CX88_VERSION_CODE >> 8) & 0xff,
2126 CX88_VERSION_CODE & 0xff);
2128 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2129 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2131 return pci_register_driver(&cx8800_pci_driver);
2134 static void cx8800_fini(void)
2136 pci_unregister_driver(&cx8800_pci_driver);
2139 module_init(cx8800_init);
2140 module_exit(cx8800_fini);
2142 EXPORT_SYMBOL(cx88_do_ioctl);
2144 /* ----------------------------------------------------------- */
2149 * 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