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