Merge tag 'for-linus-20190524' of git://git.kernel.dk/linux-block
[linux-2.6-microblaze.git] / drivers / media / usb / gspca / mr97310a.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Mars MR97310A library
4  *
5  * The original mr97310a driver, which supported the Aiptek Pencam VGA+, is
6  * Copyright (C) 2009 Kyle Guinn <elyk03@gmail.com>
7  *
8  * Support for the MR97310A cameras in addition to the Aiptek Pencam VGA+
9  * and for the routines for detecting and classifying these various cameras,
10  * is Copyright (C) 2009 Theodore Kilgore <kilgota@auburn.edu>
11  *
12  * Support for the control settings for the CIF cameras is
13  * Copyright (C) 2009 Hans de Goede <hdegoede@redhat.com> and
14  * Thomas Kaiser <thomas@kaiser-linux.li>
15  *
16  * Support for the control settings for the VGA cameras is
17  * Copyright (C) 2009 Theodore Kilgore <kilgota@auburn.edu>
18  *
19  * Several previously unsupported cameras are owned and have been tested by
20  * Hans de Goede <hdegoede@redhat.com> and
21  * Thomas Kaiser <thomas@kaiser-linux.li> and
22  * Theodore Kilgore <kilgota@auburn.edu> and
23  * Edmond Rodriguez <erodrig_97@yahoo.com> and
24  * Aurelien Jacobs <aurel@gnuage.org>
25  *
26  * The MR97311A support in gspca/mars.c has been helpful in understanding some
27  * of the registers in these cameras.
28  */
29
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
32 #define MODULE_NAME "mr97310a"
33
34 #include "gspca.h"
35
36 #define CAM_TYPE_CIF                    0
37 #define CAM_TYPE_VGA                    1
38
39 #define MR97310A_BRIGHTNESS_DEFAULT     0
40
41 #define MR97310A_EXPOSURE_MIN           0
42 #define MR97310A_EXPOSURE_MAX           4095
43 #define MR97310A_EXPOSURE_DEFAULT       1000
44
45 #define MR97310A_GAIN_MIN               0
46 #define MR97310A_GAIN_MAX               31
47 #define MR97310A_GAIN_DEFAULT           25
48
49 #define MR97310A_CONTRAST_MIN           0
50 #define MR97310A_CONTRAST_MAX           31
51 #define MR97310A_CONTRAST_DEFAULT       23
52
53 #define MR97310A_CS_GAIN_MIN            0
54 #define MR97310A_CS_GAIN_MAX            0x7ff
55 #define MR97310A_CS_GAIN_DEFAULT        0x110
56
57 #define MR97310A_CID_CLOCKDIV (V4L2_CTRL_CLASS_USER + 0x1000)
58 #define MR97310A_MIN_CLOCKDIV_MIN       3
59 #define MR97310A_MIN_CLOCKDIV_MAX       8
60 #define MR97310A_MIN_CLOCKDIV_DEFAULT   3
61
62 MODULE_AUTHOR("Kyle Guinn <elyk03@gmail.com>,Theodore Kilgore <kilgota@auburn.edu>");
63 MODULE_DESCRIPTION("GSPCA/Mars-Semi MR97310A USB Camera Driver");
64 MODULE_LICENSE("GPL");
65
66 /* global parameters */
67 static int force_sensor_type = -1;
68 module_param(force_sensor_type, int, 0644);
69 MODULE_PARM_DESC(force_sensor_type, "Force sensor type (-1 (auto), 0 or 1)");
70
71 /* specific webcam descriptor */
72 struct sd {
73         struct gspca_dev gspca_dev;  /* !! must be the first item */
74         struct { /* exposure/min_clockdiv control cluster */
75                 struct v4l2_ctrl *exposure;
76                 struct v4l2_ctrl *min_clockdiv;
77         };
78         u8 sof_read;
79         u8 cam_type;    /* 0 is CIF and 1 is VGA */
80         u8 sensor_type; /* We use 0 and 1 here, too. */
81         u8 do_lcd_stop;
82         u8 adj_colors;
83 };
84
85 struct sensor_w_data {
86         u8 reg;
87         u8 flags;
88         u8 data[16];
89         int len;
90 };
91
92 static void sd_stopN(struct gspca_dev *gspca_dev);
93
94 static const struct v4l2_pix_format vga_mode[] = {
95         {160, 120, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
96                 .bytesperline = 160,
97                 .sizeimage = 160 * 120,
98                 .colorspace = V4L2_COLORSPACE_SRGB,
99                 .priv = 4},
100         {176, 144, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
101                 .bytesperline = 176,
102                 .sizeimage = 176 * 144,
103                 .colorspace = V4L2_COLORSPACE_SRGB,
104                 .priv = 3},
105         {320, 240, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
106                 .bytesperline = 320,
107                 .sizeimage = 320 * 240,
108                 .colorspace = V4L2_COLORSPACE_SRGB,
109                 .priv = 2},
110         {352, 288, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
111                 .bytesperline = 352,
112                 .sizeimage = 352 * 288,
113                 .colorspace = V4L2_COLORSPACE_SRGB,
114                 .priv = 1},
115         {640, 480, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
116                 .bytesperline = 640,
117                 .sizeimage = 640 * 480,
118                 .colorspace = V4L2_COLORSPACE_SRGB,
119                 .priv = 0},
120 };
121
122 /* the bytes to write are in gspca_dev->usb_buf */
123 static int mr_write(struct gspca_dev *gspca_dev, int len)
124 {
125         int rc;
126
127         rc = usb_bulk_msg(gspca_dev->dev,
128                           usb_sndbulkpipe(gspca_dev->dev, 4),
129                           gspca_dev->usb_buf, len, NULL, 500);
130         if (rc < 0)
131                 pr_err("reg write [%02x] error %d\n",
132                        gspca_dev->usb_buf[0], rc);
133         return rc;
134 }
135
136 /* the bytes are read into gspca_dev->usb_buf */
137 static int mr_read(struct gspca_dev *gspca_dev, int len)
138 {
139         int rc;
140
141         rc = usb_bulk_msg(gspca_dev->dev,
142                           usb_rcvbulkpipe(gspca_dev->dev, 3),
143                           gspca_dev->usb_buf, len, NULL, 500);
144         if (rc < 0)
145                 pr_err("reg read [%02x] error %d\n",
146                        gspca_dev->usb_buf[0], rc);
147         return rc;
148 }
149
150 static int sensor_write_reg(struct gspca_dev *gspca_dev, u8 reg, u8 flags,
151         const u8 *data, int len)
152 {
153         gspca_dev->usb_buf[0] = 0x1f;
154         gspca_dev->usb_buf[1] = flags;
155         gspca_dev->usb_buf[2] = reg;
156         memcpy(gspca_dev->usb_buf + 3, data, len);
157
158         return mr_write(gspca_dev, len + 3);
159 }
160
161 static int sensor_write_regs(struct gspca_dev *gspca_dev,
162         const struct sensor_w_data *data, int len)
163 {
164         int i, rc;
165
166         for (i = 0; i < len; i++) {
167                 rc = sensor_write_reg(gspca_dev, data[i].reg, data[i].flags,
168                                           data[i].data, data[i].len);
169                 if (rc < 0)
170                         return rc;
171         }
172
173         return 0;
174 }
175
176 static int sensor_write1(struct gspca_dev *gspca_dev, u8 reg, u8 data)
177 {
178         struct sd *sd = (struct sd *) gspca_dev;
179         u8 buf, confirm_reg;
180         int rc;
181
182         buf = data;
183         if (sd->cam_type == CAM_TYPE_CIF) {
184                 rc = sensor_write_reg(gspca_dev, reg, 0x01, &buf, 1);
185                 confirm_reg = sd->sensor_type ? 0x13 : 0x11;
186         } else {
187                 rc = sensor_write_reg(gspca_dev, reg, 0x00, &buf, 1);
188                 confirm_reg = 0x11;
189         }
190         if (rc < 0)
191                 return rc;
192
193         buf = 0x01;
194         rc = sensor_write_reg(gspca_dev, confirm_reg, 0x00, &buf, 1);
195         if (rc < 0)
196                 return rc;
197
198         return 0;
199 }
200
201 static int cam_get_response16(struct gspca_dev *gspca_dev, u8 reg, int verbose)
202 {
203         int err_code;
204
205         gspca_dev->usb_buf[0] = reg;
206         err_code = mr_write(gspca_dev, 1);
207         if (err_code < 0)
208                 return err_code;
209
210         err_code = mr_read(gspca_dev, 16);
211         if (err_code < 0)
212                 return err_code;
213
214         if (verbose)
215                 gspca_dbg(gspca_dev, D_PROBE, "Register: %02x reads %02x%02x%02x\n",
216                           reg,
217                           gspca_dev->usb_buf[0],
218                           gspca_dev->usb_buf[1],
219                           gspca_dev->usb_buf[2]);
220
221         return 0;
222 }
223
224 static int zero_the_pointer(struct gspca_dev *gspca_dev)
225 {
226         __u8 *data = gspca_dev->usb_buf;
227         int err_code;
228         u8 status = 0;
229         int tries = 0;
230
231         err_code = cam_get_response16(gspca_dev, 0x21, 0);
232         if (err_code < 0)
233                 return err_code;
234
235         data[0] = 0x19;
236         data[1] = 0x51;
237         err_code = mr_write(gspca_dev, 2);
238         if (err_code < 0)
239                 return err_code;
240
241         err_code = cam_get_response16(gspca_dev, 0x21, 0);
242         if (err_code < 0)
243                 return err_code;
244
245         data[0] = 0x19;
246         data[1] = 0xba;
247         err_code = mr_write(gspca_dev, 2);
248         if (err_code < 0)
249                 return err_code;
250
251         err_code = cam_get_response16(gspca_dev, 0x21, 0);
252         if (err_code < 0)
253                 return err_code;
254
255         data[0] = 0x19;
256         data[1] = 0x00;
257         err_code = mr_write(gspca_dev, 2);
258         if (err_code < 0)
259                 return err_code;
260
261         err_code = cam_get_response16(gspca_dev, 0x21, 0);
262         if (err_code < 0)
263                 return err_code;
264
265         data[0] = 0x19;
266         data[1] = 0x00;
267         err_code = mr_write(gspca_dev, 2);
268         if (err_code < 0)
269                 return err_code;
270
271         while (status != 0x0a && tries < 256) {
272                 err_code = cam_get_response16(gspca_dev, 0x21, 0);
273                 status = data[0];
274                 tries++;
275                 if (err_code < 0)
276                         return err_code;
277         }
278         if (status != 0x0a)
279                 gspca_err(gspca_dev, "status is %02x\n", status);
280
281         tries = 0;
282         while (tries < 4) {
283                 data[0] = 0x19;
284                 data[1] = 0x00;
285                 err_code = mr_write(gspca_dev, 2);
286                 if (err_code < 0)
287                         return err_code;
288
289                 err_code = cam_get_response16(gspca_dev, 0x21, 0);
290                 status = data[0];
291                 tries++;
292                 if (err_code < 0)
293                         return err_code;
294         }
295
296         data[0] = 0x19;
297         err_code = mr_write(gspca_dev, 1);
298         if (err_code < 0)
299                 return err_code;
300
301         err_code = mr_read(gspca_dev, 16);
302         if (err_code < 0)
303                 return err_code;
304
305         return 0;
306 }
307
308 static int stream_start(struct gspca_dev *gspca_dev)
309 {
310         gspca_dev->usb_buf[0] = 0x01;
311         gspca_dev->usb_buf[1] = 0x01;
312         return mr_write(gspca_dev, 2);
313 }
314
315 static void stream_stop(struct gspca_dev *gspca_dev)
316 {
317         gspca_dev->usb_buf[0] = 0x01;
318         gspca_dev->usb_buf[1] = 0x00;
319         if (mr_write(gspca_dev, 2) < 0)
320                 gspca_err(gspca_dev, "Stream Stop failed\n");
321 }
322
323 static void lcd_stop(struct gspca_dev *gspca_dev)
324 {
325         gspca_dev->usb_buf[0] = 0x19;
326         gspca_dev->usb_buf[1] = 0x54;
327         if (mr_write(gspca_dev, 2) < 0)
328                 gspca_err(gspca_dev, "LCD Stop failed\n");
329 }
330
331 static int isoc_enable(struct gspca_dev *gspca_dev)
332 {
333         gspca_dev->usb_buf[0] = 0x00;
334         gspca_dev->usb_buf[1] = 0x4d;  /* ISOC transferring enable... */
335         return mr_write(gspca_dev, 2);
336 }
337
338 /* This function is called at probe time */
339 static int sd_config(struct gspca_dev *gspca_dev,
340                      const struct usb_device_id *id)
341 {
342         struct sd *sd = (struct sd *) gspca_dev;
343         struct cam *cam;
344         int err_code;
345
346         cam = &gspca_dev->cam;
347         cam->cam_mode = vga_mode;
348         cam->nmodes = ARRAY_SIZE(vga_mode);
349         sd->do_lcd_stop = 0;
350
351         /* Several of the supported CIF cameras share the same USB ID but
352          * require different initializations and different control settings.
353          * The same is true of the VGA cameras. Therefore, we are forced
354          * to start the initialization process in order to determine which
355          * camera is present. Some of the supported cameras require the
356          * memory pointer to be set to 0 as the very first item of business
357          * or else they will not stream. So we do that immediately.
358          */
359         err_code = zero_the_pointer(gspca_dev);
360         if (err_code < 0)
361                 return err_code;
362
363         err_code = stream_start(gspca_dev);
364         if (err_code < 0)
365                 return err_code;
366
367         /* Now, the query for sensor type. */
368         err_code = cam_get_response16(gspca_dev, 0x07, 1);
369         if (err_code < 0)
370                 return err_code;
371
372         if (id->idProduct == 0x0110 || id->idProduct == 0x010e) {
373                 sd->cam_type = CAM_TYPE_CIF;
374                 cam->nmodes--;
375                 /*
376                  * All but one of the known CIF cameras share the same USB ID,
377                  * but two different init routines are in use, and the control
378                  * settings are different, too. We need to detect which camera
379                  * of the two known varieties is connected!
380                  *
381                  * A list of known CIF cameras follows. They all report either
382                  * 0200 for type 0 or 0300 for type 1.
383                  * If you have another to report, please do
384                  *
385                  * Name         sd->sensor_type         reported by
386                  *
387                  * Sakar 56379 Spy-shot 0               T. Kilgore
388                  * Innovage             0               T. Kilgore
389                  * Vivitar Mini         0               H. De Goede
390                  * Vivitar Mini         0               E. Rodriguez
391                  * Vivitar Mini         1               T. Kilgore
392                  * Elta-Media 8212dc    1               T. Kaiser
393                  * Philips dig. keych.  1               T. Kilgore
394                  * Trust Spyc@m 100     1               A. Jacobs
395                  */
396                 switch (gspca_dev->usb_buf[0]) {
397                 case 2:
398                         sd->sensor_type = 0;
399                         break;
400                 case 3:
401                         sd->sensor_type = 1;
402                         break;
403                 default:
404                         pr_err("Unknown CIF Sensor id : %02x\n",
405                                gspca_dev->usb_buf[1]);
406                         return -ENODEV;
407                 }
408                 gspca_dbg(gspca_dev, D_PROBE, "MR97310A CIF camera detected, sensor: %d\n",
409                           sd->sensor_type);
410         } else {
411                 sd->cam_type = CAM_TYPE_VGA;
412
413                 /*
414                  * Here is a table of the responses to the query for sensor
415                  * type, from the known MR97310A VGA cameras. Six different
416                  * cameras of which five share the same USB ID.
417                  *
418                  * Name                 gspca_dev->usb_buf[]    sd->sensor_type
419                  *                              sd->do_lcd_stop
420                  * Aiptek Pencam VGA+   0300            0               1
421                  * ION digital          0300            0               1
422                  * Argus DC-1620        0450            1               0
423                  * Argus QuickClix      0420            1               1
424                  * Sakar 77379 Digital  0350            0               1
425                  * Sakar 1638x CyberPix 0120            0               2
426                  *
427                  * Based upon these results, we assume default settings
428                  * and then correct as necessary, as follows.
429                  *
430                  */
431
432                 sd->sensor_type = 1;
433                 sd->do_lcd_stop = 0;
434                 sd->adj_colors = 0;
435                 if (gspca_dev->usb_buf[0] == 0x01) {
436                         sd->sensor_type = 2;
437                 } else if ((gspca_dev->usb_buf[0] != 0x03) &&
438                                         (gspca_dev->usb_buf[0] != 0x04)) {
439                         pr_err("Unknown VGA Sensor id Byte 0: %02x\n",
440                                gspca_dev->usb_buf[0]);
441                         pr_err("Defaults assumed, may not work\n");
442                         pr_err("Please report this\n");
443                 }
444                 /* Sakar Digital color needs to be adjusted. */
445                 if ((gspca_dev->usb_buf[0] == 0x03) &&
446                                         (gspca_dev->usb_buf[1] == 0x50))
447                         sd->adj_colors = 1;
448                 if (gspca_dev->usb_buf[0] == 0x04) {
449                         sd->do_lcd_stop = 1;
450                         switch (gspca_dev->usb_buf[1]) {
451                         case 0x50:
452                                 sd->sensor_type = 0;
453                                 gspca_dbg(gspca_dev, D_PROBE, "sensor_type corrected to 0\n");
454                                 break;
455                         case 0x20:
456                                 /* Nothing to do here. */
457                                 break;
458                         default:
459                                 pr_err("Unknown VGA Sensor id Byte 1: %02x\n",
460                                        gspca_dev->usb_buf[1]);
461                                 pr_err("Defaults assumed, may not work\n");
462                                 pr_err("Please report this\n");
463                         }
464                 }
465                 gspca_dbg(gspca_dev, D_PROBE, "MR97310A VGA camera detected, sensor: %d\n",
466                           sd->sensor_type);
467         }
468         /* Stop streaming as we've started it only to probe the sensor type. */
469         sd_stopN(gspca_dev);
470
471         if (force_sensor_type != -1) {
472                 sd->sensor_type = !!force_sensor_type;
473                 gspca_dbg(gspca_dev, D_PROBE, "Forcing sensor type to: %d\n",
474                           sd->sensor_type);
475         }
476
477         return 0;
478 }
479
480 /* this function is called at probe and resume time */
481 static int sd_init(struct gspca_dev *gspca_dev)
482 {
483         return 0;
484 }
485
486 static int start_cif_cam(struct gspca_dev *gspca_dev)
487 {
488         struct sd *sd = (struct sd *) gspca_dev;
489         __u8 *data = gspca_dev->usb_buf;
490         int err_code;
491         static const __u8 startup_string[] = {
492                 0x00,
493                 0x0d,
494                 0x01,
495                 0x00, /* Hsize/8 for 352 or 320 */
496                 0x00, /* Vsize/4 for 288 or 240 */
497                 0x13, /* or 0xbb, depends on sensor */
498                 0x00, /* Hstart, depends on res. */
499                 0x00, /* reserved ? */
500                 0x00, /* Vstart, depends on res. and sensor */
501                 0x50, /* 0x54 to get 176 or 160 */
502                 0xc0
503         };
504
505         /* Note: Some of the above descriptions guessed from MR97113A driver */
506
507         memcpy(data, startup_string, 11);
508         if (sd->sensor_type)
509                 data[5] = 0xbb;
510
511         switch (gspca_dev->pixfmt.width) {
512         case 160:
513                 data[9] |= 0x04;  /* reg 8, 2:1 scale down from 320 */
514                 /* fall through */
515         case 320:
516         default:
517                 data[3] = 0x28;                    /* reg 2, H size/8 */
518                 data[4] = 0x3c;                    /* reg 3, V size/4 */
519                 data[6] = 0x14;                    /* reg 5, H start  */
520                 data[8] = 0x1a + sd->sensor_type;  /* reg 7, V start  */
521                 break;
522         case 176:
523                 data[9] |= 0x04;  /* reg 8, 2:1 scale down from 352 */
524                 /* fall through */
525         case 352:
526                 data[3] = 0x2c;                    /* reg 2, H size/8 */
527                 data[4] = 0x48;                    /* reg 3, V size/4 */
528                 data[6] = 0x06;                    /* reg 5, H start  */
529                 data[8] = 0x06 - sd->sensor_type;  /* reg 7, V start  */
530                 break;
531         }
532         err_code = mr_write(gspca_dev, 11);
533         if (err_code < 0)
534                 return err_code;
535
536         if (!sd->sensor_type) {
537                 static const struct sensor_w_data cif_sensor0_init_data[] = {
538                         {0x02, 0x00, {0x03, 0x5a, 0xb5, 0x01,
539                                       0x0f, 0x14, 0x0f, 0x10}, 8},
540                         {0x0c, 0x00, {0x04, 0x01, 0x01, 0x00, 0x1f}, 5},
541                         {0x12, 0x00, {0x07}, 1},
542                         {0x1f, 0x00, {0x06}, 1},
543                         {0x27, 0x00, {0x04}, 1},
544                         {0x29, 0x00, {0x0c}, 1},
545                         {0x40, 0x00, {0x40, 0x00, 0x04}, 3},
546                         {0x50, 0x00, {0x60}, 1},
547                         {0x60, 0x00, {0x06}, 1},
548                         {0x6b, 0x00, {0x85, 0x85, 0xc8, 0xc8, 0xc8, 0xc8}, 6},
549                         {0x72, 0x00, {0x1e, 0x56}, 2},
550                         {0x75, 0x00, {0x58, 0x40, 0xa2, 0x02, 0x31, 0x02,
551                                       0x31, 0x80, 0x00}, 9},
552                         {0x11, 0x00, {0x01}, 1},
553                         {0, 0, {0}, 0}
554                 };
555                 err_code = sensor_write_regs(gspca_dev, cif_sensor0_init_data,
556                                          ARRAY_SIZE(cif_sensor0_init_data));
557         } else {        /* sd->sensor_type = 1 */
558                 static const struct sensor_w_data cif_sensor1_init_data[] = {
559                         /* Reg 3,4, 7,8 get set by the controls */
560                         {0x02, 0x00, {0x10}, 1},
561                         {0x05, 0x01, {0x22}, 1}, /* 5/6 also seen as 65h/32h */
562                         {0x06, 0x01, {0x00}, 1},
563                         {0x09, 0x02, {0x0e}, 1},
564                         {0x0a, 0x02, {0x05}, 1},
565                         {0x0b, 0x02, {0x05}, 1},
566                         {0x0c, 0x02, {0x0f}, 1},
567                         {0x0d, 0x02, {0x07}, 1},
568                         {0x0e, 0x02, {0x0c}, 1},
569                         {0x0f, 0x00, {0x00}, 1},
570                         {0x10, 0x00, {0x06}, 1},
571                         {0x11, 0x00, {0x07}, 1},
572                         {0x12, 0x00, {0x00}, 1},
573                         {0x13, 0x00, {0x01}, 1},
574                         {0, 0, {0}, 0}
575                 };
576                 /* Without this command the cam won't work with USB-UHCI */
577                 gspca_dev->usb_buf[0] = 0x0a;
578                 gspca_dev->usb_buf[1] = 0x00;
579                 err_code = mr_write(gspca_dev, 2);
580                 if (err_code < 0)
581                         return err_code;
582                 err_code = sensor_write_regs(gspca_dev, cif_sensor1_init_data,
583                                          ARRAY_SIZE(cif_sensor1_init_data));
584         }
585         return err_code;
586 }
587
588 static int start_vga_cam(struct gspca_dev *gspca_dev)
589 {
590         struct sd *sd = (struct sd *) gspca_dev;
591         __u8 *data = gspca_dev->usb_buf;
592         int err_code;
593         static const __u8 startup_string[] =
594                 {0x00, 0x0d, 0x01, 0x00, 0x00, 0x2b, 0x00, 0x00,
595                  0x00, 0x50, 0xc0};
596         /* What some of these mean is explained in start_cif_cam(), above */
597
598         memcpy(data, startup_string, 11);
599         if (!sd->sensor_type) {
600                 data[5]  = 0x00;
601                 data[10] = 0x91;
602         }
603         if (sd->sensor_type == 2) {
604                 data[5]  = 0x00;
605                 data[10] = 0x18;
606         }
607
608         switch (gspca_dev->pixfmt.width) {
609         case 160:
610                 data[9] |= 0x0c;  /* reg 8, 4:1 scale down */
611                 /* fall through */
612         case 320:
613                 data[9] |= 0x04;  /* reg 8, 2:1 scale down */
614                 /* fall through */
615         case 640:
616         default:
617                 data[3] = 0x50;  /* reg 2, H size/8 */
618                 data[4] = 0x78;  /* reg 3, V size/4 */
619                 data[6] = 0x04;  /* reg 5, H start */
620                 data[8] = 0x03;  /* reg 7, V start */
621                 if (sd->sensor_type == 2) {
622                         data[6] = 2;
623                         data[8] = 1;
624                 }
625                 if (sd->do_lcd_stop)
626                         data[8] = 0x04;  /* Bayer tile shifted */
627                 break;
628
629         case 176:
630                 data[9] |= 0x04;  /* reg 8, 2:1 scale down */
631                 /* fall through */
632         case 352:
633                 data[3] = 0x2c;  /* reg 2, H size */
634                 data[4] = 0x48;  /* reg 3, V size */
635                 data[6] = 0x94;  /* reg 5, H start */
636                 data[8] = 0x63;  /* reg 7, V start */
637                 if (sd->do_lcd_stop)
638                         data[8] = 0x64;  /* Bayer tile shifted */
639                 break;
640         }
641
642         err_code = mr_write(gspca_dev, 11);
643         if (err_code < 0)
644                 return err_code;
645
646         if (!sd->sensor_type) {
647                 static const struct sensor_w_data vga_sensor0_init_data[] = {
648                         {0x01, 0x00, {0x0c, 0x00, 0x04}, 3},
649                         {0x14, 0x00, {0x01, 0xe4, 0x02, 0x84}, 4},
650                         {0x20, 0x00, {0x00, 0x80, 0x00, 0x08}, 4},
651                         {0x25, 0x00, {0x03, 0xa9, 0x80}, 3},
652                         {0x30, 0x00, {0x30, 0x18, 0x10, 0x18}, 4},
653                         {0, 0, {0}, 0}
654                 };
655                 err_code = sensor_write_regs(gspca_dev, vga_sensor0_init_data,
656                                          ARRAY_SIZE(vga_sensor0_init_data));
657         } else if (sd->sensor_type == 1) {
658                 static const struct sensor_w_data color_adj[] = {
659                         {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
660                                 /* adjusted blue, green, red gain correct
661                                    too much blue from the Sakar Digital */
662                                 0x05, 0x01, 0x04}, 8}
663                 };
664
665                 static const struct sensor_w_data color_no_adj[] = {
666                         {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
667                                 /* default blue, green, red gain settings */
668                                 0x07, 0x00, 0x01}, 8}
669                 };
670
671                 static const struct sensor_w_data vga_sensor1_init_data[] = {
672                         {0x11, 0x04, {0x01}, 1},
673                         {0x0a, 0x00, {0x00, 0x01, 0x00, 0x00, 0x01,
674                         /* These settings may be better for some cameras */
675                         /* {0x0a, 0x00, {0x01, 0x06, 0x00, 0x00, 0x01, */
676                                 0x00, 0x0a}, 7},
677                         {0x11, 0x04, {0x01}, 1},
678                         {0x12, 0x00, {0x00, 0x63, 0x00, 0x70, 0x00, 0x00}, 6},
679                         {0x11, 0x04, {0x01}, 1},
680                         {0, 0, {0}, 0}
681                 };
682
683                 if (sd->adj_colors)
684                         err_code = sensor_write_regs(gspca_dev, color_adj,
685                                          ARRAY_SIZE(color_adj));
686                 else
687                         err_code = sensor_write_regs(gspca_dev, color_no_adj,
688                                          ARRAY_SIZE(color_no_adj));
689
690                 if (err_code < 0)
691                         return err_code;
692
693                 err_code = sensor_write_regs(gspca_dev, vga_sensor1_init_data,
694                                          ARRAY_SIZE(vga_sensor1_init_data));
695         } else {        /* sensor type == 2 */
696                 static const struct sensor_w_data vga_sensor2_init_data[] = {
697
698                         {0x01, 0x00, {0x48}, 1},
699                         {0x02, 0x00, {0x22}, 1},
700                         /* Reg 3 msb and 4 is lsb of the exposure setting*/
701                         {0x05, 0x00, {0x10}, 1},
702                         {0x06, 0x00, {0x00}, 1},
703                         {0x07, 0x00, {0x00}, 1},
704                         {0x08, 0x00, {0x00}, 1},
705                         {0x09, 0x00, {0x00}, 1},
706                         /* The following are used in the gain control
707                          * which is BTW completely borked in the OEM driver
708                          * The values for each color go from 0 to 0x7ff
709                          *{0x0a, 0x00, {0x01}, 1},  green1 gain msb
710                          *{0x0b, 0x00, {0x10}, 1},  green1 gain lsb
711                          *{0x0c, 0x00, {0x01}, 1},  red gain msb
712                          *{0x0d, 0x00, {0x10}, 1},  red gain lsb
713                          *{0x0e, 0x00, {0x01}, 1},  blue gain msb
714                          *{0x0f, 0x00, {0x10}, 1},  blue gain lsb
715                          *{0x10, 0x00, {0x01}, 1}, green2 gain msb
716                          *{0x11, 0x00, {0x10}, 1}, green2 gain lsb
717                          */
718                         {0x12, 0x00, {0x00}, 1},
719                         {0x13, 0x00, {0x04}, 1}, /* weird effect on colors */
720                         {0x14, 0x00, {0x00}, 1},
721                         {0x15, 0x00, {0x06}, 1},
722                         {0x16, 0x00, {0x01}, 1},
723                         {0x17, 0x00, {0xe2}, 1}, /* vertical alignment */
724                         {0x18, 0x00, {0x02}, 1},
725                         {0x19, 0x00, {0x82}, 1}, /* don't mess with */
726                         {0x1a, 0x00, {0x00}, 1},
727                         {0x1b, 0x00, {0x20}, 1},
728                         /* {0x1c, 0x00, {0x17}, 1}, contrast control */
729                         {0x1d, 0x00, {0x80}, 1}, /* moving causes a mess */
730                         {0x1e, 0x00, {0x08}, 1}, /* moving jams the camera */
731                         {0x1f, 0x00, {0x0c}, 1},
732                         {0x20, 0x00, {0x00}, 1},
733                         {0, 0, {0}, 0}
734                 };
735                 err_code = sensor_write_regs(gspca_dev, vga_sensor2_init_data,
736                                          ARRAY_SIZE(vga_sensor2_init_data));
737         }
738         return err_code;
739 }
740
741 static int sd_start(struct gspca_dev *gspca_dev)
742 {
743         struct sd *sd = (struct sd *) gspca_dev;
744         int err_code;
745
746         sd->sof_read = 0;
747
748         /* Some of the VGA cameras require the memory pointer
749          * to be set to 0 again. We have been forced to start the
750          * stream in sd_config() to detect the hardware, and closed it.
751          * Thus, we need here to do a completely fresh and clean start. */
752         err_code = zero_the_pointer(gspca_dev);
753         if (err_code < 0)
754                 return err_code;
755
756         err_code = stream_start(gspca_dev);
757         if (err_code < 0)
758                 return err_code;
759
760         if (sd->cam_type == CAM_TYPE_CIF) {
761                 err_code = start_cif_cam(gspca_dev);
762         } else {
763                 err_code = start_vga_cam(gspca_dev);
764         }
765         if (err_code < 0)
766                 return err_code;
767
768         return isoc_enable(gspca_dev);
769 }
770
771 static void sd_stopN(struct gspca_dev *gspca_dev)
772 {
773         struct sd *sd = (struct sd *) gspca_dev;
774
775         stream_stop(gspca_dev);
776         /* Not all the cams need this, but even if not, probably a good idea */
777         zero_the_pointer(gspca_dev);
778         if (sd->do_lcd_stop)
779                 lcd_stop(gspca_dev);
780 }
781
782 static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
783 {
784         struct sd *sd = (struct sd *) gspca_dev;
785         u8 sign_reg = 7;  /* This reg and the next one used on CIF cams. */
786         u8 value_reg = 8; /* VGA cams seem to use regs 0x0b and 0x0c */
787         static const u8 quick_clix_table[] =
788         /*        0  1  2   3  4  5  6  7  8  9  10  11  12  13  14  15 */
789                 { 0, 4, 8, 12, 1, 2, 3, 5, 6, 9,  7, 10, 13, 11, 14, 15};
790         if (sd->cam_type == CAM_TYPE_VGA) {
791                 sign_reg += 4;
792                 value_reg += 4;
793         }
794
795         /* Note register 7 is also seen as 0x8x or 0xCx in some dumps */
796         if (val > 0) {
797                 sensor_write1(gspca_dev, sign_reg, 0x00);
798         } else {
799                 sensor_write1(gspca_dev, sign_reg, 0x01);
800                 val = 257 - val;
801         }
802         /* Use lookup table for funky Argus QuickClix brightness */
803         if (sd->do_lcd_stop)
804                 val = quick_clix_table[val];
805
806         sensor_write1(gspca_dev, value_reg, val);
807 }
808
809 static void setexposure(struct gspca_dev *gspca_dev, s32 expo, s32 min_clockdiv)
810 {
811         struct sd *sd = (struct sd *) gspca_dev;
812         int exposure = MR97310A_EXPOSURE_DEFAULT;
813         u8 buf[2];
814
815         if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1) {
816                 /* This cam does not like exposure settings < 300,
817                    so scale 0 - 4095 to 300 - 4095 */
818                 exposure = (expo * 9267) / 10000 + 300;
819                 sensor_write1(gspca_dev, 3, exposure >> 4);
820                 sensor_write1(gspca_dev, 4, exposure & 0x0f);
821         } else if (sd->sensor_type == 2) {
822                 exposure = expo;
823                 exposure >>= 3;
824                 sensor_write1(gspca_dev, 3, exposure >> 8);
825                 sensor_write1(gspca_dev, 4, exposure & 0xff);
826         } else {
827                 /* We have both a clock divider and an exposure register.
828                    We first calculate the clock divider, as that determines
829                    the maximum exposure and then we calculate the exposure
830                    register setting (which goes from 0 - 511).
831
832                    Note our 0 - 4095 exposure is mapped to 0 - 511
833                    milliseconds exposure time */
834                 u8 clockdiv = (60 * expo + 7999) / 8000;
835
836                 /* Limit framerate to not exceed usb bandwidth */
837                 if (clockdiv < min_clockdiv && gspca_dev->pixfmt.width >= 320)
838                         clockdiv = min_clockdiv;
839                 else if (clockdiv < 2)
840                         clockdiv = 2;
841
842                 if (sd->cam_type == CAM_TYPE_VGA && clockdiv < 4)
843                         clockdiv = 4;
844
845                 /* Frame exposure time in ms = 1000 * clockdiv / 60 ->
846                 exposure = (sd->exposure / 8) * 511 / (1000 * clockdiv / 60) */
847                 exposure = (60 * 511 * expo) / (8000 * clockdiv);
848                 if (exposure > 511)
849                         exposure = 511;
850
851                 /* exposure register value is reversed! */
852                 exposure = 511 - exposure;
853
854                 buf[0] = exposure & 0xff;
855                 buf[1] = exposure >> 8;
856                 sensor_write_reg(gspca_dev, 0x0e, 0, buf, 2);
857                 sensor_write1(gspca_dev, 0x02, clockdiv);
858         }
859 }
860
861 static void setgain(struct gspca_dev *gspca_dev, s32 val)
862 {
863         struct sd *sd = (struct sd *) gspca_dev;
864         u8 gainreg;
865
866         if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1)
867                 sensor_write1(gspca_dev, 0x0e, val);
868         else if (sd->cam_type == CAM_TYPE_VGA && sd->sensor_type == 2)
869                 for (gainreg = 0x0a; gainreg < 0x11; gainreg += 2) {
870                         sensor_write1(gspca_dev, gainreg, val >> 8);
871                         sensor_write1(gspca_dev, gainreg + 1, val & 0xff);
872                 }
873         else
874                 sensor_write1(gspca_dev, 0x10, val);
875 }
876
877 static void setcontrast(struct gspca_dev *gspca_dev, s32 val)
878 {
879         sensor_write1(gspca_dev, 0x1c, val);
880 }
881
882 static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
883 {
884         struct gspca_dev *gspca_dev =
885                 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
886         struct sd *sd = (struct sd *)gspca_dev;
887
888         gspca_dev->usb_err = 0;
889
890         if (!gspca_dev->streaming)
891                 return 0;
892
893         switch (ctrl->id) {
894         case V4L2_CID_BRIGHTNESS:
895                 setbrightness(gspca_dev, ctrl->val);
896                 break;
897         case V4L2_CID_CONTRAST:
898                 setcontrast(gspca_dev, ctrl->val);
899                 break;
900         case V4L2_CID_EXPOSURE:
901                 setexposure(gspca_dev, sd->exposure->val,
902                             sd->min_clockdiv ? sd->min_clockdiv->val : 0);
903                 break;
904         case V4L2_CID_GAIN:
905                 setgain(gspca_dev, ctrl->val);
906                 break;
907         }
908         return gspca_dev->usb_err;
909 }
910
911 static const struct v4l2_ctrl_ops sd_ctrl_ops = {
912         .s_ctrl = sd_s_ctrl,
913 };
914
915 static int sd_init_controls(struct gspca_dev *gspca_dev)
916 {
917         struct sd *sd = (struct sd *)gspca_dev;
918         struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
919         static const struct v4l2_ctrl_config clockdiv = {
920                 .ops = &sd_ctrl_ops,
921                 .id = MR97310A_CID_CLOCKDIV,
922                 .type = V4L2_CTRL_TYPE_INTEGER,
923                 .name = "Minimum Clock Divider",
924                 .min = MR97310A_MIN_CLOCKDIV_MIN,
925                 .max = MR97310A_MIN_CLOCKDIV_MAX,
926                 .step = 1,
927                 .def = MR97310A_MIN_CLOCKDIV_DEFAULT,
928         };
929         bool has_brightness = false;
930         bool has_argus_brightness = false;
931         bool has_contrast = false;
932         bool has_gain = false;
933         bool has_cs_gain = false;
934         bool has_exposure = false;
935         bool has_clockdiv = false;
936
937         gspca_dev->vdev.ctrl_handler = hdl;
938         v4l2_ctrl_handler_init(hdl, 4);
939
940         /* Setup controls depending on camera type */
941         if (sd->cam_type == CAM_TYPE_CIF) {
942                 /* No brightness for sensor_type 0 */
943                 if (sd->sensor_type == 0)
944                         has_exposure = has_gain = has_clockdiv = true;
945                 else
946                         has_exposure = has_gain = has_brightness = true;
947         } else {
948                 /* All controls need to be disabled if VGA sensor_type is 0 */
949                 if (sd->sensor_type == 0)
950                         ; /* no controls! */
951                 else if (sd->sensor_type == 2)
952                         has_exposure = has_cs_gain = has_contrast = true;
953                 else if (sd->do_lcd_stop)
954                         has_exposure = has_gain = has_argus_brightness =
955                                 has_clockdiv = true;
956                 else
957                         has_exposure = has_gain = has_brightness =
958                                 has_clockdiv = true;
959         }
960
961         /* Separate brightness control description for Argus QuickClix as it has
962          * different limits from the other mr97310a cameras, and separate gain
963          * control for Sakar CyberPix camera. */
964         /*
965          * This control is disabled for CIF type 1 and VGA type 0 cameras.
966          * It does not quite act linearly for the Argus QuickClix camera,
967          * but it does control brightness. The values are 0 - 15 only, and
968          * the table above makes them act consecutively.
969          */
970         if (has_brightness)
971                 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
972                         V4L2_CID_BRIGHTNESS, -254, 255, 1,
973                         MR97310A_BRIGHTNESS_DEFAULT);
974         else if (has_argus_brightness)
975                 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
976                         V4L2_CID_BRIGHTNESS, 0, 15, 1,
977                         MR97310A_BRIGHTNESS_DEFAULT);
978         if (has_contrast)
979                 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
980                         V4L2_CID_CONTRAST, MR97310A_CONTRAST_MIN,
981                         MR97310A_CONTRAST_MAX, 1, MR97310A_CONTRAST_DEFAULT);
982         if (has_gain)
983                 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
984                         V4L2_CID_GAIN, MR97310A_GAIN_MIN, MR97310A_GAIN_MAX,
985                         1, MR97310A_GAIN_DEFAULT);
986         else if (has_cs_gain)
987                 v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, V4L2_CID_GAIN,
988                         MR97310A_CS_GAIN_MIN, MR97310A_CS_GAIN_MAX,
989                         1, MR97310A_CS_GAIN_DEFAULT);
990         if (has_exposure)
991                 sd->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
992                         V4L2_CID_EXPOSURE, MR97310A_EXPOSURE_MIN,
993                         MR97310A_EXPOSURE_MAX, 1, MR97310A_EXPOSURE_DEFAULT);
994         if (has_clockdiv)
995                 sd->min_clockdiv = v4l2_ctrl_new_custom(hdl, &clockdiv, NULL);
996
997         if (hdl->error) {
998                 pr_err("Could not initialize controls\n");
999                 return hdl->error;
1000         }
1001         if (has_exposure && has_clockdiv)
1002                 v4l2_ctrl_cluster(2, &sd->exposure);
1003         return 0;
1004 }
1005
1006 /* Include pac common sof detection functions */
1007 #include "pac_common.h"
1008
1009 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1010                         u8 *data,               /* isoc packet */
1011                         int len)                /* iso packet length */
1012 {
1013         struct sd *sd = (struct sd *) gspca_dev;
1014         unsigned char *sof;
1015
1016         sof = pac_find_sof(gspca_dev, &sd->sof_read, data, len);
1017         if (sof) {
1018                 int n;
1019
1020                 /* finish decoding current frame */
1021                 n = sof - data;
1022                 if (n > sizeof pac_sof_marker)
1023                         n -= sizeof pac_sof_marker;
1024                 else
1025                         n = 0;
1026                 gspca_frame_add(gspca_dev, LAST_PACKET,
1027                                         data, n);
1028                 /* Start next frame. */
1029                 gspca_frame_add(gspca_dev, FIRST_PACKET,
1030                         pac_sof_marker, sizeof pac_sof_marker);
1031                 len -= sof - data;
1032                 data = sof;
1033         }
1034         gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
1035 }
1036
1037 /* sub-driver description */
1038 static const struct sd_desc sd_desc = {
1039         .name = MODULE_NAME,
1040         .config = sd_config,
1041         .init = sd_init,
1042         .init_controls = sd_init_controls,
1043         .start = sd_start,
1044         .stopN = sd_stopN,
1045         .pkt_scan = sd_pkt_scan,
1046 };
1047
1048 /* -- module initialisation -- */
1049 static const struct usb_device_id device_table[] = {
1050         {USB_DEVICE(0x08ca, 0x0110)},   /* Trust Spyc@m 100 */
1051         {USB_DEVICE(0x08ca, 0x0111)},   /* Aiptek Pencam VGA+ */
1052         {USB_DEVICE(0x093a, 0x010f)},   /* All other known MR97310A VGA cams */
1053         {USB_DEVICE(0x093a, 0x010e)},   /* All known MR97310A CIF cams */
1054         {}
1055 };
1056 MODULE_DEVICE_TABLE(usb, device_table);
1057
1058 /* -- device connect -- */
1059 static int sd_probe(struct usb_interface *intf,
1060                     const struct usb_device_id *id)
1061 {
1062         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1063                                THIS_MODULE);
1064 }
1065
1066 static struct usb_driver sd_driver = {
1067         .name = MODULE_NAME,
1068         .id_table = device_table,
1069         .probe = sd_probe,
1070         .disconnect = gspca_disconnect,
1071 #ifdef CONFIG_PM
1072         .suspend = gspca_suspend,
1073         .resume = gspca_resume,
1074         .reset_resume = gspca_resume,
1075 #endif
1076 };
1077
1078 module_usb_driver(sd_driver);