cx2341x.c 35.3 KB
Newer Older
1
/*
2
 * cx2341x - generic code for cx23415/6/8 based devices
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * Copyright (C) 2006 Hans Verkuil <hverkuil@xs4all.nl>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#include <linux/module.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/videodev2.h>

#include <media/tuner.h>
#include <media/cx2341x.h>
#include <media/v4l2-common.h>

33
MODULE_DESCRIPTION("cx23415/6/8 driver");
34 35 36
MODULE_AUTHOR("Hans Verkuil");
MODULE_LICENSE("GPL");

37
static int debug;
38 39 40
module_param(debug, int, 0644);
MODULE_PARM_DESC(debug, "Debug level (0-1)");

41
/* Must be sorted from low to high control ID! */
42 43 44
const u32 cx2341x_mpeg_ctrls[] = {
	V4L2_CID_MPEG_CLASS,
	V4L2_CID_MPEG_STREAM_TYPE,
45
	V4L2_CID_MPEG_STREAM_VBI_FMT,
46 47 48 49 50 51 52
	V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
	V4L2_CID_MPEG_AUDIO_ENCODING,
	V4L2_CID_MPEG_AUDIO_L2_BITRATE,
	V4L2_CID_MPEG_AUDIO_MODE,
	V4L2_CID_MPEG_AUDIO_MODE_EXTENSION,
	V4L2_CID_MPEG_AUDIO_EMPHASIS,
	V4L2_CID_MPEG_AUDIO_CRC,
53
	V4L2_CID_MPEG_AUDIO_MUTE,
54
	V4L2_CID_MPEG_AUDIO_AC3_BITRATE,
55 56 57 58 59 60 61 62 63
	V4L2_CID_MPEG_VIDEO_ENCODING,
	V4L2_CID_MPEG_VIDEO_ASPECT,
	V4L2_CID_MPEG_VIDEO_B_FRAMES,
	V4L2_CID_MPEG_VIDEO_GOP_SIZE,
	V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
	V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
	V4L2_CID_MPEG_VIDEO_BITRATE,
	V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
	V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION,
64 65
	V4L2_CID_MPEG_VIDEO_MUTE,
	V4L2_CID_MPEG_VIDEO_MUTE_YUV,
66 67 68 69 70 71 72 73 74 75 76
	V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE,
	V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER,
	V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE,
	V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE,
	V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE,
	V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER,
	V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE,
	V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM,
	V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP,
	V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM,
	V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP,
77
	V4L2_CID_MPEG_CX2341X_STREAM_INSERT_NAV_PACKETS,
78 79
	0
};
80
EXPORT_SYMBOL(cx2341x_mpeg_ctrls);
81

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
static const struct cx2341x_mpeg_params default_params = {
	/* misc */
	.capabilities = 0,
	.port = CX2341X_PORT_MEMORY,
	.width = 720,
	.height = 480,
	.is_50hz = 0,

	/* stream */
	.stream_type = V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
	.stream_vbi_fmt = V4L2_MPEG_STREAM_VBI_FMT_NONE,
	.stream_insert_nav_packets = 0,

	/* audio */
	.audio_sampling_freq = V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000,
	.audio_encoding = V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
	.audio_l2_bitrate = V4L2_MPEG_AUDIO_L2_BITRATE_224K,
99
	.audio_ac3_bitrate = V4L2_MPEG_AUDIO_AC3_BITRATE_224K,
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	.audio_mode = V4L2_MPEG_AUDIO_MODE_STEREO,
	.audio_mode_extension = V4L2_MPEG_AUDIO_MODE_EXTENSION_BOUND_4,
	.audio_emphasis = V4L2_MPEG_AUDIO_EMPHASIS_NONE,
	.audio_crc = V4L2_MPEG_AUDIO_CRC_NONE,
	.audio_mute = 0,

	/* video */
	.video_encoding = V4L2_MPEG_VIDEO_ENCODING_MPEG_2,
	.video_aspect = V4L2_MPEG_VIDEO_ASPECT_4x3,
	.video_b_frames = 2,
	.video_gop_size = 12,
	.video_gop_closure = 1,
	.video_bitrate_mode = V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
	.video_bitrate = 6000000,
	.video_bitrate_peak = 8000000,
	.video_temporal_decimation = 0,
	.video_mute = 0,
	.video_mute_yuv = 0x008080,  /* YCbCr value for black */

	/* encoding filters */
	.video_spatial_filter_mode =
		V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_MANUAL,
	.video_spatial_filter = 0,
	.video_luma_spatial_filter_type =
		V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_1D_HOR,
	.video_chroma_spatial_filter_type =
		V4L2_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE_1D_HOR,
	.video_temporal_filter_mode =
		V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_MANUAL,
	.video_temporal_filter = 8,
	.video_median_filter_type =
		V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF,
	.video_luma_median_filter_top = 255,
	.video_luma_median_filter_bottom = 0,
	.video_chroma_median_filter_top = 255,
	.video_chroma_median_filter_bottom = 0,
};

138 139 140

/* Map the control ID to the correct field in the cx2341x_mpeg_params
   struct. Return -EINVAL if the ID is unknown, else return 0. */
141
static int cx2341x_get_ctrl(const struct cx2341x_mpeg_params *params,
142 143 144 145 146 147 148 149 150 151 152 153
		struct v4l2_ext_control *ctrl)
{
	switch (ctrl->id) {
	case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
		ctrl->value = params->audio_sampling_freq;
		break;
	case V4L2_CID_MPEG_AUDIO_ENCODING:
		ctrl->value = params->audio_encoding;
		break;
	case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
		ctrl->value = params->audio_l2_bitrate;
		break;
154 155 156
	case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
		ctrl->value = params->audio_ac3_bitrate;
		break;
157 158 159 160 161 162 163 164 165 166 167 168
	case V4L2_CID_MPEG_AUDIO_MODE:
		ctrl->value = params->audio_mode;
		break;
	case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
		ctrl->value = params->audio_mode_extension;
		break;
	case V4L2_CID_MPEG_AUDIO_EMPHASIS:
		ctrl->value = params->audio_emphasis;
		break;
	case V4L2_CID_MPEG_AUDIO_CRC:
		ctrl->value = params->audio_crc;
		break;
169 170 171
	case V4L2_CID_MPEG_AUDIO_MUTE:
		ctrl->value = params->audio_mute;
		break;
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
	case V4L2_CID_MPEG_VIDEO_ENCODING:
		ctrl->value = params->video_encoding;
		break;
	case V4L2_CID_MPEG_VIDEO_ASPECT:
		ctrl->value = params->video_aspect;
		break;
	case V4L2_CID_MPEG_VIDEO_B_FRAMES:
		ctrl->value = params->video_b_frames;
		break;
	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
		ctrl->value = params->video_gop_size;
		break;
	case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
		ctrl->value = params->video_gop_closure;
		break;
	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
		ctrl->value = params->video_bitrate_mode;
		break;
	case V4L2_CID_MPEG_VIDEO_BITRATE:
		ctrl->value = params->video_bitrate;
		break;
	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
		ctrl->value = params->video_bitrate_peak;
		break;
	case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION:
		ctrl->value = params->video_temporal_decimation;
		break;
199 200 201 202 203 204
	case V4L2_CID_MPEG_VIDEO_MUTE:
		ctrl->value = params->video_mute;
		break;
	case V4L2_CID_MPEG_VIDEO_MUTE_YUV:
		ctrl->value = params->video_mute_yuv;
		break;
205 206 207
	case V4L2_CID_MPEG_STREAM_TYPE:
		ctrl->value = params->stream_type;
		break;
208 209 210
	case V4L2_CID_MPEG_STREAM_VBI_FMT:
		ctrl->value = params->stream_vbi_fmt;
		break;
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
	case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
		ctrl->value = params->video_spatial_filter_mode;
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER:
		ctrl->value = params->video_spatial_filter;
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE:
		ctrl->value = params->video_luma_spatial_filter_type;
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE:
		ctrl->value = params->video_chroma_spatial_filter_type;
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
		ctrl->value = params->video_temporal_filter_mode;
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER:
		ctrl->value = params->video_temporal_filter;
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
		ctrl->value = params->video_median_filter_type;
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP:
		ctrl->value = params->video_luma_median_filter_top;
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM:
		ctrl->value = params->video_luma_median_filter_bottom;
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP:
		ctrl->value = params->video_chroma_median_filter_top;
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM:
		ctrl->value = params->video_chroma_median_filter_bottom;
		break;
244 245 246
	case V4L2_CID_MPEG_CX2341X_STREAM_INSERT_NAV_PACKETS:
		ctrl->value = params->stream_insert_nav_packets;
		break;
247 248 249 250 251 252 253 254
	default:
		return -EINVAL;
	}
	return 0;
}

/* Map the control ID to the correct field in the cx2341x_mpeg_params
   struct. Return -EINVAL if the ID is unknown, else return 0. */
255
static int cx2341x_set_ctrl(struct cx2341x_mpeg_params *params, int busy,
256 257 258 259
		struct v4l2_ext_control *ctrl)
{
	switch (ctrl->id) {
	case V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ:
260 261
		if (busy)
			return -EBUSY;
262 263 264
		params->audio_sampling_freq = ctrl->value;
		break;
	case V4L2_CID_MPEG_AUDIO_ENCODING:
265 266
		if (busy)
			return -EBUSY;
267 268 269 270
		if (params->capabilities & CX2341X_CAP_HAS_AC3)
			if (ctrl->value != V4L2_MPEG_AUDIO_ENCODING_LAYER_2 &&
			    ctrl->value != V4L2_MPEG_AUDIO_ENCODING_AC3)
				return -ERANGE;
271 272 273
		params->audio_encoding = ctrl->value;
		break;
	case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
274 275
		if (busy)
			return -EBUSY;
276 277
		params->audio_l2_bitrate = ctrl->value;
		break;
278 279 280
	case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
		if (busy)
			return -EBUSY;
281 282
		if (!(params->capabilities & CX2341X_CAP_HAS_AC3))
			return -EINVAL;
283 284
		params->audio_ac3_bitrate = ctrl->value;
		break;
285 286 287 288 289 290 291 292 293 294 295 296
	case V4L2_CID_MPEG_AUDIO_MODE:
		params->audio_mode = ctrl->value;
		break;
	case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
		params->audio_mode_extension = ctrl->value;
		break;
	case V4L2_CID_MPEG_AUDIO_EMPHASIS:
		params->audio_emphasis = ctrl->value;
		break;
	case V4L2_CID_MPEG_AUDIO_CRC:
		params->audio_crc = ctrl->value;
		break;
297 298 299
	case V4L2_CID_MPEG_AUDIO_MUTE:
		params->audio_mute = ctrl->value;
		break;
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
	case V4L2_CID_MPEG_VIDEO_ASPECT:
		params->video_aspect = ctrl->value;
		break;
	case V4L2_CID_MPEG_VIDEO_B_FRAMES: {
		int b = ctrl->value + 1;
		int gop = params->video_gop_size;
		params->video_b_frames = ctrl->value;
		params->video_gop_size = b * ((gop + b - 1) / b);
		/* Max GOP size = 34 */
		while (params->video_gop_size > 34)
			params->video_gop_size -= b;
		break;
	}
	case V4L2_CID_MPEG_VIDEO_GOP_SIZE: {
		int b = params->video_b_frames + 1;
		int gop = ctrl->value;
		params->video_gop_size = b * ((gop + b - 1) / b);
		/* Max GOP size = 34 */
		while (params->video_gop_size > 34)
			params->video_gop_size -= b;
		ctrl->value = params->video_gop_size;
		break;
	}
	case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
		params->video_gop_closure = ctrl->value;
		break;
	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
327 328
		if (busy)
			return -EBUSY;
329 330 331 332 333 334 335
		/* MPEG-1 only allows CBR */
		if (params->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1 &&
		    ctrl->value != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
			return -EINVAL;
		params->video_bitrate_mode = ctrl->value;
		break;
	case V4L2_CID_MPEG_VIDEO_BITRATE:
336 337
		if (busy)
			return -EBUSY;
338 339 340
		params->video_bitrate = ctrl->value;
		break;
	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
341 342
		if (busy)
			return -EBUSY;
343 344 345 346 347
		params->video_bitrate_peak = ctrl->value;
		break;
	case V4L2_CID_MPEG_VIDEO_TEMPORAL_DECIMATION:
		params->video_temporal_decimation = ctrl->value;
		break;
348 349 350 351 352 353
	case V4L2_CID_MPEG_VIDEO_MUTE:
		params->video_mute = (ctrl->value != 0);
		break;
	case V4L2_CID_MPEG_VIDEO_MUTE_YUV:
		params->video_mute_yuv = ctrl->value;
		break;
354
	case V4L2_CID_MPEG_STREAM_TYPE:
355 356
		if (busy)
			return -EBUSY;
357 358
		params->stream_type = ctrl->value;
		params->video_encoding =
359 360 361 362 363
		    (params->stream_type == V4L2_MPEG_STREAM_TYPE_MPEG1_SS ||
		     params->stream_type == V4L2_MPEG_STREAM_TYPE_MPEG1_VCD) ?
			V4L2_MPEG_VIDEO_ENCODING_MPEG_1 :
			V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
		if (params->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
364
			/* MPEG-1 implies CBR */
365 366
			params->video_bitrate_mode =
				V4L2_MPEG_VIDEO_BITRATE_MODE_CBR;
367
		break;
368 369 370
	case V4L2_CID_MPEG_STREAM_VBI_FMT:
		params->stream_vbi_fmt = ctrl->value;
		break;
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
	case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
		params->video_spatial_filter_mode = ctrl->value;
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER:
		params->video_spatial_filter = ctrl->value;
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE:
		params->video_luma_spatial_filter_type = ctrl->value;
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE:
		params->video_chroma_spatial_filter_type = ctrl->value;
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
		params->video_temporal_filter_mode = ctrl->value;
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER:
		params->video_temporal_filter = ctrl->value;
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
		params->video_median_filter_type = ctrl->value;
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP:
		params->video_luma_median_filter_top = ctrl->value;
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM:
		params->video_luma_median_filter_bottom = ctrl->value;
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP:
		params->video_chroma_median_filter_top = ctrl->value;
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM:
		params->video_chroma_median_filter_bottom = ctrl->value;
		break;
404 405 406
	case V4L2_CID_MPEG_CX2341X_STREAM_INSERT_NAV_PACKETS:
		params->stream_insert_nav_packets = ctrl->value;
		break;
407 408 409 410 411 412
	default:
		return -EINVAL;
	}
	return 0;
}

413 414
static int cx2341x_ctrl_query_fill(struct v4l2_queryctrl *qctrl,
				   s32 min, s32 max, s32 step, s32 def)
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
{
	const char *name;

	qctrl->flags = 0;
	switch (qctrl->id) {
	/* MPEG controls */
	case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
		name = "Spatial Filter Mode";
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER:
		name = "Spatial Filter";
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE:
		name = "Spatial Luma Filter Type";
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE:
		name = "Spatial Chroma Filter Type";
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
		name = "Temporal Filter Mode";
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER:
		name = "Temporal Filter";
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
		name = "Median Filter Type";
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP:
		name = "Median Luma Filter Maximum";
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM:
		name = "Median Luma Filter Minimum";
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP:
		name = "Median Chroma Filter Maximum";
		break;
	case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM:
		name = "Median Chroma Filter Minimum";
		break;
454 455 456
	case V4L2_CID_MPEG_CX2341X_STREAM_INSERT_NAV_PACKETS:
		name = "Insert Navigation Packets";
		break;
457 458 459 460 461 462 463 464 465 466 467 468 469 470

	default:
		return v4l2_ctrl_query_fill(qctrl, min, max, step, def);
	}
	switch (qctrl->id) {
	case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
	case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE:
	case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE:
	case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
	case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
		qctrl->type = V4L2_CTRL_TYPE_MENU;
		min = 0;
		step = 1;
		break;
471 472 473 474 475 476
	case V4L2_CID_MPEG_CX2341X_STREAM_INSERT_NAV_PACKETS:
		qctrl->type = V4L2_CTRL_TYPE_BOOLEAN;
		min = 0;
		max = 1;
		step = 1;
		break;
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
	default:
		qctrl->type = V4L2_CTRL_TYPE_INTEGER;
		break;
	}
	switch (qctrl->id) {
	case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
	case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
	case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
		qctrl->flags |= V4L2_CTRL_FLAG_UPDATE;
		break;
	}
	qctrl->minimum = min;
	qctrl->maximum = max;
	qctrl->step = step;
	qctrl->default_value = def;
	qctrl->reserved[0] = qctrl->reserved[1] = 0;
	snprintf(qctrl->name, sizeof(qctrl->name), name);
	return 0;
}

497
int cx2341x_ctrl_query(const struct cx2341x_mpeg_params *params,
498
		       struct v4l2_queryctrl *qctrl)
499 500 501 502 503
{
	int err;

	switch (qctrl->id) {
	case V4L2_CID_MPEG_AUDIO_ENCODING:
504 505 506 507 508 509 510
		if (params->capabilities & CX2341X_CAP_HAS_AC3) {
			/*
			 * The state of L2 & AC3 bitrate controls can change
			 * when this control changes, but v4l2_ctrl_query_fill()
			 * already sets V4L2_CTRL_FLAG_UPDATE for
			 * V4L2_CID_MPEG_AUDIO_ENCODING, so we don't here.
			 */
511 512 513 514
			return v4l2_ctrl_query_fill(qctrl,
					V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
					V4L2_MPEG_AUDIO_ENCODING_AC3, 1,
					default_params.audio_encoding);
515
		}
516

517 518 519
		return v4l2_ctrl_query_fill(qctrl,
				V4L2_MPEG_AUDIO_ENCODING_LAYER_2,
				V4L2_MPEG_AUDIO_ENCODING_LAYER_2, 1,
520
				default_params.audio_encoding);
521 522

	case V4L2_CID_MPEG_AUDIO_L2_BITRATE:
523
		err = v4l2_ctrl_query_fill(qctrl,
524 525
				V4L2_MPEG_AUDIO_L2_BITRATE_192K,
				V4L2_MPEG_AUDIO_L2_BITRATE_384K, 1,
526
				default_params.audio_l2_bitrate);
527 528 529 530 531 532
		if (err)
			return err;
		if (params->capabilities & CX2341X_CAP_HAS_AC3 &&
		    params->audio_encoding != V4L2_MPEG_AUDIO_ENCODING_LAYER_2)
			qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
		return 0;
533 534 535 536 537

	case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
	case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
		return -EINVAL;

538
	case V4L2_CID_MPEG_AUDIO_AC3_BITRATE:
539
		err = v4l2_ctrl_query_fill(qctrl,
540 541 542
				V4L2_MPEG_AUDIO_AC3_BITRATE_48K,
				V4L2_MPEG_AUDIO_AC3_BITRATE_448K, 1,
				default_params.audio_ac3_bitrate);
543 544 545 546 547 548 549 550 551
		if (err)
			return err;
		if (params->capabilities & CX2341X_CAP_HAS_AC3) {
			if (params->audio_encoding !=
						   V4L2_MPEG_AUDIO_ENCODING_AC3)
				qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
		} else
			qctrl->flags |= V4L2_CTRL_FLAG_DISABLED;
		return 0;
552

553 554
	case V4L2_CID_MPEG_AUDIO_MODE_EXTENSION:
		err = v4l2_ctrl_query_fill_std(qctrl);
555 556
		if (err == 0 &&
		    params->audio_mode != V4L2_MPEG_AUDIO_MODE_JOINT_STEREO)
557 558 559 560 561 562 563
			qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
		return err;

	case V4L2_CID_MPEG_VIDEO_ENCODING:
		/* this setting is read-only for the cx2341x since the
		   V4L2_CID_MPEG_STREAM_TYPE really determines the
		   MPEG-1/2 setting */
564 565 566 567
		err = v4l2_ctrl_query_fill(qctrl,
					   V4L2_MPEG_VIDEO_ENCODING_MPEG_1,
					   V4L2_MPEG_VIDEO_ENCODING_MPEG_2, 1,
					   V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
568 569 570 571
		if (err == 0)
			qctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY;
		return err;

572 573
	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
		err = v4l2_ctrl_query_fill_std(qctrl);
574 575
		if (err == 0 &&
		    params->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1)
576 577 578
			qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
		return err;

579 580
	case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
		err = v4l2_ctrl_query_fill_std(qctrl);
581 582 583
		if (err == 0 &&
		    params->video_bitrate_mode ==
				V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
584 585 586
			qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
		return err;

587 588 589 590 591 592
	case V4L2_CID_MPEG_STREAM_VBI_FMT:
		if (params->capabilities & CX2341X_CAP_HAS_SLICED_VBI)
			return v4l2_ctrl_query_fill_std(qctrl);
		return cx2341x_ctrl_query_fill(qctrl,
				V4L2_MPEG_STREAM_VBI_FMT_NONE,
				V4L2_MPEG_STREAM_VBI_FMT_NONE, 1,
593 594 595 596 597
				default_params.stream_vbi_fmt);

	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
		return v4l2_ctrl_query_fill(qctrl, 1, 34, 1,
				params->is_50hz ? 12 : 15);
598

599 600 601
	/* CX23415/6 specific */
	case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
		return cx2341x_ctrl_query_fill(qctrl,
602 603
			V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_MANUAL,
			V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_AUTO, 1,
604
			default_params.video_spatial_filter_mode);
605 606

	case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER:
607 608
		cx2341x_ctrl_query_fill(qctrl, 0, 15, 1,
				default_params.video_spatial_filter);
609
		qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
610 611 612
		if (params->video_spatial_filter_mode ==
			    V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_AUTO)
			qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
613 614 615 616
		return 0;

	case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE:
		cx2341x_ctrl_query_fill(qctrl,
617 618 619
			V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_OFF,
			V4L2_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE_2D_SYM_NON_SEPARABLE,
			1,
620
			default_params.video_luma_spatial_filter_type);
621 622 623
		if (params->video_spatial_filter_mode ==
			    V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_AUTO)
			qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
624 625 626 627
		return 0;

	case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE:
		cx2341x_ctrl_query_fill(qctrl,
628 629 630
		    V4L2_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE_OFF,
		    V4L2_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE_1D_HOR,
		    1,
631
		    default_params.video_chroma_spatial_filter_type);
632 633 634
		if (params->video_spatial_filter_mode ==
			V4L2_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE_AUTO)
			qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
635 636 637 638
		return 0;

	case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
		return cx2341x_ctrl_query_fill(qctrl,
639 640
			V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_MANUAL,
			V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_AUTO, 1,
641
			default_params.video_temporal_filter_mode);
642 643

	case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER:
644 645
		cx2341x_ctrl_query_fill(qctrl, 0, 31, 1,
				default_params.video_temporal_filter);
646
		qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
647 648 649
		if (params->video_temporal_filter_mode ==
			V4L2_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE_AUTO)
			qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
650 651 652 653
		return 0;

	case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
		return cx2341x_ctrl_query_fill(qctrl,
654 655
			V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF,
			V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_DIAG, 1,
656
			default_params.video_median_filter_type);
657 658

	case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_TOP:
659 660
		cx2341x_ctrl_query_fill(qctrl, 0, 255, 1,
				default_params.video_luma_median_filter_top);
661
		qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
662 663 664
		if (params->video_median_filter_type ==
				V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF)
			qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
665 666 667
		return 0;

	case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_MEDIAN_FILTER_BOTTOM:
668 669
		cx2341x_ctrl_query_fill(qctrl, 0, 255, 1,
				default_params.video_luma_median_filter_bottom);
670
		qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
671 672 673
		if (params->video_median_filter_type ==
				V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF)
			qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
674 675 676
		return 0;

	case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_TOP:
677 678
		cx2341x_ctrl_query_fill(qctrl, 0, 255, 1,
				default_params.video_chroma_median_filter_top);
679
		qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
680 681 682
		if (params->video_median_filter_type ==
				V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF)
			qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
683 684 685
		return 0;

	case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_MEDIAN_FILTER_BOTTOM:
686 687
		cx2341x_ctrl_query_fill(qctrl, 0, 255, 1,
			default_params.video_chroma_median_filter_bottom);
688
		qctrl->flags |= V4L2_CTRL_FLAG_SLIDER;
689 690 691
		if (params->video_median_filter_type ==
				V4L2_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE_OFF)
			qctrl->flags |= V4L2_CTRL_FLAG_INACTIVE;
692 693
		return 0;

694
	case V4L2_CID_MPEG_CX2341X_STREAM_INSERT_NAV_PACKETS:
695 696
		return cx2341x_ctrl_query_fill(qctrl, 0, 1, 1,
				default_params.stream_insert_nav_packets);
697

698 699 700 701 702
	default:
		return v4l2_ctrl_query_fill_std(qctrl);

	}
}
703
EXPORT_SYMBOL(cx2341x_ctrl_query);
704

705
const char **cx2341x_ctrl_get_menu(const struct cx2341x_mpeg_params *p, u32 id)
706
{
707
	static const char *mpeg_stream_type_without_ts[] = {
708 709 710 711 712 713 714 715 716
		"MPEG-2 Program Stream",
		"",
		"MPEG-1 System Stream",
		"MPEG-2 DVD-compatible Stream",
		"MPEG-1 VCD-compatible Stream",
		"MPEG-2 SVCD-compatible Stream",
		NULL
	};

717 718 719 720 721 722 723 724 725 726
	static const char *mpeg_stream_type_with_ts[] = {
		"MPEG-2 Program Stream",
		"MPEG-2 Transport Stream",
		"MPEG-1 System Stream",
		"MPEG-2 DVD-compatible Stream",
		"MPEG-1 VCD-compatible Stream",
		"MPEG-2 SVCD-compatible Stream",
		NULL
	};

727 728 729 730 731 732 733 734 735
	static const char *mpeg_audio_encoding_l2_ac3[] = {
		"",
		"MPEG-1/2 Layer II",
		"",
		"",
		"AC-3",
		NULL
	};

736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
	static const char *cx2341x_video_spatial_filter_mode_menu[] = {
		"Manual",
		"Auto",
		NULL
	};

	static const char *cx2341x_video_luma_spatial_filter_type_menu[] = {
		"Off",
		"1D Horizontal",
		"1D Vertical",
		"2D H/V Separable",
		"2D Symmetric non-separable",
		NULL
	};

	static const char *cx2341x_video_chroma_spatial_filter_type_menu[] = {
		"Off",
		"1D Horizontal",
		NULL
	};

	static const char *cx2341x_video_temporal_filter_mode_menu[] = {
		"Manual",
		"Auto",
		NULL
	};

	static const char *cx2341x_video_median_filter_type_menu[] = {
		"Off",
		"Horizontal",
		"Vertical",
		"Horizontal/Vertical",
		"Diagonal",
		NULL
	};

	switch (id) {
	case V4L2_CID_MPEG_STREAM_TYPE:
774 775
		return (p->capabilities & CX2341X_CAP_HAS_TS) ?
			mpeg_stream_type_with_ts : mpeg_stream_type_without_ts;
776 777 778
	case V4L2_CID_MPEG_AUDIO_ENCODING:
		return (p->capabilities & CX2341X_CAP_HAS_AC3) ?
			mpeg_audio_encoding_l2_ac3 : v4l2_ctrl_get_menu(id);
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
	case V4L2_CID_MPEG_AUDIO_L1_BITRATE:
	case V4L2_CID_MPEG_AUDIO_L3_BITRATE:
		return NULL;
	case V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE:
		return cx2341x_video_spatial_filter_mode_menu;
	case V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE:
		return cx2341x_video_luma_spatial_filter_type_menu;
	case V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE:
		return cx2341x_video_chroma_spatial_filter_type_menu;
	case V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE:
		return cx2341x_video_temporal_filter_mode_menu;
	case V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE:
		return cx2341x_video_median_filter_type_menu;
	default:
		return v4l2_ctrl_get_menu(id);
	}
}
796
EXPORT_SYMBOL(cx2341x_ctrl_get_menu);
797

798
/* definitions for audio properties bits 29-28 */
799 800 801
#define CX2341X_AUDIO_ENCODING_METHOD_MPEG	0
#define CX2341X_AUDIO_ENCODING_METHOD_AC3	1
#define CX2341X_AUDIO_ENCODING_METHOD_LPCM	2
802

803 804
static void cx2341x_calc_audio_properties(struct cx2341x_mpeg_params *params)
{
805 806
	params->audio_properties =
		(params->audio_sampling_freq << 0) |
807 808
		(params->audio_mode << 8) |
		(params->audio_mode_extension << 10) |
809 810
		(((params->audio_emphasis == V4L2_MPEG_AUDIO_EMPHASIS_CCITT_J17)
		  ? 3 : params->audio_emphasis) << 12) |
811
		(params->audio_crc << 14);
812 813 814 815 816 817 818

	if ((params->capabilities & CX2341X_CAP_HAS_AC3) &&
	    params->audio_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3) {
		params->audio_properties |=
			/* Not sure if this MPEG Layer II setting is required */
			((3 - V4L2_MPEG_AUDIO_ENCODING_LAYER_2) << 2) |
			(params->audio_ac3_bitrate << 4) |
819
			(CX2341X_AUDIO_ENCODING_METHOD_AC3 << 28);
820 821 822 823 824 825
	} else {
		/* Assuming MPEG Layer II */
		params->audio_properties |=
			((3 - params->audio_encoding) << 2) |
			((1 + params->audio_l2_bitrate) << 4);
	}
826 827
}

828
int cx2341x_ext_ctrls(struct cx2341x_mpeg_params *params, int busy,
829
		  struct v4l2_ext_controls *ctrls, unsigned int cmd)
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
{
	int err = 0;
	int i;

	if (cmd == VIDIOC_G_EXT_CTRLS) {
		for (i = 0; i < ctrls->count; i++) {
			struct v4l2_ext_control *ctrl = ctrls->controls + i;

			err = cx2341x_get_ctrl(params, ctrl);
			if (err) {
				ctrls->error_idx = i;
				break;
			}
		}
		return err;
	}
	for (i = 0; i < ctrls->count; i++) {
		struct v4l2_ext_control *ctrl = ctrls->controls + i;
		struct v4l2_queryctrl qctrl;
		const char **menu_items = NULL;

		qctrl.id = ctrl->id;
		err = cx2341x_ctrl_query(params, &qctrl);
		if (err)
			break;
		if (qctrl.type == V4L2_CTRL_TYPE_MENU)
856
			menu_items = cx2341x_ctrl_get_menu(params, qctrl.id);
857 858 859
		err = v4l2_ctrl_check(ctrl, &qctrl, menu_items);
		if (err)
			break;
860
		err = cx2341x_set_ctrl(params, busy, ctrl);
861 862 863
		if (err)
			break;
	}
864 865 866
	if (err == 0 &&
	    params->video_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
	    params->video_bitrate_peak < params->video_bitrate) {
867 868 869
		err = -ERANGE;
		ctrls->error_idx = ctrls->count;
	}
870
	if (err)
871
		ctrls->error_idx = i;
872
	else
873 874 875
		cx2341x_calc_audio_properties(params);
	return err;
}
876
EXPORT_SYMBOL(cx2341x_ext_ctrls);
877 878 879 880 881 882

void cx2341x_fill_defaults(struct cx2341x_mpeg_params *p)
{
	*p = default_params;
	cx2341x_calc_audio_properties(p);
}
883
EXPORT_SYMBOL(cx2341x_fill_defaults);
884

885
static int cx2341x_api(void *priv, cx2341x_mbox_func func,
886
		       u32 cmd, int args, ...)
887 888 889 890 891 892 893
{
	u32 data[CX2341X_MBOX_MAX_DATA];
	va_list vargs;
	int i;

	va_start(vargs, args);

894
	for (i = 0; i < args; i++)
895 896 897 898 899
		data[i] = va_arg(vargs, int);
	va_end(vargs);
	return func(priv, cmd, args, 0, data);
}

900 901
#define NEQ(field) (old->field != new->field)

902
int cx2341x_update(void *priv, cx2341x_mbox_func func,
903 904
		   const struct cx2341x_mpeg_params *old,
		   const struct cx2341x_mpeg_params *new)
905 906 907 908 909 910 911 912 913 914 915
{
	static int mpeg_stream_type[] = {
		0,	/* MPEG-2 PS */
		1,	/* MPEG-2 TS */
		2,	/* MPEG-1 SS */
		14,	/* DVD */
		11,	/* VCD */
		12,	/* SVCD */
	};

	int err = 0;
916
	int force = (old == NULL);
917
	u16 temporal = new->video_temporal_filter;
918

919
	cx2341x_api(priv, func, CX2341X_ENC_SET_OUTPUT_PORT, 2, new->port, 0);
920

921 922 923
	if (force || NEQ(is_50hz)) {
		err = cx2341x_api(priv, func, CX2341X_ENC_SET_FRAME_RATE, 1,
				  new->is_50hz);
924 925 926
		if (err) return err;
	}

927
	if (force || NEQ(width) || NEQ(height) || NEQ(video_encoding)) {
928 929 930 931 932 933 934
		u16 w = new->width;
		u16 h = new->height;

		if (new->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1) {
			w /= 2;
			h /= 2;
		}
935 936
		err = cx2341x_api(priv, func, CX2341X_ENC_SET_FRAME_SIZE, 2,
				  h, w);
937
		if (err) return err;
938
	}
939

940
	if (new->width != 720 || new->height != (new->is_50hz ? 576 : 480)) {
941 942 943 944 945
		/* Adjust temporal filter if necessary. The problem with the
		   temporal filter is that it works well with full resolution
		   capturing, but not when the capture window is scaled (the
		   filter introduces a ghosting effect). So if the capture
		   window is scaled, then force the filter to 0.
946

947
		   For full resolution the filter really improves the video
948 949
		   quality, especially if the original video quality is
		   suboptimal. */
950
		temporal = 0;
951 952
	}

953 954 955
	if (force || NEQ(stream_type)) {
		err = cx2341x_api(priv, func, CX2341X_ENC_SET_STREAM_TYPE, 1,
				  mpeg_stream_type[new->stream_type]);
956 957
		if (err) return err;
	}
958 959 960
	if (force || NEQ(video_aspect)) {
		err = cx2341x_api(priv, func, CX2341X_ENC_SET_ASPECT_RATIO, 1,
				  1 + new->video_aspect);
961 962
		if (err) return err;
	}
963
	if (force || NEQ(video_b_frames) || NEQ(video_gop_size)) {
964 965 966 967
		err = cx2341x_api(priv, func, CX2341X_ENC_SET_GOP_PROPERTIES, 2,
				new->video_gop_size, new->video_b_frames + 1);
		if (err) return err;
	}
968 969 970
	if (force || NEQ(video_gop_closure)) {
		err = cx2341x_api(priv, func, CX2341X_ENC_SET_GOP_CLOSURE, 1,
				  new->video_gop_closure);
971 972
		if (err) return err;
	}
973 974 975
	if (force || NEQ(audio_properties)) {
		err = cx2341x_api(priv, func, CX2341X_ENC_SET_AUDIO_PROPERTIES,
				  1, new->audio_properties);
976 977
		if (err) return err;
	}
978 979 980
	if (force || NEQ(audio_mute)) {
		err = cx2341x_api(priv, func, CX2341X_ENC_MUTE_AUDIO, 1,
				  new->audio_mute);
981 982
		if (err) return err;
	}
983 984
	if (force || NEQ(video_bitrate_mode) || NEQ(video_bitrate) ||
						NEQ(video_bitrate_peak)) {
985 986 987 988 989
		err = cx2341x_api(priv, func, CX2341X_ENC_SET_BIT_RATE, 5,
				new->video_bitrate_mode, new->video_bitrate,
				new->video_bitrate_peak / 400, 0, 0);
		if (err) return err;
	}
990 991 992 993 994 995
	if (force || NEQ(video_spatial_filter_mode) ||
		     NEQ(video_temporal_filter_mode) ||
		     NEQ(video_median_filter_type)) {
		err = cx2341x_api(priv, func, CX2341X_ENC_SET_DNR_FILTER_MODE,
				  2, new->video_spatial_filter_mode |
					(new->video_temporal_filter_mode << 1),
996 997 998
				new->video_median_filter_type);
		if (err) return err;
	}
999 1000 1001 1002
	if (force || NEQ(video_luma_median_filter_bottom) ||
		     NEQ(video_luma_median_filter_top) ||
		     NEQ(video_chroma_median_filter_bottom) ||
		     NEQ(video_chroma_median_filter_top)) {
1003 1004 1005 1006 1007 1008 1009
		err = cx2341x_api(priv, func, CX2341X_ENC_SET_CORING_LEVELS, 4,
				new->video_luma_median_filter_bottom,
				new->video_luma_median_filter_top,
				new->video_chroma_median_filter_bottom,
				new->video_chroma_median_filter_top);
		if (err) return err;
	}
1010 1011 1012 1013 1014 1015
	if (force || NEQ(video_luma_spatial_filter_type) ||
		     NEQ(video_chroma_spatial_filter_type)) {
		err = cx2341x_api(priv, func,
				  CX2341X_ENC_SET_SPATIAL_FILTER_TYPE,
				  2, new->video_luma_spatial_filter_type,
				  new->video_chroma_spatial_filter_type);
1016 1017
		if (err) return err;
	}
1018 1019 1020 1021
	if (force || NEQ(video_spatial_filter) ||
		     old->video_temporal_filter != temporal) {
		err = cx2341x_api(priv, func, CX2341X_ENC_SET_DNR_FILTER_PROPS,
				  2, new->video_spatial_filter, temporal);
1022 1023
		if (err) return err;
	}
1024 1025 1026
	if (force || NEQ(video_temporal_decimation)) {
		err = cx2341x_api(priv, func, CX2341X_ENC_SET_FRAME_DROP_RATE,
				  1, new->video_temporal_decimation);
1027 1028
		if (err) return err;
	}
1029 1030 1031 1032
	if (force || NEQ(video_mute) ||
		(new->video_mute && NEQ(video_mute_yuv))) {
		err = cx2341x_api(priv, func, CX2341X_ENC_MUTE_VIDEO, 1,
				new->video_mute | (new->video_mute_yuv << 8));
1033 1034
		if (err) return err;
	}
1035 1036 1037
	if (force || NEQ(stream_insert_nav_packets)) {
		err = cx2341x_api(priv, func, CX2341X_ENC_MISC, 2,
				7, new->stream_insert_nav_packets);
1038 1039
		if (err) return err;
	}
1040 1041
	return 0;
}
1042
EXPORT_SYMBOL(cx2341x_update);
1043

1044
static const char *cx2341x_menu_item(const struct cx2341x_mpeg_params *p, u32 id)
1045
{
1046
	const char **menu = cx2341x_ctrl_get_menu(p, id);
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
	struct v4l2_ext_control ctrl;

	if (menu == NULL)
		goto invalid;
	ctrl.id = id;
	if (cx2341x_get_ctrl(p, &ctrl))
		goto invalid;
	while (ctrl.value-- && *menu) menu++;
	if (*menu == NULL)
		goto invalid;
	return *menu;

invalid:
	return "<invalid>";
}

1063
void cx2341x_log_status(const struct cx2341x_mpeg_params *p, const char *prefix)
1064 1065
{
	int is_mpeg1 = p->video_encoding == V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
1066
	int temporal = p->video_temporal_filter;
1067 1068

	/* Stream */
1069
	printk(KERN_INFO "%s: Stream: %s",
1070
		prefix,
1071
		cx2341x_menu_item(p, V4L2_CID_MPEG_STREAM_TYPE));
1072 1073 1074
	if (p->stream_insert_nav_packets)
		printk(" (with navigation packets)");
	printk("\n");
1075 1076 1077
	printk(KERN_INFO "%s: VBI Format: %s\n",
		prefix,
		cx2341x_menu_item(p, V4L2_CID_MPEG_STREAM_VBI_FMT));
1078 1079

	/* Video */
1080
	printk(KERN_INFO "%s: Video:  %dx%d, %d fps%s\n",
1081
		prefix,
1082
		p->width / (is_mpeg1 ? 2 : 1), p->height / (is_mpeg1 ? 2 : 1),
1083 1084
		p->is_50hz ? 25 : 30,
		(p->video_mute) ? " (muted)" : "");
1085 1086
	printk(KERN_INFO "%s: Video:  %s, %s, %s, %d",
		prefix,
1087 1088 1089 1090
		cx2341x_menu_item(p, V4L2_CID_MPEG_VIDEO_ENCODING),
		cx2341x_menu_item(p, V4L2_CID_MPEG_VIDEO_ASPECT),
		cx2341x_menu_item(p, V4L2_CID_MPEG_VIDEO_BITRATE_MODE),
		p->video_bitrate);
1091
	if (p->video_bitrate_mode == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
1092 1093
		printk(", Peak %d", p->video_bitrate_peak);
	printk("\n");
1094 1095
	printk(KERN_INFO
		"%s: Video:  GOP Size %d, %d B-Frames, %sGOP Closure\n",
1096
		prefix,
1097
		p->video_gop_size, p->video_b_frames,
1098
		p->video_gop_closure ? "" : "No ");
1099
	if (p->video_temporal_decimation)
1100 1101
		printk(KERN_INFO "%s: Video: Temporal Decimation %d\n",
			prefix, p->video_temporal_decimation);
1102 1103

	/* Audio */
1104
	printk(KERN_INFO "%s: Audio:  %s, %s, %s, %s%s",
1105
		prefix,
1106 1107
		cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ),
		cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_ENCODING),
1108 1109 1110 1111
		cx2341x_menu_item(p,
			   p->audio_encoding == V4L2_MPEG_AUDIO_ENCODING_AC3
					      ? V4L2_CID_MPEG_AUDIO_AC3_BITRATE
					      : V4L2_CID_MPEG_AUDIO_L2_BITRATE),
1112 1113
		cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_MODE),
		p->audio_mute ? " (muted)" : "");
1114 1115 1116
	if (p->audio_mode == V4L2_MPEG_AUDIO_MODE_JOINT_STEREO)
		printk(", %s", cx2341x_menu_item(p,
				V4L2_CID_MPEG_AUDIO_MODE_EXTENSION));
1117 1118 1119 1120 1121
	printk(", %s, %s\n",
		cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_EMPHASIS),
		cx2341x_menu_item(p, V4L2_CID_MPEG_AUDIO_CRC));

	/* Encoding filters */
1122 1123
	printk(KERN_INFO "%s: Spatial Filter:  %s, Luma %s, Chroma %s, %d\n",
		prefix,
1124 1125 1126 1127 1128 1129
		cx2341x_menu_item(p,
		    V4L2_CID_MPEG_CX2341X_VIDEO_SPATIAL_FILTER_MODE),
		cx2341x_menu_item(p,
		    V4L2_CID_MPEG_CX2341X_VIDEO_LUMA_SPATIAL_FILTER_TYPE),
		cx2341x_menu_item(p,
		    V4L2_CID_MPEG_CX2341X_VIDEO_CHROMA_SPATIAL_FILTER_TYPE),
1130
		p->video_spatial_filter);
1131 1132

	if (p->width != 720 || p->height != (p->is_50hz ? 576 : 480))
1133
		temporal = 0;
1134

1135 1136
	printk(KERN_INFO "%s: Temporal Filter: %s, %d\n",
		prefix,
1137 1138
		cx2341x_menu_item(p,
			V4L2_CID_MPEG_CX2341X_VIDEO_TEMPORAL_FILTER_MODE),
1139
		temporal);
1140 1141
	printk(KERN_INFO
		"%s: Median Filter:   %s, Luma [%d, %d], Chroma [%d, %d]\n",
1142
		prefix,
1143 1144
		cx2341x_menu_item(p,
			V4L2_CID_MPEG_CX2341X_VIDEO_MEDIAN_FILTER_TYPE),
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
		p->video_luma_median_filter_bottom,
		p->video_luma_median_filter_top,
		p->video_chroma_median_filter_bottom,
		p->video_chroma_median_filter_top);
}
EXPORT_SYMBOL(cx2341x_log_status);

/*
 * Local variables:
 * c-basic-offset: 8
 * End:
 */