Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs-2.6
[pandora-kernel.git] / drivers / media / video / bw-qcam.c
index 3c9e754..935e0c9 100644 (file)
@@ -66,19 +66,58 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include <linux/delay.h>
 #include <linux/errno.h>
 #include <linux/fs.h>
-#include <linux/init.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
 #include <linux/mm.h>
 #include <linux/parport.h>
 #include <linux/sched.h>
-#include <linux/videodev.h>
-#include <media/v4l2-common.h>
-#include <media/v4l2-ioctl.h>
+#include <linux/version.h>
+#include <linux/videodev2.h>
 #include <linux/mutex.h>
 #include <asm/uaccess.h>
-
-#include "bw-qcam.h"
+#include <media/v4l2-common.h>
+#include <media/v4l2-ioctl.h>
+#include <media/v4l2-device.h>
+
+/* One from column A... */
+#define QC_NOTSET 0
+#define QC_UNIDIR 1
+#define QC_BIDIR  2
+#define QC_SERIAL 3
+
+/* ... and one from column B */
+#define QC_ANY          0x00
+#define QC_FORCE_UNIDIR 0x10
+#define QC_FORCE_BIDIR  0x20
+#define QC_FORCE_SERIAL 0x30
+/* in the port_mode member */
+
+#define QC_MODE_MASK    0x07
+#define QC_FORCE_MASK   0x70
+
+#define MAX_HEIGHT 243
+#define MAX_WIDTH 336
+
+/* Bit fields for status flags */
+#define QC_PARAM_CHANGE        0x01 /* Camera status change has occurred */
+
+struct qcam {
+       struct v4l2_device v4l2_dev;
+       struct video_device vdev;
+       struct pardevice *pdev;
+       struct parport *pport;
+       struct mutex lock;
+       int width, height;
+       int bpp;
+       int mode;
+       int contrast, brightness, whitebal;
+       int port_mode;
+       int transfer_scale;
+       int top, left;
+       int status;
+       unsigned int saved_bits;
+       unsigned long in_use;
+};
 
 static unsigned int maxpoll = 250;   /* Maximum busy-loop count for qcam I/O */
 static unsigned int yieldlines = 4;  /* Yield after this many during capture */
@@ -93,22 +132,26 @@ module_param(video_nr, int, 0);
  * immediately attempt to initialize qcam */
 module_param(force_init, int, 0);
 
-static inline int read_lpstatus(struct qcam_device *q)
+#define MAX_CAMS 4
+static struct qcam *qcams[MAX_CAMS];
+static unsigned int num_cams;
+
+static inline int read_lpstatus(struct qcam *q)
 {
        return parport_read_status(q->pport);
 }
 
-static inline int read_lpdata(struct qcam_device *q)
+static inline int read_lpdata(struct qcam *q)
 {
        return parport_read_data(q->pport);
 }
 
-static inline void write_lpdata(struct qcam_device *q, int d)
+static inline void write_lpdata(struct qcam *q, int d)
 {
        parport_write_data(q->pport, d);
 }
 
-static inline void write_lpcontrol(struct qcam_device *q, int d)
+static void write_lpcontrol(struct qcam *q, int d)
 {
        if (d & 0x20) {
                /* Set bidirectional mode to reverse (data in) */
@@ -124,126 +167,11 @@ static inline void write_lpcontrol(struct qcam_device *q, int d)
        parport_write_control(q->pport, d);
 }
 
-static int qc_waithand(struct qcam_device *q, int val);
-static int qc_command(struct qcam_device *q, int command);
-static int qc_readparam(struct qcam_device *q);
-static int qc_setscanmode(struct qcam_device *q);
-static int qc_readbytes(struct qcam_device *q, char buffer[]);
-
-static struct video_device qcam_template;
-
-static int qc_calibrate(struct qcam_device *q)
-{
-       /*
-        *      Bugfix by Hanno Mueller hmueller@kabel.de, Mai 21 96
-        *      The white balance is an individiual value for each
-        *      quickcam.
-        */
-
-       int value;
-       int count = 0;
-
-       qc_command(q, 27);      /* AutoAdjustOffset */
-       qc_command(q, 0);       /* Dummy Parameter, ignored by the camera */
-
-       /* GetOffset (33) will read 255 until autocalibration */
-       /* is finished. After that, a value of 1-254 will be */
-       /* returned. */
-
-       do {
-               qc_command(q, 33);
-               value = qc_readparam(q);
-               mdelay(1);
-               schedule();
-               count++;
-       } while (value == 0xff && count < 2048);
-
-       q->whitebal = value;
-       return value;
-}
-
-/* Initialize the QuickCam driver control structure.  This is where
- * defaults are set for people who don't have a config file.*/
-
-static struct qcam_device *qcam_init(struct parport *port)
-{
-       struct qcam_device *q;
-
-       q = kmalloc(sizeof(struct qcam_device), GFP_KERNEL);
-       if (q == NULL)
-               return NULL;
-
-       q->pport = port;
-       q->pdev = parport_register_device(port, "bw-qcam", NULL, NULL,
-                       NULL, 0, NULL);
-       if (q->pdev == NULL) {
-               printk(KERN_ERR "bw-qcam: couldn't register for %s.\n",
-                               port->name);
-               kfree(q);
-               return NULL;
-       }
-
-       memcpy(&q->vdev, &qcam_template, sizeof(qcam_template));
-
-       mutex_init(&q->lock);
-
-       q->port_mode = (QC_ANY | QC_NOTSET);
-       q->width = 320;
-       q->height = 240;
-       q->bpp = 4;
-       q->transfer_scale = 2;
-       q->contrast = 192;
-       q->brightness = 180;
-       q->whitebal = 105;
-       q->top = 1;
-       q->left = 14;
-       q->mode = -1;
-       q->status = QC_PARAM_CHANGE;
-       return q;
-}
-
-
-/* qc_command is probably a bit of a misnomer -- it's used to send
- * bytes *to* the camera.  Generally, these bytes are either commands
- * or arguments to commands, so the name fits, but it still bugs me a
- * bit.  See the documentation for a list of commands. */
-
-static int qc_command(struct qcam_device *q, int command)
-{
-       int n1, n2;
-       int cmd;
-
-       write_lpdata(q, command);
-       write_lpcontrol(q, 6);
-
-       n1 = qc_waithand(q, 1);
-
-       write_lpcontrol(q, 0xe);
-       n2 = qc_waithand(q, 0);
-
-       cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
-       return cmd;
-}
-
-static int qc_readparam(struct qcam_device *q)
-{
-       int n1, n2;
-       int cmd;
-
-       write_lpcontrol(q, 6);
-       n1 = qc_waithand(q, 1);
-
-       write_lpcontrol(q, 0xe);
-       n2 = qc_waithand(q, 0);
-
-       cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
-       return cmd;
-}
 
 /* qc_waithand busy-waits for a handshake signal from the QuickCam.
  * Almost all communication with the camera requires handshaking. */
 
-static int qc_waithand(struct qcam_device *q, int val)
+static int qc_waithand(struct qcam *q, int val)
 {
        int status;
        int runs = 0;
@@ -286,7 +214,7 @@ static int qc_waithand(struct qcam_device *q, int val)
  * (bit 3 of status register).  It also returns the last value read,
  * since this data is useful. */
 
-static unsigned int qc_waithand2(struct qcam_device *q, int val)
+static unsigned int qc_waithand2(struct qcam *q, int val)
 {
        unsigned int status;
        int runs = 0;
@@ -309,6 +237,43 @@ static unsigned int qc_waithand2(struct qcam_device *q, int val)
        return status;
 }
 
+/* qc_command is probably a bit of a misnomer -- it's used to send
+ * bytes *to* the camera.  Generally, these bytes are either commands
+ * or arguments to commands, so the name fits, but it still bugs me a
+ * bit.  See the documentation for a list of commands. */
+
+static int qc_command(struct qcam *q, int command)
+{
+       int n1, n2;
+       int cmd;
+
+       write_lpdata(q, command);
+       write_lpcontrol(q, 6);
+
+       n1 = qc_waithand(q, 1);
+
+       write_lpcontrol(q, 0xe);
+       n2 = qc_waithand(q, 0);
+
+       cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
+       return cmd;
+}
+
+static int qc_readparam(struct qcam *q)
+{
+       int n1, n2;
+       int cmd;
+
+       write_lpcontrol(q, 6);
+       n1 = qc_waithand(q, 1);
+
+       write_lpcontrol(q, 0xe);
+       n2 = qc_waithand(q, 0);
+
+       cmd = (n1 & 0xf0) | ((n2 & 0xf0) >> 4);
+       return cmd;
+}
+
 
 /* Try to detect a QuickCam.  It appears to flash the upper 4 bits of
    the status register at 5-10 Hz.  This is only used in the autoprobe
@@ -317,7 +282,7 @@ static unsigned int qc_waithand2(struct qcam_device *q, int val)
    almost completely safe, while their method screws up my printer if
    I plug it in before the camera. */
 
-static int qc_detect(struct qcam_device *q)
+static int qc_detect(struct qcam *q)
 {
        int reg, lastreg;
        int count = 0;
@@ -358,41 +323,6 @@ static int qc_detect(struct qcam_device *q)
        }
 }
 
-
-/* Reset the QuickCam.  This uses the same sequence the Windows
- * QuickPic program uses.  Someone with a bi-directional port should
- * check that bi-directional mode is detected right, and then
- * implement bi-directional mode in qc_readbyte(). */
-
-static void qc_reset(struct qcam_device *q)
-{
-       switch (q->port_mode & QC_FORCE_MASK) {
-       case QC_FORCE_UNIDIR:
-               q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
-               break;
-
-       case QC_FORCE_BIDIR:
-               q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
-               break;
-
-       case QC_ANY:
-               write_lpcontrol(q, 0x20);
-               write_lpdata(q, 0x75);
-
-               if (read_lpdata(q) != 0x75)
-                       q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
-               else
-                       q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
-               break;
-       }
-
-       write_lpcontrol(q, 0xb);
-       udelay(250);
-       write_lpcontrol(q, 0xe);
-       qc_setscanmode(q);              /* in case port_mode changed */
-}
-
-
 /* Decide which scan mode to use.  There's no real requirement that
  * the scanmode match the resolution in q->height and q-> width -- the
  * camera takes the picture at the resolution specified in the
@@ -402,7 +332,7 @@ static void qc_reset(struct qcam_device *q)
  * returned.  If the scan is smaller, then the rest of the image
  * returned contains garbage. */
 
-static int qc_setscanmode(struct qcam_device *q)
+static int qc_setscanmode(struct qcam *q)
 {
        int old_mode = q->mode;
 
@@ -442,10 +372,45 @@ static int qc_setscanmode(struct qcam_device *q)
 }
 
 
+/* Reset the QuickCam.  This uses the same sequence the Windows
+ * QuickPic program uses.  Someone with a bi-directional port should
+ * check that bi-directional mode is detected right, and then
+ * implement bi-directional mode in qc_readbyte(). */
+
+static void qc_reset(struct qcam *q)
+{
+       switch (q->port_mode & QC_FORCE_MASK) {
+       case QC_FORCE_UNIDIR:
+               q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
+               break;
+
+       case QC_FORCE_BIDIR:
+               q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
+               break;
+
+       case QC_ANY:
+               write_lpcontrol(q, 0x20);
+               write_lpdata(q, 0x75);
+
+               if (read_lpdata(q) != 0x75)
+                       q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_BIDIR;
+               else
+                       q->port_mode = (q->port_mode & ~QC_MODE_MASK) | QC_UNIDIR;
+               break;
+       }
+
+       write_lpcontrol(q, 0xb);
+       udelay(250);
+       write_lpcontrol(q, 0xe);
+       qc_setscanmode(q);              /* in case port_mode changed */
+}
+
+
+
 /* Reset the QuickCam and program for brightness, contrast,
  * white-balance, and resolution. */
 
-static void qc_set(struct qcam_device *q)
+static void qc_set(struct qcam *q)
 {
        int val;
        int val2;
@@ -499,7 +464,7 @@ static void qc_set(struct qcam_device *q)
    the supplied buffer.  It returns the number of bytes read,
    or -1 on error. */
 
-static inline int qc_readbytes(struct qcam_device *q, char buffer[])
+static inline int qc_readbytes(struct qcam *q, char buffer[])
 {
        int ret = 1;
        unsigned int hi, lo;
@@ -590,7 +555,7 @@ static inline int qc_readbytes(struct qcam_device *q, char buffer[])
  * n=2^(bit depth)-1.  Ask me for more details if you don't understand
  * this. */
 
-static long qc_capture(struct qcam_device *q, char __user *buf, unsigned long len)
+static long qc_capture(struct qcam *q, char __user *buf, unsigned long len)
 {
        int i, j, k, yield;
        int bytes;
@@ -674,171 +639,206 @@ static long qc_capture(struct qcam_device *q, char __user *buf, unsigned long le
  *     Video4linux interfacing
  */
 
-static long qcam_do_ioctl(struct file *file, unsigned int cmd, void *arg)
+static int qcam_querycap(struct file *file, void  *priv,
+                                       struct v4l2_capability *vcap)
 {
-       struct video_device *dev = video_devdata(file);
-       struct qcam_device *qcam = (struct qcam_device *)dev;
-
-       switch (cmd) {
-       case VIDIOCGCAP:
-               {
-                       struct video_capability *b = arg;
-                       strcpy(b->name, "Quickcam");
-                       b->type = VID_TYPE_CAPTURE|VID_TYPE_SCALES|VID_TYPE_MONOCHROME;
-                       b->channels = 1;
-                       b->audios = 0;
-                       b->maxwidth = 320;
-                       b->maxheight = 240;
-                       b->minwidth = 80;
-                       b->minheight = 60;
-                       return 0;
-               }
-       case VIDIOCGCHAN:
-               {
-                       struct video_channel *v = arg;
-                       if (v->channel != 0)
-                               return -EINVAL;
-                       v->flags = 0;
-                       v->tuners = 0;
-                       /* Good question.. its composite or SVHS so.. */
-                       v->type = VIDEO_TYPE_CAMERA;
-                       strcpy(v->name, "Camera");
-                       return 0;
-               }
-       case VIDIOCSCHAN:
-               {
-                       struct video_channel *v = arg;
-                       if (v->channel != 0)
-                               return -EINVAL;
-                       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 = 0;
-                       v->mode = VIDEO_MODE_AUTO;
-                       return 0;
-               }
-       case VIDIOCSTUNER:
-               {
-                       struct video_tuner *v = arg;
-                       if (v->tuner)
-                               return -EINVAL;
-                       if (v->mode != VIDEO_MODE_AUTO)
-                               return -EINVAL;
-                       return 0;
-               }
-       case VIDIOCGPICT:
-               {
-                       struct video_picture *p = arg;
-                       p->colour = 0x8000;
-                       p->hue = 0x8000;
-                       p->brightness = qcam->brightness << 8;
-                       p->contrast = qcam->contrast << 8;
-                       p->whiteness = qcam->whitebal << 8;
-                       p->depth = qcam->bpp;
-                       p->palette = VIDEO_PALETTE_GREY;
-                       return 0;
-               }
-       case VIDIOCSPICT:
-               {
-                       struct video_picture *p = arg;
-                       if (p->palette != VIDEO_PALETTE_GREY)
-                               return -EINVAL;
-                       if (p->depth != 4 && p->depth != 6)
-                               return -EINVAL;
-
-                       /*
-                        *      Now load the camera.
-                        */
-
-                       qcam->brightness = p->brightness >> 8;
-                       qcam->contrast = p->contrast >> 8;
-                       qcam->whitebal = p->whiteness >> 8;
-                       qcam->bpp = p->depth;
-
-                       mutex_lock(&qcam->lock);
-                       qc_setscanmode(qcam);
-                       mutex_unlock(&qcam->lock);
-                       qcam->status |= QC_PARAM_CHANGE;
+       struct qcam *qcam = video_drvdata(file);
 
-                       return 0;
-               }
-       case VIDIOCSWIN:
-               {
-                       struct video_window *vw = arg;
-                       if (vw->flags)
-                               return -EINVAL;
-                       if (vw->clipcount)
-                               return -EINVAL;
-                       if (vw->height < 60 || vw->height > 240)
-                               return -EINVAL;
-                       if (vw->width < 80 || vw->width > 320)
-                               return -EINVAL;
-
-                       qcam->width = 320;
-                       qcam->height = 240;
-                       qcam->transfer_scale = 4;
-
-                       if (vw->width >= 160 && vw->height >= 120)
-                               qcam->transfer_scale = 2;
-                       if (vw->width >= 320 && vw->height >= 240) {
-                               qcam->width = 320;
-                               qcam->height = 240;
-                               qcam->transfer_scale = 1;
-                       }
-                       mutex_lock(&qcam->lock);
-                       qc_setscanmode(qcam);
-                       mutex_unlock(&qcam->lock);
+       strlcpy(vcap->driver, qcam->v4l2_dev.name, sizeof(vcap->driver));
+       strlcpy(vcap->card, "B&W Quickcam", sizeof(vcap->card));
+       strlcpy(vcap->bus_info, "parport", sizeof(vcap->bus_info));
+       vcap->version = KERNEL_VERSION(0, 0, 2);
+       vcap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
+       return 0;
+}
 
-                       /* We must update the camera before we grab. We could
-                          just have changed the grab size */
-                       qcam->status |= QC_PARAM_CHANGE;
+static int qcam_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
+{
+       if (vin->index > 0)
+               return -EINVAL;
+       strlcpy(vin->name, "Camera", sizeof(vin->name));
+       vin->type = V4L2_INPUT_TYPE_CAMERA;
+       vin->audioset = 0;
+       vin->tuner = 0;
+       vin->std = 0;
+       vin->status = 0;
+       return 0;
+}
 
-                       /* Ok we figured out what to use from our wide choice */
-                       return 0;
-               }
-       case VIDIOCGWIN:
-               {
-                       struct video_window *vw = arg;
+static int qcam_g_input(struct file *file, void *fh, unsigned int *inp)
+{
+       *inp = 0;
+       return 0;
+}
 
-                       memset(vw, 0, sizeof(*vw));
-                       vw->width = qcam->width / qcam->transfer_scale;
-                       vw->height = qcam->height / qcam->transfer_scale;
-                       return 0;
-               }
-       case VIDIOCKEY:
-               return 0;
-       case VIDIOCCAPTURE:
-       case VIDIOCGFBUF:
-       case VIDIOCSFBUF:
-       case VIDIOCGFREQ:
-       case VIDIOCSFREQ:
-       case VIDIOCGAUDIO:
-       case VIDIOCSAUDIO:
-               return -EINVAL;
+static int qcam_s_input(struct file *file, void *fh, unsigned int inp)
+{
+       return (inp > 0) ? -EINVAL : 0;
+}
+
+static int qcam_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, 180);
+       case V4L2_CID_CONTRAST:
+               return v4l2_ctrl_query_fill(qc, 0, 255, 1, 192);
+       case V4L2_CID_GAMMA:
+               return v4l2_ctrl_query_fill(qc, 0, 255, 1, 105);
+       }
+       return -EINVAL;
+}
+
+static int qcam_g_ctrl(struct file *file, void *priv,
+                                       struct v4l2_control *ctrl)
+{
+       struct qcam *qcam = video_drvdata(file);
+       int ret = 0;
+
+       switch (ctrl->id) {
+       case V4L2_CID_BRIGHTNESS:
+               ctrl->value = qcam->brightness;
+               break;
+       case V4L2_CID_CONTRAST:
+               ctrl->value = qcam->contrast;
+               break;
+       case V4L2_CID_GAMMA:
+               ctrl->value = qcam->whitebal;
+               break;
        default:
-               return -ENOIOCTLCMD;
+               ret = -EINVAL;
+               break;
        }
+       return ret;
+}
+
+static int qcam_s_ctrl(struct file *file, void *priv,
+                                       struct v4l2_control *ctrl)
+{
+       struct qcam *qcam = video_drvdata(file);
+       int ret = 0;
+
+       mutex_lock(&qcam->lock);
+       switch (ctrl->id) {
+       case V4L2_CID_BRIGHTNESS:
+               qcam->brightness = ctrl->value;
+               break;
+       case V4L2_CID_CONTRAST:
+               qcam->contrast = ctrl->value;
+               break;
+       case V4L2_CID_GAMMA:
+               qcam->whitebal = ctrl->value;
+               break;
+       default:
+               ret = -EINVAL;
+               break;
+       }
+       if (ret == 0) {
+               qc_setscanmode(qcam);
+               qcam->status |= QC_PARAM_CHANGE;
+       }
+       mutex_unlock(&qcam->lock);
+       return ret;
+}
+
+static int qcam_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
+{
+       struct qcam *qcam = video_drvdata(file);
+       struct v4l2_pix_format *pix = &fmt->fmt.pix;
+
+       pix->width = qcam->width / qcam->transfer_scale;
+       pix->height = qcam->height / qcam->transfer_scale;
+       pix->pixelformat = (qcam->bpp == 4) ? V4L2_PIX_FMT_Y4 : V4L2_PIX_FMT_Y6;
+       pix->field = V4L2_FIELD_NONE;
+       pix->bytesperline = qcam->width;
+       pix->sizeimage = qcam->width * qcam->height;
+       /* Just a guess */
+       pix->colorspace = V4L2_COLORSPACE_SRGB;
+       return 0;
+}
+
+static int qcam_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
+{
+       struct v4l2_pix_format *pix = &fmt->fmt.pix;
+
+       if (pix->height <= 60 || pix->width <= 80) {
+               pix->height = 60;
+               pix->width = 80;
+       } else if (pix->height <= 120 || pix->width <= 160) {
+               pix->height = 120;
+               pix->width = 160;
+       } else {
+               pix->height = 240;
+               pix->width = 320;
+       }
+       if (pix->pixelformat != V4L2_PIX_FMT_Y4 &&
+           pix->pixelformat != V4L2_PIX_FMT_Y6)
+               pix->pixelformat = V4L2_PIX_FMT_Y4;
+       pix->field = V4L2_FIELD_NONE;
+       pix->bytesperline = pix->width;
+       pix->sizeimage = pix->width * pix->height;
+       /* Just a guess */
+       pix->colorspace = V4L2_COLORSPACE_SRGB;
+       return 0;
+}
+
+static int qcam_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
+{
+       struct qcam *qcam = video_drvdata(file);
+       struct v4l2_pix_format *pix = &fmt->fmt.pix;
+       int ret = qcam_try_fmt_vid_cap(file, fh, fmt);
+
+       if (ret)
+               return ret;
+       qcam->width = 320;
+       qcam->height = 240;
+       if (pix->height == 60)
+               qcam->transfer_scale = 4;
+       else if (pix->height == 120)
+               qcam->transfer_scale = 2;
+       else
+               qcam->transfer_scale = 1;
+       if (pix->pixelformat == V4L2_PIX_FMT_Y6)
+               qcam->bpp = 6;
+       else
+               qcam->bpp = 4;
+
+       mutex_lock(&qcam->lock);
+       qc_setscanmode(qcam);
+       /* We must update the camera before we grab. We could
+          just have changed the grab size */
+       qcam->status |= QC_PARAM_CHANGE;
+       mutex_unlock(&qcam->lock);
        return 0;
 }
 
-static long qcam_ioctl(struct file *file,
-               unsigned int cmd, unsigned long arg)
+static int qcam_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
 {
-       return video_usercopy(file, cmd, arg, qcam_do_ioctl);
+       static struct v4l2_fmtdesc formats[] = {
+               { 0, 0, 0,
+                 "4-Bit Monochrome", V4L2_PIX_FMT_Y4,
+                 { 0, 0, 0, 0 }
+               },
+               { 0, 0, 0,
+                 "6-Bit Monochrome", V4L2_PIX_FMT_Y6,
+                 { 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 qcam_read(struct file *file, char __user *buf,
                size_t count, loff_t *ppos)
 {
-       struct video_device *v = video_devdata(file);
-       struct qcam_device *qcam = (struct qcam_device *)v;
+       struct qcam *qcam = video_drvdata(file);
        int len;
        parport_claim_or_block(qcam->pdev);
 
@@ -858,43 +858,112 @@ static ssize_t qcam_read(struct file *file, char __user *buf,
        return len;
 }
 
-static int qcam_exclusive_open(struct file *file)
+static const struct v4l2_file_operations qcam_fops = {
+       .owner          = THIS_MODULE,
+       .ioctl          = video_ioctl2,
+       .read           = qcam_read,
+};
+
+static const struct v4l2_ioctl_ops qcam_ioctl_ops = {
+       .vidioc_querycap                    = qcam_querycap,
+       .vidioc_g_input                     = qcam_g_input,
+       .vidioc_s_input                     = qcam_s_input,
+       .vidioc_enum_input                  = qcam_enum_input,
+       .vidioc_queryctrl                   = qcam_queryctrl,
+       .vidioc_g_ctrl                      = qcam_g_ctrl,
+       .vidioc_s_ctrl                      = qcam_s_ctrl,
+       .vidioc_enum_fmt_vid_cap            = qcam_enum_fmt_vid_cap,
+       .vidioc_g_fmt_vid_cap               = qcam_g_fmt_vid_cap,
+       .vidioc_s_fmt_vid_cap               = qcam_s_fmt_vid_cap,
+       .vidioc_try_fmt_vid_cap             = qcam_try_fmt_vid_cap,
+};
+
+/* Initialize the QuickCam driver control structure.  This is where
+ * defaults are set for people who don't have a config file.*/
+
+static struct qcam *qcam_init(struct parport *port)
 {
-       struct video_device *dev = video_devdata(file);
-       struct qcam_device *qcam = (struct qcam_device *)dev;
+       struct qcam *qcam;
+       struct v4l2_device *v4l2_dev;
+
+       qcam = kzalloc(sizeof(struct qcam), GFP_KERNEL);
+       if (qcam == NULL)
+               return NULL;
+
+       v4l2_dev = &qcam->v4l2_dev;
+       strlcpy(v4l2_dev->name, "bw-qcam", sizeof(v4l2_dev->name));
 
-       return test_and_set_bit(0, &qcam->in_use) ? -EBUSY : 0;
+       if (v4l2_device_register(NULL, v4l2_dev) < 0) {
+               v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
+               return NULL;
+       }
+
+       qcam->pport = port;
+       qcam->pdev = parport_register_device(port, "bw-qcam", NULL, NULL,
+                       NULL, 0, NULL);
+       if (qcam->pdev == NULL) {
+               v4l2_err(v4l2_dev, "couldn't register for %s.\n", port->name);
+               kfree(qcam);
+               return NULL;
+       }
+
+       strlcpy(qcam->vdev.name, "Connectix QuickCam", sizeof(qcam->vdev.name));
+       qcam->vdev.v4l2_dev = v4l2_dev;
+       qcam->vdev.fops = &qcam_fops;
+       qcam->vdev.ioctl_ops = &qcam_ioctl_ops;
+       qcam->vdev.release = video_device_release_empty;
+       video_set_drvdata(&qcam->vdev, qcam);
+
+       mutex_init(&qcam->lock);
+
+       qcam->port_mode = (QC_ANY | QC_NOTSET);
+       qcam->width = 320;
+       qcam->height = 240;
+       qcam->bpp = 4;
+       qcam->transfer_scale = 2;
+       qcam->contrast = 192;
+       qcam->brightness = 180;
+       qcam->whitebal = 105;
+       qcam->top = 1;
+       qcam->left = 14;
+       qcam->mode = -1;
+       qcam->status = QC_PARAM_CHANGE;
+       return qcam;
 }
 
-static int qcam_exclusive_release(struct file *file)
+static int qc_calibrate(struct qcam *q)
 {
-       struct video_device *dev = video_devdata(file);
-       struct qcam_device *qcam = (struct qcam_device *)dev;
+       /*
+        *      Bugfix by Hanno Mueller hmueller@kabel.de, Mai 21 96
+        *      The white balance is an individual value for each
+        *      quickcam.
+        */
 
-       clear_bit(0, &qcam->in_use);
-       return 0;
-}
+       int value;
+       int count = 0;
 
-static const struct v4l2_file_operations qcam_fops = {
-       .owner          = THIS_MODULE,
-       .open           = qcam_exclusive_open,
-       .release        = qcam_exclusive_release,
-       .ioctl          = qcam_ioctl,
-       .read           = qcam_read,
-};
-static struct video_device qcam_template = {
-       .name           = "Connectix Quickcam",
-       .fops           = &qcam_fops,
-       .release        = video_device_release_empty,
-};
+       qc_command(q, 27);      /* AutoAdjustOffset */
+       qc_command(q, 0);       /* Dummy Parameter, ignored by the camera */
 
-#define MAX_CAMS 4
-static struct qcam_device *qcams[MAX_CAMS];
-static unsigned int num_cams;
+       /* GetOffset (33) will read 255 until autocalibration */
+       /* is finished. After that, a value of 1-254 will be */
+       /* returned. */
+
+       do {
+               qc_command(q, 33);
+               value = qc_readparam(q);
+               mdelay(1);
+               schedule();
+               count++;
+       } while (value == 0xff && count < 2048);
+
+       q->whitebal = value;
+       return value;
+}
 
 static int init_bwqcam(struct parport *port)
 {
-       struct qcam_device *qcam;
+       struct qcam *qcam;
 
        if (num_cams == MAX_CAMS) {
                printk(KERN_ERR "Too many Quickcams (max %d)\n", MAX_CAMS);
@@ -919,7 +988,7 @@ static int init_bwqcam(struct parport *port)
 
        parport_release(qcam->pdev);
 
-       printk(KERN_INFO "Connectix Quickcam on %s\n", qcam->pport->name);
+       v4l2_info(&qcam->v4l2_dev, "Connectix Quickcam on %s\n", qcam->pport->name);
 
        if (video_register_device(&qcam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
                parport_unregister_device(qcam->pdev);
@@ -932,7 +1001,7 @@ static int init_bwqcam(struct parport *port)
        return 0;
 }
 
-static void close_bwqcam(struct qcam_device *qcam)
+static void close_bwqcam(struct qcam *qcam)
 {
        video_unregister_device(&qcam->vdev);
        parport_unregister_device(qcam->pdev);
@@ -983,7 +1052,7 @@ static void bwqcam_detach(struct parport *port)
 {
        int i;
        for (i = 0; i < num_cams; i++) {
-               struct qcam_device *qcam = qcams[i];
+               struct qcam *qcam = qcams[i];
                if (qcam && qcam->pdev->port == port) {
                        qcams[i] = NULL;
                        close_bwqcam(qcam);