]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/media/pci/cx23885/cx23885-video.c
Merge branch 'for-4.3/wacom' into for-next
[karo-tx-linux.git] / drivers / media / pci / 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
18 #include <linux/init.h>
19 #include <linux/list.h>
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/kmod.h>
23 #include <linux/kernel.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
27 #include <linux/kthread.h>
28 #include <asm/div64.h>
29
30 #include "cx23885.h"
31 #include "cx23885-video.h"
32 #include <media/v4l2-common.h>
33 #include <media/v4l2-ioctl.h>
34 #include <media/v4l2-event.h>
35 #include "cx23885-ioctl.h"
36 #include "tuner-xc2028.h"
37
38 #include <media/cx25840.h>
39
40 MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
41 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
42 MODULE_LICENSE("GPL");
43
44 /* ------------------------------------------------------------------ */
45
46 static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
47 static unsigned int vbi_nr[]   = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
48
49 module_param_array(video_nr, int, NULL, 0444);
50 module_param_array(vbi_nr,   int, NULL, 0444);
51
52 MODULE_PARM_DESC(video_nr, "video device numbers");
53 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
54
55 static unsigned int video_debug;
56 module_param(video_debug, int, 0644);
57 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
58
59 static unsigned int irq_debug;
60 module_param(irq_debug, int, 0644);
61 MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
62
63 static unsigned int vid_limit = 16;
64 module_param(vid_limit, int, 0644);
65 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
66
67 #define dprintk(level, fmt, arg...)\
68         do { if (video_debug >= level)\
69                 printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
70         } while (0)
71
72 /* ------------------------------------------------------------------- */
73 /* static data                                                         */
74
75 #define FORMAT_FLAGS_PACKED       0x01
76 static struct cx23885_fmt formats[] = {
77         {
78                 .name     = "4:2:2, packed, YUYV",
79                 .fourcc   = V4L2_PIX_FMT_YUYV,
80                 .depth    = 16,
81                 .flags    = FORMAT_FLAGS_PACKED,
82         }
83 };
84
85 static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
86 {
87         unsigned int i;
88
89         for (i = 0; i < ARRAY_SIZE(formats); i++)
90                 if (formats[i].fourcc == fourcc)
91                         return formats+i;
92         return NULL;
93 }
94
95 /* ------------------------------------------------------------------- */
96
97 void cx23885_video_wakeup(struct cx23885_dev *dev,
98         struct cx23885_dmaqueue *q, u32 count)
99 {
100         struct cx23885_buffer *buf;
101
102         if (list_empty(&q->active))
103                 return;
104         buf = list_entry(q->active.next,
105                         struct cx23885_buffer, queue);
106
107         buf->vb.v4l2_buf.sequence = q->count++;
108         v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
109         dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.v4l2_buf.index,
110                         count, q->count);
111         list_del(&buf->queue);
112         vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
113 }
114
115 int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
116 {
117         dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
118                 __func__,
119                 (unsigned int)norm,
120                 v4l2_norm_to_name(norm));
121
122         if (dev->tvnorm != norm) {
123                 if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
124                     vb2_is_busy(&dev->vb2_mpegq))
125                         return -EBUSY;
126         }
127
128         dev->tvnorm = norm;
129
130         call_all(dev, video, s_std, norm);
131
132         return 0;
133 }
134
135 static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
136                                     struct pci_dev *pci,
137                                     struct video_device *template,
138                                     char *type)
139 {
140         struct video_device *vfd;
141         dprintk(1, "%s()\n", __func__);
142
143         vfd = video_device_alloc();
144         if (NULL == vfd)
145                 return NULL;
146         *vfd = *template;
147         vfd->v4l2_dev = &dev->v4l2_dev;
148         vfd->release = video_device_release;
149         vfd->lock = &dev->lock;
150         snprintf(vfd->name, sizeof(vfd->name), "%s (%s)",
151                  cx23885_boards[dev->board].name, type);
152         video_set_drvdata(vfd, dev);
153         return vfd;
154 }
155
156 int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data)
157 {
158         /* 8 bit registers, 8 bit values */
159         u8 buf[] = { reg, data };
160
161         struct i2c_msg msg = { .addr = 0x98 >> 1,
162                 .flags = 0, .buf = buf, .len = 2 };
163
164         return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
165 }
166
167 u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg)
168 {
169         /* 8 bit registers, 8 bit values */
170         int ret;
171         u8 b0[] = { reg };
172         u8 b1[] = { 0 };
173
174         struct i2c_msg msg[] = {
175                 { .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 },
176                 { .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 }
177         };
178
179         ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2);
180         if (ret != 2)
181                 printk(KERN_ERR "%s() error\n", __func__);
182
183         return b1[0];
184 }
185
186 static void cx23885_flatiron_dump(struct cx23885_dev *dev)
187 {
188         int i;
189         dprintk(1, "Flatiron dump\n");
190         for (i = 0; i < 0x24; i++) {
191                 dprintk(1, "FI[%02x] = %02x\n", i,
192                         cx23885_flatiron_read(dev, i));
193         }
194 }
195
196 static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input)
197 {
198         u8 val;
199         dprintk(1, "%s(input = %d)\n", __func__, input);
200
201         if (input == 1)
202                 val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL;
203         else if (input == 2)
204                 val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL;
205         else
206                 return -EINVAL;
207
208         val |= 0x20; /* Enable clock to delta-sigma and dec filter */
209
210         cx23885_flatiron_write(dev, CH_PWR_CTRL1, val);
211
212         /* Wake up */
213         cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0);
214
215         if (video_debug)
216                 cx23885_flatiron_dump(dev);
217
218         return 0;
219 }
220
221 static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
222 {
223         dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
224                 __func__,
225                 input, INPUT(input)->vmux,
226                 INPUT(input)->gpio0, INPUT(input)->gpio1,
227                 INPUT(input)->gpio2, INPUT(input)->gpio3);
228         dev->input = input;
229
230         if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
231                 dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2 ||
232                 dev->board == CX23885_BOARD_MYGICA_X8507) {
233                 /* Select Analog TV */
234                 if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
235                         cx23885_gpio_clear(dev, GPIO_0);
236         }
237
238         /* Tell the internal A/V decoder */
239         v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
240                         INPUT(input)->vmux, 0, 0);
241
242         if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) ||
243                 (dev->board == CX23885_BOARD_MPX885) ||
244                 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1250) ||
245                 (dev->board == CX23885_BOARD_HAUPPAUGE_IMPACTVCBE) ||
246                 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
247                 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
248                 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
249                 (dev->board == CX23885_BOARD_MYGICA_X8507) ||
250                 (dev->board == CX23885_BOARD_AVERMEDIA_HC81R)) {
251                 /* Configure audio routing */
252                 v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
253                         INPUT(input)->amux, 0, 0);
254
255                 if (INPUT(input)->amux == CX25840_AUDIO7)
256                         cx23885_flatiron_mux(dev, 1);
257                 else if (INPUT(input)->amux == CX25840_AUDIO6)
258                         cx23885_flatiron_mux(dev, 2);
259         }
260
261         return 0;
262 }
263
264 static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
265 {
266         dprintk(1, "%s(input=%d)\n", __func__, input);
267
268         /* The baseband video core of the cx23885 has two audio inputs.
269          * LR1 and LR2. In almost every single case so far only HVR1xxx
270          * cards we've only ever supported LR1. Time to support LR2,
271          * which is available via the optional white breakout header on
272          * the board.
273          * We'll use a could of existing enums in the card struct to allow
274          * devs to specify which baseband input they need, or just default
275          * to what we've always used.
276          */
277         if (INPUT(input)->amux == CX25840_AUDIO7)
278                 cx23885_flatiron_mux(dev, 1);
279         else if (INPUT(input)->amux == CX25840_AUDIO6)
280                 cx23885_flatiron_mux(dev, 2);
281         else {
282                 /* Not specifically defined, assume the default. */
283                 cx23885_flatiron_mux(dev, 1);
284         }
285
286         return 0;
287 }
288
289 /* ------------------------------------------------------------------ */
290 static int cx23885_start_video_dma(struct cx23885_dev *dev,
291                            struct cx23885_dmaqueue *q,
292                            struct cx23885_buffer *buf)
293 {
294         dprintk(1, "%s()\n", __func__);
295
296         /* Stop the dma/fifo before we tamper with it's risc programs */
297         cx_clear(VID_A_DMA_CTL, 0x11);
298
299         /* setup fifo + format */
300         cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
301                                 buf->bpl, buf->risc.dma);
302
303         /* reset counter */
304         cx_write(VID_A_GPCNT_CTL, 3);
305         q->count = 0;
306
307         /* enable irq */
308         cx23885_irq_add_enable(dev, 0x01);
309         cx_set(VID_A_INT_MSK, 0x000011);
310
311         /* start dma */
312         cx_set(DEV_CNTRL2, (1<<5));
313         cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
314
315         return 0;
316 }
317
318 static int queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
319                            unsigned int *num_buffers, unsigned int *num_planes,
320                            unsigned int sizes[], void *alloc_ctxs[])
321 {
322         struct cx23885_dev *dev = q->drv_priv;
323
324         *num_planes = 1;
325         sizes[0] = (dev->fmt->depth * dev->width * dev->height) >> 3;
326         alloc_ctxs[0] = dev->alloc_ctx;
327         return 0;
328 }
329
330 static int buffer_prepare(struct vb2_buffer *vb)
331 {
332         struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
333         struct cx23885_buffer *buf =
334                 container_of(vb, struct cx23885_buffer, vb);
335         u32 line0_offset, line1_offset;
336         struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
337         int field_tff;
338
339         buf->bpl = (dev->width * dev->fmt->depth) >> 3;
340
341         if (vb2_plane_size(vb, 0) < dev->height * buf->bpl)
342                 return -EINVAL;
343         vb2_set_plane_payload(vb, 0, dev->height * buf->bpl);
344
345         switch (dev->field) {
346         case V4L2_FIELD_TOP:
347                 cx23885_risc_buffer(dev->pci, &buf->risc,
348                                 sgt->sgl, 0, UNSET,
349                                 buf->bpl, 0, dev->height);
350                 break;
351         case V4L2_FIELD_BOTTOM:
352                 cx23885_risc_buffer(dev->pci, &buf->risc,
353                                 sgt->sgl, UNSET, 0,
354                                 buf->bpl, 0, dev->height);
355                 break;
356         case V4L2_FIELD_INTERLACED:
357                 if (dev->tvnorm & V4L2_STD_525_60)
358                         /* NTSC or  */
359                         field_tff = 1;
360                 else
361                         field_tff = 0;
362
363                 if (cx23885_boards[dev->board].force_bff)
364                         /* PAL / SECAM OR 888 in NTSC MODE */
365                         field_tff = 0;
366
367                 if (field_tff) {
368                         /* cx25840 transmits NTSC bottom field first */
369                         dprintk(1, "%s() Creating TFF/NTSC risc\n",
370                                         __func__);
371                         line0_offset = buf->bpl;
372                         line1_offset = 0;
373                 } else {
374                         /* All other formats are top field first */
375                         dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
376                                         __func__);
377                         line0_offset = 0;
378                         line1_offset = buf->bpl;
379                 }
380                 cx23885_risc_buffer(dev->pci, &buf->risc,
381                                 sgt->sgl, line0_offset,
382                                 line1_offset,
383                                 buf->bpl, buf->bpl,
384                                 dev->height >> 1);
385                 break;
386         case V4L2_FIELD_SEQ_TB:
387                 cx23885_risc_buffer(dev->pci, &buf->risc,
388                                 sgt->sgl,
389                                 0, buf->bpl * (dev->height >> 1),
390                                 buf->bpl, 0,
391                                 dev->height >> 1);
392                 break;
393         case V4L2_FIELD_SEQ_BT:
394                 cx23885_risc_buffer(dev->pci, &buf->risc,
395                                 sgt->sgl,
396                                 buf->bpl * (dev->height >> 1), 0,
397                                 buf->bpl, 0,
398                                 dev->height >> 1);
399                 break;
400         default:
401                 BUG();
402         }
403         dprintk(2, "[%p/%d] buffer_init - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
404                 buf, buf->vb.v4l2_buf.index,
405                 dev->width, dev->height, dev->fmt->depth, dev->fmt->name,
406                 (unsigned long)buf->risc.dma);
407         return 0;
408 }
409
410 static void buffer_finish(struct vb2_buffer *vb)
411 {
412         struct cx23885_buffer *buf = container_of(vb,
413                 struct cx23885_buffer, vb);
414
415         cx23885_free_buffer(vb->vb2_queue->drv_priv, buf);
416 }
417
418 /*
419  * The risc program for each buffer works as follows: it starts with a simple
420  * 'JUMP to addr + 12', which is effectively a NOP. Then the code to DMA the
421  * buffer follows and at the end we have a JUMP back to the start + 12 (skipping
422  * the initial JUMP).
423  *
424  * This is the risc program of the first buffer to be queued if the active list
425  * is empty and it just keeps DMAing this buffer without generating any
426  * interrupts.
427  *
428  * If a new buffer is added then the initial JUMP in the code for that buffer
429  * will generate an interrupt which signals that the previous buffer has been
430  * DMAed successfully and that it can be returned to userspace.
431  *
432  * It also sets the final jump of the previous buffer to the start of the new
433  * buffer, thus chaining the new buffer into the DMA chain. This is a single
434  * atomic u32 write, so there is no race condition.
435  *
436  * The end-result of all this that you only get an interrupt when a buffer
437  * is ready, so the control flow is very easy.
438  */
439 static void buffer_queue(struct vb2_buffer *vb)
440 {
441         struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
442         struct cx23885_buffer   *buf = container_of(vb,
443                 struct cx23885_buffer, vb);
444         struct cx23885_buffer   *prev;
445         struct cx23885_dmaqueue *q    = &dev->vidq;
446         unsigned long flags;
447
448         /* add jump to start */
449         buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
450         buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
451         buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
452         buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
453
454         spin_lock_irqsave(&dev->slock, flags);
455         if (list_empty(&q->active)) {
456                 list_add_tail(&buf->queue, &q->active);
457                 dprintk(2, "[%p/%d] buffer_queue - first active\n",
458                         buf, buf->vb.v4l2_buf.index);
459         } else {
460                 buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
461                 prev = list_entry(q->active.prev, struct cx23885_buffer,
462                         queue);
463                 list_add_tail(&buf->queue, &q->active);
464                 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
465                 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
466                                 buf, buf->vb.v4l2_buf.index);
467         }
468         spin_unlock_irqrestore(&dev->slock, flags);
469 }
470
471 static int cx23885_start_streaming(struct vb2_queue *q, unsigned int count)
472 {
473         struct cx23885_dev *dev = q->drv_priv;
474         struct cx23885_dmaqueue *dmaq = &dev->vidq;
475         struct cx23885_buffer *buf = list_entry(dmaq->active.next,
476                         struct cx23885_buffer, queue);
477
478         cx23885_start_video_dma(dev, dmaq, buf);
479         return 0;
480 }
481
482 static void cx23885_stop_streaming(struct vb2_queue *q)
483 {
484         struct cx23885_dev *dev = q->drv_priv;
485         struct cx23885_dmaqueue *dmaq = &dev->vidq;
486         unsigned long flags;
487
488         cx_clear(VID_A_DMA_CTL, 0x11);
489         spin_lock_irqsave(&dev->slock, flags);
490         while (!list_empty(&dmaq->active)) {
491                 struct cx23885_buffer *buf = list_entry(dmaq->active.next,
492                         struct cx23885_buffer, queue);
493
494                 list_del(&buf->queue);
495                 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
496         }
497         spin_unlock_irqrestore(&dev->slock, flags);
498 }
499
500 static struct vb2_ops cx23885_video_qops = {
501         .queue_setup    = queue_setup,
502         .buf_prepare  = buffer_prepare,
503         .buf_finish = buffer_finish,
504         .buf_queue    = buffer_queue,
505         .wait_prepare = vb2_ops_wait_prepare,
506         .wait_finish = vb2_ops_wait_finish,
507         .start_streaming = cx23885_start_streaming,
508         .stop_streaming = cx23885_stop_streaming,
509 };
510
511 /* ------------------------------------------------------------------ */
512 /* VIDEO IOCTLS                                                       */
513
514 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
515         struct v4l2_format *f)
516 {
517         struct cx23885_dev *dev = video_drvdata(file);
518
519         f->fmt.pix.width        = dev->width;
520         f->fmt.pix.height       = dev->height;
521         f->fmt.pix.field        = dev->field;
522         f->fmt.pix.pixelformat  = dev->fmt->fourcc;
523         f->fmt.pix.bytesperline =
524                 (f->fmt.pix.width * dev->fmt->depth) >> 3;
525         f->fmt.pix.sizeimage =
526                 f->fmt.pix.height * f->fmt.pix.bytesperline;
527         f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
528
529         return 0;
530 }
531
532 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
533         struct v4l2_format *f)
534 {
535         struct cx23885_dev *dev = video_drvdata(file);
536         struct cx23885_fmt *fmt;
537         enum v4l2_field   field;
538         unsigned int      maxw, maxh;
539
540         fmt = format_by_fourcc(f->fmt.pix.pixelformat);
541         if (NULL == fmt)
542                 return -EINVAL;
543
544         field = f->fmt.pix.field;
545         maxw  = norm_maxw(dev->tvnorm);
546         maxh  = norm_maxh(dev->tvnorm);
547
548         if (V4L2_FIELD_ANY == field) {
549                 field = (f->fmt.pix.height > maxh/2)
550                         ? V4L2_FIELD_INTERLACED
551                         : V4L2_FIELD_BOTTOM;
552         }
553
554         switch (field) {
555         case V4L2_FIELD_TOP:
556         case V4L2_FIELD_BOTTOM:
557                 maxh = maxh / 2;
558                 break;
559         case V4L2_FIELD_INTERLACED:
560         case V4L2_FIELD_SEQ_TB:
561         case V4L2_FIELD_SEQ_BT:
562                 break;
563         default:
564                 field = V4L2_FIELD_INTERLACED;
565                 break;
566         }
567
568         f->fmt.pix.field = field;
569         v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
570                               &f->fmt.pix.height, 32, maxh, 0, 0);
571         f->fmt.pix.bytesperline =
572                 (f->fmt.pix.width * fmt->depth) >> 3;
573         f->fmt.pix.sizeimage =
574                 f->fmt.pix.height * f->fmt.pix.bytesperline;
575         f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
576
577         return 0;
578 }
579
580 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
581         struct v4l2_format *f)
582 {
583         struct cx23885_dev *dev = video_drvdata(file);
584         struct v4l2_subdev_format format = {
585                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
586         };
587         int err;
588
589         dprintk(2, "%s()\n", __func__);
590         err = vidioc_try_fmt_vid_cap(file, priv, f);
591
592         if (0 != err)
593                 return err;
594
595         if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
596             vb2_is_busy(&dev->vb2_mpegq))
597                 return -EBUSY;
598
599         dev->fmt        = format_by_fourcc(f->fmt.pix.pixelformat);
600         dev->width      = f->fmt.pix.width;
601         dev->height     = f->fmt.pix.height;
602         dev->field      = f->fmt.pix.field;
603         dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
604                 dev->width, dev->height, dev->field);
605         v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
606         call_all(dev, pad, set_fmt, NULL, &format);
607         v4l2_fill_pix_format(&f->fmt.pix, &format.format);
608         /* set_fmt overwrites f->fmt.pix.field, restore it */
609         f->fmt.pix.field = dev->field;
610         return 0;
611 }
612
613 static int vidioc_querycap(struct file *file, void  *priv,
614         struct v4l2_capability *cap)
615 {
616         struct cx23885_dev *dev = video_drvdata(file);
617         struct video_device *vdev = video_devdata(file);
618
619         strcpy(cap->driver, "cx23885");
620         strlcpy(cap->card, cx23885_boards[dev->board].name,
621                 sizeof(cap->card));
622         sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
623         cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING | V4L2_CAP_AUDIO;
624         if (dev->tuner_type != TUNER_ABSENT)
625                 cap->device_caps |= V4L2_CAP_TUNER;
626         if (vdev->vfl_type == VFL_TYPE_VBI)
627                 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
628         else
629                 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
630         cap->capabilities = cap->device_caps | V4L2_CAP_VBI_CAPTURE |
631                 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_DEVICE_CAPS;
632         return 0;
633 }
634
635 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
636         struct v4l2_fmtdesc *f)
637 {
638         if (unlikely(f->index >= ARRAY_SIZE(formats)))
639                 return -EINVAL;
640
641         strlcpy(f->description, formats[f->index].name,
642                 sizeof(f->description));
643         f->pixelformat = formats[f->index].fourcc;
644
645         return 0;
646 }
647
648 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
649 {
650         struct cx23885_dev *dev = video_drvdata(file);
651         dprintk(1, "%s()\n", __func__);
652
653         *id = dev->tvnorm;
654         return 0;
655 }
656
657 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
658 {
659         struct cx23885_dev *dev = video_drvdata(file);
660         dprintk(1, "%s()\n", __func__);
661
662         return cx23885_set_tvnorm(dev, tvnorms);
663 }
664
665 int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
666 {
667         static const char *iname[] = {
668                 [CX23885_VMUX_COMPOSITE1] = "Composite1",
669                 [CX23885_VMUX_COMPOSITE2] = "Composite2",
670                 [CX23885_VMUX_COMPOSITE3] = "Composite3",
671                 [CX23885_VMUX_COMPOSITE4] = "Composite4",
672                 [CX23885_VMUX_SVIDEO]     = "S-Video",
673                 [CX23885_VMUX_COMPONENT]  = "Component",
674                 [CX23885_VMUX_TELEVISION] = "Television",
675                 [CX23885_VMUX_CABLE]      = "Cable TV",
676                 [CX23885_VMUX_DVB]        = "DVB",
677                 [CX23885_VMUX_DEBUG]      = "for debug only",
678         };
679         unsigned int n;
680         dprintk(1, "%s()\n", __func__);
681
682         n = i->index;
683         if (n >= MAX_CX23885_INPUT)
684                 return -EINVAL;
685
686         if (0 == INPUT(n)->type)
687                 return -EINVAL;
688
689         i->index = n;
690         i->type  = V4L2_INPUT_TYPE_CAMERA;
691         strcpy(i->name, iname[INPUT(n)->type]);
692         i->std = CX23885_NORMS;
693         if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
694                 (CX23885_VMUX_CABLE == INPUT(n)->type)) {
695                 i->type = V4L2_INPUT_TYPE_TUNER;
696                 i->audioset = 4;
697         } else {
698                 /* Two selectable audio inputs for non-tv inputs */
699                 i->audioset = 3;
700         }
701
702         if (dev->input == n) {
703                 /* enum'd input matches our configured input.
704                  * Ask the video decoder to process the call
705                  * and give it an oppertunity to update the
706                  * status field.
707                  */
708                 call_all(dev, video, g_input_status, &i->status);
709         }
710
711         return 0;
712 }
713
714 static int vidioc_enum_input(struct file *file, void *priv,
715                                 struct v4l2_input *i)
716 {
717         struct cx23885_dev *dev = video_drvdata(file);
718         dprintk(1, "%s()\n", __func__);
719         return cx23885_enum_input(dev, i);
720 }
721
722 int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
723 {
724         struct cx23885_dev *dev = video_drvdata(file);
725
726         *i = dev->input;
727         dprintk(1, "%s() returns %d\n", __func__, *i);
728         return 0;
729 }
730
731 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
732 {
733         return cx23885_get_input(file, priv, i);
734 }
735
736 int cx23885_set_input(struct file *file, void *priv, unsigned int i)
737 {
738         struct cx23885_dev *dev = video_drvdata(file);
739
740         dprintk(1, "%s(%d)\n", __func__, i);
741
742         if (i >= MAX_CX23885_INPUT) {
743                 dprintk(1, "%s() -EINVAL\n", __func__);
744                 return -EINVAL;
745         }
746
747         if (INPUT(i)->type == 0)
748                 return -EINVAL;
749
750         cx23885_video_mux(dev, i);
751
752         /* By default establish the default audio input for the card also */
753         /* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
754         cx23885_audio_mux(dev, i);
755         return 0;
756 }
757
758 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
759 {
760         return cx23885_set_input(file, priv, i);
761 }
762
763 static int vidioc_log_status(struct file *file, void *priv)
764 {
765         struct cx23885_dev *dev = video_drvdata(file);
766
767         call_all(dev, core, log_status);
768         return 0;
769 }
770
771 static int cx23885_query_audinput(struct file *file, void *priv,
772         struct v4l2_audio *i)
773 {
774         struct cx23885_dev *dev = video_drvdata(file);
775         static const char *iname[] = {
776                 [0] = "Baseband L/R 1",
777                 [1] = "Baseband L/R 2",
778                 [2] = "TV",
779         };
780         unsigned int n;
781         dprintk(1, "%s()\n", __func__);
782
783         n = i->index;
784         if (n >= 3)
785                 return -EINVAL;
786
787         memset(i, 0, sizeof(*i));
788         i->index = n;
789         strcpy(i->name, iname[n]);
790         i->capability = V4L2_AUDCAP_STEREO;
791         return 0;
792
793 }
794
795 static int vidioc_enum_audinput(struct file *file, void *priv,
796                                 struct v4l2_audio *i)
797 {
798         return cx23885_query_audinput(file, priv, i);
799 }
800
801 static int vidioc_g_audinput(struct file *file, void *priv,
802         struct v4l2_audio *i)
803 {
804         struct cx23885_dev *dev = video_drvdata(file);
805
806         if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
807                 (CX23885_VMUX_CABLE == INPUT(dev->input)->type))
808                 i->index = 2;
809         else
810                 i->index = dev->audinput;
811         dprintk(1, "%s(input=%d)\n", __func__, i->index);
812
813         return cx23885_query_audinput(file, priv, i);
814 }
815
816 static int vidioc_s_audinput(struct file *file, void *priv,
817         const struct v4l2_audio *i)
818 {
819         struct cx23885_dev *dev = video_drvdata(file);
820
821         if ((CX23885_VMUX_TELEVISION == INPUT(dev->input)->type) ||
822                 (CX23885_VMUX_CABLE == INPUT(dev->input)->type)) {
823                 return i->index != 2 ? -EINVAL : 0;
824         }
825         if (i->index > 1)
826                 return -EINVAL;
827
828         dprintk(1, "%s(%d)\n", __func__, i->index);
829
830         dev->audinput = i->index;
831
832         /* Skip the audio defaults from the cards struct, caller wants
833          * directly touch the audio mux hardware. */
834         cx23885_flatiron_mux(dev, dev->audinput + 1);
835         return 0;
836 }
837
838 static int vidioc_g_tuner(struct file *file, void *priv,
839                                 struct v4l2_tuner *t)
840 {
841         struct cx23885_dev *dev = video_drvdata(file);
842
843         if (dev->tuner_type == TUNER_ABSENT)
844                 return -EINVAL;
845         if (0 != t->index)
846                 return -EINVAL;
847
848         strcpy(t->name, "Television");
849
850         call_all(dev, tuner, g_tuner, t);
851         return 0;
852 }
853
854 static int vidioc_s_tuner(struct file *file, void *priv,
855                                 const struct v4l2_tuner *t)
856 {
857         struct cx23885_dev *dev = video_drvdata(file);
858
859         if (dev->tuner_type == TUNER_ABSENT)
860                 return -EINVAL;
861         if (0 != t->index)
862                 return -EINVAL;
863         /* Update the A/V core */
864         call_all(dev, tuner, s_tuner, t);
865
866         return 0;
867 }
868
869 static int vidioc_g_frequency(struct file *file, void *priv,
870                                 struct v4l2_frequency *f)
871 {
872         struct cx23885_dev *dev = video_drvdata(file);
873
874         if (dev->tuner_type == TUNER_ABSENT)
875                 return -EINVAL;
876
877         f->type = V4L2_TUNER_ANALOG_TV;
878         f->frequency = dev->freq;
879
880         call_all(dev, tuner, g_frequency, f);
881
882         return 0;
883 }
884
885 static int cx23885_set_freq(struct cx23885_dev *dev, const struct v4l2_frequency *f)
886 {
887         struct v4l2_ctrl *mute;
888         int old_mute_val = 1;
889
890         if (dev->tuner_type == TUNER_ABSENT)
891                 return -EINVAL;
892         if (unlikely(f->tuner != 0))
893                 return -EINVAL;
894
895         dev->freq = f->frequency;
896
897         /* I need to mute audio here */
898         mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
899         if (mute) {
900                 old_mute_val = v4l2_ctrl_g_ctrl(mute);
901                 if (!old_mute_val)
902                         v4l2_ctrl_s_ctrl(mute, 1);
903         }
904
905         call_all(dev, tuner, s_frequency, f);
906
907         /* When changing channels it is required to reset TVAUDIO */
908         msleep(100);
909
910         /* I need to unmute audio here */
911         if (old_mute_val == 0)
912                 v4l2_ctrl_s_ctrl(mute, old_mute_val);
913
914         return 0;
915 }
916
917 static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
918         const struct v4l2_frequency *f)
919 {
920         struct v4l2_ctrl *mute;
921         int old_mute_val = 1;
922         struct vb2_dvb_frontend *vfe;
923         struct dvb_frontend *fe;
924
925         struct analog_parameters params = {
926                 .mode      = V4L2_TUNER_ANALOG_TV,
927                 .audmode   = V4L2_TUNER_MODE_STEREO,
928                 .std       = dev->tvnorm,
929                 .frequency = f->frequency
930         };
931
932         dev->freq = f->frequency;
933
934         /* I need to mute audio here */
935         mute = v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_AUDIO_MUTE);
936         if (mute) {
937                 old_mute_val = v4l2_ctrl_g_ctrl(mute);
938                 if (!old_mute_val)
939                         v4l2_ctrl_s_ctrl(mute, 1);
940         }
941
942         /* If HVR1850 */
943         dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
944                 params.frequency, f->tuner, params.std);
945
946         vfe = vb2_dvb_get_frontend(&dev->ts2.frontends, 1);
947         if (!vfe) {
948                 return -EINVAL;
949         }
950
951         fe = vfe->dvb.frontend;
952
953         if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
954             (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
955             (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111))
956                 fe = &dev->ts1.analog_fe;
957
958         if (fe && fe->ops.tuner_ops.set_analog_params) {
959                 call_all(dev, video, s_std, dev->tvnorm);
960                 fe->ops.tuner_ops.set_analog_params(fe, &params);
961         }
962         else
963                 printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
964
965         /* When changing channels it is required to reset TVAUDIO */
966         msleep(100);
967
968         /* I need to unmute audio here */
969         if (old_mute_val == 0)
970                 v4l2_ctrl_s_ctrl(mute, old_mute_val);
971
972         return 0;
973 }
974
975 int cx23885_set_frequency(struct file *file, void *priv,
976         const struct v4l2_frequency *f)
977 {
978         struct cx23885_dev *dev = video_drvdata(file);
979         int ret;
980
981         switch (dev->board) {
982         case CX23885_BOARD_HAUPPAUGE_HVR1255:
983         case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
984         case CX23885_BOARD_HAUPPAUGE_HVR1850:
985                 ret = cx23885_set_freq_via_ops(dev, f);
986                 break;
987         default:
988                 ret = cx23885_set_freq(dev, f);
989         }
990
991         return ret;
992 }
993
994 static int vidioc_s_frequency(struct file *file, void *priv,
995         const struct v4l2_frequency *f)
996 {
997         return cx23885_set_frequency(file, priv, f);
998 }
999
1000 /* ----------------------------------------------------------- */
1001
1002 int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1003 {
1004         u32 mask, count;
1005         int handled = 0;
1006
1007         mask   = cx_read(VID_A_INT_MSK);
1008         if (0 == (status & mask))
1009                 return handled;
1010
1011         cx_write(VID_A_INT_STAT, status);
1012
1013         /* risc op code error, fifo overflow or line sync detection error */
1014         if ((status & VID_BC_MSK_OPC_ERR) ||
1015                 (status & VID_BC_MSK_SYNC) ||
1016                 (status & VID_BC_MSK_OF)) {
1017
1018                 if (status & VID_BC_MSK_OPC_ERR) {
1019                         dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1020                                 VID_BC_MSK_OPC_ERR);
1021                         printk(KERN_WARNING "%s: video risc op code error\n",
1022                                 dev->name);
1023                         cx23885_sram_channel_dump(dev,
1024                                 &dev->sram_channels[SRAM_CH01]);
1025                 }
1026
1027                 if (status & VID_BC_MSK_SYNC)
1028                         dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) "
1029                                 "video lines miss-match\n",
1030                                 VID_BC_MSK_SYNC);
1031
1032                 if (status & VID_BC_MSK_OF)
1033                         dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
1034                                 VID_BC_MSK_OF);
1035
1036         }
1037
1038         /* Video */
1039         if (status & VID_BC_MSK_RISCI1) {
1040                 spin_lock(&dev->slock);
1041                 count = cx_read(VID_A_GPCNT);
1042                 cx23885_video_wakeup(dev, &dev->vidq, count);
1043                 spin_unlock(&dev->slock);
1044                 handled++;
1045         }
1046
1047         /* Allow the VBI framework to process it's payload */
1048         handled += cx23885_vbi_irq(dev, status);
1049
1050         return handled;
1051 }
1052
1053 /* ----------------------------------------------------------- */
1054 /* exported stuff                                              */
1055
1056 static const struct v4l2_file_operations video_fops = {
1057         .owner         = THIS_MODULE,
1058         .open           = v4l2_fh_open,
1059         .release        = vb2_fop_release,
1060         .read           = vb2_fop_read,
1061         .poll           = vb2_fop_poll,
1062         .unlocked_ioctl = video_ioctl2,
1063         .mmap           = vb2_fop_mmap,
1064 };
1065
1066 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1067         .vidioc_querycap      = vidioc_querycap,
1068         .vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1069         .vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1070         .vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1071         .vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1072         .vidioc_g_fmt_vbi_cap     = cx23885_vbi_fmt,
1073         .vidioc_try_fmt_vbi_cap   = cx23885_vbi_fmt,
1074         .vidioc_s_fmt_vbi_cap     = cx23885_vbi_fmt,
1075         .vidioc_reqbufs       = vb2_ioctl_reqbufs,
1076         .vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
1077         .vidioc_querybuf      = vb2_ioctl_querybuf,
1078         .vidioc_qbuf          = vb2_ioctl_qbuf,
1079         .vidioc_dqbuf         = vb2_ioctl_dqbuf,
1080         .vidioc_streamon      = vb2_ioctl_streamon,
1081         .vidioc_streamoff     = vb2_ioctl_streamoff,
1082         .vidioc_s_std         = vidioc_s_std,
1083         .vidioc_g_std         = vidioc_g_std,
1084         .vidioc_enum_input    = vidioc_enum_input,
1085         .vidioc_g_input       = vidioc_g_input,
1086         .vidioc_s_input       = vidioc_s_input,
1087         .vidioc_log_status    = vidioc_log_status,
1088         .vidioc_g_tuner       = vidioc_g_tuner,
1089         .vidioc_s_tuner       = vidioc_s_tuner,
1090         .vidioc_g_frequency   = vidioc_g_frequency,
1091         .vidioc_s_frequency   = vidioc_s_frequency,
1092 #ifdef CONFIG_VIDEO_ADV_DEBUG
1093         .vidioc_g_chip_info   = cx23885_g_chip_info,
1094         .vidioc_g_register    = cx23885_g_register,
1095         .vidioc_s_register    = cx23885_s_register,
1096 #endif
1097         .vidioc_enumaudio     = vidioc_enum_audinput,
1098         .vidioc_g_audio       = vidioc_g_audinput,
1099         .vidioc_s_audio       = vidioc_s_audinput,
1100         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1101         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1102 };
1103
1104 static struct video_device cx23885_vbi_template;
1105 static struct video_device cx23885_video_template = {
1106         .name                 = "cx23885-video",
1107         .fops                 = &video_fops,
1108         .ioctl_ops            = &video_ioctl_ops,
1109         .tvnorms              = CX23885_NORMS,
1110 };
1111
1112 void cx23885_video_unregister(struct cx23885_dev *dev)
1113 {
1114         dprintk(1, "%s()\n", __func__);
1115         cx23885_irq_remove(dev, 0x01);
1116
1117         if (dev->vbi_dev) {
1118                 if (video_is_registered(dev->vbi_dev))
1119                         video_unregister_device(dev->vbi_dev);
1120                 else
1121                         video_device_release(dev->vbi_dev);
1122                 dev->vbi_dev = NULL;
1123         }
1124         if (dev->video_dev) {
1125                 if (video_is_registered(dev->video_dev))
1126                         video_unregister_device(dev->video_dev);
1127                 else
1128                         video_device_release(dev->video_dev);
1129                 dev->video_dev = NULL;
1130         }
1131
1132         if (dev->audio_dev)
1133                 cx23885_audio_unregister(dev);
1134 }
1135
1136 int cx23885_video_register(struct cx23885_dev *dev)
1137 {
1138         struct vb2_queue *q;
1139         int err;
1140
1141         dprintk(1, "%s()\n", __func__);
1142
1143         /* Initialize VBI template */
1144         cx23885_vbi_template = cx23885_video_template;
1145         strcpy(cx23885_vbi_template.name, "cx23885-vbi");
1146
1147         dev->tvnorm = V4L2_STD_NTSC_M;
1148         dev->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
1149         dev->field = V4L2_FIELD_INTERLACED;
1150         dev->width = 720;
1151         dev->height = norm_maxh(dev->tvnorm);
1152
1153         /* init video dma queues */
1154         INIT_LIST_HEAD(&dev->vidq.active);
1155
1156         /* init vbi dma queues */
1157         INIT_LIST_HEAD(&dev->vbiq.active);
1158
1159         cx23885_irq_add_enable(dev, 0x01);
1160
1161         if ((TUNER_ABSENT != dev->tuner_type) &&
1162                         ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
1163                 struct v4l2_subdev *sd = NULL;
1164
1165                 if (dev->tuner_addr)
1166                         sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1167                                 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
1168                                 "tuner", dev->tuner_addr, NULL);
1169                 else
1170                         sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1171                                 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
1172                                 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
1173                 if (sd) {
1174                         struct tuner_setup tun_setup;
1175
1176                         memset(&tun_setup, 0, sizeof(tun_setup));
1177                         tun_setup.mode_mask = T_ANALOG_TV;
1178                         tun_setup.type = dev->tuner_type;
1179                         tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1180                         tun_setup.tuner_callback = cx23885_tuner_callback;
1181
1182                         v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1183
1184                         if ((dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) ||
1185                             (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXPVR2200)) {
1186                                 struct xc2028_ctrl ctrl = {
1187                                         .fname = XC2028_DEFAULT_FIRMWARE,
1188                                         .max_len = 64
1189                                 };
1190                                 struct v4l2_priv_tun_config cfg = {
1191                                         .tuner = dev->tuner_type,
1192                                         .priv = &ctrl
1193                                 };
1194                                 v4l2_subdev_call(sd, tuner, s_config, &cfg);
1195                         }
1196
1197                         if (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) {
1198                                 struct xc2028_ctrl ctrl = {
1199                                         .fname = "xc3028L-v36.fw",
1200                                         .max_len = 64
1201                                 };
1202                                 struct v4l2_priv_tun_config cfg = {
1203                                         .tuner = dev->tuner_type,
1204                                         .priv = &ctrl
1205                                 };
1206                                 v4l2_subdev_call(sd, tuner, s_config, &cfg);
1207                         }
1208                 }
1209         }
1210
1211         /* initial device configuration */
1212         mutex_lock(&dev->lock);
1213         cx23885_set_tvnorm(dev, dev->tvnorm);
1214         cx23885_video_mux(dev, 0);
1215         cx23885_audio_mux(dev, 0);
1216         mutex_unlock(&dev->lock);
1217
1218         q = &dev->vb2_vidq;
1219         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1220         q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1221         q->gfp_flags = GFP_DMA32;
1222         q->min_buffers_needed = 2;
1223         q->drv_priv = dev;
1224         q->buf_struct_size = sizeof(struct cx23885_buffer);
1225         q->ops = &cx23885_video_qops;
1226         q->mem_ops = &vb2_dma_sg_memops;
1227         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1228         q->lock = &dev->lock;
1229
1230         err = vb2_queue_init(q);
1231         if (err < 0)
1232                 goto fail_unreg;
1233
1234         q = &dev->vb2_vbiq;
1235         q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1236         q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1237         q->gfp_flags = GFP_DMA32;
1238         q->min_buffers_needed = 2;
1239         q->drv_priv = dev;
1240         q->buf_struct_size = sizeof(struct cx23885_buffer);
1241         q->ops = &cx23885_vbi_qops;
1242         q->mem_ops = &vb2_dma_sg_memops;
1243         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1244         q->lock = &dev->lock;
1245
1246         err = vb2_queue_init(q);
1247         if (err < 0)
1248                 goto fail_unreg;
1249
1250         /* register Video device */
1251         dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1252                 &cx23885_video_template, "video");
1253         dev->video_dev->queue = &dev->vb2_vidq;
1254         err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1255                                     video_nr[dev->nr]);
1256         if (err < 0) {
1257                 printk(KERN_INFO "%s: can't register video device\n",
1258                         dev->name);
1259                 goto fail_unreg;
1260         }
1261         printk(KERN_INFO "%s: registered device %s [v4l2]\n",
1262                dev->name, video_device_node_name(dev->video_dev));
1263
1264         /* register VBI device */
1265         dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
1266                 &cx23885_vbi_template, "vbi");
1267         dev->vbi_dev->queue = &dev->vb2_vbiq;
1268         err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1269                                     vbi_nr[dev->nr]);
1270         if (err < 0) {
1271                 printk(KERN_INFO "%s: can't register vbi device\n",
1272                         dev->name);
1273                 goto fail_unreg;
1274         }
1275         printk(KERN_INFO "%s: registered device %s\n",
1276                dev->name, video_device_node_name(dev->vbi_dev));
1277
1278         /* Register ALSA audio device */
1279         dev->audio_dev = cx23885_audio_register(dev);
1280
1281         return 0;
1282
1283 fail_unreg:
1284         cx23885_video_unregister(dev);
1285         return err;
1286 }