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