upeksonly.c 34.6 KB
Newer Older
1
2
3
4
/*
 * UPEK TouchStrip Sensor-Only driver for libfprint
 * Copyright (C) 2008 Daniel Drake <dsd@gentoo.org>
 *
5
6
7
 * TCS4C (USB ID 147e:1000) support:
 * Copyright (C) 2010 Hugo Grostabussiat <dw23.devel@gmail.com>
 *
8
9
10
 * TCRD5B (USB ID 147e:1001) support:
 * Copyright (C) 2014 Vasily Khoruzhick <anarsoul@gmail.com>
 *
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 * 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 "upeksonly"

28
#include "drivers_api.h"
29
#include "upeksonly.h"
30

31
32
#define CTRL_TIMEOUT	1000
#define NUM_BULK_TRANSFERS 24
33
#define MAX_ROWS 2048
34
#define MIN_ROWS 64
35

36
37
38
39
40
#define BLANK_THRESHOLD 250
#define FINGER_PRESENT_THRESHOLD 32
#define FINGER_REMOVED_THRESHOLD 100
#define DIFF_THRESHOLD 13

41
enum {
42
43
44
	UPEKSONLY_2016,
	UPEKSONLY_1000,
	UPEKSONLY_1001,
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
struct img_transfer_data {
	int idx;
	struct fp_img_dev *dev;
	gboolean flying;
	gboolean cancelling;
};

enum sonly_kill_transfers_action {
	NOT_KILLING = 0,

	/* abort a SSM with an error code */
	ABORT_SSM,

	/* report an image session error */
	IMG_SESSION_ERROR,

	/* iterate a SSM to the next state */
	ITERATE_SSM,

	/* call a callback */
	EXEC_CALLBACK,
};

70
71
72
73
74
75
enum sonly_fs {
	AWAIT_FINGER,
	FINGER_DETECTED,
	FINGER_REMOVED,
};

76
77
78
79
80
struct sonly_dev {
	gboolean capturing;
	gboolean deactivating;
	uint8_t read_reg_result;

81
	int dev_model;
82
	int img_width;
83

84
85
86
87
88
89
90
91
92
93
94
95
	struct fpi_ssm *loopsm;
	struct libusb_transfer *img_transfer[NUM_BULK_TRANSFERS];
	struct img_transfer_data *img_transfer_data;
	int num_flying;

	GSList *rows;
	size_t num_rows;
	unsigned char *rowbuf;
	int rowbuf_offset;

	int wraparounds;
	int num_blank;
96
97
	int num_nonblank;
	enum sonly_fs finger_state;
98
99
100
101
102
103
104
105
106
107
	int last_seqnum;

	enum sonly_kill_transfers_action killing_transfers;
	int kill_status_code;
	union {
		struct fpi_ssm *kill_ssm;
		void (*kill_cb)(struct fp_img_dev *dev);
	};
};

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

/* Calculade squared standand deviation of sum of two lines */
static int upeksonly_get_deviation2(struct fpi_line_asmbl_ctx *ctx,
			  GSList *line1, GSList *line2)
{
	unsigned char *buf1 = line1->data, *buf2 = line2->data;
	int res = 0, mean = 0, i;
	for (i = 0; i < ctx->line_width; i+= 2)
		mean += (int)buf1[i + 1] + (int)buf2[i];

	mean /= (ctx->line_width / 2);

	for (i = 0; i < ctx->line_width; i+= 2) {
		int dev = (int)buf1[i + 1] + (int)buf2[i] - mean;
		res += dev*dev;
	}

	return res / (ctx->line_width / 2);
}


static unsigned char upeksonly_get_pixel(struct fpi_line_asmbl_ctx *ctx,
				   GSList *row,
				   unsigned x)
{
	unsigned char *buf;
	unsigned offset;

	/* The scans from this device are rolled right by two colums */
	if (x < ctx->line_width - 2)
		offset = x + 2;
	else if ((x > ctx->line_width - 2) && (x < ctx->line_width))
		offset = x - (ctx->line_width - 2);
	else
		return 0;
	/* Each 2nd pixel is shifted 2 pixels down */
	if ((!(x & 1)) && g_slist_next(row) && g_slist_next(g_slist_next(row)))
		buf = g_slist_next(g_slist_next(row))->data;
	else
		buf = row->data;

	return buf[offset];
}

static struct fpi_line_asmbl_ctx assembling_ctx = {
	.max_height = 1024,
	.resolution = 8,
	.median_filter_size = 25,
	.max_search_offset = 30,
	.get_deviation = upeksonly_get_deviation2,
	.get_pixel = upeksonly_get_pixel,
};

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
/***** IMAGE PROCESSING *****/

static void free_img_transfers(struct sonly_dev *sdev)
{
	int i;
	for (i = 0; i < NUM_BULK_TRANSFERS; i++) {
		struct libusb_transfer *transfer = sdev->img_transfer[i];
		if (!transfer)
			continue;

		g_free(transfer->buffer);
		libusb_free_transfer(transfer);
	}
	g_free(sdev->img_transfer_data);
}

static void last_transfer_killed(struct fp_img_dev *dev)
{
	struct sonly_dev *sdev = dev->priv;
	switch (sdev->killing_transfers) {
	case ABORT_SSM:
		fp_dbg("abort ssm error %d", sdev->kill_status_code);
		fpi_ssm_mark_aborted(sdev->kill_ssm, sdev->kill_status_code);
		return;
	case ITERATE_SSM:
		fp_dbg("iterate ssm");
		fpi_ssm_next_state(sdev->kill_ssm);
		return;
	case IMG_SESSION_ERROR:
		fp_dbg("session error %d", sdev->kill_status_code);
		fpi_imgdev_session_error(dev, sdev->kill_status_code);
		return;
	default:
		return;
	}
}

static void cancel_img_transfers(struct fp_img_dev *dev)
{
	struct sonly_dev *sdev = dev->priv;
	int i;

	if (sdev->num_flying == 0) {
		last_transfer_killed(dev);
		return;
	}

	for (i = 0; i < NUM_BULK_TRANSFERS; i++) {
		struct img_transfer_data *idata = &sdev->img_transfer_data[i];
		if (!idata->flying || idata->cancelling)
			continue;
		fp_dbg("cancelling transfer %d", i);
		int r = libusb_cancel_transfer(sdev->img_transfer[i]);
		if (r < 0)
			fp_dbg("cancel failed error %d", r);
		idata->cancelling = TRUE;
	}
}

static gboolean is_capturing(struct sonly_dev *sdev)
{
222
	return sdev->num_rows < MAX_ROWS && (sdev->finger_state != FINGER_REMOVED);
223
224
225
226
227
}

static void handoff_img(struct fp_img_dev *dev)
{
	struct sonly_dev *sdev = dev->priv;
228
229
	struct fp_img *img;

230
231
232
233
234
235
236
	GSList *elem = sdev->rows;

	if (!elem) {
		fp_err("no rows?");
		return;
	}

237
238
	sdev->rows = g_slist_reverse(sdev->rows);

239
	fp_dbg("%lu rows", sdev->num_rows);
240
241
242
	img = fpi_assemble_lines(&assembling_ctx, sdev->rows, sdev->num_rows);

	g_slist_free_full(sdev->rows, g_free);
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
	sdev->rows = NULL;

	fpi_imgdev_image_captured(dev, img);
	fpi_imgdev_report_finger_status(dev, FALSE);

	sdev->killing_transfers = ITERATE_SSM;
	sdev->kill_ssm = sdev->loopsm;
	cancel_img_transfers(dev);
}

static void row_complete(struct fp_img_dev *dev)
{
	struct sonly_dev *sdev = dev->priv;
	sdev->rowbuf_offset = -1;

	if (sdev->num_rows > 0) {
		unsigned char *lastrow = sdev->rows->data;
260
		int std_sq_dev, mean_sq_diff;
261

262
263
		std_sq_dev = fpi_std_sq_dev(sdev->rowbuf, sdev->img_width);
		mean_sq_diff = fpi_mean_sq_diff_norm(lastrow, sdev->rowbuf, sdev->img_width);
264
265
266
267
268
269
270
271

		switch (sdev->finger_state) {
		case AWAIT_FINGER:
			if (sdev->deactivating) {
				sdev->killing_transfers = ITERATE_SSM;
				sdev->kill_ssm = sdev->loopsm;
				cancel_img_transfers(dev);
			}
272
273
			fp_dbg("std_sq_dev: %d", std_sq_dev);
			if (std_sq_dev > BLANK_THRESHOLD) {
274
275
276
277
278
				sdev->num_nonblank++;
			} else {
				sdev->num_nonblank = 0;
			}

279
			if (sdev->num_nonblank > FINGER_PRESENT_THRESHOLD) {
280
281
				sdev->finger_state = FINGER_DETECTED;
				fpi_imgdev_report_finger_status(dev, TRUE);
282
283
			} else {
				return;
284
285
286
287
288
289
290
			}
			break;
		case FINGER_DETECTED:
		case FINGER_REMOVED:
		default:
			break;
		}
291

292
		if (std_sq_dev > BLANK_THRESHOLD) {
293
294
295
			sdev->num_blank = 0;
		} else {
			sdev->num_blank++;
296
297
298
299
300
301
302
			/* Don't consider the scan complete unless theres at least
			 * MIN_ROWS recorded or very long blank read occurred.
			 *
			 * Typical problem spot: one brief touch before starting the
			 * actual scan. Happens most commonly if scan is started
			 * from before the first joint resulting in a gap after the inital touch.
			 */
303
			if (sdev->num_blank > FINGER_REMOVED_THRESHOLD) {
304
				sdev->finger_state = FINGER_REMOVED;
305
				fp_dbg("detected finger removal. Blank rows: %d, Full rows: %lu", sdev->num_blank, sdev->num_rows);
306
307
308
309
				handoff_img(dev);
				return;
			}
		}
310
311
312
		fp_dbg("mean_sq_diff: %d, std_sq_dev: %d", mean_sq_diff, std_sq_dev);
		fp_dbg("num_blank: %d", sdev->num_blank);
		if (mean_sq_diff < DIFF_THRESHOLD) {
313
			return;
314
		}
315
316
	}

317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
	switch (sdev->finger_state) {
	case AWAIT_FINGER:
		if (!sdev->num_rows) {
			sdev->rows = g_slist_prepend(sdev->rows, sdev->rowbuf);
			sdev->num_rows++;
		} else {
			return;
		}
		break;
	case FINGER_DETECTED:
	case FINGER_REMOVED:
		sdev->rows = g_slist_prepend(sdev->rows, sdev->rowbuf);
		sdev->num_rows++;
		break;
	}
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
	sdev->rowbuf = NULL;

	if (sdev->num_rows >= MAX_ROWS) {
		fp_dbg("row limit met");
		handoff_img(dev);
	}
}

/* add data to row buffer */
static void add_to_rowbuf(struct fp_img_dev *dev, unsigned char *data, int size)
{
	struct sonly_dev *sdev = dev->priv;

	memcpy(sdev->rowbuf + sdev->rowbuf_offset, data, size);
	sdev->rowbuf_offset += size;
347
	if (sdev->rowbuf_offset >= sdev->img_width)
348
		row_complete(dev);
349

350
351
352
353
354
}

static void start_new_row(struct sonly_dev *sdev, unsigned char *data, int size)
{
	if (!sdev->rowbuf)
355
		sdev->rowbuf = g_malloc(sdev->img_width);
356
	memcpy(sdev->rowbuf, data, size);
357
358
359
360
361
362
363
364
365
366
367
368
	sdev->rowbuf_offset = size;
}

/* returns number of bytes left to be copied into rowbuf (capped to 62)
 * or -1 if we aren't capturing anything */
static int rowbuf_remaining(struct sonly_dev *sdev)
{
	int r;

	if (sdev->rowbuf_offset == -1)
		return -1;

369
	r = sdev->img_width - sdev->rowbuf_offset;
370
371
372
373
374
375
376
377
378
379
380
381
382
	if (r > 62)
		r = 62;
	return r;
}

static void handle_packet(struct fp_img_dev *dev, unsigned char *data)
{
	struct sonly_dev *sdev = dev->priv;
	uint16_t seqnum = data[0] << 8 | data[1];
	int abs_base_addr;
	int for_rowbuf;
	int next_row_addr;
	int diff;
383
384
385
386
	unsigned char dummy_data[62];

	/* Init dummy data to something neutral */
	memset (dummy_data, 204, 62);
387
388
389

	data += 2; /* skip sequence number */
	if (seqnum != sdev->last_seqnum + 1) {
390
391
392
393
394
395
396
397
398
399
400
		if (seqnum != 0 && sdev->last_seqnum != 16383) {
			int missing_data = seqnum - sdev->last_seqnum;
			int i;
			fp_warn("lost %d packets of data between %d and %d", missing_data, sdev->last_seqnum, seqnum );

			/* Minimize distortions for readers that lose a lot of packets */
			for (i =1; i < missing_data; i++) {
				abs_base_addr = (sdev->last_seqnum + 1) * 62;

				/* If possible take the replacement data from last row */
				if (sdev->num_rows > 1) {
401
					int row_left = sdev->img_width - sdev->rowbuf_offset;
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
					unsigned char *last_row = g_slist_nth_data (sdev->rows, 0);

					if (row_left >= 62) {
						memcpy(dummy_data, last_row + sdev->rowbuf_offset, 62);
					} else {
						memcpy(dummy_data, last_row + sdev->rowbuf_offset, row_left);
						memcpy(dummy_data + row_left, last_row , 62 - row_left);
					}
				}

				fp_warn("adding dummy input for %d, i=%d", sdev->last_seqnum + i, i);
				for_rowbuf = rowbuf_remaining(sdev);
				if (for_rowbuf != -1) {
					add_to_rowbuf(dev, dummy_data, for_rowbuf);
					/* row boundary */
					if (for_rowbuf < 62) {
						start_new_row(sdev, dummy_data + for_rowbuf, 62 - for_rowbuf);
					}
420
				} else if (abs_base_addr % sdev->img_width == 0) {
421
422
423
424
					start_new_row(sdev, dummy_data, 62);
				} else {
					/* does the data in the packet reside on a row boundary?
					 * if so capture it */
425
					next_row_addr = ((abs_base_addr / sdev->img_width) + 1) * sdev->img_width;
426
427
428
429
430
431
432
					diff = next_row_addr - abs_base_addr;
					if (diff < 62)
						start_new_row(sdev, dummy_data + diff, 62 - diff);
				}
				sdev->last_seqnum = sdev->last_seqnum + 1;
			}
		}
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
	}
	if (seqnum <= sdev->last_seqnum) {
		fp_dbg("detected wraparound");
		sdev->wraparounds++;
	}

	sdev->last_seqnum = seqnum;
	seqnum += sdev->wraparounds * 16384;
	abs_base_addr = seqnum * 62;

	/* are we already capturing a row? if so append the data to the
	 * row buffer */
	for_rowbuf = rowbuf_remaining(sdev);
	if (for_rowbuf != -1) {
		add_to_rowbuf(dev, data, for_rowbuf);
448
449
450
451
		/*row boundary*/
		if (for_rowbuf < 62) {
			start_new_row(sdev, data + for_rowbuf, 62 - for_rowbuf);
		}
452
453
454
455
		return;
	}

	/* does the packet START on a boundary? if so we want it in full */
456
	if (abs_base_addr % sdev->img_width == 0) {
457
458
459
460
461
462
		start_new_row(sdev, data, 62);
		return;
	}

	/* does the data in the packet reside on a row boundary?
	 * if so capture it */
463
	next_row_addr = ((abs_base_addr / sdev->img_width) + 1) * sdev->img_width;
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
	diff = next_row_addr - abs_base_addr;
	if (diff < 62)
		start_new_row(sdev, data + diff, 62 - diff);
}

static void img_data_cb(struct libusb_transfer *transfer)
{
	struct img_transfer_data *idata = transfer->user_data;
	struct fp_img_dev *dev = idata->dev;
	struct sonly_dev *sdev = dev->priv;
	int i;

	idata->flying = FALSE;
	idata->cancelling = FALSE;
	sdev->num_flying--;

	if (sdev->killing_transfers) {
		if (sdev->num_flying == 0)
			last_transfer_killed(dev);

		/* don't care about error or success if we're terminating */
		return;
	}

	if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
		fp_warn("bad status %d, terminating session", transfer->status);
		sdev->killing_transfers = IMG_SESSION_ERROR;
		sdev->kill_status_code = transfer->status;
		cancel_img_transfers(dev);
	}

	/* there are 64 packets in the transfer buffer
	 * each packet is 64 bytes in length
	 * the first 2 bytes are a sequence number
	 * then there are 62 bytes for image data
	 */
	for (i = 0; i < 4096; i += 64) {
		if (!is_capturing(sdev))
			return;
		handle_packet(dev, transfer->buffer + i);
	}

	if (is_capturing(sdev)) {
		int r = libusb_submit_transfer(transfer);
		if (r < 0) {
			fp_warn("failed resubmit, error %d", r);
			sdev->killing_transfers = IMG_SESSION_ERROR;
			sdev->kill_status_code = r;
			cancel_img_transfers(dev);
			return;
		}
		sdev->num_flying++;
		idata->flying = TRUE;
	}
}

/***** STATE MACHINE HELPERS *****/

struct write_regs_data {
	struct fpi_ssm *ssm;
	struct libusb_transfer *transfer;
	const struct sonly_regwrite *regs;
	size_t num_regs;
	size_t regs_written;
};

static void write_regs_finished(struct write_regs_data *wrdata, int result)
{
	g_free(wrdata->transfer->buffer);
	libusb_free_transfer(wrdata->transfer);
	if (result == 0)
		fpi_ssm_next_state(wrdata->ssm);
	else
		fpi_ssm_mark_aborted(wrdata->ssm, result);
	g_free(wrdata);
}


static void write_regs_iterate(struct write_regs_data *wrdata)
{
	struct libusb_control_setup *setup;
	const struct sonly_regwrite *regwrite;
	int r;

	if (wrdata->regs_written >= wrdata->num_regs) {
		write_regs_finished(wrdata, 0);
		return;
	}

	regwrite = &wrdata->regs[wrdata->regs_written];

	fp_dbg("set %02x=%02x", regwrite->reg, regwrite->value);
	setup = libusb_control_transfer_get_setup(wrdata->transfer);
	setup->wIndex = regwrite->reg;
	wrdata->transfer->buffer[LIBUSB_CONTROL_SETUP_SIZE] = regwrite->value;

	r = libusb_submit_transfer(wrdata->transfer);
	if (r < 0)
		write_regs_finished(wrdata, r);
}

static void write_regs_cb(struct libusb_transfer *transfer)
{
	struct write_regs_data *wrdata = transfer->user_data;
	if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
		write_regs_finished(wrdata, transfer->status);
		return;
	}

	wrdata->regs_written++;
	write_regs_iterate(wrdata);
}

static void sm_write_regs(struct fpi_ssm *ssm,
	const struct sonly_regwrite *regs, size_t num_regs)
{
	struct write_regs_data *wrdata = g_malloc(sizeof(*wrdata));
	unsigned char *data;
582
	struct fp_dev *dev;
583
584
585
586
587
588
589
590
591
592

	wrdata->transfer = libusb_alloc_transfer(0);
	if (!wrdata->transfer) {
		g_free(wrdata);
		fpi_ssm_mark_aborted(ssm, -ENOMEM);
		return;
	}

	data = g_malloc(LIBUSB_CONTROL_SETUP_SIZE + 1);
	libusb_fill_control_setup(data, 0x40, 0x0c, 0, 0, 1);
593
	dev = fpi_ssm_get_dev(ssm);
594
595
	libusb_fill_control_transfer(wrdata->transfer,
		fpi_dev_get_usb_dev(dev), data,
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
		write_regs_cb, wrdata, CTRL_TIMEOUT);
	wrdata->transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK;

	wrdata->ssm = ssm;
	wrdata->regs = regs;
	wrdata->num_regs = num_regs;
	wrdata->regs_written = 0;
	write_regs_iterate(wrdata);
}

static void sm_write_reg_cb(struct libusb_transfer *transfer)
{
	struct fpi_ssm *ssm = transfer->user_data;
	g_free(transfer->buffer);
	if (transfer->status != LIBUSB_TRANSFER_COMPLETED)
		fpi_ssm_mark_aborted(ssm, -EIO);
	else
		fpi_ssm_next_state(ssm);

}

static void sm_write_reg(struct fpi_ssm *ssm, uint8_t reg, uint8_t value)
{
619
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
	struct libusb_transfer *transfer = libusb_alloc_transfer(0);
	unsigned char *data;
	int r;

	if (!transfer) {
		fpi_ssm_mark_aborted(ssm, -ENOMEM);
		return;
	}

	fp_dbg("set %02x=%02x", reg, value);
	data = g_malloc(LIBUSB_CONTROL_SETUP_SIZE + 1);
	libusb_fill_control_setup(data, 0x40, 0x0c, 0, reg, 1);
	libusb_fill_control_transfer(transfer, dev->udev, data, sm_write_reg_cb,
		ssm, CTRL_TIMEOUT);

	data[LIBUSB_CONTROL_SETUP_SIZE] = value;
	transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK |
		LIBUSB_TRANSFER_FREE_TRANSFER;

	r = libusb_submit_transfer(transfer);
	if (r < 0) {
		g_free(data);
		libusb_free_transfer(transfer);
		fpi_ssm_mark_aborted(ssm, r);
	}
}

static void sm_read_reg_cb(struct libusb_transfer *transfer)
{
	struct fpi_ssm *ssm = transfer->user_data;
650
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
	struct sonly_dev *sdev = dev->priv;

	if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
		fpi_ssm_mark_aborted(ssm, -EIO);
	} else {
		sdev->read_reg_result = libusb_control_transfer_get_data(transfer)[0];
		fp_dbg("read reg result = %02x", sdev->read_reg_result);
		fpi_ssm_next_state(ssm);
	}

	g_free(transfer->buffer);
}

static void sm_read_reg(struct fpi_ssm *ssm, uint8_t reg)
{
666
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
	struct libusb_transfer *transfer = libusb_alloc_transfer(0);
	unsigned char *data;
	int r;

	if (!transfer) {
		fpi_ssm_mark_aborted(ssm, -ENOMEM);
		return;
	}

	fp_dbg("read reg %02x", reg);
	data = g_malloc(LIBUSB_CONTROL_SETUP_SIZE + 8);
	libusb_fill_control_setup(data, 0xc0, 0x0c, 0, reg, 8);
	libusb_fill_control_transfer(transfer, dev->udev, data, sm_read_reg_cb,
		ssm, CTRL_TIMEOUT);
	transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK |
		LIBUSB_TRANSFER_FREE_TRANSFER;

	r = libusb_submit_transfer(transfer);
	if (r < 0) {
		g_free(data);
		libusb_free_transfer(transfer);
		fpi_ssm_mark_aborted(ssm, r);
	}
}

static void sm_await_intr_cb(struct libusb_transfer *transfer)
{
	struct fpi_ssm *ssm = transfer->user_data;
695
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
696
	struct sonly_dev *sdev = dev->priv;
697
698
699
700
701
702
703
704
705
706
707
708

	if (transfer->status != LIBUSB_TRANSFER_COMPLETED) {
		g_free(transfer->buffer);
		fpi_ssm_mark_aborted(ssm, transfer->status);
		return;
	}

	fp_dbg("interrupt received: %02x %02x %02x %02x",
		transfer->buffer[0], transfer->buffer[1],
		transfer->buffer[2], transfer->buffer[3]);
	g_free(transfer->buffer);

709
	sdev->finger_state = FINGER_DETECTED;
710
711
712
713
714
715
	fpi_imgdev_report_finger_status(dev, TRUE);
	fpi_ssm_next_state(ssm);
}

static void sm_await_intr(struct fpi_ssm *ssm)
{
716
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
717
718
719
720
721
722
723
724
725
	struct libusb_transfer *transfer = libusb_alloc_transfer(0);
	unsigned char *data;
	int r;

	if (!transfer) {
		fpi_ssm_mark_aborted(ssm, -ENOMEM);
		return;
	}

726
	G_DEBUG_HERE();
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
	data = g_malloc(4);
	libusb_fill_interrupt_transfer(transfer, dev->udev, 0x83, data, 4,
		sm_await_intr_cb, ssm, 0);
	transfer->flags = LIBUSB_TRANSFER_SHORT_NOT_OK |
		LIBUSB_TRANSFER_FREE_TRANSFER;

	r = libusb_submit_transfer(transfer);
	if (r < 0) {
		libusb_free_transfer(transfer);
		g_free(data);
		fpi_ssm_mark_aborted(ssm, r);
	}
}

/***** AWAIT FINGER *****/

743
744
745
746
747
748
749
750
751
752
753
754
enum awfsm_2016_states {
	AWFSM_2016_WRITEV_1,
	AWFSM_2016_READ_01,
	AWFSM_2016_WRITE_01,
	AWFSM_2016_WRITEV_2,
	AWFSM_2016_READ_13,
	AWFSM_2016_WRITE_13,
	AWFSM_2016_WRITEV_3,
	AWFSM_2016_READ_07,
	AWFSM_2016_WRITE_07,
	AWFSM_2016_WRITEV_4,
	AWFSM_2016_NUM_STATES,
755
756
};

757
758
759
760
761
762
enum awfsm_1000_states {
	AWFSM_1000_WRITEV_1,
	AWFSM_1000_WRITEV_2,
	AWFSM_1000_NUM_STATES,
};

763
static void awfsm_2016_run_state(struct fpi_ssm *ssm)
764
{
765
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
766
767
	struct sonly_dev *sdev = dev->priv;

768
	switch (fpi_ssm_get_cur_state(ssm)) {
769
770
	case AWFSM_2016_WRITEV_1:
		sm_write_regs(ssm, awfsm_2016_writev_1, G_N_ELEMENTS(awfsm_2016_writev_1));
771
		break;
772
	case AWFSM_2016_READ_01:
773
774
		sm_read_reg(ssm, 0x01);
		break;
775
	case AWFSM_2016_WRITE_01:
776
777
778
779
780
		if (sdev->read_reg_result != 0xc6)
			sm_write_reg(ssm, 0x01, 0x46);
		else
			sm_write_reg(ssm, 0x01, 0xc6);
		break;
781
782
	case AWFSM_2016_WRITEV_2:
		sm_write_regs(ssm, awfsm_2016_writev_2, G_N_ELEMENTS(awfsm_2016_writev_2));
783
		break;
784
	case AWFSM_2016_READ_13:
785
786
		sm_read_reg(ssm, 0x13);
		break;
787
	case AWFSM_2016_WRITE_13:
788
789
790
791
792
		if (sdev->read_reg_result != 0x45)
			sm_write_reg(ssm, 0x13, 0x05);
		else
			sm_write_reg(ssm, 0x13, 0x45);
		break;
793
794
	case AWFSM_2016_WRITEV_3:
		sm_write_regs(ssm, awfsm_2016_writev_3, G_N_ELEMENTS(awfsm_2016_writev_3));
795
		break;
796
	case AWFSM_2016_READ_07:
797
798
		sm_read_reg(ssm, 0x07);
		break;
799
	case AWFSM_2016_WRITE_07:
800
801
802
803
		if (sdev->read_reg_result != 0x10 && sdev->read_reg_result != 0x90)
			fp_warn("odd reg7 value %x", sdev->read_reg_result);
		sm_write_reg(ssm, 0x07, sdev->read_reg_result);
		break;
804
805
	case AWFSM_2016_WRITEV_4:
		sm_write_regs(ssm, awfsm_2016_writev_4, G_N_ELEMENTS(awfsm_2016_writev_4));
806
807
808
809
		break;
	}
}

810
811
static void awfsm_1000_run_state(struct fpi_ssm *ssm)
{
812
	switch (fpi_ssm_get_cur_state(ssm)) {
813
814
815
816
817
818
819
820
821
	case AWFSM_1000_WRITEV_1:
		sm_write_regs(ssm, awfsm_1000_writev_1, G_N_ELEMENTS(awfsm_1000_writev_1));
		break;
	case AWFSM_1000_WRITEV_2:
		sm_write_regs(ssm, awfsm_1000_writev_2, G_N_ELEMENTS(awfsm_1000_writev_2));
		break;
	}
}

822
823
/***** CAPTURE MODE *****/

824
825
826
827
828
829
830
enum capsm_2016_states {
	CAPSM_2016_INIT,
	CAPSM_2016_WRITE_15,
	CAPSM_2016_WRITE_30,
	CAPSM_2016_FIRE_BULK,
	CAPSM_2016_WRITEV,
	CAPSM_2016_NUM_STATES,
831
832
};

833
834
835
836
837
838
839
enum capsm_1000_states {
	CAPSM_1000_INIT,
	CAPSM_1000_FIRE_BULK,
	CAPSM_1000_WRITEV,
	CAPSM_1000_NUM_STATES,
};

840
841
842
843
844
845
846
847
848
849
850
enum capsm_1001_states {
	CAPSM_1001_INIT,
	CAPSM_1001_FIRE_BULK,
	CAPSM_1001_WRITEV_1,
	CAPSM_1001_WRITEV_2,
	CAPSM_1001_WRITEV_3,
	CAPSM_1001_WRITEV_4,
	CAPSM_1001_WRITEV_5,
	CAPSM_1001_NUM_STATES,
};

851
852
static void capsm_fire_bulk(struct fpi_ssm *ssm)
{
853
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
	struct sonly_dev *sdev = dev->priv;
	int i;
	for (i = 0; i < NUM_BULK_TRANSFERS; i++) {
		int r = libusb_submit_transfer(sdev->img_transfer[i]);
		if (r < 0) {
			if (i == 0) {
				/* first one failed: easy peasy */
				fpi_ssm_mark_aborted(ssm, r);
				return;
			}

			/* cancel all flying transfers, and request that the SSM
			 * gets aborted when the last transfer has dropped out of
			 * the sky */
			sdev->killing_transfers = ABORT_SSM;
			sdev->kill_ssm = ssm;
			sdev->kill_status_code = r;
			cancel_img_transfers(dev);
			return;
		}
		sdev->img_transfer_data[i].flying = TRUE;
		sdev->num_flying++;
	}
	sdev->capturing = TRUE;
	fpi_ssm_next_state(ssm);
}

881
static void capsm_2016_run_state(struct fpi_ssm *ssm)
882
{
883
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
884
885
	struct sonly_dev *sdev = dev->priv;

886
	switch (fpi_ssm_get_cur_state(ssm)) {
887
	case CAPSM_2016_INIT:
888
889
890
891
		sdev->rowbuf_offset = -1;
		sdev->num_rows = 0;
		sdev->wraparounds = -1;
		sdev->num_blank = 0;
892
893
		sdev->num_nonblank = 0;
		sdev->finger_state = FINGER_DETECTED;
894
895
896
897
		sdev->last_seqnum = 16383;
		sdev->killing_transfers = 0;
		fpi_ssm_next_state(ssm);
		break;
898
	case CAPSM_2016_WRITE_15:
899
900
		sm_write_reg(ssm, 0x15, 0x20);
		break;
901
	case CAPSM_2016_WRITE_30:
902
903
		sm_write_reg(ssm, 0x30, 0xe0);
		break;
904
	case CAPSM_2016_FIRE_BULK: ;
905
		capsm_fire_bulk (ssm);
906
		break;
907
908
	case CAPSM_2016_WRITEV:
		sm_write_regs(ssm, capsm_2016_writev, G_N_ELEMENTS(capsm_2016_writev));
909
910
911
912
		break;
	}
}

913
914
static void capsm_1000_run_state(struct fpi_ssm *ssm)
{
915
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
916
917
	struct sonly_dev *sdev = dev->priv;

918
	switch (fpi_ssm_get_cur_state(ssm)) {
919
920
921
922
923
	case CAPSM_1000_INIT:
		sdev->rowbuf_offset = -1;
		sdev->num_rows = 0;
		sdev->wraparounds = -1;
		sdev->num_blank = 0;
924
925
		sdev->num_nonblank = 0;
		sdev->finger_state = FINGER_DETECTED;
926
927
928
929
930
931
932
933
934
935
936
937
938
		sdev->last_seqnum = 16383;
		sdev->killing_transfers = 0;
		fpi_ssm_next_state(ssm);
		break;
	case CAPSM_1000_FIRE_BULK: ;
		capsm_fire_bulk (ssm);
		break;
	case CAPSM_1000_WRITEV:
		sm_write_regs(ssm, capsm_1000_writev, G_N_ELEMENTS(capsm_1000_writev));
		break;
	}
}

939
940
static void capsm_1001_run_state(struct fpi_ssm *ssm)
{
941
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
942
943
	struct sonly_dev *sdev = dev->priv;

944
	switch (fpi_ssm_get_cur_state(ssm)) {
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
	case CAPSM_1001_INIT:
		sdev->rowbuf_offset = -1;
		sdev->num_rows = 0;
		sdev->wraparounds = -1;
		sdev->num_blank = 0;
		sdev->num_nonblank = 0;
		sdev->finger_state = AWAIT_FINGER;
		sdev->last_seqnum = 16383;
		sdev->killing_transfers = 0;
		fpi_ssm_next_state(ssm);
		break;
	case CAPSM_1001_FIRE_BULK: ;
		capsm_fire_bulk (ssm);
		break;
	case CAPSM_1001_WRITEV_1:
		sm_write_regs(ssm, capsm_1001_writev_1, G_N_ELEMENTS(capsm_1001_writev_1));
		break;
	case CAPSM_1001_WRITEV_2:
		sm_write_regs(ssm, capsm_1001_writev_2, G_N_ELEMENTS(capsm_1001_writev_2));
		break;
	case CAPSM_1001_WRITEV_3:
		sm_write_regs(ssm, capsm_1001_writev_3, G_N_ELEMENTS(capsm_1001_writev_3));
		break;
	case CAPSM_1001_WRITEV_4:
		sm_write_regs(ssm, capsm_1001_writev_4, G_N_ELEMENTS(capsm_1001_writev_4));
		break;
	case CAPSM_1001_WRITEV_5:
		sm_write_regs(ssm, capsm_1001_writev_5, G_N_ELEMENTS(capsm_1001_writev_5));
		break;
	}
}

977
978
/***** DEINITIALIZATION *****/

979
980
981
enum deinitsm_2016_states {
	DEINITSM_2016_WRITEV,
	DEINITSM_2016_NUM_STATES,
982
983
};

984
985
986
987
988
enum deinitsm_1000_states {
	DEINITSM_1000_WRITEV,
	DEINITSM_1000_NUM_STATES,
};

989
990
991
992
993
enum deinitsm_1001_states {
	DEINITSM_1001_WRITEV,
	DEINITSM_1001_NUM_STATES,
};

994
static void deinitsm_2016_run_state(struct fpi_ssm *ssm)
995
{
996
	switch (fpi_ssm_get_cur_state(ssm)) {
997
998
	case DEINITSM_2016_WRITEV:
		sm_write_regs(ssm, deinitsm_2016_writev, G_N_ELEMENTS(deinitsm_2016_writev));
999
1000
1001
1002
		break;
	}
}

1003
1004
static void deinitsm_1000_run_state(struct fpi_ssm *ssm)
{
1005
	switch (fpi_ssm_get_cur_state(ssm)) {
1006
1007
1008
1009
1010
1011
	case DEINITSM_1000_WRITEV:
		sm_write_regs(ssm, deinitsm_1000_writev, G_N_ELEMENTS(deinitsm_1000_writev));
		break;
	}
}

1012
1013
static void deinitsm_1001_run_state(struct fpi_ssm *ssm)
{
1014
	switch (fpi_ssm_get_cur_state(ssm)) {
1015
1016
1017
1018
1019
1020
	case DEINITSM_1001_WRITEV:
		sm_write_regs(ssm, deinitsm_1001_writev, G_N_ELEMENTS(deinitsm_1001_writev));
		break;
	}
}

1021
1022
/***** INITIALIZATION *****/

1023
1024
1025
1026
1027
1028
1029
1030
1031
enum initsm_2016_states {
	INITSM_2016_WRITEV_1,
	INITSM_2016_READ_09,
	INITSM_2016_WRITE_09,
	INITSM_2016_READ_13,
	INITSM_2016_WRITE_13,
	INITSM_2016_WRITE_04,
	INITSM_2016_WRITE_05,
	INITSM_2016_NUM_STATES,
1032
1033
};

1034
1035
1036
1037
1038
enum initsm_1000_states {
	INITSM_1000_WRITEV_1,
	INITSM_1000_NUM_STATES,
};

1039
1040
1041
1042
1043
1044
1045
1046
1047
enum initsm_1001_states {
	INITSM_1001_WRITEV_1,
	INITSM_1001_WRITEV_2,
	INITSM_1001_WRITEV_3,
	INITSM_1001_WRITEV_4,
	INITSM_1001_WRITEV_5,
	INITSM_1001_NUM_STATES,
};

1048
static void initsm_2016_run_state(struct fpi_ssm *ssm)
1049
{
1050
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
1051
1052
	struct sonly_dev *sdev = dev->priv;

1053
	switch (fpi_ssm_get_cur_state(ssm)) {
1054
1055
	case INITSM_2016_WRITEV_1:
		sm_write_regs(ssm, initsm_2016_writev_1, G_N_ELEMENTS(initsm_2016_writev_1));
1056
		break;
1057
	case INITSM_2016_READ_09:
1058
1059
		sm_read_reg(ssm, 0x09);
		break;
1060
	case INITSM_2016_WRITE_09:
1061
1062
		sm_write_reg(ssm, 0x09, sdev->read_reg_result & ~0x08);
		break;
1063
	case INITSM_2016_READ_13:
1064
1065
		sm_read_reg(ssm, 0x13);
		break;
1066
	case INITSM_2016_WRITE_13:
1067
1068
		sm_write_reg(ssm, 0x13, sdev->read_reg_result & ~0x10);
		break;
1069
	case INITSM_2016_WRITE_04:
1070
1071
		sm_write_reg(ssm, 0x04, 0x00);
		break;
1072
	case INITSM_2016_WRITE_05:
1073
1074
1075
1076
1077
		sm_write_reg(ssm, 0x05, 0x00);
		break;
	}
}

1078
1079
static void initsm_1000_run_state(struct fpi_ssm *ssm)
{
1080
	switch (fpi_ssm_get_cur_state(ssm)) {
1081
1082
1083
1084
1085
1086
	case INITSM_1000_WRITEV_1:
		sm_write_regs(ssm, initsm_1000_writev_1, G_N_ELEMENTS(initsm_1000_writev_1));
		break;
	}
}

1087
1088
static void initsm_1001_run_state(struct fpi_ssm *ssm)
{
1089
	switch (fpi_ssm_get_cur_state(ssm)) {
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
	case INITSM_1001_WRITEV_1:
		sm_write_regs(ssm, initsm_1001_writev_1, G_N_ELEMENTS(initsm_1001_writev_1));
		break;
	case INITSM_1001_WRITEV_2:
		sm_write_regs(ssm, initsm_1001_writev_2, G_N_ELEMENTS(initsm_1001_writev_2));
		break;
	case INITSM_1001_WRITEV_3:
		sm_write_regs(ssm, initsm_1001_writev_3, G_N_ELEMENTS(initsm_1001_writev_3));
		break;
	case INITSM_1001_WRITEV_4:
		sm_write_regs(ssm, initsm_1001_writev_4, G_N_ELEMENTS(initsm_1001_writev_4));
		break;
	case INITSM_1001_WRITEV_5:
		sm_write_regs(ssm, initsm_1001_writev_5, G_N_ELEMENTS(initsm_1001_writev_5));
		break;
	}
}

1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
/***** CAPTURE LOOP *****/

enum loopsm_states {
	LOOPSM_RUN_AWFSM,
	LOOPSM_AWAIT_FINGER,
	LOOPSM_RUN_CAPSM,
	LOOPSM_CAPTURE,
	LOOPSM_RUN_DEINITSM,
	LOOPSM_FINAL,
	LOOPSM_NUM_STATES,
};

static void loopsm_run_state(struct fpi_ssm *ssm)
{
1122
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
1123
1124
	struct sonly_dev *sdev = dev->priv;

1125
	switch (fpi_ssm_get_cur_state(ssm)) {
1126
	case LOOPSM_RUN_AWFSM: ;
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
		switch (sdev->dev_model) {
		case UPEKSONLY_1001:
			if (sdev->deactivating) {
				fpi_ssm_mark_completed(ssm);
			} else {
				fpi_ssm_next_state(ssm);
			}
		break;
		default:
			if (sdev->deactivating) {
				fpi_ssm_mark_completed(ssm);
			} else {
				struct fpi_ssm *awfsm = NULL;
				switch (sdev->dev_model) {
				case UPEKSONLY_2016:
					awfsm = fpi_ssm_new(dev->dev, awfsm_2016_run_state,
						AWFSM_2016_NUM_STATES);
					break;
				case UPEKSONLY_1000:
					awfsm = fpi_ssm_new(dev->dev, awfsm_1000_run_state,
						AWFSM_1000_NUM_STATES);
					break;
				}
1150
				fpi_ssm_set_user_data(awfsm, dev);
1151
				fpi_ssm_start_subsm(ssm, awfsm);
1152
			}
1153
			break;
1154
1155
1156
		}
		break;
	case LOOPSM_AWAIT_FINGER:
1157
1158
1159
1160
1161
1162
1163
1164
		switch (sdev->dev_model) {
		case UPEKSONLY_1001:
			fpi_ssm_next_state(ssm);
			break;
		default:
			sm_await_intr(ssm);
			break;
		}
1165
1166
		break;
	case LOOPSM_RUN_CAPSM: ;
1167
1168
1169
		struct fpi_ssm *capsm = NULL;
		switch (sdev->dev_model) {
		case UPEKSONLY_2016:
1170
1171
			capsm = fpi_ssm_new(dev->dev, capsm_2016_run_state,
				CAPSM_2016_NUM_STATES);
1172
1173
1174
1175
1176
			break;
		case UPEKSONLY_1000:
			capsm = fpi_ssm_new(dev->dev, capsm_1000_run_state,
				CAPSM_1000_NUM_STATES);
			break;
1177
1178
1179
1180
		case UPEKSONLY_1001:
			capsm = fpi_ssm_new(dev->dev, capsm_1001_run_state,
				CAPSM_1001_NUM_STATES);
			break;
1181
		}
1182
		fpi_ssm_set_user_data(capsm, dev);
1183
1184
1185
1186
1187
		fpi_ssm_start_subsm(ssm, capsm);
		break;
	case LOOPSM_CAPTURE:
		break;
	case LOOPSM_RUN_DEINITSM: ;
1188
1189
1190
		struct fpi_ssm *deinitsm = NULL;
		switch (sdev->dev_model) {
		case UPEKSONLY_2016:
1191
1192
			deinitsm = fpi_ssm_new(dev->dev, deinitsm_2016_run_state,
				DEINITSM_2016_NUM_STATES);
1193
1194
1195
1196
1197
			break;
		case UPEKSONLY_1000:
			deinitsm = fpi_ssm_new(dev->dev, deinitsm_1000_run_state,
				DEINITSM_1000_NUM_STATES);
			break;
1198
1199
1200
1201
		case UPEKSONLY_1001:
			deinitsm = fpi_ssm_new(dev->dev, deinitsm_1001_run_state,
				DEINITSM_1001_NUM_STATES);
			break;
1202
		}
1203
		sdev->capturing = FALSE;
1204
		fpi_ssm_set_user_data(deinitsm, dev);
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
		fpi_ssm_start_subsm(ssm, deinitsm);
		break;
	case LOOPSM_FINAL:
		fpi_ssm_jump_to_state(ssm, LOOPSM_RUN_AWFSM);
		break;
	}
		
}

/***** DRIVER STUFF *****/

static void deactivate_done(struct fp_img_dev *dev)
{
	struct sonly_dev *sdev = dev->priv;

1220
	G_DEBUG_HERE();
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
	free_img_transfers(sdev);
	g_free(sdev->rowbuf);
	sdev->rowbuf = NULL;

	if (sdev->rows) {
		g_slist_foreach(sdev->rows, (GFunc) g_free, NULL);
		sdev->rows = NULL;
	}

	fpi_imgdev_deactivate_complete(dev);
}

static void dev_deactivate(struct fp_img_dev *dev)
{
	struct sonly_dev *sdev = dev->priv;

	if (!sdev->capturing) {
		deactivate_done(dev);
		return;
	}

	sdev->deactivating = TRUE;
	sdev->killing_transfers = ITERATE_SSM;
	sdev->kill_ssm = sdev->loopsm;
	cancel_img_transfers(dev);
}

static void loopsm_complete(struct fpi_ssm *ssm)
{
1250
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
1251
	struct sonly_dev *sdev = dev->priv;
1252
	int r = fpi_ssm_get_error(ssm);
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268

	fpi_ssm_free(ssm);

	if (sdev->deactivating) {
		deactivate_done(dev);
		return;
	}

	if (r) {
		fpi_imgdev_session_error(dev, r);
		return;
	}
}

static void initsm_complete(struct fpi_ssm *ssm)
{
1269
	struct fp_img_dev *dev = fpi_ssm_get_user_data(ssm);
1270
	struct sonly_dev *sdev = dev->priv;
1271
	int r = fpi_ssm_get_error(ssm);
1272
1273
1274
1275
1276
1277
1278

	fpi_ssm_free(ssm);
	fpi_imgdev_activate_complete(dev, r);
	if (r != 0)
		return;

	sdev->loopsm = fpi_ssm_new(dev->dev, loopsm_run_state, LOOPSM_NUM_STATES);
1279
	fpi_ssm_set_user_data(sdev->loopsm, dev);
1280
1281
1282
1283
1284
1285
	fpi_ssm_start(sdev->loopsm, loopsm_complete);
}

static int dev_activate(struct fp_img_dev *dev, enum fp_imgdev_state state)
{
	struct sonly_dev *sdev = dev->priv;
1286
	struct fpi_ssm *ssm = NULL;
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
	int i;

	sdev->deactivating = FALSE;
	sdev->capturing = FALSE;

	memset(sdev->img_transfer, 0,
		NUM_BULK_TRANSFERS * sizeof(struct libusb_transfer *));
	sdev->img_transfer_data =
		g_malloc0(sizeof(struct img_transfer_data) * NUM_BULK_TRANSFERS);
	sdev->num_flying = 0;
	for (i = 0; i < NUM_BULK_TRANSFERS; i++) {
		unsigned char *data;
		sdev->img_transfer[i] = libusb_alloc_transfer(0);
		if (!sdev->img_transfer[i]) {
			free_img_transfers(sdev);
			return -ENOMEM;
		}
		sdev->img_transfer_data[i].idx = i;
		sdev->img_transfer_data[i].dev = dev;
		data = g_malloc(4096);
		libusb_fill_bulk_transfer(sdev->img_transfer[i], dev->udev, 0x81, data,
			4096, img_data_cb, &sdev->img_transfer_data[i], 0);
	}

1311
1312
	switch (sdev->dev_model) {
	case UPEKSONLY_2016:
1313
		ssm = fpi_ssm_new(dev->dev, initsm_2016_run_state, INITSM_2016_NUM_STATES);
1314
1315
1316
1317
		break;
	case UPEKSONLY_1000:
		ssm = fpi_ssm_new(dev->dev, initsm_1000_run_state, INITSM_1000_NUM_STATES);
		break;
1318
1319
1320
	case UPEKSONLY_1001:
		ssm = fpi_ssm_new(dev->dev, initsm_1001_run_state, INITSM_1001_NUM_STATES);
		break;
1321
	}
1322
	fpi_ssm_set_user_data(ssm, dev);
1323
1324
1325
1326
1327
1328
1329
	fpi_ssm_start(ssm, initsm_complete);
	return 0;
}

static int dev_init(struct fp_img_dev *dev, unsigned long driver_data)
{
	int r;
1330
	struct sonly_dev *sdev;
1331
1332
1333
1334
1335
1336
1337
1338
1339

	r = libusb_set_configuration(dev->udev, 1);
	if (r < 0) {
		fp_err("could not set configuration 1");
		return r;
	}

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

1344
1345
1346
1347
1348
1349
	sdev = dev->priv = g_malloc0(sizeof(struct sonly_dev));
	sdev->dev_model = (int)driver_data;
	switch (driver_data) {
	case UPEKSONLY_1000:
		sdev->img_width = IMG_WIDTH_1000;
		upeksonly_driver.img_width = IMG_WIDTH_1000;
1350
		assembling_ctx.line_width = IMG_WIDTH_1000;
1351
1352
1353
1354
		break;
	case UPEKSONLY_1001:
		sdev->img_width = IMG_WIDTH_1001;
		upeksonly_driver.img_width = IMG_WIDTH_1001;
1355
		upeksonly_driver.bz3_threshold = 25;
1356
		assembling_ctx.line_width = IMG_WIDTH_1001;
1357
1358
1359
1360
		break;
	case UPEKSONLY_2016:
		sdev->img_width = IMG_WIDTH_2016;
		upeksonly_driver.img_width = IMG_WIDTH_2016;
1361
		assembling_ctx.line_width = IMG_WIDTH_2016;
1362
1363
		break;
	}
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
	fpi_imgdev_open_complete(dev, 0);
	return 0;
}

static void dev_deinit(struct fp_img_dev *dev)
{
	g_free(dev->priv);
	libusb_release_interface(dev->udev, 0);
	fpi_imgdev_close_complete(dev);
}

1375
1376
static int dev_discover(struct libusb_device_descriptor *dsc, uint32_t *devtype)
{
1377
1378
1379
1380
1381
1382
1383
1384
	if (dsc->idProduct == 0x2016) {
		if (dsc->bcdDevice == 1) /* Revision 1 is what we're interested in */
			return 1;
	}
	if (dsc->idProduct == 0x1000) {
		if (dsc->bcdDevice == 0x0033) /* Looking for revision 0.33 */
			return 1;
	}
1385

1386
1387
1388
	if (dsc->idProduct == 0x1001)
		return 1;

1389
1390
1391
	return 0;
}

1392
static const struct usb_id id_table[] = {
1393
1394
	{ .vendor = 0x147e, .product = 0x2016, .driver_data = UPEKSONLY_2016 },
	{ .vendor = 0x147e, .product = 0x1000, .driver_data = UPEKSONLY_1000 },
1395
	{ .vendor = 0x147e, .product = 0x1001, .driver_data = UPEKSONLY_1001 },
1396
1397
1398
1399
1400
	{ 0, 0, 0, },
};

struct fp_img_driver upeksonly_driver = {
	.driver = {
1401
		.id = UPEKSONLY_ID,
1402
1403
1404
		.name = FP_COMPONENT,
		.full_name = "UPEK TouchStrip Sensor-Only",
		.id_table = id_table,
1405
		.scan_type = FP_SCAN_TYPE_SWIPE,
1406
		.discover = dev_discover,
1407
1408
	},
	.flags = 0,
1409
	.img_width = -1,
1410
1411
1412
1413
1414
1415
1416
1417
	.img_height = -1,

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