elan.c 15.7 KB
Newer Older
Igor Filatov's avatar
Igor Filatov committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * Elan driver for libfprint
 *
 * Copyright (C) 2017 Igor Filatov <ia.filatov@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 "elan"

23
#include "drivers_api.h"
Igor Filatov's avatar
Igor Filatov committed
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
#include "elan.h"

unsigned char elan_get_pixel(struct fpi_frame_asmbl_ctx *ctx,
			     struct fpi_frame *frame, unsigned int x,
			     unsigned int y)
{
	return frame->data[x + y * ctx->frame_width];
}

static struct fpi_frame_asmbl_ctx assembling_ctx = {
	.frame_width = 0,
	.frame_height = 0,
	.image_width = 0,
	.get_pixel = elan_get_pixel,
};

struct elan_dev {
	gboolean deactivating;

	const struct elan_cmd *cmds;
	size_t cmds_len;
	int cmd_idx;
	int cmd_timeout;
	struct libusb_transfer *cur_transfer;

	unsigned char *last_read;
	unsigned char frame_width;
	unsigned char frame_height;
	unsigned char raw_frame_width;
	int num_frames;
	GSList *frames;
};

static void elan_dev_reset(struct elan_dev *elandev)
{
59
	G_DEBUG_HERE();
Igor Filatov's avatar
Igor Filatov committed
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

	BUG_ON(elandev->cur_transfer);

	elandev->deactivating = FALSE;

	elandev->cmds = NULL;
	elandev->cmd_idx = 0;
	elandev->cmd_timeout = ELAN_CMD_TIMEOUT;

	g_free(elandev->last_read);
	elandev->last_read = NULL;

	g_slist_free_full(elandev->frames, g_free);
	elandev->frames = NULL;
	elandev->num_frames = 0;
}

static void elan_save_frame(struct fp_img_dev *dev)
{
	struct elan_dev *elandev = dev->priv;
	unsigned char raw_height = elandev->frame_width;
	unsigned char raw_width = elandev->raw_frame_width;
	unsigned short *frame =
	    g_malloc(elandev->frame_width * elandev->frame_height * 2);

85
	G_DEBUG_HERE();
Igor Filatov's avatar
Igor Filatov committed
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

	/* Raw images are vertical and perpendicular to swipe direction of a
	 * normalized image, which means we need to make them horizontal before
	 * assembling. We also discard stirpes of ELAN_FRAME_MARGIN along raw
	 * height. */
	for (int y = 0; y < raw_height; y++)
		for (int x = ELAN_FRAME_MARGIN;
		     x < raw_width - ELAN_FRAME_MARGIN; x++) {
			int frame_idx =
			    y + (x - ELAN_FRAME_MARGIN) * raw_height;
			int raw_idx = x + y * raw_width;
			frame[frame_idx] =
			    ((unsigned short *)elandev->last_read)[raw_idx];
		}

	elandev->frames = g_slist_prepend(elandev->frames, frame);
	elandev->num_frames += 1;
}

/* Transform raw sesnsor data to normalized 8-bit grayscale image. */
static void elan_process_frame(unsigned short *raw_frame, GSList ** frames)
{
	unsigned int frame_size =
	    assembling_ctx.frame_width * assembling_ctx.frame_height;
	struct fpi_frame *frame =
	    g_malloc(frame_size + sizeof(struct fpi_frame));

113
	G_DEBUG_HERE();
Igor Filatov's avatar
Igor Filatov committed
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

	unsigned short min = 0xffff, max = 0;
	for (int i = 0; i < frame_size; i++) {
		if (raw_frame[i] < min)
			min = raw_frame[i];
		if (raw_frame[i] > max)
			max = raw_frame[i];
	}

	unsigned short px;
	for (int i = 0; i < frame_size; i++) {
		px = raw_frame[i];
		if (px <= min)
			px = 0;
		else if (px >= max)
			px = 0xff;
		else
			px = (px - min) * 0xff / (max - min);
		frame->data[i] = (unsigned char)px;
	}

	*frames = g_slist_prepend(*frames, frame);
}

static void elan_submit_image(struct fp_img_dev *dev)
{
	struct elan_dev *elandev = dev->priv;
	GSList *frames = NULL;
	struct fp_img *img;

144
	G_DEBUG_HERE();
Igor Filatov's avatar
Igor Filatov committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

	for (int i = 0; i < ELAN_SKIP_LAST_FRAMES; i++)
		elandev->frames = g_slist_next(elandev->frames);

	assembling_ctx.frame_width = elandev->frame_width;
	assembling_ctx.frame_height = elandev->frame_height;
	assembling_ctx.image_width = elandev->frame_width * 3 / 2;
	g_slist_foreach(elandev->frames, (GFunc) elan_process_frame, &frames);
	fpi_do_movement_estimation(&assembling_ctx, frames,
				   elandev->num_frames - ELAN_SKIP_LAST_FRAMES);
	img = fpi_assemble_frames(&assembling_ctx, frames,
				  elandev->num_frames - ELAN_SKIP_LAST_FRAMES);

	img->flags |= FP_IMG_PARTIAL;
	fpi_imgdev_image_captured(dev, img);
}

static void elan_cmd_done(struct fpi_ssm *ssm)
{
164
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
Igor Filatov's avatar
Igor Filatov committed
165
166
	struct elan_dev *elandev = dev->priv;

167
	G_DEBUG_HERE();
Igor Filatov's avatar
Igor Filatov committed
168
169
170
171
172
173
174
175
176
177
178

	elandev->cmd_idx += 1;
	if (elandev->cmd_idx < elandev->cmds_len)
		elan_run_next_cmd(ssm);
	else
		fpi_ssm_next_state(ssm);
}

static void elan_cmd_cb(struct libusb_transfer *transfer)
{
	struct fpi_ssm *ssm = transfer->user_data;
179
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
Igor Filatov's avatar
Igor Filatov committed
180
181
	struct elan_dev *elandev = dev->priv;

182
	G_DEBUG_HERE();
Igor Filatov's avatar
Igor Filatov committed
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

	elandev->cur_transfer = NULL;

	switch (transfer->status) {
	case LIBUSB_TRANSFER_COMPLETED:
		if (transfer->length != transfer->actual_length) {
			fp_dbg("unexpected transfer length");
			elan_dev_reset(elandev);
			fpi_ssm_mark_aborted(ssm, -EPROTO);
		} else if (transfer->endpoint & LIBUSB_ENDPOINT_IN)
			/* just finished receiving */
			elan_cmd_done(ssm);
		else {
			/* just finished sending */
			if (elandev->cmds[elandev->cmd_idx].response_len)
				elan_cmd_read(ssm);
			else
				elan_cmd_done(ssm);
		}
		break;
	case LIBUSB_TRANSFER_CANCELLED:
		fp_dbg("transfer cancelled");
		fpi_ssm_mark_aborted(ssm, -ECANCELED);
		break;
	case LIBUSB_TRANSFER_TIMED_OUT:
		fp_dbg("transfer timed out");
		// elan_dev_reset(elandev);
		fpi_ssm_mark_aborted(ssm, -ETIMEDOUT);
		break;
	default:
		fp_dbg("transfer failed: %d", transfer->status);
		elan_dev_reset(elandev);
		fpi_ssm_mark_aborted(ssm, -EIO);
	}
}

static void elan_cmd_read(struct fpi_ssm *ssm)
{
221
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
Igor Filatov's avatar
Igor Filatov committed
222
223
224
	struct elan_dev *elandev = dev->priv;
	int response_len = elandev->cmds[elandev->cmd_idx].response_len;

225
	G_DEBUG_HERE();
Igor Filatov's avatar
Igor Filatov committed
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

	if (elandev->cmds[elandev->cmd_idx].cmd == read_cmds[0].cmd)
		/* raw data has 2-byte "pixels" and the frame is vertical */
		response_len =
		    elandev->raw_frame_width * elandev->frame_width * 2;

	struct libusb_transfer *transfer = libusb_alloc_transfer(0);
	if (!transfer) {
		fpi_ssm_mark_aborted(ssm, -ENOMEM);
		return;
	}
	elandev->cur_transfer = transfer;

	g_free(elandev->last_read);
	elandev->last_read = g_malloc(response_len);

	libusb_fill_bulk_transfer(transfer, dev->udev,
				  elandev->cmds[elandev->cmd_idx].response_in,
				  elandev->last_read, response_len, elan_cmd_cb,
				  ssm, elandev->cmd_timeout);
	transfer->flags = LIBUSB_TRANSFER_FREE_TRANSFER;
	int r = libusb_submit_transfer(transfer);
	if (r < 0)
		fpi_ssm_mark_aborted(ssm, r);
}

static void elan_run_next_cmd(struct fpi_ssm *ssm)
{
254
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
Igor Filatov's avatar
Igor Filatov committed
255
256
	struct elan_dev *elandev = dev->priv;

257
	G_DEBUG_HERE();
Igor Filatov's avatar
Igor Filatov committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280

	struct libusb_transfer *transfer = libusb_alloc_transfer(0);
	if (!transfer) {
		fpi_ssm_mark_aborted(ssm, -ENOMEM);
		return;
	}
	elandev->cur_transfer = transfer;

	libusb_fill_bulk_transfer(transfer, dev->udev, ELAN_EP_CMD_OUT,
				  (unsigned char *)elandev->cmds[elandev->
								 cmd_idx].cmd,
				  ELAN_CMD_LEN, elan_cmd_cb, ssm,
				  elandev->cmd_timeout);
	transfer->flags = LIBUSB_TRANSFER_FREE_TRANSFER;
	int r = libusb_submit_transfer(transfer);
	if (r < 0)
		fpi_ssm_mark_aborted(ssm, r);

}

static void elan_run_cmds(struct fpi_ssm *ssm, const struct elan_cmd *cmds,
			  size_t cmds_len, int cmd_timeout)
{
281
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
Igor Filatov's avatar
Igor Filatov committed
282
283
	struct elan_dev *elandev = dev->priv;

284
	G_DEBUG_HERE();
Igor Filatov's avatar
Igor Filatov committed
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300

	elandev->cmds = cmds;
	elandev->cmds_len = cmds_len;
	elandev->cmd_idx = 0;
	if (cmd_timeout != -1)
		elandev->cmd_timeout = cmd_timeout;
	elan_run_next_cmd(ssm);
}

enum deactivate_states {
	DEACTIVATE,
	DEACTIVATE_NUM_STATES,
};

static void elan_deactivate_run_state(struct fpi_ssm *ssm)
{
301
	switch (fpi_ssm_get_cur_state(ssm)) {
Igor Filatov's avatar
Igor Filatov committed
302
303
304
305
306
307
308
309
310
	case DEACTIVATE:
		elan_run_cmds(ssm, deactivate_cmds, deactivate_cmds_len,
			      ELAN_CMD_TIMEOUT);
		break;
	}
}

static void deactivate_complete(struct fpi_ssm *ssm)
{
311
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
Igor Filatov's avatar
Igor Filatov committed
312
313
314
315
316
317
318
319

	fpi_imgdev_deactivate_complete(dev);
}

static void elan_deactivate(struct fp_img_dev *dev)
{
	struct elan_dev *elandev = dev->priv;

320
	G_DEBUG_HERE();
Igor Filatov's avatar
Igor Filatov committed
321
322
323
324
325

	elan_dev_reset(elandev);

	struct fpi_ssm *ssm = fpi_ssm_new(dev->dev, elan_deactivate_run_state,
					  DEACTIVATE_NUM_STATES);
326
	fpi_ssm_set_user_data(ssm, dev);
Igor Filatov's avatar
Igor Filatov committed
327
328
329
330
331
332
333
334
335
336
337
338
339
	fpi_ssm_start(ssm, deactivate_complete);
}

enum capture_states {
	CAPTURE_START,
	CAPTURE_WAIT_FINGER,
	CAPTURE_READ_DATA,
	CAPTURE_SAVE_FRAME,
	CAPTURE_NUM_STATES,
};

static void elan_capture_run_state(struct fpi_ssm *ssm)
{
340
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
Igor Filatov's avatar
Igor Filatov committed
341
342
	struct elan_dev *elandev = dev->priv;

343
	switch (fpi_ssm_get_cur_state(ssm)) {
Igor Filatov's avatar
Igor Filatov committed
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
	case CAPTURE_START:
		elan_run_cmds(ssm, capture_start_cmds, capture_start_cmds_len,
			      ELAN_CMD_TIMEOUT);
		break;
	case CAPTURE_WAIT_FINGER:
		elan_run_cmds(ssm, capture_wait_finger_cmds,
			      capture_wait_finger_cmds_len, -1);
		break;
	case CAPTURE_READ_DATA:
		/* 0x55 - finger present
		 * 0xff - device not calibrated */
		if (elandev->last_read && elandev->last_read[0] == 0x55) {
			fpi_imgdev_report_finger_status(dev, TRUE);
			elan_run_cmds(ssm, read_cmds, read_cmds_len,
				      ELAN_CMD_TIMEOUT);
		} else
			fpi_ssm_mark_aborted(ssm, FP_VERIFY_RETRY);
		break;
	case CAPTURE_SAVE_FRAME:
		elan_save_frame(dev);
		if (elandev->num_frames < ELAN_MAX_FRAMES) {
			/* quickly stop if finger is removed */
			elandev->cmd_timeout = ELAN_FINGER_TIMEOUT;
			fpi_ssm_jump_to_state(ssm, CAPTURE_WAIT_FINGER);
		}
		break;
	}
}

static void elan_capture_async(void *data)
{
	elan_capture((struct fp_img_dev *)data);
}

static void capture_complete(struct fpi_ssm *ssm)
{
380
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
Igor Filatov's avatar
Igor Filatov committed
381
382
	struct elan_dev *elandev = dev->priv;

383
	G_DEBUG_HERE();
Igor Filatov's avatar
Igor Filatov committed
384
385
386
387
388

	if (elandev->deactivating)
		elan_deactivate(dev);

	/* either max frames captured or timed out waiting for the next frame */
389
390
391
	else if (!fpi_ssm_get_error(ssm)
		 || (fpi_ssm_get_error(ssm) == -ETIMEDOUT
		     && fpi_ssm_get_cur_state(ssm) == CAPTURE_WAIT_FINGER))
Igor Filatov's avatar
Igor Filatov committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
		if (elandev->num_frames >= ELAN_MIN_FRAMES) {
			elan_submit_image(dev);
			fpi_imgdev_report_finger_status(dev, FALSE);
		} else
			fpi_imgdev_session_error(dev,
						 FP_VERIFY_RETRY_TOO_SHORT);

	/* other error
	 * It says "...session_error" but repotring 1 during verification
	 * makes it successful! */
	else
		fpi_imgdev_session_error(dev, FP_VERIFY_NO_MATCH);

	/* When enrolling the lib won't restart the capture after a stage has
	 * completed, so we need to keep feeding it images till it's had enough.
	 * But after that it can't finalize enrollemnt until this callback exits.
	 * That's why we schedule elan_capture instead of running it directly. */
409
	if (fpi_dev_get_dev_state(dev->dev) == DEV_STATE_ENROLLING
Igor Filatov's avatar
Igor Filatov committed
410
411
412
413
414
415
416
417
418
419
	    && !fpi_timeout_add(10, elan_capture_async, dev))
		fpi_imgdev_session_error(dev, -ETIME);

	fpi_ssm_free(ssm);
}

static void elan_capture(struct fp_img_dev *dev)
{
	struct elan_dev *elandev = dev->priv;

420
	G_DEBUG_HERE();
Igor Filatov's avatar
Igor Filatov committed
421
422
423
424

	elan_dev_reset(elandev);
	struct fpi_ssm *ssm =
	    fpi_ssm_new(dev->dev, elan_capture_run_state, CAPTURE_NUM_STATES);
425
	fpi_ssm_set_user_data(ssm, dev);
Igor Filatov's avatar
Igor Filatov committed
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
	fpi_ssm_start(ssm, capture_complete);
}

enum calibrate_states {
	CALIBRATE_START_1,
	CALIBRATE_READ_DATA_1,
	CALIBRATE_END_1,
	CALIBRATE_START_2,
	CALIBRATE_READ_DATA_2,
	CALIBRATE_END_2,
	CALIBRATE_NUM_STATES,
};

static void elan_calibrate_run_state(struct fpi_ssm *ssm)
{
441
	switch (fpi_ssm_get_cur_state(ssm)) {
Igor Filatov's avatar
Igor Filatov committed
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
	case CALIBRATE_START_1:
	case CALIBRATE_START_2:
		elan_run_cmds(ssm, calibrate_start_cmds,
			      calibrate_start_cmds_len, ELAN_CMD_TIMEOUT);
		break;
	case CALIBRATE_READ_DATA_1:
	case CALIBRATE_READ_DATA_2:
		elan_run_cmds(ssm, read_cmds, read_cmds_len, ELAN_CMD_TIMEOUT);
		break;
	case CALIBRATE_END_1:
	case CALIBRATE_END_2:
		elan_run_cmds(ssm, calibrate_end_cmds, calibrate_end_cmds_len,
			      ELAN_CMD_TIMEOUT);
	}
}

static void calibrate_complete(struct fpi_ssm *ssm)
{
460
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
Igor Filatov's avatar
Igor Filatov committed
461
462
	struct elan_dev *elandev = dev->priv;

463
	G_DEBUG_HERE();
Igor Filatov's avatar
Igor Filatov committed
464
465
466

	if (elandev->deactivating)
		elan_deactivate(dev);
467
468
	else if (fpi_ssm_get_error(ssm))
		fpi_imgdev_session_error(dev, fpi_ssm_get_error(ssm));
Igor Filatov's avatar
Igor Filatov committed
469
	else {
470
		fpi_imgdev_activate_complete(dev, fpi_ssm_get_error(ssm));
Igor Filatov's avatar
Igor Filatov committed
471
472
473
474
475
476
477
478
479
		elan_capture(dev);
	}
	fpi_ssm_free(ssm);
}

static void elan_calibrate(struct fp_img_dev *dev)
{
	struct elan_dev *elandev = dev->priv;

480
	G_DEBUG_HERE();
Igor Filatov's avatar
Igor Filatov committed
481
482
483
484

	elan_dev_reset(elandev);
	struct fpi_ssm *ssm = fpi_ssm_new(dev->dev, elan_calibrate_run_state,
					  CALIBRATE_NUM_STATES);
485
	fpi_ssm_set_user_data(ssm, dev);
Igor Filatov's avatar
Igor Filatov committed
486
487
488
489
490
491
492
493
494
495
496
497
498
499
	fpi_ssm_start(ssm, calibrate_complete);
}

enum activate_states {
	ACTIVATE_GET_SENSOR_DIM,
	ACTIVATE_SET_SENSOR_DIM,
	ACTIVATE_START,
	ACTIVATE_READ_DATA,
	ACTIVATE_END,
	ACTIVATE_NUM_STATES,
};

static void elan_activate_run_state(struct fpi_ssm *ssm)
{
500
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
Igor Filatov's avatar
Igor Filatov committed
501
502
	struct elan_dev *elandev = dev->priv;

503
	switch (fpi_ssm_get_cur_state(ssm)) {
Igor Filatov's avatar
Igor Filatov committed
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
	case ACTIVATE_GET_SENSOR_DIM:
		elan_run_cmds(ssm, get_sensor_dim_cmds, get_sensor_dim_cmds_len,
			      ELAN_CMD_TIMEOUT);
		break;
	case ACTIVATE_SET_SENSOR_DIM:
		elandev->frame_width = elandev->last_read[2];
		elandev->raw_frame_width = elandev->last_read[0];
		elandev->frame_height =
		    elandev->raw_frame_width - 2 * ELAN_FRAME_MARGIN;
		fpi_ssm_next_state(ssm);
		break;
	case ACTIVATE_START:
		elan_run_cmds(ssm, init_start_cmds, init_start_cmds_len,
			      ELAN_CMD_TIMEOUT);
		break;
	case ACTIVATE_READ_DATA:
		elan_run_cmds(ssm, read_cmds, read_cmds_len, ELAN_CMD_TIMEOUT);
		break;
	case ACTIVATE_END:
		elan_run_cmds(ssm, init_end_cmds, init_end_cmds_len,
			      ELAN_CMD_TIMEOUT);
	}
}

static void activate_complete(struct fpi_ssm *ssm)
{
530
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
Igor Filatov's avatar
Igor Filatov committed
531
532
	struct elan_dev *elandev = dev->priv;

533
	G_DEBUG_HERE();
Igor Filatov's avatar
Igor Filatov committed
534
535
536

	if (elandev->deactivating)
		elan_deactivate(dev);
537
538
	else if (fpi_ssm_get_error(ssm))
		fpi_imgdev_session_error(dev, fpi_ssm_get_error(ssm));
Igor Filatov's avatar
Igor Filatov committed
539
540
541
542
543
544
545
546
547
	else
		elan_calibrate(dev);
	fpi_ssm_free(ssm);
}

static int dev_activate(struct fp_img_dev *dev, enum fp_imgdev_state state)
{
	struct elan_dev *elandev = dev->priv;

548
	G_DEBUG_HERE();
Igor Filatov's avatar
Igor Filatov committed
549
550
551
552

	elan_dev_reset(elandev);
	struct fpi_ssm *ssm =
	    fpi_ssm_new(dev->dev, elan_activate_run_state, ACTIVATE_NUM_STATES);
553
	fpi_ssm_set_user_data(ssm, dev);
Igor Filatov's avatar
Igor Filatov committed
554
555
556
557
558
559
560
561
562
563
	fpi_ssm_start(ssm, activate_complete);

	return 0;
}

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

564
	G_DEBUG_HERE();
Igor Filatov's avatar
Igor Filatov committed
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580

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

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

static void dev_deinit(struct fp_img_dev *dev)
{
	struct elan_dev *elandev = dev->priv;

581
	G_DEBUG_HERE();
Igor Filatov's avatar
Igor Filatov committed
582
583
584
585
586
587
588
589
590
591
592

	elan_dev_reset(elandev);
	g_free(elandev);
	libusb_release_interface(dev->udev, 0);
	fpi_imgdev_close_complete(dev);
}

static void dev_deactivate(struct fp_img_dev *dev)
{
	struct elan_dev *elandev = dev->priv;

593
	G_DEBUG_HERE();
Igor Filatov's avatar
Igor Filatov committed
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

	elandev->deactivating = TRUE;

	if (elandev->cur_transfer)
		libusb_cancel_transfer(elandev->cur_transfer);
	else
		elan_deactivate(dev);
}

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

struct fp_img_driver elan_driver = {
	.driver = {
		   .id = ELAN_ID,
		   .name = FP_COMPONENT,
		   .full_name = "ElanTech Fingerprint Sensor",
		   .id_table = id_table,
		   .scan_type = FP_SCAN_TYPE_SWIPE,
		   },
	.flags = 0,

	.bz3_threshold = 22,

	.open = dev_init,
	.close = dev_deinit,
	.activate = dev_activate,
	.deactivate = dev_deactivate,
};