blob: a3a76dfdb9de884e466b667bb3630cd7cc86e3f1 [file] [log] [blame]
Daniel Drake852bba42007-11-28 13:48:45 +00001/*
hjelmn@cs.unm.edu1eff2202014-01-08 23:50:34 +00002 * libusb example program to manipulate U.are.U 4000B fingerprint scanner.
Pete Batarda544e592012-05-23 18:25:01 +01003 * Copyright © 2007 Daniel Drake <dsd@gentoo.org>
Chris Dickensf69548c2020-09-15 14:15:00 -07004 * Copyright © 2016 Nathan Hjelm <hjelmn@mac.com>
5 * Copyright © 2020 Chris Dickens <christopher.a.dickens@gmail.com>
Daniel Drake852bba42007-11-28 13:48:45 +00006 *
7 * Basic image capture program only, does not consider the powerup quirks or
8 * the fact that image encryption may be enabled. Not expected to work
9 * flawlessly all of the time.
10 *
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
Chris Dickensf69548c2020-09-15 14:15:00 -070026#include <config.h>
27
Daniel Drake9cfdb492008-03-06 23:25:20 +000028#include <errno.h>
Daniel Drake852bba42007-11-28 13:48:45 +000029#include <signal.h>
Daniel Drake852bba42007-11-28 13:48:45 +000030#include <stdio.h>
31#include <stdlib.h>
Chris Dickensf69548c2020-09-15 14:15:00 -070032#include <string.h>
Daniel Drake852bba42007-11-28 13:48:45 +000033
Ludovic Rousseau38e6eb82012-10-12 23:28:51 +020034#include "libusb.h"
Daniel Drake852bba42007-11-28 13:48:45 +000035
Chris Dickensa2f1e2d2020-11-27 18:39:53 -080036#if defined(_MSC_VER)
37#define snprintf _snprintf
38#endif
39
Chris Dickensf69548c2020-09-15 14:15:00 -070040#if defined(DPFP_THREADED)
41#if defined(PLATFORM_POSIX)
Chris Dickens9d23ed22020-11-16 13:03:52 -080042#include <fcntl.h>
Chris Dickensf69548c2020-09-15 14:15:00 -070043#include <pthread.h>
44#include <semaphore.h>
Chris Dickens9d23ed22020-11-16 13:03:52 -080045#include <unistd.h>
Chris Dickensf69548c2020-09-15 14:15:00 -070046
47#define THREAD_RETURN_VALUE NULL
Chris Dickens9d23ed22020-11-16 13:03:52 -080048typedef sem_t * semaphore_t;
Chris Dickensf69548c2020-09-15 14:15:00 -070049typedef pthread_t thread_t;
50
Chris Dickens9d23ed22020-11-16 13:03:52 -080051static inline semaphore_t semaphore_create(void)
Chris Dickensf69548c2020-09-15 14:15:00 -070052{
Chris Dickens9d23ed22020-11-16 13:03:52 -080053 sem_t *semaphore;
54 char name[50];
55
56 sprintf(name, "/org.libusb.example.dpfp_threaded:%d", (int)getpid());
57 semaphore = sem_open(name, O_CREAT | O_EXCL, 0, 0);
58 if (semaphore == SEM_FAILED)
59 return NULL;
60 /* Remove semaphore so that it does not persist after process exits */
61 (void)sem_unlink(name);
62 return semaphore;
Chris Dickensf69548c2020-09-15 14:15:00 -070063}
64
Chris Dickens9d23ed22020-11-16 13:03:52 -080065static inline void semaphore_give(semaphore_t semaphore)
Chris Dickensf69548c2020-09-15 14:15:00 -070066{
67 (void)sem_post(semaphore);
68}
69
Chris Dickens9d23ed22020-11-16 13:03:52 -080070static inline void semaphore_take(semaphore_t semaphore)
Chris Dickensf69548c2020-09-15 14:15:00 -070071{
72 (void)sem_wait(semaphore);
73}
74
Chris Dickens9d23ed22020-11-16 13:03:52 -080075static inline void semaphore_destroy(semaphore_t semaphore)
Chris Dickensf69548c2020-09-15 14:15:00 -070076{
Chris Dickens9d23ed22020-11-16 13:03:52 -080077 (void)sem_close(semaphore);
Chris Dickensf69548c2020-09-15 14:15:00 -070078}
79
80static inline int thread_create(thread_t *thread,
81 void *(*thread_entry)(void *arg), void *arg)
82{
83 return pthread_create(thread, NULL, thread_entry, arg) == 0 ? 0 : -1;
84}
85
86static inline void thread_join(thread_t thread)
87{
88 (void)pthread_join(thread, NULL);
89}
90#elif defined(PLATFORM_WINDOWS)
91#define THREAD_RETURN_VALUE 0
92typedef HANDLE semaphore_t;
93typedef HANDLE thread_t;
94
95#if defined(__CYGWIN__)
96typedef DWORD thread_return_t;
97#else
98#include <process.h>
99typedef unsigned thread_return_t;
100#endif
101
Chris Dickens9d23ed22020-11-16 13:03:52 -0800102static inline semaphore_t semaphore_create(void)
Chris Dickensf69548c2020-09-15 14:15:00 -0700103{
Chris Dickens9d23ed22020-11-16 13:03:52 -0800104 return CreateSemaphore(NULL, 0, 1, NULL);
Chris Dickensf69548c2020-09-15 14:15:00 -0700105}
106
Chris Dickens9d23ed22020-11-16 13:03:52 -0800107static inline void semaphore_give(semaphore_t semaphore)
Chris Dickensf69548c2020-09-15 14:15:00 -0700108{
Chris Dickens9d23ed22020-11-16 13:03:52 -0800109 (void)ReleaseSemaphore(semaphore, 1, NULL);
Chris Dickensf69548c2020-09-15 14:15:00 -0700110}
111
Chris Dickens9d23ed22020-11-16 13:03:52 -0800112static inline void semaphore_take(semaphore_t semaphore)
Chris Dickensf69548c2020-09-15 14:15:00 -0700113{
Chris Dickens9d23ed22020-11-16 13:03:52 -0800114 (void)WaitForSingleObject(semaphore, INFINITE);
Chris Dickensf69548c2020-09-15 14:15:00 -0700115}
116
Chris Dickens9d23ed22020-11-16 13:03:52 -0800117static inline void semaphore_destroy(semaphore_t semaphore)
Chris Dickensf69548c2020-09-15 14:15:00 -0700118{
Chris Dickens9d23ed22020-11-16 13:03:52 -0800119 (void)CloseHandle(semaphore);
Chris Dickensf69548c2020-09-15 14:15:00 -0700120}
121
122static inline int thread_create(thread_t *thread,
123 thread_return_t (__stdcall *thread_entry)(void *arg), void *arg)
124{
125#if defined(__CYGWIN__)
126 *thread = CreateThread(NULL, 0, thread_entry, arg, 0, NULL);
127#else
128 *thread = (HANDLE)_beginthreadex(NULL, 0, thread_entry, arg, 0, NULL);
129#endif
130 return *thread != NULL ? 0 : -1;
131}
132
133static inline void thread_join(thread_t thread)
134{
135 (void)WaitForSingleObject(thread, INFINITE);
136 (void)CloseHandle(thread);
137}
138#endif
139#endif
140
Daniel Drakedbb3fd82008-01-04 00:54:57 +0000141#define EP_INTR (1 | LIBUSB_ENDPOINT_IN)
142#define EP_DATA (2 | LIBUSB_ENDPOINT_IN)
Daniel Drakeead09cd2008-03-15 16:35:12 +0000143#define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN)
144#define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT)
Daniel Drake852bba42007-11-28 13:48:45 +0000145#define USB_RQ 0x04
146#define INTR_LENGTH 64
147
148enum {
149 MODE_INIT = 0x00,
150 MODE_AWAIT_FINGER_ON = 0x10,
151 MODE_AWAIT_FINGER_OFF = 0x12,
152 MODE_CAPTURE = 0x20,
153 MODE_SHUT_UP = 0x30,
154 MODE_READY = 0x80,
155};
156
157static int next_state(void);
Daniel Drake852bba42007-11-28 13:48:45 +0000158
159enum {
160 STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON = 1,
161 STATE_AWAIT_IRQ_FINGER_DETECTED,
162 STATE_AWAIT_MODE_CHANGE_CAPTURE,
163 STATE_AWAIT_IMAGE,
164 STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF,
165 STATE_AWAIT_IRQ_FINGER_REMOVED,
166};
167
168static int state = 0;
Chris Dickensf69548c2020-09-15 14:15:00 -0700169static libusb_device_handle *devh = NULL;
Daniel Drake852bba42007-11-28 13:48:45 +0000170static unsigned char imgbuf[0x1b340];
171static unsigned char irqbuf[INTR_LENGTH];
Daniel Drake66348c92008-03-09 00:58:09 +0000172static struct libusb_transfer *img_transfer = NULL;
173static struct libusb_transfer *irq_transfer = NULL;
Daniel Drake852bba42007-11-28 13:48:45 +0000174static int img_idx = 0;
Chris Dickensf69548c2020-09-15 14:15:00 -0700175static volatile sig_atomic_t do_exit = 0;
176
177#if defined(DPFP_THREADED)
178static semaphore_t exit_semaphore;
179static thread_t poll_thread;
180#endif
181
182static void request_exit(sig_atomic_t code)
183{
184 do_exit = code;
185#if defined(DPFP_THREADED)
Chris Dickens9d23ed22020-11-16 13:03:52 -0800186 semaphore_give(exit_semaphore);
Chris Dickensf69548c2020-09-15 14:15:00 -0700187#endif
188}
189
190#if defined(DPFP_THREADED)
191#if defined(PLATFORM_POSIX)
192static void *poll_thread_main(void *arg)
193#elif defined(PLATFORM_WINDOWS)
194static thread_return_t __stdcall poll_thread_main(void *arg)
195#endif
196{
197 (void)arg;
198
199 printf("poll thread running\n");
200
201 while (!do_exit) {
202 struct timeval tv = { 1, 0 };
203 int r;
204
205 r = libusb_handle_events_timeout(NULL, &tv);
206 if (r < 0) {
207 request_exit(2);
208 break;
209 }
210 }
211
212 printf("poll thread shutting down\n");
213 return THREAD_RETURN_VALUE;
214}
215#endif
Daniel Drake852bba42007-11-28 13:48:45 +0000216
Daniel Drake9cfdb492008-03-06 23:25:20 +0000217static int find_dpfp_device(void)
Daniel Drake852bba42007-11-28 13:48:45 +0000218{
Daniel Drake1df713d2008-06-24 23:01:51 -0500219 devh = libusb_open_device_with_vid_pid(NULL, 0x05ba, 0x000a);
Chris Dickensf69548c2020-09-15 14:15:00 -0700220 return devh ? 0 : -ENODEV;
Daniel Drake852bba42007-11-28 13:48:45 +0000221}
222
223static int print_f0_data(void)
224{
225 unsigned char data[0x10];
Chris Dickensf69548c2020-09-15 14:15:00 -0700226 size_t i;
Daniel Drake852bba42007-11-28 13:48:45 +0000227 int r;
Daniel Drake852bba42007-11-28 13:48:45 +0000228
Daniel Drake66348c92008-03-09 00:58:09 +0000229 r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0xf0, 0, data,
230 sizeof(data), 0);
Daniel Drake852bba42007-11-28 13:48:45 +0000231 if (r < 0) {
232 fprintf(stderr, "F0 error %d\n", r);
233 return r;
234 }
Chris Dickensf69548c2020-09-15 14:15:00 -0700235 if (r < (int)sizeof(data)) {
Daniel Drake852bba42007-11-28 13:48:45 +0000236 fprintf(stderr, "short read (%d)\n", r);
237 return -1;
238 }
239
240 printf("F0 data:");
241 for (i = 0; i < sizeof(data); i++)
Chris Dickensf69548c2020-09-15 14:15:00 -0700242 printf(" %02x", data[i]);
Daniel Drake852bba42007-11-28 13:48:45 +0000243 printf("\n");
244 return 0;
245}
246
247static int get_hwstat(unsigned char *status)
248{
Daniel Drake852bba42007-11-28 13:48:45 +0000249 int r;
250
Daniel Drake66348c92008-03-09 00:58:09 +0000251 r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0x07, 0, status, 1, 0);
Daniel Drake852bba42007-11-28 13:48:45 +0000252 if (r < 0) {
253 fprintf(stderr, "read hwstat error %d\n", r);
254 return r;
255 }
Chris Dickensf69548c2020-09-15 14:15:00 -0700256 if (r < 1) {
Daniel Drake852bba42007-11-28 13:48:45 +0000257 fprintf(stderr, "short read (%d)\n", r);
258 return -1;
259 }
260
261 printf("hwstat reads %02x\n", *status);
262 return 0;
263}
264
265static int set_hwstat(unsigned char data)
266{
267 int r;
Daniel Drake852bba42007-11-28 13:48:45 +0000268
269 printf("set hwstat to %02x\n", data);
Daniel Drake66348c92008-03-09 00:58:09 +0000270 r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x07, 0, &data, 1, 0);
Daniel Drake852bba42007-11-28 13:48:45 +0000271 if (r < 0) {
272 fprintf(stderr, "set hwstat error %d\n", r);
273 return r;
274 }
Chris Dickensf69548c2020-09-15 14:15:00 -0700275 if (r < 1) {
276 fprintf(stderr, "short write (%d)\n", r);
Daniel Drake852bba42007-11-28 13:48:45 +0000277 return -1;
278 }
279
280 return 0;
281}
282
283static int set_mode(unsigned char data)
284{
285 int r;
Daniel Drake852bba42007-11-28 13:48:45 +0000286
Chris Dickensf69548c2020-09-15 14:15:00 -0700287 printf("set mode %02x\n", data);
Daniel Drake66348c92008-03-09 00:58:09 +0000288 r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x4e, 0, &data, 1, 0);
Daniel Drake852bba42007-11-28 13:48:45 +0000289 if (r < 0) {
290 fprintf(stderr, "set mode error %d\n", r);
291 return r;
292 }
Chris Dickensf69548c2020-09-15 14:15:00 -0700293 if (r < 1) {
294 fprintf(stderr, "short write (%d)\n", r);
Daniel Drake852bba42007-11-28 13:48:45 +0000295 return -1;
296 }
297
298 return 0;
299}
300
Pete Batard29f9f9e2010-08-13 11:59:49 +0100301static void LIBUSB_CALL cb_mode_changed(struct libusb_transfer *transfer)
Daniel Drake852bba42007-11-28 13:48:45 +0000302{
Daniel Drake66348c92008-03-09 00:58:09 +0000303 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
304 fprintf(stderr, "mode change transfer not completed!\n");
Chris Dickensf69548c2020-09-15 14:15:00 -0700305 request_exit(2);
Daniel Drake852bba42007-11-28 13:48:45 +0000306 }
307
Daniel Drake7c5ea952008-03-10 11:29:24 +0000308 printf("async cb_mode_changed length=%d actual_length=%d\n",
309 transfer->length, transfer->actual_length);
Daniel Drake852bba42007-11-28 13:48:45 +0000310 if (next_state() < 0)
Chris Dickensf69548c2020-09-15 14:15:00 -0700311 request_exit(2);
Daniel Drake852bba42007-11-28 13:48:45 +0000312}
313
314static int set_mode_async(unsigned char data)
315{
Chris Dickensf69548c2020-09-15 14:15:00 -0700316 unsigned char *buf = malloc(LIBUSB_CONTROL_SETUP_SIZE + 1);
Daniel Drake66348c92008-03-09 00:58:09 +0000317 struct libusb_transfer *transfer;
Daniel Drake852bba42007-11-28 13:48:45 +0000318
Daniel Drake66348c92008-03-09 00:58:09 +0000319 if (!buf)
320 return -ENOMEM;
Pete Batarda636df42010-05-12 21:46:31 -0300321
Daniel Drake211f80c2008-03-25 16:24:30 +0000322 transfer = libusb_alloc_transfer(0);
Daniel Drake66348c92008-03-09 00:58:09 +0000323 if (!transfer) {
324 free(buf);
325 return -ENOMEM;
Daniel Drake852bba42007-11-28 13:48:45 +0000326 }
327
Daniel Drake66348c92008-03-09 00:58:09 +0000328 printf("async set mode %02x\n", data);
329 libusb_fill_control_setup(buf, CTRL_OUT, USB_RQ, 0x4e, 0, 1);
330 buf[LIBUSB_CONTROL_SETUP_SIZE] = data;
Daniel Drakeaae05f62008-03-10 11:32:15 +0000331 libusb_fill_control_transfer(transfer, devh, buf, cb_mode_changed, NULL,
332 1000);
Daniel Drake66348c92008-03-09 00:58:09 +0000333
Daniel Drakede4c5342008-03-10 18:38:15 +0000334 transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK
335 | LIBUSB_TRANSFER_FREE_BUFFER | LIBUSB_TRANSFER_FREE_TRANSFER;
Daniel Drake66348c92008-03-09 00:58:09 +0000336 return libusb_submit_transfer(transfer);
Daniel Drake852bba42007-11-28 13:48:45 +0000337}
338
339static int do_sync_intr(unsigned char *data)
340{
Daniel Drake852bba42007-11-28 13:48:45 +0000341 int r;
342 int transferred;
343
Daniel Drake66348c92008-03-09 00:58:09 +0000344 r = libusb_interrupt_transfer(devh, EP_INTR, data, INTR_LENGTH,
345 &transferred, 1000);
Daniel Drake852bba42007-11-28 13:48:45 +0000346 if (r < 0) {
347 fprintf(stderr, "intr error %d\n", r);
348 return r;
349 }
350 if (transferred < INTR_LENGTH) {
351 fprintf(stderr, "short read (%d)\n", r);
352 return -1;
353 }
354
Chris Dickensf69548c2020-09-15 14:15:00 -0700355 printf("recv interrupt %04x\n", *((uint16_t *)data));
Daniel Drake852bba42007-11-28 13:48:45 +0000356 return 0;
357}
358
359static int sync_intr(unsigned char type)
Pete Batarda636df42010-05-12 21:46:31 -0300360{
Daniel Drake852bba42007-11-28 13:48:45 +0000361 int r;
362 unsigned char data[INTR_LENGTH];
363
364 while (1) {
365 r = do_sync_intr(data);
366 if (r < 0)
367 return r;
368 if (data[0] == type)
369 return 0;
370 }
371}
372
373static int save_to_file(unsigned char *data)
374{
Chris Dickensf69548c2020-09-15 14:15:00 -0700375 FILE *f;
Daniel Drake852bba42007-11-28 13:48:45 +0000376 char filename[64];
377
Peter Stuged346cd92012-04-07 05:20:36 +0200378 snprintf(filename, sizeof(filename), "finger%d.pgm", img_idx++);
Chris Dickensf69548c2020-09-15 14:15:00 -0700379 f = fopen(filename, "w");
380 if (!f)
Daniel Drake852bba42007-11-28 13:48:45 +0000381 return -1;
382
Chris Dickensf69548c2020-09-15 14:15:00 -0700383 fputs("P5 384 289 255 ", f);
384 (void)fwrite(data + 64, 1, 384*289, f);
385 fclose(f);
Daniel Drake852bba42007-11-28 13:48:45 +0000386 printf("saved image to %s\n", filename);
387 return 0;
388}
389
390static int next_state(void)
391{
392 int r = 0;
Chris Dickensf69548c2020-09-15 14:15:00 -0700393
Daniel Drake852bba42007-11-28 13:48:45 +0000394 printf("old state: %d\n", state);
395 switch (state) {
396 case STATE_AWAIT_IRQ_FINGER_REMOVED:
397 state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON;
398 r = set_mode_async(MODE_AWAIT_FINGER_ON);
399 break;
400 case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_ON:
401 state = STATE_AWAIT_IRQ_FINGER_DETECTED;
402 break;
403 case STATE_AWAIT_IRQ_FINGER_DETECTED:
404 state = STATE_AWAIT_MODE_CHANGE_CAPTURE;
405 r = set_mode_async(MODE_CAPTURE);
406 break;
407 case STATE_AWAIT_MODE_CHANGE_CAPTURE:
408 state = STATE_AWAIT_IMAGE;
409 break;
410 case STATE_AWAIT_IMAGE:
411 state = STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF;
412 r = set_mode_async(MODE_AWAIT_FINGER_OFF);
413 break;
414 case STATE_AWAIT_MODE_CHANGE_AWAIT_FINGER_OFF:
415 state = STATE_AWAIT_IRQ_FINGER_REMOVED;
416 break;
417 default:
418 printf("unrecognised state %d\n", state);
419 }
420 if (r < 0) {
Daniel Drakec0c94322008-03-13 12:36:56 +0000421 fprintf(stderr, "error detected changing state\n");
Daniel Drake852bba42007-11-28 13:48:45 +0000422 return r;
423 }
424
425 printf("new state: %d\n", state);
426 return 0;
427}
428
Pete Batard29f9f9e2010-08-13 11:59:49 +0100429static void LIBUSB_CALL cb_irq(struct libusb_transfer *transfer)
Daniel Drake852bba42007-11-28 13:48:45 +0000430{
Daniel Drake66348c92008-03-09 00:58:09 +0000431 unsigned char irqtype = transfer->buffer[0];
Daniel Drake852bba42007-11-28 13:48:45 +0000432
Daniel Drake66348c92008-03-09 00:58:09 +0000433 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
434 fprintf(stderr, "irq transfer status %d?\n", transfer->status);
Chris Dickensf69548c2020-09-15 14:15:00 -0700435 goto err_free_transfer;
Daniel Drake852bba42007-11-28 13:48:45 +0000436 }
437
438 printf("IRQ callback %02x\n", irqtype);
439 switch (state) {
440 case STATE_AWAIT_IRQ_FINGER_DETECTED:
441 if (irqtype == 0x01) {
Chris Dickensf69548c2020-09-15 14:15:00 -0700442 if (next_state() < 0)
443 goto err_free_transfer;
Daniel Drake852bba42007-11-28 13:48:45 +0000444 } else {
445 printf("finger-on-sensor detected in wrong state!\n");
446 }
447 break;
448 case STATE_AWAIT_IRQ_FINGER_REMOVED:
449 if (irqtype == 0x02) {
Chris Dickensf69548c2020-09-15 14:15:00 -0700450 if (next_state() < 0)
451 goto err_free_transfer;
Daniel Drake852bba42007-11-28 13:48:45 +0000452 } else {
453 printf("finger-on-sensor detected in wrong state!\n");
454 }
455 break;
456 }
Daniel Drake7ac0a402008-04-27 23:27:04 +0100457 if (libusb_submit_transfer(irq_transfer) < 0)
Chris Dickensf69548c2020-09-15 14:15:00 -0700458 goto err_free_transfer;
459
460 return;
461
462err_free_transfer:
463 libusb_free_transfer(transfer);
464 irq_transfer = NULL;
465 request_exit(2);
Daniel Drake852bba42007-11-28 13:48:45 +0000466}
467
Pete Batard29f9f9e2010-08-13 11:59:49 +0100468static void LIBUSB_CALL cb_img(struct libusb_transfer *transfer)
Daniel Drake852bba42007-11-28 13:48:45 +0000469{
Daniel Drake66348c92008-03-09 00:58:09 +0000470 if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
471 fprintf(stderr, "img transfer status %d?\n", transfer->status);
Chris Dickensf69548c2020-09-15 14:15:00 -0700472 goto err_free_transfer;
Daniel Drake852bba42007-11-28 13:48:45 +0000473 }
474
475 printf("Image callback\n");
476 save_to_file(imgbuf);
Chris Dickensf69548c2020-09-15 14:15:00 -0700477 if (next_state() < 0)
478 goto err_free_transfer;
479
Daniel Drake7ac0a402008-04-27 23:27:04 +0100480 if (libusb_submit_transfer(img_transfer) < 0)
Chris Dickensf69548c2020-09-15 14:15:00 -0700481 goto err_free_transfer;
482
483 return;
484
485err_free_transfer:
486 libusb_free_transfer(transfer);
487 img_transfer = NULL;
488 request_exit(2);
Daniel Drake852bba42007-11-28 13:48:45 +0000489}
490
Daniel Drake852bba42007-11-28 13:48:45 +0000491static int init_capture(void)
492{
493 int r;
494
Daniel Drake7ac0a402008-04-27 23:27:04 +0100495 r = libusb_submit_transfer(irq_transfer);
Daniel Drake852bba42007-11-28 13:48:45 +0000496 if (r < 0)
497 return r;
498
Daniel Drake7ac0a402008-04-27 23:27:04 +0100499 r = libusb_submit_transfer(img_transfer);
Daniel Drake852bba42007-11-28 13:48:45 +0000500 if (r < 0) {
Daniel Drake7ac0a402008-04-27 23:27:04 +0100501 libusb_cancel_transfer(irq_transfer);
502 while (irq_transfer)
Daniel Drake1df713d2008-06-24 23:01:51 -0500503 if (libusb_handle_events(NULL) < 0)
Daniel Drake7ac0a402008-04-27 23:27:04 +0100504 break;
Daniel Drake852bba42007-11-28 13:48:45 +0000505 return r;
506 }
507
508 /* start state machine */
509 state = STATE_AWAIT_IRQ_FINGER_REMOVED;
510 return next_state();
511}
512
513static int do_init(void)
514{
515 unsigned char status;
516 int r;
517
518 r = get_hwstat(&status);
519 if (r < 0)
520 return r;
521
522 if (!(status & 0x80)) {
523 r = set_hwstat(status | 0x80);
524 if (r < 0)
525 return r;
526 r = get_hwstat(&status);
527 if (r < 0)
528 return r;
529 }
530
531 status &= ~0x80;
532 r = set_hwstat(status);
533 if (r < 0)
534 return r;
535
536 r = get_hwstat(&status);
537 if (r < 0)
538 return r;
539
540 r = sync_intr(0x56);
541 if (r < 0)
542 return r;
543
544 return 0;
545}
546
Daniel Drake66348c92008-03-09 00:58:09 +0000547static int alloc_transfers(void)
548{
Daniel Drake211f80c2008-03-25 16:24:30 +0000549 img_transfer = libusb_alloc_transfer(0);
Daniel Drake66348c92008-03-09 00:58:09 +0000550 if (!img_transfer)
551 return -ENOMEM;
Pete Batarda636df42010-05-12 21:46:31 -0300552
Daniel Drake211f80c2008-03-25 16:24:30 +0000553 irq_transfer = libusb_alloc_transfer(0);
Daniel Drake66348c92008-03-09 00:58:09 +0000554 if (!irq_transfer)
555 return -ENOMEM;
556
557 libusb_fill_bulk_transfer(img_transfer, devh, EP_DATA, imgbuf,
558 sizeof(imgbuf), cb_img, NULL, 0);
559 libusb_fill_interrupt_transfer(irq_transfer, devh, EP_INTR, irqbuf,
560 sizeof(irqbuf), cb_irq, NULL, 0);
561
562 return 0;
563}
564
Daniel Drake852bba42007-11-28 13:48:45 +0000565static void sighandler(int signum)
566{
Sean McBride830a9cb2017-12-27 22:32:15 -0500567 (void)signum;
568
Chris Dickensf69548c2020-09-15 14:15:00 -0700569 request_exit(1);
570}
571
572static void setup_signals(void)
573{
574#if defined(PLATFORM_POSIX)
575 struct sigaction sigact;
576
577 sigact.sa_handler = sighandler;
578 sigemptyset(&sigact.sa_mask);
579 sigact.sa_flags = 0;
580 (void)sigaction(SIGINT, &sigact, NULL);
581 (void)sigaction(SIGTERM, &sigact, NULL);
582 (void)sigaction(SIGQUIT, &sigact, NULL);
583#else
584 (void)signal(SIGINT, sighandler);
585 (void)signal(SIGTERM, sighandler);
586#endif
Daniel Drake852bba42007-11-28 13:48:45 +0000587}
588
589int main(void)
590{
Sean McBride0b3d4c62017-12-27 23:53:27 -0500591 int r;
Daniel Drake852bba42007-11-28 13:48:45 +0000592
Daniel Drake1df713d2008-06-24 23:01:51 -0500593 r = libusb_init(NULL);
Daniel Drake852bba42007-11-28 13:48:45 +0000594 if (r < 0) {
Chris Dickensf69548c2020-09-15 14:15:00 -0700595 fprintf(stderr, "failed to initialise libusb %d - %s\n", r, libusb_strerror(r));
Daniel Drake852bba42007-11-28 13:48:45 +0000596 exit(1);
597 }
598
Daniel Drake9cfdb492008-03-06 23:25:20 +0000599 r = find_dpfp_device();
600 if (r < 0) {
601 fprintf(stderr, "Could not find/open device\n");
Daniel Drake852bba42007-11-28 13:48:45 +0000602 goto out;
603 }
Daniel Drake852bba42007-11-28 13:48:45 +0000604
Daniel Drakede53d972008-01-04 01:17:06 +0000605 r = libusb_claim_interface(devh, 0);
Daniel Drake852bba42007-11-28 13:48:45 +0000606 if (r < 0) {
Chris Dickensf69548c2020-09-15 14:15:00 -0700607 fprintf(stderr, "claim interface error %d - %s\n", r, libusb_strerror(r));
Daniel Drake852bba42007-11-28 13:48:45 +0000608 goto out;
609 }
610 printf("claimed interface\n");
611
612 r = print_f0_data();
613 if (r < 0)
614 goto out_release;
615
616 r = do_init();
617 if (r < 0)
618 goto out_deinit;
619
620 /* async from here onwards */
Chris Dickensf69548c2020-09-15 14:15:00 -0700621 setup_signals();
622
Daniel Drake66348c92008-03-09 00:58:09 +0000623 r = alloc_transfers();
624 if (r < 0)
625 goto out_deinit;
626
Chris Dickensf69548c2020-09-15 14:15:00 -0700627#if defined(DPFP_THREADED)
Chris Dickens9d23ed22020-11-16 13:03:52 -0800628 exit_semaphore = semaphore_create();
629 if (!exit_semaphore) {
Chris Dickensf69548c2020-09-15 14:15:00 -0700630 fprintf(stderr, "failed to initialise semaphore\n");
631 goto out_deinit;
632 }
633
634 r = thread_create(&poll_thread, poll_thread_main, NULL);
635 if (r) {
Chris Dickens9d23ed22020-11-16 13:03:52 -0800636 semaphore_destroy(exit_semaphore);
Chris Dickensf69548c2020-09-15 14:15:00 -0700637 goto out_deinit;
638 }
639
640 r = init_capture();
641 if (r < 0)
642 request_exit(2);
643
644 while (!do_exit)
Chris Dickens9d23ed22020-11-16 13:03:52 -0800645 semaphore_take(exit_semaphore);
Chris Dickensf69548c2020-09-15 14:15:00 -0700646#else
Daniel Drake852bba42007-11-28 13:48:45 +0000647 r = init_capture();
648 if (r < 0)
649 goto out_deinit;
650
Daniel Drake852bba42007-11-28 13:48:45 +0000651 while (!do_exit) {
Daniel Drake1df713d2008-06-24 23:01:51 -0500652 r = libusb_handle_events(NULL);
Daniel Drake852bba42007-11-28 13:48:45 +0000653 if (r < 0)
Chris Dickensf69548c2020-09-15 14:15:00 -0700654 request_exit(2);
Daniel Drake852bba42007-11-28 13:48:45 +0000655 }
Chris Dickensf69548c2020-09-15 14:15:00 -0700656#endif
Daniel Drake852bba42007-11-28 13:48:45 +0000657
658 printf("shutting down...\n");
Pete Batarda636df42010-05-12 21:46:31 -0300659
Chris Dickensf69548c2020-09-15 14:15:00 -0700660#if defined(DPFP_THREADED)
661 thread_join(poll_thread);
Chris Dickens9d23ed22020-11-16 13:03:52 -0800662 semaphore_destroy(exit_semaphore);
Chris Dickensf69548c2020-09-15 14:15:00 -0700663#endif
Daniel Drake852bba42007-11-28 13:48:45 +0000664
Daniel Drakefec7c842008-05-11 20:31:58 +0100665 if (img_transfer) {
666 r = libusb_cancel_transfer(img_transfer);
667 if (r < 0)
Chris Dickensf69548c2020-09-15 14:15:00 -0700668 fprintf(stderr, "failed to cancel transfer %d - %s\n", r, libusb_strerror(r));
Daniel Drakefec7c842008-05-11 20:31:58 +0100669 }
Pete Batarda636df42010-05-12 21:46:31 -0300670
Chris Dickensf69548c2020-09-15 14:15:00 -0700671 if (irq_transfer) {
672 r = libusb_cancel_transfer(irq_transfer);
673 if (r < 0)
674 fprintf(stderr, "failed to cancel transfer %d - %s\n", r, libusb_strerror(r));
675 }
676
677 while (img_transfer || irq_transfer) {
Daniel Drake1df713d2008-06-24 23:01:51 -0500678 if (libusb_handle_events(NULL) < 0)
Daniel Drake7ac0a402008-04-27 23:27:04 +0100679 break;
Chris Dickensf69548c2020-09-15 14:15:00 -0700680 }
Pete Batarda636df42010-05-12 21:46:31 -0300681
Daniel Drake852bba42007-11-28 13:48:45 +0000682 if (do_exit == 1)
683 r = 0;
684 else
685 r = 1;
686
687out_deinit:
Chris Dickensf69548c2020-09-15 14:15:00 -0700688 if (img_transfer)
689 libusb_free_transfer(img_transfer);
690 if (irq_transfer)
691 libusb_free_transfer(irq_transfer);
Daniel Drake852bba42007-11-28 13:48:45 +0000692 set_mode(0);
693 set_hwstat(0x80);
694out_release:
Daniel Drakede53d972008-01-04 01:17:06 +0000695 libusb_release_interface(devh, 0);
Daniel Drake852bba42007-11-28 13:48:45 +0000696out:
Daniel Drakede53d972008-01-04 01:17:06 +0000697 libusb_close(devh);
Daniel Drake1df713d2008-06-24 23:01:51 -0500698 libusb_exit(NULL);
Daniel Drake852bba42007-11-28 13:48:45 +0000699 return r >= 0 ? r : -r;
700}