2 * $Id: cx88-video.c,v 1.70 2005/06/20 03:36:00 mkrufky Exp $
4 * device driver for Conexant 2388x based TV cards
5 * video4linux video interface
7 * (c) 2003-04 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/kmod.h>
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/delay.h>
33 #include <linux/kthread.h>
34 #include <asm/div64.h>
38 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
39 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
40 MODULE_LICENSE("GPL");
42 /* ------------------------------------------------------------------ */
44 static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
45 static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
46 static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
48 module_param_array(video_nr, int, NULL, 0444);
49 module_param_array(vbi_nr, int, NULL, 0444);
50 module_param_array(radio_nr, int, NULL, 0444);
52 MODULE_PARM_DESC(video_nr,"video device numbers");
53 MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
54 MODULE_PARM_DESC(radio_nr,"radio device numbers");
56 static unsigned int video_debug = 0;
57 module_param(video_debug,int,0644);
58 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
60 static unsigned int irq_debug = 0;
61 module_param(irq_debug,int,0644);
62 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
64 static unsigned int vid_limit = 16;
65 module_param(vid_limit,int,0644);
66 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
68 #define dprintk(level,fmt, arg...) if (video_debug >= level) \
69 printk(KERN_DEBUG "%s/0: " fmt, dev->core->name , ## arg)
71 /* ------------------------------------------------------------------ */
73 static LIST_HEAD(cx8800_devlist);
75 /* ------------------------------------------------------------------- */
78 static struct cx88_tvnorm tvnorms[] = {
81 .id = V4L2_STD_NTSC_M,
82 .cxiformat = VideoFormatNTSC,
83 .cxoformat = 0x181f0008,
86 .id = V4L2_STD_NTSC_M_JP,
87 .cxiformat = VideoFormatNTSCJapan,
88 .cxoformat = 0x181f0008,
93 .cxiformat = VideoFormatNTSC443,
94 .cxoformat = 0x181f0008,
98 .id = V4L2_STD_PAL_BG,
99 .cxiformat = VideoFormatPAL,
100 .cxoformat = 0x181f0008,
103 .id = V4L2_STD_PAL_DK,
104 .cxiformat = VideoFormatPAL,
105 .cxoformat = 0x181f0008,
108 .id = V4L2_STD_PAL_I,
109 .cxiformat = VideoFormatPAL,
110 .cxoformat = 0x181f0008,
113 .id = V4L2_STD_PAL_M,
114 .cxiformat = VideoFormatPALM,
115 .cxoformat = 0x1c1f0008,
118 .id = V4L2_STD_PAL_N,
119 .cxiformat = VideoFormatPALN,
120 .cxoformat = 0x1c1f0008,
123 .id = V4L2_STD_PAL_Nc,
124 .cxiformat = VideoFormatPALNC,
125 .cxoformat = 0x1c1f0008,
128 .id = V4L2_STD_PAL_60,
129 .cxiformat = VideoFormatPAL60,
130 .cxoformat = 0x181f0008,
133 .id = V4L2_STD_SECAM_L,
134 .cxiformat = VideoFormatSECAM,
135 .cxoformat = 0x181f0008,
138 .id = V4L2_STD_SECAM_DK,
139 .cxiformat = VideoFormatSECAM,
140 .cxoformat = 0x181f0008,
144 static struct cx8800_fmt formats[] = {
146 .name = "8 bpp, gray",
147 .fourcc = V4L2_PIX_FMT_GREY,
148 .cxformat = ColorFormatY8,
150 .flags = FORMAT_FLAGS_PACKED,
152 .name = "15 bpp RGB, le",
153 .fourcc = V4L2_PIX_FMT_RGB555,
154 .cxformat = ColorFormatRGB15,
156 .flags = FORMAT_FLAGS_PACKED,
158 .name = "15 bpp RGB, be",
159 .fourcc = V4L2_PIX_FMT_RGB555X,
160 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
162 .flags = FORMAT_FLAGS_PACKED,
164 .name = "16 bpp RGB, le",
165 .fourcc = V4L2_PIX_FMT_RGB565,
166 .cxformat = ColorFormatRGB16,
168 .flags = FORMAT_FLAGS_PACKED,
170 .name = "16 bpp RGB, be",
171 .fourcc = V4L2_PIX_FMT_RGB565X,
172 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
174 .flags = FORMAT_FLAGS_PACKED,
176 .name = "24 bpp RGB, le",
177 .fourcc = V4L2_PIX_FMT_BGR24,
178 .cxformat = ColorFormatRGB24,
180 .flags = FORMAT_FLAGS_PACKED,
182 .name = "32 bpp RGB, le",
183 .fourcc = V4L2_PIX_FMT_BGR32,
184 .cxformat = ColorFormatRGB32,
186 .flags = FORMAT_FLAGS_PACKED,
188 .name = "32 bpp RGB, be",
189 .fourcc = V4L2_PIX_FMT_RGB32,
190 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
192 .flags = FORMAT_FLAGS_PACKED,
194 .name = "4:2:2, packed, YUYV",
195 .fourcc = V4L2_PIX_FMT_YUYV,
196 .cxformat = ColorFormatYUY2,
198 .flags = FORMAT_FLAGS_PACKED,
200 .name = "4:2:2, packed, UYVY",
201 .fourcc = V4L2_PIX_FMT_UYVY,
202 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
204 .flags = FORMAT_FLAGS_PACKED,
208 static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
212 for (i = 0; i < ARRAY_SIZE(formats); i++)
213 if (formats[i].fourcc == fourcc)
218 /* ------------------------------------------------------------------- */
220 static const struct v4l2_queryctrl no_ctl = {
222 .flags = V4L2_CTRL_FLAG_DISABLED,
225 static struct cx88_ctrl cx8800_ctls[] = {
229 .id = V4L2_CID_BRIGHTNESS,
230 .name = "Brightness",
235 .type = V4L2_CTRL_TYPE_INTEGER,
238 .reg = MO_CONTR_BRIGHT,
243 .id = V4L2_CID_CONTRAST,
249 .type = V4L2_CTRL_TYPE_INTEGER,
251 .reg = MO_CONTR_BRIGHT,
262 .type = V4L2_CTRL_TYPE_INTEGER,
269 /* strictly, this only describes only U saturation.
270 * V saturation is handled specially through code.
273 .id = V4L2_CID_SATURATION,
274 .name = "Saturation",
279 .type = V4L2_CTRL_TYPE_INTEGER,
282 .reg = MO_UV_SATURATION,
288 .id = V4L2_CID_AUDIO_MUTE,
292 .type = V4L2_CTRL_TYPE_BOOLEAN,
295 .sreg = SHADOW_AUD_VOL_CTL,
300 .id = V4L2_CID_AUDIO_VOLUME,
306 .type = V4L2_CTRL_TYPE_INTEGER,
309 .sreg = SHADOW_AUD_VOL_CTL,
314 .id = V4L2_CID_AUDIO_BALANCE,
319 .default_value = 0x40,
320 .type = V4L2_CTRL_TYPE_INTEGER,
323 .sreg = SHADOW_AUD_BAL_CTL,
328 static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
330 /* ------------------------------------------------------------------- */
331 /* resource management */
333 static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
335 if (fh->resources & bit)
336 /* have it already allocated */
341 if (dev->resources & bit) {
342 /* no, someone else uses it */
346 /* it's free, grab it */
347 fh->resources |= bit;
348 dev->resources |= bit;
349 dprintk(1,"res: get %d\n",bit);
355 int res_check(struct cx8800_fh *fh, unsigned int bit)
357 return (fh->resources & bit);
361 int res_locked(struct cx8800_dev *dev, unsigned int bit)
363 return (dev->resources & bit);
367 void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
369 if ((fh->resources & bits) != bits)
373 fh->resources &= ~bits;
374 dev->resources &= ~bits;
375 dprintk(1,"res: put %d\n",bits);
379 /* ------------------------------------------------------------------ */
381 static int video_mux(struct cx8800_dev *dev, unsigned int input)
383 struct cx88_core *core = dev->core;
385 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
386 input, INPUT(input)->vmux,
387 INPUT(input)->gpio0,INPUT(input)->gpio1,
388 INPUT(input)->gpio2,INPUT(input)->gpio3);
389 dev->core->input = input;
390 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input)->vmux << 14);
391 cx_write(MO_GP3_IO, INPUT(input)->gpio3);
392 cx_write(MO_GP0_IO, INPUT(input)->gpio0);
393 cx_write(MO_GP1_IO, INPUT(input)->gpio1);
394 cx_write(MO_GP2_IO, INPUT(input)->gpio2);
396 switch (INPUT(input)->type) {
397 case CX88_VMUX_SVIDEO:
398 cx_set(MO_AFECFG_IO, 0x00000001);
399 cx_set(MO_INPUT_FORMAT, 0x00010010);
400 cx_set(MO_FILTER_EVEN, 0x00002020);
401 cx_set(MO_FILTER_ODD, 0x00002020);
404 cx_clear(MO_AFECFG_IO, 0x00000001);
405 cx_clear(MO_INPUT_FORMAT, 0x00010010);
406 cx_clear(MO_FILTER_EVEN, 0x00002020);
407 cx_clear(MO_FILTER_ODD, 0x00002020);
413 /* ------------------------------------------------------------------ */
415 static int start_video_dma(struct cx8800_dev *dev,
416 struct cx88_dmaqueue *q,
417 struct cx88_buffer *buf)
419 struct cx88_core *core = dev->core;
421 /* setup fifo + format */
422 cx88_sram_channel_setup(dev->core, &cx88_sram_channels[SRAM_CH21],
423 buf->bpl, buf->risc.dma);
424 cx88_set_scale(dev->core, buf->vb.width, buf->vb.height, buf->vb.field);
425 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
428 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
432 cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
433 cx_set(MO_VID_INTMSK, 0x0f0011);
436 cx_set(VID_CAPTURE_CONTROL,0x06);
439 cx_set(MO_DEV_CNTRL2, (1<<5));
440 cx_set(MO_VID_DMACNTRL, 0x11);
445 static int stop_video_dma(struct cx8800_dev *dev)
447 struct cx88_core *core = dev->core;
450 cx_clear(MO_VID_DMACNTRL, 0x11);
452 /* disable capture */
453 cx_clear(VID_CAPTURE_CONTROL,0x06);
456 cx_clear(MO_PCI_INTMSK, 0x000001);
457 cx_clear(MO_VID_INTMSK, 0x0f0011);
461 static int restart_video_queue(struct cx8800_dev *dev,
462 struct cx88_dmaqueue *q)
464 struct cx88_buffer *buf, *prev;
465 struct list_head *item;
467 if (!list_empty(&q->active)) {
468 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
469 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
471 start_video_dma(dev, q, buf);
472 list_for_each(item,&q->active) {
473 buf = list_entry(item, struct cx88_buffer, vb.queue);
474 buf->count = q->count++;
476 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
482 if (list_empty(&q->queued))
484 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
486 list_del(&buf->vb.queue);
487 list_add_tail(&buf->vb.queue,&q->active);
488 start_video_dma(dev, q, buf);
489 buf->vb.state = STATE_ACTIVE;
490 buf->count = q->count++;
491 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
492 dprintk(2,"[%p/%d] restart_queue - first active\n",
495 } else if (prev->vb.width == buf->vb.width &&
496 prev->vb.height == buf->vb.height &&
497 prev->fmt == buf->fmt) {
498 list_del(&buf->vb.queue);
499 list_add_tail(&buf->vb.queue,&q->active);
500 buf->vb.state = STATE_ACTIVE;
501 buf->count = q->count++;
502 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
503 dprintk(2,"[%p/%d] restart_queue - move to active\n",
512 /* ------------------------------------------------------------------ */
515 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
517 struct cx8800_fh *fh = q->priv_data;
519 *size = fh->fmt->depth*fh->width*fh->height >> 3;
522 while (*size * *count > vid_limit * 1024 * 1024)
528 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
529 enum v4l2_field field)
531 struct cx8800_fh *fh = q->priv_data;
532 struct cx8800_dev *dev = fh->dev;
533 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
534 int rc, init_buffer = 0;
536 BUG_ON(NULL == fh->fmt);
537 if (fh->width < 48 || fh->width > norm_maxw(dev->core->tvnorm) ||
538 fh->height < 32 || fh->height > norm_maxh(dev->core->tvnorm))
540 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
541 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
544 if (buf->fmt != fh->fmt ||
545 buf->vb.width != fh->width ||
546 buf->vb.height != fh->height ||
547 buf->vb.field != field) {
549 buf->vb.width = fh->width;
550 buf->vb.height = fh->height;
551 buf->vb.field = field;
555 if (STATE_NEEDS_INIT == buf->vb.state) {
557 if (0 != (rc = videobuf_iolock(dev->pci,&buf->vb,NULL)))
562 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
563 switch (buf->vb.field) {
565 cx88_risc_buffer(dev->pci, &buf->risc,
566 buf->vb.dma.sglist, 0, UNSET,
567 buf->bpl, 0, buf->vb.height);
569 case V4L2_FIELD_BOTTOM:
570 cx88_risc_buffer(dev->pci, &buf->risc,
571 buf->vb.dma.sglist, UNSET, 0,
572 buf->bpl, 0, buf->vb.height);
574 case V4L2_FIELD_INTERLACED:
575 cx88_risc_buffer(dev->pci, &buf->risc,
576 buf->vb.dma.sglist, 0, buf->bpl,
578 buf->vb.height >> 1);
580 case V4L2_FIELD_SEQ_TB:
581 cx88_risc_buffer(dev->pci, &buf->risc,
583 0, buf->bpl * (buf->vb.height >> 1),
585 buf->vb.height >> 1);
587 case V4L2_FIELD_SEQ_BT:
588 cx88_risc_buffer(dev->pci, &buf->risc,
590 buf->bpl * (buf->vb.height >> 1), 0,
592 buf->vb.height >> 1);
598 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
600 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
601 (unsigned long)buf->risc.dma);
603 buf->vb.state = STATE_PREPARED;
607 cx88_free_buffer(dev->pci,buf);
612 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
614 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
615 struct cx88_buffer *prev;
616 struct cx8800_fh *fh = vq->priv_data;
617 struct cx8800_dev *dev = fh->dev;
618 struct cx88_dmaqueue *q = &dev->vidq;
620 /* add jump to stopper */
621 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
622 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
624 if (!list_empty(&q->queued)) {
625 list_add_tail(&buf->vb.queue,&q->queued);
626 buf->vb.state = STATE_QUEUED;
627 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
630 } else if (list_empty(&q->active)) {
631 list_add_tail(&buf->vb.queue,&q->active);
632 start_video_dma(dev, q, buf);
633 buf->vb.state = STATE_ACTIVE;
634 buf->count = q->count++;
635 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
636 dprintk(2,"[%p/%d] buffer_queue - first active\n",
640 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
641 if (prev->vb.width == buf->vb.width &&
642 prev->vb.height == buf->vb.height &&
643 prev->fmt == buf->fmt) {
644 list_add_tail(&buf->vb.queue,&q->active);
645 buf->vb.state = STATE_ACTIVE;
646 buf->count = q->count++;
647 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
648 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
652 list_add_tail(&buf->vb.queue,&q->queued);
653 buf->vb.state = STATE_QUEUED;
654 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
660 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
662 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
663 struct cx8800_fh *fh = q->priv_data;
665 cx88_free_buffer(fh->dev->pci,buf);
668 static struct videobuf_queue_ops cx8800_video_qops = {
669 .buf_setup = buffer_setup,
670 .buf_prepare = buffer_prepare,
671 .buf_queue = buffer_queue,
672 .buf_release = buffer_release,
675 /* ------------------------------------------------------------------ */
677 #if 0 /* overlay support not finished yet */
678 static u32* ov_risc_field(struct cx8800_dev *dev, struct cx8800_fh *fh,
679 u32 *rp, struct btcx_skiplist *skips,
680 u32 sync_line, int skip_even, int skip_odd)
682 int line,maxy,start,end,skip,nskips;
686 /* sync instruction */
687 *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
689 addr = (unsigned long)dev->fbuf.base;
690 addr += dev->fbuf.fmt.bytesperline * fh->win.w.top;
691 addr += (fh->fmt->depth >> 3) * fh->win.w.left;
694 for (maxy = -1, line = 0; line < fh->win.w.height;
695 line++, addr += dev->fbuf.fmt.bytesperline) {
696 if ((line%2) == 0 && skip_even)
698 if ((line%2) == 1 && skip_odd)
701 /* calculate clipping */
703 btcx_calc_skips(line, fh->win.w.width, &maxy,
704 skips, &nskips, fh->clips, fh->nclips);
706 /* write out risc code */
707 for (start = 0, skip = 0; start < fh->win.w.width; start = end) {
708 if (skip >= nskips) {
710 end = fh->win.w.width;
711 } else if (start < skips[skip].start) {
713 end = skips[skip].start;
716 end = skips[skip].end;
719 if (RISC_WRITE == ri)
720 ra = addr + (fh->fmt->depth>>3)*start;
726 if (fh->win.w.width == end)
728 ri |= (fh->fmt->depth>>3) * (end-start);
730 *(rp++)=cpu_to_le32(ri);
732 *(rp++)=cpu_to_le32(ra);
739 static int ov_risc_frame(struct cx8800_dev *dev, struct cx8800_fh *fh,
740 struct cx88_buffer *buf)
742 struct btcx_skiplist *skips;
743 u32 instructions,fields;
747 /* skip list for window clipping */
748 if (NULL == (skips = kmalloc(sizeof(*skips) * fh->nclips,GFP_KERNEL)))
752 if (V4L2_FIELD_HAS_TOP(fh->win.field))
754 if (V4L2_FIELD_HAS_BOTTOM(fh->win.field))
757 /* estimate risc mem: worst case is (clip+1) * lines instructions
758 + syncs + jump (all 2 dwords) */
759 instructions = (fh->nclips+1) * fh->win.w.height;
760 instructions += 3 + 4;
761 if ((rc = btcx_riscmem_alloc(dev->pci,&buf->risc,instructions*8)) < 0) {
766 /* write risc instructions */
768 switch (fh->win.field) {
770 rp = ov_risc_field(dev, fh, rp, skips, 0, 0, 0);
772 case V4L2_FIELD_BOTTOM:
773 rp = ov_risc_field(dev, fh, rp, skips, 0x200, 0, 0);
775 case V4L2_FIELD_INTERLACED:
776 rp = ov_risc_field(dev, fh, rp, skips, 0, 0, 1);
777 rp = ov_risc_field(dev, fh, rp, skips, 0x200, 1, 0);
783 /* save pointer to jmp instruction address */
789 static int verify_window(struct cx8800_dev *dev, struct v4l2_window *win)
791 enum v4l2_field field;
794 if (NULL == dev->fbuf.base)
796 if (win->w.width < 48 || win->w.height < 32)
798 if (win->clipcount > 2048)
802 maxw = norm_maxw(core->tvnorm);
803 maxh = norm_maxh(core->tvnorm);
805 if (V4L2_FIELD_ANY == field) {
806 field = (win->w.height > maxh/2)
807 ? V4L2_FIELD_INTERLACED
812 case V4L2_FIELD_BOTTOM:
815 case V4L2_FIELD_INTERLACED:
822 if (win->w.width > maxw)
824 if (win->w.height > maxh)
825 win->w.height = maxh;
829 static int setup_window(struct cx8800_dev *dev, struct cx8800_fh *fh,
830 struct v4l2_window *win)
832 struct v4l2_clip *clips = NULL;
833 int n,size,retval = 0;
837 retval = verify_window(dev,win);
841 /* copy clips -- luckily v4l1 + v4l2 are binary
842 compatible here ...*/
844 size = sizeof(*clips)*(n+4);
845 clips = kmalloc(size,GFP_KERNEL);
849 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
855 /* clip against screen */
856 if (NULL != dev->fbuf.base)
857 n = btcx_screen_clips(dev->fbuf.fmt.width, dev->fbuf.fmt.height,
859 btcx_sort_clips(clips,n);
861 /* 4-byte alignments */
862 switch (fh->fmt->depth) {
865 btcx_align(&win->w, clips, n, 3);
868 btcx_align(&win->w, clips, n, 1);
871 /* no alignment fixups needed */
877 down(&fh->vidq.lock);
887 /* update overlay if needed */
890 if (check_btres(fh, RESOURCE_OVERLAY)) {
891 struct bttv_buffer *new;
893 new = videobuf_alloc(sizeof(*new));
894 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
895 retval = bttv_switch_overlay(btv,fh,new);
903 /* ------------------------------------------------------------------ */
905 static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
908 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
910 case V4L2_BUF_TYPE_VBI_CAPTURE:
918 static int get_ressource(struct cx8800_fh *fh)
921 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
922 return RESOURCE_VIDEO;
923 case V4L2_BUF_TYPE_VBI_CAPTURE:
931 static int video_open(struct inode *inode, struct file *file)
933 int minor = iminor(inode);
934 struct cx8800_dev *h,*dev = NULL;
935 struct cx8800_fh *fh;
936 struct list_head *list;
937 enum v4l2_buf_type type = 0;
940 list_for_each(list,&cx8800_devlist) {
941 h = list_entry(list, struct cx8800_dev, devlist);
942 if (h->video_dev->minor == minor) {
944 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
946 if (h->vbi_dev->minor == minor) {
948 type = V4L2_BUF_TYPE_VBI_CAPTURE;
951 h->radio_dev->minor == minor) {
959 dprintk(1,"open minor=%d radio=%d type=%s\n",
960 minor,radio,v4l2_type_names[type]);
962 /* allocate + initialize per filehandle data */
963 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
966 memset(fh,0,sizeof(*fh));
967 file->private_data = fh;
973 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
975 videobuf_queue_init(&fh->vidq, &cx8800_video_qops,
976 dev->pci, &dev->slock,
977 V4L2_BUF_TYPE_VIDEO_CAPTURE,
978 V4L2_FIELD_INTERLACED,
979 sizeof(struct cx88_buffer),
981 videobuf_queue_init(&fh->vbiq, &cx8800_vbi_qops,
982 dev->pci, &dev->slock,
983 V4L2_BUF_TYPE_VBI_CAPTURE,
985 sizeof(struct cx88_buffer),
989 struct cx88_core *core = dev->core;
990 int board = core->board;
991 dprintk(1,"video_open: setting radio device\n");
992 cx_write(MO_GP0_IO, cx88_boards[board].radio.gpio0);
993 cx_write(MO_GP1_IO, cx88_boards[board].radio.gpio1);
994 cx_write(MO_GP2_IO, cx88_boards[board].radio.gpio2);
995 cx_write(MO_GP3_IO, cx88_boards[board].radio.gpio3);
996 dev->core->tvaudio = WW_FM;
997 cx88_set_tvaudio(core);
998 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
999 cx88_call_i2c_clients(dev->core,AUDC_SET_RADIO,NULL);
1006 video_read(struct file *file, char *data, size_t count, loff_t *ppos)
1008 struct cx8800_fh *fh = file->private_data;
1011 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1012 if (res_locked(fh->dev,RESOURCE_VIDEO))
1014 return videobuf_read_one(&fh->vidq, data, count, ppos,
1015 file->f_flags & O_NONBLOCK);
1016 case V4L2_BUF_TYPE_VBI_CAPTURE:
1017 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1019 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
1020 file->f_flags & O_NONBLOCK);
1028 video_poll(struct file *file, struct poll_table_struct *wait)
1030 struct cx8800_fh *fh = file->private_data;
1031 struct cx88_buffer *buf;
1033 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
1034 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1036 return videobuf_poll_stream(file, &fh->vbiq, wait);
1039 if (res_check(fh,RESOURCE_VIDEO)) {
1040 /* streaming capture */
1041 if (list_empty(&fh->vidq.stream))
1043 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
1045 /* read() capture */
1046 buf = (struct cx88_buffer*)fh->vidq.read_buf;
1050 poll_wait(file, &buf->vb.done, wait);
1051 if (buf->vb.state == STATE_DONE ||
1052 buf->vb.state == STATE_ERROR)
1053 return POLLIN|POLLRDNORM;
1057 static int video_release(struct inode *inode, struct file *file)
1059 struct cx8800_fh *fh = file->private_data;
1060 struct cx8800_dev *dev = fh->dev;
1062 /* turn off overlay */
1063 if (res_check(fh, RESOURCE_OVERLAY)) {
1065 res_free(dev,fh,RESOURCE_OVERLAY);
1068 /* stop video capture */
1069 if (res_check(fh, RESOURCE_VIDEO)) {
1070 videobuf_queue_cancel(&fh->vidq);
1071 res_free(dev,fh,RESOURCE_VIDEO);
1073 if (fh->vidq.read_buf) {
1074 buffer_release(&fh->vidq,fh->vidq.read_buf);
1075 kfree(fh->vidq.read_buf);
1078 /* stop vbi capture */
1079 if (res_check(fh, RESOURCE_VBI)) {
1080 if (fh->vbiq.streaming)
1081 videobuf_streamoff(&fh->vbiq);
1082 if (fh->vbiq.reading)
1083 videobuf_read_stop(&fh->vbiq);
1084 res_free(dev,fh,RESOURCE_VBI);
1087 videobuf_mmap_free(&fh->vidq);
1088 videobuf_mmap_free(&fh->vbiq);
1089 file->private_data = NULL;
1095 video_mmap(struct file *file, struct vm_area_struct * vma)
1097 struct cx8800_fh *fh = file->private_data;
1099 return videobuf_mmap_mapper(get_queue(fh), vma);
1102 /* ------------------------------------------------------------------ */
1104 static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl)
1106 struct cx88_core *core = dev->core;
1107 struct cx88_ctrl *c = NULL;
1111 for (i = 0; i < CX8800_CTLS; i++)
1112 if (cx8800_ctls[i].v.id == ctl->id)
1113 c = &cx8800_ctls[i];
1117 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
1119 case V4L2_CID_AUDIO_BALANCE:
1120 ctl->value = (value & 0x40) ? (value & 0x3f) : (0x40 - (value & 0x3f));
1122 case V4L2_CID_AUDIO_VOLUME:
1123 ctl->value = 0x3f - (value & 0x3f);
1126 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
1132 static int set_control(struct cx8800_dev *dev, struct v4l2_control *ctl)
1134 struct cx88_core *core = dev->core;
1135 struct cx88_ctrl *c = NULL;
1140 for (i = 0; i < CX8800_CTLS; i++)
1141 if (cx8800_ctls[i].v.id == ctl->id)
1142 c = &cx8800_ctls[i];
1146 if (ctl->value < c->v.minimum)
1148 if (ctl->value > c->v.maximum)
1151 case V4L2_CID_AUDIO_BALANCE:
1152 value = (ctl->value < 0x40) ? (0x40 - ctl->value) : ctl->value;
1154 case V4L2_CID_AUDIO_VOLUME:
1155 value = 0x3f - (ctl->value & 0x3f);
1157 case V4L2_CID_SATURATION:
1158 /* special v_sat handling */
1159 v_sat_value = ctl->value - (0x7f - 0x5a);
1160 if (v_sat_value > 0xff)
1162 if (v_sat_value < 0x00)
1164 cx_andor(MO_UV_SATURATION, 0xff00, v_sat_value << 8);
1165 /* fall through to default route for u_sat */
1167 value = ((ctl->value - c->off) << c->shift) & c->mask;
1170 dprintk(1,"set_control id=0x%X reg=0x%x val=0x%x%s\n",
1171 ctl->id, c->reg, value, c->sreg ? " [shadowed]" : "");
1173 cx_sandor(c->sreg, c->reg, c->mask, value);
1175 cx_andor(c->reg, c->mask, value);
1180 static void init_controls(struct cx8800_dev *dev)
1182 static struct v4l2_control mute = {
1183 .id = V4L2_CID_AUDIO_MUTE,
1186 static struct v4l2_control volume = {
1187 .id = V4L2_CID_AUDIO_VOLUME,
1190 static struct v4l2_control hue = {
1194 static struct v4l2_control contrast = {
1195 .id = V4L2_CID_CONTRAST,
1198 static struct v4l2_control brightness = {
1199 .id = V4L2_CID_BRIGHTNESS,
1203 set_control(dev,&mute);
1204 set_control(dev,&volume);
1205 set_control(dev,&hue);
1206 set_control(dev,&contrast);
1207 set_control(dev,&brightness);
1210 /* ------------------------------------------------------------------ */
1212 static int cx8800_g_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1213 struct v4l2_format *f)
1216 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1217 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
1218 f->fmt.pix.width = fh->width;
1219 f->fmt.pix.height = fh->height;
1220 f->fmt.pix.field = fh->vidq.field;
1221 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1222 f->fmt.pix.bytesperline =
1223 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1224 f->fmt.pix.sizeimage =
1225 f->fmt.pix.height * f->fmt.pix.bytesperline;
1227 case V4L2_BUF_TYPE_VBI_CAPTURE:
1228 cx8800_vbi_fmt(dev, f);
1235 static int cx8800_try_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1236 struct v4l2_format *f)
1239 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1241 struct cx8800_fmt *fmt;
1242 enum v4l2_field field;
1243 unsigned int maxw, maxh;
1245 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1249 field = f->fmt.pix.field;
1250 maxw = norm_maxw(dev->core->tvnorm);
1251 maxh = norm_maxh(dev->core->tvnorm);
1253 if (V4L2_FIELD_ANY == field) {
1254 field = (f->fmt.pix.height > maxh/2)
1255 ? V4L2_FIELD_INTERLACED
1256 : V4L2_FIELD_BOTTOM;
1260 case V4L2_FIELD_TOP:
1261 case V4L2_FIELD_BOTTOM:
1264 case V4L2_FIELD_INTERLACED:
1270 f->fmt.pix.field = field;
1271 if (f->fmt.pix.height < 32)
1272 f->fmt.pix.height = 32;
1273 if (f->fmt.pix.height > maxh)
1274 f->fmt.pix.height = maxh;
1275 if (f->fmt.pix.width < 48)
1276 f->fmt.pix.width = 48;
1277 if (f->fmt.pix.width > maxw)
1278 f->fmt.pix.width = maxw;
1279 f->fmt.pix.width &= ~0x03;
1280 f->fmt.pix.bytesperline =
1281 (f->fmt.pix.width * fmt->depth) >> 3;
1282 f->fmt.pix.sizeimage =
1283 f->fmt.pix.height * f->fmt.pix.bytesperline;
1287 case V4L2_BUF_TYPE_VBI_CAPTURE:
1288 cx8800_vbi_fmt(dev, f);
1295 static int cx8800_s_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1296 struct v4l2_format *f)
1301 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1302 err = cx8800_try_fmt(dev,fh,f);
1306 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1307 fh->width = f->fmt.pix.width;
1308 fh->height = f->fmt.pix.height;
1309 fh->vidq.field = f->fmt.pix.field;
1311 case V4L2_BUF_TYPE_VBI_CAPTURE:
1312 cx8800_vbi_fmt(dev, f);
1320 * This function is _not_ called directly, but from
1321 * video_generic_ioctl (and maybe others). userspace
1322 * copying is done already, arg is a kernel pointer.
1324 static int video_do_ioctl(struct inode *inode, struct file *file,
1325 unsigned int cmd, void *arg)
1327 struct cx8800_fh *fh = file->private_data;
1328 struct cx8800_dev *dev = fh->dev;
1329 struct cx88_core *core = dev->core;
1331 unsigned long flags;
1335 if (video_debug > 1)
1336 cx88_print_ioctl(core->name,cmd);
1338 case VIDIOC_QUERYCAP:
1340 struct v4l2_capability *cap = arg;
1342 memset(cap,0,sizeof(*cap));
1343 strcpy(cap->driver, "cx8800");
1344 strlcpy(cap->card, cx88_boards[core->board].name,
1346 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1347 cap->version = CX88_VERSION_CODE;
1349 V4L2_CAP_VIDEO_CAPTURE |
1350 V4L2_CAP_READWRITE |
1351 V4L2_CAP_STREAMING |
1352 V4L2_CAP_VBI_CAPTURE |
1354 V4L2_CAP_VIDEO_OVERLAY |
1357 if (UNSET != core->tuner_type)
1358 cap->capabilities |= V4L2_CAP_TUNER;
1362 /* ---------- tv norms ---------- */
1363 case VIDIOC_ENUMSTD:
1365 struct v4l2_standard *e = arg;
1369 if (i >= ARRAY_SIZE(tvnorms))
1371 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1372 tvnorms[e->index].name);
1380 v4l2_std_id *id = arg;
1382 *id = core->tvnorm->id;
1387 v4l2_std_id *id = arg;
1390 for(i = 0; i < ARRAY_SIZE(tvnorms); i++)
1391 if (*id & tvnorms[i].id)
1393 if (i == ARRAY_SIZE(tvnorms))
1397 cx88_set_tvnorm(dev->core,&tvnorms[i]);
1402 /* ------ input switching ---------- */
1403 case VIDIOC_ENUMINPUT:
1405 static const char *iname[] = {
1406 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1407 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1408 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1409 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1410 [ CX88_VMUX_SVIDEO ] = "S-Video",
1411 [ CX88_VMUX_TELEVISION ] = "Television",
1412 [ CX88_VMUX_CABLE ] = "Cable TV",
1413 [ CX88_VMUX_DVB ] = "DVB",
1414 [ CX88_VMUX_DEBUG ] = "for debug only",
1416 struct v4l2_input *i = arg;
1422 if (0 == INPUT(n)->type)
1424 memset(i,0,sizeof(*i));
1426 i->type = V4L2_INPUT_TYPE_CAMERA;
1427 strcpy(i->name,iname[INPUT(n)->type]);
1428 if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
1429 (CX88_VMUX_CABLE == INPUT(n)->type))
1430 i->type = V4L2_INPUT_TYPE_TUNER;
1431 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1432 i->std |= tvnorms[n].id;
1435 case VIDIOC_G_INPUT:
1437 unsigned int *i = arg;
1439 *i = dev->core->input;
1442 case VIDIOC_S_INPUT:
1444 unsigned int *i = arg;
1449 cx88_newstation(core);
1458 case VIDIOC_G_AUDIO:
1460 struct v4l2_audio *a = arg;
1461 unsigned int n = a->index;
1463 memset(a,0,sizeof(*a));
1467 if ((CX88_VMUX_TELEVISION == INPUT(n)->type)
1468 || (CX88_VMUX_CABLE == INPUT(n)->type)) {
1469 strcpy(a->name,"Television");
1470 // FIXME figure out if stereo received and set V4L2_AUDCAP_STEREO.
1475 if (CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD_Q == core->board) {
1476 strcpy(a->name,"Line In");
1477 a->capability = V4L2_AUDCAP_STEREO;
1482 // Audio input not available.
1487 /* --- capture ioctls ---------------------------------------- */
1488 case VIDIOC_ENUM_FMT:
1490 struct v4l2_fmtdesc *f = arg;
1491 enum v4l2_buf_type type;
1497 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1498 if (index >= ARRAY_SIZE(formats))
1500 memset(f,0,sizeof(*f));
1503 strlcpy(f->description,formats[index].name,sizeof(f->description));
1504 f->pixelformat = formats[index].fourcc;
1513 struct v4l2_format *f = arg;
1514 return cx8800_g_fmt(dev,fh,f);
1518 struct v4l2_format *f = arg;
1519 return cx8800_s_fmt(dev,fh,f);
1521 case VIDIOC_TRY_FMT:
1523 struct v4l2_format *f = arg;
1524 return cx8800_try_fmt(dev,fh,f);
1527 /* --- controls ---------------------------------------------- */
1528 case VIDIOC_QUERYCTRL:
1530 struct v4l2_queryctrl *c = arg;
1533 if (c->id < V4L2_CID_BASE ||
1534 c->id >= V4L2_CID_LASTP1)
1536 for (i = 0; i < CX8800_CTLS; i++)
1537 if (cx8800_ctls[i].v.id == c->id)
1539 if (i == CX8800_CTLS) {
1543 *c = cx8800_ctls[i].v;
1547 return get_control(dev,arg);
1549 return set_control(dev,arg);
1551 /* --- tuner ioctls ------------------------------------------ */
1552 case VIDIOC_G_TUNER:
1554 struct v4l2_tuner *t = arg;
1557 if (UNSET == core->tuner_type)
1562 memset(t,0,sizeof(*t));
1563 strcpy(t->name, "Television");
1564 t->type = V4L2_TUNER_ANALOG_TV;
1565 t->capability = V4L2_TUNER_CAP_NORM;
1566 t->rangehigh = 0xffffffffUL;
1568 cx88_get_stereo(core ,t);
1569 reg = cx_read(MO_DEVICE_STATUS);
1570 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1573 case VIDIOC_S_TUNER:
1575 struct v4l2_tuner *t = arg;
1577 if (UNSET == core->tuner_type)
1581 cx88_set_stereo(core, t->audmode, 1);
1584 case VIDIOC_G_FREQUENCY:
1586 struct v4l2_frequency *f = arg;
1588 memset(f,0,sizeof(*f));
1590 if (UNSET == core->tuner_type)
1593 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1594 f->frequency = dev->freq;
1597 case VIDIOC_S_FREQUENCY:
1599 struct v4l2_frequency *f = arg;
1601 if (UNSET == core->tuner_type)
1605 if (0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV)
1607 if (1 == fh->radio && f->type != V4L2_TUNER_RADIO)
1610 dev->freq = f->frequency;
1611 cx88_newstation(core);
1612 cx88_call_i2c_clients(dev->core,VIDIOC_S_FREQUENCY,f);
1617 /* --- streaming capture ------------------------------------- */
1620 struct video_mbuf *mbuf = arg;
1621 struct videobuf_queue *q;
1622 struct v4l2_requestbuffers req;
1626 memset(&req,0,sizeof(req));
1629 req.memory = V4L2_MEMORY_MMAP;
1630 err = videobuf_reqbufs(q,&req);
1633 memset(mbuf,0,sizeof(*mbuf));
1634 mbuf->frames = req.count;
1636 for (i = 0; i < mbuf->frames; i++) {
1637 mbuf->offsets[i] = q->bufs[i]->boff;
1638 mbuf->size += q->bufs[i]->bsize;
1642 case VIDIOC_REQBUFS:
1643 return videobuf_reqbufs(get_queue(fh), arg);
1645 case VIDIOC_QUERYBUF:
1646 return videobuf_querybuf(get_queue(fh), arg);
1649 return videobuf_qbuf(get_queue(fh), arg);
1652 return videobuf_dqbuf(get_queue(fh), arg,
1653 file->f_flags & O_NONBLOCK);
1655 case VIDIOC_STREAMON:
1657 int res = get_ressource(fh);
1659 if (!res_get(dev,fh,res))
1661 return videobuf_streamon(get_queue(fh));
1663 case VIDIOC_STREAMOFF:
1665 int res = get_ressource(fh);
1667 err = videobuf_streamoff(get_queue(fh));
1670 res_free(dev,fh,res);
1675 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1681 static int video_ioctl(struct inode *inode, struct file *file,
1682 unsigned int cmd, unsigned long arg)
1684 return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
1687 /* ----------------------------------------------------------- */
1689 static int radio_do_ioctl(struct inode *inode, struct file *file,
1690 unsigned int cmd, void *arg)
1692 struct cx8800_fh *fh = file->private_data;
1693 struct cx8800_dev *dev = fh->dev;
1694 struct cx88_core *core = dev->core;
1696 if (video_debug > 1)
1697 cx88_print_ioctl(core->name,cmd);
1700 case VIDIOC_QUERYCAP:
1702 struct v4l2_capability *cap = arg;
1704 memset(cap,0,sizeof(*cap));
1705 strcpy(cap->driver, "cx8800");
1706 strlcpy(cap->card, cx88_boards[core->board].name,
1708 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1709 cap->version = CX88_VERSION_CODE;
1710 cap->capabilities = V4L2_CAP_TUNER;
1713 case VIDIOC_G_TUNER:
1715 struct v4l2_tuner *t = arg;
1720 memset(t,0,sizeof(*t));
1721 strcpy(t->name, "Radio");
1723 cx88_call_i2c_clients(dev->core,VIDIOC_G_TUNER,t);
1726 case VIDIOC_ENUMINPUT:
1728 struct v4l2_input *i = arg;
1732 strcpy(i->name,"Radio");
1733 i->type = V4L2_INPUT_TYPE_TUNER;
1736 case VIDIOC_G_INPUT:
1742 case VIDIOC_G_AUDIO:
1744 struct v4l2_audio *a = arg;
1746 memset(a,0,sizeof(*a));
1747 strcpy(a->name,"Radio");
1752 v4l2_std_id *id = arg;
1758 struct video_tuner *v = arg;
1760 if (v->tuner) /* Only tuner 0 */
1763 cx88_call_i2c_clients(dev->core,VIDIOCSTUNER,v);
1766 case VIDIOC_S_TUNER:
1768 struct v4l2_tuner *t = arg;
1773 cx88_call_i2c_clients(dev->core,VIDIOC_S_TUNER,t);
1778 case VIDIOC_S_AUDIO:
1779 case VIDIOC_S_INPUT:
1783 case VIDIOC_QUERYCTRL:
1785 struct v4l2_queryctrl *c = arg;
1788 if (c->id < V4L2_CID_BASE ||
1789 c->id >= V4L2_CID_LASTP1)
1791 if (c->id == V4L2_CID_AUDIO_MUTE) {
1792 for (i = 0; i < CX8800_CTLS; i++)
1793 if (cx8800_ctls[i].v.id == c->id)
1795 *c = cx8800_ctls[i].v;
1804 case VIDIOC_G_FREQUENCY:
1805 case VIDIOC_S_FREQUENCY:
1806 return video_do_ioctl(inode,file,cmd,arg);
1809 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1815 static int radio_ioctl(struct inode *inode, struct file *file,
1816 unsigned int cmd, unsigned long arg)
1818 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
1821 /* ----------------------------------------------------------- */
1823 static void cx8800_vid_timeout(unsigned long data)
1825 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1826 struct cx88_core *core = dev->core;
1827 struct cx88_dmaqueue *q = &dev->vidq;
1828 struct cx88_buffer *buf;
1829 unsigned long flags;
1831 cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH21]);
1833 cx_clear(MO_VID_DMACNTRL, 0x11);
1834 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1836 spin_lock_irqsave(&dev->slock,flags);
1837 while (!list_empty(&q->active)) {
1838 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1839 list_del(&buf->vb.queue);
1840 buf->vb.state = STATE_ERROR;
1841 wake_up(&buf->vb.done);
1842 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1843 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1845 restart_video_queue(dev,q);
1846 spin_unlock_irqrestore(&dev->slock,flags);
1849 static void cx8800_vid_irq(struct cx8800_dev *dev)
1851 struct cx88_core *core = dev->core;
1852 u32 status, mask, count;
1854 status = cx_read(MO_VID_INTSTAT);
1855 mask = cx_read(MO_VID_INTMSK);
1856 if (0 == (status & mask))
1858 cx_write(MO_VID_INTSTAT, status);
1859 if (irq_debug || (status & mask & ~0xff))
1860 cx88_print_irqbits(core->name, "irq vid",
1861 cx88_vid_irqs, status, mask);
1863 /* risc op code error */
1864 if (status & (1 << 16)) {
1865 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1866 cx_clear(MO_VID_DMACNTRL, 0x11);
1867 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1868 cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH21]);
1872 if (status & 0x01) {
1873 spin_lock(&dev->slock);
1874 count = cx_read(MO_VIDY_GPCNT);
1875 cx88_wakeup(dev->core, &dev->vidq, count);
1876 spin_unlock(&dev->slock);
1880 if (status & 0x08) {
1881 spin_lock(&dev->slock);
1882 count = cx_read(MO_VBI_GPCNT);
1883 cx88_wakeup(dev->core, &dev->vbiq, count);
1884 spin_unlock(&dev->slock);
1888 if (status & 0x10) {
1889 dprintk(2,"stopper video\n");
1890 spin_lock(&dev->slock);
1891 restart_video_queue(dev,&dev->vidq);
1892 spin_unlock(&dev->slock);
1896 if (status & 0x80) {
1897 dprintk(2,"stopper vbi\n");
1898 spin_lock(&dev->slock);
1899 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1900 spin_unlock(&dev->slock);
1904 static irqreturn_t cx8800_irq(int irq, void *dev_id, struct pt_regs *regs)
1906 struct cx8800_dev *dev = dev_id;
1907 struct cx88_core *core = dev->core;
1909 int loop, handled = 0;
1911 for (loop = 0; loop < 10; loop++) {
1912 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
1915 cx_write(MO_PCI_INTSTAT, status);
1918 if (status & core->pci_irqmask)
1919 cx88_core_irq(core,status);
1921 cx8800_vid_irq(dev);
1924 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1926 cx_write(MO_PCI_INTMSK,0);
1930 return IRQ_RETVAL(handled);
1933 /* ----------------------------------------------------------- */
1934 /* exported stuff */
1936 static struct file_operations video_fops =
1938 .owner = THIS_MODULE,
1940 .release = video_release,
1944 .ioctl = video_ioctl,
1945 .llseek = no_llseek,
1948 static struct video_device cx8800_video_template =
1950 .name = "cx8800-video",
1951 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1953 .fops = &video_fops,
1957 static struct video_device cx8800_vbi_template =
1959 .name = "cx8800-vbi",
1960 .type = VID_TYPE_TELETEXT|VID_TYPE_TUNER,
1962 .fops = &video_fops,
1966 static struct file_operations radio_fops =
1968 .owner = THIS_MODULE,
1970 .release = video_release,
1971 .ioctl = radio_ioctl,
1972 .llseek = no_llseek,
1975 static struct video_device cx8800_radio_template =
1977 .name = "cx8800-radio",
1978 .type = VID_TYPE_TUNER,
1980 .fops = &radio_fops,
1984 /* ----------------------------------------------------------- */
1986 static void cx8800_unregister_video(struct cx8800_dev *dev)
1988 if (dev->radio_dev) {
1989 if (-1 != dev->radio_dev->minor)
1990 video_unregister_device(dev->radio_dev);
1992 video_device_release(dev->radio_dev);
1993 dev->radio_dev = NULL;
1996 if (-1 != dev->vbi_dev->minor)
1997 video_unregister_device(dev->vbi_dev);
1999 video_device_release(dev->vbi_dev);
2000 dev->vbi_dev = NULL;
2002 if (dev->video_dev) {
2003 if (-1 != dev->video_dev->minor)
2004 video_unregister_device(dev->video_dev);
2006 video_device_release(dev->video_dev);
2007 dev->video_dev = NULL;
2011 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
2012 const struct pci_device_id *pci_id)
2014 struct cx8800_dev *dev;
2015 struct cx88_core *core;
2016 struct tuner_addr tun_addr;
2019 dev = kmalloc(sizeof(*dev),GFP_KERNEL);
2022 memset(dev,0,sizeof(*dev));
2026 if (pci_enable_device(pci_dev)) {
2030 core = cx88_core_get(dev->pci);
2037 /* print pci info */
2038 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
2039 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
2040 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
2041 "latency: %d, mmio: 0x%lx\n", core->name,
2042 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
2043 dev->pci_lat,pci_resource_start(pci_dev,0));
2045 pci_set_master(pci_dev);
2046 if (!pci_dma_supported(pci_dev,0xffffffff)) {
2047 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
2052 /* initialize driver struct */
2053 init_MUTEX(&dev->lock);
2054 spin_lock_init(&dev->slock);
2055 core->tvnorm = tvnorms;
2057 /* init video dma queues */
2058 INIT_LIST_HEAD(&dev->vidq.active);
2059 INIT_LIST_HEAD(&dev->vidq.queued);
2060 dev->vidq.timeout.function = cx8800_vid_timeout;
2061 dev->vidq.timeout.data = (unsigned long)dev;
2062 init_timer(&dev->vidq.timeout);
2063 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
2064 MO_VID_DMACNTRL,0x11,0x00);
2066 /* init vbi dma queues */
2067 INIT_LIST_HEAD(&dev->vbiq.active);
2068 INIT_LIST_HEAD(&dev->vbiq.queued);
2069 dev->vbiq.timeout.function = cx8800_vbi_timeout;
2070 dev->vbiq.timeout.data = (unsigned long)dev;
2071 init_timer(&dev->vbiq.timeout);
2072 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
2073 MO_VID_DMACNTRL,0x88,0x00);
2076 err = request_irq(pci_dev->irq, cx8800_irq,
2077 SA_SHIRQ | SA_INTERRUPT, core->name, dev);
2079 printk(KERN_ERR "%s: can't get IRQ %d\n",
2080 core->name,pci_dev->irq);
2083 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
2085 /* load and configure helper modules */
2086 if (TUNER_ABSENT != core->tuner_type)
2087 request_module("tuner");
2088 if (core->tda9887_conf)
2089 request_module("tda9887");
2090 if (core->radio_type != UNSET) {
2091 tun_addr.v4l2_tuner = V4L2_TUNER_RADIO;
2092 tun_addr.type = core->radio_type;
2093 tun_addr.addr = core->radio_addr;
2094 cx88_call_i2c_clients(dev->core,TUNER_SET_TYPE_ADDR, &tun_addr);
2096 if (core->tuner_type != UNSET) {
2097 tun_addr.v4l2_tuner = V4L2_TUNER_ANALOG_TV;
2098 tun_addr.type = core->tuner_type;
2099 tun_addr.addr = core->tuner_addr;
2100 cx88_call_i2c_clients(dev->core,TUNER_SET_TYPE_ADDR, &tun_addr);
2103 if (core->tda9887_conf)
2104 cx88_call_i2c_clients(dev->core,TDA9887_SET_CONFIG,&core->tda9887_conf);
2106 /* register v4l devices */
2107 dev->video_dev = cx88_vdev_init(core,dev->pci,
2108 &cx8800_video_template,"video");
2109 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
2110 video_nr[core->nr]);
2112 printk(KERN_INFO "%s: can't register video device\n",
2116 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
2117 core->name,dev->video_dev->minor & 0x1f);
2119 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
2120 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
2123 printk(KERN_INFO "%s/0: can't register vbi device\n",
2127 printk(KERN_INFO "%s/0: registered device vbi%d\n",
2128 core->name,dev->vbi_dev->minor & 0x1f);
2130 if (core->has_radio) {
2131 dev->radio_dev = cx88_vdev_init(core,dev->pci,
2132 &cx8800_radio_template,"radio");
2133 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
2134 radio_nr[core->nr]);
2136 printk(KERN_INFO "%s/0: can't register radio device\n",
2140 printk(KERN_INFO "%s/0: registered device radio%d\n",
2141 core->name,dev->radio_dev->minor & 0x1f);
2144 /* everything worked */
2145 list_add_tail(&dev->devlist,&cx8800_devlist);
2146 pci_set_drvdata(pci_dev,dev);
2148 /* initial device configuration */
2151 cx88_set_tvnorm(dev->core,tvnorms);
2155 /* start tvaudio thread */
2156 if (core->tuner_type != TUNER_ABSENT)
2157 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
2161 cx8800_unregister_video(dev);
2162 free_irq(pci_dev->irq, dev);
2164 cx88_core_put(core,dev->pci);
2170 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
2172 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2175 if (dev->core->kthread) {
2176 kthread_stop(dev->core->kthread);
2177 dev->core->kthread = NULL;
2180 cx88_shutdown(dev->core); /* FIXME */
2181 pci_disable_device(pci_dev);
2183 /* unregister stuff */
2185 free_irq(pci_dev->irq, dev);
2186 cx8800_unregister_video(dev);
2187 pci_set_drvdata(pci_dev, NULL);
2190 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
2191 list_del(&dev->devlist);
2192 cx88_core_put(dev->core,dev->pci);
2196 static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
2198 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2199 struct cx88_core *core = dev->core;
2201 /* stop video+vbi capture */
2202 spin_lock(&dev->slock);
2203 if (!list_empty(&dev->vidq.active)) {
2204 printk("%s: suspend video\n", core->name);
2205 stop_video_dma(dev);
2206 del_timer(&dev->vidq.timeout);
2208 if (!list_empty(&dev->vbiq.active)) {
2209 printk("%s: suspend vbi\n", core->name);
2210 cx8800_stop_vbi_dma(dev);
2211 del_timer(&dev->vbiq.timeout);
2213 spin_unlock(&dev->slock);
2216 /* FIXME -- shutdown device */
2217 cx88_shutdown(dev->core);
2220 pci_save_state(pci_dev);
2221 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
2222 pci_disable_device(pci_dev);
2223 dev->state.disabled = 1;
2228 static int cx8800_resume(struct pci_dev *pci_dev)
2230 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2231 struct cx88_core *core = dev->core;
2233 if (dev->state.disabled) {
2234 pci_enable_device(pci_dev);
2235 dev->state.disabled = 0;
2237 pci_set_power_state(pci_dev, PCI_D0);
2238 pci_restore_state(pci_dev);
2241 /* FIXME: re-initialize hardware */
2242 cx88_reset(dev->core);
2245 /* restart video+vbi capture */
2246 spin_lock(&dev->slock);
2247 if (!list_empty(&dev->vidq.active)) {
2248 printk("%s: resume video\n", core->name);
2249 restart_video_queue(dev,&dev->vidq);
2251 if (!list_empty(&dev->vbiq.active)) {
2252 printk("%s: resume vbi\n", core->name);
2253 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2255 spin_unlock(&dev->slock);
2260 /* ----------------------------------------------------------- */
2262 static struct pci_device_id cx8800_pci_tbl[] = {
2266 .subvendor = PCI_ANY_ID,
2267 .subdevice = PCI_ANY_ID,
2269 /* --- end of list --- */
2272 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2274 static struct pci_driver cx8800_pci_driver = {
2276 .id_table = cx8800_pci_tbl,
2277 .probe = cx8800_initdev,
2278 .remove = __devexit_p(cx8800_finidev),
2280 .suspend = cx8800_suspend,
2281 .resume = cx8800_resume,
2284 static int cx8800_init(void)
2286 printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
2287 (CX88_VERSION_CODE >> 16) & 0xff,
2288 (CX88_VERSION_CODE >> 8) & 0xff,
2289 CX88_VERSION_CODE & 0xff);
2291 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2292 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2294 return pci_register_driver(&cx8800_pci_driver);
2297 static void cx8800_fini(void)
2299 pci_unregister_driver(&cx8800_pci_driver);
2302 module_init(cx8800_init);
2303 module_exit(cx8800_fini);
2305 /* ----------------------------------------------------------- */
2310 * 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