Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / drivers / media / video / gspca / sonixj.c
index 4bb6479..863c755 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Sonix sn9c102p sn9c105 sn9c120 (jpeg) subdriver
  *
- * Copyright (C) 2009-2010 Jean-François Moine <http://moinejf.free.fr>
+ * Copyright (C) 2009-2011 Jean-François Moine <http://moinejf.free.fr>
  * Copyright (C) 2005 Michel Xhaard mxhaard@magic.fr
  *
  * This program is free software; you can redistribute it and/or modify
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
 
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
 #define MODULE_NAME "sonixj"
 
 #include <linux/input.h>
-#include <linux/slab.h>
 #include "gspca.h"
 #include "jpeg.h"
 
-#define V4L2_CID_INFRARED (V4L2_CID_PRIVATE_BASE + 0)
-
 MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
 MODULE_DESCRIPTION("GSPCA/SONIX JPEG USB Camera Driver");
 MODULE_LICENSE("GPL");
 
+/* controls */
+enum e_ctrl {
+       BRIGHTNESS,
+       CONTRAST,
+       COLORS,
+       BLUE,
+       RED,
+       GAMMA,
+       EXPOSURE,
+       AUTOGAIN,
+       GAIN,
+       HFLIP,
+       VFLIP,
+       SHARPNESS,
+       ILLUM,
+       FREQ,
+       NCTRLS          /* number of controls */
+};
+
 /* specific webcam descriptor */
 struct sd {
        struct gspca_dev gspca_dev;     /* !! must be the first item */
 
+       struct gspca_ctrl ctrls[NCTRLS];
+
        atomic_t avg_lum;
        u32 exposure;
 
-       u16 brightness;
-       u8 contrast;
-       u8 colors;
-       u8 autogain;
-       u8 blue;
-       u8 red;
-       u8 gamma;
-       u8 vflip;                       /* ov7630/ov7648 only */
-       u8 sharpness;
-       u8 infrared;                    /* mt9v111 only */
-       u8 freq;                        /* ov76xx only */
+       struct work_struct work;
+       struct workqueue_struct *work_thread;
+
+       u32 pktsz;                      /* (used by pkt_scan) */
+       u16 npkt;
+       s8 nchg;
+       s8 short_mark;
+
        u8 quality;                     /* image quality */
-#define QUALITY_MIN 60
-#define QUALITY_MAX 95
-#define QUALITY_DEF 80
-       u8 jpegqual;                    /* webcam quality */
+#define QUALITY_MIN 25
+#define QUALITY_MAX 90
+#define QUALITY_DEF 70
 
+       u8 reg01;
+       u8 reg17;
        u8 reg18;
+       u8 flags;
 
        s8 ag_cnt;
 #define AG_CNT_START 13
@@ -67,11 +86,16 @@ struct sd {
 #define BRIDGE_SN9C110 2
 #define BRIDGE_SN9C120 3
        u8 sensor;                      /* Type of image sensor chip */
-enum {
+       u8 i2c_addr;
+
+       u8 jpeg_hdr[JPEG_HDR_SZ];
+};
+enum sensors {
        SENSOR_ADCM1700,
        SENSOR_GC0307,
        SENSOR_HV7131R,
        SENSOR_MI0360,
+       SENSOR_MI0360B,
        SENSOR_MO4000,
        SENSOR_MT9V111,
        SENSOR_OM6802,
@@ -82,55 +106,55 @@ enum {
        SENSOR_PO2030N,
        SENSOR_SOI768,
        SENSOR_SP80708,
-} sensors;
-       u8 i2c_addr;
-
-       u8 jpeg_hdr[JPEG_HDR_SZ];
 };
 
+static void qual_upd(struct work_struct *work);
+
+/* device flags */
+#define F_PDN_INV      0x01    /* inverse pin S_PWR_DN / sn_xxx tables */
+#define F_ILLUM                0x02    /* presence of illuminator */
+
+/* sn9c1xx definitions */
+/* register 0x01 */
+#define S_PWR_DN       0x01    /* sensor power down */
+#define S_PDN_INV      0x02    /* inverse pin S_PWR_DN */
+#define V_TX_EN                0x04    /* video transfer enable */
+#define LED            0x08    /* output to pin LED */
+#define SCL_SEL_OD     0x20    /* open-drain mode */
+#define SYS_SEL_48M    0x40    /* system clock 0: 24MHz, 1: 48MHz */
+/* register 0x17 */
+#define MCK_SIZE_MASK  0x1f    /* sensor master clock */
+#define SEN_CLK_EN     0x20    /* enable sensor clock */
+#define DEF_EN         0x80    /* defect pixel by 0: soft, 1: hard */
+
 /* V4L2 controls supported by the driver */
-static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
-static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
-static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
-static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
-static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
-static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
-static int sd_setblue_balance(struct gspca_dev *gspca_dev, __s32 val);
-static int sd_getblue_balance(struct gspca_dev *gspca_dev, __s32 *val);
-static int sd_setred_balance(struct gspca_dev *gspca_dev, __s32 val);
-static int sd_getred_balance(struct gspca_dev *gspca_dev, __s32 *val);
-static int sd_setgamma(struct gspca_dev *gspca_dev, __s32 val);
-static int sd_getgamma(struct gspca_dev *gspca_dev, __s32 *val);
+static void setbrightness(struct gspca_dev *gspca_dev);
+static void setcontrast(struct gspca_dev *gspca_dev);
+static void setcolors(struct gspca_dev *gspca_dev);
+static void setredblue(struct gspca_dev *gspca_dev);
+static void setgamma(struct gspca_dev *gspca_dev);
+static void setexposure(struct gspca_dev *gspca_dev);
 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
-static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
-static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
-static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
-static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val);
-static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val);
-static int sd_setinfrared(struct gspca_dev *gspca_dev, __s32 val);
-static int sd_getinfrared(struct gspca_dev *gspca_dev, __s32 *val);
-static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
-static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
-
-static const struct ctrl sd_ctrls[] = {
-#define BRIGHTNESS_IDX 0
-       {
+static void setgain(struct gspca_dev *gspca_dev);
+static void sethvflip(struct gspca_dev *gspca_dev);
+static void setsharpness(struct gspca_dev *gspca_dev);
+static void setillum(struct gspca_dev *gspca_dev);
+static void setfreq(struct gspca_dev *gspca_dev);
+
+static const struct ctrl sd_ctrls[NCTRLS] = {
+[BRIGHTNESS] = {
            {
                .id      = V4L2_CID_BRIGHTNESS,
                .type    = V4L2_CTRL_TYPE_INTEGER,
                .name    = "Brightness",
                .minimum = 0,
-#define BRIGHTNESS_MAX 0xffff
-               .maximum = BRIGHTNESS_MAX,
+               .maximum = 0xff,
                .step    = 1,
-#define BRIGHTNESS_DEF 0x8000
-               .default_value = BRIGHTNESS_DEF,
+               .default_value = 0x80,
            },
-           .set = sd_setbrightness,
-           .get = sd_getbrightness,
+           .set_control = setbrightness
        },
-#define CONTRAST_IDX 1
-       {
+[CONTRAST] = {
            {
                .id      = V4L2_CID_CONTRAST,
                .type    = V4L2_CTRL_TYPE_INTEGER,
@@ -139,14 +163,11 @@ static const struct ctrl sd_ctrls[] = {
 #define CONTRAST_MAX 127
                .maximum = CONTRAST_MAX,
                .step    = 1,
-#define CONTRAST_DEF 63
-               .default_value = CONTRAST_DEF,
+               .default_value = 20,
            },
-           .set = sd_setcontrast,
-           .get = sd_getcontrast,
+           .set_control = setcontrast
        },
-#define COLOR_IDX 2
-       {
+[COLORS] = {
            {
                .id      = V4L2_CID_SATURATION,
                .type    = V4L2_CTRL_TYPE_INTEGER,
@@ -154,14 +175,12 @@ static const struct ctrl sd_ctrls[] = {
                .minimum = 0,
                .maximum = 40,
                .step    = 1,
-#define COLOR_DEF 25
-               .default_value = COLOR_DEF,
+#define COLORS_DEF 25
+               .default_value = COLORS_DEF,
            },
-           .set = sd_setcolors,
-           .get = sd_getcolors,
+           .set_control = setcolors
        },
-#define BLUE_BALANCE_IDX 3
-       {
+[BLUE] = {
            {
                .id      = V4L2_CID_BLUE_BALANCE,
                .type    = V4L2_CTRL_TYPE_INTEGER,
@@ -169,14 +188,11 @@ static const struct ctrl sd_ctrls[] = {
                .minimum = 24,
                .maximum = 40,
                .step    = 1,
-#define BLUE_BALANCE_DEF 32
-               .default_value = BLUE_BALANCE_DEF,
+               .default_value = 32,
            },
-           .set = sd_setblue_balance,
-           .get = sd_getblue_balance,
+           .set_control = setredblue
        },
-#define RED_BALANCE_IDX 4
-       {
+[RED] = {
            {
                .id      = V4L2_CID_RED_BALANCE,
                .type    = V4L2_CTRL_TYPE_INTEGER,
@@ -184,14 +200,11 @@ static const struct ctrl sd_ctrls[] = {
                .minimum = 24,
                .maximum = 40,
                .step    = 1,
-#define RED_BALANCE_DEF 32
-               .default_value = RED_BALANCE_DEF,
+               .default_value = 32,
            },
-           .set = sd_setred_balance,
-           .get = sd_getred_balance,
+           .set_control = setredblue
        },
-#define GAMMA_IDX 5
-       {
+[GAMMA] = {
            {
                .id      = V4L2_CID_GAMMA,
                .type    = V4L2_CTRL_TYPE_INTEGER,
@@ -202,11 +215,21 @@ static const struct ctrl sd_ctrls[] = {
 #define GAMMA_DEF 20
                .default_value = GAMMA_DEF,
            },
-           .set = sd_setgamma,
-           .get = sd_getgamma,
+           .set_control = setgamma
+       },
+[EXPOSURE] = {
+           {
+               .id      = V4L2_CID_EXPOSURE,
+               .type    = V4L2_CTRL_TYPE_INTEGER,
+               .name    = "Exposure",
+               .minimum = 500,
+               .maximum = 1500,
+               .step    = 1,
+               .default_value = 1024
+           },
+           .set_control = setexposure
        },
-#define AUTOGAIN_IDX 6
-       {
+[AUTOGAIN] = {
            {
                .id      = V4L2_CID_AUTOGAIN,
                .type    = V4L2_CTRL_TYPE_BOOLEAN,
@@ -214,15 +237,35 @@ static const struct ctrl sd_ctrls[] = {
                .minimum = 0,
                .maximum = 1,
                .step    = 1,
-#define AUTOGAIN_DEF 1
-               .default_value = AUTOGAIN_DEF,
+               .default_value = 1
            },
            .set = sd_setautogain,
-           .get = sd_getautogain,
        },
-/* ov7630/ov7648 only */
-#define VFLIP_IDX 7
-       {
+[GAIN] = {
+           {
+               .id      = V4L2_CID_GAIN,
+               .type    = V4L2_CTRL_TYPE_INTEGER,
+               .name    = "Gain",
+               .minimum = 4,
+               .maximum = 49,
+               .step    = 1,
+               .default_value = 15
+           },
+           .set_control = setgain
+       },
+[HFLIP] = {
+           {
+               .id      = V4L2_CID_HFLIP,
+               .type    = V4L2_CTRL_TYPE_BOOLEAN,
+               .name    = "Mirror",
+               .minimum = 0,
+               .maximum = 1,
+               .step    = 1,
+               .default_value = 0,
+           },
+           .set_control = sethvflip
+       },
+[VFLIP] = {
            {
                .id      = V4L2_CID_VFLIP,
                .type    = V4L2_CTRL_TYPE_BOOLEAN,
@@ -230,14 +273,11 @@ static const struct ctrl sd_ctrls[] = {
                .minimum = 0,
                .maximum = 1,
                .step    = 1,
-#define VFLIP_DEF 0
-               .default_value = VFLIP_DEF,
+               .default_value = 0,
            },
-           .set = sd_setvflip,
-           .get = sd_getvflip,
+           .set_control = sethvflip
        },
-#define SHARPNESS_IDX 8
-       {
+[SHARPNESS] = {
            {
                .id      = V4L2_CID_SHARPNESS,
                .type    = V4L2_CTRL_TYPE_INTEGER,
@@ -245,31 +285,24 @@ static const struct ctrl sd_ctrls[] = {
                .minimum = 0,
                .maximum = 255,
                .step    = 1,
-#define SHARPNESS_DEF 90
-               .default_value = SHARPNESS_DEF,
+               .default_value = 90,
            },
-           .set = sd_setsharpness,
-           .get = sd_getsharpness,
+           .set_control = setsharpness
        },
-/* mt9v111 only */
-#define INFRARED_IDX 9
-       {
+[ILLUM] = {
            {
-               .id      = V4L2_CID_INFRARED,
+               .id      = V4L2_CID_ILLUMINATORS_1,
                .type    = V4L2_CTRL_TYPE_BOOLEAN,
-               .name    = "Infrared",
+               .name    = "Illuminator / infrared",
                .minimum = 0,
                .maximum = 1,
                .step    = 1,
-#define INFRARED_DEF 0
-               .default_value = INFRARED_DEF,
+               .default_value = 0,
            },
-           .set = sd_setinfrared,
-           .get = sd_getinfrared,
+           .set_control = setillum
        },
 /* ov7630/ov7648/ov7660 only */
-#define FREQ_IDX 10
-       {
+[FREQ] = {
            {
                .id      = V4L2_CID_POWER_LINE_FREQUENCY,
                .type    = V4L2_CTRL_TYPE_MENU,
@@ -277,69 +310,98 @@ static const struct ctrl sd_ctrls[] = {
                .minimum = 0,
                .maximum = 2,   /* 0: 0, 1: 50Hz, 2:60Hz */
                .step    = 1,
-#define FREQ_DEF 1
-               .default_value = FREQ_DEF,
+               .default_value = 1,
            },
-           .set = sd_setfreq,
-           .get = sd_getfreq,
+           .set_control = setfreq
        },
 };
 
 /* table of the disabled controls */
 static const __u32 ctrl_dis[] = {
-[SENSOR_ADCM1700] =    (1 << AUTOGAIN_IDX) |
-                       (1 << INFRARED_IDX) |
-                       (1 << VFLIP_IDX) |
-                       (1 << FREQ_IDX),
-
-[SENSOR_GC0307] =      (1 << INFRARED_IDX) |
-                       (1 << VFLIP_IDX) |
-                       (1 << FREQ_IDX),
-
-[SENSOR_HV7131R] =     (1 << INFRARED_IDX) |
-                       (1 << FREQ_IDX),
-
-[SENSOR_MI0360] =      (1 << INFRARED_IDX) |
-                       (1 << VFLIP_IDX) |
-                       (1 << FREQ_IDX),
-
-[SENSOR_MO4000] =      (1 << INFRARED_IDX) |
-                       (1 << VFLIP_IDX) |
-                       (1 << FREQ_IDX),
-
-[SENSOR_MT9V111] =     (1 << VFLIP_IDX) |
-                       (1 << FREQ_IDX),
-
-[SENSOR_OM6802] =      (1 << INFRARED_IDX) |
-                       (1 << VFLIP_IDX) |
-                       (1 << FREQ_IDX),
-
-[SENSOR_OV7630] =      (1 << INFRARED_IDX),
-
-[SENSOR_OV7648] =      (1 << INFRARED_IDX),
-
-[SENSOR_OV7660] =      (1 << AUTOGAIN_IDX) |
-                       (1 << INFRARED_IDX) |
-                       (1 << VFLIP_IDX),
-
-[SENSOR_PO1030] =      (1 << AUTOGAIN_IDX) |
-                       (1 << INFRARED_IDX) |
-                       (1 << VFLIP_IDX) |
-                       (1 << FREQ_IDX),
-
-[SENSOR_PO2030N] =     (1 << AUTOGAIN_IDX) |
-                       (1 << INFRARED_IDX) |
-                       (1 << VFLIP_IDX) |
-                       (1 << FREQ_IDX),
-[SENSOR_SOI768] =      (1 << AUTOGAIN_IDX) |
-                       (1 << INFRARED_IDX) |
-                       (1 << VFLIP_IDX) |
-                       (1 << FREQ_IDX),
-
-[SENSOR_SP80708] =     (1 << AUTOGAIN_IDX) |
-                       (1 << INFRARED_IDX) |
-                       (1 << VFLIP_IDX) |
-                       (1 << FREQ_IDX),
+[SENSOR_ADCM1700] =    (1 << EXPOSURE) |
+                       (1 << AUTOGAIN) |
+                       (1 << GAIN) |
+                       (1 << HFLIP) |
+                       (1 << VFLIP) |
+                       (1 << FREQ),
+
+[SENSOR_GC0307] =      (1 << EXPOSURE) |
+                       (1 << GAIN) |
+                       (1 << HFLIP) |
+                       (1 << VFLIP) |
+                       (1 << FREQ),
+
+[SENSOR_HV7131R] =     (1 << EXPOSURE) |
+                       (1 << GAIN) |
+                       (1 << HFLIP) |
+                       (1 << FREQ),
+
+[SENSOR_MI0360] =      (1 << EXPOSURE) |
+                       (1 << GAIN) |
+                       (1 << HFLIP) |
+                       (1 << VFLIP) |
+                       (1 << FREQ),
+
+[SENSOR_MI0360B] =     (1 << EXPOSURE) |
+                       (1 << GAIN) |
+                       (1 << HFLIP) |
+                       (1 << VFLIP) |
+                       (1 << FREQ),
+
+[SENSOR_MO4000] =      (1 << EXPOSURE) |
+                       (1 << GAIN) |
+                       (1 << HFLIP) |
+                       (1 << VFLIP) |
+                       (1 << FREQ),
+
+[SENSOR_MT9V111] =     (1 << EXPOSURE) |
+                       (1 << GAIN) |
+                       (1 << HFLIP) |
+                       (1 << VFLIP) |
+                       (1 << FREQ),
+
+[SENSOR_OM6802] =      (1 << EXPOSURE) |
+                       (1 << GAIN) |
+                       (1 << HFLIP) |
+                       (1 << VFLIP) |
+                       (1 << FREQ),
+
+[SENSOR_OV7630] =      (1 << EXPOSURE) |
+                       (1 << GAIN) |
+                       (1 << HFLIP),
+
+[SENSOR_OV7648] =      (1 << EXPOSURE) |
+                       (1 << GAIN) |
+                       (1 << HFLIP),
+
+[SENSOR_OV7660] =      (1 << EXPOSURE) |
+                       (1 << AUTOGAIN) |
+                       (1 << GAIN) |
+                       (1 << HFLIP) |
+                       (1 << VFLIP),
+
+[SENSOR_PO1030] =      (1 << EXPOSURE) |
+                       (1 << AUTOGAIN) |
+                       (1 << GAIN) |
+                       (1 << HFLIP) |
+                       (1 << VFLIP) |
+                       (1 << FREQ),
+
+[SENSOR_PO2030N] =     (1 << FREQ),
+
+[SENSOR_SOI768] =      (1 << EXPOSURE) |
+                       (1 << AUTOGAIN) |
+                       (1 << GAIN) |
+                       (1 << HFLIP) |
+                       (1 << VFLIP) |
+                       (1 << FREQ),
+
+[SENSOR_SP80708] =     (1 << EXPOSURE) |
+                       (1 << AUTOGAIN) |
+                       (1 << GAIN) |
+                       (1 << HFLIP) |
+                       (1 << VFLIP) |
+                       (1 << FREQ),
 };
 
 static const struct v4l2_pix_format cif_mode[] = {
@@ -392,7 +454,7 @@ static const u8 sn_gc0307[0x1c] = {
 
 static const u8 sn_hv7131[0x1c] = {
 /*     reg0    reg1    reg2    reg3    reg4    reg5    reg6    reg7 */
-       0x00,   0x03,   0x64,   0x00,   0x1a,   0x20,   0x20,   0x20,
+       0x00,   0x03,   0x60,   0x00,   0x1a,   0x20,   0x20,   0x20,
 /*     reg8    reg9    rega    regb    regc    regd    rege    regf */
        0x81,   0x11,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
 /*     reg10   reg11   reg12   reg13   reg14   reg15   reg16   reg17 */
@@ -403,7 +465,7 @@ static const u8 sn_hv7131[0x1c] = {
 
 static const u8 sn_mi0360[0x1c] = {
 /*     reg0    reg1    reg2    reg3    reg4    reg5    reg6    reg7 */
-       0x00,   0x61,   0x44,   0x00,   0x1a,   0x20,   0x20,   0x20,
+       0x00,   0x63,   0x40,   0x00,   0x1a,   0x20,   0x20,   0x20,
 /*     reg8    reg9    rega    regb    regc    regd    rege    regf */
        0x81,   0x5d,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
 /*     reg10   reg11   reg12   reg13   reg14   reg15   reg16   reg17 */
@@ -412,6 +474,17 @@ static const u8 sn_mi0360[0x1c] = {
        0x06,   0x00,   0x00,   0x00
 };
 
+static const u8 sn_mi0360b[0x1c] = {
+/*     reg0    reg1    reg2    reg3    reg4    reg5    reg6    reg7 */
+       0x00,   0x61,   0x40,   0x00,   0x1a,   0x00,   0x00,   0x00,
+/*     reg8    reg9    rega    regb    regc    regd    rege    regf */
+       0x81,   0x5d,   0x00,   0x00,   0x00,   0x00,   0x00,   0x00,
+/*     reg10   reg11   reg12   reg13   reg14   reg15   reg16   reg17 */
+       0x03,   0x00,   0x00,   0x02,   0x0a,   0x28,   0x1e,   0x40,
+/*     reg18   reg19   reg1a   reg1b */
+       0x06,   0x00,   0x00,   0x00
+};
+
 static const u8 sn_mo4000[0x1c] = {
 /*     reg0    reg1    reg2    reg3    reg4    reg5    reg6    reg7 */
        0x00,   0x23,   0x60,   0x00,   0x1a,   0x00,   0x20,   0x18,
@@ -528,6 +601,7 @@ static const u8 *sn_tb[] = {
 [SENSOR_GC0307] =      sn_gc0307,
 [SENSOR_HV7131R] =     sn_hv7131,
 [SENSOR_MI0360] =      sn_mi0360,
+[SENSOR_MI0360B] =     sn_mi0360b,
 [SENSOR_MO4000] =      sn_mo4000,
 [SENSOR_MT9V111] =     sn_mt9v111,
 [SENSOR_OM6802] =      sn_om6802,
@@ -573,20 +647,23 @@ static const u8 reg84[] = {
        0x3e, 0x00, 0xcd, 0x0f, 0xf7, 0x0f,     /* VR VG VB */
        0x00, 0x00, 0x00                        /* YUV offsets */
 };
+
+#define DELAY  0xdd
+
 static const u8 adcm1700_sensor_init[][8] = {
        {0xa0, 0x51, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x10},
        {0xb0, 0x51, 0x04, 0x08, 0x00, 0x00, 0x00, 0x10},       /* reset */
-       {0xdd, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
+       {DELAY, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
        {0xb0, 0x51, 0x04, 0x00, 0x00, 0x00, 0x00, 0x10},
-       {0xdd, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
+       {DELAY, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
        {0xb0, 0x51, 0x0c, 0xe0, 0x2e, 0x00, 0x00, 0x10},
        {0xb0, 0x51, 0x10, 0x02, 0x02, 0x00, 0x00, 0x10},
        {0xb0, 0x51, 0x14, 0x0e, 0x0e, 0x00, 0x00, 0x10},
        {0xb0, 0x51, 0x1c, 0x00, 0x80, 0x00, 0x00, 0x10},
        {0xb0, 0x51, 0x20, 0x01, 0x00, 0x00, 0x00, 0x10},
-       {0xdd, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
+       {DELAY, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
        {0xb0, 0x51, 0x04, 0x04, 0x00, 0x00, 0x00, 0x10},
-       {0xdd, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
+       {DELAY, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
        {0xb0, 0x51, 0x04, 0x01, 0x00, 0x00, 0x00, 0x10},
        {0xa0, 0x51, 0xfe, 0x10, 0x00, 0x00, 0x00, 0x10},
        {0xb0, 0x51, 0x14, 0x01, 0x00, 0x00, 0x00, 0x10},
@@ -630,7 +707,7 @@ static const u8 gc0307_sensor_init[][8] = {
        {0xa0, 0x21, 0x0e, 0x02, 0x00, 0x00, 0x00, 0x10},
        {0xa0, 0x21, 0x0f, 0xb2, 0x00, 0x00, 0x00, 0x10},
        {0xa0, 0x21, 0x12, 0x70, 0x00, 0x00, 0x00, 0x10},
-       {0xdd, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /*delay 10ms*/
+       {DELAY, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /*delay 10ms*/
        {0xa0, 0x21, 0x13, 0x00, 0x00, 0x00, 0x00, 0x10},
        {0xa0, 0x21, 0x15, 0xb8, 0x00, 0x00, 0x00, 0x10},
        {0xa0, 0x21, 0x16, 0x13, 0x00, 0x00, 0x00, 0x10},
@@ -717,7 +794,7 @@ static const u8 mi0360_sensor_init[][8] = {
        {0xd1, 0x5d, 0x22, 0x00, 0x00, 0x00, 0x00, 0x10},
        {0xd1, 0x5d, 0x24, 0x00, 0x00, 0x00, 0x00, 0x10},
        {0xd1, 0x5d, 0x26, 0x00, 0x00, 0x00, 0x24, 0x10},
-       {0xd1, 0x5d, 0x2f, 0xf7, 0xB0, 0x00, 0x04, 0x10},
+       {0xd1, 0x5d, 0x2f, 0xf7, 0xb0, 0x00, 0x04, 0x10},
        {0xd1, 0x5d, 0x31, 0x00, 0x00, 0x00, 0x00, 0x10},
        {0xd1, 0x5d, 0x33, 0x00, 0x00, 0x01, 0x00, 0x10},
        {0xb1, 0x5d, 0x3d, 0x06, 0x8f, 0x00, 0x00, 0x10},
@@ -748,6 +825,62 @@ static const u8 mi0360_sensor_init[][8] = {
        {0xb1, 0x5d, 0x07, 0x00, 0x02, 0x00, 0x00, 0x10}, /* sensor on */
        {}
 };
+static const u8 mi0360b_sensor_init[][8] = {
+       {0xb1, 0x5d, 0x07, 0x00, 0x02, 0x00, 0x00, 0x10},
+       {0xb1, 0x5d, 0x0d, 0x00, 0x01, 0x00, 0x00, 0x10},
+       {DELAY, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /*delay 20ms*/
+       {0xb1, 0x5d, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x10},
+       {DELAY, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /*delay 20ms*/
+       {0xd1, 0x5d, 0x01, 0x00, 0x08, 0x00, 0x16, 0x10},
+       {0xd1, 0x5d, 0x03, 0x01, 0xe2, 0x02, 0x82, 0x10},
+       {0xd1, 0x5d, 0x05, 0x00, 0x00, 0x00, 0x00, 0x10},
+       {0xb1, 0x5d, 0x0d, 0x00, 0x02, 0x00, 0x00, 0x10},
+       {0xd1, 0x5d, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x10},
+       {0xd1, 0x5d, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x10},
+       {0xd1, 0x5d, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x10},
+       {0xd1, 0x5d, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10},
+       {0xd1, 0x5d, 0x12, 0x00, 0x00, 0x00, 0x00, 0x10},
+       {0xd1, 0x5d, 0x14, 0x00, 0x00, 0x00, 0x00, 0x10},
+       {0xd1, 0x5d, 0x16, 0x00, 0x00, 0x00, 0x00, 0x10},
+       {0xd1, 0x5d, 0x18, 0x00, 0x00, 0x00, 0x00, 0x10},
+       {0xd1, 0x5d, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x10},
+       {0xd1, 0x5d, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x10},
+       {0xb1, 0x5d, 0x32, 0x00, 0x00, 0x00, 0x00, 0x10},
+       {0xd1, 0x5d, 0x20, 0x11, 0x01, 0x00, 0x00, 0x10},
+       {0xd1, 0x5d, 0x22, 0x00, 0x00, 0x00, 0x00, 0x10},
+       {0xd1, 0x5d, 0x24, 0x00, 0x00, 0x00, 0x00, 0x10},
+       {0xd1, 0x5d, 0x26, 0x00, 0x00, 0x00, 0x24, 0x10},
+       {0xd1, 0x5d, 0x2f, 0xf7, 0xb0, 0x00, 0x04, 0x10},
+       {0xd1, 0x5d, 0x31, 0x00, 0x00, 0x00, 0x00, 0x10},
+       {0xd1, 0x5d, 0x33, 0x00, 0x00, 0x01, 0x00, 0x10},
+       {0xb1, 0x5d, 0x3d, 0x06, 0x8f, 0x00, 0x00, 0x10},
+       {0xd1, 0x5d, 0x40, 0x01, 0xe0, 0x00, 0xd1, 0x10},
+       {0xb1, 0x5d, 0x44, 0x00, 0x82, 0x00, 0x00, 0x10},
+       {0xd1, 0x5d, 0x58, 0x00, 0x78, 0x00, 0x43, 0x10},
+       {0xd1, 0x5d, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x10},
+       {0xd1, 0x5d, 0x5c, 0x00, 0x00, 0x00, 0x00, 0x10},
+       {0xd1, 0x5d, 0x5e, 0x00, 0x00, 0xa3, 0x1d, 0x10},
+       {0xb1, 0x5d, 0x62, 0x04, 0x11, 0x00, 0x00, 0x10},
+
+       {0xb1, 0x5d, 0x20, 0x11, 0x01, 0x00, 0x00, 0x10},
+       {0xb1, 0x5d, 0x20, 0x11, 0x01, 0x00, 0x00, 0x10},
+       {0xb1, 0x5d, 0x09, 0x00, 0x64, 0x00, 0x00, 0x10},
+       {0xd1, 0x5d, 0x2b, 0x00, 0x33, 0x00, 0xa0, 0x10},
+       {0xd1, 0x5d, 0x2d, 0x00, 0xa0, 0x00, 0x33, 0x10},
+       {}
+};
+static const u8 mi0360b_sensor_param1[][8] = {
+       {0xb1, 0x5d, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x10},
+       {0xb1, 0x5d, 0x06, 0x00, 0x53, 0x00, 0x00, 0x10},
+       {0xb1, 0x5d, 0x05, 0x00, 0x09, 0x00, 0x00, 0x10},
+       {0xb1, 0x5d, 0x09, 0x02, 0x35, 0x00, 0x00, 0x10}, /* exposure 2 */
+
+       {0xd1, 0x5d, 0x2b, 0x00, 0xd1, 0x01, 0xc9, 0x10},
+       {0xd1, 0x5d, 0x2d, 0x00, 0xed, 0x00, 0xd1, 0x10},
+       {0xb1, 0x5d, 0x07, 0x00, 0x03, 0x00, 0x00, 0x10}, /* update */
+       {0xb1, 0x5d, 0x07, 0x00, 0x02, 0x00, 0x00, 0x10}, /* sensor on */
+       {}
+};
 static const u8 mo4000_sensor_init[][8] = {
        {0xa1, 0x21, 0x01, 0x02, 0x00, 0x00, 0x00, 0x10},
        {0xa1, 0x21, 0x02, 0x00, 0x00, 0x00, 0x00, 0x10},
@@ -773,23 +906,13 @@ static const u8 mo4000_sensor_init[][8] = {
 };
 static const u8 mt9v111_sensor_init[][8] = {
        {0xb1, 0x5c, 0x0d, 0x00, 0x01, 0x00, 0x00, 0x10}, /* reset? */
-       {0xdd, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
+       {DELAY, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
        {0xb1, 0x5c, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x10},
        {0xb1, 0x5c, 0x01, 0x00, 0x01, 0x00, 0x00, 0x10}, /* IFP select */
        {0xb1, 0x5c, 0x08, 0x04, 0x80, 0x00, 0x00, 0x10}, /* output fmt ctrl */
        {0xb1, 0x5c, 0x06, 0x00, 0x00, 0x00, 0x00, 0x10}, /* op mode ctrl */
-       {0xb1, 0x5c, 0x02, 0x00, 0x16, 0x00, 0x00, 0x10},
-       {0xb1, 0x5c, 0x03, 0x01, 0xe1, 0x00, 0x00, 0x10},
-       {0xb1, 0x5c, 0x04, 0x02, 0x81, 0x00, 0x00, 0x10},
-       {0xb1, 0x5c, 0x05, 0x00, 0x04, 0x00, 0x00, 0x10},
        {0xb1, 0x5c, 0x01, 0x00, 0x04, 0x00, 0x00, 0x10}, /* sensor select */
-       {0xb1, 0x5c, 0x02, 0x00, 0x16, 0x00, 0x00, 0x10},
-       {0xb1, 0x5c, 0x03, 0x01, 0xe6, 0x00, 0x00, 0x10},
-       {0xb1, 0x5c, 0x04, 0x02, 0x86, 0x00, 0x00, 0x10},
-       {0xb1, 0x5c, 0x05, 0x00, 0x04, 0x00, 0x00, 0x10},
-       {0xb1, 0x5c, 0x06, 0x00, 0x00, 0x00, 0x00, 0x10},
        {0xb1, 0x5c, 0x08, 0x00, 0x08, 0x00, 0x00, 0x10}, /* row start */
-       {0xb1, 0x5c, 0x0e, 0x00, 0x08, 0x00, 0x00, 0x10},
        {0xb1, 0x5c, 0x02, 0x00, 0x16, 0x00, 0x00, 0x10}, /* col start */
        {0xb1, 0x5c, 0x03, 0x01, 0xe7, 0x00, 0x00, 0x10}, /* window height */
        {0xb1, 0x5c, 0x04, 0x02, 0x87, 0x00, 0x00, 0x10}, /* window width */
@@ -803,15 +926,10 @@ static const u8 mt9v111_sensor_init[][8] = {
        {}
 };
 static const u8 mt9v111_sensor_param1[][8] = {
-       {0xb1, 0x5c, 0x20, 0x00, 0x00, 0x00, 0x00, 0x10},
-       {0xb1, 0x5c, 0x20, 0x00, 0x00, 0x00, 0x00, 0x10},
-       {0xb1, 0x5c, 0x09, 0x01, 0x2c, 0x00, 0x00, 0x10},
-       {0xd1, 0x5c, 0x2b, 0x00, 0x33, 0x00, 0xa0, 0x10}, /* green1 gain */
-       {0xd1, 0x5c, 0x2d, 0x00, 0xa0, 0x00, 0x33, 0x10}, /* red gain */
-       /*******/
-       {0xb1, 0x5c, 0x06, 0x00, 0x1e, 0x00, 0x00, 0x10}, /* vert blanking */
-       {0xb1, 0x5c, 0x05, 0x00, 0x0a, 0x00, 0x00, 0x10}, /* horiz blanking */
-       {0xd1, 0x5c, 0x2c, 0x00, 0xad, 0x00, 0xad, 0x10}, /* blue gain */
+       {0xd1, 0x5c, 0x2b, 0x00, 0x33, 0x00, 0xad, 0x10}, /* G1 and B gains */
+       {0xd1, 0x5c, 0x2d, 0x00, 0xad, 0x00, 0x33, 0x10}, /* R and G2 gains */
+       {0xb1, 0x5c, 0x06, 0x00, 0x40, 0x00, 0x00, 0x10}, /* vert blanking */
+       {0xb1, 0x5c, 0x05, 0x00, 0x09, 0x00, 0x00, 0x10}, /* horiz blanking */
        {0xb1, 0x5c, 0x35, 0x01, 0xc0, 0x00, 0x00, 0x10}, /* global gain */
        {}
 };
@@ -861,10 +979,10 @@ static const u8 om6802_sensor_param1[][8] = {
 static const u8 ov7630_sensor_init[][8] = {
        {0xa1, 0x21, 0x76, 0x01, 0x00, 0x00, 0x00, 0x10},
        {0xa1, 0x21, 0x12, 0xc8, 0x00, 0x00, 0x00, 0x10},
-       {0xdd, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
+       {DELAY, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
        {0xa1, 0x21, 0x12, 0x48, 0x00, 0x00, 0x00, 0x10},
        {0xa1, 0x21, 0x12, 0xc8, 0x00, 0x00, 0x00, 0x10},
-       {0xdd, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
+       {DELAY, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
        {0xa1, 0x21, 0x12, 0x48, 0x00, 0x00, 0x00, 0x10},
 /* win: i2c_r from 00 to 80 */
        {0xd1, 0x21, 0x03, 0x80, 0x10, 0x20, 0x80, 0x10},
@@ -918,7 +1036,7 @@ static const u8 ov7630_sensor_param1[][8] = {
 static const u8 ov7648_sensor_init[][8] = {
        {0xa1, 0x21, 0x76, 0x00, 0x00, 0x00, 0x00, 0x10},
        {0xa1, 0x21, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},       /* reset */
-       {0xdd, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
+       {DELAY, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
        {0xa1, 0x21, 0x12, 0x00, 0x00, 0x00, 0x00, 0x10},
        {0xd1, 0x21, 0x03, 0xa4, 0x30, 0x88, 0x00, 0x10},
        {0xb1, 0x21, 0x11, 0x80, 0x08, 0x00, 0x00, 0x10},
@@ -967,7 +1085,7 @@ static const u8 ov7648_sensor_param1[][8] = {
 
 static const u8 ov7660_sensor_init[][8] = {
        {0xa1, 0x21, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10}, /* reset SCCB */
-       {0xdd, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
+       {DELAY, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
        {0xa1, 0x21, 0x12, 0x05, 0x00, 0x00, 0x00, 0x10},
                                                /* Outformat = rawRGB */
        {0xa1, 0x21, 0x13, 0xb8, 0x00, 0x00, 0x00, 0x10}, /* init COM8 */
@@ -1063,7 +1181,7 @@ static const u8 ov7660_sensor_param1[][8] = {
 static const u8 po1030_sensor_init[][8] = {
 /* the sensor registers are described in m5602/m5602_po1030.h */
        {0xa1, 0x6e, 0x3f, 0x20, 0x00, 0x00, 0x00, 0x10}, /* sensor reset */
-       {0xdd, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
+       {DELAY, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 20ms */
        {0xa1, 0x6e, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x10},
        {0xa1, 0x6e, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x10},
        {0xd1, 0x6e, 0x04, 0x02, 0xb1, 0x02, 0x39, 0x10},
@@ -1117,10 +1235,10 @@ static const u8 po1030_sensor_param1[][8] = {
 static const u8 po2030n_sensor_init[][8] = {
        {0xa1, 0x6e, 0x1e, 0x1a, 0x00, 0x00, 0x00, 0x10},
        {0xa1, 0x6e, 0x1f, 0x99, 0x00, 0x00, 0x00, 0x10},
-       {0xdd, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 10ms */
+       {DELAY, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 10ms */
        {0xa1, 0x6e, 0x1e, 0x0a, 0x00, 0x00, 0x00, 0x10},
        {0xa1, 0x6e, 0x1f, 0x19, 0x00, 0x00, 0x00, 0x10},
-       {0xdd, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 10ms */
+       {DELAY, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 10ms */
        {0xa1, 0x6e, 0x20, 0x44, 0x00, 0x00, 0x00, 0x10},
        {0xa1, 0x6e, 0x04, 0x03, 0x00, 0x00, 0x00, 0x10},
        {0xa1, 0x6e, 0x05, 0x70, 0x00, 0x00, 0x00, 0x10},
@@ -1169,30 +1287,22 @@ static const u8 po2030n_sensor_init[][8] = {
 };
 static const u8 po2030n_sensor_param1[][8] = {
        {0xa1, 0x6e, 0x1a, 0x01, 0x00, 0x00, 0x00, 0x10},
-       {0xdd, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 8ms */
+       {DELAY, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 8ms */
        {0xa1, 0x6e, 0x1b, 0xf4, 0x00, 0x00, 0x00, 0x10},
        {0xa1, 0x6e, 0x15, 0x04, 0x00, 0x00, 0x00, 0x10},
-       {0xd1, 0x6e, 0x16, 0x50, 0x40, 0x49, 0x40, 0x10},
+       {0xd1, 0x6e, 0x16, 0x40, 0x40, 0x40, 0x40, 0x10}, /* RGBG gains */
 /*param2*/
        {0xa1, 0x6e, 0x1d, 0x00, 0x00, 0x00, 0x00, 0x10},
        {0xa1, 0x6e, 0x04, 0x03, 0x00, 0x00, 0x00, 0x10},
        {0xa1, 0x6e, 0x05, 0x6f, 0x00, 0x00, 0x00, 0x10},
        {0xa1, 0x6e, 0x06, 0x02, 0x00, 0x00, 0x00, 0x10},
        {0xa1, 0x6e, 0x07, 0x25, 0x00, 0x00, 0x00, 0x10},
-       {0xa1, 0x6e, 0x15, 0x04, 0x00, 0x00, 0x00, 0x10},
-       {0xc1, 0x6e, 0x16, 0x52, 0x40, 0x48, 0x00, 0x10},
-/*after start*/
-       {0xa1, 0x6e, 0x15, 0x0f, 0x00, 0x00, 0x00, 0x10},
-       {0xdd, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 5ms */
-       {0xa1, 0x6e, 0x1a, 0x05, 0x00, 0x00, 0x00, 0x10},
-       {0xdd, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 5ms */
-       {0xa1, 0x6e, 0x1b, 0x53, 0x00, 0x00, 0x00, 0x10},
        {}
 };
 
 static const u8 soi768_sensor_init[][8] = {
        {0xa1, 0x21, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10}, /* reset */
-       {0xdd, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 96ms */
+       {DELAY, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, /* delay 96ms */
        {0xa1, 0x21, 0x12, 0x00, 0x00, 0x00, 0x00, 0x10},
        {0xa1, 0x21, 0x13, 0x80, 0x00, 0x00, 0x00, 0x10},
        {0xa1, 0x21, 0x0f, 0x03, 0x00, 0x00, 0x00, 0x10},
@@ -1216,7 +1326,7 @@ static const u8 soi768_sensor_param1[][8] = {
                        /* global gain ? : 07 - change with 0x15 at the end */
        {0xa1, 0x21, 0x10, 0x3f, 0x00, 0x00, 0x00, 0x10}, /* ???? : 063f */
        {0xa1, 0x21, 0x04, 0x06, 0x00, 0x00, 0x00, 0x10},
-       {0xb1, 0x21, 0x2d, 0x00, 0x02, 0x00, 0x00, 0x10},
+       {0xb1, 0x21, 0x2d, 0x63, 0x03, 0x00, 0x00, 0x10},
                        /* exposure ? : 0200 - change with 0x1e at the end */
        {}
 };
@@ -1311,6 +1421,7 @@ static const u8 (*sensor_init[])[8] = {
 [SENSOR_GC0307] =      gc0307_sensor_init,
 [SENSOR_HV7131R] =     hv7131r_sensor_init,
 [SENSOR_MI0360] =      mi0360_sensor_init,
+[SENSOR_MI0360B] =     mi0360b_sensor_init,
 [SENSOR_MO4000] =      mo4000_sensor_init,
 [SENSOR_MT9V111] =     mt9v111_sensor_init,
 [SENSOR_OM6802] =      om6802_sensor_init,
@@ -1327,13 +1438,17 @@ static const u8 (*sensor_init[])[8] = {
 static void reg_r(struct gspca_dev *gspca_dev,
                  u16 value, int len)
 {
+       int ret;
+
+       if (gspca_dev->usb_err < 0)
+               return;
 #ifdef GSPCA_DEBUG
        if (len > USB_BUF_SZ) {
-               err("reg_r: buffer overflow");
+               pr_err("reg_r: buffer overflow\n");
                return;
        }
 #endif
-       usb_control_msg(gspca_dev->dev,
+       ret = usb_control_msg(gspca_dev->dev,
                        usb_rcvctrlpipe(gspca_dev->dev, 0),
                        0,
                        USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
@@ -1341,15 +1456,23 @@ static void reg_r(struct gspca_dev *gspca_dev,
                        gspca_dev->usb_buf, len,
                        500);
        PDEBUG(D_USBI, "reg_r [%02x] -> %02x", value, gspca_dev->usb_buf[0]);
+       if (ret < 0) {
+               pr_err("reg_r err %d\n", ret);
+               gspca_dev->usb_err = ret;
+       }
 }
 
 static void reg_w1(struct gspca_dev *gspca_dev,
                   u16 value,
                   u8 data)
 {
+       int ret;
+
+       if (gspca_dev->usb_err < 0)
+               return;
        PDEBUG(D_USBO, "reg_w1 [%04x] = %02x", value, data);
        gspca_dev->usb_buf[0] = data;
-       usb_control_msg(gspca_dev->dev,
+       ret = usb_control_msg(gspca_dev->dev,
                        usb_sndctrlpipe(gspca_dev->dev, 0),
                        0x08,
                        USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
@@ -1357,35 +1480,50 @@ static void reg_w1(struct gspca_dev *gspca_dev,
                        0,
                        gspca_dev->usb_buf, 1,
                        500);
+       if (ret < 0) {
+               pr_err("reg_w1 err %d\n", ret);
+               gspca_dev->usb_err = ret;
+       }
 }
 static void reg_w(struct gspca_dev *gspca_dev,
                          u16 value,
                          const u8 *buffer,
                          int len)
 {
+       int ret;
+
+       if (gspca_dev->usb_err < 0)
+               return;
        PDEBUG(D_USBO, "reg_w [%04x] = %02x %02x ..",
                value, buffer[0], buffer[1]);
 #ifdef GSPCA_DEBUG
        if (len > USB_BUF_SZ) {
-               err("reg_w: buffer overflow");
+               pr_err("reg_w: buffer overflow\n");
                return;
        }
 #endif
        memcpy(gspca_dev->usb_buf, buffer, len);
-       usb_control_msg(gspca_dev->dev,
+       ret = usb_control_msg(gspca_dev->dev,
                        usb_sndctrlpipe(gspca_dev->dev, 0),
                        0x08,
                        USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
                        value, 0,
                        gspca_dev->usb_buf, len,
                        500);
+       if (ret < 0) {
+               pr_err("reg_w err %d\n", ret);
+               gspca_dev->usb_err = ret;
+       }
 }
 
 /* I2C write 1 byte */
 static void i2c_w1(struct gspca_dev *gspca_dev, u8 reg, u8 val)
 {
        struct sd *sd = (struct sd *) gspca_dev;
+       int ret;
 
+       if (gspca_dev->usb_err < 0)
+               return;
        PDEBUG(D_USBO, "i2c_w1 [%02x] = %02x", reg, val);
        switch (sd->sensor) {
        case SENSOR_ADCM1700:
@@ -1404,7 +1542,7 @@ static void i2c_w1(struct gspca_dev *gspca_dev, u8 reg, u8 val)
        gspca_dev->usb_buf[5] = 0;
        gspca_dev->usb_buf[6] = 0;
        gspca_dev->usb_buf[7] = 0x10;
-       usb_control_msg(gspca_dev->dev,
+       ret = usb_control_msg(gspca_dev->dev,
                        usb_sndctrlpipe(gspca_dev->dev, 0),
                        0x08,
                        USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
@@ -1412,16 +1550,24 @@ static void i2c_w1(struct gspca_dev *gspca_dev, u8 reg, u8 val)
                        0,
                        gspca_dev->usb_buf, 8,
                        500);
+       if (ret < 0) {
+               pr_err("i2c_w1 err %d\n", ret);
+               gspca_dev->usb_err = ret;
+       }
 }
 
 /* I2C write 8 bytes */
 static void i2c_w8(struct gspca_dev *gspca_dev,
                   const u8 *buffer)
 {
+       int ret;
+
+       if (gspca_dev->usb_err < 0)
+               return;
        PDEBUG(D_USBO, "i2c_w8 [%02x] = %02x ..",
                buffer[2], buffer[3]);
        memcpy(gspca_dev->usb_buf, buffer, 8);
-       usb_control_msg(gspca_dev->dev,
+       ret = usb_control_msg(gspca_dev->dev,
                        usb_sndctrlpipe(gspca_dev->dev, 0),
                        0x08,
                        USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
@@ -1429,6 +1575,10 @@ static void i2c_w8(struct gspca_dev *gspca_dev,
                        gspca_dev->usb_buf, 8,
                        500);
        msleep(2);
+       if (ret < 0) {
+               pr_err("i2c_w8 err %d\n", ret);
+               gspca_dev->usb_err = ret;
+       }
 }
 
 /* sensor read 'len' (1..5) bytes in gspca_dev->usb_buf */
@@ -1467,7 +1617,7 @@ static void i2c_w_seq(struct gspca_dev *gspca_dev,
                        const u8 (*data)[8])
 {
        while ((*data)[0] != 0) {
-               if ((*data)[0] != 0xdd)
+               if ((*data)[0] != DELAY)
                        i2c_w8(gspca_dev, *data);
                else
                        msleep((*data)[1]);
@@ -1475,22 +1625,22 @@ static void i2c_w_seq(struct gspca_dev *gspca_dev,
        }
 }
 
+/* check the ID of the hv7131 sensor */
+/* this sequence is needed because it activates the sensor */
 static void hv7131r_probe(struct gspca_dev *gspca_dev)
 {
-       i2c_w1(gspca_dev, 0x02, 0);                     /* sensor wakeup */
+       i2c_w1(gspca_dev, 0x02, 0);             /* sensor wakeup */
        msleep(10);
-       reg_w1(gspca_dev, 0x02, 0x66);                  /* Gpio on */
+       reg_w1(gspca_dev, 0x02, 0x66);          /* Gpio on */
        msleep(10);
-       i2c_r(gspca_dev, 0, 5);                         /* read sensor id */
-       if (gspca_dev->usb_buf[0] == 0x02
+       i2c_r(gspca_dev, 0, 5);                 /* read sensor id */
+       if (gspca_dev->usb_buf[0] == 0x02       /* chip ID (02 is R) */
            && gspca_dev->usb_buf[1] == 0x09
-           && gspca_dev->usb_buf[2] == 0x01
-           && gspca_dev->usb_buf[3] == 0x00
-           && gspca_dev->usb_buf[4] == 0x00) {
-               PDEBUG(D_PROBE, "Sensor sn9c102P HV7131R found");
+           && gspca_dev->usb_buf[2] == 0x01) {
+               PDEBUG(D_PROBE, "Sensor HV7131R found");
                return;
        }
-       PDEBUG(D_PROBE, "Sensor 0x%02x 0x%02x 0x%02x - sn9c102P not found",
+       pr_warn("Erroneous HV7131R ID 0x%02x 0x%02x 0x%02x\n",
                gspca_dev->usb_buf[0], gspca_dev->usb_buf[1],
                gspca_dev->usb_buf[2]);
 }
@@ -1530,7 +1680,13 @@ static void mi0360_probe(struct gspca_dev *gspca_dev)
                if (val != 0xffff)
                        break;
        }
+       if (gspca_dev->usb_err < 0)
+               return;
        switch (val) {
+       case 0x8221:
+               PDEBUG(D_PROBE, "Sensor mi0360b");
+               sd->sensor = SENSOR_MI0360B;
+               break;
        case 0x823a:
                PDEBUG(D_PROBE, "Sensor mt9v111");
                sd->sensor = SENSOR_MT9V111;
@@ -1557,6 +1713,8 @@ static void ov7630_probe(struct gspca_dev *gspca_dev)
        val = (gspca_dev->usb_buf[3] << 8) | gspca_dev->usb_buf[4];
        reg_w1(gspca_dev, 0x01, 0x29);
        reg_w1(gspca_dev, 0x17, 0x42);
+       if (gspca_dev->usb_err < 0)
+               return;
        if (val == 0x7628) {                    /* soi768 */
                sd->sensor = SENSOR_SOI768;
 /*fixme: only valid for 0c45:613e?*/
@@ -1594,13 +1752,14 @@ static void ov7648_probe(struct gspca_dev *gspca_dev)
        val = (gspca_dev->usb_buf[3] << 8) | gspca_dev->usb_buf[4];
        reg_w1(gspca_dev, 0x01, 0x29);
        reg_w1(gspca_dev, 0x17, 0x42);
+       if (gspca_dev->usb_err < 0)
+               return;
        if (val == 0x1030) {                    /* po1030 */
                PDEBUG(D_PROBE, "Sensor po1030");
                sd->sensor = SENSOR_PO1030;
                return;
        }
-
-       PDEBUG(D_PROBE, "Unknown sensor %04x", val);
+       pr_err("Unknown sensor %04x\n", val);
 }
 
 /* 0c45:6142 sensor may be po2030n, gc0305 or gc0307 */
@@ -1632,137 +1791,13 @@ static void po2030n_probe(struct gspca_dev *gspca_dev)
        val = (gspca_dev->usb_buf[3] << 8) | gspca_dev->usb_buf[4];
        reg_w1(gspca_dev, 0x01, 0x29);
        reg_w1(gspca_dev, 0x17, 0x42);
+       if (gspca_dev->usb_err < 0)
+               return;
        if (val == 0x2030) {
                PDEBUG(D_PROBE, "Sensor po2030n");
 /*             sd->sensor = SENSOR_PO2030N; */
        } else {
-               PDEBUG(D_PROBE, "Unknown sensor ID %04x", val);
-       }
-}
-
-static void bridge_init(struct gspca_dev *gspca_dev,
-                         const u8 *sn9c1xx)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
-       const u8 *reg9a;
-       static const u8 reg9a_def[] =
-               {0x00, 0x40, 0x20, 0x00, 0x00, 0x00};
-       static const u8 reg9a_spec[] =
-               {0x00, 0x40, 0x38, 0x30, 0x00, 0x20};
-       static const u8 regd4[] = {0x60, 0x00, 0x00};
-
-       /* sensor clock already enabled in sd_init */
-       /* reg_w1(gspca_dev, 0xf1, 0x00); */
-       reg_w1(gspca_dev, 0x01, sn9c1xx[1]);
-
-       /* configure gpio */
-       reg_w(gspca_dev, 0x01, &sn9c1xx[1], 2);
-       reg_w(gspca_dev, 0x08, &sn9c1xx[8], 2);
-       reg_w(gspca_dev, 0x17, &sn9c1xx[0x17], 5);
-       switch (sd->sensor) {
-       case SENSOR_GC0307:
-       case SENSOR_OV7660:
-       case SENSOR_PO1030:
-       case SENSOR_PO2030N:
-       case SENSOR_SOI768:
-       case SENSOR_SP80708:
-               reg9a = reg9a_spec;
-               break;
-       default:
-               reg9a = reg9a_def;
-               break;
-       }
-       reg_w(gspca_dev, 0x9a, reg9a, 6);
-
-       reg_w(gspca_dev, 0xd4, regd4, sizeof regd4);
-
-       reg_w(gspca_dev, 0x03, &sn9c1xx[3], 0x0f);
-
-       switch (sd->sensor) {
-       case SENSOR_ADCM1700:
-               reg_w1(gspca_dev, 0x01, 0x43);
-               reg_w1(gspca_dev, 0x17, 0x62);
-               reg_w1(gspca_dev, 0x01, 0x42);
-               reg_w1(gspca_dev, 0x01, 0x42);
-               break;
-       case SENSOR_GC0307:
-               msleep(50);
-               reg_w1(gspca_dev, 0x01, 0x61);
-               reg_w1(gspca_dev, 0x17, 0x22);
-               reg_w1(gspca_dev, 0x01, 0x60);
-               reg_w1(gspca_dev, 0x01, 0x40);
-               msleep(50);
-               break;
-       case SENSOR_MT9V111:
-               reg_w1(gspca_dev, 0x01, 0x61);
-               reg_w1(gspca_dev, 0x17, 0x61);
-               reg_w1(gspca_dev, 0x01, 0x60);
-               reg_w1(gspca_dev, 0x01, 0x40);
-               break;
-       case SENSOR_OM6802:
-               msleep(10);
-               reg_w1(gspca_dev, 0x02, 0x73);
-               reg_w1(gspca_dev, 0x17, 0x60);
-               reg_w1(gspca_dev, 0x01, 0x22);
-               msleep(100);
-               reg_w1(gspca_dev, 0x01, 0x62);
-               reg_w1(gspca_dev, 0x17, 0x64);
-               reg_w1(gspca_dev, 0x17, 0x64);
-               reg_w1(gspca_dev, 0x01, 0x42);
-               msleep(10);
-               reg_w1(gspca_dev, 0x01, 0x42);
-               i2c_w8(gspca_dev, om6802_init0[0]);
-               i2c_w8(gspca_dev, om6802_init0[1]);
-               msleep(15);
-               reg_w1(gspca_dev, 0x02, 0x71);
-               msleep(150);
-               break;
-       case SENSOR_OV7630:
-               reg_w1(gspca_dev, 0x01, 0x61);
-               reg_w1(gspca_dev, 0x17, 0xe2);
-               reg_w1(gspca_dev, 0x01, 0x60);
-               reg_w1(gspca_dev, 0x01, 0x40);
-               break;
-       case SENSOR_OV7648:
-               reg_w1(gspca_dev, 0x01, 0x63);
-               reg_w1(gspca_dev, 0x17, 0x20);
-               reg_w1(gspca_dev, 0x01, 0x62);
-               reg_w1(gspca_dev, 0x01, 0x42);
-               break;
-       case SENSOR_PO1030:
-       case SENSOR_SOI768:
-               reg_w1(gspca_dev, 0x01, 0x61);
-               reg_w1(gspca_dev, 0x17, 0x20);
-               reg_w1(gspca_dev, 0x01, 0x60);
-               reg_w1(gspca_dev, 0x01, 0x40);
-               break;
-       case SENSOR_PO2030N:
-               reg_w1(gspca_dev, 0x01, 0x63);
-               reg_w1(gspca_dev, 0x17, 0x20);
-               reg_w1(gspca_dev, 0x01, 0x62);
-               reg_w1(gspca_dev, 0x01, 0x42);
-               break;
-       case SENSOR_OV7660:
-               /* fall thru */
-       case SENSOR_SP80708:
-               reg_w1(gspca_dev, 0x01, 0x63);
-               reg_w1(gspca_dev, 0x17, 0x20);
-               reg_w1(gspca_dev, 0x01, 0x62);
-               reg_w1(gspca_dev, 0x01, 0x42);
-               msleep(100);
-               reg_w1(gspca_dev, 0x02, 0x62);
-               break;
-       default:
-/*     case SENSOR_HV7131R: */
-/*     case SENSOR_MI0360: */
-/*     case SENSOR_MO4000: */
-               reg_w1(gspca_dev, 0x01, 0x43);
-               reg_w1(gspca_dev, 0x17, 0x61);
-               reg_w1(gspca_dev, 0x01, 0x42);
-               if (sd->sensor == SENSOR_HV7131R
-                   && sd->bridge == BRIDGE_SN9C102P)
-                       hv7131r_probe(gspca_dev);
-               break;
+               pr_err("Unknown sensor ID %04x\n", val);
        }
 }
 
@@ -1774,7 +1809,8 @@ static int sd_config(struct gspca_dev *gspca_dev,
        struct cam *cam;
 
        sd->bridge = id->driver_info >> 16;
-       sd->sensor = id->driver_info;
+       sd->sensor = id->driver_info >> 8;
+       sd->flags = id->driver_info;
 
        cam = &gspca_dev->cam;
        if (sd->sensor == SENSOR_ADCM1700) {
@@ -1785,28 +1821,12 @@ static int sd_config(struct gspca_dev *gspca_dev,
                cam->nmodes = ARRAY_SIZE(vga_mode);
        }
        cam->npkt = 24;                 /* 24 packets per ISOC message */
+       cam->ctrls = sd->ctrls;
 
-       sd->brightness = BRIGHTNESS_DEF;
-       sd->contrast = CONTRAST_DEF;
-       sd->colors = COLOR_DEF;
-       sd->blue = BLUE_BALANCE_DEF;
-       sd->red = RED_BALANCE_DEF;
-       sd->gamma = GAMMA_DEF;
-       sd->autogain = AUTOGAIN_DEF;
        sd->ag_cnt = -1;
-       sd->vflip = VFLIP_DEF;
-       switch (sd->sensor) {
-       case SENSOR_OM6802:
-               sd->sharpness = 0x10;
-               break;
-       default:
-               sd->sharpness = SHARPNESS_DEF;
-               break;
-       }
-       sd->infrared = INFRARED_DEF;
-       sd->freq = FREQ_DEF;
        sd->quality = QUALITY_DEF;
-       sd->jpegqual = 80;
+
+       INIT_WORK(&sd->work, qual_upd);
 
        return 0;
 }
@@ -1816,58 +1836,60 @@ static int sd_init(struct gspca_dev *gspca_dev)
 {
        struct sd *sd = (struct sd *) gspca_dev;
        const u8 *sn9c1xx;
-       u8 regGpio[] = { 0x29, 0x74 };
+       u8 regGpio[] = { 0x29, 0x70 };          /* no audio */
        u8 regF1;
 
        /* setup a selector by bridge */
        reg_w1(gspca_dev, 0xf1, 0x01);
        reg_r(gspca_dev, 0x00, 1);
-       reg_w1(gspca_dev, 0xf1, gspca_dev->usb_buf[0]);
+       reg_w1(gspca_dev, 0xf1, 0x00);
        reg_r(gspca_dev, 0x00, 1);              /* get sonix chip id */
        regF1 = gspca_dev->usb_buf[0];
+       if (gspca_dev->usb_err < 0)
+               return gspca_dev->usb_err;
        PDEBUG(D_PROBE, "Sonix chip id: %02x", regF1);
+       if (gspca_dev->audio)
+               regGpio[1] |= 0x04;             /* with audio */
        switch (sd->bridge) {
        case BRIDGE_SN9C102P:
-               if (regF1 != 0x11)
-                       return -ENODEV;
-               reg_w1(gspca_dev, 0x02, regGpio[1]);
-               break;
        case BRIDGE_SN9C105:
                if (regF1 != 0x11)
                        return -ENODEV;
-               if (sd->sensor == SENSOR_MI0360)
-                       mi0360_probe(gspca_dev);
-               reg_w(gspca_dev, 0x01, regGpio, 2);
-               break;
-       case BRIDGE_SN9C120:
-               if (regF1 != 0x12)
-                       return -ENODEV;
-               switch (sd->sensor) {
-               case SENSOR_MI0360:
-                       mi0360_probe(gspca_dev);
-                       break;
-               case SENSOR_OV7630:
-                       ov7630_probe(gspca_dev);
-                       break;
-               case SENSOR_OV7648:
-                       ov7648_probe(gspca_dev);
-                       break;
-               case SENSOR_PO2030N:
-                       po2030n_probe(gspca_dev);
-                       break;
-               }
-               regGpio[1] = 0x70;
-               reg_w(gspca_dev, 0x01, regGpio, 2);
                break;
        default:
 /*     case BRIDGE_SN9C110: */
-/*     case BRIDGE_SN9C325: */
+/*     case BRIDGE_SN9C120: */
                if (regF1 != 0x12)
                        return -ENODEV;
-               reg_w1(gspca_dev, 0x02, 0x62);
+       }
+
+       switch (sd->sensor) {
+       case SENSOR_MI0360:
+               mi0360_probe(gspca_dev);
+               break;
+       case SENSOR_OV7630:
+               ov7630_probe(gspca_dev);
+               break;
+       case SENSOR_OV7648:
+               ov7648_probe(gspca_dev);
+               break;
+       case SENSOR_PO2030N:
+               po2030n_probe(gspca_dev);
+               break;
+       }
+
+       switch (sd->bridge) {
+       case BRIDGE_SN9C102P:
+               reg_w1(gspca_dev, 0x02, regGpio[1]);
+               break;
+       default:
+               reg_w(gspca_dev, 0x01, regGpio, 2);
                break;
        }
 
+       if (sd->sensor == SENSOR_OM6802)
+               sd->ctrls[SHARPNESS].def = 0x10;
+
        /* Note we do not disable the sensor clock here (power saving mode),
           as that also disables the button on the cam. */
        reg_w1(gspca_dev, 0xf1, 0x00);
@@ -1877,11 +1899,13 @@ static int sd_init(struct gspca_dev *gspca_dev)
        sd->i2c_addr = sn9c1xx[9];
 
        gspca_dev->ctrl_dis = ctrl_dis[sd->sensor];
+       if (!(sd->flags & F_ILLUM))
+               gspca_dev->ctrl_dis |= (1 << ILLUM);
 
-       return 0;
+       return gspca_dev->usb_err;
 }
 
-static u32 setexposure(struct gspca_dev *gspca_dev,
+static u32 expo_adjust(struct gspca_dev *gspca_dev,
                        u32 expo)
 {
        struct sd *sd = (struct sd *) gspca_dev;
@@ -1909,7 +1933,8 @@ static u32 setexposure(struct gspca_dev *gspca_dev,
                i2c_w8(gspca_dev, Expodoit);
                break;
            }
-       case SENSOR_MI0360: {
+       case SENSOR_MI0360:
+       case SENSOR_MI0360B: {
                u8 expoMi[] =           /* exposure 0x0635 -> 4 fp/s 0x10 */
                        { 0xb1, 0x5d, 0x09, 0x00, 0x00, 0x00, 0x00, 0x16 };
                static const u8 doit[] =                /* update sensor */
@@ -1956,10 +1981,10 @@ static u32 setexposure(struct gspca_dev *gspca_dev,
                u8 expo_c1[] =
                        { 0xb1, 0x5c, 0x09, 0x00, 0x00, 0x00, 0x00, 0x10 };
 
-               if (expo > 0x0280)
-                       expo = 0x0280;
-               else if (expo < 0x0040)
-                       expo = 0x0040;
+               if (expo > 0x0390)
+                       expo = 0x0390;
+               else if (expo < 0x0060)
+                       expo = 0x0060;
                expo_c1[3] = expo >> 8;
                expo_c1[4] = expo;
                i2c_w8(gspca_dev, expo_c1);
@@ -1988,37 +2013,45 @@ static void setbrightness(struct gspca_dev *gspca_dev)
 {
        struct sd *sd = (struct sd *) gspca_dev;
        unsigned int expo;
+       int brightness;
        u8 k2;
 
-       k2 = ((int) sd->brightness - 0x8000) >> 10;
+       brightness = sd->ctrls[BRIGHTNESS].val;
+       k2 = (brightness - 0x80) >> 2;
        switch (sd->sensor) {
        case SENSOR_ADCM1700:
                if (k2 > 0x1f)
                        k2 = 0;         /* only positive Y offset */
                break;
        case SENSOR_HV7131R:
-               expo = sd->brightness << 4;
+               expo = brightness << 12;
                if (expo > 0x002dc6c0)
                        expo = 0x002dc6c0;
                else if (expo < 0x02a0)
                        expo = 0x02a0;
-               sd->exposure = setexposure(gspca_dev, expo);
+               sd->exposure = expo_adjust(gspca_dev, expo);
                break;
        case SENSOR_MI0360:
        case SENSOR_MO4000:
-               expo = sd->brightness >> 4;
-               sd->exposure = setexposure(gspca_dev, expo);
+               expo = brightness << 4;
+               sd->exposure = expo_adjust(gspca_dev, expo);
+               break;
+       case SENSOR_MI0360B:
+               expo = brightness << 2;
+               sd->exposure = expo_adjust(gspca_dev, expo);
                break;
        case SENSOR_GC0307:
+               expo = brightness;
+               sd->exposure = expo_adjust(gspca_dev, expo);
+               return;                 /* don't set the Y offset */
        case SENSOR_MT9V111:
-               expo = sd->brightness >> 8;
-               sd->exposure = setexposure(gspca_dev, expo);
+               expo = brightness << 2;
+               sd->exposure = expo_adjust(gspca_dev, expo);
                return;                 /* don't set the Y offset */
        case SENSOR_OM6802:
-               expo = sd->brightness >> 6;
-               sd->exposure = setexposure(gspca_dev, expo);
-               k2 = sd->brightness >> 11;
-               break;
+               expo = brightness << 2;
+               sd->exposure = expo_adjust(gspca_dev, expo);
+               return;                 /* Y offset already set */
        }
 
        reg_w1(gspca_dev, 0x96, k2);    /* color matrix Y offset */
@@ -2030,12 +2063,13 @@ static void setcontrast(struct gspca_dev *gspca_dev)
        u8 k2;
        u8 contrast[6];
 
-       k2 = sd->contrast * 0x30 / (CONTRAST_MAX + 1) + 0x10;   /* 10..40 */
+       k2 = sd->ctrls[CONTRAST].val * 37 / (CONTRAST_MAX + 1)
+                               + 37;           /* 37..73 */
        contrast[0] = (k2 + 1) / 2;             /* red */
        contrast[1] = 0;
        contrast[2] = k2;                       /* green */
        contrast[3] = 0;
-       contrast[4] = (k2 + 1) / 5;             /* blue */
+       contrast[4] = k2 / 5;                   /* blue */
        contrast[5] = 0;
        reg_w(gspca_dev, 0x84, contrast, sizeof contrast);
 }
@@ -2043,15 +2077,25 @@ static void setcontrast(struct gspca_dev *gspca_dev)
 static void setcolors(struct gspca_dev *gspca_dev)
 {
        struct sd *sd = (struct sd *) gspca_dev;
-       int i, v;
+       int i, v, colors;
+       const s16 *uv;
        u8 reg8a[12];                   /* U & V gains */
-       static const s16 uv[6] = {      /* same as reg84 in signed decimal */
+       static const s16 uv_com[6] = {  /* same as reg84 in signed decimal */
                -24, -38, 64,           /* UR UG UB */
                 62, -51, -9            /* VR VG VB */
        };
+       static const s16 uv_mi0360b[6] = {
+               -20, -38, 64,           /* UR UG UB */
+                60, -51, -9            /* VR VG VB */
+       };
 
+       colors = sd->ctrls[COLORS].val;
+       if (sd->sensor == SENSOR_MI0360B)
+               uv = uv_mi0360b;
+       else
+               uv = uv_com;
        for (i = 0; i < 6; i++) {
-               v = uv[i] * sd->colors / COLOR_DEF;
+               v = uv[i] * colors / COLORS_DEF;
                reg8a[i * 2] = v;
                reg8a[i * 2 + 1] = (v >> 8) & 0x0f;
        }
@@ -2062,15 +2106,25 @@ static void setredblue(struct gspca_dev *gspca_dev)
 {
        struct sd *sd = (struct sd *) gspca_dev;
 
-       reg_w1(gspca_dev, 0x05, sd->red);
+       if (sd->sensor == SENSOR_PO2030N) {
+               u8 rg1b[] =             /* red  green1 blue (no g2) */
+                       {0xc1, 0x6e, 0x16, 0x00, 0x40, 0x00, 0x00, 0x10};
+
+               /* 0x40 = normal value = gain x 1 */
+               rg1b[3] = sd->ctrls[RED].val * 2;
+               rg1b[5] = sd->ctrls[BLUE].val * 2;
+               i2c_w8(gspca_dev, rg1b);
+               return;
+       }
+       reg_w1(gspca_dev, 0x05, sd->ctrls[RED].val);
 /*     reg_w1(gspca_dev, 0x07, 32); */
-       reg_w1(gspca_dev, 0x06, sd->blue);
+       reg_w1(gspca_dev, 0x06, sd->ctrls[BLUE].val);
 }
 
 static void setgamma(struct gspca_dev *gspca_dev)
 {
        struct sd *sd = (struct sd *) gspca_dev;
-       int i;
+       int i, val;
        u8 gamma[17];
        const u8 *gamma_base;
        static const u8 delta[17] = {
@@ -2083,6 +2137,7 @@ static void setgamma(struct gspca_dev *gspca_dev)
                gamma_base = gamma_spec_0;
                break;
        case SENSOR_HV7131R:
+       case SENSOR_MI0360B:
        case SENSOR_MT9V111:
                gamma_base = gamma_spec_1;
                break;
@@ -2097,17 +2152,35 @@ static void setgamma(struct gspca_dev *gspca_dev)
                break;
        }
 
+       val = sd->ctrls[GAMMA].val;
        for (i = 0; i < sizeof gamma; i++)
                gamma[i] = gamma_base[i]
-                       + delta[i] * (sd->gamma - GAMMA_DEF) / 32;
+                       + delta[i] * (val - GAMMA_DEF) / 32;
        reg_w(gspca_dev, 0x20, gamma, sizeof gamma);
 }
 
+static void setexposure(struct gspca_dev *gspca_dev)
+{
+       struct sd *sd = (struct sd *) gspca_dev;
+
+       if (sd->sensor == SENSOR_PO2030N) {
+               u8 rexpo[] =            /* 1a: expo H, 1b: expo M */
+                       {0xa1, 0x6e, 0x1a, 0x00, 0x40, 0x00, 0x00, 0x10};
+
+               rexpo[3] = sd->ctrls[EXPOSURE].val >> 8;
+               i2c_w8(gspca_dev, rexpo);
+               msleep(6);
+               rexpo[2] = 0x1b;
+               rexpo[3] = sd->ctrls[EXPOSURE].val;
+               i2c_w8(gspca_dev, rexpo);
+       }
+}
+
 static void setautogain(struct gspca_dev *gspca_dev)
 {
        struct sd *sd = (struct sd *) gspca_dev;
 
-       if (gspca_dev->ctrl_dis & (1 << AUTOGAIN_IDX))
+       if (gspca_dev->ctrl_dis & (1 << AUTOGAIN))
                return;
        switch (sd->sensor) {
        case SENSOR_OV7630:
@@ -2118,74 +2191,113 @@ static void setautogain(struct gspca_dev *gspca_dev)
                        comb = 0xc0;
                else
                        comb = 0xa0;
-               if (sd->autogain)
+               if (sd->ctrls[AUTOGAIN].val)
                        comb |= 0x03;
                i2c_w1(&sd->gspca_dev, 0x13, comb);
                return;
            }
        }
-       if (sd->autogain)
+       if (sd->ctrls[AUTOGAIN].val)
                sd->ag_cnt = AG_CNT_START;
        else
                sd->ag_cnt = -1;
 }
 
-/* hv7131r/ov7630/ov7648 only */
-static void setvflip(struct sd *sd)
+static void setgain(struct gspca_dev *gspca_dev)
 {
+       struct sd *sd = (struct sd *) gspca_dev;
+
+       if (sd->sensor == SENSOR_PO2030N) {
+               u8 rgain[] =            /* 15: gain */
+                       {0xa1, 0x6e, 0x15, 0x00, 0x40, 0x00, 0x00, 0x15};
+
+               rgain[3] = sd->ctrls[GAIN].val;
+               i2c_w8(gspca_dev, rgain);
+       }
+}
+
+static void sethvflip(struct gspca_dev *gspca_dev)
+{
+       struct sd *sd = (struct sd *) gspca_dev;
        u8 comn;
 
-       if (sd->gspca_dev.ctrl_dis & (1 << VFLIP_IDX))
-               return;
        switch (sd->sensor) {
        case SENSOR_HV7131R:
                comn = 0x18;                    /* clkdiv = 1, ablcen = 1 */
-               if (sd->vflip)
+               if (sd->ctrls[VFLIP].val)
                        comn |= 0x01;
-               i2c_w1(&sd->gspca_dev, 0x01, comn);     /* sctra */
+               i2c_w1(gspca_dev, 0x01, comn);  /* sctra */
                break;
        case SENSOR_OV7630:
                comn = 0x02;
-               if (!sd->vflip)
+               if (!sd->ctrls[VFLIP].val)
                        comn |= 0x80;
-               i2c_w1(&sd->gspca_dev, 0x75, comn);
+               i2c_w1(gspca_dev, 0x75, comn);
                break;
-       default:
-/*     case SENSOR_OV7648: */
+       case SENSOR_OV7648:
                comn = 0x06;
-               if (sd->vflip)
+               if (sd->ctrls[VFLIP].val)
                        comn |= 0x80;
-               i2c_w1(&sd->gspca_dev, 0x75, comn);
+               i2c_w1(gspca_dev, 0x75, comn);
+               break;
+       case SENSOR_PO2030N:
+               /* Reg. 0x1E: Timing Generator Control Register 2 (Tgcontrol2)
+                * (reset value: 0x0A)
+                * bit7: HM: Horizontal Mirror: 0: disable, 1: enable
+                * bit6: VM: Vertical Mirror: 0: disable, 1: enable
+                * bit5: ST: Shutter Selection: 0: electrical, 1: mechanical
+                * bit4: FT: Single Frame Transfer: 0: disable, 1: enable
+                * bit3-0: X
+                */
+               comn = 0x0a;
+               if (sd->ctrls[HFLIP].val)
+                       comn |= 0x80;
+               if (sd->ctrls[VFLIP].val)
+                       comn |= 0x40;
+               i2c_w1(&sd->gspca_dev, 0x1e, comn);
                break;
        }
 }
 
-static void setsharpness(struct sd *sd)
+static void setsharpness(struct gspca_dev *gspca_dev)
 {
-       reg_w1(&sd->gspca_dev, 0x99, sd->sharpness);
+       struct sd *sd = (struct sd *) gspca_dev;
+
+       reg_w1(gspca_dev, 0x99, sd->ctrls[SHARPNESS].val);
 }
 
-static void setinfrared(struct sd *sd)
+static void setillum(struct gspca_dev *gspca_dev)
 {
-       if (sd->gspca_dev.ctrl_dis & (1 << INFRARED_IDX))
+       struct sd *sd = (struct sd *) gspca_dev;
+
+       if (gspca_dev->ctrl_dis & (1 << ILLUM))
                return;
-/*fixme: different sequence for StarCam Clip and StarCam 370i */
-/* Clip */
-       i2c_w1(&sd->gspca_dev, 0x02,                    /* gpio */
-               sd->infrared ? 0x66 : 0x64);
+       switch (sd->sensor) {
+       case SENSOR_ADCM1700:
+               reg_w1(gspca_dev, 0x02,                         /* gpio */
+                       sd->ctrls[ILLUM].val ? 0x64 : 0x60);
+               break;
+       case SENSOR_MT9V111:
+               reg_w1(gspca_dev, 0x02,
+                       sd->ctrls[ILLUM].val ? 0x77 : 0x74);
+/* should have been: */
+/*                                             0x55 : 0x54);   * 370i */
+/*                                             0x66 : 0x64);   * Clip */
+               break;
+       }
 }
 
 static void setfreq(struct gspca_dev *gspca_dev)
 {
        struct sd *sd = (struct sd *) gspca_dev;
 
-       if (gspca_dev->ctrl_dis & (1 << FREQ_IDX))
+       if (gspca_dev->ctrl_dis & (1 << FREQ))
                return;
        if (sd->sensor == SENSOR_OV7660) {
                u8 com8;
 
                com8 = 0xdf;            /* auto gain/wb/expo */
-               switch (sd->freq) {
+               switch (sd->ctrls[FREQ].val) {
                case 0: /* Banding filter disabled */
                        i2c_w1(gspca_dev, 0x13, com8 | 0x20);
                        break;
@@ -2213,7 +2325,7 @@ static void setfreq(struct gspca_dev *gspca_dev)
                        break;
                }
 
-               switch (sd->freq) {
+               switch (sd->ctrls[FREQ].val) {
                case 0: /* Banding filter disabled */
                        break;
                case 1: /* 50 hz (filter on and framerate adj) */
@@ -2235,18 +2347,12 @@ static void setfreq(struct gspca_dev *gspca_dev)
 static void setjpegqual(struct gspca_dev *gspca_dev)
 {
        struct sd *sd = (struct sd *) gspca_dev;
-       int i, sc;
 
-       if (sd->jpegqual < 50)
-               sc = 5000 / sd->jpegqual;
-       else
-               sc = 200 - sd->jpegqual * 2;
+       jpeg_set_qual(sd->jpeg_hdr, sd->quality);
 #if USB_BUF_SZ < 64
 #error "No room enough in usb_buf for quantization table"
 #endif
-       for (i = 0; i < 64; i++)
-               gspca_dev->usb_buf[i] =
-                       (jpeg_head[JPEG_QT0_OFFSET + i] * sc + 50) / 100;
+       memcpy(gspca_dev->usb_buf, &sd->jpeg_hdr[JPEG_QT0_OFFSET], 64);
        usb_control_msg(gspca_dev->dev,
                        usb_sndctrlpipe(gspca_dev->dev, 0),
                        0x08,
@@ -2254,9 +2360,7 @@ static void setjpegqual(struct gspca_dev *gspca_dev)
                        0x0100, 0,
                        gspca_dev->usb_buf, 64,
                        500);
-       for (i = 0; i < 64; i++)
-               gspca_dev->usb_buf[i] =
-                       (jpeg_head[JPEG_QT1_OFFSET + i] * sc + 50) / 100;
+       memcpy(gspca_dev->usb_buf, &sd->jpeg_hdr[JPEG_QT1_OFFSET], 64);
        usb_control_msg(gspca_dev->dev,
                        usb_sndctrlpipe(gspca_dev->dev, 0),
                        0x08,
@@ -2269,15 +2373,35 @@ static void setjpegqual(struct gspca_dev *gspca_dev)
        reg_w1(gspca_dev, 0x18, sd->reg18);
 }
 
+/* JPEG quality update */
+/* This function is executed from a work queue. */
+static void qual_upd(struct work_struct *work)
+{
+       struct sd *sd = container_of(work, struct sd, work);
+       struct gspca_dev *gspca_dev = &sd->gspca_dev;
+
+       mutex_lock(&gspca_dev->usb_lock);
+       PDEBUG(D_STREAM, "qual_upd %d%%", sd->quality);
+       setjpegqual(gspca_dev);
+       mutex_unlock(&gspca_dev->usb_lock);
+}
+
 /* -- start the camera -- */
 static int sd_start(struct gspca_dev *gspca_dev)
 {
        struct sd *sd = (struct sd *) gspca_dev;
        int i;
-       u8 reg1, reg2, reg17;
+       u8 reg01, reg17;
+       u8 reg0102[2];
        const u8 *sn9c1xx;
        const u8 (*init)[8];
+       const u8 *reg9a;
        int mode;
+       static const u8 reg9a_def[] =
+               {0x00, 0x40, 0x20, 0x00, 0x00, 0x00};
+       static const u8 reg9a_spec[] =
+               {0x00, 0x40, 0x38, 0x30, 0x00, 0x20};
+       static const u8 regd4[] = {0x60, 0x00, 0x00};
        static const u8 C0[] = { 0x2d, 0x2d, 0x3a, 0x05, 0x04, 0x3f };
        static const u8 CA[] = { 0x28, 0xd8, 0x14, 0xec };
        static const u8 CA_adcm1700[] =
@@ -2295,31 +2419,91 @@ static int sd_start(struct gspca_dev *gspca_dev)
        /* create the JPEG header */
        jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
                        0x21);          /* JPEG 422 */
-       jpeg_set_qual(sd->jpeg_hdr, sd->quality);
 
        /* initialize the bridge */
        sn9c1xx = sn_tb[sd->sensor];
-       bridge_init(gspca_dev, sn9c1xx);
 
-       /* initialize the sensor */
-       i2c_w_seq(gspca_dev, sensor_init[sd->sensor]);
+       /* sensor clock already enabled in sd_init */
+       /* reg_w1(gspca_dev, 0xf1, 0x00); */
+       reg01 = sn9c1xx[1];
+       if (sd->flags & F_PDN_INV)
+               reg01 ^= S_PDN_INV;             /* power down inverted */
+       reg_w1(gspca_dev, 0x01, reg01);
 
+       /* configure gpio */
+       reg0102[0] = reg01;
+       reg0102[1] = sn9c1xx[2];
+       if (gspca_dev->audio)
+               reg0102[1] |= 0x04;     /* keep the audio connection */
+       reg_w(gspca_dev, 0x01, reg0102, 2);
+       reg_w(gspca_dev, 0x08, &sn9c1xx[8], 2);
+       reg_w(gspca_dev, 0x17, &sn9c1xx[0x17], 5);
        switch (sd->sensor) {
-       case SENSOR_ADCM1700:
-               reg2 = 0x60;
+       case SENSOR_GC0307:
+       case SENSOR_OV7660:
+       case SENSOR_PO1030:
+       case SENSOR_PO2030N:
+       case SENSOR_SOI768:
+       case SENSOR_SP80708:
+               reg9a = reg9a_spec;
+               break;
+       default:
+               reg9a = reg9a_def;
+               break;
+       }
+       reg_w(gspca_dev, 0x9a, reg9a, 6);
+
+       reg_w(gspca_dev, 0xd4, regd4, sizeof regd4);
+
+       reg_w(gspca_dev, 0x03, &sn9c1xx[3], 0x0f);
+
+       reg17 = sn9c1xx[0x17];
+       switch (sd->sensor) {
+       case SENSOR_GC0307:
+               msleep(50);             /*fixme: is it useful? */
                break;
        case SENSOR_OM6802:
-               reg2 = 0x71;
+               msleep(10);
+               reg_w1(gspca_dev, 0x02, 0x73);
+               reg17 |= SEN_CLK_EN;
+               reg_w1(gspca_dev, 0x17, reg17);
+               reg_w1(gspca_dev, 0x01, 0x22);
+               msleep(100);
+               reg01 = SCL_SEL_OD | S_PDN_INV;
+               reg17 &= ~MCK_SIZE_MASK;
+               reg17 |= 0x04;          /* clock / 4 */
                break;
-       case SENSOR_SP80708:
-               reg2 = 0x62;
+       }
+       reg01 |= SYS_SEL_48M;
+       reg_w1(gspca_dev, 0x01, reg01);
+       reg17 |= SEN_CLK_EN;
+       reg_w1(gspca_dev, 0x17, reg17);
+       reg01 &= ~S_PWR_DN;             /* sensor power on */
+       reg_w1(gspca_dev, 0x01, reg01);
+       reg01 &= ~SCL_SEL_OD;           /* remove open-drain mode */
+       reg_w1(gspca_dev, 0x01, reg01);
+
+       switch (sd->sensor) {
+       case SENSOR_HV7131R:
+               hv7131r_probe(gspca_dev);       /*fixme: is it useful? */
                break;
-       default:
-               reg2 = 0x40;
+       case SENSOR_OM6802:
+               msleep(10);
+               reg_w1(gspca_dev, 0x01, reg01);
+               i2c_w8(gspca_dev, om6802_init0[0]);
+               i2c_w8(gspca_dev, om6802_init0[1]);
+               msleep(15);
+               reg_w1(gspca_dev, 0x02, 0x71);
+               msleep(150);
+               break;
+       case SENSOR_SP80708:
+               msleep(100);
+               reg_w1(gspca_dev, 0x02, 0x62);
                break;
        }
-       reg_w1(gspca_dev, 0x02, reg2);
-       reg_w1(gspca_dev, 0x02, reg2);
+
+       /* initialize the sensor */
+       i2c_w_seq(gspca_dev, sensor_init[sd->sensor]);
 
        reg_w1(gspca_dev, 0x15, sn9c1xx[0x15]);
        reg_w1(gspca_dev, 0x16, sn9c1xx[0x16]);
@@ -2344,29 +2528,11 @@ static int sd_start(struct gspca_dev *gspca_dev)
        }
        reg_w1(gspca_dev, 0x18, sn9c1xx[0x18]);
        switch (sd->sensor) {
-       case SENSOR_GC0307:
-               reg17 = 0xa2;
-               break;
-       case SENSOR_MT9V111:
-               reg17 = 0xe0;
-               break;
-       case SENSOR_ADCM1700:
-       case SENSOR_OV7630:
-               reg17 = 0xe2;
-               break;
-       case SENSOR_OV7648:
-               reg17 = 0x20;
-               break;
-       case SENSOR_OV7660:
-       case SENSOR_SOI768:
-               reg17 = 0xa0;
-               break;
-       case SENSOR_PO1030:
-       case SENSOR_PO2030N:
-               reg17 = 0xa0;
+       case SENSOR_OM6802:
+/*     case SENSOR_OV7648:             * fixme: sometimes */
                break;
        default:
-               reg17 = 0x60;
+               reg17 |= DEF_EN;
                break;
        }
        reg_w1(gspca_dev, 0x17, reg17);
@@ -2389,6 +2555,7 @@ static int sd_start(struct gspca_dev *gspca_dev)
                break;
        case SENSOR_GC0307:
        case SENSOR_MT9V111:
+       case SENSOR_MI0360B:
                reg_w1(gspca_dev, 0x9a, 0x07);
                break;
        case SENSOR_OV7630:
@@ -2403,7 +2570,7 @@ static int sd_start(struct gspca_dev *gspca_dev)
                reg_w1(gspca_dev, 0x9a, 0x08);
                break;
        }
-       setsharpness(sd);
+       setsharpness(gspca_dev);
 
        reg_w(gspca_dev, 0x84, reg84, sizeof reg84);
        reg_w1(gspca_dev, 0x05, 0x20);          /* red */
@@ -2412,91 +2579,69 @@ static int sd_start(struct gspca_dev *gspca_dev)
 
        init = NULL;
        mode = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv;
-       if (mode)
-               reg1 = 0x46;    /* 320x240: clk 48Mhz, video trf enable */
-       else
-               reg1 = 0x06;    /* 640x480: clk 24Mhz, video trf enable */
-       reg17 = 0x61;           /* 0x:20: enable sensor clock */
+       reg01 |= SYS_SEL_48M | V_TX_EN;
+       reg17 &= ~MCK_SIZE_MASK;
+       reg17 |= 0x02;                  /* clock / 2 */
        switch (sd->sensor) {
        case SENSOR_ADCM1700:
                init = adcm1700_sensor_param1;
-               reg1 = 0x46;
-               reg17 = 0xe2;
                break;
        case SENSOR_GC0307:
                init = gc0307_sensor_param1;
-               reg17 = 0xa2;
-               reg1 = 0x44;
+               break;
+       case SENSOR_HV7131R:
+       case SENSOR_MI0360:
+               if (!mode)
+                       reg01 &= ~SYS_SEL_48M;  /* 640x480: clk 24Mhz */
+               reg17 &= ~MCK_SIZE_MASK;
+               reg17 |= 0x01;                  /* clock / 1 */
+               break;
+       case SENSOR_MI0360B:
+               init = mi0360b_sensor_param1;
                break;
        case SENSOR_MO4000:
-               if (mode) {
-/*                     reg1 = 0x46;     * 320 clk 48Mhz 60fp/s */
-                       reg1 = 0x06;    /* clk 24Mz */
-               } else {
-                       reg17 = 0x22;   /* 640 MCKSIZE */
-/*                     reg1 = 0x06;     * 640 clk 24Mz (done) */
+               if (mode) {                     /* if 320x240 */
+                       reg01 &= ~SYS_SEL_48M;  /* clk 24Mz */
+                       reg17 &= ~MCK_SIZE_MASK;
+                       reg17 |= 0x01;          /* clock / 1 */
                }
                break;
        case SENSOR_MT9V111:
                init = mt9v111_sensor_param1;
-               if (mode) {
-                       reg1 = 0x04;    /* 320 clk 48Mhz */
-               } else {
-/*                     reg1 = 0x06;     * 640 clk 24Mz (done) */
-                       reg17 = 0xc2;
-               }
                break;
        case SENSOR_OM6802:
                init = om6802_sensor_param1;
-               reg17 = 0x64;           /* 640 MCKSIZE */
+               if (!mode) {                    /* if 640x480 */
+                       reg17 &= ~MCK_SIZE_MASK;
+                       reg17 |= 0x04;          /* clock / 4 */
+               } else {
+                       reg01 &= ~SYS_SEL_48M;  /* clk 24Mz */
+                       reg17 &= ~MCK_SIZE_MASK;
+                       reg17 |= 0x02;          /* clock / 2 */
+               }
                break;
        case SENSOR_OV7630:
                init = ov7630_sensor_param1;
-               reg17 = 0xe2;
-               reg1 = 0x44;
                break;
        case SENSOR_OV7648:
                init = ov7648_sensor_param1;
-               reg17 = 0x21;
-/*             reg1 = 0x42;             * 42 - 46? */
+               reg17 &= ~MCK_SIZE_MASK;
+               reg17 |= 0x01;                  /* clock / 1 */
                break;
        case SENSOR_OV7660:
                init = ov7660_sensor_param1;
-               if (sd->bridge == BRIDGE_SN9C120) {
-                       if (mode) {             /* 320x240 - 160x120 */
-                               reg17 = 0xa2;
-                               reg1 = 0x44;    /* 48 Mhz, video trf eneble */
-                       }
-               } else {
-                       reg17 = 0x22;
-                       reg1 = 0x06;    /* 24 Mhz, video trf eneble
-                                        * inverse power down */
-               }
                break;
        case SENSOR_PO1030:
                init = po1030_sensor_param1;
-               reg17 = 0xa2;
-               reg1 = 0x44;
                break;
        case SENSOR_PO2030N:
                init = po2030n_sensor_param1;
-               reg1 = 0x46;
-               reg17 = 0xa2;
                break;
        case SENSOR_SOI768:
                init = soi768_sensor_param1;
-               reg1 = 0x44;
-               reg17 = 0xa2;
                break;
-       default:
-/*     case SENSOR_SP80708: */
+       case SENSOR_SP80708:
                init = sp80708_sensor_param1;
-               if (mode) {
-/*??                   reg1 = 0x04;     * 320 clk 48Mhz */
-               } else {
-                       reg1 = 0x46;     /* 640 clk 48Mz */
-                       reg17 = 0xa2;
-               }
                break;
        }
 
@@ -2540,22 +2685,32 @@ static int sd_start(struct gspca_dev *gspca_dev)
                break;
        }
 
-
        /* here change size mode 0 -> VGA; 1 -> CIF */
        sd->reg18 = sn9c1xx[0x18] | (mode << 4) | 0x40;
        reg_w1(gspca_dev, 0x18, sd->reg18);
        setjpegqual(gspca_dev);
 
        reg_w1(gspca_dev, 0x17, reg17);
-       reg_w1(gspca_dev, 0x01, reg1);
+       reg_w1(gspca_dev, 0x01, reg01);
+       sd->reg01 = reg01;
+       sd->reg17 = reg17;
 
-       setvflip(sd);
+       sethvflip(gspca_dev);
        setbrightness(gspca_dev);
        setcontrast(gspca_dev);
        setcolors(gspca_dev);
        setautogain(gspca_dev);
+       if (!(gspca_dev->ctrl_inac & ((1 << EXPOSURE) | (1 << GAIN)))) {
+               setexposure(gspca_dev);
+               setgain(gspca_dev);
+       }
        setfreq(gspca_dev);
-       return 0;
+
+       sd->pktsz = sd->npkt = 0;
+       sd->nchg = sd->short_mark = 0;
+       sd->work_thread = create_singlethread_workqueue(MODULE_NAME);
+
+       return gspca_dev->usb_err;
 }
 
 static void sd_stopN(struct gspca_dev *gspca_dev)
@@ -2569,44 +2724,88 @@ static void sd_stopN(struct gspca_dev *gspca_dev)
                { 0xa1, 0x21, 0x76, 0x20, 0x00, 0x00, 0x00, 0x10 };
        static const u8 stopsoi768[] =
                { 0xa1, 0x21, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10 };
-       u8 data;
-       const u8 *sn9c1xx;
+       u8 reg01;
+       u8 reg17;
 
-       data = 0x0b;
+       reg01 = sd->reg01;
+       reg17 = sd->reg17 & ~SEN_CLK_EN;
        switch (sd->sensor) {
+       case SENSOR_ADCM1700:
        case SENSOR_GC0307:
-               data = 0x29;
+       case SENSOR_PO2030N:
+       case SENSOR_SP80708:
+               reg01 |= LED;
+               reg_w1(gspca_dev, 0x01, reg01);
+               reg01 &= ~(LED | V_TX_EN);
+               reg_w1(gspca_dev, 0x01, reg01);
+/*             reg_w1(gspca_dev, 0x02, 0x??);   * LED off ? */
                break;
        case SENSOR_HV7131R:
+               reg01 &= ~V_TX_EN;
+               reg_w1(gspca_dev, 0x01, reg01);
                i2c_w8(gspca_dev, stophv7131);
-               data = 0x2b;
                break;
        case SENSOR_MI0360:
+       case SENSOR_MI0360B:
+               reg01 &= ~V_TX_EN;
+               reg_w1(gspca_dev, 0x01, reg01);
+/*             reg_w1(gspca_dev, 0x02, 0x40);    * LED off ? */
                i2c_w8(gspca_dev, stopmi0360);
-               data = 0x29;
                break;
-       case SENSOR_OV7648:
-               i2c_w8(gspca_dev, stopov7648);
-               /* fall thru */
        case SENSOR_MT9V111:
-       case SENSOR_OV7630:
+       case SENSOR_OM6802:
        case SENSOR_PO1030:
-               data = 0x29;
+               reg01 &= ~V_TX_EN;
+               reg_w1(gspca_dev, 0x01, reg01);
+               break;
+       case SENSOR_OV7630:
+       case SENSOR_OV7648:
+               reg01 &= ~V_TX_EN;
+               reg_w1(gspca_dev, 0x01, reg01);
+               i2c_w8(gspca_dev, stopov7648);
+               break;
+       case SENSOR_OV7660:
+               reg01 &= ~V_TX_EN;
+               reg_w1(gspca_dev, 0x01, reg01);
                break;
        case SENSOR_SOI768:
                i2c_w8(gspca_dev, stopsoi768);
-               data = 0x29;
                break;
        }
-       sn9c1xx = sn_tb[sd->sensor];
-       reg_w1(gspca_dev, 0x01, sn9c1xx[1]);
-       reg_w1(gspca_dev, 0x17, sn9c1xx[0x17]);
-       reg_w1(gspca_dev, 0x01, sn9c1xx[1]);
-       reg_w1(gspca_dev, 0x01, data);
+
+       reg01 |= SCL_SEL_OD;
+       reg_w1(gspca_dev, 0x01, reg01);
+       reg01 |= S_PWR_DN;              /* sensor power down */
+       reg_w1(gspca_dev, 0x01, reg01);
+       reg_w1(gspca_dev, 0x17, reg17);
+       reg01 &= ~SYS_SEL_48M;          /* clock 24MHz */
+       reg_w1(gspca_dev, 0x01, reg01);
+       reg01 |= LED;
+       reg_w1(gspca_dev, 0x01, reg01);
        /* Don't disable sensor clock as that disables the button on the cam */
        /* reg_w1(gspca_dev, 0xf1, 0x01); */
 }
 
+/* called on streamoff with alt==0 and on disconnect */
+/* the usb_lock is held at entry - restore on exit */
+static void sd_stop0(struct gspca_dev *gspca_dev)
+{
+       struct sd *sd = (struct sd *) gspca_dev;
+
+       if (sd->work_thread != NULL) {
+               mutex_unlock(&gspca_dev->usb_lock);
+               destroy_workqueue(sd->work_thread);
+               mutex_lock(&gspca_dev->usb_lock);
+               sd->work_thread = NULL;
+       }
+}
+
+/* !! coarse_grained_expo_autogain is not used !! */
+#define exp_too_low_cnt bridge
+#define exp_too_high_cnt sensor
+
+#include "autogain_functions.h"
+
 static void do_autogain(struct gspca_dev *gspca_dev)
 {
        struct sd *sd = (struct sd *) gspca_dev;
@@ -2624,6 +2823,13 @@ static void do_autogain(struct gspca_dev *gspca_dev)
 
        delta = atomic_read(&sd->avg_lum);
        PDEBUG(D_FRAM, "mean lum %d", delta);
+
+       if (sd->sensor == SENSOR_PO2030N) {
+               auto_gain_n_exposure(gspca_dev, delta, luma_mean, luma_delta,
+                                       15, 1024);
+               return;
+       }
+
        if (delta < luma_mean - luma_delta ||
            delta > luma_mean + luma_delta) {
                switch (sd->sensor) {
@@ -2632,7 +2838,7 @@ static void do_autogain(struct gspca_dev *gspca_dev)
                        expotimes += (luma_mean - delta) >> 6;
                        if (expotimes < 0)
                                expotimes = 0;
-                       sd->exposure = setexposure(gspca_dev,
+                       sd->exposure = expo_adjust(gspca_dev,
                                                   (unsigned int) expotimes);
                        break;
                case SENSOR_HV7131R:
@@ -2640,27 +2846,28 @@ static void do_autogain(struct gspca_dev *gspca_dev)
                        expotimes += (luma_mean - delta) >> 4;
                        if (expotimes < 0)
                                expotimes = 0;
-                       sd->exposure = setexposure(gspca_dev,
+                       sd->exposure = expo_adjust(gspca_dev,
                                        (unsigned int) (expotimes << 8));
                        break;
                case SENSOR_OM6802:
+               case SENSOR_MT9V111:
                        expotimes = sd->exposure;
                        expotimes += (luma_mean - delta) >> 2;
                        if (expotimes < 0)
                                expotimes = 0;
-                       sd->exposure = setexposure(gspca_dev,
+                       sd->exposure = expo_adjust(gspca_dev,
                                                   (unsigned int) expotimes);
                        setredblue(gspca_dev);
                        break;
                default:
 /*             case SENSOR_MO4000: */
 /*             case SENSOR_MI0360: */
-/*             case SENSOR_MT9V111: */
+/*             case SENSOR_MI0360B: */
                        expotimes = sd->exposure;
                        expotimes += (luma_mean - delta) >> 6;
                        if (expotimes < 0)
                                expotimes = 0;
-                       sd->exposure = setexposure(gspca_dev,
+                       sd->exposure = expo_adjust(gspca_dev,
                                                   (unsigned int) expotimes);
                        setredblue(gspca_dev);
                        break;
@@ -2668,273 +2875,185 @@ static void do_autogain(struct gspca_dev *gspca_dev)
        }
 }
 
-/* scan the URB packets */
-/* This function is run at interrupt level. */
-static void sd_pkt_scan(struct gspca_dev *gspca_dev,
-                       u8 *data,                       /* isoc packet */
-                       int len)                        /* iso packet length */
+/* set the average luminosity from an isoc marker */
+static void set_lum(struct sd *sd,
+                   u8 *data)
 {
-       struct sd *sd = (struct sd *) gspca_dev;
-       int sof, avg_lum;
+       int avg_lum;
 
-       sof = len - 64;
-       if (sof >= 0 && data[sof] == 0xff && data[sof + 1] == 0xd9) {
+       /*      w0 w1 w2
+        *      w3 w4 w5
+        *      w6 w7 w8
+        */
+       avg_lum = (data[27] << 8) + data[28]            /* w3 */
 
-               /* end of frame */
-               gspca_frame_add(gspca_dev, LAST_PACKET,
-                               data, sof + 2);
-               if (sd->ag_cnt < 0)
-                       return;
-/* w1 w2 w3 */
-/* w4 w5 w6 */
-/* w7 w8 */
-/* w4 */
-               avg_lum = ((data[sof + 29] << 8) | data[sof + 30]) >> 6;
-/* w6 */
-               avg_lum += ((data[sof + 33] << 8) | data[sof + 34]) >> 6;
-/* w2 */
-               avg_lum += ((data[sof + 25] << 8) | data[sof + 26]) >> 6;
-/* w8 */
-               avg_lum += ((data[sof + 37] << 8) | data[sof + 38]) >> 6;
-/* w5 */
-               avg_lum += ((data[sof + 31] << 8) | data[sof + 32]) >> 4;
-               avg_lum >>= 4;
-               atomic_set(&sd->avg_lum, avg_lum);
-               return;
-       }
-       if (gspca_dev->last_packet_type == LAST_PACKET) {
+               + (data[31] << 8) + data[32]            /* w5 */
 
-               /* put the JPEG 422 header */
-               gspca_frame_add(gspca_dev, FIRST_PACKET,
-                       sd->jpeg_hdr, JPEG_HDR_SZ);
-       }
-       gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
-}
+               + (data[23] << 8) + data[24]            /* w1 */
 
-static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
+               + (data[35] << 8) + data[36]            /* w7 */
 
-       sd->brightness = val;
-       if (gspca_dev->streaming)
-               setbrightness(gspca_dev);
-       return 0;
-}
-
-static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
-
-       *val = sd->brightness;
-       return 0;
-}
-
-static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
-
-       sd->contrast = val;
-       if (gspca_dev->streaming)
-               setcontrast(gspca_dev);
-       return 0;
+               + (data[29] << 10) + (data[30] << 2);   /* w4 * 4 */
+       avg_lum >>= 10;
+       atomic_set(&sd->avg_lum, avg_lum);
 }
 
-static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
-
-       *val = sd->contrast;
-       return 0;
-}
-
-static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
-
-       sd->colors = val;
-       if (gspca_dev->streaming)
-               setcolors(gspca_dev);
-       return 0;
-}
-
-static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
-
-       *val = sd->colors;
-       return 0;
-}
-
-static int sd_setblue_balance(struct gspca_dev *gspca_dev, __s32 val)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
-
-       sd->blue = val;
-       if (gspca_dev->streaming)
-               setredblue(gspca_dev);
-       return 0;
-}
-
-static int sd_getblue_balance(struct gspca_dev *gspca_dev, __s32 *val)
+/* scan the URB packets */
+/* This function is run at interrupt level. */
+static void sd_pkt_scan(struct gspca_dev *gspca_dev,
+                       u8 *data,                       /* isoc packet */
+                       int len)                        /* iso packet length */
 {
        struct sd *sd = (struct sd *) gspca_dev;
+       int i, new_qual;
+
+       /*
+        * A frame ends on the marker
+        *              ff ff 00 c4 c4 96 ..
+        * which is 62 bytes long and is followed by various information
+        * including statuses and luminosity.
+        *
+        * A marker may be splitted on two packets.
+        *
+        * The 6th byte of a marker contains the bits:
+        *      0x08: USB full
+        *      0xc0: frame sequence
+        * When the bit 'USB full' is set, the frame must be discarded;
+        * this is also the case when the 2 bytes before the marker are
+        * not the JPEG end of frame ('ff d9').
+        */
+
+       /* count the packets and their size */
+       sd->npkt++;
+       sd->pktsz += len;
+
+/*fixme: assumption about the following code:
+ *     - there can be only one marker in a packet
+ */
 
-       *val = sd->blue;
-       return 0;
-}
-
-static int sd_setred_balance(struct gspca_dev *gspca_dev, __s32 val)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
+       /* skip the remaining bytes of a short marker */
+       i = sd->short_mark;
+       if (i != 0) {
+               sd->short_mark = 0;
+               if (i < 0       /* if 'ff' at end of previous packet */
+                && data[0] == 0xff
+                && data[1] == 0x00)
+                       goto marker_found;
+               if (data[0] == 0xff && data[1] == 0xff) {
+                       i = 0;
+                       goto marker_found;
+               }
+               len -= i;
+               if (len <= 0)
+                       return;
+               data += i;
+       }
 
-       sd->red = val;
-       if (gspca_dev->streaming)
-               setredblue(gspca_dev);
-       return 0;
-}
+       /* search backwards if there is a marker in the packet */
+       for (i = len - 1; --i >= 0; ) {
+               if (data[i] != 0xff) {
+                       i--;
+                       continue;
+               }
+               if (data[i + 1] == 0xff) {
 
-static int sd_getred_balance(struct gspca_dev *gspca_dev, __s32 *val)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
+                       /* (there may be 'ff ff' inside a marker) */
+                       if (i + 2 >= len || data[i + 2] == 0x00)
+                               goto marker_found;
+               }
+       }
 
-       *val = sd->red;
-       return 0;
-}
+       /* no marker found */
+       /* add the JPEG header if first fragment */
+       if (data[len - 1] == 0xff)
+               sd->short_mark = -1;
+       if (gspca_dev->last_packet_type == LAST_PACKET)
+               gspca_frame_add(gspca_dev, FIRST_PACKET,
+                               sd->jpeg_hdr, JPEG_HDR_SZ);
+       gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
+       return;
+
+       /* marker found */
+       /* if some error, discard the frame and decrease the quality */
+marker_found:
+       new_qual = 0;
+       if (i > 2) {
+               if (data[i - 2] != 0xff || data[i - 1] != 0xd9) {
+                       gspca_dev->last_packet_type = DISCARD_PACKET;
+                       new_qual = -3;
+               }
+       } else if (i + 6 < len) {
+               if (data[i + 6] & 0x08) {
+                       gspca_dev->last_packet_type = DISCARD_PACKET;
+                       new_qual = -5;
+               }
+       }
 
-static int sd_setgamma(struct gspca_dev *gspca_dev, __s32 val)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
+       gspca_frame_add(gspca_dev, LAST_PACKET, data, i);
 
-       sd->gamma = val;
-       if (gspca_dev->streaming)
-               setgamma(gspca_dev);
-       return 0;
-}
+       /* compute the filling rate and a new JPEG quality */
+       if (new_qual == 0) {
+               int r;
 
-static int sd_getgamma(struct gspca_dev *gspca_dev, __s32 *val)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
+               r = (sd->pktsz * 100) /
+                       (sd->npkt *
+                               gspca_dev->urb[0]->iso_frame_desc[0].length);
+               if (r >= 85)
+                       new_qual = -3;
+               else if (r < 75)
+                       new_qual = 2;
+       }
+       if (new_qual != 0) {
+               sd->nchg += new_qual;
+               if (sd->nchg < -6 || sd->nchg >= 12) {
+                       sd->nchg = 0;
+                       new_qual += sd->quality;
+                       if (new_qual < QUALITY_MIN)
+                               new_qual = QUALITY_MIN;
+                       else if (new_qual > QUALITY_MAX)
+                               new_qual = QUALITY_MAX;
+                       if (new_qual != sd->quality) {
+                               sd->quality = new_qual;
+                               queue_work(sd->work_thread, &sd->work);
+                       }
+               }
+       } else {
+               sd->nchg = 0;
+       }
+       sd->pktsz = sd->npkt = 0;
 
-       *val = sd->gamma;
-       return 0;
+       /* if the marker is smaller than 62 bytes,
+        * memorize the number of bytes to skip in the next packet */
+       if (i + 62 > len) {                     /* no more usable data */
+               sd->short_mark = i + 62 - len;
+               return;
+       }
+       if (sd->ag_cnt >= 0)
+               set_lum(sd, data + i);
+
+       /* if more data, start a new frame */
+       i += 62;
+       if (i < len) {
+               data += i;
+               len -= i;
+               gspca_frame_add(gspca_dev, FIRST_PACKET,
+                               sd->jpeg_hdr, JPEG_HDR_SZ);
+               gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
+       }
 }
 
 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
 {
        struct sd *sd = (struct sd *) gspca_dev;
 
-       sd->autogain = val;
-       if (gspca_dev->streaming)
-               setautogain(gspca_dev);
-       return 0;
-}
-
-static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
-
-       *val = sd->autogain;
-       return 0;
-}
-
-static int sd_setsharpness(struct gspca_dev *gspca_dev, __s32 val)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
-
-       sd->sharpness = val;
-       if (gspca_dev->streaming)
-               setsharpness(sd);
-       return 0;
-}
-
-static int sd_getsharpness(struct gspca_dev *gspca_dev, __s32 *val)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
-
-       *val = sd->sharpness;
-       return 0;
-}
-
-static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
-
-       sd->vflip = val;
-       if (gspca_dev->streaming)
-               setvflip(sd);
-       return 0;
-}
-
-static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
-
-       *val = sd->vflip;
-       return 0;
-}
-
-static int sd_setinfrared(struct gspca_dev *gspca_dev, __s32 val)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
-
-       sd->infrared = val;
-       if (gspca_dev->streaming)
-               setinfrared(sd);
-       return 0;
-}
-
-static int sd_getinfrared(struct gspca_dev *gspca_dev, __s32 *val)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
-
-       *val = sd->infrared;
-       return 0;
-}
-
-static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
-
-       sd->freq = val;
-       if (gspca_dev->streaming)
-               setfreq(gspca_dev);
-       return 0;
-}
-
-static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
-
-       *val = sd->freq;
-       return 0;
-}
-
-static int sd_set_jcomp(struct gspca_dev *gspca_dev,
-                       struct v4l2_jpegcompression *jcomp)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
-
-       if (jcomp->quality < QUALITY_MIN)
-               sd->quality = QUALITY_MIN;
-       else if (jcomp->quality > QUALITY_MAX)
-               sd->quality = QUALITY_MAX;
+       sd->ctrls[AUTOGAIN].val = val;
+       if (val)
+               gspca_dev->ctrl_inac |= (1 << EXPOSURE) | (1 << GAIN);
        else
-               sd->quality = jcomp->quality;
+               gspca_dev->ctrl_inac &= ~(1 << EXPOSURE) & ~(1 << GAIN);
        if (gspca_dev->streaming)
-               jpeg_set_qual(sd->jpeg_hdr, sd->quality);
-       return 0;
-}
-
-static int sd_get_jcomp(struct gspca_dev *gspca_dev,
-                       struct v4l2_jpegcompression *jcomp)
-{
-       struct sd *sd = (struct sd *) gspca_dev;
-
-       memset(jcomp, 0, sizeof *jcomp);
-       jcomp->quality = sd->quality;
-       jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
-                       | V4L2_JPEG_MARKER_DQT;
-       return 0;
+               setautogain(gspca_dev);
+       return gspca_dev->usb_err;
 }
 
 static int sd_querymenu(struct gspca_dev *gspca_dev,
@@ -2958,7 +3077,7 @@ static int sd_querymenu(struct gspca_dev *gspca_dev,
        return -EINVAL;
 }
 
-#ifdef CONFIG_INPUT
+#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
 static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
                        u8 *data,               /* interrupt packet data */
                        int len)                /* interrupt packet length */
@@ -2981,17 +3100,16 @@ static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
 static const struct sd_desc sd_desc = {
        .name = MODULE_NAME,
        .ctrls = sd_ctrls,
-       .nctrls = ARRAY_SIZE(sd_ctrls),
+       .nctrls = NCTRLS,
        .config = sd_config,
        .init = sd_init,
        .start = sd_start,
        .stopN = sd_stopN,
+       .stop0 = sd_stop0,
        .pkt_scan = sd_pkt_scan,
        .dq_callback = do_autogain,
-       .get_jcomp = sd_get_jcomp,
-       .set_jcomp = sd_set_jcomp,
        .querymenu = sd_querymenu,
-#ifdef CONFIG_INPUT
+#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
        .int_pkt_scan = sd_int_pkt_scan,
 #endif
 };
@@ -2999,14 +3117,16 @@ static const struct sd_desc sd_desc = {
 /* -- module initialisation -- */
 #define BS(bridge, sensor) \
        .driver_info = (BRIDGE_ ## bridge << 16) \
-                       | SENSOR_ ## sensor
-static const __devinitdata struct usb_device_id device_table[] = {
-#if !defined CONFIG_USB_SN9C102 && !defined CONFIG_USB_SN9C102_MODULE
+                       | (SENSOR_ ## sensor << 8)
+#define BSF(bridge, sensor, flags) \
+       .driver_info = (BRIDGE_ ## bridge << 16) \
+                       | (SENSOR_ ## sensor << 8) \
+                       | (flags)
+static const struct usb_device_id device_table[] = {
        {USB_DEVICE(0x0458, 0x7025), BS(SN9C120, MI0360)},
        {USB_DEVICE(0x0458, 0x702e), BS(SN9C120, OV7660)},
-#endif
-       {USB_DEVICE(0x045e, 0x00f5), BS(SN9C105, OV7660)},
-       {USB_DEVICE(0x045e, 0x00f7), BS(SN9C105, OV7660)},
+       {USB_DEVICE(0x045e, 0x00f5), BSF(SN9C105, OV7660, F_PDN_INV)},
+       {USB_DEVICE(0x045e, 0x00f7), BSF(SN9C105, OV7660, F_PDN_INV)},
        {USB_DEVICE(0x0471, 0x0327), BS(SN9C105, MI0360)},
        {USB_DEVICE(0x0471, 0x0328), BS(SN9C105, MI0360)},
        {USB_DEVICE(0x0471, 0x0330), BS(SN9C105, MI0360)},
@@ -3018,22 +3138,21 @@ static const __devinitdata struct usb_device_id device_table[] = {
 /*     {USB_DEVICE(0x0c45, 0x607b), BS(SN9C102P, OV7660)}, */
        {USB_DEVICE(0x0c45, 0x607c), BS(SN9C102P, HV7131R)},
 /*     {USB_DEVICE(0x0c45, 0x607e), BS(SN9C102P, OV7630)}, */
-       {USB_DEVICE(0x0c45, 0x60c0), BS(SN9C105, MI0360)},
+       {USB_DEVICE(0x0c45, 0x60c0), BSF(SN9C105, MI0360, F_ILLUM)},
+                                               /* or MT9V111 */
 /*     {USB_DEVICE(0x0c45, 0x60c2), BS(SN9C105, P1030xC)}, */
 /*     {USB_DEVICE(0x0c45, 0x60c8), BS(SN9C105, OM6802)}, */
 /*     {USB_DEVICE(0x0c45, 0x60cc), BS(SN9C105, HV7131GP)}, */
-/*     {USB_DEVICE(0x0c45, 0x60ce), BS(SN9C105, SP80708)}, */
+       {USB_DEVICE(0x0c45, 0x60ce), BS(SN9C105, SP80708)},
        {USB_DEVICE(0x0c45, 0x60ec), BS(SN9C105, MO4000)},
 /*     {USB_DEVICE(0x0c45, 0x60ef), BS(SN9C105, ICM105C)}, */
 /*     {USB_DEVICE(0x0c45, 0x60fa), BS(SN9C105, OV7648)}, */
 /*     {USB_DEVICE(0x0c45, 0x60f2), BS(SN9C105, OV7660)}, */
        {USB_DEVICE(0x0c45, 0x60fb), BS(SN9C105, OV7660)},
-#if !defined CONFIG_USB_SN9C102 && !defined CONFIG_USB_SN9C102_MODULE
        {USB_DEVICE(0x0c45, 0x60fc), BS(SN9C105, HV7131R)},
        {USB_DEVICE(0x0c45, 0x60fe), BS(SN9C105, OV7630)},
-#endif
        {USB_DEVICE(0x0c45, 0x6100), BS(SN9C120, MI0360)},      /*sn9c128*/
-/*     {USB_DEVICE(0x0c45, 0x6102), BS(SN9C120, PO2030N)}, * / GC0305*/
+       {USB_DEVICE(0x0c45, 0x6102), BS(SN9C120, PO2030N)},     /* /GC0305*/
 /*     {USB_DEVICE(0x0c45, 0x6108), BS(SN9C120, OM6802)}, */
        {USB_DEVICE(0x0c45, 0x610a), BS(SN9C120, OV7648)},      /*sn9c128*/
        {USB_DEVICE(0x0c45, 0x610b), BS(SN9C120, OV7660)},      /*sn9c128*/
@@ -3045,25 +3164,24 @@ static const __devinitdata struct usb_device_id device_table[] = {
        {USB_DEVICE(0x0c45, 0x6128), BS(SN9C120, OM6802)},      /*sn9c325?*/
 /*bw600.inf:*/
        {USB_DEVICE(0x0c45, 0x612a), BS(SN9C120, OV7648)},      /*sn9c325?*/
+       {USB_DEVICE(0x0c45, 0x612b), BS(SN9C110, ADCM1700)},
        {USB_DEVICE(0x0c45, 0x612c), BS(SN9C110, MO4000)},
        {USB_DEVICE(0x0c45, 0x612e), BS(SN9C110, OV7630)},
 /*     {USB_DEVICE(0x0c45, 0x612f), BS(SN9C110, ICM105C)}, */
-#if !defined CONFIG_USB_SN9C102 && !defined CONFIG_USB_SN9C102_MODULE
        {USB_DEVICE(0x0c45, 0x6130), BS(SN9C120, MI0360)},
-#endif
+                                               /* or MT9V111 / MI0360B */
 /*     {USB_DEVICE(0x0c45, 0x6132), BS(SN9C120, OV7670)}, */
        {USB_DEVICE(0x0c45, 0x6138), BS(SN9C120, MO4000)},
        {USB_DEVICE(0x0c45, 0x613a), BS(SN9C120, OV7648)},
-#if !defined CONFIG_USB_SN9C102 && !defined CONFIG_USB_SN9C102_MODULE
        {USB_DEVICE(0x0c45, 0x613b), BS(SN9C120, OV7660)},
-#endif
        {USB_DEVICE(0x0c45, 0x613c), BS(SN9C120, HV7131R)},
        {USB_DEVICE(0x0c45, 0x613e), BS(SN9C120, OV7630)},
        {USB_DEVICE(0x0c45, 0x6142), BS(SN9C120, PO2030N)},     /*sn9c120b*/
                                                /* or GC0305 / GC0307 */
        {USB_DEVICE(0x0c45, 0x6143), BS(SN9C120, SP80708)},     /*sn9c120b*/
        {USB_DEVICE(0x0c45, 0x6148), BS(SN9C120, OM6802)},      /*sn9c120b*/
-       {USB_DEVICE(0x0c45, 0x614a), BS(SN9C120, ADCM1700)},    /*sn9c120b*/
+       {USB_DEVICE(0x0c45, 0x614a), BSF(SN9C120, ADCM1700, F_ILLUM)},
+/*     {USB_DEVICE(0x0c45, 0x614c), BS(SN9C120, GC0306)}, */   /*sn9c120b*/
        {}
 };
 MODULE_DEVICE_TABLE(usb, device_table);
@@ -3087,21 +3205,4 @@ static struct usb_driver sd_driver = {
 #endif
 };
 
-/* -- module insert / remove -- */
-static int __init sd_mod_init(void)
-{
-       int ret;
-       ret = usb_register(&sd_driver);
-       if (ret < 0)
-               return ret;
-       info("registered");
-       return 0;
-}
-static void __exit sd_mod_exit(void)
-{
-       usb_deregister(&sd_driver);
-       info("deregistered");
-}
-
-module_init(sd_mod_init);
-module_exit(sd_mod_exit);
+module_usb_driver(sd_driver);