V4L/DVB (8709): gspca: Fix initialization and controls of sn9x110 - ov7630.
[safe/jmp/linux-2.6] / drivers / media / video / gspca / pac207.c
index ab83d7b..cee45b0 100644 (file)
@@ -27,9 +27,6 @@
 
 #include "gspca.h"
 
-#define DRIVER_VERSION_NUMBER  KERNEL_VERSION(2, 1, 4)
-static const char version[] = "2.1.4";
-
 MODULE_AUTHOR("Hans de Goede <j.w.r.degoede@hhs.nl>");
 MODULE_DESCRIPTION("Pixart PAC207");
 MODULE_LICENSE("GPL");
@@ -157,9 +154,20 @@ static struct ctrl sd_ctrls[] = {
        },
 };
 
-static struct cam_mode sif_mode[] = {
-       {V4L2_PIX_FMT_PAC207, 176, 144, 1},
-       {V4L2_PIX_FMT_PAC207, 352, 288, 0},
+static struct v4l2_pix_format sif_mode[] = {
+       {176, 144, V4L2_PIX_FMT_PAC207, V4L2_FIELD_NONE,
+               .bytesperline = 176,
+               .sizeimage = (176 + 2) * 144,
+                       /* uncompressed, add 2 bytes / line for line header */
+               .colorspace = V4L2_COLORSPACE_SRGB,
+               .priv = 1},
+       {352, 288, V4L2_PIX_FMT_PAC207, V4L2_FIELD_NONE,
+               .bytesperline = 352,
+                       /* compressed, but only when needed (not compressed
+                          when the framerate is low) */
+               .sizeimage = (352 + 2) * 288,
+               .colorspace = V4L2_COLORSPACE_SRGB,
+               .priv = 0},
 };
 
 static const __u8 pac207_sensor_init[][8] = {
@@ -176,18 +184,18 @@ static const __u8 PacReg72[] = { 0x00, 0x00, 0x36, 0x00 };
 static const unsigned char pac207_sof_marker[5] =
                { 0xff, 0xff, 0x00, 0xff, 0x96 };
 
-int pac207_write_regs(struct gspca_dev *gspca_dev, u16 index,
+static int pac207_write_regs(struct gspca_dev *gspca_dev, u16 index,
        const u8 *buffer, u16 length)
 {
        struct usb_device *udev = gspca_dev->dev;
        int err;
-       u8 kbuf[8];
 
-       memcpy(kbuf, buffer, length);
+       memcpy(gspca_dev->usb_buf, buffer, length);
 
        err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x01,
                        USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
-                       0x00, index, kbuf, length, PAC207_CTRL_TIMEOUT);
+                       0x00, index,
+                       gspca_dev->usb_buf, length, PAC207_CTRL_TIMEOUT);
        if (err < 0)
                PDEBUG(D_ERR,
                        "Failed to write registers to index 0x%04X, error %d)",
@@ -197,7 +205,7 @@ int pac207_write_regs(struct gspca_dev *gspca_dev, u16 index,
 }
 
 
-int pac207_write_reg(struct gspca_dev *gspca_dev, u16 index, u16 value)
+static int pac207_write_reg(struct gspca_dev *gspca_dev, u16 index, u16 value)
 {
        struct usb_device *udev = gspca_dev->dev;
        int err;
@@ -212,16 +220,15 @@ int pac207_write_reg(struct gspca_dev *gspca_dev, u16 index, u16 value)
        return err;
 }
 
-
-int pac207_read_reg(struct gspca_dev *gspca_dev, u16 index)
+static int pac207_read_reg(struct gspca_dev *gspca_dev, u16 index)
 {
        struct usb_device *udev = gspca_dev->dev;
-       u8 buff;
        int res;
 
        res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x00,
                        USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
-                       0x00, index, &buff, 1, PAC207_CTRL_TIMEOUT);
+                       0x00, index,
+                       gspca_dev->usb_buf, 1, PAC207_CTRL_TIMEOUT);
        if (res < 0) {
                PDEBUG(D_ERR,
                        "Failed to read a register (index 0x%04X, error %d)",
@@ -229,10 +236,9 @@ int pac207_read_reg(struct gspca_dev *gspca_dev, u16 index)
                return res;
        }
 
-       return buff;
+       return gspca_dev->usb_buf[0];
 }
 
-
 /* this function is called at probe time */
 static int sd_config(struct gspca_dev *gspca_dev,
                        const struct usb_device_id *id)
@@ -265,7 +271,6 @@ static int sd_config(struct gspca_dev *gspca_dev,
                " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
 
        cam = &gspca_dev->cam;
-       cam->dev_name = (char *) id->driver_info;
        cam->epaddr = 0x05;
        cam->cam_mode = sif_mode;
        cam->nmodes = ARRAY_SIZE(sif_mode);
@@ -346,83 +351,20 @@ static void sd_close(struct gspca_dev *gspca_dev)
 {
 }
 
-static int sd_get_buff_size_op(struct gspca_dev *gspca_dev, int mode)
-{
-       switch (gspca_dev->cam.cam_mode[mode].width) {
-       case 176: /* 176x144 */
-               /* uncompressed, add 2 bytes / line for line header */
-               return (176 + 2) * 144;
-       case 352: /* 352x288 */
-               /* compressed */
-               return 352 * 288 / 2;
-       }
-       return -EIO; /* should never happen */
-}
-
-/* auto gain and exposure algorithm based on the knee algorithm described here:
- * <http://ytse.tricolour.net/docs/LowLightOptimization.html> */
 static void pac207_do_auto_gain(struct gspca_dev *gspca_dev)
 {
        struct sd *sd = (struct sd *) gspca_dev;
-       int i, steps, desired_avg_lum;
-       int orig_gain = sd->gain;
-       int orig_exposure = sd->exposure;
        int avg_lum = atomic_read(&sd->avg_lum);
 
-       if (!sd->autogain || avg_lum == -1)
+       if (avg_lum == -1)
                return;
 
-       if (sd->autogain_ignore_frames > 0) {
+       if (sd->autogain_ignore_frames > 0)
                sd->autogain_ignore_frames--;
-               return;
-       }
-
-       /* correct desired lumination for the configured brightness */
-       desired_avg_lum = 100 + sd->brightness / 2;
-
-       /* If we are of a multiple of deadzone, do multiple step to reach the
-          desired lumination fast (with the risc of a slight overshoot) */
-       steps = abs(desired_avg_lum - avg_lum) / PAC207_AUTOGAIN_DEADZONE;
-
-       for (i = 0; i < steps; i++) {
-               if (avg_lum > desired_avg_lum) {
-                       if (sd->gain > PAC207_GAIN_KNEE)
-                               sd->gain--;
-                       else if (sd->exposure > PAC207_EXPOSURE_KNEE)
-                               sd->exposure--;
-                       else if (sd->gain > PAC207_GAIN_DEFAULT)
-                               sd->gain--;
-                       else if (sd->exposure > PAC207_EXPOSURE_MIN)
-                               sd->exposure--;
-                       else if (sd->gain > PAC207_GAIN_MIN)
-                               sd->gain--;
-                       else
-                               break;
-               } else {
-                       if (sd->gain < PAC207_GAIN_DEFAULT)
-                               sd->gain++;
-                       else if (sd->exposure < PAC207_EXPOSURE_KNEE)
-                               sd->exposure++;
-                       else if (sd->gain < PAC207_GAIN_KNEE)
-                               sd->gain++;
-                       else if (sd->exposure < PAC207_EXPOSURE_MAX)
-                               sd->exposure++;
-                       else if (sd->gain < PAC207_GAIN_MAX)
-                               sd->gain++;
-                       else
-                               break;
-               }
-       }
-
-       if (sd->exposure != orig_exposure || sd->gain != orig_gain) {
-               if (sd->exposure != orig_exposure)
-                       pac207_write_reg(gspca_dev, 0x0002, sd->exposure);
-               if (sd->gain != orig_gain)
-                       pac207_write_reg(gspca_dev, 0x000e, sd->gain);
-               pac207_write_reg(gspca_dev, 0x13, 0x01); /* load reg to sen */
-               pac207_write_reg(gspca_dev, 0x1c, 0x01); /* not documented */
+       else if (gspca_auto_gain_n_exposure(gspca_dev, avg_lum,
+                       100 + sd->brightness / 2, PAC207_AUTOGAIN_DEADZONE,
+                       PAC207_GAIN_KNEE, PAC207_EXPOSURE_KNEE))
                sd->autogain_ignore_frames = PAC207_AUTOGAIN_IGNORE_FRAMES;
-       }
 }
 
 static unsigned char *pac207_find_sof(struct gspca_dev *gspca_dev,
@@ -548,10 +490,6 @@ static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
 {
        struct sd *sd = (struct sd *) gspca_dev;
 
-       /* don't allow mucking with exposure when using autogain */
-       if (sd->autogain)
-               return -EINVAL;
-
        sd->exposure = val;
        if (gspca_dev->streaming)
                setexposure(gspca_dev);
@@ -570,10 +508,6 @@ static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
 {
        struct sd *sd = (struct sd *) gspca_dev;
 
-       /* don't allow mucking with gain when using autogain */
-       if (sd->autogain)
-               return -EINVAL;
-
        sd->gain = val;
        if (gspca_dev->streaming)
                setgain(gspca_dev);
@@ -632,21 +566,19 @@ static const struct sd_desc sd_desc = {
        .close = sd_close,
        .dq_callback = pac207_do_auto_gain,
        .pkt_scan = sd_pkt_scan,
-       .get_buff_size = sd_get_buff_size_op,
 };
 
 /* -- module initialisation -- */
-#define DVNM(name) .driver_info = (kernel_ulong_t) name
 static const __devinitdata struct usb_device_id device_table[] = {
-       {USB_DEVICE(0x041e, 0x4028), DVNM("Creative Webcam Vista Plus")},
-       {USB_DEVICE(0x093a, 0x2460), DVNM("Q-Tec Webcam 100")},
-       {USB_DEVICE(0x093a, 0x2463), DVNM("Philips spc200nc pac207")},
-       {USB_DEVICE(0x093a, 0x2464), DVNM("Labtec Webcam 1200")},
-       {USB_DEVICE(0x093a, 0x2468), DVNM("PAC207")},
-       {USB_DEVICE(0x093a, 0x2470), DVNM("Genius GF112")},
-       {USB_DEVICE(0x093a, 0x2471), DVNM("Genius VideoCam GE111")},
-       {USB_DEVICE(0x093a, 0x2472), DVNM("Genius VideoCam GE110")},
-       {USB_DEVICE(0x2001, 0xf115), DVNM("D-Link DSB-C120")},
+       {USB_DEVICE(0x041e, 0x4028)},
+       {USB_DEVICE(0x093a, 0x2460)},
+       {USB_DEVICE(0x093a, 0x2463)},
+       {USB_DEVICE(0x093a, 0x2464)},
+       {USB_DEVICE(0x093a, 0x2468)},
+       {USB_DEVICE(0x093a, 0x2470)},
+       {USB_DEVICE(0x093a, 0x2471)},
+       {USB_DEVICE(0x093a, 0x2472)},
+       {USB_DEVICE(0x2001, 0xf115)},
        {}
 };
 MODULE_DEVICE_TABLE(usb, device_table);
@@ -671,7 +603,7 @@ static int __init sd_mod_init(void)
 {
        if (usb_register(&sd_driver) < 0)
                return -1;
-       PDEBUG(D_PROBE, "v%s registered", version);
+       PDEBUG(D_PROBE, "registered");
        return 0;
 }
 static void __exit sd_mod_exit(void)