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