gl860.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. if (!sd->gspca_dev.present)
  356. return;
  357. return sd->dev_post_unset_alt(gspca_dev);
  358. }
  359. /* This function is called when an image is being received */
  360. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  361. u8 *data, int len)
  362. {
  363. struct sd *sd = (struct sd *) gspca_dev;
  364. static s32 nSkipped;
  365. s32 mode = (s32) gspca_dev->curr_mode;
  366. s32 nToSkip =
  367. sd->swapRB * (gspca_dev->cam.cam_mode[mode].bytesperline + 1);
  368. /* Test only against 0202h, so endianess does not matter */
  369. switch (*(s16 *) data) {
  370. case 0x0202: /* End of frame, start a new one */
  371. gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
  372. nSkipped = 0;
  373. if (sd->nbIm >= 0 && sd->nbIm < 10)
  374. sd->nbIm++;
  375. gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
  376. break;
  377. default:
  378. data += 2;
  379. len -= 2;
  380. if (nSkipped + len <= nToSkip)
  381. nSkipped += len;
  382. else {
  383. if (nSkipped < nToSkip && nSkipped + len > nToSkip) {
  384. data += nToSkip - nSkipped;
  385. len -= nToSkip - nSkipped;
  386. nSkipped = nToSkip + 1;
  387. }
  388. gspca_frame_add(gspca_dev,
  389. INTER_PACKET, data, len);
  390. }
  391. break;
  392. }
  393. }
  394. /* This function is called when an image has been read */
  395. /* This function is used to monitor webcam orientation */
  396. static void sd_callback(struct gspca_dev *gspca_dev)
  397. {
  398. struct sd *sd = (struct sd *) gspca_dev;
  399. if (!_OV9655_) {
  400. u8 state;
  401. u8 upsideDown;
  402. /* Probe sensor orientation */
  403. ctrl_in(gspca_dev, 0xc0, 2, 0x0000, 0x0000, 1, (void *)&state);
  404. /* C8/40 means upside-down (looking backwards) */
  405. /* D8/50 means right-up (looking onwards) */
  406. upsideDown = (state == 0xc8 || state == 0x40);
  407. if (upsideDown && sd->nbRightUp > -4) {
  408. if (sd->nbRightUp > 0)
  409. sd->nbRightUp = 0;
  410. if (sd->nbRightUp == -3) {
  411. sd->mirrorMask = 1;
  412. sd->waitSet = 1;
  413. }
  414. sd->nbRightUp--;
  415. }
  416. if (!upsideDown && sd->nbRightUp < 4) {
  417. if (sd->nbRightUp < 0)
  418. sd->nbRightUp = 0;
  419. if (sd->nbRightUp == 3) {
  420. sd->mirrorMask = 0;
  421. sd->waitSet = 1;
  422. }
  423. sd->nbRightUp++;
  424. }
  425. }
  426. if (sd->waitSet)
  427. sd->dev_camera_settings(gspca_dev);
  428. }
  429. /*=================== USB driver structure initialisation ==================*/
  430. static const struct usb_device_id device_table[] = {
  431. {USB_DEVICE(0x05e3, 0x0503)},
  432. {USB_DEVICE(0x05e3, 0xf191)},
  433. {}
  434. };
  435. MODULE_DEVICE_TABLE(usb, device_table);
  436. static int sd_probe(struct usb_interface *intf,
  437. const struct usb_device_id *id)
  438. {
  439. return gspca_dev_probe(intf, id,
  440. &sd_desc_mi1320, sizeof(struct sd), THIS_MODULE);
  441. }
  442. static void sd_disconnect(struct usb_interface *intf)
  443. {
  444. gspca_disconnect(intf);
  445. }
  446. static struct usb_driver sd_driver = {
  447. .name = MODULE_NAME,
  448. .id_table = device_table,
  449. .probe = sd_probe,
  450. .disconnect = sd_disconnect,
  451. #ifdef CONFIG_PM
  452. .suspend = gspca_suspend,
  453. .resume = gspca_resume,
  454. .reset_resume = gspca_resume,
  455. #endif
  456. };
  457. /*====================== Init and Exit module functions ====================*/
  458. module_usb_driver(sd_driver);
  459. /*==========================================================================*/
  460. int gl860_RTx(struct gspca_dev *gspca_dev,
  461. unsigned char pref, u32 req, u16 val, u16 index,
  462. s32 len, void *pdata)
  463. {
  464. struct usb_device *udev = gspca_dev->dev;
  465. s32 r = 0;
  466. if (pref == 0x40) { /* Send */
  467. if (len > 0) {
  468. memcpy(gspca_dev->usb_buf, pdata, len);
  469. r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  470. req, pref, val, index,
  471. gspca_dev->usb_buf,
  472. len, 400 + 200 * (len > 1));
  473. } else {
  474. r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  475. req, pref, val, index, NULL, len, 400);
  476. }
  477. } else { /* Receive */
  478. if (len > 0) {
  479. r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  480. req, pref, val, index,
  481. gspca_dev->usb_buf,
  482. len, 400 + 200 * (len > 1));
  483. memcpy(pdata, gspca_dev->usb_buf, len);
  484. } else {
  485. r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  486. req, pref, val, index, NULL, len, 400);
  487. }
  488. }
  489. if (r < 0)
  490. pr_err("ctrl transfer failed %4d [p%02x r%d v%04x i%04x len%d]\n",
  491. r, pref, req, val, index, len);
  492. else if (len > 1 && r < len)
  493. PDEBUG(D_ERR, "short ctrl transfer %d/%d", r, len);
  494. msleep(1);
  495. return r;
  496. }
  497. int fetch_validx(struct gspca_dev *gspca_dev, struct validx *tbl, int len)
  498. {
  499. int n;
  500. for (n = 0; n < len; n++) {
  501. if (tbl[n].idx != 0xffff)
  502. ctrl_out(gspca_dev, 0x40, 1, tbl[n].val,
  503. tbl[n].idx, 0, NULL);
  504. else if (tbl[n].val == 0xffff)
  505. break;
  506. else
  507. msleep(tbl[n].val);
  508. }
  509. return n;
  510. }
  511. int keep_on_fetching_validx(struct gspca_dev *gspca_dev, struct validx *tbl,
  512. int len, int n)
  513. {
  514. while (++n < len) {
  515. if (tbl[n].idx != 0xffff)
  516. ctrl_out(gspca_dev, 0x40, 1, tbl[n].val, tbl[n].idx,
  517. 0, NULL);
  518. else if (tbl[n].val == 0xffff)
  519. break;
  520. else
  521. msleep(tbl[n].val);
  522. }
  523. return n;
  524. }
  525. void fetch_idxdata(struct gspca_dev *gspca_dev, struct idxdata *tbl, int len)
  526. {
  527. int n;
  528. for (n = 0; n < len; n++) {
  529. if (memcmp(tbl[n].data, "\xff\xff\xff", 3) != 0)
  530. ctrl_out(gspca_dev, 0x40, 3, 0x7a00, tbl[n].idx,
  531. 3, tbl[n].data);
  532. else
  533. msleep(tbl[n].idx);
  534. }
  535. }
  536. static int gl860_guess_sensor(struct gspca_dev *gspca_dev,
  537. u16 vendor_id, u16 product_id)
  538. {
  539. struct sd *sd = (struct sd *) gspca_dev;
  540. u8 probe, nb26, nb96, nOV, ntry;
  541. if (product_id == 0xf191)
  542. sd->sensor = ID_MI1320;
  543. if (sd->sensor == 0xff) {
  544. ctrl_in(gspca_dev, 0xc0, 2, 0x0000, 0x0004, 1, &probe);
  545. ctrl_in(gspca_dev, 0xc0, 2, 0x0000, 0x0004, 1, &probe);
  546. ctrl_out(gspca_dev, 0x40, 1, 0x0000, 0x0000, 0, NULL);
  547. msleep(3);
  548. ctrl_out(gspca_dev, 0x40, 1, 0x0010, 0x0010, 0, NULL);
  549. msleep(3);
  550. ctrl_out(gspca_dev, 0x40, 1, 0x0008, 0x00c0, 0, NULL);
  551. msleep(3);
  552. ctrl_out(gspca_dev, 0x40, 1, 0x0001, 0x00c1, 0, NULL);
  553. msleep(3);
  554. ctrl_out(gspca_dev, 0x40, 1, 0x0001, 0x00c2, 0, NULL);
  555. msleep(3);
  556. ctrl_out(gspca_dev, 0x40, 1, 0x0020, 0x0006, 0, NULL);
  557. msleep(3);
  558. ctrl_out(gspca_dev, 0x40, 1, 0x006a, 0x000d, 0, NULL);
  559. msleep(56);
  560. PDEBUG(D_PROBE, "probing for sensor MI2020 or OVXXXX");
  561. nOV = 0;
  562. for (ntry = 0; ntry < 4; ntry++) {
  563. ctrl_out(gspca_dev, 0x40, 1, 0x0040, 0x0000, 0, NULL);
  564. msleep(3);
  565. ctrl_out(gspca_dev, 0x40, 1, 0x0063, 0x0006, 0, NULL);
  566. msleep(3);
  567. ctrl_out(gspca_dev, 0x40, 1, 0x7a00, 0x8030, 0, NULL);
  568. msleep(10);
  569. ctrl_in(gspca_dev, 0xc0, 2, 0x7a00, 0x8030, 1, &probe);
  570. PDEBUG(D_PROBE, "probe=0x%02x", probe);
  571. if (probe == 0xff)
  572. nOV++;
  573. }
  574. if (nOV) {
  575. PDEBUG(D_PROBE, "0xff -> OVXXXX");
  576. PDEBUG(D_PROBE, "probing for sensor OV2640 or OV9655");
  577. nb26 = nb96 = 0;
  578. for (ntry = 0; ntry < 4; ntry++) {
  579. ctrl_out(gspca_dev, 0x40, 1, 0x0040, 0x0000,
  580. 0, NULL);
  581. msleep(3);
  582. ctrl_out(gspca_dev, 0x40, 1, 0x6000, 0x800a,
  583. 0, NULL);
  584. msleep(10);
  585. /* Wait for 26(OV2640) or 96(OV9655) */
  586. ctrl_in(gspca_dev, 0xc0, 2, 0x6000, 0x800a,
  587. 1, &probe);
  588. if (probe == 0x26 || probe == 0x40) {
  589. PDEBUG(D_PROBE,
  590. "probe=0x%02x -> OV2640",
  591. probe);
  592. sd->sensor = ID_OV2640;
  593. nb26 += 4;
  594. break;
  595. }
  596. if (probe == 0x96 || probe == 0x55) {
  597. PDEBUG(D_PROBE,
  598. "probe=0x%02x -> OV9655",
  599. probe);
  600. sd->sensor = ID_OV9655;
  601. nb96 += 4;
  602. break;
  603. }
  604. PDEBUG(D_PROBE, "probe=0x%02x", probe);
  605. if (probe == 0x00)
  606. nb26++;
  607. if (probe == 0xff)
  608. nb96++;
  609. msleep(3);
  610. }
  611. if (nb26 < 4 && nb96 < 4)
  612. return -1;
  613. } else {
  614. PDEBUG(D_PROBE, "Not any 0xff -> MI2020");
  615. sd->sensor = ID_MI2020;
  616. }
  617. }
  618. if (_MI1320_) {
  619. PDEBUG(D_PROBE, "05e3:f191 sensor MI1320 (1.3M)");
  620. } else if (_MI2020_) {
  621. PDEBUG(D_PROBE, "05e3:0503 sensor MI2020 (2.0M)");
  622. } else if (_OV9655_) {
  623. PDEBUG(D_PROBE, "05e3:0503 sensor OV9655 (1.3M)");
  624. } else if (_OV2640_) {
  625. PDEBUG(D_PROBE, "05e3:0503 sensor OV2640 (2.0M)");
  626. } else {
  627. PDEBUG(D_PROBE, "***** Unknown sensor *****");
  628. return -1;
  629. }
  630. return 0;
  631. }