hwmon: (acpi_power_meter) Fix acpi_bus_get_device() return value check
[linux-drm-fsl-dcu.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  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/kmod.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <linux/delay.h>
31 #include <linux/kthread.h>
32 #include <asm/div64.h>
33
34 #include "cx23885.h"
35 #include "cx23885-video.h"
36 #include <media/v4l2-common.h>
37 #include <media/v4l2-ioctl.h>
38 #include "cx23885-ioctl.h"
39 #include "tuner-xc2028.h"
40
41 #include <media/cx25840.h>
42
43 MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
44 MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
45 MODULE_LICENSE("GPL");
46
47 /* ------------------------------------------------------------------ */
48
49 static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
50 static unsigned int vbi_nr[]   = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
51 static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
52
53 module_param_array(video_nr, int, NULL, 0444);
54 module_param_array(vbi_nr,   int, NULL, 0444);
55 module_param_array(radio_nr, int, NULL, 0444);
56
57 MODULE_PARM_DESC(video_nr, "video device numbers");
58 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
59 MODULE_PARM_DESC(radio_nr, "radio device numbers");
60
61 static unsigned int video_debug;
62 module_param(video_debug, int, 0644);
63 MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
64
65 static unsigned int irq_debug;
66 module_param(irq_debug, int, 0644);
67 MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
68
69 static unsigned int vid_limit = 16;
70 module_param(vid_limit, int, 0644);
71 MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
72
73 #define dprintk(level, fmt, arg...)\
74         do { if (video_debug >= level)\
75                 printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
76         } while (0)
77
78 /* ------------------------------------------------------------------- */
79 /* static data                                                         */
80
81 #define FORMAT_FLAGS_PACKED       0x01
82 #if 0
83 static struct cx23885_fmt formats[] = {
84         {
85                 .name     = "8 bpp, gray",
86                 .fourcc   = V4L2_PIX_FMT_GREY,
87                 .depth    = 8,
88                 .flags    = FORMAT_FLAGS_PACKED,
89         }, {
90                 .name     = "15 bpp RGB, le",
91                 .fourcc   = V4L2_PIX_FMT_RGB555,
92                 .depth    = 16,
93                 .flags    = FORMAT_FLAGS_PACKED,
94         }, {
95                 .name     = "15 bpp RGB, be",
96                 .fourcc   = V4L2_PIX_FMT_RGB555X,
97                 .depth    = 16,
98                 .flags    = FORMAT_FLAGS_PACKED,
99         }, {
100                 .name     = "16 bpp RGB, le",
101                 .fourcc   = V4L2_PIX_FMT_RGB565,
102                 .depth    = 16,
103                 .flags    = FORMAT_FLAGS_PACKED,
104         }, {
105                 .name     = "16 bpp RGB, be",
106                 .fourcc   = V4L2_PIX_FMT_RGB565X,
107                 .depth    = 16,
108                 .flags    = FORMAT_FLAGS_PACKED,
109         }, {
110                 .name     = "24 bpp RGB, le",
111                 .fourcc   = V4L2_PIX_FMT_BGR24,
112                 .depth    = 24,
113                 .flags    = FORMAT_FLAGS_PACKED,
114         }, {
115                 .name     = "32 bpp RGB, le",
116                 .fourcc   = V4L2_PIX_FMT_BGR32,
117                 .depth    = 32,
118                 .flags    = FORMAT_FLAGS_PACKED,
119         }, {
120                 .name     = "32 bpp RGB, be",
121                 .fourcc   = V4L2_PIX_FMT_RGB32,
122                 .depth    = 32,
123                 .flags    = FORMAT_FLAGS_PACKED,
124         }, {
125                 .name     = "4:2:2, packed, YUYV",
126                 .fourcc   = V4L2_PIX_FMT_YUYV,
127                 .depth    = 16,
128                 .flags    = FORMAT_FLAGS_PACKED,
129         }, {
130                 .name     = "4:2:2, packed, UYVY",
131                 .fourcc   = V4L2_PIX_FMT_UYVY,
132                 .depth    = 16,
133                 .flags    = FORMAT_FLAGS_PACKED,
134         },
135 };
136 #else
137 static struct cx23885_fmt formats[] = {
138         {
139 #if 0
140                 .name     = "4:2:2, packed, UYVY",
141                 .fourcc   = V4L2_PIX_FMT_UYVY,
142                 .depth    = 16,
143                 .flags    = FORMAT_FLAGS_PACKED,
144         }, {
145 #endif
146                 .name     = "4:2:2, packed, YUYV",
147                 .fourcc   = V4L2_PIX_FMT_YUYV,
148                 .depth    = 16,
149                 .flags    = FORMAT_FLAGS_PACKED,
150         }
151 };
152 #endif
153
154 static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
155 {
156         unsigned int i;
157
158         for (i = 0; i < ARRAY_SIZE(formats); i++)
159                 if (formats[i].fourcc == fourcc)
160                         return formats+i;
161
162         printk(KERN_ERR "%s(%c%c%c%c) NOT FOUND\n", __func__,
163                 (fourcc & 0xff),
164                 ((fourcc >> 8) & 0xff),
165                 ((fourcc >> 16) & 0xff),
166                 ((fourcc >> 24) & 0xff)
167                 );
168         return NULL;
169 }
170
171 /* ------------------------------------------------------------------- */
172
173 static const struct v4l2_queryctrl no_ctl = {
174         .name  = "42",
175         .flags = V4L2_CTRL_FLAG_DISABLED,
176 };
177
178 static struct cx23885_ctrl cx23885_ctls[] = {
179         /* --- video --- */
180         {
181                 .v = {
182                         .id            = V4L2_CID_BRIGHTNESS,
183                         .name          = "Brightness",
184                         .minimum       = 0x00,
185                         .maximum       = 0xff,
186                         .step          = 1,
187                         .default_value = 0x7f,
188                         .type          = V4L2_CTRL_TYPE_INTEGER,
189                 },
190                 .off                   = 128,
191                 .reg                   = LUMA_CTRL,
192                 .mask                  = 0x00ff,
193                 .shift                 = 0,
194         }, {
195                 .v = {
196                         .id            = V4L2_CID_CONTRAST,
197                         .name          = "Contrast",
198                         .minimum       = 0,
199                         .maximum       = 0x7f,
200                         .step          = 1,
201                         .default_value = 0x3f,
202                         .type          = V4L2_CTRL_TYPE_INTEGER,
203                 },
204                 .off                   = 0,
205                 .reg                   = LUMA_CTRL,
206                 .mask                  = 0xff00,
207                 .shift                 = 8,
208         }, {
209                 .v = {
210                         .id            = V4L2_CID_HUE,
211                         .name          = "Hue",
212                         .minimum       = -127,
213                         .maximum       = 128,
214                         .step          = 1,
215                         .default_value = 0x0,
216                         .type          = V4L2_CTRL_TYPE_INTEGER,
217                 },
218                 .off                   = 128,
219                 .reg                   = CHROMA_CTRL,
220                 .mask                  = 0xff0000,
221                 .shift                 = 16,
222         }, {
223                 /* strictly, this only describes only U saturation.
224                  * V saturation is handled specially through code.
225                  */
226                 .v = {
227                         .id            = V4L2_CID_SATURATION,
228                         .name          = "Saturation",
229                         .minimum       = 0,
230                         .maximum       = 0x7f,
231                         .step          = 1,
232                         .default_value = 0x3f,
233                         .type          = V4L2_CTRL_TYPE_INTEGER,
234                 },
235                 .off                   = 0,
236                 .reg                   = CHROMA_CTRL,
237                 .mask                  = 0x00ff,
238                 .shift                 = 0,
239         }, {
240         /* --- audio --- */
241                 .v = {
242                         .id            = V4L2_CID_AUDIO_MUTE,
243                         .name          = "Mute",
244                         .minimum       = 0,
245                         .maximum       = 1,
246                         .default_value = 1,
247                         .type          = V4L2_CTRL_TYPE_BOOLEAN,
248                 },
249                 .reg                   = PATH1_CTL1,
250                 .mask                  = (0x1f << 24),
251                 .shift                 = 24,
252         }, {
253                 .v = {
254                         .id            = V4L2_CID_AUDIO_VOLUME,
255                         .name          = "Volume",
256                         .minimum       = 0,
257                         .maximum       = 65535,
258                         .step          = 65535 / 100,
259                         .default_value = 65535,
260                         .type          = V4L2_CTRL_TYPE_INTEGER,
261                 },
262                 .reg                   = PATH1_VOL_CTL,
263                 .mask                  = 0xff,
264                 .shift                 = 0,
265         }
266 };
267 static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
268
269 /* Must be sorted from low to high control ID! */
270 static const u32 cx23885_user_ctrls[] = {
271         V4L2_CID_USER_CLASS,
272         V4L2_CID_BRIGHTNESS,
273         V4L2_CID_CONTRAST,
274         V4L2_CID_SATURATION,
275         V4L2_CID_HUE,
276         V4L2_CID_AUDIO_VOLUME,
277         V4L2_CID_AUDIO_MUTE,
278         0
279 };
280
281 static const u32 *ctrl_classes[] = {
282         cx23885_user_ctrls,
283         NULL
284 };
285
286 void cx23885_video_wakeup(struct cx23885_dev *dev,
287         struct cx23885_dmaqueue *q, u32 count)
288 {
289         struct cx23885_buffer *buf;
290         int bc;
291
292         for (bc = 0;; bc++) {
293                 if (list_empty(&q->active))
294                         break;
295                 buf = list_entry(q->active.next,
296                                  struct cx23885_buffer, vb.queue);
297
298                 /* count comes from the hw and is is 16bit wide --
299                  * this trick handles wrap-arounds correctly for
300                  * up to 32767 buffers in flight... */
301                 if ((s16) (count - buf->count) < 0)
302                         break;
303
304                 v4l2_get_timestamp(&buf->vb.ts);
305                 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
306                         count, buf->count);
307                 buf->vb.state = VIDEOBUF_DONE;
308                 list_del(&buf->vb.queue);
309                 wake_up(&buf->vb.done);
310         }
311         if (list_empty(&q->active))
312                 del_timer(&q->timeout);
313         else
314                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
315         if (bc != 1)
316                 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
317                         __func__, bc);
318 }
319
320 int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
321 {
322         dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
323                 __func__,
324                 (unsigned int)norm,
325                 v4l2_norm_to_name(norm));
326
327         dev->tvnorm = norm;
328
329         call_all(dev, core, s_std, norm);
330
331         return 0;
332 }
333
334 static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
335                                     struct pci_dev *pci,
336                                     struct video_device *template,
337                                     char *type)
338 {
339         struct video_device *vfd;
340         dprintk(1, "%s()\n", __func__);
341
342         vfd = video_device_alloc();
343         if (NULL == vfd)
344                 return NULL;
345         *vfd = *template;
346         vfd->v4l2_dev = &dev->v4l2_dev;
347         vfd->release = video_device_release;
348         snprintf(vfd->name, sizeof(vfd->name), "%s (%s)",
349                  cx23885_boards[dev->board].name, type);
350         video_set_drvdata(vfd, dev);
351         return vfd;
352 }
353
354 static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
355 {
356         int i;
357
358         if (qctrl->id < V4L2_CID_BASE ||
359             qctrl->id >= V4L2_CID_LASTP1)
360                 return -EINVAL;
361         for (i = 0; i < CX23885_CTLS; i++)
362                 if (cx23885_ctls[i].v.id == qctrl->id)
363                         break;
364         if (i == CX23885_CTLS) {
365                 *qctrl = no_ctl;
366                 return 0;
367         }
368         *qctrl = cx23885_ctls[i].v;
369         return 0;
370 }
371
372 /* ------------------------------------------------------------------- */
373 /* resource management                                                 */
374
375 static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
376         unsigned int bit)
377 {
378         dprintk(1, "%s()\n", __func__);
379         if (fh->resources & bit)
380                 /* have it already allocated */
381                 return 1;
382
383         /* is it free? */
384         mutex_lock(&dev->lock);
385         if (dev->resources & bit) {
386                 /* no, someone else uses it */
387                 mutex_unlock(&dev->lock);
388                 return 0;
389         }
390         /* it's free, grab it */
391         fh->resources  |= bit;
392         dev->resources |= bit;
393         dprintk(1, "res: get %d\n", bit);
394         mutex_unlock(&dev->lock);
395         return 1;
396 }
397
398 static int res_check(struct cx23885_fh *fh, unsigned int bit)
399 {
400         return fh->resources & bit;
401 }
402
403 static int res_locked(struct cx23885_dev *dev, unsigned int bit)
404 {
405         return dev->resources & bit;
406 }
407
408 static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
409         unsigned int bits)
410 {
411         BUG_ON((fh->resources & bits) != bits);
412         dprintk(1, "%s()\n", __func__);
413
414         mutex_lock(&dev->lock);
415         fh->resources  &= ~bits;
416         dev->resources &= ~bits;
417         dprintk(1, "res: put %d\n", bits);
418         mutex_unlock(&dev->lock);
419 }
420
421 int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data)
422 {
423         /* 8 bit registers, 8 bit values */
424         u8 buf[] = { reg, data };
425
426         struct i2c_msg msg = { .addr = 0x98 >> 1,
427                 .flags = 0, .buf = buf, .len = 2 };
428
429         return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
430 }
431
432 u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg)
433 {
434         /* 8 bit registers, 8 bit values */
435         int ret;
436         u8 b0[] = { reg };
437         u8 b1[] = { 0 };
438
439         struct i2c_msg msg[] = {
440                 { .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 },
441                 { .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 }
442         };
443
444         ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2);
445         if (ret != 2)
446                 printk(KERN_ERR "%s() error\n", __func__);
447
448         return b1[0];
449 }
450
451 static void cx23885_flatiron_dump(struct cx23885_dev *dev)
452 {
453         int i;
454         dprintk(1, "Flatiron dump\n");
455         for (i = 0; i < 0x24; i++) {
456                 dprintk(1, "FI[%02x] = %02x\n", i,
457                         cx23885_flatiron_read(dev, i));
458         }
459 }
460
461 static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input)
462 {
463         u8 val;
464         dprintk(1, "%s(input = %d)\n", __func__, input);
465
466         if (input == 1)
467                 val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL;
468         else if (input == 2)
469                 val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL;
470         else
471                 return -EINVAL;
472
473         val |= 0x20; /* Enable clock to delta-sigma and dec filter */
474
475         cx23885_flatiron_write(dev, CH_PWR_CTRL1, val);
476
477         /* Wake up */
478         cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0);
479
480         if (video_debug)
481                 cx23885_flatiron_dump(dev);
482
483         return 0;
484 }
485
486 static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
487 {
488         dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
489                 __func__,
490                 input, INPUT(input)->vmux,
491                 INPUT(input)->gpio0, INPUT(input)->gpio1,
492                 INPUT(input)->gpio2, INPUT(input)->gpio3);
493         dev->input = input;
494
495         if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
496                 dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2 ||
497                 dev->board == CX23885_BOARD_MYGICA_X8507) {
498                 /* Select Analog TV */
499                 if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
500                         cx23885_gpio_clear(dev, GPIO_0);
501         }
502
503         /* Tell the internal A/V decoder */
504         v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
505                         INPUT(input)->vmux, 0, 0);
506
507         if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) ||
508                 (dev->board == CX23885_BOARD_MPX885) ||
509                 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1250) ||
510                 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
511                 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) ||
512                 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
513                 (dev->board == CX23885_BOARD_MYGICA_X8507) ||
514                 (dev->board == CX23885_BOARD_AVERMEDIA_HC81R)) {
515                 /* Configure audio routing */
516                 v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
517                         INPUT(input)->amux, 0, 0);
518
519                 if (INPUT(input)->amux == CX25840_AUDIO7)
520                         cx23885_flatiron_mux(dev, 1);
521                 else if (INPUT(input)->amux == CX25840_AUDIO6)
522                         cx23885_flatiron_mux(dev, 2);
523         }
524
525         return 0;
526 }
527
528 static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
529 {
530         dprintk(1, "%s(input=%d)\n", __func__, input);
531
532         /* The baseband video core of the cx23885 has two audio inputs.
533          * LR1 and LR2. In almost every single case so far only HVR1xxx
534          * cards we've only ever supported LR1. Time to support LR2,
535          * which is available via the optional white breakout header on
536          * the board.
537          * We'll use a could of existing enums in the card struct to allow
538          * devs to specify which baseband input they need, or just default
539          * to what we've always used.
540          */
541         if (INPUT(input)->amux == CX25840_AUDIO7)
542                 cx23885_flatiron_mux(dev, 1);
543         else if (INPUT(input)->amux == CX25840_AUDIO6)
544                 cx23885_flatiron_mux(dev, 2);
545         else {
546                 /* Not specifically defined, assume the default. */
547                 cx23885_flatiron_mux(dev, 1);
548         }
549
550         return 0;
551 }
552
553 /* ------------------------------------------------------------------ */
554 static int cx23885_start_video_dma(struct cx23885_dev *dev,
555                            struct cx23885_dmaqueue *q,
556                            struct cx23885_buffer *buf)
557 {
558         dprintk(1, "%s()\n", __func__);
559
560         /* Stop the dma/fifo before we tamper with it's risc programs */
561         cx_clear(VID_A_DMA_CTL, 0x11);
562
563         /* setup fifo + format */
564         cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
565                                 buf->bpl, buf->risc.dma);
566
567         /* reset counter */
568         cx_write(VID_A_GPCNT_CTL, 3);
569         q->count = 1;
570
571         /* enable irq */
572         cx23885_irq_add_enable(dev, 0x01);
573         cx_set(VID_A_INT_MSK, 0x000011);
574
575         /* start dma */
576         cx_set(DEV_CNTRL2, (1<<5));
577         cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
578
579         return 0;
580 }
581
582
583 static int cx23885_restart_video_queue(struct cx23885_dev *dev,
584                                struct cx23885_dmaqueue *q)
585 {
586         struct cx23885_buffer *buf, *prev;
587         struct list_head *item;
588         dprintk(1, "%s()\n", __func__);
589
590         if (!list_empty(&q->active)) {
591                 buf = list_entry(q->active.next, struct cx23885_buffer,
592                         vb.queue);
593                 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
594                         buf, buf->vb.i);
595                 cx23885_start_video_dma(dev, q, buf);
596                 list_for_each(item, &q->active) {
597                         buf = list_entry(item, struct cx23885_buffer,
598                                 vb.queue);
599                         buf->count    = q->count++;
600                 }
601                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
602                 return 0;
603         }
604
605         prev = NULL;
606         for (;;) {
607                 if (list_empty(&q->queued))
608                         return 0;
609                 buf = list_entry(q->queued.next, struct cx23885_buffer,
610                         vb.queue);
611                 if (NULL == prev) {
612                         list_move_tail(&buf->vb.queue, &q->active);
613                         cx23885_start_video_dma(dev, q, buf);
614                         buf->vb.state = VIDEOBUF_ACTIVE;
615                         buf->count    = q->count++;
616                         mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
617                         dprintk(2, "[%p/%d] restart_queue - first active\n",
618                                 buf, buf->vb.i);
619
620                 } else if (prev->vb.width  == buf->vb.width  &&
621                            prev->vb.height == buf->vb.height &&
622                            prev->fmt       == buf->fmt) {
623                         list_move_tail(&buf->vb.queue, &q->active);
624                         buf->vb.state = VIDEOBUF_ACTIVE;
625                         buf->count    = q->count++;
626                         prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
627                         prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
628                         dprintk(2, "[%p/%d] restart_queue - move to active\n",
629                                 buf, buf->vb.i);
630                 } else {
631                         return 0;
632                 }
633                 prev = buf;
634         }
635 }
636
637 static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
638         unsigned int *size)
639 {
640         struct cx23885_fh *fh = q->priv_data;
641
642         *size = fh->fmt->depth*fh->width*fh->height >> 3;
643         if (0 == *count)
644                 *count = 32;
645         if (*size * *count > vid_limit * 1024 * 1024)
646                 *count = (vid_limit * 1024 * 1024) / *size;
647         return 0;
648 }
649
650 static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
651                enum v4l2_field field)
652 {
653         struct cx23885_fh *fh  = q->priv_data;
654         struct cx23885_dev *dev = fh->dev;
655         struct cx23885_buffer *buf =
656                 container_of(vb, struct cx23885_buffer, vb);
657         int rc, init_buffer = 0;
658         u32 line0_offset, line1_offset;
659         struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
660         int field_tff;
661
662         BUG_ON(NULL == fh->fmt);
663         if (fh->width  < 48 || fh->width  > norm_maxw(dev->tvnorm) ||
664             fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
665                 return -EINVAL;
666         buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
667         if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
668                 return -EINVAL;
669
670         if (buf->fmt       != fh->fmt    ||
671             buf->vb.width  != fh->width  ||
672             buf->vb.height != fh->height ||
673             buf->vb.field  != field) {
674                 buf->fmt       = fh->fmt;
675                 buf->vb.width  = fh->width;
676                 buf->vb.height = fh->height;
677                 buf->vb.field  = field;
678                 init_buffer = 1;
679         }
680
681         if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
682                 init_buffer = 1;
683                 rc = videobuf_iolock(q, &buf->vb, NULL);
684                 if (0 != rc)
685                         goto fail;
686         }
687
688         if (init_buffer) {
689                 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
690                 switch (buf->vb.field) {
691                 case V4L2_FIELD_TOP:
692                         cx23885_risc_buffer(dev->pci, &buf->risc,
693                                          dma->sglist, 0, UNSET,
694                                          buf->bpl, 0, buf->vb.height);
695                         break;
696                 case V4L2_FIELD_BOTTOM:
697                         cx23885_risc_buffer(dev->pci, &buf->risc,
698                                          dma->sglist, UNSET, 0,
699                                          buf->bpl, 0, buf->vb.height);
700                         break;
701                 case V4L2_FIELD_INTERLACED:
702                         if (dev->tvnorm & V4L2_STD_NTSC)
703                                 /* NTSC or  */
704                                 field_tff = 1;
705                         else
706                                 field_tff = 0;
707
708                         if (cx23885_boards[dev->board].force_bff)
709                                 /* PAL / SECAM OR 888 in NTSC MODE */
710                                 field_tff = 0;
711
712                         if (field_tff) {
713                                 /* cx25840 transmits NTSC bottom field first */
714                                 dprintk(1, "%s() Creating TFF/NTSC risc\n",
715                                         __func__);
716                                 line0_offset = buf->bpl;
717                                 line1_offset = 0;
718                         } else {
719                                 /* All other formats are top field first */
720                                 dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
721                                         __func__);
722                                 line0_offset = 0;
723                                 line1_offset = buf->bpl;
724                         }
725                         cx23885_risc_buffer(dev->pci, &buf->risc,
726                                         dma->sglist, line0_offset,
727                                         line1_offset,
728                                         buf->bpl, buf->bpl,
729                                         buf->vb.height >> 1);
730                         break;
731                 case V4L2_FIELD_SEQ_TB:
732                         cx23885_risc_buffer(dev->pci, &buf->risc,
733                                          dma->sglist,
734                                          0, buf->bpl * (buf->vb.height >> 1),
735                                          buf->bpl, 0,
736                                          buf->vb.height >> 1);
737                         break;
738                 case V4L2_FIELD_SEQ_BT:
739                         cx23885_risc_buffer(dev->pci, &buf->risc,
740                                          dma->sglist,
741                                          buf->bpl * (buf->vb.height >> 1), 0,
742                                          buf->bpl, 0,
743                                          buf->vb.height >> 1);
744                         break;
745                 default:
746                         BUG();
747                 }
748         }
749         dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
750                 buf, buf->vb.i,
751                 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
752                 (unsigned long)buf->risc.dma);
753
754         buf->vb.state = VIDEOBUF_PREPARED;
755         return 0;
756
757  fail:
758         cx23885_free_buffer(q, buf);
759         return rc;
760 }
761
762 static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
763 {
764         struct cx23885_buffer   *buf = container_of(vb,
765                 struct cx23885_buffer, vb);
766         struct cx23885_buffer   *prev;
767         struct cx23885_fh       *fh   = vq->priv_data;
768         struct cx23885_dev      *dev  = fh->dev;
769         struct cx23885_dmaqueue *q    = &dev->vidq;
770
771         /* add jump to stopper */
772         buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
773         buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
774         buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
775
776         if (!list_empty(&q->queued)) {
777                 list_add_tail(&buf->vb.queue, &q->queued);
778                 buf->vb.state = VIDEOBUF_QUEUED;
779                 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
780                         buf, buf->vb.i);
781
782         } else if (list_empty(&q->active)) {
783                 list_add_tail(&buf->vb.queue, &q->active);
784                 cx23885_start_video_dma(dev, q, buf);
785                 buf->vb.state = VIDEOBUF_ACTIVE;
786                 buf->count    = q->count++;
787                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
788                 dprintk(2, "[%p/%d] buffer_queue - first active\n",
789                         buf, buf->vb.i);
790
791         } else {
792                 prev = list_entry(q->active.prev, struct cx23885_buffer,
793                         vb.queue);
794                 if (prev->vb.width  == buf->vb.width  &&
795                     prev->vb.height == buf->vb.height &&
796                     prev->fmt       == buf->fmt) {
797                         list_add_tail(&buf->vb.queue, &q->active);
798                         buf->vb.state = VIDEOBUF_ACTIVE;
799                         buf->count    = q->count++;
800                         prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
801                         /* 64 bit bits 63-32 */
802                         prev->risc.jmp[2] = cpu_to_le32(0);
803                         dprintk(2, "[%p/%d] buffer_queue - append to active\n",
804                                 buf, buf->vb.i);
805
806                 } else {
807                         list_add_tail(&buf->vb.queue, &q->queued);
808                         buf->vb.state = VIDEOBUF_QUEUED;
809                         dprintk(2, "[%p/%d] buffer_queue - first queued\n",
810                                 buf, buf->vb.i);
811                 }
812         }
813 }
814
815 static void buffer_release(struct videobuf_queue *q,
816         struct videobuf_buffer *vb)
817 {
818         struct cx23885_buffer *buf = container_of(vb,
819                 struct cx23885_buffer, vb);
820
821         cx23885_free_buffer(q, buf);
822 }
823
824 static struct videobuf_queue_ops cx23885_video_qops = {
825         .buf_setup    = buffer_setup,
826         .buf_prepare  = buffer_prepare,
827         .buf_queue    = buffer_queue,
828         .buf_release  = buffer_release,
829 };
830
831 static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
832 {
833         switch (fh->type) {
834         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
835                 return &fh->vidq;
836         case V4L2_BUF_TYPE_VBI_CAPTURE:
837                 return &fh->vbiq;
838         default:
839                 BUG();
840                 return NULL;
841         }
842 }
843
844 static int get_resource(struct cx23885_fh *fh)
845 {
846         switch (fh->type) {
847         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
848                 return RESOURCE_VIDEO;
849         case V4L2_BUF_TYPE_VBI_CAPTURE:
850                 return RESOURCE_VBI;
851         default:
852                 BUG();
853                 return 0;
854         }
855 }
856
857 static int video_open(struct file *file)
858 {
859         struct video_device *vdev = video_devdata(file);
860         struct cx23885_dev *dev = video_drvdata(file);
861         struct cx23885_fh *fh;
862         enum v4l2_buf_type type = 0;
863         int radio = 0;
864
865         switch (vdev->vfl_type) {
866         case VFL_TYPE_GRABBER:
867                 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
868                 break;
869         case VFL_TYPE_VBI:
870                 type = V4L2_BUF_TYPE_VBI_CAPTURE;
871                 break;
872         case VFL_TYPE_RADIO:
873                 radio = 1;
874                 break;
875         }
876
877         dprintk(1, "open dev=%s radio=%d type=%s\n",
878                 video_device_node_name(vdev), radio, v4l2_type_names[type]);
879
880         /* allocate + initialize per filehandle data */
881         fh = kzalloc(sizeof(*fh), GFP_KERNEL);
882         if (NULL == fh)
883                 return -ENOMEM;
884
885         file->private_data = fh;
886         fh->dev      = dev;
887         fh->radio    = radio;
888         fh->type     = type;
889         fh->width    = 320;
890         fh->height   = 240;
891         fh->fmt      = format_by_fourcc(V4L2_PIX_FMT_YUYV);
892
893         videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
894                             &dev->pci->dev, &dev->slock,
895                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
896                             V4L2_FIELD_INTERLACED,
897                             sizeof(struct cx23885_buffer),
898                             fh, NULL);
899
900         videobuf_queue_sg_init(&fh->vbiq, &cx23885_vbi_qops,
901                 &dev->pci->dev, &dev->slock,
902                 V4L2_BUF_TYPE_VBI_CAPTURE,
903                 V4L2_FIELD_SEQ_TB,
904                 sizeof(struct cx23885_buffer),
905                 fh, NULL);
906
907
908         dprintk(1, "post videobuf_queue_init()\n");
909
910         return 0;
911 }
912
913 static ssize_t video_read(struct file *file, char __user *data,
914         size_t count, loff_t *ppos)
915 {
916         struct cx23885_fh *fh = file->private_data;
917
918         switch (fh->type) {
919         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
920                 if (res_locked(fh->dev, RESOURCE_VIDEO))
921                         return -EBUSY;
922                 return videobuf_read_one(&fh->vidq, data, count, ppos,
923                                          file->f_flags & O_NONBLOCK);
924         case V4L2_BUF_TYPE_VBI_CAPTURE:
925                 if (!res_get(fh->dev, fh, RESOURCE_VBI))
926                         return -EBUSY;
927                 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
928                                             file->f_flags & O_NONBLOCK);
929         default:
930                 BUG();
931                 return 0;
932         }
933 }
934
935 static unsigned int video_poll(struct file *file,
936         struct poll_table_struct *wait)
937 {
938         struct cx23885_fh *fh = file->private_data;
939         struct cx23885_buffer *buf;
940         unsigned int rc = POLLERR;
941
942         if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
943                 if (!res_get(fh->dev, fh, RESOURCE_VBI))
944                         return POLLERR;
945                 return videobuf_poll_stream(file, &fh->vbiq, wait);
946         }
947
948         mutex_lock(&fh->vidq.vb_lock);
949         if (res_check(fh, RESOURCE_VIDEO)) {
950                 /* streaming capture */
951                 if (list_empty(&fh->vidq.stream))
952                         goto done;
953                 buf = list_entry(fh->vidq.stream.next,
954                         struct cx23885_buffer, vb.stream);
955         } else {
956                 /* read() capture */
957                 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
958                 if (NULL == buf)
959                         goto done;
960         }
961         poll_wait(file, &buf->vb.done, wait);
962         if (buf->vb.state == VIDEOBUF_DONE ||
963             buf->vb.state == VIDEOBUF_ERROR)
964                 rc =  POLLIN|POLLRDNORM;
965         else
966                 rc = 0;
967 done:
968         mutex_unlock(&fh->vidq.vb_lock);
969         return rc;
970 }
971
972 static int video_release(struct file *file)
973 {
974         struct cx23885_fh *fh = file->private_data;
975         struct cx23885_dev *dev = fh->dev;
976
977         /* turn off overlay */
978         if (res_check(fh, RESOURCE_OVERLAY)) {
979                 /* FIXME */
980                 res_free(dev, fh, RESOURCE_OVERLAY);
981         }
982
983         /* stop video capture */
984         if (res_check(fh, RESOURCE_VIDEO)) {
985                 videobuf_queue_cancel(&fh->vidq);
986                 res_free(dev, fh, RESOURCE_VIDEO);
987         }
988         if (fh->vidq.read_buf) {
989                 buffer_release(&fh->vidq, fh->vidq.read_buf);
990                 kfree(fh->vidq.read_buf);
991         }
992
993         /* stop vbi capture */
994         if (res_check(fh, RESOURCE_VBI)) {
995                 if (fh->vbiq.streaming)
996                         videobuf_streamoff(&fh->vbiq);
997                 if (fh->vbiq.reading)
998                         videobuf_read_stop(&fh->vbiq);
999                 res_free(dev, fh, RESOURCE_VBI);
1000         }
1001
1002         videobuf_mmap_free(&fh->vidq);
1003         videobuf_mmap_free(&fh->vbiq);
1004
1005         file->private_data = NULL;
1006         kfree(fh);
1007
1008         /* We are not putting the tuner to sleep here on exit, because
1009          * we want to use the mpeg encoder in another session to capture
1010          * tuner video. Closing this will result in no video to the encoder.
1011          */
1012
1013         return 0;
1014 }
1015
1016 static int video_mmap(struct file *file, struct vm_area_struct *vma)
1017 {
1018         struct cx23885_fh *fh = file->private_data;
1019
1020         return videobuf_mmap_mapper(get_queue(fh), vma);
1021 }
1022
1023 /* ------------------------------------------------------------------ */
1024 /* VIDEO CTRL IOCTLS                                                  */
1025
1026 int cx23885_get_control(struct cx23885_dev *dev,
1027         struct v4l2_control *ctl)
1028 {
1029         dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
1030         call_all(dev, core, g_ctrl, ctl);
1031         return 0;
1032 }
1033
1034 int cx23885_set_control(struct cx23885_dev *dev,
1035         struct v4l2_control *ctl)
1036 {
1037         dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)\n", __func__);
1038         call_all(dev, core, s_ctrl, ctl);
1039
1040         return 0;
1041 }
1042
1043 static void init_controls(struct cx23885_dev *dev)
1044 {
1045         struct v4l2_control ctrl;
1046         int i;
1047
1048         for (i = 0; i < CX23885_CTLS; i++) {
1049                 ctrl.id = cx23885_ctls[i].v.id;
1050                 ctrl.value = cx23885_ctls[i].v.default_value;
1051
1052                 cx23885_set_control(dev, &ctrl);
1053         }
1054 }
1055
1056 /* ------------------------------------------------------------------ */
1057 /* VIDEO IOCTLS                                                       */
1058
1059 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1060         struct v4l2_format *f)
1061 {
1062         struct cx23885_fh *fh   = priv;
1063
1064         f->fmt.pix.width        = fh->width;
1065         f->fmt.pix.height       = fh->height;
1066         f->fmt.pix.field        = fh->vidq.field;
1067         f->fmt.pix.pixelformat  = fh->fmt->fourcc;
1068         f->fmt.pix.bytesperline =
1069                 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1070         f->fmt.pix.sizeimage =
1071                 f->fmt.pix.height * f->fmt.pix.bytesperline;
1072
1073         return 0;
1074 }
1075
1076 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1077         struct v4l2_format *f)
1078 {
1079         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1080         struct cx23885_fmt *fmt;
1081         enum v4l2_field   field;
1082         unsigned int      maxw, maxh;
1083
1084         fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1085         if (NULL == fmt)
1086                 return -EINVAL;
1087
1088         field = f->fmt.pix.field;
1089         maxw  = norm_maxw(dev->tvnorm);
1090         maxh  = norm_maxh(dev->tvnorm);
1091
1092         if (V4L2_FIELD_ANY == field) {
1093                 field = (f->fmt.pix.height > maxh/2)
1094                         ? V4L2_FIELD_INTERLACED
1095                         : V4L2_FIELD_BOTTOM;
1096         }
1097
1098         switch (field) {
1099         case V4L2_FIELD_TOP:
1100         case V4L2_FIELD_BOTTOM:
1101                 maxh = maxh / 2;
1102                 break;
1103         case V4L2_FIELD_INTERLACED:
1104                 break;
1105         default:
1106                 return -EINVAL;
1107         }
1108
1109         f->fmt.pix.field = field;
1110         v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
1111                               &f->fmt.pix.height, 32, maxh, 0, 0);
1112         f->fmt.pix.bytesperline =
1113                 (f->fmt.pix.width * fmt->depth) >> 3;
1114         f->fmt.pix.sizeimage =
1115                 f->fmt.pix.height * f->fmt.pix.bytesperline;
1116
1117         return 0;
1118 }
1119
1120 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1121         struct v4l2_format *f)
1122 {
1123         struct cx23885_fh *fh = priv;
1124         struct cx23885_dev *dev  = ((struct cx23885_fh *)priv)->dev;
1125         struct v4l2_mbus_framefmt mbus_fmt;
1126         int err;
1127
1128         dprintk(2, "%s()\n", __func__);
1129         err = vidioc_try_fmt_vid_cap(file, priv, f);
1130
1131         if (0 != err)
1132                 return err;
1133         fh->fmt        = format_by_fourcc(f->fmt.pix.pixelformat);
1134         fh->width      = f->fmt.pix.width;
1135         fh->height     = f->fmt.pix.height;
1136         fh->vidq.field = f->fmt.pix.field;
1137         dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
1138                 fh->width, fh->height, fh->vidq.field);
1139         v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
1140         call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1141         v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
1142         return 0;
1143 }
1144
1145 static int vidioc_querycap(struct file *file, void  *priv,
1146         struct v4l2_capability *cap)
1147 {
1148         struct cx23885_dev *dev  = ((struct cx23885_fh *)priv)->dev;
1149
1150         strcpy(cap->driver, "cx23885");
1151         strlcpy(cap->card, cx23885_boards[dev->board].name,
1152                 sizeof(cap->card));
1153         sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1154         cap->capabilities =
1155                 V4L2_CAP_VIDEO_CAPTURE |
1156                 V4L2_CAP_READWRITE     |
1157                 V4L2_CAP_STREAMING     |
1158                 V4L2_CAP_VBI_CAPTURE;
1159         if (UNSET != dev->tuner_type)
1160                 cap->capabilities |= V4L2_CAP_TUNER;
1161         return 0;
1162 }
1163
1164 static int vidioc_enum_fmt_vid_cap(struct file *file, void  *priv,
1165         struct v4l2_fmtdesc *f)
1166 {
1167         if (unlikely(f->index >= ARRAY_SIZE(formats)))
1168                 return -EINVAL;
1169
1170         strlcpy(f->description, formats[f->index].name,
1171                 sizeof(f->description));
1172         f->pixelformat = formats[f->index].fourcc;
1173
1174         return 0;
1175 }
1176
1177 static int vidioc_reqbufs(struct file *file, void *priv,
1178         struct v4l2_requestbuffers *p)
1179 {
1180         struct cx23885_fh *fh = priv;
1181         return videobuf_reqbufs(get_queue(fh), p);
1182 }
1183
1184 static int vidioc_querybuf(struct file *file, void *priv,
1185         struct v4l2_buffer *p)
1186 {
1187         struct cx23885_fh *fh = priv;
1188         return videobuf_querybuf(get_queue(fh), p);
1189 }
1190
1191 static int vidioc_qbuf(struct file *file, void *priv,
1192         struct v4l2_buffer *p)
1193 {
1194         struct cx23885_fh *fh = priv;
1195         return videobuf_qbuf(get_queue(fh), p);
1196 }
1197
1198 static int vidioc_dqbuf(struct file *file, void *priv,
1199         struct v4l2_buffer *p)
1200 {
1201         struct cx23885_fh *fh = priv;
1202         return videobuf_dqbuf(get_queue(fh), p,
1203                                 file->f_flags & O_NONBLOCK);
1204 }
1205
1206 static int vidioc_streamon(struct file *file, void *priv,
1207         enum v4l2_buf_type i)
1208 {
1209         struct cx23885_fh *fh = priv;
1210         struct cx23885_dev *dev = fh->dev;
1211         dprintk(1, "%s()\n", __func__);
1212
1213         if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1214                 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
1215                 return -EINVAL;
1216         if (unlikely(i != fh->type))
1217                 return -EINVAL;
1218
1219         if (unlikely(!res_get(dev, fh, get_resource(fh))))
1220                 return -EBUSY;
1221
1222         /* Don't start VBI streaming unless vida streaming
1223          * has already started.
1224          */
1225         if ((fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) &&
1226                 ((cx_read(VID_A_DMA_CTL) & 0x11) == 0))
1227                 return -EINVAL;
1228
1229         return videobuf_streamon(get_queue(fh));
1230 }
1231
1232 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1233 {
1234         struct cx23885_fh *fh = priv;
1235         struct cx23885_dev *dev = fh->dev;
1236         int err, res;
1237         dprintk(1, "%s()\n", __func__);
1238
1239         if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1240                 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
1241                 return -EINVAL;
1242         if (i != fh->type)
1243                 return -EINVAL;
1244
1245         res = get_resource(fh);
1246         err = videobuf_streamoff(get_queue(fh));
1247         if (err < 0)
1248                 return err;
1249         res_free(dev, fh, res);
1250         return 0;
1251 }
1252
1253 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
1254 {
1255         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1256         dprintk(1, "%s()\n", __func__);
1257
1258         *id = dev->tvnorm;
1259         return 0;
1260 }
1261
1262 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id tvnorms)
1263 {
1264         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1265         dprintk(1, "%s()\n", __func__);
1266
1267         mutex_lock(&dev->lock);
1268         cx23885_set_tvnorm(dev, tvnorms);
1269         mutex_unlock(&dev->lock);
1270
1271         return 0;
1272 }
1273
1274 int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
1275 {
1276         static const char *iname[] = {
1277                 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1278                 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1279                 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1280                 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1281                 [CX23885_VMUX_SVIDEO]     = "S-Video",
1282                 [CX23885_VMUX_COMPONENT]  = "Component",
1283                 [CX23885_VMUX_TELEVISION] = "Television",
1284                 [CX23885_VMUX_CABLE]      = "Cable TV",
1285                 [CX23885_VMUX_DVB]        = "DVB",
1286                 [CX23885_VMUX_DEBUG]      = "for debug only",
1287         };
1288         unsigned int n;
1289         dprintk(1, "%s()\n", __func__);
1290
1291         n = i->index;
1292         if (n >= MAX_CX23885_INPUT)
1293                 return -EINVAL;
1294
1295         if (0 == INPUT(n)->type)
1296                 return -EINVAL;
1297
1298         i->index = n;
1299         i->type  = V4L2_INPUT_TYPE_CAMERA;
1300         strcpy(i->name, iname[INPUT(n)->type]);
1301         if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
1302                 (CX23885_VMUX_CABLE == INPUT(n)->type)) {
1303                 i->type = V4L2_INPUT_TYPE_TUNER;
1304                 i->std = CX23885_NORMS;
1305         }
1306
1307         /* Two selectable audio inputs for non-tv inputs */
1308         if (INPUT(n)->type != CX23885_VMUX_TELEVISION)
1309                 i->audioset = 0x3;
1310
1311         if (dev->input == n) {
1312                 /* enum'd input matches our configured input.
1313                  * Ask the video decoder to process the call
1314                  * and give it an oppertunity to update the
1315                  * status field.
1316                  */
1317                 call_all(dev, video, g_input_status, &i->status);
1318         }
1319
1320         return 0;
1321 }
1322
1323 static int vidioc_enum_input(struct file *file, void *priv,
1324                                 struct v4l2_input *i)
1325 {
1326         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1327         dprintk(1, "%s()\n", __func__);
1328         return cx23885_enum_input(dev, i);
1329 }
1330
1331 int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
1332 {
1333         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1334
1335         *i = dev->input;
1336         dprintk(1, "%s() returns %d\n", __func__, *i);
1337         return 0;
1338 }
1339
1340 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1341 {
1342         return cx23885_get_input(file, priv, i);
1343 }
1344
1345 int cx23885_set_input(struct file *file, void *priv, unsigned int i)
1346 {
1347         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1348
1349         dprintk(1, "%s(%d)\n", __func__, i);
1350
1351         if (i >= MAX_CX23885_INPUT) {
1352                 dprintk(1, "%s() -EINVAL\n", __func__);
1353                 return -EINVAL;
1354         }
1355
1356         if (INPUT(i)->type == 0)
1357                 return -EINVAL;
1358
1359         mutex_lock(&dev->lock);
1360         cx23885_video_mux(dev, i);
1361
1362         /* By default establish the default audio input for the card also */
1363         /* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
1364         cx23885_audio_mux(dev, i);
1365         mutex_unlock(&dev->lock);
1366         return 0;
1367 }
1368
1369 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1370 {
1371         return cx23885_set_input(file, priv, i);
1372 }
1373
1374 static int vidioc_log_status(struct file *file, void *priv)
1375 {
1376         struct cx23885_fh  *fh  = priv;
1377         struct cx23885_dev *dev = fh->dev;
1378
1379         printk(KERN_INFO
1380                 "%s/0: ============  START LOG STATUS  ============\n",
1381                 dev->name);
1382         call_all(dev, core, log_status);
1383         printk(KERN_INFO
1384                 "%s/0: =============  END LOG STATUS  =============\n",
1385                 dev->name);
1386         return 0;
1387 }
1388
1389 static int cx23885_query_audinput(struct file *file, void *priv,
1390         struct v4l2_audio *i)
1391 {
1392         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1393         static const char *iname[] = {
1394                 [0] = "Baseband L/R 1",
1395                 [1] = "Baseband L/R 2",
1396         };
1397         unsigned int n;
1398         dprintk(1, "%s()\n", __func__);
1399
1400         n = i->index;
1401         if (n >= 2)
1402                 return -EINVAL;
1403
1404         memset(i, 0, sizeof(*i));
1405         i->index = n;
1406         strcpy(i->name, iname[n]);
1407         i->capability  = V4L2_AUDCAP_STEREO;
1408         i->mode  = V4L2_AUDMODE_AVL;
1409         return 0;
1410
1411 }
1412
1413 static int vidioc_enum_audinput(struct file *file, void *priv,
1414                                 struct v4l2_audio *i)
1415 {
1416         return cx23885_query_audinput(file, priv, i);
1417 }
1418
1419 static int vidioc_g_audinput(struct file *file, void *priv,
1420         struct v4l2_audio *i)
1421 {
1422         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1423
1424         i->index = dev->audinput;
1425         dprintk(1, "%s(input=%d)\n", __func__, i->index);
1426
1427         return cx23885_query_audinput(file, priv, i);
1428 }
1429
1430 static int vidioc_s_audinput(struct file *file, void *priv,
1431         const struct v4l2_audio *i)
1432 {
1433         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1434         if (i->index >= 2)
1435                 return -EINVAL;
1436
1437         dprintk(1, "%s(%d)\n", __func__, i->index);
1438
1439         dev->audinput = i->index;
1440
1441         /* Skip the audio defaults from the cards struct, caller wants
1442          * directly touch the audio mux hardware. */
1443         cx23885_flatiron_mux(dev, dev->audinput + 1);
1444         return 0;
1445 }
1446
1447 static int vidioc_queryctrl(struct file *file, void *priv,
1448                                 struct v4l2_queryctrl *qctrl)
1449 {
1450         qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1451         if (unlikely(qctrl->id == 0))
1452                 return -EINVAL;
1453         return cx23885_ctrl_query(qctrl);
1454 }
1455
1456 static int vidioc_g_ctrl(struct file *file, void *priv,
1457                                 struct v4l2_control *ctl)
1458 {
1459         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1460
1461         return cx23885_get_control(dev, ctl);
1462 }
1463
1464 static int vidioc_s_ctrl(struct file *file, void *priv,
1465                                 struct v4l2_control *ctl)
1466 {
1467         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1468
1469         return cx23885_set_control(dev, ctl);
1470 }
1471
1472 static int vidioc_g_tuner(struct file *file, void *priv,
1473                                 struct v4l2_tuner *t)
1474 {
1475         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1476
1477         if (unlikely(UNSET == dev->tuner_type))
1478                 return -EINVAL;
1479         if (0 != t->index)
1480                 return -EINVAL;
1481
1482         strcpy(t->name, "Television");
1483
1484         call_all(dev, tuner, g_tuner, t);
1485         return 0;
1486 }
1487
1488 static int vidioc_s_tuner(struct file *file, void *priv,
1489                                 const struct v4l2_tuner *t)
1490 {
1491         struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1492
1493         if (UNSET == dev->tuner_type)
1494                 return -EINVAL;
1495         if (0 != t->index)
1496                 return -EINVAL;
1497         /* Update the A/V core */
1498         call_all(dev, tuner, s_tuner, t);
1499
1500         return 0;
1501 }
1502
1503 static int vidioc_g_frequency(struct file *file, void *priv,
1504                                 struct v4l2_frequency *f)
1505 {
1506         struct cx23885_fh *fh = priv;
1507         struct cx23885_dev *dev = fh->dev;
1508
1509         if (unlikely(UNSET == dev->tuner_type))
1510                 return -EINVAL;
1511
1512         /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1513         f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1514         f->frequency = dev->freq;
1515
1516         call_all(dev, tuner, g_frequency, f);
1517
1518         return 0;
1519 }
1520
1521 static int cx23885_set_freq(struct cx23885_dev *dev, const struct v4l2_frequency *f)
1522 {
1523         struct v4l2_control ctrl;
1524
1525         if (unlikely(UNSET == dev->tuner_type))
1526                 return -EINVAL;
1527         if (unlikely(f->tuner != 0))
1528                 return -EINVAL;
1529
1530         mutex_lock(&dev->lock);
1531         dev->freq = f->frequency;
1532
1533         /* I need to mute audio here */
1534         ctrl.id = V4L2_CID_AUDIO_MUTE;
1535         ctrl.value = 1;
1536         cx23885_set_control(dev, &ctrl);
1537
1538         call_all(dev, tuner, s_frequency, f);
1539
1540         /* When changing channels it is required to reset TVAUDIO */
1541         msleep(100);
1542
1543         /* I need to unmute audio here */
1544         ctrl.value = 0;
1545         cx23885_set_control(dev, &ctrl);
1546
1547         mutex_unlock(&dev->lock);
1548
1549         return 0;
1550 }
1551
1552 static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
1553         const struct v4l2_frequency *f)
1554 {
1555         struct v4l2_control ctrl;
1556         struct videobuf_dvb_frontend *vfe;
1557         struct dvb_frontend *fe;
1558
1559         struct analog_parameters params = {
1560                 .mode      = V4L2_TUNER_ANALOG_TV,
1561                 .audmode   = V4L2_TUNER_MODE_STEREO,
1562                 .std       = dev->tvnorm,
1563                 .frequency = f->frequency
1564         };
1565
1566         mutex_lock(&dev->lock);
1567         dev->freq = f->frequency;
1568
1569         /* I need to mute audio here */
1570         ctrl.id = V4L2_CID_AUDIO_MUTE;
1571         ctrl.value = 1;
1572         cx23885_set_control(dev, &ctrl);
1573
1574         /* If HVR1850 */
1575         dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
1576                 params.frequency, f->tuner, params.std);
1577
1578         vfe = videobuf_dvb_get_frontend(&dev->ts2.frontends, 1);
1579         if (!vfe) {
1580                 mutex_unlock(&dev->lock);
1581                 return -EINVAL;
1582         }
1583
1584         fe = vfe->dvb.frontend;
1585
1586         if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) ||
1587             (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) ||
1588             (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111))
1589                 fe = &dev->ts1.analog_fe;
1590
1591         if (fe && fe->ops.tuner_ops.set_analog_params) {
1592                 call_all(dev, core, s_std, dev->tvnorm);
1593                 fe->ops.tuner_ops.set_analog_params(fe, &params);
1594         }
1595         else
1596                 printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
1597
1598         /* When changing channels it is required to reset TVAUDIO */
1599         msleep(100);
1600
1601         /* I need to unmute audio here */
1602         ctrl.value = 0;
1603         cx23885_set_control(dev, &ctrl);
1604
1605         mutex_unlock(&dev->lock);
1606
1607         return 0;
1608 }
1609
1610 int cx23885_set_frequency(struct file *file, void *priv,
1611         const struct v4l2_frequency *f)
1612 {
1613         struct cx23885_fh *fh = priv;
1614         struct cx23885_dev *dev = fh->dev;
1615         int ret;
1616
1617         switch (dev->board) {
1618         case CX23885_BOARD_HAUPPAUGE_HVR1255:
1619         case CX23885_BOARD_HAUPPAUGE_HVR1255_22111:
1620         case CX23885_BOARD_HAUPPAUGE_HVR1850:
1621                 ret = cx23885_set_freq_via_ops(dev, f);
1622                 break;
1623         default:
1624                 ret = cx23885_set_freq(dev, f);
1625         }
1626
1627         return ret;
1628 }
1629
1630 static int vidioc_s_frequency(struct file *file, void *priv,
1631         const struct v4l2_frequency *f)
1632 {
1633         return cx23885_set_frequency(file, priv, f);
1634 }
1635
1636 /* ----------------------------------------------------------- */
1637
1638 static void cx23885_vid_timeout(unsigned long data)
1639 {
1640         struct cx23885_dev *dev = (struct cx23885_dev *)data;
1641         struct cx23885_dmaqueue *q = &dev->vidq;
1642         struct cx23885_buffer *buf;
1643         unsigned long flags;
1644
1645         spin_lock_irqsave(&dev->slock, flags);
1646         while (!list_empty(&q->active)) {
1647                 buf = list_entry(q->active.next,
1648                         struct cx23885_buffer, vb.queue);
1649                 list_del(&buf->vb.queue);
1650                 buf->vb.state = VIDEOBUF_ERROR;
1651                 wake_up(&buf->vb.done);
1652                 printk(KERN_ERR "%s: [%p/%d] timeout - dma=0x%08lx\n",
1653                         dev->name, buf, buf->vb.i,
1654                         (unsigned long)buf->risc.dma);
1655         }
1656         cx23885_restart_video_queue(dev, q);
1657         spin_unlock_irqrestore(&dev->slock, flags);
1658 }
1659
1660 int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1661 {
1662         u32 mask, count;
1663         int handled = 0;
1664
1665         mask   = cx_read(VID_A_INT_MSK);
1666         if (0 == (status & mask))
1667                 return handled;
1668
1669         cx_write(VID_A_INT_STAT, status);
1670
1671         /* risc op code error, fifo overflow or line sync detection error */
1672         if ((status & VID_BC_MSK_OPC_ERR) ||
1673                 (status & VID_BC_MSK_SYNC) ||
1674                 (status & VID_BC_MSK_OF)) {
1675
1676                 if (status & VID_BC_MSK_OPC_ERR) {
1677                         dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1678                                 VID_BC_MSK_OPC_ERR);
1679                         printk(KERN_WARNING "%s: video risc op code error\n",
1680                                 dev->name);
1681                         cx23885_sram_channel_dump(dev,
1682                                 &dev->sram_channels[SRAM_CH01]);
1683                 }
1684
1685                 if (status & VID_BC_MSK_SYNC)
1686                         dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) "
1687                                 "video lines miss-match\n",
1688                                 VID_BC_MSK_SYNC);
1689
1690                 if (status & VID_BC_MSK_OF)
1691                         dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
1692                                 VID_BC_MSK_OF);
1693
1694         }
1695
1696         /* Video */
1697         if (status & VID_BC_MSK_RISCI1) {
1698                 spin_lock(&dev->slock);
1699                 count = cx_read(VID_A_GPCNT);
1700                 cx23885_video_wakeup(dev, &dev->vidq, count);
1701                 spin_unlock(&dev->slock);
1702                 handled++;
1703         }
1704         if (status & VID_BC_MSK_RISCI2) {
1705                 dprintk(2, "stopper video\n");
1706                 spin_lock(&dev->slock);
1707                 cx23885_restart_video_queue(dev, &dev->vidq);
1708                 spin_unlock(&dev->slock);
1709                 handled++;
1710         }
1711
1712         /* Allow the VBI framework to process it's payload */
1713         handled += cx23885_vbi_irq(dev, status);
1714
1715         return handled;
1716 }
1717
1718 /* ----------------------------------------------------------- */
1719 /* exported stuff                                              */
1720
1721 static const struct v4l2_file_operations video_fops = {
1722         .owner         = THIS_MODULE,
1723         .open          = video_open,
1724         .release       = video_release,
1725         .read          = video_read,
1726         .poll          = video_poll,
1727         .mmap          = video_mmap,
1728         .ioctl         = video_ioctl2,
1729 };
1730
1731 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1732         .vidioc_querycap      = vidioc_querycap,
1733         .vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1734         .vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1735         .vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1736         .vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1737         .vidioc_g_fmt_vbi_cap     = cx23885_vbi_fmt,
1738         .vidioc_try_fmt_vbi_cap   = cx23885_vbi_fmt,
1739         .vidioc_s_fmt_vbi_cap     = cx23885_vbi_fmt,
1740         .vidioc_reqbufs       = vidioc_reqbufs,
1741         .vidioc_querybuf      = vidioc_querybuf,
1742         .vidioc_qbuf          = vidioc_qbuf,
1743         .vidioc_dqbuf         = vidioc_dqbuf,
1744         .vidioc_s_std         = vidioc_s_std,
1745         .vidioc_g_std         = vidioc_g_std,
1746         .vidioc_enum_input    = vidioc_enum_input,
1747         .vidioc_g_input       = vidioc_g_input,
1748         .vidioc_s_input       = vidioc_s_input,
1749         .vidioc_log_status    = vidioc_log_status,
1750         .vidioc_queryctrl     = vidioc_queryctrl,
1751         .vidioc_g_ctrl        = vidioc_g_ctrl,
1752         .vidioc_s_ctrl        = vidioc_s_ctrl,
1753         .vidioc_streamon      = vidioc_streamon,
1754         .vidioc_streamoff     = vidioc_streamoff,
1755         .vidioc_g_tuner       = vidioc_g_tuner,
1756         .vidioc_s_tuner       = vidioc_s_tuner,
1757         .vidioc_g_frequency   = vidioc_g_frequency,
1758         .vidioc_s_frequency   = vidioc_s_frequency,
1759 #ifdef CONFIG_VIDEO_ADV_DEBUG
1760         .vidioc_g_chip_info   = cx23885_g_chip_info,
1761         .vidioc_g_register    = cx23885_g_register,
1762         .vidioc_s_register    = cx23885_s_register,
1763 #endif
1764         .vidioc_enumaudio     = vidioc_enum_audinput,
1765         .vidioc_g_audio       = vidioc_g_audinput,
1766         .vidioc_s_audio       = vidioc_s_audinput,
1767 };
1768
1769 static struct video_device cx23885_vbi_template;
1770 static struct video_device cx23885_video_template = {
1771         .name                 = "cx23885-video",
1772         .fops                 = &video_fops,
1773         .ioctl_ops            = &video_ioctl_ops,
1774         .tvnorms              = CX23885_NORMS,
1775 };
1776
1777 static const struct v4l2_file_operations radio_fops = {
1778         .owner         = THIS_MODULE,
1779         .open          = video_open,
1780         .release       = video_release,
1781         .ioctl         = video_ioctl2,
1782 };
1783
1784
1785 void cx23885_video_unregister(struct cx23885_dev *dev)
1786 {
1787         dprintk(1, "%s()\n", __func__);
1788         cx23885_irq_remove(dev, 0x01);
1789
1790         if (dev->vbi_dev) {
1791                 if (video_is_registered(dev->vbi_dev))
1792                         video_unregister_device(dev->vbi_dev);
1793                 else
1794                         video_device_release(dev->vbi_dev);
1795                 dev->vbi_dev = NULL;
1796                 btcx_riscmem_free(dev->pci, &dev->vbiq.stopper);
1797         }
1798         if (dev->video_dev) {
1799                 if (video_is_registered(dev->video_dev))
1800                         video_unregister_device(dev->video_dev);
1801                 else
1802                         video_device_release(dev->video_dev);
1803                 dev->video_dev = NULL;
1804
1805                 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1806         }
1807
1808         if (dev->audio_dev)
1809                 cx23885_audio_unregister(dev);
1810 }
1811
1812 int cx23885_video_register(struct cx23885_dev *dev)
1813 {
1814         int err;
1815
1816         dprintk(1, "%s()\n", __func__);
1817         spin_lock_init(&dev->slock);
1818
1819         /* Initialize VBI template */
1820         cx23885_vbi_template = cx23885_video_template;
1821         strcpy(cx23885_vbi_template.name, "cx23885-vbi");
1822
1823         dev->tvnorm = V4L2_STD_NTSC_M;
1824
1825         /* init video dma queues */
1826         INIT_LIST_HEAD(&dev->vidq.active);
1827         INIT_LIST_HEAD(&dev->vidq.queued);
1828         dev->vidq.timeout.function = cx23885_vid_timeout;
1829         dev->vidq.timeout.data = (unsigned long)dev;
1830         init_timer(&dev->vidq.timeout);
1831         cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1832                 VID_A_DMA_CTL, 0x11, 0x00);
1833
1834         /* init vbi dma queues */
1835         INIT_LIST_HEAD(&dev->vbiq.active);
1836         INIT_LIST_HEAD(&dev->vbiq.queued);
1837         dev->vbiq.timeout.function = cx23885_vbi_timeout;
1838         dev->vbiq.timeout.data = (unsigned long)dev;
1839         init_timer(&dev->vbiq.timeout);
1840         cx23885_risc_stopper(dev->pci, &dev->vbiq.stopper,
1841                 VID_A_DMA_CTL, 0x22, 0x00);
1842
1843         cx23885_irq_add_enable(dev, 0x01);
1844
1845         if ((TUNER_ABSENT != dev->tuner_type) &&
1846                         ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
1847                 struct v4l2_subdev *sd = NULL;
1848
1849                 if (dev->tuner_addr)
1850                         sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1851                                 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
1852                                 "tuner", dev->tuner_addr, NULL);
1853                 else
1854                         sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1855                                 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
1856                                 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
1857                 if (sd) {
1858                         struct tuner_setup tun_setup;
1859
1860                         memset(&tun_setup, 0, sizeof(tun_setup));
1861                         tun_setup.mode_mask = T_ANALOG_TV;
1862                         tun_setup.type = dev->tuner_type;
1863                         tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1864                         tun_setup.tuner_callback = cx23885_tuner_callback;
1865
1866                         v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1867
1868                         if (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) {
1869                                 struct xc2028_ctrl ctrl = {
1870                                         .fname = XC2028_DEFAULT_FIRMWARE,
1871                                         .max_len = 64
1872                                 };
1873                                 struct v4l2_priv_tun_config cfg = {
1874                                         .tuner = dev->tuner_type,
1875                                         .priv = &ctrl
1876                                 };
1877                                 v4l2_subdev_call(sd, tuner, s_config, &cfg);
1878                         }
1879
1880                         if (dev->board == CX23885_BOARD_AVERMEDIA_HC81R) {
1881                                 struct xc2028_ctrl ctrl = {
1882                                         .fname = "xc3028L-v36.fw",
1883                                         .max_len = 64
1884                                 };
1885                                 struct v4l2_priv_tun_config cfg = {
1886                                         .tuner = dev->tuner_type,
1887                                         .priv = &ctrl
1888                                 };
1889                                 v4l2_subdev_call(sd, tuner, s_config, &cfg);
1890                         }
1891                 }
1892         }
1893
1894         /* register Video device */
1895         dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1896                 &cx23885_video_template, "video");
1897         err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1898                                     video_nr[dev->nr]);
1899         if (err < 0) {
1900                 printk(KERN_INFO "%s: can't register video device\n",
1901                         dev->name);
1902                 goto fail_unreg;
1903         }
1904         printk(KERN_INFO "%s: registered device %s [v4l2]\n",
1905                dev->name, video_device_node_name(dev->video_dev));
1906
1907         /* register VBI device */
1908         dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
1909                 &cx23885_vbi_template, "vbi");
1910         err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1911                                     vbi_nr[dev->nr]);
1912         if (err < 0) {
1913                 printk(KERN_INFO "%s: can't register vbi device\n",
1914                         dev->name);
1915                 goto fail_unreg;
1916         }
1917         printk(KERN_INFO "%s: registered device %s\n",
1918                dev->name, video_device_node_name(dev->vbi_dev));
1919
1920         /* Register ALSA audio device */
1921         dev->audio_dev = cx23885_audio_register(dev);
1922
1923         /* initial device configuration */
1924         mutex_lock(&dev->lock);
1925         cx23885_set_tvnorm(dev, dev->tvnorm);
1926         init_controls(dev);
1927         cx23885_video_mux(dev, 0);
1928         cx23885_audio_mux(dev, 0);
1929         mutex_unlock(&dev->lock);
1930
1931         return 0;
1932
1933 fail_unreg:
1934         cx23885_video_unregister(dev);
1935         return err;
1936 }
1937