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