2 * $Id: cx88-video.c,v 1.58 2005/03/07 15:58:05 kraxel 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 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 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,
1191 set_control(dev,&mute);
1192 set_control(dev,&volume);
1195 /* ------------------------------------------------------------------ */
1197 static int cx8800_g_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1198 struct v4l2_format *f)
1201 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1202 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
1203 f->fmt.pix.width = fh->width;
1204 f->fmt.pix.height = fh->height;
1205 f->fmt.pix.field = fh->vidq.field;
1206 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1207 f->fmt.pix.bytesperline =
1208 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1209 f->fmt.pix.sizeimage =
1210 f->fmt.pix.height * f->fmt.pix.bytesperline;
1212 case V4L2_BUF_TYPE_VBI_CAPTURE:
1213 cx8800_vbi_fmt(dev, f);
1220 static int cx8800_try_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1221 struct v4l2_format *f)
1224 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1226 struct cx8800_fmt *fmt;
1227 enum v4l2_field field;
1228 unsigned int maxw, maxh;
1230 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1234 field = f->fmt.pix.field;
1235 maxw = norm_maxw(dev->core->tvnorm);
1236 maxh = norm_maxh(dev->core->tvnorm);
1238 if (V4L2_FIELD_ANY == field) {
1239 field = (f->fmt.pix.height > maxh/2)
1240 ? V4L2_FIELD_INTERLACED
1241 : V4L2_FIELD_BOTTOM;
1245 case V4L2_FIELD_TOP:
1246 case V4L2_FIELD_BOTTOM:
1249 case V4L2_FIELD_INTERLACED:
1255 f->fmt.pix.field = field;
1256 if (f->fmt.pix.height < 32)
1257 f->fmt.pix.height = 32;
1258 if (f->fmt.pix.height > maxh)
1259 f->fmt.pix.height = maxh;
1260 if (f->fmt.pix.width < 48)
1261 f->fmt.pix.width = 48;
1262 if (f->fmt.pix.width > maxw)
1263 f->fmt.pix.width = maxw;
1264 f->fmt.pix.width &= ~0x03;
1265 f->fmt.pix.bytesperline =
1266 (f->fmt.pix.width * fmt->depth) >> 3;
1267 f->fmt.pix.sizeimage =
1268 f->fmt.pix.height * f->fmt.pix.bytesperline;
1272 case V4L2_BUF_TYPE_VBI_CAPTURE:
1273 cx8800_vbi_fmt(dev, f);
1280 static int cx8800_s_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1281 struct v4l2_format *f)
1286 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1287 err = cx8800_try_fmt(dev,fh,f);
1291 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1292 fh->width = f->fmt.pix.width;
1293 fh->height = f->fmt.pix.height;
1294 fh->vidq.field = f->fmt.pix.field;
1296 case V4L2_BUF_TYPE_VBI_CAPTURE:
1297 cx8800_vbi_fmt(dev, f);
1305 * This function is _not_ called directly, but from
1306 * video_generic_ioctl (and maybe others). userspace
1307 * copying is done already, arg is a kernel pointer.
1309 static int video_do_ioctl(struct inode *inode, struct file *file,
1310 unsigned int cmd, void *arg)
1312 struct cx8800_fh *fh = file->private_data;
1313 struct cx8800_dev *dev = fh->dev;
1314 struct cx88_core *core = dev->core;
1316 unsigned long flags;
1320 if (video_debug > 1)
1321 cx88_print_ioctl(core->name,cmd);
1323 case VIDIOC_QUERYCAP:
1325 struct v4l2_capability *cap = arg;
1327 memset(cap,0,sizeof(*cap));
1328 strcpy(cap->driver, "cx8800");
1329 strlcpy(cap->card, cx88_boards[core->board].name,
1331 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1332 cap->version = CX88_VERSION_CODE;
1334 V4L2_CAP_VIDEO_CAPTURE |
1335 V4L2_CAP_READWRITE |
1336 V4L2_CAP_STREAMING |
1337 V4L2_CAP_VBI_CAPTURE |
1339 V4L2_CAP_VIDEO_OVERLAY |
1342 if (UNSET != core->tuner_type)
1343 cap->capabilities |= V4L2_CAP_TUNER;
1347 /* ---------- tv norms ---------- */
1348 case VIDIOC_ENUMSTD:
1350 struct v4l2_standard *e = arg;
1354 if (i >= ARRAY_SIZE(tvnorms))
1356 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1357 tvnorms[e->index].name);
1365 v4l2_std_id *id = arg;
1367 *id = core->tvnorm->id;
1372 v4l2_std_id *id = arg;
1375 for(i = 0; i < ARRAY_SIZE(tvnorms); i++)
1376 if (*id & tvnorms[i].id)
1378 if (i == ARRAY_SIZE(tvnorms))
1382 cx88_set_tvnorm(dev->core,&tvnorms[i]);
1387 /* ------ input switching ---------- */
1388 case VIDIOC_ENUMINPUT:
1390 static const char *iname[] = {
1391 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1392 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1393 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1394 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1395 [ CX88_VMUX_SVIDEO ] = "S-Video",
1396 [ CX88_VMUX_TELEVISION ] = "Television",
1397 [ CX88_VMUX_CABLE ] = "Cable TV",
1398 [ CX88_VMUX_DVB ] = "DVB",
1399 [ CX88_VMUX_DEBUG ] = "for debug only",
1401 struct v4l2_input *i = arg;
1407 if (0 == INPUT(n)->type)
1409 memset(i,0,sizeof(*i));
1411 i->type = V4L2_INPUT_TYPE_CAMERA;
1412 strcpy(i->name,iname[INPUT(n)->type]);
1413 if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
1414 (CX88_VMUX_CABLE == INPUT(n)->type))
1415 i->type = V4L2_INPUT_TYPE_TUNER;
1416 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1417 i->std |= tvnorms[n].id;
1420 case VIDIOC_G_INPUT:
1422 unsigned int *i = arg;
1424 *i = dev->core->input;
1427 case VIDIOC_S_INPUT:
1429 unsigned int *i = arg;
1434 cx88_newstation(core);
1443 case VIDIOC_G_AUDIO:
1445 struct v4l2_audio *a = arg;
1446 unsigned int n = a->index;
1448 memset(a,0,sizeof(*a));
1452 if ((CX88_VMUX_TELEVISION == INPUT(n)->type)
1453 || (CX88_VMUX_CABLE == INPUT(n)->type)) {
1454 strcpy(a->name,"Television");
1455 // FIXME figure out if stereo received and set V4L2_AUDCAP_STEREO.
1460 if (CX88_BOARD_DVICO_FUSIONHDTV_3_GOLD == core->board) {
1461 strcpy(a->name,"Line In");
1462 a->capability = V4L2_AUDCAP_STEREO;
1467 // Audio input not available.
1472 /* --- capture ioctls ---------------------------------------- */
1473 case VIDIOC_ENUM_FMT:
1475 struct v4l2_fmtdesc *f = arg;
1476 enum v4l2_buf_type type;
1482 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1483 if (index >= ARRAY_SIZE(formats))
1485 memset(f,0,sizeof(*f));
1488 strlcpy(f->description,formats[index].name,sizeof(f->description));
1489 f->pixelformat = formats[index].fourcc;
1498 struct v4l2_format *f = arg;
1499 return cx8800_g_fmt(dev,fh,f);
1503 struct v4l2_format *f = arg;
1504 return cx8800_s_fmt(dev,fh,f);
1506 case VIDIOC_TRY_FMT:
1508 struct v4l2_format *f = arg;
1509 return cx8800_try_fmt(dev,fh,f);
1512 /* --- controls ---------------------------------------------- */
1513 case VIDIOC_QUERYCTRL:
1515 struct v4l2_queryctrl *c = arg;
1518 if (c->id < V4L2_CID_BASE ||
1519 c->id >= V4L2_CID_LASTP1)
1521 for (i = 0; i < CX8800_CTLS; i++)
1522 if (cx8800_ctls[i].v.id == c->id)
1524 if (i == CX8800_CTLS) {
1528 *c = cx8800_ctls[i].v;
1532 return get_control(dev,arg);
1534 return set_control(dev,arg);
1536 /* --- tuner ioctls ------------------------------------------ */
1537 case VIDIOC_G_TUNER:
1539 struct v4l2_tuner *t = arg;
1542 if (UNSET == core->tuner_type)
1547 memset(t,0,sizeof(*t));
1548 strcpy(t->name, "Television");
1549 t->type = V4L2_TUNER_ANALOG_TV;
1550 t->capability = V4L2_TUNER_CAP_NORM;
1551 t->rangehigh = 0xffffffffUL;
1553 cx88_get_stereo(core ,t);
1554 reg = cx_read(MO_DEVICE_STATUS);
1555 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1558 case VIDIOC_S_TUNER:
1560 struct v4l2_tuner *t = arg;
1562 if (UNSET == core->tuner_type)
1566 cx88_set_stereo(core, t->audmode, 1);
1569 case VIDIOC_G_FREQUENCY:
1571 struct v4l2_frequency *f = arg;
1573 if (UNSET == core->tuner_type)
1577 memset(f,0,sizeof(*f));
1578 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1579 f->frequency = dev->freq;
1582 case VIDIOC_S_FREQUENCY:
1584 struct v4l2_frequency *f = arg;
1586 if (UNSET == core->tuner_type)
1590 if (0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV)
1592 if (1 == fh->radio && f->type != V4L2_TUNER_RADIO)
1595 dev->freq = f->frequency;
1596 cx88_newstation(core);
1597 #ifdef V4L2_I2C_CLIENTS
1598 cx88_call_i2c_clients(dev->core,VIDIOC_S_FREQUENCY,f);
1600 cx88_call_i2c_clients(dev->core,VIDIOCSFREQ,&dev->freq);
1606 /* --- streaming capture ------------------------------------- */
1609 struct video_mbuf *mbuf = arg;
1610 struct videobuf_queue *q;
1611 struct v4l2_requestbuffers req;
1615 memset(&req,0,sizeof(req));
1618 req.memory = V4L2_MEMORY_MMAP;
1619 err = videobuf_reqbufs(q,&req);
1622 memset(mbuf,0,sizeof(*mbuf));
1623 mbuf->frames = req.count;
1625 for (i = 0; i < mbuf->frames; i++) {
1626 mbuf->offsets[i] = q->bufs[i]->boff;
1627 mbuf->size += q->bufs[i]->bsize;
1631 case VIDIOC_REQBUFS:
1632 return videobuf_reqbufs(get_queue(fh), arg);
1634 case VIDIOC_QUERYBUF:
1635 return videobuf_querybuf(get_queue(fh), arg);
1638 return videobuf_qbuf(get_queue(fh), arg);
1641 return videobuf_dqbuf(get_queue(fh), arg,
1642 file->f_flags & O_NONBLOCK);
1644 case VIDIOC_STREAMON:
1646 int res = get_ressource(fh);
1648 if (!res_get(dev,fh,res))
1650 return videobuf_streamon(get_queue(fh));
1652 case VIDIOC_STREAMOFF:
1654 int res = get_ressource(fh);
1656 err = videobuf_streamoff(get_queue(fh));
1659 res_free(dev,fh,res);
1664 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1670 static int video_ioctl(struct inode *inode, struct file *file,
1671 unsigned int cmd, unsigned long arg)
1673 return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
1676 /* ----------------------------------------------------------- */
1678 static int radio_do_ioctl(struct inode *inode, struct file *file,
1679 unsigned int cmd, void *arg)
1681 struct cx8800_fh *fh = file->private_data;
1682 struct cx8800_dev *dev = fh->dev;
1683 struct cx88_core *core = dev->core;
1685 if (video_debug > 1)
1686 cx88_print_ioctl(core->name,cmd);
1689 case VIDIOC_QUERYCAP:
1691 struct v4l2_capability *cap = arg;
1693 memset(cap,0,sizeof(*cap));
1694 strcpy(cap->driver, "cx8800");
1695 strlcpy(cap->card, cx88_boards[core->board].name,
1697 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1698 cap->version = CX88_VERSION_CODE;
1699 cap->capabilities = V4L2_CAP_TUNER;
1702 case VIDIOC_G_TUNER:
1704 struct v4l2_tuner *t = arg;
1709 memset(t,0,sizeof(*t));
1710 strcpy(t->name, "Radio");
1711 t->rangelow = (int)(65*16);
1712 t->rangehigh = (int)(108*16);
1714 #ifdef V4L2_I2C_CLIENTS
1715 cx88_call_i2c_clients(dev->core,VIDIOC_G_TUNER,t);
1718 struct video_tuner vt;
1719 memset(&vt,0,sizeof(vt));
1720 cx88_call_i2c_clients(dev,VIDIOCGTUNER,&vt);
1721 t->signal = vt.signal;
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;
1756 case VIDIOC_S_AUDIO:
1757 case VIDIOC_S_TUNER:
1758 case VIDIOC_S_INPUT:
1762 case VIDIOC_QUERYCTRL:
1764 struct v4l2_queryctrl *c = arg;
1767 if (c->id < V4L2_CID_BASE ||
1768 c->id >= V4L2_CID_LASTP1)
1770 if (c->id == V4L2_CID_AUDIO_MUTE) {
1771 for (i = 0; i < CX8800_CTLS; i++)
1772 if (cx8800_ctls[i].v.id == c->id)
1774 *c = cx8800_ctls[i].v;
1783 case VIDIOC_G_FREQUENCY:
1784 case VIDIOC_S_FREQUENCY:
1785 return video_do_ioctl(inode,file,cmd,arg);
1788 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1794 static int radio_ioctl(struct inode *inode, struct file *file,
1795 unsigned int cmd, unsigned long arg)
1797 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
1800 /* ----------------------------------------------------------- */
1802 static void cx8800_vid_timeout(unsigned long data)
1804 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1805 struct cx88_core *core = dev->core;
1806 struct cx88_dmaqueue *q = &dev->vidq;
1807 struct cx88_buffer *buf;
1808 unsigned long flags;
1810 cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH21]);
1812 cx_clear(MO_VID_DMACNTRL, 0x11);
1813 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1815 spin_lock_irqsave(&dev->slock,flags);
1816 while (!list_empty(&q->active)) {
1817 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1818 list_del(&buf->vb.queue);
1819 buf->vb.state = STATE_ERROR;
1820 wake_up(&buf->vb.done);
1821 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1822 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1824 restart_video_queue(dev,q);
1825 spin_unlock_irqrestore(&dev->slock,flags);
1828 static void cx8800_vid_irq(struct cx8800_dev *dev)
1830 struct cx88_core *core = dev->core;
1831 u32 status, mask, count;
1833 status = cx_read(MO_VID_INTSTAT);
1834 mask = cx_read(MO_VID_INTMSK);
1835 if (0 == (status & mask))
1837 cx_write(MO_VID_INTSTAT, status);
1838 if (irq_debug || (status & mask & ~0xff))
1839 cx88_print_irqbits(core->name, "irq vid",
1840 cx88_vid_irqs, status, mask);
1842 /* risc op code error */
1843 if (status & (1 << 16)) {
1844 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1845 cx_clear(MO_VID_DMACNTRL, 0x11);
1846 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1847 cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH21]);
1851 if (status & 0x01) {
1852 spin_lock(&dev->slock);
1853 count = cx_read(MO_VIDY_GPCNT);
1854 cx88_wakeup(dev->core, &dev->vidq, count);
1855 spin_unlock(&dev->slock);
1859 if (status & 0x08) {
1860 spin_lock(&dev->slock);
1861 count = cx_read(MO_VBI_GPCNT);
1862 cx88_wakeup(dev->core, &dev->vbiq, count);
1863 spin_unlock(&dev->slock);
1867 if (status & 0x10) {
1868 dprintk(2,"stopper video\n");
1869 spin_lock(&dev->slock);
1870 restart_video_queue(dev,&dev->vidq);
1871 spin_unlock(&dev->slock);
1875 if (status & 0x80) {
1876 dprintk(2,"stopper vbi\n");
1877 spin_lock(&dev->slock);
1878 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1879 spin_unlock(&dev->slock);
1883 static irqreturn_t cx8800_irq(int irq, void *dev_id, struct pt_regs *regs)
1885 struct cx8800_dev *dev = dev_id;
1886 struct cx88_core *core = dev->core;
1888 int loop, handled = 0;
1890 for (loop = 0; loop < 10; loop++) {
1891 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
1894 cx_write(MO_PCI_INTSTAT, status);
1897 if (status & core->pci_irqmask)
1898 cx88_core_irq(core,status);
1900 cx8800_vid_irq(dev);
1903 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1905 cx_write(MO_PCI_INTMSK,0);
1909 return IRQ_RETVAL(handled);
1912 /* ----------------------------------------------------------- */
1913 /* exported stuff */
1915 static struct file_operations video_fops =
1917 .owner = THIS_MODULE,
1919 .release = video_release,
1923 .ioctl = video_ioctl,
1924 .llseek = no_llseek,
1927 struct video_device cx8800_video_template =
1929 .name = "cx8800-video",
1930 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1932 .fops = &video_fops,
1936 struct video_device cx8800_vbi_template =
1938 .name = "cx8800-vbi",
1939 .type = VID_TYPE_TELETEXT|VID_TYPE_TUNER,
1941 .fops = &video_fops,
1945 static struct file_operations radio_fops =
1947 .owner = THIS_MODULE,
1949 .release = video_release,
1950 .ioctl = radio_ioctl,
1951 .llseek = no_llseek,
1954 struct video_device cx8800_radio_template =
1956 .name = "cx8800-radio",
1957 .type = VID_TYPE_TUNER,
1959 .fops = &radio_fops,
1963 /* ----------------------------------------------------------- */
1965 static void cx8800_unregister_video(struct cx8800_dev *dev)
1967 if (dev->radio_dev) {
1968 if (-1 != dev->radio_dev->minor)
1969 video_unregister_device(dev->radio_dev);
1971 video_device_release(dev->radio_dev);
1972 dev->radio_dev = NULL;
1975 if (-1 != dev->vbi_dev->minor)
1976 video_unregister_device(dev->vbi_dev);
1978 video_device_release(dev->vbi_dev);
1979 dev->vbi_dev = NULL;
1981 if (dev->video_dev) {
1982 if (-1 != dev->video_dev->minor)
1983 video_unregister_device(dev->video_dev);
1985 video_device_release(dev->video_dev);
1986 dev->video_dev = NULL;
1990 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1991 const struct pci_device_id *pci_id)
1993 struct cx8800_dev *dev;
1994 struct cx88_core *core;
1997 dev = kmalloc(sizeof(*dev),GFP_KERNEL);
2000 memset(dev,0,sizeof(*dev));
2004 if (pci_enable_device(pci_dev)) {
2008 core = cx88_core_get(dev->pci);
2015 /* print pci info */
2016 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
2017 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
2018 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
2019 "latency: %d, mmio: 0x%lx\n", core->name,
2020 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
2021 dev->pci_lat,pci_resource_start(pci_dev,0));
2023 pci_set_master(pci_dev);
2024 if (!pci_dma_supported(pci_dev,0xffffffff)) {
2025 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
2030 /* initialize driver struct */
2031 init_MUTEX(&dev->lock);
2032 spin_lock_init(&dev->slock);
2033 core->tvnorm = tvnorms;
2035 /* init video dma queues */
2036 INIT_LIST_HEAD(&dev->vidq.active);
2037 INIT_LIST_HEAD(&dev->vidq.queued);
2038 dev->vidq.timeout.function = cx8800_vid_timeout;
2039 dev->vidq.timeout.data = (unsigned long)dev;
2040 init_timer(&dev->vidq.timeout);
2041 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
2042 MO_VID_DMACNTRL,0x11,0x00);
2044 /* init vbi dma queues */
2045 INIT_LIST_HEAD(&dev->vbiq.active);
2046 INIT_LIST_HEAD(&dev->vbiq.queued);
2047 dev->vbiq.timeout.function = cx8800_vbi_timeout;
2048 dev->vbiq.timeout.data = (unsigned long)dev;
2049 init_timer(&dev->vbiq.timeout);
2050 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
2051 MO_VID_DMACNTRL,0x88,0x00);
2054 err = request_irq(pci_dev->irq, cx8800_irq,
2055 SA_SHIRQ | SA_INTERRUPT, core->name, dev);
2057 printk(KERN_ERR "%s: can't get IRQ %d\n",
2058 core->name,pci_dev->irq);
2061 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
2063 /* load and configure helper modules */
2064 if (TUNER_ABSENT != core->tuner_type)
2065 request_module("tuner");
2066 if (core->tda9887_conf)
2067 request_module("tda9887");
2068 if (core->tuner_type != UNSET)
2069 cx88_call_i2c_clients(dev->core,TUNER_SET_TYPE,&core->tuner_type);
2070 if (core->tda9887_conf)
2071 cx88_call_i2c_clients(dev->core,TDA9887_SET_CONFIG,&core->tda9887_conf);
2073 /* register v4l devices */
2074 dev->video_dev = cx88_vdev_init(core,dev->pci,
2075 &cx8800_video_template,"video");
2076 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
2077 video_nr[core->nr]);
2079 printk(KERN_INFO "%s: can't register video device\n",
2083 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
2084 core->name,dev->video_dev->minor & 0x1f);
2086 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
2087 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
2090 printk(KERN_INFO "%s/0: can't register vbi device\n",
2094 printk(KERN_INFO "%s/0: registered device vbi%d\n",
2095 core->name,dev->vbi_dev->minor & 0x1f);
2097 if (core->has_radio) {
2098 dev->radio_dev = cx88_vdev_init(core,dev->pci,
2099 &cx8800_radio_template,"radio");
2100 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
2101 radio_nr[core->nr]);
2103 printk(KERN_INFO "%s/0: can't register radio device\n",
2107 printk(KERN_INFO "%s/0: registered device radio%d\n",
2108 core->name,dev->radio_dev->minor & 0x1f);
2111 /* everything worked */
2112 list_add_tail(&dev->devlist,&cx8800_devlist);
2113 pci_set_drvdata(pci_dev,dev);
2115 /* initial device configuration */
2118 cx88_set_tvnorm(dev->core,tvnorms);
2122 /* start tvaudio thread */
2123 if (core->tuner_type != TUNER_ABSENT)
2124 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
2128 cx8800_unregister_video(dev);
2129 free_irq(pci_dev->irq, dev);
2131 cx88_core_put(core,dev->pci);
2137 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
2139 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2142 if (dev->core->kthread) {
2143 kthread_stop(dev->core->kthread);
2144 dev->core->kthread = NULL;
2147 cx88_shutdown(dev->core); /* FIXME */
2148 pci_disable_device(pci_dev);
2150 /* unregister stuff */
2152 free_irq(pci_dev->irq, dev);
2153 cx8800_unregister_video(dev);
2154 pci_set_drvdata(pci_dev, NULL);
2157 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
2158 list_del(&dev->devlist);
2159 cx88_core_put(dev->core,dev->pci);
2163 static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
2165 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2166 struct cx88_core *core = dev->core;
2168 /* stop video+vbi capture */
2169 spin_lock(&dev->slock);
2170 if (!list_empty(&dev->vidq.active)) {
2171 printk("%s: suspend video\n", core->name);
2172 stop_video_dma(dev);
2173 del_timer(&dev->vidq.timeout);
2175 if (!list_empty(&dev->vbiq.active)) {
2176 printk("%s: suspend vbi\n", core->name);
2177 cx8800_stop_vbi_dma(dev);
2178 del_timer(&dev->vbiq.timeout);
2180 spin_unlock(&dev->slock);
2183 /* FIXME -- shutdown device */
2184 cx88_shutdown(dev->core);
2187 pci_save_state(pci_dev);
2188 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
2189 pci_disable_device(pci_dev);
2190 dev->state.disabled = 1;
2195 static int cx8800_resume(struct pci_dev *pci_dev)
2197 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2198 struct cx88_core *core = dev->core;
2200 if (dev->state.disabled) {
2201 pci_enable_device(pci_dev);
2202 dev->state.disabled = 0;
2204 pci_set_power_state(pci_dev, PCI_D0);
2205 pci_restore_state(pci_dev);
2208 /* FIXME: re-initialize hardware */
2209 cx88_reset(dev->core);
2212 /* restart video+vbi capture */
2213 spin_lock(&dev->slock);
2214 if (!list_empty(&dev->vidq.active)) {
2215 printk("%s: resume video\n", core->name);
2216 restart_video_queue(dev,&dev->vidq);
2218 if (!list_empty(&dev->vbiq.active)) {
2219 printk("%s: resume vbi\n", core->name);
2220 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2222 spin_unlock(&dev->slock);
2227 /* ----------------------------------------------------------- */
2229 struct pci_device_id cx8800_pci_tbl[] = {
2233 .subvendor = PCI_ANY_ID,
2234 .subdevice = PCI_ANY_ID,
2236 /* --- end of list --- */
2239 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2241 static struct pci_driver cx8800_pci_driver = {
2243 .id_table = cx8800_pci_tbl,
2244 .probe = cx8800_initdev,
2245 .remove = __devexit_p(cx8800_finidev),
2247 .suspend = cx8800_suspend,
2248 .resume = cx8800_resume,
2251 static int cx8800_init(void)
2253 printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
2254 (CX88_VERSION_CODE >> 16) & 0xff,
2255 (CX88_VERSION_CODE >> 8) & 0xff,
2256 CX88_VERSION_CODE & 0xff);
2258 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2259 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2261 return pci_register_driver(&cx8800_pci_driver);
2264 static void cx8800_fini(void)
2266 pci_unregister_driver(&cx8800_pci_driver);
2269 module_init(cx8800_init);
2270 module_exit(cx8800_fini);
2272 /* ----------------------------------------------------------- */