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 * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
10 * - Multituner support
11 * - video_ioctl2 conversion
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <linux/init.h>
30 #include <linux/list.h>
31 #include <linux/module.h>
32 #include <linux/moduleparam.h>
33 #include <linux/kmod.h>
34 #include <linux/kernel.h>
35 #include <linux/slab.h>
36 #include <linux/interrupt.h>
37 #include <linux/delay.h>
38 #include <linux/kthread.h>
39 #include <asm/div64.h>
42 #include <media/v4l2-common.h>
44 #ifdef CONFIG_VIDEO_V4L1_COMPAT
45 /* Include V4L1 specific functions. Should be removed soon */
46 #include <linux/videodev.h>
49 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
50 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
51 MODULE_LICENSE("GPL");
53 /* ------------------------------------------------------------------ */
55 static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
56 static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
57 static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
59 module_param_array(video_nr, int, NULL, 0444);
60 module_param_array(vbi_nr, int, NULL, 0444);
61 module_param_array(radio_nr, int, NULL, 0444);
63 MODULE_PARM_DESC(video_nr,"video device numbers");
64 MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
65 MODULE_PARM_DESC(radio_nr,"radio device numbers");
67 static unsigned int video_debug = 0;
68 module_param(video_debug,int,0644);
69 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
71 static unsigned int irq_debug = 0;
72 module_param(irq_debug,int,0644);
73 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
75 static unsigned int vid_limit = 16;
76 module_param(vid_limit,int,0644);
77 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
79 #define dprintk(level,fmt, arg...) if (video_debug >= level) \
80 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
82 /* ------------------------------------------------------------------ */
84 static LIST_HEAD(cx8800_devlist);
86 /* ------------------------------------------------------------------- */
89 static struct v4l2_tvnorm tvnorms[] = {
92 .id = V4L2_STD_NTSC_M,
95 .id = V4L2_STD_NTSC_M_JP,
98 .id = V4L2_STD_NTSC_443,
101 .id = V4L2_STD_PAL_BG,
104 .id = V4L2_STD_PAL_DK,
107 .id = V4L2_STD_PAL_I,
110 .id = V4L2_STD_PAL_M,
113 .id = V4L2_STD_PAL_N,
116 .id = V4L2_STD_PAL_Nc,
119 .id = V4L2_STD_PAL_60,
122 .id = V4L2_STD_SECAM_L,
125 .id = V4L2_STD_SECAM_DK,
129 static struct v4l2_tvnorm radionorms[] = {
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",
226 .default_value = 0x7f,
227 .type = V4L2_CTRL_TYPE_INTEGER,
230 .reg = MO_CONTR_BRIGHT,
235 .id = V4L2_CID_CONTRAST,
240 .default_value = 0x3f,
241 .type = V4L2_CTRL_TYPE_INTEGER,
244 .reg = MO_CONTR_BRIGHT,
254 .default_value = 0x7f,
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",
271 .default_value = 0x7f,
272 .type = V4L2_CTRL_TYPE_INTEGER,
275 .reg = MO_UV_SATURATION,
281 .id = V4L2_CID_AUDIO_MUTE,
286 .type = V4L2_CTRL_TYPE_BOOLEAN,
289 .sreg = SHADOW_AUD_VOL_CTL,
294 .id = V4L2_CID_AUDIO_VOLUME,
299 .default_value = 0x3f,
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 const u32 cx88_user_ctrls[] = {
330 V4L2_CID_AUDIO_VOLUME,
331 V4L2_CID_AUDIO_BALANCE,
335 EXPORT_SYMBOL(cx88_user_ctrls);
337 static const u32 *ctrl_classes[] = {
342 int cx8800_ctrl_query(struct v4l2_queryctrl *qctrl)
346 if (qctrl->id < V4L2_CID_BASE ||
347 qctrl->id >= V4L2_CID_LASTP1)
349 for (i = 0; i < CX8800_CTLS; i++)
350 if (cx8800_ctls[i].v.id == qctrl->id)
352 if (i == CX8800_CTLS) {
356 *qctrl = cx8800_ctls[i].v;
359 EXPORT_SYMBOL(cx8800_ctrl_query);
361 /* ------------------------------------------------------------------- */
362 /* resource management */
364 static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
366 struct cx88_core *core = dev->core;
367 if (fh->resources & bit)
368 /* have it already allocated */
372 mutex_lock(&core->lock);
373 if (dev->resources & bit) {
374 /* no, someone else uses it */
375 mutex_unlock(&core->lock);
378 /* it's free, grab it */
379 fh->resources |= bit;
380 dev->resources |= bit;
381 dprintk(1,"res: get %d\n",bit);
382 mutex_unlock(&core->lock);
387 int res_check(struct cx8800_fh *fh, unsigned int bit)
389 return (fh->resources & bit);
393 int res_locked(struct cx8800_dev *dev, unsigned int bit)
395 return (dev->resources & bit);
399 void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
401 struct cx88_core *core = dev->core;
402 BUG_ON((fh->resources & bits) != bits);
404 mutex_lock(&core->lock);
405 fh->resources &= ~bits;
406 dev->resources &= ~bits;
407 dprintk(1,"res: put %d\n",bits);
408 mutex_unlock(&core->lock);
411 /* ------------------------------------------------------------------ */
413 /* static int video_mux(struct cx8800_dev *dev, unsigned int input) */
414 static int video_mux(struct cx88_core *core, unsigned int input)
416 /* struct cx88_core *core = dev->core; */
418 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
419 input, INPUT(input)->vmux,
420 INPUT(input)->gpio0,INPUT(input)->gpio1,
421 INPUT(input)->gpio2,INPUT(input)->gpio3);
423 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input)->vmux << 14);
424 cx_write(MO_GP3_IO, INPUT(input)->gpio3);
425 cx_write(MO_GP0_IO, INPUT(input)->gpio0);
426 cx_write(MO_GP1_IO, INPUT(input)->gpio1);
427 cx_write(MO_GP2_IO, INPUT(input)->gpio2);
429 switch (INPUT(input)->type) {
430 case CX88_VMUX_SVIDEO:
431 cx_set(MO_AFECFG_IO, 0x00000001);
432 cx_set(MO_INPUT_FORMAT, 0x00010010);
433 cx_set(MO_FILTER_EVEN, 0x00002020);
434 cx_set(MO_FILTER_ODD, 0x00002020);
437 cx_clear(MO_AFECFG_IO, 0x00000001);
438 cx_clear(MO_INPUT_FORMAT, 0x00010010);
439 cx_clear(MO_FILTER_EVEN, 0x00002020);
440 cx_clear(MO_FILTER_ODD, 0x00002020);
444 if (cx88_boards[core->board].mpeg & CX88_MPEG_BLACKBIRD) {
445 /* sets sound input from external adc */
446 if (INPUT(input)->extadc)
447 cx_set(AUD_CTL, EN_I2SIN_ENABLE);
449 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
454 /* ------------------------------------------------------------------ */
456 static int start_video_dma(struct cx8800_dev *dev,
457 struct cx88_dmaqueue *q,
458 struct cx88_buffer *buf)
460 struct cx88_core *core = dev->core;
462 /* setup fifo + format */
463 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
464 buf->bpl, buf->risc.dma);
465 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
466 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
469 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
473 cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
475 /* Enables corresponding bits at PCI_INT_STAT:
476 bits 0 to 4: video, audio, transport stream, VIP, Host
478 bits 8 and 9: DMA complete for: SRC, DST
479 bits 10 and 11: BERR signal asserted for RISC: RD, WR
480 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
482 cx_set(MO_VID_INTMSK, 0x0f0011);
485 cx_set(VID_CAPTURE_CONTROL,0x06);
488 cx_set(MO_DEV_CNTRL2, (1<<5));
489 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
495 static int stop_video_dma(struct cx8800_dev *dev)
497 struct cx88_core *core = dev->core;
500 cx_clear(MO_VID_DMACNTRL, 0x11);
502 /* disable capture */
503 cx_clear(VID_CAPTURE_CONTROL,0x06);
506 cx_clear(MO_PCI_INTMSK, 0x000001);
507 cx_clear(MO_VID_INTMSK, 0x0f0011);
512 static int restart_video_queue(struct cx8800_dev *dev,
513 struct cx88_dmaqueue *q)
515 struct cx88_core *core = dev->core;
516 struct cx88_buffer *buf, *prev;
517 struct list_head *item;
519 if (!list_empty(&q->active)) {
520 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
521 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
523 start_video_dma(dev, q, buf);
524 list_for_each(item,&q->active) {
525 buf = list_entry(item, struct cx88_buffer, vb.queue);
526 buf->count = q->count++;
528 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
534 if (list_empty(&q->queued))
536 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
538 list_move_tail(&buf->vb.queue, &q->active);
539 start_video_dma(dev, q, buf);
540 buf->vb.state = STATE_ACTIVE;
541 buf->count = q->count++;
542 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
543 dprintk(2,"[%p/%d] restart_queue - first active\n",
546 } else if (prev->vb.width == buf->vb.width &&
547 prev->vb.height == buf->vb.height &&
548 prev->fmt == buf->fmt) {
549 list_move_tail(&buf->vb.queue, &q->active);
550 buf->vb.state = STATE_ACTIVE;
551 buf->count = q->count++;
552 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
553 dprintk(2,"[%p/%d] restart_queue - move to active\n",
562 /* ------------------------------------------------------------------ */
565 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
567 struct cx8800_fh *fh = q->priv_data;
569 *size = fh->fmt->depth*fh->width*fh->height >> 3;
572 while (*size * *count > vid_limit * 1024 * 1024)
578 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
579 enum v4l2_field field)
581 struct cx8800_fh *fh = q->priv_data;
582 struct cx8800_dev *dev = fh->dev;
583 struct cx88_core *core = dev->core;
584 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
585 int rc, init_buffer = 0;
587 BUG_ON(NULL == fh->fmt);
588 if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
589 fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
591 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
592 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
595 if (buf->fmt != fh->fmt ||
596 buf->vb.width != fh->width ||
597 buf->vb.height != fh->height ||
598 buf->vb.field != field) {
600 buf->vb.width = fh->width;
601 buf->vb.height = fh->height;
602 buf->vb.field = field;
606 if (STATE_NEEDS_INIT == buf->vb.state) {
608 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
613 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
614 switch (buf->vb.field) {
616 cx88_risc_buffer(dev->pci, &buf->risc,
617 buf->vb.dma.sglist, 0, UNSET,
618 buf->bpl, 0, buf->vb.height);
620 case V4L2_FIELD_BOTTOM:
621 cx88_risc_buffer(dev->pci, &buf->risc,
622 buf->vb.dma.sglist, UNSET, 0,
623 buf->bpl, 0, buf->vb.height);
625 case V4L2_FIELD_INTERLACED:
626 cx88_risc_buffer(dev->pci, &buf->risc,
627 buf->vb.dma.sglist, 0, buf->bpl,
629 buf->vb.height >> 1);
631 case V4L2_FIELD_SEQ_TB:
632 cx88_risc_buffer(dev->pci, &buf->risc,
634 0, buf->bpl * (buf->vb.height >> 1),
636 buf->vb.height >> 1);
638 case V4L2_FIELD_SEQ_BT:
639 cx88_risc_buffer(dev->pci, &buf->risc,
641 buf->bpl * (buf->vb.height >> 1), 0,
643 buf->vb.height >> 1);
649 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
651 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
652 (unsigned long)buf->risc.dma);
654 buf->vb.state = STATE_PREPARED;
658 cx88_free_buffer(q,buf);
663 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
665 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
666 struct cx88_buffer *prev;
667 struct cx8800_fh *fh = vq->priv_data;
668 struct cx8800_dev *dev = fh->dev;
669 struct cx88_core *core = dev->core;
670 struct cx88_dmaqueue *q = &dev->vidq;
672 /* add jump to stopper */
673 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
674 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
676 if (!list_empty(&q->queued)) {
677 list_add_tail(&buf->vb.queue,&q->queued);
678 buf->vb.state = STATE_QUEUED;
679 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
682 } else if (list_empty(&q->active)) {
683 list_add_tail(&buf->vb.queue,&q->active);
684 start_video_dma(dev, q, buf);
685 buf->vb.state = STATE_ACTIVE;
686 buf->count = q->count++;
687 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
688 dprintk(2,"[%p/%d] buffer_queue - first active\n",
692 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
693 if (prev->vb.width == buf->vb.width &&
694 prev->vb.height == buf->vb.height &&
695 prev->fmt == buf->fmt) {
696 list_add_tail(&buf->vb.queue,&q->active);
697 buf->vb.state = STATE_ACTIVE;
698 buf->count = q->count++;
699 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
700 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
704 list_add_tail(&buf->vb.queue,&q->queued);
705 buf->vb.state = STATE_QUEUED;
706 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
712 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
714 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
716 cx88_free_buffer(q,buf);
719 static struct videobuf_queue_ops cx8800_video_qops = {
720 .buf_setup = buffer_setup,
721 .buf_prepare = buffer_prepare,
722 .buf_queue = buffer_queue,
723 .buf_release = buffer_release,
726 /* ------------------------------------------------------------------ */
729 /* ------------------------------------------------------------------ */
731 static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
734 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
736 case V4L2_BUF_TYPE_VBI_CAPTURE:
744 static int get_ressource(struct cx8800_fh *fh)
747 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
748 return RESOURCE_VIDEO;
749 case V4L2_BUF_TYPE_VBI_CAPTURE:
757 static int video_open(struct inode *inode, struct file *file)
759 int minor = iminor(inode);
760 struct cx8800_dev *h,*dev = NULL;
761 struct cx88_core *core;
762 struct cx8800_fh *fh;
763 struct list_head *list;
764 enum v4l2_buf_type type = 0;
767 list_for_each(list,&cx8800_devlist) {
768 h = list_entry(list, struct cx8800_dev, devlist);
769 if (h->video_dev->minor == minor) {
771 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
773 if (h->vbi_dev->minor == minor) {
775 type = V4L2_BUF_TYPE_VBI_CAPTURE;
778 h->radio_dev->minor == minor) {
788 dprintk(1,"open minor=%d radio=%d type=%s\n",
789 minor,radio,v4l2_type_names[type]);
791 /* allocate + initialize per filehandle data */
792 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
795 file->private_data = fh;
801 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
803 videobuf_queue_init(&fh->vidq, &cx8800_video_qops,
804 dev->pci, &dev->slock,
805 V4L2_BUF_TYPE_VIDEO_CAPTURE,
806 V4L2_FIELD_INTERLACED,
807 sizeof(struct cx88_buffer),
809 videobuf_queue_init(&fh->vbiq, &cx8800_vbi_qops,
810 dev->pci, &dev->slock,
811 V4L2_BUF_TYPE_VBI_CAPTURE,
813 sizeof(struct cx88_buffer),
817 int board = core->board;
818 dprintk(1,"video_open: setting radio device\n");
819 cx_write(MO_GP3_IO, cx88_boards[board].radio.gpio3);
820 cx_write(MO_GP0_IO, cx88_boards[board].radio.gpio0);
821 cx_write(MO_GP1_IO, cx88_boards[board].radio.gpio1);
822 cx_write(MO_GP2_IO, cx88_boards[board].radio.gpio2);
823 core->tvaudio = WW_FM;
824 cx88_set_tvaudio(core);
825 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
826 cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
833 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
835 struct cx8800_fh *fh = file->private_data;
838 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
839 if (res_locked(fh->dev,RESOURCE_VIDEO))
841 return videobuf_read_one(&fh->vidq, data, count, ppos,
842 file->f_flags & O_NONBLOCK);
843 case V4L2_BUF_TYPE_VBI_CAPTURE:
844 if (!res_get(fh->dev,fh,RESOURCE_VBI))
846 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
847 file->f_flags & O_NONBLOCK);
855 video_poll(struct file *file, struct poll_table_struct *wait)
857 struct cx8800_fh *fh = file->private_data;
858 struct cx88_buffer *buf;
860 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
861 if (!res_get(fh->dev,fh,RESOURCE_VBI))
863 return videobuf_poll_stream(file, &fh->vbiq, wait);
866 if (res_check(fh,RESOURCE_VIDEO)) {
867 /* streaming capture */
868 if (list_empty(&fh->vidq.stream))
870 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
873 buf = (struct cx88_buffer*)fh->vidq.read_buf;
877 poll_wait(file, &buf->vb.done, wait);
878 if (buf->vb.state == STATE_DONE ||
879 buf->vb.state == STATE_ERROR)
880 return POLLIN|POLLRDNORM;
884 static int video_release(struct inode *inode, struct file *file)
886 struct cx8800_fh *fh = file->private_data;
887 struct cx8800_dev *dev = fh->dev;
889 /* turn off overlay */
890 if (res_check(fh, RESOURCE_OVERLAY)) {
892 res_free(dev,fh,RESOURCE_OVERLAY);
895 /* stop video capture */
896 if (res_check(fh, RESOURCE_VIDEO)) {
897 videobuf_queue_cancel(&fh->vidq);
898 res_free(dev,fh,RESOURCE_VIDEO);
900 if (fh->vidq.read_buf) {
901 buffer_release(&fh->vidq,fh->vidq.read_buf);
902 kfree(fh->vidq.read_buf);
905 /* stop vbi capture */
906 if (res_check(fh, RESOURCE_VBI)) {
907 if (fh->vbiq.streaming)
908 videobuf_streamoff(&fh->vbiq);
909 if (fh->vbiq.reading)
910 videobuf_read_stop(&fh->vbiq);
911 res_free(dev,fh,RESOURCE_VBI);
914 videobuf_mmap_free(&fh->vidq);
915 videobuf_mmap_free(&fh->vbiq);
916 file->private_data = NULL;
919 cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
925 video_mmap(struct file *file, struct vm_area_struct * vma)
927 struct cx8800_fh *fh = file->private_data;
929 return videobuf_mmap_mapper(get_queue(fh), vma);
932 /* ------------------------------------------------------------------ */
933 /* VIDEO CTRL IOCTLS */
935 static int vidioc_g_ctrl (struct file *file, void *priv,
936 struct v4l2_control *ctl)
938 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
939 struct cx88_ctrl *c = NULL;
943 for (i = 0; i < CX8800_CTLS; i++)
944 if (cx8800_ctls[i].v.id == ctl->id)
946 if (unlikely(NULL == c))
949 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
951 case V4L2_CID_AUDIO_BALANCE:
952 ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
953 : (0x7f - (value & 0x7f));
955 case V4L2_CID_AUDIO_VOLUME:
956 ctl->value = 0x3f - (value & 0x3f);
959 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
962 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
963 ctl->id, c->v.name, ctl->value, c->reg,
964 value,c->mask, c->sreg ? " [shadowed]" : "");
968 static int set_control(struct cx88_core *core, struct v4l2_control *ctl)
970 struct cx88_ctrl *c = NULL;
974 for (i = 0; i < CX8800_CTLS; i++) {
975 if (cx8800_ctls[i].v.id == ctl->id) {
979 if (unlikely(NULL == c))
982 if (ctl->value < c->v.minimum)
983 ctl->value = c->v.minimum;
984 if (ctl->value > c->v.maximum)
985 ctl->value = c->v.maximum;
988 case V4L2_CID_AUDIO_BALANCE:
989 value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
991 case V4L2_CID_AUDIO_VOLUME:
992 value = 0x3f - (ctl->value & 0x3f);
994 case V4L2_CID_SATURATION:
995 /* special v_sat handling */
997 value = ((ctl->value - c->off) << c->shift) & c->mask;
999 if (core->tvnorm->id & V4L2_STD_SECAM) {
1000 /* For SECAM, both U and V sat should be equal */
1001 value=value<<8|value;
1003 /* Keeps U Saturation proportional to V Sat */
1004 value=(value*0x5a)/0x7f<<8|value;
1009 value = ((ctl->value - c->off) << c->shift) & c->mask;
1012 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
1013 ctl->id, c->v.name, ctl->value, c->reg, value,
1014 mask, c->sreg ? " [shadowed]" : "");
1016 cx_sandor(c->sreg, c->reg, mask, value);
1018 cx_andor(c->reg, mask, value);
1023 static void init_controls(struct cx88_core *core)
1025 struct v4l2_control ctrl;
1028 for (i = 0; i < CX8800_CTLS; i++) {
1029 ctrl.id=cx8800_ctls[i].v.id;
1030 ctrl.value=cx8800_ctls[i].v.default_value;
1032 set_control(core, &ctrl);
1036 /* ------------------------------------------------------------------ */
1039 static int vidioc_g_fmt_cap (struct file *file, void *priv,
1040 struct v4l2_format *f)
1042 struct cx8800_fh *fh = priv;
1044 f->fmt.pix.width = fh->width;
1045 f->fmt.pix.height = fh->height;
1046 f->fmt.pix.field = fh->vidq.field;
1047 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1048 f->fmt.pix.bytesperline =
1049 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1050 f->fmt.pix.sizeimage =
1051 f->fmt.pix.height * f->fmt.pix.bytesperline;
1055 static int vidioc_try_fmt_cap (struct file *file, void *priv,
1056 struct v4l2_format *f)
1058 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1059 struct cx8800_fmt *fmt;
1060 enum v4l2_field field;
1061 unsigned int maxw, maxh;
1063 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1067 field = f->fmt.pix.field;
1068 maxw = norm_maxw(core->tvnorm);
1069 maxh = norm_maxh(core->tvnorm);
1071 if (V4L2_FIELD_ANY == field) {
1072 field = (f->fmt.pix.height > maxh/2)
1073 ? V4L2_FIELD_INTERLACED
1074 : V4L2_FIELD_BOTTOM;
1078 case V4L2_FIELD_TOP:
1079 case V4L2_FIELD_BOTTOM:
1082 case V4L2_FIELD_INTERLACED:
1088 f->fmt.pix.field = field;
1089 if (f->fmt.pix.height < 32)
1090 f->fmt.pix.height = 32;
1091 if (f->fmt.pix.height > maxh)
1092 f->fmt.pix.height = maxh;
1093 if (f->fmt.pix.width < 48)
1094 f->fmt.pix.width = 48;
1095 if (f->fmt.pix.width > maxw)
1096 f->fmt.pix.width = maxw;
1097 f->fmt.pix.width &= ~0x03;
1098 f->fmt.pix.bytesperline =
1099 (f->fmt.pix.width * fmt->depth) >> 3;
1100 f->fmt.pix.sizeimage =
1101 f->fmt.pix.height * f->fmt.pix.bytesperline;
1106 static int vidioc_s_fmt_cap (struct file *file, void *priv,
1107 struct v4l2_format *f)
1109 struct cx8800_fh *fh = priv;
1110 int err = vidioc_try_fmt_cap (file,priv,f);
1114 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1115 fh->width = f->fmt.pix.width;
1116 fh->height = f->fmt.pix.height;
1117 fh->vidq.field = f->fmt.pix.field;
1121 static int vidioc_querycap (struct file *file, void *priv,
1122 struct v4l2_capability *cap)
1124 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
1125 struct cx88_core *core = dev->core;
1127 strcpy(cap->driver, "cx8800");
1128 strlcpy(cap->card, cx88_boards[core->board].name,
1130 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1131 cap->version = CX88_VERSION_CODE;
1133 V4L2_CAP_VIDEO_CAPTURE |
1134 V4L2_CAP_READWRITE |
1135 V4L2_CAP_STREAMING |
1136 V4L2_CAP_VBI_CAPTURE;
1137 if (UNSET != core->tuner_type)
1138 cap->capabilities |= V4L2_CAP_TUNER;
1142 static int vidioc_enum_fmt_cap (struct file *file, void *priv,
1143 struct v4l2_fmtdesc *f)
1145 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1148 strlcpy(f->description,formats[f->index].name,sizeof(f->description));
1149 f->pixelformat = formats[f->index].fourcc;
1154 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1155 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
1157 struct cx8800_fh *fh = priv;
1158 struct videobuf_queue *q;
1159 struct v4l2_requestbuffers req;
1164 memset(&req,0,sizeof(req));
1167 req.memory = V4L2_MEMORY_MMAP;
1168 err = videobuf_reqbufs(q,&req);
1172 mbuf->frames = req.count;
1174 for (i = 0; i < mbuf->frames; i++) {
1175 mbuf->offsets[i] = q->bufs[i]->boff;
1176 mbuf->size += q->bufs[i]->bsize;
1183 static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
1185 struct cx8800_fh *fh = priv;
1186 return (videobuf_reqbufs(get_queue(fh), p));
1189 static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
1191 struct cx8800_fh *fh = priv;
1192 return (videobuf_querybuf(get_queue(fh), p));
1195 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1197 struct cx8800_fh *fh = priv;
1198 return (videobuf_qbuf(get_queue(fh), p));
1201 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1203 struct cx8800_fh *fh = priv;
1204 return (videobuf_dqbuf(get_queue(fh), p,
1205 file->f_flags & O_NONBLOCK));
1208 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1210 struct cx8800_fh *fh = priv;
1211 struct cx8800_dev *dev = fh->dev;
1213 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1215 if (unlikely(i != fh->type))
1218 if (unlikely(!res_get(dev,fh,get_ressource(fh))))
1220 return videobuf_streamon(get_queue(fh));
1223 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1225 struct cx8800_fh *fh = priv;
1226 struct cx8800_dev *dev = fh->dev;
1229 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1234 res = get_ressource(fh);
1235 err = videobuf_streamoff(get_queue(fh));
1238 res_free(dev,fh,res);
1242 static int vidioc_s_std (struct file *file, void *priv, unsigned int i)
1244 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1246 mutex_lock(&core->lock);
1247 cx88_set_tvnorm(core,&tvnorms[i]);
1248 mutex_unlock(&core->lock);
1252 /* only one input in this sample driver */
1253 static int vidioc_enum_input (struct file *file, void *priv,
1254 struct v4l2_input *i)
1256 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1258 static const char *iname[] = {
1259 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1260 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1261 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1262 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1263 [ CX88_VMUX_SVIDEO ] = "S-Video",
1264 [ CX88_VMUX_TELEVISION ] = "Television",
1265 [ CX88_VMUX_CABLE ] = "Cable TV",
1266 [ CX88_VMUX_DVB ] = "DVB",
1267 [ CX88_VMUX_DEBUG ] = "for debug only",
1274 if (0 == INPUT(n)->type)
1276 memset(i,0,sizeof(*i));
1278 i->type = V4L2_INPUT_TYPE_CAMERA;
1279 strcpy(i->name,iname[INPUT(n)->type]);
1280 if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
1281 (CX88_VMUX_CABLE == INPUT(n)->type))
1282 i->type = V4L2_INPUT_TYPE_TUNER;
1283 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1284 i->std |= tvnorms[n].id;
1288 static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1290 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1296 static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1298 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1303 mutex_lock(&core->lock);
1304 cx88_newstation(core);
1306 mutex_unlock(&core->lock);
1312 static int vidioc_queryctrl (struct file *file, void *priv,
1313 struct v4l2_queryctrl *qctrl)
1315 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1316 if (unlikely(qctrl->id == 0))
1318 return cx8800_ctrl_query(qctrl);
1321 static int vidioc_s_ctrl (struct file *file, void *priv,
1322 struct v4l2_control *ctl)
1324 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1327 set_control(core,ctl);
1330 static int vidioc_g_tuner (struct file *file, void *priv,
1331 struct v4l2_tuner *t)
1333 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1336 if (unlikely(UNSET == core->tuner_type))
1339 strcpy(t->name, "Television");
1340 t->type = V4L2_TUNER_ANALOG_TV;
1341 t->capability = V4L2_TUNER_CAP_NORM;
1342 t->rangehigh = 0xffffffffUL;
1344 cx88_get_stereo(core ,t);
1345 reg = cx_read(MO_DEVICE_STATUS);
1346 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1350 static int vidioc_s_tuner (struct file *file, void *priv,
1351 struct v4l2_tuner *t)
1353 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1355 if (UNSET == core->tuner_type)
1360 cx88_set_stereo(core, t->audmode, 1);
1364 static int vidioc_g_frequency (struct file *file, void *priv,
1365 struct v4l2_frequency *f)
1367 struct cx8800_fh *fh = priv;
1368 struct cx88_core *core = fh->dev->core;
1370 if (unlikely(UNSET == core->tuner_type))
1373 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1374 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1375 f->frequency = core->freq;
1377 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1382 static int vidioc_s_frequency (struct file *file, void *priv,
1383 struct v4l2_frequency *f)
1385 struct cx8800_fh *fh = priv;
1386 struct cx88_core *core = fh->dev->core;
1388 if (unlikely(UNSET == core->tuner_type))
1390 if (unlikely(f->tuner != 0))
1392 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1394 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1396 mutex_lock(&core->lock);
1397 core->freq = f->frequency;
1398 cx88_newstation(core);
1399 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1401 /* When changing channels it is required to reset TVAUDIO */
1403 cx88_set_tvaudio(core);
1405 mutex_unlock(&core->lock);
1410 /* ----------------------------------------------------------- */
1411 /* RADIO ESPECIFIC IOCTLS */
1412 /* ----------------------------------------------------------- */
1414 static int radio_querycap (struct file *file, void *priv,
1415 struct v4l2_capability *cap)
1417 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
1418 struct cx88_core *core = dev->core;
1420 strcpy(cap->driver, "cx8800");
1421 strlcpy(cap->card, cx88_boards[core->board].name,
1423 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1424 cap->version = CX88_VERSION_CODE;
1425 cap->capabilities = V4L2_CAP_TUNER;
1429 static int radio_g_tuner (struct file *file, void *priv,
1430 struct v4l2_tuner *t)
1432 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1434 if (unlikely(t->index > 0))
1437 strcpy(t->name, "Radio");
1438 t->type = V4L2_TUNER_RADIO;
1440 cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
1444 static int radio_enum_input (struct file *file, void *priv,
1445 struct v4l2_input *i)
1449 strcpy(i->name,"Radio");
1450 i->type = V4L2_INPUT_TYPE_TUNER;
1455 static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
1457 if (unlikely(a->index))
1460 memset(a,0,sizeof(*a));
1461 strcpy(a->name,"Radio");
1465 /* FIXME: Should add a standard for radio */
1467 static int radio_s_tuner (struct file *file, void *priv,
1468 struct v4l2_tuner *t)
1470 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1475 cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1480 static int radio_s_audio (struct file *file, void *fh,
1481 struct v4l2_audio *a)
1486 static int radio_s_input (struct file *file, void *fh, unsigned int i)
1491 static int radio_queryctrl (struct file *file, void *priv,
1492 struct v4l2_queryctrl *c)
1496 if (c->id < V4L2_CID_BASE ||
1497 c->id >= V4L2_CID_LASTP1)
1499 if (c->id == V4L2_CID_AUDIO_MUTE) {
1500 for (i = 0; i < CX8800_CTLS; i++)
1501 if (cx8800_ctls[i].v.id == c->id)
1503 *c = cx8800_ctls[i].v;
1509 /* ----------------------------------------------------------- */
1511 static void cx8800_vid_timeout(unsigned long data)
1513 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1514 struct cx88_core *core = dev->core;
1515 struct cx88_dmaqueue *q = &dev->vidq;
1516 struct cx88_buffer *buf;
1517 unsigned long flags;
1519 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1521 cx_clear(MO_VID_DMACNTRL, 0x11);
1522 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1524 spin_lock_irqsave(&dev->slock,flags);
1525 while (!list_empty(&q->active)) {
1526 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1527 list_del(&buf->vb.queue);
1528 buf->vb.state = STATE_ERROR;
1529 wake_up(&buf->vb.done);
1530 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1531 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1533 restart_video_queue(dev,q);
1534 spin_unlock_irqrestore(&dev->slock,flags);
1537 static char *cx88_vid_irqs[32] = {
1538 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1539 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1540 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1541 "y_sync", "u_sync", "v_sync", "vbi_sync",
1542 "opc_err", "par_err", "rip_err", "pci_abort",
1545 static void cx8800_vid_irq(struct cx8800_dev *dev)
1547 struct cx88_core *core = dev->core;
1548 u32 status, mask, count;
1550 status = cx_read(MO_VID_INTSTAT);
1551 mask = cx_read(MO_VID_INTMSK);
1552 if (0 == (status & mask))
1554 cx_write(MO_VID_INTSTAT, status);
1555 if (irq_debug || (status & mask & ~0xff))
1556 cx88_print_irqbits(core->name, "irq vid",
1557 cx88_vid_irqs, status, mask);
1559 /* risc op code error */
1560 if (status & (1 << 16)) {
1561 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1562 cx_clear(MO_VID_DMACNTRL, 0x11);
1563 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1564 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1568 if (status & 0x01) {
1569 spin_lock(&dev->slock);
1570 count = cx_read(MO_VIDY_GPCNT);
1571 cx88_wakeup(core, &dev->vidq, count);
1572 spin_unlock(&dev->slock);
1576 if (status & 0x08) {
1577 spin_lock(&dev->slock);
1578 count = cx_read(MO_VBI_GPCNT);
1579 cx88_wakeup(core, &dev->vbiq, count);
1580 spin_unlock(&dev->slock);
1584 if (status & 0x10) {
1585 dprintk(2,"stopper video\n");
1586 spin_lock(&dev->slock);
1587 restart_video_queue(dev,&dev->vidq);
1588 spin_unlock(&dev->slock);
1592 if (status & 0x80) {
1593 dprintk(2,"stopper vbi\n");
1594 spin_lock(&dev->slock);
1595 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1596 spin_unlock(&dev->slock);
1600 static irqreturn_t cx8800_irq(int irq, void *dev_id)
1602 struct cx8800_dev *dev = dev_id;
1603 struct cx88_core *core = dev->core;
1605 int loop, handled = 0;
1607 for (loop = 0; loop < 10; loop++) {
1608 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
1611 cx_write(MO_PCI_INTSTAT, status);
1614 if (status & core->pci_irqmask)
1615 cx88_core_irq(core,status);
1617 cx8800_vid_irq(dev);
1620 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1622 cx_write(MO_PCI_INTMSK,0);
1626 return IRQ_RETVAL(handled);
1629 /* ----------------------------------------------------------- */
1630 /* exported stuff */
1632 static const struct file_operations video_fops =
1634 .owner = THIS_MODULE,
1636 .release = video_release,
1640 .ioctl = video_ioctl2,
1641 .compat_ioctl = v4l_compat_ioctl32,
1642 .llseek = no_llseek,
1645 static struct video_device cx8800_vbi_template;
1646 static struct video_device cx8800_video_template =
1648 .name = "cx8800-video",
1649 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1650 .fops = &video_fops,
1652 .vidioc_querycap = vidioc_querycap,
1653 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1654 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1655 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1656 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1657 .vidioc_g_fmt_vbi = cx8800_vbi_fmt,
1658 .vidioc_try_fmt_vbi = cx8800_vbi_fmt,
1659 .vidioc_s_fmt_vbi = cx8800_vbi_fmt,
1660 .vidioc_reqbufs = vidioc_reqbufs,
1661 .vidioc_querybuf = vidioc_querybuf,
1662 .vidioc_qbuf = vidioc_qbuf,
1663 .vidioc_dqbuf = vidioc_dqbuf,
1664 .vidioc_s_std = vidioc_s_std,
1665 .vidioc_enum_input = vidioc_enum_input,
1666 .vidioc_g_input = vidioc_g_input,
1667 .vidioc_s_input = vidioc_s_input,
1668 .vidioc_queryctrl = vidioc_queryctrl,
1669 .vidioc_g_ctrl = vidioc_g_ctrl,
1670 .vidioc_s_ctrl = vidioc_s_ctrl,
1671 .vidioc_streamon = vidioc_streamon,
1672 .vidioc_streamoff = vidioc_streamoff,
1673 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1674 .vidiocgmbuf = vidiocgmbuf,
1676 .vidioc_g_tuner = vidioc_g_tuner,
1677 .vidioc_s_tuner = vidioc_s_tuner,
1678 .vidioc_g_frequency = vidioc_g_frequency,
1679 .vidioc_s_frequency = vidioc_s_frequency,
1681 .tvnormsize = ARRAY_SIZE(tvnorms),
1684 static const struct file_operations radio_fops =
1686 .owner = THIS_MODULE,
1688 .release = video_release,
1689 .ioctl = video_ioctl2,
1690 .compat_ioctl = v4l_compat_ioctl32,
1691 .llseek = no_llseek,
1694 static struct video_device cx8800_radio_template =
1696 .name = "cx8800-radio",
1697 .type = VID_TYPE_TUNER,
1699 .fops = &radio_fops,
1701 .vidioc_querycap = radio_querycap,
1702 .vidioc_g_tuner = radio_g_tuner,
1703 .vidioc_enum_input = radio_enum_input,
1704 .vidioc_g_audio = radio_g_audio,
1705 .vidioc_s_tuner = radio_s_tuner,
1706 .vidioc_s_audio = radio_s_audio,
1707 .vidioc_s_input = radio_s_input,
1708 .vidioc_queryctrl = radio_queryctrl,
1709 .vidioc_g_ctrl = vidioc_g_ctrl,
1710 .vidioc_s_ctrl = vidioc_s_ctrl,
1711 .vidioc_g_frequency = vidioc_g_frequency,
1712 .vidioc_s_frequency = vidioc_s_frequency,
1713 .tvnorms = radionorms,
1714 .tvnormsize = ARRAY_SIZE(radionorms),
1717 /* ----------------------------------------------------------- */
1719 static void cx8800_unregister_video(struct cx8800_dev *dev)
1721 if (dev->radio_dev) {
1722 if (-1 != dev->radio_dev->minor)
1723 video_unregister_device(dev->radio_dev);
1725 video_device_release(dev->radio_dev);
1726 dev->radio_dev = NULL;
1729 if (-1 != dev->vbi_dev->minor)
1730 video_unregister_device(dev->vbi_dev);
1732 video_device_release(dev->vbi_dev);
1733 dev->vbi_dev = NULL;
1735 if (dev->video_dev) {
1736 if (-1 != dev->video_dev->minor)
1737 video_unregister_device(dev->video_dev);
1739 video_device_release(dev->video_dev);
1740 dev->video_dev = NULL;
1744 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1745 const struct pci_device_id *pci_id)
1747 struct cx8800_dev *dev;
1748 struct cx88_core *core;
1752 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1758 if (pci_enable_device(pci_dev)) {
1762 core = cx88_core_get(dev->pci);
1769 /* print pci info */
1770 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
1771 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1772 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1773 "latency: %d, mmio: 0x%llx\n", core->name,
1774 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1775 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
1777 pci_set_master(pci_dev);
1778 if (!pci_dma_supported(pci_dev,0xffffffff)) {
1779 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1784 /* Initialize VBI template */
1785 memcpy( &cx8800_vbi_template, &cx8800_video_template,
1786 sizeof(cx8800_vbi_template) );
1787 strcpy(cx8800_vbi_template.name,"cx8800-vbi");
1788 cx8800_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
1790 /* initialize driver struct */
1791 spin_lock_init(&dev->slock);
1792 core->tvnorm = tvnorms;
1794 /* init video dma queues */
1795 INIT_LIST_HEAD(&dev->vidq.active);
1796 INIT_LIST_HEAD(&dev->vidq.queued);
1797 dev->vidq.timeout.function = cx8800_vid_timeout;
1798 dev->vidq.timeout.data = (unsigned long)dev;
1799 init_timer(&dev->vidq.timeout);
1800 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1801 MO_VID_DMACNTRL,0x11,0x00);
1803 /* init vbi dma queues */
1804 INIT_LIST_HEAD(&dev->vbiq.active);
1805 INIT_LIST_HEAD(&dev->vbiq.queued);
1806 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1807 dev->vbiq.timeout.data = (unsigned long)dev;
1808 init_timer(&dev->vbiq.timeout);
1809 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1810 MO_VID_DMACNTRL,0x88,0x00);
1813 err = request_irq(pci_dev->irq, cx8800_irq,
1814 IRQF_SHARED | IRQF_DISABLED, core->name, dev);
1816 printk(KERN_ERR "%s: can't get IRQ %d\n",
1817 core->name,pci_dev->irq);
1820 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1822 /* load and configure helper modules */
1823 if (TUNER_ABSENT != core->tuner_type)
1824 request_module("tuner");
1826 if (cx88_boards[ core->board ].audio_chip == AUDIO_CHIP_WM8775)
1827 request_module("wm8775");
1829 /* register v4l devices */
1830 dev->video_dev = cx88_vdev_init(core,dev->pci,
1831 &cx8800_video_template,"video");
1832 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1833 video_nr[core->nr]);
1835 printk(KERN_INFO "%s: can't register video device\n",
1839 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1840 core->name,dev->video_dev->minor & 0x1f);
1842 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1843 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1846 printk(KERN_INFO "%s/0: can't register vbi device\n",
1850 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1851 core->name,dev->vbi_dev->minor & 0x1f);
1853 if (core->has_radio) {
1854 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1855 &cx8800_radio_template,"radio");
1856 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1857 radio_nr[core->nr]);
1859 printk(KERN_INFO "%s/0: can't register radio device\n",
1863 printk(KERN_INFO "%s/0: registered device radio%d\n",
1864 core->name,dev->radio_dev->minor & 0x1f);
1867 /* everything worked */
1868 list_add_tail(&dev->devlist,&cx8800_devlist);
1869 pci_set_drvdata(pci_dev,dev);
1871 /* initial device configuration */
1872 mutex_lock(&core->lock);
1873 cx88_set_tvnorm(core,tvnorms);
1874 init_controls(core);
1876 mutex_unlock(&core->lock);
1878 /* start tvaudio thread */
1879 if (core->tuner_type != TUNER_ABSENT)
1880 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
1884 cx8800_unregister_video(dev);
1885 free_irq(pci_dev->irq, dev);
1887 cx88_core_put(core,dev->pci);
1893 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1895 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1896 struct cx88_core *core = dev->core;
1899 if (core->kthread) {
1900 kthread_stop(core->kthread);
1901 core->kthread = NULL;
1904 cx88_shutdown(core); /* FIXME */
1905 pci_disable_device(pci_dev);
1907 /* unregister stuff */
1909 free_irq(pci_dev->irq, dev);
1910 cx8800_unregister_video(dev);
1911 pci_set_drvdata(pci_dev, NULL);
1914 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1915 list_del(&dev->devlist);
1916 cx88_core_put(core,dev->pci);
1921 static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1923 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1924 struct cx88_core *core = dev->core;
1926 /* stop video+vbi capture */
1927 spin_lock(&dev->slock);
1928 if (!list_empty(&dev->vidq.active)) {
1929 printk("%s: suspend video\n", core->name);
1930 stop_video_dma(dev);
1931 del_timer(&dev->vidq.timeout);
1933 if (!list_empty(&dev->vbiq.active)) {
1934 printk("%s: suspend vbi\n", core->name);
1935 cx8800_stop_vbi_dma(dev);
1936 del_timer(&dev->vbiq.timeout);
1938 spin_unlock(&dev->slock);
1940 /* FIXME -- shutdown device */
1941 cx88_shutdown(core);
1943 pci_save_state(pci_dev);
1944 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1945 pci_disable_device(pci_dev);
1946 dev->state.disabled = 1;
1951 static int cx8800_resume(struct pci_dev *pci_dev)
1953 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1954 struct cx88_core *core = dev->core;
1957 if (dev->state.disabled) {
1958 err=pci_enable_device(pci_dev);
1960 printk(KERN_ERR "%s: can't enable device\n",
1965 dev->state.disabled = 0;
1967 err= pci_set_power_state(pci_dev, PCI_D0);
1969 printk(KERN_ERR "%s: can't enable device\n",
1972 pci_disable_device(pci_dev);
1973 dev->state.disabled = 1;
1977 pci_restore_state(pci_dev);
1979 /* FIXME: re-initialize hardware */
1982 /* restart video+vbi capture */
1983 spin_lock(&dev->slock);
1984 if (!list_empty(&dev->vidq.active)) {
1985 printk("%s: resume video\n", core->name);
1986 restart_video_queue(dev,&dev->vidq);
1988 if (!list_empty(&dev->vbiq.active)) {
1989 printk("%s: resume vbi\n", core->name);
1990 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1992 spin_unlock(&dev->slock);
1998 /* ----------------------------------------------------------- */
2000 static struct pci_device_id cx8800_pci_tbl[] = {
2004 .subvendor = PCI_ANY_ID,
2005 .subdevice = PCI_ANY_ID,
2007 /* --- end of list --- */
2010 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2012 static struct pci_driver cx8800_pci_driver = {
2014 .id_table = cx8800_pci_tbl,
2015 .probe = cx8800_initdev,
2016 .remove = __devexit_p(cx8800_finidev),
2018 .suspend = cx8800_suspend,
2019 .resume = cx8800_resume,
2023 static int cx8800_init(void)
2025 printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
2026 (CX88_VERSION_CODE >> 16) & 0xff,
2027 (CX88_VERSION_CODE >> 8) & 0xff,
2028 CX88_VERSION_CODE & 0xff);
2030 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2031 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2033 return pci_register_driver(&cx8800_pci_driver);
2036 static void cx8800_fini(void)
2038 pci_unregister_driver(&cx8800_pci_driver);
2041 module_init(cx8800_init);
2042 module_exit(cx8800_fini);
2044 EXPORT_SYMBOL(cx88_do_ioctl);
2046 /* ----------------------------------------------------------- */
2051 * 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