1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Mars MR97310A library
5 * The original mr97310a driver, which supported the Aiptek Pencam VGA+, is
6 * Copyright (C) 2009 Kyle Guinn <elyk03@gmail.com>
8 * Support for the MR97310A cameras in addition to the Aiptek Pencam VGA+
9 * and for the routines for detecting and classifying these various cameras,
10 * is Copyright (C) 2009 Theodore Kilgore <kilgota@auburn.edu>
12 * Support for the control settings for the CIF cameras is
13 * Copyright (C) 2009 Hans de Goede <hdegoede@redhat.com> and
14 * Thomas Kaiser <thomas@kaiser-linux.li>
16 * Support for the control settings for the VGA cameras is
17 * Copyright (C) 2009 Theodore Kilgore <kilgota@auburn.edu>
19 * Several previously unsupported cameras are owned and have been tested by
20 * Hans de Goede <hdegoede@redhat.com> and
21 * Thomas Kaiser <thomas@kaiser-linux.li> and
22 * Theodore Kilgore <kilgota@auburn.edu> and
23 * Edmond Rodriguez <erodrig_97@yahoo.com> and
24 * Aurelien Jacobs <aurel@gnuage.org>
26 * The MR97311A support in gspca/mars.c has been helpful in understanding some
27 * of the registers in these cameras.
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32 #define MODULE_NAME "mr97310a"
36 #define CAM_TYPE_CIF 0
37 #define CAM_TYPE_VGA 1
39 #define MR97310A_BRIGHTNESS_DEFAULT 0
41 #define MR97310A_EXPOSURE_MIN 0
42 #define MR97310A_EXPOSURE_MAX 4095
43 #define MR97310A_EXPOSURE_DEFAULT 1000
45 #define MR97310A_GAIN_MIN 0
46 #define MR97310A_GAIN_MAX 31
47 #define MR97310A_GAIN_DEFAULT 25
49 #define MR97310A_CONTRAST_MIN 0
50 #define MR97310A_CONTRAST_MAX 31
51 #define MR97310A_CONTRAST_DEFAULT 23
53 #define MR97310A_CS_GAIN_MIN 0
54 #define MR97310A_CS_GAIN_MAX 0x7ff
55 #define MR97310A_CS_GAIN_DEFAULT 0x110
57 #define MR97310A_CID_CLOCKDIV (V4L2_CTRL_CLASS_USER + 0x1000)
58 #define MR97310A_MIN_CLOCKDIV_MIN 3
59 #define MR97310A_MIN_CLOCKDIV_MAX 8
60 #define MR97310A_MIN_CLOCKDIV_DEFAULT 3
62 MODULE_AUTHOR("Kyle Guinn <elyk03@gmail.com>,Theodore Kilgore <kilgota@auburn.edu>");
63 MODULE_DESCRIPTION("GSPCA/Mars-Semi MR97310A USB Camera Driver");
64 MODULE_LICENSE("GPL");
66 /* global parameters */
67 static int force_sensor_type
= -1;
68 module_param(force_sensor_type
, int, 0644);
69 MODULE_PARM_DESC(force_sensor_type
, "Force sensor type (-1 (auto), 0 or 1)");
71 /* specific webcam descriptor */
73 struct gspca_dev gspca_dev
; /* !! must be the first item */
74 struct { /* exposure/min_clockdiv control cluster */
75 struct v4l2_ctrl
*exposure
;
76 struct v4l2_ctrl
*min_clockdiv
;
79 u8 cam_type
; /* 0 is CIF and 1 is VGA */
80 u8 sensor_type
; /* We use 0 and 1 here, too. */
85 struct sensor_w_data
{
92 static void sd_stopN(struct gspca_dev
*gspca_dev
);
94 static const struct v4l2_pix_format vga_mode
[] = {
95 {160, 120, V4L2_PIX_FMT_MR97310A
, V4L2_FIELD_NONE
,
97 .sizeimage
= 160 * 120,
98 .colorspace
= V4L2_COLORSPACE_SRGB
,
100 {176, 144, V4L2_PIX_FMT_MR97310A
, V4L2_FIELD_NONE
,
102 .sizeimage
= 176 * 144,
103 .colorspace
= V4L2_COLORSPACE_SRGB
,
105 {320, 240, V4L2_PIX_FMT_MR97310A
, V4L2_FIELD_NONE
,
107 .sizeimage
= 320 * 240,
108 .colorspace
= V4L2_COLORSPACE_SRGB
,
110 {352, 288, V4L2_PIX_FMT_MR97310A
, V4L2_FIELD_NONE
,
112 .sizeimage
= 352 * 288,
113 .colorspace
= V4L2_COLORSPACE_SRGB
,
115 {640, 480, V4L2_PIX_FMT_MR97310A
, V4L2_FIELD_NONE
,
117 .sizeimage
= 640 * 480,
118 .colorspace
= V4L2_COLORSPACE_SRGB
,
122 /* the bytes to write are in gspca_dev->usb_buf */
123 static int mr_write(struct gspca_dev
*gspca_dev
, int len
)
127 rc
= usb_bulk_msg(gspca_dev
->dev
,
128 usb_sndbulkpipe(gspca_dev
->dev
, 4),
129 gspca_dev
->usb_buf
, len
, NULL
, 500);
131 pr_err("reg write [%02x] error %d\n",
132 gspca_dev
->usb_buf
[0], rc
);
136 /* the bytes are read into gspca_dev->usb_buf */
137 static int mr_read(struct gspca_dev
*gspca_dev
, int len
)
141 rc
= usb_bulk_msg(gspca_dev
->dev
,
142 usb_rcvbulkpipe(gspca_dev
->dev
, 3),
143 gspca_dev
->usb_buf
, len
, NULL
, 500);
145 pr_err("reg read [%02x] error %d\n",
146 gspca_dev
->usb_buf
[0], rc
);
150 static int sensor_write_reg(struct gspca_dev
*gspca_dev
, u8 reg
, u8 flags
,
151 const u8
*data
, int len
)
153 gspca_dev
->usb_buf
[0] = 0x1f;
154 gspca_dev
->usb_buf
[1] = flags
;
155 gspca_dev
->usb_buf
[2] = reg
;
156 memcpy(gspca_dev
->usb_buf
+ 3, data
, len
);
158 return mr_write(gspca_dev
, len
+ 3);
161 static int sensor_write_regs(struct gspca_dev
*gspca_dev
,
162 const struct sensor_w_data
*data
, int len
)
166 for (i
= 0; i
< len
; i
++) {
167 rc
= sensor_write_reg(gspca_dev
, data
[i
].reg
, data
[i
].flags
,
168 data
[i
].data
, data
[i
].len
);
176 static int sensor_write1(struct gspca_dev
*gspca_dev
, u8 reg
, u8 data
)
178 struct sd
*sd
= (struct sd
*) gspca_dev
;
183 if (sd
->cam_type
== CAM_TYPE_CIF
) {
184 rc
= sensor_write_reg(gspca_dev
, reg
, 0x01, &buf
, 1);
185 confirm_reg
= sd
->sensor_type
? 0x13 : 0x11;
187 rc
= sensor_write_reg(gspca_dev
, reg
, 0x00, &buf
, 1);
194 rc
= sensor_write_reg(gspca_dev
, confirm_reg
, 0x00, &buf
, 1);
201 static int cam_get_response16(struct gspca_dev
*gspca_dev
, u8 reg
, int verbose
)
205 gspca_dev
->usb_buf
[0] = reg
;
206 err_code
= mr_write(gspca_dev
, 1);
210 err_code
= mr_read(gspca_dev
, 16);
215 gspca_dbg(gspca_dev
, D_PROBE
, "Register: %02x reads %02x%02x%02x\n",
217 gspca_dev
->usb_buf
[0],
218 gspca_dev
->usb_buf
[1],
219 gspca_dev
->usb_buf
[2]);
224 static int zero_the_pointer(struct gspca_dev
*gspca_dev
)
226 __u8
*data
= gspca_dev
->usb_buf
;
231 err_code
= cam_get_response16(gspca_dev
, 0x21, 0);
237 err_code
= mr_write(gspca_dev
, 2);
241 err_code
= cam_get_response16(gspca_dev
, 0x21, 0);
247 err_code
= mr_write(gspca_dev
, 2);
251 err_code
= cam_get_response16(gspca_dev
, 0x21, 0);
257 err_code
= mr_write(gspca_dev
, 2);
261 err_code
= cam_get_response16(gspca_dev
, 0x21, 0);
267 err_code
= mr_write(gspca_dev
, 2);
271 while (status
!= 0x0a && tries
< 256) {
272 err_code
= cam_get_response16(gspca_dev
, 0x21, 0);
279 gspca_err(gspca_dev
, "status is %02x\n", status
);
285 err_code
= mr_write(gspca_dev
, 2);
289 err_code
= cam_get_response16(gspca_dev
, 0x21, 0);
297 err_code
= mr_write(gspca_dev
, 1);
301 err_code
= mr_read(gspca_dev
, 16);
308 static int stream_start(struct gspca_dev
*gspca_dev
)
310 gspca_dev
->usb_buf
[0] = 0x01;
311 gspca_dev
->usb_buf
[1] = 0x01;
312 return mr_write(gspca_dev
, 2);
315 static void stream_stop(struct gspca_dev
*gspca_dev
)
317 gspca_dev
->usb_buf
[0] = 0x01;
318 gspca_dev
->usb_buf
[1] = 0x00;
319 if (mr_write(gspca_dev
, 2) < 0)
320 gspca_err(gspca_dev
, "Stream Stop failed\n");
323 static void lcd_stop(struct gspca_dev
*gspca_dev
)
325 gspca_dev
->usb_buf
[0] = 0x19;
326 gspca_dev
->usb_buf
[1] = 0x54;
327 if (mr_write(gspca_dev
, 2) < 0)
328 gspca_err(gspca_dev
, "LCD Stop failed\n");
331 static int isoc_enable(struct gspca_dev
*gspca_dev
)
333 gspca_dev
->usb_buf
[0] = 0x00;
334 gspca_dev
->usb_buf
[1] = 0x4d; /* ISOC transferring enable... */
335 return mr_write(gspca_dev
, 2);
338 /* This function is called at probe time */
339 static int sd_config(struct gspca_dev
*gspca_dev
,
340 const struct usb_device_id
*id
)
342 struct sd
*sd
= (struct sd
*) gspca_dev
;
346 cam
= &gspca_dev
->cam
;
347 cam
->cam_mode
= vga_mode
;
348 cam
->nmodes
= ARRAY_SIZE(vga_mode
);
351 /* Several of the supported CIF cameras share the same USB ID but
352 * require different initializations and different control settings.
353 * The same is true of the VGA cameras. Therefore, we are forced
354 * to start the initialization process in order to determine which
355 * camera is present. Some of the supported cameras require the
356 * memory pointer to be set to 0 as the very first item of business
357 * or else they will not stream. So we do that immediately.
359 err_code
= zero_the_pointer(gspca_dev
);
363 err_code
= stream_start(gspca_dev
);
367 /* Now, the query for sensor type. */
368 err_code
= cam_get_response16(gspca_dev
, 0x07, 1);
372 if (id
->idProduct
== 0x0110 || id
->idProduct
== 0x010e) {
373 sd
->cam_type
= CAM_TYPE_CIF
;
376 * All but one of the known CIF cameras share the same USB ID,
377 * but two different init routines are in use, and the control
378 * settings are different, too. We need to detect which camera
379 * of the two known varieties is connected!
381 * A list of known CIF cameras follows. They all report either
382 * 0200 for type 0 or 0300 for type 1.
383 * If you have another to report, please do
385 * Name sd->sensor_type reported by
387 * Sakar 56379 Spy-shot 0 T. Kilgore
388 * Innovage 0 T. Kilgore
389 * Vivitar Mini 0 H. De Goede
390 * Vivitar Mini 0 E. Rodriguez
391 * Vivitar Mini 1 T. Kilgore
392 * Elta-Media 8212dc 1 T. Kaiser
393 * Philips dig. keych. 1 T. Kilgore
394 * Trust Spyc@m 100 1 A. Jacobs
396 switch (gspca_dev
->usb_buf
[0]) {
404 pr_err("Unknown CIF Sensor id : %02x\n",
405 gspca_dev
->usb_buf
[1]);
408 gspca_dbg(gspca_dev
, D_PROBE
, "MR97310A CIF camera detected, sensor: %d\n",
411 sd
->cam_type
= CAM_TYPE_VGA
;
414 * Here is a table of the responses to the query for sensor
415 * type, from the known MR97310A VGA cameras. Six different
416 * cameras of which five share the same USB ID.
418 * Name gspca_dev->usb_buf[] sd->sensor_type
420 * Aiptek Pencam VGA+ 0300 0 1
421 * ION digital 0300 0 1
422 * Argus DC-1620 0450 1 0
423 * Argus QuickClix 0420 1 1
424 * Sakar 77379 Digital 0350 0 1
425 * Sakar 1638x CyberPix 0120 0 2
427 * Based upon these results, we assume default settings
428 * and then correct as necessary, as follows.
435 if (gspca_dev
->usb_buf
[0] == 0x01) {
437 } else if ((gspca_dev
->usb_buf
[0] != 0x03) &&
438 (gspca_dev
->usb_buf
[0] != 0x04)) {
439 pr_err("Unknown VGA Sensor id Byte 0: %02x\n",
440 gspca_dev
->usb_buf
[0]);
441 pr_err("Defaults assumed, may not work\n");
442 pr_err("Please report this\n");
444 /* Sakar Digital color needs to be adjusted. */
445 if ((gspca_dev
->usb_buf
[0] == 0x03) &&
446 (gspca_dev
->usb_buf
[1] == 0x50))
448 if (gspca_dev
->usb_buf
[0] == 0x04) {
450 switch (gspca_dev
->usb_buf
[1]) {
453 gspca_dbg(gspca_dev
, D_PROBE
, "sensor_type corrected to 0\n");
456 /* Nothing to do here. */
459 pr_err("Unknown VGA Sensor id Byte 1: %02x\n",
460 gspca_dev
->usb_buf
[1]);
461 pr_err("Defaults assumed, may not work\n");
462 pr_err("Please report this\n");
465 gspca_dbg(gspca_dev
, D_PROBE
, "MR97310A VGA camera detected, sensor: %d\n",
468 /* Stop streaming as we've started it only to probe the sensor type. */
471 if (force_sensor_type
!= -1) {
472 sd
->sensor_type
= !!force_sensor_type
;
473 gspca_dbg(gspca_dev
, D_PROBE
, "Forcing sensor type to: %d\n",
480 /* this function is called at probe and resume time */
481 static int sd_init(struct gspca_dev
*gspca_dev
)
486 static int start_cif_cam(struct gspca_dev
*gspca_dev
)
488 struct sd
*sd
= (struct sd
*) gspca_dev
;
489 __u8
*data
= gspca_dev
->usb_buf
;
491 static const __u8 startup_string
[] = {
495 0x00, /* Hsize/8 for 352 or 320 */
496 0x00, /* Vsize/4 for 288 or 240 */
497 0x13, /* or 0xbb, depends on sensor */
498 0x00, /* Hstart, depends on res. */
499 0x00, /* reserved ? */
500 0x00, /* Vstart, depends on res. and sensor */
501 0x50, /* 0x54 to get 176 or 160 */
505 /* Note: Some of the above descriptions guessed from MR97113A driver */
507 memcpy(data
, startup_string
, 11);
511 switch (gspca_dev
->pixfmt
.width
) {
513 data
[9] |= 0x04; /* reg 8, 2:1 scale down from 320 */
517 data
[3] = 0x28; /* reg 2, H size/8 */
518 data
[4] = 0x3c; /* reg 3, V size/4 */
519 data
[6] = 0x14; /* reg 5, H start */
520 data
[8] = 0x1a + sd
->sensor_type
; /* reg 7, V start */
523 data
[9] |= 0x04; /* reg 8, 2:1 scale down from 352 */
526 data
[3] = 0x2c; /* reg 2, H size/8 */
527 data
[4] = 0x48; /* reg 3, V size/4 */
528 data
[6] = 0x06; /* reg 5, H start */
529 data
[8] = 0x06 - sd
->sensor_type
; /* reg 7, V start */
532 err_code
= mr_write(gspca_dev
, 11);
536 if (!sd
->sensor_type
) {
537 static const struct sensor_w_data cif_sensor0_init_data
[] = {
538 {0x02, 0x00, {0x03, 0x5a, 0xb5, 0x01,
539 0x0f, 0x14, 0x0f, 0x10}, 8},
540 {0x0c, 0x00, {0x04, 0x01, 0x01, 0x00, 0x1f}, 5},
541 {0x12, 0x00, {0x07}, 1},
542 {0x1f, 0x00, {0x06}, 1},
543 {0x27, 0x00, {0x04}, 1},
544 {0x29, 0x00, {0x0c}, 1},
545 {0x40, 0x00, {0x40, 0x00, 0x04}, 3},
546 {0x50, 0x00, {0x60}, 1},
547 {0x60, 0x00, {0x06}, 1},
548 {0x6b, 0x00, {0x85, 0x85, 0xc8, 0xc8, 0xc8, 0xc8}, 6},
549 {0x72, 0x00, {0x1e, 0x56}, 2},
550 {0x75, 0x00, {0x58, 0x40, 0xa2, 0x02, 0x31, 0x02,
551 0x31, 0x80, 0x00}, 9},
552 {0x11, 0x00, {0x01}, 1},
555 err_code
= sensor_write_regs(gspca_dev
, cif_sensor0_init_data
,
556 ARRAY_SIZE(cif_sensor0_init_data
));
557 } else { /* sd->sensor_type = 1 */
558 static const struct sensor_w_data cif_sensor1_init_data
[] = {
559 /* Reg 3,4, 7,8 get set by the controls */
560 {0x02, 0x00, {0x10}, 1},
561 {0x05, 0x01, {0x22}, 1}, /* 5/6 also seen as 65h/32h */
562 {0x06, 0x01, {0x00}, 1},
563 {0x09, 0x02, {0x0e}, 1},
564 {0x0a, 0x02, {0x05}, 1},
565 {0x0b, 0x02, {0x05}, 1},
566 {0x0c, 0x02, {0x0f}, 1},
567 {0x0d, 0x02, {0x07}, 1},
568 {0x0e, 0x02, {0x0c}, 1},
569 {0x0f, 0x00, {0x00}, 1},
570 {0x10, 0x00, {0x06}, 1},
571 {0x11, 0x00, {0x07}, 1},
572 {0x12, 0x00, {0x00}, 1},
573 {0x13, 0x00, {0x01}, 1},
576 /* Without this command the cam won't work with USB-UHCI */
577 gspca_dev
->usb_buf
[0] = 0x0a;
578 gspca_dev
->usb_buf
[1] = 0x00;
579 err_code
= mr_write(gspca_dev
, 2);
582 err_code
= sensor_write_regs(gspca_dev
, cif_sensor1_init_data
,
583 ARRAY_SIZE(cif_sensor1_init_data
));
588 static int start_vga_cam(struct gspca_dev
*gspca_dev
)
590 struct sd
*sd
= (struct sd
*) gspca_dev
;
591 __u8
*data
= gspca_dev
->usb_buf
;
593 static const __u8 startup_string
[] =
594 {0x00, 0x0d, 0x01, 0x00, 0x00, 0x2b, 0x00, 0x00,
596 /* What some of these mean is explained in start_cif_cam(), above */
598 memcpy(data
, startup_string
, 11);
599 if (!sd
->sensor_type
) {
603 if (sd
->sensor_type
== 2) {
608 switch (gspca_dev
->pixfmt
.width
) {
610 data
[9] |= 0x0c; /* reg 8, 4:1 scale down */
613 data
[9] |= 0x04; /* reg 8, 2:1 scale down */
617 data
[3] = 0x50; /* reg 2, H size/8 */
618 data
[4] = 0x78; /* reg 3, V size/4 */
619 data
[6] = 0x04; /* reg 5, H start */
620 data
[8] = 0x03; /* reg 7, V start */
621 if (sd
->sensor_type
== 2) {
626 data
[8] = 0x04; /* Bayer tile shifted */
630 data
[9] |= 0x04; /* reg 8, 2:1 scale down */
633 data
[3] = 0x2c; /* reg 2, H size */
634 data
[4] = 0x48; /* reg 3, V size */
635 data
[6] = 0x94; /* reg 5, H start */
636 data
[8] = 0x63; /* reg 7, V start */
638 data
[8] = 0x64; /* Bayer tile shifted */
642 err_code
= mr_write(gspca_dev
, 11);
646 if (!sd
->sensor_type
) {
647 static const struct sensor_w_data vga_sensor0_init_data
[] = {
648 {0x01, 0x00, {0x0c, 0x00, 0x04}, 3},
649 {0x14, 0x00, {0x01, 0xe4, 0x02, 0x84}, 4},
650 {0x20, 0x00, {0x00, 0x80, 0x00, 0x08}, 4},
651 {0x25, 0x00, {0x03, 0xa9, 0x80}, 3},
652 {0x30, 0x00, {0x30, 0x18, 0x10, 0x18}, 4},
655 err_code
= sensor_write_regs(gspca_dev
, vga_sensor0_init_data
,
656 ARRAY_SIZE(vga_sensor0_init_data
));
657 } else if (sd
->sensor_type
== 1) {
658 static const struct sensor_w_data color_adj
[] = {
659 {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
660 /* adjusted blue, green, red gain correct
661 too much blue from the Sakar Digital */
662 0x05, 0x01, 0x04}, 8}
665 static const struct sensor_w_data color_no_adj
[] = {
666 {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
667 /* default blue, green, red gain settings */
668 0x07, 0x00, 0x01}, 8}
671 static const struct sensor_w_data vga_sensor1_init_data
[] = {
672 {0x11, 0x04, {0x01}, 1},
673 {0x0a, 0x00, {0x00, 0x01, 0x00, 0x00, 0x01,
674 /* These settings may be better for some cameras */
675 /* {0x0a, 0x00, {0x01, 0x06, 0x00, 0x00, 0x01, */
677 {0x11, 0x04, {0x01}, 1},
678 {0x12, 0x00, {0x00, 0x63, 0x00, 0x70, 0x00, 0x00}, 6},
679 {0x11, 0x04, {0x01}, 1},
684 err_code
= sensor_write_regs(gspca_dev
, color_adj
,
685 ARRAY_SIZE(color_adj
));
687 err_code
= sensor_write_regs(gspca_dev
, color_no_adj
,
688 ARRAY_SIZE(color_no_adj
));
693 err_code
= sensor_write_regs(gspca_dev
, vga_sensor1_init_data
,
694 ARRAY_SIZE(vga_sensor1_init_data
));
695 } else { /* sensor type == 2 */
696 static const struct sensor_w_data vga_sensor2_init_data
[] = {
698 {0x01, 0x00, {0x48}, 1},
699 {0x02, 0x00, {0x22}, 1},
700 /* Reg 3 msb and 4 is lsb of the exposure setting*/
701 {0x05, 0x00, {0x10}, 1},
702 {0x06, 0x00, {0x00}, 1},
703 {0x07, 0x00, {0x00}, 1},
704 {0x08, 0x00, {0x00}, 1},
705 {0x09, 0x00, {0x00}, 1},
706 /* The following are used in the gain control
707 * which is BTW completely borked in the OEM driver
708 * The values for each color go from 0 to 0x7ff
709 *{0x0a, 0x00, {0x01}, 1}, green1 gain msb
710 *{0x0b, 0x00, {0x10}, 1}, green1 gain lsb
711 *{0x0c, 0x00, {0x01}, 1}, red gain msb
712 *{0x0d, 0x00, {0x10}, 1}, red gain lsb
713 *{0x0e, 0x00, {0x01}, 1}, blue gain msb
714 *{0x0f, 0x00, {0x10}, 1}, blue gain lsb
715 *{0x10, 0x00, {0x01}, 1}, green2 gain msb
716 *{0x11, 0x00, {0x10}, 1}, green2 gain lsb
718 {0x12, 0x00, {0x00}, 1},
719 {0x13, 0x00, {0x04}, 1}, /* weird effect on colors */
720 {0x14, 0x00, {0x00}, 1},
721 {0x15, 0x00, {0x06}, 1},
722 {0x16, 0x00, {0x01}, 1},
723 {0x17, 0x00, {0xe2}, 1}, /* vertical alignment */
724 {0x18, 0x00, {0x02}, 1},
725 {0x19, 0x00, {0x82}, 1}, /* don't mess with */
726 {0x1a, 0x00, {0x00}, 1},
727 {0x1b, 0x00, {0x20}, 1},
728 /* {0x1c, 0x00, {0x17}, 1}, contrast control */
729 {0x1d, 0x00, {0x80}, 1}, /* moving causes a mess */
730 {0x1e, 0x00, {0x08}, 1}, /* moving jams the camera */
731 {0x1f, 0x00, {0x0c}, 1},
732 {0x20, 0x00, {0x00}, 1},
735 err_code
= sensor_write_regs(gspca_dev
, vga_sensor2_init_data
,
736 ARRAY_SIZE(vga_sensor2_init_data
));
741 static int sd_start(struct gspca_dev
*gspca_dev
)
743 struct sd
*sd
= (struct sd
*) gspca_dev
;
748 /* Some of the VGA cameras require the memory pointer
749 * to be set to 0 again. We have been forced to start the
750 * stream in sd_config() to detect the hardware, and closed it.
751 * Thus, we need here to do a completely fresh and clean start. */
752 err_code
= zero_the_pointer(gspca_dev
);
756 err_code
= stream_start(gspca_dev
);
760 if (sd
->cam_type
== CAM_TYPE_CIF
) {
761 err_code
= start_cif_cam(gspca_dev
);
763 err_code
= start_vga_cam(gspca_dev
);
768 return isoc_enable(gspca_dev
);
771 static void sd_stopN(struct gspca_dev
*gspca_dev
)
773 struct sd
*sd
= (struct sd
*) gspca_dev
;
775 stream_stop(gspca_dev
);
776 /* Not all the cams need this, but even if not, probably a good idea */
777 zero_the_pointer(gspca_dev
);
782 static void setbrightness(struct gspca_dev
*gspca_dev
, s32 val
)
784 struct sd
*sd
= (struct sd
*) gspca_dev
;
785 u8 sign_reg
= 7; /* This reg and the next one used on CIF cams. */
786 u8 value_reg
= 8; /* VGA cams seem to use regs 0x0b and 0x0c */
787 static const u8 quick_clix_table
[] =
788 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
789 { 0, 4, 8, 12, 1, 2, 3, 5, 6, 9, 7, 10, 13, 11, 14, 15};
790 if (sd
->cam_type
== CAM_TYPE_VGA
) {
795 /* Note register 7 is also seen as 0x8x or 0xCx in some dumps */
797 sensor_write1(gspca_dev
, sign_reg
, 0x00);
799 sensor_write1(gspca_dev
, sign_reg
, 0x01);
802 /* Use lookup table for funky Argus QuickClix brightness */
804 val
= quick_clix_table
[val
];
806 sensor_write1(gspca_dev
, value_reg
, val
);
809 static void setexposure(struct gspca_dev
*gspca_dev
, s32 expo
, s32 min_clockdiv
)
811 struct sd
*sd
= (struct sd
*) gspca_dev
;
812 int exposure
= MR97310A_EXPOSURE_DEFAULT
;
815 if (sd
->cam_type
== CAM_TYPE_CIF
&& sd
->sensor_type
== 1) {
816 /* This cam does not like exposure settings < 300,
817 so scale 0 - 4095 to 300 - 4095 */
818 exposure
= (expo
* 9267) / 10000 + 300;
819 sensor_write1(gspca_dev
, 3, exposure
>> 4);
820 sensor_write1(gspca_dev
, 4, exposure
& 0x0f);
821 } else if (sd
->sensor_type
== 2) {
824 sensor_write1(gspca_dev
, 3, exposure
>> 8);
825 sensor_write1(gspca_dev
, 4, exposure
& 0xff);
827 /* We have both a clock divider and an exposure register.
828 We first calculate the clock divider, as that determines
829 the maximum exposure and then we calculate the exposure
830 register setting (which goes from 0 - 511).
832 Note our 0 - 4095 exposure is mapped to 0 - 511
833 milliseconds exposure time */
834 u8 clockdiv
= (60 * expo
+ 7999) / 8000;
836 /* Limit framerate to not exceed usb bandwidth */
837 if (clockdiv
< min_clockdiv
&& gspca_dev
->pixfmt
.width
>= 320)
838 clockdiv
= min_clockdiv
;
839 else if (clockdiv
< 2)
842 if (sd
->cam_type
== CAM_TYPE_VGA
&& clockdiv
< 4)
845 /* Frame exposure time in ms = 1000 * clockdiv / 60 ->
846 exposure = (sd->exposure / 8) * 511 / (1000 * clockdiv / 60) */
847 exposure
= (60 * 511 * expo
) / (8000 * clockdiv
);
851 /* exposure register value is reversed! */
852 exposure
= 511 - exposure
;
854 buf
[0] = exposure
& 0xff;
855 buf
[1] = exposure
>> 8;
856 sensor_write_reg(gspca_dev
, 0x0e, 0, buf
, 2);
857 sensor_write1(gspca_dev
, 0x02, clockdiv
);
861 static void setgain(struct gspca_dev
*gspca_dev
, s32 val
)
863 struct sd
*sd
= (struct sd
*) gspca_dev
;
866 if (sd
->cam_type
== CAM_TYPE_CIF
&& sd
->sensor_type
== 1)
867 sensor_write1(gspca_dev
, 0x0e, val
);
868 else if (sd
->cam_type
== CAM_TYPE_VGA
&& sd
->sensor_type
== 2)
869 for (gainreg
= 0x0a; gainreg
< 0x11; gainreg
+= 2) {
870 sensor_write1(gspca_dev
, gainreg
, val
>> 8);
871 sensor_write1(gspca_dev
, gainreg
+ 1, val
& 0xff);
874 sensor_write1(gspca_dev
, 0x10, val
);
877 static void setcontrast(struct gspca_dev
*gspca_dev
, s32 val
)
879 sensor_write1(gspca_dev
, 0x1c, val
);
882 static int sd_s_ctrl(struct v4l2_ctrl
*ctrl
)
884 struct gspca_dev
*gspca_dev
=
885 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
886 struct sd
*sd
= (struct sd
*)gspca_dev
;
888 gspca_dev
->usb_err
= 0;
890 if (!gspca_dev
->streaming
)
894 case V4L2_CID_BRIGHTNESS
:
895 setbrightness(gspca_dev
, ctrl
->val
);
897 case V4L2_CID_CONTRAST
:
898 setcontrast(gspca_dev
, ctrl
->val
);
900 case V4L2_CID_EXPOSURE
:
901 setexposure(gspca_dev
, sd
->exposure
->val
,
902 sd
->min_clockdiv
? sd
->min_clockdiv
->val
: 0);
905 setgain(gspca_dev
, ctrl
->val
);
908 return gspca_dev
->usb_err
;
911 static const struct v4l2_ctrl_ops sd_ctrl_ops
= {
915 static int sd_init_controls(struct gspca_dev
*gspca_dev
)
917 struct sd
*sd
= (struct sd
*)gspca_dev
;
918 struct v4l2_ctrl_handler
*hdl
= &gspca_dev
->ctrl_handler
;
919 static const struct v4l2_ctrl_config clockdiv
= {
921 .id
= MR97310A_CID_CLOCKDIV
,
922 .type
= V4L2_CTRL_TYPE_INTEGER
,
923 .name
= "Minimum Clock Divider",
924 .min
= MR97310A_MIN_CLOCKDIV_MIN
,
925 .max
= MR97310A_MIN_CLOCKDIV_MAX
,
927 .def
= MR97310A_MIN_CLOCKDIV_DEFAULT
,
929 bool has_brightness
= false;
930 bool has_argus_brightness
= false;
931 bool has_contrast
= false;
932 bool has_gain
= false;
933 bool has_cs_gain
= false;
934 bool has_exposure
= false;
935 bool has_clockdiv
= false;
937 gspca_dev
->vdev
.ctrl_handler
= hdl
;
938 v4l2_ctrl_handler_init(hdl
, 4);
940 /* Setup controls depending on camera type */
941 if (sd
->cam_type
== CAM_TYPE_CIF
) {
942 /* No brightness for sensor_type 0 */
943 if (sd
->sensor_type
== 0)
944 has_exposure
= has_gain
= has_clockdiv
= true;
946 has_exposure
= has_gain
= has_brightness
= true;
948 /* All controls need to be disabled if VGA sensor_type is 0 */
949 if (sd
->sensor_type
== 0)
951 else if (sd
->sensor_type
== 2)
952 has_exposure
= has_cs_gain
= has_contrast
= true;
953 else if (sd
->do_lcd_stop
)
954 has_exposure
= has_gain
= has_argus_brightness
=
957 has_exposure
= has_gain
= has_brightness
=
961 /* Separate brightness control description for Argus QuickClix as it has
962 * different limits from the other mr97310a cameras, and separate gain
963 * control for Sakar CyberPix camera. */
965 * This control is disabled for CIF type 1 and VGA type 0 cameras.
966 * It does not quite act linearly for the Argus QuickClix camera,
967 * but it does control brightness. The values are 0 - 15 only, and
968 * the table above makes them act consecutively.
971 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
972 V4L2_CID_BRIGHTNESS
, -254, 255, 1,
973 MR97310A_BRIGHTNESS_DEFAULT
);
974 else if (has_argus_brightness
)
975 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
976 V4L2_CID_BRIGHTNESS
, 0, 15, 1,
977 MR97310A_BRIGHTNESS_DEFAULT
);
979 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
980 V4L2_CID_CONTRAST
, MR97310A_CONTRAST_MIN
,
981 MR97310A_CONTRAST_MAX
, 1, MR97310A_CONTRAST_DEFAULT
);
983 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
984 V4L2_CID_GAIN
, MR97310A_GAIN_MIN
, MR97310A_GAIN_MAX
,
985 1, MR97310A_GAIN_DEFAULT
);
986 else if (has_cs_gain
)
987 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
, V4L2_CID_GAIN
,
988 MR97310A_CS_GAIN_MIN
, MR97310A_CS_GAIN_MAX
,
989 1, MR97310A_CS_GAIN_DEFAULT
);
991 sd
->exposure
= v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
992 V4L2_CID_EXPOSURE
, MR97310A_EXPOSURE_MIN
,
993 MR97310A_EXPOSURE_MAX
, 1, MR97310A_EXPOSURE_DEFAULT
);
995 sd
->min_clockdiv
= v4l2_ctrl_new_custom(hdl
, &clockdiv
, NULL
);
998 pr_err("Could not initialize controls\n");
1001 if (has_exposure
&& has_clockdiv
)
1002 v4l2_ctrl_cluster(2, &sd
->exposure
);
1006 /* Include pac common sof detection functions */
1007 #include "pac_common.h"
1009 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
1010 u8
*data
, /* isoc packet */
1011 int len
) /* iso packet length */
1013 struct sd
*sd
= (struct sd
*) gspca_dev
;
1016 sof
= pac_find_sof(gspca_dev
, &sd
->sof_read
, data
, len
);
1020 /* finish decoding current frame */
1022 if (n
> sizeof pac_sof_marker
)
1023 n
-= sizeof pac_sof_marker
;
1026 gspca_frame_add(gspca_dev
, LAST_PACKET
,
1028 /* Start next frame. */
1029 gspca_frame_add(gspca_dev
, FIRST_PACKET
,
1030 pac_sof_marker
, sizeof pac_sof_marker
);
1034 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
, len
);
1037 /* sub-driver description */
1038 static const struct sd_desc sd_desc
= {
1039 .name
= MODULE_NAME
,
1040 .config
= sd_config
,
1042 .init_controls
= sd_init_controls
,
1045 .pkt_scan
= sd_pkt_scan
,
1048 /* -- module initialisation -- */
1049 static const struct usb_device_id device_table
[] = {
1050 {USB_DEVICE(0x08ca, 0x0110)}, /* Trust Spyc@m 100 */
1051 {USB_DEVICE(0x08ca, 0x0111)}, /* Aiptek Pencam VGA+ */
1052 {USB_DEVICE(0x093a, 0x010f)}, /* All other known MR97310A VGA cams */
1053 {USB_DEVICE(0x093a, 0x010e)}, /* All known MR97310A CIF cams */
1056 MODULE_DEVICE_TABLE(usb
, device_table
);
1058 /* -- device connect -- */
1059 static int sd_probe(struct usb_interface
*intf
,
1060 const struct usb_device_id
*id
)
1062 return gspca_dev_probe(intf
, id
, &sd_desc
, sizeof(struct sd
),
1066 static struct usb_driver sd_driver
= {
1067 .name
= MODULE_NAME
,
1068 .id_table
= device_table
,
1070 .disconnect
= gspca_disconnect
,
1072 .suspend
= gspca_suspend
,
1073 .resume
= gspca_resume
,
1074 .reset_resume
= gspca_resume
,
1078 module_usb_driver(sd_driver
);