--- ./libfprint/drivers/vfs101.c.orig	2011-01-20 14:00:52.535502073 +0100
+++ ./libfprint/drivers/vfs101.c	2011-01-20 14:02:13.541864283 +0100
@@ -0,0 +1,868 @@
+/*
+ * Validity VFS101 driver for libfprint
+ * Copyright (C) 2009 Ray Lehtiniemi <rayl@mail.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
+ */
+
+#include <errno.h>
+#include <string.h>
+#include <unistd.h>
+
+#define FP_COMPONENT "vfs101"
+
+#include <fp_internal.h>
+
+/* The device send back the image into block of 16 frames of 292 bytes */
+#define VFS_FRAME_SIZE		292
+#define VFS_FRAMES			16
+
+/* There is a discontinuity after the 13th scan line,
+ * skip the first block of line */
+#define VFS_IMG_SKIP		13
+
+/* Maximum image length */
+#define VFS_IMG_MAX_LENGTH	1024
+
+/* Minimum image length */
+#define VFS_IMG_MIN_LENGTH	336
+
+/* Minimum image size */
+#define VFS_IMG_MIN_SIZE	(VFS_FRAME_SIZE * VFS_IMG_MIN_LENGTH)
+
+/* Maximum image size */
+#define VFS_IMG_MAX_SIZE	(VFS_FRAME_SIZE * VFS_IMG_MAX_LENGTH)
+
+/* Image width */
+#define VFS_IMG_WIDTH		200
+
+/* Exposure region */
+#define VFS_EXPOSURE		0x00ff500e
+
+/* Exposure level
+ *
+ * Low values (0x0800 or so) produce very over-exposed images, with little or
+ * no dark fingerprint visible against a very bright background.
+ * Medium values (0x2100 or so) produce nicely exposed images.
+ * High values (0x3800 or so) produce very dark images, or even fail to detect
+ * a fingerswipe.
+ *
+ */
+#define VFS_EXPOSURE_LEVEL	0x002100
+
+/* Contrast region */
+#define VFS_CONTRAST		0x00ff5038
+
+/* Contrast level
+ *
+ * This register controls the contrast. When the lower 7 bits are zero, the
+ * image has low contrast. Constrast increases as value approached 0x7f, then
+ * cycles over from 128 to 255.
+ *
+ */
+#define VFS_CONTRAST_LEVEL	0x000014
+
+struct vfs101_dev
+{
+	/* Sequential number */ 
+	unsigned int seqnum;
+
+	/* Buffer for input/output */ 
+	unsigned char buffer[VFS_IMG_MAX_SIZE];
+
+	/* Length of data to send or received */ 
+	unsigned int length;
+
+	/* Ignore usb error */ 
+	int ignore_error;
+};
+
+/* Return byte at specified position */
+static inline unsigned char byte(int position, int value)
+{
+	return (value >> (position * 8)) & 0xff;
+}
+
+/* Return sequential number */
+static inline unsigned short get_seqnum(int h, int l)
+{
+	return (h<<8) | l;
+}
+
+/* Check sequential number */
+static inline int check_seqnum(struct vfs101_dev *vdev)
+{
+	if ((byte(0, vdev->seqnum) == vdev->buffer[0]) &&
+		(byte(1, vdev->seqnum) == vdev->buffer[1]))
+		return 0;
+	else
+		return 1;
+}
+
+#define EP_IN(n)	(n | LIBUSB_ENDPOINT_IN)
+#define EP_OUT(n)	(n | LIBUSB_ENDPOINT_OUT)
+
+#define BULK_TIMEOUT 100
+
+#define dump_buffer(buf)	( \
+	fp_dbg("%02x %02x %02x %02x %02x %02x %02x %02x", \
+		buf[6], buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13] \
+	) \
+)
+
+/* Callback of asynchronous send */
+static void async_send_cb(struct libusb_transfer *transfer)
+{
+	struct fpi_ssm *ssm = transfer->user_data;
+	struct fp_img_dev *dev = ssm->priv;
+	struct vfs101_dev *vdev = dev->priv;
+
+	/* Skip error check if ignore_error is set */
+	if (!vdev->ignore_error)
+	{
+		if (transfer->status != LIBUSB_TRANSFER_COMPLETED)
+		{
+			/* Transfer not completed, return IO error */
+			fp_err("transfer not completed, status = %d", transfer->status);
+			fpi_imgdev_session_error(dev, -EIO);
+			goto out;
+		}
+
+		if (transfer->length != transfer->actual_length)
+		{
+			/* Data sended mismatch with expected, return protocol error */
+			fp_err("length mismatch, got %d, expected %d",
+				transfer->actual_length, transfer->length);
+			fpi_imgdev_session_error(dev, -EPROTO);
+			goto out;
+		}
+	}
+	/* Dump buffer for debug */
+	dump_buffer(vdev->buffer);
+
+	fpi_ssm_next_state(ssm);
+
+out:
+	libusb_free_transfer(transfer);
+}
+
+/* Submit asynchronous send */
+static void async_send(struct fpi_ssm *ssm)
+{
+	struct libusb_transfer *transfer = libusb_alloc_transfer(0);
+	struct fp_img_dev *dev = ssm->priv;
+	struct vfs101_dev *vdev = dev->priv;
+	int r;
+
+	if (!transfer)
+	{
+		/* Allocation transfer failed, return no memory error */
+		fp_err("allocation of usb transfer failed");
+		fpi_imgdev_session_error(dev, -ENOMEM);
+		return;
+	}
+
+	/* Put sequential number into the buffer */
+	vdev->buffer[0] = byte(0, vdev->seqnum);
+	vdev->buffer[1] = byte(1, vdev->seqnum);
+
+	/* Prepare bulk transfer */
+	libusb_fill_bulk_transfer(transfer, dev->udev, EP_OUT(1), vdev->buffer, vdev->length, async_send_cb, ssm, BULK_TIMEOUT);
+
+	/* Submit transfer */
+	r = libusb_submit_transfer(transfer);
+	if (r != 0)
+	{
+		/* Submission of transfer failed, return IO error */
+		libusb_free_transfer(transfer);
+		fp_err("submit of usb transfer failed");
+		fpi_imgdev_session_error(dev, -EIO);
+		return;
+	}
+}
+
+/* Callback of asynchronous recv */
+static void async_recv_cb(struct libusb_transfer *transfer)
+{
+	struct fpi_ssm *ssm = transfer->user_data;
+	struct fp_img_dev *dev = ssm->priv;
+	struct vfs101_dev *vdev = dev->priv;
+
+	/* Skip error check if ignore_error is set */
+	if (!vdev->ignore_error)
+	{
+		if (transfer->status != LIBUSB_TRANSFER_COMPLETED)
+		{
+			/* Transfer not completed, return IO error */
+			fp_err("transfer not completed, status = %d", transfer->status);
+			fpi_imgdev_session_error(dev, -EIO);
+			goto out;
+		}
+
+		if (check_seqnum(vdev))
+		{
+			/* Sequential number received mismatch, return protocol error */
+			fp_err("seqnum mismatch, got %04x, expected %04x",
+				get_seqnum(vdev->buffer[1], vdev->buffer[0]), vdev->seqnum);
+			fpi_imgdev_session_error(dev, -EPROTO);
+			goto out;
+		}
+	}
+	/* Dump buffer for debug */
+	dump_buffer(vdev->buffer);
+
+	/* Increase sequential number and set length of received data */
+	vdev->seqnum++;
+	vdev->length = transfer->actual_length;
+
+	fpi_ssm_next_state(ssm);
+
+out:
+	libusb_free_transfer(transfer);
+}
+
+/* Submit asynchronous recv */
+static void async_recv(struct fpi_ssm *ssm)
+{
+	struct libusb_transfer *transfer = libusb_alloc_transfer(0);
+	struct fp_img_dev *dev = ssm->priv;
+	struct vfs101_dev *vdev = dev->priv;
+	int r;
+
+	if (!transfer)
+	{
+		/* Allocation transfer failed, return no memory error */
+		fp_err("allocation of usb transfer failed");
+		fpi_imgdev_session_error(dev, -ENOMEM);
+		return;
+	}
+
+	/* Prepare bulk transfer */
+	libusb_fill_bulk_transfer(transfer, dev->udev, EP_IN(1), vdev->buffer, 0x0f, async_recv_cb, ssm, BULK_TIMEOUT);
+
+	/* Submit transfer */
+	r = libusb_submit_transfer(transfer);
+	if (r != 0)
+	{
+		/* Submission of transfer failed, return IO error */
+		libusb_free_transfer(transfer);
+		fp_err("submit of usb transfer failed");
+		fpi_imgdev_session_error(dev, -EIO);
+		return;
+	}
+}
+
+static void async_load(struct fpi_ssm *ssm);
+
+/* Callback of asynchronous load */
+static void async_load_cb(struct libusb_transfer *transfer)
+{
+	struct fpi_ssm *ssm = transfer->user_data;
+	struct fp_img_dev *dev = ssm->priv;
+	struct vfs101_dev *vdev = dev->priv;
+
+	/* Increase image length */
+	vdev->length += transfer->actual_length;
+
+	/* Skip error check if ignore_error is set */
+	if (!vdev->ignore_error)
+	{
+		if (transfer->status != LIBUSB_TRANSFER_COMPLETED)
+		{
+			if (vdev->length < VFS_IMG_MIN_SIZE)
+			{
+				/* Image not valid, too small */
+				fp_warn("image too small, length = %d", vdev->length);
+				vdev->length = 0;
+				fpi_ssm_next_state(ssm);
+				goto out;
+			}
+
+			/* Transfer not completed, return IO error */
+			fp_err("transfer not completed, status = %d", transfer->status);
+			fpi_imgdev_session_error(dev, -EIO);
+			goto out;
+		}
+
+		if (vdev->length % VFS_FRAME_SIZE)
+		{
+			/* Received incomplete frame, return protocol error */
+			fp_err("received incomplete frame");
+			fpi_imgdev_session_error(dev, -EPROTO);
+			goto out;
+		}
+	}
+
+	if (transfer->actual_length == VFS_FRAME_SIZE*VFS_FRAMES)
+	{
+		if ((VFS_IMG_MAX_SIZE - vdev->length) < (VFS_FRAME_SIZE*VFS_FRAMES))
+		{
+			/* Buffer full, image too large, return no memory error */
+			fp_err("buffer full, image too large");
+			fpi_imgdev_session_error(dev, -ENOMEM);
+			goto out;
+		}
+		else
+			/* Image load not completed, submit another asynchronous load */
+			async_load(ssm);
+	}
+	else
+	{
+		/* Image load completed, go to next state */
+		fp_dbg("image loaded, length = %d", vdev->length);
+		fpi_ssm_next_state(ssm);
+	}
+
+out:
+	libusb_free_transfer(transfer);
+}
+
+/* Submit asynchronous load */
+static void async_load(struct fpi_ssm *ssm)
+{
+	struct libusb_transfer *transfer = libusb_alloc_transfer(0);
+	struct fp_img_dev *dev = ssm->priv;
+	struct vfs101_dev *vdev = dev->priv;
+	unsigned char *buffer;
+	int r;
+
+	if (!transfer)
+	{
+		/* Allocation transfer failed, return no memory error */
+		fp_err("allocation of usb transfer failed");
+		fpi_imgdev_session_error(dev, -ENOMEM);
+		return;
+	}
+
+	/* Append new data into the buffer */
+	buffer = vdev->buffer + vdev->length;
+
+	/* Prepare bulk transfer */
+	libusb_fill_bulk_transfer(transfer, dev->udev, EP_IN(2), buffer, VFS_FRAME_SIZE*VFS_FRAMES, async_load_cb, ssm, BULK_TIMEOUT);
+
+	/* Submit transfer */
+	r = libusb_submit_transfer(transfer);
+	if (r != 0)
+	{
+		/* Submission of transfer failed, return IO error */
+		libusb_free_transfer(transfer);
+		fp_err("submit of usb transfer failed");
+		fpi_imgdev_session_error(dev, -EIO);
+		return;
+	}
+}
+
+/* Callback of asynchronous pause */
+static void async_pause_cb(void *data)
+{
+	struct fpi_ssm *ssm = data;
+
+	fpi_ssm_next_state(ssm);
+}
+
+/* Submit asynchronous pause */
+static void async_pause(unsigned int msec, struct fpi_ssm *ssm)
+{
+	if (fpi_timeout_add(msec, async_pause_cb, ssm) == NULL)
+            fpi_ssm_mark_aborted(ssm, -ETIME);
+}
+
+/* Swap ssm states */
+enum
+{
+	M_SWAP_SEND,
+	M_SWAP_RECV,
+	M_SWAP_NUM_STATES,
+};
+
+/* Exec swap sequential state machine */
+static void m_swap_state(struct fpi_ssm *ssm)
+{
+	switch (ssm->cur_state)
+	{
+	case M_SWAP_SEND:
+		/* Send data */
+		async_send(ssm);
+		break;
+
+	case M_SWAP_RECV:
+		/* Recv response */
+		async_recv(ssm);
+		break;
+	}
+}
+
+/* Start swap sequential state machine */
+static void m_swap(struct fpi_ssm *ssm, unsigned char *data, size_t length)
+{
+	struct fp_img_dev *dev = ssm->priv;
+	struct vfs101_dev *vdev = dev->priv;
+	struct fpi_ssm *subsm;
+
+	/* Prepare data for sending */
+	memcpy(vdev->buffer, data, length);
+	memset(vdev->buffer + length, 0, 16 - length);
+	vdev->length = length;
+
+	/* Start swap ssm */
+	subsm = fpi_ssm_new(dev->dev, m_swap_state, M_SWAP_NUM_STATES);
+	subsm->priv = dev;
+	fpi_ssm_start_subsm(ssm, subsm);
+}
+
+/*
+ * Protocol notes from Damir Syabitov on vfs101driver group:
+ *
+ *		00 00 01 00		Reset
+ *		00 00 02 00		GetVersion
+ * -	00 00 03 00		GetPrint
+ *		00 00 04 00		GetParam
+ * -	00 00 05 00		SetParam
+ *		00 00 06 00		GetConfiguration
+ *		00 00 07 00		DownloadPatch
+ *		00 00 08 00		GetRateData
+ *		00 00 09 00		IspRequest
+ *		00 00 0A 00		ProgramFlash
+ *		00 00 0B 00		EraseFlash
+ *		00 00 0C 00		LedStates
+ *		00 00 0D 00		LedEvent
+ *		00 00 0E 00		AbortPrint
+ *		00 00 0F 00		Spare2
+ *		00 00 10 00		Spare3
+ *		00 00 11 00		Spare4
+ *		00 00 12 00		Peek
+ *		00 00 13 00		Poke
+ *		00 00 14 00		SensorSpiTrans
+ *		00 00 15 00		SensorGPIO
+ * -	00 00 16 00		GetFingerState 
+ */
+
+/* GetPrint (00 00 03 00) - Retrieve fingerprint image information */
+static void vfs_get_print(struct fpi_ssm *ssm, unsigned int param)
+{
+	unsigned char data[0x0e] = { 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x01 };
+
+	fp_dbg("param = %04x", param);
+
+	/* Prepare data for sending */
+	data[6] = byte(0, param);
+	data[7] = byte(1, param);
+
+	m_swap(ssm, data, 0x0e);
+}
+
+/* SetParam (00 00 05 00) - Set a parameter value on the device */
+static void vfs_set_param(struct fpi_ssm *ssm, unsigned int param, unsigned int value)
+{
+	unsigned char data[0x0a] = { 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00 };
+
+	fp_dbg("param = %04x, value = %04x", param, value);
+
+	/* Prepare data for sending */
+	data[6] = byte(0, param);
+	data[7] = byte(1, param);
+	data[8] = byte(0, value);
+	data[9] = byte(1, value);
+
+	m_swap(ssm, data, 0x0a);
+}
+
+/* Poke (00 00 13 00) - Set a parameter value on the device */
+static void vfs_poke(struct fpi_ssm *ssm, unsigned int addr, unsigned int value, unsigned int size)
+{
+	unsigned char data[0x0f] = { 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
+
+	fp_dbg("addr = %04x, value = %04x", addr, value);
+
+	/* Prepare data for sending */
+	data[6] = byte(0, addr);
+	data[7] = byte(1, addr);
+	data[8] = byte(2, addr);
+	data[9] = byte(3, addr);
+	data[10] = byte(0, value);
+	data[11] = byte(1, value);
+	data[12] = byte(2, value);
+	data[13] = byte(3, value);
+	data[14] = byte(0, size);
+
+	m_swap(ssm, data, 0x0f);
+}
+
+/* GetFingerState (00 00 16 00) - Poll device for current finger state */
+static void vfs_finger_state(struct fpi_ssm *ssm)
+{
+	unsigned char data[0x06] = { 0x00, 0x00, 0x00, 0x00, 0x16, 0x00 };
+
+	fp_dbg("");
+
+	m_swap (ssm, data, 0x06);
+}
+
+/* Load raw image from reader */
+static void img_load(struct fpi_ssm *ssm)
+{
+	struct fp_img_dev *dev = ssm->priv;
+	struct vfs101_dev *vdev = dev->priv;
+
+	/* Reset image length */
+	vdev->length = 0;
+
+	/* Asynchronous load */
+	async_load(ssm);
+}
+
+/* Return height of fingerprint image
+ *
+ * the end of image contain only background and is not useful,
+ * it's possible to detect this by testing byte 283 of raw buffer.
+ *
+ */
+unsigned int img_height(struct vfs101_dev *vdev)
+{
+	unsigned int line = vdev->length / VFS_FRAME_SIZE;
+	unsigned int i = ((line - 1) * VFS_FRAME_SIZE) + 283;
+
+	/* Scan image from the end */
+	while (line > VFS_IMG_SKIP)
+	{
+		if (vdev->buffer[i] != 0x00)
+			/* Find last useful line */
+			break;
+
+		i = i - VFS_FRAME_SIZE;
+		line--;
+	}
+
+	if (line <= VFS_IMG_SKIP)
+	{
+		/* Height not find, use full image */
+		fp_warn("height not find, use full image");
+		line = vdev->length / VFS_FRAME_SIZE;
+	}
+
+	return (line - VFS_IMG_SKIP);
+}
+
+/* Extract image from reader buffer and put it into image data */
+static void img_extract(struct vfs101_dev *vdev, struct fp_img *img)
+{
+	unsigned int line;
+	unsigned char *img_buffer = img->data;
+	unsigned char *vdev_buffer = vdev->buffer + (VFS_IMG_SKIP * VFS_FRAME_SIZE) + 6;
+
+	for (line = 0; line < img->height; line++)
+	{
+		/* Copy image line from reader buffer to image data */
+		memcpy(img_buffer, vdev_buffer, VFS_IMG_WIDTH);
+
+		/* Next line of reader buffer */
+		vdev_buffer = vdev_buffer + VFS_FRAME_SIZE;
+
+		/* Next line of image buffer */
+		img_buffer = img_buffer + VFS_IMG_WIDTH;
+	}
+}
+
+/* Create fingerpint image from raw data */
+static void img_create(struct fpi_ssm *ssm)
+{
+	struct fp_img_dev *dev = ssm->priv;
+	struct vfs101_dev *vdev = dev->priv;
+	struct fp_img *img;
+	unsigned int height;
+
+	/* Find height of fingerprint image */
+	height = img_height(vdev);
+
+	/* Create new image */
+	img = fpi_img_new(height * VFS_IMG_WIDTH);
+	img->height = height;
+	img->flags = FP_IMG_V_FLIPPED;
+
+	/* Copy data int image */
+	img_extract(vdev, img);
+
+	/* Notify image captured */
+	fpi_imgdev_image_captured(dev, img);
+};
+
+/* Loop ssm states */
+enum
+{
+	M_LOOP_GET_PRINT,
+	M_LOOP_PAUSE,
+	M_LOOP_GET_STATE,
+	M_LOOP_LOAD_IMAGE,
+	M_LOOP_CREATE_IMAGE,
+	M_LOOP_NUM_STATES,
+};
+
+/* Exec loop sequential state machine */
+static void m_loop_state(struct fpi_ssm *ssm)
+{
+	struct fp_img_dev *dev = ssm->priv;
+	struct vfs101_dev *vdev = dev->priv;
+
+	switch (ssm->cur_state)
+	{
+	case M_LOOP_GET_PRINT:
+		/* Send get print command to the reader */
+		vfs_get_print(ssm, VFS_IMG_MAX_LENGTH);
+		break;
+
+	case M_LOOP_PAUSE:
+		/* Wait fingerprint scanning */
+		async_pause(100, ssm);
+		break;
+
+	case M_LOOP_GET_STATE:
+		/* Get finger state */
+		vfs_finger_state(ssm);
+		break;
+
+	case M_LOOP_LOAD_IMAGE:
+		/* Check finger state */
+		if (vdev->buffer[0x0a] == 0x02)
+		{
+			/* Finger is present, load image from reader */
+			fpi_imgdev_report_finger_status(dev, TRUE);
+
+			img_load(ssm);
+		}
+		else
+			/* Finger isn't present, loop */
+			fpi_ssm_jump_to_state(ssm, M_LOOP_PAUSE);
+		break;
+
+	case M_LOOP_CREATE_IMAGE:
+		/* Check if image is loaded */
+		if (vdev->length)
+		{
+			/* Image is loaded, create finger print from raw data */
+			img_create(ssm);
+
+			fpi_ssm_next_state(ssm);
+		}
+		else 
+			/* Image isn't loaded, loop */
+			fpi_ssm_jump_to_state(ssm, M_LOOP_GET_PRINT);
+
+		fpi_imgdev_report_finger_status(dev, FALSE);
+		break;
+	}
+}
+
+/* Complete loop sequential state machine */
+static void m_loop_complete(struct fpi_ssm *ssm)
+{
+	fpi_ssm_free(ssm);
+}
+
+/* Init ssm states */
+enum
+{
+	M_INIT_PARAM_0E,
+	M_INIT_PARAM_11,
+	M_INIT_EXPOSURE,
+	M_INIT_CONTRAST,
+	M_INIT_NUM_STATES,
+};
+
+/* Exec init sequential state machine */
+static void m_init_state(struct fpi_ssm *ssm)
+{
+	switch (ssm->cur_state)
+	{
+	case M_INIT_PARAM_0E:
+		/* Set param 0x000e, required for take image */
+		vfs_set_param(ssm, 0x000e, 0x0001);
+		break;
+
+	case M_INIT_PARAM_11:
+		/* Set param 0x0011, required for take image */
+		vfs_set_param(ssm, 0x0011, 0x0008);
+		break;
+
+	case M_INIT_EXPOSURE:
+		/* Set exposure level of reader */
+		vfs_poke(ssm, VFS_EXPOSURE, VFS_EXPOSURE_LEVEL, 0x02);
+		break;
+
+	case M_INIT_CONTRAST:
+		/* Set contrast level of reader */
+		vfs_poke(ssm, VFS_CONTRAST, VFS_CONTRAST_LEVEL, 0x01);
+		break;
+	}
+}
+
+/* Complete init sequential state machine */
+static void m_init_complete(struct fpi_ssm *ssm)
+{
+	struct fp_img_dev *dev = ssm->priv;
+	struct fpi_ssm *ssm_loop;
+
+	/* Notify activate complete */
+	fpi_imgdev_activate_complete(dev, ssm->error);
+
+	if (!ssm->error)
+	{
+		/* Start loop ssm */
+		ssm_loop = fpi_ssm_new(dev->dev, m_loop_state,M_LOOP_NUM_STATES);
+		ssm_loop->priv = dev;
+		fpi_ssm_start(ssm_loop, m_loop_complete);
+	}
+
+	fpi_ssm_free(ssm);
+}
+
+/* Open ssm states */
+enum
+{
+	M_OPEN_RECV,
+	M_OPEN_LOAD,
+	M_OPEN_NUM_STATES,
+};
+
+/* Exec open sequential state machine */
+static void m_open_state(struct fpi_ssm *ssm)
+{
+	switch (ssm->cur_state)
+	{
+	case M_OPEN_RECV:
+		/* Recv eventualy dirty data */
+		async_recv(ssm);
+		break;
+
+	case M_OPEN_LOAD:
+		/* Load eventualy dirty image */
+		img_load(ssm);
+		break;
+	}
+}
+
+/* Complete open sequential state machine */
+static void m_open_complete(struct fpi_ssm *ssm)
+{
+	struct fp_img_dev *dev = ssm->priv;
+	struct vfs101_dev *vdev = dev->priv;
+
+	if (!ssm->error)
+	{
+		/* Reset sequential number and ignore_error flag */
+		vdev->seqnum = 0;
+		vdev->ignore_error = 0;
+
+		/* Notify open complete */
+		fpi_imgdev_open_complete(dev, 0);
+	}
+
+	fpi_ssm_free(ssm);
+}
+
+/* Activate device */
+static int dev_activate(struct fp_img_dev *dev, enum fp_imgdev_state state)
+{
+	struct fpi_ssm *ssm;
+
+	/* Start open ssm */
+	ssm = fpi_ssm_new(dev->dev, m_init_state,M_INIT_NUM_STATES);
+	ssm->priv = dev;
+	fpi_ssm_start(ssm, m_init_complete);
+
+	return 0;
+}
+
+/* Deactivate device */
+static void dev_deactivate(struct fp_img_dev *dev)
+{
+	/* Notify deactivate complete */
+	fpi_imgdev_deactivate_complete(dev);
+}
+
+/* Open device */
+static int dev_open(struct fp_img_dev *dev, unsigned long driver_data)
+{
+	struct fpi_ssm *ssm;
+	struct vfs101_dev *vdev = NULL;
+	int r;
+
+	/* Claim usb interface */
+	r = libusb_claim_interface(dev->udev, 0);
+	if (r < 0)
+	{
+		/* Interface not claimed, return error */
+		fp_err("could not claim interface 0");
+		fpi_imgdev_session_error(dev, r);
+		return r;
+	}
+
+	/* Initialize private structure */
+	vdev = g_malloc0(sizeof(struct vfs101_dev));
+	vdev->ignore_error = 1;
+
+	dev->priv = vdev;
+
+	/* Start open ssm */
+	ssm = fpi_ssm_new(dev->dev, m_open_state, M_OPEN_NUM_STATES);
+	ssm->priv = dev;
+	fpi_ssm_start(ssm, m_open_complete);
+
+	return 0;
+}
+
+/* Close device */
+static void dev_close(struct fp_img_dev *dev)
+{
+	/* Release private structure */
+	g_free(dev->priv);
+
+	/* Release usb interface */
+	libusb_release_interface(dev->udev, 0);
+
+	/* Notify close complete */
+	fpi_imgdev_close_complete(dev);
+}
+
+/* Usb id table of device */
+static const struct usb_id id_table[] =
+{
+	{ .vendor = 0x138a, .product = 0x0001 },
+	{ 0, 0, 0, },
+};
+
+/* Device driver definition */
+struct fp_img_driver vfs101_driver =
+{
+	/* Driver specification */
+	.driver =
+	{
+		.id = 10,
+		.name = FP_COMPONENT,
+		.full_name = "Validity VFS101",
+		.id_table = id_table,
+		.scan_type = FP_SCAN_TYPE_SWIPE,
+	},
+
+	/* Image specification */
+	.flags = 0,
+	.img_height = -1,
+	.img_width = VFS_IMG_WIDTH,
+
+	/* Routine specification */
+	.open = dev_open,
+	.close = dev_close,
+	.activate = dev_activate,
+	.deactivate = dev_deactivate,
+};
--- ./libfprint/fp_internal.h.orig	2010-11-03 12:39:34.325159723 +0100
+++ ./libfprint/fp_internal.h	2010-11-09 12:40:01.546576461 +0100
@@ -265,6 +265,9 @@
 #ifdef ENABLE_VCOM5S
 extern struct fp_img_driver vcom5s_driver;
 #endif
+#ifdef ENABLE_VFS101
+extern struct fp_img_driver vfs101_driver;
+#endif
 
 extern libusb_context *fpi_usb_ctx;
 extern GSList *opened_devices;
--- ./libfprint/core.c.orig	2010-11-03 12:38:08.624383608 +0100
+++ ./libfprint/core.c	2010-11-09 12:39:21.633836762 +0100
@@ -368,6 +368,9 @@
 #ifdef ENABLE_AES1610
 	&aes1610_driver,
 #endif
+#ifdef ENABLE_VFS101
+	&vfs101_driver,
+#endif
 /*#ifdef ENABLE_UPEKTC
 	&upektc_driver,
 #endif
--- ./libfprint/Makefile.am.orig	2010-11-03 12:27:45.385133988 +0100
+++ ./libfprint/Makefile.am	2010-11-09 12:38:41.184676308 +0100
@@ -12,6 +12,7 @@
 AES4000_SRC = drivers/aes4000.c
 FDU2000_SRC = drivers/fdu2000.c
 VCOM5S_SRC = drivers/vcom5s.c
+VFS101_SRC = drivers/vfs101.c
 
 EXTRA_DIST = \
 	$(UPEKE2_SRC)		\
@@ -24,6 +25,7 @@
 	$(AES4000_SRC)		\
 	$(FDU2000_SRC)		\
 	$(VCOM5S_SRC)		\
+	$(VFS101_SRC)		\
 	aeslib.c aeslib.h	\
 	imagemagick.c		\
 	gdkpixbuf.c
@@ -122,6 +124,10 @@
 DRIVER_SRC += $(AES4000_SRC)
 endif
 
+if ENABLE_VFS101
+DRIVER_SRC += $(VFS101_SRC)
+endif
+
 if REQUIRE_IMAGEMAGICK
 OTHER_SRC += imagemagick.c
 libfprint_la_CFLAGS += $(IMAGING_CFLAGS)
--- ./configure.ac.orig	2010-11-03 09:55:01.698383993 +0100
+++ ./configure.ac	2010-11-09 12:36:56.961588962 +0100
@@ -20,7 +20,7 @@
 AC_SUBST(lt_revision)
 AC_SUBST(lt_age)
 
-all_drivers="upeke2 upekts upektc upeksonly vcom5s uru4000 fdu2000 aes1610 aes2501 aes4000"
+all_drivers="upeke2 upekts upektc upeksonly vcom5s uru4000 fdu2000 aes1610 aes2501 aes4000 vfs101"
 
 require_imaging='no'
 require_aeslib='no'
@@ -34,6 +34,7 @@
 enable_aes1610='no'
 enable_aes2501='no'
 enable_aes4000='no'
+enable_vfs101='no'
 
 AC_ARG_WITH([drivers],[AS_HELP_STRING([--with-drivers],
 	[List of drivers to enable])],
@@ -88,6 +89,10 @@
 			require_imaging="yes"
 			enable_aes4000="yes"
 		;;
+		vfs101)
+			AC_DEFINE([ENABLE_VFS101], [], [Build Validity VFS101 driver])
+			enable_vfs101="yes"
+		;;
 	esac
 done
 
@@ -102,6 +107,7 @@
 AM_CONDITIONAL([ENABLE_AES2501], [test "$enable_aes2501" = "yes"])
 AM_CONDITIONAL([ENABLE_AES4000], [test "$enable_aes4000" = "yes"])
 AM_CONDITIONAL([REQUIRE_AESLIB], [test "$require_aeslib" = "yes"])
+AM_CONDITIONAL([ENABLE_VFS101], [test "$enable_vfs101" = "yes"])
 
 
 PKG_CHECK_MODULES(LIBUSB, [libusb-1.0 >= 0.9.1])
@@ -263,6 +269,11 @@
 else
 	AC_MSG_NOTICE([   aes4000 driver disabled])
 fi
+if test x$enable_vfs101 != xno ; then
+	AC_MSG_NOTICE([** vfs101 driver enabled])
+else
+	AC_MSG_NOTICE([   vfs101 driver disabled])
+fi
 if test x$require_aeslib != xno ; then
 	AC_MSG_NOTICE([** aeslib helper functions enabled])
 else
