23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #define MODULE_NAME "spca561"
27 #include <linux/input.h>
34 #define EXPOSURE_MAX (2047 + 325)
54 #define AG_CNT_START 13
60 .sizeimage = 160 * 120,
65 .sizeimage = 176 * 144,
70 .sizeimage = 320 * 240 * 4 / 8,
75 .sizeimage = 352 * 288 * 4 / 8,
83 .sizeimage = 160 * 120,
88 .sizeimage = 176 * 144,
93 .sizeimage = 320 * 240,
98 .sizeimage = 352 * 288,
109 #define SPCA561_OFFSET_SNAP 1
110 #define SPCA561_OFFSET_TYPE 2
111 #define SPCA561_OFFSET_COMPRESS 3
112 #define SPCA561_OFFSET_FRAMSEQ 4
113 #define SPCA561_OFFSET_GPIO 5
114 #define SPCA561_OFFSET_USBBUFF 6
115 #define SPCA561_OFFSET_WIN2GRAVE 7
116 #define SPCA561_OFFSET_WIN2RAVE 8
117 #define SPCA561_OFFSET_WIN2BAVE 9
118 #define SPCA561_OFFSET_WIN2GBAVE 10
119 #define SPCA561_OFFSET_WIN1GRAVE 11
120 #define SPCA561_OFFSET_WIN1RAVE 12
121 #define SPCA561_OFFSET_WIN1BAVE 13
122 #define SPCA561_OFFSET_WIN1GBAVE 14
123 #define SPCA561_OFFSET_FREQ 15
124 #define SPCA561_OFFSET_VSYNC 16
125 #define SPCA561_INDEX_I2C_BASE 0x8800
126 #define SPCA561_SNAPBIT 0x20
127 #define SPCA561_SNAPCTRL 0x40
129 static const u16 rev72a_reset[][2] = {
135 static const __u16 rev72a_init_data1[][2] = {
144 static const u16 rev72a_init_sensor1[][2] = {
159 static const __u16 rev72a_init_data2[][2] = {
197 static const u16 rev72a_init_sensor2[][2] = {
210 static const __u16 Pb100_1map8300[][2] = {
225 static const __u16 Pb100_2map8300[][2] = {
232 static const __u16 spca561_161rev12A_data1[][2] = {
241 static const __u16 spca561_161rev12A_data2[][2] = {
295 value, index,
NULL, 0, 500);
296 PDEBUG(D_USBO,
"reg write: 0x%02x:0x%02x", index, value);
298 pr_err(
"reg write: error %d\n", ret);
304 struct usb_device *dev = gspca_dev->
dev;
308 while (data[i][1] != 0) {
309 reg_w_val(dev, data[i][1], data[i][0]);
315 static void reg_r(
struct gspca_dev *gspca_dev,
319 usb_rcvctrlpipe(gspca_dev->
dev, 0),
323 index, gspca_dev->
usb_buf, length, 500);
327 static void reg_w_buf(
struct gspca_dev *gspca_dev,
331 usb_sndctrlpipe(gspca_dev->
dev, 0),
335 index, gspca_dev->
usb_buf, len, 500);
342 reg_w_val(gspca_dev->
dev, 0x8801, reg);
343 reg_w_val(gspca_dev->
dev, 0x8805, value);
344 reg_w_val(gspca_dev->
dev, 0x8800, value >> 8);
346 reg_r(gspca_dev, 0x8803, 1);
358 reg_w_val(gspca_dev->
dev, 0x8804, 0x92);
359 reg_w_val(gspca_dev->
dev, 0x8801, reg);
360 reg_w_val(gspca_dev->
dev, 0x8802, mode | 0x01);
362 reg_r(gspca_dev, 0x8803, 1);
364 reg_r(gspca_dev, 0x8800, 1);
366 reg_r(gspca_dev, 0x8805, 1);
367 return ((
int) value << 8) | gspca_dev->
usb_buf[0];
374 static void sensor_mapwrite(
struct gspca_dev *gspca_dev,
375 const __u16 (*sensormap)[2])
377 while ((*sensormap)[0]) {
378 gspca_dev->
usb_buf[0] = (*sensormap)[1];
379 gspca_dev->
usb_buf[1] = (*sensormap)[1] >> 8;
380 reg_w_buf(gspca_dev, (*sensormap)[0], 2);
385 static void write_sensor_72a(
struct gspca_dev *gspca_dev,
386 const __u16 (*sensor)[2])
388 while ((*sensor)[0]) {
389 i2c_write(gspca_dev, (*sensor)[1], (*sensor)[0]);
394 static void init_161rev12A(
struct gspca_dev *gspca_dev)
396 write_vector(gspca_dev, spca561_161rev12A_data1);
397 sensor_mapwrite(gspca_dev, Pb100_1map8300);
399 write_vector(gspca_dev, spca561_161rev12A_data2);
400 sensor_mapwrite(gspca_dev, Pb100_2map8300);
404 static int sd_config(
struct gspca_dev *gspca_dev,
407 struct sd *
sd = (
struct sd *) gspca_dev;
416 reg_r(gspca_dev, 0x8104, 1);
418 reg_r(gspca_dev, 0x8105, 1);
420 vendor = (data2 << 8) | data1;
421 reg_r(gspca_dev, 0x8106, 1);
423 reg_r(gspca_dev, 0x8107, 1);
425 product = (data2 << 8) | data1;
427 PDEBUG(D_PROBE,
"Bad vendor / product from device");
431 cam = &gspca_dev->
cam;
447 static int sd_init_12a(
struct gspca_dev *gspca_dev)
449 PDEBUG(D_STREAM,
"Chip revision: 012a");
450 init_161rev12A(gspca_dev);
453 static int sd_init_72a(
struct gspca_dev *gspca_dev)
455 PDEBUG(D_STREAM,
"Chip revision: 072a");
456 write_vector(gspca_dev, rev72a_reset);
458 write_vector(gspca_dev, rev72a_init_data1);
459 write_sensor_72a(gspca_dev, rev72a_init_sensor1);
460 write_vector(gspca_dev, rev72a_init_data2);
461 write_sensor_72a(gspca_dev, rev72a_init_sensor2);
462 reg_w_val(gspca_dev->
dev, 0x8112, 0x30);
466 static void setbrightness(
struct gspca_dev *gspca_dev,
s32 val)
468 struct sd *sd = (
struct sd *) gspca_dev;
469 struct usb_device *dev = gspca_dev->
dev;
477 reg_w_val(dev, reg + 0, val);
478 reg_w_val(dev, reg + 1, val);
479 reg_w_val(dev, reg + 2, val);
480 reg_w_val(dev, reg + 3, val);
485 struct sd *sd = (
struct sd *) gspca_dev;
486 struct usb_device *dev = gspca_dev->
dev;
491 red = 0x20 + white * 3 / 8;
492 blue = 0x90 - white * 5 / 8;
497 red += contrast - 0x20;
498 blue += contrast - 0x20;
499 reg_w_val(dev, 0x8652, contrast + 0x20);
500 reg_w_val(dev, 0x8654, contrast + 0x20);
502 reg_w_val(dev, reg, red);
503 reg_w_val(dev, reg + 2, blue);
507 static void setexposure(
struct gspca_dev *gspca_dev,
s32 val)
530 if (val <= table[i + 1]) {
531 expo = val - table[
i];
540 gspca_dev->
usb_buf[1] = expo >> 8;
541 reg_w_buf(gspca_dev, 0x8309, 2);
545 static void setgain(
struct gspca_dev *gspca_dev,
s32 val)
553 gspca_dev->
usb_buf[0] = (val / 2) | 0x40;
555 gspca_dev->
usb_buf[0] = (val / 4) | 0xc0;
558 reg_w_buf(gspca_dev, 0x8335, 2);
561 static void setautogain(
struct gspca_dev *gspca_dev,
s32 val)
563 struct sd *sd = (
struct sd *) gspca_dev;
571 static int sd_start_12a(
struct gspca_dev *gspca_dev)
573 struct usb_device *dev = gspca_dev->
dev;
575 static const __u8 Reg8391[8] =
576 {0x92, 0x30, 0x20, 0x00, 0x0c, 0x00, 0x00, 0x00};
581 reg_w_val(dev, 0x8500, 0x10 | mode);
586 reg_w_val(dev, 0x8500, mode);
591 reg_w_buf(gspca_dev, 0x8307, 2);
593 reg_w_val(gspca_dev->
dev, 0x8700, 0x8a);
595 reg_w_val(gspca_dev->
dev, 0x8112, 0x1e | 0x20);
596 reg_w_val(gspca_dev->
dev, 0x850b, 0x03);
598 reg_w_buf(gspca_dev, 0x8391, 8);
599 reg_w_buf(gspca_dev, 0x8390, 8);
602 reg_w_val(gspca_dev->
dev, 0x8114, 0x00);
605 static int sd_start_72a(
struct gspca_dev *gspca_dev)
607 struct sd *sd = (
struct sd *) gspca_dev;
608 struct usb_device *dev = gspca_dev->
dev;
612 write_vector(gspca_dev, rev72a_reset);
614 write_vector(gspca_dev, rev72a_init_data1);
615 write_sensor_72a(gspca_dev, rev72a_init_sensor1);
633 reg_w_val(dev, 0x8700, Clck);
634 reg_w_val(dev, 0x8702, 0x81);
635 reg_w_val(dev, 0x8500, mode);
636 write_sensor_72a(gspca_dev, rev72a_init_sensor2);
641 reg_w_val(dev, 0x8112, 0x10 | 0x20);
645 static void sd_stopN(
struct gspca_dev *gspca_dev)
647 struct sd *sd = (
struct sd *) gspca_dev;
650 reg_w_val(gspca_dev->
dev, 0x8112, 0x0e);
652 reg_w_val(gspca_dev->
dev, 0x8114, 0x08);
654 reg_w_val(gspca_dev->
dev, 0x8112, 0x20);
659 static void do_autogain(
struct gspca_dev *gspca_dev)
661 struct sd *sd = (
struct sd *) gspca_dev;
667 __u8 luma_mean = 110;
668 __u8 luma_delta = 20;
679 reg_r(gspca_dev, 0x8621, 1);
681 reg_r(gspca_dev, 0x8622, 1);
683 reg_r(gspca_dev, 0x8623, 1);
685 reg_r(gspca_dev, 0x8624, 1);
687 y = (77 * R + 75 * (Gr + Gb) + 29 * B) >> 8;
692 if (y < luma_mean - luma_delta ||
693 y > luma_mean + luma_delta) {
694 expotimes =
i2c_read(gspca_dev, 0x09, 0x10);
696 expotimes = expotimes & 0x07ff;
700 gainG =
i2c_read(gspca_dev, 0x35, 0x10);
704 expotimes += (luma_mean -
y) >> spring;
705 gainG += (luma_mean -
y) / 50;
716 if (expotimes > 0x0256)
718 else if (expotimes < 3)
720 i2c_write(gspca_dev, expotimes | pixelclk, 0x09);
726 static void sd_pkt_scan(
struct gspca_dev *gspca_dev,
730 struct sd *sd = (
struct sd *) gspca_dev;
744 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
745 if (data[0] & 0x20) {
746 input_report_key(gspca_dev->input_dev,
KEY_CAMERA, 1);
747 input_sync(gspca_dev->input_dev);
748 input_report_key(gspca_dev->input_dev,
KEY_CAMERA, 0);
749 input_sync(gspca_dev->input_dev);
753 if (data[1] & 0x10) {
776 struct gspca_dev *gspca_dev =
778 struct sd *sd = (
struct sd *)gspca_dev;
787 setbrightness(gspca_dev, ctrl->
val);
791 setwhite(gspca_dev, sd->
hue->val, ctrl->
val);
795 setwhite(gspca_dev, ctrl->
val, 0);
798 setexposure(gspca_dev, ctrl->
val);
801 setgain(gspca_dev, ctrl->
val);
804 setautogain(gspca_dev, ctrl->
val);
814 static int sd_init_controls_12a(
struct gspca_dev *gspca_dev)
818 gspca_dev->
vdev.ctrl_handler = hdl;
830 pr_err(
"Could not initialize controls\n");
836 static int sd_init_controls_72a(
struct gspca_dev *gspca_dev)
838 struct sd *sd = (
struct sd *)gspca_dev;
841 gspca_dev->
vdev.ctrl_handler = hdl;
853 pr_err(
"Could not initialize controls\n");
861 static const struct sd_desc sd_desc_12a = {
863 .init_controls = sd_init_controls_12a,
866 .start = sd_start_12a,
868 .pkt_scan = sd_pkt_scan,
869 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
873 static const struct sd_desc sd_desc_72a = {
875 .init_controls = sd_init_controls_72a,
878 .start = sd_start_72a,
880 .pkt_scan = sd_pkt_scan,
881 .dq_callback = do_autogain,
882 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
893 {USB_DEVICE(0x041e, 0x401a), .driver_info =
Rev072A},
894 {USB_DEVICE(0x041e, 0x403b), .driver_info =
Rev012A},
895 {USB_DEVICE(0x0458, 0x7004), .driver_info =
Rev072A},
896 {USB_DEVICE(0x0461, 0x0815), .driver_info =
Rev072A},
897 {USB_DEVICE(0x046d, 0x0928), .driver_info =
Rev012A},
898 {USB_DEVICE(0x046d, 0x0929), .driver_info =
Rev012A},
899 {USB_DEVICE(0x046d, 0x092a), .driver_info =
Rev012A},
900 {USB_DEVICE(0x046d, 0x092b), .driver_info =
Rev012A},
901 {USB_DEVICE(0x046d, 0x092c), .driver_info =
Rev012A},
902 {USB_DEVICE(0x046d, 0x092d), .driver_info =
Rev012A},
903 {USB_DEVICE(0x046d, 0x092e), .driver_info =
Rev012A},
904 {USB_DEVICE(0x046d, 0x092f), .driver_info =
Rev012A},
905 {USB_DEVICE(0x04fc, 0x0561), .driver_info =
Rev072A},
906 {USB_DEVICE(0x060b, 0xa001), .driver_info =
Rev072A},
907 {USB_DEVICE(0x10fd, 0x7e50), .driver_info =
Rev072A},
908 {USB_DEVICE(0xabcd, 0xcdee), .driver_info =
Rev072A},
919 sd_desc[id->driver_info],
924 static struct usb_driver sd_driver = {
926 .id_table = device_table,
930 .suspend = gspca_suspend,
931 .resume = gspca_resume,
932 .reset_resume = gspca_resume,