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