2 * Driver for the Conexant CX25821 PCIe bridge
4 * Copyright (C) 2009 Conexant Systems Inc.
5 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
6 * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "cx25821-video.h"
26 MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
27 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
28 MODULE_LICENSE("GPL");
30 static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
31 static unsigned int radio_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
33 module_param_array(video_nr, int, NULL, 0444);
34 module_param_array(radio_nr, int, NULL, 0444);
36 MODULE_PARM_DESC(video_nr, "video device numbers");
37 MODULE_PARM_DESC(radio_nr, "radio device numbers");
39 static unsigned int video_debug = VIDEO_DEBUG;
40 module_param(video_debug, int, 0644);
41 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
43 static unsigned int irq_debug;
44 module_param(irq_debug, int, 0644);
45 MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
47 unsigned int vid_limit = 16;
48 module_param(vid_limit, int, 0644);
49 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
51 static void init_controls(struct cx25821_dev *dev, int chan_num);
53 #define FORMAT_FLAGS_PACKED 0x01
55 struct cx25821_fmt formats[] = {
57 .name = "8 bpp, gray",
58 .fourcc = V4L2_PIX_FMT_GREY,
60 .flags = FORMAT_FLAGS_PACKED,
62 .name = "4:1:1, packed, Y41P",
63 .fourcc = V4L2_PIX_FMT_Y41P,
65 .flags = FORMAT_FLAGS_PACKED,
67 .name = "4:2:2, packed, YUYV",
68 .fourcc = V4L2_PIX_FMT_YUYV,
70 .flags = FORMAT_FLAGS_PACKED,
72 .name = "4:2:2, packed, UYVY",
73 .fourcc = V4L2_PIX_FMT_UYVY,
75 .flags = FORMAT_FLAGS_PACKED,
78 .fourcc = V4L2_PIX_FMT_YUV420,
80 .flags = FORMAT_FLAGS_PACKED,
84 int cx25821_get_format_size(void)
86 return ARRAY_SIZE(formats);
89 struct cx25821_fmt *format_by_fourcc(unsigned int fourcc)
93 if (fourcc == V4L2_PIX_FMT_Y41P || fourcc == V4L2_PIX_FMT_YUV411P) {
97 for (i = 0; i < ARRAY_SIZE(formats); i++)
98 if (formats[i].fourcc == fourcc)
101 printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __func__, fourcc);
105 void cx25821_dump_video_queue(struct cx25821_dev *dev, struct cx25821_dmaqueue *q)
107 struct cx25821_buffer *buf;
108 struct list_head *item;
109 dprintk(1, "%s()\n", __func__);
111 if (!list_empty(&q->active)) {
112 list_for_each(item, &q->active)
113 buf = list_entry(item, struct cx25821_buffer, vb.queue);
116 if (!list_empty(&q->queued)) {
117 list_for_each(item, &q->queued)
118 buf = list_entry(item, struct cx25821_buffer, vb.queue);
123 void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q,
126 struct cx25821_buffer *buf;
129 for (bc = 0;; bc++) {
130 if (list_empty(&q->active)) {
131 dprintk(1, "bc=%d (=0: active empty)\n", bc);
136 list_entry(q->active.next, struct cx25821_buffer, vb.queue);
138 /* count comes from the hw and it is 16bit wide --
139 * this trick handles wrap-arounds correctly for
140 * up to 32767 buffers in flight... */
141 if ((s16) (count - buf->count) < 0) {
145 do_gettimeofday(&buf->vb.ts);
146 buf->vb.state = VIDEOBUF_DONE;
147 list_del(&buf->vb.queue);
148 wake_up(&buf->vb.done);
151 if (list_empty(&q->active))
152 del_timer(&q->timeout);
154 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
156 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
161 int cx25821_set_tvnorm(struct cx25821_dev *dev, v4l2_std_id norm)
163 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n", __func__,
164 (unsigned int)norm, v4l2_norm_to_name(norm));
168 /* Tell the internal A/V decoder */
169 cx25821_call_all(dev, core, s_std, norm);
175 struct video_device *cx25821_vdev_init(struct cx25821_dev *dev,
177 struct video_device *template,
180 struct video_device *vfd;
181 dprintk(1, "%s()\n", __func__);
183 vfd = video_device_alloc();
187 vfd->v4l2_dev = &dev->v4l2_dev;
188 vfd->release = video_device_release;
189 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name, type,
190 cx25821_boards[dev->board].name);
191 video_set_drvdata(vfd, dev);
196 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
200 if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
202 for (i = 0; i < CX25821_CTLS; i++)
203 if (cx25821_ctls[i].v.id == qctrl->id)
205 if (i == CX25821_CTLS) {
209 *qctrl = cx25821_ctls[i].v;
214 // resource management
215 int cx25821_res_get(struct cx25821_dev *dev, struct cx25821_fh *fh, unsigned int bit)
217 dprintk(1, "%s()\n", __func__);
218 if (fh->resources & bit)
219 /* have it already allocated */
223 mutex_lock(&dev->lock);
224 if (dev->resources & bit) {
225 /* no, someone else uses it */
226 mutex_unlock(&dev->lock);
229 /* it's free, grab it */
230 fh->resources |= bit;
231 dev->resources |= bit;
232 dprintk(1, "res: get %d\n", bit);
233 mutex_unlock(&dev->lock);
237 int cx25821_res_check(struct cx25821_fh *fh, unsigned int bit)
239 return fh->resources & bit;
242 int cx25821_res_locked(struct cx25821_dev *dev, unsigned int bit)
244 return dev->resources & bit;
247 void cx25821_res_free(struct cx25821_dev *dev, struct cx25821_fh *fh, unsigned int bits)
249 BUG_ON((fh->resources & bits) != bits);
250 dprintk(1, "%s()\n", __func__);
252 mutex_lock(&dev->lock);
253 fh->resources &= ~bits;
254 dev->resources &= ~bits;
255 dprintk(1, "res: put %d\n", bits);
256 mutex_unlock(&dev->lock);
259 int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input)
261 struct v4l2_routing route;
262 memset(&route, 0, sizeof(route));
264 dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
265 __func__, input, INPUT(input)->vmux, INPUT(input)->gpio0,
266 INPUT(input)->gpio1, INPUT(input)->gpio2, INPUT(input)->gpio3);
269 route.input = INPUT(input)->vmux;
271 /* Tell the internal A/V decoder */
272 cx25821_call_all(dev, video, s_routing, INPUT(input)->vmux, 0, 0);
277 int cx25821_start_video_dma(struct cx25821_dev *dev,
278 struct cx25821_dmaqueue *q,
279 struct cx25821_buffer *buf,
280 struct sram_channel *channel)
284 /* setup fifo + format */
285 cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
288 cx_write(channel->gpcnt_ctl, 3);
292 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
293 cx_set(channel->int_msk, 0x11);
296 cx_write(channel->dma_ctl, 0x11); /* FIFO and RISC enable */
298 /* make sure upstream setting if any is reversed */
299 tmp = cx_read(VID_CH_MODE_SEL);
300 cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
305 int cx25821_restart_video_queue(struct cx25821_dev *dev,
306 struct cx25821_dmaqueue *q,
307 struct sram_channel *channel)
309 struct cx25821_buffer *buf, *prev;
310 struct list_head *item;
312 if (!list_empty(&q->active)) {
314 list_entry(q->active.next, struct cx25821_buffer, vb.queue);
316 cx25821_start_video_dma(dev, q, buf, channel);
318 list_for_each(item, &q->active) {
319 buf = list_entry(item, struct cx25821_buffer, vb.queue);
320 buf->count = q->count++;
323 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
329 if (list_empty(&q->queued))
333 list_entry(q->queued.next, struct cx25821_buffer, vb.queue);
336 list_move_tail(&buf->vb.queue, &q->active);
337 cx25821_start_video_dma(dev, q, buf, channel);
338 buf->vb.state = VIDEOBUF_ACTIVE;
339 buf->count = q->count++;
340 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
341 } else if (prev->vb.width == buf->vb.width &&
342 prev->vb.height == buf->vb.height &&
343 prev->fmt == buf->fmt) {
344 list_move_tail(&buf->vb.queue, &q->active);
345 buf->vb.state = VIDEOBUF_ACTIVE;
346 buf->count = q->count++;
347 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
348 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
356 void cx25821_vid_timeout(unsigned long data)
358 struct cx25821_data *timeout_data = (struct cx25821_data *)data;
359 struct cx25821_dev *dev = timeout_data->dev;
360 struct sram_channel *channel = timeout_data->channel;
361 struct cx25821_dmaqueue *q = &dev->vidq[channel->i];
362 struct cx25821_buffer *buf;
365 //cx25821_sram_channel_dump(dev, channel);
366 cx_clear(channel->dma_ctl, 0x11);
368 spin_lock_irqsave(&dev->slock, flags);
369 while (!list_empty(&q->active)) {
371 list_entry(q->active.next, struct cx25821_buffer, vb.queue);
372 list_del(&buf->vb.queue);
374 buf->vb.state = VIDEOBUF_ERROR;
375 wake_up(&buf->vb.done);
378 cx25821_restart_video_queue(dev, q, channel);
379 spin_unlock_irqrestore(&dev->slock, flags);
382 int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
387 struct sram_channel *channel = &dev->sram_channels[chan_num];
389 mask = cx_read(channel->int_msk);
390 if (0 == (status & mask))
393 cx_write(channel->int_stat, status);
395 /* risc op code error */
396 if (status & (1 << 16)) {
397 printk(KERN_WARNING "%s, %s: video risc op code error\n",
398 dev->name, channel->name);
399 cx_clear(channel->dma_ctl, 0x11);
400 cx25821_sram_channel_dump(dev, channel);
404 if (status & FLD_VID_DST_RISC1) {
405 spin_lock(&dev->slock);
406 count = cx_read(channel->gpcnt);
407 cx25821_video_wakeup(dev, &dev->vidq[channel->i], count);
408 spin_unlock(&dev->slock);
414 dprintk(2, "stopper video\n");
415 spin_lock(&dev->slock);
416 cx25821_restart_video_queue(dev, &dev->vidq[channel->i],
418 spin_unlock(&dev->slock);
424 void cx25821_videoioctl_unregister(struct cx25821_dev *dev)
426 if (dev->ioctl_dev) {
427 if (video_is_registered(dev->ioctl_dev))
428 video_unregister_device(dev->ioctl_dev);
430 video_device_release(dev->ioctl_dev);
432 dev->ioctl_dev = NULL;
436 void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
438 cx_clear(PCI_INT_MSK, 1);
440 if (dev->video_dev[chan_num]) {
441 if (video_is_registered(dev->video_dev[chan_num]))
442 video_unregister_device(dev->video_dev[chan_num]);
444 video_device_release(dev->video_dev[chan_num]);
446 dev->video_dev[chan_num] = NULL;
448 btcx_riscmem_free(dev->pci, &dev->vidq[chan_num].stopper);
450 printk(KERN_WARNING "device %d released!\n", chan_num);
455 int cx25821_video_register(struct cx25821_dev *dev, int chan_num,
456 struct video_device *video_template)
460 spin_lock_init(&dev->slock);
462 //printk(KERN_WARNING "Channel %d\n", chan_num);
465 dev->tvnorm = video_template->current_norm;
468 /* init video dma queues */
469 dev->timeout_data[chan_num].dev = dev;
470 dev->timeout_data[chan_num].channel = &dev->sram_channels[chan_num];
471 INIT_LIST_HEAD(&dev->vidq[chan_num].active);
472 INIT_LIST_HEAD(&dev->vidq[chan_num].queued);
473 dev->vidq[chan_num].timeout.function = cx25821_vid_timeout;
474 dev->vidq[chan_num].timeout.data =
475 (unsigned long)&dev->timeout_data[chan_num];
476 init_timer(&dev->vidq[chan_num].timeout);
477 cx25821_risc_stopper(dev->pci, &dev->vidq[chan_num].stopper,
478 dev->sram_channels[chan_num].dma_ctl, 0x11, 0);
480 /* register v4l devices */
481 dev->video_dev[chan_num] =
482 cx25821_vdev_init(dev, dev->pci, video_template, "video");
484 video_register_device(dev->video_dev[chan_num], VFL_TYPE_GRABBER,
491 cx_set(PCI_INT_MSK, 0xff);
493 /* initial device configuration */
494 mutex_lock(&dev->lock);
496 cx25821_set_tvnorm(dev, dev->tvnorm);
498 mutex_unlock(&dev->lock);
500 init_controls(dev, chan_num);
505 cx25821_video_unregister(dev, chan_num);
509 int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
512 struct cx25821_fh *fh = q->priv_data;
514 *size = fh->fmt->depth * fh->width * fh->height >> 3;
519 if (*size * *count > vid_limit * 1024 * 1024)
520 *count = (vid_limit * 1024 * 1024) / *size;
525 int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
526 enum v4l2_field field)
528 struct cx25821_fh *fh = q->priv_data;
529 struct cx25821_dev *dev = fh->dev;
530 struct cx25821_buffer *buf =
531 container_of(vb, struct cx25821_buffer, vb);
532 int rc, init_buffer = 0;
533 u32 line0_offset, line1_offset;
534 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
535 int bpl_local = LINE_SIZE_D1;
536 int channel_opened = 0;
538 BUG_ON(NULL == fh->fmt);
539 if (fh->width < 48 || fh->width > 720 ||
540 fh->height < 32 || fh->height > 576)
543 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
545 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
548 if (buf->fmt != fh->fmt ||
549 buf->vb.width != fh->width ||
550 buf->vb.height != fh->height || buf->vb.field != field) {
552 buf->vb.width = fh->width;
553 buf->vb.height = fh->height;
554 buf->vb.field = field;
558 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
560 rc = videobuf_iolock(q, &buf->vb, NULL);
562 printk(KERN_DEBUG "videobuf_iolock failed!\n");
567 dprintk(1, "init_buffer=%d\n", init_buffer);
571 channel_opened = dev->channel_opened;
572 channel_opened = (channel_opened < 0
573 || channel_opened > 7) ? 7 : channel_opened;
575 if (dev->pixel_formats[channel_opened] == PIXEL_FRMT_411)
576 buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
578 buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
580 if (dev->pixel_formats[channel_opened] == PIXEL_FRMT_411) {
581 bpl_local = buf->bpl;
583 bpl_local = buf->bpl; //Default
585 if (channel_opened >= 0 && channel_opened <= 7) {
586 if (dev->use_cif_resolution[channel_opened]) {
587 if (dev->tvnorm & V4L2_STD_PAL_BG
588 || dev->tvnorm & V4L2_STD_PAL_DK)
589 bpl_local = 352 << 1;
593 cif_width[channel_opened] <<
599 switch (buf->vb.field) {
601 cx25821_risc_buffer(dev->pci, &buf->risc,
602 dma->sglist, 0, UNSET,
603 buf->bpl, 0, buf->vb.height);
605 case V4L2_FIELD_BOTTOM:
606 cx25821_risc_buffer(dev->pci, &buf->risc,
607 dma->sglist, UNSET, 0,
608 buf->bpl, 0, buf->vb.height);
610 case V4L2_FIELD_INTERLACED:
611 /* All other formats are top field first */
613 line1_offset = buf->bpl;
614 dprintk(1, "top field first\n");
616 cx25821_risc_buffer(dev->pci, &buf->risc,
617 dma->sglist, line0_offset,
618 bpl_local, bpl_local, bpl_local,
619 buf->vb.height >> 1);
621 case V4L2_FIELD_SEQ_TB:
622 cx25821_risc_buffer(dev->pci, &buf->risc,
624 0, buf->bpl * (buf->vb.height >> 1),
625 buf->bpl, 0, buf->vb.height >> 1);
627 case V4L2_FIELD_SEQ_BT:
628 cx25821_risc_buffer(dev->pci, &buf->risc,
630 buf->bpl * (buf->vb.height >> 1), 0,
631 buf->bpl, 0, buf->vb.height >> 1);
638 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
639 buf, buf->vb.i, fh->width, fh->height, fh->fmt->depth,
640 fh->fmt->name, (unsigned long)buf->risc.dma);
642 buf->vb.state = VIDEOBUF_PREPARED;
647 cx25821_free_buffer(q, buf);
651 void cx25821_buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
653 struct cx25821_buffer *buf =
654 container_of(vb, struct cx25821_buffer, vb);
656 cx25821_free_buffer(q, buf);
659 struct videobuf_queue *get_queue(struct cx25821_fh *fh)
662 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
670 int cx25821_get_resource(struct cx25821_fh *fh, int resource)
673 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
681 int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
683 struct cx25821_fh *fh = file->private_data;
685 return videobuf_mmap_mapper(get_queue(fh), vma);
689 int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f)
691 struct cx25821_fh *fh = priv;
693 f->fmt.pix.width = fh->width;
694 f->fmt.pix.height = fh->height;
695 f->fmt.pix.field = fh->vidq.field;
696 f->fmt.pix.pixelformat = fh->fmt->fourcc;
697 f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3;
698 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
703 int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f)
705 struct cx25821_fmt *fmt;
706 enum v4l2_field field;
707 unsigned int maxw, maxh;
709 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
713 field = f->fmt.pix.field;
717 if (V4L2_FIELD_ANY == field) {
718 field = (f->fmt.pix.height > maxh / 2)
719 ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
724 case V4L2_FIELD_BOTTOM:
727 case V4L2_FIELD_INTERLACED:
733 f->fmt.pix.field = field;
734 if (f->fmt.pix.height < 32)
735 f->fmt.pix.height = 32;
736 if (f->fmt.pix.height > maxh)
737 f->fmt.pix.height = maxh;
738 if (f->fmt.pix.width < 48)
739 f->fmt.pix.width = 48;
740 if (f->fmt.pix.width > maxw)
741 f->fmt.pix.width = maxw;
742 f->fmt.pix.width &= ~0x03;
743 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
744 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
749 int cx25821_vidioc_querycap(struct file *file, void *priv, struct v4l2_capability *cap)
751 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
753 strcpy(cap->driver, "cx25821");
754 strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
755 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
756 cap->version = CX25821_VERSION_CODE;
758 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
759 if (UNSET != dev->tuner_type)
760 cap->capabilities |= V4L2_CAP_TUNER;
764 int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
765 struct v4l2_fmtdesc *f)
767 if (unlikely(f->index >= ARRAY_SIZE(formats)))
770 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
771 f->pixelformat = formats[f->index].fourcc;
776 #ifdef CONFIG_VIDEO_V4L1_COMPAT
777 int cx25821_vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
779 struct cx25821_fh *fh = priv;
780 struct videobuf_queue *q;
781 struct v4l2_requestbuffers req;
786 memset(&req, 0, sizeof(req));
789 req.memory = V4L2_MEMORY_MMAP;
790 err = videobuf_reqbufs(q, &req);
794 mbuf->frames = req.count;
796 for (i = 0; i < mbuf->frames; i++) {
797 mbuf->offsets[i] = q->bufs[i]->boff;
798 mbuf->size += q->bufs[i]->bsize;
804 int cx25821_vidioc_reqbufs(struct file *file, void *priv, struct v4l2_requestbuffers *p)
806 struct cx25821_fh *fh = priv;
807 return videobuf_reqbufs(get_queue(fh), p);
810 int cx25821_vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
812 struct cx25821_fh *fh = priv;
813 return videobuf_querybuf(get_queue(fh), p);
816 int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
818 struct cx25821_fh *fh = priv;
819 return videobuf_qbuf(get_queue(fh), p);
822 int cx25821_vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
824 struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
826 *p = v4l2_prio_max(&dev->prio);
831 int cx25821_vidioc_s_priority(struct file *file, void *f, enum v4l2_priority prio)
833 struct cx25821_fh *fh = f;
834 struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
836 return v4l2_prio_change(&dev->prio, &fh->prio, prio);
840 int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id * tvnorms)
842 struct cx25821_fh *fh = priv;
843 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
846 dprintk(1, "%s()\n", __func__);
849 err = v4l2_prio_check(&dev->prio, fh->prio);
854 if (dev->tvnorm == *tvnorms) {
858 mutex_lock(&dev->lock);
859 cx25821_set_tvnorm(dev, *tvnorms);
860 mutex_unlock(&dev->lock);
862 medusa_set_videostandard(dev);
868 int cx25821_enum_input(struct cx25821_dev *dev, struct v4l2_input *i)
870 static const char *iname[] = {
871 [CX25821_VMUX_COMPOSITE] = "Composite",
872 [CX25821_VMUX_SVIDEO] = "S-Video",
873 [CX25821_VMUX_DEBUG] = "for debug only",
876 dprintk(1, "%s()\n", __func__);
882 if (0 == INPUT(n)->type)
885 memset(i, 0, sizeof(*i));
887 i->type = V4L2_INPUT_TYPE_CAMERA;
888 strcpy(i->name, iname[INPUT(n)->type]);
890 i->std = CX25821_NORMS;
894 int cx25821_vidioc_enum_input(struct file *file, void *priv, struct v4l2_input *i)
896 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
897 dprintk(1, "%s()\n", __func__);
898 return cx25821_enum_input(dev, i);
901 int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
903 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
906 dprintk(1, "%s() returns %d\n", __func__, *i);
910 int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
912 struct cx25821_fh *fh = priv;
913 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
916 dprintk(1, "%s(%d)\n", __func__, i);
919 err = v4l2_prio_check(&dev->prio, fh->prio);
925 dprintk(1, "%s() -EINVAL\n", __func__);
929 mutex_lock(&dev->lock);
930 cx25821_video_mux(dev, i);
931 mutex_unlock(&dev->lock);
936 int cx25821_vidioc_g_frequency(struct file *file, void *priv, struct v4l2_frequency *f)
938 struct cx25821_fh *fh = priv;
939 struct cx25821_dev *dev = fh->dev;
941 f->frequency = dev->freq;
943 cx25821_call_all(dev, tuner, g_frequency, f);
948 int cx25821_set_freq(struct cx25821_dev *dev, struct v4l2_frequency *f)
950 mutex_lock(&dev->lock);
951 dev->freq = f->frequency;
953 cx25821_call_all(dev, tuner, s_frequency, f);
955 /* When changing channels it is required to reset TVAUDIO */
958 mutex_unlock(&dev->lock);
963 int cx25821_vidioc_s_frequency(struct file *file, void *priv, struct v4l2_frequency *f)
965 struct cx25821_fh *fh = priv;
966 struct cx25821_dev *dev;
970 err = v4l2_prio_check(&dev->prio, fh->prio);
975 return cx25821_set_freq(dev, f);
979 #ifdef CONFIG_VIDEO_ADV_DEBUG
980 int cx25821_vidioc_g_register(struct file *file, void *fh,
981 struct v4l2_dbg_register *reg)
983 struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
985 if (!v4l2_chip_match_host(®->match))
988 cx25821_call_all(dev, core, g_register, reg);
993 int cx25821_vidioc_s_register(struct file *file, void *fh,
994 struct v4l2_dbg_register *reg)
996 struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
998 if (!v4l2_chip_match_host(®->match))
1001 cx25821_call_all(dev, core, s_register, reg);
1009 int cx25821_vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1011 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1013 if (unlikely(UNSET == dev->tuner_type))
1018 strcpy(t->name, "Television");
1019 t->type = V4L2_TUNER_ANALOG_TV;
1020 t->capability = V4L2_TUNER_CAP_NORM;
1021 t->rangehigh = 0xffffffffUL;
1023 t->signal = 0xffff; /* LOCKED */
1027 int cx25821_vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1029 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1030 struct cx25821_fh *fh = priv;
1034 err = v4l2_prio_check(&dev->prio, fh->prio);
1039 dprintk(1, "%s()\n", __func__);
1040 if (UNSET == dev->tuner_type)
1049 // ******************************************************************************************
1050 static const struct v4l2_queryctrl no_ctl = {
1052 .flags = V4L2_CTRL_FLAG_DISABLED,
1055 static struct v4l2_queryctrl cx25821_ctls[] = {
1058 .id = V4L2_CID_BRIGHTNESS,
1059 .name = "Brightness",
1063 .default_value = 6200,
1064 .type = V4L2_CTRL_TYPE_INTEGER,
1066 .id = V4L2_CID_CONTRAST,
1071 .default_value = 5000,
1072 .type = V4L2_CTRL_TYPE_INTEGER,
1074 .id = V4L2_CID_SATURATION,
1075 .name = "Saturation",
1079 .default_value = 5000,
1080 .type = V4L2_CTRL_TYPE_INTEGER,
1087 .default_value = 5000,
1088 .type = V4L2_CTRL_TYPE_INTEGER,
1091 static const int CX25821_CTLS = ARRAY_SIZE(cx25821_ctls);
1093 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
1097 if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
1099 for (i = 0; i < CX25821_CTLS; i++)
1100 if (cx25821_ctls[i].id == qctrl->id)
1102 if (i == CX25821_CTLS) {
1106 *qctrl = cx25821_ctls[i];
1110 int cx25821_vidioc_queryctrl(struct file *file, void *priv,
1111 struct v4l2_queryctrl *qctrl)
1113 return cx25821_ctrl_query(qctrl);
1116 /* ------------------------------------------------------------------ */
1117 /* VIDEO CTRL IOCTLS */
1119 static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
1123 for (i = 0; i < CX25821_CTLS; i++)
1124 if (cx25821_ctls[i].id == id)
1125 return cx25821_ctls + i;
1129 int cx25821_vidioc_g_ctrl(struct file *file, void *priv, struct v4l2_control *ctl)
1131 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1133 const struct v4l2_queryctrl *ctrl;
1135 ctrl = ctrl_by_id(ctl->id);
1140 case V4L2_CID_BRIGHTNESS:
1141 ctl->value = dev->ctl_bright;
1144 ctl->value = dev->ctl_hue;
1146 case V4L2_CID_CONTRAST:
1147 ctl->value = dev->ctl_contrast;
1149 case V4L2_CID_SATURATION:
1150 ctl->value = dev->ctl_saturation;
1156 int cx25821_set_control(struct cx25821_dev *dev,
1157 struct v4l2_control *ctl, int chan_num)
1160 const struct v4l2_queryctrl *ctrl;
1164 ctrl = ctrl_by_id(ctl->id);
1169 switch (ctrl->type) {
1170 case V4L2_CTRL_TYPE_BOOLEAN:
1171 case V4L2_CTRL_TYPE_MENU:
1172 case V4L2_CTRL_TYPE_INTEGER:
1173 if (ctl->value < ctrl->minimum)
1174 ctl->value = ctrl->minimum;
1175 if (ctl->value > ctrl->maximum)
1176 ctl->value = ctrl->maximum;
1183 case V4L2_CID_BRIGHTNESS:
1184 dev->ctl_bright = ctl->value;
1185 medusa_set_brightness(dev, ctl->value, chan_num);
1188 dev->ctl_hue = ctl->value;
1189 medusa_set_hue(dev, ctl->value, chan_num);
1191 case V4L2_CID_CONTRAST:
1192 dev->ctl_contrast = ctl->value;
1193 medusa_set_contrast(dev, ctl->value, chan_num);
1195 case V4L2_CID_SATURATION:
1196 dev->ctl_saturation = ctl->value;
1197 medusa_set_saturation(dev, ctl->value, chan_num);
1206 static void init_controls(struct cx25821_dev *dev, int chan_num)
1208 struct v4l2_control ctrl;
1210 for (i = 0; i < CX25821_CTLS; i++) {
1211 ctrl.id = cx25821_ctls[i].id;
1212 ctrl.value = cx25821_ctls[i].default_value;
1214 cx25821_set_control(dev, &ctrl, chan_num);
1218 int cx25821_vidioc_cropcap(struct file *file, void *priv, struct v4l2_cropcap *cropcap)
1220 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1222 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1224 cropcap->bounds.top = cropcap->bounds.left = 0;
1225 cropcap->bounds.width = 720;
1226 cropcap->bounds.height = dev->tvnorm == V4L2_STD_PAL_BG ? 576 : 480;
1227 cropcap->pixelaspect.numerator =
1228 dev->tvnorm == V4L2_STD_PAL_BG ? 59 : 10;
1229 cropcap->pixelaspect.denominator =
1230 dev->tvnorm == V4L2_STD_PAL_BG ? 54 : 11;
1231 cropcap->defrect = cropcap->bounds;
1235 int cx25821_vidioc_s_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1237 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1238 struct cx25821_fh *fh = priv;
1242 err = v4l2_prio_check(&dev->prio, fh->prio);
1246 // cx25821_vidioc_s_crop not supported
1250 int cx25821_vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1252 // cx25821_vidioc_g_crop not supported
1256 int cx25821_vidioc_querystd(struct file *file, void *priv, v4l2_std_id * norm)
1258 // medusa does not support video standard sensing of current input
1259 *norm = CX25821_NORMS;
1264 int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm)
1266 if (tvnorm == V4L2_STD_PAL_BG) {
1267 if (width == 352 || width == 720)
1273 if (tvnorm == V4L2_STD_NTSC_M) {
1274 if (width == 320 || width == 352 || width == 720)
1282 int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm)
1284 if (tvnorm == V4L2_STD_PAL_BG) {
1285 if (height == 576 || height == 288)
1291 if (tvnorm == V4L2_STD_NTSC_M) {
1292 if (height == 480 || height == 240)