blob: 3a3edf82a361fc08db9298fe80314229ef25d42b [file] [log] [blame]
Kyle Guinnd661e622009-01-16 05:36:14 -03001/*
2 * Mars MR97310A library
3 *
4 * Copyright (C) 2009 Kyle Guinn <elyk03@gmail.com>
5 *
Theodore Kilgore89f08632009-08-14 06:51:52 -03006 * 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 *
Kyle Guinnd661e622009-01-16 05:36:14 -030021 * 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
Theodore Kilgore89f08632009-08-14 06:51:52 -030040#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>");
Kyle Guinnd661e622009-01-16 05:36:14 -030057MODULE_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 */
Kyle Guinnd661e622009-01-16 05:36:14 -030063 u8 sof_read;
Theodore Kilgore89f08632009-08-14 06:51:52 -030064 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;
Kyle Guinnd661e622009-01-16 05:36:14 -030073};
74
Theodore Kilgore89f08632009-08-14 06:51:52 -030075struct 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
Kyle Guinnd661e622009-01-16 05:36:14 -030089/* V4L2 controls supported by the driver */
90static struct ctrl sd_ctrls[] = {
Theodore Kilgore89f08632009-08-14 06:51:52 -030091 {
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 },
Kyle Guinnd661e622009-01-16 05:36:14 -0300133};
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 */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300164static int mr_write(struct gspca_dev *gspca_dev, int len)
Kyle Guinnd661e622009-01-16 05:36:14 -0300165{
166 int rc;
167
168 rc = usb_bulk_msg(gspca_dev->dev,
169 usb_sndbulkpipe(gspca_dev->dev, 4),
Jean-Francois Moine92e8c912009-02-02 16:25:38 -0300170 gspca_dev->usb_buf, len, NULL, 500);
Kyle Guinnd661e622009-01-16 05:36:14 -0300171 if (rc < 0)
172 PDEBUG(D_ERR, "reg write [%02x] error %d",
173 gspca_dev->usb_buf[0], rc);
174 return rc;
175}
176
Theodore Kilgore89f08632009-08-14 06:51:52 -0300177/* 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
Kyle Guinnd661e622009-01-16 05:36:14 -0300353/* this function is called at probe time */
354static int sd_config(struct gspca_dev *gspca_dev,
355 const struct usb_device_id *id)
356{
Theodore Kilgore89f08632009-08-14 06:51:52 -0300357 struct sd *sd = (struct sd *) gspca_dev;
Kyle Guinnd661e622009-01-16 05:36:14 -0300358 struct cam *cam;
359
360 cam = &gspca_dev->cam;
361 cam->cam_mode = vga_mode;
362 cam->nmodes = ARRAY_SIZE(vga_mode);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300363 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 }
Kyle Guinnd661e622009-01-16 05:36:14 -0300371 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
Theodore Kilgore89f08632009-08-14 06:51:52 -0300380static 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)
Kyle Guinnd661e622009-01-16 05:36:14 -0300410{
411 struct sd *sd = (struct sd *) gspca_dev;
412 __u8 *data = gspca_dev->usb_buf;
413 int err_code;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300414 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 };
Kyle Guinnd661e622009-01-16 05:36:14 -0300427
Theodore Kilgore89f08632009-08-14 06:51:52 -0300428 /* Note: Some of the above descriptions guessed from MR97113A driver */
429 sd->sensor_type = 0;
Kyle Guinnd661e622009-01-16 05:36:14 -0300430 data[0] = 0x01;
431 data[1] = 0x01;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300432 err_code = mr_write(gspca_dev, 2);
Kyle Guinnd661e622009-01-16 05:36:14 -0300433 if (err_code < 0)
434 return err_code;
435
Theodore Kilgore89f08632009-08-14 06:51:52 -0300436 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 */
450 if ((data[0] & 0x78) == 8 || (data[0] & 0x2) == 0x2)
451 sd->sensor_type = 1;
452
453 PDEBUG(D_ERR, "Sensor type is %01x", sd->sensor_type);
454 memcpy(data, startup_string, 11);
455 if (sd->sensor_type)
456 data[5] = 0xbb;
457
458 switch (gspca_dev->width) {
459 case 160:
460 data[9] |= 0x04; /* reg 8, 2:1 scale down from 320 */
461 /* fall thru */
462 case 320:
463 default:
464 data[3] = 0x28; /* reg 2, H size/8 */
465 data[4] = 0x3c; /* reg 3, V size/4 */
466 data[6] = 0x14; /* reg 5, H start */
467 data[8] = 0x1a + sd->sensor_type; /* reg 7, V start */
468 break;
469 case 176:
470 data[9] |= 0x04; /* reg 8, 2:1 scale down from 352 */
471 /* fall thru */
472 case 352:
473 data[3] = 0x2c; /* reg 2, H size/8 */
474 data[4] = 0x48; /* reg 3, V size/4 */
475 data[6] = 0x06; /* reg 5, H start */
476 data[8] = 0x06 + sd->sensor_type; /* reg 7, V start */
477 break;
478 }
479 err_code = mr_write(gspca_dev, 11);
480 if (err_code < 0)
481 return err_code;
482
483 if (!sd->sensor_type) {
484 const struct sensor_w_data cif_sensor0_init_data[] = {
485 {0x02, 0x00, {0x03, 0x5a, 0xb5, 0x01,
486 0x0f, 0x14, 0x0f, 0x10}, 8},
487 {0x0c, 0x00, {0x04, 0x01, 0x01, 0x00, 0x1f}, 5},
488 {0x12, 0x00, {0x07}, 1},
489 {0x1f, 0x00, {0x06}, 1},
490 {0x27, 0x00, {0x04}, 1},
491 {0x29, 0x00, {0x0c}, 1},
492 {0x40, 0x00, {0x40, 0x00, 0x04}, 3},
493 {0x50, 0x00, {0x60}, 1},
494 {0x60, 0x00, {0x06}, 1},
495 {0x6b, 0x00, {0x85, 0x85, 0xc8, 0xc8, 0xc8, 0xc8}, 6},
496 {0x72, 0x00, {0x1e, 0x56}, 2},
497 {0x75, 0x00, {0x58, 0x40, 0xa2, 0x02, 0x31, 0x02,
498 0x31, 0x80, 0x00}, 9},
499 {0x11, 0x00, {0x01}, 1},
500 {0, 0, {0}, 0}
501 };
502 err_code = sensor_write_regs(gspca_dev, cif_sensor0_init_data,
503 ARRAY_SIZE(cif_sensor0_init_data));
504 } else { /* sd->sensor_type = 1 */
505 const struct sensor_w_data cif_sensor1_init_data[] = {
506 {0x02, 0x00, {0x10}, 1},
507 {0x03, 0x01, {0x12}, 1},
508 {0x04, 0x01, {0x05}, 1},
509 {0x05, 0x01, {0x65}, 1},
510 {0x06, 0x01, {0x32}, 1},
511 {0x07, 0x01, {0x00}, 1},
512 {0x08, 0x02, {0x06}, 1},
513 {0x09, 0x02, {0x0e}, 1},
514 {0x0a, 0x02, {0x05}, 1},
515 {0x0b, 0x02, {0x05}, 1},
516 {0x0c, 0x02, {0x0f}, 1},
517 {0x0d, 0x02, {0x00}, 1},
518 {0x0e, 0x02, {0x0c}, 1},
519 {0x0f, 0x00, {0x00}, 1},
520 {0x10, 0x00, {0x06}, 1},
521 {0x11, 0x00, {0x07}, 1},
522 {0x12, 0x00, {0x00}, 1},
523 {0x13, 0x00, {0x01}, 1},
524 {0, 0, {0}, 0}
525 };
526 err_code = sensor_write_regs(gspca_dev, cif_sensor1_init_data,
527 ARRAY_SIZE(cif_sensor1_init_data));
528 }
529 if (err_code < 0)
530 return err_code;
531
532 msleep(200);
Kyle Guinnd661e622009-01-16 05:36:14 -0300533 data[0] = 0x00;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300534 data[1] = 0x4d; /* ISOC transfering enable... */
535 err_code = mr_write(gspca_dev, 2);
536 if (err_code < 0)
537 return err_code;
538
539 msleep(200);
540 err_code = adjust_cif_sensor(gspca_dev);
541 if (err_code < 0)
542 return err_code;
543
544 msleep(200);
545 return 0;
546}
547
548static int start_vga_cam(struct gspca_dev *gspca_dev)
549{
550 struct sd *sd = (struct sd *) gspca_dev;
551 __u8 *data = gspca_dev->usb_buf;
552 int err_code;
553 const __u8 startup_string[] = {0x00, 0x0d, 0x01, 0x00, 0x00, 0x2b,
554 0x00, 0x00, 0x00, 0x50, 0xc0};
555
556 /* What some of these mean is explained in start_cif_cam(), above */
557 sd->sof_read = 0;
558
559 /*
560 * We have to know which camera we have, because the register writes
561 * depend upon the camera. This test, run before we actually enter
562 * the initialization routine, distinguishes most of the cameras, If
563 * needed, another routine is done later, too.
564 */
565 memset(data, 0, 16);
566 data[0] = 0x20;
567 err_code = mr_write(gspca_dev, 1);
568 if (err_code < 0)
569 return err_code;
570
571 err_code = mr_read(gspca_dev, 16);
572 if (err_code < 0)
573 return err_code;
574
575 PDEBUG(D_ERR, "Read 16 bytes from camera");
576 PDEBUG(D_ERR, "Byte reported is %02x", data[0]);
577
578 msleep(200);
579 /*
580 * Known VGA cameras. If you have another to report, please do
581 *
582 * Name byte just read sd->sensor_type
583 * sd->do_lcd_stop
584 * Aiptek Pencam VGA+ 0x31 0 1
585 * ION digital 0x31 0 1
586 * Argus DC-1620 0x30 1 0
587 * Argus QuickClix 0x30 1 1 (not caught here)
588 */
589 sd->sensor_type = data[0] & 1;
590 sd->do_lcd_stop = (~data[0]) & 1;
591
592
593
594 /* Streaming setup begins here. */
595
596
597 data[0] = 0x01;
598 data[1] = 0x01;
599 err_code = mr_write(gspca_dev, 2);
600 if (err_code < 0)
601 return err_code;
602
603 /*
604 * A second test can now resolve any remaining ambiguity in the
605 * identification of the camera type,
606 */
607 if (!sd->sensor_type) {
608 data[0] = get_sensor_id(gspca_dev);
609 if (data[0] == 0x7f) {
610 sd->sensor_type = 1;
611 PDEBUG(D_ERR, "sensor_type corrected to 1");
612 }
613 msleep(200);
614 }
615
616 /*
617 * Known VGA cameras.
618 * This test is only run if the previous test returned 0x30, but
619 * here is the information for all others, too, just for reference.
620 *
621 * Name byte just read sd->sensor_type
622 *
623 * Aiptek Pencam VGA+ 0xfb (this test not run) 1
624 * ION digital 0xbd (this test not run) 1
625 * Argus DC-1620 0xe5 (no change) 0
626 * Argus QuickClix 0x7f (reclassified) 1
627 */
628 memcpy(data, startup_string, 11);
629 if (!sd->sensor_type) {
630 data[5] = 0x00;
631 data[10] = 0x91;
632 }
Kyle Guinnd661e622009-01-16 05:36:14 -0300633
634 switch (gspca_dev->width) {
635 case 160:
636 data[9] |= 0x0c; /* reg 8, 4:1 scale down */
637 /* fall thru */
638 case 320:
639 data[9] |= 0x04; /* reg 8, 2:1 scale down */
640 /* fall thru */
641 case 640:
642 default:
Theodore Kilgore89f08632009-08-14 06:51:52 -0300643 data[3] = 0x50; /* reg 2, H size/8 */
644 data[4] = 0x78; /* reg 3, V size/4 */
Kyle Guinnd661e622009-01-16 05:36:14 -0300645 data[6] = 0x04; /* reg 5, H start */
646 data[8] = 0x03; /* reg 7, V start */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300647 if (sd->do_lcd_stop)
648 data[8] = 0x04; /* Bayer tile shifted */
Kyle Guinnd661e622009-01-16 05:36:14 -0300649 break;
650
651 case 176:
652 data[9] |= 0x04; /* reg 8, 2:1 scale down */
653 /* fall thru */
654 case 352:
655 data[3] = 0x2c; /* reg 2, H size */
656 data[4] = 0x48; /* reg 3, V size */
657 data[6] = 0x94; /* reg 5, H start */
658 data[8] = 0x63; /* reg 7, V start */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300659 if (sd->do_lcd_stop)
660 data[8] = 0x64; /* Bayer tile shifted */
Kyle Guinnd661e622009-01-16 05:36:14 -0300661 break;
662 }
663
Theodore Kilgore89f08632009-08-14 06:51:52 -0300664 err_code = mr_write(gspca_dev, 11);
Kyle Guinnd661e622009-01-16 05:36:14 -0300665 if (err_code < 0)
666 return err_code;
667
Theodore Kilgore89f08632009-08-14 06:51:52 -0300668 if (!sd->sensor_type) {
669 /* The only known sensor_type 0 cam is the Argus DC-1620 */
670 const struct sensor_w_data vga_sensor0_init_data[] = {
671 {0x01, 0x00, {0x0c, 0x00, 0x04}, 3},
672 {0x14, 0x00, {0x01, 0xe4, 0x02, 0x84}, 4},
673 {0x20, 0x00, {0x00, 0x80, 0x00, 0x08}, 4},
674 {0x25, 0x00, {0x03, 0xa9, 0x80}, 3},
675 {0x30, 0x00, {0x30, 0x18, 0x10, 0x18}, 4},
676 {0, 0, {0}, 0}
677 };
678 err_code = sensor_write_regs(gspca_dev, vga_sensor0_init_data,
679 ARRAY_SIZE(vga_sensor0_init_data));
680 } else { /* sd->sensor_type = 1 */
681 const struct sensor_w_data vga_sensor1_init_data[] = {
682 {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
683 0x07, 0x00, 0x01}, 8},
684 {0x11, 0x04, {0x01}, 1},
685 /*{0x0a, 0x00, {0x00, 0x01, 0x00, 0x00, 0x01, */
686 {0x0a, 0x00, {0x01, 0x06, 0x00, 0x00, 0x01,
687 0x00, 0x0a}, 7},
688 {0x11, 0x04, {0x01}, 1},
689 {0x12, 0x00, {0x00, 0x63, 0x00, 0x70, 0x00, 0x00}, 6},
690 {0x11, 0x04, {0x01}, 1},
691 {0, 0, {0}, 0}
692 };
693 err_code = sensor_write_regs(gspca_dev, vga_sensor1_init_data,
694 ARRAY_SIZE(vga_sensor1_init_data));
695 }
Kyle Guinnd661e622009-01-16 05:36:14 -0300696 if (err_code < 0)
697 return err_code;
698
Theodore Kilgore89f08632009-08-14 06:51:52 -0300699 msleep(200);
Kyle Guinnd661e622009-01-16 05:36:14 -0300700 data[0] = 0x00;
701 data[1] = 0x4d; /* ISOC transfering enable... */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300702 err_code = mr_write(gspca_dev, 2);
703
704 return err_code;
705}
706
707static int sd_start(struct gspca_dev *gspca_dev)
708{
709 struct sd *sd = (struct sd *) gspca_dev;
710 int err_code;
711 struct cam *cam;
712
713 /* TEST TEST */
714 int i;
715 for (i = 2; i <= 14; i++)
716 sd->regs[i] = sd_ctrls[i - 2].qctrl.default_value;
717
718 cam = &gspca_dev->cam;
719 sd->sof_read = 0;
720 /*
721 * Some of the supported cameras require the memory pointer to be
722 * set to 0, or else they will not stream.
723 */
724 zero_the_pointer(gspca_dev);
725 msleep(200);
726 if (sd->cam_type == CAM_TYPE_CIF) {
727 PDEBUG(D_ERR, "CIF camera");
728 err_code = start_cif_cam(gspca_dev);
729 } else {
730 PDEBUG(D_ERR, "VGA camera");
731 err_code = start_vga_cam(gspca_dev);
732 }
Kyle Guinnd661e622009-01-16 05:36:14 -0300733 return err_code;
734}
735
736static void sd_stopN(struct gspca_dev *gspca_dev)
737{
Theodore Kilgore89f08632009-08-14 06:51:52 -0300738 struct sd *sd = (struct sd *) gspca_dev;
Kyle Guinnd661e622009-01-16 05:36:14 -0300739 int result;
740
741 gspca_dev->usb_buf[0] = 1;
742 gspca_dev->usb_buf[1] = 0;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300743 result = mr_write(gspca_dev, 2);
Kyle Guinnd661e622009-01-16 05:36:14 -0300744 if (result < 0)
745 PDEBUG(D_ERR, "Camera Stop failed");
Theodore Kilgore89f08632009-08-14 06:51:52 -0300746
747 /* Not all the cams need this, but even if not, probably a good idea */
748 zero_the_pointer(gspca_dev);
749 if (sd->do_lcd_stop) {
750 gspca_dev->usb_buf[0] = 0x19;
751 gspca_dev->usb_buf[1] = 0x54;
752 result = mr_write(gspca_dev, 2);
753 if (result < 0)
754 PDEBUG(D_ERR, "Camera Stop failed");
755 }
756}
757
758static void setbrightness(struct gspca_dev *gspca_dev)
759{
760 struct sd *sd = (struct sd *) gspca_dev;
761 u8 val;
762 if (sd->brightness > 0) {
763 sensor_write1(gspca_dev, 7, 0);
764 val = sd->brightness;
765 } else {
766 sensor_write1(gspca_dev, 7, 1);
767 val = 257 - sd->brightness;
768 }
769 sensor_write1(gspca_dev, 8, val);
770}
771
772static void setexposure(struct gspca_dev *gspca_dev)
773{
774 struct sd *sd = (struct sd *) gspca_dev;
775 u8 val;
776
777 val = sd->exposure >> 4;
778 sensor_write1(gspca_dev, 3, val);
779 val = sd->exposure & 0xf;
780 sensor_write1(gspca_dev, 4, val);
781}
782
783static void setgain(struct gspca_dev *gspca_dev)
784{
785 struct sd *sd = (struct sd *) gspca_dev;
786
787 sensor_write1(gspca_dev, 3, sd->gain);
788}
789
790static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
791{
792 struct sd *sd = (struct sd *) gspca_dev;
793
794 sd->brightness = val;
795 if (gspca_dev->streaming)
796 setbrightness(gspca_dev);
797 return 0;
798}
799
800static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
801{
802 struct sd *sd = (struct sd *) gspca_dev;
803
804 *val = sd->brightness;
805 return 0;
806}
807
808static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
809{
810 struct sd *sd = (struct sd *) gspca_dev;
811
812 sd->exposure = val;
813 if (gspca_dev->streaming)
814 setexposure(gspca_dev);
815 return 0;
816}
817
818static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
819{
820 struct sd *sd = (struct sd *) gspca_dev;
821
822 *val = sd->exposure;
823 return 0;
824}
825
826static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
827{
828 struct sd *sd = (struct sd *) gspca_dev;
829
830 sd->gain = val;
831 if (gspca_dev->streaming)
832 setgain(gspca_dev);
833 return 0;
834}
835
836static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
837{
838 struct sd *sd = (struct sd *) gspca_dev;
839
840 *val = sd->gain;
841 return 0;
Kyle Guinnd661e622009-01-16 05:36:14 -0300842}
843
844/* Include pac common sof detection functions */
845#include "pac_common.h"
846
847static void sd_pkt_scan(struct gspca_dev *gspca_dev,
848 struct gspca_frame *frame, /* target */
849 __u8 *data, /* isoc packet */
850 int len) /* iso packet length */
851{
Kyle Guinnd661e622009-01-16 05:36:14 -0300852 unsigned char *sof;
853
854 sof = pac_find_sof(gspca_dev, data, len);
855 if (sof) {
856 int n;
857
858 /* finish decoding current frame */
859 n = sof - data;
860 if (n > sizeof pac_sof_marker)
861 n -= sizeof pac_sof_marker;
862 else
863 n = 0;
864 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
865 data, n);
Theodore Kilgore9832d762009-03-13 13:04:31 -0300866 /* Start next frame. */
867 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
868 pac_sof_marker, sizeof pac_sof_marker);
Kyle Guinnd661e622009-01-16 05:36:14 -0300869 len -= sof - data;
870 data = sof;
871 }
Kyle Guinnd661e622009-01-16 05:36:14 -0300872 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
873}
874
875/* sub-driver description */
876static const struct sd_desc sd_desc = {
877 .name = MODULE_NAME,
878 .ctrls = sd_ctrls,
879 .nctrls = ARRAY_SIZE(sd_ctrls),
880 .config = sd_config,
881 .init = sd_init,
882 .start = sd_start,
883 .stopN = sd_stopN,
884 .pkt_scan = sd_pkt_scan,
885};
886
887/* -- module initialisation -- */
888static const __devinitdata struct usb_device_id device_table[] = {
Theodore Kilgore89f08632009-08-14 06:51:52 -0300889 {USB_DEVICE(0x08ca, 0x0111)}, /* Aiptek Pencam VGA+ */
890 {USB_DEVICE(0x093a, 0x010f)}, /* All other known MR97310A VGA cams */
891 {USB_DEVICE(0x093a, 0x010e)}, /* All known MR97310A CIF cams */
Kyle Guinnd661e622009-01-16 05:36:14 -0300892 {}
893};
894MODULE_DEVICE_TABLE(usb, device_table);
895
896/* -- device connect -- */
897static int sd_probe(struct usb_interface *intf,
898 const struct usb_device_id *id)
899{
900 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
901 THIS_MODULE);
902}
903
904static struct usb_driver sd_driver = {
905 .name = MODULE_NAME,
906 .id_table = device_table,
907 .probe = sd_probe,
908 .disconnect = gspca_disconnect,
909#ifdef CONFIG_PM
910 .suspend = gspca_suspend,
911 .resume = gspca_resume,
912#endif
913};
914
915/* -- module insert / remove -- */
916static int __init sd_mod_init(void)
917{
Alexey Klimov5d3fa302009-03-27 15:57:46 -0300918 int ret;
919
920 ret = usb_register(&sd_driver);
921 if (ret < 0)
922 return ret;
Kyle Guinnd661e622009-01-16 05:36:14 -0300923 PDEBUG(D_PROBE, "registered");
924 return 0;
925}
926static void __exit sd_mod_exit(void)
927{
928 usb_deregister(&sd_driver);
929 PDEBUG(D_PROBE, "deregistered");
930}
931
932module_init(sd_mod_init);
933module_exit(sd_mod_exit);