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