Skip to content

Commit bcf0287

Browse files
committed
refactor(core): simplify usb driver api
[no changelog]
1 parent b4f857b commit bcf0287

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+1339
-2551
lines changed

core/SConscript.bootloader

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ FEATURES_WANTED = [
2727
"secure_mode",
2828
"suspend",
2929
"usb",
30+
"usb_iface_wire",
3031
]
3132

3233
if TREZOR_MODEL in ('T3W1', ):

core/SConscript.bootloader_ci

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ FEATURES_WANTED = [
1818
"secure_domain",
1919
"secure_mode",
2020
"usb",
21+
"usb_iface_wire",
2122
]
2223

2324
CCFLAGS_MOD = ''

core/SConscript.bootloader_emu

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ FEATURES_WANTED = [
2929
"power_manager",
3030
"rgb_led",
3131
"secure_mode",
32+
"usb",
33+
"usb_iface_wire",
3234
]
3335

3436
CCFLAGS_MOD = ''

core/SConscript.firmware

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ FEATURES_WANTED = [
6464
"suspend",
6565
"tropic",
6666
"usb",
67+
"usb_iface_wire",
6768
]
6869

6970
if DISABLE_OPTIGA:

core/SConscript.kernel

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ FEATURES_WANTED = [
5151
"suspend",
5252
"tropic",
5353
"usb",
54+
"usb_iface_wire",
55+
]
56+
57+
if BITCOIN_ONLY == '0':
58+
FEATURES_WANTED += [
59+
"usb_iface_webauthn",
60+
]
61+
62+
if PYOPT == '0':
63+
FEATURES_WANTED += [
64+
"usb_iface_debug",
65+
"usb_iface_vcp",
5466
]
5567

5668
if not TREZOR_MODEL in ['T3W1', 'D002']:

core/SConscript.prodtest

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ FEATURES_WANTED = [
4242
"suspend",
4343
"tropic",
4444
"usb",
45+
"usb_iface_vcp",
4546
]
4647

4748
CCFLAGS_MOD = ''

core/SConscript.prodtest_emu

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ FEATURES_WANTED = [
3232
"sd_card",
3333
"secure_mode",
3434
"tropic",
35-
"usb"
35+
"usb",
36+
"usb_iface_vcp",
3637
]
3738

3839
CCFLAGS_MOD = ''

core/SConscript.unix

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ FEATURES_WANTED = [
3434
"sd_card",
3535
"secure_mode",
3636
"storage",
37+
"usb",
38+
"usb_iface_wire",
39+
"usb_iface_debug",
40+
]
41+
42+
if BITCOIN_ONLY == '0':
43+
FEATURES_WANTED += [
44+
"usb_iface_webauthn",
3745
]
3846

3947
if not DISABLE_TROPIC:

core/embed/io/usb/inc/io/usb.h

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,10 @@
1717
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
*/
1919

20-
#ifndef __TREZORHAL_USB_H__
21-
#define __TREZORHAL_USB_H__
20+
#pragma once
2221

2322
#include <trezor_types.h>
2423

25-
#include <io/usb_hid.h>
26-
#include <io/usb_vcp.h>
27-
#include <io/usb_webusb.h>
28-
2924
#define USB_PACKET_LEN 64
3025

3126
typedef enum {
@@ -70,60 +65,78 @@ typedef union {
7065
//
7166
// clang-format on
7267

68+
#define USB_MAX_STR_SIZE 62
69+
7370
typedef struct {
7471
uint8_t device_class;
7572
uint8_t device_subclass;
7673
uint8_t device_protocol;
7774
uint16_t vendor_id;
7875
uint16_t product_id;
7976
uint16_t release_num;
80-
const char *manufacturer;
81-
const char *product;
82-
const char *serial_number;
83-
const char *interface;
77+
char manufacturer[USB_MAX_STR_SIZE + 1];
78+
char product[USB_MAX_STR_SIZE + 1];
79+
char serial_number[USB_MAX_STR_SIZE + 1];
80+
char interface[USB_MAX_STR_SIZE + 1];
8481
secbool usb21_enabled;
8582
secbool usb21_landing;
8683
} usb_dev_info_t;
8784

88-
// Initializes USB stack
89-
//
90-
// When the USB driver is initialized, class drivers can be registered.
91-
// After all class drivers are registered, `usb_start()` can be called.
92-
//
93-
// Returns `sectrue` if the initialization is successful.
85+
typedef struct {
86+
char serial_number[USB_MAX_STR_SIZE + 1];
87+
secbool usb21_landing;
88+
} usb_start_params_t;
89+
90+
/**
91+
* Initializes the USB stack driver.
92+
*
93+
* When the USB driver is initialized, class drivers can be registered using
94+
* `usb_xxx_add()` functions. After all class drivers are registered,
95+
* `usb_start` can be called.
96+
*
97+
* @param dev_info Pointer to USB device information structure.
98+
* @return `sectrue` if the initialization is successful.
99+
*/
94100
secbool usb_init(const usb_dev_info_t *dev_info);
95101

96-
// Deinitialize USB stack
97-
//
98-
// This function completely deinitializes the USB driver and all class drivers.
99-
// After this function is called, `usb_init()` can be called again.
102+
/**
103+
* Deinitializes the USB stack.
104+
*
105+
* This function completely deinitializes the USB driver and all class drivers.
106+
* After this function is called, `usb_init` can be called again.
107+
*/
100108
void usb_deinit(void);
101109

102-
// Starts USB driver and its class drivers
103-
//
104-
// Initializes the USB stack (and hardware) and starts all registered class
105-
// drivers.
106-
//
107-
// This function can be called after all class drivers are registered or after
108-
// `usb_stop()` is called.
109-
//
110-
// Returns `sectrue` if the USB stack is started successfully.
111-
secbool usb_start(void);
110+
/**
111+
* Starts the USB stack and registered class drivers.
112+
*
113+
* @param params Parameter that can be used to change some
114+
* settings specified during USB stack initialization. May be `NULL`.
115+
*
116+
* @return `sectrue` if the USB stack is started successfully.
117+
*/
118+
secbool usb_start(usb_start_params_t *params);
112119

113-
// Stops USB driver and its class drivers
114-
//
115-
// Uninitializes the USB stack (and hardware) but leaves all configuration
116-
// intact, so it can be started again with `usb_start()`.
117-
//
118-
// When the USB stack is stopped, it does not respond to any USB events and
119-
// the CPU can go to stop/standby mode.
120+
/**
121+
* Stops the USB stack but leaves all configuration intact,
122+
* so it can be re-started again with @ref usb_start.
123+
*
124+
* When the USB stack is stopped, it does not respond to any USB events and
125+
* the CPU can go to stop/standby mode.
126+
*/
120127
void usb_stop(void);
121128

122-
// Reads USB event
123-
// Return USB_EVENT_NONE if no event is available
129+
/**
130+
* @brief Reads a USB event.
131+
*
132+
* @return USB_EVENT_NONE if no event is available.
133+
*/
124134
usb_event_t usb_get_event(void);
125135

126-
// Reads USB state into `state`
136+
/**
137+
* @brief Reads the USB state into the provided structure.
138+
*
139+
* @param state Pointer to a @ref usb_state_t structure to receive the
140+
* current state.
141+
*/
127142
void usb_get_state(usb_state_t *state);
128-
129-
#endif

core/embed/io/usb/inc/io/usb_config.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* This file is part of the Trezor project, https://trezor.io/
3+
*
4+
* Copyright (c) SatoshiLabs
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#pragma once
21+
22+
#include <sys/sysevent.h>
23+
24+
typedef void (*usb_vcp_intr_callback_t)(void);
25+
26+
/**
27+
* Initialize and configures USB stack and all enabled USB interfaces.
28+
*
29+
* @param vcp_intr_callback Optional callback to be called on VCP interrupt.
30+
*/
31+
secbool usb_configure(usb_vcp_intr_callback_t vcp_intr_callback);

0 commit comments

Comments
 (0)