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 * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
10 * - video_ioctl2 conversion
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/module.h>
31 #include <linux/moduleparam.h>
32 #include <linux/kmod.h>
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
35 #include <linux/interrupt.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/delay.h>
38 #include <linux/kthread.h>
39 #include <linux/dma-mapping.h>
40 #include <asm/div64.h>
43 #include <media/v4l2-common.h>
45 #ifdef CONFIG_VIDEO_V4L1_COMPAT
46 /* Include V4L1 specific functions. Should be removed soon */
47 #include <linux/videodev.h>
50 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
51 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
52 MODULE_LICENSE("GPL");
54 /* ------------------------------------------------------------------ */
56 static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
57 static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
58 static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
60 module_param_array(video_nr, int, NULL, 0444);
61 module_param_array(vbi_nr, int, NULL, 0444);
62 module_param_array(radio_nr, int, NULL, 0444);
64 MODULE_PARM_DESC(video_nr,"video device numbers");
65 MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
66 MODULE_PARM_DESC(radio_nr,"radio device numbers");
68 static unsigned int video_debug = 0;
69 module_param(video_debug,int,0644);
70 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
72 static unsigned int irq_debug = 0;
73 module_param(irq_debug,int,0644);
74 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
76 static unsigned int vid_limit = 16;
77 module_param(vid_limit,int,0644);
78 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
80 #define dprintk(level,fmt, arg...) if (video_debug >= level) \
81 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
83 /* ------------------------------------------------------------------ */
85 static LIST_HEAD(cx8800_devlist);
87 /* ------------------------------------------------------------------- */
90 static struct cx8800_fmt formats[] = {
92 .name = "8 bpp, gray",
93 .fourcc = V4L2_PIX_FMT_GREY,
94 .cxformat = ColorFormatY8,
96 .flags = FORMAT_FLAGS_PACKED,
98 .name = "15 bpp RGB, le",
99 .fourcc = V4L2_PIX_FMT_RGB555,
100 .cxformat = ColorFormatRGB15,
102 .flags = FORMAT_FLAGS_PACKED,
104 .name = "15 bpp RGB, be",
105 .fourcc = V4L2_PIX_FMT_RGB555X,
106 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
108 .flags = FORMAT_FLAGS_PACKED,
110 .name = "16 bpp RGB, le",
111 .fourcc = V4L2_PIX_FMT_RGB565,
112 .cxformat = ColorFormatRGB16,
114 .flags = FORMAT_FLAGS_PACKED,
116 .name = "16 bpp RGB, be",
117 .fourcc = V4L2_PIX_FMT_RGB565X,
118 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
120 .flags = FORMAT_FLAGS_PACKED,
122 .name = "24 bpp RGB, le",
123 .fourcc = V4L2_PIX_FMT_BGR24,
124 .cxformat = ColorFormatRGB24,
126 .flags = FORMAT_FLAGS_PACKED,
128 .name = "32 bpp RGB, le",
129 .fourcc = V4L2_PIX_FMT_BGR32,
130 .cxformat = ColorFormatRGB32,
132 .flags = FORMAT_FLAGS_PACKED,
134 .name = "32 bpp RGB, be",
135 .fourcc = V4L2_PIX_FMT_RGB32,
136 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
138 .flags = FORMAT_FLAGS_PACKED,
140 .name = "4:2:2, packed, YUYV",
141 .fourcc = V4L2_PIX_FMT_YUYV,
142 .cxformat = ColorFormatYUY2,
144 .flags = FORMAT_FLAGS_PACKED,
146 .name = "4:2:2, packed, UYVY",
147 .fourcc = V4L2_PIX_FMT_UYVY,
148 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
150 .flags = FORMAT_FLAGS_PACKED,
154 static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
158 for (i = 0; i < ARRAY_SIZE(formats); i++)
159 if (formats[i].fourcc == fourcc)
164 /* ------------------------------------------------------------------- */
166 static const struct v4l2_queryctrl no_ctl = {
168 .flags = V4L2_CTRL_FLAG_DISABLED,
171 static struct cx88_ctrl cx8800_ctls[] = {
175 .id = V4L2_CID_BRIGHTNESS,
176 .name = "Brightness",
180 .default_value = 0x7f,
181 .type = V4L2_CTRL_TYPE_INTEGER,
184 .reg = MO_CONTR_BRIGHT,
189 .id = V4L2_CID_CONTRAST,
194 .default_value = 0x3f,
195 .type = V4L2_CTRL_TYPE_INTEGER,
198 .reg = MO_CONTR_BRIGHT,
208 .default_value = 0x7f,
209 .type = V4L2_CTRL_TYPE_INTEGER,
216 /* strictly, this only describes only U saturation.
217 * V saturation is handled specially through code.
220 .id = V4L2_CID_SATURATION,
221 .name = "Saturation",
225 .default_value = 0x7f,
226 .type = V4L2_CTRL_TYPE_INTEGER,
229 .reg = MO_UV_SATURATION,
235 .id = V4L2_CID_AUDIO_MUTE,
240 .type = V4L2_CTRL_TYPE_BOOLEAN,
243 .sreg = SHADOW_AUD_VOL_CTL,
248 .id = V4L2_CID_AUDIO_VOLUME,
253 .default_value = 0x3f,
254 .type = V4L2_CTRL_TYPE_INTEGER,
257 .sreg = SHADOW_AUD_VOL_CTL,
262 .id = V4L2_CID_AUDIO_BALANCE,
267 .default_value = 0x40,
268 .type = V4L2_CTRL_TYPE_INTEGER,
271 .sreg = SHADOW_AUD_BAL_CTL,
276 static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
278 const u32 cx88_user_ctrls[] = {
284 V4L2_CID_AUDIO_VOLUME,
285 V4L2_CID_AUDIO_BALANCE,
289 EXPORT_SYMBOL(cx88_user_ctrls);
291 static const u32 *ctrl_classes[] = {
296 int cx8800_ctrl_query(struct v4l2_queryctrl *qctrl)
300 if (qctrl->id < V4L2_CID_BASE ||
301 qctrl->id >= V4L2_CID_LASTP1)
303 for (i = 0; i < CX8800_CTLS; i++)
304 if (cx8800_ctls[i].v.id == qctrl->id)
306 if (i == CX8800_CTLS) {
310 *qctrl = cx8800_ctls[i].v;
313 EXPORT_SYMBOL(cx8800_ctrl_query);
315 /* ------------------------------------------------------------------- */
316 /* resource management */
318 static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
320 struct cx88_core *core = dev->core;
321 if (fh->resources & bit)
322 /* have it already allocated */
326 mutex_lock(&core->lock);
327 if (dev->resources & bit) {
328 /* no, someone else uses it */
329 mutex_unlock(&core->lock);
332 /* it's free, grab it */
333 fh->resources |= bit;
334 dev->resources |= bit;
335 dprintk(1,"res: get %d\n",bit);
336 mutex_unlock(&core->lock);
341 int res_check(struct cx8800_fh *fh, unsigned int bit)
343 return (fh->resources & bit);
347 int res_locked(struct cx8800_dev *dev, unsigned int bit)
349 return (dev->resources & bit);
353 void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
355 struct cx88_core *core = dev->core;
356 BUG_ON((fh->resources & bits) != bits);
358 mutex_lock(&core->lock);
359 fh->resources &= ~bits;
360 dev->resources &= ~bits;
361 dprintk(1,"res: put %d\n",bits);
362 mutex_unlock(&core->lock);
365 /* ------------------------------------------------------------------ */
367 int cx88_video_mux(struct cx88_core *core, unsigned int input)
369 /* struct cx88_core *core = dev->core; */
371 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
372 input, INPUT(input)->vmux,
373 INPUT(input)->gpio0,INPUT(input)->gpio1,
374 INPUT(input)->gpio2,INPUT(input)->gpio3);
376 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input)->vmux << 14);
377 cx_write(MO_GP3_IO, INPUT(input)->gpio3);
378 cx_write(MO_GP0_IO, INPUT(input)->gpio0);
379 cx_write(MO_GP1_IO, INPUT(input)->gpio1);
380 cx_write(MO_GP2_IO, INPUT(input)->gpio2);
382 switch (INPUT(input)->type) {
383 case CX88_VMUX_SVIDEO:
384 cx_set(MO_AFECFG_IO, 0x00000001);
385 cx_set(MO_INPUT_FORMAT, 0x00010010);
386 cx_set(MO_FILTER_EVEN, 0x00002020);
387 cx_set(MO_FILTER_ODD, 0x00002020);
390 cx_clear(MO_AFECFG_IO, 0x00000001);
391 cx_clear(MO_INPUT_FORMAT, 0x00010010);
392 cx_clear(MO_FILTER_EVEN, 0x00002020);
393 cx_clear(MO_FILTER_ODD, 0x00002020);
397 if (cx88_boards[core->board].mpeg & CX88_MPEG_BLACKBIRD) {
398 /* sets sound input from external adc */
399 if (INPUT(input)->extadc)
400 cx_set(AUD_CTL, EN_I2SIN_ENABLE);
402 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
406 EXPORT_SYMBOL(cx88_video_mux);
408 /* ------------------------------------------------------------------ */
410 static int start_video_dma(struct cx8800_dev *dev,
411 struct cx88_dmaqueue *q,
412 struct cx88_buffer *buf)
414 struct cx88_core *core = dev->core;
416 /* setup fifo + format */
417 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
418 buf->bpl, buf->risc.dma);
419 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
420 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
423 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
427 cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
429 /* Enables corresponding bits at PCI_INT_STAT:
430 bits 0 to 4: video, audio, transport stream, VIP, Host
432 bits 8 and 9: DMA complete for: SRC, DST
433 bits 10 and 11: BERR signal asserted for RISC: RD, WR
434 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
436 cx_set(MO_VID_INTMSK, 0x0f0011);
439 cx_set(VID_CAPTURE_CONTROL,0x06);
442 cx_set(MO_DEV_CNTRL2, (1<<5));
443 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);
466 static int restart_video_queue(struct cx8800_dev *dev,
467 struct cx88_dmaqueue *q)
469 struct cx88_core *core = dev->core;
470 struct cx88_buffer *buf, *prev;
471 struct list_head *item;
473 if (!list_empty(&q->active)) {
474 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
475 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
477 start_video_dma(dev, q, buf);
478 list_for_each(item,&q->active) {
479 buf = list_entry(item, struct cx88_buffer, vb.queue);
480 buf->count = q->count++;
482 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
488 if (list_empty(&q->queued))
490 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
492 list_move_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_move_tail(&buf->vb.queue, &q->active);
504 buf->vb.state = STATE_ACTIVE;
505 buf->count = q->count++;
506 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
507 dprintk(2,"[%p/%d] restart_queue - move to active\n",
516 /* ------------------------------------------------------------------ */
519 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
521 struct cx8800_fh *fh = q->priv_data;
523 *size = fh->fmt->depth*fh->width*fh->height >> 3;
526 while (*size * *count > vid_limit * 1024 * 1024)
532 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
533 enum v4l2_field field)
535 struct cx8800_fh *fh = q->priv_data;
536 struct cx8800_dev *dev = fh->dev;
537 struct cx88_core *core = dev->core;
538 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
539 int rc, init_buffer = 0;
541 BUG_ON(NULL == fh->fmt);
542 if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
543 fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
545 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
546 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
549 if (buf->fmt != fh->fmt ||
550 buf->vb.width != fh->width ||
551 buf->vb.height != fh->height ||
552 buf->vb.field != field) {
554 buf->vb.width = fh->width;
555 buf->vb.height = fh->height;
556 buf->vb.field = field;
560 if (STATE_NEEDS_INIT == buf->vb.state) {
562 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
567 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
568 switch (buf->vb.field) {
570 cx88_risc_buffer(dev->pci, &buf->risc,
571 buf->vb.dma.sglist, 0, UNSET,
572 buf->bpl, 0, buf->vb.height);
574 case V4L2_FIELD_BOTTOM:
575 cx88_risc_buffer(dev->pci, &buf->risc,
576 buf->vb.dma.sglist, UNSET, 0,
577 buf->bpl, 0, buf->vb.height);
579 case V4L2_FIELD_INTERLACED:
580 cx88_risc_buffer(dev->pci, &buf->risc,
581 buf->vb.dma.sglist, 0, buf->bpl,
583 buf->vb.height >> 1);
585 case V4L2_FIELD_SEQ_TB:
586 cx88_risc_buffer(dev->pci, &buf->risc,
588 0, buf->bpl * (buf->vb.height >> 1),
590 buf->vb.height >> 1);
592 case V4L2_FIELD_SEQ_BT:
593 cx88_risc_buffer(dev->pci, &buf->risc,
595 buf->bpl * (buf->vb.height >> 1), 0,
597 buf->vb.height >> 1);
603 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
605 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
606 (unsigned long)buf->risc.dma);
608 buf->vb.state = STATE_PREPARED;
612 cx88_free_buffer(q,buf);
617 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
619 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
620 struct cx88_buffer *prev;
621 struct cx8800_fh *fh = vq->priv_data;
622 struct cx8800_dev *dev = fh->dev;
623 struct cx88_core *core = dev->core;
624 struct cx88_dmaqueue *q = &dev->vidq;
626 /* add jump to stopper */
627 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
628 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
630 if (!list_empty(&q->queued)) {
631 list_add_tail(&buf->vb.queue,&q->queued);
632 buf->vb.state = STATE_QUEUED;
633 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
636 } else if (list_empty(&q->active)) {
637 list_add_tail(&buf->vb.queue,&q->active);
638 start_video_dma(dev, q, buf);
639 buf->vb.state = STATE_ACTIVE;
640 buf->count = q->count++;
641 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
642 dprintk(2,"[%p/%d] buffer_queue - first active\n",
646 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
647 if (prev->vb.width == buf->vb.width &&
648 prev->vb.height == buf->vb.height &&
649 prev->fmt == buf->fmt) {
650 list_add_tail(&buf->vb.queue,&q->active);
651 buf->vb.state = STATE_ACTIVE;
652 buf->count = q->count++;
653 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
654 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
658 list_add_tail(&buf->vb.queue,&q->queued);
659 buf->vb.state = STATE_QUEUED;
660 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
666 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
668 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
670 cx88_free_buffer(q,buf);
673 static struct videobuf_queue_ops cx8800_video_qops = {
674 .buf_setup = buffer_setup,
675 .buf_prepare = buffer_prepare,
676 .buf_queue = buffer_queue,
677 .buf_release = buffer_release,
680 /* ------------------------------------------------------------------ */
683 /* ------------------------------------------------------------------ */
685 static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
688 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
690 case V4L2_BUF_TYPE_VBI_CAPTURE:
698 static int get_ressource(struct cx8800_fh *fh)
701 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
702 return RESOURCE_VIDEO;
703 case V4L2_BUF_TYPE_VBI_CAPTURE:
711 static int video_open(struct inode *inode, struct file *file)
713 int minor = iminor(inode);
714 struct cx8800_dev *h,*dev = NULL;
715 struct cx88_core *core;
716 struct cx8800_fh *fh;
717 struct list_head *list;
718 enum v4l2_buf_type type = 0;
721 list_for_each(list,&cx8800_devlist) {
722 h = list_entry(list, struct cx8800_dev, devlist);
723 if (h->video_dev->minor == minor) {
725 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
727 if (h->vbi_dev->minor == minor) {
729 type = V4L2_BUF_TYPE_VBI_CAPTURE;
732 h->radio_dev->minor == minor) {
742 dprintk(1,"open minor=%d radio=%d type=%s\n",
743 minor,radio,v4l2_type_names[type]);
745 /* allocate + initialize per filehandle data */
746 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
749 file->private_data = fh;
755 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
757 videobuf_queue_init(&fh->vidq, &cx8800_video_qops,
758 dev->pci, &dev->slock,
759 V4L2_BUF_TYPE_VIDEO_CAPTURE,
760 V4L2_FIELD_INTERLACED,
761 sizeof(struct cx88_buffer),
763 videobuf_queue_init(&fh->vbiq, &cx8800_vbi_qops,
764 dev->pci, &dev->slock,
765 V4L2_BUF_TYPE_VBI_CAPTURE,
767 sizeof(struct cx88_buffer),
771 int board = core->board;
772 dprintk(1,"video_open: setting radio device\n");
773 cx_write(MO_GP3_IO, cx88_boards[board].radio.gpio3);
774 cx_write(MO_GP0_IO, cx88_boards[board].radio.gpio0);
775 cx_write(MO_GP1_IO, cx88_boards[board].radio.gpio1);
776 cx_write(MO_GP2_IO, cx88_boards[board].radio.gpio2);
777 core->tvaudio = WW_FM;
778 cx88_set_tvaudio(core);
779 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
780 cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
787 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
789 struct cx8800_fh *fh = file->private_data;
792 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
793 if (res_locked(fh->dev,RESOURCE_VIDEO))
795 return videobuf_read_one(&fh->vidq, data, count, ppos,
796 file->f_flags & O_NONBLOCK);
797 case V4L2_BUF_TYPE_VBI_CAPTURE:
798 if (!res_get(fh->dev,fh,RESOURCE_VBI))
800 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
801 file->f_flags & O_NONBLOCK);
809 video_poll(struct file *file, struct poll_table_struct *wait)
811 struct cx8800_fh *fh = file->private_data;
812 struct cx88_buffer *buf;
814 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
815 if (!res_get(fh->dev,fh,RESOURCE_VBI))
817 return videobuf_poll_stream(file, &fh->vbiq, wait);
820 if (res_check(fh,RESOURCE_VIDEO)) {
821 /* streaming capture */
822 if (list_empty(&fh->vidq.stream))
824 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
827 buf = (struct cx88_buffer*)fh->vidq.read_buf;
831 poll_wait(file, &buf->vb.done, wait);
832 if (buf->vb.state == STATE_DONE ||
833 buf->vb.state == STATE_ERROR)
834 return POLLIN|POLLRDNORM;
838 static int video_release(struct inode *inode, struct file *file)
840 struct cx8800_fh *fh = file->private_data;
841 struct cx8800_dev *dev = fh->dev;
843 /* turn off overlay */
844 if (res_check(fh, RESOURCE_OVERLAY)) {
846 res_free(dev,fh,RESOURCE_OVERLAY);
849 /* stop video capture */
850 if (res_check(fh, RESOURCE_VIDEO)) {
851 videobuf_queue_cancel(&fh->vidq);
852 res_free(dev,fh,RESOURCE_VIDEO);
854 if (fh->vidq.read_buf) {
855 buffer_release(&fh->vidq,fh->vidq.read_buf);
856 kfree(fh->vidq.read_buf);
859 /* stop vbi capture */
860 if (res_check(fh, RESOURCE_VBI)) {
861 if (fh->vbiq.streaming)
862 videobuf_streamoff(&fh->vbiq);
863 if (fh->vbiq.reading)
864 videobuf_read_stop(&fh->vbiq);
865 res_free(dev,fh,RESOURCE_VBI);
868 videobuf_mmap_free(&fh->vidq);
869 videobuf_mmap_free(&fh->vbiq);
870 file->private_data = NULL;
873 cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
879 video_mmap(struct file *file, struct vm_area_struct * vma)
881 struct cx8800_fh *fh = file->private_data;
883 return videobuf_mmap_mapper(get_queue(fh), vma);
886 /* ------------------------------------------------------------------ */
887 /* VIDEO CTRL IOCTLS */
889 int cx88_get_control (struct cx88_core *core, struct v4l2_control *ctl)
891 struct cx88_ctrl *c = NULL;
895 for (i = 0; i < CX8800_CTLS; i++)
896 if (cx8800_ctls[i].v.id == ctl->id)
898 if (unlikely(NULL == c))
901 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
903 case V4L2_CID_AUDIO_BALANCE:
904 ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
905 : (0x7f - (value & 0x7f));
907 case V4L2_CID_AUDIO_VOLUME:
908 ctl->value = 0x3f - (value & 0x3f);
911 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
914 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
915 ctl->id, c->v.name, ctl->value, c->reg,
916 value,c->mask, c->sreg ? " [shadowed]" : "");
919 EXPORT_SYMBOL(cx88_get_control);
921 int cx88_set_control(struct cx88_core *core, struct v4l2_control *ctl)
923 struct cx88_ctrl *c = NULL;
927 for (i = 0; i < CX8800_CTLS; i++) {
928 if (cx8800_ctls[i].v.id == ctl->id) {
932 if (unlikely(NULL == c))
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;
941 case V4L2_CID_AUDIO_BALANCE:
942 value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
944 case V4L2_CID_AUDIO_VOLUME:
945 value = 0x3f - (ctl->value & 0x3f);
947 case V4L2_CID_SATURATION:
948 /* special v_sat handling */
950 value = ((ctl->value - c->off) << c->shift) & c->mask;
952 if (core->tvnorm & V4L2_STD_SECAM) {
953 /* For SECAM, both U and V sat should be equal */
954 value=value<<8|value;
956 /* Keeps U Saturation proportional to V Sat */
957 value=(value*0x5a)/0x7f<<8|value;
962 value = ((ctl->value - c->off) << c->shift) & c->mask;
965 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
966 ctl->id, c->v.name, ctl->value, c->reg, value,
967 mask, c->sreg ? " [shadowed]" : "");
969 cx_sandor(c->sreg, c->reg, mask, value);
971 cx_andor(c->reg, mask, value);
975 EXPORT_SYMBOL(cx88_set_control);
977 static void init_controls(struct cx88_core *core)
979 struct v4l2_control ctrl;
982 for (i = 0; i < CX8800_CTLS; i++) {
983 ctrl.id=cx8800_ctls[i].v.id;
984 ctrl.value=cx8800_ctls[i].v.default_value;
986 cx88_set_control(core, &ctrl);
990 /* ------------------------------------------------------------------ */
993 static int vidioc_g_fmt_cap (struct file *file, void *priv,
994 struct v4l2_format *f)
996 struct cx8800_fh *fh = priv;
998 f->fmt.pix.width = fh->width;
999 f->fmt.pix.height = fh->height;
1000 f->fmt.pix.field = fh->vidq.field;
1001 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1002 f->fmt.pix.bytesperline =
1003 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1004 f->fmt.pix.sizeimage =
1005 f->fmt.pix.height * f->fmt.pix.bytesperline;
1009 static int vidioc_try_fmt_cap (struct file *file, void *priv,
1010 struct v4l2_format *f)
1012 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1013 struct cx8800_fmt *fmt;
1014 enum v4l2_field field;
1015 unsigned int maxw, maxh;
1017 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1021 field = f->fmt.pix.field;
1022 maxw = norm_maxw(core->tvnorm);
1023 maxh = norm_maxh(core->tvnorm);
1025 if (V4L2_FIELD_ANY == field) {
1026 field = (f->fmt.pix.height > maxh/2)
1027 ? V4L2_FIELD_INTERLACED
1028 : V4L2_FIELD_BOTTOM;
1032 case V4L2_FIELD_TOP:
1033 case V4L2_FIELD_BOTTOM:
1036 case V4L2_FIELD_INTERLACED:
1042 f->fmt.pix.field = field;
1043 if (f->fmt.pix.height < 32)
1044 f->fmt.pix.height = 32;
1045 if (f->fmt.pix.height > maxh)
1046 f->fmt.pix.height = maxh;
1047 if (f->fmt.pix.width < 48)
1048 f->fmt.pix.width = 48;
1049 if (f->fmt.pix.width > maxw)
1050 f->fmt.pix.width = maxw;
1051 f->fmt.pix.width &= ~0x03;
1052 f->fmt.pix.bytesperline =
1053 (f->fmt.pix.width * fmt->depth) >> 3;
1054 f->fmt.pix.sizeimage =
1055 f->fmt.pix.height * f->fmt.pix.bytesperline;
1060 static int vidioc_s_fmt_cap (struct file *file, void *priv,
1061 struct v4l2_format *f)
1063 struct cx8800_fh *fh = priv;
1064 int err = vidioc_try_fmt_cap (file,priv,f);
1068 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1069 fh->width = f->fmt.pix.width;
1070 fh->height = f->fmt.pix.height;
1071 fh->vidq.field = f->fmt.pix.field;
1075 static int vidioc_querycap (struct file *file, void *priv,
1076 struct v4l2_capability *cap)
1078 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
1079 struct cx88_core *core = dev->core;
1081 strcpy(cap->driver, "cx8800");
1082 strlcpy(cap->card, cx88_boards[core->board].name,
1084 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1085 cap->version = CX88_VERSION_CODE;
1087 V4L2_CAP_VIDEO_CAPTURE |
1088 V4L2_CAP_READWRITE |
1089 V4L2_CAP_STREAMING |
1090 V4L2_CAP_VBI_CAPTURE;
1091 if (UNSET != core->tuner_type)
1092 cap->capabilities |= V4L2_CAP_TUNER;
1096 static int vidioc_enum_fmt_cap (struct file *file, void *priv,
1097 struct v4l2_fmtdesc *f)
1099 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1102 strlcpy(f->description,formats[f->index].name,sizeof(f->description));
1103 f->pixelformat = formats[f->index].fourcc;
1108 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1109 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
1111 struct cx8800_fh *fh = priv;
1112 struct videobuf_queue *q;
1113 struct v4l2_requestbuffers req;
1118 memset(&req,0,sizeof(req));
1121 req.memory = V4L2_MEMORY_MMAP;
1122 err = videobuf_reqbufs(q,&req);
1126 mbuf->frames = req.count;
1128 for (i = 0; i < mbuf->frames; i++) {
1129 mbuf->offsets[i] = q->bufs[i]->boff;
1130 mbuf->size += q->bufs[i]->bsize;
1136 static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
1138 struct cx8800_fh *fh = priv;
1139 return (videobuf_reqbufs(get_queue(fh), p));
1142 static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
1144 struct cx8800_fh *fh = priv;
1145 return (videobuf_querybuf(get_queue(fh), p));
1148 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1150 struct cx8800_fh *fh = priv;
1151 return (videobuf_qbuf(get_queue(fh), p));
1154 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1156 struct cx8800_fh *fh = priv;
1157 return (videobuf_dqbuf(get_queue(fh), p,
1158 file->f_flags & O_NONBLOCK));
1161 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1163 struct cx8800_fh *fh = priv;
1164 struct cx8800_dev *dev = fh->dev;
1166 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1168 if (unlikely(i != fh->type))
1171 if (unlikely(!res_get(dev,fh,get_ressource(fh))))
1173 return videobuf_streamon(get_queue(fh));
1176 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1178 struct cx8800_fh *fh = priv;
1179 struct cx8800_dev *dev = fh->dev;
1182 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1187 res = get_ressource(fh);
1188 err = videobuf_streamoff(get_queue(fh));
1191 res_free(dev,fh,res);
1195 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
1197 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1199 mutex_lock(&core->lock);
1200 cx88_set_tvnorm(core,*tvnorms);
1201 mutex_unlock(&core->lock);
1206 /* only one input in this sample driver */
1207 int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
1209 static const char *iname[] = {
1210 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1211 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1212 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1213 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1214 [ CX88_VMUX_SVIDEO ] = "S-Video",
1215 [ CX88_VMUX_TELEVISION ] = "Television",
1216 [ CX88_VMUX_CABLE ] = "Cable TV",
1217 [ CX88_VMUX_DVB ] = "DVB",
1218 [ CX88_VMUX_DEBUG ] = "for debug only",
1225 if (0 == INPUT(n)->type)
1227 memset(i,0,sizeof(*i));
1229 i->type = V4L2_INPUT_TYPE_CAMERA;
1230 strcpy(i->name,iname[INPUT(n)->type]);
1231 if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
1232 (CX88_VMUX_CABLE == INPUT(n)->type))
1233 i->type = V4L2_INPUT_TYPE_TUNER;
1234 i->std = CX88_NORMS;
1237 EXPORT_SYMBOL(cx88_enum_input);
1239 static int vidioc_enum_input (struct file *file, void *priv,
1240 struct v4l2_input *i)
1242 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1243 return cx88_enum_input (core,i);
1246 static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1248 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1254 static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1256 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1261 mutex_lock(&core->lock);
1262 cx88_newstation(core);
1263 cx88_video_mux(core,i);
1264 mutex_unlock(&core->lock);
1270 static int vidioc_queryctrl (struct file *file, void *priv,
1271 struct v4l2_queryctrl *qctrl)
1273 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1274 if (unlikely(qctrl->id == 0))
1276 return cx8800_ctrl_query(qctrl);
1279 static int vidioc_g_ctrl (struct file *file, void *priv,
1280 struct v4l2_control *ctl)
1282 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1284 cx88_get_control(core,ctl);
1287 static int vidioc_s_ctrl (struct file *file, void *priv,
1288 struct v4l2_control *ctl)
1290 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1292 cx88_set_control(core,ctl);
1295 static int vidioc_g_tuner (struct file *file, void *priv,
1296 struct v4l2_tuner *t)
1298 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1301 if (unlikely(UNSET == core->tuner_type))
1306 strcpy(t->name, "Television");
1307 t->type = V4L2_TUNER_ANALOG_TV;
1308 t->capability = V4L2_TUNER_CAP_NORM;
1309 t->rangehigh = 0xffffffffUL;
1311 cx88_get_stereo(core ,t);
1312 reg = cx_read(MO_DEVICE_STATUS);
1313 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1317 static int vidioc_s_tuner (struct file *file, void *priv,
1318 struct v4l2_tuner *t)
1320 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1322 if (UNSET == core->tuner_type)
1327 cx88_set_stereo(core, t->audmode, 1);
1331 static int vidioc_g_frequency (struct file *file, void *priv,
1332 struct v4l2_frequency *f)
1334 struct cx8800_fh *fh = priv;
1335 struct cx88_core *core = fh->dev->core;
1337 if (unlikely(UNSET == core->tuner_type))
1340 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1341 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1342 f->frequency = core->freq;
1344 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1349 int cx88_set_freq (struct cx88_core *core,
1350 struct v4l2_frequency *f)
1352 if (unlikely(UNSET == core->tuner_type))
1354 if (unlikely(f->tuner != 0))
1357 mutex_lock(&core->lock);
1358 core->freq = f->frequency;
1359 cx88_newstation(core);
1360 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1362 /* When changing channels it is required to reset TVAUDIO */
1364 cx88_set_tvaudio(core);
1366 mutex_unlock(&core->lock);
1370 EXPORT_SYMBOL(cx88_set_freq);
1372 static int vidioc_s_frequency (struct file *file, void *priv,
1373 struct v4l2_frequency *f)
1375 struct cx8800_fh *fh = priv;
1376 struct cx88_core *core = fh->dev->core;
1378 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1380 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1384 cx88_set_freq (core,f);
1387 #ifdef CONFIG_VIDEO_ADV_DEBUG
1388 static int vidioc_g_register (struct file *file, void *fh,
1389 struct v4l2_register *reg)
1391 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1393 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1395 /* cx2388x has a 24-bit register space */
1396 reg->val = cx_read(reg->reg&0xffffff);
1400 static int vidioc_s_register (struct file *file, void *fh,
1401 struct v4l2_register *reg)
1403 struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1405 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1407 cx_write(reg->reg&0xffffff, reg->val);
1412 /* ----------------------------------------------------------- */
1413 /* RADIO ESPECIFIC IOCTLS */
1414 /* ----------------------------------------------------------- */
1416 static int radio_querycap (struct file *file, void *priv,
1417 struct v4l2_capability *cap)
1419 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
1420 struct cx88_core *core = dev->core;
1422 strcpy(cap->driver, "cx8800");
1423 strlcpy(cap->card, cx88_boards[core->board].name,
1425 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1426 cap->version = CX88_VERSION_CODE;
1427 cap->capabilities = V4L2_CAP_TUNER;
1431 static int radio_g_tuner (struct file *file, void *priv,
1432 struct v4l2_tuner *t)
1434 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1436 if (unlikely(t->index > 0))
1439 strcpy(t->name, "Radio");
1440 t->type = V4L2_TUNER_RADIO;
1442 cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
1446 static int radio_enum_input (struct file *file, void *priv,
1447 struct v4l2_input *i)
1451 strcpy(i->name,"Radio");
1452 i->type = V4L2_INPUT_TYPE_TUNER;
1457 static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
1459 if (unlikely(a->index))
1462 memset(a,0,sizeof(*a));
1463 strcpy(a->name,"Radio");
1467 /* FIXME: Should add a standard for radio */
1469 static int radio_s_tuner (struct file *file, void *priv,
1470 struct v4l2_tuner *t)
1472 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1477 cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1482 static int radio_s_audio (struct file *file, void *fh,
1483 struct v4l2_audio *a)
1488 static int radio_s_input (struct file *file, void *fh, unsigned int i)
1493 static int radio_queryctrl (struct file *file, void *priv,
1494 struct v4l2_queryctrl *c)
1498 if (c->id < V4L2_CID_BASE ||
1499 c->id >= V4L2_CID_LASTP1)
1501 if (c->id == V4L2_CID_AUDIO_MUTE) {
1502 for (i = 0; i < CX8800_CTLS; i++)
1503 if (cx8800_ctls[i].v.id == c->id)
1505 *c = cx8800_ctls[i].v;
1511 /* ----------------------------------------------------------- */
1513 static void cx8800_vid_timeout(unsigned long data)
1515 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1516 struct cx88_core *core = dev->core;
1517 struct cx88_dmaqueue *q = &dev->vidq;
1518 struct cx88_buffer *buf;
1519 unsigned long flags;
1521 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1523 cx_clear(MO_VID_DMACNTRL, 0x11);
1524 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1526 spin_lock_irqsave(&dev->slock,flags);
1527 while (!list_empty(&q->active)) {
1528 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1529 list_del(&buf->vb.queue);
1530 buf->vb.state = STATE_ERROR;
1531 wake_up(&buf->vb.done);
1532 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1533 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1535 restart_video_queue(dev,q);
1536 spin_unlock_irqrestore(&dev->slock,flags);
1539 static char *cx88_vid_irqs[32] = {
1540 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1541 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1542 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1543 "y_sync", "u_sync", "v_sync", "vbi_sync",
1544 "opc_err", "par_err", "rip_err", "pci_abort",
1547 static void cx8800_vid_irq(struct cx8800_dev *dev)
1549 struct cx88_core *core = dev->core;
1550 u32 status, mask, count;
1552 status = cx_read(MO_VID_INTSTAT);
1553 mask = cx_read(MO_VID_INTMSK);
1554 if (0 == (status & mask))
1556 cx_write(MO_VID_INTSTAT, status);
1557 if (irq_debug || (status & mask & ~0xff))
1558 cx88_print_irqbits(core->name, "irq vid",
1559 cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1562 /* risc op code error */
1563 if (status & (1 << 16)) {
1564 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1565 cx_clear(MO_VID_DMACNTRL, 0x11);
1566 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1567 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1571 if (status & 0x01) {
1572 spin_lock(&dev->slock);
1573 count = cx_read(MO_VIDY_GPCNT);
1574 cx88_wakeup(core, &dev->vidq, count);
1575 spin_unlock(&dev->slock);
1579 if (status & 0x08) {
1580 spin_lock(&dev->slock);
1581 count = cx_read(MO_VBI_GPCNT);
1582 cx88_wakeup(core, &dev->vbiq, count);
1583 spin_unlock(&dev->slock);
1587 if (status & 0x10) {
1588 dprintk(2,"stopper video\n");
1589 spin_lock(&dev->slock);
1590 restart_video_queue(dev,&dev->vidq);
1591 spin_unlock(&dev->slock);
1595 if (status & 0x80) {
1596 dprintk(2,"stopper vbi\n");
1597 spin_lock(&dev->slock);
1598 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1599 spin_unlock(&dev->slock);
1603 static irqreturn_t cx8800_irq(int irq, void *dev_id)
1605 struct cx8800_dev *dev = dev_id;
1606 struct cx88_core *core = dev->core;
1608 int loop, handled = 0;
1610 for (loop = 0; loop < 10; loop++) {
1611 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
1614 cx_write(MO_PCI_INTSTAT, status);
1617 if (status & core->pci_irqmask)
1618 cx88_core_irq(core,status);
1620 cx8800_vid_irq(dev);
1623 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1625 cx_write(MO_PCI_INTMSK,0);
1629 return IRQ_RETVAL(handled);
1632 /* ----------------------------------------------------------- */
1633 /* exported stuff */
1635 static const struct file_operations video_fops =
1637 .owner = THIS_MODULE,
1639 .release = video_release,
1643 .ioctl = video_ioctl2,
1644 .compat_ioctl = v4l_compat_ioctl32,
1645 .llseek = no_llseek,
1648 static struct video_device cx8800_vbi_template;
1649 static struct video_device cx8800_video_template =
1651 .name = "cx8800-video",
1652 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1653 .fops = &video_fops,
1655 .vidioc_querycap = vidioc_querycap,
1656 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1657 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1658 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1659 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1660 .vidioc_g_fmt_vbi = cx8800_vbi_fmt,
1661 .vidioc_try_fmt_vbi = cx8800_vbi_fmt,
1662 .vidioc_s_fmt_vbi = cx8800_vbi_fmt,
1663 .vidioc_reqbufs = vidioc_reqbufs,
1664 .vidioc_querybuf = vidioc_querybuf,
1665 .vidioc_qbuf = vidioc_qbuf,
1666 .vidioc_dqbuf = vidioc_dqbuf,
1667 .vidioc_s_std = vidioc_s_std,
1668 .vidioc_enum_input = vidioc_enum_input,
1669 .vidioc_g_input = vidioc_g_input,
1670 .vidioc_s_input = vidioc_s_input,
1671 .vidioc_queryctrl = vidioc_queryctrl,
1672 .vidioc_g_ctrl = vidioc_g_ctrl,
1673 .vidioc_s_ctrl = vidioc_s_ctrl,
1674 .vidioc_streamon = vidioc_streamon,
1675 .vidioc_streamoff = vidioc_streamoff,
1676 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1677 .vidiocgmbuf = vidiocgmbuf,
1679 .vidioc_g_tuner = vidioc_g_tuner,
1680 .vidioc_s_tuner = vidioc_s_tuner,
1681 .vidioc_g_frequency = vidioc_g_frequency,
1682 .vidioc_s_frequency = vidioc_s_frequency,
1683 #ifdef CONFIG_VIDEO_ADV_DEBUG
1684 .vidioc_g_register = vidioc_g_register,
1685 .vidioc_s_register = vidioc_s_register,
1687 .tvnorms = CX88_NORMS,
1688 .current_norm = V4L2_STD_NTSC_M,
1691 static const struct file_operations radio_fops =
1693 .owner = THIS_MODULE,
1695 .release = video_release,
1696 .ioctl = video_ioctl2,
1697 .compat_ioctl = v4l_compat_ioctl32,
1698 .llseek = no_llseek,
1701 static struct video_device cx8800_radio_template =
1703 .name = "cx8800-radio",
1704 .type = VID_TYPE_TUNER,
1706 .fops = &radio_fops,
1708 .vidioc_querycap = radio_querycap,
1709 .vidioc_g_tuner = radio_g_tuner,
1710 .vidioc_enum_input = radio_enum_input,
1711 .vidioc_g_audio = radio_g_audio,
1712 .vidioc_s_tuner = radio_s_tuner,
1713 .vidioc_s_audio = radio_s_audio,
1714 .vidioc_s_input = radio_s_input,
1715 .vidioc_queryctrl = radio_queryctrl,
1716 .vidioc_g_ctrl = vidioc_g_ctrl,
1717 .vidioc_s_ctrl = vidioc_s_ctrl,
1718 .vidioc_g_frequency = vidioc_g_frequency,
1719 .vidioc_s_frequency = vidioc_s_frequency,
1722 /* ----------------------------------------------------------- */
1724 static void cx8800_unregister_video(struct cx8800_dev *dev)
1726 if (dev->radio_dev) {
1727 if (-1 != dev->radio_dev->minor)
1728 video_unregister_device(dev->radio_dev);
1730 video_device_release(dev->radio_dev);
1731 dev->radio_dev = NULL;
1734 if (-1 != dev->vbi_dev->minor)
1735 video_unregister_device(dev->vbi_dev);
1737 video_device_release(dev->vbi_dev);
1738 dev->vbi_dev = NULL;
1740 if (dev->video_dev) {
1741 if (-1 != dev->video_dev->minor)
1742 video_unregister_device(dev->video_dev);
1744 video_device_release(dev->video_dev);
1745 dev->video_dev = NULL;
1749 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1750 const struct pci_device_id *pci_id)
1752 struct cx8800_dev *dev;
1753 struct cx88_core *core;
1757 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1763 if (pci_enable_device(pci_dev)) {
1767 core = cx88_core_get(dev->pci);
1774 /* print pci info */
1775 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
1776 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1777 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1778 "latency: %d, mmio: 0x%llx\n", core->name,
1779 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1780 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
1782 pci_set_master(pci_dev);
1783 if (!pci_dma_supported(pci_dev,DMA_32BIT_MASK)) {
1784 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1789 /* Initialize VBI template */
1790 memcpy( &cx8800_vbi_template, &cx8800_video_template,
1791 sizeof(cx8800_vbi_template) );
1792 strcpy(cx8800_vbi_template.name,"cx8800-vbi");
1793 cx8800_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
1795 /* initialize driver struct */
1796 spin_lock_init(&dev->slock);
1797 core->tvnorm = cx8800_video_template.current_norm;
1799 /* init video dma queues */
1800 INIT_LIST_HEAD(&dev->vidq.active);
1801 INIT_LIST_HEAD(&dev->vidq.queued);
1802 dev->vidq.timeout.function = cx8800_vid_timeout;
1803 dev->vidq.timeout.data = (unsigned long)dev;
1804 init_timer(&dev->vidq.timeout);
1805 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1806 MO_VID_DMACNTRL,0x11,0x00);
1808 /* init vbi dma queues */
1809 INIT_LIST_HEAD(&dev->vbiq.active);
1810 INIT_LIST_HEAD(&dev->vbiq.queued);
1811 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1812 dev->vbiq.timeout.data = (unsigned long)dev;
1813 init_timer(&dev->vbiq.timeout);
1814 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1815 MO_VID_DMACNTRL,0x88,0x00);
1818 err = request_irq(pci_dev->irq, cx8800_irq,
1819 IRQF_SHARED | IRQF_DISABLED, core->name, dev);
1821 printk(KERN_ERR "%s: can't get IRQ %d\n",
1822 core->name,pci_dev->irq);
1825 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1827 /* load and configure helper modules */
1828 if (TUNER_ABSENT != core->tuner_type)
1829 request_module("tuner");
1831 if (cx88_boards[ core->board ].audio_chip == AUDIO_CHIP_WM8775)
1832 request_module("wm8775");
1834 /* register v4l devices */
1835 dev->video_dev = cx88_vdev_init(core,dev->pci,
1836 &cx8800_video_template,"video");
1837 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1838 video_nr[core->nr]);
1840 printk(KERN_INFO "%s: can't register video device\n",
1844 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1845 core->name,dev->video_dev->minor & 0x1f);
1847 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1848 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1851 printk(KERN_INFO "%s/0: can't register vbi device\n",
1855 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1856 core->name,dev->vbi_dev->minor & 0x1f);
1858 if (core->has_radio) {
1859 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1860 &cx8800_radio_template,"radio");
1861 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1862 radio_nr[core->nr]);
1864 printk(KERN_INFO "%s/0: can't register radio device\n",
1868 printk(KERN_INFO "%s/0: registered device radio%d\n",
1869 core->name,dev->radio_dev->minor & 0x1f);
1872 /* everything worked */
1873 list_add_tail(&dev->devlist,&cx8800_devlist);
1874 pci_set_drvdata(pci_dev,dev);
1876 /* initial device configuration */
1877 mutex_lock(&core->lock);
1878 cx88_set_tvnorm(core,core->tvnorm);
1879 init_controls(core);
1880 cx88_video_mux(core,0);
1881 mutex_unlock(&core->lock);
1883 /* start tvaudio thread */
1884 if (core->tuner_type != TUNER_ABSENT) {
1885 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
1886 if (IS_ERR(core->kthread)) {
1887 err = PTR_ERR(core->kthread);
1888 printk(KERN_ERR "Failed to create cx88 audio thread, err=%d\n",
1895 cx8800_unregister_video(dev);
1896 free_irq(pci_dev->irq, dev);
1898 cx88_core_put(core,dev->pci);
1904 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1906 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1907 struct cx88_core *core = dev->core;
1910 if (core->kthread) {
1911 kthread_stop(core->kthread);
1912 core->kthread = NULL;
1915 cx88_shutdown(core); /* FIXME */
1916 pci_disable_device(pci_dev);
1918 /* unregister stuff */
1920 free_irq(pci_dev->irq, dev);
1921 cx8800_unregister_video(dev);
1922 pci_set_drvdata(pci_dev, NULL);
1925 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1926 list_del(&dev->devlist);
1927 cx88_core_put(core,dev->pci);
1932 static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1934 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1935 struct cx88_core *core = dev->core;
1937 /* stop video+vbi capture */
1938 spin_lock(&dev->slock);
1939 if (!list_empty(&dev->vidq.active)) {
1940 printk("%s: suspend video\n", core->name);
1941 stop_video_dma(dev);
1942 del_timer(&dev->vidq.timeout);
1944 if (!list_empty(&dev->vbiq.active)) {
1945 printk("%s: suspend vbi\n", core->name);
1946 cx8800_stop_vbi_dma(dev);
1947 del_timer(&dev->vbiq.timeout);
1949 spin_unlock(&dev->slock);
1951 /* FIXME -- shutdown device */
1952 cx88_shutdown(core);
1954 pci_save_state(pci_dev);
1955 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1956 pci_disable_device(pci_dev);
1957 dev->state.disabled = 1;
1962 static int cx8800_resume(struct pci_dev *pci_dev)
1964 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1965 struct cx88_core *core = dev->core;
1968 if (dev->state.disabled) {
1969 err=pci_enable_device(pci_dev);
1971 printk(KERN_ERR "%s: can't enable device\n",
1976 dev->state.disabled = 0;
1978 err= pci_set_power_state(pci_dev, PCI_D0);
1980 printk(KERN_ERR "%s: can't enable device\n",
1983 pci_disable_device(pci_dev);
1984 dev->state.disabled = 1;
1988 pci_restore_state(pci_dev);
1990 /* FIXME: re-initialize hardware */
1993 /* restart video+vbi capture */
1994 spin_lock(&dev->slock);
1995 if (!list_empty(&dev->vidq.active)) {
1996 printk("%s: resume video\n", core->name);
1997 restart_video_queue(dev,&dev->vidq);
1999 if (!list_empty(&dev->vbiq.active)) {
2000 printk("%s: resume vbi\n", core->name);
2001 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2003 spin_unlock(&dev->slock);
2009 /* ----------------------------------------------------------- */
2011 static struct pci_device_id cx8800_pci_tbl[] = {
2015 .subvendor = PCI_ANY_ID,
2016 .subdevice = PCI_ANY_ID,
2018 /* --- end of list --- */
2021 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2023 static struct pci_driver cx8800_pci_driver = {
2025 .id_table = cx8800_pci_tbl,
2026 .probe = cx8800_initdev,
2027 .remove = __devexit_p(cx8800_finidev),
2029 .suspend = cx8800_suspend,
2030 .resume = cx8800_resume,
2034 static int cx8800_init(void)
2036 printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
2037 (CX88_VERSION_CODE >> 16) & 0xff,
2038 (CX88_VERSION_CODE >> 8) & 0xff,
2039 CX88_VERSION_CODE & 0xff);
2041 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2042 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2044 return pci_register_driver(&cx8800_pci_driver);
2047 static void cx8800_fini(void)
2049 pci_unregister_driver(&cx8800_pci_driver);
2052 module_init(cx8800_init);
2053 module_exit(cx8800_fini);
2055 /* ----------------------------------------------------------- */
2060 * 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