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