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 *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 if (video_debug > 1)
1260 cx88_print_ioctl(core->name,cmd);
1261 printk( KERN_INFO "CORE IOCTL: 0x%x\n", cmd );
1262 cx88_print_ioctl(core->name,cmd);
1263 dprintk( 1, "CORE IOCTL: 0x%x\n", cmd );
1266 /* ---------- tv norms ---------- */
1267 case VIDIOC_ENUMSTD:
1269 struct v4l2_standard *e = arg;
1273 if (i >= ARRAY_SIZE(tvnorms))
1275 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1276 tvnorms[e->index].name);
1284 v4l2_std_id *id = arg;
1286 *id = core->tvnorm->id;
1291 v4l2_std_id *id = arg;
1294 for(i = 0; i < ARRAY_SIZE(tvnorms); i++)
1295 if (*id & tvnorms[i].id)
1297 if (i == ARRAY_SIZE(tvnorms))
1301 cx88_set_tvnorm(core,&tvnorms[i]);
1306 /* ------ input switching ---------- */
1307 case VIDIOC_ENUMINPUT:
1309 static const char *iname[] = {
1310 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1311 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1312 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1313 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1314 [ CX88_VMUX_SVIDEO ] = "S-Video",
1315 [ CX88_VMUX_TELEVISION ] = "Television",
1316 [ CX88_VMUX_CABLE ] = "Cable TV",
1317 [ CX88_VMUX_DVB ] = "DVB",
1318 [ CX88_VMUX_DEBUG ] = "for debug only",
1320 struct v4l2_input *i = arg;
1326 if (0 == INPUT(n)->type)
1328 memset(i,0,sizeof(*i));
1330 i->type = V4L2_INPUT_TYPE_CAMERA;
1331 strcpy(i->name,iname[INPUT(n)->type]);
1332 if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
1333 (CX88_VMUX_CABLE == INPUT(n)->type))
1334 i->type = V4L2_INPUT_TYPE_TUNER;
1335 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1336 i->std |= tvnorms[n].id;
1339 case VIDIOC_G_INPUT:
1341 unsigned int *i = arg;
1346 case VIDIOC_S_INPUT:
1348 unsigned int *i = arg;
1353 cx88_newstation(core);
1361 /* --- controls ---------------------------------------------- */
1362 case VIDIOC_QUERYCTRL:
1364 struct v4l2_queryctrl *c = arg;
1367 if (c->id < V4L2_CID_BASE ||
1368 c->id >= V4L2_CID_LASTP1)
1370 for (i = 0; i < CX8800_CTLS; i++)
1371 if (cx8800_ctls[i].v.id == c->id)
1373 if (i == CX8800_CTLS) {
1377 *c = cx8800_ctls[i].v;
1381 return get_control(core,arg);
1383 return set_control(core,arg);
1385 /* --- tuner ioctls ------------------------------------------ */
1386 case VIDIOC_G_TUNER:
1388 struct v4l2_tuner *t = arg;
1391 if (UNSET == core->tuner_type)
1396 memset(t,0,sizeof(*t));
1397 strcpy(t->name, "Television");
1398 t->type = V4L2_TUNER_ANALOG_TV;
1399 t->capability = V4L2_TUNER_CAP_NORM;
1400 t->rangehigh = 0xffffffffUL;
1402 cx88_get_stereo(core ,t);
1403 reg = cx_read(MO_DEVICE_STATUS);
1404 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1407 case VIDIOC_S_TUNER:
1409 struct v4l2_tuner *t = arg;
1411 if (UNSET == core->tuner_type)
1415 cx88_set_stereo(core, t->audmode, 1);
1418 case VIDIOC_G_FREQUENCY:
1420 struct v4l2_frequency *f = arg;
1422 memset(f,0,sizeof(*f));
1424 if (UNSET == core->tuner_type)
1427 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1428 f->type = radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1429 f->frequency = core->freq;
1431 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1435 case VIDIOC_S_FREQUENCY:
1437 struct v4l2_frequency *f = arg;
1439 if (UNSET == core->tuner_type)
1443 if (0 == radio && f->type != V4L2_TUNER_ANALOG_TV)
1445 if (1 == radio && f->type != V4L2_TUNER_RADIO)
1448 core->freq = f->frequency;
1449 cx88_newstation(core);
1450 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1452 /* When changing channels it is required to reset TVAUDIO */
1454 cx88_set_tvaudio(core);
1461 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1467 static int video_ioctl(struct inode *inode, struct file *file,
1468 unsigned int cmd, unsigned long arg)
1470 return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
1473 /* ----------------------------------------------------------- */
1475 static int radio_do_ioctl(struct inode *inode, struct file *file,
1476 unsigned int cmd, void *arg)
1478 struct cx8800_fh *fh = file->private_data;
1479 struct cx8800_dev *dev = fh->dev;
1480 struct cx88_core *core = dev->core;
1482 if (video_debug > 1)
1483 cx88_print_ioctl(core->name,cmd);
1486 case VIDIOC_QUERYCAP:
1488 struct v4l2_capability *cap = arg;
1490 memset(cap,0,sizeof(*cap));
1491 strcpy(cap->driver, "cx8800");
1492 strlcpy(cap->card, cx88_boards[core->board].name,
1494 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1495 cap->version = CX88_VERSION_CODE;
1496 cap->capabilities = V4L2_CAP_TUNER;
1499 case VIDIOC_G_TUNER:
1501 struct v4l2_tuner *t = arg;
1506 memset(t,0,sizeof(*t));
1507 strcpy(t->name, "Radio");
1509 cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
1512 case VIDIOC_ENUMINPUT:
1514 struct v4l2_input *i = arg;
1518 strcpy(i->name,"Radio");
1519 i->type = V4L2_INPUT_TYPE_TUNER;
1522 case VIDIOC_G_INPUT:
1528 case VIDIOC_G_AUDIO:
1530 struct v4l2_audio *a = arg;
1532 memset(a,0,sizeof(*a));
1533 strcpy(a->name,"Radio");
1538 v4l2_std_id *id = arg;
1544 struct video_tuner *v = arg;
1546 if (v->tuner) /* Only tuner 0 */
1549 cx88_call_i2c_clients(core,VIDIOCSTUNER,v);
1552 case VIDIOC_S_TUNER:
1554 struct v4l2_tuner *t = arg;
1559 cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1564 case VIDIOC_S_AUDIO:
1565 case VIDIOC_S_INPUT:
1569 case VIDIOC_QUERYCTRL:
1571 struct v4l2_queryctrl *c = arg;
1574 if (c->id < V4L2_CID_BASE ||
1575 c->id >= V4L2_CID_LASTP1)
1577 if (c->id == V4L2_CID_AUDIO_MUTE) {
1578 for (i = 0; i < CX8800_CTLS; i++)
1579 if (cx8800_ctls[i].v.id == c->id)
1581 *c = cx8800_ctls[i].v;
1590 case VIDIOC_G_FREQUENCY:
1591 case VIDIOC_S_FREQUENCY:
1592 return video_do_ioctl(inode,file,cmd,arg);
1595 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1601 static int radio_ioctl(struct inode *inode, struct file *file,
1602 unsigned int cmd, unsigned long arg)
1604 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
1607 /* ----------------------------------------------------------- */
1609 static void cx8800_vid_timeout(unsigned long data)
1611 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1612 struct cx88_core *core = dev->core;
1613 struct cx88_dmaqueue *q = &dev->vidq;
1614 struct cx88_buffer *buf;
1615 unsigned long flags;
1617 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1619 cx_clear(MO_VID_DMACNTRL, 0x11);
1620 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1622 spin_lock_irqsave(&dev->slock,flags);
1623 while (!list_empty(&q->active)) {
1624 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1625 list_del(&buf->vb.queue);
1626 buf->vb.state = STATE_ERROR;
1627 wake_up(&buf->vb.done);
1628 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1629 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1631 restart_video_queue(dev,q);
1632 spin_unlock_irqrestore(&dev->slock,flags);
1635 static char *cx88_vid_irqs[32] = {
1636 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1637 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1638 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1639 "y_sync", "u_sync", "v_sync", "vbi_sync",
1640 "opc_err", "par_err", "rip_err", "pci_abort",
1643 static void cx8800_vid_irq(struct cx8800_dev *dev)
1645 struct cx88_core *core = dev->core;
1646 u32 status, mask, count;
1648 status = cx_read(MO_VID_INTSTAT);
1649 mask = cx_read(MO_VID_INTMSK);
1650 if (0 == (status & mask))
1652 cx_write(MO_VID_INTSTAT, status);
1653 if (irq_debug || (status & mask & ~0xff))
1654 cx88_print_irqbits(core->name, "irq vid",
1655 cx88_vid_irqs, status, mask);
1657 /* risc op code error */
1658 if (status & (1 << 16)) {
1659 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1660 cx_clear(MO_VID_DMACNTRL, 0x11);
1661 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1662 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1666 if (status & 0x01) {
1667 spin_lock(&dev->slock);
1668 count = cx_read(MO_VIDY_GPCNT);
1669 cx88_wakeup(core, &dev->vidq, count);
1670 spin_unlock(&dev->slock);
1674 if (status & 0x08) {
1675 spin_lock(&dev->slock);
1676 count = cx_read(MO_VBI_GPCNT);
1677 cx88_wakeup(core, &dev->vbiq, count);
1678 spin_unlock(&dev->slock);
1682 if (status & 0x10) {
1683 dprintk(2,"stopper video\n");
1684 spin_lock(&dev->slock);
1685 restart_video_queue(dev,&dev->vidq);
1686 spin_unlock(&dev->slock);
1690 if (status & 0x80) {
1691 dprintk(2,"stopper vbi\n");
1692 spin_lock(&dev->slock);
1693 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1694 spin_unlock(&dev->slock);
1698 static irqreturn_t cx8800_irq(int irq, void *dev_id, struct pt_regs *regs)
1700 struct cx8800_dev *dev = dev_id;
1701 struct cx88_core *core = dev->core;
1703 int loop, handled = 0;
1705 for (loop = 0; loop < 10; loop++) {
1706 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
1709 cx_write(MO_PCI_INTSTAT, status);
1712 if (status & core->pci_irqmask)
1713 cx88_core_irq(core,status);
1715 cx8800_vid_irq(dev);
1718 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1720 cx_write(MO_PCI_INTMSK,0);
1724 return IRQ_RETVAL(handled);
1727 /* ----------------------------------------------------------- */
1728 /* exported stuff */
1730 static struct file_operations video_fops =
1732 .owner = THIS_MODULE,
1734 .release = video_release,
1738 .ioctl = video_ioctl,
1739 .llseek = no_llseek,
1742 static struct video_device cx8800_video_template =
1744 .name = "cx8800-video",
1745 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1747 .fops = &video_fops,
1751 static struct video_device cx8800_vbi_template =
1753 .name = "cx8800-vbi",
1754 .type = VID_TYPE_TELETEXT|VID_TYPE_TUNER,
1756 .fops = &video_fops,
1760 static struct file_operations radio_fops =
1762 .owner = THIS_MODULE,
1764 .release = video_release,
1765 .ioctl = radio_ioctl,
1766 .llseek = no_llseek,
1769 static struct video_device cx8800_radio_template =
1771 .name = "cx8800-radio",
1772 .type = VID_TYPE_TUNER,
1774 .fops = &radio_fops,
1778 /* ----------------------------------------------------------- */
1780 static void cx8800_unregister_video(struct cx8800_dev *dev)
1782 if (dev->radio_dev) {
1783 if (-1 != dev->radio_dev->minor)
1784 video_unregister_device(dev->radio_dev);
1786 video_device_release(dev->radio_dev);
1787 dev->radio_dev = NULL;
1790 if (-1 != dev->vbi_dev->minor)
1791 video_unregister_device(dev->vbi_dev);
1793 video_device_release(dev->vbi_dev);
1794 dev->vbi_dev = NULL;
1796 if (dev->video_dev) {
1797 if (-1 != dev->video_dev->minor)
1798 video_unregister_device(dev->video_dev);
1800 video_device_release(dev->video_dev);
1801 dev->video_dev = NULL;
1805 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1806 const struct pci_device_id *pci_id)
1808 struct cx8800_dev *dev;
1809 struct cx88_core *core;
1812 dev = kmalloc(sizeof(*dev),GFP_KERNEL);
1815 memset(dev,0,sizeof(*dev));
1819 if (pci_enable_device(pci_dev)) {
1823 core = cx88_core_get(dev->pci);
1830 /* print pci info */
1831 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
1832 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1833 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1834 "latency: %d, mmio: 0x%lx\n", core->name,
1835 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1836 dev->pci_lat,pci_resource_start(pci_dev,0));
1838 pci_set_master(pci_dev);
1839 if (!pci_dma_supported(pci_dev,0xffffffff)) {
1840 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1845 /* initialize driver struct */
1846 spin_lock_init(&dev->slock);
1847 core->tvnorm = tvnorms;
1849 /* init video dma queues */
1850 INIT_LIST_HEAD(&dev->vidq.active);
1851 INIT_LIST_HEAD(&dev->vidq.queued);
1852 dev->vidq.timeout.function = cx8800_vid_timeout;
1853 dev->vidq.timeout.data = (unsigned long)dev;
1854 init_timer(&dev->vidq.timeout);
1855 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1856 MO_VID_DMACNTRL,0x11,0x00);
1858 /* init vbi dma queues */
1859 INIT_LIST_HEAD(&dev->vbiq.active);
1860 INIT_LIST_HEAD(&dev->vbiq.queued);
1861 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1862 dev->vbiq.timeout.data = (unsigned long)dev;
1863 init_timer(&dev->vbiq.timeout);
1864 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1865 MO_VID_DMACNTRL,0x88,0x00);
1868 err = request_irq(pci_dev->irq, cx8800_irq,
1869 SA_SHIRQ | SA_INTERRUPT, core->name, dev);
1871 printk(KERN_ERR "%s: can't get IRQ %d\n",
1872 core->name,pci_dev->irq);
1875 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1877 /* load and configure helper modules */
1878 if (TUNER_ABSENT != core->tuner_type)
1879 request_module("tuner");
1880 if (core->tda9887_conf)
1881 request_module("tda9887");
1883 /* register v4l devices */
1884 dev->video_dev = cx88_vdev_init(core,dev->pci,
1885 &cx8800_video_template,"video");
1886 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1887 video_nr[core->nr]);
1889 printk(KERN_INFO "%s: can't register video device\n",
1893 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1894 core->name,dev->video_dev->minor & 0x1f);
1896 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1897 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1900 printk(KERN_INFO "%s/0: can't register vbi device\n",
1904 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1905 core->name,dev->vbi_dev->minor & 0x1f);
1907 if (core->has_radio) {
1908 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1909 &cx8800_radio_template,"radio");
1910 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1911 radio_nr[core->nr]);
1913 printk(KERN_INFO "%s/0: can't register radio device\n",
1917 printk(KERN_INFO "%s/0: registered device radio%d\n",
1918 core->name,dev->radio_dev->minor & 0x1f);
1921 /* everything worked */
1922 list_add_tail(&dev->devlist,&cx8800_devlist);
1923 pci_set_drvdata(pci_dev,dev);
1925 /* initial device configuration */
1927 init_controls(core);
1928 cx88_set_tvnorm(core,tvnorms);
1932 /* start tvaudio thread */
1933 if (core->tuner_type != TUNER_ABSENT)
1934 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
1938 cx8800_unregister_video(dev);
1939 free_irq(pci_dev->irq, dev);
1941 cx88_core_put(core,dev->pci);
1947 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1949 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1950 struct cx88_core *core = dev->core;
1953 if (core->kthread) {
1954 kthread_stop(core->kthread);
1955 core->kthread = NULL;
1958 cx88_shutdown(core); /* FIXME */
1959 pci_disable_device(pci_dev);
1961 /* unregister stuff */
1963 free_irq(pci_dev->irq, dev);
1964 cx8800_unregister_video(dev);
1965 pci_set_drvdata(pci_dev, NULL);
1968 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1969 list_del(&dev->devlist);
1970 cx88_core_put(core,dev->pci);
1974 static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1976 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1977 struct cx88_core *core = dev->core;
1979 /* stop video+vbi capture */
1980 spin_lock(&dev->slock);
1981 if (!list_empty(&dev->vidq.active)) {
1982 printk("%s: suspend video\n", core->name);
1983 stop_video_dma(dev);
1984 del_timer(&dev->vidq.timeout);
1986 if (!list_empty(&dev->vbiq.active)) {
1987 printk("%s: suspend vbi\n", core->name);
1988 cx8800_stop_vbi_dma(dev);
1989 del_timer(&dev->vbiq.timeout);
1991 spin_unlock(&dev->slock);
1993 /* FIXME -- shutdown device */
1994 cx88_shutdown(core);
1996 pci_save_state(pci_dev);
1997 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1998 pci_disable_device(pci_dev);
1999 dev->state.disabled = 1;
2004 static int cx8800_resume(struct pci_dev *pci_dev)
2006 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2007 struct cx88_core *core = dev->core;
2010 if (dev->state.disabled) {
2011 err=pci_enable_device(pci_dev);
2013 printk(KERN_ERR "%s: can't enable device\n",
2018 dev->state.disabled = 0;
2020 err= pci_set_power_state(pci_dev, PCI_D0);
2022 printk(KERN_ERR "%s: can't enable device\n",
2025 pci_disable_device(pci_dev);
2026 dev->state.disabled = 1;
2030 pci_restore_state(pci_dev);
2032 /* FIXME: re-initialize hardware */
2035 /* restart video+vbi capture */
2036 spin_lock(&dev->slock);
2037 if (!list_empty(&dev->vidq.active)) {
2038 printk("%s: resume video\n", core->name);
2039 restart_video_queue(dev,&dev->vidq);
2041 if (!list_empty(&dev->vbiq.active)) {
2042 printk("%s: resume vbi\n", core->name);
2043 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2045 spin_unlock(&dev->slock);
2050 /* ----------------------------------------------------------- */
2052 static struct pci_device_id cx8800_pci_tbl[] = {
2056 .subvendor = PCI_ANY_ID,
2057 .subdevice = PCI_ANY_ID,
2059 /* --- end of list --- */
2062 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2064 static struct pci_driver cx8800_pci_driver = {
2066 .id_table = cx8800_pci_tbl,
2067 .probe = cx8800_initdev,
2068 .remove = __devexit_p(cx8800_finidev),
2070 .suspend = cx8800_suspend,
2071 .resume = cx8800_resume,
2074 static int cx8800_init(void)
2076 printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
2077 (CX88_VERSION_CODE >> 16) & 0xff,
2078 (CX88_VERSION_CODE >> 8) & 0xff,
2079 CX88_VERSION_CODE & 0xff);
2081 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2082 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2084 return pci_register_driver(&cx8800_pci_driver);
2087 static void cx8800_fini(void)
2089 pci_unregister_driver(&cx8800_pci_driver);
2092 module_init(cx8800_init);
2093 module_exit(cx8800_fini);
2095 EXPORT_SYMBOL(cx88_do_ioctl);
2097 /* ----------------------------------------------------------- */
2102 * 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