Linux 4.19.133
[linux/fpc-iii.git] / drivers / media / usb / gspca / touptek.c
blobd1b9032d7863f9841ef8b4ee70f8d2c3f88328dd
1 /*
2 * ToupTek UCMOS / AmScope MU series camera driver
3 * TODO: contrast with ScopeTek / AmScope MDC cameras
5 * Copyright (C) 2012-2014 John McMaster <JohnDMcMaster@gmail.com>
7 * Special thanks to Bushing for helping with the decrypt algorithm and
8 * Sean O'Sullivan / the Rensselaer Center for Open Source
9 * Software (RCOS) for helping me learn kernel development
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
22 #include "gspca.h"
24 #define MODULE_NAME "touptek"
26 MODULE_AUTHOR("John McMaster");
27 MODULE_DESCRIPTION("ToupTek UCMOS / Amscope MU microscope camera driver");
28 MODULE_LICENSE("GPL");
31 * Exposure reg is linear with exposure time
32 * Exposure (sec), E (reg)
33 * 0.000400, 0x0002
34 * 0.001000, 0x0005
35 * 0.005000, 0x0019
36 * 0.020000, 0x0064
37 * 0.080000, 0x0190
38 * 0.400000, 0x07D0
39 * 1.000000, 0x1388
40 * 2.000000, 0x2710
42 * Three gain stages
43 * 0x1000: master channel enable bit
44 * 0x007F: low gain bits
45 * 0x0080: medium gain bit
46 * 0x0100: high gain bit
47 * gain = enable * (1 + regH) * (1 + regM) * z * regL
49 * Gain implementation
50 * Want to do something similar to mt9v011.c's set_balance
52 * Gain does not vary with resolution (checked 640x480 vs 1600x1200)
54 * Constant derivation:
56 * Raw data:
57 * Gain, GTOP, B, R, GBOT
58 * 1.00, 0x105C, 0x1068, 0x10C8, 0x105C
59 * 1.20, 0x106E, 0x107E, 0x10D6, 0x106E
60 * 1.40, 0x10C0, 0x10CA, 0x10E5, 0x10C0
61 * 1.60, 0x10C9, 0x10D4, 0x10F3, 0x10C9
62 * 1.80, 0x10D2, 0x10DE, 0x11C1, 0x10D2
63 * 2.00, 0x10DC, 0x10E9, 0x11C8, 0x10DC
64 * 2.20, 0x10E5, 0x10F3, 0x11CF, 0x10E5
65 * 2.40, 0x10EE, 0x10FE, 0x11D7, 0x10EE
66 * 2.60, 0x10F7, 0x11C4, 0x11DE, 0x10F7
67 * 2.80, 0x11C0, 0x11CA, 0x11E5, 0x11C0
68 * 3.00, 0x11C5, 0x11CF, 0x11ED, 0x11C5
70 * zR = 0.0069605943152454778
71 * about 3/431 = 0.0069605568445475635
72 * zB = 0.0095695970695970703
73 * about 6/627 = 0.0095693779904306216
74 * zG = 0.010889328063241107
75 * about 6/551 = 0.010889292196007259
76 * about 10 bits for constant + 7 bits for value => at least 17 bit
77 * intermediate with 32 bit ints should be fine for overflow etc
78 * Essentially gains are in range 0-0x001FF
80 * However, V4L expects a main gain channel + R and B balance
81 * To keep things simple for now saturate the values of balance is too high/low
82 * This isn't really ideal but easy way to fit the Linux model
84 * Converted using gain model turns out to be quite linear:
85 * Gain, GTOP, B, R, GBOT
86 * 1.00, 92, 104, 144, 92
87 * 1.20, 110, 126, 172, 110
88 * 1.40, 128, 148, 202, 128
89 * 1.60, 146, 168, 230, 146
90 * 1.80, 164, 188, 260, 164
91 * 2.00, 184, 210, 288, 184
92 * 2.20, 202, 230, 316, 202
93 * 2.40, 220, 252, 348, 220
94 * 2.60, 238, 272, 376, 238
95 * 2.80, 256, 296, 404, 256
96 * 3.00, 276, 316, 436, 276
98 * Maximum gain is 0x7FF * 2 * 2 => 0x1FFC (8188)
99 * or about 13 effective bits of gain
100 * The highest the commercial driver goes in my setup 436
101 * However, because could *maybe* damage circuits
102 * limit the gain until have a reason to go higher
103 * Solution: gain clipped and warning emitted
105 #define GAIN_MAX 511
107 /* Frame sync is a short read */
108 #define BULK_SIZE 0x4000
110 /* MT9E001 reg names to give a rough approximation */
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
145 /* specific webcam descriptor */
146 struct sd {
147 struct gspca_dev gspca_dev; /* !! must be the first item */
148 /* How many bytes this frame */
149 unsigned int this_f;
152 Device has separate gains for each Bayer quadrant
153 V4L supports master gain which is referenced to G1/G2 and supplies
154 individual balance controls for R/B
156 struct v4l2_ctrl *blue;
157 struct v4l2_ctrl *red;
160 /* Used to simplify reg write error handling */
161 struct cmd {
162 u16 value;
163 u16 index;
166 static 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},
188 * As theres no known frame sync, the only way to keep synced is to try hard
189 * to never miss any packets
191 #if MAX_NURBS < 4
192 #error "Not enough URBs in the gspca table"
193 #endif
195 static int val_reply(struct gspca_dev *gspca_dev, const char *reply, int rc)
197 if (rc < 0) {
198 gspca_err(gspca_dev, "reply has error %d\n", rc);
199 return -EIO;
201 if (rc != 1) {
202 gspca_err(gspca_dev, "Bad reply size %d\n", rc);
203 return -EIO;
205 if (reply[0] != 0x08) {
206 gspca_err(gspca_dev, "Bad reply 0x%02x\n", (int)reply[0]);
207 return -EIO;
209 return 0;
212 static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
214 char *buff = gspca_dev->usb_buf;
215 int rc;
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;
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;
236 static void reg_w_buf(struct gspca_dev *gspca_dev,
237 const struct cmd *p, int l)
239 do {
240 reg_w(gspca_dev, p->value, p->index);
241 p++;
242 } while (--l > 0);
245 static void setexposure(struct gspca_dev *gspca_dev, s32 val)
247 u16 value;
248 unsigned int w = gspca_dev->pixfmt.width;
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;
261 gspca_dbg(gspca_dev, D_STREAM, "exposure: 0x%04X ms\n\n", value);
262 /* Wonder if theres a good reason for sending it twice */
263 /* probably not but leave it in because...why not */
264 reg_w(gspca_dev, value, REG_COARSE_INTEGRATION_TIME_);
265 reg_w(gspca_dev, value, REG_COARSE_INTEGRATION_TIME_);
268 static int gainify(int in)
271 * TODO: check if there are any issues with corner cases
272 * 0x000 (0):0x07F (127): regL
273 * 0x080 (128) - 0x0FF (255): regM, regL
274 * 0x100 (256) - max: regH, regM, regL
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;
284 static void setggain(struct gspca_dev *gspca_dev, u16 global_gain)
286 u16 normalized;
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);
293 reg_w(gspca_dev, normalized, REG_GREEN1_GAIN);
294 reg_w(gspca_dev, normalized, REG_GREEN2_GAIN);
297 static void setbgain(struct gspca_dev *gspca_dev,
298 u16 gain, u16 global_gain)
300 u16 normalized;
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;
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);
313 reg_w(gspca_dev, normalized, REG_BLUE_GAIN);
316 static void setrgain(struct gspca_dev *gspca_dev,
317 u16 gain, u16 global_gain)
319 u16 normalized;
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;
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);
332 reg_w(gspca_dev, normalized, REG_RED_GAIN);
335 static void configure_wh(struct gspca_dev *gspca_dev)
337 unsigned int w = gspca_dev->pixfmt.width;
339 gspca_dbg(gspca_dev, D_STREAM, "configure_wh\n\n");
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},
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},
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},
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;
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);
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;
401 /* Packets that were encrypted, no idea if the grouping is significant */
402 static void configure_encrypted(struct gspca_dev *gspca_dev)
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},
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},
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);
436 gspca_dbg(gspca_dev, D_STREAM, "Encrypted end\n\n");
439 static int configure(struct gspca_dev *gspca_dev)
441 int rc;
442 char *buff = gspca_dev->usb_buf;
444 gspca_dbg(gspca_dev, D_STREAM, "configure()\n\n");
447 * First driver sets a sort of encryption key
448 * A number of futur requests of this type have wValue and wIndex
449 * encrypted as follows:
450 * -Compute key = this wValue rotate left by 4 bits
451 * (decrypt.py rotates right because we are decrypting)
452 * -Later packets encrypt packets by XOR'ing with key
453 * XOR encrypt/decrypt is symmetrical
454 * wValue, and wIndex are encrypted
455 * bRequest is not and bRequestType is always 0xC0
456 * This allows resyncing if key is unknown?
457 * By setting 0 we XOR with 0 and the shifting and XOR drops out
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;
467 * Next does some sort of 2 packet challenge / response
468 * evidence suggests its an Atmel I2C crypto part but nobody cares to
469 * look
470 * (to make sure its not cloned hardware?)
471 * Ignore: I want to work with their hardware, not clone it
472 * 16 bytes out challenge, requestType: 0x40
473 * 16 bytes in response, requestType: 0xC0
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;
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;
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;
501 * Serial number? Doesn't seem to be required
502 * cam1: \xE6\x0D\x00\x00, cam2: \x70\x19\x00\x00
503 * rc = usb_control_msg(gspca_dev->dev,
504 * usb_rcvctrlpipe(gspca_dev->dev, 0),
505 * 0x20, 0xC0, 0x0000, 0x0000, buff, 4, 500);
508 /* Large (EEPROM?) read, skip it since no idea what to do with it */
509 gspca_dev->usb_err = 0;
510 configure_encrypted(gspca_dev);
511 if (gspca_dev->usb_err)
512 return gspca_dev->usb_err;
514 /* Omitted this by accident, does not work without it */
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;
523 gspca_dbg(gspca_dev, D_STREAM, "Configure complete\n\n");
524 return 0;
527 static int sd_config(struct gspca_dev *gspca_dev,
528 const struct usb_device_id *id)
530 gspca_dev->cam.cam_mode = vga_mode;
531 gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
533 /* Yes we want URBs and we want them now! */
534 gspca_dev->cam.no_urb_create = 0;
535 gspca_dev->cam.bulk_nurbs = 4;
536 /* Largest size the windows driver uses */
537 gspca_dev->cam.bulk_size = BULK_SIZE;
538 /* Def need to use bulk transfers */
539 gspca_dev->cam.bulk = 1;
541 return 0;
544 static int sd_start(struct gspca_dev *gspca_dev)
546 struct sd *sd = (struct sd *) gspca_dev;
547 int rc;
549 sd->this_f = 0;
551 rc = configure(gspca_dev);
552 if (rc < 0) {
553 gspca_err(gspca_dev, "Failed configure\n");
554 return rc;
556 /* First two frames have messed up gains
557 Drop them to avoid special cases in user apps? */
558 return 0;
561 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
562 u8 *data, /* isoc packet */
563 int len) /* iso packet length */
565 struct sd *sd = (struct sd *) gspca_dev;
567 if (len != BULK_SIZE) {
568 /* can we finish a frame? */
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 /* lost some data, discard the frame */
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);
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;
589 static int sd_init(struct gspca_dev *gspca_dev)
591 return 0;
594 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
596 struct gspca_dev *gspca_dev =
597 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
598 struct sd *sd = (struct sd *) gspca_dev;
600 gspca_dev->usb_err = 0;
602 if (!gspca_dev->streaming)
603 return 0;
605 switch (ctrl->id) {
606 case V4L2_CID_EXPOSURE:
607 setexposure(gspca_dev, ctrl->val);
608 break;
609 case V4L2_CID_GAIN:
610 /* gspca_dev->gain automatically updated */
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;
622 return gspca_dev->usb_err;
625 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
626 .s_ctrl = sd_s_ctrl,
629 static int sd_init_controls(struct gspca_dev *gspca_dev)
631 struct sd *sd = (struct sd *) gspca_dev;
632 struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
634 gspca_dev->vdev.ctrl_handler = hdl;
635 v4l2_ctrl_handler_init(hdl, 4);
637 gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
638 /* Mostly limited by URB timeouts */
639 /* XXX: make dynamic based on frame rate? */
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);
648 if (hdl->error) {
649 gspca_err(gspca_dev, "Could not initialize controls\n");
650 return hdl->error;
652 return 0;
655 /* sub-driver description */
656 static 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,
665 /* Table of supported USB devices */
666 static const struct usb_device_id device_table[] = {
667 /* Commented out devices should be related */
668 /* AS: AmScope, TT: ToupTek */
669 /* { USB_DEVICE(0x0547, 0x6035) }, TT UCMOS00350KPA */
670 /* { USB_DEVICE(0x0547, 0x6130) }, TT UCMOS01300KPA */
671 /* { USB_DEVICE(0x0547, 0x6200) }, TT UCMOS02000KPA */
672 /* { USB_DEVICE(0x0547, 0x6310) }, TT UCMOS03100KPA */
673 /* { USB_DEVICE(0x0547, 0x6510) }, TT UCMOS05100KPA */
674 /* { USB_DEVICE(0x0547, 0x6800) }, TT UCMOS08000KPA */
675 /* { USB_DEVICE(0x0547, 0x6801) }, TT UCMOS08000KPB */
676 { USB_DEVICE(0x0547, 0x6801) }, /* TT UCMOS08000KPB, AS MU800 */
677 /* { USB_DEVICE(0x0547, 0x6900) }, TT UCMOS09000KPA */
678 /* { USB_DEVICE(0x0547, 0x6901) }, TT UCMOS09000KPB */
679 /* { USB_DEVICE(0x0547, 0x6010) }, TT UCMOS10000KPA */
680 /* { USB_DEVICE(0x0547, 0x6014) }, TT UCMOS14000KPA */
681 /* { USB_DEVICE(0x0547, 0x6131) }, TT UCMOS01300KMA */
682 /* { USB_DEVICE(0x0547, 0x6511) }, TT UCMOS05100KMA */
683 /* { USB_DEVICE(0x0547, 0x8080) }, TT UHCCD00800KPA */
684 /* { USB_DEVICE(0x0547, 0x8140) }, TT UHCCD01400KPA */
685 /* { USB_DEVICE(0x0547, 0x8141) }, TT EXCCD01400KPA */
686 /* { USB_DEVICE(0x0547, 0x8200) }, TT UHCCD02000KPA */
687 /* { USB_DEVICE(0x0547, 0x8201) }, TT UHCCD02000KPB */
688 /* { USB_DEVICE(0x0547, 0x8310) }, TT UHCCD03100KPA */
689 /* { USB_DEVICE(0x0547, 0x8500) }, TT UHCCD05000KPA */
690 /* { USB_DEVICE(0x0547, 0x8510) }, TT UHCCD05100KPA */
691 /* { USB_DEVICE(0x0547, 0x8600) }, TT UHCCD06000KPA */
692 /* { USB_DEVICE(0x0547, 0x8800) }, TT UHCCD08000KPA */
693 /* { USB_DEVICE(0x0547, 0x8315) }, TT UHCCD03150KPA */
694 /* { USB_DEVICE(0x0547, 0x7800) }, TT UHCCD00800KMA */
695 /* { USB_DEVICE(0x0547, 0x7140) }, TT UHCCD01400KMA */
696 /* { USB_DEVICE(0x0547, 0x7141) }, TT UHCCD01400KMB */
697 /* { USB_DEVICE(0x0547, 0x7200) }, TT UHCCD02000KMA */
698 /* { USB_DEVICE(0x0547, 0x7315) }, TT UHCCD03150KMA */
701 MODULE_DEVICE_TABLE(usb, device_table);
703 static int sd_probe(struct usb_interface *intf,
704 const struct usb_device_id *id)
706 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
707 THIS_MODULE);
710 static 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
721 static int __init sd_mod_init(void)
723 int ret;
725 ret = usb_register(&sd_driver);
726 if (ret < 0)
727 return ret;
728 return 0;
730 static void __exit sd_mod_exit(void)
732 usb_deregister(&sd_driver);
735 module_init(sd_mod_init);
736 module_exit(sd_mod_exit);