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 exposure; /* rev12a only */
36 #define EXPOSURE_MIN 1
37 #define EXPOSURE_DEF 200
38 #define EXPOSURE_MAX (4095 - 900) /* see set_exposure */
40 __u8 contrast; /* rev72a only */
41 #define CONTRAST_MIN 0x00
42 #define CONTRAST_DEF 0x20
43 #define CONTRAST_MAX 0x3f
45 __u8 brightness; /* rev72a only */
46 #define BRIGHTNESS_MIN 0
47 #define BRIGHTNESS_DEF 0x20
48 #define BRIGHTNESS_MAX 0x3f
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 const 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 const 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_INDEX_I2C_BASE 0x8800
145 #define SPCA561_SNAPBIT 0x20
146 #define SPCA561_SNAPCTRL 0x40
148 static const u16 rev72a_reset[][2] = {
149 {0x0000, 0x8114}, /* Software GPIO output data */
150 {0x0001, 0x8114}, /* Software GPIO output data */
151 {0x0000, 0x8112}, /* Some kind of reset */
154 static const __u16 rev72a_init_data1[][2] = {
155 {0x0003, 0x8701}, /* PCLK clock delay adjustment */
156 {0x0001, 0x8703}, /* HSYNC from cmos inverted */
157 {0x0011, 0x8118}, /* Enable and conf sensor */
158 {0x0001, 0x8118}, /* Conf sensor */
159 {0x0092, 0x8804}, /* I know nothing about these */
160 {0x0010, 0x8802}, /* 0x88xx registers, so I won't */
163 static const u16 rev72a_init_sensor1[][2] = {
178 static const __u16 rev72a_init_data2[][2] = {
179 {0x0018, 0x8601}, /* Pixel/line selection for color separation */
180 {0x0000, 0x8602}, /* Optical black level for user setting */
181 {0x0060, 0x8604}, /* Optical black horizontal offset */
182 {0x0002, 0x8605}, /* Optical black vertical offset */
183 {0x0000, 0x8603}, /* Non-automatic optical black level */
184 {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
185 {0x0000, 0x865f}, /* Vertical valid pixels window (x2) */
186 {0x00b0, 0x865d}, /* Horizontal valid pixels window (x2) */
187 {0x0090, 0x865e}, /* Vertical valid lines window (x2) */
188 {0x00e0, 0x8406}, /* Memory buffer threshold */
189 {0x0000, 0x8660}, /* Compensation memory stuff */
190 {0x0002, 0x8201}, /* Output address for r/w serial EEPROM */
191 {0x0008, 0x8200}, /* Clear valid bit for serial EEPROM */
192 {0x0001, 0x8200}, /* OprMode to be executed by hardware */
194 {0x0000, 0x8611}, /* R offset for white balance */
195 {0x00fd, 0x8612}, /* Gr offset for white balance */
196 {0x0003, 0x8613}, /* B offset for white balance */
197 {0x0000, 0x8614}, /* Gb offset for white balance */
199 {0x0035, 0x8651}, /* R gain for white balance */
200 {0x0040, 0x8652}, /* Gr gain for white balance */
201 {0x005f, 0x8653}, /* B gain for white balance */
202 {0x0040, 0x8654}, /* Gb gain for white balance */
203 {0x0002, 0x8502}, /* Maximum average bit rate stuff */
206 {0x0087, 0x8700}, /* Set master clock (96Mhz????) */
207 {0x0081, 0x8702}, /* Master clock output enable */
209 {0x0000, 0x8500}, /* Set image type (352x288 no compression) */
210 /* Originally was 0x0010 (352x288 compression) */
212 {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
213 {0x0003, 0x865c}, /* Vertical offset for valid lines */
216 static const u16 rev72a_init_sensor2[][2] = {
219 {0x0005, 0x002f}, /* blanking control column */
220 {0x0006, 0x0000}, /* blanking mode row*/
222 {0x0009, 0x1061}, /* setexposure times && pixel clock
223 * 0001 0 | 000 0110 0001 */
228 /******************** QC Express etch2 stuff ********************/
229 static const __u16 Pb100_1map8300[][2] = {
233 {0x8303, 0x0125}, /* image area */
236 {0x833c, 0x0001}, /*fixme: win:07*/
238 {0x832f, 0x1904}, /*fixme: was 0419*/
244 static const __u16 Pb100_2map8300[][2] = {
251 static const __u16 spca561_161rev12A_data1[][2] = {
252 {0x29, 0x8118}, /* white balance - was 21 */
253 {0x08, 0x8114}, /* white balance - was 01 */
254 {0x0e, 0x8112}, /* white balance - was 00 */
255 {0x00, 0x8102}, /* white balance - new */
257 {0x04, 0x8802}, /* windows uses 08 */
260 static const __u16 spca561_161rev12A_data2[][2] = {
268 {0x07, 0x8201}, /* windows uses 02 */
273 {0x01, 0x8114}, /* windows uses 00 */
280 {0x07, 0x8601}, /* white balance - new */
281 {0x07, 0x8602}, /* white balance - new */
282 {0x00, 0x8610}, /* *red */
283 {0x00, 0x8611}, /* 3f *green */
284 {0x00, 0x8612}, /* green *blue */
285 {0x00, 0x8613}, /* blue *green */
286 {0x43, 0x8614}, /* green *red - white balance - was 0x35 */
287 {0x40, 0x8615}, /* 40 *green - white balance - was 0x35 */
288 {0x71, 0x8616}, /* 7a *blue - white balance - was 0x35 */
289 {0x40, 0x8617}, /* 40 *green - white balance - was 0x35 */
291 {0x0c, 0x8620}, /* 0c */
292 {0xc8, 0x8631}, /* c8 */
293 {0xc8, 0x8634}, /* c8 */
294 {0x23, 0x8635}, /* 23 */
295 {0x1f, 0x8636}, /* 1f */
296 {0xdd, 0x8637}, /* dd */
297 {0xe1, 0x8638}, /* e1 */
298 {0x1d, 0x8639}, /* 1d */
299 {0x21, 0x863a}, /* 21 */
300 {0xe3, 0x863b}, /* e3 */
301 {0xdf, 0x863c}, /* df */
304 /* {0x99, 0x8700}, * - white balance - new (removed) */
308 static void reg_w_val(struct usb_device *dev, __u16 index, __u8 value)
312 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
314 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
315 value, index, NULL, 0, 500);
316 PDEBUG(D_USBO, "reg write: 0x%02x:0x%02x", index, value);
318 PDEBUG(D_ERR, "reg write: error %d", ret);
321 static void write_vector(struct gspca_dev *gspca_dev,
322 const __u16 data[][2])
324 struct usb_device *dev = gspca_dev->dev;
328 while (data[i][1] != 0) {
329 reg_w_val(dev, data[i][1], data[i][0]);
334 /* read 'len' bytes to gspca_dev->usb_buf */
335 static void reg_r(struct gspca_dev *gspca_dev,
336 __u16 index, __u16 length)
338 usb_control_msg(gspca_dev->dev,
339 usb_rcvctrlpipe(gspca_dev->dev, 0),
341 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
343 index, gspca_dev->usb_buf, length, 500);
346 /* write 'len' bytes from gspca_dev->usb_buf */
347 static void reg_w_buf(struct gspca_dev *gspca_dev,
348 __u16 index, __u16 len)
350 usb_control_msg(gspca_dev->dev,
351 usb_sndctrlpipe(gspca_dev->dev, 0),
353 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
355 index, gspca_dev->usb_buf, len, 500);
358 static void i2c_write(struct gspca_dev *gspca_dev, __u16 value, __u16 reg)
362 reg_w_val(gspca_dev->dev, 0x8801, reg);
363 reg_w_val(gspca_dev->dev, 0x8805, value);
364 reg_w_val(gspca_dev->dev, 0x8800, value >> 8);
366 reg_r(gspca_dev, 0x8803, 1);
367 if (!gspca_dev->usb_buf[0])
373 static int i2c_read(struct gspca_dev *gspca_dev, __u16 reg, __u8 mode)
378 reg_w_val(gspca_dev->dev, 0x8804, 0x92);
379 reg_w_val(gspca_dev->dev, 0x8801, reg);
380 reg_w_val(gspca_dev->dev, 0x8802, mode | 0x01);
382 reg_r(gspca_dev, 0x8803, 1);
383 if (!gspca_dev->usb_buf[0]) {
384 reg_r(gspca_dev, 0x8800, 1);
385 value = gspca_dev->usb_buf[0];
386 reg_r(gspca_dev, 0x8805, 1);
387 return ((int) value << 8) | gspca_dev->usb_buf[0];
394 static void sensor_mapwrite(struct gspca_dev *gspca_dev,
395 const __u16 (*sensormap)[2])
397 while ((*sensormap)[0]) {
398 gspca_dev->usb_buf[0] = (*sensormap)[1];
399 gspca_dev->usb_buf[1] = (*sensormap)[1] >> 8;
400 reg_w_buf(gspca_dev, (*sensormap)[0], 2);
405 static void write_sensor_72a(struct gspca_dev *gspca_dev,
406 const __u16 (*sensor)[2])
408 while ((*sensor)[0]) {
409 i2c_write(gspca_dev, (*sensor)[1], (*sensor)[0]);
414 static void init_161rev12A(struct gspca_dev *gspca_dev)
416 write_vector(gspca_dev, spca561_161rev12A_data1);
417 sensor_mapwrite(gspca_dev, Pb100_1map8300);
418 /*fixme: should be in sd_start*/
419 write_vector(gspca_dev, spca561_161rev12A_data2);
420 sensor_mapwrite(gspca_dev, Pb100_2map8300);
423 /* this function is called at probe time */
424 static int sd_config(struct gspca_dev *gspca_dev,
425 const struct usb_device_id *id)
427 struct sd *sd = (struct sd *) gspca_dev;
429 __u16 vendor, product;
432 /* Read frm global register the USB product and vendor IDs, just to
433 * prove that we can communicate with the device. This works, which
434 * confirms at we are communicating properly and that the device
436 reg_r(gspca_dev, 0x8104, 1);
437 data1 = gspca_dev->usb_buf[0];
438 reg_r(gspca_dev, 0x8105, 1);
439 data2 = gspca_dev->usb_buf[0];
440 vendor = (data2 << 8) | data1;
441 reg_r(gspca_dev, 0x8106, 1);
442 data1 = gspca_dev->usb_buf[0];
443 reg_r(gspca_dev, 0x8107, 1);
444 data2 = gspca_dev->usb_buf[0];
445 product = (data2 << 8) | data1;
446 if (vendor != id->idVendor || product != id->idProduct) {
447 PDEBUG(D_PROBE, "Bad vendor / product from device");
451 cam = &gspca_dev->cam;
452 gspca_dev->nbalt = 7 + 1; /* choose alternate 7 first */
454 sd->chip_revision = id->driver_info;
455 if (sd->chip_revision == Rev012A) {
456 cam->cam_mode = sif_012a_mode;
457 cam->nmodes = ARRAY_SIZE(sif_012a_mode);
459 cam->cam_mode = sif_072a_mode;
460 cam->nmodes = ARRAY_SIZE(sif_072a_mode);
462 sd->brightness = BRIGHTNESS_DEF;
463 sd->contrast = CONTRAST_DEF;
464 sd->white = WHITE_DEF;
465 sd->exposure = EXPOSURE_DEF;
466 sd->autogain = AUTOGAIN_DEF;
468 sd->expo12a = EXPO12A_DEF;
472 /* this function is called at probe and resume time */
473 static int sd_init_12a(struct gspca_dev *gspca_dev)
475 PDEBUG(D_STREAM, "Chip revision: 012a");
476 init_161rev12A(gspca_dev);
479 static int sd_init_72a(struct gspca_dev *gspca_dev)
481 PDEBUG(D_STREAM, "Chip revision: 072a");
482 write_vector(gspca_dev, rev72a_reset);
484 write_vector(gspca_dev, rev72a_init_data1);
485 write_sensor_72a(gspca_dev, rev72a_init_sensor1);
486 write_vector(gspca_dev, rev72a_init_data2);
487 write_sensor_72a(gspca_dev, rev72a_init_sensor2);
488 reg_w_val(gspca_dev->dev, 0x8112, 0x30);
493 static void setbrightness(struct gspca_dev *gspca_dev)
495 struct sd *sd = (struct sd *) gspca_dev;
496 struct usb_device *dev = gspca_dev->dev;
499 value = sd->brightness;
501 /* offsets for white balance */
502 reg_w_val(dev, 0x8611, value); /* R */
503 reg_w_val(dev, 0x8612, value); /* Gr */
504 reg_w_val(dev, 0x8613, value); /* B */
505 reg_w_val(dev, 0x8614, value); /* Gb */
508 static void setwhite(struct gspca_dev *gspca_dev)
510 struct sd *sd = (struct sd *) gspca_dev;
515 /* try to emulate MS-win as possible */
517 red = 0x20 + white * 3 / 8;
518 blue = 0x90 - white * 5 / 8;
519 if (sd->chip_revision == Rev012A) {
523 red += sd->contrast - 0x20;
524 blue += sd->contrast - 0x20;
526 reg_w_val(gspca_dev->dev, reg, red);
527 reg_w_val(gspca_dev->dev, reg + 2, blue);
530 static void setcontrast(struct gspca_dev *gspca_dev)
532 struct sd *sd = (struct sd *) gspca_dev;
533 struct usb_device *dev = gspca_dev->dev;
536 if (sd->chip_revision != Rev072A)
538 value = sd->contrast + 0x20;
540 /* gains for white balance */
542 /* reg_w_val(dev, 0x8651, value); * R - done by setwhite */
543 reg_w_val(dev, 0x8652, value); /* Gr */
544 /* reg_w_val(dev, 0x8653, value); * B - done by setwhite */
545 reg_w_val(dev, 0x8654, value); /* Gb */
549 static void setexposure(struct gspca_dev *gspca_dev)
551 struct sd *sd = (struct sd *) gspca_dev;
555 /* Register 0x8309 controls exposure for the spca561,
556 the basic exposure setting goes from 1-2047, where 1 is completely
557 dark and 2047 is very bright. It not only influences exposure but
558 also the framerate (to allow for longer exposure) from 1 - 300 it
559 only raises the exposure time then from 300 - 600 it halves the
560 framerate to be able to further raise the exposure time and for every
561 300 more it halves the framerate again. This allows for a maximum
562 exposure time of circa 0.2 - 0.25 seconds (30 / (2000/3000) fps).
563 Sometimes this is not enough, the 1-2047 uses bits 0-10, bits 11-12
564 configure a divider for the base framerate which us used at the
565 exposure setting of 1-300. These bits configure the base framerate
566 according to the following formula: fps = 60 / (value + 2) */
567 if (sd->exposure < 2048) {
571 /* Add 900 to make the 0 setting of the second part of the
572 exposure equal to the 2047 setting of the first part. */
573 expo = (sd->exposure - 2048) + 900;
576 expo |= clock_divider << 11;
577 gspca_dev->usb_buf[0] = expo;
578 gspca_dev->usb_buf[1] = expo >> 8;
579 reg_w_buf(gspca_dev, 0x8309, 2);
583 static void setgain(struct gspca_dev *gspca_dev)
585 struct sd *sd = (struct sd *) gspca_dev;
587 gspca_dev->usb_buf[0] = sd->gain;
588 gspca_dev->usb_buf[1] = 0;
589 reg_w_buf(gspca_dev, 0x8335, 2);
592 static void setautogain(struct gspca_dev *gspca_dev)
594 struct sd *sd = (struct sd *) gspca_dev;
597 sd->ag_cnt = AG_CNT_START;
602 static int sd_start_12a(struct gspca_dev *gspca_dev)
604 struct usb_device *dev = gspca_dev->dev;
606 static const __u8 Reg8391[8] =
607 {0x92, 0x30, 0x20, 0x00, 0x0c, 0x00, 0x00, 0x00};
609 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
611 /* Use compression on 320x240 and above */
612 reg_w_val(dev, 0x8500, 0x10 | mode);
614 /* I couldn't get the compression to work below 320x240
615 * Fortunately at these resolutions the bandwidth
616 * is sufficient to push raw frames at ~20fps */
617 reg_w_val(dev, 0x8500, mode);
618 } /* -- qq@kuku.eu.org */
620 gspca_dev->usb_buf[0] = 0xaa;
621 gspca_dev->usb_buf[1] = 0x00;
622 reg_w_buf(gspca_dev, 0x8307, 2);
623 /* clock - lower 0x8X values lead to fps > 30 */
624 reg_w_val(gspca_dev->dev, 0x8700, 0x8a);
625 /* 0x8f 0x85 0x27 clock */
626 reg_w_val(gspca_dev->dev, 0x8112, 0x1e | 0x20);
627 reg_w_val(gspca_dev->dev, 0x850b, 0x03);
628 memcpy(gspca_dev->usb_buf, Reg8391, 8);
629 reg_w_buf(gspca_dev, 0x8391, 8);
630 reg_w_buf(gspca_dev, 0x8390, 8);
632 setautogain(gspca_dev);
633 /* setgain(gspca_dev); */
634 setexposure(gspca_dev);
637 static int sd_start_72a(struct gspca_dev *gspca_dev)
639 struct usb_device *dev = gspca_dev->dev;
643 write_vector(gspca_dev, rev72a_reset);
645 write_vector(gspca_dev, rev72a_init_data1);
646 write_sensor_72a(gspca_dev, rev72a_init_sensor1);
648 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
652 Clck = 0x27; /* ms-win 0x87 */
664 reg_w_val(dev, 0x8700, Clck); /* 0x27 clock */
665 reg_w_val(dev, 0x8702, 0x81);
666 reg_w_val(dev, 0x8500, mode); /* mode */
667 write_sensor_72a(gspca_dev, rev72a_init_sensor2);
668 setcontrast(gspca_dev);
669 /* setbrightness(gspca_dev); * fixme: bad values */
670 setautogain(gspca_dev);
671 reg_w_val(dev, 0x8112, 0x10 | 0x20);
675 static void sd_stopN(struct gspca_dev *gspca_dev)
677 struct sd *sd = (struct sd *) gspca_dev;
679 if (sd->chip_revision == Rev012A) {
680 reg_w_val(gspca_dev->dev, 0x8112, 0x0e);
682 reg_w_val(gspca_dev->dev, 0x8112, 0x20);
683 /* reg_w_val(gspca_dev->dev, 0x8102, 0x00); ?? */
687 /* called on streamoff with alt 0 and on disconnect */
688 static void sd_stop0(struct gspca_dev *gspca_dev)
690 struct sd *sd = (struct sd *) gspca_dev;
692 if (!gspca_dev->present)
694 if (sd->chip_revision == Rev012A) {
695 reg_w_val(gspca_dev->dev, 0x8118, 0x29);
696 reg_w_val(gspca_dev->dev, 0x8114, 0x08);
698 /* reg_w_val(gspca_dev->dev, 0x8114, 0); */
701 static void do_autogain(struct gspca_dev *gspca_dev)
703 struct sd *sd = (struct sd *) gspca_dev;
709 __u8 luma_mean = 110;
710 __u8 luma_delta = 20;
715 if (--sd->ag_cnt >= 0)
717 sd->ag_cnt = AG_CNT_START;
719 switch (sd->chip_revision) {
721 reg_r(gspca_dev, 0x8621, 1);
722 Gr = gspca_dev->usb_buf[0];
723 reg_r(gspca_dev, 0x8622, 1);
724 R = gspca_dev->usb_buf[0];
725 reg_r(gspca_dev, 0x8623, 1);
726 B = gspca_dev->usb_buf[0];
727 reg_r(gspca_dev, 0x8624, 1);
728 Gb = gspca_dev->usb_buf[0];
729 y = (77 * R + 75 * (Gr + Gb) + 29 * B) >> 8;
730 /* u= (128*B-(43*(Gr+Gb+R))) >> 8; */
731 /* v= (128*R-(53*(Gr+Gb))-21*B) >> 8; */
732 /* PDEBUG(D_CONF,"reading Y %d U %d V %d ",y,u,v); */
734 if (y < luma_mean - luma_delta ||
735 y > luma_mean + luma_delta) {
736 expotimes = i2c_read(gspca_dev, 0x09, 0x10);
738 expotimes = expotimes & 0x07ff;
740 "Exposition Times 0x%03X Clock 0x%04X ",
741 expotimes,pixelclk); */
742 gainG = i2c_read(gspca_dev, 0x35, 0x10);
744 "reading Gain register %d", gainG); */
746 expotimes += (luma_mean - y) >> spring;
747 gainG += (luma_mean - y) / 50;
749 "compute expotimes %d gain %d",
756 i2c_write(gspca_dev, gainG, 0x35);
758 if (expotimes > 0x0256)
760 else if (expotimes < 3)
762 i2c_write(gspca_dev, expotimes | pixelclk, 0x09);
766 reg_r(gspca_dev, 0x8330, 2);
767 if (gspca_dev->usb_buf[1] > 0x08) {
768 gspca_dev->usb_buf[0] = ++sd->expo12a;
769 gspca_dev->usb_buf[1] = 0;
770 reg_w_buf(gspca_dev, 0x8339, 2);
771 } else if (gspca_dev->usb_buf[1] < 0x02) {
772 gspca_dev->usb_buf[0] = --sd->expo12a;
773 gspca_dev->usb_buf[1] = 0;
774 reg_w_buf(gspca_dev, 0x8339, 2);
780 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
781 struct gspca_frame *frame, /* target */
782 __u8 *data, /* isoc packet */
783 int len) /* iso packet length */
785 struct sd *sd = (struct sd *) gspca_dev;
788 switch (*data++) { /* sequence number */
789 case 0: /* start of frame */
790 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
792 if (data[1] & 0x10) {
793 /* compressed bayer */
794 gspca_frame_add(gspca_dev, FIRST_PACKET,
797 /* raw bayer (with a header, which we skip) */
798 if (sd->chip_revision == Rev012A) {
805 gspca_frame_add(gspca_dev, FIRST_PACKET,
809 case 0xff: /* drop (empty mpackets) */
812 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
816 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
818 struct sd *sd = (struct sd *) gspca_dev;
820 sd->brightness = val;
821 if (gspca_dev->streaming)
822 setbrightness(gspca_dev);
826 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
828 struct sd *sd = (struct sd *) gspca_dev;
830 *val = sd->brightness;
835 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
837 struct sd *sd = (struct sd *) gspca_dev;
840 if (gspca_dev->streaming)
841 setcontrast(gspca_dev);
845 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
847 struct sd *sd = (struct sd *) gspca_dev;
853 static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
855 struct sd *sd = (struct sd *) gspca_dev;
858 if (gspca_dev->streaming)
859 setautogain(gspca_dev);
863 static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
865 struct sd *sd = (struct sd *) gspca_dev;
871 static int sd_setwhite(struct gspca_dev *gspca_dev, __s32 val)
873 struct sd *sd = (struct sd *) gspca_dev;
876 if (gspca_dev->streaming)
881 static int sd_getwhite(struct gspca_dev *gspca_dev, __s32 *val)
883 struct sd *sd = (struct sd *) gspca_dev;
890 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
892 struct sd *sd = (struct sd *) gspca_dev;
895 if (gspca_dev->streaming)
896 setexposure(gspca_dev);
900 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
902 struct sd *sd = (struct sd *) gspca_dev;
909 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
911 struct sd *sd = (struct sd *) gspca_dev;
914 if (gspca_dev->streaming)
919 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
921 struct sd *sd = (struct sd *) gspca_dev;
928 static struct ctrl sd_ctrls_12a[] = {
931 .id = V4L2_CID_DO_WHITE_BALANCE,
932 .type = V4L2_CTRL_TYPE_INTEGER,
933 .name = "White Balance",
934 .minimum = WHITE_MIN,
935 .maximum = WHITE_MAX,
937 .default_value = WHITE_DEF,
944 .id = V4L2_CID_EXPOSURE,
945 .type = V4L2_CTRL_TYPE_INTEGER,
947 .minimum = EXPOSURE_MIN,
948 .maximum = EXPOSURE_MAX,
950 .default_value = EXPOSURE_DEF,
952 .set = sd_setexposure,
953 .get = sd_getexposure,
957 .id = V4L2_CID_AUTOGAIN,
958 .type = V4L2_CTRL_TYPE_BOOLEAN,
960 .minimum = AUTOGAIN_MIN,
961 .maximum = AUTOGAIN_MAX,
963 .default_value = AUTOGAIN_DEF,
965 .set = sd_setautogain,
966 .get = sd_getautogain,
971 .type = V4L2_CTRL_TYPE_INTEGER,
976 .default_value = GAIN_DEF,
983 static struct ctrl sd_ctrls_72a[] = {
986 .id = V4L2_CID_DO_WHITE_BALANCE,
987 .type = V4L2_CTRL_TYPE_INTEGER,
988 .name = "White Balance",
989 .minimum = WHITE_MIN,
990 .maximum = WHITE_MAX,
992 .default_value = WHITE_DEF,
999 .id = V4L2_CID_BRIGHTNESS,
1000 .type = V4L2_CTRL_TYPE_INTEGER,
1001 .name = "Brightness",
1002 .minimum = BRIGHTNESS_MIN,
1003 .maximum = BRIGHTNESS_MAX,
1005 .default_value = BRIGHTNESS_DEF,
1007 .set = sd_setbrightness,
1008 .get = sd_getbrightness,
1012 .id = V4L2_CID_CONTRAST,
1013 .type = V4L2_CTRL_TYPE_INTEGER,
1015 .minimum = CONTRAST_MIN,
1016 .maximum = CONTRAST_MAX,
1018 .default_value = CONTRAST_DEF,
1020 .set = sd_setcontrast,
1021 .get = sd_getcontrast,
1025 .id = V4L2_CID_AUTOGAIN,
1026 .type = V4L2_CTRL_TYPE_BOOLEAN,
1027 .name = "Auto Gain",
1028 .minimum = AUTOGAIN_MIN,
1029 .maximum = AUTOGAIN_MAX,
1031 .default_value = AUTOGAIN_DEF,
1033 .set = sd_setautogain,
1034 .get = sd_getautogain,
1038 /* sub-driver description */
1039 static const struct sd_desc sd_desc_12a = {
1040 .name = MODULE_NAME,
1041 .ctrls = sd_ctrls_12a,
1042 .nctrls = ARRAY_SIZE(sd_ctrls_12a),
1043 .config = sd_config,
1044 .init = sd_init_12a,
1045 .start = sd_start_12a,
1048 .pkt_scan = sd_pkt_scan,
1049 /* .dq_callback = do_autogain, * fixme */
1051 static const struct sd_desc sd_desc_72a = {
1052 .name = MODULE_NAME,
1053 .ctrls = sd_ctrls_72a,
1054 .nctrls = ARRAY_SIZE(sd_ctrls_72a),
1055 .config = sd_config,
1056 .init = sd_init_72a,
1057 .start = sd_start_72a,
1060 .pkt_scan = sd_pkt_scan,
1061 .dq_callback = do_autogain,
1063 static const struct sd_desc *sd_desc[2] = {
1068 /* -- module initialisation -- */
1069 static const __devinitdata struct usb_device_id device_table[] = {
1070 {USB_DEVICE(0x041e, 0x401a), .driver_info = Rev072A},
1071 {USB_DEVICE(0x041e, 0x403b), .driver_info = Rev012A},
1072 {USB_DEVICE(0x0458, 0x7004), .driver_info = Rev072A},
1073 {USB_DEVICE(0x046d, 0x0928), .driver_info = Rev012A},
1074 {USB_DEVICE(0x046d, 0x0929), .driver_info = Rev012A},
1075 {USB_DEVICE(0x046d, 0x092a), .driver_info = Rev012A},
1076 {USB_DEVICE(0x046d, 0x092b), .driver_info = Rev012A},
1077 {USB_DEVICE(0x046d, 0x092c), .driver_info = Rev012A},
1078 {USB_DEVICE(0x046d, 0x092d), .driver_info = Rev012A},
1079 {USB_DEVICE(0x046d, 0x092e), .driver_info = Rev012A},
1080 {USB_DEVICE(0x046d, 0x092f), .driver_info = Rev012A},
1081 {USB_DEVICE(0x04fc, 0x0561), .driver_info = Rev072A},
1082 {USB_DEVICE(0x060b, 0xa001), .driver_info = Rev072A},
1083 {USB_DEVICE(0x10fd, 0x7e50), .driver_info = Rev072A},
1084 {USB_DEVICE(0xabcd, 0xcdee), .driver_info = Rev072A},
1088 MODULE_DEVICE_TABLE(usb, device_table);
1090 /* -- device connect -- */
1091 static int sd_probe(struct usb_interface *intf,
1092 const struct usb_device_id *id)
1094 return gspca_dev_probe(intf, id,
1095 sd_desc[id->driver_info],
1100 static struct usb_driver sd_driver = {
1101 .name = MODULE_NAME,
1102 .id_table = device_table,
1104 .disconnect = gspca_disconnect,
1106 .suspend = gspca_suspend,
1107 .resume = gspca_resume,
1111 /* -- module insert / remove -- */
1112 static int __init sd_mod_init(void)
1115 ret = usb_register(&sd_driver);
1118 PDEBUG(D_PROBE, "registered");
1121 static void __exit sd_mod_exit(void)
1123 usb_deregister(&sd_driver);
1124 PDEBUG(D_PROBE, "deregistered");
1127 module_init(sd_mod_init);
1128 module_exit(sd_mod_exit);