blob: b66169256dc4bdb15853573dfc9f6579638182f8 [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
Theodore Kilgore1160a382009-10-30 04:29:56 -030021 * Theodore Kilgore <kilgota@auburn.edu> and
22 * Edmond Rodriguez <erodrig_97@yahoo.com> and
23 * Aurelien Jacobs <aurel@gnuage.org>
Theodore Kilgore89f08632009-08-14 06:51:52 -030024 *
25 * The MR97311A support in gspca/mars.c has been helpful in understanding some
26 * of the registers in these cameras.
27 *
Kyle Guinnd661e622009-01-16 05:36:14 -030028 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation; either version 2 of the License, or
31 * any later version.
32 *
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
37 *
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41 */
42
43#define MODULE_NAME "mr97310a"
44
45#include "gspca.h"
46
Theodore Kilgore89f08632009-08-14 06:51:52 -030047#define CAM_TYPE_CIF 0
48#define CAM_TYPE_VGA 1
49
Theodore Kilgore89f08632009-08-14 06:51:52 -030050#define MR97310A_BRIGHTNESS_DEFAULT 0
51
Theodore Kilgore930bf782009-10-05 05:11:35 -030052#define MR97310A_EXPOSURE_MIN 0
Theodore Kilgore89f08632009-08-14 06:51:52 -030053#define MR97310A_EXPOSURE_MAX 4095
54#define MR97310A_EXPOSURE_DEFAULT 1000
55
56#define MR97310A_GAIN_MIN 0
57#define MR97310A_GAIN_MAX 31
58#define MR97310A_GAIN_DEFAULT 25
59
Hans de Goede065b6f72009-10-29 07:42:30 -030060#define MR97310A_MIN_CLOCKDIV_MIN 3
61#define MR97310A_MIN_CLOCKDIV_MAX 8
62#define MR97310A_MIN_CLOCKDIV_DEFAULT 3
63
Theodore Kilgore89f08632009-08-14 06:51:52 -030064MODULE_AUTHOR("Kyle Guinn <elyk03@gmail.com>,"
65 "Theodore Kilgore <kilgota@auburn.edu>");
Kyle Guinnd661e622009-01-16 05:36:14 -030066MODULE_DESCRIPTION("GSPCA/Mars-Semi MR97310A USB Camera Driver");
67MODULE_LICENSE("GPL");
68
Hans de Goede78028702009-09-02 09:55:16 -030069/* global parameters */
70int force_sensor_type = -1;
71module_param(force_sensor_type, int, 0644);
72MODULE_PARM_DESC(force_sensor_type, "Force sensor type (-1 (auto), 0 or 1)");
73
Kyle Guinnd661e622009-01-16 05:36:14 -030074/* specific webcam descriptor */
75struct sd {
76 struct gspca_dev gspca_dev; /* !! must be the first item */
Kyle Guinnd661e622009-01-16 05:36:14 -030077 u8 sof_read;
Theodore Kilgore89f08632009-08-14 06:51:52 -030078 u8 cam_type; /* 0 is CIF and 1 is VGA */
79 u8 sensor_type; /* We use 0 and 1 here, too. */
80 u8 do_lcd_stop;
Theodore Kilgore89f08632009-08-14 06:51:52 -030081
82 int brightness;
83 u16 exposure;
Theodore Kilgore89f08632009-08-14 06:51:52 -030084 u8 gain;
Hans de Goede065b6f72009-10-29 07:42:30 -030085 u8 min_clockdiv;
Kyle Guinnd661e622009-01-16 05:36:14 -030086};
87
Theodore Kilgore89f08632009-08-14 06:51:52 -030088struct sensor_w_data {
89 u8 reg;
90 u8 flags;
91 u8 data[16];
92 int len;
93};
94
Theodore Kilgore930bf782009-10-05 05:11:35 -030095static void sd_stopN(struct gspca_dev *gspca_dev);
Theodore Kilgore89f08632009-08-14 06:51:52 -030096static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
97static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
98static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
99static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
100static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
101static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
Hans de Goede065b6f72009-10-29 07:42:30 -0300102static int sd_setmin_clockdiv(struct gspca_dev *gspca_dev, __s32 val);
103static int sd_getmin_clockdiv(struct gspca_dev *gspca_dev, __s32 *val);
Hans de Goede9ac69782009-08-14 10:15:52 -0300104static void setbrightness(struct gspca_dev *gspca_dev);
105static void setexposure(struct gspca_dev *gspca_dev);
106static void setgain(struct gspca_dev *gspca_dev);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300107
Kyle Guinnd661e622009-01-16 05:36:14 -0300108/* V4L2 controls supported by the driver */
109static struct ctrl sd_ctrls[] = {
Theodore Kilgore1160a382009-10-30 04:29:56 -0300110/* Separate brightness control description for Argus QuickClix as it has
111 different limits from the other mr97310a cameras */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300112 {
Theodore Kilgore930bf782009-10-05 05:11:35 -0300113#define NORM_BRIGHTNESS_IDX 0
Theodore Kilgore89f08632009-08-14 06:51:52 -0300114 {
115 .id = V4L2_CID_BRIGHTNESS,
116 .type = V4L2_CTRL_TYPE_INTEGER,
117 .name = "Brightness",
Theodore Kilgore930bf782009-10-05 05:11:35 -0300118 .minimum = -254,
119 .maximum = 255,
Theodore Kilgore89f08632009-08-14 06:51:52 -0300120 .step = 1,
121 .default_value = MR97310A_BRIGHTNESS_DEFAULT,
122 .flags = 0,
123 },
124 .set = sd_setbrightness,
125 .get = sd_getbrightness,
126 },
127 {
Theodore Kilgore930bf782009-10-05 05:11:35 -0300128#define ARGUS_QC_BRIGHTNESS_IDX 1
129 {
130 .id = V4L2_CID_BRIGHTNESS,
131 .type = V4L2_CTRL_TYPE_INTEGER,
132 .name = "Brightness",
133 .minimum = 0,
134 .maximum = 15,
135 .step = 1,
136 .default_value = MR97310A_BRIGHTNESS_DEFAULT,
137 .flags = 0,
138 },
139 .set = sd_setbrightness,
140 .get = sd_getbrightness,
141 },
142 {
143#define EXPOSURE_IDX 2
Theodore Kilgore89f08632009-08-14 06:51:52 -0300144 {
145 .id = V4L2_CID_EXPOSURE,
146 .type = V4L2_CTRL_TYPE_INTEGER,
147 .name = "Exposure",
148 .minimum = MR97310A_EXPOSURE_MIN,
149 .maximum = MR97310A_EXPOSURE_MAX,
150 .step = 1,
151 .default_value = MR97310A_EXPOSURE_DEFAULT,
152 .flags = 0,
153 },
154 .set = sd_setexposure,
155 .get = sd_getexposure,
156 },
157 {
Theodore Kilgore930bf782009-10-05 05:11:35 -0300158#define GAIN_IDX 3
Theodore Kilgore89f08632009-08-14 06:51:52 -0300159 {
160 .id = V4L2_CID_GAIN,
161 .type = V4L2_CTRL_TYPE_INTEGER,
162 .name = "Gain",
163 .minimum = MR97310A_GAIN_MIN,
164 .maximum = MR97310A_GAIN_MAX,
165 .step = 1,
166 .default_value = MR97310A_GAIN_DEFAULT,
167 .flags = 0,
168 },
169 .set = sd_setgain,
170 .get = sd_getgain,
171 },
Hans de Goede065b6f72009-10-29 07:42:30 -0300172 {
173#define MIN_CLOCKDIV_IDX 4
174 {
175 .id = V4L2_CID_PRIVATE_BASE,
176 .type = V4L2_CTRL_TYPE_INTEGER,
177 .name = "Minimum Clock Divider",
178 .minimum = MR97310A_MIN_CLOCKDIV_MIN,
179 .maximum = MR97310A_MIN_CLOCKDIV_MAX,
180 .step = 1,
181 .default_value = MR97310A_MIN_CLOCKDIV_DEFAULT,
182 .flags = 0,
183 },
184 .set = sd_setmin_clockdiv,
185 .get = sd_getmin_clockdiv,
186 },
Kyle Guinnd661e622009-01-16 05:36:14 -0300187};
188
189static const struct v4l2_pix_format vga_mode[] = {
190 {160, 120, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
191 .bytesperline = 160,
192 .sizeimage = 160 * 120,
193 .colorspace = V4L2_COLORSPACE_SRGB,
194 .priv = 4},
195 {176, 144, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
196 .bytesperline = 176,
197 .sizeimage = 176 * 144,
198 .colorspace = V4L2_COLORSPACE_SRGB,
199 .priv = 3},
200 {320, 240, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
201 .bytesperline = 320,
202 .sizeimage = 320 * 240,
203 .colorspace = V4L2_COLORSPACE_SRGB,
204 .priv = 2},
205 {352, 288, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
206 .bytesperline = 352,
207 .sizeimage = 352 * 288,
208 .colorspace = V4L2_COLORSPACE_SRGB,
209 .priv = 1},
210 {640, 480, V4L2_PIX_FMT_MR97310A, V4L2_FIELD_NONE,
211 .bytesperline = 640,
212 .sizeimage = 640 * 480,
213 .colorspace = V4L2_COLORSPACE_SRGB,
214 .priv = 0},
215};
216
217/* the bytes to write are in gspca_dev->usb_buf */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300218static int mr_write(struct gspca_dev *gspca_dev, int len)
Kyle Guinnd661e622009-01-16 05:36:14 -0300219{
220 int rc;
221
222 rc = usb_bulk_msg(gspca_dev->dev,
223 usb_sndbulkpipe(gspca_dev->dev, 4),
Jean-Francois Moine92e8c912009-02-02 16:25:38 -0300224 gspca_dev->usb_buf, len, NULL, 500);
Kyle Guinnd661e622009-01-16 05:36:14 -0300225 if (rc < 0)
226 PDEBUG(D_ERR, "reg write [%02x] error %d",
227 gspca_dev->usb_buf[0], rc);
228 return rc;
229}
230
Theodore Kilgore89f08632009-08-14 06:51:52 -0300231/* the bytes are read into gspca_dev->usb_buf */
232static int mr_read(struct gspca_dev *gspca_dev, int len)
233{
234 int rc;
235
236 rc = usb_bulk_msg(gspca_dev->dev,
237 usb_rcvbulkpipe(gspca_dev->dev, 3),
238 gspca_dev->usb_buf, len, NULL, 500);
239 if (rc < 0)
240 PDEBUG(D_ERR, "reg read [%02x] error %d",
241 gspca_dev->usb_buf[0], rc);
242 return rc;
243}
244
245static int sensor_write_reg(struct gspca_dev *gspca_dev, u8 reg, u8 flags,
246 const u8 *data, int len)
247{
248 gspca_dev->usb_buf[0] = 0x1f;
249 gspca_dev->usb_buf[1] = flags;
250 gspca_dev->usb_buf[2] = reg;
251 memcpy(gspca_dev->usb_buf + 3, data, len);
252
253 return mr_write(gspca_dev, len + 3);
254}
255
256static int sensor_write_regs(struct gspca_dev *gspca_dev,
257 const struct sensor_w_data *data, int len)
258{
259 int i, rc;
260
261 for (i = 0; i < len; i++) {
262 rc = sensor_write_reg(gspca_dev, data[i].reg, data[i].flags,
263 data[i].data, data[i].len);
264 if (rc < 0)
265 return rc;
266 }
267
268 return 0;
269}
270
271static int sensor_write1(struct gspca_dev *gspca_dev, u8 reg, u8 data)
272{
Hans de Goedea2e081b2009-08-14 17:11:36 -0300273 struct sd *sd = (struct sd *) gspca_dev;
274 u8 buf, confirm_reg;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300275 int rc;
276
277 buf = data;
Theodore Kilgore930bf782009-10-05 05:11:35 -0300278 if (sd->cam_type == CAM_TYPE_CIF) {
279 rc = sensor_write_reg(gspca_dev, reg, 0x01, &buf, 1);
280 confirm_reg = sd->sensor_type ? 0x13 : 0x11;
281 } else {
282 rc = sensor_write_reg(gspca_dev, reg, 0x00, &buf, 1);
283 confirm_reg = 0x11;
284 }
Theodore Kilgore89f08632009-08-14 06:51:52 -0300285 if (rc < 0)
286 return rc;
287
288 buf = 0x01;
Hans de Goedea2e081b2009-08-14 17:11:36 -0300289 rc = sensor_write_reg(gspca_dev, confirm_reg, 0x00, &buf, 1);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300290 if (rc < 0)
291 return rc;
292
293 return 0;
294}
295
Theodore Kilgore930bf782009-10-05 05:11:35 -0300296static int cam_get_response16(struct gspca_dev *gspca_dev, u8 reg, int verbose)
Theodore Kilgore89f08632009-08-14 06:51:52 -0300297{
Theodore Kilgore89f08632009-08-14 06:51:52 -0300298 int err_code;
299
Theodore Kilgore930bf782009-10-05 05:11:35 -0300300 gspca_dev->usb_buf[0] = reg;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300301 err_code = mr_write(gspca_dev, 1);
302 if (err_code < 0)
303 return err_code;
304
305 err_code = mr_read(gspca_dev, 16);
Theodore Kilgore930bf782009-10-05 05:11:35 -0300306 if (err_code < 0)
307 return err_code;
308
309 if (verbose)
310 PDEBUG(D_PROBE, "Register: %02x reads %02x%02x%02x", reg,
311 gspca_dev->usb_buf[0],
312 gspca_dev->usb_buf[1],
313 gspca_dev->usb_buf[2]);
314
315 return 0;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300316}
317
318static int zero_the_pointer(struct gspca_dev *gspca_dev)
319{
320 __u8 *data = gspca_dev->usb_buf;
321 int err_code;
322 u8 status = 0;
323 int tries = 0;
324
Theodore Kilgore930bf782009-10-05 05:11:35 -0300325 err_code = cam_get_response16(gspca_dev, 0x21, 0);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300326 if (err_code < 0)
327 return err_code;
328
329 err_code = mr_write(gspca_dev, 1);
330 data[0] = 0x19;
331 data[1] = 0x51;
332 err_code = mr_write(gspca_dev, 2);
333 if (err_code < 0)
334 return err_code;
335
Theodore Kilgore930bf782009-10-05 05:11:35 -0300336 err_code = cam_get_response16(gspca_dev, 0x21, 0);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300337 if (err_code < 0)
338 return err_code;
339
340 data[0] = 0x19;
341 data[1] = 0xba;
342 err_code = mr_write(gspca_dev, 2);
343 if (err_code < 0)
344 return err_code;
345
Theodore Kilgore930bf782009-10-05 05:11:35 -0300346 err_code = cam_get_response16(gspca_dev, 0x21, 0);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300347 if (err_code < 0)
348 return err_code;
349
350 data[0] = 0x19;
351 data[1] = 0x00;
352 err_code = mr_write(gspca_dev, 2);
353 if (err_code < 0)
354 return err_code;
355
Theodore Kilgore930bf782009-10-05 05:11:35 -0300356 err_code = cam_get_response16(gspca_dev, 0x21, 0);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300357 if (err_code < 0)
358 return err_code;
359
360 data[0] = 0x19;
361 data[1] = 0x00;
362 err_code = mr_write(gspca_dev, 2);
363 if (err_code < 0)
364 return err_code;
365
366 while (status != 0x0a && tries < 256) {
Theodore Kilgore930bf782009-10-05 05:11:35 -0300367 err_code = cam_get_response16(gspca_dev, 0x21, 0);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300368 status = data[0];
369 tries++;
370 if (err_code < 0)
371 return err_code;
372 }
Hans de Goede54943782009-08-14 11:05:38 -0300373 if (status != 0x0a)
374 PDEBUG(D_ERR, "status is %02x", status);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300375
376 tries = 0;
377 while (tries < 4) {
378 data[0] = 0x19;
379 data[1] = 0x00;
380 err_code = mr_write(gspca_dev, 2);
381 if (err_code < 0)
382 return err_code;
383
Theodore Kilgore930bf782009-10-05 05:11:35 -0300384 err_code = cam_get_response16(gspca_dev, 0x21, 0);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300385 status = data[0];
386 tries++;
387 if (err_code < 0)
388 return err_code;
389 }
Theodore Kilgore89f08632009-08-14 06:51:52 -0300390
391 data[0] = 0x19;
392 err_code = mr_write(gspca_dev, 1);
393 if (err_code < 0)
394 return err_code;
395
396 err_code = mr_read(gspca_dev, 16);
397 if (err_code < 0)
398 return err_code;
399
400 return 0;
401}
402
Theodore Kilgore930bf782009-10-05 05:11:35 -0300403static int stream_start(struct gspca_dev *gspca_dev)
Theodore Kilgore89f08632009-08-14 06:51:52 -0300404{
Theodore Kilgore930bf782009-10-05 05:11:35 -0300405 gspca_dev->usb_buf[0] = 0x01;
406 gspca_dev->usb_buf[1] = 0x01;
407 return mr_write(gspca_dev, 2);
408}
Theodore Kilgore89f08632009-08-14 06:51:52 -0300409
Theodore Kilgore930bf782009-10-05 05:11:35 -0300410static void stream_stop(struct gspca_dev *gspca_dev)
411{
412 gspca_dev->usb_buf[0] = 0x01;
413 gspca_dev->usb_buf[1] = 0x00;
414 if (mr_write(gspca_dev, 2) < 0)
415 PDEBUG(D_ERR, "Stream Stop failed");
416}
Theodore Kilgore89f08632009-08-14 06:51:52 -0300417
Theodore Kilgore930bf782009-10-05 05:11:35 -0300418static void lcd_stop(struct gspca_dev *gspca_dev)
419{
420 gspca_dev->usb_buf[0] = 0x19;
421 gspca_dev->usb_buf[1] = 0x54;
422 if (mr_write(gspca_dev, 2) < 0)
423 PDEBUG(D_ERR, "LCD Stop failed");
424}
Theodore Kilgore89f08632009-08-14 06:51:52 -0300425
Theodore Kilgore930bf782009-10-05 05:11:35 -0300426static int isoc_enable(struct gspca_dev *gspca_dev)
427{
428 gspca_dev->usb_buf[0] = 0x00;
429 gspca_dev->usb_buf[1] = 0x4d; /* ISOC transfering enable... */
430 return mr_write(gspca_dev, 2);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300431}
432
Theodore Kilgore1160a382009-10-30 04:29:56 -0300433/* This function is called at probe time */
Kyle Guinnd661e622009-01-16 05:36:14 -0300434static int sd_config(struct gspca_dev *gspca_dev,
435 const struct usb_device_id *id)
436{
Theodore Kilgore89f08632009-08-14 06:51:52 -0300437 struct sd *sd = (struct sd *) gspca_dev;
Kyle Guinnd661e622009-01-16 05:36:14 -0300438 struct cam *cam;
Hans de Goede5f5e26b2009-08-14 10:40:26 -0300439 int err_code;
Kyle Guinnd661e622009-01-16 05:36:14 -0300440
441 cam = &gspca_dev->cam;
442 cam->cam_mode = vga_mode;
443 cam->nmodes = ARRAY_SIZE(vga_mode);
Theodore Kilgore930bf782009-10-05 05:11:35 -0300444 sd->do_lcd_stop = 0;
445
Theodore Kilgore1160a382009-10-30 04:29:56 -0300446 /* Several of the supported CIF cameras share the same USB ID but
447 * require different initializations and different control settings.
448 * The same is true of the VGA cameras. Therefore, we are forced
449 * to start the initialization process in order to determine which
450 * camera is present. Some of the supported cameras require the
Theodore Kilgore930bf782009-10-05 05:11:35 -0300451 * memory pointer to be set to 0 as the very first item of business
452 * or else they will not stream. So we do that immediately.
453 */
454 err_code = zero_the_pointer(gspca_dev);
455 if (err_code < 0)
456 return err_code;
Hans de Goede9ac69782009-08-14 10:15:52 -0300457
Theodore Kilgoreb3e440e2009-10-09 03:54:49 -0300458 err_code = stream_start(gspca_dev);
459 if (err_code < 0)
460 return err_code;
461
Aurelien Jacobs8ac246c2009-10-29 07:45:24 -0300462 if (id->idProduct == 0x0110 || id->idProduct == 0x010e) {
Theodore Kilgore89f08632009-08-14 06:51:52 -0300463 sd->cam_type = CAM_TYPE_CIF;
Hans de Goede9ac69782009-08-14 10:15:52 -0300464 cam->nmodes--;
Theodore Kilgore930bf782009-10-05 05:11:35 -0300465 err_code = cam_get_response16(gspca_dev, 0x06, 1);
466 if (err_code < 0)
467 return err_code;
468 /*
Theodore Kilgore1160a382009-10-30 04:29:56 -0300469 * All but one of the known CIF cameras share the same USB ID,
470 * but two different init routines are in use, and the control
471 * settings are different, too. We need to detect which camera
472 * of the two known varieties is connected!
Theodore Kilgore930bf782009-10-05 05:11:35 -0300473 *
474 * A list of known CIF cameras follows. They all report either
475 * 0002 for type 0 or 0003 for type 1.
476 * If you have another to report, please do
477 *
478 * Name sd->sensor_type reported by
479 *
480 * Sakar Spy-shot 0 T. Kilgore
481 * Innovage 0 T. Kilgore
482 * Vivitar Mini 0 H. De Goede
483 * Vivitar Mini 0 E. Rodriguez
484 * Vivitar Mini 1 T. Kilgore
485 * Elta-Media 8212dc 1 T. Kaiser
486 * Philips dig. keych. 1 T. Kilgore
Theodore Kilgore1160a382009-10-30 04:29:56 -0300487 * Trust Spyc@m 100 1 A. Jacobs
Theodore Kilgore930bf782009-10-05 05:11:35 -0300488 */
489 switch (gspca_dev->usb_buf[1]) {
490 case 2:
491 sd->sensor_type = 0;
492 break;
493 case 3:
494 sd->sensor_type = 1;
495 break;
496 default:
497 PDEBUG(D_ERR, "Unknown CIF Sensor id : %02x",
498 gspca_dev->usb_buf[1]);
499 return -ENODEV;
500 }
501 PDEBUG(D_PROBE, "MR97310A CIF camera detected, sensor: %d",
502 sd->sensor_type);
503 } else {
504 sd->cam_type = CAM_TYPE_VGA;
Hans de Goede5f5e26b2009-08-14 10:40:26 -0300505
Theodore Kilgoreb3e440e2009-10-09 03:54:49 -0300506 err_code = cam_get_response16(gspca_dev, 0x07, 1);
Hans de Goede5f5e26b2009-08-14 10:40:26 -0300507 if (err_code < 0)
508 return err_code;
509
Hans de Goede5f5e26b2009-08-14 10:40:26 -0300510 /*
Theodore Kilgoreb3e440e2009-10-09 03:54:49 -0300511 * Here is a table of the responses to the previous command
512 * from the known MR97310A VGA cameras.
Theodore Kilgore930bf782009-10-05 05:11:35 -0300513 *
514 * Name gspca_dev->usb_buf[] sd->sensor_type
Theodore Kilgoreb3e440e2009-10-09 03:54:49 -0300515 * sd->do_lcd_stop
516 * Aiptek Pencam VGA+ 0300 0 1
517 * ION digital 0350 0 1
518 * Argus DC-1620 0450 1 0
519 * Argus QuickClix 0420 1 1
Theodore Kilgore930bf782009-10-05 05:11:35 -0300520 *
Theodore Kilgoreb3e440e2009-10-09 03:54:49 -0300521 * Based upon these results, we assume default settings
522 * and then correct as necessary, as follows.
Theodore Kilgore930bf782009-10-05 05:11:35 -0300523 *
Hans de Goede5f5e26b2009-08-14 10:40:26 -0300524 */
Hans de Goede5f5e26b2009-08-14 10:40:26 -0300525
Theodore Kilgoreb3e440e2009-10-09 03:54:49 -0300526 sd->sensor_type = 1;
527 sd->do_lcd_stop = 0;
528 if ((gspca_dev->usb_buf[0] != 0x03) &&
529 (gspca_dev->usb_buf[0] != 0x04)) {
530 PDEBUG(D_ERR, "Unknown VGA Sensor id Byte 0: %02x",
531 gspca_dev->usb_buf[1]);
532 PDEBUG(D_ERR, "Defaults assumed, may not work");
533 PDEBUG(D_ERR, "Please report this");
534 }
535 if (gspca_dev->usb_buf[0] == 0x04) {
536 sd->do_lcd_stop = 1;
Theodore Kilgore930bf782009-10-05 05:11:35 -0300537 switch (gspca_dev->usb_buf[1]) {
538 case 0x50:
Theodore Kilgoreb3e440e2009-10-09 03:54:49 -0300539 sd->sensor_type = 0;
540 PDEBUG(D_PROBE, "sensor_type corrected to 0");
Theodore Kilgore930bf782009-10-05 05:11:35 -0300541 break;
542 case 0x20:
Theodore Kilgoreb3e440e2009-10-09 03:54:49 -0300543 /* Nothing to do here. */
Theodore Kilgore930bf782009-10-05 05:11:35 -0300544 break;
545 default:
Theodore Kilgoreb3e440e2009-10-09 03:54:49 -0300546 PDEBUG(D_ERR,
547 "Unknown VGA Sensor id Byte 1: %02x",
548 gspca_dev->usb_buf[1]);
549 PDEBUG(D_ERR,
550 "Defaults assumed, may not work");
551 PDEBUG(D_ERR, "Please report this");
Theodore Kilgore930bf782009-10-05 05:11:35 -0300552 }
Hans de Goede78028702009-09-02 09:55:16 -0300553 }
Theodore Kilgore930bf782009-10-05 05:11:35 -0300554 PDEBUG(D_PROBE, "MR97310A VGA camera detected, sensor: %d",
555 sd->sensor_type);
556 }
557 /* Stop streaming as we've started it to probe the sensor type. */
558 sd_stopN(gspca_dev);
Hans de Goede78028702009-09-02 09:55:16 -0300559
Theodore Kilgore930bf782009-10-05 05:11:35 -0300560 if (force_sensor_type != -1) {
561 sd->sensor_type = !!force_sensor_type;
562 PDEBUG(D_PROBE, "Forcing sensor type to: %d",
563 sd->sensor_type);
564 }
565
566 /* Setup controls depending on camera type */
567 if (sd->cam_type == CAM_TYPE_CIF) {
568 /* No brightness for sensor_type 0 */
Hans de Goede5f5e26b2009-08-14 10:40:26 -0300569 if (sd->sensor_type == 0)
Theodore Kilgore930bf782009-10-05 05:11:35 -0300570 gspca_dev->ctrl_dis = (1 << NORM_BRIGHTNESS_IDX) |
571 (1 << ARGUS_QC_BRIGHTNESS_IDX);
572 else
Hans de Goede065b6f72009-10-29 07:42:30 -0300573 gspca_dev->ctrl_dis = (1 << ARGUS_QC_BRIGHTNESS_IDX) |
574 (1 << MIN_CLOCKDIV_IDX);
Hans de Goede9ac69782009-08-14 10:15:52 -0300575 } else {
Theodore Kilgore930bf782009-10-05 05:11:35 -0300576 /* All controls need to be disabled if VGA sensor_type is 0 */
577 if (sd->sensor_type == 0)
578 gspca_dev->ctrl_dis = (1 << NORM_BRIGHTNESS_IDX) |
579 (1 << ARGUS_QC_BRIGHTNESS_IDX) |
580 (1 << EXPOSURE_IDX) |
Hans de Goede065b6f72009-10-29 07:42:30 -0300581 (1 << GAIN_IDX) |
582 (1 << MIN_CLOCKDIV_IDX);
Theodore Kilgore930bf782009-10-05 05:11:35 -0300583 else if (sd->do_lcd_stop)
584 /* Argus QuickClix has different brightness limits */
585 gspca_dev->ctrl_dis = (1 << NORM_BRIGHTNESS_IDX);
586 else
587 gspca_dev->ctrl_dis = (1 << ARGUS_QC_BRIGHTNESS_IDX);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300588 }
Hans de Goede9ac69782009-08-14 10:15:52 -0300589
590 sd->brightness = MR97310A_BRIGHTNESS_DEFAULT;
591 sd->exposure = MR97310A_EXPOSURE_DEFAULT;
592 sd->gain = MR97310A_GAIN_DEFAULT;
Hans de Goede065b6f72009-10-29 07:42:30 -0300593 sd->min_clockdiv = MR97310A_MIN_CLOCKDIV_DEFAULT;
Hans de Goede9ac69782009-08-14 10:15:52 -0300594
Kyle Guinnd661e622009-01-16 05:36:14 -0300595 return 0;
596}
597
598/* this function is called at probe and resume time */
599static int sd_init(struct gspca_dev *gspca_dev)
600{
601 return 0;
602}
603
Theodore Kilgore89f08632009-08-14 06:51:52 -0300604static int start_cif_cam(struct gspca_dev *gspca_dev)
Kyle Guinnd661e622009-01-16 05:36:14 -0300605{
606 struct sd *sd = (struct sd *) gspca_dev;
607 __u8 *data = gspca_dev->usb_buf;
608 int err_code;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300609 const __u8 startup_string[] = {
610 0x00,
611 0x0d,
612 0x01,
613 0x00, /* Hsize/8 for 352 or 320 */
614 0x00, /* Vsize/4 for 288 or 240 */
615 0x13, /* or 0xbb, depends on sensor */
616 0x00, /* Hstart, depends on res. */
617 0x00, /* reserved ? */
618 0x00, /* Vstart, depends on res. and sensor */
619 0x50, /* 0x54 to get 176 or 160 */
620 0xc0
621 };
Kyle Guinnd661e622009-01-16 05:36:14 -0300622
Theodore Kilgore89f08632009-08-14 06:51:52 -0300623 /* Note: Some of the above descriptions guessed from MR97113A driver */
Kyle Guinnd661e622009-01-16 05:36:14 -0300624
Theodore Kilgore89f08632009-08-14 06:51:52 -0300625 memcpy(data, startup_string, 11);
626 if (sd->sensor_type)
627 data[5] = 0xbb;
628
629 switch (gspca_dev->width) {
630 case 160:
631 data[9] |= 0x04; /* reg 8, 2:1 scale down from 320 */
632 /* fall thru */
633 case 320:
634 default:
635 data[3] = 0x28; /* reg 2, H size/8 */
636 data[4] = 0x3c; /* reg 3, V size/4 */
637 data[6] = 0x14; /* reg 5, H start */
638 data[8] = 0x1a + sd->sensor_type; /* reg 7, V start */
639 break;
640 case 176:
641 data[9] |= 0x04; /* reg 8, 2:1 scale down from 352 */
642 /* fall thru */
643 case 352:
644 data[3] = 0x2c; /* reg 2, H size/8 */
645 data[4] = 0x48; /* reg 3, V size/4 */
646 data[6] = 0x06; /* reg 5, H start */
Theodore Kilgore32345b02009-11-01 12:59:42 -0300647 data[8] = 0x06 - sd->sensor_type; /* reg 7, V start */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300648 break;
649 }
650 err_code = mr_write(gspca_dev, 11);
651 if (err_code < 0)
652 return err_code;
653
654 if (!sd->sensor_type) {
655 const struct sensor_w_data cif_sensor0_init_data[] = {
656 {0x02, 0x00, {0x03, 0x5a, 0xb5, 0x01,
657 0x0f, 0x14, 0x0f, 0x10}, 8},
658 {0x0c, 0x00, {0x04, 0x01, 0x01, 0x00, 0x1f}, 5},
659 {0x12, 0x00, {0x07}, 1},
660 {0x1f, 0x00, {0x06}, 1},
661 {0x27, 0x00, {0x04}, 1},
662 {0x29, 0x00, {0x0c}, 1},
663 {0x40, 0x00, {0x40, 0x00, 0x04}, 3},
664 {0x50, 0x00, {0x60}, 1},
665 {0x60, 0x00, {0x06}, 1},
666 {0x6b, 0x00, {0x85, 0x85, 0xc8, 0xc8, 0xc8, 0xc8}, 6},
667 {0x72, 0x00, {0x1e, 0x56}, 2},
668 {0x75, 0x00, {0x58, 0x40, 0xa2, 0x02, 0x31, 0x02,
669 0x31, 0x80, 0x00}, 9},
670 {0x11, 0x00, {0x01}, 1},
671 {0, 0, {0}, 0}
672 };
673 err_code = sensor_write_regs(gspca_dev, cif_sensor0_init_data,
674 ARRAY_SIZE(cif_sensor0_init_data));
675 } else { /* sd->sensor_type = 1 */
676 const struct sensor_w_data cif_sensor1_init_data[] = {
Hans de Goede9ac69782009-08-14 10:15:52 -0300677 /* Reg 3,4, 7,8 get set by the controls */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300678 {0x02, 0x00, {0x10}, 1},
Hans de Goede9ac69782009-08-14 10:15:52 -0300679 {0x05, 0x01, {0x22}, 1}, /* 5/6 also seen as 65h/32h */
680 {0x06, 0x01, {0x00}, 1},
Theodore Kilgore89f08632009-08-14 06:51:52 -0300681 {0x09, 0x02, {0x0e}, 1},
682 {0x0a, 0x02, {0x05}, 1},
683 {0x0b, 0x02, {0x05}, 1},
684 {0x0c, 0x02, {0x0f}, 1},
Hans de Goede9ac69782009-08-14 10:15:52 -0300685 {0x0d, 0x02, {0x07}, 1},
Theodore Kilgore89f08632009-08-14 06:51:52 -0300686 {0x0e, 0x02, {0x0c}, 1},
687 {0x0f, 0x00, {0x00}, 1},
688 {0x10, 0x00, {0x06}, 1},
689 {0x11, 0x00, {0x07}, 1},
690 {0x12, 0x00, {0x00}, 1},
691 {0x13, 0x00, {0x01}, 1},
692 {0, 0, {0}, 0}
693 };
694 err_code = sensor_write_regs(gspca_dev, cif_sensor1_init_data,
695 ARRAY_SIZE(cif_sensor1_init_data));
696 }
Theodore Kilgore930bf782009-10-05 05:11:35 -0300697 return err_code;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300698}
699
700static int start_vga_cam(struct gspca_dev *gspca_dev)
701{
702 struct sd *sd = (struct sd *) gspca_dev;
703 __u8 *data = gspca_dev->usb_buf;
704 int err_code;
705 const __u8 startup_string[] = {0x00, 0x0d, 0x01, 0x00, 0x00, 0x2b,
706 0x00, 0x00, 0x00, 0x50, 0xc0};
Theodore Kilgore89f08632009-08-14 06:51:52 -0300707 /* What some of these mean is explained in start_cif_cam(), above */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300708
Theodore Kilgore89f08632009-08-14 06:51:52 -0300709 memcpy(data, startup_string, 11);
710 if (!sd->sensor_type) {
711 data[5] = 0x00;
712 data[10] = 0x91;
713 }
Kyle Guinnd661e622009-01-16 05:36:14 -0300714
715 switch (gspca_dev->width) {
716 case 160:
717 data[9] |= 0x0c; /* reg 8, 4:1 scale down */
718 /* fall thru */
719 case 320:
720 data[9] |= 0x04; /* reg 8, 2:1 scale down */
721 /* fall thru */
722 case 640:
723 default:
Theodore Kilgore89f08632009-08-14 06:51:52 -0300724 data[3] = 0x50; /* reg 2, H size/8 */
725 data[4] = 0x78; /* reg 3, V size/4 */
Kyle Guinnd661e622009-01-16 05:36:14 -0300726 data[6] = 0x04; /* reg 5, H start */
727 data[8] = 0x03; /* reg 7, V start */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300728 if (sd->do_lcd_stop)
729 data[8] = 0x04; /* Bayer tile shifted */
Kyle Guinnd661e622009-01-16 05:36:14 -0300730 break;
731
732 case 176:
733 data[9] |= 0x04; /* reg 8, 2:1 scale down */
734 /* fall thru */
735 case 352:
736 data[3] = 0x2c; /* reg 2, H size */
737 data[4] = 0x48; /* reg 3, V size */
738 data[6] = 0x94; /* reg 5, H start */
739 data[8] = 0x63; /* reg 7, V start */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300740 if (sd->do_lcd_stop)
741 data[8] = 0x64; /* Bayer tile shifted */
Kyle Guinnd661e622009-01-16 05:36:14 -0300742 break;
743 }
744
Theodore Kilgore89f08632009-08-14 06:51:52 -0300745 err_code = mr_write(gspca_dev, 11);
Kyle Guinnd661e622009-01-16 05:36:14 -0300746 if (err_code < 0)
747 return err_code;
748
Theodore Kilgore89f08632009-08-14 06:51:52 -0300749 if (!sd->sensor_type) {
750 /* The only known sensor_type 0 cam is the Argus DC-1620 */
751 const struct sensor_w_data vga_sensor0_init_data[] = {
752 {0x01, 0x00, {0x0c, 0x00, 0x04}, 3},
753 {0x14, 0x00, {0x01, 0xe4, 0x02, 0x84}, 4},
754 {0x20, 0x00, {0x00, 0x80, 0x00, 0x08}, 4},
755 {0x25, 0x00, {0x03, 0xa9, 0x80}, 3},
756 {0x30, 0x00, {0x30, 0x18, 0x10, 0x18}, 4},
757 {0, 0, {0}, 0}
758 };
759 err_code = sensor_write_regs(gspca_dev, vga_sensor0_init_data,
760 ARRAY_SIZE(vga_sensor0_init_data));
761 } else { /* sd->sensor_type = 1 */
762 const struct sensor_w_data vga_sensor1_init_data[] = {
763 {0x02, 0x00, {0x06, 0x59, 0x0c, 0x16, 0x00,
764 0x07, 0x00, 0x01}, 8},
765 {0x11, 0x04, {0x01}, 1},
766 /*{0x0a, 0x00, {0x00, 0x01, 0x00, 0x00, 0x01, */
767 {0x0a, 0x00, {0x01, 0x06, 0x00, 0x00, 0x01,
768 0x00, 0x0a}, 7},
769 {0x11, 0x04, {0x01}, 1},
770 {0x12, 0x00, {0x00, 0x63, 0x00, 0x70, 0x00, 0x00}, 6},
771 {0x11, 0x04, {0x01}, 1},
772 {0, 0, {0}, 0}
773 };
774 err_code = sensor_write_regs(gspca_dev, vga_sensor1_init_data,
775 ARRAY_SIZE(vga_sensor1_init_data));
776 }
Theodore Kilgore89f08632009-08-14 06:51:52 -0300777 return err_code;
778}
779
780static int sd_start(struct gspca_dev *gspca_dev)
781{
782 struct sd *sd = (struct sd *) gspca_dev;
783 int err_code;
Theodore Kilgore89f08632009-08-14 06:51:52 -0300784
Theodore Kilgore89f08632009-08-14 06:51:52 -0300785 sd->sof_read = 0;
Theodore Kilgore930bf782009-10-05 05:11:35 -0300786
787 /* Some of the VGA cameras require the memory pointer
788 * to be set to 0 again. We have been forced to start the
789 * stream somewhere else to detect the hardware, and closed it,
790 * and now since we are restarting the stream we need to do a
791 * completely fresh and clean start. */
792 err_code = zero_the_pointer(gspca_dev);
793 if (err_code < 0)
794 return err_code;
795
796 err_code = stream_start(gspca_dev);
797 if (err_code < 0)
798 return err_code;
799
Theodore Kilgore89f08632009-08-14 06:51:52 -0300800 if (sd->cam_type == CAM_TYPE_CIF) {
Theodore Kilgore89f08632009-08-14 06:51:52 -0300801 err_code = start_cif_cam(gspca_dev);
802 } else {
Theodore Kilgore89f08632009-08-14 06:51:52 -0300803 err_code = start_vga_cam(gspca_dev);
804 }
Theodore Kilgore930bf782009-10-05 05:11:35 -0300805 if (err_code < 0)
806 return err_code;
807
808 setbrightness(gspca_dev);
809 setexposure(gspca_dev);
810 setgain(gspca_dev);
811
812 return isoc_enable(gspca_dev);
Kyle Guinnd661e622009-01-16 05:36:14 -0300813}
814
815static void sd_stopN(struct gspca_dev *gspca_dev)
816{
Theodore Kilgore89f08632009-08-14 06:51:52 -0300817 struct sd *sd = (struct sd *) gspca_dev;
Kyle Guinnd661e622009-01-16 05:36:14 -0300818
Theodore Kilgore930bf782009-10-05 05:11:35 -0300819 stream_stop(gspca_dev);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300820 /* Not all the cams need this, but even if not, probably a good idea */
821 zero_the_pointer(gspca_dev);
Theodore Kilgore930bf782009-10-05 05:11:35 -0300822 if (sd->do_lcd_stop)
823 lcd_stop(gspca_dev);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300824}
825
826static void setbrightness(struct gspca_dev *gspca_dev)
827{
828 struct sd *sd = (struct sd *) gspca_dev;
829 u8 val;
Theodore Kilgore930bf782009-10-05 05:11:35 -0300830 u8 sign_reg = 7; /* This reg and the next one used on CIF cams. */
831 u8 value_reg = 8; /* VGA cams seem to use regs 0x0b and 0x0c */
832 const u8 quick_clix_table[] =
833 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
834 { 0, 4, 8, 12, 1, 2, 3, 5, 6, 9, 7, 10, 13, 11, 14, 15};
835 /*
836 * This control is disabled for CIF type 1 and VGA type 0 cameras.
837 * It does not quite act linearly for the Argus QuickClix camera,
838 * but it does control brightness. The values are 0 - 15 only, and
839 * the table above makes them act consecutively.
840 */
841 if ((gspca_dev->ctrl_dis & (1 << NORM_BRIGHTNESS_IDX)) &&
842 (gspca_dev->ctrl_dis & (1 << ARGUS_QC_BRIGHTNESS_IDX)))
Hans de Goede9ac69782009-08-14 10:15:52 -0300843 return;
844
Theodore Kilgore930bf782009-10-05 05:11:35 -0300845 if (sd->cam_type == CAM_TYPE_VGA) {
846 sign_reg += 4;
847 value_reg += 4;
848 }
849
Hans de Goede9ac69782009-08-14 10:15:52 -0300850 /* Note register 7 is also seen as 0x8x or 0xCx in dumps */
Theodore Kilgore89f08632009-08-14 06:51:52 -0300851 if (sd->brightness > 0) {
Theodore Kilgore930bf782009-10-05 05:11:35 -0300852 sensor_write1(gspca_dev, sign_reg, 0x00);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300853 val = sd->brightness;
854 } else {
Theodore Kilgore930bf782009-10-05 05:11:35 -0300855 sensor_write1(gspca_dev, sign_reg, 0x01);
856 val = (257 - sd->brightness);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300857 }
Theodore Kilgore930bf782009-10-05 05:11:35 -0300858 /* Use lookup table for funky Argus QuickClix brightness */
859 if (sd->do_lcd_stop)
860 val = quick_clix_table[val];
861
862 sensor_write1(gspca_dev, value_reg, val);
Theodore Kilgore89f08632009-08-14 06:51:52 -0300863}
864
865static void setexposure(struct gspca_dev *gspca_dev)
866{
867 struct sd *sd = (struct sd *) gspca_dev;
Theodore Kilgore930bf782009-10-05 05:11:35 -0300868 int exposure;
Hans de Goede065b6f72009-10-29 07:42:30 -0300869 u8 buf[2];
Theodore Kilgore89f08632009-08-14 06:51:52 -0300870
Hans de Goede9ac69782009-08-14 10:15:52 -0300871 if (gspca_dev->ctrl_dis & (1 << EXPOSURE_IDX))
872 return;
873
Theodore Kilgore930bf782009-10-05 05:11:35 -0300874 if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1) {
Hans de Goeded76f9752009-10-11 05:22:29 -0300875 /* This cam does not like exposure settings > 300,
876 so scale 0 - 4095 to 300 - 4095 */
877 exposure = (sd->exposure * 9267) / 10000 + 300;
Theodore Kilgore930bf782009-10-05 05:11:35 -0300878 sensor_write1(gspca_dev, 3, exposure >> 4);
879 sensor_write1(gspca_dev, 4, exposure & 0x0f);
Hans de Goedea2e081b2009-08-14 17:11:36 -0300880 } else {
Hans de Goedea2e081b2009-08-14 17:11:36 -0300881 /* We have both a clock divider and an exposure register.
882 We first calculate the clock divider, as that determines
883 the maximum exposure and then we calculayte the exposure
884 register setting (which goes from 0 - 511).
885
886 Note our 0 - 4095 exposure is mapped to 0 - 511
887 milliseconds exposure time */
Theodore Kilgore930bf782009-10-05 05:11:35 -0300888 u8 clockdiv = (60 * sd->exposure + 7999) / 8000;
Hans de Goedea2e081b2009-08-14 17:11:36 -0300889
890 /* Limit framerate to not exceed usb bandwidth */
Hans de Goede065b6f72009-10-29 07:42:30 -0300891 if (clockdiv < sd->min_clockdiv && gspca_dev->width >= 320)
892 clockdiv = sd->min_clockdiv;
Hans de Goedea2e081b2009-08-14 17:11:36 -0300893 else if (clockdiv < 2)
894 clockdiv = 2;
895
Theodore Kilgore930bf782009-10-05 05:11:35 -0300896 if (sd->cam_type == CAM_TYPE_VGA && clockdiv < 4)
897 clockdiv = 4;
898
Hans de Goedea2e081b2009-08-14 17:11:36 -0300899 /* Frame exposure time in ms = 1000 * clockdiv / 60 ->
900 exposure = (sd->exposure / 8) * 511 / (1000 * clockdiv / 60) */
901 exposure = (60 * 511 * sd->exposure) / (8000 * clockdiv);
902 if (exposure > 511)
903 exposure = 511;
904
905 /* exposure register value is reversed! */
906 exposure = 511 - exposure;
907
Hans de Goede065b6f72009-10-29 07:42:30 -0300908 buf[0] = exposure & 0xff;
909 buf[1] = exposure >> 8;
910 sensor_write_reg(gspca_dev, 0x0e, 0, buf, 2);
Hans de Goedea2e081b2009-08-14 17:11:36 -0300911 sensor_write1(gspca_dev, 0x02, clockdiv);
Hans de Goedea2e081b2009-08-14 17:11:36 -0300912 }
Theodore Kilgore89f08632009-08-14 06:51:52 -0300913}
914
915static void setgain(struct gspca_dev *gspca_dev)
916{
917 struct sd *sd = (struct sd *) gspca_dev;
918
Hans de Goede9ac69782009-08-14 10:15:52 -0300919 if (gspca_dev->ctrl_dis & (1 << GAIN_IDX))
920 return;
921
Theodore Kilgore930bf782009-10-05 05:11:35 -0300922 if (sd->cam_type == CAM_TYPE_CIF && sd->sensor_type == 1) {
Hans de Goede823902d2009-08-17 12:25:17 -0300923 sensor_write1(gspca_dev, 0x0e, sd->gain);
Hans de Goedea2e081b2009-08-14 17:11:36 -0300924 } else {
925 sensor_write1(gspca_dev, 0x10, sd->gain);
926 }
Theodore Kilgore89f08632009-08-14 06:51:52 -0300927}
928
929static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
930{
931 struct sd *sd = (struct sd *) gspca_dev;
932
933 sd->brightness = val;
934 if (gspca_dev->streaming)
935 setbrightness(gspca_dev);
936 return 0;
937}
938
939static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
940{
941 struct sd *sd = (struct sd *) gspca_dev;
942
943 *val = sd->brightness;
944 return 0;
945}
946
947static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
948{
949 struct sd *sd = (struct sd *) gspca_dev;
950
951 sd->exposure = val;
952 if (gspca_dev->streaming)
953 setexposure(gspca_dev);
954 return 0;
955}
956
957static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
958{
959 struct sd *sd = (struct sd *) gspca_dev;
960
961 *val = sd->exposure;
962 return 0;
963}
964
965static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
966{
967 struct sd *sd = (struct sd *) gspca_dev;
968
969 sd->gain = val;
970 if (gspca_dev->streaming)
971 setgain(gspca_dev);
972 return 0;
973}
974
975static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
976{
977 struct sd *sd = (struct sd *) gspca_dev;
978
979 *val = sd->gain;
980 return 0;
Kyle Guinnd661e622009-01-16 05:36:14 -0300981}
982
Hans de Goede065b6f72009-10-29 07:42:30 -0300983static int sd_setmin_clockdiv(struct gspca_dev *gspca_dev, __s32 val)
984{
985 struct sd *sd = (struct sd *) gspca_dev;
986
987 sd->min_clockdiv = val;
988 if (gspca_dev->streaming)
989 setexposure(gspca_dev);
990 return 0;
991}
992
993static int sd_getmin_clockdiv(struct gspca_dev *gspca_dev, __s32 *val)
994{
995 struct sd *sd = (struct sd *) gspca_dev;
996
997 *val = sd->min_clockdiv;
998 return 0;
999}
1000
Kyle Guinnd661e622009-01-16 05:36:14 -03001001/* Include pac common sof detection functions */
1002#include "pac_common.h"
1003
1004static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1005 struct gspca_frame *frame, /* target */
1006 __u8 *data, /* isoc packet */
1007 int len) /* iso packet length */
1008{
Kyle Guinnd661e622009-01-16 05:36:14 -03001009 unsigned char *sof;
1010
1011 sof = pac_find_sof(gspca_dev, data, len);
1012 if (sof) {
1013 int n;
1014
1015 /* finish decoding current frame */
1016 n = sof - data;
1017 if (n > sizeof pac_sof_marker)
1018 n -= sizeof pac_sof_marker;
1019 else
1020 n = 0;
1021 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame,
1022 data, n);
Theodore Kilgore9832d762009-03-13 13:04:31 -03001023 /* Start next frame. */
1024 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
1025 pac_sof_marker, sizeof pac_sof_marker);
Kyle Guinnd661e622009-01-16 05:36:14 -03001026 len -= sof - data;
1027 data = sof;
1028 }
Kyle Guinnd661e622009-01-16 05:36:14 -03001029 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
1030}
1031
1032/* sub-driver description */
1033static const struct sd_desc sd_desc = {
1034 .name = MODULE_NAME,
1035 .ctrls = sd_ctrls,
1036 .nctrls = ARRAY_SIZE(sd_ctrls),
1037 .config = sd_config,
1038 .init = sd_init,
1039 .start = sd_start,
1040 .stopN = sd_stopN,
1041 .pkt_scan = sd_pkt_scan,
1042};
1043
1044/* -- module initialisation -- */
1045static const __devinitdata struct usb_device_id device_table[] = {
Aurelien Jacobs8ac246c2009-10-29 07:45:24 -03001046 {USB_DEVICE(0x08ca, 0x0110)}, /* Trust Spyc@m 100 */
Theodore Kilgore89f08632009-08-14 06:51:52 -03001047 {USB_DEVICE(0x08ca, 0x0111)}, /* Aiptek Pencam VGA+ */
1048 {USB_DEVICE(0x093a, 0x010f)}, /* All other known MR97310A VGA cams */
1049 {USB_DEVICE(0x093a, 0x010e)}, /* All known MR97310A CIF cams */
Kyle Guinnd661e622009-01-16 05:36:14 -03001050 {}
1051};
1052MODULE_DEVICE_TABLE(usb, device_table);
1053
1054/* -- device connect -- */
1055static int sd_probe(struct usb_interface *intf,
1056 const struct usb_device_id *id)
1057{
1058 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1059 THIS_MODULE);
1060}
1061
1062static struct usb_driver sd_driver = {
1063 .name = MODULE_NAME,
1064 .id_table = device_table,
1065 .probe = sd_probe,
1066 .disconnect = gspca_disconnect,
1067#ifdef CONFIG_PM
1068 .suspend = gspca_suspend,
1069 .resume = gspca_resume,
1070#endif
1071};
1072
1073/* -- module insert / remove -- */
1074static int __init sd_mod_init(void)
1075{
Alexey Klimov5d3fa302009-03-27 15:57:46 -03001076 int ret;
1077
1078 ret = usb_register(&sd_driver);
1079 if (ret < 0)
1080 return ret;
Kyle Guinnd661e622009-01-16 05:36:14 -03001081 PDEBUG(D_PROBE, "registered");
1082 return 0;
1083}
1084static void __exit sd_mod_exit(void)
1085{
1086 usb_deregister(&sd_driver);
1087 PDEBUG(D_PROBE, "deregistered");
1088}
1089
1090module_init(sd_mod_init);
1091module_exit(sd_mod_exit);