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