blob: e10f28cfef5e805214208b5ae083de1311ee9f8c [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>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040029#include <linux/smp_lock.h>
Steven Tothe47f30b2008-01-10 04:25:59 -030030#include <linux/interrupt.h>
31#include <linux/delay.h>
32#include <linux/kthread.h>
33#include <asm/div64.h>
34
35#include "cx23885.h"
36#include <media/v4l2-common.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030037#include <media/v4l2-ioctl.h>
Andy Walls74618242009-09-26 22:50:44 -030038#include "cx23885-ioctl.h"
Steven Tothe47f30b2008-01-10 04:25:59 -030039
Steven Tothe47f30b2008-01-10 04:25:59 -030040MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
Steven Toth6d897612008-09-03 17:12:12 -030041MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
Steven Tothe47f30b2008-01-10 04:25:59 -030042MODULE_LICENSE("GPL");
43
44/* ------------------------------------------------------------------ */
45
46static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
47static unsigned int vbi_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
48static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
49
50module_param_array(video_nr, int, NULL, 0444);
51module_param_array(vbi_nr, int, NULL, 0444);
52module_param_array(radio_nr, int, NULL, 0444);
53
54MODULE_PARM_DESC(video_nr, "video device numbers");
55MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
56MODULE_PARM_DESC(radio_nr, "radio device numbers");
57
Steven Toth4513fc692008-01-12 11:36:36 -030058static unsigned int video_debug;
Steven Tothe47f30b2008-01-10 04:25:59 -030059module_param(video_debug, int, 0644);
60MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
61
Steven Toth4513fc692008-01-12 11:36:36 -030062static unsigned int irq_debug;
Steven Tothe47f30b2008-01-10 04:25:59 -030063module_param(irq_debug, int, 0644);
64MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
65
66static unsigned int vid_limit = 16;
67module_param(vid_limit, int, 0644);
68MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
69
70#define dprintk(level, fmt, arg...)\
Steven Toth4513fc692008-01-12 11:36:36 -030071 do { if (video_debug >= level)\
72 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
73 } while (0)
Steven Tothe47f30b2008-01-10 04:25:59 -030074
75/* ------------------------------------------------------------------- */
76/* static data */
77
78#define FORMAT_FLAGS_PACKED 0x01
79
80static struct cx23885_fmt formats[] = {
81 {
82 .name = "8 bpp, gray",
83 .fourcc = V4L2_PIX_FMT_GREY,
84 .depth = 8,
85 .flags = FORMAT_FLAGS_PACKED,
86 }, {
87 .name = "15 bpp RGB, le",
88 .fourcc = V4L2_PIX_FMT_RGB555,
89 .depth = 16,
90 .flags = FORMAT_FLAGS_PACKED,
91 }, {
92 .name = "15 bpp RGB, be",
93 .fourcc = V4L2_PIX_FMT_RGB555X,
94 .depth = 16,
95 .flags = FORMAT_FLAGS_PACKED,
96 }, {
97 .name = "16 bpp RGB, le",
98 .fourcc = V4L2_PIX_FMT_RGB565,
99 .depth = 16,
100 .flags = FORMAT_FLAGS_PACKED,
101 }, {
102 .name = "16 bpp RGB, be",
103 .fourcc = V4L2_PIX_FMT_RGB565X,
104 .depth = 16,
105 .flags = FORMAT_FLAGS_PACKED,
106 }, {
107 .name = "24 bpp RGB, le",
108 .fourcc = V4L2_PIX_FMT_BGR24,
109 .depth = 24,
110 .flags = FORMAT_FLAGS_PACKED,
111 }, {
112 .name = "32 bpp RGB, le",
113 .fourcc = V4L2_PIX_FMT_BGR32,
114 .depth = 32,
115 .flags = FORMAT_FLAGS_PACKED,
116 }, {
117 .name = "32 bpp RGB, be",
118 .fourcc = V4L2_PIX_FMT_RGB32,
119 .depth = 32,
120 .flags = FORMAT_FLAGS_PACKED,
121 }, {
122 .name = "4:2:2, packed, YUYV",
123 .fourcc = V4L2_PIX_FMT_YUYV,
124 .depth = 16,
125 .flags = FORMAT_FLAGS_PACKED,
126 }, {
127 .name = "4:2:2, packed, UYVY",
128 .fourcc = V4L2_PIX_FMT_UYVY,
129 .depth = 16,
130 .flags = FORMAT_FLAGS_PACKED,
131 },
132};
133
134static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
135{
136 unsigned int i;
137
138 for (i = 0; i < ARRAY_SIZE(formats); i++)
139 if (formats[i].fourcc == fourcc)
140 return formats+i;
141
Harvey Harrison22b4e642008-04-08 23:20:00 -0300142 printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __func__, fourcc);
Steven Tothe47f30b2008-01-10 04:25:59 -0300143 return NULL;
144}
145
146/* ------------------------------------------------------------------- */
147
148static const struct v4l2_queryctrl no_ctl = {
149 .name = "42",
150 .flags = V4L2_CTRL_FLAG_DISABLED,
151};
152
153static struct cx23885_ctrl cx23885_ctls[] = {
154 /* --- video --- */
155 {
156 .v = {
157 .id = V4L2_CID_BRIGHTNESS,
158 .name = "Brightness",
159 .minimum = 0x00,
160 .maximum = 0xff,
161 .step = 1,
162 .default_value = 0x7f,
163 .type = V4L2_CTRL_TYPE_INTEGER,
164 },
165 .off = 128,
166 .reg = LUMA_CTRL,
167 .mask = 0x00ff,
168 .shift = 0,
169 }, {
170 .v = {
171 .id = V4L2_CID_CONTRAST,
172 .name = "Contrast",
173 .minimum = 0,
174 .maximum = 0xff,
175 .step = 1,
176 .default_value = 0x3f,
177 .type = V4L2_CTRL_TYPE_INTEGER,
178 },
179 .off = 0,
180 .reg = LUMA_CTRL,
181 .mask = 0xff00,
182 .shift = 8,
183 }, {
184 .v = {
185 .id = V4L2_CID_HUE,
186 .name = "Hue",
187 .minimum = 0,
188 .maximum = 0xff,
189 .step = 1,
190 .default_value = 0x7f,
191 .type = V4L2_CTRL_TYPE_INTEGER,
192 },
193 .off = 128,
194 .reg = CHROMA_CTRL,
195 .mask = 0xff0000,
196 .shift = 16,
197 }, {
198 /* strictly, this only describes only U saturation.
199 * V saturation is handled specially through code.
200 */
201 .v = {
202 .id = V4L2_CID_SATURATION,
203 .name = "Saturation",
204 .minimum = 0,
205 .maximum = 0xff,
206 .step = 1,
207 .default_value = 0x7f,
208 .type = V4L2_CTRL_TYPE_INTEGER,
209 },
210 .off = 0,
211 .reg = CHROMA_CTRL,
212 .mask = 0x00ff,
213 .shift = 0,
214 }, {
215 /* --- audio --- */
216 .v = {
217 .id = V4L2_CID_AUDIO_MUTE,
218 .name = "Mute",
219 .minimum = 0,
220 .maximum = 1,
221 .default_value = 1,
222 .type = V4L2_CTRL_TYPE_BOOLEAN,
223 },
224 .reg = PATH1_CTL1,
225 .mask = (0x1f << 24),
226 .shift = 24,
227 }, {
228 .v = {
229 .id = V4L2_CID_AUDIO_VOLUME,
230 .name = "Volume",
231 .minimum = 0,
232 .maximum = 0x3f,
233 .step = 1,
234 .default_value = 0x3f,
235 .type = V4L2_CTRL_TYPE_INTEGER,
236 },
237 .reg = PATH1_VOL_CTL,
238 .mask = 0xff,
239 .shift = 0,
240 }
241};
242static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
243
Hans Verkuil2ba58892009-02-13 10:57:48 -0300244/* Must be sorted from low to high control ID! */
Hans Verkuild45b9b82008-09-04 03:33:43 -0300245static const u32 cx23885_user_ctrls[] = {
Steven Tothe47f30b2008-01-10 04:25:59 -0300246 V4L2_CID_USER_CLASS,
247 V4L2_CID_BRIGHTNESS,
248 V4L2_CID_CONTRAST,
249 V4L2_CID_SATURATION,
250 V4L2_CID_HUE,
251 V4L2_CID_AUDIO_VOLUME,
252 V4L2_CID_AUDIO_MUTE,
253 0
254};
Steven Tothe47f30b2008-01-10 04:25:59 -0300255
256static const u32 *ctrl_classes[] = {
257 cx23885_user_ctrls,
258 NULL
259};
260
Hans Verkuild45b9b82008-09-04 03:33:43 -0300261static void cx23885_video_wakeup(struct cx23885_dev *dev,
Steven Tothe47f30b2008-01-10 04:25:59 -0300262 struct cx23885_dmaqueue *q, u32 count)
263{
264 struct cx23885_buffer *buf;
265 int bc;
266
267 for (bc = 0;; bc++) {
268 if (list_empty(&q->active))
269 break;
270 buf = list_entry(q->active.next,
271 struct cx23885_buffer, vb.queue);
Steven Totha19602f22008-01-10 11:43:18 -0300272
Steven Tothe47f30b2008-01-10 04:25:59 -0300273 /* count comes from the hw and is is 16bit wide --
274 * this trick handles wrap-arounds correctly for
275 * up to 32767 buffers in flight... */
276 if ((s16) (count - buf->count) < 0)
277 break;
Steven Totha19602f22008-01-10 11:43:18 -0300278
Steven Tothe47f30b2008-01-10 04:25:59 -0300279 do_gettimeofday(&buf->vb.ts);
280 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
281 count, buf->count);
282 buf->vb.state = VIDEOBUF_DONE;
283 list_del(&buf->vb.queue);
284 wake_up(&buf->vb.done);
285 }
Steven Toth9c8ced52008-10-16 20:18:44 -0300286 if (list_empty(&q->active))
Steven Tothe47f30b2008-01-10 04:25:59 -0300287 del_timer(&q->timeout);
Steven Toth9c8ced52008-10-16 20:18:44 -0300288 else
Steven Tothe47f30b2008-01-10 04:25:59 -0300289 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
Steven Tothe47f30b2008-01-10 04:25:59 -0300290 if (bc != 1)
Steven Toth21a78092008-01-10 04:38:59 -0300291 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300292 __func__, bc);
Steven Tothe47f30b2008-01-10 04:25:59 -0300293}
294
Hans Verkuild45b9b82008-09-04 03:33:43 -0300295static int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
Steven Tothe47f30b2008-01-10 04:25:59 -0300296{
297 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300298 __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -0300299 (unsigned int)norm,
300 v4l2_norm_to_name(norm));
301
302 dev->tvnorm = norm;
303
Hans Verkuilf41737e2009-04-01 03:52:39 -0300304 call_all(dev, core, s_std, norm);
Steven Tothe47f30b2008-01-10 04:25:59 -0300305
306 return 0;
307}
308
Hans Verkuild45b9b82008-09-04 03:33:43 -0300309static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
Steven Tothe47f30b2008-01-10 04:25:59 -0300310 struct pci_dev *pci,
311 struct video_device *template,
312 char *type)
313{
314 struct video_device *vfd;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300315 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300316
317 vfd = video_device_alloc();
318 if (NULL == vfd)
319 return NULL;
320 *vfd = *template;
Hans Verkuilc0714f62009-03-13 08:02:43 -0300321 vfd->minor = -1;
322 vfd->v4l2_dev = &dev->v4l2_dev;
Steven Tothe47f30b2008-01-10 04:25:59 -0300323 vfd->release = video_device_release;
324 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
325 dev->name, type, cx23885_boards[dev->board].name);
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200326 video_set_drvdata(vfd, dev);
Steven Tothe47f30b2008-01-10 04:25:59 -0300327 return vfd;
328}
329
Hans Verkuild45b9b82008-09-04 03:33:43 -0300330static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300331{
332 int i;
333
334 if (qctrl->id < V4L2_CID_BASE ||
335 qctrl->id >= V4L2_CID_LASTP1)
336 return -EINVAL;
337 for (i = 0; i < CX23885_CTLS; i++)
338 if (cx23885_ctls[i].v.id == qctrl->id)
339 break;
340 if (i == CX23885_CTLS) {
341 *qctrl = no_ctl;
342 return 0;
343 }
344 *qctrl = cx23885_ctls[i].v;
345 return 0;
346}
Steven Tothe47f30b2008-01-10 04:25:59 -0300347
348/* ------------------------------------------------------------------- */
349/* resource management */
350
351static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
352 unsigned int bit)
353{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300354 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300355 if (fh->resources & bit)
356 /* have it already allocated */
357 return 1;
358
359 /* is it free? */
360 mutex_lock(&dev->lock);
361 if (dev->resources & bit) {
362 /* no, someone else uses it */
363 mutex_unlock(&dev->lock);
364 return 0;
365 }
366 /* it's free, grab it */
367 fh->resources |= bit;
368 dev->resources |= bit;
369 dprintk(1, "res: get %d\n", bit);
370 mutex_unlock(&dev->lock);
371 return 1;
372}
373
374static int res_check(struct cx23885_fh *fh, unsigned int bit)
375{
Steven Toth9c8ced52008-10-16 20:18:44 -0300376 return fh->resources & bit;
Steven Tothe47f30b2008-01-10 04:25:59 -0300377}
378
379static int res_locked(struct cx23885_dev *dev, unsigned int bit)
380{
Steven Toth9c8ced52008-10-16 20:18:44 -0300381 return dev->resources & bit;
Steven Tothe47f30b2008-01-10 04:25:59 -0300382}
383
384static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
385 unsigned int bits)
386{
387 BUG_ON((fh->resources & bits) != bits);
Harvey Harrison22b4e642008-04-08 23:20:00 -0300388 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300389
390 mutex_lock(&dev->lock);
391 fh->resources &= ~bits;
392 dev->resources &= ~bits;
393 dprintk(1, "res: put %d\n", bits);
394 mutex_unlock(&dev->lock);
395}
396
Hans Verkuild45b9b82008-09-04 03:33:43 -0300397static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
Steven Tothe47f30b2008-01-10 04:25:59 -0300398{
Steven Tothe47f30b2008-01-10 04:25:59 -0300399 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 -0300400 __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -0300401 input, INPUT(input)->vmux,
402 INPUT(input)->gpio0, INPUT(input)->gpio1,
403 INPUT(input)->gpio2, INPUT(input)->gpio3);
404 dev->input = input;
405
David T.L. Wong6f0d8c02009-10-21 13:15:30 -0300406 if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
407 dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2) {
408 /* Select Analog TV */
409 if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
410 cx23885_gpio_clear(dev, GPIO_0);
411 }
412
Steven Tothe47f30b2008-01-10 04:25:59 -0300413 /* Tell the internal A/V decoder */
Hans Verkuil5325b422009-04-02 11:26:22 -0300414 v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
415 INPUT(input)->vmux, 0, 0);
Steven Tothe47f30b2008-01-10 04:25:59 -0300416
417 return 0;
418}
Steven Tothe47f30b2008-01-10 04:25:59 -0300419
420/* ------------------------------------------------------------------ */
Hans Verkuild45b9b82008-09-04 03:33:43 -0300421static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
Steven Tothe47f30b2008-01-10 04:25:59 -0300422 unsigned int height, enum v4l2_field field)
423{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300424 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300425 return 0;
426}
427
428static int cx23885_start_video_dma(struct cx23885_dev *dev,
429 struct cx23885_dmaqueue *q,
430 struct cx23885_buffer *buf)
431{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300432 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300433
434 /* setup fifo + format */
435 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
436 buf->bpl, buf->risc.dma);
437 cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
438
439 /* reset counter */
440 cx_write(VID_A_GPCNT_CTL, 3);
441 q->count = 1;
442
443 /* enable irq */
444 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
445 cx_set(VID_A_INT_MSK, 0x000011);
446
447 /* start dma */
448 cx_set(DEV_CNTRL2, (1<<5));
449 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
450
451 return 0;
452}
453
Steven Tothe47f30b2008-01-10 04:25:59 -0300454
455static int cx23885_restart_video_queue(struct cx23885_dev *dev,
456 struct cx23885_dmaqueue *q)
457{
458 struct cx23885_buffer *buf, *prev;
459 struct list_head *item;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300460 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300461
462 if (!list_empty(&q->active)) {
463 buf = list_entry(q->active.next, struct cx23885_buffer,
464 vb.queue);
465 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
466 buf, buf->vb.i);
467 cx23885_start_video_dma(dev, q, buf);
468 list_for_each(item, &q->active) {
469 buf = list_entry(item, struct cx23885_buffer,
470 vb.queue);
471 buf->count = q->count++;
472 }
473 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
474 return 0;
475 }
476
477 prev = NULL;
478 for (;;) {
479 if (list_empty(&q->queued))
480 return 0;
481 buf = list_entry(q->queued.next, struct cx23885_buffer,
482 vb.queue);
483 if (NULL == prev) {
484 list_move_tail(&buf->vb.queue, &q->active);
485 cx23885_start_video_dma(dev, q, buf);
486 buf->vb.state = VIDEOBUF_ACTIVE;
487 buf->count = q->count++;
488 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
489 dprintk(2, "[%p/%d] restart_queue - first active\n",
490 buf, buf->vb.i);
491
492 } else if (prev->vb.width == buf->vb.width &&
493 prev->vb.height == buf->vb.height &&
494 prev->fmt == buf->fmt) {
495 list_move_tail(&buf->vb.queue, &q->active);
496 buf->vb.state = VIDEOBUF_ACTIVE;
497 buf->count = q->count++;
498 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
499 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
500 dprintk(2, "[%p/%d] restart_queue - move to active\n",
501 buf, buf->vb.i);
502 } else {
503 return 0;
504 }
505 prev = buf;
506 }
507}
508
509static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
510 unsigned int *size)
511{
512 struct cx23885_fh *fh = q->priv_data;
513
514 *size = fh->fmt->depth*fh->width*fh->height >> 3;
515 if (0 == *count)
516 *count = 32;
517 while (*size * *count > vid_limit * 1024 * 1024)
518 (*count)--;
519 return 0;
520}
521
522static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
523 enum v4l2_field field)
524{
525 struct cx23885_fh *fh = q->priv_data;
526 struct cx23885_dev *dev = fh->dev;
527 struct cx23885_buffer *buf =
528 container_of(vb, struct cx23885_buffer, vb);
529 int rc, init_buffer = 0;
530 u32 line0_offset, line1_offset;
531 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
532
533 BUG_ON(NULL == fh->fmt);
534 if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
535 fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
536 return -EINVAL;
537 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
538 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
539 return -EINVAL;
540
541 if (buf->fmt != fh->fmt ||
542 buf->vb.width != fh->width ||
543 buf->vb.height != fh->height ||
544 buf->vb.field != field) {
545 buf->fmt = fh->fmt;
546 buf->vb.width = fh->width;
547 buf->vb.height = fh->height;
548 buf->vb.field = field;
549 init_buffer = 1;
550 }
551
552 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
553 init_buffer = 1;
554 rc = videobuf_iolock(q, &buf->vb, NULL);
555 if (0 != rc)
556 goto fail;
557 }
558
559 if (init_buffer) {
560 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
561 switch (buf->vb.field) {
562 case V4L2_FIELD_TOP:
563 cx23885_risc_buffer(dev->pci, &buf->risc,
564 dma->sglist, 0, UNSET,
565 buf->bpl, 0, buf->vb.height);
566 break;
567 case V4L2_FIELD_BOTTOM:
568 cx23885_risc_buffer(dev->pci, &buf->risc,
569 dma->sglist, UNSET, 0,
570 buf->bpl, 0, buf->vb.height);
571 break;
572 case V4L2_FIELD_INTERLACED:
573 if (dev->tvnorm & V4L2_STD_NTSC) {
574 /* cx25840 transmits NTSC bottom field first */
575 dprintk(1, "%s() Creating NTSC risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300576 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300577 line0_offset = buf->bpl;
578 line1_offset = 0;
579 } else {
580 /* All other formats are top field first */
581 dprintk(1, "%s() Creating PAL/SECAM risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300582 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300583 line0_offset = 0;
584 line1_offset = buf->bpl;
585 }
586 cx23885_risc_buffer(dev->pci, &buf->risc,
587 dma->sglist, line0_offset,
588 line1_offset,
589 buf->bpl, buf->bpl,
590 buf->vb.height >> 1);
591 break;
592 case V4L2_FIELD_SEQ_TB:
593 cx23885_risc_buffer(dev->pci, &buf->risc,
594 dma->sglist,
595 0, buf->bpl * (buf->vb.height >> 1),
596 buf->bpl, 0,
597 buf->vb.height >> 1);
598 break;
599 case V4L2_FIELD_SEQ_BT:
600 cx23885_risc_buffer(dev->pci, &buf->risc,
601 dma->sglist,
602 buf->bpl * (buf->vb.height >> 1), 0,
603 buf->bpl, 0,
604 buf->vb.height >> 1);
605 break;
606 default:
607 BUG();
608 }
609 }
610 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
611 buf, buf->vb.i,
612 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
613 (unsigned long)buf->risc.dma);
614
615 buf->vb.state = VIDEOBUF_PREPARED;
616 return 0;
617
618 fail:
619 cx23885_free_buffer(q, buf);
620 return rc;
621}
622
623static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
624{
625 struct cx23885_buffer *buf = container_of(vb,
626 struct cx23885_buffer, vb);
627 struct cx23885_buffer *prev;
628 struct cx23885_fh *fh = vq->priv_data;
629 struct cx23885_dev *dev = fh->dev;
630 struct cx23885_dmaqueue *q = &dev->vidq;
631
632 /* add jump to stopper */
633 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
634 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
635 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
636
637 if (!list_empty(&q->queued)) {
638 list_add_tail(&buf->vb.queue, &q->queued);
639 buf->vb.state = VIDEOBUF_QUEUED;
640 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
641 buf, buf->vb.i);
642
643 } else if (list_empty(&q->active)) {
644 list_add_tail(&buf->vb.queue, &q->active);
645 cx23885_start_video_dma(dev, q, buf);
646 buf->vb.state = VIDEOBUF_ACTIVE;
647 buf->count = q->count++;
648 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
649 dprintk(2, "[%p/%d] buffer_queue - first active\n",
650 buf, buf->vb.i);
651
652 } else {
653 prev = list_entry(q->active.prev, struct cx23885_buffer,
654 vb.queue);
655 if (prev->vb.width == buf->vb.width &&
656 prev->vb.height == buf->vb.height &&
657 prev->fmt == buf->fmt) {
658 list_add_tail(&buf->vb.queue, &q->active);
659 buf->vb.state = VIDEOBUF_ACTIVE;
660 buf->count = q->count++;
661 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
662 /* 64 bit bits 63-32 */
663 prev->risc.jmp[2] = cpu_to_le32(0);
664 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
665 buf, buf->vb.i);
666
667 } else {
668 list_add_tail(&buf->vb.queue, &q->queued);
669 buf->vb.state = VIDEOBUF_QUEUED;
670 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
671 buf, buf->vb.i);
672 }
673 }
674}
675
676static void buffer_release(struct videobuf_queue *q,
677 struct videobuf_buffer *vb)
678{
679 struct cx23885_buffer *buf = container_of(vb,
680 struct cx23885_buffer, vb);
681
682 cx23885_free_buffer(q, buf);
683}
684
685static struct videobuf_queue_ops cx23885_video_qops = {
686 .buf_setup = buffer_setup,
687 .buf_prepare = buffer_prepare,
688 .buf_queue = buffer_queue,
689 .buf_release = buffer_release,
690};
691
692static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
693{
694 switch (fh->type) {
695 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
696 return &fh->vidq;
697 case V4L2_BUF_TYPE_VBI_CAPTURE:
698 return &fh->vbiq;
699 default:
700 BUG();
701 return NULL;
702 }
703}
704
705static int get_resource(struct cx23885_fh *fh)
706{
707 switch (fh->type) {
708 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
709 return RESOURCE_VIDEO;
710 case V4L2_BUF_TYPE_VBI_CAPTURE:
711 return RESOURCE_VBI;
712 default:
713 BUG();
714 return 0;
715 }
716}
717
Hans Verkuilbec43662008-12-30 06:58:20 -0300718static int video_open(struct file *file)
Steven Tothe47f30b2008-01-10 04:25:59 -0300719{
Hans Verkuilbec43662008-12-30 06:58:20 -0300720 int minor = video_devdata(file)->minor;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200721 struct video_device *vdev = video_devdata(file);
722 struct cx23885_dev *dev = video_drvdata(file);
Steven Tothe47f30b2008-01-10 04:25:59 -0300723 struct cx23885_fh *fh;
Steven Tothe47f30b2008-01-10 04:25:59 -0300724 enum v4l2_buf_type type = 0;
725 int radio = 0;
726
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200727 switch (vdev->vfl_type) {
728 case VFL_TYPE_GRABBER:
729 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
730 break;
731 case VFL_TYPE_VBI:
732 type = V4L2_BUF_TYPE_VBI_CAPTURE;
733 break;
734 case VFL_TYPE_RADIO:
735 radio = 1;
736 break;
Hans Verkuild56dc612008-07-30 08:43:36 -0300737 }
Steven Tothe47f30b2008-01-10 04:25:59 -0300738
739 dprintk(1, "open minor=%d radio=%d type=%s\n",
740 minor, radio, v4l2_type_names[type]);
741
742 /* allocate + initialize per filehandle data */
743 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200744 if (NULL == fh)
Steven Tothe47f30b2008-01-10 04:25:59 -0300745 return -ENOMEM;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200746
747 lock_kernel();
748
Steven Tothe47f30b2008-01-10 04:25:59 -0300749 file->private_data = fh;
750 fh->dev = dev;
751 fh->radio = radio;
752 fh->type = type;
753 fh->width = 320;
754 fh->height = 240;
755 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
756
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300757 videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
758 &dev->pci->dev, &dev->slock,
Steven Tothe47f30b2008-01-10 04:25:59 -0300759 V4L2_BUF_TYPE_VIDEO_CAPTURE,
760 V4L2_FIELD_INTERLACED,
761 sizeof(struct cx23885_buffer),
762 fh);
763
764 dprintk(1, "post videobuf_queue_init()\n");
765
Hans Verkuild56dc612008-07-30 08:43:36 -0300766 unlock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300767
768 return 0;
769}
770
771static ssize_t video_read(struct file *file, char __user *data,
772 size_t count, loff_t *ppos)
773{
774 struct cx23885_fh *fh = file->private_data;
775
776 switch (fh->type) {
777 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
778 if (res_locked(fh->dev, RESOURCE_VIDEO))
779 return -EBUSY;
780 return videobuf_read_one(&fh->vidq, data, count, ppos,
781 file->f_flags & O_NONBLOCK);
782 case V4L2_BUF_TYPE_VBI_CAPTURE:
783 if (!res_get(fh->dev, fh, RESOURCE_VBI))
784 return -EBUSY;
785 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
786 file->f_flags & O_NONBLOCK);
787 default:
788 BUG();
789 return 0;
790 }
791}
792
793static unsigned int video_poll(struct file *file,
794 struct poll_table_struct *wait)
795{
796 struct cx23885_fh *fh = file->private_data;
797 struct cx23885_buffer *buf;
Figo.zhang9fd64182009-06-16 13:31:29 -0300798 unsigned int rc = POLLERR;
Steven Tothe47f30b2008-01-10 04:25:59 -0300799
800 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
801 if (!res_get(fh->dev, fh, RESOURCE_VBI))
802 return POLLERR;
803 return videobuf_poll_stream(file, &fh->vbiq, wait);
804 }
805
Figo.zhang9fd64182009-06-16 13:31:29 -0300806 mutex_lock(&fh->vidq.vb_lock);
Steven Tothe47f30b2008-01-10 04:25:59 -0300807 if (res_check(fh, RESOURCE_VIDEO)) {
808 /* streaming capture */
809 if (list_empty(&fh->vidq.stream))
Figo.zhang9fd64182009-06-16 13:31:29 -0300810 goto done;
Steven Tothe47f30b2008-01-10 04:25:59 -0300811 buf = list_entry(fh->vidq.stream.next,
812 struct cx23885_buffer, vb.stream);
813 } else {
814 /* read() capture */
815 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
816 if (NULL == buf)
Figo.zhang9fd64182009-06-16 13:31:29 -0300817 goto done;
Steven Tothe47f30b2008-01-10 04:25:59 -0300818 }
819 poll_wait(file, &buf->vb.done, wait);
820 if (buf->vb.state == VIDEOBUF_DONE ||
821 buf->vb.state == VIDEOBUF_ERROR)
Figo.zhang9fd64182009-06-16 13:31:29 -0300822 rc = POLLIN|POLLRDNORM;
823 else
824 rc = 0;
825done:
826 mutex_unlock(&fh->vidq.vb_lock);
827 return rc;
Steven Tothe47f30b2008-01-10 04:25:59 -0300828}
829
Hans Verkuilbec43662008-12-30 06:58:20 -0300830static int video_release(struct file *file)
Steven Tothe47f30b2008-01-10 04:25:59 -0300831{
832 struct cx23885_fh *fh = file->private_data;
833 struct cx23885_dev *dev = fh->dev;
834
835 /* turn off overlay */
836 if (res_check(fh, RESOURCE_OVERLAY)) {
837 /* FIXME */
838 res_free(dev, fh, RESOURCE_OVERLAY);
839 }
840
841 /* stop video capture */
842 if (res_check(fh, RESOURCE_VIDEO)) {
843 videobuf_queue_cancel(&fh->vidq);
844 res_free(dev, fh, RESOURCE_VIDEO);
845 }
846 if (fh->vidq.read_buf) {
847 buffer_release(&fh->vidq, fh->vidq.read_buf);
848 kfree(fh->vidq.read_buf);
849 }
850
851 /* stop vbi capture */
852 if (res_check(fh, RESOURCE_VBI)) {
853 if (fh->vbiq.streaming)
854 videobuf_streamoff(&fh->vbiq);
855 if (fh->vbiq.reading)
856 videobuf_read_stop(&fh->vbiq);
857 res_free(dev, fh, RESOURCE_VBI);
858 }
859
860 videobuf_mmap_free(&fh->vidq);
861 file->private_data = NULL;
862 kfree(fh);
863
Steven Totha19602f22008-01-10 11:43:18 -0300864 /* We are not putting the tuner to sleep here on exit, because
865 * we want to use the mpeg encoder in another session to capture
866 * tuner video. Closing this will result in no video to the encoder.
867 */
Steven Tothe47f30b2008-01-10 04:25:59 -0300868
869 return 0;
870}
871
872static int video_mmap(struct file *file, struct vm_area_struct *vma)
873{
874 struct cx23885_fh *fh = file->private_data;
875
876 return videobuf_mmap_mapper(get_queue(fh), vma);
877}
878
879/* ------------------------------------------------------------------ */
880/* VIDEO CTRL IOCTLS */
881
Steven Toth9c8ced52008-10-16 20:18:44 -0300882static int cx23885_get_control(struct cx23885_dev *dev,
883 struct v4l2_control *ctl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300884{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300885 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -0300886 call_all(dev, core, g_ctrl, ctl);
Steven Tothe47f30b2008-01-10 04:25:59 -0300887 return 0;
888}
Steven Tothe47f30b2008-01-10 04:25:59 -0300889
Steven Toth9c8ced52008-10-16 20:18:44 -0300890static int cx23885_set_control(struct cx23885_dev *dev,
891 struct v4l2_control *ctl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300892{
893 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
Harvey Harrison22b4e642008-04-08 23:20:00 -0300894 " (disabled - no action)\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300895 return 0;
896}
Steven Tothe47f30b2008-01-10 04:25:59 -0300897
898static void init_controls(struct cx23885_dev *dev)
899{
900 struct v4l2_control ctrl;
901 int i;
902
903 for (i = 0; i < CX23885_CTLS; i++) {
904 ctrl.id = cx23885_ctls[i].v.id;
905 ctrl.value = cx23885_ctls[i].v.default_value;
906
907 cx23885_set_control(dev, &ctrl);
908 }
909}
910
911/* ------------------------------------------------------------------ */
912/* VIDEO IOCTLS */
913
Hans Verkuil78b526a2008-05-28 12:16:41 -0300914static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300915 struct v4l2_format *f)
916{
917 struct cx23885_fh *fh = priv;
918
919 f->fmt.pix.width = fh->width;
920 f->fmt.pix.height = fh->height;
921 f->fmt.pix.field = fh->vidq.field;
922 f->fmt.pix.pixelformat = fh->fmt->fourcc;
923 f->fmt.pix.bytesperline =
924 (f->fmt.pix.width * fh->fmt->depth) >> 3;
925 f->fmt.pix.sizeimage =
926 f->fmt.pix.height * f->fmt.pix.bytesperline;
927
928 return 0;
929}
930
Hans Verkuil78b526a2008-05-28 12:16:41 -0300931static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300932 struct v4l2_format *f)
933{
934 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
935 struct cx23885_fmt *fmt;
936 enum v4l2_field field;
937 unsigned int maxw, maxh;
938
939 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
940 if (NULL == fmt)
941 return -EINVAL;
942
943 field = f->fmt.pix.field;
944 maxw = norm_maxw(dev->tvnorm);
945 maxh = norm_maxh(dev->tvnorm);
946
947 if (V4L2_FIELD_ANY == field) {
948 field = (f->fmt.pix.height > maxh/2)
949 ? V4L2_FIELD_INTERLACED
950 : V4L2_FIELD_BOTTOM;
951 }
952
953 switch (field) {
954 case V4L2_FIELD_TOP:
955 case V4L2_FIELD_BOTTOM:
956 maxh = maxh / 2;
957 break;
958 case V4L2_FIELD_INTERLACED:
959 break;
960 default:
961 return -EINVAL;
962 }
963
964 f->fmt.pix.field = field;
Trent Piepho2449afc2009-05-30 21:45:46 -0300965 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
966 &f->fmt.pix.height, 32, maxh, 0, 0);
Steven Tothe47f30b2008-01-10 04:25:59 -0300967 f->fmt.pix.bytesperline =
968 (f->fmt.pix.width * fmt->depth) >> 3;
969 f->fmt.pix.sizeimage =
970 f->fmt.pix.height * f->fmt.pix.bytesperline;
971
972 return 0;
973}
974
Hans Verkuil78b526a2008-05-28 12:16:41 -0300975static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300976 struct v4l2_format *f)
977{
978 struct cx23885_fh *fh = priv;
979 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
980 int err;
981
Harvey Harrison22b4e642008-04-08 23:20:00 -0300982 dprintk(2, "%s()\n", __func__);
Hans Verkuil78b526a2008-05-28 12:16:41 -0300983 err = vidioc_try_fmt_vid_cap(file, priv, f);
Steven Tothe47f30b2008-01-10 04:25:59 -0300984
985 if (0 != err)
986 return err;
987 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
988 fh->width = f->fmt.pix.width;
989 fh->height = f->fmt.pix.height;
990 fh->vidq.field = f->fmt.pix.field;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300991 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -0300992 fh->width, fh->height, fh->vidq.field);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -0300993 call_all(dev, video, s_fmt, f);
Steven Tothe47f30b2008-01-10 04:25:59 -0300994 return 0;
995}
996
997static int vidioc_querycap(struct file *file, void *priv,
998 struct v4l2_capability *cap)
999{
1000 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1001
1002 strcpy(cap->driver, "cx23885");
1003 strlcpy(cap->card, cx23885_boards[dev->board].name,
1004 sizeof(cap->card));
1005 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1006 cap->version = CX23885_VERSION_CODE;
1007 cap->capabilities =
1008 V4L2_CAP_VIDEO_CAPTURE |
1009 V4L2_CAP_READWRITE |
1010 V4L2_CAP_STREAMING |
1011 V4L2_CAP_VBI_CAPTURE;
1012 if (UNSET != dev->tuner_type)
1013 cap->capabilities |= V4L2_CAP_TUNER;
1014 return 0;
1015}
1016
Hans Verkuil78b526a2008-05-28 12:16:41 -03001017static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001018 struct v4l2_fmtdesc *f)
1019{
1020 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1021 return -EINVAL;
1022
1023 strlcpy(f->description, formats[f->index].name,
1024 sizeof(f->description));
1025 f->pixelformat = formats[f->index].fourcc;
1026
1027 return 0;
1028}
1029
1030#ifdef CONFIG_VIDEO_V4L1_COMPAT
1031static int vidiocgmbuf(struct file *file, void *priv,
1032 struct video_mbuf *mbuf)
1033{
1034 struct cx23885_fh *fh = priv;
1035 struct videobuf_queue *q;
1036 struct v4l2_requestbuffers req;
1037 unsigned int i;
1038 int err;
1039
1040 q = get_queue(fh);
1041 memset(&req, 0, sizeof(req));
1042 req.type = q->type;
1043 req.count = 8;
1044 req.memory = V4L2_MEMORY_MMAP;
1045 err = videobuf_reqbufs(q, &req);
1046 if (err < 0)
1047 return err;
1048
1049 mbuf->frames = req.count;
1050 mbuf->size = 0;
1051 for (i = 0; i < mbuf->frames; i++) {
1052 mbuf->offsets[i] = q->bufs[i]->boff;
1053 mbuf->size += q->bufs[i]->bsize;
1054 }
1055 return 0;
1056}
1057#endif
1058
1059static int vidioc_reqbufs(struct file *file, void *priv,
1060 struct v4l2_requestbuffers *p)
1061{
1062 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001063 return videobuf_reqbufs(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001064}
1065
1066static int vidioc_querybuf(struct file *file, void *priv,
1067 struct v4l2_buffer *p)
1068{
1069 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001070 return videobuf_querybuf(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001071}
1072
1073static int vidioc_qbuf(struct file *file, void *priv,
1074 struct v4l2_buffer *p)
1075{
1076 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001077 return videobuf_qbuf(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001078}
1079
1080static int vidioc_dqbuf(struct file *file, void *priv,
1081 struct v4l2_buffer *p)
1082{
1083 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001084 return videobuf_dqbuf(get_queue(fh), p,
1085 file->f_flags & O_NONBLOCK);
Steven Tothe47f30b2008-01-10 04:25:59 -03001086}
1087
1088static int vidioc_streamon(struct file *file, void *priv,
1089 enum v4l2_buf_type i)
1090{
1091 struct cx23885_fh *fh = priv;
1092 struct cx23885_dev *dev = fh->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001093 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001094
1095 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1096 return -EINVAL;
1097 if (unlikely(i != fh->type))
1098 return -EINVAL;
1099
1100 if (unlikely(!res_get(dev, fh, get_resource(fh))))
1101 return -EBUSY;
1102 return videobuf_streamon(get_queue(fh));
1103}
1104
1105static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1106{
1107 struct cx23885_fh *fh = priv;
1108 struct cx23885_dev *dev = fh->dev;
1109 int err, res;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001110 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001111
1112 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1113 return -EINVAL;
1114 if (i != fh->type)
1115 return -EINVAL;
1116
1117 res = get_resource(fh);
1118 err = videobuf_streamoff(get_queue(fh));
1119 if (err < 0)
1120 return err;
1121 res_free(dev, fh, res);
1122 return 0;
1123}
1124
1125static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1126{
1127 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001128 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001129
1130 mutex_lock(&dev->lock);
1131 cx23885_set_tvnorm(dev, *tvnorms);
1132 mutex_unlock(&dev->lock);
1133
1134 return 0;
1135}
1136
Hans Verkuild45b9b82008-09-04 03:33:43 -03001137static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
Steven Tothe47f30b2008-01-10 04:25:59 -03001138{
1139 static const char *iname[] = {
1140 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1141 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1142 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1143 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1144 [CX23885_VMUX_SVIDEO] = "S-Video",
David T.L. Wongdac65fa2009-10-21 11:07:24 -03001145 [CX23885_VMUX_COMPONENT] = "Component",
Steven Tothe47f30b2008-01-10 04:25:59 -03001146 [CX23885_VMUX_TELEVISION] = "Television",
1147 [CX23885_VMUX_CABLE] = "Cable TV",
1148 [CX23885_VMUX_DVB] = "DVB",
1149 [CX23885_VMUX_DEBUG] = "for debug only",
1150 };
1151 unsigned int n;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001152 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001153
1154 n = i->index;
1155 if (n >= 4)
1156 return -EINVAL;
1157
1158 if (0 == INPUT(n)->type)
1159 return -EINVAL;
1160
1161 memset(i, 0, sizeof(*i));
1162 i->index = n;
1163 i->type = V4L2_INPUT_TYPE_CAMERA;
1164 strcpy(i->name, iname[INPUT(n)->type]);
1165 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
1166 (CX23885_VMUX_CABLE == INPUT(n)->type))
1167 i->type = V4L2_INPUT_TYPE_TUNER;
1168 i->std = CX23885_NORMS;
1169 return 0;
1170}
Steven Tothe47f30b2008-01-10 04:25:59 -03001171
1172static int vidioc_enum_input(struct file *file, void *priv,
1173 struct v4l2_input *i)
1174{
1175 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001176 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001177 return cx23885_enum_input(dev, i);
1178}
1179
1180static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1181{
1182 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1183
1184 *i = dev->input;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001185 dprintk(1, "%s() returns %d\n", __func__, *i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001186 return 0;
1187}
1188
1189static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1190{
1191 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1192
Harvey Harrison22b4e642008-04-08 23:20:00 -03001193 dprintk(1, "%s(%d)\n", __func__, i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001194
1195 if (i >= 4) {
Harvey Harrison22b4e642008-04-08 23:20:00 -03001196 dprintk(1, "%s() -EINVAL\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001197 return -EINVAL;
1198 }
1199
1200 mutex_lock(&dev->lock);
1201 cx23885_video_mux(dev, i);
1202 mutex_unlock(&dev->lock);
1203 return 0;
1204}
1205
1206static int vidioc_queryctrl(struct file *file, void *priv,
1207 struct v4l2_queryctrl *qctrl)
1208{
1209 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1210 if (unlikely(qctrl->id == 0))
1211 return -EINVAL;
1212 return cx23885_ctrl_query(qctrl);
1213}
1214
1215static int vidioc_g_ctrl(struct file *file, void *priv,
1216 struct v4l2_control *ctl)
1217{
1218 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1219
1220 return cx23885_get_control(dev, ctl);
1221}
1222
1223static int vidioc_s_ctrl(struct file *file, void *priv,
1224 struct v4l2_control *ctl)
1225{
1226 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1227
1228 return cx23885_set_control(dev, ctl);
1229}
1230
1231static int vidioc_g_tuner(struct file *file, void *priv,
1232 struct v4l2_tuner *t)
1233{
1234 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1235
1236 if (unlikely(UNSET == dev->tuner_type))
1237 return -EINVAL;
1238 if (0 != t->index)
1239 return -EINVAL;
1240
1241 strcpy(t->name, "Television");
1242 t->type = V4L2_TUNER_ANALOG_TV;
1243 t->capability = V4L2_TUNER_CAP_NORM;
1244 t->rangehigh = 0xffffffffUL;
1245 t->signal = 0xffff ; /* LOCKED */
1246 return 0;
1247}
1248
1249static int vidioc_s_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 (UNSET == dev->tuner_type)
1255 return -EINVAL;
1256 if (0 != t->index)
1257 return -EINVAL;
1258 return 0;
1259}
1260
1261static int vidioc_g_frequency(struct file *file, void *priv,
1262 struct v4l2_frequency *f)
1263{
1264 struct cx23885_fh *fh = priv;
1265 struct cx23885_dev *dev = fh->dev;
1266
1267 if (unlikely(UNSET == dev->tuner_type))
1268 return -EINVAL;
1269
1270 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1271 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1272 f->frequency = dev->freq;
1273
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001274 call_all(dev, tuner, g_frequency, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001275
1276 return 0;
1277}
1278
Hans Verkuild45b9b82008-09-04 03:33:43 -03001279static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
Steven Tothe47f30b2008-01-10 04:25:59 -03001280{
1281 if (unlikely(UNSET == dev->tuner_type))
1282 return -EINVAL;
1283 if (unlikely(f->tuner != 0))
1284 return -EINVAL;
1285
1286 mutex_lock(&dev->lock);
1287 dev->freq = f->frequency;
1288
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001289 call_all(dev, tuner, s_frequency, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001290
1291 /* When changing channels it is required to reset TVAUDIO */
1292 msleep(10);
1293
1294 mutex_unlock(&dev->lock);
1295
1296 return 0;
1297}
Steven Tothe47f30b2008-01-10 04:25:59 -03001298
1299static int vidioc_s_frequency(struct file *file, void *priv,
1300 struct v4l2_frequency *f)
1301{
1302 struct cx23885_fh *fh = priv;
1303 struct cx23885_dev *dev = fh->dev;
1304
1305 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1306 return -EINVAL;
1307 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1308 return -EINVAL;
1309
1310 return
1311 cx23885_set_freq(dev, f);
1312}
1313
Steven Tothe47f30b2008-01-10 04:25:59 -03001314/* ----------------------------------------------------------- */
Steven Tothe47f30b2008-01-10 04:25:59 -03001315
1316static void cx23885_vid_timeout(unsigned long data)
1317{
1318 struct cx23885_dev *dev = (struct cx23885_dev *)data;
1319 struct cx23885_dmaqueue *q = &dev->vidq;
1320 struct cx23885_buffer *buf;
1321 unsigned long flags;
1322
1323 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1324
1325 cx_clear(VID_A_DMA_CTL, 0x11);
1326
1327 spin_lock_irqsave(&dev->slock, flags);
1328 while (!list_empty(&q->active)) {
1329 buf = list_entry(q->active.next,
1330 struct cx23885_buffer, vb.queue);
1331 list_del(&buf->vb.queue);
1332 buf->vb.state = VIDEOBUF_ERROR;
1333 wake_up(&buf->vb.done);
1334 printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
1335 dev->name, buf, buf->vb.i,
1336 (unsigned long)buf->risc.dma);
1337 }
1338 cx23885_restart_video_queue(dev, q);
1339 spin_unlock_irqrestore(&dev->slock, flags);
1340}
1341
1342int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1343{
1344 u32 mask, count;
1345 int handled = 0;
1346
1347 mask = cx_read(VID_A_INT_MSK);
1348 if (0 == (status & mask))
1349 return handled;
1350 cx_write(VID_A_INT_STAT, status);
1351
Harvey Harrison22b4e642008-04-08 23:20:00 -03001352 dprintk(2, "%s() status = 0x%08x\n", __func__, status);
Steven Tothe47f30b2008-01-10 04:25:59 -03001353 /* risc op code error */
1354 if (status & (1 << 16)) {
1355 printk(KERN_WARNING "%s/0: video risc op code error\n",
1356 dev->name);
1357 cx_clear(VID_A_DMA_CTL, 0x11);
1358 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1359 }
1360
1361 /* risc1 y */
1362 if (status & 0x01) {
1363 spin_lock(&dev->slock);
1364 count = cx_read(VID_A_GPCNT);
1365 cx23885_video_wakeup(dev, &dev->vidq, count);
1366 spin_unlock(&dev->slock);
1367 handled++;
1368 }
1369 /* risc2 y */
1370 if (status & 0x10) {
1371 dprintk(2, "stopper video\n");
1372 spin_lock(&dev->slock);
1373 cx23885_restart_video_queue(dev, &dev->vidq);
1374 spin_unlock(&dev->slock);
1375 handled++;
1376 }
1377
1378 return handled;
1379}
1380
Steven Tothe47f30b2008-01-10 04:25:59 -03001381/* ----------------------------------------------------------- */
1382/* exported stuff */
1383
Hans Verkuilbec43662008-12-30 06:58:20 -03001384static const struct v4l2_file_operations video_fops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001385 .owner = THIS_MODULE,
1386 .open = video_open,
1387 .release = video_release,
1388 .read = video_read,
1389 .poll = video_poll,
1390 .mmap = video_mmap,
1391 .ioctl = video_ioctl2,
Steven Tothe47f30b2008-01-10 04:25:59 -03001392};
1393
Hans Verkuila3998102008-07-21 02:57:38 -03001394static const struct v4l2_ioctl_ops video_ioctl_ops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001395 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001396 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1397 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1398 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1399 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1400 .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
1401 .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
1402 .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
Steven Tothe47f30b2008-01-10 04:25:59 -03001403 .vidioc_reqbufs = vidioc_reqbufs,
1404 .vidioc_querybuf = vidioc_querybuf,
1405 .vidioc_qbuf = vidioc_qbuf,
1406 .vidioc_dqbuf = vidioc_dqbuf,
1407 .vidioc_s_std = vidioc_s_std,
1408 .vidioc_enum_input = vidioc_enum_input,
1409 .vidioc_g_input = vidioc_g_input,
1410 .vidioc_s_input = vidioc_s_input,
1411 .vidioc_queryctrl = vidioc_queryctrl,
1412 .vidioc_g_ctrl = vidioc_g_ctrl,
1413 .vidioc_s_ctrl = vidioc_s_ctrl,
1414 .vidioc_streamon = vidioc_streamon,
1415 .vidioc_streamoff = vidioc_streamoff,
1416#ifdef CONFIG_VIDEO_V4L1_COMPAT
1417 .vidiocgmbuf = vidiocgmbuf,
1418#endif
1419 .vidioc_g_tuner = vidioc_g_tuner,
1420 .vidioc_s_tuner = vidioc_s_tuner,
1421 .vidioc_g_frequency = vidioc_g_frequency,
1422 .vidioc_s_frequency = vidioc_s_frequency,
Andy Walls74618242009-09-26 22:50:44 -03001423 .vidioc_g_chip_ident = cx23885_g_chip_ident,
Steven Tothe47f30b2008-01-10 04:25:59 -03001424#ifdef CONFIG_VIDEO_ADV_DEBUG
Andy Walls74618242009-09-26 22:50:44 -03001425 .vidioc_g_register = cx23885_g_register,
1426 .vidioc_s_register = cx23885_s_register,
Steven Tothe47f30b2008-01-10 04:25:59 -03001427#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001428};
1429
1430static struct video_device cx23885_vbi_template;
1431static struct video_device cx23885_video_template = {
1432 .name = "cx23885-video",
Hans Verkuila3998102008-07-21 02:57:38 -03001433 .fops = &video_fops,
1434 .minor = -1,
1435 .ioctl_ops = &video_ioctl_ops,
Steven Tothe47f30b2008-01-10 04:25:59 -03001436 .tvnorms = CX23885_NORMS,
1437 .current_norm = V4L2_STD_NTSC_M,
1438};
1439
Hans Verkuilbec43662008-12-30 06:58:20 -03001440static const struct v4l2_file_operations radio_fops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001441 .owner = THIS_MODULE,
1442 .open = video_open,
1443 .release = video_release,
1444 .ioctl = video_ioctl2,
Steven Tothe47f30b2008-01-10 04:25:59 -03001445};
1446
1447
1448void cx23885_video_unregister(struct cx23885_dev *dev)
1449{
Harvey Harrison22b4e642008-04-08 23:20:00 -03001450 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001451 cx_clear(PCI_INT_MSK, 1);
1452
1453 if (dev->video_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001454 if (video_is_registered(dev->video_dev))
Steven Tothe47f30b2008-01-10 04:25:59 -03001455 video_unregister_device(dev->video_dev);
1456 else
1457 video_device_release(dev->video_dev);
1458 dev->video_dev = NULL;
1459
1460 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1461 }
1462}
1463
1464int cx23885_video_register(struct cx23885_dev *dev)
1465{
1466 int err;
1467
Harvey Harrison22b4e642008-04-08 23:20:00 -03001468 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001469 spin_lock_init(&dev->slock);
1470
1471 /* Initialize VBI template */
1472 memcpy(&cx23885_vbi_template, &cx23885_video_template,
1473 sizeof(cx23885_vbi_template));
1474 strcpy(cx23885_vbi_template.name, "cx23885-vbi");
Steven Tothe47f30b2008-01-10 04:25:59 -03001475
1476 dev->tvnorm = cx23885_video_template.current_norm;
1477
1478 /* init video dma queues */
1479 INIT_LIST_HEAD(&dev->vidq.active);
1480 INIT_LIST_HEAD(&dev->vidq.queued);
1481 dev->vidq.timeout.function = cx23885_vid_timeout;
1482 dev->vidq.timeout.data = (unsigned long)dev;
1483 init_timer(&dev->vidq.timeout);
1484 cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1485 VID_A_DMA_CTL, 0x11, 0x00);
1486
Steven Totha19602f22008-01-10 11:43:18 -03001487 /* Don't enable VBI yet */
Steven Tothe47f30b2008-01-10 04:25:59 -03001488 cx_set(PCI_INT_MSK, 1);
1489
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001490 if (TUNER_ABSENT != dev->tuner_type) {
1491 struct v4l2_subdev *sd = NULL;
1492
1493 if (dev->tuner_addr)
Hans Verkuile6574f22009-04-01 03:57:53 -03001494 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1495 &dev->i2c_bus[1].i2c_adap,
Hans Verkuil53dacb12009-08-10 02:49:08 -03001496 "tuner", "tuner", dev->tuner_addr, NULL);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001497 else
Hans Verkuil53dacb12009-08-10 02:49:08 -03001498 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
Hans Verkuile6574f22009-04-01 03:57:53 -03001499 &dev->i2c_bus[1].i2c_adap,
Hans Verkuil53dacb12009-08-10 02:49:08 -03001500 "tuner", "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001501 if (sd) {
1502 struct tuner_setup tun_setup;
1503
David T.L. Wongfd705e72009-10-21 11:08:30 -03001504 memset(&tun_setup, 0, sizeof(tun_setup));
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001505 tun_setup.mode_mask = T_ANALOG_TV;
1506 tun_setup.type = dev->tuner_type;
1507 tun_setup.addr = v4l2_i2c_subdev_addr(sd);
David T.L. Wong6f0d8c02009-10-21 13:15:30 -03001508 tun_setup.tuner_callback = cx23885_tuner_callback;
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001509
1510 v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1511 }
1512 }
1513
Steven Tothe47f30b2008-01-10 04:25:59 -03001514
1515 /* register v4l devices */
1516 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1517 &cx23885_video_template, "video");
1518 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1519 video_nr[dev->nr]);
1520 if (err < 0) {
1521 printk(KERN_INFO "%s: can't register video device\n",
1522 dev->name);
1523 goto fail_unreg;
1524 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001525 printk(KERN_INFO "%s/0: registered device %s [v4l2]\n",
1526 dev->name, video_device_node_name(dev->video_dev));
Steven Tothe47f30b2008-01-10 04:25:59 -03001527 /* initial device configuration */
1528 mutex_lock(&dev->lock);
1529 cx23885_set_tvnorm(dev, dev->tvnorm);
1530 init_controls(dev);
1531 cx23885_video_mux(dev, 0);
1532 mutex_unlock(&dev->lock);
1533
Steven Tothe47f30b2008-01-10 04:25:59 -03001534 return 0;
1535
1536fail_unreg:
1537 cx23885_video_unregister(dev);
1538 return err;
1539}
1540