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>
37 /* Include V4L1 specific functions. Should be removed soon */
38 #include <linux/videodev.h>
40 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
41 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
42 MODULE_LICENSE("GPL");
44 /* ------------------------------------------------------------------ */
46 static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
47 static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
48 static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
50 module_param_array(video_nr, int, NULL, 0444);
51 module_param_array(vbi_nr, int, NULL, 0444);
52 module_param_array(radio_nr, int, NULL, 0444);
54 MODULE_PARM_DESC(video_nr,"video device numbers");
55 MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
56 MODULE_PARM_DESC(radio_nr,"radio device numbers");
58 static unsigned int video_debug = 0;
59 module_param(video_debug,int,0644);
60 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
62 static unsigned int irq_debug = 0;
63 module_param(irq_debug,int,0644);
64 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
66 static unsigned int vid_limit = 16;
67 module_param(vid_limit,int,0644);
68 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
70 #define dprintk(level,fmt, arg...) if (video_debug >= level) \
71 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
73 /* ------------------------------------------------------------------ */
75 static LIST_HEAD(cx8800_devlist);
77 /* ------------------------------------------------------------------- */
80 static struct cx88_tvnorm tvnorms[] = {
83 .id = V4L2_STD_NTSC_M,
84 .cxiformat = VideoFormatNTSC,
85 .cxoformat = 0x181f0008,
88 .id = V4L2_STD_NTSC_M_JP,
89 .cxiformat = VideoFormatNTSCJapan,
90 .cxoformat = 0x181f0008,
93 .id = V4L2_STD_PAL_BG,
94 .cxiformat = VideoFormatPAL,
95 .cxoformat = 0x181f0008,
98 .id = V4L2_STD_PAL_DK,
99 .cxiformat = VideoFormatPAL,
100 .cxoformat = 0x181f0008,
103 .id = V4L2_STD_PAL_I,
104 .cxiformat = VideoFormatPAL,
105 .cxoformat = 0x181f0008,
108 .id = V4L2_STD_PAL_M,
109 .cxiformat = VideoFormatPALM,
110 .cxoformat = 0x1c1f0008,
113 .id = V4L2_STD_PAL_N,
114 .cxiformat = VideoFormatPALN,
115 .cxoformat = 0x1c1f0008,
118 .id = V4L2_STD_PAL_Nc,
119 .cxiformat = VideoFormatPALNC,
120 .cxoformat = 0x1c1f0008,
123 .id = V4L2_STD_PAL_60,
124 .cxiformat = VideoFormatPAL60,
125 .cxoformat = 0x181f0008,
128 .id = V4L2_STD_SECAM_L,
129 .cxiformat = VideoFormatSECAM,
130 .cxoformat = 0x181f0008,
133 .id = V4L2_STD_SECAM_DK,
134 .cxiformat = VideoFormatSECAM,
135 .cxoformat = 0x181f0008,
139 static struct cx8800_fmt formats[] = {
141 .name = "8 bpp, gray",
142 .fourcc = V4L2_PIX_FMT_GREY,
143 .cxformat = ColorFormatY8,
145 .flags = FORMAT_FLAGS_PACKED,
147 .name = "15 bpp RGB, le",
148 .fourcc = V4L2_PIX_FMT_RGB555,
149 .cxformat = ColorFormatRGB15,
151 .flags = FORMAT_FLAGS_PACKED,
153 .name = "15 bpp RGB, be",
154 .fourcc = V4L2_PIX_FMT_RGB555X,
155 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
157 .flags = FORMAT_FLAGS_PACKED,
159 .name = "16 bpp RGB, le",
160 .fourcc = V4L2_PIX_FMT_RGB565,
161 .cxformat = ColorFormatRGB16,
163 .flags = FORMAT_FLAGS_PACKED,
165 .name = "16 bpp RGB, be",
166 .fourcc = V4L2_PIX_FMT_RGB565X,
167 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
169 .flags = FORMAT_FLAGS_PACKED,
171 .name = "24 bpp RGB, le",
172 .fourcc = V4L2_PIX_FMT_BGR24,
173 .cxformat = ColorFormatRGB24,
175 .flags = FORMAT_FLAGS_PACKED,
177 .name = "32 bpp RGB, le",
178 .fourcc = V4L2_PIX_FMT_BGR32,
179 .cxformat = ColorFormatRGB32,
181 .flags = FORMAT_FLAGS_PACKED,
183 .name = "32 bpp RGB, be",
184 .fourcc = V4L2_PIX_FMT_RGB32,
185 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
187 .flags = FORMAT_FLAGS_PACKED,
189 .name = "4:2:2, packed, YUYV",
190 .fourcc = V4L2_PIX_FMT_YUYV,
191 .cxformat = ColorFormatYUY2,
193 .flags = FORMAT_FLAGS_PACKED,
195 .name = "4:2:2, packed, UYVY",
196 .fourcc = V4L2_PIX_FMT_UYVY,
197 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
199 .flags = FORMAT_FLAGS_PACKED,
203 static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
207 for (i = 0; i < ARRAY_SIZE(formats); i++)
208 if (formats[i].fourcc == fourcc)
213 /* ------------------------------------------------------------------- */
215 static const struct v4l2_queryctrl no_ctl = {
217 .flags = V4L2_CTRL_FLAG_DISABLED,
220 static struct cx88_ctrl cx8800_ctls[] = {
224 .id = V4L2_CID_BRIGHTNESS,
225 .name = "Brightness",
230 .type = V4L2_CTRL_TYPE_INTEGER,
233 .reg = MO_CONTR_BRIGHT,
238 .id = V4L2_CID_CONTRAST,
244 .type = V4L2_CTRL_TYPE_INTEGER,
247 .reg = MO_CONTR_BRIGHT,
258 .type = V4L2_CTRL_TYPE_INTEGER,
265 /* strictly, this only describes only U saturation.
266 * V saturation is handled specially through code.
269 .id = V4L2_CID_SATURATION,
270 .name = "Saturation",
275 .type = V4L2_CTRL_TYPE_INTEGER,
278 .reg = MO_UV_SATURATION,
284 .id = V4L2_CID_AUDIO_MUTE,
288 .type = V4L2_CTRL_TYPE_BOOLEAN,
291 .sreg = SHADOW_AUD_VOL_CTL,
296 .id = V4L2_CID_AUDIO_VOLUME,
302 .type = V4L2_CTRL_TYPE_INTEGER,
305 .sreg = SHADOW_AUD_VOL_CTL,
310 .id = V4L2_CID_AUDIO_BALANCE,
315 .default_value = 0x40,
316 .type = V4L2_CTRL_TYPE_INTEGER,
319 .sreg = SHADOW_AUD_BAL_CTL,
324 static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
326 /* ------------------------------------------------------------------- */
327 /* resource management */
329 static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
331 struct cx88_core *core = dev->core;
332 if (fh->resources & bit)
333 /* have it already allocated */
338 if (dev->resources & bit) {
339 /* no, someone else uses it */
343 /* it's free, grab it */
344 fh->resources |= bit;
345 dev->resources |= bit;
346 dprintk(1,"res: get %d\n",bit);
352 int res_check(struct cx8800_fh *fh, unsigned int bit)
354 return (fh->resources & bit);
358 int res_locked(struct cx8800_dev *dev, unsigned int bit)
360 return (dev->resources & bit);
364 void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
366 struct cx88_core *core = dev->core;
367 if ((fh->resources & bits) != bits)
371 fh->resources &= ~bits;
372 dev->resources &= ~bits;
373 dprintk(1,"res: put %d\n",bits);
377 /* ------------------------------------------------------------------ */
379 /* static int video_mux(struct cx8800_dev *dev, unsigned int input) */
380 static int video_mux(struct cx88_core *core, unsigned int input)
382 /* struct cx88_core *core = dev->core; */
384 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
385 input, INPUT(input)->vmux,
386 INPUT(input)->gpio0,INPUT(input)->gpio1,
387 INPUT(input)->gpio2,INPUT(input)->gpio3);
389 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input)->vmux << 14);
390 cx_write(MO_GP3_IO, INPUT(input)->gpio3);
391 cx_write(MO_GP0_IO, INPUT(input)->gpio0);
392 cx_write(MO_GP1_IO, INPUT(input)->gpio1);
393 cx_write(MO_GP2_IO, INPUT(input)->gpio2);
395 switch (INPUT(input)->type) {
396 case CX88_VMUX_SVIDEO:
397 cx_set(MO_AFECFG_IO, 0x00000001);
398 cx_set(MO_INPUT_FORMAT, 0x00010010);
399 cx_set(MO_FILTER_EVEN, 0x00002020);
400 cx_set(MO_FILTER_ODD, 0x00002020);
403 cx_clear(MO_AFECFG_IO, 0x00000001);
404 cx_clear(MO_INPUT_FORMAT, 0x00010010);
405 cx_clear(MO_FILTER_EVEN, 0x00002020);
406 cx_clear(MO_FILTER_ODD, 0x00002020);
412 /* ------------------------------------------------------------------ */
414 static int start_video_dma(struct cx8800_dev *dev,
415 struct cx88_dmaqueue *q,
416 struct cx88_buffer *buf)
418 struct cx88_core *core = dev->core;
420 /* setup fifo + format */
421 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
422 buf->bpl, buf->risc.dma);
423 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
424 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
427 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
431 cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
433 /* Enables corresponding bits at PCI_INT_STAT:
434 bits 0 to 4: video, audio, transport stream, VIP, Host
436 bits 8 and 9: DMA complete for: SRC, DST
437 bits 10 and 11: BERR signal asserted for RISC: RD, WR
438 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
440 cx_set(MO_VID_INTMSK, 0x0f0011);
443 cx_set(VID_CAPTURE_CONTROL,0x06);
446 cx_set(MO_DEV_CNTRL2, (1<<5));
447 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
452 static int stop_video_dma(struct cx8800_dev *dev)
454 struct cx88_core *core = dev->core;
457 cx_clear(MO_VID_DMACNTRL, 0x11);
459 /* disable capture */
460 cx_clear(VID_CAPTURE_CONTROL,0x06);
463 cx_clear(MO_PCI_INTMSK, 0x000001);
464 cx_clear(MO_VID_INTMSK, 0x0f0011);
468 static int restart_video_queue(struct cx8800_dev *dev,
469 struct cx88_dmaqueue *q)
471 struct cx88_core *core = dev->core;
472 struct cx88_buffer *buf, *prev;
473 struct list_head *item;
475 if (!list_empty(&q->active)) {
476 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
477 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
479 start_video_dma(dev, q, buf);
480 list_for_each(item,&q->active) {
481 buf = list_entry(item, struct cx88_buffer, vb.queue);
482 buf->count = q->count++;
484 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
490 if (list_empty(&q->queued))
492 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
494 list_del(&buf->vb.queue);
495 list_add_tail(&buf->vb.queue,&q->active);
496 start_video_dma(dev, q, buf);
497 buf->vb.state = STATE_ACTIVE;
498 buf->count = q->count++;
499 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
500 dprintk(2,"[%p/%d] restart_queue - first active\n",
503 } else if (prev->vb.width == buf->vb.width &&
504 prev->vb.height == buf->vb.height &&
505 prev->fmt == buf->fmt) {
506 list_del(&buf->vb.queue);
507 list_add_tail(&buf->vb.queue,&q->active);
508 buf->vb.state = STATE_ACTIVE;
509 buf->count = q->count++;
510 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
511 dprintk(2,"[%p/%d] restart_queue - move to active\n",
520 /* ------------------------------------------------------------------ */
523 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
525 struct cx8800_fh *fh = q->priv_data;
527 *size = fh->fmt->depth*fh->width*fh->height >> 3;
530 while (*size * *count > vid_limit * 1024 * 1024)
536 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
537 enum v4l2_field field)
539 struct cx8800_fh *fh = q->priv_data;
540 struct cx8800_dev *dev = fh->dev;
541 struct cx88_core *core = dev->core;
542 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
543 int rc, init_buffer = 0;
545 BUG_ON(NULL == fh->fmt);
546 if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
547 fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
549 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
550 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
553 if (buf->fmt != fh->fmt ||
554 buf->vb.width != fh->width ||
555 buf->vb.height != fh->height ||
556 buf->vb.field != field) {
558 buf->vb.width = fh->width;
559 buf->vb.height = fh->height;
560 buf->vb.field = field;
564 if (STATE_NEEDS_INIT == buf->vb.state) {
566 if (0 != (rc = videobuf_iolock(dev->pci,&buf->vb,NULL)))
571 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
572 switch (buf->vb.field) {
574 cx88_risc_buffer(dev->pci, &buf->risc,
575 buf->vb.dma.sglist, 0, UNSET,
576 buf->bpl, 0, buf->vb.height);
578 case V4L2_FIELD_BOTTOM:
579 cx88_risc_buffer(dev->pci, &buf->risc,
580 buf->vb.dma.sglist, UNSET, 0,
581 buf->bpl, 0, buf->vb.height);
583 case V4L2_FIELD_INTERLACED:
584 cx88_risc_buffer(dev->pci, &buf->risc,
585 buf->vb.dma.sglist, 0, buf->bpl,
587 buf->vb.height >> 1);
589 case V4L2_FIELD_SEQ_TB:
590 cx88_risc_buffer(dev->pci, &buf->risc,
592 0, buf->bpl * (buf->vb.height >> 1),
594 buf->vb.height >> 1);
596 case V4L2_FIELD_SEQ_BT:
597 cx88_risc_buffer(dev->pci, &buf->risc,
599 buf->bpl * (buf->vb.height >> 1), 0,
601 buf->vb.height >> 1);
607 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
609 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
610 (unsigned long)buf->risc.dma);
612 buf->vb.state = STATE_PREPARED;
616 cx88_free_buffer(dev->pci,buf);
621 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
623 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
624 struct cx88_buffer *prev;
625 struct cx8800_fh *fh = vq->priv_data;
626 struct cx8800_dev *dev = fh->dev;
627 struct cx88_core *core = dev->core;
628 struct cx88_dmaqueue *q = &dev->vidq;
630 /* add jump to stopper */
631 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
632 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
634 if (!list_empty(&q->queued)) {
635 list_add_tail(&buf->vb.queue,&q->queued);
636 buf->vb.state = STATE_QUEUED;
637 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
640 } else if (list_empty(&q->active)) {
641 list_add_tail(&buf->vb.queue,&q->active);
642 start_video_dma(dev, q, buf);
643 buf->vb.state = STATE_ACTIVE;
644 buf->count = q->count++;
645 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
646 dprintk(2,"[%p/%d] buffer_queue - first active\n",
650 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
651 if (prev->vb.width == buf->vb.width &&
652 prev->vb.height == buf->vb.height &&
653 prev->fmt == buf->fmt) {
654 list_add_tail(&buf->vb.queue,&q->active);
655 buf->vb.state = STATE_ACTIVE;
656 buf->count = q->count++;
657 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
658 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
662 list_add_tail(&buf->vb.queue,&q->queued);
663 buf->vb.state = STATE_QUEUED;
664 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
670 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
672 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
673 struct cx8800_fh *fh = q->priv_data;
675 cx88_free_buffer(fh->dev->pci,buf);
678 static struct videobuf_queue_ops cx8800_video_qops = {
679 .buf_setup = buffer_setup,
680 .buf_prepare = buffer_prepare,
681 .buf_queue = buffer_queue,
682 .buf_release = buffer_release,
685 /* ------------------------------------------------------------------ */
688 /* ------------------------------------------------------------------ */
690 static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
693 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
695 case V4L2_BUF_TYPE_VBI_CAPTURE:
703 static int get_ressource(struct cx8800_fh *fh)
706 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
707 return RESOURCE_VIDEO;
708 case V4L2_BUF_TYPE_VBI_CAPTURE:
716 static int video_open(struct inode *inode, struct file *file)
718 int minor = iminor(inode);
719 struct cx8800_dev *h,*dev = NULL;
720 struct cx88_core *core;
721 struct cx8800_fh *fh;
722 struct list_head *list;
723 enum v4l2_buf_type type = 0;
726 list_for_each(list,&cx8800_devlist) {
727 h = list_entry(list, struct cx8800_dev, devlist);
728 if (h->video_dev->minor == minor) {
730 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
732 if (h->vbi_dev->minor == minor) {
734 type = V4L2_BUF_TYPE_VBI_CAPTURE;
737 h->radio_dev->minor == minor) {
747 dprintk(1,"open minor=%d radio=%d type=%s\n",
748 minor,radio,v4l2_type_names[type]);
750 /* allocate + initialize per filehandle data */
751 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
754 memset(fh,0,sizeof(*fh));
755 file->private_data = fh;
761 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
763 videobuf_queue_init(&fh->vidq, &cx8800_video_qops,
764 dev->pci, &dev->slock,
765 V4L2_BUF_TYPE_VIDEO_CAPTURE,
766 V4L2_FIELD_INTERLACED,
767 sizeof(struct cx88_buffer),
769 videobuf_queue_init(&fh->vbiq, &cx8800_vbi_qops,
770 dev->pci, &dev->slock,
771 V4L2_BUF_TYPE_VBI_CAPTURE,
773 sizeof(struct cx88_buffer),
777 int board = core->board;
778 dprintk(1,"video_open: setting radio device\n");
779 cx_write(MO_GP3_IO, cx88_boards[board].radio.gpio3);
780 cx_write(MO_GP0_IO, cx88_boards[board].radio.gpio0);
781 cx_write(MO_GP1_IO, cx88_boards[board].radio.gpio1);
782 cx_write(MO_GP2_IO, cx88_boards[board].radio.gpio2);
783 core->tvaudio = WW_FM;
784 cx88_set_tvaudio(core);
785 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
786 cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
793 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
795 struct cx8800_fh *fh = file->private_data;
798 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
799 if (res_locked(fh->dev,RESOURCE_VIDEO))
801 return videobuf_read_one(&fh->vidq, data, count, ppos,
802 file->f_flags & O_NONBLOCK);
803 case V4L2_BUF_TYPE_VBI_CAPTURE:
804 if (!res_get(fh->dev,fh,RESOURCE_VBI))
806 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
807 file->f_flags & O_NONBLOCK);
815 video_poll(struct file *file, struct poll_table_struct *wait)
817 struct cx8800_fh *fh = file->private_data;
818 struct cx88_buffer *buf;
820 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
821 if (!res_get(fh->dev,fh,RESOURCE_VBI))
823 return videobuf_poll_stream(file, &fh->vbiq, wait);
826 if (res_check(fh,RESOURCE_VIDEO)) {
827 /* streaming capture */
828 if (list_empty(&fh->vidq.stream))
830 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
833 buf = (struct cx88_buffer*)fh->vidq.read_buf;
837 poll_wait(file, &buf->vb.done, wait);
838 if (buf->vb.state == STATE_DONE ||
839 buf->vb.state == STATE_ERROR)
840 return POLLIN|POLLRDNORM;
844 static int video_release(struct inode *inode, struct file *file)
846 struct cx8800_fh *fh = file->private_data;
847 struct cx8800_dev *dev = fh->dev;
849 /* turn off overlay */
850 if (res_check(fh, RESOURCE_OVERLAY)) {
852 res_free(dev,fh,RESOURCE_OVERLAY);
855 /* stop video capture */
856 if (res_check(fh, RESOURCE_VIDEO)) {
857 videobuf_queue_cancel(&fh->vidq);
858 res_free(dev,fh,RESOURCE_VIDEO);
860 if (fh->vidq.read_buf) {
861 buffer_release(&fh->vidq,fh->vidq.read_buf);
862 kfree(fh->vidq.read_buf);
865 /* stop vbi capture */
866 if (res_check(fh, RESOURCE_VBI)) {
867 if (fh->vbiq.streaming)
868 videobuf_streamoff(&fh->vbiq);
869 if (fh->vbiq.reading)
870 videobuf_read_stop(&fh->vbiq);
871 res_free(dev,fh,RESOURCE_VBI);
874 videobuf_mmap_free(&fh->vidq);
875 videobuf_mmap_free(&fh->vbiq);
876 file->private_data = NULL;
879 cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
885 video_mmap(struct file *file, struct vm_area_struct * vma)
887 struct cx8800_fh *fh = file->private_data;
889 return videobuf_mmap_mapper(get_queue(fh), vma);
892 /* ------------------------------------------------------------------ */
894 /* static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
895 static int get_control(struct cx88_core *core, struct v4l2_control *ctl)
897 /* struct cx88_core *core = dev->core; */
898 struct cx88_ctrl *c = NULL;
902 for (i = 0; i < CX8800_CTLS; i++)
903 if (cx8800_ctls[i].v.id == ctl->id)
908 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
910 case V4L2_CID_AUDIO_BALANCE:
911 ctl->value = (value & 0x40) ? (value & 0x3f) : (0x40 - (value & 0x3f));
913 case V4L2_CID_AUDIO_VOLUME:
914 ctl->value = 0x3f - (value & 0x3f);
917 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
923 /* static int set_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
924 static int set_control(struct cx88_core *core, struct v4l2_control *ctl)
926 /* struct cx88_core *core = dev->core; */
927 struct cx88_ctrl *c = NULL;
932 for (i = 0; i < CX8800_CTLS; i++)
933 if (cx8800_ctls[i].v.id == ctl->id)
938 if (ctl->value < c->v.minimum)
939 ctl->value = c->v.minimum;
940 if (ctl->value > c->v.maximum)
941 ctl->value = c->v.maximum;
943 case V4L2_CID_AUDIO_BALANCE:
944 value = (ctl->value < 0x40) ? (0x40 - ctl->value) : ctl->value;
946 case V4L2_CID_AUDIO_VOLUME:
947 value = 0x3f - (ctl->value & 0x3f);
949 case V4L2_CID_SATURATION:
950 /* special v_sat handling */
951 v_sat_value = ctl->value - (0x7f - 0x5a);
952 if (v_sat_value > 0xff)
954 if (v_sat_value < 0x00)
956 cx_andor(MO_UV_SATURATION, 0xff00, v_sat_value << 8);
957 /* fall through to default route for u_sat */
959 value = ((ctl->value - c->off) << c->shift) & c->mask;
962 dprintk(1,"set_control id=0x%X reg=0x%x val=0x%x%s\n",
963 ctl->id, c->reg, value, c->sreg ? " [shadowed]" : "");
965 cx_sandor(c->sreg, c->reg, c->mask, value);
967 cx_andor(c->reg, c->mask, value);
972 /* static void init_controls(struct cx8800_dev *dev) */
973 static void init_controls(struct cx88_core *core)
975 static struct v4l2_control mute = {
976 .id = V4L2_CID_AUDIO_MUTE,
979 static struct v4l2_control volume = {
980 .id = V4L2_CID_AUDIO_VOLUME,
983 static struct v4l2_control hue = {
987 static struct v4l2_control contrast = {
988 .id = V4L2_CID_CONTRAST,
991 static struct v4l2_control brightness = {
992 .id = V4L2_CID_BRIGHTNESS,
996 set_control(core,&mute);
997 set_control(core,&volume);
998 set_control(core,&hue);
999 set_control(core,&contrast);
1000 set_control(core,&brightness);
1003 /* ------------------------------------------------------------------ */
1005 static int cx8800_g_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1006 struct v4l2_format *f)
1009 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1010 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
1011 f->fmt.pix.width = fh->width;
1012 f->fmt.pix.height = fh->height;
1013 f->fmt.pix.field = fh->vidq.field;
1014 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1015 f->fmt.pix.bytesperline =
1016 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1017 f->fmt.pix.sizeimage =
1018 f->fmt.pix.height * f->fmt.pix.bytesperline;
1020 case V4L2_BUF_TYPE_VBI_CAPTURE:
1021 cx8800_vbi_fmt(dev, f);
1028 static int cx8800_try_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1029 struct v4l2_format *f)
1031 struct cx88_core *core = dev->core;
1034 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1036 struct cx8800_fmt *fmt;
1037 enum v4l2_field field;
1038 unsigned int maxw, maxh;
1040 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1044 field = f->fmt.pix.field;
1045 maxw = norm_maxw(core->tvnorm);
1046 maxh = norm_maxh(core->tvnorm);
1048 if (V4L2_FIELD_ANY == field) {
1049 field = (f->fmt.pix.height > maxh/2)
1050 ? V4L2_FIELD_INTERLACED
1051 : V4L2_FIELD_BOTTOM;
1055 case V4L2_FIELD_TOP:
1056 case V4L2_FIELD_BOTTOM:
1059 case V4L2_FIELD_INTERLACED:
1065 f->fmt.pix.field = field;
1066 if (f->fmt.pix.height < 32)
1067 f->fmt.pix.height = 32;
1068 if (f->fmt.pix.height > maxh)
1069 f->fmt.pix.height = maxh;
1070 if (f->fmt.pix.width < 48)
1071 f->fmt.pix.width = 48;
1072 if (f->fmt.pix.width > maxw)
1073 f->fmt.pix.width = maxw;
1074 f->fmt.pix.width &= ~0x03;
1075 f->fmt.pix.bytesperline =
1076 (f->fmt.pix.width * fmt->depth) >> 3;
1077 f->fmt.pix.sizeimage =
1078 f->fmt.pix.height * f->fmt.pix.bytesperline;
1082 case V4L2_BUF_TYPE_VBI_CAPTURE:
1083 cx8800_vbi_fmt(dev, f);
1090 static int cx8800_s_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1091 struct v4l2_format *f)
1096 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1097 err = cx8800_try_fmt(dev,fh,f);
1101 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1102 fh->width = f->fmt.pix.width;
1103 fh->height = f->fmt.pix.height;
1104 fh->vidq.field = f->fmt.pix.field;
1106 case V4L2_BUF_TYPE_VBI_CAPTURE:
1107 cx8800_vbi_fmt(dev, f);
1115 * This function is _not_ called directly, but from
1116 * video_generic_ioctl (and maybe others). userspace
1117 * copying is done already, arg is a kernel pointer.
1119 static int video_do_ioctl(struct inode *inode, struct file *file,
1120 unsigned int cmd, void *arg)
1122 struct cx8800_fh *fh = file->private_data;
1123 struct cx8800_dev *dev = fh->dev;
1124 struct cx88_core *core = dev->core;
1127 if (video_debug > 1)
1128 cx88_print_ioctl(core->name,cmd);
1131 /* --- capabilities ------------------------------------------ */
1132 case VIDIOC_QUERYCAP:
1134 struct v4l2_capability *cap = arg;
1136 memset(cap,0,sizeof(*cap));
1137 strcpy(cap->driver, "cx8800");
1138 strlcpy(cap->card, cx88_boards[core->board].name,
1140 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1141 cap->version = CX88_VERSION_CODE;
1143 V4L2_CAP_VIDEO_CAPTURE |
1144 V4L2_CAP_READWRITE |
1145 V4L2_CAP_STREAMING |
1146 V4L2_CAP_VBI_CAPTURE |
1147 V4L2_CAP_VIDEO_OVERLAY |
1149 if (UNSET != core->tuner_type)
1150 cap->capabilities |= V4L2_CAP_TUNER;
1154 /* --- capture ioctls ---------------------------------------- */
1155 case VIDIOC_ENUM_FMT:
1157 struct v4l2_fmtdesc *f = arg;
1158 enum v4l2_buf_type type;
1164 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1165 if (index >= ARRAY_SIZE(formats))
1167 memset(f,0,sizeof(*f));
1170 strlcpy(f->description,formats[index].name,sizeof(f->description));
1171 f->pixelformat = formats[index].fourcc;
1180 struct v4l2_format *f = arg;
1181 return cx8800_g_fmt(dev,fh,f);
1185 struct v4l2_format *f = arg;
1186 return cx8800_s_fmt(dev,fh,f);
1188 case VIDIOC_TRY_FMT:
1190 struct v4l2_format *f = arg;
1191 return cx8800_try_fmt(dev,fh,f);
1194 /* --- streaming capture ------------------------------------- */
1197 struct video_mbuf *mbuf = arg;
1198 struct videobuf_queue *q;
1199 struct v4l2_requestbuffers req;
1203 memset(&req,0,sizeof(req));
1206 req.memory = V4L2_MEMORY_MMAP;
1207 err = videobuf_reqbufs(q,&req);
1210 memset(mbuf,0,sizeof(*mbuf));
1211 mbuf->frames = req.count;
1213 for (i = 0; i < mbuf->frames; i++) {
1214 mbuf->offsets[i] = q->bufs[i]->boff;
1215 mbuf->size += q->bufs[i]->bsize;
1220 case VIDIOC_REQBUFS:
1221 return videobuf_reqbufs(get_queue(fh), arg);
1223 case VIDIOC_QUERYBUF:
1224 return videobuf_querybuf(get_queue(fh), arg);
1227 return videobuf_qbuf(get_queue(fh), arg);
1230 return videobuf_dqbuf(get_queue(fh), arg,
1231 file->f_flags & O_NONBLOCK);
1233 case VIDIOC_STREAMON:
1235 int res = get_ressource(fh);
1237 if (!res_get(dev,fh,res))
1239 return videobuf_streamon(get_queue(fh));
1241 case VIDIOC_STREAMOFF:
1243 int res = get_ressource(fh);
1245 err = videobuf_streamoff(get_queue(fh));
1248 res_free(dev,fh,res);
1252 return cx88_do_ioctl( inode, file, fh->radio, core, cmd, arg, video_do_ioctl );
1257 int cx88_do_ioctl(struct inode *inode, struct file *file, int radio,
1258 struct cx88_core *core, unsigned int cmd, void *arg, v4l2_kioctl driver_ioctl)
1262 dprintk( 1, "CORE IOCTL: 0x%x\n", cmd );
1263 if (video_debug > 1)
1264 cx88_print_ioctl(core->name,cmd);
1267 /* ---------- tv norms ---------- */
1268 case VIDIOC_ENUMSTD:
1270 struct v4l2_standard *e = arg;
1274 if (i >= ARRAY_SIZE(tvnorms))
1276 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1277 tvnorms[e->index].name);
1285 v4l2_std_id *id = arg;
1287 *id = core->tvnorm->id;
1292 v4l2_std_id *id = arg;
1295 for(i = 0; i < ARRAY_SIZE(tvnorms); i++)
1296 if (*id & tvnorms[i].id)
1298 if (i == ARRAY_SIZE(tvnorms))
1302 cx88_set_tvnorm(core,&tvnorms[i]);
1307 /* ------ input switching ---------- */
1308 case VIDIOC_ENUMINPUT:
1310 static const char *iname[] = {
1311 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1312 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1313 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1314 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1315 [ CX88_VMUX_SVIDEO ] = "S-Video",
1316 [ CX88_VMUX_TELEVISION ] = "Television",
1317 [ CX88_VMUX_CABLE ] = "Cable TV",
1318 [ CX88_VMUX_DVB ] = "DVB",
1319 [ CX88_VMUX_DEBUG ] = "for debug only",
1321 struct v4l2_input *i = arg;
1327 if (0 == INPUT(n)->type)
1329 memset(i,0,sizeof(*i));
1331 i->type = V4L2_INPUT_TYPE_CAMERA;
1332 strcpy(i->name,iname[INPUT(n)->type]);
1333 if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
1334 (CX88_VMUX_CABLE == INPUT(n)->type))
1335 i->type = V4L2_INPUT_TYPE_TUNER;
1336 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1337 i->std |= tvnorms[n].id;
1340 case VIDIOC_G_INPUT:
1342 unsigned int *i = arg;
1347 case VIDIOC_S_INPUT:
1349 unsigned int *i = arg;
1354 cx88_newstation(core);
1362 /* --- controls ---------------------------------------------- */
1363 case VIDIOC_QUERYCTRL:
1365 struct v4l2_queryctrl *c = arg;
1368 if (c->id < V4L2_CID_BASE ||
1369 c->id >= V4L2_CID_LASTP1)
1371 for (i = 0; i < CX8800_CTLS; i++)
1372 if (cx8800_ctls[i].v.id == c->id)
1374 if (i == CX8800_CTLS) {
1378 *c = cx8800_ctls[i].v;
1382 return get_control(core,arg);
1384 return set_control(core,arg);
1386 /* --- tuner ioctls ------------------------------------------ */
1387 case VIDIOC_G_TUNER:
1389 struct v4l2_tuner *t = arg;
1392 if (UNSET == core->tuner_type)
1397 memset(t,0,sizeof(*t));
1398 strcpy(t->name, "Television");
1399 t->type = V4L2_TUNER_ANALOG_TV;
1400 t->capability = V4L2_TUNER_CAP_NORM;
1401 t->rangehigh = 0xffffffffUL;
1403 cx88_get_stereo(core ,t);
1404 reg = cx_read(MO_DEVICE_STATUS);
1405 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1408 case VIDIOC_S_TUNER:
1410 struct v4l2_tuner *t = arg;
1412 if (UNSET == core->tuner_type)
1416 cx88_set_stereo(core, t->audmode, 1);
1419 case VIDIOC_G_FREQUENCY:
1421 struct v4l2_frequency *f = arg;
1423 memset(f,0,sizeof(*f));
1425 if (UNSET == core->tuner_type)
1428 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1429 f->type = radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1430 f->frequency = core->freq;
1432 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1436 case VIDIOC_S_FREQUENCY:
1438 struct v4l2_frequency *f = arg;
1440 if (UNSET == core->tuner_type)
1444 if (0 == radio && f->type != V4L2_TUNER_ANALOG_TV)
1446 if (1 == radio && f->type != V4L2_TUNER_RADIO)
1449 core->freq = f->frequency;
1450 cx88_newstation(core);
1451 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1453 /* When changing channels it is required to reset TVAUDIO */
1455 cx88_set_tvaudio(core);
1462 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1468 static int video_ioctl(struct inode *inode, struct file *file,
1469 unsigned int cmd, unsigned long arg)
1471 return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
1474 /* ----------------------------------------------------------- */
1476 static int radio_do_ioctl(struct inode *inode, struct file *file,
1477 unsigned int cmd, void *arg)
1479 struct cx8800_fh *fh = file->private_data;
1480 struct cx8800_dev *dev = fh->dev;
1481 struct cx88_core *core = dev->core;
1483 if (video_debug > 1)
1484 cx88_print_ioctl(core->name,cmd);
1487 case VIDIOC_QUERYCAP:
1489 struct v4l2_capability *cap = arg;
1491 memset(cap,0,sizeof(*cap));
1492 strcpy(cap->driver, "cx8800");
1493 strlcpy(cap->card, cx88_boards[core->board].name,
1495 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1496 cap->version = CX88_VERSION_CODE;
1497 cap->capabilities = V4L2_CAP_TUNER;
1500 case VIDIOC_G_TUNER:
1502 struct v4l2_tuner *t = arg;
1507 memset(t,0,sizeof(*t));
1508 strcpy(t->name, "Radio");
1509 t->type = V4L2_TUNER_RADIO;
1511 cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
1514 case VIDIOC_ENUMINPUT:
1516 struct v4l2_input *i = arg;
1520 strcpy(i->name,"Radio");
1521 i->type = V4L2_INPUT_TYPE_TUNER;
1524 case VIDIOC_G_INPUT:
1530 case VIDIOC_G_AUDIO:
1532 struct v4l2_audio *a = arg;
1534 memset(a,0,sizeof(*a));
1535 strcpy(a->name,"Radio");
1540 v4l2_std_id *id = arg;
1547 struct video_tuner *v = arg;
1549 if (v->tuner) /* Only tuner 0 */
1552 cx88_call_i2c_clients(core,VIDIOCSTUNER,v);
1556 case VIDIOC_S_TUNER:
1558 struct v4l2_tuner *t = arg;
1563 cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1568 case VIDIOC_S_AUDIO:
1569 case VIDIOC_S_INPUT:
1573 case VIDIOC_QUERYCTRL:
1575 struct v4l2_queryctrl *c = arg;
1578 if (c->id < V4L2_CID_BASE ||
1579 c->id >= V4L2_CID_LASTP1)
1581 if (c->id == V4L2_CID_AUDIO_MUTE) {
1582 for (i = 0; i < CX8800_CTLS; i++)
1583 if (cx8800_ctls[i].v.id == c->id)
1585 *c = cx8800_ctls[i].v;
1594 case VIDIOC_G_FREQUENCY:
1595 case VIDIOC_S_FREQUENCY:
1596 return video_do_ioctl(inode,file,cmd,arg);
1599 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1605 static int radio_ioctl(struct inode *inode, struct file *file,
1606 unsigned int cmd, unsigned long arg)
1608 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
1611 /* ----------------------------------------------------------- */
1613 static void cx8800_vid_timeout(unsigned long data)
1615 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1616 struct cx88_core *core = dev->core;
1617 struct cx88_dmaqueue *q = &dev->vidq;
1618 struct cx88_buffer *buf;
1619 unsigned long flags;
1621 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1623 cx_clear(MO_VID_DMACNTRL, 0x11);
1624 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1626 spin_lock_irqsave(&dev->slock,flags);
1627 while (!list_empty(&q->active)) {
1628 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1629 list_del(&buf->vb.queue);
1630 buf->vb.state = STATE_ERROR;
1631 wake_up(&buf->vb.done);
1632 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1633 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1635 restart_video_queue(dev,q);
1636 spin_unlock_irqrestore(&dev->slock,flags);
1639 static char *cx88_vid_irqs[32] = {
1640 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1641 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1642 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1643 "y_sync", "u_sync", "v_sync", "vbi_sync",
1644 "opc_err", "par_err", "rip_err", "pci_abort",
1647 static void cx8800_vid_irq(struct cx8800_dev *dev)
1649 struct cx88_core *core = dev->core;
1650 u32 status, mask, count;
1652 status = cx_read(MO_VID_INTSTAT);
1653 mask = cx_read(MO_VID_INTMSK);
1654 if (0 == (status & mask))
1656 cx_write(MO_VID_INTSTAT, status);
1657 if (irq_debug || (status & mask & ~0xff))
1658 cx88_print_irqbits(core->name, "irq vid",
1659 cx88_vid_irqs, status, mask);
1661 /* risc op code error */
1662 if (status & (1 << 16)) {
1663 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1664 cx_clear(MO_VID_DMACNTRL, 0x11);
1665 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1666 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1670 if (status & 0x01) {
1671 spin_lock(&dev->slock);
1672 count = cx_read(MO_VIDY_GPCNT);
1673 cx88_wakeup(core, &dev->vidq, count);
1674 spin_unlock(&dev->slock);
1678 if (status & 0x08) {
1679 spin_lock(&dev->slock);
1680 count = cx_read(MO_VBI_GPCNT);
1681 cx88_wakeup(core, &dev->vbiq, count);
1682 spin_unlock(&dev->slock);
1686 if (status & 0x10) {
1687 dprintk(2,"stopper video\n");
1688 spin_lock(&dev->slock);
1689 restart_video_queue(dev,&dev->vidq);
1690 spin_unlock(&dev->slock);
1694 if (status & 0x80) {
1695 dprintk(2,"stopper vbi\n");
1696 spin_lock(&dev->slock);
1697 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1698 spin_unlock(&dev->slock);
1702 static irqreturn_t cx8800_irq(int irq, void *dev_id, struct pt_regs *regs)
1704 struct cx8800_dev *dev = dev_id;
1705 struct cx88_core *core = dev->core;
1707 int loop, handled = 0;
1709 for (loop = 0; loop < 10; loop++) {
1710 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
1713 cx_write(MO_PCI_INTSTAT, status);
1716 if (status & core->pci_irqmask)
1717 cx88_core_irq(core,status);
1719 cx8800_vid_irq(dev);
1722 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1724 cx_write(MO_PCI_INTMSK,0);
1728 return IRQ_RETVAL(handled);
1731 /* ----------------------------------------------------------- */
1732 /* exported stuff */
1734 static struct file_operations video_fops =
1736 .owner = THIS_MODULE,
1738 .release = video_release,
1742 .ioctl = video_ioctl,
1743 .llseek = no_llseek,
1746 static struct video_device cx8800_video_template =
1748 .name = "cx8800-video",
1749 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1751 .fops = &video_fops,
1755 static struct video_device cx8800_vbi_template =
1757 .name = "cx8800-vbi",
1758 .type = VID_TYPE_TELETEXT|VID_TYPE_TUNER,
1760 .fops = &video_fops,
1764 static struct file_operations radio_fops =
1766 .owner = THIS_MODULE,
1768 .release = video_release,
1769 .ioctl = radio_ioctl,
1770 .llseek = no_llseek,
1773 static struct video_device cx8800_radio_template =
1775 .name = "cx8800-radio",
1776 .type = VID_TYPE_TUNER,
1778 .fops = &radio_fops,
1782 /* ----------------------------------------------------------- */
1784 static void cx8800_unregister_video(struct cx8800_dev *dev)
1786 if (dev->radio_dev) {
1787 if (-1 != dev->radio_dev->minor)
1788 video_unregister_device(dev->radio_dev);
1790 video_device_release(dev->radio_dev);
1791 dev->radio_dev = NULL;
1794 if (-1 != dev->vbi_dev->minor)
1795 video_unregister_device(dev->vbi_dev);
1797 video_device_release(dev->vbi_dev);
1798 dev->vbi_dev = NULL;
1800 if (dev->video_dev) {
1801 if (-1 != dev->video_dev->minor)
1802 video_unregister_device(dev->video_dev);
1804 video_device_release(dev->video_dev);
1805 dev->video_dev = NULL;
1809 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1810 const struct pci_device_id *pci_id)
1812 struct cx8800_dev *dev;
1813 struct cx88_core *core;
1816 dev = kmalloc(sizeof(*dev),GFP_KERNEL);
1819 memset(dev,0,sizeof(*dev));
1823 if (pci_enable_device(pci_dev)) {
1827 core = cx88_core_get(dev->pci);
1834 /* print pci info */
1835 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
1836 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1837 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1838 "latency: %d, mmio: 0x%lx\n", core->name,
1839 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1840 dev->pci_lat,pci_resource_start(pci_dev,0));
1842 pci_set_master(pci_dev);
1843 if (!pci_dma_supported(pci_dev,0xffffffff)) {
1844 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1849 /* initialize driver struct */
1850 spin_lock_init(&dev->slock);
1851 core->tvnorm = tvnorms;
1853 /* init video dma queues */
1854 INIT_LIST_HEAD(&dev->vidq.active);
1855 INIT_LIST_HEAD(&dev->vidq.queued);
1856 dev->vidq.timeout.function = cx8800_vid_timeout;
1857 dev->vidq.timeout.data = (unsigned long)dev;
1858 init_timer(&dev->vidq.timeout);
1859 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1860 MO_VID_DMACNTRL,0x11,0x00);
1862 /* init vbi dma queues */
1863 INIT_LIST_HEAD(&dev->vbiq.active);
1864 INIT_LIST_HEAD(&dev->vbiq.queued);
1865 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1866 dev->vbiq.timeout.data = (unsigned long)dev;
1867 init_timer(&dev->vbiq.timeout);
1868 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1869 MO_VID_DMACNTRL,0x88,0x00);
1872 err = request_irq(pci_dev->irq, cx8800_irq,
1873 SA_SHIRQ | SA_INTERRUPT, core->name, dev);
1875 printk(KERN_ERR "%s: can't get IRQ %d\n",
1876 core->name,pci_dev->irq);
1879 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1881 /* load and configure helper modules */
1882 if (TUNER_ABSENT != core->tuner_type)
1883 request_module("tuner");
1884 if (core->tda9887_conf)
1885 request_module("tda9887");
1887 /* register v4l devices */
1888 dev->video_dev = cx88_vdev_init(core,dev->pci,
1889 &cx8800_video_template,"video");
1890 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1891 video_nr[core->nr]);
1893 printk(KERN_INFO "%s: can't register video device\n",
1897 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1898 core->name,dev->video_dev->minor & 0x1f);
1900 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1901 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1904 printk(KERN_INFO "%s/0: can't register vbi device\n",
1908 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1909 core->name,dev->vbi_dev->minor & 0x1f);
1911 if (core->has_radio) {
1912 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1913 &cx8800_radio_template,"radio");
1914 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1915 radio_nr[core->nr]);
1917 printk(KERN_INFO "%s/0: can't register radio device\n",
1921 printk(KERN_INFO "%s/0: registered device radio%d\n",
1922 core->name,dev->radio_dev->minor & 0x1f);
1925 /* everything worked */
1926 list_add_tail(&dev->devlist,&cx8800_devlist);
1927 pci_set_drvdata(pci_dev,dev);
1929 /* initial device configuration */
1931 init_controls(core);
1932 cx88_set_tvnorm(core,tvnorms);
1936 /* start tvaudio thread */
1937 if (core->tuner_type != TUNER_ABSENT)
1938 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
1942 cx8800_unregister_video(dev);
1943 free_irq(pci_dev->irq, dev);
1945 cx88_core_put(core,dev->pci);
1951 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1953 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1954 struct cx88_core *core = dev->core;
1957 if (core->kthread) {
1958 kthread_stop(core->kthread);
1959 core->kthread = NULL;
1962 cx88_shutdown(core); /* FIXME */
1963 pci_disable_device(pci_dev);
1965 /* unregister stuff */
1967 free_irq(pci_dev->irq, dev);
1968 cx8800_unregister_video(dev);
1969 pci_set_drvdata(pci_dev, NULL);
1972 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1973 list_del(&dev->devlist);
1974 cx88_core_put(core,dev->pci);
1978 static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1980 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1981 struct cx88_core *core = dev->core;
1983 /* stop video+vbi capture */
1984 spin_lock(&dev->slock);
1985 if (!list_empty(&dev->vidq.active)) {
1986 printk("%s: suspend video\n", core->name);
1987 stop_video_dma(dev);
1988 del_timer(&dev->vidq.timeout);
1990 if (!list_empty(&dev->vbiq.active)) {
1991 printk("%s: suspend vbi\n", core->name);
1992 cx8800_stop_vbi_dma(dev);
1993 del_timer(&dev->vbiq.timeout);
1995 spin_unlock(&dev->slock);
1997 /* FIXME -- shutdown device */
1998 cx88_shutdown(core);
2000 pci_save_state(pci_dev);
2001 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
2002 pci_disable_device(pci_dev);
2003 dev->state.disabled = 1;
2008 static int cx8800_resume(struct pci_dev *pci_dev)
2010 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2011 struct cx88_core *core = dev->core;
2014 if (dev->state.disabled) {
2015 err=pci_enable_device(pci_dev);
2017 printk(KERN_ERR "%s: can't enable device\n",
2022 dev->state.disabled = 0;
2024 err= pci_set_power_state(pci_dev, PCI_D0);
2026 printk(KERN_ERR "%s: can't enable device\n",
2029 pci_disable_device(pci_dev);
2030 dev->state.disabled = 1;
2034 pci_restore_state(pci_dev);
2036 /* FIXME: re-initialize hardware */
2039 /* restart video+vbi capture */
2040 spin_lock(&dev->slock);
2041 if (!list_empty(&dev->vidq.active)) {
2042 printk("%s: resume video\n", core->name);
2043 restart_video_queue(dev,&dev->vidq);
2045 if (!list_empty(&dev->vbiq.active)) {
2046 printk("%s: resume vbi\n", core->name);
2047 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2049 spin_unlock(&dev->slock);
2054 /* ----------------------------------------------------------- */
2056 static struct pci_device_id cx8800_pci_tbl[] = {
2060 .subvendor = PCI_ANY_ID,
2061 .subdevice = PCI_ANY_ID,
2063 /* --- end of list --- */
2066 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2068 static struct pci_driver cx8800_pci_driver = {
2070 .id_table = cx8800_pci_tbl,
2071 .probe = cx8800_initdev,
2072 .remove = __devexit_p(cx8800_finidev),
2074 .suspend = cx8800_suspend,
2075 .resume = cx8800_resume,
2078 static int cx8800_init(void)
2080 printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
2081 (CX88_VERSION_CODE >> 16) & 0xff,
2082 (CX88_VERSION_CODE >> 8) & 0xff,
2083 CX88_VERSION_CODE & 0xff);
2085 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2086 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2088 return pci_register_driver(&cx8800_pci_driver);
2091 static void cx8800_fini(void)
2093 pci_unregister_driver(&cx8800_pci_driver);
2096 module_init(cx8800_init);
2097 module_exit(cx8800_fini);
2099 EXPORT_SYMBOL(cx88_do_ioctl);
2101 /* ----------------------------------------------------------- */
2106 * 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