-
Notifications
You must be signed in to change notification settings - Fork 55
/
usb-hid.c
694 lines (568 loc) · 18.4 KB
/
usb-hid.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
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
/*
* usb-hid.c - HID device descriptors and communication
*
* Copyright (C) 2017 Sergei Glushchenko
* Author: Sergei Glushchenko <gl.sergei@gmail.com>
*
* This file is a part of U2F firmware for STM32
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* As additional permission under GNU GPL version 3 section 7, you may
* distribute non-source form of the Program without the copy of the
* GNU GPL normally required by section 4, provided you inform the
* recipients of GNU GPL by a written offer.
*
*/
#include <stdint.h>
#include <stdlib.h>
#include <chopstx.h>
#include <eventflag.h>
#include <string.h>
#include "usb_lld.h"
#include "usb-hid.h"
#define ENDP0_RXADDR (0x040)
#define ENDP0_TXADDR (0x080)
#define ENDP1_RXADDR (0x0c0)
#define ENDP1_TXADDR (0x100)
#define HID_INTERFACE 0
#define USB_HID_REQ_GET_REPORT 1
#define USB_HID_REQ_GET_IDLE 2
#define USB_HID_REQ_GET_PROTOCOL 3
#define USB_HID_REQ_SET_REPORT 9
#define USB_HID_REQ_SET_IDLE 10
#define USB_HID_REQ_SET_PROTOCOL 11
#define USB_DT_HID 0x21
#define USB_DT_REPORT 0x22
// Size of HID reports
#define HID_RPT_SIZE 64 // Default size of raw HID report
static chopstx_intr_t interrupt;
static uint8_t hid_idle_rate; /* in 4ms */
/* HID report descriptor. */
#define HID_REPORT_DESC_SIZE (sizeof (hid_report_desc))
static const uint8_t hid_report_desc[] = {
0x06, 0xd0, 0xf1, /* USAGE_PAGE (FIDO Alliance) */
0x09, 0x01, /* USAGE (Keyboard) */
0xa1, 0x01, /* COLLECTION (Application) */
0x09, 0x20, /* USAGE (Input report data) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x95, 0x40, /* REPORT_COUNT (64) */
0x81, 0x02, /* INPUT (Data,Var,Abs); Modifier byte */
0x09, 0x21, /* USAGE (Output report data) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x26, 0xff, 0x00, /* LOGICAL_MAXIMUM (255) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x95, 0x40, /* REPORT_COUNT (64) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs); Modifier byte */
0xc0 /* END_COLLECTION */
};
/* USB Device Descriptor */
static const uint8_t u2f_device_desc[18] = {
18, /* bLength */
DEVICE_DESCRIPTOR, /* bDescriptorType */
0x10, 0x01, /* bcdUSB = 1.1 */
0x00, /* bDeviceClass (Unknown). */
0x00, /* bDeviceSubClass. */
0x00, /* bDeviceProtocol. */
0x40, /* bMaxPacketSize. */
0x83, 0x04, /* idVendor */
0xab, 0xcd, /* idProduct */
0x00, 0x01, /* bcdDevice */
1, /* iManufacturer. */
2, /* iProduct. */
3, /* iSerialNumber. */
1 /* bNumConfigurations. */
};
#define FEATURE_BUS_POWERED 0x80
/* Configuration Descriptor tree for a HID.*/
static const uint8_t u2f_config_desc[41] = {
9,
CONFIG_DESCRIPTOR, /* bDescriptorType: Configuration */
/* Configuration Descriptor.*/
41, 0x00, /* wTotalLength. */
0x01, /* bNumInterfaces. */
0x01, /* bConfigurationValue. */
0, /* iConfiguration. */
FEATURE_BUS_POWERED, /* bmAttributes. */
50, /* bMaxPower (100mA). */
/* Interface Descriptor.*/
9, /* bLength: Interface Descriptor size */
INTERFACE_DESCRIPTOR, /* bDescriptorType: Interface */
HID_INTERFACE, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x02, /* bNumEndpoints: Two endpoints used */
0x03, /* bInterfaceClass: HID */
0x00, /* bInterfaceSubClass: no boot */
0x00, /* bInterfaceProtocol: 0=none */
0x04, /* iInterface */
/* HID Descriptor.*/
9, /* bLength: HID Descriptor size */
0x21, /* bDescriptorType: HID */
0x10, 0x01, /* bcdHID: HID Class Spec release number */
0x00, /* bCountryCode: Hardware target country */
0x01, /* bNumDescriptors: Number of HID class descriptors to follow */
0x22, /* bDescriptorType */
HID_REPORT_DESC_SIZE, 0, /* wItemLength: Total length of Report descriptor */
/*Endpoint IN1 Descriptor*/
7, /* bLength: Endpoint Descriptor size */
ENDPOINT_DESCRIPTOR, /* bDescriptorType: Endpoint */
0x81, /* bEndpointAddress: (IN1) */
0x03, /* bmAttributes: Interrupt */
0x40, 0x00, /* wMaxPacketSize: 64 */
0x05, /* bInterval (5ms) */
/*Endpoint OUT1 Descriptor*/
7, /* bLength: Endpoint Descriptor size */
ENDPOINT_DESCRIPTOR, /* bDescriptorType: Endpoint */
0x01, /* bEndpointAddress: (OUT1) */
0x03, /* bmAttributes: Interrupt */
0x40, 0x00, /* wMaxPacketSize: 64 */
0x05, /* bInterval (5ms) */
};
/*
* U.S. English language identifier.
*/
static const uint8_t usb_string0[4] = {
4, /* bLength */
STRING_DESCRIPTOR,
0x09, 0x04 /* LangID = 0x0409: US-English */
};
static const uint8_t usb_string1[] = {
9*2+2, /* bLength */
STRING_DESCRIPTOR, /* bDescriptorType */
/* Manufacturer: "Gl.Sergei" */
'G', 0, 'l', 0, '.', 0, 'S', 0, 'e', 0, 'r', 0, 'g', 0, 'e', 0,
'i', 0,
};
static const uint8_t usb_string2[] = {
17*2+2, /* bLength */
STRING_DESCRIPTOR, /* bDescriptorType */
/* Product name: "U2F-token (STM32)" */
'U', 0, '2', 0, 'F', 0, '-', 0, 't', 0, 'o', 0, 'k', 0, 'e', 0,
'n', 0, ' ', 0, '(', 0, 'S', 0, 'T', 0, 'M', 0, '3', 0, '2', 0,
')', 0,
};
/*
* Serial Number string.
*/
static const uint8_t usb_string3[28] = {
28, /* bLength */
STRING_DESCRIPTOR, /* bDescriptorType */
'0', 0, '.', 0, '0', 0, '0', 0, /* Version number */
};
#define NUM_INTERFACES 1
static void
usb_device_reset (struct usb_dev *dev)
{
usb_lld_reset (dev, FEATURE_BUS_POWERED);
/* Initialize Endpoint 0 */
usb_lld_setup_endpoint (ENDP0, EP_CONTROL, 0,
ENDP0_RXADDR, ENDP0_TXADDR, HID_RPT_SIZE);
}
static void
usb_ctrl_write_finish (struct usb_dev *dev)
{
struct device_req *arg = &dev->dev_req;
if (arg->index == HID_INTERFACE && arg->request == USB_HID_REQ_SET_REPORT)
{
return;
}
/*
* The transaction was already finished. So, it is no use to call
* usb_lld_ctrl_error when the condition does not match.
*/
}
static int
usb_setup (struct usb_dev *dev)
{
struct device_req *arg = &dev->dev_req;
if (arg->index == HID_INTERFACE)
{
switch (arg->request)
{
case USB_HID_REQ_GET_IDLE:
return usb_lld_ctrl_send (dev, &hid_idle_rate, 1);
case USB_HID_REQ_SET_IDLE:
return usb_lld_ctrl_recv (dev, &hid_idle_rate, 1);
case USB_HID_REQ_GET_REPORT:
/* Reports should go via interrupt endpoint */
return -1;
case USB_HID_REQ_SET_REPORT:
/* Reports should go via interrupt endpoint */
return -1;
case USB_HID_REQ_GET_PROTOCOL:
case USB_HID_REQ_SET_PROTOCOL:
/* This driver doesn't support boot protocol. */
return -1;
default:
return -1;
}
}
return -1;
}
static int
usb_get_descriptor (struct usb_dev *dev)
{
struct device_req *arg = &dev->dev_req;
uint8_t rcp = arg->type & RECIPIENT;
uint8_t desc_type = (arg->value >> 8);
uint8_t desc_index = (arg->value & 0xff);
if (rcp == INTERFACE_RECIPIENT)
{
if (arg->index == HID_INTERFACE)
{
if (desc_type == USB_DT_HID)
return usb_lld_ctrl_send (dev, u2f_config_desc + 9 + 9, 9);
else if (desc_type == USB_DT_REPORT)
return usb_lld_ctrl_send (dev, hid_report_desc,
HID_REPORT_DESC_SIZE);
}
}
else if (rcp == DEVICE_RECIPIENT)
{
if (desc_type == DEVICE_DESCRIPTOR)
return usb_lld_ctrl_send (dev,
u2f_device_desc, sizeof (u2f_device_desc));
else if (desc_type == CONFIG_DESCRIPTOR)
return usb_lld_ctrl_send (dev,
u2f_config_desc, sizeof (u2f_config_desc));
else if (desc_type == STRING_DESCRIPTOR)
{
const uint8_t *str;
int size;
switch (desc_index)
{
case 0:
str = usb_string0;
size = sizeof (usb_string0);
break;
case 1:
str = usb_string1;
size = sizeof (usb_string1);
break;
case 2:
str = usb_string2;
size = sizeof (usb_string2);
break;
case 3:
str = usb_string3;
size = sizeof (usb_string3);
break;
default:
return -1;
}
return usb_lld_ctrl_send (dev, str, size);
}
}
return -1;
}
static void
hid_setup_endpoints_for_interface (uint16_t interface, int stop)
{
if (interface == HID_INTERFACE)
{
if (!stop)
usb_lld_setup_endpoint (ENDP1, EP_INTERRUPT, 0,
ENDP1_RXADDR, ENDP1_TXADDR, HID_RPT_SIZE);
else
{
usb_lld_stall_tx (ENDP1);
usb_lld_stall_rx (ENDP1);
}
}
}
static int
usb_set_configuration (struct usb_dev *dev)
{
int i;
uint8_t current_conf;
current_conf = usb_lld_current_configuration (dev);
if (current_conf == 0)
{
if (dev->dev_req.value != 1)
return -1;
usb_lld_set_configuration (dev, 1);
for (i = 0; i < NUM_INTERFACES; i++)
hid_setup_endpoints_for_interface (i, 0);
}
else if (current_conf != dev->dev_req.value)
{
if (dev->dev_req.value != 0)
return -1;
usb_lld_set_configuration (dev, 0);
for (i = 0; i < NUM_INTERFACES; i++)
hid_setup_endpoints_for_interface (i, 1);
}
usb_lld_ctrl_ack (dev);
return 0;
}
static int
usb_set_interface (struct usb_dev *dev)
{
uint16_t interface = dev->dev_req.index;
uint16_t alt = dev->dev_req.value;
if (interface >= NUM_INTERFACES)
return -1;
if (alt != 0)
return -1;
else
{
hid_setup_endpoints_for_interface (interface, 0);
usb_lld_ctrl_ack (dev);
return 0;
}
}
static int
usb_get_interface (struct usb_dev *dev)
{
const uint8_t zero = 0;
uint16_t interface = dev->dev_req.index;
if (interface >= NUM_INTERFACES)
return -1;
/* We don't have alternate interface, so, always return 0. */
return usb_lld_ctrl_send (dev, &zero, 1);
}
static int
usb_get_status_interface (struct usb_dev *dev)
{
const uint16_t status_info = 0;
uint16_t interface = dev->dev_req.index;
if (interface >= NUM_INTERFACES)
return -1;
return usb_lld_ctrl_send (dev, &status_info, 2);
}
struct usb_hid {
uint8_t *tx_buf;
uint8_t rx_buf[HID_RPT_SIZE];
uint16_t tx_len;
uint16_t rx_len;
struct eventflag cmd_ev;
struct eventflag usb_ev;
struct eventflag fbk_ev;
};
/* cmd events */
#define EV_TX_NEED 1
/* usb events */
#define EV_RX_DATA_READY 1
/* fbk events */
#define EV_TX_FINISHED 1
/*
* Rx ready callback
*/
static void
ep1_out_received (struct usb_hid *hid, uint16_t len)
{
if (len <= HID_RPT_SIZE)
{
usb_lld_rxcpy (hid->rx_buf, 1 /*ep_num*/, 0 /*offset*/, len);
hid->rx_len = len;
}
eventflag_signal (&hid->usb_ev, EV_RX_DATA_READY);
}
/*
* Transmit some data
*/
static void
ep1_transmit (struct usb_hid *hid)
{
usb_lld_txcpy (hid->tx_buf, 1/*ep_num*/, 0 /*offset*/, hid->tx_len);
usb_lld_tx_enable (1/*ep_num*/, hid->tx_len);
}
static void
usb_rx_ready (struct usb_hid *hid, uint8_t ep_num, uint16_t len)
{
if (ep_num == ENDP1)
ep1_out_received (hid, len);
}
static void
usb_tx_done (struct usb_hid *hid, uint8_t ep_num, uint16_t len)
{
(void)hid;
(void)ep_num;
(void)len;
if (ep_num == ENDP1)
{
eventflag_signal (&hid->fbk_ev, EV_TX_FINISHED);
}
}
static void
usb_tx (struct usb_hid *hid, uint8_t ep_num)
{
if (ep_num == ENDP1)
ep1_transmit (hid);
}
#define INTR_REQ_USB 20
#define PRIO_HID 4
extern uint8_t __process3_stack_base__[], __process3_stack_size__[];
#define STACK_ADDR_HID ((uint32_t)__process3_stack_base__)
#define STACK_SIZE_HID ((uint32_t)__process3_stack_size__)
static void
poll_tx_intr (uint32_t *timeout, struct eventflag *ev, chopstx_intr_t *intr)
{
chopstx_poll_cond_t poll_desc;
struct chx_poll_head *pd_array[2] = {
(struct chx_poll_head *)intr,
(struct chx_poll_head *)&poll_desc
};
eventflag_prepare_poll (ev, &poll_desc);
chopstx_poll (timeout, 2, pd_array);
}
static void *
hid_main (void *arg)
{
uint32_t timeout;
struct usb_dev dev;
int e;
struct usb_hid *usb_hid = (struct usb_hid *) arg;
memset (usb_hid, 0, sizeof (struct usb_hid));
eventflag_init (&usb_hid->cmd_ev);
eventflag_init (&usb_hid->usb_ev);
eventflag_init (&usb_hid->fbk_ev);
chopstx_claim_irq (&interrupt, INTR_REQ_USB);
usb_lld_init (&dev, FEATURE_BUS_POWERED);
while (1)
{
eventmask_t m;
timeout = 1950 * 1000;
poll_tx_intr (&timeout, &usb_hid->cmd_ev, &interrupt);
if (interrupt.ready)
{
uint8_t ep_num;
/*
* When interrupt is detected, call usb_lld_event_handler.
* The event may be one of following:
* (1) Transfer to endpoint (bulk or interrupt)
* In this case EP_NUM is encoded in the variable E.
* (2) "NONE" event: some trasfer was done, but all was
* done by lower layer, no other work is needed in
* upper layer.
* (3) Device events: Reset or Suspend
* (4) Device requests to the endpoint zero.
*
*/
e = usb_lld_event_handler (&dev);
ep_num = USB_EVENT_ENDP (e);
if (ep_num != 0)
{
if (USB_EVENT_TXRX (e))
usb_tx_done (usb_hid, ep_num, USB_EVENT_LEN (e));
else
usb_rx_ready (usb_hid, ep_num, USB_EVENT_LEN (e));
}
else
switch (USB_EVENT_ID (e))
{
case USB_EVENT_DEVICE_RESET:
usb_device_reset (&dev);
continue;
case USB_EVENT_DEVICE_ADDRESSED:
/* The addres is assigned to the device. We don't
* need to do anything for this actually, but in this
* application, we maintain the USB status of the
* device. Usually, just "continue" as EVENT_OK is
* OK.
*/
continue;
case USB_EVENT_GET_DESCRIPTOR:
if (usb_get_descriptor (&dev) < 0)
usb_lld_ctrl_error (&dev);
continue;
case USB_EVENT_SET_CONFIGURATION:
if (usb_set_configuration (&dev) < 0)
usb_lld_ctrl_error (&dev);
continue;
case USB_EVENT_SET_INTERFACE:
if (usb_set_interface (&dev) < 0)
usb_lld_ctrl_error (&dev);
continue;
case USB_EVENT_CTRL_REQUEST:
/* Device specific device request. */
if (usb_setup (&dev) < 0)
usb_lld_ctrl_error (&dev);
continue;
case USB_EVENT_GET_STATUS_INTERFACE:
if (usb_get_status_interface (&dev) < 0)
usb_lld_ctrl_error (&dev);
continue;
case USB_EVENT_GET_INTERFACE:
if (usb_get_interface (&dev) < 0)
usb_lld_ctrl_error (&dev);
continue;
case USB_EVENT_SET_FEATURE_DEVICE:
case USB_EVENT_SET_FEATURE_ENDPOINT:
case USB_EVENT_CLEAR_FEATURE_DEVICE:
case USB_EVENT_CLEAR_FEATURE_ENDPOINT:
usb_lld_ctrl_ack (&dev);
continue;
case USB_EVENT_CTRL_WRITE_FINISH:
/* Control WRITE transfer finished. */
usb_ctrl_write_finish (&dev);
continue;
case USB_EVENT_OK:
case USB_EVENT_DEVICE_SUSPEND:
default:
continue;
}
}
m = eventflag_get (&usb_hid->cmd_ev);
if (m == EV_TX_NEED)
{
usb_tx (usb_hid, ENDP1);
}
}
return NULL;
}
static struct usb_hid usb_hid;
struct usb_hid *
hid_open (void)
{
chopstx_create (PRIO_HID, STACK_ADDR_HID, STACK_SIZE_HID, hid_main, &usb_hid);
return &usb_hid;
}
int
hid_send (struct usb_hid *usb_hid, uint8_t *buf, uint16_t len)
{
eventmask_t m;
if (len > HID_RPT_SIZE)
return -1;
usb_hid->tx_len = len;
usb_hid->tx_buf = buf;
eventflag_signal (&usb_hid->cmd_ev, EV_TX_NEED);
do
{
m = eventflag_wait (&usb_hid->fbk_ev);
}
while (m != EV_TX_FINISHED);
return len;
}
int
hid_recv (struct usb_hid *usb_hid, uint8_t *buf, uint16_t len, uint32_t timeout)
{
eventmask_t m;
usb_lld_rx_enable (ENDP1);
do
{
m = eventflag_wait_timeout (&usb_hid->usb_ev, timeout);
}
while (m != EV_RX_DATA_READY && m != 0);
if (m == 0)
return -1;
if (usb_hid->rx_len <= len)
{
memcpy (buf, usb_hid->rx_buf, usb_hid->rx_len);
return usb_hid->rx_len;
}
return -1;
}