]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/cx88/cx88-video.c
V4L/DVB (5146): Make VIDIOC_INT_[SG]_REGISTER ioctls no longer internal only
[linux-2.6-omap-h63xx.git] / 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         if (0 != t->index)
1305                 return -EINVAL;
1306
1307         strcpy(t->name, "Television");
1308         t->type       = V4L2_TUNER_ANALOG_TV;
1309         t->capability = V4L2_TUNER_CAP_NORM;
1310         t->rangehigh  = 0xffffffffUL;
1311
1312         cx88_get_stereo(core ,t);
1313         reg = cx_read(MO_DEVICE_STATUS);
1314         t->signal = (reg & (1<<5)) ? 0xffff : 0x0000;
1315         return 0;
1316 }
1317
1318 static int vidioc_s_tuner (struct file *file, void *priv,
1319                                 struct v4l2_tuner *t)
1320 {
1321         struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1322
1323         if (UNSET == core->tuner_type)
1324                 return -EINVAL;
1325         if (0 != t->index)
1326                 return -EINVAL;
1327
1328         cx88_set_stereo(core, t->audmode, 1);
1329         return 0;
1330 }
1331
1332 static int vidioc_g_frequency (struct file *file, void *priv,
1333                                 struct v4l2_frequency *f)
1334 {
1335         struct cx8800_fh  *fh   = priv;
1336         struct cx88_core  *core = fh->dev->core;
1337
1338         if (unlikely(UNSET == core->tuner_type))
1339                 return -EINVAL;
1340
1341         /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1342         f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1343         f->frequency = core->freq;
1344
1345         cx88_call_i2c_clients(core,VIDIOC_G_FREQUENCY,f);
1346
1347         return 0;
1348 }
1349
1350 int cx88_set_freq (struct cx88_core  *core,
1351                                 struct v4l2_frequency *f)
1352 {
1353         if (unlikely(UNSET == core->tuner_type))
1354                 return -EINVAL;
1355         if (unlikely(f->tuner != 0))
1356                 return -EINVAL;
1357
1358         mutex_lock(&core->lock);
1359         core->freq = f->frequency;
1360         cx88_newstation(core);
1361         cx88_call_i2c_clients(core,VIDIOC_S_FREQUENCY,f);
1362
1363         /* When changing channels it is required to reset TVAUDIO */
1364         msleep (10);
1365         cx88_set_tvaudio(core);
1366
1367         mutex_unlock(&core->lock);
1368
1369         return 0;
1370 }
1371 EXPORT_SYMBOL(cx88_set_freq);
1372
1373 static int vidioc_s_frequency (struct file *file, void *priv,
1374                                 struct v4l2_frequency *f)
1375 {
1376         struct cx8800_fh  *fh   = priv;
1377         struct cx88_core  *core = fh->dev->core;
1378
1379         if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1380                 return -EINVAL;
1381         if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1382                 return -EINVAL;
1383
1384         return
1385                 cx88_set_freq (core,f);
1386 }
1387
1388 #ifdef CONFIG_VIDEO_ADV_DEBUG
1389 static int vidioc_g_register (struct file *file, void *fh,
1390                                 struct v4l2_register *reg)
1391 {
1392         struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1393
1394         if (reg->i2c_id != 0)
1395                 return -EINVAL;
1396         /* cx2388x has a 24-bit register space */
1397         reg->val = cx_read(reg->reg&0xffffff);
1398         return 0;
1399 }
1400
1401 static int vidioc_s_register (struct file *file, void *fh,
1402                                 struct v4l2_register *reg)
1403 {
1404         struct cx88_core *core = ((struct cx8800_fh*)fh)->dev->core;
1405
1406         if (reg->i2c_id != 0)
1407                 return -EINVAL;
1408         cx_write(reg->reg&0xffffff, reg->val);
1409         return 0;
1410 }
1411 #endif
1412
1413 /* ----------------------------------------------------------- */
1414 /* RADIO ESPECIFIC IOCTLS                                      */
1415 /* ----------------------------------------------------------- */
1416
1417 static int radio_querycap (struct file *file, void  *priv,
1418                                         struct v4l2_capability *cap)
1419 {
1420         struct cx8800_dev *dev  = ((struct cx8800_fh *)priv)->dev;
1421         struct cx88_core  *core = dev->core;
1422
1423         strcpy(cap->driver, "cx8800");
1424         strlcpy(cap->card, cx88_boards[core->board].name,
1425                 sizeof(cap->card));
1426         sprintf(cap->bus_info,"PCI:%s", pci_name(dev->pci));
1427         cap->version = CX88_VERSION_CODE;
1428         cap->capabilities = V4L2_CAP_TUNER;
1429         return 0;
1430 }
1431
1432 static int radio_g_tuner (struct file *file, void *priv,
1433                                 struct v4l2_tuner *t)
1434 {
1435         struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1436
1437         if (unlikely(t->index > 0))
1438                 return -EINVAL;
1439
1440         strcpy(t->name, "Radio");
1441         t->type = V4L2_TUNER_RADIO;
1442
1443         cx88_call_i2c_clients(core,VIDIOC_G_TUNER,t);
1444         return 0;
1445 }
1446
1447 static int radio_enum_input (struct file *file, void *priv,
1448                                 struct v4l2_input *i)
1449 {
1450         if (i->index != 0)
1451                 return -EINVAL;
1452         strcpy(i->name,"Radio");
1453         i->type = V4L2_INPUT_TYPE_TUNER;
1454
1455         return 0;
1456 }
1457
1458 static int radio_g_audio (struct file *file, void *priv, struct v4l2_audio *a)
1459 {
1460         if (unlikely(a->index))
1461                 return -EINVAL;
1462
1463         memset(a,0,sizeof(*a));
1464         strcpy(a->name,"Radio");
1465         return 0;
1466 }
1467
1468 /* FIXME: Should add a standard for radio */
1469
1470 static int radio_s_tuner (struct file *file, void *priv,
1471                                 struct v4l2_tuner *t)
1472 {
1473         struct cx88_core  *core = ((struct cx8800_fh *)priv)->dev->core;
1474
1475         if (0 != t->index)
1476                 return -EINVAL;
1477
1478         cx88_call_i2c_clients(core,VIDIOC_S_TUNER,t);
1479
1480         return 0;
1481 }
1482
1483 static int radio_s_audio (struct file *file, void *fh,
1484                           struct v4l2_audio *a)
1485 {
1486         return 0;
1487 }
1488
1489 static int radio_s_input (struct file *file, void *fh, unsigned int i)
1490 {
1491         return 0;
1492 }
1493
1494 static int radio_queryctrl (struct file *file, void *priv,
1495                             struct v4l2_queryctrl *c)
1496 {
1497         int i;
1498
1499         if (c->id <  V4L2_CID_BASE ||
1500                 c->id >= V4L2_CID_LASTP1)
1501                 return -EINVAL;
1502         if (c->id == V4L2_CID_AUDIO_MUTE) {
1503                 for (i = 0; i < CX8800_CTLS; i++)
1504                         if (cx8800_ctls[i].v.id == c->id)
1505                                 break;
1506                 *c = cx8800_ctls[i].v;
1507         } else
1508                 *c = no_ctl;
1509         return 0;
1510 }
1511
1512 /* ----------------------------------------------------------- */
1513
1514 static void cx8800_vid_timeout(unsigned long data)
1515 {
1516         struct cx8800_dev *dev = (struct cx8800_dev*)data;
1517         struct cx88_core *core = dev->core;
1518         struct cx88_dmaqueue *q = &dev->vidq;
1519         struct cx88_buffer *buf;
1520         unsigned long flags;
1521
1522         cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1523
1524         cx_clear(MO_VID_DMACNTRL, 0x11);
1525         cx_clear(VID_CAPTURE_CONTROL, 0x06);
1526
1527         spin_lock_irqsave(&dev->slock,flags);
1528         while (!list_empty(&q->active)) {
1529                 buf = list_entry(q->active.next, struct cx88_buffer, vb.queue);
1530                 list_del(&buf->vb.queue);
1531                 buf->vb.state = STATE_ERROR;
1532                 wake_up(&buf->vb.done);
1533                 printk("%s/0: [%p/%d] timeout - dma=0x%08lx\n", core->name,
1534                        buf, buf->vb.i, (unsigned long)buf->risc.dma);
1535         }
1536         restart_video_queue(dev,q);
1537         spin_unlock_irqrestore(&dev->slock,flags);
1538 }
1539
1540 static char *cx88_vid_irqs[32] = {
1541         "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
1542         "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
1543         "y_oflow",  "u_oflow",  "v_oflow",  "vbi_oflow",
1544         "y_sync",   "u_sync",   "v_sync",   "vbi_sync",
1545         "opc_err",  "par_err",  "rip_err",  "pci_abort",
1546 };
1547
1548 static void cx8800_vid_irq(struct cx8800_dev *dev)
1549 {
1550         struct cx88_core *core = dev->core;
1551         u32 status, mask, count;
1552
1553         status = cx_read(MO_VID_INTSTAT);
1554         mask   = cx_read(MO_VID_INTMSK);
1555         if (0 == (status & mask))
1556                 return;
1557         cx_write(MO_VID_INTSTAT, status);
1558         if (irq_debug  ||  (status & mask & ~0xff))
1559                 cx88_print_irqbits(core->name, "irq vid",
1560                                    cx88_vid_irqs, status, mask);
1561
1562         /* risc op code error */
1563         if (status & (1 << 16)) {
1564                 printk(KERN_WARNING "%s/0: video risc op code error\n",core->name);
1565                 cx_clear(MO_VID_DMACNTRL, 0x11);
1566                 cx_clear(VID_CAPTURE_CONTROL, 0x06);
1567                 cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH21]);
1568         }
1569
1570         /* risc1 y */
1571         if (status & 0x01) {
1572                 spin_lock(&dev->slock);
1573                 count = cx_read(MO_VIDY_GPCNT);
1574                 cx88_wakeup(core, &dev->vidq, count);
1575                 spin_unlock(&dev->slock);
1576         }
1577
1578         /* risc1 vbi */
1579         if (status & 0x08) {
1580                 spin_lock(&dev->slock);
1581                 count = cx_read(MO_VBI_GPCNT);
1582                 cx88_wakeup(core, &dev->vbiq, count);
1583                 spin_unlock(&dev->slock);
1584         }
1585
1586         /* risc2 y */
1587         if (status & 0x10) {
1588                 dprintk(2,"stopper video\n");
1589                 spin_lock(&dev->slock);
1590                 restart_video_queue(dev,&dev->vidq);
1591                 spin_unlock(&dev->slock);
1592         }
1593
1594         /* risc2 vbi */
1595         if (status & 0x80) {
1596                 dprintk(2,"stopper vbi\n");
1597                 spin_lock(&dev->slock);
1598                 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1599                 spin_unlock(&dev->slock);
1600         }
1601 }
1602
1603 static irqreturn_t cx8800_irq(int irq, void *dev_id)
1604 {
1605         struct cx8800_dev *dev = dev_id;
1606         struct cx88_core *core = dev->core;
1607         u32 status;
1608         int loop, handled = 0;
1609
1610         for (loop = 0; loop < 10; loop++) {
1611                 status = cx_read(MO_PCI_INTSTAT) & (core->pci_irqmask | 0x01);
1612                 if (0 == status)
1613                         goto out;
1614                 cx_write(MO_PCI_INTSTAT, status);
1615                 handled = 1;
1616
1617                 if (status & core->pci_irqmask)
1618                         cx88_core_irq(core,status);
1619                 if (status & 0x01)
1620                         cx8800_vid_irq(dev);
1621         };
1622         if (10 == loop) {
1623                 printk(KERN_WARNING "%s/0: irq loop -- clearing mask\n",
1624                        core->name);
1625                 cx_write(MO_PCI_INTMSK,0);
1626         }
1627
1628  out:
1629         return IRQ_RETVAL(handled);
1630 }
1631
1632 /* ----------------------------------------------------------- */
1633 /* exported stuff                                              */
1634
1635 static const struct file_operations video_fops =
1636 {
1637         .owner         = THIS_MODULE,
1638         .open          = video_open,
1639         .release       = video_release,
1640         .read          = video_read,
1641         .poll          = video_poll,
1642         .mmap          = video_mmap,
1643         .ioctl         = video_ioctl2,
1644         .compat_ioctl  = v4l_compat_ioctl32,
1645         .llseek        = no_llseek,
1646 };
1647
1648 static struct video_device cx8800_vbi_template;
1649 static struct video_device cx8800_video_template =
1650 {
1651         .name                 = "cx8800-video",
1652         .type                 = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1653         .fops                 = &video_fops,
1654         .minor                = -1,
1655         .vidioc_querycap      = vidioc_querycap,
1656         .vidioc_enum_fmt_cap  = vidioc_enum_fmt_cap,
1657         .vidioc_g_fmt_cap     = vidioc_g_fmt_cap,
1658         .vidioc_try_fmt_cap   = vidioc_try_fmt_cap,
1659         .vidioc_s_fmt_cap     = vidioc_s_fmt_cap,
1660         .vidioc_g_fmt_vbi     = cx8800_vbi_fmt,
1661         .vidioc_try_fmt_vbi   = cx8800_vbi_fmt,
1662         .vidioc_s_fmt_vbi     = cx8800_vbi_fmt,
1663         .vidioc_reqbufs       = vidioc_reqbufs,
1664         .vidioc_querybuf      = vidioc_querybuf,
1665         .vidioc_qbuf          = vidioc_qbuf,
1666         .vidioc_dqbuf         = vidioc_dqbuf,
1667         .vidioc_s_std         = vidioc_s_std,
1668         .vidioc_enum_input    = vidioc_enum_input,
1669         .vidioc_g_input       = vidioc_g_input,
1670         .vidioc_s_input       = vidioc_s_input,
1671         .vidioc_queryctrl     = vidioc_queryctrl,
1672         .vidioc_g_ctrl        = vidioc_g_ctrl,
1673         .vidioc_s_ctrl        = vidioc_s_ctrl,
1674         .vidioc_streamon      = vidioc_streamon,
1675         .vidioc_streamoff     = vidioc_streamoff,
1676 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1677         .vidiocgmbuf          = vidiocgmbuf,
1678 #endif
1679         .vidioc_g_tuner       = vidioc_g_tuner,
1680         .vidioc_s_tuner       = vidioc_s_tuner,
1681         .vidioc_g_frequency   = vidioc_g_frequency,
1682         .vidioc_s_frequency   = vidioc_s_frequency,
1683 #ifdef CONFIG_VIDEO_ADV_DEBUG
1684         .vidioc_g_register    = vidioc_g_register,
1685         .vidioc_s_register    = vidioc_s_register,
1686 #endif
1687         .tvnorms              = CX88_NORMS,
1688         .current_norm         = V4L2_STD_NTSC_M,
1689 };
1690
1691 static const struct file_operations radio_fops =
1692 {
1693         .owner         = THIS_MODULE,
1694         .open          = video_open,
1695         .release       = video_release,
1696         .ioctl         = video_ioctl2,
1697         .compat_ioctl  = v4l_compat_ioctl32,
1698         .llseek        = no_llseek,
1699 };
1700
1701 static struct video_device cx8800_radio_template =
1702 {
1703         .name                 = "cx8800-radio",
1704         .type                 = VID_TYPE_TUNER,
1705         .hardware             = 0,
1706         .fops                 = &radio_fops,
1707         .minor                = -1,
1708         .vidioc_querycap      = radio_querycap,
1709         .vidioc_g_tuner       = radio_g_tuner,
1710         .vidioc_enum_input    = radio_enum_input,
1711         .vidioc_g_audio       = radio_g_audio,
1712         .vidioc_s_tuner       = radio_s_tuner,
1713         .vidioc_s_audio       = radio_s_audio,
1714         .vidioc_s_input       = radio_s_input,
1715         .vidioc_queryctrl     = radio_queryctrl,
1716         .vidioc_g_ctrl        = vidioc_g_ctrl,
1717         .vidioc_s_ctrl        = vidioc_s_ctrl,
1718         .vidioc_g_frequency   = vidioc_g_frequency,
1719         .vidioc_s_frequency   = vidioc_s_frequency,
1720 };
1721
1722 /* ----------------------------------------------------------- */
1723
1724 static void cx8800_unregister_video(struct cx8800_dev *dev)
1725 {
1726         if (dev->radio_dev) {
1727                 if (-1 != dev->radio_dev->minor)
1728                         video_unregister_device(dev->radio_dev);
1729                 else
1730                         video_device_release(dev->radio_dev);
1731                 dev->radio_dev = NULL;
1732         }
1733         if (dev->vbi_dev) {
1734                 if (-1 != dev->vbi_dev->minor)
1735                         video_unregister_device(dev->vbi_dev);
1736                 else
1737                         video_device_release(dev->vbi_dev);
1738                 dev->vbi_dev = NULL;
1739         }
1740         if (dev->video_dev) {
1741                 if (-1 != dev->video_dev->minor)
1742                         video_unregister_device(dev->video_dev);
1743                 else
1744                         video_device_release(dev->video_dev);
1745                 dev->video_dev = NULL;
1746         }
1747 }
1748
1749 static int __devinit cx8800_initdev(struct pci_dev *pci_dev,
1750                                     const struct pci_device_id *pci_id)
1751 {
1752         struct cx8800_dev *dev;
1753         struct cx88_core *core;
1754
1755         int err;
1756
1757         dev = kzalloc(sizeof(*dev),GFP_KERNEL);
1758         if (NULL == dev)
1759                 return -ENOMEM;
1760
1761         /* pci init */
1762         dev->pci = pci_dev;
1763         if (pci_enable_device(pci_dev)) {
1764                 err = -EIO;
1765                 goto fail_free;
1766         }
1767         core = cx88_core_get(dev->pci);
1768         if (NULL == core) {
1769                 err = -EINVAL;
1770                 goto fail_free;
1771         }
1772         dev->core = core;
1773
1774         /* print pci info */
1775         pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &dev->pci_rev);
1776         pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER,  &dev->pci_lat);
1777         printk(KERN_INFO "%s/0: found at %s, rev: %d, irq: %d, "
1778                "latency: %d, mmio: 0x%llx\n", core->name,
1779                pci_name(pci_dev), dev->pci_rev, pci_dev->irq,
1780                dev->pci_lat,(unsigned long long)pci_resource_start(pci_dev,0));
1781
1782         pci_set_master(pci_dev);
1783         if (!pci_dma_supported(pci_dev,0xffffffff)) {
1784                 printk("%s/0: Oops: no 32bit PCI DMA ???\n",core->name);
1785                 err = -EIO;
1786                 goto fail_core;
1787         }
1788
1789         /* Initialize VBI template */
1790         memcpy( &cx8800_vbi_template, &cx8800_video_template,
1791                 sizeof(cx8800_vbi_template) );
1792         strcpy(cx8800_vbi_template.name,"cx8800-vbi");
1793         cx8800_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
1794
1795         /* initialize driver struct */
1796         spin_lock_init(&dev->slock);
1797         core->tvnorm = cx8800_video_template.current_norm;
1798
1799         /* init video dma queues */
1800         INIT_LIST_HEAD(&dev->vidq.active);
1801         INIT_LIST_HEAD(&dev->vidq.queued);
1802         dev->vidq.timeout.function = cx8800_vid_timeout;
1803         dev->vidq.timeout.data     = (unsigned long)dev;
1804         init_timer(&dev->vidq.timeout);
1805         cx88_risc_stopper(dev->pci,&dev->vidq.stopper,
1806                           MO_VID_DMACNTRL,0x11,0x00);
1807
1808         /* init vbi dma queues */
1809         INIT_LIST_HEAD(&dev->vbiq.active);
1810         INIT_LIST_HEAD(&dev->vbiq.queued);
1811         dev->vbiq.timeout.function = cx8800_vbi_timeout;
1812         dev->vbiq.timeout.data     = (unsigned long)dev;
1813         init_timer(&dev->vbiq.timeout);
1814         cx88_risc_stopper(dev->pci,&dev->vbiq.stopper,
1815                           MO_VID_DMACNTRL,0x88,0x00);
1816
1817         /* get irq */
1818         err = request_irq(pci_dev->irq, cx8800_irq,
1819                           IRQF_SHARED | IRQF_DISABLED, core->name, dev);
1820         if (err < 0) {
1821                 printk(KERN_ERR "%s: can't get IRQ %d\n",
1822                        core->name,pci_dev->irq);
1823                 goto fail_core;
1824         }
1825         cx_set(MO_PCI_INTMSK, core->pci_irqmask);
1826
1827         /* load and configure helper modules */
1828         if (TUNER_ABSENT != core->tuner_type)
1829                 request_module("tuner");
1830
1831         if (cx88_boards[ core->board ].audio_chip == AUDIO_CHIP_WM8775)
1832                 request_module("wm8775");
1833
1834         /* register v4l devices */
1835         dev->video_dev = cx88_vdev_init(core,dev->pci,
1836                                         &cx8800_video_template,"video");
1837         err = video_register_device(dev->video_dev,VFL_TYPE_GRABBER,
1838                                     video_nr[core->nr]);
1839         if (err < 0) {
1840                 printk(KERN_INFO "%s: can't register video device\n",
1841                        core->name);
1842                 goto fail_unreg;
1843         }
1844         printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1845                core->name,dev->video_dev->minor & 0x1f);
1846
1847         dev->vbi_dev = cx88_vdev_init(core,dev->pci,&cx8800_vbi_template,"vbi");
1848         err = video_register_device(dev->vbi_dev,VFL_TYPE_VBI,
1849                                     vbi_nr[core->nr]);
1850         if (err < 0) {
1851                 printk(KERN_INFO "%s/0: can't register vbi device\n",
1852                        core->name);
1853                 goto fail_unreg;
1854         }
1855         printk(KERN_INFO "%s/0: registered device vbi%d\n",
1856                core->name,dev->vbi_dev->minor & 0x1f);
1857
1858         if (core->has_radio) {
1859                 dev->radio_dev = cx88_vdev_init(core,dev->pci,
1860                                                 &cx8800_radio_template,"radio");
1861                 err = video_register_device(dev->radio_dev,VFL_TYPE_RADIO,
1862                                             radio_nr[core->nr]);
1863                 if (err < 0) {
1864                         printk(KERN_INFO "%s/0: can't register radio device\n",
1865                                core->name);
1866                         goto fail_unreg;
1867                 }
1868                 printk(KERN_INFO "%s/0: registered device radio%d\n",
1869                        core->name,dev->radio_dev->minor & 0x1f);
1870         }
1871
1872         /* everything worked */
1873         list_add_tail(&dev->devlist,&cx8800_devlist);
1874         pci_set_drvdata(pci_dev,dev);
1875
1876         /* initial device configuration */
1877         mutex_lock(&core->lock);
1878         cx88_set_tvnorm(core,core->tvnorm);
1879         init_controls(core);
1880         cx88_video_mux(core,0);
1881         mutex_unlock(&core->lock);
1882
1883         /* start tvaudio thread */
1884         if (core->tuner_type != TUNER_ABSENT)
1885                 core->kthread = kthread_run(cx88_audio_thread, core, "cx88 tvaudio");
1886         return 0;
1887
1888 fail_unreg:
1889         cx8800_unregister_video(dev);
1890         free_irq(pci_dev->irq, dev);
1891 fail_core:
1892         cx88_core_put(core,dev->pci);
1893 fail_free:
1894         kfree(dev);
1895         return err;
1896 }
1897
1898 static void __devexit cx8800_finidev(struct pci_dev *pci_dev)
1899 {
1900         struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1901         struct cx88_core *core = dev->core;
1902
1903         /* stop thread */
1904         if (core->kthread) {
1905                 kthread_stop(core->kthread);
1906                 core->kthread = NULL;
1907         }
1908
1909         cx88_shutdown(core); /* FIXME */
1910         pci_disable_device(pci_dev);
1911
1912         /* unregister stuff */
1913
1914         free_irq(pci_dev->irq, dev);
1915         cx8800_unregister_video(dev);
1916         pci_set_drvdata(pci_dev, NULL);
1917
1918         /* free memory */
1919         btcx_riscmem_free(dev->pci,&dev->vidq.stopper);
1920         list_del(&dev->devlist);
1921         cx88_core_put(core,dev->pci);
1922         kfree(dev);
1923 }
1924
1925 #ifdef CONFIG_PM
1926 static int cx8800_suspend(struct pci_dev *pci_dev, pm_message_t state)
1927 {
1928         struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1929         struct cx88_core *core = dev->core;
1930
1931         /* stop video+vbi capture */
1932         spin_lock(&dev->slock);
1933         if (!list_empty(&dev->vidq.active)) {
1934                 printk("%s: suspend video\n", core->name);
1935                 stop_video_dma(dev);
1936                 del_timer(&dev->vidq.timeout);
1937         }
1938         if (!list_empty(&dev->vbiq.active)) {
1939                 printk("%s: suspend vbi\n", core->name);
1940                 cx8800_stop_vbi_dma(dev);
1941                 del_timer(&dev->vbiq.timeout);
1942         }
1943         spin_unlock(&dev->slock);
1944
1945         /* FIXME -- shutdown device */
1946         cx88_shutdown(core);
1947
1948         pci_save_state(pci_dev);
1949         if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
1950                 pci_disable_device(pci_dev);
1951                 dev->state.disabled = 1;
1952         }
1953         return 0;
1954 }
1955
1956 static int cx8800_resume(struct pci_dev *pci_dev)
1957 {
1958         struct cx8800_dev *dev = pci_get_drvdata(pci_dev);
1959         struct cx88_core *core = dev->core;
1960         int err;
1961
1962         if (dev->state.disabled) {
1963                 err=pci_enable_device(pci_dev);
1964                 if (err) {
1965                         printk(KERN_ERR "%s: can't enable device\n",
1966                                                        core->name);
1967                         return err;
1968                 }
1969
1970                 dev->state.disabled = 0;
1971         }
1972         err= pci_set_power_state(pci_dev, PCI_D0);
1973         if (err) {
1974                 printk(KERN_ERR "%s: can't enable device\n",
1975                                        core->name);
1976
1977                 pci_disable_device(pci_dev);
1978                 dev->state.disabled = 1;
1979
1980                 return err;
1981         }
1982         pci_restore_state(pci_dev);
1983
1984         /* FIXME: re-initialize hardware */
1985         cx88_reset(core);
1986
1987         /* restart video+vbi capture */
1988         spin_lock(&dev->slock);
1989         if (!list_empty(&dev->vidq.active)) {
1990                 printk("%s: resume video\n", core->name);
1991                 restart_video_queue(dev,&dev->vidq);
1992         }
1993         if (!list_empty(&dev->vbiq.active)) {
1994                 printk("%s: resume vbi\n", core->name);
1995                 cx8800_restart_vbi_queue(dev,&dev->vbiq);
1996         }
1997         spin_unlock(&dev->slock);
1998
1999         return 0;
2000 }
2001 #endif
2002
2003 /* ----------------------------------------------------------- */
2004
2005 static struct pci_device_id cx8800_pci_tbl[] = {
2006         {
2007                 .vendor       = 0x14f1,
2008                 .device       = 0x8800,
2009                 .subvendor    = PCI_ANY_ID,
2010                 .subdevice    = PCI_ANY_ID,
2011         },{
2012                 /* --- end of list --- */
2013         }
2014 };
2015 MODULE_DEVICE_TABLE(pci, cx8800_pci_tbl);
2016
2017 static struct pci_driver cx8800_pci_driver = {
2018         .name     = "cx8800",
2019         .id_table = cx8800_pci_tbl,
2020         .probe    = cx8800_initdev,
2021         .remove   = __devexit_p(cx8800_finidev),
2022 #ifdef CONFIG_PM
2023         .suspend  = cx8800_suspend,
2024         .resume   = cx8800_resume,
2025 #endif
2026 };
2027
2028 static int cx8800_init(void)
2029 {
2030         printk(KERN_INFO "cx2388x v4l2 driver version %d.%d.%d loaded\n",
2031                (CX88_VERSION_CODE >> 16) & 0xff,
2032                (CX88_VERSION_CODE >>  8) & 0xff,
2033                CX88_VERSION_CODE & 0xff);
2034 #ifdef SNAPSHOT
2035         printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
2036                SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
2037 #endif
2038         return pci_register_driver(&cx8800_pci_driver);
2039 }
2040
2041 static void cx8800_fini(void)
2042 {
2043         pci_unregister_driver(&cx8800_pci_driver);
2044 }
2045
2046 module_init(cx8800_init);
2047 module_exit(cx8800_fini);
2048
2049 /* ----------------------------------------------------------- */
2050 /*
2051  * Local variables:
2052  * c-basic-offset: 8
2053  * End:
2054  * 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
2055  */