Skip to content

Commit 1f64cd3

Browse files
committed
U2F: separate queues
1 parent 98cbdb5 commit 1f64cd3

File tree

23 files changed

+405
-200
lines changed

23 files changed

+405
-200
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ set(DBB-FIRMWARE-USB-SOURCES
7878
${CMAKE_SOURCE_DIR}/src/usb/usb.c
7979
${CMAKE_SOURCE_DIR}/src/usb/usb_frame.c
8080
${CMAKE_SOURCE_DIR}/src/usb/usb_packet.c
81+
${CMAKE_SOURCE_DIR}/src/u2f/u2f_packet.c
8182
${CMAKE_SOURCE_DIR}/src/usb/usb_processing.c
8283
${CMAKE_SOURCE_DIR}/src/queue.c
8384
)

src/bootloader/bootloader.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ static void _api_setup(void)
779779
{
780780
const CMD_Callback cmd_callbacks[] = {{BOOTLOADER_CMD, _api_msg}};
781781

782-
usb_processing_register_cmds(cmd_callbacks, 1);
782+
usb_processing_register_cmds(usb_processing_hww(), cmd_callbacks, 1);
783783
}
784784

785785
#ifdef BOOTLOADER_PRODUCTION

src/bootloader/startup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ int main(void)
5050

5151
// If did not jump to firmware code, begin USB processing
5252
while (1) {
53-
usb_processing_process();
53+
usb_processing_process(usb_processing_hww());
5454
}
5555

5656
return 0;

src/drivers/usb/class/hid/hww/hid_hww.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <queue.h>
1818
#include "hid_hww.h"
1919
#include "usb_desc.h"
20+
#include "usb/usb_processing.h"
2021

2122
#define HID_HWW_VERSION 0x00000001u
2223

@@ -125,6 +126,8 @@ void hid_hww_setup(void) {
125126
// usb_report_sent is called when the outgoing usb frame is fully transmitted.
126127
hid_hww_register_callback(HID_CB_WRITE, (FUNC_PTR) _sent_done);
127128

129+
usb_processing_set_send(usb_processing_hww(), _send_next);
130+
128131
// Wait for data
129132
_read();
130133
}

src/drivers/usb/class/hid/u2f/hid_u2f.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
// limitations under the License.
1414

1515
#include <string.h>
16-
#include <usb/usb_packet.h>
16+
#include <u2f/u2f_packet.h>
1717
#include <queue.h>
1818
#include "hid_u2f.h"
1919
#include "usb_desc.h"
20+
#include "usb/usb_processing.h"
2021

2122
#define HID_U2F_VERSION 0x00000001u
2223

@@ -65,7 +66,7 @@ static int32_t _read(void)
6566
* Sends the next data, if the USB interface is ready.
6667
*/
6768
static void _send_next(void) {
68-
const uint8_t *data = queue_pull(queue_hww_queue());
69+
const uint8_t *data = queue_pull(queue_u2f_queue());
6970
if (data != NULL) {
7071
hid_write(&_func_data, data, USB_HID_REPORT_OUT_SIZE);
7172
} else {
@@ -86,7 +87,7 @@ static uint8_t _out(const uint8_t ep, const enum usb_xfer_code rc,
8687
(void) rc;
8788
(void) count;
8889

89-
bool need_more = usb_packet_process((const USB_FRAME *) _out_report, _send_next);
90+
bool need_more = u2f_packet_process((const USB_FRAME *) _out_report, _send_next);
9091
if (need_more) {
9192
_read();
9293
}
@@ -125,6 +126,8 @@ void hid_u2f_setup(void) {
125126
// usb_report_sent is called when the outgoing usb frame is fully transmitted.
126127
hid_u2f_register_callback(HID_CB_WRITE, (FUNC_PTR) _sent_done);
127128

129+
usb_processing_set_send(usb_processing_u2f(), _send_next);
130+
128131
// Wait for data
129132
_read();
130133
}

src/factorysetup.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ static void _api_msg(const Packet* in_packet, Packet* out_packet, const size_t m
170170
static void _api_setup(void)
171171
{
172172
const CMD_Callback cmd_callbacks[] = {{FACTORYSETUP_CMD, _api_msg}};
173-
usb_processing_register_cmds(cmd_callbacks, sizeof(cmd_callbacks) / sizeof(CMD_Callback));
173+
usb_processing_register_cmds(
174+
usb_processing_hww(), cmd_callbacks, sizeof(cmd_callbacks) / sizeof(CMD_Callback));
174175
screen_print_debug("READY", 0);
175176
}
176177

@@ -197,6 +198,6 @@ int main(void)
197198
}
198199
usb_start(_api_setup);
199200
while (1) {
200-
usb_processing_process();
201+
usb_processing_process(usb_processing_hww());
201202
}
202203
}

src/hww.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,5 @@ void hww_setup(void)
110110
{
111111
const CMD_Callback hww_cmd_callbacks[] = {{HWW_MSG, _msg}};
112112
usb_processing_register_cmds(
113-
hww_cmd_callbacks, sizeof(hww_cmd_callbacks) / sizeof(CMD_Callback));
113+
usb_processing_hww(), hww_cmd_callbacks, sizeof(hww_cmd_callbacks) / sizeof(CMD_Callback));
114114
}

src/queue.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626
// `start` and `end` are indices into `items`
2727
struct queue {
28-
uint32_t start;
29-
uint32_t end;
28+
uint32_t volatile start;
29+
uint32_t volatile end;
3030
uint8_t items[QUEUE_NUM_REPORTS][USB_REPORT_SIZE];
3131
};
3232

@@ -47,7 +47,7 @@ const uint8_t* queue_pull(struct queue* ctx)
4747
return ctx->items[p];
4848
}
4949

50-
int32_t queue_push(struct queue* ctx, const uint8_t* data)
50+
queue_error_t queue_push(struct queue* ctx, const uint8_t* data)
5151
{
5252
uint32_t next = (ctx->end + 1) % QUEUE_NUM_REPORTS;
5353
if (ctx->start == next) {

src/queue.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
#include <stdint.h>
1919
#include <string.h>
2020

21-
#define QUEUE_ERR_NONE 0
22-
#define QUEUE_ERR_FULL -1
21+
typedef enum {
22+
QUEUE_ERR_NONE = 0,
23+
QUEUE_ERR_FULL = 1,
24+
} queue_error_t;
2325

2426
struct queue;
2527

@@ -28,7 +30,7 @@ struct queue;
2830
* Returns QUEUE_ERR_NONE if the data was added and QUEUE_ERR_FULL if the buffer was full.
2931
* data must be USB_REPORT_SIZE large
3032
*/
31-
int32_t queue_push(struct queue* ctx, const uint8_t* data);
33+
queue_error_t queue_push(struct queue* ctx, const uint8_t* data);
3234

3335
/**
3436
* Return the first data that was added to the queue.

src/u2f.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,6 @@ void u2f_device_setup(void)
577577
{U2FHID_MSG, _cmd_msg},
578578
};
579579
usb_processing_register_cmds(
580-
u2f_cmd_callbacks, sizeof(u2f_cmd_callbacks) / sizeof(CMD_Callback));
580+
usb_processing_u2f(), u2f_cmd_callbacks, sizeof(u2f_cmd_callbacks) / sizeof(CMD_Callback));
581581
#endif
582582
}

0 commit comments

Comments
 (0)