2 * libusb example program to manipulate U.are.U 4000B fingerprint scanner.
3 * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
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.
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.
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.
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
30 #include <libusb/libusb.h>
32 #define EP_INTR (1 | LIBUSB_ENDPOINT_IN)
33 #define EP_DATA (2 | LIBUSB_ENDPOINT_IN)
34 #define CTRL_IN (LIBUSB_TYPE_VENDOR | LIBUSB_ENDPOINT_IN)
35 #define CTRL_OUT (LIBUSB_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT)
37 #define INTR_LENGTH 64
41 MODE_AWAIT_FINGER_ON = 0x10,
42 MODE_AWAIT_FINGER_OFF = 0x12,
48 static int next_state(void);
49 static int submit_irq_urb(void);
50 static int submit_img_urb(void);
53 STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON = 1,
54 STATE_AWAIT_IRQ_FINGER_DETECTED,
55 STATE_AWAIT_MODE_CHANGE_CAPTURE,
57 STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF,
58 STATE_AWAIT_IRQ_FINGER_REMOVED,
62 static struct libusb_dev_handle *devh = NULL;
63 static unsigned char imgbuf[0x1b340];
64 static unsigned char irqbuf[INTR_LENGTH];
65 static libusb_transfer *img_urbh = NULL;
66 static libusb_transfer *irq_urbh = NULL;
67 static int img_idx = 0;
68 static int do_exit = 0;
70 static struct libusb_bulk_transfer_request imgrq = {
73 .length = sizeof(imgbuf),
76 static struct libusb_bulk_transfer_request intrrq = {
79 .length = sizeof(irqbuf),
82 static int find_dpfp_device(void)
84 devh = libusb_open_device_with_vid_pid(0x05ba, 0x000a);
85 return devh ? 0 : -EIO;
88 static int print_f0_data(void)
90 unsigned char data[0x10];
91 struct libusb_control_transfer_request rq = {
92 .requesttype = CTRL_IN,
96 .length = sizeof(data),
102 r = libusb_control_transfer(devh, &rq, 0);
104 fprintf(stderr, "F0 error %d\n", r);
107 if ((unsigned int) r < sizeof(data)) {
108 fprintf(stderr, "short read (%d)\n", r);
113 for (i = 0; i < sizeof(data); i++)
114 printf("%02x ", data[i]);
119 static int get_hwstat(unsigned char *status)
121 struct libusb_control_transfer_request rq = {
122 .requesttype = CTRL_IN,
131 r = libusb_control_transfer(devh, &rq, 0);
133 fprintf(stderr, "read hwstat error %d\n", r);
136 if ((unsigned int) r < 1) {
137 fprintf(stderr, "short read (%d)\n", r);
141 printf("hwstat reads %02x\n", *status);
145 static int set_hwstat(unsigned char data)
148 struct libusb_control_transfer_request rq = {
149 .requesttype = CTRL_OUT,
157 printf("set hwstat to %02x\n", data);
159 r = libusb_control_transfer(devh, &rq, 0);
161 fprintf(stderr, "set hwstat error %d\n", r);
164 if ((unsigned int) r < 1) {
165 fprintf(stderr, "short write (%d)", r);
172 static int set_mode(unsigned char data)
175 struct libusb_control_transfer_request rq = {
176 .requesttype = CTRL_OUT,
184 printf("set mode %02x\n", data);
186 r = libusb_control_transfer(devh, &rq, 0);
188 fprintf(stderr, "set mode error %d\n", r);
191 if ((unsigned int) r < 1) {
192 fprintf(stderr, "short write (%d)", r);
199 static void cb_mode_changed(struct libusb_dev_handle *_devh,
200 struct libusb_transfer *urbh, enum libusb_transfer_status status,
201 struct libusb_control_setup *setup, unsigned char *data, int actual_length,
204 if (status != LIBUSB_TRANSFER_COMPLETED) {
205 fprintf(stderr, "mode change URB not completed!\n");
209 printf("async cb_mode_changed\n");
210 if (next_state() < 0)
214 static int set_mode_async(unsigned char data)
216 libusb_transfer *urbh;
217 struct libusb_control_transfer_request rq = {
218 .requesttype = CTRL_OUT,
226 printf("async set mode %02x\n", data);
228 urbh = libusb_async_control_transfer(devh, &rq, cb_mode_changed, NULL,
231 fprintf(stderr, "set mode submit error\n");
238 static int do_sync_intr(unsigned char *data)
240 struct libusb_bulk_transfer_request request = {
243 .length = INTR_LENGTH,
248 r = libusb_interrupt_transfer(devh, &request, &transferred, 1000);
250 fprintf(stderr, "intr error %d\n", r);
253 if (transferred < INTR_LENGTH) {
254 fprintf(stderr, "short read (%d)\n", r);
258 printf("recv interrupt %04x\n", *((uint16_t *) data));
262 static int sync_intr(unsigned char type)
265 unsigned char data[INTR_LENGTH];
268 r = do_sync_intr(data);
276 static int save_to_file(unsigned char *data)
281 sprintf(filename, "finger%d.pgm", img_idx++);
282 fd = fopen(filename, "w");
286 fputs("P5 384 289 255 ", fd);
287 fwrite(data + 64, 1, 384*289, fd);
289 printf("saved image to %s\n", filename);
293 static int next_state(void)
296 printf("old state: %d\n", state);
298 case STATE_AWAIT_IRQ_FINGER_REMOVED:
299 state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON;
300 r = set_mode_async(MODE_AWAIT_FINGER_ON);
302 case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON:
303 state = STATE_AWAIT_IRQ_FINGER_DETECTED;
305 case STATE_AWAIT_IRQ_FINGER_DETECTED:
306 state = STATE_AWAIT_MODE_CHANGE_CAPTURE;
307 r = set_mode_async(MODE_CAPTURE);
309 case STATE_AWAIT_MODE_CHANGE_CAPTURE:
310 state = STATE_AWAIT_IMAGE;
312 case STATE_AWAIT_IMAGE:
313 state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF;
314 r = set_mode_async(MODE_AWAIT_FINGER_OFF);
316 case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF:
317 state = STATE_AWAIT_IRQ_FINGER_REMOVED;
320 printf("unrecognised state %d\n", state);
323 fprintf(stderr, "error detected changing state");
327 printf("new state: %d\n", state);
331 static void cb_irq(libusb_dev_handle *_devh, libusb_transfer *urbh,
332 enum libusb_transfer_status status, unsigned char endpoint, int rqlength,
333 unsigned char *data, int actual_length, void *user_data)
335 unsigned char irqtype = data[0];
337 if (status != LIBUSB_TRANSFER_COMPLETED) {
338 fprintf(stderr, "irq URB status %d?\n", status);
343 printf("IRQ callback %02x\n", irqtype);
345 case STATE_AWAIT_IRQ_FINGER_DETECTED:
346 if (irqtype == 0x01) {
347 if (next_state() < 0) {
352 printf("finger-on-sensor detected in wrong state!\n");
355 case STATE_AWAIT_IRQ_FINGER_REMOVED:
356 if (irqtype == 0x02) {
357 if (next_state() < 0) {
362 printf("finger-on-sensor detected in wrong state!\n");
366 if (submit_irq_urb() < 0)
370 static void cb_img(libusb_dev_handle *_devh, libusb_transfer *urbh,
371 enum libusb_transfer_status status, unsigned char endpoint, int rqlength,
372 unsigned char *data, int actual_length, void *user_data)
374 if (status != LIBUSB_TRANSFER_COMPLETED) {
375 fprintf(stderr, "img URB status %d?\n", status);
380 printf("Image callback\n");
381 save_to_file(imgbuf);
382 if (next_state() < 0) {
386 if (submit_img_urb() < 0)
390 static int submit_irq_urb(void)
392 libusb_transfer_free(irq_urbh);
393 irq_urbh = libusb_async_interrupt_transfer(devh, &intrrq, cb_irq, NULL, 0);
394 return irq_urbh != NULL;
397 static int submit_img_urb(void)
399 libusb_transfer_free(img_urbh);
400 img_urbh = libusb_async_bulk_transfer(devh, &imgrq, cb_img, NULL, 0);
401 return img_urbh != NULL;
404 static int init_capture(void)
408 r = submit_irq_urb();
412 r = submit_img_urb();
414 libusb_transfer_cancel_sync(devh, img_urbh);
418 /* start state machine */
419 state = STATE_AWAIT_IRQ_FINGER_REMOVED;
423 static int do_init(void)
425 unsigned char status;
428 r = get_hwstat(&status);
432 if (!(status & 0x80)) {
433 r = set_hwstat(status | 0x80);
436 r = get_hwstat(&status);
442 r = set_hwstat(status);
446 r = get_hwstat(&status);
457 static void sighandler(int signum)
464 struct sigaction sigact;
469 fprintf(stderr, "failed to initialise libusb\n");
473 r = find_dpfp_device();
475 fprintf(stderr, "Could not find/open device\n");
479 r = libusb_claim_interface(devh, 0);
481 fprintf(stderr, "usb_claim_interface error %d %s\n", r, strerror(-r));
484 printf("claimed interface\n");
494 /* async from here onwards */
500 sigact.sa_handler = sighandler;
501 sigemptyset(&sigact.sa_mask);
503 sigaction(SIGINT, &sigact, NULL);
504 sigaction(SIGTERM, &sigact, NULL);
505 sigaction(SIGQUIT, &sigact, NULL);
513 printf("shutting down...\n");
515 r = libusb_transfer_cancel_sync(devh, irq_urbh);
519 r = libusb_transfer_cancel_sync(devh, img_urbh);
529 libusb_transfer_free(img_urbh);
530 libusb_transfer_free(irq_urbh);
534 libusb_release_interface(devh, 0);
538 return r >= 0 ? r : -r;