1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25#define MODULE_NAME "spca561"
26
27#include <linux/input.h>
28#include "gspca.h"
29
30MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
31MODULE_DESCRIPTION("GSPCA/SPCA561 USB Camera Driver");
32MODULE_LICENSE("GPL");
33
34#define EXPOSURE_MAX (2047 + 325)
35
36
37struct sd {
38 struct gspca_dev gspca_dev;
39
40 struct {
41 struct v4l2_ctrl *contrast;
42 struct v4l2_ctrl *hue;
43 };
44 struct v4l2_ctrl *autogain;
45
46#define EXPO12A_DEF 3
47 __u8 expo12a;
48
49 __u8 chip_revision;
50#define Rev012A 0
51#define Rev072A 1
52
53 signed char ag_cnt;
54#define AG_CNT_START 13
55};
56
57static const struct v4l2_pix_format sif_012a_mode[] = {
58 {160, 120, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
59 .bytesperline = 160,
60 .sizeimage = 160 * 120,
61 .colorspace = V4L2_COLORSPACE_SRGB,
62 .priv = 3},
63 {176, 144, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
64 .bytesperline = 176,
65 .sizeimage = 176 * 144,
66 .colorspace = V4L2_COLORSPACE_SRGB,
67 .priv = 2},
68 {320, 240, V4L2_PIX_FMT_SPCA561, V4L2_FIELD_NONE,
69 .bytesperline = 320,
70 .sizeimage = 320 * 240 * 4 / 8,
71 .colorspace = V4L2_COLORSPACE_SRGB,
72 .priv = 1},
73 {352, 288, V4L2_PIX_FMT_SPCA561, V4L2_FIELD_NONE,
74 .bytesperline = 352,
75 .sizeimage = 352 * 288 * 4 / 8,
76 .colorspace = V4L2_COLORSPACE_SRGB,
77 .priv = 0},
78};
79
80static const struct v4l2_pix_format sif_072a_mode[] = {
81 {160, 120, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
82 .bytesperline = 160,
83 .sizeimage = 160 * 120,
84 .colorspace = V4L2_COLORSPACE_SRGB,
85 .priv = 3},
86 {176, 144, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
87 .bytesperline = 176,
88 .sizeimage = 176 * 144,
89 .colorspace = V4L2_COLORSPACE_SRGB,
90 .priv = 2},
91 {320, 240, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
92 .bytesperline = 320,
93 .sizeimage = 320 * 240,
94 .colorspace = V4L2_COLORSPACE_SRGB,
95 .priv = 1},
96 {352, 288, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
97 .bytesperline = 352,
98 .sizeimage = 352 * 288,
99 .colorspace = V4L2_COLORSPACE_SRGB,
100 .priv = 0},
101};
102
103
104
105
106
107
108
109#define SPCA561_OFFSET_SNAP 1
110#define SPCA561_OFFSET_TYPE 2
111#define SPCA561_OFFSET_COMPRESS 3
112#define SPCA561_OFFSET_FRAMSEQ 4
113#define SPCA561_OFFSET_GPIO 5
114#define SPCA561_OFFSET_USBBUFF 6
115#define SPCA561_OFFSET_WIN2GRAVE 7
116#define SPCA561_OFFSET_WIN2RAVE 8
117#define SPCA561_OFFSET_WIN2BAVE 9
118#define SPCA561_OFFSET_WIN2GBAVE 10
119#define SPCA561_OFFSET_WIN1GRAVE 11
120#define SPCA561_OFFSET_WIN1RAVE 12
121#define SPCA561_OFFSET_WIN1BAVE 13
122#define SPCA561_OFFSET_WIN1GBAVE 14
123#define SPCA561_OFFSET_FREQ 15
124#define SPCA561_OFFSET_VSYNC 16
125#define SPCA561_INDEX_I2C_BASE 0x8800
126#define SPCA561_SNAPBIT 0x20
127#define SPCA561_SNAPCTRL 0x40
128
129static const u16 rev72a_reset[][2] = {
130 {0x0000, 0x8114},
131 {0x0001, 0x8114},
132 {0x0000, 0x8112},
133 {}
134};
135static const __u16 rev72a_init_data1[][2] = {
136 {0x0003, 0x8701},
137 {0x0001, 0x8703},
138 {0x0011, 0x8118},
139 {0x0001, 0x8118},
140 {0x0092, 0x8804},
141 {0x0010, 0x8802},
142 {}
143};
144static const u16 rev72a_init_sensor1[][2] = {
145 {0x0001, 0x000d},
146 {0x0002, 0x0018},
147 {0x0004, 0x0165},
148 {0x0005, 0x0021},
149 {0x0007, 0x00aa},
150 {0x0020, 0x1504},
151 {0x0039, 0x0002},
152 {0x0035, 0x0010},
153 {0x0009, 0x1049},
154 {0x0028, 0x000b},
155 {0x003b, 0x000f},
156 {0x003c, 0x0000},
157 {}
158};
159static const __u16 rev72a_init_data2[][2] = {
160 {0x0018, 0x8601},
161 {0x0000, 0x8602},
162 {0x0060, 0x8604},
163 {0x0002, 0x8605},
164 {0x0000, 0x8603},
165 {0x0002, 0x865b},
166 {0x0000, 0x865f},
167 {0x00b0, 0x865d},
168 {0x0090, 0x865e},
169 {0x00e0, 0x8406},
170 {0x0000, 0x8660},
171 {0x0002, 0x8201},
172 {0x0008, 0x8200},
173 {0x0001, 0x8200},
174
175 {0x0000, 0x8611},
176 {0x00fd, 0x8612},
177 {0x0003, 0x8613},
178 {0x0000, 0x8614},
179
180 {0x0035, 0x8651},
181 {0x0040, 0x8652},
182 {0x005f, 0x8653},
183 {0x0040, 0x8654},
184 {0x0002, 0x8502},
185 {0x0011, 0x8802},
186
187 {0x0087, 0x8700},
188 {0x0081, 0x8702},
189
190 {0x0000, 0x8500},
191
192
193 {0x0002, 0x865b},
194 {0x0003, 0x865c},
195 {}
196};
197static const u16 rev72a_init_sensor2[][2] = {
198 {0x0003, 0x0121},
199 {0x0004, 0x0165},
200 {0x0005, 0x002f},
201 {0x0006, 0x0000},
202 {0x000a, 0x0002},
203 {0x0009, 0x1061},
204
205 {0x0035, 0x0014},
206 {}
207};
208
209
210static const __u16 Pb100_1map8300[][2] = {
211
212 {0x8320, 0x3304},
213
214 {0x8303, 0x0125},
215 {0x8304, 0x0169},
216 {0x8328, 0x000b},
217 {0x833c, 0x0001},
218
219 {0x832f, 0x1904},
220 {0x8307, 0x00aa},
221 {0x8301, 0x0003},
222 {0x8302, 0x000e},
223 {}
224};
225static const __u16 Pb100_2map8300[][2] = {
226
227 {0x8339, 0x0000},
228 {0x8307, 0x00aa},
229 {}
230};
231
232static const __u16 spca561_161rev12A_data1[][2] = {
233 {0x29, 0x8118},
234 {0x08, 0x8114},
235 {0x0e, 0x8112},
236 {0x00, 0x8102},
237 {0x92, 0x8804},
238 {0x04, 0x8802},
239 {}
240};
241static const __u16 spca561_161rev12A_data2[][2] = {
242 {0x21, 0x8118},
243 {0x10, 0x8500},
244 {0x07, 0x8601},
245 {0x07, 0x8602},
246 {0x04, 0x8501},
247
248 {0x07, 0x8201},
249 {0x08, 0x8200},
250 {0x01, 0x8200},
251
252 {0x90, 0x8604},
253 {0x00, 0x8605},
254 {0xb0, 0x8603},
255
256
257 {0x07, 0x8601},
258 {0x07, 0x8602},
259 {0x00, 0x8610},
260 {0x00, 0x8611},
261 {0x00, 0x8612},
262 {0x00, 0x8613},
263 {0x43, 0x8614},
264 {0x40, 0x8615},
265 {0x71, 0x8616},
266 {0x40, 0x8617},
267
268 {0x0c, 0x8620},
269 {0xc8, 0x8631},
270 {0xc8, 0x8634},
271 {0x23, 0x8635},
272 {0x1f, 0x8636},
273 {0xdd, 0x8637},
274 {0xe1, 0x8638},
275 {0x1d, 0x8639},
276 {0x21, 0x863a},
277 {0xe3, 0x863b},
278 {0xdf, 0x863c},
279 {0xf0, 0x8505},
280 {0x32, 0x850a},
281
282
283
284 {0x29, 0x8118},
285 {}
286};
287
288static void reg_w_val(struct gspca_dev *gspca_dev, __u16 index, __u8 value)
289{
290 int ret;
291 struct usb_device *dev = gspca_dev->dev;
292
293 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
294 0,
295 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
296 value, index, NULL, 0, 500);
297 PDEBUG(D_USBO, "reg write: 0x%02x:0x%02x", index, value);
298 if (ret < 0)
299 pr_err("reg write: error %d\n", ret);
300}
301
302static void write_vector(struct gspca_dev *gspca_dev,
303 const __u16 data[][2])
304{
305 int i;
306
307 i = 0;
308 while (data[i][1] != 0) {
309 reg_w_val(gspca_dev, data[i][1], data[i][0]);
310 i++;
311 }
312}
313
314
315static void reg_r(struct gspca_dev *gspca_dev,
316 __u16 index, __u16 length)
317{
318 usb_control_msg(gspca_dev->dev,
319 usb_rcvctrlpipe(gspca_dev->dev, 0),
320 0,
321 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
322 0,
323 index, gspca_dev->usb_buf, length, 500);
324}
325
326
327static void reg_w_buf(struct gspca_dev *gspca_dev,
328 __u16 index, __u16 len)
329{
330 usb_control_msg(gspca_dev->dev,
331 usb_sndctrlpipe(gspca_dev->dev, 0),
332 0,
333 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
334 0,
335 index, gspca_dev->usb_buf, len, 500);
336}
337
338static void i2c_write(struct gspca_dev *gspca_dev, __u16 value, __u16 reg)
339{
340 int retry = 60;
341
342 reg_w_val(gspca_dev, 0x8801, reg);
343 reg_w_val(gspca_dev, 0x8805, value);
344 reg_w_val(gspca_dev, 0x8800, value >> 8);
345 do {
346 reg_r(gspca_dev, 0x8803, 1);
347 if (!gspca_dev->usb_buf[0])
348 return;
349 msleep(10);
350 } while (--retry);
351}
352
353static int i2c_read(struct gspca_dev *gspca_dev, __u16 reg, __u8 mode)
354{
355 int retry = 60;
356 __u8 value;
357
358 reg_w_val(gspca_dev, 0x8804, 0x92);
359 reg_w_val(gspca_dev, 0x8801, reg);
360 reg_w_val(gspca_dev, 0x8802, mode | 0x01);
361 do {
362 reg_r(gspca_dev, 0x8803, 1);
363 if (!gspca_dev->usb_buf[0]) {
364 reg_r(gspca_dev, 0x8800, 1);
365 value = gspca_dev->usb_buf[0];
366 reg_r(gspca_dev, 0x8805, 1);
367 return ((int) value << 8) | gspca_dev->usb_buf[0];
368 }
369 msleep(10);
370 } while (--retry);
371 return -1;
372}
373
374static void sensor_mapwrite(struct gspca_dev *gspca_dev,
375 const __u16 (*sensormap)[2])
376{
377 while ((*sensormap)[0]) {
378 gspca_dev->usb_buf[0] = (*sensormap)[1];
379 gspca_dev->usb_buf[1] = (*sensormap)[1] >> 8;
380 reg_w_buf(gspca_dev, (*sensormap)[0], 2);
381 sensormap++;
382 }
383}
384
385static void write_sensor_72a(struct gspca_dev *gspca_dev,
386 const __u16 (*sensor)[2])
387{
388 while ((*sensor)[0]) {
389 i2c_write(gspca_dev, (*sensor)[1], (*sensor)[0]);
390 sensor++;
391 }
392}
393
394static void init_161rev12A(struct gspca_dev *gspca_dev)
395{
396 write_vector(gspca_dev, spca561_161rev12A_data1);
397 sensor_mapwrite(gspca_dev, Pb100_1map8300);
398
399 write_vector(gspca_dev, spca561_161rev12A_data2);
400 sensor_mapwrite(gspca_dev, Pb100_2map8300);
401}
402
403
404static int sd_config(struct gspca_dev *gspca_dev,
405 const struct usb_device_id *id)
406{
407 struct sd *sd = (struct sd *) gspca_dev;
408 struct cam *cam;
409 __u16 vendor, product;
410 __u8 data1, data2;
411
412
413
414
415
416 reg_r(gspca_dev, 0x8104, 1);
417 data1 = gspca_dev->usb_buf[0];
418 reg_r(gspca_dev, 0x8105, 1);
419 data2 = gspca_dev->usb_buf[0];
420 vendor = (data2 << 8) | data1;
421 reg_r(gspca_dev, 0x8106, 1);
422 data1 = gspca_dev->usb_buf[0];
423 reg_r(gspca_dev, 0x8107, 1);
424 data2 = gspca_dev->usb_buf[0];
425 product = (data2 << 8) | data1;
426 if (vendor != id->idVendor || product != id->idProduct) {
427 PDEBUG(D_PROBE, "Bad vendor / product from device");
428 return -EINVAL;
429 }
430
431 cam = &gspca_dev->cam;
432 cam->needs_full_bandwidth = 1;
433
434 sd->chip_revision = id->driver_info;
435 if (sd->chip_revision == Rev012A) {
436 cam->cam_mode = sif_012a_mode;
437 cam->nmodes = ARRAY_SIZE(sif_012a_mode);
438 } else {
439 cam->cam_mode = sif_072a_mode;
440 cam->nmodes = ARRAY_SIZE(sif_072a_mode);
441 }
442 sd->expo12a = EXPO12A_DEF;
443 return 0;
444}
445
446
447static int sd_init_12a(struct gspca_dev *gspca_dev)
448{
449 PDEBUG(D_STREAM, "Chip revision: 012a");
450 init_161rev12A(gspca_dev);
451 return 0;
452}
453static int sd_init_72a(struct gspca_dev *gspca_dev)
454{
455 PDEBUG(D_STREAM, "Chip revision: 072a");
456 write_vector(gspca_dev, rev72a_reset);
457 msleep(200);
458 write_vector(gspca_dev, rev72a_init_data1);
459 write_sensor_72a(gspca_dev, rev72a_init_sensor1);
460 write_vector(gspca_dev, rev72a_init_data2);
461 write_sensor_72a(gspca_dev, rev72a_init_sensor2);
462 reg_w_val(gspca_dev, 0x8112, 0x30);
463 return 0;
464}
465
466static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
467{
468 struct sd *sd = (struct sd *) gspca_dev;
469 __u16 reg;
470
471 if (sd->chip_revision == Rev012A)
472 reg = 0x8610;
473 else
474 reg = 0x8611;
475
476 reg_w_val(gspca_dev, reg + 0, val);
477 reg_w_val(gspca_dev, reg + 1, val);
478 reg_w_val(gspca_dev, reg + 2, val);
479 reg_w_val(gspca_dev, reg + 3, val);
480}
481
482static void setwhite(struct gspca_dev *gspca_dev, s32 white, s32 contrast)
483{
484 struct sd *sd = (struct sd *) gspca_dev;
485 __u8 blue, red;
486 __u16 reg;
487
488
489 red = 0x20 + white * 3 / 8;
490 blue = 0x90 - white * 5 / 8;
491 if (sd->chip_revision == Rev012A) {
492 reg = 0x8614;
493 } else {
494 reg = 0x8651;
495 red += contrast - 0x20;
496 blue += contrast - 0x20;
497 reg_w_val(gspca_dev, 0x8652, contrast + 0x20);
498 reg_w_val(gspca_dev, 0x8654, contrast + 0x20);
499 }
500 reg_w_val(gspca_dev, reg, red);
501 reg_w_val(gspca_dev, reg + 2, blue);
502}
503
504
505static void setexposure(struct gspca_dev *gspca_dev, s32 val)
506{
507 int i, expo = 0;
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525 int table[] = { 0, 450, 550, 625, EXPOSURE_MAX };
526
527 for (i = 0; i < ARRAY_SIZE(table) - 1; i++) {
528 if (val <= table[i + 1]) {
529 expo = val - table[i];
530 if (i)
531 expo += 300;
532 expo |= i << 11;
533 break;
534 }
535 }
536
537 gspca_dev->usb_buf[0] = expo;
538 gspca_dev->usb_buf[1] = expo >> 8;
539 reg_w_buf(gspca_dev, 0x8309, 2);
540}
541
542
543static void setgain(struct gspca_dev *gspca_dev, s32 val)
544{
545
546
547
548 if (val < 64)
549 gspca_dev->usb_buf[0] = val;
550 else if (val < 128)
551 gspca_dev->usb_buf[0] = (val / 2) | 0x40;
552 else
553 gspca_dev->usb_buf[0] = (val / 4) | 0xc0;
554
555 gspca_dev->usb_buf[1] = 0;
556 reg_w_buf(gspca_dev, 0x8335, 2);
557}
558
559static void setautogain(struct gspca_dev *gspca_dev, s32 val)
560{
561 struct sd *sd = (struct sd *) gspca_dev;
562
563 if (val)
564 sd->ag_cnt = AG_CNT_START;
565 else
566 sd->ag_cnt = -1;
567}
568
569static int sd_start_12a(struct gspca_dev *gspca_dev)
570{
571 int mode;
572 static const __u8 Reg8391[8] =
573 {0x92, 0x30, 0x20, 0x00, 0x0c, 0x00, 0x00, 0x00};
574
575 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
576 if (mode <= 1) {
577
578 reg_w_val(gspca_dev, 0x8500, 0x10 | mode);
579 } else {
580
581
582
583 reg_w_val(gspca_dev, 0x8500, mode);
584 }
585
586 gspca_dev->usb_buf[0] = 0xaa;
587 gspca_dev->usb_buf[1] = 0x00;
588 reg_w_buf(gspca_dev, 0x8307, 2);
589
590 reg_w_val(gspca_dev, 0x8700, 0x8a);
591
592 reg_w_val(gspca_dev, 0x8112, 0x1e | 0x20);
593 reg_w_val(gspca_dev, 0x850b, 0x03);
594 memcpy(gspca_dev->usb_buf, Reg8391, 8);
595 reg_w_buf(gspca_dev, 0x8391, 8);
596 reg_w_buf(gspca_dev, 0x8390, 8);
597
598
599 reg_w_val(gspca_dev, 0x8114, 0x00);
600 return 0;
601}
602static int sd_start_72a(struct gspca_dev *gspca_dev)
603{
604 struct sd *sd = (struct sd *) gspca_dev;
605 int Clck;
606 int mode;
607
608 write_vector(gspca_dev, rev72a_reset);
609 msleep(200);
610 write_vector(gspca_dev, rev72a_init_data1);
611 write_sensor_72a(gspca_dev, rev72a_init_sensor1);
612
613 mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
614 switch (mode) {
615 default:
616 case 0:
617 Clck = 0x27;
618 break;
619 case 1:
620 Clck = 0x25;
621 break;
622 case 2:
623 Clck = 0x22;
624 break;
625 case 3:
626 Clck = 0x21;
627 break;
628 }
629 reg_w_val(gspca_dev, 0x8700, Clck);
630 reg_w_val(gspca_dev, 0x8702, 0x81);
631 reg_w_val(gspca_dev, 0x8500, mode);
632 write_sensor_72a(gspca_dev, rev72a_init_sensor2);
633 setwhite(gspca_dev, v4l2_ctrl_g_ctrl(sd->hue),
634 v4l2_ctrl_g_ctrl(sd->contrast));
635
636 setautogain(gspca_dev, v4l2_ctrl_g_ctrl(sd->autogain));
637 reg_w_val(gspca_dev, 0x8112, 0x10 | 0x20);
638 return 0;
639}
640
641static void sd_stopN(struct gspca_dev *gspca_dev)
642{
643 struct sd *sd = (struct sd *) gspca_dev;
644
645 if (sd->chip_revision == Rev012A) {
646 reg_w_val(gspca_dev, 0x8112, 0x0e);
647
648 reg_w_val(gspca_dev, 0x8114, 0x08);
649 } else {
650 reg_w_val(gspca_dev, 0x8112, 0x20);
651
652 }
653}
654
655static void do_autogain(struct gspca_dev *gspca_dev)
656{
657 struct sd *sd = (struct sd *) gspca_dev;
658 int expotimes;
659 int pixelclk;
660 int gainG;
661 __u8 R, Gr, Gb, B;
662 int y;
663 __u8 luma_mean = 110;
664 __u8 luma_delta = 20;
665 __u8 spring = 4;
666
667 if (sd->ag_cnt < 0)
668 return;
669 if (--sd->ag_cnt >= 0)
670 return;
671 sd->ag_cnt = AG_CNT_START;
672
673 switch (sd->chip_revision) {
674 case Rev072A:
675 reg_r(gspca_dev, 0x8621, 1);
676 Gr = gspca_dev->usb_buf[0];
677 reg_r(gspca_dev, 0x8622, 1);
678 R = gspca_dev->usb_buf[0];
679 reg_r(gspca_dev, 0x8623, 1);
680 B = gspca_dev->usb_buf[0];
681 reg_r(gspca_dev, 0x8624, 1);
682 Gb = gspca_dev->usb_buf[0];
683 y = (77 * R + 75 * (Gr + Gb) + 29 * B) >> 8;
684
685
686
687
688 if (y < luma_mean - luma_delta ||
689 y > luma_mean + luma_delta) {
690 expotimes = i2c_read(gspca_dev, 0x09, 0x10);
691 pixelclk = 0x0800;
692 expotimes = expotimes & 0x07ff;
693
694
695
696 gainG = i2c_read(gspca_dev, 0x35, 0x10);
697
698
699
700 expotimes += (luma_mean - y) >> spring;
701 gainG += (luma_mean - y) / 50;
702
703
704
705
706 if (gainG > 0x3f)
707 gainG = 0x3f;
708 else if (gainG < 3)
709 gainG = 3;
710 i2c_write(gspca_dev, gainG, 0x35);
711
712 if (expotimes > 0x0256)
713 expotimes = 0x0256;
714 else if (expotimes < 3)
715 expotimes = 3;
716 i2c_write(gspca_dev, expotimes | pixelclk, 0x09);
717 }
718 break;
719 }
720}
721
722static void sd_pkt_scan(struct gspca_dev *gspca_dev,
723 u8 *data,
724 int len)
725{
726 struct sd *sd = (struct sd *) gspca_dev;
727
728 len--;
729 switch (*data++) {
730 case 0:
731 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
732
733
734 if (len < 2) {
735 PERR("Short SOF packet, ignoring");
736 gspca_dev->last_packet_type = DISCARD_PACKET;
737 return;
738 }
739
740#if IS_ENABLED(CONFIG_INPUT)
741 if (data[0] & 0x20) {
742 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
743 input_sync(gspca_dev->input_dev);
744 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
745 input_sync(gspca_dev->input_dev);
746 }
747#endif
748
749 if (data[1] & 0x10) {
750
751 gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
752 } else {
753
754 if (sd->chip_revision == Rev012A) {
755 data += 20;
756 len -= 20;
757 } else {
758 data += 16;
759 len -= 16;
760 }
761 gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
762 }
763 return;
764 case 0xff:
765 return;
766 }
767 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
768}
769
770static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
771{
772 struct gspca_dev *gspca_dev =
773 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
774 struct sd *sd = (struct sd *)gspca_dev;
775
776 gspca_dev->usb_err = 0;
777
778 if (!gspca_dev->streaming)
779 return 0;
780
781 switch (ctrl->id) {
782 case V4L2_CID_BRIGHTNESS:
783 setbrightness(gspca_dev, ctrl->val);
784 break;
785 case V4L2_CID_CONTRAST:
786
787 setwhite(gspca_dev, sd->hue->val, ctrl->val);
788 break;
789 case V4L2_CID_HUE:
790
791 setwhite(gspca_dev, ctrl->val, 0);
792 break;
793 case V4L2_CID_EXPOSURE:
794 setexposure(gspca_dev, ctrl->val);
795 break;
796 case V4L2_CID_GAIN:
797 setgain(gspca_dev, ctrl->val);
798 break;
799 case V4L2_CID_AUTOGAIN:
800 setautogain(gspca_dev, ctrl->val);
801 break;
802 }
803 return gspca_dev->usb_err;
804}
805
806static const struct v4l2_ctrl_ops sd_ctrl_ops = {
807 .s_ctrl = sd_s_ctrl,
808};
809
810static int sd_init_controls_12a(struct gspca_dev *gspca_dev)
811{
812 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
813
814 gspca_dev->vdev.ctrl_handler = hdl;
815 v4l2_ctrl_handler_init(hdl, 3);
816 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
817 V4L2_CID_HUE, 1, 0x7f, 1, 0x40);
818 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
819 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
820 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
821 V4L2_CID_EXPOSURE, 1, EXPOSURE_MAX, 1, 700);
822 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
823 V4L2_CID_GAIN, 0, 255, 1, 63);
824
825 if (hdl->error) {
826 pr_err("Could not initialize controls\n");
827 return hdl->error;
828 }
829 return 0;
830}
831
832static int sd_init_controls_72a(struct gspca_dev *gspca_dev)
833{
834 struct sd *sd = (struct sd *)gspca_dev;
835 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
836
837 gspca_dev->vdev.ctrl_handler = hdl;
838 v4l2_ctrl_handler_init(hdl, 4);
839 sd->contrast = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
840 V4L2_CID_CONTRAST, 0, 0x3f, 1, 0x20);
841 sd->hue = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
842 V4L2_CID_HUE, 1, 0x7f, 1, 0x40);
843 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
844 V4L2_CID_BRIGHTNESS, 0, 0x3f, 1, 0x20);
845 sd->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
846 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
847
848 if (hdl->error) {
849 pr_err("Could not initialize controls\n");
850 return hdl->error;
851 }
852 v4l2_ctrl_cluster(2, &sd->contrast);
853 return 0;
854}
855
856
857static const struct sd_desc sd_desc_12a = {
858 .name = MODULE_NAME,
859 .init_controls = sd_init_controls_12a,
860 .config = sd_config,
861 .init = sd_init_12a,
862 .start = sd_start_12a,
863 .stopN = sd_stopN,
864 .pkt_scan = sd_pkt_scan,
865#if IS_ENABLED(CONFIG_INPUT)
866 .other_input = 1,
867#endif
868};
869static const struct sd_desc sd_desc_72a = {
870 .name = MODULE_NAME,
871 .init_controls = sd_init_controls_72a,
872 .config = sd_config,
873 .init = sd_init_72a,
874 .start = sd_start_72a,
875 .stopN = sd_stopN,
876 .pkt_scan = sd_pkt_scan,
877 .dq_callback = do_autogain,
878#if IS_ENABLED(CONFIG_INPUT)
879 .other_input = 1,
880#endif
881};
882static const struct sd_desc *sd_desc[2] = {
883 &sd_desc_12a,
884 &sd_desc_72a
885};
886
887
888static const struct usb_device_id device_table[] = {
889 {USB_DEVICE(0x041e, 0x401a), .driver_info = Rev072A},
890 {USB_DEVICE(0x041e, 0x403b), .driver_info = Rev012A},
891 {USB_DEVICE(0x0458, 0x7004), .driver_info = Rev072A},
892 {USB_DEVICE(0x0461, 0x0815), .driver_info = Rev072A},
893 {USB_DEVICE(0x046d, 0x0928), .driver_info = Rev012A},
894 {USB_DEVICE(0x046d, 0x0929), .driver_info = Rev012A},
895 {USB_DEVICE(0x046d, 0x092a), .driver_info = Rev012A},
896 {USB_DEVICE(0x046d, 0x092b), .driver_info = Rev012A},
897 {USB_DEVICE(0x046d, 0x092c), .driver_info = Rev012A},
898 {USB_DEVICE(0x046d, 0x092d), .driver_info = Rev012A},
899 {USB_DEVICE(0x046d, 0x092e), .driver_info = Rev012A},
900 {USB_DEVICE(0x046d, 0x092f), .driver_info = Rev012A},
901 {USB_DEVICE(0x04fc, 0x0561), .driver_info = Rev072A},
902 {USB_DEVICE(0x060b, 0xa001), .driver_info = Rev072A},
903 {USB_DEVICE(0x10fd, 0x7e50), .driver_info = Rev072A},
904 {USB_DEVICE(0xabcd, 0xcdee), .driver_info = Rev072A},
905 {}
906};
907
908MODULE_DEVICE_TABLE(usb, device_table);
909
910
911static int sd_probe(struct usb_interface *intf,
912 const struct usb_device_id *id)
913{
914 return gspca_dev_probe(intf, id,
915 sd_desc[id->driver_info],
916 sizeof(struct sd),
917 THIS_MODULE);
918}
919
920static struct usb_driver sd_driver = {
921 .name = MODULE_NAME,
922 .id_table = device_table,
923 .probe = sd_probe,
924 .disconnect = gspca_disconnect,
925#ifdef CONFIG_PM
926 .suspend = gspca_suspend,
927 .resume = gspca_resume,
928 .reset_resume = gspca_resume,
929#endif
930};
931
932module_usb_driver(sd_driver);
933