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 __u16 contrast; /* rev72a only */
36 #define CONTRAST_MIN 0x0000
37 #define CONTRAST_DEF 0x2000
38 #define CONTRAST_MAX 0x3fff
40 __u16 exposure; /* rev12a only */
41 #define EXPOSURE_MIN 1
42 #define EXPOSURE_DEF 200
43 #define EXPOSURE_MAX (4095 - 900) /* see set_exposure */
45 __u8 brightness; /* rev72a only */
46 #define BRIGHTNESS_MIN 0
47 #define BRIGHTNESS_DEF 32
48 #define BRIGHTNESS_MAX 63
50 __u8 white; /* rev12a only */
52 #define WHITE_DEF 0x40
53 #define WHITE_MAX 0x7f
56 #define AUTOGAIN_MIN 0
57 #define AUTOGAIN_DEF 1
58 #define AUTOGAIN_MAX 1
60 __u8 gain; /* rev12a only */
66 __u8 expo12a; /* expo/gain? for rev 12a */
73 #define AG_CNT_START 13
76 static struct v4l2_pix_format sif_012a_mode[] = {
77 {160, 120, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
79 .sizeimage = 160 * 120,
80 .colorspace = V4L2_COLORSPACE_SRGB,
82 {176, 144, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
84 .sizeimage = 176 * 144,
85 .colorspace = V4L2_COLORSPACE_SRGB,
87 {320, 240, V4L2_PIX_FMT_SPCA561, V4L2_FIELD_NONE,
89 .sizeimage = 320 * 240 * 4 / 8,
90 .colorspace = V4L2_COLORSPACE_SRGB,
92 {352, 288, V4L2_PIX_FMT_SPCA561, V4L2_FIELD_NONE,
94 .sizeimage = 352 * 288 * 4 / 8,
95 .colorspace = V4L2_COLORSPACE_SRGB,
99 static struct v4l2_pix_format sif_072a_mode[] = {
100 {160, 120, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
102 .sizeimage = 160 * 120,
103 .colorspace = V4L2_COLORSPACE_SRGB,
105 {176, 144, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
107 .sizeimage = 176 * 144,
108 .colorspace = V4L2_COLORSPACE_SRGB,
110 {320, 240, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
112 .sizeimage = 320 * 240,
113 .colorspace = V4L2_COLORSPACE_SRGB,
115 {352, 288, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
117 .sizeimage = 352 * 288,
118 .colorspace = V4L2_COLORSPACE_SRGB,
123 * Initialization data
124 * I'm not very sure how to split initialization from open data
125 * chunks. For now, we'll consider everything as initialization
127 /* Frame packet header offsets for the spca561 */
128 #define SPCA561_OFFSET_SNAP 1
129 #define SPCA561_OFFSET_TYPE 2
130 #define SPCA561_OFFSET_COMPRESS 3
131 #define SPCA561_OFFSET_FRAMSEQ 4
132 #define SPCA561_OFFSET_GPIO 5
133 #define SPCA561_OFFSET_USBBUFF 6
134 #define SPCA561_OFFSET_WIN2GRAVE 7
135 #define SPCA561_OFFSET_WIN2RAVE 8
136 #define SPCA561_OFFSET_WIN2BAVE 9
137 #define SPCA561_OFFSET_WIN2GBAVE 10
138 #define SPCA561_OFFSET_WIN1GRAVE 11
139 #define SPCA561_OFFSET_WIN1RAVE 12
140 #define SPCA561_OFFSET_WIN1BAVE 13
141 #define SPCA561_OFFSET_WIN1GBAVE 14
142 #define SPCA561_OFFSET_FREQ 15
143 #define SPCA561_OFFSET_VSYNC 16
144 #define SPCA561_OFFSET_DATA 1
145 #define SPCA561_INDEX_I2C_BASE 0x8800
146 #define SPCA561_SNAPBIT 0x20
147 #define SPCA561_SNAPCTRL 0x40
149 static void reg_w_val(struct usb_device *dev, __u16 index, __u8 value)
153 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
155 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
156 value, index, NULL, 0, 500);
157 PDEBUG(D_USBO, "reg write: 0x%02x:0x%02x", index, value);
159 PDEBUG(D_ERR, "reg write: error %d", ret);
162 static void write_vector(struct gspca_dev *gspca_dev,
163 const __u16 data[][2])
165 struct usb_device *dev = gspca_dev->dev;
169 while (data[i][1] != 0) {
170 reg_w_val(dev, data[i][1], data[i][0]);
175 /* read 'len' bytes to gspca_dev->usb_buf */
176 static void reg_r(struct gspca_dev *gspca_dev,
177 __u16 index, __u16 length)
179 usb_control_msg(gspca_dev->dev,
180 usb_rcvctrlpipe(gspca_dev->dev, 0),
182 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
184 index, gspca_dev->usb_buf, length, 500);
187 static void reg_w_buf(struct gspca_dev *gspca_dev,
188 __u16 index, const __u8 *buffer, __u16 len)
190 memcpy(gspca_dev->usb_buf, buffer, len);
191 usb_control_msg(gspca_dev->dev,
192 usb_sndctrlpipe(gspca_dev->dev, 0),
194 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
196 index, gspca_dev->usb_buf, len, 500);
199 static void i2c_write(struct gspca_dev *gspca_dev, __u16 valeur, __u16 reg)
206 DataHight = valeur >> 8;
207 reg_w_val(gspca_dev->dev, 0x8801, reg);
208 reg_w_val(gspca_dev->dev, 0x8805, DataLow);
209 reg_w_val(gspca_dev->dev, 0x8800, DataHight);
211 reg_r(gspca_dev, 0x8803, 1);
212 if (!gspca_dev->usb_buf[0])
217 static int i2c_read(struct gspca_dev *gspca_dev, __u16 reg, __u8 mode)
223 reg_w_val(gspca_dev->dev, 0x8804, 0x92);
224 reg_w_val(gspca_dev->dev, 0x8801, reg);
225 reg_w_val(gspca_dev->dev, 0x8802, (mode | 0x01));
227 reg_r(gspca_dev, 0x8803, 1);
228 if (!gspca_dev->usb_buf[0])
233 reg_r(gspca_dev, 0x8800, 1);
234 value = gspca_dev->usb_buf[0];
235 reg_r(gspca_dev, 0x8805, 1);
236 vallsb = gspca_dev->usb_buf[0];
237 return ((int) value << 8) | vallsb;
240 static const __u16 spca561_init_data[][2] = {
241 {0x0000, 0x8114}, /* Software GPIO output data */
242 {0x0001, 0x8114}, /* Software GPIO output data */
243 {0x0000, 0x8112}, /* Some kind of reset */
244 {0x0003, 0x8701}, /* PCLK clock delay adjustment */
245 {0x0001, 0x8703}, /* HSYNC from cmos inverted */
246 {0x0011, 0x8118}, /* Enable and conf sensor */
247 {0x0001, 0x8118}, /* Conf sensor */
248 {0x0092, 0x8804}, /* I know nothing about these */
249 {0x0010, 0x8802}, /* 0x88xx registers, so I won't */
251 {0x000d, 0x8805}, /* sensor default setting */
252 {0x0001, 0x8801}, /* 1 <- 0x0d */
255 {0x0002, 0x8801}, /* 2 <- 0x18 */
258 {0x0004, 0x8801}, /* 4 <- 0x01 0x65 */
261 {0x0005, 0x8801}, /* 5 <- 0x21 */
264 {0x0007, 0x8801}, /* 7 <- 0xaa */
267 {0x0020, 0x8801}, /* 0x20 <- 0x15 0x04 */
270 {0x0039, 0x8801}, /* 0x39 <- 0x02 */
273 {0x0035, 0x8801}, /* 0x35 <- 0x10 */
276 {0x0009, 0x8801}, /* 0x09 <- 0x10 0x49 */
279 {0x0028, 0x8801}, /* 0x28 <- 0x0b */
282 {0x003b, 0x8801}, /* 0x3b <- 0x0f */
285 {0x003c, 0x8801}, /* 0x3c <- 0x00 */
288 {0x0018, 0x8601}, /* Pixel/line selection for color separation */
289 {0x0000, 0x8602}, /* Optical black level for user setting */
290 {0x0060, 0x8604}, /* Optical black horizontal offset */
291 {0x0002, 0x8605}, /* Optical black vertical offset */
292 {0x0000, 0x8603}, /* Non-automatic optical black level */
293 {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
294 {0x0000, 0x865f}, /* Vertical valid pixels window (x2) */
295 {0x00b0, 0x865d}, /* Horizontal valid pixels window (x2) */
296 {0x0090, 0x865e}, /* Vertical valid lines window (x2) */
297 {0x00e0, 0x8406}, /* Memory buffer threshold */
298 {0x0000, 0x8660}, /* Compensation memory stuff */
299 {0x0002, 0x8201}, /* Output address for r/w serial EEPROM */
300 {0x0008, 0x8200}, /* Clear valid bit for serial EEPROM */
301 {0x0001, 0x8200}, /* OprMode to be executed by hardware */
302 {0x0007, 0x8201}, /* Output address for r/w serial EEPROM */
303 {0x0008, 0x8200}, /* Clear valid bit for serial EEPROM */
304 {0x0001, 0x8200}, /* OprMode to be executed by hardware */
305 {0x0010, 0x8660}, /* Compensation memory stuff */
306 {0x0018, 0x8660}, /* Compensation memory stuff */
308 {0x0004, 0x8611}, /* R offset for white balance */
309 {0x0004, 0x8612}, /* Gr offset for white balance */
310 {0x0007, 0x8613}, /* B offset for white balance */
311 {0x0000, 0x8614}, /* Gb offset for white balance */
312 {0x008c, 0x8651}, /* R gain for white balance */
313 {0x008c, 0x8652}, /* Gr gain for white balance */
314 {0x00b5, 0x8653}, /* B gain for white balance */
315 {0x008c, 0x8654}, /* Gb gain for white balance */
316 {0x0002, 0x8502}, /* Maximum average bit rate stuff */
319 {0x0087, 0x8700}, /* Set master clock (96Mhz????) */
320 {0x0081, 0x8702}, /* Master clock output enable */
322 {0x0000, 0x8500}, /* Set image type (352x288 no compression) */
323 /* Originally was 0x0010 (352x288 compression) */
325 {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
326 {0x0003, 0x865c}, /* Vertical offset for valid lines */
327 /***************//* sensor active */
328 {0x0003, 0x8801}, /* 0x03 <- 0x01 0x21 //289 */
331 {0x0004, 0x8801}, /* 0x04 <- 0x01 0x65 //357 */
334 {0x0005, 0x8801}, /* 0x05 <- 0x2f */
337 {0x0006, 0x8801}, /* 0x06 <- 0 */
340 {0x000a, 0x8801}, /* 0x0a <- 2 */
343 {0x0009, 0x8801}, /* 0x09 <- 0x1061 */
346 {0x0035, 0x8801}, /* 0x35 <-0x14 */
349 {0x0030, 0x8112}, /* ISO and drop packet enable */
350 {0x0000, 0x8112}, /* Some kind of reset ???? */
351 {0x0009, 0x8118}, /* Enable sensor and set standby */
352 {0x0000, 0x8114}, /* Software GPIO output data */
353 {0x0000, 0x8114}, /* Software GPIO output data */
354 {0x0001, 0x8114}, /* Software GPIO output data */
355 {0x0000, 0x8112}, /* Some kind of reset ??? */
376 {0x0007, 0x8801}, /* mode 0xaa */
380 {0x0015, 0x8800}, /* mode 0x0415 */
404 {0x0087, 0x8700}, /* overwrite by start */
407 /* {0x0010, 0x8500}, -- Previous line was this */
411 {0x0003, 0x8801}, /* 0x121-> 289 */
414 {0x0004, 0x8801}, /* 0x165 -> 357 */
417 {0x0005, 0x8801}, /* 0x2f //blanking control colonne */
420 {0x0006, 0x8801}, /* 0x00 //blanking mode row */
423 {0x000a, 0x8801}, /* 0x01 //0x02 */
426 {0x0009, 0x8801}, /* 0x1061 - setexposure times && pixel clock
427 * 0001 0 | 000 0110 0001 */
428 {0x0061, 0x8805}, /* 61 31 */
429 {0x0008, 0x8800}, /* 08 */
430 {0x0035, 0x8801}, /* 0x14 - set gain general */
431 {0x001f, 0x8805}, /* 0x14 */
433 {0x000e, 0x8112}, /* white balance - was 30 */
438 /******************** QC Express etch2 stuff ********************/
439 static const __u16 Pb100_1map8300[][2] = {
443 {0x8303, 0x0125}, /* image area */
446 {0x833c, 0x0001}, /*fixme: win:07*/
448 {0x832f, 0x1904}, /*fixme: was 0419*/
454 static const __u16 Pb100_2map8300[][2] = {
461 static const __u16 spca561_161rev12A_data1[][2] = {
462 {0x29, 0x8118}, /* white balance - was 21 */
463 {0x08, 0x8114}, /* white balance - was 01 */
464 {0x0e, 0x8112}, /* white balance - was 00 */
465 {0x00, 0x8102}, /* white balance - new */
467 {0x04, 0x8802}, /* windows uses 08 */
470 static const __u16 spca561_161rev12A_data2[][2] = {
478 {0x07, 0x8201}, /* windows uses 02 */
483 {0x01, 0x8114}, /* windows uses 00 */
490 {0x07, 0x8601}, /* white balance - new */
491 {0x07, 0x8602}, /* white balance - new */
492 {0x00, 0x8610}, /* *red */
493 {0x00, 0x8611}, /* 3f *green */
494 {0x00, 0x8612}, /* green *blue */
495 {0x00, 0x8613}, /* blue *green */
496 {0x43, 0x8614}, /* green *red - white balance - was 0x35 */
497 {0x40, 0x8615}, /* 40 *green - white balance - was 0x35 */
498 {0x71, 0x8616}, /* 7a *blue - white balance - was 0x35 */
499 {0x40, 0x8617}, /* 40 *green - white balance - was 0x35 */
501 {0x0c, 0x8620}, /* 0c */
502 {0xc8, 0x8631}, /* c8 */
503 {0xc8, 0x8634}, /* c8 */
504 {0x23, 0x8635}, /* 23 */
505 {0x1f, 0x8636}, /* 1f */
506 {0xdd, 0x8637}, /* dd */
507 {0xe1, 0x8638}, /* e1 */
508 {0x1d, 0x8639}, /* 1d */
509 {0x21, 0x863a}, /* 21 */
510 {0xe3, 0x863b}, /* e3 */
511 {0xdf, 0x863c}, /* df */
514 /* {0x99, 0x8700}, * - white balance - new (removed) */
518 static void sensor_mapwrite(struct gspca_dev *gspca_dev,
519 const __u16 sensormap[][2])
524 while (sensormap[i][0]) {
525 usbval[0] = sensormap[i][1];
526 usbval[1] = sensormap[i][1] >> 8;
527 reg_w_buf(gspca_dev, sensormap[i][0], usbval, 2);
531 static void init_161rev12A(struct gspca_dev *gspca_dev)
533 /* sensor_reset(gspca_dev); (not in win) */
534 write_vector(gspca_dev, spca561_161rev12A_data1);
535 sensor_mapwrite(gspca_dev, Pb100_1map8300);
536 /*fixme: should be in sd_start*/
537 write_vector(gspca_dev, spca561_161rev12A_data2);
538 sensor_mapwrite(gspca_dev, Pb100_2map8300);
541 /* this function is called at probe time */
542 static int sd_config(struct gspca_dev *gspca_dev,
543 const struct usb_device_id *id)
545 struct sd *sd = (struct sd *) gspca_dev;
547 __u16 vendor, product;
550 /* Read frm global register the USB product and vendor IDs, just to
551 * prove that we can communicate with the device. This works, which
552 * confirms at we are communicating properly and that the device
554 reg_r(gspca_dev, 0x8104, 1);
555 data1 = gspca_dev->usb_buf[0];
556 reg_r(gspca_dev, 0x8105, 1);
557 data2 = gspca_dev->usb_buf[0];
558 vendor = (data2 << 8) | data1;
559 reg_r(gspca_dev, 0x8106, 1);
560 data1 = gspca_dev->usb_buf[0];
561 reg_r(gspca_dev, 0x8107, 1);
562 data2 = gspca_dev->usb_buf[0];
563 product = (data2 << 8) | data1;
564 if (vendor != id->idVendor || product != id->idProduct) {
565 PDEBUG(D_PROBE, "Bad vendor / product from device");
569 cam = &gspca_dev->cam;
571 gspca_dev->nbalt = 7 + 1; /* choose alternate 7 first */
573 sd->chip_revision = id->driver_info;
574 if (sd->chip_revision == Rev012A) {
575 cam->cam_mode = sif_012a_mode;
576 cam->nmodes = ARRAY_SIZE(sif_012a_mode);
578 cam->cam_mode = sif_072a_mode;
579 cam->nmodes = ARRAY_SIZE(sif_072a_mode);
581 sd->brightness = BRIGHTNESS_DEF;
582 sd->contrast = CONTRAST_DEF;
583 sd->white = WHITE_DEF;
584 sd->exposure = EXPOSURE_DEF;
585 sd->autogain = AUTOGAIN_DEF;
587 sd->expo12a = EXPO12A_DEF;
591 /* this function is called at probe and resume time */
592 static int sd_init_12a(struct gspca_dev *gspca_dev)
594 PDEBUG(D_STREAM, "Chip revision: 012a");
595 init_161rev12A(gspca_dev);
598 static int sd_init_72a(struct gspca_dev *gspca_dev)
600 PDEBUG(D_STREAM, "Chip revision: 072a");
601 write_vector(gspca_dev, spca561_init_data);
605 static void setcontrast(struct gspca_dev *gspca_dev)
607 struct sd *sd = (struct sd *) gspca_dev;
608 struct usb_device *dev = gspca_dev->dev;
611 switch (sd->chip_revision) {
613 lowb = sd->contrast >> 8;
614 reg_w_val(dev, 0x8651, lowb);
615 reg_w_val(dev, 0x8652, lowb);
616 reg_w_val(dev, 0x8653, lowb);
617 reg_w_val(dev, 0x8654, lowb);
620 /* case Rev012A: { */
621 static const __u8 Reg8391[] =
622 { 0x92, 0x30, 0x20, 0x00, 0x0c, 0x00, 0x00, 0x00 };
624 reg_w_buf(gspca_dev, 0x8391, Reg8391, 8);
625 reg_w_buf(gspca_dev, 0x8390, Reg8391, 8);
632 static void setwhite(struct gspca_dev *gspca_dev)
634 struct sd *sd = (struct sd *) gspca_dev;
636 __u8 reg8614, reg8616;
639 /* try to emulate MS-win as possible */
640 reg8616 = 0x90 - white * 5 / 8;
641 reg_w_val(gspca_dev->dev, 0x8616, reg8616);
642 reg8614 = 0x20 + white * 3 / 8;
643 reg_w_val(gspca_dev->dev, 0x8614, reg8614);
647 static void setexposure(struct gspca_dev *gspca_dev)
649 struct sd *sd = (struct sd *) gspca_dev;
654 /* Register 0x8309 controls exposure for the spca561,
655 the basic exposure setting goes from 1-2047, where 1 is completely
656 dark and 2047 is very bright. It not only influences exposure but
657 also the framerate (to allow for longer exposure) from 1 - 300 it
658 only raises the exposure time then from 300 - 600 it halves the
659 framerate to be able to further raise the exposure time and for every
660 300 more it halves the framerate again. This allows for a maximum
661 exposure time of circa 0.2 - 0.25 seconds (30 / (2000/3000) fps).
662 Sometimes this is not enough, the 1-2047 uses bits 0-10, bits 11-12
663 configure a divider for the base framerate which us used at the
664 exposure setting of 1-300. These bits configure the base framerate
665 according to the following formula: fps = 60 / (value + 2) */
666 if (sd->exposure < 2048) {
670 /* Add 900 to make the 0 setting of the second part of the
671 exposure equal to the 2047 setting of the first part. */
672 expo = (sd->exposure - 2048) + 900;
675 expo |= clock_divider << 11;
678 reg_w_buf(gspca_dev, 0x8309, data, 2);
682 static void setgain(struct gspca_dev *gspca_dev)
684 struct sd *sd = (struct sd *) gspca_dev;
689 reg_w_buf(gspca_dev, 0x8335, data, 2);
692 static void setautogain(struct gspca_dev *gspca_dev)
694 struct sd *sd = (struct sd *) gspca_dev;
697 sd->ag_cnt = AG_CNT_START;
702 static int sd_start_12a(struct gspca_dev *gspca_dev)
704 struct usb_device *dev = gspca_dev->dev;
705 int Clck = 0x8a; /* lower 0x8X values lead to fps > 30 */
706 __u8 Reg8307[] = { 0xaa, 0x00 };
709 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
711 /* Use compression on 320x240 and above */
712 reg_w_val(dev, 0x8500, 0x10 | mode);
714 /* I couldn't get the compression to work below 320x240
715 * Fortunately at these resolutions the bandwidth
716 * is sufficient to push raw frames at ~20fps */
717 reg_w_val(dev, 0x8500, mode);
718 } /* -- qq@kuku.eu.org */
719 reg_w_buf(gspca_dev, 0x8307, Reg8307, 2);
720 reg_w_val(gspca_dev->dev, 0x8700, Clck);
721 /* 0x8f 0x85 0x27 clock */
722 reg_w_val(gspca_dev->dev, 0x8112, 0x1e | 0x20);
723 reg_w_val(gspca_dev->dev, 0x850b, 0x03);
724 setcontrast(gspca_dev);
726 setautogain(gspca_dev);
727 setexposure(gspca_dev);
730 static int sd_start_72a(struct gspca_dev *gspca_dev)
732 struct usb_device *dev = gspca_dev->dev;
736 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
750 reg_w_val(dev, 0x8500, mode); /* mode */
751 reg_w_val(dev, 0x8700, Clck); /* 0x27 clock */
752 reg_w_val(dev, 0x8112, 0x10 | 0x20);
753 setautogain(gspca_dev);
757 static void sd_stopN(struct gspca_dev *gspca_dev)
759 struct sd *sd = (struct sd *) gspca_dev;
761 if (sd->chip_revision == Rev012A) {
762 reg_w_val(gspca_dev->dev, 0x8112, 0x0e);
764 reg_w_val(gspca_dev->dev, 0x8112, 0x20);
765 /* reg_w_val(gspca_dev->dev, 0x8102, 0x00); ?? */
769 /* called on streamoff with alt 0 and on disconnect */
770 static void sd_stop0(struct gspca_dev *gspca_dev)
772 struct sd *sd = (struct sd *) gspca_dev;
774 if (!gspca_dev->present)
776 if (sd->chip_revision == Rev012A) {
777 reg_w_val(gspca_dev->dev, 0x8118, 0x29);
778 reg_w_val(gspca_dev->dev, 0x8114, 0x08);
780 /* reg_w_val(gspca_dev->dev, 0x8114, 0); */
783 static void do_autogain(struct gspca_dev *gspca_dev)
785 struct sd *sd = (struct sd *) gspca_dev;
791 __u8 luma_mean = 110;
792 __u8 luma_delta = 20;
798 if (--sd->ag_cnt >= 0)
800 sd->ag_cnt = AG_CNT_START;
802 switch (sd->chip_revision) {
804 reg_r(gspca_dev, 0x8621, 1);
805 Gr = gspca_dev->usb_buf[0];
806 reg_r(gspca_dev, 0x8622, 1);
807 R = gspca_dev->usb_buf[0];
808 reg_r(gspca_dev, 0x8623, 1);
809 B = gspca_dev->usb_buf[0];
810 reg_r(gspca_dev, 0x8624, 1);
811 Gb = gspca_dev->usb_buf[0];
812 y = (77 * R + 75 * (Gr + Gb) + 29 * B) >> 8;
813 /* u= (128*B-(43*(Gr+Gb+R))) >> 8; */
814 /* v= (128*R-(53*(Gr+Gb))-21*B) >> 8; */
815 /* PDEBUG(D_CONF,"reading Y %d U %d V %d ",y,u,v); */
817 if (y < luma_mean - luma_delta ||
818 y > luma_mean + luma_delta) {
819 expotimes = i2c_read(gspca_dev, 0x09, 0x10);
821 expotimes = expotimes & 0x07ff;
823 "Exposition Times 0x%03X Clock 0x%04X ",
824 expotimes,pixelclk); */
825 gainG = i2c_read(gspca_dev, 0x35, 0x10);
827 "reading Gain register %d", gainG); */
829 expotimes += (luma_mean - y) >> spring;
830 gainG += (luma_mean - y) / 50;
832 "compute expotimes %d gain %d",
839 i2c_write(gspca_dev, gainG, 0x35);
841 if (expotimes >= 0x0256)
843 else if (expotimes < 4)
845 i2c_write(gspca_dev, expotimes | pixelclk, 0x09);
849 reg_r(gspca_dev, 0x8330, 2);
850 if (gspca_dev->usb_buf[1] > 0x08) {
851 reg8339[0] = ++sd->expo12a;
853 reg_w_buf(gspca_dev, 0x8339, reg8339, 2);
854 } else if (gspca_dev->usb_buf[1] < 0x02) {
855 reg8339[0] = --sd->expo12a;
857 reg_w_buf(gspca_dev, 0x8339, reg8339, 2);
863 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
864 struct gspca_frame *frame, /* target */
865 __u8 *data, /* isoc packet */
866 int len) /* iso packet length */
868 struct sd *sd = (struct sd *) gspca_dev;
871 case 0: /* start of frame */
872 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
874 data += SPCA561_OFFSET_DATA;
875 len -= SPCA561_OFFSET_DATA;
876 if (data[1] & 0x10) {
877 /* compressed bayer */
878 gspca_frame_add(gspca_dev, FIRST_PACKET,
881 /* raw bayer (with a header, which we skip) */
882 if (sd->chip_revision == Rev012A) {
889 gspca_frame_add(gspca_dev, FIRST_PACKET,
893 case 0xff: /* drop */
894 /* gspca_dev->last_packet_type = DISCARD_PACKET; */
899 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
903 static void setbrightness(struct gspca_dev *gspca_dev)
905 struct sd *sd = (struct sd *) gspca_dev;
908 value = sd->brightness;
909 reg_w_val(gspca_dev->dev, 0x8611, value);
910 reg_w_val(gspca_dev->dev, 0x8612, value);
911 reg_w_val(gspca_dev->dev, 0x8613, value);
912 reg_w_val(gspca_dev->dev, 0x8614, value);
915 static void getbrightness(struct gspca_dev *gspca_dev)
917 struct sd *sd = (struct sd *) gspca_dev;
921 reg_r(gspca_dev, 0x8611, 1);
922 tot += gspca_dev->usb_buf[0];
923 reg_r(gspca_dev, 0x8612, 1);
924 tot += gspca_dev->usb_buf[0];
925 reg_r(gspca_dev, 0x8613, 1);
926 tot += gspca_dev->usb_buf[0];
927 reg_r(gspca_dev, 0x8614, 1);
928 tot += gspca_dev->usb_buf[0];
929 sd->brightness = tot >> 2;
933 static void getcontrast(struct gspca_dev *gspca_dev)
935 struct sd *sd = (struct sd *) gspca_dev;
939 reg_r(gspca_dev, 0x8651, 1);
940 tot += gspca_dev->usb_buf[0];
941 reg_r(gspca_dev, 0x8652, 1);
942 tot += gspca_dev->usb_buf[0];
943 reg_r(gspca_dev, 0x8653, 1);
944 tot += gspca_dev->usb_buf[0];
945 reg_r(gspca_dev, 0x8654, 1);
946 tot += gspca_dev->usb_buf[0];
947 sd->contrast = tot << 6;
948 PDEBUG(D_CONF, "get contrast %d", sd->contrast);
952 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
954 struct sd *sd = (struct sd *) gspca_dev;
956 sd->brightness = val;
957 if (gspca_dev->streaming)
958 setbrightness(gspca_dev);
962 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
964 struct sd *sd = (struct sd *) gspca_dev;
966 getbrightness(gspca_dev);
967 *val = sd->brightness;
972 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
974 struct sd *sd = (struct sd *) gspca_dev;
977 if (gspca_dev->streaming)
978 setcontrast(gspca_dev);
982 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
984 struct sd *sd = (struct sd *) gspca_dev;
986 getcontrast(gspca_dev);
991 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
993 struct sd *sd = (struct sd *) gspca_dev;
996 if (gspca_dev->streaming)
997 setautogain(gspca_dev);
1001 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
1003 struct sd *sd = (struct sd *) gspca_dev;
1005 *val = sd->autogain;
1010 static int sd_setwhite(struct gspca_dev *gspca_dev, __s32 val)
1012 struct sd *sd = (struct sd *) gspca_dev;
1015 if (gspca_dev->streaming)
1016 setwhite(gspca_dev);
1020 static int sd_getwhite(struct gspca_dev *gspca_dev, __s32 *val)
1022 struct sd *sd = (struct sd *) gspca_dev;
1029 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
1031 struct sd *sd = (struct sd *) gspca_dev;
1034 if (gspca_dev->streaming)
1035 setexposure(gspca_dev);
1039 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
1041 struct sd *sd = (struct sd *) gspca_dev;
1043 *val = sd->exposure;
1048 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
1050 struct sd *sd = (struct sd *) gspca_dev;
1053 if (gspca_dev->streaming)
1058 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
1060 struct sd *sd = (struct sd *) gspca_dev;
1066 /* control tables */
1067 static struct ctrl sd_ctrls_12a[] = {
1070 .id = V4L2_CID_DO_WHITE_BALANCE,
1071 .type = V4L2_CTRL_TYPE_INTEGER,
1072 .name = "White Balance",
1073 .minimum = WHITE_MIN,
1074 .maximum = WHITE_MAX,
1076 .default_value = WHITE_DEF,
1083 .id = V4L2_CID_EXPOSURE,
1084 .type = V4L2_CTRL_TYPE_INTEGER,
1086 .minimum = EXPOSURE_MIN,
1087 .maximum = EXPOSURE_MAX,
1089 .default_value = EXPOSURE_DEF,
1091 .set = sd_setexposure,
1092 .get = sd_getexposure,
1096 .id = V4L2_CID_AUTOGAIN,
1097 .type = V4L2_CTRL_TYPE_BOOLEAN,
1098 .name = "Auto Gain",
1099 .minimum = AUTOGAIN_MIN,
1100 .maximum = AUTOGAIN_MAX,
1102 .default_value = AUTOGAIN_DEF,
1104 .set = sd_setautogain,
1105 .get = sd_getautogain,
1109 .id = V4L2_CID_GAIN,
1110 .type = V4L2_CTRL_TYPE_INTEGER,
1112 .minimum = GAIN_MIN,
1113 .maximum = GAIN_MAX,
1115 .default_value = GAIN_DEF,
1122 static struct ctrl sd_ctrls_72a[] = {
1125 .id = V4L2_CID_BRIGHTNESS,
1126 .type = V4L2_CTRL_TYPE_INTEGER,
1127 .name = "Brightness",
1128 .minimum = BRIGHTNESS_MIN,
1129 .maximum = BRIGHTNESS_MAX,
1131 .default_value = BRIGHTNESS_DEF,
1133 .set = sd_setbrightness,
1134 .get = sd_getbrightness,
1138 .id = V4L2_CID_CONTRAST,
1139 .type = V4L2_CTRL_TYPE_INTEGER,
1141 .minimum = CONTRAST_MIN,
1142 .maximum = CONTRAST_MAX,
1144 .default_value = CONTRAST_DEF,
1146 .set = sd_setcontrast,
1147 .get = sd_getcontrast,
1151 .id = V4L2_CID_AUTOGAIN,
1152 .type = V4L2_CTRL_TYPE_BOOLEAN,
1153 .name = "Auto Gain",
1154 .minimum = AUTOGAIN_MIN,
1155 .maximum = AUTOGAIN_MAX,
1157 .default_value = AUTOGAIN_DEF,
1159 .set = sd_setautogain,
1160 .get = sd_getautogain,
1164 /* sub-driver description */
1165 static const struct sd_desc sd_desc_12a = {
1166 .name = MODULE_NAME,
1167 .ctrls = sd_ctrls_12a,
1168 .nctrls = ARRAY_SIZE(sd_ctrls_12a),
1169 .config = sd_config,
1170 .init = sd_init_12a,
1171 .start = sd_start_12a,
1174 .pkt_scan = sd_pkt_scan,
1175 /* .dq_callback = do_autogain, * fixme */
1177 static const struct sd_desc sd_desc_72a = {
1178 .name = MODULE_NAME,
1179 .ctrls = sd_ctrls_72a,
1180 .nctrls = ARRAY_SIZE(sd_ctrls_72a),
1181 .config = sd_config,
1182 .init = sd_init_72a,
1183 .start = sd_start_72a,
1186 .pkt_scan = sd_pkt_scan,
1187 .dq_callback = do_autogain,
1189 static const struct sd_desc *sd_desc[2] = {
1194 /* -- module initialisation -- */
1195 static const __devinitdata struct usb_device_id device_table[] = {
1196 {USB_DEVICE(0x041e, 0x401a), .driver_info = Rev072A},
1197 {USB_DEVICE(0x041e, 0x403b), .driver_info = Rev012A},
1198 {USB_DEVICE(0x0458, 0x7004), .driver_info = Rev072A},
1199 {USB_DEVICE(0x046d, 0x0928), .driver_info = Rev012A},
1200 {USB_DEVICE(0x046d, 0x0929), .driver_info = Rev012A},
1201 {USB_DEVICE(0x046d, 0x092a), .driver_info = Rev012A},
1202 {USB_DEVICE(0x046d, 0x092b), .driver_info = Rev012A},
1203 {USB_DEVICE(0x046d, 0x092c), .driver_info = Rev012A},
1204 {USB_DEVICE(0x046d, 0x092d), .driver_info = Rev012A},
1205 {USB_DEVICE(0x046d, 0x092e), .driver_info = Rev012A},
1206 {USB_DEVICE(0x046d, 0x092f), .driver_info = Rev012A},
1207 {USB_DEVICE(0x04fc, 0x0561), .driver_info = Rev072A},
1208 {USB_DEVICE(0x060b, 0xa001), .driver_info = Rev072A},
1209 {USB_DEVICE(0x10fd, 0x7e50), .driver_info = Rev072A},
1210 {USB_DEVICE(0xabcd, 0xcdee), .driver_info = Rev072A},
1214 MODULE_DEVICE_TABLE(usb, device_table);
1216 /* -- device connect -- */
1217 static int sd_probe(struct usb_interface *intf,
1218 const struct usb_device_id *id)
1220 return gspca_dev_probe(intf, id,
1221 sd_desc[id->driver_info],
1226 static struct usb_driver sd_driver = {
1227 .name = MODULE_NAME,
1228 .id_table = device_table,
1230 .disconnect = gspca_disconnect,
1232 .suspend = gspca_suspend,
1233 .resume = gspca_resume,
1237 /* -- module insert / remove -- */
1238 static int __init sd_mod_init(void)
1240 if (usb_register(&sd_driver) < 0)
1242 PDEBUG(D_PROBE, "registered");
1245 static void __exit sd_mod_exit(void)
1247 usb_deregister(&sd_driver);
1248 PDEBUG(D_PROBE, "deregistered");
1251 module_init(sd_mod_init);
1252 module_exit(sd_mod_exit);