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 <hdegoede@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 <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>
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.
39 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41 #define MODULE_NAME "mr97310a"
45 #define CAM_TYPE_CIF 0
46 #define CAM_TYPE_VGA 1
48 #define MR97310A_BRIGHTNESS_DEFAULT 0
50 #define MR97310A_EXPOSURE_MIN 0
51 #define MR97310A_EXPOSURE_MAX 4095
52 #define MR97310A_EXPOSURE_DEFAULT 1000
54 #define MR97310A_GAIN_MIN 0
55 #define MR97310A_GAIN_MAX 31
56 #define MR97310A_GAIN_DEFAULT 25
58 #define MR97310A_CONTRAST_MIN 0
59 #define MR97310A_CONTRAST_MAX 31
60 #define MR97310A_CONTRAST_DEFAULT 23
62 #define MR97310A_CS_GAIN_MIN 0
63 #define MR97310A_CS_GAIN_MAX 0x7ff
64 #define MR97310A_CS_GAIN_DEFAULT 0x110
66 #define MR97310A_CID_CLOCKDIV (V4L2_CTRL_CLASS_USER + 0x1000)
67 #define MR97310A_MIN_CLOCKDIV_MIN 3
68 #define MR97310A_MIN_CLOCKDIV_MAX 8
69 #define MR97310A_MIN_CLOCKDIV_DEFAULT 3
71 MODULE_AUTHOR("Kyle Guinn <elyk03@gmail.com>,Theodore Kilgore <kilgota@auburn.edu>");
72 MODULE_DESCRIPTION("GSPCA/Mars-Semi MR97310A USB Camera Driver");
73 MODULE_LICENSE("GPL");
75 /* global parameters */
76 static int force_sensor_type = -1;
77 module_param(force_sensor_type, int, 0644);
78 MODULE_PARM_DESC(force_sensor_type, "Force sensor type (-1 (auto), 0 or 1)");
80 /* specific webcam descriptor */
82 struct gspca_dev gspca_dev; /* !! must be the first item */
83 struct { /* exposure/min_clockdiv control cluster */
84 struct v4l2_ctrl *exposure;
85 struct v4l2_ctrl *min_clockdiv;
88 u8 cam_type; /* 0 is CIF and 1 is VGA */
89 u8 sensor_type; /* We use 0 and 1 here, too. */
94 struct sensor_w_data {
101 static void sd_stopN(struct gspca_dev *gspca_dev);
103 static const struct v4l2_pix_format vga_mode[] = {
104 {160, 120, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
106 .sizeimage = 160 * 120,
107 .colorspace = V4L2_COLORSPACE_SRGB,
109 {176, 144, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
111 .sizeimage = 176 * 144,
112 .colorspace = V4L2_COLORSPACE_SRGB,
114 {320, 240, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
116 .sizeimage = 320 * 240,
117 .colorspace = V4L2_COLORSPACE_SRGB,
119 {352, 288, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
121 .sizeimage = 352 * 288,
122 .colorspace = V4L2_COLORSPACE_SRGB,
124 {640, 480, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
126 .sizeimage = 640 * 480,
127 .colorspace = V4L2_COLORSPACE_SRGB,
131 /* the bytes to write are in gspca_dev->usb_buf */
132 static int mr_write(struct gspca_dev *gspca_dev, int len)
136 rc = usb_bulk_msg(gspca_dev->dev,
137 usb_sndbulkpipe(gspca_dev->dev, 4),
138 gspca_dev->usb_buf, len, NULL, 500);
140 pr_err("reg write [%02x] error %d\n",
141 gspca_dev->usb_buf[0], rc);
145 /* the bytes are read into gspca_dev->usb_buf */
146 static int mr_read(struct gspca_dev *gspca_dev, int len)
150 rc = usb_bulk_msg(gspca_dev->dev,
151 usb_rcvbulkpipe(gspca_dev->dev, 3),
152 gspca_dev->usb_buf, len, NULL, 500);
154 pr_err("reg read [%02x] error %d\n",
155 gspca_dev->usb_buf[0], rc);
159 static int sensor_write_reg(struct gspca_dev *gspca_dev, u8 reg, u8 flags,
160 const u8 *data, int len)
162 gspca_dev->usb_buf[0] = 0x1f;
163 gspca_dev->usb_buf[1] = flags;
164 gspca_dev->usb_buf[2] = reg;
165 memcpy(gspca_dev->usb_buf + 3, data, len);
167 return mr_write(gspca_dev, len + 3);
170 static int sensor_write_regs(struct gspca_dev *gspca_dev,
171 const struct sensor_w_data *data, int len)
175 for (i = 0; i < len; i++) {
176 rc = sensor_write_reg(gspca_dev, data[i].reg, data[i].flags,
177 data[i].data, data[i].len);
185 static int sensor_write1(struct gspca_dev *gspca_dev, u8 reg, u8 data)
187 struct sd *sd = (struct sd *) gspca_dev;
192 if (sd->cam_type == CAM_TYPE_CIF) {
193 rc = sensor_write_reg(gspca_dev, reg, 0x01, &buf, 1);
194 confirm_reg = sd->sensor_type ? 0x13 : 0x11;
196 rc = sensor_write_reg(gspca_dev, reg, 0x00, &buf, 1);
203 rc = sensor_write_reg(gspca_dev, confirm_reg, 0x00, &buf, 1);
210 static int cam_get_response16(struct gspca_dev *gspca_dev, u8 reg, int verbose)
214 gspca_dev->usb_buf[0] = reg;
215 err_code = mr_write(gspca_dev, 1);
219 err_code = mr_read(gspca_dev, 16);
224 PDEBUG(D_PROBE, "Register: %02x reads %02x%02x%02x", reg,
225 gspca_dev->usb_buf[0],
226 gspca_dev->usb_buf[1],
227 gspca_dev->usb_buf[2]);
232 static int zero_the_pointer(struct gspca_dev *gspca_dev)
234 __u8 *data = gspca_dev->usb_buf;
239 err_code = cam_get_response16(gspca_dev, 0x21, 0);
245 err_code = mr_write(gspca_dev, 2);
249 err_code = cam_get_response16(gspca_dev, 0x21, 0);
255 err_code = mr_write(gspca_dev, 2);
259 err_code = cam_get_response16(gspca_dev, 0x21, 0);
265 err_code = mr_write(gspca_dev, 2);
269 err_code = cam_get_response16(gspca_dev, 0x21, 0);
275 err_code = mr_write(gspca_dev, 2);
279 while (status != 0x0a && tries < 256) {
280 err_code = cam_get_response16(gspca_dev, 0x21, 0);
287 PERR("status is %02x", status);
293 err_code = mr_write(gspca_dev, 2);
297 err_code = cam_get_response16(gspca_dev, 0x21, 0);
305 err_code = mr_write(gspca_dev, 1);
309 err_code = mr_read(gspca_dev, 16);
316 static int stream_start(struct gspca_dev *gspca_dev)
318 gspca_dev->usb_buf[0] = 0x01;
319 gspca_dev->usb_buf[1] = 0x01;
320 return mr_write(gspca_dev, 2);
323 static void stream_stop(struct gspca_dev *gspca_dev)
325 gspca_dev->usb_buf[0] = 0x01;
326 gspca_dev->usb_buf[1] = 0x00;
327 if (mr_write(gspca_dev, 2) < 0)
328 PERR("Stream Stop failed");
331 static void lcd_stop(struct gspca_dev *gspca_dev)
333 gspca_dev->usb_buf[0] = 0x19;
334 gspca_dev->usb_buf[1] = 0x54;
335 if (mr_write(gspca_dev, 2) < 0)
336 PERR("LCD Stop failed");
339 static int isoc_enable(struct gspca_dev *gspca_dev)
341 gspca_dev->usb_buf[0] = 0x00;
342 gspca_dev->usb_buf[1] = 0x4d; /* ISOC transferring enable... */
343 return mr_write(gspca_dev, 2);
346 /* This function is called at probe time */
347 static int sd_config(struct gspca_dev *gspca_dev,
348 const struct usb_device_id *id)
350 struct sd *sd = (struct sd *) gspca_dev;
354 cam = &gspca_dev->cam;
355 cam->cam_mode = vga_mode;
356 cam->nmodes = ARRAY_SIZE(vga_mode);
359 /* Several of the supported CIF cameras share the same USB ID but
360 * require different initializations and different control settings.
361 * The same is true of the VGA cameras. Therefore, we are forced
362 * to start the initialization process in order to determine which
363 * camera is present. Some of the supported cameras require the
364 * memory pointer to be set to 0 as the very first item of business
365 * or else they will not stream. So we do that immediately.
367 err_code = zero_the_pointer(gspca_dev);
371 err_code = stream_start(gspca_dev);
375 /* Now, the query for sensor type. */
376 err_code = cam_get_response16(gspca_dev, 0x07, 1);
380 if (id->idProduct == 0x0110 || id->idProduct == 0x010e) {
381 sd->cam_type = CAM_TYPE_CIF;
384 * All but one of the known CIF cameras share the same USB ID,
385 * but two different init routines are in use, and the control
386 * settings are different, too. We need to detect which camera
387 * of the two known varieties is connected!
389 * A list of known CIF cameras follows. They all report either
390 * 0200 for type 0 or 0300 for type 1.
391 * If you have another to report, please do
393 * Name sd->sensor_type reported by
395 * Sakar 56379 Spy-shot 0 T. Kilgore
396 * Innovage 0 T. Kilgore
397 * Vivitar Mini 0 H. De Goede
398 * Vivitar Mini 0 E. Rodriguez
399 * Vivitar Mini 1 T. Kilgore
400 * Elta-Media 8212dc 1 T. Kaiser
401 * Philips dig. keych. 1 T. Kilgore
402 * Trust Spyc@m 100 1 A. Jacobs
404 switch (gspca_dev->usb_buf[0]) {
412 pr_err("Unknown CIF Sensor id : %02x\n",
413 gspca_dev->usb_buf[1]);
416 PDEBUG(D_PROBE, "MR97310A CIF camera detected, sensor: %d",
419 sd->cam_type = CAM_TYPE_VGA;
422 * Here is a table of the responses to the query for sensor
423 * type, from the known MR97310A VGA cameras. Six different
424 * cameras of which five share the same USB ID.
426 * Name gspca_dev->usb_buf[] sd->sensor_type
428 * Aiptek Pencam VGA+ 0300 0 1
429 * ION digital 0300 0 1
430 * Argus DC-1620 0450 1 0
431 * Argus QuickClix 0420 1 1
432 * Sakar 77379 Digital 0350 0 1
433 * Sakar 1638x CyberPix 0120 0 2
435 * Based upon these results, we assume default settings
436 * and then correct as necessary, as follows.
443 if (gspca_dev->usb_buf[0] == 0x01) {
445 } else if ((gspca_dev->usb_buf[0] != 0x03) &&
446 (gspca_dev->usb_buf[0] != 0x04)) {
447 pr_err("Unknown VGA Sensor id Byte 0: %02x\n",
448 gspca_dev->usb_buf[0]);
449 pr_err("Defaults assumed, may not work\n");
450 pr_err("Please report this\n");
452 /* Sakar Digital color needs to be adjusted. */
453 if ((gspca_dev->usb_buf[0] == 0x03) &&
454 (gspca_dev->usb_buf[1] == 0x50))
456 if (gspca_dev->usb_buf[0] == 0x04) {
458 switch (gspca_dev->usb_buf[1]) {
461 PDEBUG(D_PROBE, "sensor_type corrected to 0");
464 /* Nothing to do here. */
467 pr_err("Unknown VGA Sensor id Byte 1: %02x\n",
468 gspca_dev->usb_buf[1]);
469 pr_err("Defaults assumed, may not work\n");
470 pr_err("Please report this\n");
473 PDEBUG(D_PROBE, "MR97310A VGA camera detected, sensor: %d",
476 /* Stop streaming as we've started it only to probe the sensor type. */
479 if (force_sensor_type != -1) {
480 sd->sensor_type = !!force_sensor_type;
481 PDEBUG(D_PROBE, "Forcing sensor type to: %d",
488 /* this function is called at probe and resume time */
489 static int sd_init(struct gspca_dev *gspca_dev)
494 static int start_cif_cam(struct gspca_dev *gspca_dev)
496 struct sd *sd = (struct sd *) gspca_dev;
497 __u8 *data = gspca_dev->usb_buf;
499 static const __u8 startup_string[] = {
503 0x00, /* Hsize/8 for 352 or 320 */
504 0x00, /* Vsize/4 for 288 or 240 */
505 0x13, /* or 0xbb, depends on sensor */
506 0x00, /* Hstart, depends on res. */
507 0x00, /* reserved ? */
508 0x00, /* Vstart, depends on res. and sensor */
509 0x50, /* 0x54 to get 176 or 160 */
513 /* Note: Some of the above descriptions guessed from MR97113A driver */
515 memcpy(data, startup_string, 11);
519 switch (gspca_dev->pixfmt.width) {
521 data[9] |= 0x04; /* reg 8, 2:1 scale down from 320 */
525 data[3] = 0x28; /* reg 2, H size/8 */
526 data[4] = 0x3c; /* reg 3, V size/4 */
527 data[6] = 0x14; /* reg 5, H start */
528 data[8] = 0x1a + sd->sensor_type; /* reg 7, V start */
531 data[9] |= 0x04; /* reg 8, 2:1 scale down from 352 */
534 data[3] = 0x2c; /* reg 2, H size/8 */
535 data[4] = 0x48; /* reg 3, V size/4 */
536 data[6] = 0x06; /* reg 5, H start */
537 data[8] = 0x06 - sd->sensor_type; /* reg 7, V start */
540 err_code = mr_write(gspca_dev, 11);
544 if (!sd->sensor_type) {
545 static const struct sensor_w_data cif_sensor0_init_data[] = {
546 {0x02, 0x00, {0x03, 0x5a, 0xb5, 0x01,
547 0x0f, 0x14, 0x0f, 0x10}, 8},
548 {0x0c, 0x00, {0x04, 0x01, 0x01, 0x00, 0x1f}, 5},
549 {0x12, 0x00, {0x07}, 1},
550 {0x1f, 0x00, {0x06}, 1},
551 {0x27, 0x00, {0x04}, 1},
552 {0x29, 0x00, {0x0c}, 1},
553 {0x40, 0x00, {0x40, 0x00, 0x04}, 3},
554 {0x50, 0x00, {0x60}, 1},
555 {0x60, 0x00, {0x06}, 1},
556 {0x6b, 0x00, {0x85, 0x85, 0xc8, 0xc8, 0xc8, 0xc8}, 6},
557 {0x72, 0x00, {0x1e, 0x56}, 2},
558 {0x75, 0x00, {0x58, 0x40, 0xa2, 0x02, 0x31, 0x02,
559 0x31, 0x80, 0x00}, 9},
560 {0x11, 0x00, {0x01}, 1},
563 err_code = sensor_write_regs(gspca_dev, cif_sensor0_init_data,
564 ARRAY_SIZE(cif_sensor0_init_data));
565 } else { /* sd->sensor_type = 1 */
566 static const struct sensor_w_data cif_sensor1_init_data[] = {
567 /* Reg 3,4, 7,8 get set by the controls */
568 {0x02, 0x00, {0x10}, 1},
569 {0x05, 0x01, {0x22}, 1}, /* 5/6 also seen as 65h/32h */
570 {0x06, 0x01, {0x00}, 1},
571 {0x09, 0x02, {0x0e}, 1},
572 {0x0a, 0x02, {0x05}, 1},
573 {0x0b, 0x02, {0x05}, 1},
574 {0x0c, 0x02, {0x0f}, 1},
575 {0x0d, 0x02, {0x07}, 1},
576 {0x0e, 0x02, {0x0c}, 1},
577 {0x0f, 0x00, {0x00}, 1},
578 {0x10, 0x00, {0x06}, 1},
579 {0x11, 0x00, {0x07}, 1},
580 {0x12, 0x00, {0x00}, 1},
581 {0x13, 0x00, {0x01}, 1},
584 /* Without this command the cam won't work with USB-UHCI */
585 gspca_dev->usb_buf[0] = 0x0a;
586 gspca_dev->usb_buf[1] = 0x00;
587 err_code = mr_write(gspca_dev, 2);
590 err_code = sensor_write_regs(gspca_dev, cif_sensor1_init_data,
591 ARRAY_SIZE(cif_sensor1_init_data));
596 static int start_vga_cam(struct gspca_dev *gspca_dev)
598 struct sd *sd = (struct sd *) gspca_dev;
599 __u8 *data = gspca_dev->usb_buf;
601 static const __u8 startup_string[] =
602 {0x00, 0x0d, 0x01, 0x00, 0x00, 0x2b, 0x00, 0x00,
604 /* What some of these mean is explained in start_cif_cam(), above */
606 memcpy(data, startup_string, 11);
607 if (!sd->sensor_type) {
611 if (sd->sensor_type == 2) {
616 switch (gspca_dev->pixfmt.width) {
618 data[9] |= 0x0c; /* reg 8, 4:1 scale down */
621 data[9] |= 0x04; /* reg 8, 2:1 scale down */
625 data[3] = 0x50; /* reg 2, H size/8 */
626 data[4] = 0x78; /* reg 3, V size/4 */
627 data[6] = 0x04; /* reg 5, H start */
628 data[8] = 0x03; /* reg 7, V start */
629 if (sd->sensor_type == 2) {
634 data[8] = 0x04; /* Bayer tile shifted */
638 data[9] |= 0x04; /* reg 8, 2:1 scale down */
641 data[3] = 0x2c; /* reg 2, H size */
642 data[4] = 0x48; /* reg 3, V size */
643 data[6] = 0x94; /* reg 5, H start */
644 data[8] = 0x63; /* reg 7, V start */
646 data[8] = 0x64; /* Bayer tile shifted */
650 err_code = mr_write(gspca_dev, 11);
654 if (!sd->sensor_type) {
655 static const struct sensor_w_data vga_sensor0_init_data[] = {
656 {0x01, 0x00, {0x0c, 0x00, 0x04}, 3},
657 {0x14, 0x00, {0x01, 0xe4, 0x02, 0x84}, 4},
658 {0x20, 0x00, {0x00, 0x80, 0x00, 0x08}, 4},
659 {0x25, 0x00, {0x03, 0xa9, 0x80}, 3},
660 {0x30, 0x00, {0x30, 0x18, 0x10, 0x18}, 4},
663 err_code = sensor_write_regs(gspca_dev, vga_sensor0_init_data,
664 ARRAY_SIZE(vga_sensor0_init_data));
665 } else if (sd->sensor_type == 1) {
666 static const struct sensor_w_data color_adj[] = {
667 {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
668 /* adjusted blue, green, red gain correct
669 too much blue from the Sakar Digital */
670 0x05, 0x01, 0x04}, 8}
673 static const struct sensor_w_data color_no_adj[] = {
674 {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
675 /* default blue, green, red gain settings */
676 0x07, 0x00, 0x01}, 8}
679 static const struct sensor_w_data vga_sensor1_init_data[] = {
680 {0x11, 0x04, {0x01}, 1},
681 {0x0a, 0x00, {0x00, 0x01, 0x00, 0x00, 0x01,
682 /* These settings may be better for some cameras */
683 /* {0x0a, 0x00, {0x01, 0x06, 0x00, 0x00, 0x01, */
685 {0x11, 0x04, {0x01}, 1},
686 {0x12, 0x00, {0x00, 0x63, 0x00, 0x70, 0x00, 0x00}, 6},
687 {0x11, 0x04, {0x01}, 1},
692 err_code = sensor_write_regs(gspca_dev, color_adj,
693 ARRAY_SIZE(color_adj));
695 err_code = sensor_write_regs(gspca_dev, color_no_adj,
696 ARRAY_SIZE(color_no_adj));
701 err_code = sensor_write_regs(gspca_dev, vga_sensor1_init_data,
702 ARRAY_SIZE(vga_sensor1_init_data));
703 } else { /* sensor type == 2 */
704 static const struct sensor_w_data vga_sensor2_init_data[] = {
706 {0x01, 0x00, {0x48}, 1},
707 {0x02, 0x00, {0x22}, 1},
708 /* Reg 3 msb and 4 is lsb of the exposure setting*/
709 {0x05, 0x00, {0x10}, 1},
710 {0x06, 0x00, {0x00}, 1},
711 {0x07, 0x00, {0x00}, 1},
712 {0x08, 0x00, {0x00}, 1},
713 {0x09, 0x00, {0x00}, 1},
714 /* The following are used in the gain control
715 * which is BTW completely borked in the OEM driver
716 * The values for each color go from 0 to 0x7ff
717 *{0x0a, 0x00, {0x01}, 1}, green1 gain msb
718 *{0x0b, 0x00, {0x10}, 1}, green1 gain lsb
719 *{0x0c, 0x00, {0x01}, 1}, red gain msb
720 *{0x0d, 0x00, {0x10}, 1}, red gain lsb
721 *{0x0e, 0x00, {0x01}, 1}, blue gain msb
722 *{0x0f, 0x00, {0x10}, 1}, blue gain lsb
723 *{0x10, 0x00, {0x01}, 1}, green2 gain msb
724 *{0x11, 0x00, {0x10}, 1}, green2 gain lsb
726 {0x12, 0x00, {0x00}, 1},
727 {0x13, 0x00, {0x04}, 1}, /* weird effect on colors */
728 {0x14, 0x00, {0x00}, 1},
729 {0x15, 0x00, {0x06}, 1},
730 {0x16, 0x00, {0x01}, 1},
731 {0x17, 0x00, {0xe2}, 1}, /* vertical alignment */
732 {0x18, 0x00, {0x02}, 1},
733 {0x19, 0x00, {0x82}, 1}, /* don't mess with */
734 {0x1a, 0x00, {0x00}, 1},
735 {0x1b, 0x00, {0x20}, 1},
736 /* {0x1c, 0x00, {0x17}, 1}, contrast control */
737 {0x1d, 0x00, {0x80}, 1}, /* moving causes a mess */
738 {0x1e, 0x00, {0x08}, 1}, /* moving jams the camera */
739 {0x1f, 0x00, {0x0c}, 1},
740 {0x20, 0x00, {0x00}, 1},
743 err_code = sensor_write_regs(gspca_dev, vga_sensor2_init_data,
744 ARRAY_SIZE(vga_sensor2_init_data));
749 static int sd_start(struct gspca_dev *gspca_dev)
751 struct sd *sd = (struct sd *) gspca_dev;
756 /* Some of the VGA cameras require the memory pointer
757 * to be set to 0 again. We have been forced to start the
758 * stream in sd_config() to detect the hardware, and closed it.
759 * Thus, we need here to do a completely fresh and clean start. */
760 err_code = zero_the_pointer(gspca_dev);
764 err_code = stream_start(gspca_dev);
768 if (sd->cam_type == CAM_TYPE_CIF) {
769 err_code = start_cif_cam(gspca_dev);
771 err_code = start_vga_cam(gspca_dev);
776 return isoc_enable(gspca_dev);
779 static void sd_stopN(struct gspca_dev *gspca_dev)
781 struct sd *sd = (struct sd *) gspca_dev;
783 stream_stop(gspca_dev);
784 /* Not all the cams need this, but even if not, probably a good idea */
785 zero_the_pointer(gspca_dev);
790 static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
792 struct sd *sd = (struct sd *) gspca_dev;
793 u8 sign_reg = 7; /* This reg and the next one used on CIF cams. */
794 u8 value_reg = 8; /* VGA cams seem to use regs 0x0b and 0x0c */
795 static const u8 quick_clix_table[] =
796 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
797 { 0, 4, 8, 12, 1, 2, 3, 5, 6, 9, 7, 10, 13, 11, 14, 15};
798 if (sd->cam_type == CAM_TYPE_VGA) {
803 /* Note register 7 is also seen as 0x8x or 0xCx in some dumps */
805 sensor_write1(gspca_dev, sign_reg, 0x00);
807 sensor_write1(gspca_dev, sign_reg, 0x01);
810 /* Use lookup table for funky Argus QuickClix brightness */
812 val = quick_clix_table[val];
814 sensor_write1(gspca_dev, value_reg, val);
817 static void setexposure(struct gspca_dev *gspca_dev, s32 expo, s32 min_clockdiv)
819 struct sd *sd = (struct sd *) gspca_dev;
820 int exposure = MR97310A_EXPOSURE_DEFAULT;
823 if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1) {
824 /* This cam does not like exposure settings < 300,
825 so scale 0 - 4095 to 300 - 4095 */
826 exposure = (expo * 9267) / 10000 + 300;
827 sensor_write1(gspca_dev, 3, exposure >> 4);
828 sensor_write1(gspca_dev, 4, exposure & 0x0f);
829 } else if (sd->sensor_type == 2) {
832 sensor_write1(gspca_dev, 3, exposure >> 8);
833 sensor_write1(gspca_dev, 4, exposure & 0xff);
835 /* We have both a clock divider and an exposure register.
836 We first calculate the clock divider, as that determines
837 the maximum exposure and then we calculate the exposure
838 register setting (which goes from 0 - 511).
840 Note our 0 - 4095 exposure is mapped to 0 - 511
841 milliseconds exposure time */
842 u8 clockdiv = (60 * expo + 7999) / 8000;
844 /* Limit framerate to not exceed usb bandwidth */
845 if (clockdiv < min_clockdiv && gspca_dev->pixfmt.width >= 320)
846 clockdiv = min_clockdiv;
847 else if (clockdiv < 2)
850 if (sd->cam_type == CAM_TYPE_VGA && clockdiv < 4)
853 /* Frame exposure time in ms = 1000 * clockdiv / 60 ->
854 exposure = (sd->exposure / 8) * 511 / (1000 * clockdiv / 60) */
855 exposure = (60 * 511 * expo) / (8000 * clockdiv);
859 /* exposure register value is reversed! */
860 exposure = 511 - exposure;
862 buf[0] = exposure & 0xff;
863 buf[1] = exposure >> 8;
864 sensor_write_reg(gspca_dev, 0x0e, 0, buf, 2);
865 sensor_write1(gspca_dev, 0x02, clockdiv);
869 static void setgain(struct gspca_dev *gspca_dev, s32 val)
871 struct sd *sd = (struct sd *) gspca_dev;
874 if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1)
875 sensor_write1(gspca_dev, 0x0e, val);
876 else if (sd->cam_type == CAM_TYPE_VGA && sd->sensor_type == 2)
877 for (gainreg = 0x0a; gainreg < 0x11; gainreg += 2) {
878 sensor_write1(gspca_dev, gainreg, val >> 8);
879 sensor_write1(gspca_dev, gainreg + 1, val & 0xff);
882 sensor_write1(gspca_dev, 0x10, val);
885 static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
887 sensor_write1(gspca_dev, 0x1c, val);
890 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
892 struct gspca_dev *gspca_dev =
893 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
894 struct sd *sd = (struct sd *)gspca_dev;
896 gspca_dev->usb_err = 0;
898 if (!gspca_dev->streaming)
902 case V4L2_CID_BRIGHTNESS:
903 setbrightness(gspca_dev, ctrl->val);
905 case V4L2_CID_CONTRAST:
906 setcontrast(gspca_dev, ctrl->val);
908 case V4L2_CID_EXPOSURE:
909 setexposure(gspca_dev, sd->exposure->val,
910 sd->min_clockdiv ? sd->min_clockdiv->val : 0);
913 setgain(gspca_dev, ctrl->val);
916 return gspca_dev->usb_err;
919 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
923 static int sd_init_controls(struct gspca_dev *gspca_dev)
925 struct sd *sd = (struct sd *)gspca_dev;
926 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
927 static const struct v4l2_ctrl_config clockdiv = {
929 .id = MR97310A_CID_CLOCKDIV,
930 .type = V4L2_CTRL_TYPE_INTEGER,
931 .name = "Minimum Clock Divider",
932 .min = MR97310A_MIN_CLOCKDIV_MIN,
933 .max = MR97310A_MIN_CLOCKDIV_MAX,
935 .def = MR97310A_MIN_CLOCKDIV_DEFAULT,
937 bool has_brightness = false;
938 bool has_argus_brightness = false;
939 bool has_contrast = false;
940 bool has_gain = false;
941 bool has_cs_gain = false;
942 bool has_exposure = false;
943 bool has_clockdiv = false;
945 gspca_dev->vdev.ctrl_handler = hdl;
946 v4l2_ctrl_handler_init(hdl, 4);
948 /* Setup controls depending on camera type */
949 if (sd->cam_type == CAM_TYPE_CIF) {
950 /* No brightness for sensor_type 0 */
951 if (sd->sensor_type == 0)
952 has_exposure = has_gain = has_clockdiv = true;
954 has_exposure = has_gain = has_brightness = true;
956 /* All controls need to be disabled if VGA sensor_type is 0 */
957 if (sd->sensor_type == 0)
959 else if (sd->sensor_type == 2)
960 has_exposure = has_cs_gain = has_contrast = true;
961 else if (sd->do_lcd_stop)
962 has_exposure = has_gain = has_argus_brightness =
965 has_exposure = has_gain = has_brightness =
969 /* Separate brightness control description for Argus QuickClix as it has
970 * different limits from the other mr97310a cameras, and separate gain
971 * control for Sakar CyberPix camera. */
973 * This control is disabled for CIF type 1 and VGA type 0 cameras.
974 * It does not quite act linearly for the Argus QuickClix camera,
975 * but it does control brightness. The values are 0 - 15 only, and
976 * the table above makes them act consecutively.
979 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
980 V4L2_CID_BRIGHTNESS, -254, 255, 1,
981 MR97310A_BRIGHTNESS_DEFAULT);
982 else if (has_argus_brightness)
983 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
984 V4L2_CID_BRIGHTNESS, 0, 15, 1,
985 MR97310A_BRIGHTNESS_DEFAULT);
987 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
988 V4L2_CID_CONTRAST, MR97310A_CONTRAST_MIN,
989 MR97310A_CONTRAST_MAX, 1, MR97310A_CONTRAST_DEFAULT);
991 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
992 V4L2_CID_GAIN, MR97310A_GAIN_MIN, MR97310A_GAIN_MAX,
993 1, MR97310A_GAIN_DEFAULT);
994 else if (has_cs_gain)
995 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, V4L2_CID_GAIN,
996 MR97310A_CS_GAIN_MIN, MR97310A_CS_GAIN_MAX,
997 1, MR97310A_CS_GAIN_DEFAULT);
999 sd->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1000 V4L2_CID_EXPOSURE, MR97310A_EXPOSURE_MIN,
1001 MR97310A_EXPOSURE_MAX, 1, MR97310A_EXPOSURE_DEFAULT);
1003 sd->min_clockdiv = v4l2_ctrl_new_custom(hdl, &clockdiv, NULL);
1006 pr_err("Could not initialize controls\n");
1009 if (has_exposure && has_clockdiv)
1010 v4l2_ctrl_cluster(2, &sd->exposure);
1014 /* Include pac common sof detection functions */
1015 #include "pac_common.h"
1017 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1018 u8 *data, /* isoc packet */
1019 int len) /* iso packet length */
1021 struct sd *sd = (struct sd *) gspca_dev;
1024 sof = pac_find_sof(gspca_dev, &sd->sof_read, data, len);
1028 /* finish decoding current frame */
1030 if (n > sizeof pac_sof_marker)
1031 n -= sizeof pac_sof_marker;
1034 gspca_frame_add(gspca_dev, LAST_PACKET,
1036 /* Start next frame. */
1037 gspca_frame_add(gspca_dev, FIRST_PACKET,
1038 pac_sof_marker, sizeof pac_sof_marker);
1042 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
1045 /* sub-driver description */
1046 static const struct sd_desc sd_desc = {
1047 .name = MODULE_NAME,
1048 .config = sd_config,
1050 .init_controls = sd_init_controls,
1053 .pkt_scan = sd_pkt_scan,
1056 /* -- module initialisation -- */
1057 static const struct usb_device_id device_table[] = {
1058 {USB_DEVICE(0x08ca, 0x0110)}, /* Trust Spyc@m 100 */
1059 {USB_DEVICE(0x08ca, 0x0111)}, /* Aiptek Pencam VGA+ */
1060 {USB_DEVICE(0x093a, 0x010f)}, /* All other known MR97310A VGA cams */
1061 {USB_DEVICE(0x093a, 0x010e)}, /* All known MR97310A CIF cams */
1064 MODULE_DEVICE_TABLE(usb, device_table);
1066 /* -- device connect -- */
1067 static int sd_probe(struct usb_interface *intf,
1068 const struct usb_device_id *id)
1070 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1074 static struct usb_driver sd_driver = {
1075 .name = MODULE_NAME,
1076 .id_table = device_table,
1078 .disconnect = gspca_disconnect,
1080 .suspend = gspca_suspend,
1081 .resume = gspca_resume,
1082 .reset_resume = gspca_resume,
1086 module_usb_driver(sd_driver);