V4L/DVB (13509): pms: convert from V4L1 to V4L2.

Signed-off-by: Hans Verkuil <hverkuil@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
diff --git a/drivers/media/video/pms.c b/drivers/media/video/pms.c
index 2919aa4..73ec970 100644
--- a/drivers/media/video/pms.c
+++ b/drivers/media/video/pms.c
@@ -14,8 +14,10 @@
  *	unless the userspace driver also doesn't work for you...
  *
  *      Changes:
- *      08/07/2003        Daniele Bellucci <bellucda@tiscali.it>
- *                        - pms_capture: report back -EFAULT
+ *	25-11-2009 	Hans Verkuil <hverkuil@xs4all.nl>
+ * 			- converted to version 2 of the V4L API.
+ *      08/07/2003      Daniele Bellucci <bellucda@tiscali.it>
+ *                      - pms_capture: report back -EFAULT
  */
 
 #include <linux/module.h>
@@ -27,20 +29,21 @@
 #include <linux/mm.h>
 #include <linux/ioport.h>
 #include <linux/init.h>
+#include <linux/version.h>
+#include <linux/mutex.h>
+#include <asm/uaccess.h>
 #include <asm/io.h>
-#include <linux/videodev.h>
+
+#include <linux/videodev2.h>
 #include <media/v4l2-common.h>
 #include <media/v4l2-ioctl.h>
 #include <media/v4l2-device.h>
-#include <linux/mutex.h>
-
-#include <asm/uaccess.h>
 
 MODULE_LICENSE("GPL");
 
 
 #define MOTOROLA	1
-#define PHILIPS2	2
+#define PHILIPS2	2               /* SAA7191 */
 #define PHILIPS1	3
 #define MVVMEMORYWIDTH	0x40		/* 512 bytes */
 
@@ -54,9 +57,11 @@
 struct pms {
 	struct v4l2_device v4l2_dev;
 	struct video_device vdev;
-	struct video_picture picture;
 	int height;
 	int width;
+	int depth;
+	int input;
+	s32 brightness, saturation, hue, contrast;
 	unsigned long in_use;
 	struct mutex lock;
 	int i2c_count;
@@ -64,6 +69,7 @@
 
 	int decoder;
 	int standard;	/* 0 - auto 1 - ntsc 2 - pal 3 - secam */
+	v4l2_std_id std;
 	int io;
 	int data;
 	void __iomem *mem;
@@ -209,7 +215,19 @@
 
 static void pms_videosource(struct pms *dev, short source)
 {
-	mvv_write(dev, 0x2E, source ? 0x31 : 0x30);
+	switch (dev->decoder) {
+	case MOTOROLA:
+		break;
+	case PHILIPS2:
+		pms_i2c_andor(dev, 0x8a, 0x06, 0x7f, source ? 0x80 : 0);
+		break;
+	case PHILIPS1:
+		break;
+	}
+	mvv_write(dev, 0x2E, 0x31);
+	/* Was: mvv_write(dev, 0x2E, source ? 0x31 : 0x30);
+	   But could not make this work correctly. Only Composite input
+	   worked for me. */
 }
 
 static void pms_hue(struct pms *dev, short hue)
@@ -227,14 +245,14 @@
 	}
 }
 
-static void pms_colour(struct pms *dev, short colour)
+static void pms_saturation(struct pms *dev, short sat)
 {
 	switch (dev->decoder) {
 	case MOTOROLA:
-		pms_i2c_write(dev, 0x8a, 0x00, colour);
+		pms_i2c_write(dev, 0x8a, 0x00, sat);
 		break;
 	case PHILIPS1:
-		pms_i2c_write(dev, 0x42, 0x12, colour);
+		pms_i2c_write(dev, 0x42, 0x12, sat);
 		break;
 	}
 }
@@ -467,7 +485,7 @@
 
 static void pms_framerate(struct pms *dev, short frr)
 {
-	int fps = (dev->standard == 1) ? 30 : 25;
+	int fps = (dev->std & V4L2_STD_525_60) ? 30 : 25;
 
 	if (frr == 0)
 		return;
@@ -557,7 +575,7 @@
 	mvv_write(dev, 0x18, fg_height);
 	mvv_write(dev, 0x19, fg_height >> 8);
 
-	if (dev->standard == 1) {
+	if (dev->std & V4L2_STD_525_60) {
 		mvv_write(dev, 0x1a, 0xfc);
 		mvv_write(dev, 0x1b, 0x00);
 		if (height > fg_height)
@@ -581,7 +599,7 @@
 	mvv_write(dev, 0x22, width + 8);
 	mvv_write(dev, 0x23, (width + 8) >> 8);
 
-	if (dev->standard == 1)
+	if (dev->std & V4L2_STD_525_60)
 		pms_horzdeci(dev, width, 640);
 	else
 		pms_horzdeci(dev, width + 8, 768);
@@ -654,192 +672,252 @@
  *	Video4linux interfacing
  */
 
-static long pms_do_ioctl(struct file *file, unsigned int cmd, void *arg)
+static int pms_querycap(struct file *file, void  *priv,
+					struct v4l2_capability *vcap)
 {
 	struct pms *dev = video_drvdata(file);
 
-	switch (cmd) {
-	case VIDIOCGCAP: {
-		struct video_capability *b = arg;
-
-		strcpy(b->name, "Mediavision PMS");
-		b->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
-		b->channels = 4;
-		b->audios = 0;
-		b->maxwidth = 640;
-		b->maxheight = 480;
-		b->minwidth = 16;
-		b->minheight = 16;
-		return 0;
-	}
-	case VIDIOCGCHAN: {
-		struct video_channel *v = arg;
-
-		if (v->channel < 0 || v->channel > 3)
-			return -EINVAL;
-		v->flags = 0;
-		v->tuners = 1;
-		/* Good question.. its composite or SVHS so.. */
-		v->type = VIDEO_TYPE_CAMERA;
-		switch (v->channel) {
-		case 0:
-			strcpy(v->name, "Composite");
-			break;
-		case 1:
-			strcpy(v->name, "SVideo");
-			break;
-		case 2:
-			strcpy(v->name, "Composite(VCR)");
-			break;
-		case 3:
-			strcpy(v->name, "SVideo(VCR)");
-			break;
-		}
-		return 0;
-	}
-	case VIDIOCSCHAN: {
-		struct video_channel *v = arg;
-
-		if (v->channel < 0 || v->channel > 3)
-			return -EINVAL;
-		mutex_lock(&dev->lock);
-		pms_videosource(dev, v->channel & 1);
-		pms_vcrinput(dev, v->channel >> 1);
-		mutex_unlock(&dev->lock);
-		return 0;
-	}
-	case VIDIOCGTUNER: {
-		struct video_tuner *v = arg;
-
-		if (v->tuner)
-			return -EINVAL;
-		strcpy(v->name, "Format");
-		v->rangelow = 0;
-		v->rangehigh = 0;
-		v->flags = VIDEO_TUNER_PAL | VIDEO_TUNER_NTSC | VIDEO_TUNER_SECAM;
-		switch (dev->standard) {
-		case 0:
-			v->mode = VIDEO_MODE_AUTO;
-			break;
-		case 1:
-			v->mode = VIDEO_MODE_NTSC;
-			break;
-		case 2:
-			v->mode = VIDEO_MODE_PAL;
-			break;
-		case 3:
-			v->mode = VIDEO_MODE_SECAM;
-			break;
-		}
-		return 0;
-	}
-	case VIDIOCSTUNER: {
-		struct video_tuner *v = arg;
-
-		if (v->tuner)
-			return -EINVAL;
-		mutex_lock(&dev->lock);
-		switch (v->mode) {
-		case VIDEO_MODE_AUTO:
-			pms_framerate(dev, 25);
-			pms_secamcross(dev, 0);
-			pms_format(dev, 0);
-			break;
-		case VIDEO_MODE_NTSC:
-			pms_framerate(dev, 30);
-			pms_secamcross(dev, 0);
-			pms_format(dev, 1);
-			break;
-		case VIDEO_MODE_PAL:
-			pms_framerate(dev, 25);
-			pms_secamcross(dev, 0);
-			pms_format(dev, 2);
-			break;
-		case VIDEO_MODE_SECAM:
-			pms_framerate(dev, 25);
-			pms_secamcross(dev, 1);
-			pms_format(dev, 2);
-			break;
-		default:
-			mutex_unlock(&dev->lock);
-			return -EINVAL;
-		}
-		mutex_unlock(&dev->lock);
-		return 0;
-	}
-	case VIDIOCGPICT: {
-		struct video_picture *p = arg;
-
-		*p = dev->picture;
-		return 0;
-	}
-	case VIDIOCSPICT: {
-		struct video_picture *p = arg;
-
-		if (!((p->palette == VIDEO_PALETTE_RGB565 && p->depth == 16) ||
-		      (p->palette == VIDEO_PALETTE_RGB555 && p->depth == 15)))
-			return -EINVAL;
-		dev->picture = *p;
-
-		/*
-		 *	Now load the card.
-		 */
-
-		mutex_lock(&dev->lock);
-		pms_brightness(dev, p->brightness >> 8);
-		pms_hue(dev, p->hue >> 8);
-		pms_colour(dev, p->colour >> 8);
-		pms_contrast(dev, p->contrast >> 8);
-		mutex_unlock(&dev->lock);
-		return 0;
-	}
-	case VIDIOCSWIN: {
-		struct video_window *vw = arg;
-
-		if (vw->flags)
-			return -EINVAL;
-		if (vw->clipcount)
-			return -EINVAL;
-		if (vw->height < 16 || vw->height > 480)
-			return -EINVAL;
-		if (vw->width < 16 || vw->width > 640)
-			return -EINVAL;
-		dev->width = vw->width;
-		dev->height = vw->height;
-		mutex_lock(&dev->lock);
-		pms_resolution(dev, dev->width, dev->height);
-		/* Ok we figured out what to use from our wide choice */
-		mutex_unlock(&dev->lock);
-		return 0;
-	}
-	case VIDIOCGWIN: {
-		struct video_window *vw = arg;
-
-		memset(vw, 0, sizeof(*vw));
-		vw->width = dev->width;
-		vw->height = dev->height;
-		return 0;
-	}
-	case VIDIOCKEY:
-		return 0;
-	case VIDIOCCAPTURE:
-	case VIDIOCGFBUF:
-	case VIDIOCSFBUF:
-	case VIDIOCGFREQ:
-	case VIDIOCSFREQ:
-	case VIDIOCGAUDIO:
-	case VIDIOCSAUDIO:
-		return -EINVAL;
-	default:
-		return -ENOIOCTLCMD;
-	}
+	strlcpy(vcap->driver, dev->v4l2_dev.name, sizeof(vcap->driver));
+	strlcpy(vcap->card, "Mediavision PMS", sizeof(vcap->card));
+	strlcpy(vcap->bus_info, "ISA", sizeof(vcap->bus_info));
+	vcap->version = KERNEL_VERSION(0, 0, 3);
+	vcap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
 	return 0;
 }
 
-static long pms_ioctl(struct file *file,
-		     unsigned int cmd, unsigned long arg)
+static int pms_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
 {
-	return video_usercopy(file, cmd, arg, pms_do_ioctl);
+	static const char *inputs[4] = {
+		"Composite",
+		"S-Video",
+		"Composite (VCR)",
+		"S-Video (VCR)"
+	};
+
+	if (vin->index > 3)
+		return -EINVAL;
+	strlcpy(vin->name, inputs[vin->index], sizeof(vin->name));
+	vin->type = V4L2_INPUT_TYPE_CAMERA;
+	vin->audioset = 0;
+	vin->tuner = 0;
+	vin->std = V4L2_STD_ALL;
+	vin->status = 0;
+	return 0;
+}
+
+static int pms_g_input(struct file *file, void *fh, unsigned int *inp)
+{
+	struct pms *dev = video_drvdata(file);
+
+	*inp = dev->input;
+	return 0;
+}
+
+static int pms_s_input(struct file *file, void *fh, unsigned int inp)
+{
+	struct pms *dev = video_drvdata(file);
+
+	if (inp > 3)
+		return -EINVAL;
+
+	mutex_lock(&dev->lock);
+	dev->input = inp;
+	pms_videosource(dev, inp & 1);
+	pms_vcrinput(dev, inp >> 1);
+	mutex_unlock(&dev->lock);
+	return 0;
+}
+
+static int pms_g_std(struct file *file, void *fh, v4l2_std_id *std)
+{
+	struct pms *dev = video_drvdata(file);
+
+	*std = dev->std;
+	return 0;
+}
+
+static int pms_s_std(struct file *file, void *fh, v4l2_std_id *std)
+{
+	struct pms *dev = video_drvdata(file);
+	int ret = 0;
+
+	dev->std = *std;
+	mutex_lock(&dev->lock);
+	if (dev->std & V4L2_STD_NTSC) {
+		pms_framerate(dev, 30);
+		pms_secamcross(dev, 0);
+		pms_format(dev, 1);
+	} else if (dev->std & V4L2_STD_PAL) {
+		pms_framerate(dev, 25);
+		pms_secamcross(dev, 0);
+		pms_format(dev, 2);
+	} else if (dev->std & V4L2_STD_SECAM) {
+		pms_framerate(dev, 25);
+		pms_secamcross(dev, 1);
+		pms_format(dev, 2);
+	} else {
+		ret = -EINVAL;
+	}
+	/*
+	switch (v->mode) {
+	case VIDEO_MODE_AUTO:
+		pms_framerate(dev, 25);
+		pms_secamcross(dev, 0);
+		pms_format(dev, 0);
+		break;
+	}*/
+	mutex_unlock(&dev->lock);
+	return 0;
+}
+
+static int pms_queryctrl(struct file *file, void *priv,
+					struct v4l2_queryctrl *qc)
+{
+	switch (qc->id) {
+	case V4L2_CID_BRIGHTNESS:
+		return v4l2_ctrl_query_fill(qc, 0, 255, 1, 139);
+	case V4L2_CID_CONTRAST:
+		return v4l2_ctrl_query_fill(qc, 0, 255, 1, 70);
+	case V4L2_CID_SATURATION:
+		return v4l2_ctrl_query_fill(qc, 0, 255, 1, 64);
+	case V4L2_CID_HUE:
+		return v4l2_ctrl_query_fill(qc, 0, 255, 1, 0);
+	}
+	return -EINVAL;
+}
+
+static int pms_g_ctrl(struct file *file, void *priv,
+					struct v4l2_control *ctrl)
+{
+	struct pms *dev = video_drvdata(file);
+	int ret = 0;
+
+	switch (ctrl->id) {
+	case V4L2_CID_BRIGHTNESS:
+		ctrl->value = dev->brightness;
+		break;
+	case V4L2_CID_CONTRAST:
+		ctrl->value = dev->contrast;
+		break;
+	case V4L2_CID_SATURATION:
+		ctrl->value = dev->saturation;
+		break;
+	case V4L2_CID_HUE:
+		ctrl->value = dev->hue;
+		break;
+	default:
+		ret = -EINVAL;
+		break;
+	}
+	return ret;
+}
+
+static int pms_s_ctrl(struct file *file, void *priv,
+					struct v4l2_control *ctrl)
+{
+	struct pms *dev = video_drvdata(file);
+	int ret = 0;
+
+	mutex_lock(&dev->lock);
+	switch (ctrl->id) {
+	case V4L2_CID_BRIGHTNESS:
+		dev->brightness = ctrl->value;
+		pms_brightness(dev, dev->brightness);
+		break;
+	case V4L2_CID_CONTRAST:
+		dev->contrast = ctrl->value;
+		pms_contrast(dev, dev->contrast);
+		break;
+	case V4L2_CID_SATURATION:
+		dev->saturation = ctrl->value;
+		pms_saturation(dev, dev->saturation);
+		break;
+	case V4L2_CID_HUE:
+		dev->hue = ctrl->value;
+		pms_hue(dev, dev->hue);
+		break;
+	default:
+		ret = -EINVAL;
+		break;
+	}
+	mutex_unlock(&dev->lock);
+	return ret;
+}
+
+static int pms_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
+{
+	struct pms *dev = video_drvdata(file);
+	struct v4l2_pix_format *pix = &fmt->fmt.pix;
+
+	pix->width = dev->width;
+	pix->height = dev->height;
+	pix->pixelformat = dev->width == 15 ?
+			    V4L2_PIX_FMT_RGB555 : V4L2_PIX_FMT_RGB565;
+	pix->field = V4L2_FIELD_NONE;
+	pix->bytesperline = 2 * dev->width;
+	pix->sizeimage = 2 * dev->width * dev->height;
+	/* Just a guess */
+	pix->colorspace = V4L2_COLORSPACE_SRGB;
+	return 0;
+}
+
+static int pms_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
+{
+	struct v4l2_pix_format *pix = &fmt->fmt.pix;
+
+	if (pix->height < 16 || pix->height > 480)
+		return -EINVAL;
+	if (pix->width < 16 || pix->width > 640)
+		return -EINVAL;
+	if (pix->pixelformat != V4L2_PIX_FMT_RGB555 &&
+	    pix->pixelformat != V4L2_PIX_FMT_RGB565)
+		return -EINVAL;
+	pix->field = V4L2_FIELD_NONE;
+	pix->bytesperline = 2 * pix->width;
+	pix->sizeimage = 2 * pix->width * pix->height;
+	/* Just a guess */
+	pix->colorspace = V4L2_COLORSPACE_SRGB;
+	return 0;
+}
+
+static int pms_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
+{
+	struct pms *dev = video_drvdata(file);
+	struct v4l2_pix_format *pix = &fmt->fmt.pix;
+	int ret = pms_try_fmt_vid_cap(file, fh, fmt);
+
+	if (ret)
+		return ret;
+	mutex_lock(&dev->lock);
+	dev->width = pix->width;
+	dev->height = pix->height;
+	dev->depth = (pix->pixelformat == V4L2_PIX_FMT_RGB555) ? 15 : 16;
+	pms_resolution(dev, dev->width, dev->height);
+	/* Ok we figured out what to use from our wide choice */
+	mutex_unlock(&dev->lock);
+	return 0;
+}
+
+static int pms_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
+{
+	static struct v4l2_fmtdesc formats[] = {
+		{ 0, 0, 0,
+		  "RGB 5:5:5", V4L2_PIX_FMT_RGB555,
+		  { 0, 0, 0, 0 }
+		},
+		{ 0, 0, 0,
+		  "RGB 5:6:5", V4L2_PIX_FMT_RGB565,
+		  { 0, 0, 0, 0 }
+		},
+	};
+	enum v4l2_buf_type type = fmt->type;
+
+	if (fmt->index > 1)
+		return -EINVAL;
+
+	*fmt = formats[fmt->index];
+	fmt->type = type;
+	return 0;
 }
 
 static ssize_t pms_read(struct file *file, char __user *buf,
@@ -849,7 +927,7 @@
 	int len;
 
 	mutex_lock(&dev->lock);
-	len = pms_capture(dev, buf, (dev->picture.depth == 16) ? 0 : 1, count);
+	len = pms_capture(dev, buf, (dev->depth == 15), count);
 	mutex_unlock(&dev->lock);
 	return len;
 }
@@ -873,10 +951,25 @@
 	.owner		= THIS_MODULE,
 	.open           = pms_exclusive_open,
 	.release        = pms_exclusive_release,
-	.ioctl          = pms_ioctl,
+	.ioctl		= video_ioctl2,
 	.read           = pms_read,
 };
 
+static const struct v4l2_ioctl_ops pms_ioctl_ops = {
+	.vidioc_querycap    		    = pms_querycap,
+	.vidioc_g_input      		    = pms_g_input,
+	.vidioc_s_input      		    = pms_s_input,
+	.vidioc_enum_input   		    = pms_enum_input,
+	.vidioc_g_std 			    = pms_g_std,
+	.vidioc_s_std 			    = pms_s_std,
+	.vidioc_queryctrl 		    = pms_queryctrl,
+	.vidioc_g_ctrl  		    = pms_g_ctrl,
+	.vidioc_s_ctrl 			    = pms_s_ctrl,
+	.vidioc_enum_fmt_vid_cap 	    = pms_enum_fmt_vid_cap,
+	.vidioc_g_fmt_vid_cap 		    = pms_g_fmt_vid_cap,
+	.vidioc_s_fmt_vid_cap  		    = pms_s_fmt_vid_cap,
+	.vidioc_try_fmt_vid_cap  	    = pms_try_fmt_vid_cap,
+};
 
 /*
  *	Probe for and initialise the Mediavision PMS
@@ -1032,11 +1125,18 @@
 	strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
 	dev->vdev.v4l2_dev = v4l2_dev;
 	dev->vdev.fops = &pms_fops;
+	dev->vdev.ioctl_ops = &pms_ioctl_ops;
 	dev->vdev.release = video_device_release_empty;
 	video_set_drvdata(&dev->vdev, dev);
 	mutex_init(&dev->lock);
+	dev->std = V4L2_STD_NTSC_M;
 	dev->height = 240;
 	dev->width = 320;
+	dev->depth = 15;
+	dev->brightness = 139;
+	dev->contrast = 70;
+	dev->hue = 0;
+	dev->saturation = 64;
 	pms_swsense(dev, 75);
 	pms_resolution(dev, 320, 240);
 	pms_videosource(dev, 0);