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