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