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