2 * Sunplus spca561 subdriver
4 * Copyright (C) 2004 Michel Xhaard mxhaard@magic.fr
6 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #define MODULE_NAME "spca561"
27 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
28 MODULE_DESCRIPTION("GSPCA/SPCA561 USB Camera Driver");
29 MODULE_LICENSE("GPL");
31 /* specific webcam descriptor */
33 struct gspca_dev gspca_dev; /* !! must be the first item */
35 unsigned short contrast;
42 #define AG_CNT_START 13
45 /* V4L2 controls supported by the driver */
46 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
47 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
48 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
49 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
50 static int sd_setwhite(struct gspca_dev *gspca_dev, __s32 val);
51 static int sd_getwhite(struct gspca_dev *gspca_dev, __s32 *val);
52 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
53 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
55 static struct ctrl sd_ctrls[] = {
58 .id = V4L2_CID_BRIGHTNESS,
59 .type = V4L2_CTRL_TYPE_INTEGER,
64 #define BRIGHTNESS_DEF 32
65 .default_value = BRIGHTNESS_DEF,
67 .set = sd_setbrightness,
68 .get = sd_getbrightness,
72 .id = V4L2_CID_CONTRAST,
73 .type = V4L2_CTRL_TYPE_INTEGER,
78 #define CONTRAST_DEF 0x2000
79 .default_value = CONTRAST_DEF,
81 .set = sd_setcontrast,
82 .get = sd_getcontrast,
86 .id = V4L2_CID_DO_WHITE_BALANCE,
87 .type = V4L2_CTRL_TYPE_INTEGER,
88 .name = "While Balance",
93 .default_value = WHITE_DEF,
100 .id = V4L2_CID_AUTOGAIN,
101 .type = V4L2_CTRL_TYPE_BOOLEAN,
106 #define AUTOGAIN_DEF 1
107 .default_value = AUTOGAIN_DEF,
109 .set = sd_setautogain,
110 .get = sd_getautogain,
114 static struct v4l2_pix_format sif_mode[] = {
115 {160, 120, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
117 .sizeimage = 160 * 120,
118 .colorspace = V4L2_COLORSPACE_SRGB,
120 {176, 144, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
122 .sizeimage = 176 * 144,
123 .colorspace = V4L2_COLORSPACE_SRGB,
125 {320, 240, V4L2_PIX_FMT_SPCA561, V4L2_FIELD_NONE,
127 .sizeimage = 320 * 240 * 4 / 8,
128 .colorspace = V4L2_COLORSPACE_SRGB,
130 {352, 288, V4L2_PIX_FMT_SPCA561, V4L2_FIELD_NONE,
132 .sizeimage = 352 * 288 * 4 / 8,
133 .colorspace = V4L2_COLORSPACE_SRGB,
138 * Initialization data
139 * I'm not very sure how to split initialization from open data
140 * chunks. For now, we'll consider everything as initialization
142 /* Frame packet header offsets for the spca561 */
143 #define SPCA561_OFFSET_SNAP 1
144 #define SPCA561_OFFSET_TYPE 2
145 #define SPCA561_OFFSET_COMPRESS 3
146 #define SPCA561_OFFSET_FRAMSEQ 4
147 #define SPCA561_OFFSET_GPIO 5
148 #define SPCA561_OFFSET_USBBUFF 6
149 #define SPCA561_OFFSET_WIN2GRAVE 7
150 #define SPCA561_OFFSET_WIN2RAVE 8
151 #define SPCA561_OFFSET_WIN2BAVE 9
152 #define SPCA561_OFFSET_WIN2GBAVE 10
153 #define SPCA561_OFFSET_WIN1GRAVE 11
154 #define SPCA561_OFFSET_WIN1RAVE 12
155 #define SPCA561_OFFSET_WIN1BAVE 13
156 #define SPCA561_OFFSET_WIN1GBAVE 14
157 #define SPCA561_OFFSET_FREQ 15
158 #define SPCA561_OFFSET_VSYNC 16
159 #define SPCA561_OFFSET_DATA 1
160 #define SPCA561_INDEX_I2C_BASE 0x8800
161 #define SPCA561_SNAPBIT 0x20
162 #define SPCA561_SNAPCTRL 0x40
168 static void reg_w_val(struct usb_device *dev, __u16 index, __u16 value)
172 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
174 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
175 value, index, NULL, 0, 500);
176 PDEBUG(D_USBO, "reg write: 0x%02x:0x%02x", index, value);
178 PDEBUG(D_ERR, "reg write: error %d", ret);
181 static void write_vector(struct gspca_dev *gspca_dev,
182 const __u16 data[][2])
184 struct usb_device *dev = gspca_dev->dev;
188 while (data[i][1] != 0) {
189 reg_w_val(dev, data[i][1], data[i][0]);
194 /* read 'len' bytes to gspca_dev->usb_buf */
195 static void reg_r(struct gspca_dev *gspca_dev,
196 __u16 index, __u16 length)
198 usb_control_msg(gspca_dev->dev,
199 usb_rcvctrlpipe(gspca_dev->dev, 0),
201 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
203 index, gspca_dev->usb_buf, length, 500);
206 static void reg_w_buf(struct gspca_dev *gspca_dev,
207 __u16 index, const __u8 *buffer, __u16 len)
209 memcpy(gspca_dev->usb_buf, buffer, len);
210 usb_control_msg(gspca_dev->dev,
211 usb_sndctrlpipe(gspca_dev->dev, 0),
213 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
215 index, gspca_dev->usb_buf, len, 500);
218 static void i2c_init(struct gspca_dev *gspca_dev, __u8 mode)
220 reg_w_val(gspca_dev->dev, 0x92, 0x8804);
221 reg_w_val(gspca_dev->dev, mode, 0x8802);
224 static void i2c_write(struct gspca_dev *gspca_dev, __u16 valeur, __u16 reg)
231 DataHight = valeur >> 8;
232 reg_w_val(gspca_dev->dev, reg, 0x8801);
233 reg_w_val(gspca_dev->dev, DataLow, 0x8805);
234 reg_w_val(gspca_dev->dev, DataHight, 0x8800);
236 reg_r(gspca_dev, 0x8803, 1);
237 if (!gspca_dev->usb_buf[0])
242 static int i2c_read(struct gspca_dev *gspca_dev, __u16 reg, __u8 mode)
248 reg_w_val(gspca_dev->dev, 0x92, 0x8804);
249 reg_w_val(gspca_dev->dev, reg, 0x8801);
250 reg_w_val(gspca_dev->dev, (mode | 0x01), 0x8802);
252 reg_r(gspca_dev, 0x8803, 1);
253 if (!gspca_dev->usb_buf)
258 reg_r(gspca_dev, 0x8800, 1);
259 value = gspca_dev->usb_buf[0];
260 reg_r(gspca_dev, 0x8805, 1);
261 vallsb = gspca_dev->usb_buf[0];
262 return ((int) value << 8) | vallsb;
265 static const __u16 spca561_init_data[][2] = {
266 {0x0000, 0x8114}, /* Software GPIO output data */
267 {0x0001, 0x8114}, /* Software GPIO output data */
268 {0x0000, 0x8112}, /* Some kind of reset */
269 {0x0003, 0x8701}, /* PCLK clock delay adjustment */
270 {0x0001, 0x8703}, /* HSYNC from cmos inverted */
271 {0x0011, 0x8118}, /* Enable and conf sensor */
272 {0x0001, 0x8118}, /* Conf sensor */
273 {0x0092, 0x8804}, /* I know nothing about these */
274 {0x0010, 0x8802}, /* 0x88xx registers, so I won't */
276 {0x000d, 0x8805}, /* sensor default setting */
277 {0x0001, 0x8801}, /* 1 <- 0x0d */
280 {0x0002, 0x8801}, /* 2 <- 0x18 */
283 {0x0004, 0x8801}, /* 4 <- 0x01 0x65 */
286 {0x0005, 0x8801}, /* 5 <- 0x21 */
289 {0x0007, 0x8801}, /* 7 <- 0xaa */
292 {0x0020, 0x8801}, /* 0x20 <- 0x15 0x04 */
295 {0x0039, 0x8801}, /* 0x39 <- 0x02 */
298 {0x0035, 0x8801}, /* 0x35 <- 0x10 */
301 {0x0009, 0x8801}, /* 0x09 <- 0x10 0x49 */
304 {0x0028, 0x8801}, /* 0x28 <- 0x0b */
307 {0x003b, 0x8801}, /* 0x3b <- 0x0f */
310 {0x003c, 0x8801}, /* 0x3c <- 0x00 */
313 {0x0018, 0x8601}, /* Pixel/line selection for color separation */
314 {0x0000, 0x8602}, /* Optical black level for user setting */
315 {0x0060, 0x8604}, /* Optical black horizontal offset */
316 {0x0002, 0x8605}, /* Optical black vertical offset */
317 {0x0000, 0x8603}, /* Non-automatic optical black level */
318 {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
319 {0x0000, 0x865f}, /* Vertical valid pixels window (x2) */
320 {0x00b0, 0x865d}, /* Horizontal valid pixels window (x2) */
321 {0x0090, 0x865e}, /* Vertical valid lines window (x2) */
322 {0x00e0, 0x8406}, /* Memory buffer threshold */
323 {0x0000, 0x8660}, /* Compensation memory stuff */
324 {0x0002, 0x8201}, /* Output address for r/w serial EEPROM */
325 {0x0008, 0x8200}, /* Clear valid bit for serial EEPROM */
326 {0x0001, 0x8200}, /* OprMode to be executed by hardware */
327 {0x0007, 0x8201}, /* Output address for r/w serial EEPROM */
328 {0x0008, 0x8200}, /* Clear valid bit for serial EEPROM */
329 {0x0001, 0x8200}, /* OprMode to be executed by hardware */
330 {0x0010, 0x8660}, /* Compensation memory stuff */
331 {0x0018, 0x8660}, /* Compensation memory stuff */
333 {0x0004, 0x8611}, /* R offset for white balance */
334 {0x0004, 0x8612}, /* Gr offset for white balance */
335 {0x0007, 0x8613}, /* B offset for white balance */
336 {0x0000, 0x8614}, /* Gb offset for white balance */
337 {0x008c, 0x8651}, /* R gain for white balance */
338 {0x008c, 0x8652}, /* Gr gain for white balance */
339 {0x00b5, 0x8653}, /* B gain for white balance */
340 {0x008c, 0x8654}, /* Gb gain for white balance */
341 {0x0002, 0x8502}, /* Maximum average bit rate stuff */
344 {0x0087, 0x8700}, /* Set master clock (96Mhz????) */
345 {0x0081, 0x8702}, /* Master clock output enable */
347 {0x0000, 0x8500}, /* Set image type (352x288 no compression) */
348 /* Originally was 0x0010 (352x288 compression) */
350 {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
351 {0x0003, 0x865c}, /* Vertical offset for valid lines */
352 /***************//* sensor active */
353 {0x0003, 0x8801}, /* 0x03 <- 0x01 0x21 //289 */
356 {0x0004, 0x8801}, /* 0x04 <- 0x01 0x65 //357 */
359 {0x0005, 0x8801}, /* 0x05 <- 0x2f */
362 {0x0006, 0x8801}, /* 0x06 <- 0 */
365 {0x000a, 0x8801}, /* 0x0a <- 2 */
368 {0x0009, 0x8801}, /* 0x09 <- 0x1061 */
371 {0x0035, 0x8801}, /* 0x35 <-0x14 */
374 {0x0030, 0x8112}, /* ISO and drop packet enable */
375 {0x0000, 0x8112}, /* Some kind of reset ???? */
376 {0x0009, 0x8118}, /* Enable sensor and set standby */
377 {0x0000, 0x8114}, /* Software GPIO output data */
378 {0x0000, 0x8114}, /* Software GPIO output data */
379 {0x0001, 0x8114}, /* Software GPIO output data */
380 {0x0000, 0x8112}, /* Some kind of reset ??? */
401 {0x0007, 0x8801}, /* mode 0xaa */
405 {0x0015, 0x8800}, /* mode 0x0415 */
429 {0x0087, 0x8700}, /* overwrite by start */
432 /* {0x0010, 0x8500}, -- Previous line was this */
436 {0x0003, 0x8801}, /* 0x121-> 289 */
439 {0x0004, 0x8801}, /* 0x165 -> 357 */
442 {0x0005, 0x8801}, /* 0x2f //blanking control colonne */
445 {0x0006, 0x8801}, /* 0x00 //blanking mode row */
448 {0x000a, 0x8801}, /* 0x01 //0x02 */
451 {0x0009, 0x8801}, /* 0x1061 - setexposure times && pixel clock
452 * 0001 0 | 000 0110 0001 */
453 {0x0061, 0x8805}, /* 61 31 */
454 {0x0008, 0x8800}, /* 08 */
455 {0x0035, 0x8801}, /* 0x14 - set gain general */
456 {0x001f, 0x8805}, /* 0x14 */
458 {0x000e, 0x8112}, /* white balance - was 30 */
462 static void sensor_reset(struct gspca_dev *gspca_dev)
464 reg_w_val(gspca_dev->dev, 0x8631, 0xc8);
465 reg_w_val(gspca_dev->dev, 0x8634, 0xc8);
466 reg_w_val(gspca_dev->dev, 0x8112, 0x00);
467 reg_w_val(gspca_dev->dev, 0x8114, 0x00);
468 reg_w_val(gspca_dev->dev, 0x8118, 0x21);
469 i2c_init(gspca_dev, 0x14);
470 i2c_write(gspca_dev, 1, 0x0d);
471 i2c_write(gspca_dev, 0, 0x0d);
474 /******************** QC Express etch2 stuff ********************/
475 static const __u16 Pb100_1map8300[][2] = {
479 {0x8303, 0x0125}, /* image area */
490 static const __u16 Pb100_2map8300[][2] = {
497 static const __u16 spca561_161rev12A_data1[][2] = {
498 {0x29, 0x8118}, /* white balance - was 21 */
499 {0x08, 0x8114}, /* white balance - was 01 */
500 {0x0e, 0x8112}, /* white balance - was 00 */
501 {0x00, 0x8102}, /* white balance - new */
503 {0x04, 0x8802}, /* windows uses 08 */
506 static const __u16 spca561_161rev12A_data2[][2] = {
514 {0x07, 0x8201}, /* windows uses 02 */
519 {0x01, 0x8114}, /* windows uses 00 */
526 {0x07, 0x8601}, /* white balance - new */
527 {0x07, 0x8602}, /* white balance - new */
528 {0x00, 0x8610}, /* *red */
529 {0x00, 0x8611}, /* 3f *green */
530 {0x00, 0x8612}, /* green *blue */
531 {0x00, 0x8613}, /* blue *green */
532 {0x43, 0x8614}, /* green *red - white balance - was 0x35 */
533 {0x40, 0x8615}, /* 40 *green - white balance - was 0x35 */
534 {0x71, 0x8616}, /* 7a *blue - white balance - was 0x35 */
535 {0x40, 0x8617}, /* 40 *green - white balance - was 0x35 */
537 {0x0c, 0x8620}, /* 0c */
538 {0xc8, 0x8631}, /* c8 */
539 {0xc8, 0x8634}, /* c8 */
540 {0x23, 0x8635}, /* 23 */
541 {0x1f, 0x8636}, /* 1f */
542 {0xdd, 0x8637}, /* dd */
543 {0xe1, 0x8638}, /* e1 */
544 {0x1d, 0x8639}, /* 1d */
545 {0x21, 0x863a}, /* 21 */
546 {0xe3, 0x863b}, /* e3 */
547 {0xdf, 0x863c}, /* df */
550 {0x99, 0x8700}, /* - white balance - new */
554 static void sensor_mapwrite(struct gspca_dev *gspca_dev,
555 const __u16 sensormap[][2])
560 while (sensormap[i][0]) {
561 usbval[0] = sensormap[i][1];
562 usbval[1] = sensormap[i][1] >> 8;
563 reg_w_buf(gspca_dev, sensormap[i][0], usbval, 2);
567 static void init_161rev12A(struct gspca_dev *gspca_dev)
569 sensor_reset(gspca_dev);
570 write_vector(gspca_dev, spca561_161rev12A_data1);
571 sensor_mapwrite(gspca_dev, Pb100_1map8300);
572 write_vector(gspca_dev, spca561_161rev12A_data2);
573 sensor_mapwrite(gspca_dev, Pb100_2map8300);
576 /* this function is called at probe time */
577 static int sd_config(struct gspca_dev *gspca_dev,
578 const struct usb_device_id *id)
580 struct sd *sd = (struct sd *) gspca_dev;
582 __u16 vendor, product;
585 /* Read frm global register the USB product and vendor IDs, just to
586 * prove that we can communicate with the device. This works, which
587 * confirms at we are communicating properly and that the device
589 reg_r(gspca_dev, 0x8104, 1);
590 data1 = gspca_dev->usb_buf[0];
591 reg_r(gspca_dev, 0x8105, 1);
592 data2 = gspca_dev->usb_buf[0];
593 vendor = (data2 << 8) | data1;
594 reg_r(gspca_dev, 0x8106, 1);
595 data1 = gspca_dev->usb_buf[0];
596 reg_r(gspca_dev, 0x8107, 1);
597 data2 = gspca_dev->usb_buf[0];
598 product = (data2 << 8) | data1;
599 if (vendor != id->idVendor || product != id->idProduct) {
600 PDEBUG(D_PROBE, "Bad vendor / product from device");
604 cam = &gspca_dev->cam;
605 cam->dev_name = (char *) id->driver_info;
607 gspca_dev->nbalt = 7 + 1; /* choose alternate 7 first */
608 cam->cam_mode = sif_mode;
609 cam->nmodes = sizeof sif_mode / sizeof sif_mode[0];
611 sd->chip_revision = id->driver_info;
612 sd->brightness = BRIGHTNESS_DEF;
613 sd->contrast = CONTRAST_DEF;
614 sd->autogain = AUTOGAIN_DEF;
615 sd->white = WHITE_DEF;
619 /* this function is called at open time */
620 static int sd_open(struct gspca_dev *gspca_dev)
622 struct sd *sd = (struct sd *) gspca_dev;
624 switch (sd->chip_revision) {
626 PDEBUG(D_STREAM, "Chip revision id: 072a");
627 write_vector(gspca_dev, spca561_init_data);
631 PDEBUG(D_STREAM, "Chip revision id: 012a");
632 init_161rev12A(gspca_dev);
638 static void setcontrast(struct gspca_dev *gspca_dev)
640 struct sd *sd = (struct sd *) gspca_dev;
641 struct usb_device *dev = gspca_dev->dev;
645 switch (sd->chip_revision) {
647 lowb = sd->contrast >> 8;
648 reg_w_val(dev, lowb, 0x8651);
649 reg_w_val(dev, lowb, 0x8652);
650 reg_w_val(dev, lowb, 0x8653);
651 reg_w_val(dev, lowb, 0x8654);
654 /* case Rev012A: { */
656 { 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00 };
658 /* Write camera sensor settings */
659 expotimes = (sd->contrast >> 5) & 0x07ff;
660 /* exposure is in 8309 2b, range 0120 - 5720 */
661 Reg8391[0] = expotimes & 0xff; /* exposure */
662 Reg8391[1] = 0x18 | (expotimes >> 8);
663 Reg8391[2] = sd->brightness; /* gain */
664 /* gain in 8335, 2b range 0000 - 2400 */
665 reg_w_buf(gspca_dev, 0x8391, Reg8391, 8);
666 reg_w_buf(gspca_dev, 0x8390, Reg8391, 8);
672 static void setwhite(struct gspca_dev *gspca_dev)
674 struct sd *sd = (struct sd *) gspca_dev;
676 __u8 reg8614, reg8616;
678 switch (sd->chip_revision) {
680 /* no such hardware */
685 if (sd->white == 0) {
686 PDEBUG(D_CONF, "Discarding null whiteness");
689 /* try to emulate MS-win as possible */
693 reg8616 = 0x93 + (white >> 2);
694 reg8614 = 0x28 + (white >> 4);
695 reg_w_val(gspca_dev->dev, reg8616, 0x8616);
696 reg_w_val(gspca_dev->dev, reg8614, 0x8614);
700 static void setautogain(struct gspca_dev *gspca_dev)
702 struct sd *sd = (struct sd *) gspca_dev;
704 if (sd->chip_revision == Rev072A) {
706 sd->ag_cnt = AG_CNT_START;
712 static void sd_start(struct gspca_dev *gspca_dev)
714 struct sd *sd = (struct sd *) gspca_dev;
715 struct usb_device *dev = gspca_dev->dev;
717 __u8 Reg8307[] = { 0xaa, 0x00 };
720 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
721 switch (sd->chip_revision) {
736 reg_w_val(dev, 0x8500, mode); /* mode */
737 reg_w_val(dev, 0x8700, Clck); /* 0x27 clock */
738 reg_w_val(dev, 0x8112, 0x10 | 0x20);
739 setautogain(gspca_dev);
756 /* Use compression on 320x240 and above */
757 reg_w_val(dev, 0x8500, 0x10 | mode);
759 /* I couldn't get the compression to work below 320x240
760 * Fortunately at these resolutions the bandwidth
761 * is sufficient to push raw frames at ~20fps */
762 reg_w_val(dev, 0x8500, mode);
763 } /* -- qq@kuku.eu.org */
764 reg_w_buf(gspca_dev, 0x8307, Reg8307, 2);
765 reg_w_val(gspca_dev->dev, 0x8700, Clck);
766 /* 0x8f 0x85 0x27 clock */
767 reg_w_val(gspca_dev->dev, 0x8112, 0x1e | 0x20);
768 reg_w_val(gspca_dev->dev, 0x850b, 0x03);
769 setcontrast(gspca_dev);
775 static void sd_stopN(struct gspca_dev *gspca_dev)
777 reg_w_val(gspca_dev->dev, 0x8112, 0x20);
778 reg_w_val(gspca_dev->dev, 0x8102, 0x00); /* white balance - new */
781 static void sd_stop0(struct gspca_dev *gspca_dev)
785 /* this function is called at close time */
786 static void sd_close(struct gspca_dev *gspca_dev)
788 reg_w_val(gspca_dev->dev, 0x8114, 0);
791 static void do_autogain(struct gspca_dev *gspca_dev)
793 struct sd *sd = (struct sd *) gspca_dev;
799 __u8 luma_mean = 110;
800 __u8 luma_delta = 20;
805 if (--sd->ag_cnt >= 0)
807 sd->ag_cnt = AG_CNT_START;
809 switch (sd->chip_revision) {
811 reg_r(gspca_dev, 0x8621, 1);
812 Gr = gspca_dev->usb_buf[0];
813 reg_r(gspca_dev, 0x8622, 1);
814 R = gspca_dev->usb_buf[0];
815 reg_r(gspca_dev, 0x8623, 1);
816 B = gspca_dev->usb_buf[0];
817 reg_r(gspca_dev, 0x8624, 1);
818 Gb = gspca_dev->usb_buf[0];
819 y = (77 * R + 75 * (Gr + Gb) + 29 * B) >> 8;
820 /* u= (128*B-(43*(Gr+Gb+R))) >> 8; */
821 /* v= (128*R-(53*(Gr+Gb))-21*B) >> 8; */
822 /* PDEBUG(D_CONF,"reading Y %d U %d V %d ",y,u,v); */
824 if (y < luma_mean - luma_delta ||
825 y > luma_mean + luma_delta) {
826 expotimes = i2c_read(gspca_dev, 0x09, 0x10);
828 expotimes = expotimes & 0x07ff;
830 "Exposition Times 0x%03X Clock 0x%04X ",
831 expotimes,pixelclk); */
832 gainG = i2c_read(gspca_dev, 0x35, 0x10);
834 "reading Gain register %d", gainG); */
836 expotimes += (luma_mean - y) >> spring;
837 gainG += (luma_mean - y) / 50;
839 "compute expotimes %d gain %d",
846 i2c_write(gspca_dev, gainG, 0x35);
848 if (expotimes >= 0x0256)
850 else if (expotimes < 4)
852 i2c_write(gspca_dev, expotimes | pixelclk, 0x09);
856 /* sensor registers is access and memory mapped to 0x8300 */
857 /* readind all 0x83xx block the sensor */
859 * The data from the header seem wrong where is the luma
860 * and chroma mean value
861 * at the moment set exposure in contrast set
867 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
868 struct gspca_frame *frame, /* target */
869 __u8 *data, /* isoc packet */
870 int len) /* iso packet length */
873 case 0: /* start of frame */
874 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
876 data += SPCA561_OFFSET_DATA;
877 len -= SPCA561_OFFSET_DATA;
878 if (data[1] & 0x10) {
879 /* compressed bayer */
880 gspca_frame_add(gspca_dev, FIRST_PACKET,
883 /* raw bayer (with a header, which we skip) */
886 gspca_frame_add(gspca_dev, FIRST_PACKET,
890 case 0xff: /* drop */
891 /* gspca_dev->last_packet_type = DISCARD_PACKET; */
896 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
899 static void setbrightness(struct gspca_dev *gspca_dev)
901 struct sd *sd = (struct sd *) gspca_dev;
904 switch (sd->chip_revision) {
906 value = sd->brightness;
907 reg_w_val(gspca_dev->dev, value, 0x8611);
908 reg_w_val(gspca_dev->dev, value, 0x8612);
909 reg_w_val(gspca_dev->dev, value, 0x8613);
910 reg_w_val(gspca_dev->dev, value, 0x8614);
914 setcontrast(gspca_dev);
919 static void getbrightness(struct gspca_dev *gspca_dev)
921 struct sd *sd = (struct sd *) gspca_dev;
924 switch (sd->chip_revision) {
927 reg_r(gspca_dev, 0x8611, 1);
928 tot += gspca_dev->usb_buf[0];
929 reg_r(gspca_dev, 0x8612, 1);
930 tot += gspca_dev->usb_buf[0];
931 reg_r(gspca_dev, 0x8613, 1);
932 tot += gspca_dev->usb_buf[0];
933 reg_r(gspca_dev, 0x8614, 1);
934 tot += gspca_dev->usb_buf[0];
935 sd->brightness = tot >> 2;
939 /* no way to read sensor settings */
944 static void getcontrast(struct gspca_dev *gspca_dev)
946 struct sd *sd = (struct sd *) gspca_dev;
949 switch (sd->chip_revision) {
952 reg_r(gspca_dev, 0x8651, 1);
953 tot += gspca_dev->usb_buf[0];
954 reg_r(gspca_dev, 0x8652, 1);
955 tot += gspca_dev->usb_buf[0];
956 reg_r(gspca_dev, 0x8653, 1);
957 tot += gspca_dev->usb_buf[0];
958 reg_r(gspca_dev, 0x8654, 1);
959 tot += gspca_dev->usb_buf[0];
960 sd->contrast = tot << 6;
964 /* no way to read sensor settings */
967 PDEBUG(D_CONF, "get contrast %d", sd->contrast);
970 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
972 struct sd *sd = (struct sd *) gspca_dev;
974 sd->brightness = val;
975 if (gspca_dev->streaming)
976 setbrightness(gspca_dev);
980 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
982 struct sd *sd = (struct sd *) gspca_dev;
984 getbrightness(gspca_dev);
985 *val = sd->brightness;
989 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
991 struct sd *sd = (struct sd *) gspca_dev;
994 if (gspca_dev->streaming)
995 setcontrast(gspca_dev);
999 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
1001 struct sd *sd = (struct sd *) gspca_dev;
1003 getcontrast(gspca_dev);
1004 *val = sd->contrast;
1008 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
1010 struct sd *sd = (struct sd *) gspca_dev;
1013 if (gspca_dev->streaming)
1014 setautogain(gspca_dev);
1018 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
1020 struct sd *sd = (struct sd *) gspca_dev;
1022 *val = sd->autogain;
1026 /* white balance - new */
1027 static int sd_setwhite(struct gspca_dev *gspca_dev, __s32 val)
1029 struct sd *sd = (struct sd *) gspca_dev;
1032 if (gspca_dev->streaming)
1033 setwhite(gspca_dev);
1037 static int sd_getwhite(struct gspca_dev *gspca_dev, __s32 *val)
1039 struct sd *sd = (struct sd *) gspca_dev;
1045 /* sub-driver description */
1046 static const struct sd_desc sd_desc = {
1047 .name = MODULE_NAME,
1049 .nctrls = ARRAY_SIZE(sd_ctrls),
1050 .config = sd_config,
1056 .pkt_scan = sd_pkt_scan,
1057 .dq_callback = do_autogain,
1060 /* -- module initialisation -- */
1061 static const __devinitdata struct usb_device_id device_table[] = {
1062 {USB_DEVICE(0x041e, 0x401a), .driver_info = Rev072A},
1063 {USB_DEVICE(0x041e, 0x403b), .driver_info = Rev012A},
1064 {USB_DEVICE(0x0458, 0x7004), .driver_info = Rev072A},
1065 {USB_DEVICE(0x046d, 0x0928), .driver_info = Rev012A},
1066 {USB_DEVICE(0x046d, 0x0929), .driver_info = Rev012A},
1067 {USB_DEVICE(0x046d, 0x092a), .driver_info = Rev012A},
1068 {USB_DEVICE(0x046d, 0x092b), .driver_info = Rev012A},
1069 {USB_DEVICE(0x046d, 0x092c), .driver_info = Rev012A},
1070 {USB_DEVICE(0x046d, 0x092d), .driver_info = Rev012A},
1071 {USB_DEVICE(0x046d, 0x092e), .driver_info = Rev012A},
1072 {USB_DEVICE(0x046d, 0x092f), .driver_info = Rev012A},
1073 {USB_DEVICE(0x04fc, 0x0561), .driver_info = Rev072A},
1074 {USB_DEVICE(0x060b, 0xa001), .driver_info = Rev072A},
1075 {USB_DEVICE(0x10fd, 0x7e50), .driver_info = Rev072A},
1076 {USB_DEVICE(0xabcd, 0xcdee), .driver_info = Rev072A},
1080 MODULE_DEVICE_TABLE(usb, device_table);
1082 /* -- device connect -- */
1083 static int sd_probe(struct usb_interface *intf,
1084 const struct usb_device_id *id)
1086 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1090 static struct usb_driver sd_driver = {
1091 .name = MODULE_NAME,
1092 .id_table = device_table,
1094 .disconnect = gspca_disconnect,
1097 /* -- module insert / remove -- */
1098 static int __init sd_mod_init(void)
1100 if (usb_register(&sd_driver) < 0)
1102 PDEBUG(D_PROBE, "registered");
1105 static void __exit sd_mod_exit(void)
1107 usb_deregister(&sd_driver);
1108 PDEBUG(D_PROBE, "deregistered");
1111 module_init(sd_mod_init);
1112 module_exit(sd_mod_exit);