[media] cx25821: make cx25821_sram_channels const
[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 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                             const 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                                        const 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         const 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         const 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         const struct sram_channel *sram_ch =
791                 dev->channels[0].sram_channels;
792
793         /* stop the risc engine and fifo */
794         cx_write(sram_ch->dma_ctl, 0); /* FIFO and RISC disable */
795
796         /* stop video capture */
797         if (cx25821_res_check(fh, RESOURCE_VIDEO0)) {
798                 videobuf_queue_cancel(&fh->vidq);
799                 cx25821_res_free(dev, fh, RESOURCE_VIDEO0);
800         }
801
802         if (fh->vidq.read_buf) {
803                 cx25821_buffer_release(&fh->vidq, fh->vidq.read_buf);
804                 kfree(fh->vidq.read_buf);
805         }
806
807         videobuf_mmap_free(&fh->vidq);
808
809         v4l2_prio_close(&dev->channels[fh->channel_id].prio, fh->prio);
810         file->private_data = NULL;
811         kfree(fh);
812
813         return 0;
814 }
815
816 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
817 {
818         struct cx25821_fh *fh = priv;
819         struct cx25821_dev *dev = fh->dev;
820
821         if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
822                 return -EINVAL;
823
824         if (unlikely(i != fh->type))
825                 return -EINVAL;
826
827         if (unlikely(!cx25821_res_get(dev, fh, cx25821_get_resource(fh,
828                                                 RESOURCE_VIDEO0))))
829                 return -EBUSY;
830
831         return videobuf_streamon(get_queue(fh));
832 }
833
834 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
835 {
836         struct cx25821_fh *fh = priv;
837         struct cx25821_dev *dev = fh->dev;
838         int err, res;
839
840         if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
841                 return -EINVAL;
842         if (i != fh->type)
843                 return -EINVAL;
844
845         res = cx25821_get_resource(fh, RESOURCE_VIDEO0);
846         err = videobuf_streamoff(get_queue(fh));
847         if (err < 0)
848                 return err;
849         cx25821_res_free(dev, fh, res);
850         return 0;
851 }
852
853 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
854                                 struct v4l2_format *f)
855 {
856         struct cx25821_fh *fh = priv;
857         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
858         struct v4l2_mbus_framefmt mbus_fmt;
859         int err;
860         int pix_format = PIXEL_FRMT_422;
861
862         if (fh) {
863                 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
864                                       fh->prio);
865                 if (0 != err)
866                         return err;
867         }
868
869         dprintk(2, "%s()\n", __func__);
870         err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f);
871
872         if (0 != err)
873                 return err;
874
875         fh->fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
876         fh->vidq.field = f->fmt.pix.field;
877
878         /* check if width and height is valid based on set standard */
879         if (cx25821_is_valid_width(f->fmt.pix.width, dev->tvnorm))
880                 fh->width = f->fmt.pix.width;
881
882         if (cx25821_is_valid_height(f->fmt.pix.height, dev->tvnorm))
883                 fh->height = f->fmt.pix.height;
884
885         if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
886                 pix_format = PIXEL_FRMT_411;
887         else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
888                 pix_format = PIXEL_FRMT_422;
889         else
890                 return -EINVAL;
891
892         cx25821_set_pixel_format(dev, SRAM_CH00, pix_format);
893
894         /* check if cif resolution */
895         if (fh->width == 320 || fh->width == 352)
896                 dev->channels[fh->channel_id].use_cif_resolution = 1;
897         else
898                 dev->channels[fh->channel_id].use_cif_resolution = 0;
899
900         dev->channels[fh->channel_id].cif_width = fh->width;
901         medusa_set_resolution(dev, fh->width, SRAM_CH00);
902
903         dprintk(2, "%s(): width=%d height=%d field=%d\n", __func__, fh->width,
904                 fh->height, fh->vidq.field);
905         v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
906         cx25821_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
907
908         return 0;
909 }
910
911 static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
912 {
913         int ret_val = 0;
914         struct cx25821_fh *fh = priv;
915         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
916
917         ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
918
919         p->sequence = dev->channels[fh->channel_id].vidq.count;
920
921         return ret_val;
922 }
923
924 static int vidioc_log_status(struct file *file, void *priv)
925 {
926         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
927         struct cx25821_fh *fh = priv;
928         const struct sram_channel *sram_ch =
929                 dev->channels[fh->channel_id].sram_channels;
930         u32 tmp = 0;
931
932         cx25821_call_all(dev, core, log_status);
933         tmp = cx_read(sram_ch->dma_ctl);
934         pr_info("Video input 0 is %s\n",
935                 (tmp & 0x11) ? "streaming" : "stopped");
936         return 0;
937 }
938
939 static int vidioc_s_ctrl(struct file *file, void *priv,
940                         struct v4l2_control *ctl)
941 {
942         struct cx25821_fh *fh = priv;
943         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
944         int err;
945
946         if (fh) {
947                 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
948                                       fh->prio);
949                 if (0 != err)
950                         return err;
951         }
952
953         return cx25821_set_control(dev, ctl, fh->channel_id);
954 }
955
956 /* VIDEO IOCTLS */
957 int cx25821_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
958                                  struct v4l2_format *f)
959 {
960         struct cx25821_fh *fh = priv;
961
962         f->fmt.pix.width = fh->width;
963         f->fmt.pix.height = fh->height;
964         f->fmt.pix.field = fh->vidq.field;
965         f->fmt.pix.pixelformat = fh->fmt->fourcc;
966         f->fmt.pix.bytesperline = (f->fmt.pix.width * fh->fmt->depth) >> 3;
967         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
968
969         return 0;
970 }
971
972 int cx25821_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
973                                    struct v4l2_format *f)
974 {
975         struct cx25821_fmt *fmt;
976         enum v4l2_field field;
977         unsigned int maxw, maxh;
978
979         fmt = cx25821_format_by_fourcc(f->fmt.pix.pixelformat);
980         if (NULL == fmt)
981                 return -EINVAL;
982
983         field = f->fmt.pix.field;
984         maxw = 720;
985         maxh = 576;
986
987         if (V4L2_FIELD_ANY == field) {
988                 if (f->fmt.pix.height > maxh / 2)
989                         field = V4L2_FIELD_INTERLACED;
990                 else
991                         field = V4L2_FIELD_TOP;
992         }
993
994         switch (field) {
995         case V4L2_FIELD_TOP:
996         case V4L2_FIELD_BOTTOM:
997                 maxh = maxh / 2;
998                 break;
999         case V4L2_FIELD_INTERLACED:
1000                 break;
1001         default:
1002                 return -EINVAL;
1003         }
1004
1005         f->fmt.pix.field = field;
1006         if (f->fmt.pix.height < 32)
1007                 f->fmt.pix.height = 32;
1008         if (f->fmt.pix.height > maxh)
1009                 f->fmt.pix.height = maxh;
1010         if (f->fmt.pix.width < 48)
1011                 f->fmt.pix.width = 48;
1012         if (f->fmt.pix.width > maxw)
1013                 f->fmt.pix.width = maxw;
1014         f->fmt.pix.width &= ~0x03;
1015         f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
1016         f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
1017
1018         return 0;
1019 }
1020
1021 int cx25821_vidioc_querycap(struct file *file, void *priv,
1022                             struct v4l2_capability *cap)
1023 {
1024         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1025         struct cx25821_fh *fh = priv;
1026         const u32 cap_input = V4L2_CAP_VIDEO_CAPTURE |
1027                         V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1028         const u32 cap_output = V4L2_CAP_VIDEO_OUTPUT;
1029
1030         strcpy(cap->driver, "cx25821");
1031         strlcpy(cap->card, cx25821_boards[dev->board].name, sizeof(cap->card));
1032         sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1033         if (fh->channel_id >= VID_CHANNEL_NUM)
1034                 cap->device_caps = cap_output;
1035         else
1036                 cap->device_caps = cap_input;
1037         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1038         return 0;
1039 }
1040
1041 int cx25821_vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1042                             struct v4l2_fmtdesc *f)
1043 {
1044         if (unlikely(f->index >= ARRAY_SIZE(formats)))
1045                 return -EINVAL;
1046
1047         strlcpy(f->description, formats[f->index].name, sizeof(f->description));
1048         f->pixelformat = formats[f->index].fourcc;
1049
1050         return 0;
1051 }
1052
1053 int cx25821_vidioc_reqbufs(struct file *file, void *priv,
1054                            struct v4l2_requestbuffers *p)
1055 {
1056         struct cx25821_fh *fh = priv;
1057         return videobuf_reqbufs(get_queue(fh), p);
1058 }
1059
1060 int cx25821_vidioc_querybuf(struct file *file, void *priv,
1061                             struct v4l2_buffer *p)
1062 {
1063         struct cx25821_fh *fh = priv;
1064         return videobuf_querybuf(get_queue(fh), p);
1065 }
1066
1067 int cx25821_vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
1068 {
1069         struct cx25821_fh *fh = priv;
1070         return videobuf_qbuf(get_queue(fh), p);
1071 }
1072
1073 int cx25821_vidioc_g_priority(struct file *file, void *f, enum v4l2_priority *p)
1074 {
1075         struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1076         struct cx25821_fh *fh = f;
1077
1078         *p = v4l2_prio_max(&dev->channels[fh->channel_id].prio);
1079
1080         return 0;
1081 }
1082
1083 int cx25821_vidioc_s_priority(struct file *file, void *f,
1084                               enum v4l2_priority prio)
1085 {
1086         struct cx25821_fh *fh = f;
1087         struct cx25821_dev *dev = ((struct cx25821_fh *)f)->dev;
1088
1089         return v4l2_prio_change(&dev->channels[fh->channel_id].prio, &fh->prio,
1090                         prio);
1091 }
1092
1093 int cx25821_vidioc_g_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1094 {
1095         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1096
1097         *tvnorms = dev->tvnorm;
1098         return 0;
1099 }
1100
1101 int cx25821_vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
1102 {
1103         struct cx25821_fh *fh = priv;
1104         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1105         int err;
1106
1107         dprintk(1, "%s()\n", __func__);
1108
1109         if (fh) {
1110                 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1111                                       fh->prio);
1112                 if (0 != err)
1113                         return err;
1114         }
1115
1116         if (dev->tvnorm == tvnorms)
1117                 return 0;
1118
1119         mutex_lock(&dev->lock);
1120         cx25821_set_tvnorm(dev, tvnorms);
1121         mutex_unlock(&dev->lock);
1122
1123         medusa_set_videostandard(dev);
1124
1125         return 0;
1126 }
1127
1128 int cx25821_enum_input(struct cx25821_dev *dev, struct v4l2_input *i)
1129 {
1130         static const char * const iname[] = {
1131                 [CX25821_VMUX_COMPOSITE] = "Composite",
1132                 [CX25821_VMUX_SVIDEO] = "S-Video",
1133                 [CX25821_VMUX_DEBUG] = "for debug only",
1134         };
1135         unsigned int n;
1136         dprintk(1, "%s()\n", __func__);
1137
1138         n = i->index;
1139         if (n >= 2)
1140                 return -EINVAL;
1141
1142         if (0 == INPUT(n)->type)
1143                 return -EINVAL;
1144
1145         i->type = V4L2_INPUT_TYPE_CAMERA;
1146         strcpy(i->name, iname[INPUT(n)->type]);
1147
1148         i->std = CX25821_NORMS;
1149         return 0;
1150 }
1151
1152 int cx25821_vidioc_enum_input(struct file *file, void *priv,
1153                               struct v4l2_input *i)
1154 {
1155         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1156         dprintk(1, "%s()\n", __func__);
1157         return cx25821_enum_input(dev, i);
1158 }
1159
1160 int cx25821_vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1161 {
1162         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1163
1164         *i = dev->input;
1165         dprintk(1, "%s(): returns %d\n", __func__, *i);
1166         return 0;
1167 }
1168
1169 int cx25821_vidioc_s_input(struct file *file, void *priv, unsigned int i)
1170 {
1171         struct cx25821_fh *fh = priv;
1172         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1173         int err;
1174
1175         dprintk(1, "%s(%d)\n", __func__, i);
1176
1177         if (fh) {
1178                 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1179                                       fh->prio);
1180                 if (0 != err)
1181                         return err;
1182         }
1183
1184         if (i >= CX25821_NR_INPUT) {
1185                 dprintk(1, "%s(): -EINVAL\n", __func__);
1186                 return -EINVAL;
1187         }
1188
1189         mutex_lock(&dev->lock);
1190         cx25821_video_mux(dev, i);
1191         mutex_unlock(&dev->lock);
1192         return 0;
1193 }
1194
1195 #ifdef CONFIG_VIDEO_ADV_DEBUG
1196 int cx25821_vidioc_g_register(struct file *file, void *fh,
1197                       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, g_register, reg);
1205
1206         return 0;
1207 }
1208
1209 int cx25821_vidioc_s_register(struct file *file, void *fh,
1210                       const struct v4l2_dbg_register *reg)
1211 {
1212         struct cx25821_dev *dev = ((struct cx25821_fh *)fh)->dev;
1213
1214         if (!v4l2_chip_match_host(&reg->match))
1215                 return -EINVAL;
1216
1217         cx25821_call_all(dev, core, s_register, reg);
1218
1219         return 0;
1220 }
1221
1222 #endif
1223
1224 /*****************************************************************************/
1225 static const struct v4l2_queryctrl no_ctl = {
1226         .name = "42",
1227         .flags = V4L2_CTRL_FLAG_DISABLED,
1228 };
1229
1230 static struct v4l2_queryctrl cx25821_ctls[] = {
1231         /* --- video --- */
1232         {
1233                 .id = V4L2_CID_BRIGHTNESS,
1234                 .name = "Brightness",
1235                 .minimum = 0,
1236                 .maximum = 10000,
1237                 .step = 1,
1238                 .default_value = 6200,
1239                 .type = V4L2_CTRL_TYPE_INTEGER,
1240         }, {
1241                 .id = V4L2_CID_CONTRAST,
1242                 .name = "Contrast",
1243                 .minimum = 0,
1244                 .maximum = 10000,
1245                 .step = 1,
1246                 .default_value = 5000,
1247                 .type = V4L2_CTRL_TYPE_INTEGER,
1248         }, {
1249                 .id = V4L2_CID_SATURATION,
1250                 .name = "Saturation",
1251                 .minimum = 0,
1252                 .maximum = 10000,
1253                 .step = 1,
1254                 .default_value = 5000,
1255                 .type = V4L2_CTRL_TYPE_INTEGER,
1256         }, {
1257                 .id = V4L2_CID_HUE,
1258                 .name = "Hue",
1259                 .minimum = 0,
1260                 .maximum = 10000,
1261                 .step = 1,
1262                 .default_value = 5000,
1263                 .type = V4L2_CTRL_TYPE_INTEGER,
1264         }
1265 };
1266 static const int CX25821_CTLS = ARRAY_SIZE(cx25821_ctls);
1267
1268 static int cx25821_ctrl_query(struct v4l2_queryctrl *qctrl)
1269 {
1270         int i;
1271
1272         if (qctrl->id < V4L2_CID_BASE || qctrl->id >= V4L2_CID_LASTP1)
1273                 return -EINVAL;
1274         for (i = 0; i < CX25821_CTLS; i++)
1275                 if (cx25821_ctls[i].id == qctrl->id)
1276                         break;
1277         if (i == CX25821_CTLS) {
1278                 *qctrl = no_ctl;
1279                 return 0;
1280         }
1281         *qctrl = cx25821_ctls[i];
1282         return 0;
1283 }
1284
1285 int cx25821_vidioc_queryctrl(struct file *file, void *priv,
1286                      struct v4l2_queryctrl *qctrl)
1287 {
1288         return cx25821_ctrl_query(qctrl);
1289 }
1290
1291 /* ------------------------------------------------------------------ */
1292 /* VIDEO CTRL IOCTLS                                                  */
1293
1294 static const struct v4l2_queryctrl *ctrl_by_id(unsigned int id)
1295 {
1296         unsigned int i;
1297
1298         for (i = 0; i < CX25821_CTLS; i++)
1299                 if (cx25821_ctls[i].id == id)
1300                         return cx25821_ctls + i;
1301         return NULL;
1302 }
1303
1304 int cx25821_vidioc_g_ctrl(struct file *file, void *priv,
1305                           struct v4l2_control *ctl)
1306 {
1307         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1308         struct cx25821_fh *fh = priv;
1309
1310         const struct v4l2_queryctrl *ctrl;
1311
1312         ctrl = ctrl_by_id(ctl->id);
1313
1314         if (NULL == ctrl)
1315                 return -EINVAL;
1316         switch (ctl->id) {
1317         case V4L2_CID_BRIGHTNESS:
1318                 ctl->value = dev->channels[fh->channel_id].ctl_bright;
1319                 break;
1320         case V4L2_CID_HUE:
1321                 ctl->value = dev->channels[fh->channel_id].ctl_hue;
1322                 break;
1323         case V4L2_CID_CONTRAST:
1324                 ctl->value = dev->channels[fh->channel_id].ctl_contrast;
1325                 break;
1326         case V4L2_CID_SATURATION:
1327                 ctl->value = dev->channels[fh->channel_id].ctl_saturation;
1328                 break;
1329         }
1330         return 0;
1331 }
1332
1333 int cx25821_set_control(struct cx25821_dev *dev,
1334                         struct v4l2_control *ctl, int chan_num)
1335 {
1336         int err;
1337         const struct v4l2_queryctrl *ctrl;
1338
1339         err = -EINVAL;
1340
1341         ctrl = ctrl_by_id(ctl->id);
1342
1343         if (NULL == ctrl)
1344                 return err;
1345
1346         switch (ctrl->type) {
1347         case V4L2_CTRL_TYPE_BOOLEAN:
1348         case V4L2_CTRL_TYPE_MENU:
1349         case V4L2_CTRL_TYPE_INTEGER:
1350                 if (ctl->value < ctrl->minimum)
1351                         ctl->value = ctrl->minimum;
1352                 if (ctl->value > ctrl->maximum)
1353                         ctl->value = ctrl->maximum;
1354                 break;
1355         default:
1356                 /* nothing */ ;
1357         }
1358
1359         switch (ctl->id) {
1360         case V4L2_CID_BRIGHTNESS:
1361                 dev->channels[chan_num].ctl_bright = ctl->value;
1362                 medusa_set_brightness(dev, ctl->value, chan_num);
1363                 break;
1364         case V4L2_CID_HUE:
1365                 dev->channels[chan_num].ctl_hue = ctl->value;
1366                 medusa_set_hue(dev, ctl->value, chan_num);
1367                 break;
1368         case V4L2_CID_CONTRAST:
1369                 dev->channels[chan_num].ctl_contrast = ctl->value;
1370                 medusa_set_contrast(dev, ctl->value, chan_num);
1371                 break;
1372         case V4L2_CID_SATURATION:
1373                 dev->channels[chan_num].ctl_saturation = ctl->value;
1374                 medusa_set_saturation(dev, ctl->value, chan_num);
1375                 break;
1376         }
1377
1378         err = 0;
1379
1380         return err;
1381 }
1382
1383 static void cx25821_init_controls(struct cx25821_dev *dev, int chan_num)
1384 {
1385         struct v4l2_control ctrl;
1386         int i;
1387         for (i = 0; i < CX25821_CTLS; i++) {
1388                 ctrl.id = cx25821_ctls[i].id;
1389                 ctrl.value = cx25821_ctls[i].default_value;
1390
1391                 cx25821_set_control(dev, &ctrl, chan_num);
1392         }
1393 }
1394
1395 int cx25821_vidioc_cropcap(struct file *file, void *priv,
1396                            struct v4l2_cropcap *cropcap)
1397 {
1398         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1399
1400         if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1401                 return -EINVAL;
1402         cropcap->bounds.top = 0;
1403         cropcap->bounds.left = 0;
1404         cropcap->bounds.width = 720;
1405         cropcap->bounds.height = dev->tvnorm == V4L2_STD_PAL_BG ? 576 : 480;
1406         cropcap->pixelaspect.numerator =
1407                 dev->tvnorm == V4L2_STD_PAL_BG ? 59 : 10;
1408         cropcap->pixelaspect.denominator =
1409                 dev->tvnorm == V4L2_STD_PAL_BG ? 54 : 11;
1410         cropcap->defrect = cropcap->bounds;
1411         return 0;
1412 }
1413
1414 int cx25821_vidioc_s_crop(struct file *file, void *priv, const struct v4l2_crop *crop)
1415 {
1416         struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
1417         struct cx25821_fh *fh = priv;
1418         int err;
1419
1420         if (fh) {
1421                 err = v4l2_prio_check(&dev->channels[fh->channel_id].prio,
1422                                       fh->prio);
1423                 if (0 != err)
1424                         return err;
1425         }
1426         /* cx25821_vidioc_s_crop not supported */
1427         return -EINVAL;
1428 }
1429
1430 int cx25821_vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
1431 {
1432         /* cx25821_vidioc_g_crop not supported */
1433         return -EINVAL;
1434 }
1435
1436 int cx25821_is_valid_width(u32 width, v4l2_std_id tvnorm)
1437 {
1438         if (tvnorm == V4L2_STD_PAL_BG) {
1439                 if (width == 352 || width == 720)
1440                         return 1;
1441                 else
1442                         return 0;
1443         }
1444
1445         if (tvnorm == V4L2_STD_NTSC_M) {
1446                 if (width == 320 || width == 352 || width == 720)
1447                         return 1;
1448                 else
1449                         return 0;
1450         }
1451         return 0;
1452 }
1453
1454 int cx25821_is_valid_height(u32 height, v4l2_std_id tvnorm)
1455 {
1456         if (tvnorm == V4L2_STD_PAL_BG) {
1457                 if (height == 576 || height == 288)
1458                         return 1;
1459                 else
1460                         return 0;
1461         }
1462
1463         if (tvnorm == V4L2_STD_NTSC_M) {
1464                 if (height == 480 || height == 240)
1465                         return 1;
1466                 else
1467                         return 0;
1468         }
1469
1470         return 0;
1471 }
1472
1473 static long video_ioctl_upstream9(struct file *file, unsigned int cmd,
1474                                  unsigned long arg)
1475 {
1476         struct cx25821_fh *fh = file->private_data;
1477         struct cx25821_dev *dev = fh->dev;
1478         int command = 0;
1479         struct upstream_user_struct *data_from_user;
1480
1481         data_from_user = (struct upstream_user_struct *)arg;
1482
1483         if (!data_from_user) {
1484                 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1485                 return 0;
1486         }
1487
1488         command = data_from_user->command;
1489
1490         if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
1491                 return 0;
1492
1493         dev->input_filename = data_from_user->input_filename;
1494         dev->input_audiofilename = data_from_user->input_filename;
1495         dev->vid_stdname = data_from_user->vid_stdname;
1496         dev->pixel_format = data_from_user->pixel_format;
1497         dev->channel_select = data_from_user->channel_select;
1498         dev->command = data_from_user->command;
1499
1500         switch (command) {
1501         case UPSTREAM_START_VIDEO:
1502                 cx25821_start_upstream_video_ch1(dev, data_from_user);
1503                 break;
1504
1505         case UPSTREAM_STOP_VIDEO:
1506                 cx25821_stop_upstream_video_ch1(dev);
1507                 break;
1508         }
1509
1510         return 0;
1511 }
1512
1513 static long video_ioctl_upstream10(struct file *file, unsigned int cmd,
1514                                   unsigned long arg)
1515 {
1516         struct cx25821_fh *fh = file->private_data;
1517         struct cx25821_dev *dev = fh->dev;
1518         int command = 0;
1519         struct upstream_user_struct *data_from_user;
1520
1521         data_from_user = (struct upstream_user_struct *)arg;
1522
1523         if (!data_from_user) {
1524                 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1525                 return 0;
1526         }
1527
1528         command = data_from_user->command;
1529
1530         if (command != UPSTREAM_START_VIDEO && command != UPSTREAM_STOP_VIDEO)
1531                 return 0;
1532
1533         dev->input_filename_ch2 = data_from_user->input_filename;
1534         dev->input_audiofilename = data_from_user->input_filename;
1535         dev->vid_stdname_ch2 = data_from_user->vid_stdname;
1536         dev->pixel_format_ch2 = data_from_user->pixel_format;
1537         dev->channel_select_ch2 = data_from_user->channel_select;
1538         dev->command_ch2 = data_from_user->command;
1539
1540         switch (command) {
1541         case UPSTREAM_START_VIDEO:
1542                 cx25821_start_upstream_video_ch2(dev, data_from_user);
1543                 break;
1544
1545         case UPSTREAM_STOP_VIDEO:
1546                 cx25821_stop_upstream_video_ch2(dev);
1547                 break;
1548         }
1549
1550         return 0;
1551 }
1552
1553 static long video_ioctl_upstream11(struct file *file, unsigned int cmd,
1554                                   unsigned long arg)
1555 {
1556         struct cx25821_fh *fh = file->private_data;
1557         struct cx25821_dev *dev = fh->dev;
1558         int command = 0;
1559         struct upstream_user_struct *data_from_user;
1560
1561         data_from_user = (struct upstream_user_struct *)arg;
1562
1563         if (!data_from_user) {
1564                 pr_err("%s(): Upstream data is INVALID. Returning\n", __func__);
1565                 return 0;
1566         }
1567
1568         command = data_from_user->command;
1569
1570         if (command != UPSTREAM_START_AUDIO && command != UPSTREAM_STOP_AUDIO)
1571                 return 0;
1572
1573         dev->input_filename = data_from_user->input_filename;
1574         dev->input_audiofilename = data_from_user->input_filename;
1575         dev->vid_stdname = data_from_user->vid_stdname;
1576         dev->pixel_format = data_from_user->pixel_format;
1577         dev->channel_select = data_from_user->channel_select;
1578         dev->command = data_from_user->command;
1579
1580         switch (command) {
1581         case UPSTREAM_START_AUDIO:
1582                 cx25821_start_upstream_audio(dev, data_from_user);
1583                 break;
1584
1585         case UPSTREAM_STOP_AUDIO:
1586                 cx25821_stop_upstream_audio(dev);
1587                 break;
1588         }
1589
1590         return 0;
1591 }
1592
1593 static long video_ioctl_set(struct file *file, unsigned int cmd,
1594                            unsigned long arg)
1595 {
1596         struct cx25821_fh *fh = file->private_data;
1597         struct cx25821_dev *dev = fh->dev;
1598         struct downstream_user_struct *data_from_user;
1599         int command;
1600         int width = 720;
1601         int selected_channel = 0;
1602         int pix_format = 0;
1603         int i = 0;
1604         int cif_enable = 0;
1605         int cif_width = 0;
1606
1607         data_from_user = (struct downstream_user_struct *)arg;
1608
1609         if (!data_from_user) {
1610                 pr_err("%s(): User data is INVALID. Returning\n", __func__);
1611                 return 0;
1612         }
1613
1614         command = data_from_user->command;
1615
1616         if (command != SET_VIDEO_STD && command != SET_PIXEL_FORMAT
1617            && command != ENABLE_CIF_RESOLUTION && command != REG_READ
1618            && command != REG_WRITE && command != MEDUSA_READ
1619            && command != MEDUSA_WRITE) {
1620                 return 0;
1621         }
1622
1623         switch (command) {
1624         case SET_VIDEO_STD:
1625                 if (!strcmp(data_from_user->vid_stdname, "PAL"))
1626                         dev->tvnorm = V4L2_STD_PAL_BG;
1627                 else
1628                         dev->tvnorm = V4L2_STD_NTSC_M;
1629                 medusa_set_videostandard(dev);
1630                 break;
1631
1632         case SET_PIXEL_FORMAT:
1633                 selected_channel = data_from_user->decoder_select;
1634                 pix_format = data_from_user->pixel_format;
1635
1636                 if (!(selected_channel <= 7 && selected_channel >= 0)) {
1637                         selected_channel -= 4;
1638                         selected_channel = selected_channel % 8;
1639                 }
1640
1641                 if (selected_channel >= 0)
1642                         cx25821_set_pixel_format(dev, selected_channel,
1643                                                 pix_format);
1644
1645                 break;
1646
1647         case ENABLE_CIF_RESOLUTION:
1648                 selected_channel = data_from_user->decoder_select;
1649                 cif_enable = data_from_user->cif_resolution_enable;
1650                 cif_width = data_from_user->cif_width;
1651
1652                 if (cif_enable) {
1653                         if (dev->tvnorm & V4L2_STD_PAL_BG
1654                             || dev->tvnorm & V4L2_STD_PAL_DK) {
1655                                 width = 352;
1656                         } else {
1657                                 width = cif_width;
1658                                 if (cif_width != 320 && cif_width != 352)
1659                                         width = 320;
1660                         }
1661                 }
1662
1663                 if (!(selected_channel <= 7 && selected_channel >= 0)) {
1664                         selected_channel -= 4;
1665                         selected_channel = selected_channel % 8;
1666                 }
1667
1668                 if (selected_channel <= 7 && selected_channel >= 0) {
1669                         dev->channels[selected_channel].use_cif_resolution =
1670                                 cif_enable;
1671                         dev->channels[selected_channel].cif_width = width;
1672                 } else {
1673                         for (i = 0; i < VID_CHANNEL_NUM; i++) {
1674                                 dev->channels[i].use_cif_resolution =
1675                                         cif_enable;
1676                                 dev->channels[i].cif_width = width;
1677                         }
1678                 }
1679
1680                 medusa_set_resolution(dev, width, selected_channel);
1681                 break;
1682         case REG_READ:
1683                 data_from_user->reg_data = cx_read(data_from_user->reg_address);
1684                 break;
1685         case REG_WRITE:
1686                 cx_write(data_from_user->reg_address, data_from_user->reg_data);
1687                 break;
1688         case MEDUSA_READ:
1689                 cx25821_i2c_read(&dev->i2c_bus[0],
1690                                          (u16) data_from_user->reg_address,
1691                                          &data_from_user->reg_data);
1692                 break;
1693         case MEDUSA_WRITE:
1694                 cx25821_i2c_write(&dev->i2c_bus[0],
1695                                   (u16) data_from_user->reg_address,
1696                                   data_from_user->reg_data);
1697                 break;
1698         }
1699
1700         return 0;
1701 }
1702
1703 static long cx25821_video_ioctl(struct file *file,
1704                                 unsigned int cmd, unsigned long arg)
1705 {
1706         int ret = 0;
1707
1708         struct cx25821_fh *fh = file->private_data;
1709
1710         /* check to see if it's the video upstream */
1711         if (fh->channel_id == SRAM_CH09) {
1712                 ret = video_ioctl_upstream9(file, cmd, arg);
1713                 return ret;
1714         } else if (fh->channel_id == SRAM_CH10) {
1715                 ret = video_ioctl_upstream10(file, cmd, arg);
1716                 return ret;
1717         } else if (fh->channel_id == SRAM_CH11) {
1718                 ret = video_ioctl_upstream11(file, cmd, arg);
1719                 ret = video_ioctl_set(file, cmd, arg);
1720                 return ret;
1721         }
1722
1723         return video_ioctl2(file, cmd, arg);
1724 }
1725
1726 /* exported stuff */
1727 static const struct v4l2_file_operations video_fops = {
1728         .owner = THIS_MODULE,
1729         .open = video_open,
1730         .release = video_release,
1731         .read = video_read,
1732         .poll = video_poll,
1733         .mmap = cx25821_video_mmap,
1734         .ioctl = cx25821_video_ioctl,
1735 };
1736
1737 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1738         .vidioc_querycap = cx25821_vidioc_querycap,
1739         .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap,
1740         .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap,
1741         .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap,
1742         .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1743         .vidioc_reqbufs = cx25821_vidioc_reqbufs,
1744         .vidioc_querybuf = cx25821_vidioc_querybuf,
1745         .vidioc_qbuf = cx25821_vidioc_qbuf,
1746         .vidioc_dqbuf = vidioc_dqbuf,
1747         .vidioc_g_std = cx25821_vidioc_g_std,
1748         .vidioc_s_std = cx25821_vidioc_s_std,
1749         .vidioc_cropcap = cx25821_vidioc_cropcap,
1750         .vidioc_s_crop = cx25821_vidioc_s_crop,
1751         .vidioc_g_crop = cx25821_vidioc_g_crop,
1752         .vidioc_enum_input = cx25821_vidioc_enum_input,
1753         .vidioc_g_input = cx25821_vidioc_g_input,
1754         .vidioc_s_input = cx25821_vidioc_s_input,
1755         .vidioc_g_ctrl = cx25821_vidioc_g_ctrl,
1756         .vidioc_s_ctrl = vidioc_s_ctrl,
1757         .vidioc_queryctrl = cx25821_vidioc_queryctrl,
1758         .vidioc_streamon = vidioc_streamon,
1759         .vidioc_streamoff = vidioc_streamoff,
1760         .vidioc_log_status = vidioc_log_status,
1761         .vidioc_g_priority = cx25821_vidioc_g_priority,
1762         .vidioc_s_priority = cx25821_vidioc_s_priority,
1763 #ifdef CONFIG_VIDEO_ADV_DEBUG
1764         .vidioc_g_register = cx25821_vidioc_g_register,
1765         .vidioc_s_register = cx25821_vidioc_s_register,
1766 #endif
1767 };
1768
1769 static const struct video_device cx25821_video_device = {
1770         .name = "cx25821-video",
1771         .fops = &video_fops,
1772         .minor = -1,
1773         .ioctl_ops = &video_ioctl_ops,
1774         .tvnorms = CX25821_NORMS,
1775 };
1776
1777 void cx25821_video_unregister(struct cx25821_dev *dev, int chan_num)
1778 {
1779         cx_clear(PCI_INT_MSK, 1);
1780
1781         if (dev->channels[chan_num].video_dev) {
1782                 if (video_is_registered(dev->channels[chan_num].video_dev))
1783                         video_unregister_device(
1784                                         dev->channels[chan_num].video_dev);
1785                 else
1786                         video_device_release(
1787                                         dev->channels[chan_num].video_dev);
1788
1789                 dev->channels[chan_num].video_dev = NULL;
1790
1791                 btcx_riscmem_free(dev->pci,
1792                                 &dev->channels[chan_num].vidq.stopper);
1793
1794                 pr_warn("device %d released!\n", chan_num);
1795         }
1796
1797 }
1798
1799 int cx25821_video_register(struct cx25821_dev *dev)
1800 {
1801         int err;
1802         int i;
1803
1804         spin_lock_init(&dev->slock);
1805
1806         for (i = 0; i < VID_CHANNEL_NUM; ++i) {
1807                 if (i == SRAM_CH08) /* audio channel */
1808                         continue;
1809
1810                 cx25821_init_controls(dev, i);
1811
1812                 cx25821_risc_stopper(dev->pci, &dev->channels[i].vidq.stopper,
1813                         dev->channels[i].sram_channels->dma_ctl, 0x11, 0);
1814
1815                 dev->channels[i].sram_channels = &cx25821_sram_channels[i];
1816                 dev->channels[i].video_dev = NULL;
1817                 dev->channels[i].resources = 0;
1818
1819                 cx_write(dev->channels[i].sram_channels->int_stat, 0xffffffff);
1820
1821                 INIT_LIST_HEAD(&dev->channels[i].vidq.active);
1822                 INIT_LIST_HEAD(&dev->channels[i].vidq.queued);
1823
1824                 dev->channels[i].timeout_data.dev = dev;
1825                 dev->channels[i].timeout_data.channel =
1826                         &cx25821_sram_channels[i];
1827                 dev->channels[i].vidq.timeout.function = cx25821_vid_timeout;
1828                 dev->channels[i].vidq.timeout.data =
1829                         (unsigned long)&dev->channels[i].timeout_data;
1830                 init_timer(&dev->channels[i].vidq.timeout);
1831
1832                 /* register v4l devices */
1833                 dev->channels[i].video_dev = cx25821_vdev_init(dev, dev->pci,
1834                                 &cx25821_video_device, "video");
1835
1836                 err = video_register_device(dev->channels[i].video_dev,
1837                                 VFL_TYPE_GRABBER, video_nr[dev->nr]);
1838
1839                 if (err < 0)
1840                         goto fail_unreg;
1841
1842         }
1843
1844         /* set PCI interrupt */
1845         cx_set(PCI_INT_MSK, 0xff);
1846
1847         /* initial device configuration */
1848         mutex_lock(&dev->lock);
1849         dev->tvnorm = V4L2_STD_NTSC_M,
1850         cx25821_set_tvnorm(dev, dev->tvnorm);
1851         mutex_unlock(&dev->lock);
1852
1853         return 0;
1854
1855 fail_unreg:
1856         cx25821_video_unregister(dev, i);
1857         return err;
1858 }