blob: 4bbf9bb97bde984e23c0665fda59a72bc7dbd80a [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,
Steven Toth4c3764d2012-01-04 10:47:57 -0300256 .maximum = 65535,
257 .step = 65535 / 100,
258 .default_value = 65535,
Steven Tothe47f30b2008-01-10 04:25:59 -0300259 .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
Steven Toth35045132012-01-04 21:08:35 -0300319int 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;
Steven Toth9def5ed2012-01-04 21:13:26 -0300347 snprintf(vfd->name, sizeof(vfd->name), "%s (%s)",
348 cx23885_boards[dev->board].name, type);
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 ||
Alfredo Jesús Delaiti87988752011-11-09 15:13:00 -0300495 dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2 ||
496 dev->board == CX23885_BOARD_MYGICA_X8507) {
David T.L. Wong6f0d8c02009-10-21 13:15:30 -0300497 /* Select Analog TV */
498 if (INPUT(input)->type == CX23885_VMUX_TELEVISION)
499 cx23885_gpio_clear(dev, GPIO_0);
500 }
501
Steven Tothe47f30b2008-01-10 04:25:59 -0300502 /* Tell the internal A/V decoder */
Hans Verkuil5325b422009-04-02 11:26:22 -0300503 v4l2_subdev_call(dev->sd_cx25840, video, s_routing,
504 INPUT(input)->vmux, 0, 0);
Steven Tothe47f30b2008-01-10 04:25:59 -0300505
Steven Toth2cb9ccd2011-10-10 11:09:55 -0300506 if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) ||
Steven Toth35045132012-01-04 21:08:35 -0300507 (dev->board == CX23885_BOARD_MPX885) ||
508 (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850)) {
Steven Toth33cdeb32011-10-10 11:09:55 -0300509 /* Configure audio routing */
510 v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
511 INPUT(input)->amux, 0, 0);
512
513 if (INPUT(input)->amux == CX25840_AUDIO7)
514 cx23885_flatiron_mux(dev, 1);
515 else if (INPUT(input)->amux == CX25840_AUDIO6)
516 cx23885_flatiron_mux(dev, 2);
517 }
518
Steven Tothe47f30b2008-01-10 04:25:59 -0300519 return 0;
520}
Steven Tothe47f30b2008-01-10 04:25:59 -0300521
Steven Tothfc1a8892011-10-10 11:09:56 -0300522static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input)
523{
524 dprintk(1, "%s(input=%d)\n", __func__, input);
525
Steven Tothfa1e0fd2011-10-10 11:09:56 -0300526 /* The baseband video core of the cx23885 has two audio inputs.
527 * LR1 and LR2. In almost every single case so far only HVR1xxx
528 * cards we've only ever supported LR1. Time to support LR2,
529 * which is available via the optional white breakout header on
530 * the board.
531 * We'll use a could of existing enums in the card struct to allow
532 * devs to specify which baseband input they need, or just default
533 * to what we've always used.
534 */
535 if (INPUT(input)->amux == CX25840_AUDIO7)
536 cx23885_flatiron_mux(dev, 1);
537 else if (INPUT(input)->amux == CX25840_AUDIO6)
538 cx23885_flatiron_mux(dev, 2);
539 else {
540 /* Not specifically defined, assume the default. */
541 cx23885_flatiron_mux(dev, 1);
Steven Tothfc1a8892011-10-10 11:09:56 -0300542 }
543
544 return 0;
545}
546
Steven Tothe47f30b2008-01-10 04:25:59 -0300547/* ------------------------------------------------------------------ */
Steven Tothe47f30b2008-01-10 04:25:59 -0300548static int cx23885_start_video_dma(struct cx23885_dev *dev,
549 struct cx23885_dmaqueue *q,
550 struct cx23885_buffer *buf)
551{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300552 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300553
Steven Totha461e0a2011-10-10 11:09:56 -0300554 /* Stop the dma/fifo before we tamper with it's risc programs */
555 cx_clear(VID_A_DMA_CTL, 0x11);
556
Steven Tothe47f30b2008-01-10 04:25:59 -0300557 /* setup fifo + format */
558 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
559 buf->bpl, buf->risc.dma);
Steven Tothe47f30b2008-01-10 04:25:59 -0300560
561 /* reset counter */
562 cx_write(VID_A_GPCNT_CTL, 3);
563 q->count = 1;
564
565 /* enable irq */
Andy Wallsdbe83a32010-07-19 01:19:43 -0300566 cx23885_irq_add_enable(dev, 0x01);
Steven Tothe47f30b2008-01-10 04:25:59 -0300567 cx_set(VID_A_INT_MSK, 0x000011);
568
569 /* start dma */
570 cx_set(DEV_CNTRL2, (1<<5));
571 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
572
573 return 0;
574}
575
Steven Tothe47f30b2008-01-10 04:25:59 -0300576
577static int cx23885_restart_video_queue(struct cx23885_dev *dev,
578 struct cx23885_dmaqueue *q)
579{
580 struct cx23885_buffer *buf, *prev;
581 struct list_head *item;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300582 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300583
584 if (!list_empty(&q->active)) {
585 buf = list_entry(q->active.next, struct cx23885_buffer,
586 vb.queue);
587 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
588 buf, buf->vb.i);
589 cx23885_start_video_dma(dev, q, buf);
590 list_for_each(item, &q->active) {
591 buf = list_entry(item, struct cx23885_buffer,
592 vb.queue);
593 buf->count = q->count++;
594 }
595 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
596 return 0;
597 }
598
599 prev = NULL;
600 for (;;) {
601 if (list_empty(&q->queued))
602 return 0;
603 buf = list_entry(q->queued.next, struct cx23885_buffer,
604 vb.queue);
605 if (NULL == prev) {
606 list_move_tail(&buf->vb.queue, &q->active);
607 cx23885_start_video_dma(dev, q, buf);
608 buf->vb.state = VIDEOBUF_ACTIVE;
609 buf->count = q->count++;
610 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
611 dprintk(2, "[%p/%d] restart_queue - first active\n",
612 buf, buf->vb.i);
613
614 } else if (prev->vb.width == buf->vb.width &&
615 prev->vb.height == buf->vb.height &&
616 prev->fmt == buf->fmt) {
617 list_move_tail(&buf->vb.queue, &q->active);
618 buf->vb.state = VIDEOBUF_ACTIVE;
619 buf->count = q->count++;
620 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
621 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
622 dprintk(2, "[%p/%d] restart_queue - move to active\n",
623 buf, buf->vb.i);
624 } else {
625 return 0;
626 }
627 prev = buf;
628 }
629}
630
631static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
632 unsigned int *size)
633{
634 struct cx23885_fh *fh = q->priv_data;
635
636 *size = fh->fmt->depth*fh->width*fh->height >> 3;
637 if (0 == *count)
638 *count = 32;
Andreas Bombedab7e312010-03-21 16:02:45 -0300639 if (*size * *count > vid_limit * 1024 * 1024)
640 *count = (vid_limit * 1024 * 1024) / *size;
Steven Tothe47f30b2008-01-10 04:25:59 -0300641 return 0;
642}
643
644static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
645 enum v4l2_field field)
646{
647 struct cx23885_fh *fh = q->priv_data;
648 struct cx23885_dev *dev = fh->dev;
649 struct cx23885_buffer *buf =
650 container_of(vb, struct cx23885_buffer, vb);
651 int rc, init_buffer = 0;
652 u32 line0_offset, line1_offset;
653 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
Steven Toth35045132012-01-04 21:08:35 -0300654 int field_tff;
Steven Tothe47f30b2008-01-10 04:25:59 -0300655
656 BUG_ON(NULL == fh->fmt);
657 if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
658 fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
659 return -EINVAL;
660 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
661 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
662 return -EINVAL;
663
664 if (buf->fmt != fh->fmt ||
665 buf->vb.width != fh->width ||
666 buf->vb.height != fh->height ||
667 buf->vb.field != field) {
668 buf->fmt = fh->fmt;
669 buf->vb.width = fh->width;
670 buf->vb.height = fh->height;
671 buf->vb.field = field;
672 init_buffer = 1;
673 }
674
675 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
676 init_buffer = 1;
677 rc = videobuf_iolock(q, &buf->vb, NULL);
678 if (0 != rc)
679 goto fail;
680 }
681
682 if (init_buffer) {
683 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
684 switch (buf->vb.field) {
685 case V4L2_FIELD_TOP:
686 cx23885_risc_buffer(dev->pci, &buf->risc,
687 dma->sglist, 0, UNSET,
688 buf->bpl, 0, buf->vb.height);
689 break;
690 case V4L2_FIELD_BOTTOM:
691 cx23885_risc_buffer(dev->pci, &buf->risc,
692 dma->sglist, UNSET, 0,
693 buf->bpl, 0, buf->vb.height);
694 break;
695 case V4L2_FIELD_INTERLACED:
Steven Toth35045132012-01-04 21:08:35 -0300696 if (dev->tvnorm & V4L2_STD_NTSC)
697 /* NTSC or */
698 field_tff = 1;
699 else
700 field_tff = 0;
701
702 if (cx23885_boards[dev->board].force_bff)
703 /* PAL / SECAM OR 888 in NTSC MODE */
704 field_tff = 0;
705
706 if (field_tff) {
Steven Tothe47f30b2008-01-10 04:25:59 -0300707 /* cx25840 transmits NTSC bottom field first */
Steven Toth35045132012-01-04 21:08:35 -0300708 dprintk(1, "%s() Creating TFF/NTSC risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300709 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300710 line0_offset = buf->bpl;
711 line1_offset = 0;
712 } else {
713 /* All other formats are top field first */
Steven Toth35045132012-01-04 21:08:35 -0300714 dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300715 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300716 line0_offset = 0;
717 line1_offset = buf->bpl;
718 }
719 cx23885_risc_buffer(dev->pci, &buf->risc,
720 dma->sglist, line0_offset,
721 line1_offset,
722 buf->bpl, buf->bpl,
723 buf->vb.height >> 1);
724 break;
725 case V4L2_FIELD_SEQ_TB:
726 cx23885_risc_buffer(dev->pci, &buf->risc,
727 dma->sglist,
728 0, buf->bpl * (buf->vb.height >> 1),
729 buf->bpl, 0,
730 buf->vb.height >> 1);
731 break;
732 case V4L2_FIELD_SEQ_BT:
733 cx23885_risc_buffer(dev->pci, &buf->risc,
734 dma->sglist,
735 buf->bpl * (buf->vb.height >> 1), 0,
736 buf->bpl, 0,
737 buf->vb.height >> 1);
738 break;
739 default:
740 BUG();
741 }
742 }
743 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
744 buf, buf->vb.i,
745 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
746 (unsigned long)buf->risc.dma);
747
748 buf->vb.state = VIDEOBUF_PREPARED;
749 return 0;
750
751 fail:
752 cx23885_free_buffer(q, buf);
753 return rc;
754}
755
756static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
757{
758 struct cx23885_buffer *buf = container_of(vb,
759 struct cx23885_buffer, vb);
760 struct cx23885_buffer *prev;
761 struct cx23885_fh *fh = vq->priv_data;
762 struct cx23885_dev *dev = fh->dev;
763 struct cx23885_dmaqueue *q = &dev->vidq;
764
765 /* add jump to stopper */
766 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
767 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
768 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
769
770 if (!list_empty(&q->queued)) {
771 list_add_tail(&buf->vb.queue, &q->queued);
772 buf->vb.state = VIDEOBUF_QUEUED;
773 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
774 buf, buf->vb.i);
775
776 } else if (list_empty(&q->active)) {
777 list_add_tail(&buf->vb.queue, &q->active);
778 cx23885_start_video_dma(dev, q, buf);
779 buf->vb.state = VIDEOBUF_ACTIVE;
780 buf->count = q->count++;
781 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
782 dprintk(2, "[%p/%d] buffer_queue - first active\n",
783 buf, buf->vb.i);
784
785 } else {
786 prev = list_entry(q->active.prev, struct cx23885_buffer,
787 vb.queue);
788 if (prev->vb.width == buf->vb.width &&
789 prev->vb.height == buf->vb.height &&
790 prev->fmt == buf->fmt) {
791 list_add_tail(&buf->vb.queue, &q->active);
792 buf->vb.state = VIDEOBUF_ACTIVE;
793 buf->count = q->count++;
794 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
795 /* 64 bit bits 63-32 */
796 prev->risc.jmp[2] = cpu_to_le32(0);
797 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
798 buf, buf->vb.i);
799
800 } else {
801 list_add_tail(&buf->vb.queue, &q->queued);
802 buf->vb.state = VIDEOBUF_QUEUED;
803 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
804 buf, buf->vb.i);
805 }
806 }
807}
808
809static void buffer_release(struct videobuf_queue *q,
810 struct videobuf_buffer *vb)
811{
812 struct cx23885_buffer *buf = container_of(vb,
813 struct cx23885_buffer, vb);
814
815 cx23885_free_buffer(q, buf);
816}
817
818static struct videobuf_queue_ops cx23885_video_qops = {
819 .buf_setup = buffer_setup,
820 .buf_prepare = buffer_prepare,
821 .buf_queue = buffer_queue,
822 .buf_release = buffer_release,
823};
824
825static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
826{
827 switch (fh->type) {
828 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
829 return &fh->vidq;
830 case V4L2_BUF_TYPE_VBI_CAPTURE:
831 return &fh->vbiq;
832 default:
833 BUG();
834 return NULL;
835 }
836}
837
838static int get_resource(struct cx23885_fh *fh)
839{
840 switch (fh->type) {
841 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
842 return RESOURCE_VIDEO;
843 case V4L2_BUF_TYPE_VBI_CAPTURE:
844 return RESOURCE_VBI;
845 default:
846 BUG();
847 return 0;
848 }
849}
850
Hans Verkuilbec43662008-12-30 06:58:20 -0300851static int video_open(struct file *file)
Steven Tothe47f30b2008-01-10 04:25:59 -0300852{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200853 struct video_device *vdev = video_devdata(file);
854 struct cx23885_dev *dev = video_drvdata(file);
Steven Tothe47f30b2008-01-10 04:25:59 -0300855 struct cx23885_fh *fh;
Steven Tothe47f30b2008-01-10 04:25:59 -0300856 enum v4l2_buf_type type = 0;
857 int radio = 0;
858
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200859 switch (vdev->vfl_type) {
860 case VFL_TYPE_GRABBER:
861 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
862 break;
863 case VFL_TYPE_VBI:
864 type = V4L2_BUF_TYPE_VBI_CAPTURE;
865 break;
866 case VFL_TYPE_RADIO:
867 radio = 1;
868 break;
Hans Verkuild56dc612008-07-30 08:43:36 -0300869 }
Steven Tothe47f30b2008-01-10 04:25:59 -0300870
Laurent Pinchart50462eb2009-12-10 11:47:13 -0200871 dprintk(1, "open dev=%s radio=%d type=%s\n",
872 video_device_node_name(vdev), radio, v4l2_type_names[type]);
Steven Tothe47f30b2008-01-10 04:25:59 -0300873
874 /* allocate + initialize per filehandle data */
875 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200876 if (NULL == fh)
Steven Tothe47f30b2008-01-10 04:25:59 -0300877 return -ENOMEM;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200878
Steven Tothe47f30b2008-01-10 04:25:59 -0300879 file->private_data = fh;
880 fh->dev = dev;
881 fh->radio = radio;
882 fh->type = type;
883 fh->width = 320;
884 fh->height = 240;
Steven Tothaf76e9f2011-10-10 11:09:54 -0300885 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
Steven Tothe47f30b2008-01-10 04:25:59 -0300886
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300887 videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
888 &dev->pci->dev, &dev->slock,
Steven Tothe47f30b2008-01-10 04:25:59 -0300889 V4L2_BUF_TYPE_VIDEO_CAPTURE,
890 V4L2_FIELD_INTERLACED,
891 sizeof(struct cx23885_buffer),
Hans Verkuil08bff032010-09-20 17:39:46 -0300892 fh, NULL);
Steven Tothe47f30b2008-01-10 04:25:59 -0300893
Steven Toth79776c82011-10-10 11:09:54 -0300894 videobuf_queue_sg_init(&fh->vbiq, &cx23885_vbi_qops,
895 &dev->pci->dev, &dev->slock,
896 V4L2_BUF_TYPE_VBI_CAPTURE,
897 V4L2_FIELD_SEQ_TB,
898 sizeof(struct cx23885_buffer),
899 fh, NULL);
900
901
Steven Tothe47f30b2008-01-10 04:25:59 -0300902 dprintk(1, "post videobuf_queue_init()\n");
903
Steven Tothe47f30b2008-01-10 04:25:59 -0300904 return 0;
905}
906
907static ssize_t video_read(struct file *file, char __user *data,
908 size_t count, loff_t *ppos)
909{
910 struct cx23885_fh *fh = file->private_data;
911
912 switch (fh->type) {
913 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
914 if (res_locked(fh->dev, RESOURCE_VIDEO))
915 return -EBUSY;
916 return videobuf_read_one(&fh->vidq, data, count, ppos,
917 file->f_flags & O_NONBLOCK);
918 case V4L2_BUF_TYPE_VBI_CAPTURE:
919 if (!res_get(fh->dev, fh, RESOURCE_VBI))
920 return -EBUSY;
921 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
922 file->f_flags & O_NONBLOCK);
923 default:
924 BUG();
925 return 0;
926 }
927}
928
929static unsigned int video_poll(struct file *file,
930 struct poll_table_struct *wait)
931{
932 struct cx23885_fh *fh = file->private_data;
933 struct cx23885_buffer *buf;
Figo.zhang9fd64182009-06-16 13:31:29 -0300934 unsigned int rc = POLLERR;
Steven Tothe47f30b2008-01-10 04:25:59 -0300935
936 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
937 if (!res_get(fh->dev, fh, RESOURCE_VBI))
938 return POLLERR;
939 return videobuf_poll_stream(file, &fh->vbiq, wait);
940 }
941
Figo.zhang9fd64182009-06-16 13:31:29 -0300942 mutex_lock(&fh->vidq.vb_lock);
Steven Tothe47f30b2008-01-10 04:25:59 -0300943 if (res_check(fh, RESOURCE_VIDEO)) {
944 /* streaming capture */
945 if (list_empty(&fh->vidq.stream))
Figo.zhang9fd64182009-06-16 13:31:29 -0300946 goto done;
Steven Tothe47f30b2008-01-10 04:25:59 -0300947 buf = list_entry(fh->vidq.stream.next,
948 struct cx23885_buffer, vb.stream);
949 } else {
950 /* read() capture */
951 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
952 if (NULL == buf)
Figo.zhang9fd64182009-06-16 13:31:29 -0300953 goto done;
Steven Tothe47f30b2008-01-10 04:25:59 -0300954 }
955 poll_wait(file, &buf->vb.done, wait);
956 if (buf->vb.state == VIDEOBUF_DONE ||
957 buf->vb.state == VIDEOBUF_ERROR)
Figo.zhang9fd64182009-06-16 13:31:29 -0300958 rc = POLLIN|POLLRDNORM;
959 else
960 rc = 0;
961done:
962 mutex_unlock(&fh->vidq.vb_lock);
963 return rc;
Steven Tothe47f30b2008-01-10 04:25:59 -0300964}
965
Hans Verkuilbec43662008-12-30 06:58:20 -0300966static int video_release(struct file *file)
Steven Tothe47f30b2008-01-10 04:25:59 -0300967{
968 struct cx23885_fh *fh = file->private_data;
969 struct cx23885_dev *dev = fh->dev;
970
971 /* turn off overlay */
972 if (res_check(fh, RESOURCE_OVERLAY)) {
973 /* FIXME */
974 res_free(dev, fh, RESOURCE_OVERLAY);
975 }
976
977 /* stop video capture */
978 if (res_check(fh, RESOURCE_VIDEO)) {
979 videobuf_queue_cancel(&fh->vidq);
980 res_free(dev, fh, RESOURCE_VIDEO);
981 }
982 if (fh->vidq.read_buf) {
983 buffer_release(&fh->vidq, fh->vidq.read_buf);
984 kfree(fh->vidq.read_buf);
985 }
986
987 /* stop vbi capture */
988 if (res_check(fh, RESOURCE_VBI)) {
989 if (fh->vbiq.streaming)
990 videobuf_streamoff(&fh->vbiq);
991 if (fh->vbiq.reading)
992 videobuf_read_stop(&fh->vbiq);
993 res_free(dev, fh, RESOURCE_VBI);
994 }
995
996 videobuf_mmap_free(&fh->vidq);
Steven Toth35045132012-01-04 21:08:35 -0300997 videobuf_mmap_free(&fh->vbiq);
998
Steven Tothe47f30b2008-01-10 04:25:59 -0300999 file->private_data = NULL;
1000 kfree(fh);
1001
Steven Totha19602f22008-01-10 11:43:18 -03001002 /* We are not putting the tuner to sleep here on exit, because
1003 * we want to use the mpeg encoder in another session to capture
1004 * tuner video. Closing this will result in no video to the encoder.
1005 */
Steven Tothe47f30b2008-01-10 04:25:59 -03001006
1007 return 0;
1008}
1009
1010static int video_mmap(struct file *file, struct vm_area_struct *vma)
1011{
1012 struct cx23885_fh *fh = file->private_data;
1013
1014 return videobuf_mmap_mapper(get_queue(fh), vma);
1015}
1016
1017/* ------------------------------------------------------------------ */
1018/* VIDEO CTRL IOCTLS */
1019
Steven Toth35045132012-01-04 21:08:35 -03001020int cx23885_get_control(struct cx23885_dev *dev,
Steven Toth9c8ced52008-10-16 20:18:44 -03001021 struct v4l2_control *ctl)
Steven Tothe47f30b2008-01-10 04:25:59 -03001022{
Harvey Harrison22b4e642008-04-08 23:20:00 -03001023 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001024 call_all(dev, core, g_ctrl, ctl);
Steven Tothe47f30b2008-01-10 04:25:59 -03001025 return 0;
1026}
Steven Tothe47f30b2008-01-10 04:25:59 -03001027
Steven Toth35045132012-01-04 21:08:35 -03001028int cx23885_set_control(struct cx23885_dev *dev,
Steven Toth9c8ced52008-10-16 20:18:44 -03001029 struct v4l2_control *ctl)
Steven Tothe47f30b2008-01-10 04:25:59 -03001030{
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001031 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)\n", __func__);
1032 call_all(dev, core, s_ctrl, ctl);
1033
Steven Tothe47f30b2008-01-10 04:25:59 -03001034 return 0;
1035}
Steven Tothe47f30b2008-01-10 04:25:59 -03001036
1037static void init_controls(struct cx23885_dev *dev)
1038{
1039 struct v4l2_control ctrl;
1040 int i;
1041
1042 for (i = 0; i < CX23885_CTLS; i++) {
1043 ctrl.id = cx23885_ctls[i].v.id;
1044 ctrl.value = cx23885_ctls[i].v.default_value;
1045
1046 cx23885_set_control(dev, &ctrl);
1047 }
1048}
1049
1050/* ------------------------------------------------------------------ */
1051/* VIDEO IOCTLS */
1052
Hans Verkuil78b526a2008-05-28 12:16:41 -03001053static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001054 struct v4l2_format *f)
1055{
1056 struct cx23885_fh *fh = priv;
1057
1058 f->fmt.pix.width = fh->width;
1059 f->fmt.pix.height = fh->height;
1060 f->fmt.pix.field = fh->vidq.field;
1061 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1062 f->fmt.pix.bytesperline =
1063 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1064 f->fmt.pix.sizeimage =
1065 f->fmt.pix.height * f->fmt.pix.bytesperline;
1066
1067 return 0;
1068}
1069
Hans Verkuil78b526a2008-05-28 12:16:41 -03001070static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001071 struct v4l2_format *f)
1072{
1073 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1074 struct cx23885_fmt *fmt;
1075 enum v4l2_field field;
1076 unsigned int maxw, maxh;
1077
1078 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1079 if (NULL == fmt)
1080 return -EINVAL;
1081
1082 field = f->fmt.pix.field;
1083 maxw = norm_maxw(dev->tvnorm);
1084 maxh = norm_maxh(dev->tvnorm);
1085
1086 if (V4L2_FIELD_ANY == field) {
1087 field = (f->fmt.pix.height > maxh/2)
1088 ? V4L2_FIELD_INTERLACED
1089 : V4L2_FIELD_BOTTOM;
1090 }
1091
1092 switch (field) {
1093 case V4L2_FIELD_TOP:
1094 case V4L2_FIELD_BOTTOM:
1095 maxh = maxh / 2;
1096 break;
1097 case V4L2_FIELD_INTERLACED:
1098 break;
1099 default:
1100 return -EINVAL;
1101 }
1102
1103 f->fmt.pix.field = field;
Trent Piepho2449afc2009-05-30 21:45:46 -03001104 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
1105 &f->fmt.pix.height, 32, maxh, 0, 0);
Steven Tothe47f30b2008-01-10 04:25:59 -03001106 f->fmt.pix.bytesperline =
1107 (f->fmt.pix.width * fmt->depth) >> 3;
1108 f->fmt.pix.sizeimage =
1109 f->fmt.pix.height * f->fmt.pix.bytesperline;
1110
1111 return 0;
1112}
1113
Hans Verkuil78b526a2008-05-28 12:16:41 -03001114static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001115 struct v4l2_format *f)
1116{
1117 struct cx23885_fh *fh = priv;
1118 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Hans Verkuilcc991132010-05-09 10:09:28 -03001119 struct v4l2_mbus_framefmt mbus_fmt;
Steven Tothe47f30b2008-01-10 04:25:59 -03001120 int err;
1121
Harvey Harrison22b4e642008-04-08 23:20:00 -03001122 dprintk(2, "%s()\n", __func__);
Hans Verkuil78b526a2008-05-28 12:16:41 -03001123 err = vidioc_try_fmt_vid_cap(file, priv, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001124
1125 if (0 != err)
1126 return err;
1127 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1128 fh->width = f->fmt.pix.width;
1129 fh->height = f->fmt.pix.height;
1130 fh->vidq.field = f->fmt.pix.field;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001131 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -03001132 fh->width, fh->height, fh->vidq.field);
Hans Verkuilcc991132010-05-09 10:09:28 -03001133 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
1134 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1135 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
Steven Tothe47f30b2008-01-10 04:25:59 -03001136 return 0;
1137}
1138
1139static int vidioc_querycap(struct file *file, void *priv,
1140 struct v4l2_capability *cap)
1141{
1142 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1143
1144 strcpy(cap->driver, "cx23885");
1145 strlcpy(cap->card, cx23885_boards[dev->board].name,
1146 sizeof(cap->card));
1147 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
Steven Tothe47f30b2008-01-10 04:25:59 -03001148 cap->capabilities =
1149 V4L2_CAP_VIDEO_CAPTURE |
1150 V4L2_CAP_READWRITE |
1151 V4L2_CAP_STREAMING |
1152 V4L2_CAP_VBI_CAPTURE;
1153 if (UNSET != dev->tuner_type)
1154 cap->capabilities |= V4L2_CAP_TUNER;
1155 return 0;
1156}
1157
Hans Verkuil78b526a2008-05-28 12:16:41 -03001158static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001159 struct v4l2_fmtdesc *f)
1160{
1161 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1162 return -EINVAL;
1163
1164 strlcpy(f->description, formats[f->index].name,
1165 sizeof(f->description));
1166 f->pixelformat = formats[f->index].fourcc;
1167
1168 return 0;
1169}
1170
Steven Tothe47f30b2008-01-10 04:25:59 -03001171static int vidioc_reqbufs(struct file *file, void *priv,
1172 struct v4l2_requestbuffers *p)
1173{
1174 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001175 return videobuf_reqbufs(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001176}
1177
1178static int vidioc_querybuf(struct file *file, void *priv,
1179 struct v4l2_buffer *p)
1180{
1181 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001182 return videobuf_querybuf(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001183}
1184
1185static int vidioc_qbuf(struct file *file, void *priv,
1186 struct v4l2_buffer *p)
1187{
1188 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001189 return videobuf_qbuf(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001190}
1191
1192static int vidioc_dqbuf(struct file *file, void *priv,
1193 struct v4l2_buffer *p)
1194{
1195 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001196 return videobuf_dqbuf(get_queue(fh), p,
1197 file->f_flags & O_NONBLOCK);
Steven Tothe47f30b2008-01-10 04:25:59 -03001198}
1199
1200static int vidioc_streamon(struct file *file, void *priv,
1201 enum v4l2_buf_type i)
1202{
1203 struct cx23885_fh *fh = priv;
1204 struct cx23885_dev *dev = fh->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001205 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001206
Steven Toth5ab27e62011-10-10 11:09:54 -03001207 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1208 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
Steven Tothe47f30b2008-01-10 04:25:59 -03001209 return -EINVAL;
1210 if (unlikely(i != fh->type))
1211 return -EINVAL;
1212
1213 if (unlikely(!res_get(dev, fh, get_resource(fh))))
1214 return -EBUSY;
Steven Toth24465b42011-10-10 11:09:54 -03001215
1216 /* Don't start VBI streaming unless vida streaming
1217 * has already started.
1218 */
1219 if ((fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) &&
1220 ((cx_read(VID_A_DMA_CTL) & 0x11) == 0))
1221 return -EINVAL;
1222
Steven Tothe47f30b2008-01-10 04:25:59 -03001223 return videobuf_streamon(get_queue(fh));
1224}
1225
1226static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1227{
1228 struct cx23885_fh *fh = priv;
1229 struct cx23885_dev *dev = fh->dev;
1230 int err, res;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001231 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001232
Steven Toth5ab27e62011-10-10 11:09:54 -03001233 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1234 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
Steven Tothe47f30b2008-01-10 04:25:59 -03001235 return -EINVAL;
1236 if (i != fh->type)
1237 return -EINVAL;
1238
1239 res = get_resource(fh);
1240 err = videobuf_streamoff(get_queue(fh));
1241 if (err < 0)
1242 return err;
1243 res_free(dev, fh, res);
1244 return 0;
1245}
1246
Steven Toth35045132012-01-04 21:08:35 -03001247static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
1248{
1249 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1250 dprintk(1, "%s()\n", __func__);
1251
1252 call_all(dev, core, g_std, id);
1253
1254 return 0;
1255}
1256
Steven Tothe47f30b2008-01-10 04:25:59 -03001257static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1258{
1259 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001260 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001261
1262 mutex_lock(&dev->lock);
1263 cx23885_set_tvnorm(dev, *tvnorms);
1264 mutex_unlock(&dev->lock);
1265
1266 return 0;
1267}
1268
Steven Toth35045132012-01-04 21:08:35 -03001269int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
Steven Tothe47f30b2008-01-10 04:25:59 -03001270{
1271 static const char *iname[] = {
1272 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1273 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1274 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1275 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1276 [CX23885_VMUX_SVIDEO] = "S-Video",
David T.L. Wongdac65fa2009-10-21 11:07:24 -03001277 [CX23885_VMUX_COMPONENT] = "Component",
Steven Tothe47f30b2008-01-10 04:25:59 -03001278 [CX23885_VMUX_TELEVISION] = "Television",
1279 [CX23885_VMUX_CABLE] = "Cable TV",
1280 [CX23885_VMUX_DVB] = "DVB",
1281 [CX23885_VMUX_DEBUG] = "for debug only",
1282 };
1283 unsigned int n;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001284 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001285
1286 n = i->index;
Steven Tothe92bcf82011-10-10 11:09:55 -03001287 if (n >= MAX_CX23885_INPUT)
Steven Tothe47f30b2008-01-10 04:25:59 -03001288 return -EINVAL;
1289
1290 if (0 == INPUT(n)->type)
1291 return -EINVAL;
1292
Steven Tothe47f30b2008-01-10 04:25:59 -03001293 i->index = n;
1294 i->type = V4L2_INPUT_TYPE_CAMERA;
1295 strcpy(i->name, iname[INPUT(n)->type]);
1296 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
Julia Lawall473d8022010-08-05 17:22:44 -03001297 (CX23885_VMUX_CABLE == INPUT(n)->type)) {
Steven Tothe47f30b2008-01-10 04:25:59 -03001298 i->type = V4L2_INPUT_TYPE_TUNER;
1299 i->std = CX23885_NORMS;
Julia Lawall473d8022010-08-05 17:22:44 -03001300 }
Steven Toth6c6f52f2011-10-10 11:09:56 -03001301
1302 /* Two selectable audio inputs for non-tv inputs */
1303 if (INPUT(n)->type != CX23885_VMUX_TELEVISION)
1304 i->audioset = 0x3;
1305
Steven Tothc147f612012-01-06 11:55:32 -03001306 if (dev->input == n) {
1307 /* enum'd input matches our configured input.
1308 * Ask the video decoder to process the call
1309 * and give it an oppertunity to update the
1310 * status field.
1311 */
1312 call_all(dev, video, g_input_status, &i->status);
1313 }
1314
Steven Tothe47f30b2008-01-10 04:25:59 -03001315 return 0;
1316}
Steven Tothe47f30b2008-01-10 04:25:59 -03001317
1318static int vidioc_enum_input(struct file *file, void *priv,
1319 struct v4l2_input *i)
1320{
1321 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001322 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001323 return cx23885_enum_input(dev, i);
1324}
1325
Steven Toth35045132012-01-04 21:08:35 -03001326int cx23885_get_input(struct file *file, void *priv, unsigned int *i)
Steven Tothe47f30b2008-01-10 04:25:59 -03001327{
1328 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1329
1330 *i = dev->input;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001331 dprintk(1, "%s() returns %d\n", __func__, *i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001332 return 0;
1333}
1334
Steven Toth35045132012-01-04 21:08:35 -03001335static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1336{
1337 return cx23885_get_input(file, priv, i);
1338}
1339
1340int cx23885_set_input(struct file *file, void *priv, unsigned int i)
Steven Tothe47f30b2008-01-10 04:25:59 -03001341{
1342 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1343
Harvey Harrison22b4e642008-04-08 23:20:00 -03001344 dprintk(1, "%s(%d)\n", __func__, i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001345
Steven Tothe92bcf82011-10-10 11:09:55 -03001346 if (i >= MAX_CX23885_INPUT) {
Harvey Harrison22b4e642008-04-08 23:20:00 -03001347 dprintk(1, "%s() -EINVAL\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001348 return -EINVAL;
1349 }
1350
Steven Tothe92bcf82011-10-10 11:09:55 -03001351 if (INPUT(i)->type == 0)
1352 return -EINVAL;
1353
Steven Tothe47f30b2008-01-10 04:25:59 -03001354 mutex_lock(&dev->lock);
1355 cx23885_video_mux(dev, i);
Steven Tothfa1e0fd2011-10-10 11:09:56 -03001356
1357 /* By default establish the default audio input for the card also */
1358 /* Caller is free to use VIDIOC_S_AUDIO to override afterwards */
1359 cx23885_audio_mux(dev, i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001360 mutex_unlock(&dev->lock);
1361 return 0;
1362}
1363
Steven Toth35045132012-01-04 21:08:35 -03001364static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1365{
1366 return cx23885_set_input(file, priv, i);
1367}
1368
Andy Wallse9e5cf42010-07-18 18:18:06 -03001369static int vidioc_log_status(struct file *file, void *priv)
1370{
1371 struct cx23885_fh *fh = priv;
1372 struct cx23885_dev *dev = fh->dev;
1373
1374 printk(KERN_INFO
1375 "%s/0: ============ START LOG STATUS ============\n",
Steven Toth35045132012-01-04 21:08:35 -03001376 dev->name);
Andy Wallse9e5cf42010-07-18 18:18:06 -03001377 call_all(dev, core, log_status);
1378 printk(KERN_INFO
1379 "%s/0: ============= END LOG STATUS =============\n",
Steven Toth35045132012-01-04 21:08:35 -03001380 dev->name);
Andy Wallse9e5cf42010-07-18 18:18:06 -03001381 return 0;
1382}
1383
Steven Tothfc1a8892011-10-10 11:09:56 -03001384static int cx23885_query_audinput(struct file *file, void *priv,
1385 struct v4l2_audio *i)
1386{
1387 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1388 static const char *iname[] = {
1389 [0] = "Baseband L/R 1",
1390 [1] = "Baseband L/R 2",
1391 };
1392 unsigned int n;
1393 dprintk(1, "%s()\n", __func__);
1394
1395 n = i->index;
1396 if (n >= 2)
1397 return -EINVAL;
1398
1399 memset(i, 0, sizeof(*i));
1400 i->index = n;
1401 strcpy(i->name, iname[n]);
1402 i->capability = V4L2_AUDCAP_STEREO;
1403 i->mode = V4L2_AUDMODE_AVL;
1404 return 0;
1405
1406}
1407
1408static int vidioc_enum_audinput(struct file *file, void *priv,
1409 struct v4l2_audio *i)
1410{
1411 return cx23885_query_audinput(file, priv, i);
1412}
1413
1414static int vidioc_g_audinput(struct file *file, void *priv,
1415 struct v4l2_audio *i)
1416{
1417 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1418
1419 i->index = dev->audinput;
1420 dprintk(1, "%s(input=%d)\n", __func__, i->index);
1421
1422 return cx23885_query_audinput(file, priv, i);
1423}
1424
1425static int vidioc_s_audinput(struct file *file, void *priv,
1426 struct v4l2_audio *i)
1427{
1428 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Steven Tothfc1a8892011-10-10 11:09:56 -03001429 if (i->index >= 2)
1430 return -EINVAL;
1431
1432 dprintk(1, "%s(%d)\n", __func__, i->index);
1433
Steven Tothfc1a8892011-10-10 11:09:56 -03001434 dev->audinput = i->index;
Steven Tothfa1e0fd2011-10-10 11:09:56 -03001435
1436 /* Skip the audio defaults from the cards struct, caller wants
1437 * directly touch the audio mux hardware. */
1438 cx23885_flatiron_mux(dev, dev->audinput + 1);
Steven Tothfc1a8892011-10-10 11:09:56 -03001439 return 0;
1440}
1441
Steven Tothe47f30b2008-01-10 04:25:59 -03001442static int vidioc_queryctrl(struct file *file, void *priv,
1443 struct v4l2_queryctrl *qctrl)
1444{
1445 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1446 if (unlikely(qctrl->id == 0))
1447 return -EINVAL;
1448 return cx23885_ctrl_query(qctrl);
1449}
1450
1451static int vidioc_g_ctrl(struct file *file, void *priv,
1452 struct v4l2_control *ctl)
1453{
1454 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1455
1456 return cx23885_get_control(dev, ctl);
1457}
1458
1459static int vidioc_s_ctrl(struct file *file, void *priv,
1460 struct v4l2_control *ctl)
1461{
1462 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1463
1464 return cx23885_set_control(dev, ctl);
1465}
1466
1467static int vidioc_g_tuner(struct file *file, void *priv,
1468 struct v4l2_tuner *t)
1469{
1470 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1471
1472 if (unlikely(UNSET == dev->tuner_type))
1473 return -EINVAL;
1474 if (0 != t->index)
1475 return -EINVAL;
1476
1477 strcpy(t->name, "Television");
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001478
Steven Toth80f1e082011-10-10 11:09:53 -03001479 call_all(dev, tuner, g_tuner, t);
Steven Tothe47f30b2008-01-10 04:25:59 -03001480 return 0;
1481}
1482
1483static int vidioc_s_tuner(struct file *file, void *priv,
1484 struct v4l2_tuner *t)
1485{
1486 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1487
1488 if (UNSET == dev->tuner_type)
1489 return -EINVAL;
1490 if (0 != t->index)
1491 return -EINVAL;
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001492 /* Update the A/V core */
Steven Toth80f1e082011-10-10 11:09:53 -03001493 call_all(dev, tuner, s_tuner, t);
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001494
Steven Tothe47f30b2008-01-10 04:25:59 -03001495 return 0;
1496}
1497
1498static int vidioc_g_frequency(struct file *file, void *priv,
1499 struct v4l2_frequency *f)
1500{
1501 struct cx23885_fh *fh = priv;
1502 struct cx23885_dev *dev = fh->dev;
1503
1504 if (unlikely(UNSET == dev->tuner_type))
1505 return -EINVAL;
1506
1507 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1508 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1509 f->frequency = dev->freq;
1510
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001511 call_all(dev, tuner, g_frequency, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001512
1513 return 0;
1514}
1515
Hans Verkuild45b9b82008-09-04 03:33:43 -03001516static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
Steven Tothe47f30b2008-01-10 04:25:59 -03001517{
Steven Toth35045132012-01-04 21:08:35 -03001518 struct v4l2_control ctrl;
1519
Steven Tothe47f30b2008-01-10 04:25:59 -03001520 if (unlikely(UNSET == dev->tuner_type))
1521 return -EINVAL;
1522 if (unlikely(f->tuner != 0))
1523 return -EINVAL;
1524
1525 mutex_lock(&dev->lock);
1526 dev->freq = f->frequency;
1527
Steven Toth35045132012-01-04 21:08:35 -03001528 /* I need to mute audio here */
1529 ctrl.id = V4L2_CID_AUDIO_MUTE;
1530 ctrl.value = 1;
1531 cx23885_set_control(dev, &ctrl);
1532
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001533 call_all(dev, tuner, s_frequency, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001534
1535 /* When changing channels it is required to reset TVAUDIO */
Steven Toth35045132012-01-04 21:08:35 -03001536 msleep(100);
1537
1538 /* I need to unmute audio here */
1539 ctrl.value = 0;
1540 cx23885_set_control(dev, &ctrl);
Steven Tothe47f30b2008-01-10 04:25:59 -03001541
1542 mutex_unlock(&dev->lock);
1543
1544 return 0;
1545}
Steven Tothe47f30b2008-01-10 04:25:59 -03001546
Steven Toth35045132012-01-04 21:08:35 -03001547static int cx23885_set_freq_via_ops(struct cx23885_dev *dev,
1548 struct v4l2_frequency *f)
1549{
1550 struct v4l2_control ctrl;
1551 struct videobuf_dvb_frontend *vfe;
1552 struct dvb_frontend *fe;
1553 int err = 0;
1554
1555 struct analog_parameters params = {
1556 .mode = V4L2_TUNER_ANALOG_TV,
1557 .audmode = V4L2_TUNER_MODE_STEREO,
1558 .std = dev->tvnorm,
1559 .frequency = f->frequency
1560 };
1561
1562 mutex_lock(&dev->lock);
1563 dev->freq = f->frequency;
1564
1565 /* I need to mute audio here */
1566 ctrl.id = V4L2_CID_AUDIO_MUTE;
1567 ctrl.value = 1;
1568 cx23885_set_control(dev, &ctrl);
1569
1570 /* If HVR1850 */
1571 dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__,
1572 params.frequency, f->tuner, params.std);
1573
1574 vfe = videobuf_dvb_get_frontend(&dev->ts2.frontends, 1);
1575 if (!vfe)
1576 err = -EINVAL;
1577
1578 fe = vfe->dvb.frontend;
1579
1580 if (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850)
1581 fe = &dev->ts1.analog_fe;
1582
1583 if (fe && fe->ops.tuner_ops.set_analog_params) {
1584 call_all(dev, core, s_std, dev->tvnorm);
1585 fe->ops.tuner_ops.set_analog_params(fe, &params);
1586 }
1587 else
1588 printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
1589
1590 /* When changing channels it is required to reset TVAUDIO */
1591 msleep(100);
1592
1593 /* I need to unmute audio here */
1594 ctrl.value = 0;
1595 cx23885_set_control(dev, &ctrl);
1596
1597 mutex_unlock(&dev->lock);
1598
1599 return 0;
1600}
1601
1602int cx23885_set_frequency(struct file *file, void *priv,
1603 struct v4l2_frequency *f)
Steven Tothe47f30b2008-01-10 04:25:59 -03001604{
1605 struct cx23885_fh *fh = priv;
1606 struct cx23885_dev *dev = fh->dev;
Steven Toth35045132012-01-04 21:08:35 -03001607 int ret;
Steven Tothe47f30b2008-01-10 04:25:59 -03001608
Steven Toth35045132012-01-04 21:08:35 -03001609 switch (dev->board) {
1610 case CX23885_BOARD_HAUPPAUGE_HVR1850:
1611 ret = cx23885_set_freq_via_ops(dev, f);
1612 break;
1613 default:
1614 ret = cx23885_set_freq(dev, f);
1615 }
Steven Tothe47f30b2008-01-10 04:25:59 -03001616
Steven Toth35045132012-01-04 21:08:35 -03001617 return ret;
1618}
1619
1620static int vidioc_s_frequency(struct file *file, void *priv,
1621 struct v4l2_frequency *f)
1622{
1623 return cx23885_set_frequency(file, priv, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001624}
1625
Steven Tothe47f30b2008-01-10 04:25:59 -03001626/* ----------------------------------------------------------- */
Steven Tothe47f30b2008-01-10 04:25:59 -03001627
1628static void cx23885_vid_timeout(unsigned long data)
1629{
1630 struct cx23885_dev *dev = (struct cx23885_dev *)data;
1631 struct cx23885_dmaqueue *q = &dev->vidq;
1632 struct cx23885_buffer *buf;
1633 unsigned long flags;
1634
Steven Tothe47f30b2008-01-10 04:25:59 -03001635 spin_lock_irqsave(&dev->slock, flags);
1636 while (!list_empty(&q->active)) {
1637 buf = list_entry(q->active.next,
1638 struct cx23885_buffer, vb.queue);
1639 list_del(&buf->vb.queue);
1640 buf->vb.state = VIDEOBUF_ERROR;
1641 wake_up(&buf->vb.done);
Steven Toth79776c82011-10-10 11:09:54 -03001642 printk(KERN_ERR "%s: [%p/%d] timeout - dma=0x%08lx\n",
Steven Tothe47f30b2008-01-10 04:25:59 -03001643 dev->name, buf, buf->vb.i,
1644 (unsigned long)buf->risc.dma);
1645 }
1646 cx23885_restart_video_queue(dev, q);
1647 spin_unlock_irqrestore(&dev->slock, flags);
1648}
1649
1650int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1651{
1652 u32 mask, count;
1653 int handled = 0;
1654
1655 mask = cx_read(VID_A_INT_MSK);
1656 if (0 == (status & mask))
1657 return handled;
Steven Toth79776c82011-10-10 11:09:54 -03001658
Steven Tothe47f30b2008-01-10 04:25:59 -03001659 cx_write(VID_A_INT_STAT, status);
1660
Steven Toth19696f02011-10-10 11:09:56 -03001661 /* risc op code error, fifo overflow or line sync detection error */
Steven Toth79776c82011-10-10 11:09:54 -03001662 if ((status & VID_BC_MSK_OPC_ERR) ||
1663 (status & VID_BC_MSK_SYNC) ||
1664 (status & VID_BC_MSK_OF)) {
1665
Steven Toth19696f02011-10-10 11:09:56 -03001666 if (status & VID_BC_MSK_OPC_ERR) {
Steven Toth79776c82011-10-10 11:09:54 -03001667 dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1668 VID_BC_MSK_OPC_ERR);
Steven Toth19696f02011-10-10 11:09:56 -03001669 printk(KERN_WARNING "%s: video risc op code error\n",
1670 dev->name);
1671 cx23885_sram_channel_dump(dev,
1672 &dev->sram_channels[SRAM_CH01]);
1673 }
Steven Toth79776c82011-10-10 11:09:54 -03001674
1675 if (status & VID_BC_MSK_SYNC)
Steven Toth19696f02011-10-10 11:09:56 -03001676 dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) "
1677 "video lines miss-match\n",
Steven Toth79776c82011-10-10 11:09:54 -03001678 VID_BC_MSK_SYNC);
1679
1680 if (status & VID_BC_MSK_OF)
Steven Toth19696f02011-10-10 11:09:56 -03001681 dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n",
Steven Toth79776c82011-10-10 11:09:54 -03001682 VID_BC_MSK_OF);
1683
Steven Tothe47f30b2008-01-10 04:25:59 -03001684 }
1685
Steven Toth79776c82011-10-10 11:09:54 -03001686 /* Video */
1687 if (status & VID_BC_MSK_RISCI1) {
Steven Tothe47f30b2008-01-10 04:25:59 -03001688 spin_lock(&dev->slock);
1689 count = cx_read(VID_A_GPCNT);
1690 cx23885_video_wakeup(dev, &dev->vidq, count);
1691 spin_unlock(&dev->slock);
1692 handled++;
1693 }
Steven Toth79776c82011-10-10 11:09:54 -03001694 if (status & VID_BC_MSK_RISCI2) {
Steven Tothe47f30b2008-01-10 04:25:59 -03001695 dprintk(2, "stopper video\n");
1696 spin_lock(&dev->slock);
1697 cx23885_restart_video_queue(dev, &dev->vidq);
1698 spin_unlock(&dev->slock);
1699 handled++;
1700 }
1701
Steven Toth79776c82011-10-10 11:09:54 -03001702 /* Allow the VBI framework to process it's payload */
1703 handled += cx23885_vbi_irq(dev, status);
1704
Steven Tothe47f30b2008-01-10 04:25:59 -03001705 return handled;
1706}
1707
Steven Tothe47f30b2008-01-10 04:25:59 -03001708/* ----------------------------------------------------------- */
1709/* exported stuff */
1710
Hans Verkuilbec43662008-12-30 06:58:20 -03001711static const struct v4l2_file_operations video_fops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001712 .owner = THIS_MODULE,
1713 .open = video_open,
1714 .release = video_release,
1715 .read = video_read,
1716 .poll = video_poll,
1717 .mmap = video_mmap,
1718 .ioctl = video_ioctl2,
Steven Tothe47f30b2008-01-10 04:25:59 -03001719};
1720
Hans Verkuila3998102008-07-21 02:57:38 -03001721static const struct v4l2_ioctl_ops video_ioctl_ops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001722 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001723 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1724 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1725 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1726 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1727 .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
1728 .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
1729 .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
Steven Tothe47f30b2008-01-10 04:25:59 -03001730 .vidioc_reqbufs = vidioc_reqbufs,
1731 .vidioc_querybuf = vidioc_querybuf,
1732 .vidioc_qbuf = vidioc_qbuf,
1733 .vidioc_dqbuf = vidioc_dqbuf,
1734 .vidioc_s_std = vidioc_s_std,
Steven Toth35045132012-01-04 21:08:35 -03001735 .vidioc_g_std = vidioc_g_std,
1736 .vidioc_querystd = vidioc_g_std,
Steven Tothe47f30b2008-01-10 04:25:59 -03001737 .vidioc_enum_input = vidioc_enum_input,
1738 .vidioc_g_input = vidioc_g_input,
1739 .vidioc_s_input = vidioc_s_input,
Andy Wallse9e5cf42010-07-18 18:18:06 -03001740 .vidioc_log_status = vidioc_log_status,
Steven Tothe47f30b2008-01-10 04:25:59 -03001741 .vidioc_queryctrl = vidioc_queryctrl,
1742 .vidioc_g_ctrl = vidioc_g_ctrl,
1743 .vidioc_s_ctrl = vidioc_s_ctrl,
1744 .vidioc_streamon = vidioc_streamon,
1745 .vidioc_streamoff = vidioc_streamoff,
Steven Tothe47f30b2008-01-10 04:25:59 -03001746 .vidioc_g_tuner = vidioc_g_tuner,
1747 .vidioc_s_tuner = vidioc_s_tuner,
1748 .vidioc_g_frequency = vidioc_g_frequency,
1749 .vidioc_s_frequency = vidioc_s_frequency,
Andy Walls74618242009-09-26 22:50:44 -03001750 .vidioc_g_chip_ident = cx23885_g_chip_ident,
Steven Tothe47f30b2008-01-10 04:25:59 -03001751#ifdef CONFIG_VIDEO_ADV_DEBUG
Andy Walls74618242009-09-26 22:50:44 -03001752 .vidioc_g_register = cx23885_g_register,
1753 .vidioc_s_register = cx23885_s_register,
Steven Tothe47f30b2008-01-10 04:25:59 -03001754#endif
Steven Tothfc1a8892011-10-10 11:09:56 -03001755 .vidioc_enumaudio = vidioc_enum_audinput,
1756 .vidioc_g_audio = vidioc_g_audinput,
1757 .vidioc_s_audio = vidioc_s_audinput,
Hans Verkuila3998102008-07-21 02:57:38 -03001758};
1759
1760static struct video_device cx23885_vbi_template;
1761static struct video_device cx23885_video_template = {
1762 .name = "cx23885-video",
Hans Verkuila3998102008-07-21 02:57:38 -03001763 .fops = &video_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03001764 .ioctl_ops = &video_ioctl_ops,
Steven Tothe47f30b2008-01-10 04:25:59 -03001765 .tvnorms = CX23885_NORMS,
1766 .current_norm = V4L2_STD_NTSC_M,
1767};
1768
Hans Verkuilbec43662008-12-30 06:58:20 -03001769static const struct v4l2_file_operations radio_fops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001770 .owner = THIS_MODULE,
1771 .open = video_open,
1772 .release = video_release,
1773 .ioctl = video_ioctl2,
Steven Tothe47f30b2008-01-10 04:25:59 -03001774};
1775
1776
1777void cx23885_video_unregister(struct cx23885_dev *dev)
1778{
Harvey Harrison22b4e642008-04-08 23:20:00 -03001779 dprintk(1, "%s()\n", __func__);
Andy Wallsdbe83a32010-07-19 01:19:43 -03001780 cx23885_irq_remove(dev, 0x01);
Steven Tothe47f30b2008-01-10 04:25:59 -03001781
Steven Toth79776c82011-10-10 11:09:54 -03001782 if (dev->vbi_dev) {
1783 if (video_is_registered(dev->vbi_dev))
1784 video_unregister_device(dev->vbi_dev);
1785 else
1786 video_device_release(dev->vbi_dev);
1787 dev->vbi_dev = NULL;
1788 btcx_riscmem_free(dev->pci, &dev->vbiq.stopper);
1789 }
Steven Tothe47f30b2008-01-10 04:25:59 -03001790 if (dev->video_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001791 if (video_is_registered(dev->video_dev))
Steven Tothe47f30b2008-01-10 04:25:59 -03001792 video_unregister_device(dev->video_dev);
1793 else
1794 video_device_release(dev->video_dev);
1795 dev->video_dev = NULL;
1796
1797 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1798 }
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001799
1800 if (dev->audio_dev)
Steven Tothefa762f2011-10-10 11:09:53 -03001801 cx23885_audio_unregister(dev);
Steven Tothe47f30b2008-01-10 04:25:59 -03001802}
1803
1804int cx23885_video_register(struct cx23885_dev *dev)
1805{
1806 int err;
1807
Harvey Harrison22b4e642008-04-08 23:20:00 -03001808 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001809 spin_lock_init(&dev->slock);
1810
1811 /* Initialize VBI template */
1812 memcpy(&cx23885_vbi_template, &cx23885_video_template,
1813 sizeof(cx23885_vbi_template));
1814 strcpy(cx23885_vbi_template.name, "cx23885-vbi");
Steven Tothe47f30b2008-01-10 04:25:59 -03001815
1816 dev->tvnorm = cx23885_video_template.current_norm;
1817
1818 /* init video dma queues */
1819 INIT_LIST_HEAD(&dev->vidq.active);
1820 INIT_LIST_HEAD(&dev->vidq.queued);
1821 dev->vidq.timeout.function = cx23885_vid_timeout;
1822 dev->vidq.timeout.data = (unsigned long)dev;
1823 init_timer(&dev->vidq.timeout);
1824 cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1825 VID_A_DMA_CTL, 0x11, 0x00);
1826
Steven Toth79776c82011-10-10 11:09:54 -03001827 /* init vbi dma queues */
1828 INIT_LIST_HEAD(&dev->vbiq.active);
1829 INIT_LIST_HEAD(&dev->vbiq.queued);
1830 dev->vbiq.timeout.function = cx23885_vbi_timeout;
1831 dev->vbiq.timeout.data = (unsigned long)dev;
1832 init_timer(&dev->vbiq.timeout);
1833 cx23885_risc_stopper(dev->pci, &dev->vbiq.stopper,
1834 VID_A_DMA_CTL, 0x22, 0x00);
Andy Wallsdbe83a32010-07-19 01:19:43 -03001835
1836 cx23885_irq_add_enable(dev, 0x01);
Steven Tothe47f30b2008-01-10 04:25:59 -03001837
Igor M. Liplianin557f48d2011-01-25 17:05:00 -03001838 if ((TUNER_ABSENT != dev->tuner_type) &&
1839 ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001840 struct v4l2_subdev *sd = NULL;
1841
1842 if (dev->tuner_addr)
Hans Verkuile6574f22009-04-01 03:57:53 -03001843 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
Igor M. Liplianin557f48d2011-01-25 17:05:00 -03001844 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03001845 "tuner", dev->tuner_addr, NULL);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001846 else
Hans Verkuil53dacb12009-08-10 02:49:08 -03001847 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
Igor M. Liplianin557f48d2011-01-25 17:05:00 -03001848 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
Laurent Pinchart1532a072010-09-24 07:58:29 -03001849 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001850 if (sd) {
1851 struct tuner_setup tun_setup;
1852
David T.L. Wongfd705e72009-10-21 11:08:30 -03001853 memset(&tun_setup, 0, sizeof(tun_setup));
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001854 tun_setup.mode_mask = T_ANALOG_TV;
1855 tun_setup.type = dev->tuner_type;
1856 tun_setup.addr = v4l2_i2c_subdev_addr(sd);
David T.L. Wong6f0d8c02009-10-21 13:15:30 -03001857 tun_setup.tuner_callback = cx23885_tuner_callback;
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001858
1859 v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
Kusanagi Kouichi0b32d652010-01-22 04:55:28 -03001860
1861 if (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) {
1862 struct xc2028_ctrl ctrl = {
1863 .fname = XC2028_DEFAULT_FIRMWARE,
1864 .max_len = 64
1865 };
1866 struct v4l2_priv_tun_config cfg = {
1867 .tuner = dev->tuner_type,
1868 .priv = &ctrl
1869 };
1870 v4l2_subdev_call(sd, tuner, s_config, &cfg);
1871 }
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001872 }
1873 }
1874
Steven Toth79776c82011-10-10 11:09:54 -03001875 /* register Video device */
Steven Tothe47f30b2008-01-10 04:25:59 -03001876 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1877 &cx23885_video_template, "video");
1878 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1879 video_nr[dev->nr]);
1880 if (err < 0) {
1881 printk(KERN_INFO "%s: can't register video device\n",
1882 dev->name);
1883 goto fail_unreg;
1884 }
Steven Tothf88fb8e2011-10-10 11:09:54 -03001885 printk(KERN_INFO "%s: registered device %s [v4l2]\n",
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001886 dev->name, video_device_node_name(dev->video_dev));
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001887
Steven Tothf88fb8e2011-10-10 11:09:54 -03001888 /* register VBI device */
1889 dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
1890 &cx23885_vbi_template, "vbi");
1891 err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1892 vbi_nr[dev->nr]);
1893 if (err < 0) {
1894 printk(KERN_INFO "%s: can't register vbi device\n",
1895 dev->name);
1896 goto fail_unreg;
1897 }
1898 printk(KERN_INFO "%s: registered device %s\n",
1899 dev->name, video_device_node_name(dev->vbi_dev));
1900
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001901 /* Register ALSA audio device */
Steven Tothefa762f2011-10-10 11:09:53 -03001902 dev->audio_dev = cx23885_audio_register(dev);
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001903
Steven Tothe47f30b2008-01-10 04:25:59 -03001904 /* initial device configuration */
1905 mutex_lock(&dev->lock);
1906 cx23885_set_tvnorm(dev, dev->tvnorm);
1907 init_controls(dev);
1908 cx23885_video_mux(dev, 0);
Steven Tothfa1e0fd2011-10-10 11:09:56 -03001909 cx23885_audio_mux(dev, 0);
Steven Tothe47f30b2008-01-10 04:25:59 -03001910 mutex_unlock(&dev->lock);
1911
Steven Tothe47f30b2008-01-10 04:25:59 -03001912 return 0;
1913
1914fail_unreg:
1915 cx23885_video_unregister(dev);
1916 return err;
1917}
1918