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