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;
606 gspca_dev->nbalt = 7 + 1; /* choose alternate 7 first */
607 cam->cam_mode = sif_mode;
608 cam->nmodes = sizeof sif_mode / sizeof sif_mode[0];
610 sd->chip_revision = id->driver_info;
611 sd->brightness = BRIGHTNESS_DEF;
612 sd->contrast = CONTRAST_DEF;
613 sd->autogain = AUTOGAIN_DEF;
614 sd->white = WHITE_DEF;
618 /* this function is called at open time */
619 static int sd_open(struct gspca_dev *gspca_dev)
621 struct sd *sd = (struct sd *) gspca_dev;
623 switch (sd->chip_revision) {
625 PDEBUG(D_STREAM, "Chip revision id: 072a");
626 write_vector(gspca_dev, spca561_init_data);
630 PDEBUG(D_STREAM, "Chip revision id: 012a");
631 init_161rev12A(gspca_dev);
637 static void setcontrast(struct gspca_dev *gspca_dev)
639 struct sd *sd = (struct sd *) gspca_dev;
640 struct usb_device *dev = gspca_dev->dev;
644 switch (sd->chip_revision) {
646 lowb = sd->contrast >> 8;
647 reg_w_val(dev, lowb, 0x8651);
648 reg_w_val(dev, lowb, 0x8652);
649 reg_w_val(dev, lowb, 0x8653);
650 reg_w_val(dev, lowb, 0x8654);
653 /* case Rev012A: { */
655 { 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00 };
657 /* Write camera sensor settings */
658 expotimes = (sd->contrast >> 5) & 0x07ff;
659 /* exposure is in 8309 2b, range 0120 - 5720 */
660 Reg8391[0] = expotimes & 0xff; /* exposure */
661 Reg8391[1] = 0x18 | (expotimes >> 8);
662 Reg8391[2] = sd->brightness; /* gain */
663 /* gain in 8335, 2b range 0000 - 2400 */
664 reg_w_buf(gspca_dev, 0x8391, Reg8391, 8);
665 reg_w_buf(gspca_dev, 0x8390, Reg8391, 8);
671 static void setwhite(struct gspca_dev *gspca_dev)
673 struct sd *sd = (struct sd *) gspca_dev;
675 __u8 reg8614, reg8616;
677 switch (sd->chip_revision) {
679 /* no such hardware */
684 if (sd->white == 0) {
685 PDEBUG(D_CONF, "Discarding null whiteness");
688 /* try to emulate MS-win as possible */
692 reg8616 = 0x93 + (white >> 2);
693 reg8614 = 0x28 + (white >> 4);
694 reg_w_val(gspca_dev->dev, reg8616, 0x8616);
695 reg_w_val(gspca_dev->dev, reg8614, 0x8614);
699 static void setautogain(struct gspca_dev *gspca_dev)
701 struct sd *sd = (struct sd *) gspca_dev;
703 if (sd->chip_revision == Rev072A) {
705 sd->ag_cnt = AG_CNT_START;
711 static void sd_start(struct gspca_dev *gspca_dev)
713 struct sd *sd = (struct sd *) gspca_dev;
714 struct usb_device *dev = gspca_dev->dev;
716 __u8 Reg8307[] = { 0xaa, 0x00 };
719 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
720 switch (sd->chip_revision) {
735 reg_w_val(dev, 0x8500, mode); /* mode */
736 reg_w_val(dev, 0x8700, Clck); /* 0x27 clock */
737 reg_w_val(dev, 0x8112, 0x10 | 0x20);
738 setautogain(gspca_dev);
755 /* Use compression on 320x240 and above */
756 reg_w_val(dev, 0x8500, 0x10 | mode);
758 /* I couldn't get the compression to work below 320x240
759 * Fortunately at these resolutions the bandwidth
760 * is sufficient to push raw frames at ~20fps */
761 reg_w_val(dev, 0x8500, mode);
762 } /* -- qq@kuku.eu.org */
763 reg_w_buf(gspca_dev, 0x8307, Reg8307, 2);
764 reg_w_val(gspca_dev->dev, 0x8700, Clck);
765 /* 0x8f 0x85 0x27 clock */
766 reg_w_val(gspca_dev->dev, 0x8112, 0x1e | 0x20);
767 reg_w_val(gspca_dev->dev, 0x850b, 0x03);
768 setcontrast(gspca_dev);
774 static void sd_stopN(struct gspca_dev *gspca_dev)
776 reg_w_val(gspca_dev->dev, 0x8112, 0x20);
777 reg_w_val(gspca_dev->dev, 0x8102, 0x00); /* white balance - new */
780 static void sd_stop0(struct gspca_dev *gspca_dev)
784 /* this function is called at close time */
785 static void sd_close(struct gspca_dev *gspca_dev)
787 reg_w_val(gspca_dev->dev, 0x8114, 0);
790 static void do_autogain(struct gspca_dev *gspca_dev)
792 struct sd *sd = (struct sd *) gspca_dev;
798 __u8 luma_mean = 110;
799 __u8 luma_delta = 20;
804 if (--sd->ag_cnt >= 0)
806 sd->ag_cnt = AG_CNT_START;
808 switch (sd->chip_revision) {
810 reg_r(gspca_dev, 0x8621, 1);
811 Gr = gspca_dev->usb_buf[0];
812 reg_r(gspca_dev, 0x8622, 1);
813 R = gspca_dev->usb_buf[0];
814 reg_r(gspca_dev, 0x8623, 1);
815 B = gspca_dev->usb_buf[0];
816 reg_r(gspca_dev, 0x8624, 1);
817 Gb = gspca_dev->usb_buf[0];
818 y = (77 * R + 75 * (Gr + Gb) + 29 * B) >> 8;
819 /* u= (128*B-(43*(Gr+Gb+R))) >> 8; */
820 /* v= (128*R-(53*(Gr+Gb))-21*B) >> 8; */
821 /* PDEBUG(D_CONF,"reading Y %d U %d V %d ",y,u,v); */
823 if (y < luma_mean - luma_delta ||
824 y > luma_mean + luma_delta) {
825 expotimes = i2c_read(gspca_dev, 0x09, 0x10);
827 expotimes = expotimes & 0x07ff;
829 "Exposition Times 0x%03X Clock 0x%04X ",
830 expotimes,pixelclk); */
831 gainG = i2c_read(gspca_dev, 0x35, 0x10);
833 "reading Gain register %d", gainG); */
835 expotimes += (luma_mean - y) >> spring;
836 gainG += (luma_mean - y) / 50;
838 "compute expotimes %d gain %d",
845 i2c_write(gspca_dev, gainG, 0x35);
847 if (expotimes >= 0x0256)
849 else if (expotimes < 4)
851 i2c_write(gspca_dev, expotimes | pixelclk, 0x09);
855 /* sensor registers is access and memory mapped to 0x8300 */
856 /* readind all 0x83xx block the sensor */
858 * The data from the header seem wrong where is the luma
859 * and chroma mean value
860 * at the moment set exposure in contrast set
866 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
867 struct gspca_frame *frame, /* target */
868 __u8 *data, /* isoc packet */
869 int len) /* iso packet length */
872 case 0: /* start of frame */
873 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
875 data += SPCA561_OFFSET_DATA;
876 len -= SPCA561_OFFSET_DATA;
877 if (data[1] & 0x10) {
878 /* compressed bayer */
879 gspca_frame_add(gspca_dev, FIRST_PACKET,
882 /* raw bayer (with a header, which we skip) */
885 gspca_frame_add(gspca_dev, FIRST_PACKET,
889 case 0xff: /* drop */
890 /* gspca_dev->last_packet_type = DISCARD_PACKET; */
895 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
898 static void setbrightness(struct gspca_dev *gspca_dev)
900 struct sd *sd = (struct sd *) gspca_dev;
903 switch (sd->chip_revision) {
905 value = sd->brightness;
906 reg_w_val(gspca_dev->dev, value, 0x8611);
907 reg_w_val(gspca_dev->dev, value, 0x8612);
908 reg_w_val(gspca_dev->dev, value, 0x8613);
909 reg_w_val(gspca_dev->dev, value, 0x8614);
913 setcontrast(gspca_dev);
918 static void getbrightness(struct gspca_dev *gspca_dev)
920 struct sd *sd = (struct sd *) gspca_dev;
923 switch (sd->chip_revision) {
926 reg_r(gspca_dev, 0x8611, 1);
927 tot += gspca_dev->usb_buf[0];
928 reg_r(gspca_dev, 0x8612, 1);
929 tot += gspca_dev->usb_buf[0];
930 reg_r(gspca_dev, 0x8613, 1);
931 tot += gspca_dev->usb_buf[0];
932 reg_r(gspca_dev, 0x8614, 1);
933 tot += gspca_dev->usb_buf[0];
934 sd->brightness = tot >> 2;
938 /* no way to read sensor settings */
943 static void getcontrast(struct gspca_dev *gspca_dev)
945 struct sd *sd = (struct sd *) gspca_dev;
948 switch (sd->chip_revision) {
951 reg_r(gspca_dev, 0x8651, 1);
952 tot += gspca_dev->usb_buf[0];
953 reg_r(gspca_dev, 0x8652, 1);
954 tot += gspca_dev->usb_buf[0];
955 reg_r(gspca_dev, 0x8653, 1);
956 tot += gspca_dev->usb_buf[0];
957 reg_r(gspca_dev, 0x8654, 1);
958 tot += gspca_dev->usb_buf[0];
959 sd->contrast = tot << 6;
963 /* no way to read sensor settings */
966 PDEBUG(D_CONF, "get contrast %d", sd->contrast);
969 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
971 struct sd *sd = (struct sd *) gspca_dev;
973 sd->brightness = val;
974 if (gspca_dev->streaming)
975 setbrightness(gspca_dev);
979 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
981 struct sd *sd = (struct sd *) gspca_dev;
983 getbrightness(gspca_dev);
984 *val = sd->brightness;
988 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
990 struct sd *sd = (struct sd *) gspca_dev;
993 if (gspca_dev->streaming)
994 setcontrast(gspca_dev);
998 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
1000 struct sd *sd = (struct sd *) gspca_dev;
1002 getcontrast(gspca_dev);
1003 *val = sd->contrast;
1007 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
1009 struct sd *sd = (struct sd *) gspca_dev;
1012 if (gspca_dev->streaming)
1013 setautogain(gspca_dev);
1017 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
1019 struct sd *sd = (struct sd *) gspca_dev;
1021 *val = sd->autogain;
1025 /* white balance - new */
1026 static int sd_setwhite(struct gspca_dev *gspca_dev, __s32 val)
1028 struct sd *sd = (struct sd *) gspca_dev;
1031 if (gspca_dev->streaming)
1032 setwhite(gspca_dev);
1036 static int sd_getwhite(struct gspca_dev *gspca_dev, __s32 *val)
1038 struct sd *sd = (struct sd *) gspca_dev;
1044 /* sub-driver description */
1045 static const struct sd_desc sd_desc = {
1046 .name = MODULE_NAME,
1048 .nctrls = ARRAY_SIZE(sd_ctrls),
1049 .config = sd_config,
1055 .pkt_scan = sd_pkt_scan,
1056 .dq_callback = do_autogain,
1059 /* -- module initialisation -- */
1060 static const __devinitdata struct usb_device_id device_table[] = {
1061 {USB_DEVICE(0x041e, 0x401a), .driver_info = Rev072A},
1062 {USB_DEVICE(0x041e, 0x403b), .driver_info = Rev012A},
1063 {USB_DEVICE(0x0458, 0x7004), .driver_info = Rev072A},
1064 {USB_DEVICE(0x046d, 0x0928), .driver_info = Rev012A},
1065 {USB_DEVICE(0x046d, 0x0929), .driver_info = Rev012A},
1066 {USB_DEVICE(0x046d, 0x092a), .driver_info = Rev012A},
1067 {USB_DEVICE(0x046d, 0x092b), .driver_info = Rev012A},
1068 {USB_DEVICE(0x046d, 0x092c), .driver_info = Rev012A},
1069 {USB_DEVICE(0x046d, 0x092d), .driver_info = Rev012A},
1070 {USB_DEVICE(0x046d, 0x092e), .driver_info = Rev012A},
1071 {USB_DEVICE(0x046d, 0x092f), .driver_info = Rev012A},
1072 {USB_DEVICE(0x04fc, 0x0561), .driver_info = Rev072A},
1073 {USB_DEVICE(0x060b, 0xa001), .driver_info = Rev072A},
1074 {USB_DEVICE(0x10fd, 0x7e50), .driver_info = Rev072A},
1075 {USB_DEVICE(0xabcd, 0xcdee), .driver_info = Rev072A},
1079 MODULE_DEVICE_TABLE(usb, device_table);
1081 /* -- device connect -- */
1082 static int sd_probe(struct usb_interface *intf,
1083 const struct usb_device_id *id)
1085 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1089 static struct usb_driver sd_driver = {
1090 .name = MODULE_NAME,
1091 .id_table = device_table,
1093 .disconnect = gspca_disconnect,
1096 /* -- module insert / remove -- */
1097 static int __init sd_mod_init(void)
1099 if (usb_register(&sd_driver) < 0)
1101 PDEBUG(D_PROBE, "registered");
1104 static void __exit sd_mod_exit(void)
1106 usb_deregister(&sd_driver);
1107 PDEBUG(D_PROBE, "deregistered");
1110 module_init(sd_mod_init);
1111 module_exit(sd_mod_exit);