-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft9201.c
585 lines (466 loc) · 13.6 KB
/
ft9201.c
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
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
582
583
584
585
#include <linux/module.h>
#include <linux/printk.h>
#include <linux/usb.h>
#include "ft9201.h"
MODULE_AUTHOR("Ben Maddocks <[email protected]>");
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("FT9201 Fingeprint reader driver");
#define VENDOR_ID 0x2808
#define PRODUCT_ID 0x9338
static struct usb_device_id ft9201_table[] = {
{USB_DEVICE(VENDOR_ID, PRODUCT_ID)},
{}
};
MODULE_DEVICE_TABLE(usb, ft9201_table);
#define FT9201_MINOR_BASE 192
#define WRITES_IN_FLIGHT 8
#define USB_CONTROL_OP_TIMEOUT 1000
#define USB_READ_OP_TIMEOUT 1000
struct ft9201_device {
struct usb_device *udev;
struct usb_interface *interface;
struct semaphore limit_sem; /* limiting the number of writes in progress */
__u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */
int errors; /* the last request tanked */
spinlock_t err_lock; /* lock for errors */
struct kref kref;
struct mutex io_mutex; /* synchronize I/O with disconnect */
unsigned long disconnected:1;
wait_queue_head_t bulk_in_wait; /* to wait for an ongoing read */
struct ft9201_status device_status;
bool ongoing_read; /* a read is going on */
unsigned char *read_img_data;
size_t img_in_size; /* the size of the receive buffer */
size_t img_in_filled; /* number of bytes in the buffer */
size_t img_in_copied; /* already copied to user space */
bool timetoexit;
};
#define to_ft9201_dev(d) container_of(d, struct ft9201_device, kref)
static int ft9201_open(struct inode *inode, struct file *file);
static int ft9201_release(struct inode *inode, struct file *file);
static long ft9201_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
static ssize_t ft9201_read(struct file *fp, char __user *buf, size_t count, loff_t *f_pos);
static int ft9201_initialize(struct ft9201_device *dev);
static int ft9201_ic_sensor_mode_exit(struct ft9201_device *dev);
static int ft9201_read_image(struct ft9201_device *dev);
static void ft9201_delete(struct kref *kref);
static struct usb_driver ft9201_driver;
static const struct file_operations ft9201_fops = {
.owner = THIS_MODULE,
.llseek = noop_llseek,
.open = ft9201_open,
.release = ft9201_release,
.unlocked_ioctl = ft9201_ioctl,
.read = ft9201_read,
};
/*
* usb class driver info in order to get a minor number from the usb core,
* and to have the device registered with the driver core
*/
static struct usb_class_driver ft9201_class = {
.name = "fpreader%d",
.fops = &ft9201_fops,
.minor_base = FT9201_MINOR_BASE,
};
// IN Requests
#define FT9201_REQ_READ_REGISTERS 0x43
#define FT9201_REQ_GET_SUI_VERSION 0x1a
// OUT Requests
#define FT9201_REQ_START_CAPTURE_PROBABLY 0x34
#define FT9201_REQ_CONFIGURE_BULK_TRANSFER_SIZE_PROBABLY 0x35
#define FT9201_REQ_WRITE_REGISTER 0x3b
#define FT9201_REG_MCU_SENSOR_STATUS_INDEX 0x20
#define FT9201_AFE_0X30_SUCCESSFUL_RESPONSE 0xbb
static long ft9201_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
long errCode;
struct ft9201_device *dev = file->private_data;
pr_info("ft9201 ioctl, cmd: %u\n", cmd);
errCode = 0;
switch (cmd) {
case FT9201_IOCTL_REQ_INITIALIZE:
errCode = ft9201_initialize(dev);
if (errCode < 0) {
dev_err(&dev->interface->dev, "Error initializing device: %ld", errCode);
return errCode;
}
break;
default:
return -EINVAL;
}
return 0;
}
static int ft9201_open(struct inode *inode, struct file *file)
{
struct usb_interface *intf;
struct ft9201_device *dev;
pr_info("ft9201 open\n");
intf = usb_find_interface(&ft9201_driver, iminor(inode));
if (!intf) {
pr_err("Can't find device for minor %d\n", iminor(inode));
return -ENODEV;
}
dev = usb_get_intfdata(intf);
if (!dev) {
return -ENODEV;
}
dev->timetoexit = false;
dev->img_in_copied = 0;
dev->img_in_filled = 0;
kref_get(&dev->kref);
file->private_data = dev;
return 0;
}
static int ft9201_release(struct inode *inode, struct file *file)
{
struct ft9201_device *dev = file->private_data;
pr_info("ft9201 release\n");
if (dev == NULL) {
return -ENODEV;
}
kref_put(&dev->kref, ft9201_delete);
return 0;
}
static int ft9201_initialize(struct ft9201_device *dev)
{
int errCode = 0;
unsigned char sensor_status = 1;
dev_info(&dev->interface->dev, "ioctl initialize");
if (sensor_status != 1) {
ft9201_ic_sensor_mode_exit(dev);
}
dev->device_status.sensor_height = 0x40;
dev->device_status.sensor_width = 0x50;
dev_info(&dev->interface->dev, "Image dimensions: %d x %d", dev->device_status.sensor_width, dev->device_status.sensor_height);
if (errCode == 0) {
dev->device_status.initialized = 1;
dev_info(&dev->interface->dev, "Device initialization successful");
}
return errCode;
}
static int ft9201_read_image(struct ft9201_device *dev)
{
int retVal;
int img_size = 64 * 80;
int transfer_size = img_size;
int read_length;
unsigned char *img_with_header;
dev->device_status.sensor_width = 0x50;
dev->device_status.sensor_height = 0x40;
dev_info(&dev->interface->dev, "Reading image from scanner; dimensions: %dx%d", dev->device_status.sensor_width, dev->device_status.sensor_height);
dev->img_in_copied = 0;
dev->img_in_filled = 0;
if (dev->read_img_data != NULL) {
kfree(dev->read_img_data);
dev->img_in_size = 0;
}
dev->read_img_data = kzalloc(img_size, GFP_KERNEL);
if (dev->read_img_data == NULL) {
return -ENOMEM;
}
dev->img_in_size = img_size;
img_with_header = kzalloc(transfer_size, GFP_KERNEL);
if (img_with_header == NULL) {
retVal = -ENOMEM;
goto out;
}
retVal = usb_bulk_msg(
dev->udev,
usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
img_with_header,
transfer_size,
&read_length,
USB_READ_OP_TIMEOUT
);
if (retVal < 0) {
dev_err(&dev->interface->dev, "Error reading data from device: Error %d", retVal);
goto out;
}
dev_info(&dev->interface->dev, "Received %d bytes from device", read_length);
if (read_length != transfer_size) {
dev_err(&dev->interface->dev, "Read less than image size");
retVal = -EINVAL;
goto out;
}
// Ignore added 2 bytes at the start of data
memcpy(dev->read_img_data, img_with_header, img_size);
dev->img_in_filled = img_size;
out:
if (img_with_header != NULL) {
kfree(img_with_header);
}
return retVal;
}
static int ft9201_ic_sensor_mode_exit(struct ft9201_device *dev)
{
int errCode = 0;
return errCode;
}
static DECLARE_WAIT_QUEUE_HEAD(ft9201_wq);
static int has_data_remaining(struct ft9201_device *dev)
{
dev_info(&dev->interface->dev, "Copied: %lu, Filled: %lu", dev->img_in_copied, dev->img_in_filled);
if ((dev->img_in_copied == 5120) && (dev->img_in_filled == 5120)) {
dev->timetoexit = true;
}
return dev->img_in_copied < dev->img_in_filled;
}
static ssize_t send_read_data(struct ft9201_device *dev, char __user *buf, size_t count)
{
size_t remaining = dev->img_in_filled - dev->img_in_copied;
size_t to_copy = remaining;
if (to_copy > count) {
to_copy = count;
}
dev_info(&dev->interface->dev, "Copied: %lu, to_copy: %lu, full_size: %lu", dev->img_in_copied, to_copy, dev->img_in_size);
if (dev->img_in_copied + to_copy > dev->img_in_size) {
dev_info(&dev->interface->dev, "error img_in_copied + to_copy > dev->img_in_size");
return -EINVAL;
}
if (copy_to_user(buf, dev->read_img_data + dev->img_in_copied, to_copy)) {
dev_info(&dev->interface->dev, "error copy_to_user");
return -EFAULT;
}
dev->img_in_copied += to_copy;
dev_info(&dev->interface->dev, "Copied total: %lu", dev->img_in_copied);
return (ssize_t)to_copy;
}
static ssize_t ft9201_read(struct file *fp, char __user *buf, size_t count, loff_t *f_pos)
{
struct ft9201_device *dev = fp->private_data;
// struct inode *inode = fp->f_path.dentry->d_inode;
unsigned long loop_timeout = msecs_to_jiffies(1000);
int ret;
if (dev == NULL) {
pr_err("device is null\n");
return 0;
}
ret = mutex_lock_interruptible(&dev->io_mutex);
if (ret < 0) {
pr_info("Interrupted while waiting on IO mutex");
return ret;
}
if (dev->disconnected) {
ret = -ENODEV;
goto exit;
}
int ret2;
while (true) {
if (has_data_remaining(dev)) {
ret = send_read_data(dev, buf, count);
ret2 = ret;
break;
}
/*
if (ret2 == 5120) {
mutex_unlock(&dev->io_mutex);
kfree(dev->read_img_data);
dev->read_img_data = NULL;
ft9201_release(inode, fp);
break;
}
*/
// pr_info("ft9201 reading %d\n", i);
ret = wait_event_interruptible_timeout(ft9201_wq, 0, loop_timeout);
if ((ret == -ERESTARTSYS) || (dev->timetoexit == true)){
// We were interrupted by a signal
dev->timetoexit = false;
mutex_unlock(&dev->io_mutex);
pr_info("Done reading fingerprint closing");
ret = 0;
break;
}
int retval;
int poo = 0;
unsigned char local_value[4];
retval = usb_control_msg_send(
dev->udev,
0,
52,
0x40,
0x0003,
0,
NULL,
0,
5000,
GFP_KERNEL);
if (retval) {
dev_info(&dev->interface->dev, "Error sending control data 1: %d\n", retval);
return retval;
}
retval = usb_control_msg_send(
dev->udev,
0,
111,
0x40,
0x0020,
37248,
NULL,
0,
5000,
GFP_KERNEL);
if (retval) {
dev_info(&dev->interface->dev, "Error sending control data 2: %d\n", retval);
return retval;
}
retval = usb_control_msg_send(
dev->udev,
0,
111,
0x40,
0x1400,
36992,
NULL,
0,
5000,
GFP_KERNEL);
if (retval) {
dev_info(&dev->interface->dev, "Error sending control data 3: %d\n", retval);
return retval;
}
while(poo == 0) {
retval = usb_control_msg_recv(
dev->udev,
0,
FT9201_REQ_READ_REGISTERS,
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0,
0,
&local_value,
sizeof(local_value),
5000,
GFP_KERNEL);
if (retval) {
dev_info(&dev->interface->dev, "Error sending data: %d\n", retval);
return retval;
}
// dev_info(&dev->interface->dev, "read registers returned: %d %d %d %d\n", local_value[0], local_value[1], local_value[2], local_value[3]);
poo = local_value[0];
}
ret = ft9201_read_image(dev);
if (ret < 0) {
break;
}
dev_info(&dev->interface->dev, "ft9201_read copied : %d\n", ret);
/*
if (has_data_remaining(dev)) {
retval = send_read_data(dev, buf, count);
// break;
}
*/
// if (dev->timetoexit == true) {
// mutex_unlock(&dev->io_mutex);
// kfree(dev->read_img_data);
// dev->read_img_data = NULL;
// ft9201_release(inode, fp);
// return 0;
// }
}
exit:
mutex_unlock(&dev->io_mutex);
return ret;
}
static void ft9201_delete(struct kref *kref)
{
struct ft9201_device *dev = to_ft9201_dev(kref);
usb_put_intf(dev->interface);
usb_put_dev(dev->udev);
if (dev->read_img_data != NULL) {
kfree(dev->read_img_data);
dev->read_img_data = NULL;
}
kfree(dev);
}
static int ft9201_probe(struct usb_interface *intf, const struct usb_device_id *id) {
struct usb_device *udev = interface_to_usbdev(intf);
struct ft9201_device *dev;
struct usb_endpoint_descriptor *bulk_in, *bulk_out;
int retval;
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (!dev) {
return -ENOMEM;
}
kref_init(&dev->kref);
sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
spin_lock_init(&dev->err_lock);
init_waitqueue_head(&dev->bulk_in_wait);
dev->udev = usb_get_dev(udev);
dev->interface = usb_get_intf(intf);
// bulk_out endpoint is not yet used. It might be used only for upgrading firmware if it's ever implemented
/* use only the first bulk-in and bulk-out endpoints */
retval = usb_find_common_endpoints(intf->cur_altsetting, &bulk_in, &bulk_out, NULL, NULL);
if (retval) {
dev_err(&intf->dev, "Could not find both bulk-in and bulk-out endpoints\n");
goto error;
}
dev->bulk_in_endpointAddr = bulk_in->bEndpointAddress;
/* save our data pointer in this interface device */
usb_set_intfdata(intf, dev);
/* we can register the device now, as it is ready */
retval = usb_register_dev(intf, &ft9201_class);
if (retval) {
/* something prevented us from registering this driver */
dev_err(&intf->dev,
"Not able to get a minor for this device.\n");
usb_set_intfdata(intf, NULL);
goto error;
}
/* let the user know what node this device is now attached to */
dev_info(&intf->dev, "USB fpreader device now attached to fpreader%d", intf->minor);
retval = ft9201_initialize(dev);
if (retval < 0) {
dev_err(&dev->interface->dev, "Error initializing device: %d", retval);
}
return 0;
error:
/* this frees allocated memory */
kref_put(&dev->kref, ft9201_delete);
return retval;
}
static int ft9201_pre_reset(struct usb_interface *interface) {
pr_info("Pre reset\n");
return 0;
}
static int ft9201_post_reset(struct usb_interface *interface) {
pr_info("Post reset\n");
return 0;
}
static void ft9201_disconnect(struct usb_interface *interface) {
struct ft9201_device *dev;
int minor = interface->minor;
pr_info("Disconnect");
dev = usb_get_intfdata(interface);
if (mutex_is_locked(&dev->io_mutex) == 1) {
mutex_unlock(&dev->io_mutex);
}
pr_info("Disconnect");
usb_set_intfdata(interface, NULL);
/* give back our minor */
usb_deregister_dev(interface, &ft9201_class);
/* prevent more I/O from starting */
mutex_lock(&dev->io_mutex);
dev->disconnected = 1;
mutex_unlock(&dev->io_mutex);
/* decrement our usage count */
kref_put(&dev->kref, ft9201_delete);
dev_info(&interface->dev, "USB device fpreader%d now disconnected", minor);
}
static int ft9201_suspend(struct usb_interface *intf, pm_message_t message) {
pr_info("Suspend");
return 0;
}
static int ft9201_resume(struct usb_interface *intf) {
pr_info("Resume");
return 0;
}
static struct usb_driver ft9201_driver = {
.name = "ft9201",
.probe = ft9201_probe,
.disconnect = ft9201_disconnect,
.suspend = ft9201_suspend,
.resume = ft9201_resume,
.pre_reset = ft9201_pre_reset,
.post_reset = ft9201_post_reset,
.id_table = ft9201_table,
};
module_usb_driver(ft9201_driver);