gl860.c 18 KB

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