blob: f4c83b367900762ab5499800e16236e27892bf09 [file] [log] [blame]
Kyle Guinnd661e622009-01-16 05:36:14 -03001/*
2 * Mars MR97310A library
3 *
Theodore Kilgore930bf782009-10-05 05:11:35 -03004 * The original mr97310a driver, which supported the Aiptek Pencam VGA+, is
Kyle Guinnd661e622009-01-16 05:36:14 -03005 * Copyright (C) 2009 Kyle Guinn <elyk03@gmail.com>
6 *
Theodore Kilgore89f08632009-08-14 06:51:52 -03007 * Support for the MR97310A cameras in addition to the Aiptek Pencam VGA+
8 * and for the routines for detecting and classifying these various cameras,
Theodore Kilgore930bf782009-10-05 05:11:35 -03009 * is Copyright (C) 2009 Theodore Kilgore <kilgota@auburn.edu>
Theodore Kilgore89f08632009-08-14 06:51:52 -030010 *
Theodore Kilgore930bf782009-10-05 05:11:35 -030011 * 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>
14 *
15 * Support for the control settings for the VGA cameras is
Theodore Kilgore89f08632009-08-14 06:51:52 -030016 * Copyright (C) 2009 Theodore Kilgore <kilgota@auburn.edu>
17 *
Theodore Kilgore930bf782009-10-05 05:11:35 -030018 * 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>
Theodore Kilgore89f08632009-08-14 06:51:52 -030022 *
23 * The MR97311A support in gspca/mars.c has been helpful in understanding some
24 * of the registers in these cameras.
25 *
Kyle Guinnd661e622009-01-16 05:36:14 -030026 * 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.
30 *
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.
35 *
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
39 */
40
41#define MODULE_NAME "mr97310a"
42
43#include "gspca.h"
44
Theodore Kilgore89f08632009-08-14 06:51:52 -030045#define CAM_TYPE_CIF 0
46#define CAM_TYPE_VGA 1
47
Theodore Kilgore89f08632009-08-14 06:51:52 -030048#define MR97310A_BRIGHTNESS_DEFAULT 0
49
Theodore Kilgore930bf782009-10-05 05:11:35 -030050#define MR97310A_EXPOSURE_MIN 0
Theodore Kilgore89f08632009-08-14 06:51:52 -030051#define MR97310A_EXPOSURE_MAX 4095
52#define MR97310A_EXPOSURE_DEFAULT 1000
53
54#define MR97310A_GAIN_MIN 0
55#define MR97310A_GAIN_MAX 31
56#define MR97310A_GAIN_DEFAULT 25
57
58MODULE_AUTHOR("Kyle Guinn <elyk03@gmail.com>,"
59 "Theodore Kilgore <kilgota@auburn.edu>");
Kyle Guinnd661e622009-01-16 05:36:14 -030060MODULE_DESCRIPTION("GSPCA/Mars-Semi MR97310A USB Camera Driver");
61MODULE_LICENSE("GPL");
62
Hans de Goede78028702009-09-02 09:55:16 -030063/* global parameters */
64int force_sensor_type = -1;
65module_param(force_sensor_type, int, 0644);
66MODULE_PARM_DESC(force_sensor_type, "Force sensor type (-1 (auto), 0 or 1)");
67
Kyle Guinnd661e622009-01-16 05:36:14 -030068/* specific webcam descriptor */
69struct sd {
70 struct gspca_dev gspca_dev; /* !! must be the first item */
Kyle Guinnd661e622009-01-16 05:36:14 -030071 u8 sof_read;
Theodore Kilgore89f08632009-08-14 06:51:52 -030072 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;
Theodore Kilgore89f08632009-08-14 06:51:52 -030075
76 int brightness;
77 u16 exposure;
Theodore Kilgore89f08632009-08-14 06:51:52 -030078 u8 gain;
Kyle Guinnd661e622009-01-16 05:36:14 -030079};
80
Theodore Kilgore89f08632009-08-14 06:51:52 -030081struct sensor_w_data {
82 u8 reg;
83 u8 flags;
84 u8 data[16];
85 int len;
86};
87
Theodore Kilgore930bf782009-10-05 05:11:35 -030088static void sd_stopN(struct gspca_dev *gspca_dev);
Theodore Kilgore89f08632009-08-14 06:51:52 -030089static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
90static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
91static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
92static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
93static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
94static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
Hans de Goede9ac69782009-08-14 10:15:52 -030095static void setbrightness(struct gspca_dev *gspca_dev);
96static void setexposure(struct gspca_dev *gspca_dev);
97static void setgain(struct gspca_dev *gspca_dev);
Theodore Kilgore89f08632009-08-14 06:51:52 -030098
Kyle Guinnd661e622009-01-16 05:36:14 -030099/* V4L2 controls supported by the driver */
100static struct ctrl sd_ctrls[] = {
Theodore Kilgore930bf782009-10-05 05:11:35 -0300101/* Seprate brightness control description for Argus QuickClix as it has
102 different limits from to other mr97310a camera's */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300103 {
Theodore Kilgore930bf782009-10-05 05:11:35 -0300104#define NORM_BRIGHTNESS_IDX 0
Theodore Kilgore89f08632009-08-14 06:51:52 -0300105 {
106 .id = V4L2_CID_BRIGHTNESS,
107 .type = V4L2_CTRL_TYPE_INTEGER,
108 .name = "Brightness",
Theodore Kilgore930bf782009-10-05 05:11:35 -0300109 .minimum = -254,
110 .maximum = 255,
Theodore Kilgore89f08632009-08-14 06:51:52 -0300111 .step = 1,
112 .default_value = MR97310A_BRIGHTNESS_DEFAULT,
113 .flags = 0,
114 },
115 .set = sd_setbrightness,
116 .get = sd_getbrightness,
117 },
118 {
Theodore Kilgore930bf782009-10-05 05:11:35 -0300119#define ARGUS_QC_BRIGHTNESS_IDX 1
120 {
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,
129 },
130 .set = sd_setbrightness,
131 .get = sd_getbrightness,
132 },
133 {
134#define EXPOSURE_IDX 2
Theodore Kilgore89f08632009-08-14 06:51:52 -0300135 {
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,
144 },
145 .set = sd_setexposure,
146 .get = sd_getexposure,
147 },
148 {
Theodore Kilgore930bf782009-10-05 05:11:35 -0300149#define GAIN_IDX 3
Theodore Kilgore89f08632009-08-14 06:51:52 -0300150 {
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,
159 },
160 .set = sd_setgain,
161 .get = sd_getgain,
162 },
Kyle Guinnd661e622009-01-16 05:36:14 -0300163};
164
165static 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},
191};
192
193/* the bytes to write are in gspca_dev->usb_buf */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300194static int mr_write(struct gspca_dev *gspca_dev, int len)
Kyle Guinnd661e622009-01-16 05:36:14 -0300195{
196 int rc;
197
198 rc = usb_bulk_msg(gspca_dev->dev,
199 usb_sndbulkpipe(gspca_dev->dev, 4),
Jean-Francois Moine92e8c912009-02-02 16:25:38 -0300200 gspca_dev->usb_buf, len, NULL, 500);
Kyle Guinnd661e622009-01-16 05:36:14 -0300201 if (rc < 0)
202 PDEBUG(D_ERR, "reg write [%02x] error %d",
203 gspca_dev->usb_buf[0], rc);
204 return rc;
205}
206
Theodore Kilgore89f08632009-08-14 06:51:52 -0300207/* the bytes are read into gspca_dev->usb_buf */
208static int mr_read(struct gspca_dev *gspca_dev, int len)
209{
210 int rc;
211
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;
219}
220
221static int sensor_write_reg(struct gspca_dev *gspca_dev, u8 reg, u8 flags,
222 const u8 *data, int len)
223{
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);
228
229 return mr_write(gspca_dev, len + 3);
230}
231
232static int sensor_write_regs(struct gspca_dev *gspca_dev,
233 const struct sensor_w_data *data, int len)
234{
235 int i, rc;
236
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;
242 }
243
244 return 0;
245}
246
247static int sensor_write1(struct gspca_dev *gspca_dev, u8 reg, u8 data)
248{
Hans de Goedea2e081b2009-08-14 17:11:36 -0300249 struct sd *sd = (struct sd *) gspca_dev;
250 u8 buf, confirm_reg;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300251 int rc;
252
253 buf = data;
Theodore Kilgore930bf782009-10-05 05:11:35 -0300254 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;
260 }
Theodore Kilgore89f08632009-08-14 06:51:52 -0300261 if (rc < 0)
262 return rc;
263
264 buf = 0x01;
Hans de Goedea2e081b2009-08-14 17:11:36 -0300265 rc = sensor_write_reg(gspca_dev, confirm_reg, 0x00, &buf, 1);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300266 if (rc < 0)
267 return rc;
268
269 return 0;
270}
271
Theodore Kilgore930bf782009-10-05 05:11:35 -0300272static int cam_get_response16(struct gspca_dev *gspca_dev, u8 reg, int verbose)
Theodore Kilgore89f08632009-08-14 06:51:52 -0300273{
Theodore Kilgore89f08632009-08-14 06:51:52 -0300274 int err_code;
275
Theodore Kilgore930bf782009-10-05 05:11:35 -0300276 gspca_dev->usb_buf[0] = reg;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300277 err_code = mr_write(gspca_dev, 1);
278 if (err_code < 0)
279 return err_code;
280
281 err_code = mr_read(gspca_dev, 16);
Theodore Kilgore930bf782009-10-05 05:11:35 -0300282 if (err_code < 0)
283 return err_code;
284
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]);
290
291 return 0;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300292}
293
294static int zero_the_pointer(struct gspca_dev *gspca_dev)
295{
296 __u8 *data = gspca_dev->usb_buf;
297 int err_code;
298 u8 status = 0;
299 int tries = 0;
300
Theodore Kilgore930bf782009-10-05 05:11:35 -0300301 err_code = cam_get_response16(gspca_dev, 0x21, 0);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300302 if (err_code < 0)
303 return err_code;
304
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;
311
Theodore Kilgore930bf782009-10-05 05:11:35 -0300312 err_code = cam_get_response16(gspca_dev, 0x21, 0);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300313 if (err_code < 0)
314 return err_code;
315
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;
321
Theodore Kilgore930bf782009-10-05 05:11:35 -0300322 err_code = cam_get_response16(gspca_dev, 0x21, 0);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300323 if (err_code < 0)
324 return err_code;
325
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;
331
Theodore Kilgore930bf782009-10-05 05:11:35 -0300332 err_code = cam_get_response16(gspca_dev, 0x21, 0);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300333 if (err_code < 0)
334 return err_code;
335
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;
341
342 while (status != 0x0a && tries < 256) {
Theodore Kilgore930bf782009-10-05 05:11:35 -0300343 err_code = cam_get_response16(gspca_dev, 0x21, 0);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300344 status = data[0];
345 tries++;
346 if (err_code < 0)
347 return err_code;
348 }
Hans de Goede54943782009-08-14 11:05:38 -0300349 if (status != 0x0a)
350 PDEBUG(D_ERR, "status is %02x", status);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300351
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;
359
Theodore Kilgore930bf782009-10-05 05:11:35 -0300360 err_code = cam_get_response16(gspca_dev, 0x21, 0);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300361 status = data[0];
362 tries++;
363 if (err_code < 0)
364 return err_code;
365 }
Theodore Kilgore89f08632009-08-14 06:51:52 -0300366
367 data[0] = 0x19;
368 err_code = mr_write(gspca_dev, 1);
369 if (err_code < 0)
370 return err_code;
371
372 err_code = mr_read(gspca_dev, 16);
373 if (err_code < 0)
374 return err_code;
375
376 return 0;
377}
378
Theodore Kilgore930bf782009-10-05 05:11:35 -0300379static int stream_start(struct gspca_dev *gspca_dev)
Theodore Kilgore89f08632009-08-14 06:51:52 -0300380{
Theodore Kilgore930bf782009-10-05 05:11:35 -0300381 gspca_dev->usb_buf[0] = 0x01;
382 gspca_dev->usb_buf[1] = 0x01;
383 return mr_write(gspca_dev, 2);
384}
Theodore Kilgore89f08632009-08-14 06:51:52 -0300385
Theodore Kilgore930bf782009-10-05 05:11:35 -0300386static void stream_stop(struct gspca_dev *gspca_dev)
387{
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");
392}
Theodore Kilgore89f08632009-08-14 06:51:52 -0300393
Theodore Kilgore930bf782009-10-05 05:11:35 -0300394static void lcd_stop(struct gspca_dev *gspca_dev)
395{
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");
400}
Theodore Kilgore89f08632009-08-14 06:51:52 -0300401
Theodore Kilgore930bf782009-10-05 05:11:35 -0300402static int isoc_enable(struct gspca_dev *gspca_dev)
403{
404 gspca_dev->usb_buf[0] = 0x00;
405 gspca_dev->usb_buf[1] = 0x4d; /* ISOC transfering enable... */
406 return mr_write(gspca_dev, 2);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300407}
408
Kyle Guinnd661e622009-01-16 05:36:14 -0300409/* this function is called at probe time */
410static int sd_config(struct gspca_dev *gspca_dev,
411 const struct usb_device_id *id)
412{
Theodore Kilgore89f08632009-08-14 06:51:52 -0300413 struct sd *sd = (struct sd *) gspca_dev;
Kyle Guinnd661e622009-01-16 05:36:14 -0300414 struct cam *cam;
Hans de Goede5f5e26b2009-08-14 10:40:26 -0300415 int err_code;
Kyle Guinnd661e622009-01-16 05:36:14 -0300416
417 cam = &gspca_dev->cam;
418 cam->cam_mode = vga_mode;
419 cam->nmodes = ARRAY_SIZE(vga_mode);
Theodore Kilgore930bf782009-10-05 05:11:35 -0300420 sd->do_lcd_stop = 0;
421
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.
429 */
430 err_code = zero_the_pointer(gspca_dev);
431 if (err_code < 0)
432 return err_code;
Hans de Goede9ac69782009-08-14 10:15:52 -0300433
Theodore Kilgore89f08632009-08-14 06:51:52 -0300434 if (id->idProduct == 0x010e) {
Theodore Kilgore89f08632009-08-14 06:51:52 -0300435 sd->cam_type = CAM_TYPE_CIF;
Hans de Goede9ac69782009-08-14 10:15:52 -0300436 cam->nmodes--;
Theodore Kilgore930bf782009-10-05 05:11:35 -0300437 err_code = stream_start(gspca_dev);
438 if (err_code < 0)
439 return err_code;
440 err_code = cam_get_response16(gspca_dev, 0x06, 1);
441 if (err_code < 0)
442 return err_code;
443 /*
444 * The various CIF cameras share the same USB ID but use
445 * different init routines and different controls. We need to
446 * detect which one is connected!
447 *
448 * A list of known CIF cameras follows. They all report either
449 * 0002 for type 0 or 0003 for type 1.
450 * If you have another to report, please do
451 *
452 * Name sd->sensor_type reported by
453 *
454 * Sakar Spy-shot 0 T. Kilgore
455 * Innovage 0 T. Kilgore
456 * Vivitar Mini 0 H. De Goede
457 * Vivitar Mini 0 E. Rodriguez
458 * Vivitar Mini 1 T. Kilgore
459 * Elta-Media 8212dc 1 T. Kaiser
460 * Philips dig. keych. 1 T. Kilgore
461 */
462 switch (gspca_dev->usb_buf[1]) {
463 case 2:
464 sd->sensor_type = 0;
465 break;
466 case 3:
467 sd->sensor_type = 1;
468 break;
469 default:
470 PDEBUG(D_ERR, "Unknown CIF Sensor id : %02x",
471 gspca_dev->usb_buf[1]);
472 return -ENODEV;
473 }
474 PDEBUG(D_PROBE, "MR97310A CIF camera detected, sensor: %d",
475 sd->sensor_type);
476 } else {
477 sd->cam_type = CAM_TYPE_VGA;
Hans de Goede5f5e26b2009-08-14 10:40:26 -0300478
Theodore Kilgore930bf782009-10-05 05:11:35 -0300479 /*
480 * VGA cams also have two different sensor types. Detection
481 * requires a two-step process.
482 *
483 * Here is a report on the result of the first test for the
484 * known MR97310a VGA cameras. If you have another to report,
485 * please do.
486 *
487 * Name byte just read sd->sensor_type
488 * sd->do_lcd_stop
489 * Aiptek Pencam VGA+ 0x31 0 1
490 * ION digital 0x31 0 1
491 * Sakar Digital 77379 0x31 0 1
492 * Argus DC-1620 0x30 1 0
493 * Argus QuickClix 0x30 1 1 (see note)
494 * Note that this test fails to distinguish sd->sensor_type
495 * for the two cameras which have reported 0x30.
496 * Another test will be run on them.
497 * But the sd->do_lcd_stop setting is needed, too.
498 */
499
500 err_code = cam_get_response16(gspca_dev, 0x20, 1);
501 if (err_code < 0)
502 return err_code;
503 sd->sensor_type = gspca_dev->usb_buf[0] & 1;
504 sd->do_lcd_stop = (~gspca_dev->usb_buf[0]) & 1;
505 err_code = stream_start(gspca_dev);
Hans de Goede5f5e26b2009-08-14 10:40:26 -0300506 if (err_code < 0)
507 return err_code;
508
Hans de Goede5f5e26b2009-08-14 10:40:26 -0300509 /*
Theodore Kilgore930bf782009-10-05 05:11:35 -0300510 * A second test can now resolve any remaining ambiguity in the
511 * identification of the camera's sensor type. Specifically,
512 * it now gives the correct sensor_type for the Argus DC-1620
513 * and the Argus QuickClix.
Hans de Goede5f5e26b2009-08-14 10:40:26 -0300514 *
Theodore Kilgore930bf782009-10-05 05:11:35 -0300515 * This second test is only run if needed,
516 * but additional results from testing some other cameras
517 * are recorded here, too:
518 *
519 * Name gspca_dev->usb_buf[] sd->sensor_type
520 *
521 * Aiptek Pencam VGA+ 0300 (test not needed) 1
522 * ION digital 0350 (test not needed) 1
523 * Argus DC-1620 0450 (remains as type 0) 0
524 * Argus QuickClix 0420 (corrected to type 1) 1
525 *
526 * This test even seems able to distinguish one VGA cam from
527 * another which may be useful. However, the CIF type 1 cameras
528 * do not like it.
Hans de Goede5f5e26b2009-08-14 10:40:26 -0300529 */
Hans de Goede5f5e26b2009-08-14 10:40:26 -0300530
Theodore Kilgore930bf782009-10-05 05:11:35 -0300531 if (!sd->sensor_type) {
532 err_code = cam_get_response16(gspca_dev, 0x07, 1);
533 if (err_code < 0)
534 return err_code;
Hans de Goede5f5e26b2009-08-14 10:40:26 -0300535
Theodore Kilgore930bf782009-10-05 05:11:35 -0300536 switch (gspca_dev->usb_buf[1]) {
537 case 0x50:
538 break;
539 case 0x20:
540 sd->sensor_type = 1;
541 PDEBUG(D_PROBE, "sensor_type corrected to 1");
542 break;
543 default:
544 PDEBUG(D_ERR, "Unknown VGA Sensor id : %02x",
545 gspca_dev->usb_buf[1]);
546 return -ENODEV;
547 }
Hans de Goede78028702009-09-02 09:55:16 -0300548 }
Theodore Kilgore930bf782009-10-05 05:11:35 -0300549 PDEBUG(D_PROBE, "MR97310A VGA camera detected, sensor: %d",
550 sd->sensor_type);
551 }
552 /* Stop streaming as we've started it to probe the sensor type. */
553 sd_stopN(gspca_dev);
Hans de Goede78028702009-09-02 09:55:16 -0300554
Theodore Kilgore930bf782009-10-05 05:11:35 -0300555 if (force_sensor_type != -1) {
556 sd->sensor_type = !!force_sensor_type;
557 PDEBUG(D_PROBE, "Forcing sensor type to: %d",
558 sd->sensor_type);
559 }
560
561 /* Setup controls depending on camera type */
562 if (sd->cam_type == CAM_TYPE_CIF) {
563 /* No brightness for sensor_type 0 */
Hans de Goede5f5e26b2009-08-14 10:40:26 -0300564 if (sd->sensor_type == 0)
Theodore Kilgore930bf782009-10-05 05:11:35 -0300565 gspca_dev->ctrl_dis = (1 << NORM_BRIGHTNESS_IDX) |
566 (1 << ARGUS_QC_BRIGHTNESS_IDX);
567 else
568 gspca_dev->ctrl_dis = (1 << ARGUS_QC_BRIGHTNESS_IDX);
Hans de Goede9ac69782009-08-14 10:15:52 -0300569 } else {
Theodore Kilgore930bf782009-10-05 05:11:35 -0300570 /* All controls need to be disabled if VGA sensor_type is 0 */
571 if (sd->sensor_type == 0)
572 gspca_dev->ctrl_dis = (1 << NORM_BRIGHTNESS_IDX) |
573 (1 << ARGUS_QC_BRIGHTNESS_IDX) |
574 (1 << EXPOSURE_IDX) |
575 (1 << GAIN_IDX);
576 else if (sd->do_lcd_stop)
577 /* Argus QuickClix has different brightness limits */
578 gspca_dev->ctrl_dis = (1 << NORM_BRIGHTNESS_IDX);
579 else
580 gspca_dev->ctrl_dis = (1 << ARGUS_QC_BRIGHTNESS_IDX);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300581 }
Hans de Goede9ac69782009-08-14 10:15:52 -0300582
583 sd->brightness = MR97310A_BRIGHTNESS_DEFAULT;
584 sd->exposure = MR97310A_EXPOSURE_DEFAULT;
585 sd->gain = MR97310A_GAIN_DEFAULT;
586
Kyle Guinnd661e622009-01-16 05:36:14 -0300587 return 0;
588}
589
590/* this function is called at probe and resume time */
591static int sd_init(struct gspca_dev *gspca_dev)
592{
593 return 0;
594}
595
Theodore Kilgore89f08632009-08-14 06:51:52 -0300596static int start_cif_cam(struct gspca_dev *gspca_dev)
Kyle Guinnd661e622009-01-16 05:36:14 -0300597{
598 struct sd *sd = (struct sd *) gspca_dev;
599 __u8 *data = gspca_dev->usb_buf;
600 int err_code;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300601 const __u8 startup_string[] = {
602 0x00,
603 0x0d,
604 0x01,
605 0x00, /* Hsize/8 for 352 or 320 */
606 0x00, /* Vsize/4 for 288 or 240 */
607 0x13, /* or 0xbb, depends on sensor */
608 0x00, /* Hstart, depends on res. */
609 0x00, /* reserved ? */
610 0x00, /* Vstart, depends on res. and sensor */
611 0x50, /* 0x54 to get 176 or 160 */
612 0xc0
613 };
Kyle Guinnd661e622009-01-16 05:36:14 -0300614
Theodore Kilgore89f08632009-08-14 06:51:52 -0300615 /* Note: Some of the above descriptions guessed from MR97113A driver */
Kyle Guinnd661e622009-01-16 05:36:14 -0300616
Theodore Kilgore89f08632009-08-14 06:51:52 -0300617 memcpy(data, startup_string, 11);
618 if (sd->sensor_type)
619 data[5] = 0xbb;
620
621 switch (gspca_dev->width) {
622 case 160:
623 data[9] |= 0x04; /* reg 8, 2:1 scale down from 320 */
624 /* fall thru */
625 case 320:
626 default:
627 data[3] = 0x28; /* reg 2, H size/8 */
628 data[4] = 0x3c; /* reg 3, V size/4 */
629 data[6] = 0x14; /* reg 5, H start */
630 data[8] = 0x1a + sd->sensor_type; /* reg 7, V start */
631 break;
632 case 176:
633 data[9] |= 0x04; /* reg 8, 2:1 scale down from 352 */
634 /* fall thru */
635 case 352:
636 data[3] = 0x2c; /* reg 2, H size/8 */
637 data[4] = 0x48; /* reg 3, V size/4 */
638 data[6] = 0x06; /* reg 5, H start */
Theodore Kilgore32345b02009-11-01 12:59:42 -0300639 data[8] = 0x06 - sd->sensor_type; /* reg 7, V start */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300640 break;
641 }
642 err_code = mr_write(gspca_dev, 11);
643 if (err_code < 0)
644 return err_code;
645
646 if (!sd->sensor_type) {
647 const struct sensor_w_data cif_sensor0_init_data[] = {
648 {0x02, 0x00, {0x03, 0x5a, 0xb5, 0x01,
649 0x0f, 0x14, 0x0f, 0x10}, 8},
650 {0x0c, 0x00, {0x04, 0x01, 0x01, 0x00, 0x1f}, 5},
651 {0x12, 0x00, {0x07}, 1},
652 {0x1f, 0x00, {0x06}, 1},
653 {0x27, 0x00, {0x04}, 1},
654 {0x29, 0x00, {0x0c}, 1},
655 {0x40, 0x00, {0x40, 0x00, 0x04}, 3},
656 {0x50, 0x00, {0x60}, 1},
657 {0x60, 0x00, {0x06}, 1},
658 {0x6b, 0x00, {0x85, 0x85, 0xc8, 0xc8, 0xc8, 0xc8}, 6},
659 {0x72, 0x00, {0x1e, 0x56}, 2},
660 {0x75, 0x00, {0x58, 0x40, 0xa2, 0x02, 0x31, 0x02,
661 0x31, 0x80, 0x00}, 9},
662 {0x11, 0x00, {0x01}, 1},
663 {0, 0, {0}, 0}
664 };
665 err_code = sensor_write_regs(gspca_dev, cif_sensor0_init_data,
666 ARRAY_SIZE(cif_sensor0_init_data));
667 } else { /* sd->sensor_type = 1 */
668 const struct sensor_w_data cif_sensor1_init_data[] = {
Hans de Goede9ac69782009-08-14 10:15:52 -0300669 /* Reg 3,4, 7,8 get set by the controls */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300670 {0x02, 0x00, {0x10}, 1},
Hans de Goede9ac69782009-08-14 10:15:52 -0300671 {0x05, 0x01, {0x22}, 1}, /* 5/6 also seen as 65h/32h */
672 {0x06, 0x01, {0x00}, 1},
Theodore Kilgore89f08632009-08-14 06:51:52 -0300673 {0x09, 0x02, {0x0e}, 1},
674 {0x0a, 0x02, {0x05}, 1},
675 {0x0b, 0x02, {0x05}, 1},
676 {0x0c, 0x02, {0x0f}, 1},
Hans de Goede9ac69782009-08-14 10:15:52 -0300677 {0x0d, 0x02, {0x07}, 1},
Theodore Kilgore89f08632009-08-14 06:51:52 -0300678 {0x0e, 0x02, {0x0c}, 1},
679 {0x0f, 0x00, {0x00}, 1},
680 {0x10, 0x00, {0x06}, 1},
681 {0x11, 0x00, {0x07}, 1},
682 {0x12, 0x00, {0x00}, 1},
683 {0x13, 0x00, {0x01}, 1},
684 {0, 0, {0}, 0}
685 };
686 err_code = sensor_write_regs(gspca_dev, cif_sensor1_init_data,
687 ARRAY_SIZE(cif_sensor1_init_data));
688 }
Theodore Kilgore930bf782009-10-05 05:11:35 -0300689 return err_code;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300690}
691
692static int start_vga_cam(struct gspca_dev *gspca_dev)
693{
694 struct sd *sd = (struct sd *) gspca_dev;
695 __u8 *data = gspca_dev->usb_buf;
696 int err_code;
697 const __u8 startup_string[] = {0x00, 0x0d, 0x01, 0x00, 0x00, 0x2b,
698 0x00, 0x00, 0x00, 0x50, 0xc0};
Theodore Kilgore89f08632009-08-14 06:51:52 -0300699 /* What some of these mean is explained in start_cif_cam(), above */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300700
Theodore Kilgore89f08632009-08-14 06:51:52 -0300701 memcpy(data, startup_string, 11);
702 if (!sd->sensor_type) {
703 data[5] = 0x00;
704 data[10] = 0x91;
705 }
Kyle Guinnd661e622009-01-16 05:36:14 -0300706
707 switch (gspca_dev->width) {
708 case 160:
709 data[9] |= 0x0c; /* reg 8, 4:1 scale down */
710 /* fall thru */
711 case 320:
712 data[9] |= 0x04; /* reg 8, 2:1 scale down */
713 /* fall thru */
714 case 640:
715 default:
Theodore Kilgore89f08632009-08-14 06:51:52 -0300716 data[3] = 0x50; /* reg 2, H size/8 */
717 data[4] = 0x78; /* reg 3, V size/4 */
Kyle Guinnd661e622009-01-16 05:36:14 -0300718 data[6] = 0x04; /* reg 5, H start */
719 data[8] = 0x03; /* reg 7, V start */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300720 if (sd->do_lcd_stop)
721 data[8] = 0x04; /* Bayer tile shifted */
Kyle Guinnd661e622009-01-16 05:36:14 -0300722 break;
723
724 case 176:
725 data[9] |= 0x04; /* reg 8, 2:1 scale down */
726 /* fall thru */
727 case 352:
728 data[3] = 0x2c; /* reg 2, H size */
729 data[4] = 0x48; /* reg 3, V size */
730 data[6] = 0x94; /* reg 5, H start */
731 data[8] = 0x63; /* reg 7, V start */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300732 if (sd->do_lcd_stop)
733 data[8] = 0x64; /* Bayer tile shifted */
Kyle Guinnd661e622009-01-16 05:36:14 -0300734 break;
735 }
736
Theodore Kilgore89f08632009-08-14 06:51:52 -0300737 err_code = mr_write(gspca_dev, 11);
Kyle Guinnd661e622009-01-16 05:36:14 -0300738 if (err_code < 0)
739 return err_code;
740
Theodore Kilgore89f08632009-08-14 06:51:52 -0300741 if (!sd->sensor_type) {
742 /* The only known sensor_type 0 cam is the Argus DC-1620 */
743 const struct sensor_w_data vga_sensor0_init_data[] = {
744 {0x01, 0x00, {0x0c, 0x00, 0x04}, 3},
745 {0x14, 0x00, {0x01, 0xe4, 0x02, 0x84}, 4},
746 {0x20, 0x00, {0x00, 0x80, 0x00, 0x08}, 4},
747 {0x25, 0x00, {0x03, 0xa9, 0x80}, 3},
748 {0x30, 0x00, {0x30, 0x18, 0x10, 0x18}, 4},
749 {0, 0, {0}, 0}
750 };
751 err_code = sensor_write_regs(gspca_dev, vga_sensor0_init_data,
752 ARRAY_SIZE(vga_sensor0_init_data));
753 } else { /* sd->sensor_type = 1 */
754 const struct sensor_w_data vga_sensor1_init_data[] = {
755 {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
756 0x07, 0x00, 0x01}, 8},
757 {0x11, 0x04, {0x01}, 1},
758 /*{0x0a, 0x00, {0x00, 0x01, 0x00, 0x00, 0x01, */
759 {0x0a, 0x00, {0x01, 0x06, 0x00, 0x00, 0x01,
760 0x00, 0x0a}, 7},
761 {0x11, 0x04, {0x01}, 1},
762 {0x12, 0x00, {0x00, 0x63, 0x00, 0x70, 0x00, 0x00}, 6},
763 {0x11, 0x04, {0x01}, 1},
764 {0, 0, {0}, 0}
765 };
766 err_code = sensor_write_regs(gspca_dev, vga_sensor1_init_data,
767 ARRAY_SIZE(vga_sensor1_init_data));
768 }
Theodore Kilgore89f08632009-08-14 06:51:52 -0300769 return err_code;
770}
771
772static int sd_start(struct gspca_dev *gspca_dev)
773{
774 struct sd *sd = (struct sd *) gspca_dev;
775 int err_code;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300776
Theodore Kilgore89f08632009-08-14 06:51:52 -0300777 sd->sof_read = 0;
Theodore Kilgore930bf782009-10-05 05:11:35 -0300778
779 /* Some of the VGA cameras require the memory pointer
780 * to be set to 0 again. We have been forced to start the
781 * stream somewhere else to detect the hardware, and closed it,
782 * and now since we are restarting the stream we need to do a
783 * completely fresh and clean start. */
784 err_code = zero_the_pointer(gspca_dev);
785 if (err_code < 0)
786 return err_code;
787
788 err_code = stream_start(gspca_dev);
789 if (err_code < 0)
790 return err_code;
791
Theodore Kilgore89f08632009-08-14 06:51:52 -0300792 if (sd->cam_type == CAM_TYPE_CIF) {
Theodore Kilgore89f08632009-08-14 06:51:52 -0300793 err_code = start_cif_cam(gspca_dev);
794 } else {
Theodore Kilgore89f08632009-08-14 06:51:52 -0300795 err_code = start_vga_cam(gspca_dev);
796 }
Theodore Kilgore930bf782009-10-05 05:11:35 -0300797 if (err_code < 0)
798 return err_code;
799
800 setbrightness(gspca_dev);
801 setexposure(gspca_dev);
802 setgain(gspca_dev);
803
804 return isoc_enable(gspca_dev);
Kyle Guinnd661e622009-01-16 05:36:14 -0300805}
806
807static void sd_stopN(struct gspca_dev *gspca_dev)
808{
Theodore Kilgore89f08632009-08-14 06:51:52 -0300809 struct sd *sd = (struct sd *) gspca_dev;
Kyle Guinnd661e622009-01-16 05:36:14 -0300810
Theodore Kilgore930bf782009-10-05 05:11:35 -0300811 stream_stop(gspca_dev);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300812 /* Not all the cams need this, but even if not, probably a good idea */
813 zero_the_pointer(gspca_dev);
Theodore Kilgore930bf782009-10-05 05:11:35 -0300814 if (sd->do_lcd_stop)
815 lcd_stop(gspca_dev);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300816}
817
818static void setbrightness(struct gspca_dev *gspca_dev)
819{
820 struct sd *sd = (struct sd *) gspca_dev;
821 u8 val;
Theodore Kilgore930bf782009-10-05 05:11:35 -0300822 u8 sign_reg = 7; /* This reg and the next one used on CIF cams. */
823 u8 value_reg = 8; /* VGA cams seem to use regs 0x0b and 0x0c */
824 const u8 quick_clix_table[] =
825 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
826 { 0, 4, 8, 12, 1, 2, 3, 5, 6, 9, 7, 10, 13, 11, 14, 15};
827 /*
828 * This control is disabled for CIF type 1 and VGA type 0 cameras.
829 * It does not quite act linearly for the Argus QuickClix camera,
830 * but it does control brightness. The values are 0 - 15 only, and
831 * the table above makes them act consecutively.
832 */
833 if ((gspca_dev->ctrl_dis & (1 << NORM_BRIGHTNESS_IDX)) &&
834 (gspca_dev->ctrl_dis & (1 << ARGUS_QC_BRIGHTNESS_IDX)))
Hans de Goede9ac69782009-08-14 10:15:52 -0300835 return;
836
Theodore Kilgore930bf782009-10-05 05:11:35 -0300837 if (sd->cam_type == CAM_TYPE_VGA) {
838 sign_reg += 4;
839 value_reg += 4;
840 }
841
Hans de Goede9ac69782009-08-14 10:15:52 -0300842 /* Note register 7 is also seen as 0x8x or 0xCx in dumps */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300843 if (sd->brightness > 0) {
Theodore Kilgore930bf782009-10-05 05:11:35 -0300844 sensor_write1(gspca_dev, sign_reg, 0x00);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300845 val = sd->brightness;
846 } else {
Theodore Kilgore930bf782009-10-05 05:11:35 -0300847 sensor_write1(gspca_dev, sign_reg, 0x01);
848 val = (257 - sd->brightness);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300849 }
Theodore Kilgore930bf782009-10-05 05:11:35 -0300850 /* Use lookup table for funky Argus QuickClix brightness */
851 if (sd->do_lcd_stop)
852 val = quick_clix_table[val];
853
854 sensor_write1(gspca_dev, value_reg, val);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300855}
856
857static void setexposure(struct gspca_dev *gspca_dev)
858{
859 struct sd *sd = (struct sd *) gspca_dev;
Theodore Kilgore930bf782009-10-05 05:11:35 -0300860 int exposure;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300861
Hans de Goede9ac69782009-08-14 10:15:52 -0300862 if (gspca_dev->ctrl_dis & (1 << EXPOSURE_IDX))
863 return;
864
Theodore Kilgore930bf782009-10-05 05:11:35 -0300865 if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1) {
866 /* This cam does not like very low exposure settings */
867 exposure = (sd->exposure < 300) ? 300 : sd->exposure;
868 sensor_write1(gspca_dev, 3, exposure >> 4);
869 sensor_write1(gspca_dev, 4, exposure & 0x0f);
Hans de Goedea2e081b2009-08-14 17:11:36 -0300870 } else {
Hans de Goedea2e081b2009-08-14 17:11:36 -0300871 /* We have both a clock divider and an exposure register.
872 We first calculate the clock divider, as that determines
873 the maximum exposure and then we calculayte the exposure
874 register setting (which goes from 0 - 511).
875
876 Note our 0 - 4095 exposure is mapped to 0 - 511
877 milliseconds exposure time */
Theodore Kilgore930bf782009-10-05 05:11:35 -0300878 u8 clockdiv = (60 * sd->exposure + 7999) / 8000;
Hans de Goedea2e081b2009-08-14 17:11:36 -0300879
880 /* Limit framerate to not exceed usb bandwidth */
881 if (clockdiv < 3 && gspca_dev->width >= 320)
882 clockdiv = 3;
883 else if (clockdiv < 2)
884 clockdiv = 2;
885
Theodore Kilgore930bf782009-10-05 05:11:35 -0300886 if (sd->cam_type == CAM_TYPE_VGA && clockdiv < 4)
887 clockdiv = 4;
888
Hans de Goedea2e081b2009-08-14 17:11:36 -0300889 /* Frame exposure time in ms = 1000 * clockdiv / 60 ->
890 exposure = (sd->exposure / 8) * 511 / (1000 * clockdiv / 60) */
891 exposure = (60 * 511 * sd->exposure) / (8000 * clockdiv);
892 if (exposure > 511)
893 exposure = 511;
894
895 /* exposure register value is reversed! */
896 exposure = 511 - exposure;
897
898 sensor_write1(gspca_dev, 0x02, clockdiv);
899 sensor_write1(gspca_dev, 0x0e, exposure & 0xff);
900 sensor_write1(gspca_dev, 0x0f, exposure >> 8);
901 }
Theodore Kilgore89f08632009-08-14 06:51:52 -0300902}
903
904static void setgain(struct gspca_dev *gspca_dev)
905{
906 struct sd *sd = (struct sd *) gspca_dev;
907
Hans de Goede9ac69782009-08-14 10:15:52 -0300908 if (gspca_dev->ctrl_dis & (1 << GAIN_IDX))
909 return;
910
Theodore Kilgore930bf782009-10-05 05:11:35 -0300911 if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1) {
Hans de Goede823902d2009-08-17 12:25:17 -0300912 sensor_write1(gspca_dev, 0x0e, sd->gain);
Hans de Goedea2e081b2009-08-14 17:11:36 -0300913 } else {
914 sensor_write1(gspca_dev, 0x10, sd->gain);
915 }
Theodore Kilgore89f08632009-08-14 06:51:52 -0300916}
917
918static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
919{
920 struct sd *sd = (struct sd *) gspca_dev;
921
922 sd->brightness = val;
923 if (gspca_dev->streaming)
924 setbrightness(gspca_dev);
925 return 0;
926}
927
928static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
929{
930 struct sd *sd = (struct sd *) gspca_dev;
931
932 *val = sd->brightness;
933 return 0;
934}
935
936static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
937{
938 struct sd *sd = (struct sd *) gspca_dev;
939
940 sd->exposure = val;
941 if (gspca_dev->streaming)
942 setexposure(gspca_dev);
943 return 0;
944}
945
946static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
947{
948 struct sd *sd = (struct sd *) gspca_dev;
949
950 *val = sd->exposure;
951 return 0;
952}
953
954static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
955{
956 struct sd *sd = (struct sd *) gspca_dev;
957
958 sd->gain = val;
959 if (gspca_dev->streaming)
960 setgain(gspca_dev);
961 return 0;
962}
963
964static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
965{
966 struct sd *sd = (struct sd *) gspca_dev;
967
968 *val = sd->gain;
969 return 0;
Kyle Guinnd661e622009-01-16 05:36:14 -0300970}
971
972/* Include pac common sof detection functions */
973#include "pac_common.h"
974
975static void sd_pkt_scan(struct gspca_dev *gspca_dev,
976 struct gspca_frame *frame, /* target */
977 __u8 *data, /* isoc packet */
978 int len) /* iso packet length */
979{
Kyle Guinnd661e622009-01-16 05:36:14 -0300980 unsigned char *sof;
981
982 sof = pac_find_sof(gspca_dev, data, len);
983 if (sof) {
984 int n;
985
986 /* finish decoding current frame */
987 n = sof - data;
988 if (n > sizeof pac_sof_marker)
989 n -= sizeof pac_sof_marker;
990 else
991 n = 0;
992 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
993 data, n);
Theodore Kilgore9832d762009-03-13 13:04:31 -0300994 /* Start next frame. */
995 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
996 pac_sof_marker, sizeof pac_sof_marker);
Kyle Guinnd661e622009-01-16 05:36:14 -0300997 len -= sof - data;
998 data = sof;
999 }
Kyle Guinnd661e622009-01-16 05:36:14 -03001000 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
1001}
1002
1003/* sub-driver description */
1004static const struct sd_desc sd_desc = {
1005 .name = MODULE_NAME,
1006 .ctrls = sd_ctrls,
1007 .nctrls = ARRAY_SIZE(sd_ctrls),
1008 .config = sd_config,
1009 .init = sd_init,
1010 .start = sd_start,
1011 .stopN = sd_stopN,
1012 .pkt_scan = sd_pkt_scan,
1013};
1014
1015/* -- module initialisation -- */
1016static const __devinitdata struct usb_device_id device_table[] = {
Theodore Kilgore89f08632009-08-14 06:51:52 -03001017 {USB_DEVICE(0x08ca, 0x0111)}, /* Aiptek Pencam VGA+ */
1018 {USB_DEVICE(0x093a, 0x010f)}, /* All other known MR97310A VGA cams */
1019 {USB_DEVICE(0x093a, 0x010e)}, /* All known MR97310A CIF cams */
Kyle Guinnd661e622009-01-16 05:36:14 -03001020 {}
1021};
1022MODULE_DEVICE_TABLE(usb, device_table);
1023
1024/* -- device connect -- */
1025static int sd_probe(struct usb_interface *intf,
1026 const struct usb_device_id *id)
1027{
1028 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1029 THIS_MODULE);
1030}
1031
1032static struct usb_driver sd_driver = {
1033 .name = MODULE_NAME,
1034 .id_table = device_table,
1035 .probe = sd_probe,
1036 .disconnect = gspca_disconnect,
1037#ifdef CONFIG_PM
1038 .suspend = gspca_suspend,
1039 .resume = gspca_resume,
1040#endif
1041};
1042
1043/* -- module insert / remove -- */
1044static int __init sd_mod_init(void)
1045{
Alexey Klimov5d3fa302009-03-27 15:57:46 -03001046 int ret;
1047
1048 ret = usb_register(&sd_driver);
1049 if (ret < 0)
1050 return ret;
Kyle Guinnd661e622009-01-16 05:36:14 -03001051 PDEBUG(D_PROBE, "registered");
1052 return 0;
1053}
1054static void __exit sd_mod_exit(void)
1055{
1056 usb_deregister(&sd_driver);
1057 PDEBUG(D_PROBE, "deregistered");
1058}
1059
1060module_init(sd_mod_init);
1061module_exit(sd_mod_exit);