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
32 #define EP_INTR (1 | LIBUSB_ENDPOINT_IN)
33 #define EP_DATA (2 | LIBUSB_ENDPOINT_IN)
34 #define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN)
35 #define CTRL_OUT (LIBUSB_REQUEST_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);
51 STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON = 1,
52 STATE_AWAIT_IRQ_FINGER_DETECTED,
53 STATE_AWAIT_MODE_CHANGE_CAPTURE,
55 STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF,
56 STATE_AWAIT_IRQ_FINGER_REMOVED,
60 static struct libusb_device_handle *devh = NULL;
61 static unsigned char imgbuf[0x1b340];
62 static unsigned char irqbuf[INTR_LENGTH];
63 static struct libusb_transfer *img_transfer = NULL;
64 static struct libusb_transfer *irq_transfer = NULL;
65 static int img_idx = 0;
66 static int do_exit = 0;
68 static int find_dpfp_device(void)
70 devh = libusb_open_device_with_vid_pid(NULL, 0x05ba, 0x000a);
71 return devh ? 0 : -EIO;
74 static int print_f0_data(void)
76 unsigned char data[0x10];
80 r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0xf0, 0, data,
83 fprintf(stderr, "F0 error %d\n", r);
86 if ((unsigned int) r < sizeof(data)) {
87 fprintf(stderr, "short read (%d)\n", r);
92 for (i = 0; i < sizeof(data); i++)
93 printf("%02x ", data[i]);
98 static int get_hwstat(unsigned char *status)
102 r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0x07, 0, status, 1, 0);
104 fprintf(stderr, "read hwstat error %d\n", r);
107 if ((unsigned int) r < 1) {
108 fprintf(stderr, "short read (%d)\n", r);
112 printf("hwstat reads %02x\n", *status);
116 static int set_hwstat(unsigned char data)
120 printf("set hwstat to %02x\n", data);
121 r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x07, 0, &data, 1, 0);
123 fprintf(stderr, "set hwstat error %d\n", r);
126 if ((unsigned int) r < 1) {
127 fprintf(stderr, "short write (%d)", r);
134 static int set_mode(unsigned char data)
137 printf("set mode %02x\n", data);
139 r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x4e, 0, &data, 1, 0);
141 fprintf(stderr, "set mode error %d\n", r);
144 if ((unsigned int) r < 1) {
145 fprintf(stderr, "short write (%d)", r);
152 static void LIBUSB_CALL cb_mode_changed(struct libusb_transfer *transfer)
154 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
155 fprintf(stderr, "mode change transfer not completed!\n");
159 printf("async cb_mode_changed length=%d actual_length=%d\n",
160 transfer->length, transfer->actual_length);
161 if (next_state() < 0)
165 static int set_mode_async(unsigned char data)
167 unsigned char *buf = malloc(LIBUSB_CONTROL_SETUP_SIZE + 1);
168 struct libusb_transfer *transfer;
173 transfer = libusb_alloc_transfer(0);
179 printf("async set mode %02x\n", data);
180 libusb_fill_control_setup(buf, CTRL_OUT, USB_RQ, 0x4e, 0, 1);
181 buf[LIBUSB_CONTROL_SETUP_SIZE] = data;
182 libusb_fill_control_transfer(transfer, devh, buf, cb_mode_changed, NULL,
185 transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK
186 | LIBUSB_TRANSFER_FREE_BUFFER | LIBUSB_TRANSFER_FREE_TRANSFER;
187 return libusb_submit_transfer(transfer);
190 static int do_sync_intr(unsigned char *data)
195 r = libusb_interrupt_transfer(devh, EP_INTR, data, INTR_LENGTH,
198 fprintf(stderr, "intr error %d\n", r);
201 if (transferred < INTR_LENGTH) {
202 fprintf(stderr, "short read (%d)\n", r);
206 printf("recv interrupt %04x\n", *((uint16_t *) data));
210 static int sync_intr(unsigned char type)
213 unsigned char data[INTR_LENGTH];
216 r = do_sync_intr(data);
224 static int save_to_file(unsigned char *data)
230 sprintf(filename, "finger%d.pgm", img_idx++);
231 fd = fopen(filename, "w");
235 fputs("P5 384 289 255 ", fd);
236 ignore = fwrite(data + 64, 1, 384*289, fd);
238 printf("saved image to %s\n", filename);
242 static int next_state(void)
245 printf("old state: %d\n", state);
247 case STATE_AWAIT_IRQ_FINGER_REMOVED:
248 state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON;
249 r = set_mode_async(MODE_AWAIT_FINGER_ON);
251 case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON:
252 state = STATE_AWAIT_IRQ_FINGER_DETECTED;
254 case STATE_AWAIT_IRQ_FINGER_DETECTED:
255 state = STATE_AWAIT_MODE_CHANGE_CAPTURE;
256 r = set_mode_async(MODE_CAPTURE);
258 case STATE_AWAIT_MODE_CHANGE_CAPTURE:
259 state = STATE_AWAIT_IMAGE;
261 case STATE_AWAIT_IMAGE:
262 state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF;
263 r = set_mode_async(MODE_AWAIT_FINGER_OFF);
265 case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF:
266 state = STATE_AWAIT_IRQ_FINGER_REMOVED;
269 printf("unrecognised state %d\n", state);
272 fprintf(stderr, "error detected changing state\n");
276 printf("new state: %d\n", state);
280 static void LIBUSB_CALL cb_irq(struct libusb_transfer *transfer)
282 unsigned char irqtype = transfer->buffer[0];
284 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
285 fprintf(stderr, "irq transfer status %d?\n", transfer->status);
287 libusb_free_transfer(transfer);
292 printf("IRQ callback %02x\n", irqtype);
294 case STATE_AWAIT_IRQ_FINGER_DETECTED:
295 if (irqtype == 0x01) {
296 if (next_state() < 0) {
301 printf("finger-on-sensor detected in wrong state!\n");
304 case STATE_AWAIT_IRQ_FINGER_REMOVED:
305 if (irqtype == 0x02) {
306 if (next_state() < 0) {
311 printf("finger-on-sensor detected in wrong state!\n");
315 if (libusb_submit_transfer(irq_transfer) < 0)
319 static void LIBUSB_CALL cb_img(struct libusb_transfer *transfer)
321 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
322 fprintf(stderr, "img transfer status %d?\n", transfer->status);
324 libusb_free_transfer(transfer);
329 printf("Image callback\n");
330 save_to_file(imgbuf);
331 if (next_state() < 0) {
335 if (libusb_submit_transfer(img_transfer) < 0)
339 static int init_capture(void)
343 r = libusb_submit_transfer(irq_transfer);
347 r = libusb_submit_transfer(img_transfer);
349 libusb_cancel_transfer(irq_transfer);
351 if (libusb_handle_events(NULL) < 0)
356 /* start state machine */
357 state = STATE_AWAIT_IRQ_FINGER_REMOVED;
361 static int do_init(void)
363 unsigned char status;
366 r = get_hwstat(&status);
370 if (!(status & 0x80)) {
371 r = set_hwstat(status | 0x80);
374 r = get_hwstat(&status);
380 r = set_hwstat(status);
384 r = get_hwstat(&status);
395 static int alloc_transfers(void)
397 img_transfer = libusb_alloc_transfer(0);
401 irq_transfer = libusb_alloc_transfer(0);
405 libusb_fill_bulk_transfer(img_transfer, devh, EP_DATA, imgbuf,
406 sizeof(imgbuf), cb_img, NULL, 0);
407 libusb_fill_interrupt_transfer(irq_transfer, devh, EP_INTR, irqbuf,
408 sizeof(irqbuf), cb_irq, NULL, 0);
413 static void sighandler(int signum)
420 struct sigaction sigact;
423 r = libusb_init(NULL);
425 fprintf(stderr, "failed to initialise libusb\n");
429 r = find_dpfp_device();
431 fprintf(stderr, "Could not find/open device\n");
435 r = libusb_claim_interface(devh, 0);
437 fprintf(stderr, "usb_claim_interface error %d\n", r);
440 printf("claimed interface\n");
450 /* async from here onwards */
452 r = alloc_transfers();
460 sigact.sa_handler = sighandler;
461 sigemptyset(&sigact.sa_mask);
463 sigaction(SIGINT, &sigact, NULL);
464 sigaction(SIGTERM, &sigact, NULL);
465 sigaction(SIGQUIT, &sigact, NULL);
468 r = libusb_handle_events(NULL);
473 printf("shutting down...\n");
476 r = libusb_cancel_transfer(irq_transfer);
482 r = libusb_cancel_transfer(img_transfer);
487 while (irq_transfer || img_transfer)
488 if (libusb_handle_events(NULL) < 0)
497 libusb_free_transfer(img_transfer);
498 libusb_free_transfer(irq_transfer);
502 libusb_release_interface(devh, 0);
506 return r >= 0 ? r : -r;