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