V4L/DVB (12619): gspca: mr97310a fix detection of sensortype for vivicam with id...
[linux-2.6-block.git] / drivers / media / video / gspca / mr97310a.c
CommitLineData
d661e622
KG
1/*
2 * Mars MR97310A library
3 *
4 * Copyright (C) 2009 Kyle Guinn <elyk03@gmail.com>
5 *
89f0863c
TK
6 * Support for the MR97310A cameras in addition to the Aiptek Pencam VGA+
7 * and for the routines for detecting and classifying these various cameras,
8 *
9 * Copyright (C) 2009 Theodore Kilgore <kilgota@auburn.edu>
10 *
11 * Acknowledgements:
12 *
13 * The MR97311A support in gspca/mars.c has been helpful in understanding some
14 * of the registers in these cameras.
15 *
16 * Hans de Goede <hdgoede@redhat.com> and
17 * Thomas Kaiser <thomas@kaiser-linux.li>
18 * have assisted with their experience. Each of them has also helped by
19 * testing a previously unsupported camera.
20 *
d661e622
KG
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation; either version 2 of the License, or
24 * any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 */
35
36#define MODULE_NAME "mr97310a"
37
38#include "gspca.h"
39
89f0863c
TK
40#define CAM_TYPE_CIF 0
41#define CAM_TYPE_VGA 1
42
43#define MR97310A_BRIGHTNESS_MIN -254
44#define MR97310A_BRIGHTNESS_MAX 255
45#define MR97310A_BRIGHTNESS_DEFAULT 0
46
47#define MR97310A_EXPOSURE_MIN 300
48#define MR97310A_EXPOSURE_MAX 4095
49#define MR97310A_EXPOSURE_DEFAULT 1000
50
51#define MR97310A_GAIN_MIN 0
52#define MR97310A_GAIN_MAX 31
53#define MR97310A_GAIN_DEFAULT 25
54
55MODULE_AUTHOR("Kyle Guinn <elyk03@gmail.com>,"
56 "Theodore Kilgore <kilgota@auburn.edu>");
d661e622
KG
57MODULE_DESCRIPTION("GSPCA/Mars-Semi MR97310A USB Camera Driver");
58MODULE_LICENSE("GPL");
59
60/* specific webcam descriptor */
61struct sd {
62 struct gspca_dev gspca_dev; /* !! must be the first item */
d661e622 63 u8 sof_read;
89f0863c
TK
64 u8 cam_type; /* 0 is CIF and 1 is VGA */
65 u8 sensor_type; /* We use 0 and 1 here, too. */
66 u8 do_lcd_stop;
67 u8 regs[15];
68
69 int brightness;
70 u16 exposure;
71 u8 autogain;
72 u8 gain;
d661e622
KG
73};
74
89f0863c
TK
75struct sensor_w_data {
76 u8 reg;
77 u8 flags;
78 u8 data[16];
79 int len;
80};
81
82static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
83static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
84static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
85static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
86static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
87static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
88
d661e622
KG
89/* V4L2 controls supported by the driver */
90static struct ctrl sd_ctrls[] = {
89f0863c
TK
91 {
92 {
93 .id = V4L2_CID_BRIGHTNESS,
94 .type = V4L2_CTRL_TYPE_INTEGER,
95 .name = "Brightness",
96 .minimum = MR97310A_BRIGHTNESS_MIN,
97 .maximum = MR97310A_BRIGHTNESS_MAX,
98 .step = 1,
99 .default_value = MR97310A_BRIGHTNESS_DEFAULT,
100 .flags = 0,
101 },
102 .set = sd_setbrightness,
103 .get = sd_getbrightness,
104 },
105 {
106 {
107 .id = V4L2_CID_EXPOSURE,
108 .type = V4L2_CTRL_TYPE_INTEGER,
109 .name = "Exposure",
110 .minimum = MR97310A_EXPOSURE_MIN,
111 .maximum = MR97310A_EXPOSURE_MAX,
112 .step = 1,
113 .default_value = MR97310A_EXPOSURE_DEFAULT,
114 .flags = 0,
115 },
116 .set = sd_setexposure,
117 .get = sd_getexposure,
118 },
119 {
120 {
121 .id = V4L2_CID_GAIN,
122 .type = V4L2_CTRL_TYPE_INTEGER,
123 .name = "Gain",
124 .minimum = MR97310A_GAIN_MIN,
125 .maximum = MR97310A_GAIN_MAX,
126 .step = 1,
127 .default_value = MR97310A_GAIN_DEFAULT,
128 .flags = 0,
129 },
130 .set = sd_setgain,
131 .get = sd_getgain,
132 },
d661e622
KG
133};
134
135static const struct v4l2_pix_format vga_mode[] = {
136 {160, 120, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
137 .bytesperline = 160,
138 .sizeimage = 160 * 120,
139 .colorspace = V4L2_COLORSPACE_SRGB,
140 .priv = 4},
141 {176, 144, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
142 .bytesperline = 176,
143 .sizeimage = 176 * 144,
144 .colorspace = V4L2_COLORSPACE_SRGB,
145 .priv = 3},
146 {320, 240, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
147 .bytesperline = 320,
148 .sizeimage = 320 * 240,
149 .colorspace = V4L2_COLORSPACE_SRGB,
150 .priv = 2},
151 {352, 288, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
152 .bytesperline = 352,
153 .sizeimage = 352 * 288,
154 .colorspace = V4L2_COLORSPACE_SRGB,
155 .priv = 1},
156 {640, 480, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
157 .bytesperline = 640,
158 .sizeimage = 640 * 480,
159 .colorspace = V4L2_COLORSPACE_SRGB,
160 .priv = 0},
161};
162
163/* the bytes to write are in gspca_dev->usb_buf */
89f0863c 164static int mr_write(struct gspca_dev *gspca_dev, int len)
d661e622
KG
165{
166 int rc;
167
168 rc = usb_bulk_msg(gspca_dev->dev,
169 usb_sndbulkpipe(gspca_dev->dev, 4),
92e8c91b 170 gspca_dev->usb_buf, len, NULL, 500);
d661e622
KG
171 if (rc < 0)
172 PDEBUG(D_ERR, "reg write [%02x] error %d",
173 gspca_dev->usb_buf[0], rc);
174 return rc;
175}
176
89f0863c
TK
177/* the bytes are read into gspca_dev->usb_buf */
178static int mr_read(struct gspca_dev *gspca_dev, int len)
179{
180 int rc;
181
182 rc = usb_bulk_msg(gspca_dev->dev,
183 usb_rcvbulkpipe(gspca_dev->dev, 3),
184 gspca_dev->usb_buf, len, NULL, 500);
185 if (rc < 0)
186 PDEBUG(D_ERR, "reg read [%02x] error %d",
187 gspca_dev->usb_buf[0], rc);
188 return rc;
189}
190
191static int sensor_write_reg(struct gspca_dev *gspca_dev, u8 reg, u8 flags,
192 const u8 *data, int len)
193{
194 gspca_dev->usb_buf[0] = 0x1f;
195 gspca_dev->usb_buf[1] = flags;
196 gspca_dev->usb_buf[2] = reg;
197 memcpy(gspca_dev->usb_buf + 3, data, len);
198
199 return mr_write(gspca_dev, len + 3);
200}
201
202static int sensor_write_regs(struct gspca_dev *gspca_dev,
203 const struct sensor_w_data *data, int len)
204{
205 int i, rc;
206
207 for (i = 0; i < len; i++) {
208 rc = sensor_write_reg(gspca_dev, data[i].reg, data[i].flags,
209 data[i].data, data[i].len);
210 if (rc < 0)
211 return rc;
212 }
213
214 return 0;
215}
216
217static int sensor_write1(struct gspca_dev *gspca_dev, u8 reg, u8 data)
218{
219 u8 buf;
220 int rc;
221
222 buf = data;
223 rc = sensor_write_reg(gspca_dev, reg, 0x01, &buf, 1);
224 if (rc < 0)
225 return rc;
226
227 buf = 0x01;
228 rc = sensor_write_reg(gspca_dev, 0x13, 0x00, &buf, 1);
229 if (rc < 0)
230 return rc;
231
232 return 0;
233}
234
235static int cam_get_response16(struct gspca_dev *gspca_dev)
236{
237 __u8 *data = gspca_dev->usb_buf;
238 int err_code;
239
240 data[0] = 0x21;
241 err_code = mr_write(gspca_dev, 1);
242 if (err_code < 0)
243 return err_code;
244
245 err_code = mr_read(gspca_dev, 16);
246 return err_code;
247}
248
249static int zero_the_pointer(struct gspca_dev *gspca_dev)
250{
251 __u8 *data = gspca_dev->usb_buf;
252 int err_code;
253 u8 status = 0;
254 int tries = 0;
255
256 err_code = cam_get_response16(gspca_dev);
257 if (err_code < 0)
258 return err_code;
259
260 err_code = mr_write(gspca_dev, 1);
261 data[0] = 0x19;
262 data[1] = 0x51;
263 err_code = mr_write(gspca_dev, 2);
264 if (err_code < 0)
265 return err_code;
266
267 err_code = cam_get_response16(gspca_dev);
268 if (err_code < 0)
269 return err_code;
270
271 data[0] = 0x19;
272 data[1] = 0xba;
273 err_code = mr_write(gspca_dev, 2);
274 if (err_code < 0)
275 return err_code;
276
277 err_code = cam_get_response16(gspca_dev);
278 if (err_code < 0)
279 return err_code;
280
281 data[0] = 0x19;
282 data[1] = 0x00;
283 err_code = mr_write(gspca_dev, 2);
284 if (err_code < 0)
285 return err_code;
286
287 err_code = cam_get_response16(gspca_dev);
288 if (err_code < 0)
289 return err_code;
290
291 data[0] = 0x19;
292 data[1] = 0x00;
293 err_code = mr_write(gspca_dev, 2);
294 if (err_code < 0)
295 return err_code;
296
297 while (status != 0x0a && tries < 256) {
298 err_code = cam_get_response16(gspca_dev);
299 status = data[0];
300 tries++;
301 if (err_code < 0)
302 return err_code;
303 }
304 PDEBUG(D_ERR, "status is %02x", status);
305
306 tries = 0;
307 while (tries < 4) {
308 data[0] = 0x19;
309 data[1] = 0x00;
310 err_code = mr_write(gspca_dev, 2);
311 if (err_code < 0)
312 return err_code;
313
314 err_code = cam_get_response16(gspca_dev);
315 status = data[0];
316 tries++;
317 if (err_code < 0)
318 return err_code;
319 }
320 PDEBUG(D_ERR, "Read 16 bytes from camera");
321
322 data[0] = 0x19;
323 err_code = mr_write(gspca_dev, 1);
324 if (err_code < 0)
325 return err_code;
326
327 err_code = mr_read(gspca_dev, 16);
328 if (err_code < 0)
329 return err_code;
330
331 return 0;
332}
333
334static u8 get_sensor_id(struct gspca_dev *gspca_dev)
335{
336 int err_code;
337
338 gspca_dev->usb_buf[0] = 0x1e;
339 err_code = mr_write(gspca_dev, 1);
340 if (err_code < 0)
341 return err_code;
342
343 err_code = mr_read(gspca_dev, 16);
344 if (err_code < 0)
345 return err_code;
346
347 PDEBUG(D_ERR, "Read 16 bytes from camera");
348 PDEBUG(D_ERR, "Byte zero reported is %01x", gspca_dev->usb_buf[0]);
349
350 return gspca_dev->usb_buf[0];
351}
352
d661e622
KG
353/* this function is called at probe time */
354static int sd_config(struct gspca_dev *gspca_dev,
355 const struct usb_device_id *id)
356{
89f0863c 357 struct sd *sd = (struct sd *) gspca_dev;
d661e622
KG
358 struct cam *cam;
359
360 cam = &gspca_dev->cam;
361 cam->cam_mode = vga_mode;
362 cam->nmodes = ARRAY_SIZE(vga_mode);
89f0863c
TK
363 sd->cam_type = CAM_TYPE_VGA;
364 PDEBUG(D_PROBE,
365 "MR97310A camera detected"
366 " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
367 if (id->idProduct == 0x010e) {
368 cam->nmodes--;
369 sd->cam_type = CAM_TYPE_CIF;
370 }
d661e622
KG
371 return 0;
372}
373
374/* this function is called at probe and resume time */
375static int sd_init(struct gspca_dev *gspca_dev)
376{
377 return 0;
378}
379
89f0863c
TK
380static int adjust_cif_sensor(struct gspca_dev *gspca_dev)
381{
382 /*
383 * FIXME: The following sequence resets brightness, contrast, and
384 * related settings. Some of the values are adjustable, presumably
385 * based upon what is detected in the frames. Here, only some
386 * vaules are used which are compromises. When more is known about
387 * what is done here, this needs to be moved out to presently
388 * nonexistent functions which do controls. The same control messages
389 * do work for all of the CIF cameras.
390 */
391
392 const struct sensor_w_data cif_sensor1_adjust_data[] = {
393 {0x02, 0x01, {0x10, 0x12, 0x0a}, 3},
394 /* Last or possibly two last bytes adjustable, above. */
395 {0x13, 0x04, {0x01}, 1}, /* seems to mean "write" */
396 {0x05, 0x01, {0x22, 0x00, 0x81, 0x06}, 4},
397 /* Last or possibly two last bytes adjustable, above. */
398 {0x13, 0x04, {0x01}, 1},
399 {0x09, 0x02, {0x05, 0x00, 0x00, 0x05, 0x07, 0x16}, 6},
400 /* Last or possibly two last bytes adjustable, above. */
401 {0x13, 0x04, {0x01}, 1},
402 {0, 0, {0}, 0}
403 };
404
405 return sensor_write_regs(gspca_dev, cif_sensor1_adjust_data,
406 ARRAY_SIZE(cif_sensor1_adjust_data));
407}
408
409static int start_cif_cam(struct gspca_dev *gspca_dev)
d661e622
KG
410{
411 struct sd *sd = (struct sd *) gspca_dev;
412 __u8 *data = gspca_dev->usb_buf;
413 int err_code;
89f0863c
TK
414 const __u8 startup_string[] = {
415 0x00,
416 0x0d,
417 0x01,
418 0x00, /* Hsize/8 for 352 or 320 */
419 0x00, /* Vsize/4 for 288 or 240 */
420 0x13, /* or 0xbb, depends on sensor */
421 0x00, /* Hstart, depends on res. */
422 0x00, /* reserved ? */
423 0x00, /* Vstart, depends on res. and sensor */
424 0x50, /* 0x54 to get 176 or 160 */
425 0xc0
426 };
427
428 /* Note: Some of the above descriptions guessed from MR97113A driver */
429 sd->sensor_type = 0;
d661e622
KG
430 data[0] = 0x01;
431 data[1] = 0x01;
89f0863c 432 err_code = mr_write(gspca_dev, 2);
d661e622
KG
433 if (err_code < 0)
434 return err_code;
435
89f0863c
TK
436 msleep(200);
437 data[0] = get_sensor_id(gspca_dev);
438 /*
439 * Known CIF cameras. If you have another to report, please do
440 *
441 * Name byte just read sd->sensor_type
442 * reported by
443 * Sakar Spy-shot 0x28 T. Kilgore 0
444 * Innovage 0xf5 (unstable) T. Kilgore 0
445 * Vivitar Mini 0x53 H. De Goede 0
446 * Vivitar Mini 0x08 T. Kilgore 1
447 * Elta-Media 8212dc 0x23 T. Kaiser 1
448 * Philips dig. keych. 0x37 T. Kilgore 1
449 */
58d0b2f6
HG
450 if ((data[0] & 0x78) == 8 ||
451 ((data[0] & 0x2) == 0x2 && data[0] != 0x53))
89f0863c
TK
452 sd->sensor_type = 1;
453
454 PDEBUG(D_ERR, "Sensor type is %01x", sd->sensor_type);
455 memcpy(data, startup_string, 11);
456 if (sd->sensor_type)
457 data[5] = 0xbb;
d661e622
KG
458
459 switch (gspca_dev->width) {
460 case 160:
89f0863c 461 data[9] |= 0x04; /* reg 8, 2:1 scale down from 320 */
d661e622
KG
462 /* fall thru */
463 case 320:
d661e622 464 default:
89f0863c
TK
465 data[3] = 0x28; /* reg 2, H size/8 */
466 data[4] = 0x3c; /* reg 3, V size/4 */
467 data[6] = 0x14; /* reg 5, H start */
468 data[8] = 0x1a + sd->sensor_type; /* reg 7, V start */
d661e622 469 break;
d661e622 470 case 176:
89f0863c 471 data[9] |= 0x04; /* reg 8, 2:1 scale down from 352 */
d661e622
KG
472 /* fall thru */
473 case 352:
89f0863c
TK
474 data[3] = 0x2c; /* reg 2, H size/8 */
475 data[4] = 0x48; /* reg 3, V size/4 */
476 data[6] = 0x06; /* reg 5, H start */
477 data[8] = 0x06 + sd->sensor_type; /* reg 7, V start */
d661e622
KG
478 break;
479 }
89f0863c 480 err_code = mr_write(gspca_dev, 11);
d661e622
KG
481 if (err_code < 0)
482 return err_code;
483
89f0863c
TK
484 if (!sd->sensor_type) {
485 const struct sensor_w_data cif_sensor0_init_data[] = {
486 {0x02, 0x00, {0x03, 0x5a, 0xb5, 0x01,
487 0x0f, 0x14, 0x0f, 0x10}, 8},
488 {0x0c, 0x00, {0x04, 0x01, 0x01, 0x00, 0x1f}, 5},
489 {0x12, 0x00, {0x07}, 1},
490 {0x1f, 0x00, {0x06}, 1},
491 {0x27, 0x00, {0x04}, 1},
492 {0x29, 0x00, {0x0c}, 1},
493 {0x40, 0x00, {0x40, 0x00, 0x04}, 3},
494 {0x50, 0x00, {0x60}, 1},
495 {0x60, 0x00, {0x06}, 1},
496 {0x6b, 0x00, {0x85, 0x85, 0xc8, 0xc8, 0xc8, 0xc8}, 6},
497 {0x72, 0x00, {0x1e, 0x56}, 2},
498 {0x75, 0x00, {0x58, 0x40, 0xa2, 0x02, 0x31, 0x02,
499 0x31, 0x80, 0x00}, 9},
500 {0x11, 0x00, {0x01}, 1},
501 {0, 0, {0}, 0}
502 };
503 err_code = sensor_write_regs(gspca_dev, cif_sensor0_init_data,
504 ARRAY_SIZE(cif_sensor0_init_data));
505 } else { /* sd->sensor_type = 1 */
506 const struct sensor_w_data cif_sensor1_init_data[] = {
507 {0x02, 0x00, {0x10}, 1},
508 {0x03, 0x01, {0x12}, 1},
509 {0x04, 0x01, {0x05}, 1},
510 {0x05, 0x01, {0x65}, 1},
511 {0x06, 0x01, {0x32}, 1},
512 {0x07, 0x01, {0x00}, 1},
513 {0x08, 0x02, {0x06}, 1},
514 {0x09, 0x02, {0x0e}, 1},
515 {0x0a, 0x02, {0x05}, 1},
516 {0x0b, 0x02, {0x05}, 1},
517 {0x0c, 0x02, {0x0f}, 1},
518 {0x0d, 0x02, {0x00}, 1},
519 {0x0e, 0x02, {0x0c}, 1},
520 {0x0f, 0x00, {0x00}, 1},
521 {0x10, 0x00, {0x06}, 1},
522 {0x11, 0x00, {0x07}, 1},
523 {0x12, 0x00, {0x00}, 1},
524 {0x13, 0x00, {0x01}, 1},
525 {0, 0, {0}, 0}
526 };
527 err_code = sensor_write_regs(gspca_dev, cif_sensor1_init_data,
528 ARRAY_SIZE(cif_sensor1_init_data));
529 }
d661e622
KG
530 if (err_code < 0)
531 return err_code;
532
89f0863c
TK
533 msleep(200);
534 data[0] = 0x00;
535 data[1] = 0x4d; /* ISOC transfering enable... */
536 err_code = mr_write(gspca_dev, 2);
d661e622
KG
537 if (err_code < 0)
538 return err_code;
539
89f0863c
TK
540 msleep(200);
541 err_code = adjust_cif_sensor(gspca_dev);
d661e622
KG
542 if (err_code < 0)
543 return err_code;
544
89f0863c
TK
545 msleep(200);
546 return 0;
547}
d661e622 548
89f0863c
TK
549static int start_vga_cam(struct gspca_dev *gspca_dev)
550{
551 struct sd *sd = (struct sd *) gspca_dev;
552 __u8 *data = gspca_dev->usb_buf;
553 int err_code;
554 const __u8 startup_string[] = {0x00, 0x0d, 0x01, 0x00, 0x00, 0x2b,
555 0x00, 0x00, 0x00, 0x50, 0xc0};
d661e622 556
89f0863c
TK
557 /* What some of these mean is explained in start_cif_cam(), above */
558 sd->sof_read = 0;
d661e622 559
89f0863c
TK
560 /*
561 * We have to know which camera we have, because the register writes
562 * depend upon the camera. This test, run before we actually enter
563 * the initialization routine, distinguishes most of the cameras, If
564 * needed, another routine is done later, too.
565 */
566 memset(data, 0, 16);
567 data[0] = 0x20;
568 err_code = mr_write(gspca_dev, 1);
d661e622
KG
569 if (err_code < 0)
570 return err_code;
571
89f0863c 572 err_code = mr_read(gspca_dev, 16);
d661e622
KG
573 if (err_code < 0)
574 return err_code;
575
89f0863c
TK
576 PDEBUG(D_ERR, "Read 16 bytes from camera");
577 PDEBUG(D_ERR, "Byte reported is %02x", data[0]);
d661e622 578
89f0863c
TK
579 msleep(200);
580 /*
581 * Known VGA cameras. If you have another to report, please do
582 *
583 * Name byte just read sd->sensor_type
584 * sd->do_lcd_stop
585 * Aiptek Pencam VGA+ 0x31 0 1
586 * ION digital 0x31 0 1
587 * Argus DC-1620 0x30 1 0
588 * Argus QuickClix 0x30 1 1 (not caught here)
589 */
590 sd->sensor_type = data[0] & 1;
591 sd->do_lcd_stop = (~data[0]) & 1;
d661e622 592
89f0863c
TK
593
594
595 /* Streaming setup begins here. */
596
597
598 data[0] = 0x01;
599 data[1] = 0x01;
600 err_code = mr_write(gspca_dev, 2);
d661e622
KG
601 if (err_code < 0)
602 return err_code;
603
89f0863c
TK
604 /*
605 * A second test can now resolve any remaining ambiguity in the
606 * identification of the camera type,
607 */
608 if (!sd->sensor_type) {
609 data[0] = get_sensor_id(gspca_dev);
610 if (data[0] == 0x7f) {
611 sd->sensor_type = 1;
612 PDEBUG(D_ERR, "sensor_type corrected to 1");
613 }
614 msleep(200);
615 }
616
617 /*
618 * Known VGA cameras.
619 * This test is only run if the previous test returned 0x30, but
620 * here is the information for all others, too, just for reference.
621 *
622 * Name byte just read sd->sensor_type
623 *
624 * Aiptek Pencam VGA+ 0xfb (this test not run) 1
625 * ION digital 0xbd (this test not run) 1
626 * Argus DC-1620 0xe5 (no change) 0
627 * Argus QuickClix 0x7f (reclassified) 1
628 */
629 memcpy(data, startup_string, 11);
630 if (!sd->sensor_type) {
631 data[5] = 0x00;
632 data[10] = 0x91;
633 }
634
635 switch (gspca_dev->width) {
636 case 160:
637 data[9] |= 0x0c; /* reg 8, 4:1 scale down */
638 /* fall thru */
639 case 320:
640 data[9] |= 0x04; /* reg 8, 2:1 scale down */
641 /* fall thru */
642 case 640:
643 default:
644 data[3] = 0x50; /* reg 2, H size/8 */
645 data[4] = 0x78; /* reg 3, V size/4 */
646 data[6] = 0x04; /* reg 5, H start */
647 data[8] = 0x03; /* reg 7, V start */
648 if (sd->do_lcd_stop)
649 data[8] = 0x04; /* Bayer tile shifted */
650 break;
651
652 case 176:
653 data[9] |= 0x04; /* reg 8, 2:1 scale down */
654 /* fall thru */
655 case 352:
656 data[3] = 0x2c; /* reg 2, H size */
657 data[4] = 0x48; /* reg 3, V size */
658 data[6] = 0x94; /* reg 5, H start */
659 data[8] = 0x63; /* reg 7, V start */
660 if (sd->do_lcd_stop)
661 data[8] = 0x64; /* Bayer tile shifted */
662 break;
663 }
664
665 err_code = mr_write(gspca_dev, 11);
d661e622
KG
666 if (err_code < 0)
667 return err_code;
668
89f0863c
TK
669 if (!sd->sensor_type) {
670 /* The only known sensor_type 0 cam is the Argus DC-1620 */
671 const struct sensor_w_data vga_sensor0_init_data[] = {
672 {0x01, 0x00, {0x0c, 0x00, 0x04}, 3},
673 {0x14, 0x00, {0x01, 0xe4, 0x02, 0x84}, 4},
674 {0x20, 0x00, {0x00, 0x80, 0x00, 0x08}, 4},
675 {0x25, 0x00, {0x03, 0xa9, 0x80}, 3},
676 {0x30, 0x00, {0x30, 0x18, 0x10, 0x18}, 4},
677 {0, 0, {0}, 0}
678 };
679 err_code = sensor_write_regs(gspca_dev, vga_sensor0_init_data,
680 ARRAY_SIZE(vga_sensor0_init_data));
681 } else { /* sd->sensor_type = 1 */
682 const struct sensor_w_data vga_sensor1_init_data[] = {
683 {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
684 0x07, 0x00, 0x01}, 8},
685 {0x11, 0x04, {0x01}, 1},
686 /*{0x0a, 0x00, {0x00, 0x01, 0x00, 0x00, 0x01, */
687 {0x0a, 0x00, {0x01, 0x06, 0x00, 0x00, 0x01,
688 0x00, 0x0a}, 7},
689 {0x11, 0x04, {0x01}, 1},
690 {0x12, 0x00, {0x00, 0x63, 0x00, 0x70, 0x00, 0x00}, 6},
691 {0x11, 0x04, {0x01}, 1},
692 {0, 0, {0}, 0}
693 };
694 err_code = sensor_write_regs(gspca_dev, vga_sensor1_init_data,
695 ARRAY_SIZE(vga_sensor1_init_data));
696 }
d661e622
KG
697 if (err_code < 0)
698 return err_code;
699
89f0863c 700 msleep(200);
d661e622
KG
701 data[0] = 0x00;
702 data[1] = 0x4d; /* ISOC transfering enable... */
89f0863c
TK
703 err_code = mr_write(gspca_dev, 2);
704
705 return err_code;
706}
707
708static int sd_start(struct gspca_dev *gspca_dev)
709{
710 struct sd *sd = (struct sd *) gspca_dev;
711 int err_code;
712 struct cam *cam;
713
714 /* TEST TEST */
715 int i;
716 for (i = 2; i <= 14; i++)
717 sd->regs[i] = sd_ctrls[i - 2].qctrl.default_value;
718
719 cam = &gspca_dev->cam;
720 sd->sof_read = 0;
721 /*
722 * Some of the supported cameras require the memory pointer to be
723 * set to 0, or else they will not stream.
724 */
725 zero_the_pointer(gspca_dev);
726 msleep(200);
727 if (sd->cam_type == CAM_TYPE_CIF) {
728 PDEBUG(D_ERR, "CIF camera");
729 err_code = start_cif_cam(gspca_dev);
730 } else {
731 PDEBUG(D_ERR, "VGA camera");
732 err_code = start_vga_cam(gspca_dev);
733 }
d661e622
KG
734 return err_code;
735}
736
737static void sd_stopN(struct gspca_dev *gspca_dev)
738{
89f0863c 739 struct sd *sd = (struct sd *) gspca_dev;
d661e622
KG
740 int result;
741
742 gspca_dev->usb_buf[0] = 1;
743 gspca_dev->usb_buf[1] = 0;
89f0863c 744 result = mr_write(gspca_dev, 2);
d661e622
KG
745 if (result < 0)
746 PDEBUG(D_ERR, "Camera Stop failed");
89f0863c
TK
747
748 /* Not all the cams need this, but even if not, probably a good idea */
749 zero_the_pointer(gspca_dev);
750 if (sd->do_lcd_stop) {
751 gspca_dev->usb_buf[0] = 0x19;
752 gspca_dev->usb_buf[1] = 0x54;
753 result = mr_write(gspca_dev, 2);
754 if (result < 0)
755 PDEBUG(D_ERR, "Camera Stop failed");
756 }
757}
758
759static void setbrightness(struct gspca_dev *gspca_dev)
760{
761 struct sd *sd = (struct sd *) gspca_dev;
762 u8 val;
763 if (sd->brightness > 0) {
764 sensor_write1(gspca_dev, 7, 0);
765 val = sd->brightness;
766 } else {
767 sensor_write1(gspca_dev, 7, 1);
768 val = 257 - sd->brightness;
769 }
770 sensor_write1(gspca_dev, 8, val);
771}
772
773static void setexposure(struct gspca_dev *gspca_dev)
774{
775 struct sd *sd = (struct sd *) gspca_dev;
776 u8 val;
777
778 val = sd->exposure >> 4;
779 sensor_write1(gspca_dev, 3, val);
780 val = sd->exposure & 0xf;
781 sensor_write1(gspca_dev, 4, val);
782}
783
784static void setgain(struct gspca_dev *gspca_dev)
785{
786 struct sd *sd = (struct sd *) gspca_dev;
787
788 sensor_write1(gspca_dev, 3, sd->gain);
789}
790
791static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
792{
793 struct sd *sd = (struct sd *) gspca_dev;
794
795 sd->brightness = val;
796 if (gspca_dev->streaming)
797 setbrightness(gspca_dev);
798 return 0;
799}
800
801static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
802{
803 struct sd *sd = (struct sd *) gspca_dev;
804
805 *val = sd->brightness;
806 return 0;
807}
808
809static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
810{
811 struct sd *sd = (struct sd *) gspca_dev;
812
813 sd->exposure = val;
814 if (gspca_dev->streaming)
815 setexposure(gspca_dev);
816 return 0;
817}
818
819static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
820{
821 struct sd *sd = (struct sd *) gspca_dev;
822
823 *val = sd->exposure;
824 return 0;
825}
826
827static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
828{
829 struct sd *sd = (struct sd *) gspca_dev;
830
831 sd->gain = val;
832 if (gspca_dev->streaming)
833 setgain(gspca_dev);
834 return 0;
835}
836
837static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
838{
839 struct sd *sd = (struct sd *) gspca_dev;
840
841 *val = sd->gain;
842 return 0;
d661e622
KG
843}
844
845/* Include pac common sof detection functions */
846#include "pac_common.h"
847
848static void sd_pkt_scan(struct gspca_dev *gspca_dev,
849 struct gspca_frame *frame, /* target */
850 __u8 *data, /* isoc packet */
851 int len) /* iso packet length */
852{
d661e622
KG
853 unsigned char *sof;
854
855 sof = pac_find_sof(gspca_dev, data, len);
856 if (sof) {
857 int n;
858
859 /* finish decoding current frame */
860 n = sof - data;
861 if (n > sizeof pac_sof_marker)
862 n -= sizeof pac_sof_marker;
863 else
864 n = 0;
865 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
866 data, n);
9832d765
TK
867 /* Start next frame. */
868 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
869 pac_sof_marker, sizeof pac_sof_marker);
d661e622
KG
870 len -= sof - data;
871 data = sof;
872 }
d661e622
KG
873 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
874}
875
876/* sub-driver description */
877static const struct sd_desc sd_desc = {
878 .name = MODULE_NAME,
879 .ctrls = sd_ctrls,
880 .nctrls = ARRAY_SIZE(sd_ctrls),
881 .config = sd_config,
882 .init = sd_init,
883 .start = sd_start,
884 .stopN = sd_stopN,
885 .pkt_scan = sd_pkt_scan,
886};
887
888/* -- module initialisation -- */
889static const __devinitdata struct usb_device_id device_table[] = {
89f0863c
TK
890 {USB_DEVICE(0x08ca, 0x0111)}, /* Aiptek Pencam VGA+ */
891 {USB_DEVICE(0x093a, 0x010f)}, /* All other known MR97310A VGA cams */
892 {USB_DEVICE(0x093a, 0x010e)}, /* All known MR97310A CIF cams */
d661e622
KG
893 {}
894};
895MODULE_DEVICE_TABLE(usb, device_table);
896
897/* -- device connect -- */
898static int sd_probe(struct usb_interface *intf,
899 const struct usb_device_id *id)
900{
901 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
902 THIS_MODULE);
903}
904
905static struct usb_driver sd_driver = {
906 .name = MODULE_NAME,
907 .id_table = device_table,
908 .probe = sd_probe,
909 .disconnect = gspca_disconnect,
910#ifdef CONFIG_PM
911 .suspend = gspca_suspend,
912 .resume = gspca_resume,
913#endif
914};
915
916/* -- module insert / remove -- */
917static int __init sd_mod_init(void)
918{
5d3fa30d
AK
919 int ret;
920
921 ret = usb_register(&sd_driver);
922 if (ret < 0)
923 return ret;
d661e622
KG
924 PDEBUG(D_PROBE, "registered");
925 return 0;
926}
927static void __exit sd_mod_exit(void)
928{
929 usb_deregister(&sd_driver);
930 PDEBUG(D_PROBE, "deregistered");
931}
932
933module_init(sd_mod_init);
934module_exit(sd_mod_exit);