43 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
45 #define MODULE_NAME "mr97310a"
49 #define CAM_TYPE_CIF 0
50 #define CAM_TYPE_VGA 1
52 #define MR97310A_BRIGHTNESS_DEFAULT 0
54 #define MR97310A_EXPOSURE_MIN 0
55 #define MR97310A_EXPOSURE_MAX 4095
56 #define MR97310A_EXPOSURE_DEFAULT 1000
58 #define MR97310A_GAIN_MIN 0
59 #define MR97310A_GAIN_MAX 31
60 #define MR97310A_GAIN_DEFAULT 25
62 #define MR97310A_CONTRAST_MIN 0
63 #define MR97310A_CONTRAST_MAX 31
64 #define MR97310A_CONTRAST_DEFAULT 23
66 #define MR97310A_CS_GAIN_MIN 0
67 #define MR97310A_CS_GAIN_MAX 0x7ff
68 #define MR97310A_CS_GAIN_DEFAULT 0x110
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
81 static int force_sensor_type = -1;
83 MODULE_PARM_DESC(force_sensor_type,
"Force sensor type (-1 (auto), 0 or 1)");
111 .sizeimage = 160 * 120,
116 .sizeimage = 176 * 144,
121 .sizeimage = 320 * 240,
126 .sizeimage = 352 * 288,
131 .sizeimage = 640 * 480,
142 usb_sndbulkpipe(gspca_dev->
dev, 4),
145 pr_err(
"reg write [%02x] error %d\n",
151 static int mr_read(
struct gspca_dev *gspca_dev,
int len)
156 usb_rcvbulkpipe(gspca_dev->
dev, 3),
159 pr_err(
"reg read [%02x] error %d\n",
164 static int sensor_write_reg(
struct gspca_dev *gspca_dev,
u8 reg,
u8 flags,
172 return mr_write(gspca_dev, len + 3);
175 static int sensor_write_regs(
struct gspca_dev *gspca_dev,
180 for (i = 0; i < len; i++) {
181 rc = sensor_write_reg(gspca_dev, data[i].
reg, data[i].
flags,
182 data[i].data, data[i].len);
190 static int sensor_write1(
struct gspca_dev *gspca_dev,
u8 reg,
u8 data)
192 struct sd *
sd = (
struct sd *) gspca_dev;
198 rc = sensor_write_reg(gspca_dev, reg, 0x01, &buf, 1);
201 rc = sensor_write_reg(gspca_dev, reg, 0x00, &buf, 1);
208 rc = sensor_write_reg(gspca_dev, confirm_reg, 0x00, &buf, 1);
215 static int cam_get_response16(
struct gspca_dev *gspca_dev,
u8 reg,
int verbose)
220 err_code = mr_write(gspca_dev, 1);
224 err_code = mr_read(gspca_dev, 16);
229 PDEBUG(D_PROBE,
"Register: %02x reads %02x%02x%02x", reg,
237 static int zero_the_pointer(
struct gspca_dev *gspca_dev)
244 err_code = cam_get_response16(gspca_dev, 0x21, 0);
250 err_code = mr_write(gspca_dev, 2);
254 err_code = cam_get_response16(gspca_dev, 0x21, 0);
260 err_code = mr_write(gspca_dev, 2);
264 err_code = cam_get_response16(gspca_dev, 0x21, 0);
270 err_code = mr_write(gspca_dev, 2);
274 err_code = cam_get_response16(gspca_dev, 0x21, 0);
280 err_code = mr_write(gspca_dev, 2);
284 while (status != 0x0a && tries < 256) {
285 err_code = cam_get_response16(gspca_dev, 0x21, 0);
298 err_code = mr_write(gspca_dev, 2);
302 err_code = cam_get_response16(gspca_dev, 0x21, 0);
310 err_code = mr_write(gspca_dev, 1);
314 err_code = mr_read(gspca_dev, 16);
321 static int stream_start(
struct gspca_dev *gspca_dev)
325 return mr_write(gspca_dev, 2);
328 static void stream_stop(
struct gspca_dev *gspca_dev)
332 if (mr_write(gspca_dev, 2) < 0)
336 static void lcd_stop(
struct gspca_dev *gspca_dev)
340 if (mr_write(gspca_dev, 2) < 0)
344 static int isoc_enable(
struct gspca_dev *gspca_dev)
348 return mr_write(gspca_dev, 2);
352 static int sd_config(
struct gspca_dev *gspca_dev,
355 struct sd *sd = (
struct sd *) gspca_dev;
359 cam = &gspca_dev->
cam;
372 err_code = zero_the_pointer(gspca_dev);
376 err_code = stream_start(gspca_dev);
381 err_code = cam_get_response16(gspca_dev, 0x07, 1);
409 switch (gspca_dev->
usb_buf[0]) {
417 pr_err(
"Unknown CIF Sensor id : %02x\n",
421 PDEBUG(D_PROBE,
"MR97310A CIF camera detected, sensor: %d",
448 if (gspca_dev->
usb_buf[0] == 0x01) {
450 }
else if ((gspca_dev->
usb_buf[0] != 0x03) &&
451 (gspca_dev->
usb_buf[0] != 0x04)) {
452 pr_err(
"Unknown VGA Sensor id Byte 0: %02x\n",
454 pr_err(
"Defaults assumed, may not work\n");
455 pr_err(
"Please report this\n");
458 if ((gspca_dev->
usb_buf[0] == 0x03) &&
459 (gspca_dev->
usb_buf[1] == 0x50))
461 if (gspca_dev->
usb_buf[0] == 0x04) {
463 switch (gspca_dev->
usb_buf[1]) {
466 PDEBUG(D_PROBE,
"sensor_type corrected to 0");
472 pr_err(
"Unknown VGA Sensor id Byte 1: %02x\n",
474 pr_err(
"Defaults assumed, may not work\n");
475 pr_err(
"Please report this\n");
478 PDEBUG(D_PROBE,
"MR97310A VGA camera detected, sensor: %d",
484 if (force_sensor_type != -1) {
486 PDEBUG(D_PROBE,
"Forcing sensor type to: %d",
494 static int sd_init(
struct gspca_dev *gspca_dev)
499 static int start_cif_cam(
struct gspca_dev *gspca_dev)
501 struct sd *sd = (
struct sd *) gspca_dev;
504 static const __u8 startup_string[] = {
520 memcpy(data, startup_string, 11);
524 switch (gspca_dev->
width) {
545 err_code = mr_write(gspca_dev, 11);
550 static const struct sensor_w_data cif_sensor0_init_data[] = {
551 {0x02, 0x00, {0x03, 0x5a, 0xb5, 0x01,
552 0x0f, 0x14, 0x0f, 0x10}, 8},
553 {0x0c, 0x00, {0x04, 0x01, 0x01, 0x00, 0x1f}, 5},
554 {0x12, 0x00, {0x07}, 1},
555 {0x1f, 0x00, {0x06}, 1},
556 {0x27, 0x00, {0x04}, 1},
557 {0x29, 0x00, {0x0c}, 1},
558 {0x40, 0x00, {0x40, 0x00, 0x04}, 3},
559 {0x50, 0x00, {0x60}, 1},
560 {0x60, 0x00, {0x06}, 1},
561 {0x6b, 0x00, {0x85, 0x85, 0xc8, 0xc8, 0xc8, 0xc8}, 6},
562 {0x72, 0x00, {0x1e, 0x56}, 2},
563 {0x75, 0x00, {0x58, 0x40, 0xa2, 0x02, 0x31, 0x02,
564 0x31, 0x80, 0x00}, 9},
565 {0x11, 0x00, {0x01}, 1},
568 err_code = sensor_write_regs(gspca_dev, cif_sensor0_init_data,
571 static const struct sensor_w_data cif_sensor1_init_data[] = {
573 {0x02, 0x00, {0x10}, 1},
574 {0x05, 0x01, {0x22}, 1},
575 {0x06, 0x01, {0x00}, 1},
576 {0x09, 0x02, {0x0e}, 1},
577 {0x0a, 0x02, {0x05}, 1},
578 {0x0b, 0x02, {0x05}, 1},
579 {0x0c, 0x02, {0x0f}, 1},
580 {0x0d, 0x02, {0x07}, 1},
581 {0x0e, 0x02, {0x0c}, 1},
582 {0x0f, 0x00, {0x00}, 1},
583 {0x10, 0x00, {0x06}, 1},
584 {0x11, 0x00, {0x07}, 1},
585 {0x12, 0x00, {0x00}, 1},
586 {0x13, 0x00, {0x01}, 1},
592 err_code = mr_write(gspca_dev, 2);
595 err_code = sensor_write_regs(gspca_dev, cif_sensor1_init_data,
601 static int start_vga_cam(
struct gspca_dev *gspca_dev)
603 struct sd *sd = (
struct sd *) gspca_dev;
606 static const __u8 startup_string[] =
607 {0x00, 0x0d, 0x01, 0x00, 0x00, 0x2b, 0x00, 0x00,
611 memcpy(data, startup_string, 11);
621 switch (gspca_dev->
width) {
655 err_code = mr_write(gspca_dev, 11);
660 static const struct sensor_w_data vga_sensor0_init_data[] = {
661 {0x01, 0x00, {0x0c, 0x00, 0x04}, 3},
662 {0x14, 0x00, {0x01, 0xe4, 0x02, 0x84}, 4},
663 {0x20, 0x00, {0x00, 0x80, 0x00, 0x08}, 4},
664 {0x25, 0x00, {0x03, 0xa9, 0x80}, 3},
665 {0x30, 0x00, {0x30, 0x18, 0x10, 0x18}, 4},
668 err_code = sensor_write_regs(gspca_dev, vga_sensor0_init_data,
672 {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
675 0x05, 0x01, 0x04}, 8}
679 {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
681 0x07, 0x00, 0x01}, 8}
684 static const struct sensor_w_data vga_sensor1_init_data[] = {
685 {0x11, 0x04, {0x01}, 1},
686 {0x0a, 0x00, {0x00, 0x01, 0x00, 0x00, 0x01,
690 {0x11, 0x04, {0x01}, 1},
691 {0x12, 0x00, {0x00, 0x63, 0x00, 0x70, 0x00, 0x00}, 6},
692 {0x11, 0x04, {0x01}, 1},
697 err_code = sensor_write_regs(gspca_dev, color_adj,
700 err_code = sensor_write_regs(gspca_dev, color_no_adj,
706 err_code = sensor_write_regs(gspca_dev, vga_sensor1_init_data,
709 static const struct sensor_w_data vga_sensor2_init_data[] = {
711 {0x01, 0x00, {0x48}, 1},
712 {0x02, 0x00, {0x22}, 1},
714 {0x05, 0x00, {0x10}, 1},
715 {0x06, 0x00, {0x00}, 1},
716 {0x07, 0x00, {0x00}, 1},
717 {0x08, 0x00, {0x00}, 1},
718 {0x09, 0x00, {0x00}, 1},
731 {0x12, 0x00, {0x00}, 1},
732 {0x13, 0x00, {0x04}, 1},
733 {0x14, 0x00, {0x00}, 1},
734 {0x15, 0x00, {0x06}, 1},
735 {0x16, 0x00, {0x01}, 1},
736 {0x17, 0x00, {0xe2}, 1},
737 {0x18, 0x00, {0x02}, 1},
738 {0x19, 0x00, {0x82}, 1},
739 {0x1a, 0x00, {0x00}, 1},
740 {0x1b, 0x00, {0x20}, 1},
742 {0x1d, 0x00, {0x80}, 1},
743 {0x1e, 0x00, {0x08}, 1},
744 {0x1f, 0x00, {0x0c}, 1},
745 {0x20, 0x00, {0x00}, 1},
748 err_code = sensor_write_regs(gspca_dev, vga_sensor2_init_data,
754 static int sd_start(
struct gspca_dev *gspca_dev)
756 struct sd *sd = (
struct sd *) gspca_dev;
765 err_code = zero_the_pointer(gspca_dev);
769 err_code = stream_start(gspca_dev);
774 err_code = start_cif_cam(gspca_dev);
776 err_code = start_vga_cam(gspca_dev);
781 return isoc_enable(gspca_dev);
784 static void sd_stopN(
struct gspca_dev *gspca_dev)
786 struct sd *sd = (
struct sd *) gspca_dev;
788 stream_stop(gspca_dev);
790 zero_the_pointer(gspca_dev);
795 static void setbrightness(
struct gspca_dev *gspca_dev,
s32 val)
797 struct sd *sd = (
struct sd *) gspca_dev;
800 static const u8 quick_clix_table[] =
802 { 0, 4, 8, 12, 1, 2, 3, 5, 6, 9, 7, 10, 13, 11, 14, 15};
810 sensor_write1(gspca_dev, sign_reg, 0x00);
812 sensor_write1(gspca_dev, sign_reg, 0x01);
817 val = quick_clix_table[
val];
819 sensor_write1(gspca_dev, value_reg, val);
824 struct sd *sd = (
struct sd *) gspca_dev;
831 exposure = (expo * 9267) / 10000 + 300;
832 sensor_write1(gspca_dev, 3, exposure >> 4);
833 sensor_write1(gspca_dev, 4, exposure & 0x0f);
837 sensor_write1(gspca_dev, 3, exposure >> 8);
838 sensor_write1(gspca_dev, 4, exposure & 0xff);
850 if (clockdiv < min_clockdiv && gspca_dev->
width >= 320)
852 else if (clockdiv < 2)
860 exposure = (60 * 511 * expo) / (8000 * clockdiv);
867 buf[0] = exposure & 0xff;
868 buf[1] = exposure >> 8;
869 sensor_write_reg(gspca_dev, 0x0e, 0, buf, 2);
870 sensor_write1(gspca_dev, 0x02, clockdiv);
874 static void setgain(
struct gspca_dev *gspca_dev,
s32 val)
876 struct sd *sd = (
struct sd *) gspca_dev;
880 sensor_write1(gspca_dev, 0x0e, val);
882 for (gainreg = 0x0a; gainreg < 0x11; gainreg += 2) {
883 sensor_write1(gspca_dev, gainreg, val >> 8);
884 sensor_write1(gspca_dev, gainreg + 1, val & 0xff);
887 sensor_write1(gspca_dev, 0x10, val);
890 static void setcontrast(
struct gspca_dev *gspca_dev,
s32 val)
892 sensor_write1(gspca_dev, 0x1c, val);
897 struct gspca_dev *gspca_dev =
899 struct sd *sd = (
struct sd *)gspca_dev;
908 setbrightness(gspca_dev, ctrl->
val);
911 setcontrast(gspca_dev, ctrl->
val);
914 setexposure(gspca_dev, sd->
exposure->val,
918 setgain(gspca_dev, ctrl->
val);
928 static int sd_init_controls(
struct gspca_dev *gspca_dev)
930 struct sd *sd = (
struct sd *)gspca_dev;
936 .name =
"Minimum Clock Divider",
942 bool has_brightness =
false;
943 bool has_argus_brightness =
false;
944 bool has_contrast =
false;
945 bool has_gain =
false;
946 bool has_cs_gain =
false;
947 bool has_exposure =
false;
948 bool has_clockdiv =
false;
950 gspca_dev->
vdev.ctrl_handler = hdl;
957 has_exposure = has_gain = has_clockdiv =
true;
959 has_exposure = has_gain = has_brightness =
true;
965 has_exposure = has_cs_gain = has_contrast =
true;
967 has_exposure = has_gain = has_argus_brightness =
970 has_exposure = has_gain = has_brightness =
987 else if (has_argus_brightness)
999 else if (has_cs_gain)
1011 pr_err(
"Could not initialize controls\n");
1014 if (has_exposure && has_clockdiv)
1022 static void sd_pkt_scan(
struct gspca_dev *gspca_dev,
1026 struct sd *sd = (
struct sd *) gspca_dev;
1029 sof = pac_find_sof(&sd->
sof_read, data, len);
1035 if (n >
sizeof pac_sof_marker)
1036 n -=
sizeof pac_sof_marker;
1043 pac_sof_marker,
sizeof pac_sof_marker);
1053 .config = sd_config,
1055 .init_controls = sd_init_controls,
1058 .pkt_scan = sd_pkt_scan,
1063 {USB_DEVICE(0x08ca, 0x0110)},
1064 {USB_DEVICE(0x08ca, 0x0111)},
1065 {USB_DEVICE(0x093a, 0x010f)},
1066 {USB_DEVICE(0x093a, 0x010e)},
1079 static struct usb_driver sd_driver = {
1081 .id_table = device_table,
1085 .suspend = gspca_suspend,
1086 .resume = gspca_resume,
1087 .reset_resume = gspca_resume,