blob: 70836af3ab4891a624a87225d6ec0bf2cdd8cf32 [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
Steven Tothe47f30b2008-01-10 04:25:59 -030038MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
Steven Toth6d897612008-09-03 17:12:12 -030039MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
Steven Tothe47f30b2008-01-10 04:25:59 -030040MODULE_LICENSE("GPL");
41
42/* ------------------------------------------------------------------ */
43
44static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
45static unsigned int vbi_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
46static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
47
48module_param_array(video_nr, int, NULL, 0444);
49module_param_array(vbi_nr, int, NULL, 0444);
50module_param_array(radio_nr, int, NULL, 0444);
51
52MODULE_PARM_DESC(video_nr, "video device numbers");
53MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
54MODULE_PARM_DESC(radio_nr, "radio device numbers");
55
Steven Toth4513fc692008-01-12 11:36:36 -030056static unsigned int video_debug;
Steven Tothe47f30b2008-01-10 04:25:59 -030057module_param(video_debug, int, 0644);
58MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
59
Steven Toth4513fc692008-01-12 11:36:36 -030060static unsigned int irq_debug;
Steven Tothe47f30b2008-01-10 04:25:59 -030061module_param(irq_debug, int, 0644);
62MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
63
64static unsigned int vid_limit = 16;
65module_param(vid_limit, int, 0644);
66MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
67
68#define dprintk(level, fmt, arg...)\
Steven Toth4513fc692008-01-12 11:36:36 -030069 do { if (video_debug >= level)\
70 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
71 } while (0)
Steven Tothe47f30b2008-01-10 04:25:59 -030072
73/* ------------------------------------------------------------------- */
74/* static data */
75
76#define FORMAT_FLAGS_PACKED 0x01
77
78static struct cx23885_fmt formats[] = {
79 {
80 .name = "8 bpp, gray",
81 .fourcc = V4L2_PIX_FMT_GREY,
82 .depth = 8,
83 .flags = FORMAT_FLAGS_PACKED,
84 }, {
85 .name = "15 bpp RGB, le",
86 .fourcc = V4L2_PIX_FMT_RGB555,
87 .depth = 16,
88 .flags = FORMAT_FLAGS_PACKED,
89 }, {
90 .name = "15 bpp RGB, be",
91 .fourcc = V4L2_PIX_FMT_RGB555X,
92 .depth = 16,
93 .flags = FORMAT_FLAGS_PACKED,
94 }, {
95 .name = "16 bpp RGB, le",
96 .fourcc = V4L2_PIX_FMT_RGB565,
97 .depth = 16,
98 .flags = FORMAT_FLAGS_PACKED,
99 }, {
100 .name = "16 bpp RGB, be",
101 .fourcc = V4L2_PIX_FMT_RGB565X,
102 .depth = 16,
103 .flags = FORMAT_FLAGS_PACKED,
104 }, {
105 .name = "24 bpp RGB, le",
106 .fourcc = V4L2_PIX_FMT_BGR24,
107 .depth = 24,
108 .flags = FORMAT_FLAGS_PACKED,
109 }, {
110 .name = "32 bpp RGB, le",
111 .fourcc = V4L2_PIX_FMT_BGR32,
112 .depth = 32,
113 .flags = FORMAT_FLAGS_PACKED,
114 }, {
115 .name = "32 bpp RGB, be",
116 .fourcc = V4L2_PIX_FMT_RGB32,
117 .depth = 32,
118 .flags = FORMAT_FLAGS_PACKED,
119 }, {
120 .name = "4:2:2, packed, YUYV",
121 .fourcc = V4L2_PIX_FMT_YUYV,
122 .depth = 16,
123 .flags = FORMAT_FLAGS_PACKED,
124 }, {
125 .name = "4:2:2, packed, UYVY",
126 .fourcc = V4L2_PIX_FMT_UYVY,
127 .depth = 16,
128 .flags = FORMAT_FLAGS_PACKED,
129 },
130};
131
132static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
133{
134 unsigned int i;
135
136 for (i = 0; i < ARRAY_SIZE(formats); i++)
137 if (formats[i].fourcc == fourcc)
138 return formats+i;
139
Harvey Harrison22b4e642008-04-08 23:20:00 -0300140 printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __func__, fourcc);
Steven Tothe47f30b2008-01-10 04:25:59 -0300141 return NULL;
142}
143
144/* ------------------------------------------------------------------- */
145
146static const struct v4l2_queryctrl no_ctl = {
147 .name = "42",
148 .flags = V4L2_CTRL_FLAG_DISABLED,
149};
150
151static struct cx23885_ctrl cx23885_ctls[] = {
152 /* --- video --- */
153 {
154 .v = {
155 .id = V4L2_CID_BRIGHTNESS,
156 .name = "Brightness",
157 .minimum = 0x00,
158 .maximum = 0xff,
159 .step = 1,
160 .default_value = 0x7f,
161 .type = V4L2_CTRL_TYPE_INTEGER,
162 },
163 .off = 128,
164 .reg = LUMA_CTRL,
165 .mask = 0x00ff,
166 .shift = 0,
167 }, {
168 .v = {
169 .id = V4L2_CID_CONTRAST,
170 .name = "Contrast",
171 .minimum = 0,
172 .maximum = 0xff,
173 .step = 1,
174 .default_value = 0x3f,
175 .type = V4L2_CTRL_TYPE_INTEGER,
176 },
177 .off = 0,
178 .reg = LUMA_CTRL,
179 .mask = 0xff00,
180 .shift = 8,
181 }, {
182 .v = {
183 .id = V4L2_CID_HUE,
184 .name = "Hue",
185 .minimum = 0,
186 .maximum = 0xff,
187 .step = 1,
188 .default_value = 0x7f,
189 .type = V4L2_CTRL_TYPE_INTEGER,
190 },
191 .off = 128,
192 .reg = CHROMA_CTRL,
193 .mask = 0xff0000,
194 .shift = 16,
195 }, {
196 /* strictly, this only describes only U saturation.
197 * V saturation is handled specially through code.
198 */
199 .v = {
200 .id = V4L2_CID_SATURATION,
201 .name = "Saturation",
202 .minimum = 0,
203 .maximum = 0xff,
204 .step = 1,
205 .default_value = 0x7f,
206 .type = V4L2_CTRL_TYPE_INTEGER,
207 },
208 .off = 0,
209 .reg = CHROMA_CTRL,
210 .mask = 0x00ff,
211 .shift = 0,
212 }, {
213 /* --- audio --- */
214 .v = {
215 .id = V4L2_CID_AUDIO_MUTE,
216 .name = "Mute",
217 .minimum = 0,
218 .maximum = 1,
219 .default_value = 1,
220 .type = V4L2_CTRL_TYPE_BOOLEAN,
221 },
222 .reg = PATH1_CTL1,
223 .mask = (0x1f << 24),
224 .shift = 24,
225 }, {
226 .v = {
227 .id = V4L2_CID_AUDIO_VOLUME,
228 .name = "Volume",
229 .minimum = 0,
230 .maximum = 0x3f,
231 .step = 1,
232 .default_value = 0x3f,
233 .type = V4L2_CTRL_TYPE_INTEGER,
234 },
235 .reg = PATH1_VOL_CTL,
236 .mask = 0xff,
237 .shift = 0,
238 }
239};
240static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
241
Hans Verkuil2ba58892009-02-13 10:57:48 -0300242/* Must be sorted from low to high control ID! */
Hans Verkuild45b9b82008-09-04 03:33:43 -0300243static const u32 cx23885_user_ctrls[] = {
Steven Tothe47f30b2008-01-10 04:25:59 -0300244 V4L2_CID_USER_CLASS,
245 V4L2_CID_BRIGHTNESS,
246 V4L2_CID_CONTRAST,
247 V4L2_CID_SATURATION,
248 V4L2_CID_HUE,
249 V4L2_CID_AUDIO_VOLUME,
250 V4L2_CID_AUDIO_MUTE,
251 0
252};
Steven Tothe47f30b2008-01-10 04:25:59 -0300253
254static const u32 *ctrl_classes[] = {
255 cx23885_user_ctrls,
256 NULL
257};
258
Hans Verkuild45b9b82008-09-04 03:33:43 -0300259static void cx23885_video_wakeup(struct cx23885_dev *dev,
Steven Tothe47f30b2008-01-10 04:25:59 -0300260 struct cx23885_dmaqueue *q, u32 count)
261{
262 struct cx23885_buffer *buf;
263 int bc;
264
265 for (bc = 0;; bc++) {
266 if (list_empty(&q->active))
267 break;
268 buf = list_entry(q->active.next,
269 struct cx23885_buffer, vb.queue);
Steven Totha19602f2008-01-10 11:43:18 -0300270
Steven Tothe47f30b2008-01-10 04:25:59 -0300271 /* count comes from the hw and is is 16bit wide --
272 * this trick handles wrap-arounds correctly for
273 * up to 32767 buffers in flight... */
274 if ((s16) (count - buf->count) < 0)
275 break;
Steven Totha19602f2008-01-10 11:43:18 -0300276
Steven Tothe47f30b2008-01-10 04:25:59 -0300277 do_gettimeofday(&buf->vb.ts);
278 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
279 count, buf->count);
280 buf->vb.state = VIDEOBUF_DONE;
281 list_del(&buf->vb.queue);
282 wake_up(&buf->vb.done);
283 }
Steven Toth9c8ced52008-10-16 20:18:44 -0300284 if (list_empty(&q->active))
Steven Tothe47f30b2008-01-10 04:25:59 -0300285 del_timer(&q->timeout);
Steven Toth9c8ced52008-10-16 20:18:44 -0300286 else
Steven Tothe47f30b2008-01-10 04:25:59 -0300287 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
Steven Tothe47f30b2008-01-10 04:25:59 -0300288 if (bc != 1)
Steven Toth21a78092008-01-10 04:38:59 -0300289 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300290 __func__, bc);
Steven Tothe47f30b2008-01-10 04:25:59 -0300291}
292
Hans Verkuild45b9b82008-09-04 03:33:43 -0300293static int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
Steven Tothe47f30b2008-01-10 04:25:59 -0300294{
295 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300296 __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -0300297 (unsigned int)norm,
298 v4l2_norm_to_name(norm));
299
300 dev->tvnorm = norm;
301
Hans Verkuilf41737e2009-04-01 03:52:39 -0300302 call_all(dev, core, s_std, norm);
Steven Tothe47f30b2008-01-10 04:25:59 -0300303
304 return 0;
305}
306
Hans Verkuild45b9b82008-09-04 03:33:43 -0300307static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
Steven Tothe47f30b2008-01-10 04:25:59 -0300308 struct pci_dev *pci,
309 struct video_device *template,
310 char *type)
311{
312 struct video_device *vfd;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300313 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300314
315 vfd = video_device_alloc();
316 if (NULL == vfd)
317 return NULL;
318 *vfd = *template;
Hans Verkuilc0714f62009-03-13 08:02:43 -0300319 vfd->minor = -1;
320 vfd->v4l2_dev = &dev->v4l2_dev;
Steven Tothe47f30b2008-01-10 04:25:59 -0300321 vfd->release = video_device_release;
322 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
323 dev->name, type, cx23885_boards[dev->board].name);
324 return vfd;
325}
326
Hans Verkuild45b9b82008-09-04 03:33:43 -0300327static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300328{
329 int i;
330
331 if (qctrl->id < V4L2_CID_BASE ||
332 qctrl->id >= V4L2_CID_LASTP1)
333 return -EINVAL;
334 for (i = 0; i < CX23885_CTLS; i++)
335 if (cx23885_ctls[i].v.id == qctrl->id)
336 break;
337 if (i == CX23885_CTLS) {
338 *qctrl = no_ctl;
339 return 0;
340 }
341 *qctrl = cx23885_ctls[i].v;
342 return 0;
343}
Steven Tothe47f30b2008-01-10 04:25:59 -0300344
345/* ------------------------------------------------------------------- */
346/* resource management */
347
348static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
349 unsigned int bit)
350{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300351 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300352 if (fh->resources & bit)
353 /* have it already allocated */
354 return 1;
355
356 /* is it free? */
357 mutex_lock(&dev->lock);
358 if (dev->resources & bit) {
359 /* no, someone else uses it */
360 mutex_unlock(&dev->lock);
361 return 0;
362 }
363 /* it's free, grab it */
364 fh->resources |= bit;
365 dev->resources |= bit;
366 dprintk(1, "res: get %d\n", bit);
367 mutex_unlock(&dev->lock);
368 return 1;
369}
370
371static int res_check(struct cx23885_fh *fh, unsigned int bit)
372{
Steven Toth9c8ced52008-10-16 20:18:44 -0300373 return fh->resources & bit;
Steven Tothe47f30b2008-01-10 04:25:59 -0300374}
375
376static int res_locked(struct cx23885_dev *dev, unsigned int bit)
377{
Steven Toth9c8ced52008-10-16 20:18:44 -0300378 return dev->resources & bit;
Steven Tothe47f30b2008-01-10 04:25:59 -0300379}
380
381static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
382 unsigned int bits)
383{
384 BUG_ON((fh->resources & bits) != bits);
Harvey Harrison22b4e642008-04-08 23:20:00 -0300385 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300386
387 mutex_lock(&dev->lock);
388 fh->resources &= ~bits;
389 dev->resources &= ~bits;
390 dprintk(1, "res: put %d\n", bits);
391 mutex_unlock(&dev->lock);
392}
393
Hans Verkuild45b9b82008-09-04 03:33:43 -0300394static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
Steven Tothe47f30b2008-01-10 04:25:59 -0300395{
Steven Tothe47f30b2008-01-10 04:25:59 -0300396 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 -0300397 __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -0300398 input, INPUT(input)->vmux,
399 INPUT(input)->gpio0, INPUT(input)->gpio1,
400 INPUT(input)->gpio2, INPUT(input)->gpio3);
401 dev->input = input;
402
Steven Tothe47f30b2008-01-10 04:25:59 -0300403 /* Tell the internal A/V decoder */
Hans Verkuil5325b422009-04-02 11:26:22 -0300404 v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
405 INPUT(input)->vmux, 0, 0);
Steven Tothe47f30b2008-01-10 04:25:59 -0300406
407 return 0;
408}
Steven Tothe47f30b2008-01-10 04:25:59 -0300409
410/* ------------------------------------------------------------------ */
Hans Verkuild45b9b82008-09-04 03:33:43 -0300411static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
Steven Tothe47f30b2008-01-10 04:25:59 -0300412 unsigned int height, enum v4l2_field field)
413{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300414 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300415 return 0;
416}
417
418static int cx23885_start_video_dma(struct cx23885_dev *dev,
419 struct cx23885_dmaqueue *q,
420 struct cx23885_buffer *buf)
421{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300422 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300423
424 /* setup fifo + format */
425 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
426 buf->bpl, buf->risc.dma);
427 cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
428
429 /* reset counter */
430 cx_write(VID_A_GPCNT_CTL, 3);
431 q->count = 1;
432
433 /* enable irq */
434 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
435 cx_set(VID_A_INT_MSK, 0x000011);
436
437 /* start dma */
438 cx_set(DEV_CNTRL2, (1<<5));
439 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
440
441 return 0;
442}
443
Steven Tothe47f30b2008-01-10 04:25:59 -0300444
445static int cx23885_restart_video_queue(struct cx23885_dev *dev,
446 struct cx23885_dmaqueue *q)
447{
448 struct cx23885_buffer *buf, *prev;
449 struct list_head *item;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300450 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300451
452 if (!list_empty(&q->active)) {
453 buf = list_entry(q->active.next, struct cx23885_buffer,
454 vb.queue);
455 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
456 buf, buf->vb.i);
457 cx23885_start_video_dma(dev, q, buf);
458 list_for_each(item, &q->active) {
459 buf = list_entry(item, struct cx23885_buffer,
460 vb.queue);
461 buf->count = q->count++;
462 }
463 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
464 return 0;
465 }
466
467 prev = NULL;
468 for (;;) {
469 if (list_empty(&q->queued))
470 return 0;
471 buf = list_entry(q->queued.next, struct cx23885_buffer,
472 vb.queue);
473 if (NULL == prev) {
474 list_move_tail(&buf->vb.queue, &q->active);
475 cx23885_start_video_dma(dev, q, buf);
476 buf->vb.state = VIDEOBUF_ACTIVE;
477 buf->count = q->count++;
478 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
479 dprintk(2, "[%p/%d] restart_queue - first active\n",
480 buf, buf->vb.i);
481
482 } else if (prev->vb.width == buf->vb.width &&
483 prev->vb.height == buf->vb.height &&
484 prev->fmt == buf->fmt) {
485 list_move_tail(&buf->vb.queue, &q->active);
486 buf->vb.state = VIDEOBUF_ACTIVE;
487 buf->count = q->count++;
488 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
489 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
490 dprintk(2, "[%p/%d] restart_queue - move to active\n",
491 buf, buf->vb.i);
492 } else {
493 return 0;
494 }
495 prev = buf;
496 }
497}
498
499static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
500 unsigned int *size)
501{
502 struct cx23885_fh *fh = q->priv_data;
503
504 *size = fh->fmt->depth*fh->width*fh->height >> 3;
505 if (0 == *count)
506 *count = 32;
507 while (*size * *count > vid_limit * 1024 * 1024)
508 (*count)--;
509 return 0;
510}
511
512static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
513 enum v4l2_field field)
514{
515 struct cx23885_fh *fh = q->priv_data;
516 struct cx23885_dev *dev = fh->dev;
517 struct cx23885_buffer *buf =
518 container_of(vb, struct cx23885_buffer, vb);
519 int rc, init_buffer = 0;
520 u32 line0_offset, line1_offset;
521 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
522
523 BUG_ON(NULL == fh->fmt);
524 if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
525 fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
526 return -EINVAL;
527 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
528 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
529 return -EINVAL;
530
531 if (buf->fmt != fh->fmt ||
532 buf->vb.width != fh->width ||
533 buf->vb.height != fh->height ||
534 buf->vb.field != field) {
535 buf->fmt = fh->fmt;
536 buf->vb.width = fh->width;
537 buf->vb.height = fh->height;
538 buf->vb.field = field;
539 init_buffer = 1;
540 }
541
542 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
543 init_buffer = 1;
544 rc = videobuf_iolock(q, &buf->vb, NULL);
545 if (0 != rc)
546 goto fail;
547 }
548
549 if (init_buffer) {
550 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
551 switch (buf->vb.field) {
552 case V4L2_FIELD_TOP:
553 cx23885_risc_buffer(dev->pci, &buf->risc,
554 dma->sglist, 0, UNSET,
555 buf->bpl, 0, buf->vb.height);
556 break;
557 case V4L2_FIELD_BOTTOM:
558 cx23885_risc_buffer(dev->pci, &buf->risc,
559 dma->sglist, UNSET, 0,
560 buf->bpl, 0, buf->vb.height);
561 break;
562 case V4L2_FIELD_INTERLACED:
563 if (dev->tvnorm & V4L2_STD_NTSC) {
564 /* cx25840 transmits NTSC bottom field first */
565 dprintk(1, "%s() Creating NTSC risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300566 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300567 line0_offset = buf->bpl;
568 line1_offset = 0;
569 } else {
570 /* All other formats are top field first */
571 dprintk(1, "%s() Creating PAL/SECAM risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300572 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300573 line0_offset = 0;
574 line1_offset = buf->bpl;
575 }
576 cx23885_risc_buffer(dev->pci, &buf->risc,
577 dma->sglist, line0_offset,
578 line1_offset,
579 buf->bpl, buf->bpl,
580 buf->vb.height >> 1);
581 break;
582 case V4L2_FIELD_SEQ_TB:
583 cx23885_risc_buffer(dev->pci, &buf->risc,
584 dma->sglist,
585 0, buf->bpl * (buf->vb.height >> 1),
586 buf->bpl, 0,
587 buf->vb.height >> 1);
588 break;
589 case V4L2_FIELD_SEQ_BT:
590 cx23885_risc_buffer(dev->pci, &buf->risc,
591 dma->sglist,
592 buf->bpl * (buf->vb.height >> 1), 0,
593 buf->bpl, 0,
594 buf->vb.height >> 1);
595 break;
596 default:
597 BUG();
598 }
599 }
600 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
601 buf, buf->vb.i,
602 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
603 (unsigned long)buf->risc.dma);
604
605 buf->vb.state = VIDEOBUF_PREPARED;
606 return 0;
607
608 fail:
609 cx23885_free_buffer(q, buf);
610 return rc;
611}
612
613static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
614{
615 struct cx23885_buffer *buf = container_of(vb,
616 struct cx23885_buffer, vb);
617 struct cx23885_buffer *prev;
618 struct cx23885_fh *fh = vq->priv_data;
619 struct cx23885_dev *dev = fh->dev;
620 struct cx23885_dmaqueue *q = &dev->vidq;
621
622 /* add jump to stopper */
623 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
624 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
625 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
626
627 if (!list_empty(&q->queued)) {
628 list_add_tail(&buf->vb.queue, &q->queued);
629 buf->vb.state = VIDEOBUF_QUEUED;
630 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
631 buf, buf->vb.i);
632
633 } else if (list_empty(&q->active)) {
634 list_add_tail(&buf->vb.queue, &q->active);
635 cx23885_start_video_dma(dev, q, buf);
636 buf->vb.state = VIDEOBUF_ACTIVE;
637 buf->count = q->count++;
638 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
639 dprintk(2, "[%p/%d] buffer_queue - first active\n",
640 buf, buf->vb.i);
641
642 } else {
643 prev = list_entry(q->active.prev, struct cx23885_buffer,
644 vb.queue);
645 if (prev->vb.width == buf->vb.width &&
646 prev->vb.height == buf->vb.height &&
647 prev->fmt == buf->fmt) {
648 list_add_tail(&buf->vb.queue, &q->active);
649 buf->vb.state = VIDEOBUF_ACTIVE;
650 buf->count = q->count++;
651 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
652 /* 64 bit bits 63-32 */
653 prev->risc.jmp[2] = cpu_to_le32(0);
654 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
655 buf, buf->vb.i);
656
657 } else {
658 list_add_tail(&buf->vb.queue, &q->queued);
659 buf->vb.state = VIDEOBUF_QUEUED;
660 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
661 buf, buf->vb.i);
662 }
663 }
664}
665
666static void buffer_release(struct videobuf_queue *q,
667 struct videobuf_buffer *vb)
668{
669 struct cx23885_buffer *buf = container_of(vb,
670 struct cx23885_buffer, vb);
671
672 cx23885_free_buffer(q, buf);
673}
674
675static struct videobuf_queue_ops cx23885_video_qops = {
676 .buf_setup = buffer_setup,
677 .buf_prepare = buffer_prepare,
678 .buf_queue = buffer_queue,
679 .buf_release = buffer_release,
680};
681
682static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
683{
684 switch (fh->type) {
685 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
686 return &fh->vidq;
687 case V4L2_BUF_TYPE_VBI_CAPTURE:
688 return &fh->vbiq;
689 default:
690 BUG();
691 return NULL;
692 }
693}
694
695static int get_resource(struct cx23885_fh *fh)
696{
697 switch (fh->type) {
698 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
699 return RESOURCE_VIDEO;
700 case V4L2_BUF_TYPE_VBI_CAPTURE:
701 return RESOURCE_VBI;
702 default:
703 BUG();
704 return 0;
705 }
706}
707
Hans Verkuilbec43662008-12-30 06:58:20 -0300708static int video_open(struct file *file)
Steven Tothe47f30b2008-01-10 04:25:59 -0300709{
Hans Verkuilbec43662008-12-30 06:58:20 -0300710 int minor = video_devdata(file)->minor;
Steven Tothe47f30b2008-01-10 04:25:59 -0300711 struct cx23885_dev *h, *dev = NULL;
712 struct cx23885_fh *fh;
713 struct list_head *list;
714 enum v4l2_buf_type type = 0;
715 int radio = 0;
716
Hans Verkuild56dc612008-07-30 08:43:36 -0300717 lock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300718 list_for_each(list, &cx23885_devlist) {
719 h = list_entry(list, struct cx23885_dev, devlist);
Andy Wallscd8f8942009-01-09 22:59:27 -0300720 if (h->video_dev &&
721 h->video_dev->minor == minor) {
Steven Tothe47f30b2008-01-10 04:25:59 -0300722 dev = h;
723 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
724 }
725 if (h->vbi_dev &&
Andy Wallscd8f8942009-01-09 22:59:27 -0300726 h->vbi_dev->minor == minor) {
Steven Tothe47f30b2008-01-10 04:25:59 -0300727 dev = h;
728 type = V4L2_BUF_TYPE_VBI_CAPTURE;
729 }
730 if (h->radio_dev &&
731 h->radio_dev->minor == minor) {
732 radio = 1;
733 dev = h;
734 }
735 }
Hans Verkuild56dc612008-07-30 08:43:36 -0300736 if (NULL == dev) {
737 unlock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300738 return -ENODEV;
Hans Verkuild56dc612008-07-30 08:43:36 -0300739 }
Steven Tothe47f30b2008-01-10 04:25:59 -0300740
741 dprintk(1, "open minor=%d radio=%d type=%s\n",
742 minor, radio, v4l2_type_names[type]);
743
744 /* allocate + initialize per filehandle data */
745 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Hans Verkuild56dc612008-07-30 08:43:36 -0300746 if (NULL == fh) {
747 unlock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300748 return -ENOMEM;
Hans Verkuild56dc612008-07-30 08:43:36 -0300749 }
Steven Tothe47f30b2008-01-10 04:25:59 -0300750 file->private_data = fh;
751 fh->dev = dev;
752 fh->radio = radio;
753 fh->type = type;
754 fh->width = 320;
755 fh->height = 240;
756 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
757
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300758 videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
759 &dev->pci->dev, &dev->slock,
Steven Tothe47f30b2008-01-10 04:25:59 -0300760 V4L2_BUF_TYPE_VIDEO_CAPTURE,
761 V4L2_FIELD_INTERLACED,
762 sizeof(struct cx23885_buffer),
763 fh);
764
765 dprintk(1, "post videobuf_queue_init()\n");
766
Hans Verkuild56dc612008-07-30 08:43:36 -0300767 unlock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300768
769 return 0;
770}
771
772static ssize_t video_read(struct file *file, char __user *data,
773 size_t count, loff_t *ppos)
774{
775 struct cx23885_fh *fh = file->private_data;
776
777 switch (fh->type) {
778 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
779 if (res_locked(fh->dev, RESOURCE_VIDEO))
780 return -EBUSY;
781 return videobuf_read_one(&fh->vidq, data, count, ppos,
782 file->f_flags & O_NONBLOCK);
783 case V4L2_BUF_TYPE_VBI_CAPTURE:
784 if (!res_get(fh->dev, fh, RESOURCE_VBI))
785 return -EBUSY;
786 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
787 file->f_flags & O_NONBLOCK);
788 default:
789 BUG();
790 return 0;
791 }
792}
793
794static unsigned int video_poll(struct file *file,
795 struct poll_table_struct *wait)
796{
797 struct cx23885_fh *fh = file->private_data;
798 struct cx23885_buffer *buf;
Figo.zhang9fd64182009-06-16 13:31:29 -0300799 unsigned int rc = POLLERR;
Steven Tothe47f30b2008-01-10 04:25:59 -0300800
801 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
802 if (!res_get(fh->dev, fh, RESOURCE_VBI))
803 return POLLERR;
804 return videobuf_poll_stream(file, &fh->vbiq, wait);
805 }
806
Figo.zhang9fd64182009-06-16 13:31:29 -0300807 mutex_lock(&fh->vidq.vb_lock);
Steven Tothe47f30b2008-01-10 04:25:59 -0300808 if (res_check(fh, RESOURCE_VIDEO)) {
809 /* streaming capture */
810 if (list_empty(&fh->vidq.stream))
Figo.zhang9fd64182009-06-16 13:31:29 -0300811 goto done;
Steven Tothe47f30b2008-01-10 04:25:59 -0300812 buf = list_entry(fh->vidq.stream.next,
813 struct cx23885_buffer, vb.stream);
814 } else {
815 /* read() capture */
816 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
817 if (NULL == buf)
Figo.zhang9fd64182009-06-16 13:31:29 -0300818 goto done;
Steven Tothe47f30b2008-01-10 04:25:59 -0300819 }
820 poll_wait(file, &buf->vb.done, wait);
821 if (buf->vb.state == VIDEOBUF_DONE ||
822 buf->vb.state == VIDEOBUF_ERROR)
Figo.zhang9fd64182009-06-16 13:31:29 -0300823 rc = POLLIN|POLLRDNORM;
824 else
825 rc = 0;
826done:
827 mutex_unlock(&fh->vidq.vb_lock);
828 return rc;
Steven Tothe47f30b2008-01-10 04:25:59 -0300829}
830
Hans Verkuilbec43662008-12-30 06:58:20 -0300831static int video_release(struct file *file)
Steven Tothe47f30b2008-01-10 04:25:59 -0300832{
833 struct cx23885_fh *fh = file->private_data;
834 struct cx23885_dev *dev = fh->dev;
835
836 /* turn off overlay */
837 if (res_check(fh, RESOURCE_OVERLAY)) {
838 /* FIXME */
839 res_free(dev, fh, RESOURCE_OVERLAY);
840 }
841
842 /* stop video capture */
843 if (res_check(fh, RESOURCE_VIDEO)) {
844 videobuf_queue_cancel(&fh->vidq);
845 res_free(dev, fh, RESOURCE_VIDEO);
846 }
847 if (fh->vidq.read_buf) {
848 buffer_release(&fh->vidq, fh->vidq.read_buf);
849 kfree(fh->vidq.read_buf);
850 }
851
852 /* stop vbi capture */
853 if (res_check(fh, RESOURCE_VBI)) {
854 if (fh->vbiq.streaming)
855 videobuf_streamoff(&fh->vbiq);
856 if (fh->vbiq.reading)
857 videobuf_read_stop(&fh->vbiq);
858 res_free(dev, fh, RESOURCE_VBI);
859 }
860
861 videobuf_mmap_free(&fh->vidq);
862 file->private_data = NULL;
863 kfree(fh);
864
Steven Totha19602f2008-01-10 11:43:18 -0300865 /* We are not putting the tuner to sleep here on exit, because
866 * we want to use the mpeg encoder in another session to capture
867 * tuner video. Closing this will result in no video to the encoder.
868 */
Steven Tothe47f30b2008-01-10 04:25:59 -0300869
870 return 0;
871}
872
873static int video_mmap(struct file *file, struct vm_area_struct *vma)
874{
875 struct cx23885_fh *fh = file->private_data;
876
877 return videobuf_mmap_mapper(get_queue(fh), vma);
878}
879
880/* ------------------------------------------------------------------ */
881/* VIDEO CTRL IOCTLS */
882
Steven Toth9c8ced52008-10-16 20:18:44 -0300883static int cx23885_get_control(struct cx23885_dev *dev,
884 struct v4l2_control *ctl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300885{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300886 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -0300887 call_all(dev, core, g_ctrl, ctl);
Steven Tothe47f30b2008-01-10 04:25:59 -0300888 return 0;
889}
Steven Tothe47f30b2008-01-10 04:25:59 -0300890
Steven Toth9c8ced52008-10-16 20:18:44 -0300891static int cx23885_set_control(struct cx23885_dev *dev,
892 struct v4l2_control *ctl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300893{
894 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
Harvey Harrison22b4e642008-04-08 23:20:00 -0300895 " (disabled - no action)\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300896 return 0;
897}
Steven Tothe47f30b2008-01-10 04:25:59 -0300898
899static void init_controls(struct cx23885_dev *dev)
900{
901 struct v4l2_control ctrl;
902 int i;
903
904 for (i = 0; i < CX23885_CTLS; i++) {
905 ctrl.id = cx23885_ctls[i].v.id;
906 ctrl.value = cx23885_ctls[i].v.default_value;
907
908 cx23885_set_control(dev, &ctrl);
909 }
910}
911
912/* ------------------------------------------------------------------ */
913/* VIDEO IOCTLS */
914
Hans Verkuil78b526a2008-05-28 12:16:41 -0300915static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300916 struct v4l2_format *f)
917{
918 struct cx23885_fh *fh = priv;
919
920 f->fmt.pix.width = fh->width;
921 f->fmt.pix.height = fh->height;
922 f->fmt.pix.field = fh->vidq.field;
923 f->fmt.pix.pixelformat = fh->fmt->fourcc;
924 f->fmt.pix.bytesperline =
925 (f->fmt.pix.width * fh->fmt->depth) >> 3;
926 f->fmt.pix.sizeimage =
927 f->fmt.pix.height * f->fmt.pix.bytesperline;
928
929 return 0;
930}
931
Hans Verkuil78b526a2008-05-28 12:16:41 -0300932static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300933 struct v4l2_format *f)
934{
935 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
936 struct cx23885_fmt *fmt;
937 enum v4l2_field field;
938 unsigned int maxw, maxh;
939
940 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
941 if (NULL == fmt)
942 return -EINVAL;
943
944 field = f->fmt.pix.field;
945 maxw = norm_maxw(dev->tvnorm);
946 maxh = norm_maxh(dev->tvnorm);
947
948 if (V4L2_FIELD_ANY == field) {
949 field = (f->fmt.pix.height > maxh/2)
950 ? V4L2_FIELD_INTERLACED
951 : V4L2_FIELD_BOTTOM;
952 }
953
954 switch (field) {
955 case V4L2_FIELD_TOP:
956 case V4L2_FIELD_BOTTOM:
957 maxh = maxh / 2;
958 break;
959 case V4L2_FIELD_INTERLACED:
960 break;
961 default:
962 return -EINVAL;
963 }
964
965 f->fmt.pix.field = field;
Trent Piepho2449afc2009-05-30 21:45:46 -0300966 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
967 &f->fmt.pix.height, 32, maxh, 0, 0);
Steven Tothe47f30b2008-01-10 04:25:59 -0300968 f->fmt.pix.bytesperline =
969 (f->fmt.pix.width * fmt->depth) >> 3;
970 f->fmt.pix.sizeimage =
971 f->fmt.pix.height * f->fmt.pix.bytesperline;
972
973 return 0;
974}
975
Hans Verkuil78b526a2008-05-28 12:16:41 -0300976static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300977 struct v4l2_format *f)
978{
979 struct cx23885_fh *fh = priv;
980 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
981 int err;
982
Harvey Harrison22b4e642008-04-08 23:20:00 -0300983 dprintk(2, "%s()\n", __func__);
Hans Verkuil78b526a2008-05-28 12:16:41 -0300984 err = vidioc_try_fmt_vid_cap(file, priv, f);
Steven Tothe47f30b2008-01-10 04:25:59 -0300985
986 if (0 != err)
987 return err;
988 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
989 fh->width = f->fmt.pix.width;
990 fh->height = f->fmt.pix.height;
991 fh->vidq.field = f->fmt.pix.field;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300992 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -0300993 fh->width, fh->height, fh->vidq.field);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -0300994 call_all(dev, video, s_fmt, f);
Steven Tothe47f30b2008-01-10 04:25:59 -0300995 return 0;
996}
997
998static int vidioc_querycap(struct file *file, void *priv,
999 struct v4l2_capability *cap)
1000{
1001 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1002
1003 strcpy(cap->driver, "cx23885");
1004 strlcpy(cap->card, cx23885_boards[dev->board].name,
1005 sizeof(cap->card));
1006 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1007 cap->version = CX23885_VERSION_CODE;
1008 cap->capabilities =
1009 V4L2_CAP_VIDEO_CAPTURE |
1010 V4L2_CAP_READWRITE |
1011 V4L2_CAP_STREAMING |
1012 V4L2_CAP_VBI_CAPTURE;
1013 if (UNSET != dev->tuner_type)
1014 cap->capabilities |= V4L2_CAP_TUNER;
1015 return 0;
1016}
1017
Hans Verkuil78b526a2008-05-28 12:16:41 -03001018static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001019 struct v4l2_fmtdesc *f)
1020{
1021 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1022 return -EINVAL;
1023
1024 strlcpy(f->description, formats[f->index].name,
1025 sizeof(f->description));
1026 f->pixelformat = formats[f->index].fourcc;
1027
1028 return 0;
1029}
1030
1031#ifdef CONFIG_VIDEO_V4L1_COMPAT
1032static int vidiocgmbuf(struct file *file, void *priv,
1033 struct video_mbuf *mbuf)
1034{
1035 struct cx23885_fh *fh = priv;
1036 struct videobuf_queue *q;
1037 struct v4l2_requestbuffers req;
1038 unsigned int i;
1039 int err;
1040
1041 q = get_queue(fh);
1042 memset(&req, 0, sizeof(req));
1043 req.type = q->type;
1044 req.count = 8;
1045 req.memory = V4L2_MEMORY_MMAP;
1046 err = videobuf_reqbufs(q, &req);
1047 if (err < 0)
1048 return err;
1049
1050 mbuf->frames = req.count;
1051 mbuf->size = 0;
1052 for (i = 0; i < mbuf->frames; i++) {
1053 mbuf->offsets[i] = q->bufs[i]->boff;
1054 mbuf->size += q->bufs[i]->bsize;
1055 }
1056 return 0;
1057}
1058#endif
1059
1060static int vidioc_reqbufs(struct file *file, void *priv,
1061 struct v4l2_requestbuffers *p)
1062{
1063 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001064 return videobuf_reqbufs(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001065}
1066
1067static int vidioc_querybuf(struct file *file, void *priv,
1068 struct v4l2_buffer *p)
1069{
1070 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001071 return videobuf_querybuf(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001072}
1073
1074static int vidioc_qbuf(struct file *file, void *priv,
1075 struct v4l2_buffer *p)
1076{
1077 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001078 return videobuf_qbuf(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001079}
1080
1081static int vidioc_dqbuf(struct file *file, void *priv,
1082 struct v4l2_buffer *p)
1083{
1084 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001085 return videobuf_dqbuf(get_queue(fh), p,
1086 file->f_flags & O_NONBLOCK);
Steven Tothe47f30b2008-01-10 04:25:59 -03001087}
1088
1089static int vidioc_streamon(struct file *file, void *priv,
1090 enum v4l2_buf_type i)
1091{
1092 struct cx23885_fh *fh = priv;
1093 struct cx23885_dev *dev = fh->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001094 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001095
1096 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1097 return -EINVAL;
1098 if (unlikely(i != fh->type))
1099 return -EINVAL;
1100
1101 if (unlikely(!res_get(dev, fh, get_resource(fh))))
1102 return -EBUSY;
1103 return videobuf_streamon(get_queue(fh));
1104}
1105
1106static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1107{
1108 struct cx23885_fh *fh = priv;
1109 struct cx23885_dev *dev = fh->dev;
1110 int err, res;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001111 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001112
1113 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1114 return -EINVAL;
1115 if (i != fh->type)
1116 return -EINVAL;
1117
1118 res = get_resource(fh);
1119 err = videobuf_streamoff(get_queue(fh));
1120 if (err < 0)
1121 return err;
1122 res_free(dev, fh, res);
1123 return 0;
1124}
1125
1126static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1127{
1128 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001129 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001130
1131 mutex_lock(&dev->lock);
1132 cx23885_set_tvnorm(dev, *tvnorms);
1133 mutex_unlock(&dev->lock);
1134
1135 return 0;
1136}
1137
Hans Verkuild45b9b82008-09-04 03:33:43 -03001138static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
Steven Tothe47f30b2008-01-10 04:25:59 -03001139{
1140 static const char *iname[] = {
1141 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1142 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1143 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1144 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1145 [CX23885_VMUX_SVIDEO] = "S-Video",
1146 [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
1314#ifdef CONFIG_VIDEO_ADV_DEBUG
1315static int vidioc_g_register(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001316 struct v4l2_dbg_register *reg)
Steven Tothe47f30b2008-01-10 04:25:59 -03001317{
1318 struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1319
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001320 if (!v4l2_chip_match_host(&reg->match))
Steven Totha19602f2008-01-10 11:43:18 -03001321 return -EINVAL;
1322
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001323 call_all(dev, core, g_register, reg);
Steven Tothe47f30b2008-01-10 04:25:59 -03001324
1325 return 0;
1326}
1327
1328static int vidioc_s_register(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001329 struct v4l2_dbg_register *reg)
Steven Tothe47f30b2008-01-10 04:25:59 -03001330{
1331 struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1332
Hans Verkuilaecde8b2008-12-30 07:14:19 -03001333 if (!v4l2_chip_match_host(&reg->match))
Steven Totha19602f2008-01-10 11:43:18 -03001334 return -EINVAL;
1335
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001336 call_all(dev, core, s_register, reg);
Steven Totha19602f2008-01-10 11:43:18 -03001337
Steven Tothe47f30b2008-01-10 04:25:59 -03001338 return 0;
1339}
1340#endif
1341
1342/* ----------------------------------------------------------- */
Steven Tothe47f30b2008-01-10 04:25:59 -03001343
1344static void cx23885_vid_timeout(unsigned long data)
1345{
1346 struct cx23885_dev *dev = (struct cx23885_dev *)data;
1347 struct cx23885_dmaqueue *q = &dev->vidq;
1348 struct cx23885_buffer *buf;
1349 unsigned long flags;
1350
1351 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1352
1353 cx_clear(VID_A_DMA_CTL, 0x11);
1354
1355 spin_lock_irqsave(&dev->slock, flags);
1356 while (!list_empty(&q->active)) {
1357 buf = list_entry(q->active.next,
1358 struct cx23885_buffer, vb.queue);
1359 list_del(&buf->vb.queue);
1360 buf->vb.state = VIDEOBUF_ERROR;
1361 wake_up(&buf->vb.done);
1362 printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
1363 dev->name, buf, buf->vb.i,
1364 (unsigned long)buf->risc.dma);
1365 }
1366 cx23885_restart_video_queue(dev, q);
1367 spin_unlock_irqrestore(&dev->slock, flags);
1368}
1369
1370int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1371{
1372 u32 mask, count;
1373 int handled = 0;
1374
1375 mask = cx_read(VID_A_INT_MSK);
1376 if (0 == (status & mask))
1377 return handled;
1378 cx_write(VID_A_INT_STAT, status);
1379
Harvey Harrison22b4e642008-04-08 23:20:00 -03001380 dprintk(2, "%s() status = 0x%08x\n", __func__, status);
Steven Tothe47f30b2008-01-10 04:25:59 -03001381 /* risc op code error */
1382 if (status & (1 << 16)) {
1383 printk(KERN_WARNING "%s/0: video risc op code error\n",
1384 dev->name);
1385 cx_clear(VID_A_DMA_CTL, 0x11);
1386 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1387 }
1388
1389 /* risc1 y */
1390 if (status & 0x01) {
1391 spin_lock(&dev->slock);
1392 count = cx_read(VID_A_GPCNT);
1393 cx23885_video_wakeup(dev, &dev->vidq, count);
1394 spin_unlock(&dev->slock);
1395 handled++;
1396 }
1397 /* risc2 y */
1398 if (status & 0x10) {
1399 dprintk(2, "stopper video\n");
1400 spin_lock(&dev->slock);
1401 cx23885_restart_video_queue(dev, &dev->vidq);
1402 spin_unlock(&dev->slock);
1403 handled++;
1404 }
1405
1406 return handled;
1407}
1408
Steven Tothe47f30b2008-01-10 04:25:59 -03001409/* ----------------------------------------------------------- */
1410/* exported stuff */
1411
Hans Verkuilbec43662008-12-30 06:58:20 -03001412static const struct v4l2_file_operations video_fops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001413 .owner = THIS_MODULE,
1414 .open = video_open,
1415 .release = video_release,
1416 .read = video_read,
1417 .poll = video_poll,
1418 .mmap = video_mmap,
1419 .ioctl = video_ioctl2,
Steven Tothe47f30b2008-01-10 04:25:59 -03001420};
1421
Hans Verkuila3998102008-07-21 02:57:38 -03001422static const struct v4l2_ioctl_ops video_ioctl_ops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001423 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001424 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1425 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1426 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1427 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1428 .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
1429 .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
1430 .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
Steven Tothe47f30b2008-01-10 04:25:59 -03001431 .vidioc_reqbufs = vidioc_reqbufs,
1432 .vidioc_querybuf = vidioc_querybuf,
1433 .vidioc_qbuf = vidioc_qbuf,
1434 .vidioc_dqbuf = vidioc_dqbuf,
1435 .vidioc_s_std = vidioc_s_std,
1436 .vidioc_enum_input = vidioc_enum_input,
1437 .vidioc_g_input = vidioc_g_input,
1438 .vidioc_s_input = vidioc_s_input,
1439 .vidioc_queryctrl = vidioc_queryctrl,
1440 .vidioc_g_ctrl = vidioc_g_ctrl,
1441 .vidioc_s_ctrl = vidioc_s_ctrl,
1442 .vidioc_streamon = vidioc_streamon,
1443 .vidioc_streamoff = vidioc_streamoff,
1444#ifdef CONFIG_VIDEO_V4L1_COMPAT
1445 .vidiocgmbuf = vidiocgmbuf,
1446#endif
1447 .vidioc_g_tuner = vidioc_g_tuner,
1448 .vidioc_s_tuner = vidioc_s_tuner,
1449 .vidioc_g_frequency = vidioc_g_frequency,
1450 .vidioc_s_frequency = vidioc_s_frequency,
1451#ifdef CONFIG_VIDEO_ADV_DEBUG
1452 .vidioc_g_register = vidioc_g_register,
1453 .vidioc_s_register = vidioc_s_register,
1454#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001455};
1456
1457static struct video_device cx23885_vbi_template;
1458static struct video_device cx23885_video_template = {
1459 .name = "cx23885-video",
Hans Verkuila3998102008-07-21 02:57:38 -03001460 .fops = &video_fops,
1461 .minor = -1,
1462 .ioctl_ops = &video_ioctl_ops,
Steven Tothe47f30b2008-01-10 04:25:59 -03001463 .tvnorms = CX23885_NORMS,
1464 .current_norm = V4L2_STD_NTSC_M,
1465};
1466
Hans Verkuilbec43662008-12-30 06:58:20 -03001467static const struct v4l2_file_operations radio_fops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001468 .owner = THIS_MODULE,
1469 .open = video_open,
1470 .release = video_release,
1471 .ioctl = video_ioctl2,
Steven Tothe47f30b2008-01-10 04:25:59 -03001472};
1473
1474
1475void cx23885_video_unregister(struct cx23885_dev *dev)
1476{
Harvey Harrison22b4e642008-04-08 23:20:00 -03001477 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001478 cx_clear(PCI_INT_MSK, 1);
1479
1480 if (dev->video_dev) {
1481 if (-1 != dev->video_dev->minor)
1482 video_unregister_device(dev->video_dev);
1483 else
1484 video_device_release(dev->video_dev);
1485 dev->video_dev = NULL;
1486
1487 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1488 }
1489}
1490
1491int cx23885_video_register(struct cx23885_dev *dev)
1492{
1493 int err;
1494
Harvey Harrison22b4e642008-04-08 23:20:00 -03001495 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001496 spin_lock_init(&dev->slock);
1497
1498 /* Initialize VBI template */
1499 memcpy(&cx23885_vbi_template, &cx23885_video_template,
1500 sizeof(cx23885_vbi_template));
1501 strcpy(cx23885_vbi_template.name, "cx23885-vbi");
Steven Tothe47f30b2008-01-10 04:25:59 -03001502
1503 dev->tvnorm = cx23885_video_template.current_norm;
1504
1505 /* init video dma queues */
1506 INIT_LIST_HEAD(&dev->vidq.active);
1507 INIT_LIST_HEAD(&dev->vidq.queued);
1508 dev->vidq.timeout.function = cx23885_vid_timeout;
1509 dev->vidq.timeout.data = (unsigned long)dev;
1510 init_timer(&dev->vidq.timeout);
1511 cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1512 VID_A_DMA_CTL, 0x11, 0x00);
1513
Steven Totha19602f2008-01-10 11:43:18 -03001514 /* Don't enable VBI yet */
Steven Tothe47f30b2008-01-10 04:25:59 -03001515 cx_set(PCI_INT_MSK, 1);
1516
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001517 if (TUNER_ABSENT != dev->tuner_type) {
1518 struct v4l2_subdev *sd = NULL;
1519
1520 if (dev->tuner_addr)
Hans Verkuile6574f22009-04-01 03:57:53 -03001521 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1522 &dev->i2c_bus[1].i2c_adap,
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001523 "tuner", "tuner", dev->tuner_addr);
1524 else
Hans Verkuile6574f22009-04-01 03:57:53 -03001525 sd = v4l2_i2c_new_probed_subdev(&dev->v4l2_dev,
1526 &dev->i2c_bus[1].i2c_adap,
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001527 "tuner", "tuner", v4l2_i2c_tuner_addrs(ADDRS_TV));
1528 if (sd) {
1529 struct tuner_setup tun_setup;
1530
1531 tun_setup.mode_mask = T_ANALOG_TV;
1532 tun_setup.type = dev->tuner_type;
1533 tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1534
1535 v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1536 }
1537 }
1538
Steven Tothe47f30b2008-01-10 04:25:59 -03001539
1540 /* register v4l devices */
1541 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1542 &cx23885_video_template, "video");
1543 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1544 video_nr[dev->nr]);
1545 if (err < 0) {
1546 printk(KERN_INFO "%s: can't register video device\n",
1547 dev->name);
1548 goto fail_unreg;
1549 }
1550 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03001551 dev->name, dev->video_dev->num);
Steven Tothe47f30b2008-01-10 04:25:59 -03001552 /* initial device configuration */
1553 mutex_lock(&dev->lock);
1554 cx23885_set_tvnorm(dev, dev->tvnorm);
1555 init_controls(dev);
1556 cx23885_video_mux(dev, 0);
1557 mutex_unlock(&dev->lock);
1558
Steven Tothe47f30b2008-01-10 04:25:59 -03001559 return 0;
1560
1561fail_unreg:
1562 cx23885_video_unregister(dev);
1563 return err;
1564}
1565