xref: /openbmc/linux/drivers/media/pci/cx18/cx18-ioctl.c (revision df3305156f989339529b3d6744b898d498fb1f7b)
1 /*
2  *  cx18 ioctl system call
3  *
4  *  Derived from ivtv-ioctl.c
5  *
6  *  Copyright (C) 2007  Hans Verkuil <hverkuil@xs4all.nl>
7  *  Copyright (C) 2008  Andy Walls <awalls@md.metrocast.net>
8  *
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"
26 #include "cx18-io.h"
27 #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 
43 u16 cx18_service2vbi(int type)
44 {
45 	switch (type) {
46 	case V4L2_SLICED_TELETEXT_B:
47 		return CX18_SLICED_TYPE_TELETEXT_B;
48 	case V4L2_SLICED_CAPTION_525:
49 		return CX18_SLICED_TYPE_CAPTION_525;
50 	case V4L2_SLICED_WSS_625:
51 		return CX18_SLICED_TYPE_WSS_625;
52 	case V4L2_SLICED_VPS:
53 		return CX18_SLICED_TYPE_VPS;
54 	default:
55 		return 0;
56 	}
57 }
58 
59 /* Check if VBI services are allowed on the (field, line) for the video std */
60 static int valid_service_line(int field, int line, int is_pal)
61 {
62 	return (is_pal && line >= 6 &&
63 		((field == 0 && line <= 23) || (field == 1 && line <= 22))) ||
64 	       (!is_pal && line >= 10 && line < 22);
65 }
66 
67 /*
68  * For a (field, line, std) and inbound potential set of services for that line,
69  * return the first valid service of those passed in the incoming set for that
70  * line in priority order:
71  * CC, VPS, or WSS over TELETEXT for well known lines
72  * TELETEXT, before VPS, before CC, before WSS, for other lines
73  */
74 static u16 select_service_from_set(int field, int line, u16 set, int is_pal)
75 {
76 	u16 valid_set = (is_pal ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525);
77 	int i;
78 
79 	set = set & valid_set;
80 	if (set == 0 || !valid_service_line(field, line, is_pal))
81 		return 0;
82 	if (!is_pal) {
83 		if (line == 21 && (set & V4L2_SLICED_CAPTION_525))
84 			return V4L2_SLICED_CAPTION_525;
85 	} else {
86 		if (line == 16 && field == 0 && (set & V4L2_SLICED_VPS))
87 			return V4L2_SLICED_VPS;
88 		if (line == 23 && field == 0 && (set & V4L2_SLICED_WSS_625))
89 			return V4L2_SLICED_WSS_625;
90 		if (line == 23)
91 			return 0;
92 	}
93 	for (i = 0; i < 32; i++) {
94 		if ((1 << i) & set)
95 			return 1 << i;
96 	}
97 	return 0;
98 }
99 
100 /*
101  * Expand the service_set of *fmt into valid service_lines for the std,
102  * and clear the passed in fmt->service_set
103  */
104 void cx18_expand_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
105 {
106 	u16 set = fmt->service_set;
107 	int f, l;
108 
109 	fmt->service_set = 0;
110 	for (f = 0; f < 2; f++) {
111 		for (l = 0; l < 24; l++)
112 			fmt->service_lines[f][l] = select_service_from_set(f, l, set, is_pal);
113 	}
114 }
115 
116 /*
117  * Sanitize the service_lines in *fmt per the video std, and return 1
118  * if any service_line is left as valid after santization
119  */
120 static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
121 {
122 	int f, l;
123 	u16 set = 0;
124 
125 	for (f = 0; f < 2; f++) {
126 		for (l = 0; l < 24; l++) {
127 			fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
128 			set |= fmt->service_lines[f][l];
129 		}
130 	}
131 	return set != 0;
132 }
133 
134 /* Compute the service_set from the assumed valid service_lines of *fmt */
135 u16 cx18_get_service_set(struct v4l2_sliced_vbi_format *fmt)
136 {
137 	int f, l;
138 	u16 set = 0;
139 
140 	for (f = 0; f < 2; f++) {
141 		for (l = 0; l < 24; l++)
142 			set |= fmt->service_lines[f][l];
143 	}
144 	return set;
145 }
146 
147 static int cx18_g_fmt_vid_cap(struct file *file, void *fh,
148 				struct v4l2_format *fmt)
149 {
150 	struct cx18_open_id *id = fh2id(fh);
151 	struct cx18 *cx = id->cx;
152 	struct cx18_stream *s = &cx->streams[id->type];
153 	struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
154 
155 	pixfmt->width = cx->cxhdl.width;
156 	pixfmt->height = cx->cxhdl.height;
157 	pixfmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
158 	pixfmt->field = V4L2_FIELD_INTERLACED;
159 	if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
160 		pixfmt->pixelformat = s->pixelformat;
161 		pixfmt->sizeimage = s->vb_bytes_per_frame;
162 		pixfmt->bytesperline = s->vb_bytes_per_line;
163 	} else {
164 		pixfmt->pixelformat = V4L2_PIX_FMT_MPEG;
165 		pixfmt->sizeimage = 128 * 1024;
166 		pixfmt->bytesperline = 0;
167 	}
168 	return 0;
169 }
170 
171 static int cx18_g_fmt_vbi_cap(struct file *file, void *fh,
172 				struct v4l2_format *fmt)
173 {
174 	struct cx18 *cx = fh2id(fh)->cx;
175 	struct v4l2_vbi_format *vbifmt = &fmt->fmt.vbi;
176 
177 	vbifmt->sampling_rate = 27000000;
178 	vbifmt->offset = 248; /* FIXME - slightly wrong for both 50 & 60 Hz */
179 	vbifmt->samples_per_line = vbi_active_samples - 4;
180 	vbifmt->sample_format = V4L2_PIX_FMT_GREY;
181 	vbifmt->start[0] = cx->vbi.start[0];
182 	vbifmt->start[1] = cx->vbi.start[1];
183 	vbifmt->count[0] = vbifmt->count[1] = cx->vbi.count;
184 	vbifmt->flags = 0;
185 	vbifmt->reserved[0] = 0;
186 	vbifmt->reserved[1] = 0;
187 	return 0;
188 }
189 
190 static int cx18_g_fmt_sliced_vbi_cap(struct file *file, void *fh,
191 					struct v4l2_format *fmt)
192 {
193 	struct cx18 *cx = fh2id(fh)->cx;
194 	struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
195 
196 	/* sane, V4L2 spec compliant, defaults */
197 	vbifmt->reserved[0] = 0;
198 	vbifmt->reserved[1] = 0;
199 	vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
200 	memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
201 	vbifmt->service_set = 0;
202 
203 	/*
204 	 * Fetch the configured service_lines and total service_set from the
205 	 * digitizer/slicer.  Note, cx18_av_vbi() wipes the passed in
206 	 * fmt->fmt.sliced under valid calling conditions
207 	 */
208 	if (v4l2_subdev_call(cx->sd_av, vbi, g_sliced_fmt, &fmt->fmt.sliced))
209 		return -EINVAL;
210 
211 	vbifmt->service_set = cx18_get_service_set(vbifmt);
212 	return 0;
213 }
214 
215 static int cx18_try_fmt_vid_cap(struct file *file, void *fh,
216 				struct v4l2_format *fmt)
217 {
218 	struct cx18_open_id *id = fh2id(fh);
219 	struct cx18 *cx = id->cx;
220 	int w = fmt->fmt.pix.width;
221 	int h = fmt->fmt.pix.height;
222 	int min_h = 2;
223 
224 	w = min(w, 720);
225 	w = max(w, 2);
226 	if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
227 		/* YUV height must be a multiple of 32 */
228 		h &= ~0x1f;
229 		min_h = 32;
230 	}
231 	h = min(h, cx->is_50hz ? 576 : 480);
232 	h = max(h, min_h);
233 
234 	fmt->fmt.pix.width = w;
235 	fmt->fmt.pix.height = h;
236 	return 0;
237 }
238 
239 static int cx18_try_fmt_vbi_cap(struct file *file, void *fh,
240 				struct v4l2_format *fmt)
241 {
242 	return cx18_g_fmt_vbi_cap(file, fh, fmt);
243 }
244 
245 static int cx18_try_fmt_sliced_vbi_cap(struct file *file, void *fh,
246 					struct v4l2_format *fmt)
247 {
248 	struct cx18 *cx = fh2id(fh)->cx;
249 	struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
250 
251 	vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
252 	vbifmt->reserved[0] = 0;
253 	vbifmt->reserved[1] = 0;
254 
255 	/* If given a service set, expand it validly & clear passed in set */
256 	if (vbifmt->service_set)
257 		cx18_expand_service_set(vbifmt, cx->is_50hz);
258 	/* Sanitize the service_lines, and compute the new set if any valid */
259 	if (check_service_set(vbifmt, cx->is_50hz))
260 		vbifmt->service_set = cx18_get_service_set(vbifmt);
261 	return 0;
262 }
263 
264 static int cx18_s_fmt_vid_cap(struct file *file, void *fh,
265 				struct v4l2_format *fmt)
266 {
267 	struct cx18_open_id *id = fh2id(fh);
268 	struct cx18 *cx = id->cx;
269 	struct v4l2_mbus_framefmt mbus_fmt;
270 	struct cx18_stream *s = &cx->streams[id->type];
271 	int ret;
272 	int w, h;
273 
274 	ret = cx18_try_fmt_vid_cap(file, fh, fmt);
275 	if (ret)
276 		return ret;
277 	w = fmt->fmt.pix.width;
278 	h = fmt->fmt.pix.height;
279 
280 	if (cx->cxhdl.width == w && cx->cxhdl.height == h &&
281 	    s->pixelformat == fmt->fmt.pix.pixelformat)
282 		return 0;
283 
284 	if (atomic_read(&cx->ana_capturing) > 0)
285 		return -EBUSY;
286 
287 	s->pixelformat = fmt->fmt.pix.pixelformat;
288 	/* HM12 YUV size is (Y=(h*720) + UV=(h*(720/2)))
289 	   UYUV YUV size is (Y=(h*720) + UV=(h*(720))) */
290 	if (s->pixelformat == V4L2_PIX_FMT_HM12) {
291 		s->vb_bytes_per_frame = h * 720 * 3 / 2;
292 		s->vb_bytes_per_line = 720; /* First plane */
293 	} else {
294 		s->vb_bytes_per_frame = h * 720 * 2;
295 		s->vb_bytes_per_line = 1440; /* Packed */
296 	}
297 
298 	mbus_fmt.width = cx->cxhdl.width = w;
299 	mbus_fmt.height = cx->cxhdl.height = h;
300 	mbus_fmt.code = MEDIA_BUS_FMT_FIXED;
301 	v4l2_subdev_call(cx->sd_av, video, s_mbus_fmt, &mbus_fmt);
302 	return cx18_g_fmt_vid_cap(file, fh, fmt);
303 }
304 
305 static int cx18_s_fmt_vbi_cap(struct file *file, void *fh,
306 				struct v4l2_format *fmt)
307 {
308 	struct cx18_open_id *id = fh2id(fh);
309 	struct cx18 *cx = id->cx;
310 	int ret;
311 
312 	/*
313 	 * Changing the Encoder's Raw VBI parameters won't have any effect
314 	 * if any analog capture is ongoing
315 	 */
316 	if (!cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
317 		return -EBUSY;
318 
319 	/*
320 	 * Set the digitizer registers for raw active VBI.
321 	 * Note cx18_av_vbi_wipes out a lot of the passed in fmt under valid
322 	 * calling conditions
323 	 */
324 	ret = v4l2_subdev_call(cx->sd_av, vbi, s_raw_fmt, &fmt->fmt.vbi);
325 	if (ret)
326 		return ret;
327 
328 	/* Store our new v4l2 (non-)sliced VBI state */
329 	cx->vbi.sliced_in->service_set = 0;
330 	cx->vbi.in.type = V4L2_BUF_TYPE_VBI_CAPTURE;
331 
332 	return cx18_g_fmt_vbi_cap(file, fh, fmt);
333 }
334 
335 static int cx18_s_fmt_sliced_vbi_cap(struct file *file, void *fh,
336 					struct v4l2_format *fmt)
337 {
338 	struct cx18_open_id *id = fh2id(fh);
339 	struct cx18 *cx = id->cx;
340 	int ret;
341 	struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
342 
343 	cx18_try_fmt_sliced_vbi_cap(file, fh, fmt);
344 
345 	/*
346 	 * Changing the Encoder's Raw VBI parameters won't have any effect
347 	 * if any analog capture is ongoing
348 	 */
349 	if (cx18_raw_vbi(cx) && atomic_read(&cx->ana_capturing) > 0)
350 		return -EBUSY;
351 
352 	/*
353 	 * Set the service_lines requested in the digitizer/slicer registers.
354 	 * Note, cx18_av_vbi() wipes some "impossible" service lines in the
355 	 * passed in fmt->fmt.sliced under valid calling conditions
356 	 */
357 	ret = v4l2_subdev_call(cx->sd_av, vbi, s_sliced_fmt, &fmt->fmt.sliced);
358 	if (ret)
359 		return ret;
360 	/* Store our current v4l2 sliced VBI settings */
361 	cx->vbi.in.type =  V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
362 	memcpy(cx->vbi.sliced_in, vbifmt, sizeof(*cx->vbi.sliced_in));
363 	return 0;
364 }
365 
366 #ifdef CONFIG_VIDEO_ADV_DEBUG
367 static int cx18_g_register(struct file *file, void *fh,
368 				struct v4l2_dbg_register *reg)
369 {
370 	struct cx18 *cx = fh2id(fh)->cx;
371 
372 	if (reg->reg & 0x3)
373 		return -EINVAL;
374 	if (reg->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
375 		return -EINVAL;
376 	reg->size = 4;
377 	reg->val = cx18_read_enc(cx, reg->reg);
378 	return 0;
379 }
380 
381 static int cx18_s_register(struct file *file, void *fh,
382 				const struct v4l2_dbg_register *reg)
383 {
384 	struct cx18 *cx = fh2id(fh)->cx;
385 
386 	if (reg->reg & 0x3)
387 		return -EINVAL;
388 	if (reg->reg >= CX18_MEM_OFFSET + CX18_MEM_SIZE)
389 		return -EINVAL;
390 	cx18_write_enc(cx, reg->val, reg->reg);
391 	return 0;
392 }
393 #endif
394 
395 static int cx18_querycap(struct file *file, void *fh,
396 				struct v4l2_capability *vcap)
397 {
398 	struct cx18_open_id *id = fh2id(fh);
399 	struct cx18_stream *s = video_drvdata(file);
400 	struct cx18 *cx = id->cx;
401 
402 	strlcpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
403 	strlcpy(vcap->card, cx->card_name, sizeof(vcap->card));
404 	snprintf(vcap->bus_info, sizeof(vcap->bus_info),
405 		 "PCI:%s", pci_name(cx->pci_dev));
406 	vcap->capabilities = cx->v4l2_cap;	/* capabilities */
407 	vcap->device_caps = s->v4l2_dev_caps;	/* device capabilities */
408 	vcap->capabilities |= V4L2_CAP_DEVICE_CAPS;
409 	return 0;
410 }
411 
412 static int cx18_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
413 {
414 	struct cx18 *cx = fh2id(fh)->cx;
415 
416 	return cx18_get_audio_input(cx, vin->index, vin);
417 }
418 
419 static int cx18_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
420 {
421 	struct cx18 *cx = fh2id(fh)->cx;
422 
423 	vin->index = cx->audio_input;
424 	return cx18_get_audio_input(cx, vin->index, vin);
425 }
426 
427 static int cx18_s_audio(struct file *file, void *fh, const struct v4l2_audio *vout)
428 {
429 	struct cx18 *cx = fh2id(fh)->cx;
430 
431 	if (vout->index >= cx->nof_audio_inputs)
432 		return -EINVAL;
433 	cx->audio_input = vout->index;
434 	cx18_audio_set_io(cx);
435 	return 0;
436 }
437 
438 static int cx18_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
439 {
440 	struct cx18 *cx = fh2id(fh)->cx;
441 
442 	/* set it to defaults from our table */
443 	return cx18_get_input(cx, vin->index, vin);
444 }
445 
446 static int cx18_cropcap(struct file *file, void *fh,
447 			struct v4l2_cropcap *cropcap)
448 {
449 	struct cx18 *cx = fh2id(fh)->cx;
450 
451 	if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
452 		return -EINVAL;
453 	cropcap->bounds.top = cropcap->bounds.left = 0;
454 	cropcap->bounds.width = 720;
455 	cropcap->bounds.height = cx->is_50hz ? 576 : 480;
456 	cropcap->pixelaspect.numerator = cx->is_50hz ? 59 : 10;
457 	cropcap->pixelaspect.denominator = cx->is_50hz ? 54 : 11;
458 	cropcap->defrect = cropcap->bounds;
459 	return 0;
460 }
461 
462 static int cx18_s_crop(struct file *file, void *fh, const struct v4l2_crop *crop)
463 {
464 	struct cx18_open_id *id = fh2id(fh);
465 	struct cx18 *cx = id->cx;
466 
467 	if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
468 		return -EINVAL;
469 	CX18_DEBUG_WARN("VIDIOC_S_CROP not implemented\n");
470 	return -EINVAL;
471 }
472 
473 static int cx18_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
474 {
475 	struct cx18 *cx = fh2id(fh)->cx;
476 
477 	if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
478 		return -EINVAL;
479 	CX18_DEBUG_WARN("VIDIOC_G_CROP not implemented\n");
480 	return -EINVAL;
481 }
482 
483 static int cx18_enum_fmt_vid_cap(struct file *file, void *fh,
484 					struct v4l2_fmtdesc *fmt)
485 {
486 	static const struct v4l2_fmtdesc formats[] = {
487 		{ 0, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
488 		  "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12, { 0, 0, 0, 0 }
489 		},
490 		{ 1, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FMT_FLAG_COMPRESSED,
491 		  "MPEG", V4L2_PIX_FMT_MPEG, { 0, 0, 0, 0 }
492 		},
493 		{ 2, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
494 		  "UYVY 4:2:2", V4L2_PIX_FMT_UYVY, { 0, 0, 0, 0 }
495 		},
496 	};
497 
498 	if (fmt->index > ARRAY_SIZE(formats) - 1)
499 		return -EINVAL;
500 	*fmt = formats[fmt->index];
501 	return 0;
502 }
503 
504 static int cx18_g_input(struct file *file, void *fh, unsigned int *i)
505 {
506 	struct cx18 *cx = fh2id(fh)->cx;
507 
508 	*i = cx->active_input;
509 	return 0;
510 }
511 
512 int cx18_s_input(struct file *file, void *fh, unsigned int inp)
513 {
514 	struct cx18_open_id *id = fh2id(fh);
515 	struct cx18 *cx = id->cx;
516 
517 	if (inp >= cx->nof_inputs)
518 		return -EINVAL;
519 
520 	if (inp == cx->active_input) {
521 		CX18_DEBUG_INFO("Input unchanged\n");
522 		return 0;
523 	}
524 
525 	CX18_DEBUG_INFO("Changing input from %d to %d\n",
526 			cx->active_input, inp);
527 
528 	cx->active_input = inp;
529 	/* Set the audio input to whatever is appropriate for the input type. */
530 	cx->audio_input = cx->card->video_inputs[inp].audio_index;
531 
532 	/* prevent others from messing with the streams until
533 	   we're finished changing inputs. */
534 	cx18_mute(cx);
535 	cx18_video_set_io(cx);
536 	cx18_audio_set_io(cx);
537 	cx18_unmute(cx);
538 	return 0;
539 }
540 
541 static int cx18_g_frequency(struct file *file, void *fh,
542 				struct v4l2_frequency *vf)
543 {
544 	struct cx18 *cx = fh2id(fh)->cx;
545 
546 	if (vf->tuner != 0)
547 		return -EINVAL;
548 
549 	cx18_call_all(cx, tuner, g_frequency, vf);
550 	return 0;
551 }
552 
553 int cx18_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
554 {
555 	struct cx18_open_id *id = fh2id(fh);
556 	struct cx18 *cx = id->cx;
557 
558 	if (vf->tuner != 0)
559 		return -EINVAL;
560 
561 	cx18_mute(cx);
562 	CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
563 	cx18_call_all(cx, tuner, s_frequency, vf);
564 	cx18_unmute(cx);
565 	return 0;
566 }
567 
568 static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
569 {
570 	struct cx18 *cx = fh2id(fh)->cx;
571 
572 	*std = cx->std;
573 	return 0;
574 }
575 
576 int cx18_s_std(struct file *file, void *fh, v4l2_std_id std)
577 {
578 	struct cx18_open_id *id = fh2id(fh);
579 	struct cx18 *cx = id->cx;
580 
581 	if ((std & V4L2_STD_ALL) == 0)
582 		return -EINVAL;
583 
584 	if (std == cx->std)
585 		return 0;
586 
587 	if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
588 	    atomic_read(&cx->ana_capturing) > 0) {
589 		/* Switching standard would turn off the radio or mess
590 		   with already running streams, prevent that by
591 		   returning EBUSY. */
592 		return -EBUSY;
593 	}
594 
595 	cx->std = std;
596 	cx->is_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
597 	cx->is_50hz = !cx->is_60hz;
598 	cx2341x_handler_set_50hz(&cx->cxhdl, cx->is_50hz);
599 	cx->cxhdl.width = 720;
600 	cx->cxhdl.height = cx->is_50hz ? 576 : 480;
601 	cx->vbi.count = cx->is_50hz ? 18 : 12;
602 	cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
603 	cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
604 	CX18_DEBUG_INFO("Switching standard to %llx.\n",
605 			(unsigned long long) cx->std);
606 
607 	/* Tuner */
608 	cx18_call_all(cx, video, s_std, cx->std);
609 	return 0;
610 }
611 
612 static int cx18_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
613 {
614 	struct cx18_open_id *id = fh2id(fh);
615 	struct cx18 *cx = id->cx;
616 
617 	if (vt->index != 0)
618 		return -EINVAL;
619 
620 	cx18_call_all(cx, tuner, s_tuner, vt);
621 	return 0;
622 }
623 
624 static int cx18_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
625 {
626 	struct cx18 *cx = fh2id(fh)->cx;
627 
628 	if (vt->index != 0)
629 		return -EINVAL;
630 
631 	cx18_call_all(cx, tuner, g_tuner, vt);
632 
633 	if (vt->type == V4L2_TUNER_RADIO)
634 		strlcpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
635 	else
636 		strlcpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
637 	return 0;
638 }
639 
640 static int cx18_g_sliced_vbi_cap(struct file *file, void *fh,
641 					struct v4l2_sliced_vbi_cap *cap)
642 {
643 	struct cx18 *cx = fh2id(fh)->cx;
644 	int set = cx->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
645 	int f, l;
646 
647 	if (cap->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
648 		return -EINVAL;
649 
650 	cap->service_set = 0;
651 	for (f = 0; f < 2; f++) {
652 		for (l = 0; l < 24; l++) {
653 			if (valid_service_line(f, l, cx->is_50hz)) {
654 				/*
655 				 * We can find all v4l2 supported vbi services
656 				 * for the standard, on a valid line for the std
657 				 */
658 				cap->service_lines[f][l] = set;
659 				cap->service_set |= set;
660 			} else
661 				cap->service_lines[f][l] = 0;
662 		}
663 	}
664 	for (f = 0; f < 3; f++)
665 		cap->reserved[f] = 0;
666 	return 0;
667 }
668 
669 static int _cx18_process_idx_data(struct cx18_buffer *buf,
670 				  struct v4l2_enc_idx *idx)
671 {
672 	int consumed, remaining;
673 	struct v4l2_enc_idx_entry *e_idx;
674 	struct cx18_enc_idx_entry *e_buf;
675 
676 	/* Frame type lookup: 1=I, 2=P, 4=B */
677 	const int mapping[8] = {
678 		-1, V4L2_ENC_IDX_FRAME_I, V4L2_ENC_IDX_FRAME_P,
679 		-1, V4L2_ENC_IDX_FRAME_B, -1, -1, -1
680 	};
681 
682 	/*
683 	 * Assumption here is that a buf holds an integral number of
684 	 * struct cx18_enc_idx_entry objects and is properly aligned.
685 	 * This is enforced by the module options on IDX buffer sizes.
686 	 */
687 	remaining = buf->bytesused - buf->readpos;
688 	consumed = 0;
689 	e_idx = &idx->entry[idx->entries];
690 	e_buf = (struct cx18_enc_idx_entry *) &buf->buf[buf->readpos];
691 
692 	while (remaining >= sizeof(struct cx18_enc_idx_entry) &&
693 	       idx->entries < V4L2_ENC_IDX_ENTRIES) {
694 
695 		e_idx->offset = (((u64) le32_to_cpu(e_buf->offset_high)) << 32)
696 				| le32_to_cpu(e_buf->offset_low);
697 
698 		e_idx->pts = (((u64) (le32_to_cpu(e_buf->pts_high) & 1)) << 32)
699 			     | le32_to_cpu(e_buf->pts_low);
700 
701 		e_idx->length = le32_to_cpu(e_buf->length);
702 
703 		e_idx->flags = mapping[le32_to_cpu(e_buf->flags) & 0x7];
704 
705 		e_idx->reserved[0] = 0;
706 		e_idx->reserved[1] = 0;
707 
708 		idx->entries++;
709 		e_idx = &idx->entry[idx->entries];
710 		e_buf++;
711 
712 		remaining -= sizeof(struct cx18_enc_idx_entry);
713 		consumed += sizeof(struct cx18_enc_idx_entry);
714 	}
715 
716 	/* Swallow any partial entries at the end, if there are any */
717 	if (remaining > 0 && remaining < sizeof(struct cx18_enc_idx_entry))
718 		consumed += remaining;
719 
720 	buf->readpos += consumed;
721 	return consumed;
722 }
723 
724 static int cx18_process_idx_data(struct cx18_stream *s, struct cx18_mdl *mdl,
725 				 struct v4l2_enc_idx *idx)
726 {
727 	if (s->type != CX18_ENC_STREAM_TYPE_IDX)
728 		return -EINVAL;
729 
730 	if (mdl->curr_buf == NULL)
731 		mdl->curr_buf = list_first_entry(&mdl->buf_list,
732 						 struct cx18_buffer, list);
733 
734 	if (list_entry_is_past_end(mdl->curr_buf, &mdl->buf_list, list)) {
735 		/*
736 		 * For some reason we've exhausted the buffers, but the MDL
737 		 * object still said some data was unread.
738 		 * Fix that and bail out.
739 		 */
740 		mdl->readpos = mdl->bytesused;
741 		return 0;
742 	}
743 
744 	list_for_each_entry_from(mdl->curr_buf, &mdl->buf_list, list) {
745 
746 		/* Skip any empty buffers in the MDL */
747 		if (mdl->curr_buf->readpos >= mdl->curr_buf->bytesused)
748 			continue;
749 
750 		mdl->readpos += _cx18_process_idx_data(mdl->curr_buf, idx);
751 
752 		/* exit when MDL drained or request satisfied */
753 		if (idx->entries >= V4L2_ENC_IDX_ENTRIES ||
754 		    mdl->curr_buf->readpos < mdl->curr_buf->bytesused ||
755 		    mdl->readpos >= mdl->bytesused)
756 			break;
757 	}
758 	return 0;
759 }
760 
761 static int cx18_g_enc_index(struct file *file, void *fh,
762 				struct v4l2_enc_idx *idx)
763 {
764 	struct cx18 *cx = fh2id(fh)->cx;
765 	struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_IDX];
766 	s32 tmp;
767 	struct cx18_mdl *mdl;
768 
769 	if (!cx18_stream_enabled(s)) /* Module options inhibited IDX stream */
770 		return -EINVAL;
771 
772 	/* Compute the best case number of entries we can buffer */
773 	tmp = s->buffers -
774 			  s->bufs_per_mdl * CX18_ENC_STREAM_TYPE_IDX_FW_MDL_MIN;
775 	if (tmp <= 0)
776 		tmp = 1;
777 	tmp = tmp * s->buf_size / sizeof(struct cx18_enc_idx_entry);
778 
779 	/* Fill out the header of the return structure */
780 	idx->entries = 0;
781 	idx->entries_cap = tmp;
782 	memset(idx->reserved, 0, sizeof(idx->reserved));
783 
784 	/* Pull IDX MDLs and buffers from q_full and populate the entries */
785 	do {
786 		mdl = cx18_dequeue(s, &s->q_full);
787 		if (mdl == NULL) /* No more IDX data right now */
788 			break;
789 
790 		/* Extract the Index entry data from the MDL and buffers */
791 		cx18_process_idx_data(s, mdl, idx);
792 		if (mdl->readpos < mdl->bytesused) {
793 			/* We finished with data remaining, push the MDL back */
794 			cx18_push(s, mdl, &s->q_full);
795 			break;
796 		}
797 
798 		/* We drained this MDL, schedule it to go to the firmware */
799 		cx18_enqueue(s, mdl, &s->q_free);
800 
801 	} while (idx->entries < V4L2_ENC_IDX_ENTRIES);
802 
803 	/* Tell the work handler to send free IDX MDLs to the firmware */
804 	cx18_stream_load_fw_queue(s);
805 	return 0;
806 }
807 
808 static struct videobuf_queue *cx18_vb_queue(struct cx18_open_id *id)
809 {
810 	struct videobuf_queue *q = NULL;
811 	struct cx18 *cx = id->cx;
812 	struct cx18_stream *s = &cx->streams[id->type];
813 
814 	switch (s->vb_type) {
815 	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
816 		q = &s->vbuf_q;
817 		break;
818 	case V4L2_BUF_TYPE_VBI_CAPTURE:
819 		break;
820 	default:
821 		break;
822 	}
823 	return q;
824 }
825 
826 static int cx18_streamon(struct file *file, void *priv,
827 	enum v4l2_buf_type type)
828 {
829 	struct cx18_open_id *id = file->private_data;
830 	struct cx18 *cx = id->cx;
831 	struct cx18_stream *s = &cx->streams[id->type];
832 
833 	/* Start the hardware only if we're the video device */
834 	if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
835 		(s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
836 		return -EINVAL;
837 
838 	if (id->type != CX18_ENC_STREAM_TYPE_YUV)
839 		return -EINVAL;
840 
841 	/* Establish a buffer timeout */
842 	mod_timer(&s->vb_timeout, msecs_to_jiffies(2000) + jiffies);
843 
844 	return videobuf_streamon(cx18_vb_queue(id));
845 }
846 
847 static int cx18_streamoff(struct file *file, void *priv,
848 	enum v4l2_buf_type type)
849 {
850 	struct cx18_open_id *id = file->private_data;
851 	struct cx18 *cx = id->cx;
852 	struct cx18_stream *s = &cx->streams[id->type];
853 
854 	/* Start the hardware only if we're the video device */
855 	if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
856 		(s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
857 		return -EINVAL;
858 
859 	if (id->type != CX18_ENC_STREAM_TYPE_YUV)
860 		return -EINVAL;
861 
862 	return videobuf_streamoff(cx18_vb_queue(id));
863 }
864 
865 static int cx18_reqbufs(struct file *file, void *priv,
866 	struct v4l2_requestbuffers *rb)
867 {
868 	struct cx18_open_id *id = file->private_data;
869 	struct cx18 *cx = id->cx;
870 	struct cx18_stream *s = &cx->streams[id->type];
871 
872 	if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
873 		(s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
874 		return -EINVAL;
875 
876 	return videobuf_reqbufs(cx18_vb_queue(id), rb);
877 }
878 
879 static int cx18_querybuf(struct file *file, void *priv,
880 	struct v4l2_buffer *b)
881 {
882 	struct cx18_open_id *id = file->private_data;
883 	struct cx18 *cx = id->cx;
884 	struct cx18_stream *s = &cx->streams[id->type];
885 
886 	if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
887 		(s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
888 		return -EINVAL;
889 
890 	return videobuf_querybuf(cx18_vb_queue(id), b);
891 }
892 
893 static int cx18_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
894 {
895 	struct cx18_open_id *id = file->private_data;
896 	struct cx18 *cx = id->cx;
897 	struct cx18_stream *s = &cx->streams[id->type];
898 
899 	if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
900 		(s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
901 		return -EINVAL;
902 
903 	return videobuf_qbuf(cx18_vb_queue(id), b);
904 }
905 
906 static int cx18_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
907 {
908 	struct cx18_open_id *id = file->private_data;
909 	struct cx18 *cx = id->cx;
910 	struct cx18_stream *s = &cx->streams[id->type];
911 
912 	if ((s->vb_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
913 		(s->vb_type != V4L2_BUF_TYPE_VBI_CAPTURE))
914 		return -EINVAL;
915 
916 	return videobuf_dqbuf(cx18_vb_queue(id), b, file->f_flags & O_NONBLOCK);
917 }
918 
919 static int cx18_encoder_cmd(struct file *file, void *fh,
920 				struct v4l2_encoder_cmd *enc)
921 {
922 	struct cx18_open_id *id = fh2id(fh);
923 	struct cx18 *cx = id->cx;
924 	u32 h;
925 
926 	switch (enc->cmd) {
927 	case V4L2_ENC_CMD_START:
928 		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
929 		enc->flags = 0;
930 		return cx18_start_capture(id);
931 
932 	case V4L2_ENC_CMD_STOP:
933 		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
934 		enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
935 		cx18_stop_capture(id,
936 				  enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
937 		break;
938 
939 	case V4L2_ENC_CMD_PAUSE:
940 		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
941 		enc->flags = 0;
942 		if (!atomic_read(&cx->ana_capturing))
943 			return -EPERM;
944 		if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
945 			return 0;
946 		h = cx18_find_handle(cx);
947 		if (h == CX18_INVALID_TASK_HANDLE) {
948 			CX18_ERR("Can't find valid task handle for "
949 				 "V4L2_ENC_CMD_PAUSE\n");
950 			return -EBADFD;
951 		}
952 		cx18_mute(cx);
953 		cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, h);
954 		break;
955 
956 	case V4L2_ENC_CMD_RESUME:
957 		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
958 		enc->flags = 0;
959 		if (!atomic_read(&cx->ana_capturing))
960 			return -EPERM;
961 		if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
962 			return 0;
963 		h = cx18_find_handle(cx);
964 		if (h == CX18_INVALID_TASK_HANDLE) {
965 			CX18_ERR("Can't find valid task handle for "
966 				 "V4L2_ENC_CMD_RESUME\n");
967 			return -EBADFD;
968 		}
969 		cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, h);
970 		cx18_unmute(cx);
971 		break;
972 
973 	default:
974 		CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
975 		return -EINVAL;
976 	}
977 	return 0;
978 }
979 
980 static int cx18_try_encoder_cmd(struct file *file, void *fh,
981 				struct v4l2_encoder_cmd *enc)
982 {
983 	struct cx18 *cx = fh2id(fh)->cx;
984 
985 	switch (enc->cmd) {
986 	case V4L2_ENC_CMD_START:
987 		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
988 		enc->flags = 0;
989 		break;
990 
991 	case V4L2_ENC_CMD_STOP:
992 		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
993 		enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
994 		break;
995 
996 	case V4L2_ENC_CMD_PAUSE:
997 		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
998 		enc->flags = 0;
999 		break;
1000 
1001 	case V4L2_ENC_CMD_RESUME:
1002 		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
1003 		enc->flags = 0;
1004 		break;
1005 
1006 	default:
1007 		CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
1008 		return -EINVAL;
1009 	}
1010 	return 0;
1011 }
1012 
1013 static int cx18_log_status(struct file *file, void *fh)
1014 {
1015 	struct cx18 *cx = fh2id(fh)->cx;
1016 	struct v4l2_input vidin;
1017 	struct v4l2_audio audin;
1018 	int i;
1019 
1020 	CX18_INFO("Version: %s  Card: %s\n", CX18_VERSION, cx->card_name);
1021 	if (cx->hw_flags & CX18_HW_TVEEPROM) {
1022 		struct tveeprom tv;
1023 
1024 		cx18_read_eeprom(cx, &tv);
1025 	}
1026 	cx18_call_all(cx, core, log_status);
1027 	cx18_get_input(cx, cx->active_input, &vidin);
1028 	cx18_get_audio_input(cx, cx->audio_input, &audin);
1029 	CX18_INFO("Video Input: %s\n", vidin.name);
1030 	CX18_INFO("Audio Input: %s\n", audin.name);
1031 	mutex_lock(&cx->gpio_lock);
1032 	CX18_INFO("GPIO:  direction 0x%08x, value 0x%08x\n",
1033 		cx->gpio_dir, cx->gpio_val);
1034 	mutex_unlock(&cx->gpio_lock);
1035 	CX18_INFO("Tuner: %s\n",
1036 		test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ?  "Radio" : "TV");
1037 	v4l2_ctrl_handler_log_status(&cx->cxhdl.hdl, cx->v4l2_dev.name);
1038 	CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
1039 	for (i = 0; i < CX18_MAX_STREAMS; i++) {
1040 		struct cx18_stream *s = &cx->streams[i];
1041 
1042 		if (s->video_dev.v4l2_dev == NULL || s->buffers == 0)
1043 			continue;
1044 		CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
1045 			  s->name, s->s_flags,
1046 			  atomic_read(&s->q_full.depth) * s->bufs_per_mdl * 100
1047 			   / s->buffers,
1048 			  (s->buffers * s->buf_size) / 1024, s->buffers);
1049 	}
1050 	CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
1051 			(long long)cx->mpg_data_received,
1052 			(long long)cx->vbi_data_inserted);
1053 	return 0;
1054 }
1055 
1056 static long cx18_default(struct file *file, void *fh, bool valid_prio,
1057 			 unsigned int cmd, void *arg)
1058 {
1059 	struct cx18 *cx = fh2id(fh)->cx;
1060 
1061 	switch (cmd) {
1062 	case VIDIOC_INT_RESET: {
1063 		u32 val = *(u32 *)arg;
1064 
1065 		if ((val == 0) || (val & 0x01))
1066 			cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL, core, reset,
1067 				     (u32) CX18_GPIO_RESET_Z8F0811);
1068 		break;
1069 	}
1070 
1071 	default:
1072 		return -ENOTTY;
1073 	}
1074 	return 0;
1075 }
1076 
1077 static const struct v4l2_ioctl_ops cx18_ioctl_ops = {
1078 	.vidioc_querycap                = cx18_querycap,
1079 	.vidioc_s_audio                 = cx18_s_audio,
1080 	.vidioc_g_audio                 = cx18_g_audio,
1081 	.vidioc_enumaudio               = cx18_enumaudio,
1082 	.vidioc_enum_input              = cx18_enum_input,
1083 	.vidioc_cropcap                 = cx18_cropcap,
1084 	.vidioc_s_crop                  = cx18_s_crop,
1085 	.vidioc_g_crop                  = cx18_g_crop,
1086 	.vidioc_g_input                 = cx18_g_input,
1087 	.vidioc_s_input                 = cx18_s_input,
1088 	.vidioc_g_frequency             = cx18_g_frequency,
1089 	.vidioc_s_frequency             = cx18_s_frequency,
1090 	.vidioc_s_tuner                 = cx18_s_tuner,
1091 	.vidioc_g_tuner                 = cx18_g_tuner,
1092 	.vidioc_g_enc_index             = cx18_g_enc_index,
1093 	.vidioc_g_std                   = cx18_g_std,
1094 	.vidioc_s_std                   = cx18_s_std,
1095 	.vidioc_log_status              = cx18_log_status,
1096 	.vidioc_enum_fmt_vid_cap        = cx18_enum_fmt_vid_cap,
1097 	.vidioc_encoder_cmd             = cx18_encoder_cmd,
1098 	.vidioc_try_encoder_cmd         = cx18_try_encoder_cmd,
1099 	.vidioc_g_fmt_vid_cap           = cx18_g_fmt_vid_cap,
1100 	.vidioc_g_fmt_vbi_cap           = cx18_g_fmt_vbi_cap,
1101 	.vidioc_g_fmt_sliced_vbi_cap    = cx18_g_fmt_sliced_vbi_cap,
1102 	.vidioc_s_fmt_vid_cap           = cx18_s_fmt_vid_cap,
1103 	.vidioc_s_fmt_vbi_cap           = cx18_s_fmt_vbi_cap,
1104 	.vidioc_s_fmt_sliced_vbi_cap    = cx18_s_fmt_sliced_vbi_cap,
1105 	.vidioc_try_fmt_vid_cap         = cx18_try_fmt_vid_cap,
1106 	.vidioc_try_fmt_vbi_cap         = cx18_try_fmt_vbi_cap,
1107 	.vidioc_try_fmt_sliced_vbi_cap  = cx18_try_fmt_sliced_vbi_cap,
1108 	.vidioc_g_sliced_vbi_cap        = cx18_g_sliced_vbi_cap,
1109 #ifdef CONFIG_VIDEO_ADV_DEBUG
1110 	.vidioc_g_register              = cx18_g_register,
1111 	.vidioc_s_register              = cx18_s_register,
1112 #endif
1113 	.vidioc_default                 = cx18_default,
1114 	.vidioc_streamon                = cx18_streamon,
1115 	.vidioc_streamoff               = cx18_streamoff,
1116 	.vidioc_reqbufs                 = cx18_reqbufs,
1117 	.vidioc_querybuf                = cx18_querybuf,
1118 	.vidioc_qbuf                    = cx18_qbuf,
1119 	.vidioc_dqbuf                   = cx18_dqbuf,
1120 };
1121 
1122 void cx18_set_funcs(struct video_device *vdev)
1123 {
1124 	vdev->ioctl_ops = &cx18_ioctl_ops;
1125 }
1126