[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / media / video / gspca / gl860 / gl860.c
blob6ef59ac7f502c985bd881c3b876dd215daa10bb4
1 /* @file gl860.c
2 * @date 2009-08-27
4 * Genesys Logic webcam with gl860 subdrivers
6 * Driver by Olivier Lorin <o.lorin@laposte.net>
7 * GSPCA by Jean-Francois Moine <http://moinejf.free.fr>
8 * Thanks BUGabundo and Malmostoso for your amazing help!
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "gspca.h"
24 #include "gl860.h"
26 MODULE_AUTHOR("Olivier Lorin <lorin@laposte.net>");
27 MODULE_DESCRIPTION("GSPCA/Genesys Logic GL860 USB Camera Driver");
28 MODULE_LICENSE("GPL");
30 /*======================== static function declarations ====================*/
32 static void (*dev_init_settings)(struct gspca_dev *gspca_dev);
34 static int sd_config(struct gspca_dev *gspca_dev,
35 const struct usb_device_id *id);
36 static int sd_init(struct gspca_dev *gspca_dev);
37 static int sd_isoc_init(struct gspca_dev *gspca_dev);
38 static int sd_start(struct gspca_dev *gspca_dev);
39 static void sd_stop0(struct gspca_dev *gspca_dev);
40 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
41 struct gspca_frame *frame, u8 *data, s32 len);
42 static void sd_callback(struct gspca_dev *gspca_dev);
44 static int gl860_guess_sensor(struct gspca_dev *gspca_dev,
45 s32 vendor_id, s32 product_id);
47 /*============================ driver options ==============================*/
49 static s32 AC50Hz = 0xff;
50 module_param(AC50Hz, int, 0644);
51 MODULE_PARM_DESC(AC50Hz, " Does AC power frequency is 50Hz? (0/1)");
53 static char sensor[7];
54 module_param_string(sensor, sensor, sizeof(sensor), 0644);
55 MODULE_PARM_DESC(sensor,
56 " Driver sensor ('MI1320'/'MI2020'/'OV9655'/'OV2640'/'')");
58 /*============================ webcam controls =============================*/
60 /* Functions to get and set a control value */
61 #define SD_SETGET(thename) \
62 static int sd_set_##thename(struct gspca_dev *gspca_dev, s32 val)\
64 struct sd *sd = (struct sd *) gspca_dev;\
66 sd->vcur.thename = val;\
67 if (gspca_dev->streaming)\
68 sd->dev_camera_settings(gspca_dev);\
69 return 0;\
70 } \
71 static int sd_get_##thename(struct gspca_dev *gspca_dev, s32 *val)\
73 struct sd *sd = (struct sd *) gspca_dev;\
75 *val = sd->vcur.thename;\
76 return 0;\
79 SD_SETGET(mirror)
80 SD_SETGET(flip)
81 SD_SETGET(AC50Hz)
82 SD_SETGET(backlight)
83 SD_SETGET(brightness)
84 SD_SETGET(gamma)
85 SD_SETGET(hue)
86 SD_SETGET(saturation)
87 SD_SETGET(sharpness)
88 SD_SETGET(whitebal)
89 SD_SETGET(contrast)
91 #define GL860_NCTRLS 11
93 /* control table */
94 static struct ctrl sd_ctrls_mi1320[GL860_NCTRLS];
95 static struct ctrl sd_ctrls_mi2020[GL860_NCTRLS];
96 static struct ctrl sd_ctrls_mi2020b[GL860_NCTRLS];
97 static struct ctrl sd_ctrls_ov2640[GL860_NCTRLS];
98 static struct ctrl sd_ctrls_ov9655[GL860_NCTRLS];
100 #define SET_MY_CTRL(theid, \
101 thetype, thelabel, thename) \
102 if (sd->vmax.thename != 0) {\
103 sd_ctrls[nCtrls].qctrl.id = theid;\
104 sd_ctrls[nCtrls].qctrl.type = thetype;\
105 strcpy(sd_ctrls[nCtrls].qctrl.name, thelabel);\
106 sd_ctrls[nCtrls].qctrl.minimum = 0;\
107 sd_ctrls[nCtrls].qctrl.maximum = sd->vmax.thename;\
108 sd_ctrls[nCtrls].qctrl.default_value = sd->vcur.thename;\
109 sd_ctrls[nCtrls].qctrl.step = \
110 (sd->vmax.thename < 16) ? 1 : sd->vmax.thename/16;\
111 sd_ctrls[nCtrls].set = sd_set_##thename;\
112 sd_ctrls[nCtrls].get = sd_get_##thename;\
113 nCtrls++;\
116 static int gl860_build_control_table(struct gspca_dev *gspca_dev)
118 struct sd *sd = (struct sd *) gspca_dev;
119 struct ctrl *sd_ctrls;
120 int nCtrls = 0;
122 if (_MI1320_)
123 sd_ctrls = sd_ctrls_mi1320;
124 else if (_MI2020_)
125 sd_ctrls = sd_ctrls_mi2020;
126 else if (_MI2020b_)
127 sd_ctrls = sd_ctrls_mi2020b;
128 else if (_OV2640_)
129 sd_ctrls = sd_ctrls_ov2640;
130 else if (_OV9655_)
131 sd_ctrls = sd_ctrls_ov9655;
132 else
133 return 0;
135 memset(sd_ctrls, 0, GL860_NCTRLS * sizeof(struct ctrl));
137 SET_MY_CTRL(V4L2_CID_BRIGHTNESS,
138 V4L2_CTRL_TYPE_INTEGER, "Brightness", brightness)
139 SET_MY_CTRL(V4L2_CID_SHARPNESS,
140 V4L2_CTRL_TYPE_INTEGER, "Sharpness", sharpness)
141 SET_MY_CTRL(V4L2_CID_CONTRAST,
142 V4L2_CTRL_TYPE_INTEGER, "Contrast", contrast)
143 SET_MY_CTRL(V4L2_CID_GAMMA,
144 V4L2_CTRL_TYPE_INTEGER, "Gamma", gamma)
145 SET_MY_CTRL(V4L2_CID_HUE,
146 V4L2_CTRL_TYPE_INTEGER, "Palette", hue)
147 SET_MY_CTRL(V4L2_CID_SATURATION,
148 V4L2_CTRL_TYPE_INTEGER, "Saturation", saturation)
149 SET_MY_CTRL(V4L2_CID_WHITE_BALANCE_TEMPERATURE,
150 V4L2_CTRL_TYPE_INTEGER, "White Bal.", whitebal)
151 SET_MY_CTRL(V4L2_CID_BACKLIGHT_COMPENSATION,
152 V4L2_CTRL_TYPE_INTEGER, "Backlight" , backlight)
154 SET_MY_CTRL(V4L2_CID_HFLIP,
155 V4L2_CTRL_TYPE_BOOLEAN, "Mirror", mirror)
156 SET_MY_CTRL(V4L2_CID_VFLIP,
157 V4L2_CTRL_TYPE_BOOLEAN, "Flip", flip)
158 SET_MY_CTRL(V4L2_CID_POWER_LINE_FREQUENCY,
159 V4L2_CTRL_TYPE_BOOLEAN, "50Hz", AC50Hz)
161 return nCtrls;
164 /*==================== sud-driver structure initialisation =================*/
166 static struct sd_desc sd_desc_mi1320 = {
167 .name = MODULE_NAME,
168 .ctrls = sd_ctrls_mi1320,
169 .nctrls = GL860_NCTRLS,
170 .config = sd_config,
171 .init = sd_init,
172 .isoc_init = sd_isoc_init,
173 .start = sd_start,
174 .stop0 = sd_stop0,
175 .pkt_scan = sd_pkt_scan,
176 .dq_callback = sd_callback,
179 static struct sd_desc sd_desc_mi2020 = {
180 .name = MODULE_NAME,
181 .ctrls = sd_ctrls_mi2020,
182 .nctrls = GL860_NCTRLS,
183 .config = sd_config,
184 .init = sd_init,
185 .isoc_init = sd_isoc_init,
186 .start = sd_start,
187 .stop0 = sd_stop0,
188 .pkt_scan = sd_pkt_scan,
189 .dq_callback = sd_callback,
192 static struct sd_desc sd_desc_mi2020b = {
193 .name = MODULE_NAME,
194 .ctrls = sd_ctrls_mi2020b,
195 .nctrls = GL860_NCTRLS,
196 .config = sd_config,
197 .init = sd_init,
198 .isoc_init = sd_isoc_init,
199 .start = sd_start,
200 .stop0 = sd_stop0,
201 .pkt_scan = sd_pkt_scan,
202 .dq_callback = sd_callback,
205 static struct sd_desc sd_desc_ov2640 = {
206 .name = MODULE_NAME,
207 .ctrls = sd_ctrls_ov2640,
208 .nctrls = GL860_NCTRLS,
209 .config = sd_config,
210 .init = sd_init,
211 .isoc_init = sd_isoc_init,
212 .start = sd_start,
213 .stop0 = sd_stop0,
214 .pkt_scan = sd_pkt_scan,
215 .dq_callback = sd_callback,
218 static struct sd_desc sd_desc_ov9655 = {
219 .name = MODULE_NAME,
220 .ctrls = sd_ctrls_ov9655,
221 .nctrls = GL860_NCTRLS,
222 .config = sd_config,
223 .init = sd_init,
224 .isoc_init = sd_isoc_init,
225 .start = sd_start,
226 .stop0 = sd_stop0,
227 .pkt_scan = sd_pkt_scan,
228 .dq_callback = sd_callback,
231 /*=========================== sub-driver image sizes =======================*/
233 static struct v4l2_pix_format mi2020_mode[] = {
234 { 640, 480, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
235 .bytesperline = 640,
236 .sizeimage = 640 * 480,
237 .colorspace = V4L2_COLORSPACE_SRGB,
238 .priv = 0
240 { 800, 600, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
241 .bytesperline = 800,
242 .sizeimage = 800 * 600,
243 .colorspace = V4L2_COLORSPACE_SRGB,
244 .priv = 1
246 {1280, 1024, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
247 .bytesperline = 1280,
248 .sizeimage = 1280 * 1024,
249 .colorspace = V4L2_COLORSPACE_SRGB,
250 .priv = 2
252 {1600, 1200, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
253 .bytesperline = 1600,
254 .sizeimage = 1600 * 1200,
255 .colorspace = V4L2_COLORSPACE_SRGB,
256 .priv = 3
260 static struct v4l2_pix_format ov2640_mode[] = {
261 { 640, 480, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
262 .bytesperline = 640,
263 .sizeimage = 640 * 480,
264 .colorspace = V4L2_COLORSPACE_SRGB,
265 .priv = 0
267 { 800, 600, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
268 .bytesperline = 800,
269 .sizeimage = 800 * 600,
270 .colorspace = V4L2_COLORSPACE_SRGB,
271 .priv = 1
273 {1280, 960, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
274 .bytesperline = 1280,
275 .sizeimage = 1280 * 960,
276 .colorspace = V4L2_COLORSPACE_SRGB,
277 .priv = 2
279 {1600, 1200, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
280 .bytesperline = 1600,
281 .sizeimage = 1600 * 1200,
282 .colorspace = V4L2_COLORSPACE_SRGB,
283 .priv = 3
287 static struct v4l2_pix_format mi1320_mode[] = {
288 { 640, 480, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
289 .bytesperline = 640,
290 .sizeimage = 640 * 480,
291 .colorspace = V4L2_COLORSPACE_SRGB,
292 .priv = 0
294 { 800, 600, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
295 .bytesperline = 800,
296 .sizeimage = 800 * 600,
297 .colorspace = V4L2_COLORSPACE_SRGB,
298 .priv = 1
300 {1280, 960, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
301 .bytesperline = 1280,
302 .sizeimage = 1280 * 960,
303 .colorspace = V4L2_COLORSPACE_SRGB,
304 .priv = 2
308 static struct v4l2_pix_format ov9655_mode[] = {
309 { 640, 480, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
310 .bytesperline = 640,
311 .sizeimage = 640 * 480,
312 .colorspace = V4L2_COLORSPACE_SRGB,
313 .priv = 0
315 {1280, 960, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
316 .bytesperline = 1280,
317 .sizeimage = 1280 * 960,
318 .colorspace = V4L2_COLORSPACE_SRGB,
319 .priv = 1
323 /*========================= sud-driver functions ===========================*/
325 /* This function is called at probe time */
326 static int sd_config(struct gspca_dev *gspca_dev,
327 const struct usb_device_id *id)
329 struct sd *sd = (struct sd *) gspca_dev;
330 struct cam *cam;
331 s32 vendor_id, product_id;
333 /* Get USB VendorID and ProductID */
334 vendor_id = le16_to_cpu(id->idVendor);
335 product_id = le16_to_cpu(id->idProduct);
337 sd->nbRightUp = 1;
338 sd->nbIm = -1;
340 sd->sensor = 0xff;
341 if (strcmp(sensor, "MI1320") == 0)
342 sd->sensor = ID_MI1320;
343 else if (strcmp(sensor, "OV2640") == 0)
344 sd->sensor = ID_OV2640;
345 else if (strcmp(sensor, "OV9655") == 0)
346 sd->sensor = ID_OV9655;
347 else if (strcmp(sensor, "MI2020") == 0)
348 sd->sensor = ID_MI2020;
349 else if (strcmp(sensor, "MI2020b") == 0)
350 sd->sensor = ID_MI2020b;
352 /* Get sensor and set the suitable init/start/../stop functions */
353 if (gl860_guess_sensor(gspca_dev, vendor_id, product_id) == -1)
354 return -1;
356 cam = &gspca_dev->cam;
357 gspca_dev->nbalt = 4;
359 switch (sd->sensor) {
360 case ID_MI1320:
361 gspca_dev->sd_desc = &sd_desc_mi1320;
362 cam->cam_mode = mi1320_mode;
363 cam->nmodes = ARRAY_SIZE(mi1320_mode);
364 dev_init_settings = mi1320_init_settings;
365 break;
367 case ID_MI2020:
368 gspca_dev->sd_desc = &sd_desc_mi2020;
369 cam->cam_mode = mi2020_mode;
370 cam->nmodes = ARRAY_SIZE(mi2020_mode);
371 dev_init_settings = mi2020_init_settings;
372 break;
374 case ID_MI2020b:
375 gspca_dev->sd_desc = &sd_desc_mi2020b;
376 cam->cam_mode = mi2020_mode;
377 cam->nmodes = ARRAY_SIZE(mi2020_mode);
378 dev_init_settings = mi2020_init_settings;
379 break;
381 case ID_OV2640:
382 gspca_dev->sd_desc = &sd_desc_ov2640;
383 cam->cam_mode = ov2640_mode;
384 cam->nmodes = ARRAY_SIZE(ov2640_mode);
385 dev_init_settings = ov2640_init_settings;
386 break;
388 case ID_OV9655:
389 gspca_dev->sd_desc = &sd_desc_ov9655;
390 cam->cam_mode = ov9655_mode;
391 cam->nmodes = ARRAY_SIZE(ov9655_mode);
392 dev_init_settings = ov9655_init_settings;
393 break;
396 dev_init_settings(gspca_dev);
397 if (AC50Hz != 0xff)
398 ((struct sd *) gspca_dev)->vcur.AC50Hz = AC50Hz;
399 gl860_build_control_table(gspca_dev);
401 return 0;
404 /* This function is called at probe time after sd_config */
405 static int sd_init(struct gspca_dev *gspca_dev)
407 struct sd *sd = (struct sd *) gspca_dev;
409 return sd->dev_init_at_startup(gspca_dev);
412 /* This function is called before to choose the alt setting */
413 static int sd_isoc_init(struct gspca_dev *gspca_dev)
415 struct sd *sd = (struct sd *) gspca_dev;
417 return sd->dev_configure_alt(gspca_dev);
420 /* This function is called to start the webcam */
421 static int sd_start(struct gspca_dev *gspca_dev)
423 struct sd *sd = (struct sd *) gspca_dev;
425 return sd->dev_init_pre_alt(gspca_dev);
428 /* This function is called to stop the webcam */
429 static void sd_stop0(struct gspca_dev *gspca_dev)
431 struct sd *sd = (struct sd *) gspca_dev;
433 return sd->dev_post_unset_alt(gspca_dev);
436 /* This function is called when an image is being received */
437 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
438 struct gspca_frame *frame, u8 *data, s32 len)
440 struct sd *sd = (struct sd *) gspca_dev;
441 static s32 nSkipped;
443 s32 mode = (s32) gspca_dev->curr_mode;
444 s32 nToSkip =
445 sd->swapRB * (gspca_dev->cam.cam_mode[mode].bytesperline + 1);
447 /* Test only against 0202h, so endianess does not matter */
448 switch (*(s16 *) data) {
449 case 0x0202: /* End of frame, start a new one */
450 frame = gspca_frame_add(gspca_dev, LAST_PACKET, frame, data, 0);
451 nSkipped = 0;
452 if (sd->nbIm >= 0 && sd->nbIm < 10)
453 sd->nbIm++;
454 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, data, 0);
455 break;
457 default:
458 data += 2;
459 len -= 2;
460 if (nSkipped + len <= nToSkip)
461 nSkipped += len;
462 else {
463 if (nSkipped < nToSkip && nSkipped + len > nToSkip) {
464 data += nToSkip - nSkipped;
465 len -= nToSkip - nSkipped;
466 nSkipped = nToSkip + 1;
468 gspca_frame_add(gspca_dev,
469 INTER_PACKET, frame, data, len);
471 break;
475 /* This function is called when an image has been read */
476 /* This function is used to monitor webcam orientation */
477 static void sd_callback(struct gspca_dev *gspca_dev)
479 struct sd *sd = (struct sd *) gspca_dev;
481 if (!_OV9655_) {
482 u8 state;
483 u8 upsideDown;
485 /* Probe sensor orientation */
486 ctrl_in(gspca_dev, 0xc0, 2, 0x0000, 0x0000, 1, (void *)&state);
488 /* C8/40 means upside-down (looking backwards) */
489 /* D8/50 means right-up (looking onwards) */
490 upsideDown = (state == 0xc8 || state == 0x40);
492 if (upsideDown && sd->nbRightUp > -4) {
493 if (sd->nbRightUp > 0)
494 sd->nbRightUp = 0;
495 if (sd->nbRightUp == -3) {
496 sd->mirrorMask = 1;
497 sd->waitSet = 1;
499 sd->nbRightUp--;
501 if (!upsideDown && sd->nbRightUp < 4) {
502 if (sd->nbRightUp < 0)
503 sd->nbRightUp = 0;
504 if (sd->nbRightUp == 3) {
505 sd->mirrorMask = 0;
506 sd->waitSet = 1;
508 sd->nbRightUp++;
512 if (sd->waitSet)
513 sd->dev_camera_settings(gspca_dev);
516 /*=================== USB driver structure initialisation ==================*/
518 static const __devinitdata struct usb_device_id device_table[] = {
519 {USB_DEVICE(0x05e3, 0x0503)},
520 {USB_DEVICE(0x05e3, 0xf191)},
524 MODULE_DEVICE_TABLE(usb, device_table);
526 static int sd_probe(struct usb_interface *intf,
527 const struct usb_device_id *id)
529 struct gspca_dev *gspca_dev;
530 s32 ret;
532 ret = gspca_dev_probe(intf, id,
533 &sd_desc_mi1320, sizeof(struct sd), THIS_MODULE);
535 if (ret >= 0) {
536 gspca_dev = usb_get_intfdata(intf);
538 PDEBUG(D_PROBE,
539 "Camera is now controlling video device /dev/video%d",
540 gspca_dev->vdev.minor);
543 return ret;
546 static void sd_disconnect(struct usb_interface *intf)
548 gspca_disconnect(intf);
551 static struct usb_driver sd_driver = {
552 .name = MODULE_NAME,
553 .id_table = device_table,
554 .probe = sd_probe,
555 .disconnect = sd_disconnect,
556 #ifdef CONFIG_PM
557 .suspend = gspca_suspend,
558 .resume = gspca_resume,
559 #endif
562 /*====================== Init and Exit module functions ====================*/
564 static int __init sd_mod_init(void)
566 PDEBUG(D_PROBE, "driver startup - version %s", DRIVER_VERSION);
568 if (usb_register(&sd_driver) < 0)
569 return -1;
570 PDEBUG(D_PROBE, "driver registered");
572 return 0;
575 static void __exit sd_mod_exit(void)
577 usb_deregister(&sd_driver);
578 PDEBUG(D_PROBE, "driver deregistered");
581 module_init(sd_mod_init);
582 module_exit(sd_mod_exit);
584 /*==========================================================================*/
586 int gl860_RTx(struct gspca_dev *gspca_dev,
587 unsigned char pref, u32 req, u16 val, u16 index,
588 s32 len, void *pdata)
590 struct usb_device *udev = gspca_dev->dev;
591 s32 r = 0;
593 if (pref == 0x40) { /* Send */
594 if (len > 0) {
595 memcpy(gspca_dev->usb_buf, pdata, len);
596 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
597 req, pref, val, index,
598 gspca_dev->usb_buf,
599 len, 400 + 200 * (len > 1));
600 } else {
601 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
602 req, pref, val, index, NULL, len, 400);
604 } else { /* Receive */
605 if (len > 0) {
606 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
607 req, pref, val, index,
608 gspca_dev->usb_buf,
609 len, 400 + 200 * (len > 1));
610 memcpy(pdata, gspca_dev->usb_buf, len);
611 } else {
612 r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
613 req, pref, val, index, NULL, len, 400);
617 if (r < 0)
618 PDEBUG(D_ERR,
619 "ctrl transfer failed %4d "
620 "[p%02x r%d v%04x i%04x len%d]",
621 r, pref, req, val, index, len);
622 else if (len > 1 && r < len)
623 PDEBUG(D_ERR, "short ctrl transfer %d/%d", r, len);
625 if ((_MI2020_ || _MI2020b_ || _MI2020c_) && (val || index))
626 msleep(1);
627 if (_OV2640_)
628 msleep(1);
630 return r;
633 int fetch_validx(struct gspca_dev *gspca_dev, struct validx *tbl, int len)
635 int n;
637 for (n = 0; n < len; n++) {
638 if (tbl[n].idx != 0xffff)
639 ctrl_out(gspca_dev, 0x40, 1, tbl[n].val,
640 tbl[n].idx, 0, NULL);
641 else if (tbl[n].val == 0xffff)
642 break;
643 else
644 msleep(tbl[n].val);
646 return n;
649 int keep_on_fetching_validx(struct gspca_dev *gspca_dev, struct validx *tbl,
650 int len, int n)
652 while (++n < len) {
653 if (tbl[n].idx != 0xffff)
654 ctrl_out(gspca_dev, 0x40, 1, tbl[n].val, tbl[n].idx,
655 0, NULL);
656 else if (tbl[n].val == 0xffff)
657 break;
658 else
659 msleep(tbl[n].val);
661 return n;
664 void fetch_idxdata(struct gspca_dev *gspca_dev, struct idxdata *tbl, int len)
666 int n;
668 for (n = 0; n < len; n++) {
669 if (memcmp(tbl[n].data, "\xff\xff\xff", 3) != 0)
670 ctrl_out(gspca_dev, 0x40, 3, 0x7a00, tbl[n].idx,
671 3, tbl[n].data);
672 else
673 msleep(tbl[n].idx);
677 static int gl860_guess_sensor(struct gspca_dev *gspca_dev,
678 s32 vendor_id, s32 product_id)
680 struct sd *sd = (struct sd *) gspca_dev;
681 u8 probe, nb26, nb96, nOV, ntry;
683 if (product_id == 0xf191)
684 sd->sensor = ID_MI1320;
686 if (sd->sensor == 0xff) {
687 ctrl_in(gspca_dev, 0xc0, 2, 0x0000, 0x0004, 1, &probe);
688 ctrl_in(gspca_dev, 0xc0, 2, 0x0000, 0x0004, 1, &probe);
690 ctrl_out(gspca_dev, 0x40, 1, 0x0000, 0x0000, 0, NULL);
691 msleep(3);
692 ctrl_out(gspca_dev, 0x40, 1, 0x0010, 0x0010, 0, NULL);
693 msleep(3);
694 ctrl_out(gspca_dev, 0x40, 1, 0x0008, 0x00c0, 0, NULL);
695 msleep(3);
696 ctrl_out(gspca_dev, 0x40, 1, 0x0001, 0x00c1, 0, NULL);
697 msleep(3);
698 ctrl_out(gspca_dev, 0x40, 1, 0x0001, 0x00c2, 0, NULL);
699 msleep(3);
700 ctrl_out(gspca_dev, 0x40, 1, 0x0020, 0x0006, 0, NULL);
701 msleep(3);
702 ctrl_out(gspca_dev, 0x40, 1, 0x006a, 0x000d, 0, NULL);
703 msleep(56);
705 nOV = 0;
706 for (ntry = 0; ntry < 4; ntry++) {
707 ctrl_out(gspca_dev, 0x40, 1, 0x0040, 0x0000, 0, NULL);
708 msleep(3);
709 ctrl_out(gspca_dev, 0x40, 1, 0x0063, 0x0006, 0, NULL);
710 msleep(3);
711 ctrl_out(gspca_dev, 0x40, 1, 0x7a00, 0x8030, 0, NULL);
712 msleep(10);
713 ctrl_in(gspca_dev, 0xc0, 2, 0x7a00, 0x8030, 1, &probe);
714 PDEBUG(D_PROBE, "1st probe=%02x", probe);
715 if (probe == 0xff)
716 nOV++;
719 if (nOV) {
720 PDEBUG(D_PROBE, "0xff -> sensor OVXXXX");
721 PDEBUG(D_PROBE, "Probing for sensor OV2640 or OV9655");
723 nb26 = nb96 = 0;
724 for (ntry = 0; ntry < 4; ntry++) {
725 ctrl_out(gspca_dev, 0x40, 1, 0x0040, 0x0000,
726 0, NULL);
727 msleep(3);
728 ctrl_out(gspca_dev, 0x40, 1, 0x6000, 0x800a,
729 0, NULL);
730 msleep(10);
731 /* Wait for 26(OV2640) or 96(OV9655) */
732 ctrl_in(gspca_dev, 0xc0, 2, 0x6000, 0x800a,
733 1, &probe);
735 PDEBUG(D_PROBE, "2nd probe=%02x", probe);
736 if (probe == 0x00)
737 nb26++;
738 if (probe == 0x26 || probe == 0x40) {
739 sd->sensor = ID_OV2640;
740 nb26 += 4;
741 break;
743 if (probe == 0x96 || probe == 0x55) {
744 sd->sensor = ID_OV9655;
745 nb96 += 4;
746 break;
748 if (probe == 0xff)
749 nb96++;
750 msleep(3);
752 if (nb26 < 4 && nb96 < 4) {
753 PDEBUG(D_PROBE, "No relevant answer ");
754 PDEBUG(D_PROBE, "* 1.3Mpixels -> use OV9655");
755 PDEBUG(D_PROBE, "* 2.0Mpixels -> use OV2640");
756 PDEBUG(D_PROBE,
757 "To force a sensor, add that line to "
758 "/etc/modprobe.d/options.conf:");
759 PDEBUG(D_PROBE, "options gspca_gl860 "
760 "sensor=\"OV2640\" or \"OV9655\"");
761 return -1;
763 } else { /* probe = 0 */
764 PDEBUG(D_PROBE, "No 0xff -> sensor MI2020");
765 sd->sensor = ID_MI2020;
769 if (_MI1320_) {
770 PDEBUG(D_PROBE, "05e3:f191 sensor MI1320 (1.3M)");
771 } else if (_MI2020_) {
772 PDEBUG(D_PROBE, "05e3:0503 sensor MI2020 (2.0M)");
773 } else if (_MI2020b_) {
774 PDEBUG(D_PROBE, "05e3:0503 sensor MI2020 alt. driver (2.0M)");
775 } else if (_OV9655_) {
776 PDEBUG(D_PROBE, "05e3:0503 sensor OV9655 (1.3M)");
777 } else if (_OV2640_) {
778 PDEBUG(D_PROBE, "05e3:0503 sensor OV2640 (2.0M)");
779 } else {
780 PDEBUG(D_PROBE, "***** Unknown sensor *****");
781 return -1;
784 return 0;