ecc580af01481d1cef7431aa201204af5f7fa00a
[sfrench/cifs-2.6.git] / drivers / media / pci / cx23885 / cx23885-video.c
1 /*
2  *  Driver for the Conexant CX23885 PCIe bridge
3  *
4  *  Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *
15  *  GNU General Public License for more details.
16  */
17
18 #include "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_HVR1850) ||
267                 (dev->board == CX23885_BOARD_MYGICA_X8507) ||
268                 (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) ||
269                 (dev->board == CX23885_BOARD_VIEWCAST_260E) ||
270                 (dev->board == CX23885_BOARD_VIEWCAST_460E)) {
271                 /* Configure audio routing */
272                 v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
273                         INPUT(input)->amux, 0, 0);
274
275                 if (INPUT(input)->amux == CX25840_AUDIO7)
276                         cx23885_flatiron_mux(dev, 1);
277                 else if (INPUT(input)->amux == CX25840_AUDIO6)
278                         cx23885_flatiron_mux(dev, 2);
279         }
280
281         return 0;
282 }
283
284 static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
285 {
286         dprintk(1, "%s(input=%d)\n", __func__, input);
287
288         /* The baseband video core of the cx23885 has two audio inputs.
289          * LR1 and LR2. In almost every single case so far only HVR1xxx
290          * cards we've only ever supported LR1. Time to support LR2,
291          * which is available via the optional white breakout header on
292          * the board.
293          * We'll use a could of existing enums in the card struct to allow
294          * devs to specify which baseband input they need, or just default
295          * to what we've always used.
296          */
297         if (INPUT(input)->amux == CX25840_AUDIO7)
298                 cx23885_flatiron_mux(dev, 1);
299         else if (INPUT(input)->amux == CX25840_AUDIO6)
300                 cx23885_flatiron_mux(dev, 2);
301         else {
302                 /* Not specifically defined, assume the default. */
303                 cx23885_flatiron_mux(dev, 1);
304         }
305
306         return 0;
307 }
308
309 /* ------------------------------------------------------------------ */
310 static int cx23885_start_video_dma(struct cx23885_dev *dev,
311                            struct cx23885_dmaqueue *q,
312                            struct cx23885_buffer *buf)
313 {
314         dprintk(1, "%s()\n", __func__);
315
316         /* Stop the dma/fifo before we tamper with it's risc programs */
317         cx_clear(VID_A_DMA_CTL, 0x11);
318
319         /* setup fifo + format */
320         cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
321                                 buf->bpl, buf->risc.dma);
322
323         /* reset counter */
324         cx_write(VID_A_GPCNT_CTL, 3);
325         q->count = 0;
326
327         /* enable irq */
328         cx23885_irq_add_enable(dev, 0x01);
329         cx_set(VID_A_INT_MSK, 0x000011);
330
331         /* start dma */
332         cx_set(DEV_CNTRL2, (1<<5));
333         cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
334
335         return 0;
336 }
337
338 static int queue_setup(struct vb2_queue *q,
339                            unsigned int *num_buffers, unsigned int *num_planes,
340                            unsigned int sizes[], struct device *alloc_devs[])
341 {
342         struct cx23885_dev *dev = q->drv_priv;
343
344         *num_planes = 1;
345         sizes[0] = (dev->fmt->depth * dev->width * dev->height) >> 3;
346         return 0;
347 }
348
349 static int buffer_prepare(struct vb2_buffer *vb)
350 {
351         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
352         struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
353         struct cx23885_buffer *buf =
354                 container_of(vbuf, struct cx23885_buffer, vb);
355         u32 line0_offset, line1_offset;
356         struct sg_table *sgt = vb2_dma_sg_plane_desc(vb, 0);
357         int field_tff;
358
359         buf->bpl = (dev->width * dev->fmt->depth) >> 3;
360
361         if (vb2_plane_size(vb, 0) < dev->height * buf->bpl)
362                 return -EINVAL;
363         vb2_set_plane_payload(vb, 0, dev->height * buf->bpl);
364
365         switch (dev->field) {
366         case V4L2_FIELD_TOP:
367                 cx23885_risc_buffer(dev->pci, &buf->risc,
368                                 sgt->sgl, 0, UNSET,
369                                 buf->bpl, 0, dev->height);
370                 break;
371         case V4L2_FIELD_BOTTOM:
372                 cx23885_risc_buffer(dev->pci, &buf->risc,
373                                 sgt->sgl, UNSET, 0,
374                                 buf->bpl, 0, dev->height);
375                 break;
376         case V4L2_FIELD_INTERLACED:
377                 if (dev->tvnorm & V4L2_STD_525_60)
378                         /* NTSC or  */
379                         field_tff = 1;
380                 else
381                         field_tff = 0;
382
383                 if (cx23885_boards[dev->board].force_bff)
384                         /* PAL / SECAM OR 888 in NTSC MODE */
385                         field_tff = 0;
386
387                 if (field_tff) {
388                         /* cx25840 transmits NTSC bottom field first */
389                         dprintk(1, "%s() Creating TFF/NTSC risc\n",
390                                         __func__);
391                         line0_offset = buf->bpl;
392                         line1_offset = 0;
393                 } else {
394                         /* All other formats are top field first */
395                         dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
396                                         __func__);
397                         line0_offset = 0;
398                         line1_offset = buf->bpl;
399                 }
400                 cx23885_risc_buffer(dev->pci, &buf->risc,
401                                 sgt->sgl, line0_offset,
402                                 line1_offset,
403                                 buf->bpl, buf->bpl,
404                                 dev->height >> 1);
405                 break;
406         case V4L2_FIELD_SEQ_TB:
407                 cx23885_risc_buffer(dev->pci, &buf->risc,
408                                 sgt->sgl,
409                                 0, buf->bpl * (dev->height >> 1),
410                                 buf->bpl, 0,
411                                 dev->height >> 1);
412                 break;
413         case V4L2_FIELD_SEQ_BT:
414                 cx23885_risc_buffer(dev->pci, &buf->risc,
415                                 sgt->sgl,
416                                 buf->bpl * (dev->height >> 1), 0,
417                                 buf->bpl, 0,
418                                 dev->height >> 1);
419                 break;
420         default:
421                 BUG();
422         }
423         dprintk(2, "[%p/%d] buffer_init - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
424                 buf, buf->vb.vb2_buf.index,
425                 dev->width, dev->height, dev->fmt->depth, dev->fmt->name,
426                 (unsigned long)buf->risc.dma);
427         return 0;
428 }
429
430 static void buffer_finish(struct vb2_buffer *vb)
431 {
432         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
433         struct cx23885_buffer *buf = container_of(vbuf,
434                 struct cx23885_buffer, vb);
435
436         cx23885_free_buffer(vb->vb2_queue->drv_priv, buf);
437 }
438
439 /*
440  * The risc program for each buffer works as follows: it starts with a simple
441  * 'JUMP to addr + 12', which is effectively a NOP. Then the code to DMA the
442  * buffer follows and at the end we have a JUMP back to the start + 12 (skipping
443  * the initial JUMP).
444  *
445  * This is the risc program of the first buffer to be queued if the active list
446  * is empty and it just keeps DMAing this buffer without generating any
447  * interrupts.
448  *
449  * If a new buffer is added then the initial JUMP in the code for that buffer
450  * will generate an interrupt which signals that the previous buffer has been
451  * DMAed successfully and that it can be returned to userspace.
452  *
453  * It also sets the final jump of the previous buffer to the start of the new
454  * buffer, thus chaining the new buffer into the DMA chain. This is a single
455  * atomic u32 write, so there is no race condition.
456  *
457  * The end-result of all this that you only get an interrupt when a buffer
458  * is ready, so the control flow is very easy.
459  */
460 static void buffer_queue(struct vb2_buffer *vb)
461 {
462         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
463         struct cx23885_dev *dev = vb->vb2_queue->drv_priv;
464         struct cx23885_buffer   *buf = container_of(vbuf,
465                 struct cx23885_buffer, vb);
466         struct cx23885_buffer   *prev;
467         struct cx23885_dmaqueue *q    = &dev->vidq;
468         unsigned long flags;
469
470         /* add jump to start */
471         buf->risc.cpu[1] = cpu_to_le32(buf->risc.dma + 12);
472         buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_CNT_INC);
473         buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma + 12);
474         buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
475
476         spin_lock_irqsave(&dev->slock, flags);
477         if (list_empty(&q->active)) {
478                 list_add_tail(&buf->queue, &q->active);
479                 dprintk(2, "[%p/%d] buffer_queue - first active\n",
480                         buf, buf->vb.vb2_buf.index);
481         } else {
482                 buf->risc.cpu[0] |= cpu_to_le32(RISC_IRQ1);
483                 prev = list_entry(q->active.prev, struct cx23885_buffer,
484                         queue);
485                 list_add_tail(&buf->queue, &q->active);
486                 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
487                 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
488                                 buf, buf->vb.vb2_buf.index);
489         }
490         spin_unlock_irqrestore(&dev->slock, flags);
491 }
492
493 static int cx23885_start_streaming(struct vb2_queue *q, unsigned int count)
494 {
495         struct cx23885_dev *dev = q->drv_priv;
496         struct cx23885_dmaqueue *dmaq = &dev->vidq;
497         struct cx23885_buffer *buf = list_entry(dmaq->active.next,
498                         struct cx23885_buffer, queue);
499
500         cx23885_start_video_dma(dev, dmaq, buf);
501         return 0;
502 }
503
504 static void cx23885_stop_streaming(struct vb2_queue *q)
505 {
506         struct cx23885_dev *dev = q->drv_priv;
507         struct cx23885_dmaqueue *dmaq = &dev->vidq;
508         unsigned long flags;
509
510         cx_clear(VID_A_DMA_CTL, 0x11);
511         spin_lock_irqsave(&dev->slock, flags);
512         while (!list_empty(&dmaq->active)) {
513                 struct cx23885_buffer *buf = list_entry(dmaq->active.next,
514                         struct cx23885_buffer, queue);
515
516                 list_del(&buf->queue);
517                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
518         }
519         spin_unlock_irqrestore(&dev->slock, flags);
520 }
521
522 static const struct vb2_ops cx23885_video_qops = {
523         .queue_setup    = queue_setup,
524         .buf_prepare  = buffer_prepare,
525         .buf_finish = buffer_finish,
526         .buf_queue    = buffer_queue,
527         .wait_prepare = vb2_ops_wait_prepare,
528         .wait_finish = vb2_ops_wait_finish,
529         .start_streaming = cx23885_start_streaming,
530         .stop_streaming = cx23885_stop_streaming,
531 };
532
533 /* ------------------------------------------------------------------ */
534 /* VIDEO IOCTLS                                                       */
535
536 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
537         struct v4l2_format *f)
538 {
539         struct cx23885_dev *dev = video_drvdata(file);
540
541         f->fmt.pix.width        = dev->width;
542         f->fmt.pix.height       = dev->height;
543         f->fmt.pix.field        = dev->field;
544         f->fmt.pix.pixelformat  = dev->fmt->fourcc;
545         f->fmt.pix.bytesperline =
546                 (f->fmt.pix.width * dev->fmt->depth) >> 3;
547         f->fmt.pix.sizeimage =
548                 f->fmt.pix.height * f->fmt.pix.bytesperline;
549         f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
550
551         return 0;
552 }
553
554 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
555         struct v4l2_format *f)
556 {
557         struct cx23885_dev *dev = video_drvdata(file);
558         struct cx23885_fmt *fmt;
559         enum v4l2_field   field;
560         unsigned int      maxw, maxh;
561
562         fmt = format_by_fourcc(f->fmt.pix.pixelformat);
563         if (NULL == fmt)
564                 return -EINVAL;
565
566         field = f->fmt.pix.field;
567         maxw  = 720;
568         maxh  = norm_maxh(dev->tvnorm);
569
570         if (V4L2_FIELD_ANY == field) {
571                 field = (f->fmt.pix.height > maxh/2)
572                         ? V4L2_FIELD_INTERLACED
573                         : V4L2_FIELD_BOTTOM;
574         }
575
576         switch (field) {
577         case V4L2_FIELD_TOP:
578         case V4L2_FIELD_BOTTOM:
579                 maxh = maxh / 2;
580                 break;
581         case V4L2_FIELD_INTERLACED:
582         case V4L2_FIELD_SEQ_TB:
583         case V4L2_FIELD_SEQ_BT:
584                 break;
585         default:
586                 field = V4L2_FIELD_INTERLACED;
587                 break;
588         }
589
590         f->fmt.pix.field = field;
591         v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
592                               &f->fmt.pix.height, 32, maxh, 0, 0);
593         f->fmt.pix.bytesperline =
594                 (f->fmt.pix.width * fmt->depth) >> 3;
595         f->fmt.pix.sizeimage =
596                 f->fmt.pix.height * f->fmt.pix.bytesperline;
597         f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
598
599         return 0;
600 }
601
602 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
603         struct v4l2_format *f)
604 {
605         struct cx23885_dev *dev = video_drvdata(file);
606         struct v4l2_subdev_format format = {
607                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
608         };
609         int err;
610
611         dprintk(2, "%s()\n", __func__);
612         err = vidioc_try_fmt_vid_cap(file, priv, f);
613
614         if (0 != err)
615                 return err;
616
617         if (vb2_is_busy(&dev->vb2_vidq) || vb2_is_busy(&dev->vb2_vbiq) ||
618             vb2_is_busy(&dev->vb2_mpegq))
619                 return -EBUSY;
620
621         dev->fmt        = format_by_fourcc(f->fmt.pix.pixelformat);
622         dev->width      = f->fmt.pix.width;
623         dev->height     = f->fmt.pix.height;
624         dev->field      = f->fmt.pix.field;
625         dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
626                 dev->width, dev->height, dev->field);
627         v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
628         call_all(dev, pad, set_fmt, NULL, &format);
629         v4l2_fill_pix_format(&f->fmt.pix, &format.format);
630         /* set_fmt overwrites f->fmt.pix.field, restore it */
631         f->fmt.pix.field = dev->field;
632         return 0;
633 }
634
635 static int vidioc_querycap(struct file *file, void  *priv,
636         struct v4l2_capability *cap)
637 {
638         struct cx23885_dev *dev = video_drvdata(file);
639         struct video_device *vdev = video_devdata(file);
640
641         strcpy(cap->driver, "cx23885");
642         strlcpy(cap->card, cx23885_boards[dev->board].name,
643                 sizeof(cap->card));
644         sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
645         cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING | V4L2_CAP_AUDIO;
646         if (dev->tuner_type != TUNER_ABSENT)
647                 cap->device_caps |= V4L2_CAP_TUNER;
648         if (vdev->vfl_type == VFL_TYPE_VBI)
649                 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
650         else
651                 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
652         cap->capabilities = cap->device_caps | V4L2_CAP_VBI_CAPTURE |
653                 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_DEVICE_CAPS;
654         return 0;
655 }
656
657 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
658         struct v4l2_fmtdesc *f)
659 {
660         if (unlikely(f->index >= ARRAY_SIZE(formats)))
661                 return -EINVAL;
662
663         strlcpy(f->description, formats[f->index].name,
664                 sizeof(f->description));
665         f->pixelformat = formats[f->index].fourcc;
666
667         return 0;
668 }
669
670 static int vidioc_cropcap(struct file *file, void *priv,
671                           struct v4l2_cropcap *cc)
672 {
673         struct cx23885_dev *dev = video_drvdata(file);
674         bool is_50hz = dev->tvnorm & V4L2_STD_625_50;
675
676         if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
677                 return -EINVAL;
678
679         cc->bounds.left = 0;
680         cc->bounds.top = 0;
681         cc->bounds.width = 720;
682         cc->bounds.height = norm_maxh(dev->tvnorm);
683         cc->defrect = cc->bounds;
684         cc->pixelaspect.numerator = is_50hz ? 54 : 11;
685         cc->pixelaspect.denominator = is_50hz ? 59 : 10;
686
687         return 0;
688 }
689
690 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
691 {
692         struct cx23885_dev *dev = video_drvdata(file);
693         dprintk(1, "%s()\n", __func__);
694
695         *id = dev->tvnorm;
696         return 0;
697 }
698
699 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
700 {
701         struct cx23885_dev *dev = video_drvdata(file);
702         dprintk(1, "%s()\n", __func__);
703
704         return cx23885_set_tvnorm(dev, tvnorms);
705 }
706
707 int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
708 {
709         static const char *iname[] = {
710                 [CX23885_VMUX_COMPOSITE1] = "Composite1",
711                 [CX23885_VMUX_COMPOSITE2] = "Composite2",
712                 [CX23885_VMUX_COMPOSITE3] = "Composite3",
713                 [CX23885_VMUX_COMPOSITE4] = "Composite4",
714                 [CX23885_VMUX_SVIDEO]     = "S-Video",
715                 [CX23885_VMUX_COMPONENT]  = "Component",
716                 [CX23885_VMUX_TELEVISION] = "Television",
717                 [CX23885_VMUX_CABLE]      = "Cable TV",
718                 [CX23885_VMUX_DVB]        = "DVB",
719                 [CX23885_VMUX_DEBUG]      = "for debug only",
720         };
721         unsigned int n;
722         dprintk(1, "%s()\n", __func__);
723
724         n = i->index;
725         if (n >= MAX_CX23885_INPUT)
726                 return -EINVAL;
727
728         if (0 == INPUT(n)->type)
729                 return -EINVAL;
730
731         i->index = n;
732         i->type  = V4L2_INPUT_TYPE_CAMERA;
733         strcpy(i->name, iname[INPUT(n)->type]);
734         i->std = CX23885_NORMS;
735         if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
736                 (CX23885_VMUX_CABLE == INPUT(n)->type)) {
737                 i->type = V4L2_INPUT_TYPE_TUNER;
738                 i->audioset = 4;
739         } else {
740                 /* Two selectable audio inputs for non-tv inputs */
741                 i->audioset = 3;
742         }
743
744         if (dev->input == n) {
745                 /* enum'd input matches our configured input.
746                  * Ask the video decoder to process the call
747                  * and give it an oppertunity to update the
748                  * status field.
749                  */
750                 call_all(dev, video, g_input_status, &i->status);
751         }
752
753         return 0;
754 }
755
756 static int vidioc_enum_input(struct file *file, void *priv,
757                                 struct v4l2_input *i)
758 {
759         struct cx23885_dev *dev = video_drvdata(file);
760         dprintk(1, "%s()\n", __func__);
761         return cx23885_enum_input(dev, i);
762 }
763
764 int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
765 {
766         struct cx23885_dev *dev = video_drvdata(file);
767
768         *i = dev->input;
769         dprintk(1, "%s() returns %d\n", __func__, *i);
770         return 0;
771 }
772
773 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
774 {
775         return cx23885_get_input(file, priv, i);
776 }
777
778 int cx23885_set_input(struct file *file, void *priv, unsigned int i)
779 {
780         struct cx23885_dev *dev = video_drvdata(file);
781
782         dprintk(1, "%s(%d)\n", __func__, i);
783
784         if (i >= MAX_CX23885_INPUT) {
785                 dprintk(1, "%s() -EINVAL\n", __func__);
786                 return -EINVAL;
787         }
788
789         if (INPUT(i)->type == 0)
790                 return -EINVAL;
791
792         cx23885_video_mux(dev, i);
793
794         /* By default establish the default audio input for the card also */
795         /* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
796         cx23885_audio_mux(dev, i);
797         return 0;
798 }
799
800 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
801 {
802         return cx23885_set_input(file, priv, i);
803 }
804
805 static int vidioc_log_status(struct file *file, void *priv)
806 {
807         struct cx23885_dev *dev = video_drvdata(file);
808
809         call_all(dev, core, log_status);
810         return 0;
811 }
812
813 static int cx23885_query_audinput(struct file *file, void *priv,
814         struct v4l2_audio *i)
815 {
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                 pr_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                         pr_warn("%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) video lines miss-match\n",
1070                                 VID_BC_MSK_SYNC);
1071
1072                 if (status & VID_BC_MSK_OF)
1073                         dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
1074                                 VID_BC_MSK_OF);
1075
1076         }
1077
1078         /* Video */
1079         if (status & VID_BC_MSK_RISCI1) {
1080                 spin_lock(&dev->slock);
1081                 count = cx_read(VID_A_GPCNT);
1082                 cx23885_video_wakeup(dev, &dev->vidq, count);
1083                 spin_unlock(&dev->slock);
1084                 handled++;
1085         }
1086
1087         /* Allow the VBI framework to process it's payload */
1088         handled += cx23885_vbi_irq(dev, status);
1089
1090         return handled;
1091 }
1092
1093 /* ----------------------------------------------------------- */
1094 /* exported stuff                                              */
1095
1096 static const struct v4l2_file_operations video_fops = {
1097         .owner         = THIS_MODULE,
1098         .open           = v4l2_fh_open,
1099         .release        = vb2_fop_release,
1100         .read           = vb2_fop_read,
1101         .poll           = vb2_fop_poll,
1102         .unlocked_ioctl = video_ioctl2,
1103         .mmap           = vb2_fop_mmap,
1104 };
1105
1106 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1107         .vidioc_querycap      = vidioc_querycap,
1108         .vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1109         .vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1110         .vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1111         .vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1112         .vidioc_g_fmt_vbi_cap     = cx23885_vbi_fmt,
1113         .vidioc_try_fmt_vbi_cap   = cx23885_vbi_fmt,
1114         .vidioc_s_fmt_vbi_cap     = cx23885_vbi_fmt,
1115         .vidioc_reqbufs       = vb2_ioctl_reqbufs,
1116         .vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
1117         .vidioc_querybuf      = vb2_ioctl_querybuf,
1118         .vidioc_qbuf          = vb2_ioctl_qbuf,
1119         .vidioc_dqbuf         = vb2_ioctl_dqbuf,
1120         .vidioc_streamon      = vb2_ioctl_streamon,
1121         .vidioc_streamoff     = vb2_ioctl_streamoff,
1122         .vidioc_cropcap       = vidioc_cropcap,
1123         .vidioc_s_std         = vidioc_s_std,
1124         .vidioc_g_std         = vidioc_g_std,
1125         .vidioc_enum_input    = vidioc_enum_input,
1126         .vidioc_g_input       = vidioc_g_input,
1127         .vidioc_s_input       = vidioc_s_input,
1128         .vidioc_log_status    = vidioc_log_status,
1129         .vidioc_g_tuner       = vidioc_g_tuner,
1130         .vidioc_s_tuner       = vidioc_s_tuner,
1131         .vidioc_g_frequency   = vidioc_g_frequency,
1132         .vidioc_s_frequency   = vidioc_s_frequency,
1133 #ifdef CONFIG_VIDEO_ADV_DEBUG
1134         .vidioc_g_chip_info   = cx23885_g_chip_info,
1135         .vidioc_g_register    = cx23885_g_register,
1136         .vidioc_s_register    = cx23885_s_register,
1137 #endif
1138         .vidioc_enumaudio     = vidioc_enum_audinput,
1139         .vidioc_g_audio       = vidioc_g_audinput,
1140         .vidioc_s_audio       = vidioc_s_audinput,
1141         .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1142         .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1143 };
1144
1145 static struct video_device cx23885_vbi_template;
1146 static struct video_device cx23885_video_template = {
1147         .name                 = "cx23885-video",
1148         .fops                 = &video_fops,
1149         .ioctl_ops            = &video_ioctl_ops,
1150         .tvnorms              = CX23885_NORMS,
1151 };
1152
1153 void cx23885_video_unregister(struct cx23885_dev *dev)
1154 {
1155         dprintk(1, "%s()\n", __func__);
1156         cx23885_irq_remove(dev, 0x01);
1157
1158         if (dev->vbi_dev) {
1159                 if (video_is_registered(dev->vbi_dev))
1160                         video_unregister_device(dev->vbi_dev);
1161                 else
1162                         video_device_release(dev->vbi_dev);
1163                 dev->vbi_dev = NULL;
1164         }
1165         if (dev->video_dev) {
1166                 if (video_is_registered(dev->video_dev))
1167                         video_unregister_device(dev->video_dev);
1168                 else
1169                         video_device_release(dev->video_dev);
1170                 dev->video_dev = NULL;
1171         }
1172
1173         if (dev->audio_dev)
1174                 cx23885_audio_unregister(dev);
1175 }
1176
1177 int cx23885_video_register(struct cx23885_dev *dev)
1178 {
1179         struct vb2_queue *q;
1180         int err;
1181
1182         dprintk(1, "%s()\n", __func__);
1183
1184         /* Initialize VBI template */
1185         cx23885_vbi_template = cx23885_video_template;
1186         strcpy(cx23885_vbi_template.name, "cx23885-vbi");
1187
1188         dev->tvnorm = V4L2_STD_NTSC_M;
1189         dev->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
1190         dev->field = V4L2_FIELD_INTERLACED;
1191         dev->width = 720;
1192         dev->height = norm_maxh(dev->tvnorm);
1193
1194         /* init video dma queues */
1195         INIT_LIST_HEAD(&dev->vidq.active);
1196
1197         /* init vbi dma queues */
1198         INIT_LIST_HEAD(&dev->vbiq.active);
1199
1200         cx23885_irq_add_enable(dev, 0x01);
1201
1202         if ((TUNER_ABSENT != dev->tuner_type) &&
1203                         ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
1204                 struct v4l2_subdev *sd = NULL;
1205
1206                 if (dev->tuner_addr)
1207                         sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1208                                 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
1209                                 "tuner", dev->tuner_addr, NULL);
1210                 else
1211                         sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1212                                 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
1213                                 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
1214                 if (sd) {
1215                         struct tuner_setup tun_setup;
1216
1217                         memset(&tun_setup, 0, sizeof(tun_setup));
1218                         tun_setup.mode_mask = T_ANALOG_TV;
1219                         tun_setup.type = dev->tuner_type;
1220                         tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1221                         tun_setup.tuner_callback = cx23885_tuner_callback;
1222
1223                         v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1224
1225                         if ((dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) ||
1226                             (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXPVR2200)) {
1227                                 struct xc2028_ctrl ctrl = {
1228                                         .fname = XC2028_DEFAULT_FIRMWARE,
1229                                         .max_len = 64
1230                                 };
1231                                 struct v4l2_priv_tun_config cfg = {
1232                                         .tuner = dev->tuner_type,
1233                                         .priv = &ctrl
1234                                 };
1235                                 v4l2_subdev_call(sd, tuner, s_config, &cfg);
1236                         }
1237
1238                         if (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) {
1239                                 struct xc2028_ctrl ctrl = {
1240                                         .fname = "xc3028L-v36.fw",
1241                                         .max_len = 64
1242                                 };
1243                                 struct v4l2_priv_tun_config cfg = {
1244                                         .tuner = dev->tuner_type,
1245                                         .priv = &ctrl
1246                                 };
1247                                 v4l2_subdev_call(sd, tuner, s_config, &cfg);
1248                         }
1249                 }
1250         }
1251
1252         /* initial device configuration */
1253         mutex_lock(&dev->lock);
1254         cx23885_set_tvnorm(dev, dev->tvnorm);
1255         cx23885_video_mux(dev, 0);
1256         cx23885_audio_mux(dev, 0);
1257         mutex_unlock(&dev->lock);
1258
1259         q = &dev->vb2_vidq;
1260         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1261         q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1262         q->gfp_flags = GFP_DMA32;
1263         q->min_buffers_needed = 2;
1264         q->drv_priv = dev;
1265         q->buf_struct_size = sizeof(struct cx23885_buffer);
1266         q->ops = &cx23885_video_qops;
1267         q->mem_ops = &vb2_dma_sg_memops;
1268         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1269         q->lock = &dev->lock;
1270         q->dev = &dev->pci->dev;
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         q->dev = &dev->pci->dev;
1288
1289         err = vb2_queue_init(q);
1290         if (err < 0)
1291                 goto fail_unreg;
1292
1293         /* register Video device */
1294         dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1295                 &cx23885_video_template, "video");
1296         dev->video_dev->queue = &dev->vb2_vidq;
1297         err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1298                                     video_nr[dev->nr]);
1299         if (err < 0) {
1300                 pr_info("%s: can't register video device\n",
1301                         dev->name);
1302                 goto fail_unreg;
1303         }
1304         pr_info("%s: registered device %s [v4l2]\n",
1305                dev->name, video_device_node_name(dev->video_dev));
1306
1307         /* register VBI device */
1308         dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
1309                 &cx23885_vbi_template, "vbi");
1310         dev->vbi_dev->queue = &dev->vb2_vbiq;
1311         err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1312                                     vbi_nr[dev->nr]);
1313         if (err < 0) {
1314                 pr_info("%s: can't register vbi device\n",
1315                         dev->name);
1316                 goto fail_unreg;
1317         }
1318         pr_info("%s: registered device %s\n",
1319                dev->name, video_device_node_name(dev->vbi_dev));
1320
1321         /* Register ALSA audio device */
1322         dev->audio_dev = cx23885_audio_register(dev);
1323
1324         return 0;
1325
1326 fail_unreg:
1327         cx23885_video_unregister(dev);
1328         return err;
1329 }