V4L/DVB: V4L2: Replace loops for finding max buffers in VIDIOC_REQBUFS callbacks
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / cx25821 / cx25821-video.c
bloba3bea2739cefb159224a474386c7be724c1c65bf
1 /*
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,
59 .depth = 8,
60 .flags = FORMAT_FLAGS_PACKED,
61 }, {
62 .name = "4:1:1, packed, Y41P",
63 .fourcc = V4L2_PIX_FMT_Y41P,
64 .depth = 12,
65 .flags = FORMAT_FLAGS_PACKED,
66 }, {
67 .name = "4:2:2, packed, YUYV",
68 .fourcc = V4L2_PIX_FMT_YUYV,
69 .depth = 16,
70 .flags = FORMAT_FLAGS_PACKED,
71 }, {
72 .name = "4:2:2, packed, UYVY",
73 .fourcc = V4L2_PIX_FMT_UYVY,
74 .depth = 16,
75 .flags = FORMAT_FLAGS_PACKED,
76 }, {
77 .name = "4:2:0, YUV",
78 .fourcc = V4L2_PIX_FMT_YUV420,
79 .depth = 12,
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)
91 unsigned int i;
93 if (fourcc == V4L2_PIX_FMT_Y41P || fourcc == V4L2_PIX_FMT_YUV411P) {
94 return formats + 1;
97 for (i = 0; i < ARRAY_SIZE(formats); i++)
98 if (formats[i].fourcc == fourcc)
99 return formats + i;
101 printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __func__, fourcc);
102 return NULL;
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,
124 u32 count)
126 struct cx25821_buffer *buf;
127 int bc;
129 for (bc = 0;; bc++) {
130 if (list_empty(&q->active)) {
131 dprintk(1, "bc=%d (=0: active empty)\n", bc);
132 break;
135 buf =
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) {
142 break;
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);
153 else
154 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
155 if (bc != 1)
156 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
157 __func__, bc);
160 #ifdef TUNER_FLAG
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));
166 dev->tvnorm = norm;
168 /* Tell the internal A/V decoder */
169 cx25821_call_all(dev, core, s_std, norm);
171 return 0;
173 #endif
175 struct video_device *cx25821_vdev_init(struct cx25821_dev *dev,
176 struct pci_dev *pci,
177 struct video_device *template,
178 char *type)
180 struct video_device *vfd;
181 dprintk(1, "%s()\n", __func__);
183 vfd = video_device_alloc();
184 if (NULL == vfd)
185 return NULL;
186 *vfd = *template;
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);
192 return vfd;
196 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
198 int i;
200 if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
201 return -EINVAL;
202 for (i = 0; i < CX25821_CTLS; i++)
203 if (cx25821_ctls[i].v.id == qctrl->id)
204 break;
205 if (i == CX25821_CTLS) {
206 *qctrl = no_ctl;
207 return 0;
209 *qctrl = cx25821_ctls[i].v;
210 return 0;
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 */
220 return 1;
222 /* is it free? */
223 mutex_lock(&dev->lock);
224 if (dev->resources & bit) {
225 /* no, someone else uses it */
226 mutex_unlock(&dev->lock);
227 return 0;
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);
234 return 1;
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);
267 dev->input = input;
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);
274 return 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)
282 int tmp = 0;
284 /* setup fifo + format */
285 cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
287 /* reset counter */
288 cx_write(channel->gpcnt_ctl, 3);
289 q->count = 1;
291 /* enable irq */
292 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
293 cx_set(channel->int_msk, 0x11);
295 /* start dma */
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);
302 return 0;
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)) {
313 buf =
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);
324 return 0;
327 prev = NULL;
328 for (;;) {
329 if (list_empty(&q->queued))
330 return 0;
332 buf =
333 list_entry(q->queued.next, struct cx25821_buffer, vb.queue);
335 if (NULL == prev) {
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 */
349 } else {
350 return 0;
352 prev = buf;
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;
363 unsigned long flags;
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)) {
370 buf =
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)
384 u32 count = 0;
385 int handled = 0;
386 u32 mask;
387 struct sram_channel *channel = &dev->sram_channels[chan_num];
389 mask = cx_read(channel->int_msk);
390 if (0 == (status & mask))
391 return handled;
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);
403 /* risc1 y */
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);
409 handled++;
412 /* risc2 y */
413 if (status & 0x10) {
414 dprintk(2, "stopper video\n");
415 spin_lock(&dev->slock);
416 cx25821_restart_video_queue(dev, &dev->vidq[channel->i],
417 channel);
418 spin_unlock(&dev->slock);
419 handled++;
421 return handled;
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);
429 else
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]);
443 else
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)
458 int err;
460 spin_lock_init(&dev->slock);
462 //printk(KERN_WARNING "Channel %d\n", chan_num);
464 #ifdef TUNER_FLAG
465 dev->tvnorm = video_template->current_norm;
466 #endif
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");
483 err =
484 video_register_device(dev->video_dev[chan_num], VFL_TYPE_GRABBER,
485 video_nr[dev->nr]);
487 if (err < 0) {
488 goto fail_unreg;
490 //set PCI interrupt
491 cx_set(PCI_INT_MSK, 0xff);
493 /* initial device configuration */
494 mutex_lock(&dev->lock);
495 #ifdef TUNER_FLAG
496 cx25821_set_tvnorm(dev, dev->tvnorm);
497 #endif
498 mutex_unlock(&dev->lock);
500 init_controls(dev, chan_num);
502 return 0;
504 fail_unreg:
505 cx25821_video_unregister(dev, chan_num);
506 return err;
509 int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
510 unsigned int *size)
512 struct cx25821_fh *fh = q->priv_data;
514 *size = fh->fmt->depth * fh->width * fh->height >> 3;
516 if (0 == *count)
517 *count = 32;
519 if (*size * *count > vid_limit * 1024 * 1024)
520 *count = (vid_limit * 1024 * 1024) / *size;
522 return 0;
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)
541 return -EINVAL;
543 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
545 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
546 return -EINVAL;
548 if (buf->fmt != fh->fmt ||
549 buf->vb.width != fh->width ||
550 buf->vb.height != fh->height || buf->vb.field != field) {
551 buf->fmt = fh->fmt;
552 buf->vb.width = fh->width;
553 buf->vb.height = fh->height;
554 buf->vb.field = field;
555 init_buffer = 1;
558 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
559 init_buffer = 1;
560 rc = videobuf_iolock(q, &buf->vb, NULL);
561 if (0 != rc) {
562 printk(KERN_DEBUG "videobuf_iolock failed!\n");
563 goto fail;
567 dprintk(1, "init_buffer=%d\n", init_buffer);
569 if (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;
577 else
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;
582 } else {
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;
590 else
591 bpl_local =
592 dev->
593 cif_width[channel_opened] <<
599 switch (buf->vb.field) {
600 case V4L2_FIELD_TOP:
601 cx25821_risc_buffer(dev->pci, &buf->risc,
602 dma->sglist, 0, UNSET,
603 buf->bpl, 0, buf->vb.height);
604 break;
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);
609 break;
610 case V4L2_FIELD_INTERLACED:
611 /* All other formats are top field first */
612 line0_offset = 0;
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);
620 break;
621 case V4L2_FIELD_SEQ_TB:
622 cx25821_risc_buffer(dev->pci, &buf->risc,
623 dma->sglist,
624 0, buf->bpl * (buf->vb.height >> 1),
625 buf->bpl, 0, buf->vb.height >> 1);
626 break;
627 case V4L2_FIELD_SEQ_BT:
628 cx25821_risc_buffer(dev->pci, &buf->risc,
629 dma->sglist,
630 buf->bpl * (buf->vb.height >> 1), 0,
631 buf->bpl, 0, buf->vb.height >> 1);
632 break;
633 default:
634 BUG();
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;
644 return 0;
646 fail:
647 cx25821_free_buffer(q, buf);
648 return rc;
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)
661 switch (fh->type) {
662 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
663 return &fh->vidq;
664 default:
665 BUG();
666 return NULL;
670 int cx25821_get_resource(struct cx25821_fh *fh, int resource)
672 switch (fh->type) {
673 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
674 return resource;
675 default:
676 BUG();
677 return 0;
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);
688 /* VIDEO IOCTLS */
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;
700 return 0;
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);
710 if (NULL == fmt)
711 return -EINVAL;
713 field = f->fmt.pix.field;
714 maxw = 720;
715 maxh = 576;
717 if (V4L2_FIELD_ANY == field) {
718 field = (f->fmt.pix.height > maxh / 2)
719 ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
722 switch (field) {
723 case V4L2_FIELD_TOP:
724 case V4L2_FIELD_BOTTOM:
725 maxh = maxh / 2;
726 break;
727 case V4L2_FIELD_INTERLACED:
728 break;
729 default:
730 return -EINVAL;
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;
746 return 0;
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;
757 cap->capabilities =
758 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
759 if (UNSET != dev->tuner_type)
760 cap->capabilities |= V4L2_CAP_TUNER;
761 return 0;
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)))
768 return -EINVAL;
770 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
771 f->pixelformat = formats[f->index].fourcc;
773 return 0;
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;
782 unsigned int i;
783 int err;
785 q = get_queue(fh);
786 memset(&req, 0, sizeof(req));
787 req.type = q->type;
788 req.count = 8;
789 req.memory = V4L2_MEMORY_MMAP;
790 err = videobuf_reqbufs(q, &req);
791 if (err < 0)
792 return err;
794 mbuf->frames = req.count;
795 mbuf->size = 0;
796 for (i = 0; i < mbuf->frames; i++) {
797 mbuf->offsets[i] = q->bufs[i]->boff;
798 mbuf->size += q->bufs[i]->bsize;
800 return 0;
802 #endif
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);
828 return 0;
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);
839 #ifdef TUNER_FLAG
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;
844 int err;
846 dprintk(1, "%s()\n", __func__);
848 if (fh) {
849 err = v4l2_prio_check(&dev->prio, &fh->prio);
850 if (0 != err)
851 return err;
854 if (dev->tvnorm == *tvnorms) {
855 return 0;
858 mutex_lock(&dev->lock);
859 cx25821_set_tvnorm(dev, *tvnorms);
860 mutex_unlock(&dev->lock);
862 medusa_set_videostandard(dev);
864 return 0;
866 #endif
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",
875 unsigned int n;
876 dprintk(1, "%s()\n", __func__);
878 n = i->index;
879 if (n >= 2)
880 return -EINVAL;
882 if (0 == INPUT(n)->type)
883 return -EINVAL;
885 memset(i, 0, sizeof(*i));
886 i->index = n;
887 i->type = V4L2_INPUT_TYPE_CAMERA;
888 strcpy(i->name, iname[INPUT(n)->type]);
890 i->std = CX25821_NORMS;
891 return 0;
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;
905 *i = dev->input;
906 dprintk(1, "%s() returns %d\n", __func__, *i);
907 return 0;
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;
914 int err;
916 dprintk(1, "%s(%d)\n", __func__, i);
918 if (fh) {
919 err = v4l2_prio_check(&dev->prio, &fh->prio);
920 if (0 != err)
921 return err;
924 if (i > 2) {
925 dprintk(1, "%s() -EINVAL\n", __func__);
926 return -EINVAL;
929 mutex_lock(&dev->lock);
930 cx25821_video_mux(dev, i);
931 mutex_unlock(&dev->lock);
932 return 0;
935 #ifdef TUNER_FLAG
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);
945 return 0;
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 */
956 msleep(10);
958 mutex_unlock(&dev->lock);
960 return 0;
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;
967 int err;
969 if (fh) {
970 dev = fh->dev;
971 err = v4l2_prio_check(&dev->prio, &fh->prio);
972 if (0 != err)
973 return err;
976 return cx25821_set_freq(dev, f);
978 #endif
980 #ifdef CONFIG_VIDEO_ADV_DEBUG
981 int cx25821_vidioc_g_register(struct file *file, void *fh,
982 struct v4l2_dbg_register *reg)
984 struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
986 if (!v4l2_chip_match_host(&reg->match))
987 return -EINVAL;
989 cx25821_call_all(dev, core, g_register, reg);
991 return 0;
994 int cx25821_vidioc_s_register(struct file *file, void *fh,
995 struct v4l2_dbg_register *reg)
997 struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
999 if (!v4l2_chip_match_host(&reg->match))
1000 return -EINVAL;
1002 cx25821_call_all(dev, core, s_register, reg);
1004 return 0;
1007 #endif
1009 #ifdef TUNER_FLAG
1010 int cx25821_vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1012 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1014 if (unlikely(UNSET == dev->tuner_type))
1015 return -EINVAL;
1016 if (0 != t->index)
1017 return -EINVAL;
1019 strcpy(t->name, "Television");
1020 t->type = V4L2_TUNER_ANALOG_TV;
1021 t->capability = V4L2_TUNER_CAP_NORM;
1022 t->rangehigh = 0xffffffffUL;
1024 t->signal = 0xffff; /* LOCKED */
1025 return 0;
1028 int cx25821_vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1030 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1031 struct cx25821_fh *fh = priv;
1032 int err;
1034 if (fh) {
1035 err = v4l2_prio_check(&dev->prio, &fh->prio);
1036 if (0 != err)
1037 return err;
1040 dprintk(1, "%s()\n", __func__);
1041 if (UNSET == dev->tuner_type)
1042 return -EINVAL;
1043 if (0 != t->index)
1044 return -EINVAL;
1046 return 0;
1049 #endif
1050 // ******************************************************************************************
1051 static const struct v4l2_queryctrl no_ctl = {
1052 .name = "42",
1053 .flags = V4L2_CTRL_FLAG_DISABLED,
1056 static struct v4l2_queryctrl cx25821_ctls[] = {
1057 /* --- video --- */
1059 .id = V4L2_CID_BRIGHTNESS,
1060 .name = "Brightness",
1061 .minimum = 0,
1062 .maximum = 10000,
1063 .step = 1,
1064 .default_value = 6200,
1065 .type = V4L2_CTRL_TYPE_INTEGER,
1066 }, {
1067 .id = V4L2_CID_CONTRAST,
1068 .name = "Contrast",
1069 .minimum = 0,
1070 .maximum = 10000,
1071 .step = 1,
1072 .default_value = 5000,
1073 .type = V4L2_CTRL_TYPE_INTEGER,
1074 }, {
1075 .id = V4L2_CID_SATURATION,
1076 .name = "Saturation",
1077 .minimum = 0,
1078 .maximum = 10000,
1079 .step = 1,
1080 .default_value = 5000,
1081 .type = V4L2_CTRL_TYPE_INTEGER,
1082 }, {
1083 .id = V4L2_CID_HUE,
1084 .name = "Hue",
1085 .minimum = 0,
1086 .maximum = 10000,
1087 .step = 1,
1088 .default_value = 5000,
1089 .type = V4L2_CTRL_TYPE_INTEGER,
1092 static const int CX25821_CTLS = ARRAY_SIZE(cx25821_ctls);
1094 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
1096 int i;
1098 if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
1099 return -EINVAL;
1100 for (i = 0; i < CX25821_CTLS; i++)
1101 if (cx25821_ctls[i].id == qctrl->id)
1102 break;
1103 if (i == CX25821_CTLS) {
1104 *qctrl = no_ctl;
1105 return 0;
1107 *qctrl = cx25821_ctls[i];
1108 return 0;
1111 int cx25821_vidioc_queryctrl(struct file *file, void *priv,
1112 struct v4l2_queryctrl *qctrl)
1114 return cx25821_ctrl_query(qctrl);
1117 /* ------------------------------------------------------------------ */
1118 /* VIDEO CTRL IOCTLS */
1120 static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
1122 unsigned int i;
1124 for (i = 0; i < CX25821_CTLS; i++)
1125 if (cx25821_ctls[i].id == id)
1126 return cx25821_ctls + i;
1127 return NULL;
1130 int cx25821_vidioc_g_ctrl(struct file *file, void *priv, struct v4l2_control *ctl)
1132 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1134 const struct v4l2_queryctrl *ctrl;
1136 ctrl = ctrl_by_id(ctl->id);
1138 if (NULL == ctrl)
1139 return -EINVAL;
1140 switch (ctl->id) {
1141 case V4L2_CID_BRIGHTNESS:
1142 ctl->value = dev->ctl_bright;
1143 break;
1144 case V4L2_CID_HUE:
1145 ctl->value = dev->ctl_hue;
1146 break;
1147 case V4L2_CID_CONTRAST:
1148 ctl->value = dev->ctl_contrast;
1149 break;
1150 case V4L2_CID_SATURATION:
1151 ctl->value = dev->ctl_saturation;
1152 break;
1154 return 0;
1157 int cx25821_set_control(struct cx25821_dev *dev,
1158 struct v4l2_control *ctl, int chan_num)
1160 int err;
1161 const struct v4l2_queryctrl *ctrl;
1163 err = -EINVAL;
1165 ctrl = ctrl_by_id(ctl->id);
1167 if (NULL == ctrl)
1168 return err;
1170 switch (ctrl->type) {
1171 case V4L2_CTRL_TYPE_BOOLEAN:
1172 case V4L2_CTRL_TYPE_MENU:
1173 case V4L2_CTRL_TYPE_INTEGER:
1174 if (ctl->value < ctrl->minimum)
1175 ctl->value = ctrl->minimum;
1176 if (ctl->value > ctrl->maximum)
1177 ctl->value = ctrl->maximum;
1178 break;
1179 default:
1180 /* nothing */ ;
1183 switch (ctl->id) {
1184 case V4L2_CID_BRIGHTNESS:
1185 dev->ctl_bright = ctl->value;
1186 medusa_set_brightness(dev, ctl->value, chan_num);
1187 break;
1188 case V4L2_CID_HUE:
1189 dev->ctl_hue = ctl->value;
1190 medusa_set_hue(dev, ctl->value, chan_num);
1191 break;
1192 case V4L2_CID_CONTRAST:
1193 dev->ctl_contrast = ctl->value;
1194 medusa_set_contrast(dev, ctl->value, chan_num);
1195 break;
1196 case V4L2_CID_SATURATION:
1197 dev->ctl_saturation = ctl->value;
1198 medusa_set_saturation(dev, ctl->value, chan_num);
1199 break;
1202 err = 0;
1204 return err;
1207 static void init_controls(struct cx25821_dev *dev, int chan_num)
1209 struct v4l2_control ctrl;
1210 int i;
1211 for (i = 0; i < CX25821_CTLS; i++) {
1212 ctrl.id = cx25821_ctls[i].id;
1213 ctrl.value = cx25821_ctls[i].default_value;
1215 cx25821_set_control(dev, &ctrl, chan_num);
1219 int cx25821_vidioc_cropcap(struct file *file, void *priv, struct v4l2_cropcap *cropcap)
1221 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1223 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1224 return -EINVAL;
1225 cropcap->bounds.top = cropcap->bounds.left = 0;
1226 cropcap->bounds.width = 720;
1227 cropcap->bounds.height = dev->tvnorm == V4L2_STD_PAL_BG ? 576 : 480;
1228 cropcap->pixelaspect.numerator =
1229 dev->tvnorm == V4L2_STD_PAL_BG ? 59 : 10;
1230 cropcap->pixelaspect.denominator =
1231 dev->tvnorm == V4L2_STD_PAL_BG ? 54 : 11;
1232 cropcap->defrect = cropcap->bounds;
1233 return 0;
1236 int cx25821_vidioc_s_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1238 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1239 struct cx25821_fh *fh = priv;
1240 int err;
1242 if (fh) {
1243 err = v4l2_prio_check(&dev->prio, &fh->prio);
1244 if (0 != err)
1245 return err;
1247 // cx25821_vidioc_s_crop not supported
1248 return -EINVAL;
1251 int cx25821_vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1253 // cx25821_vidioc_g_crop not supported
1254 return -EINVAL;
1257 int cx25821_vidioc_querystd(struct file *file, void *priv, v4l2_std_id * norm)
1259 // medusa does not support video standard sensing of current input
1260 *norm = CX25821_NORMS;
1262 return 0;
1265 int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm)
1267 if (tvnorm == V4L2_STD_PAL_BG) {
1268 if (width == 352 || width == 720)
1269 return 1;
1270 else
1271 return 0;
1274 if (tvnorm == V4L2_STD_NTSC_M) {
1275 if (width == 320 || width == 352 || width == 720)
1276 return 1;
1277 else
1278 return 0;
1280 return 0;
1283 int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm)
1285 if (tvnorm == V4L2_STD_PAL_BG) {
1286 if (height == 576 || height == 288)
1287 return 1;
1288 else
1289 return 0;
1292 if (tvnorm == V4L2_STD_NTSC_M) {
1293 if (height == 480 || height == 240)
1294 return 1;
1295 else
1296 return 0;
1299 return 0;