Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / 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         call_all(dev, tuner, s_std, norm);
303
304         return 0;
305 }
306
307 static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
308                                     struct pci_dev *pci,
309                                     struct video_device *template,
310                                     char *type)
311 {
312         struct video_device *vfd;
313         dprintk(1, "%s()\n", __func__);
314
315         vfd = video_device_alloc();
316         if (NULL == vfd)
317                 return NULL;
318         *vfd = *template;
319         vfd->minor = -1;
320         vfd->v4l2_dev = &dev->v4l2_dev;
321         vfd->release = video_device_release;
322         snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
323                  dev->name, type, cx23885_boards[dev->board].name);
324         return vfd;
325 }
326
327 static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
328 {
329         int i;
330
331         if (qctrl->id < V4L2_CID_BASE ||
332             qctrl->id >= V4L2_CID_LASTP1)
333                 return -EINVAL;
334         for (i = 0; i < CX23885_CTLS; i++)
335                 if (cx23885_ctls[i].v.id == qctrl->id)
336                         break;
337         if (i == CX23885_CTLS) {
338                 *qctrl = no_ctl;
339                 return 0;
340         }
341         *qctrl = cx23885_ctls[i].v;
342         return 0;
343 }
344
345 /* ------------------------------------------------------------------- */
346 /* resource management                                                 */
347
348 static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
349         unsigned int bit)
350 {
351         dprintk(1, "%s()\n", __func__);
352         if (fh->resources & bit)
353                 /* have it already allocated */
354                 return 1;
355
356         /* is it free? */
357         mutex_lock(&dev->lock);
358         if (dev->resources & bit) {
359                 /* no, someone else uses it */
360                 mutex_unlock(&dev->lock);
361                 return 0;
362         }
363         /* it's free, grab it */
364         fh->resources  |= bit;
365         dev->resources |= bit;
366         dprintk(1, "res: get %d\n", bit);
367         mutex_unlock(&dev->lock);
368         return 1;
369 }
370
371 static int res_check(struct cx23885_fh *fh, unsigned int bit)
372 {
373         return fh->resources & bit;
374 }
375
376 static int res_locked(struct cx23885_dev *dev, unsigned int bit)
377 {
378         return dev->resources & bit;
379 }
380
381 static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
382         unsigned int bits)
383 {
384         BUG_ON((fh->resources & bits) != bits);
385         dprintk(1, "%s()\n", __func__);
386
387         mutex_lock(&dev->lock);
388         fh->resources  &= ~bits;
389         dev->resources &= ~bits;
390         dprintk(1, "res: put %d\n", bits);
391         mutex_unlock(&dev->lock);
392 }
393
394 static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
395 {
396         struct v4l2_routing route;
397         memset(&route, 0, sizeof(route));
398
399         dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
400                 __func__,
401                 input, INPUT(input)->vmux,
402                 INPUT(input)->gpio0, INPUT(input)->gpio1,
403                 INPUT(input)->gpio2, INPUT(input)->gpio3);
404         dev->input = input;
405
406         route.input = INPUT(input)->vmux;
407
408         /* Tell the internal A/V decoder */
409         v4l2_subdev_call(dev->sd_cx25840, video, s_routing, &route);
410
411         return 0;
412 }
413
414 /* ------------------------------------------------------------------ */
415 static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
416         unsigned int height, enum v4l2_field field)
417 {
418         dprintk(1, "%s()\n", __func__);
419         return 0;
420 }
421
422 static int cx23885_start_video_dma(struct cx23885_dev *dev,
423                            struct cx23885_dmaqueue *q,
424                            struct cx23885_buffer *buf)
425 {
426         dprintk(1, "%s()\n", __func__);
427
428         /* setup fifo + format */
429         cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
430                                 buf->bpl, buf->risc.dma);
431         cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
432
433         /* reset counter */
434         cx_write(VID_A_GPCNT_CTL, 3);
435         q->count = 1;
436
437         /* enable irq */
438         cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
439         cx_set(VID_A_INT_MSK, 0x000011);
440
441         /* start dma */
442         cx_set(DEV_CNTRL2, (1<<5));
443         cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
444
445         return 0;
446 }
447
448
449 static int cx23885_restart_video_queue(struct cx23885_dev *dev,
450                                struct cx23885_dmaqueue *q)
451 {
452         struct cx23885_buffer *buf, *prev;
453         struct list_head *item;
454         dprintk(1, "%s()\n", __func__);
455
456         if (!list_empty(&q->active)) {
457                 buf = list_entry(q->active.next, struct cx23885_buffer,
458                         vb.queue);
459                 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
460                         buf, buf->vb.i);
461                 cx23885_start_video_dma(dev, q, buf);
462                 list_for_each(item, &q->active) {
463                         buf = list_entry(item, struct cx23885_buffer,
464                                 vb.queue);
465                         buf->count    = q->count++;
466                 }
467                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
468                 return 0;
469         }
470
471         prev = NULL;
472         for (;;) {
473                 if (list_empty(&q->queued))
474                         return 0;
475                 buf = list_entry(q->queued.next, struct cx23885_buffer,
476                         vb.queue);
477                 if (NULL == prev) {
478                         list_move_tail(&buf->vb.queue, &q->active);
479                         cx23885_start_video_dma(dev, q, buf);
480                         buf->vb.state = VIDEOBUF_ACTIVE;
481                         buf->count    = q->count++;
482                         mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
483                         dprintk(2, "[%p/%d] restart_queue - first active\n",
484                                 buf, buf->vb.i);
485
486                 } else if (prev->vb.width  == buf->vb.width  &&
487                            prev->vb.height == buf->vb.height &&
488                            prev->fmt       == buf->fmt) {
489                         list_move_tail(&buf->vb.queue, &q->active);
490                         buf->vb.state = VIDEOBUF_ACTIVE;
491                         buf->count    = q->count++;
492                         prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
493                         prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
494                         dprintk(2, "[%p/%d] restart_queue - move to active\n",
495                                 buf, buf->vb.i);
496                 } else {
497                         return 0;
498                 }
499                 prev = buf;
500         }
501 }
502
503 static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
504         unsigned int *size)
505 {
506         struct cx23885_fh *fh = q->priv_data;
507
508         *size = fh->fmt->depth*fh->width*fh->height >> 3;
509         if (0 == *count)
510                 *count = 32;
511         while (*size * *count > vid_limit * 1024 * 1024)
512                 (*count)--;
513         return 0;
514 }
515
516 static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
517                enum v4l2_field field)
518 {
519         struct cx23885_fh *fh  = q->priv_data;
520         struct cx23885_dev *dev = fh->dev;
521         struct cx23885_buffer *buf =
522                 container_of(vb, struct cx23885_buffer, vb);
523         int rc, init_buffer = 0;
524         u32 line0_offset, line1_offset;
525         struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
526
527         BUG_ON(NULL == fh->fmt);
528         if (fh->width  < 48 || fh->width  > norm_maxw(dev->tvnorm) ||
529             fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
530                 return -EINVAL;
531         buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
532         if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
533                 return -EINVAL;
534
535         if (buf->fmt       != fh->fmt    ||
536             buf->vb.width  != fh->width  ||
537             buf->vb.height != fh->height ||
538             buf->vb.field  != field) {
539                 buf->fmt       = fh->fmt;
540                 buf->vb.width  = fh->width;
541                 buf->vb.height = fh->height;
542                 buf->vb.field  = field;
543                 init_buffer = 1;
544         }
545
546         if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
547                 init_buffer = 1;
548                 rc = videobuf_iolock(q, &buf->vb, NULL);
549                 if (0 != rc)
550                         goto fail;
551         }
552
553         if (init_buffer) {
554                 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
555                 switch (buf->vb.field) {
556                 case V4L2_FIELD_TOP:
557                         cx23885_risc_buffer(dev->pci, &buf->risc,
558                                          dma->sglist, 0, UNSET,
559                                          buf->bpl, 0, buf->vb.height);
560                         break;
561                 case V4L2_FIELD_BOTTOM:
562                         cx23885_risc_buffer(dev->pci, &buf->risc,
563                                          dma->sglist, UNSET, 0,
564                                          buf->bpl, 0, buf->vb.height);
565                         break;
566                 case V4L2_FIELD_INTERLACED:
567                         if (dev->tvnorm & V4L2_STD_NTSC) {
568                                 /* cx25840 transmits NTSC bottom field first */
569                                 dprintk(1, "%s() Creating NTSC risc\n",
570                                         __func__);
571                                 line0_offset = buf->bpl;
572                                 line1_offset = 0;
573                         } else {
574                                 /* All other formats are top field first */
575                                 dprintk(1, "%s() Creating PAL/SECAM risc\n",
576                                         __func__);
577                                 line0_offset = 0;
578                                 line1_offset = buf->bpl;
579                         }
580                         cx23885_risc_buffer(dev->pci, &buf->risc,
581                                         dma->sglist, line0_offset,
582                                         line1_offset,
583                                         buf->bpl, buf->bpl,
584                                         buf->vb.height >> 1);
585                         break;
586                 case V4L2_FIELD_SEQ_TB:
587                         cx23885_risc_buffer(dev->pci, &buf->risc,
588                                          dma->sglist,
589                                          0, buf->bpl * (buf->vb.height >> 1),
590                                          buf->bpl, 0,
591                                          buf->vb.height >> 1);
592                         break;
593                 case V4L2_FIELD_SEQ_BT:
594                         cx23885_risc_buffer(dev->pci, &buf->risc,
595                                          dma->sglist,
596                                          buf->bpl * (buf->vb.height >> 1), 0,
597                                          buf->bpl, 0,
598                                          buf->vb.height >> 1);
599                         break;
600                 default:
601                         BUG();
602                 }
603         }
604         dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
605                 buf, buf->vb.i,
606                 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
607                 (unsigned long)buf->risc.dma);
608
609         buf->vb.state = VIDEOBUF_PREPARED;
610         return 0;
611
612  fail:
613         cx23885_free_buffer(q, buf);
614         return rc;
615 }
616
617 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
618 {
619         struct cx23885_buffer   *buf = container_of(vb,
620                 struct cx23885_buffer, vb);
621         struct cx23885_buffer   *prev;
622         struct cx23885_fh       *fh   = vq->priv_data;
623         struct cx23885_dev      *dev  = fh->dev;
624         struct cx23885_dmaqueue *q    = &dev->vidq;
625
626         /* add jump to stopper */
627         buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
628         buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
629         buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
630
631         if (!list_empty(&q->queued)) {
632                 list_add_tail(&buf->vb.queue, &q->queued);
633                 buf->vb.state = VIDEOBUF_QUEUED;
634                 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
635                         buf, buf->vb.i);
636
637         } else if (list_empty(&q->active)) {
638                 list_add_tail(&buf->vb.queue, &q->active);
639                 cx23885_start_video_dma(dev, q, buf);
640                 buf->vb.state = VIDEOBUF_ACTIVE;
641                 buf->count    = q->count++;
642                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
643                 dprintk(2, "[%p/%d] buffer_queue - first active\n",
644                         buf, buf->vb.i);
645
646         } else {
647                 prev = list_entry(q->active.prev, struct cx23885_buffer,
648                         vb.queue);
649                 if (prev->vb.width  == buf->vb.width  &&
650                     prev->vb.height == buf->vb.height &&
651                     prev->fmt       == buf->fmt) {
652                         list_add_tail(&buf->vb.queue, &q->active);
653                         buf->vb.state = VIDEOBUF_ACTIVE;
654                         buf->count    = q->count++;
655                         prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
656                         /* 64 bit bits 63-32 */
657                         prev->risc.jmp[2] = cpu_to_le32(0);
658                         dprintk(2, "[%p/%d] buffer_queue - append to active\n",
659                                 buf, buf->vb.i);
660
661                 } else {
662                         list_add_tail(&buf->vb.queue, &q->queued);
663                         buf->vb.state = VIDEOBUF_QUEUED;
664                         dprintk(2, "[%p/%d] buffer_queue - first queued\n",
665                                 buf, buf->vb.i);
666                 }
667         }
668 }
669
670 static void buffer_release(struct videobuf_queue *q,
671         struct videobuf_buffer *vb)
672 {
673         struct cx23885_buffer *buf = container_of(vb,
674                 struct cx23885_buffer, vb);
675
676         cx23885_free_buffer(q, buf);
677 }
678
679 static struct videobuf_queue_ops cx23885_video_qops = {
680         .buf_setup    = buffer_setup,
681         .buf_prepare  = buffer_prepare,
682         .buf_queue    = buffer_queue,
683         .buf_release  = buffer_release,
684 };
685
686 static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
687 {
688         switch (fh->type) {
689         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
690                 return &fh->vidq;
691         case V4L2_BUF_TYPE_VBI_CAPTURE:
692                 return &fh->vbiq;
693         default:
694                 BUG();
695                 return NULL;
696         }
697 }
698
699 static int get_resource(struct cx23885_fh *fh)
700 {
701         switch (fh->type) {
702         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
703                 return RESOURCE_VIDEO;
704         case V4L2_BUF_TYPE_VBI_CAPTURE:
705                 return RESOURCE_VBI;
706         default:
707                 BUG();
708                 return 0;
709         }
710 }
711
712 static int video_open(struct file *file)
713 {
714         int minor = video_devdata(file)->minor;
715         struct cx23885_dev *h, *dev = NULL;
716         struct cx23885_fh *fh;
717         struct list_head *list;
718         enum v4l2_buf_type type = 0;
719         int radio = 0;
720
721         lock_kernel();
722         list_for_each(list, &cx23885_devlist) {
723                 h = list_entry(list, struct cx23885_dev, devlist);
724                 if (h->video_dev &&
725                     h->video_dev->minor == minor) {
726                         dev  = h;
727                         type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
728                 }
729                 if (h->vbi_dev &&
730                     h->vbi_dev->minor == minor) {
731                         dev  = h;
732                         type = V4L2_BUF_TYPE_VBI_CAPTURE;
733                 }
734                 if (h->radio_dev &&
735                     h->radio_dev->minor == minor) {
736                         radio = 1;
737                         dev   = h;
738                 }
739         }
740         if (NULL == dev) {
741                 unlock_kernel();
742                 return -ENODEV;
743         }
744
745         dprintk(1, "open minor=%d radio=%d type=%s\n",
746                 minor, radio, v4l2_type_names[type]);
747
748         /* allocate + initialize per filehandle data */
749         fh = kzalloc(sizeof(*fh), GFP_KERNEL);
750         if (NULL == fh) {
751                 unlock_kernel();
752                 return -ENOMEM;
753         }
754         file->private_data = fh;
755         fh->dev      = dev;
756         fh->radio    = radio;
757         fh->type     = type;
758         fh->width    = 320;
759         fh->height   = 240;
760         fh->fmt      = format_by_fourcc(V4L2_PIX_FMT_BGR24);
761
762         videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
763                             &dev->pci->dev, &dev->slock,
764                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
765                             V4L2_FIELD_INTERLACED,
766                             sizeof(struct cx23885_buffer),
767                             fh);
768
769         dprintk(1, "post videobuf_queue_init()\n");
770
771         unlock_kernel();
772
773         return 0;
774 }
775
776 static ssize_t video_read(struct file *file, char __user *data,
777         size_t count, loff_t *ppos)
778 {
779         struct cx23885_fh *fh = file->private_data;
780
781         switch (fh->type) {
782         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
783                 if (res_locked(fh->dev, RESOURCE_VIDEO))
784                         return -EBUSY;
785                 return videobuf_read_one(&fh->vidq, data, count, ppos,
786                                          file->f_flags & O_NONBLOCK);
787         case V4L2_BUF_TYPE_VBI_CAPTURE:
788                 if (!res_get(fh->dev, fh, RESOURCE_VBI))
789                         return -EBUSY;
790                 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
791                                             file->f_flags & O_NONBLOCK);
792         default:
793                 BUG();
794                 return 0;
795         }
796 }
797
798 static unsigned int video_poll(struct file *file,
799         struct poll_table_struct *wait)
800 {
801         struct cx23885_fh *fh = file->private_data;
802         struct cx23885_buffer *buf;
803
804         if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
805                 if (!res_get(fh->dev, fh, RESOURCE_VBI))
806                         return POLLERR;
807                 return videobuf_poll_stream(file, &fh->vbiq, wait);
808         }
809
810         if (res_check(fh, RESOURCE_VIDEO)) {
811                 /* streaming capture */
812                 if (list_empty(&fh->vidq.stream))
813                         return POLLERR;
814                 buf = list_entry(fh->vidq.stream.next,
815                         struct cx23885_buffer, vb.stream);
816         } else {
817                 /* read() capture */
818                 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
819                 if (NULL == buf)
820                         return POLLERR;
821         }
822         poll_wait(file, &buf->vb.done, wait);
823         if (buf->vb.state == VIDEOBUF_DONE ||
824             buf->vb.state == VIDEOBUF_ERROR)
825                 return POLLIN|POLLRDNORM;
826         return 0;
827 }
828
829 static int video_release(struct file *file)
830 {
831         struct cx23885_fh *fh = file->private_data;
832         struct cx23885_dev *dev = fh->dev;
833
834         /* turn off overlay */
835         if (res_check(fh, RESOURCE_OVERLAY)) {
836                 /* FIXME */
837                 res_free(dev, fh, RESOURCE_OVERLAY);
838         }
839
840         /* stop video capture */
841         if (res_check(fh, RESOURCE_VIDEO)) {
842                 videobuf_queue_cancel(&fh->vidq);
843                 res_free(dev, fh, RESOURCE_VIDEO);
844         }
845         if (fh->vidq.read_buf) {
846                 buffer_release(&fh->vidq, fh->vidq.read_buf);
847                 kfree(fh->vidq.read_buf);
848         }
849
850         /* stop vbi capture */
851         if (res_check(fh, RESOURCE_VBI)) {
852                 if (fh->vbiq.streaming)
853                         videobuf_streamoff(&fh->vbiq);
854                 if (fh->vbiq.reading)
855                         videobuf_read_stop(&fh->vbiq);
856                 res_free(dev, fh, RESOURCE_VBI);
857         }
858
859         videobuf_mmap_free(&fh->vidq);
860         file->private_data = NULL;
861         kfree(fh);
862
863         /* We are not putting the tuner to sleep here on exit, because
864          * we want to use the mpeg encoder in another session to capture
865          * tuner video. Closing this will result in no video to the encoder.
866          */
867
868         return 0;
869 }
870
871 static int video_mmap(struct file *file, struct vm_area_struct *vma)
872 {
873         struct cx23885_fh *fh = file->private_data;
874
875         return videobuf_mmap_mapper(get_queue(fh), vma);
876 }
877
878 /* ------------------------------------------------------------------ */
879 /* VIDEO CTRL IOCTLS                                                  */
880
881 static int cx23885_get_control(struct cx23885_dev *dev,
882         struct v4l2_control *ctl)
883 {
884         dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
885         call_all(dev, core, g_ctrl, ctl);
886         return 0;
887 }
888
889 static int cx23885_set_control(struct cx23885_dev *dev,
890         struct v4l2_control *ctl)
891 {
892         dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
893                 " (disabled - no action)\n", __func__);
894         return 0;
895 }
896
897 static void init_controls(struct cx23885_dev *dev)
898 {
899         struct v4l2_control ctrl;
900         int i;
901
902         for (i = 0; i < CX23885_CTLS; i++) {
903                 ctrl.id = cx23885_ctls[i].v.id;
904                 ctrl.value = cx23885_ctls[i].v.default_value;
905
906                 cx23885_set_control(dev, &ctrl);
907         }
908 }
909
910 /* ------------------------------------------------------------------ */
911 /* VIDEO IOCTLS                                                       */
912
913 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
914         struct v4l2_format *f)
915 {
916         struct cx23885_fh *fh   = priv;
917
918         f->fmt.pix.width        = fh->width;
919         f->fmt.pix.height       = fh->height;
920         f->fmt.pix.field        = fh->vidq.field;
921         f->fmt.pix.pixelformat  = fh->fmt->fourcc;
922         f->fmt.pix.bytesperline =
923                 (f->fmt.pix.width * fh->fmt->depth) >> 3;
924         f->fmt.pix.sizeimage =
925                 f->fmt.pix.height * f->fmt.pix.bytesperline;
926
927         return 0;
928 }
929
930 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
931         struct v4l2_format *f)
932 {
933         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
934         struct cx23885_fmt *fmt;
935         enum v4l2_field   field;
936         unsigned int      maxw, maxh;
937
938         fmt = format_by_fourcc(f->fmt.pix.pixelformat);
939         if (NULL == fmt)
940                 return -EINVAL;
941
942         field = f->fmt.pix.field;
943         maxw  = norm_maxw(dev->tvnorm);
944         maxh  = norm_maxh(dev->tvnorm);
945
946         if (V4L2_FIELD_ANY == field) {
947                 field = (f->fmt.pix.height > maxh/2)
948                         ? V4L2_FIELD_INTERLACED
949                         : V4L2_FIELD_BOTTOM;
950         }
951
952         switch (field) {
953         case V4L2_FIELD_TOP:
954         case V4L2_FIELD_BOTTOM:
955                 maxh = maxh / 2;
956                 break;
957         case V4L2_FIELD_INTERLACED:
958                 break;
959         default:
960                 return -EINVAL;
961         }
962
963         f->fmt.pix.field = field;
964         if (f->fmt.pix.height < 32)
965                 f->fmt.pix.height = 32;
966         if (f->fmt.pix.height > maxh)
967                 f->fmt.pix.height = maxh;
968         if (f->fmt.pix.width < 48)
969                 f->fmt.pix.width = 48;
970         if (f->fmt.pix.width > maxw)
971                 f->fmt.pix.width = maxw;
972         f->fmt.pix.width &= ~0x03;
973         f->fmt.pix.bytesperline =
974                 (f->fmt.pix.width * fmt->depth) >> 3;
975         f->fmt.pix.sizeimage =
976                 f->fmt.pix.height * f->fmt.pix.bytesperline;
977
978         return 0;
979 }
980
981 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
982         struct v4l2_format *f)
983 {
984         struct cx23885_fh *fh = priv;
985         struct cx23885_dev *dev  = ((struct cx23885_fh *)priv)->dev;
986         int err;
987
988         dprintk(2, "%s()\n", __func__);
989         err = vidioc_try_fmt_vid_cap(file, priv, f);
990
991         if (0 != err)
992                 return err;
993         fh->fmt        = format_by_fourcc(f->fmt.pix.pixelformat);
994         fh->width      = f->fmt.pix.width;
995         fh->height     = f->fmt.pix.height;
996         fh->vidq.field = f->fmt.pix.field;
997         dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
998                 fh->width, fh->height, fh->vidq.field);
999         call_all(dev, video, s_fmt, f);
1000         return 0;
1001 }
1002
1003 static int vidioc_querycap(struct file *file, void  *priv,
1004         struct v4l2_capability *cap)
1005 {
1006         struct cx23885_dev *dev  = ((struct cx23885_fh *)priv)->dev;
1007
1008         strcpy(cap->driver, "cx23885");
1009         strlcpy(cap->card, cx23885_boards[dev->board].name,
1010                 sizeof(cap->card));
1011         sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1012         cap->version = CX23885_VERSION_CODE;
1013         cap->capabilities =
1014                 V4L2_CAP_VIDEO_CAPTURE |
1015                 V4L2_CAP_READWRITE     |
1016                 V4L2_CAP_STREAMING     |
1017                 V4L2_CAP_VBI_CAPTURE;
1018         if (UNSET != dev->tuner_type)
1019                 cap->capabilities |= V4L2_CAP_TUNER;
1020         return 0;
1021 }
1022
1023 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
1024         struct v4l2_fmtdesc *f)
1025 {
1026         if (unlikely(f->index >= ARRAY_SIZE(formats)))
1027                 return -EINVAL;
1028
1029         strlcpy(f->description, formats[f->index].name,
1030                 sizeof(f->description));
1031         f->pixelformat = formats[f->index].fourcc;
1032
1033         return 0;
1034 }
1035
1036 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1037 static int vidiocgmbuf(struct file *file, void *priv,
1038         struct video_mbuf *mbuf)
1039 {
1040         struct cx23885_fh *fh = priv;
1041         struct videobuf_queue *q;
1042         struct v4l2_requestbuffers req;
1043         unsigned int i;
1044         int err;
1045
1046         q = get_queue(fh);
1047         memset(&req, 0, sizeof(req));
1048         req.type   = q->type;
1049         req.count  = 8;
1050         req.memory = V4L2_MEMORY_MMAP;
1051         err = videobuf_reqbufs(q, &req);
1052         if (err < 0)
1053                 return err;
1054
1055         mbuf->frames = req.count;
1056         mbuf->size   = 0;
1057         for (i = 0; i < mbuf->frames; i++) {
1058                 mbuf->offsets[i]  = q->bufs[i]->boff;
1059                 mbuf->size       += q->bufs[i]->bsize;
1060         }
1061         return 0;
1062 }
1063 #endif
1064
1065 static int vidioc_reqbufs(struct file *file, void *priv,
1066         struct v4l2_requestbuffers *p)
1067 {
1068         struct cx23885_fh *fh = priv;
1069         return videobuf_reqbufs(get_queue(fh), p);
1070 }
1071
1072 static int vidioc_querybuf(struct file *file, void *priv,
1073         struct v4l2_buffer *p)
1074 {
1075         struct cx23885_fh *fh = priv;
1076         return videobuf_querybuf(get_queue(fh), p);
1077 }
1078
1079 static int vidioc_qbuf(struct file *file, void *priv,
1080         struct v4l2_buffer *p)
1081 {
1082         struct cx23885_fh *fh = priv;
1083         return videobuf_qbuf(get_queue(fh), p);
1084 }
1085
1086 static int vidioc_dqbuf(struct file *file, void *priv,
1087         struct v4l2_buffer *p)
1088 {
1089         struct cx23885_fh *fh = priv;
1090         return videobuf_dqbuf(get_queue(fh), p,
1091                                 file->f_flags & O_NONBLOCK);
1092 }
1093
1094 static int vidioc_streamon(struct file *file, void *priv,
1095         enum v4l2_buf_type i)
1096 {
1097         struct cx23885_fh *fh = priv;
1098         struct cx23885_dev *dev = fh->dev;
1099         dprintk(1, "%s()\n", __func__);
1100
1101         if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1102                 return -EINVAL;
1103         if (unlikely(i != fh->type))
1104                 return -EINVAL;
1105
1106         if (unlikely(!res_get(dev, fh, get_resource(fh))))
1107                 return -EBUSY;
1108         return videobuf_streamon(get_queue(fh));
1109 }
1110
1111 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1112 {
1113         struct cx23885_fh *fh = priv;
1114         struct cx23885_dev *dev = fh->dev;
1115         int err, res;
1116         dprintk(1, "%s()\n", __func__);
1117
1118         if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1119                 return -EINVAL;
1120         if (i != fh->type)
1121                 return -EINVAL;
1122
1123         res = get_resource(fh);
1124         err = videobuf_streamoff(get_queue(fh));
1125         if (err < 0)
1126                 return err;
1127         res_free(dev, fh, res);
1128         return 0;
1129 }
1130
1131 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1132 {
1133         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1134         dprintk(1, "%s()\n", __func__);
1135
1136         mutex_lock(&dev->lock);
1137         cx23885_set_tvnorm(dev, *tvnorms);
1138         mutex_unlock(&dev->lock);
1139
1140         return 0;
1141 }
1142
1143 static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
1144 {
1145         static const char *iname[] = {
1146                 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1147                 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1148                 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1149                 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1150                 [CX23885_VMUX_SVIDEO]     = "S-Video",
1151                 [CX23885_VMUX_TELEVISION] = "Television",
1152                 [CX23885_VMUX_CABLE]      = "Cable TV",
1153                 [CX23885_VMUX_DVB]        = "DVB",
1154                 [CX23885_VMUX_DEBUG]      = "for debug only",
1155         };
1156         unsigned int n;
1157         dprintk(1, "%s()\n", __func__);
1158
1159         n = i->index;
1160         if (n >= 4)
1161                 return -EINVAL;
1162
1163         if (0 == INPUT(n)->type)
1164                 return -EINVAL;
1165
1166         memset(i, 0, sizeof(*i));
1167         i->index = n;
1168         i->type  = V4L2_INPUT_TYPE_CAMERA;
1169         strcpy(i->name, iname[INPUT(n)->type]);
1170         if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
1171                 (CX23885_VMUX_CABLE == INPUT(n)->type))
1172                 i->type = V4L2_INPUT_TYPE_TUNER;
1173                 i->std = CX23885_NORMS;
1174         return 0;
1175 }
1176
1177 static int vidioc_enum_input(struct file *file, void *priv,
1178                                 struct v4l2_input *i)
1179 {
1180         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1181         dprintk(1, "%s()\n", __func__);
1182         return cx23885_enum_input(dev, i);
1183 }
1184
1185 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1186 {
1187         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1188
1189         *i = dev->input;
1190         dprintk(1, "%s() returns %d\n", __func__, *i);
1191         return 0;
1192 }
1193
1194 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1195 {
1196         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1197
1198         dprintk(1, "%s(%d)\n", __func__, i);
1199
1200         if (i >= 4) {
1201                 dprintk(1, "%s() -EINVAL\n", __func__);
1202                 return -EINVAL;
1203         }
1204
1205         mutex_lock(&dev->lock);
1206         cx23885_video_mux(dev, i);
1207         mutex_unlock(&dev->lock);
1208         return 0;
1209 }
1210
1211 static int vidioc_queryctrl(struct file *file, void *priv,
1212                                 struct v4l2_queryctrl *qctrl)
1213 {
1214         qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1215         if (unlikely(qctrl->id == 0))
1216                 return -EINVAL;
1217         return cx23885_ctrl_query(qctrl);
1218 }
1219
1220 static int vidioc_g_ctrl(struct file *file, void *priv,
1221                                 struct v4l2_control *ctl)
1222 {
1223         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1224
1225         return cx23885_get_control(dev, ctl);
1226 }
1227
1228 static int vidioc_s_ctrl(struct file *file, void *priv,
1229                                 struct v4l2_control *ctl)
1230 {
1231         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1232
1233         return cx23885_set_control(dev, ctl);
1234 }
1235
1236 static int vidioc_g_tuner(struct file *file, void *priv,
1237                                 struct v4l2_tuner *t)
1238 {
1239         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1240
1241         if (unlikely(UNSET == dev->tuner_type))
1242                 return -EINVAL;
1243         if (0 != t->index)
1244                 return -EINVAL;
1245
1246         strcpy(t->name, "Television");
1247         t->type       = V4L2_TUNER_ANALOG_TV;
1248         t->capability = V4L2_TUNER_CAP_NORM;
1249         t->rangehigh  = 0xffffffffUL;
1250         t->signal = 0xffff ; /* LOCKED */
1251         return 0;
1252 }
1253
1254 static int vidioc_s_tuner(struct file *file, void *priv,
1255                                 struct v4l2_tuner *t)
1256 {
1257         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1258
1259         if (UNSET == dev->tuner_type)
1260                 return -EINVAL;
1261         if (0 != t->index)
1262                 return -EINVAL;
1263         return 0;
1264 }
1265
1266 static int vidioc_g_frequency(struct file *file, void *priv,
1267                                 struct v4l2_frequency *f)
1268 {
1269         struct cx23885_fh *fh = priv;
1270         struct cx23885_dev *dev = fh->dev;
1271
1272         if (unlikely(UNSET == dev->tuner_type))
1273                 return -EINVAL;
1274
1275         /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1276         f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1277         f->frequency = dev->freq;
1278
1279         call_all(dev, tuner, g_frequency, f);
1280
1281         return 0;
1282 }
1283
1284 static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
1285 {
1286         if (unlikely(UNSET == dev->tuner_type))
1287                 return -EINVAL;
1288         if (unlikely(f->tuner != 0))
1289                 return -EINVAL;
1290
1291         mutex_lock(&dev->lock);
1292         dev->freq = f->frequency;
1293
1294         call_all(dev, tuner, s_frequency, f);
1295
1296         /* When changing channels it is required to reset TVAUDIO */
1297         msleep(10);
1298
1299         mutex_unlock(&dev->lock);
1300
1301         return 0;
1302 }
1303
1304 static int vidioc_s_frequency(struct file *file, void *priv,
1305                                 struct v4l2_frequency *f)
1306 {
1307         struct cx23885_fh *fh = priv;
1308         struct cx23885_dev *dev = fh->dev;
1309
1310         if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1311                 return -EINVAL;
1312         if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1313                 return -EINVAL;
1314
1315         return
1316                 cx23885_set_freq(dev, f);
1317 }
1318
1319 #ifdef CONFIG_VIDEO_ADV_DEBUG
1320 static int vidioc_g_register(struct file *file, void *fh,
1321                                 struct v4l2_dbg_register *reg)
1322 {
1323         struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1324
1325         if (!v4l2_chip_match_host(&reg->match))
1326                 return -EINVAL;
1327
1328         call_all(dev, core, g_register, reg);
1329
1330         return 0;
1331 }
1332
1333 static int vidioc_s_register(struct file *file, void *fh,
1334                                 struct v4l2_dbg_register *reg)
1335 {
1336         struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1337
1338         if (!v4l2_chip_match_host(&reg->match))
1339                 return -EINVAL;
1340
1341         call_all(dev, core, s_register, reg);
1342
1343         return 0;
1344 }
1345 #endif
1346
1347 /* ----------------------------------------------------------- */
1348
1349 static void cx23885_vid_timeout(unsigned long data)
1350 {
1351         struct cx23885_dev *dev = (struct cx23885_dev *)data;
1352         struct cx23885_dmaqueue *q = &dev->vidq;
1353         struct cx23885_buffer *buf;
1354         unsigned long flags;
1355
1356         cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1357
1358         cx_clear(VID_A_DMA_CTL, 0x11);
1359
1360         spin_lock_irqsave(&dev->slock, flags);
1361         while (!list_empty(&q->active)) {
1362                 buf = list_entry(q->active.next,
1363                         struct cx23885_buffer, vb.queue);
1364                 list_del(&buf->vb.queue);
1365                 buf->vb.state = VIDEOBUF_ERROR;
1366                 wake_up(&buf->vb.done);
1367                 printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
1368                         dev->name, buf, buf->vb.i,
1369                         (unsigned long)buf->risc.dma);
1370         }
1371         cx23885_restart_video_queue(dev, q);
1372         spin_unlock_irqrestore(&dev->slock, flags);
1373 }
1374
1375 int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1376 {
1377         u32 mask, count;
1378         int handled = 0;
1379
1380         mask   = cx_read(VID_A_INT_MSK);
1381         if (0 == (status & mask))
1382                 return handled;
1383         cx_write(VID_A_INT_STAT, status);
1384
1385         dprintk(2, "%s() status = 0x%08x\n", __func__, status);
1386         /* risc op code error */
1387         if (status & (1 << 16)) {
1388                 printk(KERN_WARNING "%s/0: video risc op code error\n",
1389                         dev->name);
1390                 cx_clear(VID_A_DMA_CTL, 0x11);
1391                 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1392         }
1393
1394         /* risc1 y */
1395         if (status & 0x01) {
1396                 spin_lock(&dev->slock);
1397                 count = cx_read(VID_A_GPCNT);
1398                 cx23885_video_wakeup(dev, &dev->vidq, count);
1399                 spin_unlock(&dev->slock);
1400                 handled++;
1401         }
1402         /* risc2 y */
1403         if (status & 0x10) {
1404                 dprintk(2, "stopper video\n");
1405                 spin_lock(&dev->slock);
1406                 cx23885_restart_video_queue(dev, &dev->vidq);
1407                 spin_unlock(&dev->slock);
1408                 handled++;
1409         }
1410
1411         return handled;
1412 }
1413
1414 /* ----------------------------------------------------------- */
1415 /* exported stuff                                              */
1416
1417 static const struct v4l2_file_operations video_fops = {
1418         .owner         = THIS_MODULE,
1419         .open          = video_open,
1420         .release       = video_release,
1421         .read          = video_read,
1422         .poll          = video_poll,
1423         .mmap          = video_mmap,
1424         .ioctl         = video_ioctl2,
1425 };
1426
1427 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1428         .vidioc_querycap      = vidioc_querycap,
1429         .vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1430         .vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1431         .vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1432         .vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1433         .vidioc_g_fmt_vbi_cap     = cx23885_vbi_fmt,
1434         .vidioc_try_fmt_vbi_cap   = cx23885_vbi_fmt,
1435         .vidioc_s_fmt_vbi_cap     = cx23885_vbi_fmt,
1436         .vidioc_reqbufs       = vidioc_reqbufs,
1437         .vidioc_querybuf      = vidioc_querybuf,
1438         .vidioc_qbuf          = vidioc_qbuf,
1439         .vidioc_dqbuf         = vidioc_dqbuf,
1440         .vidioc_s_std         = vidioc_s_std,
1441         .vidioc_enum_input    = vidioc_enum_input,
1442         .vidioc_g_input       = vidioc_g_input,
1443         .vidioc_s_input       = vidioc_s_input,
1444         .vidioc_queryctrl     = vidioc_queryctrl,
1445         .vidioc_g_ctrl        = vidioc_g_ctrl,
1446         .vidioc_s_ctrl        = vidioc_s_ctrl,
1447         .vidioc_streamon      = vidioc_streamon,
1448         .vidioc_streamoff     = vidioc_streamoff,
1449 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1450         .vidiocgmbuf          = vidiocgmbuf,
1451 #endif
1452         .vidioc_g_tuner       = vidioc_g_tuner,
1453         .vidioc_s_tuner       = vidioc_s_tuner,
1454         .vidioc_g_frequency   = vidioc_g_frequency,
1455         .vidioc_s_frequency   = vidioc_s_frequency,
1456 #ifdef CONFIG_VIDEO_ADV_DEBUG
1457         .vidioc_g_register    = vidioc_g_register,
1458         .vidioc_s_register    = vidioc_s_register,
1459 #endif
1460 };
1461
1462 static struct video_device cx23885_vbi_template;
1463 static struct video_device cx23885_video_template = {
1464         .name                 = "cx23885-video",
1465         .fops                 = &video_fops,
1466         .minor                = -1,
1467         .ioctl_ops            = &video_ioctl_ops,
1468         .tvnorms              = CX23885_NORMS,
1469         .current_norm         = V4L2_STD_NTSC_M,
1470 };
1471
1472 static const struct v4l2_file_operations radio_fops = {
1473         .owner         = THIS_MODULE,
1474         .open          = video_open,
1475         .release       = video_release,
1476         .ioctl         = video_ioctl2,
1477 };
1478
1479
1480 void cx23885_video_unregister(struct cx23885_dev *dev)
1481 {
1482         dprintk(1, "%s()\n", __func__);
1483         cx_clear(PCI_INT_MSK, 1);
1484
1485         if (dev->video_dev) {
1486                 if (-1 != dev->video_dev->minor)
1487                         video_unregister_device(dev->video_dev);
1488                 else
1489                         video_device_release(dev->video_dev);
1490                 dev->video_dev = NULL;
1491
1492                 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1493         }
1494 }
1495
1496 int cx23885_video_register(struct cx23885_dev *dev)
1497 {
1498         int err;
1499
1500         dprintk(1, "%s()\n", __func__);
1501         spin_lock_init(&dev->slock);
1502
1503         /* Initialize VBI template */
1504         memcpy(&cx23885_vbi_template, &cx23885_video_template,
1505                 sizeof(cx23885_vbi_template));
1506         strcpy(cx23885_vbi_template.name, "cx23885-vbi");
1507
1508         dev->tvnorm = cx23885_video_template.current_norm;
1509
1510         /* init video dma queues */
1511         INIT_LIST_HEAD(&dev->vidq.active);
1512         INIT_LIST_HEAD(&dev->vidq.queued);
1513         dev->vidq.timeout.function = cx23885_vid_timeout;
1514         dev->vidq.timeout.data = (unsigned long)dev;
1515         init_timer(&dev->vidq.timeout);
1516         cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1517                 VID_A_DMA_CTL, 0x11, 0x00);
1518
1519         /* Don't enable VBI yet */
1520         cx_set(PCI_INT_MSK, 1);
1521
1522         if (TUNER_ABSENT != dev->tuner_type) {
1523                 struct v4l2_subdev *sd = NULL;
1524
1525                 if (dev->tuner_addr)
1526                         sd = v4l2_i2c_new_subdev(&dev->i2c_bus[1].i2c_adap,
1527                                 "tuner", "tuner", dev->tuner_addr);
1528                 else
1529                         sd = v4l2_i2c_new_probed_subdev(&dev->i2c_bus[1].i2c_adap,
1530                                 "tuner", "tuner", v4l2_i2c_tuner_addrs(ADDRS_TV));
1531                 if (sd) {
1532                         struct tuner_setup tun_setup;
1533
1534                         tun_setup.mode_mask = T_ANALOG_TV;
1535                         tun_setup.type = dev->tuner_type;
1536                         tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1537
1538                         v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1539                 }
1540         }
1541
1542
1543         /* register v4l devices */
1544         dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1545                 &cx23885_video_template, "video");
1546         err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1547                                     video_nr[dev->nr]);
1548         if (err < 0) {
1549                 printk(KERN_INFO "%s: can't register video device\n",
1550                         dev->name);
1551                 goto fail_unreg;
1552         }
1553         printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1554                dev->name, dev->video_dev->num);
1555         /* initial device configuration */
1556         mutex_lock(&dev->lock);
1557         cx23885_set_tvnorm(dev, dev->tvnorm);
1558         init_controls(dev);
1559         cx23885_video_mux(dev, 0);
1560         mutex_unlock(&dev->lock);
1561
1562         return 0;
1563
1564 fail_unreg:
1565         cx23885_video_unregister(dev);
1566         return err;
1567 }
1568