2 * Mars MR97310A library
4 * The original mr97310a driver, which supported the Aiptek Pencam VGA+, is
5 * Copyright (C) 2009 Kyle Guinn <elyk03@gmail.com>
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>
11 * Support for the control settings for the CIF cameras is
12 * Copyright (C) 2009 Hans de Goede <hdgoede@redhat.com> and
13 * Thomas Kaiser <thomas@kaiser-linux.li>
15 * Support for the control settings for the VGA cameras is
16 * Copyright (C) 2009 Theodore Kilgore <kilgota@auburn.edu>
18 * Several previously unsupported cameras are owned and have been tested by
19 * Hans de Goede <hdgoede@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>
25 * The MR97311A support in gspca/mars.c has been helpful in understanding some
26 * of the registers in these cameras.
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
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.
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
43 #define MODULE_NAME "mr97310a"
47 #define CAM_TYPE_CIF 0
48 #define CAM_TYPE_VGA 1
50 #define MR97310A_BRIGHTNESS_DEFAULT 0
52 #define MR97310A_EXPOSURE_MIN 0
53 #define MR97310A_EXPOSURE_MAX 4095
54 #define MR97310A_EXPOSURE_DEFAULT 1000
56 #define MR97310A_GAIN_MIN 0
57 #define MR97310A_GAIN_MAX 31
58 #define MR97310A_GAIN_DEFAULT 25
60 #define MR97310A_MIN_CLOCKDIV_MIN 3
61 #define MR97310A_MIN_CLOCKDIV_MAX 8
62 #define MR97310A_MIN_CLOCKDIV_DEFAULT 3
64 MODULE_AUTHOR("Kyle Guinn <elyk03@gmail.com>,"
65 "Theodore Kilgore <kilgota@auburn.edu>");
66 MODULE_DESCRIPTION("GSPCA/Mars-Semi MR97310A USB Camera Driver");
67 MODULE_LICENSE("GPL");
69 /* global parameters */
70 static int force_sensor_type = -1;
71 module_param(force_sensor_type, int, 0644);
72 MODULE_PARM_DESC(force_sensor_type, "Force sensor type (-1 (auto), 0 or 1)");
74 /* specific webcam descriptor */
76 struct gspca_dev gspca_dev; /* !! must be the first item */
78 u8 cam_type; /* 0 is CIF and 1 is VGA */
79 u8 sensor_type; /* We use 0 and 1 here, too. */
89 struct sensor_w_data {
96 static void sd_stopN(struct gspca_dev *gspca_dev);
97 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
98 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
99 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
100 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
101 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
102 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
103 static int sd_setmin_clockdiv(struct gspca_dev *gspca_dev, __s32 val);
104 static int sd_getmin_clockdiv(struct gspca_dev *gspca_dev, __s32 *val);
105 static void setbrightness(struct gspca_dev *gspca_dev);
106 static void setexposure(struct gspca_dev *gspca_dev);
107 static void setgain(struct gspca_dev *gspca_dev);
109 /* V4L2 controls supported by the driver */
110 static const struct ctrl sd_ctrls[] = {
111 /* Separate brightness control description for Argus QuickClix as it has
112 different limits from the other mr97310a cameras */
114 #define NORM_BRIGHTNESS_IDX 0
116 .id = V4L2_CID_BRIGHTNESS,
117 .type = V4L2_CTRL_TYPE_INTEGER,
118 .name = "Brightness",
122 .default_value = MR97310A_BRIGHTNESS_DEFAULT,
125 .set = sd_setbrightness,
126 .get = sd_getbrightness,
129 #define ARGUS_QC_BRIGHTNESS_IDX 1
131 .id = V4L2_CID_BRIGHTNESS,
132 .type = V4L2_CTRL_TYPE_INTEGER,
133 .name = "Brightness",
137 .default_value = MR97310A_BRIGHTNESS_DEFAULT,
140 .set = sd_setbrightness,
141 .get = sd_getbrightness,
144 #define EXPOSURE_IDX 2
146 .id = V4L2_CID_EXPOSURE,
147 .type = V4L2_CTRL_TYPE_INTEGER,
149 .minimum = MR97310A_EXPOSURE_MIN,
150 .maximum = MR97310A_EXPOSURE_MAX,
152 .default_value = MR97310A_EXPOSURE_DEFAULT,
155 .set = sd_setexposure,
156 .get = sd_getexposure,
162 .type = V4L2_CTRL_TYPE_INTEGER,
164 .minimum = MR97310A_GAIN_MIN,
165 .maximum = MR97310A_GAIN_MAX,
167 .default_value = MR97310A_GAIN_DEFAULT,
174 #define MIN_CLOCKDIV_IDX 4
176 .id = V4L2_CID_PRIVATE_BASE,
177 .type = V4L2_CTRL_TYPE_INTEGER,
178 .name = "Minimum Clock Divider",
179 .minimum = MR97310A_MIN_CLOCKDIV_MIN,
180 .maximum = MR97310A_MIN_CLOCKDIV_MAX,
182 .default_value = MR97310A_MIN_CLOCKDIV_DEFAULT,
185 .set = sd_setmin_clockdiv,
186 .get = sd_getmin_clockdiv,
190 static const struct v4l2_pix_format vga_mode[] = {
191 {160, 120, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
193 .sizeimage = 160 * 120,
194 .colorspace = V4L2_COLORSPACE_SRGB,
196 {176, 144, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
198 .sizeimage = 176 * 144,
199 .colorspace = V4L2_COLORSPACE_SRGB,
201 {320, 240, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
203 .sizeimage = 320 * 240,
204 .colorspace = V4L2_COLORSPACE_SRGB,
206 {352, 288, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
208 .sizeimage = 352 * 288,
209 .colorspace = V4L2_COLORSPACE_SRGB,
211 {640, 480, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
213 .sizeimage = 640 * 480,
214 .colorspace = V4L2_COLORSPACE_SRGB,
218 /* the bytes to write are in gspca_dev->usb_buf */
219 static int mr_write(struct gspca_dev *gspca_dev, int len)
223 rc = usb_bulk_msg(gspca_dev->dev,
224 usb_sndbulkpipe(gspca_dev->dev, 4),
225 gspca_dev->usb_buf, len, NULL, 500);
227 PDEBUG(D_ERR, "reg write [%02x] error %d",
228 gspca_dev->usb_buf[0], rc);
232 /* the bytes are read into gspca_dev->usb_buf */
233 static int mr_read(struct gspca_dev *gspca_dev, int len)
237 rc = usb_bulk_msg(gspca_dev->dev,
238 usb_rcvbulkpipe(gspca_dev->dev, 3),
239 gspca_dev->usb_buf, len, NULL, 500);
241 PDEBUG(D_ERR, "reg read [%02x] error %d",
242 gspca_dev->usb_buf[0], rc);
246 static int sensor_write_reg(struct gspca_dev *gspca_dev, u8 reg, u8 flags,
247 const u8 *data, int len)
249 gspca_dev->usb_buf[0] = 0x1f;
250 gspca_dev->usb_buf[1] = flags;
251 gspca_dev->usb_buf[2] = reg;
252 memcpy(gspca_dev->usb_buf + 3, data, len);
254 return mr_write(gspca_dev, len + 3);
257 static int sensor_write_regs(struct gspca_dev *gspca_dev,
258 const struct sensor_w_data *data, int len)
262 for (i = 0; i < len; i++) {
263 rc = sensor_write_reg(gspca_dev, data[i].reg, data[i].flags,
264 data[i].data, data[i].len);
272 static int sensor_write1(struct gspca_dev *gspca_dev, u8 reg, u8 data)
274 struct sd *sd = (struct sd *) gspca_dev;
279 if (sd->cam_type == CAM_TYPE_CIF) {
280 rc = sensor_write_reg(gspca_dev, reg, 0x01, &buf, 1);
281 confirm_reg = sd->sensor_type ? 0x13 : 0x11;
283 rc = sensor_write_reg(gspca_dev, reg, 0x00, &buf, 1);
290 rc = sensor_write_reg(gspca_dev, confirm_reg, 0x00, &buf, 1);
297 static int cam_get_response16(struct gspca_dev *gspca_dev, u8 reg, int verbose)
301 gspca_dev->usb_buf[0] = reg;
302 err_code = mr_write(gspca_dev, 1);
306 err_code = mr_read(gspca_dev, 16);
311 PDEBUG(D_PROBE, "Register: %02x reads %02x%02x%02x", reg,
312 gspca_dev->usb_buf[0],
313 gspca_dev->usb_buf[1],
314 gspca_dev->usb_buf[2]);
319 static int zero_the_pointer(struct gspca_dev *gspca_dev)
321 __u8 *data = gspca_dev->usb_buf;
326 err_code = cam_get_response16(gspca_dev, 0x21, 0);
330 err_code = mr_write(gspca_dev, 1);
333 err_code = mr_write(gspca_dev, 2);
337 err_code = cam_get_response16(gspca_dev, 0x21, 0);
343 err_code = mr_write(gspca_dev, 2);
347 err_code = cam_get_response16(gspca_dev, 0x21, 0);
353 err_code = mr_write(gspca_dev, 2);
357 err_code = cam_get_response16(gspca_dev, 0x21, 0);
363 err_code = mr_write(gspca_dev, 2);
367 while (status != 0x0a && tries < 256) {
368 err_code = cam_get_response16(gspca_dev, 0x21, 0);
375 PDEBUG(D_ERR, "status is %02x", status);
381 err_code = mr_write(gspca_dev, 2);
385 err_code = cam_get_response16(gspca_dev, 0x21, 0);
393 err_code = mr_write(gspca_dev, 1);
397 err_code = mr_read(gspca_dev, 16);
404 static int stream_start(struct gspca_dev *gspca_dev)
406 gspca_dev->usb_buf[0] = 0x01;
407 gspca_dev->usb_buf[1] = 0x01;
408 return mr_write(gspca_dev, 2);
411 static void stream_stop(struct gspca_dev *gspca_dev)
413 gspca_dev->usb_buf[0] = 0x01;
414 gspca_dev->usb_buf[1] = 0x00;
415 if (mr_write(gspca_dev, 2) < 0)
416 PDEBUG(D_ERR, "Stream Stop failed");
419 static void lcd_stop(struct gspca_dev *gspca_dev)
421 gspca_dev->usb_buf[0] = 0x19;
422 gspca_dev->usb_buf[1] = 0x54;
423 if (mr_write(gspca_dev, 2) < 0)
424 PDEBUG(D_ERR, "LCD Stop failed");
427 static int isoc_enable(struct gspca_dev *gspca_dev)
429 gspca_dev->usb_buf[0] = 0x00;
430 gspca_dev->usb_buf[1] = 0x4d; /* ISOC transfering enable... */
431 return mr_write(gspca_dev, 2);
434 /* This function is called at probe time */
435 static int sd_config(struct gspca_dev *gspca_dev,
436 const struct usb_device_id *id)
438 struct sd *sd = (struct sd *) gspca_dev;
442 cam = &gspca_dev->cam;
443 cam->cam_mode = vga_mode;
444 cam->nmodes = ARRAY_SIZE(vga_mode);
447 /* Several of the supported CIF cameras share the same USB ID but
448 * require different initializations and different control settings.
449 * The same is true of the VGA cameras. Therefore, we are forced
450 * to start the initialization process in order to determine which
451 * camera is present. Some of the supported cameras require the
452 * memory pointer to be set to 0 as the very first item of business
453 * or else they will not stream. So we do that immediately.
455 err_code = zero_the_pointer(gspca_dev);
459 err_code = stream_start(gspca_dev);
463 if (id->idProduct == 0x0110 || id->idProduct == 0x010e) {
464 sd->cam_type = CAM_TYPE_CIF;
466 err_code = cam_get_response16(gspca_dev, 0x06, 1);
470 * All but one of the known CIF cameras share the same USB ID,
471 * but two different init routines are in use, and the control
472 * settings are different, too. We need to detect which camera
473 * of the two known varieties is connected!
475 * A list of known CIF cameras follows. They all report either
476 * 0002 for type 0 or 0003 for type 1.
477 * If you have another to report, please do
479 * Name sd->sensor_type reported by
481 * Sakar Spy-shot 0 T. Kilgore
482 * Innovage 0 T. Kilgore
483 * Vivitar Mini 0 H. De Goede
484 * Vivitar Mini 0 E. Rodriguez
485 * Vivitar Mini 1 T. Kilgore
486 * Elta-Media 8212dc 1 T. Kaiser
487 * Philips dig. keych. 1 T. Kilgore
488 * Trust Spyc@m 100 1 A. Jacobs
490 switch (gspca_dev->usb_buf[1]) {
498 PDEBUG(D_ERR, "Unknown CIF Sensor id : %02x",
499 gspca_dev->usb_buf[1]);
502 PDEBUG(D_PROBE, "MR97310A CIF camera detected, sensor: %d",
505 sd->cam_type = CAM_TYPE_VGA;
507 err_code = cam_get_response16(gspca_dev, 0x07, 1);
512 * Here is a table of the responses to the previous command
513 * from the known MR97310A VGA cameras.
515 * Name gspca_dev->usb_buf[] sd->sensor_type
517 * Aiptek Pencam VGA+ 0300 0 1
518 * ION digital 0350 0 1
519 * Argus DC-1620 0450 1 0
520 * Argus QuickClix 0420 1 1
522 * Based upon these results, we assume default settings
523 * and then correct as necessary, as follows.
530 if ((gspca_dev->usb_buf[0] != 0x03) &&
531 (gspca_dev->usb_buf[0] != 0x04)) {
532 PDEBUG(D_ERR, "Unknown VGA Sensor id Byte 0: %02x",
533 gspca_dev->usb_buf[1]);
534 PDEBUG(D_ERR, "Defaults assumed, may not work");
535 PDEBUG(D_ERR, "Please report this");
537 /* Sakar Digital color needs to be adjusted. */
538 if ((gspca_dev->usb_buf[0] == 0x03) &&
539 (gspca_dev->usb_buf[1] == 0x50))
541 if (gspca_dev->usb_buf[0] == 0x04) {
543 switch (gspca_dev->usb_buf[1]) {
546 PDEBUG(D_PROBE, "sensor_type corrected to 0");
549 /* Nothing to do here. */
553 "Unknown VGA Sensor id Byte 1: %02x",
554 gspca_dev->usb_buf[1]);
556 "Defaults assumed, may not work");
557 PDEBUG(D_ERR, "Please report this");
560 PDEBUG(D_PROBE, "MR97310A VGA camera detected, sensor: %d",
563 /* Stop streaming as we've started it to probe the sensor type. */
566 if (force_sensor_type != -1) {
567 sd->sensor_type = !!force_sensor_type;
568 PDEBUG(D_PROBE, "Forcing sensor type to: %d",
572 /* Setup controls depending on camera type */
573 if (sd->cam_type == CAM_TYPE_CIF) {
574 /* No brightness for sensor_type 0 */
575 if (sd->sensor_type == 0)
576 gspca_dev->ctrl_dis = (1 << NORM_BRIGHTNESS_IDX) |
577 (1 << ARGUS_QC_BRIGHTNESS_IDX);
579 gspca_dev->ctrl_dis = (1 << ARGUS_QC_BRIGHTNESS_IDX) |
580 (1 << MIN_CLOCKDIV_IDX);
582 /* All controls need to be disabled if VGA sensor_type is 0 */
583 if (sd->sensor_type == 0)
584 gspca_dev->ctrl_dis = (1 << NORM_BRIGHTNESS_IDX) |
585 (1 << ARGUS_QC_BRIGHTNESS_IDX) |
586 (1 << EXPOSURE_IDX) |
588 (1 << MIN_CLOCKDIV_IDX);
589 else if (sd->do_lcd_stop)
590 /* Argus QuickClix has different brightness limits */
591 gspca_dev->ctrl_dis = (1 << NORM_BRIGHTNESS_IDX);
593 gspca_dev->ctrl_dis = (1 << ARGUS_QC_BRIGHTNESS_IDX);
596 sd->brightness = MR97310A_BRIGHTNESS_DEFAULT;
597 sd->exposure = MR97310A_EXPOSURE_DEFAULT;
598 sd->gain = MR97310A_GAIN_DEFAULT;
599 sd->min_clockdiv = MR97310A_MIN_CLOCKDIV_DEFAULT;
604 /* this function is called at probe and resume time */
605 static int sd_init(struct gspca_dev *gspca_dev)
610 static int start_cif_cam(struct gspca_dev *gspca_dev)
612 struct sd *sd = (struct sd *) gspca_dev;
613 __u8 *data = gspca_dev->usb_buf;
615 const __u8 startup_string[] = {
619 0x00, /* Hsize/8 for 352 or 320 */
620 0x00, /* Vsize/4 for 288 or 240 */
621 0x13, /* or 0xbb, depends on sensor */
622 0x00, /* Hstart, depends on res. */
623 0x00, /* reserved ? */
624 0x00, /* Vstart, depends on res. and sensor */
625 0x50, /* 0x54 to get 176 or 160 */
629 /* Note: Some of the above descriptions guessed from MR97113A driver */
631 memcpy(data, startup_string, 11);
635 switch (gspca_dev->width) {
637 data[9] |= 0x04; /* reg 8, 2:1 scale down from 320 */
641 data[3] = 0x28; /* reg 2, H size/8 */
642 data[4] = 0x3c; /* reg 3, V size/4 */
643 data[6] = 0x14; /* reg 5, H start */
644 data[8] = 0x1a + sd->sensor_type; /* reg 7, V start */
647 data[9] |= 0x04; /* reg 8, 2:1 scale down from 352 */
650 data[3] = 0x2c; /* reg 2, H size/8 */
651 data[4] = 0x48; /* reg 3, V size/4 */
652 data[6] = 0x06; /* reg 5, H start */
653 data[8] = 0x06 - sd->sensor_type; /* reg 7, V start */
656 err_code = mr_write(gspca_dev, 11);
660 if (!sd->sensor_type) {
661 const struct sensor_w_data cif_sensor0_init_data[] = {
662 {0x02, 0x00, {0x03, 0x5a, 0xb5, 0x01,
663 0x0f, 0x14, 0x0f, 0x10}, 8},
664 {0x0c, 0x00, {0x04, 0x01, 0x01, 0x00, 0x1f}, 5},
665 {0x12, 0x00, {0x07}, 1},
666 {0x1f, 0x00, {0x06}, 1},
667 {0x27, 0x00, {0x04}, 1},
668 {0x29, 0x00, {0x0c}, 1},
669 {0x40, 0x00, {0x40, 0x00, 0x04}, 3},
670 {0x50, 0x00, {0x60}, 1},
671 {0x60, 0x00, {0x06}, 1},
672 {0x6b, 0x00, {0x85, 0x85, 0xc8, 0xc8, 0xc8, 0xc8}, 6},
673 {0x72, 0x00, {0x1e, 0x56}, 2},
674 {0x75, 0x00, {0x58, 0x40, 0xa2, 0x02, 0x31, 0x02,
675 0x31, 0x80, 0x00}, 9},
676 {0x11, 0x00, {0x01}, 1},
679 err_code = sensor_write_regs(gspca_dev, cif_sensor0_init_data,
680 ARRAY_SIZE(cif_sensor0_init_data));
681 } else { /* sd->sensor_type = 1 */
682 const struct sensor_w_data cif_sensor1_init_data[] = {
683 /* Reg 3,4, 7,8 get set by the controls */
684 {0x02, 0x00, {0x10}, 1},
685 {0x05, 0x01, {0x22}, 1}, /* 5/6 also seen as 65h/32h */
686 {0x06, 0x01, {0x00}, 1},
687 {0x09, 0x02, {0x0e}, 1},
688 {0x0a, 0x02, {0x05}, 1},
689 {0x0b, 0x02, {0x05}, 1},
690 {0x0c, 0x02, {0x0f}, 1},
691 {0x0d, 0x02, {0x07}, 1},
692 {0x0e, 0x02, {0x0c}, 1},
693 {0x0f, 0x00, {0x00}, 1},
694 {0x10, 0x00, {0x06}, 1},
695 {0x11, 0x00, {0x07}, 1},
696 {0x12, 0x00, {0x00}, 1},
697 {0x13, 0x00, {0x01}, 1},
700 /* Without this command the cam won't work with USB-UHCI */
701 gspca_dev->usb_buf[0] = 0x0a;
702 gspca_dev->usb_buf[1] = 0x00;
703 err_code = mr_write(gspca_dev, 2);
706 err_code = sensor_write_regs(gspca_dev, cif_sensor1_init_data,
707 ARRAY_SIZE(cif_sensor1_init_data));
712 static int start_vga_cam(struct gspca_dev *gspca_dev)
714 struct sd *sd = (struct sd *) gspca_dev;
715 __u8 *data = gspca_dev->usb_buf;
717 const __u8 startup_string[] = {0x00, 0x0d, 0x01, 0x00, 0x00, 0x2b,
718 0x00, 0x00, 0x00, 0x50, 0xc0};
719 /* What some of these mean is explained in start_cif_cam(), above */
721 memcpy(data, startup_string, 11);
722 if (!sd->sensor_type) {
727 switch (gspca_dev->width) {
729 data[9] |= 0x0c; /* reg 8, 4:1 scale down */
732 data[9] |= 0x04; /* reg 8, 2:1 scale down */
736 data[3] = 0x50; /* reg 2, H size/8 */
737 data[4] = 0x78; /* reg 3, V size/4 */
738 data[6] = 0x04; /* reg 5, H start */
739 data[8] = 0x03; /* reg 7, V start */
741 data[8] = 0x04; /* Bayer tile shifted */
745 data[9] |= 0x04; /* reg 8, 2:1 scale down */
748 data[3] = 0x2c; /* reg 2, H size */
749 data[4] = 0x48; /* reg 3, V size */
750 data[6] = 0x94; /* reg 5, H start */
751 data[8] = 0x63; /* reg 7, V start */
753 data[8] = 0x64; /* Bayer tile shifted */
757 err_code = mr_write(gspca_dev, 11);
761 if (!sd->sensor_type) {
762 /* The only known sensor_type 0 cam is the Argus DC-1620 */
763 const struct sensor_w_data vga_sensor0_init_data[] = {
764 {0x01, 0x00, {0x0c, 0x00, 0x04}, 3},
765 {0x14, 0x00, {0x01, 0xe4, 0x02, 0x84}, 4},
766 {0x20, 0x00, {0x00, 0x80, 0x00, 0x08}, 4},
767 {0x25, 0x00, {0x03, 0xa9, 0x80}, 3},
768 {0x30, 0x00, {0x30, 0x18, 0x10, 0x18}, 4},
771 err_code = sensor_write_regs(gspca_dev, vga_sensor0_init_data,
772 ARRAY_SIZE(vga_sensor0_init_data));
773 } else { /* sd->sensor_type = 1 */
774 const struct sensor_w_data color_adj[] = {
775 {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
776 /* adjusted blue, green, red gain correct
777 too much blue from the Sakar Digital */
778 0x05, 0x01, 0x04}, 8}
781 const struct sensor_w_data color_no_adj[] = {
782 {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
783 /* default blue, green, red gain settings */
784 0x07, 0x00, 0x01}, 8}
787 const struct sensor_w_data vga_sensor1_init_data[] = {
788 {0x11, 0x04, {0x01}, 1},
789 {0x0a, 0x00, {0x00, 0x01, 0x00, 0x00, 0x01,
790 /* These settings may be better for some cameras */
791 /* {0x0a, 0x00, {0x01, 0x06, 0x00, 0x00, 0x01, */
793 {0x11, 0x04, {0x01}, 1},
794 {0x12, 0x00, {0x00, 0x63, 0x00, 0x70, 0x00, 0x00}, 6},
795 {0x11, 0x04, {0x01}, 1},
800 err_code = sensor_write_regs(gspca_dev, color_adj,
801 ARRAY_SIZE(color_adj));
803 err_code = sensor_write_regs(gspca_dev, color_no_adj,
804 ARRAY_SIZE(color_no_adj));
809 err_code = sensor_write_regs(gspca_dev, vga_sensor1_init_data,
810 ARRAY_SIZE(vga_sensor1_init_data));
815 static int sd_start(struct gspca_dev *gspca_dev)
817 struct sd *sd = (struct sd *) gspca_dev;
822 /* Some of the VGA cameras require the memory pointer
823 * to be set to 0 again. We have been forced to start the
824 * stream in sd_config() to detect the hardware, and closed it.
825 * Thus, we need here to do a completely fresh and clean start. */
826 err_code = zero_the_pointer(gspca_dev);
830 err_code = stream_start(gspca_dev);
834 if (sd->cam_type == CAM_TYPE_CIF) {
835 err_code = start_cif_cam(gspca_dev);
837 err_code = start_vga_cam(gspca_dev);
842 setbrightness(gspca_dev);
843 setexposure(gspca_dev);
846 return isoc_enable(gspca_dev);
849 static void sd_stopN(struct gspca_dev *gspca_dev)
851 struct sd *sd = (struct sd *) gspca_dev;
853 stream_stop(gspca_dev);
854 /* Not all the cams need this, but even if not, probably a good idea */
855 zero_the_pointer(gspca_dev);
860 static void setbrightness(struct gspca_dev *gspca_dev)
862 struct sd *sd = (struct sd *) gspca_dev;
864 u8 sign_reg = 7; /* This reg and the next one used on CIF cams. */
865 u8 value_reg = 8; /* VGA cams seem to use regs 0x0b and 0x0c */
866 const u8 quick_clix_table[] =
867 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
868 { 0, 4, 8, 12, 1, 2, 3, 5, 6, 9, 7, 10, 13, 11, 14, 15};
870 * This control is disabled for CIF type 1 and VGA type 0 cameras.
871 * It does not quite act linearly for the Argus QuickClix camera,
872 * but it does control brightness. The values are 0 - 15 only, and
873 * the table above makes them act consecutively.
875 if ((gspca_dev->ctrl_dis & (1 << NORM_BRIGHTNESS_IDX)) &&
876 (gspca_dev->ctrl_dis & (1 << ARGUS_QC_BRIGHTNESS_IDX)))
879 if (sd->cam_type == CAM_TYPE_VGA) {
884 /* Note register 7 is also seen as 0x8x or 0xCx in some dumps */
885 if (sd->brightness > 0) {
886 sensor_write1(gspca_dev, sign_reg, 0x00);
887 val = sd->brightness;
889 sensor_write1(gspca_dev, sign_reg, 0x01);
890 val = (257 - sd->brightness);
892 /* Use lookup table for funky Argus QuickClix brightness */
894 val = quick_clix_table[val];
896 sensor_write1(gspca_dev, value_reg, val);
899 static void setexposure(struct gspca_dev *gspca_dev)
901 struct sd *sd = (struct sd *) gspca_dev;
905 if (gspca_dev->ctrl_dis & (1 << EXPOSURE_IDX))
908 if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1) {
909 /* This cam does not like exposure settings < 300,
910 so scale 0 - 4095 to 300 - 4095 */
911 exposure = (sd->exposure * 9267) / 10000 + 300;
912 sensor_write1(gspca_dev, 3, exposure >> 4);
913 sensor_write1(gspca_dev, 4, exposure & 0x0f);
915 /* We have both a clock divider and an exposure register.
916 We first calculate the clock divider, as that determines
917 the maximum exposure and then we calculate the exposure
918 register setting (which goes from 0 - 511).
920 Note our 0 - 4095 exposure is mapped to 0 - 511
921 milliseconds exposure time */
922 u8 clockdiv = (60 * sd->exposure + 7999) / 8000;
924 /* Limit framerate to not exceed usb bandwidth */
925 if (clockdiv < sd->min_clockdiv && gspca_dev->width >= 320)
926 clockdiv = sd->min_clockdiv;
927 else if (clockdiv < 2)
930 if (sd->cam_type == CAM_TYPE_VGA && clockdiv < 4)
933 /* Frame exposure time in ms = 1000 * clockdiv / 60 ->
934 exposure = (sd->exposure / 8) * 511 / (1000 * clockdiv / 60) */
935 exposure = (60 * 511 * sd->exposure) / (8000 * clockdiv);
939 /* exposure register value is reversed! */
940 exposure = 511 - exposure;
942 buf[0] = exposure & 0xff;
943 buf[1] = exposure >> 8;
944 sensor_write_reg(gspca_dev, 0x0e, 0, buf, 2);
945 sensor_write1(gspca_dev, 0x02, clockdiv);
949 static void setgain(struct gspca_dev *gspca_dev)
951 struct sd *sd = (struct sd *) gspca_dev;
953 if (gspca_dev->ctrl_dis & (1 << GAIN_IDX))
956 if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1) {
957 sensor_write1(gspca_dev, 0x0e, sd->gain);
959 sensor_write1(gspca_dev, 0x10, sd->gain);
963 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
965 struct sd *sd = (struct sd *) gspca_dev;
967 sd->brightness = val;
968 if (gspca_dev->streaming)
969 setbrightness(gspca_dev);
973 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
975 struct sd *sd = (struct sd *) gspca_dev;
977 *val = sd->brightness;
981 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
983 struct sd *sd = (struct sd *) gspca_dev;
986 if (gspca_dev->streaming)
987 setexposure(gspca_dev);
991 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
993 struct sd *sd = (struct sd *) gspca_dev;
999 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
1001 struct sd *sd = (struct sd *) gspca_dev;
1004 if (gspca_dev->streaming)
1009 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
1011 struct sd *sd = (struct sd *) gspca_dev;
1017 static int sd_setmin_clockdiv(struct gspca_dev *gspca_dev, __s32 val)
1019 struct sd *sd = (struct sd *) gspca_dev;
1021 sd->min_clockdiv = val;
1022 if (gspca_dev->streaming)
1023 setexposure(gspca_dev);
1027 static int sd_getmin_clockdiv(struct gspca_dev *gspca_dev, __s32 *val)
1029 struct sd *sd = (struct sd *) gspca_dev;
1031 *val = sd->min_clockdiv;
1035 /* Include pac common sof detection functions */
1036 #include "pac_common.h"
1038 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1039 u8 *data, /* isoc packet */
1040 int len) /* iso packet length */
1042 struct sd *sd = (struct sd *) gspca_dev;
1045 sof = pac_find_sof(&sd->sof_read, data, len);
1049 /* finish decoding current frame */
1051 if (n > sizeof pac_sof_marker)
1052 n -= sizeof pac_sof_marker;
1055 gspca_frame_add(gspca_dev, LAST_PACKET,
1057 /* Start next frame. */
1058 gspca_frame_add(gspca_dev, FIRST_PACKET,
1059 pac_sof_marker, sizeof pac_sof_marker);
1063 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
1066 /* sub-driver description */
1067 static const struct sd_desc sd_desc = {
1068 .name = MODULE_NAME,
1070 .nctrls = ARRAY_SIZE(sd_ctrls),
1071 .config = sd_config,
1075 .pkt_scan = sd_pkt_scan,
1078 /* -- module initialisation -- */
1079 static const __devinitdata struct usb_device_id device_table[] = {
1080 {USB_DEVICE(0x08ca, 0x0110)}, /* Trust Spyc@m 100 */
1081 {USB_DEVICE(0x08ca, 0x0111)}, /* Aiptek Pencam VGA+ */
1082 {USB_DEVICE(0x093a, 0x010f)}, /* All other known MR97310A VGA cams */
1083 {USB_DEVICE(0x093a, 0x010e)}, /* All known MR97310A CIF cams */
1086 MODULE_DEVICE_TABLE(usb, device_table);
1088 /* -- device connect -- */
1089 static int sd_probe(struct usb_interface *intf,
1090 const struct usb_device_id *id)
1092 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1096 static struct usb_driver sd_driver = {
1097 .name = MODULE_NAME,
1098 .id_table = device_table,
1100 .disconnect = gspca_disconnect,
1102 .suspend = gspca_suspend,
1103 .resume = gspca_resume,
1107 /* -- module insert / remove -- */
1108 static int __init sd_mod_init(void)
1112 ret = usb_register(&sd_driver);
1115 PDEBUG(D_PROBE, "registered");
1118 static void __exit sd_mod_exit(void)
1120 usb_deregister(&sd_driver);
1121 PDEBUG(D_PROBE, "deregistered");
1124 module_init(sd_mod_init);
1125 module_exit(sd_mod_exit);