4 * device driver for Conexant 2388x based TV cards
5 * video4linux video interface
7 * (c) 2003-04 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
9 * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
10 * - Multituner support
11 * - video_ioctl2 conversion
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <linux/init.h>
30 #include <linux/list.h>
31 #include <linux/module.h>
32 #include <linux/moduleparam.h>
33 #include <linux/kmod.h>
34 #include <linux/kernel.h>
35 #include <linux/slab.h>
36 #include <linux/interrupt.h>
37 #include <linux/delay.h>
38 #include <linux/kthread.h>
39 #include <asm/div64.h>
42 #include <media/v4l2-common.h>
44 #ifdef CONFIG_VIDEO_V4L1_COMPAT
45 /* Include V4L1 specific functions. Should be removed soon */
46 #include <linux/videodev.h>
49 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
50 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
51 MODULE_LICENSE("GPL");
53 /* ------------------------------------------------------------------ */
55 static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
56 static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
57 static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
59 module_param_array(video_nr, int, NULL, 0444);
60 module_param_array(vbi_nr, int, NULL, 0444);
61 module_param_array(radio_nr, int, NULL, 0444);
63 MODULE_PARM_DESC(video_nr,"video device numbers");
64 MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
65 MODULE_PARM_DESC(radio_nr,"radio device numbers");
67 static unsigned int video_debug = 0;
68 module_param(video_debug,int,0644);
69 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
71 static unsigned int irq_debug = 0;
72 module_param(irq_debug,int,0644);
73 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
75 static unsigned int vid_limit = 16;
76 module_param(vid_limit,int,0644);
77 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
79 #define dprintk(level,fmt, arg...) if (video_debug >= level) \
80 printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
82 /* ------------------------------------------------------------------ */
84 static LIST_HEAD(cx8800_devlist);
86 /* ------------------------------------------------------------------- */
89 v4l2_std_id radionorms[] = { 0 };
91 static struct cx8800_fmt formats[] = {
93 .name = "8 bpp, gray",
94 .fourcc = V4L2_PIX_FMT_GREY,
95 .cxformat = ColorFormatY8,
97 .flags = FORMAT_FLAGS_PACKED,
99 .name = "15 bpp RGB, le",
100 .fourcc = V4L2_PIX_FMT_RGB555,
101 .cxformat = ColorFormatRGB15,
103 .flags = FORMAT_FLAGS_PACKED,
105 .name = "15 bpp RGB, be",
106 .fourcc = V4L2_PIX_FMT_RGB555X,
107 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
109 .flags = FORMAT_FLAGS_PACKED,
111 .name = "16 bpp RGB, le",
112 .fourcc = V4L2_PIX_FMT_RGB565,
113 .cxformat = ColorFormatRGB16,
115 .flags = FORMAT_FLAGS_PACKED,
117 .name = "16 bpp RGB, be",
118 .fourcc = V4L2_PIX_FMT_RGB565X,
119 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
121 .flags = FORMAT_FLAGS_PACKED,
123 .name = "24 bpp RGB, le",
124 .fourcc = V4L2_PIX_FMT_BGR24,
125 .cxformat = ColorFormatRGB24,
127 .flags = FORMAT_FLAGS_PACKED,
129 .name = "32 bpp RGB, le",
130 .fourcc = V4L2_PIX_FMT_BGR32,
131 .cxformat = ColorFormatRGB32,
133 .flags = FORMAT_FLAGS_PACKED,
135 .name = "32 bpp RGB, be",
136 .fourcc = V4L2_PIX_FMT_RGB32,
137 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
139 .flags = FORMAT_FLAGS_PACKED,
141 .name = "4:2:2, packed, YUYV",
142 .fourcc = V4L2_PIX_FMT_YUYV,
143 .cxformat = ColorFormatYUY2,
145 .flags = FORMAT_FLAGS_PACKED,
147 .name = "4:2:2, packed, UYVY",
148 .fourcc = V4L2_PIX_FMT_UYVY,
149 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
151 .flags = FORMAT_FLAGS_PACKED,
155 static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
159 for (i = 0; i < ARRAY_SIZE(formats); i++)
160 if (formats[i].fourcc == fourcc)
165 /* ------------------------------------------------------------------- */
167 static const struct v4l2_queryctrl no_ctl = {
169 .flags = V4L2_CTRL_FLAG_DISABLED,
172 static struct cx88_ctrl cx8800_ctls[] = {
176 .id = V4L2_CID_BRIGHTNESS,
177 .name = "Brightness",
181 .default_value = 0x7f,
182 .type = V4L2_CTRL_TYPE_INTEGER,
185 .reg = MO_CONTR_BRIGHT,
190 .id = V4L2_CID_CONTRAST,
195 .default_value = 0x3f,
196 .type = V4L2_CTRL_TYPE_INTEGER,
199 .reg = MO_CONTR_BRIGHT,
209 .default_value = 0x7f,
210 .type = V4L2_CTRL_TYPE_INTEGER,
217 /* strictly, this only describes only U saturation.
218 * V saturation is handled specially through code.
221 .id = V4L2_CID_SATURATION,
222 .name = "Saturation",
226 .default_value = 0x7f,
227 .type = V4L2_CTRL_TYPE_INTEGER,
230 .reg = MO_UV_SATURATION,
236 .id = V4L2_CID_AUDIO_MUTE,
241 .type = V4L2_CTRL_TYPE_BOOLEAN,
244 .sreg = SHADOW_AUD_VOL_CTL,
249 .id = V4L2_CID_AUDIO_VOLUME,
254 .default_value = 0x3f,
255 .type = V4L2_CTRL_TYPE_INTEGER,
258 .sreg = SHADOW_AUD_VOL_CTL,
263 .id = V4L2_CID_AUDIO_BALANCE,
268 .default_value = 0x40,
269 .type = V4L2_CTRL_TYPE_INTEGER,
272 .sreg = SHADOW_AUD_BAL_CTL,
277 static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
279 const u32 cx88_user_ctrls[] = {
285 V4L2_CID_AUDIO_VOLUME,
286 V4L2_CID_AUDIO_BALANCE,
290 EXPORT_SYMBOL(cx88_user_ctrls);
292 static const u32 *ctrl_classes[] = {
297 int cx8800_ctrl_query(struct v4l2_queryctrl *qctrl)
301 if (qctrl->id < V4L2_CID_BASE ||
302 qctrl->id >= V4L2_CID_LASTP1)
304 for (i = 0; i < CX8800_CTLS; i++)
305 if (cx8800_ctls[i].v.id == qctrl->id)
307 if (i == CX8800_CTLS) {
311 *qctrl = cx8800_ctls[i].v;
314 EXPORT_SYMBOL(cx8800_ctrl_query);
316 /* ------------------------------------------------------------------- */
317 /* resource management */
319 static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
321 struct cx88_core *core = dev->core;
322 if (fh->resources & bit)
323 /* have it already allocated */
327 mutex_lock(&core->lock);
328 if (dev->resources & bit) {
329 /* no, someone else uses it */
330 mutex_unlock(&core->lock);
333 /* it's free, grab it */
334 fh->resources |= bit;
335 dev->resources |= bit;
336 dprintk(1,"res: get %d\n",bit);
337 mutex_unlock(&core->lock);
342 int res_check(struct cx8800_fh *fh, unsigned int bit)
344 return (fh->resources & bit);
348 int res_locked(struct cx8800_dev *dev, unsigned int bit)
350 return (dev->resources & bit);
354 void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
356 struct cx88_core *core = dev->core;
357 BUG_ON((fh->resources & bits) != bits);
359 mutex_lock(&core->lock);
360 fh->resources &= ~bits;
361 dev->resources &= ~bits;
362 dprintk(1,"res: put %d\n",bits);
363 mutex_unlock(&core->lock);
366 /* ------------------------------------------------------------------ */
368 int cx88_video_mux(struct cx88_core *core, unsigned int input)
370 /* struct cx88_core *core = dev->core; */
372 dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
373 input, INPUT(input)->vmux,
374 INPUT(input)->gpio0,INPUT(input)->gpio1,
375 INPUT(input)->gpio2,INPUT(input)->gpio3);
377 cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input)->vmux << 14);
378 cx_write(MO_GP3_IO, INPUT(input)->gpio3);
379 cx_write(MO_GP0_IO, INPUT(input)->gpio0);
380 cx_write(MO_GP1_IO, INPUT(input)->gpio1);
381 cx_write(MO_GP2_IO, INPUT(input)->gpio2);
383 switch (INPUT(input)->type) {
384 case CX88_VMUX_SVIDEO:
385 cx_set(MO_AFECFG_IO, 0x00000001);
386 cx_set(MO_INPUT_FORMAT, 0x00010010);
387 cx_set(MO_FILTER_EVEN, 0x00002020);
388 cx_set(MO_FILTER_ODD, 0x00002020);
391 cx_clear(MO_AFECFG_IO, 0x00000001);
392 cx_clear(MO_INPUT_FORMAT, 0x00010010);
393 cx_clear(MO_FILTER_EVEN, 0x00002020);
394 cx_clear(MO_FILTER_ODD, 0x00002020);
398 if (cx88_boards[core->board].mpeg & CX88_MPEG_BLACKBIRD) {
399 /* sets sound input from external adc */
400 if (INPUT(input)->extadc)
401 cx_set(AUD_CTL, EN_I2SIN_ENABLE);
403 cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
407 EXPORT_SYMBOL(cx88_video_mux);
409 /* ------------------------------------------------------------------ */
411 static int start_video_dma(struct cx8800_dev *dev,
412 struct cx88_dmaqueue *q,
413 struct cx88_buffer *buf)
415 struct cx88_core *core = dev->core;
417 /* setup fifo + format */
418 cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
419 buf->bpl, buf->risc.dma);
420 cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
421 cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
424 cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
428 cx_set(MO_PCI_INTMSK, core->pci_irqmask | 0x01);
430 /* Enables corresponding bits at PCI_INT_STAT:
431 bits 0 to 4: video, audio, transport stream, VIP, Host
433 bits 8 and 9: DMA complete for: SRC, DST
434 bits 10 and 11: BERR signal asserted for RISC: RD, WR
435 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
437 cx_set(MO_VID_INTMSK, 0x0f0011);
440 cx_set(VID_CAPTURE_CONTROL,0x06);
443 cx_set(MO_DEV_CNTRL2, (1<<5));
444 cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
450 static int stop_video_dma(struct cx8800_dev *dev)
452 struct cx88_core *core = dev->core;
455 cx_clear(MO_VID_DMACNTRL, 0x11);
457 /* disable capture */
458 cx_clear(VID_CAPTURE_CONTROL,0x06);
461 cx_clear(MO_PCI_INTMSK, 0x000001);
462 cx_clear(MO_VID_INTMSK, 0x0f0011);
467 static int restart_video_queue(struct cx8800_dev *dev,
468 struct cx88_dmaqueue *q)
470 struct cx88_core *core = dev->core;
471 struct cx88_buffer *buf, *prev;
472 struct list_head *item;
474 if (!list_empty(&q->active)) {
475 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
476 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
478 start_video_dma(dev, q, buf);
479 list_for_each(item,&q->active) {
480 buf = list_entry(item, struct cx88_buffer, vb.queue);
481 buf->count = q->count++;
483 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
489 if (list_empty(&q->queued))
491 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
493 list_move_tail(&buf->vb.queue, &q->active);
494 start_video_dma(dev, q, buf);
495 buf->vb.state = STATE_ACTIVE;
496 buf->count = q->count++;
497 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
498 dprintk(2,"[%p/%d] restart_queue - first active\n",
501 } else if (prev->vb.width == buf->vb.width &&
502 prev->vb.height == buf->vb.height &&
503 prev->fmt == buf->fmt) {
504 list_move_tail(&buf->vb.queue, &q->active);
505 buf->vb.state = STATE_ACTIVE;
506 buf->count = q->count++;
507 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
508 dprintk(2,"[%p/%d] restart_queue - move to active\n",
517 /* ------------------------------------------------------------------ */
520 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
522 struct cx8800_fh *fh = q->priv_data;
524 *size = fh->fmt->depth*fh->width*fh->height >> 3;
527 while (*size * *count > vid_limit * 1024 * 1024)
533 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
534 enum v4l2_field field)
536 struct cx8800_fh *fh = q->priv_data;
537 struct cx8800_dev *dev = fh->dev;
538 struct cx88_core *core = dev->core;
539 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
540 int rc, init_buffer = 0;
542 BUG_ON(NULL == fh->fmt);
543 if (fh->width < 48 || fh->width > norm_maxw(core->tvnorm) ||
544 fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
546 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
547 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
550 if (buf->fmt != fh->fmt ||
551 buf->vb.width != fh->width ||
552 buf->vb.height != fh->height ||
553 buf->vb.field != field) {
555 buf->vb.width = fh->width;
556 buf->vb.height = fh->height;
557 buf->vb.field = field;
561 if (STATE_NEEDS_INIT == buf->vb.state) {
563 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
568 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
569 switch (buf->vb.field) {
571 cx88_risc_buffer(dev->pci, &buf->risc,
572 buf->vb.dma.sglist, 0, UNSET,
573 buf->bpl, 0, buf->vb.height);
575 case V4L2_FIELD_BOTTOM:
576 cx88_risc_buffer(dev->pci, &buf->risc,
577 buf->vb.dma.sglist, UNSET, 0,
578 buf->bpl, 0, buf->vb.height);
580 case V4L2_FIELD_INTERLACED:
581 cx88_risc_buffer(dev->pci, &buf->risc,
582 buf->vb.dma.sglist, 0, buf->bpl,
584 buf->vb.height >> 1);
586 case V4L2_FIELD_SEQ_TB:
587 cx88_risc_buffer(dev->pci, &buf->risc,
589 0, buf->bpl * (buf->vb.height >> 1),
591 buf->vb.height >> 1);
593 case V4L2_FIELD_SEQ_BT:
594 cx88_risc_buffer(dev->pci, &buf->risc,
596 buf->bpl * (buf->vb.height >> 1), 0,
598 buf->vb.height >> 1);
604 dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
606 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
607 (unsigned long)buf->risc.dma);
609 buf->vb.state = STATE_PREPARED;
613 cx88_free_buffer(q,buf);
618 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
620 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
621 struct cx88_buffer *prev;
622 struct cx8800_fh *fh = vq->priv_data;
623 struct cx8800_dev *dev = fh->dev;
624 struct cx88_core *core = dev->core;
625 struct cx88_dmaqueue *q = &dev->vidq;
627 /* add jump to stopper */
628 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
629 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
631 if (!list_empty(&q->queued)) {
632 list_add_tail(&buf->vb.queue,&q->queued);
633 buf->vb.state = STATE_QUEUED;
634 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
637 } else if (list_empty(&q->active)) {
638 list_add_tail(&buf->vb.queue,&q->active);
639 start_video_dma(dev, q, buf);
640 buf->vb.state = STATE_ACTIVE;
641 buf->count = q->count++;
642 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
643 dprintk(2,"[%p/%d] buffer_queue - first active\n",
647 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
648 if (prev->vb.width == buf->vb.width &&
649 prev->vb.height == buf->vb.height &&
650 prev->fmt == buf->fmt) {
651 list_add_tail(&buf->vb.queue,&q->active);
652 buf->vb.state = STATE_ACTIVE;
653 buf->count = q->count++;
654 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
655 dprintk(2,"[%p/%d] buffer_queue - append to active\n",
659 list_add_tail(&buf->vb.queue,&q->queued);
660 buf->vb.state = STATE_QUEUED;
661 dprintk(2,"[%p/%d] buffer_queue - first queued\n",
667 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
669 struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
671 cx88_free_buffer(q,buf);
674 static struct videobuf_queue_ops cx8800_video_qops = {
675 .buf_setup = buffer_setup,
676 .buf_prepare = buffer_prepare,
677 .buf_queue = buffer_queue,
678 .buf_release = buffer_release,
681 /* ------------------------------------------------------------------ */
684 /* ------------------------------------------------------------------ */
686 static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
689 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
691 case V4L2_BUF_TYPE_VBI_CAPTURE:
699 static int get_ressource(struct cx8800_fh *fh)
702 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
703 return RESOURCE_VIDEO;
704 case V4L2_BUF_TYPE_VBI_CAPTURE:
712 static int video_open(struct inode *inode, struct file *file)
714 int minor = iminor(inode);
715 struct cx8800_dev *h,*dev = NULL;
716 struct cx88_core *core;
717 struct cx8800_fh *fh;
718 struct list_head *list;
719 enum v4l2_buf_type type = 0;
722 list_for_each(list,&cx8800_devlist) {
723 h = list_entry(list, struct cx8800_dev, devlist);
724 if (h->video_dev->minor == minor) {
726 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
728 if (h->vbi_dev->minor == minor) {
730 type = V4L2_BUF_TYPE_VBI_CAPTURE;
733 h->radio_dev->minor == minor) {
743 dprintk(1,"open minor=%d radio=%d type=%s\n",
744 minor,radio,v4l2_type_names[type]);
746 /* allocate + initialize per filehandle data */
747 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
750 file->private_data = fh;
756 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
758 videobuf_queue_init(&fh->vidq, &cx8800_video_qops,
759 dev->pci, &dev->slock,
760 V4L2_BUF_TYPE_VIDEO_CAPTURE,
761 V4L2_FIELD_INTERLACED,
762 sizeof(struct cx88_buffer),
764 videobuf_queue_init(&fh->vbiq, &cx8800_vbi_qops,
765 dev->pci, &dev->slock,
766 V4L2_BUF_TYPE_VBI_CAPTURE,
768 sizeof(struct cx88_buffer),
772 int board = core->board;
773 dprintk(1,"video_open: setting radio device\n");
774 cx_write(MO_GP3_IO, cx88_boards[board].radio.gpio3);
775 cx_write(MO_GP0_IO, cx88_boards[board].radio.gpio0);
776 cx_write(MO_GP1_IO, cx88_boards[board].radio.gpio1);
777 cx_write(MO_GP2_IO, cx88_boards[board].radio.gpio2);
778 core->tvaudio = WW_FM;
779 cx88_set_tvaudio(core);
780 cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
781 cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
788 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
790 struct cx8800_fh *fh = file->private_data;
793 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
794 if (res_locked(fh->dev,RESOURCE_VIDEO))
796 return videobuf_read_one(&fh->vidq, data, count, ppos,
797 file->f_flags & O_NONBLOCK);
798 case V4L2_BUF_TYPE_VBI_CAPTURE:
799 if (!res_get(fh->dev,fh,RESOURCE_VBI))
801 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
802 file->f_flags & O_NONBLOCK);
810 video_poll(struct file *file, struct poll_table_struct *wait)
812 struct cx8800_fh *fh = file->private_data;
813 struct cx88_buffer *buf;
815 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
816 if (!res_get(fh->dev,fh,RESOURCE_VBI))
818 return videobuf_poll_stream(file, &fh->vbiq, wait);
821 if (res_check(fh,RESOURCE_VIDEO)) {
822 /* streaming capture */
823 if (list_empty(&fh->vidq.stream))
825 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
828 buf = (struct cx88_buffer*)fh->vidq.read_buf;
832 poll_wait(file, &buf->vb.done, wait);
833 if (buf->vb.state == STATE_DONE ||
834 buf->vb.state == STATE_ERROR)
835 return POLLIN|POLLRDNORM;
839 static int video_release(struct inode *inode, struct file *file)
841 struct cx8800_fh *fh = file->private_data;
842 struct cx8800_dev *dev = fh->dev;
844 /* turn off overlay */
845 if (res_check(fh, RESOURCE_OVERLAY)) {
847 res_free(dev,fh,RESOURCE_OVERLAY);
850 /* stop video capture */
851 if (res_check(fh, RESOURCE_VIDEO)) {
852 videobuf_queue_cancel(&fh->vidq);
853 res_free(dev,fh,RESOURCE_VIDEO);
855 if (fh->vidq.read_buf) {
856 buffer_release(&fh->vidq,fh->vidq.read_buf);
857 kfree(fh->vidq.read_buf);
860 /* stop vbi capture */
861 if (res_check(fh, RESOURCE_VBI)) {
862 if (fh->vbiq.streaming)
863 videobuf_streamoff(&fh->vbiq);
864 if (fh->vbiq.reading)
865 videobuf_read_stop(&fh->vbiq);
866 res_free(dev,fh,RESOURCE_VBI);
869 videobuf_mmap_free(&fh->vidq);
870 videobuf_mmap_free(&fh->vbiq);
871 file->private_data = NULL;
874 cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
880 video_mmap(struct file *file, struct vm_area_struct * vma)
882 struct cx8800_fh *fh = file->private_data;
884 return videobuf_mmap_mapper(get_queue(fh), vma);
887 /* ------------------------------------------------------------------ */
888 /* VIDEO CTRL IOCTLS */
890 int cx88_get_control (struct cx88_core *core, struct v4l2_control *ctl)
892 struct cx88_ctrl *c = NULL;
896 for (i = 0; i < CX8800_CTLS; i++)
897 if (cx8800_ctls[i].v.id == ctl->id)
899 if (unlikely(NULL == c))
902 value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
904 case V4L2_CID_AUDIO_BALANCE:
905 ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
906 : (0x7f - (value & 0x7f));
908 case V4L2_CID_AUDIO_VOLUME:
909 ctl->value = 0x3f - (value & 0x3f);
912 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
915 dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
916 ctl->id, c->v.name, ctl->value, c->reg,
917 value,c->mask, c->sreg ? " [shadowed]" : "");
920 EXPORT_SYMBOL(cx88_get_control);
922 int cx88_set_control(struct cx88_core *core, struct v4l2_control *ctl)
924 struct cx88_ctrl *c = NULL;
928 for (i = 0; i < CX8800_CTLS; i++) {
929 if (cx8800_ctls[i].v.id == ctl->id) {
933 if (unlikely(NULL == c))
936 if (ctl->value < c->v.minimum)
937 ctl->value = c->v.minimum;
938 if (ctl->value > c->v.maximum)
939 ctl->value = c->v.maximum;
942 case V4L2_CID_AUDIO_BALANCE:
943 value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
945 case V4L2_CID_AUDIO_VOLUME:
946 value = 0x3f - (ctl->value & 0x3f);
948 case V4L2_CID_SATURATION:
949 /* special v_sat handling */
951 value = ((ctl->value - c->off) << c->shift) & c->mask;
953 if (core->tvnorm & V4L2_STD_SECAM) {
954 /* For SECAM, both U and V sat should be equal */
955 value=value<<8|value;
957 /* Keeps U Saturation proportional to V Sat */
958 value=(value*0x5a)/0x7f<<8|value;
963 value = ((ctl->value - c->off) << c->shift) & c->mask;
966 dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
967 ctl->id, c->v.name, ctl->value, c->reg, value,
968 mask, c->sreg ? " [shadowed]" : "");
970 cx_sandor(c->sreg, c->reg, mask, value);
972 cx_andor(c->reg, mask, value);
976 EXPORT_SYMBOL(cx88_set_control);
978 static void init_controls(struct cx88_core *core)
980 struct v4l2_control ctrl;
983 for (i = 0; i < CX8800_CTLS; i++) {
984 ctrl.id=cx8800_ctls[i].v.id;
985 ctrl.value=cx8800_ctls[i].v.default_value;
987 cx88_set_control(core, &ctrl);
991 /* ------------------------------------------------------------------ */
994 static int vidioc_g_fmt_cap (struct file *file, void *priv,
995 struct v4l2_format *f)
997 struct cx8800_fh *fh = priv;
999 f->fmt.pix.width = fh->width;
1000 f->fmt.pix.height = fh->height;
1001 f->fmt.pix.field = fh->vidq.field;
1002 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1003 f->fmt.pix.bytesperline =
1004 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1005 f->fmt.pix.sizeimage =
1006 f->fmt.pix.height * f->fmt.pix.bytesperline;
1010 static int vidioc_try_fmt_cap (struct file *file, void *priv,
1011 struct v4l2_format *f)
1013 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1014 struct cx8800_fmt *fmt;
1015 enum v4l2_field field;
1016 unsigned int maxw, maxh;
1018 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1022 field = f->fmt.pix.field;
1023 maxw = norm_maxw(core->tvnorm);
1024 maxh = norm_maxh(core->tvnorm);
1026 if (V4L2_FIELD_ANY == field) {
1027 field = (f->fmt.pix.height > maxh/2)
1028 ? V4L2_FIELD_INTERLACED
1029 : V4L2_FIELD_BOTTOM;
1033 case V4L2_FIELD_TOP:
1034 case V4L2_FIELD_BOTTOM:
1037 case V4L2_FIELD_INTERLACED:
1043 f->fmt.pix.field = field;
1044 if (f->fmt.pix.height < 32)
1045 f->fmt.pix.height = 32;
1046 if (f->fmt.pix.height > maxh)
1047 f->fmt.pix.height = maxh;
1048 if (f->fmt.pix.width < 48)
1049 f->fmt.pix.width = 48;
1050 if (f->fmt.pix.width > maxw)
1051 f->fmt.pix.width = maxw;
1052 f->fmt.pix.width &= ~0x03;
1053 f->fmt.pix.bytesperline =
1054 (f->fmt.pix.width * fmt->depth) >> 3;
1055 f->fmt.pix.sizeimage =
1056 f->fmt.pix.height * f->fmt.pix.bytesperline;
1061 static int vidioc_s_fmt_cap (struct file *file, void *priv,
1062 struct v4l2_format *f)
1064 struct cx8800_fh *fh = priv;
1065 int err = vidioc_try_fmt_cap (file,priv,f);
1069 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1070 fh->width = f->fmt.pix.width;
1071 fh->height = f->fmt.pix.height;
1072 fh->vidq.field = f->fmt.pix.field;
1076 static int vidioc_querycap (struct file *file, void *priv,
1077 struct v4l2_capability *cap)
1079 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
1080 struct cx88_core *core = dev->core;
1082 strcpy(cap->driver, "cx8800");
1083 strlcpy(cap->card, cx88_boards[core->board].name,
1085 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1086 cap->version = CX88_VERSION_CODE;
1088 V4L2_CAP_VIDEO_CAPTURE |
1089 V4L2_CAP_READWRITE |
1090 V4L2_CAP_STREAMING |
1091 V4L2_CAP_VBI_CAPTURE;
1092 if (UNSET != core->tuner_type)
1093 cap->capabilities |= V4L2_CAP_TUNER;
1097 static int vidioc_enum_fmt_cap (struct file *file, void *priv,
1098 struct v4l2_fmtdesc *f)
1100 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1103 strlcpy(f->description,formats[f->index].name,sizeof(f->description));
1104 f->pixelformat = formats[f->index].fourcc;
1109 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1110 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
1112 struct cx8800_fh *fh = priv;
1113 struct videobuf_queue *q;
1114 struct v4l2_requestbuffers req;
1119 memset(&req,0,sizeof(req));
1122 req.memory = V4L2_MEMORY_MMAP;
1123 err = videobuf_reqbufs(q,&req);
1127 mbuf->frames = req.count;
1129 for (i = 0; i < mbuf->frames; i++) {
1130 mbuf->offsets[i] = q->bufs[i]->boff;
1131 mbuf->size += q->bufs[i]->bsize;
1137 static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
1139 struct cx8800_fh *fh = priv;
1140 return (videobuf_reqbufs(get_queue(fh), p));
1143 static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
1145 struct cx8800_fh *fh = priv;
1146 return (videobuf_querybuf(get_queue(fh), p));
1149 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1151 struct cx8800_fh *fh = priv;
1152 return (videobuf_qbuf(get_queue(fh), p));
1155 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1157 struct cx8800_fh *fh = priv;
1158 return (videobuf_dqbuf(get_queue(fh), p,
1159 file->f_flags & O_NONBLOCK));
1162 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1164 struct cx8800_fh *fh = priv;
1165 struct cx8800_dev *dev = fh->dev;
1167 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1169 if (unlikely(i != fh->type))
1172 if (unlikely(!res_get(dev,fh,get_ressource(fh))))
1174 return videobuf_streamon(get_queue(fh));
1177 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1179 struct cx8800_fh *fh = priv;
1180 struct cx8800_dev *dev = fh->dev;
1183 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1188 res = get_ressource(fh);
1189 err = videobuf_streamoff(get_queue(fh));
1192 res_free(dev,fh,res);
1196 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
1198 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1200 mutex_lock(&core->lock);
1201 cx88_set_tvnorm(core,*tvnorms);
1202 mutex_unlock(&core->lock);
1207 /* only one input in this sample driver */
1208 int cx88_enum_input (struct cx88_core *core,struct v4l2_input *i)
1210 static const char *iname[] = {
1211 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1212 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1213 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1214 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1215 [ CX88_VMUX_SVIDEO ] = "S-Video",
1216 [ CX88_VMUX_TELEVISION ] = "Television",
1217 [ CX88_VMUX_CABLE ] = "Cable TV",
1218 [ CX88_VMUX_DVB ] = "DVB",
1219 [ CX88_VMUX_DEBUG ] = "for debug only",
1226 if (0 == INPUT(n)->type)
1228 memset(i,0,sizeof(*i));
1230 i->type = V4L2_INPUT_TYPE_CAMERA;
1231 strcpy(i->name,iname[INPUT(n)->type]);
1232 if ((CX88_VMUX_TELEVISION == INPUT(n)->type) ||
1233 (CX88_VMUX_CABLE == INPUT(n)->type))
1234 i->type = V4L2_INPUT_TYPE_TUNER;
1235 i->std = CX88_NORMS;
1238 EXPORT_SYMBOL(cx88_enum_input);
1240 static int vidioc_enum_input (struct file *file, void *priv,
1241 struct v4l2_input *i)
1243 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1244 return cx88_enum_input (core,i);
1247 static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1249 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1255 static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1257 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1262 mutex_lock(&core->lock);
1263 cx88_newstation(core);
1264 cx88_video_mux(core,i);
1265 mutex_unlock(&core->lock);
1271 static int vidioc_queryctrl (struct file *file, void *priv,
1272 struct v4l2_queryctrl *qctrl)
1274 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1275 if (unlikely(qctrl->id == 0))
1277 return cx8800_ctrl_query(qctrl);
1280 static int vidioc_g_ctrl (struct file *file, void *priv,
1281 struct v4l2_control *ctl)
1283 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1285 cx88_get_control(core,ctl);
1288 static int vidioc_s_ctrl (struct file *file, void *priv,
1289 struct v4l2_control *ctl)
1291 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1293 cx88_set_control(core,ctl);
1296 static int vidioc_g_tuner (struct file *file, void *priv,
1297 struct v4l2_tuner *t)
1299 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1302 if (unlikely(UNSET == core->tuner_type))
1307 strcpy(t->name, "Television");
1308 t->type = V4L2_TUNER_ANALOG_TV;
1309 t->capability = V4L2_TUNER_CAP_NORM;
1310 t->rangehigh = 0xffffffffUL;
1312 cx88_get_stereo(core ,t);
1313 reg = cx_read(MO_DEVICE_STATUS);
1314 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1318 static int vidioc_s_tuner (struct file *file, void *priv,
1319 struct v4l2_tuner *t)
1321 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1323 if (UNSET == core->tuner_type)
1328 cx88_set_stereo(core, t->audmode, 1);
1332 static int vidioc_g_frequency (struct file *file, void *priv,
1333 struct v4l2_frequency *f)
1335 struct cx8800_fh *fh = priv;
1336 struct cx88_core *core = fh->dev->core;
1338 if (unlikely(UNSET == core->tuner_type))
1341 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1342 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1343 f->frequency = core->freq;
1345 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1350 int cx88_set_freq (struct cx88_core *core,
1351 struct v4l2_frequency *f)
1353 if (unlikely(UNSET == core->tuner_type))
1355 if (unlikely(f->tuner != 0))
1358 mutex_lock(&core->lock);
1359 core->freq = f->frequency;
1360 cx88_newstation(core);
1361 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1363 /* When changing channels it is required to reset TVAUDIO */
1365 cx88_set_tvaudio(core);
1367 mutex_unlock(&core->lock);
1371 EXPORT_SYMBOL(cx88_set_freq);
1373 static int vidioc_s_frequency (struct file *file, void *priv,
1374 struct v4l2_frequency *f)
1376 struct cx8800_fh *fh = priv;
1377 struct cx88_core *core = fh->dev->core;
1379 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1381 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1385 cx88_set_freq (core,f);
1389 /* ----------------------------------------------------------- */
1390 /* RADIO ESPECIFIC IOCTLS */
1391 /* ----------------------------------------------------------- */
1393 static int radio_querycap (struct file *file, void *priv,
1394 struct v4l2_capability *cap)
1396 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
1397 struct cx88_core *core = dev->core;
1399 strcpy(cap->driver, "cx8800");
1400 strlcpy(cap->card, cx88_boards[core->board].name,
1402 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1403 cap->version = CX88_VERSION_CODE;
1404 cap->capabilities = V4L2_CAP_TUNER;
1408 static int radio_g_tuner (struct file *file, void *priv,
1409 struct v4l2_tuner *t)
1411 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1413 if (unlikely(t->index > 0))
1416 strcpy(t->name, "Radio");
1417 t->type = V4L2_TUNER_RADIO;
1419 cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
1423 static int radio_enum_input (struct file *file, void *priv,
1424 struct v4l2_input *i)
1428 strcpy(i->name,"Radio");
1429 i->type = V4L2_INPUT_TYPE_TUNER;
1434 static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
1436 if (unlikely(a->index))
1439 memset(a,0,sizeof(*a));
1440 strcpy(a->name,"Radio");
1444 /* FIXME: Should add a standard for radio */
1446 static int radio_s_tuner (struct file *file, void *priv,
1447 struct v4l2_tuner *t)
1449 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1454 cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1459 static int radio_s_audio (struct file *file, void *fh,
1460 struct v4l2_audio *a)
1465 static int radio_s_input (struct file *file, void *fh, unsigned int i)
1470 static int radio_queryctrl (struct file *file, void *priv,
1471 struct v4l2_queryctrl *c)
1475 if (c->id < V4L2_CID_BASE ||
1476 c->id >= V4L2_CID_LASTP1)
1478 if (c->id == V4L2_CID_AUDIO_MUTE) {
1479 for (i = 0; i < CX8800_CTLS; i++)
1480 if (cx8800_ctls[i].v.id == c->id)
1482 *c = cx8800_ctls[i].v;
1488 /* ----------------------------------------------------------- */
1490 static void cx8800_vid_timeout(unsigned long data)
1492 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1493 struct cx88_core *core = dev->core;
1494 struct cx88_dmaqueue *q = &dev->vidq;
1495 struct cx88_buffer *buf;
1496 unsigned long flags;
1498 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1500 cx_clear(MO_VID_DMACNTRL, 0x11);
1501 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1503 spin_lock_irqsave(&dev->slock,flags);
1504 while (!list_empty(&q->active)) {
1505 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1506 list_del(&buf->vb.queue);
1507 buf->vb.state = STATE_ERROR;
1508 wake_up(&buf->vb.done);
1509 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1510 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1512 restart_video_queue(dev,q);
1513 spin_unlock_irqrestore(&dev->slock,flags);
1516 static char *cx88_vid_irqs[32] = {
1517 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1518 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1519 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1520 "y_sync", "u_sync", "v_sync", "vbi_sync",
1521 "opc_err", "par_err", "rip_err", "pci_abort",
1524 static void cx8800_vid_irq(struct cx8800_dev *dev)
1526 struct cx88_core *core = dev->core;
1527 u32 status, mask, count;
1529 status = cx_read(MO_VID_INTSTAT);
1530 mask = cx_read(MO_VID_INTMSK);
1531 if (0 == (status & mask))
1533 cx_write(MO_VID_INTSTAT, status);
1534 if (irq_debug || (status & mask & ~0xff))
1535 cx88_print_irqbits(core->name, "irq vid",
1536 cx88_vid_irqs, status, mask);
1538 /* risc op code error */
1539 if (status & (1 << 16)) {
1540 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1541 cx_clear(MO_VID_DMACNTRL, 0x11);
1542 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1543 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1547 if (status & 0x01) {
1548 spin_lock(&dev->slock);
1549 count = cx_read(MO_VIDY_GPCNT);
1550 cx88_wakeup(core, &dev->vidq, count);
1551 spin_unlock(&dev->slock);
1555 if (status & 0x08) {
1556 spin_lock(&dev->slock);
1557 count = cx_read(MO_VBI_GPCNT);
1558 cx88_wakeup(core, &dev->vbiq, count);
1559 spin_unlock(&dev->slock);
1563 if (status & 0x10) {
1564 dprintk(2,"stopper video\n");
1565 spin_lock(&dev->slock);
1566 restart_video_queue(dev,&dev->vidq);
1567 spin_unlock(&dev->slock);
1571 if (status & 0x80) {
1572 dprintk(2,"stopper vbi\n");
1573 spin_lock(&dev->slock);
1574 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1575 spin_unlock(&dev->slock);
1579 static irqreturn_t cx8800_irq(int irq, void *dev_id)
1581 struct cx8800_dev *dev = dev_id;
1582 struct cx88_core *core = dev->core;
1584 int loop, handled = 0;
1586 for (loop = 0; loop < 10; loop++) {
1587 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
1590 cx_write(MO_PCI_INTSTAT, status);
1593 if (status & core->pci_irqmask)
1594 cx88_core_irq(core,status);
1596 cx8800_vid_irq(dev);
1599 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1601 cx_write(MO_PCI_INTMSK,0);
1605 return IRQ_RETVAL(handled);
1608 /* ----------------------------------------------------------- */
1609 /* exported stuff */
1611 static const struct file_operations video_fops =
1613 .owner = THIS_MODULE,
1615 .release = video_release,
1619 .ioctl = video_ioctl2,
1620 .compat_ioctl = v4l_compat_ioctl32,
1621 .llseek = no_llseek,
1624 static struct video_device cx8800_vbi_template;
1625 static struct video_device cx8800_video_template =
1627 .name = "cx8800-video",
1628 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1629 .fops = &video_fops,
1631 .vidioc_querycap = vidioc_querycap,
1632 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1633 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1634 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1635 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1636 .vidioc_g_fmt_vbi = cx8800_vbi_fmt,
1637 .vidioc_try_fmt_vbi = cx8800_vbi_fmt,
1638 .vidioc_s_fmt_vbi = cx8800_vbi_fmt,
1639 .vidioc_reqbufs = vidioc_reqbufs,
1640 .vidioc_querybuf = vidioc_querybuf,
1641 .vidioc_qbuf = vidioc_qbuf,
1642 .vidioc_dqbuf = vidioc_dqbuf,
1643 .vidioc_s_std = vidioc_s_std,
1644 .vidioc_enum_input = vidioc_enum_input,
1645 .vidioc_g_input = vidioc_g_input,
1646 .vidioc_s_input = vidioc_s_input,
1647 .vidioc_queryctrl = vidioc_queryctrl,
1648 .vidioc_g_ctrl = vidioc_g_ctrl,
1649 .vidioc_s_ctrl = vidioc_s_ctrl,
1650 .vidioc_streamon = vidioc_streamon,
1651 .vidioc_streamoff = vidioc_streamoff,
1652 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1653 .vidiocgmbuf = vidiocgmbuf,
1655 .vidioc_g_tuner = vidioc_g_tuner,
1656 .vidioc_s_tuner = vidioc_s_tuner,
1657 .vidioc_g_frequency = vidioc_g_frequency,
1658 .vidioc_s_frequency = vidioc_s_frequency,
1659 .tvnorms = CX88_NORMS,
1660 .current_norm = V4L2_STD_PAL_BG,
1663 static const struct file_operations radio_fops =
1665 .owner = THIS_MODULE,
1667 .release = video_release,
1668 .ioctl = video_ioctl2,
1669 .compat_ioctl = v4l_compat_ioctl32,
1670 .llseek = no_llseek,
1673 static struct video_device cx8800_radio_template =
1675 .name = "cx8800-radio",
1676 .type = VID_TYPE_TUNER,
1678 .fops = &radio_fops,
1680 .vidioc_querycap = radio_querycap,
1681 .vidioc_g_tuner = radio_g_tuner,
1682 .vidioc_enum_input = radio_enum_input,
1683 .vidioc_g_audio = radio_g_audio,
1684 .vidioc_s_tuner = radio_s_tuner,
1685 .vidioc_s_audio = radio_s_audio,
1686 .vidioc_s_input = radio_s_input,
1687 .vidioc_queryctrl = radio_queryctrl,
1688 .vidioc_g_ctrl = vidioc_g_ctrl,
1689 .vidioc_s_ctrl = vidioc_s_ctrl,
1690 .vidioc_g_frequency = vidioc_g_frequency,
1691 .vidioc_s_frequency = vidioc_s_frequency,
1694 /* ----------------------------------------------------------- */
1696 static void cx8800_unregister_video(struct cx8800_dev *dev)
1698 if (dev->radio_dev) {
1699 if (-1 != dev->radio_dev->minor)
1700 video_unregister_device(dev->radio_dev);
1702 video_device_release(dev->radio_dev);
1703 dev->radio_dev = NULL;
1706 if (-1 != dev->vbi_dev->minor)
1707 video_unregister_device(dev->vbi_dev);
1709 video_device_release(dev->vbi_dev);
1710 dev->vbi_dev = NULL;
1712 if (dev->video_dev) {
1713 if (-1 != dev->video_dev->minor)
1714 video_unregister_device(dev->video_dev);
1716 video_device_release(dev->video_dev);
1717 dev->video_dev = NULL;
1721 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1722 const struct pci_device_id *pci_id)
1724 struct cx8800_dev *dev;
1725 struct cx88_core *core;
1729 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1735 if (pci_enable_device(pci_dev)) {
1739 core = cx88_core_get(dev->pci);
1746 /* print pci info */
1747 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
1748 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1749 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1750 "latency: %d, mmio: 0x%llx\n", core->name,
1751 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1752 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
1754 pci_set_master(pci_dev);
1755 if (!pci_dma_supported(pci_dev,0xffffffff)) {
1756 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1761 /* Initialize VBI template */
1762 memcpy( &cx8800_vbi_template, &cx8800_video_template,
1763 sizeof(cx8800_vbi_template) );
1764 strcpy(cx8800_vbi_template.name,"cx8800-vbi");
1765 cx8800_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
1767 /* initialize driver struct */
1768 spin_lock_init(&dev->slock);
1769 core->tvnorm = cx8800_video_template.current_norm;
1771 /* init video dma queues */
1772 INIT_LIST_HEAD(&dev->vidq.active);
1773 INIT_LIST_HEAD(&dev->vidq.queued);
1774 dev->vidq.timeout.function = cx8800_vid_timeout;
1775 dev->vidq.timeout.data = (unsigned long)dev;
1776 init_timer(&dev->vidq.timeout);
1777 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1778 MO_VID_DMACNTRL,0x11,0x00);
1780 /* init vbi dma queues */
1781 INIT_LIST_HEAD(&dev->vbiq.active);
1782 INIT_LIST_HEAD(&dev->vbiq.queued);
1783 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1784 dev->vbiq.timeout.data = (unsigned long)dev;
1785 init_timer(&dev->vbiq.timeout);
1786 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1787 MO_VID_DMACNTRL,0x88,0x00);
1790 err = request_irq(pci_dev->irq, cx8800_irq,
1791 IRQF_SHARED | IRQF_DISABLED, core->name, dev);
1793 printk(KERN_ERR "%s: can't get IRQ %d\n",
1794 core->name,pci_dev->irq);
1797 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1799 /* load and configure helper modules */
1800 if (TUNER_ABSENT != core->tuner_type)
1801 request_module("tuner");
1803 if (cx88_boards[ core->board ].audio_chip == AUDIO_CHIP_WM8775)
1804 request_module("wm8775");
1806 /* register v4l devices */
1807 dev->video_dev = cx88_vdev_init(core,dev->pci,
1808 &cx8800_video_template,"video");
1809 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1810 video_nr[core->nr]);
1812 printk(KERN_INFO "%s: can't register video device\n",
1816 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1817 core->name,dev->video_dev->minor & 0x1f);
1819 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1820 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1823 printk(KERN_INFO "%s/0: can't register vbi device\n",
1827 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1828 core->name,dev->vbi_dev->minor & 0x1f);
1830 if (core->has_radio) {
1831 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1832 &cx8800_radio_template,"radio");
1833 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1834 radio_nr[core->nr]);
1836 printk(KERN_INFO "%s/0: can't register radio device\n",
1840 printk(KERN_INFO "%s/0: registered device radio%d\n",
1841 core->name,dev->radio_dev->minor & 0x1f);
1844 /* everything worked */
1845 list_add_tail(&dev->devlist,&cx8800_devlist);
1846 pci_set_drvdata(pci_dev,dev);
1848 /* initial device configuration */
1849 mutex_lock(&core->lock);
1850 cx88_set_tvnorm(core,core->tvnorm);
1851 init_controls(core);
1852 cx88_video_mux(core,0);
1853 mutex_unlock(&core->lock);
1855 /* start tvaudio thread */
1856 if (core->tuner_type != TUNER_ABSENT)
1857 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
1861 cx8800_unregister_video(dev);
1862 free_irq(pci_dev->irq, dev);
1864 cx88_core_put(core,dev->pci);
1870 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1872 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1873 struct cx88_core *core = dev->core;
1876 if (core->kthread) {
1877 kthread_stop(core->kthread);
1878 core->kthread = NULL;
1881 cx88_shutdown(core); /* FIXME */
1882 pci_disable_device(pci_dev);
1884 /* unregister stuff */
1886 free_irq(pci_dev->irq, dev);
1887 cx8800_unregister_video(dev);
1888 pci_set_drvdata(pci_dev, NULL);
1891 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1892 list_del(&dev->devlist);
1893 cx88_core_put(core,dev->pci);
1898 static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1900 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1901 struct cx88_core *core = dev->core;
1903 /* stop video+vbi capture */
1904 spin_lock(&dev->slock);
1905 if (!list_empty(&dev->vidq.active)) {
1906 printk("%s: suspend video\n", core->name);
1907 stop_video_dma(dev);
1908 del_timer(&dev->vidq.timeout);
1910 if (!list_empty(&dev->vbiq.active)) {
1911 printk("%s: suspend vbi\n", core->name);
1912 cx8800_stop_vbi_dma(dev);
1913 del_timer(&dev->vbiq.timeout);
1915 spin_unlock(&dev->slock);
1917 /* FIXME -- shutdown device */
1918 cx88_shutdown(core);
1920 pci_save_state(pci_dev);
1921 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1922 pci_disable_device(pci_dev);
1923 dev->state.disabled = 1;
1928 static int cx8800_resume(struct pci_dev *pci_dev)
1930 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1931 struct cx88_core *core = dev->core;
1934 if (dev->state.disabled) {
1935 err=pci_enable_device(pci_dev);
1937 printk(KERN_ERR "%s: can't enable device\n",
1942 dev->state.disabled = 0;
1944 err= pci_set_power_state(pci_dev, PCI_D0);
1946 printk(KERN_ERR "%s: can't enable device\n",
1949 pci_disable_device(pci_dev);
1950 dev->state.disabled = 1;
1954 pci_restore_state(pci_dev);
1956 /* FIXME: re-initialize hardware */
1959 /* restart video+vbi capture */
1960 spin_lock(&dev->slock);
1961 if (!list_empty(&dev->vidq.active)) {
1962 printk("%s: resume video\n", core->name);
1963 restart_video_queue(dev,&dev->vidq);
1965 if (!list_empty(&dev->vbiq.active)) {
1966 printk("%s: resume vbi\n", core->name);
1967 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1969 spin_unlock(&dev->slock);
1975 /* ----------------------------------------------------------- */
1977 static struct pci_device_id cx8800_pci_tbl[] = {
1981 .subvendor = PCI_ANY_ID,
1982 .subdevice = PCI_ANY_ID,
1984 /* --- end of list --- */
1987 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
1989 static struct pci_driver cx8800_pci_driver = {
1991 .id_table = cx8800_pci_tbl,
1992 .probe = cx8800_initdev,
1993 .remove = __devexit_p(cx8800_finidev),
1995 .suspend = cx8800_suspend,
1996 .resume = cx8800_resume,
2000 static int cx8800_init(void)
2002 printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
2003 (CX88_VERSION_CODE >> 16) & 0xff,
2004 (CX88_VERSION_CODE >> 8) & 0xff,
2005 CX88_VERSION_CODE & 0xff);
2007 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2008 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2010 return pci_register_driver(&cx8800_pci_driver);
2013 static void cx8800_fini(void)
2015 pci_unregister_driver(&cx8800_pci_driver);
2018 module_init(cx8800_init);
2019 module_exit(cx8800_fini);
2021 /* ----------------------------------------------------------- */
2026 * 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