dpfp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * libusb example program to manipulate U.are.U 4000B fingerprint scanner.
  3. * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
  4. *
  5. * Basic image capture program only, does not consider the powerup quirks or
  6. * the fact that image encryption may be enabled. Not expected to work
  7. * flawlessly all of the time.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include <errno.h>
  24. #include <signal.h>
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <libusb.h>
  29. #define EP_INTR (1 | LIBUSB_ENDPOINT_IN)
  30. #define EP_DATA (2 | LIBUSB_ENDPOINT_IN)
  31. #define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN)
  32. #define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT)
  33. #define USB_RQ 0x04
  34. #define INTR_LENGTH 64
  35. enum {
  36. MODE_INIT = 0x00,
  37. MODE_AWAIT_FINGER_ON = 0x10,
  38. MODE_AWAIT_FINGER_OFF = 0x12,
  39. MODE_CAPTURE = 0x20,
  40. MODE_SHUT_UP = 0x30,
  41. MODE_READY = 0x80,
  42. };
  43. static int next_state(void);
  44. enum {
  45. STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON = 1,
  46. STATE_AWAIT_IRQ_FINGER_DETECTED,
  47. STATE_AWAIT_MODE_CHANGE_CAPTURE,
  48. STATE_AWAIT_IMAGE,
  49. STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF,
  50. STATE_AWAIT_IRQ_FINGER_REMOVED,
  51. };
  52. static int state = 0;
  53. static struct libusb_device_handle *devh = NULL;
  54. static unsigned char imgbuf[0x1b340];
  55. static unsigned char irqbuf[INTR_LENGTH];
  56. static struct libusb_transfer *img_transfer = NULL;
  57. static struct libusb_transfer *irq_transfer = NULL;
  58. static int img_idx = 0;
  59. static int do_exit = 0;
  60. static int find_dpfp_device(void)
  61. {
  62. devh = libusb_open_device_with_vid_pid(NULL, 0x05ba, 0x000a);
  63. return devh ? 0 : -EIO;
  64. }
  65. static int print_f0_data(void)
  66. {
  67. unsigned char data[0x10];
  68. int r;
  69. unsigned int i;
  70. r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0xf0, 0, data,
  71. sizeof(data), 0);
  72. if (r < 0) {
  73. fprintf(stderr, "F0 error %d\n", r);
  74. return r;
  75. }
  76. if ((unsigned int) r < sizeof(data)) {
  77. fprintf(stderr, "short read (%d)\n", r);
  78. return -1;
  79. }
  80. printf("F0 data:");
  81. for (i = 0; i < sizeof(data); i++)
  82. printf("%02x ", data[i]);
  83. printf("\n");
  84. return 0;
  85. }
  86. static int get_hwstat(unsigned char *status)
  87. {
  88. int r;
  89. r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0x07, 0, status, 1, 0);
  90. if (r < 0) {
  91. fprintf(stderr, "read hwstat error %d\n", r);
  92. return r;
  93. }
  94. if ((unsigned int) r < 1) {
  95. fprintf(stderr, "short read (%d)\n", r);
  96. return -1;
  97. }
  98. printf("hwstat reads %02x\n", *status);
  99. return 0;
  100. }
  101. static int set_hwstat(unsigned char data)
  102. {
  103. int r;
  104. printf("set hwstat to %02x\n", data);
  105. r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x07, 0, &data, 1, 0);
  106. if (r < 0) {
  107. fprintf(stderr, "set hwstat error %d\n", r);
  108. return r;
  109. }
  110. if ((unsigned int) r < 1) {
  111. fprintf(stderr, "short write (%d)", r);
  112. return -1;
  113. }
  114. return 0;
  115. }
  116. static int set_mode(unsigned char data)
  117. {
  118. int r;
  119. printf("set mode %02x\n", data);
  120. r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x4e, 0, &data, 1, 0);
  121. if (r < 0) {
  122. fprintf(stderr, "set mode error %d\n", r);
  123. return r;
  124. }
  125. if ((unsigned int) r < 1) {
  126. fprintf(stderr, "short write (%d)", r);
  127. return -1;
  128. }
  129. return 0;
  130. }
  131. static void LIBUSB_CALL cb_mode_changed(struct libusb_transfer *transfer)
  132. {
  133. if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
  134. fprintf(stderr, "mode change transfer not completed!\n");
  135. do_exit = 2;
  136. }
  137. printf("async cb_mode_changed length=%d actual_length=%d\n",
  138. transfer->length, transfer->actual_length);
  139. if (next_state() < 0)
  140. do_exit = 2;
  141. }
  142. static int set_mode_async(unsigned char data)
  143. {
  144. unsigned char *buf = malloc(LIBUSB_CONTROL_SETUP_SIZE + 1);
  145. struct libusb_transfer *transfer;
  146. if (!buf)
  147. return -ENOMEM;
  148. transfer = libusb_alloc_transfer(0);
  149. if (!transfer) {
  150. free(buf);
  151. return -ENOMEM;
  152. }
  153. printf("async set mode %02x\n", data);
  154. libusb_fill_control_setup(buf, CTRL_OUT, USB_RQ, 0x4e, 0, 1);
  155. buf[LIBUSB_CONTROL_SETUP_SIZE] = data;
  156. libusb_fill_control_transfer(transfer, devh, buf, cb_mode_changed, NULL,
  157. 1000);
  158. transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK
  159. | LIBUSB_TRANSFER_FREE_BUFFER | LIBUSB_TRANSFER_FREE_TRANSFER;
  160. return libusb_submit_transfer(transfer);
  161. }
  162. static int do_sync_intr(unsigned char *data)
  163. {
  164. int r;
  165. int transferred;
  166. r = libusb_interrupt_transfer(devh, EP_INTR, data, INTR_LENGTH,
  167. &transferred, 1000);
  168. if (r < 0) {
  169. fprintf(stderr, "intr error %d\n", r);
  170. return r;
  171. }
  172. if (transferred < INTR_LENGTH) {
  173. fprintf(stderr, "short read (%d)\n", r);
  174. return -1;
  175. }
  176. printf("recv interrupt %04x\n", *((uint16_t *) data));
  177. return 0;
  178. }
  179. static int sync_intr(unsigned char type)
  180. {
  181. int r;
  182. unsigned char data[INTR_LENGTH];
  183. while (1) {
  184. r = do_sync_intr(data);
  185. if (r < 0)
  186. return r;
  187. if (data[0] == type)
  188. return 0;
  189. }
  190. }
  191. static int save_to_file(unsigned char *data)
  192. {
  193. FILE *fd;
  194. char filename[64];
  195. snprintf(filename, sizeof(filename), "finger%d.pgm", img_idx++);
  196. fd = fopen(filename, "w");
  197. if (!fd)
  198. return -1;
  199. fputs("P5 384 289 255 ", fd);
  200. (void) fwrite(data + 64, 1, 384*289, fd);
  201. fclose(fd);
  202. printf("saved image to %s\n", filename);
  203. return 0;
  204. }
  205. static int next_state(void)
  206. {
  207. int r = 0;
  208. printf("old state: %d\n", state);
  209. switch (state) {
  210. case STATE_AWAIT_IRQ_FINGER_REMOVED:
  211. state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON;
  212. r = set_mode_async(MODE_AWAIT_FINGER_ON);
  213. break;
  214. case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON:
  215. state = STATE_AWAIT_IRQ_FINGER_DETECTED;
  216. break;
  217. case STATE_AWAIT_IRQ_FINGER_DETECTED:
  218. state = STATE_AWAIT_MODE_CHANGE_CAPTURE;
  219. r = set_mode_async(MODE_CAPTURE);
  220. break;
  221. case STATE_AWAIT_MODE_CHANGE_CAPTURE:
  222. state = STATE_AWAIT_IMAGE;
  223. break;
  224. case STATE_AWAIT_IMAGE:
  225. state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF;
  226. r = set_mode_async(MODE_AWAIT_FINGER_OFF);
  227. break;
  228. case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF:
  229. state = STATE_AWAIT_IRQ_FINGER_REMOVED;
  230. break;
  231. default:
  232. printf("unrecognised state %d\n", state);
  233. }
  234. if (r < 0) {
  235. fprintf(stderr, "error detected changing state\n");
  236. return r;
  237. }
  238. printf("new state: %d\n", state);
  239. return 0;
  240. }
  241. static void LIBUSB_CALL cb_irq(struct libusb_transfer *transfer)
  242. {
  243. unsigned char irqtype = transfer->buffer[0];
  244. if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
  245. fprintf(stderr, "irq transfer status %d?\n", transfer->status);
  246. do_exit = 2;
  247. libusb_free_transfer(transfer);
  248. irq_transfer = NULL;
  249. return;
  250. }
  251. printf("IRQ callback %02x\n", irqtype);
  252. switch (state) {
  253. case STATE_AWAIT_IRQ_FINGER_DETECTED:
  254. if (irqtype == 0x01) {
  255. if (next_state() < 0) {
  256. do_exit = 2;
  257. return;
  258. }
  259. } else {
  260. printf("finger-on-sensor detected in wrong state!\n");
  261. }
  262. break;
  263. case STATE_AWAIT_IRQ_FINGER_REMOVED:
  264. if (irqtype == 0x02) {
  265. if (next_state() < 0) {
  266. do_exit = 2;
  267. return;
  268. }
  269. } else {
  270. printf("finger-on-sensor detected in wrong state!\n");
  271. }
  272. break;
  273. }
  274. if (libusb_submit_transfer(irq_transfer) < 0)
  275. do_exit = 2;
  276. }
  277. static void LIBUSB_CALL cb_img(struct libusb_transfer *transfer)
  278. {
  279. if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
  280. fprintf(stderr, "img transfer status %d?\n", transfer->status);
  281. do_exit = 2;
  282. libusb_free_transfer(transfer);
  283. img_transfer = NULL;
  284. return;
  285. }
  286. printf("Image callback\n");
  287. save_to_file(imgbuf);
  288. if (next_state() < 0) {
  289. do_exit = 2;
  290. return;
  291. }
  292. if (libusb_submit_transfer(img_transfer) < 0)
  293. do_exit = 2;
  294. }
  295. static int init_capture(void)
  296. {
  297. int r;
  298. r = libusb_submit_transfer(irq_transfer);
  299. if (r < 0)
  300. return r;
  301. r = libusb_submit_transfer(img_transfer);
  302. if (r < 0) {
  303. libusb_cancel_transfer(irq_transfer);
  304. while (irq_transfer)
  305. if (libusb_handle_events(NULL) < 0)
  306. break;
  307. return r;
  308. }
  309. /* start state machine */
  310. state = STATE_AWAIT_IRQ_FINGER_REMOVED;
  311. return next_state();
  312. }
  313. static int do_init(void)
  314. {
  315. unsigned char status;
  316. int r;
  317. r = get_hwstat(&status);
  318. if (r < 0)
  319. return r;
  320. if (!(status & 0x80)) {
  321. r = set_hwstat(status | 0x80);
  322. if (r < 0)
  323. return r;
  324. r = get_hwstat(&status);
  325. if (r < 0)
  326. return r;
  327. }
  328. status &= ~0x80;
  329. r = set_hwstat(status);
  330. if (r < 0)
  331. return r;
  332. r = get_hwstat(&status);
  333. if (r < 0)
  334. return r;
  335. r = sync_intr(0x56);
  336. if (r < 0)
  337. return r;
  338. return 0;
  339. }
  340. static int alloc_transfers(void)
  341. {
  342. img_transfer = libusb_alloc_transfer(0);
  343. if (!img_transfer)
  344. return -ENOMEM;
  345. irq_transfer = libusb_alloc_transfer(0);
  346. if (!irq_transfer)
  347. return -ENOMEM;
  348. libusb_fill_bulk_transfer(img_transfer, devh, EP_DATA, imgbuf,
  349. sizeof(imgbuf), cb_img, NULL, 0);
  350. libusb_fill_interrupt_transfer(irq_transfer, devh, EP_INTR, irqbuf,
  351. sizeof(irqbuf), cb_irq, NULL, 0);
  352. return 0;
  353. }
  354. static void sighandler(int signum)
  355. {
  356. do_exit = 1;
  357. }
  358. int main(void)
  359. {
  360. struct sigaction sigact;
  361. int r = 1;
  362. r = libusb_init(NULL);
  363. if (r < 0) {
  364. fprintf(stderr, "failed to initialise libusb\n");
  365. exit(1);
  366. }
  367. r = find_dpfp_device();
  368. if (r < 0) {
  369. fprintf(stderr, "Could not find/open device\n");
  370. goto out;
  371. }
  372. r = libusb_claim_interface(devh, 0);
  373. if (r < 0) {
  374. fprintf(stderr, "usb_claim_interface error %d\n", r);
  375. goto out;
  376. }
  377. printf("claimed interface\n");
  378. r = print_f0_data();
  379. if (r < 0)
  380. goto out_release;
  381. r = do_init();
  382. if (r < 0)
  383. goto out_deinit;
  384. /* async from here onwards */
  385. r = alloc_transfers();
  386. if (r < 0)
  387. goto out_deinit;
  388. r = init_capture();
  389. if (r < 0)
  390. goto out_deinit;
  391. sigact.sa_handler = sighandler;
  392. sigemptyset(&sigact.sa_mask);
  393. sigact.sa_flags = 0;
  394. sigaction(SIGINT, &sigact, NULL);
  395. sigaction(SIGTERM, &sigact, NULL);
  396. sigaction(SIGQUIT, &sigact, NULL);
  397. while (!do_exit) {
  398. r = libusb_handle_events(NULL);
  399. if (r < 0)
  400. goto out_deinit;
  401. }
  402. printf("shutting down...\n");
  403. if (irq_transfer) {
  404. r = libusb_cancel_transfer(irq_transfer);
  405. if (r < 0)
  406. goto out_deinit;
  407. }
  408. if (img_transfer) {
  409. r = libusb_cancel_transfer(img_transfer);
  410. if (r < 0)
  411. goto out_deinit;
  412. }
  413. while (irq_transfer || img_transfer)
  414. if (libusb_handle_events(NULL) < 0)
  415. break;
  416. if (do_exit == 1)
  417. r = 0;
  418. else
  419. r = 1;
  420. out_deinit:
  421. libusb_free_transfer(img_transfer);
  422. libusb_free_transfer(irq_transfer);
  423. set_mode(0);
  424. set_hwstat(0x80);
  425. out_release:
  426. libusb_release_interface(devh, 0);
  427. out:
  428. libusb_close(devh);
  429. libusb_exit(NULL);
  430. return r >= 0 ? r : -r;
  431. }