blob: ce7b3f8cdc651e3795cf76d3ab931135b7c6f3c8 [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 Totha19602f22008-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 Totha19602f22008-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{
396 struct v4l2_routing route;
397 memset(&route, 0, sizeof(route));
398
399 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
406 route.input = INPUT(input)->vmux;
407
408 /* Tell the internal A/V decoder */
Hans Verkuil0d5a19f2009-03-29 06:53:29 -0300409 v4l2_subdev_call(dev->sd_cx25840, video, s_routing, &route);
Steven Tothe47f30b2008-01-10 04:25:59 -0300410
411 return 0;
412}
Steven Tothe47f30b2008-01-10 04:25:59 -0300413
414/* ------------------------------------------------------------------ */
Hans Verkuild45b9b82008-09-04 03:33:43 -0300415static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
Steven Tothe47f30b2008-01-10 04:25:59 -0300416 unsigned int height, enum v4l2_field field)
417{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300418 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300419 return 0;
420}
421
422static int cx23885_start_video_dma(struct cx23885_dev *dev,
423 struct cx23885_dmaqueue *q,
424 struct cx23885_buffer *buf)
425{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300426 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300427
428 /* setup fifo + format */
429 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
430 buf->bpl, buf->risc.dma);
431 cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
432
433 /* reset counter */
434 cx_write(VID_A_GPCNT_CTL, 3);
435 q->count = 1;
436
437 /* enable irq */
438 cx_set(PCI_INT_MSK, cx_read(PCI_INT_MSK) | 0x01);
439 cx_set(VID_A_INT_MSK, 0x000011);
440
441 /* start dma */
442 cx_set(DEV_CNTRL2, (1<<5));
443 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
444
445 return 0;
446}
447
Steven Tothe47f30b2008-01-10 04:25:59 -0300448
449static int cx23885_restart_video_queue(struct cx23885_dev *dev,
450 struct cx23885_dmaqueue *q)
451{
452 struct cx23885_buffer *buf, *prev;
453 struct list_head *item;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300454 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300455
456 if (!list_empty(&q->active)) {
457 buf = list_entry(q->active.next, struct cx23885_buffer,
458 vb.queue);
459 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
460 buf, buf->vb.i);
461 cx23885_start_video_dma(dev, q, buf);
462 list_for_each(item, &q->active) {
463 buf = list_entry(item, struct cx23885_buffer,
464 vb.queue);
465 buf->count = q->count++;
466 }
467 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
468 return 0;
469 }
470
471 prev = NULL;
472 for (;;) {
473 if (list_empty(&q->queued))
474 return 0;
475 buf = list_entry(q->queued.next, struct cx23885_buffer,
476 vb.queue);
477 if (NULL == prev) {
478 list_move_tail(&buf->vb.queue, &q->active);
479 cx23885_start_video_dma(dev, q, buf);
480 buf->vb.state = VIDEOBUF_ACTIVE;
481 buf->count = q->count++;
482 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
483 dprintk(2, "[%p/%d] restart_queue - first active\n",
484 buf, buf->vb.i);
485
486 } else if (prev->vb.width == buf->vb.width &&
487 prev->vb.height == buf->vb.height &&
488 prev->fmt == buf->fmt) {
489 list_move_tail(&buf->vb.queue, &q->active);
490 buf->vb.state = VIDEOBUF_ACTIVE;
491 buf->count = q->count++;
492 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
493 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
494 dprintk(2, "[%p/%d] restart_queue - move to active\n",
495 buf, buf->vb.i);
496 } else {
497 return 0;
498 }
499 prev = buf;
500 }
501}
502
503static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
504 unsigned int *size)
505{
506 struct cx23885_fh *fh = q->priv_data;
507
508 *size = fh->fmt->depth*fh->width*fh->height >> 3;
509 if (0 == *count)
510 *count = 32;
511 while (*size * *count > vid_limit * 1024 * 1024)
512 (*count)--;
513 return 0;
514}
515
516static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
517 enum v4l2_field field)
518{
519 struct cx23885_fh *fh = q->priv_data;
520 struct cx23885_dev *dev = fh->dev;
521 struct cx23885_buffer *buf =
522 container_of(vb, struct cx23885_buffer, vb);
523 int rc, init_buffer = 0;
524 u32 line0_offset, line1_offset;
525 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
526
527 BUG_ON(NULL == fh->fmt);
528 if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
529 fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
530 return -EINVAL;
531 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
532 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
533 return -EINVAL;
534
535 if (buf->fmt != fh->fmt ||
536 buf->vb.width != fh->width ||
537 buf->vb.height != fh->height ||
538 buf->vb.field != field) {
539 buf->fmt = fh->fmt;
540 buf->vb.width = fh->width;
541 buf->vb.height = fh->height;
542 buf->vb.field = field;
543 init_buffer = 1;
544 }
545
546 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
547 init_buffer = 1;
548 rc = videobuf_iolock(q, &buf->vb, NULL);
549 if (0 != rc)
550 goto fail;
551 }
552
553 if (init_buffer) {
554 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
555 switch (buf->vb.field) {
556 case V4L2_FIELD_TOP:
557 cx23885_risc_buffer(dev->pci, &buf->risc,
558 dma->sglist, 0, UNSET,
559 buf->bpl, 0, buf->vb.height);
560 break;
561 case V4L2_FIELD_BOTTOM:
562 cx23885_risc_buffer(dev->pci, &buf->risc,
563 dma->sglist, UNSET, 0,
564 buf->bpl, 0, buf->vb.height);
565 break;
566 case V4L2_FIELD_INTERLACED:
567 if (dev->tvnorm & V4L2_STD_NTSC) {
568 /* cx25840 transmits NTSC bottom field first */
569 dprintk(1, "%s() Creating NTSC risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300570 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300571 line0_offset = buf->bpl;
572 line1_offset = 0;
573 } else {
574 /* All other formats are top field first */
575 dprintk(1, "%s() Creating PAL/SECAM risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300576 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300577 line0_offset = 0;
578 line1_offset = buf->bpl;
579 }
580 cx23885_risc_buffer(dev->pci, &buf->risc,
581 dma->sglist, line0_offset,
582 line1_offset,
583 buf->bpl, buf->bpl,
584 buf->vb.height >> 1);
585 break;
586 case V4L2_FIELD_SEQ_TB:
587 cx23885_risc_buffer(dev->pci, &buf->risc,
588 dma->sglist,
589 0, buf->bpl * (buf->vb.height >> 1),
590 buf->bpl, 0,
591 buf->vb.height >> 1);
592 break;
593 case V4L2_FIELD_SEQ_BT:
594 cx23885_risc_buffer(dev->pci, &buf->risc,
595 dma->sglist,
596 buf->bpl * (buf->vb.height >> 1), 0,
597 buf->bpl, 0,
598 buf->vb.height >> 1);
599 break;
600 default:
601 BUG();
602 }
603 }
604 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
605 buf, buf->vb.i,
606 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
607 (unsigned long)buf->risc.dma);
608
609 buf->vb.state = VIDEOBUF_PREPARED;
610 return 0;
611
612 fail:
613 cx23885_free_buffer(q, buf);
614 return rc;
615}
616
617static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
618{
619 struct cx23885_buffer *buf = container_of(vb,
620 struct cx23885_buffer, vb);
621 struct cx23885_buffer *prev;
622 struct cx23885_fh *fh = vq->priv_data;
623 struct cx23885_dev *dev = fh->dev;
624 struct cx23885_dmaqueue *q = &dev->vidq;
625
626 /* add jump to stopper */
627 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
628 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
629 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
630
631 if (!list_empty(&q->queued)) {
632 list_add_tail(&buf->vb.queue, &q->queued);
633 buf->vb.state = VIDEOBUF_QUEUED;
634 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
635 buf, buf->vb.i);
636
637 } else if (list_empty(&q->active)) {
638 list_add_tail(&buf->vb.queue, &q->active);
639 cx23885_start_video_dma(dev, q, buf);
640 buf->vb.state = VIDEOBUF_ACTIVE;
641 buf->count = q->count++;
642 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
643 dprintk(2, "[%p/%d] buffer_queue - first active\n",
644 buf, buf->vb.i);
645
646 } else {
647 prev = list_entry(q->active.prev, struct cx23885_buffer,
648 vb.queue);
649 if (prev->vb.width == buf->vb.width &&
650 prev->vb.height == buf->vb.height &&
651 prev->fmt == buf->fmt) {
652 list_add_tail(&buf->vb.queue, &q->active);
653 buf->vb.state = VIDEOBUF_ACTIVE;
654 buf->count = q->count++;
655 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
656 /* 64 bit bits 63-32 */
657 prev->risc.jmp[2] = cpu_to_le32(0);
658 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
659 buf, buf->vb.i);
660
661 } else {
662 list_add_tail(&buf->vb.queue, &q->queued);
663 buf->vb.state = VIDEOBUF_QUEUED;
664 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
665 buf, buf->vb.i);
666 }
667 }
668}
669
670static void buffer_release(struct videobuf_queue *q,
671 struct videobuf_buffer *vb)
672{
673 struct cx23885_buffer *buf = container_of(vb,
674 struct cx23885_buffer, vb);
675
676 cx23885_free_buffer(q, buf);
677}
678
679static struct videobuf_queue_ops cx23885_video_qops = {
680 .buf_setup = buffer_setup,
681 .buf_prepare = buffer_prepare,
682 .buf_queue = buffer_queue,
683 .buf_release = buffer_release,
684};
685
686static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
687{
688 switch (fh->type) {
689 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
690 return &fh->vidq;
691 case V4L2_BUF_TYPE_VBI_CAPTURE:
692 return &fh->vbiq;
693 default:
694 BUG();
695 return NULL;
696 }
697}
698
699static int get_resource(struct cx23885_fh *fh)
700{
701 switch (fh->type) {
702 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
703 return RESOURCE_VIDEO;
704 case V4L2_BUF_TYPE_VBI_CAPTURE:
705 return RESOURCE_VBI;
706 default:
707 BUG();
708 return 0;
709 }
710}
711
Hans Verkuilbec43662008-12-30 06:58:20 -0300712static int video_open(struct file *file)
Steven Tothe47f30b2008-01-10 04:25:59 -0300713{
Hans Verkuilbec43662008-12-30 06:58:20 -0300714 int minor = video_devdata(file)->minor;
Steven Tothe47f30b2008-01-10 04:25:59 -0300715 struct cx23885_dev *h, *dev = NULL;
716 struct cx23885_fh *fh;
717 struct list_head *list;
718 enum v4l2_buf_type type = 0;
719 int radio = 0;
720
Hans Verkuild56dc612008-07-30 08:43:36 -0300721 lock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300722 list_for_each(list, &cx23885_devlist) {
723 h = list_entry(list, struct cx23885_dev, devlist);
Andy Wallscd8f8942009-01-09 22:59:27 -0300724 if (h->video_dev &&
725 h->video_dev->minor == minor) {
Steven Tothe47f30b2008-01-10 04:25:59 -0300726 dev = h;
727 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
728 }
729 if (h->vbi_dev &&
Andy Wallscd8f8942009-01-09 22:59:27 -0300730 h->vbi_dev->minor == minor) {
Steven Tothe47f30b2008-01-10 04:25:59 -0300731 dev = h;
732 type = V4L2_BUF_TYPE_VBI_CAPTURE;
733 }
734 if (h->radio_dev &&
735 h->radio_dev->minor == minor) {
736 radio = 1;
737 dev = h;
738 }
739 }
Hans Verkuild56dc612008-07-30 08:43:36 -0300740 if (NULL == dev) {
741 unlock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300742 return -ENODEV;
Hans Verkuild56dc612008-07-30 08:43:36 -0300743 }
Steven Tothe47f30b2008-01-10 04:25:59 -0300744
745 dprintk(1, "open minor=%d radio=%d type=%s\n",
746 minor, radio, v4l2_type_names[type]);
747
748 /* allocate + initialize per filehandle data */
749 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Hans Verkuild56dc612008-07-30 08:43:36 -0300750 if (NULL == fh) {
751 unlock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300752 return -ENOMEM;
Hans Verkuild56dc612008-07-30 08:43:36 -0300753 }
Steven Tothe47f30b2008-01-10 04:25:59 -0300754 file->private_data = fh;
755 fh->dev = dev;
756 fh->radio = radio;
757 fh->type = type;
758 fh->width = 320;
759 fh->height = 240;
760 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
761
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300762 videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
763 &dev->pci->dev, &dev->slock,
Steven Tothe47f30b2008-01-10 04:25:59 -0300764 V4L2_BUF_TYPE_VIDEO_CAPTURE,
765 V4L2_FIELD_INTERLACED,
766 sizeof(struct cx23885_buffer),
767 fh);
768
769 dprintk(1, "post videobuf_queue_init()\n");
770
Hans Verkuild56dc612008-07-30 08:43:36 -0300771 unlock_kernel();
Steven Tothe47f30b2008-01-10 04:25:59 -0300772
773 return 0;
774}
775
776static ssize_t video_read(struct file *file, char __user *data,
777 size_t count, loff_t *ppos)
778{
779 struct cx23885_fh *fh = file->private_data;
780
781 switch (fh->type) {
782 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
783 if (res_locked(fh->dev, RESOURCE_VIDEO))
784 return -EBUSY;
785 return videobuf_read_one(&fh->vidq, data, count, ppos,
786 file->f_flags & O_NONBLOCK);
787 case V4L2_BUF_TYPE_VBI_CAPTURE:
788 if (!res_get(fh->dev, fh, RESOURCE_VBI))
789 return -EBUSY;
790 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
791 file->f_flags & O_NONBLOCK);
792 default:
793 BUG();
794 return 0;
795 }
796}
797
798static unsigned int video_poll(struct file *file,
799 struct poll_table_struct *wait)
800{
801 struct cx23885_fh *fh = file->private_data;
802 struct cx23885_buffer *buf;
803
804 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
805 if (!res_get(fh->dev, fh, RESOURCE_VBI))
806 return POLLERR;
807 return videobuf_poll_stream(file, &fh->vbiq, wait);
808 }
809
810 if (res_check(fh, RESOURCE_VIDEO)) {
811 /* streaming capture */
812 if (list_empty(&fh->vidq.stream))
813 return POLLERR;
814 buf = list_entry(fh->vidq.stream.next,
815 struct cx23885_buffer, vb.stream);
816 } else {
817 /* read() capture */
818 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
819 if (NULL == buf)
820 return POLLERR;
821 }
822 poll_wait(file, &buf->vb.done, wait);
823 if (buf->vb.state == VIDEOBUF_DONE ||
824 buf->vb.state == VIDEOBUF_ERROR)
825 return POLLIN|POLLRDNORM;
826 return 0;
827}
828
Hans Verkuilbec43662008-12-30 06:58:20 -0300829static int video_release(struct file *file)
Steven Tothe47f30b2008-01-10 04:25:59 -0300830{
831 struct cx23885_fh *fh = file->private_data;
832 struct cx23885_dev *dev = fh->dev;
833
834 /* turn off overlay */
835 if (res_check(fh, RESOURCE_OVERLAY)) {
836 /* FIXME */
837 res_free(dev, fh, RESOURCE_OVERLAY);
838 }
839
840 /* stop video capture */
841 if (res_check(fh, RESOURCE_VIDEO)) {
842 videobuf_queue_cancel(&fh->vidq);
843 res_free(dev, fh, RESOURCE_VIDEO);
844 }
845 if (fh->vidq.read_buf) {
846 buffer_release(&fh->vidq, fh->vidq.read_buf);
847 kfree(fh->vidq.read_buf);
848 }
849
850 /* stop vbi capture */
851 if (res_check(fh, RESOURCE_VBI)) {
852 if (fh->vbiq.streaming)
853 videobuf_streamoff(&fh->vbiq);
854 if (fh->vbiq.reading)
855 videobuf_read_stop(&fh->vbiq);
856 res_free(dev, fh, RESOURCE_VBI);
857 }
858
859 videobuf_mmap_free(&fh->vidq);
860 file->private_data = NULL;
861 kfree(fh);
862
Steven Totha19602f22008-01-10 11:43:18 -0300863 /* We are not putting the tuner to sleep here on exit, because
864 * we want to use the mpeg encoder in another session to capture
865 * tuner video. Closing this will result in no video to the encoder.
866 */
Steven Tothe47f30b2008-01-10 04:25:59 -0300867
868 return 0;
869}
870
871static int video_mmap(struct file *file, struct vm_area_struct *vma)
872{
873 struct cx23885_fh *fh = file->private_data;
874
875 return videobuf_mmap_mapper(get_queue(fh), vma);
876}
877
878/* ------------------------------------------------------------------ */
879/* VIDEO CTRL IOCTLS */
880
Steven Toth9c8ced52008-10-16 20:18:44 -0300881static int cx23885_get_control(struct cx23885_dev *dev,
882 struct v4l2_control *ctl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300883{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300884 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -0300885 call_all(dev, core, g_ctrl, ctl);
Steven Tothe47f30b2008-01-10 04:25:59 -0300886 return 0;
887}
Steven Tothe47f30b2008-01-10 04:25:59 -0300888
Steven Toth9c8ced52008-10-16 20:18:44 -0300889static int cx23885_set_control(struct cx23885_dev *dev,
890 struct v4l2_control *ctl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300891{
892 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
Harvey Harrison22b4e642008-04-08 23:20:00 -0300893 " (disabled - no action)\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300894 return 0;
895}
Steven Tothe47f30b2008-01-10 04:25:59 -0300896
897static void init_controls(struct cx23885_dev *dev)
898{
899 struct v4l2_control ctrl;
900 int i;
901
902 for (i = 0; i < CX23885_CTLS; i++) {
903 ctrl.id = cx23885_ctls[i].v.id;
904 ctrl.value = cx23885_ctls[i].v.default_value;
905
906 cx23885_set_control(dev, &ctrl);
907 }
908}
909
910/* ------------------------------------------------------------------ */
911/* VIDEO IOCTLS */
912
Hans Verkuil78b526a2008-05-28 12:16:41 -0300913static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300914 struct v4l2_format *f)
915{
916 struct cx23885_fh *fh = priv;
917
918 f->fmt.pix.width = fh->width;
919 f->fmt.pix.height = fh->height;
920 f->fmt.pix.field = fh->vidq.field;
921 f->fmt.pix.pixelformat = fh->fmt->fourcc;
922 f->fmt.pix.bytesperline =
923 (f->fmt.pix.width * fh->fmt->depth) >> 3;
924 f->fmt.pix.sizeimage =
925 f->fmt.pix.height * f->fmt.pix.bytesperline;
926
927 return 0;
928}
929
Hans Verkuil78b526a2008-05-28 12:16:41 -0300930static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300931 struct v4l2_format *f)
932{
933 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
934 struct cx23885_fmt *fmt;
935 enum v4l2_field field;
936 unsigned int maxw, maxh;
937
938 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
939 if (NULL == fmt)
940 return -EINVAL;
941
942 field = f->fmt.pix.field;
943 maxw = norm_maxw(dev->tvnorm);
944 maxh = norm_maxh(dev->tvnorm);
945
946 if (V4L2_FIELD_ANY == field) {
947 field = (f->fmt.pix.height > maxh/2)
948 ? V4L2_FIELD_INTERLACED
949 : V4L2_FIELD_BOTTOM;
950 }
951
952 switch (field) {
953 case V4L2_FIELD_TOP:
954 case V4L2_FIELD_BOTTOM:
955 maxh = maxh / 2;
956 break;
957 case V4L2_FIELD_INTERLACED:
958 break;
959 default:
960 return -EINVAL;
961 }
962
963 f->fmt.pix.field = field;
964 if (f->fmt.pix.height < 32)
965 f->fmt.pix.height = 32;
966 if (f->fmt.pix.height > maxh)
967 f->fmt.pix.height = maxh;
968 if (f->fmt.pix.width < 48)
969 f->fmt.pix.width = 48;
970 if (f->fmt.pix.width > maxw)
971 f->fmt.pix.width = maxw;
972 f->fmt.pix.width &= ~0x03;
973 f->fmt.pix.bytesperline =
974 (f->fmt.pix.width * fmt->depth) >> 3;
975 f->fmt.pix.sizeimage =
976 f->fmt.pix.height * f->fmt.pix.bytesperline;
977
978 return 0;
979}
980
Hans Verkuil78b526a2008-05-28 12:16:41 -0300981static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300982 struct v4l2_format *f)
983{
984 struct cx23885_fh *fh = priv;
985 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
986 int err;
987
Harvey Harrison22b4e642008-04-08 23:20:00 -0300988 dprintk(2, "%s()\n", __func__);
Hans Verkuil78b526a2008-05-28 12:16:41 -0300989 err = vidioc_try_fmt_vid_cap(file, priv, f);
Steven Tothe47f30b2008-01-10 04:25:59 -0300990
991 if (0 != err)
992 return err;
993 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
994 fh->width = f->fmt.pix.width;
995 fh->height = f->fmt.pix.height;
996 fh->vidq.field = f->fmt.pix.field;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300997 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -0300998 fh->width, fh->height, fh->vidq.field);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -0300999 call_all(dev, video, s_fmt, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001000 return 0;
1001}
1002
1003static int vidioc_querycap(struct file *file, void *priv,
1004 struct v4l2_capability *cap)
1005{
1006 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1007
1008 strcpy(cap->driver, "cx23885");
1009 strlcpy(cap->card, cx23885_boards[dev->board].name,
1010 sizeof(cap->card));
1011 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1012 cap->version = CX23885_VERSION_CODE;
1013 cap->capabilities =
1014 V4L2_CAP_VIDEO_CAPTURE |
1015 V4L2_CAP_READWRITE |
1016 V4L2_CAP_STREAMING |
1017 V4L2_CAP_VBI_CAPTURE;
1018 if (UNSET != dev->tuner_type)
1019 cap->capabilities |= V4L2_CAP_TUNER;
1020 return 0;
1021}
1022
Hans Verkuil78b526a2008-05-28 12:16:41 -03001023static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001024 struct v4l2_fmtdesc *f)
1025{
1026 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1027 return -EINVAL;
1028
1029 strlcpy(f->description, formats[f->index].name,
1030 sizeof(f->description));
1031 f->pixelformat = formats[f->index].fourcc;
1032
1033 return 0;
1034}
1035
1036#ifdef CONFIG_VIDEO_V4L1_COMPAT
1037static int vidiocgmbuf(struct file *file, void *priv,
1038 struct video_mbuf *mbuf)
1039{
1040 struct cx23885_fh *fh = priv;
1041 struct videobuf_queue *q;
1042 struct v4l2_requestbuffers req;
1043 unsigned int i;
1044 int err;
1045
1046 q = get_queue(fh);
1047 memset(&req, 0, sizeof(req));
1048 req.type = q->type;
1049 req.count = 8;
1050 req.memory = V4L2_MEMORY_MMAP;
1051 err = videobuf_reqbufs(q, &req);
1052 if (err < 0)
1053 return err;
1054
1055 mbuf->frames = req.count;
1056 mbuf->size = 0;
1057 for (i = 0; i < mbuf->frames; i++) {
1058 mbuf->offsets[i] = q->bufs[i]->boff;
1059 mbuf->size += q->bufs[i]->bsize;
1060 }
1061 return 0;
1062}
1063#endif
1064
1065static int vidioc_reqbufs(struct file *file, void *priv,
1066 struct v4l2_requestbuffers *p)
1067{
1068 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001069 return videobuf_reqbufs(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001070}
1071
1072static int vidioc_querybuf(struct file *file, void *priv,
1073 struct v4l2_buffer *p)
1074{
1075 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001076 return videobuf_querybuf(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001077}
1078
1079static int vidioc_qbuf(struct file *file, void *priv,
1080 struct v4l2_buffer *p)
1081{
1082 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001083 return videobuf_qbuf(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001084}
1085
1086static int vidioc_dqbuf(struct file *file, void *priv,
1087 struct v4l2_buffer *p)
1088{
1089 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001090 return videobuf_dqbuf(get_queue(fh), p,
1091 file->f_flags & O_NONBLOCK);
Steven Tothe47f30b2008-01-10 04:25:59 -03001092}
1093
1094static int vidioc_streamon(struct file *file, void *priv,
1095 enum v4l2_buf_type i)
1096{
1097 struct cx23885_fh *fh = priv;
1098 struct cx23885_dev *dev = fh->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001099 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001100
1101 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1102 return -EINVAL;
1103 if (unlikely(i != fh->type))
1104 return -EINVAL;
1105
1106 if (unlikely(!res_get(dev, fh, get_resource(fh))))
1107 return -EBUSY;
1108 return videobuf_streamon(get_queue(fh));
1109}
1110
1111static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1112{
1113 struct cx23885_fh *fh = priv;
1114 struct cx23885_dev *dev = fh->dev;
1115 int err, res;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001116 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001117
1118 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1119 return -EINVAL;
1120 if (i != fh->type)
1121 return -EINVAL;
1122
1123 res = get_resource(fh);
1124 err = videobuf_streamoff(get_queue(fh));
1125 if (err < 0)
1126 return err;
1127 res_free(dev, fh, res);
1128 return 0;
1129}
1130
1131static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1132{
1133 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001134 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001135
1136 mutex_lock(&dev->lock);
1137 cx23885_set_tvnorm(dev, *tvnorms);
1138 mutex_unlock(&dev->lock);
1139
1140 return 0;
1141}
1142
Hans Verkuild45b9b82008-09-04 03:33:43 -03001143static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
Steven Tothe47f30b2008-01-10 04:25:59 -03001144{
1145 static const char *iname[] = {
1146 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1147 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1148 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1149 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1150 [CX23885_VMUX_SVIDEO] = "S-Video",
1151 [CX23885_VMUX_TELEVISION] = "Television",
1152 [CX23885_VMUX_CABLE] = "Cable TV",
1153 [CX23885_VMUX_DVB] = "DVB",
1154 [CX23885_VMUX_DEBUG] = "for debug only",
1155 };
1156 unsigned int n;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001157 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001158
1159 n = i->index;
1160 if (n >= 4)
1161 return -EINVAL;
1162
1163 if (0 == INPUT(n)->type)
1164 return -EINVAL;
1165
1166 memset(i, 0, sizeof(*i));
1167 i->index = n;
1168 i->type = V4L2_INPUT_TYPE_CAMERA;
1169 strcpy(i->name, iname[INPUT(n)->type]);
1170 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
1171 (CX23885_VMUX_CABLE == INPUT(n)->type))
1172 i->type = V4L2_INPUT_TYPE_TUNER;
1173 i->std = CX23885_NORMS;
1174 return 0;
1175}
Steven Tothe47f30b2008-01-10 04:25:59 -03001176
1177static int vidioc_enum_input(struct file *file, void *priv,
1178 struct v4l2_input *i)
1179{
1180 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001181 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001182 return cx23885_enum_input(dev, i);
1183}
1184
1185static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1186{
1187 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1188
1189 *i = dev->input;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001190 dprintk(1, "%s() returns %d\n", __func__, *i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001191 return 0;
1192}
1193
1194static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1195{
1196 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1197
Harvey Harrison22b4e642008-04-08 23:20:00 -03001198 dprintk(1, "%s(%d)\n", __func__, i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001199
1200 if (i >= 4) {
Harvey Harrison22b4e642008-04-08 23:20:00 -03001201 dprintk(1, "%s() -EINVAL\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001202 return -EINVAL;
1203 }
1204
1205 mutex_lock(&dev->lock);
1206 cx23885_video_mux(dev, i);
1207 mutex_unlock(&dev->lock);
1208 return 0;
1209}
1210
1211static int vidioc_queryctrl(struct file *file, void *priv,
1212 struct v4l2_queryctrl *qctrl)
1213{
1214 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1215 if (unlikely(qctrl->id == 0))
1216 return -EINVAL;
1217 return cx23885_ctrl_query(qctrl);
1218}
1219
1220static int vidioc_g_ctrl(struct file *file, void *priv,
1221 struct v4l2_control *ctl)
1222{
1223 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1224
1225 return cx23885_get_control(dev, ctl);
1226}
1227
1228static int vidioc_s_ctrl(struct file *file, void *priv,
1229 struct v4l2_control *ctl)
1230{
1231 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1232
1233 return cx23885_set_control(dev, ctl);
1234}
1235
1236static int vidioc_g_tuner(struct file *file, void *priv,
1237 struct v4l2_tuner *t)
1238{
1239 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1240
1241 if (unlikely(UNSET == dev->tuner_type))
1242 return -EINVAL;
1243 if (0 != t->index)
1244 return -EINVAL;
1245
1246 strcpy(t->name, "Television");
1247 t->type = V4L2_TUNER_ANALOG_TV;
1248 t->capability = V4L2_TUNER_CAP_NORM;
1249 t->rangehigh = 0xffffffffUL;
1250 t->signal = 0xffff ; /* LOCKED */
1251 return 0;
1252}
1253
1254static int vidioc_s_tuner(struct file *file, void *priv,
1255 struct v4l2_tuner *t)
1256{
1257 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1258
1259 if (UNSET == dev->tuner_type)
1260 return -EINVAL;
1261 if (0 != t->index)
1262 return -EINVAL;
1263 return 0;
1264}
1265
1266static int vidioc_g_frequency(struct file *file, void *priv,
1267 struct v4l2_frequency *f)
1268{
1269 struct cx23885_fh *fh = priv;
1270 struct cx23885_dev *dev = fh->dev;
1271
1272 if (unlikely(UNSET == dev->tuner_type))
1273 return -EINVAL;
1274
1275 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1276 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1277 f->frequency = dev->freq;
1278
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001279 call_all(dev, tuner, g_frequency, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001280
1281 return 0;
1282}
1283
Hans Verkuild45b9b82008-09-04 03:33:43 -03001284static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
Steven Tothe47f30b2008-01-10 04:25:59 -03001285{
1286 if (unlikely(UNSET == dev->tuner_type))
1287 return -EINVAL;
1288 if (unlikely(f->tuner != 0))
1289 return -EINVAL;
1290
1291 mutex_lock(&dev->lock);
1292 dev->freq = f->frequency;
1293
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001294 call_all(dev, tuner, s_frequency, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001295
1296 /* When changing channels it is required to reset TVAUDIO */
1297 msleep(10);
1298
1299 mutex_unlock(&dev->lock);
1300
1301 return 0;
1302}
Steven Tothe47f30b2008-01-10 04:25:59 -03001303
1304static int vidioc_s_frequency(struct file *file, void *priv,
1305 struct v4l2_frequency *f)
1306{
1307 struct cx23885_fh *fh = priv;
1308 struct cx23885_dev *dev = fh->dev;
1309
1310 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1311 return -EINVAL;
1312 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1313 return -EINVAL;
1314
1315 return
1316 cx23885_set_freq(dev, f);
1317}
1318
1319#ifdef CONFIG_VIDEO_ADV_DEBUG
1320static int vidioc_g_register(struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001321 struct v4l2_dbg_register *reg)
Steven Tothe47f30b2008-01-10 04:25:59 -03001322{
1323 struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1324
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001325 if (!v4l2_chip_match_host(&reg->match))
Steven Totha19602f22008-01-10 11:43:18 -03001326 return -EINVAL;
1327
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001328 call_all(dev, core, g_register, reg);
Steven Tothe47f30b2008-01-10 04:25:59 -03001329
1330 return 0;
1331}
1332
1333static int vidioc_s_register(struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001334 struct v4l2_dbg_register *reg)
Steven Tothe47f30b2008-01-10 04:25:59 -03001335{
1336 struct cx23885_dev *dev = ((struct cx23885_fh *)fh)->dev;
1337
Hans Verkuilaecde8b52008-12-30 07:14:19 -03001338 if (!v4l2_chip_match_host(&reg->match))
Steven Totha19602f22008-01-10 11:43:18 -03001339 return -EINVAL;
1340
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001341 call_all(dev, core, s_register, reg);
Steven Totha19602f22008-01-10 11:43:18 -03001342
Steven Tothe47f30b2008-01-10 04:25:59 -03001343 return 0;
1344}
1345#endif
1346
1347/* ----------------------------------------------------------- */
Steven Tothe47f30b2008-01-10 04:25:59 -03001348
1349static void cx23885_vid_timeout(unsigned long data)
1350{
1351 struct cx23885_dev *dev = (struct cx23885_dev *)data;
1352 struct cx23885_dmaqueue *q = &dev->vidq;
1353 struct cx23885_buffer *buf;
1354 unsigned long flags;
1355
1356 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1357
1358 cx_clear(VID_A_DMA_CTL, 0x11);
1359
1360 spin_lock_irqsave(&dev->slock, flags);
1361 while (!list_empty(&q->active)) {
1362 buf = list_entry(q->active.next,
1363 struct cx23885_buffer, vb.queue);
1364 list_del(&buf->vb.queue);
1365 buf->vb.state = VIDEOBUF_ERROR;
1366 wake_up(&buf->vb.done);
1367 printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
1368 dev->name, buf, buf->vb.i,
1369 (unsigned long)buf->risc.dma);
1370 }
1371 cx23885_restart_video_queue(dev, q);
1372 spin_unlock_irqrestore(&dev->slock, flags);
1373}
1374
1375int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1376{
1377 u32 mask, count;
1378 int handled = 0;
1379
1380 mask = cx_read(VID_A_INT_MSK);
1381 if (0 == (status & mask))
1382 return handled;
1383 cx_write(VID_A_INT_STAT, status);
1384
Harvey Harrison22b4e642008-04-08 23:20:00 -03001385 dprintk(2, "%s() status = 0x%08x\n", __func__, status);
Steven Tothe47f30b2008-01-10 04:25:59 -03001386 /* risc op code error */
1387 if (status & (1 << 16)) {
1388 printk(KERN_WARNING "%s/0: video risc op code error\n",
1389 dev->name);
1390 cx_clear(VID_A_DMA_CTL, 0x11);
1391 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1392 }
1393
1394 /* risc1 y */
1395 if (status & 0x01) {
1396 spin_lock(&dev->slock);
1397 count = cx_read(VID_A_GPCNT);
1398 cx23885_video_wakeup(dev, &dev->vidq, count);
1399 spin_unlock(&dev->slock);
1400 handled++;
1401 }
1402 /* risc2 y */
1403 if (status & 0x10) {
1404 dprintk(2, "stopper video\n");
1405 spin_lock(&dev->slock);
1406 cx23885_restart_video_queue(dev, &dev->vidq);
1407 spin_unlock(&dev->slock);
1408 handled++;
1409 }
1410
1411 return handled;
1412}
1413
Steven Tothe47f30b2008-01-10 04:25:59 -03001414/* ----------------------------------------------------------- */
1415/* exported stuff */
1416
Hans Verkuilbec43662008-12-30 06:58:20 -03001417static const struct v4l2_file_operations video_fops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001418 .owner = THIS_MODULE,
1419 .open = video_open,
1420 .release = video_release,
1421 .read = video_read,
1422 .poll = video_poll,
1423 .mmap = video_mmap,
1424 .ioctl = video_ioctl2,
Steven Tothe47f30b2008-01-10 04:25:59 -03001425};
1426
Hans Verkuila3998102008-07-21 02:57:38 -03001427static const struct v4l2_ioctl_ops video_ioctl_ops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001428 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001429 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1430 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1431 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1432 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1433 .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
1434 .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
1435 .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
Steven Tothe47f30b2008-01-10 04:25:59 -03001436 .vidioc_reqbufs = vidioc_reqbufs,
1437 .vidioc_querybuf = vidioc_querybuf,
1438 .vidioc_qbuf = vidioc_qbuf,
1439 .vidioc_dqbuf = vidioc_dqbuf,
1440 .vidioc_s_std = vidioc_s_std,
1441 .vidioc_enum_input = vidioc_enum_input,
1442 .vidioc_g_input = vidioc_g_input,
1443 .vidioc_s_input = vidioc_s_input,
1444 .vidioc_queryctrl = vidioc_queryctrl,
1445 .vidioc_g_ctrl = vidioc_g_ctrl,
1446 .vidioc_s_ctrl = vidioc_s_ctrl,
1447 .vidioc_streamon = vidioc_streamon,
1448 .vidioc_streamoff = vidioc_streamoff,
1449#ifdef CONFIG_VIDEO_V4L1_COMPAT
1450 .vidiocgmbuf = vidiocgmbuf,
1451#endif
1452 .vidioc_g_tuner = vidioc_g_tuner,
1453 .vidioc_s_tuner = vidioc_s_tuner,
1454 .vidioc_g_frequency = vidioc_g_frequency,
1455 .vidioc_s_frequency = vidioc_s_frequency,
1456#ifdef CONFIG_VIDEO_ADV_DEBUG
1457 .vidioc_g_register = vidioc_g_register,
1458 .vidioc_s_register = vidioc_s_register,
1459#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001460};
1461
1462static struct video_device cx23885_vbi_template;
1463static struct video_device cx23885_video_template = {
1464 .name = "cx23885-video",
Hans Verkuila3998102008-07-21 02:57:38 -03001465 .fops = &video_fops,
1466 .minor = -1,
1467 .ioctl_ops = &video_ioctl_ops,
Steven Tothe47f30b2008-01-10 04:25:59 -03001468 .tvnorms = CX23885_NORMS,
1469 .current_norm = V4L2_STD_NTSC_M,
1470};
1471
Hans Verkuilbec43662008-12-30 06:58:20 -03001472static const struct v4l2_file_operations radio_fops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001473 .owner = THIS_MODULE,
1474 .open = video_open,
1475 .release = video_release,
1476 .ioctl = video_ioctl2,
Steven Tothe47f30b2008-01-10 04:25:59 -03001477};
1478
1479
1480void cx23885_video_unregister(struct cx23885_dev *dev)
1481{
Harvey Harrison22b4e642008-04-08 23:20:00 -03001482 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001483 cx_clear(PCI_INT_MSK, 1);
1484
1485 if (dev->video_dev) {
1486 if (-1 != dev->video_dev->minor)
1487 video_unregister_device(dev->video_dev);
1488 else
1489 video_device_release(dev->video_dev);
1490 dev->video_dev = NULL;
1491
1492 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1493 }
1494}
1495
1496int cx23885_video_register(struct cx23885_dev *dev)
1497{
1498 int err;
1499
Harvey Harrison22b4e642008-04-08 23:20:00 -03001500 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001501 spin_lock_init(&dev->slock);
1502
1503 /* Initialize VBI template */
1504 memcpy(&cx23885_vbi_template, &cx23885_video_template,
1505 sizeof(cx23885_vbi_template));
1506 strcpy(cx23885_vbi_template.name, "cx23885-vbi");
Steven Tothe47f30b2008-01-10 04:25:59 -03001507
1508 dev->tvnorm = cx23885_video_template.current_norm;
1509
1510 /* init video dma queues */
1511 INIT_LIST_HEAD(&dev->vidq.active);
1512 INIT_LIST_HEAD(&dev->vidq.queued);
1513 dev->vidq.timeout.function = cx23885_vid_timeout;
1514 dev->vidq.timeout.data = (unsigned long)dev;
1515 init_timer(&dev->vidq.timeout);
1516 cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1517 VID_A_DMA_CTL, 0x11, 0x00);
1518
Steven Totha19602f22008-01-10 11:43:18 -03001519 /* Don't enable VBI yet */
Steven Tothe47f30b2008-01-10 04:25:59 -03001520 cx_set(PCI_INT_MSK, 1);
1521
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001522 if (TUNER_ABSENT != dev->tuner_type) {
1523 struct v4l2_subdev *sd = NULL;
1524
1525 if (dev->tuner_addr)
Hans Verkuile6574f22009-04-01 03:57:53 -03001526 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
1527 &dev->i2c_bus[1].i2c_adap,
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001528 "tuner", "tuner", dev->tuner_addr);
1529 else
Hans Verkuile6574f22009-04-01 03:57:53 -03001530 sd = v4l2_i2c_new_probed_subdev(&dev->v4l2_dev,
1531 &dev->i2c_bus[1].i2c_adap,
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001532 "tuner", "tuner", v4l2_i2c_tuner_addrs(ADDRS_TV));
1533 if (sd) {
1534 struct tuner_setup tun_setup;
1535
1536 tun_setup.mode_mask = T_ANALOG_TV;
1537 tun_setup.type = dev->tuner_type;
1538 tun_setup.addr = v4l2_i2c_subdev_addr(sd);
1539
1540 v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
1541 }
1542 }
1543
Steven Tothe47f30b2008-01-10 04:25:59 -03001544
1545 /* register v4l devices */
1546 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1547 &cx23885_video_template, "video");
1548 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1549 video_nr[dev->nr]);
1550 if (err < 0) {
1551 printk(KERN_INFO "%s: can't register video device\n",
1552 dev->name);
1553 goto fail_unreg;
1554 }
1555 printk(KERN_INFO "%s/0: registered device video%d [v4l2]\n",
Hans Verkuilc6330fb2008-10-19 18:54:26 -03001556 dev->name, dev->video_dev->num);
Steven Tothe47f30b2008-01-10 04:25:59 -03001557 /* initial device configuration */
1558 mutex_lock(&dev->lock);
1559 cx23885_set_tvnorm(dev, dev->tvnorm);
1560 init_controls(dev);
1561 cx23885_video_mux(dev, 0);
1562 mutex_unlock(&dev->lock);
1563
Steven Tothe47f30b2008-01-10 04:25:59 -03001564 return 0;
1565
1566fail_unreg:
1567 cx23885_video_unregister(dev);
1568 return err;
1569}
1570