-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathfuri_hal_usb_ccid.c
More file actions
710 lines (585 loc) · 26.1 KB
/
Copy pathfuri_hal_usb_ccid.c
File metadata and controls
710 lines (585 loc) · 26.1 KB
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
586
587
588
589
590
591
592
593
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
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
#include <furi_hal_version.h>
#include <furi_hal_usb_i.h>
#include <furi_hal_usb.h>
#include <furi_hal_usb_ccid.h>
#include <furi.h>
#include <usb_ccid.h>
#define USB_DEVICE_NO_CLASS (0x0)
#define USB_DEVICE_NO_SUBCLASS (0x0)
#define USB_DEVICE_NO_PROTOCOL (0x0)
#define FIXED_CONTROL_ENDPOINT_SIZE (8)
#define IF_NUM_MAX (1)
#define CCID_VID_DEFAULT (0x1234)
#define CCID_PID_DEFAULT (0xABCD)
#define CCID_TOTAL_SLOTS (1)
#define CCID_SLOT_INDEX (0)
#define CCID_DATABLOCK_SIZE \
(4 + 1 + CCID_SHORT_APDU_SIZE + 1) //APDU Header + Lc + Short APDU size + Le
#define ENDPOINT_DIR_IN (0x80)
#define ENDPOINT_DIR_OUT (0x00)
#define ENDPOINT_DIR_INTERRUPT (0x40)
#define INTERFACE_ID_CCID (0)
#define CCID_IN_EPADDR (ENDPOINT_DIR_IN | 2)
/** Endpoint address of the CCID data OUT endpoint, for host-to-device data transfers. */
#define CCID_OUT_EPADDR (ENDPOINT_DIR_OUT | 1)
#define CCID_INTERRUPT_EPADDR (ENDPOINT_DIR_INTERRUPT | 2)
/** Endpoint size in bytes of the CCID data being sent between IN and OUT endpoints. */
#define CCID_EPSIZE 64
struct CcidIntfDescriptor {
struct usb_interface_descriptor ccid;
struct usb_ccid_descriptor ccid_desc;
struct usb_endpoint_descriptor ccid_bulk_in;
struct usb_endpoint_descriptor ccid_bulk_out;
struct usb_endpoint_descriptor ccid_interrupt;
} FURI_PACKED;
struct CcidConfigDescriptor {
struct usb_config_descriptor config;
struct CcidIntfDescriptor intf_0;
} FURI_PACKED;
enum CCID_Features_Auto_t {
CCID_Features_Auto_None = 0x0,
CCID_Features_Auto_ParameterConfiguration = 0x2,
CCID_Features_Auto_ICCActivation = 0x4,
CCID_Features_Auto_VoltageSelection = 0x8,
CCID_Features_Auto_ICCClockFrequencyChange = 0x10,
CCID_Features_Auto_ICCBaudRateChange = 0x20,
CCID_Features_Auto_ParameterNegotiation = 0x40,
CCID_Features_Auto_PPS = 0x80,
};
enum CCID_Features_ExchangeLevel_t {
CCID_Features_ExchangeLevel_TPDU = 0x00010000,
CCID_Features_ExchangeLevel_ShortAPDU = 0x00020000,
CCID_Features_ExchangeLevel_ShortExtendedAPDU = 0x00040000
};
typedef enum {
WorkerEvtStop = (1 << 0),
WorkerEvtRequest = (1 << 1),
WorkerEvtInsertSmartcard = (1 << 2),
WorkerEvtRemoveSmartcard = (1 << 3),
} WorkerEvtFlags;
typedef struct ccid_bulk_message_header {
uint8_t bMessageType;
uint32_t dwLength;
uint8_t bSlot;
uint8_t bSeq;
} FURI_PACKED ccid_bulk_message_header_t;
/* Device descriptor */
static struct usb_device_descriptor ccid_device_desc = {
.bLength = sizeof(struct usb_device_descriptor),
.bDescriptorType = USB_DTYPE_DEVICE,
.bcdUSB = VERSION_BCD(2, 0, 0),
.bDeviceClass = USB_DEVICE_NO_CLASS,
.bDeviceSubClass = USB_DEVICE_NO_SUBCLASS,
.bDeviceProtocol = USB_DEVICE_NO_PROTOCOL,
.bMaxPacketSize0 = FIXED_CONTROL_ENDPOINT_SIZE,
.idVendor = CCID_VID_DEFAULT,
.idProduct = CCID_PID_DEFAULT,
.bcdDevice = VERSION_BCD(1, 0, 0),
.iManufacturer = UsbDevManuf,
.iProduct = UsbDevProduct,
.iSerialNumber = UsbDevSerial,
.bNumConfigurations = 1,
};
/* Device configuration descriptor*/
static const struct CcidConfigDescriptor ccid_cfg_desc = {
.config =
{
.bLength = sizeof(struct usb_config_descriptor),
.bDescriptorType = USB_DTYPE_CONFIGURATION,
.wTotalLength = sizeof(struct CcidConfigDescriptor),
.bNumInterfaces = 1,
.bConfigurationValue = 1,
.iConfiguration = NO_DESCRIPTOR,
.bmAttributes = USB_CFG_ATTR_RESERVED | USB_CFG_ATTR_SELFPOWERED,
.bMaxPower = USB_CFG_POWER_MA(500),
},
.intf_0 =
{
.ccid =
{.bLength = sizeof(struct usb_interface_descriptor),
.bDescriptorType = USB_DTYPE_INTERFACE,
.bInterfaceNumber = INTERFACE_ID_CCID,
.bAlternateSetting = 0x00,
.bNumEndpoints = 2,
.bInterfaceClass = USB_CLASS_CCID,
.bInterfaceSubClass = 0,
.bInterfaceProtocol = 0,
.iInterface = NO_DESCRIPTOR
},
.ccid_desc =
{.bLength = sizeof(struct usb_ccid_descriptor),
.bDescriptorType = USB_DTYPE_CCID_FUNCTIONAL,
.bcdCCID = CCID_CURRENT_SPEC_RELEASE_NUMBER,
.bMaxSlotIndex = 0x00,
.bVoltageSupport = CCID_VOLTAGESUPPORT_5V,
.dwProtocols = 0x01, //T0
.dwDefaultClock = 16000, //16MHz
.dwMaximumClock = 16000, //16MHz
.bNumClockSupported = 0,
.dwDataRate = 307200,
.dwMaxDataRate = 307200,
.bNumDataRatesSupported = 0,
.dwMaxIFSD = 2038,
.dwSynchProtocols = 0,
.dwMechanical = 0,
.dwFeatures = CCID_Features_ExchangeLevel_ShortAPDU |
CCID_Features_Auto_ParameterConfiguration |
CCID_Features_Auto_ICCActivation |
CCID_Features_Auto_VoltageSelection,
.dwMaxCCIDMessageLength = sizeof(ccid_bulk_message_header_t) + CCID_DATABLOCK_SIZE,
.bClassGetResponse = 0xff,
.bClassEnvelope = 0xff,
.wLcdLayout = 0,
.bPINSupport = 0,
.bMaxCCIDBusySlots = 1},
.ccid_bulk_in =
{.bLength = sizeof(struct usb_endpoint_descriptor),
.bDescriptorType = USB_DTYPE_ENDPOINT,
.bEndpointAddress = CCID_IN_EPADDR,
.bmAttributes = USB_EPTYPE_BULK,
.wMaxPacketSize = CCID_EPSIZE,
.bInterval = 0x05
},
.ccid_bulk_out =
{.bLength = sizeof(struct usb_endpoint_descriptor),
.bDescriptorType = USB_DTYPE_ENDPOINT,
.bEndpointAddress = CCID_OUT_EPADDR,
.bmAttributes = USB_EPTYPE_BULK,
.wMaxPacketSize = CCID_EPSIZE,
.bInterval = 0x05},
.ccid_interrupt =
{.bLength = sizeof(struct usb_endpoint_descriptor),
.bDescriptorType = USB_DTYPE_ENDPOINT,
.bEndpointAddress = CCID_INTERRUPT_EPADDR,
.bmAttributes = USB_EPTYPE_INTERRUPT,
.wMaxPacketSize = CCID_EPSIZE,
.bInterval = 0x05},
},
};
static void ccid_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx);
static void ccid_deinit(usbd_device* dev);
static void ccid_on_wakeup(usbd_device* dev);
static void ccid_on_suspend(usbd_device* dev);
static int32_t ccid_worker(void* context);
FuriHalUsbInterface usb_ccid = {
.init = ccid_init,
.deinit = ccid_deinit,
.wakeup = ccid_on_wakeup,
.suspend = ccid_on_suspend,
.dev_descr = (struct usb_device_descriptor*)&ccid_device_desc,
.str_manuf_descr = NULL,
.str_prod_descr = NULL,
.str_serial_descr = NULL,
.cfg_descr = (void*)&ccid_cfg_desc,
};
static usbd_respond ccid_ep_config(usbd_device* dev, uint8_t cfg);
static usbd_respond ccid_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback);
typedef struct {
bool connected;
bool smartcard_inserted;
CcidCallbacks* callbacks[CCID_TOTAL_SLOTS];
void* cb_ctx[CCID_TOTAL_SLOTS];
FuriThread* ccid_thread;
FuriSemaphore* ccid_semaphore;
usbd_device* usb_dev;
uint16_t receive_buffer_data_index;
uint8_t send_buffer[sizeof(ccid_bulk_message_header_t) + CCID_DATABLOCK_SIZE];
uint8_t receive_buffer[sizeof(ccid_bulk_message_header_t) + CCID_DATABLOCK_SIZE];
} FuriHalUsbCcid;
static FuriHalUsbCcid* furi_hal_usb_ccid = NULL;
static void* ccid_set_string_descr(char* str) {
furi_check(str);
size_t len = strlen(str);
struct usb_string_descriptor* dev_str_desc = malloc(len * 2 + 2);
dev_str_desc->bLength = len * 2 + 2;
dev_str_desc->bDescriptorType = USB_DTYPE_STRING;
for(size_t i = 0; i < len; i++)
dev_str_desc->wString[i] = str[i];
return dev_str_desc;
}
static void ccid_init(usbd_device* dev, FuriHalUsbInterface* intf, void* ctx) {
UNUSED(intf);
FuriHalUsbCcidConfig* cfg = (FuriHalUsbCcidConfig*)ctx;
furi_check(furi_hal_usb_ccid == NULL);
furi_hal_usb_ccid = malloc(sizeof(FuriHalUsbCcid));
furi_hal_usb_ccid->usb_dev = dev;
furi_hal_usb_ccid->ccid_semaphore = furi_semaphore_alloc(1, 1);
usb_ccid.dev_descr->iManufacturer = 0;
usb_ccid.dev_descr->iProduct = 0;
usb_ccid.str_manuf_descr = NULL;
usb_ccid.str_prod_descr = NULL;
usb_ccid.dev_descr->idVendor = CCID_VID_DEFAULT;
usb_ccid.dev_descr->idProduct = CCID_PID_DEFAULT;
if(cfg != NULL) {
usb_ccid.dev_descr->idVendor = cfg->vid;
usb_ccid.dev_descr->idProduct = cfg->pid;
if(cfg->manuf[0] != '\0') {
usb_ccid.str_manuf_descr = ccid_set_string_descr(cfg->manuf);
usb_ccid.dev_descr->iManufacturer = UsbDevManuf;
}
if(cfg->product[0] != '\0') {
usb_ccid.str_prod_descr = ccid_set_string_descr(cfg->product);
usb_ccid.dev_descr->iProduct = UsbDevProduct;
}
}
usbd_reg_config(dev, ccid_ep_config);
usbd_reg_control(dev, ccid_control);
usbd_connect(dev, true);
furi_hal_usb_ccid->receive_buffer_data_index = 0;
furi_hal_usb_ccid->ccid_thread = furi_thread_alloc_ex("CcidWorker", 2048, ccid_worker, ctx);
furi_thread_start(furi_hal_usb_ccid->ccid_thread);
}
static void ccid_deinit(usbd_device* dev) {
furi_thread_flags_set(furi_thread_get_id(furi_hal_usb_ccid->ccid_thread), WorkerEvtStop);
furi_thread_join(furi_hal_usb_ccid->ccid_thread);
furi_thread_free(furi_hal_usb_ccid->ccid_thread);
usbd_reg_config(dev, NULL);
usbd_reg_control(dev, NULL);
free(usb_ccid.str_prod_descr);
free(usb_ccid.str_serial_descr);
free(furi_hal_usb_ccid);
furi_hal_usb_ccid = NULL;
}
static void ccid_on_wakeup(usbd_device* dev) {
UNUSED(dev);
furi_check(furi_hal_usb_ccid);
furi_hal_usb_ccid->connected = true;
}
static void ccid_on_suspend(usbd_device* dev) {
UNUSED(dev);
furi_check(furi_hal_usb_ccid);
furi_hal_usb_ccid->connected = false;
}
void CALLBACK_CCID_GetSlotStatus(
uint8_t slot,
uint8_t seq,
struct rdr_to_pc_slot_status* responseSlotStatus) {
furi_check(responseSlotStatus);
furi_check(furi_hal_usb_ccid);
responseSlotStatus->bMessageType = RDR_TO_PC_SLOTSTATUS;
responseSlotStatus->bSlot = slot;
responseSlotStatus->bSeq = seq;
responseSlotStatus->bClockStatus = 0;
responseSlotStatus->dwLength = 0;
if(responseSlotStatus->bSlot == CCID_SLOT_INDEX) {
responseSlotStatus->bError = CCID_ERROR_NOERROR;
if(furi_hal_usb_ccid->smartcard_inserted) {
responseSlotStatus->bStatus = CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR |
CCID_ICCSTATUS_PRESENTANDACTIVE;
} else {
responseSlotStatus->bStatus = CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR |
CCID_ICCSTATUS_NOICCPRESENT;
}
} else {
responseSlotStatus->bError = CCID_ERROR_SLOTNOTFOUND;
responseSlotStatus->bStatus = CCID_COMMANDSTATUS_FAILED | CCID_ICCSTATUS_NOICCPRESENT;
}
}
void CALLBACK_CCID_SetParametersT0(
struct pc_to_rdr_set_parameters_t0* requestSetParametersT0,
struct rdr_to_pc_parameters_t0* responseSetParametersT0) {
furi_check(requestSetParametersT0);
furi_check(responseSetParametersT0);
furi_check(furi_hal_usb_ccid);
furi_check(requestSetParametersT0->bProtocolNum == 0x00); //T0
responseSetParametersT0->bMessageType = RDR_TO_PC_PARAMETERS;
responseSetParametersT0->bSlot = requestSetParametersT0->bSlot;
responseSetParametersT0->bSeq = requestSetParametersT0->bSeq;
responseSetParametersT0->dwLength =
sizeof(struct pc_to_rdr_set_parameters_t0) - sizeof(ccid_bulk_message_header_t);
if(responseSetParametersT0->bSlot == CCID_SLOT_INDEX) {
responseSetParametersT0->bError = CCID_ERROR_NOERROR;
if(furi_hal_usb_ccid->smartcard_inserted) {
responseSetParametersT0->bProtocolNum = requestSetParametersT0->bProtocolNum;
responseSetParametersT0->bStatus = CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR |
CCID_ICCSTATUS_PRESENTANDACTIVE;
} else {
responseSetParametersT0->bStatus = CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR |
CCID_ICCSTATUS_NOICCPRESENT;
}
} else {
responseSetParametersT0->bError = CCID_ERROR_SLOTNOTFOUND;
responseSetParametersT0->bStatus = CCID_COMMANDSTATUS_FAILED | CCID_ICCSTATUS_NOICCPRESENT;
}
}
void CALLBACK_CCID_IccPowerOn(
uint8_t slot,
uint8_t seq,
struct rdr_to_pc_data_block* responseDataBlock) {
furi_check(responseDataBlock);
furi_check(furi_hal_usb_ccid);
responseDataBlock->bMessageType = RDR_TO_PC_DATABLOCK;
responseDataBlock->dwLength = 0;
responseDataBlock->bSlot = slot;
responseDataBlock->bSeq = seq;
if(responseDataBlock->bSlot == CCID_SLOT_INDEX) {
responseDataBlock->bError = CCID_ERROR_NOERROR;
if(furi_hal_usb_ccid->smartcard_inserted) {
if(furi_hal_usb_ccid->callbacks[CCID_SLOT_INDEX] != NULL) {
furi_hal_usb_ccid->callbacks[CCID_SLOT_INDEX]->icc_power_on_callback(
responseDataBlock->abData,
&responseDataBlock->dwLength,
furi_hal_usb_ccid->cb_ctx[CCID_SLOT_INDEX]);
responseDataBlock->bStatus = CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR |
CCID_ICCSTATUS_PRESENTANDACTIVE;
} else {
responseDataBlock->bStatus = CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR |
CCID_ICCSTATUS_PRESENTANDINACTIVE;
}
} else {
responseDataBlock->bStatus = CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR |
CCID_ICCSTATUS_NOICCPRESENT;
}
} else {
responseDataBlock->bError = CCID_ERROR_SLOTNOTFOUND;
responseDataBlock->bStatus = CCID_COMMANDSTATUS_FAILED | CCID_ICCSTATUS_NOICCPRESENT;
}
}
void CALLBACK_CCID_XfrBlock(
struct pc_to_rdr_xfr_block* receivedXfrBlock,
struct rdr_to_pc_data_block* responseDataBlock) {
furi_check(receivedXfrBlock);
furi_check(responseDataBlock);
furi_check(furi_hal_usb_ccid);
responseDataBlock->bMessageType = RDR_TO_PC_DATABLOCK;
responseDataBlock->bSlot = receivedXfrBlock->bSlot;
responseDataBlock->bSeq = receivedXfrBlock->bSeq;
responseDataBlock->bChainParameter = 0;
if(responseDataBlock->bSlot == CCID_SLOT_INDEX) {
responseDataBlock->bError = CCID_ERROR_NOERROR;
if(furi_hal_usb_ccid->smartcard_inserted) {
if(furi_hal_usb_ccid->callbacks[CCID_SLOT_INDEX] != NULL) {
furi_hal_usb_ccid->callbacks[CCID_SLOT_INDEX]->xfr_datablock_callback(
(const uint8_t*)receivedXfrBlock->abData,
receivedXfrBlock->dwLength,
responseDataBlock->abData,
&responseDataBlock->dwLength,
furi_hal_usb_ccid->cb_ctx[CCID_SLOT_INDEX]);
responseDataBlock->bStatus = CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR |
CCID_ICCSTATUS_PRESENTANDACTIVE;
} else {
responseDataBlock->bStatus = CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR |
CCID_ICCSTATUS_PRESENTANDINACTIVE;
}
} else {
responseDataBlock->bStatus = CCID_COMMANDSTATUS_PROCESSEDWITHOUTERROR |
CCID_ICCSTATUS_NOICCPRESENT;
}
} else {
responseDataBlock->bError = CCID_ERROR_SLOTNOTFOUND;
responseDataBlock->bStatus = CCID_COMMANDSTATUS_FAILED | CCID_ICCSTATUS_NOICCPRESENT;
}
}
void CCID_NotifySlotChange(
struct rdr_to_pc_notify_slot_change* message,
uint8_t slot,
bool inserted) {
if(slot == CCID_SLOT_INDEX && inserted != furi_hal_usb_ccid->smartcard_inserted) {
message->bMessageType = RDR_TO_PC_NOTIFYSLOTCHANGE;
if(inserted) {
message->bmSlotICCState[0] = 0x40; //ICC inserted for slot 0
} else {
message->bmSlotICCState[0] = 0x80; //ICC removed for slot 0
}
}
}
void furi_hal_usb_ccid_insert_smartcard(void) {
furi_check(furi_hal_usb_ccid);
furi_hal_usb_ccid->smartcard_inserted = true;
furi_thread_flags_set(
furi_thread_get_id(furi_hal_usb_ccid->ccid_thread), WorkerEvtInsertSmartcard);
}
void furi_hal_usb_ccid_remove_smartcard(void) {
furi_check(furi_hal_usb_ccid);
furi_hal_usb_ccid->smartcard_inserted = false;
furi_thread_flags_set(
furi_thread_get_id(furi_hal_usb_ccid->ccid_thread), WorkerEvtRemoveSmartcard);
}
void furi_hal_usb_ccid_set_callbacks(CcidCallbacks* cb, void* context) {
furi_check(furi_hal_usb_ccid);
furi_hal_usb_ccid->callbacks[CCID_SLOT_INDEX] = cb;
furi_hal_usb_ccid->cb_ctx[CCID_SLOT_INDEX] = context;
}
void ccid_send_packet(uint8_t* data, uint8_t len) {
furi_check(furi_hal_usb_ccid);
if(furi_hal_usb_ccid->ccid_semaphore == NULL || furi_hal_usb_ccid->connected == false) return;
furi_check(
furi_semaphore_acquire(furi_hal_usb_ccid->ccid_semaphore, FuriWaitForever) ==
FuriStatusOk);
if(furi_hal_usb_ccid->connected == true) {
usbd_ep_write(furi_hal_usb_ccid->usb_dev, CCID_IN_EPADDR, data, len);
}
}
void ccid_send_response(uint8_t* data, uint32_t len) {
uint32_t data_to_send = len;
uint32_t data_index = 0;
while(data_to_send >= CCID_EPSIZE) {
ccid_send_packet(&data[data_index], CCID_EPSIZE);
data_to_send = data_to_send - CCID_EPSIZE;
data_index = data_index + CCID_EPSIZE;
}
ccid_send_packet(&data[data_index], data_to_send);
}
static void ccid_rx_ep_callback(usbd_device* dev, uint8_t event, uint8_t ep) {
UNUSED(dev);
UNUSED(event);
UNUSED(ep);
furi_check(furi_hal_usb_ccid);
furi_semaphore_release(furi_hal_usb_ccid->ccid_semaphore);
}
static void ccid_tx_ep_callback(usbd_device* dev, uint8_t event, uint8_t ep) {
UNUSED(dev);
UNUSED(ep);
if(event == usbd_evt_eprx) {
furi_check(furi_hal_usb_ccid);
int32_t bytes_read = usbd_ep_read(
furi_hal_usb_ccid->usb_dev,
CCID_OUT_EPADDR,
&furi_hal_usb_ccid->receive_buffer[furi_hal_usb_ccid->receive_buffer_data_index],
CCID_EPSIZE);
if(bytes_read > 0) {
furi_hal_usb_ccid->receive_buffer_data_index =
furi_hal_usb_ccid->receive_buffer_data_index + bytes_read;
furi_thread_flags_set(
furi_thread_get_id(furi_hal_usb_ccid->ccid_thread), WorkerEvtRequest);
}
}
}
static int32_t ccid_worker(void* context) {
UNUSED(context);
while(1) {
furi_check(furi_hal_usb_ccid);
uint32_t flags = furi_thread_flags_wait(
WorkerEvtStop | WorkerEvtRequest, FuriFlagWaitAny, FuriWaitForever);
if(flags & WorkerEvtRequest) {
//read initial CCID message header
ccid_bulk_message_header_t* message =
(ccid_bulk_message_header_t*)&furi_hal_usb_ccid->receive_buffer; //-V641
furi_check(message);
if(message->bMessageType == PC_TO_RDR_ICCPOWERON) {
struct pc_to_rdr_icc_power_on* requestDataBlock =
(struct pc_to_rdr_icc_power_on*)message; //-V641
struct rdr_to_pc_data_block* responseDataBlock =
(struct rdr_to_pc_data_block*)&furi_hal_usb_ccid->send_buffer;
CALLBACK_CCID_IccPowerOn(
requestDataBlock->bSlot, requestDataBlock->bSeq, responseDataBlock);
ccid_send_response(
furi_hal_usb_ccid->send_buffer,
sizeof(struct rdr_to_pc_data_block) +
(sizeof(uint8_t) * responseDataBlock->dwLength));
furi_hal_usb_ccid->receive_buffer_data_index = 0;
} else if(message->bMessageType == PC_TO_RDR_ICCPOWEROFF) {
struct pc_to_rdr_icc_power_off* requestIccPowerOff =
(struct pc_to_rdr_icc_power_off*)message; //-V641
struct rdr_to_pc_slot_status* responseSlotStatus =
(struct rdr_to_pc_slot_status*)&furi_hal_usb_ccid->send_buffer; //-V641
CALLBACK_CCID_GetSlotStatus(
requestIccPowerOff->bSlot, requestIccPowerOff->bSeq, responseSlotStatus);
ccid_send_response(
furi_hal_usb_ccid->send_buffer, sizeof(struct rdr_to_pc_slot_status));
furi_hal_usb_ccid->receive_buffer_data_index = 0;
} else if(message->bMessageType == PC_TO_RDR_GETSLOTSTATUS) {
struct pc_to_rdr_get_slot_status* requestSlotStatus =
(struct pc_to_rdr_get_slot_status*)message; //-V641
struct rdr_to_pc_slot_status* responseSlotStatus =
(struct rdr_to_pc_slot_status*)&furi_hal_usb_ccid->send_buffer; //-V641
CALLBACK_CCID_GetSlotStatus(
requestSlotStatus->bSlot, requestSlotStatus->bSeq, responseSlotStatus);
ccid_send_response(
furi_hal_usb_ccid->send_buffer, sizeof(struct rdr_to_pc_slot_status));
furi_hal_usb_ccid->receive_buffer_data_index = 0;
} else if(message->bMessageType == PC_TO_RDR_XFRBLOCK) {
struct pc_to_rdr_xfr_block* receivedXfrBlock =
(struct pc_to_rdr_xfr_block*)message;
struct rdr_to_pc_data_block* responseDataBlock =
(struct rdr_to_pc_data_block*)&furi_hal_usb_ccid->send_buffer;
if(furi_hal_usb_ccid->receive_buffer_data_index >=
sizeof(struct pc_to_rdr_xfr_block) + receivedXfrBlock->dwLength) {
CALLBACK_CCID_XfrBlock(receivedXfrBlock, responseDataBlock);
ccid_send_response(
furi_hal_usb_ccid->send_buffer,
sizeof(struct rdr_to_pc_data_block) +
(sizeof(uint8_t) * responseDataBlock->dwLength));
furi_hal_usb_ccid->receive_buffer_data_index = 0;
}
} else if(message->bMessageType == PC_TO_RDR_SETPARAMETERS) {
struct pc_to_rdr_set_parameters_t0* requestSetParametersT0 =
(struct pc_to_rdr_set_parameters_t0*)message; //-V641
struct rdr_to_pc_parameters_t0* responseSetParametersT0 =
(struct rdr_to_pc_parameters_t0*)&furi_hal_usb_ccid->send_buffer; //-V641
CALLBACK_CCID_SetParametersT0(requestSetParametersT0, responseSetParametersT0);
ccid_send_response(
furi_hal_usb_ccid->send_buffer, sizeof(struct rdr_to_pc_parameters_t0));
furi_hal_usb_ccid->receive_buffer_data_index = 0;
}
} else if(flags & WorkerEvtStop) {
break;
} else if(flags & WorkerEvtInsertSmartcard) {
if(!furi_hal_usb_ccid->smartcard_inserted) {
struct rdr_to_pc_notify_slot_change* responseNotifySlotChange =
(struct rdr_to_pc_notify_slot_change*)&furi_hal_usb_ccid->send_buffer;
CCID_NotifySlotChange(responseNotifySlotChange, CCID_SLOT_INDEX, true);
usbd_ep_write(
furi_hal_usb_ccid->usb_dev,
CCID_INTERRUPT_EPADDR,
furi_hal_usb_ccid->send_buffer,
sizeof(struct rdr_to_pc_notify_slot_change) + sizeof(uint8_t));
}
} else if(flags & WorkerEvtRemoveSmartcard) {
if(furi_hal_usb_ccid->smartcard_inserted) {
struct rdr_to_pc_notify_slot_change* responseNotifySlotChange =
(struct rdr_to_pc_notify_slot_change*)&furi_hal_usb_ccid->send_buffer;
CCID_NotifySlotChange(responseNotifySlotChange, CCID_SLOT_INDEX, false);
usbd_ep_write(
furi_hal_usb_ccid->usb_dev,
CCID_INTERRUPT_EPADDR,
furi_hal_usb_ccid->send_buffer,
sizeof(struct rdr_to_pc_notify_slot_change) + sizeof(uint8_t));
}
}
}
return 0;
}
/* Configure endpoints */
static usbd_respond ccid_ep_config(usbd_device* dev, uint8_t cfg) {
switch(cfg) {
case 0:
/* deconfiguring device */
usbd_ep_deconfig(dev, CCID_IN_EPADDR);
usbd_ep_deconfig(dev, CCID_OUT_EPADDR);
usbd_ep_deconfig(dev, CCID_INTERRUPT_EPADDR);
usbd_reg_endpoint(dev, CCID_IN_EPADDR, 0);
usbd_reg_endpoint(dev, CCID_OUT_EPADDR, 0);
return usbd_ack;
case 1:
/* configuring device */
usbd_ep_config(dev, CCID_IN_EPADDR, USB_EPTYPE_BULK, CCID_EPSIZE);
usbd_ep_config(dev, CCID_OUT_EPADDR, USB_EPTYPE_BULK, CCID_EPSIZE);
usbd_ep_config(dev, CCID_INTERRUPT_EPADDR, USB_EPTYPE_INTERRUPT, CCID_EPSIZE);
usbd_reg_endpoint(dev, CCID_IN_EPADDR, ccid_rx_ep_callback);
usbd_reg_endpoint(dev, CCID_OUT_EPADDR, ccid_tx_ep_callback);
return usbd_ack;
default:
return usbd_fail;
}
}
/* Control requests handler */
static usbd_respond ccid_control(usbd_device* dev, usbd_ctlreq* req, usbd_rqc_callback* callback) {
UNUSED(callback);
/* CDC control requests */
if(((USB_REQ_RECIPIENT | USB_REQ_TYPE) & req->bmRequestType) ==
(USB_REQ_INTERFACE | USB_REQ_CLASS) &&
(req->wIndex == 0 || req->wIndex == 2)) {
switch(req->bRequest) {
case CCID_ABORT:
return usbd_fail;
case CCID_GET_CLOCK_FREQUENCIES:
dev->status.data_ptr = (void*)&(ccid_cfg_desc.intf_0.ccid_desc.dwDefaultClock);
dev->status.data_count = sizeof(ccid_cfg_desc.intf_0.ccid_desc.dwDefaultClock);
return usbd_ack;
default:
return usbd_fail;
}
}
return usbd_fail;
}