gl860.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include "gspca.h"
  23. #include "gl860.h"
  24. MODULE_AUTHOR("Olivier Lorin <o.lorin@laposte.net>");
  25. MODULE_DESCRIPTION("Genesys Logic USB PC Camera Driver");
  26. MODULE_LICENSE("GPL");
  27. /*======================== static function declarations ====================*/
  28. static void (*dev_init_settings)(struct gspca_dev *gspca_dev);
  29. static int sd_config(struct gspca_dev *gspca_dev,
  30. const struct usb_device_id *id);
  31. static int sd_init(struct gspca_dev *gspca_dev);
  32. static int sd_isoc_init(struct gspca_dev *gspca_dev);
  33. static int sd_start(struct gspca_dev *gspca_dev);
  34. static void sd_stop0(struct gspca_dev *gspca_dev);
  35. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  36. u8 *data, int len);
  37. static void sd_callback(struct gspca_dev *gspca_dev);
  38. static int gl860_guess_sensor(struct gspca_dev *gspca_dev,
  39. u16 vendor_id, u16 product_id);
  40. /*============================ driver options ==============================*/
  41. static s32 AC50Hz = 0xff;
  42. module_param(AC50Hz, int, 0644);
  43. MODULE_PARM_DESC(AC50Hz, " Does AC power frequency is 50Hz? (0/1)");
  44. static char sensor[7];
  45. module_param_string(sensor, sensor, sizeof(sensor), 0644);
  46. MODULE_PARM_DESC(sensor,
  47. " Driver sensor ('MI1320'/'MI2020'/'OV9655'/'OV2640')");
  48. /*============================ webcam controls =============================*/
  49. /* Functions to get and set a control value */
  50. #define SD_SETGET(thename) \
  51. static int sd_set_##thename(struct gspca_dev *gspca_dev, s32 val)\
  52. {\
  53. struct sd *sd = (struct sd *) gspca_dev;\
  54. \
  55. sd->vcur.thename = val;\
  56. if (gspca_dev->streaming)\
  57. sd->waitSet = 1;\
  58. return 0;\
  59. } \
  60. static int sd_get_##thename(struct gspca_dev *gspca_dev, s32 *val)\
  61. {\
  62. struct sd *sd = (struct sd *) gspca_dev;\
  63. \
  64. *val = sd->vcur.thename;\
  65. return 0;\
  66. }
  67. SD_SETGET(mirror)
  68. SD_SETGET(flip)
  69. SD_SETGET(AC50Hz)
  70. SD_SETGET(backlight)
  71. SD_SETGET(brightness)
  72. SD_SETGET(gamma)
  73. SD_SETGET(hue)
  74. SD_SETGET(saturation)
  75. SD_SETGET(sharpness)
  76. SD_SETGET(whitebal)
  77. SD_SETGET(contrast)
  78. #define GL860_NCTRLS 11
  79. /* control table */
  80. static struct ctrl sd_ctrls_mi1320[GL860_NCTRLS];
  81. static struct ctrl sd_ctrls_mi2020[GL860_NCTRLS];
  82. static struct ctrl sd_ctrls_ov2640[GL860_NCTRLS];
  83. static struct ctrl sd_ctrls_ov9655[GL860_NCTRLS];
  84. #define SET_MY_CTRL(theid, \
  85. thetype, thelabel, thename) \
  86. if (sd->vmax.thename != 0) {\
  87. sd_ctrls[nCtrls].qctrl.id = theid;\
  88. sd_ctrls[nCtrls].qctrl.type = thetype;\
  89. strcpy(sd_ctrls[nCtrls].qctrl.name, thelabel);\
  90. sd_ctrls[nCtrls].qctrl.minimum = 0;\
  91. sd_ctrls[nCtrls].qctrl.maximum = sd->vmax.thename;\
  92. sd_ctrls[nCtrls].qctrl.default_value = sd->vcur.thename;\
  93. sd_ctrls[nCtrls].qctrl.step = \
  94. (sd->vmax.thename < 16) ? 1 : sd->vmax.thename/16;\
  95. sd_ctrls[nCtrls].set = sd_set_##thename;\
  96. sd_ctrls[nCtrls].get = sd_get_##thename;\
  97. nCtrls++;\
  98. }
  99. static int gl860_build_control_table(struct gspca_dev *gspca_dev)
  100. {
  101. struct sd *sd = (struct sd *) gspca_dev;
  102. struct ctrl *sd_ctrls;
  103. int nCtrls = 0;
  104. if (_MI1320_)
  105. sd_ctrls = sd_ctrls_mi1320;
  106. else if (_MI2020_)
  107. sd_ctrls = sd_ctrls_mi2020;
  108. else if (_OV2640_)
  109. sd_ctrls = sd_ctrls_ov2640;
  110. else if (_OV9655_)
  111. sd_ctrls = sd_ctrls_ov9655;
  112. else
  113. return 0;
  114. memset(sd_ctrls, 0, GL860_NCTRLS * sizeof(struct ctrl));
  115. SET_MY_CTRL(V4L2_CID_BRIGHTNESS,
  116. V4L2_CTRL_TYPE_INTEGER, "Brightness", brightness)
  117. SET_MY_CTRL(V4L2_CID_SHARPNESS,
  118. V4L2_CTRL_TYPE_INTEGER, "Sharpness", sharpness)
  119. SET_MY_CTRL(V4L2_CID_CONTRAST,
  120. V4L2_CTRL_TYPE_INTEGER, "Contrast", contrast)
  121. SET_MY_CTRL(V4L2_CID_GAMMA,
  122. V4L2_CTRL_TYPE_INTEGER, "Gamma", gamma)
  123. SET_MY_CTRL(V4L2_CID_HUE,
  124. V4L2_CTRL_TYPE_INTEGER, "Palette", hue)
  125. SET_MY_CTRL(V4L2_CID_SATURATION,
  126. V4L2_CTRL_TYPE_INTEGER, "Saturation", saturation)
  127. SET_MY_CTRL(V4L2_CID_WHITE_BALANCE_TEMPERATURE,
  128. V4L2_CTRL_TYPE_INTEGER, "White Bal.", whitebal)
  129. SET_MY_CTRL(V4L2_CID_BACKLIGHT_COMPENSATION,
  130. V4L2_CTRL_TYPE_INTEGER, "Backlight" , backlight)
  131. SET_MY_CTRL(V4L2_CID_HFLIP,
  132. V4L2_CTRL_TYPE_BOOLEAN, "Mirror", mirror)
  133. SET_MY_CTRL(V4L2_CID_VFLIP,
  134. V4L2_CTRL_TYPE_BOOLEAN, "Flip", flip)
  135. SET_MY_CTRL(V4L2_CID_POWER_LINE_FREQUENCY,
  136. V4L2_CTRL_TYPE_BOOLEAN, "AC power 50Hz", AC50Hz)
  137. return nCtrls;
  138. }
  139. /*==================== sud-driver structure initialisation =================*/
  140. static const struct sd_desc sd_desc_mi1320 = {
  141. .name = MODULE_NAME,
  142. .ctrls = sd_ctrls_mi1320,
  143. .nctrls = GL860_NCTRLS,
  144. .config = sd_config,
  145. .init = sd_init,
  146. .isoc_init = sd_isoc_init,
  147. .start = sd_start,
  148. .stop0 = sd_stop0,
  149. .pkt_scan = sd_pkt_scan,
  150. .dq_callback = sd_callback,
  151. };
  152. static const struct sd_desc sd_desc_mi2020 = {
  153. .name = MODULE_NAME,
  154. .ctrls = sd_ctrls_mi2020,
  155. .nctrls = GL860_NCTRLS,
  156. .config = sd_config,
  157. .init = sd_init,
  158. .isoc_init = sd_isoc_init,
  159. .start = sd_start,
  160. .stop0 = sd_stop0,
  161. .pkt_scan = sd_pkt_scan,
  162. .dq_callback = sd_callback,
  163. };
  164. static const struct sd_desc sd_desc_ov2640 = {
  165. .name = MODULE_NAME,
  166. .ctrls = sd_ctrls_ov2640,
  167. .nctrls = GL860_NCTRLS,
  168. .config = sd_config,
  169. .init = sd_init,
  170. .isoc_init = sd_isoc_init,
  171. .start = sd_start,
  172. .stop0 = sd_stop0,
  173. .pkt_scan = sd_pkt_scan,
  174. .dq_callback = sd_callback,
  175. };
  176. static const struct sd_desc sd_desc_ov9655 = {
  177. .name = MODULE_NAME,
  178. .ctrls = sd_ctrls_ov9655,
  179. .nctrls = GL860_NCTRLS,
  180. .config = sd_config,
  181. .init = sd_init,
  182. .isoc_init = sd_isoc_init,
  183. .start = sd_start,
  184. .stop0 = sd_stop0,
  185. .pkt_scan = sd_pkt_scan,
  186. .dq_callback = sd_callback,
  187. };
  188. /*=========================== sub-driver image sizes =======================*/
  189. static struct v4l2_pix_format mi2020_mode[] = {
  190. { 640, 480, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  191. .bytesperline = 640,
  192. .sizeimage = 640 * 480,
  193. .colorspace = V4L2_COLORSPACE_SRGB,
  194. .priv = 0
  195. },
  196. { 800, 598, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  197. .bytesperline = 800,
  198. .sizeimage = 800 * 598,
  199. .colorspace = V4L2_COLORSPACE_SRGB,
  200. .priv = 1
  201. },
  202. {1280, 1024, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  203. .bytesperline = 1280,
  204. .sizeimage = 1280 * 1024,
  205. .colorspace = V4L2_COLORSPACE_SRGB,
  206. .priv = 2
  207. },
  208. {1600, 1198, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  209. .bytesperline = 1600,
  210. .sizeimage = 1600 * 1198,
  211. .colorspace = V4L2_COLORSPACE_SRGB,
  212. .priv = 3
  213. },
  214. };
  215. static struct v4l2_pix_format ov2640_mode[] = {
  216. { 640, 480, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  217. .bytesperline = 640,
  218. .sizeimage = 640 * 480,
  219. .colorspace = V4L2_COLORSPACE_SRGB,
  220. .priv = 0
  221. },
  222. { 800, 600, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  223. .bytesperline = 800,
  224. .sizeimage = 800 * 600,
  225. .colorspace = V4L2_COLORSPACE_SRGB,
  226. .priv = 1
  227. },
  228. {1280, 960, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  229. .bytesperline = 1280,
  230. .sizeimage = 1280 * 960,
  231. .colorspace = V4L2_COLORSPACE_SRGB,
  232. .priv = 2
  233. },
  234. {1600, 1200, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  235. .bytesperline = 1600,
  236. .sizeimage = 1600 * 1200,
  237. .colorspace = V4L2_COLORSPACE_SRGB,
  238. .priv = 3
  239. },
  240. };
  241. static struct v4l2_pix_format mi1320_mode[] = {
  242. { 640, 480, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  243. .bytesperline = 640,
  244. .sizeimage = 640 * 480,
  245. .colorspace = V4L2_COLORSPACE_SRGB,
  246. .priv = 0
  247. },
  248. { 800, 600, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  249. .bytesperline = 800,
  250. .sizeimage = 800 * 600,
  251. .colorspace = V4L2_COLORSPACE_SRGB,
  252. .priv = 1
  253. },
  254. {1280, 960, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  255. .bytesperline = 1280,
  256. .sizeimage = 1280 * 960,
  257. .colorspace = V4L2_COLORSPACE_SRGB,
  258. .priv = 2
  259. },
  260. };
  261. static struct v4l2_pix_format ov9655_mode[] = {
  262. { 640, 480, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  263. .bytesperline = 640,
  264. .sizeimage = 640 * 480,
  265. .colorspace = V4L2_COLORSPACE_SRGB,
  266. .priv = 0
  267. },
  268. {1280, 960, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
  269. .bytesperline = 1280,
  270. .sizeimage = 1280 * 960,
  271. .colorspace = V4L2_COLORSPACE_SRGB,
  272. .priv = 1
  273. },
  274. };
  275. /*========================= sud-driver functions ===========================*/
  276. /* This function is called at probe time */
  277. static int sd_config(struct gspca_dev *gspca_dev,
  278. const struct usb_device_id *id)
  279. {
  280. struct sd *sd = (struct sd *) gspca_dev;
  281. struct cam *cam;
  282. u16 vendor_id, product_id;
  283. /* Get USB VendorID and ProductID */
  284. vendor_id = id->idVendor;
  285. product_id = id->idProduct;
  286. sd->nbRightUp = 1;
  287. sd->nbIm = -1;
  288. sd->sensor = 0xff;
  289. if (strcmp(sensor, "MI1320") == 0)
  290. sd->sensor = ID_MI1320;
  291. else if (strcmp(sensor, "OV2640") == 0)
  292. sd->sensor = ID_OV2640;
  293. else if (strcmp(sensor, "OV9655") == 0)
  294. sd->sensor = ID_OV9655;
  295. else if (strcmp(sensor, "MI2020") == 0)
  296. sd->sensor = ID_MI2020;
  297. /* Get sensor and set the suitable init/start/../stop functions */
  298. if (gl860_guess_sensor(gspca_dev, vendor_id, product_id) == -1)
  299. return -1;
  300. cam = &gspca_dev->cam;
  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. module_usb_driver(sd_driver);
  456. /*==========================================================================*/
  457. int gl860_RTx(struct gspca_dev *gspca_dev,
  458. unsigned char pref, u32 req, u16 val, u16 index,
  459. s32 len, void *pdata)
  460. {
  461. struct usb_device *udev = gspca_dev->dev;
  462. s32 r = 0;
  463. if (pref == 0x40) { /* Send */
  464. if (len > 0) {
  465. memcpy(gspca_dev->usb_buf, pdata, len);
  466. r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  467. req, pref, val, index,
  468. gspca_dev->usb_buf,
  469. len, 400 + 200 * (len > 1));
  470. } else {
  471. r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  472. req, pref, val, index, NULL, len, 400);
  473. }
  474. } else { /* Receive */
  475. if (len > 0) {
  476. r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  477. req, pref, val, index,
  478. gspca_dev->usb_buf,
  479. len, 400 + 200 * (len > 1));
  480. memcpy(pdata, gspca_dev->usb_buf, len);
  481. } else {
  482. r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  483. req, pref, val, index, NULL, len, 400);
  484. }
  485. }
  486. if (r < 0)
  487. pr_err("ctrl transfer failed %4d [p%02x r%d v%04x i%04x len%d]\n",
  488. r, pref, req, val, index, len);
  489. else if (len > 1 && r < len)
  490. PDEBUG(D_ERR, "short ctrl transfer %d/%d", r, len);
  491. msleep(1);
  492. return r;
  493. }
  494. int fetch_validx(struct gspca_dev *gspca_dev, struct validx *tbl, int len)
  495. {
  496. int n;
  497. for (n = 0; n < len; n++) {
  498. if (tbl[n].idx != 0xffff)
  499. ctrl_out(gspca_dev, 0x40, 1, tbl[n].val,
  500. tbl[n].idx, 0, NULL);
  501. else if (tbl[n].val == 0xffff)
  502. break;
  503. else
  504. msleep(tbl[n].val);
  505. }
  506. return n;
  507. }
  508. int keep_on_fetching_validx(struct gspca_dev *gspca_dev, struct validx *tbl,
  509. int len, int n)
  510. {
  511. while (++n < len) {
  512. if (tbl[n].idx != 0xffff)
  513. ctrl_out(gspca_dev, 0x40, 1, tbl[n].val, tbl[n].idx,
  514. 0, NULL);
  515. else if (tbl[n].val == 0xffff)
  516. break;
  517. else
  518. msleep(tbl[n].val);
  519. }
  520. return n;
  521. }
  522. void fetch_idxdata(struct gspca_dev *gspca_dev, struct idxdata *tbl, int len)
  523. {
  524. int n;
  525. for (n = 0; n < len; n++) {
  526. if (memcmp(tbl[n].data, "\xff\xff\xff", 3) != 0)
  527. ctrl_out(gspca_dev, 0x40, 3, 0x7a00, tbl[n].idx,
  528. 3, tbl[n].data);
  529. else
  530. msleep(tbl[n].idx);
  531. }
  532. }
  533. static int gl860_guess_sensor(struct gspca_dev *gspca_dev,
  534. u16 vendor_id, u16 product_id)
  535. {
  536. struct sd *sd = (struct sd *) gspca_dev;
  537. u8 probe, nb26, nb96, nOV, ntry;
  538. if (product_id == 0xf191)
  539. sd->sensor = ID_MI1320;
  540. if (sd->sensor == 0xff) {
  541. ctrl_in(gspca_dev, 0xc0, 2, 0x0000, 0x0004, 1, &probe);
  542. ctrl_in(gspca_dev, 0xc0, 2, 0x0000, 0x0004, 1, &probe);
  543. ctrl_out(gspca_dev, 0x40, 1, 0x0000, 0x0000, 0, NULL);
  544. msleep(3);
  545. ctrl_out(gspca_dev, 0x40, 1, 0x0010, 0x0010, 0, NULL);
  546. msleep(3);
  547. ctrl_out(gspca_dev, 0x40, 1, 0x0008, 0x00c0, 0, NULL);
  548. msleep(3);
  549. ctrl_out(gspca_dev, 0x40, 1, 0x0001, 0x00c1, 0, NULL);
  550. msleep(3);
  551. ctrl_out(gspca_dev, 0x40, 1, 0x0001, 0x00c2, 0, NULL);
  552. msleep(3);
  553. ctrl_out(gspca_dev, 0x40, 1, 0x0020, 0x0006, 0, NULL);
  554. msleep(3);
  555. ctrl_out(gspca_dev, 0x40, 1, 0x006a, 0x000d, 0, NULL);
  556. msleep(56);
  557. PDEBUG(D_PROBE, "probing for sensor MI2020 or OVXXXX");
  558. nOV = 0;
  559. for (ntry = 0; ntry < 4; ntry++) {
  560. ctrl_out(gspca_dev, 0x40, 1, 0x0040, 0x0000, 0, NULL);
  561. msleep(3);
  562. ctrl_out(gspca_dev, 0x40, 1, 0x0063, 0x0006, 0, NULL);
  563. msleep(3);
  564. ctrl_out(gspca_dev, 0x40, 1, 0x7a00, 0x8030, 0, NULL);
  565. msleep(10);
  566. ctrl_in(gspca_dev, 0xc0, 2, 0x7a00, 0x8030, 1, &probe);
  567. PDEBUG(D_PROBE, "probe=0x%02x", probe);
  568. if (probe == 0xff)
  569. nOV++;
  570. }
  571. if (nOV) {
  572. PDEBUG(D_PROBE, "0xff -> OVXXXX");
  573. PDEBUG(D_PROBE, "probing for sensor OV2640 or OV9655");
  574. nb26 = nb96 = 0;
  575. for (ntry = 0; ntry < 4; ntry++) {
  576. ctrl_out(gspca_dev, 0x40, 1, 0x0040, 0x0000,
  577. 0, NULL);
  578. msleep(3);
  579. ctrl_out(gspca_dev, 0x40, 1, 0x6000, 0x800a,
  580. 0, NULL);
  581. msleep(10);
  582. /* Wait for 26(OV2640) or 96(OV9655) */
  583. ctrl_in(gspca_dev, 0xc0, 2, 0x6000, 0x800a,
  584. 1, &probe);
  585. if (probe == 0x26 || probe == 0x40) {
  586. PDEBUG(D_PROBE,
  587. "probe=0x%02x -> OV2640",
  588. probe);
  589. sd->sensor = ID_OV2640;
  590. nb26 += 4;
  591. break;
  592. }
  593. if (probe == 0x96 || probe == 0x55) {
  594. PDEBUG(D_PROBE,
  595. "probe=0x%02x -> OV9655",
  596. probe);
  597. sd->sensor = ID_OV9655;
  598. nb96 += 4;
  599. break;
  600. }
  601. PDEBUG(D_PROBE, "probe=0x%02x", probe);
  602. if (probe == 0x00)
  603. nb26++;
  604. if (probe == 0xff)
  605. nb96++;
  606. msleep(3);
  607. }
  608. if (nb26 < 4 && nb96 < 4)
  609. return -1;
  610. } else {
  611. PDEBUG(D_PROBE, "Not any 0xff -> MI2020");
  612. sd->sensor = ID_MI2020;
  613. }
  614. }
  615. if (_MI1320_) {
  616. PDEBUG(D_PROBE, "05e3:f191 sensor MI1320 (1.3M)");
  617. } else if (_MI2020_) {
  618. PDEBUG(D_PROBE, "05e3:0503 sensor MI2020 (2.0M)");
  619. } else if (_OV9655_) {
  620. PDEBUG(D_PROBE, "05e3:0503 sensor OV9655 (1.3M)");
  621. } else if (_OV2640_) {
  622. PDEBUG(D_PROBE, "05e3:0503 sensor OV2640 (2.0M)");
  623. } else {
  624. PDEBUG(D_PROBE, "***** Unknown sensor *****");
  625. return -1;
  626. }
  627. return 0;
  628. }