V4L/DVB (8482): videodev: move all ioctl callbacks to a new v4l2_ioctl_ops struct
[GitHub/mt8127/android_kernel_alcatel_ttab.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>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 * 02111-1307 USA
22 */
23
24#include "cx18-driver.h"
25#include "cx18-version.h"
26#include "cx18-mailbox.h"
27#include "cx18-i2c.h"
28#include "cx18-queue.h"
29#include "cx18-fileops.h"
30#include "cx18-vbi.h"
31#include "cx18-audio.h"
32#include "cx18-video.h"
33#include "cx18-streams.h"
34#include "cx18-ioctl.h"
35#include "cx18-gpio.h"
36#include "cx18-controls.h"
37#include "cx18-cards.h"
38#include "cx18-av-core.h"
39#include <media/tveeprom.h>
40#include <media/v4l2-chip-ident.h>
41#include <linux/i2c-id.h>
42
aed6abd6 43u16 cx18_service2vbi(int type)
1c1e45d1
HV
44{
45 switch (type) {
46 case V4L2_SLICED_TELETEXT_B:
47 return CX18_SLICED_TYPE_TELETEXT_B;
48 case V4L2_SLICED_CAPTION_525:
49 return CX18_SLICED_TYPE_CAPTION_525;
50 case V4L2_SLICED_WSS_625:
51 return CX18_SLICED_TYPE_WSS_625;
52 case V4L2_SLICED_VPS:
53 return CX18_SLICED_TYPE_VPS;
54 default:
55 return 0;
56 }
57}
58
59static int valid_service_line(int field, int line, int is_pal)
60{
61 return (is_pal && line >= 6 && (line != 23 || field == 0)) ||
62 (!is_pal && line >= 10 && line < 22);
63}
64
65static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
66{
67 u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
68 int i;
69
70 set = set & valid_set;
71 if (set == 0 || !valid_service_line(field, line, is_pal))
72 return 0;
73 if (!is_pal) {
74 if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
75 return V4L2_SLICED_CAPTION_525;
76 } else {
77 if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
78 return V4L2_SLICED_VPS;
79 if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
80 return V4L2_SLICED_WSS_625;
81 if (line == 23)
82 return 0;
83 }
84 for (i = 0; i < 32; i++) {
85 if ((1 << i) & set)
86 return 1 << i;
87 }
88 return 0;
89}
90
aed6abd6 91void cx18_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
1c1e45d1
HV
92{
93 u16 set = fmt->service_set;
94 int f, l;
95
96 fmt->service_set = 0;
97 for (f = 0; f < 2; f++) {
98 for (l = 0; l < 24; l++)
99 fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
100 }
101}
102
1c1e45d1 103
aed6abd6 104u16 cx18_get_service_set(struct v4l2_sliced_vbi_format *fmt)
1c1e45d1
HV
105{
106 int f, l;
107 u16 set = 0;
108
109 for (f = 0; f < 2; f++) {
110 for (l = 0; l < 24; l++)
111 set |= fmt->service_lines[f][l];
112 }
113 return set;
114}
115
3b6fe58f
AW
116static int cx18_g_fmt_vid_cap(struct file *file, void *fh,
117 struct v4l2_format *fmt)
118{
119 struct cx18_open_id *id = fh;
120 struct cx18 *cx = id->cx;
0b5a30e9 121 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1c1e45d1 122
0b5a30e9
HV
123 pixfmt->width = cx->params.width;
124 pixfmt->height = cx->params.height;
125 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
126 pixfmt->field = V4L2_FIELD_INTERLACED;
127 pixfmt->priv = 0;
3b6fe58f 128 if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
0b5a30e9 129 pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
3b6fe58f 130 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
0b5a30e9
HV
131 pixfmt->sizeimage =
132 pixfmt->height * pixfmt->width +
133 pixfmt->height * (pixfmt->width / 2);
134 pixfmt->bytesperline = 720;
3b6fe58f 135 } else {
0b5a30e9
HV
136 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
137 pixfmt->sizeimage = 128 * 1024;
138 pixfmt->bytesperline = 0;
3b6fe58f
AW
139 }
140 return 0;
141}
1c1e45d1 142
3b6fe58f
AW
143static int cx18_g_fmt_vbi_cap(struct file *file, void *fh,
144 struct v4l2_format *fmt)
145{
146 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
0b5a30e9 147 struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
1c1e45d1 148
0b5a30e9
HV
149 vbifmt->sampling_rate = 27000000;
150 vbifmt->offset = 248;
151 vbifmt->samples_per_line = cx->vbi.raw_decoder_line_size - 4;
152 vbifmt->sample_format = V4L2_PIX_FMT_GREY;
153 vbifmt->start[0] = cx->vbi.start[0];
154 vbifmt->start[1] = cx->vbi.start[1];
155 vbifmt->count[0] = vbifmt->count[1] = cx->vbi.count;
156 vbifmt->flags = 0;
157 vbifmt->reserved[0] = 0;
158 vbifmt->reserved[1] = 0;
1c1e45d1
HV
159 return 0;
160}
161
3b6fe58f
AW
162static int cx18_g_fmt_sliced_vbi_cap(struct file *file, void *fh,
163 struct v4l2_format *fmt)
1c1e45d1 164{
3b6fe58f
AW
165 return -EINVAL;
166}
1c1e45d1 167
3b6fe58f
AW
168static int cx18_try_fmt_vid_cap(struct file *file, void *fh,
169 struct v4l2_format *fmt)
170{
171 struct cx18_open_id *id = fh;
172 struct cx18 *cx = id->cx;
1c1e45d1 173
3b6fe58f
AW
174 int w = fmt->fmt.pix.width;
175 int h = fmt->fmt.pix.height;
1c1e45d1 176
3b6fe58f
AW
177 w = min(w, 720);
178 w = max(w, 1);
179 h = min(h, cx->is_50hz ? 576 : 480);
180 h = max(h, 2);
181 cx18_g_fmt_vid_cap(file, fh, fmt);
182 fmt->fmt.pix.width = w;
183 fmt->fmt.pix.height = h;
184 return 0;
185}
1c1e45d1 186
3b6fe58f
AW
187static int cx18_try_fmt_vbi_cap(struct file *file, void *fh,
188 struct v4l2_format *fmt)
189{
3b6fe58f
AW
190 return cx18_g_fmt_vbi_cap(file, fh, fmt);
191}
192
193static int cx18_try_fmt_sliced_vbi_cap(struct file *file, void *fh,
194 struct v4l2_format *fmt)
195{
196 return -EINVAL;
197}
198
199static int cx18_s_fmt_vid_cap(struct file *file, void *fh,
200 struct v4l2_format *fmt)
201{
202 struct cx18_open_id *id = fh;
203 struct cx18 *cx = id->cx;
204 int ret;
205 int w = fmt->fmt.pix.width;
206 int h = fmt->fmt.pix.height;
207
208 ret = v4l2_prio_check(&cx->prio, &id->prio);
209 if (ret)
210 return ret;
1c1e45d1 211
3b6fe58f
AW
212 ret = cx18_try_fmt_vid_cap(file, fh, fmt);
213 if (ret)
214 return ret;
1c1e45d1 215
3b6fe58f 216 if (cx->params.width == w && cx->params.height == h)
1c1e45d1 217 return 0;
3b6fe58f
AW
218
219 if (atomic_read(&cx->ana_capturing) > 0)
1c1e45d1 220 return -EBUSY;
3b6fe58f
AW
221
222 cx->params.width = w;
223 cx->params.height = h;
1c1e45d1 224 cx18_av_cmd(cx, VIDIOC_S_FMT, fmt);
3b6fe58f 225 return cx18_g_fmt_vid_cap(file, fh, fmt);
1c1e45d1
HV
226}
227
3b6fe58f
AW
228static int cx18_s_fmt_vbi_cap(struct file *file, void *fh,
229 struct v4l2_format *fmt)
1c1e45d1 230{
3b6fe58f 231 struct cx18_open_id *id = fh;
1c1e45d1 232 struct cx18 *cx = id->cx;
3b6fe58f 233 int ret;
1c1e45d1 234
3b6fe58f
AW
235 ret = v4l2_prio_check(&cx->prio, &id->prio);
236 if (ret)
237 return ret;
1c1e45d1 238
3b6fe58f
AW
239 if (id->type == CX18_ENC_STREAM_TYPE_VBI &&
240 cx->vbi.sliced_in->service_set &&
241 atomic_read(&cx->ana_capturing) > 0)
242 return -EBUSY;
1c1e45d1 243
3b6fe58f
AW
244 cx->vbi.sliced_in->service_set = 0;
245 cx18_av_cmd(cx, VIDIOC_S_FMT, &cx->vbi.in);
246 return cx18_g_fmt_vbi_cap(file, fh, fmt);
247}
248
249static int cx18_s_fmt_sliced_vbi_cap(struct file *file, void *fh,
250 struct v4l2_format *fmt)
251{
252 return -EINVAL;
253}
254
255static int cx18_g_chip_ident(struct file *file, void *fh,
256 struct v4l2_chip_ident *chip)
257{
258 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
259
3b6fe58f
AW
260 chip->ident = V4L2_IDENT_NONE;
261 chip->revision = 0;
262 if (chip->match_type == V4L2_CHIP_MATCH_HOST) {
263 if (v4l2_chip_match_host(chip->match_type, chip->match_chip))
264 chip->ident = V4L2_IDENT_CX23418;
265 return 0;
1c1e45d1 266 }
3b6fe58f
AW
267 if (chip->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
268 return cx18_i2c_id(cx, chip->match_chip, VIDIOC_G_CHIP_IDENT,
269 chip);
270 if (chip->match_type == V4L2_CHIP_MATCH_I2C_ADDR)
271 return cx18_call_i2c_client(cx, chip->match_chip,
272 VIDIOC_G_CHIP_IDENT, chip);
273 return -EINVAL;
274}
275
36ecd495
HV
276#ifdef CONFIG_VIDEO_ADV_DEBUG
277static int cx18_cxc(struct cx18 *cx, unsigned int cmd, void *arg)
278{
279 struct v4l2_register *regs = arg;
280 unsigned long flags;
281
282 if (!capable(CAP_SYS_ADMIN))
283 return -EPERM;
284 if (regs->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
285 return -EINVAL;
286
287 spin_lock_irqsave(&cx18_cards_lock, flags);
288 if (cmd == VIDIOC_DBG_G_REGISTER)
289 regs->val = read_enc(regs->reg);
290 else
291 write_enc(regs->val, regs->reg);
292 spin_unlock_irqrestore(&cx18_cards_lock, flags);
293 return 0;
294}
295
3b6fe58f
AW
296static int cx18_g_register(struct file *file, void *fh,
297 struct v4l2_register *reg)
298{
299 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
300
3b6fe58f
AW
301 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
302 return cx18_cxc(cx, VIDIOC_DBG_G_REGISTER, reg);
303 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
304 return cx18_i2c_id(cx, reg->match_chip, VIDIOC_DBG_G_REGISTER,
305 reg);
306 return cx18_call_i2c_client(cx, reg->match_chip, VIDIOC_DBG_G_REGISTER,
307 reg);
308}
309
310static int cx18_s_register(struct file *file, void *fh,
311 struct v4l2_register *reg)
312{
313 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
314
3b6fe58f
AW
315 if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
316 return cx18_cxc(cx, VIDIOC_DBG_S_REGISTER, reg);
317 if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
318 return cx18_i2c_id(cx, reg->match_chip, VIDIOC_DBG_S_REGISTER,
319 reg);
320 return cx18_call_i2c_client(cx, reg->match_chip, VIDIOC_DBG_S_REGISTER,
321 reg);
322}
36ecd495 323#endif
3b6fe58f
AW
324
325static int cx18_g_priority(struct file *file, void *fh, enum v4l2_priority *p)
326{
327 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
328
3b6fe58f 329 *p = v4l2_prio_max(&cx->prio);
1c1e45d1
HV
330 return 0;
331}
332
3b6fe58f 333static int cx18_s_priority(struct file *file, void *fh, enum v4l2_priority prio)
1c1e45d1 334{
3b6fe58f
AW
335 struct cx18_open_id *id = fh;
336 struct cx18 *cx = id->cx;
1c1e45d1 337
3b6fe58f
AW
338 return v4l2_prio_change(&cx->prio, &id->prio, prio);
339}
1c1e45d1 340
3b6fe58f
AW
341static int cx18_querycap(struct file *file, void *fh,
342 struct v4l2_capability *vcap)
343{
344 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
1c1e45d1 345
3b6fe58f
AW
346 strlcpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
347 strlcpy(vcap->card, cx->card_name, sizeof(vcap->card));
348 strlcpy(vcap->bus_info, pci_name(cx->dev), sizeof(vcap->bus_info));
349 vcap->version = CX18_DRIVER_VERSION; /* version */
350 vcap->capabilities = cx->v4l2_cap; /* capabilities */
351 return 0;
352}
1c1e45d1 353
3b6fe58f
AW
354static int cx18_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
355{
356 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
1c1e45d1 357
3b6fe58f
AW
358 return cx18_get_audio_input(cx, vin->index, vin);
359}
1c1e45d1 360
3b6fe58f
AW
361static int cx18_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
362{
363 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
1c1e45d1 364
3b6fe58f
AW
365 vin->index = cx->audio_input;
366 return cx18_get_audio_input(cx, vin->index, vin);
367}
1c1e45d1 368
3b6fe58f
AW
369static int cx18_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
370{
371 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
1c1e45d1 372
3b6fe58f
AW
373 if (vout->index >= cx->nof_audio_inputs)
374 return -EINVAL;
375 cx->audio_input = vout->index;
376 cx18_audio_set_io(cx);
377 return 0;
378}
1c1e45d1 379
3b6fe58f
AW
380static int cx18_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
381{
382 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
1c1e45d1 383
3b6fe58f
AW
384 /* set it to defaults from our table */
385 return cx18_get_input(cx, vin->index, vin);
386}
1c1e45d1 387
3b6fe58f
AW
388static int cx18_cropcap(struct file *file, void *fh,
389 struct v4l2_cropcap *cropcap)
390{
391 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
1c1e45d1 392
3b6fe58f
AW
393 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
394 return -EINVAL;
395 cropcap->bounds.top = cropcap->bounds.left = 0;
396 cropcap->bounds.width = 720;
397 cropcap->bounds.height = cx->is_50hz ? 576 : 480;
398 cropcap->pixelaspect.numerator = cx->is_50hz ? 59 : 10;
399 cropcap->pixelaspect.denominator = cx->is_50hz ? 54 : 11;
400 cropcap->defrect = cropcap->bounds;
401 return 0;
402}
1c1e45d1 403
3b6fe58f
AW
404static int cx18_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
405{
406 struct cx18_open_id *id = fh;
407 struct cx18 *cx = id->cx;
408 int ret;
1c1e45d1 409
3b6fe58f
AW
410 ret = v4l2_prio_check(&cx->prio, &id->prio);
411 if (ret)
412 return ret;
1c1e45d1 413
3b6fe58f
AW
414 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
415 return -EINVAL;
416 return cx18_av_cmd(cx, VIDIOC_S_CROP, crop);
417}
1c1e45d1 418
3b6fe58f
AW
419static int cx18_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
420{
421 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
1c1e45d1 422
3b6fe58f
AW
423 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
424 return -EINVAL;
425 return cx18_av_cmd(cx, VIDIOC_G_CROP, crop);
426}
427
428static int cx18_enum_fmt_vid_cap(struct file *file, void *fh,
429 struct v4l2_fmtdesc *fmt)
430{
431 static struct v4l2_fmtdesc formats[] = {
432 { 0, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
433 "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12, { 0, 0, 0, 0 }
434 },
435 { 1, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FMT_FLAG_COMPRESSED,
436 "MPEG", V4L2_PIX_FMT_MPEG, { 0, 0, 0, 0 }
1c1e45d1 437 }
3b6fe58f
AW
438 };
439
3b6fe58f
AW
440 if (fmt->index > 1)
441 return -EINVAL;
442 *fmt = formats[fmt->index];
443 return 0;
444}
445
446static int cx18_g_input(struct file *file, void *fh, unsigned int *i)
447{
448 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
449
3b6fe58f
AW
450 *i = cx->active_input;
451 return 0;
452}
453
454int cx18_s_input(struct file *file, void *fh, unsigned int inp)
455{
456 struct cx18_open_id *id = fh;
457 struct cx18 *cx = id->cx;
458 int ret;
459
460 ret = v4l2_prio_check(&cx->prio, &id->prio);
461 if (ret)
462 return ret;
463
3b6fe58f
AW
464 if (inp < 0 || inp >= cx->nof_inputs)
465 return -EINVAL;
466
467 if (inp == cx->active_input) {
468 CX18_DEBUG_INFO("Input unchanged\n");
1c1e45d1
HV
469 return 0;
470 }
471
3b6fe58f
AW
472 CX18_DEBUG_INFO("Changing input from %d to %d\n",
473 cx->active_input, inp);
1c1e45d1 474
3b6fe58f
AW
475 cx->active_input = inp;
476 /* Set the audio input to whatever is appropriate for the input type. */
477 cx->audio_input = cx->card->video_inputs[inp].audio_index;
1c1e45d1 478
3b6fe58f
AW
479 /* prevent others from messing with the streams until
480 we're finished changing inputs. */
481 cx18_mute(cx);
482 cx18_video_set_io(cx);
483 cx18_audio_set_io(cx);
484 cx18_unmute(cx);
485 return 0;
486}
1c1e45d1 487
3b6fe58f
AW
488static int cx18_g_frequency(struct file *file, void *fh,
489 struct v4l2_frequency *vf)
490{
491 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
1c1e45d1 492
3b6fe58f
AW
493 if (vf->tuner != 0)
494 return -EINVAL;
1c1e45d1 495
3b6fe58f
AW
496 cx18_call_i2c_clients(cx, VIDIOC_G_FREQUENCY, vf);
497 return 0;
498}
1c1e45d1 499
3b6fe58f
AW
500int cx18_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
501{
502 struct cx18_open_id *id = fh;
503 struct cx18 *cx = id->cx;
504 int ret;
1c1e45d1 505
3b6fe58f
AW
506 ret = v4l2_prio_check(&cx->prio, &id->prio);
507 if (ret)
508 return ret;
1c1e45d1 509
3b6fe58f
AW
510 if (vf->tuner != 0)
511 return -EINVAL;
1c1e45d1 512
3b6fe58f
AW
513 cx18_mute(cx);
514 CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
515 cx18_call_i2c_clients(cx, VIDIOC_S_FREQUENCY, vf);
516 cx18_unmute(cx);
517 return 0;
518}
1c1e45d1 519
3b6fe58f
AW
520static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
521{
522 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
1c1e45d1 523
3b6fe58f
AW
524 *std = cx->std;
525 return 0;
526}
1c1e45d1 527
3b6fe58f
AW
528int cx18_s_std(struct file *file, void *fh, v4l2_std_id *std)
529{
530 struct cx18_open_id *id = fh;
531 struct cx18 *cx = id->cx;
532 int ret;
1c1e45d1 533
3b6fe58f
AW
534 ret = v4l2_prio_check(&cx->prio, &id->prio);
535 if (ret)
536 return ret;
1c1e45d1 537
3b6fe58f
AW
538 if ((*std & V4L2_STD_ALL) == 0)
539 return -EINVAL;
1c1e45d1 540
3b6fe58f
AW
541 if (*std == cx->std)
542 return 0;
543
544 if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
545 atomic_read(&cx->ana_capturing) > 0) {
546 /* Switching standard would turn off the radio or mess
547 with already running streams, prevent that by
548 returning EBUSY. */
549 return -EBUSY;
1c1e45d1
HV
550 }
551
3b6fe58f
AW
552 cx->std = *std;
553 cx->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
554 cx->params.is_50hz = cx->is_50hz = !cx->is_60hz;
555 cx->params.width = 720;
556 cx->params.height = cx->is_50hz ? 576 : 480;
557 cx->vbi.count = cx->is_50hz ? 18 : 12;
558 cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
559 cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
560 cx->vbi.sliced_decoder_line_size = cx->is_60hz ? 272 : 284;
561 CX18_DEBUG_INFO("Switching standard to %llx.\n",
562 (unsigned long long) cx->std);
563
564 /* Tuner */
565 cx18_call_i2c_clients(cx, VIDIOC_S_STD, &cx->std);
566 return 0;
567}
1c1e45d1 568
3b6fe58f
AW
569static int cx18_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
570{
571 struct cx18_open_id *id = fh;
572 struct cx18 *cx = id->cx;
573 int ret;
1c1e45d1 574
3b6fe58f
AW
575 ret = v4l2_prio_check(&cx->prio, &id->prio);
576 if (ret)
577 return ret;
578
3b6fe58f
AW
579 if (vt->index != 0)
580 return -EINVAL;
581
582 /* Setting tuner can only set audio mode */
583 cx18_call_i2c_clients(cx, VIDIOC_S_TUNER, vt);
584
585 return 0;
586}
587
588static int cx18_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
589{
590 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
591
3b6fe58f
AW
592 if (vt->index != 0)
593 return -EINVAL;
594
595 cx18_call_i2c_clients(cx, VIDIOC_G_TUNER, vt);
596
597 if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
598 strlcpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
599 vt->type = V4L2_TUNER_RADIO;
600 } else {
601 strlcpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
602 vt->type = V4L2_TUNER_ANALOG_TV;
1c1e45d1
HV
603 }
604
3b6fe58f
AW
605 return 0;
606}
607
608static int cx18_g_sliced_vbi_cap(struct file *file, void *fh,
609 struct v4l2_sliced_vbi_cap *cap)
610{
611 return -EINVAL;
612}
1c1e45d1 613
3b6fe58f
AW
614static int cx18_g_enc_index(struct file *file, void *fh,
615 struct v4l2_enc_idx *idx)
616{
617 return -EINVAL;
618}
1c1e45d1 619
3b6fe58f
AW
620static int cx18_encoder_cmd(struct file *file, void *fh,
621 struct v4l2_encoder_cmd *enc)
622{
623 struct cx18_open_id *id = fh;
624 struct cx18 *cx = id->cx;
1c1e45d1 625
3b6fe58f
AW
626 switch (enc->cmd) {
627 case V4L2_ENC_CMD_START:
628 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
629 enc->flags = 0;
630 return cx18_start_capture(id);
631
632 case V4L2_ENC_CMD_STOP:
633 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
634 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
635 cx18_stop_capture(id,
636 enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
1c1e45d1 637 break;
1c1e45d1 638
3b6fe58f
AW
639 case V4L2_ENC_CMD_PAUSE:
640 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
641 enc->flags = 0;
642 if (!atomic_read(&cx->ana_capturing))
643 return -EPERM;
644 if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
1c1e45d1 645 return 0;
3b6fe58f
AW
646 cx18_mute(cx);
647 cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, cx18_find_handle(cx));
648 break;
649
650 case V4L2_ENC_CMD_RESUME:
651 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
652 enc->flags = 0;
653 if (!atomic_read(&cx->ana_capturing))
654 return -EPERM;
655 if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
656 return 0;
657 cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, cx18_find_handle(cx));
658 cx18_unmute(cx);
659 break;
660
661 default:
662 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1c1e45d1
HV
663 return -EINVAL;
664 }
3b6fe58f
AW
665 return 0;
666}
1c1e45d1 667
3b6fe58f
AW
668static int cx18_try_encoder_cmd(struct file *file, void *fh,
669 struct v4l2_encoder_cmd *enc)
670{
671 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
1c1e45d1 672
3b6fe58f
AW
673 switch (enc->cmd) {
674 case V4L2_ENC_CMD_START:
675 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
676 enc->flags = 0;
1c1e45d1 677 break;
1c1e45d1 678
3b6fe58f
AW
679 case V4L2_ENC_CMD_STOP:
680 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
681 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
682 break;
1c1e45d1 683
3b6fe58f
AW
684 case V4L2_ENC_CMD_PAUSE:
685 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
686 enc->flags = 0;
687 break;
1c1e45d1 688
3b6fe58f
AW
689 case V4L2_ENC_CMD_RESUME:
690 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
691 enc->flags = 0;
1c1e45d1 692 break;
1c1e45d1
HV
693
694 default:
3b6fe58f 695 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1c1e45d1
HV
696 return -EINVAL;
697 }
698 return 0;
699}
700
3b6fe58f 701static int cx18_log_status(struct file *file, void *fh)
1c1e45d1 702{
3b6fe58f
AW
703 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
704 struct v4l2_input vidin;
705 struct v4l2_audio audin;
706 int i;
1c1e45d1 707
3b6fe58f
AW
708 CX18_INFO("================= START STATUS CARD #%d =================\n", cx->num);
709 if (cx->hw_flags & CX18_HW_TVEEPROM) {
710 struct tveeprom tv;
711
712 cx18_read_eeprom(cx, &tv);
713 }
714 cx18_call_i2c_clients(cx, VIDIOC_LOG_STATUS, NULL);
715 cx18_get_input(cx, cx->active_input, &vidin);
716 cx18_get_audio_input(cx, cx->audio_input, &audin);
717 CX18_INFO("Video Input: %s\n", vidin.name);
718 CX18_INFO("Audio Input: %s\n", audin.name);
8abdd00d 719 mutex_lock(&cx->gpio_lock);
3d66c405
HV
720 CX18_INFO("GPIO: direction 0x%08x, value 0x%08x\n",
721 cx->gpio_dir, cx->gpio_val);
8abdd00d 722 mutex_unlock(&cx->gpio_lock);
3b6fe58f
AW
723 CX18_INFO("Tuner: %s\n",
724 test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ? "Radio" : "TV");
725 cx2341x_log_status(&cx->params, cx->name);
726 CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
727 for (i = 0; i < CX18_MAX_STREAMS; i++) {
728 struct cx18_stream *s = &cx->streams[i];
729
730 if (s->v4l2dev == NULL || s->buffers == 0)
731 continue;
732 CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
733 s->name, s->s_flags,
734 (s->buffers - s->q_free.buffers) * 100 / s->buffers,
735 (s->buffers * s->buf_size) / 1024, s->buffers);
736 }
737 CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
738 (long long)cx->mpg_data_received,
739 (long long)cx->vbi_data_inserted);
740 CX18_INFO("================== END STATUS CARD #%d ==================\n", cx->num);
741 return 0;
742}
743
744static int cx18_default(struct file *file, void *fh, int cmd, void *arg)
745{
746 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
1c1e45d1
HV
747
748 switch (cmd) {
3b6fe58f
AW
749 case VIDIOC_INT_S_AUDIO_ROUTING: {
750 struct v4l2_routing *route = arg;
37f89f95
HV
751
752 CX18_DEBUG_IOCTL("VIDIOC_INT_S_AUDIO_ROUTING(%d, %d)\n",
753 route->input, route->output);
3b6fe58f
AW
754 cx18_audio_set_route(cx, route);
755 break;
756 }
02fa272f
AW
757
758 case VIDIOC_INT_RESET: {
759 u32 val = *(u32 *)arg;
760
761 if ((val == 0) || (val & 0x01))
762 cx18_reset_ir_gpio(&cx->i2c_algo_cb_data[0]);
763 break;
764 }
765
3b6fe58f 766 default:
1c1e45d1 767 return -EINVAL;
1c1e45d1
HV
768 }
769 return 0;
770}
771
772int cx18_v4l2_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
773 unsigned long arg)
774{
37f89f95 775 struct video_device *vfd = video_devdata(filp);
1c1e45d1
HV
776 struct cx18_open_id *id = (struct cx18_open_id *)filp->private_data;
777 struct cx18 *cx = id->cx;
778 int res;
779
780 mutex_lock(&cx->serialize_lock);
37f89f95
HV
781
782 if (cx18_debug & CX18_DBGFLG_IOCTL)
783 vfd->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
3b6fe58f 784 res = video_ioctl2(inode, filp, cmd, arg);
37f89f95 785 vfd->debug = 0;
1c1e45d1
HV
786 mutex_unlock(&cx->serialize_lock);
787 return res;
788}
3b6fe58f 789
a399810c
HV
790static const struct v4l2_ioctl_ops cx18_ioctl_ops = {
791 .vidioc_querycap = cx18_querycap,
792 .vidioc_g_priority = cx18_g_priority,
793 .vidioc_s_priority = cx18_s_priority,
794 .vidioc_s_audio = cx18_s_audio,
795 .vidioc_g_audio = cx18_g_audio,
796 .vidioc_enumaudio = cx18_enumaudio,
797 .vidioc_enum_input = cx18_enum_input,
798 .vidioc_cropcap = cx18_cropcap,
799 .vidioc_s_crop = cx18_s_crop,
800 .vidioc_g_crop = cx18_g_crop,
801 .vidioc_g_input = cx18_g_input,
802 .vidioc_s_input = cx18_s_input,
803 .vidioc_g_frequency = cx18_g_frequency,
804 .vidioc_s_frequency = cx18_s_frequency,
805 .vidioc_s_tuner = cx18_s_tuner,
806 .vidioc_g_tuner = cx18_g_tuner,
807 .vidioc_g_enc_index = cx18_g_enc_index,
808 .vidioc_g_std = cx18_g_std,
809 .vidioc_s_std = cx18_s_std,
810 .vidioc_log_status = cx18_log_status,
811 .vidioc_enum_fmt_vid_cap = cx18_enum_fmt_vid_cap,
812 .vidioc_encoder_cmd = cx18_encoder_cmd,
813 .vidioc_try_encoder_cmd = cx18_try_encoder_cmd,
814 .vidioc_g_fmt_vid_cap = cx18_g_fmt_vid_cap,
815 .vidioc_g_fmt_vbi_cap = cx18_g_fmt_vbi_cap,
816 .vidioc_g_fmt_sliced_vbi_cap = cx18_g_fmt_sliced_vbi_cap,
817 .vidioc_s_fmt_vid_cap = cx18_s_fmt_vid_cap,
818 .vidioc_s_fmt_vbi_cap = cx18_s_fmt_vbi_cap,
819 .vidioc_s_fmt_sliced_vbi_cap = cx18_s_fmt_sliced_vbi_cap,
820 .vidioc_try_fmt_vid_cap = cx18_try_fmt_vid_cap,
821 .vidioc_try_fmt_vbi_cap = cx18_try_fmt_vbi_cap,
822 .vidioc_try_fmt_sliced_vbi_cap = cx18_try_fmt_sliced_vbi_cap,
823 .vidioc_g_sliced_vbi_cap = cx18_g_sliced_vbi_cap,
824 .vidioc_g_chip_ident = cx18_g_chip_ident,
36ecd495 825#ifdef CONFIG_VIDEO_ADV_DEBUG
a399810c
HV
826 .vidioc_g_register = cx18_g_register,
827 .vidioc_s_register = cx18_s_register,
36ecd495 828#endif
a399810c
HV
829 .vidioc_default = cx18_default,
830 .vidioc_queryctrl = cx18_queryctrl,
831 .vidioc_querymenu = cx18_querymenu,
832 .vidioc_g_ext_ctrls = cx18_g_ext_ctrls,
833 .vidioc_s_ext_ctrls = cx18_s_ext_ctrls,
834 .vidioc_try_ext_ctrls = cx18_try_ext_ctrls,
835};
836
837void cx18_set_funcs(struct video_device *vdev)
838{
839 vdev->ioctl_ops = &cx18_ioctl_ops;
3b6fe58f 840}