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))
1305 strcpy(t->name, "Television");
1306 t->type = V4L2_TUNER_ANALOG_TV;
1307 t->capability = V4L2_TUNER_CAP_NORM;
1308 t->rangehigh = 0xffffffffUL;
1310 cx88_get_stereo(core ,t);
1311 reg = cx_read(MO_DEVICE_STATUS);
1312 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1316 static int vidioc_s_tuner (struct file *file, void *priv,
1317 struct v4l2_tuner *t)
1319 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1321 if (UNSET == core->tuner_type)
1326 cx88_set_stereo(core, t->audmode, 1);
1330 static int vidioc_g_frequency (struct file *file, void *priv,
1331 struct v4l2_frequency *f)
1333 struct cx8800_fh *fh = priv;
1334 struct cx88_core *core = fh->dev->core;
1336 if (unlikely(UNSET == core->tuner_type))
1339 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1340 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1341 f->frequency = core->freq;
1343 cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1348 int cx88_set_freq (struct cx88_core *core,
1349 struct v4l2_frequency *f)
1351 if (unlikely(UNSET == core->tuner_type))
1353 if (unlikely(f->tuner != 0))
1356 mutex_lock(&core->lock);
1357 core->freq = f->frequency;
1358 cx88_newstation(core);
1359 cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1361 /* When changing channels it is required to reset TVAUDIO */
1363 cx88_set_tvaudio(core);
1365 mutex_unlock(&core->lock);
1369 EXPORT_SYMBOL(cx88_set_freq);
1371 static int vidioc_s_frequency (struct file *file, void *priv,
1372 struct v4l2_frequency *f)
1374 struct cx8800_fh *fh = priv;
1375 struct cx88_core *core = fh->dev->core;
1377 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1379 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1383 cx88_set_freq (core,f);
1387 /* ----------------------------------------------------------- */
1388 /* RADIO ESPECIFIC IOCTLS */
1389 /* ----------------------------------------------------------- */
1391 static int radio_querycap (struct file *file, void *priv,
1392 struct v4l2_capability *cap)
1394 struct cx8800_dev *dev = ((struct cx8800_fh *)priv)->dev;
1395 struct cx88_core *core = dev->core;
1397 strcpy(cap->driver, "cx8800");
1398 strlcpy(cap->card, cx88_boards[core->board].name,
1400 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1401 cap->version = CX88_VERSION_CODE;
1402 cap->capabilities = V4L2_CAP_TUNER;
1406 static int radio_g_tuner (struct file *file, void *priv,
1407 struct v4l2_tuner *t)
1409 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1411 if (unlikely(t->index > 0))
1414 strcpy(t->name, "Radio");
1415 t->type = V4L2_TUNER_RADIO;
1417 cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
1421 static int radio_enum_input (struct file *file, void *priv,
1422 struct v4l2_input *i)
1426 strcpy(i->name,"Radio");
1427 i->type = V4L2_INPUT_TYPE_TUNER;
1432 static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
1434 if (unlikely(a->index))
1437 memset(a,0,sizeof(*a));
1438 strcpy(a->name,"Radio");
1442 /* FIXME: Should add a standard for radio */
1444 static int radio_s_tuner (struct file *file, void *priv,
1445 struct v4l2_tuner *t)
1447 struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1452 cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1457 static int radio_s_audio (struct file *file, void *fh,
1458 struct v4l2_audio *a)
1463 static int radio_s_input (struct file *file, void *fh, unsigned int i)
1468 static int radio_queryctrl (struct file *file, void *priv,
1469 struct v4l2_queryctrl *c)
1473 if (c->id < V4L2_CID_BASE ||
1474 c->id >= V4L2_CID_LASTP1)
1476 if (c->id == V4L2_CID_AUDIO_MUTE) {
1477 for (i = 0; i < CX8800_CTLS; i++)
1478 if (cx8800_ctls[i].v.id == c->id)
1480 *c = cx8800_ctls[i].v;
1486 /* ----------------------------------------------------------- */
1488 static void cx8800_vid_timeout(unsigned long data)
1490 struct cx8800_dev *dev = (struct cx8800_dev*)data;
1491 struct cx88_core *core = dev->core;
1492 struct cx88_dmaqueue *q = &dev->vidq;
1493 struct cx88_buffer *buf;
1494 unsigned long flags;
1496 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1498 cx_clear(MO_VID_DMACNTRL, 0x11);
1499 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1501 spin_lock_irqsave(&dev->slock,flags);
1502 while (!list_empty(&q->active)) {
1503 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1504 list_del(&buf->vb.queue);
1505 buf->vb.state = STATE_ERROR;
1506 wake_up(&buf->vb.done);
1507 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1508 buf, buf->vb.i, (unsigned long)buf->risc.dma);
1510 restart_video_queue(dev,q);
1511 spin_unlock_irqrestore(&dev->slock,flags);
1514 static char *cx88_vid_irqs[32] = {
1515 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1516 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1517 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
1518 "y_sync", "u_sync", "v_sync", "vbi_sync",
1519 "opc_err", "par_err", "rip_err", "pci_abort",
1522 static void cx8800_vid_irq(struct cx8800_dev *dev)
1524 struct cx88_core *core = dev->core;
1525 u32 status, mask, count;
1527 status = cx_read(MO_VID_INTSTAT);
1528 mask = cx_read(MO_VID_INTMSK);
1529 if (0 == (status & mask))
1531 cx_write(MO_VID_INTSTAT, status);
1532 if (irq_debug || (status & mask & ~0xff))
1533 cx88_print_irqbits(core->name, "irq vid",
1534 cx88_vid_irqs, status, mask);
1536 /* risc op code error */
1537 if (status & (1 << 16)) {
1538 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1539 cx_clear(MO_VID_DMACNTRL, 0x11);
1540 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1541 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1545 if (status & 0x01) {
1546 spin_lock(&dev->slock);
1547 count = cx_read(MO_VIDY_GPCNT);
1548 cx88_wakeup(core, &dev->vidq, count);
1549 spin_unlock(&dev->slock);
1553 if (status & 0x08) {
1554 spin_lock(&dev->slock);
1555 count = cx_read(MO_VBI_GPCNT);
1556 cx88_wakeup(core, &dev->vbiq, count);
1557 spin_unlock(&dev->slock);
1561 if (status & 0x10) {
1562 dprintk(2,"stopper video\n");
1563 spin_lock(&dev->slock);
1564 restart_video_queue(dev,&dev->vidq);
1565 spin_unlock(&dev->slock);
1569 if (status & 0x80) {
1570 dprintk(2,"stopper vbi\n");
1571 spin_lock(&dev->slock);
1572 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1573 spin_unlock(&dev->slock);
1577 static irqreturn_t cx8800_irq(int irq, void *dev_id)
1579 struct cx8800_dev *dev = dev_id;
1580 struct cx88_core *core = dev->core;
1582 int loop, handled = 0;
1584 for (loop = 0; loop < 10; loop++) {
1585 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
1588 cx_write(MO_PCI_INTSTAT, status);
1591 if (status & core->pci_irqmask)
1592 cx88_core_irq(core,status);
1594 cx8800_vid_irq(dev);
1597 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1599 cx_write(MO_PCI_INTMSK,0);
1603 return IRQ_RETVAL(handled);
1606 /* ----------------------------------------------------------- */
1607 /* exported stuff */
1609 static const struct file_operations video_fops =
1611 .owner = THIS_MODULE,
1613 .release = video_release,
1617 .ioctl = video_ioctl2,
1618 .compat_ioctl = v4l_compat_ioctl32,
1619 .llseek = no_llseek,
1622 static struct video_device cx8800_vbi_template;
1623 static struct video_device cx8800_video_template =
1625 .name = "cx8800-video",
1626 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1627 .fops = &video_fops,
1629 .vidioc_querycap = vidioc_querycap,
1630 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1631 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1632 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1633 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1634 .vidioc_g_fmt_vbi = cx8800_vbi_fmt,
1635 .vidioc_try_fmt_vbi = cx8800_vbi_fmt,
1636 .vidioc_s_fmt_vbi = cx8800_vbi_fmt,
1637 .vidioc_reqbufs = vidioc_reqbufs,
1638 .vidioc_querybuf = vidioc_querybuf,
1639 .vidioc_qbuf = vidioc_qbuf,
1640 .vidioc_dqbuf = vidioc_dqbuf,
1641 .vidioc_s_std = vidioc_s_std,
1642 .vidioc_enum_input = vidioc_enum_input,
1643 .vidioc_g_input = vidioc_g_input,
1644 .vidioc_s_input = vidioc_s_input,
1645 .vidioc_queryctrl = vidioc_queryctrl,
1646 .vidioc_g_ctrl = vidioc_g_ctrl,
1647 .vidioc_s_ctrl = vidioc_s_ctrl,
1648 .vidioc_streamon = vidioc_streamon,
1649 .vidioc_streamoff = vidioc_streamoff,
1650 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1651 .vidiocgmbuf = vidiocgmbuf,
1653 .vidioc_g_tuner = vidioc_g_tuner,
1654 .vidioc_s_tuner = vidioc_s_tuner,
1655 .vidioc_g_frequency = vidioc_g_frequency,
1656 .vidioc_s_frequency = vidioc_s_frequency,
1657 .tvnorms = CX88_NORMS,
1658 .current_norm = V4L2_STD_PAL_BG,
1661 static const struct file_operations radio_fops =
1663 .owner = THIS_MODULE,
1665 .release = video_release,
1666 .ioctl = video_ioctl2,
1667 .compat_ioctl = v4l_compat_ioctl32,
1668 .llseek = no_llseek,
1671 static struct video_device cx8800_radio_template =
1673 .name = "cx8800-radio",
1674 .type = VID_TYPE_TUNER,
1676 .fops = &radio_fops,
1678 .vidioc_querycap = radio_querycap,
1679 .vidioc_g_tuner = radio_g_tuner,
1680 .vidioc_enum_input = radio_enum_input,
1681 .vidioc_g_audio = radio_g_audio,
1682 .vidioc_s_tuner = radio_s_tuner,
1683 .vidioc_s_audio = radio_s_audio,
1684 .vidioc_s_input = radio_s_input,
1685 .vidioc_queryctrl = radio_queryctrl,
1686 .vidioc_g_ctrl = vidioc_g_ctrl,
1687 .vidioc_s_ctrl = vidioc_s_ctrl,
1688 .vidioc_g_frequency = vidioc_g_frequency,
1689 .vidioc_s_frequency = vidioc_s_frequency,
1692 /* ----------------------------------------------------------- */
1694 static void cx8800_unregister_video(struct cx8800_dev *dev)
1696 if (dev->radio_dev) {
1697 if (-1 != dev->radio_dev->minor)
1698 video_unregister_device(dev->radio_dev);
1700 video_device_release(dev->radio_dev);
1701 dev->radio_dev = NULL;
1704 if (-1 != dev->vbi_dev->minor)
1705 video_unregister_device(dev->vbi_dev);
1707 video_device_release(dev->vbi_dev);
1708 dev->vbi_dev = NULL;
1710 if (dev->video_dev) {
1711 if (-1 != dev->video_dev->minor)
1712 video_unregister_device(dev->video_dev);
1714 video_device_release(dev->video_dev);
1715 dev->video_dev = NULL;
1719 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1720 const struct pci_device_id *pci_id)
1722 struct cx8800_dev *dev;
1723 struct cx88_core *core;
1727 dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1733 if (pci_enable_device(pci_dev)) {
1737 core = cx88_core_get(dev->pci);
1744 /* print pci info */
1745 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
1746 pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &dev->pci_lat);
1747 printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1748 "latency: %d, mmio: 0x%llx\n", core->name,
1749 pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1750 dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
1752 pci_set_master(pci_dev);
1753 if (!pci_dma_supported(pci_dev,0xffffffff)) {
1754 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1759 /* Initialize VBI template */
1760 memcpy( &cx8800_vbi_template, &cx8800_video_template,
1761 sizeof(cx8800_vbi_template) );
1762 strcpy(cx8800_vbi_template.name,"cx8800-vbi");
1763 cx8800_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
1765 /* initialize driver struct */
1766 spin_lock_init(&dev->slock);
1767 core->tvnorm = cx8800_video_template.current_norm;
1769 /* init video dma queues */
1770 INIT_LIST_HEAD(&dev->vidq.active);
1771 INIT_LIST_HEAD(&dev->vidq.queued);
1772 dev->vidq.timeout.function = cx8800_vid_timeout;
1773 dev->vidq.timeout.data = (unsigned long)dev;
1774 init_timer(&dev->vidq.timeout);
1775 cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1776 MO_VID_DMACNTRL,0x11,0x00);
1778 /* init vbi dma queues */
1779 INIT_LIST_HEAD(&dev->vbiq.active);
1780 INIT_LIST_HEAD(&dev->vbiq.queued);
1781 dev->vbiq.timeout.function = cx8800_vbi_timeout;
1782 dev->vbiq.timeout.data = (unsigned long)dev;
1783 init_timer(&dev->vbiq.timeout);
1784 cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1785 MO_VID_DMACNTRL,0x88,0x00);
1788 err = request_irq(pci_dev->irq, cx8800_irq,
1789 IRQF_SHARED | IRQF_DISABLED, core->name, dev);
1791 printk(KERN_ERR "%s: can't get IRQ %d\n",
1792 core->name,pci_dev->irq);
1795 cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1797 /* load and configure helper modules */
1798 if (TUNER_ABSENT != core->tuner_type)
1799 request_module("tuner");
1801 if (cx88_boards[ core->board ].audio_chip == AUDIO_CHIP_WM8775)
1802 request_module("wm8775");
1804 /* register v4l devices */
1805 dev->video_dev = cx88_vdev_init(core,dev->pci,
1806 &cx8800_video_template,"video");
1807 err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1808 video_nr[core->nr]);
1810 printk(KERN_INFO "%s: can't register video device\n",
1814 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1815 core->name,dev->video_dev->minor & 0x1f);
1817 dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1818 err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1821 printk(KERN_INFO "%s/0: can't register vbi device\n",
1825 printk(KERN_INFO "%s/0: registered device vbi%d\n",
1826 core->name,dev->vbi_dev->minor & 0x1f);
1828 if (core->has_radio) {
1829 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1830 &cx8800_radio_template,"radio");
1831 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1832 radio_nr[core->nr]);
1834 printk(KERN_INFO "%s/0: can't register radio device\n",
1838 printk(KERN_INFO "%s/0: registered device radio%d\n",
1839 core->name,dev->radio_dev->minor & 0x1f);
1842 /* everything worked */
1843 list_add_tail(&dev->devlist,&cx8800_devlist);
1844 pci_set_drvdata(pci_dev,dev);
1846 /* initial device configuration */
1847 mutex_lock(&core->lock);
1848 cx88_set_tvnorm(core,core->tvnorm);
1849 init_controls(core);
1850 cx88_video_mux(core,0);
1851 mutex_unlock(&core->lock);
1853 /* start tvaudio thread */
1854 if (core->tuner_type != TUNER_ABSENT)
1855 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
1859 cx8800_unregister_video(dev);
1860 free_irq(pci_dev->irq, dev);
1862 cx88_core_put(core,dev->pci);
1868 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1870 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1871 struct cx88_core *core = dev->core;
1874 if (core->kthread) {
1875 kthread_stop(core->kthread);
1876 core->kthread = NULL;
1879 cx88_shutdown(core); /* FIXME */
1880 pci_disable_device(pci_dev);
1882 /* unregister stuff */
1884 free_irq(pci_dev->irq, dev);
1885 cx8800_unregister_video(dev);
1886 pci_set_drvdata(pci_dev, NULL);
1889 btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1890 list_del(&dev->devlist);
1891 cx88_core_put(core,dev->pci);
1896 static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1898 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1899 struct cx88_core *core = dev->core;
1901 /* stop video+vbi capture */
1902 spin_lock(&dev->slock);
1903 if (!list_empty(&dev->vidq.active)) {
1904 printk("%s: suspend video\n", core->name);
1905 stop_video_dma(dev);
1906 del_timer(&dev->vidq.timeout);
1908 if (!list_empty(&dev->vbiq.active)) {
1909 printk("%s: suspend vbi\n", core->name);
1910 cx8800_stop_vbi_dma(dev);
1911 del_timer(&dev->vbiq.timeout);
1913 spin_unlock(&dev->slock);
1915 /* FIXME -- shutdown device */
1916 cx88_shutdown(core);
1918 pci_save_state(pci_dev);
1919 if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1920 pci_disable_device(pci_dev);
1921 dev->state.disabled = 1;
1926 static int cx8800_resume(struct pci_dev *pci_dev)
1928 struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1929 struct cx88_core *core = dev->core;
1932 if (dev->state.disabled) {
1933 err=pci_enable_device(pci_dev);
1935 printk(KERN_ERR "%s: can't enable device\n",
1940 dev->state.disabled = 0;
1942 err= pci_set_power_state(pci_dev, PCI_D0);
1944 printk(KERN_ERR "%s: can't enable device\n",
1947 pci_disable_device(pci_dev);
1948 dev->state.disabled = 1;
1952 pci_restore_state(pci_dev);
1954 /* FIXME: re-initialize hardware */
1957 /* restart video+vbi capture */
1958 spin_lock(&dev->slock);
1959 if (!list_empty(&dev->vidq.active)) {
1960 printk("%s: resume video\n", core->name);
1961 restart_video_queue(dev,&dev->vidq);
1963 if (!list_empty(&dev->vbiq.active)) {
1964 printk("%s: resume vbi\n", core->name);
1965 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1967 spin_unlock(&dev->slock);
1973 /* ----------------------------------------------------------- */
1975 static struct pci_device_id cx8800_pci_tbl[] = {
1979 .subvendor = PCI_ANY_ID,
1980 .subdevice = PCI_ANY_ID,
1982 /* --- end of list --- */
1985 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
1987 static struct pci_driver cx8800_pci_driver = {
1989 .id_table = cx8800_pci_tbl,
1990 .probe = cx8800_initdev,
1991 .remove = __devexit_p(cx8800_finidev),
1993 .suspend = cx8800_suspend,
1994 .resume = cx8800_resume,
1998 static int cx8800_init(void)
2000 printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
2001 (CX88_VERSION_CODE >> 16) & 0xff,
2002 (CX88_VERSION_CODE >> 8) & 0xff,
2003 CX88_VERSION_CODE & 0xff);
2005 printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2006 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2008 return pci_register_driver(&cx8800_pci_driver);
2011 static void cx8800_fini(void)
2013 pci_unregister_driver(&cx8800_pci_driver);
2016 module_init(cx8800_init);
2017 module_exit(cx8800_fini);
2019 /* ----------------------------------------------------------- */
2024 * 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