aes1610.c 27.3 KB
Newer Older
1 2
/*
 * AuthenTec AES1610 driver for libfprint
3
 * Copyright (C) 2007-2008 Daniel Drake <dsd@gentoo.org>
4 5
 * Copyright (C) 2007 Cyrille Bagard
 * Copyright (C) 2007 Vasily Khoruzhick
6
 * Copyright (C) 2009 Guido Grazioli <guido.grazioli@gmail.com>
7
 *
8
 * Based on code from libfprint aes2501 driver.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#define FP_COMPONENT "aes1610"

#include <errno.h>
#include <string.h>

30
#include <libusb.h>
31

32
#include <aeslib.h>
33 34
#include <fp_internal.h>

35 36 37 38 39 40 41 42 43 44
static void start_capture(struct fp_img_dev *dev);
static void complete_deactivation(struct fp_img_dev *dev);
static int adjust_gain(unsigned char *buffer, int status);

#define FIRST_AES1610_REG	0x1B
#define LAST_AES1610_REG	0xFF

#define GAIN_STATUS_FIRST 1
#define GAIN_STATUS_NORMAL 2

45
/* FIXME these need checking */
46 47
#define EP_IN			(1 | LIBUSB_ENDPOINT_IN)
#define EP_OUT			(2 | LIBUSB_ENDPOINT_OUT)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

#define BULK_TIMEOUT 4000

/*
 * The AES1610 is an imaging device using a swipe-type sensor. It samples
 * the finger at preprogrammed intervals, sending a 128x8 frame to the
 * computer.
 * Unless the user is scanning their finger unreasonably fast, the frames
 * *will* overlap. The implementation below detects this overlap and produces
 * a contiguous image as the end result.
 * The fact that the user determines the length of the swipe (and hence the
 * number of useful frames) and also the fact that overlap varies means that
 * images returned from this driver vary in height.
 */

63
#define FRAME_WIDTH	128
64
#define FRAME_HEIGHT	8
65
#define FRAME_SIZE	(FRAME_WIDTH * FRAME_HEIGHT)
66 67
/* maximum number of frames to read during a scan */
/* FIXME reduce substantially */
68
#define MAX_FRAMES		350
69

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/****** GENERAL FUNCTIONS ******/

struct aes1610_dev {
	uint8_t read_regs_retry_count;
	GSList *strips;
	size_t strips_len;
	gboolean deactivating;
	uint8_t blanks_count;
};

typedef void (*aes1610_read_regs_cb)(struct fp_img_dev *dev, int status,
	unsigned char *regs, void *user_data);

struct aes1610_read_regs {
	struct fp_img_dev *dev;
	aes1610_read_regs_cb callback;
	struct aes_regwrite *regwrite;
	void *user_data;
};

/* FIXME: what to do here? */
static void stub_capture_stop_cb(struct fp_img_dev *dev, int result, void *user_data)
92 93 94 95 96
{

}


97 98 99 100
/* check that read succeeded but ignore all data */
static void generic_ignore_data_cb(struct libusb_transfer *transfer)
{
	struct fpi_ssm *ssm = transfer->user_data;
101

102 103 104 105 106 107 108 109 110 111 112 113 114
	if (transfer->status != LIBUSB_TRANSFER_COMPLETED)
		fpi_ssm_mark_aborted(ssm, -EIO);
	else if (transfer->length != transfer->actual_length)
		fpi_ssm_mark_aborted(ssm, -EPROTO);
	else
		fpi_ssm_next_state(ssm);

	g_free(transfer->buffer);
	libusb_free_transfer(transfer);
}


static void read_regs_data_cb(struct libusb_transfer *transfer)
115
{
116 117
	struct aes1610_read_regs *rdata = transfer->user_data;
	unsigned char *retdata = NULL;
118 119
	int r;

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
	if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
		r = -EIO;
	} else if (transfer->length != transfer->actual_length) {
		r = -EPROTO;
	} else {
		r = 0;
		retdata = transfer->buffer;
	}

	rdata->callback(rdata->dev, r, retdata, rdata->user_data);
	g_free(rdata);
	g_free(transfer->buffer);
	libusb_free_transfer(transfer);
}

static void read_regs_rq_cb(struct fp_img_dev *dev, int result, void *user_data)
{
	struct aes1610_read_regs *rdata = user_data;
	struct libusb_transfer *transfer;
	unsigned char *data;
	int r;

	g_free(rdata->regwrite);
	if (result != 0)
		goto err;

	transfer = libusb_alloc_transfer(0);
	if (!transfer) {
		result = -ENOMEM;
		goto err;
	}

	data = g_malloc(126);
	libusb_fill_bulk_transfer(transfer, dev->udev, EP_IN, data, 126,
		read_regs_data_cb, rdata, BULK_TIMEOUT);

	r = libusb_submit_transfer(transfer);
157
	if (r < 0) {
158 159 160 161
		g_free(data);
		libusb_free_transfer(transfer);
		result = -EIO;
		goto err;
162 163
	}

164 165 166 167 168
	return;
err:
	rdata->callback(dev, result, NULL, rdata->user_data);
	g_free(rdata);
}
169

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

// XXX: this comes from aes2501 driver but it is unused here
static void read_regs(struct fp_img_dev *dev, aes1610_read_regs_cb callback,
	void *user_data)
{
	/* FIXME: regwrite is dynamic because of asynchronity. is this really
	 * required? */
	struct aes_regwrite *regwrite = g_malloc(sizeof(*regwrite));
	struct aes1610_read_regs *rdata = g_malloc(sizeof(*rdata));

	fp_dbg("");
	//regwrite->reg = AES1610_REG_CTRL2;
	//regwrite->value = AES1610_CTRL2_READ_REGS;
	rdata->dev = dev;
	rdata->callback = callback;
	rdata->user_data = user_data;
	rdata->regwrite = regwrite;

	//aes_write_regv(dev, (const struct aes_regwrite *) regwrite, 1,
	//	read_regs_rq_cb, rdata);
190 191
}

192 193
/* Read the value of a specific register from a register dump */
static int regval_from_dump(unsigned char *data, uint8_t target)
194
{
195 196 197 198 199 200 201 202 203 204 205 206 207
	if (*data != FIRST_AES1610_REG) {
		fp_err("not a register dump");
		return -EILSEQ;
	}

	if (!(FIRST_AES1610_REG <= target && target <= LAST_AES1610_REG)) {
		fp_err("out of range");
		return -EINVAL;
	}

	target -= FIRST_AES1610_REG;
	target *= 2;
	return data[target + 1];
208 209
}

210 211
static void generic_write_regv_cb(struct fp_img_dev *dev, int result,
	void *user_data)
212
{
213 214 215 216 217
	struct fpi_ssm *ssm = user_data;
	if (result == 0)
		fpi_ssm_next_state(ssm);
	else
		fpi_ssm_mark_aborted(ssm, result);
218 219 220 221
}



222 223 224
/* read the specified number of bytes from the IN endpoint but throw them
 * away, then increment the SSM */
static void generic_read_ignore_data(struct fpi_ssm *ssm, size_t bytes)
225
{
226 227
	struct libusb_transfer *transfer = libusb_alloc_transfer(0);
	unsigned char *data;
228 229
	int r;

230 231 232 233
	if (!transfer) {
		fpi_ssm_mark_aborted(ssm, -ENOMEM);
		return;
	}
234

235 236 237
	data = g_malloc(bytes);
	libusb_fill_bulk_transfer(transfer, ssm->dev->udev, EP_IN, data, bytes,
		generic_ignore_data_cb, ssm, BULK_TIMEOUT);
238

239 240 241 242 243
	r = libusb_submit_transfer(transfer);
	if (r < 0) {
		g_free(data);
		libusb_free_transfer(transfer);
		fpi_ssm_mark_aborted(ssm, r);
244 245 246
	}
}

247 248 249
/****** IMAGE PROCESSING ******/

static int sum_histogram_values(unsigned char *data, uint8_t threshold)
250
{
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
	int r = 0;
	int i;
	uint16_t *histogram = (uint16_t *)(data + 1);

	if (*data != 0xde)
		return -EILSEQ;

	if (threshold > 0x0f)
		return -EINVAL;

	/* FIXME endianness */
	for (i = threshold; i < 16; i++)
		r += histogram[i];

	return r;
266 267
}

268
/* find overlapping parts of  frames */
269 270 271 272 273 274 275 276 277 278 279 280
static unsigned int find_overlap(unsigned char *first_frame,
	unsigned char *second_frame, unsigned int *min_error)
{
	unsigned int dy;
	unsigned int not_overlapped_height = 0;
	*min_error = 255 * FRAME_SIZE;
	for (dy = 0; dy < FRAME_HEIGHT; dy++) {
		/* Calculating difference (error) between parts of frames */
		unsigned int i;
		unsigned int error = 0;
		for (i = 0; i < FRAME_WIDTH * (FRAME_HEIGHT - dy); i++) {
			/* Using ? operator to avoid abs function */
281 282 283
			error += first_frame[i] > second_frame[i] ?
				(first_frame[i] - second_frame[i]) :
				(second_frame[i] - first_frame[i]);
284
		}
285

286 287 288 289 290 291 292 293 294
		/* Normalize error */
		error *= 15;
		error /= i;
		if (error < *min_error) {
			*min_error = error;
			not_overlapped_height = dy;
		}
		first_frame += FRAME_WIDTH;
	}
295 296

	return not_overlapped_height;
297 298 299
}

/* assemble a series of frames into a single image */
300 301
static unsigned int assemble(struct aes1610_dev *aesdev, unsigned char *output,
	gboolean reverse, unsigned int *errors_sum)
302 303 304 305 306
{
	uint8_t *assembled = output;
	int frame;
	uint32_t image_height = FRAME_HEIGHT;
	unsigned int min_error;
307 308
	size_t num_strips = aesdev->strips_len;
	GSList *list_entry = aesdev->strips;
309 310 311 312
	*errors_sum = 0;

	if (num_strips < 1)
		return 0;
313 314

	/* Rotating given data by 90 degrees
315 316
	 * Taken from document describing aes1610 image format
	 * TODO: move reversing detection here */
317

318 319 320
	if (reverse)
		output += (num_strips - 1) * FRAME_SIZE;
	for (frame = 0; frame < num_strips; frame++) {
321
		aes_assemble_image(list_entry->data, FRAME_WIDTH, FRAME_HEIGHT, output);
322 323

		if (reverse)
324
		    output -= FRAME_SIZE;
325
		else
326 327
		    output += FRAME_SIZE;
		list_entry = g_slist_next(list_entry);
328 329 330 331 332 333 334 335 336 337 338 339
	}

	/* Detecting where frames overlaped */
	output = assembled;
	for (frame = 1; frame < num_strips; frame++) {
		int not_overlapped;

		output += FRAME_SIZE;
		not_overlapped = find_overlap(assembled, output, &min_error);
		*errors_sum += min_error;
		image_height += not_overlapped;
		assembled += FRAME_WIDTH * not_overlapped;
340
		memcpy(assembled, output, FRAME_SIZE);
341 342 343 344
	}
	return image_height;
}

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
static void assemble_and_submit_image(struct fp_img_dev *dev)
{
	struct aes1610_dev *aesdev = dev->priv;
	size_t final_size;
	struct fp_img *img;
	unsigned int errors_sum, r_errors_sum;

	fp_dbg("");

	BUG_ON(aesdev->strips_len == 0);

	/* reverse list */
	aesdev->strips = g_slist_reverse(aesdev->strips);

	/* create buffer big enough for max image */
	img = fpi_img_new(aesdev->strips_len * FRAME_SIZE);

	img->flags = FP_IMG_COLORS_INVERTED;
	img->height = assemble(aesdev, img->data, FALSE, &errors_sum);
	img->height = assemble(aesdev, img->data, TRUE, &r_errors_sum);

	if (r_errors_sum > errors_sum) {
	    img->height = assemble(aesdev, img->data, FALSE, &errors_sum);
		img->flags |= FP_IMG_V_FLIPPED | FP_IMG_H_FLIPPED;
		fp_dbg("normal scan direction");
	} else {
		fp_dbg("reversed scan direction");
	}

	/* now that overlap has been removed, resize output image buffer */
	final_size = img->height * FRAME_WIDTH;
	img = fpi_img_resize(img, final_size);
	/* FIXME: ugly workaround */
	if (img->height < 12)
		img->height = 12;
	fpi_imgdev_image_captured(dev, img);

	/* free strips and strip list */
	g_slist_foreach(aesdev->strips, (GFunc) g_free, NULL);
	g_slist_free(aesdev->strips);
	aesdev->strips = NULL;
	aesdev->strips_len = 0;
	aesdev->blanks_count = 0;
}


/****** FINGER PRESENCE DETECTION ******/


static const struct aes_regwrite finger_det_reqs[] = {
	{ 0x80, 0x01 },
	{ 0x80, 0x12 },
	{ 0x85, 0x00 },
	{ 0x8A, 0x00 },
	{ 0x8B, 0x0E },
	{ 0x8C, 0x90 },
	{ 0x8D, 0x83 },
	{ 0x8E, 0x07 },
	{ 0x8F, 0x07 },
	{ 0x96, 0x00 },
	{ 0x97, 0x48 },
	{ 0xA1, 0x00 },
	{ 0xA2, 0x50 },
	{ 0xA6, 0xE4 },
	{ 0xAD, 0x08 },
	{ 0xAE, 0x5B },
	{ 0xAF, 0x54 },
	{ 0xB1, 0x28 },
	{ 0xB5, 0xAB },
	{ 0xB6, 0x0E },
	{ 0x1B, 0x2D },
	{ 0x81, 0x04 }
};

static const struct aes_regwrite finger_det_none[] = {
	{ 0x80, 0x01 },
	{ 0x82, 0x00 },
	{ 0x86, 0x00 },
	{ 0xB1, 0x28 },
	{ 0x1D, 0x00 }
};


static void start_finger_detection(struct fp_img_dev *dev);

static void finger_det_data_cb(struct libusb_transfer *transfer)
{
	struct fp_img_dev *dev = transfer->user_data;
	unsigned char *data = transfer->buffer;
	int i;
	int sum = 0;

	if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
		fpi_imgdev_session_error(dev, -EIO);
		goto out;
	} else if (transfer->length != transfer->actual_length) {
		fpi_imgdev_session_error(dev, -EPROTO);
		goto out;
	}

	/* examine histogram to determine finger presence */
	for (i = 3; i < 17; i++)
		sum += (data[i] & 0xf) + (data[i] >> 4);
	if (sum > 20) {
		/* reset default gain */
		adjust_gain(data,GAIN_STATUS_FIRST);
		/* finger present, start capturing */
		fpi_imgdev_report_finger_status(dev, TRUE);
		start_capture(dev);
	} else {
		/* no finger, poll for a new histogram */
		start_finger_detection(dev);
	}

out:
	g_free(data);
	libusb_free_transfer(transfer);
}


static void finger_det_none_cb(struct fp_img_dev *dev, int result, void *user_data){
	fpi_imgdev_report_finger_status(dev, FALSE);
	start_finger_detection(dev);
}

static void finger_det_reqs_cb(struct fp_img_dev *dev, int result, void *user_data)
{
	struct libusb_transfer *transfer;
	unsigned char *data;
	int r;

	if (result) {
		fpi_imgdev_session_error(dev, result);
		return;
	}

	transfer = libusb_alloc_transfer(0);
	if (!transfer) {
		fpi_imgdev_session_error(dev, -ENOMEM);
		return;
	}

	data = g_malloc(19);
	libusb_fill_bulk_transfer(transfer, dev->udev, EP_IN, data, 19,
		finger_det_data_cb, dev, BULK_TIMEOUT);

	r = libusb_submit_transfer(transfer);
	if (r < 0) {
		g_free(data);
		libusb_free_transfer(transfer);
		fpi_imgdev_session_error(dev, r);
	}

}

static void start_finger_detection(struct fp_img_dev *dev)
{
	struct aes1610_dev *aesdev = dev->priv;
	struct libusb_transfer *transfer;

	if (aesdev->deactivating) {
		complete_deactivation(dev);
		return;
	}

	aes_write_regv(dev, finger_det_reqs, G_N_ELEMENTS(finger_det_reqs), finger_det_reqs_cb, NULL);

}

/****** CAPTURE ******/

static struct aes_regwrite capture_reqs[] = {
517 518 519 520 521 522 523 524 525
	{ 0x80, 0x01 },
	{ 0x80, 0x12 },
	{ 0x84, 0x01 },
	{ 0x85, 0x00 },
	{ 0x89, 0x64 },
	{ 0x8A, 0x00 },
	{ 0x8B, 0x0E },
	{ 0x8C, 0x90 },
	{ 0xBE, 0x23 },
526 527
	{ 0x29, 0x04 },
	{ 0x2A, 0xFF },
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
	{ 0x96, 0x00 },
	{ 0x98, 0x03 },
	{ 0x99, 0x00 },
	{ 0x9C, 0xA5 },
	{ 0x9D, 0x40 },
	{ 0x9E, 0xC6 },
	{ 0x9F, 0x8E },
	{ 0xA2, 0x50 },
	{ 0xA3, 0xF0 },
	{ 0xAD, 0x08 },
	{ 0xBD, 0x4F },
	{ 0xAF, 0x54 },
	{ 0xB1, 0x08 },
	{ 0xB5, 0xAB },
	{ 0x1B, 0x2D },
	{ 0xB6, 0x4E },
	{ 0xB8, 0x70 },
	{ 0x2B, 0xB3 },
	{ 0x2C, 0x5D },
	{ 0x2D, 0x98 },
	{ 0x2E, 0xB0 },
	{ 0x2F, 0x20 },
	{ 0xA2, 0xD0 },
	{ 0x1D, 0x21 },
	{ 0x1E, 0xBE },
	{ 0x1C, 0x00 },
	{ 0x1D, 0x30 },
	{ 0x1E, 0x29 },
	{ 0x1C, 0x01 },
	{ 0x1D, 0x00 },
	{ 0x1E, 0x9E },
	{ 0x1C, 0x02 },
	{ 0x1D, 0x30 },
	{ 0x1E, 0xBB },
	{ 0x1C, 0x03 },
	{ 0x1D, 0x00 },
	{ 0x1E, 0x9D },
	{ 0x1C, 0x04 },
	{ 0x1D, 0x22 },
	{ 0x1E, 0xFF },
	{ 0x1C, 0x05 },
	{ 0x1D, 0x1B },
	{ 0x1E, 0x4E },
	{ 0x1C, 0x06 },
	{ 0x1D, 0x16 },
	{ 0x1E, 0x28 },
	{ 0x1C, 0x07 },
	{ 0x1D, 0x22 },
	{ 0x1E, 0xFF },
	{ 0x1C, 0x08 },
	{ 0x1D, 0x15 },
	{ 0x1E, 0xF1 },
	{ 0x1C, 0x09 },
	{ 0x1D, 0x30 },
	{ 0x1E, 0xD5 },
	{ 0x1C, 0x0A },
	{ 0x1D, 0x00 },
	{ 0x1E, 0x9E },
	{ 0x1C, 0x0B },
	{ 0x1D, 0x17 },
	{ 0x1E, 0x9D },
	{ 0x1C, 0x0C },
	{ 0x1D, 0x28 },
	{ 0x1E, 0xD7 },
	{ 0x1C, 0x0D },
	{ 0x1D, 0x17 },
	{ 0x1E, 0xD7 },
	{ 0x1C, 0x0E },
	{ 0x1D, 0x0A },
	{ 0x1E, 0xCB },
	{ 0x1C, 0x0F },
	{ 0x1D, 0x24 },
	{ 0x1E, 0x14 },
	{ 0x1C, 0x10 },
	{ 0x1D, 0x17 },
	{ 0x1E, 0x85 },
	{ 0x1C, 0x11 },
	{ 0x1D, 0x15 },
	{ 0x1E, 0x71 },
	{ 0x1C, 0x12 },
	{ 0x1D, 0x2B },
	{ 0x1E, 0x36 },
	{ 0x1C, 0x13 },
	{ 0x1D, 0x12 },
	{ 0x1E, 0x06 },
	{ 0x1C, 0x14 },
	{ 0x1D, 0x30 },
	{ 0x1E, 0x97 },
	{ 0x1C, 0x15 },
	{ 0x1D, 0x21 },
	{ 0x1E, 0x32 },
	{ 0x1C, 0x16 },
	{ 0x1D, 0x06 },
	{ 0x1E, 0xE6 },
	{ 0x1C, 0x17 },
	{ 0x1D, 0x16 },
	{ 0x1E, 0x06 },
	{ 0x1C, 0x18 },
	{ 0x1D, 0x30 },
	{ 0x1E, 0x01 },
	{ 0x1C, 0x19 },
	{ 0x1D, 0x21 },
	{ 0x1E, 0x37 },
	{ 0x1C, 0x1A },
	{ 0x1D, 0x00 },
	{ 0x1E, 0x08 },
	{ 0x1C, 0x1B },
	{ 0x1D, 0x80 },
	{ 0x1E, 0xD5 },
	{ 0xA2, 0x50 },
	{ 0xA2, 0x50 },
	{ 0x81, 0x01 }
};

642
static struct aes_regwrite strip_scan_reqs[] = {
643
	{ 0xBE, 0x23 },
644 645
	{ 0x29, 0x04 },
	{ 0x2A, 0xFF },
646 647 648 649
	{ 0xBD, 0x4F },
	{ 0xFF, 0x00 }
};

650
static const struct aes_regwrite capture_stop[] = {
651 652 653
	{ 0x81,0x00 }
};

654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
/*
 * The different possible values for 0xBE register */
static unsigned char list_BE_values[10] = {
	0x23, 0x43, 0x63, 0x64, 0x65, 0x67, 0x6A, 0x6B
};

/*
 * The different possible values for 0xBD register */
static unsigned char list_BD_values[10] = {
	0x48, 0x4B, 0x4F, 0x52, 0x57, 0x59, 0x5B
};

/*
 * Adjust the gain according to the histogram data
 * 0xbd, 0xbe, 0x29 and 0x2A registers are affected
 * Returns 0 if no problem occured
 * TODO: This is a basic support for gain. It needs testing/tweaking.  */
static int adjust_gain(unsigned char *buffer, int status)
672
{
673 674 675
	// The position in the array of possible values for 0xBE and 0xBD registers
	static int pos_list_BE = 0;
	static int pos_list_BD = 0;
676

677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
	// This is the first adjustement (we begin acquisition)
	// We adjust strip_scan_reqs for future strips and capture_reqs that is sent just after this step
	if (status == GAIN_STATUS_FIRST) {
		if (buffer[1] > 0x78) { // maximum gain needed
			strip_scan_reqs[0].value = 0x6B;
			strip_scan_reqs[1].value = 0x06;
			strip_scan_reqs[2].value = 0x35;
			strip_scan_reqs[3].value = 0x5B;
		}
		else if (buffer[1] > 0x55) {
			strip_scan_reqs[0].value = 0x63;
			strip_scan_reqs[1].value = 0x15;
			strip_scan_reqs[2].value = 0x35;
			strip_scan_reqs[3].value = 0x4F;
		}
		else if (buffer[1] > 0x40 || buffer[16] > 0x19) {
			strip_scan_reqs[0].value = 0x43;
			strip_scan_reqs[1].value = 0x13;
			strip_scan_reqs[2].value = 0x35;
			strip_scan_reqs[3].value = 0x4B;
		}
		else { // minimum gain needed
			strip_scan_reqs[0].value = 0x23;
			strip_scan_reqs[1].value = 0x07;
			strip_scan_reqs[2].value = 0x35;
			strip_scan_reqs[3].value = 0x48;
		}
704

705 706 707 708 709
		// Now copy this values in capture_reqs
		capture_reqs[8].value = strip_scan_reqs[0].value;
		capture_reqs[9].value = strip_scan_reqs[1].value;
		capture_reqs[10].value = strip_scan_reqs[2].value;
		capture_reqs[21].value = strip_scan_reqs[3].value;
710

711 712
		fp_dbg("first gain: %x %x %x %x %x %x %x %x", strip_scan_reqs[0].reg, strip_scan_reqs[0].value, strip_scan_reqs[1].reg, strip_scan_reqs[1].value, strip_scan_reqs[2].reg, strip_scan_reqs[2].value, strip_scan_reqs[3].reg, strip_scan_reqs[3].value);
	}
713

714 715 716 717 718 719 720
	// Every 2/3 strips
	// We try to soften big changes of the gain (at least for 0xBE and 0xBD
	// FIXME: This softenning will need testing and tweaking too
	else if (status == GAIN_STATUS_NORMAL) {
		if (buffer[514] > 0x78) { // maximum gain needed
			if (pos_list_BE < 7)
				pos_list_BE++;
721

722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
			if (pos_list_BD < 6)
				pos_list_BD++;

			strip_scan_reqs[1].value = 0x04;
			strip_scan_reqs[2].value = 0x35;
		}
		else if (buffer[514] > 0x55) {
			if (pos_list_BE < 2)
				pos_list_BE++;
			else if (pos_list_BE > 2)
				pos_list_BE--;

			if (pos_list_BD < 2)
				pos_list_BD++;
			else if (pos_list_BD > 2)
				pos_list_BD--;

			strip_scan_reqs[1].value = 0x15;
			strip_scan_reqs[2].value = 0x35;
741
		}
742 743 744 745 746 747 748 749 750 751 752 753 754
		else if (buffer[514] > 0x40 || buffer[529] > 0x19) {
			if (pos_list_BE < 1)
				pos_list_BE++;
			else if (pos_list_BE > 1)
				pos_list_BE--;

			if (pos_list_BD < 1)
				pos_list_BD++;
			else if (pos_list_BD > 1)
				pos_list_BD--;

			strip_scan_reqs[1].value = 0x13;
			strip_scan_reqs[2].value = 0x35;
755
		}
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
		else { // minimum gain needed
			if (pos_list_BE > 0)
				pos_list_BE--;

			if (pos_list_BD > 0)
				pos_list_BD--;

			strip_scan_reqs[1].value = 0x07;
			strip_scan_reqs[2].value = 0x35;
		}

		strip_scan_reqs[0].value = list_BE_values[pos_list_BE];
		strip_scan_reqs[3].value = list_BD_values[pos_list_BD];

		fp_dbg("gain: %x %x %x %x %x %x %x %x", strip_scan_reqs[0].reg, strip_scan_reqs[0].value, strip_scan_reqs[1].reg, strip_scan_reqs[1].value, strip_scan_reqs[2].reg, strip_scan_reqs[2].value, strip_scan_reqs[3].reg, strip_scan_reqs[3].value);
	}
	// Unknown status
	else {
		fp_err("Unexpected gain status.");
		return 1;
	}

	return 0;
}

/*
 * Restore the default gain values */
static void restore_gain()
{
	strip_scan_reqs[0].value = list_BE_values[0];
	strip_scan_reqs[1].value = 0x04;
	strip_scan_reqs[2].value = 0xFF;
	strip_scan_reqs[3].value = list_BD_values[0];

	capture_reqs[8].value = list_BE_values[0];
	capture_reqs[9].value = 0x04;
	capture_reqs[10].value = 0xFF;
	capture_reqs[21].value = list_BD_values[0];
}


/* capture SM movement:
 * request and read strip,
 * jump back to request UNLESS theres no finger, in which case exit SM,
 * report lack of finger presence, and move to finger detection */

enum capture_states {
	CAPTURE_WRITE_REQS,
	CAPTURE_READ_DATA,
	CAPTURE_REQUEST_STRIP,
	CAPTURE_READ_STRIP,
	CAPTURE_NUM_STATES,
};

static void capture_read_strip_cb(struct libusb_transfer *transfer)
{
	unsigned char *stripdata;
	struct fpi_ssm *ssm = transfer->user_data;
	struct fp_img_dev *dev = ssm->priv;
	struct aes1610_dev *aesdev = dev->priv;
	unsigned char *data = transfer->buffer;
	int sum, i;
	int threshold;

	if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
		fpi_ssm_mark_aborted(ssm, -EIO);
		goto out;
	} else if (transfer->length != transfer->actual_length) {
		fpi_ssm_mark_aborted(ssm, -EPROTO);
		goto out;
	}

	/* FIXME: would preallocating strip buffers be a decent optimization? */
	//stripdata = g_malloc(128 * 4);
	//memcpy(stripdata, data + 1, 128 * 4);
	//aesdev->strips = g_slist_prepend(aesdev->strips, stripdata);
	//aesdev->strips_len++;

	/*threshold = regval_from_dump(data + 1 + 128*8 + 1 + 16*2 + 1 + 8,
		0x97);
	if (threshold < 0) {
		fpi_ssm_mark_aborted(ssm, threshold);
		goto out;
	}*/

	sum = 0;
	for (i = 516; i < 530; i++)
	{
		/* histogram[i] = number of pixels of value i
		   Only the pixel values from 10 to 15 are used to detect finger. */
		sum += data[i];
	}

	if (sum > 0) {
	        /* FIXME: would preallocating strip buffers be a decent optimization? */
	        stripdata = g_malloc(128 * 4);
	        memcpy(stripdata, data + 1, 128 * 4);
	        aesdev->strips = g_slist_prepend(aesdev->strips, stripdata);
	        aesdev->strips_len++;
		aesdev->blanks_count = 0;
	}

	if (sum < 0) {
		fpi_ssm_mark_aborted(ssm, sum);
		goto out;
	}
	fp_dbg("sum=%d", sum);

	/* FIXME: 0 might be too low as a threshold */
	/* FIXME: sometimes we get 0 in the middle of a scan, should we wait for
	 * a few consecutive zeroes? */

	/* If sum is 0 for a reasonable # of frames, finger has been removed */
	if (sum == 0) {
		aesdev->blanks_count++;
		fp_dbg("got blank frame");
	}

	/* use histogram data above for gain calibration (0xbd, 0xbe, 0x29 and 0x2A ) */
	adjust_gain(data, GAIN_STATUS_NORMAL);

	/* stop capturing if MAX_FRAMES is reached */
	if (aesdev->blanks_count > 10 || g_slist_length(aesdev->strips) >= MAX_FRAMES) {
		fp_dbg("sending stop capture.... blanks=%d  frames=%d", aesdev->blanks_count, g_slist_length(aesdev->strips));
		/* send stop capture bits */
		aes_write_regv(dev, capture_stop, G_N_ELEMENTS(capture_stop), stub_capture_stop_cb, NULL);
		/* assemble image and submit it to library */
		assemble_and_submit_image(dev);
		fpi_imgdev_report_finger_status(dev, FALSE);
		/* marking machine complete will re-trigger finger detection loop */
		fpi_ssm_mark_completed(ssm);
		/* Acquisition finished: restore default gain values */
		restore_gain();
	} else {
		/* obtain next strip */
		fpi_ssm_jump_to_state(ssm, CAPTURE_REQUEST_STRIP);
	}

out:
	g_free(data);
	libusb_free_transfer(transfer);
}

static void capture_run_state(struct fpi_ssm *ssm)
{
	struct fp_img_dev *dev = ssm->priv;
	struct aes1610_dev *aesdev = dev->priv;
	int r;

	switch (ssm->cur_state) {
	case CAPTURE_WRITE_REQS:
		fp_dbg("write reqs");
		aes_write_regv(dev, capture_reqs, G_N_ELEMENTS(capture_reqs),
			generic_write_regv_cb, ssm);
		break;
	case CAPTURE_READ_DATA:
		fp_dbg("read data");
		generic_read_ignore_data(ssm, 665);
		break;
	case CAPTURE_REQUEST_STRIP:
		fp_dbg("request strip");
		if (aesdev->deactivating)
			fpi_ssm_mark_completed(ssm);
919
		else
920 921 922 923 924 925 926 927 928
			aes_write_regv(dev, strip_scan_reqs, G_N_ELEMENTS(strip_scan_reqs),
				generic_write_regv_cb, ssm);
		break;
	case CAPTURE_READ_STRIP: ;
		struct libusb_transfer *transfer = libusb_alloc_transfer(0);
		unsigned char *data;

		if (!transfer) {
			fpi_ssm_mark_aborted(ssm, -ENOMEM);
929
			break;
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
		}

		data = g_malloc(665);
		libusb_fill_bulk_transfer(transfer, dev->udev, EP_IN, data, 665,
			capture_read_strip_cb, ssm, BULK_TIMEOUT);

		r = libusb_submit_transfer(transfer);
		if (r < 0) {
			g_free(data);
			libusb_free_transfer(transfer);
			fpi_ssm_mark_aborted(ssm, r);
		}
		break;
	};
}

static void capture_sm_complete(struct fpi_ssm *ssm)
{
	struct fp_img_dev *dev = ssm->priv;
	struct aes1610_dev *aesdev = dev->priv;

	fp_dbg("");
	if (aesdev->deactivating)
		complete_deactivation(dev);
	else if (ssm->error)
		fpi_imgdev_session_error(dev, ssm->error);
	else
		start_finger_detection(dev);
	fpi_ssm_free(ssm);
}

static void start_capture(struct fp_img_dev *dev)
{
	struct aes1610_dev *aesdev = dev->priv;
	struct fpi_ssm *ssm;

	if (aesdev->deactivating) {
		complete_deactivation(dev);
		return;
969 970
	}

971 972 973 974 975
	ssm = fpi_ssm_new(dev->dev, capture_run_state, CAPTURE_NUM_STATES);
	fp_dbg("");
	ssm->priv = dev;
	fpi_ssm_start(ssm, capture_sm_complete);
}
976

977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
/****** INITIALIZATION/DEINITIALIZATION ******/

static const struct aes_regwrite init[] = {
	{ 0x82, 0x00 }
};

static const struct aes_regwrite stop_reader[] = {
	{ 0xFF, 0x00 }
};


enum activate_states {
	WRITE_INIT,
//	READ_DATA,
//	READ_REGS,
	ACTIVATE_NUM_STATES,
};

/* this come from aes2501 and is unused here
void activate_read_regs_cb(struct fp_img_dev *dev, int status,
	unsigned char *regs, void *user_data)
{
	struct fpi_ssm *ssm = user_data;
	struct aes1610_dev *aesdev = dev->priv;

	if (status != 0) {
		fpi_ssm_mark_aborted(ssm, status);
1004
	} else {
1005
		fpi_ssm_next_state(ssm);
1006
	}
1007 1008
}
*/
1009

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
static void activate_run_state(struct fpi_ssm *ssm)
{
	struct fp_img_dev *dev = ssm->priv;

	/* activation on aes1610 seems much more straightforward compared to aes2501 */
	/* verify theres anything missing here */
	switch (ssm->cur_state) {
	case WRITE_INIT:
		fp_dbg("write init");
		aes_write_regv(dev, init, G_N_ELEMENTS(init), generic_write_regv_cb, ssm);
		break;
/*	case READ_DATA:
		fp_dbg("read data");
		generic_read_ignore_data(ssm, 20);
		break;
	case READ_REGS:
		fp_dbg("read regs");
		read_regs(dev, activate_read_regs_cb, ssm);
		break;*/
	}
}

/* jump to finger detection */
static void activate_sm_complete(struct fpi_ssm *ssm)
{
	struct fp_img_dev *dev = ssm->priv;
	fp_dbg("status %d", ssm->error);
	fpi_imgdev_activate_complete(dev, ssm->error);

	if (!ssm->error)
		start_finger_detection(dev);
	fpi_ssm_free(ssm);
}

static int dev_activate(struct fp_img_dev *dev, enum fp_imgdev_state state)
{
	struct aes1610_dev *aesdev = dev->priv;
	struct fpi_ssm *ssm = fpi_ssm_new(dev->dev, activate_run_state,
		ACTIVATE_NUM_STATES);
	ssm->priv = dev;
	aesdev->read_regs_retry_count = 0;
	fpi_ssm_start(ssm, activate_sm_complete);
1052
	return 0;
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
}

static void dev_deactivate(struct fp_img_dev *dev)
{
	struct aes1610_dev *aesdev = dev->priv;
	/* FIXME: audit cancellation points, probably need more, specifically
	 * in error handling paths? */
	aesdev->deactivating = TRUE;
}

static void complete_deactivation(struct fp_img_dev *dev)
{
	struct aes1610_dev *aesdev = dev->priv;
	fp_dbg("");

	/* FIXME: if we're in the middle of a scan, we should cancel the scan.
	 * maybe we can do this with a master reset, unconditionally? */

	aesdev->deactivating = FALSE;
	g_slist_free(aesdev->strips);
	aesdev->strips = NULL;
	aesdev->strips_len = 0;
	aesdev->blanks_count = 0;
	fpi_imgdev_deactivate_complete(dev);
}

static int dev_init(struct fp_img_dev *dev, unsigned long driver_data)
{
	/* FIXME check endpoints */
	int r;

	r = libusb_claim_interface(dev->udev, 0);
	if (r < 0) {
		fp_err("could not claim interface 0");
		return r;
	}

	dev->priv = g_malloc0(sizeof(struct aes1610_dev));
	fpi_imgdev_open_complete(dev, 0);
	return 0;
}

static void dev_deinit(struct fp_img_dev *dev)
{
	g_free(dev->priv);
	libusb_release_interface(dev->udev, 0);
	fpi_imgdev_close_complete(dev);
1100 1101 1102
}

static const struct usb_id id_table[] = {
1103
	{ .vendor = 0x08ff, .product = 0x1600 }, /* AES1600 */
1104 1105 1106 1107 1108 1109 1110 1111 1112
	{ 0, 0, 0, },
};

struct fp_img_driver aes1610_driver = {
	.driver = {
		.id = 6,
		.name = FP_COMPONENT,
		.full_name = "AuthenTec AES1610",
		.id_table = id_table,
1113
		.scan_type = FP_SCAN_TYPE_SWIPE,
1114 1115 1116 1117 1118 1119 1120 1121 1122
	},
	.flags = 0,
	.img_height = -1,
	.img_width = 128,

	/* temporarily lowered until we sort out image processing code
	 * binarized scan quality is good, minutiae detection is accurate,
	 * it's just that we get fewer minutiae than other scanners (less scanning
	 * area) */
1123
	.bz3_threshold = 10,
1124

1125 1126 1127 1128
	.open = dev_init,
	.close = dev_deinit,
	.activate = dev_activate,
	.deactivate = dev_deactivate,
1129 1130
};