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