blob: 1adb97220920fa44ba31f822ab51a1f7306cceb8 [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
Andy Walls302df972009-01-31 00:33:02 -0300105static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
106{
107 int f, l;
108 u16 set = 0;
109
110 for (f = 0; f < 2; f++) {
111 for (l = 0; l < 24; l++) {
112 fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
113 set |= fmt->service_lines[f][l];
114 }
115 }
116 return set != 0;
117}
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300118
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -0300119u16 cx18_get_service_set(struct v4l2_sliced_vbi_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300120{
121 int f, l;
122 u16 set = 0;
123
124 for (f = 0; f < 2; f++) {
125 for (l = 0; l < 24; l++)
126 set |= fmt->service_lines[f][l];
127 }
128 return set;
129}
130
Andy Walls3b6fe582008-06-21 08:36:31 -0300131static int cx18_g_fmt_vid_cap(struct file *file, void *fh,
132 struct v4l2_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300133{
Andy Walls3b6fe582008-06-21 08:36:31 -0300134 struct cx18_open_id *id = fh;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300135 struct cx18 *cx = id->cx;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300136 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300137
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300138 pixfmt->width = cx->params.width;
139 pixfmt->height = cx->params.height;
140 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
141 pixfmt->field = V4L2_FIELD_INTERLACED;
142 pixfmt->priv = 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300143 if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300144 pixfmt->pixelformat = V4L2_PIX_FMT_HM12;
Andy Walls3b6fe582008-06-21 08:36:31 -0300145 /* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300146 pixfmt->sizeimage =
147 pixfmt->height * pixfmt->width +
148 pixfmt->height * (pixfmt->width / 2);
149 pixfmt->bytesperline = 720;
Andy Walls3b6fe582008-06-21 08:36:31 -0300150 } else {
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300151 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
152 pixfmt->sizeimage = 128 * 1024;
153 pixfmt->bytesperline = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300154 }
155 return 0;
156}
157
Andy Walls3b6fe582008-06-21 08:36:31 -0300158static int cx18_g_fmt_vbi_cap(struct file *file, void *fh,
159 struct v4l2_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300160{
Andy Walls3b6fe582008-06-21 08:36:31 -0300161 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300162 struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300163
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300164 vbifmt->sampling_rate = 27000000;
165 vbifmt->offset = 248;
Andy Walls302df972009-01-31 00:33:02 -0300166 vbifmt->samples_per_line = vbi_active_samples - 4;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300167 vbifmt->sample_format = V4L2_PIX_FMT_GREY;
168 vbifmt->start[0] = cx->vbi.start[0];
169 vbifmt->start[1] = cx->vbi.start[1];
170 vbifmt->count[0] = vbifmt->count[1] = cx->vbi.count;
171 vbifmt->flags = 0;
172 vbifmt->reserved[0] = 0;
173 vbifmt->reserved[1] = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300174 return 0;
175}
176
Andy Walls3b6fe582008-06-21 08:36:31 -0300177static int cx18_g_fmt_sliced_vbi_cap(struct file *file, void *fh,
178 struct v4l2_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300179{
Andy Walls302df972009-01-31 00:33:02 -0300180 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
181 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
182
183 vbifmt->reserved[0] = 0;
184 vbifmt->reserved[1] = 0;
185 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
186 memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
187
188 cx18_av_cmd(cx, VIDIOC_G_FMT, fmt);
189 vbifmt->service_set = cx18_get_service_set(vbifmt);
190 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300191}
192
193static int cx18_try_fmt_vid_cap(struct file *file, void *fh,
194 struct v4l2_format *fmt)
195{
196 struct cx18_open_id *id = fh;
197 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300198 int w = fmt->fmt.pix.width;
199 int h = fmt->fmt.pix.height;
200
Andy Walls3b6fe582008-06-21 08:36:31 -0300201 w = min(w, 720);
202 w = max(w, 1);
203 h = min(h, cx->is_50hz ? 576 : 480);
204 h = max(h, 2);
205 cx18_g_fmt_vid_cap(file, fh, fmt);
206 fmt->fmt.pix.width = w;
207 fmt->fmt.pix.height = h;
208 return 0;
209}
210
211static int cx18_try_fmt_vbi_cap(struct file *file, void *fh,
212 struct v4l2_format *fmt)
213{
Andy Walls3b6fe582008-06-21 08:36:31 -0300214 return cx18_g_fmt_vbi_cap(file, fh, fmt);
215}
216
217static int cx18_try_fmt_sliced_vbi_cap(struct file *file, void *fh,
218 struct v4l2_format *fmt)
219{
Andy Walls302df972009-01-31 00:33:02 -0300220 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
221 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
222
223 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
224 vbifmt->reserved[0] = 0;
225 vbifmt->reserved[1] = 0;
226
227 if (vbifmt->service_set)
228 cx18_expand_service_set(vbifmt, cx->is_50hz);
229 check_service_set(vbifmt, cx->is_50hz);
230 vbifmt->service_set = cx18_get_service_set(vbifmt);
231 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300232}
233
234static int cx18_s_fmt_vid_cap(struct file *file, void *fh,
235 struct v4l2_format *fmt)
236{
237 struct cx18_open_id *id = fh;
238 struct cx18 *cx = id->cx;
239 int ret;
Hans Verkuileffc3462008-09-06 08:24:37 -0300240 int w, h;
Andy Walls3b6fe582008-06-21 08:36:31 -0300241
242 ret = v4l2_prio_check(&cx->prio, &id->prio);
243 if (ret)
244 return ret;
245
Andy Walls3b6fe582008-06-21 08:36:31 -0300246 ret = cx18_try_fmt_vid_cap(file, fh, fmt);
247 if (ret)
248 return ret;
Hans Verkuileffc3462008-09-06 08:24:37 -0300249 w = fmt->fmt.pix.width;
250 h = fmt->fmt.pix.height;
Andy Walls3b6fe582008-06-21 08:36:31 -0300251
252 if (cx->params.width == w && cx->params.height == h)
253 return 0;
254
255 if (atomic_read(&cx->ana_capturing) > 0)
256 return -EBUSY;
257
258 cx->params.width = w;
259 cx->params.height = h;
Andy Walls3b6fe582008-06-21 08:36:31 -0300260 cx18_av_cmd(cx, VIDIOC_S_FMT, fmt);
261 return cx18_g_fmt_vid_cap(file, fh, fmt);
262}
263
264static int cx18_s_fmt_vbi_cap(struct file *file, void *fh,
265 struct v4l2_format *fmt)
266{
267 struct cx18_open_id *id = fh;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300268 struct cx18 *cx = id->cx;
269 int ret;
270
Andy Walls3b6fe582008-06-21 08:36:31 -0300271 ret = v4l2_prio_check(&cx->prio, &id->prio);
272 if (ret)
273 return ret;
274
Andy Wallsdd073432008-12-12 16:24:04 -0300275 if (!cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
Andy Walls3b6fe582008-06-21 08:36:31 -0300276 return -EBUSY;
277
278 cx->vbi.sliced_in->service_set = 0;
Andy Wallsdd073432008-12-12 16:24:04 -0300279 cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
280 cx18_av_cmd(cx, VIDIOC_S_FMT, fmt);
Andy Walls3b6fe582008-06-21 08:36:31 -0300281 return cx18_g_fmt_vbi_cap(file, fh, fmt);
282}
283
284static int cx18_s_fmt_sliced_vbi_cap(struct file *file, void *fh,
285 struct v4l2_format *fmt)
286{
Andy Walls302df972009-01-31 00:33:02 -0300287 struct cx18_open_id *id = fh;
288 struct cx18 *cx = id->cx;
289 int ret;
290 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
291
292 ret = v4l2_prio_check(&cx->prio, &id->prio);
293 if (ret)
294 return ret;
295
296 ret = cx18_try_fmt_sliced_vbi_cap(file, fh, fmt);
297 if (ret)
298 return ret;
299
300 if (check_service_set(vbifmt, cx->is_50hz) == 0)
301 return -EINVAL;
302
303 if (cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
304 return -EBUSY;
305 cx->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
306 cx18_av_cmd(cx, VIDIOC_S_FMT, fmt);
307 memcpy(cx->vbi.sliced_in, vbifmt, sizeof(*cx->vbi.sliced_in));
308 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300309}
310
311static int cx18_g_chip_ident(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300312 struct v4l2_dbg_chip_ident *chip)
Andy Walls3b6fe582008-06-21 08:36:31 -0300313{
314 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
315
Andy Walls3b6fe582008-06-21 08:36:31 -0300316 chip->ident = V4L2_IDENT_NONE;
317 chip->revision = 0;
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300318 if (v4l2_chip_match_host(&chip->match)) {
319 chip->ident = V4L2_IDENT_CX23418;
Andy Walls3b6fe582008-06-21 08:36:31 -0300320 return 0;
321 }
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300322 cx18_call_i2c_clients(cx, VIDIOC_DBG_G_CHIP_IDENT, chip);
323 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300324}
325
Hans Verkuil36ecd492008-06-25 06:00:17 -0300326#ifdef CONFIG_VIDEO_ADV_DEBUG
327static int cx18_cxc(struct cx18 *cx, unsigned int cmd, void *arg)
328{
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300329 struct v4l2_dbg_register *regs = arg;
Hans Verkuil36ecd492008-06-25 06:00:17 -0300330 unsigned long flags;
331
332 if (!capable(CAP_SYS_ADMIN))
333 return -EPERM;
334 if (regs->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
335 return -EINVAL;
336
337 spin_lock_irqsave(&cx18_cards_lock, flags);
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300338 regs->size = 4;
Hans Verkuil36ecd492008-06-25 06:00:17 -0300339 if (cmd == VIDIOC_DBG_G_REGISTER)
Andy Wallsb1526422008-08-30 16:03:44 -0300340 regs->val = cx18_read_enc(cx, regs->reg);
Hans Verkuil36ecd492008-06-25 06:00:17 -0300341 else
Andy Wallsb1526422008-08-30 16:03:44 -0300342 cx18_write_enc(cx, regs->val, regs->reg);
Hans Verkuil36ecd492008-06-25 06:00:17 -0300343 spin_unlock_irqrestore(&cx18_cards_lock, flags);
344 return 0;
345}
346
Andy Walls3b6fe582008-06-21 08:36:31 -0300347static int cx18_g_register(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300348 struct v4l2_dbg_register *reg)
Andy Walls3b6fe582008-06-21 08:36:31 -0300349{
350 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
351
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300352 if (v4l2_chip_match_host(&reg->match))
Andy Walls3b6fe582008-06-21 08:36:31 -0300353 return cx18_cxc(cx, VIDIOC_DBG_G_REGISTER, reg);
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300354 cx18_call_i2c_clients(cx, VIDIOC_DBG_G_REGISTER, reg);
355 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300356}
357
358static int cx18_s_register(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300359 struct v4l2_dbg_register *reg)
Andy Walls3b6fe582008-06-21 08:36:31 -0300360{
361 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
362
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300363 if (v4l2_chip_match_host(&reg->match))
Andy Walls3b6fe582008-06-21 08:36:31 -0300364 return cx18_cxc(cx, VIDIOC_DBG_S_REGISTER, reg);
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300365 cx18_call_i2c_clients(cx, VIDIOC_DBG_S_REGISTER, reg);
366 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300367}
Hans Verkuil36ecd492008-06-25 06:00:17 -0300368#endif
Andy Walls3b6fe582008-06-21 08:36:31 -0300369
370static int cx18_g_priority(struct file *file, void *fh, enum v4l2_priority *p)
371{
372 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
373
Andy Walls3b6fe582008-06-21 08:36:31 -0300374 *p = v4l2_prio_max(&cx->prio);
375 return 0;
376}
377
378static int cx18_s_priority(struct file *file, void *fh, enum v4l2_priority prio)
379{
380 struct cx18_open_id *id = fh;
381 struct cx18 *cx = id->cx;
382
Andy Walls3b6fe582008-06-21 08:36:31 -0300383 return v4l2_prio_change(&cx->prio, &id->prio, prio);
384}
385
386static int cx18_querycap(struct file *file, void *fh,
387 struct v4l2_capability *vcap)
388{
389 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
390
Andy Walls3b6fe582008-06-21 08:36:31 -0300391 strlcpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
392 strlcpy(vcap->card, cx->card_name, sizeof(vcap->card));
Andy Walls3d059132009-01-10 21:54:39 -0300393 snprintf(vcap->bus_info, sizeof(vcap->bus_info),
394 "PCI:%s", pci_name(cx->pci_dev));
Andy Walls3b6fe582008-06-21 08:36:31 -0300395 vcap->version = CX18_DRIVER_VERSION; /* version */
396 vcap->capabilities = cx->v4l2_cap; /* capabilities */
397 return 0;
398}
399
400static int cx18_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
401{
402 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
403
Andy Walls3b6fe582008-06-21 08:36:31 -0300404 return cx18_get_audio_input(cx, vin->index, vin);
405}
406
407static int cx18_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
408{
409 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
410
Andy Walls3b6fe582008-06-21 08:36:31 -0300411 vin->index = cx->audio_input;
412 return cx18_get_audio_input(cx, vin->index, vin);
413}
414
415static int cx18_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
416{
417 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
418
Andy Walls3b6fe582008-06-21 08:36:31 -0300419 if (vout->index >= cx->nof_audio_inputs)
420 return -EINVAL;
421 cx->audio_input = vout->index;
422 cx18_audio_set_io(cx);
423 return 0;
424}
425
426static int cx18_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
427{
428 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
429
Andy Walls3b6fe582008-06-21 08:36:31 -0300430 /* set it to defaults from our table */
431 return cx18_get_input(cx, vin->index, vin);
432}
433
434static int cx18_cropcap(struct file *file, void *fh,
435 struct v4l2_cropcap *cropcap)
436{
437 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
438
Andy Walls3b6fe582008-06-21 08:36:31 -0300439 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
440 return -EINVAL;
441 cropcap->bounds.top = cropcap->bounds.left = 0;
442 cropcap->bounds.width = 720;
443 cropcap->bounds.height = cx->is_50hz ? 576 : 480;
444 cropcap->pixelaspect.numerator = cx->is_50hz ? 59 : 10;
445 cropcap->pixelaspect.denominator = cx->is_50hz ? 54 : 11;
446 cropcap->defrect = cropcap->bounds;
447 return 0;
448}
449
450static int cx18_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
451{
452 struct cx18_open_id *id = fh;
453 struct cx18 *cx = id->cx;
454 int ret;
455
456 ret = v4l2_prio_check(&cx->prio, &id->prio);
457 if (ret)
458 return ret;
459
Andy Walls3b6fe582008-06-21 08:36:31 -0300460 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
461 return -EINVAL;
462 return cx18_av_cmd(cx, VIDIOC_S_CROP, crop);
463}
464
465static int cx18_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
466{
467 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
468
Andy Walls3b6fe582008-06-21 08:36:31 -0300469 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
470 return -EINVAL;
471 return cx18_av_cmd(cx, VIDIOC_G_CROP, crop);
472}
473
474static int cx18_enum_fmt_vid_cap(struct file *file, void *fh,
475 struct v4l2_fmtdesc *fmt)
476{
477 static struct v4l2_fmtdesc formats[] = {
478 { 0, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
479 "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12, { 0, 0, 0, 0 }
480 },
481 { 1, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FMT_FLAG_COMPRESSED,
482 "MPEG", V4L2_PIX_FMT_MPEG, { 0, 0, 0, 0 }
483 }
484 };
485
Andy Walls3b6fe582008-06-21 08:36:31 -0300486 if (fmt->index > 1)
487 return -EINVAL;
488 *fmt = formats[fmt->index];
489 return 0;
490}
491
492static int cx18_g_input(struct file *file, void *fh, unsigned int *i)
493{
494 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
495
Andy Walls3b6fe582008-06-21 08:36:31 -0300496 *i = cx->active_input;
497 return 0;
498}
499
500int cx18_s_input(struct file *file, void *fh, unsigned int inp)
501{
502 struct cx18_open_id *id = fh;
503 struct cx18 *cx = id->cx;
504 int ret;
505
506 ret = v4l2_prio_check(&cx->prio, &id->prio);
507 if (ret)
508 return ret;
509
Andy Walls3b6fe582008-06-21 08:36:31 -0300510 if (inp < 0 || inp >= cx->nof_inputs)
511 return -EINVAL;
512
513 if (inp == cx->active_input) {
514 CX18_DEBUG_INFO("Input unchanged\n");
515 return 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300516 }
517
Andy Walls3b6fe582008-06-21 08:36:31 -0300518 CX18_DEBUG_INFO("Changing input from %d to %d\n",
519 cx->active_input, inp);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300520
Andy Walls3b6fe582008-06-21 08:36:31 -0300521 cx->active_input = inp;
522 /* Set the audio input to whatever is appropriate for the input type. */
523 cx->audio_input = cx->card->video_inputs[inp].audio_index;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300524
Andy Walls3b6fe582008-06-21 08:36:31 -0300525 /* prevent others from messing with the streams until
526 we're finished changing inputs. */
527 cx18_mute(cx);
528 cx18_video_set_io(cx);
529 cx18_audio_set_io(cx);
530 cx18_unmute(cx);
531 return 0;
532}
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300533
Andy Walls3b6fe582008-06-21 08:36:31 -0300534static int cx18_g_frequency(struct file *file, void *fh,
535 struct v4l2_frequency *vf)
536{
537 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
538
Andy Walls3b6fe582008-06-21 08:36:31 -0300539 if (vf->tuner != 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300540 return -EINVAL;
Andy Walls3b6fe582008-06-21 08:36:31 -0300541
542 cx18_call_i2c_clients(cx, VIDIOC_G_FREQUENCY, vf);
543 return 0;
544}
545
546int cx18_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
547{
548 struct cx18_open_id *id = fh;
549 struct cx18 *cx = id->cx;
550 int ret;
551
552 ret = v4l2_prio_check(&cx->prio, &id->prio);
553 if (ret)
554 return ret;
555
Andy Walls3b6fe582008-06-21 08:36:31 -0300556 if (vf->tuner != 0)
557 return -EINVAL;
558
559 cx18_mute(cx);
560 CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
561 cx18_call_i2c_clients(cx, VIDIOC_S_FREQUENCY, vf);
562 cx18_unmute(cx);
563 return 0;
564}
565
566static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
567{
568 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
569
Andy Walls3b6fe582008-06-21 08:36:31 -0300570 *std = cx->std;
571 return 0;
572}
573
574int cx18_s_std(struct file *file, void *fh, v4l2_std_id *std)
575{
576 struct cx18_open_id *id = fh;
577 struct cx18 *cx = id->cx;
578 int ret;
579
580 ret = v4l2_prio_check(&cx->prio, &id->prio);
581 if (ret)
582 return ret;
583
Andy Walls3b6fe582008-06-21 08:36:31 -0300584 if ((*std & V4L2_STD_ALL) == 0)
585 return -EINVAL;
586
587 if (*std == cx->std)
588 return 0;
589
590 if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
591 atomic_read(&cx->ana_capturing) > 0) {
592 /* Switching standard would turn off the radio or mess
593 with already running streams, prevent that by
594 returning EBUSY. */
595 return -EBUSY;
596 }
597
598 cx->std = *std;
599 cx->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
600 cx->params.is_50hz = cx->is_50hz = !cx->is_60hz;
601 cx->params.width = 720;
602 cx->params.height = cx->is_50hz ? 576 : 480;
603 cx->vbi.count = cx->is_50hz ? 18 : 12;
604 cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
605 cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
Andy Walls3b6fe582008-06-21 08:36:31 -0300606 CX18_DEBUG_INFO("Switching standard to %llx.\n",
607 (unsigned long long) cx->std);
608
609 /* Tuner */
610 cx18_call_i2c_clients(cx, VIDIOC_S_STD, &cx->std);
611 return 0;
612}
613
614static int cx18_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
615{
616 struct cx18_open_id *id = fh;
617 struct cx18 *cx = id->cx;
618 int ret;
619
620 ret = v4l2_prio_check(&cx->prio, &id->prio);
621 if (ret)
622 return ret;
623
Andy Walls3b6fe582008-06-21 08:36:31 -0300624 if (vt->index != 0)
625 return -EINVAL;
626
627 /* Setting tuner can only set audio mode */
628 cx18_call_i2c_clients(cx, VIDIOC_S_TUNER, vt);
629
630 return 0;
631}
632
633static int cx18_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
634{
635 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
636
Andy Walls3b6fe582008-06-21 08:36:31 -0300637 if (vt->index != 0)
638 return -EINVAL;
639
640 cx18_call_i2c_clients(cx, VIDIOC_G_TUNER, vt);
641
642 if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
643 strlcpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
644 vt->type = V4L2_TUNER_RADIO;
645 } else {
646 strlcpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
647 vt->type = V4L2_TUNER_ANALOG_TV;
648 }
649
650 return 0;
651}
652
653static int cx18_g_sliced_vbi_cap(struct file *file, void *fh,
654 struct v4l2_sliced_vbi_cap *cap)
655{
Andy Walls302df972009-01-31 00:33:02 -0300656 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
657 int set = cx->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
658 int f, l;
659
660 if (cap->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
661 for (f = 0; f < 2; f++) {
662 for (l = 0; l < 24; l++) {
663 if (valid_service_line(f, l, cx->is_50hz))
664 cap->service_lines[f][l] = set;
665 }
666 }
667 return 0;
668 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300669 return -EINVAL;
670}
671
672static int cx18_g_enc_index(struct file *file, void *fh,
673 struct v4l2_enc_idx *idx)
674{
675 return -EINVAL;
676}
677
678static int cx18_encoder_cmd(struct file *file, void *fh,
679 struct v4l2_encoder_cmd *enc)
680{
681 struct cx18_open_id *id = fh;
682 struct cx18 *cx = id->cx;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300683 u32 h;
Andy Walls3b6fe582008-06-21 08:36:31 -0300684
Andy Walls3b6fe582008-06-21 08:36:31 -0300685 switch (enc->cmd) {
686 case V4L2_ENC_CMD_START:
687 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
688 enc->flags = 0;
689 return cx18_start_capture(id);
690
691 case V4L2_ENC_CMD_STOP:
692 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
693 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
694 cx18_stop_capture(id,
695 enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
696 break;
697
698 case V4L2_ENC_CMD_PAUSE:
699 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
700 enc->flags = 0;
701 if (!atomic_read(&cx->ana_capturing))
702 return -EPERM;
703 if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
704 return 0;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300705 h = cx18_find_handle(cx);
706 if (h == CX18_INVALID_TASK_HANDLE) {
707 CX18_ERR("Can't find valid task handle for "
708 "V4L2_ENC_CMD_PAUSE\n");
709 return -EBADFD;
710 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300711 cx18_mute(cx);
Andy Wallsd3c5e702008-08-23 16:42:29 -0300712 cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, h);
Andy Walls3b6fe582008-06-21 08:36:31 -0300713 break;
714
715 case V4L2_ENC_CMD_RESUME:
716 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
717 enc->flags = 0;
718 if (!atomic_read(&cx->ana_capturing))
719 return -EPERM;
720 if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
721 return 0;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300722 h = cx18_find_handle(cx);
723 if (h == CX18_INVALID_TASK_HANDLE) {
724 CX18_ERR("Can't find valid task handle for "
725 "V4L2_ENC_CMD_RESUME\n");
726 return -EBADFD;
727 }
728 cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, h);
Andy Walls3b6fe582008-06-21 08:36:31 -0300729 cx18_unmute(cx);
730 break;
731
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300732 default:
Andy Walls3b6fe582008-06-21 08:36:31 -0300733 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
734 return -EINVAL;
735 }
736 return 0;
737}
738
739static int cx18_try_encoder_cmd(struct file *file, void *fh,
740 struct v4l2_encoder_cmd *enc)
741{
742 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
743
Andy Walls3b6fe582008-06-21 08:36:31 -0300744 switch (enc->cmd) {
745 case V4L2_ENC_CMD_START:
746 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
747 enc->flags = 0;
748 break;
749
750 case V4L2_ENC_CMD_STOP:
751 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
752 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
753 break;
754
755 case V4L2_ENC_CMD_PAUSE:
756 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
757 enc->flags = 0;
758 break;
759
760 case V4L2_ENC_CMD_RESUME:
761 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
762 enc->flags = 0;
763 break;
764
765 default:
766 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
767 return -EINVAL;
768 }
769 return 0;
770}
771
772static int cx18_log_status(struct file *file, void *fh)
773{
774 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
775 struct v4l2_input vidin;
776 struct v4l2_audio audin;
777 int i;
778
Andy Wallse0f28b62009-01-10 14:13:46 -0300779 CX18_INFO("================= START STATUS CARD #%d "
780 "=================\n", cx->num);
781 CX18_INFO("Version: %s Card: %s\n", CX18_VERSION, cx->card_name);
Andy Walls3b6fe582008-06-21 08:36:31 -0300782 if (cx->hw_flags & CX18_HW_TVEEPROM) {
783 struct tveeprom tv;
784
785 cx18_read_eeprom(cx, &tv);
786 }
787 cx18_call_i2c_clients(cx, VIDIOC_LOG_STATUS, NULL);
788 cx18_get_input(cx, cx->active_input, &vidin);
789 cx18_get_audio_input(cx, cx->audio_input, &audin);
790 CX18_INFO("Video Input: %s\n", vidin.name);
791 CX18_INFO("Audio Input: %s\n", audin.name);
Andy Walls8abdd002008-07-13 19:05:25 -0300792 mutex_lock(&cx->gpio_lock);
Hans Verkuil3d66c402008-06-21 11:19:34 -0300793 CX18_INFO("GPIO: direction 0x%08x, value 0x%08x\n",
794 cx->gpio_dir, cx->gpio_val);
Andy Walls8abdd002008-07-13 19:05:25 -0300795 mutex_unlock(&cx->gpio_lock);
Andy Walls3b6fe582008-06-21 08:36:31 -0300796 CX18_INFO("Tuner: %s\n",
797 test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ? "Radio" : "TV");
798 cx2341x_log_status(&cx->params, cx->name);
799 CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
800 for (i = 0; i < CX18_MAX_STREAMS; i++) {
801 struct cx18_stream *s = &cx->streams[i];
802
Andy Walls3d059132009-01-10 21:54:39 -0300803 if (s->video_dev == NULL || s->buffers == 0)
Andy Walls3b6fe582008-06-21 08:36:31 -0300804 continue;
805 CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
806 s->name, s->s_flags,
Andy Walls66c2a6b2008-12-08 23:02:45 -0300807 atomic_read(&s->q_full.buffers) * 100 / s->buffers,
Andy Walls3b6fe582008-06-21 08:36:31 -0300808 (s->buffers * s->buf_size) / 1024, s->buffers);
809 }
810 CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
811 (long long)cx->mpg_data_received,
812 (long long)cx->vbi_data_inserted);
813 CX18_INFO("================== END STATUS CARD #%d ==================\n", cx->num);
814 return 0;
815}
816
Hans Verkuil069b7472008-12-30 07:04:34 -0300817static long cx18_default(struct file *file, void *fh, int cmd, void *arg)
Andy Walls3b6fe582008-06-21 08:36:31 -0300818{
819 struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
820
821 switch (cmd) {
822 case VIDIOC_INT_S_AUDIO_ROUTING: {
823 struct v4l2_routing *route = arg;
Hans Verkuil37f89f92008-06-22 11:57:31 -0300824
825 CX18_DEBUG_IOCTL("VIDIOC_INT_S_AUDIO_ROUTING(%d, %d)\n",
826 route->input, route->output);
Andy Walls3b6fe582008-06-21 08:36:31 -0300827 cx18_audio_set_route(cx, route);
828 break;
829 }
Andy Walls02fa2722008-07-13 19:30:15 -0300830
831 case VIDIOC_INT_RESET: {
832 u32 val = *(u32 *)arg;
833
834 if ((val == 0) || (val & 0x01))
835 cx18_reset_ir_gpio(&cx->i2c_algo_cb_data[0]);
836 break;
837 }
838
Andy Walls3b6fe582008-06-21 08:36:31 -0300839 default:
Andy Walls3b6fe582008-06-21 08:36:31 -0300840 return -EINVAL;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300841 }
842 return 0;
843}
844
Hans Verkuil069b7472008-12-30 07:04:34 -0300845long cx18_v4l2_ioctl(struct file *filp, unsigned int cmd,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300846 unsigned long arg)
847{
Hans Verkuil37f89f92008-06-22 11:57:31 -0300848 struct video_device *vfd = video_devdata(filp);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300849 struct cx18_open_id *id = (struct cx18_open_id *)filp->private_data;
850 struct cx18 *cx = id->cx;
Hans Verkuil069b7472008-12-30 07:04:34 -0300851 long res;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300852
853 mutex_lock(&cx->serialize_lock);
Hans Verkuil37f89f92008-06-22 11:57:31 -0300854
855 if (cx18_debug & CX18_DBGFLG_IOCTL)
856 vfd->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
Hans Verkuilbec43662008-12-30 06:58:20 -0300857 res = video_ioctl2(filp, cmd, arg);
Hans Verkuil37f89f92008-06-22 11:57:31 -0300858 vfd->debug = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300859 mutex_unlock(&cx->serialize_lock);
860 return res;
861}
Andy Walls3b6fe582008-06-21 08:36:31 -0300862
Hans Verkuila3998102008-07-21 02:57:38 -0300863static const struct v4l2_ioctl_ops cx18_ioctl_ops = {
864 .vidioc_querycap = cx18_querycap,
865 .vidioc_g_priority = cx18_g_priority,
866 .vidioc_s_priority = cx18_s_priority,
867 .vidioc_s_audio = cx18_s_audio,
868 .vidioc_g_audio = cx18_g_audio,
869 .vidioc_enumaudio = cx18_enumaudio,
870 .vidioc_enum_input = cx18_enum_input,
871 .vidioc_cropcap = cx18_cropcap,
872 .vidioc_s_crop = cx18_s_crop,
873 .vidioc_g_crop = cx18_g_crop,
874 .vidioc_g_input = cx18_g_input,
875 .vidioc_s_input = cx18_s_input,
876 .vidioc_g_frequency = cx18_g_frequency,
877 .vidioc_s_frequency = cx18_s_frequency,
878 .vidioc_s_tuner = cx18_s_tuner,
879 .vidioc_g_tuner = cx18_g_tuner,
880 .vidioc_g_enc_index = cx18_g_enc_index,
881 .vidioc_g_std = cx18_g_std,
882 .vidioc_s_std = cx18_s_std,
883 .vidioc_log_status = cx18_log_status,
884 .vidioc_enum_fmt_vid_cap = cx18_enum_fmt_vid_cap,
885 .vidioc_encoder_cmd = cx18_encoder_cmd,
886 .vidioc_try_encoder_cmd = cx18_try_encoder_cmd,
887 .vidioc_g_fmt_vid_cap = cx18_g_fmt_vid_cap,
888 .vidioc_g_fmt_vbi_cap = cx18_g_fmt_vbi_cap,
889 .vidioc_g_fmt_sliced_vbi_cap = cx18_g_fmt_sliced_vbi_cap,
890 .vidioc_s_fmt_vid_cap = cx18_s_fmt_vid_cap,
891 .vidioc_s_fmt_vbi_cap = cx18_s_fmt_vbi_cap,
892 .vidioc_s_fmt_sliced_vbi_cap = cx18_s_fmt_sliced_vbi_cap,
893 .vidioc_try_fmt_vid_cap = cx18_try_fmt_vid_cap,
894 .vidioc_try_fmt_vbi_cap = cx18_try_fmt_vbi_cap,
895 .vidioc_try_fmt_sliced_vbi_cap = cx18_try_fmt_sliced_vbi_cap,
896 .vidioc_g_sliced_vbi_cap = cx18_g_sliced_vbi_cap,
897 .vidioc_g_chip_ident = cx18_g_chip_ident,
898#ifdef CONFIG_VIDEO_ADV_DEBUG
899 .vidioc_g_register = cx18_g_register,
900 .vidioc_s_register = cx18_s_register,
901#endif
902 .vidioc_default = cx18_default,
903 .vidioc_queryctrl = cx18_queryctrl,
904 .vidioc_querymenu = cx18_querymenu,
905 .vidioc_g_ext_ctrls = cx18_g_ext_ctrls,
906 .vidioc_s_ext_ctrls = cx18_s_ext_ctrls,
907 .vidioc_try_ext_ctrls = cx18_try_ext_ctrls,
908};
909
Andy Walls3b6fe582008-06-21 08:36:31 -0300910void cx18_set_funcs(struct video_device *vdev)
911{
Hans Verkuila3998102008-07-21 02:57:38 -0300912 vdev->ioctl_ops = &cx18_ioctl_ops;
Andy Walls3b6fe582008-06-21 08:36:31 -0300913}