blob: 777d7265c8a861483c4dc6d0979c765e7ce21894 [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>
42#include <media/v4l2-chip-ident.h>
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030043
Steven Tothb7101de2011-04-06 08:32:56 -030044static struct v4l2_fmtdesc formats[] = {
45 { 0, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
46 "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12, { 0, 0, 0, 0 }
47 },
48 { 1, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FMT_FLAG_COMPRESSED,
49 "MPEG", V4L2_PIX_FMT_MPEG, { 0, 0, 0, 0 }
50 },
51 { 2, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
52 "YUYV 4:2:2", V4L2_PIX_FMT_YUYV, { 0, 0, 0, 0 }
53 },
54};
55
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -030056u16 cx18_service2vbi(int type)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030057{
58 switch (type) {
59 case V4L2_SLICED_TELETEXT_B:
60 return CX18_SLICED_TYPE_TELETEXT_B;
61 case V4L2_SLICED_CAPTION_525:
62 return CX18_SLICED_TYPE_CAPTION_525;
63 case V4L2_SLICED_WSS_625:
64 return CX18_SLICED_TYPE_WSS_625;
65 case V4L2_SLICED_VPS:
66 return CX18_SLICED_TYPE_VPS;
67 default:
68 return 0;
69 }
70}
71
Andy Wallsc1994082009-02-05 22:37:49 -030072/* Check if VBI services are allowed on the (field, line) for the video std */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030073static int valid_service_line(int field, int line, int is_pal)
74{
Andy Wallsc1994082009-02-05 22:37:49 -030075 return (is_pal && line >= 6 &&
76 ((field == 0 && line <= 23) || (field == 1 && line <= 22))) ||
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030077 (!is_pal && line >= 10 && line < 22);
78}
79
Andy Wallsc1994082009-02-05 22:37:49 -030080/*
81 * For a (field, line, std) and inbound potential set of services for that line,
82 * return the first valid service of those passed in the incoming set for that
83 * line in priority order:
84 * CC, VPS, or WSS over TELETEXT for well known lines
85 * TELETEXT, before VPS, before CC, before WSS, for other lines
86 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030087static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
88{
89 u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
90 int i;
91
92 set = set & valid_set;
93 if (set == 0 || !valid_service_line(field, line, is_pal))
94 return 0;
95 if (!is_pal) {
96 if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
97 return V4L2_SLICED_CAPTION_525;
98 } else {
99 if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
100 return V4L2_SLICED_VPS;
101 if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
102 return V4L2_SLICED_WSS_625;
103 if (line == 23)
104 return 0;
105 }
106 for (i = 0; i < 32; i++) {
107 if ((1 << i) & set)
108 return 1 << i;
109 }
110 return 0;
111}
112
Andy Wallsc1994082009-02-05 22:37:49 -0300113/*
114 * Expand the service_set of *fmt into valid service_lines for the std,
115 * and clear the passed in fmt->service_set
116 */
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -0300117void cx18_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300118{
119 u16 set = fmt->service_set;
120 int f, l;
121
122 fmt->service_set = 0;
123 for (f = 0; f < 2; f++) {
124 for (l = 0; l < 24; l++)
125 fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
126 }
127}
128
Andy Wallsc1994082009-02-05 22:37:49 -0300129/*
130 * Sanitize the service_lines in *fmt per the video std, and return 1
131 * if any service_line is left as valid after santization
132 */
Andy Walls302df972009-01-31 00:33:02 -0300133static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
134{
135 int f, l;
136 u16 set = 0;
137
138 for (f = 0; f < 2; f++) {
139 for (l = 0; l < 24; l++) {
140 fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
141 set |= fmt->service_lines[f][l];
142 }
143 }
144 return set != 0;
145}
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300146
Andy Wallsc1994082009-02-05 22:37:49 -0300147/* Compute the service_set from the assumed valid service_lines of *fmt */
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -0300148u16 cx18_get_service_set(struct v4l2_sliced_vbi_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300149{
150 int f, l;
151 u16 set = 0;
152
153 for (f = 0; f < 2; f++) {
154 for (l = 0; l < 24; l++)
155 set |= fmt->service_lines[f][l];
156 }
157 return set;
158}
159
Andy Walls3b6fe582008-06-21 08:36:31 -0300160static int cx18_g_fmt_vid_cap(struct file *file, void *fh,
161 struct v4l2_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300162{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300163 struct cx18_open_id *id = fh2id(fh);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300164 struct cx18 *cx = id->cx;
Steven Tothb7101de2011-04-06 08:32:56 -0300165 struct cx18_stream *s = &cx->streams[id->type];
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300166 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300167
Hans Verkuila75b9be2010-12-31 10:22:52 -0300168 pixfmt->width = cx->cxhdl.width;
169 pixfmt->height = cx->cxhdl.height;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300170 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
171 pixfmt->field = V4L2_FIELD_INTERLACED;
172 pixfmt->priv = 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300173 if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
Steven Tothb7101de2011-04-06 08:32:56 -0300174 pixfmt->pixelformat = s->pixelformat;
Hans Verkuila4a78712009-02-06 15:31:59 -0300175 /* YUV size is (Y=(h*720) + UV=(h*(720/2))) */
176 pixfmt->sizeimage = pixfmt->height * 720 * 3 / 2;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300177 pixfmt->bytesperline = 720;
Andy Walls3b6fe582008-06-21 08:36:31 -0300178 } else {
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300179 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
180 pixfmt->sizeimage = 128 * 1024;
181 pixfmt->bytesperline = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300182 }
183 return 0;
184}
185
Andy Walls3b6fe582008-06-21 08:36:31 -0300186static int cx18_g_fmt_vbi_cap(struct file *file, void *fh,
187 struct v4l2_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300188{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300189 struct cx18 *cx = fh2id(fh)->cx;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300190 struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300191
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300192 vbifmt->sampling_rate = 27000000;
Andy Wallsc1994082009-02-05 22:37:49 -0300193 vbifmt->offset = 248; /* FIXME - slightly wrong for both 50 & 60 Hz */
Andy Walls302df972009-01-31 00:33:02 -0300194 vbifmt->samples_per_line = vbi_active_samples - 4;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300195 vbifmt->sample_format = V4L2_PIX_FMT_GREY;
196 vbifmt->start[0] = cx->vbi.start[0];
197 vbifmt->start[1] = cx->vbi.start[1];
198 vbifmt->count[0] = vbifmt->count[1] = cx->vbi.count;
199 vbifmt->flags = 0;
200 vbifmt->reserved[0] = 0;
201 vbifmt->reserved[1] = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300202 return 0;
203}
204
Andy Walls3b6fe582008-06-21 08:36:31 -0300205static int cx18_g_fmt_sliced_vbi_cap(struct file *file, void *fh,
206 struct v4l2_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300207{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300208 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls302df972009-01-31 00:33:02 -0300209 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
210
Andy Wallsc1994082009-02-05 22:37:49 -0300211 /* sane, V4L2 spec compliant, defaults */
Andy Walls302df972009-01-31 00:33:02 -0300212 vbifmt->reserved[0] = 0;
213 vbifmt->reserved[1] = 0;
214 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
215 memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
Andy Wallsc1994082009-02-05 22:37:49 -0300216 vbifmt->service_set = 0;
Andy Walls302df972009-01-31 00:33:02 -0300217
Andy Wallsc1994082009-02-05 22:37:49 -0300218 /*
219 * Fetch the configured service_lines and total service_set from the
220 * digitizer/slicer. Note, cx18_av_vbi() wipes the passed in
221 * fmt->fmt.sliced under valid calling conditions
222 */
Hans Verkuiladd632c2010-03-14 12:24:15 -0300223 if (v4l2_subdev_call(cx->sd_av, vbi, g_sliced_fmt, &fmt->fmt.sliced))
Andy Wallsc1994082009-02-05 22:37:49 -0300224 return -EINVAL;
225
226 /* Ensure V4L2 spec compliant output */
227 vbifmt->reserved[0] = 0;
228 vbifmt->reserved[1] = 0;
229 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
Andy Walls302df972009-01-31 00:33:02 -0300230 vbifmt->service_set = cx18_get_service_set(vbifmt);
231 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300232}
233
234static int cx18_try_fmt_vid_cap(struct file *file, void *fh,
235 struct v4l2_format *fmt)
236{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300237 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300238 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300239 int w = fmt->fmt.pix.width;
240 int h = fmt->fmt.pix.height;
Hans Verkuila4a78712009-02-06 15:31:59 -0300241 int min_h = 2;
Andy Walls3b6fe582008-06-21 08:36:31 -0300242
Andy Walls3b6fe582008-06-21 08:36:31 -0300243 w = min(w, 720);
Hans Verkuila4a78712009-02-06 15:31:59 -0300244 w = max(w, 2);
245 if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
246 /* YUV height must be a multiple of 32 */
247 h &= ~0x1f;
248 min_h = 32;
249 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300250 h = min(h, cx->is_50hz ? 576 : 480);
Hans Verkuila4a78712009-02-06 15:31:59 -0300251 h = max(h, min_h);
252
Andy Walls3b6fe582008-06-21 08:36:31 -0300253 fmt->fmt.pix.width = w;
254 fmt->fmt.pix.height = h;
255 return 0;
256}
257
258static int cx18_try_fmt_vbi_cap(struct file *file, void *fh,
259 struct v4l2_format *fmt)
260{
Andy Walls3b6fe582008-06-21 08:36:31 -0300261 return cx18_g_fmt_vbi_cap(file, fh, fmt);
262}
263
264static int cx18_try_fmt_sliced_vbi_cap(struct file *file, void *fh,
265 struct v4l2_format *fmt)
266{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300267 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls302df972009-01-31 00:33:02 -0300268 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
269
270 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
271 vbifmt->reserved[0] = 0;
272 vbifmt->reserved[1] = 0;
273
Andy Wallsc1994082009-02-05 22:37:49 -0300274 /* If given a service set, expand it validly & clear passed in set */
Andy Walls302df972009-01-31 00:33:02 -0300275 if (vbifmt->service_set)
276 cx18_expand_service_set(vbifmt, cx->is_50hz);
Andy Wallsc1994082009-02-05 22:37:49 -0300277 /* Sanitize the service_lines, and compute the new set if any valid */
278 if (check_service_set(vbifmt, cx->is_50hz))
279 vbifmt->service_set = cx18_get_service_set(vbifmt);
Andy Walls302df972009-01-31 00:33:02 -0300280 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300281}
282
283static int cx18_s_fmt_vid_cap(struct file *file, void *fh,
284 struct v4l2_format *fmt)
285{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300286 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300287 struct cx18 *cx = id->cx;
Hans Verkuil9e36eab2010-05-08 16:37:53 -0300288 struct v4l2_mbus_framefmt mbus_fmt;
Steven Tothb7101de2011-04-06 08:32:56 -0300289 struct cx18_stream *s = &cx->streams[id->type];
Andy Walls3b6fe582008-06-21 08:36:31 -0300290 int ret;
Hans Verkuileffc3462008-09-06 08:24:37 -0300291 int w, h;
Andy Walls3b6fe582008-06-21 08:36:31 -0300292
Andy Walls3b6fe582008-06-21 08:36:31 -0300293 ret = cx18_try_fmt_vid_cap(file, fh, fmt);
294 if (ret)
295 return ret;
Hans Verkuileffc3462008-09-06 08:24:37 -0300296 w = fmt->fmt.pix.width;
297 h = fmt->fmt.pix.height;
Andy Walls3b6fe582008-06-21 08:36:31 -0300298
Steven Tothb7101de2011-04-06 08:32:56 -0300299 s->pixelformat = fmt->fmt.pix.pixelformat;
300 s->vbheight = h;
301 s->vbwidth = w;
302
Hans Verkuila75b9be2010-12-31 10:22:52 -0300303 if (cx->cxhdl.width == w && cx->cxhdl.height == h)
Andy Walls3b6fe582008-06-21 08:36:31 -0300304 return 0;
305
306 if (atomic_read(&cx->ana_capturing) > 0)
307 return -EBUSY;
308
Hans Verkuila75b9be2010-12-31 10:22:52 -0300309 mbus_fmt.width = cx->cxhdl.width = w;
310 mbus_fmt.height = cx->cxhdl.height = h;
Hans Verkuil9e36eab2010-05-08 16:37:53 -0300311 mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
312 v4l2_subdev_call(cx->sd_av, video, s_mbus_fmt, &mbus_fmt);
Andy Walls3b6fe582008-06-21 08:36:31 -0300313 return cx18_g_fmt_vid_cap(file, fh, fmt);
314}
315
316static int cx18_s_fmt_vbi_cap(struct file *file, void *fh,
317 struct v4l2_format *fmt)
318{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300319 struct cx18_open_id *id = fh2id(fh);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300320 struct cx18 *cx = id->cx;
321 int ret;
322
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300323 /*
324 * Changing the Encoder's Raw VBI parameters won't have any effect
325 * if any analog capture is ongoing
326 */
327 if (!cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
Andy Walls3b6fe582008-06-21 08:36:31 -0300328 return -EBUSY;
329
Andy Wallsc1994082009-02-05 22:37:49 -0300330 /*
331 * Set the digitizer registers for raw active VBI.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300332 * Note cx18_av_vbi_wipes out a lot of the passed in fmt under valid
Andy Wallsc1994082009-02-05 22:37:49 -0300333 * calling conditions
334 */
Hans Verkuiladd632c2010-03-14 12:24:15 -0300335 ret = v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &fmt->fmt.vbi);
Andy Wallsc1994082009-02-05 22:37:49 -0300336 if (ret)
337 return ret;
338
339 /* Store our new v4l2 (non-)sliced VBI state */
Andy Walls3b6fe582008-06-21 08:36:31 -0300340 cx->vbi.sliced_in->service_set = 0;
Andy Wallsdd073432008-12-12 16:24:04 -0300341 cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
Andy Wallsc1994082009-02-05 22:37:49 -0300342
Andy Walls3b6fe582008-06-21 08:36:31 -0300343 return cx18_g_fmt_vbi_cap(file, fh, fmt);
344}
345
346static int cx18_s_fmt_sliced_vbi_cap(struct file *file, void *fh,
347 struct v4l2_format *fmt)
348{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300349 struct cx18_open_id *id = fh2id(fh);
Andy Walls302df972009-01-31 00:33:02 -0300350 struct cx18 *cx = id->cx;
351 int ret;
352 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
353
Andy Wallsc1994082009-02-05 22:37:49 -0300354 cx18_try_fmt_sliced_vbi_cap(file, fh, fmt);
355
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300356 /*
357 * Changing the Encoder's Raw VBI parameters won't have any effect
358 * if any analog capture is ongoing
359 */
360 if (cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
Andy Wallsc1994082009-02-05 22:37:49 -0300361 return -EBUSY;
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300362
Andy Wallsc1994082009-02-05 22:37:49 -0300363 /*
364 * Set the service_lines requested in the digitizer/slicer registers.
365 * Note, cx18_av_vbi() wipes some "impossible" service lines in the
366 * passed in fmt->fmt.sliced under valid calling conditions
367 */
Hans Verkuiladd632c2010-03-14 12:24:15 -0300368 ret = v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &fmt->fmt.sliced);
Andy Walls302df972009-01-31 00:33:02 -0300369 if (ret)
370 return ret;
Andy Wallsc1994082009-02-05 22:37:49 -0300371 /* Store our current v4l2 sliced VBI settings */
Andy Walls302df972009-01-31 00:33:02 -0300372 cx->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
Andy Walls302df972009-01-31 00:33:02 -0300373 memcpy(cx->vbi.sliced_in, vbifmt, sizeof(*cx->vbi.sliced_in));
374 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300375}
376
377static int cx18_g_chip_ident(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300378 struct v4l2_dbg_chip_ident *chip)
Andy Walls3b6fe582008-06-21 08:36:31 -0300379{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300380 struct cx18 *cx = fh2id(fh)->cx;
Andy Wallsff2a2002009-02-20 23:52:13 -0300381 int err = 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300382
Andy Walls3b6fe582008-06-21 08:36:31 -0300383 chip->ident = V4L2_IDENT_NONE;
384 chip->revision = 0;
Andy Wallsff2a2002009-02-20 23:52:13 -0300385 switch (chip->match.type) {
386 case V4L2_CHIP_MATCH_HOST:
387 switch (chip->match.addr) {
388 case 0:
389 chip->ident = V4L2_IDENT_CX23418;
390 chip->revision = cx18_read_reg(cx, 0xC72028);
391 break;
392 case 1:
393 /*
394 * The A/V decoder is always present, but in the rare
395 * case that the card doesn't have analog, we don't
396 * use it. We find it w/o using the cx->sd_av pointer
397 */
398 cx18_call_hw(cx, CX18_HW_418_AV,
399 core, g_chip_ident, chip);
400 break;
401 default:
402 /*
403 * Could return ident = V4L2_IDENT_UNKNOWN if we had
404 * other host chips at higher addresses, but we don't
405 */
406 err = -EINVAL; /* per V4L2 spec */
407 break;
408 }
409 break;
410 case V4L2_CHIP_MATCH_I2C_DRIVER:
411 /* If needed, returns V4L2_IDENT_AMBIGUOUS without extra work */
412 cx18_call_all(cx, core, g_chip_ident, chip);
413 break;
414 case V4L2_CHIP_MATCH_I2C_ADDR:
415 /*
416 * We could return V4L2_IDENT_UNKNOWN, but we don't do the work
417 * to look if a chip is at the address with no driver. That's a
418 * dangerous thing to do with EEPROMs anyway.
419 */
420 cx18_call_all(cx, core, g_chip_ident, chip);
421 break;
422 default:
423 err = -EINVAL;
424 break;
Andy Walls3b6fe582008-06-21 08:36:31 -0300425 }
Andy Wallsff2a2002009-02-20 23:52:13 -0300426 return err;
Andy Walls3b6fe582008-06-21 08:36:31 -0300427}
428
Hans Verkuil36ecd492008-06-25 06:00:17 -0300429#ifdef CONFIG_VIDEO_ADV_DEBUG
430static int cx18_cxc(struct cx18 *cx, unsigned int cmd, void *arg)
431{
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300432 struct v4l2_dbg_register *regs = arg;
Hans Verkuil36ecd492008-06-25 06:00:17 -0300433
434 if (!capable(CAP_SYS_ADMIN))
435 return -EPERM;
436 if (regs->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
437 return -EINVAL;
438
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300439 regs->size = 4;
Andy Wallsff2a2002009-02-20 23:52:13 -0300440 if (cmd == VIDIOC_DBG_S_REGISTER)
Andy Wallsb1526422008-08-30 16:03:44 -0300441 cx18_write_enc(cx, regs->val, regs->reg);
Andy Wallsff2a2002009-02-20 23:52:13 -0300442 else
443 regs->val = cx18_read_enc(cx, regs->reg);
Hans Verkuil36ecd492008-06-25 06:00:17 -0300444 return 0;
445}
446
Andy Walls3b6fe582008-06-21 08:36:31 -0300447static int cx18_g_register(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300448 struct v4l2_dbg_register *reg)
Andy Walls3b6fe582008-06-21 08:36:31 -0300449{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300450 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300451
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300452 if (v4l2_chip_match_host(&reg->match))
Andy Walls3b6fe582008-06-21 08:36:31 -0300453 return cx18_cxc(cx, VIDIOC_DBG_G_REGISTER, reg);
Andy Wallsff2a2002009-02-20 23:52:13 -0300454 /* FIXME - errors shouldn't be ignored */
455 cx18_call_all(cx, core, g_register, reg);
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300456 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300457}
458
459static int cx18_s_register(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300460 struct v4l2_dbg_register *reg)
Andy Walls3b6fe582008-06-21 08:36:31 -0300461{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300462 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300463
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300464 if (v4l2_chip_match_host(&reg->match))
Andy Walls3b6fe582008-06-21 08:36:31 -0300465 return cx18_cxc(cx, VIDIOC_DBG_S_REGISTER, reg);
Andy Wallsff2a2002009-02-20 23:52:13 -0300466 /* FIXME - errors shouldn't be ignored */
467 cx18_call_all(cx, core, s_register, reg);
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300468 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300469}
Hans Verkuil36ecd492008-06-25 06:00:17 -0300470#endif
Andy Walls3b6fe582008-06-21 08:36:31 -0300471
Andy Walls3b6fe582008-06-21 08:36:31 -0300472static int cx18_querycap(struct file *file, void *fh,
473 struct v4l2_capability *vcap)
474{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300475 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300476
Andy Walls3b6fe582008-06-21 08:36:31 -0300477 strlcpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
478 strlcpy(vcap->card, cx->card_name, sizeof(vcap->card));
Andy Walls3d059132009-01-10 21:54:39 -0300479 snprintf(vcap->bus_info, sizeof(vcap->bus_info),
480 "PCI:%s", pci_name(cx->pci_dev));
Andy Walls3b6fe582008-06-21 08:36:31 -0300481 vcap->version = CX18_DRIVER_VERSION; /* version */
482 vcap->capabilities = cx->v4l2_cap; /* capabilities */
483 return 0;
484}
485
486static int cx18_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
487{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300488 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300489
Andy Walls3b6fe582008-06-21 08:36:31 -0300490 return cx18_get_audio_input(cx, vin->index, vin);
491}
492
493static int cx18_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
494{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300495 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300496
Andy Walls3b6fe582008-06-21 08:36:31 -0300497 vin->index = cx->audio_input;
498 return cx18_get_audio_input(cx, vin->index, vin);
499}
500
501static int cx18_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
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 if (vout->index >= cx->nof_audio_inputs)
506 return -EINVAL;
507 cx->audio_input = vout->index;
508 cx18_audio_set_io(cx);
509 return 0;
510}
511
512static int cx18_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
513{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300514 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300515
Andy Walls3b6fe582008-06-21 08:36:31 -0300516 /* set it to defaults from our table */
517 return cx18_get_input(cx, vin->index, vin);
518}
519
520static int cx18_cropcap(struct file *file, void *fh,
521 struct v4l2_cropcap *cropcap)
522{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300523 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300524
Andy Walls3b6fe582008-06-21 08:36:31 -0300525 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
526 return -EINVAL;
527 cropcap->bounds.top = cropcap->bounds.left = 0;
528 cropcap->bounds.width = 720;
529 cropcap->bounds.height = cx->is_50hz ? 576 : 480;
530 cropcap->pixelaspect.numerator = cx->is_50hz ? 59 : 10;
531 cropcap->pixelaspect.denominator = cx->is_50hz ? 54 : 11;
532 cropcap->defrect = cropcap->bounds;
533 return 0;
534}
535
536static int cx18_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
537{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300538 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300539 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300540
Andy Walls3b6fe582008-06-21 08:36:31 -0300541 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
542 return -EINVAL;
Andy Wallsfa3e7032009-02-16 02:23:25 -0300543 CX18_DEBUG_WARN("VIDIOC_S_CROP not implemented\n");
544 return -EINVAL;
Andy Walls3b6fe582008-06-21 08:36:31 -0300545}
546
547static int cx18_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
548{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300549 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300550
Andy Walls3b6fe582008-06-21 08:36:31 -0300551 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
552 return -EINVAL;
Andy Wallsfa3e7032009-02-16 02:23:25 -0300553 CX18_DEBUG_WARN("VIDIOC_G_CROP not implemented\n");
554 return -EINVAL;
Andy Walls3b6fe582008-06-21 08:36:31 -0300555}
556
557static int cx18_enum_fmt_vid_cap(struct file *file, void *fh,
558 struct v4l2_fmtdesc *fmt)
559{
Steven Tothb7101de2011-04-06 08:32:56 -0300560 if (fmt->index > ARRAY_SIZE(formats) - 1)
Andy Walls3b6fe582008-06-21 08:36:31 -0300561 return -EINVAL;
562 *fmt = formats[fmt->index];
563 return 0;
564}
565
566static int cx18_g_input(struct file *file, void *fh, unsigned int *i)
567{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300568 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300569
Andy Walls3b6fe582008-06-21 08:36:31 -0300570 *i = cx->active_input;
571 return 0;
572}
573
574int cx18_s_input(struct file *file, void *fh, unsigned int inp)
575{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300576 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300577 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300578
Roel Kluinde81c3c2009-07-02 16:09:25 -0300579 if (inp >= cx->nof_inputs)
Andy Walls3b6fe582008-06-21 08:36:31 -0300580 return -EINVAL;
581
582 if (inp == cx->active_input) {
583 CX18_DEBUG_INFO("Input unchanged\n");
584 return 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300585 }
586
Andy Walls3b6fe582008-06-21 08:36:31 -0300587 CX18_DEBUG_INFO("Changing input from %d to %d\n",
588 cx->active_input, inp);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300589
Andy Walls3b6fe582008-06-21 08:36:31 -0300590 cx->active_input = inp;
591 /* Set the audio input to whatever is appropriate for the input type. */
592 cx->audio_input = cx->card->video_inputs[inp].audio_index;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300593
Andy Walls3b6fe582008-06-21 08:36:31 -0300594 /* prevent others from messing with the streams until
595 we're finished changing inputs. */
596 cx18_mute(cx);
597 cx18_video_set_io(cx);
598 cx18_audio_set_io(cx);
599 cx18_unmute(cx);
600 return 0;
601}
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300602
Andy Walls3b6fe582008-06-21 08:36:31 -0300603static int cx18_g_frequency(struct file *file, void *fh,
604 struct v4l2_frequency *vf)
605{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300606 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300607
Andy Walls3b6fe582008-06-21 08:36:31 -0300608 if (vf->tuner != 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300609 return -EINVAL;
Andy Walls3b6fe582008-06-21 08:36:31 -0300610
Andy Wallsff2a2002009-02-20 23:52:13 -0300611 cx18_call_all(cx, tuner, g_frequency, vf);
Andy Walls3b6fe582008-06-21 08:36:31 -0300612 return 0;
613}
614
615int cx18_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
616{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300617 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300618 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300619
Andy Walls3b6fe582008-06-21 08:36:31 -0300620 if (vf->tuner != 0)
621 return -EINVAL;
622
623 cx18_mute(cx);
624 CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
Andy Wallsff2a2002009-02-20 23:52:13 -0300625 cx18_call_all(cx, tuner, s_frequency, vf);
Andy Walls3b6fe582008-06-21 08:36:31 -0300626 cx18_unmute(cx);
627 return 0;
628}
629
630static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
631{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300632 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300633
Andy Walls3b6fe582008-06-21 08:36:31 -0300634 *std = cx->std;
635 return 0;
636}
637
638int cx18_s_std(struct file *file, void *fh, v4l2_std_id *std)
639{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300640 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300641 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300642
Andy Walls3b6fe582008-06-21 08:36:31 -0300643 if ((*std & V4L2_STD_ALL) == 0)
644 return -EINVAL;
645
646 if (*std == cx->std)
647 return 0;
648
649 if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
650 atomic_read(&cx->ana_capturing) > 0) {
651 /* Switching standard would turn off the radio or mess
652 with already running streams, prevent that by
653 returning EBUSY. */
654 return -EBUSY;
655 }
656
657 cx->std = *std;
658 cx->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
Hans Verkuila75b9be2010-12-31 10:22:52 -0300659 cx->is_50hz = !cx->is_60hz;
660 cx2341x_handler_set_50hz(&cx->cxhdl, cx->is_50hz);
661 cx->cxhdl.width = 720;
662 cx->cxhdl.height = cx->is_50hz ? 576 : 480;
Andy Walls3b6fe582008-06-21 08:36:31 -0300663 cx->vbi.count = cx->is_50hz ? 18 : 12;
664 cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
665 cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
Andy Walls3b6fe582008-06-21 08:36:31 -0300666 CX18_DEBUG_INFO("Switching standard to %llx.\n",
667 (unsigned long long) cx->std);
668
669 /* Tuner */
Hans Verkuilf41737e2009-04-01 03:52:39 -0300670 cx18_call_all(cx, core, s_std, cx->std);
Andy Walls3b6fe582008-06-21 08:36:31 -0300671 return 0;
672}
673
674static int cx18_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
675{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300676 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300677 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300678
Andy Walls3b6fe582008-06-21 08:36:31 -0300679 if (vt->index != 0)
680 return -EINVAL;
681
Andy Wallsff2a2002009-02-20 23:52:13 -0300682 cx18_call_all(cx, tuner, s_tuner, vt);
Andy Walls3b6fe582008-06-21 08:36:31 -0300683 return 0;
684}
685
686static int cx18_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
687{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300688 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300689
Andy Walls3b6fe582008-06-21 08:36:31 -0300690 if (vt->index != 0)
691 return -EINVAL;
692
Andy Wallsff2a2002009-02-20 23:52:13 -0300693 cx18_call_all(cx, tuner, g_tuner, vt);
Andy Walls3b6fe582008-06-21 08:36:31 -0300694
695 if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
696 strlcpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
697 vt->type = V4L2_TUNER_RADIO;
698 } else {
699 strlcpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
700 vt->type = V4L2_TUNER_ANALOG_TV;
701 }
702
703 return 0;
704}
705
706static int cx18_g_sliced_vbi_cap(struct file *file, void *fh,
707 struct v4l2_sliced_vbi_cap *cap)
708{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300709 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls302df972009-01-31 00:33:02 -0300710 int set = cx->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
711 int f, l;
712
Andy Wallsc1994082009-02-05 22:37:49 -0300713 if (cap->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
714 return -EINVAL;
715
716 cap->service_set = 0;
717 for (f = 0; f < 2; f++) {
718 for (l = 0; l < 24; l++) {
719 if (valid_service_line(f, l, cx->is_50hz)) {
720 /*
721 * We can find all v4l2 supported vbi services
722 * for the standard, on a valid line for the std
723 */
724 cap->service_lines[f][l] = set;
725 cap->service_set |= set;
726 } else
727 cap->service_lines[f][l] = 0;
Andy Walls302df972009-01-31 00:33:02 -0300728 }
Andy Walls302df972009-01-31 00:33:02 -0300729 }
Andy Wallsc1994082009-02-05 22:37:49 -0300730 for (f = 0; f < 3; f++)
731 cap->reserved[f] = 0;
732 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300733}
734
Andy Walls82acdc82009-12-31 22:09:51 -0300735static int _cx18_process_idx_data(struct cx18_buffer *buf,
736 struct v4l2_enc_idx *idx)
737{
738 int consumed, remaining;
739 struct v4l2_enc_idx_entry *e_idx;
740 struct cx18_enc_idx_entry *e_buf;
741
742 /* Frame type lookup: 1=I, 2=P, 4=B */
743 const int mapping[8] = {
744 -1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P,
745 -1, V4L2_ENC_IDX_FRAME_B, -1, -1, -1
746 };
747
748 /*
749 * Assumption here is that a buf holds an integral number of
750 * struct cx18_enc_idx_entry objects and is properly aligned.
751 * This is enforced by the module options on IDX buffer sizes.
752 */
753 remaining = buf->bytesused - buf->readpos;
754 consumed = 0;
755 e_idx = &idx->entry[idx->entries];
756 e_buf = (struct cx18_enc_idx_entry *) &buf->buf[buf->readpos];
757
758 while (remaining >= sizeof(struct cx18_enc_idx_entry) &&
759 idx->entries < V4L2_ENC_IDX_ENTRIES) {
760
761 e_idx->offset = (((u64) le32_to_cpu(e_buf->offset_high)) << 32)
762 | le32_to_cpu(e_buf->offset_low);
763
764 e_idx->pts = (((u64) (le32_to_cpu(e_buf->pts_high) & 1)) << 32)
765 | le32_to_cpu(e_buf->pts_low);
766
767 e_idx->length = le32_to_cpu(e_buf->length);
768
769 e_idx->flags = mapping[le32_to_cpu(e_buf->flags) & 0x7];
770
771 e_idx->reserved[0] = 0;
772 e_idx->reserved[1] = 0;
773
774 idx->entries++;
775 e_idx = &idx->entry[idx->entries];
776 e_buf++;
777
778 remaining -= sizeof(struct cx18_enc_idx_entry);
779 consumed += sizeof(struct cx18_enc_idx_entry);
780 }
781
782 /* Swallow any partial entries at the end, if there are any */
783 if (remaining > 0 && remaining < sizeof(struct cx18_enc_idx_entry))
784 consumed += remaining;
785
786 buf->readpos += consumed;
787 return consumed;
788}
789
790static int cx18_process_idx_data(struct cx18_stream *s, struct cx18_mdl *mdl,
791 struct v4l2_enc_idx *idx)
792{
793 if (s->type != CX18_ENC_STREAM_TYPE_IDX)
794 return -EINVAL;
795
796 if (mdl->curr_buf == NULL)
797 mdl->curr_buf = list_first_entry(&mdl->buf_list,
798 struct cx18_buffer, list);
799
800 if (list_entry_is_past_end(mdl->curr_buf, &mdl->buf_list, list)) {
801 /*
802 * For some reason we've exhausted the buffers, but the MDL
803 * object still said some data was unread.
804 * Fix that and bail out.
805 */
806 mdl->readpos = mdl->bytesused;
807 return 0;
808 }
809
810 list_for_each_entry_from(mdl->curr_buf, &mdl->buf_list, list) {
811
812 /* Skip any empty buffers in the MDL */
813 if (mdl->curr_buf->readpos >= mdl->curr_buf->bytesused)
814 continue;
815
816 mdl->readpos += _cx18_process_idx_data(mdl->curr_buf, idx);
817
818 /* exit when MDL drained or request satisfied */
819 if (idx->entries >= V4L2_ENC_IDX_ENTRIES ||
820 mdl->curr_buf->readpos < mdl->curr_buf->bytesused ||
821 mdl->readpos >= mdl->bytesused)
822 break;
823 }
824 return 0;
825}
826
Andy Walls3b6fe582008-06-21 08:36:31 -0300827static int cx18_g_enc_index(struct file *file, void *fh,
828 struct v4l2_enc_idx *idx)
829{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300830 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls82acdc82009-12-31 22:09:51 -0300831 struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
832 s32 tmp;
833 struct cx18_mdl *mdl;
834
835 if (!cx18_stream_enabled(s)) /* Module options inhibited IDX stream */
836 return -EINVAL;
837
838 /* Compute the best case number of entries we can buffer */
839 tmp = s->buffers -
840 s->bufs_per_mdl * CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN;
841 if (tmp <= 0)
842 tmp = 1;
843 tmp = tmp * s->buf_size / sizeof(struct cx18_enc_idx_entry);
844
845 /* Fill out the header of the return structure */
846 idx->entries = 0;
847 idx->entries_cap = tmp;
848 memset(idx->reserved, 0, sizeof(idx->reserved));
849
850 /* Pull IDX MDLs and buffers from q_full and populate the entries */
851 do {
852 mdl = cx18_dequeue(s, &s->q_full);
853 if (mdl == NULL) /* No more IDX data right now */
854 break;
855
856 /* Extract the Index entry data from the MDL and buffers */
857 cx18_process_idx_data(s, mdl, idx);
858 if (mdl->readpos < mdl->bytesused) {
859 /* We finished with data remaining, push the MDL back */
860 cx18_push(s, mdl, &s->q_full);
861 break;
862 }
863
864 /* We drained this MDL, schedule it to go to the firmware */
865 cx18_enqueue(s, mdl, &s->q_free);
866
867 } while (idx->entries < V4L2_ENC_IDX_ENTRIES);
868
869 /* Tell the work handler to send free IDX MDLs to the firmware */
870 cx18_stream_load_fw_queue(s);
871 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300872}
873
Steven Tothb7101de2011-04-06 08:32:56 -0300874static struct videobuf_queue *cx18_vb_queue(struct cx18_open_id *id)
875{
876 struct videobuf_queue *q = NULL;
877
878 switch (id->vb_type) {
879 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
880 q = &id->vbuf_q;
881 break;
882 case V4L2_BUF_TYPE_VBI_CAPTURE:
883 break;
884 default:
885 break;
886 }
887 return q;
888}
889
890static int cx18_streamon(struct file *file, void *priv,
891 enum v4l2_buf_type type)
892{
893 struct cx18_open_id *id = file->private_data;
894 struct cx18 *cx = id->cx;
895 struct cx18_stream *s = &cx->streams[id->type];
896
897 /* Start the hardware only if we're the video device */
898 if ((id->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
899 (id->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
900 return -EINVAL;
901
902 if (id->type != CX18_ENC_STREAM_TYPE_YUV)
903 return -EINVAL;
904
905 /* Establish a buffer timeout */
906 mod_timer(&s->vb_timeout, jiffies + (HZ * 2));
907
908 return videobuf_streamon(cx18_vb_queue(id));
909}
910
911static int cx18_streamoff(struct file *file, void *priv,
912 enum v4l2_buf_type type)
913{
914 struct cx18_open_id *id = file->private_data;
915
916 /* Start the hardware only if we're the video device */
917 if ((id->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
918 (id->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
919 return -EINVAL;
920
921 if (id->type != CX18_ENC_STREAM_TYPE_YUV)
922 return -EINVAL;
923
924 return videobuf_streamoff(cx18_vb_queue(id));
925}
926
927static int cx18_reqbufs(struct file *file, void *priv,
928 struct v4l2_requestbuffers *rb)
929{
930 struct cx18_open_id *id = file->private_data;
931
932 if ((id->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
933 (id->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
934 return -EINVAL;
935
936 return videobuf_reqbufs(cx18_vb_queue(id), rb);
937}
938
939static int cx18_querybuf(struct file *file, void *priv,
940 struct v4l2_buffer *b)
941{
942 struct cx18_open_id *id = file->private_data;
943
944 if ((id->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
945 (id->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
946 return -EINVAL;
947
948 return videobuf_querybuf(cx18_vb_queue(id), b);
949}
950
951static int cx18_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
952{
953 struct cx18_open_id *id = file->private_data;
954
955 if ((id->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
956 (id->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
957 return -EINVAL;
958
959 return videobuf_qbuf(cx18_vb_queue(id), b);
960}
961
962static int cx18_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
963{
964 struct cx18_open_id *id = file->private_data;
965 if ((id->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
966 (id->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
967 return -EINVAL;
968
969 return videobuf_dqbuf(cx18_vb_queue(id), b, file->f_flags & O_NONBLOCK);
970}
971
Andy Walls3b6fe582008-06-21 08:36:31 -0300972static int cx18_encoder_cmd(struct file *file, void *fh,
973 struct v4l2_encoder_cmd *enc)
974{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300975 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300976 struct cx18 *cx = id->cx;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300977 u32 h;
Andy Walls3b6fe582008-06-21 08:36:31 -0300978
Andy Walls3b6fe582008-06-21 08:36:31 -0300979 switch (enc->cmd) {
980 case V4L2_ENC_CMD_START:
981 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
982 enc->flags = 0;
983 return cx18_start_capture(id);
984
985 case V4L2_ENC_CMD_STOP:
986 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
987 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
988 cx18_stop_capture(id,
989 enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
990 break;
991
992 case V4L2_ENC_CMD_PAUSE:
993 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
994 enc->flags = 0;
995 if (!atomic_read(&cx->ana_capturing))
996 return -EPERM;
997 if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
998 return 0;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300999 h = cx18_find_handle(cx);
1000 if (h == CX18_INVALID_TASK_HANDLE) {
1001 CX18_ERR("Can't find valid task handle for "
1002 "V4L2_ENC_CMD_PAUSE\n");
1003 return -EBADFD;
1004 }
Andy Walls3b6fe582008-06-21 08:36:31 -03001005 cx18_mute(cx);
Andy Wallsd3c5e702008-08-23 16:42:29 -03001006 cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, h);
Andy Walls3b6fe582008-06-21 08:36:31 -03001007 break;
1008
1009 case V4L2_ENC_CMD_RESUME:
1010 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1011 enc->flags = 0;
1012 if (!atomic_read(&cx->ana_capturing))
1013 return -EPERM;
1014 if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
1015 return 0;
Andy Wallsd3c5e702008-08-23 16:42:29 -03001016 h = cx18_find_handle(cx);
1017 if (h == CX18_INVALID_TASK_HANDLE) {
1018 CX18_ERR("Can't find valid task handle for "
1019 "V4L2_ENC_CMD_RESUME\n");
1020 return -EBADFD;
1021 }
1022 cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, h);
Andy Walls3b6fe582008-06-21 08:36:31 -03001023 cx18_unmute(cx);
1024 break;
1025
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001026 default:
Andy Walls3b6fe582008-06-21 08:36:31 -03001027 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1028 return -EINVAL;
1029 }
1030 return 0;
1031}
1032
1033static int cx18_try_encoder_cmd(struct file *file, void *fh,
1034 struct v4l2_encoder_cmd *enc)
1035{
Hans Verkuil0b5f2652011-03-12 06:35:33 -03001036 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -03001037
Andy Walls3b6fe582008-06-21 08:36:31 -03001038 switch (enc->cmd) {
1039 case V4L2_ENC_CMD_START:
1040 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1041 enc->flags = 0;
1042 break;
1043
1044 case V4L2_ENC_CMD_STOP:
1045 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1046 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1047 break;
1048
1049 case V4L2_ENC_CMD_PAUSE:
1050 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1051 enc->flags = 0;
1052 break;
1053
1054 case V4L2_ENC_CMD_RESUME:
1055 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1056 enc->flags = 0;
1057 break;
1058
1059 default:
1060 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1061 return -EINVAL;
1062 }
1063 return 0;
1064}
1065
1066static int cx18_log_status(struct file *file, void *fh)
1067{
Hans Verkuil0b5f2652011-03-12 06:35:33 -03001068 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -03001069 struct v4l2_input vidin;
1070 struct v4l2_audio audin;
1071 int i;
1072
Andy Wallse0f28b62009-01-10 14:13:46 -03001073 CX18_INFO("================= START STATUS CARD #%d "
Andy Walls5811cf92009-02-14 17:08:37 -03001074 "=================\n", cx->instance);
Andy Wallse0f28b62009-01-10 14:13:46 -03001075 CX18_INFO("Version: %s Card: %s\n", CX18_VERSION, cx->card_name);
Andy Walls3b6fe582008-06-21 08:36:31 -03001076 if (cx->hw_flags & CX18_HW_TVEEPROM) {
1077 struct tveeprom tv;
1078
1079 cx18_read_eeprom(cx, &tv);
1080 }
Andy Wallsff2a2002009-02-20 23:52:13 -03001081 cx18_call_all(cx, core, log_status);
Andy Walls3b6fe582008-06-21 08:36:31 -03001082 cx18_get_input(cx, cx->active_input, &vidin);
1083 cx18_get_audio_input(cx, cx->audio_input, &audin);
1084 CX18_INFO("Video Input: %s\n", vidin.name);
1085 CX18_INFO("Audio Input: %s\n", audin.name);
Andy Walls8abdd002008-07-13 19:05:25 -03001086 mutex_lock(&cx->gpio_lock);
Hans Verkuil3d66c402008-06-21 11:19:34 -03001087 CX18_INFO("GPIO: direction 0x%08x, value 0x%08x\n",
1088 cx->gpio_dir, cx->gpio_val);
Andy Walls8abdd002008-07-13 19:05:25 -03001089 mutex_unlock(&cx->gpio_lock);
Andy Walls3b6fe582008-06-21 08:36:31 -03001090 CX18_INFO("Tuner: %s\n",
1091 test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ? "Radio" : "TV");
Hans Verkuila75b9be2010-12-31 10:22:52 -03001092 v4l2_ctrl_handler_log_status(&cx->cxhdl.hdl, cx->v4l2_dev.name);
Andy Walls3b6fe582008-06-21 08:36:31 -03001093 CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
1094 for (i = 0; i < CX18_MAX_STREAMS; i++) {
1095 struct cx18_stream *s = &cx->streams[i];
1096
Andy Walls3d059132009-01-10 21:54:39 -03001097 if (s->video_dev == NULL || s->buffers == 0)
Andy Walls3b6fe582008-06-21 08:36:31 -03001098 continue;
1099 CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
1100 s->name, s->s_flags,
Andy Walls52fcb3e2009-11-08 23:45:24 -03001101 atomic_read(&s->q_full.depth) * s->bufs_per_mdl * 100
1102 / s->buffers,
Andy Walls3b6fe582008-06-21 08:36:31 -03001103 (s->buffers * s->buf_size) / 1024, s->buffers);
1104 }
1105 CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
1106 (long long)cx->mpg_data_received,
1107 (long long)cx->vbi_data_inserted);
Andy Walls5811cf92009-02-14 17:08:37 -03001108 CX18_INFO("================== END STATUS CARD #%d "
1109 "==================\n", cx->instance);
Andy Walls3b6fe582008-06-21 08:36:31 -03001110 return 0;
1111}
1112
Hans Verkuil99cd47bc2011-03-11 19:00:56 -03001113static long cx18_default(struct file *file, void *fh, bool valid_prio,
1114 int cmd, void *arg)
Andy Walls3b6fe582008-06-21 08:36:31 -03001115{
Hans Verkuil0b5f2652011-03-12 06:35:33 -03001116 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -03001117
1118 switch (cmd) {
Andy Walls02fa2722008-07-13 19:30:15 -03001119 case VIDIOC_INT_RESET: {
1120 u32 val = *(u32 *)arg;
1121
1122 if ((val == 0) || (val & 0x01))
Andy Wallseefe1012009-02-21 18:42:49 -03001123 cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL, core, reset,
1124 (u32) CX18_GPIO_RESET_Z8F0811);
Andy Walls02fa2722008-07-13 19:30:15 -03001125 break;
1126 }
1127
Andy Walls3b6fe582008-06-21 08:36:31 -03001128 default:
Andy Walls3b6fe582008-06-21 08:36:31 -03001129 return -EINVAL;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001130 }
1131 return 0;
1132}
1133
Hans Verkuil069b7472008-12-30 07:04:34 -03001134long cx18_v4l2_ioctl(struct file *filp, unsigned int cmd,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001135 unsigned long arg)
1136{
Hans Verkuil37f89f92008-06-22 11:57:31 -03001137 struct video_device *vfd = video_devdata(filp);
Hans Verkuil0b5f2652011-03-12 06:35:33 -03001138 struct cx18_open_id *id = file2id(filp);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001139 struct cx18 *cx = id->cx;
Hans Verkuil069b7472008-12-30 07:04:34 -03001140 long res;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001141
1142 mutex_lock(&cx->serialize_lock);
Hans Verkuil37f89f92008-06-22 11:57:31 -03001143
1144 if (cx18_debug & CX18_DBGFLG_IOCTL)
1145 vfd->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
Hans Verkuilbec43662008-12-30 06:58:20 -03001146 res = video_ioctl2(filp, cmd, arg);
Hans Verkuil37f89f92008-06-22 11:57:31 -03001147 vfd->debug = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001148 mutex_unlock(&cx->serialize_lock);
1149 return res;
1150}
Andy Walls3b6fe582008-06-21 08:36:31 -03001151
Hans Verkuila3998102008-07-21 02:57:38 -03001152static const struct v4l2_ioctl_ops cx18_ioctl_ops = {
1153 .vidioc_querycap = cx18_querycap,
Hans Verkuila3998102008-07-21 02:57:38 -03001154 .vidioc_s_audio = cx18_s_audio,
1155 .vidioc_g_audio = cx18_g_audio,
1156 .vidioc_enumaudio = cx18_enumaudio,
1157 .vidioc_enum_input = cx18_enum_input,
1158 .vidioc_cropcap = cx18_cropcap,
1159 .vidioc_s_crop = cx18_s_crop,
1160 .vidioc_g_crop = cx18_g_crop,
1161 .vidioc_g_input = cx18_g_input,
1162 .vidioc_s_input = cx18_s_input,
1163 .vidioc_g_frequency = cx18_g_frequency,
1164 .vidioc_s_frequency = cx18_s_frequency,
1165 .vidioc_s_tuner = cx18_s_tuner,
1166 .vidioc_g_tuner = cx18_g_tuner,
1167 .vidioc_g_enc_index = cx18_g_enc_index,
1168 .vidioc_g_std = cx18_g_std,
1169 .vidioc_s_std = cx18_s_std,
1170 .vidioc_log_status = cx18_log_status,
1171 .vidioc_enum_fmt_vid_cap = cx18_enum_fmt_vid_cap,
1172 .vidioc_encoder_cmd = cx18_encoder_cmd,
1173 .vidioc_try_encoder_cmd = cx18_try_encoder_cmd,
1174 .vidioc_g_fmt_vid_cap = cx18_g_fmt_vid_cap,
1175 .vidioc_g_fmt_vbi_cap = cx18_g_fmt_vbi_cap,
1176 .vidioc_g_fmt_sliced_vbi_cap = cx18_g_fmt_sliced_vbi_cap,
1177 .vidioc_s_fmt_vid_cap = cx18_s_fmt_vid_cap,
1178 .vidioc_s_fmt_vbi_cap = cx18_s_fmt_vbi_cap,
1179 .vidioc_s_fmt_sliced_vbi_cap = cx18_s_fmt_sliced_vbi_cap,
1180 .vidioc_try_fmt_vid_cap = cx18_try_fmt_vid_cap,
1181 .vidioc_try_fmt_vbi_cap = cx18_try_fmt_vbi_cap,
1182 .vidioc_try_fmt_sliced_vbi_cap = cx18_try_fmt_sliced_vbi_cap,
1183 .vidioc_g_sliced_vbi_cap = cx18_g_sliced_vbi_cap,
1184 .vidioc_g_chip_ident = cx18_g_chip_ident,
1185#ifdef CONFIG_VIDEO_ADV_DEBUG
1186 .vidioc_g_register = cx18_g_register,
1187 .vidioc_s_register = cx18_s_register,
1188#endif
1189 .vidioc_default = cx18_default,
Steven Tothb7101de2011-04-06 08:32:56 -03001190 .vidioc_streamon = cx18_streamon,
1191 .vidioc_streamoff = cx18_streamoff,
1192 .vidioc_reqbufs = cx18_reqbufs,
1193 .vidioc_querybuf = cx18_querybuf,
1194 .vidioc_qbuf = cx18_qbuf,
1195 .vidioc_dqbuf = cx18_dqbuf,
Hans Verkuila3998102008-07-21 02:57:38 -03001196};
1197
Andy Walls3b6fe582008-06-21 08:36:31 -03001198void cx18_set_funcs(struct video_device *vdev)
1199{
Hans Verkuila3998102008-07-21 02:57:38 -03001200 vdev->ioctl_ops = &cx18_ioctl_ops;
Andy Walls3b6fe582008-06-21 08:36:31 -03001201}