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