dpfp_threaded.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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 <pthread.h>
  25. #include <signal.h>
  26. #include <string.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <libusb.h>
  30. #define EP_INTR (1 | LIBUSB_ENDPOINT_IN)
  31. #define EP_DATA (2 | LIBUSB_ENDPOINT_IN)
  32. #define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN)
  33. #define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT)
  34. #define USB_RQ 0x04
  35. #define INTR_LENGTH 64
  36. enum {
  37. MODE_INIT = 0x00,
  38. MODE_AWAIT_FINGER_ON = 0x10,
  39. MODE_AWAIT_FINGER_OFF = 0x12,
  40. MODE_CAPTURE = 0x20,
  41. MODE_SHUT_UP = 0x30,
  42. MODE_READY = 0x80,
  43. };
  44. static int next_state(void);
  45. enum {
  46. STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON = 1,
  47. STATE_AWAIT_IRQ_FINGER_DETECTED,
  48. STATE_AWAIT_MODE_CHANGE_CAPTURE,
  49. STATE_AWAIT_IMAGE,
  50. STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF,
  51. STATE_AWAIT_IRQ_FINGER_REMOVED,
  52. };
  53. static int state = 0;
  54. static struct libusb_device_handle *devh = NULL;
  55. static unsigned char imgbuf[0x1b340];
  56. static unsigned char irqbuf[INTR_LENGTH];
  57. static struct libusb_transfer *img_transfer = NULL;
  58. static struct libusb_transfer *irq_transfer = NULL;
  59. static int img_idx = 0;
  60. static int do_exit = 0;
  61. static pthread_t poll_thread;
  62. static pthread_cond_t exit_cond = PTHREAD_COND_INITIALIZER;
  63. static pthread_mutex_t exit_cond_lock = PTHREAD_MUTEX_INITIALIZER;
  64. static void request_exit(int code)
  65. {
  66. do_exit = code;
  67. pthread_cond_signal(&exit_cond);
  68. }
  69. static void *poll_thread_main(void *arg)
  70. {
  71. int r = 0;
  72. printf("poll thread running\n");
  73. while (!do_exit) {
  74. struct timeval tv = { 1, 0 };
  75. r = libusb_handle_events_timeout(NULL, &tv);
  76. if (r < 0) {
  77. request_exit(2);
  78. break;
  79. }
  80. }
  81. printf("poll thread shutting down\n");
  82. return NULL;
  83. }
  84. static int find_dpfp_device(void)
  85. {
  86. devh = libusb_open_device_with_vid_pid(NULL, 0x05ba, 0x000a);
  87. return devh ? 0 : -EIO;
  88. }
  89. static int print_f0_data(void)
  90. {
  91. unsigned char data[0x10];
  92. int r;
  93. unsigned int i;
  94. r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0xf0, 0, data,
  95. sizeof(data), 0);
  96. if (r < 0) {
  97. fprintf(stderr, "F0 error %d\n", r);
  98. return r;
  99. }
  100. if ((unsigned int) r < sizeof(data)) {
  101. fprintf(stderr, "short read (%d)\n", r);
  102. return -1;
  103. }
  104. printf("F0 data:");
  105. for (i = 0; i < sizeof(data); i++)
  106. printf("%02x ", data[i]);
  107. printf("\n");
  108. return 0;
  109. }
  110. static int get_hwstat(unsigned char *status)
  111. {
  112. int r;
  113. r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0x07, 0, status, 1, 0);
  114. if (r < 0) {
  115. fprintf(stderr, "read hwstat error %d\n", r);
  116. return r;
  117. }
  118. if ((unsigned int) r < 1) {
  119. fprintf(stderr, "short read (%d)\n", r);
  120. return -1;
  121. }
  122. printf("hwstat reads %02x\n", *status);
  123. return 0;
  124. }
  125. static int set_hwstat(unsigned char data)
  126. {
  127. int r;
  128. printf("set hwstat to %02x\n", data);
  129. r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x07, 0, &data, 1, 0);
  130. if (r < 0) {
  131. fprintf(stderr, "set hwstat error %d\n", r);
  132. return r;
  133. }
  134. if ((unsigned int) r < 1) {
  135. fprintf(stderr, "short write (%d)", r);
  136. return -1;
  137. }
  138. return 0;
  139. }
  140. static int set_mode(unsigned char data)
  141. {
  142. int r;
  143. printf("set mode %02x\n", data);
  144. r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x4e, 0, &data, 1, 0);
  145. if (r < 0) {
  146. fprintf(stderr, "set mode error %d\n", r);
  147. return r;
  148. }
  149. if ((unsigned int) r < 1) {
  150. fprintf(stderr, "short write (%d)", r);
  151. return -1;
  152. }
  153. return 0;
  154. }
  155. static void LIBUSB_CALL cb_mode_changed(struct libusb_transfer *transfer)
  156. {
  157. if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
  158. fprintf(stderr, "mode change transfer not completed!\n");
  159. request_exit(2);
  160. }
  161. printf("async cb_mode_changed length=%d actual_length=%d\n",
  162. transfer->length, transfer->actual_length);
  163. if (next_state() < 0)
  164. request_exit(2);
  165. }
  166. static int set_mode_async(unsigned char data)
  167. {
  168. unsigned char *buf = malloc(LIBUSB_CONTROL_SETUP_SIZE + 1);
  169. struct libusb_transfer *transfer;
  170. if (!buf)
  171. return -ENOMEM;
  172. transfer = libusb_alloc_transfer(0);
  173. if (!transfer) {
  174. free(buf);
  175. return -ENOMEM;
  176. }
  177. printf("async set mode %02x\n", data);
  178. libusb_fill_control_setup(buf, CTRL_OUT, USB_RQ, 0x4e, 0, 1);
  179. buf[LIBUSB_CONTROL_SETUP_SIZE] = data;
  180. libusb_fill_control_transfer(transfer, devh, buf, cb_mode_changed, NULL,
  181. 1000);
  182. transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK
  183. | LIBUSB_TRANSFER_FREE_BUFFER | LIBUSB_TRANSFER_FREE_TRANSFER;
  184. return libusb_submit_transfer(transfer);
  185. }
  186. static int do_sync_intr(unsigned char *data)
  187. {
  188. int r;
  189. int transferred;
  190. r = libusb_interrupt_transfer(devh, EP_INTR, data, INTR_LENGTH,
  191. &transferred, 1000);
  192. if (r < 0) {
  193. fprintf(stderr, "intr error %d\n", r);
  194. return r;
  195. }
  196. if (transferred < INTR_LENGTH) {
  197. fprintf(stderr, "short read (%d)\n", r);
  198. return -1;
  199. }
  200. printf("recv interrupt %04x\n", *((uint16_t *) data));
  201. return 0;
  202. }
  203. static int sync_intr(unsigned char type)
  204. {
  205. int r;
  206. unsigned char data[INTR_LENGTH];
  207. while (1) {
  208. r = do_sync_intr(data);
  209. if (r < 0)
  210. return r;
  211. if (data[0] == type)
  212. return 0;
  213. }
  214. }
  215. static int save_to_file(unsigned char *data)
  216. {
  217. FILE *fd;
  218. char filename[64];
  219. snprintf(filename, sizeof(filename), "finger%d.pgm", img_idx++);
  220. fd = fopen(filename, "w");
  221. if (!fd)
  222. return -1;
  223. fputs("P5 384 289 255 ", fd);
  224. (void) fwrite(data + 64, 1, 384*289, fd);
  225. fclose(fd);
  226. printf("saved image to %s\n", filename);
  227. return 0;
  228. }
  229. static int next_state(void)
  230. {
  231. int r = 0;
  232. printf("old state: %d\n", state);
  233. switch (state) {
  234. case STATE_AWAIT_IRQ_FINGER_REMOVED:
  235. state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON;
  236. r = set_mode_async(MODE_AWAIT_FINGER_ON);
  237. break;
  238. case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON:
  239. state = STATE_AWAIT_IRQ_FINGER_DETECTED;
  240. break;
  241. case STATE_AWAIT_IRQ_FINGER_DETECTED:
  242. state = STATE_AWAIT_MODE_CHANGE_CAPTURE;
  243. r = set_mode_async(MODE_CAPTURE);
  244. break;
  245. case STATE_AWAIT_MODE_CHANGE_CAPTURE:
  246. state = STATE_AWAIT_IMAGE;
  247. break;
  248. case STATE_AWAIT_IMAGE:
  249. state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF;
  250. r = set_mode_async(MODE_AWAIT_FINGER_OFF);
  251. break;
  252. case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF:
  253. state = STATE_AWAIT_IRQ_FINGER_REMOVED;
  254. break;
  255. default:
  256. printf("unrecognised state %d\n", state);
  257. }
  258. if (r < 0) {
  259. fprintf(stderr, "error detected changing state\n");
  260. return r;
  261. }
  262. printf("new state: %d\n", state);
  263. return 0;
  264. }
  265. static void LIBUSB_CALL cb_irq(struct libusb_transfer *transfer)
  266. {
  267. unsigned char irqtype = transfer->buffer[0];
  268. if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
  269. fprintf(stderr, "irq transfer status %d?\n", transfer->status);
  270. irq_transfer = NULL;
  271. request_exit(2);
  272. return;
  273. }
  274. printf("IRQ callback %02x\n", irqtype);
  275. switch (state) {
  276. case STATE_AWAIT_IRQ_FINGER_DETECTED:
  277. if (irqtype == 0x01) {
  278. if (next_state() < 0) {
  279. request_exit(2);
  280. return;
  281. }
  282. } else {
  283. printf("finger-on-sensor detected in wrong state!\n");
  284. }
  285. break;
  286. case STATE_AWAIT_IRQ_FINGER_REMOVED:
  287. if (irqtype == 0x02) {
  288. if (next_state() < 0) {
  289. request_exit(2);
  290. return;
  291. }
  292. } else {
  293. printf("finger-on-sensor detected in wrong state!\n");
  294. }
  295. break;
  296. }
  297. if (libusb_submit_transfer(irq_transfer) < 0)
  298. request_exit(2);
  299. }
  300. static void LIBUSB_CALL cb_img(struct libusb_transfer *transfer)
  301. {
  302. if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
  303. fprintf(stderr, "img transfer status %d?\n", transfer->status);
  304. img_transfer = NULL;
  305. request_exit(2);
  306. return;
  307. }
  308. printf("Image callback\n");
  309. save_to_file(imgbuf);
  310. if (next_state() < 0) {
  311. request_exit(2);
  312. return;
  313. }
  314. if (libusb_submit_transfer(img_transfer) < 0)
  315. request_exit(2);
  316. }
  317. static int init_capture(void)
  318. {
  319. int r;
  320. r = libusb_submit_transfer(irq_transfer);
  321. if (r < 0)
  322. return r;
  323. r = libusb_submit_transfer(img_transfer);
  324. if (r < 0) {
  325. libusb_cancel_transfer(irq_transfer);
  326. while (irq_transfer)
  327. if (libusb_handle_events(NULL) < 0)
  328. break;
  329. return r;
  330. }
  331. /* start state machine */
  332. state = STATE_AWAIT_IRQ_FINGER_REMOVED;
  333. return next_state();
  334. }
  335. static int do_init(void)
  336. {
  337. unsigned char status;
  338. int r;
  339. r = get_hwstat(&status);
  340. if (r < 0)
  341. return r;
  342. if (!(status & 0x80)) {
  343. r = set_hwstat(status | 0x80);
  344. if (r < 0)
  345. return r;
  346. r = get_hwstat(&status);
  347. if (r < 0)
  348. return r;
  349. }
  350. status &= ~0x80;
  351. r = set_hwstat(status);
  352. if (r < 0)
  353. return r;
  354. r = get_hwstat(&status);
  355. if (r < 0)
  356. return r;
  357. r = sync_intr(0x56);
  358. if (r < 0)
  359. return r;
  360. return 0;
  361. }
  362. static int alloc_transfers(void)
  363. {
  364. img_transfer = libusb_alloc_transfer(0);
  365. if (!img_transfer)
  366. return -ENOMEM;
  367. irq_transfer = libusb_alloc_transfer(0);
  368. if (!irq_transfer)
  369. return -ENOMEM;
  370. libusb_fill_bulk_transfer(img_transfer, devh, EP_DATA, imgbuf,
  371. sizeof(imgbuf), cb_img, NULL, 0);
  372. libusb_fill_interrupt_transfer(irq_transfer, devh, EP_INTR, irqbuf,
  373. sizeof(irqbuf), cb_irq, NULL, 0);
  374. return 0;
  375. }
  376. static void sighandler(int signum)
  377. {
  378. request_exit(1);
  379. }
  380. int main(void)
  381. {
  382. struct sigaction sigact;
  383. int r = 1;
  384. r = libusb_init(NULL);
  385. if (r < 0) {
  386. fprintf(stderr, "failed to initialise libusb\n");
  387. exit(1);
  388. }
  389. r = find_dpfp_device();
  390. if (r < 0) {
  391. fprintf(stderr, "Could not find/open device\n");
  392. goto out;
  393. }
  394. r = libusb_claim_interface(devh, 0);
  395. if (r < 0) {
  396. fprintf(stderr, "usb_claim_interface error %d %s\n", r, strerror(-r));
  397. goto out;
  398. }
  399. printf("claimed interface\n");
  400. r = print_f0_data();
  401. if (r < 0)
  402. goto out_release;
  403. r = do_init();
  404. if (r < 0)
  405. goto out_deinit;
  406. /* async from here onwards */
  407. sigact.sa_handler = sighandler;
  408. sigemptyset(&sigact.sa_mask);
  409. sigact.sa_flags = 0;
  410. sigaction(SIGINT, &sigact, NULL);
  411. sigaction(SIGTERM, &sigact, NULL);
  412. sigaction(SIGQUIT, &sigact, NULL);
  413. r = pthread_create(&poll_thread, NULL, poll_thread_main, NULL);
  414. if (r)
  415. goto out_deinit;
  416. r = alloc_transfers();
  417. if (r < 0) {
  418. request_exit(1);
  419. pthread_join(poll_thread, NULL);
  420. goto out_deinit;
  421. }
  422. r = init_capture();
  423. if (r < 0) {
  424. request_exit(1);
  425. pthread_join(poll_thread, NULL);
  426. goto out_deinit;
  427. }
  428. while (!do_exit) {
  429. pthread_mutex_lock(&exit_cond_lock);
  430. pthread_cond_wait(&exit_cond, &exit_cond_lock);
  431. pthread_mutex_unlock(&exit_cond_lock);
  432. }
  433. printf("shutting down...\n");
  434. pthread_join(poll_thread, NULL);
  435. r = libusb_cancel_transfer(irq_transfer);
  436. if (r < 0) {
  437. request_exit(1);
  438. goto out_deinit;
  439. }
  440. r = libusb_cancel_transfer(img_transfer);
  441. if (r < 0) {
  442. request_exit(1);
  443. goto out_deinit;
  444. }
  445. while (img_transfer || irq_transfer)
  446. if (libusb_handle_events(NULL) < 0)
  447. break;
  448. if (do_exit == 1)
  449. r = 0;
  450. else
  451. r = 1;
  452. out_deinit:
  453. libusb_free_transfer(img_transfer);
  454. libusb_free_transfer(irq_transfer);
  455. set_mode(0);
  456. set_hwstat(0x80);
  457. out_release:
  458. libusb_release_interface(devh, 0);
  459. out:
  460. libusb_close(devh);
  461. libusb_exit(NULL);
  462. return r >= 0 ? r : -r;
  463. }