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