Merge branch 'topic/misc' into for-linus
[pandora-kernel.git] / drivers / staging / cx25821 / cx25821-video.c
1 /*
2  *  Driver for the Conexant CX25821 PCIe bridge
3  *
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
7  *  Parts adapted/taken from Eduardo Moscoso Rubino
8  *  Copyright (C) 2009 Eduardo Moscoso Rubino <moscoso@TopoLogica.com>
9  *
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  */
26
27 #include "cx25821-video.h"
28
29 MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
30 MODULE_AUTHOR("Hiep Huynh <hiep.huynh@conexant.com>");
31 MODULE_LICENSE("GPL");
32
33 static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
34 static unsigned int radio_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
35
36 module_param_array(video_nr, int, NULL, 0444);
37 module_param_array(radio_nr, int, NULL, 0444);
38
39 MODULE_PARM_DESC(video_nr, "video device numbers");
40 MODULE_PARM_DESC(radio_nr, "radio device numbers");
41
42 static unsigned int video_debug = VIDEO_DEBUG;
43 module_param(video_debug, int, 0644);
44 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
45
46 static unsigned int irq_debug;
47 module_param(irq_debug, int, 0644);
48 MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
49
50 unsigned int vid_limit = 16;
51 module_param(vid_limit, int, 0644);
52 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
53
54 static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num);
55
56 static const struct v4l2_file_operations video_fops;
57 static const struct v4l2_ioctl_ops video_ioctl_ops;
58
59 #define FORMAT_FLAGS_PACKED       0x01
60
61 struct cx25821_fmt formats[] = {
62         {
63                 .name = "8 bpp, gray",
64                 .fourcc = V4L2_PIX_FMT_GREY,
65                 .depth = 8,
66                 .flags = FORMAT_FLAGS_PACKED,
67          }, {
68                 .name = "4:1:1, packed, Y41P",
69                 .fourcc = V4L2_PIX_FMT_Y41P,
70                 .depth = 12,
71                 .flags = FORMAT_FLAGS_PACKED,
72         }, {
73                 .name = "4:2:2, packed, YUYV",
74                 .fourcc = V4L2_PIX_FMT_YUYV,
75                 .depth = 16,
76                 .flags = FORMAT_FLAGS_PACKED,
77         }, {
78                 .name = "4:2:2, packed, UYVY",
79                 .fourcc = V4L2_PIX_FMT_UYVY,
80                 .depth = 16,
81                 .flags = FORMAT_FLAGS_PACKED,
82         }, {
83                 .name = "4:2:0, YUV",
84                 .fourcc = V4L2_PIX_FMT_YUV420,
85                 .depth = 12,
86                 .flags = FORMAT_FLAGS_PACKED,
87         },
88 };
89
90 int cx25821_get_format_size(void)
91 {
92         return ARRAY_SIZE(formats);
93 }
94
95 struct cx25821_fmt *cx25821_format_by_fourcc(unsigned int fourcc)
96 {
97         unsigned int i;
98
99         if (fourcc == V4L2_PIX_FMT_Y41P || fourcc == V4L2_PIX_FMT_YUV411P) {
100                 return formats + 1;
101         }
102
103         for (i = 0; i < ARRAY_SIZE(formats); i++)
104                 if (formats[i].fourcc == fourcc)
105                         return formats + i;
106
107         printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __func__, fourcc);
108         return NULL;
109 }
110
111 void cx25821_dump_video_queue(struct cx25821_dev *dev, struct cx25821_dmaqueue *q)
112 {
113         struct cx25821_buffer *buf;
114         struct list_head *item;
115         dprintk(1, "%s()\n", __func__);
116
117         if (!list_empty(&q->active)) {
118                 list_for_each(item, &q->active)
119                     buf = list_entry(item, struct cx25821_buffer, vb.queue);
120         }
121
122         if (!list_empty(&q->queued)) {
123                 list_for_each(item, &q->queued)
124                     buf = list_entry(item, struct cx25821_buffer, vb.queue);
125         }
126
127 }
128
129 void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q,
130                           u32 count)
131 {
132         struct cx25821_buffer *buf;
133         int bc;
134
135         for (bc = 0;; bc++) {
136                 if (list_empty(&q->active)) {
137                         dprintk(1, "bc=%d (=0: active empty)\n", bc);
138                         break;
139                 }
140
141                 buf =
142                     list_entry(q->active.next, struct cx25821_buffer, vb.queue);
143
144                 /* count comes from the hw and it is 16bit wide --
145                  * this trick handles wrap-arounds correctly for
146                  * up to 32767 buffers in flight... */
147                 if ((s16) (count - buf->count) < 0) {
148                         break;
149                 }
150
151                 do_gettimeofday(&buf->vb.ts);
152                 buf->vb.state = VIDEOBUF_DONE;
153                 list_del(&buf->vb.queue);
154                 wake_up(&buf->vb.done);
155         }
156
157         if (list_empty(&q->active))
158                 del_timer(&q->timeout);
159         else
160                 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
161         if (bc != 1)
162                 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
163                        __func__, bc);
164 }
165
166 #ifdef TUNER_FLAG
167 int cx25821_set_tvnorm(struct cx25821_dev *dev, v4l2_std_id norm)
168 {
169         dprintk(1, "%s(norm = 0x%08x) name: [%s]\n", __func__,
170                 (unsigned int)norm, v4l2_norm_to_name(norm));
171
172         dev->tvnorm = norm;
173
174         /* Tell the internal A/V decoder */
175         cx25821_call_all(dev, core, s_std, norm);
176
177         return 0;
178 }
179 #endif
180
181 struct video_device *cx25821_vdev_init(struct cx25821_dev *dev,
182                                        struct pci_dev *pci,
183                                        struct video_device *template,
184                                        char *type)
185 {
186         struct video_device *vfd;
187         dprintk(1, "%s()\n", __func__);
188
189         vfd = video_device_alloc();
190         if (NULL == vfd)
191                 return NULL;
192         *vfd = *template;
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,
196                  cx25821_boards[dev->board].name);
197         video_set_drvdata(vfd, dev);
198         return vfd;
199 }
200
201 /*
202 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
203 {
204     int i;
205
206     if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
207         return -EINVAL;
208     for (i = 0; i < CX25821_CTLS; i++)
209         if (cx25821_ctls[i].v.id == qctrl->id)
210             break;
211     if (i == CX25821_CTLS) {
212         *qctrl = no_ctl;
213         return 0;
214     }
215     *qctrl = cx25821_ctls[i].v;
216     return 0;
217 }
218 */
219
220 /* resource management */
221 int cx25821_res_get(struct cx25821_dev *dev, struct cx25821_fh *fh, unsigned int bit)
222 {
223         dprintk(1, "%s()\n", __func__);
224         if (fh->resources & bit)
225                 /* have it already allocated */
226                 return 1;
227
228         /* is it free? */
229         mutex_lock(&dev->lock);
230        if (dev->channels[fh->channel_id].resources & bit) {
231                 /* no, someone else uses it */
232                 mutex_unlock(&dev->lock);
233                 return 0;
234         }
235         /* it's free, grab it */
236         fh->resources |= bit;
237        dev->channels[fh->channel_id].resources |= bit;
238         dprintk(1, "res: get %d\n", bit);
239         mutex_unlock(&dev->lock);
240         return 1;
241 }
242
243 int cx25821_res_check(struct cx25821_fh *fh, unsigned int bit)
244 {
245         return fh->resources & bit;
246 }
247
248 int cx25821_res_locked(struct cx25821_fh *fh, unsigned int bit)
249 {
250        return fh->dev->channels[fh->channel_id].resources & bit;
251 }
252
253 void cx25821_res_free(struct cx25821_dev *dev, struct cx25821_fh *fh, unsigned int bits)
254 {
255         BUG_ON((fh->resources & bits) != bits);
256         dprintk(1, "%s()\n", __func__);
257
258         mutex_lock(&dev->lock);
259         fh->resources &= ~bits;
260        dev->channels[fh->channel_id].resources &= ~bits;
261         dprintk(1, "res: put %d\n", bits);
262         mutex_unlock(&dev->lock);
263 }
264
265 int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input)
266 {
267         struct v4l2_routing route;
268         memset(&route, 0, sizeof(route));
269
270         dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
271                 __func__, input, INPUT(input)->vmux, INPUT(input)->gpio0,
272                 INPUT(input)->gpio1, INPUT(input)->gpio2, INPUT(input)->gpio3);
273         dev->input = input;
274
275         route.input = INPUT(input)->vmux;
276
277         /* Tell the internal A/V decoder */
278         cx25821_call_all(dev, video, s_routing, INPUT(input)->vmux, 0, 0);
279
280         return 0;
281 }
282
283 int cx25821_start_video_dma(struct cx25821_dev *dev,
284                             struct cx25821_dmaqueue *q,
285                             struct cx25821_buffer *buf,
286                             struct sram_channel *channel)
287 {
288         int tmp = 0;
289
290         /* setup fifo + format */
291         cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
292
293         /* reset counter */
294         cx_write(channel->gpcnt_ctl, 3);
295         q->count = 1;
296
297         /* enable irq */
298         cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
299         cx_set(channel->int_msk, 0x11);
300
301         /* start dma */
302         cx_write(channel->dma_ctl, 0x11);       /* FIFO and RISC enable */
303
304         /* make sure upstream setting if any is reversed */
305         tmp = cx_read(VID_CH_MODE_SEL);
306         cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
307
308         return 0;
309 }
310
311 int cx25821_restart_video_queue(struct cx25821_dev *dev,
312                                 struct cx25821_dmaqueue *q,
313                                 struct sram_channel *channel)
314 {
315         struct cx25821_buffer *buf, *prev;
316         struct list_head *item;
317
318         if (!list_empty(&q->active)) {
319                 buf =
320                     list_entry(q->active.next, struct cx25821_buffer, vb.queue);
321
322                 cx25821_start_video_dma(dev, q, buf, channel);
323
324                 list_for_each(item, &q->active) {
325                         buf = list_entry(item, struct cx25821_buffer, vb.queue);
326                         buf->count = q->count++;
327                 }
328
329                 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
330                 return 0;
331         }
332
333         prev = NULL;
334         for (;;) {
335                 if (list_empty(&q->queued))
336                         return 0;
337
338                 buf =
339                     list_entry(q->queued.next, struct cx25821_buffer, vb.queue);
340
341                 if (NULL == prev) {
342                         list_move_tail(&buf->vb.queue, &q->active);
343                         cx25821_start_video_dma(dev, q, buf, channel);
344                         buf->vb.state = VIDEOBUF_ACTIVE;
345                         buf->count = q->count++;
346                         mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
347                 } else if (prev->vb.width == buf->vb.width &&
348                            prev->vb.height == buf->vb.height &&
349                            prev->fmt == buf->fmt) {
350                         list_move_tail(&buf->vb.queue, &q->active);
351                         buf->vb.state = VIDEOBUF_ACTIVE;
352                         buf->count = q->count++;
353                         prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
354                         prev->risc.jmp[2] = cpu_to_le32(0);     /* Bits 63 - 32 */
355                 } else {
356                         return 0;
357                 }
358                 prev = buf;
359         }
360 }
361
362 void cx25821_vid_timeout(unsigned long data)
363 {
364         struct cx25821_data *timeout_data = (struct cx25821_data *)data;
365         struct cx25821_dev *dev = timeout_data->dev;
366         struct sram_channel *channel = timeout_data->channel;
367        struct cx25821_dmaqueue *q = &dev->channels[channel->i].vidq;
368         struct cx25821_buffer *buf;
369         unsigned long flags;
370
371        /* cx25821_sram_channel_dump(dev, channel); */
372         cx_clear(channel->dma_ctl, 0x11);
373
374         spin_lock_irqsave(&dev->slock, flags);
375         while (!list_empty(&q->active)) {
376                 buf =
377                     list_entry(q->active.next, struct cx25821_buffer, vb.queue);
378                 list_del(&buf->vb.queue);
379
380                 buf->vb.state = VIDEOBUF_ERROR;
381                 wake_up(&buf->vb.done);
382         }
383
384         cx25821_restart_video_queue(dev, q, channel);
385         spin_unlock_irqrestore(&dev->slock, flags);
386 }
387
388 int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
389 {
390         u32 count = 0;
391         int handled = 0;
392         u32 mask;
393        struct sram_channel *channel = dev->channels[chan_num].sram_channels;
394
395         mask = cx_read(channel->int_msk);
396         if (0 == (status & mask))
397                 return handled;
398
399         cx_write(channel->int_stat, status);
400
401         /* risc op code error */
402         if (status & (1 << 16)) {
403                 printk(KERN_WARNING "%s, %s: video risc op code error\n",
404                        dev->name, channel->name);
405                 cx_clear(channel->dma_ctl, 0x11);
406                 cx25821_sram_channel_dump(dev, channel);
407         }
408
409         /* risc1 y */
410         if (status & FLD_VID_DST_RISC1) {
411                 spin_lock(&dev->slock);
412                 count = cx_read(channel->gpcnt);
413                cx25821_video_wakeup(dev,
414                        &dev->channels[channel->i].vidq, count);
415                 spin_unlock(&dev->slock);
416                 handled++;
417         }
418
419         /* risc2 y */
420         if (status & 0x10) {
421                 dprintk(2, "stopper video\n");
422                 spin_lock(&dev->slock);
423                cx25821_restart_video_queue(dev,
424                                &dev->channels[channel->i].vidq,
425                                        channel);
426                 spin_unlock(&dev->slock);
427                 handled++;
428         }
429         return handled;
430 }
431
432 void cx25821_videoioctl_unregister(struct cx25821_dev *dev)
433 {
434         if (dev->ioctl_dev) {
435                 if (video_is_registered(dev->ioctl_dev))
436                         video_unregister_device(dev->ioctl_dev);
437                 else
438                         video_device_release(dev->ioctl_dev);
439
440                 dev->ioctl_dev = NULL;
441         }
442 }
443
444 void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
445 {
446         cx_clear(PCI_INT_MSK, 1);
447
448        if (dev->channels[chan_num].video_dev) {
449                if (video_is_registered(dev->channels[chan_num].video_dev))
450                        video_unregister_device(
451                                dev->channels[chan_num].video_dev);
452                 else
453                        video_device_release(
454                                dev->channels[chan_num].video_dev);
455
456                dev->channels[chan_num].video_dev = NULL;
457
458                btcx_riscmem_free(dev->pci,
459                        &dev->channels[chan_num].vidq.stopper);
460
461                 printk(KERN_WARNING "device %d released!\n", chan_num);
462         }
463
464 }
465
466 int cx25821_video_register(struct cx25821_dev *dev)
467 {
468         int err;
469        int i;
470
471        struct video_device cx25821_video_device = {
472                .name = "cx25821-video",
473                .fops = &video_fops,
474                .minor = -1,
475                .ioctl_ops = &video_ioctl_ops,
476                .tvnorms = CX25821_NORMS,
477                .current_norm = V4L2_STD_NTSC_M,
478        };
479
480         spin_lock_init(&dev->slock);
481
482     for (i = 0; i < MAX_VID_CHANNEL_NUM - 1; ++i) {
483                cx25821_init_controls(dev, i);
484
485                cx25821_risc_stopper(dev->pci,
486                                &dev->channels[i].vidq.stopper,
487                                dev->channels[i].sram_channels->dma_ctl,
488                                0x11, 0);
489
490                dev->channels[i].sram_channels = &cx25821_sram_channels[i];
491                dev->channels[i].video_dev = NULL;
492                dev->channels[i].resources = 0;
493
494                cx_write(dev->channels[i].sram_channels->int_stat,
495                                0xffffffff);
496
497                INIT_LIST_HEAD(&dev->channels[i].vidq.active);
498                INIT_LIST_HEAD(&dev->channels[i].vidq.queued);
499
500                dev->channels[i].timeout_data.dev = dev;
501                dev->channels[i].timeout_data.channel =
502                                        &cx25821_sram_channels[i];
503                dev->channels[i].vidq.timeout.function =
504                                        cx25821_vid_timeout;
505                dev->channels[i].vidq.timeout.data =
506                        (unsigned long)&dev->channels[i].timeout_data;
507                init_timer(&dev->channels[i].vidq.timeout);
508
509                /* register v4l devices */
510                dev->channels[i].video_dev = cx25821_vdev_init(dev,
511                        dev->pci, &cx25821_video_device, "video");
512
513                err = video_register_device(dev->channels[i].video_dev,
514                                VFL_TYPE_GRABBER, video_nr[dev->nr]);
515
516                if (err < 0)
517                        goto fail_unreg;
518
519         }
520
521     /* set PCI interrupt */
522         cx_set(PCI_INT_MSK, 0xff);
523
524         /* initial device configuration */
525         mutex_lock(&dev->lock);
526 #ifdef TUNER_FLAG
527        dev->tvnorm = cx25821_video_device.current_norm;
528         cx25821_set_tvnorm(dev, dev->tvnorm);
529 #endif
530         mutex_unlock(&dev->lock);
531
532
533     return 0;
534
535 fail_unreg:
536        cx25821_video_unregister(dev, i);
537         return err;
538 }
539
540 int cx25821_buffer_setup(struct videobuf_queue *q, unsigned int *count,
541                  unsigned int *size)
542 {
543         struct cx25821_fh *fh = q->priv_data;
544
545         *size = fh->fmt->depth * fh->width * fh->height >> 3;
546
547         if (0 == *count)
548                 *count = 32;
549
550         if (*size * *count > vid_limit * 1024 * 1024)
551                 *count = (vid_limit * 1024 * 1024) / *size;
552
553         return 0;
554 }
555
556 int cx25821_buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
557                    enum v4l2_field field)
558 {
559         struct cx25821_fh *fh = q->priv_data;
560         struct cx25821_dev *dev = fh->dev;
561         struct cx25821_buffer *buf =
562             container_of(vb, struct cx25821_buffer, vb);
563         int rc, init_buffer = 0;
564         u32 line0_offset, line1_offset;
565         struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
566         int bpl_local = LINE_SIZE_D1;
567        int channel_opened = fh->channel_id;
568
569         BUG_ON(NULL == fh->fmt);
570         if (fh->width < 48 || fh->width > 720 ||
571             fh->height < 32 || fh->height > 576)
572                 return -EINVAL;
573
574         buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
575
576         if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
577                 return -EINVAL;
578
579         if (buf->fmt != fh->fmt ||
580             buf->vb.width != fh->width ||
581             buf->vb.height != fh->height || buf->vb.field != field) {
582                 buf->fmt = fh->fmt;
583                 buf->vb.width = fh->width;
584                 buf->vb.height = fh->height;
585                 buf->vb.field = field;
586                 init_buffer = 1;
587         }
588
589         if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
590                 init_buffer = 1;
591                 rc = videobuf_iolock(q, &buf->vb, NULL);
592                 if (0 != rc) {
593                         printk(KERN_DEBUG "videobuf_iolock failed!\n");
594                         goto fail;
595                 }
596         }
597
598         dprintk(1, "init_buffer=%d\n", init_buffer);
599
600         if (init_buffer) {
601
602                 channel_opened = dev->channel_opened;
603                 channel_opened = (channel_opened < 0
604                                   || channel_opened > 7) ? 7 : channel_opened;
605
606                if (dev->channels[channel_opened]
607                        .pixel_formats == PIXEL_FRMT_411)
608                         buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
609                 else
610                         buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
611
612                if (dev->channels[channel_opened]
613                        .pixel_formats == PIXEL_FRMT_411) {
614                         bpl_local = buf->bpl;
615                 } else {
616                        bpl_local = buf->bpl;   /* Default */
617
618                         if (channel_opened >= 0 && channel_opened <= 7) {
619                                if (dev->channels[channel_opened]
620                                                .use_cif_resolution) {
621                                         if (dev->tvnorm & V4L2_STD_PAL_BG
622                                             || dev->tvnorm & V4L2_STD_PAL_DK)
623                                                 bpl_local = 352 << 1;
624                                         else
625                                                 bpl_local =
626                                                  dev->channels[channel_opened].
627                                                  cif_width <<
628                                                  1;
629                                 }
630                         }
631                 }
632
633                 switch (buf->vb.field) {
634                 case V4L2_FIELD_TOP:
635                         cx25821_risc_buffer(dev->pci, &buf->risc,
636                                             dma->sglist, 0, UNSET,
637                                             buf->bpl, 0, buf->vb.height);
638                         break;
639                 case V4L2_FIELD_BOTTOM:
640                         cx25821_risc_buffer(dev->pci, &buf->risc,
641                                             dma->sglist, UNSET, 0,
642                                             buf->bpl, 0, buf->vb.height);
643                         break;
644                 case V4L2_FIELD_INTERLACED:
645                         /* All other formats are top field first */
646                         line0_offset = 0;
647                         line1_offset = buf->bpl;
648                         dprintk(1, "top field first\n");
649
650                         cx25821_risc_buffer(dev->pci, &buf->risc,
651                                             dma->sglist, line0_offset,
652                                             bpl_local, bpl_local, bpl_local,
653                                             buf->vb.height >> 1);
654                         break;
655                 case V4L2_FIELD_SEQ_TB:
656                         cx25821_risc_buffer(dev->pci, &buf->risc,
657                                             dma->sglist,
658                                             0, buf->bpl * (buf->vb.height >> 1),
659                                             buf->bpl, 0, buf->vb.height >> 1);
660                         break;
661                 case V4L2_FIELD_SEQ_BT:
662                         cx25821_risc_buffer(dev->pci, &buf->risc,
663                                             dma->sglist,
664                                             buf->bpl * (buf->vb.height >> 1), 0,
665                                             buf->bpl, 0, buf->vb.height >> 1);
666                         break;
667                 default:
668                         BUG();
669                 }
670         }
671
672         dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
673                 buf, buf->vb.i, fh->width, fh->height, fh->fmt->depth,
674                 fh->fmt->name, (unsigned long)buf->risc.dma);
675
676         buf->vb.state = VIDEOBUF_PREPARED;
677
678         return 0;
679
680       fail:
681         cx25821_free_buffer(q, buf);
682         return rc;
683 }
684
685 void cx25821_buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
686 {
687         struct cx25821_buffer *buf =
688             container_of(vb, struct cx25821_buffer, vb);
689
690         cx25821_free_buffer(q, buf);
691 }
692
693 struct videobuf_queue *get_queue(struct cx25821_fh *fh)
694 {
695         switch (fh->type) {
696         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
697                 return &fh->vidq;
698         default:
699                 BUG();
700                 return NULL;
701         }
702 }
703
704 int cx25821_get_resource(struct cx25821_fh *fh, int resource)
705 {
706         switch (fh->type) {
707         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
708                 return resource;
709         default:
710                 BUG();
711                 return 0;
712         }
713 }
714
715 int cx25821_video_mmap(struct file *file, struct vm_area_struct *vma)
716 {
717         struct cx25821_fh *fh = file->private_data;
718
719         return videobuf_mmap_mapper(get_queue(fh), vma);
720 }
721
722
723 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
724 {
725        struct cx25821_buffer *buf =
726            container_of(vb, struct cx25821_buffer, vb);
727        struct cx25821_buffer *prev;
728        struct cx25821_fh *fh = vq->priv_data;
729        struct cx25821_dev *dev = fh->dev;
730        struct cx25821_dmaqueue *q = &dev->channels[fh->channel_id].vidq;
731
732        /* add jump to stopper */
733        buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
734        buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
735        buf->risc.jmp[2] = cpu_to_le32(0);      /* bits 63-32 */
736
737        dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
738
739        if (!list_empty(&q->queued)) {
740                list_add_tail(&buf->vb.queue, &q->queued);
741                buf->vb.state = VIDEOBUF_QUEUED;
742                dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
743                        buf->vb.i);
744
745        } else if (list_empty(&q->active)) {
746                list_add_tail(&buf->vb.queue, &q->active);
747                cx25821_start_video_dma(dev, q, buf,
748                                        dev->channels[fh->channel_id].
749                                        sram_channels);
750                buf->vb.state = VIDEOBUF_ACTIVE;
751                buf->count = q->count++;
752                mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
753                dprintk(2,
754                        "[%p/%d] buffer_queue - first active, buf cnt = %d, \
755                        q->count = %d\n",
756                        buf, buf->vb.i, buf->count, q->count);
757        } else {
758                prev =
759                    list_entry(q->active.prev, struct cx25821_buffer, vb.queue);
760                if (prev->vb.width == buf->vb.width
761                    && prev->vb.height == buf->vb.height
762                    && prev->fmt == buf->fmt) {
763                        list_add_tail(&buf->vb.queue, &q->active);
764                        buf->vb.state = VIDEOBUF_ACTIVE;
765                        buf->count = q->count++;
766                        prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
767
768                        /* 64 bit bits 63-32 */
769                        prev->risc.jmp[2] = cpu_to_le32(0);
770                        dprintk(2,
771                                "[%p/%d] buffer_queue - append to active, \
772                                buf->count=%d\n",
773                                buf, buf->vb.i, buf->count);
774
775                } else {
776                        list_add_tail(&buf->vb.queue, &q->queued);
777                        buf->vb.state = VIDEOBUF_QUEUED;
778                        dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
779                                buf->vb.i);
780                }
781        }
782
783        if (list_empty(&q->active))
784                dprintk(2, "active queue empty!\n");
785 }
786
787 static struct videobuf_queue_ops cx25821_video_qops = {
788        .buf_setup = cx25821_buffer_setup,
789        .buf_prepare = cx25821_buffer_prepare,
790        .buf_queue = buffer_queue,
791        .buf_release = cx25821_buffer_release,
792 };
793
794 static int video_open(struct file *file)
795 {
796        struct video_device *vdev = video_devdata(file);
797        struct cx25821_dev *h, *dev = video_drvdata(file);
798        struct cx25821_fh *fh;
799        struct list_head *list;
800        int minor = video_devdata(file)->minor;
801        enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
802        u32 pix_format;
803        int ch_id = 0;
804        int i;
805
806        dprintk(1, "open dev=%s type=%s\n",
807                        video_device_node_name(vdev),
808                        v4l2_type_names[type]);
809
810        /* allocate + initialize per filehandle data */
811        fh = kzalloc(sizeof(*fh), GFP_KERNEL);
812        if (NULL == fh)
813                return -ENOMEM;
814
815        lock_kernel();
816
817        list_for_each(list, &cx25821_devlist)
818        {
819                h = list_entry(list, struct cx25821_dev, devlist);
820
821                for (i = 0; i < MAX_VID_CHANNEL_NUM; i++) {
822                        if (h->channels[i].video_dev &&
823                            h->channels[i].video_dev->minor == minor) {
824                                dev = h;
825                                ch_id = i;
826                                type  = V4L2_BUF_TYPE_VIDEO_CAPTURE;
827                        }
828                }
829        }
830
831        if (NULL == dev) {
832                unlock_kernel();
833                return -ENODEV;
834        }
835
836        file->private_data = fh;
837        fh->dev = dev;
838        fh->type = type;
839        fh->width = 720;
840     fh->channel_id = ch_id;
841
842        if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
843                fh->height = 576;
844        else
845                fh->height = 480;
846
847        dev->channel_opened = fh->channel_id;
848        pix_format =
849            (dev->channels[ch_id].pixel_formats ==
850             PIXEL_FRMT_411) ? V4L2_PIX_FMT_Y41P : V4L2_PIX_FMT_YUYV;
851        fh->fmt = cx25821_format_by_fourcc(pix_format);
852
853        v4l2_prio_open(&dev->channels[ch_id].prio, &fh->prio);
854
855        videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops,
856                               &dev->pci->dev, &dev->slock,
857                               V4L2_BUF_TYPE_VIDEO_CAPTURE,
858                               V4L2_FIELD_INTERLACED,
859                               sizeof(struct cx25821_buffer), fh, NULL);
860
861        dprintk(1, "post videobuf_queue_init()\n");
862        unlock_kernel();
863
864        return 0;
865 }
866
867 static ssize_t video_read(struct file *file, char __user * data, size_t count,
868                          loff_t *ppos)
869 {
870        struct cx25821_fh *fh = file->private_data;
871
872        switch (fh->type) {
873        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
874                if (cx25821_res_locked(fh, RESOURCE_VIDEO0))
875                        return -EBUSY;
876
877                return videobuf_read_one(&fh->vidq, data, count, ppos,
878                                         file->f_flags & O_NONBLOCK);
879
880        default:
881                BUG();
882                return 0;
883        }
884 }
885
886 static unsigned int video_poll(struct file *file,
887                               struct poll_table_struct *wait)
888 {
889        struct cx25821_fh *fh = file->private_data;
890        struct cx25821_buffer *buf;
891
892        if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
893                /* streaming capture */
894                if (list_empty(&fh->vidq.stream))
895                        return POLLERR;
896                buf = list_entry(fh->vidq.stream.next,
897                                 struct cx25821_buffer, vb.stream);
898        } else {
899                /* read() capture */
900                buf = (struct cx25821_buffer *)fh->vidq.read_buf;
901                if (NULL == buf)
902                        return POLLERR;
903        }
904
905        poll_wait(file, &buf->vb.done, wait);
906        if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR) {
907                if (buf->vb.state == VIDEOBUF_DONE) {
908                        struct cx25821_dev *dev = fh->dev;
909
910                        if (dev && dev->channels[fh->channel_id]
911                                                .use_cif_resolution) {
912                                u8 cam_id = *((char *)buf->vb.baddr + 3);
913                                memcpy((char *)buf->vb.baddr,
914                                       (char *)buf->vb.baddr + (fh->width * 2),
915                                       (fh->width * 2));
916                                *((char *)buf->vb.baddr + 3) = cam_id;
917                        }
918                }
919
920                return POLLIN | POLLRDNORM;
921        }
922
923        return 0;
924 }
925
926 static int video_release(struct file *file)
927 {
928        struct cx25821_fh *fh = file->private_data;
929        struct cx25821_dev *dev = fh->dev;
930
931        /* stop the risc engine and fifo */
932        cx_write(channel0->dma_ctl, 0); /* FIFO and RISC disable */
933
934        /* stop video capture */
935        if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
936                videobuf_queue_cancel(&fh->vidq);
937                cx25821_res_free(dev, fh, RESOURCE_VIDEO0);
938        }
939
940        if (fh->vidq.read_buf) {
941                cx25821_buffer_release(&fh->vidq, fh->vidq.read_buf);
942                kfree(fh->vidq.read_buf);
943        }
944
945        videobuf_mmap_free(&fh->vidq);
946
947        v4l2_prio_close(&dev->channels[fh->channel_id].prio, fh->prio);
948        file->private_data = NULL;
949        kfree(fh);
950
951        return 0;
952 }
953
954 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
955 {
956        struct cx25821_fh *fh = priv;
957        struct cx25821_dev *dev = fh->dev;
958
959        if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
960                return -EINVAL;
961
962        if (unlikely(i != fh->type))
963                return -EINVAL;
964
965        if (unlikely(!cx25821_res_get(dev, fh,
966                        cx25821_get_resource(fh, RESOURCE_VIDEO0))))
967                return -EBUSY;
968
969        return videobuf_streamon(get_queue(fh));
970 }
971
972 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
973 {
974        struct cx25821_fh *fh = priv;
975        struct cx25821_dev *dev = fh->dev;
976        int err, res;
977
978        if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
979                return -EINVAL;
980        if (i != fh->type)
981                return -EINVAL;
982
983        res = cx25821_get_resource(fh, RESOURCE_VIDEO0);
984        err = videobuf_streamoff(get_queue(fh));
985        if (err < 0)
986                return err;
987        cx25821_res_free(dev, fh, res);
988        return 0;
989 }
990
991 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
992                                struct v4l2_format *f)
993 {
994        struct cx25821_fh *fh = priv;
995        struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
996         struct v4l2_mbus_framefmt mbus_fmt;
997        int err;
998        int pix_format = PIXEL_FRMT_422;
999
1000        if (fh) {
1001                err = v4l2_prio_check(&dev->channels[fh->channel_id]
1002                                                .prio, fh->prio);
1003                if (0 != err)
1004                        return err;
1005        }
1006
1007        dprintk(2, "%s()\n", __func__);
1008        err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
1009
1010        if (0 != err)
1011                return err;
1012
1013        fh->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
1014        fh->vidq.field = f->fmt.pix.field;
1015
1016        /* check if width and height is valid based on set standard */
1017        if (cx25821_is_valid_width(f->fmt.pix.width, dev->tvnorm))
1018                fh->width = f->fmt.pix.width;
1019
1020        if (cx25821_is_valid_height(f->fmt.pix.height, dev->tvnorm))
1021                fh->height = f->fmt.pix.height;
1022
1023        if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
1024                pix_format = PIXEL_FRMT_411;
1025        else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
1026                pix_format = PIXEL_FRMT_422;
1027        else
1028                return -EINVAL;
1029
1030        cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
1031
1032        /* check if cif resolution */
1033        if (fh->width == 320 || fh->width == 352)
1034                dev->channels[fh->channel_id].use_cif_resolution = 1;
1035        else
1036                dev->channels[fh->channel_id].use_cif_resolution = 0;
1037
1038        dev->channels[fh->channel_id].cif_width = fh->width;
1039        medusa_set_resolution(dev, fh->width, SRAM_CH00);
1040
1041        dprintk(2, "%s() width=%d height=%d field=%d\n", __func__, fh->width,
1042                fh->height, fh->vidq.field);
1043         v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
1044         cx25821_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1045
1046        return 0;
1047 }
1048
1049 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1050 {
1051        int ret_val = 0;
1052        struct cx25821_fh *fh = priv;
1053        struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1054
1055        ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
1056
1057     p->sequence = dev->channels[fh->channel_id].vidq.count;
1058
1059        return ret_val;
1060 }
1061
1062 static int vidioc_log_status(struct file *file, void *priv)
1063 {
1064        struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1065        struct cx25821_fh *fh = priv;
1066        char name[32 + 2];
1067
1068        struct sram_channel *sram_ch = dev->channels[fh->channel_id]
1069                                                        .sram_channels;
1070        u32 tmp = 0;
1071
1072        snprintf(name, sizeof(name), "%s/2", dev->name);
1073        printk(KERN_INFO "%s/2: ============  START LOG STATUS  ============\n",
1074               dev->name);
1075        cx25821_call_all(dev, core, log_status);
1076        tmp = cx_read(sram_ch->dma_ctl);
1077        printk(KERN_INFO "Video input 0 is %s\n",
1078               (tmp & 0x11) ? "streaming" : "stopped");
1079        printk(KERN_INFO "%s/2: =============  END LOG STATUS  =============\n",
1080               dev->name);
1081        return 0;
1082 }
1083
1084 static int vidioc_s_ctrl(struct file *file, void *priv,
1085                         struct v4l2_control *ctl)
1086 {
1087        struct cx25821_fh *fh = priv;
1088        struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1089        int err;
1090
1091        if (fh) {
1092                err = v4l2_prio_check(&dev->channels[fh->channel_id]
1093                                                .prio, fh->prio);
1094                if (0 != err)
1095                        return err;
1096        }
1097
1098        return cx25821_set_control(dev, ctl, fh->channel_id);
1099 }
1100
1101 /* VIDEO IOCTLS                                                       */
1102 int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f)
1103 {
1104         struct cx25821_fh *fh = priv;
1105
1106         f->fmt.pix.width = fh->width;
1107         f->fmt.pix.height = fh->height;
1108         f->fmt.pix.field = fh->vidq.field;
1109         f->fmt.pix.pixelformat = fh->fmt->fourcc;
1110         f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3;
1111         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
1112
1113         return 0;
1114 }
1115
1116 int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f)
1117 {
1118         struct cx25821_fmt *fmt;
1119         enum v4l2_field field;
1120         unsigned int maxw, maxh;
1121
1122         fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
1123         if (NULL == fmt)
1124                 return -EINVAL;
1125
1126         field = f->fmt.pix.field;
1127         maxw = 720;
1128         maxh = 576;
1129
1130         if (V4L2_FIELD_ANY == field) {
1131                 field = (f->fmt.pix.height > maxh / 2)
1132                     ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
1133         }
1134
1135         switch (field) {
1136         case V4L2_FIELD_TOP:
1137         case V4L2_FIELD_BOTTOM:
1138                 maxh = maxh / 2;
1139                 break;
1140         case V4L2_FIELD_INTERLACED:
1141                 break;
1142         default:
1143                 return -EINVAL;
1144         }
1145
1146         f->fmt.pix.field = field;
1147         if (f->fmt.pix.height < 32)
1148                 f->fmt.pix.height = 32;
1149         if (f->fmt.pix.height > maxh)
1150                 f->fmt.pix.height = maxh;
1151         if (f->fmt.pix.width < 48)
1152                 f->fmt.pix.width = 48;
1153         if (f->fmt.pix.width > maxw)
1154                 f->fmt.pix.width = maxw;
1155         f->fmt.pix.width &= ~0x03;
1156         f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
1157         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
1158
1159         return 0;
1160 }
1161
1162 int cx25821_vidioc_querycap(struct file *file, void *priv, struct v4l2_capability *cap)
1163 {
1164         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1165
1166         strcpy(cap->driver, "cx25821");
1167         strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
1168         sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1169         cap->version = CX25821_VERSION_CODE;
1170         cap->capabilities =
1171             V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1172         if (UNSET != dev->tuner_type)
1173                 cap->capabilities |= V4L2_CAP_TUNER;
1174         return 0;
1175 }
1176
1177 int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1178                             struct v4l2_fmtdesc *f)
1179 {
1180         if (unlikely(f->index >= ARRAY_SIZE(formats)))
1181                 return -EINVAL;
1182
1183         strlcpy(f->description, formats[f->index].name, sizeof(f->description));
1184         f->pixelformat = formats[f->index].fourcc;
1185
1186         return 0;
1187 }
1188
1189 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1190 int cx25821_vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
1191 {
1192         struct cx25821_fh *fh = priv;
1193         struct videobuf_queue *q;
1194         struct v4l2_requestbuffers req;
1195         unsigned int i;
1196         int err;
1197
1198         q = get_queue(fh);
1199         memset(&req, 0, sizeof(req));
1200         req.type = q->type;
1201         req.count = 8;
1202         req.memory = V4L2_MEMORY_MMAP;
1203         err = videobuf_reqbufs(q, &req);
1204         if (err < 0)
1205                 return err;
1206
1207         mbuf->frames = req.count;
1208         mbuf->size = 0;
1209         for (i = 0; i < mbuf->frames; i++) {
1210                 mbuf->offsets[i] = q->bufs[i]->boff;
1211                 mbuf->size += q->bufs[i]->bsize;
1212         }
1213         return 0;
1214 }
1215 #endif
1216
1217 int cx25821_vidioc_reqbufs(struct file *file, void *priv, struct v4l2_requestbuffers *p)
1218 {
1219         struct cx25821_fh *fh = priv;
1220         return videobuf_reqbufs(get_queue(fh), p);
1221 }
1222
1223 int cx25821_vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
1224 {
1225         struct cx25821_fh *fh = priv;
1226         return videobuf_querybuf(get_queue(fh), p);
1227 }
1228
1229 int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1230 {
1231         struct cx25821_fh *fh = priv;
1232         return videobuf_qbuf(get_queue(fh), p);
1233 }
1234
1235 int cx25821_vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
1236 {
1237         struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1238        struct cx25821_fh *fh = f;
1239
1240        *p = v4l2_prio_max(&dev->channels[fh->channel_id].prio);
1241
1242         return 0;
1243 }
1244
1245 int cx25821_vidioc_s_priority(struct file *file, void *f, enum v4l2_priority prio)
1246 {
1247         struct cx25821_fh *fh = f;
1248         struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1249
1250        return v4l2_prio_change(&dev->channels[fh->channel_id]
1251                                        .prio, &fh->prio, prio);
1252 }
1253
1254 #ifdef TUNER_FLAG
1255 int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id * tvnorms)
1256 {
1257         struct cx25821_fh *fh = priv;
1258         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1259         int err;
1260
1261         dprintk(1, "%s()\n", __func__);
1262
1263         if (fh) {
1264                err = v4l2_prio_check(&dev->channels[fh->channel_id]
1265                                                .prio, fh->prio);
1266                 if (0 != err)
1267                         return err;
1268         }
1269
1270         if (dev->tvnorm == *tvnorms) {
1271                 return 0;
1272         }
1273
1274         mutex_lock(&dev->lock);
1275         cx25821_set_tvnorm(dev, *tvnorms);
1276         mutex_unlock(&dev->lock);
1277
1278         medusa_set_videostandard(dev);
1279
1280         return 0;
1281 }
1282 #endif
1283
1284 int cx25821_enum_input(struct cx25821_dev *dev, struct v4l2_input *i)
1285 {
1286         static const char *iname[] = {
1287                 [CX25821_VMUX_COMPOSITE] = "Composite",
1288                 [CX25821_VMUX_SVIDEO] = "S-Video",
1289                 [CX25821_VMUX_DEBUG] = "for debug only",
1290         };
1291         unsigned int n;
1292         dprintk(1, "%s()\n", __func__);
1293
1294         n = i->index;
1295         if (n >= 2)
1296                 return -EINVAL;
1297
1298         if (0 == INPUT(n)->type)
1299                 return -EINVAL;
1300
1301         memset(i, 0, sizeof(*i));
1302         i->index = n;
1303         i->type = V4L2_INPUT_TYPE_CAMERA;
1304         strcpy(i->name, iname[INPUT(n)->type]);
1305
1306         i->std = CX25821_NORMS;
1307         return 0;
1308 }
1309
1310 int cx25821_vidioc_enum_input(struct file *file, void *priv, struct v4l2_input *i)
1311 {
1312         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1313         dprintk(1, "%s()\n", __func__);
1314         return cx25821_enum_input(dev, i);
1315 }
1316
1317 int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1318 {
1319         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1320
1321         *i = dev->input;
1322         dprintk(1, "%s() returns %d\n", __func__, *i);
1323         return 0;
1324 }
1325
1326 int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
1327 {
1328         struct cx25821_fh *fh = priv;
1329         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1330         int err;
1331
1332         dprintk(1, "%s(%d)\n", __func__, i);
1333
1334         if (fh) {
1335                err = v4l2_prio_check(&dev->channels[fh->channel_id]
1336                                                .prio, fh->prio);
1337                 if (0 != err)
1338                         return err;
1339         }
1340
1341         if (i > 2) {
1342                 dprintk(1, "%s() -EINVAL\n", __func__);
1343                 return -EINVAL;
1344         }
1345
1346         mutex_lock(&dev->lock);
1347         cx25821_video_mux(dev, i);
1348         mutex_unlock(&dev->lock);
1349         return 0;
1350 }
1351
1352 #ifdef TUNER_FLAG
1353 int cx25821_vidioc_g_frequency(struct file *file, void *priv, struct v4l2_frequency *f)
1354 {
1355         struct cx25821_fh *fh = priv;
1356         struct cx25821_dev *dev = fh->dev;
1357
1358         f->frequency = dev->freq;
1359
1360         cx25821_call_all(dev, tuner, g_frequency, f);
1361
1362         return 0;
1363 }
1364
1365 int cx25821_set_freq(struct cx25821_dev *dev, struct v4l2_frequency *f)
1366 {
1367         mutex_lock(&dev->lock);
1368         dev->freq = f->frequency;
1369
1370         cx25821_call_all(dev, tuner, s_frequency, f);
1371
1372         /* When changing channels it is required to reset TVAUDIO */
1373         msleep(10);
1374
1375         mutex_unlock(&dev->lock);
1376
1377         return 0;
1378 }
1379
1380 int cx25821_vidioc_s_frequency(struct file *file, void *priv, struct v4l2_frequency *f)
1381 {
1382         struct cx25821_fh *fh = priv;
1383         struct cx25821_dev *dev;
1384         int err;
1385
1386         if (fh) {
1387                dev = fh->dev;
1388                err = v4l2_prio_check(&dev->channels[fh->channel_id]
1389                                                .prio, fh->prio);
1390                 if (0 != err)
1391                         return err;
1392        } else {
1393                printk(KERN_ERR "Invalid fh pointer!\n");
1394                return -EINVAL;
1395         }
1396
1397         return cx25821_set_freq(dev, f);
1398 }
1399 #endif
1400
1401 #ifdef CONFIG_VIDEO_ADV_DEBUG
1402 int cx25821_vidioc_g_register(struct file *file, void *fh,
1403                       struct v4l2_dbg_register *reg)
1404 {
1405         struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1406
1407         if (!v4l2_chip_match_host(&reg->match))
1408                 return -EINVAL;
1409
1410         cx25821_call_all(dev, core, g_register, reg);
1411
1412         return 0;
1413 }
1414
1415 int cx25821_vidioc_s_register(struct file *file, void *fh,
1416                       struct v4l2_dbg_register *reg)
1417 {
1418         struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1419
1420         if (!v4l2_chip_match_host(&reg->match))
1421                 return -EINVAL;
1422
1423         cx25821_call_all(dev, core, s_register, reg);
1424
1425         return 0;
1426 }
1427
1428 #endif
1429
1430 #ifdef TUNER_FLAG
1431 int cx25821_vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1432 {
1433         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1434
1435         if (unlikely(UNSET == dev->tuner_type))
1436                 return -EINVAL;
1437         if (0 != t->index)
1438                 return -EINVAL;
1439
1440         strcpy(t->name, "Television");
1441         t->type = V4L2_TUNER_ANALOG_TV;
1442         t->capability = V4L2_TUNER_CAP_NORM;
1443         t->rangehigh = 0xffffffffUL;
1444
1445         t->signal = 0xffff;     /* LOCKED */
1446         return 0;
1447 }
1448
1449 int cx25821_vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1450 {
1451         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1452         struct cx25821_fh *fh = priv;
1453         int err;
1454
1455         if (fh) {
1456                err = v4l2_prio_check(&dev->channels[fh->channel_id]
1457                                                .prio, fh->prio);
1458                 if (0 != err)
1459                         return err;
1460         }
1461
1462         dprintk(1, "%s()\n", __func__);
1463         if (UNSET == dev->tuner_type)
1464                 return -EINVAL;
1465         if (0 != t->index)
1466                 return -EINVAL;
1467
1468         return 0;
1469 }
1470
1471 #endif
1472 /*****************************************************************************/
1473 static const struct v4l2_queryctrl no_ctl = {
1474         .name = "42",
1475         .flags = V4L2_CTRL_FLAG_DISABLED,
1476 };
1477
1478 static struct v4l2_queryctrl cx25821_ctls[] = {
1479         /* --- video --- */
1480         {
1481          .id = V4L2_CID_BRIGHTNESS,
1482          .name = "Brightness",
1483          .minimum = 0,
1484          .maximum = 10000,
1485          .step = 1,
1486          .default_value = 6200,
1487          .type = V4L2_CTRL_TYPE_INTEGER,
1488          }, {
1489              .id = V4L2_CID_CONTRAST,
1490              .name = "Contrast",
1491              .minimum = 0,
1492              .maximum = 10000,
1493              .step = 1,
1494              .default_value = 5000,
1495              .type = V4L2_CTRL_TYPE_INTEGER,
1496              }, {
1497                  .id = V4L2_CID_SATURATION,
1498                  .name = "Saturation",
1499                  .minimum = 0,
1500                  .maximum = 10000,
1501                  .step = 1,
1502                  .default_value = 5000,
1503                  .type = V4L2_CTRL_TYPE_INTEGER,
1504                  }, {
1505                      .id = V4L2_CID_HUE,
1506                      .name = "Hue",
1507                      .minimum = 0,
1508                      .maximum = 10000,
1509                      .step = 1,
1510                      .default_value = 5000,
1511                      .type = V4L2_CTRL_TYPE_INTEGER,
1512                      }
1513 };
1514 static const int CX25821_CTLS = ARRAY_SIZE(cx25821_ctls);
1515
1516 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
1517 {
1518         int i;
1519
1520         if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
1521                 return -EINVAL;
1522         for (i = 0; i < CX25821_CTLS; i++)
1523                 if (cx25821_ctls[i].id == qctrl->id)
1524                         break;
1525         if (i == CX25821_CTLS) {
1526                 *qctrl = no_ctl;
1527                 return 0;
1528         }
1529         *qctrl = cx25821_ctls[i];
1530         return 0;
1531 }
1532
1533 int cx25821_vidioc_queryctrl(struct file *file, void *priv,
1534                      struct v4l2_queryctrl *qctrl)
1535 {
1536         return cx25821_ctrl_query(qctrl);
1537 }
1538
1539 /* ------------------------------------------------------------------ */
1540 /* VIDEO CTRL IOCTLS                                                  */
1541
1542 static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
1543 {
1544         unsigned int i;
1545
1546         for (i = 0; i < CX25821_CTLS; i++)
1547                 if (cx25821_ctls[i].id == id)
1548                         return cx25821_ctls + i;
1549         return NULL;
1550 }
1551
1552 int cx25821_vidioc_g_ctrl(struct file *file, void *priv, struct v4l2_control *ctl)
1553 {
1554         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1555        struct cx25821_fh *fh = priv;
1556
1557         const struct v4l2_queryctrl *ctrl;
1558
1559         ctrl = ctrl_by_id(ctl->id);
1560
1561         if (NULL == ctrl)
1562                 return -EINVAL;
1563         switch (ctl->id) {
1564         case V4L2_CID_BRIGHTNESS:
1565                ctl->value = dev->channels[fh->channel_id].ctl_bright;
1566                 break;
1567         case V4L2_CID_HUE:
1568                ctl->value = dev->channels[fh->channel_id].ctl_hue;
1569                 break;
1570         case V4L2_CID_CONTRAST:
1571                ctl->value = dev->channels[fh->channel_id].ctl_contrast;
1572                 break;
1573         case V4L2_CID_SATURATION:
1574                ctl->value = dev->channels[fh->channel_id].ctl_saturation;
1575                 break;
1576         }
1577         return 0;
1578 }
1579
1580 int cx25821_set_control(struct cx25821_dev *dev,
1581                         struct v4l2_control *ctl, int chan_num)
1582 {
1583         int err;
1584         const struct v4l2_queryctrl *ctrl;
1585
1586         err = -EINVAL;
1587
1588         ctrl = ctrl_by_id(ctl->id);
1589
1590         if (NULL == ctrl)
1591                 return err;
1592
1593         switch (ctrl->type) {
1594         case V4L2_CTRL_TYPE_BOOLEAN:
1595         case V4L2_CTRL_TYPE_MENU:
1596         case V4L2_CTRL_TYPE_INTEGER:
1597                 if (ctl->value < ctrl->minimum)
1598                         ctl->value = ctrl->minimum;
1599                 if (ctl->value > ctrl->maximum)
1600                         ctl->value = ctrl->maximum;
1601                 break;
1602         default:
1603                 /* nothing */ ;
1604         };
1605
1606         switch (ctl->id) {
1607         case V4L2_CID_BRIGHTNESS:
1608                dev->channels[chan_num].ctl_bright = ctl->value;
1609                 medusa_set_brightness(dev, ctl->value, chan_num);
1610                 break;
1611         case V4L2_CID_HUE:
1612                dev->channels[chan_num].ctl_hue = ctl->value;
1613                 medusa_set_hue(dev, ctl->value, chan_num);
1614                 break;
1615         case V4L2_CID_CONTRAST:
1616                dev->channels[chan_num].ctl_contrast = ctl->value;
1617                 medusa_set_contrast(dev, ctl->value, chan_num);
1618                 break;
1619         case V4L2_CID_SATURATION:
1620                dev->channels[chan_num].ctl_saturation = ctl->value;
1621                 medusa_set_saturation(dev, ctl->value, chan_num);
1622                 break;
1623         }
1624
1625         err = 0;
1626
1627         return err;
1628 }
1629
1630 static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num)
1631 {
1632         struct v4l2_control ctrl;
1633         int i;
1634         for (i = 0; i < CX25821_CTLS; i++) {
1635                 ctrl.id = cx25821_ctls[i].id;
1636                 ctrl.value = cx25821_ctls[i].default_value;
1637
1638                 cx25821_set_control(dev, &ctrl, chan_num);
1639         }
1640 }
1641
1642 int cx25821_vidioc_cropcap(struct file *file, void *priv, struct v4l2_cropcap *cropcap)
1643 {
1644         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1645
1646         if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1647                 return -EINVAL;
1648         cropcap->bounds.top = cropcap->bounds.left = 0;
1649         cropcap->bounds.width = 720;
1650         cropcap->bounds.height = dev->tvnorm == V4L2_STD_PAL_BG ? 576 : 480;
1651         cropcap->pixelaspect.numerator =
1652             dev->tvnorm == V4L2_STD_PAL_BG ? 59 : 10;
1653         cropcap->pixelaspect.denominator =
1654             dev->tvnorm == V4L2_STD_PAL_BG ? 54 : 11;
1655         cropcap->defrect = cropcap->bounds;
1656         return 0;
1657 }
1658
1659 int cx25821_vidioc_s_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1660 {
1661         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1662         struct cx25821_fh *fh = priv;
1663         int err;
1664
1665         if (fh) {
1666                err = v4l2_prio_check(&dev->channels[fh->channel_id].
1667                                                prio, fh->prio);
1668                 if (0 != err)
1669                         return err;
1670         }
1671        /* cx25821_vidioc_s_crop not supported */
1672         return -EINVAL;
1673 }
1674
1675 int cx25821_vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1676 {
1677        /* cx25821_vidioc_g_crop not supported */
1678         return -EINVAL;
1679 }
1680
1681 int cx25821_vidioc_querystd(struct file *file, void *priv, v4l2_std_id * norm)
1682 {
1683        /* medusa does not support video standard sensing of current input */
1684         *norm = CX25821_NORMS;
1685
1686         return 0;
1687 }
1688
1689 int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm)
1690 {
1691         if (tvnorm == V4L2_STD_PAL_BG) {
1692                 if (width == 352 || width == 720)
1693                         return 1;
1694                 else
1695                         return 0;
1696         }
1697
1698         if (tvnorm == V4L2_STD_NTSC_M) {
1699                 if (width == 320 || width == 352 || width == 720)
1700                         return 1;
1701                 else
1702                         return 0;
1703         }
1704         return 0;
1705 }
1706
1707 int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm)
1708 {
1709         if (tvnorm == V4L2_STD_PAL_BG) {
1710                 if (height == 576 || height == 288)
1711                         return 1;
1712                 else
1713                         return 0;
1714         }
1715
1716         if (tvnorm == V4L2_STD_NTSC_M) {
1717                 if (height == 480 || height == 240)
1718                         return 1;
1719                 else
1720                         return 0;
1721         }
1722
1723         return 0;
1724 }
1725
1726 static long video_ioctl_upstream9(struct file *file, unsigned int cmd,
1727                                  unsigned long arg)
1728 {
1729        struct cx25821_fh *fh = file->private_data;
1730        struct cx25821_dev *dev = fh->dev;
1731        int command = 0;
1732        struct upstream_user_struct *data_from_user;
1733
1734        data_from_user = (struct upstream_user_struct *)arg;
1735
1736        if (!data_from_user) {
1737                printk
1738                    ("cx25821 in %s(): Upstream data is INVALID. Returning.\n",
1739                     __func__);
1740                return 0;
1741        }
1742
1743        command = data_from_user->command;
1744
1745        if (command != UPSTREAM_START_VIDEO &&
1746                command != UPSTREAM_STOP_VIDEO)
1747                return 0;
1748
1749        dev->input_filename = data_from_user->input_filename;
1750        dev->input_audiofilename = data_from_user->input_filename;
1751        dev->vid_stdname = data_from_user->vid_stdname;
1752        dev->pixel_format = data_from_user->pixel_format;
1753        dev->channel_select = data_from_user->channel_select;
1754        dev->command = data_from_user->command;
1755
1756        switch (command) {
1757        case UPSTREAM_START_VIDEO:
1758                cx25821_start_upstream_video_ch1(dev, data_from_user);
1759                break;
1760
1761        case UPSTREAM_STOP_VIDEO:
1762                cx25821_stop_upstream_video_ch1(dev);
1763                break;
1764        }
1765
1766        return 0;
1767 }
1768
1769 static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
1770                                   unsigned long arg)
1771 {
1772        struct cx25821_fh *fh = file->private_data;
1773        struct cx25821_dev *dev = fh->dev;
1774        int command = 0;
1775        struct upstream_user_struct *data_from_user;
1776
1777        data_from_user = (struct upstream_user_struct *)arg;
1778
1779        if (!data_from_user) {
1780                printk
1781                    ("cx25821 in %s(): Upstream data is INVALID. Returning.\n",
1782                     __func__);
1783                return 0;
1784        }
1785
1786        command = data_from_user->command;
1787
1788        if (command != UPSTREAM_START_VIDEO &&
1789                command != UPSTREAM_STOP_VIDEO)
1790                return 0;
1791
1792        dev->input_filename_ch2 = data_from_user->input_filename;
1793        dev->input_audiofilename = data_from_user->input_filename;
1794        dev->vid_stdname_ch2 = data_from_user->vid_stdname;
1795        dev->pixel_format_ch2 = data_from_user->pixel_format;
1796        dev->channel_select_ch2 = data_from_user->channel_select;
1797        dev->command_ch2 = data_from_user->command;
1798
1799        switch (command) {
1800        case UPSTREAM_START_VIDEO:
1801                cx25821_start_upstream_video_ch2(dev, data_from_user);
1802                break;
1803
1804        case UPSTREAM_STOP_VIDEO:
1805                cx25821_stop_upstream_video_ch2(dev);
1806                break;
1807        }
1808
1809        return 0;
1810 }
1811
1812 static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
1813                                   unsigned long arg)
1814 {
1815        struct cx25821_fh *fh = file->private_data;
1816        struct cx25821_dev *dev = fh->dev;
1817        int command = 0;
1818        struct upstream_user_struct *data_from_user;
1819
1820        data_from_user = (struct upstream_user_struct *)arg;
1821
1822        if (!data_from_user) {
1823                printk
1824                    ("cx25821 in %s(): Upstream data is INVALID. Returning.\n",
1825                     __func__);
1826                return 0;
1827        }
1828
1829        command = data_from_user->command;
1830
1831        if (command != UPSTREAM_START_AUDIO &&
1832                command != UPSTREAM_STOP_AUDIO)
1833                return 0;
1834
1835        dev->input_filename = data_from_user->input_filename;
1836        dev->input_audiofilename = data_from_user->input_filename;
1837        dev->vid_stdname = data_from_user->vid_stdname;
1838        dev->pixel_format = data_from_user->pixel_format;
1839        dev->channel_select = data_from_user->channel_select;
1840        dev->command = data_from_user->command;
1841
1842        switch (command) {
1843        case UPSTREAM_START_AUDIO:
1844                cx25821_start_upstream_audio(dev, data_from_user);
1845                break;
1846
1847        case UPSTREAM_STOP_AUDIO:
1848                cx25821_stop_upstream_audio(dev);
1849                break;
1850        }
1851
1852        return 0;
1853 }
1854
1855 static long video_ioctl_set(struct file *file, unsigned int cmd,
1856                            unsigned long arg)
1857 {
1858        struct cx25821_fh *fh = file->private_data;
1859        struct cx25821_dev *dev = fh->dev;
1860        struct downstream_user_struct *data_from_user;
1861        int command;
1862        int width = 720;
1863        int selected_channel = 0, pix_format = 0, i = 0;
1864        int cif_enable = 0, cif_width = 0;
1865        u32 value = 0;
1866
1867        data_from_user = (struct downstream_user_struct *)arg;
1868
1869        if (!data_from_user) {
1870                printk(
1871                "cx25821 in %s(): User data is INVALID. Returning.\n",
1872                __func__);
1873                return 0;
1874        }
1875
1876        command = data_from_user->command;
1877
1878        if (command != SET_VIDEO_STD && command != SET_PIXEL_FORMAT
1879            && command != ENABLE_CIF_RESOLUTION && command != REG_READ
1880            && command != REG_WRITE && command != MEDUSA_READ
1881            && command != MEDUSA_WRITE) {
1882                return 0;
1883        }
1884
1885        switch (command) {
1886        case SET_VIDEO_STD:
1887                dev->tvnorm =
1888                    !strcmp(data_from_user->vid_stdname,
1889                            "PAL") ? V4L2_STD_PAL_BG : V4L2_STD_NTSC_M;
1890                medusa_set_videostandard(dev);
1891                break;
1892
1893        case SET_PIXEL_FORMAT:
1894                selected_channel = data_from_user->decoder_select;
1895                pix_format = data_from_user->pixel_format;
1896
1897                if (!(selected_channel <= 7 && selected_channel >= 0)) {
1898                        selected_channel -= 4;
1899                        selected_channel = selected_channel % 8;
1900                }
1901
1902                if (selected_channel >= 0)
1903                        cx25821_set_pixel_format(dev, selected_channel,
1904                                                 pix_format);
1905
1906                break;
1907
1908        case ENABLE_CIF_RESOLUTION:
1909                selected_channel = data_from_user->decoder_select;
1910                cif_enable = data_from_user->cif_resolution_enable;
1911                cif_width = data_from_user->cif_width;
1912
1913                if (cif_enable) {
1914                        if (dev->tvnorm & V4L2_STD_PAL_BG
1915                            || dev->tvnorm & V4L2_STD_PAL_DK)
1916                                width = 352;
1917                        else
1918                                width = (cif_width == 320
1919                                         || cif_width == 352) ? cif_width : 320;
1920                }
1921
1922                if (!(selected_channel <= 7 && selected_channel >= 0)) {
1923                        selected_channel -= 4;
1924                        selected_channel = selected_channel % 8;
1925                }
1926
1927                if (selected_channel <= 7 && selected_channel >= 0) {
1928                        dev->channels[selected_channel].
1929                                use_cif_resolution = cif_enable;
1930                        dev->channels[selected_channel].cif_width = width;
1931                } else {
1932                        for (i = 0; i < VID_CHANNEL_NUM; i++) {
1933                                dev->channels[i].use_cif_resolution =
1934                                        cif_enable;
1935                                dev->channels[i].cif_width = width;
1936                        }
1937                }
1938
1939                medusa_set_resolution(dev, width, selected_channel);
1940                break;
1941        case REG_READ:
1942                data_from_user->reg_data = cx_read(data_from_user->reg_address);
1943                break;
1944        case REG_WRITE:
1945                cx_write(data_from_user->reg_address, data_from_user->reg_data);
1946                break;
1947        case MEDUSA_READ:
1948                value =
1949                    cx25821_i2c_read(&dev->i2c_bus[0],
1950                                     (u16) data_from_user->reg_address,
1951                                     &data_from_user->reg_data);
1952                break;
1953        case MEDUSA_WRITE:
1954                cx25821_i2c_write(&dev->i2c_bus[0],
1955                                  (u16) data_from_user->reg_address,
1956                                  data_from_user->reg_data);
1957                break;
1958        }
1959
1960        return 0;
1961 }
1962
1963 static long cx25821_video_ioctl(struct file *file,
1964                                unsigned int cmd, unsigned long arg)
1965 {
1966        int  ret = 0;
1967
1968        struct cx25821_fh  *fh  = file->private_data;
1969
1970        /* check to see if it's the video upstream */
1971        if (fh->channel_id == SRAM_CH09) {
1972                ret = video_ioctl_upstream9(file, cmd, arg);
1973                return ret;
1974        } else if (fh->channel_id == SRAM_CH10) {
1975                ret = video_ioctl_upstream10(file, cmd, arg);
1976                return ret;
1977        } else if (fh->channel_id == SRAM_CH11) {
1978                ret = video_ioctl_upstream11(file, cmd, arg);
1979                ret = video_ioctl_set(file, cmd, arg);
1980                return ret;
1981        }
1982
1983     return video_ioctl2(file, cmd, arg);
1984 }
1985
1986 /* exported stuff */
1987 static const struct v4l2_file_operations video_fops = {
1988        .owner = THIS_MODULE,
1989        .open = video_open,
1990        .release = video_release,
1991        .read = video_read,
1992        .poll = video_poll,
1993        .mmap = cx25821_video_mmap,
1994        .ioctl = cx25821_video_ioctl,
1995 };
1996
1997 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1998        .vidioc_querycap = cx25821_vidioc_querycap,
1999        .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
2000        .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
2001        .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
2002        .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2003        .vidioc_reqbufs = cx25821_vidioc_reqbufs,
2004        .vidioc_querybuf = cx25821_vidioc_querybuf,
2005        .vidioc_qbuf = cx25821_vidioc_qbuf,
2006        .vidioc_dqbuf = vidioc_dqbuf,
2007 #ifdef TUNER_FLAG
2008        .vidioc_s_std = cx25821_vidioc_s_std,
2009        .vidioc_querystd = cx25821_vidioc_querystd,
2010 #endif
2011        .vidioc_cropcap = cx25821_vidioc_cropcap,
2012        .vidioc_s_crop = cx25821_vidioc_s_crop,
2013        .vidioc_g_crop = cx25821_vidioc_g_crop,
2014        .vidioc_enum_input = cx25821_vidioc_enum_input,
2015        .vidioc_g_input = cx25821_vidioc_g_input,
2016        .vidioc_s_input = cx25821_vidioc_s_input,
2017        .vidioc_g_ctrl = cx25821_vidioc_g_ctrl,
2018        .vidioc_s_ctrl = vidioc_s_ctrl,
2019        .vidioc_queryctrl = cx25821_vidioc_queryctrl,
2020        .vidioc_streamon = vidioc_streamon,
2021        .vidioc_streamoff = vidioc_streamoff,
2022        .vidioc_log_status = vidioc_log_status,
2023        .vidioc_g_priority = cx25821_vidioc_g_priority,
2024        .vidioc_s_priority = cx25821_vidioc_s_priority,
2025 #ifdef CONFIG_VIDEO_V4L1_COMPAT
2026        .vidiocgmbuf = cx25821_vidiocgmbuf,
2027 #endif
2028 #ifdef TUNER_FLAG
2029        .vidioc_g_tuner = cx25821_vidioc_g_tuner,
2030        .vidioc_s_tuner = cx25821_vidioc_s_tuner,
2031        .vidioc_g_frequency = cx25821_vidioc_g_frequency,
2032        .vidioc_s_frequency = cx25821_vidioc_s_frequency,
2033 #endif
2034 #ifdef CONFIG_VIDEO_ADV_DEBUG
2035        .vidioc_g_register = cx25821_vidioc_g_register,
2036        .vidioc_s_register = cx25821_vidioc_s_register,
2037 #endif
2038 };
2039
2040 struct video_device cx25821_videoioctl_template = {
2041                .name = "cx25821-videoioctl",
2042                .fops = &video_fops,
2043                .ioctl_ops = &video_ioctl_ops,
2044                .tvnorms = CX25821_NORMS,
2045                .current_norm = V4L2_STD_NTSC_M,
2046 };