/drivers/media/video/gspca/gl860/gl860.c

https://bitbucket.org/wisechild/galaxy-nexus · C · 735 lines · 595 code · 94 blank · 46 comment · 107 complexity · 5686c82c295f4cca9185208575bdd995 MD5 · raw file

  1. /* GSPCA subdrivers for Genesys Logic webcams with the GL860 chip
  2. * Subdriver core
  3. *
  4. * 2009/09/24 Olivier Lorin <o.lorin@laposte.net>
  5. * GSPCA by Jean-Francois Moine <http://moinejf.free.fr>
  6. * Thanks BUGabundo and Malmostoso for your amazing help!
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "gspca.h"
  22. #include "gl860.h"
  23. MODULE_AUTHOR("Olivier Lorin <o.lorin@laposte.net>");
  24. MODULE_DESCRIPTION("Genesys Logic USB PC Camera Driver");
  25. MODULE_LICENSE("GPL");
  26. /*======================== static function declarations ====================*/
  27. static void (*dev_init_settings)(struct gspca_dev *gspca_dev);
  28. static int sd_config(struct gspca_dev *gspca_dev,
  29. const struct usb_device_id *id);
  30. static int sd_init(struct gspca_dev *gspca_dev);
  31. static int sd_isoc_init(struct gspca_dev *gspca_dev);
  32. static int sd_start(struct gspca_dev *gspca_dev);
  33. static void sd_stop0(struct gspca_dev *gspca_dev);
  34. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  35. u8 *data, int len);
  36. static void sd_callback(struct gspca_dev *gspca_dev);
  37. static int gl860_guess_sensor(struct gspca_dev *gspca_dev,
  38. u16 vendor_id, u16 product_id);
  39. /*============================ driver options ==============================*/
  40. static s32 AC50Hz = 0xff;
  41. module_param(AC50Hz, int, 0644);
  42. MODULE_PARM_DESC(AC50Hz, " Does AC power frequency is 50Hz? (0/1)");
  43. static char sensor[7];
  44. module_param_string(sensor, sensor, sizeof(sensor), 0644);
  45. MODULE_PARM_DESC(sensor,
  46. " Driver sensor ('MI1320'/'MI2020'/'OV9655'/'OV2640')");
  47. /*============================ webcam controls =============================*/
  48. /* Functions to get and set a control value */
  49. #define SD_SETGET(thename) \
  50. static int sd_set_##thename(struct gspca_dev *gspca_dev, s32 val)\
  51. {\
  52. struct sd *sd = (struct sd *) gspca_dev;\
  53. \
  54. sd->vcur.thename = val;\
  55. if (gspca_dev->streaming)\
  56. sd->waitSet = 1;\
  57. return 0;\
  58. } \
  59. static int sd_get_##thename(struct gspca_dev *gspca_dev, s32 *val)\
  60. {\
  61. struct sd *sd = (struct sd *) gspca_dev;\
  62. \
  63. *val = sd->vcur.thename;\
  64. return 0;\
  65. }
  66. SD_SETGET(mirror)
  67. SD_SETGET(flip)
  68. SD_SETGET(AC50Hz)
  69. SD_SETGET(backlight)
  70. SD_SETGET(brightness)
  71. SD_SETGET(gamma)
  72. SD_SETGET(hue)
  73. SD_SETGET(saturation)
  74. SD_SETGET(sharpness)
  75. SD_SETGET(whitebal)
  76. SD_SETGET(contrast)
  77. #define GL860_NCTRLS 11
  78. /* control table */
  79. static struct ctrl sd_ctrls_mi1320[GL860_NCTRLS];
  80. static struct ctrl sd_ctrls_mi2020[GL860_NCTRLS];
  81. static struct ctrl sd_ctrls_ov2640[GL860_NCTRLS];
  82. static struct ctrl sd_ctrls_ov9655[GL860_NCTRLS];
  83. #define SET_MY_CTRL(theid, \
  84. thetype, thelabel, thename) \
  85. if (sd->vmax.thename != 0) {\
  86. sd_ctrls[nCtrls].qctrl.id = theid;\
  87. sd_ctrls[nCtrls].qctrl.type = thetype;\
  88. strcpy(sd_ctrls[nCtrls].qctrl.name, thelabel);\
  89. sd_ctrls[nCtrls].qctrl.minimum = 0;\
  90. sd_ctrls[nCtrls].qctrl.maximum = sd->vmax.thename;\
  91. sd_ctrls[nCtrls].qctrl.default_value = sd->vcur.thename;\
  92. sd_ctrls[nCtrls].qctrl.step = \
  93. (sd->vmax.thename < 16) ? 1 : sd->vmax.thename/16;\
  94. sd_ctrls[nCtrls].set = sd_set_##thename;\
  95. sd_ctrls[nCtrls].get = sd_get_##thename;\
  96. nCtrls++;\
  97. }
  98. static int gl860_build_control_table(struct gspca_dev *gspca_dev)
  99. {
  100. struct sd *sd = (struct sd *) gspca_dev;
  101. struct ctrl *sd_ctrls;
  102. int nCtrls = 0;
  103. if (_MI1320_)
  104. sd_ctrls = sd_ctrls_mi1320;
  105. else if (_MI2020_)
  106. sd_ctrls = sd_ctrls_mi2020;
  107. else if (_OV2640_)
  108. sd_ctrls = sd_ctrls_ov2640;
  109. else if (_OV9655_)
  110. sd_ctrls = sd_ctrls_ov9655;
  111. else
  112. return 0;
  113. memset(sd_ctrls, 0, GL860_NCTRLS * sizeof(struct ctrl));
  114. SET_MY_CTRL(V4L2_CID_BRIGHTNESS,
  115. V4L2_CTRL_TYPE_INTEGER, "Brightness", brightness)
  116. SET_MY_CTRL(V4L2_CID_SHARPNESS,
  117. V4L2_CTRL_TYPE_INTEGER, "Sharpness", sharpness)
  118. SET_MY_CTRL(V4L2_CID_CONTRAST,
  119. V4L2_CTRL_TYPE_INTEGER, "Contrast", contrast)
  120. SET_MY_CTRL(V4L2_CID_GAMMA,
  121. V4L2_CTRL_TYPE_INTEGER, "Gamma", gamma)
  122. SET_MY_CTRL(V4L2_CID_HUE,
  123. V4L2_CTRL_TYPE_INTEGER, "Palette", hue)
  124. SET_MY_CTRL(V4L2_CID_SATURATION,
  125. V4L2_CTRL_TYPE_INTEGER, "Saturation", saturation)
  126. SET_MY_CTRL(V4L2_CID_WHITE_BALANCE_TEMPERATURE,
  127. V4L2_CTRL_TYPE_INTEGER, "White Bal.", whitebal)
  128. SET_MY_CTRL(V4L2_CID_BACKLIGHT_COMPENSATION,
  129. V4L2_CTRL_TYPE_INTEGER, "Backlight" , backlight)
  130. SET_MY_CTRL(V4L2_CID_HFLIP,
  131. V4L2_CTRL_TYPE_BOOLEAN, "Mirror", mirror)
  132. SET_MY_CTRL(V4L2_CID_VFLIP,
  133. V4L2_CTRL_TYPE_BOOLEAN, "Flip", flip)
  134. SET_MY_CTRL(V4L2_CID_POWER_LINE_FREQUENCY,
  135. V4L2_CTRL_TYPE_BOOLEAN, "AC power 50Hz", AC50Hz)
  136. return nCtrls;
  137. }
  138. /*==================== sud-driver structure initialisation =================*/
  139. static const struct sd_desc sd_desc_mi1320 = {
  140. .name = MODULE_NAME,
  141. .ctrls = sd_ctrls_mi1320,
  142. .nctrls = GL860_NCTRLS,
  143. .config = sd_config,
  144. .init = sd_init,
  145. .isoc_init = sd_isoc_init,
  146. .start = sd_start,
  147. .stop0 = sd_stop0,
  148. .pkt_scan = sd_pkt_scan,
  149. .dq_callback = sd_callback,
  150. };
  151. static const struct sd_desc sd_desc_mi2020 = {
  152. .name = MODULE_NAME,
  153. .ctrls = sd_ctrls_mi2020,
  154. .nctrls = GL860_NCTRLS,
  155. .config = sd_config,
  156. .init = sd_init,
  157. .isoc_init = sd_isoc_init,
  158. .start = sd_start,
  159. .stop0 = sd_stop0,
  160. .pkt_scan = sd_pkt_scan,
  161. .dq_callback = sd_callback,
  162. };
  163. static const struct sd_desc sd_desc_ov2640 = {
  164. .name = MODULE_NAME,
  165. .ctrls = sd_ctrls_ov2640,
  166. .nctrls = GL860_NCTRLS,
  167. .config = sd_config,
  168. .init = sd_init,
  169. .isoc_init = sd_isoc_init,
  170. .start = sd_start,
  171. .stop0 = sd_stop0,
  172. .pkt_scan = sd_pkt_scan,
  173. .dq_callback = sd_callback,
  174. };
  175. static const struct sd_desc sd_desc_ov9655 = {
  176. .name = MODULE_NAME,
  177. .ctrls = sd_ctrls_ov9655,
  178. .nctrls = GL860_NCTRLS,
  179. .config = sd_config,
  180. .init = sd_init,
  181. .isoc_init = sd_isoc_init,
  182. .start = sd_start,
  183. .stop0 = sd_stop0,
  184. .pkt_scan = sd_pkt_scan,
  185. .dq_callback = sd_callback,
  186. };
  187. /*=========================== sub-driver image sizes =======================*/
  188. static struct v4l2_pix_format mi2020_mode[] = {
  189. { 640, 480, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  190. .bytesperline = 640,
  191. .sizeimage = 640 * 480,
  192. .colorspace = V4L2_COLORSPACE_SRGB,
  193. .priv = 0
  194. },
  195. { 800, 598, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  196. .bytesperline = 800,
  197. .sizeimage = 800 * 598,
  198. .colorspace = V4L2_COLORSPACE_SRGB,
  199. .priv = 1
  200. },
  201. {1280, 1024, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  202. .bytesperline = 1280,
  203. .sizeimage = 1280 * 1024,
  204. .colorspace = V4L2_COLORSPACE_SRGB,
  205. .priv = 2
  206. },
  207. {1600, 1198, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  208. .bytesperline = 1600,
  209. .sizeimage = 1600 * 1198,
  210. .colorspace = V4L2_COLORSPACE_SRGB,
  211. .priv = 3
  212. },
  213. };
  214. static struct v4l2_pix_format ov2640_mode[] = {
  215. { 640, 480, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  216. .bytesperline = 640,
  217. .sizeimage = 640 * 480,
  218. .colorspace = V4L2_COLORSPACE_SRGB,
  219. .priv = 0
  220. },
  221. { 800, 600, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  222. .bytesperline = 800,
  223. .sizeimage = 800 * 600,
  224. .colorspace = V4L2_COLORSPACE_SRGB,
  225. .priv = 1
  226. },
  227. {1280, 960, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  228. .bytesperline = 1280,
  229. .sizeimage = 1280 * 960,
  230. .colorspace = V4L2_COLORSPACE_SRGB,
  231. .priv = 2
  232. },
  233. {1600, 1200, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  234. .bytesperline = 1600,
  235. .sizeimage = 1600 * 1200,
  236. .colorspace = V4L2_COLORSPACE_SRGB,
  237. .priv = 3
  238. },
  239. };
  240. static struct v4l2_pix_format mi1320_mode[] = {
  241. { 640, 480, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  242. .bytesperline = 640,
  243. .sizeimage = 640 * 480,
  244. .colorspace = V4L2_COLORSPACE_SRGB,
  245. .priv = 0
  246. },
  247. { 800, 600, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  248. .bytesperline = 800,
  249. .sizeimage = 800 * 600,
  250. .colorspace = V4L2_COLORSPACE_SRGB,
  251. .priv = 1
  252. },
  253. {1280, 960, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  254. .bytesperline = 1280,
  255. .sizeimage = 1280 * 960,
  256. .colorspace = V4L2_COLORSPACE_SRGB,
  257. .priv = 2
  258. },
  259. };
  260. static struct v4l2_pix_format ov9655_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
  266. },
  267. {1280, 960, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  268. .bytesperline = 1280,
  269. .sizeimage = 1280 * 960,
  270. .colorspace = V4L2_COLORSPACE_SRGB,
  271. .priv = 1
  272. },
  273. };
  274. /*========================= sud-driver functions ===========================*/
  275. /* This function is called at probe time */
  276. static int sd_config(struct gspca_dev *gspca_dev,
  277. const struct usb_device_id *id)
  278. {
  279. struct sd *sd = (struct sd *) gspca_dev;
  280. struct cam *cam;
  281. u16 vendor_id, product_id;
  282. /* Get USB VendorID and ProductID */
  283. vendor_id = id->idVendor;
  284. product_id = id->idProduct;
  285. sd->nbRightUp = 1;
  286. sd->nbIm = -1;
  287. sd->sensor = 0xff;
  288. if (strcmp(sensor, "MI1320") == 0)
  289. sd->sensor = ID_MI1320;
  290. else if (strcmp(sensor, "OV2640") == 0)
  291. sd->sensor = ID_OV2640;
  292. else if (strcmp(sensor, "OV9655") == 0)
  293. sd->sensor = ID_OV9655;
  294. else if (strcmp(sensor, "MI2020") == 0)
  295. sd->sensor = ID_MI2020;
  296. /* Get sensor and set the suitable init/start/../stop functions */
  297. if (gl860_guess_sensor(gspca_dev, vendor_id, product_id) == -1)
  298. return -1;
  299. cam = &gspca_dev->cam;
  300. gspca_dev->nbalt = 4;
  301. switch (sd->sensor) {
  302. case ID_MI1320:
  303. gspca_dev->sd_desc = &sd_desc_mi1320;
  304. cam->cam_mode = mi1320_mode;
  305. cam->nmodes = ARRAY_SIZE(mi1320_mode);
  306. dev_init_settings = mi1320_init_settings;
  307. break;
  308. case ID_MI2020:
  309. gspca_dev->sd_desc = &sd_desc_mi2020;
  310. cam->cam_mode = mi2020_mode;
  311. cam->nmodes = ARRAY_SIZE(mi2020_mode);
  312. dev_init_settings = mi2020_init_settings;
  313. break;
  314. case ID_OV2640:
  315. gspca_dev->sd_desc = &sd_desc_ov2640;
  316. cam->cam_mode = ov2640_mode;
  317. cam->nmodes = ARRAY_SIZE(ov2640_mode);
  318. dev_init_settings = ov2640_init_settings;
  319. break;
  320. case ID_OV9655:
  321. gspca_dev->sd_desc = &sd_desc_ov9655;
  322. cam->cam_mode = ov9655_mode;
  323. cam->nmodes = ARRAY_SIZE(ov9655_mode);
  324. dev_init_settings = ov9655_init_settings;
  325. break;
  326. }
  327. dev_init_settings(gspca_dev);
  328. if (AC50Hz != 0xff)
  329. ((struct sd *) gspca_dev)->vcur.AC50Hz = AC50Hz;
  330. gl860_build_control_table(gspca_dev);
  331. return 0;
  332. }
  333. /* This function is called at probe time after sd_config */
  334. static int sd_init(struct gspca_dev *gspca_dev)
  335. {
  336. struct sd *sd = (struct sd *) gspca_dev;
  337. return sd->dev_init_at_startup(gspca_dev);
  338. }
  339. /* This function is called before to choose the alt setting */
  340. static int sd_isoc_init(struct gspca_dev *gspca_dev)
  341. {
  342. struct sd *sd = (struct sd *) gspca_dev;
  343. return sd->dev_configure_alt(gspca_dev);
  344. }
  345. /* This function is called to start the webcam */
  346. static int sd_start(struct gspca_dev *gspca_dev)
  347. {
  348. struct sd *sd = (struct sd *) gspca_dev;
  349. return sd->dev_init_pre_alt(gspca_dev);
  350. }
  351. /* This function is called to stop the webcam */
  352. static void sd_stop0(struct gspca_dev *gspca_dev)
  353. {
  354. struct sd *sd = (struct sd *) gspca_dev;
  355. return sd->dev_post_unset_alt(gspca_dev);
  356. }
  357. /* This function is called when an image is being received */
  358. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  359. u8 *data, int len)
  360. {
  361. struct sd *sd = (struct sd *) gspca_dev;
  362. static s32 nSkipped;
  363. s32 mode = (s32) gspca_dev->curr_mode;
  364. s32 nToSkip =
  365. sd->swapRB * (gspca_dev->cam.cam_mode[mode].bytesperline + 1);
  366. /* Test only against 0202h, so endianess does not matter */
  367. switch (*(s16 *) data) {
  368. case 0x0202: /* End of frame, start a new one */
  369. gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
  370. nSkipped = 0;
  371. if (sd->nbIm >= 0 && sd->nbIm < 10)
  372. sd->nbIm++;
  373. gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
  374. break;
  375. default:
  376. data += 2;
  377. len -= 2;
  378. if (nSkipped + len <= nToSkip)
  379. nSkipped += len;
  380. else {
  381. if (nSkipped < nToSkip && nSkipped + len > nToSkip) {
  382. data += nToSkip - nSkipped;
  383. len -= nToSkip - nSkipped;
  384. nSkipped = nToSkip + 1;
  385. }
  386. gspca_frame_add(gspca_dev,
  387. INTER_PACKET, data, len);
  388. }
  389. break;
  390. }
  391. }
  392. /* This function is called when an image has been read */
  393. /* This function is used to monitor webcam orientation */
  394. static void sd_callback(struct gspca_dev *gspca_dev)
  395. {
  396. struct sd *sd = (struct sd *) gspca_dev;
  397. if (!_OV9655_) {
  398. u8 state;
  399. u8 upsideDown;
  400. /* Probe sensor orientation */
  401. ctrl_in(gspca_dev, 0xc0, 2, 0x0000, 0x0000, 1, (void *)&state);
  402. /* C8/40 means upside-down (looking backwards) */
  403. /* D8/50 means right-up (looking onwards) */
  404. upsideDown = (state == 0xc8 || state == 0x40);
  405. if (upsideDown && sd->nbRightUp > -4) {
  406. if (sd->nbRightUp > 0)
  407. sd->nbRightUp = 0;
  408. if (sd->nbRightUp == -3) {
  409. sd->mirrorMask = 1;
  410. sd->waitSet = 1;
  411. }
  412. sd->nbRightUp--;
  413. }
  414. if (!upsideDown && sd->nbRightUp < 4) {
  415. if (sd->nbRightUp < 0)
  416. sd->nbRightUp = 0;
  417. if (sd->nbRightUp == 3) {
  418. sd->mirrorMask = 0;
  419. sd->waitSet = 1;
  420. }
  421. sd->nbRightUp++;
  422. }
  423. }
  424. if (sd->waitSet)
  425. sd->dev_camera_settings(gspca_dev);
  426. }
  427. /*=================== USB driver structure initialisation ==================*/
  428. static const struct usb_device_id device_table[] = {
  429. {USB_DEVICE(0x05e3, 0x0503)},
  430. {USB_DEVICE(0x05e3, 0xf191)},
  431. {}
  432. };
  433. MODULE_DEVICE_TABLE(usb, device_table);
  434. static int sd_probe(struct usb_interface *intf,
  435. const struct usb_device_id *id)
  436. {
  437. return gspca_dev_probe(intf, id,
  438. &sd_desc_mi1320, sizeof(struct sd), THIS_MODULE);
  439. }
  440. static void sd_disconnect(struct usb_interface *intf)
  441. {
  442. gspca_disconnect(intf);
  443. }
  444. static struct usb_driver sd_driver = {
  445. .name = MODULE_NAME,
  446. .id_table = device_table,
  447. .probe = sd_probe,
  448. .disconnect = sd_disconnect,
  449. #ifdef CONFIG_PM
  450. .suspend = gspca_suspend,
  451. .resume = gspca_resume,
  452. #endif
  453. };
  454. /*====================== Init and Exit module functions ====================*/
  455. static int __init sd_mod_init(void)
  456. {
  457. PDEBUG(D_PROBE, "driver startup - version %s", DRIVER_VERSION);
  458. if (usb_register(&sd_driver) < 0)
  459. return -1;
  460. return 0;
  461. }
  462. static void __exit sd_mod_exit(void)
  463. {
  464. usb_deregister(&sd_driver);
  465. }
  466. module_init(sd_mod_init);
  467. module_exit(sd_mod_exit);
  468. /*==========================================================================*/
  469. int gl860_RTx(struct gspca_dev *gspca_dev,
  470. unsigned char pref, u32 req, u16 val, u16 index,
  471. s32 len, void *pdata)
  472. {
  473. struct usb_device *udev = gspca_dev->dev;
  474. s32 r = 0;
  475. if (pref == 0x40) { /* Send */
  476. if (len > 0) {
  477. memcpy(gspca_dev->usb_buf, pdata, len);
  478. r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  479. req, pref, val, index,
  480. gspca_dev->usb_buf,
  481. len, 400 + 200 * (len > 1));
  482. } else {
  483. r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  484. req, pref, val, index, NULL, len, 400);
  485. }
  486. } else { /* Receive */
  487. if (len > 0) {
  488. r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  489. req, pref, val, index,
  490. gspca_dev->usb_buf,
  491. len, 400 + 200 * (len > 1));
  492. memcpy(pdata, gspca_dev->usb_buf, len);
  493. } else {
  494. r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  495. req, pref, val, index, NULL, len, 400);
  496. }
  497. }
  498. if (r < 0)
  499. err("ctrl transfer failed %4d "
  500. "[p%02x r%d v%04x i%04x len%d]",
  501. r, pref, req, val, index, len);
  502. else if (len > 1 && r < len)
  503. PDEBUG(D_ERR, "short ctrl transfer %d/%d", r, len);
  504. msleep(1);
  505. return r;
  506. }
  507. int fetch_validx(struct gspca_dev *gspca_dev, struct validx *tbl, int len)
  508. {
  509. int n;
  510. for (n = 0; n < len; n++) {
  511. if (tbl[n].idx != 0xffff)
  512. ctrl_out(gspca_dev, 0x40, 1, tbl[n].val,
  513. tbl[n].idx, 0, NULL);
  514. else if (tbl[n].val == 0xffff)
  515. break;
  516. else
  517. msleep(tbl[n].val);
  518. }
  519. return n;
  520. }
  521. int keep_on_fetching_validx(struct gspca_dev *gspca_dev, struct validx *tbl,
  522. int len, int n)
  523. {
  524. while (++n < len) {
  525. if (tbl[n].idx != 0xffff)
  526. ctrl_out(gspca_dev, 0x40, 1, tbl[n].val, tbl[n].idx,
  527. 0, NULL);
  528. else if (tbl[n].val == 0xffff)
  529. break;
  530. else
  531. msleep(tbl[n].val);
  532. }
  533. return n;
  534. }
  535. void fetch_idxdata(struct gspca_dev *gspca_dev, struct idxdata *tbl, int len)
  536. {
  537. int n;
  538. for (n = 0; n < len; n++) {
  539. if (memcmp(tbl[n].data, "\xff\xff\xff", 3) != 0)
  540. ctrl_out(gspca_dev, 0x40, 3, 0x7a00, tbl[n].idx,
  541. 3, tbl[n].data);
  542. else
  543. msleep(tbl[n].idx);
  544. }
  545. }
  546. static int gl860_guess_sensor(struct gspca_dev *gspca_dev,
  547. u16 vendor_id, u16 product_id)
  548. {
  549. struct sd *sd = (struct sd *) gspca_dev;
  550. u8 probe, nb26, nb96, nOV, ntry;
  551. if (product_id == 0xf191)
  552. sd->sensor = ID_MI1320;
  553. if (sd->sensor == 0xff) {
  554. ctrl_in(gspca_dev, 0xc0, 2, 0x0000, 0x0004, 1, &probe);
  555. ctrl_in(gspca_dev, 0xc0, 2, 0x0000, 0x0004, 1, &probe);
  556. ctrl_out(gspca_dev, 0x40, 1, 0x0000, 0x0000, 0, NULL);
  557. msleep(3);
  558. ctrl_out(gspca_dev, 0x40, 1, 0x0010, 0x0010, 0, NULL);
  559. msleep(3);
  560. ctrl_out(gspca_dev, 0x40, 1, 0x0008, 0x00c0, 0, NULL);
  561. msleep(3);
  562. ctrl_out(gspca_dev, 0x40, 1, 0x0001, 0x00c1, 0, NULL);
  563. msleep(3);
  564. ctrl_out(gspca_dev, 0x40, 1, 0x0001, 0x00c2, 0, NULL);
  565. msleep(3);
  566. ctrl_out(gspca_dev, 0x40, 1, 0x0020, 0x0006, 0, NULL);
  567. msleep(3);
  568. ctrl_out(gspca_dev, 0x40, 1, 0x006a, 0x000d, 0, NULL);
  569. msleep(56);
  570. PDEBUG(D_PROBE, "probing for sensor MI2020 or OVXXXX");
  571. nOV = 0;
  572. for (ntry = 0; ntry < 4; ntry++) {
  573. ctrl_out(gspca_dev, 0x40, 1, 0x0040, 0x0000, 0, NULL);
  574. msleep(3);
  575. ctrl_out(gspca_dev, 0x40, 1, 0x0063, 0x0006, 0, NULL);
  576. msleep(3);
  577. ctrl_out(gspca_dev, 0x40, 1, 0x7a00, 0x8030, 0, NULL);
  578. msleep(10);
  579. ctrl_in(gspca_dev, 0xc0, 2, 0x7a00, 0x8030, 1, &probe);
  580. PDEBUG(D_PROBE, "probe=0x%02x", probe);
  581. if (probe == 0xff)
  582. nOV++;
  583. }
  584. if (nOV) {
  585. PDEBUG(D_PROBE, "0xff -> OVXXXX");
  586. PDEBUG(D_PROBE, "probing for sensor OV2640 or OV9655");
  587. nb26 = nb96 = 0;
  588. for (ntry = 0; ntry < 4; ntry++) {
  589. ctrl_out(gspca_dev, 0x40, 1, 0x0040, 0x0000,
  590. 0, NULL);
  591. msleep(3);
  592. ctrl_out(gspca_dev, 0x40, 1, 0x6000, 0x800a,
  593. 0, NULL);
  594. msleep(10);
  595. /* Wait for 26(OV2640) or 96(OV9655) */
  596. ctrl_in(gspca_dev, 0xc0, 2, 0x6000, 0x800a,
  597. 1, &probe);
  598. if (probe == 0x26 || probe == 0x40) {
  599. PDEBUG(D_PROBE,
  600. "probe=0x%02x -> OV2640",
  601. probe);
  602. sd->sensor = ID_OV2640;
  603. nb26 += 4;
  604. break;
  605. }
  606. if (probe == 0x96 || probe == 0x55) {
  607. PDEBUG(D_PROBE,
  608. "probe=0x%02x -> OV9655",
  609. probe);
  610. sd->sensor = ID_OV9655;
  611. nb96 += 4;
  612. break;
  613. }
  614. PDEBUG(D_PROBE, "probe=0x%02x", probe);
  615. if (probe == 0x00)
  616. nb26++;
  617. if (probe == 0xff)
  618. nb96++;
  619. msleep(3);
  620. }
  621. if (nb26 < 4 && nb96 < 4)
  622. return -1;
  623. } else {
  624. PDEBUG(D_PROBE, "Not any 0xff -> MI2020");
  625. sd->sensor = ID_MI2020;
  626. }
  627. }
  628. if (_MI1320_) {
  629. PDEBUG(D_PROBE, "05e3:f191 sensor MI1320 (1.3M)");
  630. } else if (_MI2020_) {
  631. PDEBUG(D_PROBE, "05e3:0503 sensor MI2020 (2.0M)");
  632. } else if (_OV9655_) {
  633. PDEBUG(D_PROBE, "05e3:0503 sensor OV9655 (1.3M)");
  634. } else if (_OV2640_) {
  635. PDEBUG(D_PROBE, "05e3:0503 sensor OV2640 (2.0M)");
  636. } else {
  637. PDEBUG(D_PROBE, "***** Unknown sensor *****");
  638. return -1;
  639. }
  640. return 0;
  641. }