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