Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / media / usb / gspca / touptek.c
1 /*
2  * ToupTek UCMOS / AmScope MU series camera driver
3  * TODO: contrast with ScopeTek / AmScope MDC cameras
4  *
5  * Copyright (C) 2012-2014 John McMaster <JohnDMcMaster@gmail.com>
6  *
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
10  *
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.
15  *
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.
20  */
21
22 #include "gspca.h"
23
24 #define MODULE_NAME "touptek"
25
26 MODULE_AUTHOR("John McMaster");
27 MODULE_DESCRIPTION("ToupTek UCMOS / Amscope MU microscope camera driver");
28 MODULE_LICENSE("GPL");
29
30 /*
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
41  *
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
48  *
49  * Gain implementation
50  * Want to do something similar to mt9v011.c's set_balance
51  *
52  * Gain does not vary with resolution (checked 640x480 vs 1600x1200)
53  *
54  * Constant derivation:
55  *
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
69  *
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
79  *
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
83  *
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
97  *
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
104  */
105 #define GAIN_MAX                511
106
107 /* Frame sync is a short read */
108 #define BULK_SIZE               0x4000
109
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
143
144
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;
150
151         /*
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
155         */
156         struct v4l2_ctrl *blue;
157         struct v4l2_ctrl *red;
158 };
159
160 /* Used to simplify reg write error handling */
161 struct cmd {
162         u16 value;
163         u16 index;
164 };
165
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},
185 };
186
187 /*
188  * As theres no known frame sync, the only way to keep synced is to try hard
189  * to never miss any packets
190  */
191 #if MAX_NURBS < 4
192 #error "Not enough URBs in the gspca table"
193 #endif
194
195 static 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", reply[0]);
207                 return -EIO;
208         }
209         return 0;
210 }
211
212 static void reg_w(struct gspca_dev *gspca_dev, u16 value, u16 index)
213 {
214         char buff[1];
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, 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
236 static 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
245 static 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         /* 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_);
266 }
267
268 static int gainify(int in)
269 {
270         /*
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
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
284 static 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
297 static 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
316 static 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
335 static 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 /* Packets that were encrypted, no idea if the grouping is significant */
402 static 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
438 static int configure(struct gspca_dev *gspca_dev)
439 {
440         int rc;
441         uint8_t buff[4];
442
443         PDEBUG(D_STREAM, "configure()\n");
444
445         /*
446          * First driver sets a sort of encryption key
447          * A number of futur requests of this type have wValue and wIndex
448          * encrypted as follows:
449          * -Compute key = this wValue rotate left by 4 bits
450          *      (decrypt.py rotates right because we are decrypting)
451          * -Later packets encrypt packets by XOR'ing with key
452          *      XOR encrypt/decrypt is symmetrical
453          *      wValue, and wIndex are encrypted
454          *      bRequest is not and bRequestType is always 0xC0
455          *              This allows resyncing if key is unknown?
456          * By setting 0 we XOR with 0 and the shifting and XOR drops out
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          * Next does some sort of 2 packet challenge / response
467          * evidence suggests its an Atmel I2C crypto part but nobody cares to
468          * look
469          * (to make sure its not cloned hardware?)
470          * Ignore: I want to work with their hardware, not clone it
471          * 16 bytes out challenge, requestType: 0x40
472          * 16 bytes in response, requestType: 0xC0
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          * Serial number?  Doesn't seem to be required
498          * cam1: \xE6\x0D\x00\x00, cam2: \x70\x19\x00\x00
499          * rc = usb_control_msg(gspca_dev->dev,
500          *                      usb_rcvctrlpipe(gspca_dev->dev, 0),
501          *                      0x20, 0xC0, 0x0000, 0x0000, buff, 4, 500);
502          */
503
504         /* Large (EEPROM?) read, skip it since no idea what to do with it */
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         /* Omitted this by accident, does not work without it */
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
522 static 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         /* Yes we want URBs and we want them now! */
529         gspca_dev->cam.no_urb_create = 0;
530         gspca_dev->cam.bulk_nurbs = 4;
531         /* Largest size the windows driver uses */
532         gspca_dev->cam.bulk_size = BULK_SIZE;
533         /* Def need to use bulk transfers */
534         gspca_dev->cam.bulk = 1;
535
536         return 0;
537 }
538
539 static 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         /* First two frames have messed up gains
552         Drop them to avoid special cases in user apps? */
553         return 0;
554 }
555
556 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
557                         u8 *data,       /* isoc packet */
558                         int len)        /* iso packet length */
559 {
560         struct sd *sd = (struct sd *) gspca_dev;
561
562         if (len != BULK_SIZE) {
563                 /* can we finish a frame? */
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                 /* lost some data, discard the frame */
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
584 static int sd_init(struct gspca_dev *gspca_dev)
585 {
586         return 0;
587 }
588
589 static 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                 /* gspca_dev->gain automatically updated */
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
620 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
621         .s_ctrl = sd_s_ctrl,
622 };
623
624 static 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         /* Mostly limited by URB timeouts */
634         /* XXX: make dynamic based on frame rate? */
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 /* sub-driver description */
651 static 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 /* Table of supported USB devices */
661 static const struct usb_device_id device_table[] = {
662         /* Commented out devices should be related */
663         /* AS: AmScope, TT: ToupTek */
664         /* { USB_DEVICE(0x0547, 0x6035) },  TT UCMOS00350KPA */
665         /* { USB_DEVICE(0x0547, 0x6130) },  TT UCMOS01300KPA */
666         /* { USB_DEVICE(0x0547, 0x6200) },  TT UCMOS02000KPA */
667         /* { USB_DEVICE(0x0547, 0x6310) },  TT UCMOS03100KPA */
668         /* { USB_DEVICE(0x0547, 0x6510) },  TT UCMOS05100KPA */
669         /* { USB_DEVICE(0x0547, 0x6800) },  TT UCMOS08000KPA */
670         /* { USB_DEVICE(0x0547, 0x6801) },  TT UCMOS08000KPB */
671         { USB_DEVICE(0x0547, 0x6801) }, /* TT UCMOS08000KPB, AS MU800 */
672         /* { USB_DEVICE(0x0547, 0x6900) },  TT UCMOS09000KPA */
673         /* { USB_DEVICE(0x0547, 0x6901) },  TT UCMOS09000KPB */
674         /* { USB_DEVICE(0x0547, 0x6010) },  TT UCMOS10000KPA */
675         /* { USB_DEVICE(0x0547, 0x6014) },  TT UCMOS14000KPA */
676         /* { USB_DEVICE(0x0547, 0x6131) },  TT UCMOS01300KMA */
677         /* { USB_DEVICE(0x0547, 0x6511) },  TT UCMOS05100KMA */
678         /* { USB_DEVICE(0x0547, 0x8080) },  TT UHCCD00800KPA */
679         /* { USB_DEVICE(0x0547, 0x8140) },  TT UHCCD01400KPA */
680         /* { USB_DEVICE(0x0547, 0x8141) },  TT EXCCD01400KPA */
681         /* { USB_DEVICE(0x0547, 0x8200) },  TT UHCCD02000KPA */
682         /* { USB_DEVICE(0x0547, 0x8201) },  TT UHCCD02000KPB */
683         /* { USB_DEVICE(0x0547, 0x8310) },  TT UHCCD03100KPA */
684         /* { USB_DEVICE(0x0547, 0x8500) },  TT UHCCD05000KPA */
685         /* { USB_DEVICE(0x0547, 0x8510) },  TT UHCCD05100KPA */
686         /* { USB_DEVICE(0x0547, 0x8600) },  TT UHCCD06000KPA */
687         /* { USB_DEVICE(0x0547, 0x8800) },  TT UHCCD08000KPA */
688         /* { USB_DEVICE(0x0547, 0x8315) },  TT UHCCD03150KPA */
689         /* { USB_DEVICE(0x0547, 0x7800) },  TT UHCCD00800KMA */
690         /* { USB_DEVICE(0x0547, 0x7140) },  TT UHCCD01400KMA */
691         /* { USB_DEVICE(0x0547, 0x7141) },  TT UHCCD01400KMB */
692         /* { USB_DEVICE(0x0547, 0x7200) },  TT UHCCD02000KMA */
693         /* { USB_DEVICE(0x0547, 0x7315) },  TT UHCCD03150KMA */
694         { }
695 };
696 MODULE_DEVICE_TABLE(usb, device_table);
697
698 static 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
705 static 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
716 static 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 }
725 static void __exit sd_mod_exit(void)
726 {
727         usb_deregister(&sd_driver);
728 }
729
730 module_init(sd_mod_init);
731 module_exit(sd_mod_exit);