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