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