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