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