]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/cx23885/cx23885-video.c
V4L/DVB (10907): avoid loading the entire videodev.h header on V4L2 drivers
[linux-2.6-omap-h63xx.git] / drivers / media / video / cx23885 / cx23885-video.c
1 /*
2  *  Driver for the Conexant CX23885 PCIe bridge
3  *
4  *  Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/kmod.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <linux/delay.h>
31 #include <linux/kthread.h>
32 #include <asm/div64.h>
33
34 #include "cx23885.h"
35 #include <media/v4l2-common.h>
36 #include <media/v4l2-ioctl.h>
37
38 MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
39 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
40 MODULE_LICENSE("GPL");
41
42 /* ------------------------------------------------------------------ */
43
44 static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
45 static unsigned int vbi_nr[]   = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
46 static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
47
48 module_param_array(video_nr, int, NULL, 0444);
49 module_param_array(vbi_nr,   int, NULL, 0444);
50 module_param_array(radio_nr, int, NULL, 0444);
51
52 MODULE_PARM_DESC(video_nr, "video device numbers");
53 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
54 MODULE_PARM_DESC(radio_nr, "radio device numbers");
55
56 static unsigned int video_debug;
57 module_param(video_debug, int, 0644);
58 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
59
60 static unsigned int irq_debug;
61 module_param(irq_debug, int, 0644);
62 MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
63
64 static unsigned int vid_limit = 16;
65 module_param(vid_limit, int, 0644);
66 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
67
68 #define dprintk(level, fmt, arg...)\
69         do { if (video_debug >= level)\
70                 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
71         } while (0)
72
73 /* ------------------------------------------------------------------- */
74 /* static data                                                         */
75
76 #define FORMAT_FLAGS_PACKED       0x01
77
78 static struct cx23885_fmt formats[] = {
79         {
80                 .name     = "8 bpp, gray",
81                 .fourcc   = V4L2_PIX_FMT_GREY,
82                 .depth    = 8,
83                 .flags    = FORMAT_FLAGS_PACKED,
84         }, {
85                 .name     = "15 bpp RGB, le",
86                 .fourcc   = V4L2_PIX_FMT_RGB555,
87                 .depth    = 16,
88                 .flags    = FORMAT_FLAGS_PACKED,
89         }, {
90                 .name     = "15 bpp RGB, be",
91                 .fourcc   = V4L2_PIX_FMT_RGB555X,
92                 .depth    = 16,
93                 .flags    = FORMAT_FLAGS_PACKED,
94         }, {
95                 .name     = "16 bpp RGB, le",
96                 .fourcc   = V4L2_PIX_FMT_RGB565,
97                 .depth    = 16,
98                 .flags    = FORMAT_FLAGS_PACKED,
99         }, {
100                 .name     = "16 bpp RGB, be",
101                 .fourcc   = V4L2_PIX_FMT_RGB565X,
102                 .depth    = 16,
103                 .flags    = FORMAT_FLAGS_PACKED,
104         }, {
105                 .name     = "24 bpp RGB, le",
106                 .fourcc   = V4L2_PIX_FMT_BGR24,
107                 .depth    = 24,
108                 .flags    = FORMAT_FLAGS_PACKED,
109         }, {
110                 .name     = "32 bpp RGB, le",
111                 .fourcc   = V4L2_PIX_FMT_BGR32,
112                 .depth    = 32,
113                 .flags    = FORMAT_FLAGS_PACKED,
114         }, {
115                 .name     = "32 bpp RGB, be",
116                 .fourcc   = V4L2_PIX_FMT_RGB32,
117                 .depth    = 32,
118                 .flags    = FORMAT_FLAGS_PACKED,
119         }, {
120                 .name     = "4:2:2, packed, YUYV",
121                 .fourcc   = V4L2_PIX_FMT_YUYV,
122                 .depth    = 16,
123                 .flags    = FORMAT_FLAGS_PACKED,
124         }, {
125                 .name     = "4:2:2, packed, UYVY",
126                 .fourcc   = V4L2_PIX_FMT_UYVY,
127                 .depth    = 16,
128                 .flags    = FORMAT_FLAGS_PACKED,
129         },
130 };
131
132 static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
133 {
134         unsigned int i;
135
136         for (i = 0; i < ARRAY_SIZE(formats); i++)
137                 if (formats[i].fourcc == fourcc)
138                         return formats+i;
139
140         printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __func__, fourcc);
141         return NULL;
142 }
143
144 /* ------------------------------------------------------------------- */
145
146 static const struct v4l2_queryctrl no_ctl = {
147         .name  = "42",
148         .flags = V4L2_CTRL_FLAG_DISABLED,
149 };
150
151 static struct cx23885_ctrl cx23885_ctls[] = {
152         /* --- video --- */
153         {
154                 .v = {
155                         .id            = V4L2_CID_BRIGHTNESS,
156                         .name          = "Brightness",
157                         .minimum       = 0x00,
158                         .maximum       = 0xff,
159                         .step          = 1,
160                         .default_value = 0x7f,
161                         .type          = V4L2_CTRL_TYPE_INTEGER,
162                 },
163                 .off                   = 128,
164                 .reg                   = LUMA_CTRL,
165                 .mask                  = 0x00ff,
166                 .shift                 = 0,
167         }, {
168                 .v = {
169                         .id            = V4L2_CID_CONTRAST,
170                         .name          = "Contrast",
171                         .minimum       = 0,
172                         .maximum       = 0xff,
173                         .step          = 1,
174                         .default_value = 0x3f,
175                         .type          = V4L2_CTRL_TYPE_INTEGER,
176                 },
177                 .off                   = 0,
178                 .reg                   = LUMA_CTRL,
179                 .mask                  = 0xff00,
180                 .shift                 = 8,
181         }, {
182                 .v = {
183                         .id            = V4L2_CID_HUE,
184                         .name          = "Hue",
185                         .minimum       = 0,
186                         .maximum       = 0xff,
187                         .step          = 1,
188                         .default_value = 0x7f,
189                         .type          = V4L2_CTRL_TYPE_INTEGER,
190                 },
191                 .off                   = 128,
192                 .reg                   = CHROMA_CTRL,
193                 .mask                  = 0xff0000,
194                 .shift                 = 16,
195         }, {
196                 /* strictly, this only describes only U saturation.
197                  * V saturation is handled specially through code.
198                  */
199                 .v = {
200                         .id            = V4L2_CID_SATURATION,
201                         .name          = "Saturation",
202                         .minimum       = 0,
203                         .maximum       = 0xff,
204                         .step          = 1,
205                         .default_value = 0x7f,
206                         .type          = V4L2_CTRL_TYPE_INTEGER,
207                 },
208                 .off                   = 0,
209                 .reg                   = CHROMA_CTRL,
210                 .mask                  = 0x00ff,
211                 .shift                 = 0,
212         }, {
213         /* --- audio --- */
214                 .v = {
215                         .id            = V4L2_CID_AUDIO_MUTE,
216                         .name          = "Mute",
217                         .minimum       = 0,
218                         .maximum       = 1,
219                         .default_value = 1,
220                         .type          = V4L2_CTRL_TYPE_BOOLEAN,
221                 },
222                 .reg                   = PATH1_CTL1,
223                 .mask                  = (0x1f << 24),
224                 .shift                 = 24,
225         }, {
226                 .v = {
227                         .id            = V4L2_CID_AUDIO_VOLUME,
228                         .name          = "Volume",
229                         .minimum       = 0,
230                         .maximum       = 0x3f,
231                         .step          = 1,
232                         .default_value = 0x3f,
233                         .type          = V4L2_CTRL_TYPE_INTEGER,
234                 },
235                 .reg                   = PATH1_VOL_CTL,
236                 .mask                  = 0xff,
237                 .shift                 = 0,
238         }
239 };
240 static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
241
242 /* Must be sorted from low to high control ID! */
243 static const u32 cx23885_user_ctrls[] = {
244         V4L2_CID_USER_CLASS,
245         V4L2_CID_BRIGHTNESS,
246         V4L2_CID_CONTRAST,
247         V4L2_CID_SATURATION,
248         V4L2_CID_HUE,
249         V4L2_CID_AUDIO_VOLUME,
250         V4L2_CID_AUDIO_MUTE,
251         0
252 };
253
254 static const u32 *ctrl_classes[] = {
255         cx23885_user_ctrls,
256         NULL
257 };
258
259 static void cx23885_video_wakeup(struct cx23885_dev *dev,
260                  struct cx23885_dmaqueue *q, u32 count)
261 {
262         struct cx23885_buffer *buf;
263         int bc;
264
265         for (bc = 0;; bc++) {
266                 if (list_empty(&q->active))
267                         break;
268                 buf = list_entry(q->active.next,
269                                  struct cx23885_buffer, vb.queue);
270
271                 /* count comes from the hw and is is 16bit wide --
272                  * this trick handles wrap-arounds correctly for
273                  * up to 32767 buffers in flight... */
274                 if ((s16) (count - buf->count) < 0)
275                         break;
276
277                 do_gettimeofday(&buf->vb.ts);
278                 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
279                         count, buf->count);
280                 buf->vb.state = VIDEOBUF_DONE;
281                 list_del(&buf->vb.queue);
282                 wake_up(&buf->vb.done);
283         }
284         if (list_empty(&q->active))
285                 del_timer(&q->timeout);
286         else
287                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
288         if (bc != 1)
289                 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
290                         __func__, bc);
291 }
292
293 static int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
294 {
295         dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
296                 __func__,
297                 (unsigned int)norm,
298                 v4l2_norm_to_name(norm));
299
300         dev->tvnorm = norm;
301
302         /* Tell the analog tuner/demods */
303         cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_STD, &norm);
304
305         /* Tell the internal A/V decoder */
306         cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_STD, &norm);
307
308         return 0;
309 }
310
311 static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
312                                     struct pci_dev *pci,
313                                     struct video_device *template,
314                                     char *type)
315 {
316         struct video_device *vfd;
317         dprintk(1, "%s()\n", __func__);
318
319         vfd = video_device_alloc();
320         if (NULL == vfd)
321                 return NULL;
322         *vfd = *template;
323         vfd->minor   = -1;
324         vfd->parent  = &pci->dev;
325         vfd->release = video_device_release;
326         snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
327                  dev->name, type, cx23885_boards[dev->board].name);
328         return vfd;
329 }
330
331 static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
332 {
333         int i;
334
335         if (qctrl->id < V4L2_CID_BASE ||
336             qctrl->id >= V4L2_CID_LASTP1)
337                 return -EINVAL;
338         for (i = 0; i < CX23885_CTLS; i++)
339                 if (cx23885_ctls[i].v.id == qctrl->id)
340                         break;
341         if (i == CX23885_CTLS) {
342                 *qctrl = no_ctl;
343                 return 0;
344         }
345         *qctrl = cx23885_ctls[i].v;
346         return 0;
347 }
348
349 /* ------------------------------------------------------------------- */
350 /* resource management                                                 */
351
352 static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
353         unsigned int bit)
354 {
355         dprintk(1, "%s()\n", __func__);
356         if (fh->resources & bit)
357                 /* have it already allocated */
358                 return 1;
359
360         /* is it free? */
361         mutex_lock(&dev->lock);
362         if (dev->resources & bit) {
363                 /* no, someone else uses it */
364                 mutex_unlock(&dev->lock);
365                 return 0;
366         }
367         /* it's free, grab it */
368         fh->resources  |= bit;
369         dev->resources |= bit;
370         dprintk(1, "res: get %d\n", bit);
371         mutex_unlock(&dev->lock);
372         return 1;
373 }
374
375 static int res_check(struct cx23885_fh *fh, unsigned int bit)
376 {
377         return fh->resources & bit;
378 }
379
380 static int res_locked(struct cx23885_dev *dev, unsigned int bit)
381 {
382         return dev->resources & bit;
383 }
384
385 static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
386         unsigned int bits)
387 {
388         BUG_ON((fh->resources & bits) != bits);
389         dprintk(1, "%s()\n", __func__);
390
391         mutex_lock(&dev->lock);
392         fh->resources  &= ~bits;
393         dev->resources &= ~bits;
394         dprintk(1, "res: put %d\n", bits);
395         mutex_unlock(&dev->lock);
396 }
397
398 static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
399 {
400         struct v4l2_routing route;
401         memset(&route, 0, sizeof(route));
402
403         dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
404                 __func__,
405                 input, INPUT(input)->vmux,
406                 INPUT(input)->gpio0, INPUT(input)->gpio1,
407                 INPUT(input)->gpio2, INPUT(input)->gpio3);
408         dev->input = input;
409
410         route.input = INPUT(input)->vmux;
411
412         /* Tell the internal A/V decoder */
413         cx23885_call_i2c_clients(&dev->i2c_bus[2],
414                 VIDIOC_INT_S_VIDEO_ROUTING, &route);
415
416         return 0;
417 }
418
419 /* ------------------------------------------------------------------ */
420 static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
421         unsigned int height, enum v4l2_field field)
422 {
423         dprintk(1, "%s()\n", __func__);
424         return 0;
425 }
426
427 static int cx23885_start_video_dma(struct cx23885_dev *dev,
428                            struct cx23885_dmaqueue *q,
429                            struct cx23885_buffer *buf)
430 {
431         dprintk(1, "%s()\n", __func__);
432
433         /* setup fifo + format */
434         cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
435                                 buf->bpl, buf->risc.dma);
436         cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
437
438         /* reset counter */
439         cx_write(VID_A_GPCNT_CTL, 3);
440         q->count = 1;
441
442         /* enable irq */
443         cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
444         cx_set(VID_A_INT_MSK, 0x000011);
445
446         /* start dma */
447         cx_set(DEV_CNTRL2, (1<<5));
448         cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
449
450         return 0;
451 }
452
453
454 static int cx23885_restart_video_queue(struct cx23885_dev *dev,
455                                struct cx23885_dmaqueue *q)
456 {
457         struct cx23885_buffer *buf, *prev;
458         struct list_head *item;
459         dprintk(1, "%s()\n", __func__);
460
461         if (!list_empty(&q->active)) {
462                 buf = list_entry(q->active.next, struct cx23885_buffer,
463                         vb.queue);
464                 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
465                         buf, buf->vb.i);
466                 cx23885_start_video_dma(dev, q, buf);
467                 list_for_each(item, &q->active) {
468                         buf = list_entry(item, struct cx23885_buffer,
469                                 vb.queue);
470                         buf->count    = q->count++;
471                 }
472                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
473                 return 0;
474         }
475
476         prev = NULL;
477         for (;;) {
478                 if (list_empty(&q->queued))
479                         return 0;
480                 buf = list_entry(q->queued.next, struct cx23885_buffer,
481                         vb.queue);
482                 if (NULL == prev) {
483                         list_move_tail(&buf->vb.queue, &q->active);
484                         cx23885_start_video_dma(dev, q, buf);
485                         buf->vb.state = VIDEOBUF_ACTIVE;
486                         buf->count    = q->count++;
487                         mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
488                         dprintk(2, "[%p/%d] restart_queue - first active\n",
489                                 buf, buf->vb.i);
490
491                 } else if (prev->vb.width  == buf->vb.width  &&
492                            prev->vb.height == buf->vb.height &&
493                            prev->fmt       == buf->fmt) {
494                         list_move_tail(&buf->vb.queue, &q->active);
495                         buf->vb.state = VIDEOBUF_ACTIVE;
496                         buf->count    = q->count++;
497                         prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
498                         prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
499                         dprintk(2, "[%p/%d] restart_queue - move to active\n",
500                                 buf, buf->vb.i);
501                 } else {
502                         return 0;
503                 }
504                 prev = buf;
505         }
506 }
507
508 static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
509         unsigned int *size)
510 {
511         struct cx23885_fh *fh = q->priv_data;
512
513         *size = fh->fmt->depth*fh->width*fh->height >> 3;
514         if (0 == *count)
515                 *count = 32;
516         while (*size * *count > vid_limit * 1024 * 1024)
517                 (*count)--;
518         return 0;
519 }
520
521 static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
522                enum v4l2_field field)
523 {
524         struct cx23885_fh *fh  = q->priv_data;
525         struct cx23885_dev *dev = fh->dev;
526         struct cx23885_buffer *buf =
527                 container_of(vb, struct cx23885_buffer, vb);
528         int rc, init_buffer = 0;
529         u32 line0_offset, line1_offset;
530         struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
531
532         BUG_ON(NULL == fh->fmt);
533         if (fh->width  < 48 || fh->width  > norm_maxw(dev->tvnorm) ||
534             fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
535                 return -EINVAL;
536         buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
537         if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
538                 return -EINVAL;
539
540         if (buf->fmt       != fh->fmt    ||
541             buf->vb.width  != fh->width  ||
542             buf->vb.height != fh->height ||
543             buf->vb.field  != field) {
544                 buf->fmt       = fh->fmt;
545                 buf->vb.width  = fh->width;
546                 buf->vb.height = fh->height;
547                 buf->vb.field  = field;
548                 init_buffer = 1;
549         }
550
551         if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
552                 init_buffer = 1;
553                 rc = videobuf_iolock(q, &buf->vb, NULL);
554                 if (0 != rc)
555                         goto fail;
556         }
557
558         if (init_buffer) {
559                 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
560                 switch (buf->vb.field) {
561                 case V4L2_FIELD_TOP:
562                         cx23885_risc_buffer(dev->pci, &buf->risc,
563                                          dma->sglist, 0, UNSET,
564                                          buf->bpl, 0, buf->vb.height);
565                         break;
566                 case V4L2_FIELD_BOTTOM:
567                         cx23885_risc_buffer(dev->pci, &buf->risc,
568                                          dma->sglist, UNSET, 0,
569                                          buf->bpl, 0, buf->vb.height);
570                         break;
571                 case V4L2_FIELD_INTERLACED:
572                         if (dev->tvnorm & V4L2_STD_NTSC) {
573                                 /* cx25840 transmits NTSC bottom field first */
574                                 dprintk(1, "%s() Creating NTSC risc\n",
575                                         __func__);
576                                 line0_offset = buf->bpl;
577                                 line1_offset = 0;
578                         } else {
579                                 /* All other formats are top field first */
580                                 dprintk(1, "%s() Creating PAL/SECAM risc\n",
581                                         __func__);
582                                 line0_offset = 0;
583                                 line1_offset = buf->bpl;
584                         }
585                         cx23885_risc_buffer(dev->pci, &buf->risc,
586                                         dma->sglist, line0_offset,
587                                         line1_offset,
588                                         buf->bpl, buf->bpl,
589                                         buf->vb.height >> 1);
590                         break;
591                 case V4L2_FIELD_SEQ_TB:
592                         cx23885_risc_buffer(dev->pci, &buf->risc,
593                                          dma->sglist,
594                                          0, buf->bpl * (buf->vb.height >> 1),
595                                          buf->bpl, 0,
596                                          buf->vb.height >> 1);
597                         break;
598                 case V4L2_FIELD_SEQ_BT:
599                         cx23885_risc_buffer(dev->pci, &buf->risc,
600                                          dma->sglist,
601                                          buf->bpl * (buf->vb.height >> 1), 0,
602                                          buf->bpl, 0,
603                                          buf->vb.height >> 1);
604                         break;
605                 default:
606                         BUG();
607                 }
608         }
609         dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
610                 buf, buf->vb.i,
611                 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
612                 (unsigned long)buf->risc.dma);
613
614         buf->vb.state = VIDEOBUF_PREPARED;
615         return 0;
616
617  fail:
618         cx23885_free_buffer(q, buf);
619         return rc;
620 }
621
622 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
623 {
624         struct cx23885_buffer   *buf = container_of(vb,
625                 struct cx23885_buffer, vb);
626         struct cx23885_buffer   *prev;
627         struct cx23885_fh       *fh   = vq->priv_data;
628         struct cx23885_dev      *dev  = fh->dev;
629         struct cx23885_dmaqueue *q    = &dev->vidq;
630
631         /* add jump to stopper */
632         buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
633         buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
634         buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
635
636         if (!list_empty(&q->queued)) {
637                 list_add_tail(&buf->vb.queue, &q->queued);
638                 buf->vb.state = VIDEOBUF_QUEUED;
639                 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
640                         buf, buf->vb.i);
641
642         } else if (list_empty(&q->active)) {
643                 list_add_tail(&buf->vb.queue, &q->active);
644                 cx23885_start_video_dma(dev, q, buf);
645                 buf->vb.state = VIDEOBUF_ACTIVE;
646                 buf->count    = q->count++;
647                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
648                 dprintk(2, "[%p/%d] buffer_queue - first active\n",
649                         buf, buf->vb.i);
650
651         } else {
652                 prev = list_entry(q->active.prev, struct cx23885_buffer,
653                         vb.queue);
654                 if (prev->vb.width  == buf->vb.width  &&
655                     prev->vb.height == buf->vb.height &&
656                     prev->fmt       == buf->fmt) {
657                         list_add_tail(&buf->vb.queue, &q->active);
658                         buf->vb.state = VIDEOBUF_ACTIVE;
659                         buf->count    = q->count++;
660                         prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
661                         /* 64 bit bits 63-32 */
662                         prev->risc.jmp[2] = cpu_to_le32(0);
663                         dprintk(2, "[%p/%d] buffer_queue - append to active\n",
664                                 buf, buf->vb.i);
665
666                 } else {
667                         list_add_tail(&buf->vb.queue, &q->queued);
668                         buf->vb.state = VIDEOBUF_QUEUED;
669                         dprintk(2, "[%p/%d] buffer_queue - first queued\n",
670                                 buf, buf->vb.i);
671                 }
672         }
673 }
674
675 static void buffer_release(struct videobuf_queue *q,
676         struct videobuf_buffer *vb)
677 {
678         struct cx23885_buffer *buf = container_of(vb,
679                 struct cx23885_buffer, vb);
680
681         cx23885_free_buffer(q, buf);
682 }
683
684 static struct videobuf_queue_ops cx23885_video_qops = {
685         .buf_setup    = buffer_setup,
686         .buf_prepare  = buffer_prepare,
687         .buf_queue    = buffer_queue,
688         .buf_release  = buffer_release,
689 };
690
691 static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
692 {
693         switch (fh->type) {
694         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
695                 return &fh->vidq;
696         case V4L2_BUF_TYPE_VBI_CAPTURE:
697                 return &fh->vbiq;
698         default:
699                 BUG();
700                 return NULL;
701         }
702 }
703
704 static int get_resource(struct cx23885_fh *fh)
705 {
706         switch (fh->type) {
707         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
708                 return RESOURCE_VIDEO;
709         case V4L2_BUF_TYPE_VBI_CAPTURE:
710                 return RESOURCE_VBI;
711         default:
712                 BUG();
713                 return 0;
714         }
715 }
716
717 static int video_open(struct file *file)
718 {
719         int minor = video_devdata(file)->minor;
720         struct cx23885_dev *h, *dev = NULL;
721         struct cx23885_fh *fh;
722         struct list_head *list;
723         enum v4l2_buf_type type = 0;
724         int radio = 0;
725
726         lock_kernel();
727         list_for_each(list, &cx23885_devlist) {
728                 h = list_entry(list, struct cx23885_dev, devlist);
729                 if (h->video_dev &&
730                     h->video_dev->minor == minor) {
731                         dev  = h;
732                         type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
733                 }
734                 if (h->vbi_dev &&
735                     h->vbi_dev->minor == minor) {
736                         dev  = h;
737                         type = V4L2_BUF_TYPE_VBI_CAPTURE;
738                 }
739                 if (h->radio_dev &&
740                     h->radio_dev->minor == minor) {
741                         radio = 1;
742                         dev   = h;
743                 }
744         }
745         if (NULL == dev) {
746                 unlock_kernel();
747                 return -ENODEV;
748         }
749
750         dprintk(1, "open minor=%d radio=%d type=%s\n",
751                 minor, radio, v4l2_type_names[type]);
752
753         /* allocate + initialize per filehandle data */
754         fh = kzalloc(sizeof(*fh), GFP_KERNEL);
755         if (NULL == fh) {
756                 unlock_kernel();
757                 return -ENOMEM;
758         }
759         file->private_data = fh;
760         fh->dev      = dev;
761         fh->radio    = radio;
762         fh->type     = type;
763         fh->width    = 320;
764         fh->height   = 240;
765         fh->fmt      = format_by_fourcc(V4L2_PIX_FMT_BGR24);
766
767         videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
768                             &dev->pci->dev, &dev->slock,
769                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
770                             V4L2_FIELD_INTERLACED,
771                             sizeof(struct cx23885_buffer),
772                             fh);
773
774         dprintk(1, "post videobuf_queue_init()\n");
775
776         unlock_kernel();
777
778         return 0;
779 }
780
781 static ssize_t video_read(struct file *file, char __user *data,
782         size_t count, loff_t *ppos)
783 {
784         struct cx23885_fh *fh = file->private_data;
785
786         switch (fh->type) {
787         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
788                 if (res_locked(fh->dev, RESOURCE_VIDEO))
789                         return -EBUSY;
790                 return videobuf_read_one(&fh->vidq, data, count, ppos,
791                                          file->f_flags & O_NONBLOCK);
792         case V4L2_BUF_TYPE_VBI_CAPTURE:
793                 if (!res_get(fh->dev, fh, RESOURCE_VBI))
794                         return -EBUSY;
795                 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
796                                             file->f_flags & O_NONBLOCK);
797         default:
798                 BUG();
799                 return 0;
800         }
801 }
802
803 static unsigned int video_poll(struct file *file,
804         struct poll_table_struct *wait)
805 {
806         struct cx23885_fh *fh = file->private_data;
807         struct cx23885_buffer *buf;
808
809         if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
810                 if (!res_get(fh->dev, fh, RESOURCE_VBI))
811                         return POLLERR;
812                 return videobuf_poll_stream(file, &fh->vbiq, wait);
813         }
814
815         if (res_check(fh, RESOURCE_VIDEO)) {
816                 /* streaming capture */
817                 if (list_empty(&fh->vidq.stream))
818                         return POLLERR;
819                 buf = list_entry(fh->vidq.stream.next,
820                         struct cx23885_buffer, vb.stream);
821         } else {
822                 /* read() capture */
823                 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
824                 if (NULL == buf)
825                         return POLLERR;
826         }
827         poll_wait(file, &buf->vb.done, wait);
828         if (buf->vb.state == VIDEOBUF_DONE ||
829             buf->vb.state == VIDEOBUF_ERROR)
830                 return POLLIN|POLLRDNORM;
831         return 0;
832 }
833
834 static int video_release(struct file *file)
835 {
836         struct cx23885_fh *fh = file->private_data;
837         struct cx23885_dev *dev = fh->dev;
838
839         /* turn off overlay */
840         if (res_check(fh, RESOURCE_OVERLAY)) {
841                 /* FIXME */
842                 res_free(dev, fh, RESOURCE_OVERLAY);
843         }
844
845         /* stop video capture */
846         if (res_check(fh, RESOURCE_VIDEO)) {
847                 videobuf_queue_cancel(&fh->vidq);
848                 res_free(dev, fh, RESOURCE_VIDEO);
849         }
850         if (fh->vidq.read_buf) {
851                 buffer_release(&fh->vidq, fh->vidq.read_buf);
852                 kfree(fh->vidq.read_buf);
853         }
854
855         /* stop vbi capture */
856         if (res_check(fh, RESOURCE_VBI)) {
857                 if (fh->vbiq.streaming)
858                         videobuf_streamoff(&fh->vbiq);
859                 if (fh->vbiq.reading)
860                         videobuf_read_stop(&fh->vbiq);
861                 res_free(dev, fh, RESOURCE_VBI);
862         }
863
864         videobuf_mmap_free(&fh->vidq);
865         file->private_data = NULL;
866         kfree(fh);
867
868         /* We are not putting the tuner to sleep here on exit, because
869          * we want to use the mpeg encoder in another session to capture
870          * tuner video. Closing this will result in no video to the encoder.
871          */
872
873         return 0;
874 }
875
876 static int video_mmap(struct file *file, struct vm_area_struct *vma)
877 {
878         struct cx23885_fh *fh = file->private_data;
879
880         return videobuf_mmap_mapper(get_queue(fh), vma);
881 }
882
883 /* ------------------------------------------------------------------ */
884 /* VIDEO CTRL IOCTLS                                                  */
885
886 static int cx23885_get_control(struct cx23885_dev *dev,
887         struct v4l2_control *ctl)
888 {
889         dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
890         cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_G_CTRL, ctl);
891         return 0;
892 }
893
894 static int cx23885_set_control(struct cx23885_dev *dev,
895         struct v4l2_control *ctl)
896 {
897         dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
898                 " (disabled - no action)\n", __func__);
899         return 0;
900 }
901
902 static void init_controls(struct cx23885_dev *dev)
903 {
904         struct v4l2_control ctrl;
905         int i;
906
907         for (i = 0; i < CX23885_CTLS; i++) {
908                 ctrl.id = cx23885_ctls[i].v.id;
909                 ctrl.value = cx23885_ctls[i].v.default_value;
910
911                 cx23885_set_control(dev, &ctrl);
912         }
913 }
914
915 /* ------------------------------------------------------------------ */
916 /* VIDEO IOCTLS                                                       */
917
918 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
919         struct v4l2_format *f)
920 {
921         struct cx23885_fh *fh   = priv;
922
923         f->fmt.pix.width        = fh->width;
924         f->fmt.pix.height       = fh->height;
925         f->fmt.pix.field        = fh->vidq.field;
926         f->fmt.pix.pixelformat  = fh->fmt->fourcc;
927         f->fmt.pix.bytesperline =
928                 (f->fmt.pix.width * fh->fmt->depth) >> 3;
929         f->fmt.pix.sizeimage =
930                 f->fmt.pix.height * f->fmt.pix.bytesperline;
931
932         return 0;
933 }
934
935 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
936         struct v4l2_format *f)
937 {
938         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
939         struct cx23885_fmt *fmt;
940         enum v4l2_field   field;
941         unsigned int      maxw, maxh;
942
943         fmt = format_by_fourcc(f->fmt.pix.pixelformat);
944         if (NULL == fmt)
945                 return -EINVAL;
946
947         field = f->fmt.pix.field;
948         maxw  = norm_maxw(dev->tvnorm);
949         maxh  = norm_maxh(dev->tvnorm);
950
951         if (V4L2_FIELD_ANY == field) {
952                 field = (f->fmt.pix.height > maxh/2)
953                         ? V4L2_FIELD_INTERLACED
954                         : V4L2_FIELD_BOTTOM;
955         }
956
957         switch (field) {
958         case V4L2_FIELD_TOP:
959         case V4L2_FIELD_BOTTOM:
960                 maxh = maxh / 2;
961                 break;
962         case V4L2_FIELD_INTERLACED:
963                 break;
964         default:
965                 return -EINVAL;
966         }
967
968         f->fmt.pix.field = field;
969         if (f->fmt.pix.height < 32)
970                 f->fmt.pix.height = 32;
971         if (f->fmt.pix.height > maxh)
972                 f->fmt.pix.height = maxh;
973         if (f->fmt.pix.width < 48)
974                 f->fmt.pix.width = 48;
975         if (f->fmt.pix.width > maxw)
976                 f->fmt.pix.width = maxw;
977         f->fmt.pix.width &= ~0x03;
978         f->fmt.pix.bytesperline =
979                 (f->fmt.pix.width * fmt->depth) >> 3;
980         f->fmt.pix.sizeimage =
981                 f->fmt.pix.height * f->fmt.pix.bytesperline;
982
983         return 0;
984 }
985
986 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
987         struct v4l2_format *f)
988 {
989         struct cx23885_fh *fh = priv;
990         struct cx23885_dev *dev  = ((struct cx23885_fh *)priv)->dev;
991         int err;
992
993         dprintk(2, "%s()\n", __func__);
994         err = vidioc_try_fmt_vid_cap(file, priv, f);
995
996         if (0 != err)
997                 return err;
998         fh->fmt        = format_by_fourcc(f->fmt.pix.pixelformat);
999         fh->width      = f->fmt.pix.width;
1000         fh->height     = f->fmt.pix.height;
1001         fh->vidq.field = f->fmt.pix.field;
1002         dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
1003                 fh->width, fh->height, fh->vidq.field);
1004         cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_FMT, f);
1005         return 0;
1006 }
1007
1008 static int vidioc_querycap(struct file *file, void  *priv,
1009         struct v4l2_capability *cap)
1010 {
1011         struct cx23885_dev *dev  = ((struct cx23885_fh *)priv)->dev;
1012
1013         strcpy(cap->driver, "cx23885");
1014         strlcpy(cap->card, cx23885_boards[dev->board].name,
1015                 sizeof(cap->card));
1016         sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1017         cap->version = CX23885_VERSION_CODE;
1018         cap->capabilities =
1019                 V4L2_CAP_VIDEO_CAPTURE |
1020                 V4L2_CAP_READWRITE     |
1021                 V4L2_CAP_STREAMING     |
1022                 V4L2_CAP_VBI_CAPTURE;
1023         if (UNSET != dev->tuner_type)
1024                 cap->capabilities |= V4L2_CAP_TUNER;
1025         return 0;
1026 }
1027
1028 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
1029         struct v4l2_fmtdesc *f)
1030 {
1031         if (unlikely(f->index >= ARRAY_SIZE(formats)))
1032                 return -EINVAL;
1033
1034         strlcpy(f->description, formats[f->index].name,
1035                 sizeof(f->description));
1036         f->pixelformat = formats[f->index].fourcc;
1037
1038         return 0;
1039 }
1040
1041 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1042 static int vidiocgmbuf(struct file *file, void *priv,
1043         struct video_mbuf *mbuf)
1044 {
1045         struct cx23885_fh *fh = priv;
1046         struct videobuf_queue *q;
1047         struct v4l2_requestbuffers req;
1048         unsigned int i;
1049         int err;
1050
1051         q = get_queue(fh);
1052         memset(&req, 0, sizeof(req));
1053         req.type   = q->type;
1054         req.count  = 8;
1055         req.memory = V4L2_MEMORY_MMAP;
1056         err = videobuf_reqbufs(q, &req);
1057         if (err < 0)
1058                 return err;
1059
1060         mbuf->frames = req.count;
1061         mbuf->size   = 0;
1062         for (i = 0; i < mbuf->frames; i++) {
1063                 mbuf->offsets[i]  = q->bufs[i]->boff;
1064                 mbuf->size       += q->bufs[i]->bsize;
1065         }
1066         return 0;
1067 }
1068 #endif
1069
1070 static int vidioc_reqbufs(struct file *file, void *priv,
1071         struct v4l2_requestbuffers *p)
1072 {
1073         struct cx23885_fh *fh = priv;
1074         return videobuf_reqbufs(get_queue(fh), p);
1075 }
1076
1077 static int vidioc_querybuf(struct file *file, void *priv,
1078         struct v4l2_buffer *p)
1079 {
1080         struct cx23885_fh *fh = priv;
1081         return videobuf_querybuf(get_queue(fh), p);
1082 }
1083
1084 static int vidioc_qbuf(struct file *file, void *priv,
1085         struct v4l2_buffer *p)
1086 {
1087         struct cx23885_fh *fh = priv;
1088         return videobuf_qbuf(get_queue(fh), p);
1089 }
1090
1091 static int vidioc_dqbuf(struct file *file, void *priv,
1092         struct v4l2_buffer *p)
1093 {
1094         struct cx23885_fh *fh = priv;
1095         return videobuf_dqbuf(get_queue(fh), p,
1096                                 file->f_flags & O_NONBLOCK);
1097 }
1098
1099 static int vidioc_streamon(struct file *file, void *priv,
1100         enum v4l2_buf_type i)
1101 {
1102         struct cx23885_fh *fh = priv;
1103         struct cx23885_dev *dev = fh->dev;
1104         dprintk(1, "%s()\n", __func__);
1105
1106         if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1107                 return -EINVAL;
1108         if (unlikely(i != fh->type))
1109                 return -EINVAL;
1110
1111         if (unlikely(!res_get(dev, fh, get_resource(fh))))
1112                 return -EBUSY;
1113         return videobuf_streamon(get_queue(fh));
1114 }
1115
1116 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1117 {
1118         struct cx23885_fh *fh = priv;
1119         struct cx23885_dev *dev = fh->dev;
1120         int err, res;
1121         dprintk(1, "%s()\n", __func__);
1122
1123         if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1124                 return -EINVAL;
1125         if (i != fh->type)
1126                 return -EINVAL;
1127
1128         res = get_resource(fh);
1129         err = videobuf_streamoff(get_queue(fh));
1130         if (err < 0)
1131                 return err;
1132         res_free(dev, fh, res);
1133         return 0;
1134 }
1135
1136 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1137 {
1138         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1139         dprintk(1, "%s()\n", __func__);
1140
1141         mutex_lock(&dev->lock);
1142         cx23885_set_tvnorm(dev, *tvnorms);
1143         mutex_unlock(&dev->lock);
1144
1145         return 0;
1146 }
1147
1148 static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
1149 {
1150         static const char *iname[] = {
1151                 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1152                 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1153                 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1154                 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1155                 [CX23885_VMUX_SVIDEO]     = "S-Video",
1156                 [CX23885_VMUX_TELEVISION] = "Television",
1157                 [CX23885_VMUX_CABLE]      = "Cable TV",
1158                 [CX23885_VMUX_DVB]        = "DVB",
1159                 [CX23885_VMUX_DEBUG]      = "for debug only",
1160         };
1161         unsigned int n;
1162         dprintk(1, "%s()\n", __func__);
1163
1164         n = i->index;
1165         if (n >= 4)
1166                 return -EINVAL;
1167
1168         if (0 == INPUT(n)->type)
1169                 return -EINVAL;
1170
1171         memset(i, 0, sizeof(*i));
1172         i->index = n;
1173         i->type  = V4L2_INPUT_TYPE_CAMERA;
1174         strcpy(i->name, iname[INPUT(n)->type]);
1175         if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
1176                 (CX23885_VMUX_CABLE == INPUT(n)->type))
1177                 i->type = V4L2_INPUT_TYPE_TUNER;
1178                 i->std = CX23885_NORMS;
1179         return 0;
1180 }
1181
1182 static int vidioc_enum_input(struct file *file, void *priv,
1183                                 struct v4l2_input *i)
1184 {
1185         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1186         dprintk(1, "%s()\n", __func__);
1187         return cx23885_enum_input(dev, i);
1188 }
1189
1190 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1191 {
1192         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1193
1194         *i = dev->input;
1195         dprintk(1, "%s() returns %d\n", __func__, *i);
1196         return 0;
1197 }
1198
1199 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1200 {
1201         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1202
1203         dprintk(1, "%s(%d)\n", __func__, i);
1204
1205         if (i >= 4) {
1206                 dprintk(1, "%s() -EINVAL\n", __func__);
1207                 return -EINVAL;
1208         }
1209
1210         mutex_lock(&dev->lock);
1211         cx23885_video_mux(dev, i);
1212         mutex_unlock(&dev->lock);
1213         return 0;
1214 }
1215
1216 static int vidioc_queryctrl(struct file *file, void *priv,
1217                                 struct v4l2_queryctrl *qctrl)
1218 {
1219         qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1220         if (unlikely(qctrl->id == 0))
1221                 return -EINVAL;
1222         return cx23885_ctrl_query(qctrl);
1223 }
1224
1225 static int vidioc_g_ctrl(struct file *file, void *priv,
1226                                 struct v4l2_control *ctl)
1227 {
1228         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1229
1230         return cx23885_get_control(dev, ctl);
1231 }
1232
1233 static int vidioc_s_ctrl(struct file *file, void *priv,
1234                                 struct v4l2_control *ctl)
1235 {
1236         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1237
1238         return cx23885_set_control(dev, ctl);
1239 }
1240
1241 static int vidioc_g_tuner(struct file *file, void *priv,
1242                                 struct v4l2_tuner *t)
1243 {
1244         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1245
1246         if (unlikely(UNSET == dev->tuner_type))
1247                 return -EINVAL;
1248         if (0 != t->index)
1249                 return -EINVAL;
1250
1251         strcpy(t->name, "Television");
1252         t->type       = V4L2_TUNER_ANALOG_TV;
1253         t->capability = V4L2_TUNER_CAP_NORM;
1254         t->rangehigh  = 0xffffffffUL;
1255         t->signal = 0xffff ; /* LOCKED */
1256         return 0;
1257 }
1258
1259 static int vidioc_s_tuner(struct file *file, void *priv,
1260                                 struct v4l2_tuner *t)
1261 {
1262         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1263
1264         if (UNSET == dev->tuner_type)
1265                 return -EINVAL;
1266         if (0 != t->index)
1267                 return -EINVAL;
1268         return 0;
1269 }
1270
1271 static int vidioc_g_frequency(struct file *file, void *priv,
1272                                 struct v4l2_frequency *f)
1273 {
1274         struct cx23885_fh *fh = priv;
1275         struct cx23885_dev *dev = fh->dev;
1276
1277         if (unlikely(UNSET == dev->tuner_type))
1278                 return -EINVAL;
1279
1280         /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1281         f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1282         f->frequency = dev->freq;
1283
1284         cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_G_FREQUENCY, f);
1285
1286         return 0;
1287 }
1288
1289 static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
1290 {
1291         if (unlikely(UNSET == dev->tuner_type))
1292                 return -EINVAL;
1293         if (unlikely(f->tuner != 0))
1294                 return -EINVAL;
1295
1296         mutex_lock(&dev->lock);
1297         dev->freq = f->frequency;
1298
1299         cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, f);
1300
1301         /* When changing channels it is required to reset TVAUDIO */
1302         msleep(10);
1303
1304         mutex_unlock(&dev->lock);
1305
1306         return 0;
1307 }
1308
1309 static int vidioc_s_frequency(struct file *file, void *priv,
1310                                 struct v4l2_frequency *f)
1311 {
1312         struct cx23885_fh *fh = priv;
1313         struct cx23885_dev *dev = fh->dev;
1314
1315         if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1316                 return -EINVAL;
1317         if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1318                 return -EINVAL;
1319
1320         return
1321                 cx23885_set_freq(dev, f);
1322 }
1323
1324 #ifdef CONFIG_VIDEO_ADV_DEBUG
1325 static int vidioc_g_register(struct file *file, void *fh,
1326                                 struct v4l2_dbg_register *reg)
1327 {
1328         struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1329
1330         if (!v4l2_chip_match_host(&reg->match))
1331                 return -EINVAL;
1332
1333         cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_G_REGISTER, reg);
1334
1335         return 0;
1336 }
1337
1338 static int vidioc_s_register(struct file *file, void *fh,
1339                                 struct v4l2_dbg_register *reg)
1340 {
1341         struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1342
1343         if (!v4l2_chip_match_host(&reg->match))
1344                 return -EINVAL;
1345
1346         cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_S_REGISTER, reg);
1347
1348         return 0;
1349 }
1350 #endif
1351
1352 /* ----------------------------------------------------------- */
1353
1354 static void cx23885_vid_timeout(unsigned long data)
1355 {
1356         struct cx23885_dev *dev = (struct cx23885_dev *)data;
1357         struct cx23885_dmaqueue *q = &dev->vidq;
1358         struct cx23885_buffer *buf;
1359         unsigned long flags;
1360
1361         cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1362
1363         cx_clear(VID_A_DMA_CTL, 0x11);
1364
1365         spin_lock_irqsave(&dev->slock, flags);
1366         while (!list_empty(&q->active)) {
1367                 buf = list_entry(q->active.next,
1368                         struct cx23885_buffer, vb.queue);
1369                 list_del(&buf->vb.queue);
1370                 buf->vb.state = VIDEOBUF_ERROR;
1371                 wake_up(&buf->vb.done);
1372                 printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
1373                         dev->name, buf, buf->vb.i,
1374                         (unsigned long)buf->risc.dma);
1375         }
1376         cx23885_restart_video_queue(dev, q);
1377         spin_unlock_irqrestore(&dev->slock, flags);
1378 }
1379
1380 int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1381 {
1382         u32 mask, count;
1383         int handled = 0;
1384
1385         mask   = cx_read(VID_A_INT_MSK);
1386         if (0 == (status & mask))
1387                 return handled;
1388         cx_write(VID_A_INT_STAT, status);
1389
1390         dprintk(2, "%s() status = 0x%08x\n", __func__, status);
1391         /* risc op code error */
1392         if (status & (1 << 16)) {
1393                 printk(KERN_WARNING "%s/0: video risc op code error\n",
1394                         dev->name);
1395                 cx_clear(VID_A_DMA_CTL, 0x11);
1396                 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1397         }
1398
1399         /* risc1 y */
1400         if (status & 0x01) {
1401                 spin_lock(&dev->slock);
1402                 count = cx_read(VID_A_GPCNT);
1403                 cx23885_video_wakeup(dev, &dev->vidq, count);
1404                 spin_unlock(&dev->slock);
1405                 handled++;
1406         }
1407         /* risc2 y */
1408         if (status & 0x10) {
1409                 dprintk(2, "stopper video\n");
1410                 spin_lock(&dev->slock);
1411                 cx23885_restart_video_queue(dev, &dev->vidq);
1412                 spin_unlock(&dev->slock);
1413                 handled++;
1414         }
1415
1416         return handled;
1417 }
1418
1419 /* ----------------------------------------------------------- */
1420 /* exported stuff                                              */
1421
1422 static const struct v4l2_file_operations video_fops = {
1423         .owner         = THIS_MODULE,
1424         .open          = video_open,
1425         .release       = video_release,
1426         .read          = video_read,
1427         .poll          = video_poll,
1428         .mmap          = video_mmap,
1429         .ioctl         = video_ioctl2,
1430 };
1431
1432 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1433         .vidioc_querycap      = vidioc_querycap,
1434         .vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1435         .vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1436         .vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1437         .vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1438         .vidioc_g_fmt_vbi_cap     = cx23885_vbi_fmt,
1439         .vidioc_try_fmt_vbi_cap   = cx23885_vbi_fmt,
1440         .vidioc_s_fmt_vbi_cap     = cx23885_vbi_fmt,
1441         .vidioc_reqbufs       = vidioc_reqbufs,
1442         .vidioc_querybuf      = vidioc_querybuf,
1443         .vidioc_qbuf          = vidioc_qbuf,
1444         .vidioc_dqbuf         = vidioc_dqbuf,
1445         .vidioc_s_std         = vidioc_s_std,
1446         .vidioc_enum_input    = vidioc_enum_input,
1447         .vidioc_g_input       = vidioc_g_input,
1448         .vidioc_s_input       = vidioc_s_input,
1449         .vidioc_queryctrl     = vidioc_queryctrl,
1450         .vidioc_g_ctrl        = vidioc_g_ctrl,
1451         .vidioc_s_ctrl        = vidioc_s_ctrl,
1452         .vidioc_streamon      = vidioc_streamon,
1453         .vidioc_streamoff     = vidioc_streamoff,
1454 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1455         .vidiocgmbuf          = vidiocgmbuf,
1456 #endif
1457         .vidioc_g_tuner       = vidioc_g_tuner,
1458         .vidioc_s_tuner       = vidioc_s_tuner,
1459         .vidioc_g_frequency   = vidioc_g_frequency,
1460         .vidioc_s_frequency   = vidioc_s_frequency,
1461 #ifdef CONFIG_VIDEO_ADV_DEBUG
1462         .vidioc_g_register    = vidioc_g_register,
1463         .vidioc_s_register    = vidioc_s_register,
1464 #endif
1465 };
1466
1467 static struct video_device cx23885_vbi_template;
1468 static struct video_device cx23885_video_template = {
1469         .name                 = "cx23885-video",
1470         .fops                 = &video_fops,
1471         .minor                = -1,
1472         .ioctl_ops            = &video_ioctl_ops,
1473         .tvnorms              = CX23885_NORMS,
1474         .current_norm         = V4L2_STD_NTSC_M,
1475 };
1476
1477 static const struct v4l2_file_operations radio_fops = {
1478         .owner         = THIS_MODULE,
1479         .open          = video_open,
1480         .release       = video_release,
1481         .ioctl         = video_ioctl2,
1482 };
1483
1484
1485 void cx23885_video_unregister(struct cx23885_dev *dev)
1486 {
1487         dprintk(1, "%s()\n", __func__);
1488         cx_clear(PCI_INT_MSK, 1);
1489
1490         if (dev->video_dev) {
1491                 if (-1 != dev->video_dev->minor)
1492                         video_unregister_device(dev->video_dev);
1493                 else
1494                         video_device_release(dev->video_dev);
1495                 dev->video_dev = NULL;
1496
1497                 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1498         }
1499 }
1500
1501 int cx23885_video_register(struct cx23885_dev *dev)
1502 {
1503         int err;
1504
1505         dprintk(1, "%s()\n", __func__);
1506         spin_lock_init(&dev->slock);
1507
1508         /* Initialize VBI template */
1509         memcpy(&cx23885_vbi_template, &cx23885_video_template,
1510                 sizeof(cx23885_vbi_template));
1511         strcpy(cx23885_vbi_template.name, "cx23885-vbi");
1512
1513         dev->tvnorm = cx23885_video_template.current_norm;
1514
1515         /* init video dma queues */
1516         INIT_LIST_HEAD(&dev->vidq.active);
1517         INIT_LIST_HEAD(&dev->vidq.queued);
1518         dev->vidq.timeout.function = cx23885_vid_timeout;
1519         dev->vidq.timeout.data = (unsigned long)dev;
1520         init_timer(&dev->vidq.timeout);
1521         cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1522                 VID_A_DMA_CTL, 0x11, 0x00);
1523
1524         /* Don't enable VBI yet */
1525         cx_set(PCI_INT_MSK, 1);
1526
1527
1528         /* register v4l devices */
1529         dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1530                 &cx23885_video_template, "video");
1531         err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1532                                     video_nr[dev->nr]);
1533         if (err < 0) {
1534                 printk(KERN_INFO "%s: can't register video device\n",
1535                         dev->name);
1536                 goto fail_unreg;
1537         }
1538         printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1539                dev->name, dev->video_dev->num);
1540         /* initial device configuration */
1541         mutex_lock(&dev->lock);
1542         cx23885_set_tvnorm(dev, dev->tvnorm);
1543         init_controls(dev);
1544         cx23885_video_mux(dev, 0);
1545         mutex_unlock(&dev->lock);
1546
1547         return 0;
1548
1549 fail_unreg:
1550         cx23885_video_unregister(dev);
1551         return err;
1552 }
1553