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 gspca_err(gspca_dev, "reply has error %d\n", rc);
199 return -EIO;
200 }
201 if (rc != 1) {
202 gspca_err(gspca_dev, "Bad reply size %d\n", rc);
203 return -EIO;
204 }
205 if (reply[0] != 0x08) {
206 gspca_err(gspca_dev, "Bad reply 0x%02x\n", (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 gspca_dbg(gspca_dev, D_USBO,
218 "reg_w bReq=0x0B, bReqT=0xC0, wVal=0x%04X, wInd=0x%04X\n\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 gspca_dbg(gspca_dev, D_USBO, "rc=%d, ret={0x%02x}\n", rc, (int)buff[0]);
223 if (rc < 0) {
224 gspca_err(gspca_dev, "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 gspca_err(gspca_dev, "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 gspca_err(gspca_dev, "Invalid width %u\n", w);
258 gspca_dev->usb_err = -EINVAL;
259 return;
260 }
261 gspca_dbg(gspca_dev, D_STREAM, "exposure: 0x%04X ms\n\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 gspca_dbg(gspca_dev, D_STREAM, "gain G1/G2 (0x%04X): 0x%04X (src 0x%04X)\n\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 gspca_dbg(gspca_dev, D_STREAM, "Truncating blue 0x%04X w/ value 0x%04X\n\n",
306 GAIN_MAX, normalized);
307 normalized = GAIN_MAX;
308 }
309 normalized = gainify(normalized);
310 gspca_dbg(gspca_dev, D_STREAM, "gain B (0x%04X): 0x%04X w/ source 0x%04X\n\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 gspca_dbg(gspca_dev, D_STREAM, "Truncating gain 0x%04X w/ value 0x%04X\n\n",
325 GAIN_MAX, normalized);
326 normalized = GAIN_MAX;
327 }
328 normalized = gainify(normalized);
329 gspca_dbg(gspca_dev, D_STREAM, "gain R (0x%04X): 0x%04X w / source 0x%04X\n\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 gspca_dbg(gspca_dev, D_STREAM, "configure_wh\n\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 gspca_err(gspca_dev, "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 gspca_err(gspca_dev, "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 gspca_dbg(gspca_dev, D_STREAM, "Encrypted begin, w = %u\n\n",
429 gspca_dev->pixfmt.width);
430 reg_w_buf(gspca_dev, reg_init_begin, ARRAY_SIZE(reg_init_begin));
431 configure_wh(gspca_dev);
432 reg_w_buf(gspca_dev, reg_init_end, ARRAY_SIZE(reg_init_end));
433 reg_w(gspca_dev, 0x0100, REG_GROUPED_PARAMETER_HOLD);
434 reg_w(gspca_dev, 0x0000, REG_GROUPED_PARAMETER_HOLD);
435
436 gspca_dbg(gspca_dev, D_STREAM, "Encrypted end\n\n");
437}
438
439static int configure(struct gspca_dev *gspca_dev)
440{
441 int rc;
442 char *buff = gspca_dev->usb_buf;
443
444 gspca_dbg(gspca_dev, D_STREAM, "configure()\n\n");
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459 rc = usb_control_msg(gspca_dev->dev, usb_rcvctrlpipe(gspca_dev->dev, 0),
460 0x16, 0xC0, 0x0000, 0x0000, buff, 2, 500);
461 if (val_reply(gspca_dev, buff, rc)) {
462 gspca_err(gspca_dev, "failed key req\n");
463 return -EIO;
464 }
465
466
467
468
469
470
471
472
473
474
475
476 rc = usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
477 0x01, 0x40, 0x0001, 0x000F, NULL, 0, 500);
478 if (rc < 0) {
479 gspca_err(gspca_dev, "failed to replay packet 176 w/ rc %d\n",
480 rc);
481 return rc;
482 }
483
484 rc = usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
485 0x01, 0x40, 0x0000, 0x000F, NULL, 0, 500);
486 if (rc < 0) {
487 gspca_err(gspca_dev, "failed to replay packet 178 w/ rc %d\n",
488 rc);
489 return rc;
490 }
491
492 rc = usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
493 0x01, 0x40, 0x0001, 0x000F, NULL, 0, 500);
494 if (rc < 0) {
495 gspca_err(gspca_dev, "failed to replay packet 180 w/ rc %d\n",
496 rc);
497 return rc;
498 }
499
500
501
502
503
504
505
506
507
508
509 gspca_dev->usb_err = 0;
510 configure_encrypted(gspca_dev);
511 if (gspca_dev->usb_err)
512 return gspca_dev->usb_err;
513
514
515 rc = usb_control_msg(gspca_dev->dev, usb_sndctrlpipe(gspca_dev->dev, 0),
516 0x01, 0x40, 0x0003, 0x000F, NULL, 0, 500);
517 if (rc < 0) {
518 gspca_err(gspca_dev, "failed to replay final packet w/ rc %d\n",
519 rc);
520 return rc;
521 }
522
523 gspca_dbg(gspca_dev, D_STREAM, "Configure complete\n\n");
524 return 0;
525}
526
527static int sd_config(struct gspca_dev *gspca_dev,
528 const struct usb_device_id *id)
529{
530 gspca_dev->cam.cam_mode = vga_mode;
531 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
532
533
534 gspca_dev->cam.no_urb_create = 0;
535 gspca_dev->cam.bulk_nurbs = 4;
536
537 gspca_dev->cam.bulk_size = BULK_SIZE;
538
539 gspca_dev->cam.bulk = 1;
540
541 return 0;
542}
543
544static int sd_start(struct gspca_dev *gspca_dev)
545{
546 struct sd *sd = (struct sd *) gspca_dev;
547 int rc;
548
549 sd->this_f = 0;
550
551 rc = configure(gspca_dev);
552 if (rc < 0) {
553 gspca_err(gspca_dev, "Failed configure\n");
554 return rc;
555 }
556
557
558 return 0;
559}
560
561static void sd_pkt_scan(struct gspca_dev *gspca_dev,
562 u8 *data,
563 int len)
564{
565 struct sd *sd = (struct sd *) gspca_dev;
566
567 if (len != BULK_SIZE) {
568
569 if (sd->this_f + len == gspca_dev->pixfmt.sizeimage) {
570 gspca_frame_add(gspca_dev, LAST_PACKET, data, len);
571 gspca_dbg(gspca_dev, D_FRAM, "finish frame sz %u/%u w/ len %u\n\n",
572 sd->this_f, gspca_dev->pixfmt.sizeimage, len);
573
574 } else {
575 gspca_frame_add(gspca_dev, DISCARD_PACKET, NULL, 0);
576 gspca_dbg(gspca_dev, D_FRAM, "abort frame sz %u/%u w/ len %u\n\n",
577 sd->this_f, gspca_dev->pixfmt.sizeimage, len);
578 }
579 sd->this_f = 0;
580 } else {
581 if (sd->this_f == 0)
582 gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
583 else
584 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
585 sd->this_f += len;
586 }
587}
588
589static int sd_init(struct gspca_dev *gspca_dev)
590{
591 return 0;
592}
593
594static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
595{
596 struct gspca_dev *gspca_dev =
597 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
598 struct sd *sd = (struct sd *) gspca_dev;
599
600 gspca_dev->usb_err = 0;
601
602 if (!gspca_dev->streaming)
603 return 0;
604
605 switch (ctrl->id) {
606 case V4L2_CID_EXPOSURE:
607 setexposure(gspca_dev, ctrl->val);
608 break;
609 case V4L2_CID_GAIN:
610
611 setggain(gspca_dev, gspca_dev->gain->val);
612 break;
613 case V4L2_CID_BLUE_BALANCE:
614 sd->blue->val = ctrl->val;
615 setbgain(gspca_dev, sd->blue->val, gspca_dev->gain->val);
616 break;
617 case V4L2_CID_RED_BALANCE:
618 sd->red->val = ctrl->val;
619 setrgain(gspca_dev, sd->red->val, gspca_dev->gain->val);
620 break;
621 }
622 return gspca_dev->usb_err;
623}
624
625static const struct v4l2_ctrl_ops sd_ctrl_ops = {
626 .s_ctrl = sd_s_ctrl,
627};
628
629static int sd_init_controls(struct gspca_dev *gspca_dev)
630{
631 struct sd *sd = (struct sd *) gspca_dev;
632 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
633
634 gspca_dev->vdev.ctrl_handler = hdl;
635 v4l2_ctrl_handler_init(hdl, 4);
636
637 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
638
639
640 V4L2_CID_EXPOSURE, 0, 800, 1, 350);
641 gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
642 V4L2_CID_GAIN, 0, 511, 1, 128);
643 sd->blue = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
644 V4L2_CID_BLUE_BALANCE, 0, 1023, 1, 80);
645 sd->red = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
646 V4L2_CID_RED_BALANCE, 0, 1023, 1, 295);
647
648 if (hdl->error) {
649 gspca_err(gspca_dev, "Could not initialize controls\n");
650 return hdl->error;
651 }
652 return 0;
653}
654
655
656static const struct sd_desc sd_desc = {
657 .name = MODULE_NAME,
658 .config = sd_config,
659 .init = sd_init,
660 .init_controls = sd_init_controls,
661 .start = sd_start,
662 .pkt_scan = sd_pkt_scan,
663};
664
665
666static const struct usb_device_id device_table[] = {
667
668
669
670
671
672
673
674
675
676 { USB_DEVICE(0x0547, 0x6801) },
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699 { }
700};
701MODULE_DEVICE_TABLE(usb, device_table);
702
703static int sd_probe(struct usb_interface *intf,
704 const struct usb_device_id *id)
705{
706 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
707 THIS_MODULE);
708}
709
710static struct usb_driver sd_driver = {
711 .name = MODULE_NAME,
712 .id_table = device_table,
713 .probe = sd_probe,
714 .disconnect = gspca_disconnect,
715#ifdef CONFIG_PM
716 .suspend = gspca_suspend,
717 .resume = gspca_resume,
718#endif
719};
720
721static int __init sd_mod_init(void)
722{
723 int ret;
724
725 ret = usb_register(&sd_driver);
726 if (ret < 0)
727 return ret;
728 return 0;
729}
730static void __exit sd_mod_exit(void)
731{
732 usb_deregister(&sd_driver);
733}
734
735module_init(sd_mod_init);
736module_exit(sd_mod_exit);
737