blob: 8eaebdb76b1e8f08d65e4265a8133610e52deaaa [file] [log] [blame]
Steven Tothe47f30b2008-01-10 04:25:59 -03001/*
2 * Driver for the Conexant CX23885 PCIe bridge
3 *
4 * Copyright (c) 2007 Steven Toth <stoth@hauppauge.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include <linux/init.h>
23#include <linux/list.h>
24#include <linux/module.h>
25#include <linux/moduleparam.h>
26#include <linux/kmod.h>
27#include <linux/kernel.h>
28#include <linux/slab.h>
29#include <linux/interrupt.h>
30#include <linux/delay.h>
31#include <linux/kthread.h>
32#include <asm/div64.h>
33
34#include "cx23885.h"
35#include <media/v4l2-common.h>
36
37#ifdef CONFIG_VIDEO_V4L1_COMPAT
38/* Include V4L1 specific functions. Should be removed soon */
39#include <linux/videodev.h>
40#endif
41
42MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
43MODULE_AUTHOR("Steven Toth <stoth@hauppauge.com>");
44MODULE_LICENSE("GPL");
45
46/* ------------------------------------------------------------------ */
47
48static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
49static unsigned int vbi_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
50static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
51
52module_param_array(video_nr, int, NULL, 0444);
53module_param_array(vbi_nr, int, NULL, 0444);
54module_param_array(radio_nr, int, NULL, 0444);
55
56MODULE_PARM_DESC(video_nr, "video device numbers");
57MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
58MODULE_PARM_DESC(radio_nr, "radio device numbers");
59
60static unsigned int video_debug = 0;
61module_param(video_debug, int, 0644);
62MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
63
64static unsigned int irq_debug = 0;
65module_param(irq_debug, int, 0644);
66MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
67
68static unsigned int vid_limit = 16;
69module_param(vid_limit, int, 0644);
70MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
71
72#define dprintk(level, fmt, arg...)\
73 if (video_debug >= level)\
74 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg)
75
76/* ------------------------------------------------------------------- */
77/* static data */
78
79#define FORMAT_FLAGS_PACKED 0x01
80
81static struct cx23885_fmt formats[] = {
82 {
83 .name = "8 bpp, gray",
84 .fourcc = V4L2_PIX_FMT_GREY,
85 .depth = 8,
86 .flags = FORMAT_FLAGS_PACKED,
87 }, {
88 .name = "15 bpp RGB, le",
89 .fourcc = V4L2_PIX_FMT_RGB555,
90 .depth = 16,
91 .flags = FORMAT_FLAGS_PACKED,
92 }, {
93 .name = "15 bpp RGB, be",
94 .fourcc = V4L2_PIX_FMT_RGB555X,
95 .depth = 16,
96 .flags = FORMAT_FLAGS_PACKED,
97 }, {
98 .name = "16 bpp RGB, le",
99 .fourcc = V4L2_PIX_FMT_RGB565,
100 .depth = 16,
101 .flags = FORMAT_FLAGS_PACKED,
102 }, {
103 .name = "16 bpp RGB, be",
104 .fourcc = V4L2_PIX_FMT_RGB565X,
105 .depth = 16,
106 .flags = FORMAT_FLAGS_PACKED,
107 }, {
108 .name = "24 bpp RGB, le",
109 .fourcc = V4L2_PIX_FMT_BGR24,
110 .depth = 24,
111 .flags = FORMAT_FLAGS_PACKED,
112 }, {
113 .name = "32 bpp RGB, le",
114 .fourcc = V4L2_PIX_FMT_BGR32,
115 .depth = 32,
116 .flags = FORMAT_FLAGS_PACKED,
117 }, {
118 .name = "32 bpp RGB, be",
119 .fourcc = V4L2_PIX_FMT_RGB32,
120 .depth = 32,
121 .flags = FORMAT_FLAGS_PACKED,
122 }, {
123 .name = "4:2:2, packed, YUYV",
124 .fourcc = V4L2_PIX_FMT_YUYV,
125 .depth = 16,
126 .flags = FORMAT_FLAGS_PACKED,
127 }, {
128 .name = "4:2:2, packed, UYVY",
129 .fourcc = V4L2_PIX_FMT_UYVY,
130 .depth = 16,
131 .flags = FORMAT_FLAGS_PACKED,
132 },
133};
134
135static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
136{
137 unsigned int i;
138
139 for (i = 0; i < ARRAY_SIZE(formats); i++)
140 if (formats[i].fourcc == fourcc)
141 return formats+i;
142
143 printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __FUNCTION__, fourcc);
144 return NULL;
145}
146
147/* ------------------------------------------------------------------- */
148
149static const struct v4l2_queryctrl no_ctl = {
150 .name = "42",
151 .flags = V4L2_CTRL_FLAG_DISABLED,
152};
153
154static struct cx23885_ctrl cx23885_ctls[] = {
155 /* --- video --- */
156 {
157 .v = {
158 .id = V4L2_CID_BRIGHTNESS,
159 .name = "Brightness",
160 .minimum = 0x00,
161 .maximum = 0xff,
162 .step = 1,
163 .default_value = 0x7f,
164 .type = V4L2_CTRL_TYPE_INTEGER,
165 },
166 .off = 128,
167 .reg = LUMA_CTRL,
168 .mask = 0x00ff,
169 .shift = 0,
170 }, {
171 .v = {
172 .id = V4L2_CID_CONTRAST,
173 .name = "Contrast",
174 .minimum = 0,
175 .maximum = 0xff,
176 .step = 1,
177 .default_value = 0x3f,
178 .type = V4L2_CTRL_TYPE_INTEGER,
179 },
180 .off = 0,
181 .reg = LUMA_CTRL,
182 .mask = 0xff00,
183 .shift = 8,
184 }, {
185 .v = {
186 .id = V4L2_CID_HUE,
187 .name = "Hue",
188 .minimum = 0,
189 .maximum = 0xff,
190 .step = 1,
191 .default_value = 0x7f,
192 .type = V4L2_CTRL_TYPE_INTEGER,
193 },
194 .off = 128,
195 .reg = CHROMA_CTRL,
196 .mask = 0xff0000,
197 .shift = 16,
198 }, {
199 /* strictly, this only describes only U saturation.
200 * V saturation is handled specially through code.
201 */
202 .v = {
203 .id = V4L2_CID_SATURATION,
204 .name = "Saturation",
205 .minimum = 0,
206 .maximum = 0xff,
207 .step = 1,
208 .default_value = 0x7f,
209 .type = V4L2_CTRL_TYPE_INTEGER,
210 },
211 .off = 0,
212 .reg = CHROMA_CTRL,
213 .mask = 0x00ff,
214 .shift = 0,
215 }, {
216 /* --- audio --- */
217 .v = {
218 .id = V4L2_CID_AUDIO_MUTE,
219 .name = "Mute",
220 .minimum = 0,
221 .maximum = 1,
222 .default_value = 1,
223 .type = V4L2_CTRL_TYPE_BOOLEAN,
224 },
225 .reg = PATH1_CTL1,
226 .mask = (0x1f << 24),
227 .shift = 24,
228 }, {
229 .v = {
230 .id = V4L2_CID_AUDIO_VOLUME,
231 .name = "Volume",
232 .minimum = 0,
233 .maximum = 0x3f,
234 .step = 1,
235 .default_value = 0x3f,
236 .type = V4L2_CTRL_TYPE_INTEGER,
237 },
238 .reg = PATH1_VOL_CTL,
239 .mask = 0xff,
240 .shift = 0,
241 }
242};
243static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
244
245const u32 cx23885_user_ctrls[] = {
246 V4L2_CID_USER_CLASS,
247 V4L2_CID_BRIGHTNESS,
248 V4L2_CID_CONTRAST,
249 V4L2_CID_SATURATION,
250 V4L2_CID_HUE,
251 V4L2_CID_AUDIO_VOLUME,
252 V4L2_CID_AUDIO_MUTE,
253 0
254};
255EXPORT_SYMBOL(cx23885_user_ctrls);
256
257static const u32 *ctrl_classes[] = {
258 cx23885_user_ctrls,
259 NULL
260};
261
262void cx23885_video_wakeup(struct cx23885_dev *dev,
263 struct cx23885_dmaqueue *q, u32 count)
264{
265 struct cx23885_buffer *buf;
266 int bc;
267
268 for (bc = 0;; bc++) {
269 if (list_empty(&q->active))
270 break;
271 buf = list_entry(q->active.next,
272 struct cx23885_buffer, vb.queue);
273 /* count comes from the hw and is is 16bit wide --
274 * this trick handles wrap-arounds correctly for
275 * up to 32767 buffers in flight... */
276 if ((s16) (count - buf->count) < 0)
277 break;
278 do_gettimeofday(&buf->vb.ts);
279 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
280 count, buf->count);
281 buf->vb.state = VIDEOBUF_DONE;
282 list_del(&buf->vb.queue);
283 wake_up(&buf->vb.done);
284 }
285 if (list_empty(&q->active)) {
286 del_timer(&q->timeout);
287 } else {
288 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
289 }
290 if (bc != 1)
291 printk(KERN_WARN "%s: %d buffers handled (should be 1)\n",
292 __FUNCTION__, bc);
293}
294
295int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
296{
297 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
298 __FUNCTION__,
299 (unsigned int)norm,
300 v4l2_norm_to_name(norm));
301
302 dev->tvnorm = norm;
303
304
305 /* Tell the analog tuner/demods */
306 cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_STD, &norm);
307
308 /* Tell the internal A/V decoder */
309 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_STD, &norm);
310
311 return 0;
312}
313
314struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
315 struct pci_dev *pci,
316 struct video_device *template,
317 char *type)
318{
319 struct video_device *vfd;
320 dprintk(1, "%s()\n", __FUNCTION__);
321
322 vfd = video_device_alloc();
323 if (NULL == vfd)
324 return NULL;
325 *vfd = *template;
326 vfd->minor = -1;
327 vfd->dev = &pci->dev;
328 vfd->release = video_device_release;
329 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
330 dev->name, type, cx23885_boards[dev->board].name);
331 return vfd;
332}
333
334int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
335{
336 int i;
337
338 if (qctrl->id < V4L2_CID_BASE ||
339 qctrl->id >= V4L2_CID_LASTP1)
340 return -EINVAL;
341 for (i = 0; i < CX23885_CTLS; i++)
342 if (cx23885_ctls[i].v.id == qctrl->id)
343 break;
344 if (i == CX23885_CTLS) {
345 *qctrl = no_ctl;
346 return 0;
347 }
348 *qctrl = cx23885_ctls[i].v;
349 return 0;
350}
351EXPORT_SYMBOL(cx23885_ctrl_query);
352
353/* ------------------------------------------------------------------- */
354/* resource management */
355
356static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
357 unsigned int bit)
358{
359 dprintk(1, "%s()\n", __FUNCTION__);
360 if (fh->resources & bit)
361 /* have it already allocated */
362 return 1;
363
364 /* is it free? */
365 mutex_lock(&dev->lock);
366 if (dev->resources & bit) {
367 /* no, someone else uses it */
368 mutex_unlock(&dev->lock);
369 return 0;
370 }
371 /* it's free, grab it */
372 fh->resources |= bit;
373 dev->resources |= bit;
374 dprintk(1, "res: get %d\n", bit);
375 mutex_unlock(&dev->lock);
376 return 1;
377}
378
379static int res_check(struct cx23885_fh *fh, unsigned int bit)
380{
381 return (fh->resources & bit);
382}
383
384static int res_locked(struct cx23885_dev *dev, unsigned int bit)
385{
386 return (dev->resources & bit);
387}
388
389static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
390 unsigned int bits)
391{
392 BUG_ON((fh->resources & bits) != bits);
393 dprintk(1, "%s()\n", __FUNCTION__);
394
395 mutex_lock(&dev->lock);
396 fh->resources &= ~bits;
397 dev->resources &= ~bits;
398 dprintk(1, "res: put %d\n", bits);
399 mutex_unlock(&dev->lock);
400}
401
402int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
403{
404 struct v4l2_routing route;
405 memset(&route, 0, sizeof(route));
406
407 dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
408 __FUNCTION__,
409 input, INPUT(input)->vmux,
410 INPUT(input)->gpio0, INPUT(input)->gpio1,
411 INPUT(input)->gpio2, INPUT(input)->gpio3);
412 dev->input = input;
413
414 route.input = INPUT(input)->vmux;
415
416 /* Tell the internal A/V decoder */
417 cx23885_call_i2c_clients(&dev->i2c_bus[2],
418 VIDIOC_INT_S_VIDEO_ROUTING, &route);
419
420 return 0;
421}
422EXPORT_SYMBOL(cx23885_video_mux);
423
424/* ------------------------------------------------------------------ */
425int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
426 unsigned int height, enum v4l2_field field)
427{
428 dprintk(1, "%s()\n", __FUNCTION__);
429 return 0;
430}
431
432static int cx23885_start_video_dma(struct cx23885_dev *dev,
433 struct cx23885_dmaqueue *q,
434 struct cx23885_buffer *buf)
435{
436 dprintk(1, "%s()\n", __FUNCTION__);
437
438 /* setup fifo + format */
439 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
440 buf->bpl, buf->risc.dma);
441 cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
442
443 /* reset counter */
444 cx_write(VID_A_GPCNT_CTL, 3);
445 q->count = 1;
446
447 /* enable irq */
448 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
449 cx_set(VID_A_INT_MSK, 0x000011);
450
451 /* start dma */
452 cx_set(DEV_CNTRL2, (1<<5));
453 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
454
455 return 0;
456}
457
458#ifdef CONFIG_PM
459static int cx23885_stop_video_dma(struct cx23885_dev *dev)
460{
461 dprintk(1, "%s()\n", __FUNCTION__);
462 /* stop dma */
463 cx_clear(VID_A_DMA_CTL, 0x11);
464
465 /* disable irqs */
466 cx_clear(PCI_INT_MSK, 0x000001);
467 cx_clear(VID_A_INT_MSK, 0x000011);
468
469 return 0;
470}
471#endif
472
473static int cx23885_restart_video_queue(struct cx23885_dev *dev,
474 struct cx23885_dmaqueue *q)
475{
476 struct cx23885_buffer *buf, *prev;
477 struct list_head *item;
478 dprintk(1, "%s()\n", __FUNCTION__);
479
480 if (!list_empty(&q->active)) {
481 buf = list_entry(q->active.next, struct cx23885_buffer,
482 vb.queue);
483 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
484 buf, buf->vb.i);
485 cx23885_start_video_dma(dev, q, buf);
486 list_for_each(item, &q->active) {
487 buf = list_entry(item, struct cx23885_buffer,
488 vb.queue);
489 buf->count = q->count++;
490 }
491 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
492 return 0;
493 }
494
495 prev = NULL;
496 for (;;) {
497 if (list_empty(&q->queued))
498 return 0;
499 buf = list_entry(q->queued.next, struct cx23885_buffer,
500 vb.queue);
501 if (NULL == prev) {
502 list_move_tail(&buf->vb.queue, &q->active);
503 cx23885_start_video_dma(dev, q, buf);
504 buf->vb.state = VIDEOBUF_ACTIVE;
505 buf->count = q->count++;
506 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
507 dprintk(2, "[%p/%d] restart_queue - first active\n",
508 buf, buf->vb.i);
509
510 } else if (prev->vb.width == buf->vb.width &&
511 prev->vb.height == buf->vb.height &&
512 prev->fmt == buf->fmt) {
513 list_move_tail(&buf->vb.queue, &q->active);
514 buf->vb.state = VIDEOBUF_ACTIVE;
515 buf->count = q->count++;
516 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
517 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
518 dprintk(2, "[%p/%d] restart_queue - move to active\n",
519 buf, buf->vb.i);
520 } else {
521 return 0;
522 }
523 prev = buf;
524 }
525}
526
527static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
528 unsigned int *size)
529{
530 struct cx23885_fh *fh = q->priv_data;
531
532 *size = fh->fmt->depth*fh->width*fh->height >> 3;
533 if (0 == *count)
534 *count = 32;
535 while (*size * *count > vid_limit * 1024 * 1024)
536 (*count)--;
537 return 0;
538}
539
540static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
541 enum v4l2_field field)
542{
543 struct cx23885_fh *fh = q->priv_data;
544 struct cx23885_dev *dev = fh->dev;
545 struct cx23885_buffer *buf =
546 container_of(vb, struct cx23885_buffer, vb);
547 int rc, init_buffer = 0;
548 u32 line0_offset, line1_offset;
549 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
550
551 BUG_ON(NULL == fh->fmt);
552 if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
553 fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
554 return -EINVAL;
555 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
556 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
557 return -EINVAL;
558
559 if (buf->fmt != fh->fmt ||
560 buf->vb.width != fh->width ||
561 buf->vb.height != fh->height ||
562 buf->vb.field != field) {
563 buf->fmt = fh->fmt;
564 buf->vb.width = fh->width;
565 buf->vb.height = fh->height;
566 buf->vb.field = field;
567 init_buffer = 1;
568 }
569
570 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
571 init_buffer = 1;
572 rc = videobuf_iolock(q, &buf->vb, NULL);
573 if (0 != rc)
574 goto fail;
575 }
576
577 if (init_buffer) {
578 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
579 switch (buf->vb.field) {
580 case V4L2_FIELD_TOP:
581 cx23885_risc_buffer(dev->pci, &buf->risc,
582 dma->sglist, 0, UNSET,
583 buf->bpl, 0, buf->vb.height);
584 break;
585 case V4L2_FIELD_BOTTOM:
586 cx23885_risc_buffer(dev->pci, &buf->risc,
587 dma->sglist, UNSET, 0,
588 buf->bpl, 0, buf->vb.height);
589 break;
590 case V4L2_FIELD_INTERLACED:
591 if (dev->tvnorm & V4L2_STD_NTSC) {
592 /* cx25840 transmits NTSC bottom field first */
593 dprintk(1, "%s() Creating NTSC risc\n",
594 __FUNCTION__);
595 line0_offset = buf->bpl;
596 line1_offset = 0;
597 } else {
598 /* All other formats are top field first */
599 dprintk(1, "%s() Creating PAL/SECAM risc\n",
600 __FUNCTION__);
601 line0_offset = 0;
602 line1_offset = buf->bpl;
603 }
604 cx23885_risc_buffer(dev->pci, &buf->risc,
605 dma->sglist, line0_offset,
606 line1_offset,
607 buf->bpl, buf->bpl,
608 buf->vb.height >> 1);
609 break;
610 case V4L2_FIELD_SEQ_TB:
611 cx23885_risc_buffer(dev->pci, &buf->risc,
612 dma->sglist,
613 0, buf->bpl * (buf->vb.height >> 1),
614 buf->bpl, 0,
615 buf->vb.height >> 1);
616 break;
617 case V4L2_FIELD_SEQ_BT:
618 cx23885_risc_buffer(dev->pci, &buf->risc,
619 dma->sglist,
620 buf->bpl * (buf->vb.height >> 1), 0,
621 buf->bpl, 0,
622 buf->vb.height >> 1);
623 break;
624 default:
625 BUG();
626 }
627 }
628 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
629 buf, buf->vb.i,
630 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
631 (unsigned long)buf->risc.dma);
632
633 buf->vb.state = VIDEOBUF_PREPARED;
634 return 0;
635
636 fail:
637 cx23885_free_buffer(q, buf);
638 return rc;
639}
640
641static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
642{
643 struct cx23885_buffer *buf = container_of(vb,
644 struct cx23885_buffer, vb);
645 struct cx23885_buffer *prev;
646 struct cx23885_fh *fh = vq->priv_data;
647 struct cx23885_dev *dev = fh->dev;
648 struct cx23885_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 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
654
655 if (!list_empty(&q->queued)) {
656 list_add_tail(&buf->vb.queue, &q->queued);
657 buf->vb.state = VIDEOBUF_QUEUED;
658 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
659 buf, buf->vb.i);
660
661 } else if (list_empty(&q->active)) {
662 list_add_tail(&buf->vb.queue, &q->active);
663 cx23885_start_video_dma(dev, q, buf);
664 buf->vb.state = VIDEOBUF_ACTIVE;
665 buf->count = q->count++;
666 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
667 dprintk(2, "[%p/%d] buffer_queue - first active\n",
668 buf, buf->vb.i);
669
670 } else {
671 prev = list_entry(q->active.prev, struct cx23885_buffer,
672 vb.queue);
673 if (prev->vb.width == buf->vb.width &&
674 prev->vb.height == buf->vb.height &&
675 prev->fmt == buf->fmt) {
676 list_add_tail(&buf->vb.queue, &q->active);
677 buf->vb.state = VIDEOBUF_ACTIVE;
678 buf->count = q->count++;
679 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
680 /* 64 bit bits 63-32 */
681 prev->risc.jmp[2] = cpu_to_le32(0);
682 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
683 buf, buf->vb.i);
684
685 } else {
686 list_add_tail(&buf->vb.queue, &q->queued);
687 buf->vb.state = VIDEOBUF_QUEUED;
688 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
689 buf, buf->vb.i);
690 }
691 }
692}
693
694static void buffer_release(struct videobuf_queue *q,
695 struct videobuf_buffer *vb)
696{
697 struct cx23885_buffer *buf = container_of(vb,
698 struct cx23885_buffer, vb);
699
700 cx23885_free_buffer(q, buf);
701}
702
703static struct videobuf_queue_ops cx23885_video_qops = {
704 .buf_setup = buffer_setup,
705 .buf_prepare = buffer_prepare,
706 .buf_queue = buffer_queue,
707 .buf_release = buffer_release,
708};
709
710static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
711{
712 switch (fh->type) {
713 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
714 return &fh->vidq;
715 case V4L2_BUF_TYPE_VBI_CAPTURE:
716 return &fh->vbiq;
717 default:
718 BUG();
719 return NULL;
720 }
721}
722
723static int get_resource(struct cx23885_fh *fh)
724{
725 switch (fh->type) {
726 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
727 return RESOURCE_VIDEO;
728 case V4L2_BUF_TYPE_VBI_CAPTURE:
729 return RESOURCE_VBI;
730 default:
731 BUG();
732 return 0;
733 }
734}
735
736static int video_open(struct inode *inode, struct file *file)
737{
738 int minor = iminor(inode);
739 struct cx23885_dev *h, *dev = NULL;
740 struct cx23885_fh *fh;
741 struct list_head *list;
742 enum v4l2_buf_type type = 0;
743 int radio = 0;
744
745 list_for_each(list, &cx23885_devlist) {
746 h = list_entry(list, struct cx23885_dev, devlist);
747 if (h->video_dev->minor == minor) {
748 dev = h;
749 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
750 }
751 if (h->vbi_dev &&
752 h->vbi_dev->minor == minor) {
753 dev = h;
754 type = V4L2_BUF_TYPE_VBI_CAPTURE;
755 }
756 if (h->radio_dev &&
757 h->radio_dev->minor == minor) {
758 radio = 1;
759 dev = h;
760 }
761 }
762 if (NULL == dev)
763 return -ENODEV;
764
765 dprintk(1, "open minor=%d radio=%d type=%s\n",
766 minor, radio, v4l2_type_names[type]);
767
768 /* allocate + initialize per filehandle data */
769 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
770 if (NULL == fh)
771 return -ENOMEM;
772 file->private_data = fh;
773 fh->dev = dev;
774 fh->radio = radio;
775 fh->type = type;
776 fh->width = 320;
777 fh->height = 240;
778 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
779
780 videobuf_queue_pci_init(&fh->vidq, &cx23885_video_qops,
781 dev->pci, &dev->slock,
782 V4L2_BUF_TYPE_VIDEO_CAPTURE,
783 V4L2_FIELD_INTERLACED,
784 sizeof(struct cx23885_buffer),
785 fh);
786
787 dprintk(1, "post videobuf_queue_init()\n");
788
789
790 return 0;
791}
792
793static ssize_t video_read(struct file *file, char __user *data,
794 size_t count, loff_t *ppos)
795{
796 struct cx23885_fh *fh = file->private_data;
797
798 switch (fh->type) {
799 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
800 if (res_locked(fh->dev, RESOURCE_VIDEO))
801 return -EBUSY;
802 return videobuf_read_one(&fh->vidq, data, count, ppos,
803 file->f_flags & O_NONBLOCK);
804 case V4L2_BUF_TYPE_VBI_CAPTURE:
805 if (!res_get(fh->dev, fh, RESOURCE_VBI))
806 return -EBUSY;
807 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
808 file->f_flags & O_NONBLOCK);
809 default:
810 BUG();
811 return 0;
812 }
813}
814
815static unsigned int video_poll(struct file *file,
816 struct poll_table_struct *wait)
817{
818 struct cx23885_fh *fh = file->private_data;
819 struct cx23885_buffer *buf;
820
821 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
822 if (!res_get(fh->dev, fh, RESOURCE_VBI))
823 return POLLERR;
824 return videobuf_poll_stream(file, &fh->vbiq, wait);
825 }
826
827 if (res_check(fh, RESOURCE_VIDEO)) {
828 /* streaming capture */
829 if (list_empty(&fh->vidq.stream))
830 return POLLERR;
831 buf = list_entry(fh->vidq.stream.next,
832 struct cx23885_buffer, vb.stream);
833 } else {
834 /* read() capture */
835 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
836 if (NULL == buf)
837 return POLLERR;
838 }
839 poll_wait(file, &buf->vb.done, wait);
840 if (buf->vb.state == VIDEOBUF_DONE ||
841 buf->vb.state == VIDEOBUF_ERROR)
842 return POLLIN|POLLRDNORM;
843 return 0;
844}
845
846static int video_release(struct inode *inode, struct file *file)
847{
848 struct cx23885_fh *fh = file->private_data;
849 struct cx23885_dev *dev = fh->dev;
850
851 /* turn off overlay */
852 if (res_check(fh, RESOURCE_OVERLAY)) {
853 /* FIXME */
854 res_free(dev, fh, RESOURCE_OVERLAY);
855 }
856
857 /* stop video capture */
858 if (res_check(fh, RESOURCE_VIDEO)) {
859 videobuf_queue_cancel(&fh->vidq);
860 res_free(dev, fh, RESOURCE_VIDEO);
861 }
862 if (fh->vidq.read_buf) {
863 buffer_release(&fh->vidq, fh->vidq.read_buf);
864 kfree(fh->vidq.read_buf);
865 }
866
867 /* stop vbi capture */
868 if (res_check(fh, RESOURCE_VBI)) {
869 if (fh->vbiq.streaming)
870 videobuf_streamoff(&fh->vbiq);
871 if (fh->vbiq.reading)
872 videobuf_read_stop(&fh->vbiq);
873 res_free(dev, fh, RESOURCE_VBI);
874 }
875
876 videobuf_mmap_free(&fh->vidq);
877 file->private_data = NULL;
878 kfree(fh);
879
880
881 return 0;
882}
883
884static int video_mmap(struct file *file, struct vm_area_struct *vma)
885{
886 struct cx23885_fh *fh = file->private_data;
887
888 return videobuf_mmap_mapper(get_queue(fh), vma);
889}
890
891/* ------------------------------------------------------------------ */
892/* VIDEO CTRL IOCTLS */
893
894int cx23885_get_control(struct cx23885_dev *dev, struct v4l2_control *ctl)
895{
896 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __FUNCTION__);
897 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_G_CTRL, ctl);
898 return 0;
899}
900EXPORT_SYMBOL(cx23885_get_control);
901
902int cx23885_set_control(struct cx23885_dev *dev, struct v4l2_control *ctl)
903{
904 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
905 " (disabled - no action)\n", __FUNCTION__);
906 return 0;
907}
908EXPORT_SYMBOL(cx23885_set_control);
909
910static void init_controls(struct cx23885_dev *dev)
911{
912 struct v4l2_control ctrl;
913 int i;
914
915 for (i = 0; i < CX23885_CTLS; i++) {
916 ctrl.id = cx23885_ctls[i].v.id;
917 ctrl.value = cx23885_ctls[i].v.default_value;
918
919 cx23885_set_control(dev, &ctrl);
920 }
921}
922
923/* ------------------------------------------------------------------ */
924/* VIDEO IOCTLS */
925
926static int vidioc_g_fmt_cap(struct file *file, void *priv,
927 struct v4l2_format *f)
928{
929 struct cx23885_fh *fh = priv;
930
931 f->fmt.pix.width = fh->width;
932 f->fmt.pix.height = fh->height;
933 f->fmt.pix.field = fh->vidq.field;
934 f->fmt.pix.pixelformat = fh->fmt->fourcc;
935 f->fmt.pix.bytesperline =
936 (f->fmt.pix.width * fh->fmt->depth) >> 3;
937 f->fmt.pix.sizeimage =
938 f->fmt.pix.height * f->fmt.pix.bytesperline;
939
940 return 0;
941}
942
943static int vidioc_try_fmt_cap(struct file *file, void *priv,
944 struct v4l2_format *f)
945{
946 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
947 struct cx23885_fmt *fmt;
948 enum v4l2_field field;
949 unsigned int maxw, maxh;
950
951 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
952 if (NULL == fmt)
953 return -EINVAL;
954
955 field = f->fmt.pix.field;
956 maxw = norm_maxw(dev->tvnorm);
957 maxh = norm_maxh(dev->tvnorm);
958
959 if (V4L2_FIELD_ANY == field) {
960 field = (f->fmt.pix.height > maxh/2)
961 ? V4L2_FIELD_INTERLACED
962 : V4L2_FIELD_BOTTOM;
963 }
964
965 switch (field) {
966 case V4L2_FIELD_TOP:
967 case V4L2_FIELD_BOTTOM:
968 maxh = maxh / 2;
969 break;
970 case V4L2_FIELD_INTERLACED:
971 break;
972 default:
973 return -EINVAL;
974 }
975
976 f->fmt.pix.field = field;
977 if (f->fmt.pix.height < 32)
978 f->fmt.pix.height = 32;
979 if (f->fmt.pix.height > maxh)
980 f->fmt.pix.height = maxh;
981 if (f->fmt.pix.width < 48)
982 f->fmt.pix.width = 48;
983 if (f->fmt.pix.width > maxw)
984 f->fmt.pix.width = maxw;
985 f->fmt.pix.width &= ~0x03;
986 f->fmt.pix.bytesperline =
987 (f->fmt.pix.width * fmt->depth) >> 3;
988 f->fmt.pix.sizeimage =
989 f->fmt.pix.height * f->fmt.pix.bytesperline;
990
991 return 0;
992}
993
994static int vidioc_s_fmt_cap(struct file *file, void *priv,
995 struct v4l2_format *f)
996{
997 struct cx23885_fh *fh = priv;
998 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
999 int err;
1000
1001 dprintk(2, "%s()\n", __FUNCTION__);
1002 err = vidioc_try_fmt_cap(file, priv, f);
1003
1004 if (0 != err)
1005 return err;
1006 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1007 fh->width = f->fmt.pix.width;
1008 fh->height = f->fmt.pix.height;
1009 fh->vidq.field = f->fmt.pix.field;
1010 dprintk(2, "%s() width=%d height=%d field=%d\n", __FUNCTION__,
1011 fh->width, fh->height, fh->vidq.field);
1012 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_FMT, f);
1013 return 0;
1014}
1015
1016static int vidioc_querycap(struct file *file, void *priv,
1017 struct v4l2_capability *cap)
1018{
1019 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1020
1021 strcpy(cap->driver, "cx23885");
1022 strlcpy(cap->card, cx23885_boards[dev->board].name,
1023 sizeof(cap->card));
1024 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1025 cap->version = CX23885_VERSION_CODE;
1026 cap->capabilities =
1027 V4L2_CAP_VIDEO_CAPTURE |
1028 V4L2_CAP_READWRITE |
1029 V4L2_CAP_STREAMING |
1030 V4L2_CAP_VBI_CAPTURE;
1031 if (UNSET != dev->tuner_type)
1032 cap->capabilities |= V4L2_CAP_TUNER;
1033 return 0;
1034}
1035
1036static int vidioc_enum_fmt_cap(struct file *file, void *priv,
1037 struct v4l2_fmtdesc *f)
1038{
1039 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1040 return -EINVAL;
1041
1042 strlcpy(f->description, formats[f->index].name,
1043 sizeof(f->description));
1044 f->pixelformat = formats[f->index].fourcc;
1045
1046 return 0;
1047}
1048
1049#ifdef CONFIG_VIDEO_V4L1_COMPAT
1050static int vidiocgmbuf(struct file *file, void *priv,
1051 struct video_mbuf *mbuf)
1052{
1053 struct cx23885_fh *fh = priv;
1054 struct videobuf_queue *q;
1055 struct v4l2_requestbuffers req;
1056 unsigned int i;
1057 int err;
1058
1059 q = get_queue(fh);
1060 memset(&req, 0, sizeof(req));
1061 req.type = q->type;
1062 req.count = 8;
1063 req.memory = V4L2_MEMORY_MMAP;
1064 err = videobuf_reqbufs(q, &req);
1065 if (err < 0)
1066 return err;
1067
1068 mbuf->frames = req.count;
1069 mbuf->size = 0;
1070 for (i = 0; i < mbuf->frames; i++) {
1071 mbuf->offsets[i] = q->bufs[i]->boff;
1072 mbuf->size += q->bufs[i]->bsize;
1073 }
1074 return 0;
1075}
1076#endif
1077
1078static int vidioc_reqbufs(struct file *file, void *priv,
1079 struct v4l2_requestbuffers *p)
1080{
1081 struct cx23885_fh *fh = priv;
1082 return (videobuf_reqbufs(get_queue(fh), p));
1083}
1084
1085static int vidioc_querybuf(struct file *file, void *priv,
1086 struct v4l2_buffer *p)
1087{
1088 struct cx23885_fh *fh = priv;
1089 return (videobuf_querybuf(get_queue(fh), p));
1090}
1091
1092static int vidioc_qbuf(struct file *file, void *priv,
1093 struct v4l2_buffer *p)
1094{
1095 struct cx23885_fh *fh = priv;
1096 return (videobuf_qbuf(get_queue(fh), p));
1097}
1098
1099static int vidioc_dqbuf(struct file *file, void *priv,
1100 struct v4l2_buffer *p)
1101{
1102 struct cx23885_fh *fh = priv;
1103 return (videobuf_dqbuf(get_queue(fh), p,
1104 file->f_flags & O_NONBLOCK));
1105}
1106
1107static int vidioc_streamon(struct file *file, void *priv,
1108 enum v4l2_buf_type i)
1109{
1110 struct cx23885_fh *fh = priv;
1111 struct cx23885_dev *dev = fh->dev;
1112 dprintk(1, "%s()\n", __FUNCTION__);
1113
1114 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1115 return -EINVAL;
1116 if (unlikely(i != fh->type))
1117 return -EINVAL;
1118
1119 if (unlikely(!res_get(dev, fh, get_resource(fh))))
1120 return -EBUSY;
1121 return videobuf_streamon(get_queue(fh));
1122}
1123
1124static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1125{
1126 struct cx23885_fh *fh = priv;
1127 struct cx23885_dev *dev = fh->dev;
1128 int err, res;
1129 dprintk(1, "%s()\n", __FUNCTION__);
1130
1131 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1132 return -EINVAL;
1133 if (i != fh->type)
1134 return -EINVAL;
1135
1136 res = get_resource(fh);
1137 err = videobuf_streamoff(get_queue(fh));
1138 if (err < 0)
1139 return err;
1140 res_free(dev, fh, res);
1141 return 0;
1142}
1143
1144static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1145{
1146 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1147 dprintk(1, "%s()\n", __FUNCTION__);
1148
1149 mutex_lock(&dev->lock);
1150 cx23885_set_tvnorm(dev, *tvnorms);
1151 mutex_unlock(&dev->lock);
1152
1153 return 0;
1154}
1155
1156int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
1157{
1158 static const char *iname[] = {
1159 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1160 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1161 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1162 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1163 [CX23885_VMUX_SVIDEO] = "S-Video",
1164 [CX23885_VMUX_TELEVISION] = "Television",
1165 [CX23885_VMUX_CABLE] = "Cable TV",
1166 [CX23885_VMUX_DVB] = "DVB",
1167 [CX23885_VMUX_DEBUG] = "for debug only",
1168 };
1169 unsigned int n;
1170 dprintk(1, "%s()\n", __FUNCTION__);
1171
1172 n = i->index;
1173 if (n >= 4)
1174 return -EINVAL;
1175
1176 if (0 == INPUT(n)->type)
1177 return -EINVAL;
1178
1179 memset(i, 0, sizeof(*i));
1180 i->index = n;
1181 i->type = V4L2_INPUT_TYPE_CAMERA;
1182 strcpy(i->name, iname[INPUT(n)->type]);
1183 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
1184 (CX23885_VMUX_CABLE == INPUT(n)->type))
1185 i->type = V4L2_INPUT_TYPE_TUNER;
1186 i->std = CX23885_NORMS;
1187 return 0;
1188}
1189EXPORT_SYMBOL(cx23885_enum_input);
1190
1191static int vidioc_enum_input(struct file *file, void *priv,
1192 struct v4l2_input *i)
1193{
1194 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1195 dprintk(1, "%s()\n", __FUNCTION__);
1196 return cx23885_enum_input(dev, i);
1197}
1198
1199static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1200{
1201 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1202
1203 *i = dev->input;
1204 dprintk(1, "%s() returns %d\n", __FUNCTION__, *i);
1205 return 0;
1206}
1207
1208static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1209{
1210 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1211
1212 dprintk(1, "%s(%d)\n", __FUNCTION__, i);
1213
1214 if (i >= 4) {
1215 dprintk(1, "%s() -EINVAL\n", __FUNCTION__);
1216 return -EINVAL;
1217 }
1218
1219 mutex_lock(&dev->lock);
1220 cx23885_video_mux(dev, i);
1221 mutex_unlock(&dev->lock);
1222 return 0;
1223}
1224
1225static int vidioc_queryctrl(struct file *file, void *priv,
1226 struct v4l2_queryctrl *qctrl)
1227{
1228 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1229 if (unlikely(qctrl->id == 0))
1230 return -EINVAL;
1231 return cx23885_ctrl_query(qctrl);
1232}
1233
1234static int vidioc_g_ctrl(struct file *file, void *priv,
1235 struct v4l2_control *ctl)
1236{
1237 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1238
1239 return cx23885_get_control(dev, ctl);
1240}
1241
1242static int vidioc_s_ctrl(struct file *file, void *priv,
1243 struct v4l2_control *ctl)
1244{
1245 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1246
1247 return cx23885_set_control(dev, ctl);
1248}
1249
1250static int vidioc_g_tuner(struct file *file, void *priv,
1251 struct v4l2_tuner *t)
1252{
1253 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1254
1255 if (unlikely(UNSET == dev->tuner_type))
1256 return -EINVAL;
1257 if (0 != t->index)
1258 return -EINVAL;
1259
1260 strcpy(t->name, "Television");
1261 t->type = V4L2_TUNER_ANALOG_TV;
1262 t->capability = V4L2_TUNER_CAP_NORM;
1263 t->rangehigh = 0xffffffffUL;
1264 t->signal = 0xffff ; /* LOCKED */
1265 return 0;
1266}
1267
1268static int vidioc_s_tuner(struct file *file, void *priv,
1269 struct v4l2_tuner *t)
1270{
1271 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1272
1273 if (UNSET == dev->tuner_type)
1274 return -EINVAL;
1275 if (0 != t->index)
1276 return -EINVAL;
1277 return 0;
1278}
1279
1280static int vidioc_g_frequency(struct file *file, void *priv,
1281 struct v4l2_frequency *f)
1282{
1283 struct cx23885_fh *fh = priv;
1284 struct cx23885_dev *dev = fh->dev;
1285
1286 if (unlikely(UNSET == dev->tuner_type))
1287 return -EINVAL;
1288
1289 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1290 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1291 f->frequency = dev->freq;
1292
1293 cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_G_FREQUENCY, f);
1294
1295 return 0;
1296}
1297
1298int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
1299{
1300 if (unlikely(UNSET == dev->tuner_type))
1301 return -EINVAL;
1302 if (unlikely(f->tuner != 0))
1303 return -EINVAL;
1304
1305 mutex_lock(&dev->lock);
1306 dev->freq = f->frequency;
1307
1308 cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, f);
1309
1310 /* When changing channels it is required to reset TVAUDIO */
1311 msleep(10);
1312
1313 mutex_unlock(&dev->lock);
1314
1315 return 0;
1316}
1317EXPORT_SYMBOL(cx23885_set_freq);
1318
1319static int vidioc_s_frequency(struct file *file, void *priv,
1320 struct v4l2_frequency *f)
1321{
1322 struct cx23885_fh *fh = priv;
1323 struct cx23885_dev *dev = fh->dev;
1324
1325 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1326 return -EINVAL;
1327 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1328 return -EINVAL;
1329
1330 return
1331 cx23885_set_freq(dev, f);
1332}
1333
1334#ifdef CONFIG_VIDEO_ADV_DEBUG
1335static int vidioc_g_register(struct file *file, void *fh,
1336 struct v4l2_register *reg)
1337{
1338 struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1339
1340 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_G_REGISTER, reg);
1341
1342 return 0;
1343}
1344
1345static int vidioc_s_register(struct file *file, void *fh,
1346 struct v4l2_register *reg)
1347{
1348 struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1349
1350 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_S_REGISTER, reg);
1351 return 0;
1352}
1353#endif
1354
1355/* ----------------------------------------------------------- */
1356/* RADIO ESPECIFIC IOCTLS */
1357/* ----------------------------------------------------------- */
1358
1359static int radio_querycap(struct file *file, void *priv,
1360 struct v4l2_capability *cap)
1361{
1362 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1363
1364 strcpy(cap->driver, "cx23885");
1365 strlcpy(cap->card, cx23885_boards[dev->board].name,
1366 sizeof(cap->card));
1367 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1368 cap->version = CX23885_VERSION_CODE;
1369 cap->capabilities = V4L2_CAP_TUNER;
1370 return 0;
1371}
1372
1373static int radio_g_tuner(struct file *file, void *priv,
1374 struct v4l2_tuner *t)
1375{
1376 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1377
1378 if (unlikely(t->index > 0))
1379 return -EINVAL;
1380
1381 strcpy(t->name, "Radio");
1382 t->type = V4L2_TUNER_RADIO;
1383
1384 cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_G_TUNER, t);
1385 return 0;
1386}
1387
1388static int radio_enum_input(struct file *file, void *priv,
1389 struct v4l2_input *i)
1390{
1391 if (i->index != 0)
1392 return -EINVAL;
1393 strcpy(i->name, "Radio");
1394 i->type = V4L2_INPUT_TYPE_TUNER;
1395
1396 return 0;
1397}
1398
1399static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
1400{
1401 if (unlikely(a->index))
1402 return -EINVAL;
1403
1404 memset(a, 0, sizeof(*a));
1405 strcpy(a->name, "Radio");
1406 return 0;
1407}
1408
1409/* FIXME: Should add a standard for radio */
1410
1411static int radio_s_tuner(struct file *file, void *priv,
1412 struct v4l2_tuner *t)
1413{
1414 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1415
1416 if (0 != t->index)
1417 return -EINVAL;
1418
1419 cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_TUNER, t);
1420
1421 return 0;
1422}
1423
1424static int radio_s_audio(struct file *file, void *fh,
1425 struct v4l2_audio *a)
1426{
1427 return 0;
1428}
1429
1430static int radio_s_input(struct file *file, void *fh, unsigned int i)
1431{
1432 return 0;
1433}
1434
1435static int radio_queryctrl(struct file *file, void *priv,
1436 struct v4l2_queryctrl *c)
1437{
1438 int i;
1439
1440 if (c->id < V4L2_CID_BASE ||
1441 c->id >= V4L2_CID_LASTP1)
1442 return -EINVAL;
1443 if (c->id == V4L2_CID_AUDIO_MUTE) {
1444 for (i = 0; i < CX23885_CTLS; i++)
1445 if (cx23885_ctls[i].v.id == c->id)
1446 break;
1447 *c = cx23885_ctls[i].v;
1448 } else
1449 *c = no_ctl;
1450 return 0;
1451}
1452
1453/* ----------------------------------------------------------- */
1454
1455static void cx23885_vid_timeout(unsigned long data)
1456{
1457 struct cx23885_dev *dev = (struct cx23885_dev *)data;
1458 struct cx23885_dmaqueue *q = &dev->vidq;
1459 struct cx23885_buffer *buf;
1460 unsigned long flags;
1461
1462 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1463
1464 cx_clear(VID_A_DMA_CTL, 0x11);
1465
1466 spin_lock_irqsave(&dev->slock, flags);
1467 while (!list_empty(&q->active)) {
1468 buf = list_entry(q->active.next,
1469 struct cx23885_buffer, vb.queue);
1470 list_del(&buf->vb.queue);
1471 buf->vb.state = VIDEOBUF_ERROR;
1472 wake_up(&buf->vb.done);
1473 printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
1474 dev->name, buf, buf->vb.i,
1475 (unsigned long)buf->risc.dma);
1476 }
1477 cx23885_restart_video_queue(dev, q);
1478 spin_unlock_irqrestore(&dev->slock, flags);
1479}
1480
1481int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1482{
1483 u32 mask, count;
1484 int handled = 0;
1485
1486 mask = cx_read(VID_A_INT_MSK);
1487 if (0 == (status & mask))
1488 return handled;
1489 cx_write(VID_A_INT_STAT, status);
1490
1491 dprintk(2, "%s() status = 0x%08x\n", __FUNCTION__, status);
1492 /* risc op code error */
1493 if (status & (1 << 16)) {
1494 printk(KERN_WARNING "%s/0: video risc op code error\n",
1495 dev->name);
1496 cx_clear(VID_A_DMA_CTL, 0x11);
1497 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1498 }
1499
1500 /* risc1 y */
1501 if (status & 0x01) {
1502 spin_lock(&dev->slock);
1503 count = cx_read(VID_A_GPCNT);
1504 cx23885_video_wakeup(dev, &dev->vidq, count);
1505 spin_unlock(&dev->slock);
1506 handled++;
1507 }
1508 /* risc2 y */
1509 if (status & 0x10) {
1510 dprintk(2, "stopper video\n");
1511 spin_lock(&dev->slock);
1512 cx23885_restart_video_queue(dev, &dev->vidq);
1513 spin_unlock(&dev->slock);
1514 handled++;
1515 }
1516
1517 return handled;
1518}
1519
1520
1521/* ----------------------------------------------------------- */
1522/* exported stuff */
1523
1524static const struct file_operations video_fops = {
1525 .owner = THIS_MODULE,
1526 .open = video_open,
1527 .release = video_release,
1528 .read = video_read,
1529 .poll = video_poll,
1530 .mmap = video_mmap,
1531 .ioctl = video_ioctl2,
1532 .compat_ioctl = v4l_compat_ioctl32,
1533 .llseek = no_llseek,
1534};
1535
1536static struct video_device cx23885_vbi_template;
1537static struct video_device cx23885_video_template = {
1538 .name = "cx23885-video",
1539 .type = VID_TYPE_CAPTURE|VID_TYPE_TUNER|VID_TYPE_SCALES,
1540 .fops = &video_fops,
1541 .minor = -1,
1542 .vidioc_querycap = vidioc_querycap,
1543 .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
1544 .vidioc_g_fmt_cap = vidioc_g_fmt_cap,
1545 .vidioc_try_fmt_cap = vidioc_try_fmt_cap,
1546 .vidioc_s_fmt_cap = vidioc_s_fmt_cap,
1547 .vidioc_g_fmt_vbi = cx23885_vbi_fmt,
1548 .vidioc_try_fmt_vbi = cx23885_vbi_fmt,
1549 .vidioc_s_fmt_vbi = cx23885_vbi_fmt,
1550 .vidioc_reqbufs = vidioc_reqbufs,
1551 .vidioc_querybuf = vidioc_querybuf,
1552 .vidioc_qbuf = vidioc_qbuf,
1553 .vidioc_dqbuf = vidioc_dqbuf,
1554 .vidioc_s_std = vidioc_s_std,
1555 .vidioc_enum_input = vidioc_enum_input,
1556 .vidioc_g_input = vidioc_g_input,
1557 .vidioc_s_input = vidioc_s_input,
1558 .vidioc_queryctrl = vidioc_queryctrl,
1559 .vidioc_g_ctrl = vidioc_g_ctrl,
1560 .vidioc_s_ctrl = vidioc_s_ctrl,
1561 .vidioc_streamon = vidioc_streamon,
1562 .vidioc_streamoff = vidioc_streamoff,
1563#ifdef CONFIG_VIDEO_V4L1_COMPAT
1564 .vidiocgmbuf = vidiocgmbuf,
1565#endif
1566 .vidioc_g_tuner = vidioc_g_tuner,
1567 .vidioc_s_tuner = vidioc_s_tuner,
1568 .vidioc_g_frequency = vidioc_g_frequency,
1569 .vidioc_s_frequency = vidioc_s_frequency,
1570#ifdef CONFIG_VIDEO_ADV_DEBUG
1571 .vidioc_g_register = vidioc_g_register,
1572 .vidioc_s_register = vidioc_s_register,
1573#endif
1574 .tvnorms = CX23885_NORMS,
1575 .current_norm = V4L2_STD_NTSC_M,
1576};
1577
1578static const struct file_operations radio_fops = {
1579 .owner = THIS_MODULE,
1580 .open = video_open,
1581 .release = video_release,
1582 .ioctl = video_ioctl2,
1583 .compat_ioctl = v4l_compat_ioctl32,
1584 .llseek = no_llseek,
1585};
1586
1587
1588void cx23885_video_unregister(struct cx23885_dev *dev)
1589{
1590 dprintk(1, "%s()\n", __FUNCTION__);
1591 cx_clear(PCI_INT_MSK, 1);
1592
1593 if (dev->video_dev) {
1594 if (-1 != dev->video_dev->minor)
1595 video_unregister_device(dev->video_dev);
1596 else
1597 video_device_release(dev->video_dev);
1598 dev->video_dev = NULL;
1599
1600 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1601 }
1602}
1603
1604int cx23885_video_register(struct cx23885_dev *dev)
1605{
1606 int err;
1607
1608 dprintk(1, "%s()\n", __FUNCTION__);
1609 spin_lock_init(&dev->slock);
1610
1611 /* Initialize VBI template */
1612 memcpy(&cx23885_vbi_template, &cx23885_video_template,
1613 sizeof(cx23885_vbi_template));
1614 strcpy(cx23885_vbi_template.name, "cx23885-vbi");
1615 cx23885_vbi_template.type = VID_TYPE_TELETEXT|VID_TYPE_TUNER;
1616
1617 dev->tvnorm = cx23885_video_template.current_norm;
1618
1619 /* init video dma queues */
1620 INIT_LIST_HEAD(&dev->vidq.active);
1621 INIT_LIST_HEAD(&dev->vidq.queued);
1622 dev->vidq.timeout.function = cx23885_vid_timeout;
1623 dev->vidq.timeout.data = (unsigned long)dev;
1624 init_timer(&dev->vidq.timeout);
1625 cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1626 VID_A_DMA_CTL, 0x11, 0x00);
1627
1628 cx_set(PCI_INT_MSK, 1);
1629
1630
1631 /* register v4l devices */
1632 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1633 &cx23885_video_template, "video");
1634 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1635 video_nr[dev->nr]);
1636 if (err < 0) {
1637 printk(KERN_INFO "%s: can't register video device\n",
1638 dev->name);
1639 goto fail_unreg;
1640 }
1641 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1642 dev->name, dev->video_dev->minor & 0x1f);
1643 /* initial device configuration */
1644 mutex_lock(&dev->lock);
1645 cx23885_set_tvnorm(dev, dev->tvnorm);
1646 init_controls(dev);
1647 cx23885_video_mux(dev, 0);
1648 mutex_unlock(&dev->lock);
1649
1650 /* FIXME start tvaudio thread */
1651
1652 return 0;
1653
1654fail_unreg:
1655 cx23885_video_unregister(dev);
1656 return err;
1657}
1658