Skip to content

Refactoring of USB API #5541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/SConscript.bootloader
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ FEATURES_WANTED = [
"secure_mode",
"suspend",
"usb",
"usb_iface_wire",
]

if TREZOR_MODEL in ('T3W1', ):
Expand Down
1 change: 1 addition & 0 deletions core/SConscript.bootloader_ci
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ FEATURES_WANTED = [
"secure_domain",
"secure_mode",
"usb",
"usb_iface_wire",
]

CCFLAGS_MOD = ''
Expand Down
2 changes: 2 additions & 0 deletions core/SConscript.bootloader_emu
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ FEATURES_WANTED = [
"power_manager",
"rgb_led",
"secure_mode",
"usb",
"usb_iface_wire",
]

CCFLAGS_MOD = ''
Expand Down
1 change: 1 addition & 0 deletions core/SConscript.firmware
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ FEATURES_WANTED = [
"suspend",
"tropic",
"usb",
"usb_iface_wire",
]

if DISABLE_OPTIGA:
Expand Down
12 changes: 12 additions & 0 deletions core/SConscript.kernel
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ FEATURES_WANTED = [
"suspend",
"tropic",
"usb",
"usb_iface_wire",
]

if BITCOIN_ONLY == '0':
FEATURES_WANTED += [
"usb_iface_webauthn",
]

if PYOPT == '0':
FEATURES_WANTED += [
"usb_iface_debug",
"usb_iface_vcp",
]

if not TREZOR_MODEL in ['T3W1', 'D002']:
Expand Down
1 change: 1 addition & 0 deletions core/SConscript.prodtest
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ FEATURES_WANTED = [
"suspend",
"tropic",
"usb",
"usb_iface_vcp",
]

CCFLAGS_MOD = ''
Expand Down
3 changes: 2 additions & 1 deletion core/SConscript.prodtest_emu
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ FEATURES_WANTED = [
"sd_card",
"secure_mode",
"tropic",
"usb"
"usb",
"usb_iface_vcp",
]

CCFLAGS_MOD = ''
Expand Down
8 changes: 8 additions & 0 deletions core/SConscript.unix
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ FEATURES_WANTED = [
"sd_card",
"secure_mode",
"storage",
"usb",
"usb_iface_wire",
"usb_iface_debug",
]

if BITCOIN_ONLY == '0':
FEATURES_WANTED += [
"usb_iface_webauthn",
]

if not DISABLE_TROPIC:
Expand Down
97 changes: 55 additions & 42 deletions core/embed/io/usb/inc/io/usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __TREZORHAL_USB_H__
#define __TREZORHAL_USB_H__
#pragma once

#include <trezor_types.h>

#include <io/usb_hid.h>
#include <io/usb_vcp.h>
#include <io/usb_webusb.h>

#define USB_PACKET_LEN 64

typedef enum {
Expand Down Expand Up @@ -70,60 +65,78 @@ typedef union {
//
// clang-format on

#define USB_MAX_STR_SIZE 62

typedef struct {
uint8_t device_class;
uint8_t device_subclass;
uint8_t device_protocol;
uint16_t vendor_id;
uint16_t product_id;
uint16_t release_num;
const char *manufacturer;
const char *product;
const char *serial_number;
const char *interface;
char manufacturer[USB_MAX_STR_SIZE + 1];
char product[USB_MAX_STR_SIZE + 1];
char serial_number[USB_MAX_STR_SIZE + 1];
char interface[USB_MAX_STR_SIZE + 1];
secbool usb21_enabled;
secbool usb21_landing;
} usb_dev_info_t;

// Initializes USB stack
//
// When the USB driver is initialized, class drivers can be registered.
// After all class drivers are registered, `usb_start()` can be called.
//
// Returns `sectrue` if the initialization is successful.
typedef struct {
char serial_number[USB_MAX_STR_SIZE + 1];
secbool usb21_landing;
} usb_start_params_t;

/**
* Initializes the USB stack driver.
*
* When the USB driver is initialized, class drivers can be registered using
* `usb_xxx_add()` functions. After all class drivers are registered,
* `usb_start` can be called.
*
* @param dev_info Pointer to USB device information structure.
* @return `sectrue` if the initialization is successful.
*/
secbool usb_init(const usb_dev_info_t *dev_info);

// Deinitialize USB stack
//
// This function completely deinitializes the USB driver and all class drivers.
// After this function is called, `usb_init()` can be called again.
/**
* Deinitializes the USB stack.
*
* This function completely deinitializes the USB driver and all class drivers.
* After this function is called, `usb_init` can be called again.
*/
void usb_deinit(void);

// Starts USB driver and its class drivers
//
// Initializes the USB stack (and hardware) and starts all registered class
// drivers.
//
// This function can be called after all class drivers are registered or after
// `usb_stop()` is called.
//
// Returns `sectrue` if the USB stack is started successfully.
secbool usb_start(void);
/**
* Starts the USB stack and registered class drivers.
*
* @param params Parameter that can be used to change some
* settings specified during USB stack initialization. May be `NULL`.
*
* @return `sectrue` if the USB stack is started successfully.
*/
secbool usb_start(usb_start_params_t *params);

// Stops USB driver and its class drivers
//
// Uninitializes the USB stack (and hardware) but leaves all configuration
// intact, so it can be started again with `usb_start()`.
//
// When the USB stack is stopped, it does not respond to any USB events and
// the CPU can go to stop/standby mode.
/**
* Stops the USB stack but leaves all configuration intact,
* so it can be re-started again with @ref usb_start.
*
* When the USB stack is stopped, it does not respond to any USB events and
* the CPU can go to stop/standby mode.
*/
void usb_stop(void);

// Reads USB event
// Return USB_EVENT_NONE if no event is available
/**
* @brief Reads a USB event.
*
* @return USB_EVENT_NONE if no event is available.
*/
usb_event_t usb_get_event(void);

// Reads USB state into `state`
/**
* @brief Reads the USB state into the provided structure.
*
* @param state Pointer to a @ref usb_state_t structure to receive the
* current state.
*/
void usb_get_state(usb_state_t *state);

#endif
31 changes: 31 additions & 0 deletions core/embed/io/usb/inc/io/usb_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* This file is part of the Trezor project, https://trezor.io/
*
* Copyright (c) SatoshiLabs
*
* 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/>.
*/

#pragma once

#include <sys/sysevent.h>

typedef void (*usb_vcp_intr_callback_t)(void);

/**
* Initialize and configures USB stack and all enabled USB interfaces.
*
* @param vcp_intr_callback Optional callback to be called on VCP interrupt.
*/
secbool usb_configure(usb_vcp_intr_callback_t vcp_intr_callback);
18 changes: 4 additions & 14 deletions core/embed/io/usb/inc/io/usb_hid.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef TREZORHAL_USB_CLASS_HID_H
#define TREZORHAL_USB_CLASS_HID_H
#pragma once

#include <trezor_types.h>

#include <sys/sysevent.h>

/* usb_hid_info_t contains all information for setting up a HID interface. All
* passed pointers need to live at least until the interface is disabled
* (usb_stop is called). */
typedef struct {
syshandle_t handle;
const uint8_t *report_desc; // With length of report_desc_len bytes
uint8_t *rx_buffer; // With length of max_packet_len bytes
uint8_t iface_num; // Address of this HID interface
Expand All @@ -43,15 +45,3 @@ typedef struct {
} usb_hid_info_t;

secbool __wur usb_hid_add(const usb_hid_info_t *hid_info);
secbool __wur usb_hid_can_read(uint8_t iface_num);
secbool __wur usb_hid_can_write(uint8_t iface_num);
int __wur usb_hid_read(uint8_t iface_num, uint8_t *buf, uint32_t len);
int __wur usb_hid_write(uint8_t iface_num, const uint8_t *buf, uint32_t len);

int __wur usb_hid_read_select(uint32_t timeout);
int __wur usb_hid_read_blocking(uint8_t iface_num, uint8_t *buf, uint32_t len,
int timeout);
int __wur usb_hid_write_blocking(uint8_t iface_num, const uint8_t *buf,
uint32_t len, int timeout);

#endif // TREZORHAL_USB_CLASS_HID_H
17 changes: 4 additions & 13 deletions core/embed/io/usb/inc/io/usb_vcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef TREZORHAL_USB_CLASS_VCP_H
#define TREZORHAL_USB_CLASS_VCP_H
#pragma once

#include <trezor_types.h>

#include <sys/sysevent.h>

/* usb_vcp_info_t contains all information for setting up a VCP interface. All
* passed pointers need to live at least until the interface is disabled
* (usb_stop is called). */
typedef struct {
syshandle_t handle;
uint8_t *tx_packet; // Buffer for one packet, with length of at least
// max_packet_len bytes
uint8_t *tx_buffer; // Buffer for IN EP ring buffer, with length of at least
Expand Down Expand Up @@ -55,14 +57,3 @@ typedef struct {
} usb_vcp_info_t;

secbool __wur usb_vcp_add(const usb_vcp_info_t *vcp_info);
secbool __wur usb_vcp_can_read(uint8_t iface_num);
secbool __wur usb_vcp_can_write(uint8_t iface_num);
int __wur usb_vcp_read(uint8_t iface_num, uint8_t *buf, uint32_t len);
int __wur usb_vcp_write(uint8_t iface_num, const uint8_t *buf, uint32_t len);

int __wur usb_vcp_read_blocking(uint8_t iface_num, uint8_t *buf, uint32_t len,
int timeout);
int __wur usb_vcp_write_blocking(uint8_t iface_num, const uint8_t *buf,
uint32_t len, int timeout);

#endif // TREZORHAL_USB_CLASS_VCP_H
18 changes: 4 additions & 14 deletions core/embed/io/usb/inc/io/usb_webusb.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef TREZORHAL_USB_CLASS_WEBUSB_H
#define TREZORHAL_USB_CLASS_WEBUSB_H
#pragma once

#include <trezor_types.h>

#include <sys/sysevent.h>

/* usb_webusb_info_t contains all information for setting up a WebUSB interface.
* All passed pointers need to live at least until the interface is disabled
* (usb_stop is called). */
typedef struct {
syshandle_t handle;
uint8_t *rx_buffer; // With length of max_packet_len bytes
uint8_t iface_num; // Address of this WebUSB interface
#ifdef TREZOR_EMULATOR
Expand All @@ -41,15 +43,3 @@ typedef struct {
} usb_webusb_info_t;

secbool __wur usb_webusb_add(const usb_webusb_info_t *webusb_info);
secbool __wur usb_webusb_can_read(uint8_t iface_num);
secbool __wur usb_webusb_can_write(uint8_t iface_num);
int __wur usb_webusb_read(uint8_t iface_num, uint8_t *buf, uint32_t len);
int __wur usb_webusb_write(uint8_t iface_num, const uint8_t *buf, uint32_t len);

int __wur usb_webusb_read_select(uint32_t timeout);
int __wur usb_webusb_read_blocking(uint8_t iface_num, uint8_t *buf,
uint32_t len, int timeout);
int __wur usb_webusb_write_blocking(uint8_t iface_num, const uint8_t *buf,
uint32_t len, int timeout);

#endif // TREZORHAL_USB_CLASS_WEBUSB_H
Loading