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