Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 1 | /* |
hjelmn@cs.unm.edu | 1eff220 | 2014-01-08 23:50:34 +0000 | [diff] [blame] | 2 | * libusb example program to manipulate U.are.U 4000B fingerprint scanner. |
Pete Batard | a544e59 | 2012-05-23 18:25:01 +0100 | [diff] [blame] | 3 | * Copyright © 2007 Daniel Drake <dsd@gentoo.org> |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 4 | * Copyright © 2016 Nathan Hjelm <hjelmn@mac.com> |
| 5 | * Copyright © 2020 Chris Dickens <christopher.a.dickens@gmail.com> |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 6 | * |
| 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 Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 26 | #include <config.h> |
| 27 | |
Daniel Drake | 9cfdb49 | 2008-03-06 23:25:20 +0000 | [diff] [blame] | 28 | #include <errno.h> |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 29 | #include <signal.h> |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 32 | #include <string.h> |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 33 | |
Ludovic Rousseau | 38e6eb8 | 2012-10-12 23:28:51 +0200 | [diff] [blame] | 34 | #include "libusb.h" |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 35 | |
Chris Dickens | a2f1e2d | 2020-11-27 18:39:53 -0800 | [diff] [blame] | 36 | #if defined(_MSC_VER) |
| 37 | #define snprintf _snprintf |
| 38 | #endif |
| 39 | |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 40 | #if defined(DPFP_THREADED) |
| 41 | #if defined(PLATFORM_POSIX) |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 42 | #include <fcntl.h> |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 43 | #include <pthread.h> |
| 44 | #include <semaphore.h> |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 45 | #include <unistd.h> |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 46 | |
| 47 | #define THREAD_RETURN_VALUE NULL |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 48 | typedef sem_t * semaphore_t; |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 49 | typedef pthread_t thread_t; |
| 50 | |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 51 | static inline semaphore_t semaphore_create(void) |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 52 | { |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 53 | 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 Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 65 | static inline void semaphore_give(semaphore_t semaphore) |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 66 | { |
| 67 | (void)sem_post(semaphore); |
| 68 | } |
| 69 | |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 70 | static inline void semaphore_take(semaphore_t semaphore) |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 71 | { |
| 72 | (void)sem_wait(semaphore); |
| 73 | } |
| 74 | |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 75 | static inline void semaphore_destroy(semaphore_t semaphore) |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 76 | { |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 77 | (void)sem_close(semaphore); |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static 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 | |
| 86 | static 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 |
| 92 | typedef HANDLE semaphore_t; |
| 93 | typedef HANDLE thread_t; |
| 94 | |
| 95 | #if defined(__CYGWIN__) |
| 96 | typedef DWORD thread_return_t; |
| 97 | #else |
| 98 | #include <process.h> |
| 99 | typedef unsigned thread_return_t; |
| 100 | #endif |
| 101 | |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 102 | static inline semaphore_t semaphore_create(void) |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 103 | { |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 104 | return CreateSemaphore(NULL, 0, 1, NULL); |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 107 | static inline void semaphore_give(semaphore_t semaphore) |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 108 | { |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 109 | (void)ReleaseSemaphore(semaphore, 1, NULL); |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 112 | static inline void semaphore_take(semaphore_t semaphore) |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 113 | { |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 114 | (void)WaitForSingleObject(semaphore, INFINITE); |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 117 | static inline void semaphore_destroy(semaphore_t semaphore) |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 118 | { |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 119 | (void)CloseHandle(semaphore); |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static 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 | |
| 133 | static inline void thread_join(thread_t thread) |
| 134 | { |
| 135 | (void)WaitForSingleObject(thread, INFINITE); |
| 136 | (void)CloseHandle(thread); |
| 137 | } |
| 138 | #endif |
| 139 | #endif |
| 140 | |
Daniel Drake | dbb3fd8 | 2008-01-04 00:54:57 +0000 | [diff] [blame] | 141 | #define EP_INTR (1 | LIBUSB_ENDPOINT_IN) |
| 142 | #define EP_DATA (2 | LIBUSB_ENDPOINT_IN) |
Daniel Drake | ead09cd | 2008-03-15 16:35:12 +0000 | [diff] [blame] | 143 | #define CTRL_IN (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_IN) |
| 144 | #define CTRL_OUT (LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT) |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 145 | #define USB_RQ 0x04 |
| 146 | #define INTR_LENGTH 64 |
| 147 | |
| 148 | enum { |
| 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 | |
| 157 | static int next_state(void); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 158 | |
| 159 | enum { |
| 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 | |
| 168 | static int state = 0; |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 169 | static libusb_device_handle *devh = NULL; |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 170 | static unsigned char imgbuf[0x1b340]; |
| 171 | static unsigned char irqbuf[INTR_LENGTH]; |
Daniel Drake | 66348c9 | 2008-03-09 00:58:09 +0000 | [diff] [blame] | 172 | static struct libusb_transfer *img_transfer = NULL; |
| 173 | static struct libusb_transfer *irq_transfer = NULL; |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 174 | static int img_idx = 0; |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 175 | static volatile sig_atomic_t do_exit = 0; |
| 176 | |
| 177 | #if defined(DPFP_THREADED) |
| 178 | static semaphore_t exit_semaphore; |
| 179 | static thread_t poll_thread; |
| 180 | #endif |
| 181 | |
| 182 | static void request_exit(sig_atomic_t code) |
| 183 | { |
| 184 | do_exit = code; |
| 185 | #if defined(DPFP_THREADED) |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 186 | semaphore_give(exit_semaphore); |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 187 | #endif |
| 188 | } |
| 189 | |
| 190 | #if defined(DPFP_THREADED) |
| 191 | #if defined(PLATFORM_POSIX) |
| 192 | static void *poll_thread_main(void *arg) |
| 193 | #elif defined(PLATFORM_WINDOWS) |
| 194 | static 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 Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 216 | |
Daniel Drake | 9cfdb49 | 2008-03-06 23:25:20 +0000 | [diff] [blame] | 217 | static int find_dpfp_device(void) |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 218 | { |
Daniel Drake | 1df713d | 2008-06-24 23:01:51 -0500 | [diff] [blame] | 219 | devh = libusb_open_device_with_vid_pid(NULL, 0x05ba, 0x000a); |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 220 | return devh ? 0 : -ENODEV; |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static int print_f0_data(void) |
| 224 | { |
| 225 | unsigned char data[0x10]; |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 226 | size_t i; |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 227 | int r; |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 228 | |
Daniel Drake | 66348c9 | 2008-03-09 00:58:09 +0000 | [diff] [blame] | 229 | r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0xf0, 0, data, |
| 230 | sizeof(data), 0); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 231 | if (r < 0) { |
| 232 | fprintf(stderr, "F0 error %d\n", r); |
| 233 | return r; |
| 234 | } |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 235 | if (r < (int)sizeof(data)) { |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 236 | 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 Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 242 | printf(" %02x", data[i]); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 243 | printf("\n"); |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static int get_hwstat(unsigned char *status) |
| 248 | { |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 249 | int r; |
| 250 | |
Daniel Drake | 66348c9 | 2008-03-09 00:58:09 +0000 | [diff] [blame] | 251 | r = libusb_control_transfer(devh, CTRL_IN, USB_RQ, 0x07, 0, status, 1, 0); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 252 | if (r < 0) { |
| 253 | fprintf(stderr, "read hwstat error %d\n", r); |
| 254 | return r; |
| 255 | } |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 256 | if (r < 1) { |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 257 | fprintf(stderr, "short read (%d)\n", r); |
| 258 | return -1; |
| 259 | } |
| 260 | |
| 261 | printf("hwstat reads %02x\n", *status); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static int set_hwstat(unsigned char data) |
| 266 | { |
| 267 | int r; |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 268 | |
| 269 | printf("set hwstat to %02x\n", data); |
Daniel Drake | 66348c9 | 2008-03-09 00:58:09 +0000 | [diff] [blame] | 270 | r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x07, 0, &data, 1, 0); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 271 | if (r < 0) { |
| 272 | fprintf(stderr, "set hwstat error %d\n", r); |
| 273 | return r; |
| 274 | } |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 275 | if (r < 1) { |
| 276 | fprintf(stderr, "short write (%d)\n", r); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 277 | return -1; |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static int set_mode(unsigned char data) |
| 284 | { |
| 285 | int r; |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 286 | |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 287 | printf("set mode %02x\n", data); |
Daniel Drake | 66348c9 | 2008-03-09 00:58:09 +0000 | [diff] [blame] | 288 | r = libusb_control_transfer(devh, CTRL_OUT, USB_RQ, 0x4e, 0, &data, 1, 0); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 289 | if (r < 0) { |
| 290 | fprintf(stderr, "set mode error %d\n", r); |
| 291 | return r; |
| 292 | } |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 293 | if (r < 1) { |
| 294 | fprintf(stderr, "short write (%d)\n", r); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 295 | return -1; |
| 296 | } |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
Pete Batard | 29f9f9e | 2010-08-13 11:59:49 +0100 | [diff] [blame] | 301 | static void LIBUSB_CALL cb_mode_changed(struct libusb_transfer *transfer) |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 302 | { |
Daniel Drake | 66348c9 | 2008-03-09 00:58:09 +0000 | [diff] [blame] | 303 | if (transfer->status != LIBUSB_TRANSFER_COMPLETED) { |
| 304 | fprintf(stderr, "mode change transfer not completed!\n"); |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 305 | request_exit(2); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Daniel Drake | 7c5ea95 | 2008-03-10 11:29:24 +0000 | [diff] [blame] | 308 | printf("async cb_mode_changed length=%d actual_length=%d\n", |
| 309 | transfer->length, transfer->actual_length); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 310 | if (next_state() < 0) |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 311 | request_exit(2); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | static int set_mode_async(unsigned char data) |
| 315 | { |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 316 | unsigned char *buf = malloc(LIBUSB_CONTROL_SETUP_SIZE + 1); |
Daniel Drake | 66348c9 | 2008-03-09 00:58:09 +0000 | [diff] [blame] | 317 | struct libusb_transfer *transfer; |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 318 | |
Daniel Drake | 66348c9 | 2008-03-09 00:58:09 +0000 | [diff] [blame] | 319 | if (!buf) |
| 320 | return -ENOMEM; |
Pete Batard | a636df4 | 2010-05-12 21:46:31 -0300 | [diff] [blame] | 321 | |
Daniel Drake | 211f80c | 2008-03-25 16:24:30 +0000 | [diff] [blame] | 322 | transfer = libusb_alloc_transfer(0); |
Daniel Drake | 66348c9 | 2008-03-09 00:58:09 +0000 | [diff] [blame] | 323 | if (!transfer) { |
| 324 | free(buf); |
| 325 | return -ENOMEM; |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Daniel Drake | 66348c9 | 2008-03-09 00:58:09 +0000 | [diff] [blame] | 328 | 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 Drake | aae05f6 | 2008-03-10 11:32:15 +0000 | [diff] [blame] | 331 | libusb_fill_control_transfer(transfer, devh, buf, cb_mode_changed, NULL, |
| 332 | 1000); |
Daniel Drake | 66348c9 | 2008-03-09 00:58:09 +0000 | [diff] [blame] | 333 | |
Daniel Drake | de4c534 | 2008-03-10 18:38:15 +0000 | [diff] [blame] | 334 | transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK |
| 335 | | LIBUSB_TRANSFER_FREE_BUFFER | LIBUSB_TRANSFER_FREE_TRANSFER; |
Daniel Drake | 66348c9 | 2008-03-09 00:58:09 +0000 | [diff] [blame] | 336 | return libusb_submit_transfer(transfer); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static int do_sync_intr(unsigned char *data) |
| 340 | { |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 341 | int r; |
| 342 | int transferred; |
| 343 | |
Daniel Drake | 66348c9 | 2008-03-09 00:58:09 +0000 | [diff] [blame] | 344 | r = libusb_interrupt_transfer(devh, EP_INTR, data, INTR_LENGTH, |
| 345 | &transferred, 1000); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 346 | 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 Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 355 | printf("recv interrupt %04x\n", *((uint16_t *)data)); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | static int sync_intr(unsigned char type) |
Pete Batard | a636df4 | 2010-05-12 21:46:31 -0300 | [diff] [blame] | 360 | { |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 361 | 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 | |
| 373 | static int save_to_file(unsigned char *data) |
| 374 | { |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 375 | FILE *f; |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 376 | char filename[64]; |
| 377 | |
Peter Stuge | d346cd9 | 2012-04-07 05:20:36 +0200 | [diff] [blame] | 378 | snprintf(filename, sizeof(filename), "finger%d.pgm", img_idx++); |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 379 | f = fopen(filename, "w"); |
| 380 | if (!f) |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 381 | return -1; |
| 382 | |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 383 | fputs("P5 384 289 255 ", f); |
| 384 | (void)fwrite(data + 64, 1, 384*289, f); |
| 385 | fclose(f); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 386 | printf("saved image to %s\n", filename); |
| 387 | return 0; |
| 388 | } |
| 389 | |
| 390 | static int next_state(void) |
| 391 | { |
| 392 | int r = 0; |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 393 | |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 394 | 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 Drake | c0c9432 | 2008-03-13 12:36:56 +0000 | [diff] [blame] | 421 | fprintf(stderr, "error detected changing state\n"); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 422 | return r; |
| 423 | } |
| 424 | |
| 425 | printf("new state: %d\n", state); |
| 426 | return 0; |
| 427 | } |
| 428 | |
Pete Batard | 29f9f9e | 2010-08-13 11:59:49 +0100 | [diff] [blame] | 429 | static void LIBUSB_CALL cb_irq(struct libusb_transfer *transfer) |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 430 | { |
Daniel Drake | 66348c9 | 2008-03-09 00:58:09 +0000 | [diff] [blame] | 431 | unsigned char irqtype = transfer->buffer[0]; |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 432 | |
Daniel Drake | 66348c9 | 2008-03-09 00:58:09 +0000 | [diff] [blame] | 433 | if (transfer->status != LIBUSB_TRANSFER_COMPLETED) { |
| 434 | fprintf(stderr, "irq transfer status %d?\n", transfer->status); |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 435 | goto err_free_transfer; |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | printf("IRQ callback %02x\n", irqtype); |
| 439 | switch (state) { |
| 440 | case STATE_AWAIT_IRQ_FINGER_DETECTED: |
| 441 | if (irqtype == 0x01) { |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 442 | if (next_state() < 0) |
| 443 | goto err_free_transfer; |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 444 | } 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 Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 450 | if (next_state() < 0) |
| 451 | goto err_free_transfer; |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 452 | } else { |
| 453 | printf("finger-on-sensor detected in wrong state!\n"); |
| 454 | } |
| 455 | break; |
| 456 | } |
Daniel Drake | 7ac0a40 | 2008-04-27 23:27:04 +0100 | [diff] [blame] | 457 | if (libusb_submit_transfer(irq_transfer) < 0) |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 458 | goto err_free_transfer; |
| 459 | |
| 460 | return; |
| 461 | |
| 462 | err_free_transfer: |
| 463 | libusb_free_transfer(transfer); |
| 464 | irq_transfer = NULL; |
| 465 | request_exit(2); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Pete Batard | 29f9f9e | 2010-08-13 11:59:49 +0100 | [diff] [blame] | 468 | static void LIBUSB_CALL cb_img(struct libusb_transfer *transfer) |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 469 | { |
Daniel Drake | 66348c9 | 2008-03-09 00:58:09 +0000 | [diff] [blame] | 470 | if (transfer->status != LIBUSB_TRANSFER_COMPLETED) { |
| 471 | fprintf(stderr, "img transfer status %d?\n", transfer->status); |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 472 | goto err_free_transfer; |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | printf("Image callback\n"); |
| 476 | save_to_file(imgbuf); |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 477 | if (next_state() < 0) |
| 478 | goto err_free_transfer; |
| 479 | |
Daniel Drake | 7ac0a40 | 2008-04-27 23:27:04 +0100 | [diff] [blame] | 480 | if (libusb_submit_transfer(img_transfer) < 0) |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 481 | goto err_free_transfer; |
| 482 | |
| 483 | return; |
| 484 | |
| 485 | err_free_transfer: |
| 486 | libusb_free_transfer(transfer); |
| 487 | img_transfer = NULL; |
| 488 | request_exit(2); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 491 | static int init_capture(void) |
| 492 | { |
| 493 | int r; |
| 494 | |
Daniel Drake | 7ac0a40 | 2008-04-27 23:27:04 +0100 | [diff] [blame] | 495 | r = libusb_submit_transfer(irq_transfer); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 496 | if (r < 0) |
| 497 | return r; |
| 498 | |
Daniel Drake | 7ac0a40 | 2008-04-27 23:27:04 +0100 | [diff] [blame] | 499 | r = libusb_submit_transfer(img_transfer); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 500 | if (r < 0) { |
Daniel Drake | 7ac0a40 | 2008-04-27 23:27:04 +0100 | [diff] [blame] | 501 | libusb_cancel_transfer(irq_transfer); |
| 502 | while (irq_transfer) |
Daniel Drake | 1df713d | 2008-06-24 23:01:51 -0500 | [diff] [blame] | 503 | if (libusb_handle_events(NULL) < 0) |
Daniel Drake | 7ac0a40 | 2008-04-27 23:27:04 +0100 | [diff] [blame] | 504 | break; |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 505 | return r; |
| 506 | } |
| 507 | |
| 508 | /* start state machine */ |
| 509 | state = STATE_AWAIT_IRQ_FINGER_REMOVED; |
| 510 | return next_state(); |
| 511 | } |
| 512 | |
| 513 | static 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 Drake | 66348c9 | 2008-03-09 00:58:09 +0000 | [diff] [blame] | 547 | static int alloc_transfers(void) |
| 548 | { |
Daniel Drake | 211f80c | 2008-03-25 16:24:30 +0000 | [diff] [blame] | 549 | img_transfer = libusb_alloc_transfer(0); |
Daniel Drake | 66348c9 | 2008-03-09 00:58:09 +0000 | [diff] [blame] | 550 | if (!img_transfer) |
| 551 | return -ENOMEM; |
Pete Batard | a636df4 | 2010-05-12 21:46:31 -0300 | [diff] [blame] | 552 | |
Daniel Drake | 211f80c | 2008-03-25 16:24:30 +0000 | [diff] [blame] | 553 | irq_transfer = libusb_alloc_transfer(0); |
Daniel Drake | 66348c9 | 2008-03-09 00:58:09 +0000 | [diff] [blame] | 554 | 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 Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 565 | static void sighandler(int signum) |
| 566 | { |
Sean McBride | 830a9cb | 2017-12-27 22:32:15 -0500 | [diff] [blame] | 567 | (void)signum; |
| 568 | |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 569 | request_exit(1); |
| 570 | } |
| 571 | |
| 572 | static 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 Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | int main(void) |
| 590 | { |
Sean McBride | 0b3d4c6 | 2017-12-27 23:53:27 -0500 | [diff] [blame] | 591 | int r; |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 592 | |
Daniel Drake | 1df713d | 2008-06-24 23:01:51 -0500 | [diff] [blame] | 593 | r = libusb_init(NULL); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 594 | if (r < 0) { |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 595 | fprintf(stderr, "failed to initialise libusb %d - %s\n", r, libusb_strerror(r)); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 596 | exit(1); |
| 597 | } |
| 598 | |
Daniel Drake | 9cfdb49 | 2008-03-06 23:25:20 +0000 | [diff] [blame] | 599 | r = find_dpfp_device(); |
| 600 | if (r < 0) { |
| 601 | fprintf(stderr, "Could not find/open device\n"); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 602 | goto out; |
| 603 | } |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 604 | |
Daniel Drake | de53d97 | 2008-01-04 01:17:06 +0000 | [diff] [blame] | 605 | r = libusb_claim_interface(devh, 0); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 606 | if (r < 0) { |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 607 | fprintf(stderr, "claim interface error %d - %s\n", r, libusb_strerror(r)); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 608 | 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 Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 621 | setup_signals(); |
| 622 | |
Daniel Drake | 66348c9 | 2008-03-09 00:58:09 +0000 | [diff] [blame] | 623 | r = alloc_transfers(); |
| 624 | if (r < 0) |
| 625 | goto out_deinit; |
| 626 | |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 627 | #if defined(DPFP_THREADED) |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 628 | exit_semaphore = semaphore_create(); |
| 629 | if (!exit_semaphore) { |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 630 | 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 Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 636 | semaphore_destroy(exit_semaphore); |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 637 | goto out_deinit; |
| 638 | } |
| 639 | |
| 640 | r = init_capture(); |
| 641 | if (r < 0) |
| 642 | request_exit(2); |
| 643 | |
| 644 | while (!do_exit) |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 645 | semaphore_take(exit_semaphore); |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 646 | #else |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 647 | r = init_capture(); |
| 648 | if (r < 0) |
| 649 | goto out_deinit; |
| 650 | |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 651 | while (!do_exit) { |
Daniel Drake | 1df713d | 2008-06-24 23:01:51 -0500 | [diff] [blame] | 652 | r = libusb_handle_events(NULL); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 653 | if (r < 0) |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 654 | request_exit(2); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 655 | } |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 656 | #endif |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 657 | |
| 658 | printf("shutting down...\n"); |
Pete Batard | a636df4 | 2010-05-12 21:46:31 -0300 | [diff] [blame] | 659 | |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 660 | #if defined(DPFP_THREADED) |
| 661 | thread_join(poll_thread); |
Chris Dickens | 9d23ed2 | 2020-11-16 13:03:52 -0800 | [diff] [blame] | 662 | semaphore_destroy(exit_semaphore); |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 663 | #endif |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 664 | |
Daniel Drake | fec7c84 | 2008-05-11 20:31:58 +0100 | [diff] [blame] | 665 | if (img_transfer) { |
| 666 | r = libusb_cancel_transfer(img_transfer); |
| 667 | if (r < 0) |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 668 | fprintf(stderr, "failed to cancel transfer %d - %s\n", r, libusb_strerror(r)); |
Daniel Drake | fec7c84 | 2008-05-11 20:31:58 +0100 | [diff] [blame] | 669 | } |
Pete Batard | a636df4 | 2010-05-12 21:46:31 -0300 | [diff] [blame] | 670 | |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 671 | 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 Drake | 1df713d | 2008-06-24 23:01:51 -0500 | [diff] [blame] | 678 | if (libusb_handle_events(NULL) < 0) |
Daniel Drake | 7ac0a40 | 2008-04-27 23:27:04 +0100 | [diff] [blame] | 679 | break; |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 680 | } |
Pete Batard | a636df4 | 2010-05-12 21:46:31 -0300 | [diff] [blame] | 681 | |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 682 | if (do_exit == 1) |
| 683 | r = 0; |
| 684 | else |
| 685 | r = 1; |
| 686 | |
| 687 | out_deinit: |
Chris Dickens | f69548c | 2020-09-15 14:15:00 -0700 | [diff] [blame] | 688 | if (img_transfer) |
| 689 | libusb_free_transfer(img_transfer); |
| 690 | if (irq_transfer) |
| 691 | libusb_free_transfer(irq_transfer); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 692 | set_mode(0); |
| 693 | set_hwstat(0x80); |
| 694 | out_release: |
Daniel Drake | de53d97 | 2008-01-04 01:17:06 +0000 | [diff] [blame] | 695 | libusb_release_interface(devh, 0); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 696 | out: |
Daniel Drake | de53d97 | 2008-01-04 01:17:06 +0000 | [diff] [blame] | 697 | libusb_close(devh); |
Daniel Drake | 1df713d | 2008-06-24 23:01:51 -0500 | [diff] [blame] | 698 | libusb_exit(NULL); |
Daniel Drake | 852bba4 | 2007-11-28 13:48:45 +0000 | [diff] [blame] | 699 | return r >= 0 ? r : -r; |
| 700 | } |