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 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
38 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
39 MODULE_LICENSE("GPL");
41 /* ------------------------------------------------------------------ */
43 static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
44 static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
45 static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
47 module_param_array(video_nr, int, NULL, 0444);
48 module_param_array(vbi_nr, int, NULL, 0444);
49 module_param_array(radio_nr, int, NULL, 0444);
51 MODULE_PARM_DESC(video_nr,"video device numbers");
52 MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
53 MODULE_PARM_DESC(radio_nr,"radio device numbers");
55 static unsigned int video_debug = 0;
56 module_param(video_debug,int,0644);
57 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
59 static unsigned int irq_debug = 0;
60 module_param(irq_debug,int,0644);
61 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
63 static unsigned int vid_limit = 16;
64 module_param(vid_limit,int,0644);
65 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
67 #define dprintk(level,fmt, arg...) if (video_debug >= level) \
68 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
70 /* ------------------------------------------------------------------ */
72 static LIST_HEAD(cx8800_devlist);
74 /* ------------------------------------------------------------------- */
77 static struct cx88_tvnorm tvnorms[] = {
80 .id = V4L2_STD_NTSC_M,
81 .cxiformat = VideoFormatNTSC,
82 .cxoformat = 0x181f0008,
85 .id = V4L2_STD_NTSC_M_JP,
86 .cxiformat = VideoFormatNTSCJapan,
87 .cxoformat = 0x181f0008,
90 .id = V4L2_STD_PAL_BG,
91 .cxiformat = VideoFormatPAL,
92 .cxoformat = 0x181f0008,
95 .id = V4L2_STD_PAL_DK,
96 .cxiformat = VideoFormatPAL,
97 .cxoformat = 0x181f0008,
100 .id = V4L2_STD_PAL_I,
101 .cxiformat = VideoFormatPAL,
102 .cxoformat = 0x181f0008,
105 .id = V4L2_STD_PAL_M,
106 .cxiformat = VideoFormatPALM,
107 .cxoformat = 0x1c1f0008,
110 .id = V4L2_STD_PAL_N,
111 .cxiformat = VideoFormatPALN,
112 .cxoformat = 0x1c1f0008,
115 .id = V4L2_STD_PAL_Nc,
116 .cxiformat = VideoFormatPALNC,
117 .cxoformat = 0x1c1f0008,
120 .id = V4L2_STD_PAL_60,
121 .cxiformat = VideoFormatPAL60,
122 .cxoformat = 0x181f0008,
125 .id = V4L2_STD_SECAM_L,
126 .cxiformat = VideoFormatSECAM,
127 .cxoformat = 0x181f0008,
130 .id = V4L2_STD_SECAM_DK,
131 .cxiformat = VideoFormatSECAM,
132 .cxoformat = 0x181f0008,
136 static struct cx8800_fmt formats[] = {
138 .name = "8 bpp, gray",
139 .fourcc = V4L2_PIX_FMT_GREY,
140 .cxformat = ColorFormatY8,
142 .flags = FORMAT_FLAGS_PACKED,
144 .name = "15 bpp RGB, le",
145 .fourcc = V4L2_PIX_FMT_RGB555,
146 .cxformat = ColorFormatRGB15,
148 .flags = FORMAT_FLAGS_PACKED,
150 .name = "15 bpp RGB, be",
151 .fourcc = V4L2_PIX_FMT_RGB555X,
152 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
154 .flags = FORMAT_FLAGS_PACKED,
156 .name = "16 bpp RGB, le",
157 .fourcc = V4L2_PIX_FMT_RGB565,
158 .cxformat = ColorFormatRGB16,
160 .flags = FORMAT_FLAGS_PACKED,
162 .name = "16 bpp RGB, be",
163 .fourcc = V4L2_PIX_FMT_RGB565X,
164 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
166 .flags = FORMAT_FLAGS_PACKED,
168 .name = "24 bpp RGB, le",
169 .fourcc = V4L2_PIX_FMT_BGR24,
170 .cxformat = ColorFormatRGB24,
172 .flags = FORMAT_FLAGS_PACKED,
174 .name = "32 bpp RGB, le",
175 .fourcc = V4L2_PIX_FMT_BGR32,
176 .cxformat = ColorFormatRGB32,
178 .flags = FORMAT_FLAGS_PACKED,
180 .name = "32 bpp RGB, be",
181 .fourcc = V4L2_PIX_FMT_RGB32,
182 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
184 .flags = FORMAT_FLAGS_PACKED,
186 .name = "4:2:2, packed, YUYV",
187 .fourcc = V4L2_PIX_FMT_YUYV,
188 .cxformat = ColorFormatYUY2,
190 .flags = FORMAT_FLAGS_PACKED,
192 .name = "4:2:2, packed, UYVY",
193 .fourcc = V4L2_PIX_FMT_UYVY,
194 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
196 .flags = FORMAT_FLAGS_PACKED,
200 static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
204 for (i = 0; i < ARRAY_SIZE(formats); i++)
205 if (formats[i].fourcc == fourcc)
210 /* ------------------------------------------------------------------- */
212 static const struct v4l2_queryctrl no_ctl = {
214 .flags = V4L2_CTRL_FLAG_DISABLED,
217 static struct cx88_ctrl cx8800_ctls[] = {
221 .id = V4L2_CID_BRIGHTNESS,
222 .name = "Brightness",
227 .type = V4L2_CTRL_TYPE_INTEGER,
230 .reg = MO_CONTR_BRIGHT,
235 .id = V4L2_CID_CONTRAST,
241 .type = V4L2_CTRL_TYPE_INTEGER,
244 .reg = MO_CONTR_BRIGHT,
255 .type = V4L2_CTRL_TYPE_INTEGER,
262 /* strictly, this only describes only U saturation.
263 * V saturation is handled specially through code.
266 .id = V4L2_CID_SATURATION,
267 .name = "Saturation",
272 .type = V4L2_CTRL_TYPE_INTEGER,
275 .reg = MO_UV_SATURATION,
281 .id = V4L2_CID_AUDIO_MUTE,
285 .type = V4L2_CTRL_TYPE_BOOLEAN,
288 .sreg = SHADOW_AUD_VOL_CTL,
293 .id = V4L2_CID_AUDIO_VOLUME,
299 .type = V4L2_CTRL_TYPE_INTEGER,
302 .sreg = SHADOW_AUD_VOL_CTL,
307 .id = V4L2_CID_AUDIO_BALANCE,
312 .default_value = 0x40,
313 .type = V4L2_CTRL_TYPE_INTEGER,
316 .sreg = SHADOW_AUD_BAL_CTL,
321 static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
323 /* ------------------------------------------------------------------- */
324 /* resource management */
326 static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
328 struct cx88_core *core = dev->core;
329 if (fh->resources & bit)
330 /* have it already allocated */
335 if (dev->resources & bit) {
336 /* no, someone else uses it */
340 /* it's free, grab it */
341 fh->resources |= bit;
342 dev->resources |= bit;
343 dprintk(1,"res: get %d\n",bit);
349 int res_check(struct cx8800_fh *fh, unsigned int bit)
351 return (fh->resources & bit);
355 int res_locked(struct cx8800_dev *dev, unsigned int bit)
357 return (dev->resources & bit);
361 void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
363 struct cx88_core *core = dev->core;
364 if ((fh->resources & bits) != bits)
368 fh->resources &= ~bits;
369 dev->resources &= ~bits;
370 dprintk(1,"res: put %d\n",bits);
374 /* ------------------------------------------------------------------ */
376 /* static int video_mux(struct cx8800_dev *dev, unsigned int input) */
377 static int video_mux(struct cx88_core *core, unsigned int input)
379 /* struct cx88_core *core = dev->core; */
381 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
382 input, INPUT(input)->vmux,
383 INPUT(input)->gpio0,INPUT(input)->gpio1,
384 INPUT(input)->gpio2,INPUT(input)->gpio3);
386 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input)->vmux << 14);
387 cx_write(MO_GP3_IO, INPUT(input)->gpio3);
388 cx_write(MO_GP0_IO, INPUT(input)->gpio0);
389 cx_write(MO_GP1_IO, INPUT(input)->gpio1);
390 cx_write(MO_GP2_IO, INPUT(input)->gpio2);
392 switch (INPUT(input)->type) {
393 case CX88_VMUX_SVIDEO:
394 cx_set(MO_AFECFG_IO, 0x00000001);
395 cx_set(MO_INPUT_FORMAT, 0x00010010);
396 cx_set(MO_FILTER_EVEN, 0x00002020);
397 cx_set(MO_FILTER_ODD, 0x00002020);
400 cx_clear(MO_AFECFG_IO, 0x00000001);
401 cx_clear(MO_INPUT_FORMAT, 0x00010010);
402 cx_clear(MO_FILTER_EVEN, 0x00002020);
403 cx_clear(MO_FILTER_ODD, 0x00002020);
409 /* ------------------------------------------------------------------ */
411 static int start_video_dma(struct cx8800_dev *dev,
412 struct cx88_dmaqueue *q,
413 struct cx88_buffer *buf)
415 struct cx88_core *core = dev->core;
417 /* setup fifo + format */
418 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
419 buf->bpl, buf->risc.dma);
420 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
421 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
424 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
428 cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
430 /* Enables corresponding bits at PCI_INT_STAT:
431 bits 0 to 4: video, audio, transport stream, VIP, Host
433 bits 8 and 9: DMA complete for: SRC, DST
434 bits 10 and 11: BERR signal asserted for RISC: RD, WR
435 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
437 cx_set(MO_VID_INTMSK, 0x0f0011);
440 cx_set(VID_CAPTURE_CONTROL,0x06);
443 cx_set(MO_DEV_CNTRL2, (1<<5));
444 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
449 static int stop_video_dma(struct cx8800_dev *dev)
451 struct cx88_core *core = dev->core;
454 cx_clear(MO_VID_DMACNTRL, 0x11);
456 /* disable capture */
457 cx_clear(VID_CAPTURE_CONTROL,0x06);
460 cx_clear(MO_PCI_INTMSK, 0x000001);
461 cx_clear(MO_VID_INTMSK, 0x0f0011);
465 static int restart_video_queue(struct cx8800_dev *dev,
466 struct cx88_dmaqueue *q)
468 struct cx88_core *core = dev->core;
469 struct cx88_buffer *buf, *prev;
470 struct list_head *item;
472 if (!list_empty(&q->active)) {
473 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
474 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
476 start_video_dma(dev, q, buf);
477 list_for_each(item,&q->active) {
478 buf = list_entry(item, struct cx88_buffer, vb.queue);
479 buf->count = q->count++;
481 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
487 if (list_empty(&q->queued))
489 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
491 list_del(&buf->vb.queue);
492 list_add_tail(&buf->vb.queue,&q->active);
493 start_video_dma(dev, q, buf);
494 buf->vb.state = STATE_ACTIVE;
495 buf->count = q->count++;
496 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
497 dprintk(2,"[%p/%d] restart_queue - first active\n",
500 } else if (prev->vb.width == buf->vb.width &&
501 prev->vb.height == buf->vb.height &&
502 prev->fmt == buf->fmt) {
503 list_del(&buf->vb.queue);
504 list_add_tail(&buf->vb.queue,&q->active);
505 buf->vb.state = STATE_ACTIVE;
506 buf->count = q->count++;
507 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
508 dprintk(2,"[%p/%d] restart_queue - move to active\n",
517 /* ------------------------------------------------------------------ */
520 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
522 struct cx8800_fh *fh = q->priv_data;
524 *size = fh->fmt->depth*fh->width*fh->height >> 3;
527 while (*size * *count > vid_limit * 1024 * 1024)
533 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
534 enum v4l2_field field)
536 struct cx8800_fh *fh = q->priv_data;
537 struct cx8800_dev *dev = fh->dev;
538 struct cx88_core *core = dev->core;
539 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
540 int rc, init_buffer = 0;
542 BUG_ON(NULL == fh->fmt);
543 if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
544 fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
546 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
547 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
550 if (buf->fmt != fh->fmt ||
551 buf->vb.width != fh->width ||
552 buf->vb.height != fh->height ||
553 buf->vb.field != field) {
555 buf->vb.width = fh->width;
556 buf->vb.height = fh->height;
557 buf->vb.field = field;
561 if (STATE_NEEDS_INIT == buf->vb.state) {
563 if (0 != (rc = videobuf_iolock(dev->pci,&buf->vb,NULL)))
568 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
569 switch (buf->vb.field) {
571 cx88_risc_buffer(dev->pci, &buf->risc,
572 buf->vb.dma.sglist, 0, UNSET,
573 buf->bpl, 0, buf->vb.height);
575 case V4L2_FIELD_BOTTOM:
576 cx88_risc_buffer(dev->pci, &buf->risc,
577 buf->vb.dma.sglist, UNSET, 0,
578 buf->bpl, 0, buf->vb.height);
580 case V4L2_FIELD_INTERLACED:
581 cx88_risc_buffer(dev->pci, &buf->risc,
582 buf->vb.dma.sglist, 0, buf->bpl,
584 buf->vb.height >> 1);
586 case V4L2_FIELD_SEQ_TB:
587 cx88_risc_buffer(dev->pci, &buf->risc,
589 0, buf->bpl * (buf->vb.height >> 1),
591 buf->vb.height >> 1);
593 case V4L2_FIELD_SEQ_BT:
594 cx88_risc_buffer(dev->pci, &buf->risc,
596 buf->bpl * (buf->vb.height >> 1), 0,
598 buf->vb.height >> 1);
604 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
606 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
607 (unsigned long)buf->risc.dma);
609 buf->vb.state = STATE_PREPARED;
613 cx88_free_buffer(dev->pci,buf);
618 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
620 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
621 struct cx88_buffer *prev;
622 struct cx8800_fh *fh = vq->priv_data;
623 struct cx8800_dev *dev = fh->dev;
624 struct cx88_core *core = dev->core;
625 struct cx88_dmaqueue *q = &dev->vidq;
627 /* add jump to stopper */
628 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
629 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
631 if (!list_empty(&q->queued)) {
632 list_add_tail(&buf->vb.queue,&q->queued);
633 buf->vb.state = STATE_QUEUED;
634 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
637 } else if (list_empty(&q->active)) {
638 list_add_tail(&buf->vb.queue,&q->active);
639 start_video_dma(dev, q, buf);
640 buf->vb.state = STATE_ACTIVE;
641 buf->count = q->count++;
642 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
643 dprintk(2,"[%p/%d] buffer_queue - first active\n",
647 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
648 if (prev->vb.width == buf->vb.width &&
649 prev->vb.height == buf->vb.height &&
650 prev->fmt == buf->fmt) {
651 list_add_tail(&buf->vb.queue,&q->active);
652 buf->vb.state = STATE_ACTIVE;
653 buf->count = q->count++;
654 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
655 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
659 list_add_tail(&buf->vb.queue,&q->queued);
660 buf->vb.state = STATE_QUEUED;
661 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
667 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
669 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
670 struct cx8800_fh *fh = q->priv_data;
672 cx88_free_buffer(fh->dev->pci,buf);
675 static struct videobuf_queue_ops cx8800_video_qops = {
676 .buf_setup = buffer_setup,
677 .buf_prepare = buffer_prepare,
678 .buf_queue = buffer_queue,
679 .buf_release = buffer_release,
682 /* ------------------------------------------------------------------ */
685 /* ------------------------------------------------------------------ */
687 static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
690 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
692 case V4L2_BUF_TYPE_VBI_CAPTURE:
700 static int get_ressource(struct cx8800_fh *fh)
703 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
704 return RESOURCE_VIDEO;
705 case V4L2_BUF_TYPE_VBI_CAPTURE:
713 static int video_open(struct inode *inode, struct file *file)
715 int minor = iminor(inode);
716 struct cx8800_dev *h,*dev = NULL;
717 struct cx88_core *core;
718 struct cx8800_fh *fh;
719 struct list_head *list;
720 enum v4l2_buf_type type = 0;
723 list_for_each(list,&cx8800_devlist) {
724 h = list_entry(list, struct cx8800_dev, devlist);
725 if (h->video_dev->minor == minor) {
727 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
729 if (h->vbi_dev->minor == minor) {
731 type = V4L2_BUF_TYPE_VBI_CAPTURE;
734 h->radio_dev->minor == minor) {
744 dprintk(1,"open minor=%d radio=%d type=%s\n",
745 minor,radio,v4l2_type_names[type]);
747 /* allocate + initialize per filehandle data */
748 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
751 memset(fh,0,sizeof(*fh));
752 file->private_data = fh;
758 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
760 videobuf_queue_init(&fh->vidq, &cx8800_video_qops,
761 dev->pci, &dev->slock,
762 V4L2_BUF_TYPE_VIDEO_CAPTURE,
763 V4L2_FIELD_INTERLACED,
764 sizeof(struct cx88_buffer),
766 videobuf_queue_init(&fh->vbiq, &cx8800_vbi_qops,
767 dev->pci, &dev->slock,
768 V4L2_BUF_TYPE_VBI_CAPTURE,
770 sizeof(struct cx88_buffer),
774 int board = core->board;
775 dprintk(1,"video_open: setting radio device\n");
776 cx_write(MO_GP3_IO, cx88_boards[board].radio.gpio3);
777 cx_write(MO_GP0_IO, cx88_boards[board].radio.gpio0);
778 cx_write(MO_GP1_IO, cx88_boards[board].radio.gpio1);
779 cx_write(MO_GP2_IO, cx88_boards[board].radio.gpio2);
780 core->tvaudio = WW_FM;
781 cx88_set_tvaudio(core);
782 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
783 cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
790 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
792 struct cx8800_fh *fh = file->private_data;
795 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
796 if (res_locked(fh->dev,RESOURCE_VIDEO))
798 return videobuf_read_one(&fh->vidq, data, count, ppos,
799 file->f_flags & O_NONBLOCK);
800 case V4L2_BUF_TYPE_VBI_CAPTURE:
801 if (!res_get(fh->dev,fh,RESOURCE_VBI))
803 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
804 file->f_flags & O_NONBLOCK);
812 video_poll(struct file *file, struct poll_table_struct *wait)
814 struct cx8800_fh *fh = file->private_data;
815 struct cx88_buffer *buf;
817 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
818 if (!res_get(fh->dev,fh,RESOURCE_VBI))
820 return videobuf_poll_stream(file, &fh->vbiq, wait);
823 if (res_check(fh,RESOURCE_VIDEO)) {
824 /* streaming capture */
825 if (list_empty(&fh->vidq.stream))
827 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
830 buf = (struct cx88_buffer*)fh->vidq.read_buf;
834 poll_wait(file, &buf->vb.done, wait);
835 if (buf->vb.state == STATE_DONE ||
836 buf->vb.state == STATE_ERROR)
837 return POLLIN|POLLRDNORM;
841 static int video_release(struct inode *inode, struct file *file)
843 struct cx8800_fh *fh = file->private_data;
844 struct cx8800_dev *dev = fh->dev;
846 /* turn off overlay */
847 if (res_check(fh, RESOURCE_OVERLAY)) {
849 res_free(dev,fh,RESOURCE_OVERLAY);
852 /* stop video capture */
853 if (res_check(fh, RESOURCE_VIDEO)) {
854 videobuf_queue_cancel(&fh->vidq);
855 res_free(dev,fh,RESOURCE_VIDEO);
857 if (fh->vidq.read_buf) {
858 buffer_release(&fh->vidq,fh->vidq.read_buf);
859 kfree(fh->vidq.read_buf);
862 /* stop vbi capture */
863 if (res_check(fh, RESOURCE_VBI)) {
864 if (fh->vbiq.streaming)
865 videobuf_streamoff(&fh->vbiq);
866 if (fh->vbiq.reading)
867 videobuf_read_stop(&fh->vbiq);
868 res_free(dev,fh,RESOURCE_VBI);
871 videobuf_mmap_free(&fh->vidq);
872 videobuf_mmap_free(&fh->vbiq);
873 file->private_data = NULL;
876 cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
882 video_mmap(struct file *file, struct vm_area_struct * vma)
884 struct cx8800_fh *fh = file->private_data;
886 return videobuf_mmap_mapper(get_queue(fh), vma);
889 /* ------------------------------------------------------------------ */
891 /* static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
892 static int get_control(struct cx88_core *core, struct v4l2_control *ctl)
894 /* struct cx88_core *core = dev->core; */
895 struct cx88_ctrl *c = NULL;
899 for (i = 0; i < CX8800_CTLS; i++)
900 if (cx8800_ctls[i].v.id == ctl->id)
905 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
907 case V4L2_CID_AUDIO_BALANCE:
908 ctl->value = (value & 0x40) ? (value & 0x3f) : (0x40 - (value & 0x3f));
910 case V4L2_CID_AUDIO_VOLUME:
911 ctl->value = 0x3f - (value & 0x3f);
914 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
920 /* static int set_control(struct cx8800_dev *dev, struct v4l2_control *ctl) */
921 static int set_control(struct cx88_core *core, struct v4l2_control *ctl)
923 /* struct cx88_core *core = dev->core; */
924 struct cx88_ctrl *c = NULL;
929 for (i = 0; i < CX8800_CTLS; i++)
930 if (cx8800_ctls[i].v.id == ctl->id)
935 if (ctl->value < c->v.minimum)
936 ctl->value = c->v.minimum;
937 if (ctl->value > c->v.maximum)
938 ctl->value = c->v.maximum;
940 case V4L2_CID_AUDIO_BALANCE:
941 value = (ctl->value < 0x40) ? (0x40 - ctl->value) : ctl->value;
943 case V4L2_CID_AUDIO_VOLUME:
944 value = 0x3f - (ctl->value & 0x3f);
946 case V4L2_CID_SATURATION:
947 /* special v_sat handling */
948 v_sat_value = ctl->value - (0x7f - 0x5a);
949 if (v_sat_value > 0xff)
951 if (v_sat_value < 0x00)
953 cx_andor(MO_UV_SATURATION, 0xff00, v_sat_value << 8);
954 /* fall through to default route for u_sat */
956 value = ((ctl->value - c->off) << c->shift) & c->mask;
959 dprintk(1,"set_control id=0x%X reg=0x%x val=0x%x%s\n",
960 ctl->id, c->reg, value, c->sreg ? " [shadowed]" : "");
962 cx_sandor(c->sreg, c->reg, c->mask, value);
964 cx_andor(c->reg, c->mask, value);
969 /* static void init_controls(struct cx8800_dev *dev) */
970 static void init_controls(struct cx88_core *core)
972 static struct v4l2_control mute = {
973 .id = V4L2_CID_AUDIO_MUTE,
976 static struct v4l2_control volume = {
977 .id = V4L2_CID_AUDIO_VOLUME,
980 static struct v4l2_control hue = {
984 static struct v4l2_control contrast = {
985 .id = V4L2_CID_CONTRAST,
988 static struct v4l2_control brightness = {
989 .id = V4L2_CID_BRIGHTNESS,
993 set_control(core,&mute);
994 set_control(core,&volume);
995 set_control(core,&hue);
996 set_control(core,&contrast);
997 set_control(core,&brightness);
1000 /* ------------------------------------------------------------------ */
1002 static int cx8800_g_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1003 struct v4l2_format *f)
1006 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1007 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
1008 f->fmt.pix.width = fh->width;
1009 f->fmt.pix.height = fh->height;
1010 f->fmt.pix.field = fh->vidq.field;
1011 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1012 f->fmt.pix.bytesperline =
1013 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1014 f->fmt.pix.sizeimage =
1015 f->fmt.pix.height * f->fmt.pix.bytesperline;
1017 case V4L2_BUF_TYPE_VBI_CAPTURE:
1018 cx8800_vbi_fmt(dev, f);
1025 static int cx8800_try_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1026 struct v4l2_format *f)
1028 struct cx88_core *core = dev->core;
1031 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1033 struct cx8800_fmt *fmt;
1034 enum v4l2_field field;
1035 unsigned int maxw, maxh;
1037 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1041 field = f->fmt.pix.field;
1042 maxw = norm_maxw(core->tvnorm);
1043 maxh = norm_maxh(core->tvnorm);
1045 if (V4L2_FIELD_ANY == field) {
1046 field = (f->fmt.pix.height > maxh/2)
1047 ? V4L2_FIELD_INTERLACED
1048 : V4L2_FIELD_BOTTOM;
1052 case V4L2_FIELD_TOP:
1053 case V4L2_FIELD_BOTTOM:
1056 case V4L2_FIELD_INTERLACED:
1062 f->fmt.pix.field = field;
1063 if (f->fmt.pix.height < 32)
1064 f->fmt.pix.height = 32;
1065 if (f->fmt.pix.height > maxh)
1066 f->fmt.pix.height = maxh;
1067 if (f->fmt.pix.width < 48)
1068 f->fmt.pix.width = 48;
1069 if (f->fmt.pix.width > maxw)
1070 f->fmt.pix.width = maxw;
1071 f->fmt.pix.width &= ~0x03;
1072 f->fmt.pix.bytesperline =
1073 (f->fmt.pix.width * fmt->depth) >> 3;
1074 f->fmt.pix.sizeimage =
1075 f->fmt.pix.height * f->fmt.pix.bytesperline;
1079 case V4L2_BUF_TYPE_VBI_CAPTURE:
1080 cx8800_vbi_fmt(dev, f);
1087 static int cx8800_s_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1088 struct v4l2_format *f)
1093 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1094 err = cx8800_try_fmt(dev,fh,f);
1098 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1099 fh->width = f->fmt.pix.width;
1100 fh->height = f->fmt.pix.height;
1101 fh->vidq.field = f->fmt.pix.field;
1103 case V4L2_BUF_TYPE_VBI_CAPTURE:
1104 cx8800_vbi_fmt(dev, f);
1112 * This function is _not_ called directly, but from
1113 * video_generic_ioctl (and maybe others). userspace
1114 * copying is done already, arg is a kernel pointer.
1116 static int video_do_ioctl(struct inode *inode, struct file *file,
1117 unsigned int cmd, void *arg)
1119 struct cx8800_fh *fh = file->private_data;
1120 struct cx8800_dev *dev = fh->dev;
1121 struct cx88_core *core = dev->core;
1124 if (video_debug > 1)
1125 cx88_print_ioctl(core->name,cmd);
1128 /* --- capabilities ------------------------------------------ */
1129 case VIDIOC_QUERYCAP:
1131 struct v4l2_capability *cap = arg;
1133 memset(cap,0,sizeof(*cap));
1134 strcpy(cap->driver, "cx8800");
1135 strlcpy(cap->card, cx88_boards[core->board].name,
1137 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1138 cap->version = CX88_VERSION_CODE;
1140 V4L2_CAP_VIDEO_CAPTURE |
1141 V4L2_CAP_READWRITE |
1142 V4L2_CAP_STREAMING |
1143 V4L2_CAP_VBI_CAPTURE |
1144 V4L2_CAP_VIDEO_OVERLAY |
1146 if (UNSET != core->tuner_type)
1147 cap->capabilities |= V4L2_CAP_TUNER;
1151 /* --- capture ioctls ---------------------------------------- */
1152 case VIDIOC_ENUM_FMT:
1154 struct v4l2_fmtdesc *f = arg;
1155 enum v4l2_buf_type type;
1161 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1162 if (index >= ARRAY_SIZE(formats))
1164 memset(f,0,sizeof(*f));
1167 strlcpy(f->description,formats[index].name,sizeof(f->description));
1168 f->pixelformat = formats[index].fourcc;
1177 struct v4l2_format *f = arg;
1178 return cx8800_g_fmt(dev,fh,f);
1182 struct v4l2_format *f = arg;
1183 return cx8800_s_fmt(dev,fh,f);
1185 case VIDIOC_TRY_FMT:
1187 struct v4l2_format *f = arg;
1188 return cx8800_try_fmt(dev,fh,f);
1191 /* --- streaming capture ------------------------------------- */
1194 struct video_mbuf *mbuf = arg;
1195 struct videobuf_queue *q;
1196 struct v4l2_requestbuffers req;
1200 memset(&req,0,sizeof(req));
1203 req.memory = V4L2_MEMORY_MMAP;
1204 err = videobuf_reqbufs(q,&req);
1207 memset(mbuf,0,sizeof(*mbuf));
1208 mbuf->frames = req.count;
1210 for (i = 0; i < mbuf->frames; i++) {
1211 mbuf->offsets[i] = q->bufs[i]->boff;
1212 mbuf->size += q->bufs[i]->bsize;
1216 case VIDIOC_REQBUFS:
1217 return videobuf_reqbufs(get_queue(fh), arg);
1219 case VIDIOC_QUERYBUF:
1220 return videobuf_querybuf(get_queue(fh), arg);
1223 return videobuf_qbuf(get_queue(fh), arg);
1226 return videobuf_dqbuf(get_queue(fh), arg,
1227 file->f_flags & O_NONBLOCK);
1229 case VIDIOC_STREAMON:
1231 int res = get_ressource(fh);
1233 if (!res_get(dev,fh,res))
1235 return videobuf_streamon(get_queue(fh));
1237 case VIDIOC_STREAMOFF:
1239 int res = get_ressource(fh);
1241 err = videobuf_streamoff(get_queue(fh));
1244 res_free(dev,fh,res);
1249 return cx88_do_ioctl( inode, file, fh->radio, core, cmd, arg, video_do_ioctl );
1254 int cx88_do_ioctl(struct inode *inode, struct file *file, int radio,
1255 struct cx88_core *core, unsigned int cmd, void *arg, v4l2_kioctl driver_ioctl)
1259 dprintk( 1, "CORE IOCTL: 0x%x\n", cmd );
1260 if (video_debug > 1)
1261 cx88_print_ioctl(core->name,cmd);
1264 /* ---------- tv norms ---------- */
1265 case VIDIOC_ENUMSTD:
1267 struct v4l2_standard *e = arg;
1271 if (i >= ARRAY_SIZE(tvnorms))
1273 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1274 tvnorms[e->index].name);
1282 v4l2_std_id *id = arg;
1284 *id = core->tvnorm->id;
1289 v4l2_std_id *id = arg;
1292 for(i = 0; i < ARRAY_SIZE(tvnorms); i++)
1293 if (*id & tvnorms[i].id)
1295 if (i == ARRAY_SIZE(tvnorms))
1299 cx88_set_tvnorm(core,&tvnorms[i]);
1304 /* ------ input switching ---------- */
1305 case VIDIOC_ENUMINPUT:
1307 static const char *iname[] = {
1308 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1309 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1310 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1311 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1312 [ CX88_VMUX_SVIDEO ] = "S-Video",
1313 [ CX88_VMUX_TELEVISION ] = "Television",
1314 [ CX88_VMUX_CABLE ] = "Cable TV",
1315 [ CX88_VMUX_DVB ] = "DVB",
1316 [ CX88_VMUX_DEBUG ] = "for debug only",
1318 struct v4l2_input *i = arg;
1324 if (0 == INPUT(n)->type)
1326 memset(i,0,sizeof(*i));
1328 i->type = V4L2_INPUT_TYPE_CAMERA;
1329 strcpy(i->name,iname[INPUT(n)->type]);
1330 if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
1331 (CX88_VMUX_CABLE == INPUT(n)->type))
1332 i->type = V4L2_INPUT_TYPE_TUNER;
1333 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1334 i->std |= tvnorms[n].id;
1337 case VIDIOC_G_INPUT:
1339 unsigned int *i = arg;
1344 case VIDIOC_S_INPUT:
1346 unsigned int *i = arg;
1351 cx88_newstation(core);
1359 /* --- controls ---------------------------------------------- */
1360 case VIDIOC_QUERYCTRL:
1362 struct v4l2_queryctrl *c = arg;
1365 if (c->id < V4L2_CID_BASE ||
1366 c->id >= V4L2_CID_LASTP1)
1368 for (i = 0; i < CX8800_CTLS; i++)
1369 if (cx8800_ctls[i].v.id == c->id)
1371 if (i == CX8800_CTLS) {
1375 *c = cx8800_ctls[i].v;
1379 return get_control(core,arg);
1381 return set_control(core,arg);
1383 /* --- tuner ioctls ------------------------------------------ */
1384 case VIDIOC_G_TUNER:
1386 struct v4l2_tuner *t = arg;
1389 if (UNSET == core->tuner_type)
1394 memset(t,0,sizeof(*t));
1395 strcpy(t->name, "Television");
1396 t->type = V4L2_TUNER_ANALOG_TV;
1397 t->capability = V4L2_TUNER_CAP_NORM;
1398 t->rangehigh = 0xffffffffUL;
1400 cx88_get_stereo(core ,t);
1401 reg = cx_read(MO_DEVICE_STATUS);
1402 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1405 case VIDIOC_S_TUNER:
1407 struct v4l2_tuner *t = arg;
1409 if (UNSET == core->tuner_type)
1413 cx88_set_stereo(core, t->audmode, 1);
1416 case VIDIOC_G_FREQUENCY:
1418 struct v4l2_frequency *f = arg;
1420 memset(f,0,sizeof(*f));
1422 if (UNSET == core->tuner_type)
1425 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1426 f->type = radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1427 f->frequency = core->freq;
1429 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1433 case VIDIOC_S_FREQUENCY:
1435 struct v4l2_frequency *f = arg;
1437 if (UNSET == core->tuner_type)
1441 if (0 == radio && f->type != V4L2_TUNER_ANALOG_TV)
1443 if (1 == radio && f->type != V4L2_TUNER_RADIO)
1446 core->freq = f->frequency;
1447 cx88_newstation(core);
1448 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1450 /* When changing channels it is required to reset TVAUDIO */
1452 cx88_set_tvaudio(core);
1459 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1465 static int video_ioctl(struct inode *inode, struct file *file,
1466 unsigned int cmd, unsigned long arg)
1468 return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
1471 /* ----------------------------------------------------------- */
1473 static int radio_do_ioctl(struct inode *inode, struct file *file,
1474 unsigned int cmd, void *arg)
1476 struct cx8800_fh *fh = file->private_data;
1477 struct cx8800_dev *dev = fh->dev;
1478 struct cx88_core *core = dev->core;
1480 if (video_debug > 1)
1481 cx88_print_ioctl(core->name,cmd);
1484 case VIDIOC_QUERYCAP:
1486 struct v4l2_capability *cap = arg;
1488 memset(cap,0,sizeof(*cap));
1489 strcpy(cap->driver, "cx8800");
1490 strlcpy(cap->card, cx88_boards[core->board].name,
1492 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1493 cap->version = CX88_VERSION_CODE;
1494 cap->capabilities = V4L2_CAP_TUNER;
1497 case VIDIOC_G_TUNER:
1499 struct v4l2_tuner *t = arg;
1504 memset(t,0,sizeof(*t));
1505 strcpy(t->name, "Radio");
1507 cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
1510 case VIDIOC_ENUMINPUT:
1512 struct v4l2_input *i = arg;
1516 strcpy(i->name,"Radio");
1517 i->type = V4L2_INPUT_TYPE_TUNER;
1520 case VIDIOC_G_INPUT:
1526 case VIDIOC_G_AUDIO:
1528 struct v4l2_audio *a = arg;
1530 memset(a,0,sizeof(*a));
1531 strcpy(a->name,"Radio");
1536 v4l2_std_id *id = arg;
1542 struct video_tuner *v = arg;
1544 if (v->tuner) /* Only tuner 0 */
1547 cx88_call_i2c_clients(core,VIDIOCSTUNER,v);
1550 case VIDIOC_S_TUNER:
1552 struct v4l2_tuner *t = arg;
1557 cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1562 case VIDIOC_S_AUDIO:
1563 case VIDIOC_S_INPUT:
1567 case VIDIOC_QUERYCTRL:
1569 struct v4l2_queryctrl *c = arg;
1572 if (c->id < V4L2_CID_BASE ||
1573 c->id >= V4L2_CID_LASTP1)
1575 if (c->id == V4L2_CID_AUDIO_MUTE) {
1576 for (i = 0; i < CX8800_CTLS; i++)
1577 if (cx8800_ctls[i].v.id == c->id)
1579 *c = cx8800_ctls[i].v;
1588 case VIDIOC_G_FREQUENCY:
1589 case VIDIOC_S_FREQUENCY:
1590 return video_do_ioctl(inode,file,cmd,arg);
1593 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1599 static int radio_ioctl(struct inode *inode, struct file *file,
1600 unsigned int cmd, unsigned long arg)
1602 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
1605 /* ----------------------------------------------------------- */
1607 static void cx8800_vid_timeout(unsigned long data)
1609 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1610 struct cx88_core *core = dev->core;
1611 struct cx88_dmaqueue *q = &dev->vidq;
1612 struct cx88_buffer *buf;
1613 unsigned long flags;
1615 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1617 cx_clear(MO_VID_DMACNTRL, 0x11);
1618 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1620 spin_lock_irqsave(&dev->slock,flags);
1621 while (!list_empty(&q->active)) {
1622 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1623 list_del(&buf->vb.queue);
1624 buf->vb.state = STATE_ERROR;
1625 wake_up(&buf->vb.done);
1626 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1627 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1629 restart_video_queue(dev,q);
1630 spin_unlock_irqrestore(&dev->slock,flags);
1633 static char *cx88_vid_irqs[32] = {
1634 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1635 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1636 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1637 "y_sync", "u_sync", "v_sync", "vbi_sync",
1638 "opc_err", "par_err", "rip_err", "pci_abort",
1641 static void cx8800_vid_irq(struct cx8800_dev *dev)
1643 struct cx88_core *core = dev->core;
1644 u32 status, mask, count;
1646 status = cx_read(MO_VID_INTSTAT);
1647 mask = cx_read(MO_VID_INTMSK);
1648 if (0 == (status & mask))
1650 cx_write(MO_VID_INTSTAT, status);
1651 if (irq_debug || (status & mask & ~0xff))
1652 cx88_print_irqbits(core->name, "irq vid",
1653 cx88_vid_irqs, status, mask);
1655 /* risc op code error */
1656 if (status & (1 << 16)) {
1657 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1658 cx_clear(MO_VID_DMACNTRL, 0x11);
1659 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1660 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1664 if (status & 0x01) {
1665 spin_lock(&dev->slock);
1666 count = cx_read(MO_VIDY_GPCNT);
1667 cx88_wakeup(core, &dev->vidq, count);
1668 spin_unlock(&dev->slock);
1672 if (status & 0x08) {
1673 spin_lock(&dev->slock);
1674 count = cx_read(MO_VBI_GPCNT);
1675 cx88_wakeup(core, &dev->vbiq, count);
1676 spin_unlock(&dev->slock);
1680 if (status & 0x10) {
1681 dprintk(2,"stopper video\n");
1682 spin_lock(&dev->slock);
1683 restart_video_queue(dev,&dev->vidq);
1684 spin_unlock(&dev->slock);
1688 if (status & 0x80) {
1689 dprintk(2,"stopper vbi\n");
1690 spin_lock(&dev->slock);
1691 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1692 spin_unlock(&dev->slock);
1696 static irqreturn_t cx8800_irq(int irq, void *dev_id, struct pt_regs *regs)
1698 struct cx8800_dev *dev = dev_id;
1699 struct cx88_core *core = dev->core;
1701 int loop, handled = 0;
1703 for (loop = 0; loop < 10; loop++) {
1704 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
1707 cx_write(MO_PCI_INTSTAT, status);
1710 if (status & core->pci_irqmask)
1711 cx88_core_irq(core,status);
1713 cx8800_vid_irq(dev);
1716 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1718 cx_write(MO_PCI_INTMSK,0);
1722 return IRQ_RETVAL(handled);
1725 /* ----------------------------------------------------------- */
1726 /* exported stuff */
1728 static struct file_operations video_fops =
1730 .owner = THIS_MODULE,
1732 .release = video_release,
1736 .ioctl = video_ioctl,
1737 .llseek = no_llseek,
1740 static struct video_device cx8800_video_template =
1742 .name = "cx8800-video",
1743 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1745 .fops = &video_fops,
1749 static struct video_device cx8800_vbi_template =
1751 .name = "cx8800-vbi",
1752 .type = VID_TYPE_TELETEXT|VID_TYPE_TUNER,
1754 .fops = &video_fops,
1758 static struct file_operations radio_fops =
1760 .owner = THIS_MODULE,
1762 .release = video_release,
1763 .ioctl = radio_ioctl,
1764 .llseek = no_llseek,
1767 static struct video_device cx8800_radio_template =
1769 .name = "cx8800-radio",
1770 .type = VID_TYPE_TUNER,
1772 .fops = &radio_fops,
1776 /* ----------------------------------------------------------- */
1778 static void cx8800_unregister_video(struct cx8800_dev *dev)
1780 if (dev->radio_dev) {
1781 if (-1 != dev->radio_dev->minor)
1782 video_unregister_device(dev->radio_dev);
1784 video_device_release(dev->radio_dev);
1785 dev->radio_dev = NULL;
1788 if (-1 != dev->vbi_dev->minor)
1789 video_unregister_device(dev->vbi_dev);
1791 video_device_release(dev->vbi_dev);
1792 dev->vbi_dev = NULL;
1794 if (dev->video_dev) {
1795 if (-1 != dev->video_dev->minor)
1796 video_unregister_device(dev->video_dev);
1798 video_device_release(dev->video_dev);
1799 dev->video_dev = NULL;
1803 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1804 const struct pci_device_id *pci_id)
1806 struct cx8800_dev *dev;
1807 struct cx88_core *core;
1810 dev = kmalloc(sizeof(*dev),GFP_KERNEL);
1813 memset(dev,0,sizeof(*dev));
1817 if (pci_enable_device(pci_dev)) {
1821 core = cx88_core_get(dev->pci);
1828 /* print pci info */
1829 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
1830 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1831 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1832 "latency: %d, mmio: 0x%lx\n", core->name,
1833 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1834 dev->pci_lat,pci_resource_start(pci_dev,0));
1836 pci_set_master(pci_dev);
1837 if (!pci_dma_supported(pci_dev,0xffffffff)) {
1838 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1843 /* initialize driver struct */
1844 spin_lock_init(&dev->slock);
1845 core->tvnorm = tvnorms;
1847 /* init video dma queues */
1848 INIT_LIST_HEAD(&dev->vidq.active);
1849 INIT_LIST_HEAD(&dev->vidq.queued);
1850 dev->vidq.timeout.function = cx8800_vid_timeout;
1851 dev->vidq.timeout.data = (unsigned long)dev;
1852 init_timer(&dev->vidq.timeout);
1853 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1854 MO_VID_DMACNTRL,0x11,0x00);
1856 /* init vbi dma queues */
1857 INIT_LIST_HEAD(&dev->vbiq.active);
1858 INIT_LIST_HEAD(&dev->vbiq.queued);
1859 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1860 dev->vbiq.timeout.data = (unsigned long)dev;
1861 init_timer(&dev->vbiq.timeout);
1862 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1863 MO_VID_DMACNTRL,0x88,0x00);
1866 err = request_irq(pci_dev->irq, cx8800_irq,
1867 SA_SHIRQ | SA_INTERRUPT, core->name, dev);
1869 printk(KERN_ERR "%s: can't get IRQ %d\n",
1870 core->name,pci_dev->irq);
1873 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1875 /* load and configure helper modules */
1876 if (TUNER_ABSENT != core->tuner_type)
1877 request_module("tuner");
1878 if (core->tda9887_conf)
1879 request_module("tda9887");
1881 /* register v4l devices */
1882 dev->video_dev = cx88_vdev_init(core,dev->pci,
1883 &cx8800_video_template,"video");
1884 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1885 video_nr[core->nr]);
1887 printk(KERN_INFO "%s: can't register video device\n",
1891 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1892 core->name,dev->video_dev->minor & 0x1f);
1894 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1895 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1898 printk(KERN_INFO "%s/0: can't register vbi device\n",
1902 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1903 core->name,dev->vbi_dev->minor & 0x1f);
1905 if (core->has_radio) {
1906 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1907 &cx8800_radio_template,"radio");
1908 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1909 radio_nr[core->nr]);
1911 printk(KERN_INFO "%s/0: can't register radio device\n",
1915 printk(KERN_INFO "%s/0: registered device radio%d\n",
1916 core->name,dev->radio_dev->minor & 0x1f);
1919 /* everything worked */
1920 list_add_tail(&dev->devlist,&cx8800_devlist);
1921 pci_set_drvdata(pci_dev,dev);
1923 /* initial device configuration */
1925 init_controls(core);
1926 cx88_set_tvnorm(core,tvnorms);
1930 /* start tvaudio thread */
1931 if (core->tuner_type != TUNER_ABSENT)
1932 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
1936 cx8800_unregister_video(dev);
1937 free_irq(pci_dev->irq, dev);
1939 cx88_core_put(core,dev->pci);
1945 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1947 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1948 struct cx88_core *core = dev->core;
1951 if (core->kthread) {
1952 kthread_stop(core->kthread);
1953 core->kthread = NULL;
1956 cx88_shutdown(core); /* FIXME */
1957 pci_disable_device(pci_dev);
1959 /* unregister stuff */
1961 free_irq(pci_dev->irq, dev);
1962 cx8800_unregister_video(dev);
1963 pci_set_drvdata(pci_dev, NULL);
1966 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1967 list_del(&dev->devlist);
1968 cx88_core_put(core,dev->pci);
1972 static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1974 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1975 struct cx88_core *core = dev->core;
1977 /* stop video+vbi capture */
1978 spin_lock(&dev->slock);
1979 if (!list_empty(&dev->vidq.active)) {
1980 printk("%s: suspend video\n", core->name);
1981 stop_video_dma(dev);
1982 del_timer(&dev->vidq.timeout);
1984 if (!list_empty(&dev->vbiq.active)) {
1985 printk("%s: suspend vbi\n", core->name);
1986 cx8800_stop_vbi_dma(dev);
1987 del_timer(&dev->vbiq.timeout);
1989 spin_unlock(&dev->slock);
1991 /* FIXME -- shutdown device */
1992 cx88_shutdown(core);
1994 pci_save_state(pci_dev);
1995 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1996 pci_disable_device(pci_dev);
1997 dev->state.disabled = 1;
2002 static int cx8800_resume(struct pci_dev *pci_dev)
2004 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2005 struct cx88_core *core = dev->core;
2008 if (dev->state.disabled) {
2009 err=pci_enable_device(pci_dev);
2011 printk(KERN_ERR "%s: can't enable device\n",
2016 dev->state.disabled = 0;
2018 err= pci_set_power_state(pci_dev, PCI_D0);
2020 printk(KERN_ERR "%s: can't enable device\n",
2023 pci_disable_device(pci_dev);
2024 dev->state.disabled = 1;
2028 pci_restore_state(pci_dev);
2030 /* FIXME: re-initialize hardware */
2033 /* restart video+vbi capture */
2034 spin_lock(&dev->slock);
2035 if (!list_empty(&dev->vidq.active)) {
2036 printk("%s: resume video\n", core->name);
2037 restart_video_queue(dev,&dev->vidq);
2039 if (!list_empty(&dev->vbiq.active)) {
2040 printk("%s: resume vbi\n", core->name);
2041 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2043 spin_unlock(&dev->slock);
2048 /* ----------------------------------------------------------- */
2050 static struct pci_device_id cx8800_pci_tbl[] = {
2054 .subvendor = PCI_ANY_ID,
2055 .subdevice = PCI_ANY_ID,
2057 /* --- end of list --- */
2060 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2062 static struct pci_driver cx8800_pci_driver = {
2064 .id_table = cx8800_pci_tbl,
2065 .probe = cx8800_initdev,
2066 .remove = __devexit_p(cx8800_finidev),
2068 .suspend = cx8800_suspend,
2069 .resume = cx8800_resume,
2072 static int cx8800_init(void)
2074 printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
2075 (CX88_VERSION_CODE >> 16) & 0xff,
2076 (CX88_VERSION_CODE >> 8) & 0xff,
2077 CX88_VERSION_CODE & 0xff);
2079 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2080 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2082 return pci_register_driver(&cx8800_pci_driver);
2085 static void cx8800_fini(void)
2087 pci_unregister_driver(&cx8800_pci_driver);
2090 module_init(cx8800_init);
2091 module_exit(cx8800_fini);
2093 EXPORT_SYMBOL(cx88_do_ioctl);
2095 /* ----------------------------------------------------------- */
2100 * 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