1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include "gspca.h"
23
24#define MODULE_NAME "touptek"
25
26MODULE_AUTHOR("John McMaster");
27MODULE_DESCRIPTION("ToupTek UCMOS / Amscope MU microscope camera driver");
28MODULE_LICENSE("GPL");
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105#define GAIN_MAX 511
106
107
108#define BULK_SIZE 0x4000
109
110
111#define REG_COARSE_INTEGRATION_TIME_ 0x3012
112#define REG_GROUPED_PARAMETER_HOLD_ 0x3022
113#define REG_MODE_SELECT 0x0100
114#define REG_OP_SYS_CLK_DIV 0x030A
115#define REG_VT_SYS_CLK_DIV 0x0302
116#define REG_PRE_PLL_CLK_DIV 0x0304
117#define REG_VT_PIX_CLK_DIV 0x0300
118#define REG_OP_PIX_CLK_DIV 0x0308
119#define REG_PLL_MULTIPLIER 0x0306
120#define REG_COARSE_INTEGRATION_TIME_ 0x3012
121#define REG_FRAME_LENGTH_LINES 0x0340
122#define REG_FRAME_LENGTH_LINES_ 0x300A
123#define REG_GREEN1_GAIN 0x3056
124#define REG_GREEN2_GAIN 0x305C
125#define REG_GROUPED_PARAMETER_HOLD 0x0104
126#define REG_LINE_LENGTH_PCK_ 0x300C
127#define REG_MODE_SELECT 0x0100
128#define REG_PLL_MULTIPLIER 0x0306
129#define REG_READ_MODE 0x3040
130#define REG_BLUE_GAIN 0x3058
131#define REG_RED_GAIN 0x305A
132#define REG_RESET_REGISTER 0x301A
133#define REG_SCALE_M 0x0404
134#define REG_SCALING_MODE 0x0400
135#define REG_SOFTWARE_RESET 0x0103
136#define REG_X_ADDR_END 0x0348
137#define REG_X_ADDR_START 0x0344
138#define REG_X_ADDR_START 0x0344
139#define REG_X_OUTPUT_SIZE 0x034C
140#define REG_Y_ADDR_END 0x034A
141#define REG_Y_ADDR_START 0x0346
142#define REG_Y_OUTPUT_SIZE 0x034E
143
144
145
146struct sd {
147 struct gspca_dev gspca_dev;
148
149 unsigned int this_f;
150
151
152
153
154
155
156 struct v4l2_ctrl *blue;
157 struct v4l2_ctrl *red;
158};
159
160
161struct cmd {
162 u16 value;
163 u16 index;
164};
165
166static const struct v4l2_pix_format vga_mode[] = {
167 {800, 600,
168 V4L2_PIX_FMT_SGRBG8,
169 V4L2_FIELD_NONE,
170 .bytesperline = 800,
171 .sizeimage = 800 * 600,
172 .colorspace = V4L2_COLORSPACE_SRGB},
173 {1600, 1200,
174 V4L2_PIX_FMT_SGRBG8,
175 V4L2_FIELD_NONE,
176 .bytesperline = 1600,
177 .sizeimage = 1600 * 1200,
178 .colorspace = V4L2_COLORSPACE_SRGB},
179 {3264, 2448,
180 V4L2_PIX_FMT_SGRBG8,
181 V4L2_FIELD_NONE,
182 .bytesperline = 3264,
183 .sizeimage = 3264 * 2448,
184 .colorspace = V4L2_COLORSPACE_SRGB},
185};
186
187
188
189
190
191#if MAX_NURBS < 4
192#error "Not enough URBs in the gspca table"
193#endif
194
195static int val_reply(struct gspca_dev *gspca_dev, const char *reply, int rc)
196{
197 if (rc < 0) {
198 PERR("reply has error %d", rc);
199 return -EIO;
200 }
201 if (rc != 1) {
202 PERR("Bad reply size %d", rc);
203 return -EIO;
204 }
205 if (reply[0] != 0x08) {
206 PERR("Bad reply 0x%02x", (int)reply[0]);
207 return -EIO;
208 }
209 return 0;
210}
211
212static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
213{
214 char *buff = gspca_dev->usb_buf;
215 int rc;
216
217 PDEBUG(D_USBO,
218 "reg_w bReq=0x0B, bReqT=0xC0, wVal=0x%04X, wInd=0x%04X\n",
219 value, index);
220 rc = usb_control_msg(gspca_dev->dev, usb_rcvctrlpipe(gspca_dev->dev, 0),
221 0x0B, 0xC0, value, index, buff, 1, 500);
222 PDEBUG(D_USBO, "rc=%d, ret={0x%02x}", rc, (int)buff[0]);
223 if (rc < 0) {
224 PERR("Failed reg_w(0x0B, 0xC0, 0x%04X, 0x%04X) w/ rc %d\n",
225 value, index, rc);
226 gspca_dev->usb_err = rc;
227 return;
228 }
229 if (val_reply(gspca_dev, buff, rc)) {
230 PERR("Bad reply to reg_w(0x0B, 0xC0, 0x%04X, 0x%04X\n",
231 value, index);
232 gspca_dev->usb_err = -EIO;
233 }
234}
235
236static void reg_w_buf(struct gspca_dev *gspca_dev,
237 const struct cmd *p, int l)
238{
239 do {
240 reg_w(gspca_dev, p->value, p->index);
241 p++;
242 } while (--l > 0);
243}
244
245static void setexposure(struct gspca_dev *gspca_dev, s32 val)
246{
247 u16 value;
248 unsigned int w = gspca_dev->pixfmt.width;
249
250 if (w == 800)
251 value = val * 5;
252 else if (w == 1600)
253 value = val * 3;
254 else if (w == 3264)
255 value = val * 3 / 2;
256 else {
257 PERR("Invalid width %u\n", w);
258 gspca_dev->usb_err = -EINVAL;
259 return;
260 }
261 PDEBUG(D_STREAM, "exposure: 0x%04X ms\n", value);
262
263
264 reg_w(gspca_dev, value, REG_COARSE_INTEGRATION_TIME_);
265 reg_w(gspca_dev, value, REG_COARSE_INTEGRATION_TIME_);
266}
267
268static int gainify(int in)
269{
270
271
272
273
274
275
276 if (in <= 0x7F)
277 return 0x1000 | in;
278 else if (in <= 0xFF)
279 return 0x1080 | in / 2;
280 else
281 return 0x1180 | in / 4;
282}
283
284static void setggain(struct gspca_dev *gspca_dev, u16 global_gain)
285{
286 u16 normalized;
287
288 normalized = gainify(global_gain);
289 PDEBUG(D_STREAM, "gain G1/G2 (0x%04X): 0x%04X (src 0x%04X)\n",
290 REG_GREEN1_GAIN,
291 normalized, global_gain);
292
293 reg_w(gspca_dev, normalized, REG_GREEN1_GAIN);
294 reg_w(gspca_dev, normalized, REG_GREEN2_GAIN);
295}
296
297static void setbgain(struct gspca_dev *gspca_dev,
298 u16 gain, u16 global_gain)
299{
300 u16 normalized;
301
302 normalized = global_gain +
303 ((u32)global_gain) * gain / GAIN_MAX;
304 if (normalized > GAIN_MAX) {
305 PDEBUG(D_STREAM, "Truncating blue 0x%04X w/ value 0x%04X\n",
306 GAIN_MAX, normalized);
307 normalized = GAIN_MAX;
308 }
309 normalized = gainify(normalized);
310 PDEBUG(D_STREAM, "gain B (0x%04X): 0x%04X w/ source 0x%04X\n",
311 REG_BLUE_GAIN, normalized, gain);
312
313 reg_w(gspca_dev, normalized, REG_BLUE_GAIN);
314}
315
316static void setrgain(struct gspca_dev *gspca_dev,
317 u16 gain, u16 global_gain)
318{
319 u16 normalized;
320
321 normalized = global_gain +
322 ((u32)global_gain) * gain / GAIN_MAX;
323 if (normalized > GAIN_MAX) {
324 PDEBUG(D_STREAM, "Truncating gain 0x%04X w/ value 0x%04X\n",
325 GAIN_MAX, normalized);
326 normalized = GAIN_MAX;
327 }
328 normalized = gainify(normalized);
329 PDEBUG(D_STREAM, "gain R (0x%04X): 0x%04X w / source 0x%04X\n",
330 REG_RED_GAIN, normalized, gain);
331
332 reg_w(gspca_dev, normalized, REG_RED_GAIN);
333}
334
335static void configure_wh(struct gspca_dev *gspca_dev)
336{
337 unsigned int w = gspca_dev->pixfmt.width;
338
339 PDEBUG(D_STREAM, "configure_wh\n");
340
341 if (w == 800) {
342 static const struct cmd reg_init_res[] = {
343 {0x0060, REG_X_ADDR_START},
344 {0x0CD9, REG_X_ADDR_END},
345 {0x0036, REG_Y_ADDR_START},
346 {0x098F, REG_Y_ADDR_END},
347 {0x07C7, REG_READ_MODE},
348 };
349
350 reg_w_buf(gspca_dev,
351 reg_init_res, ARRAY_SIZE(reg_init_res));
352 } else if (w == 1600) {
353 static const struct cmd reg_init_res[] = {
354 {0x009C, REG_X_ADDR_START},
355 {0x0D19, REG_X_ADDR_END},
356 {0x0068, REG_Y_ADDR_START},
357 {0x09C5, REG_Y_ADDR_END},
358 {0x06C3, REG_READ_MODE},
359 };
360
361 reg_w_buf(gspca_dev,
362 reg_init_res, ARRAY_SIZE(reg_init_res));
363 } else if (w == 3264) {
364 static const struct cmd reg_init_res[] = {
365 {0x00E8, REG_X_ADDR_START},
366 {0x0DA7, REG_X_ADDR_END},
367 {0x009E, REG_Y_ADDR_START},
368 {0x0A2D, REG_Y_ADDR_END},
369 {0x0241, REG_READ_MODE},
370 };
371
372 reg_w_buf(gspca_dev,
373 reg_init_res, ARRAY_SIZE(reg_init_res));
374 } else {
375 PERR("bad width %u\n", w);
376 gspca_dev->usb_err = -EINVAL;
377 return;
378 }
379
380 reg_w(gspca_dev, 0x0000, REG_SCALING_MODE);
381 reg_w(gspca_dev, 0x0010, REG_SCALE_M);
382 reg_w(gspca_dev, w, REG_X_OUTPUT_SIZE);
383 reg_w(gspca_dev, gspca_dev->pixfmt.height, REG_Y_OUTPUT_SIZE);
384
385 if (w == 800) {
386 reg_w(gspca_dev, 0x0384, REG_FRAME_LENGTH_LINES_);
387 reg_w(gspca_dev, 0x0960, REG_LINE_LENGTH_PCK_);
388 } else if (w == 1600) {
389 reg_w(gspca_dev, 0x0640, REG_FRAME_LENGTH_LINES_);
390 reg_w(gspca_dev, 0x0FA0, REG_LINE_LENGTH_PCK_);
391 } else if (w == 3264) {
392 reg_w(gspca_dev, 0x0B4B, REG_FRAME_LENGTH_LINES_);
393 reg_w(gspca_dev, 0x1F40, REG_LINE_LENGTH_PCK_);
394 } else {
395 PERR("bad width %u\n", w);
396 gspca_dev->usb_err = -EINVAL;
397 return;
398 }
399}
400
401
402static void configure_encrypted(struct gspca_dev *gspca_dev)
403{
404 static const struct cmd reg_init_begin[] = {
405 {0x0100, REG_SOFTWARE_RESET},
406 {0x0000, REG_MODE_SELECT},
407 {0x0100, REG_GROUPED_PARAMETER_HOLD},
408 {0x0004, REG_VT_PIX_CLK_DIV},
409 {0x0001, REG_VT_SYS_CLK_DIV},
410 {0x0008, REG_OP_PIX_CLK_DIV},
411 {0x0001, REG_OP_SYS_CLK_DIV},
412 {0x0004, REG_PRE_PLL_CLK_DIV},
413 {0x0040, REG_PLL_MULTIPLIER},
414 {0x0000, REG_GROUPED_PARAMETER_HOLD},
415 {0x0100, REG_GROUPED_PARAMETER_HOLD},
416 };
417 static const struct cmd reg_init_end[] = {
418 {0x0000, REG_GROUPED_PARAMETER_HOLD},
419 {0x0301, 0x31AE},
420 {0x0805, 0x3064},
421 {0x0071, 0x3170},
422 {0x10DE, REG_RESET_REGISTER},
423 {0x0000, REG_MODE_SELECT},
424 {0x0010, REG_PLL_MULTIPLIER},
425 {0x0100, REG_MODE_SELECT},
426 };
427
428 PDEBUG(D_STREAM, "Encrypted begin, w = %u\n", gspca_dev->pixfmt.width);
429 reg_w_buf(gspca_dev, reg_init_begin, ARRAY_SIZE(reg_init_begin));
430 configure_wh(gspca_dev);
431 reg_w_buf(gspca_dev, reg_init_end, ARRAY_SIZE(reg_init_end));
432 reg_w(gspca_dev, 0x0100, REG_GROUPED_PARAMETER_HOLD);
433 reg_w(gspca_dev, 0x0000, REG_GROUPED_PARAMETER_HOLD);
434
435 PDEBUG(D_STREAM, "Encrypted end\n");
436}
437
438static int configure(struct gspca_dev *gspca_dev)
439{
440 int rc;
441 char *buff = gspca_dev->usb_buf;
442
443 PDEBUG(D_STREAM, "configure()\n");
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458 rc = usb_control_msg(gspca_dev->dev, usb_rcvctrlpipe(gspca_dev->dev, 0),
459 0x16, 0xC0, 0x0000, 0x0000, buff, 2, 500);
460 if (val_reply(gspca_dev, buff, rc)) {
461 PERR("failed key req");
462 return -EIO;
463 }
464
465
466
467
468
469
470
471
472
473
474
475 rc = usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
476 0x01, 0x40, 0x0001, 0x000F, NULL, 0, 500);
477 if (rc < 0) {
478 PERR("failed to replay packet 176 w/ rc %d\n", rc);
479 return rc;
480 }
481
482 rc = usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
483 0x01, 0x40, 0x0000, 0x000F, NULL, 0, 500);
484 if (rc < 0) {
485 PERR("failed to replay packet 178 w/ rc %d\n", rc);
486 return rc;
487 }
488
489 rc = usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
490 0x01, 0x40, 0x0001, 0x000F, NULL, 0, 500);
491 if (rc < 0) {
492 PERR("failed to replay packet 180 w/ rc %d\n", rc);
493 return rc;
494 }
495
496
497
498
499
500
501
502
503
504
505 gspca_dev->usb_err = 0;
506 configure_encrypted(gspca_dev);
507 if (gspca_dev->usb_err)
508 return gspca_dev->usb_err;
509
510
511 rc = usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
512 0x01, 0x40, 0x0003, 0x000F, NULL, 0, 500);
513 if (rc < 0) {
514 PERR("failed to replay final packet w/ rc %d\n", rc);
515 return rc;
516 }
517
518 PDEBUG(D_STREAM, "Configure complete\n");
519 return 0;
520}
521
522static int sd_config(struct gspca_dev *gspca_dev,
523 const struct usb_device_id *id)
524{
525 gspca_dev->cam.cam_mode = vga_mode;
526 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
527
528
529 gspca_dev->cam.no_urb_create = 0;
530 gspca_dev->cam.bulk_nurbs = 4;
531
532 gspca_dev->cam.bulk_size = BULK_SIZE;
533
534 gspca_dev->cam.bulk = 1;
535
536 return 0;
537}
538
539static int sd_start(struct gspca_dev *gspca_dev)
540{
541 struct sd *sd = (struct sd *) gspca_dev;
542 int rc;
543
544 sd->this_f = 0;
545
546 rc = configure(gspca_dev);
547 if (rc < 0) {
548 PERR("Failed configure");
549 return rc;
550 }
551
552
553 return 0;
554}
555
556static void sd_pkt_scan(struct gspca_dev *gspca_dev,
557 u8 *data,
558 int len)
559{
560 struct sd *sd = (struct sd *) gspca_dev;
561
562 if (len != BULK_SIZE) {
563
564 if (sd->this_f + len == gspca_dev->pixfmt.sizeimage) {
565 gspca_frame_add(gspca_dev, LAST_PACKET, data, len);
566 PDEBUG(D_FRAM, "finish frame sz %u/%u w/ len %u\n",
567 sd->this_f, gspca_dev->pixfmt.sizeimage, len);
568
569 } else {
570 gspca_frame_add(gspca_dev, DISCARD_PACKET, NULL, 0);
571 PDEBUG(D_FRAM, "abort frame sz %u/%u w/ len %u\n",
572 sd->this_f, gspca_dev->pixfmt.sizeimage, len);
573 }
574 sd->this_f = 0;
575 } else {
576 if (sd->this_f == 0)
577 gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
578 else
579 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
580 sd->this_f += len;
581 }
582}
583
584static int sd_init(struct gspca_dev *gspca_dev)
585{
586 return 0;
587}
588
589static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
590{
591 struct gspca_dev *gspca_dev =
592 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
593 struct sd *sd = (struct sd *) gspca_dev;
594
595 gspca_dev->usb_err = 0;
596
597 if (!gspca_dev->streaming)
598 return 0;
599
600 switch (ctrl->id) {
601 case V4L2_CID_EXPOSURE:
602 setexposure(gspca_dev, ctrl->val);
603 break;
604 case V4L2_CID_GAIN:
605
606 setggain(gspca_dev, gspca_dev->gain->val);
607 break;
608 case V4L2_CID_BLUE_BALANCE:
609 sd->blue->val = ctrl->val;
610 setbgain(gspca_dev, sd->blue->val, gspca_dev->gain->val);
611 break;
612 case V4L2_CID_RED_BALANCE:
613 sd->red->val = ctrl->val;
614 setrgain(gspca_dev, sd->red->val, gspca_dev->gain->val);
615 break;
616 }
617 return gspca_dev->usb_err;
618}
619
620static const struct v4l2_ctrl_ops sd_ctrl_ops = {
621 .s_ctrl = sd_s_ctrl,
622};
623
624static int sd_init_controls(struct gspca_dev *gspca_dev)
625{
626 struct sd *sd = (struct sd *) gspca_dev;
627 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
628
629 gspca_dev->vdev.ctrl_handler = hdl;
630 v4l2_ctrl_handler_init(hdl, 4);
631
632 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
633
634
635 V4L2_CID_EXPOSURE, 0, 800, 1, 350);
636 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
637 V4L2_CID_GAIN, 0, 511, 1, 128);
638 sd->blue = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
639 V4L2_CID_BLUE_BALANCE, 0, 1023, 1, 80);
640 sd->red = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
641 V4L2_CID_RED_BALANCE, 0, 1023, 1, 295);
642
643 if (hdl->error) {
644 PERR("Could not initialize controls\n");
645 return hdl->error;
646 }
647 return 0;
648}
649
650
651static const struct sd_desc sd_desc = {
652 .name = MODULE_NAME,
653 .config = sd_config,
654 .init = sd_init,
655 .init_controls = sd_init_controls,
656 .start = sd_start,
657 .pkt_scan = sd_pkt_scan,
658};
659
660
661static const struct usb_device_id device_table[] = {
662
663
664
665
666
667
668
669
670
671 { USB_DEVICE(0x0547, 0x6801) },
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694 { }
695};
696MODULE_DEVICE_TABLE(usb, device_table);
697
698static int sd_probe(struct usb_interface *intf,
699 const struct usb_device_id *id)
700{
701 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
702 THIS_MODULE);
703}
704
705static struct usb_driver sd_driver = {
706 .name = MODULE_NAME,
707 .id_table = device_table,
708 .probe = sd_probe,
709 .disconnect = gspca_disconnect,
710#ifdef CONFIG_PM
711 .suspend = gspca_suspend,
712 .resume = gspca_resume,
713#endif
714};
715
716static int __init sd_mod_init(void)
717{
718 int ret;
719
720 ret = usb_register(&sd_driver);
721 if (ret < 0)
722 return ret;
723 return 0;
724}
725static void __exit sd_mod_exit(void)
726{
727 usb_deregister(&sd_driver);
728}
729
730module_init(sd_mod_init);
731module_exit(sd_mod_exit);
732