1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Sunplus spca561 subdriver
5 * Copyright (C) 2004 Michel Xhaard mxhaard@magic.fr
7 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #define MODULE_NAME "spca561"
14 #include <linux/input.h>
17 MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
18 MODULE_DESCRIPTION("GSPCA/SPCA561 USB Camera Driver");
19 MODULE_LICENSE("GPL");
21 #define EXPOSURE_MAX (2047 + 325)
23 /* specific webcam descriptor */
25 struct gspca_dev gspca_dev
; /* !! must be the first item */
27 struct { /* hue/contrast control cluster */
28 struct v4l2_ctrl
*contrast
;
29 struct v4l2_ctrl
*hue
;
31 struct v4l2_ctrl
*autogain
;
34 __u8 expo12a
; /* expo/gain? for rev 12a */
41 #define AG_CNT_START 13
44 static const struct v4l2_pix_format sif_012a_mode
[] = {
45 {160, 120, V4L2_PIX_FMT_SGBRG8
, V4L2_FIELD_NONE
,
47 .sizeimage
= 160 * 120,
48 .colorspace
= V4L2_COLORSPACE_SRGB
,
50 {176, 144, V4L2_PIX_FMT_SGBRG8
, V4L2_FIELD_NONE
,
52 .sizeimage
= 176 * 144,
53 .colorspace
= V4L2_COLORSPACE_SRGB
,
55 {320, 240, V4L2_PIX_FMT_SPCA561
, V4L2_FIELD_NONE
,
57 .sizeimage
= 320 * 240 * 4 / 8,
58 .colorspace
= V4L2_COLORSPACE_SRGB
,
60 {352, 288, V4L2_PIX_FMT_SPCA561
, V4L2_FIELD_NONE
,
62 .sizeimage
= 352 * 288 * 4 / 8,
63 .colorspace
= V4L2_COLORSPACE_SRGB
,
67 static const struct v4l2_pix_format sif_072a_mode
[] = {
68 {160, 120, V4L2_PIX_FMT_SGBRG8
, V4L2_FIELD_NONE
,
70 .sizeimage
= 160 * 120,
71 .colorspace
= V4L2_COLORSPACE_SRGB
,
73 {176, 144, V4L2_PIX_FMT_SGBRG8
, V4L2_FIELD_NONE
,
75 .sizeimage
= 176 * 144,
76 .colorspace
= V4L2_COLORSPACE_SRGB
,
78 {320, 240, V4L2_PIX_FMT_SGBRG8
, V4L2_FIELD_NONE
,
80 .sizeimage
= 320 * 240,
81 .colorspace
= V4L2_COLORSPACE_SRGB
,
83 {352, 288, V4L2_PIX_FMT_SGBRG8
, V4L2_FIELD_NONE
,
85 .sizeimage
= 352 * 288,
86 .colorspace
= V4L2_COLORSPACE_SRGB
,
92 * I'm not very sure how to split initialization from open data
93 * chunks. For now, we'll consider everything as initialization
95 /* Frame packet header offsets for the spca561 */
96 #define SPCA561_OFFSET_SNAP 1
97 #define SPCA561_OFFSET_TYPE 2
98 #define SPCA561_OFFSET_COMPRESS 3
99 #define SPCA561_OFFSET_FRAMSEQ 4
100 #define SPCA561_OFFSET_GPIO 5
101 #define SPCA561_OFFSET_USBBUFF 6
102 #define SPCA561_OFFSET_WIN2GRAVE 7
103 #define SPCA561_OFFSET_WIN2RAVE 8
104 #define SPCA561_OFFSET_WIN2BAVE 9
105 #define SPCA561_OFFSET_WIN2GBAVE 10
106 #define SPCA561_OFFSET_WIN1GRAVE 11
107 #define SPCA561_OFFSET_WIN1RAVE 12
108 #define SPCA561_OFFSET_WIN1BAVE 13
109 #define SPCA561_OFFSET_WIN1GBAVE 14
110 #define SPCA561_OFFSET_FREQ 15
111 #define SPCA561_OFFSET_VSYNC 16
112 #define SPCA561_INDEX_I2C_BASE 0x8800
113 #define SPCA561_SNAPBIT 0x20
114 #define SPCA561_SNAPCTRL 0x40
116 static const u16 rev72a_reset
[][2] = {
117 {0x0000, 0x8114}, /* Software GPIO output data */
118 {0x0001, 0x8114}, /* Software GPIO output data */
119 {0x0000, 0x8112}, /* Some kind of reset */
122 static const __u16 rev72a_init_data1
[][2] = {
123 {0x0003, 0x8701}, /* PCLK clock delay adjustment */
124 {0x0001, 0x8703}, /* HSYNC from cmos inverted */
125 {0x0011, 0x8118}, /* Enable and conf sensor */
126 {0x0001, 0x8118}, /* Conf sensor */
127 {0x0092, 0x8804}, /* I know nothing about these */
128 {0x0010, 0x8802}, /* 0x88xx registers, so I won't */
131 static const u16 rev72a_init_sensor1
[][2] = {
146 static const __u16 rev72a_init_data2
[][2] = {
147 {0x0018, 0x8601}, /* Pixel/line selection for color separation */
148 {0x0000, 0x8602}, /* Optical black level for user setting */
149 {0x0060, 0x8604}, /* Optical black horizontal offset */
150 {0x0002, 0x8605}, /* Optical black vertical offset */
151 {0x0000, 0x8603}, /* Non-automatic optical black level */
152 {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
153 {0x0000, 0x865f}, /* Vertical valid pixels window (x2) */
154 {0x00b0, 0x865d}, /* Horizontal valid pixels window (x2) */
155 {0x0090, 0x865e}, /* Vertical valid lines window (x2) */
156 {0x00e0, 0x8406}, /* Memory buffer threshold */
157 {0x0000, 0x8660}, /* Compensation memory stuff */
158 {0x0002, 0x8201}, /* Output address for r/w serial EEPROM */
159 {0x0008, 0x8200}, /* Clear valid bit for serial EEPROM */
160 {0x0001, 0x8200}, /* OprMode to be executed by hardware */
162 {0x0000, 0x8611}, /* R offset for white balance */
163 {0x00fd, 0x8612}, /* Gr offset for white balance */
164 {0x0003, 0x8613}, /* B offset for white balance */
165 {0x0000, 0x8614}, /* Gb offset for white balance */
167 {0x0035, 0x8651}, /* R gain for white balance */
168 {0x0040, 0x8652}, /* Gr gain for white balance */
169 {0x005f, 0x8653}, /* B gain for white balance */
170 {0x0040, 0x8654}, /* Gb gain for white balance */
171 {0x0002, 0x8502}, /* Maximum average bit rate stuff */
174 {0x0087, 0x8700}, /* Set master clock (96Mhz????) */
175 {0x0081, 0x8702}, /* Master clock output enable */
177 {0x0000, 0x8500}, /* Set image type (352x288 no compression) */
178 /* Originally was 0x0010 (352x288 compression) */
180 {0x0002, 0x865b}, /* Horizontal offset for valid pixels */
181 {0x0003, 0x865c}, /* Vertical offset for valid lines */
184 static const u16 rev72a_init_sensor2
[][2] = {
187 {0x0005, 0x002f}, /* blanking control column */
188 {0x0006, 0x0000}, /* blanking mode row*/
190 {0x0009, 0x1061}, /* setexposure times && pixel clock
191 * 0001 0 | 000 0110 0001 */
196 /******************** QC Express etch2 stuff ********************/
197 static const __u16 Pb100_1map8300
[][2] = {
201 {0x8303, 0x0125}, /* image area */
204 {0x833c, 0x0001}, /*fixme: win:07*/
206 {0x832f, 0x1904}, /*fixme: was 0419*/
212 static const __u16 Pb100_2map8300
[][2] = {
219 static const __u16 spca561_161rev12A_data1
[][2] = {
220 {0x29, 0x8118}, /* Control register (various enable bits) */
221 {0x08, 0x8114}, /* GPIO: Led off */
222 {0x0e, 0x8112}, /* 0x0e stream off 0x3e stream on */
223 {0x00, 0x8102}, /* white balance - new */
225 {0x04, 0x8802}, /* windows uses 08 */
228 static const __u16 spca561_161rev12A_data2
[][2] = {
235 {0x07, 0x8201}, /* windows uses 02 */
244 {0x07, 0x8601}, /* white balance - new */
245 {0x07, 0x8602}, /* white balance - new */
246 {0x00, 0x8610}, /* *red */
247 {0x00, 0x8611}, /* 3f *green */
248 {0x00, 0x8612}, /* green *blue */
249 {0x00, 0x8613}, /* blue *green */
250 {0x43, 0x8614}, /* green *red - white balance - was 0x35 */
251 {0x40, 0x8615}, /* 40 *green - white balance - was 0x35 */
252 {0x71, 0x8616}, /* 7a *blue - white balance - was 0x35 */
253 {0x40, 0x8617}, /* 40 *green - white balance - was 0x35 */
255 {0x0c, 0x8620}, /* 0c */
256 {0xc8, 0x8631}, /* c8 */
257 {0xc8, 0x8634}, /* c8 */
258 {0x23, 0x8635}, /* 23 */
259 {0x1f, 0x8636}, /* 1f */
260 {0xdd, 0x8637}, /* dd */
261 {0xe1, 0x8638}, /* e1 */
262 {0x1d, 0x8639}, /* 1d */
263 {0x21, 0x863a}, /* 21 */
264 {0xe3, 0x863b}, /* e3 */
265 {0xdf, 0x863c}, /* df */
268 /* {0x99, 0x8700}, * - white balance - new (removed) */
269 /* HDG we used to do this in stop0, making the init state and the state
270 after a start / stop different, so do this here instead. */
275 static void reg_w_val(struct gspca_dev
*gspca_dev
, __u16 index
, __u8 value
)
278 struct usb_device
*dev
= gspca_dev
->dev
;
280 ret
= usb_control_msg(dev
, usb_sndctrlpipe(dev
, 0),
282 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
283 value
, index
, NULL
, 0, 500);
284 gspca_dbg(gspca_dev
, D_USBO
, "reg write: 0x%02x:0x%02x\n",
287 pr_err("reg write: error %d\n", ret
);
290 static void write_vector(struct gspca_dev
*gspca_dev
,
291 const __u16 data
[][2])
296 while (data
[i
][1] != 0) {
297 reg_w_val(gspca_dev
, data
[i
][1], data
[i
][0]);
302 /* read 'len' bytes to gspca_dev->usb_buf */
303 static void reg_r(struct gspca_dev
*gspca_dev
,
304 __u16 index
, __u16 length
)
306 usb_control_msg(gspca_dev
->dev
,
307 usb_rcvctrlpipe(gspca_dev
->dev
, 0),
309 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
311 index
, gspca_dev
->usb_buf
, length
, 500);
314 /* write 'len' bytes from gspca_dev->usb_buf */
315 static void reg_w_buf(struct gspca_dev
*gspca_dev
,
316 __u16 index
, __u16 len
)
318 usb_control_msg(gspca_dev
->dev
,
319 usb_sndctrlpipe(gspca_dev
->dev
, 0),
321 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
323 index
, gspca_dev
->usb_buf
, len
, 500);
326 static void i2c_write(struct gspca_dev
*gspca_dev
, __u16 value
, __u16 reg
)
330 reg_w_val(gspca_dev
, 0x8801, reg
);
331 reg_w_val(gspca_dev
, 0x8805, value
);
332 reg_w_val(gspca_dev
, 0x8800, value
>> 8);
334 reg_r(gspca_dev
, 0x8803, 1);
335 if (!gspca_dev
->usb_buf
[0])
341 static int i2c_read(struct gspca_dev
*gspca_dev
, __u16 reg
, __u8 mode
)
346 reg_w_val(gspca_dev
, 0x8804, 0x92);
347 reg_w_val(gspca_dev
, 0x8801, reg
);
348 reg_w_val(gspca_dev
, 0x8802, mode
| 0x01);
350 reg_r(gspca_dev
, 0x8803, 1);
351 if (!gspca_dev
->usb_buf
[0]) {
352 reg_r(gspca_dev
, 0x8800, 1);
353 value
= gspca_dev
->usb_buf
[0];
354 reg_r(gspca_dev
, 0x8805, 1);
355 return ((int) value
<< 8) | gspca_dev
->usb_buf
[0];
362 static void sensor_mapwrite(struct gspca_dev
*gspca_dev
,
363 const __u16 (*sensormap
)[2])
365 while ((*sensormap
)[0]) {
366 gspca_dev
->usb_buf
[0] = (*sensormap
)[1];
367 gspca_dev
->usb_buf
[1] = (*sensormap
)[1] >> 8;
368 reg_w_buf(gspca_dev
, (*sensormap
)[0], 2);
373 static void write_sensor_72a(struct gspca_dev
*gspca_dev
,
374 const __u16 (*sensor
)[2])
376 while ((*sensor
)[0]) {
377 i2c_write(gspca_dev
, (*sensor
)[1], (*sensor
)[0]);
382 static void init_161rev12A(struct gspca_dev
*gspca_dev
)
384 write_vector(gspca_dev
, spca561_161rev12A_data1
);
385 sensor_mapwrite(gspca_dev
, Pb100_1map8300
);
386 /*fixme: should be in sd_start*/
387 write_vector(gspca_dev
, spca561_161rev12A_data2
);
388 sensor_mapwrite(gspca_dev
, Pb100_2map8300
);
391 /* this function is called at probe time */
392 static int sd_config(struct gspca_dev
*gspca_dev
,
393 const struct usb_device_id
*id
)
395 struct sd
*sd
= (struct sd
*) gspca_dev
;
397 __u16 vendor
, product
;
400 /* Read frm global register the USB product and vendor IDs, just to
401 * prove that we can communicate with the device. This works, which
402 * confirms at we are communicating properly and that the device
404 reg_r(gspca_dev
, 0x8104, 1);
405 data1
= gspca_dev
->usb_buf
[0];
406 reg_r(gspca_dev
, 0x8105, 1);
407 data2
= gspca_dev
->usb_buf
[0];
408 vendor
= (data2
<< 8) | data1
;
409 reg_r(gspca_dev
, 0x8106, 1);
410 data1
= gspca_dev
->usb_buf
[0];
411 reg_r(gspca_dev
, 0x8107, 1);
412 data2
= gspca_dev
->usb_buf
[0];
413 product
= (data2
<< 8) | data1
;
414 if (vendor
!= id
->idVendor
|| product
!= id
->idProduct
) {
415 gspca_dbg(gspca_dev
, D_PROBE
, "Bad vendor / product from device\n");
419 cam
= &gspca_dev
->cam
;
420 cam
->needs_full_bandwidth
= 1;
422 sd
->chip_revision
= id
->driver_info
;
423 if (sd
->chip_revision
== Rev012A
) {
424 cam
->cam_mode
= sif_012a_mode
;
425 cam
->nmodes
= ARRAY_SIZE(sif_012a_mode
);
427 cam
->cam_mode
= sif_072a_mode
;
428 cam
->nmodes
= ARRAY_SIZE(sif_072a_mode
);
430 sd
->expo12a
= EXPO12A_DEF
;
434 /* this function is called at probe and resume time */
435 static int sd_init_12a(struct gspca_dev
*gspca_dev
)
437 gspca_dbg(gspca_dev
, D_STREAM
, "Chip revision: 012a\n");
438 init_161rev12A(gspca_dev
);
441 static int sd_init_72a(struct gspca_dev
*gspca_dev
)
443 gspca_dbg(gspca_dev
, D_STREAM
, "Chip revision: 072a\n");
444 write_vector(gspca_dev
, rev72a_reset
);
446 write_vector(gspca_dev
, rev72a_init_data1
);
447 write_sensor_72a(gspca_dev
, rev72a_init_sensor1
);
448 write_vector(gspca_dev
, rev72a_init_data2
);
449 write_sensor_72a(gspca_dev
, rev72a_init_sensor2
);
450 reg_w_val(gspca_dev
, 0x8112, 0x30);
454 static void setbrightness(struct gspca_dev
*gspca_dev
, s32 val
)
456 struct sd
*sd
= (struct sd
*) gspca_dev
;
459 if (sd
->chip_revision
== Rev012A
)
464 reg_w_val(gspca_dev
, reg
+ 0, val
); /* R */
465 reg_w_val(gspca_dev
, reg
+ 1, val
); /* Gr */
466 reg_w_val(gspca_dev
, reg
+ 2, val
); /* B */
467 reg_w_val(gspca_dev
, reg
+ 3, val
); /* Gb */
470 static void setwhite(struct gspca_dev
*gspca_dev
, s32 white
, s32 contrast
)
472 struct sd
*sd
= (struct sd
*) gspca_dev
;
476 /* try to emulate MS-win as possible */
477 red
= 0x20 + white
* 3 / 8;
478 blue
= 0x90 - white
* 5 / 8;
479 if (sd
->chip_revision
== Rev012A
) {
483 red
+= contrast
- 0x20;
484 blue
+= contrast
- 0x20;
485 reg_w_val(gspca_dev
, 0x8652, contrast
+ 0x20); /* Gr */
486 reg_w_val(gspca_dev
, 0x8654, contrast
+ 0x20); /* Gb */
488 reg_w_val(gspca_dev
, reg
, red
);
489 reg_w_val(gspca_dev
, reg
+ 2, blue
);
493 static void setexposure(struct gspca_dev
*gspca_dev
, s32 val
)
497 /* Register 0x8309 controls exposure for the spca561,
498 the basic exposure setting goes from 1-2047, where 1 is completely
499 dark and 2047 is very bright. It not only influences exposure but
500 also the framerate (to allow for longer exposure) from 1 - 300 it
501 only raises the exposure time then from 300 - 600 it halves the
502 framerate to be able to further raise the exposure time and for every
503 300 more it halves the framerate again. This allows for a maximum
504 exposure time of circa 0.2 - 0.25 seconds (30 / (2000/3000) fps).
505 Sometimes this is not enough, the 1-2047 uses bits 0-10, bits 11-12
506 configure a divider for the base framerate which us used at the
507 exposure setting of 1-300. These bits configure the base framerate
508 according to the following formula: fps = 60 / (value + 2) */
510 /* We choose to use the high bits setting the fixed framerate divisor
511 asap, as setting high basic exposure setting without the fixed
512 divider in combination with high gains makes the cam stop */
513 int table
[] = { 0, 450, 550, 625, EXPOSURE_MAX
};
515 for (i
= 0; i
< ARRAY_SIZE(table
) - 1; i
++) {
516 if (val
<= table
[i
+ 1]) {
517 expo
= val
- table
[i
];
525 gspca_dev
->usb_buf
[0] = expo
;
526 gspca_dev
->usb_buf
[1] = expo
>> 8;
527 reg_w_buf(gspca_dev
, 0x8309, 2);
531 static void setgain(struct gspca_dev
*gspca_dev
, s32 val
)
533 /* gain reg low 6 bits 0-63 gain, bit 6 and 7, both double the
534 sensitivity when set, so 31 + one of them set == 63, and 15
535 with both of them set == 63 */
537 gspca_dev
->usb_buf
[0] = val
;
539 gspca_dev
->usb_buf
[0] = (val
/ 2) | 0x40;
541 gspca_dev
->usb_buf
[0] = (val
/ 4) | 0xc0;
543 gspca_dev
->usb_buf
[1] = 0;
544 reg_w_buf(gspca_dev
, 0x8335, 2);
547 static void setautogain(struct gspca_dev
*gspca_dev
, s32 val
)
549 struct sd
*sd
= (struct sd
*) gspca_dev
;
552 sd
->ag_cnt
= AG_CNT_START
;
557 static int sd_start_12a(struct gspca_dev
*gspca_dev
)
560 static const __u8 Reg8391
[8] =
561 {0x92, 0x30, 0x20, 0x00, 0x0c, 0x00, 0x00, 0x00};
563 mode
= gspca_dev
->cam
.cam_mode
[(int) gspca_dev
->curr_mode
].priv
;
565 /* Use compression on 320x240 and above */
566 reg_w_val(gspca_dev
, 0x8500, 0x10 | mode
);
568 /* I couldn't get the compression to work below 320x240
569 * Fortunately at these resolutions the bandwidth
570 * is sufficient to push raw frames at ~20fps */
571 reg_w_val(gspca_dev
, 0x8500, mode
);
572 } /* -- qq@kuku.eu.org */
574 gspca_dev
->usb_buf
[0] = 0xaa;
575 gspca_dev
->usb_buf
[1] = 0x00;
576 reg_w_buf(gspca_dev
, 0x8307, 2);
577 /* clock - lower 0x8X values lead to fps > 30 */
578 reg_w_val(gspca_dev
, 0x8700, 0x8a);
579 /* 0x8f 0x85 0x27 clock */
580 reg_w_val(gspca_dev
, 0x8112, 0x1e | 0x20);
581 reg_w_val(gspca_dev
, 0x850b, 0x03);
582 memcpy(gspca_dev
->usb_buf
, Reg8391
, 8);
583 reg_w_buf(gspca_dev
, 0x8391, 8);
584 reg_w_buf(gspca_dev
, 0x8390, 8);
586 /* Led ON (bit 3 -> 0 */
587 reg_w_val(gspca_dev
, 0x8114, 0x00);
590 static int sd_start_72a(struct gspca_dev
*gspca_dev
)
592 struct sd
*sd
= (struct sd
*) gspca_dev
;
596 write_vector(gspca_dev
, rev72a_reset
);
598 write_vector(gspca_dev
, rev72a_init_data1
);
599 write_sensor_72a(gspca_dev
, rev72a_init_sensor1
);
601 mode
= gspca_dev
->cam
.cam_mode
[(int) gspca_dev
->curr_mode
].priv
;
605 Clck
= 0x27; /* ms-win 0x87 */
617 reg_w_val(gspca_dev
, 0x8700, Clck
); /* 0x27 clock */
618 reg_w_val(gspca_dev
, 0x8702, 0x81);
619 reg_w_val(gspca_dev
, 0x8500, mode
); /* mode */
620 write_sensor_72a(gspca_dev
, rev72a_init_sensor2
);
621 setwhite(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->hue
),
622 v4l2_ctrl_g_ctrl(sd
->contrast
));
623 /* setbrightness(gspca_dev); * fixme: bad values */
624 setautogain(gspca_dev
, v4l2_ctrl_g_ctrl(sd
->autogain
));
625 reg_w_val(gspca_dev
, 0x8112, 0x10 | 0x20);
629 static void sd_stopN(struct gspca_dev
*gspca_dev
)
631 struct sd
*sd
= (struct sd
*) gspca_dev
;
633 if (sd
->chip_revision
== Rev012A
) {
634 reg_w_val(gspca_dev
, 0x8112, 0x0e);
635 /* Led Off (bit 3 -> 1 */
636 reg_w_val(gspca_dev
, 0x8114, 0x08);
638 reg_w_val(gspca_dev
, 0x8112, 0x20);
639 /* reg_w_val(gspca_dev, 0x8102, 0x00); ?? */
643 static void do_autogain(struct gspca_dev
*gspca_dev
)
645 struct sd
*sd
= (struct sd
*) gspca_dev
;
651 __u8 luma_mean
= 110;
652 __u8 luma_delta
= 20;
657 if (--sd
->ag_cnt
>= 0)
659 sd
->ag_cnt
= AG_CNT_START
;
661 switch (sd
->chip_revision
) {
663 reg_r(gspca_dev
, 0x8621, 1);
664 Gr
= gspca_dev
->usb_buf
[0];
665 reg_r(gspca_dev
, 0x8622, 1);
666 R
= gspca_dev
->usb_buf
[0];
667 reg_r(gspca_dev
, 0x8623, 1);
668 B
= gspca_dev
->usb_buf
[0];
669 reg_r(gspca_dev
, 0x8624, 1);
670 Gb
= gspca_dev
->usb_buf
[0];
671 y
= (77 * R
+ 75 * (Gr
+ Gb
) + 29 * B
) >> 8;
672 /* u= (128*B-(43*(Gr+Gb+R))) >> 8; */
673 /* v= (128*R-(53*(Gr+Gb))-21*B) >> 8; */
675 if (y
< luma_mean
- luma_delta
||
676 y
> luma_mean
+ luma_delta
) {
677 expotimes
= i2c_read(gspca_dev
, 0x09, 0x10);
679 expotimes
= expotimes
& 0x07ff;
680 gainG
= i2c_read(gspca_dev
, 0x35, 0x10);
682 expotimes
+= (luma_mean
- y
) >> spring
;
683 gainG
+= (luma_mean
- y
) / 50;
689 i2c_write(gspca_dev
, gainG
, 0x35);
691 if (expotimes
> 0x0256)
693 else if (expotimes
< 3)
695 i2c_write(gspca_dev
, expotimes
| pixelclk
, 0x09);
701 static void sd_pkt_scan(struct gspca_dev
*gspca_dev
,
702 u8
*data
, /* isoc packet */
703 int len
) /* iso packet length */
705 struct sd
*sd
= (struct sd
*) gspca_dev
;
708 switch (*data
++) { /* sequence number */
709 case 0: /* start of frame */
710 gspca_frame_add(gspca_dev
, LAST_PACKET
, NULL
, 0);
712 /* This should never happen */
714 gspca_err(gspca_dev
, "Short SOF packet, ignoring\n\n\n\n\n");
715 gspca_dev
->last_packet_type
= DISCARD_PACKET
;
719 #if IS_ENABLED(CONFIG_INPUT)
720 if (data
[0] & 0x20) {
721 input_report_key(gspca_dev
->input_dev
, KEY_CAMERA
, 1);
722 input_sync(gspca_dev
->input_dev
);
723 input_report_key(gspca_dev
->input_dev
, KEY_CAMERA
, 0);
724 input_sync(gspca_dev
->input_dev
);
728 if (data
[1] & 0x10) {
729 /* compressed bayer */
730 gspca_frame_add(gspca_dev
, FIRST_PACKET
, data
, len
);
732 /* raw bayer (with a header, which we skip) */
733 if (sd
->chip_revision
== Rev012A
) {
740 gspca_frame_add(gspca_dev
, FIRST_PACKET
, data
, len
);
743 case 0xff: /* drop (empty mpackets) */
746 gspca_frame_add(gspca_dev
, INTER_PACKET
, data
, len
);
749 static int sd_s_ctrl(struct v4l2_ctrl
*ctrl
)
751 struct gspca_dev
*gspca_dev
=
752 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
753 struct sd
*sd
= (struct sd
*)gspca_dev
;
755 gspca_dev
->usb_err
= 0;
757 if (!gspca_dev
->streaming
)
761 case V4L2_CID_BRIGHTNESS
:
762 setbrightness(gspca_dev
, ctrl
->val
);
764 case V4L2_CID_CONTRAST
:
765 /* hue/contrast control cluster for 72a */
766 setwhite(gspca_dev
, sd
->hue
->val
, ctrl
->val
);
769 /* just plain hue control for 12a */
770 setwhite(gspca_dev
, ctrl
->val
, 0);
772 case V4L2_CID_EXPOSURE
:
773 setexposure(gspca_dev
, ctrl
->val
);
776 setgain(gspca_dev
, ctrl
->val
);
778 case V4L2_CID_AUTOGAIN
:
779 setautogain(gspca_dev
, ctrl
->val
);
782 return gspca_dev
->usb_err
;
785 static const struct v4l2_ctrl_ops sd_ctrl_ops
= {
789 static int sd_init_controls_12a(struct gspca_dev
*gspca_dev
)
791 struct v4l2_ctrl_handler
*hdl
= &gspca_dev
->ctrl_handler
;
793 gspca_dev
->vdev
.ctrl_handler
= hdl
;
794 v4l2_ctrl_handler_init(hdl
, 3);
795 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
796 V4L2_CID_HUE
, 1, 0x7f, 1, 0x40);
797 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
798 V4L2_CID_BRIGHTNESS
, -128, 127, 1, 0);
799 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
800 V4L2_CID_EXPOSURE
, 1, EXPOSURE_MAX
, 1, 700);
801 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
802 V4L2_CID_GAIN
, 0, 255, 1, 63);
805 pr_err("Could not initialize controls\n");
811 static int sd_init_controls_72a(struct gspca_dev
*gspca_dev
)
813 struct sd
*sd
= (struct sd
*)gspca_dev
;
814 struct v4l2_ctrl_handler
*hdl
= &gspca_dev
->ctrl_handler
;
816 gspca_dev
->vdev
.ctrl_handler
= hdl
;
817 v4l2_ctrl_handler_init(hdl
, 4);
818 sd
->contrast
= v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
819 V4L2_CID_CONTRAST
, 0, 0x3f, 1, 0x20);
820 sd
->hue
= v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
821 V4L2_CID_HUE
, 1, 0x7f, 1, 0x40);
822 v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
823 V4L2_CID_BRIGHTNESS
, 0, 0x3f, 1, 0x20);
824 sd
->autogain
= v4l2_ctrl_new_std(hdl
, &sd_ctrl_ops
,
825 V4L2_CID_AUTOGAIN
, 0, 1, 1, 1);
828 pr_err("Could not initialize controls\n");
831 v4l2_ctrl_cluster(2, &sd
->contrast
);
835 /* sub-driver description */
836 static const struct sd_desc sd_desc_12a
= {
838 .init_controls
= sd_init_controls_12a
,
841 .start
= sd_start_12a
,
843 .pkt_scan
= sd_pkt_scan
,
844 #if IS_ENABLED(CONFIG_INPUT)
848 static const struct sd_desc sd_desc_72a
= {
850 .init_controls
= sd_init_controls_72a
,
853 .start
= sd_start_72a
,
855 .pkt_scan
= sd_pkt_scan
,
856 .dq_callback
= do_autogain
,
857 #if IS_ENABLED(CONFIG_INPUT)
861 static const struct sd_desc
*sd_desc
[2] = {
866 /* -- module initialisation -- */
867 static const struct usb_device_id device_table
[] = {
868 {USB_DEVICE(0x041e, 0x401a), .driver_info
= Rev072A
},
869 {USB_DEVICE(0x041e, 0x403b), .driver_info
= Rev012A
},
870 {USB_DEVICE(0x0458, 0x7004), .driver_info
= Rev072A
},
871 {USB_DEVICE(0x0461, 0x0815), .driver_info
= Rev072A
},
872 {USB_DEVICE(0x046d, 0x0928), .driver_info
= Rev012A
},
873 {USB_DEVICE(0x046d, 0x0929), .driver_info
= Rev012A
},
874 {USB_DEVICE(0x046d, 0x092a), .driver_info
= Rev012A
},
875 {USB_DEVICE(0x046d, 0x092b), .driver_info
= Rev012A
},
876 {USB_DEVICE(0x046d, 0x092c), .driver_info
= Rev012A
},
877 {USB_DEVICE(0x046d, 0x092d), .driver_info
= Rev012A
},
878 {USB_DEVICE(0x046d, 0x092e), .driver_info
= Rev012A
},
879 {USB_DEVICE(0x046d, 0x092f), .driver_info
= Rev012A
},
880 {USB_DEVICE(0x04fc, 0x0561), .driver_info
= Rev072A
},
881 {USB_DEVICE(0x060b, 0xa001), .driver_info
= Rev072A
},
882 {USB_DEVICE(0x10fd, 0x7e50), .driver_info
= Rev072A
},
883 {USB_DEVICE(0xabcd, 0xcdee), .driver_info
= Rev072A
},
887 MODULE_DEVICE_TABLE(usb
, device_table
);
889 /* -- device connect -- */
890 static int sd_probe(struct usb_interface
*intf
,
891 const struct usb_device_id
*id
)
893 return gspca_dev_probe(intf
, id
,
894 sd_desc
[id
->driver_info
],
899 static struct usb_driver sd_driver
= {
901 .id_table
= device_table
,
903 .disconnect
= gspca_disconnect
,
905 .suspend
= gspca_suspend
,
906 .resume
= gspca_resume
,
907 .reset_resume
= gspca_resume
,
911 module_usb_driver(sd_driver
);