1 /* 2 * Mars MR97310A library 3 * 4 * The original mr97310a driver, which supported the Aiptek Pencam VGA+, is 5 * Copyright (C) 2009 Kyle Guinn <elyk03@gmail.com> 6 * 7 * Support for the MR97310A cameras in addition to the Aiptek Pencam VGA+ 8 * and for the routines for detecting and classifying these various cameras, 9 * is Copyright (C) 2009 Theodore Kilgore <kilgota@auburn.edu> 10 * 11 * Support for the control settings for the CIF cameras is 12 * Copyright (C) 2009 Hans de Goede <hdegoede@redhat.com> and 13 * Thomas Kaiser <thomas@kaiser-linux.li> 14 * 15 * Support for the control settings for the VGA cameras is 16 * Copyright (C) 2009 Theodore Kilgore <kilgota@auburn.edu> 17 * 18 * Several previously unsupported cameras are owned and have been tested by 19 * Hans de Goede <hdegoede@redhat.com> and 20 * Thomas Kaiser <thomas@kaiser-linux.li> and 21 * Theodore Kilgore <kilgota@auburn.edu> and 22 * Edmond Rodriguez <erodrig_97@yahoo.com> and 23 * Aurelien Jacobs <aurel@gnuage.org> 24 * 25 * The MR97311A support in gspca/mars.c has been helpful in understanding some 26 * of the registers in these cameras. 27 * 28 * This program is free software; you can redistribute it and/or modify 29 * it under the terms of the GNU General Public License as published by 30 * the Free Software Foundation; either version 2 of the License, or 31 * any later version. 32 * 33 * This program is distributed in the hope that it will be useful, 34 * but WITHOUT ANY WARRANTY; without even the implied warranty of 35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 36 * GNU General Public License for more details. 37 * 38 * You should have received a copy of the GNU General Public License 39 * along with this program; if not, write to the Free Software 40 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 41 */ 42 43 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 44 45 #define MODULE_NAME "mr97310a" 46 47 #include "gspca.h" 48 49 #define CAM_TYPE_CIF 0 50 #define CAM_TYPE_VGA 1 51 52 #define MR97310A_BRIGHTNESS_DEFAULT 0 53 54 #define MR97310A_EXPOSURE_MIN 0 55 #define MR97310A_EXPOSURE_MAX 4095 56 #define MR97310A_EXPOSURE_DEFAULT 1000 57 58 #define MR97310A_GAIN_MIN 0 59 #define MR97310A_GAIN_MAX 31 60 #define MR97310A_GAIN_DEFAULT 25 61 62 #define MR97310A_CONTRAST_MIN 0 63 #define MR97310A_CONTRAST_MAX 31 64 #define MR97310A_CONTRAST_DEFAULT 23 65 66 #define MR97310A_CS_GAIN_MIN 0 67 #define MR97310A_CS_GAIN_MAX 0x7ff 68 #define MR97310A_CS_GAIN_DEFAULT 0x110 69 70 #define MR97310A_CID_CLOCKDIV (V4L2_CTRL_CLASS_USER + 0x1000) 71 #define MR97310A_MIN_CLOCKDIV_MIN 3 72 #define MR97310A_MIN_CLOCKDIV_MAX 8 73 #define MR97310A_MIN_CLOCKDIV_DEFAULT 3 74 75 MODULE_AUTHOR("Kyle Guinn <elyk03@gmail.com>,Theodore Kilgore <kilgota@auburn.edu>"); 76 MODULE_DESCRIPTION("GSPCA/Mars-Semi MR97310A USB Camera Driver"); 77 MODULE_LICENSE("GPL"); 78 79 /* global parameters */ 80 static int force_sensor_type = -1; 81 module_param(force_sensor_type, int, 0644); 82 MODULE_PARM_DESC(force_sensor_type, "Force sensor type (-1 (auto), 0 or 1)"); 83 84 /* specific webcam descriptor */ 85 struct sd { 86 struct gspca_dev gspca_dev; /* !! must be the first item */ 87 struct { /* exposure/min_clockdiv control cluster */ 88 struct v4l2_ctrl *exposure; 89 struct v4l2_ctrl *min_clockdiv; 90 }; 91 u8 sof_read; 92 u8 cam_type; /* 0 is CIF and 1 is VGA */ 93 u8 sensor_type; /* We use 0 and 1 here, too. */ 94 u8 do_lcd_stop; 95 u8 adj_colors; 96 }; 97 98 struct sensor_w_data { 99 u8 reg; 100 u8 flags; 101 u8 data[16]; 102 int len; 103 }; 104 105 static void sd_stopN(struct gspca_dev *gspca_dev); 106 107 static const struct v4l2_pix_format vga_mode[] = { 108 {160, 120, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE, 109 .bytesperline = 160, 110 .sizeimage = 160 * 120, 111 .colorspace = V4L2_COLORSPACE_SRGB, 112 .priv = 4}, 113 {176, 144, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE, 114 .bytesperline = 176, 115 .sizeimage = 176 * 144, 116 .colorspace = V4L2_COLORSPACE_SRGB, 117 .priv = 3}, 118 {320, 240, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE, 119 .bytesperline = 320, 120 .sizeimage = 320 * 240, 121 .colorspace = V4L2_COLORSPACE_SRGB, 122 .priv = 2}, 123 {352, 288, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE, 124 .bytesperline = 352, 125 .sizeimage = 352 * 288, 126 .colorspace = V4L2_COLORSPACE_SRGB, 127 .priv = 1}, 128 {640, 480, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE, 129 .bytesperline = 640, 130 .sizeimage = 640 * 480, 131 .colorspace = V4L2_COLORSPACE_SRGB, 132 .priv = 0}, 133 }; 134 135 /* the bytes to write are in gspca_dev->usb_buf */ 136 static int mr_write(struct gspca_dev *gspca_dev, int len) 137 { 138 int rc; 139 140 rc = usb_bulk_msg(gspca_dev->dev, 141 usb_sndbulkpipe(gspca_dev->dev, 4), 142 gspca_dev->usb_buf, len, NULL, 500); 143 if (rc < 0) 144 pr_err("reg write [%02x] error %d\n", 145 gspca_dev->usb_buf[0], rc); 146 return rc; 147 } 148 149 /* the bytes are read into gspca_dev->usb_buf */ 150 static int mr_read(struct gspca_dev *gspca_dev, int len) 151 { 152 int rc; 153 154 rc = usb_bulk_msg(gspca_dev->dev, 155 usb_rcvbulkpipe(gspca_dev->dev, 3), 156 gspca_dev->usb_buf, len, NULL, 500); 157 if (rc < 0) 158 pr_err("reg read [%02x] error %d\n", 159 gspca_dev->usb_buf[0], rc); 160 return rc; 161 } 162 163 static int sensor_write_reg(struct gspca_dev *gspca_dev, u8 reg, u8 flags, 164 const u8 *data, int len) 165 { 166 gspca_dev->usb_buf[0] = 0x1f; 167 gspca_dev->usb_buf[1] = flags; 168 gspca_dev->usb_buf[2] = reg; 169 memcpy(gspca_dev->usb_buf + 3, data, len); 170 171 return mr_write(gspca_dev, len + 3); 172 } 173 174 static int sensor_write_regs(struct gspca_dev *gspca_dev, 175 const struct sensor_w_data *data, int len) 176 { 177 int i, rc; 178 179 for (i = 0; i < len; i++) { 180 rc = sensor_write_reg(gspca_dev, data[i].reg, data[i].flags, 181 data[i].data, data[i].len); 182 if (rc < 0) 183 return rc; 184 } 185 186 return 0; 187 } 188 189 static int sensor_write1(struct gspca_dev *gspca_dev, u8 reg, u8 data) 190 { 191 struct sd *sd = (struct sd *) gspca_dev; 192 u8 buf, confirm_reg; 193 int rc; 194 195 buf = data; 196 if (sd->cam_type == CAM_TYPE_CIF) { 197 rc = sensor_write_reg(gspca_dev, reg, 0x01, &buf, 1); 198 confirm_reg = sd->sensor_type ? 0x13 : 0x11; 199 } else { 200 rc = sensor_write_reg(gspca_dev, reg, 0x00, &buf, 1); 201 confirm_reg = 0x11; 202 } 203 if (rc < 0) 204 return rc; 205 206 buf = 0x01; 207 rc = sensor_write_reg(gspca_dev, confirm_reg, 0x00, &buf, 1); 208 if (rc < 0) 209 return rc; 210 211 return 0; 212 } 213 214 static int cam_get_response16(struct gspca_dev *gspca_dev, u8 reg, int verbose) 215 { 216 int err_code; 217 218 gspca_dev->usb_buf[0] = reg; 219 err_code = mr_write(gspca_dev, 1); 220 if (err_code < 0) 221 return err_code; 222 223 err_code = mr_read(gspca_dev, 16); 224 if (err_code < 0) 225 return err_code; 226 227 if (verbose) 228 PDEBUG(D_PROBE, "Register: %02x reads %02x%02x%02x", reg, 229 gspca_dev->usb_buf[0], 230 gspca_dev->usb_buf[1], 231 gspca_dev->usb_buf[2]); 232 233 return 0; 234 } 235 236 static int zero_the_pointer(struct gspca_dev *gspca_dev) 237 { 238 __u8 *data = gspca_dev->usb_buf; 239 int err_code; 240 u8 status = 0; 241 int tries = 0; 242 243 err_code = cam_get_response16(gspca_dev, 0x21, 0); 244 if (err_code < 0) 245 return err_code; 246 247 data[0] = 0x19; 248 data[1] = 0x51; 249 err_code = mr_write(gspca_dev, 2); 250 if (err_code < 0) 251 return err_code; 252 253 err_code = cam_get_response16(gspca_dev, 0x21, 0); 254 if (err_code < 0) 255 return err_code; 256 257 data[0] = 0x19; 258 data[1] = 0xba; 259 err_code = mr_write(gspca_dev, 2); 260 if (err_code < 0) 261 return err_code; 262 263 err_code = cam_get_response16(gspca_dev, 0x21, 0); 264 if (err_code < 0) 265 return err_code; 266 267 data[0] = 0x19; 268 data[1] = 0x00; 269 err_code = mr_write(gspca_dev, 2); 270 if (err_code < 0) 271 return err_code; 272 273 err_code = cam_get_response16(gspca_dev, 0x21, 0); 274 if (err_code < 0) 275 return err_code; 276 277 data[0] = 0x19; 278 data[1] = 0x00; 279 err_code = mr_write(gspca_dev, 2); 280 if (err_code < 0) 281 return err_code; 282 283 while (status != 0x0a && tries < 256) { 284 err_code = cam_get_response16(gspca_dev, 0x21, 0); 285 status = data[0]; 286 tries++; 287 if (err_code < 0) 288 return err_code; 289 } 290 if (status != 0x0a) 291 PERR("status is %02x", status); 292 293 tries = 0; 294 while (tries < 4) { 295 data[0] = 0x19; 296 data[1] = 0x00; 297 err_code = mr_write(gspca_dev, 2); 298 if (err_code < 0) 299 return err_code; 300 301 err_code = cam_get_response16(gspca_dev, 0x21, 0); 302 status = data[0]; 303 tries++; 304 if (err_code < 0) 305 return err_code; 306 } 307 308 data[0] = 0x19; 309 err_code = mr_write(gspca_dev, 1); 310 if (err_code < 0) 311 return err_code; 312 313 err_code = mr_read(gspca_dev, 16); 314 if (err_code < 0) 315 return err_code; 316 317 return 0; 318 } 319 320 static int stream_start(struct gspca_dev *gspca_dev) 321 { 322 gspca_dev->usb_buf[0] = 0x01; 323 gspca_dev->usb_buf[1] = 0x01; 324 return mr_write(gspca_dev, 2); 325 } 326 327 static void stream_stop(struct gspca_dev *gspca_dev) 328 { 329 gspca_dev->usb_buf[0] = 0x01; 330 gspca_dev->usb_buf[1] = 0x00; 331 if (mr_write(gspca_dev, 2) < 0) 332 PERR("Stream Stop failed"); 333 } 334 335 static void lcd_stop(struct gspca_dev *gspca_dev) 336 { 337 gspca_dev->usb_buf[0] = 0x19; 338 gspca_dev->usb_buf[1] = 0x54; 339 if (mr_write(gspca_dev, 2) < 0) 340 PERR("LCD Stop failed"); 341 } 342 343 static int isoc_enable(struct gspca_dev *gspca_dev) 344 { 345 gspca_dev->usb_buf[0] = 0x00; 346 gspca_dev->usb_buf[1] = 0x4d; /* ISOC transferring enable... */ 347 return mr_write(gspca_dev, 2); 348 } 349 350 /* This function is called at probe time */ 351 static int sd_config(struct gspca_dev *gspca_dev, 352 const struct usb_device_id *id) 353 { 354 struct sd *sd = (struct sd *) gspca_dev; 355 struct cam *cam; 356 int err_code; 357 358 cam = &gspca_dev->cam; 359 cam->cam_mode = vga_mode; 360 cam->nmodes = ARRAY_SIZE(vga_mode); 361 sd->do_lcd_stop = 0; 362 363 /* Several of the supported CIF cameras share the same USB ID but 364 * require different initializations and different control settings. 365 * The same is true of the VGA cameras. Therefore, we are forced 366 * to start the initialization process in order to determine which 367 * camera is present. Some of the supported cameras require the 368 * memory pointer to be set to 0 as the very first item of business 369 * or else they will not stream. So we do that immediately. 370 */ 371 err_code = zero_the_pointer(gspca_dev); 372 if (err_code < 0) 373 return err_code; 374 375 err_code = stream_start(gspca_dev); 376 if (err_code < 0) 377 return err_code; 378 379 /* Now, the query for sensor type. */ 380 err_code = cam_get_response16(gspca_dev, 0x07, 1); 381 if (err_code < 0) 382 return err_code; 383 384 if (id->idProduct == 0x0110 || id->idProduct == 0x010e) { 385 sd->cam_type = CAM_TYPE_CIF; 386 cam->nmodes--; 387 /* 388 * All but one of the known CIF cameras share the same USB ID, 389 * but two different init routines are in use, and the control 390 * settings are different, too. We need to detect which camera 391 * of the two known varieties is connected! 392 * 393 * A list of known CIF cameras follows. They all report either 394 * 0200 for type 0 or 0300 for type 1. 395 * If you have another to report, please do 396 * 397 * Name sd->sensor_type reported by 398 * 399 * Sakar 56379 Spy-shot 0 T. Kilgore 400 * Innovage 0 T. Kilgore 401 * Vivitar Mini 0 H. De Goede 402 * Vivitar Mini 0 E. Rodriguez 403 * Vivitar Mini 1 T. Kilgore 404 * Elta-Media 8212dc 1 T. Kaiser 405 * Philips dig. keych. 1 T. Kilgore 406 * Trust Spyc@m 100 1 A. Jacobs 407 */ 408 switch (gspca_dev->usb_buf[0]) { 409 case 2: 410 sd->sensor_type = 0; 411 break; 412 case 3: 413 sd->sensor_type = 1; 414 break; 415 default: 416 pr_err("Unknown CIF Sensor id : %02x\n", 417 gspca_dev->usb_buf[1]); 418 return -ENODEV; 419 } 420 PDEBUG(D_PROBE, "MR97310A CIF camera detected, sensor: %d", 421 sd->sensor_type); 422 } else { 423 sd->cam_type = CAM_TYPE_VGA; 424 425 /* 426 * Here is a table of the responses to the query for sensor 427 * type, from the known MR97310A VGA cameras. Six different 428 * cameras of which five share the same USB ID. 429 * 430 * Name gspca_dev->usb_buf[] sd->sensor_type 431 * sd->do_lcd_stop 432 * Aiptek Pencam VGA+ 0300 0 1 433 * ION digital 0300 0 1 434 * Argus DC-1620 0450 1 0 435 * Argus QuickClix 0420 1 1 436 * Sakar 77379 Digital 0350 0 1 437 * Sakar 1638x CyberPix 0120 0 2 438 * 439 * Based upon these results, we assume default settings 440 * and then correct as necessary, as follows. 441 * 442 */ 443 444 sd->sensor_type = 1; 445 sd->do_lcd_stop = 0; 446 sd->adj_colors = 0; 447 if (gspca_dev->usb_buf[0] == 0x01) { 448 sd->sensor_type = 2; 449 } else if ((gspca_dev->usb_buf[0] != 0x03) && 450 (gspca_dev->usb_buf[0] != 0x04)) { 451 pr_err("Unknown VGA Sensor id Byte 0: %02x\n", 452 gspca_dev->usb_buf[0]); 453 pr_err("Defaults assumed, may not work\n"); 454 pr_err("Please report this\n"); 455 } 456 /* Sakar Digital color needs to be adjusted. */ 457 if ((gspca_dev->usb_buf[0] == 0x03) && 458 (gspca_dev->usb_buf[1] == 0x50)) 459 sd->adj_colors = 1; 460 if (gspca_dev->usb_buf[0] == 0x04) { 461 sd->do_lcd_stop = 1; 462 switch (gspca_dev->usb_buf[1]) { 463 case 0x50: 464 sd->sensor_type = 0; 465 PDEBUG(D_PROBE, "sensor_type corrected to 0"); 466 break; 467 case 0x20: 468 /* Nothing to do here. */ 469 break; 470 default: 471 pr_err("Unknown VGA Sensor id Byte 1: %02x\n", 472 gspca_dev->usb_buf[1]); 473 pr_err("Defaults assumed, may not work\n"); 474 pr_err("Please report this\n"); 475 } 476 } 477 PDEBUG(D_PROBE, "MR97310A VGA camera detected, sensor: %d", 478 sd->sensor_type); 479 } 480 /* Stop streaming as we've started it only to probe the sensor type. */ 481 sd_stopN(gspca_dev); 482 483 if (force_sensor_type != -1) { 484 sd->sensor_type = !!force_sensor_type; 485 PDEBUG(D_PROBE, "Forcing sensor type to: %d", 486 sd->sensor_type); 487 } 488 489 return 0; 490 } 491 492 /* this function is called at probe and resume time */ 493 static int sd_init(struct gspca_dev *gspca_dev) 494 { 495 return 0; 496 } 497 498 static int start_cif_cam(struct gspca_dev *gspca_dev) 499 { 500 struct sd *sd = (struct sd *) gspca_dev; 501 __u8 *data = gspca_dev->usb_buf; 502 int err_code; 503 static const __u8 startup_string[] = { 504 0x00, 505 0x0d, 506 0x01, 507 0x00, /* Hsize/8 for 352 or 320 */ 508 0x00, /* Vsize/4 for 288 or 240 */ 509 0x13, /* or 0xbb, depends on sensor */ 510 0x00, /* Hstart, depends on res. */ 511 0x00, /* reserved ? */ 512 0x00, /* Vstart, depends on res. and sensor */ 513 0x50, /* 0x54 to get 176 or 160 */ 514 0xc0 515 }; 516 517 /* Note: Some of the above descriptions guessed from MR97113A driver */ 518 519 memcpy(data, startup_string, 11); 520 if (sd->sensor_type) 521 data[5] = 0xbb; 522 523 switch (gspca_dev->pixfmt.width) { 524 case 160: 525 data[9] |= 0x04; /* reg 8, 2:1 scale down from 320 */ 526 /* fall thru */ 527 case 320: 528 default: 529 data[3] = 0x28; /* reg 2, H size/8 */ 530 data[4] = 0x3c; /* reg 3, V size/4 */ 531 data[6] = 0x14; /* reg 5, H start */ 532 data[8] = 0x1a + sd->sensor_type; /* reg 7, V start */ 533 break; 534 case 176: 535 data[9] |= 0x04; /* reg 8, 2:1 scale down from 352 */ 536 /* fall thru */ 537 case 352: 538 data[3] = 0x2c; /* reg 2, H size/8 */ 539 data[4] = 0x48; /* reg 3, V size/4 */ 540 data[6] = 0x06; /* reg 5, H start */ 541 data[8] = 0x06 - sd->sensor_type; /* reg 7, V start */ 542 break; 543 } 544 err_code = mr_write(gspca_dev, 11); 545 if (err_code < 0) 546 return err_code; 547 548 if (!sd->sensor_type) { 549 static const struct sensor_w_data cif_sensor0_init_data[] = { 550 {0x02, 0x00, {0x03, 0x5a, 0xb5, 0x01, 551 0x0f, 0x14, 0x0f, 0x10}, 8}, 552 {0x0c, 0x00, {0x04, 0x01, 0x01, 0x00, 0x1f}, 5}, 553 {0x12, 0x00, {0x07}, 1}, 554 {0x1f, 0x00, {0x06}, 1}, 555 {0x27, 0x00, {0x04}, 1}, 556 {0x29, 0x00, {0x0c}, 1}, 557 {0x40, 0x00, {0x40, 0x00, 0x04}, 3}, 558 {0x50, 0x00, {0x60}, 1}, 559 {0x60, 0x00, {0x06}, 1}, 560 {0x6b, 0x00, {0x85, 0x85, 0xc8, 0xc8, 0xc8, 0xc8}, 6}, 561 {0x72, 0x00, {0x1e, 0x56}, 2}, 562 {0x75, 0x00, {0x58, 0x40, 0xa2, 0x02, 0x31, 0x02, 563 0x31, 0x80, 0x00}, 9}, 564 {0x11, 0x00, {0x01}, 1}, 565 {0, 0, {0}, 0} 566 }; 567 err_code = sensor_write_regs(gspca_dev, cif_sensor0_init_data, 568 ARRAY_SIZE(cif_sensor0_init_data)); 569 } else { /* sd->sensor_type = 1 */ 570 static const struct sensor_w_data cif_sensor1_init_data[] = { 571 /* Reg 3,4, 7,8 get set by the controls */ 572 {0x02, 0x00, {0x10}, 1}, 573 {0x05, 0x01, {0x22}, 1}, /* 5/6 also seen as 65h/32h */ 574 {0x06, 0x01, {0x00}, 1}, 575 {0x09, 0x02, {0x0e}, 1}, 576 {0x0a, 0x02, {0x05}, 1}, 577 {0x0b, 0x02, {0x05}, 1}, 578 {0x0c, 0x02, {0x0f}, 1}, 579 {0x0d, 0x02, {0x07}, 1}, 580 {0x0e, 0x02, {0x0c}, 1}, 581 {0x0f, 0x00, {0x00}, 1}, 582 {0x10, 0x00, {0x06}, 1}, 583 {0x11, 0x00, {0x07}, 1}, 584 {0x12, 0x00, {0x00}, 1}, 585 {0x13, 0x00, {0x01}, 1}, 586 {0, 0, {0}, 0} 587 }; 588 /* Without this command the cam won't work with USB-UHCI */ 589 gspca_dev->usb_buf[0] = 0x0a; 590 gspca_dev->usb_buf[1] = 0x00; 591 err_code = mr_write(gspca_dev, 2); 592 if (err_code < 0) 593 return err_code; 594 err_code = sensor_write_regs(gspca_dev, cif_sensor1_init_data, 595 ARRAY_SIZE(cif_sensor1_init_data)); 596 } 597 return err_code; 598 } 599 600 static int start_vga_cam(struct gspca_dev *gspca_dev) 601 { 602 struct sd *sd = (struct sd *) gspca_dev; 603 __u8 *data = gspca_dev->usb_buf; 604 int err_code; 605 static const __u8 startup_string[] = 606 {0x00, 0x0d, 0x01, 0x00, 0x00, 0x2b, 0x00, 0x00, 607 0x00, 0x50, 0xc0}; 608 /* What some of these mean is explained in start_cif_cam(), above */ 609 610 memcpy(data, startup_string, 11); 611 if (!sd->sensor_type) { 612 data[5] = 0x00; 613 data[10] = 0x91; 614 } 615 if (sd->sensor_type == 2) { 616 data[5] = 0x00; 617 data[10] = 0x18; 618 } 619 620 switch (gspca_dev->pixfmt.width) { 621 case 160: 622 data[9] |= 0x0c; /* reg 8, 4:1 scale down */ 623 /* fall thru */ 624 case 320: 625 data[9] |= 0x04; /* reg 8, 2:1 scale down */ 626 /* fall thru */ 627 case 640: 628 default: 629 data[3] = 0x50; /* reg 2, H size/8 */ 630 data[4] = 0x78; /* reg 3, V size/4 */ 631 data[6] = 0x04; /* reg 5, H start */ 632 data[8] = 0x03; /* reg 7, V start */ 633 if (sd->sensor_type == 2) { 634 data[6] = 2; 635 data[8] = 1; 636 } 637 if (sd->do_lcd_stop) 638 data[8] = 0x04; /* Bayer tile shifted */ 639 break; 640 641 case 176: 642 data[9] |= 0x04; /* reg 8, 2:1 scale down */ 643 /* fall thru */ 644 case 352: 645 data[3] = 0x2c; /* reg 2, H size */ 646 data[4] = 0x48; /* reg 3, V size */ 647 data[6] = 0x94; /* reg 5, H start */ 648 data[8] = 0x63; /* reg 7, V start */ 649 if (sd->do_lcd_stop) 650 data[8] = 0x64; /* Bayer tile shifted */ 651 break; 652 } 653 654 err_code = mr_write(gspca_dev, 11); 655 if (err_code < 0) 656 return err_code; 657 658 if (!sd->sensor_type) { 659 static const struct sensor_w_data vga_sensor0_init_data[] = { 660 {0x01, 0x00, {0x0c, 0x00, 0x04}, 3}, 661 {0x14, 0x00, {0x01, 0xe4, 0x02, 0x84}, 4}, 662 {0x20, 0x00, {0x00, 0x80, 0x00, 0x08}, 4}, 663 {0x25, 0x00, {0x03, 0xa9, 0x80}, 3}, 664 {0x30, 0x00, {0x30, 0x18, 0x10, 0x18}, 4}, 665 {0, 0, {0}, 0} 666 }; 667 err_code = sensor_write_regs(gspca_dev, vga_sensor0_init_data, 668 ARRAY_SIZE(vga_sensor0_init_data)); 669 } else if (sd->sensor_type == 1) { 670 static const struct sensor_w_data color_adj[] = { 671 {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00, 672 /* adjusted blue, green, red gain correct 673 too much blue from the Sakar Digital */ 674 0x05, 0x01, 0x04}, 8} 675 }; 676 677 static const struct sensor_w_data color_no_adj[] = { 678 {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00, 679 /* default blue, green, red gain settings */ 680 0x07, 0x00, 0x01}, 8} 681 }; 682 683 static const struct sensor_w_data vga_sensor1_init_data[] = { 684 {0x11, 0x04, {0x01}, 1}, 685 {0x0a, 0x00, {0x00, 0x01, 0x00, 0x00, 0x01, 686 /* These settings may be better for some cameras */ 687 /* {0x0a, 0x00, {0x01, 0x06, 0x00, 0x00, 0x01, */ 688 0x00, 0x0a}, 7}, 689 {0x11, 0x04, {0x01}, 1}, 690 {0x12, 0x00, {0x00, 0x63, 0x00, 0x70, 0x00, 0x00}, 6}, 691 {0x11, 0x04, {0x01}, 1}, 692 {0, 0, {0}, 0} 693 }; 694 695 if (sd->adj_colors) 696 err_code = sensor_write_regs(gspca_dev, color_adj, 697 ARRAY_SIZE(color_adj)); 698 else 699 err_code = sensor_write_regs(gspca_dev, color_no_adj, 700 ARRAY_SIZE(color_no_adj)); 701 702 if (err_code < 0) 703 return err_code; 704 705 err_code = sensor_write_regs(gspca_dev, vga_sensor1_init_data, 706 ARRAY_SIZE(vga_sensor1_init_data)); 707 } else { /* sensor type == 2 */ 708 static const struct sensor_w_data vga_sensor2_init_data[] = { 709 710 {0x01, 0x00, {0x48}, 1}, 711 {0x02, 0x00, {0x22}, 1}, 712 /* Reg 3 msb and 4 is lsb of the exposure setting*/ 713 {0x05, 0x00, {0x10}, 1}, 714 {0x06, 0x00, {0x00}, 1}, 715 {0x07, 0x00, {0x00}, 1}, 716 {0x08, 0x00, {0x00}, 1}, 717 {0x09, 0x00, {0x00}, 1}, 718 /* The following are used in the gain control 719 * which is BTW completely borked in the OEM driver 720 * The values for each color go from 0 to 0x7ff 721 *{0x0a, 0x00, {0x01}, 1}, green1 gain msb 722 *{0x0b, 0x00, {0x10}, 1}, green1 gain lsb 723 *{0x0c, 0x00, {0x01}, 1}, red gain msb 724 *{0x0d, 0x00, {0x10}, 1}, red gain lsb 725 *{0x0e, 0x00, {0x01}, 1}, blue gain msb 726 *{0x0f, 0x00, {0x10}, 1}, blue gain lsb 727 *{0x10, 0x00, {0x01}, 1}, green2 gain msb 728 *{0x11, 0x00, {0x10}, 1}, green2 gain lsb 729 */ 730 {0x12, 0x00, {0x00}, 1}, 731 {0x13, 0x00, {0x04}, 1}, /* weird effect on colors */ 732 {0x14, 0x00, {0x00}, 1}, 733 {0x15, 0x00, {0x06}, 1}, 734 {0x16, 0x00, {0x01}, 1}, 735 {0x17, 0x00, {0xe2}, 1}, /* vertical alignment */ 736 {0x18, 0x00, {0x02}, 1}, 737 {0x19, 0x00, {0x82}, 1}, /* don't mess with */ 738 {0x1a, 0x00, {0x00}, 1}, 739 {0x1b, 0x00, {0x20}, 1}, 740 /* {0x1c, 0x00, {0x17}, 1}, contrast control */ 741 {0x1d, 0x00, {0x80}, 1}, /* moving causes a mess */ 742 {0x1e, 0x00, {0x08}, 1}, /* moving jams the camera */ 743 {0x1f, 0x00, {0x0c}, 1}, 744 {0x20, 0x00, {0x00}, 1}, 745 {0, 0, {0}, 0} 746 }; 747 err_code = sensor_write_regs(gspca_dev, vga_sensor2_init_data, 748 ARRAY_SIZE(vga_sensor2_init_data)); 749 } 750 return err_code; 751 } 752 753 static int sd_start(struct gspca_dev *gspca_dev) 754 { 755 struct sd *sd = (struct sd *) gspca_dev; 756 int err_code; 757 758 sd->sof_read = 0; 759 760 /* Some of the VGA cameras require the memory pointer 761 * to be set to 0 again. We have been forced to start the 762 * stream in sd_config() to detect the hardware, and closed it. 763 * Thus, we need here to do a completely fresh and clean start. */ 764 err_code = zero_the_pointer(gspca_dev); 765 if (err_code < 0) 766 return err_code; 767 768 err_code = stream_start(gspca_dev); 769 if (err_code < 0) 770 return err_code; 771 772 if (sd->cam_type == CAM_TYPE_CIF) { 773 err_code = start_cif_cam(gspca_dev); 774 } else { 775 err_code = start_vga_cam(gspca_dev); 776 } 777 if (err_code < 0) 778 return err_code; 779 780 return isoc_enable(gspca_dev); 781 } 782 783 static void sd_stopN(struct gspca_dev *gspca_dev) 784 { 785 struct sd *sd = (struct sd *) gspca_dev; 786 787 stream_stop(gspca_dev); 788 /* Not all the cams need this, but even if not, probably a good idea */ 789 zero_the_pointer(gspca_dev); 790 if (sd->do_lcd_stop) 791 lcd_stop(gspca_dev); 792 } 793 794 static void setbrightness(struct gspca_dev *gspca_dev, s32 val) 795 { 796 struct sd *sd = (struct sd *) gspca_dev; 797 u8 sign_reg = 7; /* This reg and the next one used on CIF cams. */ 798 u8 value_reg = 8; /* VGA cams seem to use regs 0x0b and 0x0c */ 799 static const u8 quick_clix_table[] = 800 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ 801 { 0, 4, 8, 12, 1, 2, 3, 5, 6, 9, 7, 10, 13, 11, 14, 15}; 802 if (sd->cam_type == CAM_TYPE_VGA) { 803 sign_reg += 4; 804 value_reg += 4; 805 } 806 807 /* Note register 7 is also seen as 0x8x or 0xCx in some dumps */ 808 if (val > 0) { 809 sensor_write1(gspca_dev, sign_reg, 0x00); 810 } else { 811 sensor_write1(gspca_dev, sign_reg, 0x01); 812 val = 257 - val; 813 } 814 /* Use lookup table for funky Argus QuickClix brightness */ 815 if (sd->do_lcd_stop) 816 val = quick_clix_table[val]; 817 818 sensor_write1(gspca_dev, value_reg, val); 819 } 820 821 static void setexposure(struct gspca_dev *gspca_dev, s32 expo, s32 min_clockdiv) 822 { 823 struct sd *sd = (struct sd *) gspca_dev; 824 int exposure = MR97310A_EXPOSURE_DEFAULT; 825 u8 buf[2]; 826 827 if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1) { 828 /* This cam does not like exposure settings < 300, 829 so scale 0 - 4095 to 300 - 4095 */ 830 exposure = (expo * 9267) / 10000 + 300; 831 sensor_write1(gspca_dev, 3, exposure >> 4); 832 sensor_write1(gspca_dev, 4, exposure & 0x0f); 833 } else if (sd->sensor_type == 2) { 834 exposure = expo; 835 exposure >>= 3; 836 sensor_write1(gspca_dev, 3, exposure >> 8); 837 sensor_write1(gspca_dev, 4, exposure & 0xff); 838 } else { 839 /* We have both a clock divider and an exposure register. 840 We first calculate the clock divider, as that determines 841 the maximum exposure and then we calculate the exposure 842 register setting (which goes from 0 - 511). 843 844 Note our 0 - 4095 exposure is mapped to 0 - 511 845 milliseconds exposure time */ 846 u8 clockdiv = (60 * expo + 7999) / 8000; 847 848 /* Limit framerate to not exceed usb bandwidth */ 849 if (clockdiv < min_clockdiv && gspca_dev->pixfmt.width >= 320) 850 clockdiv = min_clockdiv; 851 else if (clockdiv < 2) 852 clockdiv = 2; 853 854 if (sd->cam_type == CAM_TYPE_VGA && clockdiv < 4) 855 clockdiv = 4; 856 857 /* Frame exposure time in ms = 1000 * clockdiv / 60 -> 858 exposure = (sd->exposure / 8) * 511 / (1000 * clockdiv / 60) */ 859 exposure = (60 * 511 * expo) / (8000 * clockdiv); 860 if (exposure > 511) 861 exposure = 511; 862 863 /* exposure register value is reversed! */ 864 exposure = 511 - exposure; 865 866 buf[0] = exposure & 0xff; 867 buf[1] = exposure >> 8; 868 sensor_write_reg(gspca_dev, 0x0e, 0, buf, 2); 869 sensor_write1(gspca_dev, 0x02, clockdiv); 870 } 871 } 872 873 static void setgain(struct gspca_dev *gspca_dev, s32 val) 874 { 875 struct sd *sd = (struct sd *) gspca_dev; 876 u8 gainreg; 877 878 if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1) 879 sensor_write1(gspca_dev, 0x0e, val); 880 else if (sd->cam_type == CAM_TYPE_VGA && sd->sensor_type == 2) 881 for (gainreg = 0x0a; gainreg < 0x11; gainreg += 2) { 882 sensor_write1(gspca_dev, gainreg, val >> 8); 883 sensor_write1(gspca_dev, gainreg + 1, val & 0xff); 884 } 885 else 886 sensor_write1(gspca_dev, 0x10, val); 887 } 888 889 static void setcontrast(struct gspca_dev *gspca_dev, s32 val) 890 { 891 sensor_write1(gspca_dev, 0x1c, val); 892 } 893 894 static int sd_s_ctrl(struct v4l2_ctrl *ctrl) 895 { 896 struct gspca_dev *gspca_dev = 897 container_of(ctrl->handler, struct gspca_dev, ctrl_handler); 898 struct sd *sd = (struct sd *)gspca_dev; 899 900 gspca_dev->usb_err = 0; 901 902 if (!gspca_dev->streaming) 903 return 0; 904 905 switch (ctrl->id) { 906 case V4L2_CID_BRIGHTNESS: 907 setbrightness(gspca_dev, ctrl->val); 908 break; 909 case V4L2_CID_CONTRAST: 910 setcontrast(gspca_dev, ctrl->val); 911 break; 912 case V4L2_CID_EXPOSURE: 913 setexposure(gspca_dev, sd->exposure->val, 914 sd->min_clockdiv ? sd->min_clockdiv->val : 0); 915 break; 916 case V4L2_CID_GAIN: 917 setgain(gspca_dev, ctrl->val); 918 break; 919 } 920 return gspca_dev->usb_err; 921 } 922 923 static const struct v4l2_ctrl_ops sd_ctrl_ops = { 924 .s_ctrl = sd_s_ctrl, 925 }; 926 927 static int sd_init_controls(struct gspca_dev *gspca_dev) 928 { 929 struct sd *sd = (struct sd *)gspca_dev; 930 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler; 931 static const struct v4l2_ctrl_config clockdiv = { 932 .ops = &sd_ctrl_ops, 933 .id = MR97310A_CID_CLOCKDIV, 934 .type = V4L2_CTRL_TYPE_INTEGER, 935 .name = "Minimum Clock Divider", 936 .min = MR97310A_MIN_CLOCKDIV_MIN, 937 .max = MR97310A_MIN_CLOCKDIV_MAX, 938 .step = 1, 939 .def = MR97310A_MIN_CLOCKDIV_DEFAULT, 940 }; 941 bool has_brightness = false; 942 bool has_argus_brightness = false; 943 bool has_contrast = false; 944 bool has_gain = false; 945 bool has_cs_gain = false; 946 bool has_exposure = false; 947 bool has_clockdiv = false; 948 949 gspca_dev->vdev.ctrl_handler = hdl; 950 v4l2_ctrl_handler_init(hdl, 4); 951 952 /* Setup controls depending on camera type */ 953 if (sd->cam_type == CAM_TYPE_CIF) { 954 /* No brightness for sensor_type 0 */ 955 if (sd->sensor_type == 0) 956 has_exposure = has_gain = has_clockdiv = true; 957 else 958 has_exposure = has_gain = has_brightness = true; 959 } else { 960 /* All controls need to be disabled if VGA sensor_type is 0 */ 961 if (sd->sensor_type == 0) 962 ; /* no controls! */ 963 else if (sd->sensor_type == 2) 964 has_exposure = has_cs_gain = has_contrast = true; 965 else if (sd->do_lcd_stop) 966 has_exposure = has_gain = has_argus_brightness = 967 has_clockdiv = true; 968 else 969 has_exposure = has_gain = has_brightness = 970 has_clockdiv = true; 971 } 972 973 /* Separate brightness control description for Argus QuickClix as it has 974 * different limits from the other mr97310a cameras, and separate gain 975 * control for Sakar CyberPix camera. */ 976 /* 977 * This control is disabled for CIF type 1 and VGA type 0 cameras. 978 * It does not quite act linearly for the Argus QuickClix camera, 979 * but it does control brightness. The values are 0 - 15 only, and 980 * the table above makes them act consecutively. 981 */ 982 if (has_brightness) 983 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, 984 V4L2_CID_BRIGHTNESS, -254, 255, 1, 985 MR97310A_BRIGHTNESS_DEFAULT); 986 else if (has_argus_brightness) 987 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, 988 V4L2_CID_BRIGHTNESS, 0, 15, 1, 989 MR97310A_BRIGHTNESS_DEFAULT); 990 if (has_contrast) 991 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, 992 V4L2_CID_CONTRAST, MR97310A_CONTRAST_MIN, 993 MR97310A_CONTRAST_MAX, 1, MR97310A_CONTRAST_DEFAULT); 994 if (has_gain) 995 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, 996 V4L2_CID_GAIN, MR97310A_GAIN_MIN, MR97310A_GAIN_MAX, 997 1, MR97310A_GAIN_DEFAULT); 998 else if (has_cs_gain) 999 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, V4L2_CID_GAIN, 1000 MR97310A_CS_GAIN_MIN, MR97310A_CS_GAIN_MAX, 1001 1, MR97310A_CS_GAIN_DEFAULT); 1002 if (has_exposure) 1003 sd->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, 1004 V4L2_CID_EXPOSURE, MR97310A_EXPOSURE_MIN, 1005 MR97310A_EXPOSURE_MAX, 1, MR97310A_EXPOSURE_DEFAULT); 1006 if (has_clockdiv) 1007 sd->min_clockdiv = v4l2_ctrl_new_custom(hdl, &clockdiv, NULL); 1008 1009 if (hdl->error) { 1010 pr_err("Could not initialize controls\n"); 1011 return hdl->error; 1012 } 1013 if (has_exposure && has_clockdiv) 1014 v4l2_ctrl_cluster(2, &sd->exposure); 1015 return 0; 1016 } 1017 1018 /* Include pac common sof detection functions */ 1019 #include "pac_common.h" 1020 1021 static void sd_pkt_scan(struct gspca_dev *gspca_dev, 1022 u8 *data, /* isoc packet */ 1023 int len) /* iso packet length */ 1024 { 1025 struct sd *sd = (struct sd *) gspca_dev; 1026 unsigned char *sof; 1027 1028 sof = pac_find_sof(gspca_dev, &sd->sof_read, data, len); 1029 if (sof) { 1030 int n; 1031 1032 /* finish decoding current frame */ 1033 n = sof - data; 1034 if (n > sizeof pac_sof_marker) 1035 n -= sizeof pac_sof_marker; 1036 else 1037 n = 0; 1038 gspca_frame_add(gspca_dev, LAST_PACKET, 1039 data, n); 1040 /* Start next frame. */ 1041 gspca_frame_add(gspca_dev, FIRST_PACKET, 1042 pac_sof_marker, sizeof pac_sof_marker); 1043 len -= sof - data; 1044 data = sof; 1045 } 1046 gspca_frame_add(gspca_dev, INTER_PACKET, data, len); 1047 } 1048 1049 /* sub-driver description */ 1050 static const struct sd_desc sd_desc = { 1051 .name = MODULE_NAME, 1052 .config = sd_config, 1053 .init = sd_init, 1054 .init_controls = sd_init_controls, 1055 .start = sd_start, 1056 .stopN = sd_stopN, 1057 .pkt_scan = sd_pkt_scan, 1058 }; 1059 1060 /* -- module initialisation -- */ 1061 static const struct usb_device_id device_table[] = { 1062 {USB_DEVICE(0x08ca, 0x0110)}, /* Trust Spyc@m 100 */ 1063 {USB_DEVICE(0x08ca, 0x0111)}, /* Aiptek Pencam VGA+ */ 1064 {USB_DEVICE(0x093a, 0x010f)}, /* All other known MR97310A VGA cams */ 1065 {USB_DEVICE(0x093a, 0x010e)}, /* All known MR97310A CIF cams */ 1066 {} 1067 }; 1068 MODULE_DEVICE_TABLE(usb, device_table); 1069 1070 /* -- device connect -- */ 1071 static int sd_probe(struct usb_interface *intf, 1072 const struct usb_device_id *id) 1073 { 1074 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd), 1075 THIS_MODULE); 1076 } 1077 1078 static struct usb_driver sd_driver = { 1079 .name = MODULE_NAME, 1080 .id_table = device_table, 1081 .probe = sd_probe, 1082 .disconnect = gspca_disconnect, 1083 #ifdef CONFIG_PM 1084 .suspend = gspca_suspend, 1085 .resume = gspca_resume, 1086 .reset_resume = gspca_resume, 1087 #endif 1088 }; 1089 1090 module_usb_driver(sd_driver); 1091