5b0fbc602f3efe9bc774debfb6a341fc3ad4b4f2
[pandora-kernel.git] / drivers / media / video / cx88 / cx88-video.c
1 /*
2  *
3  * device driver for Conexant 2388x based TV cards
4  * video4linux video interface
5  *
6  * (c) 2003-04 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7  *
8  * (c) 2005-2006 Mauro Carvalho Chehab <mchehab@infradead.org>
9  *      - Multituner support
10  *      - video_ioctl2 conversion
11  *      - PAL/M fixes
12  *
13  *  This program is free software; you can redistribute it and/or modify
14  *  it under the terms of the GNU General Public License as published by
15  *  the Free Software Foundation; either version 2 of the License, or
16  *  (at your option) any later version.
17  *
18  *  This program is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *  GNU General Public License for more details.
22  *
23  *  You should have received a copy of the GNU General Public License
24  *  along with this program; if not, write to the Free Software
25  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/module.h>
31 #include <linux/kmod.h>
32 #include <linux/kernel.h>
33 #include <linux/slab.h>
34 #include <linux/interrupt.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/delay.h>
37 #include <linux/kthread.h>
38 #include <asm/div64.h>
39
40 #include "cx88.h"
41 #include <media/v4l2-common.h>
42 #include <media/v4l2-ioctl.h>
43
44 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
45 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
46 MODULE_LICENSE("GPL");
47
48 /* ------------------------------------------------------------------ */
49
50 static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
51 static unsigned int vbi_nr[]   = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
52 static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
53
54 module_param_array(video_nr, int, NULL, 0444);
55 module_param_array(vbi_nr,   int, NULL, 0444);
56 module_param_array(radio_nr, int, NULL, 0444);
57
58 MODULE_PARM_DESC(video_nr,"video device numbers");
59 MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
60 MODULE_PARM_DESC(radio_nr,"radio device numbers");
61
62 static unsigned int video_debug;
63 module_param(video_debug,int,0644);
64 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
65
66 static unsigned int irq_debug;
67 module_param(irq_debug,int,0644);
68 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
69
70 static unsigned int vid_limit = 16;
71 module_param(vid_limit,int,0644);
72 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
73
74 #define dprintk(level,fmt, arg...)      if (video_debug >= level) \
75         printk(KERN_DEBUG "%s/0: " fmt, core->name , ## arg)
76
77 /* ------------------------------------------------------------------ */
78
79 static LIST_HEAD(cx8800_devlist);
80
81 /* ------------------------------------------------------------------- */
82 /* static data                                                         */
83
84 static struct cx8800_fmt formats[] = {
85         {
86                 .name     = "8 bpp, gray",
87                 .fourcc   = V4L2_PIX_FMT_GREY,
88                 .cxformat = ColorFormatY8,
89                 .depth    = 8,
90                 .flags    = FORMAT_FLAGS_PACKED,
91         },{
92                 .name     = "15 bpp RGB, le",
93                 .fourcc   = V4L2_PIX_FMT_RGB555,
94                 .cxformat = ColorFormatRGB15,
95                 .depth    = 16,
96                 .flags    = FORMAT_FLAGS_PACKED,
97         },{
98                 .name     = "15 bpp RGB, be",
99                 .fourcc   = V4L2_PIX_FMT_RGB555X,
100                 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
101                 .depth    = 16,
102                 .flags    = FORMAT_FLAGS_PACKED,
103         },{
104                 .name     = "16 bpp RGB, le",
105                 .fourcc   = V4L2_PIX_FMT_RGB565,
106                 .cxformat = ColorFormatRGB16,
107                 .depth    = 16,
108                 .flags    = FORMAT_FLAGS_PACKED,
109         },{
110                 .name     = "16 bpp RGB, be",
111                 .fourcc   = V4L2_PIX_FMT_RGB565X,
112                 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
113                 .depth    = 16,
114                 .flags    = FORMAT_FLAGS_PACKED,
115         },{
116                 .name     = "24 bpp RGB, le",
117                 .fourcc   = V4L2_PIX_FMT_BGR24,
118                 .cxformat = ColorFormatRGB24,
119                 .depth    = 24,
120                 .flags    = FORMAT_FLAGS_PACKED,
121         },{
122                 .name     = "32 bpp RGB, le",
123                 .fourcc   = V4L2_PIX_FMT_BGR32,
124                 .cxformat = ColorFormatRGB32,
125                 .depth    = 32,
126                 .flags    = FORMAT_FLAGS_PACKED,
127         },{
128                 .name     = "32 bpp RGB, be",
129                 .fourcc   = V4L2_PIX_FMT_RGB32,
130                 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
131                 .depth    = 32,
132                 .flags    = FORMAT_FLAGS_PACKED,
133         },{
134                 .name     = "4:2:2, packed, YUYV",
135                 .fourcc   = V4L2_PIX_FMT_YUYV,
136                 .cxformat = ColorFormatYUY2,
137                 .depth    = 16,
138                 .flags    = FORMAT_FLAGS_PACKED,
139         },{
140                 .name     = "4:2:2, packed, UYVY",
141                 .fourcc   = V4L2_PIX_FMT_UYVY,
142                 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
143                 .depth    = 16,
144                 .flags    = FORMAT_FLAGS_PACKED,
145         },
146 };
147
148 static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
149 {
150         unsigned int i;
151
152         for (i = 0; i < ARRAY_SIZE(formats); i++)
153                 if (formats[i].fourcc == fourcc)
154                         return formats+i;
155         return NULL;
156 }
157
158 /* ------------------------------------------------------------------- */
159
160 static const struct v4l2_queryctrl no_ctl = {
161         .name  = "42",
162         .flags = V4L2_CTRL_FLAG_DISABLED,
163 };
164
165 static struct cx88_ctrl cx8800_ctls[] = {
166         /* --- video --- */
167         {
168                 .v = {
169                         .id            = V4L2_CID_BRIGHTNESS,
170                         .name          = "Brightness",
171                         .minimum       = 0x00,
172                         .maximum       = 0xff,
173                         .step          = 1,
174                         .default_value = 0x7f,
175                         .type          = V4L2_CTRL_TYPE_INTEGER,
176                 },
177                 .off                   = 128,
178                 .reg                   = MO_CONTR_BRIGHT,
179                 .mask                  = 0x00ff,
180                 .shift                 = 0,
181         },{
182                 .v = {
183                         .id            = V4L2_CID_CONTRAST,
184                         .name          = "Contrast",
185                         .minimum       = 0,
186                         .maximum       = 0xff,
187                         .step          = 1,
188                         .default_value = 0x3f,
189                         .type          = V4L2_CTRL_TYPE_INTEGER,
190                 },
191                 .off                   = 0,
192                 .reg                   = MO_CONTR_BRIGHT,
193                 .mask                  = 0xff00,
194                 .shift                 = 8,
195         },{
196                 .v = {
197                         .id            = V4L2_CID_HUE,
198                         .name          = "Hue",
199                         .minimum       = 0,
200                         .maximum       = 0xff,
201                         .step          = 1,
202                         .default_value = 0x7f,
203                         .type          = V4L2_CTRL_TYPE_INTEGER,
204                 },
205                 .off                   = 128,
206                 .reg                   = MO_HUE,
207                 .mask                  = 0x00ff,
208                 .shift                 = 0,
209         },{
210                 /* strictly, this only describes only U saturation.
211                  * V saturation is handled specially through code.
212                  */
213                 .v = {
214                         .id            = V4L2_CID_SATURATION,
215                         .name          = "Saturation",
216                         .minimum       = 0,
217                         .maximum       = 0xff,
218                         .step          = 1,
219                         .default_value = 0x7f,
220                         .type          = V4L2_CTRL_TYPE_INTEGER,
221                 },
222                 .off                   = 0,
223                 .reg                   = MO_UV_SATURATION,
224                 .mask                  = 0x00ff,
225                 .shift                 = 0,
226         },{
227                 .v = {
228                         .id            = V4L2_CID_CHROMA_AGC,
229                         .name          = "Chroma AGC",
230                         .minimum       = 0,
231                         .maximum       = 1,
232                         .default_value = 0x1,
233                         .type          = V4L2_CTRL_TYPE_BOOLEAN,
234                 },
235                 .reg                   = MO_INPUT_FORMAT,
236                 .mask                  = 1 << 10,
237                 .shift                 = 10,
238         }, {
239                 .v = {
240                         .id            = V4L2_CID_COLOR_KILLER,
241                         .name          = "Color killer",
242                         .minimum       = 0,
243                         .maximum       = 1,
244                         .default_value = 0x1,
245                         .type          = V4L2_CTRL_TYPE_BOOLEAN,
246                 },
247                 .reg                   = MO_INPUT_FORMAT,
248                 .mask                  = 1 << 9,
249                 .shift                 = 9,
250         }, {
251         /* --- audio --- */
252                 .v = {
253                         .id            = V4L2_CID_AUDIO_MUTE,
254                         .name          = "Mute",
255                         .minimum       = 0,
256                         .maximum       = 1,
257                         .default_value = 1,
258                         .type          = V4L2_CTRL_TYPE_BOOLEAN,
259                 },
260                 .reg                   = AUD_VOL_CTL,
261                 .sreg                  = SHADOW_AUD_VOL_CTL,
262                 .mask                  = (1 << 6),
263                 .shift                 = 6,
264         },{
265                 .v = {
266                         .id            = V4L2_CID_AUDIO_VOLUME,
267                         .name          = "Volume",
268                         .minimum       = 0,
269                         .maximum       = 0x3f,
270                         .step          = 1,
271                         .default_value = 0x3f,
272                         .type          = V4L2_CTRL_TYPE_INTEGER,
273                 },
274                 .reg                   = AUD_VOL_CTL,
275                 .sreg                  = SHADOW_AUD_VOL_CTL,
276                 .mask                  = 0x3f,
277                 .shift                 = 0,
278         },{
279                 .v = {
280                         .id            = V4L2_CID_AUDIO_BALANCE,
281                         .name          = "Balance",
282                         .minimum       = 0,
283                         .maximum       = 0x7f,
284                         .step          = 1,
285                         .default_value = 0x40,
286                         .type          = V4L2_CTRL_TYPE_INTEGER,
287                 },
288                 .reg                   = AUD_BAL_CTL,
289                 .sreg                  = SHADOW_AUD_BAL_CTL,
290                 .mask                  = 0x7f,
291                 .shift                 = 0,
292         }
293 };
294 static const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
295
296 /* Must be sorted from low to high control ID! */
297 const u32 cx88_user_ctrls[] = {
298         V4L2_CID_USER_CLASS,
299         V4L2_CID_BRIGHTNESS,
300         V4L2_CID_CONTRAST,
301         V4L2_CID_SATURATION,
302         V4L2_CID_HUE,
303         V4L2_CID_AUDIO_VOLUME,
304         V4L2_CID_AUDIO_BALANCE,
305         V4L2_CID_AUDIO_MUTE,
306         V4L2_CID_CHROMA_AGC,
307         V4L2_CID_COLOR_KILLER,
308         0
309 };
310 EXPORT_SYMBOL(cx88_user_ctrls);
311
312 static const u32 *ctrl_classes[] = {
313         cx88_user_ctrls,
314         NULL
315 };
316
317 int cx8800_ctrl_query(struct cx88_core *core, struct v4l2_queryctrl *qctrl)
318 {
319         int i;
320
321         if (qctrl->id < V4L2_CID_BASE ||
322             qctrl->id >= V4L2_CID_LASTP1)
323                 return -EINVAL;
324         for (i = 0; i < CX8800_CTLS; i++)
325                 if (cx8800_ctls[i].v.id == qctrl->id)
326                         break;
327         if (i == CX8800_CTLS) {
328                 *qctrl = no_ctl;
329                 return 0;
330         }
331         *qctrl = cx8800_ctls[i].v;
332         /* Report chroma AGC as inactive when SECAM is selected */
333         if (cx8800_ctls[i].v.id == V4L2_CID_CHROMA_AGC &&
334             core->tvnorm & V4L2_STD_SECAM)
335                 qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
336
337         return 0;
338 }
339 EXPORT_SYMBOL(cx8800_ctrl_query);
340
341 /* ------------------------------------------------------------------- */
342 /* resource management                                                 */
343
344 static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
345 {
346         struct cx88_core *core = dev->core;
347         if (fh->resources & bit)
348                 /* have it already allocated */
349                 return 1;
350
351         /* is it free? */
352         mutex_lock(&core->lock);
353         if (dev->resources & bit) {
354                 /* no, someone else uses it */
355                 mutex_unlock(&core->lock);
356                 return 0;
357         }
358         /* it's free, grab it */
359         fh->resources  |= bit;
360         dev->resources |= bit;
361         dprintk(1,"res: get %d\n",bit);
362         mutex_unlock(&core->lock);
363         return 1;
364 }
365
366 static
367 int res_check(struct cx8800_fh *fh, unsigned int bit)
368 {
369         return (fh->resources & bit);
370 }
371
372 static
373 int res_locked(struct cx8800_dev *dev, unsigned int bit)
374 {
375         return (dev->resources & bit);
376 }
377
378 static
379 void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
380 {
381         struct cx88_core *core = dev->core;
382         BUG_ON((fh->resources & bits) != bits);
383
384         mutex_lock(&core->lock);
385         fh->resources  &= ~bits;
386         dev->resources &= ~bits;
387         dprintk(1,"res: put %d\n",bits);
388         mutex_unlock(&core->lock);
389 }
390
391 /* ------------------------------------------------------------------ */
392
393 int cx88_video_mux(struct cx88_core *core, unsigned int input)
394 {
395         /* struct cx88_core *core = dev->core; */
396
397         dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
398                 input, INPUT(input).vmux,
399                 INPUT(input).gpio0,INPUT(input).gpio1,
400                 INPUT(input).gpio2,INPUT(input).gpio3);
401         core->input = input;
402         cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input).vmux << 14);
403         cx_write(MO_GP3_IO, INPUT(input).gpio3);
404         cx_write(MO_GP0_IO, INPUT(input).gpio0);
405         cx_write(MO_GP1_IO, INPUT(input).gpio1);
406         cx_write(MO_GP2_IO, INPUT(input).gpio2);
407
408         switch (INPUT(input).type) {
409         case CX88_VMUX_SVIDEO:
410                 cx_set(MO_AFECFG_IO,    0x00000001);
411                 cx_set(MO_INPUT_FORMAT, 0x00010010);
412                 cx_set(MO_FILTER_EVEN,  0x00002020);
413                 cx_set(MO_FILTER_ODD,   0x00002020);
414                 break;
415         default:
416                 cx_clear(MO_AFECFG_IO,    0x00000001);
417                 cx_clear(MO_INPUT_FORMAT, 0x00010010);
418                 cx_clear(MO_FILTER_EVEN,  0x00002020);
419                 cx_clear(MO_FILTER_ODD,   0x00002020);
420                 break;
421         }
422
423         /* if there are audioroutes defined, we have an external
424            ADC to deal with audio */
425         if (INPUT(input).audioroute) {
426                 /* The wm8775 module has the "2" route hardwired into
427                    the initialization. Some boards may use different
428                    routes for different inputs. HVR-1300 surely does */
429                 if (core->board.audio_chip &&
430                     core->board.audio_chip == V4L2_IDENT_WM8775) {
431                         struct v4l2_routing route;
432
433                         route.input = INPUT(input).audioroute;
434                         cx88_call_i2c_clients(core,
435                                 VIDIOC_INT_S_AUDIO_ROUTING, &route);
436                 }
437                 /* cx2388's C-ADC is connected to the tuner only.
438                    When used with S-Video, that ADC is busy dealing with
439                    chroma, so an external must be used for baseband audio */
440                 if (INPUT(input).type != CX88_VMUX_TELEVISION ) {
441                         /* "I2S ADC mode" */
442                         core->tvaudio = WW_I2SADC;
443                         cx88_set_tvaudio(core);
444                 } else {
445                         /* Normal mode */
446                         cx_write(AUD_I2SCNTL, 0x0);
447                         cx_clear(AUD_CTL, EN_I2SIN_ENABLE);
448                 }
449         }
450
451         return 0;
452 }
453 EXPORT_SYMBOL(cx88_video_mux);
454
455 /* ------------------------------------------------------------------ */
456
457 static int start_video_dma(struct cx8800_dev    *dev,
458                            struct cx88_dmaqueue *q,
459                            struct cx88_buffer   *buf)
460 {
461         struct cx88_core *core = dev->core;
462
463         /* setup fifo + format */
464         cx88_sram_channel_setup(core, &cx88_sram_channels[SRAM_CH21],
465                                 buf->bpl, buf->risc.dma);
466         cx88_set_scale(core, buf->vb.width, buf->vb.height, buf->vb.field);
467         cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
468
469         /* reset counter */
470         cx_write(MO_VIDY_GPCNTRL,GP_COUNT_CONTROL_RESET);
471         q->count = 1;
472
473         /* enable irqs */
474         cx_set(MO_PCI_INTMSK, core->pci_irqmask | PCI_INT_VIDINT);
475
476         /* Enables corresponding bits at PCI_INT_STAT:
477                 bits 0 to 4: video, audio, transport stream, VIP, Host
478                 bit 7: timer
479                 bits 8 and 9: DMA complete for: SRC, DST
480                 bits 10 and 11: BERR signal asserted for RISC: RD, WR
481                 bits 12 to 15: BERR signal asserted for: BRDG, SRC, DST, IPB
482          */
483         cx_set(MO_VID_INTMSK, 0x0f0011);
484
485         /* enable capture */
486         cx_set(VID_CAPTURE_CONTROL,0x06);
487
488         /* start dma */
489         cx_set(MO_DEV_CNTRL2, (1<<5));
490         cx_set(MO_VID_DMACNTRL, 0x11); /* Planar Y and packed FIFO and RISC enable */
491
492         return 0;
493 }
494
495 #ifdef CONFIG_PM
496 static int stop_video_dma(struct cx8800_dev    *dev)
497 {
498         struct cx88_core *core = dev->core;
499
500         /* stop dma */
501         cx_clear(MO_VID_DMACNTRL, 0x11);
502
503         /* disable capture */
504         cx_clear(VID_CAPTURE_CONTROL,0x06);
505
506         /* disable irqs */
507         cx_clear(MO_PCI_INTMSK, PCI_INT_VIDINT);
508         cx_clear(MO_VID_INTMSK, 0x0f0011);
509         return 0;
510 }
511 #endif
512
513 static int restart_video_queue(struct cx8800_dev    *dev,
514                                struct cx88_dmaqueue *q)
515 {
516         struct cx88_core *core = dev->core;
517         struct cx88_buffer *buf, *prev;
518
519         if (!list_empty(&q->active)) {
520                 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
521                 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
522                         buf, buf->vb.i);
523                 start_video_dma(dev, q, buf);
524                 list_for_each_entry(buf, &q->active, vb.queue)
525                         buf->count = q->count++;
526                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
527                 return 0;
528         }
529
530         prev = NULL;
531         for (;;) {
532                 if (list_empty(&q->queued))
533                         return 0;
534                 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
535                 if (NULL == prev) {
536                         list_move_tail(&buf->vb.queue, &q->active);
537                         start_video_dma(dev, q, buf);
538                         buf->vb.state = VIDEOBUF_ACTIVE;
539                         buf->count    = q->count++;
540                         mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
541                         dprintk(2,"[%p/%d] restart_queue - first active\n",
542                                 buf,buf->vb.i);
543
544                 } else if (prev->vb.width  == buf->vb.width  &&
545                            prev->vb.height == buf->vb.height &&
546                            prev->fmt       == buf->fmt) {
547                         list_move_tail(&buf->vb.queue, &q->active);
548                         buf->vb.state = VIDEOBUF_ACTIVE;
549                         buf->count    = q->count++;
550                         prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
551                         dprintk(2,"[%p/%d] restart_queue - move to active\n",
552                                 buf,buf->vb.i);
553                 } else {
554                         return 0;
555                 }
556                 prev = buf;
557         }
558 }
559
560 /* ------------------------------------------------------------------ */
561
562 static int
563 buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
564 {
565         struct cx8800_fh *fh = q->priv_data;
566
567         *size = fh->fmt->depth*fh->width*fh->height >> 3;
568         if (0 == *count)
569                 *count = 32;
570         while (*size * *count > vid_limit * 1024 * 1024)
571                 (*count)--;
572         return 0;
573 }
574
575 static int
576 buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
577                enum v4l2_field field)
578 {
579         struct cx8800_fh   *fh  = q->priv_data;
580         struct cx8800_dev  *dev = fh->dev;
581         struct cx88_core *core = dev->core;
582         struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
583         struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
584         int rc, init_buffer = 0;
585
586         BUG_ON(NULL == fh->fmt);
587         if (fh->width  < 48 || fh->width  > norm_maxw(core->tvnorm) ||
588             fh->height < 32 || fh->height > norm_maxh(core->tvnorm))
589                 return -EINVAL;
590         buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
591         if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
592                 return -EINVAL;
593
594         if (buf->fmt       != fh->fmt    ||
595             buf->vb.width  != fh->width  ||
596             buf->vb.height != fh->height ||
597             buf->vb.field  != field) {
598                 buf->fmt       = fh->fmt;
599                 buf->vb.width  = fh->width;
600                 buf->vb.height = fh->height;
601                 buf->vb.field  = field;
602                 init_buffer = 1;
603         }
604
605         if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
606                 init_buffer = 1;
607                 if (0 != (rc = videobuf_iolock(q,&buf->vb,NULL)))
608                         goto fail;
609         }
610
611         if (init_buffer) {
612                 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
613                 switch (buf->vb.field) {
614                 case V4L2_FIELD_TOP:
615                         cx88_risc_buffer(dev->pci, &buf->risc,
616                                          dma->sglist, 0, UNSET,
617                                          buf->bpl, 0, buf->vb.height);
618                         break;
619                 case V4L2_FIELD_BOTTOM:
620                         cx88_risc_buffer(dev->pci, &buf->risc,
621                                          dma->sglist, UNSET, 0,
622                                          buf->bpl, 0, buf->vb.height);
623                         break;
624                 case V4L2_FIELD_INTERLACED:
625                         cx88_risc_buffer(dev->pci, &buf->risc,
626                                          dma->sglist, 0, buf->bpl,
627                                          buf->bpl, buf->bpl,
628                                          buf->vb.height >> 1);
629                         break;
630                 case V4L2_FIELD_SEQ_TB:
631                         cx88_risc_buffer(dev->pci, &buf->risc,
632                                          dma->sglist,
633                                          0, buf->bpl * (buf->vb.height >> 1),
634                                          buf->bpl, 0,
635                                          buf->vb.height >> 1);
636                         break;
637                 case V4L2_FIELD_SEQ_BT:
638                         cx88_risc_buffer(dev->pci, &buf->risc,
639                                          dma->sglist,
640                                          buf->bpl * (buf->vb.height >> 1), 0,
641                                          buf->bpl, 0,
642                                          buf->vb.height >> 1);
643                         break;
644                 default:
645                         BUG();
646                 }
647         }
648         dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
649                 buf, buf->vb.i,
650                 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
651                 (unsigned long)buf->risc.dma);
652
653         buf->vb.state = VIDEOBUF_PREPARED;
654         return 0;
655
656  fail:
657         cx88_free_buffer(q,buf);
658         return rc;
659 }
660
661 static void
662 buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
663 {
664         struct cx88_buffer    *buf = container_of(vb,struct cx88_buffer,vb);
665         struct cx88_buffer    *prev;
666         struct cx8800_fh      *fh   = vq->priv_data;
667         struct cx8800_dev     *dev  = fh->dev;
668         struct cx88_core      *core = dev->core;
669         struct cx88_dmaqueue  *q    = &dev->vidq;
670
671         /* add jump to stopper */
672         buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
673         buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
674
675         if (!list_empty(&q->queued)) {
676                 list_add_tail(&buf->vb.queue,&q->queued);
677                 buf->vb.state = VIDEOBUF_QUEUED;
678                 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
679                         buf, buf->vb.i);
680
681         } else if (list_empty(&q->active)) {
682                 list_add_tail(&buf->vb.queue,&q->active);
683                 start_video_dma(dev, q, buf);
684                 buf->vb.state = VIDEOBUF_ACTIVE;
685                 buf->count    = q->count++;
686                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
687                 dprintk(2,"[%p/%d] buffer_queue - first active\n",
688                         buf, buf->vb.i);
689
690         } else {
691                 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
692                 if (prev->vb.width  == buf->vb.width  &&
693                     prev->vb.height == buf->vb.height &&
694                     prev->fmt       == buf->fmt) {
695                         list_add_tail(&buf->vb.queue,&q->active);
696                         buf->vb.state = VIDEOBUF_ACTIVE;
697                         buf->count    = q->count++;
698                         prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
699                         dprintk(2,"[%p/%d] buffer_queue - append to active\n",
700                                 buf, buf->vb.i);
701
702                 } else {
703                         list_add_tail(&buf->vb.queue,&q->queued);
704                         buf->vb.state = VIDEOBUF_QUEUED;
705                         dprintk(2,"[%p/%d] buffer_queue - first queued\n",
706                                 buf, buf->vb.i);
707                 }
708         }
709 }
710
711 static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
712 {
713         struct cx88_buffer *buf = container_of(vb,struct cx88_buffer,vb);
714
715         cx88_free_buffer(q,buf);
716 }
717
718 static struct videobuf_queue_ops cx8800_video_qops = {
719         .buf_setup    = buffer_setup,
720         .buf_prepare  = buffer_prepare,
721         .buf_queue    = buffer_queue,
722         .buf_release  = buffer_release,
723 };
724
725 /* ------------------------------------------------------------------ */
726
727
728 /* ------------------------------------------------------------------ */
729
730 static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
731 {
732         switch (fh->type) {
733         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
734                 return &fh->vidq;
735         case V4L2_BUF_TYPE_VBI_CAPTURE:
736                 return &fh->vbiq;
737         default:
738                 BUG();
739                 return NULL;
740         }
741 }
742
743 static int get_ressource(struct cx8800_fh *fh)
744 {
745         switch (fh->type) {
746         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
747                 return RESOURCE_VIDEO;
748         case V4L2_BUF_TYPE_VBI_CAPTURE:
749                 return RESOURCE_VBI;
750         default:
751                 BUG();
752                 return 0;
753         }
754 }
755
756 static int video_open(struct file *file)
757 {
758         int minor = video_devdata(file)->minor;
759         struct cx8800_dev *h,*dev = NULL;
760         struct cx88_core *core;
761         struct cx8800_fh *fh;
762         enum v4l2_buf_type type = 0;
763         int radio = 0;
764
765         lock_kernel();
766         list_for_each_entry(h, &cx8800_devlist, devlist) {
767                 if (h->video_dev->minor == minor) {
768                         dev  = h;
769                         type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
770                 }
771                 if (h->vbi_dev->minor == minor) {
772                         dev  = h;
773                         type = V4L2_BUF_TYPE_VBI_CAPTURE;
774                 }
775                 if (h->radio_dev &&
776                     h->radio_dev->minor == minor) {
777                         radio = 1;
778                         dev   = h;
779                 }
780         }
781         if (NULL == dev) {
782                 unlock_kernel();
783                 return -ENODEV;
784         }
785
786         core = dev->core;
787
788         dprintk(1,"open minor=%d radio=%d type=%s\n",
789                 minor,radio,v4l2_type_names[type]);
790
791         /* allocate + initialize per filehandle data */
792         fh = kzalloc(sizeof(*fh),GFP_KERNEL);
793         if (NULL == fh) {
794                 unlock_kernel();
795                 return -ENOMEM;
796         }
797         file->private_data = fh;
798         fh->dev      = dev;
799         fh->radio    = radio;
800         fh->type     = type;
801         fh->width    = 320;
802         fh->height   = 240;
803         fh->fmt      = format_by_fourcc(V4L2_PIX_FMT_BGR24);
804
805         videobuf_queue_sg_init(&fh->vidq, &cx8800_video_qops,
806                             &dev->pci->dev, &dev->slock,
807                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
808                             V4L2_FIELD_INTERLACED,
809                             sizeof(struct cx88_buffer),
810                             fh);
811         videobuf_queue_sg_init(&fh->vbiq, &cx8800_vbi_qops,
812                             &dev->pci->dev, &dev->slock,
813                             V4L2_BUF_TYPE_VBI_CAPTURE,
814                             V4L2_FIELD_SEQ_TB,
815                             sizeof(struct cx88_buffer),
816                             fh);
817
818         if (fh->radio) {
819                 dprintk(1,"video_open: setting radio device\n");
820                 cx_write(MO_GP3_IO, core->board.radio.gpio3);
821                 cx_write(MO_GP0_IO, core->board.radio.gpio0);
822                 cx_write(MO_GP1_IO, core->board.radio.gpio1);
823                 cx_write(MO_GP2_IO, core->board.radio.gpio2);
824                 if (core->board.radio.audioroute) {
825                         if(core->board.audio_chip &&
826                                 core->board.audio_chip == V4L2_IDENT_WM8775) {
827                                 struct v4l2_routing route;
828
829                                 route.input = core->board.radio.audioroute;
830                                 cx88_call_i2c_clients(core,
831                                         VIDIOC_INT_S_AUDIO_ROUTING, &route);
832                         }
833                         /* "I2S ADC mode" */
834                         core->tvaudio = WW_I2SADC;
835                         cx88_set_tvaudio(core);
836                 } else {
837                         /* FM Mode */
838                         core->tvaudio = WW_FM;
839                         cx88_set_tvaudio(core);
840                         cx88_set_stereo(core,V4L2_TUNER_MODE_STEREO,1);
841                 }
842                 cx88_call_i2c_clients(core,AUDC_SET_RADIO,NULL);
843         }
844         unlock_kernel();
845
846         atomic_inc(&core->users);
847
848         return 0;
849 }
850
851 static ssize_t
852 video_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
853 {
854         struct cx8800_fh *fh = file->private_data;
855
856         switch (fh->type) {
857         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
858                 if (res_locked(fh->dev,RESOURCE_VIDEO))
859                         return -EBUSY;
860                 return videobuf_read_one(&fh->vidq, data, count, ppos,
861                                          file->f_flags & O_NONBLOCK);
862         case V4L2_BUF_TYPE_VBI_CAPTURE:
863                 if (!res_get(fh->dev,fh,RESOURCE_VBI))
864                         return -EBUSY;
865                 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
866                                             file->f_flags & O_NONBLOCK);
867         default:
868                 BUG();
869                 return 0;
870         }
871 }
872
873 static unsigned int
874 video_poll(struct file *file, struct poll_table_struct *wait)
875 {
876         struct cx8800_fh *fh = file->private_data;
877         struct cx88_buffer *buf;
878
879         if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
880                 if (!res_get(fh->dev,fh,RESOURCE_VBI))
881                         return POLLERR;
882                 return videobuf_poll_stream(file, &fh->vbiq, wait);
883         }
884
885         if (res_check(fh,RESOURCE_VIDEO)) {
886                 /* streaming capture */
887                 if (list_empty(&fh->vidq.stream))
888                         return POLLERR;
889                 buf = list_entry(fh->vidq.stream.next,struct cx88_buffer,vb.stream);
890         } else {
891                 /* read() capture */
892                 buf = (struct cx88_buffer*)fh->vidq.read_buf;
893                 if (NULL == buf)
894                         return POLLERR;
895         }
896         poll_wait(file, &buf->vb.done, wait);
897         if (buf->vb.state == VIDEOBUF_DONE ||
898             buf->vb.state == VIDEOBUF_ERROR)
899                 return POLLIN|POLLRDNORM;
900         return 0;
901 }
902
903 static int video_release(struct file *file)
904 {
905         struct cx8800_fh  *fh  = file->private_data;
906         struct cx8800_dev *dev = fh->dev;
907
908         /* turn off overlay */
909         if (res_check(fh, RESOURCE_OVERLAY)) {
910                 /* FIXME */
911                 res_free(dev,fh,RESOURCE_OVERLAY);
912         }
913
914         /* stop video capture */
915         if (res_check(fh, RESOURCE_VIDEO)) {
916                 videobuf_queue_cancel(&fh->vidq);
917                 res_free(dev,fh,RESOURCE_VIDEO);
918         }
919         if (fh->vidq.read_buf) {
920                 buffer_release(&fh->vidq,fh->vidq.read_buf);
921                 kfree(fh->vidq.read_buf);
922         }
923
924         /* stop vbi capture */
925         if (res_check(fh, RESOURCE_VBI)) {
926                 videobuf_stop(&fh->vbiq);
927                 res_free(dev,fh,RESOURCE_VBI);
928         }
929
930         videobuf_mmap_free(&fh->vidq);
931         videobuf_mmap_free(&fh->vbiq);
932         file->private_data = NULL;
933         kfree(fh);
934
935         if(atomic_dec_and_test(&dev->core->users))
936                 cx88_call_i2c_clients (dev->core, TUNER_SET_STANDBY, NULL);
937
938         return 0;
939 }
940
941 static int
942 video_mmap(struct file *file, struct vm_area_struct * vma)
943 {
944         struct cx8800_fh *fh = file->private_data;
945
946         return videobuf_mmap_mapper(get_queue(fh), vma);
947 }
948
949 /* ------------------------------------------------------------------ */
950 /* VIDEO CTRL IOCTLS                                                  */
951
952 int cx88_get_control (struct cx88_core  *core, struct v4l2_control *ctl)
953 {
954         struct cx88_ctrl  *c    = NULL;
955         u32 value;
956         int i;
957
958         for (i = 0; i < CX8800_CTLS; i++)
959                 if (cx8800_ctls[i].v.id == ctl->id)
960                         c = &cx8800_ctls[i];
961         if (unlikely(NULL == c))
962                 return -EINVAL;
963
964         value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
965         switch (ctl->id) {
966         case V4L2_CID_AUDIO_BALANCE:
967                 ctl->value = ((value & 0x7f) < 0x40) ? ((value & 0x7f) + 0x40)
968                                         : (0x7f - (value & 0x7f));
969                 break;
970         case V4L2_CID_AUDIO_VOLUME:
971                 ctl->value = 0x3f - (value & 0x3f);
972                 break;
973         default:
974                 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
975                 break;
976         }
977         dprintk(1,"get_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
978                                 ctl->id, c->v.name, ctl->value, c->reg,
979                                 value,c->mask, c->sreg ? " [shadowed]" : "");
980         return 0;
981 }
982 EXPORT_SYMBOL(cx88_get_control);
983
984 int cx88_set_control(struct cx88_core *core, struct v4l2_control *ctl)
985 {
986         struct cx88_ctrl *c = NULL;
987         u32 value,mask;
988         int i;
989
990         for (i = 0; i < CX8800_CTLS; i++) {
991                 if (cx8800_ctls[i].v.id == ctl->id) {
992                         c = &cx8800_ctls[i];
993                 }
994         }
995         if (unlikely(NULL == c))
996                 return -EINVAL;
997
998         if (ctl->value < c->v.minimum)
999                 ctl->value = c->v.minimum;
1000         if (ctl->value > c->v.maximum)
1001                 ctl->value = c->v.maximum;
1002         mask=c->mask;
1003         switch (ctl->id) {
1004         case V4L2_CID_AUDIO_BALANCE:
1005                 value = (ctl->value < 0x40) ? (0x7f - ctl->value) : (ctl->value - 0x40);
1006                 break;
1007         case V4L2_CID_AUDIO_VOLUME:
1008                 value = 0x3f - (ctl->value & 0x3f);
1009                 break;
1010         case V4L2_CID_SATURATION:
1011                 /* special v_sat handling */
1012
1013                 value = ((ctl->value - c->off) << c->shift) & c->mask;
1014
1015                 if (core->tvnorm & V4L2_STD_SECAM) {
1016                         /* For SECAM, both U and V sat should be equal */
1017                         value=value<<8|value;
1018                 } else {
1019                         /* Keeps U Saturation proportional to V Sat */
1020                         value=(value*0x5a)/0x7f<<8|value;
1021                 }
1022                 mask=0xffff;
1023                 break;
1024         case V4L2_CID_CHROMA_AGC:
1025                 /* Do not allow chroma AGC to be enabled for SECAM */
1026                 value = ((ctl->value - c->off) << c->shift) & c->mask;
1027                 if (core->tvnorm & V4L2_STD_SECAM && value)
1028                         return -EINVAL;
1029                 break;
1030         default:
1031                 value = ((ctl->value - c->off) << c->shift) & c->mask;
1032                 break;
1033         }
1034         dprintk(1,"set_control id=0x%X(%s) ctrl=0x%02x, reg=0x%02x val=0x%02x (mask 0x%02x)%s\n",
1035                                 ctl->id, c->v.name, ctl->value, c->reg, value,
1036                                 mask, c->sreg ? " [shadowed]" : "");
1037         if (c->sreg) {
1038                 cx_sandor(c->sreg, c->reg, mask, value);
1039         } else {
1040                 cx_andor(c->reg, mask, value);
1041         }
1042         return 0;
1043 }
1044 EXPORT_SYMBOL(cx88_set_control);
1045
1046 static void init_controls(struct cx88_core *core)
1047 {
1048         struct v4l2_control ctrl;
1049         int i;
1050
1051         for (i = 0; i < CX8800_CTLS; i++) {
1052                 ctrl.id=cx8800_ctls[i].v.id;
1053                 ctrl.value=cx8800_ctls[i].v.default_value;
1054
1055                 cx88_set_control(core, &ctrl);
1056         }
1057 }
1058
1059 /* ------------------------------------------------------------------ */
1060 /* VIDEO IOCTLS                                                       */
1061
1062 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
1063                                         struct v4l2_format *f)
1064 {
1065         struct cx8800_fh  *fh   = priv;
1066
1067         f->fmt.pix.width        = fh->width;
1068         f->fmt.pix.height       = fh->height;
1069         f->fmt.pix.field        = fh->vidq.field;
1070         f->fmt.pix.pixelformat  = fh->fmt->fourcc;
1071         f->fmt.pix.bytesperline =
1072                 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1073         f->fmt.pix.sizeimage =
1074                 f->fmt.pix.height * f->fmt.pix.bytesperline;
1075         return 0;
1076 }
1077
1078 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
1079                         struct v4l2_format *f)
1080 {
1081         struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1082         struct cx8800_fmt *fmt;
1083         enum v4l2_field   field;
1084         unsigned int      maxw, maxh;
1085
1086         fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1087         if (NULL == fmt)
1088                 return -EINVAL;
1089
1090         field = f->fmt.pix.field;
1091         maxw  = norm_maxw(core->tvnorm);
1092         maxh  = norm_maxh(core->tvnorm);
1093
1094         if (V4L2_FIELD_ANY == field) {
1095                 field = (f->fmt.pix.height > maxh/2)
1096                         ? V4L2_FIELD_INTERLACED
1097                         : V4L2_FIELD_BOTTOM;
1098         }
1099
1100         switch (field) {
1101         case V4L2_FIELD_TOP:
1102         case V4L2_FIELD_BOTTOM:
1103                 maxh = maxh / 2;
1104                 break;
1105         case V4L2_FIELD_INTERLACED:
1106                 break;
1107         default:
1108                 return -EINVAL;
1109         }
1110
1111         f->fmt.pix.field = field;
1112         if (f->fmt.pix.height < 32)
1113                 f->fmt.pix.height = 32;
1114         if (f->fmt.pix.height > maxh)
1115                 f->fmt.pix.height = maxh;
1116         if (f->fmt.pix.width < 48)
1117                 f->fmt.pix.width = 48;
1118         if (f->fmt.pix.width > maxw)
1119                 f->fmt.pix.width = maxw;
1120         f->fmt.pix.width &= ~0x03;
1121         f->fmt.pix.bytesperline =
1122                 (f->fmt.pix.width * fmt->depth) >> 3;
1123         f->fmt.pix.sizeimage =
1124                 f->fmt.pix.height * f->fmt.pix.bytesperline;
1125
1126         return 0;
1127 }
1128
1129 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1130                                         struct v4l2_format *f)
1131 {
1132         struct cx8800_fh  *fh   = priv;
1133         int err = vidioc_try_fmt_vid_cap (file,priv,f);
1134
1135         if (0 != err)
1136                 return err;
1137         fh->fmt        = format_by_fourcc(f->fmt.pix.pixelformat);
1138         fh->width      = f->fmt.pix.width;
1139         fh->height     = f->fmt.pix.height;
1140         fh->vidq.field = f->fmt.pix.field;
1141         return 0;
1142 }
1143
1144 static int vidioc_querycap (struct file *file, void  *priv,
1145                                         struct v4l2_capability *cap)
1146 {
1147         struct cx8800_dev *dev  = ((struct cx8800_fh *)priv)->dev;
1148         struct cx88_core  *core = dev->core;
1149
1150         strcpy(cap->driver, "cx8800");
1151         strlcpy(cap->card, core->board.name, sizeof(cap->card));
1152         sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1153         cap->version = CX88_VERSION_CODE;
1154         cap->capabilities =
1155                 V4L2_CAP_VIDEO_CAPTURE |
1156                 V4L2_CAP_READWRITE     |
1157                 V4L2_CAP_STREAMING     |
1158                 V4L2_CAP_VBI_CAPTURE;
1159         if (UNSET != core->board.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,sizeof(f->description));
1171         f->pixelformat = formats[f->index].fourcc;
1172
1173         return 0;
1174 }
1175
1176 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1177 static int vidiocgmbuf (struct file *file, void *priv, struct video_mbuf *mbuf)
1178 {
1179         struct cx8800_fh           *fh  = priv;
1180
1181         return videobuf_cgmbuf (get_queue(fh), mbuf, 8);
1182 }
1183 #endif
1184
1185 static int vidioc_reqbufs (struct file *file, void *priv, struct v4l2_requestbuffers *p)
1186 {
1187         struct cx8800_fh  *fh   = priv;
1188         return (videobuf_reqbufs(get_queue(fh), p));
1189 }
1190
1191 static int vidioc_querybuf (struct file *file, void *priv, struct v4l2_buffer *p)
1192 {
1193         struct cx8800_fh  *fh   = priv;
1194         return (videobuf_querybuf(get_queue(fh), p));
1195 }
1196
1197 static int vidioc_qbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1198 {
1199         struct cx8800_fh  *fh   = priv;
1200         return (videobuf_qbuf(get_queue(fh), p));
1201 }
1202
1203 static int vidioc_dqbuf (struct file *file, void *priv, struct v4l2_buffer *p)
1204 {
1205         struct cx8800_fh  *fh   = priv;
1206         return (videobuf_dqbuf(get_queue(fh), p,
1207                                 file->f_flags & O_NONBLOCK));
1208 }
1209
1210 static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
1211 {
1212         struct cx8800_fh  *fh   = priv;
1213         struct cx8800_dev *dev  = fh->dev;
1214
1215         /* We should remember that this driver also supports teletext,  */
1216         /* so we have to test if the v4l2_buf_type is VBI capture data. */
1217         if (unlikely((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1218                      (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)))
1219                 return -EINVAL;
1220
1221         if (unlikely(i != fh->type))
1222                 return -EINVAL;
1223
1224         if (unlikely(!res_get(dev,fh,get_ressource(fh))))
1225                 return -EBUSY;
1226         return videobuf_streamon(get_queue(fh));
1227 }
1228
1229 static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1230 {
1231         struct cx8800_fh  *fh   = priv;
1232         struct cx8800_dev *dev  = fh->dev;
1233         int               err, res;
1234
1235         if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1236             (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
1237                 return -EINVAL;
1238
1239         if (i != fh->type)
1240                 return -EINVAL;
1241
1242         res = get_ressource(fh);
1243         err = videobuf_streamoff(get_queue(fh));
1244         if (err < 0)
1245                 return err;
1246         res_free(dev,fh,res);
1247         return 0;
1248 }
1249
1250 static int vidioc_s_std (struct file *file, void *priv, v4l2_std_id *tvnorms)
1251 {
1252         struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1253
1254         mutex_lock(&core->lock);
1255         cx88_set_tvnorm(core,*tvnorms);
1256         mutex_unlock(&core->lock);
1257
1258         return 0;
1259 }
1260
1261 /* only one input in this sample driver */
1262 int cx88_enum_input (struct cx88_core  *core,struct v4l2_input *i)
1263 {
1264         static const char *iname[] = {
1265                 [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1266                 [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1267                 [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1268                 [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1269                 [ CX88_VMUX_SVIDEO     ] = "S-Video",
1270                 [ CX88_VMUX_TELEVISION ] = "Television",
1271                 [ CX88_VMUX_CABLE      ] = "Cable TV",
1272                 [ CX88_VMUX_DVB        ] = "DVB",
1273                 [ CX88_VMUX_DEBUG      ] = "for debug only",
1274         };
1275         unsigned int n = i->index;
1276
1277         if (n >= 4)
1278                 return -EINVAL;
1279         if (0 == INPUT(n).type)
1280                 return -EINVAL;
1281         i->type  = V4L2_INPUT_TYPE_CAMERA;
1282         strcpy(i->name,iname[INPUT(n).type]);
1283         if ((CX88_VMUX_TELEVISION == INPUT(n).type) ||
1284             (CX88_VMUX_CABLE      == INPUT(n).type))
1285                 i->type = V4L2_INPUT_TYPE_TUNER;
1286                 i->std = CX88_NORMS;
1287         return 0;
1288 }
1289 EXPORT_SYMBOL(cx88_enum_input);
1290
1291 static int vidioc_enum_input (struct file *file, void *priv,
1292                                 struct v4l2_input *i)
1293 {
1294         struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1295         return cx88_enum_input (core,i);
1296 }
1297
1298 static int vidioc_g_input (struct file *file, void *priv, unsigned int *i)
1299 {
1300         struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1301
1302         *i = core->input;
1303         return 0;
1304 }
1305
1306 static int vidioc_s_input (struct file *file, void *priv, unsigned int i)
1307 {
1308         struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1309
1310         if (i >= 4)
1311                 return -EINVAL;
1312
1313         mutex_lock(&core->lock);
1314         cx88_newstation(core);
1315         cx88_video_mux(core,i);
1316         mutex_unlock(&core->lock);
1317         return 0;
1318 }
1319
1320
1321
1322 static int vidioc_queryctrl (struct file *file, void *priv,
1323                                 struct v4l2_queryctrl *qctrl)
1324 {
1325         struct cx88_core *core = ((struct cx8800_fh *)priv)->dev->core;
1326
1327         qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1328         if (unlikely(qctrl->id == 0))
1329                 return -EINVAL;
1330         return cx8800_ctrl_query(core, qctrl);
1331 }
1332
1333 static int vidioc_g_ctrl (struct file *file, void *priv,
1334                                 struct v4l2_control *ctl)
1335 {
1336         struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1337         return
1338                 cx88_get_control(core,ctl);
1339 }
1340
1341 static int vidioc_s_ctrl (struct file *file, void *priv,
1342                                 struct v4l2_control *ctl)
1343 {
1344         struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1345         return
1346                 cx88_set_control(core,ctl);
1347 }
1348
1349 static int vidioc_g_tuner (struct file *file, void *priv,
1350                                 struct v4l2_tuner *t)
1351 {
1352         struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1353         u32 reg;
1354
1355         if (unlikely(UNSET == core->board.tuner_type))
1356                 return -EINVAL;
1357         if (0 != t->index)
1358                 return -EINVAL;
1359
1360         strcpy(t->name, "Television");
1361         t->type       = V4L2_TUNER_ANALOG_TV;
1362         t->capability = V4L2_TUNER_CAP_NORM;
1363         t->rangehigh  = 0xffffffffUL;
1364
1365         cx88_get_stereo(core ,t);
1366         reg = cx_read(MO_DEVICE_STATUS);
1367         t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1368         return 0;
1369 }
1370
1371 static int vidioc_s_tuner (struct file *file, void *priv,
1372                                 struct v4l2_tuner *t)
1373 {
1374         struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1375
1376         if (UNSET == core->board.tuner_type)
1377                 return -EINVAL;
1378         if (0 != t->index)
1379                 return -EINVAL;
1380
1381         cx88_set_stereo(core, t->audmode, 1);
1382         return 0;
1383 }
1384
1385 static int vidioc_g_frequency (struct file *file, void *priv,
1386                                 struct v4l2_frequency *f)
1387 {
1388         struct cx8800_fh  *fh   = priv;
1389         struct cx88_core  *core = fh->dev->core;
1390
1391         if (unlikely(UNSET == core->board.tuner_type))
1392                 return -EINVAL;
1393
1394         /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1395         f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1396         f->frequency = core->freq;
1397
1398         cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1399
1400         return 0;
1401 }
1402
1403 int cx88_set_freq (struct cx88_core  *core,
1404                                 struct v4l2_frequency *f)
1405 {
1406         if (unlikely(UNSET == core->board.tuner_type))
1407                 return -EINVAL;
1408         if (unlikely(f->tuner != 0))
1409                 return -EINVAL;
1410
1411         mutex_lock(&core->lock);
1412         core->freq = f->frequency;
1413         cx88_newstation(core);
1414         cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1415
1416         /* When changing channels it is required to reset TVAUDIO */
1417         msleep (10);
1418         cx88_set_tvaudio(core);
1419
1420         mutex_unlock(&core->lock);
1421
1422         return 0;
1423 }
1424 EXPORT_SYMBOL(cx88_set_freq);
1425
1426 static int vidioc_s_frequency (struct file *file, void *priv,
1427                                 struct v4l2_frequency *f)
1428 {
1429         struct cx8800_fh  *fh   = priv;
1430         struct cx88_core  *core = fh->dev->core;
1431
1432         if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1433                 return -EINVAL;
1434         if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1435                 return -EINVAL;
1436
1437         return
1438                 cx88_set_freq (core,f);
1439 }
1440
1441 #ifdef CONFIG_VIDEO_ADV_DEBUG
1442 static int vidioc_g_register (struct file *file, void *fh,
1443                                 struct v4l2_dbg_register *reg)
1444 {
1445         struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1446
1447         if (!v4l2_chip_match_host(&reg->match))
1448                 return -EINVAL;
1449         /* cx2388x has a 24-bit register space */
1450         reg->val = cx_read(reg->reg & 0xffffff);
1451         reg->size = 4;
1452         return 0;
1453 }
1454
1455 static int vidioc_s_register (struct file *file, void *fh,
1456                                 struct v4l2_dbg_register *reg)
1457 {
1458         struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1459
1460         if (!v4l2_chip_match_host(&reg->match))
1461                 return -EINVAL;
1462         cx_write(reg->reg & 0xffffff, reg->val);
1463         return 0;
1464 }
1465 #endif
1466
1467 /* ----------------------------------------------------------- */
1468 /* RADIO ESPECIFIC IOCTLS                                      */
1469 /* ----------------------------------------------------------- */
1470
1471 static int radio_querycap (struct file *file, void  *priv,
1472                                         struct v4l2_capability *cap)
1473 {
1474         struct cx8800_dev *dev  = ((struct cx8800_fh *)priv)->dev;
1475         struct cx88_core  *core = dev->core;
1476
1477         strcpy(cap->driver, "cx8800");
1478         strlcpy(cap->card, core->board.name, sizeof(cap->card));
1479         sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1480         cap->version = CX88_VERSION_CODE;
1481         cap->capabilities = V4L2_CAP_TUNER;
1482         return 0;
1483 }
1484
1485 static int radio_g_tuner (struct file *file, void *priv,
1486                                 struct v4l2_tuner *t)
1487 {
1488         struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1489
1490         if (unlikely(t->index > 0))
1491                 return -EINVAL;
1492
1493         strcpy(t->name, "Radio");
1494         t->type = V4L2_TUNER_RADIO;
1495
1496         cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
1497         return 0;
1498 }
1499
1500 static int radio_enum_input (struct file *file, void *priv,
1501                                 struct v4l2_input *i)
1502 {
1503         if (i->index != 0)
1504                 return -EINVAL;
1505         strcpy(i->name,"Radio");
1506         i->type = V4L2_INPUT_TYPE_TUNER;
1507
1508         return 0;
1509 }
1510
1511 static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
1512 {
1513         if (unlikely(a->index))
1514                 return -EINVAL;
1515
1516         strcpy(a->name,"Radio");
1517         return 0;
1518 }
1519
1520 /* FIXME: Should add a standard for radio */
1521
1522 static int radio_s_tuner (struct file *file, void *priv,
1523                                 struct v4l2_tuner *t)
1524 {
1525         struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1526
1527         if (0 != t->index)
1528                 return -EINVAL;
1529
1530         cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1531
1532         return 0;
1533 }
1534
1535 static int radio_s_audio (struct file *file, void *fh,
1536                           struct v4l2_audio *a)
1537 {
1538         return 0;
1539 }
1540
1541 static int radio_s_input (struct file *file, void *fh, unsigned int i)
1542 {
1543         return 0;
1544 }
1545
1546 static int radio_queryctrl (struct file *file, void *priv,
1547                             struct v4l2_queryctrl *c)
1548 {
1549         int i;
1550
1551         if (c->id <  V4L2_CID_BASE ||
1552                 c->id >= V4L2_CID_LASTP1)
1553                 return -EINVAL;
1554         if (c->id == V4L2_CID_AUDIO_MUTE) {
1555                 for (i = 0; i < CX8800_CTLS; i++)
1556                         if (cx8800_ctls[i].v.id == c->id)
1557                                 break;
1558                 *c = cx8800_ctls[i].v;
1559         } else
1560                 *c = no_ctl;
1561         return 0;
1562 }
1563
1564 /* ----------------------------------------------------------- */
1565
1566 static void cx8800_vid_timeout(unsigned long data)
1567 {
1568         struct cx8800_dev *dev = (struct cx8800_dev*)data;
1569         struct cx88_core *core = dev->core;
1570         struct cx88_dmaqueue *q = &dev->vidq;
1571         struct cx88_buffer *buf;
1572         unsigned long flags;
1573
1574         cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1575
1576         cx_clear(MO_VID_DMACNTRL, 0x11);
1577         cx_clear(VID_CAPTURE_CONTROL, 0x06);
1578
1579         spin_lock_irqsave(&dev->slock,flags);
1580         while (!list_empty(&q->active)) {
1581                 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1582                 list_del(&buf->vb.queue);
1583                 buf->vb.state = VIDEOBUF_ERROR;
1584                 wake_up(&buf->vb.done);
1585                 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1586                        buf, buf->vb.i, (unsigned long)buf->risc.dma);
1587         }
1588         restart_video_queue(dev,q);
1589         spin_unlock_irqrestore(&dev->slock,flags);
1590 }
1591
1592 static char *cx88_vid_irqs[32] = {
1593         "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1594         "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1595         "y_oflow",  "u_oflow",  "v_oflow",  "vbi_oflow",
1596         "y_sync",   "u_sync",   "v_sync",   "vbi_sync",
1597         "opc_err",  "par_err",  "rip_err",  "pci_abort",
1598 };
1599
1600 static void cx8800_vid_irq(struct cx8800_dev *dev)
1601 {
1602         struct cx88_core *core = dev->core;
1603         u32 status, mask, count;
1604
1605         status = cx_read(MO_VID_INTSTAT);
1606         mask   = cx_read(MO_VID_INTMSK);
1607         if (0 == (status & mask))
1608                 return;
1609         cx_write(MO_VID_INTSTAT, status);
1610         if (irq_debug  ||  (status & mask & ~0xff))
1611                 cx88_print_irqbits(core->name, "irq vid",
1612                                    cx88_vid_irqs, ARRAY_SIZE(cx88_vid_irqs),
1613                                    status, mask);
1614
1615         /* risc op code error */
1616         if (status & (1 << 16)) {
1617                 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1618                 cx_clear(MO_VID_DMACNTRL, 0x11);
1619                 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1620                 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1621         }
1622
1623         /* risc1 y */
1624         if (status & 0x01) {
1625                 spin_lock(&dev->slock);
1626                 count = cx_read(MO_VIDY_GPCNT);
1627                 cx88_wakeup(core, &dev->vidq, count);
1628                 spin_unlock(&dev->slock);
1629         }
1630
1631         /* risc1 vbi */
1632         if (status & 0x08) {
1633                 spin_lock(&dev->slock);
1634                 count = cx_read(MO_VBI_GPCNT);
1635                 cx88_wakeup(core, &dev->vbiq, count);
1636                 spin_unlock(&dev->slock);
1637         }
1638
1639         /* risc2 y */
1640         if (status & 0x10) {
1641                 dprintk(2,"stopper video\n");
1642                 spin_lock(&dev->slock);
1643                 restart_video_queue(dev,&dev->vidq);
1644                 spin_unlock(&dev->slock);
1645         }
1646
1647         /* risc2 vbi */
1648         if (status & 0x80) {
1649                 dprintk(2,"stopper vbi\n");
1650                 spin_lock(&dev->slock);
1651                 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1652                 spin_unlock(&dev->slock);
1653         }
1654 }
1655
1656 static irqreturn_t cx8800_irq(int irq, void *dev_id)
1657 {
1658         struct cx8800_dev *dev = dev_id;
1659         struct cx88_core *core = dev->core;
1660         u32 status;
1661         int loop, handled = 0;
1662
1663         for (loop = 0; loop < 10; loop++) {
1664                 status = cx_read(MO_PCI_INTSTAT) &
1665                         (core->pci_irqmask | PCI_INT_VIDINT);
1666                 if (0 == status)
1667                         goto out;
1668                 cx_write(MO_PCI_INTSTAT, status);
1669                 handled = 1;
1670
1671                 if (status & core->pci_irqmask)
1672                         cx88_core_irq(core,status);
1673                 if (status & PCI_INT_VIDINT)
1674                         cx8800_vid_irq(dev);
1675         };
1676         if (10 == loop) {
1677                 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1678                        core->name);
1679                 cx_write(MO_PCI_INTMSK,0);
1680         }
1681
1682  out:
1683         return IRQ_RETVAL(handled);
1684 }
1685
1686 /* ----------------------------------------------------------- */
1687 /* exported stuff                                              */
1688
1689 static const struct v4l2_file_operations video_fops =
1690 {
1691         .owner         = THIS_MODULE,
1692         .open          = video_open,
1693         .release       = video_release,
1694         .read          = video_read,
1695         .poll          = video_poll,
1696         .mmap          = video_mmap,
1697         .ioctl         = video_ioctl2,
1698 };
1699
1700 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1701         .vidioc_querycap      = vidioc_querycap,
1702         .vidioc_enum_fmt_vid_cap  = vidioc_enum_fmt_vid_cap,
1703         .vidioc_g_fmt_vid_cap     = vidioc_g_fmt_vid_cap,
1704         .vidioc_try_fmt_vid_cap   = vidioc_try_fmt_vid_cap,
1705         .vidioc_s_fmt_vid_cap     = vidioc_s_fmt_vid_cap,
1706         .vidioc_g_fmt_vbi_cap     = cx8800_vbi_fmt,
1707         .vidioc_try_fmt_vbi_cap   = cx8800_vbi_fmt,
1708         .vidioc_s_fmt_vbi_cap     = cx8800_vbi_fmt,
1709         .vidioc_reqbufs       = vidioc_reqbufs,
1710         .vidioc_querybuf      = vidioc_querybuf,
1711         .vidioc_qbuf          = vidioc_qbuf,
1712         .vidioc_dqbuf         = vidioc_dqbuf,
1713         .vidioc_s_std         = vidioc_s_std,
1714         .vidioc_enum_input    = vidioc_enum_input,
1715         .vidioc_g_input       = vidioc_g_input,
1716         .vidioc_s_input       = vidioc_s_input,
1717         .vidioc_queryctrl     = vidioc_queryctrl,
1718         .vidioc_g_ctrl        = vidioc_g_ctrl,
1719         .vidioc_s_ctrl        = vidioc_s_ctrl,
1720         .vidioc_streamon      = vidioc_streamon,
1721         .vidioc_streamoff     = vidioc_streamoff,
1722 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1723         .vidiocgmbuf          = vidiocgmbuf,
1724 #endif
1725         .vidioc_g_tuner       = vidioc_g_tuner,
1726         .vidioc_s_tuner       = vidioc_s_tuner,
1727         .vidioc_g_frequency   = vidioc_g_frequency,
1728         .vidioc_s_frequency   = vidioc_s_frequency,
1729 #ifdef CONFIG_VIDEO_ADV_DEBUG
1730         .vidioc_g_register    = vidioc_g_register,
1731         .vidioc_s_register    = vidioc_s_register,
1732 #endif
1733 };
1734
1735 static struct video_device cx8800_vbi_template;
1736
1737 static struct video_device cx8800_video_template = {
1738         .name                 = "cx8800-video",
1739         .fops                 = &video_fops,
1740         .minor                = -1,
1741         .ioctl_ops            = &video_ioctl_ops,
1742         .tvnorms              = CX88_NORMS,
1743         .current_norm         = V4L2_STD_NTSC_M,
1744 };
1745
1746 static const struct v4l2_file_operations radio_fops =
1747 {
1748         .owner         = THIS_MODULE,
1749         .open          = video_open,
1750         .release       = video_release,
1751         .ioctl         = video_ioctl2,
1752 };
1753
1754 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1755         .vidioc_querycap      = radio_querycap,
1756         .vidioc_g_tuner       = radio_g_tuner,
1757         .vidioc_enum_input    = radio_enum_input,
1758         .vidioc_g_audio       = radio_g_audio,
1759         .vidioc_s_tuner       = radio_s_tuner,
1760         .vidioc_s_audio       = radio_s_audio,
1761         .vidioc_s_input       = radio_s_input,
1762         .vidioc_queryctrl     = radio_queryctrl,
1763         .vidioc_g_ctrl        = vidioc_g_ctrl,
1764         .vidioc_s_ctrl        = vidioc_s_ctrl,
1765         .vidioc_g_frequency   = vidioc_g_frequency,
1766         .vidioc_s_frequency   = vidioc_s_frequency,
1767 #ifdef CONFIG_VIDEO_ADV_DEBUG
1768         .vidioc_g_register    = vidioc_g_register,
1769         .vidioc_s_register    = vidioc_s_register,
1770 #endif
1771 };
1772
1773 static struct video_device cx8800_radio_template = {
1774         .name                 = "cx8800-radio",
1775         .fops                 = &radio_fops,
1776         .minor                = -1,
1777         .ioctl_ops            = &radio_ioctl_ops,
1778 };
1779
1780 /* ----------------------------------------------------------- */
1781
1782 static void cx8800_unregister_video(struct cx8800_dev *dev)
1783 {
1784         if (dev->radio_dev) {
1785                 if (-1 != dev->radio_dev->minor)
1786                         video_unregister_device(dev->radio_dev);
1787                 else
1788                         video_device_release(dev->radio_dev);
1789                 dev->radio_dev = NULL;
1790         }
1791         if (dev->vbi_dev) {
1792                 if (-1 != dev->vbi_dev->minor)
1793                         video_unregister_device(dev->vbi_dev);
1794                 else
1795                         video_device_release(dev->vbi_dev);
1796                 dev->vbi_dev = NULL;
1797         }
1798         if (dev->video_dev) {
1799                 if (-1 != dev->video_dev->minor)
1800                         video_unregister_device(dev->video_dev);
1801                 else
1802                         video_device_release(dev->video_dev);
1803                 dev->video_dev = NULL;
1804         }
1805 }
1806
1807 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1808                                     const struct pci_device_id *pci_id)
1809 {
1810         struct cx8800_dev *dev;
1811         struct cx88_core *core;
1812
1813         int err;
1814
1815         dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1816         if (NULL == dev)
1817                 return -ENOMEM;
1818
1819         /* pci init */
1820         dev->pci = pci_dev;
1821         if (pci_enable_device(pci_dev)) {
1822                 err = -EIO;
1823                 goto fail_free;
1824         }
1825         core = cx88_core_get(dev->pci);
1826         if (NULL == core) {
1827                 err = -EINVAL;
1828                 goto fail_free;
1829         }
1830         dev->core = core;
1831
1832         /* print pci info */
1833         pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
1834         pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER,  &dev->pci_lat);
1835         printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1836                "latency: %d, mmio: 0x%llx\n", core->name,
1837                pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1838                dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
1839
1840         pci_set_master(pci_dev);
1841         if (!pci_dma_supported(pci_dev,DMA_32BIT_MASK)) {
1842                 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1843                 err = -EIO;
1844                 goto fail_core;
1845         }
1846
1847         /* Initialize VBI template */
1848         memcpy( &cx8800_vbi_template, &cx8800_video_template,
1849                 sizeof(cx8800_vbi_template) );
1850         strcpy(cx8800_vbi_template.name,"cx8800-vbi");
1851
1852         /* initialize driver struct */
1853         spin_lock_init(&dev->slock);
1854         core->tvnorm = cx8800_video_template.current_norm;
1855
1856         /* init video dma queues */
1857         INIT_LIST_HEAD(&dev->vidq.active);
1858         INIT_LIST_HEAD(&dev->vidq.queued);
1859         dev->vidq.timeout.function = cx8800_vid_timeout;
1860         dev->vidq.timeout.data     = (unsigned long)dev;
1861         init_timer(&dev->vidq.timeout);
1862         cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1863                           MO_VID_DMACNTRL,0x11,0x00);
1864
1865         /* init vbi dma queues */
1866         INIT_LIST_HEAD(&dev->vbiq.active);
1867         INIT_LIST_HEAD(&dev->vbiq.queued);
1868         dev->vbiq.timeout.function = cx8800_vbi_timeout;
1869         dev->vbiq.timeout.data     = (unsigned long)dev;
1870         init_timer(&dev->vbiq.timeout);
1871         cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1872                           MO_VID_DMACNTRL,0x88,0x00);
1873
1874         /* get irq */
1875         err = request_irq(pci_dev->irq, cx8800_irq,
1876                           IRQF_SHARED | IRQF_DISABLED, core->name, dev);
1877         if (err < 0) {
1878                 printk(KERN_ERR "%s/0: can't get IRQ %d\n",
1879                        core->name,pci_dev->irq);
1880                 goto fail_core;
1881         }
1882         cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1883
1884         /* load and configure helper modules */
1885
1886         if (core->board.audio_chip == V4L2_IDENT_WM8775)
1887                 request_module("wm8775");
1888
1889         switch (core->boardnr) {
1890         case CX88_BOARD_DVICO_FUSIONHDTV_5_GOLD:
1891         case CX88_BOARD_DVICO_FUSIONHDTV_7_GOLD:
1892                 request_module("rtc-isl1208");
1893                 /* break intentionally omitted */
1894         case CX88_BOARD_DVICO_FUSIONHDTV_5_PCI_NANO:
1895                 request_module("ir-kbd-i2c");
1896         }
1897
1898         /* register v4l devices */
1899         dev->video_dev = cx88_vdev_init(core,dev->pci,
1900                                         &cx8800_video_template,"video");
1901         err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1902                                     video_nr[core->nr]);
1903         if (err < 0) {
1904                 printk(KERN_ERR "%s/0: can't register video device\n",
1905                        core->name);
1906                 goto fail_unreg;
1907         }
1908         printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1909                core->name, dev->video_dev->num);
1910
1911         dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1912         err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1913                                     vbi_nr[core->nr]);
1914         if (err < 0) {
1915                 printk(KERN_ERR "%s/0: can't register vbi device\n",
1916                        core->name);
1917                 goto fail_unreg;
1918         }
1919         printk(KERN_INFO "%s/0: registered device vbi%d\n",
1920                core->name, dev->vbi_dev->num);
1921
1922         if (core->board.radio.type == CX88_RADIO) {
1923                 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1924                                                 &cx8800_radio_template,"radio");
1925                 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1926                                             radio_nr[core->nr]);
1927                 if (err < 0) {
1928                         printk(KERN_ERR "%s/0: can't register radio device\n",
1929                                core->name);
1930                         goto fail_unreg;
1931                 }
1932                 printk(KERN_INFO "%s/0: registered device radio%d\n",
1933                        core->name, dev->radio_dev->num);
1934         }
1935
1936         /* everything worked */
1937         list_add_tail(&dev->devlist,&cx8800_devlist);
1938         pci_set_drvdata(pci_dev,dev);
1939
1940         /* initial device configuration */
1941         mutex_lock(&core->lock);
1942         cx88_set_tvnorm(core,core->tvnorm);
1943         init_controls(core);
1944         cx88_video_mux(core,0);
1945         mutex_unlock(&core->lock);
1946
1947         /* start tvaudio thread */
1948         if (core->board.tuner_type != TUNER_ABSENT) {
1949                 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
1950                 if (IS_ERR(core->kthread)) {
1951                         err = PTR_ERR(core->kthread);
1952                         printk(KERN_ERR "%s/0: failed to create cx88 audio thread, err=%d\n",
1953                                core->name, err);
1954                 }
1955         }
1956         return 0;
1957
1958 fail_unreg:
1959         cx8800_unregister_video(dev);
1960         free_irq(pci_dev->irq, dev);
1961 fail_core:
1962         cx88_core_put(core,dev->pci);
1963 fail_free:
1964         kfree(dev);
1965         return err;
1966 }
1967
1968 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1969 {
1970         struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1971         struct cx88_core *core = dev->core;
1972
1973         /* stop thread */
1974         if (core->kthread) {
1975                 kthread_stop(core->kthread);
1976                 core->kthread = NULL;
1977         }
1978
1979         if (core->ir)
1980                 cx88_ir_stop(core, core->ir);
1981
1982         cx88_shutdown(core); /* FIXME */
1983         pci_disable_device(pci_dev);
1984
1985         /* unregister stuff */
1986
1987         free_irq(pci_dev->irq, dev);
1988         cx8800_unregister_video(dev);
1989         pci_set_drvdata(pci_dev, NULL);
1990
1991         /* free memory */
1992         btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1993         list_del(&dev->devlist);
1994         cx88_core_put(core,dev->pci);
1995         kfree(dev);
1996 }
1997
1998 #ifdef CONFIG_PM
1999 static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
2000 {
2001         struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2002         struct cx88_core *core = dev->core;
2003
2004         /* stop video+vbi capture */
2005         spin_lock(&dev->slock);
2006         if (!list_empty(&dev->vidq.active)) {
2007                 printk("%s/0: suspend video\n", core->name);
2008                 stop_video_dma(dev);
2009                 del_timer(&dev->vidq.timeout);
2010         }
2011         if (!list_empty(&dev->vbiq.active)) {
2012                 printk("%s/0: suspend vbi\n", core->name);
2013                 cx8800_stop_vbi_dma(dev);
2014                 del_timer(&dev->vbiq.timeout);
2015         }
2016         spin_unlock(&dev->slock);
2017
2018         if (core->ir)
2019                 cx88_ir_stop(core, core->ir);
2020         /* FIXME -- shutdown device */
2021         cx88_shutdown(core);
2022
2023         pci_save_state(pci_dev);
2024         if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
2025                 pci_disable_device(pci_dev);
2026                 dev->state.disabled = 1;
2027         }
2028         return 0;
2029 }
2030
2031 static int cx8800_resume(struct pci_dev *pci_dev)
2032 {
2033         struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2034         struct cx88_core *core = dev->core;
2035         int err;
2036
2037         if (dev->state.disabled) {
2038                 err=pci_enable_device(pci_dev);
2039                 if (err) {
2040                         printk(KERN_ERR "%s/0: can't enable device\n",
2041                                core->name);
2042                         return err;
2043                 }
2044
2045                 dev->state.disabled = 0;
2046         }
2047         err= pci_set_power_state(pci_dev, PCI_D0);
2048         if (err) {
2049                 printk(KERN_ERR "%s/0: can't set power state\n", core->name);
2050                 pci_disable_device(pci_dev);
2051                 dev->state.disabled = 1;
2052
2053                 return err;
2054         }
2055         pci_restore_state(pci_dev);
2056
2057         /* FIXME: re-initialize hardware */
2058         cx88_reset(core);
2059         if (core->ir)
2060                 cx88_ir_start(core, core->ir);
2061
2062         cx_set(MO_PCI_INTMSK, core->pci_irqmask);
2063
2064         /* restart video+vbi capture */
2065         spin_lock(&dev->slock);
2066         if (!list_empty(&dev->vidq.active)) {
2067                 printk("%s/0: resume video\n", core->name);
2068                 restart_video_queue(dev,&dev->vidq);
2069         }
2070         if (!list_empty(&dev->vbiq.active)) {
2071                 printk("%s/0: resume vbi\n", core->name);
2072                 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2073         }
2074         spin_unlock(&dev->slock);
2075
2076         return 0;
2077 }
2078 #endif
2079
2080 /* ----------------------------------------------------------- */
2081
2082 static struct pci_device_id cx8800_pci_tbl[] = {
2083         {
2084                 .vendor       = 0x14f1,
2085                 .device       = 0x8800,
2086                 .subvendor    = PCI_ANY_ID,
2087                 .subdevice    = PCI_ANY_ID,
2088         },{
2089                 /* --- end of list --- */
2090         }
2091 };
2092 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2093
2094 static struct pci_driver cx8800_pci_driver = {
2095         .name     = "cx8800",
2096         .id_table = cx8800_pci_tbl,
2097         .probe    = cx8800_initdev,
2098         .remove   = __devexit_p(cx8800_finidev),
2099 #ifdef CONFIG_PM
2100         .suspend  = cx8800_suspend,
2101         .resume   = cx8800_resume,
2102 #endif
2103 };
2104
2105 static int cx8800_init(void)
2106 {
2107         printk(KERN_INFO "cx88/0: cx2388x v4l2 driver version %d.%d.%d loaded\n",
2108                (CX88_VERSION_CODE >> 16) & 0xff,
2109                (CX88_VERSION_CODE >>  8) & 0xff,
2110                CX88_VERSION_CODE & 0xff);
2111 #ifdef SNAPSHOT
2112         printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2113                SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2114 #endif
2115         return pci_register_driver(&cx8800_pci_driver);
2116 }
2117
2118 static void cx8800_fini(void)
2119 {
2120         pci_unregister_driver(&cx8800_pci_driver);
2121 }
2122
2123 module_init(cx8800_init);
2124 module_exit(cx8800_fini);
2125
2126 /* ----------------------------------------------------------- */
2127 /*
2128  * Local variables:
2129  * c-basic-offset: 8
2130  * End:
2131  * kate: eol "unix"; indent-width 3; remove-trailing-space on; replace-trailing-space-save on; tab-width 8; replace-tabs off; space-indent off; mixed-indent off
2132  */