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