blob: ee57f6bedbe3dd186d4f3255d55a0e82d8f4d151 [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>
Andy Walls74618242009-09-26 22:50:44 -030037#include "cx23885-ioctl.h"
Kusanagi Kouichi0b32d652010-01-22 04:55:28 -030038#include "tuner-xc2028.h"
Steven Tothe47f30b2008-01-10 04:25:59 -030039
Steven Tothe47f30b2008-01-10 04:25:59 -030040MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
Steven Toth6d897612008-09-03 17:12:12 -030041MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
Steven Tothe47f30b2008-01-10 04:25:59 -030042MODULE_LICENSE("GPL");
43
44/* ------------------------------------------------------------------ */
45
46static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
47static unsigned int vbi_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
48static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
49
50module_param_array(video_nr, int, NULL, 0444);
51module_param_array(vbi_nr, int, NULL, 0444);
52module_param_array(radio_nr, int, NULL, 0444);
53
54MODULE_PARM_DESC(video_nr, "video device numbers");
55MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
56MODULE_PARM_DESC(radio_nr, "radio device numbers");
57
Steven Toth4513fc692008-01-12 11:36:36 -030058static unsigned int video_debug;
Steven Tothe47f30b2008-01-10 04:25:59 -030059module_param(video_debug, int, 0644);
60MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
61
Steven Toth4513fc692008-01-12 11:36:36 -030062static unsigned int irq_debug;
Steven Tothe47f30b2008-01-10 04:25:59 -030063module_param(irq_debug, int, 0644);
64MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
65
66static unsigned int vid_limit = 16;
67module_param(vid_limit, int, 0644);
68MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
69
70#define dprintk(level, fmt, arg...)\
Steven Toth4513fc692008-01-12 11:36:36 -030071 do { if (video_debug >= level)\
72 printk(KERN_DEBUG "%s/0: " fmt, dev->name, ## arg);\
73 } while (0)
Steven Tothe47f30b2008-01-10 04:25:59 -030074
75/* ------------------------------------------------------------------- */
76/* static data */
77
78#define FORMAT_FLAGS_PACKED 0x01
79
80static struct cx23885_fmt formats[] = {
81 {
82 .name = "8 bpp, gray",
83 .fourcc = V4L2_PIX_FMT_GREY,
84 .depth = 8,
85 .flags = FORMAT_FLAGS_PACKED,
86 }, {
87 .name = "15 bpp RGB, le",
88 .fourcc = V4L2_PIX_FMT_RGB555,
89 .depth = 16,
90 .flags = FORMAT_FLAGS_PACKED,
91 }, {
92 .name = "15 bpp RGB, be",
93 .fourcc = V4L2_PIX_FMT_RGB555X,
94 .depth = 16,
95 .flags = FORMAT_FLAGS_PACKED,
96 }, {
97 .name = "16 bpp RGB, le",
98 .fourcc = V4L2_PIX_FMT_RGB565,
99 .depth = 16,
100 .flags = FORMAT_FLAGS_PACKED,
101 }, {
102 .name = "16 bpp RGB, be",
103 .fourcc = V4L2_PIX_FMT_RGB565X,
104 .depth = 16,
105 .flags = FORMAT_FLAGS_PACKED,
106 }, {
107 .name = "24 bpp RGB, le",
108 .fourcc = V4L2_PIX_FMT_BGR24,
109 .depth = 24,
110 .flags = FORMAT_FLAGS_PACKED,
111 }, {
112 .name = "32 bpp RGB, le",
113 .fourcc = V4L2_PIX_FMT_BGR32,
114 .depth = 32,
115 .flags = FORMAT_FLAGS_PACKED,
116 }, {
117 .name = "32 bpp RGB, be",
118 .fourcc = V4L2_PIX_FMT_RGB32,
119 .depth = 32,
120 .flags = FORMAT_FLAGS_PACKED,
121 }, {
122 .name = "4:2:2, packed, YUYV",
123 .fourcc = V4L2_PIX_FMT_YUYV,
124 .depth = 16,
125 .flags = FORMAT_FLAGS_PACKED,
126 }, {
127 .name = "4:2:2, packed, UYVY",
128 .fourcc = V4L2_PIX_FMT_UYVY,
129 .depth = 16,
130 .flags = FORMAT_FLAGS_PACKED,
131 },
132};
133
134static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
135{
136 unsigned int i;
137
138 for (i = 0; i < ARRAY_SIZE(formats); i++)
139 if (formats[i].fourcc == fourcc)
140 return formats+i;
141
Harvey Harrison22b4e642008-04-08 23:20:00 -0300142 printk(KERN_ERR "%s(0x%08x) NOT FOUND\n", __func__, fourcc);
Steven Tothe47f30b2008-01-10 04:25:59 -0300143 return NULL;
144}
145
146/* ------------------------------------------------------------------- */
147
148static const struct v4l2_queryctrl no_ctl = {
149 .name = "42",
150 .flags = V4L2_CTRL_FLAG_DISABLED,
151};
152
153static struct cx23885_ctrl cx23885_ctls[] = {
154 /* --- video --- */
155 {
156 .v = {
157 .id = V4L2_CID_BRIGHTNESS,
158 .name = "Brightness",
159 .minimum = 0x00,
160 .maximum = 0xff,
161 .step = 1,
162 .default_value = 0x7f,
163 .type = V4L2_CTRL_TYPE_INTEGER,
164 },
165 .off = 128,
166 .reg = LUMA_CTRL,
167 .mask = 0x00ff,
168 .shift = 0,
169 }, {
170 .v = {
171 .id = V4L2_CID_CONTRAST,
172 .name = "Contrast",
173 .minimum = 0,
174 .maximum = 0xff,
175 .step = 1,
176 .default_value = 0x3f,
177 .type = V4L2_CTRL_TYPE_INTEGER,
178 },
179 .off = 0,
180 .reg = LUMA_CTRL,
181 .mask = 0xff00,
182 .shift = 8,
183 }, {
184 .v = {
185 .id = V4L2_CID_HUE,
186 .name = "Hue",
187 .minimum = 0,
188 .maximum = 0xff,
189 .step = 1,
190 .default_value = 0x7f,
191 .type = V4L2_CTRL_TYPE_INTEGER,
192 },
193 .off = 128,
194 .reg = CHROMA_CTRL,
195 .mask = 0xff0000,
196 .shift = 16,
197 }, {
198 /* strictly, this only describes only U saturation.
199 * V saturation is handled specially through code.
200 */
201 .v = {
202 .id = V4L2_CID_SATURATION,
203 .name = "Saturation",
204 .minimum = 0,
205 .maximum = 0xff,
206 .step = 1,
207 .default_value = 0x7f,
208 .type = V4L2_CTRL_TYPE_INTEGER,
209 },
210 .off = 0,
211 .reg = CHROMA_CTRL,
212 .mask = 0x00ff,
213 .shift = 0,
214 }, {
215 /* --- audio --- */
216 .v = {
217 .id = V4L2_CID_AUDIO_MUTE,
218 .name = "Mute",
219 .minimum = 0,
220 .maximum = 1,
221 .default_value = 1,
222 .type = V4L2_CTRL_TYPE_BOOLEAN,
223 },
224 .reg = PATH1_CTL1,
225 .mask = (0x1f << 24),
226 .shift = 24,
227 }, {
228 .v = {
229 .id = V4L2_CID_AUDIO_VOLUME,
230 .name = "Volume",
231 .minimum = 0,
232 .maximum = 0x3f,
233 .step = 1,
234 .default_value = 0x3f,
235 .type = V4L2_CTRL_TYPE_INTEGER,
236 },
237 .reg = PATH1_VOL_CTL,
238 .mask = 0xff,
239 .shift = 0,
240 }
241};
242static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
243
Hans Verkuil2ba58892009-02-13 10:57:48 -0300244/* Must be sorted from low to high control ID! */
Hans Verkuild45b9b82008-09-04 03:33:43 -0300245static const u32 cx23885_user_ctrls[] = {
Steven Tothe47f30b2008-01-10 04:25:59 -0300246 V4L2_CID_USER_CLASS,
247 V4L2_CID_BRIGHTNESS,
248 V4L2_CID_CONTRAST,
249 V4L2_CID_SATURATION,
250 V4L2_CID_HUE,
251 V4L2_CID_AUDIO_VOLUME,
252 V4L2_CID_AUDIO_MUTE,
253 0
254};
Steven Tothe47f30b2008-01-10 04:25:59 -0300255
256static const u32 *ctrl_classes[] = {
257 cx23885_user_ctrls,
258 NULL
259};
260
Hans Verkuild45b9b82008-09-04 03:33:43 -0300261static void cx23885_video_wakeup(struct cx23885_dev *dev,
Steven Tothe47f30b2008-01-10 04:25:59 -0300262 struct cx23885_dmaqueue *q, u32 count)
263{
264 struct cx23885_buffer *buf;
265 int bc;
266
267 for (bc = 0;; bc++) {
268 if (list_empty(&q->active))
269 break;
270 buf = list_entry(q->active.next,
271 struct cx23885_buffer, vb.queue);
Steven Totha19602f22008-01-10 11:43:18 -0300272
Steven Tothe47f30b2008-01-10 04:25:59 -0300273 /* count comes from the hw and is is 16bit wide --
274 * this trick handles wrap-arounds correctly for
275 * up to 32767 buffers in flight... */
276 if ((s16) (count - buf->count) < 0)
277 break;
Steven Totha19602f22008-01-10 11:43:18 -0300278
Steven Tothe47f30b2008-01-10 04:25:59 -0300279 do_gettimeofday(&buf->vb.ts);
280 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
281 count, buf->count);
282 buf->vb.state = VIDEOBUF_DONE;
283 list_del(&buf->vb.queue);
284 wake_up(&buf->vb.done);
285 }
Steven Toth9c8ced52008-10-16 20:18:44 -0300286 if (list_empty(&q->active))
Steven Tothe47f30b2008-01-10 04:25:59 -0300287 del_timer(&q->timeout);
Steven Toth9c8ced52008-10-16 20:18:44 -0300288 else
Steven Tothe47f30b2008-01-10 04:25:59 -0300289 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
Steven Tothe47f30b2008-01-10 04:25:59 -0300290 if (bc != 1)
Steven Toth21a78092008-01-10 04:38:59 -0300291 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300292 __func__, bc);
Steven Tothe47f30b2008-01-10 04:25:59 -0300293}
294
Hans Verkuild45b9b82008-09-04 03:33:43 -0300295static int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
Steven Tothe47f30b2008-01-10 04:25:59 -0300296{
297 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300298 __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -0300299 (unsigned int)norm,
300 v4l2_norm_to_name(norm));
301
302 dev->tvnorm = norm;
303
Hans Verkuilf41737e2009-04-01 03:52:39 -0300304 call_all(dev, core, s_std, norm);
Steven Tothe47f30b2008-01-10 04:25:59 -0300305
306 return 0;
307}
308
Hans Verkuild45b9b82008-09-04 03:33:43 -0300309static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
Steven Tothe47f30b2008-01-10 04:25:59 -0300310 struct pci_dev *pci,
311 struct video_device *template,
312 char *type)
313{
314 struct video_device *vfd;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300315 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300316
317 vfd = video_device_alloc();
318 if (NULL == vfd)
319 return NULL;
320 *vfd = *template;
Hans Verkuilc0714f62009-03-13 08:02:43 -0300321 vfd->v4l2_dev = &dev->v4l2_dev;
Steven Tothe47f30b2008-01-10 04:25:59 -0300322 vfd->release = video_device_release;
323 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
324 dev->name, type, cx23885_boards[dev->board].name);
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200325 video_set_drvdata(vfd, dev);
Steven Tothe47f30b2008-01-10 04:25:59 -0300326 return vfd;
327}
328
Hans Verkuild45b9b82008-09-04 03:33:43 -0300329static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300330{
331 int i;
332
333 if (qctrl->id < V4L2_CID_BASE ||
334 qctrl->id >= V4L2_CID_LASTP1)
335 return -EINVAL;
336 for (i = 0; i < CX23885_CTLS; i++)
337 if (cx23885_ctls[i].v.id == qctrl->id)
338 break;
339 if (i == CX23885_CTLS) {
340 *qctrl = no_ctl;
341 return 0;
342 }
343 *qctrl = cx23885_ctls[i].v;
344 return 0;
345}
Steven Tothe47f30b2008-01-10 04:25:59 -0300346
347/* ------------------------------------------------------------------- */
348/* resource management */
349
350static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
351 unsigned int bit)
352{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300353 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300354 if (fh->resources & bit)
355 /* have it already allocated */
356 return 1;
357
358 /* is it free? */
359 mutex_lock(&dev->lock);
360 if (dev->resources & bit) {
361 /* no, someone else uses it */
362 mutex_unlock(&dev->lock);
363 return 0;
364 }
365 /* it's free, grab it */
366 fh->resources |= bit;
367 dev->resources |= bit;
368 dprintk(1, "res: get %d\n", bit);
369 mutex_unlock(&dev->lock);
370 return 1;
371}
372
373static int res_check(struct cx23885_fh *fh, unsigned int bit)
374{
Steven Toth9c8ced52008-10-16 20:18:44 -0300375 return fh->resources & bit;
Steven Tothe47f30b2008-01-10 04:25:59 -0300376}
377
378static int res_locked(struct cx23885_dev *dev, unsigned int bit)
379{
Steven Toth9c8ced52008-10-16 20:18:44 -0300380 return dev->resources & bit;
Steven Tothe47f30b2008-01-10 04:25:59 -0300381}
382
383static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
384 unsigned int bits)
385{
386 BUG_ON((fh->resources & bits) != bits);
Harvey Harrison22b4e642008-04-08 23:20:00 -0300387 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300388
389 mutex_lock(&dev->lock);
390 fh->resources &= ~bits;
391 dev->resources &= ~bits;
392 dprintk(1, "res: put %d\n", bits);
393 mutex_unlock(&dev->lock);
394}
395
Hans Verkuild45b9b82008-09-04 03:33:43 -0300396static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
Steven Tothe47f30b2008-01-10 04:25:59 -0300397{
Steven Tothe47f30b2008-01-10 04:25:59 -0300398 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 -0300399 __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -0300400 input, INPUT(input)->vmux,
401 INPUT(input)->gpio0, INPUT(input)->gpio1,
402 INPUT(input)->gpio2, INPUT(input)->gpio3);
403 dev->input = input;
404
David T.L. Wong6f0d8c02009-10-21 13:15:30 -0300405 if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
406 dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2) {
407 /* Select Analog TV */
408 if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
409 cx23885_gpio_clear(dev, GPIO_0);
410 }
411
Steven Tothe47f30b2008-01-10 04:25:59 -0300412 /* Tell the internal A/V decoder */
Hans Verkuil5325b422009-04-02 11:26:22 -0300413 v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
414 INPUT(input)->vmux, 0, 0);
Steven Tothe47f30b2008-01-10 04:25:59 -0300415
416 return 0;
417}
Steven Tothe47f30b2008-01-10 04:25:59 -0300418
419/* ------------------------------------------------------------------ */
Hans Verkuild45b9b82008-09-04 03:33:43 -0300420static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
Steven Tothe47f30b2008-01-10 04:25:59 -0300421 unsigned int height, enum v4l2_field field)
422{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300423 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300424 return 0;
425}
426
427static int cx23885_start_video_dma(struct cx23885_dev *dev,
428 struct cx23885_dmaqueue *q,
429 struct cx23885_buffer *buf)
430{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300431 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300432
433 /* setup fifo + format */
434 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
435 buf->bpl, buf->risc.dma);
436 cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
437
438 /* reset counter */
439 cx_write(VID_A_GPCNT_CTL, 3);
440 q->count = 1;
441
442 /* enable irq */
Andy Wallsdbe83a32010-07-19 01:19:43 -0300443 cx23885_irq_add_enable(dev, 0x01);
Steven Tothe47f30b2008-01-10 04:25:59 -0300444 cx_set(VID_A_INT_MSK, 0x000011);
445
446 /* start dma */
447 cx_set(DEV_CNTRL2, (1<<5));
448 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
449
450 return 0;
451}
452
Steven Tothe47f30b2008-01-10 04:25:59 -0300453
454static int cx23885_restart_video_queue(struct cx23885_dev *dev,
455 struct cx23885_dmaqueue *q)
456{
457 struct cx23885_buffer *buf, *prev;
458 struct list_head *item;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300459 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300460
461 if (!list_empty(&q->active)) {
462 buf = list_entry(q->active.next, struct cx23885_buffer,
463 vb.queue);
464 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
465 buf, buf->vb.i);
466 cx23885_start_video_dma(dev, q, buf);
467 list_for_each(item, &q->active) {
468 buf = list_entry(item, struct cx23885_buffer,
469 vb.queue);
470 buf->count = q->count++;
471 }
472 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
473 return 0;
474 }
475
476 prev = NULL;
477 for (;;) {
478 if (list_empty(&q->queued))
479 return 0;
480 buf = list_entry(q->queued.next, struct cx23885_buffer,
481 vb.queue);
482 if (NULL == prev) {
483 list_move_tail(&buf->vb.queue, &q->active);
484 cx23885_start_video_dma(dev, q, buf);
485 buf->vb.state = VIDEOBUF_ACTIVE;
486 buf->count = q->count++;
487 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
488 dprintk(2, "[%p/%d] restart_queue - first active\n",
489 buf, buf->vb.i);
490
491 } else if (prev->vb.width == buf->vb.width &&
492 prev->vb.height == buf->vb.height &&
493 prev->fmt == buf->fmt) {
494 list_move_tail(&buf->vb.queue, &q->active);
495 buf->vb.state = VIDEOBUF_ACTIVE;
496 buf->count = q->count++;
497 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
498 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
499 dprintk(2, "[%p/%d] restart_queue - move to active\n",
500 buf, buf->vb.i);
501 } else {
502 return 0;
503 }
504 prev = buf;
505 }
506}
507
508static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
509 unsigned int *size)
510{
511 struct cx23885_fh *fh = q->priv_data;
512
513 *size = fh->fmt->depth*fh->width*fh->height >> 3;
514 if (0 == *count)
515 *count = 32;
Andreas Bombedab7e312010-03-21 16:02:45 -0300516 if (*size * *count > vid_limit * 1024 * 1024)
517 *count = (vid_limit * 1024 * 1024) / *size;
Steven Tothe47f30b2008-01-10 04:25:59 -0300518 return 0;
519}
520
521static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
522 enum v4l2_field field)
523{
524 struct cx23885_fh *fh = q->priv_data;
525 struct cx23885_dev *dev = fh->dev;
526 struct cx23885_buffer *buf =
527 container_of(vb, struct cx23885_buffer, vb);
528 int rc, init_buffer = 0;
529 u32 line0_offset, line1_offset;
530 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
531
532 BUG_ON(NULL == fh->fmt);
533 if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
534 fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
535 return -EINVAL;
536 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
537 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
538 return -EINVAL;
539
540 if (buf->fmt != fh->fmt ||
541 buf->vb.width != fh->width ||
542 buf->vb.height != fh->height ||
543 buf->vb.field != field) {
544 buf->fmt = fh->fmt;
545 buf->vb.width = fh->width;
546 buf->vb.height = fh->height;
547 buf->vb.field = field;
548 init_buffer = 1;
549 }
550
551 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
552 init_buffer = 1;
553 rc = videobuf_iolock(q, &buf->vb, NULL);
554 if (0 != rc)
555 goto fail;
556 }
557
558 if (init_buffer) {
559 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
560 switch (buf->vb.field) {
561 case V4L2_FIELD_TOP:
562 cx23885_risc_buffer(dev->pci, &buf->risc,
563 dma->sglist, 0, UNSET,
564 buf->bpl, 0, buf->vb.height);
565 break;
566 case V4L2_FIELD_BOTTOM:
567 cx23885_risc_buffer(dev->pci, &buf->risc,
568 dma->sglist, UNSET, 0,
569 buf->bpl, 0, buf->vb.height);
570 break;
571 case V4L2_FIELD_INTERLACED:
572 if (dev->tvnorm & V4L2_STD_NTSC) {
573 /* cx25840 transmits NTSC bottom field first */
574 dprintk(1, "%s() Creating NTSC risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300575 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300576 line0_offset = buf->bpl;
577 line1_offset = 0;
578 } else {
579 /* All other formats are top field first */
580 dprintk(1, "%s() Creating PAL/SECAM risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300581 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300582 line0_offset = 0;
583 line1_offset = buf->bpl;
584 }
585 cx23885_risc_buffer(dev->pci, &buf->risc,
586 dma->sglist, line0_offset,
587 line1_offset,
588 buf->bpl, buf->bpl,
589 buf->vb.height >> 1);
590 break;
591 case V4L2_FIELD_SEQ_TB:
592 cx23885_risc_buffer(dev->pci, &buf->risc,
593 dma->sglist,
594 0, buf->bpl * (buf->vb.height >> 1),
595 buf->bpl, 0,
596 buf->vb.height >> 1);
597 break;
598 case V4L2_FIELD_SEQ_BT:
599 cx23885_risc_buffer(dev->pci, &buf->risc,
600 dma->sglist,
601 buf->bpl * (buf->vb.height >> 1), 0,
602 buf->bpl, 0,
603 buf->vb.height >> 1);
604 break;
605 default:
606 BUG();
607 }
608 }
609 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
610 buf, buf->vb.i,
611 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
612 (unsigned long)buf->risc.dma);
613
614 buf->vb.state = VIDEOBUF_PREPARED;
615 return 0;
616
617 fail:
618 cx23885_free_buffer(q, buf);
619 return rc;
620}
621
622static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
623{
624 struct cx23885_buffer *buf = container_of(vb,
625 struct cx23885_buffer, vb);
626 struct cx23885_buffer *prev;
627 struct cx23885_fh *fh = vq->priv_data;
628 struct cx23885_dev *dev = fh->dev;
629 struct cx23885_dmaqueue *q = &dev->vidq;
630
631 /* add jump to stopper */
632 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
633 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
634 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
635
636 if (!list_empty(&q->queued)) {
637 list_add_tail(&buf->vb.queue, &q->queued);
638 buf->vb.state = VIDEOBUF_QUEUED;
639 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
640 buf, buf->vb.i);
641
642 } else if (list_empty(&q->active)) {
643 list_add_tail(&buf->vb.queue, &q->active);
644 cx23885_start_video_dma(dev, q, buf);
645 buf->vb.state = VIDEOBUF_ACTIVE;
646 buf->count = q->count++;
647 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
648 dprintk(2, "[%p/%d] buffer_queue - first active\n",
649 buf, buf->vb.i);
650
651 } else {
652 prev = list_entry(q->active.prev, struct cx23885_buffer,
653 vb.queue);
654 if (prev->vb.width == buf->vb.width &&
655 prev->vb.height == buf->vb.height &&
656 prev->fmt == buf->fmt) {
657 list_add_tail(&buf->vb.queue, &q->active);
658 buf->vb.state = VIDEOBUF_ACTIVE;
659 buf->count = q->count++;
660 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
661 /* 64 bit bits 63-32 */
662 prev->risc.jmp[2] = cpu_to_le32(0);
663 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
664 buf, buf->vb.i);
665
666 } else {
667 list_add_tail(&buf->vb.queue, &q->queued);
668 buf->vb.state = VIDEOBUF_QUEUED;
669 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
670 buf, buf->vb.i);
671 }
672 }
673}
674
675static void buffer_release(struct videobuf_queue *q,
676 struct videobuf_buffer *vb)
677{
678 struct cx23885_buffer *buf = container_of(vb,
679 struct cx23885_buffer, vb);
680
681 cx23885_free_buffer(q, buf);
682}
683
684static struct videobuf_queue_ops cx23885_video_qops = {
685 .buf_setup = buffer_setup,
686 .buf_prepare = buffer_prepare,
687 .buf_queue = buffer_queue,
688 .buf_release = buffer_release,
689};
690
691static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
692{
693 switch (fh->type) {
694 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
695 return &fh->vidq;
696 case V4L2_BUF_TYPE_VBI_CAPTURE:
697 return &fh->vbiq;
698 default:
699 BUG();
700 return NULL;
701 }
702}
703
704static int get_resource(struct cx23885_fh *fh)
705{
706 switch (fh->type) {
707 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
708 return RESOURCE_VIDEO;
709 case V4L2_BUF_TYPE_VBI_CAPTURE:
710 return RESOURCE_VBI;
711 default:
712 BUG();
713 return 0;
714 }
715}
716
Hans Verkuilbec43662008-12-30 06:58:20 -0300717static int video_open(struct file *file)
Steven Tothe47f30b2008-01-10 04:25:59 -0300718{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200719 struct video_device *vdev = video_devdata(file);
720 struct cx23885_dev *dev = video_drvdata(file);
Steven Tothe47f30b2008-01-10 04:25:59 -0300721 struct cx23885_fh *fh;
Steven Tothe47f30b2008-01-10 04:25:59 -0300722 enum v4l2_buf_type type = 0;
723 int radio = 0;
724
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200725 switch (vdev->vfl_type) {
726 case VFL_TYPE_GRABBER:
727 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
728 break;
729 case VFL_TYPE_VBI:
730 type = V4L2_BUF_TYPE_VBI_CAPTURE;
731 break;
732 case VFL_TYPE_RADIO:
733 radio = 1;
734 break;
Hans Verkuild56dc612008-07-30 08:43:36 -0300735 }
Steven Tothe47f30b2008-01-10 04:25:59 -0300736
Laurent Pinchart50462eb2009-12-10 11:47:13 -0200737 dprintk(1, "open dev=%s radio=%d type=%s\n",
738 video_device_node_name(vdev), radio, v4l2_type_names[type]);
Steven Tothe47f30b2008-01-10 04:25:59 -0300739
740 /* allocate + initialize per filehandle data */
741 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200742 if (NULL == fh)
Steven Tothe47f30b2008-01-10 04:25:59 -0300743 return -ENOMEM;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200744
Steven Tothe47f30b2008-01-10 04:25:59 -0300745 file->private_data = fh;
746 fh->dev = dev;
747 fh->radio = radio;
748 fh->type = type;
749 fh->width = 320;
750 fh->height = 240;
751 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
752
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300753 videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
754 &dev->pci->dev, &dev->slock,
Steven Tothe47f30b2008-01-10 04:25:59 -0300755 V4L2_BUF_TYPE_VIDEO_CAPTURE,
756 V4L2_FIELD_INTERLACED,
757 sizeof(struct cx23885_buffer),
Hans Verkuil08bff032010-09-20 17:39:46 -0300758 fh, NULL);
Steven Tothe47f30b2008-01-10 04:25:59 -0300759
760 dprintk(1, "post videobuf_queue_init()\n");
761
Steven Tothe47f30b2008-01-10 04:25:59 -0300762 return 0;
763}
764
765static ssize_t video_read(struct file *file, char __user *data,
766 size_t count, loff_t *ppos)
767{
768 struct cx23885_fh *fh = file->private_data;
769
770 switch (fh->type) {
771 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
772 if (res_locked(fh->dev, RESOURCE_VIDEO))
773 return -EBUSY;
774 return videobuf_read_one(&fh->vidq, data, count, ppos,
775 file->f_flags & O_NONBLOCK);
776 case V4L2_BUF_TYPE_VBI_CAPTURE:
777 if (!res_get(fh->dev, fh, RESOURCE_VBI))
778 return -EBUSY;
779 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
780 file->f_flags & O_NONBLOCK);
781 default:
782 BUG();
783 return 0;
784 }
785}
786
787static unsigned int video_poll(struct file *file,
788 struct poll_table_struct *wait)
789{
790 struct cx23885_fh *fh = file->private_data;
791 struct cx23885_buffer *buf;
Figo.zhang9fd64182009-06-16 13:31:29 -0300792 unsigned int rc = POLLERR;
Steven Tothe47f30b2008-01-10 04:25:59 -0300793
794 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
795 if (!res_get(fh->dev, fh, RESOURCE_VBI))
796 return POLLERR;
797 return videobuf_poll_stream(file, &fh->vbiq, wait);
798 }
799
Figo.zhang9fd64182009-06-16 13:31:29 -0300800 mutex_lock(&fh->vidq.vb_lock);
Steven Tothe47f30b2008-01-10 04:25:59 -0300801 if (res_check(fh, RESOURCE_VIDEO)) {
802 /* streaming capture */
803 if (list_empty(&fh->vidq.stream))
Figo.zhang9fd64182009-06-16 13:31:29 -0300804 goto done;
Steven Tothe47f30b2008-01-10 04:25:59 -0300805 buf = list_entry(fh->vidq.stream.next,
806 struct cx23885_buffer, vb.stream);
807 } else {
808 /* read() capture */
809 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
810 if (NULL == buf)
Figo.zhang9fd64182009-06-16 13:31:29 -0300811 goto done;
Steven Tothe47f30b2008-01-10 04:25:59 -0300812 }
813 poll_wait(file, &buf->vb.done, wait);
814 if (buf->vb.state == VIDEOBUF_DONE ||
815 buf->vb.state == VIDEOBUF_ERROR)
Figo.zhang9fd64182009-06-16 13:31:29 -0300816 rc = POLLIN|POLLRDNORM;
817 else
818 rc = 0;
819done:
820 mutex_unlock(&fh->vidq.vb_lock);
821 return rc;
Steven Tothe47f30b2008-01-10 04:25:59 -0300822}
823
Hans Verkuilbec43662008-12-30 06:58:20 -0300824static int video_release(struct file *file)
Steven Tothe47f30b2008-01-10 04:25:59 -0300825{
826 struct cx23885_fh *fh = file->private_data;
827 struct cx23885_dev *dev = fh->dev;
828
829 /* turn off overlay */
830 if (res_check(fh, RESOURCE_OVERLAY)) {
831 /* FIXME */
832 res_free(dev, fh, RESOURCE_OVERLAY);
833 }
834
835 /* stop video capture */
836 if (res_check(fh, RESOURCE_VIDEO)) {
837 videobuf_queue_cancel(&fh->vidq);
838 res_free(dev, fh, RESOURCE_VIDEO);
839 }
840 if (fh->vidq.read_buf) {
841 buffer_release(&fh->vidq, fh->vidq.read_buf);
842 kfree(fh->vidq.read_buf);
843 }
844
845 /* stop vbi capture */
846 if (res_check(fh, RESOURCE_VBI)) {
847 if (fh->vbiq.streaming)
848 videobuf_streamoff(&fh->vbiq);
849 if (fh->vbiq.reading)
850 videobuf_read_stop(&fh->vbiq);
851 res_free(dev, fh, RESOURCE_VBI);
852 }
853
854 videobuf_mmap_free(&fh->vidq);
855 file->private_data = NULL;
856 kfree(fh);
857
Steven Totha19602f22008-01-10 11:43:18 -0300858 /* We are not putting the tuner to sleep here on exit, because
859 * we want to use the mpeg encoder in another session to capture
860 * tuner video. Closing this will result in no video to the encoder.
861 */
Steven Tothe47f30b2008-01-10 04:25:59 -0300862
863 return 0;
864}
865
866static int video_mmap(struct file *file, struct vm_area_struct *vma)
867{
868 struct cx23885_fh *fh = file->private_data;
869
870 return videobuf_mmap_mapper(get_queue(fh), vma);
871}
872
873/* ------------------------------------------------------------------ */
874/* VIDEO CTRL IOCTLS */
875
Steven Toth9c8ced52008-10-16 20:18:44 -0300876static int cx23885_get_control(struct cx23885_dev *dev,
877 struct v4l2_control *ctl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300878{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300879 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -0300880 call_all(dev, core, g_ctrl, ctl);
Steven Tothe47f30b2008-01-10 04:25:59 -0300881 return 0;
882}
Steven Tothe47f30b2008-01-10 04:25:59 -0300883
Steven Toth9c8ced52008-10-16 20:18:44 -0300884static int cx23885_set_control(struct cx23885_dev *dev,
885 struct v4l2_control *ctl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300886{
887 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)"
Harvey Harrison22b4e642008-04-08 23:20:00 -0300888 " (disabled - no action)\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300889 return 0;
890}
Steven Tothe47f30b2008-01-10 04:25:59 -0300891
892static void init_controls(struct cx23885_dev *dev)
893{
894 struct v4l2_control ctrl;
895 int i;
896
897 for (i = 0; i < CX23885_CTLS; i++) {
898 ctrl.id = cx23885_ctls[i].v.id;
899 ctrl.value = cx23885_ctls[i].v.default_value;
900
901 cx23885_set_control(dev, &ctrl);
902 }
903}
904
905/* ------------------------------------------------------------------ */
906/* VIDEO IOCTLS */
907
Hans Verkuil78b526a2008-05-28 12:16:41 -0300908static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300909 struct v4l2_format *f)
910{
911 struct cx23885_fh *fh = priv;
912
913 f->fmt.pix.width = fh->width;
914 f->fmt.pix.height = fh->height;
915 f->fmt.pix.field = fh->vidq.field;
916 f->fmt.pix.pixelformat = fh->fmt->fourcc;
917 f->fmt.pix.bytesperline =
918 (f->fmt.pix.width * fh->fmt->depth) >> 3;
919 f->fmt.pix.sizeimage =
920 f->fmt.pix.height * f->fmt.pix.bytesperline;
921
922 return 0;
923}
924
Hans Verkuil78b526a2008-05-28 12:16:41 -0300925static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300926 struct v4l2_format *f)
927{
928 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
929 struct cx23885_fmt *fmt;
930 enum v4l2_field field;
931 unsigned int maxw, maxh;
932
933 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
934 if (NULL == fmt)
935 return -EINVAL;
936
937 field = f->fmt.pix.field;
938 maxw = norm_maxw(dev->tvnorm);
939 maxh = norm_maxh(dev->tvnorm);
940
941 if (V4L2_FIELD_ANY == field) {
942 field = (f->fmt.pix.height > maxh/2)
943 ? V4L2_FIELD_INTERLACED
944 : V4L2_FIELD_BOTTOM;
945 }
946
947 switch (field) {
948 case V4L2_FIELD_TOP:
949 case V4L2_FIELD_BOTTOM:
950 maxh = maxh / 2;
951 break;
952 case V4L2_FIELD_INTERLACED:
953 break;
954 default:
955 return -EINVAL;
956 }
957
958 f->fmt.pix.field = field;
Trent Piepho2449afc2009-05-30 21:45:46 -0300959 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
960 &f->fmt.pix.height, 32, maxh, 0, 0);
Steven Tothe47f30b2008-01-10 04:25:59 -0300961 f->fmt.pix.bytesperline =
962 (f->fmt.pix.width * fmt->depth) >> 3;
963 f->fmt.pix.sizeimage =
964 f->fmt.pix.height * f->fmt.pix.bytesperline;
965
966 return 0;
967}
968
Hans Verkuil78b526a2008-05-28 12:16:41 -0300969static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300970 struct v4l2_format *f)
971{
972 struct cx23885_fh *fh = priv;
973 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Hans Verkuilcc991132010-05-09 10:09:28 -0300974 struct v4l2_mbus_framefmt mbus_fmt;
Steven Tothe47f30b2008-01-10 04:25:59 -0300975 int err;
976
Harvey Harrison22b4e642008-04-08 23:20:00 -0300977 dprintk(2, "%s()\n", __func__);
Hans Verkuil78b526a2008-05-28 12:16:41 -0300978 err = vidioc_try_fmt_vid_cap(file, priv, f);
Steven Tothe47f30b2008-01-10 04:25:59 -0300979
980 if (0 != err)
981 return err;
982 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
983 fh->width = f->fmt.pix.width;
984 fh->height = f->fmt.pix.height;
985 fh->vidq.field = f->fmt.pix.field;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300986 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -0300987 fh->width, fh->height, fh->vidq.field);
Hans Verkuilcc991132010-05-09 10:09:28 -0300988 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
989 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
990 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
Steven Tothe47f30b2008-01-10 04:25:59 -0300991 return 0;
992}
993
994static int vidioc_querycap(struct file *file, void *priv,
995 struct v4l2_capability *cap)
996{
997 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
998
999 strcpy(cap->driver, "cx23885");
1000 strlcpy(cap->card, cx23885_boards[dev->board].name,
1001 sizeof(cap->card));
1002 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
1003 cap->version = CX23885_VERSION_CODE;
1004 cap->capabilities =
1005 V4L2_CAP_VIDEO_CAPTURE |
1006 V4L2_CAP_READWRITE |
1007 V4L2_CAP_STREAMING |
1008 V4L2_CAP_VBI_CAPTURE;
1009 if (UNSET != dev->tuner_type)
1010 cap->capabilities |= V4L2_CAP_TUNER;
1011 return 0;
1012}
1013
Hans Verkuil78b526a2008-05-28 12:16:41 -03001014static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001015 struct v4l2_fmtdesc *f)
1016{
1017 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1018 return -EINVAL;
1019
1020 strlcpy(f->description, formats[f->index].name,
1021 sizeof(f->description));
1022 f->pixelformat = formats[f->index].fourcc;
1023
1024 return 0;
1025}
1026
Steven Tothe47f30b2008-01-10 04:25:59 -03001027static int vidioc_reqbufs(struct file *file, void *priv,
1028 struct v4l2_requestbuffers *p)
1029{
1030 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001031 return videobuf_reqbufs(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001032}
1033
1034static int vidioc_querybuf(struct file *file, void *priv,
1035 struct v4l2_buffer *p)
1036{
1037 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001038 return videobuf_querybuf(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001039}
1040
1041static int vidioc_qbuf(struct file *file, void *priv,
1042 struct v4l2_buffer *p)
1043{
1044 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001045 return videobuf_qbuf(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001046}
1047
1048static int vidioc_dqbuf(struct file *file, void *priv,
1049 struct v4l2_buffer *p)
1050{
1051 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001052 return videobuf_dqbuf(get_queue(fh), p,
1053 file->f_flags & O_NONBLOCK);
Steven Tothe47f30b2008-01-10 04:25:59 -03001054}
1055
1056static int vidioc_streamon(struct file *file, void *priv,
1057 enum v4l2_buf_type i)
1058{
1059 struct cx23885_fh *fh = priv;
1060 struct cx23885_dev *dev = fh->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001061 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001062
1063 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE))
1064 return -EINVAL;
1065 if (unlikely(i != fh->type))
1066 return -EINVAL;
1067
1068 if (unlikely(!res_get(dev, fh, get_resource(fh))))
1069 return -EBUSY;
1070 return videobuf_streamon(get_queue(fh));
1071}
1072
1073static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1074{
1075 struct cx23885_fh *fh = priv;
1076 struct cx23885_dev *dev = fh->dev;
1077 int err, res;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001078 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001079
1080 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1081 return -EINVAL;
1082 if (i != fh->type)
1083 return -EINVAL;
1084
1085 res = get_resource(fh);
1086 err = videobuf_streamoff(get_queue(fh));
1087 if (err < 0)
1088 return err;
1089 res_free(dev, fh, res);
1090 return 0;
1091}
1092
1093static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1094{
1095 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001096 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001097
1098 mutex_lock(&dev->lock);
1099 cx23885_set_tvnorm(dev, *tvnorms);
1100 mutex_unlock(&dev->lock);
1101
1102 return 0;
1103}
1104
Hans Verkuild45b9b82008-09-04 03:33:43 -03001105static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
Steven Tothe47f30b2008-01-10 04:25:59 -03001106{
1107 static const char *iname[] = {
1108 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1109 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1110 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1111 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1112 [CX23885_VMUX_SVIDEO] = "S-Video",
David T.L. Wongdac65fa2009-10-21 11:07:24 -03001113 [CX23885_VMUX_COMPONENT] = "Component",
Steven Tothe47f30b2008-01-10 04:25:59 -03001114 [CX23885_VMUX_TELEVISION] = "Television",
1115 [CX23885_VMUX_CABLE] = "Cable TV",
1116 [CX23885_VMUX_DVB] = "DVB",
1117 [CX23885_VMUX_DEBUG] = "for debug only",
1118 };
1119 unsigned int n;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001120 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001121
1122 n = i->index;
1123 if (n >= 4)
1124 return -EINVAL;
1125
1126 if (0 == INPUT(n)->type)
1127 return -EINVAL;
1128
Steven Tothe47f30b2008-01-10 04:25:59 -03001129 i->index = n;
1130 i->type = V4L2_INPUT_TYPE_CAMERA;
1131 strcpy(i->name, iname[INPUT(n)->type]);
1132 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
Julia Lawall473d8022010-08-05 17:22:44 -03001133 (CX23885_VMUX_CABLE == INPUT(n)->type)) {
Steven Tothe47f30b2008-01-10 04:25:59 -03001134 i->type = V4L2_INPUT_TYPE_TUNER;
1135 i->std = CX23885_NORMS;
Julia Lawall473d8022010-08-05 17:22:44 -03001136 }
Steven Tothe47f30b2008-01-10 04:25:59 -03001137 return 0;
1138}
Steven Tothe47f30b2008-01-10 04:25:59 -03001139
1140static int vidioc_enum_input(struct file *file, void *priv,
1141 struct v4l2_input *i)
1142{
1143 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001144 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001145 return cx23885_enum_input(dev, i);
1146}
1147
1148static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1149{
1150 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1151
1152 *i = dev->input;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001153 dprintk(1, "%s() returns %d\n", __func__, *i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001154 return 0;
1155}
1156
1157static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1158{
1159 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1160
Harvey Harrison22b4e642008-04-08 23:20:00 -03001161 dprintk(1, "%s(%d)\n", __func__, i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001162
1163 if (i >= 4) {
Harvey Harrison22b4e642008-04-08 23:20:00 -03001164 dprintk(1, "%s() -EINVAL\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001165 return -EINVAL;
1166 }
1167
1168 mutex_lock(&dev->lock);
1169 cx23885_video_mux(dev, i);
1170 mutex_unlock(&dev->lock);
1171 return 0;
1172}
1173
Andy Wallse9e5cf42010-07-18 18:18:06 -03001174static int vidioc_log_status(struct file *file, void *priv)
1175{
1176 struct cx23885_fh *fh = priv;
1177 struct cx23885_dev *dev = fh->dev;
1178
1179 printk(KERN_INFO
1180 "%s/0: ============ START LOG STATUS ============\n",
1181 dev->name);
1182 call_all(dev, core, log_status);
1183 printk(KERN_INFO
1184 "%s/0: ============= END LOG STATUS =============\n",
1185 dev->name);
1186 return 0;
1187}
1188
Steven Tothe47f30b2008-01-10 04:25:59 -03001189static int vidioc_queryctrl(struct file *file, void *priv,
1190 struct v4l2_queryctrl *qctrl)
1191{
1192 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1193 if (unlikely(qctrl->id == 0))
1194 return -EINVAL;
1195 return cx23885_ctrl_query(qctrl);
1196}
1197
1198static int vidioc_g_ctrl(struct file *file, void *priv,
1199 struct v4l2_control *ctl)
1200{
1201 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1202
1203 return cx23885_get_control(dev, ctl);
1204}
1205
1206static int vidioc_s_ctrl(struct file *file, void *priv,
1207 struct v4l2_control *ctl)
1208{
1209 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1210
1211 return cx23885_set_control(dev, ctl);
1212}
1213
1214static int vidioc_g_tuner(struct file *file, void *priv,
1215 struct v4l2_tuner *t)
1216{
1217 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1218
1219 if (unlikely(UNSET == dev->tuner_type))
1220 return -EINVAL;
1221 if (0 != t->index)
1222 return -EINVAL;
1223
1224 strcpy(t->name, "Television");
1225 t->type = V4L2_TUNER_ANALOG_TV;
1226 t->capability = V4L2_TUNER_CAP_NORM;
1227 t->rangehigh = 0xffffffffUL;
1228 t->signal = 0xffff ; /* LOCKED */
1229 return 0;
1230}
1231
1232static int vidioc_s_tuner(struct file *file, void *priv,
1233 struct v4l2_tuner *t)
1234{
1235 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1236
1237 if (UNSET == dev->tuner_type)
1238 return -EINVAL;
1239 if (0 != t->index)
1240 return -EINVAL;
1241 return 0;
1242}
1243
1244static int vidioc_g_frequency(struct file *file, void *priv,
1245 struct v4l2_frequency *f)
1246{
1247 struct cx23885_fh *fh = priv;
1248 struct cx23885_dev *dev = fh->dev;
1249
1250 if (unlikely(UNSET == dev->tuner_type))
1251 return -EINVAL;
1252
1253 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1254 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1255 f->frequency = dev->freq;
1256
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001257 call_all(dev, tuner, g_frequency, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001258
1259 return 0;
1260}
1261
Hans Verkuild45b9b82008-09-04 03:33:43 -03001262static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
Steven Tothe47f30b2008-01-10 04:25:59 -03001263{
1264 if (unlikely(UNSET == dev->tuner_type))
1265 return -EINVAL;
1266 if (unlikely(f->tuner != 0))
1267 return -EINVAL;
1268
1269 mutex_lock(&dev->lock);
1270 dev->freq = f->frequency;
1271
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001272 call_all(dev, tuner, s_frequency, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001273
1274 /* When changing channels it is required to reset TVAUDIO */
1275 msleep(10);
1276
1277 mutex_unlock(&dev->lock);
1278
1279 return 0;
1280}
Steven Tothe47f30b2008-01-10 04:25:59 -03001281
1282static int vidioc_s_frequency(struct file *file, void *priv,
1283 struct v4l2_frequency *f)
1284{
1285 struct cx23885_fh *fh = priv;
1286 struct cx23885_dev *dev = fh->dev;
1287
1288 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1289 return -EINVAL;
1290 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1291 return -EINVAL;
1292
1293 return
1294 cx23885_set_freq(dev, f);
1295}
1296
Steven Tothe47f30b2008-01-10 04:25:59 -03001297/* ----------------------------------------------------------- */
Steven Tothe47f30b2008-01-10 04:25:59 -03001298
1299static void cx23885_vid_timeout(unsigned long data)
1300{
1301 struct cx23885_dev *dev = (struct cx23885_dev *)data;
1302 struct cx23885_dmaqueue *q = &dev->vidq;
1303 struct cx23885_buffer *buf;
1304 unsigned long flags;
1305
1306 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1307
1308 cx_clear(VID_A_DMA_CTL, 0x11);
1309
1310 spin_lock_irqsave(&dev->slock, flags);
1311 while (!list_empty(&q->active)) {
1312 buf = list_entry(q->active.next,
1313 struct cx23885_buffer, vb.queue);
1314 list_del(&buf->vb.queue);
1315 buf->vb.state = VIDEOBUF_ERROR;
1316 wake_up(&buf->vb.done);
1317 printk(KERN_ERR "%s/0: [%p/%d] timeout - dma=0x%08lx\n",
1318 dev->name, buf, buf->vb.i,
1319 (unsigned long)buf->risc.dma);
1320 }
1321 cx23885_restart_video_queue(dev, q);
1322 spin_unlock_irqrestore(&dev->slock, flags);
1323}
1324
1325int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1326{
1327 u32 mask, count;
1328 int handled = 0;
1329
1330 mask = cx_read(VID_A_INT_MSK);
1331 if (0 == (status & mask))
1332 return handled;
1333 cx_write(VID_A_INT_STAT, status);
1334
Harvey Harrison22b4e642008-04-08 23:20:00 -03001335 dprintk(2, "%s() status = 0x%08x\n", __func__, status);
Steven Tothe47f30b2008-01-10 04:25:59 -03001336 /* risc op code error */
1337 if (status & (1 << 16)) {
1338 printk(KERN_WARNING "%s/0: video risc op code error\n",
1339 dev->name);
1340 cx_clear(VID_A_DMA_CTL, 0x11);
1341 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1342 }
1343
1344 /* risc1 y */
1345 if (status & 0x01) {
1346 spin_lock(&dev->slock);
1347 count = cx_read(VID_A_GPCNT);
1348 cx23885_video_wakeup(dev, &dev->vidq, count);
1349 spin_unlock(&dev->slock);
1350 handled++;
1351 }
1352 /* risc2 y */
1353 if (status & 0x10) {
1354 dprintk(2, "stopper video\n");
1355 spin_lock(&dev->slock);
1356 cx23885_restart_video_queue(dev, &dev->vidq);
1357 spin_unlock(&dev->slock);
1358 handled++;
1359 }
1360
1361 return handled;
1362}
1363
Steven Tothe47f30b2008-01-10 04:25:59 -03001364/* ----------------------------------------------------------- */
1365/* exported stuff */
1366
Hans Verkuilbec43662008-12-30 06:58:20 -03001367static const struct v4l2_file_operations video_fops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001368 .owner = THIS_MODULE,
1369 .open = video_open,
1370 .release = video_release,
1371 .read = video_read,
1372 .poll = video_poll,
1373 .mmap = video_mmap,
1374 .ioctl = video_ioctl2,
Steven Tothe47f30b2008-01-10 04:25:59 -03001375};
1376
Hans Verkuila3998102008-07-21 02:57:38 -03001377static const struct v4l2_ioctl_ops video_ioctl_ops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001378 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001379 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1380 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1381 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1382 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1383 .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
1384 .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
1385 .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
Steven Tothe47f30b2008-01-10 04:25:59 -03001386 .vidioc_reqbufs = vidioc_reqbufs,
1387 .vidioc_querybuf = vidioc_querybuf,
1388 .vidioc_qbuf = vidioc_qbuf,
1389 .vidioc_dqbuf = vidioc_dqbuf,
1390 .vidioc_s_std = vidioc_s_std,
1391 .vidioc_enum_input = vidioc_enum_input,
1392 .vidioc_g_input = vidioc_g_input,
1393 .vidioc_s_input = vidioc_s_input,
Andy Wallse9e5cf42010-07-18 18:18:06 -03001394 .vidioc_log_status = vidioc_log_status,
Steven Tothe47f30b2008-01-10 04:25:59 -03001395 .vidioc_queryctrl = vidioc_queryctrl,
1396 .vidioc_g_ctrl = vidioc_g_ctrl,
1397 .vidioc_s_ctrl = vidioc_s_ctrl,
1398 .vidioc_streamon = vidioc_streamon,
1399 .vidioc_streamoff = vidioc_streamoff,
Steven Tothe47f30b2008-01-10 04:25:59 -03001400 .vidioc_g_tuner = vidioc_g_tuner,
1401 .vidioc_s_tuner = vidioc_s_tuner,
1402 .vidioc_g_frequency = vidioc_g_frequency,
1403 .vidioc_s_frequency = vidioc_s_frequency,
Andy Walls74618242009-09-26 22:50:44 -03001404 .vidioc_g_chip_ident = cx23885_g_chip_ident,
Steven Tothe47f30b2008-01-10 04:25:59 -03001405#ifdef CONFIG_VIDEO_ADV_DEBUG
Andy Walls74618242009-09-26 22:50:44 -03001406 .vidioc_g_register = cx23885_g_register,
1407 .vidioc_s_register = cx23885_s_register,
Steven Tothe47f30b2008-01-10 04:25:59 -03001408#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001409};
1410
1411static struct video_device cx23885_vbi_template;
1412static struct video_device cx23885_video_template = {
1413 .name = "cx23885-video",
Hans Verkuila3998102008-07-21 02:57:38 -03001414 .fops = &video_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03001415 .ioctl_ops = &video_ioctl_ops,
Steven Tothe47f30b2008-01-10 04:25:59 -03001416 .tvnorms = CX23885_NORMS,
1417 .current_norm = V4L2_STD_NTSC_M,
1418};
1419
Hans Verkuilbec43662008-12-30 06:58:20 -03001420static const struct v4l2_file_operations radio_fops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001421 .owner = THIS_MODULE,
1422 .open = video_open,
1423 .release = video_release,
1424 .ioctl = video_ioctl2,
Steven Tothe47f30b2008-01-10 04:25:59 -03001425};
1426
1427
1428void cx23885_video_unregister(struct cx23885_dev *dev)
1429{
Harvey Harrison22b4e642008-04-08 23:20:00 -03001430 dprintk(1, "%s()\n", __func__);
Andy Wallsdbe83a32010-07-19 01:19:43 -03001431 cx23885_irq_remove(dev, 0x01);
Steven Tothe47f30b2008-01-10 04:25:59 -03001432
1433 if (dev->video_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001434 if (video_is_registered(dev->video_dev))
Steven Tothe47f30b2008-01-10 04:25:59 -03001435 video_unregister_device(dev->video_dev);
1436 else
1437 video_device_release(dev->video_dev);
1438 dev->video_dev = NULL;
1439
1440 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1441 }
1442}
1443
1444int cx23885_video_register(struct cx23885_dev *dev)
1445{
1446 int err;
1447
Harvey Harrison22b4e642008-04-08 23:20:00 -03001448 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001449 spin_lock_init(&dev->slock);
1450
1451 /* Initialize VBI template */
1452 memcpy(&cx23885_vbi_template, &cx23885_video_template,
1453 sizeof(cx23885_vbi_template));
1454 strcpy(cx23885_vbi_template.name, "cx23885-vbi");
Steven Tothe47f30b2008-01-10 04:25:59 -03001455
1456 dev->tvnorm = cx23885_video_template.current_norm;
1457
1458 /* init video dma queues */
1459 INIT_LIST_HEAD(&dev->vidq.active);
1460 INIT_LIST_HEAD(&dev->vidq.queued);
1461 dev->vidq.timeout.function = cx23885_vid_timeout;
1462 dev->vidq.timeout.data = (unsigned long)dev;
1463 init_timer(&dev->vidq.timeout);
1464 cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1465 VID_A_DMA_CTL, 0x11, 0x00);
1466
Steven Totha19602f22008-01-10 11:43:18 -03001467 /* Don't enable VBI yet */
Andy Wallsdbe83a32010-07-19 01:19:43 -03001468
1469 cx23885_irq_add_enable(dev, 0x01);
Steven Tothe47f30b2008-01-10 04:25:59 -03001470
Igor M. Liplianin557f48d2011-01-25 17:05:00 -03001471 if ((TUNER_ABSENT != dev->tuner_type) &&
1472 ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001473 struct v4l2_subdev *sd = NULL;
1474
1475 if (dev->tuner_addr)
Hans Verkuile6574f22009-04-01 03:57:53 -03001476 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
Igor M. Liplianin557f48d2011-01-25 17:05:00 -03001477 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03001478 "tuner", dev->tuner_addr, NULL);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001479 else
Hans Verkuil53dacb12009-08-10 02:49:08 -03001480 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
Igor M. Liplianin557f48d2011-01-25 17:05:00 -03001481 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
Laurent Pinchart1532a072010-09-24 07:58:29 -03001482 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001483 if (sd) {
1484 struct tuner_setup tun_setup;
1485
David T.L. Wongfd705e72009-10-21 11:08:30 -03001486 memset(&tun_setup, 0, sizeof(tun_setup));
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001487 tun_setup.mode_mask = T_ANALOG_TV;
1488 tun_setup.type = dev->tuner_type;
1489 tun_setup.addr = v4l2_i2c_subdev_addr(sd);
David T.L. Wong6f0d8c02009-10-21 13:15:30 -03001490 tun_setup.tuner_callback = cx23885_tuner_callback;
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001491
1492 v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
Kusanagi Kouichi0b32d652010-01-22 04:55:28 -03001493
1494 if (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) {
1495 struct xc2028_ctrl ctrl = {
1496 .fname = XC2028_DEFAULT_FIRMWARE,
1497 .max_len = 64
1498 };
1499 struct v4l2_priv_tun_config cfg = {
1500 .tuner = dev->tuner_type,
1501 .priv = &ctrl
1502 };
1503 v4l2_subdev_call(sd, tuner, s_config, &cfg);
1504 }
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001505 }
1506 }
1507
Steven Tothe47f30b2008-01-10 04:25:59 -03001508
1509 /* register v4l devices */
1510 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1511 &cx23885_video_template, "video");
1512 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1513 video_nr[dev->nr]);
1514 if (err < 0) {
1515 printk(KERN_INFO "%s: can't register video device\n",
1516 dev->name);
1517 goto fail_unreg;
1518 }
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001519 printk(KERN_INFO "%s/0: registered device %s [v4l2]\n",
1520 dev->name, video_device_node_name(dev->video_dev));
Steven Tothe47f30b2008-01-10 04:25:59 -03001521 /* initial device configuration */
1522 mutex_lock(&dev->lock);
1523 cx23885_set_tvnorm(dev, dev->tvnorm);
1524 init_controls(dev);
1525 cx23885_video_mux(dev, 0);
1526 mutex_unlock(&dev->lock);
1527
Steven Tothe47f30b2008-01-10 04:25:59 -03001528 return 0;
1529
1530fail_unreg:
1531 cx23885_video_unregister(dev);
1532 return err;
1533}
1534