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