0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
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
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
0072 struct sd {
0073 struct gspca_dev gspca_dev;
0074 struct {
0075 struct v4l2_ctrl *exposure;
0076 struct v4l2_ctrl *min_clockdiv;
0077 };
0078 u8 sof_read;
0079 u8 cam_type;
0080 u8 sensor_type;
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
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
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;
0334 return mr_write(gspca_dev, 2);
0335 }
0336
0337
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
0351
0352
0353
0354
0355
0356
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
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
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
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
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
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
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
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
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
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,
0495 0x00,
0496 0x13,
0497 0x00,
0498 0x00,
0499 0x00,
0500 0x50,
0501 0xc0
0502 };
0503
0504
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;
0513 fallthrough;
0514 case 320:
0515 default:
0516 data[3] = 0x28;
0517 data[4] = 0x3c;
0518 data[6] = 0x14;
0519 data[8] = 0x1a + sd->sensor_type;
0520 break;
0521 case 176:
0522 data[9] |= 0x04;
0523 fallthrough;
0524 case 352:
0525 data[3] = 0x2c;
0526 data[4] = 0x48;
0527 data[6] = 0x06;
0528 data[8] = 0x06 - sd->sensor_type;
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 {
0557 static const struct sensor_w_data cif_sensor1_init_data[] = {
0558
0559 {0x02, 0x00, {0x10}, 1},
0560 {0x05, 0x01, {0x22}, 1},
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
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
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;
0610 fallthrough;
0611 case 320:
0612 data[9] |= 0x04;
0613 fallthrough;
0614 case 640:
0615 default:
0616 data[3] = 0x50;
0617 data[4] = 0x78;
0618 data[6] = 0x04;
0619 data[8] = 0x03;
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;
0626 break;
0627
0628 case 176:
0629 data[9] |= 0x04;
0630 fallthrough;
0631 case 352:
0632 data[3] = 0x2c;
0633 data[4] = 0x48;
0634 data[6] = 0x94;
0635 data[8] = 0x63;
0636 if (sd->do_lcd_stop)
0637 data[8] = 0x64;
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
0660
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
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
0674
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 {
0695 static const struct sensor_w_data vga_sensor2_init_data[] = {
0696
0697 {0x01, 0x00, {0x48}, 1},
0698 {0x02, 0x00, {0x22}, 1},
0699
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
0706
0707
0708
0709
0710
0711
0712
0713
0714
0715
0716
0717 {0x12, 0x00, {0x00}, 1},
0718 {0x13, 0x00, {0x04}, 1},
0719 {0x14, 0x00, {0x00}, 1},
0720 {0x15, 0x00, {0x06}, 1},
0721 {0x16, 0x00, {0x01}, 1},
0722 {0x17, 0x00, {0xe2}, 1},
0723 {0x18, 0x00, {0x02}, 1},
0724 {0x19, 0x00, {0x82}, 1},
0725 {0x1a, 0x00, {0x00}, 1},
0726 {0x1b, 0x00, {0x20}, 1},
0727
0728 {0x1d, 0x00, {0x80}, 1},
0729 {0x1e, 0x00, {0x08}, 1},
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
0748
0749
0750
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
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;
0785 u8 value_reg = 8;
0786 static const u8 quick_clix_table[] =
0787
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
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
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
0816
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
0827
0828
0829
0830
0831
0832
0833 u8 clockdiv = (60 * expo + 7999) / 8000;
0834
0835
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
0845
0846 exposure = (60 * 511 * expo) / (8000 * clockdiv);
0847 if (exposure > 511)
0848 exposure = 511;
0849
0850
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
0940 if (sd->cam_type == CAM_TYPE_CIF) {
0941
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
0948 if (sd->sensor_type == 0)
0949 ;
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
0961
0962
0963
0964
0965
0966
0967
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
1006 #include "pac_common.h"
1007
1008 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1009 u8 *data,
1010 int len)
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
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
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
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
1048 static const struct usb_device_id device_table[] = {
1049 {USB_DEVICE(0x08ca, 0x0110)},
1050 {USB_DEVICE(0x08ca, 0x0111)},
1051 {USB_DEVICE(0x093a, 0x010f)},
1052 {USB_DEVICE(0x093a, 0x010e)},
1053 {}
1054 };
1055 MODULE_DEVICE_TABLE(usb, device_table);
1056
1057
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);