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