Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Mars MR97310A library
0004  *
0005  * The original mr97310a driver, which supported the Aiptek Pencam VGA+, is
0006  * Copyright (C) 2009 Kyle Guinn <elyk03@gmail.com>
0007  *
0008  * Support for the MR97310A cameras in addition to the Aiptek Pencam VGA+
0009  * and for the routines for detecting and classifying these various cameras,
0010  * is Copyright (C) 2009 Theodore Kilgore <kilgota@auburn.edu>
0011  *
0012  * Support for the control settings for the CIF cameras is
0013  * Copyright (C) 2009 Hans de Goede <hdegoede@redhat.com> and
0014  * Thomas Kaiser <thomas@kaiser-linux.li>
0015  *
0016  * Support for the control settings for the VGA cameras is
0017  * Copyright (C) 2009 Theodore Kilgore <kilgota@auburn.edu>
0018  *
0019  * Several previously unsupported cameras are owned and have been tested by
0020  * Hans de Goede <hdegoede@redhat.com> and
0021  * Thomas Kaiser <thomas@kaiser-linux.li> and
0022  * Theodore Kilgore <kilgota@auburn.edu> and
0023  * Edmond Rodriguez <erodrig_97@yahoo.com> and
0024  * Aurelien Jacobs <aurel@gnuage.org>
0025  *
0026  * The MR97311A support in gspca/mars.c has been helpful in understanding some
0027  * of the registers in these cameras.
0028  */
0029 
0030 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0031 
0032 #define MODULE_NAME "mr97310a"
0033 
0034 #include "gspca.h"
0035 
0036 #define CAM_TYPE_CIF            0
0037 #define CAM_TYPE_VGA            1
0038 
0039 #define MR97310A_BRIGHTNESS_DEFAULT 0
0040 
0041 #define MR97310A_EXPOSURE_MIN       0
0042 #define MR97310A_EXPOSURE_MAX       4095
0043 #define MR97310A_EXPOSURE_DEFAULT   1000
0044 
0045 #define MR97310A_GAIN_MIN       0
0046 #define MR97310A_GAIN_MAX       31
0047 #define MR97310A_GAIN_DEFAULT       25
0048 
0049 #define MR97310A_CONTRAST_MIN       0
0050 #define MR97310A_CONTRAST_MAX       31
0051 #define MR97310A_CONTRAST_DEFAULT   23
0052 
0053 #define MR97310A_CS_GAIN_MIN        0
0054 #define MR97310A_CS_GAIN_MAX        0x7ff
0055 #define MR97310A_CS_GAIN_DEFAULT    0x110
0056 
0057 #define MR97310A_CID_CLOCKDIV (V4L2_CTRL_CLASS_USER + 0x1000)
0058 #define MR97310A_MIN_CLOCKDIV_MIN   3
0059 #define MR97310A_MIN_CLOCKDIV_MAX   8
0060 #define MR97310A_MIN_CLOCKDIV_DEFAULT   3
0061 
0062 MODULE_AUTHOR("Kyle Guinn <elyk03@gmail.com>,Theodore Kilgore <kilgota@auburn.edu>");
0063 MODULE_DESCRIPTION("GSPCA/Mars-Semi MR97310A USB Camera Driver");
0064 MODULE_LICENSE("GPL");
0065 
0066 /* global parameters */
0067 static int force_sensor_type = -1;
0068 module_param(force_sensor_type, int, 0644);
0069 MODULE_PARM_DESC(force_sensor_type, "Force sensor type (-1 (auto), 0 or 1)");
0070 
0071 /* specific webcam descriptor */
0072 struct sd {
0073     struct gspca_dev gspca_dev;  /* !! must be the first item */
0074     struct { /* exposure/min_clockdiv control cluster */
0075         struct v4l2_ctrl *exposure;
0076         struct v4l2_ctrl *min_clockdiv;
0077     };
0078     u8 sof_read;
0079     u8 cam_type;    /* 0 is CIF and 1 is VGA */
0080     u8 sensor_type; /* We use 0 and 1 here, too. */
0081     u8 do_lcd_stop;
0082     u8 adj_colors;
0083 };
0084 
0085 struct sensor_w_data {
0086     u8 reg;
0087     u8 flags;
0088     u8 data[16];
0089     int len;
0090 };
0091 
0092 static void sd_stopN(struct gspca_dev *gspca_dev);
0093 
0094 static const struct v4l2_pix_format vga_mode[] = {
0095     {160, 120, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
0096         .bytesperline = 160,
0097         .sizeimage = 160 * 120,
0098         .colorspace = V4L2_COLORSPACE_SRGB,
0099         .priv = 4},
0100     {176, 144, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
0101         .bytesperline = 176,
0102         .sizeimage = 176 * 144,
0103         .colorspace = V4L2_COLORSPACE_SRGB,
0104         .priv = 3},
0105     {320, 240, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
0106         .bytesperline = 320,
0107         .sizeimage = 320 * 240,
0108         .colorspace = V4L2_COLORSPACE_SRGB,
0109         .priv = 2},
0110     {352, 288, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
0111         .bytesperline = 352,
0112         .sizeimage = 352 * 288,
0113         .colorspace = V4L2_COLORSPACE_SRGB,
0114         .priv = 1},
0115     {640, 480, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
0116         .bytesperline = 640,
0117         .sizeimage = 640 * 480,
0118         .colorspace = V4L2_COLORSPACE_SRGB,
0119         .priv = 0},
0120 };
0121 
0122 /* the bytes to write are in gspca_dev->usb_buf */
0123 static int mr_write(struct gspca_dev *gspca_dev, int len)
0124 {
0125     int rc;
0126 
0127     rc = usb_bulk_msg(gspca_dev->dev,
0128               usb_sndbulkpipe(gspca_dev->dev, 4),
0129               gspca_dev->usb_buf, len, NULL, 500);
0130     if (rc < 0)
0131         pr_err("reg write [%02x] error %d\n",
0132                gspca_dev->usb_buf[0], rc);
0133     return rc;
0134 }
0135 
0136 /* the bytes are read into gspca_dev->usb_buf */
0137 static int mr_read(struct gspca_dev *gspca_dev, int len)
0138 {
0139     int rc;
0140 
0141     rc = usb_bulk_msg(gspca_dev->dev,
0142               usb_rcvbulkpipe(gspca_dev->dev, 3),
0143               gspca_dev->usb_buf, len, NULL, 500);
0144     if (rc < 0)
0145         pr_err("reg read [%02x] error %d\n",
0146                gspca_dev->usb_buf[0], rc);
0147     return rc;
0148 }
0149 
0150 static int sensor_write_reg(struct gspca_dev *gspca_dev, u8 reg, u8 flags,
0151     const u8 *data, int len)
0152 {
0153     gspca_dev->usb_buf[0] = 0x1f;
0154     gspca_dev->usb_buf[1] = flags;
0155     gspca_dev->usb_buf[2] = reg;
0156     memcpy(gspca_dev->usb_buf + 3, data, len);
0157 
0158     return mr_write(gspca_dev, len + 3);
0159 }
0160 
0161 static int sensor_write_regs(struct gspca_dev *gspca_dev,
0162     const struct sensor_w_data *data, int len)
0163 {
0164     int i, rc;
0165 
0166     for (i = 0; i < len; i++) {
0167         rc = sensor_write_reg(gspca_dev, data[i].reg, data[i].flags,
0168                       data[i].data, data[i].len);
0169         if (rc < 0)
0170             return rc;
0171     }
0172 
0173     return 0;
0174 }
0175 
0176 static int sensor_write1(struct gspca_dev *gspca_dev, u8 reg, u8 data)
0177 {
0178     struct sd *sd = (struct sd *) gspca_dev;
0179     u8 buf, confirm_reg;
0180     int rc;
0181 
0182     buf = data;
0183     if (sd->cam_type == CAM_TYPE_CIF) {
0184         rc = sensor_write_reg(gspca_dev, reg, 0x01, &buf, 1);
0185         confirm_reg = sd->sensor_type ? 0x13 : 0x11;
0186     } else {
0187         rc = sensor_write_reg(gspca_dev, reg, 0x00, &buf, 1);
0188         confirm_reg = 0x11;
0189     }
0190     if (rc < 0)
0191         return rc;
0192 
0193     buf = 0x01;
0194     rc = sensor_write_reg(gspca_dev, confirm_reg, 0x00, &buf, 1);
0195     if (rc < 0)
0196         return rc;
0197 
0198     return 0;
0199 }
0200 
0201 static int cam_get_response16(struct gspca_dev *gspca_dev, u8 reg, int verbose)
0202 {
0203     int err_code;
0204 
0205     gspca_dev->usb_buf[0] = reg;
0206     err_code = mr_write(gspca_dev, 1);
0207     if (err_code < 0)
0208         return err_code;
0209 
0210     err_code = mr_read(gspca_dev, 16);
0211     if (err_code < 0)
0212         return err_code;
0213 
0214     if (verbose)
0215         gspca_dbg(gspca_dev, D_PROBE, "Register: %02x reads %02x%02x%02x\n",
0216               reg,
0217               gspca_dev->usb_buf[0],
0218               gspca_dev->usb_buf[1],
0219               gspca_dev->usb_buf[2]);
0220 
0221     return 0;
0222 }
0223 
0224 static int zero_the_pointer(struct gspca_dev *gspca_dev)
0225 {
0226     __u8 *data = gspca_dev->usb_buf;
0227     int err_code;
0228     u8 status = 0;
0229     int tries = 0;
0230 
0231     err_code = cam_get_response16(gspca_dev, 0x21, 0);
0232     if (err_code < 0)
0233         return err_code;
0234 
0235     data[0] = 0x19;
0236     data[1] = 0x51;
0237     err_code = mr_write(gspca_dev, 2);
0238     if (err_code < 0)
0239         return err_code;
0240 
0241     err_code = cam_get_response16(gspca_dev, 0x21, 0);
0242     if (err_code < 0)
0243         return err_code;
0244 
0245     data[0] = 0x19;
0246     data[1] = 0xba;
0247     err_code = mr_write(gspca_dev, 2);
0248     if (err_code < 0)
0249         return err_code;
0250 
0251     err_code = cam_get_response16(gspca_dev, 0x21, 0);
0252     if (err_code < 0)
0253         return err_code;
0254 
0255     data[0] = 0x19;
0256     data[1] = 0x00;
0257     err_code = mr_write(gspca_dev, 2);
0258     if (err_code < 0)
0259         return err_code;
0260 
0261     err_code = cam_get_response16(gspca_dev, 0x21, 0);
0262     if (err_code < 0)
0263         return err_code;
0264 
0265     data[0] = 0x19;
0266     data[1] = 0x00;
0267     err_code = mr_write(gspca_dev, 2);
0268     if (err_code < 0)
0269         return err_code;
0270 
0271     while (status != 0x0a && tries < 256) {
0272         err_code = cam_get_response16(gspca_dev, 0x21, 0);
0273         status = data[0];
0274         tries++;
0275         if (err_code < 0)
0276             return err_code;
0277     }
0278     if (status != 0x0a)
0279         gspca_err(gspca_dev, "status is %02x\n", status);
0280 
0281     tries = 0;
0282     while (tries < 4) {
0283         data[0] = 0x19;
0284         data[1] = 0x00;
0285         err_code = mr_write(gspca_dev, 2);
0286         if (err_code < 0)
0287             return err_code;
0288 
0289         err_code = cam_get_response16(gspca_dev, 0x21, 0);
0290         tries++;
0291         if (err_code < 0)
0292             return err_code;
0293     }
0294 
0295     data[0] = 0x19;
0296     err_code = mr_write(gspca_dev, 1);
0297     if (err_code < 0)
0298         return err_code;
0299 
0300     err_code = mr_read(gspca_dev, 16);
0301     if (err_code < 0)
0302         return err_code;
0303 
0304     return 0;
0305 }
0306 
0307 static int stream_start(struct gspca_dev *gspca_dev)
0308 {
0309     gspca_dev->usb_buf[0] = 0x01;
0310     gspca_dev->usb_buf[1] = 0x01;
0311     return mr_write(gspca_dev, 2);
0312 }
0313 
0314 static void stream_stop(struct gspca_dev *gspca_dev)
0315 {
0316     gspca_dev->usb_buf[0] = 0x01;
0317     gspca_dev->usb_buf[1] = 0x00;
0318     if (mr_write(gspca_dev, 2) < 0)
0319         gspca_err(gspca_dev, "Stream Stop failed\n");
0320 }
0321 
0322 static void lcd_stop(struct gspca_dev *gspca_dev)
0323 {
0324     gspca_dev->usb_buf[0] = 0x19;
0325     gspca_dev->usb_buf[1] = 0x54;
0326     if (mr_write(gspca_dev, 2) < 0)
0327         gspca_err(gspca_dev, "LCD Stop failed\n");
0328 }
0329 
0330 static int isoc_enable(struct gspca_dev *gspca_dev)
0331 {
0332     gspca_dev->usb_buf[0] = 0x00;
0333     gspca_dev->usb_buf[1] = 0x4d;  /* ISOC transferring enable... */
0334     return mr_write(gspca_dev, 2);
0335 }
0336 
0337 /* This function is called at probe time */
0338 static int sd_config(struct gspca_dev *gspca_dev,
0339              const struct usb_device_id *id)
0340 {
0341     struct sd *sd = (struct sd *) gspca_dev;
0342     struct cam *cam;
0343     int err_code;
0344 
0345     cam = &gspca_dev->cam;
0346     cam->cam_mode = vga_mode;
0347     cam->nmodes = ARRAY_SIZE(vga_mode);
0348     sd->do_lcd_stop = 0;
0349 
0350     /* Several of the supported CIF cameras share the same USB ID but
0351      * require different initializations and different control settings.
0352      * The same is true of the VGA cameras. Therefore, we are forced
0353      * to start the initialization process in order to determine which
0354      * camera is present. Some of the supported cameras require the
0355      * memory pointer to be set to 0 as the very first item of business
0356      * or else they will not stream. So we do that immediately.
0357      */
0358     err_code = zero_the_pointer(gspca_dev);
0359     if (err_code < 0)
0360         return err_code;
0361 
0362     err_code = stream_start(gspca_dev);
0363     if (err_code < 0)
0364         return err_code;
0365 
0366     /* Now, the query for sensor type. */
0367     err_code = cam_get_response16(gspca_dev, 0x07, 1);
0368     if (err_code < 0)
0369         return err_code;
0370 
0371     if (id->idProduct == 0x0110 || id->idProduct == 0x010e) {
0372         sd->cam_type = CAM_TYPE_CIF;
0373         cam->nmodes--;
0374         /*
0375          * All but one of the known CIF cameras share the same USB ID,
0376          * but two different init routines are in use, and the control
0377          * settings are different, too. We need to detect which camera
0378          * of the two known varieties is connected!
0379          *
0380          * A list of known CIF cameras follows. They all report either
0381          * 0200 for type 0 or 0300 for type 1.
0382          * If you have another to report, please do
0383          *
0384          * Name     sd->sensor_type     reported by
0385          *
0386          * Sakar 56379 Spy-shot 0       T. Kilgore
0387          * Innovage     0       T. Kilgore
0388          * Vivitar Mini     0       H. De Goede
0389          * Vivitar Mini     0       E. Rodriguez
0390          * Vivitar Mini     1       T. Kilgore
0391          * Elta-Media 8212dc    1       T. Kaiser
0392          * Philips dig. keych.  1       T. Kilgore
0393          * Trust Spyc@m 100 1       A. Jacobs
0394          */
0395         switch (gspca_dev->usb_buf[0]) {
0396         case 2:
0397             sd->sensor_type = 0;
0398             break;
0399         case 3:
0400             sd->sensor_type = 1;
0401             break;
0402         default:
0403             pr_err("Unknown CIF Sensor id : %02x\n",
0404                    gspca_dev->usb_buf[1]);
0405             return -ENODEV;
0406         }
0407         gspca_dbg(gspca_dev, D_PROBE, "MR97310A CIF camera detected, sensor: %d\n",
0408               sd->sensor_type);
0409     } else {
0410         sd->cam_type = CAM_TYPE_VGA;
0411 
0412         /*
0413          * Here is a table of the responses to the query for sensor
0414          * type, from the known MR97310A VGA cameras. Six different
0415          * cameras of which five share the same USB ID.
0416          *
0417          * Name         gspca_dev->usb_buf[]    sd->sensor_type
0418          *              sd->do_lcd_stop
0419          * Aiptek Pencam VGA+   0300        0       1
0420          * ION digital      0300        0       1
0421          * Argus DC-1620    0450        1       0
0422          * Argus QuickClix  0420        1       1
0423          * Sakar 77379 Digital  0350        0       1
0424          * Sakar 1638x CyberPix 0120        0       2
0425          *
0426          * Based upon these results, we assume default settings
0427          * and then correct as necessary, as follows.
0428          *
0429          */
0430 
0431         sd->sensor_type = 1;
0432         sd->do_lcd_stop = 0;
0433         sd->adj_colors = 0;
0434         if (gspca_dev->usb_buf[0] == 0x01) {
0435             sd->sensor_type = 2;
0436         } else if ((gspca_dev->usb_buf[0] != 0x03) &&
0437                     (gspca_dev->usb_buf[0] != 0x04)) {
0438             pr_err("Unknown VGA Sensor id Byte 0: %02x\n",
0439                    gspca_dev->usb_buf[0]);
0440             pr_err("Defaults assumed, may not work\n");
0441             pr_err("Please report this\n");
0442         }
0443         /* Sakar Digital color needs to be adjusted. */
0444         if ((gspca_dev->usb_buf[0] == 0x03) &&
0445                     (gspca_dev->usb_buf[1] == 0x50))
0446             sd->adj_colors = 1;
0447         if (gspca_dev->usb_buf[0] == 0x04) {
0448             sd->do_lcd_stop = 1;
0449             switch (gspca_dev->usb_buf[1]) {
0450             case 0x50:
0451                 sd->sensor_type = 0;
0452                 gspca_dbg(gspca_dev, D_PROBE, "sensor_type corrected to 0\n");
0453                 break;
0454             case 0x20:
0455                 /* Nothing to do here. */
0456                 break;
0457             default:
0458                 pr_err("Unknown VGA Sensor id Byte 1: %02x\n",
0459                        gspca_dev->usb_buf[1]);
0460                 pr_err("Defaults assumed, may not work\n");
0461                 pr_err("Please report this\n");
0462             }
0463         }
0464         gspca_dbg(gspca_dev, D_PROBE, "MR97310A VGA camera detected, sensor: %d\n",
0465               sd->sensor_type);
0466     }
0467     /* Stop streaming as we've started it only to probe the sensor type. */
0468     sd_stopN(gspca_dev);
0469 
0470     if (force_sensor_type != -1) {
0471         sd->sensor_type = !!force_sensor_type;
0472         gspca_dbg(gspca_dev, D_PROBE, "Forcing sensor type to: %d\n",
0473               sd->sensor_type);
0474     }
0475 
0476     return 0;
0477 }
0478 
0479 /* this function is called at probe and resume time */
0480 static int sd_init(struct gspca_dev *gspca_dev)
0481 {
0482     return 0;
0483 }
0484 
0485 static int start_cif_cam(struct gspca_dev *gspca_dev)
0486 {
0487     struct sd *sd = (struct sd *) gspca_dev;
0488     __u8 *data = gspca_dev->usb_buf;
0489     int err_code;
0490     static const __u8 startup_string[] = {
0491         0x00,
0492         0x0d,
0493         0x01,
0494         0x00, /* Hsize/8 for 352 or 320 */
0495         0x00, /* Vsize/4 for 288 or 240 */
0496         0x13, /* or 0xbb, depends on sensor */
0497         0x00, /* Hstart, depends on res. */
0498         0x00, /* reserved ? */
0499         0x00, /* Vstart, depends on res. and sensor */
0500         0x50, /* 0x54 to get 176 or 160 */
0501         0xc0
0502     };
0503 
0504     /* Note: Some of the above descriptions guessed from MR97113A driver */
0505 
0506     memcpy(data, startup_string, 11);
0507     if (sd->sensor_type)
0508         data[5] = 0xbb;
0509 
0510     switch (gspca_dev->pixfmt.width) {
0511     case 160:
0512         data[9] |= 0x04;  /* reg 8, 2:1 scale down from 320 */
0513         fallthrough;
0514     case 320:
0515     default:
0516         data[3] = 0x28;            /* reg 2, H size/8 */
0517         data[4] = 0x3c;            /* reg 3, V size/4 */
0518         data[6] = 0x14;            /* reg 5, H start  */
0519         data[8] = 0x1a + sd->sensor_type;  /* reg 7, V start  */
0520         break;
0521     case 176:
0522         data[9] |= 0x04;  /* reg 8, 2:1 scale down from 352 */
0523         fallthrough;
0524     case 352:
0525         data[3] = 0x2c;            /* reg 2, H size/8 */
0526         data[4] = 0x48;            /* reg 3, V size/4 */
0527         data[6] = 0x06;            /* reg 5, H start  */
0528         data[8] = 0x06 - sd->sensor_type;  /* reg 7, V start  */
0529         break;
0530     }
0531     err_code = mr_write(gspca_dev, 11);
0532     if (err_code < 0)
0533         return err_code;
0534 
0535     if (!sd->sensor_type) {
0536         static const struct sensor_w_data cif_sensor0_init_data[] = {
0537             {0x02, 0x00, {0x03, 0x5a, 0xb5, 0x01,
0538                       0x0f, 0x14, 0x0f, 0x10}, 8},
0539             {0x0c, 0x00, {0x04, 0x01, 0x01, 0x00, 0x1f}, 5},
0540             {0x12, 0x00, {0x07}, 1},
0541             {0x1f, 0x00, {0x06}, 1},
0542             {0x27, 0x00, {0x04}, 1},
0543             {0x29, 0x00, {0x0c}, 1},
0544             {0x40, 0x00, {0x40, 0x00, 0x04}, 3},
0545             {0x50, 0x00, {0x60}, 1},
0546             {0x60, 0x00, {0x06}, 1},
0547             {0x6b, 0x00, {0x85, 0x85, 0xc8, 0xc8, 0xc8, 0xc8}, 6},
0548             {0x72, 0x00, {0x1e, 0x56}, 2},
0549             {0x75, 0x00, {0x58, 0x40, 0xa2, 0x02, 0x31, 0x02,
0550                       0x31, 0x80, 0x00}, 9},
0551             {0x11, 0x00, {0x01}, 1},
0552             {0, 0, {0}, 0}
0553         };
0554         err_code = sensor_write_regs(gspca_dev, cif_sensor0_init_data,
0555                      ARRAY_SIZE(cif_sensor0_init_data));
0556     } else {    /* sd->sensor_type = 1 */
0557         static const struct sensor_w_data cif_sensor1_init_data[] = {
0558             /* Reg 3,4, 7,8 get set by the controls */
0559             {0x02, 0x00, {0x10}, 1},
0560             {0x05, 0x01, {0x22}, 1}, /* 5/6 also seen as 65h/32h */
0561             {0x06, 0x01, {0x00}, 1},
0562             {0x09, 0x02, {0x0e}, 1},
0563             {0x0a, 0x02, {0x05}, 1},
0564             {0x0b, 0x02, {0x05}, 1},
0565             {0x0c, 0x02, {0x0f}, 1},
0566             {0x0d, 0x02, {0x07}, 1},
0567             {0x0e, 0x02, {0x0c}, 1},
0568             {0x0f, 0x00, {0x00}, 1},
0569             {0x10, 0x00, {0x06}, 1},
0570             {0x11, 0x00, {0x07}, 1},
0571             {0x12, 0x00, {0x00}, 1},
0572             {0x13, 0x00, {0x01}, 1},
0573             {0, 0, {0}, 0}
0574         };
0575         /* Without this command the cam won't work with USB-UHCI */
0576         gspca_dev->usb_buf[0] = 0x0a;
0577         gspca_dev->usb_buf[1] = 0x00;
0578         err_code = mr_write(gspca_dev, 2);
0579         if (err_code < 0)
0580             return err_code;
0581         err_code = sensor_write_regs(gspca_dev, cif_sensor1_init_data,
0582                      ARRAY_SIZE(cif_sensor1_init_data));
0583     }
0584     return err_code;
0585 }
0586 
0587 static int start_vga_cam(struct gspca_dev *gspca_dev)
0588 {
0589     struct sd *sd = (struct sd *) gspca_dev;
0590     __u8 *data = gspca_dev->usb_buf;
0591     int err_code;
0592     static const __u8 startup_string[] =
0593         {0x00, 0x0d, 0x01, 0x00, 0x00, 0x2b, 0x00, 0x00,
0594          0x00, 0x50, 0xc0};
0595     /* What some of these mean is explained in start_cif_cam(), above */
0596 
0597     memcpy(data, startup_string, 11);
0598     if (!sd->sensor_type) {
0599         data[5]  = 0x00;
0600         data[10] = 0x91;
0601     }
0602     if (sd->sensor_type == 2) {
0603         data[5]  = 0x00;
0604         data[10] = 0x18;
0605     }
0606 
0607     switch (gspca_dev->pixfmt.width) {
0608     case 160:
0609         data[9] |= 0x0c;  /* reg 8, 4:1 scale down */
0610         fallthrough;
0611     case 320:
0612         data[9] |= 0x04;  /* reg 8, 2:1 scale down */
0613         fallthrough;
0614     case 640:
0615     default:
0616         data[3] = 0x50;  /* reg 2, H size/8 */
0617         data[4] = 0x78;  /* reg 3, V size/4 */
0618         data[6] = 0x04;  /* reg 5, H start */
0619         data[8] = 0x03;  /* reg 7, V start */
0620         if (sd->sensor_type == 2) {
0621             data[6] = 2;
0622             data[8] = 1;
0623         }
0624         if (sd->do_lcd_stop)
0625             data[8] = 0x04;  /* Bayer tile shifted */
0626         break;
0627 
0628     case 176:
0629         data[9] |= 0x04;  /* reg 8, 2:1 scale down */
0630         fallthrough;
0631     case 352:
0632         data[3] = 0x2c;  /* reg 2, H size */
0633         data[4] = 0x48;  /* reg 3, V size */
0634         data[6] = 0x94;  /* reg 5, H start */
0635         data[8] = 0x63;  /* reg 7, V start */
0636         if (sd->do_lcd_stop)
0637             data[8] = 0x64;  /* Bayer tile shifted */
0638         break;
0639     }
0640 
0641     err_code = mr_write(gspca_dev, 11);
0642     if (err_code < 0)
0643         return err_code;
0644 
0645     if (!sd->sensor_type) {
0646         static const struct sensor_w_data vga_sensor0_init_data[] = {
0647             {0x01, 0x00, {0x0c, 0x00, 0x04}, 3},
0648             {0x14, 0x00, {0x01, 0xe4, 0x02, 0x84}, 4},
0649             {0x20, 0x00, {0x00, 0x80, 0x00, 0x08}, 4},
0650             {0x25, 0x00, {0x03, 0xa9, 0x80}, 3},
0651             {0x30, 0x00, {0x30, 0x18, 0x10, 0x18}, 4},
0652             {0, 0, {0}, 0}
0653         };
0654         err_code = sensor_write_regs(gspca_dev, vga_sensor0_init_data,
0655                      ARRAY_SIZE(vga_sensor0_init_data));
0656     } else if (sd->sensor_type == 1) {
0657         static const struct sensor_w_data color_adj[] = {
0658             {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
0659                 /* adjusted blue, green, red gain correct
0660                    too much blue from the Sakar Digital */
0661                 0x05, 0x01, 0x04}, 8}
0662         };
0663 
0664         static const struct sensor_w_data color_no_adj[] = {
0665             {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
0666                 /* default blue, green, red gain settings */
0667                 0x07, 0x00, 0x01}, 8}
0668         };
0669 
0670         static const struct sensor_w_data vga_sensor1_init_data[] = {
0671             {0x11, 0x04, {0x01}, 1},
0672             {0x0a, 0x00, {0x00, 0x01, 0x00, 0x00, 0x01,
0673             /* These settings may be better for some cameras */
0674             /* {0x0a, 0x00, {0x01, 0x06, 0x00, 0x00, 0x01, */
0675                 0x00, 0x0a}, 7},
0676             {0x11, 0x04, {0x01}, 1},
0677             {0x12, 0x00, {0x00, 0x63, 0x00, 0x70, 0x00, 0x00}, 6},
0678             {0x11, 0x04, {0x01}, 1},
0679             {0, 0, {0}, 0}
0680         };
0681 
0682         if (sd->adj_colors)
0683             err_code = sensor_write_regs(gspca_dev, color_adj,
0684                      ARRAY_SIZE(color_adj));
0685         else
0686             err_code = sensor_write_regs(gspca_dev, color_no_adj,
0687                      ARRAY_SIZE(color_no_adj));
0688 
0689         if (err_code < 0)
0690             return err_code;
0691 
0692         err_code = sensor_write_regs(gspca_dev, vga_sensor1_init_data,
0693                      ARRAY_SIZE(vga_sensor1_init_data));
0694     } else {    /* sensor type == 2 */
0695         static const struct sensor_w_data vga_sensor2_init_data[] = {
0696 
0697             {0x01, 0x00, {0x48}, 1},
0698             {0x02, 0x00, {0x22}, 1},
0699             /* Reg 3 msb and 4 is lsb of the exposure setting*/
0700             {0x05, 0x00, {0x10}, 1},
0701             {0x06, 0x00, {0x00}, 1},
0702             {0x07, 0x00, {0x00}, 1},
0703             {0x08, 0x00, {0x00}, 1},
0704             {0x09, 0x00, {0x00}, 1},
0705             /* The following are used in the gain control
0706              * which is BTW completely borked in the OEM driver
0707              * The values for each color go from 0 to 0x7ff
0708              *{0x0a, 0x00, {0x01}, 1},  green1 gain msb
0709              *{0x0b, 0x00, {0x10}, 1},  green1 gain lsb
0710              *{0x0c, 0x00, {0x01}, 1},  red gain msb
0711              *{0x0d, 0x00, {0x10}, 1},  red gain lsb
0712              *{0x0e, 0x00, {0x01}, 1},  blue gain msb
0713              *{0x0f, 0x00, {0x10}, 1},  blue gain lsb
0714              *{0x10, 0x00, {0x01}, 1}, green2 gain msb
0715              *{0x11, 0x00, {0x10}, 1}, green2 gain lsb
0716              */
0717             {0x12, 0x00, {0x00}, 1},
0718             {0x13, 0x00, {0x04}, 1}, /* weird effect on colors */
0719             {0x14, 0x00, {0x00}, 1},
0720             {0x15, 0x00, {0x06}, 1},
0721             {0x16, 0x00, {0x01}, 1},
0722             {0x17, 0x00, {0xe2}, 1}, /* vertical alignment */
0723             {0x18, 0x00, {0x02}, 1},
0724             {0x19, 0x00, {0x82}, 1}, /* don't mess with */
0725             {0x1a, 0x00, {0x00}, 1},
0726             {0x1b, 0x00, {0x20}, 1},
0727             /* {0x1c, 0x00, {0x17}, 1}, contrast control */
0728             {0x1d, 0x00, {0x80}, 1}, /* moving causes a mess */
0729             {0x1e, 0x00, {0x08}, 1}, /* moving jams the camera */
0730             {0x1f, 0x00, {0x0c}, 1},
0731             {0x20, 0x00, {0x00}, 1},
0732             {0, 0, {0}, 0}
0733         };
0734         err_code = sensor_write_regs(gspca_dev, vga_sensor2_init_data,
0735                      ARRAY_SIZE(vga_sensor2_init_data));
0736     }
0737     return err_code;
0738 }
0739 
0740 static int sd_start(struct gspca_dev *gspca_dev)
0741 {
0742     struct sd *sd = (struct sd *) gspca_dev;
0743     int err_code;
0744 
0745     sd->sof_read = 0;
0746 
0747     /* Some of the VGA cameras require the memory pointer
0748      * to be set to 0 again. We have been forced to start the
0749      * stream in sd_config() to detect the hardware, and closed it.
0750      * Thus, we need here to do a completely fresh and clean start. */
0751     err_code = zero_the_pointer(gspca_dev);
0752     if (err_code < 0)
0753         return err_code;
0754 
0755     err_code = stream_start(gspca_dev);
0756     if (err_code < 0)
0757         return err_code;
0758 
0759     if (sd->cam_type == CAM_TYPE_CIF) {
0760         err_code = start_cif_cam(gspca_dev);
0761     } else {
0762         err_code = start_vga_cam(gspca_dev);
0763     }
0764     if (err_code < 0)
0765         return err_code;
0766 
0767     return isoc_enable(gspca_dev);
0768 }
0769 
0770 static void sd_stopN(struct gspca_dev *gspca_dev)
0771 {
0772     struct sd *sd = (struct sd *) gspca_dev;
0773 
0774     stream_stop(gspca_dev);
0775     /* Not all the cams need this, but even if not, probably a good idea */
0776     zero_the_pointer(gspca_dev);
0777     if (sd->do_lcd_stop)
0778         lcd_stop(gspca_dev);
0779 }
0780 
0781 static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
0782 {
0783     struct sd *sd = (struct sd *) gspca_dev;
0784     u8 sign_reg = 7;  /* This reg and the next one used on CIF cams. */
0785     u8 value_reg = 8; /* VGA cams seem to use regs 0x0b and 0x0c */
0786     static const u8 quick_clix_table[] =
0787     /*    0  1  2   3  4  5  6  7  8  9  10  11  12  13  14  15 */
0788         { 0, 4, 8, 12, 1, 2, 3, 5, 6, 9,  7, 10, 13, 11, 14, 15};
0789     if (sd->cam_type == CAM_TYPE_VGA) {
0790         sign_reg += 4;
0791         value_reg += 4;
0792     }
0793 
0794     /* Note register 7 is also seen as 0x8x or 0xCx in some dumps */
0795     if (val > 0) {
0796         sensor_write1(gspca_dev, sign_reg, 0x00);
0797     } else {
0798         sensor_write1(gspca_dev, sign_reg, 0x01);
0799         val = 257 - val;
0800     }
0801     /* Use lookup table for funky Argus QuickClix brightness */
0802     if (sd->do_lcd_stop)
0803         val = quick_clix_table[val];
0804 
0805     sensor_write1(gspca_dev, value_reg, val);
0806 }
0807 
0808 static void setexposure(struct gspca_dev *gspca_dev, s32 expo, s32 min_clockdiv)
0809 {
0810     struct sd *sd = (struct sd *) gspca_dev;
0811     int exposure = MR97310A_EXPOSURE_DEFAULT;
0812     u8 buf[2];
0813 
0814     if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1) {
0815         /* This cam does not like exposure settings < 300,
0816            so scale 0 - 4095 to 300 - 4095 */
0817         exposure = (expo * 9267) / 10000 + 300;
0818         sensor_write1(gspca_dev, 3, exposure >> 4);
0819         sensor_write1(gspca_dev, 4, exposure & 0x0f);
0820     } else if (sd->sensor_type == 2) {
0821         exposure = expo;
0822         exposure >>= 3;
0823         sensor_write1(gspca_dev, 3, exposure >> 8);
0824         sensor_write1(gspca_dev, 4, exposure & 0xff);
0825     } else {
0826         /* We have both a clock divider and an exposure register.
0827            We first calculate the clock divider, as that determines
0828            the maximum exposure and then we calculate the exposure
0829            register setting (which goes from 0 - 511).
0830 
0831            Note our 0 - 4095 exposure is mapped to 0 - 511
0832            milliseconds exposure time */
0833         u8 clockdiv = (60 * expo + 7999) / 8000;
0834 
0835         /* Limit framerate to not exceed usb bandwidth */
0836         if (clockdiv < min_clockdiv && gspca_dev->pixfmt.width >= 320)
0837             clockdiv = min_clockdiv;
0838         else if (clockdiv < 2)
0839             clockdiv = 2;
0840 
0841         if (sd->cam_type == CAM_TYPE_VGA && clockdiv < 4)
0842             clockdiv = 4;
0843 
0844         /* Frame exposure time in ms = 1000 * clockdiv / 60 ->
0845         exposure = (sd->exposure / 8) * 511 / (1000 * clockdiv / 60) */
0846         exposure = (60 * 511 * expo) / (8000 * clockdiv);
0847         if (exposure > 511)
0848             exposure = 511;
0849 
0850         /* exposure register value is reversed! */
0851         exposure = 511 - exposure;
0852 
0853         buf[0] = exposure & 0xff;
0854         buf[1] = exposure >> 8;
0855         sensor_write_reg(gspca_dev, 0x0e, 0, buf, 2);
0856         sensor_write1(gspca_dev, 0x02, clockdiv);
0857     }
0858 }
0859 
0860 static void setgain(struct gspca_dev *gspca_dev, s32 val)
0861 {
0862     struct sd *sd = (struct sd *) gspca_dev;
0863     u8 gainreg;
0864 
0865     if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1)
0866         sensor_write1(gspca_dev, 0x0e, val);
0867     else if (sd->cam_type == CAM_TYPE_VGA && sd->sensor_type == 2)
0868         for (gainreg = 0x0a; gainreg < 0x11; gainreg += 2) {
0869             sensor_write1(gspca_dev, gainreg, val >> 8);
0870             sensor_write1(gspca_dev, gainreg + 1, val & 0xff);
0871         }
0872     else
0873         sensor_write1(gspca_dev, 0x10, val);
0874 }
0875 
0876 static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
0877 {
0878     sensor_write1(gspca_dev, 0x1c, val);
0879 }
0880 
0881 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
0882 {
0883     struct gspca_dev *gspca_dev =
0884         container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
0885     struct sd *sd = (struct sd *)gspca_dev;
0886 
0887     gspca_dev->usb_err = 0;
0888 
0889     if (!gspca_dev->streaming)
0890         return 0;
0891 
0892     switch (ctrl->id) {
0893     case V4L2_CID_BRIGHTNESS:
0894         setbrightness(gspca_dev, ctrl->val);
0895         break;
0896     case V4L2_CID_CONTRAST:
0897         setcontrast(gspca_dev, ctrl->val);
0898         break;
0899     case V4L2_CID_EXPOSURE:
0900         setexposure(gspca_dev, sd->exposure->val,
0901                 sd->min_clockdiv ? sd->min_clockdiv->val : 0);
0902         break;
0903     case V4L2_CID_GAIN:
0904         setgain(gspca_dev, ctrl->val);
0905         break;
0906     }
0907     return gspca_dev->usb_err;
0908 }
0909 
0910 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
0911     .s_ctrl = sd_s_ctrl,
0912 };
0913 
0914 static int sd_init_controls(struct gspca_dev *gspca_dev)
0915 {
0916     struct sd *sd = (struct sd *)gspca_dev;
0917     struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
0918     static const struct v4l2_ctrl_config clockdiv = {
0919         .ops = &sd_ctrl_ops,
0920         .id = MR97310A_CID_CLOCKDIV,
0921         .type = V4L2_CTRL_TYPE_INTEGER,
0922         .name = "Minimum Clock Divider",
0923         .min = MR97310A_MIN_CLOCKDIV_MIN,
0924         .max = MR97310A_MIN_CLOCKDIV_MAX,
0925         .step = 1,
0926         .def = MR97310A_MIN_CLOCKDIV_DEFAULT,
0927     };
0928     bool has_brightness = false;
0929     bool has_argus_brightness = false;
0930     bool has_contrast = false;
0931     bool has_gain = false;
0932     bool has_cs_gain = false;
0933     bool has_exposure = false;
0934     bool has_clockdiv = false;
0935 
0936     gspca_dev->vdev.ctrl_handler = hdl;
0937     v4l2_ctrl_handler_init(hdl, 4);
0938 
0939     /* Setup controls depending on camera type */
0940     if (sd->cam_type == CAM_TYPE_CIF) {
0941         /* No brightness for sensor_type 0 */
0942         if (sd->sensor_type == 0)
0943             has_exposure = has_gain = has_clockdiv = true;
0944         else
0945             has_exposure = has_gain = has_brightness = true;
0946     } else {
0947         /* All controls need to be disabled if VGA sensor_type is 0 */
0948         if (sd->sensor_type == 0)
0949             ; /* no controls! */
0950         else if (sd->sensor_type == 2)
0951             has_exposure = has_cs_gain = has_contrast = true;
0952         else if (sd->do_lcd_stop)
0953             has_exposure = has_gain = has_argus_brightness =
0954                 has_clockdiv = true;
0955         else
0956             has_exposure = has_gain = has_brightness =
0957                 has_clockdiv = true;
0958     }
0959 
0960     /* Separate brightness control description for Argus QuickClix as it has
0961      * different limits from the other mr97310a cameras, and separate gain
0962      * control for Sakar CyberPix camera. */
0963     /*
0964      * This control is disabled for CIF type 1 and VGA type 0 cameras.
0965      * It does not quite act linearly for the Argus QuickClix camera,
0966      * but it does control brightness. The values are 0 - 15 only, and
0967      * the table above makes them act consecutively.
0968      */
0969     if (has_brightness)
0970         v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
0971             V4L2_CID_BRIGHTNESS, -254, 255, 1,
0972             MR97310A_BRIGHTNESS_DEFAULT);
0973     else if (has_argus_brightness)
0974         v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
0975             V4L2_CID_BRIGHTNESS, 0, 15, 1,
0976             MR97310A_BRIGHTNESS_DEFAULT);
0977     if (has_contrast)
0978         v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
0979             V4L2_CID_CONTRAST, MR97310A_CONTRAST_MIN,
0980             MR97310A_CONTRAST_MAX, 1, MR97310A_CONTRAST_DEFAULT);
0981     if (has_gain)
0982         v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
0983             V4L2_CID_GAIN, MR97310A_GAIN_MIN, MR97310A_GAIN_MAX,
0984             1, MR97310A_GAIN_DEFAULT);
0985     else if (has_cs_gain)
0986         v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, V4L2_CID_GAIN,
0987             MR97310A_CS_GAIN_MIN, MR97310A_CS_GAIN_MAX,
0988             1, MR97310A_CS_GAIN_DEFAULT);
0989     if (has_exposure)
0990         sd->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
0991             V4L2_CID_EXPOSURE, MR97310A_EXPOSURE_MIN,
0992             MR97310A_EXPOSURE_MAX, 1, MR97310A_EXPOSURE_DEFAULT);
0993     if (has_clockdiv)
0994         sd->min_clockdiv = v4l2_ctrl_new_custom(hdl, &clockdiv, NULL);
0995 
0996     if (hdl->error) {
0997         pr_err("Could not initialize controls\n");
0998         return hdl->error;
0999     }
1000     if (has_exposure && has_clockdiv)
1001         v4l2_ctrl_cluster(2, &sd->exposure);
1002     return 0;
1003 }
1004 
1005 /* Include pac common sof detection functions */
1006 #include "pac_common.h"
1007 
1008 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1009             u8 *data,       /* isoc packet */
1010             int len)        /* iso packet length */
1011 {
1012     struct sd *sd = (struct sd *) gspca_dev;
1013     unsigned char *sof;
1014 
1015     sof = pac_find_sof(gspca_dev, &sd->sof_read, data, len);
1016     if (sof) {
1017         int n;
1018 
1019         /* finish decoding current frame */
1020         n = sof - data;
1021         if (n > sizeof pac_sof_marker)
1022             n -= sizeof pac_sof_marker;
1023         else
1024             n = 0;
1025         gspca_frame_add(gspca_dev, LAST_PACKET,
1026                     data, n);
1027         /* Start next frame. */
1028         gspca_frame_add(gspca_dev, FIRST_PACKET,
1029             pac_sof_marker, sizeof pac_sof_marker);
1030         len -= sof - data;
1031         data = sof;
1032     }
1033     gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
1034 }
1035 
1036 /* sub-driver description */
1037 static const struct sd_desc sd_desc = {
1038     .name = MODULE_NAME,
1039     .config = sd_config,
1040     .init = sd_init,
1041     .init_controls = sd_init_controls,
1042     .start = sd_start,
1043     .stopN = sd_stopN,
1044     .pkt_scan = sd_pkt_scan,
1045 };
1046 
1047 /* -- module initialisation -- */
1048 static const struct usb_device_id device_table[] = {
1049     {USB_DEVICE(0x08ca, 0x0110)},   /* Trust Spyc@m 100 */
1050     {USB_DEVICE(0x08ca, 0x0111)},   /* Aiptek Pencam VGA+ */
1051     {USB_DEVICE(0x093a, 0x010f)},   /* All other known MR97310A VGA cams */
1052     {USB_DEVICE(0x093a, 0x010e)},   /* All known MR97310A CIF cams */
1053     {}
1054 };
1055 MODULE_DEVICE_TABLE(usb, device_table);
1056 
1057 /* -- device connect -- */
1058 static int sd_probe(struct usb_interface *intf,
1059             const struct usb_device_id *id)
1060 {
1061     return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1062                    THIS_MODULE);
1063 }
1064 
1065 static struct usb_driver sd_driver = {
1066     .name = MODULE_NAME,
1067     .id_table = device_table,
1068     .probe = sd_probe,
1069     .disconnect = gspca_disconnect,
1070 #ifdef CONFIG_PM
1071     .suspend = gspca_suspend,
1072     .resume = gspca_resume,
1073     .reset_resume = gspca_resume,
1074 #endif
1075 };
1076 
1077 module_usb_driver(sd_driver);