V4L/DVB (13142): gspca_mr97310a: small tweak to CIF sensor type 1 exposure setting
[linux-2.6.git] / drivers / media / video / gspca / mr97310a.c
blobf282f8000701685454c078759e6077d3753268ea
1 /*
2 * Mars MR97310A library
4 * The original mr97310a driver, which supported the Aiptek Pencam VGA+, is
5 * Copyright (C) 2009 Kyle Guinn <elyk03@gmail.com>
7 * Support for the MR97310A cameras in addition to the Aiptek Pencam VGA+
8 * and for the routines for detecting and classifying these various cameras,
9 * is Copyright (C) 2009 Theodore Kilgore <kilgota@auburn.edu>
11 * Support for the control settings for the CIF cameras is
12 * Copyright (C) 2009 Hans de Goede <hdgoede@redhat.com> and
13 * Thomas Kaiser <thomas@kaiser-linux.li>
15 * Support for the control settings for the VGA cameras is
16 * Copyright (C) 2009 Theodore Kilgore <kilgota@auburn.edu>
18 * Several previously unsupported cameras are owned and have been tested by
19 * Hans de Goede <hdgoede@redhat.com> and
20 * Thomas Kaiser <thomas@kaiser-linux.li> and
21 * Theodore Kilgore <kilgota@auburn.edu>
23 * The MR97311A support in gspca/mars.c has been helpful in understanding some
24 * of the registers in these cameras.
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or
29 * any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program; if not, write to the Free Software
38 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41 #define MODULE_NAME "mr97310a"
43 #include "gspca.h"
45 #define CAM_TYPE_CIF 0
46 #define CAM_TYPE_VGA 1
48 #define MR97310A_BRIGHTNESS_DEFAULT 0
50 #define MR97310A_EXPOSURE_MIN 0
51 #define MR97310A_EXPOSURE_MAX 4095
52 #define MR97310A_EXPOSURE_DEFAULT 1000
54 #define MR97310A_GAIN_MIN 0
55 #define MR97310A_GAIN_MAX 31
56 #define MR97310A_GAIN_DEFAULT 25
58 MODULE_AUTHOR("Kyle Guinn <elyk03@gmail.com>,"
59 "Theodore Kilgore <kilgota@auburn.edu>");
60 MODULE_DESCRIPTION("GSPCA/Mars-Semi MR97310A USB Camera Driver");
61 MODULE_LICENSE("GPL");
63 /* global parameters */
64 int force_sensor_type = -1;
65 module_param(force_sensor_type, int, 0644);
66 MODULE_PARM_DESC(force_sensor_type, "Force sensor type (-1 (auto), 0 or 1)");
68 /* specific webcam descriptor */
69 struct sd {
70 struct gspca_dev gspca_dev; /* !! must be the first item */
71 u8 sof_read;
72 u8 cam_type; /* 0 is CIF and 1 is VGA */
73 u8 sensor_type; /* We use 0 and 1 here, too. */
74 u8 do_lcd_stop;
76 int brightness;
77 u16 exposure;
78 u8 gain;
81 struct sensor_w_data {
82 u8 reg;
83 u8 flags;
84 u8 data[16];
85 int len;
88 static void sd_stopN(struct gspca_dev *gspca_dev);
89 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
90 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
91 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
92 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
93 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
94 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
95 static void setbrightness(struct gspca_dev *gspca_dev);
96 static void setexposure(struct gspca_dev *gspca_dev);
97 static void setgain(struct gspca_dev *gspca_dev);
99 /* V4L2 controls supported by the driver */
100 static struct ctrl sd_ctrls[] = {
101 /* Seprate brightness control description for Argus QuickClix as it has
102 different limits from to other mr97310a camera's */
104 #define NORM_BRIGHTNESS_IDX 0
106 .id = V4L2_CID_BRIGHTNESS,
107 .type = V4L2_CTRL_TYPE_INTEGER,
108 .name = "Brightness",
109 .minimum = -254,
110 .maximum = 255,
111 .step = 1,
112 .default_value = MR97310A_BRIGHTNESS_DEFAULT,
113 .flags = 0,
115 .set = sd_setbrightness,
116 .get = sd_getbrightness,
119 #define ARGUS_QC_BRIGHTNESS_IDX 1
121 .id = V4L2_CID_BRIGHTNESS,
122 .type = V4L2_CTRL_TYPE_INTEGER,
123 .name = "Brightness",
124 .minimum = 0,
125 .maximum = 15,
126 .step = 1,
127 .default_value = MR97310A_BRIGHTNESS_DEFAULT,
128 .flags = 0,
130 .set = sd_setbrightness,
131 .get = sd_getbrightness,
134 #define EXPOSURE_IDX 2
136 .id = V4L2_CID_EXPOSURE,
137 .type = V4L2_CTRL_TYPE_INTEGER,
138 .name = "Exposure",
139 .minimum = MR97310A_EXPOSURE_MIN,
140 .maximum = MR97310A_EXPOSURE_MAX,
141 .step = 1,
142 .default_value = MR97310A_EXPOSURE_DEFAULT,
143 .flags = 0,
145 .set = sd_setexposure,
146 .get = sd_getexposure,
149 #define GAIN_IDX 3
151 .id = V4L2_CID_GAIN,
152 .type = V4L2_CTRL_TYPE_INTEGER,
153 .name = "Gain",
154 .minimum = MR97310A_GAIN_MIN,
155 .maximum = MR97310A_GAIN_MAX,
156 .step = 1,
157 .default_value = MR97310A_GAIN_DEFAULT,
158 .flags = 0,
160 .set = sd_setgain,
161 .get = sd_getgain,
165 static const struct v4l2_pix_format vga_mode[] = {
166 {160, 120, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
167 .bytesperline = 160,
168 .sizeimage = 160 * 120,
169 .colorspace = V4L2_COLORSPACE_SRGB,
170 .priv = 4},
171 {176, 144, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
172 .bytesperline = 176,
173 .sizeimage = 176 * 144,
174 .colorspace = V4L2_COLORSPACE_SRGB,
175 .priv = 3},
176 {320, 240, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
177 .bytesperline = 320,
178 .sizeimage = 320 * 240,
179 .colorspace = V4L2_COLORSPACE_SRGB,
180 .priv = 2},
181 {352, 288, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
182 .bytesperline = 352,
183 .sizeimage = 352 * 288,
184 .colorspace = V4L2_COLORSPACE_SRGB,
185 .priv = 1},
186 {640, 480, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
187 .bytesperline = 640,
188 .sizeimage = 640 * 480,
189 .colorspace = V4L2_COLORSPACE_SRGB,
190 .priv = 0},
193 /* the bytes to write are in gspca_dev->usb_buf */
194 static int mr_write(struct gspca_dev *gspca_dev, int len)
196 int rc;
198 rc = usb_bulk_msg(gspca_dev->dev,
199 usb_sndbulkpipe(gspca_dev->dev, 4),
200 gspca_dev->usb_buf, len, NULL, 500);
201 if (rc < 0)
202 PDEBUG(D_ERR, "reg write [%02x] error %d",
203 gspca_dev->usb_buf[0], rc);
204 return rc;
207 /* the bytes are read into gspca_dev->usb_buf */
208 static int mr_read(struct gspca_dev *gspca_dev, int len)
210 int rc;
212 rc = usb_bulk_msg(gspca_dev->dev,
213 usb_rcvbulkpipe(gspca_dev->dev, 3),
214 gspca_dev->usb_buf, len, NULL, 500);
215 if (rc < 0)
216 PDEBUG(D_ERR, "reg read [%02x] error %d",
217 gspca_dev->usb_buf[0], rc);
218 return rc;
221 static int sensor_write_reg(struct gspca_dev *gspca_dev, u8 reg, u8 flags,
222 const u8 *data, int len)
224 gspca_dev->usb_buf[0] = 0x1f;
225 gspca_dev->usb_buf[1] = flags;
226 gspca_dev->usb_buf[2] = reg;
227 memcpy(gspca_dev->usb_buf + 3, data, len);
229 return mr_write(gspca_dev, len + 3);
232 static int sensor_write_regs(struct gspca_dev *gspca_dev,
233 const struct sensor_w_data *data, int len)
235 int i, rc;
237 for (i = 0; i < len; i++) {
238 rc = sensor_write_reg(gspca_dev, data[i].reg, data[i].flags,
239 data[i].data, data[i].len);
240 if (rc < 0)
241 return rc;
244 return 0;
247 static int sensor_write1(struct gspca_dev *gspca_dev, u8 reg, u8 data)
249 struct sd *sd = (struct sd *) gspca_dev;
250 u8 buf, confirm_reg;
251 int rc;
253 buf = data;
254 if (sd->cam_type == CAM_TYPE_CIF) {
255 rc = sensor_write_reg(gspca_dev, reg, 0x01, &buf, 1);
256 confirm_reg = sd->sensor_type ? 0x13 : 0x11;
257 } else {
258 rc = sensor_write_reg(gspca_dev, reg, 0x00, &buf, 1);
259 confirm_reg = 0x11;
261 if (rc < 0)
262 return rc;
264 buf = 0x01;
265 rc = sensor_write_reg(gspca_dev, confirm_reg, 0x00, &buf, 1);
266 if (rc < 0)
267 return rc;
269 return 0;
272 static int cam_get_response16(struct gspca_dev *gspca_dev, u8 reg, int verbose)
274 int err_code;
276 gspca_dev->usb_buf[0] = reg;
277 err_code = mr_write(gspca_dev, 1);
278 if (err_code < 0)
279 return err_code;
281 err_code = mr_read(gspca_dev, 16);
282 if (err_code < 0)
283 return err_code;
285 if (verbose)
286 PDEBUG(D_PROBE, "Register: %02x reads %02x%02x%02x", reg,
287 gspca_dev->usb_buf[0],
288 gspca_dev->usb_buf[1],
289 gspca_dev->usb_buf[2]);
291 return 0;
294 static int zero_the_pointer(struct gspca_dev *gspca_dev)
296 __u8 *data = gspca_dev->usb_buf;
297 int err_code;
298 u8 status = 0;
299 int tries = 0;
301 err_code = cam_get_response16(gspca_dev, 0x21, 0);
302 if (err_code < 0)
303 return err_code;
305 err_code = mr_write(gspca_dev, 1);
306 data[0] = 0x19;
307 data[1] = 0x51;
308 err_code = mr_write(gspca_dev, 2);
309 if (err_code < 0)
310 return err_code;
312 err_code = cam_get_response16(gspca_dev, 0x21, 0);
313 if (err_code < 0)
314 return err_code;
316 data[0] = 0x19;
317 data[1] = 0xba;
318 err_code = mr_write(gspca_dev, 2);
319 if (err_code < 0)
320 return err_code;
322 err_code = cam_get_response16(gspca_dev, 0x21, 0);
323 if (err_code < 0)
324 return err_code;
326 data[0] = 0x19;
327 data[1] = 0x00;
328 err_code = mr_write(gspca_dev, 2);
329 if (err_code < 0)
330 return err_code;
332 err_code = cam_get_response16(gspca_dev, 0x21, 0);
333 if (err_code < 0)
334 return err_code;
336 data[0] = 0x19;
337 data[1] = 0x00;
338 err_code = mr_write(gspca_dev, 2);
339 if (err_code < 0)
340 return err_code;
342 while (status != 0x0a && tries < 256) {
343 err_code = cam_get_response16(gspca_dev, 0x21, 0);
344 status = data[0];
345 tries++;
346 if (err_code < 0)
347 return err_code;
349 if (status != 0x0a)
350 PDEBUG(D_ERR, "status is %02x", status);
352 tries = 0;
353 while (tries < 4) {
354 data[0] = 0x19;
355 data[1] = 0x00;
356 err_code = mr_write(gspca_dev, 2);
357 if (err_code < 0)
358 return err_code;
360 err_code = cam_get_response16(gspca_dev, 0x21, 0);
361 status = data[0];
362 tries++;
363 if (err_code < 0)
364 return err_code;
367 data[0] = 0x19;
368 err_code = mr_write(gspca_dev, 1);
369 if (err_code < 0)
370 return err_code;
372 err_code = mr_read(gspca_dev, 16);
373 if (err_code < 0)
374 return err_code;
376 return 0;
379 static int stream_start(struct gspca_dev *gspca_dev)
381 gspca_dev->usb_buf[0] = 0x01;
382 gspca_dev->usb_buf[1] = 0x01;
383 return mr_write(gspca_dev, 2);
386 static void stream_stop(struct gspca_dev *gspca_dev)
388 gspca_dev->usb_buf[0] = 0x01;
389 gspca_dev->usb_buf[1] = 0x00;
390 if (mr_write(gspca_dev, 2) < 0)
391 PDEBUG(D_ERR, "Stream Stop failed");
394 static void lcd_stop(struct gspca_dev *gspca_dev)
396 gspca_dev->usb_buf[0] = 0x19;
397 gspca_dev->usb_buf[1] = 0x54;
398 if (mr_write(gspca_dev, 2) < 0)
399 PDEBUG(D_ERR, "LCD Stop failed");
402 static int isoc_enable(struct gspca_dev *gspca_dev)
404 gspca_dev->usb_buf[0] = 0x00;
405 gspca_dev->usb_buf[1] = 0x4d; /* ISOC transfering enable... */
406 return mr_write(gspca_dev, 2);
409 /* this function is called at probe time */
410 static int sd_config(struct gspca_dev *gspca_dev,
411 const struct usb_device_id *id)
413 struct sd *sd = (struct sd *) gspca_dev;
414 struct cam *cam;
415 int err_code;
417 cam = &gspca_dev->cam;
418 cam->cam_mode = vga_mode;
419 cam->nmodes = ARRAY_SIZE(vga_mode);
420 sd->do_lcd_stop = 0;
422 /* Now, logical layout of the driver must fall sacrifice to the
423 * realities of the hardware supported. We have to sort out several
424 * cameras which share the USB ID but are in fact different inside.
425 * We need to start the initialization process for the cameras in
426 * order to classify them. Some of the supported cameras require the
427 * memory pointer to be set to 0 as the very first item of business
428 * or else they will not stream. So we do that immediately.
430 err_code = zero_the_pointer(gspca_dev);
431 if (err_code < 0)
432 return err_code;
434 err_code = stream_start(gspca_dev);
435 if (err_code < 0)
436 return err_code;
438 if (id->idProduct == 0x010e) {
439 sd->cam_type = CAM_TYPE_CIF;
440 cam->nmodes--;
441 err_code = cam_get_response16(gspca_dev, 0x06, 1);
442 if (err_code < 0)
443 return err_code;
445 * The various CIF cameras share the same USB ID but use
446 * different init routines and different controls. We need to
447 * detect which one is connected!
449 * A list of known CIF cameras follows. They all report either
450 * 0002 for type 0 or 0003 for type 1.
451 * If you have another to report, please do
453 * Name sd->sensor_type reported by
455 * Sakar Spy-shot 0 T. Kilgore
456 * Innovage 0 T. Kilgore
457 * Vivitar Mini 0 H. De Goede
458 * Vivitar Mini 0 E. Rodriguez
459 * Vivitar Mini 1 T. Kilgore
460 * Elta-Media 8212dc 1 T. Kaiser
461 * Philips dig. keych. 1 T. Kilgore
463 switch (gspca_dev->usb_buf[1]) {
464 case 2:
465 sd->sensor_type = 0;
466 break;
467 case 3:
468 sd->sensor_type = 1;
469 break;
470 default:
471 PDEBUG(D_ERR, "Unknown CIF Sensor id : %02x",
472 gspca_dev->usb_buf[1]);
473 return -ENODEV;
475 PDEBUG(D_PROBE, "MR97310A CIF camera detected, sensor: %d",
476 sd->sensor_type);
477 } else {
478 sd->cam_type = CAM_TYPE_VGA;
480 err_code = cam_get_response16(gspca_dev, 0x07, 1);
481 if (err_code < 0)
482 return err_code;
485 * Here is a table of the responses to the previous command
486 * from the known MR97310A VGA cameras.
488 * Name gspca_dev->usb_buf[] sd->sensor_type
489 * sd->do_lcd_stop
490 * Aiptek Pencam VGA+ 0300 0 1
491 * ION digital 0350 0 1
492 * Argus DC-1620 0450 1 0
493 * Argus QuickClix 0420 1 1
495 * Based upon these results, we assume default settings
496 * and then correct as necessary, as follows.
500 sd->sensor_type = 1;
501 sd->do_lcd_stop = 0;
502 if ((gspca_dev->usb_buf[0] != 0x03) &&
503 (gspca_dev->usb_buf[0] != 0x04)) {
504 PDEBUG(D_ERR, "Unknown VGA Sensor id Byte 0: %02x",
505 gspca_dev->usb_buf[1]);
506 PDEBUG(D_ERR, "Defaults assumed, may not work");
507 PDEBUG(D_ERR, "Please report this");
509 if (gspca_dev->usb_buf[0] == 0x04) {
510 sd->do_lcd_stop = 1;
511 switch (gspca_dev->usb_buf[1]) {
512 case 0x50:
513 sd->sensor_type = 0;
514 PDEBUG(D_PROBE, "sensor_type corrected to 0");
515 break;
516 case 0x20:
517 /* Nothing to do here. */
518 break;
519 default:
520 PDEBUG(D_ERR,
521 "Unknown VGA Sensor id Byte 1: %02x",
522 gspca_dev->usb_buf[1]);
523 PDEBUG(D_ERR,
524 "Defaults assumed, may not work");
525 PDEBUG(D_ERR, "Please report this");
528 PDEBUG(D_PROBE, "MR97310A VGA camera detected, sensor: %d",
529 sd->sensor_type);
531 /* Stop streaming as we've started it to probe the sensor type. */
532 sd_stopN(gspca_dev);
534 if (force_sensor_type != -1) {
535 sd->sensor_type = !!force_sensor_type;
536 PDEBUG(D_PROBE, "Forcing sensor type to: %d",
537 sd->sensor_type);
540 /* Setup controls depending on camera type */
541 if (sd->cam_type == CAM_TYPE_CIF) {
542 /* No brightness for sensor_type 0 */
543 if (sd->sensor_type == 0)
544 gspca_dev->ctrl_dis = (1 << NORM_BRIGHTNESS_IDX) |
545 (1 << ARGUS_QC_BRIGHTNESS_IDX);
546 else
547 gspca_dev->ctrl_dis = (1 << ARGUS_QC_BRIGHTNESS_IDX);
548 } else {
549 /* All controls need to be disabled if VGA sensor_type is 0 */
550 if (sd->sensor_type == 0)
551 gspca_dev->ctrl_dis = (1 << NORM_BRIGHTNESS_IDX) |
552 (1 << ARGUS_QC_BRIGHTNESS_IDX) |
553 (1 << EXPOSURE_IDX) |
554 (1 << GAIN_IDX);
555 else if (sd->do_lcd_stop)
556 /* Argus QuickClix has different brightness limits */
557 gspca_dev->ctrl_dis = (1 << NORM_BRIGHTNESS_IDX);
558 else
559 gspca_dev->ctrl_dis = (1 << ARGUS_QC_BRIGHTNESS_IDX);
562 sd->brightness = MR97310A_BRIGHTNESS_DEFAULT;
563 sd->exposure = MR97310A_EXPOSURE_DEFAULT;
564 sd->gain = MR97310A_GAIN_DEFAULT;
566 return 0;
569 /* this function is called at probe and resume time */
570 static int sd_init(struct gspca_dev *gspca_dev)
572 return 0;
575 static int start_cif_cam(struct gspca_dev *gspca_dev)
577 struct sd *sd = (struct sd *) gspca_dev;
578 __u8 *data = gspca_dev->usb_buf;
579 int err_code;
580 const __u8 startup_string[] = {
581 0x00,
582 0x0d,
583 0x01,
584 0x00, /* Hsize/8 for 352 or 320 */
585 0x00, /* Vsize/4 for 288 or 240 */
586 0x13, /* or 0xbb, depends on sensor */
587 0x00, /* Hstart, depends on res. */
588 0x00, /* reserved ? */
589 0x00, /* Vstart, depends on res. and sensor */
590 0x50, /* 0x54 to get 176 or 160 */
591 0xc0
594 /* Note: Some of the above descriptions guessed from MR97113A driver */
596 memcpy(data, startup_string, 11);
597 if (sd->sensor_type)
598 data[5] = 0xbb;
600 switch (gspca_dev->width) {
601 case 160:
602 data[9] |= 0x04; /* reg 8, 2:1 scale down from 320 */
603 /* fall thru */
604 case 320:
605 default:
606 data[3] = 0x28; /* reg 2, H size/8 */
607 data[4] = 0x3c; /* reg 3, V size/4 */
608 data[6] = 0x14; /* reg 5, H start */
609 data[8] = 0x1a + sd->sensor_type; /* reg 7, V start */
610 break;
611 case 176:
612 data[9] |= 0x04; /* reg 8, 2:1 scale down from 352 */
613 /* fall thru */
614 case 352:
615 data[3] = 0x2c; /* reg 2, H size/8 */
616 data[4] = 0x48; /* reg 3, V size/4 */
617 data[6] = 0x06; /* reg 5, H start */
618 data[8] = 0x06 - sd->sensor_type; /* reg 7, V start */
619 break;
621 err_code = mr_write(gspca_dev, 11);
622 if (err_code < 0)
623 return err_code;
625 if (!sd->sensor_type) {
626 const struct sensor_w_data cif_sensor0_init_data[] = {
627 {0x02, 0x00, {0x03, 0x5a, 0xb5, 0x01,
628 0x0f, 0x14, 0x0f, 0x10}, 8},
629 {0x0c, 0x00, {0x04, 0x01, 0x01, 0x00, 0x1f}, 5},
630 {0x12, 0x00, {0x07}, 1},
631 {0x1f, 0x00, {0x06}, 1},
632 {0x27, 0x00, {0x04}, 1},
633 {0x29, 0x00, {0x0c}, 1},
634 {0x40, 0x00, {0x40, 0x00, 0x04}, 3},
635 {0x50, 0x00, {0x60}, 1},
636 {0x60, 0x00, {0x06}, 1},
637 {0x6b, 0x00, {0x85, 0x85, 0xc8, 0xc8, 0xc8, 0xc8}, 6},
638 {0x72, 0x00, {0x1e, 0x56}, 2},
639 {0x75, 0x00, {0x58, 0x40, 0xa2, 0x02, 0x31, 0x02,
640 0x31, 0x80, 0x00}, 9},
641 {0x11, 0x00, {0x01}, 1},
642 {0, 0, {0}, 0}
644 err_code = sensor_write_regs(gspca_dev, cif_sensor0_init_data,
645 ARRAY_SIZE(cif_sensor0_init_data));
646 } else { /* sd->sensor_type = 1 */
647 const struct sensor_w_data cif_sensor1_init_data[] = {
648 /* Reg 3,4, 7,8 get set by the controls */
649 {0x02, 0x00, {0x10}, 1},
650 {0x05, 0x01, {0x22}, 1}, /* 5/6 also seen as 65h/32h */
651 {0x06, 0x01, {0x00}, 1},
652 {0x09, 0x02, {0x0e}, 1},
653 {0x0a, 0x02, {0x05}, 1},
654 {0x0b, 0x02, {0x05}, 1},
655 {0x0c, 0x02, {0x0f}, 1},
656 {0x0d, 0x02, {0x07}, 1},
657 {0x0e, 0x02, {0x0c}, 1},
658 {0x0f, 0x00, {0x00}, 1},
659 {0x10, 0x00, {0x06}, 1},
660 {0x11, 0x00, {0x07}, 1},
661 {0x12, 0x00, {0x00}, 1},
662 {0x13, 0x00, {0x01}, 1},
663 {0, 0, {0}, 0}
665 err_code = sensor_write_regs(gspca_dev, cif_sensor1_init_data,
666 ARRAY_SIZE(cif_sensor1_init_data));
668 return err_code;
671 static int start_vga_cam(struct gspca_dev *gspca_dev)
673 struct sd *sd = (struct sd *) gspca_dev;
674 __u8 *data = gspca_dev->usb_buf;
675 int err_code;
676 const __u8 startup_string[] = {0x00, 0x0d, 0x01, 0x00, 0x00, 0x2b,
677 0x00, 0x00, 0x00, 0x50, 0xc0};
678 /* What some of these mean is explained in start_cif_cam(), above */
680 memcpy(data, startup_string, 11);
681 if (!sd->sensor_type) {
682 data[5] = 0x00;
683 data[10] = 0x91;
686 switch (gspca_dev->width) {
687 case 160:
688 data[9] |= 0x0c; /* reg 8, 4:1 scale down */
689 /* fall thru */
690 case 320:
691 data[9] |= 0x04; /* reg 8, 2:1 scale down */
692 /* fall thru */
693 case 640:
694 default:
695 data[3] = 0x50; /* reg 2, H size/8 */
696 data[4] = 0x78; /* reg 3, V size/4 */
697 data[6] = 0x04; /* reg 5, H start */
698 data[8] = 0x03; /* reg 7, V start */
699 if (sd->do_lcd_stop)
700 data[8] = 0x04; /* Bayer tile shifted */
701 break;
703 case 176:
704 data[9] |= 0x04; /* reg 8, 2:1 scale down */
705 /* fall thru */
706 case 352:
707 data[3] = 0x2c; /* reg 2, H size */
708 data[4] = 0x48; /* reg 3, V size */
709 data[6] = 0x94; /* reg 5, H start */
710 data[8] = 0x63; /* reg 7, V start */
711 if (sd->do_lcd_stop)
712 data[8] = 0x64; /* Bayer tile shifted */
713 break;
716 err_code = mr_write(gspca_dev, 11);
717 if (err_code < 0)
718 return err_code;
720 if (!sd->sensor_type) {
721 /* The only known sensor_type 0 cam is the Argus DC-1620 */
722 const struct sensor_w_data vga_sensor0_init_data[] = {
723 {0x01, 0x00, {0x0c, 0x00, 0x04}, 3},
724 {0x14, 0x00, {0x01, 0xe4, 0x02, 0x84}, 4},
725 {0x20, 0x00, {0x00, 0x80, 0x00, 0x08}, 4},
726 {0x25, 0x00, {0x03, 0xa9, 0x80}, 3},
727 {0x30, 0x00, {0x30, 0x18, 0x10, 0x18}, 4},
728 {0, 0, {0}, 0}
730 err_code = sensor_write_regs(gspca_dev, vga_sensor0_init_data,
731 ARRAY_SIZE(vga_sensor0_init_data));
732 } else { /* sd->sensor_type = 1 */
733 const struct sensor_w_data vga_sensor1_init_data[] = {
734 {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
735 0x07, 0x00, 0x01}, 8},
736 {0x11, 0x04, {0x01}, 1},
737 /*{0x0a, 0x00, {0x00, 0x01, 0x00, 0x00, 0x01, */
738 {0x0a, 0x00, {0x01, 0x06, 0x00, 0x00, 0x01,
739 0x00, 0x0a}, 7},
740 {0x11, 0x04, {0x01}, 1},
741 {0x12, 0x00, {0x00, 0x63, 0x00, 0x70, 0x00, 0x00}, 6},
742 {0x11, 0x04, {0x01}, 1},
743 {0, 0, {0}, 0}
745 err_code = sensor_write_regs(gspca_dev, vga_sensor1_init_data,
746 ARRAY_SIZE(vga_sensor1_init_data));
748 return err_code;
751 static int sd_start(struct gspca_dev *gspca_dev)
753 struct sd *sd = (struct sd *) gspca_dev;
754 int err_code;
756 sd->sof_read = 0;
758 /* Some of the VGA cameras require the memory pointer
759 * to be set to 0 again. We have been forced to start the
760 * stream somewhere else to detect the hardware, and closed it,
761 * and now since we are restarting the stream we need to do a
762 * completely fresh and clean start. */
763 err_code = zero_the_pointer(gspca_dev);
764 if (err_code < 0)
765 return err_code;
767 err_code = stream_start(gspca_dev);
768 if (err_code < 0)
769 return err_code;
771 if (sd->cam_type == CAM_TYPE_CIF) {
772 err_code = start_cif_cam(gspca_dev);
773 } else {
774 err_code = start_vga_cam(gspca_dev);
776 if (err_code < 0)
777 return err_code;
779 setbrightness(gspca_dev);
780 setexposure(gspca_dev);
781 setgain(gspca_dev);
783 return isoc_enable(gspca_dev);
786 static void sd_stopN(struct gspca_dev *gspca_dev)
788 struct sd *sd = (struct sd *) gspca_dev;
790 stream_stop(gspca_dev);
791 /* Not all the cams need this, but even if not, probably a good idea */
792 zero_the_pointer(gspca_dev);
793 if (sd->do_lcd_stop)
794 lcd_stop(gspca_dev);
797 static void setbrightness(struct gspca_dev *gspca_dev)
799 struct sd *sd = (struct sd *) gspca_dev;
800 u8 val;
801 u8 sign_reg = 7; /* This reg and the next one used on CIF cams. */
802 u8 value_reg = 8; /* VGA cams seem to use regs 0x0b and 0x0c */
803 const u8 quick_clix_table[] =
804 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
805 { 0, 4, 8, 12, 1, 2, 3, 5, 6, 9, 7, 10, 13, 11, 14, 15};
807 * This control is disabled for CIF type 1 and VGA type 0 cameras.
808 * It does not quite act linearly for the Argus QuickClix camera,
809 * but it does control brightness. The values are 0 - 15 only, and
810 * the table above makes them act consecutively.
812 if ((gspca_dev->ctrl_dis & (1 << NORM_BRIGHTNESS_IDX)) &&
813 (gspca_dev->ctrl_dis & (1 << ARGUS_QC_BRIGHTNESS_IDX)))
814 return;
816 if (sd->cam_type == CAM_TYPE_VGA) {
817 sign_reg += 4;
818 value_reg += 4;
821 /* Note register 7 is also seen as 0x8x or 0xCx in dumps */
822 if (sd->brightness > 0) {
823 sensor_write1(gspca_dev, sign_reg, 0x00);
824 val = sd->brightness;
825 } else {
826 sensor_write1(gspca_dev, sign_reg, 0x01);
827 val = (257 - sd->brightness);
829 /* Use lookup table for funky Argus QuickClix brightness */
830 if (sd->do_lcd_stop)
831 val = quick_clix_table[val];
833 sensor_write1(gspca_dev, value_reg, val);
836 static void setexposure(struct gspca_dev *gspca_dev)
838 struct sd *sd = (struct sd *) gspca_dev;
839 int exposure;
841 if (gspca_dev->ctrl_dis & (1 << EXPOSURE_IDX))
842 return;
844 if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1) {
845 /* This cam does not like exposure settings > 300,
846 so scale 0 - 4095 to 300 - 4095 */
847 exposure = (sd->exposure * 9267) / 10000 + 300;
848 sensor_write1(gspca_dev, 3, exposure >> 4);
849 sensor_write1(gspca_dev, 4, exposure & 0x0f);
850 } else {
851 /* We have both a clock divider and an exposure register.
852 We first calculate the clock divider, as that determines
853 the maximum exposure and then we calculayte the exposure
854 register setting (which goes from 0 - 511).
856 Note our 0 - 4095 exposure is mapped to 0 - 511
857 milliseconds exposure time */
858 u8 clockdiv = (60 * sd->exposure + 7999) / 8000;
860 /* Limit framerate to not exceed usb bandwidth */
861 if (clockdiv < 3 && gspca_dev->width >= 320)
862 clockdiv = 3;
863 else if (clockdiv < 2)
864 clockdiv = 2;
866 if (sd->cam_type == CAM_TYPE_VGA && clockdiv < 4)
867 clockdiv = 4;
869 /* Frame exposure time in ms = 1000 * clockdiv / 60 ->
870 exposure = (sd->exposure / 8) * 511 / (1000 * clockdiv / 60) */
871 exposure = (60 * 511 * sd->exposure) / (8000 * clockdiv);
872 if (exposure > 511)
873 exposure = 511;
875 /* exposure register value is reversed! */
876 exposure = 511 - exposure;
878 sensor_write1(gspca_dev, 0x02, clockdiv);
879 sensor_write1(gspca_dev, 0x0e, exposure & 0xff);
880 sensor_write1(gspca_dev, 0x0f, exposure >> 8);
884 static void setgain(struct gspca_dev *gspca_dev)
886 struct sd *sd = (struct sd *) gspca_dev;
888 if (gspca_dev->ctrl_dis & (1 << GAIN_IDX))
889 return;
891 if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1) {
892 sensor_write1(gspca_dev, 0x0e, sd->gain);
893 } else {
894 sensor_write1(gspca_dev, 0x10, sd->gain);
898 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
900 struct sd *sd = (struct sd *) gspca_dev;
902 sd->brightness = val;
903 if (gspca_dev->streaming)
904 setbrightness(gspca_dev);
905 return 0;
908 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
910 struct sd *sd = (struct sd *) gspca_dev;
912 *val = sd->brightness;
913 return 0;
916 static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
918 struct sd *sd = (struct sd *) gspca_dev;
920 sd->exposure = val;
921 if (gspca_dev->streaming)
922 setexposure(gspca_dev);
923 return 0;
926 static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
928 struct sd *sd = (struct sd *) gspca_dev;
930 *val = sd->exposure;
931 return 0;
934 static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
936 struct sd *sd = (struct sd *) gspca_dev;
938 sd->gain = val;
939 if (gspca_dev->streaming)
940 setgain(gspca_dev);
941 return 0;
944 static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
946 struct sd *sd = (struct sd *) gspca_dev;
948 *val = sd->gain;
949 return 0;
952 /* Include pac common sof detection functions */
953 #include "pac_common.h"
955 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
956 struct gspca_frame *frame, /* target */
957 __u8 *data, /* isoc packet */
958 int len) /* iso packet length */
960 unsigned char *sof;
962 sof = pac_find_sof(gspca_dev, data, len);
963 if (sof) {
964 int n;
966 /* finish decoding current frame */
967 n = sof - data;
968 if (n > sizeof pac_sof_marker)
969 n -= sizeof pac_sof_marker;
970 else
971 n = 0;
972 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
973 data, n);
974 /* Start next frame. */
975 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
976 pac_sof_marker, sizeof pac_sof_marker);
977 len -= sof - data;
978 data = sof;
980 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
983 /* sub-driver description */
984 static const struct sd_desc sd_desc = {
985 .name = MODULE_NAME,
986 .ctrls = sd_ctrls,
987 .nctrls = ARRAY_SIZE(sd_ctrls),
988 .config = sd_config,
989 .init = sd_init,
990 .start = sd_start,
991 .stopN = sd_stopN,
992 .pkt_scan = sd_pkt_scan,
995 /* -- module initialisation -- */
996 static const __devinitdata struct usb_device_id device_table[] = {
997 {USB_DEVICE(0x08ca, 0x0111)}, /* Aiptek Pencam VGA+ */
998 {USB_DEVICE(0x093a, 0x010f)}, /* All other known MR97310A VGA cams */
999 {USB_DEVICE(0x093a, 0x010e)}, /* All known MR97310A CIF cams */
1002 MODULE_DEVICE_TABLE(usb, device_table);
1004 /* -- device connect -- */
1005 static int sd_probe(struct usb_interface *intf,
1006 const struct usb_device_id *id)
1008 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1009 THIS_MODULE);
1012 static struct usb_driver sd_driver = {
1013 .name = MODULE_NAME,
1014 .id_table = device_table,
1015 .probe = sd_probe,
1016 .disconnect = gspca_disconnect,
1017 #ifdef CONFIG_PM
1018 .suspend = gspca_suspend,
1019 .resume = gspca_resume,
1020 #endif
1023 /* -- module insert / remove -- */
1024 static int __init sd_mod_init(void)
1026 int ret;
1028 ret = usb_register(&sd_driver);
1029 if (ret < 0)
1030 return ret;
1031 PDEBUG(D_PROBE, "registered");
1032 return 0;
1034 static void __exit sd_mod_exit(void)
1036 usb_deregister(&sd_driver);
1037 PDEBUG(D_PROBE, "deregistered");
1040 module_init(sd_mod_init);
1041 module_exit(sd_mod_exit);