ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / media / video / cx88 / cx88-video.c
1 /*
2  * device driver for Conexant 2388x based TV cards
3  * video4linux video interface
4  *
5  * (c) 2003 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #define __NO_VERSION__ 1
23
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/kmod.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/interrupt.h>
31 #include <asm/div64.h>
32
33 #include "cx88.h"
34
35 MODULE_DESCRIPTION("v4l2 driver module for cx2388x based TV cards");
36 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
37 MODULE_LICENSE("GPL");
38
39 /* ------------------------------------------------------------------ */
40
41 static unsigned int video_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
42 MODULE_PARM(video_nr,"1-" __stringify(CX88_MAXBOARDS) "i");
43 MODULE_PARM_DESC(video_nr,"video device numbers");
44
45 static unsigned int vbi_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
46 MODULE_PARM(vbi_nr,"1-" __stringify(CX88_MAXBOARDS) "i");
47 MODULE_PARM_DESC(vbi_nr,"vbi device numbers");
48
49 static unsigned int radio_nr[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
50 MODULE_PARM(radio_nr,"1-" __stringify(CX88_MAXBOARDS) "i");
51 MODULE_PARM_DESC(radio_nr,"radio device numbers");
52
53 static unsigned int latency = UNSET;
54 MODULE_PARM(latency,"i");
55 MODULE_PARM_DESC(latency,"pci latency timer");
56
57 static unsigned int video_debug = 0;
58 MODULE_PARM(video_debug,"i");
59 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
60
61 static unsigned int irq_debug = 0;
62 MODULE_PARM(irq_debug,"i");
63 MODULE_PARM_DESC(irq_debug,"enable debug messages [IRQ handler]");
64
65 static unsigned int vid_limit = 16;
66 MODULE_PARM(vid_limit,"i");
67 MODULE_PARM_DESC(vid_limit,"capture memory limit in megabytes");
68
69 static unsigned int tuner[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
70 MODULE_PARM(tuner,"1-" __stringify(CX88_MAXBOARDS) "i");
71 MODULE_PARM_DESC(tuner,"tuner type");
72
73 static unsigned int card[] = {[0 ... (CX88_MAXBOARDS - 1)] = UNSET };
74 MODULE_PARM(card,"1-" __stringify(CX88_MAXBOARDS) "i");
75 MODULE_PARM_DESC(card,"card type");
76
77 static unsigned int nicam = 0;
78 MODULE_PARM(nicam,"i");
79 MODULE_PARM_DESC(nicam,"tv audio is nicam");
80
81 #define dprintk(level,fmt, arg...)      if (video_debug >= level) \
82         printk(KERN_DEBUG "%s: " fmt, dev->name , ## arg)
83
84 /* ------------------------------------------------------------------ */
85
86 static struct list_head  cx8800_devlist;
87 static unsigned int      cx8800_devcount;
88
89 /* ------------------------------------------------------------------- */
90 /* static data                                                         */
91
92 static unsigned int inline norm_swidth(struct cx8800_tvnorm *norm)
93 {
94         return (norm->id & V4L2_STD_625_50) ? 922 : 754;
95 }
96
97 static unsigned int inline norm_hdelay(struct cx8800_tvnorm *norm)
98 {
99         return (norm->id & V4L2_STD_625_50) ? 186 : 135;
100 }
101
102 static unsigned int inline norm_vdelay(struct cx8800_tvnorm *norm)
103 {
104         return (norm->id & V4L2_STD_625_50) ? 0x24 : 0x18;
105 }
106
107 static unsigned int inline norm_maxw(struct cx8800_tvnorm *norm)
108 {
109         return (norm->id & V4L2_STD_625_50) ? 768 : 640;
110 //      return (norm->id & V4L2_STD_625_50) ? 720 : 640;
111 }
112
113 static unsigned int inline norm_maxh(struct cx8800_tvnorm *norm)
114 {
115         return (norm->id & V4L2_STD_625_50) ? 576 : 480;
116 }
117
118 static unsigned int inline norm_fsc8(struct cx8800_tvnorm *norm)
119 {
120         static const unsigned int ntsc = 28636360;
121         static const unsigned int pal  = 35468950;
122         
123         return (norm->id & V4L2_STD_625_50) ? pal : ntsc;
124 }
125
126 static unsigned int inline norm_notchfilter(struct cx8800_tvnorm *norm)
127 {
128         return (norm->id & V4L2_STD_625_50)
129                 ? HLNotchFilter135PAL
130                 : HLNotchFilter135NTSC;
131 }
132
133 static unsigned int inline norm_htotal(struct cx8800_tvnorm *norm)
134 {
135         return (norm->id & V4L2_STD_625_50) ? 1135 : 910;
136 }
137
138 static unsigned int inline norm_vbipack(struct cx8800_tvnorm *norm)
139 {
140         return (norm->id & V4L2_STD_625_50) ? 511 : 288;
141 }
142
143 static struct cx8800_tvnorm tvnorms[] = {
144         {
145                 .name      = "NTSC-M",
146                 .id        = V4L2_STD_NTSC_M,
147                 .cxiformat = VideoFormatNTSC,
148                 .cxoformat = 0x181f0008,
149         },{
150                 .name      = "NTSC-JP",
151                 .id        = V4L2_STD_NTSC_M_JP,
152                 .cxiformat = VideoFormatNTSCJapan,
153                 .cxoformat = 0x181f0008,
154 #if 0
155         },{
156                 .name      = "NTSC-4.43",
157                 .id        = FIXME,
158                 .cxiformat = VideoFormatNTSC443,
159                 .cxoformat = 0x181f0008,
160 #endif
161         },{
162                 .name      = "PAL-BG",
163                 .id        = V4L2_STD_PAL_BG,
164                 .cxiformat = VideoFormatPAL,
165                 .cxoformat = 0x181f0008,
166         },{
167                 .name      = "PAL-DK",
168                 .id        = V4L2_STD_PAL_DK,
169                 .cxiformat = VideoFormatPAL,
170                 .cxoformat = 0x181f0008,
171         },{
172                 .name      = "PAL-I",
173                 .id        = V4L2_STD_PAL_I,
174                 .cxiformat = VideoFormatPAL,
175                 .cxoformat = 0x181f0008,
176         },{
177                 .name      = "PAL-M",
178                 .id        = V4L2_STD_PAL_M,
179                 .cxiformat = VideoFormatPALM,
180                 .cxoformat = 0x1c1f0008,
181         },{
182                 .name      = "PAL-N",
183                 .id        = V4L2_STD_PAL_N,
184                 .cxiformat = VideoFormatPALN,
185                 .cxoformat = 0x1c1f0008,
186         },{
187                 .name      = "PAL-Nc",
188                 .id        = V4L2_STD_PAL_Nc,
189                 .cxiformat = VideoFormatPALNC,
190                 .cxoformat = 0x1c1f0008,
191         },{
192                 .name      = "PAL-60",
193                 .id        = V4L2_STD_PAL_60,
194                 .cxiformat = VideoFormatPAL60,
195                 .cxoformat = 0x181f0008,
196         },{
197                 .name      = "SECAM",
198                 .id        = V4L2_STD_SECAM,
199                 .cxiformat = VideoFormatSECAM,
200                 .cxoformat = 0x181f0008,
201         }
202 };
203
204 static struct cx8800_fmt formats[] = {
205         {
206                 .name     = "8 bpp, gray",
207                 .fourcc   = V4L2_PIX_FMT_GREY,
208                 .cxformat = ColorFormatY8,
209                 .depth    = 8,
210                 .flags    = FORMAT_FLAGS_PACKED,
211         },{
212                 .name     = "15 bpp RGB, le",
213                 .fourcc   = V4L2_PIX_FMT_RGB555,
214                 .cxformat = ColorFormatRGB15,
215                 .depth    = 16,
216                 .flags    = FORMAT_FLAGS_PACKED,
217         },{
218                 .name     = "15 bpp RGB, be",
219                 .fourcc   = V4L2_PIX_FMT_RGB555X,
220                 .cxformat = ColorFormatRGB15 | ColorFormatBSWAP,
221                 .depth    = 16,
222                 .flags    = FORMAT_FLAGS_PACKED,
223         },{
224                 .name     = "16 bpp RGB, le",
225                 .fourcc   = V4L2_PIX_FMT_RGB565,
226                 .cxformat = ColorFormatRGB16,
227                 .depth    = 16,
228                 .flags    = FORMAT_FLAGS_PACKED,
229         },{
230                 .name     = "16 bpp RGB, be",
231                 .fourcc   = V4L2_PIX_FMT_RGB565X,
232                 .cxformat = ColorFormatRGB16 | ColorFormatBSWAP,
233                 .depth    = 16,
234                 .flags    = FORMAT_FLAGS_PACKED,
235         },{
236                 .name     = "24 bpp RGB, le",
237                 .fourcc   = V4L2_PIX_FMT_BGR24,
238                 .cxformat = ColorFormatRGB24,
239                 .depth    = 24,
240                 .flags    = FORMAT_FLAGS_PACKED,
241         },{
242                 .name     = "32 bpp RGB, le",
243                 .fourcc   = V4L2_PIX_FMT_BGR32,
244                 .cxformat = ColorFormatRGB32,
245                 .depth    = 32,
246                 .flags    = FORMAT_FLAGS_PACKED,
247         },{
248                 .name     = "32 bpp RGB, be",
249                 .fourcc   = V4L2_PIX_FMT_RGB32,
250                 .cxformat = ColorFormatRGB32 | ColorFormatBSWAP | ColorFormatWSWAP,
251                 .depth    = 32,
252                 .flags    = FORMAT_FLAGS_PACKED,
253         },{
254                 .name     = "4:2:2, packed, YUYV",
255                 .fourcc   = V4L2_PIX_FMT_YUYV,
256                 .cxformat = ColorFormatYUY2,
257                 .depth    = 16,
258                 .flags    = FORMAT_FLAGS_PACKED,
259         },{
260                 .name     = "4:2:2, packed, UYVY",
261                 .fourcc   = V4L2_PIX_FMT_UYVY,
262                 .cxformat = ColorFormatYUY2 | ColorFormatBSWAP,
263                 .depth    = 16,
264                 .flags    = FORMAT_FLAGS_PACKED,
265         },
266 };
267
268 static struct cx8800_fmt* format_by_fourcc(unsigned int fourcc)
269 {
270         unsigned int i;
271         
272         for (i = 0; i < ARRAY_SIZE(formats); i++)
273                 if (formats[i].fourcc == fourcc)
274                         return formats+i;
275         return NULL;
276 }
277
278 /* ------------------------------------------------------------------- */
279
280 static const struct v4l2_queryctrl no_ctl = {
281         .name  = "42",
282         .flags = V4L2_CTRL_FLAG_DISABLED,
283 };
284
285 static struct cx88_ctrl cx8800_ctls[] = {
286         /* --- video --- */
287         {
288                 .v = {
289                         .id            = V4L2_CID_BRIGHTNESS,
290                         .name          = "Brightness",
291                         .minimum       = 0x00,
292                         .maximum       = 0xff,
293                         .step          = 1,
294                         .default_value = 0,
295                         .type          = V4L2_CTRL_TYPE_INTEGER,
296                 },
297                 .off                   = 128,
298                 .reg                   = MO_CONTR_BRIGHT,
299                 .mask                  = 0x00ff,
300                 .shift                 = 0,
301         },{
302                 .v = {
303                         .id            = V4L2_CID_CONTRAST,
304                         .name          = "Contrast",
305                         .minimum       = 0,
306                         .maximum       = 0xff,
307                         .step          = 1,
308                         .default_value = 0,
309                         .type          = V4L2_CTRL_TYPE_INTEGER,
310                 },
311                 .reg                   = MO_CONTR_BRIGHT,
312                 .mask                  = 0xff00,
313                 .shift                 = 8,
314         },{
315                 .v = {
316                         .id            = V4L2_CID_HUE,
317                         .name          = "Hue",
318                         .minimum       = 0,
319                         .maximum       = 0xff,
320                         .step          = 1,
321                         .default_value = 0,
322                         .type          = V4L2_CTRL_TYPE_INTEGER,
323                 },
324                 .off                   = 0,
325                 .reg                   = MO_HUE,
326                 .mask                  = 0x00ff,
327                 .shift                 = 0,
328         },{
329                 /* strictly, this only describes only U saturation.
330                  * V saturation is handled specially through code.
331                  */
332                 .v = {
333                         .id            = V4L2_CID_SATURATION,
334                         .name          = "Saturation",
335                         .minimum       = 0,
336                         .maximum       = 0xff,
337                         .step          = 1,
338                         .default_value = 0,
339                         .type          = V4L2_CTRL_TYPE_INTEGER,
340                 },
341                 .off                   = 0,
342                 .reg                   = MO_UV_SATURATION,
343                 .mask                  = 0x00ff,
344                 .shift                 = 0,
345         },{
346         /* --- audio --- */
347                 .v = {
348                         .id            = V4L2_CID_AUDIO_MUTE,
349                         .name          = "Mute",
350                         .minimum       = 0,
351                         .maximum       = 1,
352                         .type          = V4L2_CTRL_TYPE_BOOLEAN,
353                 },
354                 .reg                   = AUD_VOL_CTL,
355                 .sreg                  = SHADOW_AUD_VOL_CTL,
356                 .mask                  = (1 << 6),
357                 .shift                 = 6,
358         },{
359                 .v = {
360                         .id            = V4L2_CID_AUDIO_VOLUME,
361                         .name          = "Volume",
362                         .minimum       = 0,
363                         .maximum       = 0x3f,
364                         .step          = 1,
365                         .default_value = 0,
366                         .type          = V4L2_CTRL_TYPE_INTEGER,
367                 },
368                 .reg                   = AUD_VOL_CTL,
369                 .sreg                  = SHADOW_AUD_VOL_CTL,
370                 .mask                  = 0x3f,
371                 .shift                 = 0,
372         },{
373                 .v = {
374                         .id            = V4L2_CID_AUDIO_BALANCE,
375                         .name          = "Balance",
376                         .minimum       = 0,
377                         .maximum       = 0x7f,
378                         .step          = 1,
379                         .default_value = 0x40,
380                         .type          = V4L2_CTRL_TYPE_INTEGER,
381                 },
382                 .reg                   = AUD_BAL_CTL,
383                 .sreg                  = SHADOW_AUD_BAL_CTL,
384                 .mask                  = 0x7f,
385                 .shift                 = 0,
386         }
387 };
388 const int CX8800_CTLS = ARRAY_SIZE(cx8800_ctls);
389
390 /* ------------------------------------------------------------------- */
391 /* resource management                                                 */
392
393 static int res_get(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bit)
394 {
395         if (fh->resources & bit)
396                 /* have it already allocated */
397                 return 1;
398
399         /* is it free? */
400         down(&dev->lock);
401         if (dev->resources & bit) {
402                 /* no, someone else uses it */
403                 up(&dev->lock);
404                 return 0;
405         }
406         /* it's free, grab it */
407         fh->resources  |= bit;
408         dev->resources |= bit;
409         dprintk(1,"res: get %d\n",bit);
410         up(&dev->lock);
411         return 1;
412 }
413
414 static
415 int res_check(struct cx8800_fh *fh, unsigned int bit)
416 {
417         return (fh->resources & bit);
418 }
419
420 static
421 int res_locked(struct cx8800_dev *dev, unsigned int bit)
422 {
423         return (dev->resources & bit);
424 }
425
426 static
427 void res_free(struct cx8800_dev *dev, struct cx8800_fh *fh, unsigned int bits)
428 {
429         if ((fh->resources & bits) != bits)
430                 BUG();
431
432         down(&dev->lock);
433         fh->resources  &= ~bits;
434         dev->resources &= ~bits;
435         dprintk(1,"res: put %d\n",bits);
436         up(&dev->lock);
437 }
438
439 /* ------------------------------------------------------------------ */
440
441 static const u32 xtal = 28636363;
442
443 static int set_pll(struct cx8800_dev *dev, int prescale, u32 ofreq)
444 {
445         static u32 pre[] = { 0, 0, 0, 3, 2, 1 };
446         u64 pll;
447         u32 reg;
448         int i;
449
450         if (prescale < 2)
451                 prescale = 2;
452         if (prescale > 5)
453                 prescale = 5;
454
455         pll = ofreq * 8 * prescale * (u64)(1 << 20);
456         do_div(pll,xtal);
457         reg = (pll & 0x3ffffff) | (pre[prescale] << 26);
458         if (((reg >> 20) & 0x3f) < 14) {
459                 printk("%s: pll out of range\n",dev->name);
460                 return -1;
461         }
462                 
463         dprintk(1,"set_pll:    MO_PLL_REG       0x%08x [old=0x%08x,freq=%d]\n",
464                 reg, cx_read(MO_PLL_REG), ofreq);
465         cx_write(MO_PLL_REG, reg);
466         for (i = 0; i < 10; i++) {
467                 reg = cx_read(MO_DEVICE_STATUS);
468                 if (reg & (1<<2)) {
469                         dprintk(1,"pll locked [pre=%d,ofreq=%d]\n",
470                                 prescale,ofreq);
471                         return 0;
472                 }
473                 dprintk(1,"pll not locked yet, waiting ...\n");
474                 set_current_state(TASK_INTERRUPTIBLE);
475                 schedule_timeout(HZ/10);
476         }
477         dprintk(1,"pll NOT locked [pre=%d,ofreq=%d]\n",prescale,ofreq);
478         return -1;
479 }
480
481 static int set_tvaudio(struct cx8800_dev *dev)
482 {
483         if (CX88_VMUX_TELEVISION != INPUT(dev->input)->type)
484                 return 0;
485
486         switch (dev->tvnorm->id) {
487         case V4L2_STD_PAL_BG:
488                 dev->tvaudio = nicam ? WW_NICAM_BGDKL : WW_A2_BG;
489                 break;
490         case V4L2_STD_PAL_DK:
491                 dev->tvaudio = nicam ? WW_NICAM_BGDKL : WW_A2_DK;
492                 break;
493         case V4L2_STD_PAL_I:
494                 dev->tvaudio = WW_NICAM_I;
495                 break;
496         case V4L2_STD_SECAM:
497                 dev->tvaudio = WW_SYSTEM_L_AM;  /* FIXME: fr != ru */
498                 break;
499         case V4L2_STD_NTSC_M:
500                 dev->tvaudio = WW_BTSC;
501                 break;
502         case V4L2_STD_NTSC_M_JP:
503                 dev->tvaudio = WW_EIAJ;
504                 break;
505         default:
506                 dprintk(1,"tvaudio support needs work for this tv norm [%s], sorry\n",
507                         dev->tvnorm->name);
508                 dev->tvaudio = 0;
509                 return 0;
510         }
511
512         cx_andor(MO_AFECFG_IO, 0x1f, 0x0);
513         cx88_set_tvaudio(dev);
514         cx88_set_stereo(dev,V4L2_TUNER_MODE_STEREO);
515
516         cx_write(MO_AUDD_LNGTH, 128/8);  /* fifo size */
517         cx_write(MO_AUDR_LNGTH, 128/8);  /* fifo size */
518         cx_write(MO_AUD_DMACNTRL, 0x03); /* need audio fifo */
519         return 0;
520 }
521
522 static int set_tvnorm(struct cx8800_dev *dev, struct cx8800_tvnorm *norm)
523 {
524         u32 fsc8;
525         u32 adc_clock;
526         u32 vdec_clock;
527         u64 tmp64;
528         u32 bdelay,agcdelay,htotal;
529         struct video_channel c;
530         
531         dev->tvnorm = norm;
532         fsc8       = norm_fsc8(norm);
533         adc_clock  = xtal;
534         vdec_clock = fsc8;
535
536         dprintk(1,"set_tvnorm: \"%s\" fsc8=%d adc=%d vdec=%d\n",
537                 norm->name, fsc8, adc_clock, vdec_clock);
538         set_pll(dev,2,vdec_clock);
539         
540         dprintk(1,"set_tvnorm: MO_INPUT_FORMAT  0x%08x [old=0x%08x]\n",
541                 norm->cxiformat, cx_read(MO_INPUT_FORMAT) & 0x0f);
542         cx_andor(MO_INPUT_FORMAT, 0xf, norm->cxiformat);
543
544 #if 1
545         // FIXME: as-is from DScaler
546         dprintk(1,"set_tvnorm: MO_OUTPUT_FORMAT 0x%08x [old=0x%08x]\n",
547                 norm->cxoformat, cx_read(MO_OUTPUT_FORMAT));
548         cx_write(MO_OUTPUT_FORMAT, norm->cxoformat);
549 #endif
550
551         // MO_SCONV_REG = adc clock / video dec clock * 2^17
552         tmp64  = adc_clock * (u64)(1 << 17);
553         do_div(tmp64, vdec_clock);
554         dprintk(1,"set_tvnorm: MO_SCONV_REG     0x%08x [old=0x%08x]\n",
555                 (u32)tmp64, cx_read(MO_SCONV_REG));
556         cx_write(MO_SCONV_REG, (u32)tmp64);
557
558         // MO_SUB_STEP = 8 * fsc / video dec clock * 2^22
559         tmp64  = fsc8 * (u64)(1 << 22);
560         do_div(tmp64, vdec_clock);
561         dprintk(1,"set_tvnorm: MO_SUB_STEP      0x%08x [old=0x%08x]\n",
562                 (u32)tmp64, cx_read(MO_SUB_STEP));
563         cx_write(MO_SUB_STEP, (u32)tmp64);
564
565         // MO_SUB_STEP_DR = 8 * 4406250 / video dec clock * 2^22
566         tmp64  = 4406250 * 8 * (u64)(1 << 22);
567         do_div(tmp64, vdec_clock);
568         dprintk(1,"set_tvnorm: MO_SUB_STEP_DR   0x%08x [old=0x%08x]\n",
569                 (u32)tmp64, cx_read(MO_SUB_STEP_DR));
570         cx_write(MO_SUB_STEP_DR, (u32)tmp64);
571
572         // bdelay + agcdelay
573         bdelay   = vdec_clock * 65 / 20000000 + 21;
574         agcdelay = vdec_clock * 68 / 20000000 + 15;
575         dprintk(1,"set_tvnorm: MO_AGC_BURST     0x%08x [old=0x%08x,bdelay=%d,agcdelay=%d]\n",
576                 (bdelay << 8) | agcdelay, cx_read(MO_AGC_BURST), bdelay, agcdelay);
577         cx_write(MO_AGC_BURST, (bdelay << 8) | agcdelay);
578
579         // htotal
580         tmp64 = norm_htotal(norm) * (u64)vdec_clock;
581         do_div(tmp64, fsc8);
582         htotal = (u32)tmp64 | (norm_notchfilter(norm) << 11);
583         dprintk(1,"set_tvnorm: MO_HTOTAL        0x%08x [old=0x%08x,htotal=%d]\n",
584                 htotal, cx_read(MO_HTOTAL), (u32)tmp64);
585         cx_write(MO_HTOTAL, htotal);
586
587         // vbi stuff
588         cx_write(MO_VBI_PACKET, ((1 << 11) | /* (norm_vdelay(norm)   << 11) | */
589                                  norm_vbipack(norm)));
590         
591         // audio
592         set_tvaudio(dev);
593
594         // tell i2c chips
595         memset(&c,0,sizeof(c));
596         c.channel = dev->input;
597         c.norm = VIDEO_MODE_PAL;
598         if ((norm->id & (V4L2_STD_NTSC_M|V4L2_STD_NTSC_M_JP)))
599                 c.norm = VIDEO_MODE_NTSC;
600         if (norm->id & V4L2_STD_SECAM)
601                 c.norm = VIDEO_MODE_SECAM;
602         cx8800_call_i2c_clients(dev,VIDIOCSCHAN,&c);
603
604         // done
605         return 0;
606 }
607
608 static int set_scale(struct cx8800_dev *dev, unsigned int width, unsigned int height,
609                      int interlaced)
610 {
611         unsigned int swidth  = norm_swidth(dev->tvnorm);
612         unsigned int sheight = norm_maxh(dev->tvnorm);
613         u32 value;
614
615         dprintk(1,"set_scale: %dx%d [%s]\n", width, height, dev->tvnorm->name);
616
617         // recalc H delay and scale registers
618         value = (width * norm_hdelay(dev->tvnorm)) / swidth;
619         cx_write(MO_HDELAY_EVEN,  value);
620         cx_write(MO_HDELAY_ODD,   value);
621         dprintk(1,"set_scale: hdelay  0x%04x\n", value);
622         
623         value = (swidth * 4096 / width) - 4096;
624         cx_write(MO_HSCALE_EVEN,  value);
625         cx_write(MO_HSCALE_ODD,   value);
626         dprintk(1,"set_scale: hscale  0x%04x\n", value);
627
628         cx_write(MO_HACTIVE_EVEN, width);
629         cx_write(MO_HACTIVE_ODD,  width);
630         dprintk(1,"set_scale: hactive 0x%04x\n", width);
631         
632         // recalc V scale Register (delay is constant)
633         cx_write(MO_VDELAY_EVEN, norm_vdelay(dev->tvnorm));
634         cx_write(MO_VDELAY_ODD,  norm_vdelay(dev->tvnorm));
635         dprintk(1,"set_scale: vdelay  0x%04x\n", norm_vdelay(dev->tvnorm));
636         
637         value = (0x10000 - (sheight * 512 / height - 512)) & 0x1fff;
638         cx_write(MO_VSCALE_EVEN,  value);
639         cx_write(MO_VSCALE_ODD,   value);
640         dprintk(1,"set_scale: vscale  0x%04x\n", value);
641
642         cx_write(MO_VACTIVE_EVEN, sheight);
643         cx_write(MO_VACTIVE_ODD,  sheight);
644         dprintk(1,"set_scale: vactive 0x%04x\n", sheight);
645
646         // setup filters
647         value = 0;
648         value |= (1 << 19);        // CFILT (default)
649         if (interlaced)
650                 value |= (1 << 3); // VINT (interlaced vertical scaling)
651         if (width < 385)
652                 value |= (1 << 0); // 3-tap interpolation
653         if (width < 193)
654                 value |= (1 << 1); // 5-tap interpolation
655
656         cx_write(MO_FILTER_EVEN,  value);
657         cx_write(MO_FILTER_ODD,   value);
658         dprintk(1,"set_scale: filter  0x%04x\n", value);
659         
660         return 0;
661 }
662
663 static int video_mux(struct cx8800_dev *dev, unsigned int input)
664 {
665         dprintk(1,"video_mux: %d [vmux=%d,gpio=0x%x,0x%x,0x%x,0x%x]\n",
666                 input, INPUT(input)->vmux,
667                 INPUT(input)->gpio0,INPUT(input)->gpio1,
668                 INPUT(input)->gpio2,INPUT(input)->gpio3);
669         dev->input = input;
670         cx_andor(MO_INPUT_FORMAT, 0x03 << 14, INPUT(input)->vmux << 14);
671         cx_write(MO_GP0_IO, INPUT(input)->gpio0);
672         cx_write(MO_GP1_IO, INPUT(input)->gpio1);
673         cx_write(MO_GP2_IO, INPUT(input)->gpio2);
674         cx_write(MO_GP3_IO, INPUT(input)->gpio3);
675
676         switch (INPUT(input)->type) {
677         case CX88_VMUX_SVIDEO:
678                 cx_andor(MO_AFECFG_IO, 0x01, 0x01);
679                 break;
680         default:
681                 cx_andor(MO_AFECFG_IO, 0x01, 0x00);
682                 break;
683         }
684         return 0;
685 }
686
687 /* ------------------------------------------------------------------ */
688
689 static int start_video_dma(struct cx8800_dev    *dev,
690                            struct cx88_dmaqueue *q,
691                            struct cx88_buffer   *buf)
692 {
693         /* setup fifo + format */
694         cx88_sram_channel_setup(dev, &cx88_sram_channels[SRAM_CH21],
695                                 buf->bpl, buf->risc.dma);
696         set_scale(dev, buf->vb.width, buf->vb.height, 1);
697         cx_write(MO_COLOR_CTRL, buf->fmt->cxformat | ColorFormatGamma);
698
699         /* reset counter */
700         cx_write(MO_VIDY_GPCNTRL,0x3);
701         q->count = 1;
702
703         /* enable irqs */
704         cx_set(MO_PCI_INTMSK, 0x00fc01);
705         cx_set(MO_VID_INTMSK, 0x0f0011);
706         
707         /* enable capture */
708         cx_set(VID_CAPTURE_CONTROL,0x06);
709         
710         /* start dma */
711         cx_set(MO_DEV_CNTRL2, (1<<5));
712         cx_set(MO_VID_DMACNTRL, 0x11);
713
714         return 0;
715 }
716
717 static int restart_video_queue(struct cx8800_dev    *dev,
718                                struct cx88_dmaqueue *q)
719 {
720         struct cx88_buffer *buf, *prev;
721         struct list_head *item;
722         
723         if (!list_empty(&q->active)) {
724                 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
725                 dprintk(2,"restart_queue [%p/%d]: restart dma\n",
726                         buf, buf->vb.i);
727                 start_video_dma(dev, q, buf);
728                 list_for_each(item,&q->active) {
729                         buf = list_entry(item, struct cx88_buffer, vb.queue);
730                         buf->count    = q->count++;
731                 }
732                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
733                 return 0;
734         }
735
736         prev = NULL;
737         for (;;) {
738                 if (list_empty(&q->queued))
739                         return 0;
740                 buf = list_entry(q->queued.next, struct cx88_buffer, vb.queue);
741                 if (NULL == prev) {
742                         list_del(&buf->vb.queue);
743                         list_add_tail(&buf->vb.queue,&q->active);
744                         start_video_dma(dev, q, buf);
745                         buf->vb.state = STATE_ACTIVE;
746                         buf->count    = q->count++;
747                         mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
748                         dprintk(2,"[%p/%d] restart_queue - first active\n",
749                                 buf,buf->vb.i);
750
751                 } else if (prev->vb.width  == buf->vb.width  &&
752                            prev->vb.height == buf->vb.height &&
753                            prev->fmt       == buf->fmt) {
754                         list_del(&buf->vb.queue);
755                         list_add_tail(&buf->vb.queue,&q->active);
756                         buf->vb.state = STATE_ACTIVE;
757                         buf->count    = q->count++;
758                         prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
759                         dprintk(2,"[%p/%d] restart_queue - move to active\n",
760                                 buf,buf->vb.i);
761                 } else {
762                         return 0;
763                 }
764                 prev = buf;
765         }
766 }
767
768 /* ------------------------------------------------------------------ */
769
770 static int
771 buffer_setup(struct file *file, unsigned int *count, unsigned int *size)
772 {
773         struct cx8800_fh *fh = file->private_data;
774         
775         *size = fh->fmt->depth*fh->width*fh->height >> 3;
776         if (0 == *count)
777                 *count = 32;
778         while (*size * *count > vid_limit * 1024 * 1024)
779                 (*count)--;
780         return 0;
781 }
782
783 static int
784 buffer_prepare(struct file *file, struct videobuf_buffer *vb,
785                enum v4l2_field field)
786 {
787         struct cx8800_fh   *fh  = file->private_data;
788         struct cx8800_dev  *dev = fh->dev;
789         struct cx88_buffer *buf = (struct cx88_buffer*)vb;
790         int rc, init_buffer = 0;
791
792         BUG_ON(NULL == fh->fmt);
793         if (fh->width  < 48 || fh->width  > norm_maxw(dev->tvnorm) ||
794             fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
795                 return -EINVAL;
796         buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
797         if (0 != buf->vb.baddr  &&  buf->vb.bsize < buf->vb.size)
798                 return -EINVAL;
799
800         if (buf->fmt       != fh->fmt    ||
801             buf->vb.width  != fh->width  ||
802             buf->vb.height != fh->height ||
803             buf->vb.field  != field) {
804                 buf->fmt       = fh->fmt;
805                 buf->vb.width  = fh->width;
806                 buf->vb.height = fh->height;
807                 buf->vb.field  = field;
808                 init_buffer = 1;
809         }
810
811         if (STATE_NEEDS_INIT == buf->vb.state) {
812                 init_buffer = 1;
813                 if (0 != (rc = videobuf_iolock(dev->pci,&buf->vb,NULL)))
814                         goto fail;
815         }
816
817         if (init_buffer) {
818                 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
819                 switch (buf->vb.field) {
820                 case V4L2_FIELD_TOP:
821                         cx88_risc_buffer(dev->pci, &buf->risc,
822                                          buf->vb.dma.sglist, 0, UNSET,
823                                          buf->bpl, 0, buf->vb.height);
824                         break;
825                 case V4L2_FIELD_BOTTOM:
826                         cx88_risc_buffer(dev->pci, &buf->risc,
827                                          buf->vb.dma.sglist, UNSET, 0,
828                                          buf->bpl, 0, buf->vb.height);
829                         break;
830                 case V4L2_FIELD_INTERLACED:
831                         cx88_risc_buffer(dev->pci, &buf->risc,
832                                          buf->vb.dma.sglist, 0, buf->bpl,
833                                          buf->bpl, buf->bpl,
834                                          buf->vb.height >> 1);
835                         break;
836                 case V4L2_FIELD_SEQ_TB:
837                         cx88_risc_buffer(dev->pci, &buf->risc,
838                                          buf->vb.dma.sglist,
839                                          0, buf->bpl * (buf->vb.height >> 1),
840                                          buf->bpl, 0,
841                                          buf->vb.height >> 1);
842                         break;
843                 case V4L2_FIELD_SEQ_BT:
844                         cx88_risc_buffer(dev->pci, &buf->risc,
845                                          buf->vb.dma.sglist,
846                                          buf->bpl * (buf->vb.height >> 1), 0,
847                                          buf->bpl, 0,
848                                          buf->vb.height >> 1);
849                         break;
850                 default:
851                         BUG();
852                 }
853         }
854         dprintk(2,"[%p/%d] buffer_prepare - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
855                 buf, buf->vb.i,
856                 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
857                 (unsigned long)buf->risc.dma);
858
859         buf->vb.state = STATE_PREPARED;
860         return 0;
861
862  fail:
863         cx88_free_buffer(dev->pci,buf);
864         return rc;
865 }
866
867 static void
868 buffer_queue(struct file *file, struct videobuf_buffer *vb)
869 {
870         struct cx88_buffer    *buf  = (struct cx88_buffer*)vb;
871         struct cx88_buffer    *prev;
872         struct cx8800_fh      *fh   = file->private_data;
873         struct cx8800_dev     *dev  = fh->dev;
874         struct cx88_dmaqueue  *q    = &dev->vidq;
875
876         /* add jump to stopper */
877         buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | 0x10000);
878         buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
879
880         if (!list_empty(&q->queued)) {
881                 list_add_tail(&buf->vb.queue,&q->queued);
882                 buf->vb.state = STATE_QUEUED;
883                 dprintk(2,"[%p/%d] buffer_queue - append to queued\n",
884                         buf, buf->vb.i);
885
886         } else if (list_empty(&q->active)) {
887                 list_add_tail(&buf->vb.queue,&q->active);
888                 start_video_dma(dev, q, buf);
889                 buf->vb.state = STATE_ACTIVE;
890                 buf->count    = q->count++;
891                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
892                 dprintk(2,"[%p/%d] buffer_queue - first active\n",
893                         buf, buf->vb.i);
894
895         } else {
896                 prev = list_entry(q->active.prev, struct cx88_buffer, vb.queue);
897                 if (prev->vb.width  == buf->vb.width  &&
898                     prev->vb.height == buf->vb.height &&
899                     prev->fmt       == buf->fmt) {
900                         list_add_tail(&buf->vb.queue,&q->active);
901                         buf->vb.state = STATE_ACTIVE;
902                         buf->count    = q->count++;
903                         prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
904                         dprintk(2,"[%p/%d] buffer_queue - append to active\n",
905                                 buf, buf->vb.i);
906
907                 } else {
908                         list_add_tail(&buf->vb.queue,&q->queued);
909                         buf->vb.state = STATE_QUEUED;
910                         dprintk(2,"[%p/%d] buffer_queue - first queued\n",
911                                 buf, buf->vb.i);
912                 }
913         }
914 }
915
916 static void buffer_release(struct file *file, struct videobuf_buffer *vb)
917 {
918         struct cx88_buffer *buf = (struct cx88_buffer*)vb;
919         struct cx8800_fh   *fh  = file->private_data;
920
921         cx88_free_buffer(fh->dev->pci,buf);
922 }
923
924 struct videobuf_queue_ops cx8800_video_qops = {
925         .buf_setup    = buffer_setup,
926         .buf_prepare  = buffer_prepare,
927         .buf_queue    = buffer_queue,
928         .buf_release  = buffer_release,
929 };
930
931 /* ------------------------------------------------------------------ */
932
933 #if 0 /* overlay support not finished yet */
934 static u32* ov_risc_field(struct cx8800_dev *dev, struct cx8800_fh *fh,
935                           u32 *rp, struct btcx_skiplist *skips,
936                           u32 sync_line, int skip_even, int skip_odd)
937 {
938         int line,maxy,start,end,skip,nskips;
939         u32 ri,ra;
940         u32 addr;
941
942         /* sync instruction */
943         *(rp++) = cpu_to_le32(RISC_RESYNC | sync_line);
944
945         addr  = (unsigned long)dev->fbuf.base;
946         addr += dev->fbuf.fmt.bytesperline * fh->win.w.top;
947         addr += (fh->fmt->depth >> 3)      * fh->win.w.left;
948
949         /* scan lines */
950         for (maxy = -1, line = 0; line < fh->win.w.height;
951              line++, addr += dev->fbuf.fmt.bytesperline) {
952                 if ((line%2) == 0  &&  skip_even)
953                         continue;
954                 if ((line%2) == 1  &&  skip_odd)
955                         continue;
956
957                 /* calculate clipping */
958                 if (line > maxy)
959                         btcx_calc_skips(line, fh->win.w.width, &maxy,
960                                         skips, &nskips, fh->clips, fh->nclips);
961
962                 /* write out risc code */
963                 for (start = 0, skip = 0; start < fh->win.w.width; start = end) {
964                         if (skip >= nskips) {
965                                 ri  = RISC_WRITE;
966                                 end = fh->win.w.width;
967                         } else if (start < skips[skip].start) {
968                                 ri  = RISC_WRITE;
969                                 end = skips[skip].start;
970                         } else {
971                                 ri  = RISC_SKIP;
972                                 end = skips[skip].end;
973                                 skip++;
974                         }
975                         if (RISC_WRITE == ri)
976                                 ra = addr + (fh->fmt->depth>>3)*start;
977                         else
978                                 ra = 0;
979                                 
980                         if (0 == start)
981                                 ri |= RISC_SOL;
982                         if (fh->win.w.width == end)
983                                 ri |= RISC_EOL;
984                         ri |= (fh->fmt->depth>>3) * (end-start);
985
986                         *(rp++)=cpu_to_le32(ri);
987                         if (0 != ra)
988                                 *(rp++)=cpu_to_le32(ra);
989                 }
990         }
991         kfree(skips);
992         return rp;
993 }
994
995 static int ov_risc_frame(struct cx8800_dev *dev, struct cx8800_fh *fh,
996                          struct cx88_buffer *buf)
997 {
998         struct btcx_skiplist *skips;
999         u32 instructions,fields;
1000         u32 *rp;
1001         int rc;
1002         
1003         /* skip list for window clipping */
1004         if (NULL == (skips = kmalloc(sizeof(*skips) * fh->nclips,GFP_KERNEL)))
1005                 return -ENOMEM;
1006         
1007         fields = 0;
1008         if (V4L2_FIELD_HAS_TOP(fh->win.field))
1009                 fields++;
1010         if (V4L2_FIELD_HAS_BOTTOM(fh->win.field))
1011                 fields++;
1012
1013         /* estimate risc mem: worst case is (clip+1) * lines instructions
1014            + syncs + jump (all 2 dwords) */
1015         instructions  = (fh->nclips+1) * fh->win.w.height;
1016         instructions += 3 + 4;
1017         if ((rc = btcx_riscmem_alloc(dev->pci,&buf->risc,instructions*8)) < 0) {
1018                 kfree(skips);
1019                 return rc;
1020         }
1021
1022         /* write risc instructions */
1023         rp = buf->risc.cpu;
1024         switch (fh->win.field) {
1025         case V4L2_FIELD_TOP:
1026                 rp = ov_risc_field(dev, fh, rp, skips, 0,     0, 0);
1027                 break;
1028         case V4L2_FIELD_BOTTOM:
1029                 rp = ov_risc_field(dev, fh, rp, skips, 0x200, 0, 0);
1030                 break;
1031         case V4L2_FIELD_INTERLACED:
1032                 rp = ov_risc_field(dev, fh, rp, skips, 0,     0, 1);
1033                 rp = ov_risc_field(dev, fh, rp, skips, 0x200, 1, 0);
1034                 break;
1035         default:
1036                 BUG();
1037         }
1038
1039         /* save pointer to jmp instruction address */
1040         buf->risc.jmp = rp;
1041         kfree(skips);
1042         return 0;
1043 }
1044
1045 static int verify_window(struct cx8800_dev *dev, struct v4l2_window *win)
1046 {
1047         enum v4l2_field field;
1048         int maxw, maxh;
1049
1050         if (NULL == dev->fbuf.base)
1051                 return -EINVAL;
1052         if (win->w.width < 48 || win->w.height <  32)
1053                 return -EINVAL;
1054         if (win->clipcount > 2048)
1055                 return -EINVAL;
1056
1057         field = win->field;
1058         maxw  = norm_maxw(dev->tvnorm);
1059         maxh  = norm_maxh(dev->tvnorm);
1060
1061         if (V4L2_FIELD_ANY == field) {
1062                 field = (win->w.height > maxh/2)
1063                         ? V4L2_FIELD_INTERLACED
1064                         : V4L2_FIELD_TOP;
1065         }
1066         switch (field) {
1067         case V4L2_FIELD_TOP:
1068         case V4L2_FIELD_BOTTOM:
1069                 maxh = maxh / 2;
1070                 break;
1071         case V4L2_FIELD_INTERLACED:
1072                 break;
1073         default:
1074                 return -EINVAL;
1075         }
1076
1077         win->field = field;
1078         if (win->w.width > maxw)
1079                 win->w.width = maxw;
1080         if (win->w.height > maxh)
1081                 win->w.height = maxh;
1082         return 0;
1083 }
1084
1085 static int setup_window(struct cx8800_dev *dev, struct cx8800_fh *fh,
1086                         struct v4l2_window *win)
1087 {
1088         struct v4l2_clip *clips = NULL;
1089         int n,size,retval = 0;
1090
1091         if (NULL == fh->fmt)
1092                 return -EINVAL;
1093         retval = verify_window(dev,win);
1094         if (0 != retval)
1095                 return retval;
1096
1097         /* copy clips  --  luckily v4l1 + v4l2 are binary
1098            compatible here ...*/
1099         n = win->clipcount;
1100         size = sizeof(*clips)*(n+4);
1101         clips = kmalloc(size,GFP_KERNEL);
1102         if (NULL == clips)
1103                 return -ENOMEM;
1104         if (n > 0) {
1105                 if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
1106                         kfree(clips);
1107                         return -EFAULT;
1108                 }
1109         }
1110
1111         /* clip against screen */
1112         if (NULL != dev->fbuf.base)
1113                 n = btcx_screen_clips(dev->fbuf.fmt.width, dev->fbuf.fmt.height,
1114                                       &win->w, clips, n);
1115         btcx_sort_clips(clips,n);
1116
1117         /* 4-byte alignments */
1118         switch (fh->fmt->depth) {
1119         case 8:
1120         case 24:
1121                 btcx_align(&win->w, clips, n, 3);
1122                 break;
1123         case 16:
1124                 btcx_align(&win->w, clips, n, 1);
1125                 break;
1126         case 32:
1127                 /* no alignment fixups needed */
1128                 break;
1129         default:
1130                 BUG();
1131         }
1132         
1133         down(&fh->vidq.lock);
1134         if (fh->clips)
1135                 kfree(fh->clips);
1136         fh->clips    = clips;
1137         fh->nclips   = n;
1138         fh->win      = *win;
1139 #if 0
1140         fh->ov.setup_ok = 1;
1141 #endif
1142         
1143         /* update overlay if needed */
1144         retval = 0;
1145 #if 0
1146         if (check_btres(fh, RESOURCE_OVERLAY)) {
1147                 struct bttv_buffer *new;
1148                 
1149                 new = videobuf_alloc(sizeof(*new));
1150                 bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
1151                 retval = bttv_switch_overlay(btv,fh,new);
1152         }
1153 #endif
1154         up(&fh->vidq.lock);
1155         return retval;
1156 }
1157 #endif
1158
1159 /* ------------------------------------------------------------------ */
1160
1161 static struct videobuf_queue* get_queue(struct cx8800_fh *fh)
1162 {
1163         switch (fh->type) {
1164         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1165                 return &fh->vidq;
1166         case V4L2_BUF_TYPE_VBI_CAPTURE:
1167                 return &fh->vbiq;
1168         default:
1169                 BUG();
1170                 return NULL;
1171         }
1172 }
1173
1174 static int get_ressource(struct cx8800_fh *fh)
1175 {
1176         switch (fh->type) {
1177         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1178                 return RESOURCE_VIDEO;
1179         case V4L2_BUF_TYPE_VBI_CAPTURE:
1180                 return RESOURCE_VBI;
1181         default:
1182                 BUG();
1183                 return 0;
1184         }
1185 }
1186
1187 static int video_open(struct inode *inode, struct file *file)
1188 {
1189         int minor = iminor(inode);
1190         struct cx8800_dev *h,*dev = NULL;
1191         struct cx8800_fh *fh;
1192         struct list_head *list;
1193         enum v4l2_buf_type type = 0;
1194         int radio = 0;
1195         
1196         list_for_each(list,&cx8800_devlist) {
1197                 h = list_entry(list, struct cx8800_dev, devlist);
1198                 if (h->video_dev->minor == minor) {
1199                         dev  = h;
1200                         type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1201                 }
1202                 if (h->vbi_dev->minor == minor) {
1203                         dev  = h;
1204                         type = V4L2_BUF_TYPE_VBI_CAPTURE;
1205                 }
1206                 if (h->radio_dev &&
1207                     h->radio_dev->minor == minor) {
1208                         radio = 1;
1209                         dev   = h;
1210                 }
1211         }
1212         if (NULL == dev)
1213                 return -ENODEV;
1214
1215         dprintk(1,"open minor=%d radio=%d type=%s\n",
1216                 minor,radio,v4l2_type_names[type]);
1217
1218         /* allocate + initialize per filehandle data */
1219         fh = kmalloc(sizeof(*fh),GFP_KERNEL);
1220         if (NULL == fh)
1221                 return -ENOMEM;
1222         memset(fh,0,sizeof(*fh));
1223         file->private_data = fh;
1224         fh->dev      = dev;
1225         fh->radio    = radio;
1226         fh->type     = type;
1227         fh->width    = 320;
1228         fh->height   = 240;
1229         fh->fmt      = format_by_fourcc(V4L2_PIX_FMT_BGR24);
1230
1231         videobuf_queue_init(&fh->vidq, &cx8800_video_qops,
1232                             dev->pci, &dev->slock,
1233                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
1234                             V4L2_FIELD_INTERLACED,
1235                             sizeof(struct cx88_buffer));
1236         videobuf_queue_init(&fh->vbiq, &cx8800_vbi_qops,
1237                             dev->pci, &dev->slock,
1238                             V4L2_BUF_TYPE_VBI_CAPTURE,
1239                             V4L2_FIELD_SEQ_TB,
1240                             sizeof(struct cx88_buffer));
1241         init_MUTEX(&fh->vidq.lock);
1242         init_MUTEX(&fh->vbiq.lock);
1243
1244         if (fh->radio) {
1245                 dprintk(1,"video_open: setting radio device\n");
1246                 cx_write(MO_GP0_IO, cx88_boards[dev->board].radio.gpio0);
1247                 cx_write(MO_GP1_IO, cx88_boards[dev->board].radio.gpio1);
1248                 cx_write(MO_GP2_IO, cx88_boards[dev->board].radio.gpio2);
1249                 cx_write(MO_GP3_IO, cx88_boards[dev->board].radio.gpio3);
1250                 dev->tvaudio = WW_FM;
1251                 cx88_set_tvaudio(dev);
1252                 cx88_set_stereo(dev,V4L2_TUNER_MODE_STEREO);
1253                 cx8800_call_i2c_clients(dev,AUDC_SET_RADIO,NULL);
1254         }
1255
1256         return 0;
1257 }
1258
1259 static ssize_t
1260 video_read(struct file *file, char *data, size_t count, loff_t *ppos)
1261 {
1262         struct cx8800_fh *fh = file->private_data;
1263
1264         switch (fh->type) {
1265         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1266                 if (res_locked(fh->dev,RESOURCE_VIDEO))
1267                         return -EBUSY;
1268                 return videobuf_read_one(file, &fh->vidq, data, count, ppos);
1269         case V4L2_BUF_TYPE_VBI_CAPTURE:
1270                 if (!res_get(fh->dev,fh,RESOURCE_VBI))
1271                         return -EBUSY;
1272                 return videobuf_read_stream(file, &fh->vbiq, data, count, ppos, 1);
1273         default:
1274                 BUG();
1275                 return 0;
1276         }
1277 }
1278
1279 static unsigned int
1280 video_poll(struct file *file, struct poll_table_struct *wait)
1281 {
1282         struct cx8800_fh *fh = file->private_data;
1283
1284         if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)
1285                 return videobuf_poll_stream(file, &fh->vbiq, wait);
1286
1287         /* FIXME */
1288         return POLLERR;
1289 }
1290
1291 static int video_release(struct inode *inode, struct file *file)
1292 {
1293         struct cx8800_fh  *fh  = file->private_data;
1294         struct cx8800_dev *dev = fh->dev;
1295
1296         /* turn off overlay */
1297         if (res_check(fh, RESOURCE_OVERLAY)) {
1298                 /* FIXME */
1299                 res_free(dev,fh,RESOURCE_OVERLAY);
1300         }
1301
1302         /* stop video capture */
1303         if (res_check(fh, RESOURCE_VIDEO)) {
1304                 videobuf_queue_cancel(file,&fh->vidq);
1305                 res_free(dev,fh,RESOURCE_VIDEO);
1306         }
1307         if (fh->vidq.read_buf) {
1308                 buffer_release(file,fh->vidq.read_buf);
1309                 kfree(fh->vidq.read_buf);
1310         }
1311
1312         /* stop vbi capture */
1313         if (res_check(fh, RESOURCE_VBI)) {
1314                 if (fh->vbiq.streaming)
1315                         videobuf_streamoff(file,&fh->vbiq);
1316                 if (fh->vbiq.reading)
1317                         videobuf_read_stop(file,&fh->vbiq);
1318                 res_free(dev,fh,RESOURCE_VBI);
1319         }
1320
1321         file->private_data = NULL;
1322         kfree(fh);
1323         return 0;
1324 }
1325
1326 static int
1327 video_mmap(struct file *file, struct vm_area_struct * vma)
1328 {
1329         struct cx8800_fh *fh = file->private_data;
1330
1331         return videobuf_mmap_mapper(vma, get_queue(fh));
1332 }
1333
1334 /* ------------------------------------------------------------------ */
1335
1336 static int get_control(struct cx8800_dev *dev, struct v4l2_control *ctl)
1337 {
1338         struct cx88_ctrl *c = NULL;
1339         u32 value;
1340         int i;
1341         
1342         for (i = 0; i < CX8800_CTLS; i++)
1343                 if (cx8800_ctls[i].v.id == ctl->id)
1344                         c = &cx8800_ctls[i];
1345         if (NULL == c)
1346                 return -EINVAL;
1347
1348         value = c->sreg ? cx_sread(c->sreg) : cx_read(c->reg);
1349         switch (ctl->id) {
1350         case V4L2_CID_AUDIO_BALANCE:
1351                 ctl->value = (value & 0x40) ? (value & 0x3f) : (0x40 - (value & 0x3f));
1352                 break;
1353         default:
1354                 ctl->value = ((value + (c->off << c->shift)) & c->mask) >> c->shift;
1355                 break;
1356         }
1357         return 0;
1358 }
1359
1360 static int set_control(struct cx8800_dev *dev, struct v4l2_control *ctl)
1361 {
1362         struct cx88_ctrl *c = NULL;
1363         u32 v_sat_value;
1364         u32 value;
1365         int i;
1366
1367         for (i = 0; i < CX8800_CTLS; i++)
1368                 if (cx8800_ctls[i].v.id == ctl->id)
1369                         c = &cx8800_ctls[i];
1370         if (NULL == c)
1371                 return -EINVAL;
1372
1373         if (ctl->value < c->v.minimum)
1374                 return -ERANGE;
1375         if (ctl->value > c->v.maximum)
1376                 return -ERANGE;
1377         switch (ctl->id) {
1378         case V4L2_CID_AUDIO_BALANCE:
1379                 value = (ctl->value < 0x40) ? (0x40 - ctl->value) : ctl->value;
1380                 break;
1381         case V4L2_CID_SATURATION:
1382                 /* special v_sat handling */
1383                 v_sat_value = ctl->value - (0x7f - 0x5a);
1384                 if (v_sat_value > 0xff)
1385                         v_sat_value = 0xff;
1386                 if (v_sat_value < 0x00)
1387                         v_sat_value = 0x00;
1388                 cx_andor(MO_UV_SATURATION, 0xff00, v_sat_value << 8);
1389                 /* fall through to default route for u_sat */
1390         default:
1391                 value = ((ctl->value - c->off) << c->shift) & c->mask;
1392                 break;
1393         }
1394         dprintk(1,"set_control id=0x%X reg=0x%x val=0x%x%s\n",
1395                 ctl->id, c->reg, value, c->sreg ? " [shadowed]" : "");
1396         if (c->sreg) {
1397                 cx_sandor(c->sreg, c->reg, c->mask, value);
1398         } else {
1399                 cx_andor(c->reg, c->mask, value);
1400         }
1401         return 0;
1402 }
1403
1404 static void init_controls(struct cx8800_dev *dev)
1405 {
1406         static struct v4l2_control mute = {
1407                 .id    = V4L2_CID_AUDIO_MUTE,
1408                 .value = 1,
1409         };
1410         static struct v4l2_control volume = {
1411                 .id    = V4L2_CID_AUDIO_VOLUME,
1412                 .value = 0,
1413         };
1414
1415         set_control(dev,&mute);
1416         set_control(dev,&volume);
1417 }
1418
1419 /* ------------------------------------------------------------------ */
1420
1421 static int cx8800_g_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1422                         struct v4l2_format *f)
1423 {
1424         switch (f->type) {
1425         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1426                 memset(&f->fmt.pix,0,sizeof(f->fmt.pix));
1427                 f->fmt.pix.width        = fh->width;
1428                 f->fmt.pix.height       = fh->height;
1429                 f->fmt.pix.field        = fh->vidq.field;
1430                 f->fmt.pix.pixelformat  = fh->fmt->fourcc;
1431                 f->fmt.pix.bytesperline =
1432                         (f->fmt.pix.width * fh->fmt->depth) >> 3;
1433                 f->fmt.pix.sizeimage =
1434                         f->fmt.pix.height * f->fmt.pix.bytesperline;
1435                 return 0;
1436         case V4L2_BUF_TYPE_VBI_CAPTURE:
1437                 cx8800_vbi_fmt(dev, f);
1438                 return 0;
1439         default:
1440                 return -EINVAL;
1441         }
1442 }
1443
1444 static int cx8800_try_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1445                           struct v4l2_format *f)
1446 {
1447         switch (f->type) {
1448         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1449         {
1450                 struct cx8800_fmt *fmt;
1451                 enum v4l2_field field;
1452                 unsigned int maxw, maxh;
1453
1454                 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1455                 if (NULL == fmt)
1456                         return -EINVAL;
1457
1458                 field = f->fmt.pix.field;
1459                 maxw  = norm_maxw(dev->tvnorm);
1460                 maxh  = norm_maxh(dev->tvnorm);
1461
1462 #if 0
1463                 if (V4L2_FIELD_ANY == field) {
1464                         field = (f->fmt.pix.height > maxh/2)
1465                                 ? V4L2_FIELD_INTERLACED
1466                                 : V4L2_FIELD_BOTTOM;
1467                 }
1468 #else
1469                 field = V4L2_FIELD_INTERLACED;
1470 #endif
1471                 switch (field) {
1472                 case V4L2_FIELD_TOP:
1473                 case V4L2_FIELD_BOTTOM:
1474                         maxh = maxh / 2;
1475                         break;
1476                 case V4L2_FIELD_INTERLACED:
1477                         break;
1478                 default:
1479                         return -EINVAL;
1480                 }
1481
1482                 f->fmt.pix.field = field;
1483                 if (f->fmt.pix.width < 48)
1484                         f->fmt.pix.width = 48;
1485                 if (f->fmt.pix.height < 32)
1486                         f->fmt.pix.height = 32;
1487                 if (f->fmt.pix.width > maxw)
1488                         f->fmt.pix.width = maxw;
1489                 if (f->fmt.pix.height > maxh)
1490                         f->fmt.pix.height = maxh;
1491                 f->fmt.pix.bytesperline =
1492                         (f->fmt.pix.width * fmt->depth) >> 3;
1493                 f->fmt.pix.sizeimage =
1494                         f->fmt.pix.height * f->fmt.pix.bytesperline;
1495
1496                 return 0;
1497         }
1498         case V4L2_BUF_TYPE_VBI_CAPTURE:
1499                 cx8800_vbi_fmt(dev, f);
1500                 return 0;
1501         default:
1502                 return -EINVAL;
1503         }
1504 }
1505
1506 static int cx8800_s_fmt(struct cx8800_dev *dev, struct cx8800_fh *fh,
1507                         struct v4l2_format *f)
1508 {
1509         int err;
1510         
1511         switch (f->type) {
1512         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1513                 err = cx8800_try_fmt(dev,fh,f);
1514                 if (0 != err)
1515                         return err;
1516
1517                 fh->fmt        = format_by_fourcc(f->fmt.pix.pixelformat);
1518                 fh->width      = f->fmt.pix.width;
1519                 fh->height     = f->fmt.pix.height;
1520                 fh->vidq.field = f->fmt.pix.field;
1521                 return 0;
1522         case V4L2_BUF_TYPE_VBI_CAPTURE:
1523                 cx8800_vbi_fmt(dev, f);
1524                 return 0;
1525         default:
1526                 return -EINVAL;
1527         }
1528 }
1529
1530 /*
1531  * This function is _not_ called directly, but from
1532  * video_generic_ioctl (and maybe others).  userspace
1533  * copying is done already, arg is a kernel pointer.
1534  */
1535 static int video_do_ioctl(struct inode *inode, struct file *file,
1536                           unsigned int cmd, void *arg)
1537 {
1538         struct cx8800_fh  *fh  = file->private_data;
1539         struct cx8800_dev *dev = fh->dev;
1540 #if 0
1541         unsigned long flags;
1542 #endif
1543         int err;
1544
1545         if (video_debug > 1)
1546                 cx88_print_ioctl(dev->name,cmd);
1547         switch (cmd) {
1548         case VIDIOC_QUERYCAP:
1549         {
1550                 struct v4l2_capability *cap = arg;
1551                 
1552                 memset(cap,0,sizeof(*cap));
1553                 strcpy(cap->driver, "cx8800");
1554                 strlcpy(cap->card, cx88_boards[dev->board].name,
1555                         sizeof(cap->card));
1556                 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
1557                 cap->version = CX88_VERSION_CODE;
1558                 cap->capabilities =
1559                         V4L2_CAP_VIDEO_CAPTURE |
1560                         V4L2_CAP_READWRITE     |
1561                         V4L2_CAP_STREAMING     |
1562                         V4L2_CAP_VBI_CAPTURE   |
1563 #if 0
1564                         V4L2_CAP_VIDEO_OVERLAY |
1565 #endif
1566                         0;
1567                 if (UNSET != dev->tuner_type)
1568                         cap->capabilities |= V4L2_CAP_TUNER;
1569
1570                 return 0;
1571         }
1572
1573         /* ---------- tv norms ---------- */
1574         case VIDIOC_ENUMSTD:
1575         {
1576                 struct v4l2_standard *e = arg;
1577                 unsigned int i;
1578
1579                 i = e->index;
1580                 if (i >= ARRAY_SIZE(tvnorms))
1581                         return -EINVAL;
1582                 err = v4l2_video_std_construct(e, tvnorms[e->index].id,
1583                                                tvnorms[e->index].name);
1584                 e->index = i;
1585                 if (err < 0)
1586                         return err;
1587                 return 0;
1588         }
1589         case VIDIOC_G_STD:
1590         {
1591                 v4l2_std_id *id = arg;
1592
1593                 *id = dev->tvnorm->id;
1594                 return 0;
1595         }
1596         case VIDIOC_S_STD:
1597         {
1598                 v4l2_std_id *id = arg;
1599                 unsigned int i;
1600
1601                 for(i = 0; i < ARRAY_SIZE(tvnorms); i++)
1602                         if (*id & tvnorms[i].id)
1603                                 break;
1604                 if (i == ARRAY_SIZE(tvnorms))
1605                         return -EINVAL;
1606
1607                 down(&dev->lock);
1608                 set_tvnorm(dev,&tvnorms[i]);
1609                 up(&dev->lock);
1610                 return 0;
1611         }
1612
1613         /* ------ input switching ---------- */
1614         case VIDIOC_ENUMINPUT:
1615         {
1616                 static const char *iname[] = {
1617                         [ CX88_VMUX_COMPOSITE1 ] = "Composite1",
1618                         [ CX88_VMUX_COMPOSITE2 ] = "Composite2",
1619                         [ CX88_VMUX_COMPOSITE3 ] = "Composite3",
1620                         [ CX88_VMUX_COMPOSITE4 ] = "Composite4",
1621                         [ CX88_VMUX_TELEVISION ] = "Television",
1622                         [ CX88_VMUX_SVIDEO     ] = "S-Video",
1623                         [ CX88_VMUX_DEBUG      ] = "for debug only",
1624                 };
1625                 struct v4l2_input *i = arg;
1626                 unsigned int n;
1627
1628                 n = i->index;
1629                 if (n >= 4)
1630                         return -EINVAL;
1631                 if (0 == INPUT(n)->type)
1632                         return -EINVAL;
1633                 memset(i,0,sizeof(*i));
1634                 i->index = n;
1635                 i->type  = V4L2_INPUT_TYPE_CAMERA;
1636                 strcpy(i->name,iname[INPUT(n)->type]);
1637                 if (CX88_VMUX_TELEVISION == INPUT(n)->type)
1638                         i->type = V4L2_INPUT_TYPE_TUNER;
1639                 for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
1640                         i->std |= tvnorms[n].id;
1641                 return 0;
1642         }
1643         case VIDIOC_G_INPUT:
1644         {
1645                 unsigned int *i = arg;
1646
1647                 *i = dev->input;
1648                 return 0;
1649         }
1650         case VIDIOC_S_INPUT:
1651         {
1652                 unsigned int *i = arg;
1653                 
1654                 if (*i >= 4)
1655                         return -EINVAL;
1656                 down(&dev->lock);
1657                 video_mux(dev,*i);
1658                 up(&dev->lock);
1659                 return 0;
1660         }
1661
1662         /* --- capture ioctls ---------------------------------------- */
1663         case VIDIOC_ENUM_FMT:
1664         {
1665                 struct v4l2_fmtdesc *f = arg;
1666                 enum v4l2_buf_type type;
1667                 unsigned int index;
1668
1669                 index = f->index;
1670                 type  = f->type;
1671                 switch (type) {
1672                 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1673                         if (index >= ARRAY_SIZE(formats))
1674                                 return -EINVAL;
1675                         memset(f,0,sizeof(*f));
1676                         f->index = index;
1677                         f->type  = type;
1678                         strlcpy(f->description,formats[index].name,sizeof(f->description));
1679                         f->pixelformat = formats[index].fourcc;
1680                         break;
1681                 default:
1682                         return -EINVAL;
1683                 }
1684                 return 0;
1685         }
1686         case VIDIOC_G_FMT:
1687         {
1688                 struct v4l2_format *f = arg;
1689                 return cx8800_g_fmt(dev,fh,f);
1690         }
1691         case VIDIOC_S_FMT:
1692         {
1693                 struct v4l2_format *f = arg;
1694                 return cx8800_s_fmt(dev,fh,f);
1695         }
1696         case VIDIOC_TRY_FMT:
1697         {
1698                 struct v4l2_format *f = arg;
1699                 return cx8800_try_fmt(dev,fh,f);
1700         }
1701
1702         /* --- controls ---------------------------------------------- */
1703         case VIDIOC_QUERYCTRL:
1704         {
1705                 struct v4l2_queryctrl *c = arg;
1706                 int i;
1707
1708                 if (c->id <  V4L2_CID_BASE ||
1709                     c->id >= V4L2_CID_LASTP1)
1710                         return -EINVAL;
1711                 for (i = 0; i < CX8800_CTLS; i++)
1712                         if (cx8800_ctls[i].v.id == c->id)
1713                                 break;
1714                 if (i == CX8800_CTLS) {
1715                         *c = no_ctl;
1716                         return 0;
1717                 }
1718                 *c = cx8800_ctls[i].v;
1719                 return 0;
1720         }
1721         case VIDIOC_G_CTRL:
1722                 return get_control(dev,arg);
1723         case VIDIOC_S_CTRL:
1724                 return set_control(dev,arg);
1725                 
1726         /* --- tuner ioctls ------------------------------------------ */
1727         case VIDIOC_G_TUNER:
1728         {
1729                 struct v4l2_tuner *t = arg;
1730                 u32 reg;
1731                 
1732                 if (UNSET == dev->tuner_type)
1733                         return -EINVAL;
1734                 if (0 != t->index)
1735                         return -EINVAL;
1736
1737                 memset(t,0,sizeof(*t));
1738                 strcpy(t->name, "Television");
1739                 t->type       = V4L2_TUNER_ANALOG_TV;
1740                 t->capability = V4L2_TUNER_CAP_NORM;
1741                 t->rangehigh  = 0xffffffffUL;
1742
1743                 cx88_get_stereo(dev ,t);
1744                 reg = cx_read(MO_DEVICE_STATUS);
1745                 t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1746                 return 0;
1747         }
1748         case VIDIOC_S_TUNER:
1749         {
1750                 struct v4l2_tuner *t = arg;
1751
1752                 if (UNSET == dev->tuner_type)
1753                         return -EINVAL;
1754                 if (0 != t->index)
1755                         return -EINVAL;
1756                 cx88_set_stereo(dev,t->audmode);
1757                 return 0;
1758         }
1759         case VIDIOC_G_FREQUENCY:
1760         {
1761                 struct v4l2_frequency *f = arg;
1762
1763                 if (UNSET == dev->tuner_type)
1764                         return -EINVAL;
1765                 if (f->tuner != 0)
1766                         return -EINVAL;
1767                 memset(f,0,sizeof(*f));
1768                 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1769                 f->frequency = dev->freq;
1770                 return 0;
1771         }
1772         case VIDIOC_S_FREQUENCY:
1773         {
1774                 struct v4l2_frequency *f = arg;
1775
1776                 if (UNSET == dev->tuner_type)
1777                         return -EINVAL;
1778                 if (f->tuner != 0)
1779                         return -EINVAL;
1780                 if (0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV)
1781                         return -EINVAL;
1782                 if (1 == fh->radio && f->type != V4L2_TUNER_RADIO)
1783                         return -EINVAL;
1784                 down(&dev->lock);
1785                 dev->freq = f->frequency;
1786                 cx8800_call_i2c_clients(dev,VIDIOCSFREQ,&dev->freq);
1787                 up(&dev->lock);
1788                 return 0;
1789         }
1790
1791         /* --- streaming capture ------------------------------------- */
1792         case VIDIOCGMBUF:
1793         {
1794                 struct video_mbuf *mbuf = arg;
1795                 struct videobuf_queue *q;
1796                 struct v4l2_requestbuffers req;
1797                 unsigned int i;
1798
1799                 q = get_queue(fh);
1800                 memset(&req,0,sizeof(req));
1801                 req.type   = q->type;
1802                 req.count  = 8;
1803                 req.memory = V4L2_MEMORY_MMAP;
1804                 err = videobuf_reqbufs(file,q,&req);
1805                 if (err < 0)
1806                         return err;
1807                 memset(mbuf,0,sizeof(*mbuf));
1808                 mbuf->frames = req.count;
1809                 mbuf->size   = 0;
1810                 for (i = 0; i < mbuf->frames; i++) {
1811                         mbuf->offsets[i]  = q->bufs[i]->boff;
1812                         mbuf->size       += q->bufs[i]->bsize;
1813                 }
1814                 return 0;
1815         }
1816         case VIDIOC_REQBUFS:
1817                 return videobuf_reqbufs(file, get_queue(fh), arg);
1818
1819         case VIDIOC_QUERYBUF:
1820                 return videobuf_querybuf(get_queue(fh), arg);
1821
1822         case VIDIOC_QBUF:
1823                 return videobuf_qbuf(file, get_queue(fh), arg);
1824
1825         case VIDIOC_DQBUF:
1826                 return videobuf_dqbuf(file, get_queue(fh), arg);
1827
1828         case VIDIOC_STREAMON:
1829         {
1830                 int res = get_ressource(fh);
1831
1832                 if (!res_get(dev,fh,res))
1833                         return -EBUSY;
1834                 return videobuf_streamon(file, get_queue(fh));
1835         }
1836         case VIDIOC_STREAMOFF:
1837         {
1838                 int res = get_ressource(fh);
1839
1840                 err = videobuf_streamoff(file, get_queue(fh));
1841                 if (err < 0)
1842                         return err;
1843                 res_free(dev,fh,res);
1844                 return 0;
1845         }
1846
1847         default:
1848                 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1849                                                   video_do_ioctl);
1850         }
1851         return 0;
1852 }
1853
1854 static int video_ioctl(struct inode *inode, struct file *file,
1855                        unsigned int cmd, unsigned long arg)
1856 {
1857         return video_usercopy(inode, file, cmd, arg, video_do_ioctl);
1858 }
1859
1860 /* ----------------------------------------------------------- */
1861
1862 static int radio_do_ioctl(struct inode *inode, struct file *file,
1863                         unsigned int cmd, void *arg)
1864 {
1865         struct cx8800_fh *fh = file->private_data;
1866         struct cx8800_dev *dev = fh->dev;
1867         
1868         if (video_debug > 1)
1869                 cx88_print_ioctl(dev->name,cmd);
1870
1871         switch (cmd) {
1872         case VIDIOC_QUERYCAP:
1873         {
1874                 struct v4l2_capability *cap = arg;
1875                 
1876                 memset(cap,0,sizeof(*cap));
1877                 strcpy(cap->driver, "cx8800");
1878                 strlcpy(cap->card, cx88_boards[dev->board].name,
1879                         sizeof(cap->card));
1880                 sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1881                 cap->version = CX88_VERSION_CODE;
1882                 cap->capabilities = V4L2_CAP_TUNER;
1883                 return 0;
1884         }
1885         case VIDIOC_G_TUNER:
1886         {
1887                 struct v4l2_tuner *t = arg;
1888                 struct video_tuner vt;
1889
1890                 if (t->index > 0)
1891                         return -EINVAL;
1892
1893                 memset(t,0,sizeof(*t));
1894                 strcpy(t->name, "Radio");
1895                 t->rangelow  = (int)(65*16);
1896                 t->rangehigh = (int)(108*16);
1897                 
1898                 memset(&vt,0,sizeof(vt));
1899                 cx8800_call_i2c_clients(dev,VIDIOCGTUNER,&vt);
1900                 t->signal = vt.signal;
1901                 return 0;
1902         }
1903         case VIDIOC_ENUMINPUT:
1904         {
1905                 struct v4l2_input *i = arg;
1906                 
1907                 if (i->index != 0)
1908                         return -EINVAL;
1909                 strcpy(i->name,"Radio");
1910                 i->type = V4L2_INPUT_TYPE_TUNER;
1911                 return 0;
1912         }
1913         case VIDIOC_G_INPUT:
1914         {
1915                 int *i = arg;
1916                 *i = 0;
1917                 return 0;
1918         }
1919         case VIDIOC_G_AUDIO:
1920         {
1921                 struct v4l2_audio *a = arg;
1922
1923                 memset(a,0,sizeof(*a));
1924                 strcpy(a->name,"Radio");
1925                 return 0;
1926         }
1927         case VIDIOC_G_STD:
1928         {
1929                 v4l2_std_id *id = arg;
1930                 *id = 0;
1931                 return 0;
1932         }
1933         case VIDIOC_S_AUDIO:
1934         case VIDIOC_S_TUNER:
1935         case VIDIOC_S_INPUT:
1936         case VIDIOC_S_STD:
1937                 return 0;
1938
1939         case VIDIOC_QUERYCTRL:
1940         {
1941                 struct v4l2_queryctrl *c = arg;
1942                 int i;
1943
1944                 if (c->id <  V4L2_CID_BASE ||
1945                     c->id >= V4L2_CID_LASTP1)
1946                         return -EINVAL;
1947                 if (c->id == V4L2_CID_AUDIO_MUTE) {
1948                         for (i = 0; i < CX8800_CTLS; i++)
1949                                 if (cx8800_ctls[i].v.id == c->id)
1950                                         break;
1951                         *c = cx8800_ctls[i].v;
1952                 } else
1953                         *c = no_ctl;
1954                 return 0;
1955         }
1956
1957
1958         case VIDIOC_G_CTRL:
1959         case VIDIOC_S_CTRL:
1960         case VIDIOC_G_FREQUENCY:
1961         case VIDIOC_S_FREQUENCY:
1962                 return video_do_ioctl(inode,file,cmd,arg);
1963                 
1964         default:
1965                 return v4l_compat_translate_ioctl(inode,file,cmd,arg,
1966                                                   radio_do_ioctl);
1967         }
1968         return 0;
1969 };
1970
1971 static int radio_ioctl(struct inode *inode, struct file *file,
1972                         unsigned int cmd, unsigned long arg)
1973 {
1974         return video_usercopy(inode, file, cmd, arg, radio_do_ioctl);
1975 };
1976
1977 /* ----------------------------------------------------------- */
1978
1979 static void cx8800_vid_timeout(unsigned long data)
1980 {
1981         struct cx8800_dev *dev = (struct cx8800_dev*)data;
1982         struct cx88_dmaqueue *q = &dev->vidq;
1983         struct cx88_buffer *buf;
1984         unsigned long flags;
1985
1986         cx88_sram_channel_dump(dev, &cx88_sram_channels[SRAM_CH21]);
1987         //cx88_risc_disasm(dev,&dev->vidq.stopper);
1988         
1989         cx_clear(MO_VID_DMACNTRL, 0x11);
1990         cx_clear(VID_CAPTURE_CONTROL, 0x06);
1991
1992         spin_lock_irqsave(&dev->slock,flags);
1993         while (!list_empty(&q->active)) {
1994                 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1995                 list_del(&buf->vb.queue);
1996                 buf->vb.state = STATE_ERROR;
1997                 wake_up(&buf->vb.done);
1998                 printk("%s: [%p/%d] timeout - dma=0x%08lx\n", dev->name,
1999                        buf, buf->vb.i, (unsigned long)buf->risc.dma);
2000         }
2001         restart_video_queue(dev,q);
2002         spin_unlock_irqrestore(&dev->slock,flags);
2003 }
2004
2005 static void cx8800_wakeup(struct cx8800_dev *dev,
2006                           struct cx88_dmaqueue *q, u32 count)
2007 {
2008         struct cx88_buffer *buf;
2009
2010         for (;;) {
2011                 if (list_empty(&q->active))
2012                         break;
2013                 buf = list_entry(q->active.next,
2014                                  struct cx88_buffer, vb.queue);
2015                 if (buf->count > count)
2016                         break;
2017                 do_gettimeofday(&buf->vb.ts);
2018                 dprintk(2,"[%p/%d] wakeup reg=%d buf=%d\n",buf,buf->vb.i,
2019                         count, buf->count);
2020                 buf->vb.state = STATE_DONE;
2021                 list_del(&buf->vb.queue);
2022                 wake_up(&buf->vb.done);
2023         }
2024         if (list_empty(&q->active)) {
2025                 del_timer(&q->timeout);
2026         } else {
2027                 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
2028         }
2029 }
2030
2031 static void cx8800_vid_irq(struct cx8800_dev *dev)
2032 {
2033         u32 status, mask, count;
2034
2035         status = cx_read(MO_VID_INTSTAT);
2036         mask   = cx_read(MO_VID_INTMSK);
2037         if (0 == (status & mask))
2038                 return;
2039         cx_write(MO_VID_INTSTAT, status);
2040         if (irq_debug  ||  (status & mask & ~0xff))
2041                 cx88_print_irqbits(dev->name, "irq vid",
2042                                    cx88_vid_irqs, status, mask);
2043
2044         /* risc op code error */
2045         if (status & (1 << 16)) {
2046                 printk(KERN_WARNING "%s: video risc op code error\n",dev->name);
2047                 cx_clear(MO_VID_DMACNTRL, 0x11);
2048                 cx_clear(VID_CAPTURE_CONTROL, 0x06);
2049                 cx88_sram_channel_dump(dev, &cx88_sram_channels[SRAM_CH21]);
2050         }
2051         
2052         /* risc1 y */
2053         if (status & 0x01) {
2054                 spin_lock(&dev->slock);
2055                 count = cx_read(MO_VIDY_GPCNT);
2056                 cx8800_wakeup(dev, &dev->vidq, count);
2057                 spin_unlock(&dev->slock);
2058         }
2059
2060         /* risc1 vbi */
2061         if (status & 0x08) {
2062                 spin_lock(&dev->slock);
2063                 count = cx_read(MO_VBI_GPCNT);
2064                 cx8800_wakeup(dev, &dev->vbiq, count);
2065                 spin_unlock(&dev->slock);
2066         }
2067
2068         /* risc2 y */
2069         if (status & 0x10) {
2070                 dprintk(2,"stopper video\n");
2071                 spin_lock(&dev->slock);
2072                 restart_video_queue(dev,&dev->vidq);
2073                 spin_unlock(&dev->slock);
2074         }
2075
2076         /* risc2 vbi */
2077         if (status & 0x80) {
2078                 dprintk(2,"stopper vbi\n");
2079                 spin_lock(&dev->slock);
2080                 cx8800_restart_vbi_queue(dev,&dev->vbiq);
2081                 spin_unlock(&dev->slock);
2082         }
2083 }
2084
2085 static irqreturn_t cx8800_irq(int irq, void *dev_id, struct pt_regs *regs)
2086 {
2087         struct cx8800_dev *dev = dev_id;
2088         u32 status, mask;
2089         int loop, handled = 0;
2090
2091         for (loop = 0; loop < 10; loop++) {
2092                 status = cx_read(MO_PCI_INTSTAT);
2093                 mask   = cx_read(MO_PCI_INTMSK);
2094                 if (0 == (status & mask))
2095                         goto out;
2096                 handled = 1;
2097                 cx_write(MO_PCI_INTSTAT, status);
2098                 if (irq_debug  ||  (status & mask & ~0x1f))
2099                         cx88_print_irqbits(dev->name, "irq pci",
2100                                            cx88_pci_irqs, status, mask);
2101
2102                 if (status & 1)
2103                         cx8800_vid_irq(dev);
2104         };
2105         if (10 == loop) {
2106                 printk(KERN_WARNING "%s: irq loop -- clearing mask\n",
2107                        dev->name);
2108                 cx_write(MO_PCI_INTMSK,0);
2109         }
2110         
2111  out:
2112         return IRQ_RETVAL(handled);
2113 }
2114
2115 /* ----------------------------------------------------------- */
2116 /* exported stuff                                              */
2117
2118 static struct file_operations video_fops =
2119 {
2120         .owner         = THIS_MODULE,
2121         .open          = video_open,
2122         .release       = video_release,
2123         .read          = video_read,
2124         .poll          = video_poll,
2125         .mmap          = video_mmap,
2126         .ioctl         = video_ioctl,
2127         .llseek        = no_llseek,
2128 };
2129
2130 struct video_device cx8800_video_template =
2131 {
2132         .name          = "cx8800-video",
2133         .type          = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
2134         .hardware      = 0,
2135         .fops          = &video_fops,
2136         .minor         = -1,
2137 };
2138
2139 struct video_device cx8800_vbi_template =
2140 {
2141         .name          = "cx8800-vbi",
2142         .type          = VID_TYPE_TELETEXT|VID_TYPE_TUNER,
2143         .hardware      = 0,
2144         .fops          = &video_fops,
2145         .minor         = -1,
2146 };
2147
2148 static struct file_operations radio_fops =
2149 {
2150         .owner         = THIS_MODULE,
2151         .open          = video_open,
2152         .release       = video_release,
2153         .ioctl         = radio_ioctl,
2154         .llseek        = no_llseek,
2155 };
2156
2157 struct video_device cx8800_radio_template =
2158 {
2159         .name          = "cx8800-radio",
2160         .type          = VID_TYPE_TUNER,
2161         .hardware      = 0,
2162         .fops          = &radio_fops,
2163         .minor         = -1,
2164 };
2165
2166 /* ----------------------------------------------------------- */
2167
2168 static void cx8800_shutdown(struct cx8800_dev *dev)
2169 {
2170         /* disable RISC controller + IRQs */
2171         cx_write(MO_DEV_CNTRL2, 0);
2172
2173         /* stop dma transfers */
2174         cx_write(MO_VID_DMACNTRL, 0x0);
2175         cx_write(MO_AUD_DMACNTRL, 0x0);
2176         cx_write(MO_TS_DMACNTRL, 0x0);
2177         cx_write(MO_VIP_DMACNTRL, 0x0);
2178         cx_write(MO_GPHST_DMACNTRL, 0x0);
2179
2180         /* stop interupts */
2181         cx_write(MO_PCI_INTMSK, 0x0);
2182         cx_write(MO_VID_INTMSK, 0x0);
2183         cx_write(MO_AUD_INTMSK, 0x0);
2184         cx_write(MO_TS_INTMSK, 0x0);
2185         cx_write(MO_VIP_INTMSK, 0x0);
2186         cx_write(MO_GPHST_INTMSK, 0x0);
2187
2188         /* stop capturing */
2189         cx_write(VID_CAPTURE_CONTROL, 0);
2190 }
2191
2192 static int cx8800_reset(struct cx8800_dev *dev)
2193 {
2194         dprintk(1,"cx8800_reset\n");
2195
2196         cx8800_shutdown(dev);
2197         
2198         /* clear irq status */
2199         cx_write(MO_VID_INTSTAT, 0xFFFFFFFF); // Clear PIV int
2200         cx_write(MO_PCI_INTSTAT, 0xFFFFFFFF); // Clear PCI int
2201         cx_write(MO_INT1_STAT,   0xFFFFFFFF); // Clear RISC int
2202
2203         /* wait a bit */
2204         set_current_state(TASK_INTERRUPTIBLE);
2205         schedule_timeout(HZ/10);
2206         
2207         /* init sram */
2208         cx88_sram_channel_setup(dev, &cx88_sram_channels[SRAM_CH21], 720*4, 0);
2209         cx88_sram_channel_setup(dev, &cx88_sram_channels[SRAM_CH22], 128, 0);
2210         cx88_sram_channel_setup(dev, &cx88_sram_channels[SRAM_CH23], 128, 0);
2211         cx88_sram_channel_setup(dev, &cx88_sram_channels[SRAM_CH24], 128, 0);
2212         cx88_sram_channel_setup(dev, &cx88_sram_channels[SRAM_CH25], 128, 0);
2213         cx88_sram_channel_setup(dev, &cx88_sram_channels[SRAM_CH26], 128, 0);
2214         
2215         /* misc init ... */
2216         cx_write(MO_INPUT_FORMAT, ((1 << 13) |   // agc enable
2217                                    (1 << 12) |   // agc gain
2218                                    (1 << 11) |   // adaptibe agc
2219                                    (0 << 10) |   // chroma agc
2220                                    (0 <<  9) |   // ckillen
2221                                    (7)));
2222
2223         /* setup image format */
2224         cx_andor(MO_COLOR_CTRL, 0x4000, 0x4000);
2225
2226         /* setup FIFO Threshholds */
2227         cx_write(MO_PDMA_STHRSH,   0x0807);
2228         cx_write(MO_PDMA_DTHRSH,   0x0807);
2229
2230         /* fixes flashing of image */
2231         cx_write(MO_AGC_SYNC_TIP1, 0x0380000F);
2232         cx_write(MO_AGC_BACK_VBI,  0x00E00555);
2233         
2234         cx_write(MO_VID_INTSTAT,   0xFFFFFFFF); // Clear PIV int
2235         cx_write(MO_PCI_INTSTAT,   0xFFFFFFFF); // Clear PCI int
2236         cx_write(MO_INT1_STAT,     0xFFFFFFFF); // Clear RISC int
2237
2238         return 0;
2239 }
2240
2241 static struct video_device *vdev_init(struct cx8800_dev *dev,
2242                                       struct video_device *template,
2243                                       char *type)
2244 {
2245         struct video_device *vfd;
2246
2247         vfd = video_device_alloc();
2248         if (NULL == vfd)
2249                 return NULL;
2250         *vfd = *template;
2251         vfd->minor   = -1;
2252         vfd->dev     = &dev->pci->dev;
2253         vfd->release = video_device_release;
2254         snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
2255                  dev->name, type, cx88_boards[dev->board].name);
2256         return vfd;
2257 }
2258
2259 static void cx8800_unregister_video(struct cx8800_dev *dev)
2260 {
2261         if (dev->radio_dev) {
2262                 if (-1 != dev->radio_dev->minor)
2263                         video_unregister_device(dev->radio_dev);
2264                 else
2265                         video_device_release(dev->radio_dev);
2266                 dev->radio_dev = NULL;
2267         }
2268         if (dev->vbi_dev) {
2269                 if (-1 != dev->vbi_dev->minor)
2270                         video_unregister_device(dev->vbi_dev);
2271                 else
2272                         video_device_release(dev->vbi_dev);
2273                 dev->vbi_dev = NULL;
2274         }
2275         if (dev->video_dev) {
2276                 if (-1 != dev->video_dev->minor)
2277                         video_unregister_device(dev->video_dev);
2278                 else
2279                         video_device_release(dev->video_dev);
2280                 dev->video_dev = NULL;
2281         }
2282 }
2283
2284 /* debug that damn oops ... */
2285 static unsigned int oops = 0;
2286 MODULE_PARM(oops,"i");
2287 #define OOPS(msg) if (oops) printk("%s: %s\n",__FUNCTION__,msg);
2288
2289 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
2290                                     const struct pci_device_id *pci_id)
2291 {
2292         struct cx8800_dev *dev;
2293         unsigned int i;
2294         int err;
2295
2296         dev = kmalloc(sizeof(*dev),GFP_KERNEL);
2297         if (NULL == dev)
2298                 return -ENOMEM;
2299         memset(dev,0,sizeof(*dev));
2300
2301         /* pci init */
2302         OOPS("pci init");
2303         dev->pci = pci_dev;
2304         if (pci_enable_device(pci_dev)) {
2305                 err = -EIO;
2306                 goto fail1;
2307         }
2308         sprintf(dev->name,"cx%x[%d]",pci_dev->device,cx8800_devcount);
2309
2310         /* pci quirks */
2311         OOPS("pci quirks");
2312         cx88_pci_quirks(dev->name, dev->pci, &latency);
2313         if (UNSET != latency) {
2314                 printk(KERN_INFO "%s: setting pci latency timer to %d\n",
2315                        dev->name,latency);
2316                 pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, latency);
2317         }
2318
2319         /* print pci info */
2320         OOPS("pci info");
2321         pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
2322         pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER,  &dev->pci_lat);
2323         printk(KERN_INFO "%s: found at %s, rev: %d, irq: %d, "
2324                "latency: %d, mmio: 0x%lx\n", dev->name,
2325                pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
2326                dev->pci_lat,pci_resource_start(pci_dev,0));
2327
2328         pci_set_master(pci_dev);
2329         if (!pci_dma_supported(pci_dev,0xffffffff)) {
2330                 printk("%s: Oops: no 32bit PCI DMA ???\n",dev->name);
2331                 err = -EIO;
2332                 goto fail1;
2333         }
2334
2335         /* board config */
2336         OOPS("board config");
2337         dev->board = card[cx8800_devcount];
2338         for (i = 0; UNSET == dev->board  &&  i < cx88_idcount; i++) 
2339                 if (pci_dev->subsystem_vendor == cx88_subids[i].subvendor &&
2340                     pci_dev->subsystem_device == cx88_subids[i].subdevice)
2341                         dev->board = cx88_subids[i].card;
2342         if (UNSET == dev->board)
2343                 dev->board = CX88_BOARD_UNKNOWN;
2344         printk(KERN_INFO "%s: subsystem: %04x:%04x, board: %s [card=%d,%s]\n",
2345                dev->name,pci_dev->subsystem_vendor,
2346                pci_dev->subsystem_device,cx88_boards[dev->board].name,
2347                dev->board, card[cx8800_devcount] == dev->board ?
2348                "insmod option" : "autodetected");
2349
2350         dev->tuner_type = tuner[cx8800_devcount];
2351         if (UNSET == dev->tuner_type)
2352                 dev->tuner_type = cx88_boards[dev->board].tuner_type;
2353
2354         /* get mmio */
2355         OOPS("get mmio");
2356         if (!request_mem_region(pci_resource_start(pci_dev,0),
2357                                 pci_resource_len(pci_dev,0),
2358                                 dev->name)) {
2359                 err = -EBUSY;
2360                 printk(KERN_ERR "%s: can't get MMIO memory @ 0x%lx\n",
2361                        dev->name,pci_resource_start(pci_dev,0));
2362                 goto fail1;
2363         }
2364         dev->lmmio = ioremap(pci_resource_start(pci_dev,0),
2365                              pci_resource_len(pci_dev,0));
2366         dev->bmmio = (u8*)dev->lmmio;
2367
2368         /* initialize driver struct */
2369         OOPS("init structs");
2370         init_MUTEX(&dev->lock);
2371         dev->slock = SPIN_LOCK_UNLOCKED;
2372         dev->tvnorm = tvnorms;
2373
2374         /* init video dma queues */
2375         INIT_LIST_HEAD(&dev->vidq.active);
2376         INIT_LIST_HEAD(&dev->vidq.queued);
2377         dev->vidq.timeout.function = cx8800_vid_timeout;
2378         dev->vidq.timeout.data     = (unsigned long)dev;
2379         init_timer(&dev->vidq.timeout);
2380         cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
2381                           MO_VID_DMACNTRL,0x11,0x00);
2382
2383         /* init vbi dma queues */
2384         INIT_LIST_HEAD(&dev->vbiq.active);
2385         INIT_LIST_HEAD(&dev->vbiq.queued);
2386         dev->vbiq.timeout.function = cx8800_vbi_timeout;
2387         dev->vbiq.timeout.data     = (unsigned long)dev;
2388         init_timer(&dev->vbiq.timeout);
2389         cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
2390                           MO_VID_DMACNTRL,0x88,0x00);
2391
2392         /* initialize hardware */
2393         OOPS("reset hardware");
2394         cx8800_reset(dev);
2395
2396         /* get irq */
2397         OOPS("install irq handler");
2398         err = request_irq(pci_dev->irq, cx8800_irq,
2399                           SA_SHIRQ | SA_INTERRUPT, dev->name, dev);
2400         if (err < 0) {
2401                 printk(KERN_ERR "%s: can't get IRQ %d\n",
2402                        dev->name,pci_dev->irq);
2403                 goto fail2;
2404         }
2405
2406         /* register i2c bus + load i2c helpers */
2407         OOPS("i2c setup");
2408         cx8800_i2c_init(dev);
2409         OOPS("card setup");
2410         cx88_card_setup(dev);
2411
2412         /* load and configure helper modules */
2413         OOPS("configure i2c clients");
2414         if (TUNER_ABSENT != dev->tuner_type)
2415                 request_module("tuner");
2416         if (cx88_boards[dev->board].needs_tda9887)
2417                 request_module("tda9887");
2418         if (dev->tuner_type != UNSET)
2419                 cx8800_call_i2c_clients(dev,TUNER_SET_TYPE,&dev->tuner_type);
2420
2421         /* register v4l devices */
2422         OOPS("register video");
2423         dev->video_dev = vdev_init(dev,&cx8800_video_template,"video");
2424         err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
2425                                     video_nr[cx8800_devcount]);
2426         if (err < 0) {
2427                 printk(KERN_INFO "%s: can't register video device\n",
2428                        dev->name);
2429                 goto fail3;
2430         }
2431         printk(KERN_INFO "%s: registered device video%d [v4l2]\n",
2432                dev->name,dev->video_dev->minor & 0x1f);
2433
2434         OOPS("register vbi");
2435         dev->vbi_dev = vdev_init(dev,&cx8800_vbi_template,"vbi");
2436         err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
2437                                     vbi_nr[cx8800_devcount]);
2438         if (err < 0) {
2439                 printk(KERN_INFO "%s: can't register vbi device\n",
2440                        dev->name);
2441                 goto fail3;
2442         }
2443         printk(KERN_INFO "%s: registered device vbi%d\n",
2444                dev->name,dev->vbi_dev->minor & 0x1f);
2445
2446         if (dev->has_radio) {
2447                 OOPS("register radio");
2448                 dev->radio_dev = vdev_init(dev,&cx8800_radio_template,"radio");
2449                 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
2450                                             radio_nr[cx8800_devcount]);
2451                 if (err < 0) {
2452                         printk(KERN_INFO "%s: can't register radio device\n",
2453                                dev->name);
2454                         goto fail3;
2455                 }
2456                 printk(KERN_INFO "%s: registered device radio%d\n",
2457                        dev->name,dev->radio_dev->minor & 0x1f);
2458         }
2459
2460         /* everything worked */
2461         OOPS("finalize");
2462         list_add_tail(&dev->devlist,&cx8800_devlist);
2463         pci_set_drvdata(pci_dev,dev);
2464         cx8800_devcount++;
2465
2466         /* initial device configuration */
2467         OOPS("init device");
2468         down(&dev->lock);
2469         init_controls(dev);
2470         set_tvnorm(dev,tvnorms);
2471         video_mux(dev,0);
2472         up(&dev->lock);
2473         return 0;
2474
2475  fail3:
2476         OOPS("fail3");
2477         cx8800_unregister_video(dev);
2478         if (0 == dev->i2c_rc)
2479                 i2c_bit_del_bus(&dev->i2c_adap);
2480         free_irq(pci_dev->irq, dev);
2481  fail2:
2482         OOPS("fail2");
2483         release_mem_region(pci_resource_start(pci_dev,0),
2484                            pci_resource_len(pci_dev,0));
2485  fail1:
2486         OOPS("fail1");
2487         kfree(dev);
2488         return err;
2489 }
2490
2491 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
2492 {
2493         struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2494
2495         cx8800_shutdown(dev);
2496         pci_disable_device(pci_dev);
2497
2498         /* unregister stuff */  
2499         if (0 == dev->i2c_rc)
2500                 i2c_bit_del_bus(&dev->i2c_adap);
2501
2502         free_irq(pci_dev->irq, dev);
2503         release_mem_region(pci_resource_start(pci_dev,0),
2504                            pci_resource_len(pci_dev,0));
2505
2506         cx8800_unregister_video(dev);
2507         pci_set_drvdata(pci_dev, NULL);
2508
2509         /* free memory */
2510         btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
2511         list_del(&dev->devlist);
2512         cx8800_devcount--;
2513         kfree(dev);
2514 }
2515
2516 static int cx8800_suspend(struct pci_dev *pci_dev, u32 state)
2517 {
2518         struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2519
2520         printk("%s: suspend %d\n", dev->name, state);
2521
2522         cx8800_shutdown(dev);
2523         del_timer(&dev->vidq.timeout);
2524         
2525         pci_save_state(pci_dev, dev->state.pci_cfg);
2526         if (0 != pci_set_power_state(pci_dev, state)) {
2527                 pci_disable_device(pci_dev);
2528                 dev->state.disabled = 1;
2529         }
2530         return 0;
2531 }
2532
2533 static int cx8800_resume(struct pci_dev *pci_dev)
2534 {
2535         struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
2536
2537         printk("%s: resume\n", dev->name);
2538
2539         if (dev->state.disabled) {
2540                 pci_enable_device(pci_dev);
2541                 dev->state.disabled = 0;
2542         }
2543         pci_set_power_state(pci_dev, 0);
2544         pci_restore_state(pci_dev, dev->state.pci_cfg);
2545
2546         /* re-initialize hardware */
2547         cx8800_reset(dev);
2548
2549         /* restart video capture */
2550         spin_lock(&dev->slock);
2551         restart_video_queue(dev,&dev->vidq);
2552         spin_unlock(&dev->slock);
2553
2554         return 0;
2555 }
2556
2557 /* ----------------------------------------------------------- */
2558
2559 struct pci_device_id cx8800_pci_tbl[] = {
2560         {
2561                 .vendor       = 0x14f1,
2562                 .device       = 0x8800,
2563                 .subvendor    = PCI_ANY_ID,
2564                 .subdevice    = PCI_ANY_ID,
2565         },{
2566                 /* --- end of list --- */
2567         }
2568 };
2569 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2570
2571 static struct pci_driver cx8800_pci_driver = {
2572         .name     = "cx8800",
2573         .id_table = cx8800_pci_tbl,
2574         .probe    = cx8800_initdev,
2575         .remove   = cx8800_finidev,
2576
2577         .suspend  = cx8800_suspend,
2578         .resume   = cx8800_resume,
2579 };
2580
2581 static int cx8800_init(void)
2582 {
2583         INIT_LIST_HEAD(&cx8800_devlist);
2584         printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
2585                (CX88_VERSION_CODE >> 16) & 0xff,
2586                (CX88_VERSION_CODE >>  8) & 0xff,
2587                CX88_VERSION_CODE & 0xff);
2588 #ifdef SNAPSHOT
2589         printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2590                SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2591 #endif
2592         return pci_module_init(&cx8800_pci_driver);
2593 }
2594
2595 static void cx8800_fini(void)
2596 {
2597         pci_unregister_driver(&cx8800_pci_driver);
2598 }
2599
2600 module_init(cx8800_init);
2601 module_exit(cx8800_fini);
2602
2603 /* ----------------------------------------------------------- */
2604 /*
2605  * Local variables:
2606  * c-basic-offset: 8
2607  * End:
2608  */