2 * $Id: cx88-video.c,v 1.80 2005/07/13 08:49:08 mchehab Exp $
4 * device driver for Conexant 2388x based TV cards
5 * video4linux video interface
7 * (c) 2003-04 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/kmod.h>
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/delay.h>
33 #include <linux/kthread.h>
34 #include <asm/div64.h>
38 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
39 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
40 MODULE_LICENSE("GPL");
42 /* ------------------------------------------------------------------ */
44 static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
45 static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
46 static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
48 module_param_array(video_nr, int, NULL, 0444);
49 module_param_array(vbi_nr, int, NULL, 0444);
50 module_param_array(radio_nr, int, NULL, 0444);
52 MODULE_PARM_DESC(video_nr,"video device numbers");
53 MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
54 MODULE_PARM_DESC(radio_nr,"radio device numbers");
56 static unsigned int video_debug = 0;
57 module_param(video_debug,int,0644);
58 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
60 static unsigned int irq_debug = 0;
61 module_param(irq_debug,int,0644);
62 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
64 static unsigned int vid_limit = 16;
65 module_param(vid_limit,int,0644);
66 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
68 #define dprintk(level,fmt, arg...) if (video_debug >= level) \
69 printk(KERN_DEBUG "%s/0: " fmt, dev->core->name , ## arg)
71 /* ------------------------------------------------------------------ */
73 static LIST_HEAD(cx8800_devlist);
75 /* ------------------------------------------------------------------- */
78 static struct cx88_tvnorm tvnorms[] = {
81 .id = V4L2_STD_NTSC_M,
82 .cxiformat = VideoFormatNTSC,
83 .cxoformat = 0x181f0008,
86 .id = V4L2_STD_NTSC_M_JP,
87 .cxiformat = VideoFormatNTSCJapan,
88 .cxoformat = 0x181f0008,
91 .id = V4L2_STD_PAL_BG,
92 .cxiformat = VideoFormatPAL,
93 .cxoformat = 0x181f0008,
96 .id = V4L2_STD_PAL_DK,
97 .cxiformat = VideoFormatPAL,
98 .cxoformat = 0x181f0008,
101 .id = V4L2_STD_PAL_I,
102 .cxiformat = VideoFormatPAL,
103 .cxoformat = 0x181f0008,
106 .id = V4L2_STD_PAL_M,
107 .cxiformat = VideoFormatPALM,
108 .cxoformat = 0x1c1f0008,
111 .id = V4L2_STD_PAL_N,
112 .cxiformat = VideoFormatPALN,
113 .cxoformat = 0x1c1f0008,
116 .id = V4L2_STD_PAL_Nc,
117 .cxiformat = VideoFormatPALNC,
118 .cxoformat = 0x1c1f0008,
121 .id = V4L2_STD_PAL_60,
122 .cxiformat = VideoFormatPAL60,
123 .cxoformat = 0x181f0008,
126 .id = V4L2_STD_SECAM_L,
127 .cxiformat = VideoFormatSECAM,
128 .cxoformat = 0x181f0008,
131 .id = V4L2_STD_SECAM_DK,
132 .cxiformat = VideoFormatSECAM,
133 .cxoformat = 0x181f0008,
137 static struct cx8800_fmt formats[] = {
139 .name = "8 bpp, gray",
140 .fourcc = V4L2_PIX_FMT_GREY,
141 .cxformat = ColorFormatY8,
143 .flags = FORMAT_FLAGS_PACKED,
145 .name = "15 bpp RGB, le",
146 .fourcc = V4L2_PIX_FMT_RGB555,
147 .cxformat = ColorFormatRGB15,
149 .flags = FORMAT_FLAGS_PACKED,
151 .name = "15 bpp RGB, be",
152 .fourcc = V4L2_PIX_FMT_RGB555X,
153 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
155 .flags = FORMAT_FLAGS_PACKED,
157 .name = "16 bpp RGB, le",
158 .fourcc = V4L2_PIX_FMT_RGB565,
159 .cxformat = ColorFormatRGB16,
161 .flags = FORMAT_FLAGS_PACKED,
163 .name = "16 bpp RGB, be",
164 .fourcc = V4L2_PIX_FMT_RGB565X,
165 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
167 .flags = FORMAT_FLAGS_PACKED,
169 .name = "24 bpp RGB, le",
170 .fourcc = V4L2_PIX_FMT_BGR24,
171 .cxformat = ColorFormatRGB24,
173 .flags = FORMAT_FLAGS_PACKED,
175 .name = "32 bpp RGB, le",
176 .fourcc = V4L2_PIX_FMT_BGR32,
177 .cxformat = ColorFormatRGB32,
179 .flags = FORMAT_FLAGS_PACKED,
181 .name = "32 bpp RGB, be",
182 .fourcc = V4L2_PIX_FMT_RGB32,
183 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
185 .flags = FORMAT_FLAGS_PACKED,
187 .name = "4:2:2, packed, YUYV",
188 .fourcc = V4L2_PIX_FMT_YUYV,
189 .cxformat = ColorFormatYUY2,
191 .flags = FORMAT_FLAGS_PACKED,
193 .name = "4:2:2, packed, UYVY",
194 .fourcc = V4L2_PIX_FMT_UYVY,
195 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
197 .flags = FORMAT_FLAGS_PACKED,
201 static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
205 for (i = 0; i < ARRAY_SIZE(formats); i++)
206 if (formats[i].fourcc == fourcc)
211 /* ------------------------------------------------------------------- */
213 static const struct v4l2_queryctrl no_ctl = {
215 .flags = V4L2_CTRL_FLAG_DISABLED,
218 static struct cx88_ctrl cx8800_ctls[] = {
222 .id = V4L2_CID_BRIGHTNESS,
223 .name = "Brightness",
228 .type = V4L2_CTRL_TYPE_INTEGER,
231 .reg = MO_CONTR_BRIGHT,
236 .id = V4L2_CID_CONTRAST,
242 .type = V4L2_CTRL_TYPE_INTEGER,
245 .reg = MO_CONTR_BRIGHT,
256 .type = V4L2_CTRL_TYPE_INTEGER,
263 /* strictly, this only describes only U saturation.
264 * V saturation is handled specially through code.
267 .id = V4L2_CID_SATURATION,
268 .name = "Saturation",
273 .type = V4L2_CTRL_TYPE_INTEGER,
276 .reg = MO_UV_SATURATION,
282 .id = V4L2_CID_AUDIO_MUTE,
286 .type = V4L2_CTRL_TYPE_BOOLEAN,
289 .sreg = SHADOW_AUD_VOL_CTL,
294 .id = V4L2_CID_AUDIO_VOLUME,
300 .type = V4L2_CTRL_TYPE_INTEGER,
303 .sreg = SHADOW_AUD_VOL_CTL,
308 .id = V4L2_CID_AUDIO_BALANCE,
313 .default_value = 0x40,
314 .type = V4L2_CTRL_TYPE_INTEGER,
317 .sreg = SHADOW_AUD_BAL_CTL,
322 static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
324 /* ------------------------------------------------------------------- */
325 /* resource management */
327 static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
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 if ((fh->resources & bits) != bits)
367 fh->resources &= ~bits;
368 dev->resources &= ~bits;
369 dprintk(1,"res: put %d\n",bits);
373 /* ------------------------------------------------------------------ */
375 static int video_mux(struct cx8800_dev *dev, unsigned int input)
377 struct cx88_core *core = dev->core;
379 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
380 input, INPUT(input)->vmux,
381 INPUT(input)->gpio0,INPUT(input)->gpio1,
382 INPUT(input)->gpio2,INPUT(input)->gpio3);
383 dev->core->input = input;
384 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input)->vmux << 14);
385 cx_write(MO_GP3_IO, INPUT(input)->gpio3);
386 cx_write(MO_GP0_IO, INPUT(input)->gpio0);
387 cx_write(MO_GP1_IO, INPUT(input)->gpio1);
388 cx_write(MO_GP2_IO, INPUT(input)->gpio2);
390 switch (INPUT(input)->type) {
391 case CX88_VMUX_SVIDEO:
392 cx_set(MO_AFECFG_IO, 0x00000001);
393 cx_set(MO_INPUT_FORMAT, 0x00010010);
394 cx_set(MO_FILTER_EVEN, 0x00002020);
395 cx_set(MO_FILTER_ODD, 0x00002020);
398 cx_clear(MO_AFECFG_IO, 0x00000001);
399 cx_clear(MO_INPUT_FORMAT, 0x00010010);
400 cx_clear(MO_FILTER_EVEN, 0x00002020);
401 cx_clear(MO_FILTER_ODD, 0x00002020);
407 /* ------------------------------------------------------------------ */
409 static int start_video_dma(struct cx8800_dev *dev,
410 struct cx88_dmaqueue *q,
411 struct cx88_buffer *buf)
413 struct cx88_core *core = dev->core;
415 /* setup fifo + format */
416 cx88_sram_channel_setup(dev->core, &cx88_sram_channels[SRAM_CH21],
417 buf->bpl, buf->risc.dma);
418 cx88_set_scale(dev->core, buf->vb.width, buf->vb.height, buf->vb.field);
419 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
422 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
426 cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
427 cx_set(MO_VID_INTMSK, 0x0f0011);
430 cx_set(VID_CAPTURE_CONTROL,0x06);
433 cx_set(MO_DEV_CNTRL2, (1<<5));
434 cx_set(MO_VID_DMACNTRL, 0x11);
439 static int stop_video_dma(struct cx8800_dev *dev)
441 struct cx88_core *core = dev->core;
444 cx_clear(MO_VID_DMACNTRL, 0x11);
446 /* disable capture */
447 cx_clear(VID_CAPTURE_CONTROL,0x06);
450 cx_clear(MO_PCI_INTMSK, 0x000001);
451 cx_clear(MO_VID_INTMSK, 0x0f0011);
455 static int restart_video_queue(struct cx8800_dev *dev,
456 struct cx88_dmaqueue *q)
458 struct cx88_buffer *buf, *prev;
459 struct list_head *item;
461 if (!list_empty(&q->active)) {
462 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
463 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
465 start_video_dma(dev, q, buf);
466 list_for_each(item,&q->active) {
467 buf = list_entry(item, struct cx88_buffer, vb.queue);
468 buf->count = q->count++;
470 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
476 if (list_empty(&q->queued))
478 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
480 list_del(&buf->vb.queue);
481 list_add_tail(&buf->vb.queue,&q->active);
482 start_video_dma(dev, q, buf);
483 buf->vb.state = STATE_ACTIVE;
484 buf->count = q->count++;
485 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
486 dprintk(2,"[%p/%d] restart_queue - first active\n",
489 } else if (prev->vb.width == buf->vb.width &&
490 prev->vb.height == buf->vb.height &&
491 prev->fmt == buf->fmt) {
492 list_del(&buf->vb.queue);
493 list_add_tail(&buf->vb.queue,&q->active);
494 buf->vb.state = STATE_ACTIVE;
495 buf->count = q->count++;
496 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
497 dprintk(2,"[%p/%d] restart_queue - move to active\n",
506 /* ------------------------------------------------------------------ */
509 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
511 struct cx8800_fh *fh = q->priv_data;
513 *size = fh->fmt->depth*fh->width*fh->height >> 3;
516 while (*size * *count > vid_limit * 1024 * 1024)
522 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
523 enum v4l2_field field)
525 struct cx8800_fh *fh = q->priv_data;
526 struct cx8800_dev *dev = fh->dev;
527 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
528 int rc, init_buffer = 0;
530 BUG_ON(NULL == fh->fmt);
531 if (fh->width < 48 || fh->width > norm_maxw(dev->core->tvnorm) ||
532 fh->height < 32 || fh->height > norm_maxh(dev->core->tvnorm))
534 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
535 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
538 if (buf->fmt != fh->fmt ||
539 buf->vb.width != fh->width ||
540 buf->vb.height != fh->height ||
541 buf->vb.field != field) {
543 buf->vb.width = fh->width;
544 buf->vb.height = fh->height;
545 buf->vb.field = field;
549 if (STATE_NEEDS_INIT == buf->vb.state) {
551 if (0 != (rc = videobuf_iolock(dev->pci,&buf->vb,NULL)))
556 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
557 switch (buf->vb.field) {
559 cx88_risc_buffer(dev->pci, &buf->risc,
560 buf->vb.dma.sglist, 0, UNSET,
561 buf->bpl, 0, buf->vb.height);
563 case V4L2_FIELD_BOTTOM:
564 cx88_risc_buffer(dev->pci, &buf->risc,
565 buf->vb.dma.sglist, UNSET, 0,
566 buf->bpl, 0, buf->vb.height);
568 case V4L2_FIELD_INTERLACED:
569 cx88_risc_buffer(dev->pci, &buf->risc,
570 buf->vb.dma.sglist, 0, buf->bpl,
572 buf->vb.height >> 1);
574 case V4L2_FIELD_SEQ_TB:
575 cx88_risc_buffer(dev->pci, &buf->risc,
577 0, buf->bpl * (buf->vb.height >> 1),
579 buf->vb.height >> 1);
581 case V4L2_FIELD_SEQ_BT:
582 cx88_risc_buffer(dev->pci, &buf->risc,
584 buf->bpl * (buf->vb.height >> 1), 0,
586 buf->vb.height >> 1);
592 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
594 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
595 (unsigned long)buf->risc.dma);
597 buf->vb.state = STATE_PREPARED;
601 cx88_free_buffer(dev->pci,buf);
606 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
608 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
609 struct cx88_buffer *prev;
610 struct cx8800_fh *fh = vq->priv_data;
611 struct cx8800_dev *dev = fh->dev;
612 struct cx88_dmaqueue *q = &dev->vidq;
614 /* add jump to stopper */
615 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
616 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
618 if (!list_empty(&q->queued)) {
619 list_add_tail(&buf->vb.queue,&q->queued);
620 buf->vb.state = STATE_QUEUED;
621 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
624 } else if (list_empty(&q->active)) {
625 list_add_tail(&buf->vb.queue,&q->active);
626 start_video_dma(dev, q, buf);
627 buf->vb.state = STATE_ACTIVE;
628 buf->count = q->count++;
629 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
630 dprintk(2,"[%p/%d] buffer_queue - first active\n",
634 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
635 if (prev->vb.width == buf->vb.width &&
636 prev->vb.height == buf->vb.height &&
637 prev->fmt == buf->fmt) {
638 list_add_tail(&buf->vb.queue,&q->active);
639 buf->vb.state = STATE_ACTIVE;
640 buf->count = q->count++;
641 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
642 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
646 list_add_tail(&buf->vb.queue,&q->queued);
647 buf->vb.state = STATE_QUEUED;
648 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
654 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
656 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
657 struct cx8800_fh *fh = q->priv_data;
659 cx88_free_buffer(fh->dev->pci,buf);
662 static struct videobuf_queue_ops cx8800_video_qops = {
663 .buf_setup = buffer_setup,
664 .buf_prepare = buffer_prepare,
665 .buf_queue = buffer_queue,
666 .buf_release = buffer_release,
669 /* ------------------------------------------------------------------ */
672 /* ------------------------------------------------------------------ */
674 static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
677 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
679 case V4L2_BUF_TYPE_VBI_CAPTURE:
687 static int get_ressource(struct cx8800_fh *fh)
690 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
691 return RESOURCE_VIDEO;
692 case V4L2_BUF_TYPE_VBI_CAPTURE:
700 static int video_open(struct inode *inode, struct file *file)
702 int minor = iminor(inode);
703 struct cx8800_dev *h,*dev = NULL;
704 struct cx8800_fh *fh;
705 struct list_head *list;
706 enum v4l2_buf_type type = 0;
709 list_for_each(list,&cx8800_devlist) {
710 h = list_entry(list, struct cx8800_dev, devlist);
711 if (h->video_dev->minor == minor) {
713 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
715 if (h->vbi_dev->minor == minor) {
717 type = V4L2_BUF_TYPE_VBI_CAPTURE;
720 h->radio_dev->minor == minor) {
728 dprintk(1,"open minor=%d radio=%d type=%s\n",
729 minor,radio,v4l2_type_names[type]);
731 /* allocate + initialize per filehandle data */
732 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
735 memset(fh,0,sizeof(*fh));
736 file->private_data = fh;
742 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
744 videobuf_queue_init(&fh->vidq, &cx8800_video_qops,
745 dev->pci, &dev->slock,
746 V4L2_BUF_TYPE_VIDEO_CAPTURE,
747 V4L2_FIELD_INTERLACED,
748 sizeof(struct cx88_buffer),
750 videobuf_queue_init(&fh->vbiq, &cx8800_vbi_qops,
751 dev->pci, &dev->slock,
752 V4L2_BUF_TYPE_VBI_CAPTURE,
754 sizeof(struct cx88_buffer),
758 struct cx88_core *core = dev->core;
759 int board = core->board;
760 dprintk(1,"video_open: setting radio device\n");
761 cx_write(MO_GP0_IO, cx88_boards[board].radio.gpio0);
762 cx_write(MO_GP1_IO, cx88_boards[board].radio.gpio1);
763 cx_write(MO_GP2_IO, cx88_boards[board].radio.gpio2);
764 cx_write(MO_GP3_IO, cx88_boards[board].radio.gpio3);
765 dev->core->tvaudio = WW_FM;
766 cx88_set_tvaudio(core);
767 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
768 cx88_call_i2c_clients(dev->core,AUDC_SET_RADIO,NULL);
775 video_read(struct file *file, char *data, size_t count, loff_t *ppos)
777 struct cx8800_fh *fh = file->private_data;
780 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
781 if (res_locked(fh->dev,RESOURCE_VIDEO))
783 return videobuf_read_one(&fh->vidq, data, count, ppos,
784 file->f_flags & O_NONBLOCK);
785 case V4L2_BUF_TYPE_VBI_CAPTURE:
786 if (!res_get(fh->dev,fh,RESOURCE_VBI))
788 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
789 file->f_flags & O_NONBLOCK);
797 video_poll(struct file *file, struct poll_table_struct *wait)
799 struct cx8800_fh *fh = file->private_data;
800 struct cx88_buffer *buf;
802 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
803 if (!res_get(fh->dev,fh,RESOURCE_VBI))
805 return videobuf_poll_stream(file, &fh->vbiq, wait);
808 if (res_check(fh,RESOURCE_VIDEO)) {
809 /* streaming capture */
810 if (list_empty(&fh->vidq.stream))
812 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
815 buf = (struct cx88_buffer*)fh->vidq.read_buf;
819 poll_wait(file, &buf->vb.done, wait);
820 if (buf->vb.state == STATE_DONE ||
821 buf->vb.state == STATE_ERROR)
822 return POLLIN|POLLRDNORM;
826 static int video_release(struct inode *inode, struct file *file)
828 struct cx8800_fh *fh = file->private_data;
829 struct cx8800_dev *dev = fh->dev;
831 /* turn off overlay */
832 if (res_check(fh, RESOURCE_OVERLAY)) {
834 res_free(dev,fh,RESOURCE_OVERLAY);
837 /* stop video capture */
838 if (res_check(fh, RESOURCE_VIDEO)) {
839 videobuf_queue_cancel(&fh->vidq);
840 res_free(dev,fh,RESOURCE_VIDEO);
842 if (fh->vidq.read_buf) {
843 buffer_release(&fh->vidq,fh->vidq.read_buf);
844 kfree(fh->vidq.read_buf);
847 /* stop vbi capture */
848 if (res_check(fh, RESOURCE_VBI)) {
849 if (fh->vbiq.streaming)
850 videobuf_streamoff(&fh->vbiq);
851 if (fh->vbiq.reading)
852 videobuf_read_stop(&fh->vbiq);
853 res_free(dev,fh,RESOURCE_VBI);
856 videobuf_mmap_free(&fh->vidq);
857 videobuf_mmap_free(&fh->vbiq);
858 file->private_data = NULL;
864 video_mmap(struct file *file, struct vm_area_struct * vma)
866 struct cx8800_fh *fh = file->private_data;
868 return videobuf_mmap_mapper(get_queue(fh), vma);
871 /* ------------------------------------------------------------------ */
873 static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl)
875 struct cx88_core *core = dev->core;
876 struct cx88_ctrl *c = NULL;
880 for (i = 0; i < CX8800_CTLS; i++)
881 if (cx8800_ctls[i].v.id == ctl->id)
886 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
888 case V4L2_CID_AUDIO_BALANCE:
889 ctl->value = (value & 0x40) ? (value & 0x3f) : (0x40 - (value & 0x3f));
891 case V4L2_CID_AUDIO_VOLUME:
892 ctl->value = 0x3f - (value & 0x3f);
895 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
901 static int set_control(struct cx8800_dev *dev, struct v4l2_control *ctl)
903 struct cx88_core *core = dev->core;
904 struct cx88_ctrl *c = NULL;
909 for (i = 0; i < CX8800_CTLS; i++)
910 if (cx8800_ctls[i].v.id == ctl->id)
915 if (ctl->value < c->v.minimum)
917 if (ctl->value > c->v.maximum)
920 case V4L2_CID_AUDIO_BALANCE:
921 value = (ctl->value < 0x40) ? (0x40 - ctl->value) : ctl->value;
923 case V4L2_CID_AUDIO_VOLUME:
924 value = 0x3f - (ctl->value & 0x3f);
926 case V4L2_CID_SATURATION:
927 /* special v_sat handling */
928 v_sat_value = ctl->value - (0x7f - 0x5a);
929 if (v_sat_value > 0xff)
931 if (v_sat_value < 0x00)
933 cx_andor(MO_UV_SATURATION, 0xff00, v_sat_value << 8);
934 /* fall through to default route for u_sat */
936 value = ((ctl->value - c->off) << c->shift) & c->mask;
939 dprintk(1,"set_control id=0x%X reg=0x%x val=0x%x%s\n",
940 ctl->id, c->reg, value, c->sreg ? " [shadowed]" : "");
942 cx_sandor(c->sreg, c->reg, c->mask, value);
944 cx_andor(c->reg, c->mask, value);
949 static void init_controls(struct cx8800_dev *dev)
951 static struct v4l2_control mute = {
952 .id = V4L2_CID_AUDIO_MUTE,
955 static struct v4l2_control volume = {
956 .id = V4L2_CID_AUDIO_VOLUME,
959 static struct v4l2_control hue = {
963 static struct v4l2_control contrast = {
964 .id = V4L2_CID_CONTRAST,
967 static struct v4l2_control brightness = {
968 .id = V4L2_CID_BRIGHTNESS,
972 set_control(dev,&mute);
973 set_control(dev,&volume);
974 set_control(dev,&hue);
975 set_control(dev,&contrast);
976 set_control(dev,&brightness);
979 /* ------------------------------------------------------------------ */
981 static int cx8800_g_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
982 struct v4l2_format *f)
985 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
986 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
987 f->fmt.pix.width = fh->width;
988 f->fmt.pix.height = fh->height;
989 f->fmt.pix.field = fh->vidq.field;
990 f->fmt.pix.pixelformat = fh->fmt->fourcc;
991 f->fmt.pix.bytesperline =
992 (f->fmt.pix.width * fh->fmt->depth) >> 3;
993 f->fmt.pix.sizeimage =
994 f->fmt.pix.height * f->fmt.pix.bytesperline;
996 case V4L2_BUF_TYPE_VBI_CAPTURE:
997 cx8800_vbi_fmt(dev, f);
1004 static int cx8800_try_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1005 struct v4l2_format *f)
1008 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1010 struct cx8800_fmt *fmt;
1011 enum v4l2_field field;
1012 unsigned int maxw, maxh;
1014 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1018 field = f->fmt.pix.field;
1019 maxw = norm_maxw(dev->core->tvnorm);
1020 maxh = norm_maxh(dev->core->tvnorm);
1022 if (V4L2_FIELD_ANY == field) {
1023 field = (f->fmt.pix.height > maxh/2)
1024 ? V4L2_FIELD_INTERLACED
1025 : V4L2_FIELD_BOTTOM;
1029 case V4L2_FIELD_TOP:
1030 case V4L2_FIELD_BOTTOM:
1033 case V4L2_FIELD_INTERLACED:
1039 f->fmt.pix.field = field;
1040 if (f->fmt.pix.height < 32)
1041 f->fmt.pix.height = 32;
1042 if (f->fmt.pix.height > maxh)
1043 f->fmt.pix.height = maxh;
1044 if (f->fmt.pix.width < 48)
1045 f->fmt.pix.width = 48;
1046 if (f->fmt.pix.width > maxw)
1047 f->fmt.pix.width = maxw;
1048 f->fmt.pix.width &= ~0x03;
1049 f->fmt.pix.bytesperline =
1050 (f->fmt.pix.width * fmt->depth) >> 3;
1051 f->fmt.pix.sizeimage =
1052 f->fmt.pix.height * f->fmt.pix.bytesperline;
1056 case V4L2_BUF_TYPE_VBI_CAPTURE:
1057 cx8800_vbi_fmt(dev, f);
1064 static int cx8800_s_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1065 struct v4l2_format *f)
1070 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1071 err = cx8800_try_fmt(dev,fh,f);
1075 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1076 fh->width = f->fmt.pix.width;
1077 fh->height = f->fmt.pix.height;
1078 fh->vidq.field = f->fmt.pix.field;
1080 case V4L2_BUF_TYPE_VBI_CAPTURE:
1081 cx8800_vbi_fmt(dev, f);
1089 * This function is _not_ called directly, but from
1090 * video_generic_ioctl (and maybe others). userspace
1091 * copying is done already, arg is a kernel pointer.
1093 static int video_do_ioctl(struct inode *inode, struct file *file,
1094 unsigned int cmd, void *arg)
1096 struct cx8800_fh *fh = file->private_data;
1097 struct cx8800_dev *dev = fh->dev;
1098 struct cx88_core *core = dev->core;
1101 if (video_debug > 1)
1102 cx88_print_ioctl(core->name,cmd);
1104 case VIDIOC_QUERYCAP:
1106 struct v4l2_capability *cap = arg;
1108 memset(cap,0,sizeof(*cap));
1109 strcpy(cap->driver, "cx8800");
1110 strlcpy(cap->card, cx88_boards[core->board].name,
1112 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1113 cap->version = CX88_VERSION_CODE;
1115 V4L2_CAP_VIDEO_CAPTURE |
1116 V4L2_CAP_READWRITE |
1117 V4L2_CAP_STREAMING |
1118 V4L2_CAP_VBI_CAPTURE |
1120 if (UNSET != core->tuner_type)
1121 cap->capabilities |= V4L2_CAP_TUNER;
1125 /* ---------- tv norms ---------- */
1126 case VIDIOC_ENUMSTD:
1128 struct v4l2_standard *e = arg;
1132 if (i >= ARRAY_SIZE(tvnorms))
1134 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1135 tvnorms[e->index].name);
1143 v4l2_std_id *id = arg;
1145 *id = core->tvnorm->id;
1150 v4l2_std_id *id = arg;
1153 for(i = 0; i < ARRAY_SIZE(tvnorms); i++)
1154 if (*id & tvnorms[i].id)
1156 if (i == ARRAY_SIZE(tvnorms))
1160 cx88_set_tvnorm(dev->core,&tvnorms[i]);
1165 /* ------ input switching ---------- */
1166 case VIDIOC_ENUMINPUT:
1168 static const char *iname[] = {
1169 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1170 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1171 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1172 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1173 [ CX88_VMUX_SVIDEO ] = "S-Video",
1174 [ CX88_VMUX_TELEVISION ] = "Television",
1175 [ CX88_VMUX_CABLE ] = "Cable TV",
1176 [ CX88_VMUX_DVB ] = "DVB",
1177 [ CX88_VMUX_DEBUG ] = "for debug only",
1179 struct v4l2_input *i = arg;
1185 if (0 == INPUT(n)->type)
1187 memset(i,0,sizeof(*i));
1189 i->type = V4L2_INPUT_TYPE_CAMERA;
1190 strcpy(i->name,iname[INPUT(n)->type]);
1191 if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
1192 (CX88_VMUX_CABLE == INPUT(n)->type))
1193 i->type = V4L2_INPUT_TYPE_TUNER;
1194 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1195 i->std |= tvnorms[n].id;
1198 case VIDIOC_G_INPUT:
1200 unsigned int *i = arg;
1202 *i = dev->core->input;
1205 case VIDIOC_S_INPUT:
1207 unsigned int *i = arg;
1212 cx88_newstation(core);
1220 /* --- capture ioctls ---------------------------------------- */
1221 case VIDIOC_ENUM_FMT:
1223 struct v4l2_fmtdesc *f = arg;
1224 enum v4l2_buf_type type;
1230 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1231 if (index >= ARRAY_SIZE(formats))
1233 memset(f,0,sizeof(*f));
1236 strlcpy(f->description,formats[index].name,sizeof(f->description));
1237 f->pixelformat = formats[index].fourcc;
1246 struct v4l2_format *f = arg;
1247 return cx8800_g_fmt(dev,fh,f);
1251 struct v4l2_format *f = arg;
1252 return cx8800_s_fmt(dev,fh,f);
1254 case VIDIOC_TRY_FMT:
1256 struct v4l2_format *f = arg;
1257 return cx8800_try_fmt(dev,fh,f);
1260 /* --- controls ---------------------------------------------- */
1261 case VIDIOC_QUERYCTRL:
1263 struct v4l2_queryctrl *c = arg;
1266 if (c->id < V4L2_CID_BASE ||
1267 c->id >= V4L2_CID_LASTP1)
1269 for (i = 0; i < CX8800_CTLS; i++)
1270 if (cx8800_ctls[i].v.id == c->id)
1272 if (i == CX8800_CTLS) {
1276 *c = cx8800_ctls[i].v;
1280 return get_control(dev,arg);
1282 return set_control(dev,arg);
1284 /* --- tuner ioctls ------------------------------------------ */
1285 case VIDIOC_G_TUNER:
1287 struct v4l2_tuner *t = arg;
1290 if (UNSET == core->tuner_type)
1295 memset(t,0,sizeof(*t));
1296 strcpy(t->name, "Television");
1297 t->type = V4L2_TUNER_ANALOG_TV;
1298 t->capability = V4L2_TUNER_CAP_NORM;
1299 t->rangehigh = 0xffffffffUL;
1301 cx88_get_stereo(core ,t);
1302 reg = cx_read(MO_DEVICE_STATUS);
1303 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1306 case VIDIOC_S_TUNER:
1308 struct v4l2_tuner *t = arg;
1310 if (UNSET == core->tuner_type)
1314 cx88_set_stereo(core, t->audmode, 1);
1317 case VIDIOC_G_FREQUENCY:
1319 struct v4l2_frequency *f = arg;
1321 memset(f,0,sizeof(*f));
1323 if (UNSET == core->tuner_type)
1326 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1327 f->frequency = dev->freq;
1329 cx88_call_i2c_clients(dev->core,VIDIOC_G_FREQUENCY,f);
1333 case VIDIOC_S_FREQUENCY:
1335 struct v4l2_frequency *f = arg;
1337 if (UNSET == core->tuner_type)
1341 if (0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV)
1343 if (1 == fh->radio && f->type != V4L2_TUNER_RADIO)
1346 dev->freq = f->frequency;
1347 cx88_newstation(core);
1348 cx88_call_i2c_clients(dev->core,VIDIOC_S_FREQUENCY,f);
1350 /* When changing channels it is required to reset TVAUDIO */
1352 cx88_set_tvaudio(core);
1358 /* --- streaming capture ------------------------------------- */
1361 struct video_mbuf *mbuf = arg;
1362 struct videobuf_queue *q;
1363 struct v4l2_requestbuffers req;
1367 memset(&req,0,sizeof(req));
1370 req.memory = V4L2_MEMORY_MMAP;
1371 err = videobuf_reqbufs(q,&req);
1374 memset(mbuf,0,sizeof(*mbuf));
1375 mbuf->frames = req.count;
1377 for (i = 0; i < mbuf->frames; i++) {
1378 mbuf->offsets[i] = q->bufs[i]->boff;
1379 mbuf->size += q->bufs[i]->bsize;
1383 case VIDIOC_REQBUFS:
1384 return videobuf_reqbufs(get_queue(fh), arg);
1386 case VIDIOC_QUERYBUF:
1387 return videobuf_querybuf(get_queue(fh), arg);
1390 return videobuf_qbuf(get_queue(fh), arg);
1393 return videobuf_dqbuf(get_queue(fh), arg,
1394 file->f_flags & O_NONBLOCK);
1396 case VIDIOC_STREAMON:
1398 int res = get_ressource(fh);
1400 if (!res_get(dev,fh,res))
1402 return videobuf_streamon(get_queue(fh));
1404 case VIDIOC_STREAMOFF:
1406 int res = get_ressource(fh);
1408 err = videobuf_streamoff(get_queue(fh));
1411 res_free(dev,fh,res);
1416 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1422 static int video_ioctl(struct inode *inode, struct file *file,
1423 unsigned int cmd, unsigned long arg)
1425 return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
1428 /* ----------------------------------------------------------- */
1430 static int radio_do_ioctl(struct inode *inode, struct file *file,
1431 unsigned int cmd, void *arg)
1433 struct cx8800_fh *fh = file->private_data;
1434 struct cx8800_dev *dev = fh->dev;
1435 struct cx88_core *core = dev->core;
1437 if (video_debug > 1)
1438 cx88_print_ioctl(core->name,cmd);
1441 case VIDIOC_QUERYCAP:
1443 struct v4l2_capability *cap = arg;
1445 memset(cap,0,sizeof(*cap));
1446 strcpy(cap->driver, "cx8800");
1447 strlcpy(cap->card, cx88_boards[core->board].name,
1449 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1450 cap->version = CX88_VERSION_CODE;
1451 cap->capabilities = V4L2_CAP_TUNER;
1454 case VIDIOC_G_TUNER:
1456 struct v4l2_tuner *t = arg;
1461 memset(t,0,sizeof(*t));
1462 strcpy(t->name, "Radio");
1464 cx88_call_i2c_clients(dev->core,VIDIOC_G_TUNER,t);
1467 case VIDIOC_ENUMINPUT:
1469 struct v4l2_input *i = arg;
1473 strcpy(i->name,"Radio");
1474 i->type = V4L2_INPUT_TYPE_TUNER;
1477 case VIDIOC_G_INPUT:
1483 case VIDIOC_G_AUDIO:
1485 struct v4l2_audio *a = arg;
1487 memset(a,0,sizeof(*a));
1488 strcpy(a->name,"Radio");
1493 v4l2_std_id *id = arg;
1499 struct video_tuner *v = arg;
1501 if (v->tuner) /* Only tuner 0 */
1504 cx88_call_i2c_clients(dev->core,VIDIOCSTUNER,v);
1507 case VIDIOC_S_TUNER:
1509 struct v4l2_tuner *t = arg;
1514 cx88_call_i2c_clients(dev->core,VIDIOC_S_TUNER,t);
1519 case VIDIOC_S_AUDIO:
1520 case VIDIOC_S_INPUT:
1524 case VIDIOC_QUERYCTRL:
1526 struct v4l2_queryctrl *c = arg;
1529 if (c->id < V4L2_CID_BASE ||
1530 c->id >= V4L2_CID_LASTP1)
1532 if (c->id == V4L2_CID_AUDIO_MUTE) {
1533 for (i = 0; i < CX8800_CTLS; i++)
1534 if (cx8800_ctls[i].v.id == c->id)
1536 *c = cx8800_ctls[i].v;
1545 case VIDIOC_G_FREQUENCY:
1546 case VIDIOC_S_FREQUENCY:
1547 return video_do_ioctl(inode,file,cmd,arg);
1550 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1556 static int radio_ioctl(struct inode *inode, struct file *file,
1557 unsigned int cmd, unsigned long arg)
1559 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
1562 /* ----------------------------------------------------------- */
1564 static void cx8800_vid_timeout(unsigned long data)
1566 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1567 struct cx88_core *core = dev->core;
1568 struct cx88_dmaqueue *q = &dev->vidq;
1569 struct cx88_buffer *buf;
1570 unsigned long flags;
1572 cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH21]);
1574 cx_clear(MO_VID_DMACNTRL, 0x11);
1575 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1577 spin_lock_irqsave(&dev->slock,flags);
1578 while (!list_empty(&q->active)) {
1579 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1580 list_del(&buf->vb.queue);
1581 buf->vb.state = STATE_ERROR;
1582 wake_up(&buf->vb.done);
1583 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1584 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1586 restart_video_queue(dev,q);
1587 spin_unlock_irqrestore(&dev->slock,flags);
1590 static char *cx88_vid_irqs[32] = {
1591 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1592 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1593 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1594 "y_sync", "u_sync", "v_sync", "vbi_sync",
1595 "opc_err", "par_err", "rip_err", "pci_abort",
1598 static void cx8800_vid_irq(struct cx8800_dev *dev)
1600 struct cx88_core *core = dev->core;
1601 u32 status, mask, count;
1603 status = cx_read(MO_VID_INTSTAT);
1604 mask = cx_read(MO_VID_INTMSK);
1605 if (0 == (status & mask))
1607 cx_write(MO_VID_INTSTAT, status);
1608 if (irq_debug || (status & mask & ~0xff))
1609 cx88_print_irqbits(core->name, "irq vid",
1610 cx88_vid_irqs, status, mask);
1612 /* risc op code error */
1613 if (status & (1 << 16)) {
1614 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1615 cx_clear(MO_VID_DMACNTRL, 0x11);
1616 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1617 cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH21]);
1621 if (status & 0x01) {
1622 spin_lock(&dev->slock);
1623 count = cx_read(MO_VIDY_GPCNT);
1624 cx88_wakeup(dev->core, &dev->vidq, count);
1625 spin_unlock(&dev->slock);
1629 if (status & 0x08) {
1630 spin_lock(&dev->slock);
1631 count = cx_read(MO_VBI_GPCNT);
1632 cx88_wakeup(dev->core, &dev->vbiq, count);
1633 spin_unlock(&dev->slock);
1637 if (status & 0x10) {
1638 dprintk(2,"stopper video\n");
1639 spin_lock(&dev->slock);
1640 restart_video_queue(dev,&dev->vidq);
1641 spin_unlock(&dev->slock);
1645 if (status & 0x80) {
1646 dprintk(2,"stopper vbi\n");
1647 spin_lock(&dev->slock);
1648 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1649 spin_unlock(&dev->slock);
1653 static irqreturn_t cx8800_irq(int irq, void *dev_id, struct pt_regs *regs)
1655 struct cx8800_dev *dev = dev_id;
1656 struct cx88_core *core = dev->core;
1658 int loop, handled = 0;
1660 for (loop = 0; loop < 10; loop++) {
1661 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
1664 cx_write(MO_PCI_INTSTAT, status);
1667 if (status & core->pci_irqmask)
1668 cx88_core_irq(core,status);
1670 cx8800_vid_irq(dev);
1673 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1675 cx_write(MO_PCI_INTMSK,0);
1679 return IRQ_RETVAL(handled);
1682 /* ----------------------------------------------------------- */
1683 /* exported stuff */
1685 static struct file_operations video_fops =
1687 .owner = THIS_MODULE,
1689 .release = video_release,
1693 .ioctl = video_ioctl,
1694 .llseek = no_llseek,
1697 static struct video_device cx8800_video_template =
1699 .name = "cx8800-video",
1700 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1702 .fops = &video_fops,
1706 static struct video_device cx8800_vbi_template =
1708 .name = "cx8800-vbi",
1709 .type = VID_TYPE_TELETEXT|VID_TYPE_TUNER,
1711 .fops = &video_fops,
1715 static struct file_operations radio_fops =
1717 .owner = THIS_MODULE,
1719 .release = video_release,
1720 .ioctl = radio_ioctl,
1721 .llseek = no_llseek,
1724 static struct video_device cx8800_radio_template =
1726 .name = "cx8800-radio",
1727 .type = VID_TYPE_TUNER,
1729 .fops = &radio_fops,
1733 /* ----------------------------------------------------------- */
1735 static void cx8800_unregister_video(struct cx8800_dev *dev)
1737 if (dev->radio_dev) {
1738 if (-1 != dev->radio_dev->minor)
1739 video_unregister_device(dev->radio_dev);
1741 video_device_release(dev->radio_dev);
1742 dev->radio_dev = NULL;
1745 if (-1 != dev->vbi_dev->minor)
1746 video_unregister_device(dev->vbi_dev);
1748 video_device_release(dev->vbi_dev);
1749 dev->vbi_dev = NULL;
1751 if (dev->video_dev) {
1752 if (-1 != dev->video_dev->minor)
1753 video_unregister_device(dev->video_dev);
1755 video_device_release(dev->video_dev);
1756 dev->video_dev = NULL;
1760 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1761 const struct pci_device_id *pci_id)
1763 struct cx8800_dev *dev;
1764 struct cx88_core *core;
1767 dev = kmalloc(sizeof(*dev),GFP_KERNEL);
1770 memset(dev,0,sizeof(*dev));
1774 if (pci_enable_device(pci_dev)) {
1778 core = cx88_core_get(dev->pci);
1785 /* print pci info */
1786 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
1787 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1788 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1789 "latency: %d, mmio: 0x%lx\n", core->name,
1790 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1791 dev->pci_lat,pci_resource_start(pci_dev,0));
1793 pci_set_master(pci_dev);
1794 if (!pci_dma_supported(pci_dev,0xffffffff)) {
1795 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1800 /* initialize driver struct */
1801 init_MUTEX(&dev->lock);
1802 spin_lock_init(&dev->slock);
1803 core->tvnorm = tvnorms;
1805 /* init video dma queues */
1806 INIT_LIST_HEAD(&dev->vidq.active);
1807 INIT_LIST_HEAD(&dev->vidq.queued);
1808 dev->vidq.timeout.function = cx8800_vid_timeout;
1809 dev->vidq.timeout.data = (unsigned long)dev;
1810 init_timer(&dev->vidq.timeout);
1811 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1812 MO_VID_DMACNTRL,0x11,0x00);
1814 /* init vbi dma queues */
1815 INIT_LIST_HEAD(&dev->vbiq.active);
1816 INIT_LIST_HEAD(&dev->vbiq.queued);
1817 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1818 dev->vbiq.timeout.data = (unsigned long)dev;
1819 init_timer(&dev->vbiq.timeout);
1820 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1821 MO_VID_DMACNTRL,0x88,0x00);
1824 err = request_irq(pci_dev->irq, cx8800_irq,
1825 SA_SHIRQ | SA_INTERRUPT, core->name, dev);
1827 printk(KERN_ERR "%s: can't get IRQ %d\n",
1828 core->name,pci_dev->irq);
1831 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1833 /* load and configure helper modules */
1834 if (TUNER_ABSENT != core->tuner_type)
1835 request_module("tuner");
1836 if (core->tda9887_conf)
1837 request_module("tda9887");
1838 /* register v4l devices */
1839 dev->video_dev = cx88_vdev_init(core,dev->pci,
1840 &cx8800_video_template,"video");
1841 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1842 video_nr[core->nr]);
1844 printk(KERN_INFO "%s: can't register video device\n",
1848 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1849 core->name,dev->video_dev->minor & 0x1f);
1851 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1852 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1855 printk(KERN_INFO "%s/0: can't register vbi device\n",
1859 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1860 core->name,dev->vbi_dev->minor & 0x1f);
1862 if (core->has_radio) {
1863 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1864 &cx8800_radio_template,"radio");
1865 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1866 radio_nr[core->nr]);
1868 printk(KERN_INFO "%s/0: can't register radio device\n",
1872 printk(KERN_INFO "%s/0: registered device radio%d\n",
1873 core->name,dev->radio_dev->minor & 0x1f);
1876 /* everything worked */
1877 list_add_tail(&dev->devlist,&cx8800_devlist);
1878 pci_set_drvdata(pci_dev,dev);
1880 /* initial device configuration */
1883 cx88_set_tvnorm(dev->core,tvnorms);
1887 /* start tvaudio thread */
1888 if (core->tuner_type != TUNER_ABSENT)
1889 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
1893 cx8800_unregister_video(dev);
1894 free_irq(pci_dev->irq, dev);
1896 cx88_core_put(core,dev->pci);
1902 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1904 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1907 if (dev->core->kthread) {
1908 kthread_stop(dev->core->kthread);
1909 dev->core->kthread = NULL;
1912 cx88_shutdown(dev->core); /* FIXME */
1913 pci_disable_device(pci_dev);
1915 /* unregister stuff */
1917 free_irq(pci_dev->irq, dev);
1918 cx8800_unregister_video(dev);
1919 pci_set_drvdata(pci_dev, NULL);
1922 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1923 list_del(&dev->devlist);
1924 cx88_core_put(dev->core,dev->pci);
1928 static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1930 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1931 struct cx88_core *core = dev->core;
1933 /* stop video+vbi capture */
1934 spin_lock(&dev->slock);
1935 if (!list_empty(&dev->vidq.active)) {
1936 printk("%s: suspend video\n", core->name);
1937 stop_video_dma(dev);
1938 del_timer(&dev->vidq.timeout);
1940 if (!list_empty(&dev->vbiq.active)) {
1941 printk("%s: suspend vbi\n", core->name);
1942 cx8800_stop_vbi_dma(dev);
1943 del_timer(&dev->vbiq.timeout);
1945 spin_unlock(&dev->slock);
1947 /* FIXME -- shutdown device */
1948 cx88_shutdown(dev->core);
1950 pci_save_state(pci_dev);
1951 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1952 pci_disable_device(pci_dev);
1953 dev->state.disabled = 1;
1958 static int cx8800_resume(struct pci_dev *pci_dev)
1960 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1961 struct cx88_core *core = dev->core;
1963 if (dev->state.disabled) {
1964 pci_enable_device(pci_dev);
1965 dev->state.disabled = 0;
1967 pci_set_power_state(pci_dev, PCI_D0);
1968 pci_restore_state(pci_dev);
1970 /* FIXME: re-initialize hardware */
1971 cx88_reset(dev->core);
1973 /* restart video+vbi capture */
1974 spin_lock(&dev->slock);
1975 if (!list_empty(&dev->vidq.active)) {
1976 printk("%s: resume video\n", core->name);
1977 restart_video_queue(dev,&dev->vidq);
1979 if (!list_empty(&dev->vbiq.active)) {
1980 printk("%s: resume vbi\n", core->name);
1981 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1983 spin_unlock(&dev->slock);
1988 /* ----------------------------------------------------------- */
1990 static struct pci_device_id cx8800_pci_tbl[] = {
1994 .subvendor = PCI_ANY_ID,
1995 .subdevice = PCI_ANY_ID,
1997 /* --- end of list --- */
2000 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2002 static struct pci_driver cx8800_pci_driver = {
2004 .id_table = cx8800_pci_tbl,
2005 .probe = cx8800_initdev,
2006 .remove = __devexit_p(cx8800_finidev),
2008 .suspend = cx8800_suspend,
2009 .resume = cx8800_resume,
2012 static int cx8800_init(void)
2014 printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
2015 (CX88_VERSION_CODE >> 16) & 0xff,
2016 (CX88_VERSION_CODE >> 8) & 0xff,
2017 CX88_VERSION_CODE & 0xff);
2019 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2020 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2022 return pci_register_driver(&cx8800_pci_driver);
2025 static void cx8800_fini(void)
2027 pci_unregister_driver(&cx8800_pci_driver);
2030 module_init(cx8800_init);
2031 module_exit(cx8800_fini);
2033 /* ----------------------------------------------------------- */
2038 * 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