1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#define MODULE_NAME "spca561"
24
25#include "gspca.h"
26
27MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
28MODULE_DESCRIPTION("GSPCA/SPCA561 USB Camera Driver");
29MODULE_LICENSE("GPL");
30
31
32struct sd {
33 struct gspca_dev gspca_dev;
34
35 __u16 exposure;
36#define EXPOSURE_MIN 1
37#define EXPOSURE_DEF 700
38#define EXPOSURE_MAX (2047 + 325)
39
40 __u8 contrast;
41#define CONTRAST_MIN 0x00
42#define CONTRAST_DEF 0x20
43#define CONTRAST_MAX 0x3f
44
45 __u8 brightness;
46#define BRIGHTNESS_MIN 0
47#define BRIGHTNESS_DEF 0x20
48#define BRIGHTNESS_MAX 0x3f
49
50 __u8 white;
51#define HUE_MIN 1
52#define HUE_DEF 0x40
53#define HUE_MAX 0x7f
54
55 __u8 autogain;
56#define AUTOGAIN_MIN 0
57#define AUTOGAIN_DEF 1
58#define AUTOGAIN_MAX 1
59
60 __u8 gain;
61#define GAIN_MIN 0
62#define GAIN_DEF 63
63#define GAIN_MAX 255
64
65#define EXPO12A_DEF 3
66 __u8 expo12a;
67
68 __u8 chip_revision;
69#define Rev012A 0
70#define Rev072A 1
71
72 signed char ag_cnt;
73#define AG_CNT_START 13
74};
75
76static const struct v4l2_pix_format sif_012a_mode[] = {
77 {160, 120, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
78 .bytesperline = 160,
79 .sizeimage = 160 * 120,
80 .colorspace = V4L2_COLORSPACE_SRGB,
81 .priv = 3},
82 {176, 144, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
83 .bytesperline = 176,
84 .sizeimage = 176 * 144,
85 .colorspace = V4L2_COLORSPACE_SRGB,
86 .priv = 2},
87 {320, 240, V4L2_PIX_FMT_SPCA561, V4L2_FIELD_NONE,
88 .bytesperline = 320,
89 .sizeimage = 320 * 240 * 4 / 8,
90 .colorspace = V4L2_COLORSPACE_SRGB,
91 .priv = 1},
92 {352, 288, V4L2_PIX_FMT_SPCA561, V4L2_FIELD_NONE,
93 .bytesperline = 352,
94 .sizeimage = 352 * 288 * 4 / 8,
95 .colorspace = V4L2_COLORSPACE_SRGB,
96 .priv = 0},
97};
98
99static const struct v4l2_pix_format sif_072a_mode[] = {
100 {160, 120, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
101 .bytesperline = 160,
102 .sizeimage = 160 * 120,
103 .colorspace = V4L2_COLORSPACE_SRGB,
104 .priv = 3},
105 {176, 144, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
106 .bytesperline = 176,
107 .sizeimage = 176 * 144,
108 .colorspace = V4L2_COLORSPACE_SRGB,
109 .priv = 2},
110 {320, 240, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
111 .bytesperline = 320,
112 .sizeimage = 320 * 240,
113 .colorspace = V4L2_COLORSPACE_SRGB,
114 .priv = 1},
115 {352, 288, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
116 .bytesperline = 352,
117 .sizeimage = 352 * 288,
118 .colorspace = V4L2_COLORSPACE_SRGB,
119 .priv = 0},
120};
121
122
123
124
125
126
127
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
147
148static const u16 rev72a_reset[][2] = {
149 {0x0000, 0x8114},
150 {0x0001, 0x8114},
151 {0x0000, 0x8112},
152 {}
153};
154static const __u16 rev72a_init_data1[][2] = {
155 {0x0003, 0x8701},
156 {0x0001, 0x8703},
157 {0x0011, 0x8118},
158 {0x0001, 0x8118},
159 {0x0092, 0x8804},
160 {0x0010, 0x8802},
161 {}
162};
163static const u16 rev72a_init_sensor1[][2] = {
164 {0x0001, 0x000d},
165 {0x0002, 0x0018},
166 {0x0004, 0x0165},
167 {0x0005, 0x0021},
168 {0x0007, 0x00aa},
169 {0x0020, 0x1504},
170 {0x0039, 0x0002},
171 {0x0035, 0x0010},
172 {0x0009, 0x1049},
173 {0x0028, 0x000b},
174 {0x003b, 0x000f},
175 {0x003c, 0x0000},
176 {}
177};
178static const __u16 rev72a_init_data2[][2] = {
179 {0x0018, 0x8601},
180 {0x0000, 0x8602},
181 {0x0060, 0x8604},
182 {0x0002, 0x8605},
183 {0x0000, 0x8603},
184 {0x0002, 0x865b},
185 {0x0000, 0x865f},
186 {0x00b0, 0x865d},
187 {0x0090, 0x865e},
188 {0x00e0, 0x8406},
189 {0x0000, 0x8660},
190 {0x0002, 0x8201},
191 {0x0008, 0x8200},
192 {0x0001, 0x8200},
193
194 {0x0000, 0x8611},
195 {0x00fd, 0x8612},
196 {0x0003, 0x8613},
197 {0x0000, 0x8614},
198
199 {0x0035, 0x8651},
200 {0x0040, 0x8652},
201 {0x005f, 0x8653},
202 {0x0040, 0x8654},
203 {0x0002, 0x8502},
204 {0x0011, 0x8802},
205
206 {0x0087, 0x8700},
207 {0x0081, 0x8702},
208
209 {0x0000, 0x8500},
210
211
212 {0x0002, 0x865b},
213 {0x0003, 0x865c},
214 {}
215};
216static const u16 rev72a_init_sensor2[][2] = {
217 {0x0003, 0x0121},
218 {0x0004, 0x0165},
219 {0x0005, 0x002f},
220 {0x0006, 0x0000},
221 {0x000a, 0x0002},
222 {0x0009, 0x1061},
223
224 {0x0035, 0x0014},
225 {}
226};
227
228
229static const __u16 Pb100_1map8300[][2] = {
230
231 {0x8320, 0x3304},
232
233 {0x8303, 0x0125},
234 {0x8304, 0x0169},
235 {0x8328, 0x000b},
236 {0x833c, 0x0001},
237
238 {0x832f, 0x1904},
239 {0x8307, 0x00aa},
240 {0x8301, 0x0003},
241 {0x8302, 0x000e},
242 {}
243};
244static const __u16 Pb100_2map8300[][2] = {
245
246 {0x8339, 0x0000},
247 {0x8307, 0x00aa},
248 {}
249};
250
251static const __u16 spca561_161rev12A_data1[][2] = {
252 {0x29, 0x8118},
253 {0x08, 0x8114},
254 {0x0e, 0x8112},
255 {0x00, 0x8102},
256 {0x92, 0x8804},
257 {0x04, 0x8802},
258 {}
259};
260static const __u16 spca561_161rev12A_data2[][2] = {
261 {0x21, 0x8118},
262 {0x10, 0x8500},
263 {0x07, 0x8601},
264 {0x07, 0x8602},
265 {0x04, 0x8501},
266 {0x21, 0x8118},
267
268 {0x07, 0x8201},
269 {0x08, 0x8200},
270 {0x01, 0x8200},
271
272 {0x00, 0x8114},
273 {0x01, 0x8114},
274
275 {0x90, 0x8604},
276 {0x00, 0x8605},
277 {0xb0, 0x8603},
278
279
280 {0x07, 0x8601},
281 {0x07, 0x8602},
282 {0x00, 0x8610},
283 {0x00, 0x8611},
284 {0x00, 0x8612},
285 {0x00, 0x8613},
286 {0x43, 0x8614},
287 {0x40, 0x8615},
288 {0x71, 0x8616},
289 {0x40, 0x8617},
290
291 {0x0c, 0x8620},
292 {0xc8, 0x8631},
293 {0xc8, 0x8634},
294 {0x23, 0x8635},
295 {0x1f, 0x8636},
296 {0xdd, 0x8637},
297 {0xe1, 0x8638},
298 {0x1d, 0x8639},
299 {0x21, 0x863a},
300 {0xe3, 0x863b},
301 {0xdf, 0x863c},
302 {0xf0, 0x8505},
303 {0x32, 0x850a},
304
305 {}
306};
307
308static void reg_w_val(struct usb_device *dev, __u16 index, __u8 value)
309{
310 int ret;
311
312 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
313 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);
317 if (ret < 0)
318 PDEBUG(D_ERR, "reg write: error %d", ret);
319}
320
321static void write_vector(struct gspca_dev *gspca_dev,
322 const __u16 data[][2])
323{
324 struct usb_device *dev = gspca_dev->dev;
325 int i;
326
327 i = 0;
328 while (data[i][1] != 0) {
329 reg_w_val(dev, data[i][1], data[i][0]);
330 i++;
331 }
332}
333
334
335static void reg_r(struct gspca_dev *gspca_dev,
336 __u16 index, __u16 length)
337{
338 usb_control_msg(gspca_dev->dev,
339 usb_rcvctrlpipe(gspca_dev->dev, 0),
340 0,
341 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
342 0,
343 index, gspca_dev->usb_buf, length, 500);
344}
345
346
347static void reg_w_buf(struct gspca_dev *gspca_dev,
348 __u16 index, __u16 len)
349{
350 usb_control_msg(gspca_dev->dev,
351 usb_sndctrlpipe(gspca_dev->dev, 0),
352 0,
353 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
354 0,
355 index, gspca_dev->usb_buf, len, 500);
356}
357
358static void i2c_write(struct gspca_dev *gspca_dev, __u16 value, __u16 reg)
359{
360 int retry = 60;
361
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);
365 do {
366 reg_r(gspca_dev, 0x8803, 1);
367 if (!gspca_dev->usb_buf[0])
368 return;
369 msleep(10);
370 } while (--retry);
371}
372
373static int i2c_read(struct gspca_dev *gspca_dev, __u16 reg, __u8 mode)
374{
375 int retry = 60;
376 __u8 value;
377
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);
381 do {
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];
388 }
389 msleep(10);
390 } while (--retry);
391 return -1;
392}
393
394static void sensor_mapwrite(struct gspca_dev *gspca_dev,
395 const __u16 (*sensormap)[2])
396{
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);
401 sensormap++;
402 }
403}
404
405static void write_sensor_72a(struct gspca_dev *gspca_dev,
406 const __u16 (*sensor)[2])
407{
408 while ((*sensor)[0]) {
409 i2c_write(gspca_dev, (*sensor)[1], (*sensor)[0]);
410 sensor++;
411 }
412}
413
414static void init_161rev12A(struct gspca_dev *gspca_dev)
415{
416 write_vector(gspca_dev, spca561_161rev12A_data1);
417 sensor_mapwrite(gspca_dev, Pb100_1map8300);
418
419 write_vector(gspca_dev, spca561_161rev12A_data2);
420 sensor_mapwrite(gspca_dev, Pb100_2map8300);
421}
422
423
424static int sd_config(struct gspca_dev *gspca_dev,
425 const struct usb_device_id *id)
426{
427 struct sd *sd = (struct sd *) gspca_dev;
428 struct cam *cam;
429 __u16 vendor, product;
430 __u8 data1, data2;
431
432
433
434
435
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");
448 return -EINVAL;
449 }
450
451 cam = &gspca_dev->cam;
452 gspca_dev->nbalt = 7 + 1;
453
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);
458 } else {
459 cam->cam_mode = sif_072a_mode;
460 cam->nmodes = ARRAY_SIZE(sif_072a_mode);
461 }
462 sd->brightness = BRIGHTNESS_DEF;
463 sd->contrast = CONTRAST_DEF;
464 sd->white = HUE_DEF;
465 sd->exposure = EXPOSURE_DEF;
466 sd->autogain = AUTOGAIN_DEF;
467 sd->gain = GAIN_DEF;
468 sd->expo12a = EXPO12A_DEF;
469 return 0;
470}
471
472
473static int sd_init_12a(struct gspca_dev *gspca_dev)
474{
475 PDEBUG(D_STREAM, "Chip revision: 012a");
476 init_161rev12A(gspca_dev);
477 return 0;
478}
479static int sd_init_72a(struct gspca_dev *gspca_dev)
480{
481 PDEBUG(D_STREAM, "Chip revision: 072a");
482 write_vector(gspca_dev, rev72a_reset);
483 msleep(200);
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);
489 return 0;
490}
491
492
493static void setbrightness(struct gspca_dev *gspca_dev)
494{
495 struct sd *sd = (struct sd *) gspca_dev;
496 struct usb_device *dev = gspca_dev->dev;
497 __u8 value;
498
499 value = sd->brightness;
500
501
502 reg_w_val(dev, 0x8611, value);
503 reg_w_val(dev, 0x8612, value);
504 reg_w_val(dev, 0x8613, value);
505 reg_w_val(dev, 0x8614, value);
506}
507
508static void setwhite(struct gspca_dev *gspca_dev)
509{
510 struct sd *sd = (struct sd *) gspca_dev;
511 __u16 white;
512 __u8 blue, red;
513 __u16 reg;
514
515
516 white = sd->white;
517 red = 0x20 + white * 3 / 8;
518 blue = 0x90 - white * 5 / 8;
519 if (sd->chip_revision == Rev012A) {
520 reg = 0x8614;
521 } else {
522 reg = 0x8651;
523 red += sd->contrast - 0x20;
524 blue += sd->contrast - 0x20;
525 }
526 reg_w_val(gspca_dev->dev, reg, red);
527 reg_w_val(gspca_dev->dev, reg + 2, blue);
528}
529
530static void setcontrast(struct gspca_dev *gspca_dev)
531{
532 struct sd *sd = (struct sd *) gspca_dev;
533 struct usb_device *dev = gspca_dev->dev;
534 __u8 value;
535
536 if (sd->chip_revision != Rev072A)
537 return;
538 value = sd->contrast + 0x20;
539
540
541 setwhite(gspca_dev);
542
543 reg_w_val(dev, 0x8652, value);
544
545 reg_w_val(dev, 0x8654, value);
546}
547
548
549static void setexposure(struct gspca_dev *gspca_dev)
550{
551 struct sd *sd = (struct sd *) gspca_dev;
552 int i, expo = 0;
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570 int table[] = { 0, 450, 550, 625, EXPOSURE_MAX };
571
572 for (i = 0; i < ARRAY_SIZE(table) - 1; i++) {
573 if (sd->exposure <= table[i + 1]) {
574 expo = sd->exposure - table[i];
575 if (i)
576 expo += 300;
577 expo |= i << 11;
578 break;
579 }
580 }
581
582 gspca_dev->usb_buf[0] = expo;
583 gspca_dev->usb_buf[1] = expo >> 8;
584 reg_w_buf(gspca_dev, 0x8309, 2);
585}
586
587
588static void setgain(struct gspca_dev *gspca_dev)
589{
590 struct sd *sd = (struct sd *) gspca_dev;
591
592
593
594
595 if (sd->gain < 64)
596 gspca_dev->usb_buf[0] = sd->gain;
597 else if (sd->gain < 128)
598 gspca_dev->usb_buf[0] = (sd->gain / 2) | 0x40;
599 else
600 gspca_dev->usb_buf[0] = (sd->gain / 4) | 0xC0;
601
602 gspca_dev->usb_buf[1] = 0;
603 reg_w_buf(gspca_dev, 0x8335, 2);
604}
605
606static void setautogain(struct gspca_dev *gspca_dev)
607{
608 struct sd *sd = (struct sd *) gspca_dev;
609
610 if (sd->autogain)
611 sd->ag_cnt = AG_CNT_START;
612 else
613 sd->ag_cnt = -1;
614}
615
616static int sd_start_12a(struct gspca_dev *gspca_dev)
617{
618 struct usb_device *dev = gspca_dev->dev;
619 int mode;
620 static const __u8 Reg8391[8] =
621 {0x92, 0x30, 0x20, 0x00, 0x0c, 0x00, 0x00, 0x00};
622
623 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
624 if (mode <= 1) {
625
626 reg_w_val(dev, 0x8500, 0x10 | mode);
627 } else {
628
629
630
631 reg_w_val(dev, 0x8500, mode);
632 }
633
634 gspca_dev->usb_buf[0] = 0xaa;
635 gspca_dev->usb_buf[1] = 0x00;
636 reg_w_buf(gspca_dev, 0x8307, 2);
637
638 reg_w_val(gspca_dev->dev, 0x8700, 0x8a);
639
640 reg_w_val(gspca_dev->dev, 0x8112, 0x1e | 0x20);
641 reg_w_val(gspca_dev->dev, 0x850b, 0x03);
642 memcpy(gspca_dev->usb_buf, Reg8391, 8);
643 reg_w_buf(gspca_dev, 0x8391, 8);
644 reg_w_buf(gspca_dev, 0x8390, 8);
645 setwhite(gspca_dev);
646 setgain(gspca_dev);
647 setexposure(gspca_dev);
648 return 0;
649}
650static int sd_start_72a(struct gspca_dev *gspca_dev)
651{
652 struct usb_device *dev = gspca_dev->dev;
653 int Clck;
654 int mode;
655
656 write_vector(gspca_dev, rev72a_reset);
657 msleep(200);
658 write_vector(gspca_dev, rev72a_init_data1);
659 write_sensor_72a(gspca_dev, rev72a_init_sensor1);
660
661 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
662 switch (mode) {
663 default:
664 case 0:
665 Clck = 0x27;
666 break;
667 case 1:
668 Clck = 0x25;
669 break;
670 case 2:
671 Clck = 0x22;
672 break;
673 case 3:
674 Clck = 0x21;
675 break;
676 }
677 reg_w_val(dev, 0x8700, Clck);
678 reg_w_val(dev, 0x8702, 0x81);
679 reg_w_val(dev, 0x8500, mode);
680 write_sensor_72a(gspca_dev, rev72a_init_sensor2);
681 setcontrast(gspca_dev);
682
683 setautogain(gspca_dev);
684 reg_w_val(dev, 0x8112, 0x10 | 0x20);
685 return 0;
686}
687
688static void sd_stopN(struct gspca_dev *gspca_dev)
689{
690 struct sd *sd = (struct sd *) gspca_dev;
691
692 if (sd->chip_revision == Rev012A) {
693 reg_w_val(gspca_dev->dev, 0x8112, 0x0e);
694 } else {
695 reg_w_val(gspca_dev->dev, 0x8112, 0x20);
696
697 }
698}
699
700
701static void sd_stop0(struct gspca_dev *gspca_dev)
702{
703 struct sd *sd = (struct sd *) gspca_dev;
704
705 if (!gspca_dev->present)
706 return;
707 if (sd->chip_revision == Rev012A) {
708 reg_w_val(gspca_dev->dev, 0x8118, 0x29);
709 reg_w_val(gspca_dev->dev, 0x8114, 0x08);
710 }
711
712}
713
714static void do_autogain(struct gspca_dev *gspca_dev)
715{
716 struct sd *sd = (struct sd *) gspca_dev;
717 int expotimes;
718 int pixelclk;
719 int gainG;
720 __u8 R, Gr, Gb, B;
721 int y;
722 __u8 luma_mean = 110;
723 __u8 luma_delta = 20;
724 __u8 spring = 4;
725
726 if (sd->ag_cnt < 0)
727 return;
728 if (--sd->ag_cnt >= 0)
729 return;
730 sd->ag_cnt = AG_CNT_START;
731
732 switch (sd->chip_revision) {
733 case Rev072A:
734 reg_r(gspca_dev, 0x8621, 1);
735 Gr = gspca_dev->usb_buf[0];
736 reg_r(gspca_dev, 0x8622, 1);
737 R = gspca_dev->usb_buf[0];
738 reg_r(gspca_dev, 0x8623, 1);
739 B = gspca_dev->usb_buf[0];
740 reg_r(gspca_dev, 0x8624, 1);
741 Gb = gspca_dev->usb_buf[0];
742 y = (77 * R + 75 * (Gr + Gb) + 29 * B) >> 8;
743
744
745
746
747 if (y < luma_mean - luma_delta ||
748 y > luma_mean + luma_delta) {
749 expotimes = i2c_read(gspca_dev, 0x09, 0x10);
750 pixelclk = 0x0800;
751 expotimes = expotimes & 0x07ff;
752
753
754
755 gainG = i2c_read(gspca_dev, 0x35, 0x10);
756
757
758
759 expotimes += (luma_mean - y) >> spring;
760 gainG += (luma_mean - y) / 50;
761
762
763
764
765 if (gainG > 0x3f)
766 gainG = 0x3f;
767 else if (gainG < 3)
768 gainG = 3;
769 i2c_write(gspca_dev, gainG, 0x35);
770
771 if (expotimes > 0x0256)
772 expotimes = 0x0256;
773 else if (expotimes < 3)
774 expotimes = 3;
775 i2c_write(gspca_dev, expotimes | pixelclk, 0x09);
776 }
777 break;
778 }
779}
780
781static void sd_pkt_scan(struct gspca_dev *gspca_dev,
782 struct gspca_frame *frame,
783 __u8 *data,
784 int len)
785{
786 struct sd *sd = (struct sd *) gspca_dev;
787
788 len--;
789 switch (*data++) {
790 case 0:
791 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
792 data, 0);
793 if (data[1] & 0x10) {
794
795 gspca_frame_add(gspca_dev, FIRST_PACKET,
796 frame, data, len);
797 } else {
798
799 if (sd->chip_revision == Rev012A) {
800 data += 20;
801 len -= 20;
802 } else {
803 data += 16;
804 len -= 16;
805 }
806 gspca_frame_add(gspca_dev, FIRST_PACKET,
807 frame, data, len);
808 }
809 return;
810 case 0xff:
811 return;
812 }
813 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
814}
815
816
817static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
818{
819 struct sd *sd = (struct sd *) gspca_dev;
820
821 sd->brightness = val;
822 if (gspca_dev->streaming)
823 setbrightness(gspca_dev);
824 return 0;
825}
826
827static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
828{
829 struct sd *sd = (struct sd *) gspca_dev;
830
831 *val = sd->brightness;
832 return 0;
833}
834
835
836static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
837{
838 struct sd *sd = (struct sd *) gspca_dev;
839
840 sd->contrast = val;
841 if (gspca_dev->streaming)
842 setcontrast(gspca_dev);
843 return 0;
844}
845
846static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
847{
848 struct sd *sd = (struct sd *) gspca_dev;
849
850 *val = sd->contrast;
851 return 0;
852}
853
854static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
855{
856 struct sd *sd = (struct sd *) gspca_dev;
857
858 sd->autogain = val;
859 if (gspca_dev->streaming)
860 setautogain(gspca_dev);
861 return 0;
862}
863
864static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
865{
866 struct sd *sd = (struct sd *) gspca_dev;
867
868 *val = sd->autogain;
869 return 0;
870}
871
872static int sd_setwhite(struct gspca_dev *gspca_dev, __s32 val)
873{
874 struct sd *sd = (struct sd *) gspca_dev;
875
876 sd->white = val;
877 if (gspca_dev->streaming)
878 setwhite(gspca_dev);
879 return 0;
880}
881
882static int sd_getwhite(struct gspca_dev *gspca_dev, __s32 *val)
883{
884 struct sd *sd = (struct sd *) gspca_dev;
885
886 *val = sd->white;
887 return 0;
888}
889
890
891static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
892{
893 struct sd *sd = (struct sd *) gspca_dev;
894
895 sd->exposure = val;
896 if (gspca_dev->streaming)
897 setexposure(gspca_dev);
898 return 0;
899}
900
901static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
902{
903 struct sd *sd = (struct sd *) gspca_dev;
904
905 *val = sd->exposure;
906 return 0;
907}
908
909
910static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
911{
912 struct sd *sd = (struct sd *) gspca_dev;
913
914 sd->gain = val;
915 if (gspca_dev->streaming)
916 setgain(gspca_dev);
917 return 0;
918}
919
920static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
921{
922 struct sd *sd = (struct sd *) gspca_dev;
923
924 *val = sd->gain;
925 return 0;
926}
927
928
929static struct ctrl sd_ctrls_12a[] = {
930 {
931 {
932 .id = V4L2_CID_HUE,
933 .type = V4L2_CTRL_TYPE_INTEGER,
934 .name = "Hue",
935 .minimum = HUE_MIN,
936 .maximum = HUE_MAX,
937 .step = 1,
938 .default_value = HUE_DEF,
939 },
940 .set = sd_setwhite,
941 .get = sd_getwhite,
942 },
943 {
944 {
945 .id = V4L2_CID_EXPOSURE,
946 .type = V4L2_CTRL_TYPE_INTEGER,
947 .name = "Exposure",
948 .minimum = EXPOSURE_MIN,
949 .maximum = EXPOSURE_MAX,
950 .step = 1,
951 .default_value = EXPOSURE_DEF,
952 },
953 .set = sd_setexposure,
954 .get = sd_getexposure,
955 },
956 {
957 {
958 .id = V4L2_CID_GAIN,
959 .type = V4L2_CTRL_TYPE_INTEGER,
960 .name = "Gain",
961 .minimum = GAIN_MIN,
962 .maximum = GAIN_MAX,
963 .step = 1,
964 .default_value = GAIN_DEF,
965 },
966 .set = sd_setgain,
967 .get = sd_getgain,
968 },
969};
970
971static struct ctrl sd_ctrls_72a[] = {
972 {
973 {
974 .id = V4L2_CID_HUE,
975 .type = V4L2_CTRL_TYPE_INTEGER,
976 .name = "Hue",
977 .minimum = HUE_MIN,
978 .maximum = HUE_MAX,
979 .step = 1,
980 .default_value = HUE_DEF,
981 },
982 .set = sd_setwhite,
983 .get = sd_getwhite,
984 },
985 {
986 {
987 .id = V4L2_CID_BRIGHTNESS,
988 .type = V4L2_CTRL_TYPE_INTEGER,
989 .name = "Brightness",
990 .minimum = BRIGHTNESS_MIN,
991 .maximum = BRIGHTNESS_MAX,
992 .step = 1,
993 .default_value = BRIGHTNESS_DEF,
994 },
995 .set = sd_setbrightness,
996 .get = sd_getbrightness,
997 },
998 {
999 {
1000 .id = V4L2_CID_CONTRAST,
1001 .type = V4L2_CTRL_TYPE_INTEGER,
1002 .name = "Contrast",
1003 .minimum = CONTRAST_MIN,
1004 .maximum = CONTRAST_MAX,
1005 .step = 1,
1006 .default_value = CONTRAST_DEF,
1007 },
1008 .set = sd_setcontrast,
1009 .get = sd_getcontrast,
1010 },
1011 {
1012 {
1013 .id = V4L2_CID_AUTOGAIN,
1014 .type = V4L2_CTRL_TYPE_BOOLEAN,
1015 .name = "Auto Gain",
1016 .minimum = AUTOGAIN_MIN,
1017 .maximum = AUTOGAIN_MAX,
1018 .step = 1,
1019 .default_value = AUTOGAIN_DEF,
1020 },
1021 .set = sd_setautogain,
1022 .get = sd_getautogain,
1023 },
1024};
1025
1026
1027static const struct sd_desc sd_desc_12a = {
1028 .name = MODULE_NAME,
1029 .ctrls = sd_ctrls_12a,
1030 .nctrls = ARRAY_SIZE(sd_ctrls_12a),
1031 .config = sd_config,
1032 .init = sd_init_12a,
1033 .start = sd_start_12a,
1034 .stopN = sd_stopN,
1035 .stop0 = sd_stop0,
1036 .pkt_scan = sd_pkt_scan,
1037};
1038static const struct sd_desc sd_desc_72a = {
1039 .name = MODULE_NAME,
1040 .ctrls = sd_ctrls_72a,
1041 .nctrls = ARRAY_SIZE(sd_ctrls_72a),
1042 .config = sd_config,
1043 .init = sd_init_72a,
1044 .start = sd_start_72a,
1045 .stopN = sd_stopN,
1046 .stop0 = sd_stop0,
1047 .pkt_scan = sd_pkt_scan,
1048 .dq_callback = do_autogain,
1049};
1050static const struct sd_desc *sd_desc[2] = {
1051 &sd_desc_12a,
1052 &sd_desc_72a
1053};
1054
1055
1056static const __devinitdata struct usb_device_id device_table[] = {
1057 {USB_DEVICE(0x041e, 0x401a), .driver_info = Rev072A},
1058 {USB_DEVICE(0x041e, 0x403b), .driver_info = Rev012A},
1059 {USB_DEVICE(0x0458, 0x7004), .driver_info = Rev072A},
1060 {USB_DEVICE(0x046d, 0x0928), .driver_info = Rev012A},
1061 {USB_DEVICE(0x046d, 0x0929), .driver_info = Rev012A},
1062 {USB_DEVICE(0x046d, 0x092a), .driver_info = Rev012A},
1063 {USB_DEVICE(0x046d, 0x092b), .driver_info = Rev012A},
1064 {USB_DEVICE(0x046d, 0x092c), .driver_info = Rev012A},
1065 {USB_DEVICE(0x046d, 0x092d), .driver_info = Rev012A},
1066 {USB_DEVICE(0x046d, 0x092e), .driver_info = Rev012A},
1067 {USB_DEVICE(0x046d, 0x092f), .driver_info = Rev012A},
1068 {USB_DEVICE(0x04fc, 0x0561), .driver_info = Rev072A},
1069 {USB_DEVICE(0x060b, 0xa001), .driver_info = Rev072A},
1070 {USB_DEVICE(0x10fd, 0x7e50), .driver_info = Rev072A},
1071 {USB_DEVICE(0xabcd, 0xcdee), .driver_info = Rev072A},
1072 {}
1073};
1074
1075MODULE_DEVICE_TABLE(usb, device_table);
1076
1077
1078static int sd_probe(struct usb_interface *intf,
1079 const struct usb_device_id *id)
1080{
1081 return gspca_dev_probe(intf, id,
1082 sd_desc[id->driver_info],
1083 sizeof(struct sd),
1084 THIS_MODULE);
1085}
1086
1087static struct usb_driver sd_driver = {
1088 .name = MODULE_NAME,
1089 .id_table = device_table,
1090 .probe = sd_probe,
1091 .disconnect = gspca_disconnect,
1092#ifdef CONFIG_PM
1093 .suspend = gspca_suspend,
1094 .resume = gspca_resume,
1095#endif
1096};
1097
1098
1099static int __init sd_mod_init(void)
1100{
1101 int ret;
1102 ret = usb_register(&sd_driver);
1103 if (ret < 0)
1104 return ret;
1105 PDEBUG(D_PROBE, "registered");
1106 return 0;
1107}
1108static void __exit sd_mod_exit(void)
1109{
1110 usb_deregister(&sd_driver);
1111 PDEBUG(D_PROBE, "deregistered");
1112}
1113
1114module_init(sd_mod_init);
1115module_exit(sd_mod_exit);
1116