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