blob: 20eaf38ba95976b4a46ae7c785d4e3b63c658071 [file] [log] [blame]
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001/*
2 * cx18 ioctl system call
3 *
4 * Derived from ivtv-ioctl.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
Andy Walls6afdeaf2010-05-23 18:53:35 -03007 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
23 */
24
25#include "cx18-driver.h"
Andy Wallsb1526422008-08-30 16:03:44 -030026#include "cx18-io.h"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030027#include "cx18-version.h"
28#include "cx18-mailbox.h"
29#include "cx18-i2c.h"
30#include "cx18-queue.h"
31#include "cx18-fileops.h"
32#include "cx18-vbi.h"
33#include "cx18-audio.h"
34#include "cx18-video.h"
35#include "cx18-streams.h"
36#include "cx18-ioctl.h"
37#include "cx18-gpio.h"
38#include "cx18-controls.h"
39#include "cx18-cards.h"
40#include "cx18-av-core.h"
41#include <media/tveeprom.h>
42#include <media/v4l2-chip-ident.h>
43#include <linux/i2c-id.h>
44
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -030045u16 cx18_service2vbi(int type)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030046{
47 switch (type) {
48 case V4L2_SLICED_TELETEXT_B:
49 return CX18_SLICED_TYPE_TELETEXT_B;
50 case V4L2_SLICED_CAPTION_525:
51 return CX18_SLICED_TYPE_CAPTION_525;
52 case V4L2_SLICED_WSS_625:
53 return CX18_SLICED_TYPE_WSS_625;
54 case V4L2_SLICED_VPS:
55 return CX18_SLICED_TYPE_VPS;
56 default:
57 return 0;
58 }
59}
60
Andy Wallsc1994082009-02-05 22:37:49 -030061/* Check if VBI services are allowed on the (field, line) for the video std */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030062static int valid_service_line(int field, int line, int is_pal)
63{
Andy Wallsc1994082009-02-05 22:37:49 -030064 return (is_pal && line >= 6 &&
65 ((field == 0 && line <= 23) || (field == 1 && line <= 22))) ||
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030066 (!is_pal && line >= 10 && line < 22);
67}
68
Andy Wallsc1994082009-02-05 22:37:49 -030069/*
70 * For a (field, line, std) and inbound potential set of services for that line,
71 * return the first valid service of those passed in the incoming set for that
72 * line in priority order:
73 * CC, VPS, or WSS over TELETEXT for well known lines
74 * TELETEXT, before VPS, before CC, before WSS, for other lines
75 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030076static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
77{
78 u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
79 int i;
80
81 set = set & valid_set;
82 if (set == 0 || !valid_service_line(field, line, is_pal))
83 return 0;
84 if (!is_pal) {
85 if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
86 return V4L2_SLICED_CAPTION_525;
87 } else {
88 if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
89 return V4L2_SLICED_VPS;
90 if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
91 return V4L2_SLICED_WSS_625;
92 if (line == 23)
93 return 0;
94 }
95 for (i = 0; i < 32; i++) {
96 if ((1 << i) & set)
97 return 1 << i;
98 }
99 return 0;
100}
101
Andy Wallsc1994082009-02-05 22:37:49 -0300102/*
103 * Expand the service_set of *fmt into valid service_lines for the std,
104 * and clear the passed in fmt->service_set
105 */
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -0300106void cx18_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300107{
108 u16 set = fmt->service_set;
109 int f, l;
110
111 fmt->service_set = 0;
112 for (f = 0; f < 2; f++) {
113 for (l = 0; l < 24; l++)
114 fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
115 }
116}
117
Andy Wallsc1994082009-02-05 22:37:49 -0300118/*
119 * Sanitize the service_lines in *fmt per the video std, and return 1
120 * if any service_line is left as valid after santization
121 */
Andy Walls302df972009-01-31 00:33:02 -0300122static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
123{
124 int f, l;
125 u16 set = 0;
126
127 for (f = 0; f < 2; f++) {
128 for (l = 0; l < 24; l++) {
129 fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
130 set |= fmt->service_lines[f][l];
131 }
132 }
133 return set != 0;
134}
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300135
Andy Wallsc1994082009-02-05 22:37:49 -0300136/* Compute the service_set from the assumed valid service_lines of *fmt */
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -0300137u16 cx18_get_service_set(struct v4l2_sliced_vbi_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300138{
139 int f, l;
140 u16 set = 0;
141
142 for (f = 0; f < 2; f++) {
143 for (l = 0; l < 24; l++)
144 set |= fmt->service_lines[f][l];
145 }
146 return set;
147}
148
Andy Walls3b6fe582008-06-21 08:36:31 -0300149static int cx18_g_fmt_vid_cap(struct file *file, void *fh,
150 struct v4l2_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300151{
Andy Walls3b6fe582008-06-21 08:36:31 -0300152 struct cx18_open_id *id = fh;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300153 struct cx18 *cx = id->cx;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300154 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300155
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300156 pixfmt->width = cx->params.width;
157 pixfmt->height = cx->params.height;
158 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
159 pixfmt->field = V4L2_FIELD_INTERLACED;
160 pixfmt->priv = 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300161 if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300162 pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
Hans Verkuila4a78712009-02-06 15:31:59 -0300163 /* YUV size is (Y=(h*720) + UV=(h*(720/2))) */
164 pixfmt->sizeimage = pixfmt->height * 720 * 3 / 2;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300165 pixfmt->bytesperline = 720;
Andy Walls3b6fe582008-06-21 08:36:31 -0300166 } else {
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300167 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
168 pixfmt->sizeimage = 128 * 1024;
169 pixfmt->bytesperline = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300170 }
171 return 0;
172}
173
Andy Walls3b6fe582008-06-21 08:36:31 -0300174static int cx18_g_fmt_vbi_cap(struct file *file, void *fh,
175 struct v4l2_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300176{
Andy Walls3b6fe582008-06-21 08:36:31 -0300177 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300178 struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300179
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300180 vbifmt->sampling_rate = 27000000;
Andy Wallsc1994082009-02-05 22:37:49 -0300181 vbifmt->offset = 248; /* FIXME - slightly wrong for both 50 & 60 Hz */
Andy Walls302df972009-01-31 00:33:02 -0300182 vbifmt->samples_per_line = vbi_active_samples - 4;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300183 vbifmt->sample_format = V4L2_PIX_FMT_GREY;
184 vbifmt->start[0] = cx->vbi.start[0];
185 vbifmt->start[1] = cx->vbi.start[1];
186 vbifmt->count[0] = vbifmt->count[1] = cx->vbi.count;
187 vbifmt->flags = 0;
188 vbifmt->reserved[0] = 0;
189 vbifmt->reserved[1] = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300190 return 0;
191}
192
Andy Walls3b6fe582008-06-21 08:36:31 -0300193static int cx18_g_fmt_sliced_vbi_cap(struct file *file, void *fh,
194 struct v4l2_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300195{
Andy Walls302df972009-01-31 00:33:02 -0300196 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
197 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
198
Andy Wallsc1994082009-02-05 22:37:49 -0300199 /* sane, V4L2 spec compliant, defaults */
Andy Walls302df972009-01-31 00:33:02 -0300200 vbifmt->reserved[0] = 0;
201 vbifmt->reserved[1] = 0;
202 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
203 memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
Andy Wallsc1994082009-02-05 22:37:49 -0300204 vbifmt->service_set = 0;
Andy Walls302df972009-01-31 00:33:02 -0300205
Andy Wallsc1994082009-02-05 22:37:49 -0300206 /*
207 * Fetch the configured service_lines and total service_set from the
208 * digitizer/slicer. Note, cx18_av_vbi() wipes the passed in
209 * fmt->fmt.sliced under valid calling conditions
210 */
Hans Verkuiladd632c2010-03-14 12:24:15 -0300211 if (v4l2_subdev_call(cx->sd_av, vbi, g_sliced_fmt, &fmt->fmt.sliced))
Andy Wallsc1994082009-02-05 22:37:49 -0300212 return -EINVAL;
213
214 /* Ensure V4L2 spec compliant output */
215 vbifmt->reserved[0] = 0;
216 vbifmt->reserved[1] = 0;
217 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
Andy Walls302df972009-01-31 00:33:02 -0300218 vbifmt->service_set = cx18_get_service_set(vbifmt);
219 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300220}
221
222static int cx18_try_fmt_vid_cap(struct file *file, void *fh,
223 struct v4l2_format *fmt)
224{
225 struct cx18_open_id *id = fh;
226 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300227 int w = fmt->fmt.pix.width;
228 int h = fmt->fmt.pix.height;
Hans Verkuila4a78712009-02-06 15:31:59 -0300229 int min_h = 2;
Andy Walls3b6fe582008-06-21 08:36:31 -0300230
Andy Walls3b6fe582008-06-21 08:36:31 -0300231 w = min(w, 720);
Hans Verkuila4a78712009-02-06 15:31:59 -0300232 w = max(w, 2);
233 if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
234 /* YUV height must be a multiple of 32 */
235 h &= ~0x1f;
236 min_h = 32;
237 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300238 h = min(h, cx->is_50hz ? 576 : 480);
Hans Verkuila4a78712009-02-06 15:31:59 -0300239 h = max(h, min_h);
240
Andy Walls3b6fe582008-06-21 08:36:31 -0300241 cx18_g_fmt_vid_cap(file, fh, fmt);
242 fmt->fmt.pix.width = w;
243 fmt->fmt.pix.height = h;
244 return 0;
245}
246
247static int cx18_try_fmt_vbi_cap(struct file *file, void *fh,
248 struct v4l2_format *fmt)
249{
Andy Walls3b6fe582008-06-21 08:36:31 -0300250 return cx18_g_fmt_vbi_cap(file, fh, fmt);
251}
252
253static int cx18_try_fmt_sliced_vbi_cap(struct file *file, void *fh,
254 struct v4l2_format *fmt)
255{
Andy Walls302df972009-01-31 00:33:02 -0300256 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
257 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
258
259 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
260 vbifmt->reserved[0] = 0;
261 vbifmt->reserved[1] = 0;
262
Andy Wallsc1994082009-02-05 22:37:49 -0300263 /* If given a service set, expand it validly & clear passed in set */
Andy Walls302df972009-01-31 00:33:02 -0300264 if (vbifmt->service_set)
265 cx18_expand_service_set(vbifmt, cx->is_50hz);
Andy Wallsc1994082009-02-05 22:37:49 -0300266 /* Sanitize the service_lines, and compute the new set if any valid */
267 if (check_service_set(vbifmt, cx->is_50hz))
268 vbifmt->service_set = cx18_get_service_set(vbifmt);
Andy Walls302df972009-01-31 00:33:02 -0300269 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300270}
271
272static int cx18_s_fmt_vid_cap(struct file *file, void *fh,
273 struct v4l2_format *fmt)
274{
275 struct cx18_open_id *id = fh;
276 struct cx18 *cx = id->cx;
Hans Verkuil9e36eab2010-05-08 16:37:53 -0300277 struct v4l2_mbus_framefmt mbus_fmt;
Andy Walls3b6fe582008-06-21 08:36:31 -0300278 int ret;
Hans Verkuileffc3462008-09-06 08:24:37 -0300279 int w, h;
Andy Walls3b6fe582008-06-21 08:36:31 -0300280
Hans Verkuilffb48772010-05-01 08:03:24 -0300281 ret = v4l2_prio_check(&cx->prio, id->prio);
Andy Walls3b6fe582008-06-21 08:36:31 -0300282 if (ret)
283 return ret;
284
Andy Walls3b6fe582008-06-21 08:36:31 -0300285 ret = cx18_try_fmt_vid_cap(file, fh, fmt);
286 if (ret)
287 return ret;
Hans Verkuileffc3462008-09-06 08:24:37 -0300288 w = fmt->fmt.pix.width;
289 h = fmt->fmt.pix.height;
Andy Walls3b6fe582008-06-21 08:36:31 -0300290
291 if (cx->params.width == w && cx->params.height == h)
292 return 0;
293
294 if (atomic_read(&cx->ana_capturing) > 0)
295 return -EBUSY;
296
Hans Verkuil9e36eab2010-05-08 16:37:53 -0300297 mbus_fmt.width = cx->params.width = w;
298 mbus_fmt.height = cx->params.height = h;
299 mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
300 v4l2_subdev_call(cx->sd_av, video, s_mbus_fmt, &mbus_fmt);
Andy Walls3b6fe582008-06-21 08:36:31 -0300301 return cx18_g_fmt_vid_cap(file, fh, fmt);
302}
303
304static int cx18_s_fmt_vbi_cap(struct file *file, void *fh,
305 struct v4l2_format *fmt)
306{
307 struct cx18_open_id *id = fh;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300308 struct cx18 *cx = id->cx;
309 int ret;
310
Hans Verkuilffb48772010-05-01 08:03:24 -0300311 ret = v4l2_prio_check(&cx->prio, id->prio);
Andy Walls3b6fe582008-06-21 08:36:31 -0300312 if (ret)
313 return ret;
314
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300315 /*
316 * Changing the Encoder's Raw VBI parameters won't have any effect
317 * if any analog capture is ongoing
318 */
319 if (!cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
Andy Walls3b6fe582008-06-21 08:36:31 -0300320 return -EBUSY;
321
Andy Wallsc1994082009-02-05 22:37:49 -0300322 /*
323 * Set the digitizer registers for raw active VBI.
324 * Note cx18_av_vbi_wipes out alot of the passed in fmt under valid
325 * calling conditions
326 */
Hans Verkuiladd632c2010-03-14 12:24:15 -0300327 ret = v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &fmt->fmt.vbi);
Andy Wallsc1994082009-02-05 22:37:49 -0300328 if (ret)
329 return ret;
330
331 /* Store our new v4l2 (non-)sliced VBI state */
Andy Walls3b6fe582008-06-21 08:36:31 -0300332 cx->vbi.sliced_in->service_set = 0;
Andy Wallsdd073432008-12-12 16:24:04 -0300333 cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
Andy Wallsc1994082009-02-05 22:37:49 -0300334
Andy Walls3b6fe582008-06-21 08:36:31 -0300335 return cx18_g_fmt_vbi_cap(file, fh, fmt);
336}
337
338static int cx18_s_fmt_sliced_vbi_cap(struct file *file, void *fh,
339 struct v4l2_format *fmt)
340{
Andy Walls302df972009-01-31 00:33:02 -0300341 struct cx18_open_id *id = fh;
342 struct cx18 *cx = id->cx;
343 int ret;
344 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
345
Hans Verkuilffb48772010-05-01 08:03:24 -0300346 ret = v4l2_prio_check(&cx->prio, id->prio);
Andy Walls302df972009-01-31 00:33:02 -0300347 if (ret)
348 return ret;
349
Andy Wallsc1994082009-02-05 22:37:49 -0300350 cx18_try_fmt_sliced_vbi_cap(file, fh, fmt);
351
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300352 /*
353 * Changing the Encoder's Raw VBI parameters won't have any effect
354 * if any analog capture is ongoing
355 */
356 if (cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
Andy Wallsc1994082009-02-05 22:37:49 -0300357 return -EBUSY;
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300358
Andy Wallsc1994082009-02-05 22:37:49 -0300359 /*
360 * Set the service_lines requested in the digitizer/slicer registers.
361 * Note, cx18_av_vbi() wipes some "impossible" service lines in the
362 * passed in fmt->fmt.sliced under valid calling conditions
363 */
Hans Verkuiladd632c2010-03-14 12:24:15 -0300364 ret = v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &fmt->fmt.sliced);
Andy Walls302df972009-01-31 00:33:02 -0300365 if (ret)
366 return ret;
Andy Wallsc1994082009-02-05 22:37:49 -0300367 /* Store our current v4l2 sliced VBI settings */
Andy Walls302df972009-01-31 00:33:02 -0300368 cx->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
Andy Walls302df972009-01-31 00:33:02 -0300369 memcpy(cx->vbi.sliced_in, vbifmt, sizeof(*cx->vbi.sliced_in));
370 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300371}
372
373static int cx18_g_chip_ident(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300374 struct v4l2_dbg_chip_ident *chip)
Andy Walls3b6fe582008-06-21 08:36:31 -0300375{
376 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
Andy Wallsff2a2002009-02-20 23:52:13 -0300377 int err = 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300378
Andy Walls3b6fe582008-06-21 08:36:31 -0300379 chip->ident = V4L2_IDENT_NONE;
380 chip->revision = 0;
Andy Wallsff2a2002009-02-20 23:52:13 -0300381 switch (chip->match.type) {
382 case V4L2_CHIP_MATCH_HOST:
383 switch (chip->match.addr) {
384 case 0:
385 chip->ident = V4L2_IDENT_CX23418;
386 chip->revision = cx18_read_reg(cx, 0xC72028);
387 break;
388 case 1:
389 /*
390 * The A/V decoder is always present, but in the rare
391 * case that the card doesn't have analog, we don't
392 * use it. We find it w/o using the cx->sd_av pointer
393 */
394 cx18_call_hw(cx, CX18_HW_418_AV,
395 core, g_chip_ident, chip);
396 break;
397 default:
398 /*
399 * Could return ident = V4L2_IDENT_UNKNOWN if we had
400 * other host chips at higher addresses, but we don't
401 */
402 err = -EINVAL; /* per V4L2 spec */
403 break;
404 }
405 break;
406 case V4L2_CHIP_MATCH_I2C_DRIVER:
407 /* If needed, returns V4L2_IDENT_AMBIGUOUS without extra work */
408 cx18_call_all(cx, core, g_chip_ident, chip);
409 break;
410 case V4L2_CHIP_MATCH_I2C_ADDR:
411 /*
412 * We could return V4L2_IDENT_UNKNOWN, but we don't do the work
413 * to look if a chip is at the address with no driver. That's a
414 * dangerous thing to do with EEPROMs anyway.
415 */
416 cx18_call_all(cx, core, g_chip_ident, chip);
417 break;
418 default:
419 err = -EINVAL;
420 break;
Andy Walls3b6fe582008-06-21 08:36:31 -0300421 }
Andy Wallsff2a2002009-02-20 23:52:13 -0300422 return err;
Andy Walls3b6fe582008-06-21 08:36:31 -0300423}
424
Hans Verkuil36ecd492008-06-25 06:00:17 -0300425#ifdef CONFIG_VIDEO_ADV_DEBUG
426static int cx18_cxc(struct cx18 *cx, unsigned int cmd, void *arg)
427{
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300428 struct v4l2_dbg_register *regs = arg;
Hans Verkuil36ecd492008-06-25 06:00:17 -0300429
430 if (!capable(CAP_SYS_ADMIN))
431 return -EPERM;
432 if (regs->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
433 return -EINVAL;
434
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300435 regs->size = 4;
Andy Wallsff2a2002009-02-20 23:52:13 -0300436 if (cmd == VIDIOC_DBG_S_REGISTER)
Andy Wallsb1526422008-08-30 16:03:44 -0300437 cx18_write_enc(cx, regs->val, regs->reg);
Andy Wallsff2a2002009-02-20 23:52:13 -0300438 else
439 regs->val = cx18_read_enc(cx, regs->reg);
Hans Verkuil36ecd492008-06-25 06:00:17 -0300440 return 0;
441}
442
Andy Walls3b6fe582008-06-21 08:36:31 -0300443static int cx18_g_register(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300444 struct v4l2_dbg_register *reg)
Andy Walls3b6fe582008-06-21 08:36:31 -0300445{
446 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
447
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300448 if (v4l2_chip_match_host(&reg->match))
Andy Walls3b6fe582008-06-21 08:36:31 -0300449 return cx18_cxc(cx, VIDIOC_DBG_G_REGISTER, reg);
Andy Wallsff2a2002009-02-20 23:52:13 -0300450 /* FIXME - errors shouldn't be ignored */
451 cx18_call_all(cx, core, g_register, reg);
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300452 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300453}
454
455static int cx18_s_register(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300456 struct v4l2_dbg_register *reg)
Andy Walls3b6fe582008-06-21 08:36:31 -0300457{
458 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
459
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300460 if (v4l2_chip_match_host(&reg->match))
Andy Walls3b6fe582008-06-21 08:36:31 -0300461 return cx18_cxc(cx, VIDIOC_DBG_S_REGISTER, reg);
Andy Wallsff2a2002009-02-20 23:52:13 -0300462 /* FIXME - errors shouldn't be ignored */
463 cx18_call_all(cx, core, s_register, reg);
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300464 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300465}
Hans Verkuil36ecd492008-06-25 06:00:17 -0300466#endif
Andy Walls3b6fe582008-06-21 08:36:31 -0300467
468static int cx18_g_priority(struct file *file, void *fh, enum v4l2_priority *p)
469{
470 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
471
Andy Walls3b6fe582008-06-21 08:36:31 -0300472 *p = v4l2_prio_max(&cx->prio);
473 return 0;
474}
475
476static int cx18_s_priority(struct file *file, void *fh, enum v4l2_priority prio)
477{
478 struct cx18_open_id *id = fh;
479 struct cx18 *cx = id->cx;
480
Andy Walls3b6fe582008-06-21 08:36:31 -0300481 return v4l2_prio_change(&cx->prio, &id->prio, prio);
482}
483
484static int cx18_querycap(struct file *file, void *fh,
485 struct v4l2_capability *vcap)
486{
487 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
488
Andy Walls3b6fe582008-06-21 08:36:31 -0300489 strlcpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
490 strlcpy(vcap->card, cx->card_name, sizeof(vcap->card));
Andy Walls3d059132009-01-10 21:54:39 -0300491 snprintf(vcap->bus_info, sizeof(vcap->bus_info),
492 "PCI:%s", pci_name(cx->pci_dev));
Andy Walls3b6fe582008-06-21 08:36:31 -0300493 vcap->version = CX18_DRIVER_VERSION; /* version */
494 vcap->capabilities = cx->v4l2_cap; /* capabilities */
495 return 0;
496}
497
498static int cx18_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
499{
500 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
501
Andy Walls3b6fe582008-06-21 08:36:31 -0300502 return cx18_get_audio_input(cx, vin->index, vin);
503}
504
505static int cx18_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
506{
507 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
508
Andy Walls3b6fe582008-06-21 08:36:31 -0300509 vin->index = cx->audio_input;
510 return cx18_get_audio_input(cx, vin->index, vin);
511}
512
513static int cx18_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
514{
515 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
516
Andy Walls3b6fe582008-06-21 08:36:31 -0300517 if (vout->index >= cx->nof_audio_inputs)
518 return -EINVAL;
519 cx->audio_input = vout->index;
520 cx18_audio_set_io(cx);
521 return 0;
522}
523
524static int cx18_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
525{
526 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
527
Andy Walls3b6fe582008-06-21 08:36:31 -0300528 /* set it to defaults from our table */
529 return cx18_get_input(cx, vin->index, vin);
530}
531
532static int cx18_cropcap(struct file *file, void *fh,
533 struct v4l2_cropcap *cropcap)
534{
535 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
536
Andy Walls3b6fe582008-06-21 08:36:31 -0300537 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
538 return -EINVAL;
539 cropcap->bounds.top = cropcap->bounds.left = 0;
540 cropcap->bounds.width = 720;
541 cropcap->bounds.height = cx->is_50hz ? 576 : 480;
542 cropcap->pixelaspect.numerator = cx->is_50hz ? 59 : 10;
543 cropcap->pixelaspect.denominator = cx->is_50hz ? 54 : 11;
544 cropcap->defrect = cropcap->bounds;
545 return 0;
546}
547
548static int cx18_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
549{
550 struct cx18_open_id *id = fh;
551 struct cx18 *cx = id->cx;
552 int ret;
553
Hans Verkuilffb48772010-05-01 08:03:24 -0300554 ret = v4l2_prio_check(&cx->prio, id->prio);
Andy Walls3b6fe582008-06-21 08:36:31 -0300555 if (ret)
556 return ret;
557
Andy Walls3b6fe582008-06-21 08:36:31 -0300558 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
559 return -EINVAL;
Andy Wallsfa3e7032009-02-16 02:23:25 -0300560 CX18_DEBUG_WARN("VIDIOC_S_CROP not implemented\n");
561 return -EINVAL;
Andy Walls3b6fe582008-06-21 08:36:31 -0300562}
563
564static int cx18_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
565{
566 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
567
Andy Walls3b6fe582008-06-21 08:36:31 -0300568 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
569 return -EINVAL;
Andy Wallsfa3e7032009-02-16 02:23:25 -0300570 CX18_DEBUG_WARN("VIDIOC_G_CROP not implemented\n");
571 return -EINVAL;
Andy Walls3b6fe582008-06-21 08:36:31 -0300572}
573
574static int cx18_enum_fmt_vid_cap(struct file *file, void *fh,
575 struct v4l2_fmtdesc *fmt)
576{
577 static struct v4l2_fmtdesc formats[] = {
578 { 0, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
579 "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12, { 0, 0, 0, 0 }
580 },
581 { 1, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FMT_FLAG_COMPRESSED,
582 "MPEG", V4L2_PIX_FMT_MPEG, { 0, 0, 0, 0 }
583 }
584 };
585
Andy Walls3b6fe582008-06-21 08:36:31 -0300586 if (fmt->index > 1)
587 return -EINVAL;
588 *fmt = formats[fmt->index];
589 return 0;
590}
591
592static int cx18_g_input(struct file *file, void *fh, unsigned int *i)
593{
594 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
595
Andy Walls3b6fe582008-06-21 08:36:31 -0300596 *i = cx->active_input;
597 return 0;
598}
599
600int cx18_s_input(struct file *file, void *fh, unsigned int inp)
601{
602 struct cx18_open_id *id = fh;
603 struct cx18 *cx = id->cx;
604 int ret;
605
Hans Verkuilffb48772010-05-01 08:03:24 -0300606 ret = v4l2_prio_check(&cx->prio, id->prio);
Andy Walls3b6fe582008-06-21 08:36:31 -0300607 if (ret)
608 return ret;
609
Roel Kluinde81c3c2009-07-02 16:09:25 -0300610 if (inp >= cx->nof_inputs)
Andy Walls3b6fe582008-06-21 08:36:31 -0300611 return -EINVAL;
612
613 if (inp == cx->active_input) {
614 CX18_DEBUG_INFO("Input unchanged\n");
615 return 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300616 }
617
Andy Walls3b6fe582008-06-21 08:36:31 -0300618 CX18_DEBUG_INFO("Changing input from %d to %d\n",
619 cx->active_input, inp);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300620
Andy Walls3b6fe582008-06-21 08:36:31 -0300621 cx->active_input = inp;
622 /* Set the audio input to whatever is appropriate for the input type. */
623 cx->audio_input = cx->card->video_inputs[inp].audio_index;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300624
Andy Walls3b6fe582008-06-21 08:36:31 -0300625 /* prevent others from messing with the streams until
626 we're finished changing inputs. */
627 cx18_mute(cx);
628 cx18_video_set_io(cx);
629 cx18_audio_set_io(cx);
630 cx18_unmute(cx);
631 return 0;
632}
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300633
Andy Walls3b6fe582008-06-21 08:36:31 -0300634static int cx18_g_frequency(struct file *file, void *fh,
635 struct v4l2_frequency *vf)
636{
637 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
638
Andy Walls3b6fe582008-06-21 08:36:31 -0300639 if (vf->tuner != 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300640 return -EINVAL;
Andy Walls3b6fe582008-06-21 08:36:31 -0300641
Andy Wallsff2a2002009-02-20 23:52:13 -0300642 cx18_call_all(cx, tuner, g_frequency, vf);
Andy Walls3b6fe582008-06-21 08:36:31 -0300643 return 0;
644}
645
646int cx18_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
647{
648 struct cx18_open_id *id = fh;
649 struct cx18 *cx = id->cx;
650 int ret;
651
Hans Verkuilffb48772010-05-01 08:03:24 -0300652 ret = v4l2_prio_check(&cx->prio, id->prio);
Andy Walls3b6fe582008-06-21 08:36:31 -0300653 if (ret)
654 return ret;
655
Andy Walls3b6fe582008-06-21 08:36:31 -0300656 if (vf->tuner != 0)
657 return -EINVAL;
658
659 cx18_mute(cx);
660 CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
Andy Wallsff2a2002009-02-20 23:52:13 -0300661 cx18_call_all(cx, tuner, s_frequency, vf);
Andy Walls3b6fe582008-06-21 08:36:31 -0300662 cx18_unmute(cx);
663 return 0;
664}
665
666static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
667{
668 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
669
Andy Walls3b6fe582008-06-21 08:36:31 -0300670 *std = cx->std;
671 return 0;
672}
673
674int cx18_s_std(struct file *file, void *fh, v4l2_std_id *std)
675{
676 struct cx18_open_id *id = fh;
677 struct cx18 *cx = id->cx;
678 int ret;
679
Hans Verkuilffb48772010-05-01 08:03:24 -0300680 ret = v4l2_prio_check(&cx->prio, id->prio);
Andy Walls3b6fe582008-06-21 08:36:31 -0300681 if (ret)
682 return ret;
683
Andy Walls3b6fe582008-06-21 08:36:31 -0300684 if ((*std & V4L2_STD_ALL) == 0)
685 return -EINVAL;
686
687 if (*std == cx->std)
688 return 0;
689
690 if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
691 atomic_read(&cx->ana_capturing) > 0) {
692 /* Switching standard would turn off the radio or mess
693 with already running streams, prevent that by
694 returning EBUSY. */
695 return -EBUSY;
696 }
697
698 cx->std = *std;
699 cx->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
700 cx->params.is_50hz = cx->is_50hz = !cx->is_60hz;
701 cx->params.width = 720;
702 cx->params.height = cx->is_50hz ? 576 : 480;
703 cx->vbi.count = cx->is_50hz ? 18 : 12;
704 cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
705 cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
Andy Walls3b6fe582008-06-21 08:36:31 -0300706 CX18_DEBUG_INFO("Switching standard to %llx.\n",
707 (unsigned long long) cx->std);
708
709 /* Tuner */
Hans Verkuilf41737e2009-04-01 03:52:39 -0300710 cx18_call_all(cx, core, s_std, cx->std);
Andy Walls3b6fe582008-06-21 08:36:31 -0300711 return 0;
712}
713
714static int cx18_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
715{
716 struct cx18_open_id *id = fh;
717 struct cx18 *cx = id->cx;
718 int ret;
719
Hans Verkuilffb48772010-05-01 08:03:24 -0300720 ret = v4l2_prio_check(&cx->prio, id->prio);
Andy Walls3b6fe582008-06-21 08:36:31 -0300721 if (ret)
722 return ret;
723
Andy Walls3b6fe582008-06-21 08:36:31 -0300724 if (vt->index != 0)
725 return -EINVAL;
726
Andy Wallsff2a2002009-02-20 23:52:13 -0300727 cx18_call_all(cx, tuner, s_tuner, vt);
Andy Walls3b6fe582008-06-21 08:36:31 -0300728 return 0;
729}
730
731static int cx18_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
732{
733 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
734
Andy Walls3b6fe582008-06-21 08:36:31 -0300735 if (vt->index != 0)
736 return -EINVAL;
737
Andy Wallsff2a2002009-02-20 23:52:13 -0300738 cx18_call_all(cx, tuner, g_tuner, vt);
Andy Walls3b6fe582008-06-21 08:36:31 -0300739
740 if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
741 strlcpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
742 vt->type = V4L2_TUNER_RADIO;
743 } else {
744 strlcpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
745 vt->type = V4L2_TUNER_ANALOG_TV;
746 }
747
748 return 0;
749}
750
751static int cx18_g_sliced_vbi_cap(struct file *file, void *fh,
752 struct v4l2_sliced_vbi_cap *cap)
753{
Andy Walls302df972009-01-31 00:33:02 -0300754 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
755 int set = cx->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
756 int f, l;
757
Andy Wallsc1994082009-02-05 22:37:49 -0300758 if (cap->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
759 return -EINVAL;
760
761 cap->service_set = 0;
762 for (f = 0; f < 2; f++) {
763 for (l = 0; l < 24; l++) {
764 if (valid_service_line(f, l, cx->is_50hz)) {
765 /*
766 * We can find all v4l2 supported vbi services
767 * for the standard, on a valid line for the std
768 */
769 cap->service_lines[f][l] = set;
770 cap->service_set |= set;
771 } else
772 cap->service_lines[f][l] = 0;
Andy Walls302df972009-01-31 00:33:02 -0300773 }
Andy Walls302df972009-01-31 00:33:02 -0300774 }
Andy Wallsc1994082009-02-05 22:37:49 -0300775 for (f = 0; f < 3; f++)
776 cap->reserved[f] = 0;
777 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300778}
779
Andy Walls82acdc82009-12-31 22:09:51 -0300780static int _cx18_process_idx_data(struct cx18_buffer *buf,
781 struct v4l2_enc_idx *idx)
782{
783 int consumed, remaining;
784 struct v4l2_enc_idx_entry *e_idx;
785 struct cx18_enc_idx_entry *e_buf;
786
787 /* Frame type lookup: 1=I, 2=P, 4=B */
788 const int mapping[8] = {
789 -1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P,
790 -1, V4L2_ENC_IDX_FRAME_B, -1, -1, -1
791 };
792
793 /*
794 * Assumption here is that a buf holds an integral number of
795 * struct cx18_enc_idx_entry objects and is properly aligned.
796 * This is enforced by the module options on IDX buffer sizes.
797 */
798 remaining = buf->bytesused - buf->readpos;
799 consumed = 0;
800 e_idx = &idx->entry[idx->entries];
801 e_buf = (struct cx18_enc_idx_entry *) &buf->buf[buf->readpos];
802
803 while (remaining >= sizeof(struct cx18_enc_idx_entry) &&
804 idx->entries < V4L2_ENC_IDX_ENTRIES) {
805
806 e_idx->offset = (((u64) le32_to_cpu(e_buf->offset_high)) << 32)
807 | le32_to_cpu(e_buf->offset_low);
808
809 e_idx->pts = (((u64) (le32_to_cpu(e_buf->pts_high) & 1)) << 32)
810 | le32_to_cpu(e_buf->pts_low);
811
812 e_idx->length = le32_to_cpu(e_buf->length);
813
814 e_idx->flags = mapping[le32_to_cpu(e_buf->flags) & 0x7];
815
816 e_idx->reserved[0] = 0;
817 e_idx->reserved[1] = 0;
818
819 idx->entries++;
820 e_idx = &idx->entry[idx->entries];
821 e_buf++;
822
823 remaining -= sizeof(struct cx18_enc_idx_entry);
824 consumed += sizeof(struct cx18_enc_idx_entry);
825 }
826
827 /* Swallow any partial entries at the end, if there are any */
828 if (remaining > 0 && remaining < sizeof(struct cx18_enc_idx_entry))
829 consumed += remaining;
830
831 buf->readpos += consumed;
832 return consumed;
833}
834
835static int cx18_process_idx_data(struct cx18_stream *s, struct cx18_mdl *mdl,
836 struct v4l2_enc_idx *idx)
837{
838 if (s->type != CX18_ENC_STREAM_TYPE_IDX)
839 return -EINVAL;
840
841 if (mdl->curr_buf == NULL)
842 mdl->curr_buf = list_first_entry(&mdl->buf_list,
843 struct cx18_buffer, list);
844
845 if (list_entry_is_past_end(mdl->curr_buf, &mdl->buf_list, list)) {
846 /*
847 * For some reason we've exhausted the buffers, but the MDL
848 * object still said some data was unread.
849 * Fix that and bail out.
850 */
851 mdl->readpos = mdl->bytesused;
852 return 0;
853 }
854
855 list_for_each_entry_from(mdl->curr_buf, &mdl->buf_list, list) {
856
857 /* Skip any empty buffers in the MDL */
858 if (mdl->curr_buf->readpos >= mdl->curr_buf->bytesused)
859 continue;
860
861 mdl->readpos += _cx18_process_idx_data(mdl->curr_buf, idx);
862
863 /* exit when MDL drained or request satisfied */
864 if (idx->entries >= V4L2_ENC_IDX_ENTRIES ||
865 mdl->curr_buf->readpos < mdl->curr_buf->bytesused ||
866 mdl->readpos >= mdl->bytesused)
867 break;
868 }
869 return 0;
870}
871
Andy Walls3b6fe582008-06-21 08:36:31 -0300872static int cx18_g_enc_index(struct file *file, void *fh,
873 struct v4l2_enc_idx *idx)
874{
Andy Walls82acdc82009-12-31 22:09:51 -0300875 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
876 struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
877 s32 tmp;
878 struct cx18_mdl *mdl;
879
880 if (!cx18_stream_enabled(s)) /* Module options inhibited IDX stream */
881 return -EINVAL;
882
883 /* Compute the best case number of entries we can buffer */
884 tmp = s->buffers -
885 s->bufs_per_mdl * CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN;
886 if (tmp <= 0)
887 tmp = 1;
888 tmp = tmp * s->buf_size / sizeof(struct cx18_enc_idx_entry);
889
890 /* Fill out the header of the return structure */
891 idx->entries = 0;
892 idx->entries_cap = tmp;
893 memset(idx->reserved, 0, sizeof(idx->reserved));
894
895 /* Pull IDX MDLs and buffers from q_full and populate the entries */
896 do {
897 mdl = cx18_dequeue(s, &s->q_full);
898 if (mdl == NULL) /* No more IDX data right now */
899 break;
900
901 /* Extract the Index entry data from the MDL and buffers */
902 cx18_process_idx_data(s, mdl, idx);
903 if (mdl->readpos < mdl->bytesused) {
904 /* We finished with data remaining, push the MDL back */
905 cx18_push(s, mdl, &s->q_full);
906 break;
907 }
908
909 /* We drained this MDL, schedule it to go to the firmware */
910 cx18_enqueue(s, mdl, &s->q_free);
911
912 } while (idx->entries < V4L2_ENC_IDX_ENTRIES);
913
914 /* Tell the work handler to send free IDX MDLs to the firmware */
915 cx18_stream_load_fw_queue(s);
916 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300917}
918
919static int cx18_encoder_cmd(struct file *file, void *fh,
920 struct v4l2_encoder_cmd *enc)
921{
922 struct cx18_open_id *id = fh;
923 struct cx18 *cx = id->cx;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300924 u32 h;
Andy Walls3b6fe582008-06-21 08:36:31 -0300925
Andy Walls3b6fe582008-06-21 08:36:31 -0300926 switch (enc->cmd) {
927 case V4L2_ENC_CMD_START:
928 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
929 enc->flags = 0;
930 return cx18_start_capture(id);
931
932 case V4L2_ENC_CMD_STOP:
933 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
934 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
935 cx18_stop_capture(id,
936 enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
937 break;
938
939 case V4L2_ENC_CMD_PAUSE:
940 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
941 enc->flags = 0;
942 if (!atomic_read(&cx->ana_capturing))
943 return -EPERM;
944 if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
945 return 0;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300946 h = cx18_find_handle(cx);
947 if (h == CX18_INVALID_TASK_HANDLE) {
948 CX18_ERR("Can't find valid task handle for "
949 "V4L2_ENC_CMD_PAUSE\n");
950 return -EBADFD;
951 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300952 cx18_mute(cx);
Andy Wallsd3c5e702008-08-23 16:42:29 -0300953 cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, h);
Andy Walls3b6fe582008-06-21 08:36:31 -0300954 break;
955
956 case V4L2_ENC_CMD_RESUME:
957 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
958 enc->flags = 0;
959 if (!atomic_read(&cx->ana_capturing))
960 return -EPERM;
961 if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
962 return 0;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300963 h = cx18_find_handle(cx);
964 if (h == CX18_INVALID_TASK_HANDLE) {
965 CX18_ERR("Can't find valid task handle for "
966 "V4L2_ENC_CMD_RESUME\n");
967 return -EBADFD;
968 }
969 cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, h);
Andy Walls3b6fe582008-06-21 08:36:31 -0300970 cx18_unmute(cx);
971 break;
972
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300973 default:
Andy Walls3b6fe582008-06-21 08:36:31 -0300974 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
975 return -EINVAL;
976 }
977 return 0;
978}
979
980static int cx18_try_encoder_cmd(struct file *file, void *fh,
981 struct v4l2_encoder_cmd *enc)
982{
983 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
984
Andy Walls3b6fe582008-06-21 08:36:31 -0300985 switch (enc->cmd) {
986 case V4L2_ENC_CMD_START:
987 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
988 enc->flags = 0;
989 break;
990
991 case V4L2_ENC_CMD_STOP:
992 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
993 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
994 break;
995
996 case V4L2_ENC_CMD_PAUSE:
997 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
998 enc->flags = 0;
999 break;
1000
1001 case V4L2_ENC_CMD_RESUME:
1002 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1003 enc->flags = 0;
1004 break;
1005
1006 default:
1007 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1008 return -EINVAL;
1009 }
1010 return 0;
1011}
1012
1013static int cx18_log_status(struct file *file, void *fh)
1014{
1015 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
1016 struct v4l2_input vidin;
1017 struct v4l2_audio audin;
1018 int i;
1019
Andy Wallse0f28b62009-01-10 14:13:46 -03001020 CX18_INFO("================= START STATUS CARD #%d "
Andy Walls5811cf92009-02-14 17:08:37 -03001021 "=================\n", cx->instance);
Andy Wallse0f28b62009-01-10 14:13:46 -03001022 CX18_INFO("Version: %s Card: %s\n", CX18_VERSION, cx->card_name);
Andy Walls3b6fe582008-06-21 08:36:31 -03001023 if (cx->hw_flags & CX18_HW_TVEEPROM) {
1024 struct tveeprom tv;
1025
1026 cx18_read_eeprom(cx, &tv);
1027 }
Andy Wallsff2a2002009-02-20 23:52:13 -03001028 cx18_call_all(cx, core, log_status);
Andy Walls3b6fe582008-06-21 08:36:31 -03001029 cx18_get_input(cx, cx->active_input, &vidin);
1030 cx18_get_audio_input(cx, cx->audio_input, &audin);
1031 CX18_INFO("Video Input: %s\n", vidin.name);
1032 CX18_INFO("Audio Input: %s\n", audin.name);
Andy Walls8abdd002008-07-13 19:05:25 -03001033 mutex_lock(&cx->gpio_lock);
Hans Verkuil3d66c402008-06-21 11:19:34 -03001034 CX18_INFO("GPIO: direction 0x%08x, value 0x%08x\n",
1035 cx->gpio_dir, cx->gpio_val);
Andy Walls8abdd002008-07-13 19:05:25 -03001036 mutex_unlock(&cx->gpio_lock);
Andy Walls3b6fe582008-06-21 08:36:31 -03001037 CX18_INFO("Tuner: %s\n",
1038 test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ? "Radio" : "TV");
Andy Walls5811cf92009-02-14 17:08:37 -03001039 cx2341x_log_status(&cx->params, cx->v4l2_dev.name);
Andy Walls3b6fe582008-06-21 08:36:31 -03001040 CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
1041 for (i = 0; i < CX18_MAX_STREAMS; i++) {
1042 struct cx18_stream *s = &cx->streams[i];
1043
Andy Walls3d059132009-01-10 21:54:39 -03001044 if (s->video_dev == NULL || s->buffers == 0)
Andy Walls3b6fe582008-06-21 08:36:31 -03001045 continue;
1046 CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
1047 s->name, s->s_flags,
Andy Walls52fcb3e2009-11-08 23:45:24 -03001048 atomic_read(&s->q_full.depth) * s->bufs_per_mdl * 100
1049 / s->buffers,
Andy Walls3b6fe582008-06-21 08:36:31 -03001050 (s->buffers * s->buf_size) / 1024, s->buffers);
1051 }
1052 CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
1053 (long long)cx->mpg_data_received,
1054 (long long)cx->vbi_data_inserted);
Andy Walls5811cf92009-02-14 17:08:37 -03001055 CX18_INFO("================== END STATUS CARD #%d "
1056 "==================\n", cx->instance);
Andy Walls3b6fe582008-06-21 08:36:31 -03001057 return 0;
1058}
1059
Hans Verkuil069b7472008-12-30 07:04:34 -03001060static long cx18_default(struct file *file, void *fh, int cmd, void *arg)
Andy Walls3b6fe582008-06-21 08:36:31 -03001061{
1062 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
1063
1064 switch (cmd) {
Andy Walls02fa2722008-07-13 19:30:15 -03001065 case VIDIOC_INT_RESET: {
1066 u32 val = *(u32 *)arg;
1067
1068 if ((val == 0) || (val & 0x01))
Andy Wallseefe1012009-02-21 18:42:49 -03001069 cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL, core, reset,
1070 (u32) CX18_GPIO_RESET_Z8F0811);
Andy Walls02fa2722008-07-13 19:30:15 -03001071 break;
1072 }
1073
Andy Walls3b6fe582008-06-21 08:36:31 -03001074 default:
Andy Walls3b6fe582008-06-21 08:36:31 -03001075 return -EINVAL;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001076 }
1077 return 0;
1078}
1079
Hans Verkuil069b7472008-12-30 07:04:34 -03001080long cx18_v4l2_ioctl(struct file *filp, unsigned int cmd,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001081 unsigned long arg)
1082{
Hans Verkuil37f89f92008-06-22 11:57:31 -03001083 struct video_device *vfd = video_devdata(filp);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001084 struct cx18_open_id *id = (struct cx18_open_id *)filp->private_data;
1085 struct cx18 *cx = id->cx;
Hans Verkuil069b7472008-12-30 07:04:34 -03001086 long res;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001087
1088 mutex_lock(&cx->serialize_lock);
Hans Verkuil37f89f92008-06-22 11:57:31 -03001089
Andy Wallsff2a2002009-02-20 23:52:13 -03001090 /* FIXME - consolidate v4l2_prio_check()'s here */
1091
Hans Verkuil37f89f92008-06-22 11:57:31 -03001092 if (cx18_debug & CX18_DBGFLG_IOCTL)
1093 vfd->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
Hans Verkuilbec43662008-12-30 06:58:20 -03001094 res = video_ioctl2(filp, cmd, arg);
Hans Verkuil37f89f92008-06-22 11:57:31 -03001095 vfd->debug = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001096 mutex_unlock(&cx->serialize_lock);
1097 return res;
1098}
Andy Walls3b6fe582008-06-21 08:36:31 -03001099
Hans Verkuila3998102008-07-21 02:57:38 -03001100static const struct v4l2_ioctl_ops cx18_ioctl_ops = {
1101 .vidioc_querycap = cx18_querycap,
1102 .vidioc_g_priority = cx18_g_priority,
1103 .vidioc_s_priority = cx18_s_priority,
1104 .vidioc_s_audio = cx18_s_audio,
1105 .vidioc_g_audio = cx18_g_audio,
1106 .vidioc_enumaudio = cx18_enumaudio,
1107 .vidioc_enum_input = cx18_enum_input,
1108 .vidioc_cropcap = cx18_cropcap,
1109 .vidioc_s_crop = cx18_s_crop,
1110 .vidioc_g_crop = cx18_g_crop,
1111 .vidioc_g_input = cx18_g_input,
1112 .vidioc_s_input = cx18_s_input,
1113 .vidioc_g_frequency = cx18_g_frequency,
1114 .vidioc_s_frequency = cx18_s_frequency,
1115 .vidioc_s_tuner = cx18_s_tuner,
1116 .vidioc_g_tuner = cx18_g_tuner,
1117 .vidioc_g_enc_index = cx18_g_enc_index,
1118 .vidioc_g_std = cx18_g_std,
1119 .vidioc_s_std = cx18_s_std,
1120 .vidioc_log_status = cx18_log_status,
1121 .vidioc_enum_fmt_vid_cap = cx18_enum_fmt_vid_cap,
1122 .vidioc_encoder_cmd = cx18_encoder_cmd,
1123 .vidioc_try_encoder_cmd = cx18_try_encoder_cmd,
1124 .vidioc_g_fmt_vid_cap = cx18_g_fmt_vid_cap,
1125 .vidioc_g_fmt_vbi_cap = cx18_g_fmt_vbi_cap,
1126 .vidioc_g_fmt_sliced_vbi_cap = cx18_g_fmt_sliced_vbi_cap,
1127 .vidioc_s_fmt_vid_cap = cx18_s_fmt_vid_cap,
1128 .vidioc_s_fmt_vbi_cap = cx18_s_fmt_vbi_cap,
1129 .vidioc_s_fmt_sliced_vbi_cap = cx18_s_fmt_sliced_vbi_cap,
1130 .vidioc_try_fmt_vid_cap = cx18_try_fmt_vid_cap,
1131 .vidioc_try_fmt_vbi_cap = cx18_try_fmt_vbi_cap,
1132 .vidioc_try_fmt_sliced_vbi_cap = cx18_try_fmt_sliced_vbi_cap,
1133 .vidioc_g_sliced_vbi_cap = cx18_g_sliced_vbi_cap,
1134 .vidioc_g_chip_ident = cx18_g_chip_ident,
1135#ifdef CONFIG_VIDEO_ADV_DEBUG
1136 .vidioc_g_register = cx18_g_register,
1137 .vidioc_s_register = cx18_s_register,
1138#endif
1139 .vidioc_default = cx18_default,
1140 .vidioc_queryctrl = cx18_queryctrl,
1141 .vidioc_querymenu = cx18_querymenu,
1142 .vidioc_g_ext_ctrls = cx18_g_ext_ctrls,
1143 .vidioc_s_ext_ctrls = cx18_s_ext_ctrls,
1144 .vidioc_try_ext_ctrls = cx18_try_ext_ctrls,
1145};
1146
Andy Walls3b6fe582008-06-21 08:36:31 -03001147void cx18_set_funcs(struct video_device *vdev)
1148{
Hans Verkuila3998102008-07-21 02:57:38 -03001149 vdev->ioctl_ops = &cx18_ioctl_ops;
Andy Walls3b6fe582008-06-21 08:36:31 -03001150}