blob: adc8f78d4c58bde9621d8dd9b90667d8afbb0dc7 [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
Steven Toth33cdeb32011-10-10 11:09:55 -0300420static int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data)
421{
422 /* 8 bit registers, 8 bit values */
423 u8 buf[] = { reg, data };
424
425 struct i2c_msg msg = { .addr = 0x98 >> 1,
426 .flags = 0, .buf = buf, .len = 2 };
427
428 return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1);
429}
430
431static u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg)
432{
433 /* 8 bit registers, 8 bit values */
434 int ret;
435 u8 b0[] = { reg };
436 u8 b1[] = { 0 };
437
438 struct i2c_msg msg[] = {
439 { .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 },
440 { .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 }
441 };
442
443 ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2);
444 if (ret != 2)
445 printk(KERN_ERR "%s() error\n", __func__);
446
447 return b1[0];
448}
449
450static void cx23885_flatiron_dump(struct cx23885_dev *dev)
451{
452 int i;
453 dprintk(1, "Flatiron dump\n");
454 for (i = 0; i < 0x24; i++) {
455 dprintk(1, "FI[%02x] = %02x\n", i,
456 cx23885_flatiron_read(dev, i));
457 }
458}
459
460static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input)
461{
462 u8 val;
463 dprintk(1, "%s(input = %d)\n", __func__, input);
464
465 if (input == 1)
466 val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL;
467 else if (input == 2)
468 val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL;
469 else
470 return -EINVAL;
471
472 val |= 0x20; /* Enable clock to delta-sigma and dec filter */
473
474 cx23885_flatiron_write(dev, CH_PWR_CTRL1, val);
475
476 /* Wake up */
477 cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0);
478
479 if (video_debug)
480 cx23885_flatiron_dump(dev);
481
482 return 0;
483}
484
Hans Verkuild45b9b82008-09-04 03:33:43 -0300485static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input)
Steven Tothe47f30b2008-01-10 04:25:59 -0300486{
Steven Tothe47f30b2008-01-10 04:25:59 -0300487 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 -0300488 __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -0300489 input, INPUT(input)->vmux,
490 INPUT(input)->gpio0, INPUT(input)->gpio1,
491 INPUT(input)->gpio2, INPUT(input)->gpio3);
492 dev->input = input;
493
David T.L. Wong6f0d8c02009-10-21 13:15:30 -0300494 if (dev->board == CX23885_BOARD_MYGICA_X8506 ||
495 dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2) {
496 /* Select Analog TV */
497 if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
498 cx23885_gpio_clear(dev, GPIO_0);
499 }
500
Steven Tothe47f30b2008-01-10 04:25:59 -0300501 /* Tell the internal A/V decoder */
Hans Verkuil5325b422009-04-02 11:26:22 -0300502 v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
503 INPUT(input)->vmux, 0, 0);
Steven Tothe47f30b2008-01-10 04:25:59 -0300504
Steven Toth33cdeb32011-10-10 11:09:55 -0300505 if (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) {
506 /* Configure audio routing */
507 v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
508 INPUT(input)->amux, 0, 0);
509
510 if (INPUT(input)->amux == CX25840_AUDIO7)
511 cx23885_flatiron_mux(dev, 1);
512 else if (INPUT(input)->amux == CX25840_AUDIO6)
513 cx23885_flatiron_mux(dev, 2);
514 }
515
Steven Tothe47f30b2008-01-10 04:25:59 -0300516 return 0;
517}
Steven Tothe47f30b2008-01-10 04:25:59 -0300518
519/* ------------------------------------------------------------------ */
Hans Verkuild45b9b82008-09-04 03:33:43 -0300520static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
Steven Tothe47f30b2008-01-10 04:25:59 -0300521 unsigned int height, enum v4l2_field field)
522{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300523 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300524 return 0;
525}
526
527static int cx23885_start_video_dma(struct cx23885_dev *dev,
528 struct cx23885_dmaqueue *q,
529 struct cx23885_buffer *buf)
530{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300531 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300532
533 /* setup fifo + format */
534 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
535 buf->bpl, buf->risc.dma);
536 cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
537
538 /* reset counter */
539 cx_write(VID_A_GPCNT_CTL, 3);
540 q->count = 1;
541
542 /* enable irq */
Andy Wallsdbe83a32010-07-19 01:19:43 -0300543 cx23885_irq_add_enable(dev, 0x01);
Steven Tothe47f30b2008-01-10 04:25:59 -0300544 cx_set(VID_A_INT_MSK, 0x000011);
545
546 /* start dma */
547 cx_set(DEV_CNTRL2, (1<<5));
548 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
549
550 return 0;
551}
552
Steven Tothe47f30b2008-01-10 04:25:59 -0300553
554static int cx23885_restart_video_queue(struct cx23885_dev *dev,
555 struct cx23885_dmaqueue *q)
556{
557 struct cx23885_buffer *buf, *prev;
558 struct list_head *item;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300559 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300560
561 if (!list_empty(&q->active)) {
562 buf = list_entry(q->active.next, struct cx23885_buffer,
563 vb.queue);
564 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
565 buf, buf->vb.i);
566 cx23885_start_video_dma(dev, q, buf);
567 list_for_each(item, &q->active) {
568 buf = list_entry(item, struct cx23885_buffer,
569 vb.queue);
570 buf->count = q->count++;
571 }
572 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
573 return 0;
574 }
575
576 prev = NULL;
577 for (;;) {
578 if (list_empty(&q->queued))
579 return 0;
580 buf = list_entry(q->queued.next, struct cx23885_buffer,
581 vb.queue);
582 if (NULL == prev) {
583 list_move_tail(&buf->vb.queue, &q->active);
584 cx23885_start_video_dma(dev, q, buf);
585 buf->vb.state = VIDEOBUF_ACTIVE;
586 buf->count = q->count++;
587 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
588 dprintk(2, "[%p/%d] restart_queue - first active\n",
589 buf, buf->vb.i);
590
591 } else if (prev->vb.width == buf->vb.width &&
592 prev->vb.height == buf->vb.height &&
593 prev->fmt == buf->fmt) {
594 list_move_tail(&buf->vb.queue, &q->active);
595 buf->vb.state = VIDEOBUF_ACTIVE;
596 buf->count = q->count++;
597 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
598 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
599 dprintk(2, "[%p/%d] restart_queue - move to active\n",
600 buf, buf->vb.i);
601 } else {
602 return 0;
603 }
604 prev = buf;
605 }
606}
607
608static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
609 unsigned int *size)
610{
611 struct cx23885_fh *fh = q->priv_data;
612
613 *size = fh->fmt->depth*fh->width*fh->height >> 3;
614 if (0 == *count)
615 *count = 32;
Andreas Bombedab7e312010-03-21 16:02:45 -0300616 if (*size * *count > vid_limit * 1024 * 1024)
617 *count = (vid_limit * 1024 * 1024) / *size;
Steven Tothe47f30b2008-01-10 04:25:59 -0300618 return 0;
619}
620
621static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
622 enum v4l2_field field)
623{
624 struct cx23885_fh *fh = q->priv_data;
625 struct cx23885_dev *dev = fh->dev;
626 struct cx23885_buffer *buf =
627 container_of(vb, struct cx23885_buffer, vb);
628 int rc, init_buffer = 0;
629 u32 line0_offset, line1_offset;
630 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
631
632 BUG_ON(NULL == fh->fmt);
633 if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
634 fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
635 return -EINVAL;
636 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
637 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
638 return -EINVAL;
639
640 if (buf->fmt != fh->fmt ||
641 buf->vb.width != fh->width ||
642 buf->vb.height != fh->height ||
643 buf->vb.field != field) {
644 buf->fmt = fh->fmt;
645 buf->vb.width = fh->width;
646 buf->vb.height = fh->height;
647 buf->vb.field = field;
648 init_buffer = 1;
649 }
650
651 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
652 init_buffer = 1;
653 rc = videobuf_iolock(q, &buf->vb, NULL);
654 if (0 != rc)
655 goto fail;
656 }
657
658 if (init_buffer) {
659 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
660 switch (buf->vb.field) {
661 case V4L2_FIELD_TOP:
662 cx23885_risc_buffer(dev->pci, &buf->risc,
663 dma->sglist, 0, UNSET,
664 buf->bpl, 0, buf->vb.height);
665 break;
666 case V4L2_FIELD_BOTTOM:
667 cx23885_risc_buffer(dev->pci, &buf->risc,
668 dma->sglist, UNSET, 0,
669 buf->bpl, 0, buf->vb.height);
670 break;
671 case V4L2_FIELD_INTERLACED:
672 if (dev->tvnorm & V4L2_STD_NTSC) {
673 /* cx25840 transmits NTSC bottom field first */
674 dprintk(1, "%s() Creating NTSC risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300675 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300676 line0_offset = buf->bpl;
677 line1_offset = 0;
678 } else {
679 /* All other formats are top field first */
680 dprintk(1, "%s() Creating PAL/SECAM risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300681 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300682 line0_offset = 0;
683 line1_offset = buf->bpl;
684 }
685 cx23885_risc_buffer(dev->pci, &buf->risc,
686 dma->sglist, line0_offset,
687 line1_offset,
688 buf->bpl, buf->bpl,
689 buf->vb.height >> 1);
690 break;
691 case V4L2_FIELD_SEQ_TB:
692 cx23885_risc_buffer(dev->pci, &buf->risc,
693 dma->sglist,
694 0, buf->bpl * (buf->vb.height >> 1),
695 buf->bpl, 0,
696 buf->vb.height >> 1);
697 break;
698 case V4L2_FIELD_SEQ_BT:
699 cx23885_risc_buffer(dev->pci, &buf->risc,
700 dma->sglist,
701 buf->bpl * (buf->vb.height >> 1), 0,
702 buf->bpl, 0,
703 buf->vb.height >> 1);
704 break;
705 default:
706 BUG();
707 }
708 }
709 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
710 buf, buf->vb.i,
711 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
712 (unsigned long)buf->risc.dma);
713
714 buf->vb.state = VIDEOBUF_PREPARED;
715 return 0;
716
717 fail:
718 cx23885_free_buffer(q, buf);
719 return rc;
720}
721
722static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
723{
724 struct cx23885_buffer *buf = container_of(vb,
725 struct cx23885_buffer, vb);
726 struct cx23885_buffer *prev;
727 struct cx23885_fh *fh = vq->priv_data;
728 struct cx23885_dev *dev = fh->dev;
729 struct cx23885_dmaqueue *q = &dev->vidq;
730
731 /* add jump to stopper */
732 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
733 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
734 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
735
736 if (!list_empty(&q->queued)) {
737 list_add_tail(&buf->vb.queue, &q->queued);
738 buf->vb.state = VIDEOBUF_QUEUED;
739 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
740 buf, buf->vb.i);
741
742 } else if (list_empty(&q->active)) {
743 list_add_tail(&buf->vb.queue, &q->active);
744 cx23885_start_video_dma(dev, q, buf);
745 buf->vb.state = VIDEOBUF_ACTIVE;
746 buf->count = q->count++;
747 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
748 dprintk(2, "[%p/%d] buffer_queue - first active\n",
749 buf, buf->vb.i);
750
751 } else {
752 prev = list_entry(q->active.prev, struct cx23885_buffer,
753 vb.queue);
754 if (prev->vb.width == buf->vb.width &&
755 prev->vb.height == buf->vb.height &&
756 prev->fmt == buf->fmt) {
757 list_add_tail(&buf->vb.queue, &q->active);
758 buf->vb.state = VIDEOBUF_ACTIVE;
759 buf->count = q->count++;
760 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
761 /* 64 bit bits 63-32 */
762 prev->risc.jmp[2] = cpu_to_le32(0);
763 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
764 buf, buf->vb.i);
765
766 } else {
767 list_add_tail(&buf->vb.queue, &q->queued);
768 buf->vb.state = VIDEOBUF_QUEUED;
769 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
770 buf, buf->vb.i);
771 }
772 }
773}
774
775static void buffer_release(struct videobuf_queue *q,
776 struct videobuf_buffer *vb)
777{
778 struct cx23885_buffer *buf = container_of(vb,
779 struct cx23885_buffer, vb);
780
781 cx23885_free_buffer(q, buf);
782}
783
784static struct videobuf_queue_ops cx23885_video_qops = {
785 .buf_setup = buffer_setup,
786 .buf_prepare = buffer_prepare,
787 .buf_queue = buffer_queue,
788 .buf_release = buffer_release,
789};
790
791static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
792{
793 switch (fh->type) {
794 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
795 return &fh->vidq;
796 case V4L2_BUF_TYPE_VBI_CAPTURE:
797 return &fh->vbiq;
798 default:
799 BUG();
800 return NULL;
801 }
802}
803
804static int get_resource(struct cx23885_fh *fh)
805{
806 switch (fh->type) {
807 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
808 return RESOURCE_VIDEO;
809 case V4L2_BUF_TYPE_VBI_CAPTURE:
810 return RESOURCE_VBI;
811 default:
812 BUG();
813 return 0;
814 }
815}
816
Hans Verkuilbec43662008-12-30 06:58:20 -0300817static int video_open(struct file *file)
Steven Tothe47f30b2008-01-10 04:25:59 -0300818{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200819 struct video_device *vdev = video_devdata(file);
820 struct cx23885_dev *dev = video_drvdata(file);
Steven Tothe47f30b2008-01-10 04:25:59 -0300821 struct cx23885_fh *fh;
Steven Tothe47f30b2008-01-10 04:25:59 -0300822 enum v4l2_buf_type type = 0;
823 int radio = 0;
824
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200825 switch (vdev->vfl_type) {
826 case VFL_TYPE_GRABBER:
827 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
828 break;
829 case VFL_TYPE_VBI:
830 type = V4L2_BUF_TYPE_VBI_CAPTURE;
831 break;
832 case VFL_TYPE_RADIO:
833 radio = 1;
834 break;
Hans Verkuild56dc612008-07-30 08:43:36 -0300835 }
Steven Tothe47f30b2008-01-10 04:25:59 -0300836
Laurent Pinchart50462eb2009-12-10 11:47:13 -0200837 dprintk(1, "open dev=%s radio=%d type=%s\n",
838 video_device_node_name(vdev), radio, v4l2_type_names[type]);
Steven Tothe47f30b2008-01-10 04:25:59 -0300839
840 /* allocate + initialize per filehandle data */
841 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200842 if (NULL == fh)
Steven Tothe47f30b2008-01-10 04:25:59 -0300843 return -ENOMEM;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200844
Steven Tothe47f30b2008-01-10 04:25:59 -0300845 file->private_data = fh;
846 fh->dev = dev;
847 fh->radio = radio;
848 fh->type = type;
849 fh->width = 320;
850 fh->height = 240;
Steven Tothaf76e9f2011-10-10 11:09:54 -0300851 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
Steven Tothe47f30b2008-01-10 04:25:59 -0300852
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300853 videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
854 &dev->pci->dev, &dev->slock,
Steven Tothe47f30b2008-01-10 04:25:59 -0300855 V4L2_BUF_TYPE_VIDEO_CAPTURE,
856 V4L2_FIELD_INTERLACED,
857 sizeof(struct cx23885_buffer),
Hans Verkuil08bff032010-09-20 17:39:46 -0300858 fh, NULL);
Steven Tothe47f30b2008-01-10 04:25:59 -0300859
Steven Toth79776c82011-10-10 11:09:54 -0300860 videobuf_queue_sg_init(&fh->vbiq, &cx23885_vbi_qops,
861 &dev->pci->dev, &dev->slock,
862 V4L2_BUF_TYPE_VBI_CAPTURE,
863 V4L2_FIELD_SEQ_TB,
864 sizeof(struct cx23885_buffer),
865 fh, NULL);
866
867
Steven Tothe47f30b2008-01-10 04:25:59 -0300868 dprintk(1, "post videobuf_queue_init()\n");
869
Steven Tothe47f30b2008-01-10 04:25:59 -0300870 return 0;
871}
872
873static ssize_t video_read(struct file *file, char __user *data,
874 size_t count, loff_t *ppos)
875{
876 struct cx23885_fh *fh = file->private_data;
877
878 switch (fh->type) {
879 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
880 if (res_locked(fh->dev, RESOURCE_VIDEO))
881 return -EBUSY;
882 return videobuf_read_one(&fh->vidq, data, count, ppos,
883 file->f_flags & O_NONBLOCK);
884 case V4L2_BUF_TYPE_VBI_CAPTURE:
885 if (!res_get(fh->dev, fh, RESOURCE_VBI))
886 return -EBUSY;
887 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
888 file->f_flags & O_NONBLOCK);
889 default:
890 BUG();
891 return 0;
892 }
893}
894
895static unsigned int video_poll(struct file *file,
896 struct poll_table_struct *wait)
897{
898 struct cx23885_fh *fh = file->private_data;
899 struct cx23885_buffer *buf;
Figo.zhang9fd64182009-06-16 13:31:29 -0300900 unsigned int rc = POLLERR;
Steven Tothe47f30b2008-01-10 04:25:59 -0300901
902 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
903 if (!res_get(fh->dev, fh, RESOURCE_VBI))
904 return POLLERR;
905 return videobuf_poll_stream(file, &fh->vbiq, wait);
906 }
907
Figo.zhang9fd64182009-06-16 13:31:29 -0300908 mutex_lock(&fh->vidq.vb_lock);
Steven Tothe47f30b2008-01-10 04:25:59 -0300909 if (res_check(fh, RESOURCE_VIDEO)) {
910 /* streaming capture */
911 if (list_empty(&fh->vidq.stream))
Figo.zhang9fd64182009-06-16 13:31:29 -0300912 goto done;
Steven Tothe47f30b2008-01-10 04:25:59 -0300913 buf = list_entry(fh->vidq.stream.next,
914 struct cx23885_buffer, vb.stream);
915 } else {
916 /* read() capture */
917 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
918 if (NULL == buf)
Figo.zhang9fd64182009-06-16 13:31:29 -0300919 goto done;
Steven Tothe47f30b2008-01-10 04:25:59 -0300920 }
921 poll_wait(file, &buf->vb.done, wait);
922 if (buf->vb.state == VIDEOBUF_DONE ||
923 buf->vb.state == VIDEOBUF_ERROR)
Figo.zhang9fd64182009-06-16 13:31:29 -0300924 rc = POLLIN|POLLRDNORM;
925 else
926 rc = 0;
927done:
928 mutex_unlock(&fh->vidq.vb_lock);
929 return rc;
Steven Tothe47f30b2008-01-10 04:25:59 -0300930}
931
Hans Verkuilbec43662008-12-30 06:58:20 -0300932static int video_release(struct file *file)
Steven Tothe47f30b2008-01-10 04:25:59 -0300933{
934 struct cx23885_fh *fh = file->private_data;
935 struct cx23885_dev *dev = fh->dev;
936
937 /* turn off overlay */
938 if (res_check(fh, RESOURCE_OVERLAY)) {
939 /* FIXME */
940 res_free(dev, fh, RESOURCE_OVERLAY);
941 }
942
943 /* stop video capture */
944 if (res_check(fh, RESOURCE_VIDEO)) {
945 videobuf_queue_cancel(&fh->vidq);
946 res_free(dev, fh, RESOURCE_VIDEO);
947 }
948 if (fh->vidq.read_buf) {
949 buffer_release(&fh->vidq, fh->vidq.read_buf);
950 kfree(fh->vidq.read_buf);
951 }
952
953 /* stop vbi capture */
954 if (res_check(fh, RESOURCE_VBI)) {
955 if (fh->vbiq.streaming)
956 videobuf_streamoff(&fh->vbiq);
957 if (fh->vbiq.reading)
958 videobuf_read_stop(&fh->vbiq);
959 res_free(dev, fh, RESOURCE_VBI);
960 }
961
962 videobuf_mmap_free(&fh->vidq);
963 file->private_data = NULL;
964 kfree(fh);
965
Steven Totha19602f22008-01-10 11:43:18 -0300966 /* We are not putting the tuner to sleep here on exit, because
967 * we want to use the mpeg encoder in another session to capture
968 * tuner video. Closing this will result in no video to the encoder.
969 */
Steven Tothe47f30b2008-01-10 04:25:59 -0300970
971 return 0;
972}
973
974static int video_mmap(struct file *file, struct vm_area_struct *vma)
975{
976 struct cx23885_fh *fh = file->private_data;
977
978 return videobuf_mmap_mapper(get_queue(fh), vma);
979}
980
981/* ------------------------------------------------------------------ */
982/* VIDEO CTRL IOCTLS */
983
Steven Toth9c8ced52008-10-16 20:18:44 -0300984static int cx23885_get_control(struct cx23885_dev *dev,
985 struct v4l2_control *ctl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300986{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300987 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -0300988 call_all(dev, core, g_ctrl, ctl);
Steven Tothe47f30b2008-01-10 04:25:59 -0300989 return 0;
990}
Steven Tothe47f30b2008-01-10 04:25:59 -0300991
Steven Toth9c8ced52008-10-16 20:18:44 -0300992static int cx23885_set_control(struct cx23885_dev *dev,
993 struct v4l2_control *ctl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300994{
Mijhail Moreyra97ce5672011-10-10 11:09:53 -0300995 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)\n", __func__);
996 call_all(dev, core, s_ctrl, ctl);
997
Steven Tothe47f30b2008-01-10 04:25:59 -0300998 return 0;
999}
Steven Tothe47f30b2008-01-10 04:25:59 -03001000
1001static void init_controls(struct cx23885_dev *dev)
1002{
1003 struct v4l2_control ctrl;
1004 int i;
1005
1006 for (i = 0; i < CX23885_CTLS; i++) {
1007 ctrl.id = cx23885_ctls[i].v.id;
1008 ctrl.value = cx23885_ctls[i].v.default_value;
1009
1010 cx23885_set_control(dev, &ctrl);
1011 }
1012}
1013
1014/* ------------------------------------------------------------------ */
1015/* VIDEO IOCTLS */
1016
Hans Verkuil78b526a2008-05-28 12:16:41 -03001017static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001018 struct v4l2_format *f)
1019{
1020 struct cx23885_fh *fh = priv;
1021
1022 f->fmt.pix.width = fh->width;
1023 f->fmt.pix.height = fh->height;
1024 f->fmt.pix.field = fh->vidq.field;
1025 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1026 f->fmt.pix.bytesperline =
1027 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1028 f->fmt.pix.sizeimage =
1029 f->fmt.pix.height * f->fmt.pix.bytesperline;
1030
1031 return 0;
1032}
1033
Hans Verkuil78b526a2008-05-28 12:16:41 -03001034static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001035 struct v4l2_format *f)
1036{
1037 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1038 struct cx23885_fmt *fmt;
1039 enum v4l2_field field;
1040 unsigned int maxw, maxh;
1041
1042 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1043 if (NULL == fmt)
1044 return -EINVAL;
1045
1046 field = f->fmt.pix.field;
1047 maxw = norm_maxw(dev->tvnorm);
1048 maxh = norm_maxh(dev->tvnorm);
1049
1050 if (V4L2_FIELD_ANY == field) {
1051 field = (f->fmt.pix.height > maxh/2)
1052 ? V4L2_FIELD_INTERLACED
1053 : V4L2_FIELD_BOTTOM;
1054 }
1055
1056 switch (field) {
1057 case V4L2_FIELD_TOP:
1058 case V4L2_FIELD_BOTTOM:
1059 maxh = maxh / 2;
1060 break;
1061 case V4L2_FIELD_INTERLACED:
1062 break;
1063 default:
1064 return -EINVAL;
1065 }
1066
1067 f->fmt.pix.field = field;
Trent Piepho2449afc2009-05-30 21:45:46 -03001068 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
1069 &f->fmt.pix.height, 32, maxh, 0, 0);
Steven Tothe47f30b2008-01-10 04:25:59 -03001070 f->fmt.pix.bytesperline =
1071 (f->fmt.pix.width * fmt->depth) >> 3;
1072 f->fmt.pix.sizeimage =
1073 f->fmt.pix.height * f->fmt.pix.bytesperline;
1074
1075 return 0;
1076}
1077
Hans Verkuil78b526a2008-05-28 12:16:41 -03001078static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001079 struct v4l2_format *f)
1080{
1081 struct cx23885_fh *fh = priv;
1082 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Hans Verkuilcc991132010-05-09 10:09:28 -03001083 struct v4l2_mbus_framefmt mbus_fmt;
Steven Tothe47f30b2008-01-10 04:25:59 -03001084 int err;
1085
Harvey Harrison22b4e642008-04-08 23:20:00 -03001086 dprintk(2, "%s()\n", __func__);
Hans Verkuil78b526a2008-05-28 12:16:41 -03001087 err = vidioc_try_fmt_vid_cap(file, priv, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001088
1089 if (0 != err)
1090 return err;
1091 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1092 fh->width = f->fmt.pix.width;
1093 fh->height = f->fmt.pix.height;
1094 fh->vidq.field = f->fmt.pix.field;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001095 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -03001096 fh->width, fh->height, fh->vidq.field);
Hans Verkuilcc991132010-05-09 10:09:28 -03001097 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
1098 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1099 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
Steven Tothe47f30b2008-01-10 04:25:59 -03001100 return 0;
1101}
1102
1103static int vidioc_querycap(struct file *file, void *priv,
1104 struct v4l2_capability *cap)
1105{
1106 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1107
1108 strcpy(cap->driver, "cx23885");
1109 strlcpy(cap->card, cx23885_boards[dev->board].name,
1110 sizeof(cap->card));
1111 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
Steven Tothe47f30b2008-01-10 04:25:59 -03001112 cap->capabilities =
1113 V4L2_CAP_VIDEO_CAPTURE |
1114 V4L2_CAP_READWRITE |
1115 V4L2_CAP_STREAMING |
1116 V4L2_CAP_VBI_CAPTURE;
1117 if (UNSET != dev->tuner_type)
1118 cap->capabilities |= V4L2_CAP_TUNER;
1119 return 0;
1120}
1121
Hans Verkuil78b526a2008-05-28 12:16:41 -03001122static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001123 struct v4l2_fmtdesc *f)
1124{
1125 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1126 return -EINVAL;
1127
1128 strlcpy(f->description, formats[f->index].name,
1129 sizeof(f->description));
1130 f->pixelformat = formats[f->index].fourcc;
1131
1132 return 0;
1133}
1134
Steven Tothe47f30b2008-01-10 04:25:59 -03001135static int vidioc_reqbufs(struct file *file, void *priv,
1136 struct v4l2_requestbuffers *p)
1137{
1138 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001139 return videobuf_reqbufs(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001140}
1141
1142static int vidioc_querybuf(struct file *file, void *priv,
1143 struct v4l2_buffer *p)
1144{
1145 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001146 return videobuf_querybuf(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001147}
1148
1149static int vidioc_qbuf(struct file *file, void *priv,
1150 struct v4l2_buffer *p)
1151{
1152 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001153 return videobuf_qbuf(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001154}
1155
1156static int vidioc_dqbuf(struct file *file, void *priv,
1157 struct v4l2_buffer *p)
1158{
1159 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001160 return videobuf_dqbuf(get_queue(fh), p,
1161 file->f_flags & O_NONBLOCK);
Steven Tothe47f30b2008-01-10 04:25:59 -03001162}
1163
1164static int vidioc_streamon(struct file *file, void *priv,
1165 enum v4l2_buf_type i)
1166{
1167 struct cx23885_fh *fh = priv;
1168 struct cx23885_dev *dev = fh->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001169 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001170
Steven Toth5ab27e62011-10-10 11:09:54 -03001171 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1172 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
Steven Tothe47f30b2008-01-10 04:25:59 -03001173 return -EINVAL;
1174 if (unlikely(i != fh->type))
1175 return -EINVAL;
1176
1177 if (unlikely(!res_get(dev, fh, get_resource(fh))))
1178 return -EBUSY;
Steven Toth24465b42011-10-10 11:09:54 -03001179
1180 /* Don't start VBI streaming unless vida streaming
1181 * has already started.
1182 */
1183 if ((fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) &&
1184 ((cx_read(VID_A_DMA_CTL) & 0x11) == 0))
1185 return -EINVAL;
1186
Steven Tothe47f30b2008-01-10 04:25:59 -03001187 return videobuf_streamon(get_queue(fh));
1188}
1189
1190static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1191{
1192 struct cx23885_fh *fh = priv;
1193 struct cx23885_dev *dev = fh->dev;
1194 int err, res;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001195 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001196
Steven Toth5ab27e62011-10-10 11:09:54 -03001197 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1198 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
Steven Tothe47f30b2008-01-10 04:25:59 -03001199 return -EINVAL;
1200 if (i != fh->type)
1201 return -EINVAL;
1202
1203 res = get_resource(fh);
1204 err = videobuf_streamoff(get_queue(fh));
1205 if (err < 0)
1206 return err;
1207 res_free(dev, fh, res);
1208 return 0;
1209}
1210
1211static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1212{
1213 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001214 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001215
1216 mutex_lock(&dev->lock);
1217 cx23885_set_tvnorm(dev, *tvnorms);
1218 mutex_unlock(&dev->lock);
1219
1220 return 0;
1221}
1222
Hans Verkuild45b9b82008-09-04 03:33:43 -03001223static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
Steven Tothe47f30b2008-01-10 04:25:59 -03001224{
1225 static const char *iname[] = {
1226 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1227 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1228 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1229 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1230 [CX23885_VMUX_SVIDEO] = "S-Video",
David T.L. Wongdac65fa2009-10-21 11:07:24 -03001231 [CX23885_VMUX_COMPONENT] = "Component",
Steven Tothe47f30b2008-01-10 04:25:59 -03001232 [CX23885_VMUX_TELEVISION] = "Television",
1233 [CX23885_VMUX_CABLE] = "Cable TV",
1234 [CX23885_VMUX_DVB] = "DVB",
1235 [CX23885_VMUX_DEBUG] = "for debug only",
1236 };
1237 unsigned int n;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001238 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001239
1240 n = i->index;
1241 if (n >= 4)
1242 return -EINVAL;
1243
1244 if (0 == INPUT(n)->type)
1245 return -EINVAL;
1246
Steven Tothe47f30b2008-01-10 04:25:59 -03001247 i->index = n;
1248 i->type = V4L2_INPUT_TYPE_CAMERA;
1249 strcpy(i->name, iname[INPUT(n)->type]);
1250 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
Julia Lawall473d8022010-08-05 17:22:44 -03001251 (CX23885_VMUX_CABLE == INPUT(n)->type)) {
Steven Tothe47f30b2008-01-10 04:25:59 -03001252 i->type = V4L2_INPUT_TYPE_TUNER;
1253 i->std = CX23885_NORMS;
Julia Lawall473d8022010-08-05 17:22:44 -03001254 }
Steven Tothe47f30b2008-01-10 04:25:59 -03001255 return 0;
1256}
Steven Tothe47f30b2008-01-10 04:25:59 -03001257
1258static int vidioc_enum_input(struct file *file, void *priv,
1259 struct v4l2_input *i)
1260{
1261 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001262 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001263 return cx23885_enum_input(dev, i);
1264}
1265
1266static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1267{
1268 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1269
1270 *i = dev->input;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001271 dprintk(1, "%s() returns %d\n", __func__, *i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001272 return 0;
1273}
1274
1275static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1276{
1277 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1278
Harvey Harrison22b4e642008-04-08 23:20:00 -03001279 dprintk(1, "%s(%d)\n", __func__, i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001280
1281 if (i >= 4) {
Harvey Harrison22b4e642008-04-08 23:20:00 -03001282 dprintk(1, "%s() -EINVAL\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001283 return -EINVAL;
1284 }
1285
1286 mutex_lock(&dev->lock);
1287 cx23885_video_mux(dev, i);
1288 mutex_unlock(&dev->lock);
1289 return 0;
1290}
1291
Andy Wallse9e5cf42010-07-18 18:18:06 -03001292static int vidioc_log_status(struct file *file, void *priv)
1293{
1294 struct cx23885_fh *fh = priv;
1295 struct cx23885_dev *dev = fh->dev;
1296
1297 printk(KERN_INFO
1298 "%s/0: ============ START LOG STATUS ============\n",
1299 dev->name);
1300 call_all(dev, core, log_status);
1301 printk(KERN_INFO
1302 "%s/0: ============= END LOG STATUS =============\n",
1303 dev->name);
1304 return 0;
1305}
1306
Steven Tothe47f30b2008-01-10 04:25:59 -03001307static int vidioc_queryctrl(struct file *file, void *priv,
1308 struct v4l2_queryctrl *qctrl)
1309{
1310 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1311 if (unlikely(qctrl->id == 0))
1312 return -EINVAL;
1313 return cx23885_ctrl_query(qctrl);
1314}
1315
1316static int vidioc_g_ctrl(struct file *file, void *priv,
1317 struct v4l2_control *ctl)
1318{
1319 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1320
1321 return cx23885_get_control(dev, ctl);
1322}
1323
1324static int vidioc_s_ctrl(struct file *file, void *priv,
1325 struct v4l2_control *ctl)
1326{
1327 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1328
1329 return cx23885_set_control(dev, ctl);
1330}
1331
1332static int vidioc_g_tuner(struct file *file, void *priv,
1333 struct v4l2_tuner *t)
1334{
1335 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1336
1337 if (unlikely(UNSET == dev->tuner_type))
1338 return -EINVAL;
1339 if (0 != t->index)
1340 return -EINVAL;
1341
1342 strcpy(t->name, "Television");
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001343
Steven Toth80f1e082011-10-10 11:09:53 -03001344 call_all(dev, tuner, g_tuner, t);
Steven Tothe47f30b2008-01-10 04:25:59 -03001345 return 0;
1346}
1347
1348static int vidioc_s_tuner(struct file *file, void *priv,
1349 struct v4l2_tuner *t)
1350{
1351 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1352
1353 if (UNSET == dev->tuner_type)
1354 return -EINVAL;
1355 if (0 != t->index)
1356 return -EINVAL;
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001357 /* Update the A/V core */
Steven Toth80f1e082011-10-10 11:09:53 -03001358 call_all(dev, tuner, s_tuner, t);
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001359
Steven Tothe47f30b2008-01-10 04:25:59 -03001360 return 0;
1361}
1362
1363static int vidioc_g_frequency(struct file *file, void *priv,
1364 struct v4l2_frequency *f)
1365{
1366 struct cx23885_fh *fh = priv;
1367 struct cx23885_dev *dev = fh->dev;
1368
1369 if (unlikely(UNSET == dev->tuner_type))
1370 return -EINVAL;
1371
1372 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1373 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1374 f->frequency = dev->freq;
1375
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001376 call_all(dev, tuner, g_frequency, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001377
1378 return 0;
1379}
1380
Hans Verkuild45b9b82008-09-04 03:33:43 -03001381static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
Steven Tothe47f30b2008-01-10 04:25:59 -03001382{
1383 if (unlikely(UNSET == dev->tuner_type))
1384 return -EINVAL;
1385 if (unlikely(f->tuner != 0))
1386 return -EINVAL;
1387
1388 mutex_lock(&dev->lock);
1389 dev->freq = f->frequency;
1390
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001391 call_all(dev, tuner, s_frequency, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001392
1393 /* When changing channels it is required to reset TVAUDIO */
1394 msleep(10);
1395
1396 mutex_unlock(&dev->lock);
1397
1398 return 0;
1399}
Steven Tothe47f30b2008-01-10 04:25:59 -03001400
1401static int vidioc_s_frequency(struct file *file, void *priv,
1402 struct v4l2_frequency *f)
1403{
1404 struct cx23885_fh *fh = priv;
1405 struct cx23885_dev *dev = fh->dev;
1406
1407 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1408 return -EINVAL;
1409 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1410 return -EINVAL;
1411
1412 return
1413 cx23885_set_freq(dev, f);
1414}
1415
Steven Tothe47f30b2008-01-10 04:25:59 -03001416/* ----------------------------------------------------------- */
Steven Tothe47f30b2008-01-10 04:25:59 -03001417
1418static void cx23885_vid_timeout(unsigned long data)
1419{
1420 struct cx23885_dev *dev = (struct cx23885_dev *)data;
1421 struct cx23885_dmaqueue *q = &dev->vidq;
1422 struct cx23885_buffer *buf;
1423 unsigned long flags;
1424
1425 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1426
1427 cx_clear(VID_A_DMA_CTL, 0x11);
1428
1429 spin_lock_irqsave(&dev->slock, flags);
1430 while (!list_empty(&q->active)) {
1431 buf = list_entry(q->active.next,
1432 struct cx23885_buffer, vb.queue);
1433 list_del(&buf->vb.queue);
1434 buf->vb.state = VIDEOBUF_ERROR;
1435 wake_up(&buf->vb.done);
Steven Toth79776c82011-10-10 11:09:54 -03001436 printk(KERN_ERR "%s: [%p/%d] timeout - dma=0x%08lx\n",
Steven Tothe47f30b2008-01-10 04:25:59 -03001437 dev->name, buf, buf->vb.i,
1438 (unsigned long)buf->risc.dma);
1439 }
1440 cx23885_restart_video_queue(dev, q);
1441 spin_unlock_irqrestore(&dev->slock, flags);
1442}
1443
1444int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1445{
1446 u32 mask, count;
1447 int handled = 0;
1448
1449 mask = cx_read(VID_A_INT_MSK);
1450 if (0 == (status & mask))
1451 return handled;
Steven Toth79776c82011-10-10 11:09:54 -03001452
Steven Tothe47f30b2008-01-10 04:25:59 -03001453 cx_write(VID_A_INT_STAT, status);
1454
Steven Tothe47f30b2008-01-10 04:25:59 -03001455 /* risc op code error */
Steven Toth79776c82011-10-10 11:09:54 -03001456 if ((status & VID_BC_MSK_OPC_ERR) ||
1457 (status & VID_BC_MSK_SYNC) ||
1458 (status & VID_BC_MSK_OF)) {
1459
1460 if (status & VID_BC_MSK_OPC_ERR)
1461 dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1462 VID_BC_MSK_OPC_ERR);
1463
1464 if (status & VID_BC_MSK_SYNC)
1465 dprintk(7, " (VID_BC_MSK_SYNC 0x%08x)\n",
1466 VID_BC_MSK_SYNC);
1467
1468 if (status & VID_BC_MSK_OF)
1469 dprintk(7, " (VID_BC_MSK_OF 0x%08x)\n",
1470 VID_BC_MSK_OF);
1471
1472 printk(KERN_WARNING "%s: video risc op code error\n",
Steven Tothe47f30b2008-01-10 04:25:59 -03001473 dev->name);
Steven Toth79776c82011-10-10 11:09:54 -03001474
Steven Tothe47f30b2008-01-10 04:25:59 -03001475 cx_clear(VID_A_DMA_CTL, 0x11);
1476 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
Steven Toth79776c82011-10-10 11:09:54 -03001477
Steven Tothe47f30b2008-01-10 04:25:59 -03001478 }
1479
Steven Toth79776c82011-10-10 11:09:54 -03001480 /* Video */
1481 if (status & VID_BC_MSK_RISCI1) {
Steven Tothe47f30b2008-01-10 04:25:59 -03001482 spin_lock(&dev->slock);
1483 count = cx_read(VID_A_GPCNT);
1484 cx23885_video_wakeup(dev, &dev->vidq, count);
1485 spin_unlock(&dev->slock);
1486 handled++;
1487 }
Steven Toth79776c82011-10-10 11:09:54 -03001488 if (status & VID_BC_MSK_RISCI2) {
Steven Tothe47f30b2008-01-10 04:25:59 -03001489 dprintk(2, "stopper video\n");
1490 spin_lock(&dev->slock);
1491 cx23885_restart_video_queue(dev, &dev->vidq);
1492 spin_unlock(&dev->slock);
1493 handled++;
1494 }
1495
Steven Toth79776c82011-10-10 11:09:54 -03001496 /* Allow the VBI framework to process it's payload */
1497 handled += cx23885_vbi_irq(dev, status);
1498
Steven Tothe47f30b2008-01-10 04:25:59 -03001499 return handled;
1500}
1501
Steven Tothe47f30b2008-01-10 04:25:59 -03001502/* ----------------------------------------------------------- */
1503/* exported stuff */
1504
Hans Verkuilbec43662008-12-30 06:58:20 -03001505static const struct v4l2_file_operations video_fops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001506 .owner = THIS_MODULE,
1507 .open = video_open,
1508 .release = video_release,
1509 .read = video_read,
1510 .poll = video_poll,
1511 .mmap = video_mmap,
1512 .ioctl = video_ioctl2,
Steven Tothe47f30b2008-01-10 04:25:59 -03001513};
1514
Hans Verkuila3998102008-07-21 02:57:38 -03001515static const struct v4l2_ioctl_ops video_ioctl_ops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001516 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001517 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1518 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1519 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1520 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1521 .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
1522 .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
1523 .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
Steven Tothe47f30b2008-01-10 04:25:59 -03001524 .vidioc_reqbufs = vidioc_reqbufs,
1525 .vidioc_querybuf = vidioc_querybuf,
1526 .vidioc_qbuf = vidioc_qbuf,
1527 .vidioc_dqbuf = vidioc_dqbuf,
1528 .vidioc_s_std = vidioc_s_std,
1529 .vidioc_enum_input = vidioc_enum_input,
1530 .vidioc_g_input = vidioc_g_input,
1531 .vidioc_s_input = vidioc_s_input,
Andy Wallse9e5cf42010-07-18 18:18:06 -03001532 .vidioc_log_status = vidioc_log_status,
Steven Tothe47f30b2008-01-10 04:25:59 -03001533 .vidioc_queryctrl = vidioc_queryctrl,
1534 .vidioc_g_ctrl = vidioc_g_ctrl,
1535 .vidioc_s_ctrl = vidioc_s_ctrl,
1536 .vidioc_streamon = vidioc_streamon,
1537 .vidioc_streamoff = vidioc_streamoff,
Steven Tothe47f30b2008-01-10 04:25:59 -03001538 .vidioc_g_tuner = vidioc_g_tuner,
1539 .vidioc_s_tuner = vidioc_s_tuner,
1540 .vidioc_g_frequency = vidioc_g_frequency,
1541 .vidioc_s_frequency = vidioc_s_frequency,
Andy Walls74618242009-09-26 22:50:44 -03001542 .vidioc_g_chip_ident = cx23885_g_chip_ident,
Steven Tothe47f30b2008-01-10 04:25:59 -03001543#ifdef CONFIG_VIDEO_ADV_DEBUG
Andy Walls74618242009-09-26 22:50:44 -03001544 .vidioc_g_register = cx23885_g_register,
1545 .vidioc_s_register = cx23885_s_register,
Steven Tothe47f30b2008-01-10 04:25:59 -03001546#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001547};
1548
1549static struct video_device cx23885_vbi_template;
1550static struct video_device cx23885_video_template = {
1551 .name = "cx23885-video",
Hans Verkuila3998102008-07-21 02:57:38 -03001552 .fops = &video_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03001553 .ioctl_ops = &video_ioctl_ops,
Steven Tothe47f30b2008-01-10 04:25:59 -03001554 .tvnorms = CX23885_NORMS,
1555 .current_norm = V4L2_STD_NTSC_M,
1556};
1557
Hans Verkuilbec43662008-12-30 06:58:20 -03001558static const struct v4l2_file_operations radio_fops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001559 .owner = THIS_MODULE,
1560 .open = video_open,
1561 .release = video_release,
1562 .ioctl = video_ioctl2,
Steven Tothe47f30b2008-01-10 04:25:59 -03001563};
1564
1565
1566void cx23885_video_unregister(struct cx23885_dev *dev)
1567{
Harvey Harrison22b4e642008-04-08 23:20:00 -03001568 dprintk(1, "%s()\n", __func__);
Andy Wallsdbe83a32010-07-19 01:19:43 -03001569 cx23885_irq_remove(dev, 0x01);
Steven Tothe47f30b2008-01-10 04:25:59 -03001570
Steven Toth79776c82011-10-10 11:09:54 -03001571 if (dev->vbi_dev) {
1572 if (video_is_registered(dev->vbi_dev))
1573 video_unregister_device(dev->vbi_dev);
1574 else
1575 video_device_release(dev->vbi_dev);
1576 dev->vbi_dev = NULL;
1577 btcx_riscmem_free(dev->pci, &dev->vbiq.stopper);
1578 }
Steven Tothe47f30b2008-01-10 04:25:59 -03001579 if (dev->video_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001580 if (video_is_registered(dev->video_dev))
Steven Tothe47f30b2008-01-10 04:25:59 -03001581 video_unregister_device(dev->video_dev);
1582 else
1583 video_device_release(dev->video_dev);
1584 dev->video_dev = NULL;
1585
1586 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1587 }
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001588
1589 if (dev->audio_dev)
Steven Tothefa762f2011-10-10 11:09:53 -03001590 cx23885_audio_unregister(dev);
Steven Tothe47f30b2008-01-10 04:25:59 -03001591}
1592
1593int cx23885_video_register(struct cx23885_dev *dev)
1594{
1595 int err;
1596
Harvey Harrison22b4e642008-04-08 23:20:00 -03001597 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001598 spin_lock_init(&dev->slock);
1599
1600 /* Initialize VBI template */
1601 memcpy(&cx23885_vbi_template, &cx23885_video_template,
1602 sizeof(cx23885_vbi_template));
1603 strcpy(cx23885_vbi_template.name, "cx23885-vbi");
Steven Tothe47f30b2008-01-10 04:25:59 -03001604
1605 dev->tvnorm = cx23885_video_template.current_norm;
1606
1607 /* init video dma queues */
1608 INIT_LIST_HEAD(&dev->vidq.active);
1609 INIT_LIST_HEAD(&dev->vidq.queued);
1610 dev->vidq.timeout.function = cx23885_vid_timeout;
1611 dev->vidq.timeout.data = (unsigned long)dev;
1612 init_timer(&dev->vidq.timeout);
1613 cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1614 VID_A_DMA_CTL, 0x11, 0x00);
1615
Steven Toth79776c82011-10-10 11:09:54 -03001616 /* init vbi dma queues */
1617 INIT_LIST_HEAD(&dev->vbiq.active);
1618 INIT_LIST_HEAD(&dev->vbiq.queued);
1619 dev->vbiq.timeout.function = cx23885_vbi_timeout;
1620 dev->vbiq.timeout.data = (unsigned long)dev;
1621 init_timer(&dev->vbiq.timeout);
1622 cx23885_risc_stopper(dev->pci, &dev->vbiq.stopper,
1623 VID_A_DMA_CTL, 0x22, 0x00);
Andy Wallsdbe83a32010-07-19 01:19:43 -03001624
1625 cx23885_irq_add_enable(dev, 0x01);
Steven Tothe47f30b2008-01-10 04:25:59 -03001626
Igor M. Liplianin557f48d2011-01-25 17:05:00 -03001627 if ((TUNER_ABSENT != dev->tuner_type) &&
1628 ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001629 struct v4l2_subdev *sd = NULL;
1630
1631 if (dev->tuner_addr)
Hans Verkuile6574f22009-04-01 03:57:53 -03001632 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
Igor M. Liplianin557f48d2011-01-25 17:05:00 -03001633 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03001634 "tuner", dev->tuner_addr, NULL);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001635 else
Hans Verkuil53dacb12009-08-10 02:49:08 -03001636 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
Igor M. Liplianin557f48d2011-01-25 17:05:00 -03001637 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
Laurent Pinchart1532a072010-09-24 07:58:29 -03001638 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001639 if (sd) {
1640 struct tuner_setup tun_setup;
1641
David T.L. Wongfd705e72009-10-21 11:08:30 -03001642 memset(&tun_setup, 0, sizeof(tun_setup));
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001643 tun_setup.mode_mask = T_ANALOG_TV;
1644 tun_setup.type = dev->tuner_type;
1645 tun_setup.addr = v4l2_i2c_subdev_addr(sd);
David T.L. Wong6f0d8c02009-10-21 13:15:30 -03001646 tun_setup.tuner_callback = cx23885_tuner_callback;
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001647
1648 v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
Kusanagi Kouichi0b32d652010-01-22 04:55:28 -03001649
1650 if (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) {
1651 struct xc2028_ctrl ctrl = {
1652 .fname = XC2028_DEFAULT_FIRMWARE,
1653 .max_len = 64
1654 };
1655 struct v4l2_priv_tun_config cfg = {
1656 .tuner = dev->tuner_type,
1657 .priv = &ctrl
1658 };
1659 v4l2_subdev_call(sd, tuner, s_config, &cfg);
1660 }
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001661 }
1662 }
1663
Steven Toth79776c82011-10-10 11:09:54 -03001664 /* register Video device */
Steven Tothe47f30b2008-01-10 04:25:59 -03001665 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1666 &cx23885_video_template, "video");
1667 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1668 video_nr[dev->nr]);
1669 if (err < 0) {
1670 printk(KERN_INFO "%s: can't register video device\n",
1671 dev->name);
1672 goto fail_unreg;
1673 }
Steven Tothf88fb8e2011-10-10 11:09:54 -03001674 printk(KERN_INFO "%s: registered device %s [v4l2]\n",
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001675 dev->name, video_device_node_name(dev->video_dev));
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001676
Steven Tothf88fb8e2011-10-10 11:09:54 -03001677 /* register VBI device */
1678 dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
1679 &cx23885_vbi_template, "vbi");
1680 err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1681 vbi_nr[dev->nr]);
1682 if (err < 0) {
1683 printk(KERN_INFO "%s: can't register vbi device\n",
1684 dev->name);
1685 goto fail_unreg;
1686 }
1687 printk(KERN_INFO "%s: registered device %s\n",
1688 dev->name, video_device_node_name(dev->vbi_dev));
1689
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001690 /* Register ALSA audio device */
Steven Tothefa762f2011-10-10 11:09:53 -03001691 dev->audio_dev = cx23885_audio_register(dev);
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001692
Steven Tothe47f30b2008-01-10 04:25:59 -03001693 /* initial device configuration */
1694 mutex_lock(&dev->lock);
1695 cx23885_set_tvnorm(dev, dev->tvnorm);
1696 init_controls(dev);
1697 cx23885_video_mux(dev, 0);
1698 mutex_unlock(&dev->lock);
1699
Steven Tothe47f30b2008-01-10 04:25:59 -03001700 return 0;
1701
1702fail_unreg:
1703 cx23885_video_unregister(dev);
1704 return err;
1705}
1706