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