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