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