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