vfs0050.c 20.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Validity VFS0050 driver for libfprint
 * Copyright (C) 2015-2016 Konstantin Semenov <zemen17@gmail.com>
 *
 * 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 "vfs0050"

22
#include "drivers_api.h"
23 24 25 26 27 28 29
#include "vfs0050.h"

/* USB functions */

/* Callback for async_write */
static void async_write_callback(struct libusb_transfer *transfer)
{
Bastien Nocera's avatar
Bastien Nocera committed
30
	fpi_ssm *ssm = transfer->user_data;
31
	struct fp_img_dev *idev = fpi_ssm_get_user_data(ssm);
32 33 34 35 36 37 38

	int transferred = transfer->actual_length, error =
	    transfer->status, len = transfer->length;

	if (error != 0) {
		fp_err("USB write transfer: %s", libusb_error_name(error));
		fpi_imgdev_session_error(idev, -EIO);
39
		fpi_ssm_mark_failed(ssm, -EIO);
40 41 42 43 44 45
		return;
	}

	if (transferred != len) {
		fp_err("Written only %d of %d bytes", transferred, len);
		fpi_imgdev_session_error(idev, -EIO);
46
		fpi_ssm_mark_failed(ssm, -EIO);
47 48 49 50 51 52 53
		return;
	}

	fpi_ssm_next_state(ssm);
}

/* Send data to EP1, the only out endpoint */
Bastien Nocera's avatar
Bastien Nocera committed
54
static void async_write(fpi_ssm *ssm, void *data, int len)
55
{
56
	struct fp_img_dev *idev = fpi_ssm_get_user_data(ssm);
57
	struct libusb_device_handle *usb_dev = fpi_dev_get_usb_dev(FP_DEV(idev));
58
	struct vfs_dev_t *vdev = FP_INSTANCE_DATA(FP_DEV(idev));
59 60 61

	vdev->transfer = libusb_alloc_transfer(0);
	vdev->transfer->flags |= LIBUSB_TRANSFER_FREE_TRANSFER;
62
	libusb_fill_bulk_transfer(vdev->transfer, usb_dev, 0x01, data, len,
63 64 65 66 67 68 69
				  async_write_callback, ssm, VFS_USB_TIMEOUT);
	libusb_submit_transfer(vdev->transfer);
}

/* Callback for async_read */
static void async_read_callback(struct libusb_transfer *transfer)
{
Bastien Nocera's avatar
Bastien Nocera committed
70
	fpi_ssm *ssm = transfer->user_data;
71
	struct fp_img_dev *idev = fpi_ssm_get_user_data(ssm);
72 73 74 75 76 77 78 79 80

	int transferred = transfer->actual_length, error =
	    transfer->status, len = transfer->length;
	int ep = transfer->endpoint;

	if (error != 0) {
		fp_err("USB read transfer on endpoint %d: %s", ep - 0x80,
		       libusb_error_name(error));
		fpi_imgdev_session_error(idev, -EIO);
81
		fpi_ssm_mark_failed(ssm, -EIO);
82 83 84 85 86 87
		return;
	}

	if (transferred != len) {
		fp_err("Received %d instead of %d bytes", transferred, len);
		fpi_imgdev_session_error(idev, -EIO);
88
		fpi_ssm_mark_failed(ssm, -EIO);
89 90 91 92 93 94 95
		return;
	}

	fpi_ssm_next_state(ssm);
}

/* Receive data from the given ep and compare with expected */
Bastien Nocera's avatar
Bastien Nocera committed
96
static void async_read(fpi_ssm *ssm, int ep, void *data, int len)
97
{
98
	struct fp_img_dev *idev = fpi_ssm_get_user_data(ssm);
99
	struct libusb_device_handle *usb_dev = fpi_dev_get_usb_dev(FP_DEV(idev));
100
	struct vfs_dev_t *vdev = FP_INSTANCE_DATA(FP_DEV(idev));
101 102 103 104 105 106 107 108

	ep |= LIBUSB_ENDPOINT_IN;

	vdev->transfer = libusb_alloc_transfer(0);
	vdev->transfer->flags |= LIBUSB_TRANSFER_FREE_TRANSFER;

	/* 0x83 is the only interrupt endpoint */
	if (ep == EP3_IN)
109
		libusb_fill_interrupt_transfer(vdev->transfer, usb_dev, ep, data,
110 111 112
					       len, async_read_callback, ssm,
					       VFS_USB_TIMEOUT);
	else
113
		libusb_fill_bulk_transfer(vdev->transfer, usb_dev, ep, data, len,
114 115 116 117 118 119 120 121
					  async_read_callback, ssm,
					  VFS_USB_TIMEOUT);
	libusb_submit_transfer(vdev->transfer);
}

/* Callback for async_read */
static void async_abort_callback(struct libusb_transfer *transfer)
{
Bastien Nocera's avatar
Bastien Nocera committed
122
	fpi_ssm *ssm = transfer->user_data;
123
	struct fp_img_dev *idev = fpi_ssm_get_user_data(ssm);
124 125 126 127 128 129 130 131 132 133 134 135 136

	int transferred = transfer->actual_length, error = transfer->status;
	int ep = transfer->endpoint;

	/* In normal case endpoint is empty */
	if (error == LIBUSB_TRANSFER_TIMED_OUT) {
		fpi_ssm_next_state(ssm);
		return;
	}

	if (error != 0) {
		fp_err("USB write transfer: %s", libusb_error_name(error));
		fpi_imgdev_session_error(idev, -EIO);
137
		fpi_ssm_mark_failed(ssm, -EIO);
138 139 140 141 142 143 144 145
		return;
	}

	/* Don't stop process, only print warning */
	if (transferred > 0)
		fp_warn("Endpoint %d had extra %d bytes", ep - 0x80,
			transferred);

146
	fpi_ssm_jump_to_state(ssm, fpi_ssm_get_cur_state(ssm));
147 148 149
}

/* Receive data from the given ep and compare with expected */
Bastien Nocera's avatar
Bastien Nocera committed
150
static void async_abort(fpi_ssm *ssm, int ep)
151
{
152
	struct fp_img_dev *idev = fpi_ssm_get_user_data(ssm);
153
	struct libusb_device_handle *usb_dev = fpi_dev_get_usb_dev(FP_DEV(idev));
154
	struct vfs_dev_t *vdev = FP_INSTANCE_DATA(FP_DEV(idev));
155 156 157 158 159 160 161 162 163 164 165 166

	int len = VFS_USB_BUFFER_SIZE;
	unsigned char *data = g_malloc(VFS_USB_BUFFER_SIZE);

	ep |= LIBUSB_ENDPOINT_IN;

	vdev->transfer = libusb_alloc_transfer(0);
	vdev->transfer->flags |=
	    LIBUSB_TRANSFER_FREE_TRANSFER | LIBUSB_TRANSFER_FREE_BUFFER;

	/* 0x83 is the only interrupt endpoint */
	if (ep == EP3_IN)
167
		libusb_fill_interrupt_transfer(vdev->transfer, usb_dev, ep, data,
168 169 170
					       len, async_abort_callback, ssm,
					       VFS_USB_ABORT_TIMEOUT);
	else
171
		libusb_fill_bulk_transfer(vdev->transfer, usb_dev, ep, data, len,
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
					  async_abort_callback, ssm,
					  VFS_USB_ABORT_TIMEOUT);
	libusb_submit_transfer(vdev->transfer);
}

/* Image processing functions */

/* Pixel getter for fpi_assemble_lines */
static unsigned char vfs0050_get_pixel(struct fpi_line_asmbl_ctx *ctx,
				       GSList * line, unsigned int x)
{
	return ((struct vfs_line *)line->data)->data[x];
}

/* Deviation getter for fpi_assemble_lines */
static int vfs0050_get_difference(struct fpi_line_asmbl_ctx *ctx,
				  GSList * line_list_1, GSList * line_list_2)
{
	struct vfs_line *line1 = line_list_1->data;
	struct vfs_line *line2 = line_list_2->data;
	const int shift = (VFS_IMAGE_WIDTH - VFS_NEXT_LINE_WIDTH) / 2 - 1;
	int res = 0;
	for (int i = 0; i < VFS_NEXT_LINE_WIDTH; ++i) {
		int x =
		    (int)line1->next_line_part[i] - (int)line2->data[shift + i];
		res += x * x;
	}
	return res;
}

#define VFS_NOISE_THRESHOLD 40

/* Checks whether line is noise or not using hardware parameters */
static char is_noise(struct vfs_line *line)
{
	int val1 = line->noise_hash_1;
	int val2 = line->noise_hash_2;
	if (val1 > VFS_NOISE_THRESHOLD
	    && val1 < 256 - VFS_NOISE_THRESHOLD
	    && val2 > VFS_NOISE_THRESHOLD && val2 < 256 - VFS_NOISE_THRESHOLD)
		return 1;
	return 0;
}

/* Parameters for fpi_assemble_lines */
static struct fpi_line_asmbl_ctx assembling_ctx = {
	.line_width = VFS_IMAGE_WIDTH,
	.max_height = VFS_MAX_HEIGHT,
	.resolution = 10,
	.median_filter_size = 25,
	.max_search_offset = 100,
	.get_deviation = vfs0050_get_difference,
	.get_pixel = vfs0050_get_pixel,
};

/* Processes image before submitting */
static struct fp_img *prepare_image(struct vfs_dev_t *vdev)
{
	int height = vdev->bytes / VFS_LINE_SIZE;

	/* Noise cleaning. IMHO, it works pretty well
	   I've not detected cases when it doesn't work or cuts a part of the finger
	   Noise arises at the end of scan when some water remains on the scanner */
	while (height > 0) {
		if (!is_noise(vdev->lines_buffer + height - 1))
			break;
		--height;
	}
	if (height > VFS_MAX_HEIGHT)
		height = VFS_MAX_HEIGHT;

	/* If image is not good enough */
	if (height < VFS_IMAGE_WIDTH)
		return NULL;

	/* Building GSList */
	GSList *lines = NULL;
	for (int i = height - 1; i >= 0; --i)
		lines = g_slist_prepend(lines, vdev->lines_buffer + i);

	/* Perform line assembling */
	struct fp_img *img = fpi_assemble_lines(&assembling_ctx, lines, height);

	g_slist_free(lines);
	return img;
}

/* Processes and submits image after fingerprint received */
static void submit_image(struct fp_img_dev *idev)
{
262
	struct vfs_dev_t *vdev = FP_INSTANCE_DATA(FP_DEV(idev));
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

	/* We were not asked to submit image actually */
	if (!vdev->active)
		return;

	struct fp_img *img = prepare_image(vdev);

	if (!img)
		fpi_imgdev_abort_scan(idev, FP_VERIFY_RETRY_TOO_SHORT);
	else
		fpi_imgdev_image_captured(idev, img);

	/* Finger not on the scanner */
	fpi_imgdev_report_finger_status(idev, 0);
}

/* Proto functions */

/* SSM loop for clear_ep2 */
282
static void clear_ep2_ssm(fpi_ssm *ssm, struct fp_dev *_dev, void *user_data)
283
{
284
	struct fp_img_dev *idev = fpi_ssm_get_user_data(ssm);
285 286 287 288

	short result;
	char command04 = 0x04;

289
	switch (fpi_ssm_get_cur_state(ssm)) {
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
	case SUBSM1_COMMAND_04:
		async_write(ssm, &command04, sizeof(command04));
		break;

	case SUBSM1_RETURN_CODE:
		async_read(ssm, 1, &result, sizeof(result));
		break;

	case SUBSM1_ABORT_2:
		async_abort(ssm, 2);
		break;

	default:
		fp_err("Unknown SUBSM1 state");
		fpi_imgdev_session_error(idev, -EIO);
305
		fpi_ssm_mark_failed(ssm, -EIO);
306 307 308 309
	}
}

/* Send command to clear EP2 */
Bastien Nocera's avatar
Bastien Nocera committed
310
static void clear_ep2(fpi_ssm *ssm)
311
{
312
	struct fp_img_dev *idev = fpi_ssm_get_user_data(ssm);
313

Bastien Nocera's avatar
Bastien Nocera committed
314
	fpi_ssm *subsm =
315
	    fpi_ssm_new(FP_DEV(idev), clear_ep2_ssm, SUBSM1_STATES, idev);
316 317 318
	fpi_ssm_start_subsm(ssm, subsm);
}

319
static void send_control_packet_ssm(fpi_ssm *ssm, struct fp_dev *_dev, void *user_data)
320
{
321
	struct fp_img_dev *idev = fpi_ssm_get_user_data(ssm);
322
	struct vfs_dev_t *vdev = FP_INSTANCE_DATA(FP_DEV(idev));
323 324 325 326

	short result;
	unsigned char *commit_result = NULL;

327
	switch (fpi_ssm_get_cur_state(ssm)) {
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
	case SUBSM2_SEND_CONTROL:
		async_write(ssm, vdev->control_packet, VFS_CONTROL_PACKET_SIZE);
		break;

	case SUBSM2_RETURN_CODE:
		async_read(ssm, 1, &result, sizeof(result));
		break;

	case SUBSM2_SEND_COMMIT:
		/* next_receive_* packets could be sent only in pair */
		if (vdev->control_packet == next_receive_1) {
			vdev->control_packet = next_receive_2;
			fpi_ssm_jump_to_state(ssm, SUBSM2_SEND_CONTROL);
			break;
		}
		/* commit_out in Windows differs in each commit, but I send the same each time */
		async_write(ssm, commit_out, sizeof(commit_out));
		break;

	case SUBSM2_COMMIT_RESPONSE:
		commit_result = g_malloc(VFS_COMMIT_RESPONSE_SIZE);
		async_read(ssm, 1, commit_result, VFS_COMMIT_RESPONSE_SIZE);
		break;

	case SUBSM2_READ_EMPTY_INTERRUPT:
		/* I don't know how to check result, it could be different */
		g_free(commit_result);

		async_read(ssm, 3, vdev->interrupt, VFS_INTERRUPT_SIZE);
		break;

	case SUBSM2_ABORT_3:
		/* Check that interrupt is empty */
		if (memcmp
		    (vdev->interrupt, empty_interrupt, VFS_INTERRUPT_SIZE)) {
			fp_err("Unknown SUBSM2 state");
			fpi_imgdev_session_error(idev, -EIO);
365
			fpi_ssm_mark_failed(ssm, -EIO);
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
			break;
		}
		async_abort(ssm, 3);
		break;

	case SUBSM2_CLEAR_EP2:
		/* After turn_on Windows doesn't clear EP2 */
		if (vdev->control_packet != turn_on)
			clear_ep2(ssm);
		else
			fpi_ssm_next_state(ssm);
		break;

	default:
		fp_err("Unknown SUBSM2 state");
		fpi_imgdev_session_error(idev, -EIO);
382
		fpi_ssm_mark_failed(ssm, -EIO);
383 384 385 386
	}
}

/* Send device state control packet */
Bastien Nocera's avatar
Bastien Nocera committed
387
static void send_control_packet(fpi_ssm *ssm)
388
{
389
	struct fp_img_dev *idev = fpi_ssm_get_user_data(ssm);
390

Bastien Nocera's avatar
Bastien Nocera committed
391
	fpi_ssm *subsm =
392
	    fpi_ssm_new(FP_DEV(idev), send_control_packet_ssm, SUBSM2_STATES, idev);
393 394 395 396 397 398 399 400 401 402 403 404 405 406
	fpi_ssm_start_subsm(ssm, subsm);
}

/* Clears all fprint data */
static void clear_data(struct vfs_dev_t *vdev)
{
	g_free(vdev->lines_buffer);
	vdev->lines_buffer = NULL;
	vdev->memory = vdev->bytes = 0;
}

/* After receiving interrupt from EP3 */
static void interrupt_callback(struct libusb_transfer *transfer)
{
Bastien Nocera's avatar
Bastien Nocera committed
407
	fpi_ssm *ssm = transfer->user_data;
408
	struct fp_img_dev *idev = fpi_ssm_get_user_data(ssm);
409
	struct vfs_dev_t *vdev = FP_INSTANCE_DATA(FP_DEV(idev));
410 411 412 413 414 415 416 417 418 419 420 421 422 423

	char *interrupt = vdev->interrupt;
	int error = transfer->status, transferred = transfer->actual_length;

	vdev->wait_interrupt = 0;

	/* When we have cancelled transfer, error is ok actually */
	if (!vdev->active && error == LIBUSB_TRANSFER_CANCELLED)
		return;

	if (error != 0) {
		fp_err("USB read interrupt transfer: %s",
		       libusb_error_name(error));
		fpi_imgdev_session_error(idev, -EIO);
424
		fpi_ssm_mark_failed(ssm, -EIO);
425 426 427 428 429 430 431 432
		return;
	}

	/* Interrupt size is VFS_INTERRUPT_SIZE bytes in all known cases */
	if (transferred != VFS_INTERRUPT_SIZE) {
		fp_err("Unknown interrupt size %d", transferred);
		/* Abort ssm */
		fpi_imgdev_session_error(idev, -EIO);
433
		fpi_ssm_mark_failed(ssm, -EIO);
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
		return;
	}

	/* Standard interrupts */
	if (memcmp(interrupt, interrupt1, VFS_INTERRUPT_SIZE) == 0 ||
	    memcmp(interrupt, interrupt2, VFS_INTERRUPT_SIZE) == 0 ||
	    memcmp(interrupt, interrupt3, VFS_INTERRUPT_SIZE) == 0) {
		/* Go to the next ssm stage */
		fpi_ssm_next_state(ssm);
		return;
	}

	/* When finger is on the scanner before turn_on */
	if (interrupt[0] == 0x01) {
		fp_warn("Finger is already on the scanner");

		/* Go to the next ssm stage */
		fpi_ssm_next_state(ssm);
		return;
	}

	/* Unknown interrupt; abort the session */
	fp_err("Unknown interrupt '%02x:%02x:%02x:%02x:%02x'!",
	       interrupt[0] & 0xff, interrupt[1] & 0xff, interrupt[2] & 0xff,
	       interrupt[3] & 0xff, interrupt[4] & 0xff);

	/* Abort ssm */
	fpi_imgdev_session_error(idev, -EIO);
462
	fpi_ssm_mark_failed(ssm, -EIO);
463 464 465 466
}

static void receive_callback(struct libusb_transfer *transfer)
{
Bastien Nocera's avatar
Bastien Nocera committed
467
	fpi_ssm *ssm = transfer->user_data;
468
	struct fp_img_dev *idev = fpi_ssm_get_user_data(ssm);
469
	struct vfs_dev_t *vdev = FP_INSTANCE_DATA(FP_DEV(idev));
470 471 472 473 474 475 476

	int transferred = transfer->actual_length, error = transfer->status;

	if (error != 0 && error != LIBUSB_TRANSFER_TIMED_OUT) {
		fp_err("USB read transfer: %s", libusb_error_name(error));

		fpi_imgdev_session_error(idev, -EIO);
477
		fpi_ssm_mark_failed(ssm, -EIO);
478 479 480 481 482 483 484 485 486 487
		return;
	}

	/* Check if fingerprint data is over */
	if (transferred == 0) {
		fpi_ssm_next_state(ssm);
	} else {
		vdev->bytes += transferred;

		/* We need more data */
488
		fpi_ssm_jump_to_state(ssm, fpi_ssm_get_cur_state(ssm));
489 490 491 492 493 494
	}
}

/* Stub to keep SSM alive when waiting an interrupt */
static void wait_interrupt(void *data)
{
Bastien Nocera's avatar
Bastien Nocera committed
495
	fpi_ssm *ssm = data;
496
	struct fp_img_dev *idev = fpi_ssm_get_user_data(ssm);
497
	struct vfs_dev_t *vdev = FP_INSTANCE_DATA(FP_DEV(idev));
498 499 500

	/* Keep sleeping while this flag is on */
	if (vdev->wait_interrupt)
501
		fpi_ssm_jump_to_state(ssm, fpi_ssm_get_cur_state(ssm));
502 503 504 505 506
}

/* SSM stub to prepare device to another scan after orange light was on */
static void another_scan(void *data)
{
Bastien Nocera's avatar
Bastien Nocera committed
507
	fpi_ssm *ssm = data;
508 509 510 511 512 513
	fpi_ssm_jump_to_state(ssm, SSM_TURN_ON);
}

/* Another SSM stub to continue after waiting for probable vdev->active changes */
static void scan_completed(void *data)
{
Bastien Nocera's avatar
Bastien Nocera committed
514
	fpi_ssm *ssm = data;
515 516 517 518
	fpi_ssm_next_state(ssm);
}

/* Main SSM loop */
519
static void activate_ssm(fpi_ssm *ssm, struct fp_dev *_dev, void *user_data)
520
{
521
	struct fp_img_dev *idev = fpi_ssm_get_user_data(ssm);
522
	struct libusb_device_handle *usb_dev = fpi_dev_get_usb_dev(FP_DEV(idev));
523
	struct vfs_dev_t *vdev = FP_INSTANCE_DATA(FP_DEV(idev));
524

525
	switch (fpi_ssm_get_cur_state(ssm)) {
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
	case SSM_INITIAL_ABORT_1:
		async_abort(ssm, 1);
		break;

	case SSM_INITIAL_ABORT_2:
		async_abort(ssm, 2);
		break;

	case SSM_INITIAL_ABORT_3:
		async_abort(ssm, 3);
		break;

	case SSM_CLEAR_EP2:
		clear_ep2(ssm);
		break;

	case SSM_TURN_OFF:
		/* Set control_packet argument */
		vdev->control_packet = turn_off;

		send_control_packet(ssm);
		break;

	case SSM_TURN_ON:
		if (!vdev->active) {
			/* The only correct exit */
			fpi_ssm_mark_completed(ssm);

			if (vdev->need_report) {
				fpi_imgdev_deactivate_complete(idev);
				vdev->need_report = 0;
			}
			break;
		}
		/* Set control_packet argument */
		vdev->control_packet = turn_on;

		send_control_packet(ssm);
		break;

	case SSM_ASK_INTERRUPT:
		/* Activated, light must be blinking now */

		/* If we first time here, report that activate completed */
		if (vdev->need_report) {
			fpi_imgdev_activate_complete(idev, 0);
			vdev->need_report = 0;
		}

		/* Asyncronously enquire an interrupt */
		vdev->transfer = libusb_alloc_transfer(0);
		vdev->transfer->flags |= LIBUSB_TRANSFER_FREE_TRANSFER;
578
		libusb_fill_interrupt_transfer(vdev->transfer, usb_dev, 0x83,
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
					       vdev->interrupt,
					       VFS_INTERRUPT_SIZE,
					       interrupt_callback, ssm, 0);
		libusb_submit_transfer(vdev->transfer);

		/* This flag could be turned off only in callback function */
		vdev->wait_interrupt = 1;

		/* I've put it here to be sure that data is cleared */
		clear_data(vdev);

		fpi_ssm_next_state(ssm);
		break;

	case SSM_WAIT_INTERRUPT:
		/* Check if user had interrupted the process */
		if (!vdev->active) {
			libusb_cancel_transfer(vdev->transfer);
			fpi_ssm_jump_to_state(ssm, SSM_CLEAR_EP2);
			break;
		}

		if (vdev->wait_interrupt)
			fpi_timeout_add(VFS_SSM_TIMEOUT, wait_interrupt, ssm);
		break;

	case SSM_RECEIVE_FINGER:
		if (vdev->memory == 0) {
			/* Initialize fingerprint buffer */
			g_free(vdev->lines_buffer);
			vdev->memory = VFS_USB_BUFFER_SIZE;
			vdev->lines_buffer = g_malloc(vdev->memory);
			vdev->bytes = 0;

			/* Finger is on the scanner */
			fpi_imgdev_report_finger_status(idev, 1);
		}

		/* Increase buffer size while it's insufficient */
		while (vdev->bytes + VFS_USB_BUFFER_SIZE > vdev->memory) {
			vdev->memory <<= 1;
			vdev->lines_buffer =
			    (struct vfs_line *)g_realloc(vdev->lines_buffer,
							 vdev->memory);
		}

		/* Receive chunk of data */
		vdev->transfer = libusb_alloc_transfer(0);
		vdev->transfer->flags |= LIBUSB_TRANSFER_FREE_TRANSFER;
628
		libusb_fill_bulk_transfer(vdev->transfer, usb_dev, 0x82,
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
					  (void *)vdev->lines_buffer +
					  vdev->bytes, VFS_USB_BUFFER_SIZE,
					  receive_callback, ssm,
					  VFS_USB_TIMEOUT);
		libusb_submit_transfer(vdev->transfer);
		break;

	case SSM_SUBMIT_IMAGE:
		submit_image(idev);
		clear_data(vdev);

		/* Wait for probable vdev->active changing */
		fpi_timeout_add(VFS_SSM_TIMEOUT, scan_completed, ssm);
		break;

	case SSM_NEXT_RECEIVE:
		if (!vdev->active) {
			/* It's the last scan */
			fpi_ssm_jump_to_state(ssm, SSM_CLEAR_EP2);
			break;
		}

		/* Set control_packet argument */
		vdev->control_packet = next_receive_1;

		send_control_packet(ssm);
		break;

	case SSM_WAIT_ANOTHER_SCAN:
		/* Orange light is on now */
		fpi_timeout_add(VFS_SSM_ORANGE_TIMEOUT, another_scan, ssm);
		break;

	default:
		fp_err("Unknown state");
		fpi_imgdev_session_error(idev, -EIO);
665
		fpi_ssm_mark_failed(ssm, -EIO);
666 667 668 669 670 671
	}
}

/* Driver functions */

/* Callback for dev_activate ssm */
672
static void dev_activate_callback(fpi_ssm *ssm, struct fp_dev *_dev, void *user_data)
673
{
674
	struct fp_img_dev *idev = fpi_ssm_get_user_data(ssm);
675
	struct vfs_dev_t *vdev = FP_INSTANCE_DATA(FP_DEV(idev));
676 677 678 679 680 681 682 683 684

	vdev->ssm_active = 0;

	fpi_ssm_free(ssm);
}

/* Activate device */
static int dev_activate(struct fp_img_dev *idev, enum fp_imgdev_state state)
{
685
	struct vfs_dev_t *vdev = FP_INSTANCE_DATA(FP_DEV(idev));
686 687 688 689 690 691

	/* Initialize flags */
	vdev->active = 1;
	vdev->need_report = 1;
	vdev->ssm_active = 1;

692
	fpi_ssm *ssm = fpi_ssm_new(FP_DEV(idev), activate_ssm, SSM_STATES, idev);
693 694 695 696 697 698 699
	fpi_ssm_start(ssm, dev_activate_callback);
	return 0;
}

/* Deactivate device */
static void dev_deactivate(struct fp_img_dev *idev)
{
700
	struct vfs_dev_t *vdev = FP_INSTANCE_DATA(FP_DEV(idev));
701 702 703 704 705 706 707 708 709 710 711 712

	if (!vdev->ssm_active) {
		fpi_imgdev_deactivate_complete(idev);
		return;
	}

	/* Initialize flags */
	vdev->active = 0;
	vdev->need_report = 1;
}

/* Callback for dev_open ssm */
713
static void dev_open_callback(fpi_ssm *ssm, struct fp_dev *_dev, void *user_data)
714 715
{
	/* Notify open complete */
716
	fpi_imgdev_open_complete(fpi_ssm_get_user_data(ssm), 0);
717 718 719 720 721 722
	fpi_ssm_free(ssm);
}

/* Open device */
static int dev_open(struct fp_img_dev *idev, unsigned long driver_data)
{
723 724
	struct vfs_dev_t *vdev;

725
	/* Claim usb interface */
726
	int error = libusb_claim_interface(fpi_dev_get_usb_dev(FP_DEV(idev)), 0);
727 728 729 730 731 732 733
	if (error < 0) {
		/* Interface not claimed, return error */
		fp_err("could not claim interface 0");
		return error;
	}

	/* Initialize private structure */
734
	vdev = g_malloc0(sizeof(struct vfs_dev_t));
735
	fp_dev_set_instance_data(FP_DEV(idev), vdev);
736 737

	/* Clearing previous device state */
738
	fpi_ssm *ssm = fpi_ssm_new(FP_DEV(idev), activate_ssm, SSM_STATES, idev);
739 740 741 742 743 744 745
	fpi_ssm_start(ssm, dev_open_callback);
	return 0;
}

/* Close device */
static void dev_close(struct fp_img_dev *idev)
{
746 747
	struct vfs_dev_t *vdev;

748
	/* Release private structure */
749
	vdev = FP_INSTANCE_DATA(FP_DEV(idev));
750
	g_free(vdev);
751 752

	/* Release usb interface */
753
	libusb_release_interface(fpi_dev_get_usb_dev(FP_DEV(idev)), 0);
754 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

	/* Notify close complete */
	fpi_imgdev_close_complete(idev);
}

/* Usb id table of device */
static const struct usb_id id_table[] = {
	{.vendor = 0x138a,.product = 0x0050},
	{0, 0, 0,},
};

/* Device driver definition */
struct fp_img_driver vfs0050_driver = {
	/* Driver specification */
	.driver = {
		   .id = VFS0050_ID,
		   .name = FP_COMPONENT,
		   .full_name = "Validity VFS0050",
		   .id_table = id_table,
		   .scan_type = FP_SCAN_TYPE_SWIPE,
		   },

	/* Image specification */
	.flags = 0,
	.img_width = VFS_IMAGE_WIDTH,
	.img_height = -1,
	.bz3_threshold = 24,

	/* Routine specification */
	.open = dev_open,
	.close = dev_close,
	.activate = dev_activate,
	.deactivate = dev_deactivate,
};