aes1610.c 13.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 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 517 518 519 520 521 522 523 524 525 526 527 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
/*
 * AuthenTec AES1610 driver for libfprint
 * Copyright (C) 2007 Anthony Bretaudeau <wxcover@users.sourceforge.net>
 * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
 * Copyright (C) 2007 Cyrille Bagard
 * Copyright (C) 2007 Vasily Khoruzhick
 *
 * Based on code from http://home.gna.org/aes2501, relicensed with permission
 *
 * 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>

#include <usb.h>

#include <fp_internal.h>

/* FIXME these need checking */
#define EP_IN			(1 | USB_ENDPOINT_IN)
#define EP_OUT			(2 | USB_ENDPOINT_OUT)

#define BULK_TIMEOUT 4000
#define MAX_REGWRITES_PER_REQUEST	16

#define FIRST_AES1610_REG	0x1B
#define LAST_AES1610_REG	0xFF

/*
 * 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.
 */

#define FRAME_WIDTH		128
#define FRAME_HEIGHT	8
#define FRAME_SIZE		(FRAME_WIDTH * FRAME_HEIGHT)
/* maximum number of frames to read during a scan */
/* FIXME reduce substantially */
#define MAX_FRAMES		150

struct aes1610_regwrite {
	unsigned char reg;
	unsigned char value;
};

static int do_write_regv(struct fp_img_dev *dev, const struct aes1610_regwrite *regs,
	unsigned int num)
{
	size_t alloc_size = num * 2;
	unsigned char *data = g_malloc(alloc_size);
	unsigned int i;
	size_t offset = 0;
	int r;

	for (i = 0; i < num; i++) {
		data[offset++] = regs[i].reg;
		data[offset++] = regs[i].value;
	}

	r = usb_bulk_write(dev->udev, EP_OUT, data, alloc_size, BULK_TIMEOUT);
	g_free(data);
	if (r < 0) {
		fp_err("bulk write error %d", r);
		return r;
	} else if (r < (int) alloc_size) {
		fp_err("unexpected short write %d/%d", r, alloc_size);
		return -EIO;
	}

	return 0;
}

static int write_regv(struct fp_img_dev *dev, const struct aes1610_regwrite *regs,
	unsigned int num)
{
	unsigned int i;
	int skip = 0;
	int add_offset = 0;
	fp_dbg("write %d regs", num);

	for (i = 0; i < num; i += add_offset + skip) {
		int r, j;
		int limit = MIN(num, i + MAX_REGWRITES_PER_REQUEST);
		skip = 0;

		for (j = i; j < limit; j++)
			if (!regs[j].reg) {
				skip = 1;
				break;
			}

		add_offset = j - i;
		r = do_write_regv(dev, &regs[i], add_offset);
		if (r < 0)
			return r;
	}

	return 0;
}

static int read_data(struct fp_img_dev *dev, unsigned char *data, size_t len)
{
	int r;
	fp_dbg("len=%zd", len);

	r = usb_bulk_read(dev->udev, EP_IN, data, len, BULK_TIMEOUT);
	if (r < 0) {
		fp_err("bulk read error %d", r);
		return r;
	} else if (r < (int) len) {
		fp_err("unexpected short read %d/%zd", r, len);
		return -EIO;
	}
	return 0;
}

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

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

static int do_init(struct fp_img_dev *dev)
{
	return write_regv(dev, init, G_N_ELEMENTS(init));
}

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

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

	/* FIXME check endpoints */

	return do_init(dev);
}

static int do_exit(struct fp_img_dev *dev)
{
	return write_regv(dev, stop_reader, G_N_ELEMENTS(stop_reader));
}

static void dev_exit(struct fp_img_dev *dev)
{
	do_exit(dev);
	usb_release_interface(dev->udev, 0);
}

static const struct aes1610_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 aes1610_regwrite finger_det_none[] = {
	{ 0x80, 0x01 },
	{ 0x82, 0x00 }, 
	{ 0x86, 0x00 }, 
	{ 0xB1, 0x28 }, 
	{ 0x1D, 0x00 }
};

static int detect_finger(struct fp_img_dev *dev)
{
	unsigned char buffer[19];
	int r;
	int i;
	int sum = 0;

	r = write_regv(dev, finger_det_reqs, G_N_ELEMENTS(finger_det_reqs));
	if (r < 0)
		return r;

	r = read_data(dev, buffer, 19);
	if (r < 0)
		return r;

	for (i = 3; i < 17; i++)
		sum += (buffer[i] & 0xf) + (buffer[i] >> 4);
		
	/* We need to answer something if no finger has been detected */
	if (sum <= 20) {
		r = write_regv(dev, finger_det_none, G_N_ELEMENTS(finger_det_none));
		if (r < 0)
			return r;
	}
	
	return sum > 20;
}

static int await_finger_on(struct fp_img_dev *dev)
{
	int r;
	do {
		r = detect_finger(dev);
	} while (r == 0);
	return (r < 0) ? r : 0;
}

/* Read the value of a specific register from a register dump */
static int regval_from_dump(unsigned char *data, uint8_t target)
{
	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];
}

/* find overlapping parts of frames */
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 */
			error += first_frame[i] > second_frame[i] ? 
					(first_frame[i] - second_frame[i]) :
					(second_frame[i] - first_frame[i]); 
		}
		
		/* Normalize error */
		error *= 15;
		error /= i;
		if (error < *min_error) {
			*min_error = error;
			not_overlapped_height = dy;
		}
		first_frame += FRAME_WIDTH;
	}
	
	return not_overlapped_height; 
}

/* assemble a series of frames into a single image */
static unsigned int assemble(unsigned char *input, unsigned char *output,
	int num_strips, gboolean reverse, unsigned int *errors_sum)
{
	uint8_t *assembled = output;
	int frame;
	uint32_t image_height = FRAME_HEIGHT;
	unsigned int min_error;
	*errors_sum = 0;

	if (num_strips < 1)
		return 0;
	
	/* Rotating given data by 90 degrees 
	 * Taken from document describing aes1610 image format
	 * TODO: move reversing detection here */
	
	if (reverse)
		output += (num_strips - 1) * FRAME_SIZE;
	for (frame = 0; frame < num_strips; frame++) {
		int column;
		for (column = 0; column < FRAME_WIDTH; column++) {
			int row;
			for (row = 0; row < (FRAME_HEIGHT / 2); row++) {
				output[FRAME_WIDTH * ( 2 * row) + column] = *input & 0x0F;
				output[FRAME_WIDTH * ( 2 * row + 1) + column] = *input >> 4;
				input++;
			}
		}

		if (reverse)
			output -= FRAME_SIZE;
		else
			output += FRAME_SIZE;
	}

	/* 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;
		memcpy(assembled, output, FRAME_SIZE); 
	}
	return image_height;
}

static const struct aes1610_regwrite capture_reqs[] = {
	{ 0x80, 0x01 },
	{ 0x80, 0x12 },
	{ 0x84, 0x01 },
	{ 0x85, 0x00 },
	{ 0x89, 0x64 },
	{ 0x8A, 0x00 },
	{ 0x8B, 0x0E },
	{ 0x8C, 0x90 },
	{ 0xBE, 0x23 },
	{ 0x29, 0x06 },
	{ 0x2A, 0x35 },
	{ 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 },
	{ 0x1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 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 }
};

static const struct aes1610_regwrite strip_scan_reqs[] = {
	{ 0xBE, 0x23 },
	{ 0x29, 0x06 },
	{ 0x2A, 0x35 },
	{ 0xBD, 0x4F },
	{ 0xFF, 0x00 }
};

static const struct aes1610_regwrite capture_stop[] = {
	{ 0x81,0x00 }
};

static int capture(struct fp_img_dev *dev, gboolean unconditional,
	struct fp_img **ret)
{
	int r;
	struct fp_img *img;
	unsigned int nstrips;
	unsigned int errors_sum, r_errors_sum;
	unsigned char *cooked;
	unsigned char *imgptr;
	unsigned char buf[665];
	int sum;
	int i;

	/* FIXME can do better here in terms of buffer management? */
	fp_dbg("");

	r = write_regv(dev, capture_reqs, G_N_ELEMENTS(capture_reqs));
	if (r < 0)
		return r;
  
	/* FIXME: use histogram data above for gain calibration (0x8e xx) */

	img = fpi_img_new((3 * MAX_FRAMES * FRAME_SIZE) / 2);
	imgptr = img->data;
	cooked = imgptr + (MAX_FRAMES * FRAME_SIZE) / 2;

	r = read_data(dev, buf, 665);
	if (r < 0)
		goto err;
	memcpy(imgptr, buf + 1, 128*4);
	imgptr += 128*4;

	r = read_data(dev, buf, 665);
	if (r < 0)
		goto err;
	memcpy(imgptr, buf + 1, 128*4);
	imgptr += 128*4;

	for (nstrips = 0; nstrips < MAX_FRAMES; nstrips++) {
		int threshold;

		r = write_regv(dev, strip_scan_reqs, G_N_ELEMENTS(strip_scan_reqs));
		if (r < 0)
			goto err;
		r = read_data(dev, buf, 665);
		if (r < 0)
			goto err;
		memcpy(imgptr, buf + 1, 128*4);
		imgptr += 128*4;

		r = read_data(dev, buf, 665);
		if (r < 0)
			goto err;
		memcpy(imgptr, buf + 1, 128*4);
		imgptr += 128*4;

		sum = 0;
		for (i = 515; 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 += buf[i];
		}
		if (sum < 0) {
			r = sum;
			goto err;
		}
		fp_dbg("sum=%d", sum);
		if (sum == 0)
			break;
	}
	
	r = write_regv(dev, capture_stop, G_N_ELEMENTS(capture_stop));
	if (r < 0)
		goto err;
	r = read_data(dev, buf, 665);
	if (r < 0)
		goto err;
	memcpy(imgptr, buf + 1, 128*4);
	imgptr += 128*4;

	r = read_data(dev, buf, 665);
	if (r < 0)
		goto err;
	memcpy(imgptr, buf + 1, 128*4);
	imgptr += 128*4;
	
	if (nstrips == MAX_FRAMES)
		fp_warn("swiping finger too slow?");

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

	for (i = 0; i < img->height * FRAME_WIDTH; i++)
		img->data[i] = (cooked[i] << 4) | 0xf;

	img = fpi_img_resize(img, img->height * FRAME_WIDTH);
	*ret = img;
	return 0;
err:
	fp_img_free(img);
	return r;
}

static const struct usb_id id_table[] = {
	{ .vendor = 0x08ff, .product = 0x1600 },
	{ 0, 0, 0, },
};

struct fp_img_driver aes1610_driver = {
	.driver = {
		.id = 6,
		.name = FP_COMPONENT,
		.full_name = "AuthenTec AES1610",
		.id_table = id_table,
	},
	.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) */
	.bz3_threshold = 15,

	.init = dev_init,
	.exit = dev_exit,
	.await_finger_on = await_finger_on,
	.capture = capture,
};