blob: d9bef1a54d1fa8285a96122a6b236230c4797ef3 [file] [log] [blame]
Steven Tothe47f30b2008-01-10 04:25:59 -03001/*
2 * Driver for the Conexant CX23885 PCIe bridge
3 *
Steven Toth6d897612008-09-03 17:12:12 -03004 * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org>
Steven Tothe47f30b2008-01-10 04:25:59 -03005 *
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>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030036#include <media/v4l2-ioctl.h>
Steven Tothe47f30b2008-01-10 04:25:59 -030037
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");
Steven Toth6d897612008-09-03 17:12:12 -030044MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
Steven Tothe47f30b2008-01-10 04:25:59 -030045MODULE_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
Steven Toth4513fc692008-01-12 11:36:36 -030061static unsigned int video_debug;
Steven Tothe47f30b2008-01-10 04:25:59 -030062module_param(video_debug, int, 0644);
63MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
64
Steven Toth4513fc692008-01-12 11:36:36 -030065static unsigned int irq_debug;
Steven Tothe47f30b2008-01-10 04:25:59 -030066module_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...)\
Steven Toth4513fc692008-01-12 11:36:36 -030074 do { if (video_debug >= level)\
75 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
76 } while (0)
Steven Tothe47f30b2008-01-10 04:25:59 -030077
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
Harvey Harrison22b4e642008-04-08 23:20:00 -0300145 printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __func__, fourcc);
Steven Tothe47f30b2008-01-10 04:25:59 -0300146 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
247const u32 cx23885_user_ctrls[] = {
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};
257EXPORT_SYMBOL(cx23885_user_ctrls);
258
259static const u32 *ctrl_classes[] = {
260 cx23885_user_ctrls,
261 NULL
262};
263
264void cx23885_video_wakeup(struct cx23885_dev *dev,
265 struct cx23885_dmaqueue *q, u32 count)
266{
267 struct cx23885_buffer *buf;
268 int bc;
269
270 for (bc = 0;; bc++) {
271 if (list_empty(&q->active))
272 break;
273 buf = list_entry(q->active.next,
274 struct cx23885_buffer, vb.queue);
Steven Totha19602f22008-01-10 11:43:18 -0300275
Steven Tothe47f30b2008-01-10 04:25:59 -0300276 /* count comes from the hw and is is 16bit wide --
277 * this trick handles wrap-arounds correctly for
278 * up to 32767 buffers in flight... */
279 if ((s16) (count - buf->count) < 0)
280 break;
Steven Totha19602f22008-01-10 11:43:18 -0300281
Steven Tothe47f30b2008-01-10 04:25:59 -0300282 do_gettimeofday(&buf->vb.ts);
283 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
284 count, buf->count);
285 buf->vb.state = VIDEOBUF_DONE;
286 list_del(&buf->vb.queue);
287 wake_up(&buf->vb.done);
288 }
289 if (list_empty(&q->active)) {
290 del_timer(&q->timeout);
291 } else {
292 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
293 }
294 if (bc != 1)
Steven Toth21a78092008-01-10 04:38:59 -0300295 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300296 __func__, bc);
Steven Tothe47f30b2008-01-10 04:25:59 -0300297}
298
299int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
300{
301 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300302 __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -0300303 (unsigned int)norm,
304 v4l2_norm_to_name(norm));
305
306 dev->tvnorm = norm;
307
Steven Tothe47f30b2008-01-10 04:25:59 -0300308 /* Tell the analog tuner/demods */
309 cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_STD, &norm);
310
311 /* Tell the internal A/V decoder */
312 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_STD, &norm);
313
314 return 0;
315}
316
317struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
318 struct pci_dev *pci,
319 struct video_device *template,
320 char *type)
321{
322 struct video_device *vfd;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300323 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300324
325 vfd = video_device_alloc();
326 if (NULL == vfd)
327 return NULL;
328 *vfd = *template;
329 vfd->minor = -1;
Hans Verkuil5e85e732008-07-20 06:31:39 -0300330 vfd->parent = &pci->dev;
Steven Tothe47f30b2008-01-10 04:25:59 -0300331 vfd->release = video_device_release;
332 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
333 dev->name, type, cx23885_boards[dev->board].name);
334 return vfd;
335}
336
337int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
338{
339 int i;
340
341 if (qctrl->id < V4L2_CID_BASE ||
342 qctrl->id >= V4L2_CID_LASTP1)
343 return -EINVAL;
344 for (i = 0; i < CX23885_CTLS; i++)
345 if (cx23885_ctls[i].v.id == qctrl->id)
346 break;
347 if (i == CX23885_CTLS) {
348 *qctrl = no_ctl;
349 return 0;
350 }
351 *qctrl = cx23885_ctls[i].v;
352 return 0;
353}
354EXPORT_SYMBOL(cx23885_ctrl_query);
355
356/* ------------------------------------------------------------------- */
357/* resource management */
358
359static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
360 unsigned int bit)
361{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300362 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300363 if (fh->resources & bit)
364 /* have it already allocated */
365 return 1;
366
367 /* is it free? */
368 mutex_lock(&dev->lock);
369 if (dev->resources & bit) {
370 /* no, someone else uses it */
371 mutex_unlock(&dev->lock);
372 return 0;
373 }
374 /* it's free, grab it */
375 fh->resources |= bit;
376 dev->resources |= bit;
377 dprintk(1, "res: get %d\n", bit);
378 mutex_unlock(&dev->lock);
379 return 1;
380}
381
382static int res_check(struct cx23885_fh *fh, unsigned int bit)
383{
384 return (fh->resources & bit);
385}
386
387static int res_locked(struct cx23885_dev *dev, unsigned int bit)
388{
389 return (dev->resources & bit);
390}
391
392static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
393 unsigned int bits)
394{
395 BUG_ON((fh->resources & bits) != bits);
Harvey Harrison22b4e642008-04-08 23:20:00 -0300396 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300397
398 mutex_lock(&dev->lock);
399 fh->resources &= ~bits;
400 dev->resources &= ~bits;
401 dprintk(1, "res: put %d\n", bits);
402 mutex_unlock(&dev->lock);
403}
404
405int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
406{
407 struct v4l2_routing route;
408 memset(&route, 0, sizeof(route));
409
410 dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300411 __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -0300412 input, INPUT(input)->vmux,
413 INPUT(input)->gpio0, INPUT(input)->gpio1,
414 INPUT(input)->gpio2, INPUT(input)->gpio3);
415 dev->input = input;
416
417 route.input = INPUT(input)->vmux;
418
419 /* Tell the internal A/V decoder */
420 cx23885_call_i2c_clients(&dev->i2c_bus[2],
421 VIDIOC_INT_S_VIDEO_ROUTING, &route);
422
423 return 0;
424}
425EXPORT_SYMBOL(cx23885_video_mux);
426
427/* ------------------------------------------------------------------ */
428int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
429 unsigned int height, enum v4l2_field field)
430{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300431 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300432 return 0;
433}
434
435static int cx23885_start_video_dma(struct cx23885_dev *dev,
436 struct cx23885_dmaqueue *q,
437 struct cx23885_buffer *buf)
438{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300439 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300440
441 /* setup fifo + format */
442 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
443 buf->bpl, buf->risc.dma);
444 cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
445
446 /* reset counter */
447 cx_write(VID_A_GPCNT_CTL, 3);
448 q->count = 1;
449
450 /* enable irq */
451 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
452 cx_set(VID_A_INT_MSK, 0x000011);
453
454 /* start dma */
455 cx_set(DEV_CNTRL2, (1<<5));
456 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
457
458 return 0;
459}
460
Steven Tothe47f30b2008-01-10 04:25:59 -0300461
462static int cx23885_restart_video_queue(struct cx23885_dev *dev,
463 struct cx23885_dmaqueue *q)
464{
465 struct cx23885_buffer *buf, *prev;
466 struct list_head *item;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300467 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300468
469 if (!list_empty(&q->active)) {
470 buf = list_entry(q->active.next, struct cx23885_buffer,
471 vb.queue);
472 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
473 buf, buf->vb.i);
474 cx23885_start_video_dma(dev, q, buf);
475 list_for_each(item, &q->active) {
476 buf = list_entry(item, struct cx23885_buffer,
477 vb.queue);
478 buf->count = q->count++;
479 }
480 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
481 return 0;
482 }
483
484 prev = NULL;
485 for (;;) {
486 if (list_empty(&q->queued))
487 return 0;
488 buf = list_entry(q->queued.next, struct cx23885_buffer,
489 vb.queue);
490 if (NULL == prev) {
491 list_move_tail(&buf->vb.queue, &q->active);
492 cx23885_start_video_dma(dev, q, buf);
493 buf->vb.state = VIDEOBUF_ACTIVE;
494 buf->count = q->count++;
495 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
496 dprintk(2, "[%p/%d] restart_queue - first active\n",
497 buf, buf->vb.i);
498
499 } else if (prev->vb.width == buf->vb.width &&
500 prev->vb.height == buf->vb.height &&
501 prev->fmt == buf->fmt) {
502 list_move_tail(&buf->vb.queue, &q->active);
503 buf->vb.state = VIDEOBUF_ACTIVE;
504 buf->count = q->count++;
505 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
506 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
507 dprintk(2, "[%p/%d] restart_queue - move to active\n",
508 buf, buf->vb.i);
509 } else {
510 return 0;
511 }
512 prev = buf;
513 }
514}
515
516static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
517 unsigned int *size)
518{
519 struct cx23885_fh *fh = q->priv_data;
520
521 *size = fh->fmt->depth*fh->width*fh->height >> 3;
522 if (0 == *count)
523 *count = 32;
524 while (*size * *count > vid_limit * 1024 * 1024)
525 (*count)--;
526 return 0;
527}
528
529static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
530 enum v4l2_field field)
531{
532 struct cx23885_fh *fh = q->priv_data;
533 struct cx23885_dev *dev = fh->dev;
534 struct cx23885_buffer *buf =
535 container_of(vb, struct cx23885_buffer, vb);
536 int rc, init_buffer = 0;
537 u32 line0_offset, line1_offset;
538 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
539
540 BUG_ON(NULL == fh->fmt);
541 if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
542 fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
543 return -EINVAL;
544 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
545 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
546 return -EINVAL;
547
548 if (buf->fmt != fh->fmt ||
549 buf->vb.width != fh->width ||
550 buf->vb.height != fh->height ||
551 buf->vb.field != field) {
552 buf->fmt = fh->fmt;
553 buf->vb.width = fh->width;
554 buf->vb.height = fh->height;
555 buf->vb.field = field;
556 init_buffer = 1;
557 }
558
559 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
560 init_buffer = 1;
561 rc = videobuf_iolock(q, &buf->vb, NULL);
562 if (0 != rc)
563 goto fail;
564 }
565
566 if (init_buffer) {
567 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
568 switch (buf->vb.field) {
569 case V4L2_FIELD_TOP:
570 cx23885_risc_buffer(dev->pci, &buf->risc,
571 dma->sglist, 0, UNSET,
572 buf->bpl, 0, buf->vb.height);
573 break;
574 case V4L2_FIELD_BOTTOM:
575 cx23885_risc_buffer(dev->pci, &buf->risc,
576 dma->sglist, UNSET, 0,
577 buf->bpl, 0, buf->vb.height);
578 break;
579 case V4L2_FIELD_INTERLACED:
580 if (dev->tvnorm & V4L2_STD_NTSC) {
581 /* cx25840 transmits NTSC bottom field first */
582 dprintk(1, "%s() Creating NTSC risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300583 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300584 line0_offset = buf->bpl;
585 line1_offset = 0;
586 } else {
587 /* All other formats are top field first */
588 dprintk(1, "%s() Creating PAL/SECAM risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300589 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300590 line0_offset = 0;
591 line1_offset = buf->bpl;
592 }
593 cx23885_risc_buffer(dev->pci, &buf->risc,
594 dma->sglist, line0_offset,
595 line1_offset,
596 buf->bpl, buf->bpl,
597 buf->vb.height >> 1);
598 break;
599 case V4L2_FIELD_SEQ_TB:
600 cx23885_risc_buffer(dev->pci, &buf->risc,
601 dma->sglist,
602 0, buf->bpl * (buf->vb.height >> 1),
603 buf->bpl, 0,
604 buf->vb.height >> 1);
605 break;
606 case V4L2_FIELD_SEQ_BT:
607 cx23885_risc_buffer(dev->pci, &buf->risc,
608 dma->sglist,
609 buf->bpl * (buf->vb.height >> 1), 0,
610 buf->bpl, 0,
611 buf->vb.height >> 1);
612 break;
613 default:
614 BUG();
615 }
616 }
617 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
618 buf, buf->vb.i,
619 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
620 (unsigned long)buf->risc.dma);
621
622 buf->vb.state = VIDEOBUF_PREPARED;
623 return 0;
624
625 fail:
626 cx23885_free_buffer(q, buf);
627 return rc;
628}
629
630static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
631{
632 struct cx23885_buffer *buf = container_of(vb,
633 struct cx23885_buffer, vb);
634 struct cx23885_buffer *prev;
635 struct cx23885_fh *fh = vq->priv_data;
636 struct cx23885_dev *dev = fh->dev;
637 struct cx23885_dmaqueue *q = &dev->vidq;
638
639 /* add jump to stopper */
640 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
641 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
642 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
643
644 if (!list_empty(&q->queued)) {
645 list_add_tail(&buf->vb.queue, &q->queued);
646 buf->vb.state = VIDEOBUF_QUEUED;
647 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
648 buf, buf->vb.i);
649
650 } else if (list_empty(&q->active)) {
651 list_add_tail(&buf->vb.queue, &q->active);
652 cx23885_start_video_dma(dev, q, buf);
653 buf->vb.state = VIDEOBUF_ACTIVE;
654 buf->count = q->count++;
655 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
656 dprintk(2, "[%p/%d] buffer_queue - first active\n",
657 buf, buf->vb.i);
658
659 } else {
660 prev = list_entry(q->active.prev, struct cx23885_buffer,
661 vb.queue);
662 if (prev->vb.width == buf->vb.width &&
663 prev->vb.height == buf->vb.height &&
664 prev->fmt == buf->fmt) {
665 list_add_tail(&buf->vb.queue, &q->active);
666 buf->vb.state = VIDEOBUF_ACTIVE;
667 buf->count = q->count++;
668 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
669 /* 64 bit bits 63-32 */
670 prev->risc.jmp[2] = cpu_to_le32(0);
671 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
672 buf, buf->vb.i);
673
674 } else {
675 list_add_tail(&buf->vb.queue, &q->queued);
676 buf->vb.state = VIDEOBUF_QUEUED;
677 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
678 buf, buf->vb.i);
679 }
680 }
681}
682
683static void buffer_release(struct videobuf_queue *q,
684 struct videobuf_buffer *vb)
685{
686 struct cx23885_buffer *buf = container_of(vb,
687 struct cx23885_buffer, vb);
688
689 cx23885_free_buffer(q, buf);
690}
691
692static struct videobuf_queue_ops cx23885_video_qops = {
693 .buf_setup = buffer_setup,
694 .buf_prepare = buffer_prepare,
695 .buf_queue = buffer_queue,
696 .buf_release = buffer_release,
697};
698
699static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
700{
701 switch (fh->type) {
702 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
703 return &fh->vidq;
704 case V4L2_BUF_TYPE_VBI_CAPTURE:
705 return &fh->vbiq;
706 default:
707 BUG();
708 return NULL;
709 }
710}
711
712static int get_resource(struct cx23885_fh *fh)
713{
714 switch (fh->type) {
715 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
716 return RESOURCE_VIDEO;
717 case V4L2_BUF_TYPE_VBI_CAPTURE:
718 return RESOURCE_VBI;
719 default:
720 BUG();
721 return 0;
722 }
723}
724
725static int video_open(struct inode *inode, struct file *file)
726{
727 int minor = iminor(inode);
728 struct cx23885_dev *h, *dev = NULL;
729 struct cx23885_fh *fh;
730 struct list_head *list;
731 enum v4l2_buf_type type = 0;
732 int radio = 0;
733
Hans Verkuild56dc612008-07-30 08:43:36 -0300734 lock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300735 list_for_each(list, &cx23885_devlist) {
736 h = list_entry(list, struct cx23885_dev, devlist);
737 if (h->video_dev->minor == minor) {
738 dev = h;
739 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
740 }
741 if (h->vbi_dev &&
742 h->vbi_dev->minor == minor) {
743 dev = h;
744 type = V4L2_BUF_TYPE_VBI_CAPTURE;
745 }
746 if (h->radio_dev &&
747 h->radio_dev->minor == minor) {
748 radio = 1;
749 dev = h;
750 }
751 }
Hans Verkuild56dc612008-07-30 08:43:36 -0300752 if (NULL == dev) {
753 unlock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300754 return -ENODEV;
Hans Verkuild56dc612008-07-30 08:43:36 -0300755 }
Steven Tothe47f30b2008-01-10 04:25:59 -0300756
757 dprintk(1, "open minor=%d radio=%d type=%s\n",
758 minor, radio, v4l2_type_names[type]);
759
760 /* allocate + initialize per filehandle data */
761 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Hans Verkuild56dc612008-07-30 08:43:36 -0300762 if (NULL == fh) {
763 unlock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300764 return -ENOMEM;
Hans Verkuild56dc612008-07-30 08:43:36 -0300765 }
Steven Tothe47f30b2008-01-10 04:25:59 -0300766 file->private_data = fh;
767 fh->dev = dev;
768 fh->radio = radio;
769 fh->type = type;
770 fh->width = 320;
771 fh->height = 240;
772 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
773
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300774 videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
775 &dev->pci->dev, &dev->slock,
Steven Tothe47f30b2008-01-10 04:25:59 -0300776 V4L2_BUF_TYPE_VIDEO_CAPTURE,
777 V4L2_FIELD_INTERLACED,
778 sizeof(struct cx23885_buffer),
779 fh);
780
781 dprintk(1, "post videobuf_queue_init()\n");
782
Hans Verkuild56dc612008-07-30 08:43:36 -0300783 unlock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300784
785 return 0;
786}
787
788static ssize_t video_read(struct file *file, char __user *data,
789 size_t count, loff_t *ppos)
790{
791 struct cx23885_fh *fh = file->private_data;
792
793 switch (fh->type) {
794 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
795 if (res_locked(fh->dev, RESOURCE_VIDEO))
796 return -EBUSY;
797 return videobuf_read_one(&fh->vidq, data, count, ppos,
798 file->f_flags & O_NONBLOCK);
799 case V4L2_BUF_TYPE_VBI_CAPTURE:
800 if (!res_get(fh->dev, fh, RESOURCE_VBI))
801 return -EBUSY;
802 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
803 file->f_flags & O_NONBLOCK);
804 default:
805 BUG();
806 return 0;
807 }
808}
809
810static unsigned int video_poll(struct file *file,
811 struct poll_table_struct *wait)
812{
813 struct cx23885_fh *fh = file->private_data;
814 struct cx23885_buffer *buf;
815
816 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
817 if (!res_get(fh->dev, fh, RESOURCE_VBI))
818 return POLLERR;
819 return videobuf_poll_stream(file, &fh->vbiq, wait);
820 }
821
822 if (res_check(fh, RESOURCE_VIDEO)) {
823 /* streaming capture */
824 if (list_empty(&fh->vidq.stream))
825 return POLLERR;
826 buf = list_entry(fh->vidq.stream.next,
827 struct cx23885_buffer, vb.stream);
828 } else {
829 /* read() capture */
830 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
831 if (NULL == buf)
832 return POLLERR;
833 }
834 poll_wait(file, &buf->vb.done, wait);
835 if (buf->vb.state == VIDEOBUF_DONE ||
836 buf->vb.state == VIDEOBUF_ERROR)
837 return POLLIN|POLLRDNORM;
838 return 0;
839}
840
841static int video_release(struct inode *inode, struct file *file)
842{
843 struct cx23885_fh *fh = file->private_data;
844 struct cx23885_dev *dev = fh->dev;
845
846 /* turn off overlay */
847 if (res_check(fh, RESOURCE_OVERLAY)) {
848 /* FIXME */
849 res_free(dev, fh, RESOURCE_OVERLAY);
850 }
851
852 /* stop video capture */
853 if (res_check(fh, RESOURCE_VIDEO)) {
854 videobuf_queue_cancel(&fh->vidq);
855 res_free(dev, fh, RESOURCE_VIDEO);
856 }
857 if (fh->vidq.read_buf) {
858 buffer_release(&fh->vidq, fh->vidq.read_buf);
859 kfree(fh->vidq.read_buf);
860 }
861
862 /* stop vbi capture */
863 if (res_check(fh, RESOURCE_VBI)) {
864 if (fh->vbiq.streaming)
865 videobuf_streamoff(&fh->vbiq);
866 if (fh->vbiq.reading)
867 videobuf_read_stop(&fh->vbiq);
868 res_free(dev, fh, RESOURCE_VBI);
869 }
870
871 videobuf_mmap_free(&fh->vidq);
872 file->private_data = NULL;
873 kfree(fh);
874
Steven Totha19602f22008-01-10 11:43:18 -0300875 /* We are not putting the tuner to sleep here on exit, because
876 * we want to use the mpeg encoder in another session to capture
877 * tuner video. Closing this will result in no video to the encoder.
878 */
Steven Tothe47f30b2008-01-10 04:25:59 -0300879
880 return 0;
881}
882
883static int video_mmap(struct file *file, struct vm_area_struct *vma)
884{
885 struct cx23885_fh *fh = file->private_data;
886
887 return videobuf_mmap_mapper(get_queue(fh), vma);
888}
889
890/* ------------------------------------------------------------------ */
891/* VIDEO CTRL IOCTLS */
892
893int cx23885_get_control(struct cx23885_dev *dev, struct v4l2_control *ctl)
894{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300895 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300896 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_G_CTRL, ctl);
897 return 0;
898}
899EXPORT_SYMBOL(cx23885_get_control);
900
901int cx23885_set_control(struct cx23885_dev *dev, struct v4l2_control *ctl)
902{
903 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
Harvey Harrison22b4e642008-04-08 23:20:00 -0300904 " (disabled - no action)\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300905 return 0;
906}
907EXPORT_SYMBOL(cx23885_set_control);
908
909static void init_controls(struct cx23885_dev *dev)
910{
911 struct v4l2_control ctrl;
912 int i;
913
914 for (i = 0; i < CX23885_CTLS; i++) {
915 ctrl.id = cx23885_ctls[i].v.id;
916 ctrl.value = cx23885_ctls[i].v.default_value;
917
918 cx23885_set_control(dev, &ctrl);
919 }
920}
921
922/* ------------------------------------------------------------------ */
923/* VIDEO IOCTLS */
924
Hans Verkuil78b526a2008-05-28 12:16:41 -0300925static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300926 struct v4l2_format *f)
927{
928 struct cx23885_fh *fh = priv;
929
930 f->fmt.pix.width = fh->width;
931 f->fmt.pix.height = fh->height;
932 f->fmt.pix.field = fh->vidq.field;
933 f->fmt.pix.pixelformat = fh->fmt->fourcc;
934 f->fmt.pix.bytesperline =
935 (f->fmt.pix.width * fh->fmt->depth) >> 3;
936 f->fmt.pix.sizeimage =
937 f->fmt.pix.height * f->fmt.pix.bytesperline;
938
939 return 0;
940}
941
Hans Verkuil78b526a2008-05-28 12:16:41 -0300942static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300943 struct v4l2_format *f)
944{
945 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
946 struct cx23885_fmt *fmt;
947 enum v4l2_field field;
948 unsigned int maxw, maxh;
949
950 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
951 if (NULL == fmt)
952 return -EINVAL;
953
954 field = f->fmt.pix.field;
955 maxw = norm_maxw(dev->tvnorm);
956 maxh = norm_maxh(dev->tvnorm);
957
958 if (V4L2_FIELD_ANY == field) {
959 field = (f->fmt.pix.height > maxh/2)
960 ? V4L2_FIELD_INTERLACED
961 : V4L2_FIELD_BOTTOM;
962 }
963
964 switch (field) {
965 case V4L2_FIELD_TOP:
966 case V4L2_FIELD_BOTTOM:
967 maxh = maxh / 2;
968 break;
969 case V4L2_FIELD_INTERLACED:
970 break;
971 default:
972 return -EINVAL;
973 }
974
975 f->fmt.pix.field = field;
976 if (f->fmt.pix.height < 32)
977 f->fmt.pix.height = 32;
978 if (f->fmt.pix.height > maxh)
979 f->fmt.pix.height = maxh;
980 if (f->fmt.pix.width < 48)
981 f->fmt.pix.width = 48;
982 if (f->fmt.pix.width > maxw)
983 f->fmt.pix.width = maxw;
984 f->fmt.pix.width &= ~0x03;
985 f->fmt.pix.bytesperline =
986 (f->fmt.pix.width * fmt->depth) >> 3;
987 f->fmt.pix.sizeimage =
988 f->fmt.pix.height * f->fmt.pix.bytesperline;
989
990 return 0;
991}
992
Hans Verkuil78b526a2008-05-28 12:16:41 -0300993static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300994 struct v4l2_format *f)
995{
996 struct cx23885_fh *fh = priv;
997 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
998 int err;
999
Harvey Harrison22b4e642008-04-08 23:20:00 -03001000 dprintk(2, "%s()\n", __func__);
Hans Verkuil78b526a2008-05-28 12:16:41 -03001001 err = vidioc_try_fmt_vid_cap(file, priv, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001002
1003 if (0 != err)
1004 return err;
1005 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1006 fh->width = f->fmt.pix.width;
1007 fh->height = f->fmt.pix.height;
1008 fh->vidq.field = f->fmt.pix.field;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001009 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -03001010 fh->width, fh->height, fh->vidq.field);
1011 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_FMT, f);
1012 return 0;
1013}
1014
1015static int vidioc_querycap(struct file *file, void *priv,
1016 struct v4l2_capability *cap)
1017{
1018 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1019
1020 strcpy(cap->driver, "cx23885");
1021 strlcpy(cap->card, cx23885_boards[dev->board].name,
1022 sizeof(cap->card));
1023 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1024 cap->version = CX23885_VERSION_CODE;
1025 cap->capabilities =
1026 V4L2_CAP_VIDEO_CAPTURE |
1027 V4L2_CAP_READWRITE |
1028 V4L2_CAP_STREAMING |
1029 V4L2_CAP_VBI_CAPTURE;
1030 if (UNSET != dev->tuner_type)
1031 cap->capabilities |= V4L2_CAP_TUNER;
1032 return 0;
1033}
1034
Hans Verkuil78b526a2008-05-28 12:16:41 -03001035static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001036 struct v4l2_fmtdesc *f)
1037{
1038 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1039 return -EINVAL;
1040
1041 strlcpy(f->description, formats[f->index].name,
1042 sizeof(f->description));
1043 f->pixelformat = formats[f->index].fourcc;
1044
1045 return 0;
1046}
1047
1048#ifdef CONFIG_VIDEO_V4L1_COMPAT
1049static int vidiocgmbuf(struct file *file, void *priv,
1050 struct video_mbuf *mbuf)
1051{
1052 struct cx23885_fh *fh = priv;
1053 struct videobuf_queue *q;
1054 struct v4l2_requestbuffers req;
1055 unsigned int i;
1056 int err;
1057
1058 q = get_queue(fh);
1059 memset(&req, 0, sizeof(req));
1060 req.type = q->type;
1061 req.count = 8;
1062 req.memory = V4L2_MEMORY_MMAP;
1063 err = videobuf_reqbufs(q, &req);
1064 if (err < 0)
1065 return err;
1066
1067 mbuf->frames = req.count;
1068 mbuf->size = 0;
1069 for (i = 0; i < mbuf->frames; i++) {
1070 mbuf->offsets[i] = q->bufs[i]->boff;
1071 mbuf->size += q->bufs[i]->bsize;
1072 }
1073 return 0;
1074}
1075#endif
1076
1077static int vidioc_reqbufs(struct file *file, void *priv,
1078 struct v4l2_requestbuffers *p)
1079{
1080 struct cx23885_fh *fh = priv;
1081 return (videobuf_reqbufs(get_queue(fh), p));
1082}
1083
1084static int vidioc_querybuf(struct file *file, void *priv,
1085 struct v4l2_buffer *p)
1086{
1087 struct cx23885_fh *fh = priv;
1088 return (videobuf_querybuf(get_queue(fh), p));
1089}
1090
1091static int vidioc_qbuf(struct file *file, void *priv,
1092 struct v4l2_buffer *p)
1093{
1094 struct cx23885_fh *fh = priv;
1095 return (videobuf_qbuf(get_queue(fh), p));
1096}
1097
1098static int vidioc_dqbuf(struct file *file, void *priv,
1099 struct v4l2_buffer *p)
1100{
1101 struct cx23885_fh *fh = priv;
1102 return (videobuf_dqbuf(get_queue(fh), p,
1103 file->f_flags & O_NONBLOCK));
1104}
1105
1106static int vidioc_streamon(struct file *file, void *priv,
1107 enum v4l2_buf_type i)
1108{
1109 struct cx23885_fh *fh = priv;
1110 struct cx23885_dev *dev = fh->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001111 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001112
1113 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1114 return -EINVAL;
1115 if (unlikely(i != fh->type))
1116 return -EINVAL;
1117
1118 if (unlikely(!res_get(dev, fh, get_resource(fh))))
1119 return -EBUSY;
1120 return videobuf_streamon(get_queue(fh));
1121}
1122
1123static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1124{
1125 struct cx23885_fh *fh = priv;
1126 struct cx23885_dev *dev = fh->dev;
1127 int err, res;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001128 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001129
1130 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1131 return -EINVAL;
1132 if (i != fh->type)
1133 return -EINVAL;
1134
1135 res = get_resource(fh);
1136 err = videobuf_streamoff(get_queue(fh));
1137 if (err < 0)
1138 return err;
1139 res_free(dev, fh, res);
1140 return 0;
1141}
1142
1143static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1144{
1145 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001146 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001147
1148 mutex_lock(&dev->lock);
1149 cx23885_set_tvnorm(dev, *tvnorms);
1150 mutex_unlock(&dev->lock);
1151
1152 return 0;
1153}
1154
1155int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
1156{
1157 static const char *iname[] = {
1158 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1159 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1160 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1161 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1162 [CX23885_VMUX_SVIDEO] = "S-Video",
1163 [CX23885_VMUX_TELEVISION] = "Television",
1164 [CX23885_VMUX_CABLE] = "Cable TV",
1165 [CX23885_VMUX_DVB] = "DVB",
1166 [CX23885_VMUX_DEBUG] = "for debug only",
1167 };
1168 unsigned int n;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001169 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001170
1171 n = i->index;
1172 if (n >= 4)
1173 return -EINVAL;
1174
1175 if (0 == INPUT(n)->type)
1176 return -EINVAL;
1177
1178 memset(i, 0, sizeof(*i));
1179 i->index = n;
1180 i->type = V4L2_INPUT_TYPE_CAMERA;
1181 strcpy(i->name, iname[INPUT(n)->type]);
1182 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
1183 (CX23885_VMUX_CABLE == INPUT(n)->type))
1184 i->type = V4L2_INPUT_TYPE_TUNER;
1185 i->std = CX23885_NORMS;
1186 return 0;
1187}
1188EXPORT_SYMBOL(cx23885_enum_input);
1189
1190static int vidioc_enum_input(struct file *file, void *priv,
1191 struct v4l2_input *i)
1192{
1193 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001194 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001195 return cx23885_enum_input(dev, i);
1196}
1197
1198static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1199{
1200 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1201
1202 *i = dev->input;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001203 dprintk(1, "%s() returns %d\n", __func__, *i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001204 return 0;
1205}
1206
1207static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1208{
1209 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1210
Harvey Harrison22b4e642008-04-08 23:20:00 -03001211 dprintk(1, "%s(%d)\n", __func__, i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001212
1213 if (i >= 4) {
Harvey Harrison22b4e642008-04-08 23:20:00 -03001214 dprintk(1, "%s() -EINVAL\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001215 return -EINVAL;
1216 }
1217
1218 mutex_lock(&dev->lock);
1219 cx23885_video_mux(dev, i);
1220 mutex_unlock(&dev->lock);
1221 return 0;
1222}
1223
1224static int vidioc_queryctrl(struct file *file, void *priv,
1225 struct v4l2_queryctrl *qctrl)
1226{
1227 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1228 if (unlikely(qctrl->id == 0))
1229 return -EINVAL;
1230 return cx23885_ctrl_query(qctrl);
1231}
1232
1233static int vidioc_g_ctrl(struct file *file, void *priv,
1234 struct v4l2_control *ctl)
1235{
1236 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1237
1238 return cx23885_get_control(dev, ctl);
1239}
1240
1241static int vidioc_s_ctrl(struct file *file, void *priv,
1242 struct v4l2_control *ctl)
1243{
1244 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1245
1246 return cx23885_set_control(dev, ctl);
1247}
1248
1249static int vidioc_g_tuner(struct file *file, void *priv,
1250 struct v4l2_tuner *t)
1251{
1252 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1253
1254 if (unlikely(UNSET == dev->tuner_type))
1255 return -EINVAL;
1256 if (0 != t->index)
1257 return -EINVAL;
1258
1259 strcpy(t->name, "Television");
1260 t->type = V4L2_TUNER_ANALOG_TV;
1261 t->capability = V4L2_TUNER_CAP_NORM;
1262 t->rangehigh = 0xffffffffUL;
1263 t->signal = 0xffff ; /* LOCKED */
1264 return 0;
1265}
1266
1267static int vidioc_s_tuner(struct file *file, void *priv,
1268 struct v4l2_tuner *t)
1269{
1270 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1271
1272 if (UNSET == dev->tuner_type)
1273 return -EINVAL;
1274 if (0 != t->index)
1275 return -EINVAL;
1276 return 0;
1277}
1278
1279static int vidioc_g_frequency(struct file *file, void *priv,
1280 struct v4l2_frequency *f)
1281{
1282 struct cx23885_fh *fh = priv;
1283 struct cx23885_dev *dev = fh->dev;
1284
1285 if (unlikely(UNSET == dev->tuner_type))
1286 return -EINVAL;
1287
1288 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1289 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1290 f->frequency = dev->freq;
1291
1292 cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_G_FREQUENCY, f);
1293
1294 return 0;
1295}
1296
1297int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
1298{
1299 if (unlikely(UNSET == dev->tuner_type))
1300 return -EINVAL;
1301 if (unlikely(f->tuner != 0))
1302 return -EINVAL;
1303
1304 mutex_lock(&dev->lock);
1305 dev->freq = f->frequency;
1306
1307 cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, f);
1308
1309 /* When changing channels it is required to reset TVAUDIO */
1310 msleep(10);
1311
1312 mutex_unlock(&dev->lock);
1313
1314 return 0;
1315}
1316EXPORT_SYMBOL(cx23885_set_freq);
1317
1318static int vidioc_s_frequency(struct file *file, void *priv,
1319 struct v4l2_frequency *f)
1320{
1321 struct cx23885_fh *fh = priv;
1322 struct cx23885_dev *dev = fh->dev;
1323
1324 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1325 return -EINVAL;
1326 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1327 return -EINVAL;
1328
1329 return
1330 cx23885_set_freq(dev, f);
1331}
1332
1333#ifdef CONFIG_VIDEO_ADV_DEBUG
1334static int vidioc_g_register(struct file *file, void *fh,
1335 struct v4l2_register *reg)
1336{
1337 struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1338
Steven Totha19602f22008-01-10 11:43:18 -03001339 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1340 return -EINVAL;
1341
Steven Tothe47f30b2008-01-10 04:25:59 -03001342 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_G_REGISTER, reg);
1343
1344 return 0;
1345}
1346
1347static int vidioc_s_register(struct file *file, void *fh,
1348 struct v4l2_register *reg)
1349{
1350 struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1351
Steven Totha19602f22008-01-10 11:43:18 -03001352 if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
1353 return -EINVAL;
1354
Steven Tothe47f30b2008-01-10 04:25:59 -03001355 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_S_REGISTER, reg);
Steven Totha19602f22008-01-10 11:43:18 -03001356
Steven Tothe47f30b2008-01-10 04:25:59 -03001357 return 0;
1358}
1359#endif
1360
1361/* ----------------------------------------------------------- */
Steven Tothe47f30b2008-01-10 04:25:59 -03001362
1363static void cx23885_vid_timeout(unsigned long data)
1364{
1365 struct cx23885_dev *dev = (struct cx23885_dev *)data;
1366 struct cx23885_dmaqueue *q = &dev->vidq;
1367 struct cx23885_buffer *buf;
1368 unsigned long flags;
1369
1370 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1371
1372 cx_clear(VID_A_DMA_CTL, 0x11);
1373
1374 spin_lock_irqsave(&dev->slock, flags);
1375 while (!list_empty(&q->active)) {
1376 buf = list_entry(q->active.next,
1377 struct cx23885_buffer, vb.queue);
1378 list_del(&buf->vb.queue);
1379 buf->vb.state = VIDEOBUF_ERROR;
1380 wake_up(&buf->vb.done);
1381 printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
1382 dev->name, buf, buf->vb.i,
1383 (unsigned long)buf->risc.dma);
1384 }
1385 cx23885_restart_video_queue(dev, q);
1386 spin_unlock_irqrestore(&dev->slock, flags);
1387}
1388
1389int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1390{
1391 u32 mask, count;
1392 int handled = 0;
1393
1394 mask = cx_read(VID_A_INT_MSK);
1395 if (0 == (status & mask))
1396 return handled;
1397 cx_write(VID_A_INT_STAT, status);
1398
Harvey Harrison22b4e642008-04-08 23:20:00 -03001399 dprintk(2, "%s() status = 0x%08x\n", __func__, status);
Steven Tothe47f30b2008-01-10 04:25:59 -03001400 /* risc op code error */
1401 if (status & (1 << 16)) {
1402 printk(KERN_WARNING "%s/0: video risc op code error\n",
1403 dev->name);
1404 cx_clear(VID_A_DMA_CTL, 0x11);
1405 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1406 }
1407
1408 /* risc1 y */
1409 if (status & 0x01) {
1410 spin_lock(&dev->slock);
1411 count = cx_read(VID_A_GPCNT);
1412 cx23885_video_wakeup(dev, &dev->vidq, count);
1413 spin_unlock(&dev->slock);
1414 handled++;
1415 }
1416 /* risc2 y */
1417 if (status & 0x10) {
1418 dprintk(2, "stopper video\n");
1419 spin_lock(&dev->slock);
1420 cx23885_restart_video_queue(dev, &dev->vidq);
1421 spin_unlock(&dev->slock);
1422 handled++;
1423 }
1424
1425 return handled;
1426}
1427
Steven Tothe47f30b2008-01-10 04:25:59 -03001428/* ----------------------------------------------------------- */
1429/* exported stuff */
1430
1431static const struct file_operations video_fops = {
1432 .owner = THIS_MODULE,
1433 .open = video_open,
1434 .release = video_release,
1435 .read = video_read,
1436 .poll = video_poll,
1437 .mmap = video_mmap,
1438 .ioctl = video_ioctl2,
1439 .compat_ioctl = v4l_compat_ioctl32,
1440 .llseek = no_llseek,
1441};
1442
Hans Verkuila3998102008-07-21 02:57:38 -03001443static const struct v4l2_ioctl_ops video_ioctl_ops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001444 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001445 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1446 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1447 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1448 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1449 .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
1450 .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
1451 .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
Steven Tothe47f30b2008-01-10 04:25:59 -03001452 .vidioc_reqbufs = vidioc_reqbufs,
1453 .vidioc_querybuf = vidioc_querybuf,
1454 .vidioc_qbuf = vidioc_qbuf,
1455 .vidioc_dqbuf = vidioc_dqbuf,
1456 .vidioc_s_std = vidioc_s_std,
1457 .vidioc_enum_input = vidioc_enum_input,
1458 .vidioc_g_input = vidioc_g_input,
1459 .vidioc_s_input = vidioc_s_input,
1460 .vidioc_queryctrl = vidioc_queryctrl,
1461 .vidioc_g_ctrl = vidioc_g_ctrl,
1462 .vidioc_s_ctrl = vidioc_s_ctrl,
1463 .vidioc_streamon = vidioc_streamon,
1464 .vidioc_streamoff = vidioc_streamoff,
1465#ifdef CONFIG_VIDEO_V4L1_COMPAT
1466 .vidiocgmbuf = vidiocgmbuf,
1467#endif
1468 .vidioc_g_tuner = vidioc_g_tuner,
1469 .vidioc_s_tuner = vidioc_s_tuner,
1470 .vidioc_g_frequency = vidioc_g_frequency,
1471 .vidioc_s_frequency = vidioc_s_frequency,
1472#ifdef CONFIG_VIDEO_ADV_DEBUG
1473 .vidioc_g_register = vidioc_g_register,
1474 .vidioc_s_register = vidioc_s_register,
1475#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001476};
1477
1478static struct video_device cx23885_vbi_template;
1479static struct video_device cx23885_video_template = {
1480 .name = "cx23885-video",
Hans Verkuila3998102008-07-21 02:57:38 -03001481 .fops = &video_fops,
1482 .minor = -1,
1483 .ioctl_ops = &video_ioctl_ops,
Steven Tothe47f30b2008-01-10 04:25:59 -03001484 .tvnorms = CX23885_NORMS,
1485 .current_norm = V4L2_STD_NTSC_M,
1486};
1487
1488static const struct file_operations radio_fops = {
1489 .owner = THIS_MODULE,
1490 .open = video_open,
1491 .release = video_release,
1492 .ioctl = video_ioctl2,
1493 .compat_ioctl = v4l_compat_ioctl32,
1494 .llseek = no_llseek,
1495};
1496
1497
1498void cx23885_video_unregister(struct cx23885_dev *dev)
1499{
Harvey Harrison22b4e642008-04-08 23:20:00 -03001500 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001501 cx_clear(PCI_INT_MSK, 1);
1502
1503 if (dev->video_dev) {
1504 if (-1 != dev->video_dev->minor)
1505 video_unregister_device(dev->video_dev);
1506 else
1507 video_device_release(dev->video_dev);
1508 dev->video_dev = NULL;
1509
1510 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1511 }
1512}
1513
1514int cx23885_video_register(struct cx23885_dev *dev)
1515{
1516 int err;
1517
Harvey Harrison22b4e642008-04-08 23:20:00 -03001518 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001519 spin_lock_init(&dev->slock);
1520
1521 /* Initialize VBI template */
1522 memcpy(&cx23885_vbi_template, &cx23885_video_template,
1523 sizeof(cx23885_vbi_template));
1524 strcpy(cx23885_vbi_template.name, "cx23885-vbi");
Steven Tothe47f30b2008-01-10 04:25:59 -03001525
1526 dev->tvnorm = cx23885_video_template.current_norm;
1527
1528 /* init video dma queues */
1529 INIT_LIST_HEAD(&dev->vidq.active);
1530 INIT_LIST_HEAD(&dev->vidq.queued);
1531 dev->vidq.timeout.function = cx23885_vid_timeout;
1532 dev->vidq.timeout.data = (unsigned long)dev;
1533 init_timer(&dev->vidq.timeout);
1534 cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1535 VID_A_DMA_CTL, 0x11, 0x00);
1536
Steven Totha19602f22008-01-10 11:43:18 -03001537 /* Don't enable VBI yet */
Steven Tothe47f30b2008-01-10 04:25:59 -03001538 cx_set(PCI_INT_MSK, 1);
1539
1540
1541 /* register v4l devices */
1542 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1543 &cx23885_video_template, "video");
1544 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1545 video_nr[dev->nr]);
1546 if (err < 0) {
1547 printk(KERN_INFO "%s: can't register video device\n",
1548 dev->name);
1549 goto fail_unreg;
1550 }
1551 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
1552 dev->name, dev->video_dev->minor & 0x1f);
1553 /* initial device configuration */
1554 mutex_lock(&dev->lock);
1555 cx23885_set_tvnorm(dev, dev->tvnorm);
1556 init_controls(dev);
1557 cx23885_video_mux(dev, 0);
1558 mutex_unlock(&dev->lock);
1559
Steven Tothe47f30b2008-01-10 04:25:59 -03001560 return 0;
1561
1562fail_unreg:
1563 cx23885_video_unregister(dev);
1564 return err;
1565}
1566