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