GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 85.9% 250 / 0 / 291
Functions: 96.3% 26 / 0 / 27
Branches: 57.8% 48 / 0 / 83

libfprint/drivers/aes2501.c
Line Branch Exec Source
1 /*
2 * AuthenTec AES2501 driver for libfprint
3 * Copyright (C) 2007-2008 Daniel Drake <dsd@gentoo.org>
4 * Copyright (C) 2007 Cyrille Bagard
5 * Copyright (C) 2007-2008, 2012 Vasily Khoruzhick <anarsoul@gmail.com>
6 *
7 * Based on code from http://home.gna.org/aes2501, relicensed with permission
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 #define FP_COMPONENT "aes2501"
25
26 #include "drivers_api.h"
27 #include "aeslib.h"
28 #include "aes2501.h"
29
30 static void start_capture (FpImageDevice *dev);
31 static void complete_deactivation (FpImageDevice *dev);
32
33 /* FIXME these need checking */
34 #define EP_IN (1 | FPI_USB_ENDPOINT_IN)
35 #define EP_OUT (2 | FPI_USB_ENDPOINT_OUT)
36
37 #define BULK_TIMEOUT 4000
38
39 #define FINGER_DETECTION_LEN 20
40 #define READ_REGS_LEN 126
41 #define READ_REGS_RESP_LEN 159
42 #define STRIP_CAPTURE_LEN 1705
43
44 /*
45 * The AES2501 is an imaging device using a swipe-type sensor. It samples
46 * the finger at preprogrammed intervals, sending a 192x16 frame to the
47 * computer.
48 * Unless the user is scanning their finger unreasonably fast, the frames
49 * *will* overlap. The implementation below detects this overlap and produces
50 * a contiguous image as the end result.
51 * The fact that the user determines the length of the swipe (and hence the
52 * number of useful frames) and also the fact that overlap varies means that
53 * images returned from this driver vary in height.
54 */
55
56 #define FRAME_WIDTH 192
57 #define FRAME_HEIGHT 16
58 #define FRAME_SIZE (FRAME_WIDTH * FRAME_HEIGHT)
59 #define IMAGE_WIDTH (FRAME_WIDTH + (FRAME_WIDTH / 2))
60 /* maximum number of frames to read during a scan */
61 /* FIXME reduce substantially */
62 #define MAX_FRAMES 150
63
64 /****** GENERAL FUNCTIONS ******/
65
66 struct _FpiDeviceAes2501
67 {
68 FpImageDevice parent;
69
70 guint8 read_regs_retry_count;
71 GSList *strips;
72 size_t strips_len;
73 gboolean deactivating;
74 int no_finger_cnt;
75 };
76 G_DECLARE_FINAL_TYPE (FpiDeviceAes2501, fpi_device_aes2501, FPI, DEVICE_AES2501,
77 FpImageDevice);
78
4/6
fpi_device_aes2501_class_intern_init:
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 125 times.
fpi_device_aes2501_get_type:
✓ Branch 2 → 3 taken 125 times.
✓ Branch 2 → 7 taken 24 times.
✓ Branch 4 → 5 taken 125 times.
✗ Branch 4 → 7 not taken.
399 G_DEFINE_TYPE (FpiDeviceAes2501, fpi_device_aes2501, FP_TYPE_IMAGE_DEVICE);
79
80 static struct fpi_frame_asmbl_ctx assembling_ctx = {
81 .frame_width = FRAME_WIDTH,
82 .frame_height = FRAME_HEIGHT,
83 .image_width = IMAGE_WIDTH,
84 .get_pixel = aes_get_pixel,
85 };
86
87 typedef void (*aes2501_read_regs_cb)(FpImageDevice *dev,
88 GError *error,
89 unsigned char *regs,
90 void *user_data);
91
92 struct aes2501_read_regs
93 {
94 FpImageDevice *dev;
95 aes2501_read_regs_cb callback;
96 struct aes_regwrite *regwrite;
97 void *user_data;
98 };
99
100 static void
101 1 read_regs_data_cb (FpiUsbTransfer *transfer, FpDevice *dev,
102 gpointer user_data, GError *error)
103 {
104 1 struct aes2501_read_regs *rdata = user_data;
105
106 1 rdata->callback (rdata->dev, error, transfer->buffer, rdata->user_data);
107 1 g_free (rdata);
108 1 }
109
110 static void
111 1 read_regs_rq_cb (FpImageDevice *dev, GError *error, void *user_data)
112 {
113 1 struct aes2501_read_regs *rdata = user_data;
114 1 FpiUsbTransfer *transfer;
115
116 1 g_free (rdata->regwrite);
117
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 7 taken 1 time.
1 if (error)
118 {
119 rdata->callback (dev, error, NULL, rdata->user_data);
120 g_free (rdata);
121 return;
122 }
123
124 1 transfer = fpi_usb_transfer_new (FP_DEVICE (dev));
125 1 transfer->short_is_error = TRUE;
126 1 fpi_usb_transfer_fill_bulk (transfer, EP_IN, READ_REGS_LEN);
127 1 fpi_usb_transfer_submit (transfer, BULK_TIMEOUT, NULL,
128 read_regs_data_cb, rdata);
129 }
130
131 static void
132 1 read_regs (FpImageDevice *dev, aes2501_read_regs_cb callback,
133 void *user_data)
134 {
135 /* FIXME: regwrite is dynamic because of asynchronity. is this really
136 * required? */
137 1 struct aes_regwrite *regwrite = g_malloc (sizeof (*regwrite));
138 1 struct aes2501_read_regs *rdata = g_malloc (sizeof (*rdata));
139
140 1 G_DEBUG_HERE ();
141 1 regwrite->reg = AES2501_REG_CTRL2;
142 1 regwrite->value = AES2501_CTRL2_READ_REGS;
143 1 rdata->dev = dev;
144 1 rdata->callback = callback;
145 1 rdata->user_data = user_data;
146 1 rdata->regwrite = regwrite;
147
148 1 aes_write_regv (dev, (const struct aes_regwrite *) regwrite, 1,
149 read_regs_rq_cb, rdata);
150 1 }
151
152 /* Read the value of a specific register from a register dump */
153 static int
154 48 regval_from_dump (unsigned char *data, guint8 target)
155 {
156
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 48 times.
48 if (*data != FIRST_AES2501_REG)
157 {
158 fp_err ("not a register dump");
159 return -1;
160 }
161
162
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 8 taken 48 times.
48 if (!(FIRST_AES2501_REG <= target && target <= LAST_AES2501_REG))
163 {
164 fp_err ("out of range");
165 return -1;
166 }
167
168 48 target -= FIRST_AES2501_REG;
169 48 target *= 2;
170 48 return data[target + 1];
171 }
172
173 static void
174 54 generic_write_regv_cb (FpImageDevice *dev, GError *error,
175 void *user_data)
176 {
177 54 FpiSsm *ssm = user_data;
178
179
1/2
✓ Branch 2 → 3 taken 54 times.
✗ Branch 2 → 4 not taken.
54 if (!error)
180 54 fpi_ssm_next_state (ssm);
181 else
182 fpi_ssm_mark_failed (ssm, error);
183 54 }
184
185 /* read the specified number of bytes from the IN endpoint but throw them
186 * away, then increment the SSM */
187 static void
188 3 generic_read_ignore_data (FpiSsm *ssm, FpDevice *dev,
189 size_t bytes)
190 {
191 3 FpiUsbTransfer *transfer;
192
193 3 transfer = fpi_usb_transfer_new (dev);
194 3 transfer->ssm = ssm;
195 3 transfer->short_is_error = TRUE;
196 3 fpi_usb_transfer_fill_bulk (transfer, EP_IN, bytes);
197 3 fpi_usb_transfer_submit (transfer, BULK_TIMEOUT, NULL,
198 fpi_ssm_usb_transfer_cb, NULL);
199 3 }
200
201 /****** IMAGE PROCESSING ******/
202
203 static int
204 48 sum_histogram_values (unsigned char *data, guint8 threshold)
205 {
206 48 int r = 0;
207 48 int i;
208 48 guint16 *histogram = (guint16 *) (data + 1);
209
210
1/2
✓ Branch 2 → 3 taken 48 times.
✗ Branch 2 → 7 not taken.
48 if (*data != 0xde)
211 return -1;
212
213
1/2
✓ Branch 3 → 4 taken 48 times.
✗ Branch 3 → 7 not taken.
48 if (threshold > 0x0f)
214 return -1;
215
216
2/2
✓ Branch 6 → 5 taken 384 times.
✓ Branch 6 → 7 taken 48 times.
432 for (i = threshold; i < 16; i++)
217 384 r += GUINT16_FROM_LE (histogram[i]);
218
219 return r;
220 }
221
222 /****** FINGER PRESENCE DETECTION ******/
223
224 static const struct aes_regwrite finger_det_reqs[] = {
225 { AES2501_REG_CTRL1, AES2501_CTRL1_MASTER_RESET },
226 { AES2501_REG_EXCITCTRL, 0x40 },
227 { AES2501_REG_DETCTRL,
228 AES2501_DETCTRL_DRATE_CONTINUOUS | AES2501_DETCTRL_SDELAY_31_MS },
229 { AES2501_REG_COLSCAN, AES2501_COLSCAN_SRATE_128_US },
230 { AES2501_REG_MEASDRV, AES2501_MEASDRV_MDRIVE_0_325 | AES2501_MEASDRV_MEASURE_SQUARE },
231 { AES2501_REG_MEASFREQ, AES2501_MEASFREQ_2M },
232 { AES2501_REG_DEMODPHASE1, DEMODPHASE_NONE },
233 { AES2501_REG_DEMODPHASE2, DEMODPHASE_NONE },
234 { AES2501_REG_CHANGAIN,
235 AES2501_CHANGAIN_STAGE2_4X | AES2501_CHANGAIN_STAGE1_16X },
236 { AES2501_REG_ADREFHI, 0x44 },
237 { AES2501_REG_ADREFLO, 0x34 },
238 { AES2501_REG_STRTCOL, 0x16 },
239 { AES2501_REG_ENDCOL, 0x16 },
240 { AES2501_REG_DATFMT, AES2501_DATFMT_BIN_IMG | 0x08 },
241 { AES2501_REG_TREG1, 0x70 },
242 { 0xa2, 0x02 },
243 { 0xa7, 0x00 },
244 { AES2501_REG_TREGC, AES2501_TREGC_ENABLE },
245 { AES2501_REG_TREGD, 0x1a },
246 { 0, 0 },
247 { AES2501_REG_CTRL1, AES2501_CTRL1_REG_UPDATE },
248 { AES2501_REG_CTRL2, AES2501_CTRL2_SET_ONE_SHOT },
249 { AES2501_REG_LPONT, AES2501_LPONT_MIN_VALUE },
250 };
251
252 static void start_finger_detection (FpImageDevice *dev);
253
254 static void
255 706 finger_det_data_cb (FpiUsbTransfer *transfer, FpDevice *_dev,
256 gpointer user_data, GError *error)
257 {
258 706 FpImageDevice *dev = FP_IMAGE_DEVICE (_dev);
259 706 unsigned char *data = transfer->buffer;
260 706 int i;
261 706 int sum = 0;
262
263
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 6 taken 706 times.
706 if (error)
264 {
265 fpi_image_device_session_error (dev, error);
266 return;
267 }
268
269 /* examine histogram to determine finger presence */
270
2/2
✓ Branch 6 → 5 taken 5648 times.
✓ Branch 6 → 7 taken 706 times.
6354 for (i = 1; i < 9; i++)
271 5648 sum += (data[i] & 0xf) + (data[i] >> 4);
272
2/2
✓ Branch 7 → 8 taken 1 time.
✓ Branch 7 → 10 taken 705 times.
706 if (sum > 20)
273 {
274 /* finger present, start capturing */
275 1 fpi_image_device_report_finger_status (dev, TRUE);
276 1 start_capture (dev);
277 }
278 else
279 {
280 /* no finger, poll for a new histogram */
281 705 start_finger_detection (dev);
282 }
283 }
284
285 static void
286 706 finger_det_reqs_cb (FpImageDevice *dev, GError *error,
287 void *user_data)
288 {
289 706 FpiUsbTransfer *transfer;
290
291
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 706 times.
706 if (error)
292 {
293 fpi_image_device_session_error (dev, error);
294 return;
295 }
296
297 706 transfer = fpi_usb_transfer_new (FP_DEVICE (dev));
298 706 transfer->short_is_error = TRUE;
299 706 fpi_usb_transfer_fill_bulk (transfer, EP_IN, FINGER_DETECTION_LEN);
300 706 fpi_usb_transfer_submit (transfer, BULK_TIMEOUT, NULL,
301 finger_det_data_cb, NULL);
302 }
303
304 static void
305 706 start_finger_detection (FpImageDevice *dev)
306 {
307 706 FpiDeviceAes2501 *self = FPI_DEVICE_AES2501 (dev);
308
309 706 G_DEBUG_HERE ();
310
311
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 7 taken 706 times.
706 if (self->deactivating)
312 {
313 complete_deactivation (dev);
314 return;
315 }
316
317 706 aes_write_regv (dev, finger_det_reqs, G_N_ELEMENTS (finger_det_reqs),
318 finger_det_reqs_cb, NULL);
319 }
320
321 /****** CAPTURE ******/
322
323 static const struct aes_regwrite capture_reqs_1[] = {
324 { AES2501_REG_CTRL1, AES2501_CTRL1_MASTER_RESET },
325 { 0, 0 },
326 { AES2501_REG_EXCITCTRL, 0x40 },
327 { AES2501_REG_DETCTRL,
328 AES2501_DETCTRL_SDELAY_31_MS | AES2501_DETCTRL_DRATE_CONTINUOUS },
329 { AES2501_REG_COLSCAN, AES2501_COLSCAN_SRATE_128_US },
330 { AES2501_REG_DEMODPHASE2, 0x7c },
331 { AES2501_REG_MEASDRV,
332 AES2501_MEASDRV_MEASURE_SQUARE | AES2501_MEASDRV_MDRIVE_0_325 },
333 { AES2501_REG_DEMODPHASE1, 0x24 },
334 { AES2501_REG_CHWORD1, 0x00 },
335 { AES2501_REG_CHWORD2, 0x6c },
336 { AES2501_REG_CHWORD3, 0x09 },
337 { AES2501_REG_CHWORD4, 0x54 },
338 { AES2501_REG_CHWORD5, 0x78 },
339 { 0xa2, 0x02 },
340 { 0xa7, 0x00 },
341 { 0xb6, 0x26 },
342 { 0xb7, 0x1a },
343 { AES2501_REG_CTRL1, AES2501_CTRL1_REG_UPDATE },
344 { AES2501_REG_IMAGCTRL,
345 AES2501_IMAGCTRL_TST_REG_ENABLE | AES2501_IMAGCTRL_HISTO_DATA_ENABLE |
346 AES2501_IMAGCTRL_IMG_DATA_DISABLE },
347 { AES2501_REG_STRTCOL, 0x10 },
348 { AES2501_REG_ENDCOL, 0x1f },
349 { AES2501_REG_CHANGAIN,
350 AES2501_CHANGAIN_STAGE1_2X | AES2501_CHANGAIN_STAGE2_2X },
351 { AES2501_REG_ADREFHI, 0x70 },
352 { AES2501_REG_ADREFLO, 0x20 },
353 { AES2501_REG_CTRL2, AES2501_CTRL2_SET_ONE_SHOT },
354 { AES2501_REG_LPONT, AES2501_LPONT_MIN_VALUE },
355 };
356
357 static const struct aes_regwrite capture_reqs_2[] = {
358 { AES2501_REG_IMAGCTRL,
359 AES2501_IMAGCTRL_TST_REG_ENABLE | AES2501_IMAGCTRL_HISTO_DATA_ENABLE |
360 AES2501_IMAGCTRL_IMG_DATA_DISABLE },
361 { AES2501_REG_STRTCOL, 0x10 },
362 { AES2501_REG_ENDCOL, 0x1f },
363 { AES2501_REG_CHANGAIN, AES2501_CHANGAIN_STAGE1_16X },
364 { AES2501_REG_ADREFHI, 0x70 },
365 { AES2501_REG_ADREFLO, 0x20 },
366 { AES2501_REG_CTRL2, AES2501_CTRL2_SET_ONE_SHOT },
367 };
368
369 static struct aes_regwrite strip_scan_reqs[] = {
370 { AES2501_REG_IMAGCTRL,
371 AES2501_IMAGCTRL_TST_REG_ENABLE | AES2501_IMAGCTRL_HISTO_DATA_ENABLE },
372 { AES2501_REG_STRTCOL, 0x00 },
373 { AES2501_REG_ENDCOL, 0x2f },
374 { AES2501_REG_CHANGAIN, AES2501_CHANGAIN_STAGE1_16X },
375 { AES2501_REG_ADREFHI, AES2501_ADREFHI_MAX_VALUE },
376 { AES2501_REG_ADREFLO, 0x20 },
377 { AES2501_REG_CTRL2, AES2501_CTRL2_SET_ONE_SHOT },
378 };
379
380 /* capture SM movement:
381 * write reqs and read data 1 + 2,
382 * request and read strip,
383 * jump back to request UNLESS there's no finger, in which case exit SM,
384 * report lack of finger presence, and move to finger detection */
385
386 enum capture_states {
387 CAPTURE_WRITE_REQS_1,
388 CAPTURE_READ_DATA_1,
389 CAPTURE_WRITE_REQS_2,
390 CAPTURE_READ_DATA_2,
391 CAPTURE_REQUEST_STRIP,
392 CAPTURE_READ_STRIP,
393 CAPTURE_NUM_STATES,
394 };
395
396 static void
397 48 capture_read_strip_cb (FpiUsbTransfer *transfer, FpDevice *_dev,
398 gpointer user_data, GError *error)
399 {
400 48 FpiSsm *ssm = transfer->ssm;
401 48 unsigned char *stripdata;
402 48 FpImageDevice *dev = FP_IMAGE_DEVICE (_dev);
403 48 FpiDeviceAes2501 *self = FPI_DEVICE_AES2501 (_dev);
404 48 unsigned char *data = transfer->buffer;
405 48 int sum;
406 48 int threshold;
407
408
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 48 times.
48 if (error)
409 {
410 fpi_ssm_mark_failed (ssm, error);
411 return;
412 }
413
414 48 threshold = regval_from_dump (data + 1 + 192 * 8 + 1 + 16 * 2 + 1 + 8,
415 AES2501_REG_DATFMT);
416
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 10 taken 48 times.
48 if (threshold < 0)
417 {
418 fpi_ssm_mark_failed (ssm,
419 fpi_device_error_new (FP_DEVICE_ERROR_PROTO));
420 return;
421 }
422
423 48 sum = sum_histogram_values (data + 1 + 192 * 8, threshold & 0x0f);
424
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 14 taken 48 times.
48 if (sum < 0)
425 {
426 fpi_ssm_mark_failed (ssm,
427 fpi_device_error_new (FP_DEVICE_ERROR_PROTO));
428 return;
429 }
430 48 fp_dbg ("sum=%d", sum);
431
432
1/2
✓ Branch 15 → 16 taken 48 times.
✗ Branch 15 → 18 not taken.
48 if (sum < AES2501_SUM_LOW_THRESH)
433 {
434 48 strip_scan_reqs[4].value -= 0x8;
435
2/2
✓ Branch 16 → 17 taken 42 times.
✓ Branch 16 → 21 taken 6 times.
48 if (strip_scan_reqs[4].value < AES2501_ADREFHI_MIN_VALUE)
436 42 strip_scan_reqs[4].value = AES2501_ADREFHI_MIN_VALUE;
437 }
438 else if (sum > AES2501_SUM_HIGH_THRESH)
439 {
440 strip_scan_reqs[4].value += 0x8;
441 if (strip_scan_reqs[4].value > AES2501_ADREFHI_MAX_VALUE)
442 strip_scan_reqs[4].value = AES2501_ADREFHI_MAX_VALUE;
443 }
444 48 fp_dbg ("ADREFHI is %.2x", strip_scan_reqs[4].value);
445
446 /* Sum is 0, maybe finger was removed? Wait for 3 empty frames
447 * to ensure
448 */
449
2/2
✓ Branch 22 → 23 taken 3 times.
✓ Branch 22 → 32 taken 45 times.
48 if (sum == 0)
450 {
451 3 self->no_finger_cnt++;
452
2/2
✓ Branch 23 → 24 taken 1 time.
✓ Branch 23 → 31 taken 2 times.
3 if (self->no_finger_cnt == 3)
453 {
454 1 FpImage *img;
455
456 1 self->strips = g_slist_reverse (self->strips);
457 1 fpi_do_movement_estimation (&assembling_ctx, self->strips);
458 1 img = fpi_assemble_frames (&assembling_ctx,
459 self->strips);
460 1 img->flags |= FPI_IMAGE_PARTIAL;
461 1 g_slist_free_full (self->strips, g_free);
462 1 self->strips = NULL;
463 1 self->strips_len = 0;
464 1 fpi_image_device_image_captured (dev, img);
465 1 fpi_image_device_report_finger_status (dev, FALSE);
466 /* marking machine complete will re-trigger finger detection loop */
467 1 fpi_ssm_mark_completed (ssm);
468 }
469 else
470 {
471 2 fpi_ssm_jump_to_state (ssm, CAPTURE_REQUEST_STRIP);
472 }
473 }
474 else
475 {
476 /* obtain next strip */
477 /* FIXME: would preallocating strip buffers be a decent optimization? */
478 45 struct fpi_frame *stripe = g_malloc (FRAME_WIDTH * FRAME_HEIGHT / 2 + sizeof (struct fpi_frame));
479 45 stripe->delta_x = 0;
480 45 stripe->delta_y = 0;
481 45 stripdata = stripe->data;
482 45 memcpy (stripdata, data + 1, 192 * 8);
483 45 self->no_finger_cnt = 0;
484 45 self->strips = g_slist_prepend (self->strips, stripe);
485 45 self->strips_len++;
486
487 45 fpi_ssm_jump_to_state (ssm, CAPTURE_REQUEST_STRIP);
488 }
489 }
490
491 static void
492 100 capture_run_state (FpiSsm *ssm, FpDevice *device)
493 {
494 100 FpImageDevice *dev = FP_IMAGE_DEVICE (device);
495 100 FpiDeviceAes2501 *self = FPI_DEVICE_AES2501 (device);
496
497
6/7
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 6 taken 1 time.
✓ Branch 3 → 8 taken 1 time.
✓ Branch 3 → 10 taken 1 time.
✓ Branch 3 → 12 taken 48 times.
✓ Branch 3 → 15 taken 48 times.
✗ Branch 3 → 19 not taken.
100 switch (fpi_ssm_get_cur_state (ssm))
498 {
499 1 case CAPTURE_WRITE_REQS_1:
500 1 aes_write_regv (dev, capture_reqs_1, G_N_ELEMENTS (capture_reqs_1),
501 generic_write_regv_cb, ssm);
502 1 break;
503
504 1 case CAPTURE_READ_DATA_1:
505 1 generic_read_ignore_data (ssm, device, READ_REGS_RESP_LEN);
506 1 break;
507
508 1 case CAPTURE_WRITE_REQS_2:
509 1 aes_write_regv (dev, capture_reqs_2, G_N_ELEMENTS (capture_reqs_2),
510 generic_write_regv_cb, ssm);
511 1 break;
512
513 1 case CAPTURE_READ_DATA_2:
514 1 generic_read_ignore_data (ssm, device, READ_REGS_RESP_LEN);
515 1 break;
516
517 48 case CAPTURE_REQUEST_STRIP:
518
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 48 times.
48 if (self->deactivating)
519 fpi_ssm_mark_completed (ssm);
520 else
521 48 aes_write_regv (dev, strip_scan_reqs, G_N_ELEMENTS (strip_scan_reqs),
522 generic_write_regv_cb, ssm);
523 break;
524
525 48 case CAPTURE_READ_STRIP: {
526 48 FpiUsbTransfer *transfer;
527
528 48 transfer = fpi_usb_transfer_new (device);
529 48 transfer->ssm = ssm;
530 48 transfer->short_is_error = TRUE;
531 48 fpi_usb_transfer_fill_bulk (transfer, EP_IN, STRIP_CAPTURE_LEN);
532 48 fpi_usb_transfer_submit (transfer, BULK_TIMEOUT, NULL,
533 capture_read_strip_cb, NULL);
534 48 break;
535 }
536 }
537 100 ;
538 100 }
539
540 static void
541 1 capture_sm_complete (FpiSsm *ssm, FpDevice *_dev, GError *error)
542 {
543 1 FpImageDevice *dev = FP_IMAGE_DEVICE (_dev);
544 1 FpiDeviceAes2501 *self = FPI_DEVICE_AES2501 (_dev);
545
546 1 G_DEBUG_HERE ();
547
548
1/2
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 8 not taken.
1 if (self->deactivating)
549 {
550 1 complete_deactivation (dev);
551
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 11 taken 1 time.
1 g_clear_pointer (&error, g_error_free);
552 }
553 else if (error)
554 {
555 fpi_image_device_session_error (dev, error);
556 }
557 else
558 {
559 start_finger_detection (dev);
560 }
561 1 }
562
563 static void
564 1 start_capture (FpImageDevice *dev)
565 {
566 1 FpiDeviceAes2501 *self = FPI_DEVICE_AES2501 (dev);
567 1 FpiSsm *ssm;
568
569
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 5 taken 1 time.
1 if (self->deactivating)
570 {
571 complete_deactivation (dev);
572 return;
573 }
574
575 1 self->no_finger_cnt = 0;
576 /* Reset gain */
577 1 strip_scan_reqs[4].value = AES2501_ADREFHI_MAX_VALUE;
578 1 ssm = fpi_ssm_new (FP_DEVICE (dev), capture_run_state,
579 CAPTURE_NUM_STATES);
580 1 G_DEBUG_HERE ();
581 1 fpi_ssm_start (ssm, capture_sm_complete);
582 }
583
584 /****** INITIALIZATION/DEINITIALIZATION ******/
585
586 static const struct aes_regwrite init_1[] = {
587 { AES2501_REG_CTRL1, AES2501_CTRL1_MASTER_RESET },
588 { 0, 0 },
589 { 0xb0, 0x27 }, /* Reserved? */
590 { AES2501_REG_CTRL1, AES2501_CTRL1_MASTER_RESET },
591 { AES2501_REG_EXCITCTRL, 0x40 },
592 { 0xff, 0x00 }, /* Reserved? */
593 { 0xff, 0x00 }, /* Reserved? */
594 { 0xff, 0x00 }, /* Reserved? */
595 { 0xff, 0x00 }, /* Reserved? */
596 { 0xff, 0x00 }, /* Reserved? */
597 { 0xff, 0x00 }, /* Reserved? */
598 { 0xff, 0x00 }, /* Reserved? */
599 { 0xff, 0x00 }, /* Reserved? */
600 { 0xff, 0x00 }, /* Reserved? */
601 { 0xff, 0x00 }, /* Reserved? */
602 { 0xff, 0x00 }, /* Reserved? */
603 { AES2501_REG_CTRL1, AES2501_CTRL1_MASTER_RESET },
604 { AES2501_REG_EXCITCTRL, 0x40 },
605 { AES2501_REG_DETCTRL,
606 AES2501_DETCTRL_DRATE_CONTINUOUS | AES2501_DETCTRL_SDELAY_31_MS },
607 { AES2501_REG_COLSCAN, AES2501_COLSCAN_SRATE_128_US },
608 { AES2501_REG_MEASDRV,
609 AES2501_MEASDRV_MDRIVE_0_325 | AES2501_MEASDRV_MEASURE_SQUARE },
610 { AES2501_REG_MEASFREQ, AES2501_MEASFREQ_2M },
611 { AES2501_REG_DEMODPHASE1, DEMODPHASE_NONE },
612 { AES2501_REG_DEMODPHASE2, DEMODPHASE_NONE },
613 { AES2501_REG_CHANGAIN,
614 AES2501_CHANGAIN_STAGE2_4X | AES2501_CHANGAIN_STAGE1_16X },
615 { AES2501_REG_ADREFHI, 0x44 },
616 { AES2501_REG_ADREFLO, 0x34 },
617 { AES2501_REG_STRTCOL, 0x16 },
618 { AES2501_REG_ENDCOL, 0x16 },
619 { AES2501_REG_DATFMT, AES2501_DATFMT_BIN_IMG | 0x08 },
620 { AES2501_REG_TREG1, 0x70 },
621 { 0xa2, 0x02 },
622 { 0xa7, 0x00 },
623 { AES2501_REG_TREGC, AES2501_TREGC_ENABLE },
624 { AES2501_REG_TREGD, 0x1a },
625 { AES2501_REG_CTRL1, AES2501_CTRL1_REG_UPDATE },
626 { AES2501_REG_CTRL2, AES2501_CTRL2_SET_ONE_SHOT },
627 { AES2501_REG_LPONT, AES2501_LPONT_MIN_VALUE },
628 };
629
630 static const struct aes_regwrite init_2[] = {
631 { AES2501_REG_CTRL1, AES2501_CTRL1_MASTER_RESET },
632 { AES2501_REG_EXCITCTRL, 0x40 },
633 { AES2501_REG_CTRL1, AES2501_CTRL1_MASTER_RESET },
634 { AES2501_REG_AUTOCALOFFSET, 0x41 },
635 { AES2501_REG_EXCITCTRL, 0x42 },
636 { AES2501_REG_DETCTRL, 0x53 },
637 { AES2501_REG_CTRL1, AES2501_CTRL1_REG_UPDATE },
638 };
639
640 static const struct aes_regwrite init_3[] = {
641 { 0xff, 0x00 },
642 { AES2501_REG_CTRL1, AES2501_CTRL1_MASTER_RESET },
643 { AES2501_REG_AUTOCALOFFSET, 0x41 },
644 { AES2501_REG_EXCITCTRL, 0x42 },
645 { AES2501_REG_DETCTRL, 0x53 },
646 { AES2501_REG_CTRL1, AES2501_CTRL1_REG_UPDATE },
647 };
648
649 static const struct aes_regwrite init_4[] = {
650 { AES2501_REG_CTRL1, AES2501_CTRL1_MASTER_RESET },
651 { AES2501_REG_EXCITCTRL, 0x40 },
652 { 0xb0, 0x27 },
653 { AES2501_REG_ENDROW, 0x0a },
654 { AES2501_REG_CTRL1, AES2501_CTRL1_REG_UPDATE },
655 { AES2501_REG_DETCTRL, 0x45 },
656 { AES2501_REG_AUTOCALOFFSET, 0x41 },
657 };
658
659 static const struct aes_regwrite init_5[] = {
660 { 0xb0, 0x27 },
661 { AES2501_REG_CTRL1, AES2501_CTRL1_MASTER_RESET },
662 { AES2501_REG_EXCITCTRL, 0x40 },
663 { 0xff, 0x00 },
664 { AES2501_REG_CTRL1, AES2501_CTRL1_MASTER_RESET },
665 { AES2501_REG_EXCITCTRL, 0x40 },
666 { AES2501_REG_CTRL1, AES2501_CTRL1_MASTER_RESET },
667 { AES2501_REG_EXCITCTRL, 0x40 },
668 { AES2501_REG_CTRL1, AES2501_CTRL1_MASTER_RESET },
669 { AES2501_REG_EXCITCTRL, 0x40 },
670 { AES2501_REG_CTRL1, AES2501_CTRL1_MASTER_RESET },
671 { AES2501_REG_EXCITCTRL, 0x40 },
672 { AES2501_REG_CTRL1, AES2501_CTRL1_MASTER_RESET },
673 { AES2501_REG_EXCITCTRL, 0x40 },
674 { AES2501_REG_CTRL1, AES2501_CTRL1_SCAN_RESET },
675 { AES2501_REG_CTRL1, AES2501_CTRL1_SCAN_RESET },
676 };
677
678 enum activate_states {
679 WRITE_INIT_1,
680 READ_DATA_1,
681 WRITE_INIT_2,
682 READ_REGS,
683 WRITE_INIT_3,
684 WRITE_INIT_4,
685 WRITE_INIT_5,
686 ACTIVATE_NUM_STATES,
687 };
688
689 static void
690 1 activate_read_regs_cb (FpImageDevice *dev, GError *error,
691 unsigned char *regs, void *user_data)
692 {
693 1 FpiSsm *ssm = user_data;
694 1 FpiDeviceAes2501 *self = FPI_DEVICE_AES2501 (dev);
695
696
1/2
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1 time.
1 if (error)
697 {
698 fpi_ssm_mark_failed (ssm, error);
699 }
700 else
701 {
702 1 fp_dbg ("reg 0xaf = %x", regs[0x5f]);
703
1/4
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 1 time.
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
1 if (regs[0x5f] != 0x6b || ++self->read_regs_retry_count == 13)
704 1 fpi_ssm_jump_to_state (ssm, WRITE_INIT_4);
705 else
706 fpi_ssm_next_state (ssm);
707 }
708 1 }
709
710 static void
711 activate_init3_cb (FpImageDevice *dev, GError *error,
712 void *user_data)
713 {
714 FpiSsm *ssm = user_data;
715
716 if (!error)
717 fpi_ssm_jump_to_state (ssm, READ_REGS);
718 else
719 fpi_ssm_mark_failed (ssm, error);
720 }
721
722 static void
723 6 activate_run_state (FpiSsm *ssm, FpDevice *_dev)
724 {
725 6 FpImageDevice *dev = FP_IMAGE_DEVICE (_dev);
726
727 /* This state machine isn't as linear as it may appear. After doing init1
728 * and init2 register configuration writes, we have to poll a register
729 * waiting for a specific value. READ_REGS checks the register value, and
730 * if we're ready to move on, we jump to init4. Otherwise, we write init3
731 * and then jump back to READ_REGS. In a synchronous model:
732
733 [...]
734 aes_write_regv(init_2);
735 read_regs(into buffer);
736 i = 0;
737 while (buffer[0x5f] == 0x6b) {
738 aes_write_regv(init_3);
739 read_regs(into buffer);
740 if (++i == 13)
741 break;
742 }
743 aes_write_regv(init_4);
744 */
745
746
6/8
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 6 taken 1 time.
✓ Branch 3 → 9 taken 1 time.
✓ Branch 3 → 11 taken 1 time.
✗ Branch 3 → 13 not taken.
✓ Branch 3 → 15 taken 1 time.
✓ Branch 3 → 17 taken 1 time.
✗ Branch 3 → 19 not taken.
6 switch (fpi_ssm_get_cur_state (ssm))
747 {
748 1 case WRITE_INIT_1:
749 1 aes_write_regv (dev, init_1, G_N_ELEMENTS (init_1),
750 generic_write_regv_cb, ssm);
751 1 break;
752
753 1 case READ_DATA_1:
754 1 fp_dbg ("read data 1");
755 1 generic_read_ignore_data (ssm, _dev, FINGER_DETECTION_LEN);
756 1 break;
757
758 1 case WRITE_INIT_2:
759 1 aes_write_regv (dev, init_2, G_N_ELEMENTS (init_2),
760 generic_write_regv_cb, ssm);
761 1 break;
762
763 1 case READ_REGS:
764 1 read_regs (dev, activate_read_regs_cb, ssm);
765 1 break;
766
767 case WRITE_INIT_3:
768 aes_write_regv (dev, init_3, G_N_ELEMENTS (init_3),
769 activate_init3_cb, ssm);
770 break;
771
772 1 case WRITE_INIT_4:
773 1 aes_write_regv (dev, init_4, G_N_ELEMENTS (init_4),
774 generic_write_regv_cb, ssm);
775 1 break;
776
777 1 case WRITE_INIT_5:
778 1 aes_write_regv (dev, init_5, G_N_ELEMENTS (init_5),
779 generic_write_regv_cb, ssm);
780 1 break;
781 }
782 6 }
783
784 static void
785 1 activate_sm_complete (FpiSsm *ssm, FpDevice *dev, GError *error)
786 {
787 1 fpi_image_device_activate_complete (FP_IMAGE_DEVICE (dev), error);
788
789
1/2
✓ Branch 3 → 4 taken 1 time.
✗ Branch 3 → 5 not taken.
1 if (!error)
790 1 start_finger_detection (FP_IMAGE_DEVICE (dev));
791 1 }
792
793 static void
794 1 dev_activate (FpImageDevice *dev)
795 {
796 1 FpiDeviceAes2501 *self = FPI_DEVICE_AES2501 (dev);
797 1 FpiSsm *ssm = fpi_ssm_new (FP_DEVICE (dev), activate_run_state,
798 ACTIVATE_NUM_STATES);
799
800 1 self->read_regs_retry_count = 0;
801 1 fpi_ssm_start (ssm, activate_sm_complete);
802 1 }
803
804 static void
805 1 dev_deactivate (FpImageDevice *dev)
806 {
807 1 FpiDeviceAes2501 *self = FPI_DEVICE_AES2501 (dev);
808
809 /* FIXME: audit cancellation points, probably need more, specifically
810 * in error handling paths? */
811 1 self->deactivating = TRUE;
812 1 }
813
814 static void
815 1 complete_deactivation (FpImageDevice *dev)
816 {
817 1 FpiDeviceAes2501 *self = FPI_DEVICE_AES2501 (dev);
818
819 1 G_DEBUG_HERE ();
820
821 /* FIXME: if we're in the middle of a scan, we should cancel the scan.
822 * maybe we can do this with a master reset, unconditionally? */
823
824 1 self->deactivating = FALSE;
825 1 g_slist_free_full (self->strips, g_free);
826 1 self->strips = NULL;
827 1 self->strips_len = 0;
828 1 fpi_image_device_deactivate_complete (dev, NULL);
829 1 }
830
831 static void
832 1 dev_init (FpImageDevice *dev)
833 {
834 1 GError *error = NULL;
835
836 /* FIXME check endpoints */
837
838 1 g_usb_device_claim_interface (fpi_device_get_usb_device (FP_DEVICE (dev)), 0, 0, &error);
839 1 fpi_image_device_open_complete (dev, error);
840 1 }
841
842 static void
843 1 dev_deinit (FpImageDevice *dev)
844 {
845 1 GError *error = NULL;
846
847 1 g_usb_device_release_interface (fpi_device_get_usb_device (FP_DEVICE (dev)),
848 0, 0, &error);
849 1 fpi_image_device_close_complete (dev, error);
850 1 }
851
852 static const FpIdEntry id_table[] = {
853 { .vid = 0x08ff, .pid = 0x2500, },/* AES2500 */
854 { .vid = 0x08ff, .pid = 0x2580, },/* AES2501 */
855 { .vid = 0, .pid = 0, .driver_data = 0 },
856 };
857
858 static void
859 1 fpi_device_aes2501_init (FpiDeviceAes2501 *self)
860 {
861 1 }
862 static void
863 125 fpi_device_aes2501_class_init (FpiDeviceAes2501Class *klass)
864 {
865 125 FpDeviceClass *dev_class = FP_DEVICE_CLASS (klass);
866 125 FpImageDeviceClass *img_class = FP_IMAGE_DEVICE_CLASS (klass);
867
868 125 dev_class->id = "aes2501";
869 125 dev_class->full_name = "AuthenTec AES2501";
870 125 dev_class->type = FP_DEVICE_TYPE_USB;
871 125 dev_class->id_table = id_table;
872 125 dev_class->scan_type = FP_SCAN_TYPE_SWIPE;
873
874 125 img_class->img_open = dev_init;
875 125 img_class->img_close = dev_deinit;
876 125 img_class->activate = dev_activate;
877 125 img_class->deactivate = dev_deactivate;
878
879 125 img_class->img_width = IMAGE_WIDTH;
880 125 img_class->img_height = -1;
881 }
882