blob: 1a6532fa8548ac101096eb3978fe824d0b3aa4f8 [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;
Hans de Goede5f5e26b2009-08-14 10:40:26 -0300363 __u8 *data = gspca_dev->usb_buf;
364 int err_code;
Kyle Guinnd661e622009-01-16 05:36:14 -0300365
366 cam = &gspca_dev->cam;
367 cam->cam_mode = vga_mode;
368 cam->nmodes = ARRAY_SIZE(vga_mode);
Hans de Goede9ac69782009-08-14 10:15:52 -0300369
Theodore Kilgore89f08632009-08-14 06:51:52 -0300370 PDEBUG(D_PROBE,
371 "MR97310A camera detected"
372 " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
Hans de Goede9ac69782009-08-14 10:15:52 -0300373
Theodore Kilgore89f08632009-08-14 06:51:52 -0300374 if (id->idProduct == 0x010e) {
Theodore Kilgore89f08632009-08-14 06:51:52 -0300375 sd->cam_type = CAM_TYPE_CIF;
Hans de Goede9ac69782009-08-14 10:15:52 -0300376 cam->nmodes--;
Hans de Goede5f5e26b2009-08-14 10:40:26 -0300377
378 data[0] = 0x01;
379 data[1] = 0x01;
380 err_code = mr_write(gspca_dev, 2);
381 if (err_code < 0)
382 return err_code;
383
384 msleep(200);
385 data[0] = get_sensor_id(gspca_dev);
386 /*
387 * Known CIF cameras. If you have another to report, please do
388 *
389 * Name byte just read sd->sensor_type
390 * reported by
391 * Sakar Spy-shot 0x28 T. Kilgore 0
392 * Innovage 0xf5 (unstable) T. Kilgore 0
393 * Vivitar Mini 0x53 H. De Goede 0
394 * Vivitar Mini 0x08 T. Kilgore 1
395 * Elta-Media 8212dc 0x23 T. Kaiser 1
396 * Philips dig. keych. 0x37 T. Kilgore 1
397 */
398 if ((data[0] & 0x78) == 8 ||
399 ((data[0] & 0x2) == 0x2 && data[0] != 0x53))
400 sd->sensor_type = 1;
401 else
402 sd->sensor_type = 0;
403
404 PDEBUG(D_ERR, "Sensor type is %01x", sd->sensor_type);
405
406 if (sd->sensor_type == 0)
407 gspca_dev->ctrl_dis = (1 << BRIGHTNESS_IDX) |
408 (1 << EXPOSURE_IDX) | (1 << GAIN_IDX);
Hans de Goede9ac69782009-08-14 10:15:52 -0300409 } else {
410 sd->cam_type = CAM_TYPE_VGA;
411 gspca_dev->ctrl_dis = (1 << BRIGHTNESS_IDX) |
412 (1 << EXPOSURE_IDX) | (1 << GAIN_IDX);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300413 }
Hans de Goede9ac69782009-08-14 10:15:52 -0300414
415 sd->brightness = MR97310A_BRIGHTNESS_DEFAULT;
416 sd->exposure = MR97310A_EXPOSURE_DEFAULT;
417 sd->gain = MR97310A_GAIN_DEFAULT;
418
Kyle Guinnd661e622009-01-16 05:36:14 -0300419 return 0;
420}
421
422/* this function is called at probe and resume time */
423static int sd_init(struct gspca_dev *gspca_dev)
424{
425 return 0;
426}
427
Theodore Kilgore89f08632009-08-14 06:51:52 -0300428static int start_cif_cam(struct gspca_dev *gspca_dev)
Kyle Guinnd661e622009-01-16 05:36:14 -0300429{
430 struct sd *sd = (struct sd *) gspca_dev;
431 __u8 *data = gspca_dev->usb_buf;
432 int err_code;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300433 const __u8 startup_string[] = {
434 0x00,
435 0x0d,
436 0x01,
437 0x00, /* Hsize/8 for 352 or 320 */
438 0x00, /* Vsize/4 for 288 or 240 */
439 0x13, /* or 0xbb, depends on sensor */
440 0x00, /* Hstart, depends on res. */
441 0x00, /* reserved ? */
442 0x00, /* Vstart, depends on res. and sensor */
443 0x50, /* 0x54 to get 176 or 160 */
444 0xc0
445 };
Kyle Guinnd661e622009-01-16 05:36:14 -0300446
Theodore Kilgore89f08632009-08-14 06:51:52 -0300447 /* Note: Some of the above descriptions guessed from MR97113A driver */
Kyle Guinnd661e622009-01-16 05:36:14 -0300448 data[0] = 0x01;
449 data[1] = 0x01;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300450 err_code = mr_write(gspca_dev, 2);
Kyle Guinnd661e622009-01-16 05:36:14 -0300451 if (err_code < 0)
452 return err_code;
453
Theodore Kilgore89f08632009-08-14 06:51:52 -0300454 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[] = {
Hans de Goede9ac69782009-08-14 10:15:52 -0300506 /* Reg 3,4, 7,8 get set by the controls */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300507 {0x02, 0x00, {0x10}, 1},
Hans de Goede9ac69782009-08-14 10:15:52 -0300508 {0x05, 0x01, {0x22}, 1}, /* 5/6 also seen as 65h/32h */
509 {0x06, 0x01, {0x00}, 1},
Theodore Kilgore89f08632009-08-14 06:51:52 -0300510 {0x09, 0x02, {0x0e}, 1},
511 {0x0a, 0x02, {0x05}, 1},
512 {0x0b, 0x02, {0x05}, 1},
513 {0x0c, 0x02, {0x0f}, 1},
Hans de Goede9ac69782009-08-14 10:15:52 -0300514 {0x0d, 0x02, {0x07}, 1},
Theodore Kilgore89f08632009-08-14 06:51:52 -0300515 {0x0e, 0x02, {0x0c}, 1},
516 {0x0f, 0x00, {0x00}, 1},
517 {0x10, 0x00, {0x06}, 1},
518 {0x11, 0x00, {0x07}, 1},
519 {0x12, 0x00, {0x00}, 1},
520 {0x13, 0x00, {0x01}, 1},
521 {0, 0, {0}, 0}
522 };
523 err_code = sensor_write_regs(gspca_dev, cif_sensor1_init_data,
524 ARRAY_SIZE(cif_sensor1_init_data));
525 }
526 if (err_code < 0)
527 return err_code;
528
Hans de Goede9ac69782009-08-14 10:15:52 -0300529 setbrightness(gspca_dev);
530 setexposure(gspca_dev);
531 setgain(gspca_dev);
532
Theodore Kilgore89f08632009-08-14 06:51:52 -0300533 msleep(200);
Hans de Goede9ac69782009-08-14 10:15:52 -0300534
Kyle Guinnd661e622009-01-16 05:36:14 -0300535 data[0] = 0x00;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300536 data[1] = 0x4d; /* ISOC transfering enable... */
537 err_code = mr_write(gspca_dev, 2);
538 if (err_code < 0)
539 return err_code;
540
Theodore Kilgore89f08632009-08-14 06:51:52 -0300541 return 0;
542}
543
544static int start_vga_cam(struct gspca_dev *gspca_dev)
545{
546 struct sd *sd = (struct sd *) gspca_dev;
547 __u8 *data = gspca_dev->usb_buf;
548 int err_code;
549 const __u8 startup_string[] = {0x00, 0x0d, 0x01, 0x00, 0x00, 0x2b,
550 0x00, 0x00, 0x00, 0x50, 0xc0};
551
552 /* What some of these mean is explained in start_cif_cam(), above */
553 sd->sof_read = 0;
554
555 /*
556 * We have to know which camera we have, because the register writes
557 * depend upon the camera. This test, run before we actually enter
558 * the initialization routine, distinguishes most of the cameras, If
559 * needed, another routine is done later, too.
560 */
561 memset(data, 0, 16);
562 data[0] = 0x20;
563 err_code = mr_write(gspca_dev, 1);
564 if (err_code < 0)
565 return err_code;
566
567 err_code = mr_read(gspca_dev, 16);
568 if (err_code < 0)
569 return err_code;
570
571 PDEBUG(D_ERR, "Read 16 bytes from camera");
572 PDEBUG(D_ERR, "Byte reported is %02x", data[0]);
573
574 msleep(200);
575 /*
576 * Known VGA cameras. If you have another to report, please do
577 *
578 * Name byte just read sd->sensor_type
579 * sd->do_lcd_stop
580 * Aiptek Pencam VGA+ 0x31 0 1
581 * ION digital 0x31 0 1
582 * Argus DC-1620 0x30 1 0
583 * Argus QuickClix 0x30 1 1 (not caught here)
584 */
585 sd->sensor_type = data[0] & 1;
586 sd->do_lcd_stop = (~data[0]) & 1;
587
588
589
590 /* Streaming setup begins here. */
591
592
593 data[0] = 0x01;
594 data[1] = 0x01;
595 err_code = mr_write(gspca_dev, 2);
596 if (err_code < 0)
597 return err_code;
598
599 /*
600 * A second test can now resolve any remaining ambiguity in the
601 * identification of the camera type,
602 */
603 if (!sd->sensor_type) {
604 data[0] = get_sensor_id(gspca_dev);
605 if (data[0] == 0x7f) {
606 sd->sensor_type = 1;
607 PDEBUG(D_ERR, "sensor_type corrected to 1");
608 }
609 msleep(200);
610 }
611
612 /*
613 * Known VGA cameras.
614 * This test is only run if the previous test returned 0x30, but
615 * here is the information for all others, too, just for reference.
616 *
617 * Name byte just read sd->sensor_type
618 *
619 * Aiptek Pencam VGA+ 0xfb (this test not run) 1
620 * ION digital 0xbd (this test not run) 1
621 * Argus DC-1620 0xe5 (no change) 0
622 * Argus QuickClix 0x7f (reclassified) 1
623 */
624 memcpy(data, startup_string, 11);
625 if (!sd->sensor_type) {
626 data[5] = 0x00;
627 data[10] = 0x91;
628 }
Kyle Guinnd661e622009-01-16 05:36:14 -0300629
630 switch (gspca_dev->width) {
631 case 160:
632 data[9] |= 0x0c; /* reg 8, 4:1 scale down */
633 /* fall thru */
634 case 320:
635 data[9] |= 0x04; /* reg 8, 2:1 scale down */
636 /* fall thru */
637 case 640:
638 default:
Theodore Kilgore89f08632009-08-14 06:51:52 -0300639 data[3] = 0x50; /* reg 2, H size/8 */
640 data[4] = 0x78; /* reg 3, V size/4 */
Kyle Guinnd661e622009-01-16 05:36:14 -0300641 data[6] = 0x04; /* reg 5, H start */
642 data[8] = 0x03; /* reg 7, V start */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300643 if (sd->do_lcd_stop)
644 data[8] = 0x04; /* Bayer tile shifted */
Kyle Guinnd661e622009-01-16 05:36:14 -0300645 break;
646
647 case 176:
648 data[9] |= 0x04; /* reg 8, 2:1 scale down */
649 /* fall thru */
650 case 352:
651 data[3] = 0x2c; /* reg 2, H size */
652 data[4] = 0x48; /* reg 3, V size */
653 data[6] = 0x94; /* reg 5, H start */
654 data[8] = 0x63; /* reg 7, V start */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300655 if (sd->do_lcd_stop)
656 data[8] = 0x64; /* Bayer tile shifted */
Kyle Guinnd661e622009-01-16 05:36:14 -0300657 break;
658 }
659
Theodore Kilgore89f08632009-08-14 06:51:52 -0300660 err_code = mr_write(gspca_dev, 11);
Kyle Guinnd661e622009-01-16 05:36:14 -0300661 if (err_code < 0)
662 return err_code;
663
Theodore Kilgore89f08632009-08-14 06:51:52 -0300664 if (!sd->sensor_type) {
665 /* The only known sensor_type 0 cam is the Argus DC-1620 */
666 const struct sensor_w_data vga_sensor0_init_data[] = {
667 {0x01, 0x00, {0x0c, 0x00, 0x04}, 3},
668 {0x14, 0x00, {0x01, 0xe4, 0x02, 0x84}, 4},
669 {0x20, 0x00, {0x00, 0x80, 0x00, 0x08}, 4},
670 {0x25, 0x00, {0x03, 0xa9, 0x80}, 3},
671 {0x30, 0x00, {0x30, 0x18, 0x10, 0x18}, 4},
672 {0, 0, {0}, 0}
673 };
674 err_code = sensor_write_regs(gspca_dev, vga_sensor0_init_data,
675 ARRAY_SIZE(vga_sensor0_init_data));
676 } else { /* sd->sensor_type = 1 */
677 const struct sensor_w_data vga_sensor1_init_data[] = {
678 {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
679 0x07, 0x00, 0x01}, 8},
680 {0x11, 0x04, {0x01}, 1},
681 /*{0x0a, 0x00, {0x00, 0x01, 0x00, 0x00, 0x01, */
682 {0x0a, 0x00, {0x01, 0x06, 0x00, 0x00, 0x01,
683 0x00, 0x0a}, 7},
684 {0x11, 0x04, {0x01}, 1},
685 {0x12, 0x00, {0x00, 0x63, 0x00, 0x70, 0x00, 0x00}, 6},
686 {0x11, 0x04, {0x01}, 1},
687 {0, 0, {0}, 0}
688 };
689 err_code = sensor_write_regs(gspca_dev, vga_sensor1_init_data,
690 ARRAY_SIZE(vga_sensor1_init_data));
691 }
Kyle Guinnd661e622009-01-16 05:36:14 -0300692 if (err_code < 0)
693 return err_code;
694
Theodore Kilgore89f08632009-08-14 06:51:52 -0300695 msleep(200);
Kyle Guinnd661e622009-01-16 05:36:14 -0300696 data[0] = 0x00;
697 data[1] = 0x4d; /* ISOC transfering enable... */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300698 err_code = mr_write(gspca_dev, 2);
699
700 return err_code;
701}
702
703static int sd_start(struct gspca_dev *gspca_dev)
704{
705 struct sd *sd = (struct sd *) gspca_dev;
706 int err_code;
707 struct cam *cam;
708
Theodore Kilgore89f08632009-08-14 06:51:52 -0300709 cam = &gspca_dev->cam;
710 sd->sof_read = 0;
711 /*
712 * Some of the supported cameras require the memory pointer to be
713 * set to 0, or else they will not stream.
714 */
715 zero_the_pointer(gspca_dev);
716 msleep(200);
717 if (sd->cam_type == CAM_TYPE_CIF) {
718 PDEBUG(D_ERR, "CIF camera");
719 err_code = start_cif_cam(gspca_dev);
720 } else {
721 PDEBUG(D_ERR, "VGA camera");
722 err_code = start_vga_cam(gspca_dev);
723 }
Kyle Guinnd661e622009-01-16 05:36:14 -0300724 return err_code;
725}
726
727static void sd_stopN(struct gspca_dev *gspca_dev)
728{
Theodore Kilgore89f08632009-08-14 06:51:52 -0300729 struct sd *sd = (struct sd *) gspca_dev;
Kyle Guinnd661e622009-01-16 05:36:14 -0300730 int result;
731
732 gspca_dev->usb_buf[0] = 1;
733 gspca_dev->usb_buf[1] = 0;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300734 result = mr_write(gspca_dev, 2);
Kyle Guinnd661e622009-01-16 05:36:14 -0300735 if (result < 0)
736 PDEBUG(D_ERR, "Camera Stop failed");
Theodore Kilgore89f08632009-08-14 06:51:52 -0300737
738 /* Not all the cams need this, but even if not, probably a good idea */
739 zero_the_pointer(gspca_dev);
740 if (sd->do_lcd_stop) {
741 gspca_dev->usb_buf[0] = 0x19;
742 gspca_dev->usb_buf[1] = 0x54;
743 result = mr_write(gspca_dev, 2);
744 if (result < 0)
745 PDEBUG(D_ERR, "Camera Stop failed");
746 }
747}
748
749static void setbrightness(struct gspca_dev *gspca_dev)
750{
751 struct sd *sd = (struct sd *) gspca_dev;
752 u8 val;
Hans de Goede9ac69782009-08-14 10:15:52 -0300753
754 if (gspca_dev->ctrl_dis & (1 << BRIGHTNESS_IDX))
755 return;
756
757 /* Note register 7 is also seen as 0x8x or 0xCx in dumps */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300758 if (sd->brightness > 0) {
Hans de Goede9ac69782009-08-14 10:15:52 -0300759 sensor_write1(gspca_dev, 7, 0x00);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300760 val = sd->brightness;
761 } else {
Hans de Goede9ac69782009-08-14 10:15:52 -0300762 sensor_write1(gspca_dev, 7, 0x01);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300763 val = 257 - sd->brightness;
764 }
765 sensor_write1(gspca_dev, 8, val);
766}
767
768static void setexposure(struct gspca_dev *gspca_dev)
769{
770 struct sd *sd = (struct sd *) gspca_dev;
771 u8 val;
772
Hans de Goede9ac69782009-08-14 10:15:52 -0300773 if (gspca_dev->ctrl_dis & (1 << EXPOSURE_IDX))
774 return;
775
Theodore Kilgore89f08632009-08-14 06:51:52 -0300776 val = sd->exposure >> 4;
777 sensor_write1(gspca_dev, 3, val);
778 val = sd->exposure & 0xf;
779 sensor_write1(gspca_dev, 4, val);
780}
781
782static void setgain(struct gspca_dev *gspca_dev)
783{
784 struct sd *sd = (struct sd *) gspca_dev;
785
Hans de Goede9ac69782009-08-14 10:15:52 -0300786 if (gspca_dev->ctrl_dis & (1 << GAIN_IDX))
787 return;
788
Theodore Kilgore89f08632009-08-14 06:51:52 -0300789 sensor_write1(gspca_dev, 3, sd->gain);
790}
791
792static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
793{
794 struct sd *sd = (struct sd *) gspca_dev;
795
796 sd->brightness = val;
797 if (gspca_dev->streaming)
798 setbrightness(gspca_dev);
799 return 0;
800}
801
802static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
803{
804 struct sd *sd = (struct sd *) gspca_dev;
805
806 *val = sd->brightness;
807 return 0;
808}
809
810static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
811{
812 struct sd *sd = (struct sd *) gspca_dev;
813
814 sd->exposure = val;
815 if (gspca_dev->streaming)
816 setexposure(gspca_dev);
817 return 0;
818}
819
820static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
821{
822 struct sd *sd = (struct sd *) gspca_dev;
823
824 *val = sd->exposure;
825 return 0;
826}
827
828static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
829{
830 struct sd *sd = (struct sd *) gspca_dev;
831
832 sd->gain = val;
833 if (gspca_dev->streaming)
834 setgain(gspca_dev);
835 return 0;
836}
837
838static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
839{
840 struct sd *sd = (struct sd *) gspca_dev;
841
842 *val = sd->gain;
843 return 0;
Kyle Guinnd661e622009-01-16 05:36:14 -0300844}
845
846/* Include pac common sof detection functions */
847#include "pac_common.h"
848
849static void sd_pkt_scan(struct gspca_dev *gspca_dev,
850 struct gspca_frame *frame, /* target */
851 __u8 *data, /* isoc packet */
852 int len) /* iso packet length */
853{
Kyle Guinnd661e622009-01-16 05:36:14 -0300854 unsigned char *sof;
855
856 sof = pac_find_sof(gspca_dev, data, len);
857 if (sof) {
858 int n;
859
860 /* finish decoding current frame */
861 n = sof - data;
862 if (n > sizeof pac_sof_marker)
863 n -= sizeof pac_sof_marker;
864 else
865 n = 0;
866 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
867 data, n);
Theodore Kilgore9832d762009-03-13 13:04:31 -0300868 /* Start next frame. */
869 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
870 pac_sof_marker, sizeof pac_sof_marker);
Kyle Guinnd661e622009-01-16 05:36:14 -0300871 len -= sof - data;
872 data = sof;
873 }
Kyle Guinnd661e622009-01-16 05:36:14 -0300874 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
875}
876
877/* sub-driver description */
878static const struct sd_desc sd_desc = {
879 .name = MODULE_NAME,
880 .ctrls = sd_ctrls,
881 .nctrls = ARRAY_SIZE(sd_ctrls),
882 .config = sd_config,
883 .init = sd_init,
884 .start = sd_start,
885 .stopN = sd_stopN,
886 .pkt_scan = sd_pkt_scan,
887};
888
889/* -- module initialisation -- */
890static const __devinitdata struct usb_device_id device_table[] = {
Theodore Kilgore89f08632009-08-14 06:51:52 -0300891 {USB_DEVICE(0x08ca, 0x0111)}, /* Aiptek Pencam VGA+ */
892 {USB_DEVICE(0x093a, 0x010f)}, /* All other known MR97310A VGA cams */
893 {USB_DEVICE(0x093a, 0x010e)}, /* All known MR97310A CIF cams */
Kyle Guinnd661e622009-01-16 05:36:14 -0300894 {}
895};
896MODULE_DEVICE_TABLE(usb, device_table);
897
898/* -- device connect -- */
899static int sd_probe(struct usb_interface *intf,
900 const struct usb_device_id *id)
901{
902 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
903 THIS_MODULE);
904}
905
906static struct usb_driver sd_driver = {
907 .name = MODULE_NAME,
908 .id_table = device_table,
909 .probe = sd_probe,
910 .disconnect = gspca_disconnect,
911#ifdef CONFIG_PM
912 .suspend = gspca_suspend,
913 .resume = gspca_resume,
914#endif
915};
916
917/* -- module insert / remove -- */
918static int __init sd_mod_init(void)
919{
Alexey Klimov5d3fa302009-03-27 15:57:46 -0300920 int ret;
921
922 ret = usb_register(&sd_driver);
923 if (ret < 0)
924 return ret;
Kyle Guinnd661e622009-01-16 05:36:14 -0300925 PDEBUG(D_PROBE, "registered");
926 return 0;
927}
928static void __exit sd_mod_exit(void)
929{
930 usb_deregister(&sd_driver);
931 PDEBUG(D_PROBE, "deregistered");
932}
933
934module_init(sd_mod_init);
935module_exit(sd_mod_exit);