blob: 3bdf064699e9b01bd685eb0af62855a8ae4f4795 [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;
Theodore Kilgore89f08632009-08-14 06:51:52 -030067
68 int brightness;
69 u16 exposure;
Theodore Kilgore89f08632009-08-14 06:51:52 -030070 u8 gain;
Kyle Guinnd661e622009-01-16 05:36:14 -030071};
72
Theodore Kilgore89f08632009-08-14 06:51:52 -030073struct sensor_w_data {
74 u8 reg;
75 u8 flags;
76 u8 data[16];
77 int len;
78};
79
80static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
81static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
82static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
83static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
84static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
85static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
Hans de Goede9ac69782009-08-14 10:15:52 -030086static void setbrightness(struct gspca_dev *gspca_dev);
87static void setexposure(struct gspca_dev *gspca_dev);
88static void setgain(struct gspca_dev *gspca_dev);
Theodore Kilgore89f08632009-08-14 06:51:52 -030089
Kyle Guinnd661e622009-01-16 05:36:14 -030090/* V4L2 controls supported by the driver */
91static struct ctrl sd_ctrls[] = {
Theodore Kilgore89f08632009-08-14 06:51:52 -030092 {
Hans de Goede9ac69782009-08-14 10:15:52 -030093#define BRIGHTNESS_IDX 0
Theodore Kilgore89f08632009-08-14 06:51:52 -030094 {
95 .id = V4L2_CID_BRIGHTNESS,
96 .type = V4L2_CTRL_TYPE_INTEGER,
97 .name = "Brightness",
98 .minimum = MR97310A_BRIGHTNESS_MIN,
99 .maximum = MR97310A_BRIGHTNESS_MAX,
100 .step = 1,
101 .default_value = MR97310A_BRIGHTNESS_DEFAULT,
102 .flags = 0,
103 },
104 .set = sd_setbrightness,
105 .get = sd_getbrightness,
106 },
107 {
Hans de Goede9ac69782009-08-14 10:15:52 -0300108#define EXPOSURE_IDX 1
Theodore Kilgore89f08632009-08-14 06:51:52 -0300109 {
110 .id = V4L2_CID_EXPOSURE,
111 .type = V4L2_CTRL_TYPE_INTEGER,
112 .name = "Exposure",
113 .minimum = MR97310A_EXPOSURE_MIN,
114 .maximum = MR97310A_EXPOSURE_MAX,
115 .step = 1,
116 .default_value = MR97310A_EXPOSURE_DEFAULT,
117 .flags = 0,
118 },
119 .set = sd_setexposure,
120 .get = sd_getexposure,
121 },
122 {
Hans de Goede9ac69782009-08-14 10:15:52 -0300123#define GAIN_IDX 2
Theodore Kilgore89f08632009-08-14 06:51:52 -0300124 {
125 .id = V4L2_CID_GAIN,
126 .type = V4L2_CTRL_TYPE_INTEGER,
127 .name = "Gain",
128 .minimum = MR97310A_GAIN_MIN,
129 .maximum = MR97310A_GAIN_MAX,
130 .step = 1,
131 .default_value = MR97310A_GAIN_DEFAULT,
132 .flags = 0,
133 },
134 .set = sd_setgain,
135 .get = sd_getgain,
136 },
Kyle Guinnd661e622009-01-16 05:36:14 -0300137};
138
139static const struct v4l2_pix_format vga_mode[] = {
140 {160, 120, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
141 .bytesperline = 160,
142 .sizeimage = 160 * 120,
143 .colorspace = V4L2_COLORSPACE_SRGB,
144 .priv = 4},
145 {176, 144, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
146 .bytesperline = 176,
147 .sizeimage = 176 * 144,
148 .colorspace = V4L2_COLORSPACE_SRGB,
149 .priv = 3},
150 {320, 240, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
151 .bytesperline = 320,
152 .sizeimage = 320 * 240,
153 .colorspace = V4L2_COLORSPACE_SRGB,
154 .priv = 2},
155 {352, 288, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
156 .bytesperline = 352,
157 .sizeimage = 352 * 288,
158 .colorspace = V4L2_COLORSPACE_SRGB,
159 .priv = 1},
160 {640, 480, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
161 .bytesperline = 640,
162 .sizeimage = 640 * 480,
163 .colorspace = V4L2_COLORSPACE_SRGB,
164 .priv = 0},
165};
166
167/* the bytes to write are in gspca_dev->usb_buf */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300168static int mr_write(struct gspca_dev *gspca_dev, int len)
Kyle Guinnd661e622009-01-16 05:36:14 -0300169{
170 int rc;
171
172 rc = usb_bulk_msg(gspca_dev->dev,
173 usb_sndbulkpipe(gspca_dev->dev, 4),
Jean-Francois Moine92e8c912009-02-02 16:25:38 -0300174 gspca_dev->usb_buf, len, NULL, 500);
Kyle Guinnd661e622009-01-16 05:36:14 -0300175 if (rc < 0)
176 PDEBUG(D_ERR, "reg write [%02x] error %d",
177 gspca_dev->usb_buf[0], rc);
178 return rc;
179}
180
Theodore Kilgore89f08632009-08-14 06:51:52 -0300181/* the bytes are read into gspca_dev->usb_buf */
182static int mr_read(struct gspca_dev *gspca_dev, int len)
183{
184 int rc;
185
186 rc = usb_bulk_msg(gspca_dev->dev,
187 usb_rcvbulkpipe(gspca_dev->dev, 3),
188 gspca_dev->usb_buf, len, NULL, 500);
189 if (rc < 0)
190 PDEBUG(D_ERR, "reg read [%02x] error %d",
191 gspca_dev->usb_buf[0], rc);
192 return rc;
193}
194
195static int sensor_write_reg(struct gspca_dev *gspca_dev, u8 reg, u8 flags,
196 const u8 *data, int len)
197{
198 gspca_dev->usb_buf[0] = 0x1f;
199 gspca_dev->usb_buf[1] = flags;
200 gspca_dev->usb_buf[2] = reg;
201 memcpy(gspca_dev->usb_buf + 3, data, len);
202
203 return mr_write(gspca_dev, len + 3);
204}
205
206static int sensor_write_regs(struct gspca_dev *gspca_dev,
207 const struct sensor_w_data *data, int len)
208{
209 int i, rc;
210
211 for (i = 0; i < len; i++) {
212 rc = sensor_write_reg(gspca_dev, data[i].reg, data[i].flags,
213 data[i].data, data[i].len);
214 if (rc < 0)
215 return rc;
216 }
217
218 return 0;
219}
220
221static int sensor_write1(struct gspca_dev *gspca_dev, u8 reg, u8 data)
222{
223 u8 buf;
224 int rc;
225
226 buf = data;
227 rc = sensor_write_reg(gspca_dev, reg, 0x01, &buf, 1);
228 if (rc < 0)
229 return rc;
230
231 buf = 0x01;
232 rc = sensor_write_reg(gspca_dev, 0x13, 0x00, &buf, 1);
233 if (rc < 0)
234 return rc;
235
236 return 0;
237}
238
239static int cam_get_response16(struct gspca_dev *gspca_dev)
240{
241 __u8 *data = gspca_dev->usb_buf;
242 int err_code;
243
244 data[0] = 0x21;
245 err_code = mr_write(gspca_dev, 1);
246 if (err_code < 0)
247 return err_code;
248
249 err_code = mr_read(gspca_dev, 16);
250 return err_code;
251}
252
253static int zero_the_pointer(struct gspca_dev *gspca_dev)
254{
255 __u8 *data = gspca_dev->usb_buf;
256 int err_code;
257 u8 status = 0;
258 int tries = 0;
259
260 err_code = cam_get_response16(gspca_dev);
261 if (err_code < 0)
262 return err_code;
263
264 err_code = mr_write(gspca_dev, 1);
265 data[0] = 0x19;
266 data[1] = 0x51;
267 err_code = mr_write(gspca_dev, 2);
268 if (err_code < 0)
269 return err_code;
270
271 err_code = cam_get_response16(gspca_dev);
272 if (err_code < 0)
273 return err_code;
274
275 data[0] = 0x19;
276 data[1] = 0xba;
277 err_code = mr_write(gspca_dev, 2);
278 if (err_code < 0)
279 return err_code;
280
281 err_code = cam_get_response16(gspca_dev);
282 if (err_code < 0)
283 return err_code;
284
285 data[0] = 0x19;
286 data[1] = 0x00;
287 err_code = mr_write(gspca_dev, 2);
288 if (err_code < 0)
289 return err_code;
290
291 err_code = cam_get_response16(gspca_dev);
292 if (err_code < 0)
293 return err_code;
294
295 data[0] = 0x19;
296 data[1] = 0x00;
297 err_code = mr_write(gspca_dev, 2);
298 if (err_code < 0)
299 return err_code;
300
301 while (status != 0x0a && tries < 256) {
302 err_code = cam_get_response16(gspca_dev);
303 status = data[0];
304 tries++;
305 if (err_code < 0)
306 return err_code;
307 }
308 PDEBUG(D_ERR, "status is %02x", status);
309
310 tries = 0;
311 while (tries < 4) {
312 data[0] = 0x19;
313 data[1] = 0x00;
314 err_code = mr_write(gspca_dev, 2);
315 if (err_code < 0)
316 return err_code;
317
318 err_code = cam_get_response16(gspca_dev);
319 status = data[0];
320 tries++;
321 if (err_code < 0)
322 return err_code;
323 }
324 PDEBUG(D_ERR, "Read 16 bytes from camera");
325
326 data[0] = 0x19;
327 err_code = mr_write(gspca_dev, 1);
328 if (err_code < 0)
329 return err_code;
330
331 err_code = mr_read(gspca_dev, 16);
332 if (err_code < 0)
333 return err_code;
334
335 return 0;
336}
337
338static u8 get_sensor_id(struct gspca_dev *gspca_dev)
339{
340 int err_code;
341
342 gspca_dev->usb_buf[0] = 0x1e;
343 err_code = mr_write(gspca_dev, 1);
344 if (err_code < 0)
345 return err_code;
346
347 err_code = mr_read(gspca_dev, 16);
348 if (err_code < 0)
349 return err_code;
350
351 PDEBUG(D_ERR, "Read 16 bytes from camera");
352 PDEBUG(D_ERR, "Byte zero reported is %01x", gspca_dev->usb_buf[0]);
353
354 return gspca_dev->usb_buf[0];
355}
356
Kyle Guinnd661e622009-01-16 05:36:14 -0300357/* this function is called at probe time */
358static int sd_config(struct gspca_dev *gspca_dev,
359 const struct usb_device_id *id)
360{
Theodore Kilgore89f08632009-08-14 06:51:52 -0300361 struct sd *sd = (struct sd *) gspca_dev;
Kyle Guinnd661e622009-01-16 05:36:14 -0300362 struct cam *cam;
363
364 cam = &gspca_dev->cam;
365 cam->cam_mode = vga_mode;
366 cam->nmodes = ARRAY_SIZE(vga_mode);
Hans de Goede9ac69782009-08-14 10:15:52 -0300367
Theodore Kilgore89f08632009-08-14 06:51:52 -0300368 PDEBUG(D_PROBE,
369 "MR97310A camera detected"
370 " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
Hans de Goede9ac69782009-08-14 10:15:52 -0300371
Theodore Kilgore89f08632009-08-14 06:51:52 -0300372 if (id->idProduct == 0x010e) {
Theodore Kilgore89f08632009-08-14 06:51:52 -0300373 sd->cam_type = CAM_TYPE_CIF;
Hans de Goede9ac69782009-08-14 10:15:52 -0300374 cam->nmodes--;
375 } else {
376 sd->cam_type = CAM_TYPE_VGA;
377 gspca_dev->ctrl_dis = (1 << BRIGHTNESS_IDX) |
378 (1 << EXPOSURE_IDX) | (1 << GAIN_IDX);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300379 }
Hans de Goede9ac69782009-08-14 10:15:52 -0300380
381 sd->brightness = MR97310A_BRIGHTNESS_DEFAULT;
382 sd->exposure = MR97310A_EXPOSURE_DEFAULT;
383 sd->gain = MR97310A_GAIN_DEFAULT;
384
Kyle Guinnd661e622009-01-16 05:36:14 -0300385 return 0;
386}
387
388/* this function is called at probe and resume time */
389static int sd_init(struct gspca_dev *gspca_dev)
390{
391 return 0;
392}
393
Theodore Kilgore89f08632009-08-14 06:51:52 -0300394static int start_cif_cam(struct gspca_dev *gspca_dev)
Kyle Guinnd661e622009-01-16 05:36:14 -0300395{
396 struct sd *sd = (struct sd *) gspca_dev;
397 __u8 *data = gspca_dev->usb_buf;
398 int err_code;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300399 const __u8 startup_string[] = {
400 0x00,
401 0x0d,
402 0x01,
403 0x00, /* Hsize/8 for 352 or 320 */
404 0x00, /* Vsize/4 for 288 or 240 */
405 0x13, /* or 0xbb, depends on sensor */
406 0x00, /* Hstart, depends on res. */
407 0x00, /* reserved ? */
408 0x00, /* Vstart, depends on res. and sensor */
409 0x50, /* 0x54 to get 176 or 160 */
410 0xc0
411 };
Kyle Guinnd661e622009-01-16 05:36:14 -0300412
Theodore Kilgore89f08632009-08-14 06:51:52 -0300413 /* Note: Some of the above descriptions guessed from MR97113A driver */
414 sd->sensor_type = 0;
Kyle Guinnd661e622009-01-16 05:36:14 -0300415 data[0] = 0x01;
416 data[1] = 0x01;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300417 err_code = mr_write(gspca_dev, 2);
Kyle Guinnd661e622009-01-16 05:36:14 -0300418 if (err_code < 0)
419 return err_code;
420
Theodore Kilgore89f08632009-08-14 06:51:52 -0300421 msleep(200);
422 data[0] = get_sensor_id(gspca_dev);
423 /*
424 * Known CIF cameras. If you have another to report, please do
425 *
426 * Name byte just read sd->sensor_type
427 * reported by
428 * Sakar Spy-shot 0x28 T. Kilgore 0
429 * Innovage 0xf5 (unstable) T. Kilgore 0
430 * Vivitar Mini 0x53 H. De Goede 0
431 * Vivitar Mini 0x08 T. Kilgore 1
432 * Elta-Media 8212dc 0x23 T. Kaiser 1
433 * Philips dig. keych. 0x37 T. Kilgore 1
434 */
Hans de Goede58d0b2f2009-08-14 06:56:32 -0300435 if ((data[0] & 0x78) == 8 ||
436 ((data[0] & 0x2) == 0x2 && data[0] != 0x53))
Theodore Kilgore89f08632009-08-14 06:51:52 -0300437 sd->sensor_type = 1;
438
439 PDEBUG(D_ERR, "Sensor type is %01x", sd->sensor_type);
Hans de Goede9ac69782009-08-14 10:15:52 -0300440
441 if (sd->sensor_type == 0)
442 gspca_dev->ctrl_dis = (1 << BRIGHTNESS_IDX) |
443 (1 << EXPOSURE_IDX) | (1 << GAIN_IDX);
444
Theodore Kilgore89f08632009-08-14 06:51:52 -0300445 memcpy(data, startup_string, 11);
446 if (sd->sensor_type)
447 data[5] = 0xbb;
448
449 switch (gspca_dev->width) {
450 case 160:
451 data[9] |= 0x04; /* reg 8, 2:1 scale down from 320 */
452 /* fall thru */
453 case 320:
454 default:
455 data[3] = 0x28; /* reg 2, H size/8 */
456 data[4] = 0x3c; /* reg 3, V size/4 */
457 data[6] = 0x14; /* reg 5, H start */
458 data[8] = 0x1a + sd->sensor_type; /* reg 7, V start */
459 break;
460 case 176:
461 data[9] |= 0x04; /* reg 8, 2:1 scale down from 352 */
462 /* fall thru */
463 case 352:
464 data[3] = 0x2c; /* reg 2, H size/8 */
465 data[4] = 0x48; /* reg 3, V size/4 */
466 data[6] = 0x06; /* reg 5, H start */
467 data[8] = 0x06 + sd->sensor_type; /* reg 7, V start */
468 break;
469 }
470 err_code = mr_write(gspca_dev, 11);
471 if (err_code < 0)
472 return err_code;
473
474 if (!sd->sensor_type) {
475 const struct sensor_w_data cif_sensor0_init_data[] = {
476 {0x02, 0x00, {0x03, 0x5a, 0xb5, 0x01,
477 0x0f, 0x14, 0x0f, 0x10}, 8},
478 {0x0c, 0x00, {0x04, 0x01, 0x01, 0x00, 0x1f}, 5},
479 {0x12, 0x00, {0x07}, 1},
480 {0x1f, 0x00, {0x06}, 1},
481 {0x27, 0x00, {0x04}, 1},
482 {0x29, 0x00, {0x0c}, 1},
483 {0x40, 0x00, {0x40, 0x00, 0x04}, 3},
484 {0x50, 0x00, {0x60}, 1},
485 {0x60, 0x00, {0x06}, 1},
486 {0x6b, 0x00, {0x85, 0x85, 0xc8, 0xc8, 0xc8, 0xc8}, 6},
487 {0x72, 0x00, {0x1e, 0x56}, 2},
488 {0x75, 0x00, {0x58, 0x40, 0xa2, 0x02, 0x31, 0x02,
489 0x31, 0x80, 0x00}, 9},
490 {0x11, 0x00, {0x01}, 1},
491 {0, 0, {0}, 0}
492 };
493 err_code = sensor_write_regs(gspca_dev, cif_sensor0_init_data,
494 ARRAY_SIZE(cif_sensor0_init_data));
495 } else { /* sd->sensor_type = 1 */
496 const struct sensor_w_data cif_sensor1_init_data[] = {
Hans de Goede9ac69782009-08-14 10:15:52 -0300497 /* Reg 3,4, 7,8 get set by the controls */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300498 {0x02, 0x00, {0x10}, 1},
Hans de Goede9ac69782009-08-14 10:15:52 -0300499 {0x05, 0x01, {0x22}, 1}, /* 5/6 also seen as 65h/32h */
500 {0x06, 0x01, {0x00}, 1},
Theodore Kilgore89f08632009-08-14 06:51:52 -0300501 {0x09, 0x02, {0x0e}, 1},
502 {0x0a, 0x02, {0x05}, 1},
503 {0x0b, 0x02, {0x05}, 1},
504 {0x0c, 0x02, {0x0f}, 1},
Hans de Goede9ac69782009-08-14 10:15:52 -0300505 {0x0d, 0x02, {0x07}, 1},
Theodore Kilgore89f08632009-08-14 06:51:52 -0300506 {0x0e, 0x02, {0x0c}, 1},
507 {0x0f, 0x00, {0x00}, 1},
508 {0x10, 0x00, {0x06}, 1},
509 {0x11, 0x00, {0x07}, 1},
510 {0x12, 0x00, {0x00}, 1},
511 {0x13, 0x00, {0x01}, 1},
512 {0, 0, {0}, 0}
513 };
514 err_code = sensor_write_regs(gspca_dev, cif_sensor1_init_data,
515 ARRAY_SIZE(cif_sensor1_init_data));
516 }
517 if (err_code < 0)
518 return err_code;
519
Hans de Goede9ac69782009-08-14 10:15:52 -0300520 setbrightness(gspca_dev);
521 setexposure(gspca_dev);
522 setgain(gspca_dev);
523
Theodore Kilgore89f08632009-08-14 06:51:52 -0300524 msleep(200);
Hans de Goede9ac69782009-08-14 10:15:52 -0300525
Kyle Guinnd661e622009-01-16 05:36:14 -0300526 data[0] = 0x00;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300527 data[1] = 0x4d; /* ISOC transfering enable... */
528 err_code = mr_write(gspca_dev, 2);
529 if (err_code < 0)
530 return err_code;
531
Theodore Kilgore89f08632009-08-14 06:51:52 -0300532 return 0;
533}
534
535static int start_vga_cam(struct gspca_dev *gspca_dev)
536{
537 struct sd *sd = (struct sd *) gspca_dev;
538 __u8 *data = gspca_dev->usb_buf;
539 int err_code;
540 const __u8 startup_string[] = {0x00, 0x0d, 0x01, 0x00, 0x00, 0x2b,
541 0x00, 0x00, 0x00, 0x50, 0xc0};
542
543 /* What some of these mean is explained in start_cif_cam(), above */
544 sd->sof_read = 0;
545
546 /*
547 * We have to know which camera we have, because the register writes
548 * depend upon the camera. This test, run before we actually enter
549 * the initialization routine, distinguishes most of the cameras, If
550 * needed, another routine is done later, too.
551 */
552 memset(data, 0, 16);
553 data[0] = 0x20;
554 err_code = mr_write(gspca_dev, 1);
555 if (err_code < 0)
556 return err_code;
557
558 err_code = mr_read(gspca_dev, 16);
559 if (err_code < 0)
560 return err_code;
561
562 PDEBUG(D_ERR, "Read 16 bytes from camera");
563 PDEBUG(D_ERR, "Byte reported is %02x", data[0]);
564
565 msleep(200);
566 /*
567 * Known VGA cameras. If you have another to report, please do
568 *
569 * Name byte just read sd->sensor_type
570 * sd->do_lcd_stop
571 * Aiptek Pencam VGA+ 0x31 0 1
572 * ION digital 0x31 0 1
573 * Argus DC-1620 0x30 1 0
574 * Argus QuickClix 0x30 1 1 (not caught here)
575 */
576 sd->sensor_type = data[0] & 1;
577 sd->do_lcd_stop = (~data[0]) & 1;
578
579
580
581 /* Streaming setup begins here. */
582
583
584 data[0] = 0x01;
585 data[1] = 0x01;
586 err_code = mr_write(gspca_dev, 2);
587 if (err_code < 0)
588 return err_code;
589
590 /*
591 * A second test can now resolve any remaining ambiguity in the
592 * identification of the camera type,
593 */
594 if (!sd->sensor_type) {
595 data[0] = get_sensor_id(gspca_dev);
596 if (data[0] == 0x7f) {
597 sd->sensor_type = 1;
598 PDEBUG(D_ERR, "sensor_type corrected to 1");
599 }
600 msleep(200);
601 }
602
603 /*
604 * Known VGA cameras.
605 * This test is only run if the previous test returned 0x30, but
606 * here is the information for all others, too, just for reference.
607 *
608 * Name byte just read sd->sensor_type
609 *
610 * Aiptek Pencam VGA+ 0xfb (this test not run) 1
611 * ION digital 0xbd (this test not run) 1
612 * Argus DC-1620 0xe5 (no change) 0
613 * Argus QuickClix 0x7f (reclassified) 1
614 */
615 memcpy(data, startup_string, 11);
616 if (!sd->sensor_type) {
617 data[5] = 0x00;
618 data[10] = 0x91;
619 }
Kyle Guinnd661e622009-01-16 05:36:14 -0300620
621 switch (gspca_dev->width) {
622 case 160:
623 data[9] |= 0x0c; /* reg 8, 4:1 scale down */
624 /* fall thru */
625 case 320:
626 data[9] |= 0x04; /* reg 8, 2:1 scale down */
627 /* fall thru */
628 case 640:
629 default:
Theodore Kilgore89f08632009-08-14 06:51:52 -0300630 data[3] = 0x50; /* reg 2, H size/8 */
631 data[4] = 0x78; /* reg 3, V size/4 */
Kyle Guinnd661e622009-01-16 05:36:14 -0300632 data[6] = 0x04; /* reg 5, H start */
633 data[8] = 0x03; /* reg 7, V start */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300634 if (sd->do_lcd_stop)
635 data[8] = 0x04; /* Bayer tile shifted */
Kyle Guinnd661e622009-01-16 05:36:14 -0300636 break;
637
638 case 176:
639 data[9] |= 0x04; /* reg 8, 2:1 scale down */
640 /* fall thru */
641 case 352:
642 data[3] = 0x2c; /* reg 2, H size */
643 data[4] = 0x48; /* reg 3, V size */
644 data[6] = 0x94; /* reg 5, H start */
645 data[8] = 0x63; /* reg 7, V start */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300646 if (sd->do_lcd_stop)
647 data[8] = 0x64; /* Bayer tile shifted */
Kyle Guinnd661e622009-01-16 05:36:14 -0300648 break;
649 }
650
Theodore Kilgore89f08632009-08-14 06:51:52 -0300651 err_code = mr_write(gspca_dev, 11);
Kyle Guinnd661e622009-01-16 05:36:14 -0300652 if (err_code < 0)
653 return err_code;
654
Theodore Kilgore89f08632009-08-14 06:51:52 -0300655 if (!sd->sensor_type) {
656 /* The only known sensor_type 0 cam is the Argus DC-1620 */
657 const struct sensor_w_data vga_sensor0_init_data[] = {
658 {0x01, 0x00, {0x0c, 0x00, 0x04}, 3},
659 {0x14, 0x00, {0x01, 0xe4, 0x02, 0x84}, 4},
660 {0x20, 0x00, {0x00, 0x80, 0x00, 0x08}, 4},
661 {0x25, 0x00, {0x03, 0xa9, 0x80}, 3},
662 {0x30, 0x00, {0x30, 0x18, 0x10, 0x18}, 4},
663 {0, 0, {0}, 0}
664 };
665 err_code = sensor_write_regs(gspca_dev, vga_sensor0_init_data,
666 ARRAY_SIZE(vga_sensor0_init_data));
667 } else { /* sd->sensor_type = 1 */
668 const struct sensor_w_data vga_sensor1_init_data[] = {
669 {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
670 0x07, 0x00, 0x01}, 8},
671 {0x11, 0x04, {0x01}, 1},
672 /*{0x0a, 0x00, {0x00, 0x01, 0x00, 0x00, 0x01, */
673 {0x0a, 0x00, {0x01, 0x06, 0x00, 0x00, 0x01,
674 0x00, 0x0a}, 7},
675 {0x11, 0x04, {0x01}, 1},
676 {0x12, 0x00, {0x00, 0x63, 0x00, 0x70, 0x00, 0x00}, 6},
677 {0x11, 0x04, {0x01}, 1},
678 {0, 0, {0}, 0}
679 };
680 err_code = sensor_write_regs(gspca_dev, vga_sensor1_init_data,
681 ARRAY_SIZE(vga_sensor1_init_data));
682 }
Kyle Guinnd661e622009-01-16 05:36:14 -0300683 if (err_code < 0)
684 return err_code;
685
Theodore Kilgore89f08632009-08-14 06:51:52 -0300686 msleep(200);
Kyle Guinnd661e622009-01-16 05:36:14 -0300687 data[0] = 0x00;
688 data[1] = 0x4d; /* ISOC transfering enable... */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300689 err_code = mr_write(gspca_dev, 2);
690
691 return err_code;
692}
693
694static int sd_start(struct gspca_dev *gspca_dev)
695{
696 struct sd *sd = (struct sd *) gspca_dev;
697 int err_code;
698 struct cam *cam;
699
Theodore Kilgore89f08632009-08-14 06:51:52 -0300700 cam = &gspca_dev->cam;
701 sd->sof_read = 0;
702 /*
703 * Some of the supported cameras require the memory pointer to be
704 * set to 0, or else they will not stream.
705 */
706 zero_the_pointer(gspca_dev);
707 msleep(200);
708 if (sd->cam_type == CAM_TYPE_CIF) {
709 PDEBUG(D_ERR, "CIF camera");
710 err_code = start_cif_cam(gspca_dev);
711 } else {
712 PDEBUG(D_ERR, "VGA camera");
713 err_code = start_vga_cam(gspca_dev);
714 }
Kyle Guinnd661e622009-01-16 05:36:14 -0300715 return err_code;
716}
717
718static void sd_stopN(struct gspca_dev *gspca_dev)
719{
Theodore Kilgore89f08632009-08-14 06:51:52 -0300720 struct sd *sd = (struct sd *) gspca_dev;
Kyle Guinnd661e622009-01-16 05:36:14 -0300721 int result;
722
723 gspca_dev->usb_buf[0] = 1;
724 gspca_dev->usb_buf[1] = 0;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300725 result = mr_write(gspca_dev, 2);
Kyle Guinnd661e622009-01-16 05:36:14 -0300726 if (result < 0)
727 PDEBUG(D_ERR, "Camera Stop failed");
Theodore Kilgore89f08632009-08-14 06:51:52 -0300728
729 /* Not all the cams need this, but even if not, probably a good idea */
730 zero_the_pointer(gspca_dev);
731 if (sd->do_lcd_stop) {
732 gspca_dev->usb_buf[0] = 0x19;
733 gspca_dev->usb_buf[1] = 0x54;
734 result = mr_write(gspca_dev, 2);
735 if (result < 0)
736 PDEBUG(D_ERR, "Camera Stop failed");
737 }
738}
739
740static void setbrightness(struct gspca_dev *gspca_dev)
741{
742 struct sd *sd = (struct sd *) gspca_dev;
743 u8 val;
Hans de Goede9ac69782009-08-14 10:15:52 -0300744
745 if (gspca_dev->ctrl_dis & (1 << BRIGHTNESS_IDX))
746 return;
747
748 /* Note register 7 is also seen as 0x8x or 0xCx in dumps */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300749 if (sd->brightness > 0) {
Hans de Goede9ac69782009-08-14 10:15:52 -0300750 sensor_write1(gspca_dev, 7, 0x00);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300751 val = sd->brightness;
752 } else {
Hans de Goede9ac69782009-08-14 10:15:52 -0300753 sensor_write1(gspca_dev, 7, 0x01);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300754 val = 257 - sd->brightness;
755 }
756 sensor_write1(gspca_dev, 8, val);
757}
758
759static void setexposure(struct gspca_dev *gspca_dev)
760{
761 struct sd *sd = (struct sd *) gspca_dev;
762 u8 val;
763
Hans de Goede9ac69782009-08-14 10:15:52 -0300764 if (gspca_dev->ctrl_dis & (1 << EXPOSURE_IDX))
765 return;
766
Theodore Kilgore89f08632009-08-14 06:51:52 -0300767 val = sd->exposure >> 4;
768 sensor_write1(gspca_dev, 3, val);
769 val = sd->exposure & 0xf;
770 sensor_write1(gspca_dev, 4, val);
771}
772
773static void setgain(struct gspca_dev *gspca_dev)
774{
775 struct sd *sd = (struct sd *) gspca_dev;
776
Hans de Goede9ac69782009-08-14 10:15:52 -0300777 if (gspca_dev->ctrl_dis & (1 << GAIN_IDX))
778 return;
779
Theodore Kilgore89f08632009-08-14 06:51:52 -0300780 sensor_write1(gspca_dev, 3, sd->gain);
781}
782
783static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
784{
785 struct sd *sd = (struct sd *) gspca_dev;
786
787 sd->brightness = val;
788 if (gspca_dev->streaming)
789 setbrightness(gspca_dev);
790 return 0;
791}
792
793static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
794{
795 struct sd *sd = (struct sd *) gspca_dev;
796
797 *val = sd->brightness;
798 return 0;
799}
800
801static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
802{
803 struct sd *sd = (struct sd *) gspca_dev;
804
805 sd->exposure = val;
806 if (gspca_dev->streaming)
807 setexposure(gspca_dev);
808 return 0;
809}
810
811static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
812{
813 struct sd *sd = (struct sd *) gspca_dev;
814
815 *val = sd->exposure;
816 return 0;
817}
818
819static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
820{
821 struct sd *sd = (struct sd *) gspca_dev;
822
823 sd->gain = val;
824 if (gspca_dev->streaming)
825 setgain(gspca_dev);
826 return 0;
827}
828
829static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
830{
831 struct sd *sd = (struct sd *) gspca_dev;
832
833 *val = sd->gain;
834 return 0;
Kyle Guinnd661e622009-01-16 05:36:14 -0300835}
836
837/* Include pac common sof detection functions */
838#include "pac_common.h"
839
840static void sd_pkt_scan(struct gspca_dev *gspca_dev,
841 struct gspca_frame *frame, /* target */
842 __u8 *data, /* isoc packet */
843 int len) /* iso packet length */
844{
Kyle Guinnd661e622009-01-16 05:36:14 -0300845 unsigned char *sof;
846
847 sof = pac_find_sof(gspca_dev, data, len);
848 if (sof) {
849 int n;
850
851 /* finish decoding current frame */
852 n = sof - data;
853 if (n > sizeof pac_sof_marker)
854 n -= sizeof pac_sof_marker;
855 else
856 n = 0;
857 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
858 data, n);
Theodore Kilgore9832d762009-03-13 13:04:31 -0300859 /* Start next frame. */
860 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
861 pac_sof_marker, sizeof pac_sof_marker);
Kyle Guinnd661e622009-01-16 05:36:14 -0300862 len -= sof - data;
863 data = sof;
864 }
Kyle Guinnd661e622009-01-16 05:36:14 -0300865 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
866}
867
868/* sub-driver description */
869static const struct sd_desc sd_desc = {
870 .name = MODULE_NAME,
871 .ctrls = sd_ctrls,
872 .nctrls = ARRAY_SIZE(sd_ctrls),
873 .config = sd_config,
874 .init = sd_init,
875 .start = sd_start,
876 .stopN = sd_stopN,
877 .pkt_scan = sd_pkt_scan,
878};
879
880/* -- module initialisation -- */
881static const __devinitdata struct usb_device_id device_table[] = {
Theodore Kilgore89f08632009-08-14 06:51:52 -0300882 {USB_DEVICE(0x08ca, 0x0111)}, /* Aiptek Pencam VGA+ */
883 {USB_DEVICE(0x093a, 0x010f)}, /* All other known MR97310A VGA cams */
884 {USB_DEVICE(0x093a, 0x010e)}, /* All known MR97310A CIF cams */
Kyle Guinnd661e622009-01-16 05:36:14 -0300885 {}
886};
887MODULE_DEVICE_TABLE(usb, device_table);
888
889/* -- device connect -- */
890static int sd_probe(struct usb_interface *intf,
891 const struct usb_device_id *id)
892{
893 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
894 THIS_MODULE);
895}
896
897static struct usb_driver sd_driver = {
898 .name = MODULE_NAME,
899 .id_table = device_table,
900 .probe = sd_probe,
901 .disconnect = gspca_disconnect,
902#ifdef CONFIG_PM
903 .suspend = gspca_suspend,
904 .resume = gspca_resume,
905#endif
906};
907
908/* -- module insert / remove -- */
909static int __init sd_mod_init(void)
910{
Alexey Klimov5d3fa302009-03-27 15:57:46 -0300911 int ret;
912
913 ret = usb_register(&sd_driver);
914 if (ret < 0)
915 return ret;
Kyle Guinnd661e622009-01-16 05:36:14 -0300916 PDEBUG(D_PROBE, "registered");
917 return 0;
918}
919static void __exit sd_mod_exit(void)
920{
921 usb_deregister(&sd_driver);
922 PDEBUG(D_PROBE, "deregistered");
923}
924
925module_init(sd_mod_init);
926module_exit(sd_mod_exit);