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