blob: f13c40e653c19b0d1a00987543808d4a874d03c4 [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 Toth2cb9ccd2011-10-10 11:09:55 -0300505 if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) ||
506 (dev->board == CX23885_BOARD_MPX885)) {
Steven Toth33cdeb32011-10-10 11:09:55 -0300507 /* Configure audio routing */
508 v4l2_subdev_call(dev->sd_cx25840, audio, s_routing,
509 INPUT(input)->amux, 0, 0);
510
511 if (INPUT(input)->amux == CX25840_AUDIO7)
512 cx23885_flatiron_mux(dev, 1);
513 else if (INPUT(input)->amux == CX25840_AUDIO6)
514 cx23885_flatiron_mux(dev, 2);
515 }
516
Steven Tothe47f30b2008-01-10 04:25:59 -0300517 return 0;
518}
Steven Tothe47f30b2008-01-10 04:25:59 -0300519
520/* ------------------------------------------------------------------ */
Hans Verkuild45b9b82008-09-04 03:33:43 -0300521static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width,
Steven Tothe47f30b2008-01-10 04:25:59 -0300522 unsigned int height, enum v4l2_field field)
523{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300524 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300525 return 0;
526}
527
528static int cx23885_start_video_dma(struct cx23885_dev *dev,
529 struct cx23885_dmaqueue *q,
530 struct cx23885_buffer *buf)
531{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300532 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300533
534 /* setup fifo + format */
535 cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01],
536 buf->bpl, buf->risc.dma);
537 cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field);
538
539 /* reset counter */
540 cx_write(VID_A_GPCNT_CTL, 3);
541 q->count = 1;
542
543 /* enable irq */
Andy Wallsdbe83a32010-07-19 01:19:43 -0300544 cx23885_irq_add_enable(dev, 0x01);
Steven Tothe47f30b2008-01-10 04:25:59 -0300545 cx_set(VID_A_INT_MSK, 0x000011);
546
547 /* start dma */
548 cx_set(DEV_CNTRL2, (1<<5));
549 cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */
550
551 return 0;
552}
553
Steven Tothe47f30b2008-01-10 04:25:59 -0300554
555static int cx23885_restart_video_queue(struct cx23885_dev *dev,
556 struct cx23885_dmaqueue *q)
557{
558 struct cx23885_buffer *buf, *prev;
559 struct list_head *item;
Harvey Harrison22b4e642008-04-08 23:20:00 -0300560 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300561
562 if (!list_empty(&q->active)) {
563 buf = list_entry(q->active.next, struct cx23885_buffer,
564 vb.queue);
565 dprintk(2, "restart_queue [%p/%d]: restart dma\n",
566 buf, buf->vb.i);
567 cx23885_start_video_dma(dev, q, buf);
568 list_for_each(item, &q->active) {
569 buf = list_entry(item, struct cx23885_buffer,
570 vb.queue);
571 buf->count = q->count++;
572 }
573 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
574 return 0;
575 }
576
577 prev = NULL;
578 for (;;) {
579 if (list_empty(&q->queued))
580 return 0;
581 buf = list_entry(q->queued.next, struct cx23885_buffer,
582 vb.queue);
583 if (NULL == prev) {
584 list_move_tail(&buf->vb.queue, &q->active);
585 cx23885_start_video_dma(dev, q, buf);
586 buf->vb.state = VIDEOBUF_ACTIVE;
587 buf->count = q->count++;
588 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
589 dprintk(2, "[%p/%d] restart_queue - first active\n",
590 buf, buf->vb.i);
591
592 } else if (prev->vb.width == buf->vb.width &&
593 prev->vb.height == buf->vb.height &&
594 prev->fmt == buf->fmt) {
595 list_move_tail(&buf->vb.queue, &q->active);
596 buf->vb.state = VIDEOBUF_ACTIVE;
597 buf->count = q->count++;
598 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
599 prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */
600 dprintk(2, "[%p/%d] restart_queue - move to active\n",
601 buf, buf->vb.i);
602 } else {
603 return 0;
604 }
605 prev = buf;
606 }
607}
608
609static int buffer_setup(struct videobuf_queue *q, unsigned int *count,
610 unsigned int *size)
611{
612 struct cx23885_fh *fh = q->priv_data;
613
614 *size = fh->fmt->depth*fh->width*fh->height >> 3;
615 if (0 == *count)
616 *count = 32;
Andreas Bombedab7e312010-03-21 16:02:45 -0300617 if (*size * *count > vid_limit * 1024 * 1024)
618 *count = (vid_limit * 1024 * 1024) / *size;
Steven Tothe47f30b2008-01-10 04:25:59 -0300619 return 0;
620}
621
622static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
623 enum v4l2_field field)
624{
625 struct cx23885_fh *fh = q->priv_data;
626 struct cx23885_dev *dev = fh->dev;
627 struct cx23885_buffer *buf =
628 container_of(vb, struct cx23885_buffer, vb);
629 int rc, init_buffer = 0;
630 u32 line0_offset, line1_offset;
631 struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb);
632
633 BUG_ON(NULL == fh->fmt);
634 if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) ||
635 fh->height < 32 || fh->height > norm_maxh(dev->tvnorm))
636 return -EINVAL;
637 buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3;
638 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
639 return -EINVAL;
640
641 if (buf->fmt != fh->fmt ||
642 buf->vb.width != fh->width ||
643 buf->vb.height != fh->height ||
644 buf->vb.field != field) {
645 buf->fmt = fh->fmt;
646 buf->vb.width = fh->width;
647 buf->vb.height = fh->height;
648 buf->vb.field = field;
649 init_buffer = 1;
650 }
651
652 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
653 init_buffer = 1;
654 rc = videobuf_iolock(q, &buf->vb, NULL);
655 if (0 != rc)
656 goto fail;
657 }
658
659 if (init_buffer) {
660 buf->bpl = buf->vb.width * buf->fmt->depth >> 3;
661 switch (buf->vb.field) {
662 case V4L2_FIELD_TOP:
663 cx23885_risc_buffer(dev->pci, &buf->risc,
664 dma->sglist, 0, UNSET,
665 buf->bpl, 0, buf->vb.height);
666 break;
667 case V4L2_FIELD_BOTTOM:
668 cx23885_risc_buffer(dev->pci, &buf->risc,
669 dma->sglist, UNSET, 0,
670 buf->bpl, 0, buf->vb.height);
671 break;
672 case V4L2_FIELD_INTERLACED:
673 if (dev->tvnorm & V4L2_STD_NTSC) {
674 /* cx25840 transmits NTSC bottom field first */
675 dprintk(1, "%s() Creating NTSC risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300676 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300677 line0_offset = buf->bpl;
678 line1_offset = 0;
679 } else {
680 /* All other formats are top field first */
681 dprintk(1, "%s() Creating PAL/SECAM risc\n",
Harvey Harrison22b4e642008-04-08 23:20:00 -0300682 __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -0300683 line0_offset = 0;
684 line1_offset = buf->bpl;
685 }
686 cx23885_risc_buffer(dev->pci, &buf->risc,
687 dma->sglist, line0_offset,
688 line1_offset,
689 buf->bpl, buf->bpl,
690 buf->vb.height >> 1);
691 break;
692 case V4L2_FIELD_SEQ_TB:
693 cx23885_risc_buffer(dev->pci, &buf->risc,
694 dma->sglist,
695 0, buf->bpl * (buf->vb.height >> 1),
696 buf->bpl, 0,
697 buf->vb.height >> 1);
698 break;
699 case V4L2_FIELD_SEQ_BT:
700 cx23885_risc_buffer(dev->pci, &buf->risc,
701 dma->sglist,
702 buf->bpl * (buf->vb.height >> 1), 0,
703 buf->bpl, 0,
704 buf->vb.height >> 1);
705 break;
706 default:
707 BUG();
708 }
709 }
710 dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n",
711 buf, buf->vb.i,
712 fh->width, fh->height, fh->fmt->depth, fh->fmt->name,
713 (unsigned long)buf->risc.dma);
714
715 buf->vb.state = VIDEOBUF_PREPARED;
716 return 0;
717
718 fail:
719 cx23885_free_buffer(q, buf);
720 return rc;
721}
722
723static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
724{
725 struct cx23885_buffer *buf = container_of(vb,
726 struct cx23885_buffer, vb);
727 struct cx23885_buffer *prev;
728 struct cx23885_fh *fh = vq->priv_data;
729 struct cx23885_dev *dev = fh->dev;
730 struct cx23885_dmaqueue *q = &dev->vidq;
731
732 /* add jump to stopper */
733 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
734 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
735 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
736
737 if (!list_empty(&q->queued)) {
738 list_add_tail(&buf->vb.queue, &q->queued);
739 buf->vb.state = VIDEOBUF_QUEUED;
740 dprintk(2, "[%p/%d] buffer_queue - append to queued\n",
741 buf, buf->vb.i);
742
743 } else if (list_empty(&q->active)) {
744 list_add_tail(&buf->vb.queue, &q->active);
745 cx23885_start_video_dma(dev, q, buf);
746 buf->vb.state = VIDEOBUF_ACTIVE;
747 buf->count = q->count++;
748 mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT);
749 dprintk(2, "[%p/%d] buffer_queue - first active\n",
750 buf, buf->vb.i);
751
752 } else {
753 prev = list_entry(q->active.prev, struct cx23885_buffer,
754 vb.queue);
755 if (prev->vb.width == buf->vb.width &&
756 prev->vb.height == buf->vb.height &&
757 prev->fmt == buf->fmt) {
758 list_add_tail(&buf->vb.queue, &q->active);
759 buf->vb.state = VIDEOBUF_ACTIVE;
760 buf->count = q->count++;
761 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
762 /* 64 bit bits 63-32 */
763 prev->risc.jmp[2] = cpu_to_le32(0);
764 dprintk(2, "[%p/%d] buffer_queue - append to active\n",
765 buf, buf->vb.i);
766
767 } else {
768 list_add_tail(&buf->vb.queue, &q->queued);
769 buf->vb.state = VIDEOBUF_QUEUED;
770 dprintk(2, "[%p/%d] buffer_queue - first queued\n",
771 buf, buf->vb.i);
772 }
773 }
774}
775
776static void buffer_release(struct videobuf_queue *q,
777 struct videobuf_buffer *vb)
778{
779 struct cx23885_buffer *buf = container_of(vb,
780 struct cx23885_buffer, vb);
781
782 cx23885_free_buffer(q, buf);
783}
784
785static struct videobuf_queue_ops cx23885_video_qops = {
786 .buf_setup = buffer_setup,
787 .buf_prepare = buffer_prepare,
788 .buf_queue = buffer_queue,
789 .buf_release = buffer_release,
790};
791
792static struct videobuf_queue *get_queue(struct cx23885_fh *fh)
793{
794 switch (fh->type) {
795 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
796 return &fh->vidq;
797 case V4L2_BUF_TYPE_VBI_CAPTURE:
798 return &fh->vbiq;
799 default:
800 BUG();
801 return NULL;
802 }
803}
804
805static int get_resource(struct cx23885_fh *fh)
806{
807 switch (fh->type) {
808 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
809 return RESOURCE_VIDEO;
810 case V4L2_BUF_TYPE_VBI_CAPTURE:
811 return RESOURCE_VBI;
812 default:
813 BUG();
814 return 0;
815 }
816}
817
Hans Verkuilbec43662008-12-30 06:58:20 -0300818static int video_open(struct file *file)
Steven Tothe47f30b2008-01-10 04:25:59 -0300819{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200820 struct video_device *vdev = video_devdata(file);
821 struct cx23885_dev *dev = video_drvdata(file);
Steven Tothe47f30b2008-01-10 04:25:59 -0300822 struct cx23885_fh *fh;
Steven Tothe47f30b2008-01-10 04:25:59 -0300823 enum v4l2_buf_type type = 0;
824 int radio = 0;
825
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200826 switch (vdev->vfl_type) {
827 case VFL_TYPE_GRABBER:
828 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
829 break;
830 case VFL_TYPE_VBI:
831 type = V4L2_BUF_TYPE_VBI_CAPTURE;
832 break;
833 case VFL_TYPE_RADIO:
834 radio = 1;
835 break;
Hans Verkuild56dc612008-07-30 08:43:36 -0300836 }
Steven Tothe47f30b2008-01-10 04:25:59 -0300837
Laurent Pinchart50462eb2009-12-10 11:47:13 -0200838 dprintk(1, "open dev=%s radio=%d type=%s\n",
839 video_device_node_name(vdev), radio, v4l2_type_names[type]);
Steven Tothe47f30b2008-01-10 04:25:59 -0300840
841 /* allocate + initialize per filehandle data */
842 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200843 if (NULL == fh)
Steven Tothe47f30b2008-01-10 04:25:59 -0300844 return -ENOMEM;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200845
Steven Tothe47f30b2008-01-10 04:25:59 -0300846 file->private_data = fh;
847 fh->dev = dev;
848 fh->radio = radio;
849 fh->type = type;
850 fh->width = 320;
851 fh->height = 240;
Steven Tothaf76e9f2011-10-10 11:09:54 -0300852 fh->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV);
Steven Tothe47f30b2008-01-10 04:25:59 -0300853
Guennadi Liakhovetski07051352008-04-22 14:42:13 -0300854 videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops,
855 &dev->pci->dev, &dev->slock,
Steven Tothe47f30b2008-01-10 04:25:59 -0300856 V4L2_BUF_TYPE_VIDEO_CAPTURE,
857 V4L2_FIELD_INTERLACED,
858 sizeof(struct cx23885_buffer),
Hans Verkuil08bff032010-09-20 17:39:46 -0300859 fh, NULL);
Steven Tothe47f30b2008-01-10 04:25:59 -0300860
Steven Toth79776c82011-10-10 11:09:54 -0300861 videobuf_queue_sg_init(&fh->vbiq, &cx23885_vbi_qops,
862 &dev->pci->dev, &dev->slock,
863 V4L2_BUF_TYPE_VBI_CAPTURE,
864 V4L2_FIELD_SEQ_TB,
865 sizeof(struct cx23885_buffer),
866 fh, NULL);
867
868
Steven Tothe47f30b2008-01-10 04:25:59 -0300869 dprintk(1, "post videobuf_queue_init()\n");
870
Steven Tothe47f30b2008-01-10 04:25:59 -0300871 return 0;
872}
873
874static ssize_t video_read(struct file *file, char __user *data,
875 size_t count, loff_t *ppos)
876{
877 struct cx23885_fh *fh = file->private_data;
878
879 switch (fh->type) {
880 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
881 if (res_locked(fh->dev, RESOURCE_VIDEO))
882 return -EBUSY;
883 return videobuf_read_one(&fh->vidq, data, count, ppos,
884 file->f_flags & O_NONBLOCK);
885 case V4L2_BUF_TYPE_VBI_CAPTURE:
886 if (!res_get(fh->dev, fh, RESOURCE_VBI))
887 return -EBUSY;
888 return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1,
889 file->f_flags & O_NONBLOCK);
890 default:
891 BUG();
892 return 0;
893 }
894}
895
896static unsigned int video_poll(struct file *file,
897 struct poll_table_struct *wait)
898{
899 struct cx23885_fh *fh = file->private_data;
900 struct cx23885_buffer *buf;
Figo.zhang9fd64182009-06-16 13:31:29 -0300901 unsigned int rc = POLLERR;
Steven Tothe47f30b2008-01-10 04:25:59 -0300902
903 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
904 if (!res_get(fh->dev, fh, RESOURCE_VBI))
905 return POLLERR;
906 return videobuf_poll_stream(file, &fh->vbiq, wait);
907 }
908
Figo.zhang9fd64182009-06-16 13:31:29 -0300909 mutex_lock(&fh->vidq.vb_lock);
Steven Tothe47f30b2008-01-10 04:25:59 -0300910 if (res_check(fh, RESOURCE_VIDEO)) {
911 /* streaming capture */
912 if (list_empty(&fh->vidq.stream))
Figo.zhang9fd64182009-06-16 13:31:29 -0300913 goto done;
Steven Tothe47f30b2008-01-10 04:25:59 -0300914 buf = list_entry(fh->vidq.stream.next,
915 struct cx23885_buffer, vb.stream);
916 } else {
917 /* read() capture */
918 buf = (struct cx23885_buffer *)fh->vidq.read_buf;
919 if (NULL == buf)
Figo.zhang9fd64182009-06-16 13:31:29 -0300920 goto done;
Steven Tothe47f30b2008-01-10 04:25:59 -0300921 }
922 poll_wait(file, &buf->vb.done, wait);
923 if (buf->vb.state == VIDEOBUF_DONE ||
924 buf->vb.state == VIDEOBUF_ERROR)
Figo.zhang9fd64182009-06-16 13:31:29 -0300925 rc = POLLIN|POLLRDNORM;
926 else
927 rc = 0;
928done:
929 mutex_unlock(&fh->vidq.vb_lock);
930 return rc;
Steven Tothe47f30b2008-01-10 04:25:59 -0300931}
932
Hans Verkuilbec43662008-12-30 06:58:20 -0300933static int video_release(struct file *file)
Steven Tothe47f30b2008-01-10 04:25:59 -0300934{
935 struct cx23885_fh *fh = file->private_data;
936 struct cx23885_dev *dev = fh->dev;
937
938 /* turn off overlay */
939 if (res_check(fh, RESOURCE_OVERLAY)) {
940 /* FIXME */
941 res_free(dev, fh, RESOURCE_OVERLAY);
942 }
943
944 /* stop video capture */
945 if (res_check(fh, RESOURCE_VIDEO)) {
946 videobuf_queue_cancel(&fh->vidq);
947 res_free(dev, fh, RESOURCE_VIDEO);
948 }
949 if (fh->vidq.read_buf) {
950 buffer_release(&fh->vidq, fh->vidq.read_buf);
951 kfree(fh->vidq.read_buf);
952 }
953
954 /* stop vbi capture */
955 if (res_check(fh, RESOURCE_VBI)) {
956 if (fh->vbiq.streaming)
957 videobuf_streamoff(&fh->vbiq);
958 if (fh->vbiq.reading)
959 videobuf_read_stop(&fh->vbiq);
960 res_free(dev, fh, RESOURCE_VBI);
961 }
962
963 videobuf_mmap_free(&fh->vidq);
964 file->private_data = NULL;
965 kfree(fh);
966
Steven Totha19602f22008-01-10 11:43:18 -0300967 /* We are not putting the tuner to sleep here on exit, because
968 * we want to use the mpeg encoder in another session to capture
969 * tuner video. Closing this will result in no video to the encoder.
970 */
Steven Tothe47f30b2008-01-10 04:25:59 -0300971
972 return 0;
973}
974
975static int video_mmap(struct file *file, struct vm_area_struct *vma)
976{
977 struct cx23885_fh *fh = file->private_data;
978
979 return videobuf_mmap_mapper(get_queue(fh), vma);
980}
981
982/* ------------------------------------------------------------------ */
983/* VIDEO CTRL IOCTLS */
984
Steven Toth9c8ced52008-10-16 20:18:44 -0300985static int cx23885_get_control(struct cx23885_dev *dev,
986 struct v4l2_control *ctl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300987{
Harvey Harrison22b4e642008-04-08 23:20:00 -0300988 dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -0300989 call_all(dev, core, g_ctrl, ctl);
Steven Tothe47f30b2008-01-10 04:25:59 -0300990 return 0;
991}
Steven Tothe47f30b2008-01-10 04:25:59 -0300992
Steven Toth9c8ced52008-10-16 20:18:44 -0300993static int cx23885_set_control(struct cx23885_dev *dev,
994 struct v4l2_control *ctl)
Steven Tothe47f30b2008-01-10 04:25:59 -0300995{
Mijhail Moreyra97ce5672011-10-10 11:09:53 -0300996 dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)\n", __func__);
997 call_all(dev, core, s_ctrl, ctl);
998
Steven Tothe47f30b2008-01-10 04:25:59 -0300999 return 0;
1000}
Steven Tothe47f30b2008-01-10 04:25:59 -03001001
1002static void init_controls(struct cx23885_dev *dev)
1003{
1004 struct v4l2_control ctrl;
1005 int i;
1006
1007 for (i = 0; i < CX23885_CTLS; i++) {
1008 ctrl.id = cx23885_ctls[i].v.id;
1009 ctrl.value = cx23885_ctls[i].v.default_value;
1010
1011 cx23885_set_control(dev, &ctrl);
1012 }
1013}
1014
1015/* ------------------------------------------------------------------ */
1016/* VIDEO IOCTLS */
1017
Hans Verkuil78b526a2008-05-28 12:16:41 -03001018static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001019 struct v4l2_format *f)
1020{
1021 struct cx23885_fh *fh = priv;
1022
1023 f->fmt.pix.width = fh->width;
1024 f->fmt.pix.height = fh->height;
1025 f->fmt.pix.field = fh->vidq.field;
1026 f->fmt.pix.pixelformat = fh->fmt->fourcc;
1027 f->fmt.pix.bytesperline =
1028 (f->fmt.pix.width * fh->fmt->depth) >> 3;
1029 f->fmt.pix.sizeimage =
1030 f->fmt.pix.height * f->fmt.pix.bytesperline;
1031
1032 return 0;
1033}
1034
Hans Verkuil78b526a2008-05-28 12:16:41 -03001035static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001036 struct v4l2_format *f)
1037{
1038 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1039 struct cx23885_fmt *fmt;
1040 enum v4l2_field field;
1041 unsigned int maxw, maxh;
1042
1043 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1044 if (NULL == fmt)
1045 return -EINVAL;
1046
1047 field = f->fmt.pix.field;
1048 maxw = norm_maxw(dev->tvnorm);
1049 maxh = norm_maxh(dev->tvnorm);
1050
1051 if (V4L2_FIELD_ANY == field) {
1052 field = (f->fmt.pix.height > maxh/2)
1053 ? V4L2_FIELD_INTERLACED
1054 : V4L2_FIELD_BOTTOM;
1055 }
1056
1057 switch (field) {
1058 case V4L2_FIELD_TOP:
1059 case V4L2_FIELD_BOTTOM:
1060 maxh = maxh / 2;
1061 break;
1062 case V4L2_FIELD_INTERLACED:
1063 break;
1064 default:
1065 return -EINVAL;
1066 }
1067
1068 f->fmt.pix.field = field;
Trent Piepho2449afc2009-05-30 21:45:46 -03001069 v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2,
1070 &f->fmt.pix.height, 32, maxh, 0, 0);
Steven Tothe47f30b2008-01-10 04:25:59 -03001071 f->fmt.pix.bytesperline =
1072 (f->fmt.pix.width * fmt->depth) >> 3;
1073 f->fmt.pix.sizeimage =
1074 f->fmt.pix.height * f->fmt.pix.bytesperline;
1075
1076 return 0;
1077}
1078
Hans Verkuil78b526a2008-05-28 12:16:41 -03001079static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001080 struct v4l2_format *f)
1081{
1082 struct cx23885_fh *fh = priv;
1083 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Hans Verkuilcc991132010-05-09 10:09:28 -03001084 struct v4l2_mbus_framefmt mbus_fmt;
Steven Tothe47f30b2008-01-10 04:25:59 -03001085 int err;
1086
Harvey Harrison22b4e642008-04-08 23:20:00 -03001087 dprintk(2, "%s()\n", __func__);
Hans Verkuil78b526a2008-05-28 12:16:41 -03001088 err = vidioc_try_fmt_vid_cap(file, priv, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001089
1090 if (0 != err)
1091 return err;
1092 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1093 fh->width = f->fmt.pix.width;
1094 fh->height = f->fmt.pix.height;
1095 fh->vidq.field = f->fmt.pix.field;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001096 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__,
Steven Tothe47f30b2008-01-10 04:25:59 -03001097 fh->width, fh->height, fh->vidq.field);
Hans Verkuilcc991132010-05-09 10:09:28 -03001098 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
1099 call_all(dev, video, s_mbus_fmt, &mbus_fmt);
1100 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
Steven Tothe47f30b2008-01-10 04:25:59 -03001101 return 0;
1102}
1103
1104static int vidioc_querycap(struct file *file, void *priv,
1105 struct v4l2_capability *cap)
1106{
1107 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1108
1109 strcpy(cap->driver, "cx23885");
1110 strlcpy(cap->card, cx23885_boards[dev->board].name,
1111 sizeof(cap->card));
1112 sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci));
Steven Tothe47f30b2008-01-10 04:25:59 -03001113 cap->capabilities =
1114 V4L2_CAP_VIDEO_CAPTURE |
1115 V4L2_CAP_READWRITE |
1116 V4L2_CAP_STREAMING |
1117 V4L2_CAP_VBI_CAPTURE;
1118 if (UNSET != dev->tuner_type)
1119 cap->capabilities |= V4L2_CAP_TUNER;
1120 return 0;
1121}
1122
Hans Verkuil78b526a2008-05-28 12:16:41 -03001123static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
Steven Tothe47f30b2008-01-10 04:25:59 -03001124 struct v4l2_fmtdesc *f)
1125{
1126 if (unlikely(f->index >= ARRAY_SIZE(formats)))
1127 return -EINVAL;
1128
1129 strlcpy(f->description, formats[f->index].name,
1130 sizeof(f->description));
1131 f->pixelformat = formats[f->index].fourcc;
1132
1133 return 0;
1134}
1135
Steven Tothe47f30b2008-01-10 04:25:59 -03001136static int vidioc_reqbufs(struct file *file, void *priv,
1137 struct v4l2_requestbuffers *p)
1138{
1139 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001140 return videobuf_reqbufs(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001141}
1142
1143static int vidioc_querybuf(struct file *file, void *priv,
1144 struct v4l2_buffer *p)
1145{
1146 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001147 return videobuf_querybuf(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001148}
1149
1150static int vidioc_qbuf(struct file *file, void *priv,
1151 struct v4l2_buffer *p)
1152{
1153 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001154 return videobuf_qbuf(get_queue(fh), p);
Steven Tothe47f30b2008-01-10 04:25:59 -03001155}
1156
1157static int vidioc_dqbuf(struct file *file, void *priv,
1158 struct v4l2_buffer *p)
1159{
1160 struct cx23885_fh *fh = priv;
Steven Toth9c8ced52008-10-16 20:18:44 -03001161 return videobuf_dqbuf(get_queue(fh), p,
1162 file->f_flags & O_NONBLOCK);
Steven Tothe47f30b2008-01-10 04:25:59 -03001163}
1164
1165static int vidioc_streamon(struct file *file, void *priv,
1166 enum v4l2_buf_type i)
1167{
1168 struct cx23885_fh *fh = priv;
1169 struct cx23885_dev *dev = fh->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001170 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001171
Steven Toth5ab27e62011-10-10 11:09:54 -03001172 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1173 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
Steven Tothe47f30b2008-01-10 04:25:59 -03001174 return -EINVAL;
1175 if (unlikely(i != fh->type))
1176 return -EINVAL;
1177
1178 if (unlikely(!res_get(dev, fh, get_resource(fh))))
1179 return -EBUSY;
Steven Toth24465b42011-10-10 11:09:54 -03001180
1181 /* Don't start VBI streaming unless vida streaming
1182 * has already started.
1183 */
1184 if ((fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) &&
1185 ((cx_read(VID_A_DMA_CTL) & 0x11) == 0))
1186 return -EINVAL;
1187
Steven Tothe47f30b2008-01-10 04:25:59 -03001188 return videobuf_streamon(get_queue(fh));
1189}
1190
1191static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
1192{
1193 struct cx23885_fh *fh = priv;
1194 struct cx23885_dev *dev = fh->dev;
1195 int err, res;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001196 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001197
Steven Toth5ab27e62011-10-10 11:09:54 -03001198 if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
1199 (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
Steven Tothe47f30b2008-01-10 04:25:59 -03001200 return -EINVAL;
1201 if (i != fh->type)
1202 return -EINVAL;
1203
1204 res = get_resource(fh);
1205 err = videobuf_streamoff(get_queue(fh));
1206 if (err < 0)
1207 return err;
1208 res_free(dev, fh, res);
1209 return 0;
1210}
1211
1212static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms)
1213{
1214 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001215 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001216
1217 mutex_lock(&dev->lock);
1218 cx23885_set_tvnorm(dev, *tvnorms);
1219 mutex_unlock(&dev->lock);
1220
1221 return 0;
1222}
1223
Hans Verkuild45b9b82008-09-04 03:33:43 -03001224static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i)
Steven Tothe47f30b2008-01-10 04:25:59 -03001225{
1226 static const char *iname[] = {
1227 [CX23885_VMUX_COMPOSITE1] = "Composite1",
1228 [CX23885_VMUX_COMPOSITE2] = "Composite2",
1229 [CX23885_VMUX_COMPOSITE3] = "Composite3",
1230 [CX23885_VMUX_COMPOSITE4] = "Composite4",
1231 [CX23885_VMUX_SVIDEO] = "S-Video",
David T.L. Wongdac65fa2009-10-21 11:07:24 -03001232 [CX23885_VMUX_COMPONENT] = "Component",
Steven Tothe47f30b2008-01-10 04:25:59 -03001233 [CX23885_VMUX_TELEVISION] = "Television",
1234 [CX23885_VMUX_CABLE] = "Cable TV",
1235 [CX23885_VMUX_DVB] = "DVB",
1236 [CX23885_VMUX_DEBUG] = "for debug only",
1237 };
1238 unsigned int n;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001239 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001240
1241 n = i->index;
1242 if (n >= 4)
1243 return -EINVAL;
1244
1245 if (0 == INPUT(n)->type)
1246 return -EINVAL;
1247
Steven Tothe47f30b2008-01-10 04:25:59 -03001248 i->index = n;
1249 i->type = V4L2_INPUT_TYPE_CAMERA;
1250 strcpy(i->name, iname[INPUT(n)->type]);
1251 if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) ||
Julia Lawall473d8022010-08-05 17:22:44 -03001252 (CX23885_VMUX_CABLE == INPUT(n)->type)) {
Steven Tothe47f30b2008-01-10 04:25:59 -03001253 i->type = V4L2_INPUT_TYPE_TUNER;
1254 i->std = CX23885_NORMS;
Julia Lawall473d8022010-08-05 17:22:44 -03001255 }
Steven Tothe47f30b2008-01-10 04:25:59 -03001256 return 0;
1257}
Steven Tothe47f30b2008-01-10 04:25:59 -03001258
1259static int vidioc_enum_input(struct file *file, void *priv,
1260 struct v4l2_input *i)
1261{
1262 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001263 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001264 return cx23885_enum_input(dev, i);
1265}
1266
1267static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
1268{
1269 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1270
1271 *i = dev->input;
Harvey Harrison22b4e642008-04-08 23:20:00 -03001272 dprintk(1, "%s() returns %d\n", __func__, *i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001273 return 0;
1274}
1275
1276static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
1277{
1278 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1279
Harvey Harrison22b4e642008-04-08 23:20:00 -03001280 dprintk(1, "%s(%d)\n", __func__, i);
Steven Tothe47f30b2008-01-10 04:25:59 -03001281
1282 if (i >= 4) {
Harvey Harrison22b4e642008-04-08 23:20:00 -03001283 dprintk(1, "%s() -EINVAL\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001284 return -EINVAL;
1285 }
1286
1287 mutex_lock(&dev->lock);
1288 cx23885_video_mux(dev, i);
1289 mutex_unlock(&dev->lock);
1290 return 0;
1291}
1292
Andy Wallse9e5cf42010-07-18 18:18:06 -03001293static int vidioc_log_status(struct file *file, void *priv)
1294{
1295 struct cx23885_fh *fh = priv;
1296 struct cx23885_dev *dev = fh->dev;
1297
1298 printk(KERN_INFO
1299 "%s/0: ============ START LOG STATUS ============\n",
1300 dev->name);
1301 call_all(dev, core, log_status);
1302 printk(KERN_INFO
1303 "%s/0: ============= END LOG STATUS =============\n",
1304 dev->name);
1305 return 0;
1306}
1307
Steven Tothe47f30b2008-01-10 04:25:59 -03001308static int vidioc_queryctrl(struct file *file, void *priv,
1309 struct v4l2_queryctrl *qctrl)
1310{
1311 qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id);
1312 if (unlikely(qctrl->id == 0))
1313 return -EINVAL;
1314 return cx23885_ctrl_query(qctrl);
1315}
1316
1317static int vidioc_g_ctrl(struct file *file, void *priv,
1318 struct v4l2_control *ctl)
1319{
1320 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1321
1322 return cx23885_get_control(dev, ctl);
1323}
1324
1325static int vidioc_s_ctrl(struct file *file, void *priv,
1326 struct v4l2_control *ctl)
1327{
1328 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1329
1330 return cx23885_set_control(dev, ctl);
1331}
1332
1333static int vidioc_g_tuner(struct file *file, void *priv,
1334 struct v4l2_tuner *t)
1335{
1336 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1337
1338 if (unlikely(UNSET == dev->tuner_type))
1339 return -EINVAL;
1340 if (0 != t->index)
1341 return -EINVAL;
1342
1343 strcpy(t->name, "Television");
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001344
Steven Toth80f1e082011-10-10 11:09:53 -03001345 call_all(dev, tuner, g_tuner, t);
Steven Tothe47f30b2008-01-10 04:25:59 -03001346 return 0;
1347}
1348
1349static int vidioc_s_tuner(struct file *file, void *priv,
1350 struct v4l2_tuner *t)
1351{
1352 struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev;
1353
1354 if (UNSET == dev->tuner_type)
1355 return -EINVAL;
1356 if (0 != t->index)
1357 return -EINVAL;
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001358 /* Update the A/V core */
Steven Toth80f1e082011-10-10 11:09:53 -03001359 call_all(dev, tuner, s_tuner, t);
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001360
Steven Tothe47f30b2008-01-10 04:25:59 -03001361 return 0;
1362}
1363
1364static int vidioc_g_frequency(struct file *file, void *priv,
1365 struct v4l2_frequency *f)
1366{
1367 struct cx23885_fh *fh = priv;
1368 struct cx23885_dev *dev = fh->dev;
1369
1370 if (unlikely(UNSET == dev->tuner_type))
1371 return -EINVAL;
1372
1373 /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */
1374 f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
1375 f->frequency = dev->freq;
1376
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001377 call_all(dev, tuner, g_frequency, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001378
1379 return 0;
1380}
1381
Hans Verkuild45b9b82008-09-04 03:33:43 -03001382static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f)
Steven Tothe47f30b2008-01-10 04:25:59 -03001383{
1384 if (unlikely(UNSET == dev->tuner_type))
1385 return -EINVAL;
1386 if (unlikely(f->tuner != 0))
1387 return -EINVAL;
1388
1389 mutex_lock(&dev->lock);
1390 dev->freq = f->frequency;
1391
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001392 call_all(dev, tuner, s_frequency, f);
Steven Tothe47f30b2008-01-10 04:25:59 -03001393
1394 /* When changing channels it is required to reset TVAUDIO */
1395 msleep(10);
1396
1397 mutex_unlock(&dev->lock);
1398
1399 return 0;
1400}
Steven Tothe47f30b2008-01-10 04:25:59 -03001401
1402static int vidioc_s_frequency(struct file *file, void *priv,
1403 struct v4l2_frequency *f)
1404{
1405 struct cx23885_fh *fh = priv;
1406 struct cx23885_dev *dev = fh->dev;
1407
1408 if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
1409 return -EINVAL;
1410 if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
1411 return -EINVAL;
1412
1413 return
1414 cx23885_set_freq(dev, f);
1415}
1416
Steven Tothe47f30b2008-01-10 04:25:59 -03001417/* ----------------------------------------------------------- */
Steven Tothe47f30b2008-01-10 04:25:59 -03001418
1419static void cx23885_vid_timeout(unsigned long data)
1420{
1421 struct cx23885_dev *dev = (struct cx23885_dev *)data;
1422 struct cx23885_dmaqueue *q = &dev->vidq;
1423 struct cx23885_buffer *buf;
1424 unsigned long flags;
1425
1426 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
1427
1428 cx_clear(VID_A_DMA_CTL, 0x11);
1429
1430 spin_lock_irqsave(&dev->slock, flags);
1431 while (!list_empty(&q->active)) {
1432 buf = list_entry(q->active.next,
1433 struct cx23885_buffer, vb.queue);
1434 list_del(&buf->vb.queue);
1435 buf->vb.state = VIDEOBUF_ERROR;
1436 wake_up(&buf->vb.done);
Steven Toth79776c82011-10-10 11:09:54 -03001437 printk(KERN_ERR "%s: [%p/%d] timeout - dma=0x%08lx\n",
Steven Tothe47f30b2008-01-10 04:25:59 -03001438 dev->name, buf, buf->vb.i,
1439 (unsigned long)buf->risc.dma);
1440 }
1441 cx23885_restart_video_queue(dev, q);
1442 spin_unlock_irqrestore(&dev->slock, flags);
1443}
1444
1445int cx23885_video_irq(struct cx23885_dev *dev, u32 status)
1446{
1447 u32 mask, count;
1448 int handled = 0;
1449
1450 mask = cx_read(VID_A_INT_MSK);
1451 if (0 == (status & mask))
1452 return handled;
Steven Toth79776c82011-10-10 11:09:54 -03001453
Steven Tothe47f30b2008-01-10 04:25:59 -03001454 cx_write(VID_A_INT_STAT, status);
1455
Steven Tothe47f30b2008-01-10 04:25:59 -03001456 /* risc op code error */
Steven Toth79776c82011-10-10 11:09:54 -03001457 if ((status & VID_BC_MSK_OPC_ERR) ||
1458 (status & VID_BC_MSK_SYNC) ||
1459 (status & VID_BC_MSK_OF)) {
1460
1461 if (status & VID_BC_MSK_OPC_ERR)
1462 dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n",
1463 VID_BC_MSK_OPC_ERR);
1464
1465 if (status & VID_BC_MSK_SYNC)
1466 dprintk(7, " (VID_BC_MSK_SYNC 0x%08x)\n",
1467 VID_BC_MSK_SYNC);
1468
1469 if (status & VID_BC_MSK_OF)
1470 dprintk(7, " (VID_BC_MSK_OF 0x%08x)\n",
1471 VID_BC_MSK_OF);
1472
1473 printk(KERN_WARNING "%s: video risc op code error\n",
Steven Tothe47f30b2008-01-10 04:25:59 -03001474 dev->name);
Steven Toth79776c82011-10-10 11:09:54 -03001475
Steven Tothe47f30b2008-01-10 04:25:59 -03001476 cx_clear(VID_A_DMA_CTL, 0x11);
1477 cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]);
Steven Toth79776c82011-10-10 11:09:54 -03001478
Steven Tothe47f30b2008-01-10 04:25:59 -03001479 }
1480
Steven Toth79776c82011-10-10 11:09:54 -03001481 /* Video */
1482 if (status & VID_BC_MSK_RISCI1) {
Steven Tothe47f30b2008-01-10 04:25:59 -03001483 spin_lock(&dev->slock);
1484 count = cx_read(VID_A_GPCNT);
1485 cx23885_video_wakeup(dev, &dev->vidq, count);
1486 spin_unlock(&dev->slock);
1487 handled++;
1488 }
Steven Toth79776c82011-10-10 11:09:54 -03001489 if (status & VID_BC_MSK_RISCI2) {
Steven Tothe47f30b2008-01-10 04:25:59 -03001490 dprintk(2, "stopper video\n");
1491 spin_lock(&dev->slock);
1492 cx23885_restart_video_queue(dev, &dev->vidq);
1493 spin_unlock(&dev->slock);
1494 handled++;
1495 }
1496
Steven Toth79776c82011-10-10 11:09:54 -03001497 /* Allow the VBI framework to process it's payload */
1498 handled += cx23885_vbi_irq(dev, status);
1499
Steven Tothe47f30b2008-01-10 04:25:59 -03001500 return handled;
1501}
1502
Steven Tothe47f30b2008-01-10 04:25:59 -03001503/* ----------------------------------------------------------- */
1504/* exported stuff */
1505
Hans Verkuilbec43662008-12-30 06:58:20 -03001506static const struct v4l2_file_operations video_fops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001507 .owner = THIS_MODULE,
1508 .open = video_open,
1509 .release = video_release,
1510 .read = video_read,
1511 .poll = video_poll,
1512 .mmap = video_mmap,
1513 .ioctl = video_ioctl2,
Steven Tothe47f30b2008-01-10 04:25:59 -03001514};
1515
Hans Verkuila3998102008-07-21 02:57:38 -03001516static const struct v4l2_ioctl_ops video_ioctl_ops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001517 .vidioc_querycap = vidioc_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -03001518 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1519 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1520 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1521 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1522 .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt,
1523 .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt,
1524 .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt,
Steven Tothe47f30b2008-01-10 04:25:59 -03001525 .vidioc_reqbufs = vidioc_reqbufs,
1526 .vidioc_querybuf = vidioc_querybuf,
1527 .vidioc_qbuf = vidioc_qbuf,
1528 .vidioc_dqbuf = vidioc_dqbuf,
1529 .vidioc_s_std = vidioc_s_std,
1530 .vidioc_enum_input = vidioc_enum_input,
1531 .vidioc_g_input = vidioc_g_input,
1532 .vidioc_s_input = vidioc_s_input,
Andy Wallse9e5cf42010-07-18 18:18:06 -03001533 .vidioc_log_status = vidioc_log_status,
Steven Tothe47f30b2008-01-10 04:25:59 -03001534 .vidioc_queryctrl = vidioc_queryctrl,
1535 .vidioc_g_ctrl = vidioc_g_ctrl,
1536 .vidioc_s_ctrl = vidioc_s_ctrl,
1537 .vidioc_streamon = vidioc_streamon,
1538 .vidioc_streamoff = vidioc_streamoff,
Steven Tothe47f30b2008-01-10 04:25:59 -03001539 .vidioc_g_tuner = vidioc_g_tuner,
1540 .vidioc_s_tuner = vidioc_s_tuner,
1541 .vidioc_g_frequency = vidioc_g_frequency,
1542 .vidioc_s_frequency = vidioc_s_frequency,
Andy Walls74618242009-09-26 22:50:44 -03001543 .vidioc_g_chip_ident = cx23885_g_chip_ident,
Steven Tothe47f30b2008-01-10 04:25:59 -03001544#ifdef CONFIG_VIDEO_ADV_DEBUG
Andy Walls74618242009-09-26 22:50:44 -03001545 .vidioc_g_register = cx23885_g_register,
1546 .vidioc_s_register = cx23885_s_register,
Steven Tothe47f30b2008-01-10 04:25:59 -03001547#endif
Hans Verkuila3998102008-07-21 02:57:38 -03001548};
1549
1550static struct video_device cx23885_vbi_template;
1551static struct video_device cx23885_video_template = {
1552 .name = "cx23885-video",
Hans Verkuila3998102008-07-21 02:57:38 -03001553 .fops = &video_fops,
Hans Verkuila3998102008-07-21 02:57:38 -03001554 .ioctl_ops = &video_ioctl_ops,
Steven Tothe47f30b2008-01-10 04:25:59 -03001555 .tvnorms = CX23885_NORMS,
1556 .current_norm = V4L2_STD_NTSC_M,
1557};
1558
Hans Verkuilbec43662008-12-30 06:58:20 -03001559static const struct v4l2_file_operations radio_fops = {
Steven Tothe47f30b2008-01-10 04:25:59 -03001560 .owner = THIS_MODULE,
1561 .open = video_open,
1562 .release = video_release,
1563 .ioctl = video_ioctl2,
Steven Tothe47f30b2008-01-10 04:25:59 -03001564};
1565
1566
1567void cx23885_video_unregister(struct cx23885_dev *dev)
1568{
Harvey Harrison22b4e642008-04-08 23:20:00 -03001569 dprintk(1, "%s()\n", __func__);
Andy Wallsdbe83a32010-07-19 01:19:43 -03001570 cx23885_irq_remove(dev, 0x01);
Steven Tothe47f30b2008-01-10 04:25:59 -03001571
Steven Toth79776c82011-10-10 11:09:54 -03001572 if (dev->vbi_dev) {
1573 if (video_is_registered(dev->vbi_dev))
1574 video_unregister_device(dev->vbi_dev);
1575 else
1576 video_device_release(dev->vbi_dev);
1577 dev->vbi_dev = NULL;
1578 btcx_riscmem_free(dev->pci, &dev->vbiq.stopper);
1579 }
Steven Tothe47f30b2008-01-10 04:25:59 -03001580 if (dev->video_dev) {
Laurent Pinchartf0813b42009-11-27 13:57:30 -03001581 if (video_is_registered(dev->video_dev))
Steven Tothe47f30b2008-01-10 04:25:59 -03001582 video_unregister_device(dev->video_dev);
1583 else
1584 video_device_release(dev->video_dev);
1585 dev->video_dev = NULL;
1586
1587 btcx_riscmem_free(dev->pci, &dev->vidq.stopper);
1588 }
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001589
1590 if (dev->audio_dev)
Steven Tothefa762f2011-10-10 11:09:53 -03001591 cx23885_audio_unregister(dev);
Steven Tothe47f30b2008-01-10 04:25:59 -03001592}
1593
1594int cx23885_video_register(struct cx23885_dev *dev)
1595{
1596 int err;
1597
Harvey Harrison22b4e642008-04-08 23:20:00 -03001598 dprintk(1, "%s()\n", __func__);
Steven Tothe47f30b2008-01-10 04:25:59 -03001599 spin_lock_init(&dev->slock);
1600
1601 /* Initialize VBI template */
1602 memcpy(&cx23885_vbi_template, &cx23885_video_template,
1603 sizeof(cx23885_vbi_template));
1604 strcpy(cx23885_vbi_template.name, "cx23885-vbi");
Steven Tothe47f30b2008-01-10 04:25:59 -03001605
1606 dev->tvnorm = cx23885_video_template.current_norm;
1607
1608 /* init video dma queues */
1609 INIT_LIST_HEAD(&dev->vidq.active);
1610 INIT_LIST_HEAD(&dev->vidq.queued);
1611 dev->vidq.timeout.function = cx23885_vid_timeout;
1612 dev->vidq.timeout.data = (unsigned long)dev;
1613 init_timer(&dev->vidq.timeout);
1614 cx23885_risc_stopper(dev->pci, &dev->vidq.stopper,
1615 VID_A_DMA_CTL, 0x11, 0x00);
1616
Steven Toth79776c82011-10-10 11:09:54 -03001617 /* init vbi dma queues */
1618 INIT_LIST_HEAD(&dev->vbiq.active);
1619 INIT_LIST_HEAD(&dev->vbiq.queued);
1620 dev->vbiq.timeout.function = cx23885_vbi_timeout;
1621 dev->vbiq.timeout.data = (unsigned long)dev;
1622 init_timer(&dev->vbiq.timeout);
1623 cx23885_risc_stopper(dev->pci, &dev->vbiq.stopper,
1624 VID_A_DMA_CTL, 0x22, 0x00);
Andy Wallsdbe83a32010-07-19 01:19:43 -03001625
1626 cx23885_irq_add_enable(dev, 0x01);
Steven Tothe47f30b2008-01-10 04:25:59 -03001627
Igor M. Liplianin557f48d2011-01-25 17:05:00 -03001628 if ((TUNER_ABSENT != dev->tuner_type) &&
1629 ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) {
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001630 struct v4l2_subdev *sd = NULL;
1631
1632 if (dev->tuner_addr)
Hans Verkuile6574f22009-04-01 03:57:53 -03001633 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
Igor M. Liplianin557f48d2011-01-25 17:05:00 -03001634 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
Laurent Pinchart9a1f8b32010-09-24 10:16:44 -03001635 "tuner", dev->tuner_addr, NULL);
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001636 else
Hans Verkuil53dacb12009-08-10 02:49:08 -03001637 sd = v4l2_i2c_new_subdev(&dev->v4l2_dev,
Igor M. Liplianin557f48d2011-01-25 17:05:00 -03001638 &dev->i2c_bus[dev->tuner_bus].i2c_adap,
Laurent Pinchart1532a072010-09-24 07:58:29 -03001639 "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV));
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001640 if (sd) {
1641 struct tuner_setup tun_setup;
1642
David T.L. Wongfd705e72009-10-21 11:08:30 -03001643 memset(&tun_setup, 0, sizeof(tun_setup));
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001644 tun_setup.mode_mask = T_ANALOG_TV;
1645 tun_setup.type = dev->tuner_type;
1646 tun_setup.addr = v4l2_i2c_subdev_addr(sd);
David T.L. Wong6f0d8c02009-10-21 13:15:30 -03001647 tun_setup.tuner_callback = cx23885_tuner_callback;
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001648
1649 v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup);
Kusanagi Kouichi0b32d652010-01-22 04:55:28 -03001650
1651 if (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) {
1652 struct xc2028_ctrl ctrl = {
1653 .fname = XC2028_DEFAULT_FIRMWARE,
1654 .max_len = 64
1655 };
1656 struct v4l2_priv_tun_config cfg = {
1657 .tuner = dev->tuner_type,
1658 .priv = &ctrl
1659 };
1660 v4l2_subdev_call(sd, tuner, s_config, &cfg);
1661 }
Hans Verkuil0d5a19f2009-03-29 06:53:29 -03001662 }
1663 }
1664
Steven Toth79776c82011-10-10 11:09:54 -03001665 /* register Video device */
Steven Tothe47f30b2008-01-10 04:25:59 -03001666 dev->video_dev = cx23885_vdev_init(dev, dev->pci,
1667 &cx23885_video_template, "video");
1668 err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER,
1669 video_nr[dev->nr]);
1670 if (err < 0) {
1671 printk(KERN_INFO "%s: can't register video device\n",
1672 dev->name);
1673 goto fail_unreg;
1674 }
Steven Tothf88fb8e2011-10-10 11:09:54 -03001675 printk(KERN_INFO "%s: registered device %s [v4l2]\n",
Laurent Pinchart38c7c032009-11-27 13:57:15 -03001676 dev->name, video_device_node_name(dev->video_dev));
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001677
Steven Tothf88fb8e2011-10-10 11:09:54 -03001678 /* register VBI device */
1679 dev->vbi_dev = cx23885_vdev_init(dev, dev->pci,
1680 &cx23885_vbi_template, "vbi");
1681 err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
1682 vbi_nr[dev->nr]);
1683 if (err < 0) {
1684 printk(KERN_INFO "%s: can't register vbi device\n",
1685 dev->name);
1686 goto fail_unreg;
1687 }
1688 printk(KERN_INFO "%s: registered device %s\n",
1689 dev->name, video_device_node_name(dev->vbi_dev));
1690
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001691 /* Register ALSA audio device */
Steven Tothefa762f2011-10-10 11:09:53 -03001692 dev->audio_dev = cx23885_audio_register(dev);
Mijhail Moreyra97ce5672011-10-10 11:09:53 -03001693
Steven Tothe47f30b2008-01-10 04:25:59 -03001694 /* initial device configuration */
1695 mutex_lock(&dev->lock);
1696 cx23885_set_tvnorm(dev, dev->tvnorm);
1697 init_controls(dev);
1698 cx23885_video_mux(dev, 0);
1699 mutex_unlock(&dev->lock);
1700
Steven Tothe47f30b2008-01-10 04:25:59 -03001701 return 0;
1702
1703fail_unreg:
1704 cx23885_video_unregister(dev);
1705 return err;
1706}
1707