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