[PATCH] v4l: bug fixes for tuner, cx88 and tea5767
[linux-2.6/linux-loongson.git] / drivers / media / video / cx88 / cx88-video.c
blob5588a3aeecb4f605953f442526714cc116e3d30d
1 /*
2 * $Id: cx88-video.c,v 1.80 2005/07/13 08:49:08 mchehab Exp $
4 * device driver for Conexant 2388x based TV cards
5 * video4linux video interface
7 * (c) 2003-04 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/kmod.h>
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/delay.h>
33 #include <linux/kthread.h>
34 #include <asm/div64.h>
36 #include "cx88.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 /* ------------------------------------------------------------------- */
76 /* static data */
78 static struct cx88_tvnorm tvnorms[] = {
80 .name = "NTSC-M",
81 .id = V4L2_STD_NTSC_M,
82 .cxiformat = VideoFormatNTSC,
83 .cxoformat = 0x181f0008,
84 },{
85 .name = "NTSC-JP",
86 .id = V4L2_STD_NTSC_M_JP,
87 .cxiformat = VideoFormatNTSCJapan,
88 .cxoformat = 0x181f0008,
89 },{
90 .name = "PAL-BG",
91 .id = V4L2_STD_PAL_BG,
92 .cxiformat = VideoFormatPAL,
93 .cxoformat = 0x181f0008,
94 },{
95 .name = "PAL-DK",
96 .id = V4L2_STD_PAL_DK,
97 .cxiformat = VideoFormatPAL,
98 .cxoformat = 0x181f0008,
99 },{
100 .name = "PAL-I",
101 .id = V4L2_STD_PAL_I,
102 .cxiformat = VideoFormatPAL,
103 .cxoformat = 0x181f0008,
105 .name = "PAL-M",
106 .id = V4L2_STD_PAL_M,
107 .cxiformat = VideoFormatPALM,
108 .cxoformat = 0x1c1f0008,
110 .name = "PAL-N",
111 .id = V4L2_STD_PAL_N,
112 .cxiformat = VideoFormatPALN,
113 .cxoformat = 0x1c1f0008,
115 .name = "PAL-Nc",
116 .id = V4L2_STD_PAL_Nc,
117 .cxiformat = VideoFormatPALNC,
118 .cxoformat = 0x1c1f0008,
120 .name = "PAL-60",
121 .id = V4L2_STD_PAL_60,
122 .cxiformat = VideoFormatPAL60,
123 .cxoformat = 0x181f0008,
125 .name = "SECAM-L",
126 .id = V4L2_STD_SECAM_L,
127 .cxiformat = VideoFormatSECAM,
128 .cxoformat = 0x181f0008,
130 .name = "SECAM-DK",
131 .id = V4L2_STD_SECAM_DK,
132 .cxiformat = VideoFormatSECAM,
133 .cxoformat = 0x181f0008,
137 static struct cx8800_fmt formats[] = {
139 .name = "8 bpp, gray",
140 .fourcc = V4L2_PIX_FMT_GREY,
141 .cxformat = ColorFormatY8,
142 .depth = 8,
143 .flags = FORMAT_FLAGS_PACKED,
145 .name = "15 bpp RGB, le",
146 .fourcc = V4L2_PIX_FMT_RGB555,
147 .cxformat = ColorFormatRGB15,
148 .depth = 16,
149 .flags = FORMAT_FLAGS_PACKED,
151 .name = "15 bpp RGB, be",
152 .fourcc = V4L2_PIX_FMT_RGB555X,
153 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
154 .depth = 16,
155 .flags = FORMAT_FLAGS_PACKED,
157 .name = "16 bpp RGB, le",
158 .fourcc = V4L2_PIX_FMT_RGB565,
159 .cxformat = ColorFormatRGB16,
160 .depth = 16,
161 .flags = FORMAT_FLAGS_PACKED,
163 .name = "16 bpp RGB, be",
164 .fourcc = V4L2_PIX_FMT_RGB565X,
165 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
166 .depth = 16,
167 .flags = FORMAT_FLAGS_PACKED,
169 .name = "24 bpp RGB, le",
170 .fourcc = V4L2_PIX_FMT_BGR24,
171 .cxformat = ColorFormatRGB24,
172 .depth = 24,
173 .flags = FORMAT_FLAGS_PACKED,
175 .name = "32 bpp RGB, le",
176 .fourcc = V4L2_PIX_FMT_BGR32,
177 .cxformat = ColorFormatRGB32,
178 .depth = 32,
179 .flags = FORMAT_FLAGS_PACKED,
181 .name = "32 bpp RGB, be",
182 .fourcc = V4L2_PIX_FMT_RGB32,
183 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
184 .depth = 32,
185 .flags = FORMAT_FLAGS_PACKED,
187 .name = "4:2:2, packed, YUYV",
188 .fourcc = V4L2_PIX_FMT_YUYV,
189 .cxformat = ColorFormatYUY2,
190 .depth = 16,
191 .flags = FORMAT_FLAGS_PACKED,
193 .name = "4:2:2, packed, UYVY",
194 .fourcc = V4L2_PIX_FMT_UYVY,
195 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
196 .depth = 16,
197 .flags = FORMAT_FLAGS_PACKED,
201 static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
203 unsigned int i;
205 for (i = 0; i < ARRAY_SIZE(formats); i++)
206 if (formats[i].fourcc == fourcc)
207 return formats+i;
208 return NULL;
211 /* ------------------------------------------------------------------- */
213 static const struct v4l2_queryctrl no_ctl = {
214 .name = "42",
215 .flags = V4L2_CTRL_FLAG_DISABLED,
218 static struct cx88_ctrl cx8800_ctls[] = {
219 /* --- video --- */
221 .v = {
222 .id = V4L2_CID_BRIGHTNESS,
223 .name = "Brightness",
224 .minimum = 0x00,
225 .maximum = 0xff,
226 .step = 1,
227 .default_value = 0,
228 .type = V4L2_CTRL_TYPE_INTEGER,
230 .off = 128,
231 .reg = MO_CONTR_BRIGHT,
232 .mask = 0x00ff,
233 .shift = 0,
235 .v = {
236 .id = V4L2_CID_CONTRAST,
237 .name = "Contrast",
238 .minimum = 0,
239 .maximum = 0xff,
240 .step = 1,
241 .default_value = 0,
242 .type = V4L2_CTRL_TYPE_INTEGER,
244 .off = 0,
245 .reg = MO_CONTR_BRIGHT,
246 .mask = 0xff00,
247 .shift = 8,
249 .v = {
250 .id = V4L2_CID_HUE,
251 .name = "Hue",
252 .minimum = 0,
253 .maximum = 0xff,
254 .step = 1,
255 .default_value = 0,
256 .type = V4L2_CTRL_TYPE_INTEGER,
258 .off = 128,
259 .reg = MO_HUE,
260 .mask = 0x00ff,
261 .shift = 0,
263 /* strictly, this only describes only U saturation.
264 * V saturation is handled specially through code.
266 .v = {
267 .id = V4L2_CID_SATURATION,
268 .name = "Saturation",
269 .minimum = 0,
270 .maximum = 0xff,
271 .step = 1,
272 .default_value = 0,
273 .type = V4L2_CTRL_TYPE_INTEGER,
275 .off = 0,
276 .reg = MO_UV_SATURATION,
277 .mask = 0x00ff,
278 .shift = 0,
280 /* --- audio --- */
281 .v = {
282 .id = V4L2_CID_AUDIO_MUTE,
283 .name = "Mute",
284 .minimum = 0,
285 .maximum = 1,
286 .type = V4L2_CTRL_TYPE_BOOLEAN,
288 .reg = AUD_VOL_CTL,
289 .sreg = SHADOW_AUD_VOL_CTL,
290 .mask = (1 << 6),
291 .shift = 6,
293 .v = {
294 .id = V4L2_CID_AUDIO_VOLUME,
295 .name = "Volume",
296 .minimum = 0,
297 .maximum = 0x3f,
298 .step = 1,
299 .default_value = 0,
300 .type = V4L2_CTRL_TYPE_INTEGER,
302 .reg = AUD_VOL_CTL,
303 .sreg = SHADOW_AUD_VOL_CTL,
304 .mask = 0x3f,
305 .shift = 0,
307 .v = {
308 .id = V4L2_CID_AUDIO_BALANCE,
309 .name = "Balance",
310 .minimum = 0,
311 .maximum = 0x7f,
312 .step = 1,
313 .default_value = 0x40,
314 .type = V4L2_CTRL_TYPE_INTEGER,
316 .reg = AUD_BAL_CTL,
317 .sreg = SHADOW_AUD_BAL_CTL,
318 .mask = 0x7f,
319 .shift = 0,
322 static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
324 /* ------------------------------------------------------------------- */
325 /* resource management */
327 static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
329 if (fh->resources & bit)
330 /* have it already allocated */
331 return 1;
333 /* is it free? */
334 down(&dev->lock);
335 if (dev->resources & bit) {
336 /* no, someone else uses it */
337 up(&dev->lock);
338 return 0;
340 /* it's free, grab it */
341 fh->resources |= bit;
342 dev->resources |= bit;
343 dprintk(1,"res: get %d\n",bit);
344 up(&dev->lock);
345 return 1;
348 static
349 int res_check(struct cx8800_fh *fh, unsigned int bit)
351 return (fh->resources & bit);
354 static
355 int res_locked(struct cx8800_dev *dev, unsigned int bit)
357 return (dev->resources & bit);
360 static
361 void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
363 if ((fh->resources & bits) != bits)
364 BUG();
366 down(&dev->lock);
367 fh->resources &= ~bits;
368 dev->resources &= ~bits;
369 dprintk(1,"res: put %d\n",bits);
370 up(&dev->lock);
373 /* ------------------------------------------------------------------ */
375 static int video_mux(struct cx8800_dev *dev, unsigned int input)
377 struct cx88_core *core = dev->core;
379 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
380 input, INPUT(input)->vmux,
381 INPUT(input)->gpio0,INPUT(input)->gpio1,
382 INPUT(input)->gpio2,INPUT(input)->gpio3);
383 dev->core->input = input;
384 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input)->vmux << 14);
385 cx_write(MO_GP3_IO, INPUT(input)->gpio3);
386 cx_write(MO_GP0_IO, INPUT(input)->gpio0);
387 cx_write(MO_GP1_IO, INPUT(input)->gpio1);
388 cx_write(MO_GP2_IO, INPUT(input)->gpio2);
390 switch (INPUT(input)->type) {
391 case CX88_VMUX_SVIDEO:
392 cx_set(MO_AFECFG_IO, 0x00000001);
393 cx_set(MO_INPUT_FORMAT, 0x00010010);
394 cx_set(MO_FILTER_EVEN, 0x00002020);
395 cx_set(MO_FILTER_ODD, 0x00002020);
396 break;
397 default:
398 cx_clear(MO_AFECFG_IO, 0x00000001);
399 cx_clear(MO_INPUT_FORMAT, 0x00010010);
400 cx_clear(MO_FILTER_EVEN, 0x00002020);
401 cx_clear(MO_FILTER_ODD, 0x00002020);
402 break;
404 return 0;
407 /* ------------------------------------------------------------------ */
409 static int start_video_dma(struct cx8800_dev *dev,
410 struct cx88_dmaqueue *q,
411 struct cx88_buffer *buf)
413 struct cx88_core *core = dev->core;
415 /* setup fifo + format */
416 cx88_sram_channel_setup(dev->core, &cx88_sram_channels[SRAM_CH21],
417 buf->bpl, buf->risc.dma);
418 cx88_set_scale(dev->core, buf->vb.width, buf->vb.height, buf->vb.field);
419 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
421 /* reset counter */
422 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
423 q->count = 1;
425 /* enable irqs */
426 cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
427 cx_set(MO_VID_INTMSK, 0x0f0011);
429 /* enable capture */
430 cx_set(VID_CAPTURE_CONTROL,0x06);
432 /* start dma */
433 cx_set(MO_DEV_CNTRL2, (1<<5));
434 cx_set(MO_VID_DMACNTRL, 0x11);
436 return 0;
439 static int stop_video_dma(struct cx8800_dev *dev)
441 struct cx88_core *core = dev->core;
443 /* stop dma */
444 cx_clear(MO_VID_DMACNTRL, 0x11);
446 /* disable capture */
447 cx_clear(VID_CAPTURE_CONTROL,0x06);
449 /* disable irqs */
450 cx_clear(MO_PCI_INTMSK, 0x000001);
451 cx_clear(MO_VID_INTMSK, 0x0f0011);
452 return 0;
455 static int restart_video_queue(struct cx8800_dev *dev,
456 struct cx88_dmaqueue *q)
458 struct cx88_buffer *buf, *prev;
459 struct list_head *item;
461 if (!list_empty(&q->active)) {
462 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
463 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
464 buf, buf->vb.i);
465 start_video_dma(dev, q, buf);
466 list_for_each(item,&q->active) {
467 buf = list_entry(item, struct cx88_buffer, vb.queue);
468 buf->count = q->count++;
470 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
471 return 0;
474 prev = NULL;
475 for (;;) {
476 if (list_empty(&q->queued))
477 return 0;
478 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
479 if (NULL == prev) {
480 list_del(&buf->vb.queue);
481 list_add_tail(&buf->vb.queue,&q->active);
482 start_video_dma(dev, q, buf);
483 buf->vb.state = STATE_ACTIVE;
484 buf->count = q->count++;
485 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
486 dprintk(2,"[%p/%d] restart_queue - first active\n",
487 buf,buf->vb.i);
489 } else if (prev->vb.width == buf->vb.width &&
490 prev->vb.height == buf->vb.height &&
491 prev->fmt == buf->fmt) {
492 list_del(&buf->vb.queue);
493 list_add_tail(&buf->vb.queue,&q->active);
494 buf->vb.state = STATE_ACTIVE;
495 buf->count = q->count++;
496 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
497 dprintk(2,"[%p/%d] restart_queue - move to active\n",
498 buf,buf->vb.i);
499 } else {
500 return 0;
502 prev = buf;
506 /* ------------------------------------------------------------------ */
508 static int
509 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
511 struct cx8800_fh *fh = q->priv_data;
513 *size = fh->fmt->depth*fh->width*fh->height >> 3;
514 if (0 == *count)
515 *count = 32;
516 while (*size * *count > vid_limit * 1024 * 1024)
517 (*count)--;
518 return 0;
521 static int
522 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
523 enum v4l2_field field)
525 struct cx8800_fh *fh = q->priv_data;
526 struct cx8800_dev *dev = fh->dev;
527 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
528 int rc, init_buffer = 0;
530 BUG_ON(NULL == fh->fmt);
531 if (fh->width < 48 || fh->width > norm_maxw(dev->core->tvnorm) ||
532 fh->height < 32 || fh->height > norm_maxh(dev->core->tvnorm))
533 return -EINVAL;
534 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
535 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
536 return -EINVAL;
538 if (buf->fmt != fh->fmt ||
539 buf->vb.width != fh->width ||
540 buf->vb.height != fh->height ||
541 buf->vb.field != field) {
542 buf->fmt = fh->fmt;
543 buf->vb.width = fh->width;
544 buf->vb.height = fh->height;
545 buf->vb.field = field;
546 init_buffer = 1;
549 if (STATE_NEEDS_INIT == buf->vb.state) {
550 init_buffer = 1;
551 if (0 != (rc = videobuf_iolock(dev->pci,&buf->vb,NULL)))
552 goto fail;
555 if (init_buffer) {
556 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
557 switch (buf->vb.field) {
558 case V4L2_FIELD_TOP:
559 cx88_risc_buffer(dev->pci, &buf->risc,
560 buf->vb.dma.sglist, 0, UNSET,
561 buf->bpl, 0, buf->vb.height);
562 break;
563 case V4L2_FIELD_BOTTOM:
564 cx88_risc_buffer(dev->pci, &buf->risc,
565 buf->vb.dma.sglist, UNSET, 0,
566 buf->bpl, 0, buf->vb.height);
567 break;
568 case V4L2_FIELD_INTERLACED:
569 cx88_risc_buffer(dev->pci, &buf->risc,
570 buf->vb.dma.sglist, 0, buf->bpl,
571 buf->bpl, buf->bpl,
572 buf->vb.height >> 1);
573 break;
574 case V4L2_FIELD_SEQ_TB:
575 cx88_risc_buffer(dev->pci, &buf->risc,
576 buf->vb.dma.sglist,
577 0, buf->bpl * (buf->vb.height >> 1),
578 buf->bpl, 0,
579 buf->vb.height >> 1);
580 break;
581 case V4L2_FIELD_SEQ_BT:
582 cx88_risc_buffer(dev->pci, &buf->risc,
583 buf->vb.dma.sglist,
584 buf->bpl * (buf->vb.height >> 1), 0,
585 buf->bpl, 0,
586 buf->vb.height >> 1);
587 break;
588 default:
589 BUG();
592 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
593 buf, buf->vb.i,
594 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
595 (unsigned long)buf->risc.dma);
597 buf->vb.state = STATE_PREPARED;
598 return 0;
600 fail:
601 cx88_free_buffer(dev->pci,buf);
602 return rc;
605 static void
606 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
608 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
609 struct cx88_buffer *prev;
610 struct cx8800_fh *fh = vq->priv_data;
611 struct cx8800_dev *dev = fh->dev;
612 struct cx88_dmaqueue *q = &dev->vidq;
614 /* add jump to stopper */
615 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
616 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
618 if (!list_empty(&q->queued)) {
619 list_add_tail(&buf->vb.queue,&q->queued);
620 buf->vb.state = STATE_QUEUED;
621 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
622 buf, buf->vb.i);
624 } else if (list_empty(&q->active)) {
625 list_add_tail(&buf->vb.queue,&q->active);
626 start_video_dma(dev, q, buf);
627 buf->vb.state = STATE_ACTIVE;
628 buf->count = q->count++;
629 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
630 dprintk(2,"[%p/%d] buffer_queue - first active\n",
631 buf, buf->vb.i);
633 } else {
634 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
635 if (prev->vb.width == buf->vb.width &&
636 prev->vb.height == buf->vb.height &&
637 prev->fmt == buf->fmt) {
638 list_add_tail(&buf->vb.queue,&q->active);
639 buf->vb.state = STATE_ACTIVE;
640 buf->count = q->count++;
641 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
642 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
643 buf, buf->vb.i);
645 } else {
646 list_add_tail(&buf->vb.queue,&q->queued);
647 buf->vb.state = STATE_QUEUED;
648 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
649 buf, buf->vb.i);
654 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
656 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
657 struct cx8800_fh *fh = q->priv_data;
659 cx88_free_buffer(fh->dev->pci,buf);
662 static struct videobuf_queue_ops cx8800_video_qops = {
663 .buf_setup = buffer_setup,
664 .buf_prepare = buffer_prepare,
665 .buf_queue = buffer_queue,
666 .buf_release = buffer_release,
669 /* ------------------------------------------------------------------ */
672 /* ------------------------------------------------------------------ */
674 static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
676 switch (fh->type) {
677 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
678 return &fh->vidq;
679 case V4L2_BUF_TYPE_VBI_CAPTURE:
680 return &fh->vbiq;
681 default:
682 BUG();
683 return NULL;
687 static int get_ressource(struct cx8800_fh *fh)
689 switch (fh->type) {
690 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
691 return RESOURCE_VIDEO;
692 case V4L2_BUF_TYPE_VBI_CAPTURE:
693 return RESOURCE_VBI;
694 default:
695 BUG();
696 return 0;
700 static int video_open(struct inode *inode, struct file *file)
702 int minor = iminor(inode);
703 struct cx8800_dev *h,*dev = NULL;
704 struct cx8800_fh *fh;
705 struct list_head *list;
706 enum v4l2_buf_type type = 0;
707 int radio = 0;
709 list_for_each(list,&cx8800_devlist) {
710 h = list_entry(list, struct cx8800_dev, devlist);
711 if (h->video_dev->minor == minor) {
712 dev = h;
713 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
715 if (h->vbi_dev->minor == minor) {
716 dev = h;
717 type = V4L2_BUF_TYPE_VBI_CAPTURE;
719 if (h->radio_dev &&
720 h->radio_dev->minor == minor) {
721 radio = 1;
722 dev = h;
725 if (NULL == dev)
726 return -ENODEV;
728 dprintk(1,"open minor=%d radio=%d type=%s\n",
729 minor,radio,v4l2_type_names[type]);
731 /* allocate + initialize per filehandle data */
732 fh = kmalloc(sizeof(*fh),GFP_KERNEL);
733 if (NULL == fh)
734 return -ENOMEM;
735 memset(fh,0,sizeof(*fh));
736 file->private_data = fh;
737 fh->dev = dev;
738 fh->radio = radio;
739 fh->type = type;
740 fh->width = 320;
741 fh->height = 240;
742 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
744 videobuf_queue_init(&fh->vidq, &cx8800_video_qops,
745 dev->pci, &dev->slock,
746 V4L2_BUF_TYPE_VIDEO_CAPTURE,
747 V4L2_FIELD_INTERLACED,
748 sizeof(struct cx88_buffer),
749 fh);
750 videobuf_queue_init(&fh->vbiq, &cx8800_vbi_qops,
751 dev->pci, &dev->slock,
752 V4L2_BUF_TYPE_VBI_CAPTURE,
753 V4L2_FIELD_SEQ_TB,
754 sizeof(struct cx88_buffer),
755 fh);
757 if (fh->radio) {
758 struct cx88_core *core = dev->core;
759 int board = core->board;
760 dprintk(1,"video_open: setting radio device\n");
761 cx_write(MO_GP0_IO, cx88_boards[board].radio.gpio0);
762 cx_write(MO_GP1_IO, cx88_boards[board].radio.gpio1);
763 cx_write(MO_GP2_IO, cx88_boards[board].radio.gpio2);
764 cx_write(MO_GP3_IO, cx88_boards[board].radio.gpio3);
765 dev->core->tvaudio = WW_FM;
766 cx88_set_tvaudio(core);
767 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
768 cx88_call_i2c_clients(dev->core,AUDC_SET_RADIO,NULL);
771 return 0;
774 static ssize_t
775 video_read(struct file *file, char *data, size_t count, loff_t *ppos)
777 struct cx8800_fh *fh = file->private_data;
779 switch (fh->type) {
780 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
781 if (res_locked(fh->dev,RESOURCE_VIDEO))
782 return -EBUSY;
783 return videobuf_read_one(&fh->vidq, data, count, ppos,
784 file->f_flags & O_NONBLOCK);
785 case V4L2_BUF_TYPE_VBI_CAPTURE:
786 if (!res_get(fh->dev,fh,RESOURCE_VBI))
787 return -EBUSY;
788 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
789 file->f_flags & O_NONBLOCK);
790 default:
791 BUG();
792 return 0;
796 static unsigned int
797 video_poll(struct file *file, struct poll_table_struct *wait)
799 struct cx8800_fh *fh = file->private_data;
800 struct cx88_buffer *buf;
802 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
803 if (!res_get(fh->dev,fh,RESOURCE_VBI))
804 return POLLERR;
805 return videobuf_poll_stream(file, &fh->vbiq, wait);
808 if (res_check(fh,RESOURCE_VIDEO)) {
809 /* streaming capture */
810 if (list_empty(&fh->vidq.stream))
811 return POLLERR;
812 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
813 } else {
814 /* read() capture */
815 buf = (struct cx88_buffer*)fh->vidq.read_buf;
816 if (NULL == buf)
817 return POLLERR;
819 poll_wait(file, &buf->vb.done, wait);
820 if (buf->vb.state == STATE_DONE ||
821 buf->vb.state == STATE_ERROR)
822 return POLLIN|POLLRDNORM;
823 return 0;
826 static int video_release(struct inode *inode, struct file *file)
828 struct cx8800_fh *fh = file->private_data;
829 struct cx8800_dev *dev = fh->dev;
831 /* turn off overlay */
832 if (res_check(fh, RESOURCE_OVERLAY)) {
833 /* FIXME */
834 res_free(dev,fh,RESOURCE_OVERLAY);
837 /* stop video capture */
838 if (res_check(fh, RESOURCE_VIDEO)) {
839 videobuf_queue_cancel(&fh->vidq);
840 res_free(dev,fh,RESOURCE_VIDEO);
842 if (fh->vidq.read_buf) {
843 buffer_release(&fh->vidq,fh->vidq.read_buf);
844 kfree(fh->vidq.read_buf);
847 /* stop vbi capture */
848 if (res_check(fh, RESOURCE_VBI)) {
849 if (fh->vbiq.streaming)
850 videobuf_streamoff(&fh->vbiq);
851 if (fh->vbiq.reading)
852 videobuf_read_stop(&fh->vbiq);
853 res_free(dev,fh,RESOURCE_VBI);
856 videobuf_mmap_free(&fh->vidq);
857 videobuf_mmap_free(&fh->vbiq);
858 file->private_data = NULL;
859 kfree(fh);
860 return 0;
863 static int
864 video_mmap(struct file *file, struct vm_area_struct * vma)
866 struct cx8800_fh *fh = file->private_data;
868 return videobuf_mmap_mapper(get_queue(fh), vma);
871 /* ------------------------------------------------------------------ */
873 static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl)
875 struct cx88_core *core = dev->core;
876 struct cx88_ctrl *c = NULL;
877 u32 value;
878 int i;
880 for (i = 0; i < CX8800_CTLS; i++)
881 if (cx8800_ctls[i].v.id == ctl->id)
882 c = &cx8800_ctls[i];
883 if (NULL == c)
884 return -EINVAL;
886 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
887 switch (ctl->id) {
888 case V4L2_CID_AUDIO_BALANCE:
889 ctl->value = (value & 0x40) ? (value & 0x3f) : (0x40 - (value & 0x3f));
890 break;
891 case V4L2_CID_AUDIO_VOLUME:
892 ctl->value = 0x3f - (value & 0x3f);
893 break;
894 default:
895 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
896 break;
898 return 0;
901 static int set_control(struct cx8800_dev *dev, struct v4l2_control *ctl)
903 struct cx88_core *core = dev->core;
904 struct cx88_ctrl *c = NULL;
905 u32 v_sat_value;
906 u32 value;
907 int i;
909 for (i = 0; i < CX8800_CTLS; i++)
910 if (cx8800_ctls[i].v.id == ctl->id)
911 c = &cx8800_ctls[i];
912 if (NULL == c)
913 return -EINVAL;
915 if (ctl->value < c->v.minimum)
916 return -ERANGE;
917 if (ctl->value > c->v.maximum)
918 return -ERANGE;
919 switch (ctl->id) {
920 case V4L2_CID_AUDIO_BALANCE:
921 value = (ctl->value < 0x40) ? (0x40 - ctl->value) : ctl->value;
922 break;
923 case V4L2_CID_AUDIO_VOLUME:
924 value = 0x3f - (ctl->value & 0x3f);
925 break;
926 case V4L2_CID_SATURATION:
927 /* special v_sat handling */
928 v_sat_value = ctl->value - (0x7f - 0x5a);
929 if (v_sat_value > 0xff)
930 v_sat_value = 0xff;
931 if (v_sat_value < 0x00)
932 v_sat_value = 0x00;
933 cx_andor(MO_UV_SATURATION, 0xff00, v_sat_value << 8);
934 /* fall through to default route for u_sat */
935 default:
936 value = ((ctl->value - c->off) << c->shift) & c->mask;
937 break;
939 dprintk(1,"set_control id=0x%X reg=0x%x val=0x%x%s\n",
940 ctl->id, c->reg, value, c->sreg ? " [shadowed]" : "");
941 if (c->sreg) {
942 cx_sandor(c->sreg, c->reg, c->mask, value);
943 } else {
944 cx_andor(c->reg, c->mask, value);
946 return 0;
949 static void init_controls(struct cx8800_dev *dev)
951 static struct v4l2_control mute = {
952 .id = V4L2_CID_AUDIO_MUTE,
953 .value = 1,
955 static struct v4l2_control volume = {
956 .id = V4L2_CID_AUDIO_VOLUME,
957 .value = 0x3f,
959 static struct v4l2_control hue = {
960 .id = V4L2_CID_HUE,
961 .value = 0x80,
963 static struct v4l2_control contrast = {
964 .id = V4L2_CID_CONTRAST,
965 .value = 0x80,
967 static struct v4l2_control brightness = {
968 .id = V4L2_CID_BRIGHTNESS,
969 .value = 0x80,
972 set_control(dev,&mute);
973 set_control(dev,&volume);
974 set_control(dev,&hue);
975 set_control(dev,&contrast);
976 set_control(dev,&brightness);
979 /* ------------------------------------------------------------------ */
981 static int cx8800_g_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
982 struct v4l2_format *f)
984 switch (f->type) {
985 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
986 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
987 f->fmt.pix.width = fh->width;
988 f->fmt.pix.height = fh->height;
989 f->fmt.pix.field = fh->vidq.field;
990 f->fmt.pix.pixelformat = fh->fmt->fourcc;
991 f->fmt.pix.bytesperline =
992 (f->fmt.pix.width * fh->fmt->depth) >> 3;
993 f->fmt.pix.sizeimage =
994 f->fmt.pix.height * f->fmt.pix.bytesperline;
995 return 0;
996 case V4L2_BUF_TYPE_VBI_CAPTURE:
997 cx8800_vbi_fmt(dev, f);
998 return 0;
999 default:
1000 return -EINVAL;
1004 static int cx8800_try_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1005 struct v4l2_format *f)
1007 switch (f->type) {
1008 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1010 struct cx8800_fmt *fmt;
1011 enum v4l2_field field;
1012 unsigned int maxw, maxh;
1014 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1015 if (NULL == fmt)
1016 return -EINVAL;
1018 field = f->fmt.pix.field;
1019 maxw = norm_maxw(dev->core->tvnorm);
1020 maxh = norm_maxh(dev->core->tvnorm);
1022 if (V4L2_FIELD_ANY == field) {
1023 field = (f->fmt.pix.height > maxh/2)
1024 ? V4L2_FIELD_INTERLACED
1025 : V4L2_FIELD_BOTTOM;
1028 switch (field) {
1029 case V4L2_FIELD_TOP:
1030 case V4L2_FIELD_BOTTOM:
1031 maxh = maxh / 2;
1032 break;
1033 case V4L2_FIELD_INTERLACED:
1034 break;
1035 default:
1036 return -EINVAL;
1039 f->fmt.pix.field = field;
1040 if (f->fmt.pix.height < 32)
1041 f->fmt.pix.height = 32;
1042 if (f->fmt.pix.height > maxh)
1043 f->fmt.pix.height = maxh;
1044 if (f->fmt.pix.width < 48)
1045 f->fmt.pix.width = 48;
1046 if (f->fmt.pix.width > maxw)
1047 f->fmt.pix.width = maxw;
1048 f->fmt.pix.width &= ~0x03;
1049 f->fmt.pix.bytesperline =
1050 (f->fmt.pix.width * fmt->depth) >> 3;
1051 f->fmt.pix.sizeimage =
1052 f->fmt.pix.height * f->fmt.pix.bytesperline;
1054 return 0;
1056 case V4L2_BUF_TYPE_VBI_CAPTURE:
1057 cx8800_vbi_fmt(dev, f);
1058 return 0;
1059 default:
1060 return -EINVAL;
1064 static int cx8800_s_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1065 struct v4l2_format *f)
1067 int err;
1069 switch (f->type) {
1070 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1071 err = cx8800_try_fmt(dev,fh,f);
1072 if (0 != err)
1073 return err;
1075 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1076 fh->width = f->fmt.pix.width;
1077 fh->height = f->fmt.pix.height;
1078 fh->vidq.field = f->fmt.pix.field;
1079 return 0;
1080 case V4L2_BUF_TYPE_VBI_CAPTURE:
1081 cx8800_vbi_fmt(dev, f);
1082 return 0;
1083 default:
1084 return -EINVAL;
1089 * This function is _not_ called directly, but from
1090 * video_generic_ioctl (and maybe others). userspace
1091 * copying is done already, arg is a kernel pointer.
1093 static int video_do_ioctl(struct inode *inode, struct file *file,
1094 unsigned int cmd, void *arg)
1096 struct cx8800_fh *fh = file->private_data;
1097 struct cx8800_dev *dev = fh->dev;
1098 struct cx88_core *core = dev->core;
1099 int err;
1101 if (video_debug > 1)
1102 cx88_print_ioctl(core->name,cmd);
1103 switch (cmd) {
1104 case VIDIOC_QUERYCAP:
1106 struct v4l2_capability *cap = arg;
1108 memset(cap,0,sizeof(*cap));
1109 strcpy(cap->driver, "cx8800");
1110 strlcpy(cap->card, cx88_boards[core->board].name,
1111 sizeof(cap->card));
1112 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1113 cap->version = CX88_VERSION_CODE;
1114 cap->capabilities =
1115 V4L2_CAP_VIDEO_CAPTURE |
1116 V4L2_CAP_READWRITE |
1117 V4L2_CAP_STREAMING |
1118 V4L2_CAP_VBI_CAPTURE |
1120 if (UNSET != core->tuner_type)
1121 cap->capabilities |= V4L2_CAP_TUNER;
1122 return 0;
1125 /* ---------- tv norms ---------- */
1126 case VIDIOC_ENUMSTD:
1128 struct v4l2_standard *e = arg;
1129 unsigned int i;
1131 i = e->index;
1132 if (i >= ARRAY_SIZE(tvnorms))
1133 return -EINVAL;
1134 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1135 tvnorms[e->index].name);
1136 e->index = i;
1137 if (err < 0)
1138 return err;
1139 return 0;
1141 case VIDIOC_G_STD:
1143 v4l2_std_id *id = arg;
1145 *id = core->tvnorm->id;
1146 return 0;
1148 case VIDIOC_S_STD:
1150 v4l2_std_id *id = arg;
1151 unsigned int i;
1153 for(i = 0; i < ARRAY_SIZE(tvnorms); i++)
1154 if (*id & tvnorms[i].id)
1155 break;
1156 if (i == ARRAY_SIZE(tvnorms))
1157 return -EINVAL;
1159 down(&dev->lock);
1160 cx88_set_tvnorm(dev->core,&tvnorms[i]);
1161 up(&dev->lock);
1162 return 0;
1165 /* ------ input switching ---------- */
1166 case VIDIOC_ENUMINPUT:
1168 static const char *iname[] = {
1169 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1170 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1171 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1172 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1173 [ CX88_VMUX_SVIDEO ] = "S-Video",
1174 [ CX88_VMUX_TELEVISION ] = "Television",
1175 [ CX88_VMUX_CABLE ] = "Cable TV",
1176 [ CX88_VMUX_DVB ] = "DVB",
1177 [ CX88_VMUX_DEBUG ] = "for debug only",
1179 struct v4l2_input *i = arg;
1180 unsigned int n;
1182 n = i->index;
1183 if (n >= 4)
1184 return -EINVAL;
1185 if (0 == INPUT(n)->type)
1186 return -EINVAL;
1187 memset(i,0,sizeof(*i));
1188 i->index = n;
1189 i->type = V4L2_INPUT_TYPE_CAMERA;
1190 strcpy(i->name,iname[INPUT(n)->type]);
1191 if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
1192 (CX88_VMUX_CABLE == INPUT(n)->type))
1193 i->type = V4L2_INPUT_TYPE_TUNER;
1194 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1195 i->std |= tvnorms[n].id;
1196 return 0;
1198 case VIDIOC_G_INPUT:
1200 unsigned int *i = arg;
1202 *i = dev->core->input;
1203 return 0;
1205 case VIDIOC_S_INPUT:
1207 unsigned int *i = arg;
1209 if (*i >= 4)
1210 return -EINVAL;
1211 down(&dev->lock);
1212 cx88_newstation(core);
1213 video_mux(dev,*i);
1214 up(&dev->lock);
1215 return 0;
1220 /* --- capture ioctls ---------------------------------------- */
1221 case VIDIOC_ENUM_FMT:
1223 struct v4l2_fmtdesc *f = arg;
1224 enum v4l2_buf_type type;
1225 unsigned int index;
1227 index = f->index;
1228 type = f->type;
1229 switch (type) {
1230 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1231 if (index >= ARRAY_SIZE(formats))
1232 return -EINVAL;
1233 memset(f,0,sizeof(*f));
1234 f->index = index;
1235 f->type = type;
1236 strlcpy(f->description,formats[index].name,sizeof(f->description));
1237 f->pixelformat = formats[index].fourcc;
1238 break;
1239 default:
1240 return -EINVAL;
1242 return 0;
1244 case VIDIOC_G_FMT:
1246 struct v4l2_format *f = arg;
1247 return cx8800_g_fmt(dev,fh,f);
1249 case VIDIOC_S_FMT:
1251 struct v4l2_format *f = arg;
1252 return cx8800_s_fmt(dev,fh,f);
1254 case VIDIOC_TRY_FMT:
1256 struct v4l2_format *f = arg;
1257 return cx8800_try_fmt(dev,fh,f);
1260 /* --- controls ---------------------------------------------- */
1261 case VIDIOC_QUERYCTRL:
1263 struct v4l2_queryctrl *c = arg;
1264 int i;
1266 if (c->id < V4L2_CID_BASE ||
1267 c->id >= V4L2_CID_LASTP1)
1268 return -EINVAL;
1269 for (i = 0; i < CX8800_CTLS; i++)
1270 if (cx8800_ctls[i].v.id == c->id)
1271 break;
1272 if (i == CX8800_CTLS) {
1273 *c = no_ctl;
1274 return 0;
1276 *c = cx8800_ctls[i].v;
1277 return 0;
1279 case VIDIOC_G_CTRL:
1280 return get_control(dev,arg);
1281 case VIDIOC_S_CTRL:
1282 return set_control(dev,arg);
1284 /* --- tuner ioctls ------------------------------------------ */
1285 case VIDIOC_G_TUNER:
1287 struct v4l2_tuner *t = arg;
1288 u32 reg;
1290 if (UNSET == core->tuner_type)
1291 return -EINVAL;
1292 if (0 != t->index)
1293 return -EINVAL;
1295 memset(t,0,sizeof(*t));
1296 strcpy(t->name, "Television");
1297 t->type = V4L2_TUNER_ANALOG_TV;
1298 t->capability = V4L2_TUNER_CAP_NORM;
1299 t->rangehigh = 0xffffffffUL;
1301 cx88_get_stereo(core ,t);
1302 reg = cx_read(MO_DEVICE_STATUS);
1303 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1304 return 0;
1306 case VIDIOC_S_TUNER:
1308 struct v4l2_tuner *t = arg;
1310 if (UNSET == core->tuner_type)
1311 return -EINVAL;
1312 if (0 != t->index)
1313 return -EINVAL;
1314 cx88_set_stereo(core, t->audmode, 1);
1315 return 0;
1317 case VIDIOC_G_FREQUENCY:
1319 struct v4l2_frequency *f = arg;
1321 memset(f,0,sizeof(*f));
1323 if (UNSET == core->tuner_type)
1324 return -EINVAL;
1326 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1327 f->frequency = dev->freq;
1329 cx88_call_i2c_clients(dev->core,VIDIOC_G_FREQUENCY,f);
1331 return 0;
1333 case VIDIOC_S_FREQUENCY:
1335 struct v4l2_frequency *f = arg;
1337 if (UNSET == core->tuner_type)
1338 return -EINVAL;
1339 if (f->tuner != 0)
1340 return -EINVAL;
1341 if (0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV)
1342 return -EINVAL;
1343 if (1 == fh->radio && f->type != V4L2_TUNER_RADIO)
1344 return -EINVAL;
1345 down(&dev->lock);
1346 dev->freq = f->frequency;
1347 cx88_newstation(core);
1348 cx88_call_i2c_clients(dev->core,VIDIOC_S_FREQUENCY,f);
1350 /* When changing channels it is required to reset TVAUDIO */
1351 msleep (10);
1352 cx88_set_tvaudio(core);
1354 up(&dev->lock);
1355 return 0;
1358 /* --- streaming capture ------------------------------------- */
1359 case VIDIOCGMBUF:
1361 struct video_mbuf *mbuf = arg;
1362 struct videobuf_queue *q;
1363 struct v4l2_requestbuffers req;
1364 unsigned int i;
1366 q = get_queue(fh);
1367 memset(&req,0,sizeof(req));
1368 req.type = q->type;
1369 req.count = 8;
1370 req.memory = V4L2_MEMORY_MMAP;
1371 err = videobuf_reqbufs(q,&req);
1372 if (err < 0)
1373 return err;
1374 memset(mbuf,0,sizeof(*mbuf));
1375 mbuf->frames = req.count;
1376 mbuf->size = 0;
1377 for (i = 0; i < mbuf->frames; i++) {
1378 mbuf->offsets[i] = q->bufs[i]->boff;
1379 mbuf->size += q->bufs[i]->bsize;
1381 return 0;
1383 case VIDIOC_REQBUFS:
1384 return videobuf_reqbufs(get_queue(fh), arg);
1386 case VIDIOC_QUERYBUF:
1387 return videobuf_querybuf(get_queue(fh), arg);
1389 case VIDIOC_QBUF:
1390 return videobuf_qbuf(get_queue(fh), arg);
1392 case VIDIOC_DQBUF:
1393 return videobuf_dqbuf(get_queue(fh), arg,
1394 file->f_flags & O_NONBLOCK);
1396 case VIDIOC_STREAMON:
1398 int res = get_ressource(fh);
1400 if (!res_get(dev,fh,res))
1401 return -EBUSY;
1402 return videobuf_streamon(get_queue(fh));
1404 case VIDIOC_STREAMOFF:
1406 int res = get_ressource(fh);
1408 err = videobuf_streamoff(get_queue(fh));
1409 if (err < 0)
1410 return err;
1411 res_free(dev,fh,res);
1412 return 0;
1415 default:
1416 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1417 video_do_ioctl);
1419 return 0;
1422 static int video_ioctl(struct inode *inode, struct file *file,
1423 unsigned int cmd, unsigned long arg)
1425 return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
1428 /* ----------------------------------------------------------- */
1430 static int radio_do_ioctl(struct inode *inode, struct file *file,
1431 unsigned int cmd, void *arg)
1433 struct cx8800_fh *fh = file->private_data;
1434 struct cx8800_dev *dev = fh->dev;
1435 struct cx88_core *core = dev->core;
1437 if (video_debug > 1)
1438 cx88_print_ioctl(core->name,cmd);
1440 switch (cmd) {
1441 case VIDIOC_QUERYCAP:
1443 struct v4l2_capability *cap = arg;
1445 memset(cap,0,sizeof(*cap));
1446 strcpy(cap->driver, "cx8800");
1447 strlcpy(cap->card, cx88_boards[core->board].name,
1448 sizeof(cap->card));
1449 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1450 cap->version = CX88_VERSION_CODE;
1451 cap->capabilities = V4L2_CAP_TUNER;
1452 return 0;
1454 case VIDIOC_G_TUNER:
1456 struct v4l2_tuner *t = arg;
1458 if (t->index > 0)
1459 return -EINVAL;
1461 memset(t,0,sizeof(*t));
1462 strcpy(t->name, "Radio");
1464 cx88_call_i2c_clients(dev->core,VIDIOC_G_TUNER,t);
1465 return 0;
1467 case VIDIOC_ENUMINPUT:
1469 struct v4l2_input *i = arg;
1471 if (i->index != 0)
1472 return -EINVAL;
1473 strcpy(i->name,"Radio");
1474 i->type = V4L2_INPUT_TYPE_TUNER;
1475 return 0;
1477 case VIDIOC_G_INPUT:
1479 int *i = arg;
1480 *i = 0;
1481 return 0;
1483 case VIDIOC_G_AUDIO:
1485 struct v4l2_audio *a = arg;
1487 memset(a,0,sizeof(*a));
1488 strcpy(a->name,"Radio");
1489 return 0;
1491 case VIDIOC_G_STD:
1493 v4l2_std_id *id = arg;
1494 *id = 0;
1495 return 0;
1497 case VIDIOCSTUNER:
1499 struct video_tuner *v = arg;
1501 if (v->tuner) /* Only tuner 0 */
1502 return -EINVAL;
1504 cx88_call_i2c_clients(dev->core,VIDIOCSTUNER,v);
1505 return 0;
1507 case VIDIOC_S_TUNER:
1509 struct v4l2_tuner *t = arg;
1511 if (0 != t->index)
1512 return -EINVAL;
1514 cx88_call_i2c_clients(dev->core,VIDIOC_S_TUNER,t);
1516 return 0;
1519 case VIDIOC_S_AUDIO:
1520 case VIDIOC_S_INPUT:
1521 case VIDIOC_S_STD:
1522 return 0;
1524 case VIDIOC_QUERYCTRL:
1526 struct v4l2_queryctrl *c = arg;
1527 int i;
1529 if (c->id < V4L2_CID_BASE ||
1530 c->id >= V4L2_CID_LASTP1)
1531 return -EINVAL;
1532 if (c->id == V4L2_CID_AUDIO_MUTE) {
1533 for (i = 0; i < CX8800_CTLS; i++)
1534 if (cx8800_ctls[i].v.id == c->id)
1535 break;
1536 *c = cx8800_ctls[i].v;
1537 } else
1538 *c = no_ctl;
1539 return 0;
1543 case VIDIOC_G_CTRL:
1544 case VIDIOC_S_CTRL:
1545 case VIDIOC_G_FREQUENCY:
1546 case VIDIOC_S_FREQUENCY:
1547 return video_do_ioctl(inode,file,cmd,arg);
1549 default:
1550 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1551 radio_do_ioctl);
1553 return 0;
1556 static int radio_ioctl(struct inode *inode, struct file *file,
1557 unsigned int cmd, unsigned long arg)
1559 return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
1562 /* ----------------------------------------------------------- */
1564 static void cx8800_vid_timeout(unsigned long data)
1566 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1567 struct cx88_core *core = dev->core;
1568 struct cx88_dmaqueue *q = &dev->vidq;
1569 struct cx88_buffer *buf;
1570 unsigned long flags;
1572 cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH21]);
1574 cx_clear(MO_VID_DMACNTRL, 0x11);
1575 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1577 spin_lock_irqsave(&dev->slock,flags);
1578 while (!list_empty(&q->active)) {
1579 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1580 list_del(&buf->vb.queue);
1581 buf->vb.state = STATE_ERROR;
1582 wake_up(&buf->vb.done);
1583 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1584 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1586 restart_video_queue(dev,q);
1587 spin_unlock_irqrestore(&dev->slock,flags);
1590 static char *cx88_vid_irqs[32] = {
1591 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1592 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1593 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1594 "y_sync", "u_sync", "v_sync", "vbi_sync",
1595 "opc_err", "par_err", "rip_err", "pci_abort",
1598 static void cx8800_vid_irq(struct cx8800_dev *dev)
1600 struct cx88_core *core = dev->core;
1601 u32 status, mask, count;
1603 status = cx_read(MO_VID_INTSTAT);
1604 mask = cx_read(MO_VID_INTMSK);
1605 if (0 == (status & mask))
1606 return;
1607 cx_write(MO_VID_INTSTAT, status);
1608 if (irq_debug || (status & mask & ~0xff))
1609 cx88_print_irqbits(core->name, "irq vid",
1610 cx88_vid_irqs, status, mask);
1612 /* risc op code error */
1613 if (status & (1 << 16)) {
1614 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1615 cx_clear(MO_VID_DMACNTRL, 0x11);
1616 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1617 cx88_sram_channel_dump(dev->core, &cx88_sram_channels[SRAM_CH21]);
1620 /* risc1 y */
1621 if (status & 0x01) {
1622 spin_lock(&dev->slock);
1623 count = cx_read(MO_VIDY_GPCNT);
1624 cx88_wakeup(dev->core, &dev->vidq, count);
1625 spin_unlock(&dev->slock);
1628 /* risc1 vbi */
1629 if (status & 0x08) {
1630 spin_lock(&dev->slock);
1631 count = cx_read(MO_VBI_GPCNT);
1632 cx88_wakeup(dev->core, &dev->vbiq, count);
1633 spin_unlock(&dev->slock);
1636 /* risc2 y */
1637 if (status & 0x10) {
1638 dprintk(2,"stopper video\n");
1639 spin_lock(&dev->slock);
1640 restart_video_queue(dev,&dev->vidq);
1641 spin_unlock(&dev->slock);
1644 /* risc2 vbi */
1645 if (status & 0x80) {
1646 dprintk(2,"stopper vbi\n");
1647 spin_lock(&dev->slock);
1648 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1649 spin_unlock(&dev->slock);
1653 static irqreturn_t cx8800_irq(int irq, void *dev_id, struct pt_regs *regs)
1655 struct cx8800_dev *dev = dev_id;
1656 struct cx88_core *core = dev->core;
1657 u32 status;
1658 int loop, handled = 0;
1660 for (loop = 0; loop < 10; loop++) {
1661 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
1662 if (0 == status)
1663 goto out;
1664 cx_write(MO_PCI_INTSTAT, status);
1665 handled = 1;
1667 if (status & core->pci_irqmask)
1668 cx88_core_irq(core,status);
1669 if (status & 0x01)
1670 cx8800_vid_irq(dev);
1672 if (10 == loop) {
1673 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1674 core->name);
1675 cx_write(MO_PCI_INTMSK,0);
1678 out:
1679 return IRQ_RETVAL(handled);
1682 /* ----------------------------------------------------------- */
1683 /* exported stuff */
1685 static struct file_operations video_fops =
1687 .owner = THIS_MODULE,
1688 .open = video_open,
1689 .release = video_release,
1690 .read = video_read,
1691 .poll = video_poll,
1692 .mmap = video_mmap,
1693 .ioctl = video_ioctl,
1694 .llseek = no_llseek,
1697 static struct video_device cx8800_video_template =
1699 .name = "cx8800-video",
1700 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1701 .hardware = 0,
1702 .fops = &video_fops,
1703 .minor = -1,
1706 static struct video_device cx8800_vbi_template =
1708 .name = "cx8800-vbi",
1709 .type = VID_TYPE_TELETEXT|VID_TYPE_TUNER,
1710 .hardware = 0,
1711 .fops = &video_fops,
1712 .minor = -1,
1715 static struct file_operations radio_fops =
1717 .owner = THIS_MODULE,
1718 .open = video_open,
1719 .release = video_release,
1720 .ioctl = radio_ioctl,
1721 .llseek = no_llseek,
1724 static struct video_device cx8800_radio_template =
1726 .name = "cx8800-radio",
1727 .type = VID_TYPE_TUNER,
1728 .hardware = 0,
1729 .fops = &radio_fops,
1730 .minor = -1,
1733 /* ----------------------------------------------------------- */
1735 static void cx8800_unregister_video(struct cx8800_dev *dev)
1737 if (dev->radio_dev) {
1738 if (-1 != dev->radio_dev->minor)
1739 video_unregister_device(dev->radio_dev);
1740 else
1741 video_device_release(dev->radio_dev);
1742 dev->radio_dev = NULL;
1744 if (dev->vbi_dev) {
1745 if (-1 != dev->vbi_dev->minor)
1746 video_unregister_device(dev->vbi_dev);
1747 else
1748 video_device_release(dev->vbi_dev);
1749 dev->vbi_dev = NULL;
1751 if (dev->video_dev) {
1752 if (-1 != dev->video_dev->minor)
1753 video_unregister_device(dev->video_dev);
1754 else
1755 video_device_release(dev->video_dev);
1756 dev->video_dev = NULL;
1760 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1761 const struct pci_device_id *pci_id)
1763 struct cx8800_dev *dev;
1764 struct cx88_core *core;
1765 int err;
1767 dev = kmalloc(sizeof(*dev),GFP_KERNEL);
1768 if (NULL == dev)
1769 return -ENOMEM;
1770 memset(dev,0,sizeof(*dev));
1772 /* pci init */
1773 dev->pci = pci_dev;
1774 if (pci_enable_device(pci_dev)) {
1775 err = -EIO;
1776 goto fail_free;
1778 core = cx88_core_get(dev->pci);
1779 if (NULL == core) {
1780 err = -EINVAL;
1781 goto fail_free;
1783 dev->core = core;
1785 /* print pci info */
1786 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
1787 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1788 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1789 "latency: %d, mmio: 0x%lx\n", core->name,
1790 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1791 dev->pci_lat,pci_resource_start(pci_dev,0));
1793 pci_set_master(pci_dev);
1794 if (!pci_dma_supported(pci_dev,0xffffffff)) {
1795 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1796 err = -EIO;
1797 goto fail_core;
1800 /* initialize driver struct */
1801 init_MUTEX(&dev->lock);
1802 spin_lock_init(&dev->slock);
1803 core->tvnorm = tvnorms;
1805 /* init video dma queues */
1806 INIT_LIST_HEAD(&dev->vidq.active);
1807 INIT_LIST_HEAD(&dev->vidq.queued);
1808 dev->vidq.timeout.function = cx8800_vid_timeout;
1809 dev->vidq.timeout.data = (unsigned long)dev;
1810 init_timer(&dev->vidq.timeout);
1811 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1812 MO_VID_DMACNTRL,0x11,0x00);
1814 /* init vbi dma queues */
1815 INIT_LIST_HEAD(&dev->vbiq.active);
1816 INIT_LIST_HEAD(&dev->vbiq.queued);
1817 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1818 dev->vbiq.timeout.data = (unsigned long)dev;
1819 init_timer(&dev->vbiq.timeout);
1820 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1821 MO_VID_DMACNTRL,0x88,0x00);
1823 /* get irq */
1824 err = request_irq(pci_dev->irq, cx8800_irq,
1825 SA_SHIRQ | SA_INTERRUPT, core->name, dev);
1826 if (err < 0) {
1827 printk(KERN_ERR "%s: can't get IRQ %d\n",
1828 core->name,pci_dev->irq);
1829 goto fail_core;
1831 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1833 /* load and configure helper modules */
1834 if (TUNER_ABSENT != core->tuner_type)
1835 request_module("tuner");
1836 if (core->tda9887_conf)
1837 request_module("tda9887");
1838 /* register v4l devices */
1839 dev->video_dev = cx88_vdev_init(core,dev->pci,
1840 &cx8800_video_template,"video");
1841 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1842 video_nr[core->nr]);
1843 if (err < 0) {
1844 printk(KERN_INFO "%s: can't register video device\n",
1845 core->name);
1846 goto fail_unreg;
1848 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1849 core->name,dev->video_dev->minor & 0x1f);
1851 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1852 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1853 vbi_nr[core->nr]);
1854 if (err < 0) {
1855 printk(KERN_INFO "%s/0: can't register vbi device\n",
1856 core->name);
1857 goto fail_unreg;
1859 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1860 core->name,dev->vbi_dev->minor & 0x1f);
1862 if (core->has_radio) {
1863 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1864 &cx8800_radio_template,"radio");
1865 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1866 radio_nr[core->nr]);
1867 if (err < 0) {
1868 printk(KERN_INFO "%s/0: can't register radio device\n",
1869 core->name);
1870 goto fail_unreg;
1872 printk(KERN_INFO "%s/0: registered device radio%d\n",
1873 core->name,dev->radio_dev->minor & 0x1f);
1876 /* everything worked */
1877 list_add_tail(&dev->devlist,&cx8800_devlist);
1878 pci_set_drvdata(pci_dev,dev);
1880 /* initial device configuration */
1881 down(&dev->lock);
1882 init_controls(dev);
1883 cx88_set_tvnorm(dev->core,tvnorms);
1884 video_mux(dev,0);
1885 up(&dev->lock);
1887 /* start tvaudio thread */
1888 if (core->tuner_type != TUNER_ABSENT)
1889 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
1890 return 0;
1892 fail_unreg:
1893 cx8800_unregister_video(dev);
1894 free_irq(pci_dev->irq, dev);
1895 fail_core:
1896 cx88_core_put(core,dev->pci);
1897 fail_free:
1898 kfree(dev);
1899 return err;
1902 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1904 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1906 /* stop thread */
1907 if (dev->core->kthread) {
1908 kthread_stop(dev->core->kthread);
1909 dev->core->kthread = NULL;
1912 cx88_shutdown(dev->core); /* FIXME */
1913 pci_disable_device(pci_dev);
1915 /* unregister stuff */
1917 free_irq(pci_dev->irq, dev);
1918 cx8800_unregister_video(dev);
1919 pci_set_drvdata(pci_dev, NULL);
1921 /* free memory */
1922 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1923 list_del(&dev->devlist);
1924 cx88_core_put(dev->core,dev->pci);
1925 kfree(dev);
1928 static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1930 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1931 struct cx88_core *core = dev->core;
1933 /* stop video+vbi capture */
1934 spin_lock(&dev->slock);
1935 if (!list_empty(&dev->vidq.active)) {
1936 printk("%s: suspend video\n", core->name);
1937 stop_video_dma(dev);
1938 del_timer(&dev->vidq.timeout);
1940 if (!list_empty(&dev->vbiq.active)) {
1941 printk("%s: suspend vbi\n", core->name);
1942 cx8800_stop_vbi_dma(dev);
1943 del_timer(&dev->vbiq.timeout);
1945 spin_unlock(&dev->slock);
1947 /* FIXME -- shutdown device */
1948 cx88_shutdown(dev->core);
1950 pci_save_state(pci_dev);
1951 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1952 pci_disable_device(pci_dev);
1953 dev->state.disabled = 1;
1955 return 0;
1958 static int cx8800_resume(struct pci_dev *pci_dev)
1960 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1961 struct cx88_core *core = dev->core;
1963 if (dev->state.disabled) {
1964 pci_enable_device(pci_dev);
1965 dev->state.disabled = 0;
1967 pci_set_power_state(pci_dev, PCI_D0);
1968 pci_restore_state(pci_dev);
1970 /* FIXME: re-initialize hardware */
1971 cx88_reset(dev->core);
1973 /* restart video+vbi capture */
1974 spin_lock(&dev->slock);
1975 if (!list_empty(&dev->vidq.active)) {
1976 printk("%s: resume video\n", core->name);
1977 restart_video_queue(dev,&dev->vidq);
1979 if (!list_empty(&dev->vbiq.active)) {
1980 printk("%s: resume vbi\n", core->name);
1981 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1983 spin_unlock(&dev->slock);
1985 return 0;
1988 /* ----------------------------------------------------------- */
1990 static struct pci_device_id cx8800_pci_tbl[] = {
1992 .vendor = 0x14f1,
1993 .device = 0x8800,
1994 .subvendor = PCI_ANY_ID,
1995 .subdevice = PCI_ANY_ID,
1997 /* --- end of list --- */
2000 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2002 static struct pci_driver cx8800_pci_driver = {
2003 .name = "cx8800",
2004 .id_table = cx8800_pci_tbl,
2005 .probe = cx8800_initdev,
2006 .remove = __devexit_p(cx8800_finidev),
2008 .suspend = cx8800_suspend,
2009 .resume = cx8800_resume,
2012 static int cx8800_init(void)
2014 printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
2015 (CX88_VERSION_CODE >> 16) & 0xff,
2016 (CX88_VERSION_CODE >> 8) & 0xff,
2017 CX88_VERSION_CODE & 0xff);
2018 #ifdef SNAPSHOT
2019 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2020 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2021 #endif
2022 return pci_register_driver(&cx8800_pci_driver);
2025 static void cx8800_fini(void)
2027 pci_unregister_driver(&cx8800_pci_driver);
2030 module_init(cx8800_init);
2031 module_exit(cx8800_fini);
2033 /* ----------------------------------------------------------- */
2035 * Local variables:
2036 * c-basic-offset: 8
2037 * End:
2038 * kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off