blob: 1110bcb14e2f680a9436b1af21a21546bb2413b2 [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 Walls6afdeaf2010-05-23 18:53:35 -03007 * Copyright (C) 2008 Andy Walls <awalls@md.metrocast.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>
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030042
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -030043u16 cx18_service2vbi(int type)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030044{
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
Andy Wallsc1994082009-02-05 22:37:49 -030059/* Check if VBI services are allowed on the (field, line) for the video std */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030060static int valid_service_line(int field, int line, int is_pal)
61{
Andy Wallsc1994082009-02-05 22:37:49 -030062 return (is_pal && line >= 6 &&
63 ((field == 0 && line <= 23) || (field == 1 && line <= 22))) ||
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030064 (!is_pal && line >= 10 && line < 22);
65}
66
Andy Wallsc1994082009-02-05 22:37:49 -030067/*
68 * For a (field, line, std) and inbound potential set of services for that line,
69 * return the first valid service of those passed in the incoming set for that
70 * line in priority order:
71 * CC, VPS, or WSS over TELETEXT for well known lines
72 * TELETEXT, before VPS, before CC, before WSS, for other lines
73 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030074static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
75{
76 u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
77 int i;
78
79 set = set & valid_set;
80 if (set == 0 || !valid_service_line(field, line, is_pal))
81 return 0;
82 if (!is_pal) {
83 if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
84 return V4L2_SLICED_CAPTION_525;
85 } else {
86 if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
87 return V4L2_SLICED_VPS;
88 if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
89 return V4L2_SLICED_WSS_625;
90 if (line == 23)
91 return 0;
92 }
93 for (i = 0; i < 32; i++) {
94 if ((1 << i) & set)
95 return 1 << i;
96 }
97 return 0;
98}
99
Andy Wallsc1994082009-02-05 22:37:49 -0300100/*
101 * Expand the service_set of *fmt into valid service_lines for the std,
102 * and clear the passed in fmt->service_set
103 */
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -0300104void cx18_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300105{
106 u16 set = fmt->service_set;
107 int f, l;
108
109 fmt->service_set = 0;
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, set, is_pal);
113 }
114}
115
Andy Wallsc1994082009-02-05 22:37:49 -0300116/*
117 * Sanitize the service_lines in *fmt per the video std, and return 1
118 * if any service_line is left as valid after santization
119 */
Andy Walls302df972009-01-31 00:33:02 -0300120static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
121{
122 int f, l;
123 u16 set = 0;
124
125 for (f = 0; f < 2; f++) {
126 for (l = 0; l < 24; l++) {
127 fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
128 set |= fmt->service_lines[f][l];
129 }
130 }
131 return set != 0;
132}
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300133
Andy Wallsc1994082009-02-05 22:37:49 -0300134/* Compute the service_set from the assumed valid service_lines of *fmt */
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -0300135u16 cx18_get_service_set(struct v4l2_sliced_vbi_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300136{
137 int f, l;
138 u16 set = 0;
139
140 for (f = 0; f < 2; f++) {
141 for (l = 0; l < 24; l++)
142 set |= fmt->service_lines[f][l];
143 }
144 return set;
145}
146
Andy Walls3b6fe582008-06-21 08:36:31 -0300147static int cx18_g_fmt_vid_cap(struct file *file, void *fh,
148 struct v4l2_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300149{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300150 struct cx18_open_id *id = fh2id(fh);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300151 struct cx18 *cx = id->cx;
Steven Tothb7101de2011-04-06 08:32:56 -0300152 struct cx18_stream *s = &cx->streams[id->type];
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300153 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300154
Hans Verkuila75b9be2010-12-31 10:22:52 -0300155 pixfmt->width = cx->cxhdl.width;
156 pixfmt->height = cx->cxhdl.height;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300157 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
158 pixfmt->field = V4L2_FIELD_INTERLACED;
159 pixfmt->priv = 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300160 if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
Steven Tothb7101de2011-04-06 08:32:56 -0300161 pixfmt->pixelformat = s->pixelformat;
Simon Farnsworth09fc9802011-09-05 12:23:12 -0300162 pixfmt->sizeimage = s->vb_bytes_per_frame;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300163 pixfmt->bytesperline = 720;
Andy Walls3b6fe582008-06-21 08:36:31 -0300164 } else {
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300165 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
166 pixfmt->sizeimage = 128 * 1024;
167 pixfmt->bytesperline = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300168 }
169 return 0;
170}
171
Andy Walls3b6fe582008-06-21 08:36:31 -0300172static int cx18_g_fmt_vbi_cap(struct file *file, void *fh,
173 struct v4l2_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300174{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300175 struct cx18 *cx = fh2id(fh)->cx;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300176 struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300177
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300178 vbifmt->sampling_rate = 27000000;
Andy Wallsc1994082009-02-05 22:37:49 -0300179 vbifmt->offset = 248; /* FIXME - slightly wrong for both 50 & 60 Hz */
Andy Walls302df972009-01-31 00:33:02 -0300180 vbifmt->samples_per_line = vbi_active_samples - 4;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300181 vbifmt->sample_format = V4L2_PIX_FMT_GREY;
182 vbifmt->start[0] = cx->vbi.start[0];
183 vbifmt->start[1] = cx->vbi.start[1];
184 vbifmt->count[0] = vbifmt->count[1] = cx->vbi.count;
185 vbifmt->flags = 0;
186 vbifmt->reserved[0] = 0;
187 vbifmt->reserved[1] = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300188 return 0;
189}
190
Andy Walls3b6fe582008-06-21 08:36:31 -0300191static int cx18_g_fmt_sliced_vbi_cap(struct file *file, void *fh,
192 struct v4l2_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300193{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300194 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls302df972009-01-31 00:33:02 -0300195 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
196
Andy Wallsc1994082009-02-05 22:37:49 -0300197 /* sane, V4L2 spec compliant, defaults */
Andy Walls302df972009-01-31 00:33:02 -0300198 vbifmt->reserved[0] = 0;
199 vbifmt->reserved[1] = 0;
200 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
201 memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
Andy Wallsc1994082009-02-05 22:37:49 -0300202 vbifmt->service_set = 0;
Andy Walls302df972009-01-31 00:33:02 -0300203
Andy Wallsc1994082009-02-05 22:37:49 -0300204 /*
205 * Fetch the configured service_lines and total service_set from the
206 * digitizer/slicer. Note, cx18_av_vbi() wipes the passed in
207 * fmt->fmt.sliced under valid calling conditions
208 */
Hans Verkuiladd632c2010-03-14 12:24:15 -0300209 if (v4l2_subdev_call(cx->sd_av, vbi, g_sliced_fmt, &fmt->fmt.sliced))
Andy Wallsc1994082009-02-05 22:37:49 -0300210 return -EINVAL;
211
Andy Walls302df972009-01-31 00:33:02 -0300212 vbifmt->service_set = cx18_get_service_set(vbifmt);
213 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300214}
215
216static int cx18_try_fmt_vid_cap(struct file *file, void *fh,
217 struct v4l2_format *fmt)
218{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300219 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300220 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300221 int w = fmt->fmt.pix.width;
222 int h = fmt->fmt.pix.height;
Hans Verkuila4a78712009-02-06 15:31:59 -0300223 int min_h = 2;
Andy Walls3b6fe582008-06-21 08:36:31 -0300224
Andy Walls3b6fe582008-06-21 08:36:31 -0300225 w = min(w, 720);
Hans Verkuila4a78712009-02-06 15:31:59 -0300226 w = max(w, 2);
227 if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
228 /* YUV height must be a multiple of 32 */
229 h &= ~0x1f;
230 min_h = 32;
231 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300232 h = min(h, cx->is_50hz ? 576 : 480);
Hans Verkuila4a78712009-02-06 15:31:59 -0300233 h = max(h, min_h);
234
Andy Walls3b6fe582008-06-21 08:36:31 -0300235 fmt->fmt.pix.width = w;
236 fmt->fmt.pix.height = h;
237 return 0;
238}
239
240static int cx18_try_fmt_vbi_cap(struct file *file, void *fh,
241 struct v4l2_format *fmt)
242{
Andy Walls3b6fe582008-06-21 08:36:31 -0300243 return cx18_g_fmt_vbi_cap(file, fh, fmt);
244}
245
246static int cx18_try_fmt_sliced_vbi_cap(struct file *file, void *fh,
247 struct v4l2_format *fmt)
248{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300249 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls302df972009-01-31 00:33:02 -0300250 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
251
252 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
253 vbifmt->reserved[0] = 0;
254 vbifmt->reserved[1] = 0;
255
Andy Wallsc1994082009-02-05 22:37:49 -0300256 /* If given a service set, expand it validly & clear passed in set */
Andy Walls302df972009-01-31 00:33:02 -0300257 if (vbifmt->service_set)
258 cx18_expand_service_set(vbifmt, cx->is_50hz);
Andy Wallsc1994082009-02-05 22:37:49 -0300259 /* Sanitize the service_lines, and compute the new set if any valid */
260 if (check_service_set(vbifmt, cx->is_50hz))
261 vbifmt->service_set = cx18_get_service_set(vbifmt);
Andy Walls302df972009-01-31 00:33:02 -0300262 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300263}
264
265static int cx18_s_fmt_vid_cap(struct file *file, void *fh,
266 struct v4l2_format *fmt)
267{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300268 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300269 struct cx18 *cx = id->cx;
Hans Verkuil9e36eab2010-05-08 16:37:53 -0300270 struct v4l2_mbus_framefmt mbus_fmt;
Steven Tothb7101de2011-04-06 08:32:56 -0300271 struct cx18_stream *s = &cx->streams[id->type];
Andy Walls3b6fe582008-06-21 08:36:31 -0300272 int ret;
Hans Verkuileffc3462008-09-06 08:24:37 -0300273 int w, h;
Andy Walls3b6fe582008-06-21 08:36:31 -0300274
Andy Walls3b6fe582008-06-21 08:36:31 -0300275 ret = cx18_try_fmt_vid_cap(file, fh, fmt);
276 if (ret)
277 return ret;
Hans Verkuileffc3462008-09-06 08:24:37 -0300278 w = fmt->fmt.pix.width;
279 h = fmt->fmt.pix.height;
Andy Walls3b6fe582008-06-21 08:36:31 -0300280
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300281 if (cx->cxhdl.width == w && cx->cxhdl.height == h &&
282 s->pixelformat == fmt->fmt.pix.pixelformat)
Andy Walls3b6fe582008-06-21 08:36:31 -0300283 return 0;
284
285 if (atomic_read(&cx->ana_capturing) > 0)
286 return -EBUSY;
287
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300288 s->pixelformat = fmt->fmt.pix.pixelformat;
Simon Farnsworth09fc9802011-09-05 12:23:12 -0300289 /* HM12 YUV size is (Y=(h*720) + UV=(h*(720/2)))
290 UYUV YUV size is (Y=(h*720) + UV=(h*(720))) */
291 if (s->pixelformat == V4L2_PIX_FMT_HM12)
292 s->vb_bytes_per_frame = h * 720 * 3 / 2;
293 else
294 s->vb_bytes_per_frame = h * 720 * 2;
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300295
Hans Verkuila75b9be2010-12-31 10:22:52 -0300296 mbus_fmt.width = cx->cxhdl.width = w;
297 mbus_fmt.height = cx->cxhdl.height = h;
Hans Verkuil9e36eab2010-05-08 16:37:53 -0300298 mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
299 v4l2_subdev_call(cx->sd_av, video, s_mbus_fmt, &mbus_fmt);
Andy Walls3b6fe582008-06-21 08:36:31 -0300300 return cx18_g_fmt_vid_cap(file, fh, fmt);
301}
302
303static int cx18_s_fmt_vbi_cap(struct file *file, void *fh,
304 struct v4l2_format *fmt)
305{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300306 struct cx18_open_id *id = fh2id(fh);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300307 struct cx18 *cx = id->cx;
308 int ret;
309
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300310 /*
311 * Changing the Encoder's Raw VBI parameters won't have any effect
312 * if any analog capture is ongoing
313 */
314 if (!cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
Andy Walls3b6fe582008-06-21 08:36:31 -0300315 return -EBUSY;
316
Andy Wallsc1994082009-02-05 22:37:49 -0300317 /*
318 * Set the digitizer registers for raw active VBI.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300319 * Note cx18_av_vbi_wipes out a lot of the passed in fmt under valid
Andy Wallsc1994082009-02-05 22:37:49 -0300320 * calling conditions
321 */
Hans Verkuiladd632c2010-03-14 12:24:15 -0300322 ret = v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &fmt->fmt.vbi);
Andy Wallsc1994082009-02-05 22:37:49 -0300323 if (ret)
324 return ret;
325
326 /* Store our new v4l2 (non-)sliced VBI state */
Andy Walls3b6fe582008-06-21 08:36:31 -0300327 cx->vbi.sliced_in->service_set = 0;
Andy Wallsdd073432008-12-12 16:24:04 -0300328 cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
Andy Wallsc1994082009-02-05 22:37:49 -0300329
Andy Walls3b6fe582008-06-21 08:36:31 -0300330 return cx18_g_fmt_vbi_cap(file, fh, fmt);
331}
332
333static int cx18_s_fmt_sliced_vbi_cap(struct file *file, void *fh,
334 struct v4l2_format *fmt)
335{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300336 struct cx18_open_id *id = fh2id(fh);
Andy Walls302df972009-01-31 00:33:02 -0300337 struct cx18 *cx = id->cx;
338 int ret;
339 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
340
Andy Wallsc1994082009-02-05 22:37:49 -0300341 cx18_try_fmt_sliced_vbi_cap(file, fh, fmt);
342
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300343 /*
344 * Changing the Encoder's Raw VBI parameters won't have any effect
345 * if any analog capture is ongoing
346 */
347 if (cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
Andy Wallsc1994082009-02-05 22:37:49 -0300348 return -EBUSY;
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300349
Andy Wallsc1994082009-02-05 22:37:49 -0300350 /*
351 * Set the service_lines requested in the digitizer/slicer registers.
352 * Note, cx18_av_vbi() wipes some "impossible" service lines in the
353 * passed in fmt->fmt.sliced under valid calling conditions
354 */
Hans Verkuiladd632c2010-03-14 12:24:15 -0300355 ret = v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &fmt->fmt.sliced);
Andy Walls302df972009-01-31 00:33:02 -0300356 if (ret)
357 return ret;
Andy Wallsc1994082009-02-05 22:37:49 -0300358 /* Store our current v4l2 sliced VBI settings */
Andy Walls302df972009-01-31 00:33:02 -0300359 cx->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
Andy Walls302df972009-01-31 00:33:02 -0300360 memcpy(cx->vbi.sliced_in, vbifmt, sizeof(*cx->vbi.sliced_in));
361 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300362}
363
Hans Verkuil36ecd492008-06-25 06:00:17 -0300364#ifdef CONFIG_VIDEO_ADV_DEBUG
Andy Walls3b6fe582008-06-21 08:36:31 -0300365static int cx18_g_register(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300366 struct v4l2_dbg_register *reg)
Andy Walls3b6fe582008-06-21 08:36:31 -0300367{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300368 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300369
Hans Verkuil771d7732013-05-29 07:00:08 -0300370 if (reg->reg & 0x3)
371 return -EINVAL;
Hans Verkuil076c3452013-05-29 06:59:36 -0300372 if (reg->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
373 return -EINVAL;
374 reg->size = 4;
375 reg->val = cx18_read_enc(cx, reg->reg);
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300376 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300377}
378
379static int cx18_s_register(struct file *file, void *fh,
Hans Verkuil977ba3b2013-03-24 08:28:46 -0300380 const struct v4l2_dbg_register *reg)
Andy Walls3b6fe582008-06-21 08:36:31 -0300381{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300382 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300383
Hans Verkuil771d7732013-05-29 07:00:08 -0300384 if (reg->reg & 0x3)
385 return -EINVAL;
Hans Verkuil076c3452013-05-29 06:59:36 -0300386 if (reg->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
387 return -EINVAL;
388 cx18_write_enc(cx, reg->val, reg->reg);
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300389 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300390}
Hans Verkuil36ecd492008-06-25 06:00:17 -0300391#endif
Andy Walls3b6fe582008-06-21 08:36:31 -0300392
Andy Walls3b6fe582008-06-21 08:36:31 -0300393static int cx18_querycap(struct file *file, void *fh,
394 struct v4l2_capability *vcap)
395{
Simon Farnsworth09fc9802011-09-05 12:23:12 -0300396 struct cx18_open_id *id = fh2id(fh);
397 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300398
Andy Walls3b6fe582008-06-21 08:36:31 -0300399 strlcpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
400 strlcpy(vcap->card, cx->card_name, sizeof(vcap->card));
Andy Walls3d059132009-01-10 21:54:39 -0300401 snprintf(vcap->bus_info, sizeof(vcap->bus_info),
402 "PCI:%s", pci_name(cx->pci_dev));
Andy Walls3b6fe582008-06-21 08:36:31 -0300403 vcap->capabilities = cx->v4l2_cap; /* capabilities */
Simon Farnsworth09fc9802011-09-05 12:23:12 -0300404 if (id->type == CX18_ENC_STREAM_TYPE_YUV)
405 vcap->capabilities |= V4L2_CAP_STREAMING;
Andy Walls3b6fe582008-06-21 08:36:31 -0300406 return 0;
407}
408
409static int cx18_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
410{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300411 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300412
Andy Walls3b6fe582008-06-21 08:36:31 -0300413 return cx18_get_audio_input(cx, vin->index, vin);
414}
415
416static int cx18_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
417{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300418 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300419
Andy Walls3b6fe582008-06-21 08:36:31 -0300420 vin->index = cx->audio_input;
421 return cx18_get_audio_input(cx, vin->index, vin);
422}
423
Hans Verkuil0e8025b2012-09-04 11:59:31 -0300424static int cx18_s_audio(struct file *file, void *fh, const struct v4l2_audio *vout)
Andy Walls3b6fe582008-06-21 08:36:31 -0300425{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300426 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300427
Andy Walls3b6fe582008-06-21 08:36:31 -0300428 if (vout->index >= cx->nof_audio_inputs)
429 return -EINVAL;
430 cx->audio_input = vout->index;
431 cx18_audio_set_io(cx);
432 return 0;
433}
434
435static int cx18_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
436{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300437 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300438
Andy Walls3b6fe582008-06-21 08:36:31 -0300439 /* set it to defaults from our table */
440 return cx18_get_input(cx, vin->index, vin);
441}
442
443static int cx18_cropcap(struct file *file, void *fh,
444 struct v4l2_cropcap *cropcap)
445{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300446 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300447
Andy Walls3b6fe582008-06-21 08:36:31 -0300448 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
449 return -EINVAL;
450 cropcap->bounds.top = cropcap->bounds.left = 0;
451 cropcap->bounds.width = 720;
452 cropcap->bounds.height = cx->is_50hz ? 576 : 480;
453 cropcap->pixelaspect.numerator = cx->is_50hz ? 59 : 10;
454 cropcap->pixelaspect.denominator = cx->is_50hz ? 54 : 11;
455 cropcap->defrect = cropcap->bounds;
456 return 0;
457}
458
Hans Verkuil4f9965942012-09-05 05:10:48 -0300459static int cx18_s_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
Andy Walls3b6fe582008-06-21 08:36:31 -0300460{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300461 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300462 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300463
Andy Walls3b6fe582008-06-21 08:36:31 -0300464 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
465 return -EINVAL;
Andy Wallsfa3e7032009-02-16 02:23:25 -0300466 CX18_DEBUG_WARN("VIDIOC_S_CROP not implemented\n");
467 return -EINVAL;
Andy Walls3b6fe582008-06-21 08:36:31 -0300468}
469
470static int cx18_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
471{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300472 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300473
Andy Walls3b6fe582008-06-21 08:36:31 -0300474 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
475 return -EINVAL;
Andy Wallsfa3e7032009-02-16 02:23:25 -0300476 CX18_DEBUG_WARN("VIDIOC_G_CROP not implemented\n");
477 return -EINVAL;
Andy Walls3b6fe582008-06-21 08:36:31 -0300478}
479
480static int cx18_enum_fmt_vid_cap(struct file *file, void *fh,
481 struct v4l2_fmtdesc *fmt)
482{
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300483 static const struct v4l2_fmtdesc formats[] = {
484 { 0, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
485 "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12, { 0, 0, 0, 0 }
486 },
487 { 1, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FMT_FLAG_COMPRESSED,
488 "MPEG", V4L2_PIX_FMT_MPEG, { 0, 0, 0, 0 }
489 },
490 { 2, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
491 "UYVY 4:2:2", V4L2_PIX_FMT_UYVY, { 0, 0, 0, 0 }
492 },
493 };
494
Steven Tothb7101de2011-04-06 08:32:56 -0300495 if (fmt->index > ARRAY_SIZE(formats) - 1)
Andy Walls3b6fe582008-06-21 08:36:31 -0300496 return -EINVAL;
497 *fmt = formats[fmt->index];
498 return 0;
499}
500
501static int cx18_g_input(struct file *file, void *fh, unsigned int *i)
502{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300503 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300504
Andy Walls3b6fe582008-06-21 08:36:31 -0300505 *i = cx->active_input;
506 return 0;
507}
508
509int cx18_s_input(struct file *file, void *fh, unsigned int inp)
510{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300511 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300512 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300513
Roel Kluinde81c3c2009-07-02 16:09:25 -0300514 if (inp >= cx->nof_inputs)
Andy Walls3b6fe582008-06-21 08:36:31 -0300515 return -EINVAL;
516
517 if (inp == cx->active_input) {
518 CX18_DEBUG_INFO("Input unchanged\n");
519 return 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300520 }
521
Andy Walls3b6fe582008-06-21 08:36:31 -0300522 CX18_DEBUG_INFO("Changing input from %d to %d\n",
523 cx->active_input, inp);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300524
Andy Walls3b6fe582008-06-21 08:36:31 -0300525 cx->active_input = inp;
526 /* Set the audio input to whatever is appropriate for the input type. */
527 cx->audio_input = cx->card->video_inputs[inp].audio_index;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300528
Andy Walls3b6fe582008-06-21 08:36:31 -0300529 /* prevent others from messing with the streams until
530 we're finished changing inputs. */
531 cx18_mute(cx);
532 cx18_video_set_io(cx);
533 cx18_audio_set_io(cx);
534 cx18_unmute(cx);
535 return 0;
536}
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300537
Andy Walls3b6fe582008-06-21 08:36:31 -0300538static int cx18_g_frequency(struct file *file, void *fh,
539 struct v4l2_frequency *vf)
540{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300541 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300542
Andy Walls3b6fe582008-06-21 08:36:31 -0300543 if (vf->tuner != 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300544 return -EINVAL;
Andy Walls3b6fe582008-06-21 08:36:31 -0300545
Andy Wallsff2a2002009-02-20 23:52:13 -0300546 cx18_call_all(cx, tuner, g_frequency, vf);
Andy Walls3b6fe582008-06-21 08:36:31 -0300547 return 0;
548}
549
Hans Verkuilb530a442013-03-19 04:09:26 -0300550int cx18_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
Andy Walls3b6fe582008-06-21 08:36:31 -0300551{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300552 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300553 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300554
Andy Walls3b6fe582008-06-21 08:36:31 -0300555 if (vf->tuner != 0)
556 return -EINVAL;
557
558 cx18_mute(cx);
559 CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
Andy Wallsff2a2002009-02-20 23:52:13 -0300560 cx18_call_all(cx, tuner, s_frequency, vf);
Andy Walls3b6fe582008-06-21 08:36:31 -0300561 cx18_unmute(cx);
562 return 0;
563}
564
565static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
566{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300567 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300568
Andy Walls3b6fe582008-06-21 08:36:31 -0300569 *std = cx->std;
570 return 0;
571}
572
Hans Verkuil314527a2013-03-15 06:10:40 -0300573int cx18_s_std(struct file *file, void *fh, v4l2_std_id std)
Andy Walls3b6fe582008-06-21 08:36:31 -0300574{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300575 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300576 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300577
Hans Verkuil314527a2013-03-15 06:10:40 -0300578 if ((std & V4L2_STD_ALL) == 0)
Andy Walls3b6fe582008-06-21 08:36:31 -0300579 return -EINVAL;
580
Hans Verkuil314527a2013-03-15 06:10:40 -0300581 if (std == cx->std)
Andy Walls3b6fe582008-06-21 08:36:31 -0300582 return 0;
583
584 if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
585 atomic_read(&cx->ana_capturing) > 0) {
586 /* Switching standard would turn off the radio or mess
587 with already running streams, prevent that by
588 returning EBUSY. */
589 return -EBUSY;
590 }
591
Hans Verkuil314527a2013-03-15 06:10:40 -0300592 cx->std = std;
593 cx->is_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
Hans Verkuila75b9be2010-12-31 10:22:52 -0300594 cx->is_50hz = !cx->is_60hz;
595 cx2341x_handler_set_50hz(&cx->cxhdl, cx->is_50hz);
596 cx->cxhdl.width = 720;
597 cx->cxhdl.height = cx->is_50hz ? 576 : 480;
Andy Walls3b6fe582008-06-21 08:36:31 -0300598 cx->vbi.count = cx->is_50hz ? 18 : 12;
599 cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
600 cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
Andy Walls3b6fe582008-06-21 08:36:31 -0300601 CX18_DEBUG_INFO("Switching standard to %llx.\n",
602 (unsigned long long) cx->std);
603
604 /* Tuner */
Hans Verkuilf41737e2009-04-01 03:52:39 -0300605 cx18_call_all(cx, core, s_std, cx->std);
Andy Walls3b6fe582008-06-21 08:36:31 -0300606 return 0;
607}
608
Hans Verkuil2f73c7c2013-03-15 06:10:06 -0300609static int cx18_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
Andy Walls3b6fe582008-06-21 08:36:31 -0300610{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300611 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300612 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300613
Andy Walls3b6fe582008-06-21 08:36:31 -0300614 if (vt->index != 0)
615 return -EINVAL;
616
Andy Wallsff2a2002009-02-20 23:52:13 -0300617 cx18_call_all(cx, tuner, s_tuner, vt);
Andy Walls3b6fe582008-06-21 08:36:31 -0300618 return 0;
619}
620
621static int cx18_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
622{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300623 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300624
Andy Walls3b6fe582008-06-21 08:36:31 -0300625 if (vt->index != 0)
626 return -EINVAL;
627
Andy Wallsff2a2002009-02-20 23:52:13 -0300628 cx18_call_all(cx, tuner, g_tuner, vt);
Andy Walls3b6fe582008-06-21 08:36:31 -0300629
Hans Verkuild118e292011-06-25 10:28:21 -0300630 if (vt->type == V4L2_TUNER_RADIO)
Andy Walls3b6fe582008-06-21 08:36:31 -0300631 strlcpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
Hans Verkuild118e292011-06-25 10:28:21 -0300632 else
Andy Walls3b6fe582008-06-21 08:36:31 -0300633 strlcpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
Andy Walls3b6fe582008-06-21 08:36:31 -0300634 return 0;
635}
636
637static int cx18_g_sliced_vbi_cap(struct file *file, void *fh,
638 struct v4l2_sliced_vbi_cap *cap)
639{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300640 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls302df972009-01-31 00:33:02 -0300641 int set = cx->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
642 int f, l;
643
Andy Wallsc1994082009-02-05 22:37:49 -0300644 if (cap->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
645 return -EINVAL;
646
647 cap->service_set = 0;
648 for (f = 0; f < 2; f++) {
649 for (l = 0; l < 24; l++) {
650 if (valid_service_line(f, l, cx->is_50hz)) {
651 /*
652 * We can find all v4l2 supported vbi services
653 * for the standard, on a valid line for the std
654 */
655 cap->service_lines[f][l] = set;
656 cap->service_set |= set;
657 } else
658 cap->service_lines[f][l] = 0;
Andy Walls302df972009-01-31 00:33:02 -0300659 }
Andy Walls302df972009-01-31 00:33:02 -0300660 }
Andy Wallsc1994082009-02-05 22:37:49 -0300661 for (f = 0; f < 3; f++)
662 cap->reserved[f] = 0;
663 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300664}
665
Andy Walls82acdc82009-12-31 22:09:51 -0300666static int _cx18_process_idx_data(struct cx18_buffer *buf,
667 struct v4l2_enc_idx *idx)
668{
669 int consumed, remaining;
670 struct v4l2_enc_idx_entry *e_idx;
671 struct cx18_enc_idx_entry *e_buf;
672
673 /* Frame type lookup: 1=I, 2=P, 4=B */
674 const int mapping[8] = {
675 -1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P,
676 -1, V4L2_ENC_IDX_FRAME_B, -1, -1, -1
677 };
678
679 /*
680 * Assumption here is that a buf holds an integral number of
681 * struct cx18_enc_idx_entry objects and is properly aligned.
682 * This is enforced by the module options on IDX buffer sizes.
683 */
684 remaining = buf->bytesused - buf->readpos;
685 consumed = 0;
686 e_idx = &idx->entry[idx->entries];
687 e_buf = (struct cx18_enc_idx_entry *) &buf->buf[buf->readpos];
688
689 while (remaining >= sizeof(struct cx18_enc_idx_entry) &&
690 idx->entries < V4L2_ENC_IDX_ENTRIES) {
691
692 e_idx->offset = (((u64) le32_to_cpu(e_buf->offset_high)) << 32)
693 | le32_to_cpu(e_buf->offset_low);
694
695 e_idx->pts = (((u64) (le32_to_cpu(e_buf->pts_high) & 1)) << 32)
696 | le32_to_cpu(e_buf->pts_low);
697
698 e_idx->length = le32_to_cpu(e_buf->length);
699
700 e_idx->flags = mapping[le32_to_cpu(e_buf->flags) & 0x7];
701
702 e_idx->reserved[0] = 0;
703 e_idx->reserved[1] = 0;
704
705 idx->entries++;
706 e_idx = &idx->entry[idx->entries];
707 e_buf++;
708
709 remaining -= sizeof(struct cx18_enc_idx_entry);
710 consumed += sizeof(struct cx18_enc_idx_entry);
711 }
712
713 /* Swallow any partial entries at the end, if there are any */
714 if (remaining > 0 && remaining < sizeof(struct cx18_enc_idx_entry))
715 consumed += remaining;
716
717 buf->readpos += consumed;
718 return consumed;
719}
720
721static int cx18_process_idx_data(struct cx18_stream *s, struct cx18_mdl *mdl,
722 struct v4l2_enc_idx *idx)
723{
724 if (s->type != CX18_ENC_STREAM_TYPE_IDX)
725 return -EINVAL;
726
727 if (mdl->curr_buf == NULL)
728 mdl->curr_buf = list_first_entry(&mdl->buf_list,
729 struct cx18_buffer, list);
730
731 if (list_entry_is_past_end(mdl->curr_buf, &mdl->buf_list, list)) {
732 /*
733 * For some reason we've exhausted the buffers, but the MDL
734 * object still said some data was unread.
735 * Fix that and bail out.
736 */
737 mdl->readpos = mdl->bytesused;
738 return 0;
739 }
740
741 list_for_each_entry_from(mdl->curr_buf, &mdl->buf_list, list) {
742
743 /* Skip any empty buffers in the MDL */
744 if (mdl->curr_buf->readpos >= mdl->curr_buf->bytesused)
745 continue;
746
747 mdl->readpos += _cx18_process_idx_data(mdl->curr_buf, idx);
748
749 /* exit when MDL drained or request satisfied */
750 if (idx->entries >= V4L2_ENC_IDX_ENTRIES ||
751 mdl->curr_buf->readpos < mdl->curr_buf->bytesused ||
752 mdl->readpos >= mdl->bytesused)
753 break;
754 }
755 return 0;
756}
757
Andy Walls3b6fe582008-06-21 08:36:31 -0300758static int cx18_g_enc_index(struct file *file, void *fh,
759 struct v4l2_enc_idx *idx)
760{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300761 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls82acdc82009-12-31 22:09:51 -0300762 struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
763 s32 tmp;
764 struct cx18_mdl *mdl;
765
766 if (!cx18_stream_enabled(s)) /* Module options inhibited IDX stream */
767 return -EINVAL;
768
769 /* Compute the best case number of entries we can buffer */
770 tmp = s->buffers -
771 s->bufs_per_mdl * CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN;
772 if (tmp <= 0)
773 tmp = 1;
774 tmp = tmp * s->buf_size / sizeof(struct cx18_enc_idx_entry);
775
776 /* Fill out the header of the return structure */
777 idx->entries = 0;
778 idx->entries_cap = tmp;
779 memset(idx->reserved, 0, sizeof(idx->reserved));
780
781 /* Pull IDX MDLs and buffers from q_full and populate the entries */
782 do {
783 mdl = cx18_dequeue(s, &s->q_full);
784 if (mdl == NULL) /* No more IDX data right now */
785 break;
786
787 /* Extract the Index entry data from the MDL and buffers */
788 cx18_process_idx_data(s, mdl, idx);
789 if (mdl->readpos < mdl->bytesused) {
790 /* We finished with data remaining, push the MDL back */
791 cx18_push(s, mdl, &s->q_full);
792 break;
793 }
794
795 /* We drained this MDL, schedule it to go to the firmware */
796 cx18_enqueue(s, mdl, &s->q_free);
797
798 } while (idx->entries < V4L2_ENC_IDX_ENTRIES);
799
800 /* Tell the work handler to send free IDX MDLs to the firmware */
801 cx18_stream_load_fw_queue(s);
802 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300803}
804
Steven Tothb7101de2011-04-06 08:32:56 -0300805static struct videobuf_queue *cx18_vb_queue(struct cx18_open_id *id)
806{
807 struct videobuf_queue *q = NULL;
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300808 struct cx18 *cx = id->cx;
809 struct cx18_stream *s = &cx->streams[id->type];
Steven Tothb7101de2011-04-06 08:32:56 -0300810
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300811 switch (s->vb_type) {
Steven Tothb7101de2011-04-06 08:32:56 -0300812 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300813 q = &s->vbuf_q;
Steven Tothb7101de2011-04-06 08:32:56 -0300814 break;
815 case V4L2_BUF_TYPE_VBI_CAPTURE:
816 break;
817 default:
818 break;
819 }
820 return q;
821}
822
823static int cx18_streamon(struct file *file, void *priv,
824 enum v4l2_buf_type type)
825{
826 struct cx18_open_id *id = file->private_data;
827 struct cx18 *cx = id->cx;
828 struct cx18_stream *s = &cx->streams[id->type];
829
830 /* Start the hardware only if we're the video device */
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300831 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
832 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
Steven Tothb7101de2011-04-06 08:32:56 -0300833 return -EINVAL;
834
835 if (id->type != CX18_ENC_STREAM_TYPE_YUV)
836 return -EINVAL;
837
838 /* Establish a buffer timeout */
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300839 mod_timer(&s->vb_timeout, msecs_to_jiffies(2000) + jiffies);
Steven Tothb7101de2011-04-06 08:32:56 -0300840
841 return videobuf_streamon(cx18_vb_queue(id));
842}
843
844static int cx18_streamoff(struct file *file, void *priv,
845 enum v4l2_buf_type type)
846{
847 struct cx18_open_id *id = file->private_data;
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300848 struct cx18 *cx = id->cx;
849 struct cx18_stream *s = &cx->streams[id->type];
Steven Tothb7101de2011-04-06 08:32:56 -0300850
851 /* Start the hardware only if we're the video device */
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300852 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
853 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
Steven Tothb7101de2011-04-06 08:32:56 -0300854 return -EINVAL;
855
856 if (id->type != CX18_ENC_STREAM_TYPE_YUV)
857 return -EINVAL;
858
859 return videobuf_streamoff(cx18_vb_queue(id));
860}
861
862static int cx18_reqbufs(struct file *file, void *priv,
863 struct v4l2_requestbuffers *rb)
864{
865 struct cx18_open_id *id = file->private_data;
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300866 struct cx18 *cx = id->cx;
867 struct cx18_stream *s = &cx->streams[id->type];
Steven Tothb7101de2011-04-06 08:32:56 -0300868
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300869 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
870 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
Steven Tothb7101de2011-04-06 08:32:56 -0300871 return -EINVAL;
872
873 return videobuf_reqbufs(cx18_vb_queue(id), rb);
874}
875
876static int cx18_querybuf(struct file *file, void *priv,
877 struct v4l2_buffer *b)
878{
879 struct cx18_open_id *id = file->private_data;
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300880 struct cx18 *cx = id->cx;
881 struct cx18_stream *s = &cx->streams[id->type];
Steven Tothb7101de2011-04-06 08:32:56 -0300882
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300883 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
884 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
Steven Tothb7101de2011-04-06 08:32:56 -0300885 return -EINVAL;
886
887 return videobuf_querybuf(cx18_vb_queue(id), b);
888}
889
890static int cx18_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
891{
892 struct cx18_open_id *id = file->private_data;
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300893 struct cx18 *cx = id->cx;
894 struct cx18_stream *s = &cx->streams[id->type];
Steven Tothb7101de2011-04-06 08:32:56 -0300895
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300896 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
897 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
Steven Tothb7101de2011-04-06 08:32:56 -0300898 return -EINVAL;
899
900 return videobuf_qbuf(cx18_vb_queue(id), b);
901}
902
903static int cx18_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
904{
905 struct cx18_open_id *id = file->private_data;
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300906 struct cx18 *cx = id->cx;
907 struct cx18_stream *s = &cx->streams[id->type];
908
909 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
910 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
Steven Tothb7101de2011-04-06 08:32:56 -0300911 return -EINVAL;
912
913 return videobuf_dqbuf(cx18_vb_queue(id), b, file->f_flags & O_NONBLOCK);
914}
915
Andy Walls3b6fe582008-06-21 08:36:31 -0300916static int cx18_encoder_cmd(struct file *file, void *fh,
917 struct v4l2_encoder_cmd *enc)
918{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300919 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300920 struct cx18 *cx = id->cx;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300921 u32 h;
Andy Walls3b6fe582008-06-21 08:36:31 -0300922
Andy Walls3b6fe582008-06-21 08:36:31 -0300923 switch (enc->cmd) {
924 case V4L2_ENC_CMD_START:
925 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
926 enc->flags = 0;
927 return cx18_start_capture(id);
928
929 case V4L2_ENC_CMD_STOP:
930 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
931 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
932 cx18_stop_capture(id,
933 enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
934 break;
935
936 case V4L2_ENC_CMD_PAUSE:
937 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
938 enc->flags = 0;
939 if (!atomic_read(&cx->ana_capturing))
940 return -EPERM;
941 if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
942 return 0;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300943 h = cx18_find_handle(cx);
944 if (h == CX18_INVALID_TASK_HANDLE) {
945 CX18_ERR("Can't find valid task handle for "
946 "V4L2_ENC_CMD_PAUSE\n");
947 return -EBADFD;
948 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300949 cx18_mute(cx);
Andy Wallsd3c5e702008-08-23 16:42:29 -0300950 cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, h);
Andy Walls3b6fe582008-06-21 08:36:31 -0300951 break;
952
953 case V4L2_ENC_CMD_RESUME:
954 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
955 enc->flags = 0;
956 if (!atomic_read(&cx->ana_capturing))
957 return -EPERM;
958 if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
959 return 0;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300960 h = cx18_find_handle(cx);
961 if (h == CX18_INVALID_TASK_HANDLE) {
962 CX18_ERR("Can't find valid task handle for "
963 "V4L2_ENC_CMD_RESUME\n");
964 return -EBADFD;
965 }
966 cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, h);
Andy Walls3b6fe582008-06-21 08:36:31 -0300967 cx18_unmute(cx);
968 break;
969
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300970 default:
Andy Walls3b6fe582008-06-21 08:36:31 -0300971 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
972 return -EINVAL;
973 }
974 return 0;
975}
976
977static int cx18_try_encoder_cmd(struct file *file, void *fh,
978 struct v4l2_encoder_cmd *enc)
979{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300980 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300981
Andy Walls3b6fe582008-06-21 08:36:31 -0300982 switch (enc->cmd) {
983 case V4L2_ENC_CMD_START:
984 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
985 enc->flags = 0;
986 break;
987
988 case V4L2_ENC_CMD_STOP:
989 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
990 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
991 break;
992
993 case V4L2_ENC_CMD_PAUSE:
994 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
995 enc->flags = 0;
996 break;
997
998 case V4L2_ENC_CMD_RESUME:
999 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1000 enc->flags = 0;
1001 break;
1002
1003 default:
1004 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1005 return -EINVAL;
1006 }
1007 return 0;
1008}
1009
1010static int cx18_log_status(struct file *file, void *fh)
1011{
Hans Verkuil0b5f2652011-03-12 06:35:33 -03001012 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -03001013 struct v4l2_input vidin;
1014 struct v4l2_audio audin;
1015 int i;
1016
Andy Wallse0f28b62009-01-10 14:13:46 -03001017 CX18_INFO("Version: %s Card: %s\n", CX18_VERSION, cx->card_name);
Andy Walls3b6fe582008-06-21 08:36:31 -03001018 if (cx->hw_flags & CX18_HW_TVEEPROM) {
1019 struct tveeprom tv;
1020
1021 cx18_read_eeprom(cx, &tv);
1022 }
Andy Wallsff2a2002009-02-20 23:52:13 -03001023 cx18_call_all(cx, core, log_status);
Andy Walls3b6fe582008-06-21 08:36:31 -03001024 cx18_get_input(cx, cx->active_input, &vidin);
1025 cx18_get_audio_input(cx, cx->audio_input, &audin);
1026 CX18_INFO("Video Input: %s\n", vidin.name);
1027 CX18_INFO("Audio Input: %s\n", audin.name);
Andy Walls8abdd002008-07-13 19:05:25 -03001028 mutex_lock(&cx->gpio_lock);
Hans Verkuil3d66c402008-06-21 11:19:34 -03001029 CX18_INFO("GPIO: direction 0x%08x, value 0x%08x\n",
1030 cx->gpio_dir, cx->gpio_val);
Andy Walls8abdd002008-07-13 19:05:25 -03001031 mutex_unlock(&cx->gpio_lock);
Andy Walls3b6fe582008-06-21 08:36:31 -03001032 CX18_INFO("Tuner: %s\n",
1033 test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ? "Radio" : "TV");
Hans Verkuila75b9be2010-12-31 10:22:52 -03001034 v4l2_ctrl_handler_log_status(&cx->cxhdl.hdl, cx->v4l2_dev.name);
Andy Walls3b6fe582008-06-21 08:36:31 -03001035 CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
1036 for (i = 0; i < CX18_MAX_STREAMS; i++) {
1037 struct cx18_stream *s = &cx->streams[i];
1038
Andy Walls3d059132009-01-10 21:54:39 -03001039 if (s->video_dev == NULL || s->buffers == 0)
Andy Walls3b6fe582008-06-21 08:36:31 -03001040 continue;
1041 CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
1042 s->name, s->s_flags,
Andy Walls52fcb3e2009-11-08 23:45:24 -03001043 atomic_read(&s->q_full.depth) * s->bufs_per_mdl * 100
1044 / s->buffers,
Andy Walls3b6fe582008-06-21 08:36:31 -03001045 (s->buffers * s->buf_size) / 1024, s->buffers);
1046 }
1047 CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
1048 (long long)cx->mpg_data_received,
1049 (long long)cx->vbi_data_inserted);
Andy Walls3b6fe582008-06-21 08:36:31 -03001050 return 0;
1051}
1052
Hans Verkuil99cd47bc2011-03-11 19:00:56 -03001053static long cx18_default(struct file *file, void *fh, bool valid_prio,
Mauro Carvalho Chehab6d43be72013-03-26 08:04:52 -03001054 unsigned int cmd, void *arg)
Andy Walls3b6fe582008-06-21 08:36:31 -03001055{
Hans Verkuil0b5f2652011-03-12 06:35:33 -03001056 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -03001057
1058 switch (cmd) {
Andy Walls02fa2722008-07-13 19:30:15 -03001059 case VIDIOC_INT_RESET: {
1060 u32 val = *(u32 *)arg;
1061
1062 if ((val == 0) || (val & 0x01))
Andy Wallseefe1012009-02-21 18:42:49 -03001063 cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL, core, reset,
1064 (u32) CX18_GPIO_RESET_Z8F0811);
Andy Walls02fa2722008-07-13 19:30:15 -03001065 break;
1066 }
1067
Andy Walls3b6fe582008-06-21 08:36:31 -03001068 default:
Hans Verkuild1c754a2012-04-19 12:36:03 -03001069 return -ENOTTY;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001070 }
1071 return 0;
1072}
1073
Hans Verkuila3998102008-07-21 02:57:38 -03001074static const struct v4l2_ioctl_ops cx18_ioctl_ops = {
1075 .vidioc_querycap = cx18_querycap,
Hans Verkuila3998102008-07-21 02:57:38 -03001076 .vidioc_s_audio = cx18_s_audio,
1077 .vidioc_g_audio = cx18_g_audio,
1078 .vidioc_enumaudio = cx18_enumaudio,
1079 .vidioc_enum_input = cx18_enum_input,
1080 .vidioc_cropcap = cx18_cropcap,
1081 .vidioc_s_crop = cx18_s_crop,
1082 .vidioc_g_crop = cx18_g_crop,
1083 .vidioc_g_input = cx18_g_input,
1084 .vidioc_s_input = cx18_s_input,
1085 .vidioc_g_frequency = cx18_g_frequency,
1086 .vidioc_s_frequency = cx18_s_frequency,
1087 .vidioc_s_tuner = cx18_s_tuner,
1088 .vidioc_g_tuner = cx18_g_tuner,
1089 .vidioc_g_enc_index = cx18_g_enc_index,
1090 .vidioc_g_std = cx18_g_std,
1091 .vidioc_s_std = cx18_s_std,
1092 .vidioc_log_status = cx18_log_status,
1093 .vidioc_enum_fmt_vid_cap = cx18_enum_fmt_vid_cap,
1094 .vidioc_encoder_cmd = cx18_encoder_cmd,
1095 .vidioc_try_encoder_cmd = cx18_try_encoder_cmd,
1096 .vidioc_g_fmt_vid_cap = cx18_g_fmt_vid_cap,
1097 .vidioc_g_fmt_vbi_cap = cx18_g_fmt_vbi_cap,
1098 .vidioc_g_fmt_sliced_vbi_cap = cx18_g_fmt_sliced_vbi_cap,
1099 .vidioc_s_fmt_vid_cap = cx18_s_fmt_vid_cap,
1100 .vidioc_s_fmt_vbi_cap = cx18_s_fmt_vbi_cap,
1101 .vidioc_s_fmt_sliced_vbi_cap = cx18_s_fmt_sliced_vbi_cap,
1102 .vidioc_try_fmt_vid_cap = cx18_try_fmt_vid_cap,
1103 .vidioc_try_fmt_vbi_cap = cx18_try_fmt_vbi_cap,
1104 .vidioc_try_fmt_sliced_vbi_cap = cx18_try_fmt_sliced_vbi_cap,
1105 .vidioc_g_sliced_vbi_cap = cx18_g_sliced_vbi_cap,
Hans Verkuila3998102008-07-21 02:57:38 -03001106#ifdef CONFIG_VIDEO_ADV_DEBUG
1107 .vidioc_g_register = cx18_g_register,
1108 .vidioc_s_register = cx18_s_register,
1109#endif
1110 .vidioc_default = cx18_default,
Steven Tothb7101de2011-04-06 08:32:56 -03001111 .vidioc_streamon = cx18_streamon,
1112 .vidioc_streamoff = cx18_streamoff,
1113 .vidioc_reqbufs = cx18_reqbufs,
1114 .vidioc_querybuf = cx18_querybuf,
1115 .vidioc_qbuf = cx18_qbuf,
1116 .vidioc_dqbuf = cx18_dqbuf,
Hans Verkuila3998102008-07-21 02:57:38 -03001117};
1118
Andy Walls3b6fe582008-06-21 08:36:31 -03001119void cx18_set_funcs(struct video_device *vdev)
1120{
Hans Verkuila3998102008-07-21 02:57:38 -03001121 vdev->ioctl_ops = &cx18_ioctl_ops;
Andy Walls3b6fe582008-06-21 08:36:31 -03001122}