]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/staging/cx25821/cx25821-video.c
c0c99c0fd4374cce5b020808a5ddb89177cc44a6
[net-next-2.6.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  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include "cx25821-video.h"
25
26 MODULE_DESCRIPTION("v4l2 driver module for cx25821 based TV cards");
27 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
28 MODULE_LICENSE("GPL");
29
30 static unsigned int video_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
31 static unsigned int radio_nr[] = {[0 ... (CX25821_MAXBOARDS - 1)] = UNSET };
32
33 module_param_array(video_nr, int, NULL, 0444);
34 module_param_array(radio_nr, int, NULL, 0444);
35
36 MODULE_PARM_DESC(video_nr, "video device numbers");
37 MODULE_PARM_DESC(radio_nr, "radio device numbers");
38
39 static unsigned int video_debug = VIDEO_DEBUG;
40 module_param(video_debug, int, 0644);
41 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
42
43 static unsigned int irq_debug;
44 module_param(irq_debug, int, 0644);
45 MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
46
47 unsigned int vid_limit = 16;
48 module_param(vid_limit, int, 0644);
49 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
50
51 static void init_controls(struct cx25821_dev *dev, int chan_num);
52
53 #define FORMAT_FLAGS_PACKED       0x01
54
55 struct cx25821_fmt formats[] = {
56         {
57          .name = "8 bpp, gray",
58          .fourcc = V4L2_PIX_FMT_GREY,
59          .depth = 8,
60          .flags = FORMAT_FLAGS_PACKED,
61          }, {
62              .name = "4:1:1, packed, Y41P",
63              .fourcc = V4L2_PIX_FMT_Y41P,
64              .depth = 12,
65              .flags = FORMAT_FLAGS_PACKED,
66              }, {
67                  .name = "4:2:2, packed, YUYV",
68                  .fourcc = V4L2_PIX_FMT_YUYV,
69                  .depth = 16,
70                  .flags = FORMAT_FLAGS_PACKED,
71                  }, {
72                      .name = "4:2:2, packed, UYVY",
73                      .fourcc = V4L2_PIX_FMT_UYVY,
74                      .depth = 16,
75                      .flags = FORMAT_FLAGS_PACKED,
76                      }, {
77                          .name = "4:2:0, YUV",
78                          .fourcc = V4L2_PIX_FMT_YUV420,
79                          .depth = 12,
80                          .flags = FORMAT_FLAGS_PACKED,
81                          },
82 };
83
84 int get_format_size(void)
85 {
86         return ARRAY_SIZE(formats);
87 }
88
89 struct cx25821_fmt *format_by_fourcc(unsigned int fourcc)
90 {
91         unsigned int i;
92
93         if (fourcc == V4L2_PIX_FMT_Y41P || fourcc == V4L2_PIX_FMT_YUV411P) {
94                 return formats + 1;
95         }
96
97         for (i = 0; i < ARRAY_SIZE(formats); i++)
98                 if (formats[i].fourcc == fourcc)
99                         return formats + i;
100
101         printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __func__, fourcc);
102         return NULL;
103 }
104
105 void dump_video_queue(struct cx25821_dev *dev, struct cx25821_dmaqueue *q)
106 {
107         struct cx25821_buffer *buf;
108         struct list_head *item;
109         dprintk(1, "%s()\n", __func__);
110
111         if (!list_empty(&q->active)) {
112                 list_for_each(item, &q->active)
113                     buf = list_entry(item, struct cx25821_buffer, vb.queue);
114         }
115
116         if (!list_empty(&q->queued)) {
117                 list_for_each(item, &q->queued)
118                     buf = list_entry(item, struct cx25821_buffer, vb.queue);
119         }
120
121 }
122
123 void cx25821_video_wakeup(struct cx25821_dev *dev, struct cx25821_dmaqueue *q,
124                           u32 count)
125 {
126         struct cx25821_buffer *buf;
127         int bc;
128
129         for (bc = 0;; bc++) {
130                 if (list_empty(&q->active)) {
131                         dprintk(1, "bc=%d (=0: active empty)\n", bc);
132                         break;
133                 }
134
135                 buf =
136                     list_entry(q->active.next, struct cx25821_buffer, vb.queue);
137
138                 /* count comes from the hw and it is 16bit wide --
139                  * this trick handles wrap-arounds correctly for
140                  * up to 32767 buffers in flight... */
141                 if ((s16) (count - buf->count) < 0) {
142                         break;
143                 }
144
145                 do_gettimeofday(&buf->vb.ts);
146                 buf->vb.state = VIDEOBUF_DONE;
147                 list_del(&buf->vb.queue);
148                 wake_up(&buf->vb.done);
149         }
150
151         if (list_empty(&q->active))
152                 del_timer(&q->timeout);
153         else
154                 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
155         if (bc != 1)
156                 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
157                        __func__, bc);
158 }
159
160 #ifdef TUNER_FLAG
161 int cx25821_set_tvnorm(struct cx25821_dev *dev, v4l2_std_id norm)
162 {
163         dprintk(1, "%s(norm = 0x%08x) name: [%s]\n", __func__,
164                 (unsigned int)norm, v4l2_norm_to_name(norm));
165
166         dev->tvnorm = norm;
167
168         /* Tell the internal A/V decoder */
169         cx25821_call_all(dev, core, s_std, norm);
170
171         return 0;
172 }
173 #endif
174
175 struct video_device *cx25821_vdev_init(struct cx25821_dev *dev,
176                                        struct pci_dev *pci,
177                                        struct video_device *template,
178                                        char *type)
179 {
180         struct video_device *vfd;
181         dprintk(1, "%s()\n", __func__);
182
183         vfd = video_device_alloc();
184         if (NULL == vfd)
185                 return NULL;
186         *vfd = *template;
187         vfd->minor = -1;
188         vfd->v4l2_dev = &dev->v4l2_dev;
189         vfd->release = video_device_release;
190         snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name, type,
191                  cx25821_boards[dev->board].name);
192         video_set_drvdata(vfd, dev);
193         return vfd;
194 }
195
196 /*
197 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
198 {
199     int i;
200
201     if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
202         return -EINVAL;
203     for (i = 0; i < CX25821_CTLS; i++)
204         if (cx25821_ctls[i].v.id == qctrl->id)
205             break;
206     if (i == CX25821_CTLS) {
207         *qctrl = no_ctl;
208         return 0;
209     }
210     *qctrl = cx25821_ctls[i].v;
211     return 0;
212 }
213 */
214
215 // resource management
216 int res_get(struct cx25821_dev *dev, struct cx25821_fh *fh, unsigned int bit)
217 {
218         dprintk(1, "%s()\n", __func__);
219         if (fh->resources & bit)
220                 /* have it already allocated */
221                 return 1;
222
223         /* is it free? */
224         mutex_lock(&dev->lock);
225         if (dev->resources & bit) {
226                 /* no, someone else uses it */
227                 mutex_unlock(&dev->lock);
228                 return 0;
229         }
230         /* it's free, grab it */
231         fh->resources |= bit;
232         dev->resources |= bit;
233         dprintk(1, "res: get %d\n", bit);
234         mutex_unlock(&dev->lock);
235         return 1;
236 }
237
238 int res_check(struct cx25821_fh *fh, unsigned int bit)
239 {
240         return fh->resources & bit;
241 }
242
243 int res_locked(struct cx25821_dev *dev, unsigned int bit)
244 {
245         return dev->resources & bit;
246 }
247
248 void res_free(struct cx25821_dev *dev, struct cx25821_fh *fh, unsigned int bits)
249 {
250         BUG_ON((fh->resources & bits) != bits);
251         dprintk(1, "%s()\n", __func__);
252
253         mutex_lock(&dev->lock);
254         fh->resources &= ~bits;
255         dev->resources &= ~bits;
256         dprintk(1, "res: put %d\n", bits);
257         mutex_unlock(&dev->lock);
258 }
259
260 int cx25821_video_mux(struct cx25821_dev *dev, unsigned int input)
261 {
262         struct v4l2_routing route;
263         memset(&route, 0, sizeof(route));
264
265         dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
266                 __func__, input, INPUT(input)->vmux, INPUT(input)->gpio0,
267                 INPUT(input)->gpio1, INPUT(input)->gpio2, INPUT(input)->gpio3);
268         dev->input = input;
269
270         route.input = INPUT(input)->vmux;
271
272         /* Tell the internal A/V decoder */
273         cx25821_call_all(dev, video, s_routing, INPUT(input)->vmux, 0, 0);
274
275         return 0;
276 }
277
278 int cx25821_start_video_dma(struct cx25821_dev *dev,
279                             struct cx25821_dmaqueue *q,
280                             struct cx25821_buffer *buf,
281                             struct sram_channel *channel)
282 {
283         int tmp = 0;
284
285         /* setup fifo + format */
286         cx25821_sram_channel_setup(dev, channel, buf->bpl, buf->risc.dma);
287
288         /* reset counter */
289         cx_write(channel->gpcnt_ctl, 3);
290         q->count = 1;
291
292         /* enable irq */
293         cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | (1 << channel->i));
294         cx_set(channel->int_msk, 0x11);
295
296         /* start dma */
297         cx_write(channel->dma_ctl, 0x11);       /* FIFO and RISC enable */
298
299         /* make sure upstream setting if any is reversed */
300         tmp = cx_read(VID_CH_MODE_SEL);
301         cx_write(VID_CH_MODE_SEL, tmp & 0xFFFFFE00);
302
303         return 0;
304 }
305
306 int cx25821_restart_video_queue(struct cx25821_dev *dev,
307                                 struct cx25821_dmaqueue *q,
308                                 struct sram_channel *channel)
309 {
310         struct cx25821_buffer *buf, *prev;
311         struct list_head *item;
312
313         if (!list_empty(&q->active)) {
314                 buf =
315                     list_entry(q->active.next, struct cx25821_buffer, vb.queue);
316
317                 cx25821_start_video_dma(dev, q, buf, channel);
318
319                 list_for_each(item, &q->active) {
320                         buf = list_entry(item, struct cx25821_buffer, vb.queue);
321                         buf->count = q->count++;
322                 }
323
324                 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
325                 return 0;
326         }
327
328         prev = NULL;
329         for (;;) {
330                 if (list_empty(&q->queued))
331                         return 0;
332
333                 buf =
334                     list_entry(q->queued.next, struct cx25821_buffer, vb.queue);
335
336                 if (NULL == prev) {
337                         list_move_tail(&buf->vb.queue, &q->active);
338                         cx25821_start_video_dma(dev, q, buf, channel);
339                         buf->vb.state = VIDEOBUF_ACTIVE;
340                         buf->count = q->count++;
341                         mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
342                 } else if (prev->vb.width == buf->vb.width &&
343                            prev->vb.height == buf->vb.height &&
344                            prev->fmt == buf->fmt) {
345                         list_move_tail(&buf->vb.queue, &q->active);
346                         buf->vb.state = VIDEOBUF_ACTIVE;
347                         buf->count = q->count++;
348                         prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
349                         prev->risc.jmp[2] = cpu_to_le32(0);     /* Bits 63 - 32 */
350                 } else {
351                         return 0;
352                 }
353                 prev = buf;
354         }
355 }
356
357 void cx25821_vid_timeout(unsigned long data)
358 {
359         struct cx25821_data *timeout_data = (struct cx25821_data *)data;
360         struct cx25821_dev *dev = timeout_data->dev;
361         struct sram_channel *channel = timeout_data->channel;
362         struct cx25821_dmaqueue *q = &dev->vidq[channel->i];
363         struct cx25821_buffer *buf;
364         unsigned long flags;
365
366         //cx25821_sram_channel_dump(dev, channel);
367         cx_clear(channel->dma_ctl, 0x11);
368
369         spin_lock_irqsave(&dev->slock, flags);
370         while (!list_empty(&q->active)) {
371                 buf =
372                     list_entry(q->active.next, struct cx25821_buffer, vb.queue);
373                 list_del(&buf->vb.queue);
374
375                 buf->vb.state = VIDEOBUF_ERROR;
376                 wake_up(&buf->vb.done);
377         }
378
379         cx25821_restart_video_queue(dev, q, channel);
380         spin_unlock_irqrestore(&dev->slock, flags);
381 }
382
383 int cx25821_video_irq(struct cx25821_dev *dev, int chan_num, u32 status)
384 {
385         u32 count = 0;
386         int handled = 0;
387         u32 mask;
388         struct sram_channel *channel = &dev->sram_channels[chan_num];
389
390         mask = cx_read(channel->int_msk);
391         if (0 == (status & mask))
392                 return handled;
393
394         cx_write(channel->int_stat, status);
395
396         /* risc op code error */
397         if (status & (1 << 16)) {
398                 printk(KERN_WARNING "%s, %s: video risc op code error\n",
399                        dev->name, channel->name);
400                 cx_clear(channel->dma_ctl, 0x11);
401                 cx25821_sram_channel_dump(dev, channel);
402         }
403
404         /* risc1 y */
405         if (status & FLD_VID_DST_RISC1) {
406                 spin_lock(&dev->slock);
407                 count = cx_read(channel->gpcnt);
408                 cx25821_video_wakeup(dev, &dev->vidq[channel->i], count);
409                 spin_unlock(&dev->slock);
410                 handled++;
411         }
412
413         /* risc2 y */
414         if (status & 0x10) {
415                 dprintk(2, "stopper video\n");
416                 spin_lock(&dev->slock);
417                 cx25821_restart_video_queue(dev, &dev->vidq[channel->i],
418                                             channel);
419                 spin_unlock(&dev->slock);
420                 handled++;
421         }
422         return handled;
423 }
424
425 void cx25821_videoioctl_unregister(struct cx25821_dev *dev)
426 {
427         if (dev->ioctl_dev) {
428                 if (video_is_registered(dev->ioctl_dev))
429                         video_unregister_device(dev->ioctl_dev);
430                 else
431                         video_device_release(dev->ioctl_dev);
432
433                 dev->ioctl_dev = NULL;
434         }
435 }
436
437 void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
438 {
439         cx_clear(PCI_INT_MSK, 1);
440
441         if (dev->video_dev[chan_num]) {
442                 if (video_is_registered(dev->video_dev[chan_num]))
443                         video_unregister_device(dev->video_dev[chan_num]);
444                 else
445                         video_device_release(dev->video_dev[chan_num]);
446
447                 dev->video_dev[chan_num] = NULL;
448
449                 btcx_riscmem_free(dev->pci, &dev->vidq[chan_num].stopper);
450
451                 printk(KERN_WARNING "device %d released!\n", chan_num);
452         }
453
454 }
455
456 int cx25821_video_register(struct cx25821_dev *dev, int chan_num,
457                            struct video_device *video_template)
458 {
459         int err;
460
461         spin_lock_init(&dev->slock);
462
463         //printk(KERN_WARNING "Channel %d\n", chan_num);
464
465 #ifdef TUNER_FLAG
466         dev->tvnorm = video_template->current_norm;
467 #endif
468
469         /* init video dma queues */
470         dev->timeout_data[chan_num].dev = dev;
471         dev->timeout_data[chan_num].channel = &dev->sram_channels[chan_num];
472         INIT_LIST_HEAD(&dev->vidq[chan_num].active);
473         INIT_LIST_HEAD(&dev->vidq[chan_num].queued);
474         dev->vidq[chan_num].timeout.function = cx25821_vid_timeout;
475         dev->vidq[chan_num].timeout.data =
476             (unsigned long)&dev->timeout_data[chan_num];
477         init_timer(&dev->vidq[chan_num].timeout);
478         cx25821_risc_stopper(dev->pci, &dev->vidq[chan_num].stopper,
479                              dev->sram_channels[chan_num].dma_ctl, 0x11, 0);
480
481         /* register v4l devices */
482         dev->video_dev[chan_num] =
483             cx25821_vdev_init(dev, dev->pci, video_template, "video");
484         err =
485             video_register_device(dev->video_dev[chan_num], VFL_TYPE_GRABBER,
486                                   video_nr[dev->nr]);
487
488         if (err < 0) {
489                 goto fail_unreg;
490         }
491         //set PCI interrupt
492         cx_set(PCI_INT_MSK, 0xff);
493
494         /* initial device configuration */
495         mutex_lock(&dev->lock);
496 #ifdef TUNER_FLAG
497         cx25821_set_tvnorm(dev, dev->tvnorm);
498 #endif
499         mutex_unlock(&dev->lock);
500
501         init_controls(dev, chan_num);
502
503         return 0;
504
505       fail_unreg:
506         cx25821_video_unregister(dev, chan_num);
507         return err;
508 }
509
510 int buffer_setup(struct videobuf_queue *q, unsigned int *count,
511                  unsigned int *size)
512 {
513         struct cx25821_fh *fh = q->priv_data;
514
515         *size = fh->fmt->depth * fh->width * fh->height >> 3;
516
517         if (0 == *count)
518                 *count = 32;
519
520         while (*size * *count > vid_limit * 1024 * 1024)
521                 (*count)--;
522
523         return 0;
524 }
525
526 int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
527                    enum v4l2_field field)
528 {
529         struct cx25821_fh *fh = q->priv_data;
530         struct cx25821_dev *dev = fh->dev;
531         struct cx25821_buffer *buf =
532             container_of(vb, struct cx25821_buffer, vb);
533         int rc, init_buffer = 0;
534         u32 line0_offset, line1_offset;
535         struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
536         int bpl_local = LINE_SIZE_D1;
537         int channel_opened = 0;
538
539         BUG_ON(NULL == fh->fmt);
540         if (fh->width < 48 || fh->width > 720 ||
541             fh->height < 32 || fh->height > 576)
542                 return -EINVAL;
543
544         buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
545
546         if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
547                 return -EINVAL;
548
549         if (buf->fmt != fh->fmt ||
550             buf->vb.width != fh->width ||
551             buf->vb.height != fh->height || buf->vb.field != field) {
552                 buf->fmt = fh->fmt;
553                 buf->vb.width = fh->width;
554                 buf->vb.height = fh->height;
555                 buf->vb.field = field;
556                 init_buffer = 1;
557         }
558
559         if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
560                 init_buffer = 1;
561                 rc = videobuf_iolock(q, &buf->vb, NULL);
562                 if (0 != rc) {
563                         printk(KERN_DEBUG "videobuf_iolock failed!\n");
564                         goto fail;
565                 }
566         }
567
568         dprintk(1, "init_buffer=%d\n", init_buffer);
569
570         if (init_buffer) {
571
572                 channel_opened = dev->channel_opened;
573                 channel_opened = (channel_opened < 0
574                                   || channel_opened > 7) ? 7 : channel_opened;
575
576                 if (dev->pixel_formats[channel_opened] == PIXEL_FRMT_411)
577                         buf->bpl = (buf->fmt->depth * buf->vb.width) >> 3;
578                 else
579                         buf->bpl = (buf->fmt->depth >> 3) * (buf->vb.width);
580
581                 if (dev->pixel_formats[channel_opened] == PIXEL_FRMT_411) {
582                         bpl_local = buf->bpl;
583                 } else {
584                         bpl_local = buf->bpl;   //Default
585
586                         if (channel_opened >= 0 && channel_opened <= 7) {
587                                 if (dev->use_cif_resolution[channel_opened]) {
588                                         if (dev->tvnorm & V4L2_STD_PAL_BG
589                                             || dev->tvnorm & V4L2_STD_PAL_DK)
590                                                 bpl_local = 352 << 1;
591                                         else
592                                                 bpl_local =
593                                                     dev->
594                                                     cif_width[channel_opened] <<
595                                                     1;
596                                 }
597                         }
598                 }
599
600                 switch (buf->vb.field) {
601                 case V4L2_FIELD_TOP:
602                         cx25821_risc_buffer(dev->pci, &buf->risc,
603                                             dma->sglist, 0, UNSET,
604                                             buf->bpl, 0, buf->vb.height);
605                         break;
606                 case V4L2_FIELD_BOTTOM:
607                         cx25821_risc_buffer(dev->pci, &buf->risc,
608                                             dma->sglist, UNSET, 0,
609                                             buf->bpl, 0, buf->vb.height);
610                         break;
611                 case V4L2_FIELD_INTERLACED:
612                         /* All other formats are top field first */
613                         line0_offset = 0;
614                         line1_offset = buf->bpl;
615                         dprintk(1, "top field first\n");
616
617                         cx25821_risc_buffer(dev->pci, &buf->risc,
618                                             dma->sglist, line0_offset,
619                                             bpl_local, bpl_local, bpl_local,
620                                             buf->vb.height >> 1);
621                         break;
622                 case V4L2_FIELD_SEQ_TB:
623                         cx25821_risc_buffer(dev->pci, &buf->risc,
624                                             dma->sglist,
625                                             0, buf->bpl * (buf->vb.height >> 1),
626                                             buf->bpl, 0, buf->vb.height >> 1);
627                         break;
628                 case V4L2_FIELD_SEQ_BT:
629                         cx25821_risc_buffer(dev->pci, &buf->risc,
630                                             dma->sglist,
631                                             buf->bpl * (buf->vb.height >> 1), 0,
632                                             buf->bpl, 0, buf->vb.height >> 1);
633                         break;
634                 default:
635                         BUG();
636                 }
637         }
638
639         dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
640                 buf, buf->vb.i, fh->width, fh->height, fh->fmt->depth,
641                 fh->fmt->name, (unsigned long)buf->risc.dma);
642
643         buf->vb.state = VIDEOBUF_PREPARED;
644
645         return 0;
646
647       fail:
648         cx25821_free_buffer(q, buf);
649         return rc;
650 }
651
652 void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
653 {
654         struct cx25821_buffer *buf =
655             container_of(vb, struct cx25821_buffer, vb);
656
657         cx25821_free_buffer(q, buf);
658 }
659
660 struct videobuf_queue *get_queue(struct cx25821_fh *fh)
661 {
662         switch (fh->type) {
663         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
664                 return &fh->vidq;
665         default:
666                 BUG();
667                 return NULL;
668         }
669 }
670
671 int get_resource(struct cx25821_fh *fh, int resource)
672 {
673         switch (fh->type) {
674         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
675                 return resource;
676         default:
677                 BUG();
678                 return 0;
679         }
680 }
681
682 int video_mmap(struct file *file, struct vm_area_struct *vma)
683 {
684         struct cx25821_fh *fh = file->private_data;
685
686         return videobuf_mmap_mapper(get_queue(fh), vma);
687 }
688
689 /* VIDEO IOCTLS                                                       */
690 int vidioc_g_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f)
691 {
692         struct cx25821_fh *fh = priv;
693
694         f->fmt.pix.width = fh->width;
695         f->fmt.pix.height = fh->height;
696         f->fmt.pix.field = fh->vidq.field;
697         f->fmt.pix.pixelformat = fh->fmt->fourcc;
698         f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3;
699         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
700
701         return 0;
702 }
703
704 int vidioc_try_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f)
705 {
706         struct cx25821_fmt *fmt;
707         enum v4l2_field field;
708         unsigned int maxw, maxh;
709
710         fmt = format_by_fourcc(f->fmt.pix.pixelformat);
711         if (NULL == fmt)
712                 return -EINVAL;
713
714         field = f->fmt.pix.field;
715         maxw = 720;
716         maxh = 576;
717
718         if (V4L2_FIELD_ANY == field) {
719                 field = (f->fmt.pix.height > maxh / 2)
720                     ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
721         }
722
723         switch (field) {
724         case V4L2_FIELD_TOP:
725         case V4L2_FIELD_BOTTOM:
726                 maxh = maxh / 2;
727                 break;
728         case V4L2_FIELD_INTERLACED:
729                 break;
730         default:
731                 return -EINVAL;
732         }
733
734         f->fmt.pix.field = field;
735         if (f->fmt.pix.height < 32)
736                 f->fmt.pix.height = 32;
737         if (f->fmt.pix.height > maxh)
738                 f->fmt.pix.height = maxh;
739         if (f->fmt.pix.width < 48)
740                 f->fmt.pix.width = 48;
741         if (f->fmt.pix.width > maxw)
742                 f->fmt.pix.width = maxw;
743         f->fmt.pix.width &= ~0x03;
744         f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
745         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
746
747         return 0;
748 }
749
750 int vidioc_querycap(struct file *file, void *priv, struct v4l2_capability *cap)
751 {
752         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
753
754         strcpy(cap->driver, "cx25821");
755         strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
756         sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
757         cap->version = CX25821_VERSION_CODE;
758         cap->capabilities =
759             V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
760         if (UNSET != dev->tuner_type)
761                 cap->capabilities |= V4L2_CAP_TUNER;
762         return 0;
763 }
764
765 int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
766                             struct v4l2_fmtdesc *f)
767 {
768         if (unlikely(f->index >= ARRAY_SIZE(formats)))
769                 return -EINVAL;
770
771         strlcpy(f->description, formats[f->index].name, sizeof(f->description));
772         f->pixelformat = formats[f->index].fourcc;
773
774         return 0;
775 }
776
777 #ifdef CONFIG_VIDEO_V4L1_COMPAT
778 int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
779 {
780         struct cx25821_fh *fh = priv;
781         struct videobuf_queue *q;
782         struct v4l2_requestbuffers req;
783         unsigned int i;
784         int err;
785
786         q = get_queue(fh);
787         memset(&req, 0, sizeof(req));
788         req.type = q->type;
789         req.count = 8;
790         req.memory = V4L2_MEMORY_MMAP;
791         err = videobuf_reqbufs(q, &req);
792         if (err < 0)
793                 return err;
794
795         mbuf->frames = req.count;
796         mbuf->size = 0;
797         for (i = 0; i < mbuf->frames; i++) {
798                 mbuf->offsets[i] = q->bufs[i]->boff;
799                 mbuf->size += q->bufs[i]->bsize;
800         }
801         return 0;
802 }
803 #endif
804
805 int vidioc_reqbufs(struct file *file, void *priv, struct v4l2_requestbuffers *p)
806 {
807         struct cx25821_fh *fh = priv;
808         return videobuf_reqbufs(get_queue(fh), p);
809 }
810
811 int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
812 {
813         struct cx25821_fh *fh = priv;
814         return videobuf_querybuf(get_queue(fh), p);
815 }
816
817 int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
818 {
819         struct cx25821_fh *fh = priv;
820         return videobuf_qbuf(get_queue(fh), p);
821 }
822
823 int vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
824 {
825         struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
826
827         *p = v4l2_prio_max(&dev->prio);
828
829         return 0;
830 }
831
832 int vidioc_s_priority(struct file *file, void *f, enum v4l2_priority prio)
833 {
834         struct cx25821_fh *fh = f;
835         struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
836
837         return v4l2_prio_change(&dev->prio, &fh->prio, prio);
838 }
839
840 #ifdef TUNER_FLAG
841 int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * tvnorms)
842 {
843         struct cx25821_fh *fh = priv;
844         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
845         int err;
846
847         dprintk(1, "%s()\n", __func__);
848
849         if (fh) {
850                 err = v4l2_prio_check(&dev->prio, &fh->prio);
851                 if (0 != err)
852                         return err;
853         }
854
855         if (dev->tvnorm == *tvnorms) {
856                 return 0;
857         }
858
859         mutex_lock(&dev->lock);
860         cx25821_set_tvnorm(dev, *tvnorms);
861         mutex_unlock(&dev->lock);
862
863         medusa_set_videostandard(dev);
864
865         return 0;
866 }
867 #endif
868
869 int cx25821_enum_input(struct cx25821_dev *dev, struct v4l2_input *i)
870 {
871         static const char *iname[] = {
872                 [CX25821_VMUX_COMPOSITE] = "Composite",
873                 [CX25821_VMUX_SVIDEO] = "S-Video",
874                 [CX25821_VMUX_DEBUG] = "for debug only",
875         };
876         unsigned int n;
877         dprintk(1, "%s()\n", __func__);
878
879         n = i->index;
880         if (n > 2)
881                 return -EINVAL;
882
883         if (0 == INPUT(n)->type)
884                 return -EINVAL;
885
886         memset(i, 0, sizeof(*i));
887         i->index = n;
888         i->type = V4L2_INPUT_TYPE_CAMERA;
889         strcpy(i->name, iname[INPUT(n)->type]);
890
891         i->std = CX25821_NORMS;
892         return 0;
893 }
894
895 int vidioc_enum_input(struct file *file, void *priv, struct v4l2_input *i)
896 {
897         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
898         dprintk(1, "%s()\n", __func__);
899         return cx25821_enum_input(dev, i);
900 }
901
902 int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
903 {
904         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
905
906         *i = dev->input;
907         dprintk(1, "%s() returns %d\n", __func__, *i);
908         return 0;
909 }
910
911 int vidioc_s_input(struct file *file, void *priv, unsigned int i)
912 {
913         struct cx25821_fh *fh = priv;
914         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
915         int err;
916
917         dprintk(1, "%s(%d)\n", __func__, i);
918
919         if (fh) {
920                 err = v4l2_prio_check(&dev->prio, &fh->prio);
921                 if (0 != err)
922                         return err;
923         }
924
925         if (i > 2) {
926                 dprintk(1, "%s() -EINVAL\n", __func__);
927                 return -EINVAL;
928         }
929
930         mutex_lock(&dev->lock);
931         cx25821_video_mux(dev, i);
932         mutex_unlock(&dev->lock);
933         return 0;
934 }
935
936 #ifdef TUNER_FLAG
937 int vidioc_g_frequency(struct file *file, void *priv, struct v4l2_frequency *f)
938 {
939         struct cx25821_fh *fh = priv;
940         struct cx25821_dev *dev = fh->dev;
941
942         f->frequency = dev->freq;
943
944         cx25821_call_all(dev, tuner, g_frequency, f);
945
946         return 0;
947 }
948
949 int cx25821_set_freq(struct cx25821_dev *dev, struct v4l2_frequency *f)
950 {
951         mutex_lock(&dev->lock);
952         dev->freq = f->frequency;
953
954         cx25821_call_all(dev, tuner, s_frequency, f);
955
956         /* When changing channels it is required to reset TVAUDIO */
957         msleep(10);
958
959         mutex_unlock(&dev->lock);
960
961         return 0;
962 }
963
964 int vidioc_s_frequency(struct file *file, void *priv, struct v4l2_frequency *f)
965 {
966         struct cx25821_fh *fh = priv;
967         struct cx25821_dev *dev = fh->dev;
968         int err;
969
970         if (fh) {
971                 err = v4l2_prio_check(&dev->prio, &fh->prio);
972                 if (0 != err)
973                         return err;
974         }
975
976         return cx25821_set_freq(dev, f);
977 }
978 #endif
979
980 #ifdef CONFIG_VIDEO_ADV_DEBUG
981 int vidioc_g_register(struct file *file, void *fh,
982                       struct v4l2_dbg_register *reg)
983 {
984         struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
985
986         if (!v4l2_chip_match_host(&reg->match))
987                 return -EINVAL;
988
989         cx25821_call_all(dev, core, g_register, reg);
990
991         return 0;
992 }
993
994 int vidioc_s_register(struct file *file, void *fh,
995                       struct v4l2_dbg_register *reg)
996 {
997         struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
998
999         if (!v4l2_chip_match_host(&reg->match))
1000                 return -EINVAL;
1001
1002         cx25821_call_all(dev, core, s_register, reg);
1003
1004         return 0;
1005 }
1006
1007 #endif
1008
1009 #ifdef TUNER_FLAG
1010 int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1011 {
1012         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1013
1014         if (unlikely(UNSET == dev->tuner_type))
1015                 return -EINVAL;
1016         if (0 != t->index)
1017                 return -EINVAL;
1018
1019         strcpy(t->name, "Television");
1020         t->type = V4L2_TUNER_ANALOG_TV;
1021         t->capability = V4L2_TUNER_CAP_NORM;
1022         t->rangehigh = 0xffffffffUL;
1023
1024         t->signal = 0xffff;     /* LOCKED */
1025         return 0;
1026 }
1027
1028 int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
1029 {
1030         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1031         struct cx25821_fh *fh = priv;
1032         int err;
1033
1034         if (fh) {
1035                 err = v4l2_prio_check(&dev->prio, &fh->prio);
1036                 if (0 != err)
1037                         return err;
1038         }
1039
1040         dprintk(1, "%s()\n", __func__);
1041         if (UNSET == dev->tuner_type)
1042                 return -EINVAL;
1043         if (0 != t->index)
1044                 return -EINVAL;
1045
1046         return 0;
1047 }
1048
1049 #endif
1050 // ******************************************************************************************
1051 static const struct v4l2_queryctrl no_ctl = {
1052         .name = "42",
1053         .flags = V4L2_CTRL_FLAG_DISABLED,
1054 };
1055
1056 static struct v4l2_queryctrl cx25821_ctls[] = {
1057         /* --- video --- */
1058         {
1059          .id = V4L2_CID_BRIGHTNESS,
1060          .name = "Brightness",
1061          .minimum = 0,
1062          .maximum = 10000,
1063          .step = 1,
1064          .default_value = 6200,
1065          .type = V4L2_CTRL_TYPE_INTEGER,
1066          }, {
1067              .id = V4L2_CID_CONTRAST,
1068              .name = "Contrast",
1069              .minimum = 0,
1070              .maximum = 10000,
1071              .step = 1,
1072              .default_value = 5000,
1073              .type = V4L2_CTRL_TYPE_INTEGER,
1074              }, {
1075                  .id = V4L2_CID_SATURATION,
1076                  .name = "Saturation",
1077                  .minimum = 0,
1078                  .maximum = 10000,
1079                  .step = 1,
1080                  .default_value = 5000,
1081                  .type = V4L2_CTRL_TYPE_INTEGER,
1082                  }, {
1083                      .id = V4L2_CID_HUE,
1084                      .name = "Hue",
1085                      .minimum = 0,
1086                      .maximum = 10000,
1087                      .step = 1,
1088                      .default_value = 5000,
1089                      .type = V4L2_CTRL_TYPE_INTEGER,
1090                      }
1091 };
1092 static const int CX25821_CTLS = ARRAY_SIZE(cx25821_ctls);
1093
1094 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
1095 {
1096         int i;
1097
1098         if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
1099                 return -EINVAL;
1100         for (i = 0; i < CX25821_CTLS; i++)
1101                 if (cx25821_ctls[i].id == qctrl->id)
1102                         break;
1103         if (i == CX25821_CTLS) {
1104                 *qctrl = no_ctl;
1105                 return 0;
1106         }
1107         *qctrl = cx25821_ctls[i];
1108         return 0;
1109 }
1110
1111 int vidioc_queryctrl(struct file *file, void *priv,
1112                      struct v4l2_queryctrl *qctrl)
1113 {
1114         return cx25821_ctrl_query(qctrl);
1115 }
1116
1117 /* ------------------------------------------------------------------ */
1118 /* VIDEO CTRL IOCTLS                                                  */
1119
1120 static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
1121 {
1122         unsigned int i;
1123
1124         for (i = 0; i < CX25821_CTLS; i++)
1125                 if (cx25821_ctls[i].id == id)
1126                         return cx25821_ctls + i;
1127         return NULL;
1128 }
1129
1130 int vidioc_g_ctrl(struct file *file, void *priv, struct v4l2_control *ctl)
1131 {
1132         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1133
1134         const struct v4l2_queryctrl *ctrl;
1135
1136         ctrl = ctrl_by_id(ctl->id);
1137
1138         if (NULL == ctrl)
1139                 return -EINVAL;
1140         switch (ctl->id) {
1141         case V4L2_CID_BRIGHTNESS:
1142                 ctl->value = dev->ctl_bright;
1143                 break;
1144         case V4L2_CID_HUE:
1145                 ctl->value = dev->ctl_hue;
1146                 break;
1147         case V4L2_CID_CONTRAST:
1148                 ctl->value = dev->ctl_contrast;
1149                 break;
1150         case V4L2_CID_SATURATION:
1151                 ctl->value = dev->ctl_saturation;
1152                 break;
1153         }
1154         return 0;
1155 }
1156
1157 int cx25821_set_control(struct cx25821_dev *dev,
1158                         struct v4l2_control *ctl, int chan_num)
1159 {
1160         int err;
1161         const struct v4l2_queryctrl *ctrl;
1162
1163         err = -EINVAL;
1164
1165         ctrl = ctrl_by_id(ctl->id);
1166
1167         if (NULL == ctrl)
1168                 return err;
1169
1170         switch (ctrl->type) {
1171         case V4L2_CTRL_TYPE_BOOLEAN:
1172         case V4L2_CTRL_TYPE_MENU:
1173         case V4L2_CTRL_TYPE_INTEGER:
1174                 if (ctl->value < ctrl->minimum)
1175                         ctl->value = ctrl->minimum;
1176                 if (ctl->value > ctrl->maximum)
1177                         ctl->value = ctrl->maximum;
1178                 break;
1179         default:
1180                 /* nothing */ ;
1181         };
1182
1183         switch (ctl->id) {
1184         case V4L2_CID_BRIGHTNESS:
1185                 dev->ctl_bright = ctl->value;
1186                 medusa_set_brightness(dev, ctl->value, chan_num);
1187                 break;
1188         case V4L2_CID_HUE:
1189                 dev->ctl_hue = ctl->value;
1190                 medusa_set_hue(dev, ctl->value, chan_num);
1191                 break;
1192         case V4L2_CID_CONTRAST:
1193                 dev->ctl_contrast = ctl->value;
1194                 medusa_set_contrast(dev, ctl->value, chan_num);
1195                 break;
1196         case V4L2_CID_SATURATION:
1197                 dev->ctl_saturation = ctl->value;
1198                 medusa_set_saturation(dev, ctl->value, chan_num);
1199                 break;
1200         }
1201
1202         err = 0;
1203
1204         return err;
1205 }
1206
1207 static void init_controls(struct cx25821_dev *dev, int chan_num)
1208 {
1209         struct v4l2_control ctrl;
1210         int i;
1211         for (i = 0; i < CX25821_CTLS; i++) {
1212                 ctrl.id = cx25821_ctls[i].id;
1213                 ctrl.value = cx25821_ctls[i].default_value;
1214
1215                 cx25821_set_control(dev, &ctrl, chan_num);
1216         }
1217 }
1218
1219 int vidioc_cropcap(struct file *file, void *priv, struct v4l2_cropcap *cropcap)
1220 {
1221         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1222
1223         if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1224                 return -EINVAL;
1225         cropcap->bounds.top = cropcap->bounds.left = 0;
1226         cropcap->bounds.width = 720;
1227         cropcap->bounds.height = dev->tvnorm == V4L2_STD_PAL_BG ? 576 : 480;
1228         cropcap->pixelaspect.numerator =
1229             dev->tvnorm == V4L2_STD_PAL_BG ? 59 : 10;
1230         cropcap->pixelaspect.denominator =
1231             dev->tvnorm == V4L2_STD_PAL_BG ? 54 : 11;
1232         cropcap->defrect = cropcap->bounds;
1233         return 0;
1234 }
1235
1236 int vidioc_s_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1237 {
1238         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1239         struct cx25821_fh *fh = priv;
1240         int err;
1241
1242         if (fh) {
1243                 err = v4l2_prio_check(&dev->prio, &fh->prio);
1244                 if (0 != err)
1245                         return err;
1246         }
1247         // vidioc_s_crop not supported
1248         return -EINVAL;
1249 }
1250
1251 int vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1252 {
1253         // vidioc_g_crop not supported
1254         return -EINVAL;
1255 }
1256
1257 int vidioc_querystd(struct file *file, void *priv, v4l2_std_id * norm)
1258 {
1259         // medusa does not support video standard sensing of current input
1260         *norm = CX25821_NORMS;
1261
1262         return 0;
1263 }
1264
1265 int is_valid_width(u32 width, v4l2_std_id tvnorm)
1266 {
1267         if (tvnorm == V4L2_STD_PAL_BG) {
1268                 if (width == 352 || width == 720)
1269                         return 1;
1270                 else
1271                         return 0;
1272         }
1273
1274         if (tvnorm == V4L2_STD_NTSC_M) {
1275                 if (width == 320 || width == 352 || width == 720)
1276                         return 1;
1277                 else
1278                         return 0;
1279         }
1280         return 0;
1281 }
1282
1283 int is_valid_height(u32 height, v4l2_std_id tvnorm)
1284 {
1285         if (tvnorm == V4L2_STD_PAL_BG) {
1286                 if (height == 576 || height == 288)
1287                         return 1;
1288                 else
1289                         return 0;
1290         }
1291
1292         if (tvnorm == V4L2_STD_NTSC_M) {
1293                 if (height == 480 || height == 240)
1294                         return 1;
1295                 else
1296                         return 0;
1297         }
1298
1299         return 0;
1300 }