V4L/DVB (12732): cx25821: fix bad whitespacing
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / cx25821 / cx25821-video.c
blobba8115b6e0aa10436cea8551e0f836a17747baa6
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,
85 int get_format_size(void)
87 return ARRAY_SIZE(formats);
91 struct cx25821_fmt *format_by_fourcc(unsigned int fourcc)
93 unsigned int i;
95 if( fourcc == V4L2_PIX_FMT_Y41P || fourcc == V4L2_PIX_FMT_YUV411P )
97 return formats+1;
100 for (i = 0; i < ARRAY_SIZE(formats); i++)
101 if (formats[i].fourcc == fourcc)
102 return formats+i;
104 printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __func__, fourcc);
105 return NULL;
108 void dump_video_queue(struct cx25821_dev *dev, struct cx25821_dmaqueue *q)
110 struct cx25821_buffer *buf;
111 struct list_head *item;
112 dprintk(1, "%s()\n", __func__);
114 if (!list_empty(&q->active)) {
115 list_for_each(item, &q->active)
116 buf = list_entry(item, struct cx25821_buffer, vb.queue);
119 if (!list_empty(&q->queued))
121 list_for_each(item, &q->queued)
122 buf = list_entry(item, struct cx25821_buffer, vb.queue);
128 void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q, u32 count)
130 struct cx25821_buffer *buf;
131 int bc;
133 for (bc = 0;; bc++) {
134 if (list_empty(&q->active))
136 dprintk(1, "bc=%d (=0: active empty)\n", bc);
137 break;
140 buf = list_entry(q->active.next, struct cx25821_buffer, vb.queue);
142 /* count comes from the hw and it is 16bit wide --
143 * this trick handles wrap-arounds correctly for
144 * up to 32767 buffers in flight... */
145 if ((s16) (count - buf->count) < 0)
147 break;
150 do_gettimeofday(&buf->vb.ts);
151 buf->vb.state = VIDEOBUF_DONE;
152 list_del(&buf->vb.queue);
153 wake_up(&buf->vb.done);
156 if (list_empty(&q->active))
157 del_timer(&q->timeout);
158 else
159 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
160 if (bc != 1)
161 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n", __func__, bc);
164 #ifdef TUNER_FLAG
165 int cx25821_set_tvnorm(struct cx25821_dev *dev, v4l2_std_id norm)
167 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n", __func__,
168 (unsigned int)norm,
169 v4l2_norm_to_name(norm));
171 dev->tvnorm = norm;
173 /* Tell the internal A/V decoder */
174 cx25821_call_all(dev, core, s_std, norm);
176 return 0;
178 #endif
180 struct video_device *cx25821_vdev_init(struct cx25821_dev *dev,
181 struct pci_dev *pci,
182 struct video_device *template,
183 char *type)
185 struct video_device *vfd;
186 dprintk(1, "%s()\n", __func__);
188 vfd = video_device_alloc();
189 if (NULL == vfd)
190 return NULL;
191 *vfd = *template;
192 vfd->minor = -1;
193 vfd->v4l2_dev = &dev->v4l2_dev;
194 vfd->release = video_device_release;
195 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name, type, cx25821_boards[dev->board].name);
196 return vfd;
200 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
202 int i;
204 if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
205 return -EINVAL;
206 for (i = 0; i < CX25821_CTLS; i++)
207 if (cx25821_ctls[i].v.id == qctrl->id)
208 break;
209 if (i == CX25821_CTLS) {
210 *qctrl = no_ctl;
211 return 0;
213 *qctrl = cx25821_ctls[i].v;
214 return 0;
218 // resource management
219 int res_get(struct cx25821_dev *dev, struct cx25821_fh *fh, unsigned int bit)
221 dprintk(1, "%s()\n", __func__);
222 if (fh->resources & bit)
223 /* have it already allocated */
224 return 1;
226 /* is it free? */
227 mutex_lock(&dev->lock);
228 if (dev->resources & bit) {
229 /* no, someone else uses it */
230 mutex_unlock(&dev->lock);
231 return 0;
233 /* it's free, grab it */
234 fh->resources |= bit;
235 dev->resources |= bit;
236 dprintk(1, "res: get %d\n", bit);
237 mutex_unlock(&dev->lock);
238 return 1;
241 int res_check(struct cx25821_fh *fh, unsigned int bit)
243 return fh->resources & bit;
246 int res_locked(struct cx25821_dev *dev, unsigned int bit)
248 return dev->resources & bit;
251 void res_free(struct cx25821_dev *dev, struct cx25821_fh *fh, unsigned int bits)
253 BUG_ON((fh->resources & bits) != bits);
254 dprintk(1, "%s()\n", __func__);
256 mutex_lock(&dev->lock);
257 fh->resources &= ~bits;
258 dev->resources &= ~bits;
259 dprintk(1, "res: put %d\n", bits);
260 mutex_unlock(&dev->lock);
263 int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input)
265 struct v4l2_routing route;
266 memset(&route, 0, sizeof(route));
268 dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n", __func__,
269 input, INPUT(input)->vmux,
270 INPUT(input)->gpio0, INPUT(input)->gpio1,
271 INPUT(input)->gpio2, INPUT(input)->gpio3);
272 dev->input = input;
274 route.input = INPUT(input)->vmux;
276 /* Tell the internal A/V decoder */
277 cx25821_call_all(dev, video, s_routing, INPUT(input)->vmux, 0, 0);
279 return 0;
282 int cx25821_start_video_dma(struct cx25821_dev *dev,
283 struct cx25821_dmaqueue *q,
284 struct cx25821_buffer *buf,
285 struct sram_channel *channel)
287 int tmp = 0;
289 /* setup fifo + format */
290 cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
292 /* reset counter */
293 cx_write(channel->gpcnt_ctl, 3);
294 q->count = 1;
296 /* enable irq */
297 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1<<channel->i));
298 cx_set(channel->int_msk, 0x11);
300 /* start dma */
301 cx_write(channel->dma_ctl, 0x11); /* FIFO and RISC enable */
303 /* make sure upstream setting if any is reversed */
304 tmp = cx_read( VID_CH_MODE_SEL );
305 cx_write( VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
307 return 0;
311 int cx25821_restart_video_queue(struct cx25821_dev *dev, struct cx25821_dmaqueue *q, struct sram_channel *channel)
313 struct cx25821_buffer *buf, *prev;
314 struct list_head *item;
316 if (!list_empty(&q->active)) {
317 buf = list_entry(q->active.next, struct cx25821_buffer, vb.queue);
319 cx25821_start_video_dma(dev, q, buf, channel);
321 list_for_each(item, &q->active) {
322 buf = list_entry(item, struct cx25821_buffer, vb.queue);
323 buf->count = q->count++;
326 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
327 return 0;
330 prev = NULL;
331 for (;;) {
332 if (list_empty(&q->queued))
333 return 0;
335 buf = list_entry(q->queued.next, struct cx25821_buffer, vb.queue);
337 if (NULL == prev) {
338 list_move_tail(&buf->vb.queue, &q->active);
339 cx25821_start_video_dma(dev, q, buf, channel);
340 buf->vb.state = VIDEOBUF_ACTIVE;
341 buf->count = q->count++;
342 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
343 } else if (prev->vb.width == buf->vb.width &&
344 prev->vb.height == buf->vb.height &&
345 prev->fmt == buf->fmt) {
346 list_move_tail(&buf->vb.queue, &q->active);
347 buf->vb.state = VIDEOBUF_ACTIVE;
348 buf->count = q->count++;
349 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
350 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
351 } else {
352 return 0;
354 prev = buf;
358 void cx25821_vid_timeout(unsigned long data)
360 struct cx25821_data *timeout_data = (struct cx25821_data *)data;
361 struct cx25821_dev *dev = timeout_data->dev;
362 struct sram_channel *channel = timeout_data->channel;
363 struct cx25821_dmaqueue *q = &dev->vidq[channel->i];
364 struct cx25821_buffer *buf;
365 unsigned long flags;
367 //cx25821_sram_channel_dump(dev, channel);
368 cx_clear(channel->dma_ctl, 0x11);
370 spin_lock_irqsave(&dev->slock, flags);
371 while (!list_empty(&q->active)) {
372 buf = list_entry(q->active.next, struct cx25821_buffer, vb.queue);
373 list_del(&buf->vb.queue);
375 buf->vb.state = VIDEOBUF_ERROR;
376 wake_up(&buf->vb.done);
379 cx25821_restart_video_queue(dev, q, channel);
380 spin_unlock_irqrestore(&dev->slock, flags);
383 int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
385 u32 count=0;
386 int handled = 0;
387 u32 mask;
388 struct sram_channel *channel = &dev->sram_channels[chan_num];
390 mask = cx_read(channel->int_msk);
391 if (0 == (status & mask))
392 return handled;
394 cx_write(channel->int_stat, status);
396 /* risc op code error */
397 if (status & (1 << 16)) {
398 printk(KERN_WARNING "%s, %s: video risc op code error\n", 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], channel);
417 spin_unlock(&dev->slock);
418 handled++;
420 return handled;
423 void cx25821_videoioctl_unregister(struct cx25821_dev *dev)
425 if( dev->ioctl_dev )
427 if (dev->ioctl_dev->minor != -1)
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 (-1 != dev->video_dev[chan_num]->minor)
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);
456 int cx25821_video_register(struct cx25821_dev *dev, int chan_num, 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 = (unsigned long)&dev->timeout_data[chan_num];
475 init_timer(&dev->vidq[chan_num].timeout);
476 cx25821_risc_stopper(dev->pci, &dev->vidq[chan_num].stopper, dev->sram_channels[chan_num].dma_ctl, 0x11, 0);
479 /* register v4l devices */
480 dev->video_dev[chan_num] = cx25821_vdev_init(dev, dev->pci, video_template, "video");
481 err = video_register_device(dev->video_dev[chan_num], VFL_TYPE_GRABBER, video_nr[dev->nr]);
483 if (err < 0) {
484 goto fail_unreg;
487 //set PCI interrupt
488 cx_set(PCI_INT_MSK, 0xff);
491 /* initial device configuration */
492 mutex_lock(&dev->lock);
493 #ifdef TUNER_FLAG
494 cx25821_set_tvnorm(dev, dev->tvnorm);
495 #endif
496 mutex_unlock(&dev->lock);
498 init_controls(dev, chan_num);
500 return 0;
502 fail_unreg:
503 cx25821_video_unregister(dev, chan_num);
504 return err;
507 int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
509 struct cx25821_fh *fh = q->priv_data;
511 *size = fh->fmt->depth*fh->width*fh->height >> 3;
514 if (0 == *count)
515 *count = 32;
517 while (*size * *count > vid_limit * 1024 * 1024)
518 (*count)--;
520 return 0;
523 int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb, enum v4l2_field field)
525 struct cx25821_fh *fh = q->priv_data;
526 struct cx25821_dev *dev = fh->dev;
527 struct cx25821_buffer *buf = container_of(vb, struct cx25821_buffer, vb);
528 int rc, init_buffer = 0;
529 u32 line0_offset, line1_offset;
530 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
531 int bpl_local = LINE_SIZE_D1;
532 int channel_opened = 0;
535 BUG_ON(NULL == fh->fmt);
536 if (fh->width < 48 || fh->width > 720 ||
537 fh->height < 32 || fh->height > 576)
538 return -EINVAL;
540 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
542 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
543 return -EINVAL;
546 if (buf->fmt != fh->fmt ||
547 buf->vb.width != fh->width ||
548 buf->vb.height != fh->height ||
549 buf->vb.field != field) {
550 buf->fmt = fh->fmt;
551 buf->vb.width = fh->width;
552 buf->vb.height = fh->height;
553 buf->vb.field = field;
554 init_buffer = 1;
557 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
558 init_buffer = 1;
559 rc = videobuf_iolock(q, &buf->vb, NULL);
560 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 || channel_opened > 7) ? 7 : channel_opened;
574 if( dev->pixel_formats[channel_opened] == PIXEL_FRMT_411 )
575 buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
576 else
577 buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
580 if( dev->pixel_formats[channel_opened] == PIXEL_FRMT_411 )
582 bpl_local = buf->bpl;
584 else
586 bpl_local = buf->bpl; //Default
588 if( channel_opened >= 0 && channel_opened <= 7 )
590 if( dev->use_cif_resolution[channel_opened] )
592 if( dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK )
593 bpl_local = 352 << 1;
594 else
595 bpl_local = dev->cif_width[channel_opened] << 1;
601 switch (buf->vb.field) {
602 case V4L2_FIELD_TOP:
603 cx25821_risc_buffer(dev->pci, &buf->risc,
604 dma->sglist, 0, UNSET,
605 buf->bpl, 0, buf->vb.height);
606 break;
607 case V4L2_FIELD_BOTTOM:
608 cx25821_risc_buffer(dev->pci, &buf->risc,
609 dma->sglist, UNSET, 0,
610 buf->bpl, 0, buf->vb.height);
611 break;
612 case V4L2_FIELD_INTERLACED:
613 /* All other formats are top field first */
614 line0_offset = 0;
615 line1_offset = buf->bpl;
616 dprintk(1, "top field first\n");
618 cx25821_risc_buffer(dev->pci, &buf->risc,
619 dma->sglist, line0_offset,
620 bpl_local, bpl_local, bpl_local,
621 buf->vb.height >> 1);
622 break;
623 case V4L2_FIELD_SEQ_TB:
624 cx25821_risc_buffer(dev->pci, &buf->risc,
625 dma->sglist,
626 0, buf->bpl * (buf->vb.height >> 1),
627 buf->bpl, 0,
628 buf->vb.height >> 1);
629 break;
630 case V4L2_FIELD_SEQ_BT:
631 cx25821_risc_buffer(dev->pci, &buf->risc,
632 dma->sglist,
633 buf->bpl * (buf->vb.height >> 1), 0,
634 buf->bpl, 0,
635 buf->vb.height >> 1);
636 break;
637 default:
638 BUG();
642 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
643 buf, buf->vb.i, fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
644 (unsigned long)buf->risc.dma);
646 buf->vb.state = VIDEOBUF_PREPARED;
648 return 0;
650 fail:
651 cx25821_free_buffer(q, buf);
652 return rc;
656 void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
658 struct cx25821_buffer *buf = container_of(vb, struct cx25821_buffer, vb);
660 cx25821_free_buffer(q, buf);
664 struct videobuf_queue *get_queue(struct cx25821_fh *fh)
666 switch (fh->type) {
667 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
668 return &fh->vidq;
669 default:
670 BUG();
671 return NULL;
675 int get_resource(struct cx25821_fh *fh, int resource)
677 switch (fh->type) {
678 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
679 return resource;
680 default:
681 BUG();
682 return 0;
687 int video_mmap(struct file *file, struct vm_area_struct *vma)
689 struct cx25821_fh *fh = file->private_data;
691 return videobuf_mmap_mapper(get_queue(fh), vma);
694 /* VIDEO IOCTLS */
695 int vidioc_g_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f)
697 struct cx25821_fh *fh = priv;
699 f->fmt.pix.width = fh->width;
700 f->fmt.pix.height = fh->height;
701 f->fmt.pix.field = fh->vidq.field;
702 f->fmt.pix.pixelformat = fh->fmt->fourcc;
703 f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3;
704 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
706 return 0;
709 int vidioc_try_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f)
711 struct cx25821_fmt *fmt;
712 enum v4l2_field field;
713 unsigned int maxw, maxh;
715 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
716 if (NULL == fmt)
717 return -EINVAL;
719 field = f->fmt.pix.field;
720 maxw = 720;
721 maxh = 576;
723 if (V4L2_FIELD_ANY == field) {
724 field = (f->fmt.pix.height > maxh/2)
725 ? V4L2_FIELD_INTERLACED
726 : V4L2_FIELD_TOP;
729 switch (field) {
730 case V4L2_FIELD_TOP:
731 case V4L2_FIELD_BOTTOM:
732 maxh = maxh / 2;
733 break;
734 case V4L2_FIELD_INTERLACED:
735 break;
736 default:
737 return -EINVAL;
740 f->fmt.pix.field = field;
741 if (f->fmt.pix.height < 32)
742 f->fmt.pix.height = 32;
743 if (f->fmt.pix.height > maxh)
744 f->fmt.pix.height = maxh;
745 if (f->fmt.pix.width < 48)
746 f->fmt.pix.width = 48;
747 if (f->fmt.pix.width > maxw)
748 f->fmt.pix.width = maxw;
749 f->fmt.pix.width &= ~0x03;
750 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
751 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
753 return 0;
758 int vidioc_querycap(struct file *file, void *priv, struct v4l2_capability *cap)
760 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
762 strcpy(cap->driver, "cx25821");
763 strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
764 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
765 cap->version = CX25821_VERSION_CODE;
766 cap->capabilities =
767 V4L2_CAP_VIDEO_CAPTURE |
768 V4L2_CAP_READWRITE |
769 V4L2_CAP_STREAMING;
770 if (UNSET != dev->tuner_type)
771 cap->capabilities |= V4L2_CAP_TUNER;
772 return 0;
775 int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, struct v4l2_fmtdesc *f)
777 if (unlikely(f->index >= ARRAY_SIZE(formats)))
778 return -EINVAL;
780 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
781 f->pixelformat = formats[f->index].fourcc;
783 return 0;
786 #ifdef CONFIG_VIDEO_V4L1_COMPAT
787 int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
789 struct cx25821_fh *fh = priv;
790 struct videobuf_queue *q;
791 struct v4l2_requestbuffers req;
792 unsigned int i;
793 int err;
795 q = get_queue(fh);
796 memset(&req, 0, sizeof(req));
797 req.type = q->type;
798 req.count = 8;
799 req.memory = V4L2_MEMORY_MMAP;
800 err = videobuf_reqbufs(q, &req);
801 if (err < 0)
802 return err;
804 mbuf->frames = req.count;
805 mbuf->size = 0;
806 for (i = 0; i < mbuf->frames; i++) {
807 mbuf->offsets[i] = q->bufs[i]->boff;
808 mbuf->size += q->bufs[i]->bsize;
810 return 0;
812 #endif
814 int vidioc_reqbufs(struct file *file, void *priv, struct v4l2_requestbuffers *p)
816 struct cx25821_fh *fh = priv;
817 return videobuf_reqbufs(get_queue(fh), p);
820 int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
822 struct cx25821_fh *fh = priv;
823 return videobuf_querybuf(get_queue(fh), p);
826 int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
828 struct cx25821_fh *fh = priv;
829 return videobuf_qbuf(get_queue(fh), p);
832 int vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
834 struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
836 *p = v4l2_prio_max(&dev->prio);
838 return 0;
841 int vidioc_s_priority(struct file *file, void *f,
842 enum v4l2_priority prio)
844 struct cx25821_fh *fh = f;
845 struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
847 return v4l2_prio_change(&dev->prio, &fh->prio, prio);
851 #ifdef TUNER_FLAG
852 int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
854 struct cx25821_fh *fh = priv;
855 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
856 int err;
858 dprintk(1, "%s()\n", __func__);
860 if (fh)
862 err = v4l2_prio_check(&dev->prio, &fh->prio);
863 if (0 != err)
864 return err;
867 if( dev->tvnorm == *tvnorms )
869 return 0;
872 mutex_lock(&dev->lock);
873 cx25821_set_tvnorm(dev, *tvnorms);
874 mutex_unlock(&dev->lock);
876 medusa_set_videostandard(dev);
878 return 0;
880 #endif
882 int cx25821_enum_input(struct cx25821_dev *dev, struct v4l2_input *i)
884 static const char *iname[] = {
885 [CX25821_VMUX_COMPOSITE] = "Composite",
886 [CX25821_VMUX_SVIDEO] = "S-Video",
887 [CX25821_VMUX_DEBUG] = "for debug only",
889 unsigned int n;
890 dprintk(1, "%s()\n", __func__);
892 n = i->index;
893 if (n > 2)
894 return -EINVAL;
896 if (0 == INPUT(n)->type)
897 return -EINVAL;
899 memset(i, 0, sizeof(*i));
900 i->index = n;
901 i->type = V4L2_INPUT_TYPE_CAMERA;
902 strcpy(i->name, iname[INPUT(n)->type]);
904 i->std = CX25821_NORMS;
905 return 0;
908 int vidioc_enum_input(struct file *file, void *priv, struct v4l2_input *i)
910 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
911 dprintk(1, "%s()\n", __func__);
912 return cx25821_enum_input(dev, i);
915 int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
917 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
919 *i = dev->input;
920 dprintk(1, "%s() returns %d\n", __func__, *i);
921 return 0;
925 int vidioc_s_input(struct file *file, void *priv, unsigned int i)
927 struct cx25821_fh *fh = priv;
928 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
929 int err;
931 dprintk(1, "%s(%d)\n", __func__, i);
933 if (fh)
935 err = v4l2_prio_check(&dev->prio, &fh->prio);
936 if (0 != err)
937 return err;
940 if (i > 2) {
941 dprintk(1, "%s() -EINVAL\n", __func__);
942 return -EINVAL;
945 mutex_lock(&dev->lock);
946 cx25821_video_mux(dev, i);
947 mutex_unlock(&dev->lock);
948 return 0;
951 #ifdef TUNER_FLAG
952 int vidioc_g_frequency(struct file *file, void *priv, struct v4l2_frequency *f)
954 struct cx25821_fh *fh = priv;
955 struct cx25821_dev *dev = fh->dev;
957 f->frequency = dev->freq;
959 cx25821_call_all(dev, tuner, g_frequency, f);
961 return 0;
964 int cx25821_set_freq(struct cx25821_dev *dev, struct v4l2_frequency *f)
966 mutex_lock(&dev->lock);
967 dev->freq = f->frequency;
969 cx25821_call_all(dev, tuner, s_frequency, f);
971 /* When changing channels it is required to reset TVAUDIO */
972 msleep(10);
974 mutex_unlock(&dev->lock);
976 return 0;
979 int vidioc_s_frequency(struct file *file, void *priv, struct v4l2_frequency *f)
981 struct cx25821_fh *fh = priv;
982 struct cx25821_dev *dev = fh->dev;
983 int err;
985 if (fh)
987 err = v4l2_prio_check(&dev->prio, &fh->prio);
988 if (0 != err)
989 return err;
992 return cx25821_set_freq(dev, f);
994 #endif
996 #ifdef CONFIG_VIDEO_ADV_DEBUG
997 int vidioc_g_register(struct file *file, void *fh,
998 struct v4l2_dbg_register *reg)
1000 struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1002 if (!v4l2_chip_match_host(&reg->match))
1003 return -EINVAL;
1005 cx25821_call_all(dev, core, g_register, reg);
1007 return 0;
1010 int vidioc_s_register(struct file *file, void *fh,
1011 struct v4l2_dbg_register *reg)
1013 struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1015 if (!v4l2_chip_match_host(&reg->match))
1016 return -EINVAL;
1018 cx25821_call_all(dev, core, s_register, reg);
1020 return 0;
1023 #endif
1026 #ifdef TUNER_FLAG
1027 int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1029 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1031 if (unlikely(UNSET == dev->tuner_type))
1032 return -EINVAL;
1033 if (0 != t->index)
1034 return -EINVAL;
1036 strcpy(t->name, "Television");
1037 t->type = V4L2_TUNER_ANALOG_TV;
1038 t->capability = V4L2_TUNER_CAP_NORM;
1039 t->rangehigh = 0xffffffffUL;
1041 t->signal = 0xffff ; /* LOCKED */
1042 return 0;
1045 int vidioc_s_tuner(struct file *file, void *priv,
1046 struct v4l2_tuner *t)
1048 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1049 struct cx25821_fh *fh = priv;
1050 int err;
1052 if (fh)
1054 err = v4l2_prio_check(&dev->prio, &fh->prio);
1055 if (0 != err)
1056 return err;
1059 dprintk(1, "%s()\n", __func__);
1060 if (UNSET == dev->tuner_type)
1061 return -EINVAL;
1062 if (0 != t->index)
1063 return -EINVAL;
1065 return 0;
1068 #endif
1069 // ******************************************************************************************
1070 static const struct v4l2_queryctrl no_ctl = {
1071 .name = "42",
1072 .flags = V4L2_CTRL_FLAG_DISABLED,
1075 static struct v4l2_queryctrl cx25821_ctls[] = {
1076 /* --- video --- */
1078 .id = V4L2_CID_BRIGHTNESS,
1079 .name = "Brightness",
1080 .minimum = 0,
1081 .maximum = 10000,
1082 .step = 1,
1083 .default_value = 6200,
1084 .type = V4L2_CTRL_TYPE_INTEGER,
1086 .id = V4L2_CID_CONTRAST,
1087 .name = "Contrast",
1088 .minimum = 0,
1089 .maximum = 10000,
1090 .step = 1,
1091 .default_value = 5000,
1092 .type = V4L2_CTRL_TYPE_INTEGER,
1094 .id = V4L2_CID_SATURATION,
1095 .name = "Saturation",
1096 .minimum = 0,
1097 .maximum = 10000,
1098 .step = 1,
1099 .default_value = 5000,
1100 .type = V4L2_CTRL_TYPE_INTEGER,
1102 .id = V4L2_CID_HUE,
1103 .name = "Hue",
1104 .minimum = 0,
1105 .maximum = 10000,
1106 .step = 1,
1107 .default_value = 5000,
1108 .type = V4L2_CTRL_TYPE_INTEGER,
1111 static const int CX25821_CTLS = ARRAY_SIZE(cx25821_ctls);
1113 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
1115 int i;
1117 if (qctrl->id < V4L2_CID_BASE ||
1118 qctrl->id >= V4L2_CID_LASTP1)
1119 return -EINVAL;
1120 for (i = 0; i < CX25821_CTLS; i++)
1121 if (cx25821_ctls[i].id == qctrl->id)
1122 break;
1123 if (i == CX25821_CTLS) {
1124 *qctrl = no_ctl;
1125 return 0;
1127 *qctrl = cx25821_ctls[i];
1128 return 0;
1131 int vidioc_queryctrl(struct file *file, void *priv,
1132 struct v4l2_queryctrl *qctrl)
1134 return cx25821_ctrl_query(qctrl);
1137 /* ------------------------------------------------------------------ */
1138 /* VIDEO CTRL IOCTLS */
1140 static const struct v4l2_queryctrl* ctrl_by_id(unsigned int id)
1142 unsigned int i;
1144 for (i = 0; i < CX25821_CTLS; i++)
1145 if (cx25821_ctls[i].id == id)
1146 return cx25821_ctls+i;
1147 return NULL;
1150 int vidioc_g_ctrl(struct file *file,
1151 void *priv,
1152 struct v4l2_control *ctl)
1154 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1156 const struct v4l2_queryctrl* ctrl;
1158 ctrl = ctrl_by_id(ctl->id);
1160 if (NULL == ctrl)
1161 return -EINVAL;
1162 switch (ctl->id)
1164 case V4L2_CID_BRIGHTNESS:
1165 ctl->value = dev->ctl_bright;
1166 break;
1167 case V4L2_CID_HUE:
1168 ctl->value = dev->ctl_hue;
1169 break;
1170 case V4L2_CID_CONTRAST:
1171 ctl->value = dev->ctl_contrast;
1172 break;
1173 case V4L2_CID_SATURATION:
1174 ctl->value = dev->ctl_saturation;
1175 break;
1177 return 0;
1180 int cx25821_set_control(struct cx25821_dev *dev,
1181 struct v4l2_control *ctl, int chan_num)
1183 int err;
1184 const struct v4l2_queryctrl* ctrl;
1186 err = -EINVAL;
1188 ctrl = ctrl_by_id(ctl->id);
1190 if (NULL == ctrl)
1191 return err;
1193 switch (ctrl->type)
1195 case V4L2_CTRL_TYPE_BOOLEAN:
1196 case V4L2_CTRL_TYPE_MENU:
1197 case V4L2_CTRL_TYPE_INTEGER:
1198 if (ctl->value < ctrl->minimum)
1199 ctl->value = ctrl->minimum;
1200 if (ctl->value > ctrl->maximum)
1201 ctl->value = ctrl->maximum;
1202 break;
1203 default:
1204 /* nothing */;
1207 switch (ctl->id)
1209 case V4L2_CID_BRIGHTNESS:
1210 dev->ctl_bright = ctl->value;
1211 medusa_set_brightness(dev, ctl->value, chan_num);
1212 break;
1213 case V4L2_CID_HUE:
1214 dev->ctl_hue = ctl->value;
1215 medusa_set_hue(dev, ctl->value, chan_num);
1216 break;
1217 case V4L2_CID_CONTRAST:
1218 dev->ctl_contrast = ctl->value;
1219 medusa_set_contrast(dev, ctl->value, chan_num);
1220 break;
1221 case V4L2_CID_SATURATION:
1222 dev->ctl_saturation = ctl->value;
1223 medusa_set_saturation(dev, ctl->value, chan_num);
1224 break;
1227 err = 0;
1229 return err;
1232 static void init_controls(struct cx25821_dev *dev, int chan_num)
1234 struct v4l2_control ctrl;
1235 int i;
1236 for (i = 0; i < CX25821_CTLS; i++) {
1237 ctrl.id = cx25821_ctls[i].id;
1238 ctrl.value = cx25821_ctls[i].default_value;
1240 cx25821_set_control(dev, &ctrl, chan_num);
1244 int vidioc_cropcap(struct file *file,
1245 void *priv,
1246 struct v4l2_cropcap *cropcap)
1248 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1250 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1251 return -EINVAL;
1252 cropcap->bounds.top = cropcap->bounds.left = 0;
1253 cropcap->bounds.width = 720;
1254 cropcap->bounds.height = dev->tvnorm == V4L2_STD_PAL_BG ? 576 : 480;
1255 cropcap->pixelaspect.numerator = dev->tvnorm == V4L2_STD_PAL_BG ? 59 : 10;
1256 cropcap->pixelaspect.denominator = dev->tvnorm == V4L2_STD_PAL_BG ? 54 : 11;
1257 cropcap->defrect = cropcap->bounds;
1258 return 0;
1261 int vidioc_s_crop(struct file *file,
1262 void *priv,
1263 struct v4l2_crop *crop)
1265 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1266 struct cx25821_fh *fh = priv;
1267 int err;
1269 if (fh)
1271 err = v4l2_prio_check(&dev->prio, &fh->prio);
1272 if (0 != err)
1273 return err;
1275 // vidioc_s_crop not supported
1276 return -EINVAL;
1279 int vidioc_g_crop(struct file *file,
1280 void *priv,
1281 struct v4l2_crop *crop)
1283 // vidioc_g_crop not supported
1284 return -EINVAL;
1287 int vidioc_querystd(struct file *file,
1288 void *priv,
1289 v4l2_std_id *norm)
1291 // medusa does not support video standard sensing of current input
1292 *norm = CX25821_NORMS;
1294 return 0;
1297 int is_valid_width(u32 width, v4l2_std_id tvnorm)
1299 if(tvnorm == V4L2_STD_PAL_BG)
1301 if (width == 352 || width == 720)
1302 return 1;
1303 else
1304 return 0;
1307 if(tvnorm == V4L2_STD_NTSC_M)
1309 if (width == 320 || width == 352 || width == 720)
1310 return 1;
1311 else
1312 return 0;
1314 return 0;
1317 int is_valid_height(u32 height, v4l2_std_id tvnorm)
1319 if(tvnorm == V4L2_STD_PAL_BG)
1321 if (height == 576 || height == 288)
1322 return 1;
1323 else
1324 return 0;
1327 if(tvnorm == V4L2_STD_NTSC_M)
1329 if (height == 480 || height == 240)
1330 return 1;
1331 else
1332 return 0;
1335 return 0;