V4L/DVB (13422): gspca - ov534: ov772x changes from Richard Kaswy.
[safe/jmp/linux-2.6] / drivers / media / video / gspca / pac207.c
index d9668f4..57e13e2 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Pixart PAC207BCA library
  *
- * Copyright (C) 2008 Hans de Goede <j.w.r.degoede@hhs.nl>
+ * Copyright (C) 2008 Hans de Goede <hdgoede@redhat.com>
  * Copyright (C) 2005 Thomas Kaiser thomas@kaiser-linux.li
  * Copyleft (C) 2005 Michel Xhaard mxhaard@magic.fr
  *
@@ -27,7 +27,7 @@
 
 #include "gspca.h"
 
-MODULE_AUTHOR("Hans de Goede <j.w.r.degoede@hhs.nl>");
+MODULE_AUTHOR("Hans de Goede <hdgoede@redhat.com>");
 MODULE_DESCRIPTION("Pixart PAC207");
 MODULE_LICENSE("GPL");
 
@@ -35,33 +35,19 @@ MODULE_LICENSE("GPL");
 
 #define PAC207_BRIGHTNESS_MIN          0
 #define PAC207_BRIGHTNESS_MAX          255
-#define PAC207_BRIGHTNESS_DEFAULT      4 /* power on default: 4 */
-
-/* An exposure value of 4 also works (3 does not) but then we need to lower
-   the compression balance setting when in 352x288 mode, otherwise the usb
-   bandwidth is not enough and packets get dropped resulting in corrupt
-   frames. The problem with this is that when the compression balance gets
-   lowered below 0x80, the pac207 starts using a different compression
-   algorithm for some lines, these lines get prefixed with a 0x2dd2 prefix
-   and currently we do not know how to decompress these lines, so for now
-   we use a minimum exposure value of 5 */
-#define PAC207_EXPOSURE_MIN            5
+#define PAC207_BRIGHTNESS_DEFAULT      46
+
+#define PAC207_EXPOSURE_MIN            3
 #define PAC207_EXPOSURE_MAX            26
-#define PAC207_EXPOSURE_DEFAULT                5 /* power on default: 3 ?? */
-#define PAC207_EXPOSURE_KNEE           11 /* 4 = 30 fps, 11 = 8, 15 = 6 */
+#define PAC207_EXPOSURE_DEFAULT                5 /* power on default: 3 */
+#define PAC207_EXPOSURE_KNEE           8 /* 4 = 30 fps, 11 = 8, 15 = 6 */
 
 #define PAC207_GAIN_MIN                        0
 #define PAC207_GAIN_MAX                        31
 #define PAC207_GAIN_DEFAULT            9 /* power on default: 9 */
-#define PAC207_GAIN_KNEE               20
+#define PAC207_GAIN_KNEE               31
 
 #define PAC207_AUTOGAIN_DEADZONE       30
-/* We calculating the autogain at the end of the transfer of a frame, at this
-   moment a frame with the old settings is being transmitted, and a frame is
-   being captured with the old settings. So if we adjust the autogain we must
-   ignore atleast the 2 next frames for the new settings to come into effect
-   before doing any other adjustments */
-#define PAC207_AUTOGAIN_IGNORE_FRAMES  3
 
 /* specific webcam descriptor */
 struct sd {
@@ -131,7 +117,8 @@ static struct ctrl sd_ctrls[] = {
                .minimum = 0,
                .maximum = 1,
                .step   = 1,
-               .default_value = 1,
+#define AUTOGAIN_DEF 1
+               .default_value = AUTOGAIN_DEF,
                .flags = 0,
            },
            .set = sd_setautogain,
@@ -154,7 +141,7 @@ static struct ctrl sd_ctrls[] = {
        },
 };
 
-static struct v4l2_pix_format sif_mode[] = {
+static const struct v4l2_pix_format sif_mode[] = {
        {176, 144, V4L2_PIX_FMT_PAC207, V4L2_FIELD_NONE,
                .bytesperline = 176,
                .sizeimage = (176 + 2) * 144,
@@ -171,19 +158,12 @@ static struct v4l2_pix_format sif_mode[] = {
 };
 
 static const __u8 pac207_sensor_init[][8] = {
-       {0x10, 0x12, 0x0d, 0x12, 0x0c, 0x01, 0x29, 0xf0},
-       {0x00, 0x64, 0x64, 0x64, 0x04, 0x10, 0xf0, 0x30},
+       {0x10, 0x12, 0x0d, 0x12, 0x0c, 0x01, 0x29, 0x84},
+       {0x49, 0x64, 0x64, 0x64, 0x04, 0x10, 0xf0, 0x30},
        {0x00, 0x00, 0x00, 0x70, 0xa0, 0xf8, 0x00, 0x00},
-       {0x00, 0x00, 0x32, 0x00, 0x96, 0x00, 0xa2, 0x02},
        {0x32, 0x00, 0x96, 0x00, 0xA2, 0x02, 0xaf, 0x00},
 };
 
-                       /* 48 reg_72 Rate Control end BalSize_4a =0x36 */
-static const __u8 PacReg72[] = { 0x00, 0x00, 0x36, 0x00 };
-
-static const unsigned char pac207_sof_marker[5] =
-               { 0xff, 0xff, 0x00, 0xff, 0x96 };
-
 static int pac207_write_regs(struct gspca_dev *gspca_dev, u16 index,
        const u8 *buffer, u16 length)
 {
@@ -259,39 +239,35 @@ static int sd_config(struct gspca_dev *gspca_dev,
                return -ENODEV;
        }
 
-       pac207_write_reg(gspca_dev, 0x41, 0x00);
-                               /* Bit_0=Image Format,
-                                * Bit_1=LED,
-                                * Bit_2=Compression test mode enable */
-       pac207_write_reg(gspca_dev, 0x0f, 0x00); /* Power Control */
-       pac207_write_reg(gspca_dev, 0x11, 0x30); /* Analog Bias */
-
        PDEBUG(D_PROBE,
                "Pixart PAC207BCA Image Processor and Control Chip detected"
                " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
 
        cam = &gspca_dev->cam;
-       cam->epaddr = 0x05;
        cam->cam_mode = sif_mode;
        cam->nmodes = ARRAY_SIZE(sif_mode);
        sd->brightness = PAC207_BRIGHTNESS_DEFAULT;
        sd->exposure = PAC207_EXPOSURE_DEFAULT;
        sd->gain = PAC207_GAIN_DEFAULT;
+       sd->autogain = AUTOGAIN_DEF;
 
        return 0;
 }
 
-/* this function is called at open time */
-static int sd_open(struct gspca_dev *gspca_dev)
+/* this function is called at probe and resume time */
+static int sd_init(struct gspca_dev *gspca_dev)
 {
-       struct sd *sd = (struct sd *) gspca_dev;
+       pac207_write_reg(gspca_dev, 0x41, 0x00);
+                               /* Bit_0=Image Format,
+                                * Bit_1=LED,
+                                * Bit_2=Compression test mode enable */
+       pac207_write_reg(gspca_dev, 0x0f, 0x00); /* Power Control */
 
-       sd->autogain = 1;
        return 0;
 }
 
 /* -- start the camera -- */
-static void sd_start(struct gspca_dev *gspca_dev)
+static int sd_start(struct gspca_dev *gspca_dev)
 {
        struct sd *sd = (struct sd *) gspca_dev;
        __u8 mode;
@@ -300,15 +276,13 @@ static void sd_start(struct gspca_dev *gspca_dev)
        pac207_write_regs(gspca_dev, 0x0002, pac207_sensor_init[0], 8);
        pac207_write_regs(gspca_dev, 0x000a, pac207_sensor_init[1], 8);
        pac207_write_regs(gspca_dev, 0x0012, pac207_sensor_init[2], 8);
-       pac207_write_regs(gspca_dev, 0x0040, pac207_sensor_init[3], 8);
-       pac207_write_regs(gspca_dev, 0x0042, pac207_sensor_init[4], 8);
-       pac207_write_regs(gspca_dev, 0x0048, PacReg72, 4);
+       pac207_write_regs(gspca_dev, 0x0042, pac207_sensor_init[3], 8);
 
        /* Compression Balance */
        if (gspca_dev->width == 176)
                pac207_write_reg(gspca_dev, 0x4a, 0xff);
        else
-               pac207_write_reg(gspca_dev, 0x4a, 0x88);
+               pac207_write_reg(gspca_dev, 0x4a, 0x30);
        pac207_write_reg(gspca_dev, 0x4b, 0x00); /* Sram test value */
        pac207_write_reg(gspca_dev, 0x08, sd->brightness);
 
@@ -333,6 +307,7 @@ static void sd_start(struct gspca_dev *gspca_dev)
        sd->sof_read = 0;
        sd->autogain_ignore_frames = 0;
        atomic_set(&sd->avg_lum, -1);
+       return 0;
 }
 
 static void sd_stopN(struct gspca_dev *gspca_dev)
@@ -342,14 +317,8 @@ static void sd_stopN(struct gspca_dev *gspca_dev)
        pac207_write_reg(gspca_dev, 0x0f, 0x00); /* Power Control */
 }
 
-static void sd_stop0(struct gspca_dev *gspca_dev)
-{
-}
-
-/* this function is called at close time */
-static void sd_close(struct gspca_dev *gspca_dev)
-{
-}
+/* Include pac common sof detection functions */
+#include "pac_common.h"
 
 static void pac207_do_auto_gain(struct gspca_dev *gspca_dev)
 {
@@ -362,35 +331,9 @@ static void pac207_do_auto_gain(struct gspca_dev *gspca_dev)
        if (sd->autogain_ignore_frames > 0)
                sd->autogain_ignore_frames--;
        else if (gspca_auto_gain_n_exposure(gspca_dev, avg_lum,
-                       100 + sd->brightness / 2, PAC207_AUTOGAIN_DEADZONE,
+                       100, 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,
-                                       unsigned char *m, int len)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
-       int i;
-
-       /* Search for the SOF marker (fixed part) in the header */
-       for (i = 0; i < len; i++) {
-               if (m[i] == pac207_sof_marker[sd->sof_read]) {
-                       sd->sof_read++;
-                       if (sd->sof_read == sizeof(pac207_sof_marker)) {
-                               PDEBUG(D_STREAM,
-                                       "SOF found, bytes to analyze: %u."
-                                       " Frame starts at byte #%u",
-                                       len, i + 1);
-                               sd->sof_read = 0;
-                               return m + i + 1;
-                       }
-               } else {
-                       sd->sof_read = 0;
-               }
-       }
-
-       return NULL;
+               sd->autogain_ignore_frames = PAC_AUTOGAIN_IGNORE_FRAMES;
 }
 
 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
@@ -401,14 +344,14 @@ static void sd_pkt_scan(struct gspca_dev *gspca_dev,
        struct sd *sd = (struct sd *) gspca_dev;
        unsigned char *sof;
 
-       sof = pac207_find_sof(gspca_dev, data, len);
+       sof = pac_find_sof(&sd->sof_read, data, len);
        if (sof) {
                int n;
 
                /* finish decoding current frame */
                n = sof - data;
-               if (n > sizeof pac207_sof_marker)
-                       n -= sizeof pac207_sof_marker;
+               if (n > sizeof pac_sof_marker)
+                       n -= sizeof pac_sof_marker;
                else
                        n = 0;
                frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
@@ -536,7 +479,7 @@ static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
                sd->gain = PAC207_GAIN_DEFAULT;
                if (gspca_dev->streaming) {
                        sd->autogain_ignore_frames =
-                               PAC207_AUTOGAIN_IGNORE_FRAMES;
+                               PAC_AUTOGAIN_IGNORE_FRAMES;
                        setexposure(gspca_dev);
                        setgain(gspca_dev);
                }
@@ -559,11 +502,9 @@ static const struct sd_desc sd_desc = {
        .ctrls = sd_ctrls,
        .nctrls = ARRAY_SIZE(sd_ctrls),
        .config = sd_config,
-       .open = sd_open,
+       .init = sd_init,
        .start = sd_start,
        .stopN = sd_stopN,
-       .stop0 = sd_stop0,
-       .close = sd_close,
        .dq_callback = pac207_do_auto_gain,
        .pkt_scan = sd_pkt_scan,
 };
@@ -572,12 +513,16 @@ static const struct sd_desc sd_desc = {
 static const __devinitdata struct usb_device_id device_table[] = {
        {USB_DEVICE(0x041e, 0x4028)},
        {USB_DEVICE(0x093a, 0x2460)},
+       {USB_DEVICE(0x093a, 0x2461)},
        {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(0x093a, 0x2474)},
+       {USB_DEVICE(0x093a, 0x2476)},
+       {USB_DEVICE(0x145f, 0x013a)},
        {USB_DEVICE(0x2001, 0xf115)},
        {}
 };
@@ -605,8 +550,10 @@ static struct usb_driver sd_driver = {
 /* -- module insert / remove -- */
 static int __init sd_mod_init(void)
 {
-       if (usb_register(&sd_driver) < 0)
-               return -1;
+       int ret;
+       ret = usb_register(&sd_driver);
+       if (ret < 0)
+               return ret;
        PDEBUG(D_PROBE, "registered");
        return 0;
 }