blob: 7086aaba77d677ed5cc9f8d697b0206be56d4f17 [file] [log] [blame]
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001/*
2 * cx18 ioctl system call
3 *
4 * Derived from ivtv-ioctl.c
5 *
6 * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
Andy Walls1ed9dcc2008-11-22 01:37:34 -03007 * Copyright (C) 2008 Andy Walls <awalls@radix.net>
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 * 02111-1307 USA
23 */
24
25#include "cx18-driver.h"
Andy Wallsb1526422008-08-30 16:03:44 -030026#include "cx18-io.h"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030027#include "cx18-version.h"
28#include "cx18-mailbox.h"
29#include "cx18-i2c.h"
30#include "cx18-queue.h"
31#include "cx18-fileops.h"
32#include "cx18-vbi.h"
33#include "cx18-audio.h"
34#include "cx18-video.h"
35#include "cx18-streams.h"
36#include "cx18-ioctl.h"
37#include "cx18-gpio.h"
38#include "cx18-controls.h"
39#include "cx18-cards.h"
40#include "cx18-av-core.h"
41#include <media/tveeprom.h>
42#include <media/v4l2-chip-ident.h>
43#include <linux/i2c-id.h>
44
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -030045u16 cx18_service2vbi(int type)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030046{
47 switch (type) {
48 case V4L2_SLICED_TELETEXT_B:
49 return CX18_SLICED_TYPE_TELETEXT_B;
50 case V4L2_SLICED_CAPTION_525:
51 return CX18_SLICED_TYPE_CAPTION_525;
52 case V4L2_SLICED_WSS_625:
53 return CX18_SLICED_TYPE_WSS_625;
54 case V4L2_SLICED_VPS:
55 return CX18_SLICED_TYPE_VPS;
56 default:
57 return 0;
58 }
59}
60
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
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -030093void cx18_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030094{
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
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300105
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -0300106u16 cx18_get_service_set(struct v4l2_sliced_vbi_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300107{
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
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300118static int cx18_g_fmt_vid_cap(struct file *file, void *fh,
119 struct v4l2_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300120{
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300121 struct cx18_open_id *id = fh;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300122 struct cx18 *cx = id->cx;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300123 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300124
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300125 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;
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300130 if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300131 pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300132 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300133 pixfmt->sizeimage =
134 pixfmt->height * pixfmt->width +
135 pixfmt->height * (pixfmt->width / 2);
136 pixfmt->bytesperline = 720;
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300137 } else {
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300138 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
139 pixfmt->sizeimage = 128 * 1024;
140 pixfmt->bytesperline = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300141 }
142 return 0;
143}
144
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300145static int cx18_g_fmt_vbi_cap(struct file *file, void *fh,
146 struct v4l2_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300147{
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300148 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300149 struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300150
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300151 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;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300161 return 0;
162}
163
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300164static int cx18_g_fmt_sliced_vbi_cap(struct file *file, void *fh,
165 struct v4l2_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300166{
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300167 return -EINVAL;
168}
169
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;
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300175 int w = fmt->fmt.pix.width;
176 int h = fmt->fmt.pix.height;
177
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300178 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}
187
188static int cx18_try_fmt_vbi_cap(struct file *file, void *fh,
189 struct v4l2_format *fmt)
190{
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300191 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;
Hans Verkuileffc3462008-09-06 08:24:37 -0300206 int w, h;
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300207
208 ret = v4l2_prio_check(&cx->prio, &id->prio);
209 if (ret)
210 return ret;
211
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300212 ret = cx18_try_fmt_vid_cap(file, fh, fmt);
213 if (ret)
214 return ret;
Hans Verkuileffc3462008-09-06 08:24:37 -0300215 w = fmt->fmt.pix.width;
216 h = fmt->fmt.pix.height;
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300217
218 if (cx->params.width == w && cx->params.height == h)
219 return 0;
220
221 if (atomic_read(&cx->ana_capturing) > 0)
222 return -EBUSY;
223
224 cx->params.width = w;
225 cx->params.height = h;
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300226 cx18_av_cmd(cx, VIDIOC_S_FMT, fmt);
227 return cx18_g_fmt_vid_cap(file, fh, fmt);
228}
229
230static int cx18_s_fmt_vbi_cap(struct file *file, void *fh,
231 struct v4l2_format *fmt)
232{
233 struct cx18_open_id *id = fh;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300234 struct cx18 *cx = id->cx;
235 int ret;
236
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300237 ret = v4l2_prio_check(&cx->prio, &id->prio);
238 if (ret)
239 return ret;
240
Andy Wallsdd073432008-12-12 16:24:04 -0300241 if (!cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300242 return -EBUSY;
243
244 cx->vbi.sliced_in->service_set = 0;
Andy Wallsdd073432008-12-12 16:24:04 -0300245 cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
246 cx18_av_cmd(cx, VIDIOC_S_FMT, fmt);
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300247 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,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300257 struct v4l2_dbg_chip_ident *chip)
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300258{
259 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
260
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300261 chip->ident = V4L2_IDENT_NONE;
262 chip->revision = 0;
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300263 if (v4l2_chip_match_host(&chip->match)) {
264 chip->ident = V4L2_IDENT_CX23418;
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300265 return 0;
266 }
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300267 cx18_call_i2c_clients(cx, VIDIOC_DBG_G_CHIP_IDENT, chip);
268 return 0;
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300269}
270
Hans Verkuil36ecd492008-06-25 06:00:17 -0300271#ifdef CONFIG_VIDEO_ADV_DEBUG
272static int cx18_cxc(struct cx18 *cx, unsigned int cmd, void *arg)
273{
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300274 struct v4l2_dbg_register *regs = arg;
Hans Verkuil36ecd492008-06-25 06:00:17 -0300275 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);
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300283 regs->size = 4;
Hans Verkuil36ecd492008-06-25 06:00:17 -0300284 if (cmd == VIDIOC_DBG_G_REGISTER)
Andy Wallsb1526422008-08-30 16:03:44 -0300285 regs->val = cx18_read_enc(cx, regs->reg);
Hans Verkuil36ecd492008-06-25 06:00:17 -0300286 else
Andy Wallsb1526422008-08-30 16:03:44 -0300287 cx18_write_enc(cx, regs->val, regs->reg);
Hans Verkuil36ecd492008-06-25 06:00:17 -0300288 spin_unlock_irqrestore(&cx18_cards_lock, flags);
289 return 0;
290}
291
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300292static int cx18_g_register(struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300293 struct v4l2_dbg_register *reg)
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300294{
295 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
296
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300297 if (v4l2_chip_match_host(&reg->match))
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300298 return cx18_cxc(cx, VIDIOC_DBG_G_REGISTER, reg);
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300299 cx18_call_i2c_clients(cx, VIDIOC_DBG_G_REGISTER, reg);
300 return 0;
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300301}
302
303static int cx18_s_register(struct file *file, void *fh,
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300304 struct v4l2_dbg_register *reg)
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300305{
306 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
307
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300308 if (v4l2_chip_match_host(&reg->match))
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300309 return cx18_cxc(cx, VIDIOC_DBG_S_REGISTER, reg);
Hans Verkuilaecde8b52008-12-30 07:14:19 -0300310 cx18_call_i2c_clients(cx, VIDIOC_DBG_S_REGISTER, reg);
311 return 0;
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300312}
Hans Verkuil36ecd492008-06-25 06:00:17 -0300313#endif
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300314
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
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300319 *p = v4l2_prio_max(&cx->prio);
320 return 0;
321}
322
323static int cx18_s_priority(struct file *file, void *fh, enum v4l2_priority prio)
324{
325 struct cx18_open_id *id = fh;
326 struct cx18 *cx = id->cx;
327
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300328 return v4l2_prio_change(&cx->prio, &id->prio, prio);
329}
330
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;
335
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300336 strlcpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
337 strlcpy(vcap->card, cx->card_name, sizeof(vcap->card));
Hans Verkuil741e1f32008-09-08 16:59:02 -0300338 snprintf(vcap->bus_info, sizeof(vcap->bus_info), "PCI:%s", pci_name(cx->dev));
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300339 vcap->version = CX18_DRIVER_VERSION; /* version */
340 vcap->capabilities = cx->v4l2_cap; /* capabilities */
341 return 0;
342}
343
344static int cx18_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
345{
346 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
347
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300348 return cx18_get_audio_input(cx, vin->index, vin);
349}
350
351static int cx18_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
352{
353 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
354
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300355 vin->index = cx->audio_input;
356 return cx18_get_audio_input(cx, vin->index, vin);
357}
358
359static int cx18_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
360{
361 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
362
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300363 if (vout->index >= cx->nof_audio_inputs)
364 return -EINVAL;
365 cx->audio_input = vout->index;
366 cx18_audio_set_io(cx);
367 return 0;
368}
369
370static int cx18_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
371{
372 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
373
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300374 /* set it to defaults from our table */
375 return cx18_get_input(cx, vin->index, vin);
376}
377
378static int cx18_cropcap(struct file *file, void *fh,
379 struct v4l2_cropcap *cropcap)
380{
381 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
382
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300383 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
384 return -EINVAL;
385 cropcap->bounds.top = cropcap->bounds.left = 0;
386 cropcap->bounds.width = 720;
387 cropcap->bounds.height = cx->is_50hz ? 576 : 480;
388 cropcap->pixelaspect.numerator = cx->is_50hz ? 59 : 10;
389 cropcap->pixelaspect.denominator = cx->is_50hz ? 54 : 11;
390 cropcap->defrect = cropcap->bounds;
391 return 0;
392}
393
394static int cx18_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
395{
396 struct cx18_open_id *id = fh;
397 struct cx18 *cx = id->cx;
398 int ret;
399
400 ret = v4l2_prio_check(&cx->prio, &id->prio);
401 if (ret)
402 return ret;
403
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300404 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
405 return -EINVAL;
406 return cx18_av_cmd(cx, VIDIOC_S_CROP, crop);
407}
408
409static int cx18_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
410{
411 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
412
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300413 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
414 return -EINVAL;
415 return cx18_av_cmd(cx, VIDIOC_G_CROP, crop);
416}
417
418static int cx18_enum_fmt_vid_cap(struct file *file, void *fh,
419 struct v4l2_fmtdesc *fmt)
420{
421 static struct v4l2_fmtdesc formats[] = {
422 { 0, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
423 "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12, { 0, 0, 0, 0 }
424 },
425 { 1, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FMT_FLAG_COMPRESSED,
426 "MPEG", V4L2_PIX_FMT_MPEG, { 0, 0, 0, 0 }
427 }
428 };
429
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300430 if (fmt->index > 1)
431 return -EINVAL;
432 *fmt = formats[fmt->index];
433 return 0;
434}
435
436static int cx18_g_input(struct file *file, void *fh, unsigned int *i)
437{
438 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
439
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300440 *i = cx->active_input;
441 return 0;
442}
443
444int cx18_s_input(struct file *file, void *fh, unsigned int inp)
445{
446 struct cx18_open_id *id = fh;
447 struct cx18 *cx = id->cx;
448 int ret;
449
450 ret = v4l2_prio_check(&cx->prio, &id->prio);
451 if (ret)
452 return ret;
453
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300454 if (inp < 0 || inp >= cx->nof_inputs)
455 return -EINVAL;
456
457 if (inp == cx->active_input) {
458 CX18_DEBUG_INFO("Input unchanged\n");
459 return 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300460 }
461
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300462 CX18_DEBUG_INFO("Changing input from %d to %d\n",
463 cx->active_input, inp);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300464
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300465 cx->active_input = inp;
466 /* Set the audio input to whatever is appropriate for the input type. */
467 cx->audio_input = cx->card->video_inputs[inp].audio_index;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300468
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300469 /* prevent others from messing with the streams until
470 we're finished changing inputs. */
471 cx18_mute(cx);
472 cx18_video_set_io(cx);
473 cx18_audio_set_io(cx);
474 cx18_unmute(cx);
475 return 0;
476}
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300477
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300478static int cx18_g_frequency(struct file *file, void *fh,
479 struct v4l2_frequency *vf)
480{
481 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
482
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300483 if (vf->tuner != 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300484 return -EINVAL;
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300485
486 cx18_call_i2c_clients(cx, VIDIOC_G_FREQUENCY, vf);
487 return 0;
488}
489
490int cx18_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
491{
492 struct cx18_open_id *id = fh;
493 struct cx18 *cx = id->cx;
494 int ret;
495
496 ret = v4l2_prio_check(&cx->prio, &id->prio);
497 if (ret)
498 return ret;
499
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300500 if (vf->tuner != 0)
501 return -EINVAL;
502
503 cx18_mute(cx);
504 CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
505 cx18_call_i2c_clients(cx, VIDIOC_S_FREQUENCY, vf);
506 cx18_unmute(cx);
507 return 0;
508}
509
510static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
511{
512 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
513
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300514 *std = cx->std;
515 return 0;
516}
517
518int cx18_s_std(struct file *file, void *fh, v4l2_std_id *std)
519{
520 struct cx18_open_id *id = fh;
521 struct cx18 *cx = id->cx;
522 int ret;
523
524 ret = v4l2_prio_check(&cx->prio, &id->prio);
525 if (ret)
526 return ret;
527
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300528 if ((*std & V4L2_STD_ALL) == 0)
529 return -EINVAL;
530
531 if (*std == cx->std)
532 return 0;
533
534 if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
535 atomic_read(&cx->ana_capturing) > 0) {
536 /* Switching standard would turn off the radio or mess
537 with already running streams, prevent that by
538 returning EBUSY. */
539 return -EBUSY;
540 }
541
542 cx->std = *std;
543 cx->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
544 cx->params.is_50hz = cx->is_50hz = !cx->is_60hz;
545 cx->params.width = 720;
546 cx->params.height = cx->is_50hz ? 576 : 480;
547 cx->vbi.count = cx->is_50hz ? 18 : 12;
548 cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
549 cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
550 cx->vbi.sliced_decoder_line_size = cx->is_60hz ? 272 : 284;
551 CX18_DEBUG_INFO("Switching standard to %llx.\n",
552 (unsigned long long) cx->std);
553
554 /* Tuner */
555 cx18_call_i2c_clients(cx, VIDIOC_S_STD, &cx->std);
556 return 0;
557}
558
559static int cx18_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
560{
561 struct cx18_open_id *id = fh;
562 struct cx18 *cx = id->cx;
563 int ret;
564
565 ret = v4l2_prio_check(&cx->prio, &id->prio);
566 if (ret)
567 return ret;
568
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300569 if (vt->index != 0)
570 return -EINVAL;
571
572 /* Setting tuner can only set audio mode */
573 cx18_call_i2c_clients(cx, VIDIOC_S_TUNER, vt);
574
575 return 0;
576}
577
578static int cx18_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
579{
580 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
581
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300582 if (vt->index != 0)
583 return -EINVAL;
584
585 cx18_call_i2c_clients(cx, VIDIOC_G_TUNER, vt);
586
587 if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
588 strlcpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
589 vt->type = V4L2_TUNER_RADIO;
590 } else {
591 strlcpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
592 vt->type = V4L2_TUNER_ANALOG_TV;
593 }
594
595 return 0;
596}
597
598static int cx18_g_sliced_vbi_cap(struct file *file, void *fh,
599 struct v4l2_sliced_vbi_cap *cap)
600{
601 return -EINVAL;
602}
603
604static int cx18_g_enc_index(struct file *file, void *fh,
605 struct v4l2_enc_idx *idx)
606{
607 return -EINVAL;
608}
609
610static int cx18_encoder_cmd(struct file *file, void *fh,
611 struct v4l2_encoder_cmd *enc)
612{
613 struct cx18_open_id *id = fh;
614 struct cx18 *cx = id->cx;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300615 u32 h;
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300616
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300617 switch (enc->cmd) {
618 case V4L2_ENC_CMD_START:
619 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
620 enc->flags = 0;
621 return cx18_start_capture(id);
622
623 case V4L2_ENC_CMD_STOP:
624 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
625 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
626 cx18_stop_capture(id,
627 enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
628 break;
629
630 case V4L2_ENC_CMD_PAUSE:
631 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
632 enc->flags = 0;
633 if (!atomic_read(&cx->ana_capturing))
634 return -EPERM;
635 if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
636 return 0;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300637 h = cx18_find_handle(cx);
638 if (h == CX18_INVALID_TASK_HANDLE) {
639 CX18_ERR("Can't find valid task handle for "
640 "V4L2_ENC_CMD_PAUSE\n");
641 return -EBADFD;
642 }
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300643 cx18_mute(cx);
Andy Wallsd3c5e702008-08-23 16:42:29 -0300644 cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, h);
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300645 break;
646
647 case V4L2_ENC_CMD_RESUME:
648 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
649 enc->flags = 0;
650 if (!atomic_read(&cx->ana_capturing))
651 return -EPERM;
652 if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
653 return 0;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300654 h = cx18_find_handle(cx);
655 if (h == CX18_INVALID_TASK_HANDLE) {
656 CX18_ERR("Can't find valid task handle for "
657 "V4L2_ENC_CMD_RESUME\n");
658 return -EBADFD;
659 }
660 cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, h);
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300661 cx18_unmute(cx);
662 break;
663
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300664 default:
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300665 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
666 return -EINVAL;
667 }
668 return 0;
669}
670
671static int cx18_try_encoder_cmd(struct file *file, void *fh,
672 struct v4l2_encoder_cmd *enc)
673{
674 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
675
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300676 switch (enc->cmd) {
677 case V4L2_ENC_CMD_START:
678 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
679 enc->flags = 0;
680 break;
681
682 case V4L2_ENC_CMD_STOP:
683 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
684 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
685 break;
686
687 case V4L2_ENC_CMD_PAUSE:
688 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
689 enc->flags = 0;
690 break;
691
692 case V4L2_ENC_CMD_RESUME:
693 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
694 enc->flags = 0;
695 break;
696
697 default:
698 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
699 return -EINVAL;
700 }
701 return 0;
702}
703
704static int cx18_log_status(struct file *file, void *fh)
705{
706 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
707 struct v4l2_input vidin;
708 struct v4l2_audio audin;
709 int i;
710
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300711 CX18_INFO("================= START STATUS CARD #%d =================\n", cx->num);
712 if (cx->hw_flags & CX18_HW_TVEEPROM) {
713 struct tveeprom tv;
714
715 cx18_read_eeprom(cx, &tv);
716 }
717 cx18_call_i2c_clients(cx, VIDIOC_LOG_STATUS, NULL);
718 cx18_get_input(cx, cx->active_input, &vidin);
719 cx18_get_audio_input(cx, cx->audio_input, &audin);
720 CX18_INFO("Video Input: %s\n", vidin.name);
721 CX18_INFO("Audio Input: %s\n", audin.name);
Andy Walls8abdd002008-07-13 19:05:25 -0300722 mutex_lock(&cx->gpio_lock);
Hans Verkuil3d66c402008-06-21 11:19:34 -0300723 CX18_INFO("GPIO: direction 0x%08x, value 0x%08x\n",
724 cx->gpio_dir, cx->gpio_val);
Andy Walls8abdd002008-07-13 19:05:25 -0300725 mutex_unlock(&cx->gpio_lock);
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300726 CX18_INFO("Tuner: %s\n",
727 test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ? "Radio" : "TV");
728 cx2341x_log_status(&cx->params, cx->name);
729 CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
730 for (i = 0; i < CX18_MAX_STREAMS; i++) {
731 struct cx18_stream *s = &cx->streams[i];
732
733 if (s->v4l2dev == NULL || s->buffers == 0)
734 continue;
735 CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
736 s->name, s->s_flags,
Andy Walls66c2a6b2008-12-08 23:02:45 -0300737 atomic_read(&s->q_full.buffers) * 100 / s->buffers,
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300738 (s->buffers * s->buf_size) / 1024, s->buffers);
739 }
740 CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
741 (long long)cx->mpg_data_received,
742 (long long)cx->vbi_data_inserted);
743 CX18_INFO("================== END STATUS CARD #%d ==================\n", cx->num);
744 return 0;
745}
746
Hans Verkuil069b7472008-12-30 07:04:34 -0300747static long cx18_default(struct file *file, void *fh, int cmd, void *arg)
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300748{
749 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
750
751 switch (cmd) {
752 case VIDIOC_INT_S_AUDIO_ROUTING: {
753 struct v4l2_routing *route = arg;
Hans Verkuil37f89f92008-06-22 11:57:31 -0300754
755 CX18_DEBUG_IOCTL("VIDIOC_INT_S_AUDIO_ROUTING(%d, %d)\n",
756 route->input, route->output);
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300757 cx18_audio_set_route(cx, route);
758 break;
759 }
Andy Walls02fa2722008-07-13 19:30:15 -0300760
761 case VIDIOC_INT_RESET: {
762 u32 val = *(u32 *)arg;
763
764 if ((val == 0) || (val & 0x01))
765 cx18_reset_ir_gpio(&cx->i2c_algo_cb_data[0]);
766 break;
767 }
768
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300769 default:
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300770 return -EINVAL;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300771 }
772 return 0;
773}
774
Hans Verkuil069b7472008-12-30 07:04:34 -0300775long cx18_v4l2_ioctl(struct file *filp, unsigned int cmd,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300776 unsigned long arg)
777{
Hans Verkuil37f89f92008-06-22 11:57:31 -0300778 struct video_device *vfd = video_devdata(filp);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300779 struct cx18_open_id *id = (struct cx18_open_id *)filp->private_data;
780 struct cx18 *cx = id->cx;
Hans Verkuil069b7472008-12-30 07:04:34 -0300781 long res;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300782
783 mutex_lock(&cx->serialize_lock);
Hans Verkuil37f89f92008-06-22 11:57:31 -0300784
785 if (cx18_debug & CX18_DBGFLG_IOCTL)
786 vfd->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
Hans Verkuilbec43662008-12-30 06:58:20 -0300787 res = video_ioctl2(filp, cmd, arg);
Hans Verkuil37f89f92008-06-22 11:57:31 -0300788 vfd->debug = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300789 mutex_unlock(&cx->serialize_lock);
790 return res;
791}
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300792
Hans Verkuila3998102008-07-21 02:57:38 -0300793static const struct v4l2_ioctl_ops cx18_ioctl_ops = {
794 .vidioc_querycap = cx18_querycap,
795 .vidioc_g_priority = cx18_g_priority,
796 .vidioc_s_priority = cx18_s_priority,
797 .vidioc_s_audio = cx18_s_audio,
798 .vidioc_g_audio = cx18_g_audio,
799 .vidioc_enumaudio = cx18_enumaudio,
800 .vidioc_enum_input = cx18_enum_input,
801 .vidioc_cropcap = cx18_cropcap,
802 .vidioc_s_crop = cx18_s_crop,
803 .vidioc_g_crop = cx18_g_crop,
804 .vidioc_g_input = cx18_g_input,
805 .vidioc_s_input = cx18_s_input,
806 .vidioc_g_frequency = cx18_g_frequency,
807 .vidioc_s_frequency = cx18_s_frequency,
808 .vidioc_s_tuner = cx18_s_tuner,
809 .vidioc_g_tuner = cx18_g_tuner,
810 .vidioc_g_enc_index = cx18_g_enc_index,
811 .vidioc_g_std = cx18_g_std,
812 .vidioc_s_std = cx18_s_std,
813 .vidioc_log_status = cx18_log_status,
814 .vidioc_enum_fmt_vid_cap = cx18_enum_fmt_vid_cap,
815 .vidioc_encoder_cmd = cx18_encoder_cmd,
816 .vidioc_try_encoder_cmd = cx18_try_encoder_cmd,
817 .vidioc_g_fmt_vid_cap = cx18_g_fmt_vid_cap,
818 .vidioc_g_fmt_vbi_cap = cx18_g_fmt_vbi_cap,
819 .vidioc_g_fmt_sliced_vbi_cap = cx18_g_fmt_sliced_vbi_cap,
820 .vidioc_s_fmt_vid_cap = cx18_s_fmt_vid_cap,
821 .vidioc_s_fmt_vbi_cap = cx18_s_fmt_vbi_cap,
822 .vidioc_s_fmt_sliced_vbi_cap = cx18_s_fmt_sliced_vbi_cap,
823 .vidioc_try_fmt_vid_cap = cx18_try_fmt_vid_cap,
824 .vidioc_try_fmt_vbi_cap = cx18_try_fmt_vbi_cap,
825 .vidioc_try_fmt_sliced_vbi_cap = cx18_try_fmt_sliced_vbi_cap,
826 .vidioc_g_sliced_vbi_cap = cx18_g_sliced_vbi_cap,
827 .vidioc_g_chip_ident = cx18_g_chip_ident,
828#ifdef CONFIG_VIDEO_ADV_DEBUG
829 .vidioc_g_register = cx18_g_register,
830 .vidioc_s_register = cx18_s_register,
831#endif
832 .vidioc_default = cx18_default,
833 .vidioc_queryctrl = cx18_queryctrl,
834 .vidioc_querymenu = cx18_querymenu,
835 .vidioc_g_ext_ctrls = cx18_g_ext_ctrls,
836 .vidioc_s_ext_ctrls = cx18_s_ext_ctrls,
837 .vidioc_try_ext_ctrls = cx18_try_ext_ctrls,
838};
839
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300840void cx18_set_funcs(struct video_device *vdev)
841{
Hans Verkuila3998102008-07-21 02:57:38 -0300842 vdev->ioctl_ops = &cx18_ioctl_ops;
Andy Walls3b6fe58f2008-06-21 08:36:31 -0300843}