blob: a182e829c8d0f7b9817e9d833640d2032be46e28 [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
Mijhail Moreyra97ce5672011-10-10 11:09:53 -030040#include <media/cx25840.h>
41
Steven Tothe47f30b2008-01-10 04:25:59 -030042MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards");
Steven Toth6d897612008-09-03 17:12:12 -030043MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>");
Steven Tothe47f30b2008-01-10 04:25:59 -030044MODULE_LICENSE("GPL");
45
46/* ------------------------------------------------------------------ */
47
48static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
49static unsigned int vbi_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
50static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET };
51
52module_param_array(video_nr, int, NULL, 0444);
53module_param_array(vbi_nr, int, NULL, 0444);
54module_param_array(radio_nr, int, NULL, 0444);
55
56MODULE_PARM_DESC(video_nr, "video device numbers");
57MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
58MODULE_PARM_DESC(radio_nr, "radio device numbers");
59
Steven Toth4513fc692008-01-12 11:36:36 -030060static unsigned int video_debug;
Steven Tothe47f30b2008-01-10 04:25:59 -030061module_param(video_debug, int, 0644);
62MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
63
Steven Toth4513fc692008-01-12 11:36:36 -030064static unsigned int irq_debug;
Steven Tothe47f30b2008-01-10 04:25:59 -030065module_param(irq_debug, int, 0644);
66MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]");
67
68static unsigned int vid_limit = 16;
69module_param(vid_limit, int, 0644);
70MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes");
71
72#define dprintk(level, fmt, arg...)\
Steven Toth4513fc692008-01-12 11:36:36 -030073 do { if (video_debug >= level)\
Steven Toth79776c82011-10-10 11:09:54 -030074 printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\
Steven Toth4513fc692008-01-12 11:36:36 -030075 } while (0)
Steven Tothe47f30b2008-01-10 04:25:59 -030076
77/* ------------------------------------------------------------------- */
78/* static data */
79
80#define FORMAT_FLAGS_PACKED 0x01
Steven Tothaf76e9f2011-10-10 11:09:54 -030081#if 0
Steven Tothe47f30b2008-01-10 04:25:59 -030082static struct cx23885_fmt formats[] = {
83 {
84 .name = "8 bpp, gray",
85 .fourcc = V4L2_PIX_FMT_GREY,
86 .depth = 8,
87 .flags = FORMAT_FLAGS_PACKED,
88 }, {
89 .name = "15 bpp RGB, le",
90 .fourcc = V4L2_PIX_FMT_RGB555,
91 .depth = 16,
92 .flags = FORMAT_FLAGS_PACKED,
93 }, {
94 .name = "15 bpp RGB, be",
95 .fourcc = V4L2_PIX_FMT_RGB555X,
96 .depth = 16,
97 .flags = FORMAT_FLAGS_PACKED,
98 }, {
99 .name = "16 bpp RGB, le",
100 .fourcc = V4L2_PIX_FMT_RGB565,
101 .depth = 16,
102 .flags = FORMAT_FLAGS_PACKED,
103 }, {
104 .name = "16 bpp RGB, be",
105 .fourcc = V4L2_PIX_FMT_RGB565X,
106 .depth = 16,
107 .flags = FORMAT_FLAGS_PACKED,
108 }, {
109 .name = "24 bpp RGB, le",
110 .fourcc = V4L2_PIX_FMT_BGR24,
111 .depth = 24,
112 .flags = FORMAT_FLAGS_PACKED,
113 }, {
114 .name = "32 bpp RGB, le",
115 .fourcc = V4L2_PIX_FMT_BGR32,
116 .depth = 32,
117 .flags = FORMAT_FLAGS_PACKED,
118 }, {
119 .name = "32 bpp RGB, be",
120 .fourcc = V4L2_PIX_FMT_RGB32,
121 .depth = 32,
122 .flags = FORMAT_FLAGS_PACKED,
123 }, {
124 .name = "4:2:2, packed, YUYV",
125 .fourcc = V4L2_PIX_FMT_YUYV,
126 .depth = 16,
127 .flags = FORMAT_FLAGS_PACKED,
128 }, {
129 .name = "4:2:2, packed, UYVY",
130 .fourcc = V4L2_PIX_FMT_UYVY,
131 .depth = 16,
132 .flags = FORMAT_FLAGS_PACKED,
133 },
134};
Steven Tothaf76e9f2011-10-10 11:09:54 -0300135#else
136static struct cx23885_fmt formats[] = {
137 {
138#if 0
139 .name = "4:2:2, packed, UYVY",
140 .fourcc = V4L2_PIX_FMT_UYVY,
141 .depth = 16,
142 .flags = FORMAT_FLAGS_PACKED,
143 }, {
144#endif
145 .name = "4:2:2, packed, YUYV",
146 .fourcc = V4L2_PIX_FMT_YUYV,
147 .depth = 16,
148 .flags = FORMAT_FLAGS_PACKED,
149 }
150};
151#endif
Steven Tothe47f30b2008-01-10 04:25:59 -0300152
153static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc)
154{
155 unsigned int i;
156
157 for (i = 0; i < ARRAY_SIZE(formats); i++)
158 if (formats[i].fourcc == fourcc)
159 return formats+i;
160
Steven Tothaf76e9f2011-10-10 11:09:54 -0300161 printk(KERN_ERR "%s(%c%c%c%c) NOT FOUND\n", __func__,
162 (fourcc & 0xff),
163 ((fourcc >> 8) & 0xff),
164 ((fourcc >> 16) & 0xff),
165 ((fourcc >> 24) & 0xff)
166 );
Steven Tothe47f30b2008-01-10 04:25:59 -0300167 return NULL;
168}
169
170/* ------------------------------------------------------------------- */
171
172static const struct v4l2_queryctrl no_ctl = {
173 .name = "42",
174 .flags = V4L2_CTRL_FLAG_DISABLED,
175};
176
177static struct cx23885_ctrl cx23885_ctls[] = {
178 /* --- video --- */
179 {
180 .v = {
181 .id = V4L2_CID_BRIGHTNESS,
182 .name = "Brightness",
183 .minimum = 0x00,
184 .maximum = 0xff,
185 .step = 1,
186 .default_value = 0x7f,
187 .type = V4L2_CTRL_TYPE_INTEGER,
188 },
189 .off = 128,
190 .reg = LUMA_CTRL,
191 .mask = 0x00ff,
192 .shift = 0,
193 }, {
194 .v = {
195 .id = V4L2_CID_CONTRAST,
196 .name = "Contrast",
197 .minimum = 0,
Mijhail Moreyrab12ab3f2011-10-10 11:09:53 -0300198 .maximum = 0x7f,
Steven Tothe47f30b2008-01-10 04:25:59 -0300199 .step = 1,
200 .default_value = 0x3f,
201 .type = V4L2_CTRL_TYPE_INTEGER,
202 },
203 .off = 0,
204 .reg = LUMA_CTRL,
205 .mask = 0xff00,
206 .shift = 8,
207 }, {
208 .v = {
209 .id = V4L2_CID_HUE,
210 .name = "Hue",
Mijhail Moreyrab12ab3f2011-10-10 11:09:53 -0300211 .minimum = -127,
212 .maximum = 128,
Steven Tothe47f30b2008-01-10 04:25:59 -0300213 .step = 1,
Mijhail Moreyrab12ab3f2011-10-10 11:09:53 -0300214 .default_value = 0x0,
Steven Tothe47f30b2008-01-10 04:25:59 -0300215 .type = V4L2_CTRL_TYPE_INTEGER,
216 },
217 .off = 128,
218 .reg = CHROMA_CTRL,
219 .mask = 0xff0000,
220 .shift = 16,
221 }, {
222 /* strictly, this only describes only U saturation.
223 * V saturation is handled specially through code.
224 */
225 .v = {
226 .id = V4L2_CID_SATURATION,
227 .name = "Saturation",
228 .minimum = 0,
Mijhail Moreyrab12ab3f2011-10-10 11:09:53 -0300229 .maximum = 0x7f,
Steven Tothe47f30b2008-01-10 04:25:59 -0300230 .step = 1,
Mijhail Moreyrab12ab3f2011-10-10 11:09:53 -0300231 .default_value = 0x3f,
Steven Tothe47f30b2008-01-10 04:25:59 -0300232 .type = V4L2_CTRL_TYPE_INTEGER,
233 },
234 .off = 0,
235 .reg = CHROMA_CTRL,
236 .mask = 0x00ff,
237 .shift = 0,
238 }, {
239 /* --- audio --- */
240 .v = {
241 .id = V4L2_CID_AUDIO_MUTE,
242 .name = "Mute",
243 .minimum = 0,
244 .maximum = 1,
245 .default_value = 1,
246 .type = V4L2_CTRL_TYPE_BOOLEAN,
247 },
248 .reg = PATH1_CTL1,
249 .mask = (0x1f << 24),
250 .shift = 24,
251 }, {
252 .v = {
253 .id = V4L2_CID_AUDIO_VOLUME,
254 .name = "Volume",
255 .minimum = 0,
256 .maximum = 0x3f,
257 .step = 1,
258 .default_value = 0x3f,
259 .type = V4L2_CTRL_TYPE_INTEGER,
260 },
261 .reg = PATH1_VOL_CTL,
262 .mask = 0xff,
263 .shift = 0,
264 }
265};
266static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls);
267
Hans Verkuil2ba58892009-02-13 10:57:48 -0300268/* Must be sorted from low to high control ID! */
Hans Verkuild45b9b82008-09-04 03:33:43 -0300269static const u32 cx23885_user_ctrls[] = {
Steven Tothe47f30b2008-01-10 04:25:59 -0300270 V4L2_CID_USER_CLASS,
271 V4L2_CID_BRIGHTNESS,
272 V4L2_CID_CONTRAST,
273 V4L2_CID_SATURATION,
274 V4L2_CID_HUE,
275 V4L2_CID_AUDIO_VOLUME,
276 V4L2_CID_AUDIO_MUTE,
277 0
278};
Steven Tothe47f30b2008-01-10 04:25:59 -0300279
280static const u32 *ctrl_classes[] = {
281 cx23885_user_ctrls,
282 NULL
283};
284
Steven Toth79776c82011-10-10 11:09:54 -0300285void cx23885_video_wakeup(struct cx23885_dev *dev,
286 struct cx23885_dmaqueue *q, u32 count)
Steven Tothe47f30b2008-01-10 04:25:59 -0300287{
288 struct cx23885_buffer *buf;
289 int bc;
290
291 for (bc = 0;; bc++) {
292 if (list_empty(&q->active))
293 break;
294 buf = list_entry(q->active.next,
295 struct cx23885_buffer, vb.queue);
Steven Totha19602f22008-01-10 11:43:18 -0300296
Steven Tothe47f30b2008-01-10 04:25:59 -0300297 /* count comes from the hw and is is 16bit wide --
298 * this trick handles wrap-arounds correctly for
299 * up to 32767 buffers in flight... */
300 if ((s16) (count - buf->count) < 0)
301 break;
Steven Totha19602f22008-01-10 11:43:18 -0300302
Steven Tothe47f30b2008-01-10 04:25:59 -0300303 do_gettimeofday(&buf->vb.ts);
304 dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i,
305 count, buf->count);
306 buf->vb.state = VIDEOBUF_DONE;
307 list_del(&buf->vb.queue);
308 wake_up(&buf->vb.done);
309 }
Steven Toth9c8ced52008-10-16 20:18:44 -0300310 if (list_empty(&q->active))
Steven Tothe47f30b2008-01-10 04:25:59 -0300311 del_timer(&q->timeout);
Steven Toth9c8ced52008-10-16 20:18:44 -0300312 else
Steven Tothe47f30b2008-01-10 04:25:59 -0300313 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
Steven Tothe47f30b2008-01-10 04:25:59 -0300314 if (bc != 1)
Steven Toth21a78092008-01-10 04:38:59 -0300315 printk(KERN_ERR "%s: %d buffers handled (should be 1)\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300316 __func__, bc);
Steven Tothe47f30b2008-01-10 04:25:59 -0300317}
318
Hans Verkuild45b9b82008-09-04 03:33:43 -0300319static int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm)
Steven Tothe47f30b2008-01-10 04:25:59 -0300320{
321 dprintk(1, "%s(norm = 0x%08x) name: [%s]\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300322 __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -0300323 (unsigned int)norm,
324 v4l2_norm_to_name(norm));
325
326 dev->tvnorm = norm;
327
Hans Verkuilf41737e2009-04-01 03:52:39 -0300328 call_all(dev, core, s_std, norm);
Steven Tothe47f30b2008-01-10 04:25:59 -0300329
330 return 0;
331}
332
Hans Verkuild45b9b82008-09-04 03:33:43 -0300333static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev,
Steven Tothe47f30b2008-01-10 04:25:59 -0300334 struct pci_dev *pci,
335 struct video_device *template,
336 char *type)
337{
338 struct video_device *vfd;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300339 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300340
341 vfd = video_device_alloc();
342 if (NULL == vfd)
343 return NULL;
344 *vfd = *template;
Hans Verkuilc0714f62009-03-13 08:02:43 -0300345 vfd->v4l2_dev = &dev->v4l2_dev;
Steven Tothe47f30b2008-01-10 04:25:59 -0300346 vfd->release = video_device_release;
347 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)",
348 dev->name, type, cx23885_boards[dev->board].name);
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200349 video_set_drvdata(vfd, dev);
Steven Tothe47f30b2008-01-10 04:25:59 -0300350 return vfd;
351}
352
Hans Verkuild45b9b82008-09-04 03:33:43 -0300353static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300354{
355 int i;
356
357 if (qctrl->id < V4L2_CID_BASE ||
358 qctrl->id >= V4L2_CID_LASTP1)
359 return -EINVAL;
360 for (i = 0; i < CX23885_CTLS; i++)
361 if (cx23885_ctls[i].v.id == qctrl->id)
362 break;
363 if (i == CX23885_CTLS) {
364 *qctrl = no_ctl;
365 return 0;
366 }
367 *qctrl = cx23885_ctls[i].v;
368 return 0;
369}
Steven Tothe47f30b2008-01-10 04:25:59 -0300370
371/* ------------------------------------------------------------------- */
372/* resource management */
373
374static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh,
375 unsigned int bit)
376{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300377 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300378 if (fh->resources & bit)
379 /* have it already allocated */
380 return 1;
381
382 /* is it free? */
383 mutex_lock(&dev->lock);
384 if (dev->resources & bit) {
385 /* no, someone else uses it */
386 mutex_unlock(&dev->lock);
387 return 0;
388 }
389 /* it's free, grab it */
390 fh->resources |= bit;
391 dev->resources |= bit;
392 dprintk(1, "res: get %d\n", bit);
393 mutex_unlock(&dev->lock);
394 return 1;
395}
396
397static int res_check(struct cx23885_fh *fh, unsigned int bit)
398{
Steven Toth9c8ced52008-10-16 20:18:44 -0300399 return fh->resources & bit;
Steven Tothe47f30b2008-01-10 04:25:59 -0300400}
401
402static int res_locked(struct cx23885_dev *dev, unsigned int bit)
403{
Steven Toth9c8ced52008-10-16 20:18:44 -0300404 return dev->resources & bit;
Steven Tothe47f30b2008-01-10 04:25:59 -0300405}
406
407static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh,
408 unsigned int bits)
409{
410 BUG_ON((fh->resources & bits) != bits);
Harvey Harrison22b4e642008-04-08 23:20:00 -0300411 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300412
413 mutex_lock(&dev->lock);
414 fh->resources &= ~bits;
415 dev->resources &= ~bits;
416 dprintk(1, "res: put %d\n", bits);
417 mutex_unlock(&dev->lock);
418}
419
Hans Verkuild45b9b82008-09-04 03:33:43 -0300420static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
Steven Tothe47f30b2008-01-10 04:25:59 -0300421{
Steven Tothe47f30b2008-01-10 04:25:59 -0300422 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 -0300423 __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -0300424 input, INPUT(input)->vmux,
425 INPUT(input)->gpio0, INPUT(input)->gpio1,
426 INPUT(input)->gpio2, INPUT(input)->gpio3);
427 dev->input = input;
428
David T.L. Wong6f0d8c02009-10-21 13:15:30 -0300429 if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
430 dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2) {
431 /* Select Analog TV */
432 if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
433 cx23885_gpio_clear(dev, GPIO_0);
434 }
435
Steven Tothe47f30b2008-01-10 04:25:59 -0300436 /* Tell the internal A/V decoder */
Hans Verkuil5325b422009-04-02 11:26:22 -0300437 v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
438 INPUT(input)->vmux, 0, 0);
Steven Tothe47f30b2008-01-10 04:25:59 -0300439
440 return 0;
441}
Steven Tothe47f30b2008-01-10 04:25:59 -0300442
443/* ------------------------------------------------------------------ */
Hans Verkuild45b9b82008-09-04 03:33:43 -0300444static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
Steven Tothe47f30b2008-01-10 04:25:59 -0300445 unsigned int height, enum v4l2_field field)
446{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300447 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300448 return 0;
449}
450
451static int cx23885_start_video_dma(struct cx23885_dev *dev,
452 struct cx23885_dmaqueue *q,
453 struct cx23885_buffer *buf)
454{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300455 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300456
457 /* setup fifo + format */
458 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
459 buf->bpl, buf->risc.dma);
460 cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
461
462 /* reset counter */
463 cx_write(VID_A_GPCNT_CTL, 3);
464 q->count = 1;
465
466 /* enable irq */
Andy Wallsdbe83a32010-07-19 01:19:43 -0300467 cx23885_irq_add_enable(dev, 0x01);
Steven Tothe47f30b2008-01-10 04:25:59 -0300468 cx_set(VID_A_INT_MSK, 0x000011);
469
470 /* start dma */
471 cx_set(DEV_CNTRL2, (1<<5));
472 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
473
474 return 0;
475}
476
Steven Tothe47f30b2008-01-10 04:25:59 -0300477
478static int cx23885_restart_video_queue(struct cx23885_dev *dev,
479 struct cx23885_dmaqueue *q)
480{
481 struct cx23885_buffer *buf, *prev;
482 struct list_head *item;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300483 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300484
485 if (!list_empty(&q->active)) {
486 buf = list_entry(q->active.next, struct cx23885_buffer,
487 vb.queue);
488 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
489 buf, buf->vb.i);
490 cx23885_start_video_dma(dev, q, buf);
491 list_for_each(item, &q->active) {
492 buf = list_entry(item, struct cx23885_buffer,
493 vb.queue);
494 buf->count = q->count++;
495 }
496 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
497 return 0;
498 }
499
500 prev = NULL;
501 for (;;) {
502 if (list_empty(&q->queued))
503 return 0;
504 buf = list_entry(q->queued.next, struct cx23885_buffer,
505 vb.queue);
506 if (NULL == prev) {
507 list_move_tail(&buf->vb.queue, &q->active);
508 cx23885_start_video_dma(dev, q, buf);
509 buf->vb.state = VIDEOBUF_ACTIVE;
510 buf->count = q->count++;
511 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
512 dprintk(2, "[%p/%d] restart_queue - first active\n",
513 buf, buf->vb.i);
514
515 } else if (prev->vb.width == buf->vb.width &&
516 prev->vb.height == buf->vb.height &&
517 prev->fmt == buf->fmt) {
518 list_move_tail(&buf->vb.queue, &q->active);
519 buf->vb.state = VIDEOBUF_ACTIVE;
520 buf->count = q->count++;
521 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
522 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
523 dprintk(2, "[%p/%d] restart_queue - move to active\n",
524 buf, buf->vb.i);
525 } else {
526 return 0;
527 }
528 prev = buf;
529 }
530}
531
532static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
533 unsigned int *size)
534{
535 struct cx23885_fh *fh = q->priv_data;
536
537 *size = fh->fmt->depth*fh->width*fh->height >> 3;
538 if (0 == *count)
539 *count = 32;
Andreas Bombedab7e312010-03-21 16:02:45 -0300540 if (*size * *count > vid_limit * 1024 * 1024)
541 *count = (vid_limit * 1024 * 1024) / *size;
Steven Tothe47f30b2008-01-10 04:25:59 -0300542 return 0;
543}
544
545static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
546 enum v4l2_field field)
547{
548 struct cx23885_fh *fh = q->priv_data;
549 struct cx23885_dev *dev = fh->dev;
550 struct cx23885_buffer *buf =
551 container_of(vb, struct cx23885_buffer, vb);
552 int rc, init_buffer = 0;
553 u32 line0_offset, line1_offset;
554 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
555
556 BUG_ON(NULL == fh->fmt);
557 if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
558 fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
559 return -EINVAL;
560 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
561 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
562 return -EINVAL;
563
564 if (buf->fmt != fh->fmt ||
565 buf->vb.width != fh->width ||
566 buf->vb.height != fh->height ||
567 buf->vb.field != field) {
568 buf->fmt = fh->fmt;
569 buf->vb.width = fh->width;
570 buf->vb.height = fh->height;
571 buf->vb.field = field;
572 init_buffer = 1;
573 }
574
575 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
576 init_buffer = 1;
577 rc = videobuf_iolock(q, &buf->vb, NULL);
578 if (0 != rc)
579 goto fail;
580 }
581
582 if (init_buffer) {
583 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
584 switch (buf->vb.field) {
585 case V4L2_FIELD_TOP:
586 cx23885_risc_buffer(dev->pci, &buf->risc,
587 dma->sglist, 0, UNSET,
588 buf->bpl, 0, buf->vb.height);
589 break;
590 case V4L2_FIELD_BOTTOM:
591 cx23885_risc_buffer(dev->pci, &buf->risc,
592 dma->sglist, UNSET, 0,
593 buf->bpl, 0, buf->vb.height);
594 break;
595 case V4L2_FIELD_INTERLACED:
596 if (dev->tvnorm & V4L2_STD_NTSC) {
597 /* cx25840 transmits NTSC bottom field first */
598 dprintk(1, "%s() Creating NTSC risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300599 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300600 line0_offset = buf->bpl;
601 line1_offset = 0;
602 } else {
603 /* All other formats are top field first */
604 dprintk(1, "%s() Creating PAL/SECAM risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300605 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300606 line0_offset = 0;
607 line1_offset = buf->bpl;
608 }
609 cx23885_risc_buffer(dev->pci, &buf->risc,
610 dma->sglist, line0_offset,
611 line1_offset,
612 buf->bpl, buf->bpl,
613 buf->vb.height >> 1);
614 break;
615 case V4L2_FIELD_SEQ_TB:
616 cx23885_risc_buffer(dev->pci, &buf->risc,
617 dma->sglist,
618 0, buf->bpl * (buf->vb.height >> 1),
619 buf->bpl, 0,
620 buf->vb.height >> 1);
621 break;
622 case V4L2_FIELD_SEQ_BT:
623 cx23885_risc_buffer(dev->pci, &buf->risc,
624 dma->sglist,
625 buf->bpl * (buf->vb.height >> 1), 0,
626 buf->bpl, 0,
627 buf->vb.height >> 1);
628 break;
629 default:
630 BUG();
631 }
632 }
633 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
634 buf, buf->vb.i,
635 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
636 (unsigned long)buf->risc.dma);
637
638 buf->vb.state = VIDEOBUF_PREPARED;
639 return 0;
640
641 fail:
642 cx23885_free_buffer(q, buf);
643 return rc;
644}
645
646static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
647{
648 struct cx23885_buffer *buf = container_of(vb,
649 struct cx23885_buffer, vb);
650 struct cx23885_buffer *prev;
651 struct cx23885_fh *fh = vq->priv_data;
652 struct cx23885_dev *dev = fh->dev;
653 struct cx23885_dmaqueue *q = &dev->vidq;
654
655 /* add jump to stopper */
656 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
657 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
658 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
659
660 if (!list_empty(&q->queued)) {
661 list_add_tail(&buf->vb.queue, &q->queued);
662 buf->vb.state = VIDEOBUF_QUEUED;
663 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
664 buf, buf->vb.i);
665
666 } else if (list_empty(&q->active)) {
667 list_add_tail(&buf->vb.queue, &q->active);
668 cx23885_start_video_dma(dev, q, buf);
669 buf->vb.state = VIDEOBUF_ACTIVE;
670 buf->count = q->count++;
671 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
672 dprintk(2, "[%p/%d] buffer_queue - first active\n",
673 buf, buf->vb.i);
674
675 } else {
676 prev = list_entry(q->active.prev, struct cx23885_buffer,
677 vb.queue);
678 if (prev->vb.width == buf->vb.width &&
679 prev->vb.height == buf->vb.height &&
680 prev->fmt == buf->fmt) {
681 list_add_tail(&buf->vb.queue, &q->active);
682 buf->vb.state = VIDEOBUF_ACTIVE;
683 buf->count = q->count++;
684 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
685 /* 64 bit bits 63-32 */
686 prev->risc.jmp[2] = cpu_to_le32(0);
687 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
688 buf, buf->vb.i);
689
690 } else {
691 list_add_tail(&buf->vb.queue, &q->queued);
692 buf->vb.state = VIDEOBUF_QUEUED;
693 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
694 buf, buf->vb.i);
695 }
696 }
697}
698
699static void buffer_release(struct videobuf_queue *q,
700 struct videobuf_buffer *vb)
701{
702 struct cx23885_buffer *buf = container_of(vb,
703 struct cx23885_buffer, vb);
704
705 cx23885_free_buffer(q, buf);
706}
707
708static struct videobuf_queue_ops cx23885_video_qops = {
709 .buf_setup = buffer_setup,
710 .buf_prepare = buffer_prepare,
711 .buf_queue = buffer_queue,
712 .buf_release = buffer_release,
713};
714
715static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
716{
717 switch (fh->type) {
718 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
719 return &fh->vidq;
720 case V4L2_BUF_TYPE_VBI_CAPTURE:
721 return &fh->vbiq;
722 default:
723 BUG();
724 return NULL;
725 }
726}
727
728static int get_resource(struct cx23885_fh *fh)
729{
730 switch (fh->type) {
731 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
732 return RESOURCE_VIDEO;
733 case V4L2_BUF_TYPE_VBI_CAPTURE:
734 return RESOURCE_VBI;
735 default:
736 BUG();
737 return 0;
738 }
739}
740
Hans Verkuilbec43662008-12-30 06:58:20 -0300741static int video_open(struct file *file)
Steven Tothe47f30b2008-01-10 04:25:59 -0300742{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200743 struct video_device *vdev = video_devdata(file);
744 struct cx23885_dev *dev = video_drvdata(file);
Steven Tothe47f30b2008-01-10 04:25:59 -0300745 struct cx23885_fh *fh;
Steven Tothe47f30b2008-01-10 04:25:59 -0300746 enum v4l2_buf_type type = 0;
747 int radio = 0;
748
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200749 switch (vdev->vfl_type) {
750 case VFL_TYPE_GRABBER:
751 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
752 break;
753 case VFL_TYPE_VBI:
754 type = V4L2_BUF_TYPE_VBI_CAPTURE;
755 break;
756 case VFL_TYPE_RADIO:
757 radio = 1;
758 break;
Hans Verkuild56dc612008-07-30 08:43:36 -0300759 }
Steven Tothe47f30b2008-01-10 04:25:59 -0300760
Laurent Pinchart50462eb2009-12-10 11:47:13 -0200761 dprintk(1, "open dev=%s radio=%d type=%s\n",
762 video_device_node_name(vdev), radio, v4l2_type_names[type]);
Steven Tothe47f30b2008-01-10 04:25:59 -0300763
764 /* allocate + initialize per filehandle data */
765 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200766 if (NULL == fh)
Steven Tothe47f30b2008-01-10 04:25:59 -0300767 return -ENOMEM;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200768
Steven Tothe47f30b2008-01-10 04:25:59 -0300769 file->private_data = fh;
770 fh->dev = dev;
771 fh->radio = radio;
772 fh->type = type;
773 fh->width = 320;
774 fh->height = 240;
Steven Tothaf76e9f2011-10-10 11:09:54 -0300775 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
Steven Tothe47f30b2008-01-10 04:25:59 -0300776
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300777 videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
778 &dev->pci->dev, &dev->slock,
Steven Tothe47f30b2008-01-10 04:25:59 -0300779 V4L2_BUF_TYPE_VIDEO_CAPTURE,
780 V4L2_FIELD_INTERLACED,
781 sizeof(struct cx23885_buffer),
Hans Verkuil08bff032010-09-20 17:39:46 -0300782 fh, NULL);
Steven Tothe47f30b2008-01-10 04:25:59 -0300783
Steven Toth79776c82011-10-10 11:09:54 -0300784 videobuf_queue_sg_init(&fh->vbiq, &cx23885_vbi_qops,
785 &dev->pci->dev, &dev->slock,
786 V4L2_BUF_TYPE_VBI_CAPTURE,
787 V4L2_FIELD_SEQ_TB,
788 sizeof(struct cx23885_buffer),
789 fh, NULL);
790
791
Steven Tothe47f30b2008-01-10 04:25:59 -0300792 dprintk(1, "post videobuf_queue_init()\n");
793
Steven Tothe47f30b2008-01-10 04:25:59 -0300794 return 0;
795}
796
797static ssize_t video_read(struct file *file, char __user *data,
798 size_t count, loff_t *ppos)
799{
800 struct cx23885_fh *fh = file->private_data;
801
802 switch (fh->type) {
803 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
804 if (res_locked(fh->dev, RESOURCE_VIDEO))
805 return -EBUSY;
806 return videobuf_read_one(&fh->vidq, data, count, ppos,
807 file->f_flags & O_NONBLOCK);
808 case V4L2_BUF_TYPE_VBI_CAPTURE:
809 if (!res_get(fh->dev, fh, RESOURCE_VBI))
810 return -EBUSY;
811 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
812 file->f_flags & O_NONBLOCK);
813 default:
814 BUG();
815 return 0;
816 }
817}
818
819static unsigned int video_poll(struct file *file,
820 struct poll_table_struct *wait)
821{
822 struct cx23885_fh *fh = file->private_data;
823 struct cx23885_buffer *buf;
Figo.zhang9fd64182009-06-16 13:31:29 -0300824 unsigned int rc = POLLERR;
Steven Tothe47f30b2008-01-10 04:25:59 -0300825
826 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
827 if (!res_get(fh->dev, fh, RESOURCE_VBI))
828 return POLLERR;
829 return videobuf_poll_stream(file, &fh->vbiq, wait);
830 }
831
Figo.zhang9fd64182009-06-16 13:31:29 -0300832 mutex_lock(&fh->vidq.vb_lock);
Steven Tothe47f30b2008-01-10 04:25:59 -0300833 if (res_check(fh, RESOURCE_VIDEO)) {
834 /* streaming capture */
835 if (list_empty(&fh->vidq.stream))
Figo.zhang9fd64182009-06-16 13:31:29 -0300836 goto done;
Steven Tothe47f30b2008-01-10 04:25:59 -0300837 buf = list_entry(fh->vidq.stream.next,
838 struct cx23885_buffer, vb.stream);
839 } else {
840 /* read() capture */
841 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
842 if (NULL == buf)
Figo.zhang9fd64182009-06-16 13:31:29 -0300843 goto done;
Steven Tothe47f30b2008-01-10 04:25:59 -0300844 }
845 poll_wait(file, &buf->vb.done, wait);
846 if (buf->vb.state == VIDEOBUF_DONE ||
847 buf->vb.state == VIDEOBUF_ERROR)
Figo.zhang9fd64182009-06-16 13:31:29 -0300848 rc = POLLIN|POLLRDNORM;
849 else
850 rc = 0;
851done:
852 mutex_unlock(&fh->vidq.vb_lock);
853 return rc;
Steven Tothe47f30b2008-01-10 04:25:59 -0300854}
855
Hans Verkuilbec43662008-12-30 06:58:20 -0300856static int video_release(struct file *file)
Steven Tothe47f30b2008-01-10 04:25:59 -0300857{
858 struct cx23885_fh *fh = file->private_data;
859 struct cx23885_dev *dev = fh->dev;
860
861 /* turn off overlay */
862 if (res_check(fh, RESOURCE_OVERLAY)) {
863 /* FIXME */
864 res_free(dev, fh, RESOURCE_OVERLAY);
865 }
866
867 /* stop video capture */
868 if (res_check(fh, RESOURCE_VIDEO)) {
869 videobuf_queue_cancel(&fh->vidq);
870 res_free(dev, fh, RESOURCE_VIDEO);
871 }
872 if (fh->vidq.read_buf) {
873 buffer_release(&fh->vidq, fh->vidq.read_buf);
874 kfree(fh->vidq.read_buf);
875 }
876
877 /* stop vbi capture */
878 if (res_check(fh, RESOURCE_VBI)) {
879 if (fh->vbiq.streaming)
880 videobuf_streamoff(&fh->vbiq);
881 if (fh->vbiq.reading)
882 videobuf_read_stop(&fh->vbiq);
883 res_free(dev, fh, RESOURCE_VBI);
884 }
885
886 videobuf_mmap_free(&fh->vidq);
887 file->private_data = NULL;
888 kfree(fh);
889
Steven Totha19602f22008-01-10 11:43:18 -0300890 /* We are not putting the tuner to sleep here on exit, because
891 * we want to use the mpeg encoder in another session to capture
892 * tuner video. Closing this will result in no video to the encoder.
893 */
Steven Tothe47f30b2008-01-10 04:25:59 -0300894
895 return 0;
896}
897
898static int video_mmap(struct file *file, struct vm_area_struct *vma)
899{
900 struct cx23885_fh *fh = file->private_data;
901
902 return videobuf_mmap_mapper(get_queue(fh), vma);
903}
904
905/* ------------------------------------------------------------------ */
906/* VIDEO CTRL IOCTLS */
907
Steven Toth9c8ced52008-10-16 20:18:44 -0300908static int cx23885_get_control(struct cx23885_dev *dev,
909 struct v4l2_control *ctl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300910{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300911 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -0300912 call_all(dev, core, g_ctrl, ctl);
Steven Tothe47f30b2008-01-10 04:25:59 -0300913 return 0;
914}
Steven Tothe47f30b2008-01-10 04:25:59 -0300915
Steven Toth9c8ced52008-10-16 20:18:44 -0300916static int cx23885_set_control(struct cx23885_dev *dev,
917 struct v4l2_control *ctl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300918{
Mijhail Moreyra97ce5672011-10-10 11:09:53 -0300919 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)\n", __func__);
920 call_all(dev, core, s_ctrl, ctl);
921
Steven Tothe47f30b2008-01-10 04:25:59 -0300922 return 0;
923}
Steven Tothe47f30b2008-01-10 04:25:59 -0300924
925static void init_controls(struct cx23885_dev *dev)
926{
927 struct v4l2_control ctrl;
928 int i;
929
930 for (i = 0; i < CX23885_CTLS; i++) {
931 ctrl.id = cx23885_ctls[i].v.id;
932 ctrl.value = cx23885_ctls[i].v.default_value;
933
934 cx23885_set_control(dev, &ctrl);
935 }
936}
937
938/* ------------------------------------------------------------------ */
939/* VIDEO IOCTLS */
940
Hans Verkuil78b526a2008-05-28 12:16:41 -0300941static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300942 struct v4l2_format *f)
943{
944 struct cx23885_fh *fh = priv;
945
946 f->fmt.pix.width = fh->width;
947 f->fmt.pix.height = fh->height;
948 f->fmt.pix.field = fh->vidq.field;
949 f->fmt.pix.pixelformat = fh->fmt->fourcc;
950 f->fmt.pix.bytesperline =
951 (f->fmt.pix.width * fh->fmt->depth) >> 3;
952 f->fmt.pix.sizeimage =
953 f->fmt.pix.height * f->fmt.pix.bytesperline;
954
955 return 0;
956}
957
Hans Verkuil78b526a2008-05-28 12:16:41 -0300958static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -0300959 struct v4l2_format *f)
960{
961 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
962 struct cx23885_fmt *fmt;
963 enum v4l2_field field;
964 unsigned int maxw, maxh;
965
966 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
967 if (NULL == fmt)
968 return -EINVAL;
969
970 field = f->fmt.pix.field;
971 maxw = norm_maxw(dev->tvnorm);
972 maxh = norm_maxh(dev->tvnorm);
973
974 if (V4L2_FIELD_ANY == field) {
975 field = (f->fmt.pix.height > maxh/2)
976 ? V4L2_FIELD_INTERLACED
977 : V4L2_FIELD_BOTTOM;
978 }
979
980 switch (field) {
981 case V4L2_FIELD_TOP:
982 case V4L2_FIELD_BOTTOM:
983 maxh = maxh / 2;
984 break;
985 case V4L2_FIELD_INTERLACED:
986 break;
987 default:
988 return -EINVAL;
989 }
990
991 f->fmt.pix.field = field;
Trent Piepho2449afc2009-05-30 21:45:46 -0300992 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
993 &f->fmt.pix.height, 32, maxh, 0, 0);
Steven Tothe47f30b2008-01-10 04:25:59 -0300994 f->fmt.pix.bytesperline =
995 (f->fmt.pix.width * fmt->depth) >> 3;
996 f->fmt.pix.sizeimage =
997 f->fmt.pix.height * f->fmt.pix.bytesperline;
998
999 return 0;
1000}
1001
Hans Verkuil78b526a2008-05-28 12:16:41 -03001002static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001003 struct v4l2_format *f)
1004{
1005 struct cx23885_fh *fh = priv;
1006 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Hans Verkuilcc991132010-05-09 10:09:28 -03001007 struct v4l2_mbus_framefmt mbus_fmt;
Steven Tothe47f30b2008-01-10 04:25:59 -03001008 int err;
1009
Harvey Harrison22b4e642008-04-08 23:20:00 -03001010 dprintk(2, "%s()\n", __func__);
Hans Verkuil78b526a2008-05-28 12:16:41 -03001011 err = vidioc_try_fmt_vid_cap(file, priv, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001012
1013 if (0 != err)
1014 return err;
1015 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1016 fh->width = f->fmt.pix.width;
1017 fh->height = f->fmt.pix.height;
1018 fh->vidq.field = f->fmt.pix.field;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001019 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -03001020 fh->width, fh->height, fh->vidq.field);
Hans Verkuilcc991132010-05-09 10:09:28 -03001021 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
1022 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1023 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
Steven Tothe47f30b2008-01-10 04:25:59 -03001024 return 0;
1025}
1026
1027static int vidioc_querycap(struct file *file, void *priv,
1028 struct v4l2_capability *cap)
1029{
1030 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1031
1032 strcpy(cap->driver, "cx23885");
1033 strlcpy(cap->card, cx23885_boards[dev->board].name,
1034 sizeof(cap->card));
1035 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
Steven Tothe47f30b2008-01-10 04:25:59 -03001036 cap->capabilities =
1037 V4L2_CAP_VIDEO_CAPTURE |
1038 V4L2_CAP_READWRITE |
1039 V4L2_CAP_STREAMING |
1040 V4L2_CAP_VBI_CAPTURE;
1041 if (UNSET != dev->tuner_type)
1042 cap->capabilities |= V4L2_CAP_TUNER;
1043 return 0;
1044}
1045
Hans Verkuil78b526a2008-05-28 12:16:41 -03001046static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001047 struct v4l2_fmtdesc *f)
1048{
1049 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1050 return -EINVAL;
1051
1052 strlcpy(f->description, formats[f->index].name,
1053 sizeof(f->description));
1054 f->pixelformat = formats[f->index].fourcc;
1055
1056 return 0;
1057}
1058
Steven Tothe47f30b2008-01-10 04:25:59 -03001059static int vidioc_reqbufs(struct file *file, void *priv,
1060 struct v4l2_requestbuffers *p)
1061{
1062 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001063 return videobuf_reqbufs(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001064}
1065
1066static int vidioc_querybuf(struct file *file, void *priv,
1067 struct v4l2_buffer *p)
1068{
1069 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001070 return videobuf_querybuf(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001071}
1072
1073static int vidioc_qbuf(struct file *file, void *priv,
1074 struct v4l2_buffer *p)
1075{
1076 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001077 return videobuf_qbuf(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001078}
1079
1080static int vidioc_dqbuf(struct file *file, void *priv,
1081 struct v4l2_buffer *p)
1082{
1083 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001084 return videobuf_dqbuf(get_queue(fh), p,
1085 file->f_flags & O_NONBLOCK);
Steven Tothe47f30b2008-01-10 04:25:59 -03001086}
1087
1088static int vidioc_streamon(struct file *file, void *priv,
1089 enum v4l2_buf_type i)
1090{
1091 struct cx23885_fh *fh = priv;
1092 struct cx23885_dev *dev = fh->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001093 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001094
Steven Toth5ab27e62011-10-10 11:09:54 -03001095 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1096 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
Steven Tothe47f30b2008-01-10 04:25:59 -03001097 return -EINVAL;
1098 if (unlikely(i != fh->type))
1099 return -EINVAL;
1100
1101 if (unlikely(!res_get(dev, fh, get_resource(fh))))
1102 return -EBUSY;
1103 return videobuf_streamon(get_queue(fh));
1104}
1105
1106static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1107{
1108 struct cx23885_fh *fh = priv;
1109 struct cx23885_dev *dev = fh->dev;
1110 int err, res;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001111 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001112
Steven Toth5ab27e62011-10-10 11:09:54 -03001113 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1114 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
Steven Tothe47f30b2008-01-10 04:25:59 -03001115 return -EINVAL;
1116 if (i != fh->type)
1117 return -EINVAL;
1118
1119 res = get_resource(fh);
1120 err = videobuf_streamoff(get_queue(fh));
1121 if (err < 0)
1122 return err;
1123 res_free(dev, fh, res);
1124 return 0;
1125}
1126
1127static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1128{
1129 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001130 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001131
1132 mutex_lock(&dev->lock);
1133 cx23885_set_tvnorm(dev, *tvnorms);
1134 mutex_unlock(&dev->lock);
1135
1136 return 0;
1137}
1138
Hans Verkuild45b9b82008-09-04 03:33:43 -03001139static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
Steven Tothe47f30b2008-01-10 04:25:59 -03001140{
1141 static const char *iname[] = {
1142 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1143 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1144 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1145 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1146 [CX23885_VMUX_SVIDEO] = "S-Video",
David T.L. Wongdac65fa2009-10-21 11:07:24 -03001147 [CX23885_VMUX_COMPONENT] = "Component",
Steven Tothe47f30b2008-01-10 04:25:59 -03001148 [CX23885_VMUX_TELEVISION] = "Television",
1149 [CX23885_VMUX_CABLE] = "Cable TV",
1150 [CX23885_VMUX_DVB] = "DVB",
1151 [CX23885_VMUX_DEBUG] = "for debug only",
1152 };
1153 unsigned int n;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001154 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001155
1156 n = i->index;
1157 if (n >= 4)
1158 return -EINVAL;
1159
1160 if (0 == INPUT(n)->type)
1161 return -EINVAL;
1162
Steven Tothe47f30b2008-01-10 04:25:59 -03001163 i->index = n;
1164 i->type = V4L2_INPUT_TYPE_CAMERA;
1165 strcpy(i->name, iname[INPUT(n)->type]);
1166 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
Julia Lawall473d8022010-08-05 17:22:44 -03001167 (CX23885_VMUX_CABLE == INPUT(n)->type)) {
Steven Tothe47f30b2008-01-10 04:25:59 -03001168 i->type = V4L2_INPUT_TYPE_TUNER;
1169 i->std = CX23885_NORMS;
Julia Lawall473d8022010-08-05 17:22:44 -03001170 }
Steven Tothe47f30b2008-01-10 04:25:59 -03001171 return 0;
1172}
Steven Tothe47f30b2008-01-10 04:25:59 -03001173
1174static int vidioc_enum_input(struct file *file, void *priv,
1175 struct v4l2_input *i)
1176{
1177 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001178 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001179 return cx23885_enum_input(dev, i);
1180}
1181
1182static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1183{
1184 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1185
1186 *i = dev->input;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001187 dprintk(1, "%s() returns %d\n", __func__, *i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001188 return 0;
1189}
1190
1191static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1192{
1193 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1194
Harvey Harrison22b4e642008-04-08 23:20:00 -03001195 dprintk(1, "%s(%d)\n", __func__, i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001196
1197 if (i >= 4) {
Harvey Harrison22b4e642008-04-08 23:20:00 -03001198 dprintk(1, "%s() -EINVAL\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001199 return -EINVAL;
1200 }
1201
1202 mutex_lock(&dev->lock);
1203 cx23885_video_mux(dev, i);
1204 mutex_unlock(&dev->lock);
1205 return 0;
1206}
1207
Andy Wallse9e5cf42010-07-18 18:18:06 -03001208static int vidioc_log_status(struct file *file, void *priv)
1209{
1210 struct cx23885_fh *fh = priv;
1211 struct cx23885_dev *dev = fh->dev;
1212
1213 printk(KERN_INFO
1214 "%s/0: ============ START LOG STATUS ============\n",
1215 dev->name);
1216 call_all(dev, core, log_status);
1217 printk(KERN_INFO
1218 "%s/0: ============= END LOG STATUS =============\n",
1219 dev->name);
1220 return 0;
1221}
1222
Steven Tothe47f30b2008-01-10 04:25:59 -03001223static int vidioc_queryctrl(struct file *file, void *priv,
1224 struct v4l2_queryctrl *qctrl)
1225{
1226 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1227 if (unlikely(qctrl->id == 0))
1228 return -EINVAL;
1229 return cx23885_ctrl_query(qctrl);
1230}
1231
1232static int vidioc_g_ctrl(struct file *file, void *priv,
1233 struct v4l2_control *ctl)
1234{
1235 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1236
1237 return cx23885_get_control(dev, ctl);
1238}
1239
1240static int vidioc_s_ctrl(struct file *file, void *priv,
1241 struct v4l2_control *ctl)
1242{
1243 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1244
1245 return cx23885_set_control(dev, ctl);
1246}
1247
1248static int vidioc_g_tuner(struct file *file, void *priv,
1249 struct v4l2_tuner *t)
1250{
1251 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1252
1253 if (unlikely(UNSET == dev->tuner_type))
1254 return -EINVAL;
1255 if (0 != t->index)
1256 return -EINVAL;
1257
1258 strcpy(t->name, "Television");
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001259
Steven Toth80f1e082011-10-10 11:09:53 -03001260 call_all(dev, tuner, g_tuner, t);
Steven Tothe47f30b2008-01-10 04:25:59 -03001261 return 0;
1262}
1263
1264static int vidioc_s_tuner(struct file *file, void *priv,
1265 struct v4l2_tuner *t)
1266{
1267 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1268
1269 if (UNSET == dev->tuner_type)
1270 return -EINVAL;
1271 if (0 != t->index)
1272 return -EINVAL;
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001273 /* Update the A/V core */
Steven Toth80f1e082011-10-10 11:09:53 -03001274 call_all(dev, tuner, s_tuner, t);
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001275
Steven Tothe47f30b2008-01-10 04:25:59 -03001276 return 0;
1277}
1278
1279static int vidioc_g_frequency(struct file *file, void *priv,
1280 struct v4l2_frequency *f)
1281{
1282 struct cx23885_fh *fh = priv;
1283 struct cx23885_dev *dev = fh->dev;
1284
1285 if (unlikely(UNSET == dev->tuner_type))
1286 return -EINVAL;
1287
1288 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1289 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1290 f->frequency = dev->freq;
1291
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001292 call_all(dev, tuner, g_frequency, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001293
1294 return 0;
1295}
1296
Hans Verkuild45b9b82008-09-04 03:33:43 -03001297static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
Steven Tothe47f30b2008-01-10 04:25:59 -03001298{
1299 if (unlikely(UNSET == dev->tuner_type))
1300 return -EINVAL;
1301 if (unlikely(f->tuner != 0))
1302 return -EINVAL;
1303
1304 mutex_lock(&dev->lock);
1305 dev->freq = f->frequency;
1306
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001307 call_all(dev, tuner, s_frequency, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001308
1309 /* When changing channels it is required to reset TVAUDIO */
1310 msleep(10);
1311
1312 mutex_unlock(&dev->lock);
1313
1314 return 0;
1315}
Steven Tothe47f30b2008-01-10 04:25:59 -03001316
1317static int vidioc_s_frequency(struct file *file, void *priv,
1318 struct v4l2_frequency *f)
1319{
1320 struct cx23885_fh *fh = priv;
1321 struct cx23885_dev *dev = fh->dev;
1322
1323 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1324 return -EINVAL;
1325 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1326 return -EINVAL;
1327
1328 return
1329 cx23885_set_freq(dev, f);
1330}
1331
Steven Tothe47f30b2008-01-10 04:25:59 -03001332/* ----------------------------------------------------------- */
Steven Tothe47f30b2008-01-10 04:25:59 -03001333
1334static void cx23885_vid_timeout(unsigned long data)
1335{
1336 struct cx23885_dev *dev = (struct cx23885_dev *)data;
1337 struct cx23885_dmaqueue *q = &dev->vidq;
1338 struct cx23885_buffer *buf;
1339 unsigned long flags;
1340
1341 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1342
1343 cx_clear(VID_A_DMA_CTL, 0x11);
1344
1345 spin_lock_irqsave(&dev->slock, flags);
1346 while (!list_empty(&q->active)) {
1347 buf = list_entry(q->active.next,
1348 struct cx23885_buffer, vb.queue);
1349 list_del(&buf->vb.queue);
1350 buf->vb.state = VIDEOBUF_ERROR;
1351 wake_up(&buf->vb.done);
Steven Toth79776c82011-10-10 11:09:54 -03001352 printk(KERN_ERR "%s: [%p/%d] timeout - dma=0x%08lx\n",
Steven Tothe47f30b2008-01-10 04:25:59 -03001353 dev->name, buf, buf->vb.i,
1354 (unsigned long)buf->risc.dma);
1355 }
1356 cx23885_restart_video_queue(dev, q);
1357 spin_unlock_irqrestore(&dev->slock, flags);
1358}
1359
1360int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1361{
1362 u32 mask, count;
1363 int handled = 0;
1364
1365 mask = cx_read(VID_A_INT_MSK);
1366 if (0 == (status & mask))
1367 return handled;
Steven Toth79776c82011-10-10 11:09:54 -03001368
Steven Tothe47f30b2008-01-10 04:25:59 -03001369 cx_write(VID_A_INT_STAT, status);
1370
Steven Tothe47f30b2008-01-10 04:25:59 -03001371 /* risc op code error */
Steven Toth79776c82011-10-10 11:09:54 -03001372 if ((status & VID_BC_MSK_OPC_ERR) ||
1373 (status & VID_BC_MSK_SYNC) ||
1374 (status & VID_BC_MSK_OF)) {
1375
1376 if (status & VID_BC_MSK_OPC_ERR)
1377 dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1378 VID_BC_MSK_OPC_ERR);
1379
1380 if (status & VID_BC_MSK_SYNC)
1381 dprintk(7, " (VID_BC_MSK_SYNC 0x%08x)\n",
1382 VID_BC_MSK_SYNC);
1383
1384 if (status & VID_BC_MSK_OF)
1385 dprintk(7, " (VID_BC_MSK_OF 0x%08x)\n",
1386 VID_BC_MSK_OF);
1387
1388 printk(KERN_WARNING "%s: video risc op code error\n",
Steven Tothe47f30b2008-01-10 04:25:59 -03001389 dev->name);
Steven Toth79776c82011-10-10 11:09:54 -03001390
Steven Tothe47f30b2008-01-10 04:25:59 -03001391 cx_clear(VID_A_DMA_CTL, 0x11);
1392 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
Steven Toth79776c82011-10-10 11:09:54 -03001393
Steven Tothe47f30b2008-01-10 04:25:59 -03001394 }
1395
Steven Toth79776c82011-10-10 11:09:54 -03001396 /* Video */
1397 if (status & VID_BC_MSK_RISCI1) {
Steven Tothe47f30b2008-01-10 04:25:59 -03001398 spin_lock(&dev->slock);
1399 count = cx_read(VID_A_GPCNT);
1400 cx23885_video_wakeup(dev, &dev->vidq, count);
1401 spin_unlock(&dev->slock);
1402 handled++;
1403 }
Steven Toth79776c82011-10-10 11:09:54 -03001404 if (status & VID_BC_MSK_RISCI2) {
Steven Tothe47f30b2008-01-10 04:25:59 -03001405 dprintk(2, "stopper video\n");
1406 spin_lock(&dev->slock);
1407 cx23885_restart_video_queue(dev, &dev->vidq);
1408 spin_unlock(&dev->slock);
1409 handled++;
1410 }
1411
Steven Toth79776c82011-10-10 11:09:54 -03001412 /* Allow the VBI framework to process it's payload */
1413 handled += cx23885_vbi_irq(dev, status);
1414
Steven Tothe47f30b2008-01-10 04:25:59 -03001415 return handled;
1416}
1417
Steven Tothe47f30b2008-01-10 04:25:59 -03001418/* ----------------------------------------------------------- */
1419/* exported stuff */
1420
Hans Verkuilbec43662008-12-30 06:58:20 -03001421static const struct v4l2_file_operations video_fops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001422 .owner = THIS_MODULE,
1423 .open = video_open,
1424 .release = video_release,
1425 .read = video_read,
1426 .poll = video_poll,
1427 .mmap = video_mmap,
1428 .ioctl = video_ioctl2,
Steven Tothe47f30b2008-01-10 04:25:59 -03001429};
1430
Hans Verkuila3998102008-07-21 02:57:38 -03001431static const struct v4l2_ioctl_ops video_ioctl_ops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001432 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001433 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1434 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1435 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1436 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1437 .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
1438 .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
1439 .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
Steven Tothe47f30b2008-01-10 04:25:59 -03001440 .vidioc_reqbufs = vidioc_reqbufs,
1441 .vidioc_querybuf = vidioc_querybuf,
1442 .vidioc_qbuf = vidioc_qbuf,
1443 .vidioc_dqbuf = vidioc_dqbuf,
1444 .vidioc_s_std = vidioc_s_std,
1445 .vidioc_enum_input = vidioc_enum_input,
1446 .vidioc_g_input = vidioc_g_input,
1447 .vidioc_s_input = vidioc_s_input,
Andy Wallse9e5cf42010-07-18 18:18:06 -03001448 .vidioc_log_status = vidioc_log_status,
Steven Tothe47f30b2008-01-10 04:25:59 -03001449 .vidioc_queryctrl = vidioc_queryctrl,
1450 .vidioc_g_ctrl = vidioc_g_ctrl,
1451 .vidioc_s_ctrl = vidioc_s_ctrl,
1452 .vidioc_streamon = vidioc_streamon,
1453 .vidioc_streamoff = vidioc_streamoff,
Steven Tothe47f30b2008-01-10 04:25:59 -03001454 .vidioc_g_tuner = vidioc_g_tuner,
1455 .vidioc_s_tuner = vidioc_s_tuner,
1456 .vidioc_g_frequency = vidioc_g_frequency,
1457 .vidioc_s_frequency = vidioc_s_frequency,
Andy Walls74618242009-09-26 22:50:44 -03001458 .vidioc_g_chip_ident = cx23885_g_chip_ident,
Steven Tothe47f30b2008-01-10 04:25:59 -03001459#ifdef CONFIG_VIDEO_ADV_DEBUG
Andy Walls74618242009-09-26 22:50:44 -03001460 .vidioc_g_register = cx23885_g_register,
1461 .vidioc_s_register = cx23885_s_register,
Steven Tothe47f30b2008-01-10 04:25:59 -03001462#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001463};
1464
1465static struct video_device cx23885_vbi_template;
1466static struct video_device cx23885_video_template = {
1467 .name = "cx23885-video",
Hans Verkuila3998102008-07-21 02:57:38 -03001468 .fops = &video_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03001469 .ioctl_ops = &video_ioctl_ops,
Steven Tothe47f30b2008-01-10 04:25:59 -03001470 .tvnorms = CX23885_NORMS,
1471 .current_norm = V4L2_STD_NTSC_M,
1472};
1473
Hans Verkuilbec43662008-12-30 06:58:20 -03001474static const struct v4l2_file_operations radio_fops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001475 .owner = THIS_MODULE,
1476 .open = video_open,
1477 .release = video_release,
1478 .ioctl = video_ioctl2,
Steven Tothe47f30b2008-01-10 04:25:59 -03001479};
1480
1481
1482void cx23885_video_unregister(struct cx23885_dev *dev)
1483{
Harvey Harrison22b4e642008-04-08 23:20:00 -03001484 dprintk(1, "%s()\n", __func__);
Andy Wallsdbe83a32010-07-19 01:19:43 -03001485 cx23885_irq_remove(dev, 0x01);
Steven Tothe47f30b2008-01-10 04:25:59 -03001486
Steven Toth79776c82011-10-10 11:09:54 -03001487 if (dev->vbi_dev) {
1488 if (video_is_registered(dev->vbi_dev))
1489 video_unregister_device(dev->vbi_dev);
1490 else
1491 video_device_release(dev->vbi_dev);
1492 dev->vbi_dev = NULL;
1493 btcx_riscmem_free(dev->pci, &dev->vbiq.stopper);
1494 }
Steven Tothe47f30b2008-01-10 04:25:59 -03001495 if (dev->video_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001496 if (video_is_registered(dev->video_dev))
Steven Tothe47f30b2008-01-10 04:25:59 -03001497 video_unregister_device(dev->video_dev);
1498 else
1499 video_device_release(dev->video_dev);
1500 dev->video_dev = NULL;
1501
1502 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1503 }
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001504
1505 if (dev->audio_dev)
Steven Tothefa762f2011-10-10 11:09:53 -03001506 cx23885_audio_unregister(dev);
Steven Tothe47f30b2008-01-10 04:25:59 -03001507}
1508
1509int cx23885_video_register(struct cx23885_dev *dev)
1510{
1511 int err;
1512
Harvey Harrison22b4e642008-04-08 23:20:00 -03001513 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001514 spin_lock_init(&dev->slock);
1515
1516 /* Initialize VBI template */
1517 memcpy(&cx23885_vbi_template, &cx23885_video_template,
1518 sizeof(cx23885_vbi_template));
1519 strcpy(cx23885_vbi_template.name, "cx23885-vbi");
Steven Tothe47f30b2008-01-10 04:25:59 -03001520
1521 dev->tvnorm = cx23885_video_template.current_norm;
1522
1523 /* init video dma queues */
1524 INIT_LIST_HEAD(&dev->vidq.active);
1525 INIT_LIST_HEAD(&dev->vidq.queued);
1526 dev->vidq.timeout.function = cx23885_vid_timeout;
1527 dev->vidq.timeout.data = (unsigned long)dev;
1528 init_timer(&dev->vidq.timeout);
1529 cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1530 VID_A_DMA_CTL, 0x11, 0x00);
1531
Steven Toth79776c82011-10-10 11:09:54 -03001532 /* init vbi dma queues */
1533 INIT_LIST_HEAD(&dev->vbiq.active);
1534 INIT_LIST_HEAD(&dev->vbiq.queued);
1535 dev->vbiq.timeout.function = cx23885_vbi_timeout;
1536 dev->vbiq.timeout.data = (unsigned long)dev;
1537 init_timer(&dev->vbiq.timeout);
1538 cx23885_risc_stopper(dev->pci, &dev->vbiq.stopper,
1539 VID_A_DMA_CTL, 0x22, 0x00);
Andy Wallsdbe83a32010-07-19 01:19:43 -03001540
1541 cx23885_irq_add_enable(dev, 0x01);
Steven Tothe47f30b2008-01-10 04:25:59 -03001542
Igor M. Liplianin557f48d2011-01-25 17:05:00 -03001543 if ((TUNER_ABSENT != dev->tuner_type) &&
1544 ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001545 struct v4l2_subdev *sd = NULL;
1546
1547 if (dev->tuner_addr)
Hans Verkuile6574f22009-04-01 03:57:53 -03001548 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
Igor M. Liplianin557f48d2011-01-25 17:05:00 -03001549 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03001550 "tuner", dev->tuner_addr, NULL);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001551 else
Hans Verkuil53dacb12009-08-10 02:49:08 -03001552 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
Igor M. Liplianin557f48d2011-01-25 17:05:00 -03001553 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
Laurent Pinchart1532a072010-09-24 07:58:29 -03001554 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001555 if (sd) {
1556 struct tuner_setup tun_setup;
1557
David T.L. Wongfd705e72009-10-21 11:08:30 -03001558 memset(&tun_setup, 0, sizeof(tun_setup));
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001559 tun_setup.mode_mask = T_ANALOG_TV;
1560 tun_setup.type = dev->tuner_type;
1561 tun_setup.addr = v4l2_i2c_subdev_addr(sd);
David T.L. Wong6f0d8c02009-10-21 13:15:30 -03001562 tun_setup.tuner_callback = cx23885_tuner_callback;
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001563
1564 v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
Kusanagi Kouichi0b32d652010-01-22 04:55:28 -03001565
1566 if (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) {
1567 struct xc2028_ctrl ctrl = {
1568 .fname = XC2028_DEFAULT_FIRMWARE,
1569 .max_len = 64
1570 };
1571 struct v4l2_priv_tun_config cfg = {
1572 .tuner = dev->tuner_type,
1573 .priv = &ctrl
1574 };
1575 v4l2_subdev_call(sd, tuner, s_config, &cfg);
1576 }
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001577 }
1578 }
1579
Steven Toth79776c82011-10-10 11:09:54 -03001580 /* register Video device */
Steven Tothe47f30b2008-01-10 04:25:59 -03001581 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1582 &cx23885_video_template, "video");
1583 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1584 video_nr[dev->nr]);
1585 if (err < 0) {
1586 printk(KERN_INFO "%s: can't register video device\n",
1587 dev->name);
1588 goto fail_unreg;
1589 }
Steven Tothf88fb8e2011-10-10 11:09:54 -03001590 printk(KERN_INFO "%s: registered device %s [v4l2]\n",
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001591 dev->name, video_device_node_name(dev->video_dev));
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001592
Steven Tothf88fb8e2011-10-10 11:09:54 -03001593 /* register VBI device */
1594 dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
1595 &cx23885_vbi_template, "vbi");
1596 err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1597 vbi_nr[dev->nr]);
1598 if (err < 0) {
1599 printk(KERN_INFO "%s: can't register vbi device\n",
1600 dev->name);
1601 goto fail_unreg;
1602 }
1603 printk(KERN_INFO "%s: registered device %s\n",
1604 dev->name, video_device_node_name(dev->vbi_dev));
1605
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001606 /* Register ALSA audio device */
Steven Tothefa762f2011-10-10 11:09:53 -03001607 dev->audio_dev = cx23885_audio_register(dev);
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001608
Steven Tothe47f30b2008-01-10 04:25:59 -03001609 /* initial device configuration */
1610 mutex_lock(&dev->lock);
1611 cx23885_set_tvnorm(dev, dev->tvnorm);
1612 init_controls(dev);
1613 cx23885_video_mux(dev, 0);
1614 mutex_unlock(&dev->lock);
1615
Steven Tothe47f30b2008-01-10 04:25:59 -03001616 return 0;
1617
1618fail_unreg:
1619 cx23885_video_unregister(dev);
1620 return err;
1621}
1622