]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/media/video/cx18/cx18-ioctl.c
[media] tuner-core: power up tuner when called with s_power(1)
[mirror_ubuntu-hirsute-kernel.git] / drivers / media / video / cx18 / cx18-ioctl.c
CommitLineData
1c1e45d1
HV
1/*
2 * cx18 ioctl system call
3 *
4 * Derived from ivtv-ioctl.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
6afdeaf8 7 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.net>
1c1e45d1
HV
8 *
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"
b1526421 26#include "cx18-io.h"
1c1e45d1
HV
27#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>
1c1e45d1 43
aed6abd6 44u16 cx18_service2vbi(int type)
1c1e45d1
HV
45{
46 switch (type) {
47 case V4L2_SLICED_TELETEXT_B:
48 return CX18_SLICED_TYPE_TELETEXT_B;
49 case V4L2_SLICED_CAPTION_525:
50 return CX18_SLICED_TYPE_CAPTION_525;
51 case V4L2_SLICED_WSS_625:
52 return CX18_SLICED_TYPE_WSS_625;
53 case V4L2_SLICED_VPS:
54 return CX18_SLICED_TYPE_VPS;
55 default:
56 return 0;
57 }
58}
59
c1994084 60/* Check if VBI services are allowed on the (field, line) for the video std */
1c1e45d1
HV
61static int valid_service_line(int field, int line, int is_pal)
62{
c1994084
AW
63 return (is_pal && line >= 6 &&
64 ((field == 0 && line <= 23) || (field == 1 && line <= 22))) ||
1c1e45d1
HV
65 (!is_pal && line >= 10 && line < 22);
66}
67
c1994084
AW
68/*
69 * For a (field, line, std) and inbound potential set of services for that line,
70 * return the first valid service of those passed in the incoming set for that
71 * line in priority order:
72 * CC, VPS, or WSS over TELETEXT for well known lines
73 * TELETEXT, before VPS, before CC, before WSS, for other lines
74 */
1c1e45d1
HV
75static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
76{
77 u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
78 int i;
79
80 set = set & valid_set;
81 if (set == 0 || !valid_service_line(field, line, is_pal))
82 return 0;
83 if (!is_pal) {
84 if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
85 return V4L2_SLICED_CAPTION_525;
86 } else {
87 if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
88 return V4L2_SLICED_VPS;
89 if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
90 return V4L2_SLICED_WSS_625;
91 if (line == 23)
92 return 0;
93 }
94 for (i = 0; i < 32; i++) {
95 if ((1 << i) & set)
96 return 1 << i;
97 }
98 return 0;
99}
100
c1994084
AW
101/*
102 * Expand the service_set of *fmt into valid service_lines for the std,
103 * and clear the passed in fmt->service_set
104 */
aed6abd6 105void cx18_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
1c1e45d1
HV
106{
107 u16 set = fmt->service_set;
108 int f, l;
109
110 fmt->service_set = 0;
111 for (f = 0; f < 2; f++) {
112 for (l = 0; l < 24; l++)
113 fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
114 }
115}
116
c1994084
AW
117/*
118 * Sanitize the service_lines in *fmt per the video std, and return 1
119 * if any service_line is left as valid after santization
120 */
302df970
AW
121static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
122{
123 int f, l;
124 u16 set = 0;
125
126 for (f = 0; f < 2; f++) {
127 for (l = 0; l < 24; l++) {
128 fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
129 set |= fmt->service_lines[f][l];
130 }
131 }
132 return set != 0;
133}
1c1e45d1 134
c1994084 135/* Compute the service_set from the assumed valid service_lines of *fmt */
aed6abd6 136u16 cx18_get_service_set(struct v4l2_sliced_vbi_format *fmt)
1c1e45d1
HV
137{
138 int f, l;
139 u16 set = 0;
140
141 for (f = 0; f < 2; f++) {
142 for (l = 0; l < 24; l++)
143 set |= fmt->service_lines[f][l];
144 }
145 return set;
146}
147
3b6fe58f
AW
148static int cx18_g_fmt_vid_cap(struct file *file, void *fh,
149 struct v4l2_format *fmt)
150{
0b5f265a 151 struct cx18_open_id *id = fh2id(fh);
3b6fe58f 152 struct cx18 *cx = id->cx;
b7101de3 153 struct cx18_stream *s = &cx->streams[id->type];
0b5a30e9 154 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1c1e45d1 155
a75b9be1
HV
156 pixfmt->width = cx->cxhdl.width;
157 pixfmt->height = cx->cxhdl.height;
0b5a30e9
HV
158 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
159 pixfmt->field = V4L2_FIELD_INTERLACED;
160 pixfmt->priv = 0;
3b6fe58f 161 if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
b7101de3 162 pixfmt->pixelformat = s->pixelformat;
1bf5842f
SF
163 /* HM12 YUV size is (Y=(h*720) + UV=(h*(720/2)))
164 UYUV YUV size is (Y=(h*720) + UV=(h*(720))) */
165 if (s->pixelformat == V4L2_PIX_FMT_HM12)
166 pixfmt->sizeimage = pixfmt->height * 720 * 3 / 2;
167 else
168 pixfmt->sizeimage = pixfmt->height * 720 * 2;
0b5a30e9 169 pixfmt->bytesperline = 720;
3b6fe58f 170 } else {
0b5a30e9
HV
171 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
172 pixfmt->sizeimage = 128 * 1024;
173 pixfmt->bytesperline = 0;
3b6fe58f
AW
174 }
175 return 0;
176}
1c1e45d1 177
3b6fe58f
AW
178static int cx18_g_fmt_vbi_cap(struct file *file, void *fh,
179 struct v4l2_format *fmt)
180{
0b5f265a 181 struct cx18 *cx = fh2id(fh)->cx;
0b5a30e9 182 struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
1c1e45d1 183
0b5a30e9 184 vbifmt->sampling_rate = 27000000;
c1994084 185 vbifmt->offset = 248; /* FIXME - slightly wrong for both 50 & 60 Hz */
302df970 186 vbifmt->samples_per_line = vbi_active_samples - 4;
0b5a30e9
HV
187 vbifmt->sample_format = V4L2_PIX_FMT_GREY;
188 vbifmt->start[0] = cx->vbi.start[0];
189 vbifmt->start[1] = cx->vbi.start[1];
190 vbifmt->count[0] = vbifmt->count[1] = cx->vbi.count;
191 vbifmt->flags = 0;
192 vbifmt->reserved[0] = 0;
193 vbifmt->reserved[1] = 0;
1c1e45d1
HV
194 return 0;
195}
196
3b6fe58f
AW
197static int cx18_g_fmt_sliced_vbi_cap(struct file *file, void *fh,
198 struct v4l2_format *fmt)
1c1e45d1 199{
0b5f265a 200 struct cx18 *cx = fh2id(fh)->cx;
302df970
AW
201 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
202
c1994084 203 /* sane, V4L2 spec compliant, defaults */
302df970
AW
204 vbifmt->reserved[0] = 0;
205 vbifmt->reserved[1] = 0;
206 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
207 memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
c1994084
AW
208 vbifmt->service_set = 0;
209
210 /*
211 * Fetch the configured service_lines and total service_set from the
212 * digitizer/slicer. Note, cx18_av_vbi() wipes the passed in
213 * fmt->fmt.sliced under valid calling conditions
214 */
add632cd 215 if (v4l2_subdev_call(cx->sd_av, vbi, g_sliced_fmt, &fmt->fmt.sliced))
c1994084 216 return -EINVAL;
302df970 217
c1994084
AW
218 /* Ensure V4L2 spec compliant output */
219 vbifmt->reserved[0] = 0;
220 vbifmt->reserved[1] = 0;
221 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
302df970
AW
222 vbifmt->service_set = cx18_get_service_set(vbifmt);
223 return 0;
3b6fe58f 224}
1c1e45d1 225
3b6fe58f
AW
226static int cx18_try_fmt_vid_cap(struct file *file, void *fh,
227 struct v4l2_format *fmt)
228{
0b5f265a 229 struct cx18_open_id *id = fh2id(fh);
3b6fe58f 230 struct cx18 *cx = id->cx;
3b6fe58f
AW
231 int w = fmt->fmt.pix.width;
232 int h = fmt->fmt.pix.height;
a4a78718 233 int min_h = 2;
1c1e45d1 234
3b6fe58f 235 w = min(w, 720);
a4a78718
HV
236 w = max(w, 2);
237 if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
238 /* YUV height must be a multiple of 32 */
239 h &= ~0x1f;
240 min_h = 32;
241 }
3b6fe58f 242 h = min(h, cx->is_50hz ? 576 : 480);
a4a78718
HV
243 h = max(h, min_h);
244
3b6fe58f
AW
245 fmt->fmt.pix.width = w;
246 fmt->fmt.pix.height = h;
247 return 0;
248}
1c1e45d1 249
3b6fe58f
AW
250static int cx18_try_fmt_vbi_cap(struct file *file, void *fh,
251 struct v4l2_format *fmt)
252{
3b6fe58f
AW
253 return cx18_g_fmt_vbi_cap(file, fh, fmt);
254}
255
256static int cx18_try_fmt_sliced_vbi_cap(struct file *file, void *fh,
257 struct v4l2_format *fmt)
258{
0b5f265a 259 struct cx18 *cx = fh2id(fh)->cx;
302df970
AW
260 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
261
262 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
263 vbifmt->reserved[0] = 0;
264 vbifmt->reserved[1] = 0;
265
c1994084 266 /* If given a service set, expand it validly & clear passed in set */
302df970
AW
267 if (vbifmt->service_set)
268 cx18_expand_service_set(vbifmt, cx->is_50hz);
c1994084
AW
269 /* Sanitize the service_lines, and compute the new set if any valid */
270 if (check_service_set(vbifmt, cx->is_50hz))
271 vbifmt->service_set = cx18_get_service_set(vbifmt);
302df970 272 return 0;
3b6fe58f
AW
273}
274
275static int cx18_s_fmt_vid_cap(struct file *file, void *fh,
276 struct v4l2_format *fmt)
277{
0b5f265a 278 struct cx18_open_id *id = fh2id(fh);
3b6fe58f 279 struct cx18 *cx = id->cx;
9e36eabc 280 struct v4l2_mbus_framefmt mbus_fmt;
b7101de3 281 struct cx18_stream *s = &cx->streams[id->type];
3b6fe58f 282 int ret;
effc3466 283 int w, h;
3b6fe58f 284
3b6fe58f
AW
285 ret = cx18_try_fmt_vid_cap(file, fh, fmt);
286 if (ret)
287 return ret;
effc3466
HV
288 w = fmt->fmt.pix.width;
289 h = fmt->fmt.pix.height;
1c1e45d1 290
1bf5842f
SF
291 if (cx->cxhdl.width == w && cx->cxhdl.height == h &&
292 s->pixelformat == fmt->fmt.pix.pixelformat)
1c1e45d1 293 return 0;
3b6fe58f
AW
294
295 if (atomic_read(&cx->ana_capturing) > 0)
1c1e45d1 296 return -EBUSY;
3b6fe58f 297
1bf5842f
SF
298 s->pixelformat = fmt->fmt.pix.pixelformat;
299
a75b9be1
HV
300 mbus_fmt.width = cx->cxhdl.width = w;
301 mbus_fmt.height = cx->cxhdl.height = h;
9e36eabc
HV
302 mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
303 v4l2_subdev_call(cx->sd_av, video, s_mbus_fmt, &mbus_fmt);
3b6fe58f 304 return cx18_g_fmt_vid_cap(file, fh, fmt);
1c1e45d1
HV
305}
306
3b6fe58f
AW
307static int cx18_s_fmt_vbi_cap(struct file *file, void *fh,
308 struct v4l2_format *fmt)
1c1e45d1 309{
0b5f265a 310 struct cx18_open_id *id = fh2id(fh);
1c1e45d1 311 struct cx18 *cx = id->cx;
3b6fe58f 312 int ret;
1c1e45d1 313
dcc0ef88
AW
314 /*
315 * Changing the Encoder's Raw VBI parameters won't have any effect
316 * if any analog capture is ongoing
317 */
318 if (!cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
3b6fe58f 319 return -EBUSY;
1c1e45d1 320
c1994084
AW
321 /*
322 * Set the digitizer registers for raw active VBI.
25985edc 323 * Note cx18_av_vbi_wipes out a lot of the passed in fmt under valid
c1994084
AW
324 * calling conditions
325 */
add632cd 326 ret = v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &fmt->fmt.vbi);
c1994084
AW
327 if (ret)
328 return ret;
329
330 /* Store our new v4l2 (non-)sliced VBI state */
3b6fe58f 331 cx->vbi.sliced_in->service_set = 0;
dd073434 332 cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
c1994084 333
3b6fe58f
AW
334 return cx18_g_fmt_vbi_cap(file, fh, fmt);
335}
336
337static int cx18_s_fmt_sliced_vbi_cap(struct file *file, void *fh,
338 struct v4l2_format *fmt)
339{
0b5f265a 340 struct cx18_open_id *id = fh2id(fh);
302df970
AW
341 struct cx18 *cx = id->cx;
342 int ret;
343 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
344
c1994084 345 cx18_try_fmt_sliced_vbi_cap(file, fh, fmt);
302df970 346
dcc0ef88
AW
347 /*
348 * Changing the Encoder's Raw VBI parameters won't have any effect
349 * if any analog capture is ongoing
350 */
351 if (cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
302df970 352 return -EBUSY;
dcc0ef88 353
c1994084
AW
354 /*
355 * Set the service_lines requested in the digitizer/slicer registers.
356 * Note, cx18_av_vbi() wipes some "impossible" service lines in the
357 * passed in fmt->fmt.sliced under valid calling conditions
358 */
add632cd 359 ret = v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &fmt->fmt.sliced);
c1994084
AW
360 if (ret)
361 return ret;
362 /* Store our current v4l2 sliced VBI settings */
302df970 363 cx->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
302df970
AW
364 memcpy(cx->vbi.sliced_in, vbifmt, sizeof(*cx->vbi.sliced_in));
365 return 0;
3b6fe58f
AW
366}
367
368static int cx18_g_chip_ident(struct file *file, void *fh,
aecde8b5 369 struct v4l2_dbg_chip_ident *chip)
3b6fe58f 370{
0b5f265a 371 struct cx18 *cx = fh2id(fh)->cx;
ff2a2001 372 int err = 0;
3b6fe58f 373
3b6fe58f
AW
374 chip->ident = V4L2_IDENT_NONE;
375 chip->revision = 0;
ff2a2001
AW
376 switch (chip->match.type) {
377 case V4L2_CHIP_MATCH_HOST:
378 switch (chip->match.addr) {
379 case 0:
380 chip->ident = V4L2_IDENT_CX23418;
381 chip->revision = cx18_read_reg(cx, 0xC72028);
382 break;
383 case 1:
384 /*
385 * The A/V decoder is always present, but in the rare
386 * case that the card doesn't have analog, we don't
387 * use it. We find it w/o using the cx->sd_av pointer
388 */
389 cx18_call_hw(cx, CX18_HW_418_AV,
390 core, g_chip_ident, chip);
391 break;
392 default:
393 /*
394 * Could return ident = V4L2_IDENT_UNKNOWN if we had
395 * other host chips at higher addresses, but we don't
396 */
397 err = -EINVAL; /* per V4L2 spec */
398 break;
399 }
400 break;
401 case V4L2_CHIP_MATCH_I2C_DRIVER:
402 /* If needed, returns V4L2_IDENT_AMBIGUOUS without extra work */
403 cx18_call_all(cx, core, g_chip_ident, chip);
404 break;
405 case V4L2_CHIP_MATCH_I2C_ADDR:
406 /*
407 * We could return V4L2_IDENT_UNKNOWN, but we don't do the work
408 * to look if a chip is at the address with no driver. That's a
409 * dangerous thing to do with EEPROMs anyway.
410 */
411 cx18_call_all(cx, core, g_chip_ident, chip);
412 break;
413 default:
414 err = -EINVAL;
415 break;
1c1e45d1 416 }
ff2a2001 417 return err;
3b6fe58f
AW
418}
419
36ecd495
HV
420#ifdef CONFIG_VIDEO_ADV_DEBUG
421static int cx18_cxc(struct cx18 *cx, unsigned int cmd, void *arg)
422{
aecde8b5 423 struct v4l2_dbg_register *regs = arg;
36ecd495
HV
424
425 if (!capable(CAP_SYS_ADMIN))
426 return -EPERM;
427 if (regs->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
428 return -EINVAL;
429
aecde8b5 430 regs->size = 4;
ff2a2001 431 if (cmd == VIDIOC_DBG_S_REGISTER)
b1526421 432 cx18_write_enc(cx, regs->val, regs->reg);
ff2a2001
AW
433 else
434 regs->val = cx18_read_enc(cx, regs->reg);
36ecd495
HV
435 return 0;
436}
437
3b6fe58f 438static int cx18_g_register(struct file *file, void *fh,
aecde8b5 439 struct v4l2_dbg_register *reg)
3b6fe58f 440{
0b5f265a 441 struct cx18 *cx = fh2id(fh)->cx;
3b6fe58f 442
aecde8b5 443 if (v4l2_chip_match_host(&reg->match))
3b6fe58f 444 return cx18_cxc(cx, VIDIOC_DBG_G_REGISTER, reg);
ff2a2001
AW
445 /* FIXME - errors shouldn't be ignored */
446 cx18_call_all(cx, core, g_register, reg);
aecde8b5 447 return 0;
3b6fe58f
AW
448}
449
450static int cx18_s_register(struct file *file, void *fh,
aecde8b5 451 struct v4l2_dbg_register *reg)
3b6fe58f 452{
0b5f265a 453 struct cx18 *cx = fh2id(fh)->cx;
3b6fe58f 454
aecde8b5 455 if (v4l2_chip_match_host(&reg->match))
3b6fe58f 456 return cx18_cxc(cx, VIDIOC_DBG_S_REGISTER, reg);
ff2a2001
AW
457 /* FIXME - errors shouldn't be ignored */
458 cx18_call_all(cx, core, s_register, reg);
aecde8b5 459 return 0;
3b6fe58f 460}
36ecd495 461#endif
3b6fe58f 462
3b6fe58f
AW
463static int cx18_querycap(struct file *file, void *fh,
464 struct v4l2_capability *vcap)
465{
0b5f265a 466 struct cx18 *cx = fh2id(fh)->cx;
1c1e45d1 467
3b6fe58f
AW
468 strlcpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
469 strlcpy(vcap->card, cx->card_name, sizeof(vcap->card));
3d05913d
AW
470 snprintf(vcap->bus_info, sizeof(vcap->bus_info),
471 "PCI:%s", pci_name(cx->pci_dev));
3b6fe58f
AW
472 vcap->version = CX18_DRIVER_VERSION; /* version */
473 vcap->capabilities = cx->v4l2_cap; /* capabilities */
474 return 0;
475}
1c1e45d1 476
3b6fe58f
AW
477static int cx18_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
478{
0b5f265a 479 struct cx18 *cx = fh2id(fh)->cx;
1c1e45d1 480
3b6fe58f
AW
481 return cx18_get_audio_input(cx, vin->index, vin);
482}
1c1e45d1 483
3b6fe58f
AW
484static int cx18_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
485{
0b5f265a 486 struct cx18 *cx = fh2id(fh)->cx;
1c1e45d1 487
3b6fe58f
AW
488 vin->index = cx->audio_input;
489 return cx18_get_audio_input(cx, vin->index, vin);
490}
1c1e45d1 491
3b6fe58f
AW
492static int cx18_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
493{
0b5f265a 494 struct cx18 *cx = fh2id(fh)->cx;
1c1e45d1 495
3b6fe58f
AW
496 if (vout->index >= cx->nof_audio_inputs)
497 return -EINVAL;
498 cx->audio_input = vout->index;
499 cx18_audio_set_io(cx);
500 return 0;
501}
1c1e45d1 502
3b6fe58f
AW
503static int cx18_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
504{
0b5f265a 505 struct cx18 *cx = fh2id(fh)->cx;
1c1e45d1 506
3b6fe58f
AW
507 /* set it to defaults from our table */
508 return cx18_get_input(cx, vin->index, vin);
509}
1c1e45d1 510
3b6fe58f
AW
511static int cx18_cropcap(struct file *file, void *fh,
512 struct v4l2_cropcap *cropcap)
513{
0b5f265a 514 struct cx18 *cx = fh2id(fh)->cx;
1c1e45d1 515
3b6fe58f
AW
516 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
517 return -EINVAL;
518 cropcap->bounds.top = cropcap->bounds.left = 0;
519 cropcap->bounds.width = 720;
520 cropcap->bounds.height = cx->is_50hz ? 576 : 480;
521 cropcap->pixelaspect.numerator = cx->is_50hz ? 59 : 10;
522 cropcap->pixelaspect.denominator = cx->is_50hz ? 54 : 11;
523 cropcap->defrect = cropcap->bounds;
524 return 0;
525}
1c1e45d1 526
3b6fe58f
AW
527static int cx18_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
528{
0b5f265a 529 struct cx18_open_id *id = fh2id(fh);
3b6fe58f 530 struct cx18 *cx = id->cx;
1c1e45d1 531
3b6fe58f
AW
532 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
533 return -EINVAL;
fa3e7036
AW
534 CX18_DEBUG_WARN("VIDIOC_S_CROP not implemented\n");
535 return -EINVAL;
3b6fe58f 536}
1c1e45d1 537
3b6fe58f
AW
538static int cx18_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
539{
0b5f265a 540 struct cx18 *cx = fh2id(fh)->cx;
1c1e45d1 541
3b6fe58f
AW
542 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
543 return -EINVAL;
fa3e7036
AW
544 CX18_DEBUG_WARN("VIDIOC_G_CROP not implemented\n");
545 return -EINVAL;
3b6fe58f
AW
546}
547
548static int cx18_enum_fmt_vid_cap(struct file *file, void *fh,
549 struct v4l2_fmtdesc *fmt)
550{
1bf5842f
SF
551 static const struct v4l2_fmtdesc formats[] = {
552 { 0, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
553 "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12, { 0, 0, 0, 0 }
554 },
555 { 1, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FMT_FLAG_COMPRESSED,
556 "MPEG", V4L2_PIX_FMT_MPEG, { 0, 0, 0, 0 }
557 },
558 { 2, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
559 "UYVY 4:2:2", V4L2_PIX_FMT_UYVY, { 0, 0, 0, 0 }
560 },
561 };
562
b7101de3 563 if (fmt->index > ARRAY_SIZE(formats) - 1)
3b6fe58f
AW
564 return -EINVAL;
565 *fmt = formats[fmt->index];
566 return 0;
567}
568
569static int cx18_g_input(struct file *file, void *fh, unsigned int *i)
570{
0b5f265a 571 struct cx18 *cx = fh2id(fh)->cx;
3b6fe58f 572
3b6fe58f
AW
573 *i = cx->active_input;
574 return 0;
575}
576
577int cx18_s_input(struct file *file, void *fh, unsigned int inp)
578{
0b5f265a 579 struct cx18_open_id *id = fh2id(fh);
3b6fe58f 580 struct cx18 *cx = id->cx;
3b6fe58f 581
de81c3c3 582 if (inp >= cx->nof_inputs)
3b6fe58f
AW
583 return -EINVAL;
584
585 if (inp == cx->active_input) {
586 CX18_DEBUG_INFO("Input unchanged\n");
1c1e45d1
HV
587 return 0;
588 }
589
3b6fe58f
AW
590 CX18_DEBUG_INFO("Changing input from %d to %d\n",
591 cx->active_input, inp);
1c1e45d1 592
3b6fe58f
AW
593 cx->active_input = inp;
594 /* Set the audio input to whatever is appropriate for the input type. */
595 cx->audio_input = cx->card->video_inputs[inp].audio_index;
1c1e45d1 596
3b6fe58f
AW
597 /* prevent others from messing with the streams until
598 we're finished changing inputs. */
599 cx18_mute(cx);
600 cx18_video_set_io(cx);
601 cx18_audio_set_io(cx);
602 cx18_unmute(cx);
603 return 0;
604}
1c1e45d1 605
3b6fe58f
AW
606static int cx18_g_frequency(struct file *file, void *fh,
607 struct v4l2_frequency *vf)
608{
0b5f265a 609 struct cx18 *cx = fh2id(fh)->cx;
1c1e45d1 610
3b6fe58f
AW
611 if (vf->tuner != 0)
612 return -EINVAL;
1c1e45d1 613
ff2a2001 614 cx18_call_all(cx, tuner, g_frequency, vf);
3b6fe58f
AW
615 return 0;
616}
1c1e45d1 617
3b6fe58f
AW
618int cx18_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
619{
0b5f265a 620 struct cx18_open_id *id = fh2id(fh);
3b6fe58f 621 struct cx18 *cx = id->cx;
1c1e45d1 622
3b6fe58f
AW
623 if (vf->tuner != 0)
624 return -EINVAL;
1c1e45d1 625
3b6fe58f
AW
626 cx18_mute(cx);
627 CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
ff2a2001 628 cx18_call_all(cx, tuner, s_frequency, vf);
3b6fe58f
AW
629 cx18_unmute(cx);
630 return 0;
631}
1c1e45d1 632
3b6fe58f
AW
633static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
634{
0b5f265a 635 struct cx18 *cx = fh2id(fh)->cx;
1c1e45d1 636
3b6fe58f
AW
637 *std = cx->std;
638 return 0;
639}
1c1e45d1 640
3b6fe58f
AW
641int cx18_s_std(struct file *file, void *fh, v4l2_std_id *std)
642{
0b5f265a 643 struct cx18_open_id *id = fh2id(fh);
3b6fe58f 644 struct cx18 *cx = id->cx;
1c1e45d1 645
3b6fe58f
AW
646 if ((*std & V4L2_STD_ALL) == 0)
647 return -EINVAL;
1c1e45d1 648
3b6fe58f
AW
649 if (*std == cx->std)
650 return 0;
651
652 if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
653 atomic_read(&cx->ana_capturing) > 0) {
654 /* Switching standard would turn off the radio or mess
655 with already running streams, prevent that by
656 returning EBUSY. */
657 return -EBUSY;
1c1e45d1
HV
658 }
659
3b6fe58f
AW
660 cx->std = *std;
661 cx->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
a75b9be1
HV
662 cx->is_50hz = !cx->is_60hz;
663 cx2341x_handler_set_50hz(&cx->cxhdl, cx->is_50hz);
664 cx->cxhdl.width = 720;
665 cx->cxhdl.height = cx->is_50hz ? 576 : 480;
3b6fe58f
AW
666 cx->vbi.count = cx->is_50hz ? 18 : 12;
667 cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
668 cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
3b6fe58f
AW
669 CX18_DEBUG_INFO("Switching standard to %llx.\n",
670 (unsigned long long) cx->std);
671
672 /* Tuner */
f41737ec 673 cx18_call_all(cx, core, s_std, cx->std);
3b6fe58f
AW
674 return 0;
675}
1c1e45d1 676
3b6fe58f
AW
677static int cx18_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
678{
0b5f265a 679 struct cx18_open_id *id = fh2id(fh);
3b6fe58f 680 struct cx18 *cx = id->cx;
3b6fe58f 681
3b6fe58f
AW
682 if (vt->index != 0)
683 return -EINVAL;
684
ff2a2001 685 cx18_call_all(cx, tuner, s_tuner, vt);
3b6fe58f
AW
686 return 0;
687}
688
689static int cx18_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
690{
0b5f265a 691 struct cx18 *cx = fh2id(fh)->cx;
3b6fe58f 692
3b6fe58f
AW
693 if (vt->index != 0)
694 return -EINVAL;
695
ff2a2001 696 cx18_call_all(cx, tuner, g_tuner, vt);
3b6fe58f
AW
697
698 if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
699 strlcpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
700 vt->type = V4L2_TUNER_RADIO;
701 } else {
702 strlcpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
703 vt->type = V4L2_TUNER_ANALOG_TV;
1c1e45d1
HV
704 }
705
3b6fe58f
AW
706 return 0;
707}
708
709static int cx18_g_sliced_vbi_cap(struct file *file, void *fh,
710 struct v4l2_sliced_vbi_cap *cap)
711{
0b5f265a 712 struct cx18 *cx = fh2id(fh)->cx;
302df970
AW
713 int set = cx->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
714 int f, l;
715
c1994084
AW
716 if (cap->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
717 return -EINVAL;
718
719 cap->service_set = 0;
720 for (f = 0; f < 2; f++) {
721 for (l = 0; l < 24; l++) {
722 if (valid_service_line(f, l, cx->is_50hz)) {
723 /*
724 * We can find all v4l2 supported vbi services
725 * for the standard, on a valid line for the std
726 */
727 cap->service_lines[f][l] = set;
728 cap->service_set |= set;
729 } else
730 cap->service_lines[f][l] = 0;
302df970 731 }
302df970 732 }
c1994084
AW
733 for (f = 0; f < 3; f++)
734 cap->reserved[f] = 0;
735 return 0;
3b6fe58f 736}
1c1e45d1 737
82acdc84
AW
738static int _cx18_process_idx_data(struct cx18_buffer *buf,
739 struct v4l2_enc_idx *idx)
740{
741 int consumed, remaining;
742 struct v4l2_enc_idx_entry *e_idx;
743 struct cx18_enc_idx_entry *e_buf;
744
745 /* Frame type lookup: 1=I, 2=P, 4=B */
746 const int mapping[8] = {
747 -1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P,
748 -1, V4L2_ENC_IDX_FRAME_B, -1, -1, -1
749 };
750
751 /*
752 * Assumption here is that a buf holds an integral number of
753 * struct cx18_enc_idx_entry objects and is properly aligned.
754 * This is enforced by the module options on IDX buffer sizes.
755 */
756 remaining = buf->bytesused - buf->readpos;
757 consumed = 0;
758 e_idx = &idx->entry[idx->entries];
759 e_buf = (struct cx18_enc_idx_entry *) &buf->buf[buf->readpos];
760
761 while (remaining >= sizeof(struct cx18_enc_idx_entry) &&
762 idx->entries < V4L2_ENC_IDX_ENTRIES) {
763
764 e_idx->offset = (((u64) le32_to_cpu(e_buf->offset_high)) << 32)
765 | le32_to_cpu(e_buf->offset_low);
766
767 e_idx->pts = (((u64) (le32_to_cpu(e_buf->pts_high) & 1)) << 32)
768 | le32_to_cpu(e_buf->pts_low);
769
770 e_idx->length = le32_to_cpu(e_buf->length);
771
772 e_idx->flags = mapping[le32_to_cpu(e_buf->flags) & 0x7];
773
774 e_idx->reserved[0] = 0;
775 e_idx->reserved[1] = 0;
776
777 idx->entries++;
778 e_idx = &idx->entry[idx->entries];
779 e_buf++;
780
781 remaining -= sizeof(struct cx18_enc_idx_entry);
782 consumed += sizeof(struct cx18_enc_idx_entry);
783 }
784
785 /* Swallow any partial entries at the end, if there are any */
786 if (remaining > 0 && remaining < sizeof(struct cx18_enc_idx_entry))
787 consumed += remaining;
788
789 buf->readpos += consumed;
790 return consumed;
791}
792
793static int cx18_process_idx_data(struct cx18_stream *s, struct cx18_mdl *mdl,
794 struct v4l2_enc_idx *idx)
795{
796 if (s->type != CX18_ENC_STREAM_TYPE_IDX)
797 return -EINVAL;
798
799 if (mdl->curr_buf == NULL)
800 mdl->curr_buf = list_first_entry(&mdl->buf_list,
801 struct cx18_buffer, list);
802
803 if (list_entry_is_past_end(mdl->curr_buf, &mdl->buf_list, list)) {
804 /*
805 * For some reason we've exhausted the buffers, but the MDL
806 * object still said some data was unread.
807 * Fix that and bail out.
808 */
809 mdl->readpos = mdl->bytesused;
810 return 0;
811 }
812
813 list_for_each_entry_from(mdl->curr_buf, &mdl->buf_list, list) {
814
815 /* Skip any empty buffers in the MDL */
816 if (mdl->curr_buf->readpos >= mdl->curr_buf->bytesused)
817 continue;
818
819 mdl->readpos += _cx18_process_idx_data(mdl->curr_buf, idx);
820
821 /* exit when MDL drained or request satisfied */
822 if (idx->entries >= V4L2_ENC_IDX_ENTRIES ||
823 mdl->curr_buf->readpos < mdl->curr_buf->bytesused ||
824 mdl->readpos >= mdl->bytesused)
825 break;
826 }
827 return 0;
828}
829
3b6fe58f
AW
830static int cx18_g_enc_index(struct file *file, void *fh,
831 struct v4l2_enc_idx *idx)
832{
0b5f265a 833 struct cx18 *cx = fh2id(fh)->cx;
82acdc84
AW
834 struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
835 s32 tmp;
836 struct cx18_mdl *mdl;
837
838 if (!cx18_stream_enabled(s)) /* Module options inhibited IDX stream */
839 return -EINVAL;
840
841 /* Compute the best case number of entries we can buffer */
842 tmp = s->buffers -
843 s->bufs_per_mdl * CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN;
844 if (tmp <= 0)
845 tmp = 1;
846 tmp = tmp * s->buf_size / sizeof(struct cx18_enc_idx_entry);
847
848 /* Fill out the header of the return structure */
849 idx->entries = 0;
850 idx->entries_cap = tmp;
851 memset(idx->reserved, 0, sizeof(idx->reserved));
852
853 /* Pull IDX MDLs and buffers from q_full and populate the entries */
854 do {
855 mdl = cx18_dequeue(s, &s->q_full);
856 if (mdl == NULL) /* No more IDX data right now */
857 break;
858
859 /* Extract the Index entry data from the MDL and buffers */
860 cx18_process_idx_data(s, mdl, idx);
861 if (mdl->readpos < mdl->bytesused) {
862 /* We finished with data remaining, push the MDL back */
863 cx18_push(s, mdl, &s->q_full);
864 break;
865 }
866
867 /* We drained this MDL, schedule it to go to the firmware */
868 cx18_enqueue(s, mdl, &s->q_free);
869
870 } while (idx->entries < V4L2_ENC_IDX_ENTRIES);
871
872 /* Tell the work handler to send free IDX MDLs to the firmware */
873 cx18_stream_load_fw_queue(s);
874 return 0;
3b6fe58f 875}
1c1e45d1 876
b7101de3
ST
877static struct videobuf_queue *cx18_vb_queue(struct cx18_open_id *id)
878{
879 struct videobuf_queue *q = NULL;
1bf5842f
SF
880 struct cx18 *cx = id->cx;
881 struct cx18_stream *s = &cx->streams[id->type];
b7101de3 882
1bf5842f 883 switch (s->vb_type) {
b7101de3 884 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1bf5842f 885 q = &s->vbuf_q;
b7101de3
ST
886 break;
887 case V4L2_BUF_TYPE_VBI_CAPTURE:
888 break;
889 default:
890 break;
891 }
892 return q;
893}
894
895static int cx18_streamon(struct file *file, void *priv,
896 enum v4l2_buf_type type)
897{
898 struct cx18_open_id *id = file->private_data;
899 struct cx18 *cx = id->cx;
900 struct cx18_stream *s = &cx->streams[id->type];
901
902 /* Start the hardware only if we're the video device */
1bf5842f
SF
903 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
904 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
b7101de3
ST
905 return -EINVAL;
906
907 if (id->type != CX18_ENC_STREAM_TYPE_YUV)
908 return -EINVAL;
909
910 /* Establish a buffer timeout */
1bf5842f 911 mod_timer(&s->vb_timeout, msecs_to_jiffies(2000) + jiffies);
b7101de3
ST
912
913 return videobuf_streamon(cx18_vb_queue(id));
914}
915
916static int cx18_streamoff(struct file *file, void *priv,
917 enum v4l2_buf_type type)
918{
919 struct cx18_open_id *id = file->private_data;
1bf5842f
SF
920 struct cx18 *cx = id->cx;
921 struct cx18_stream *s = &cx->streams[id->type];
b7101de3
ST
922
923 /* Start the hardware only if we're the video device */
1bf5842f
SF
924 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
925 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
b7101de3
ST
926 return -EINVAL;
927
928 if (id->type != CX18_ENC_STREAM_TYPE_YUV)
929 return -EINVAL;
930
931 return videobuf_streamoff(cx18_vb_queue(id));
932}
933
934static int cx18_reqbufs(struct file *file, void *priv,
935 struct v4l2_requestbuffers *rb)
936{
937 struct cx18_open_id *id = file->private_data;
1bf5842f
SF
938 struct cx18 *cx = id->cx;
939 struct cx18_stream *s = &cx->streams[id->type];
b7101de3 940
1bf5842f
SF
941 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
942 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
b7101de3
ST
943 return -EINVAL;
944
945 return videobuf_reqbufs(cx18_vb_queue(id), rb);
946}
947
948static int cx18_querybuf(struct file *file, void *priv,
949 struct v4l2_buffer *b)
950{
951 struct cx18_open_id *id = file->private_data;
1bf5842f
SF
952 struct cx18 *cx = id->cx;
953 struct cx18_stream *s = &cx->streams[id->type];
b7101de3 954
1bf5842f
SF
955 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
956 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
b7101de3
ST
957 return -EINVAL;
958
959 return videobuf_querybuf(cx18_vb_queue(id), b);
960}
961
962static int cx18_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
963{
964 struct cx18_open_id *id = file->private_data;
1bf5842f
SF
965 struct cx18 *cx = id->cx;
966 struct cx18_stream *s = &cx->streams[id->type];
b7101de3 967
1bf5842f
SF
968 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
969 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
b7101de3
ST
970 return -EINVAL;
971
972 return videobuf_qbuf(cx18_vb_queue(id), b);
973}
974
975static int cx18_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
976{
977 struct cx18_open_id *id = file->private_data;
1bf5842f
SF
978 struct cx18 *cx = id->cx;
979 struct cx18_stream *s = &cx->streams[id->type];
980
981 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
982 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
b7101de3
ST
983 return -EINVAL;
984
985 return videobuf_dqbuf(cx18_vb_queue(id), b, file->f_flags & O_NONBLOCK);
986}
987
3b6fe58f
AW
988static int cx18_encoder_cmd(struct file *file, void *fh,
989 struct v4l2_encoder_cmd *enc)
990{
0b5f265a 991 struct cx18_open_id *id = fh2id(fh);
3b6fe58f 992 struct cx18 *cx = id->cx;
d3c5e707 993 u32 h;
1c1e45d1 994
3b6fe58f
AW
995 switch (enc->cmd) {
996 case V4L2_ENC_CMD_START:
997 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
998 enc->flags = 0;
999 return cx18_start_capture(id);
1000
1001 case V4L2_ENC_CMD_STOP:
1002 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1003 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1004 cx18_stop_capture(id,
1005 enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1c1e45d1 1006 break;
1c1e45d1 1007
3b6fe58f
AW
1008 case V4L2_ENC_CMD_PAUSE:
1009 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1010 enc->flags = 0;
1011 if (!atomic_read(&cx->ana_capturing))
1012 return -EPERM;
1013 if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
1c1e45d1 1014 return 0;
d3c5e707
AW
1015 h = cx18_find_handle(cx);
1016 if (h == CX18_INVALID_TASK_HANDLE) {
1017 CX18_ERR("Can't find valid task handle for "
1018 "V4L2_ENC_CMD_PAUSE\n");
1019 return -EBADFD;
1020 }
3b6fe58f 1021 cx18_mute(cx);
d3c5e707 1022 cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, h);
3b6fe58f
AW
1023 break;
1024
1025 case V4L2_ENC_CMD_RESUME:
1026 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1027 enc->flags = 0;
1028 if (!atomic_read(&cx->ana_capturing))
1029 return -EPERM;
1030 if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
1031 return 0;
d3c5e707
AW
1032 h = cx18_find_handle(cx);
1033 if (h == CX18_INVALID_TASK_HANDLE) {
1034 CX18_ERR("Can't find valid task handle for "
1035 "V4L2_ENC_CMD_RESUME\n");
1036 return -EBADFD;
1037 }
1038 cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, h);
3b6fe58f
AW
1039 cx18_unmute(cx);
1040 break;
1041
1042 default:
1043 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1c1e45d1
HV
1044 return -EINVAL;
1045 }
3b6fe58f
AW
1046 return 0;
1047}
1c1e45d1 1048
3b6fe58f
AW
1049static int cx18_try_encoder_cmd(struct file *file, void *fh,
1050 struct v4l2_encoder_cmd *enc)
1051{
0b5f265a 1052 struct cx18 *cx = fh2id(fh)->cx;
1c1e45d1 1053
3b6fe58f
AW
1054 switch (enc->cmd) {
1055 case V4L2_ENC_CMD_START:
1056 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1057 enc->flags = 0;
1c1e45d1 1058 break;
1c1e45d1 1059
3b6fe58f
AW
1060 case V4L2_ENC_CMD_STOP:
1061 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1062 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1063 break;
1c1e45d1 1064
3b6fe58f
AW
1065 case V4L2_ENC_CMD_PAUSE:
1066 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1067 enc->flags = 0;
1068 break;
1c1e45d1 1069
3b6fe58f
AW
1070 case V4L2_ENC_CMD_RESUME:
1071 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1072 enc->flags = 0;
1c1e45d1 1073 break;
1c1e45d1
HV
1074
1075 default:
3b6fe58f 1076 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1c1e45d1
HV
1077 return -EINVAL;
1078 }
1079 return 0;
1080}
1081
3b6fe58f 1082static int cx18_log_status(struct file *file, void *fh)
1c1e45d1 1083{
0b5f265a 1084 struct cx18 *cx = fh2id(fh)->cx;
3b6fe58f
AW
1085 struct v4l2_input vidin;
1086 struct v4l2_audio audin;
1087 int i;
1c1e45d1 1088
e0f28b6a 1089 CX18_INFO("================= START STATUS CARD #%d "
5811cf99 1090 "=================\n", cx->instance);
e0f28b6a 1091 CX18_INFO("Version: %s Card: %s\n", CX18_VERSION, cx->card_name);
3b6fe58f
AW
1092 if (cx->hw_flags & CX18_HW_TVEEPROM) {
1093 struct tveeprom tv;
1094
1095 cx18_read_eeprom(cx, &tv);
1096 }
ff2a2001 1097 cx18_call_all(cx, core, log_status);
3b6fe58f
AW
1098 cx18_get_input(cx, cx->active_input, &vidin);
1099 cx18_get_audio_input(cx, cx->audio_input, &audin);
1100 CX18_INFO("Video Input: %s\n", vidin.name);
1101 CX18_INFO("Audio Input: %s\n", audin.name);
8abdd00d 1102 mutex_lock(&cx->gpio_lock);
3d66c405
HV
1103 CX18_INFO("GPIO: direction 0x%08x, value 0x%08x\n",
1104 cx->gpio_dir, cx->gpio_val);
8abdd00d 1105 mutex_unlock(&cx->gpio_lock);
3b6fe58f
AW
1106 CX18_INFO("Tuner: %s\n",
1107 test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ? "Radio" : "TV");
a75b9be1 1108 v4l2_ctrl_handler_log_status(&cx->cxhdl.hdl, cx->v4l2_dev.name);
3b6fe58f
AW
1109 CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
1110 for (i = 0; i < CX18_MAX_STREAMS; i++) {
1111 struct cx18_stream *s = &cx->streams[i];
1112
3d05913d 1113 if (s->video_dev == NULL || s->buffers == 0)
3b6fe58f
AW
1114 continue;
1115 CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
1116 s->name, s->s_flags,
52fcb3ec
AW
1117 atomic_read(&s->q_full.depth) * s->bufs_per_mdl * 100
1118 / s->buffers,
3b6fe58f
AW
1119 (s->buffers * s->buf_size) / 1024, s->buffers);
1120 }
1121 CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
1122 (long long)cx->mpg_data_received,
1123 (long long)cx->vbi_data_inserted);
5811cf99
AW
1124 CX18_INFO("================== END STATUS CARD #%d "
1125 "==================\n", cx->instance);
3b6fe58f
AW
1126 return 0;
1127}
1128
99cd47bc
HV
1129static long cx18_default(struct file *file, void *fh, bool valid_prio,
1130 int cmd, void *arg)
3b6fe58f 1131{
0b5f265a 1132 struct cx18 *cx = fh2id(fh)->cx;
1c1e45d1
HV
1133
1134 switch (cmd) {
02fa272f
AW
1135 case VIDIOC_INT_RESET: {
1136 u32 val = *(u32 *)arg;
1137
1138 if ((val == 0) || (val & 0x01))
eefe1010
AW
1139 cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL, core, reset,
1140 (u32) CX18_GPIO_RESET_Z8F0811);
02fa272f
AW
1141 break;
1142 }
1143
3b6fe58f 1144 default:
1c1e45d1 1145 return -EINVAL;
1c1e45d1
HV
1146 }
1147 return 0;
1148}
1149
069b7479 1150long cx18_v4l2_ioctl(struct file *filp, unsigned int cmd,
1c1e45d1
HV
1151 unsigned long arg)
1152{
37f89f95 1153 struct video_device *vfd = video_devdata(filp);
0b5f265a 1154 struct cx18_open_id *id = file2id(filp);
1c1e45d1 1155 struct cx18 *cx = id->cx;
069b7479 1156 long res;
1c1e45d1
HV
1157
1158 mutex_lock(&cx->serialize_lock);
37f89f95
HV
1159
1160 if (cx18_debug & CX18_DBGFLG_IOCTL)
1161 vfd->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
bec43661 1162 res = video_ioctl2(filp, cmd, arg);
37f89f95 1163 vfd->debug = 0;
1c1e45d1
HV
1164 mutex_unlock(&cx->serialize_lock);
1165 return res;
1166}
3b6fe58f 1167
a399810c
HV
1168static const struct v4l2_ioctl_ops cx18_ioctl_ops = {
1169 .vidioc_querycap = cx18_querycap,
a399810c
HV
1170 .vidioc_s_audio = cx18_s_audio,
1171 .vidioc_g_audio = cx18_g_audio,
1172 .vidioc_enumaudio = cx18_enumaudio,
1173 .vidioc_enum_input = cx18_enum_input,
1174 .vidioc_cropcap = cx18_cropcap,
1175 .vidioc_s_crop = cx18_s_crop,
1176 .vidioc_g_crop = cx18_g_crop,
1177 .vidioc_g_input = cx18_g_input,
1178 .vidioc_s_input = cx18_s_input,
1179 .vidioc_g_frequency = cx18_g_frequency,
1180 .vidioc_s_frequency = cx18_s_frequency,
1181 .vidioc_s_tuner = cx18_s_tuner,
1182 .vidioc_g_tuner = cx18_g_tuner,
1183 .vidioc_g_enc_index = cx18_g_enc_index,
1184 .vidioc_g_std = cx18_g_std,
1185 .vidioc_s_std = cx18_s_std,
1186 .vidioc_log_status = cx18_log_status,
1187 .vidioc_enum_fmt_vid_cap = cx18_enum_fmt_vid_cap,
1188 .vidioc_encoder_cmd = cx18_encoder_cmd,
1189 .vidioc_try_encoder_cmd = cx18_try_encoder_cmd,
1190 .vidioc_g_fmt_vid_cap = cx18_g_fmt_vid_cap,
1191 .vidioc_g_fmt_vbi_cap = cx18_g_fmt_vbi_cap,
1192 .vidioc_g_fmt_sliced_vbi_cap = cx18_g_fmt_sliced_vbi_cap,
1193 .vidioc_s_fmt_vid_cap = cx18_s_fmt_vid_cap,
1194 .vidioc_s_fmt_vbi_cap = cx18_s_fmt_vbi_cap,
1195 .vidioc_s_fmt_sliced_vbi_cap = cx18_s_fmt_sliced_vbi_cap,
1196 .vidioc_try_fmt_vid_cap = cx18_try_fmt_vid_cap,
1197 .vidioc_try_fmt_vbi_cap = cx18_try_fmt_vbi_cap,
1198 .vidioc_try_fmt_sliced_vbi_cap = cx18_try_fmt_sliced_vbi_cap,
1199 .vidioc_g_sliced_vbi_cap = cx18_g_sliced_vbi_cap,
1200 .vidioc_g_chip_ident = cx18_g_chip_ident,
36ecd495 1201#ifdef CONFIG_VIDEO_ADV_DEBUG
a399810c
HV
1202 .vidioc_g_register = cx18_g_register,
1203 .vidioc_s_register = cx18_s_register,
36ecd495 1204#endif
a399810c 1205 .vidioc_default = cx18_default,
b7101de3
ST
1206 .vidioc_streamon = cx18_streamon,
1207 .vidioc_streamoff = cx18_streamoff,
1208 .vidioc_reqbufs = cx18_reqbufs,
1209 .vidioc_querybuf = cx18_querybuf,
1210 .vidioc_qbuf = cx18_qbuf,
1211 .vidioc_dqbuf = cx18_dqbuf,
a399810c
HV
1212};
1213
1214void cx18_set_funcs(struct video_device *vdev)
1215{
1216 vdev->ioctl_ops = &cx18_ioctl_ops;
3b6fe58f 1217}