summaryrefslogtreecommitdiff
path: root/driver/fingerprint/elan/elan_private.c
blob: fc95ceb685a80673aa9489b1a9d890cbb32e7a75 (plain)
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
/* Copyright 2021 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include <stddef.h>
#include "common.h"
#include "console.h"
#include "endian.h"
#include "gpio.h"
#include "link_defs.h"
#include "spi.h"
#include "system.h"
#include "timer.h"
#include "util.h"
#include "shared_mem.h"
#include "math_util.h"
#include "fpsensor.h"
#include "cryptoc/util.h"

#include "elan_sensor.h"
#include "elan_setting.h"
#include "elan_sensor_pal.h"

static uint16_t errors;

#define CPRINTF(format, args...) cprintf(CC_FP, format, ##args)

/* Sensor description */
static struct ec_response_fp_info ec_fp_sensor_info = {
	/* Sensor identification */
	.vendor_id = VID,
	.product_id = PID,
	.model_id = MID,
	.version = VERSION,
	/* Image frame characteristics */
	.frame_size = FP_SENSOR_RES_X * FP_SENSOR_RES_Y,
	.pixel_format = V4L2_PIX_FMT_GREY,
	.width = FP_SENSOR_RES_X,
	.height = FP_SENSOR_RES_Y,
	.bpp = FP_SENSOR_RES_BPP,
};

/**
 * set fingerprint sensor into power saving mode
 */
void fp_sensor_low_power(void)
{
	elan_woe_mode();
}

/**
 * Reset and initialize the sensor IC
 */
int fp_sensor_init(void)
{
	CPRINTF("========%s=======\n", __func__);

	errors = 0;
	elan_execute_reset();
	algorithm_parameter_setting();
	if (elan_execute_calibration() < 0)
		errors |= FP_ERROR_INIT_FAIL;
	if (elan_woe_mode() != 0)
		errors |= FP_ERROR_SPI_COMM;

	return EC_SUCCESS;
}

/**
 * Deinitialize the sensor IC
 */
int fp_sensor_deinit(void)
{
	CPRINTF("========%s=======\n", __func__);
	return elan_fp_deinit();
}

/**
 * Fill the 'ec_response_fp_info' buffer with the sensor information
 *
 * @param[out] resp      retrieve the version, sensor and template information
 *
 * @return EC_SUCCESS on success otherwise error.
 */
int fp_sensor_get_info(struct ec_response_fp_info *resp)
{
	int ret = 0;

	CPRINTF("========%s=======\n", __func__);
	memcpy(resp, &ec_fp_sensor_info, sizeof(struct ec_response_fp_info));
	elan_sensor_get_alg_info(resp);
	resp->errors |= errors;
	CPRINTF("##%s## FrameSize=%d, errors=0x%04x\n", __func__,
		resp->frame_size, resp->errors);

	return ret;
}

/**
 * Compares given finger image against enrolled templates.
 *
 * @param[in]  templ            a pointer to the array of template buffers.
 * @param[in]  templ_count      the number of buffers in the array of templates.
 * @param[in]  image            the buffer containing the finger image
 * @param[out] match_index      index of the matched finger in the template
 *                              array if any.
 * @param[out] update_bitmap    contains one bit per template, the bit is set if
 *                              the match has updated the given template.
 *
 * @return negative value on error, else one of the following code :
 * - EC_MKBP_FP_ERR_MATCH_NO on non-match
 * - EC_MKBP_FP_ERR_MATCH_YES for match when template was not updated with
 *   new data
 * - EC_MKBP_FP_ERR_MATCH_YES_UPDATED for match when template was updated
 * - EC_MKBP_FP_ERR_MATCH_YES_UPDATE_FAILED match, but update failed (not saved)
 * - EC_MKBP_FP_ERR_MATCH_LOW_QUALITY when matching could not be performed due
 *   to low image quality
 * - EC_MKBP_FP_ERR_MATCH_LOW_COVERAGE when matching could not be performed
 *   due to finger covering too little area of the sensor
 */
int fp_finger_match(void *templ, uint32_t templ_count, uint8_t *image,
		    int32_t *match_index, uint32_t *update_bitmap)
{
	CPRINTF("========%s=======\n", __func__);
	return elan_match(templ, templ_count, image, match_index,
			  update_bitmap);
}

/**
 * start a finger enrollment session and initialize enrollment data
 *
 * @return 0 on success.
 *
 */
int fp_enrollment_begin(void)
{
	CPRINTF("========%s=======\n", __func__);
	return elan_enrollment_begin();
}

/**
 * Generate a template from the finger whose enrollment has just being
 * completed.
 *
 * @param templ the buffer which will receive the template.
 *              templ can be set to NULL to abort the current enrollment
 *              process.
 *
 * @return 0 on success or a negative error code.
 */
int fp_enrollment_finish(void *templ)
{
	CPRINTF("========%s=======\n", __func__);
	return elan_enrollment_finish(templ);
}

/**
 * Adds fingerprint image to the current enrollment session.
 *
 * @param[in]  image             fingerprint image data
 * @param[out] completion        retrieve percentage of current enrollment
 *
 * @return a negative value on error or one of the following codes:
 * - EC_MKBP_FP_ERR_ENROLL_OK when image was successfully enrolled
 * - EC_MKBP_FP_ERR_ENROLL_IMMOBILE when image added, but user should be
 *   advised to move finger
 * - EC_MKBP_FP_ERR_ENROLL_LOW_QUALITY when image could not be used due to
 *   low image quality
 * - EC_MKBP_FP_ERR_ENROLL_LOW_COVERAGE when image could not be used due to
 *   finger covering too little area of the sensor
 */
int fp_finger_enroll(uint8_t *image, int *completion)
{
	CPRINTF("========%s=======\n", __func__);
	return elan_enroll(image, completion);
}

/**
 * Put the sensor in its lowest power state.
 *
 * fp_sensor_configure_detect needs to be called to restore finger detection
 * functionality.
 */
void fp_sensor_configure_detect(void)
{
	CPRINTF("========%s=======\n", __func__);
	elan_woe_mode();
}

/**
 * Acquires a fingerprint image with specific capture mode.
 *
 * @param[out] image_data    Memory buffer to retrieve fingerprint image data
 *                          Image_data is allocated by the caller and the size
 *                          is FP_SENSOR_IMAGE_SIZE.
 * @param[in]  mode         one of the FP_CAPTURE_ constants to get a specific
 *                          image type
 * - FP_CAPTURE_VENDOR_FORMAT: Full blown vendor-defined capture
 * - FP_CAPTURE_SIMPLE_IMAGE: Simple raw image capture
 * - FP_CAPTURE_PATTERN0: Self test pattern
 * - FP_CAPTURE_PATTERN1: Self test pattern
 * - FP_CAPTURE_QUALITY_TEST
 * - FP_CAPTURE_RESET_TEST
 * - FP_CAPTURE_TYPE_MAX
 *
 * @return
 * - 0 on success
 * - negative value on error
 * - FP_SENSOR_LOW_IMAGE_QUALITY on image captured but quality is too low
 * - FP_SENSOR_TOO_FAST on finger removed before image was captured
 * - FP_SENSOR_LOW_SENSOR_COVERAGE on sensor not fully covered by finger
 */
int fp_sensor_acquire_image_with_mode(uint8_t *image_data, int mode)
{
	CPRINTF("========%s=======\n", __func__);
	return elan_sensor_acquire_image_with_mode(image_data, mode);
}

/**
 * Returns the status of the finger on the sensor.
 *
 * @return one of the following codes:
 * - FINGER_NONE
 * - FINGER_PARTIAL
 * - FINGER_PRESENT
 */
enum finger_state fp_sensor_finger_status(void)
{
	CPRINTF("========%s=======\n", __func__);
	return elan_sensor_finger_status();
}

/**
 * Runs a test for defective pixels.
 *
 * Should be triggered periodically by the client. The maintenance command can
 * take several hundred milliseconds to run.
 *
 * @return EC_ERROR_HW_INTERNAL on error (such as finger on sensor)
 * @return EC_SUCCESS on success
 */
int fp_maintenance(void)
{
	CPRINTF("========%s=======\n", __func__);
	return elan_fp_maintenance(&errors);
}