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