V4L/DVB (8082): cx18: convert to video_ioctl2()

cx18: convert driver to use video_ioctl2().  Pushed down ioctl debug
messages and priority checks as well.  Still left serialization lock in
place for now.  #if 0'ed out sliced vbi ioctl code for now.

Patch heavily based on similar changes made to ivtv by Hans Verkuil.

Signed-off-by: Andy Walls <awalls@radix.net>
Signed-off-by: Hans Verkuil <hverkuil@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab@infradead.org>
diff --git a/drivers/media/video/cx18/cx18-ioctl.c b/drivers/media/video/cx18/cx18-ioctl.c
index 4151f1e..70afedf 100644
--- a/drivers/media/video/cx18/cx18-ioctl.c
+++ b/drivers/media/video/cx18/cx18-ioctl.c
@@ -100,19 +100,6 @@
 	}
 }
 
-static int check_service_set(struct v4l2_sliced_vbi_format *fmt, int is_pal)
-{
-	int f, l;
-	u16 set = 0;
-
-	for (f = 0; f < 2; f++) {
-		for (l = 0; l < 24; l++) {
-			fmt->service_lines[f][l] = select_service_from_set(f, l, fmt->service_lines[f][l], is_pal);
-			set |= fmt->service_lines[f][l];
-		}
-	}
-	return set != 0;
-}
 
 u16 cx18_get_service_set(struct v4l2_sliced_vbi_format *fmt)
 {
@@ -126,35 +113,6 @@
 	return set;
 }
 
-static const struct {
-	v4l2_std_id  std;
-	char        *name;
-} enum_stds[] = {
-	{ V4L2_STD_PAL_BG | V4L2_STD_PAL_H, "PAL-BGH" },
-	{ V4L2_STD_PAL_DK,    "PAL-DK"    },
-	{ V4L2_STD_PAL_I,     "PAL-I"     },
-	{ V4L2_STD_PAL_M,     "PAL-M"     },
-	{ V4L2_STD_PAL_N,     "PAL-N"     },
-	{ V4L2_STD_PAL_Nc,    "PAL-Nc"    },
-	{ V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H, "SECAM-BGH" },
-	{ V4L2_STD_SECAM_DK,  "SECAM-DK"  },
-	{ V4L2_STD_SECAM_L,   "SECAM-L"   },
-	{ V4L2_STD_SECAM_LC,  "SECAM-L'"  },
-	{ V4L2_STD_NTSC_M,    "NTSC-M"    },
-	{ V4L2_STD_NTSC_M_JP, "NTSC-J"    },
-	{ V4L2_STD_NTSC_M_KR, "NTSC-K"    },
-};
-
-static const struct v4l2_standard cx18_std_60hz = {
-	.frameperiod = {.numerator = 1001, .denominator = 30000},
-	.framelines = 525,
-};
-
-static const struct v4l2_standard cx18_std_50hz = {
-	.frameperiod = { .numerator = 1, .denominator = 25 },
-	.framelines = 625,
-};
-
 static int cx18_cxc(struct cx18 *cx, unsigned int cmd, void *arg)
 {
 	struct v4l2_register *regs = arg;
@@ -174,665 +132,702 @@
 	return 0;
 }
 
-static int cx18_get_fmt(struct cx18 *cx, int streamtype, struct v4l2_format *fmt)
+static int cx18_g_fmt_vid_cap(struct file *file, void *fh,
+				struct v4l2_format *fmt)
 {
-	switch (fmt->type) {
-	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
-		fmt->fmt.pix.width = cx->params.width;
-		fmt->fmt.pix.height = cx->params.height;
-		fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
-		fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
-		if (streamtype == CX18_ENC_STREAM_TYPE_YUV) {
-			fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
-			/* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
-			fmt->fmt.pix.sizeimage =
-				fmt->fmt.pix.height * fmt->fmt.pix.width +
-				fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
-		} else {
-			fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
-			fmt->fmt.pix.sizeimage = 128 * 1024;
-		}
-		break;
-
-	case V4L2_BUF_TYPE_VBI_CAPTURE:
-		fmt->fmt.vbi.sampling_rate = 27000000;
-		fmt->fmt.vbi.offset = 248;
-		fmt->fmt.vbi.samples_per_line = cx->vbi.raw_decoder_line_size - 4;
-		fmt->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
-		fmt->fmt.vbi.start[0] = cx->vbi.start[0];
-		fmt->fmt.vbi.start[1] = cx->vbi.start[1];
-		fmt->fmt.vbi.count[0] = fmt->fmt.vbi.count[1] = cx->vbi.count;
-		break;
-
-	case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
-	{
-		struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
-
-		vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
-		memset(vbifmt->reserved, 0, sizeof(vbifmt->reserved));
-		memset(vbifmt->service_lines, 0, sizeof(vbifmt->service_lines));
-
-		cx18_av_cmd(cx, VIDIOC_G_FMT, fmt);
-		vbifmt->service_set = cx18_get_service_set(vbifmt);
-		break;
-	}
-	default:
-		return -EINVAL;
-	}
-	return 0;
-}
-
-static int cx18_try_or_set_fmt(struct cx18 *cx, int streamtype,
-		struct v4l2_format *fmt, int set_fmt)
-{
-	struct v4l2_sliced_vbi_format *vbifmt = &fmt->fmt.sliced;
-	u16 set;
-
-	/* set window size */
-	if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
-		int w = fmt->fmt.pix.width;
-		int h = fmt->fmt.pix.height;
-
-		if (w > 720)
-			w = 720;
-		else if (w < 1)
-			w = 1;
-		if (h > (cx->is_50hz ? 576 : 480))
-			h = (cx->is_50hz ? 576 : 480);
-		else if (h < 2)
-			h = 2;
-		cx18_get_fmt(cx, streamtype, fmt);
-		fmt->fmt.pix.width = w;
-		fmt->fmt.pix.height = h;
-
-		if (!set_fmt || (cx->params.width == w && cx->params.height == h))
-			return 0;
-		if (atomic_read(&cx->ana_capturing) > 0)
-			return -EBUSY;
-
-		cx->params.width = w;
-		cx->params.height = h;
-		if (w != 720 || h != (cx->is_50hz ? 576 : 480))
-			cx->params.video_temporal_filter = 0;
-		else
-			cx->params.video_temporal_filter = 8;
-		cx18_av_cmd(cx, VIDIOC_S_FMT, fmt);
-		return cx18_get_fmt(cx, streamtype, fmt);
-	}
-
-	/* set raw VBI format */
-	if (fmt->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
-		if (set_fmt && streamtype == CX18_ENC_STREAM_TYPE_VBI &&
-		    cx->vbi.sliced_in->service_set &&
-		    atomic_read(&cx->ana_capturing) > 0)
-			return -EBUSY;
-		if (set_fmt) {
-			cx->vbi.sliced_in->service_set = 0;
-			cx18_av_cmd(cx, VIDIOC_S_FMT, &cx->vbi.in);
-		}
-		return cx18_get_fmt(cx, streamtype, fmt);
-	}
-
-	/* any else but sliced VBI capture is an error */
-	if (fmt->type != V4L2_BUF_TYPE_SLICED_VBI_CAPTURE)
-		return -EINVAL;
-
-	/* TODO: implement sliced VBI, for now silently return 0 */
-	return 0;
-
-	/* set sliced VBI capture format */
-	vbifmt->io_size = sizeof(struct v4l2_sliced_vbi_data) * 36;
-	memset(vbifmt->reserved, 0, sizeof(vbifmt->reserved));
-
-	if (vbifmt->service_set)
-		cx18_expand_service_set(vbifmt, cx->is_50hz);
-	set = check_service_set(vbifmt, cx->is_50hz);
-	vbifmt->service_set = cx18_get_service_set(vbifmt);
-
-	if (!set_fmt)
-		return 0;
-	if (set == 0)
-		return -EINVAL;
-	if (atomic_read(&cx->ana_capturing) > 0 && cx->vbi.sliced_in->service_set == 0)
-		return -EBUSY;
-	cx18_av_cmd(cx, VIDIOC_S_FMT, fmt);
-	memcpy(cx->vbi.sliced_in, vbifmt, sizeof(*cx->vbi.sliced_in));
-	return 0;
-}
-
-static int cx18_debug_ioctls(struct file *filp, unsigned int cmd, void *arg)
-{
-	struct cx18_open_id *id = (struct cx18_open_id *)filp->private_data;
+	struct cx18_open_id *id = fh;
 	struct cx18 *cx = id->cx;
-	struct v4l2_register *reg = arg;
 
-	switch (cmd) {
-	/* ioctls to allow direct access to the encoder registers for testing */
-	case VIDIOC_DBG_G_REGISTER:
-		if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
-			return cx18_cxc(cx, cmd, arg);
-		if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
-			return cx18_i2c_id(cx, reg->match_chip, cmd, arg);
-		return cx18_call_i2c_client(cx, reg->match_chip, cmd, arg);
+	CX18_DEBUG_IOCTL("VIDIOC_G_FMT: V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
 
-	case VIDIOC_DBG_S_REGISTER:
-		if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
-			return cx18_cxc(cx, cmd, arg);
-		if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
-			return cx18_i2c_id(cx, reg->match_chip, cmd, arg);
-		return cx18_call_i2c_client(cx, reg->match_chip, cmd, arg);
-
-	case VIDIOC_G_CHIP_IDENT: {
-		struct v4l2_chip_ident *chip = arg;
-
-		chip->ident = V4L2_IDENT_NONE;
-		chip->revision = 0;
-		if (reg->match_type == V4L2_CHIP_MATCH_HOST) {
-			if (v4l2_chip_match_host(reg->match_type, reg->match_chip)) {
-				struct v4l2_chip_ident *chip = arg;
-
-				chip->ident = V4L2_IDENT_CX23418;
-			}
-			return 0;
-		}
-		if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
-			return cx18_i2c_id(cx, reg->match_chip, cmd, arg);
-		if (reg->match_type == V4L2_CHIP_MATCH_I2C_ADDR)
-			return cx18_call_i2c_client(cx, reg->match_chip, cmd, arg);
-		return -EINVAL;
-	}
-
-	case VIDIOC_INT_S_AUDIO_ROUTING: {
-		struct v4l2_routing *route = arg;
-
-		cx18_audio_set_route(cx, route);
-		break;
-	}
-
-	default:
-		return -EINVAL;
+	fmt->fmt.pix.width = cx->params.width;
+	fmt->fmt.pix.height = cx->params.height;
+	fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
+	fmt->fmt.pix.field = V4L2_FIELD_INTERLACED;
+	if (id->type == CX18_ENC_STREAM_TYPE_YUV) {
+		fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_HM12;
+		/* YUV size is (Y=(h*w) + UV=(h*(w/2))) */
+		fmt->fmt.pix.sizeimage =
+			fmt->fmt.pix.height * fmt->fmt.pix.width +
+			fmt->fmt.pix.height * (fmt->fmt.pix.width / 2);
+	} else {
+		fmt->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
+		fmt->fmt.pix.sizeimage = 128 * 1024;
 	}
 	return 0;
 }
 
-int cx18_v4l2_ioctls(struct cx18 *cx, struct file *filp, unsigned cmd, void *arg)
+static int cx18_g_fmt_vbi_cap(struct file *file, void *fh,
+				struct v4l2_format *fmt)
 {
-	struct cx18_open_id *id = NULL;
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
 
-	if (filp)
-		id = (struct cx18_open_id *)filp->private_data;
+	CX18_DEBUG_IOCTL("VIDIOC_G_FMT: V4L2_BUF_TYPE_VBI_CAPTURE\n");
 
-	switch (cmd) {
-	case VIDIOC_G_PRIORITY:
-	{
-		enum v4l2_priority *p = arg;
-
-		*p = v4l2_prio_max(&cx->prio);
-		break;
-	}
-
-	case VIDIOC_S_PRIORITY:
-	{
-		enum v4l2_priority *prio = arg;
-
-		return v4l2_prio_change(&cx->prio, &id->prio, *prio);
-	}
-
-	case VIDIOC_QUERYCAP:{
-		struct v4l2_capability *vcap = arg;
-
-		memset(vcap, 0, sizeof(*vcap));
-		strlcpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
-		strlcpy(vcap->card, cx->card_name, sizeof(vcap->card));
-		strlcpy(vcap->bus_info, pci_name(cx->dev), sizeof(vcap->bus_info));
-		vcap->version = CX18_DRIVER_VERSION; 	    /* version */
-		vcap->capabilities = cx->v4l2_cap; 	    /* capabilities */
-
-		/* reserved.. must set to 0! */
-		vcap->reserved[0] = vcap->reserved[1] =
-			vcap->reserved[2] = vcap->reserved[3] = 0;
-		break;
-	}
-
-	case VIDIOC_ENUMAUDIO:{
-		struct v4l2_audio *vin = arg;
-
-		return cx18_get_audio_input(cx, vin->index, vin);
-	}
-
-	case VIDIOC_G_AUDIO:{
-		struct v4l2_audio *vin = arg;
-
-		vin->index = cx->audio_input;
-		return cx18_get_audio_input(cx, vin->index, vin);
-	}
-
-	case VIDIOC_S_AUDIO:{
-		struct v4l2_audio *vout = arg;
-
-		if (vout->index >= cx->nof_audio_inputs)
-			return -EINVAL;
-		cx->audio_input = vout->index;
-		cx18_audio_set_io(cx);
-		break;
-	}
-
-	case VIDIOC_ENUMINPUT:{
-		struct v4l2_input *vin = arg;
-
-		/* set it to defaults from our table */
-		return cx18_get_input(cx, vin->index, vin);
-	}
-
-	case VIDIOC_TRY_FMT:
-	case VIDIOC_S_FMT: {
-		struct v4l2_format *fmt = arg;
-
-		return cx18_try_or_set_fmt(cx, id->type, fmt, cmd == VIDIOC_S_FMT);
-	}
-
-	case VIDIOC_G_FMT: {
-		struct v4l2_format *fmt = arg;
-		int type = fmt->type;
-
-		memset(fmt, 0, sizeof(*fmt));
-		fmt->type = type;
-		return cx18_get_fmt(cx, id->type, fmt);
-	}
-
-	case VIDIOC_CROPCAP: {
-		struct v4l2_cropcap *cropcap = arg;
-
-		if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
-			return -EINVAL;
-		cropcap->bounds.top = cropcap->bounds.left = 0;
-		cropcap->bounds.width = 720;
-		cropcap->bounds.height = cx->is_50hz ? 576 : 480;
-		cropcap->pixelaspect.numerator = cx->is_50hz ? 59 : 10;
-		cropcap->pixelaspect.denominator = cx->is_50hz ? 54 : 11;
-		cropcap->defrect = cropcap->bounds;
-		return 0;
-	}
-
-	case VIDIOC_S_CROP: {
-		struct v4l2_crop *crop = arg;
-
-		if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
-			return -EINVAL;
-		return cx18_av_cmd(cx, VIDIOC_S_CROP, arg);
-	}
-
-	case VIDIOC_G_CROP: {
-		struct v4l2_crop *crop = arg;
-
-		if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
-			return -EINVAL;
-		return cx18_av_cmd(cx, VIDIOC_G_CROP, arg);
-	}
-
-	case VIDIOC_ENUM_FMT: {
-		static struct v4l2_fmtdesc formats[] = {
-			{ 0, 0, 0,
-			  "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12,
-			  { 0, 0, 0, 0 }
-			},
-			{ 1, 0, V4L2_FMT_FLAG_COMPRESSED,
-			  "MPEG", V4L2_PIX_FMT_MPEG,
-			  { 0, 0, 0, 0 }
-			}
-		};
-		struct v4l2_fmtdesc *fmt = arg;
-		enum v4l2_buf_type type = fmt->type;
-
-		switch (type) {
-		case V4L2_BUF_TYPE_VIDEO_CAPTURE:
-			break;
-		default:
-			return -EINVAL;
-		}
-		if (fmt->index > 1)
-			return -EINVAL;
-		*fmt = formats[fmt->index];
-		fmt->type = type;
-		return 0;
-	}
-
-	case VIDIOC_G_INPUT:{
-		*(int *)arg = cx->active_input;
-		break;
-	}
-
-	case VIDIOC_S_INPUT:{
-		int inp = *(int *)arg;
-
-		if (inp < 0 || inp >= cx->nof_inputs)
-			return -EINVAL;
-
-		if (inp == cx->active_input) {
-			CX18_DEBUG_INFO("Input unchanged\n");
-			break;
-		}
-		CX18_DEBUG_INFO("Changing input from %d to %d\n",
-				cx->active_input, inp);
-
-		cx->active_input = inp;
-		/* Set the audio input to whatever is appropriate for the
-		   input type. */
-		cx->audio_input = cx->card->video_inputs[inp].audio_index;
-
-		/* prevent others from messing with the streams until
-		   we're finished changing inputs. */
-		cx18_mute(cx);
-		cx18_video_set_io(cx);
-		cx18_audio_set_io(cx);
-		cx18_unmute(cx);
-		break;
-	}
-
-	case VIDIOC_G_FREQUENCY:{
-		struct v4l2_frequency *vf = arg;
-
-		if (vf->tuner != 0)
-			return -EINVAL;
-		cx18_call_i2c_clients(cx, cmd, arg);
-		break;
-	}
-
-	case VIDIOC_S_FREQUENCY:{
-		struct v4l2_frequency vf = *(struct v4l2_frequency *)arg;
-
-		if (vf.tuner != 0)
-			return -EINVAL;
-
-		cx18_mute(cx);
-		CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf.frequency);
-		cx18_call_i2c_clients(cx, cmd, &vf);
-		cx18_unmute(cx);
-		break;
-	}
-
-	case VIDIOC_ENUMSTD:{
-		struct v4l2_standard *vs = arg;
-		int idx = vs->index;
-
-		if (idx < 0 || idx >= ARRAY_SIZE(enum_stds))
-			return -EINVAL;
-
-		*vs = (enum_stds[idx].std & V4L2_STD_525_60) ?
-				cx18_std_60hz : cx18_std_50hz;
-		vs->index = idx;
-		vs->id = enum_stds[idx].std;
-		strlcpy(vs->name, enum_stds[idx].name, sizeof(vs->name));
-		break;
-	}
-
-	case VIDIOC_G_STD:{
-		*(v4l2_std_id *) arg = cx->std;
-		break;
-	}
-
-	case VIDIOC_S_STD: {
-		v4l2_std_id std = *(v4l2_std_id *) arg;
-
-		if ((std & V4L2_STD_ALL) == 0)
-			return -EINVAL;
-
-		if (std == cx->std)
-			break;
-
-		if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
-		    atomic_read(&cx->ana_capturing) > 0) {
-			/* Switching standard would turn off the radio or mess
-			   with already running streams, prevent that by
-			   returning EBUSY. */
-			return -EBUSY;
-		}
-
-		cx->std = std;
-		cx->is_60hz = (std & V4L2_STD_525_60) ? 1 : 0;
-		cx->params.is_50hz = cx->is_50hz = !cx->is_60hz;
-		cx->params.width = 720;
-		cx->params.height = cx->is_50hz ? 576 : 480;
-		cx->vbi.count = cx->is_50hz ? 18 : 12;
-		cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
-		cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
-		cx->vbi.sliced_decoder_line_size = cx->is_60hz ? 272 : 284;
-		CX18_DEBUG_INFO("Switching standard to %llx.\n", (unsigned long long)cx->std);
-
-		/* Tuner */
-		cx18_call_i2c_clients(cx, VIDIOC_S_STD, &cx->std);
-		break;
-	}
-
-	case VIDIOC_S_TUNER: {	/* Setting tuner can only set audio mode */
-		struct v4l2_tuner *vt = arg;
-
-		if (vt->index != 0)
-			return -EINVAL;
-
-		cx18_call_i2c_clients(cx, VIDIOC_S_TUNER, vt);
-		break;
-	}
-
-	case VIDIOC_G_TUNER: {
-		struct v4l2_tuner *vt = arg;
-
-		if (vt->index != 0)
-			return -EINVAL;
-
-		memset(vt, 0, sizeof(*vt));
-		cx18_call_i2c_clients(cx, VIDIOC_G_TUNER, vt);
-
-		if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
-			strlcpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
-			vt->type = V4L2_TUNER_RADIO;
-		} else {
-			strlcpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
-			vt->type = V4L2_TUNER_ANALOG_TV;
-		}
-		break;
-	}
-
-	case VIDIOC_G_SLICED_VBI_CAP: {
-		struct v4l2_sliced_vbi_cap *cap = arg;
-		int set = cx->is_50hz ? V4L2_SLICED_VBI_625 : V4L2_SLICED_VBI_525;
-		int f, l;
-		enum v4l2_buf_type type = cap->type;
-
-		memset(cap, 0, sizeof(*cap));
-		cap->type = type;
-		if (type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
-			for (f = 0; f < 2; f++) {
-				for (l = 0; l < 24; l++) {
-					if (valid_service_line(f, l, cx->is_50hz))
-						cap->service_lines[f][l] = set;
-				}
-			}
-			return 0;
-		}
-		return -EINVAL;
-	}
-
-	case VIDIOC_ENCODER_CMD:
-	case VIDIOC_TRY_ENCODER_CMD: {
-		struct v4l2_encoder_cmd *enc = arg;
-		int try = cmd == VIDIOC_TRY_ENCODER_CMD;
-
-		memset(&enc->raw, 0, sizeof(enc->raw));
-		switch (enc->cmd) {
-		case V4L2_ENC_CMD_START:
-			enc->flags = 0;
-			if (try)
-				return 0;
-			return cx18_start_capture(id);
-
-		case V4L2_ENC_CMD_STOP:
-			enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
-			if (try)
-				return 0;
-			cx18_stop_capture(id, enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
-			return 0;
-
-		case V4L2_ENC_CMD_PAUSE:
-			enc->flags = 0;
-			if (try)
-				return 0;
-			if (!atomic_read(&cx->ana_capturing))
-				return -EPERM;
-			if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
-				return 0;
-			cx18_mute(cx);
-			cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, cx18_find_handle(cx));
-			break;
-
-		case V4L2_ENC_CMD_RESUME:
-			enc->flags = 0;
-			if (try)
-				return 0;
-			if (!atomic_read(&cx->ana_capturing))
-				return -EPERM;
-			if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
-				return 0;
-			cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, cx18_find_handle(cx));
-			cx18_unmute(cx);
-			break;
-		default:
-			return -EINVAL;
-		}
-		break;
-	}
-
-	case VIDIOC_LOG_STATUS:
-	{
-		struct v4l2_input vidin;
-		struct v4l2_audio audin;
-		int i;
-
-		CX18_INFO("=================  START STATUS CARD #%d  =================\n", cx->num);
-		if (cx->hw_flags & CX18_HW_TVEEPROM) {
-			struct tveeprom tv;
-
-			cx18_read_eeprom(cx, &tv);
-		}
-		cx18_call_i2c_clients(cx, VIDIOC_LOG_STATUS, NULL);
-		cx18_get_input(cx, cx->active_input, &vidin);
-		cx18_get_audio_input(cx, cx->audio_input, &audin);
-		CX18_INFO("Video Input: %s\n", vidin.name);
-		CX18_INFO("Audio Input: %s\n", audin.name);
-		CX18_INFO("Tuner: %s\n",
-			test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ?
-			"Radio" : "TV");
-		cx2341x_log_status(&cx->params, cx->name);
-		CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
-		for (i = 0; i < CX18_MAX_STREAMS; i++) {
-			struct cx18_stream *s = &cx->streams[i];
-
-			if (s->v4l2dev == NULL || s->buffers == 0)
-				continue;
-			CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
-				s->name, s->s_flags,
-				(s->buffers - s->q_free.buffers) * 100 / s->buffers,
-				(s->buffers * s->buf_size) / 1024, s->buffers);
-		}
-		CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
-				(long long)cx->mpg_data_received,
-				(long long)cx->vbi_data_inserted);
-		CX18_INFO("==================  END STATUS CARD #%d  ==================\n", cx->num);
-		break;
-	}
-
-	default:
-		return -EINVAL;
-	}
+	fmt->fmt.vbi.sampling_rate = 27000000;
+	fmt->fmt.vbi.offset = 248;
+	fmt->fmt.vbi.samples_per_line = cx->vbi.raw_decoder_line_size - 4;
+	fmt->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
+	fmt->fmt.vbi.start[0] = cx->vbi.start[0];
+	fmt->fmt.vbi.start[1] = cx->vbi.start[1];
+	fmt->fmt.vbi.count[0] = fmt->fmt.vbi.count[1] = cx->vbi.count;
 	return 0;
 }
 
-static int cx18_v4l2_do_ioctl(struct inode *inode, struct file *filp,
-			      unsigned int cmd, void *arg)
+static int cx18_g_fmt_sliced_vbi_cap(struct file *file, void *fh,
+					struct v4l2_format *fmt)
 {
-	struct cx18_open_id *id = (struct cx18_open_id *)filp->private_data;
+	return -EINVAL;
+}
+
+static int cx18_try_fmt_vid_cap(struct file *file, void *fh,
+				struct v4l2_format *fmt)
+{
+	struct cx18_open_id *id = fh;
+	struct cx18 *cx = id->cx;
+
+	int w = fmt->fmt.pix.width;
+	int h = fmt->fmt.pix.height;
+
+	CX18_DEBUG_IOCTL("VIDIOC_TRY_FMT: V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
+
+	w = min(w, 720);
+	w = max(w, 1);
+	h = min(h, cx->is_50hz ? 576 : 480);
+	h = max(h, 2);
+	cx18_g_fmt_vid_cap(file, fh, fmt);
+	fmt->fmt.pix.width = w;
+	fmt->fmt.pix.height = h;
+	return 0;
+}
+
+static int cx18_try_fmt_vbi_cap(struct file *file, void *fh,
+				struct v4l2_format *fmt)
+{
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
+
+	CX18_DEBUG_IOCTL("VIDIOC_TRY_FMT: V4L2_BUF_TYPE_VBI_CAPTURE\n");
+
+	return cx18_g_fmt_vbi_cap(file, fh, fmt);
+}
+
+static int cx18_try_fmt_sliced_vbi_cap(struct file *file, void *fh,
+					struct v4l2_format *fmt)
+{
+	return -EINVAL;
+}
+
+static int cx18_s_fmt_vid_cap(struct file *file, void *fh,
+				struct v4l2_format *fmt)
+{
+	struct cx18_open_id *id = fh;
+	struct cx18 *cx = id->cx;
+	int ret;
+	int w = fmt->fmt.pix.width;
+	int h = fmt->fmt.pix.height;
+
+	ret = v4l2_prio_check(&cx->prio, &id->prio);
+	if (ret)
+		return ret;
+
+	CX18_DEBUG_IOCTL("VIDIOC_S_FMT: V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
+
+	ret = cx18_try_fmt_vid_cap(file, fh, fmt);
+	if (ret)
+		return ret;
+
+	if (cx->params.width == w && cx->params.height == h)
+		return 0;
+
+	if (atomic_read(&cx->ana_capturing) > 0)
+		return -EBUSY;
+
+	cx->params.width = w;
+	cx->params.height = h;
+	if (w != 720 || h != (cx->is_50hz ? 576 : 480))
+		cx->params.video_temporal_filter = 0;
+	else
+		cx->params.video_temporal_filter = 8;
+	cx18_av_cmd(cx, VIDIOC_S_FMT, fmt);
+	return cx18_g_fmt_vid_cap(file, fh, fmt);
+}
+
+static int cx18_s_fmt_vbi_cap(struct file *file, void *fh,
+				struct v4l2_format *fmt)
+{
+	struct cx18_open_id *id = fh;
 	struct cx18 *cx = id->cx;
 	int ret;
 
-	/* check priority */
-	switch (cmd) {
-	case VIDIOC_S_CTRL:
-	case VIDIOC_S_STD:
-	case VIDIOC_S_INPUT:
-	case VIDIOC_S_TUNER:
-	case VIDIOC_S_FREQUENCY:
-	case VIDIOC_S_FMT:
-	case VIDIOC_S_CROP:
-	case VIDIOC_S_EXT_CTRLS:
-		ret = v4l2_prio_check(&cx->prio, &id->prio);
-		if (ret)
-			return ret;
+	ret = v4l2_prio_check(&cx->prio, &id->prio);
+	if (ret)
+		return ret;
+
+	CX18_DEBUG_IOCTL("VIDIOC_S_FMT: V4L2_BUF_TYPE_VBI_CAPTURE\n");
+
+	if (id->type == CX18_ENC_STREAM_TYPE_VBI &&
+			cx->vbi.sliced_in->service_set &&
+			atomic_read(&cx->ana_capturing) > 0)
+		return -EBUSY;
+
+	cx->vbi.sliced_in->service_set = 0;
+	cx18_av_cmd(cx, VIDIOC_S_FMT, &cx->vbi.in);
+	return cx18_g_fmt_vbi_cap(file, fh, fmt);
+}
+
+static int cx18_s_fmt_sliced_vbi_cap(struct file *file, void *fh,
+					struct v4l2_format *fmt)
+{
+	return -EINVAL;
+}
+
+static int cx18_g_chip_ident(struct file *file, void *fh,
+				struct v4l2_chip_ident *chip)
+{
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
+
+	CX18_DEBUG_IOCTL("VIDIOC_G_CHIP_IDENT\n");
+
+	chip->ident = V4L2_IDENT_NONE;
+	chip->revision = 0;
+	if (chip->match_type == V4L2_CHIP_MATCH_HOST) {
+		if (v4l2_chip_match_host(chip->match_type, chip->match_chip))
+			chip->ident = V4L2_IDENT_CX23418;
+		return 0;
+	}
+	if (chip->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
+		return cx18_i2c_id(cx, chip->match_chip, VIDIOC_G_CHIP_IDENT,
+					chip);
+	if (chip->match_type == V4L2_CHIP_MATCH_I2C_ADDR)
+		return cx18_call_i2c_client(cx, chip->match_chip,
+						VIDIOC_G_CHIP_IDENT, chip);
+	return -EINVAL;
+}
+
+static int cx18_g_register(struct file *file, void *fh,
+				struct v4l2_register *reg)
+{
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
+
+	CX18_DEBUG_IOCTL("VIDIOC_DBG_G_REGISTER\n");
+
+	if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
+		return cx18_cxc(cx, VIDIOC_DBG_G_REGISTER, reg);
+	if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
+		return cx18_i2c_id(cx, reg->match_chip, VIDIOC_DBG_G_REGISTER,
+					reg);
+	return cx18_call_i2c_client(cx, reg->match_chip, VIDIOC_DBG_G_REGISTER,
+					reg);
+}
+
+static int cx18_s_register(struct file *file, void *fh,
+				struct v4l2_register *reg)
+{
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
+
+	CX18_DEBUG_IOCTL("VIDIOC_DBG_S_REGISTER\n");
+
+	if (v4l2_chip_match_host(reg->match_type, reg->match_chip))
+		return cx18_cxc(cx, VIDIOC_DBG_S_REGISTER, reg);
+	if (reg->match_type == V4L2_CHIP_MATCH_I2C_DRIVER)
+		return cx18_i2c_id(cx, reg->match_chip, VIDIOC_DBG_S_REGISTER,
+					reg);
+	return cx18_call_i2c_client(cx, reg->match_chip, VIDIOC_DBG_S_REGISTER,
+					reg);
+}
+
+static int cx18_g_priority(struct file *file, void *fh, enum v4l2_priority *p)
+{
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
+
+	CX18_DEBUG_IOCTL("VIDIOC_G_PRIORITY\n");
+
+	*p = v4l2_prio_max(&cx->prio);
+	return 0;
+}
+
+static int cx18_s_priority(struct file *file, void *fh, enum v4l2_priority prio)
+{
+	struct cx18_open_id *id = fh;
+	struct cx18 *cx = id->cx;
+
+	CX18_DEBUG_IOCTL("VIDIOC_S_PRIORITY\n");
+
+	return v4l2_prio_change(&cx->prio, &id->prio, prio);
+}
+
+static int cx18_querycap(struct file *file, void *fh,
+				struct v4l2_capability *vcap)
+{
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
+
+	CX18_DEBUG_IOCTL("VIDIOC_QUERYCAP\n");
+
+	strlcpy(vcap->driver, CX18_DRIVER_NAME, sizeof(vcap->driver));
+	strlcpy(vcap->card, cx->card_name, sizeof(vcap->card));
+	strlcpy(vcap->bus_info, pci_name(cx->dev), sizeof(vcap->bus_info));
+	vcap->version = CX18_DRIVER_VERSION; 	    /* version */
+	vcap->capabilities = cx->v4l2_cap; 	    /* capabilities */
+	return 0;
+}
+
+static int cx18_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
+{
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
+
+	CX18_DEBUG_IOCTL("VIDIOC_ENUMAUDIO\n");
+
+	return cx18_get_audio_input(cx, vin->index, vin);
+}
+
+static int cx18_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
+{
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
+
+	CX18_DEBUG_IOCTL("VIDIOC_G_AUDIO\n");
+
+	vin->index = cx->audio_input;
+	return cx18_get_audio_input(cx, vin->index, vin);
+}
+
+static int cx18_s_audio(struct file *file, void *fh, struct v4l2_audio *vout)
+{
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
+
+	CX18_DEBUG_IOCTL("VIDIOC_S_AUDIO\n");
+
+	if (vout->index >= cx->nof_audio_inputs)
+		return -EINVAL;
+	cx->audio_input = vout->index;
+	cx18_audio_set_io(cx);
+	return 0;
+}
+
+static int cx18_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
+{
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
+
+	CX18_DEBUG_IOCTL("VIDIOC_ENUMINPUT\n");
+
+	/* set it to defaults from our table */
+	return cx18_get_input(cx, vin->index, vin);
+}
+
+static int cx18_cropcap(struct file *file, void *fh,
+			struct v4l2_cropcap *cropcap)
+{
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
+
+	CX18_DEBUG_IOCTL("VIDIOC_CROPCAP\n");
+
+	if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+		return -EINVAL;
+	cropcap->bounds.top = cropcap->bounds.left = 0;
+	cropcap->bounds.width = 720;
+	cropcap->bounds.height = cx->is_50hz ? 576 : 480;
+	cropcap->pixelaspect.numerator = cx->is_50hz ? 59 : 10;
+	cropcap->pixelaspect.denominator = cx->is_50hz ? 54 : 11;
+	cropcap->defrect = cropcap->bounds;
+	return 0;
+}
+
+static int cx18_s_crop(struct file *file, void *fh, struct v4l2_crop *crop)
+{
+	struct cx18_open_id *id = fh;
+	struct cx18 *cx = id->cx;
+	int ret;
+
+	ret = v4l2_prio_check(&cx->prio, &id->prio);
+	if (ret)
+		return ret;
+
+	CX18_DEBUG_IOCTL("VIDIOC_S_CROP\n");
+
+	if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+		return -EINVAL;
+	return cx18_av_cmd(cx, VIDIOC_S_CROP, crop);
+}
+
+static int cx18_g_crop(struct file *file, void *fh, struct v4l2_crop *crop)
+{
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
+
+	CX18_DEBUG_IOCTL("VIDIOC_G_CROP\n");
+
+	if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
+		return -EINVAL;
+	return cx18_av_cmd(cx, VIDIOC_G_CROP, crop);
+}
+
+static int cx18_enum_fmt_vid_cap(struct file *file, void *fh,
+					struct v4l2_fmtdesc *fmt)
+{
+	static struct v4l2_fmtdesc formats[] = {
+		{ 0, V4L2_BUF_TYPE_VIDEO_CAPTURE, 0,
+		  "HM12 (YUV 4:1:1)", V4L2_PIX_FMT_HM12, { 0, 0, 0, 0 }
+		},
+		{ 1, V4L2_BUF_TYPE_VIDEO_CAPTURE, V4L2_FMT_FLAG_COMPRESSED,
+		  "MPEG", V4L2_PIX_FMT_MPEG, { 0, 0, 0, 0 }
+		}
+	};
+
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
+
+	CX18_DEBUG_IOCTL("VIDIOC_ENUM_FMT: V4L2_BUF_TYPE_VIDEO_CAPTURE\n");
+
+	if (fmt->index > 1)
+		return -EINVAL;
+	*fmt = formats[fmt->index];
+	return 0;
+}
+
+static int cx18_g_input(struct file *file, void *fh, unsigned int *i)
+{
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
+
+	CX18_DEBUG_IOCTL("VIDIOC_G_INPUT\n");
+
+	*i = cx->active_input;
+	return 0;
+}
+
+int cx18_s_input(struct file *file, void *fh, unsigned int inp)
+{
+	struct cx18_open_id *id = fh;
+	struct cx18 *cx = id->cx;
+	int ret;
+
+	ret = v4l2_prio_check(&cx->prio, &id->prio);
+	if (ret)
+		return ret;
+
+	CX18_DEBUG_IOCTL("VIDIOC_S_INPUT\n");
+
+	if (inp < 0 || inp >= cx->nof_inputs)
+		return -EINVAL;
+
+	if (inp == cx->active_input) {
+		CX18_DEBUG_INFO("Input unchanged\n");
+		return 0;
 	}
 
-	switch (cmd) {
-	case VIDIOC_DBG_G_REGISTER:
-	case VIDIOC_DBG_S_REGISTER:
-	case VIDIOC_G_CHIP_IDENT:
-	case VIDIOC_INT_S_AUDIO_ROUTING:
-	case VIDIOC_INT_RESET:
-		if (cx18_debug & CX18_DBGFLG_IOCTL) {
-			printk(KERN_INFO "cx18%d ioctl: ", cx->num);
-			v4l_printk_ioctl(cmd);
-		}
-		return cx18_debug_ioctls(filp, cmd, arg);
+	CX18_DEBUG_INFO("Changing input from %d to %d\n",
+			cx->active_input, inp);
 
-	case VIDIOC_G_PRIORITY:
-	case VIDIOC_S_PRIORITY:
-	case VIDIOC_QUERYCAP:
-	case VIDIOC_ENUMINPUT:
-	case VIDIOC_G_INPUT:
-	case VIDIOC_S_INPUT:
-	case VIDIOC_G_FMT:
-	case VIDIOC_S_FMT:
-	case VIDIOC_TRY_FMT:
-	case VIDIOC_ENUM_FMT:
-	case VIDIOC_CROPCAP:
-	case VIDIOC_G_CROP:
-	case VIDIOC_S_CROP:
-	case VIDIOC_G_FREQUENCY:
-	case VIDIOC_S_FREQUENCY:
-	case VIDIOC_ENUMSTD:
-	case VIDIOC_G_STD:
-	case VIDIOC_S_STD:
-	case VIDIOC_S_TUNER:
-	case VIDIOC_G_TUNER:
-	case VIDIOC_ENUMAUDIO:
-	case VIDIOC_S_AUDIO:
-	case VIDIOC_G_AUDIO:
-	case VIDIOC_G_SLICED_VBI_CAP:
-	case VIDIOC_LOG_STATUS:
-	case VIDIOC_G_ENC_INDEX:
-	case VIDIOC_ENCODER_CMD:
-	case VIDIOC_TRY_ENCODER_CMD:
-		if (cx18_debug & CX18_DBGFLG_IOCTL) {
-			printk(KERN_INFO "cx18%d ioctl: ", cx->num);
-			v4l_printk_ioctl(cmd);
-		}
-		return cx18_v4l2_ioctls(cx, filp, cmd, arg);
+	cx->active_input = inp;
+	/* Set the audio input to whatever is appropriate for the input type. */
+	cx->audio_input = cx->card->video_inputs[inp].audio_index;
 
-	case VIDIOC_QUERYMENU:
-	case VIDIOC_QUERYCTRL:
-	case VIDIOC_S_CTRL:
-	case VIDIOC_G_CTRL:
-	case VIDIOC_S_EXT_CTRLS:
-	case VIDIOC_G_EXT_CTRLS:
-	case VIDIOC_TRY_EXT_CTRLS:
-		if (cx18_debug & CX18_DBGFLG_IOCTL) {
-			printk(KERN_INFO "cx18%d ioctl: ", cx->num);
-			v4l_printk_ioctl(cmd);
-		}
-		return cx18_control_ioctls(cx, cmd, arg);
+	/* prevent others from messing with the streams until
+	   we're finished changing inputs. */
+	cx18_mute(cx);
+	cx18_video_set_io(cx);
+	cx18_audio_set_io(cx);
+	cx18_unmute(cx);
+	return 0;
+}
 
-	case 0x00005401:	/* Handle isatty() calls */
+static int cx18_g_frequency(struct file *file, void *fh,
+				struct v4l2_frequency *vf)
+{
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
+
+	CX18_DEBUG_IOCTL("VIDIOC_G_FREQUENCY\n");
+
+	if (vf->tuner != 0)
 		return -EINVAL;
+
+	cx18_call_i2c_clients(cx, VIDIOC_G_FREQUENCY, vf);
+	return 0;
+}
+
+int cx18_s_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
+{
+	struct cx18_open_id *id = fh;
+	struct cx18 *cx = id->cx;
+	int ret;
+
+	ret = v4l2_prio_check(&cx->prio, &id->prio);
+	if (ret)
+		return ret;
+
+	CX18_DEBUG_IOCTL("VIDIOC_S_FREQUENCY\n");
+
+	if (vf->tuner != 0)
+		return -EINVAL;
+
+	cx18_mute(cx);
+	CX18_DEBUG_INFO("v4l2 ioctl: set frequency %d\n", vf->frequency);
+	cx18_call_i2c_clients(cx, VIDIOC_S_FREQUENCY, vf);
+	cx18_unmute(cx);
+	return 0;
+}
+
+static int cx18_g_std(struct file *file, void *fh, v4l2_std_id *std)
+{
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
+
+	CX18_DEBUG_IOCTL("VIDIOC_G_STD\n");
+
+	*std = cx->std;
+	return 0;
+}
+
+int cx18_s_std(struct file *file, void *fh, v4l2_std_id *std)
+{
+	struct cx18_open_id *id = fh;
+	struct cx18 *cx = id->cx;
+	int ret;
+
+	ret = v4l2_prio_check(&cx->prio, &id->prio);
+	if (ret)
+		return ret;
+
+	CX18_DEBUG_IOCTL("VIDIOC_S_STD\n");
+
+	if ((*std & V4L2_STD_ALL) == 0)
+		return -EINVAL;
+
+	if (*std == cx->std)
+		return 0;
+
+	if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ||
+	    atomic_read(&cx->ana_capturing) > 0) {
+		/* Switching standard would turn off the radio or mess
+		   with already running streams, prevent that by
+		   returning EBUSY. */
+		return -EBUSY;
+	}
+
+	cx->std = *std;
+	cx->is_60hz = (*std & V4L2_STD_525_60) ? 1 : 0;
+	cx->params.is_50hz = cx->is_50hz = !cx->is_60hz;
+	cx->params.width = 720;
+	cx->params.height = cx->is_50hz ? 576 : 480;
+	cx->vbi.count = cx->is_50hz ? 18 : 12;
+	cx->vbi.start[0] = cx->is_50hz ? 6 : 10;
+	cx->vbi.start[1] = cx->is_50hz ? 318 : 273;
+	cx->vbi.sliced_decoder_line_size = cx->is_60hz ? 272 : 284;
+	CX18_DEBUG_INFO("Switching standard to %llx.\n",
+			(unsigned long long) cx->std);
+
+	/* Tuner */
+	cx18_call_i2c_clients(cx, VIDIOC_S_STD, &cx->std);
+	return 0;
+}
+
+static int cx18_s_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
+{
+	struct cx18_open_id *id = fh;
+	struct cx18 *cx = id->cx;
+	int ret;
+
+	ret = v4l2_prio_check(&cx->prio, &id->prio);
+	if (ret)
+		return ret;
+
+	CX18_DEBUG_IOCTL("VIDIOC_S_TUNER\n");
+
+	if (vt->index != 0)
+		return -EINVAL;
+
+	/* Setting tuner can only set audio mode */
+	cx18_call_i2c_clients(cx, VIDIOC_S_TUNER, vt);
+
+	return 0;
+}
+
+static int cx18_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
+{
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
+
+	CX18_DEBUG_IOCTL("VIDIOC_G_TUNER\n");
+
+	if (vt->index != 0)
+		return -EINVAL;
+
+	cx18_call_i2c_clients(cx, VIDIOC_G_TUNER, vt);
+
+	if (test_bit(CX18_F_I_RADIO_USER, &cx->i_flags)) {
+		strlcpy(vt->name, "cx18 Radio Tuner", sizeof(vt->name));
+		vt->type = V4L2_TUNER_RADIO;
+	} else {
+		strlcpy(vt->name, "cx18 TV Tuner", sizeof(vt->name));
+		vt->type = V4L2_TUNER_ANALOG_TV;
+	}
+
+	return 0;
+}
+
+static int cx18_g_sliced_vbi_cap(struct file *file, void *fh,
+					struct v4l2_sliced_vbi_cap *cap)
+{
+	return -EINVAL;
+}
+
+static int cx18_g_enc_index(struct file *file, void *fh,
+				struct v4l2_enc_idx *idx)
+{
+	return -EINVAL;
+}
+
+static int cx18_encoder_cmd(struct file *file, void *fh,
+				struct v4l2_encoder_cmd *enc)
+{
+	struct cx18_open_id *id = fh;
+	struct cx18 *cx = id->cx;
+
+	CX18_DEBUG_IOCTL("VIDIOC_ENCODER_CMD:\n");
+
+	memset(&enc->raw, 0, sizeof(enc->raw));
+
+	switch (enc->cmd) {
+	case V4L2_ENC_CMD_START:
+		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
+		enc->flags = 0;
+		return cx18_start_capture(id);
+
+	case V4L2_ENC_CMD_STOP:
+		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
+		enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
+		cx18_stop_capture(id,
+				  enc->flags & V4L2_ENC_CMD_STOP_AT_GOP_END);
+		break;
+
+	case V4L2_ENC_CMD_PAUSE:
+		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
+		enc->flags = 0;
+		if (!atomic_read(&cx->ana_capturing))
+			return -EPERM;
+		if (test_and_set_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
+			return 0;
+		cx18_mute(cx);
+		cx18_vapi(cx, CX18_CPU_CAPTURE_PAUSE, 1, cx18_find_handle(cx));
+		break;
+
+	case V4L2_ENC_CMD_RESUME:
+		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
+		enc->flags = 0;
+		if (!atomic_read(&cx->ana_capturing))
+			return -EPERM;
+		if (!test_and_clear_bit(CX18_F_I_ENC_PAUSED, &cx->i_flags))
+			return 0;
+		cx18_vapi(cx, CX18_CPU_CAPTURE_RESUME, 1, cx18_find_handle(cx));
+		cx18_unmute(cx);
+		break;
+
 	default:
-		return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
-						   cx18_v4l2_do_ioctl);
+		CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
+		return -EINVAL;
+	}
+	return 0;
+}
+
+static int cx18_try_encoder_cmd(struct file *file, void *fh,
+				struct v4l2_encoder_cmd *enc)
+{
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
+
+	CX18_DEBUG_IOCTL("VIDIOC_TRY_ENCDOER_CMD:\n");
+
+	memset(&enc->raw, 0, sizeof(enc->raw));
+
+	switch (enc->cmd) {
+	case V4L2_ENC_CMD_START:
+		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_START\n");
+		enc->flags = 0;
+		break;
+
+	case V4L2_ENC_CMD_STOP:
+		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_STOP\n");
+		enc->flags &= V4L2_ENC_CMD_STOP_AT_GOP_END;
+		break;
+
+	case V4L2_ENC_CMD_PAUSE:
+		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_PAUSE\n");
+		enc->flags = 0;
+		break;
+
+	case V4L2_ENC_CMD_RESUME:
+		CX18_DEBUG_IOCTL("V4L2_ENC_CMD_RESUME\n");
+		enc->flags = 0;
+		break;
+
+	default:
+		CX18_DEBUG_IOCTL("Unknown cmd %d\n", enc->cmd);
+		return -EINVAL;
+	}
+	return 0;
+}
+
+static int cx18_log_status(struct file *file, void *fh)
+{
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
+	struct v4l2_input vidin;
+	struct v4l2_audio audin;
+	int i;
+
+	CX18_DEBUG_IOCTL("VIDIOC_LOG_STATUS\n");
+	CX18_INFO("=================  START STATUS CARD #%d  =================\n", cx->num);
+	if (cx->hw_flags & CX18_HW_TVEEPROM) {
+		struct tveeprom tv;
+
+		cx18_read_eeprom(cx, &tv);
+	}
+	cx18_call_i2c_clients(cx, VIDIOC_LOG_STATUS, NULL);
+	cx18_get_input(cx, cx->active_input, &vidin);
+	cx18_get_audio_input(cx, cx->audio_input, &audin);
+	CX18_INFO("Video Input: %s\n", vidin.name);
+	CX18_INFO("Audio Input: %s\n", audin.name);
+	CX18_INFO("Tuner: %s\n",
+		test_bit(CX18_F_I_RADIO_USER, &cx->i_flags) ?  "Radio" : "TV");
+	cx2341x_log_status(&cx->params, cx->name);
+	CX18_INFO("Status flags: 0x%08lx\n", cx->i_flags);
+	for (i = 0; i < CX18_MAX_STREAMS; i++) {
+		struct cx18_stream *s = &cx->streams[i];
+
+		if (s->v4l2dev == NULL || s->buffers == 0)
+			continue;
+		CX18_INFO("Stream %s: status 0x%04lx, %d%% of %d KiB (%d buffers) in use\n",
+			  s->name, s->s_flags,
+			  (s->buffers - s->q_free.buffers) * 100 / s->buffers,
+			  (s->buffers * s->buf_size) / 1024, s->buffers);
+	}
+	CX18_INFO("Read MPEG/VBI: %lld/%lld bytes\n",
+			(long long)cx->mpg_data_received,
+			(long long)cx->vbi_data_inserted);
+	CX18_INFO("==================  END STATUS CARD #%d  ==================\n", cx->num);
+	return 0;
+}
+
+static int cx18_default(struct file *file, void *fh, int cmd, void *arg)
+{
+	struct cx18 *cx = ((struct cx18_open_id *)fh)->cx;
+
+	switch (cmd) {
+	case VIDIOC_INT_S_AUDIO_ROUTING: {
+		struct v4l2_routing *route = arg;
+		CX18_DEBUG_IOCTL("VIDIOC_INT_S_AUDIO_ROUTING (%d, %d)\n",
+					route->input, route->output);
+		cx18_audio_set_route(cx, route);
+		break;
+	}
+	case VIDIOC_INT_RESET: {
+		u32 val = *(u32 *)arg;
+		CX18_DEBUG_IOCTL("VIDIOC_INT_RESET (%#10x)\n", val);
+		/* No op right now */
+		/* cx18_av_cmd(cx, cmd, arg) */
+		/* cx18_call_i2c_clients(cx, cmd, arg) */
+		break;
+	}
+	default:
+		if (cx18_debug & CX18_DBGFLG_IOCTL) {
+			printk(KERN_INFO "cx18%d ioctl: unsupported cmd: ",
+				 cx->num);
+			v4l_printk_ioctl(cmd);
+			printk("\n");
+		}
+		return -EINVAL;
 	}
 	return 0;
 }
@@ -845,7 +840,55 @@
 	int res;
 
 	mutex_lock(&cx->serialize_lock);
-	res = video_usercopy(inode, filp, cmd, arg, cx18_v4l2_do_ioctl);
+	res = video_ioctl2(inode, filp, cmd, arg);
 	mutex_unlock(&cx->serialize_lock);
 	return res;
 }
+
+void cx18_set_funcs(struct video_device *vdev)
+{
+	vdev->vidioc_querycap                = cx18_querycap;
+	vdev->vidioc_g_priority              = cx18_g_priority;
+	vdev->vidioc_s_priority              = cx18_s_priority;
+	vdev->vidioc_s_audio                 = cx18_s_audio;
+	vdev->vidioc_g_audio                 = cx18_g_audio;
+	vdev->vidioc_enumaudio               = cx18_enumaudio;
+	vdev->vidioc_enum_input              = cx18_enum_input;
+	vdev->vidioc_cropcap                 = cx18_cropcap;
+	vdev->vidioc_s_crop                  = cx18_s_crop;
+	vdev->vidioc_g_crop                  = cx18_g_crop;
+	vdev->vidioc_g_input                 = cx18_g_input;
+	vdev->vidioc_s_input                 = cx18_s_input;
+	vdev->vidioc_g_frequency             = cx18_g_frequency;
+	vdev->vidioc_s_frequency             = cx18_s_frequency;
+	vdev->vidioc_s_tuner                 = cx18_s_tuner;
+	vdev->vidioc_g_tuner                 = cx18_g_tuner;
+	vdev->vidioc_g_enc_index             = cx18_g_enc_index;
+	vdev->vidioc_g_std                   = cx18_g_std;
+	vdev->vidioc_s_std                   = cx18_s_std;
+	vdev->vidioc_log_status              = cx18_log_status;
+	vdev->vidioc_enum_fmt_vid_cap        = cx18_enum_fmt_vid_cap;
+	vdev->vidioc_encoder_cmd             = cx18_encoder_cmd;
+	vdev->vidioc_try_encoder_cmd         = cx18_try_encoder_cmd;
+	vdev->vidioc_g_fmt_vid_cap           = cx18_g_fmt_vid_cap;
+	vdev->vidioc_g_fmt_vbi_cap           = cx18_g_fmt_vbi_cap;
+	vdev->vidioc_g_fmt_sliced_vbi_cap    = cx18_g_fmt_sliced_vbi_cap;
+	vdev->vidioc_s_fmt_vid_cap           = cx18_s_fmt_vid_cap;
+	vdev->vidioc_s_fmt_vbi_cap           = cx18_s_fmt_vbi_cap;
+	vdev->vidioc_s_fmt_sliced_vbi_cap    = cx18_s_fmt_sliced_vbi_cap;
+	vdev->vidioc_try_fmt_vid_cap         = cx18_try_fmt_vid_cap;
+	vdev->vidioc_try_fmt_vbi_cap         = cx18_try_fmt_vbi_cap;
+	vdev->vidioc_try_fmt_sliced_vbi_cap  = cx18_try_fmt_sliced_vbi_cap;
+	vdev->vidioc_g_sliced_vbi_cap        = cx18_g_sliced_vbi_cap;
+	vdev->vidioc_g_chip_ident            = cx18_g_chip_ident;
+	vdev->vidioc_g_register              = cx18_g_register;
+	vdev->vidioc_s_register              = cx18_s_register;
+	vdev->vidioc_default                 = cx18_default;
+	vdev->vidioc_queryctrl               = cx18_queryctrl;
+	vdev->vidioc_querymenu               = cx18_querymenu;
+	vdev->vidioc_g_ctrl                  = cx18_g_ctrl;
+	vdev->vidioc_s_ctrl                  = cx18_s_ctrl;
+	vdev->vidioc_g_ext_ctrls             = cx18_g_ext_ctrls;
+	vdev->vidioc_s_ext_ctrls             = cx18_s_ext_ctrls;
+	vdev->vidioc_try_ext_ctrls           = cx18_try_ext_ctrls;
+}