blob: 7dbd5a9451ed71522528c17c19f13f797bb53448 [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
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -030044u16 cx18_service2vbi(int type)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030045{
46 switch (type) {
47 case V4L2_SLICED_TELETEXT_B:
48 return CX18_SLICED_TYPE_TELETEXT_B;
49 case V4L2_SLICED_CAPTION_525:
50 return CX18_SLICED_TYPE_CAPTION_525;
51 case V4L2_SLICED_WSS_625:
52 return CX18_SLICED_TYPE_WSS_625;
53 case V4L2_SLICED_VPS:
54 return CX18_SLICED_TYPE_VPS;
55 default:
56 return 0;
57 }
58}
59
Andy Wallsc1994082009-02-05 22:37:49 -030060/* Check if VBI services are allowed on the (field, line) for the video std */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030061static int valid_service_line(int field, int line, int is_pal)
62{
Andy Wallsc1994082009-02-05 22:37:49 -030063 return (is_pal && line >= 6 &&
64 ((field == 0 && line <= 23) || (field == 1 && line <= 22))) ||
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030065 (!is_pal && line >= 10 && line < 22);
66}
67
Andy Wallsc1994082009-02-05 22:37:49 -030068/*
69 * For a (field, line, std) and inbound potential set of services for that line,
70 * return the first valid service of those passed in the incoming set for that
71 * line in priority order:
72 * CC, VPS, or WSS over TELETEXT for well known lines
73 * TELETEXT, before VPS, before CC, before WSS, for other lines
74 */
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030075static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
76{
77 u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
78 int i;
79
80 set = set & valid_set;
81 if (set == 0 || !valid_service_line(field, line, is_pal))
82 return 0;
83 if (!is_pal) {
84 if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
85 return V4L2_SLICED_CAPTION_525;
86 } else {
87 if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
88 return V4L2_SLICED_VPS;
89 if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
90 return V4L2_SLICED_WSS_625;
91 if (line == 23)
92 return 0;
93 }
94 for (i = 0; i < 32; i++) {
95 if ((1 << i) & set)
96 return 1 << i;
97 }
98 return 0;
99}
100
Andy Wallsc1994082009-02-05 22:37:49 -0300101/*
102 * Expand the service_set of *fmt into valid service_lines for the std,
103 * and clear the passed in fmt->service_set
104 */
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -0300105void cx18_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300106{
107 u16 set = fmt->service_set;
108 int f, l;
109
110 fmt->service_set = 0;
111 for (f = 0; f < 2; f++) {
112 for (l = 0; l < 24; l++)
113 fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
114 }
115}
116
Andy Wallsc1994082009-02-05 22:37:49 -0300117/*
118 * Sanitize the service_lines in *fmt per the video std, and return 1
119 * if any service_line is left as valid after santization
120 */
Andy Walls302df972009-01-31 00:33:02 -0300121static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
122{
123 int f, l;
124 u16 set = 0;
125
126 for (f = 0; f < 2; f++) {
127 for (l = 0; l < 24; l++) {
128 fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
129 set |= fmt->service_lines[f][l];
130 }
131 }
132 return set != 0;
133}
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300134
Andy Wallsc1994082009-02-05 22:37:49 -0300135/* Compute the service_set from the assumed valid service_lines of *fmt */
Mauro Carvalho Chehabaed6abd2008-04-29 21:38:51 -0300136u16 cx18_get_service_set(struct v4l2_sliced_vbi_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300137{
138 int f, l;
139 u16 set = 0;
140
141 for (f = 0; f < 2; f++) {
142 for (l = 0; l < 24; l++)
143 set |= fmt->service_lines[f][l];
144 }
145 return set;
146}
147
Andy Walls3b6fe582008-06-21 08:36:31 -0300148static int cx18_g_fmt_vid_cap(struct file *file, void *fh,
149 struct v4l2_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300150{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300151 struct cx18_open_id *id = fh2id(fh);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300152 struct cx18 *cx = id->cx;
Steven Tothb7101de2011-04-06 08:32:56 -0300153 struct cx18_stream *s = &cx->streams[id->type];
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300154 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300155
Hans Verkuila75b9be2010-12-31 10:22:52 -0300156 pixfmt->width = cx->cxhdl.width;
157 pixfmt->height = cx->cxhdl.height;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300158 pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
159 pixfmt->field = V4L2_FIELD_INTERLACED;
160 pixfmt->priv = 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300161 if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
Steven Tothb7101de2011-04-06 08:32:56 -0300162 pixfmt->pixelformat = s->pixelformat;
Simon Farnsworth09fc9802011-09-05 12:23:12 -0300163 pixfmt->sizeimage = s->vb_bytes_per_frame;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300164 pixfmt->bytesperline = 720;
Andy Walls3b6fe582008-06-21 08:36:31 -0300165 } else {
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300166 pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
167 pixfmt->sizeimage = 128 * 1024;
168 pixfmt->bytesperline = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300169 }
170 return 0;
171}
172
Andy Walls3b6fe582008-06-21 08:36:31 -0300173static int cx18_g_fmt_vbi_cap(struct file *file, void *fh,
174 struct v4l2_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300175{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300176 struct cx18 *cx = fh2id(fh)->cx;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300177 struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300178
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300179 vbifmt->sampling_rate = 27000000;
Andy Wallsc1994082009-02-05 22:37:49 -0300180 vbifmt->offset = 248; /* FIXME - slightly wrong for both 50 & 60 Hz */
Andy Walls302df972009-01-31 00:33:02 -0300181 vbifmt->samples_per_line = vbi_active_samples - 4;
Hans Verkuil0b5a30e2008-06-21 09:22:19 -0300182 vbifmt->sample_format = V4L2_PIX_FMT_GREY;
183 vbifmt->start[0] = cx->vbi.start[0];
184 vbifmt->start[1] = cx->vbi.start[1];
185 vbifmt->count[0] = vbifmt->count[1] = cx->vbi.count;
186 vbifmt->flags = 0;
187 vbifmt->reserved[0] = 0;
188 vbifmt->reserved[1] = 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300189 return 0;
190}
191
Andy Walls3b6fe582008-06-21 08:36:31 -0300192static int cx18_g_fmt_sliced_vbi_cap(struct file *file, void *fh,
193 struct v4l2_format *fmt)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300194{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300195 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls302df972009-01-31 00:33:02 -0300196 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
197
Andy Wallsc1994082009-02-05 22:37:49 -0300198 /* sane, V4L2 spec compliant, defaults */
Andy Walls302df972009-01-31 00:33:02 -0300199 vbifmt->reserved[0] = 0;
200 vbifmt->reserved[1] = 0;
201 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
202 memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
Andy Wallsc1994082009-02-05 22:37:49 -0300203 vbifmt->service_set = 0;
Andy Walls302df972009-01-31 00:33:02 -0300204
Andy Wallsc1994082009-02-05 22:37:49 -0300205 /*
206 * Fetch the configured service_lines and total service_set from the
207 * digitizer/slicer. Note, cx18_av_vbi() wipes the passed in
208 * fmt->fmt.sliced under valid calling conditions
209 */
Hans Verkuiladd632c2010-03-14 12:24:15 -0300210 if (v4l2_subdev_call(cx->sd_av, vbi, g_sliced_fmt, &fmt->fmt.sliced))
Andy Wallsc1994082009-02-05 22:37:49 -0300211 return -EINVAL;
212
Andy Walls302df972009-01-31 00:33:02 -0300213 vbifmt->service_set = cx18_get_service_set(vbifmt);
214 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300215}
216
217static int cx18_try_fmt_vid_cap(struct file *file, void *fh,
218 struct v4l2_format *fmt)
219{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300220 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300221 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300222 int w = fmt->fmt.pix.width;
223 int h = fmt->fmt.pix.height;
Hans Verkuila4a78712009-02-06 15:31:59 -0300224 int min_h = 2;
Andy Walls3b6fe582008-06-21 08:36:31 -0300225
Andy Walls3b6fe582008-06-21 08:36:31 -0300226 w = min(w, 720);
Hans Verkuila4a78712009-02-06 15:31:59 -0300227 w = max(w, 2);
228 if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
229 /* YUV height must be a multiple of 32 */
230 h &= ~0x1f;
231 min_h = 32;
232 }
Andy Walls3b6fe582008-06-21 08:36:31 -0300233 h = min(h, cx->is_50hz ? 576 : 480);
Hans Verkuila4a78712009-02-06 15:31:59 -0300234 h = max(h, min_h);
235
Andy Walls3b6fe582008-06-21 08:36:31 -0300236 fmt->fmt.pix.width = w;
237 fmt->fmt.pix.height = h;
238 return 0;
239}
240
241static int cx18_try_fmt_vbi_cap(struct file *file, void *fh,
242 struct v4l2_format *fmt)
243{
Andy Walls3b6fe582008-06-21 08:36:31 -0300244 return cx18_g_fmt_vbi_cap(file, fh, fmt);
245}
246
247static int cx18_try_fmt_sliced_vbi_cap(struct file *file, void *fh,
248 struct v4l2_format *fmt)
249{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300250 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls302df972009-01-31 00:33:02 -0300251 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
252
253 vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
254 vbifmt->reserved[0] = 0;
255 vbifmt->reserved[1] = 0;
256
Andy Wallsc1994082009-02-05 22:37:49 -0300257 /* If given a service set, expand it validly & clear passed in set */
Andy Walls302df972009-01-31 00:33:02 -0300258 if (vbifmt->service_set)
259 cx18_expand_service_set(vbifmt, cx->is_50hz);
Andy Wallsc1994082009-02-05 22:37:49 -0300260 /* Sanitize the service_lines, and compute the new set if any valid */
261 if (check_service_set(vbifmt, cx->is_50hz))
262 vbifmt->service_set = cx18_get_service_set(vbifmt);
Andy Walls302df972009-01-31 00:33:02 -0300263 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300264}
265
266static int cx18_s_fmt_vid_cap(struct file *file, void *fh,
267 struct v4l2_format *fmt)
268{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300269 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300270 struct cx18 *cx = id->cx;
Hans Verkuil9e36eab2010-05-08 16:37:53 -0300271 struct v4l2_mbus_framefmt mbus_fmt;
Steven Tothb7101de2011-04-06 08:32:56 -0300272 struct cx18_stream *s = &cx->streams[id->type];
Andy Walls3b6fe582008-06-21 08:36:31 -0300273 int ret;
Hans Verkuileffc3462008-09-06 08:24:37 -0300274 int w, h;
Andy Walls3b6fe582008-06-21 08:36:31 -0300275
Andy Walls3b6fe582008-06-21 08:36:31 -0300276 ret = cx18_try_fmt_vid_cap(file, fh, fmt);
277 if (ret)
278 return ret;
Hans Verkuileffc3462008-09-06 08:24:37 -0300279 w = fmt->fmt.pix.width;
280 h = fmt->fmt.pix.height;
Andy Walls3b6fe582008-06-21 08:36:31 -0300281
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300282 if (cx->cxhdl.width == w && cx->cxhdl.height == h &&
283 s->pixelformat == fmt->fmt.pix.pixelformat)
Andy Walls3b6fe582008-06-21 08:36:31 -0300284 return 0;
285
286 if (atomic_read(&cx->ana_capturing) > 0)
287 return -EBUSY;
288
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300289 s->pixelformat = fmt->fmt.pix.pixelformat;
Simon Farnsworth09fc9802011-09-05 12:23:12 -0300290 /* HM12 YUV size is (Y=(h*720) + UV=(h*(720/2)))
291 UYUV YUV size is (Y=(h*720) + UV=(h*(720))) */
292 if (s->pixelformat == V4L2_PIX_FMT_HM12)
293 s->vb_bytes_per_frame = h * 720 * 3 / 2;
294 else
295 s->vb_bytes_per_frame = h * 720 * 2;
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300296
Hans Verkuila75b9be2010-12-31 10:22:52 -0300297 mbus_fmt.width = cx->cxhdl.width = w;
298 mbus_fmt.height = cx->cxhdl.height = h;
Hans Verkuil9e36eab2010-05-08 16:37:53 -0300299 mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
300 v4l2_subdev_call(cx->sd_av, video, s_mbus_fmt, &mbus_fmt);
Andy Walls3b6fe582008-06-21 08:36:31 -0300301 return cx18_g_fmt_vid_cap(file, fh, fmt);
302}
303
304static int cx18_s_fmt_vbi_cap(struct file *file, void *fh,
305 struct v4l2_format *fmt)
306{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300307 struct cx18_open_id *id = fh2id(fh);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300308 struct cx18 *cx = id->cx;
309 int ret;
310
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300311 /*
312 * Changing the Encoder's Raw VBI parameters won't have any effect
313 * if any analog capture is ongoing
314 */
315 if (!cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
Andy Walls3b6fe582008-06-21 08:36:31 -0300316 return -EBUSY;
317
Andy Wallsc1994082009-02-05 22:37:49 -0300318 /*
319 * Set the digitizer registers for raw active VBI.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300320 * Note cx18_av_vbi_wipes out a lot of the passed in fmt under valid
Andy Wallsc1994082009-02-05 22:37:49 -0300321 * calling conditions
322 */
Hans Verkuiladd632c2010-03-14 12:24:15 -0300323 ret = v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &fmt->fmt.vbi);
Andy Wallsc1994082009-02-05 22:37:49 -0300324 if (ret)
325 return ret;
326
327 /* Store our new v4l2 (non-)sliced VBI state */
Andy Walls3b6fe582008-06-21 08:36:31 -0300328 cx->vbi.sliced_in->service_set = 0;
Andy Wallsdd073432008-12-12 16:24:04 -0300329 cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
Andy Wallsc1994082009-02-05 22:37:49 -0300330
Andy Walls3b6fe582008-06-21 08:36:31 -0300331 return cx18_g_fmt_vbi_cap(file, fh, fmt);
332}
333
334static int cx18_s_fmt_sliced_vbi_cap(struct file *file, void *fh,
335 struct v4l2_format *fmt)
336{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300337 struct cx18_open_id *id = fh2id(fh);
Andy Walls302df972009-01-31 00:33:02 -0300338 struct cx18 *cx = id->cx;
339 int ret;
340 struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
341
Andy Wallsc1994082009-02-05 22:37:49 -0300342 cx18_try_fmt_sliced_vbi_cap(file, fh, fmt);
343
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300344 /*
345 * Changing the Encoder's Raw VBI parameters won't have any effect
346 * if any analog capture is ongoing
347 */
348 if (cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
Andy Wallsc1994082009-02-05 22:37:49 -0300349 return -EBUSY;
Andy Wallsdcc0ef82009-02-06 18:33:43 -0300350
Andy Wallsc1994082009-02-05 22:37:49 -0300351 /*
352 * Set the service_lines requested in the digitizer/slicer registers.
353 * Note, cx18_av_vbi() wipes some "impossible" service lines in the
354 * passed in fmt->fmt.sliced under valid calling conditions
355 */
Hans Verkuiladd632c2010-03-14 12:24:15 -0300356 ret = v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &fmt->fmt.sliced);
Andy Walls302df972009-01-31 00:33:02 -0300357 if (ret)
358 return ret;
Andy Wallsc1994082009-02-05 22:37:49 -0300359 /* Store our current v4l2 sliced VBI settings */
Andy Walls302df972009-01-31 00:33:02 -0300360 cx->vbi.in.type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
Andy Walls302df972009-01-31 00:33:02 -0300361 memcpy(cx->vbi.sliced_in, vbifmt, sizeof(*cx->vbi.sliced_in));
362 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300363}
364
365static int cx18_g_chip_ident(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300366 struct v4l2_dbg_chip_ident *chip)
Andy Walls3b6fe582008-06-21 08:36:31 -0300367{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300368 struct cx18 *cx = fh2id(fh)->cx;
Andy Wallsff2a2002009-02-20 23:52:13 -0300369 int err = 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300370
Andy Walls3b6fe582008-06-21 08:36:31 -0300371 chip->ident = V4L2_IDENT_NONE;
372 chip->revision = 0;
Andy Wallsff2a2002009-02-20 23:52:13 -0300373 switch (chip->match.type) {
374 case V4L2_CHIP_MATCH_HOST:
375 switch (chip->match.addr) {
376 case 0:
377 chip->ident = V4L2_IDENT_CX23418;
378 chip->revision = cx18_read_reg(cx, 0xC72028);
379 break;
380 case 1:
381 /*
382 * The A/V decoder is always present, but in the rare
383 * case that the card doesn't have analog, we don't
384 * use it. We find it w/o using the cx->sd_av pointer
385 */
386 cx18_call_hw(cx, CX18_HW_418_AV,
387 core, g_chip_ident, chip);
388 break;
389 default:
390 /*
391 * Could return ident = V4L2_IDENT_UNKNOWN if we had
392 * other host chips at higher addresses, but we don't
393 */
394 err = -EINVAL; /* per V4L2 spec */
395 break;
396 }
397 break;
398 case V4L2_CHIP_MATCH_I2C_DRIVER:
399 /* If needed, returns V4L2_IDENT_AMBIGUOUS without extra work */
400 cx18_call_all(cx, core, g_chip_ident, chip);
401 break;
402 case V4L2_CHIP_MATCH_I2C_ADDR:
403 /*
404 * We could return V4L2_IDENT_UNKNOWN, but we don't do the work
405 * to look if a chip is at the address with no driver. That's a
406 * dangerous thing to do with EEPROMs anyway.
407 */
408 cx18_call_all(cx, core, g_chip_ident, chip);
409 break;
410 default:
411 err = -EINVAL;
412 break;
Andy Walls3b6fe582008-06-21 08:36:31 -0300413 }
Andy Wallsff2a2002009-02-20 23:52:13 -0300414 return err;
Andy Walls3b6fe582008-06-21 08:36:31 -0300415}
416
Hans Verkuil36ecd492008-06-25 06:00:17 -0300417#ifdef CONFIG_VIDEO_ADV_DEBUG
Andy Walls3b6fe582008-06-21 08:36:31 -0300418static int cx18_g_register(struct file *file, void *fh,
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300419 struct v4l2_dbg_register *reg)
Andy Walls3b6fe582008-06-21 08:36:31 -0300420{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300421 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300422
Hans Verkuil977ba3b2013-03-24 08:28:46 -0300423 if (v4l2_chip_match_host(&reg->match)) {
424 if (reg->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
425 return -EINVAL;
426 reg->size = 4;
427 reg->val = cx18_read_enc(cx, reg->reg);
428 return 0;
429 }
Andy Wallsff2a2002009-02-20 23:52:13 -0300430 /* FIXME - errors shouldn't be ignored */
431 cx18_call_all(cx, core, g_register, reg);
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300432 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300433}
434
435static int cx18_s_register(struct file *file, void *fh,
Hans Verkuil977ba3b2013-03-24 08:28:46 -0300436 const struct v4l2_dbg_register *reg)
Andy Walls3b6fe582008-06-21 08:36:31 -0300437{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300438 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300439
Hans Verkuil977ba3b2013-03-24 08:28:46 -0300440 if (v4l2_chip_match_host(&reg->match)) {
441 if (reg->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
442 return -EINVAL;
443 cx18_write_enc(cx, reg->val, reg->reg);
444 return 0;
445 }
Andy Wallsff2a2002009-02-20 23:52:13 -0300446 /* FIXME - errors shouldn't be ignored */
447 cx18_call_all(cx, core, s_register, reg);
Hans Verkuilaecde8b2008-12-30 07:14:19 -0300448 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300449}
Hans Verkuil36ecd492008-06-25 06:00:17 -0300450#endif
Andy Walls3b6fe582008-06-21 08:36:31 -0300451
Andy Walls3b6fe582008-06-21 08:36:31 -0300452static int cx18_querycap(struct file *file, void *fh,
453 struct v4l2_capability *vcap)
454{
Simon Farnsworth09fc9802011-09-05 12:23:12 -0300455 struct cx18_open_id *id = fh2id(fh);
456 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300457
Andy Walls3b6fe582008-06-21 08:36:31 -0300458 strlcpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
459 strlcpy(vcap->card, cx->card_name, sizeof(vcap->card));
Andy Walls3d059132009-01-10 21:54:39 -0300460 snprintf(vcap->bus_info, sizeof(vcap->bus_info),
461 "PCI:%s", pci_name(cx->pci_dev));
Andy Walls3b6fe582008-06-21 08:36:31 -0300462 vcap->capabilities = cx->v4l2_cap; /* capabilities */
Simon Farnsworth09fc9802011-09-05 12:23:12 -0300463 if (id->type == CX18_ENC_STREAM_TYPE_YUV)
464 vcap->capabilities |= V4L2_CAP_STREAMING;
Andy Walls3b6fe582008-06-21 08:36:31 -0300465 return 0;
466}
467
468static int cx18_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
469{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300470 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300471
Andy Walls3b6fe582008-06-21 08:36:31 -0300472 return cx18_get_audio_input(cx, vin->index, vin);
473}
474
475static int cx18_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
476{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300477 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300478
Andy Walls3b6fe582008-06-21 08:36:31 -0300479 vin->index = cx->audio_input;
480 return cx18_get_audio_input(cx, vin->index, vin);
481}
482
Hans Verkuil0e8025b2012-09-04 11:59:31 -0300483static int cx18_s_audio(struct file *file, void *fh, const struct v4l2_audio *vout)
Andy Walls3b6fe582008-06-21 08:36:31 -0300484{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300485 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300486
Andy Walls3b6fe582008-06-21 08:36:31 -0300487 if (vout->index >= cx->nof_audio_inputs)
488 return -EINVAL;
489 cx->audio_input = vout->index;
490 cx18_audio_set_io(cx);
491 return 0;
492}
493
494static int cx18_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
495{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300496 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300497
Andy Walls3b6fe582008-06-21 08:36:31 -0300498 /* set it to defaults from our table */
499 return cx18_get_input(cx, vin->index, vin);
500}
501
502static int cx18_cropcap(struct file *file, void *fh,
503 struct v4l2_cropcap *cropcap)
504{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300505 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300506
Andy Walls3b6fe582008-06-21 08:36:31 -0300507 if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
508 return -EINVAL;
509 cropcap->bounds.top = cropcap->bounds.left = 0;
510 cropcap->bounds.width = 720;
511 cropcap->bounds.height = cx->is_50hz ? 576 : 480;
512 cropcap->pixelaspect.numerator = cx->is_50hz ? 59 : 10;
513 cropcap->pixelaspect.denominator = cx->is_50hz ? 54 : 11;
514 cropcap->defrect = cropcap->bounds;
515 return 0;
516}
517
Hans Verkuil4f9965942012-09-05 05:10:48 -0300518static int cx18_s_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
Andy Walls3b6fe582008-06-21 08:36:31 -0300519{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300520 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300521 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300522
Andy Walls3b6fe582008-06-21 08:36:31 -0300523 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
524 return -EINVAL;
Andy Wallsfa3e7032009-02-16 02:23:25 -0300525 CX18_DEBUG_WARN("VIDIOC_S_CROP not implemented\n");
526 return -EINVAL;
Andy Walls3b6fe582008-06-21 08:36:31 -0300527}
528
529static int cx18_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
530{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300531 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300532
Andy Walls3b6fe582008-06-21 08:36:31 -0300533 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
534 return -EINVAL;
Andy Wallsfa3e7032009-02-16 02:23:25 -0300535 CX18_DEBUG_WARN("VIDIOC_G_CROP not implemented\n");
536 return -EINVAL;
Andy Walls3b6fe582008-06-21 08:36:31 -0300537}
538
539static int cx18_enum_fmt_vid_cap(struct file *file, void *fh,
540 struct v4l2_fmtdesc *fmt)
541{
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300542 static const struct v4l2_fmtdesc formats[] = {
543 { 0, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
544 "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12, { 0, 0, 0, 0 }
545 },
546 { 1, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FMT_FLAG_COMPRESSED,
547 "MPEG", V4L2_PIX_FMT_MPEG, { 0, 0, 0, 0 }
548 },
549 { 2, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
550 "UYVY 4:2:2", V4L2_PIX_FMT_UYVY, { 0, 0, 0, 0 }
551 },
552 };
553
Steven Tothb7101de2011-04-06 08:32:56 -0300554 if (fmt->index > ARRAY_SIZE(formats) - 1)
Andy Walls3b6fe582008-06-21 08:36:31 -0300555 return -EINVAL;
556 *fmt = formats[fmt->index];
557 return 0;
558}
559
560static int cx18_g_input(struct file *file, void *fh, unsigned int *i)
561{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300562 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300563
Andy Walls3b6fe582008-06-21 08:36:31 -0300564 *i = cx->active_input;
565 return 0;
566}
567
568int cx18_s_input(struct file *file, void *fh, unsigned int inp)
569{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300570 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300571 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300572
Roel Kluinde81c3c2009-07-02 16:09:25 -0300573 if (inp >= cx->nof_inputs)
Andy Walls3b6fe582008-06-21 08:36:31 -0300574 return -EINVAL;
575
576 if (inp == cx->active_input) {
577 CX18_DEBUG_INFO("Input unchanged\n");
578 return 0;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300579 }
580
Andy Walls3b6fe582008-06-21 08:36:31 -0300581 CX18_DEBUG_INFO("Changing input from %d to %d\n",
582 cx->active_input, inp);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300583
Andy Walls3b6fe582008-06-21 08:36:31 -0300584 cx->active_input = inp;
585 /* Set the audio input to whatever is appropriate for the input type. */
586 cx->audio_input = cx->card->video_inputs[inp].audio_index;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300587
Andy Walls3b6fe582008-06-21 08:36:31 -0300588 /* prevent others from messing with the streams until
589 we're finished changing inputs. */
590 cx18_mute(cx);
591 cx18_video_set_io(cx);
592 cx18_audio_set_io(cx);
593 cx18_unmute(cx);
594 return 0;
595}
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300596
Andy Walls3b6fe582008-06-21 08:36:31 -0300597static int cx18_g_frequency(struct file *file, void *fh,
598 struct v4l2_frequency *vf)
599{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300600 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300601
Andy Walls3b6fe582008-06-21 08:36:31 -0300602 if (vf->tuner != 0)
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300603 return -EINVAL;
Andy Walls3b6fe582008-06-21 08:36:31 -0300604
Andy Wallsff2a2002009-02-20 23:52:13 -0300605 cx18_call_all(cx, tuner, g_frequency, vf);
Andy Walls3b6fe582008-06-21 08:36:31 -0300606 return 0;
607}
608
Hans Verkuilb530a442013-03-19 04:09:26 -0300609int cx18_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
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 (vf->tuner != 0)
615 return -EINVAL;
616
617 cx18_mute(cx);
618 CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
Andy Wallsff2a2002009-02-20 23:52:13 -0300619 cx18_call_all(cx, tuner, s_frequency, vf);
Andy Walls3b6fe582008-06-21 08:36:31 -0300620 cx18_unmute(cx);
621 return 0;
622}
623
624static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
625{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300626 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300627
Andy Walls3b6fe582008-06-21 08:36:31 -0300628 *std = cx->std;
629 return 0;
630}
631
Hans Verkuil314527a2013-03-15 06:10:40 -0300632int cx18_s_std(struct file *file, void *fh, v4l2_std_id std)
Andy Walls3b6fe582008-06-21 08:36:31 -0300633{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300634 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300635 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300636
Hans Verkuil314527a2013-03-15 06:10:40 -0300637 if ((std & V4L2_STD_ALL) == 0)
Andy Walls3b6fe582008-06-21 08:36:31 -0300638 return -EINVAL;
639
Hans Verkuil314527a2013-03-15 06:10:40 -0300640 if (std == cx->std)
Andy Walls3b6fe582008-06-21 08:36:31 -0300641 return 0;
642
643 if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
644 atomic_read(&cx->ana_capturing) > 0) {
645 /* Switching standard would turn off the radio or mess
646 with already running streams, prevent that by
647 returning EBUSY. */
648 return -EBUSY;
649 }
650
Hans Verkuil314527a2013-03-15 06:10:40 -0300651 cx->std = std;
652 cx->is_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
Hans Verkuila75b9be2010-12-31 10:22:52 -0300653 cx->is_50hz = !cx->is_60hz;
654 cx2341x_handler_set_50hz(&cx->cxhdl, cx->is_50hz);
655 cx->cxhdl.width = 720;
656 cx->cxhdl.height = cx->is_50hz ? 576 : 480;
Andy Walls3b6fe582008-06-21 08:36:31 -0300657 cx->vbi.count = cx->is_50hz ? 18 : 12;
658 cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
659 cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
Andy Walls3b6fe582008-06-21 08:36:31 -0300660 CX18_DEBUG_INFO("Switching standard to %llx.\n",
661 (unsigned long long) cx->std);
662
663 /* Tuner */
Hans Verkuilf41737e2009-04-01 03:52:39 -0300664 cx18_call_all(cx, core, s_std, cx->std);
Andy Walls3b6fe582008-06-21 08:36:31 -0300665 return 0;
666}
667
Hans Verkuil2f73c7c2013-03-15 06:10:06 -0300668static int cx18_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
Andy Walls3b6fe582008-06-21 08:36:31 -0300669{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300670 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300671 struct cx18 *cx = id->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300672
Andy Walls3b6fe582008-06-21 08:36:31 -0300673 if (vt->index != 0)
674 return -EINVAL;
675
Andy Wallsff2a2002009-02-20 23:52:13 -0300676 cx18_call_all(cx, tuner, s_tuner, vt);
Andy Walls3b6fe582008-06-21 08:36:31 -0300677 return 0;
678}
679
680static int cx18_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
681{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300682 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -0300683
Andy Walls3b6fe582008-06-21 08:36:31 -0300684 if (vt->index != 0)
685 return -EINVAL;
686
Andy Wallsff2a2002009-02-20 23:52:13 -0300687 cx18_call_all(cx, tuner, g_tuner, vt);
Andy Walls3b6fe582008-06-21 08:36:31 -0300688
Hans Verkuild118e292011-06-25 10:28:21 -0300689 if (vt->type == V4L2_TUNER_RADIO)
Andy Walls3b6fe582008-06-21 08:36:31 -0300690 strlcpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
Hans Verkuild118e292011-06-25 10:28:21 -0300691 else
Andy Walls3b6fe582008-06-21 08:36:31 -0300692 strlcpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
Andy Walls3b6fe582008-06-21 08:36:31 -0300693 return 0;
694}
695
696static int cx18_g_sliced_vbi_cap(struct file *file, void *fh,
697 struct v4l2_sliced_vbi_cap *cap)
698{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300699 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls302df972009-01-31 00:33:02 -0300700 int set = cx->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
701 int f, l;
702
Andy Wallsc1994082009-02-05 22:37:49 -0300703 if (cap->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
704 return -EINVAL;
705
706 cap->service_set = 0;
707 for (f = 0; f < 2; f++) {
708 for (l = 0; l < 24; l++) {
709 if (valid_service_line(f, l, cx->is_50hz)) {
710 /*
711 * We can find all v4l2 supported vbi services
712 * for the standard, on a valid line for the std
713 */
714 cap->service_lines[f][l] = set;
715 cap->service_set |= set;
716 } else
717 cap->service_lines[f][l] = 0;
Andy Walls302df972009-01-31 00:33:02 -0300718 }
Andy Walls302df972009-01-31 00:33:02 -0300719 }
Andy Wallsc1994082009-02-05 22:37:49 -0300720 for (f = 0; f < 3; f++)
721 cap->reserved[f] = 0;
722 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300723}
724
Andy Walls82acdc82009-12-31 22:09:51 -0300725static int _cx18_process_idx_data(struct cx18_buffer *buf,
726 struct v4l2_enc_idx *idx)
727{
728 int consumed, remaining;
729 struct v4l2_enc_idx_entry *e_idx;
730 struct cx18_enc_idx_entry *e_buf;
731
732 /* Frame type lookup: 1=I, 2=P, 4=B */
733 const int mapping[8] = {
734 -1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P,
735 -1, V4L2_ENC_IDX_FRAME_B, -1, -1, -1
736 };
737
738 /*
739 * Assumption here is that a buf holds an integral number of
740 * struct cx18_enc_idx_entry objects and is properly aligned.
741 * This is enforced by the module options on IDX buffer sizes.
742 */
743 remaining = buf->bytesused - buf->readpos;
744 consumed = 0;
745 e_idx = &idx->entry[idx->entries];
746 e_buf = (struct cx18_enc_idx_entry *) &buf->buf[buf->readpos];
747
748 while (remaining >= sizeof(struct cx18_enc_idx_entry) &&
749 idx->entries < V4L2_ENC_IDX_ENTRIES) {
750
751 e_idx->offset = (((u64) le32_to_cpu(e_buf->offset_high)) << 32)
752 | le32_to_cpu(e_buf->offset_low);
753
754 e_idx->pts = (((u64) (le32_to_cpu(e_buf->pts_high) & 1)) << 32)
755 | le32_to_cpu(e_buf->pts_low);
756
757 e_idx->length = le32_to_cpu(e_buf->length);
758
759 e_idx->flags = mapping[le32_to_cpu(e_buf->flags) & 0x7];
760
761 e_idx->reserved[0] = 0;
762 e_idx->reserved[1] = 0;
763
764 idx->entries++;
765 e_idx = &idx->entry[idx->entries];
766 e_buf++;
767
768 remaining -= sizeof(struct cx18_enc_idx_entry);
769 consumed += sizeof(struct cx18_enc_idx_entry);
770 }
771
772 /* Swallow any partial entries at the end, if there are any */
773 if (remaining > 0 && remaining < sizeof(struct cx18_enc_idx_entry))
774 consumed += remaining;
775
776 buf->readpos += consumed;
777 return consumed;
778}
779
780static int cx18_process_idx_data(struct cx18_stream *s, struct cx18_mdl *mdl,
781 struct v4l2_enc_idx *idx)
782{
783 if (s->type != CX18_ENC_STREAM_TYPE_IDX)
784 return -EINVAL;
785
786 if (mdl->curr_buf == NULL)
787 mdl->curr_buf = list_first_entry(&mdl->buf_list,
788 struct cx18_buffer, list);
789
790 if (list_entry_is_past_end(mdl->curr_buf, &mdl->buf_list, list)) {
791 /*
792 * For some reason we've exhausted the buffers, but the MDL
793 * object still said some data was unread.
794 * Fix that and bail out.
795 */
796 mdl->readpos = mdl->bytesused;
797 return 0;
798 }
799
800 list_for_each_entry_from(mdl->curr_buf, &mdl->buf_list, list) {
801
802 /* Skip any empty buffers in the MDL */
803 if (mdl->curr_buf->readpos >= mdl->curr_buf->bytesused)
804 continue;
805
806 mdl->readpos += _cx18_process_idx_data(mdl->curr_buf, idx);
807
808 /* exit when MDL drained or request satisfied */
809 if (idx->entries >= V4L2_ENC_IDX_ENTRIES ||
810 mdl->curr_buf->readpos < mdl->curr_buf->bytesused ||
811 mdl->readpos >= mdl->bytesused)
812 break;
813 }
814 return 0;
815}
816
Andy Walls3b6fe582008-06-21 08:36:31 -0300817static int cx18_g_enc_index(struct file *file, void *fh,
818 struct v4l2_enc_idx *idx)
819{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300820 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls82acdc82009-12-31 22:09:51 -0300821 struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
822 s32 tmp;
823 struct cx18_mdl *mdl;
824
825 if (!cx18_stream_enabled(s)) /* Module options inhibited IDX stream */
826 return -EINVAL;
827
828 /* Compute the best case number of entries we can buffer */
829 tmp = s->buffers -
830 s->bufs_per_mdl * CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN;
831 if (tmp <= 0)
832 tmp = 1;
833 tmp = tmp * s->buf_size / sizeof(struct cx18_enc_idx_entry);
834
835 /* Fill out the header of the return structure */
836 idx->entries = 0;
837 idx->entries_cap = tmp;
838 memset(idx->reserved, 0, sizeof(idx->reserved));
839
840 /* Pull IDX MDLs and buffers from q_full and populate the entries */
841 do {
842 mdl = cx18_dequeue(s, &s->q_full);
843 if (mdl == NULL) /* No more IDX data right now */
844 break;
845
846 /* Extract the Index entry data from the MDL and buffers */
847 cx18_process_idx_data(s, mdl, idx);
848 if (mdl->readpos < mdl->bytesused) {
849 /* We finished with data remaining, push the MDL back */
850 cx18_push(s, mdl, &s->q_full);
851 break;
852 }
853
854 /* We drained this MDL, schedule it to go to the firmware */
855 cx18_enqueue(s, mdl, &s->q_free);
856
857 } while (idx->entries < V4L2_ENC_IDX_ENTRIES);
858
859 /* Tell the work handler to send free IDX MDLs to the firmware */
860 cx18_stream_load_fw_queue(s);
861 return 0;
Andy Walls3b6fe582008-06-21 08:36:31 -0300862}
863
Steven Tothb7101de2011-04-06 08:32:56 -0300864static struct videobuf_queue *cx18_vb_queue(struct cx18_open_id *id)
865{
866 struct videobuf_queue *q = NULL;
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300867 struct cx18 *cx = id->cx;
868 struct cx18_stream *s = &cx->streams[id->type];
Steven Tothb7101de2011-04-06 08:32:56 -0300869
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300870 switch (s->vb_type) {
Steven Tothb7101de2011-04-06 08:32:56 -0300871 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300872 q = &s->vbuf_q;
Steven Tothb7101de2011-04-06 08:32:56 -0300873 break;
874 case V4L2_BUF_TYPE_VBI_CAPTURE:
875 break;
876 default:
877 break;
878 }
879 return q;
880}
881
882static int cx18_streamon(struct file *file, void *priv,
883 enum v4l2_buf_type type)
884{
885 struct cx18_open_id *id = file->private_data;
886 struct cx18 *cx = id->cx;
887 struct cx18_stream *s = &cx->streams[id->type];
888
889 /* Start the hardware only if we're the video device */
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300890 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
891 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
Steven Tothb7101de2011-04-06 08:32:56 -0300892 return -EINVAL;
893
894 if (id->type != CX18_ENC_STREAM_TYPE_YUV)
895 return -EINVAL;
896
897 /* Establish a buffer timeout */
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300898 mod_timer(&s->vb_timeout, msecs_to_jiffies(2000) + jiffies);
Steven Tothb7101de2011-04-06 08:32:56 -0300899
900 return videobuf_streamon(cx18_vb_queue(id));
901}
902
903static int cx18_streamoff(struct file *file, void *priv,
904 enum v4l2_buf_type type)
905{
906 struct cx18_open_id *id = file->private_data;
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300907 struct cx18 *cx = id->cx;
908 struct cx18_stream *s = &cx->streams[id->type];
Steven Tothb7101de2011-04-06 08:32:56 -0300909
910 /* Start the hardware only if we're the video device */
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300911 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
912 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
Steven Tothb7101de2011-04-06 08:32:56 -0300913 return -EINVAL;
914
915 if (id->type != CX18_ENC_STREAM_TYPE_YUV)
916 return -EINVAL;
917
918 return videobuf_streamoff(cx18_vb_queue(id));
919}
920
921static int cx18_reqbufs(struct file *file, void *priv,
922 struct v4l2_requestbuffers *rb)
923{
924 struct cx18_open_id *id = file->private_data;
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300925 struct cx18 *cx = id->cx;
926 struct cx18_stream *s = &cx->streams[id->type];
Steven Tothb7101de2011-04-06 08:32:56 -0300927
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300928 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
929 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
Steven Tothb7101de2011-04-06 08:32:56 -0300930 return -EINVAL;
931
932 return videobuf_reqbufs(cx18_vb_queue(id), rb);
933}
934
935static int cx18_querybuf(struct file *file, void *priv,
936 struct v4l2_buffer *b)
937{
938 struct cx18_open_id *id = file->private_data;
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300939 struct cx18 *cx = id->cx;
940 struct cx18_stream *s = &cx->streams[id->type];
Steven Tothb7101de2011-04-06 08:32:56 -0300941
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300942 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
943 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
Steven Tothb7101de2011-04-06 08:32:56 -0300944 return -EINVAL;
945
946 return videobuf_querybuf(cx18_vb_queue(id), b);
947}
948
949static int cx18_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
950{
951 struct cx18_open_id *id = file->private_data;
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300952 struct cx18 *cx = id->cx;
953 struct cx18_stream *s = &cx->streams[id->type];
Steven Tothb7101de2011-04-06 08:32:56 -0300954
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300955 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
956 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
Steven Tothb7101de2011-04-06 08:32:56 -0300957 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;
Simon Farnsworth1bf58422011-05-03 08:57:40 -0300965 struct cx18 *cx = id->cx;
966 struct cx18_stream *s = &cx->streams[id->type];
967
968 if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
969 (s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
Steven Tothb7101de2011-04-06 08:32:56 -0300970 return -EINVAL;
971
972 return videobuf_dqbuf(cx18_vb_queue(id), b, file->f_flags & O_NONBLOCK);
973}
974
Andy Walls3b6fe582008-06-21 08:36:31 -0300975static int cx18_encoder_cmd(struct file *file, void *fh,
976 struct v4l2_encoder_cmd *enc)
977{
Hans Verkuil0b5f2652011-03-12 06:35:33 -0300978 struct cx18_open_id *id = fh2id(fh);
Andy Walls3b6fe582008-06-21 08:36:31 -0300979 struct cx18 *cx = id->cx;
Andy Wallsd3c5e702008-08-23 16:42:29 -0300980 u32 h;
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 return cx18_start_capture(id);
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 cx18_stop_capture(id,
992 enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
993 break;
994
995 case V4L2_ENC_CMD_PAUSE:
996 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
997 enc->flags = 0;
998 if (!atomic_read(&cx->ana_capturing))
999 return -EPERM;
1000 if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
1001 return 0;
Andy Wallsd3c5e702008-08-23 16:42:29 -03001002 h = cx18_find_handle(cx);
1003 if (h == CX18_INVALID_TASK_HANDLE) {
1004 CX18_ERR("Can't find valid task handle for "
1005 "V4L2_ENC_CMD_PAUSE\n");
1006 return -EBADFD;
1007 }
Andy Walls3b6fe582008-06-21 08:36:31 -03001008 cx18_mute(cx);
Andy Wallsd3c5e702008-08-23 16:42:29 -03001009 cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, h);
Andy Walls3b6fe582008-06-21 08:36:31 -03001010 break;
1011
1012 case V4L2_ENC_CMD_RESUME:
1013 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1014 enc->flags = 0;
1015 if (!atomic_read(&cx->ana_capturing))
1016 return -EPERM;
1017 if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
1018 return 0;
Andy Wallsd3c5e702008-08-23 16:42:29 -03001019 h = cx18_find_handle(cx);
1020 if (h == CX18_INVALID_TASK_HANDLE) {
1021 CX18_ERR("Can't find valid task handle for "
1022 "V4L2_ENC_CMD_RESUME\n");
1023 return -EBADFD;
1024 }
1025 cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, h);
Andy Walls3b6fe582008-06-21 08:36:31 -03001026 cx18_unmute(cx);
1027 break;
1028
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001029 default:
Andy Walls3b6fe582008-06-21 08:36:31 -03001030 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1031 return -EINVAL;
1032 }
1033 return 0;
1034}
1035
1036static int cx18_try_encoder_cmd(struct file *file, void *fh,
1037 struct v4l2_encoder_cmd *enc)
1038{
Hans Verkuil0b5f2652011-03-12 06:35:33 -03001039 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -03001040
Andy Walls3b6fe582008-06-21 08:36:31 -03001041 switch (enc->cmd) {
1042 case V4L2_ENC_CMD_START:
1043 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
1044 enc->flags = 0;
1045 break;
1046
1047 case V4L2_ENC_CMD_STOP:
1048 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
1049 enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
1050 break;
1051
1052 case V4L2_ENC_CMD_PAUSE:
1053 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
1054 enc->flags = 0;
1055 break;
1056
1057 case V4L2_ENC_CMD_RESUME:
1058 CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1059 enc->flags = 0;
1060 break;
1061
1062 default:
1063 CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1064 return -EINVAL;
1065 }
1066 return 0;
1067}
1068
1069static int cx18_log_status(struct file *file, void *fh)
1070{
Hans Verkuil0b5f2652011-03-12 06:35:33 -03001071 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -03001072 struct v4l2_input vidin;
1073 struct v4l2_audio audin;
1074 int i;
1075
Andy Wallse0f28b62009-01-10 14:13:46 -03001076 CX18_INFO("Version: %s Card: %s\n", CX18_VERSION, cx->card_name);
Andy Walls3b6fe582008-06-21 08:36:31 -03001077 if (cx->hw_flags & CX18_HW_TVEEPROM) {
1078 struct tveeprom tv;
1079
1080 cx18_read_eeprom(cx, &tv);
1081 }
Andy Wallsff2a2002009-02-20 23:52:13 -03001082 cx18_call_all(cx, core, log_status);
Andy Walls3b6fe582008-06-21 08:36:31 -03001083 cx18_get_input(cx, cx->active_input, &vidin);
1084 cx18_get_audio_input(cx, cx->audio_input, &audin);
1085 CX18_INFO("Video Input: %s\n", vidin.name);
1086 CX18_INFO("Audio Input: %s\n", audin.name);
Andy Walls8abdd002008-07-13 19:05:25 -03001087 mutex_lock(&cx->gpio_lock);
Hans Verkuil3d66c402008-06-21 11:19:34 -03001088 CX18_INFO("GPIO: direction 0x%08x, value 0x%08x\n",
1089 cx->gpio_dir, cx->gpio_val);
Andy Walls8abdd002008-07-13 19:05:25 -03001090 mutex_unlock(&cx->gpio_lock);
Andy Walls3b6fe582008-06-21 08:36:31 -03001091 CX18_INFO("Tuner: %s\n",
1092 test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ? "Radio" : "TV");
Hans Verkuila75b9be2010-12-31 10:22:52 -03001093 v4l2_ctrl_handler_log_status(&cx->cxhdl.hdl, cx->v4l2_dev.name);
Andy Walls3b6fe582008-06-21 08:36:31 -03001094 CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
1095 for (i = 0; i < CX18_MAX_STREAMS; i++) {
1096 struct cx18_stream *s = &cx->streams[i];
1097
Andy Walls3d059132009-01-10 21:54:39 -03001098 if (s->video_dev == NULL || s->buffers == 0)
Andy Walls3b6fe582008-06-21 08:36:31 -03001099 continue;
1100 CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
1101 s->name, s->s_flags,
Andy Walls52fcb3e2009-11-08 23:45:24 -03001102 atomic_read(&s->q_full.depth) * s->bufs_per_mdl * 100
1103 / s->buffers,
Andy Walls3b6fe582008-06-21 08:36:31 -03001104 (s->buffers * s->buf_size) / 1024, s->buffers);
1105 }
1106 CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
1107 (long long)cx->mpg_data_received,
1108 (long long)cx->vbi_data_inserted);
Andy Walls3b6fe582008-06-21 08:36:31 -03001109 return 0;
1110}
1111
Hans Verkuil99cd47bc2011-03-11 19:00:56 -03001112static long cx18_default(struct file *file, void *fh, bool valid_prio,
1113 int cmd, void *arg)
Andy Walls3b6fe582008-06-21 08:36:31 -03001114{
Hans Verkuil0b5f2652011-03-12 06:35:33 -03001115 struct cx18 *cx = fh2id(fh)->cx;
Andy Walls3b6fe582008-06-21 08:36:31 -03001116
1117 switch (cmd) {
Andy Walls02fa2722008-07-13 19:30:15 -03001118 case VIDIOC_INT_RESET: {
1119 u32 val = *(u32 *)arg;
1120
1121 if ((val == 0) || (val & 0x01))
Andy Wallseefe1012009-02-21 18:42:49 -03001122 cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL, core, reset,
1123 (u32) CX18_GPIO_RESET_Z8F0811);
Andy Walls02fa2722008-07-13 19:30:15 -03001124 break;
1125 }
1126
Andy Walls3b6fe582008-06-21 08:36:31 -03001127 default:
Hans Verkuild1c754a2012-04-19 12:36:03 -03001128 return -ENOTTY;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001129 }
1130 return 0;
1131}
1132
Hans Verkuila3998102008-07-21 02:57:38 -03001133static const struct v4l2_ioctl_ops cx18_ioctl_ops = {
1134 .vidioc_querycap = cx18_querycap,
Hans Verkuila3998102008-07-21 02:57:38 -03001135 .vidioc_s_audio = cx18_s_audio,
1136 .vidioc_g_audio = cx18_g_audio,
1137 .vidioc_enumaudio = cx18_enumaudio,
1138 .vidioc_enum_input = cx18_enum_input,
1139 .vidioc_cropcap = cx18_cropcap,
1140 .vidioc_s_crop = cx18_s_crop,
1141 .vidioc_g_crop = cx18_g_crop,
1142 .vidioc_g_input = cx18_g_input,
1143 .vidioc_s_input = cx18_s_input,
1144 .vidioc_g_frequency = cx18_g_frequency,
1145 .vidioc_s_frequency = cx18_s_frequency,
1146 .vidioc_s_tuner = cx18_s_tuner,
1147 .vidioc_g_tuner = cx18_g_tuner,
1148 .vidioc_g_enc_index = cx18_g_enc_index,
1149 .vidioc_g_std = cx18_g_std,
1150 .vidioc_s_std = cx18_s_std,
1151 .vidioc_log_status = cx18_log_status,
1152 .vidioc_enum_fmt_vid_cap = cx18_enum_fmt_vid_cap,
1153 .vidioc_encoder_cmd = cx18_encoder_cmd,
1154 .vidioc_try_encoder_cmd = cx18_try_encoder_cmd,
1155 .vidioc_g_fmt_vid_cap = cx18_g_fmt_vid_cap,
1156 .vidioc_g_fmt_vbi_cap = cx18_g_fmt_vbi_cap,
1157 .vidioc_g_fmt_sliced_vbi_cap = cx18_g_fmt_sliced_vbi_cap,
1158 .vidioc_s_fmt_vid_cap = cx18_s_fmt_vid_cap,
1159 .vidioc_s_fmt_vbi_cap = cx18_s_fmt_vbi_cap,
1160 .vidioc_s_fmt_sliced_vbi_cap = cx18_s_fmt_sliced_vbi_cap,
1161 .vidioc_try_fmt_vid_cap = cx18_try_fmt_vid_cap,
1162 .vidioc_try_fmt_vbi_cap = cx18_try_fmt_vbi_cap,
1163 .vidioc_try_fmt_sliced_vbi_cap = cx18_try_fmt_sliced_vbi_cap,
1164 .vidioc_g_sliced_vbi_cap = cx18_g_sliced_vbi_cap,
1165 .vidioc_g_chip_ident = cx18_g_chip_ident,
1166#ifdef CONFIG_VIDEO_ADV_DEBUG
1167 .vidioc_g_register = cx18_g_register,
1168 .vidioc_s_register = cx18_s_register,
1169#endif
1170 .vidioc_default = cx18_default,
Steven Tothb7101de2011-04-06 08:32:56 -03001171 .vidioc_streamon = cx18_streamon,
1172 .vidioc_streamoff = cx18_streamoff,
1173 .vidioc_reqbufs = cx18_reqbufs,
1174 .vidioc_querybuf = cx18_querybuf,
1175 .vidioc_qbuf = cx18_qbuf,
1176 .vidioc_dqbuf = cx18_dqbuf,
Hans Verkuila3998102008-07-21 02:57:38 -03001177};
1178
Andy Walls3b6fe582008-06-21 08:36:31 -03001179void cx18_set_funcs(struct video_device *vdev)
1180{
Hans Verkuila3998102008-07-21 02:57:38 -03001181 vdev->ioctl_ops = &cx18_ioctl_ops;
Andy Walls3b6fe582008-06-21 08:36:31 -03001182}