blob: c2ed2505b72558cc77f43d4c3f2fec4a616ee8ca [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
Hans Verkuil2ba58892009-02-13 10:57:48 -0300247/* Must be sorted from low to high control ID! */
Hans Verkuild45b9b82008-09-04 03:33:43 -0300248static const u32 cx23885_user_ctrls[] = {
Steven Tothe47f30b2008-01-10 04:25:59 -0300249 V4L2_CID_USER_CLASS,
250 V4L2_CID_BRIGHTNESS,
251 V4L2_CID_CONTRAST,
252 V4L2_CID_SATURATION,
253 V4L2_CID_HUE,
254 V4L2_CID_AUDIO_VOLUME,
255 V4L2_CID_AUDIO_MUTE,
256 0
257};
Steven Tothe47f30b2008-01-10 04:25:59 -0300258
259static const u32 *ctrl_classes[] = {
260 cx23885_user_ctrls,
261 NULL
262};
263
Hans Verkuild45b9b82008-09-04 03:33:43 -0300264static void cx23885_video_wakeup(struct cx23885_dev *dev,
Steven Tothe47f30b2008-01-10 04:25:59 -0300265 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 }
Steven Toth9c8ced52008-10-16 20:18:44 -0300289 if (list_empty(&q->active))
Steven Tothe47f30b2008-01-10 04:25:59 -0300290 del_timer(&q->timeout);
Steven Toth9c8ced52008-10-16 20:18:44 -0300291 else
Steven Tothe47f30b2008-01-10 04:25:59 -0300292 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
Steven Tothe47f30b2008-01-10 04:25:59 -0300293 if (bc != 1)
Steven Toth21a78092008-01-10 04:38:59 -0300294 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300295 __func__, bc);
Steven Tothe47f30b2008-01-10 04:25:59 -0300296}
297
Hans Verkuild45b9b82008-09-04 03:33:43 -0300298static int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
Steven Tothe47f30b2008-01-10 04:25:59 -0300299{
300 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300301 __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -0300302 (unsigned int)norm,
303 v4l2_norm_to_name(norm));
304
305 dev->tvnorm = norm;
306
Steven Tothe47f30b2008-01-10 04:25:59 -0300307 /* Tell the analog tuner/demods */
308 cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_STD, &norm);
309
310 /* Tell the internal A/V decoder */
311 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_STD, &norm);
312
313 return 0;
314}
315
Hans Verkuild45b9b82008-09-04 03:33:43 -0300316static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
Steven Tothe47f30b2008-01-10 04:25:59 -0300317 struct pci_dev *pci,
318 struct video_device *template,
319 char *type)
320{
321 struct video_device *vfd;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300322 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300323
324 vfd = video_device_alloc();
325 if (NULL == vfd)
326 return NULL;
327 *vfd = *template;
328 vfd->minor = -1;
Hans Verkuil5e85e732008-07-20 06:31:39 -0300329 vfd->parent = &pci->dev;
Steven Tothe47f30b2008-01-10 04:25:59 -0300330 vfd->release = video_device_release;
331 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
332 dev->name, type, cx23885_boards[dev->board].name);
333 return vfd;
334}
335
Hans Verkuild45b9b82008-09-04 03:33:43 -0300336static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300337{
338 int i;
339
340 if (qctrl->id < V4L2_CID_BASE ||
341 qctrl->id >= V4L2_CID_LASTP1)
342 return -EINVAL;
343 for (i = 0; i < CX23885_CTLS; i++)
344 if (cx23885_ctls[i].v.id == qctrl->id)
345 break;
346 if (i == CX23885_CTLS) {
347 *qctrl = no_ctl;
348 return 0;
349 }
350 *qctrl = cx23885_ctls[i].v;
351 return 0;
352}
Steven Tothe47f30b2008-01-10 04:25:59 -0300353
354/* ------------------------------------------------------------------- */
355/* resource management */
356
357static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
358 unsigned int bit)
359{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300360 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300361 if (fh->resources & bit)
362 /* have it already allocated */
363 return 1;
364
365 /* is it free? */
366 mutex_lock(&dev->lock);
367 if (dev->resources & bit) {
368 /* no, someone else uses it */
369 mutex_unlock(&dev->lock);
370 return 0;
371 }
372 /* it's free, grab it */
373 fh->resources |= bit;
374 dev->resources |= bit;
375 dprintk(1, "res: get %d\n", bit);
376 mutex_unlock(&dev->lock);
377 return 1;
378}
379
380static int res_check(struct cx23885_fh *fh, unsigned int bit)
381{
Steven Toth9c8ced52008-10-16 20:18:44 -0300382 return fh->resources & bit;
Steven Tothe47f30b2008-01-10 04:25:59 -0300383}
384
385static int res_locked(struct cx23885_dev *dev, unsigned int bit)
386{
Steven Toth9c8ced52008-10-16 20:18:44 -0300387 return dev->resources & bit;
Steven Tothe47f30b2008-01-10 04:25:59 -0300388}
389
390static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
391 unsigned int bits)
392{
393 BUG_ON((fh->resources & bits) != bits);
Harvey Harrison22b4e642008-04-08 23:20:00 -0300394 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300395
396 mutex_lock(&dev->lock);
397 fh->resources &= ~bits;
398 dev->resources &= ~bits;
399 dprintk(1, "res: put %d\n", bits);
400 mutex_unlock(&dev->lock);
401}
402
Hans Verkuild45b9b82008-09-04 03:33:43 -0300403static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
Steven Tothe47f30b2008-01-10 04:25:59 -0300404{
405 struct v4l2_routing route;
406 memset(&route, 0, sizeof(route));
407
408 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 -0300409 __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -0300410 input, INPUT(input)->vmux,
411 INPUT(input)->gpio0, INPUT(input)->gpio1,
412 INPUT(input)->gpio2, INPUT(input)->gpio3);
413 dev->input = input;
414
415 route.input = INPUT(input)->vmux;
416
417 /* Tell the internal A/V decoder */
418 cx23885_call_i2c_clients(&dev->i2c_bus[2],
419 VIDIOC_INT_S_VIDEO_ROUTING, &route);
420
421 return 0;
422}
Steven Tothe47f30b2008-01-10 04:25:59 -0300423
424/* ------------------------------------------------------------------ */
Hans Verkuild45b9b82008-09-04 03:33:43 -0300425static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
Steven Tothe47f30b2008-01-10 04:25:59 -0300426 unsigned int height, enum v4l2_field field)
427{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300428 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300429 return 0;
430}
431
432static int cx23885_start_video_dma(struct cx23885_dev *dev,
433 struct cx23885_dmaqueue *q,
434 struct cx23885_buffer *buf)
435{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300436 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300437
438 /* setup fifo + format */
439 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
440 buf->bpl, buf->risc.dma);
441 cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
442
443 /* reset counter */
444 cx_write(VID_A_GPCNT_CTL, 3);
445 q->count = 1;
446
447 /* enable irq */
448 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
449 cx_set(VID_A_INT_MSK, 0x000011);
450
451 /* start dma */
452 cx_set(DEV_CNTRL2, (1<<5));
453 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
454
455 return 0;
456}
457
Steven Tothe47f30b2008-01-10 04:25:59 -0300458
459static int cx23885_restart_video_queue(struct cx23885_dev *dev,
460 struct cx23885_dmaqueue *q)
461{
462 struct cx23885_buffer *buf, *prev;
463 struct list_head *item;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300464 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300465
466 if (!list_empty(&q->active)) {
467 buf = list_entry(q->active.next, struct cx23885_buffer,
468 vb.queue);
469 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
470 buf, buf->vb.i);
471 cx23885_start_video_dma(dev, q, buf);
472 list_for_each(item, &q->active) {
473 buf = list_entry(item, struct cx23885_buffer,
474 vb.queue);
475 buf->count = q->count++;
476 }
477 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
478 return 0;
479 }
480
481 prev = NULL;
482 for (;;) {
483 if (list_empty(&q->queued))
484 return 0;
485 buf = list_entry(q->queued.next, struct cx23885_buffer,
486 vb.queue);
487 if (NULL == prev) {
488 list_move_tail(&buf->vb.queue, &q->active);
489 cx23885_start_video_dma(dev, q, buf);
490 buf->vb.state = VIDEOBUF_ACTIVE;
491 buf->count = q->count++;
492 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
493 dprintk(2, "[%p/%d] restart_queue - first active\n",
494 buf, buf->vb.i);
495
496 } else if (prev->vb.width == buf->vb.width &&
497 prev->vb.height == buf->vb.height &&
498 prev->fmt == buf->fmt) {
499 list_move_tail(&buf->vb.queue, &q->active);
500 buf->vb.state = VIDEOBUF_ACTIVE;
501 buf->count = q->count++;
502 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
503 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
504 dprintk(2, "[%p/%d] restart_queue - move to active\n",
505 buf, buf->vb.i);
506 } else {
507 return 0;
508 }
509 prev = buf;
510 }
511}
512
513static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
514 unsigned int *size)
515{
516 struct cx23885_fh *fh = q->priv_data;
517
518 *size = fh->fmt->depth*fh->width*fh->height >> 3;
519 if (0 == *count)
520 *count = 32;
521 while (*size * *count > vid_limit * 1024 * 1024)
522 (*count)--;
523 return 0;
524}
525
526static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
527 enum v4l2_field field)
528{
529 struct cx23885_fh *fh = q->priv_data;
530 struct cx23885_dev *dev = fh->dev;
531 struct cx23885_buffer *buf =
532 container_of(vb, struct cx23885_buffer, vb);
533 int rc, init_buffer = 0;
534 u32 line0_offset, line1_offset;
535 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
536
537 BUG_ON(NULL == fh->fmt);
538 if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
539 fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
540 return -EINVAL;
541 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
542 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
543 return -EINVAL;
544
545 if (buf->fmt != fh->fmt ||
546 buf->vb.width != fh->width ||
547 buf->vb.height != fh->height ||
548 buf->vb.field != field) {
549 buf->fmt = fh->fmt;
550 buf->vb.width = fh->width;
551 buf->vb.height = fh->height;
552 buf->vb.field = field;
553 init_buffer = 1;
554 }
555
556 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
557 init_buffer = 1;
558 rc = videobuf_iolock(q, &buf->vb, NULL);
559 if (0 != rc)
560 goto fail;
561 }
562
563 if (init_buffer) {
564 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
565 switch (buf->vb.field) {
566 case V4L2_FIELD_TOP:
567 cx23885_risc_buffer(dev->pci, &buf->risc,
568 dma->sglist, 0, UNSET,
569 buf->bpl, 0, buf->vb.height);
570 break;
571 case V4L2_FIELD_BOTTOM:
572 cx23885_risc_buffer(dev->pci, &buf->risc,
573 dma->sglist, UNSET, 0,
574 buf->bpl, 0, buf->vb.height);
575 break;
576 case V4L2_FIELD_INTERLACED:
577 if (dev->tvnorm & V4L2_STD_NTSC) {
578 /* cx25840 transmits NTSC bottom field first */
579 dprintk(1, "%s() Creating NTSC risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300580 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300581 line0_offset = buf->bpl;
582 line1_offset = 0;
583 } else {
584 /* All other formats are top field first */
585 dprintk(1, "%s() Creating PAL/SECAM risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300586 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300587 line0_offset = 0;
588 line1_offset = buf->bpl;
589 }
590 cx23885_risc_buffer(dev->pci, &buf->risc,
591 dma->sglist, line0_offset,
592 line1_offset,
593 buf->bpl, buf->bpl,
594 buf->vb.height >> 1);
595 break;
596 case V4L2_FIELD_SEQ_TB:
597 cx23885_risc_buffer(dev->pci, &buf->risc,
598 dma->sglist,
599 0, buf->bpl * (buf->vb.height >> 1),
600 buf->bpl, 0,
601 buf->vb.height >> 1);
602 break;
603 case V4L2_FIELD_SEQ_BT:
604 cx23885_risc_buffer(dev->pci, &buf->risc,
605 dma->sglist,
606 buf->bpl * (buf->vb.height >> 1), 0,
607 buf->bpl, 0,
608 buf->vb.height >> 1);
609 break;
610 default:
611 BUG();
612 }
613 }
614 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
615 buf, buf->vb.i,
616 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
617 (unsigned long)buf->risc.dma);
618
619 buf->vb.state = VIDEOBUF_PREPARED;
620 return 0;
621
622 fail:
623 cx23885_free_buffer(q, buf);
624 return rc;
625}
626
627static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
628{
629 struct cx23885_buffer *buf = container_of(vb,
630 struct cx23885_buffer, vb);
631 struct cx23885_buffer *prev;
632 struct cx23885_fh *fh = vq->priv_data;
633 struct cx23885_dev *dev = fh->dev;
634 struct cx23885_dmaqueue *q = &dev->vidq;
635
636 /* add jump to stopper */
637 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
638 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
639 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
640
641 if (!list_empty(&q->queued)) {
642 list_add_tail(&buf->vb.queue, &q->queued);
643 buf->vb.state = VIDEOBUF_QUEUED;
644 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
645 buf, buf->vb.i);
646
647 } else if (list_empty(&q->active)) {
648 list_add_tail(&buf->vb.queue, &q->active);
649 cx23885_start_video_dma(dev, q, buf);
650 buf->vb.state = VIDEOBUF_ACTIVE;
651 buf->count = q->count++;
652 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
653 dprintk(2, "[%p/%d] buffer_queue - first active\n",
654 buf, buf->vb.i);
655
656 } else {
657 prev = list_entry(q->active.prev, struct cx23885_buffer,
658 vb.queue);
659 if (prev->vb.width == buf->vb.width &&
660 prev->vb.height == buf->vb.height &&
661 prev->fmt == buf->fmt) {
662 list_add_tail(&buf->vb.queue, &q->active);
663 buf->vb.state = VIDEOBUF_ACTIVE;
664 buf->count = q->count++;
665 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
666 /* 64 bit bits 63-32 */
667 prev->risc.jmp[2] = cpu_to_le32(0);
668 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
669 buf, buf->vb.i);
670
671 } else {
672 list_add_tail(&buf->vb.queue, &q->queued);
673 buf->vb.state = VIDEOBUF_QUEUED;
674 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
675 buf, buf->vb.i);
676 }
677 }
678}
679
680static void buffer_release(struct videobuf_queue *q,
681 struct videobuf_buffer *vb)
682{
683 struct cx23885_buffer *buf = container_of(vb,
684 struct cx23885_buffer, vb);
685
686 cx23885_free_buffer(q, buf);
687}
688
689static struct videobuf_queue_ops cx23885_video_qops = {
690 .buf_setup = buffer_setup,
691 .buf_prepare = buffer_prepare,
692 .buf_queue = buffer_queue,
693 .buf_release = buffer_release,
694};
695
696static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
697{
698 switch (fh->type) {
699 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
700 return &fh->vidq;
701 case V4L2_BUF_TYPE_VBI_CAPTURE:
702 return &fh->vbiq;
703 default:
704 BUG();
705 return NULL;
706 }
707}
708
709static int get_resource(struct cx23885_fh *fh)
710{
711 switch (fh->type) {
712 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
713 return RESOURCE_VIDEO;
714 case V4L2_BUF_TYPE_VBI_CAPTURE:
715 return RESOURCE_VBI;
716 default:
717 BUG();
718 return 0;
719 }
720}
721
Hans Verkuilbec43662008-12-30 06:58:20 -0300722static int video_open(struct file *file)
Steven Tothe47f30b2008-01-10 04:25:59 -0300723{
Hans Verkuilbec43662008-12-30 06:58:20 -0300724 int minor = video_devdata(file)->minor;
Steven Tothe47f30b2008-01-10 04:25:59 -0300725 struct cx23885_dev *h, *dev = NULL;
726 struct cx23885_fh *fh;
727 struct list_head *list;
728 enum v4l2_buf_type type = 0;
729 int radio = 0;
730
Hans Verkuild56dc612008-07-30 08:43:36 -0300731 lock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300732 list_for_each(list, &cx23885_devlist) {
733 h = list_entry(list, struct cx23885_dev, devlist);
Andy Wallscd8f8942009-01-09 22:59:27 -0300734 if (h->video_dev &&
735 h->video_dev->minor == minor) {
Steven Tothe47f30b2008-01-10 04:25:59 -0300736 dev = h;
737 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
738 }
739 if (h->vbi_dev &&
Andy Wallscd8f8942009-01-09 22:59:27 -0300740 h->vbi_dev->minor == minor) {
Steven Tothe47f30b2008-01-10 04:25:59 -0300741 dev = h;
742 type = V4L2_BUF_TYPE_VBI_CAPTURE;
743 }
744 if (h->radio_dev &&
745 h->radio_dev->minor == minor) {
746 radio = 1;
747 dev = h;
748 }
749 }
Hans Verkuild56dc612008-07-30 08:43:36 -0300750 if (NULL == dev) {
751 unlock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300752 return -ENODEV;
Hans Verkuild56dc612008-07-30 08:43:36 -0300753 }
Steven Tothe47f30b2008-01-10 04:25:59 -0300754
755 dprintk(1, "open minor=%d radio=%d type=%s\n",
756 minor, radio, v4l2_type_names[type]);
757
758 /* allocate + initialize per filehandle data */
759 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Hans Verkuild56dc612008-07-30 08:43:36 -0300760 if (NULL == fh) {
761 unlock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300762 return -ENOMEM;
Hans Verkuild56dc612008-07-30 08:43:36 -0300763 }
Steven Tothe47f30b2008-01-10 04:25:59 -0300764 file->private_data = fh;
765 fh->dev = dev;
766 fh->radio = radio;
767 fh->type = type;
768 fh->width = 320;
769 fh->height = 240;
770 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
771
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300772 videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
773 &dev->pci->dev, &dev->slock,
Steven Tothe47f30b2008-01-10 04:25:59 -0300774 V4L2_BUF_TYPE_VIDEO_CAPTURE,
775 V4L2_FIELD_INTERLACED,
776 sizeof(struct cx23885_buffer),
777 fh);
778
779 dprintk(1, "post videobuf_queue_init()\n");
780
Hans Verkuild56dc612008-07-30 08:43:36 -0300781 unlock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300782
783 return 0;
784}
785
786static ssize_t video_read(struct file *file, char __user *data,
787 size_t count, loff_t *ppos)
788{
789 struct cx23885_fh *fh = file->private_data;
790
791 switch (fh->type) {
792 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
793 if (res_locked(fh->dev, RESOURCE_VIDEO))
794 return -EBUSY;
795 return videobuf_read_one(&fh->vidq, data, count, ppos,
796 file->f_flags & O_NONBLOCK);
797 case V4L2_BUF_TYPE_VBI_CAPTURE:
798 if (!res_get(fh->dev, fh, RESOURCE_VBI))
799 return -EBUSY;
800 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
801 file->f_flags & O_NONBLOCK);
802 default:
803 BUG();
804 return 0;
805 }
806}
807
808static unsigned int video_poll(struct file *file,
809 struct poll_table_struct *wait)
810{
811 struct cx23885_fh *fh = file->private_data;
812 struct cx23885_buffer *buf;
813
814 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
815 if (!res_get(fh->dev, fh, RESOURCE_VBI))
816 return POLLERR;
817 return videobuf_poll_stream(file, &fh->vbiq, wait);
818 }
819
820 if (res_check(fh, RESOURCE_VIDEO)) {
821 /* streaming capture */
822 if (list_empty(&fh->vidq.stream))
823 return POLLERR;
824 buf = list_entry(fh->vidq.stream.next,
825 struct cx23885_buffer, vb.stream);
826 } else {
827 /* read() capture */
828 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
829 if (NULL == buf)
830 return POLLERR;
831 }
832 poll_wait(file, &buf->vb.done, wait);
833 if (buf->vb.state == VIDEOBUF_DONE ||
834 buf->vb.state == VIDEOBUF_ERROR)
835 return POLLIN|POLLRDNORM;
836 return 0;
837}
838
Hans Verkuilbec43662008-12-30 06:58:20 -0300839static int video_release(struct file *file)
Steven Tothe47f30b2008-01-10 04:25:59 -0300840{
841 struct cx23885_fh *fh = file->private_data;
842 struct cx23885_dev *dev = fh->dev;
843
844 /* turn off overlay */
845 if (res_check(fh, RESOURCE_OVERLAY)) {
846 /* FIXME */
847 res_free(dev, fh, RESOURCE_OVERLAY);
848 }
849
850 /* stop video capture */
851 if (res_check(fh, RESOURCE_VIDEO)) {
852 videobuf_queue_cancel(&fh->vidq);
853 res_free(dev, fh, RESOURCE_VIDEO);
854 }
855 if (fh->vidq.read_buf) {
856 buffer_release(&fh->vidq, fh->vidq.read_buf);
857 kfree(fh->vidq.read_buf);
858 }
859
860 /* stop vbi capture */
861 if (res_check(fh, RESOURCE_VBI)) {
862 if (fh->vbiq.streaming)
863 videobuf_streamoff(&fh->vbiq);
864 if (fh->vbiq.reading)
865 videobuf_read_stop(&fh->vbiq);
866 res_free(dev, fh, RESOURCE_VBI);
867 }
868
869 videobuf_mmap_free(&fh->vidq);
870 file->private_data = NULL;
871 kfree(fh);
872
Steven Totha19602f22008-01-10 11:43:18 -0300873 /* We are not putting the tuner to sleep here on exit, because
874 * we want to use the mpeg encoder in another session to capture
875 * tuner video. Closing this will result in no video to the encoder.
876 */
Steven Tothe47f30b2008-01-10 04:25:59 -0300877
878 return 0;
879}
880
881static int video_mmap(struct file *file, struct vm_area_struct *vma)
882{
883 struct cx23885_fh *fh = file->private_data;
884
885 return videobuf_mmap_mapper(get_queue(fh), vma);
886}
887
888/* ------------------------------------------------------------------ */
889/* VIDEO CTRL IOCTLS */
890
Steven Toth9c8ced52008-10-16 20:18:44 -0300891static int cx23885_get_control(struct cx23885_dev *dev,
892 struct v4l2_control *ctl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300893{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300894 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300895 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_G_CTRL, ctl);
896 return 0;
897}
Steven Tothe47f30b2008-01-10 04:25:59 -0300898
Steven Toth9c8ced52008-10-16 20:18:44 -0300899static int cx23885_set_control(struct cx23885_dev *dev,
900 struct v4l2_control *ctl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300901{
902 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
Harvey Harrison22b4e642008-04-08 23:20:00 -0300903 " (disabled - no action)\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300904 return 0;
905}
Steven Tothe47f30b2008-01-10 04:25:59 -0300906
907static void init_controls(struct cx23885_dev *dev)
908{
909 struct v4l2_control ctrl;
910 int i;
911
912 for (i = 0; i < CX23885_CTLS; i++) {
913 ctrl.id = cx23885_ctls[i].v.id;
914 ctrl.value = cx23885_ctls[i].v.default_value;
915
916 cx23885_set_control(dev, &ctrl);
917 }
918}
919
920/* ------------------------------------------------------------------ */
921/* VIDEO IOCTLS */
922
Hans Verkuil78b526a2008-05-28 12:16:41 -0300923static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300924 struct v4l2_format *f)
925{
926 struct cx23885_fh *fh = priv;
927
928 f->fmt.pix.width = fh->width;
929 f->fmt.pix.height = fh->height;
930 f->fmt.pix.field = fh->vidq.field;
931 f->fmt.pix.pixelformat = fh->fmt->fourcc;
932 f->fmt.pix.bytesperline =
933 (f->fmt.pix.width * fh->fmt->depth) >> 3;
934 f->fmt.pix.sizeimage =
935 f->fmt.pix.height * f->fmt.pix.bytesperline;
936
937 return 0;
938}
939
Hans Verkuil78b526a2008-05-28 12:16:41 -0300940static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300941 struct v4l2_format *f)
942{
943 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
944 struct cx23885_fmt *fmt;
945 enum v4l2_field field;
946 unsigned int maxw, maxh;
947
948 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
949 if (NULL == fmt)
950 return -EINVAL;
951
952 field = f->fmt.pix.field;
953 maxw = norm_maxw(dev->tvnorm);
954 maxh = norm_maxh(dev->tvnorm);
955
956 if (V4L2_FIELD_ANY == field) {
957 field = (f->fmt.pix.height > maxh/2)
958 ? V4L2_FIELD_INTERLACED
959 : V4L2_FIELD_BOTTOM;
960 }
961
962 switch (field) {
963 case V4L2_FIELD_TOP:
964 case V4L2_FIELD_BOTTOM:
965 maxh = maxh / 2;
966 break;
967 case V4L2_FIELD_INTERLACED:
968 break;
969 default:
970 return -EINVAL;
971 }
972
973 f->fmt.pix.field = field;
974 if (f->fmt.pix.height < 32)
975 f->fmt.pix.height = 32;
976 if (f->fmt.pix.height > maxh)
977 f->fmt.pix.height = maxh;
978 if (f->fmt.pix.width < 48)
979 f->fmt.pix.width = 48;
980 if (f->fmt.pix.width > maxw)
981 f->fmt.pix.width = maxw;
982 f->fmt.pix.width &= ~0x03;
983 f->fmt.pix.bytesperline =
984 (f->fmt.pix.width * fmt->depth) >> 3;
985 f->fmt.pix.sizeimage =
986 f->fmt.pix.height * f->fmt.pix.bytesperline;
987
988 return 0;
989}
990
Hans Verkuil78b526a2008-05-28 12:16:41 -0300991static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300992 struct v4l2_format *f)
993{
994 struct cx23885_fh *fh = priv;
995 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
996 int err;
997
Harvey Harrison22b4e642008-04-08 23:20:00 -0300998 dprintk(2, "%s()\n", __func__);
Hans Verkuil78b526a2008-05-28 12:16:41 -0300999 err = vidioc_try_fmt_vid_cap(file, priv, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001000
1001 if (0 != err)
1002 return err;
1003 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1004 fh->width = f->fmt.pix.width;
1005 fh->height = f->fmt.pix.height;
1006 fh->vidq.field = f->fmt.pix.field;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001007 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -03001008 fh->width, fh->height, fh->vidq.field);
1009 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_S_FMT, f);
1010 return 0;
1011}
1012
1013static int vidioc_querycap(struct file *file, void *priv,
1014 struct v4l2_capability *cap)
1015{
1016 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1017
1018 strcpy(cap->driver, "cx23885");
1019 strlcpy(cap->card, cx23885_boards[dev->board].name,
1020 sizeof(cap->card));
1021 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1022 cap->version = CX23885_VERSION_CODE;
1023 cap->capabilities =
1024 V4L2_CAP_VIDEO_CAPTURE |
1025 V4L2_CAP_READWRITE |
1026 V4L2_CAP_STREAMING |
1027 V4L2_CAP_VBI_CAPTURE;
1028 if (UNSET != dev->tuner_type)
1029 cap->capabilities |= V4L2_CAP_TUNER;
1030 return 0;
1031}
1032
Hans Verkuil78b526a2008-05-28 12:16:41 -03001033static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001034 struct v4l2_fmtdesc *f)
1035{
1036 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1037 return -EINVAL;
1038
1039 strlcpy(f->description, formats[f->index].name,
1040 sizeof(f->description));
1041 f->pixelformat = formats[f->index].fourcc;
1042
1043 return 0;
1044}
1045
1046#ifdef CONFIG_VIDEO_V4L1_COMPAT
1047static int vidiocgmbuf(struct file *file, void *priv,
1048 struct video_mbuf *mbuf)
1049{
1050 struct cx23885_fh *fh = priv;
1051 struct videobuf_queue *q;
1052 struct v4l2_requestbuffers req;
1053 unsigned int i;
1054 int err;
1055
1056 q = get_queue(fh);
1057 memset(&req, 0, sizeof(req));
1058 req.type = q->type;
1059 req.count = 8;
1060 req.memory = V4L2_MEMORY_MMAP;
1061 err = videobuf_reqbufs(q, &req);
1062 if (err < 0)
1063 return err;
1064
1065 mbuf->frames = req.count;
1066 mbuf->size = 0;
1067 for (i = 0; i < mbuf->frames; i++) {
1068 mbuf->offsets[i] = q->bufs[i]->boff;
1069 mbuf->size += q->bufs[i]->bsize;
1070 }
1071 return 0;
1072}
1073#endif
1074
1075static int vidioc_reqbufs(struct file *file, void *priv,
1076 struct v4l2_requestbuffers *p)
1077{
1078 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001079 return videobuf_reqbufs(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001080}
1081
1082static int vidioc_querybuf(struct file *file, void *priv,
1083 struct v4l2_buffer *p)
1084{
1085 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001086 return videobuf_querybuf(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001087}
1088
1089static int vidioc_qbuf(struct file *file, void *priv,
1090 struct v4l2_buffer *p)
1091{
1092 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001093 return videobuf_qbuf(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001094}
1095
1096static int vidioc_dqbuf(struct file *file, void *priv,
1097 struct v4l2_buffer *p)
1098{
1099 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001100 return videobuf_dqbuf(get_queue(fh), p,
1101 file->f_flags & O_NONBLOCK);
Steven Tothe47f30b2008-01-10 04:25:59 -03001102}
1103
1104static int vidioc_streamon(struct file *file, void *priv,
1105 enum v4l2_buf_type i)
1106{
1107 struct cx23885_fh *fh = priv;
1108 struct cx23885_dev *dev = fh->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001109 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001110
1111 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1112 return -EINVAL;
1113 if (unlikely(i != fh->type))
1114 return -EINVAL;
1115
1116 if (unlikely(!res_get(dev, fh, get_resource(fh))))
1117 return -EBUSY;
1118 return videobuf_streamon(get_queue(fh));
1119}
1120
1121static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1122{
1123 struct cx23885_fh *fh = priv;
1124 struct cx23885_dev *dev = fh->dev;
1125 int err, res;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001126 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001127
1128 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1129 return -EINVAL;
1130 if (i != fh->type)
1131 return -EINVAL;
1132
1133 res = get_resource(fh);
1134 err = videobuf_streamoff(get_queue(fh));
1135 if (err < 0)
1136 return err;
1137 res_free(dev, fh, res);
1138 return 0;
1139}
1140
1141static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1142{
1143 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001144 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001145
1146 mutex_lock(&dev->lock);
1147 cx23885_set_tvnorm(dev, *tvnorms);
1148 mutex_unlock(&dev->lock);
1149
1150 return 0;
1151}
1152
Hans Verkuild45b9b82008-09-04 03:33:43 -03001153static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
Steven Tothe47f30b2008-01-10 04:25:59 -03001154{
1155 static const char *iname[] = {
1156 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1157 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1158 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1159 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1160 [CX23885_VMUX_SVIDEO] = "S-Video",
1161 [CX23885_VMUX_TELEVISION] = "Television",
1162 [CX23885_VMUX_CABLE] = "Cable TV",
1163 [CX23885_VMUX_DVB] = "DVB",
1164 [CX23885_VMUX_DEBUG] = "for debug only",
1165 };
1166 unsigned int n;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001167 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001168
1169 n = i->index;
1170 if (n >= 4)
1171 return -EINVAL;
1172
1173 if (0 == INPUT(n)->type)
1174 return -EINVAL;
1175
1176 memset(i, 0, sizeof(*i));
1177 i->index = n;
1178 i->type = V4L2_INPUT_TYPE_CAMERA;
1179 strcpy(i->name, iname[INPUT(n)->type]);
1180 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
1181 (CX23885_VMUX_CABLE == INPUT(n)->type))
1182 i->type = V4L2_INPUT_TYPE_TUNER;
1183 i->std = CX23885_NORMS;
1184 return 0;
1185}
Steven Tothe47f30b2008-01-10 04:25:59 -03001186
1187static int vidioc_enum_input(struct file *file, void *priv,
1188 struct v4l2_input *i)
1189{
1190 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001191 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001192 return cx23885_enum_input(dev, i);
1193}
1194
1195static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1196{
1197 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1198
1199 *i = dev->input;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001200 dprintk(1, "%s() returns %d\n", __func__, *i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001201 return 0;
1202}
1203
1204static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1205{
1206 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1207
Harvey Harrison22b4e642008-04-08 23:20:00 -03001208 dprintk(1, "%s(%d)\n", __func__, i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001209
1210 if (i >= 4) {
Harvey Harrison22b4e642008-04-08 23:20:00 -03001211 dprintk(1, "%s() -EINVAL\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001212 return -EINVAL;
1213 }
1214
1215 mutex_lock(&dev->lock);
1216 cx23885_video_mux(dev, i);
1217 mutex_unlock(&dev->lock);
1218 return 0;
1219}
1220
1221static int vidioc_queryctrl(struct file *file, void *priv,
1222 struct v4l2_queryctrl *qctrl)
1223{
1224 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1225 if (unlikely(qctrl->id == 0))
1226 return -EINVAL;
1227 return cx23885_ctrl_query(qctrl);
1228}
1229
1230static int vidioc_g_ctrl(struct file *file, void *priv,
1231 struct v4l2_control *ctl)
1232{
1233 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1234
1235 return cx23885_get_control(dev, ctl);
1236}
1237
1238static int vidioc_s_ctrl(struct file *file, void *priv,
1239 struct v4l2_control *ctl)
1240{
1241 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1242
1243 return cx23885_set_control(dev, ctl);
1244}
1245
1246static int vidioc_g_tuner(struct file *file, void *priv,
1247 struct v4l2_tuner *t)
1248{
1249 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1250
1251 if (unlikely(UNSET == dev->tuner_type))
1252 return -EINVAL;
1253 if (0 != t->index)
1254 return -EINVAL;
1255
1256 strcpy(t->name, "Television");
1257 t->type = V4L2_TUNER_ANALOG_TV;
1258 t->capability = V4L2_TUNER_CAP_NORM;
1259 t->rangehigh = 0xffffffffUL;
1260 t->signal = 0xffff ; /* LOCKED */
1261 return 0;
1262}
1263
1264static int vidioc_s_tuner(struct file *file, void *priv,
1265 struct v4l2_tuner *t)
1266{
1267 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1268
1269 if (UNSET == dev->tuner_type)
1270 return -EINVAL;
1271 if (0 != t->index)
1272 return -EINVAL;
1273 return 0;
1274}
1275
1276static int vidioc_g_frequency(struct file *file, void *priv,
1277 struct v4l2_frequency *f)
1278{
1279 struct cx23885_fh *fh = priv;
1280 struct cx23885_dev *dev = fh->dev;
1281
1282 if (unlikely(UNSET == dev->tuner_type))
1283 return -EINVAL;
1284
1285 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1286 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1287 f->frequency = dev->freq;
1288
1289 cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_G_FREQUENCY, f);
1290
1291 return 0;
1292}
1293
Hans Verkuild45b9b82008-09-04 03:33:43 -03001294static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
Steven Tothe47f30b2008-01-10 04:25:59 -03001295{
1296 if (unlikely(UNSET == dev->tuner_type))
1297 return -EINVAL;
1298 if (unlikely(f->tuner != 0))
1299 return -EINVAL;
1300
1301 mutex_lock(&dev->lock);
1302 dev->freq = f->frequency;
1303
1304 cx23885_call_i2c_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, f);
1305
1306 /* When changing channels it is required to reset TVAUDIO */
1307 msleep(10);
1308
1309 mutex_unlock(&dev->lock);
1310
1311 return 0;
1312}
Steven Tothe47f30b2008-01-10 04:25:59 -03001313
1314static int vidioc_s_frequency(struct file *file, void *priv,
1315 struct v4l2_frequency *f)
1316{
1317 struct cx23885_fh *fh = priv;
1318 struct cx23885_dev *dev = fh->dev;
1319
1320 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1321 return -EINVAL;
1322 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1323 return -EINVAL;
1324
1325 return
1326 cx23885_set_freq(dev, f);
1327}
1328
1329#ifdef CONFIG_VIDEO_ADV_DEBUG
1330static int vidioc_g_register(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001331 struct v4l2_dbg_register *reg)
Steven Tothe47f30b2008-01-10 04:25:59 -03001332{
1333 struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1334
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001335 if (!v4l2_chip_match_host(&reg->match))
Steven Totha19602f22008-01-10 11:43:18 -03001336 return -EINVAL;
1337
Steven Tothe47f30b2008-01-10 04:25:59 -03001338 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_G_REGISTER, reg);
1339
1340 return 0;
1341}
1342
1343static int vidioc_s_register(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001344 struct v4l2_dbg_register *reg)
Steven Tothe47f30b2008-01-10 04:25:59 -03001345{
1346 struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1347
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001348 if (!v4l2_chip_match_host(&reg->match))
Steven Totha19602f22008-01-10 11:43:18 -03001349 return -EINVAL;
1350
Steven Tothe47f30b2008-01-10 04:25:59 -03001351 cx23885_call_i2c_clients(&dev->i2c_bus[2], VIDIOC_DBG_S_REGISTER, reg);
Steven Totha19602f22008-01-10 11:43:18 -03001352
Steven Tothe47f30b2008-01-10 04:25:59 -03001353 return 0;
1354}
1355#endif
1356
1357/* ----------------------------------------------------------- */
Steven Tothe47f30b2008-01-10 04:25:59 -03001358
1359static void cx23885_vid_timeout(unsigned long data)
1360{
1361 struct cx23885_dev *dev = (struct cx23885_dev *)data;
1362 struct cx23885_dmaqueue *q = &dev->vidq;
1363 struct cx23885_buffer *buf;
1364 unsigned long flags;
1365
1366 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1367
1368 cx_clear(VID_A_DMA_CTL, 0x11);
1369
1370 spin_lock_irqsave(&dev->slock, flags);
1371 while (!list_empty(&q->active)) {
1372 buf = list_entry(q->active.next,
1373 struct cx23885_buffer, vb.queue);
1374 list_del(&buf->vb.queue);
1375 buf->vb.state = VIDEOBUF_ERROR;
1376 wake_up(&buf->vb.done);
1377 printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
1378 dev->name, buf, buf->vb.i,
1379 (unsigned long)buf->risc.dma);
1380 }
1381 cx23885_restart_video_queue(dev, q);
1382 spin_unlock_irqrestore(&dev->slock, flags);
1383}
1384
1385int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1386{
1387 u32 mask, count;
1388 int handled = 0;
1389
1390 mask = cx_read(VID_A_INT_MSK);
1391 if (0 == (status & mask))
1392 return handled;
1393 cx_write(VID_A_INT_STAT, status);
1394
Harvey Harrison22b4e642008-04-08 23:20:00 -03001395 dprintk(2, "%s() status = 0x%08x\n", __func__, status);
Steven Tothe47f30b2008-01-10 04:25:59 -03001396 /* risc op code error */
1397 if (status & (1 << 16)) {
1398 printk(KERN_WARNING "%s/0: video risc op code error\n",
1399 dev->name);
1400 cx_clear(VID_A_DMA_CTL, 0x11);
1401 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1402 }
1403
1404 /* risc1 y */
1405 if (status & 0x01) {
1406 spin_lock(&dev->slock);
1407 count = cx_read(VID_A_GPCNT);
1408 cx23885_video_wakeup(dev, &dev->vidq, count);
1409 spin_unlock(&dev->slock);
1410 handled++;
1411 }
1412 /* risc2 y */
1413 if (status & 0x10) {
1414 dprintk(2, "stopper video\n");
1415 spin_lock(&dev->slock);
1416 cx23885_restart_video_queue(dev, &dev->vidq);
1417 spin_unlock(&dev->slock);
1418 handled++;
1419 }
1420
1421 return handled;
1422}
1423
Steven Tothe47f30b2008-01-10 04:25:59 -03001424/* ----------------------------------------------------------- */
1425/* exported stuff */
1426
Hans Verkuilbec43662008-12-30 06:58:20 -03001427static const struct v4l2_file_operations video_fops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001428 .owner = THIS_MODULE,
1429 .open = video_open,
1430 .release = video_release,
1431 .read = video_read,
1432 .poll = video_poll,
1433 .mmap = video_mmap,
1434 .ioctl = video_ioctl2,
Steven Tothe47f30b2008-01-10 04:25:59 -03001435};
1436
Hans Verkuila3998102008-07-21 02:57:38 -03001437static const struct v4l2_ioctl_ops video_ioctl_ops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001438 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001439 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1440 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1441 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1442 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1443 .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
1444 .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
1445 .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
Steven Tothe47f30b2008-01-10 04:25:59 -03001446 .vidioc_reqbufs = vidioc_reqbufs,
1447 .vidioc_querybuf = vidioc_querybuf,
1448 .vidioc_qbuf = vidioc_qbuf,
1449 .vidioc_dqbuf = vidioc_dqbuf,
1450 .vidioc_s_std = vidioc_s_std,
1451 .vidioc_enum_input = vidioc_enum_input,
1452 .vidioc_g_input = vidioc_g_input,
1453 .vidioc_s_input = vidioc_s_input,
1454 .vidioc_queryctrl = vidioc_queryctrl,
1455 .vidioc_g_ctrl = vidioc_g_ctrl,
1456 .vidioc_s_ctrl = vidioc_s_ctrl,
1457 .vidioc_streamon = vidioc_streamon,
1458 .vidioc_streamoff = vidioc_streamoff,
1459#ifdef CONFIG_VIDEO_V4L1_COMPAT
1460 .vidiocgmbuf = vidiocgmbuf,
1461#endif
1462 .vidioc_g_tuner = vidioc_g_tuner,
1463 .vidioc_s_tuner = vidioc_s_tuner,
1464 .vidioc_g_frequency = vidioc_g_frequency,
1465 .vidioc_s_frequency = vidioc_s_frequency,
1466#ifdef CONFIG_VIDEO_ADV_DEBUG
1467 .vidioc_g_register = vidioc_g_register,
1468 .vidioc_s_register = vidioc_s_register,
1469#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001470};
1471
1472static struct video_device cx23885_vbi_template;
1473static struct video_device cx23885_video_template = {
1474 .name = "cx23885-video",
Hans Verkuila3998102008-07-21 02:57:38 -03001475 .fops = &video_fops,
1476 .minor = -1,
1477 .ioctl_ops = &video_ioctl_ops,
Steven Tothe47f30b2008-01-10 04:25:59 -03001478 .tvnorms = CX23885_NORMS,
1479 .current_norm = V4L2_STD_NTSC_M,
1480};
1481
Hans Verkuilbec43662008-12-30 06:58:20 -03001482static const struct v4l2_file_operations radio_fops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001483 .owner = THIS_MODULE,
1484 .open = video_open,
1485 .release = video_release,
1486 .ioctl = video_ioctl2,
Steven Tothe47f30b2008-01-10 04:25:59 -03001487};
1488
1489
1490void cx23885_video_unregister(struct cx23885_dev *dev)
1491{
Harvey Harrison22b4e642008-04-08 23:20:00 -03001492 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001493 cx_clear(PCI_INT_MSK, 1);
1494
1495 if (dev->video_dev) {
1496 if (-1 != dev->video_dev->minor)
1497 video_unregister_device(dev->video_dev);
1498 else
1499 video_device_release(dev->video_dev);
1500 dev->video_dev = NULL;
1501
1502 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1503 }
1504}
1505
1506int cx23885_video_register(struct cx23885_dev *dev)
1507{
1508 int err;
1509
Harvey Harrison22b4e642008-04-08 23:20:00 -03001510 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001511 spin_lock_init(&dev->slock);
1512
1513 /* Initialize VBI template */
1514 memcpy(&cx23885_vbi_template, &cx23885_video_template,
1515 sizeof(cx23885_vbi_template));
1516 strcpy(cx23885_vbi_template.name, "cx23885-vbi");
Steven Tothe47f30b2008-01-10 04:25:59 -03001517
1518 dev->tvnorm = cx23885_video_template.current_norm;
1519
1520 /* init video dma queues */
1521 INIT_LIST_HEAD(&dev->vidq.active);
1522 INIT_LIST_HEAD(&dev->vidq.queued);
1523 dev->vidq.timeout.function = cx23885_vid_timeout;
1524 dev->vidq.timeout.data = (unsigned long)dev;
1525 init_timer(&dev->vidq.timeout);
1526 cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1527 VID_A_DMA_CTL, 0x11, 0x00);
1528
Steven Totha19602f22008-01-10 11:43:18 -03001529 /* Don't enable VBI yet */
Steven Tothe47f30b2008-01-10 04:25:59 -03001530 cx_set(PCI_INT_MSK, 1);
1531
1532
1533 /* register v4l devices */
1534 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1535 &cx23885_video_template, "video");
1536 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1537 video_nr[dev->nr]);
1538 if (err < 0) {
1539 printk(KERN_INFO "%s: can't register video device\n",
1540 dev->name);
1541 goto fail_unreg;
1542 }
1543 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03001544 dev->name, dev->video_dev->num);
Steven Tothe47f30b2008-01-10 04:25:59 -03001545 /* initial device configuration */
1546 mutex_lock(&dev->lock);
1547 cx23885_set_tvnorm(dev, dev->tvnorm);
1548 init_controls(dev);
1549 cx23885_video_mux(dev, 0);
1550 mutex_unlock(&dev->lock);
1551
Steven Tothe47f30b2008-01-10 04:25:59 -03001552 return 0;
1553
1554fail_unreg:
1555 cx23885_video_unregister(dev);
1556 return err;
1557}
1558