mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-26 09:28:13 +00:00
feat(core/embed): prepare usb driver for low power mode
[no changelog]
This commit is contained in:
parent
a705335fe1
commit
baff1691a0
@ -122,11 +122,11 @@ static void usb_init_all(secbool usb21_landing) {
|
|||||||
.polling_interval = 1,
|
.polling_interval = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
usb_init(&dev_info);
|
ensure(usb_init(&dev_info), NULL);
|
||||||
|
|
||||||
ensure(usb_webusb_add(&webusb_info), NULL);
|
ensure(usb_webusb_add(&webusb_info), NULL);
|
||||||
|
|
||||||
usb_start();
|
ensure(usb_start(), NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static usb_result_t bootloader_usb_loop(const vendor_header *const vhdr,
|
static usb_result_t bootloader_usb_loop(const vendor_header *const vhdr,
|
||||||
|
@ -78,11 +78,11 @@ static void usb_init_all(secbool usb21_landing) {
|
|||||||
.polling_interval = 1,
|
.polling_interval = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
usb_init(&dev_info);
|
ensure(usb_init(&dev_info), NULL);
|
||||||
|
|
||||||
ensure(usb_webusb_add(&webusb_info), NULL);
|
ensure(usb_webusb_add(&webusb_info), NULL);
|
||||||
|
|
||||||
usb_start();
|
ensure(usb_start(), NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static secbool bootloader_usb_loop(const vendor_header *const vhdr,
|
static secbool bootloader_usb_loop(const vendor_header *const vhdr,
|
||||||
|
@ -190,7 +190,9 @@ STATIC mp_obj_t mod_trezorio_USB_open(mp_obj_t self,
|
|||||||
mp_obj_get_array(MP_OBJ_FROM_PTR(&o->ifaces), &iface_cnt, &iface_objs);
|
mp_obj_get_array(MP_OBJ_FROM_PTR(&o->ifaces), &iface_cnt, &iface_objs);
|
||||||
|
|
||||||
// Initialize the USB stack
|
// Initialize the USB stack
|
||||||
usb_init(&o->info);
|
if (sectrue != usb_init(&o->info)) {
|
||||||
|
mp_raise_msg(&mp_type_RuntimeError, "failed to initialize usb driver");
|
||||||
|
}
|
||||||
|
|
||||||
int vcp_iface_num = -1;
|
int vcp_iface_num = -1;
|
||||||
|
|
||||||
@ -224,7 +226,10 @@ STATIC mp_obj_t mod_trezorio_USB_open(mp_obj_t self,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Start the USB stack
|
// Start the USB stack
|
||||||
usb_start();
|
if (sectrue != usb_start()) {
|
||||||
|
usb_deinit();
|
||||||
|
mp_raise_msg(&mp_type_RuntimeError, "failed to start usb driver");
|
||||||
|
}
|
||||||
o->state = USB_OPENED;
|
o->state = USB_OPENED;
|
||||||
|
|
||||||
// If we found any VCP interfaces, use the last one for stdio,
|
// If we found any VCP interfaces, use the last one for stdio,
|
||||||
|
@ -155,9 +155,9 @@ static void usb_init_all(void) {
|
|||||||
.max_packet_len = VCP_PACKET_LEN,
|
.max_packet_len = VCP_PACKET_LEN,
|
||||||
};
|
};
|
||||||
|
|
||||||
usb_init(&dev_info);
|
ensure(usb_init(&dev_info), NULL);
|
||||||
ensure(usb_vcp_add(&vcp_info), "usb_vcp_add");
|
ensure(usb_vcp_add(&vcp_info), "usb_vcp_add");
|
||||||
usb_start();
|
ensure(usb_start(), NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void draw_border(int width, int padding) {
|
static void draw_border(int width, int padding) {
|
||||||
|
@ -19,12 +19,11 @@
|
|||||||
|
|
||||||
#include STM32_HAL_H
|
#include STM32_HAL_H
|
||||||
|
|
||||||
|
#include "usb.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "random_delays.h"
|
#include "random_delays.h"
|
||||||
#include "secbool.h"
|
#include "secbool.h"
|
||||||
#include "usb.h"
|
#include "usb_internal.h"
|
||||||
#include "usbd_core.h"
|
|
||||||
#include "usbd_internal.h"
|
|
||||||
|
|
||||||
#define USB_MAX_CONFIG_DESC_SIZE 256
|
#define USB_MAX_CONFIG_DESC_SIZE 256
|
||||||
#define USB_MAX_STR_SIZE 62
|
#define USB_MAX_STR_SIZE 62
|
||||||
@ -55,6 +54,8 @@ typedef struct {
|
|||||||
} usb_iface_t;
|
} usb_iface_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
// Set if the driver is initialized
|
||||||
|
secbool initialized;
|
||||||
// Handle to the USB device (lower layer driver)
|
// Handle to the USB device (lower layer driver)
|
||||||
USBD_HandleTypeDef dev_handle;
|
USBD_HandleTypeDef dev_handle;
|
||||||
// Device descriptor
|
// Device descriptor
|
||||||
@ -75,10 +76,17 @@ typedef struct {
|
|||||||
secbool usb21_enabled;
|
secbool usb21_enabled;
|
||||||
secbool usb21_landing;
|
secbool usb21_landing;
|
||||||
|
|
||||||
|
// Time (in ticks) when we've seen the USB ready last time
|
||||||
|
uint32_t ready_time;
|
||||||
|
// Set to `sectrue` if the USB stack was ready sinced the last start
|
||||||
|
secbool was_ready;
|
||||||
|
|
||||||
} usb_driver_t;
|
} usb_driver_t;
|
||||||
|
|
||||||
// USB driver instance
|
// USB driver instance
|
||||||
static usb_driver_t g_usb_driver;
|
static usb_driver_t g_usb_driver = {
|
||||||
|
.initialized = secfalse,
|
||||||
|
};
|
||||||
|
|
||||||
// forward declarations of dispatch functions
|
// forward declarations of dispatch functions
|
||||||
static const USBD_ClassTypeDef usb_class;
|
static const USBD_ClassTypeDef usb_class;
|
||||||
@ -90,126 +98,198 @@ static secbool __wur check_desc_str(const char *s) {
|
|||||||
return sectrue;
|
return sectrue;
|
||||||
}
|
}
|
||||||
|
|
||||||
void usb_init(const usb_dev_info_t *dev_info) {
|
secbool usb_init(const usb_dev_info_t *dev_info) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
|
if (drv->initialized == sectrue) {
|
||||||
|
// Already initialized
|
||||||
|
return sectrue;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(drv, 0, sizeof(usb_driver_t));
|
||||||
|
|
||||||
// enable/disable USB 2.1 features
|
// enable/disable USB 2.1 features
|
||||||
usb->usb21_enabled = dev_info->usb21_enabled;
|
drv->usb21_enabled = dev_info->usb21_enabled;
|
||||||
usb->usb21_landing = dev_info->usb21_landing;
|
drv->usb21_landing = dev_info->usb21_landing;
|
||||||
|
|
||||||
// Device descriptor
|
// Device descriptor
|
||||||
usb->dev_desc.bLength = sizeof(usb_device_descriptor_t);
|
drv->dev_desc.bLength = sizeof(usb_device_descriptor_t);
|
||||||
usb->dev_desc.bDescriptorType = USB_DESC_TYPE_DEVICE;
|
drv->dev_desc.bDescriptorType = USB_DESC_TYPE_DEVICE;
|
||||||
usb->dev_desc.bcdUSB =
|
drv->dev_desc.bcdUSB =
|
||||||
(sectrue == usb->usb21_enabled) ? 0x0210 : 0x0200; // USB 2.1 or USB 2.0
|
(sectrue == drv->usb21_enabled) ? 0x0210 : 0x0200; // USB 2.1 or USB 2.0
|
||||||
usb->dev_desc.bDeviceClass = dev_info->device_class;
|
drv->dev_desc.bDeviceClass = dev_info->device_class;
|
||||||
usb->dev_desc.bDeviceSubClass = dev_info->device_subclass;
|
drv->dev_desc.bDeviceSubClass = dev_info->device_subclass;
|
||||||
usb->dev_desc.bDeviceProtocol = dev_info->device_protocol;
|
drv->dev_desc.bDeviceProtocol = dev_info->device_protocol;
|
||||||
usb->dev_desc.bMaxPacketSize0 = USB_MAX_EP0_SIZE;
|
drv->dev_desc.bMaxPacketSize0 = USB_MAX_EP0_SIZE;
|
||||||
usb->dev_desc.idVendor = dev_info->vendor_id;
|
drv->dev_desc.idVendor = dev_info->vendor_id;
|
||||||
usb->dev_desc.idProduct = dev_info->product_id;
|
drv->dev_desc.idProduct = dev_info->product_id;
|
||||||
usb->dev_desc.bcdDevice = dev_info->release_num;
|
drv->dev_desc.bcdDevice = dev_info->release_num;
|
||||||
usb->dev_desc.iManufacturer =
|
drv->dev_desc.iManufacturer =
|
||||||
USBD_IDX_MFC_STR; // Index of manufacturer string
|
USBD_IDX_MFC_STR; // Index of manufacturer string
|
||||||
usb->dev_desc.iProduct = USBD_IDX_PRODUCT_STR; // Index of product string
|
drv->dev_desc.iProduct = USBD_IDX_PRODUCT_STR; // Index of product string
|
||||||
usb->dev_desc.iSerialNumber =
|
drv->dev_desc.iSerialNumber =
|
||||||
USBD_IDX_SERIAL_STR; // Index of serial number string
|
USBD_IDX_SERIAL_STR; // Index of serial number string
|
||||||
usb->dev_desc.bNumConfigurations = 1;
|
drv->dev_desc.bNumConfigurations = 1;
|
||||||
|
|
||||||
// String table
|
// String table
|
||||||
ensure(check_desc_str(dev_info->manufacturer), NULL);
|
if (sectrue != check_desc_str(dev_info->manufacturer)) {
|
||||||
ensure(check_desc_str(dev_info->product), NULL);
|
return secfalse;
|
||||||
ensure(check_desc_str(dev_info->serial_number), NULL);
|
}
|
||||||
ensure(check_desc_str(dev_info->interface), NULL);
|
if (sectrue != check_desc_str(dev_info->product)) {
|
||||||
|
return secfalse;
|
||||||
|
}
|
||||||
|
if (sectrue != check_desc_str(dev_info->serial_number)) {
|
||||||
|
return secfalse;
|
||||||
|
}
|
||||||
|
if (sectrue != check_desc_str(dev_info->interface)) {
|
||||||
|
return secfalse;
|
||||||
|
}
|
||||||
|
|
||||||
usb->str_table.manufacturer = dev_info->manufacturer;
|
drv->str_table.manufacturer = dev_info->manufacturer;
|
||||||
usb->str_table.product = dev_info->product;
|
drv->str_table.product = dev_info->product;
|
||||||
usb->str_table.serial_number = dev_info->serial_number;
|
drv->str_table.serial_number = dev_info->serial_number;
|
||||||
usb->str_table.interface = dev_info->interface;
|
drv->str_table.interface = dev_info->interface;
|
||||||
|
|
||||||
usb->config_desc = (usb_config_descriptor_t *)(usb->desc_buffer);
|
drv->config_desc = (usb_config_descriptor_t *)(drv->desc_buffer);
|
||||||
|
|
||||||
// Configuration descriptor
|
// Configuration descriptor
|
||||||
usb->config_desc->bLength = sizeof(usb_config_descriptor_t);
|
drv->config_desc->bLength = sizeof(usb_config_descriptor_t);
|
||||||
usb->config_desc->bDescriptorType = USB_DESC_TYPE_CONFIGURATION;
|
drv->config_desc->bDescriptorType = USB_DESC_TYPE_CONFIGURATION;
|
||||||
usb->config_desc->wTotalLength =
|
drv->config_desc->wTotalLength =
|
||||||
sizeof(usb_config_descriptor_t); // will be updated later via
|
sizeof(usb_config_descriptor_t); // will be updated later via
|
||||||
// usb_alloc_class_descriptors()
|
// usb_alloc_class_descriptors()
|
||||||
usb->config_desc->bNumInterfaces =
|
drv->config_desc->bNumInterfaces =
|
||||||
0; // will be updated later via usb_set_iface_class()
|
0; // will be updated later via usb_set_iface_class()
|
||||||
usb->config_desc->bConfigurationValue = 0x01;
|
drv->config_desc->bConfigurationValue = 0x01;
|
||||||
usb->config_desc->iConfiguration = 0;
|
drv->config_desc->iConfiguration = 0;
|
||||||
usb->config_desc->bmAttributes =
|
drv->config_desc->bmAttributes =
|
||||||
0x80; // 0x80 = bus powered; 0xC0 = self powered
|
0x80; // 0x80 = bus powered; 0xC0 = self powered
|
||||||
usb->config_desc->bMaxPower = 0x32; // Maximum Power Consumption in 2mA units
|
drv->config_desc->bMaxPower = 0x32; // Maximum Power Consumption in 2mA units
|
||||||
|
|
||||||
ensure(sectrue *
|
drv->initialized = sectrue;
|
||||||
(USBD_OK == USBD_Init(&usb->dev_handle,
|
|
||||||
(USBD_DescriptorsTypeDef *)&usb_descriptors,
|
return sectrue;
|
||||||
USB_PHY_ID)),
|
|
||||||
NULL);
|
|
||||||
ensure(sectrue *
|
|
||||||
(USBD_OK == USBD_RegisterClass(&usb->dev_handle,
|
|
||||||
(USBD_ClassTypeDef *)&usb_class)),
|
|
||||||
NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void usb_deinit(void) {
|
void usb_deinit(void) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
USBD_DeInit(&usb->dev_handle);
|
if (drv->initialized != sectrue) {
|
||||||
for (int i = 0; i < USBD_MAX_NUM_INTERFACES; i++) {
|
return;
|
||||||
usb->ifaces[i].class = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
usb_stop();
|
||||||
|
|
||||||
|
drv->initialized = secfalse;
|
||||||
}
|
}
|
||||||
|
|
||||||
void usb_start(void) {
|
secbool usb_start(void) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
USBD_Start(&usb->dev_handle);
|
if (drv->initialized != sectrue) {
|
||||||
|
// The driver is not initialized
|
||||||
|
return secfalse;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (drv->dev_handle.dev_state != USBD_STATE_UNINITIALIZED) {
|
||||||
|
// The driver has been started already
|
||||||
|
return sectrue;
|
||||||
|
}
|
||||||
|
|
||||||
|
drv->was_ready = secfalse;
|
||||||
|
|
||||||
|
if (USBD_OK != USBD_Init(&drv->dev_handle,
|
||||||
|
(USBD_DescriptorsTypeDef *)&usb_descriptors,
|
||||||
|
USB_PHY_ID)) {
|
||||||
|
usb_stop();
|
||||||
|
return secfalse;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (USBD_OK !=
|
||||||
|
USBD_RegisterClass(&drv->dev_handle, (USBD_ClassTypeDef *)&usb_class)) {
|
||||||
|
usb_stop();
|
||||||
|
return secfalse;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (USBD_OK != USBD_Start(&drv->dev_handle)) {
|
||||||
|
usb_stop();
|
||||||
|
return secfalse;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sectrue;
|
||||||
}
|
}
|
||||||
|
|
||||||
void usb_stop(void) {
|
void usb_stop(void) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
USBD_Stop(&usb->dev_handle);
|
if (drv->initialized != sectrue) {
|
||||||
|
// The driver is not initialized
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (drv->dev_handle.dev_state == USBD_STATE_UNINITIALIZED) {
|
||||||
|
// The driver is already stopped
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
USBD_DeInit(&drv->dev_handle);
|
||||||
|
|
||||||
|
// Set drv->dev_handle.dev_state to USBD_STATE_INITIALIZED
|
||||||
|
memset(&drv->dev_handle, 0, sizeof(drv->dev_handle));
|
||||||
}
|
}
|
||||||
|
|
||||||
secbool usb_configured(void) {
|
secbool usb_configured(void) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
|
if (drv->initialized != sectrue) {
|
||||||
|
// The driver is not initialized
|
||||||
|
return secfalse;
|
||||||
|
}
|
||||||
|
|
||||||
|
const USBD_HandleTypeDef *pdev = &drv->dev_handle;
|
||||||
|
|
||||||
|
if (pdev->dev_state == USBD_STATE_UNINITIALIZED) {
|
||||||
|
// The driver has not been started yet
|
||||||
|
return secfalse;
|
||||||
|
}
|
||||||
|
|
||||||
|
secbool powered_from_usb = sectrue; // TODO
|
||||||
|
|
||||||
|
secbool ready = secfalse;
|
||||||
|
|
||||||
|
if (pdev->dev_state == USBD_STATE_CONFIGURED) {
|
||||||
|
// USB is configured, ready to transfer data
|
||||||
|
ready = sectrue;
|
||||||
|
} else if (pdev->dev_state == USBD_STATE_SUSPENDED &&
|
||||||
|
pdev->dev_old_state == USBD_STATE_CONFIGURED) {
|
||||||
|
// USB is suspended, but was configured before
|
||||||
|
//
|
||||||
|
// Linux has autosuspend device after 2 seconds by default.
|
||||||
|
// So a suspended device that was seen as configured is reported as
|
||||||
|
// configured.
|
||||||
|
//
|
||||||
|
ready = sectrue;
|
||||||
|
} else if ((drv->was_ready == secfalse) && (powered_from_usb == sectrue)) {
|
||||||
|
// First run after the startup with USB power
|
||||||
|
drv->was_ready = sectrue;
|
||||||
|
ready = sectrue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This is a workaround to handle the glitches in the USB connection,
|
||||||
|
// especially for USB-powered-only devices. This should be
|
||||||
|
// revisited and probably fixed elsewhere.
|
||||||
|
|
||||||
static uint32_t usb_configured_last_ok = 0;
|
|
||||||
uint32_t ticks = hal_ticks_ms();
|
uint32_t ticks = hal_ticks_ms();
|
||||||
|
|
||||||
const USBD_HandleTypeDef *pdev = &usb->dev_handle;
|
if (ready == sectrue) {
|
||||||
if (pdev->dev_state == USBD_STATE_CONFIGURED) {
|
drv->ready_time = ticks;
|
||||||
usb_configured_last_ok = hal_ticks_ms();
|
} else if ((drv->was_ready == sectrue) && (ticks - drv->ready_time) < 2000) {
|
||||||
return sectrue;
|
// NOTE: When the timer overflows the timeout is shortened.
|
||||||
|
// We are ignoring it for now.
|
||||||
|
ready = sectrue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Linux has autosuspend device after 2 seconds by default.
|
return ready;
|
||||||
// So a suspended device that was seen as configured is reported as
|
|
||||||
// configured.
|
|
||||||
if (pdev->dev_state == USBD_STATE_SUSPENDED &&
|
|
||||||
pdev->dev_old_state == USBD_STATE_CONFIGURED) {
|
|
||||||
usb_configured_last_ok = hal_ticks_ms();
|
|
||||||
return sectrue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (usb_configured_last_ok == 0) {
|
|
||||||
usb_configured_last_ok = ticks;
|
|
||||||
return sectrue;
|
|
||||||
}
|
|
||||||
if (usb_configured_last_ok > ticks) {
|
|
||||||
// probably overflow of 32bit ms counter, ignore as its just once in a long
|
|
||||||
// time
|
|
||||||
return sectrue;
|
|
||||||
}
|
|
||||||
if ((hal_ticks_ms() - usb_configured_last_ok) < 2000) {
|
|
||||||
return sectrue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return secfalse;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ==========================================================================
|
// ==========================================================================
|
||||||
@ -217,10 +297,10 @@ secbool usb_configured(void) {
|
|||||||
// ==========================================================================
|
// ==========================================================================
|
||||||
|
|
||||||
void *usb_get_iface_state(uint8_t iface_num, const USBD_ClassTypeDef *class) {
|
void *usb_get_iface_state(uint8_t iface_num, const USBD_ClassTypeDef *class) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
if (iface_num < USBD_MAX_NUM_INTERFACES) {
|
if (iface_num < USBD_MAX_NUM_INTERFACES) {
|
||||||
usb_iface_t *iface = &usb->ifaces[iface_num];
|
usb_iface_t *iface = &drv->ifaces[iface_num];
|
||||||
|
|
||||||
if (iface->class == class) {
|
if (iface->class == class) {
|
||||||
return &iface->state;
|
return &iface->state;
|
||||||
@ -232,14 +312,14 @@ void *usb_get_iface_state(uint8_t iface_num, const USBD_ClassTypeDef *class) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void usb_set_iface_class(uint8_t iface_num, const USBD_ClassTypeDef *class) {
|
void usb_set_iface_class(uint8_t iface_num, const USBD_ClassTypeDef *class) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
if (iface_num < USBD_MAX_NUM_INTERFACES) {
|
if (iface_num < USBD_MAX_NUM_INTERFACES) {
|
||||||
if (usb->ifaces[iface_num].class == NULL && class != NULL) {
|
if (drv->ifaces[iface_num].class == NULL && class != NULL) {
|
||||||
usb->config_desc->bNumInterfaces++;
|
drv->config_desc->bNumInterfaces++;
|
||||||
}
|
}
|
||||||
|
|
||||||
usb->ifaces[iface_num].class = class;
|
drv->ifaces[iface_num].class = class;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -252,9 +332,9 @@ USBD_HandleTypeDef *usb_get_dev_handle(void) {
|
|||||||
void *usb_alloc_class_descriptors(size_t desc_len) {
|
void *usb_alloc_class_descriptors(size_t desc_len) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *usb = &g_usb_driver;
|
||||||
|
|
||||||
if (usb->config_desc->wTotalLength + desc_len < USB_MAX_CONFIG_DESC_SIZE) {
|
if (drv->config_desc->wTotalLength + desc_len < USB_MAX_CONFIG_DESC_SIZE) {
|
||||||
void *retval = &usb->desc_buffer[usb->config_desc->wTotalLength];
|
void *retval = &drv->desc_buffer[drv->config_desc->wTotalLength];
|
||||||
usb->config_desc->wTotalLength += desc_len;
|
drv->config_desc->wTotalLength += desc_len;
|
||||||
return retval;
|
return retval;
|
||||||
} else {
|
} else {
|
||||||
return NULL; // Not enough space in the descriptor
|
return NULL; // Not enough space in the descriptor
|
||||||
@ -267,10 +347,10 @@ void *usb_alloc_class_descriptors(size_t desc_len) {
|
|||||||
|
|
||||||
static uint8_t *usb_get_dev_descriptor(USBD_SpeedTypeDef speed,
|
static uint8_t *usb_get_dev_descriptor(USBD_SpeedTypeDef speed,
|
||||||
uint16_t *length) {
|
uint16_t *length) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
*length = sizeof(usb->dev_desc);
|
*length = sizeof(drv->dev_desc);
|
||||||
return (uint8_t *)(&usb->dev_desc);
|
return (uint8_t *)(&drv->dev_desc);
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t *usb_get_langid_str_descriptor(USBD_SpeedTypeDef speed,
|
static uint8_t *usb_get_langid_str_descriptor(USBD_SpeedTypeDef speed,
|
||||||
@ -286,49 +366,49 @@ static uint8_t *usb_get_langid_str_descriptor(USBD_SpeedTypeDef speed,
|
|||||||
|
|
||||||
static uint8_t *usb_get_manufacturer_str_descriptor(USBD_SpeedTypeDef speed,
|
static uint8_t *usb_get_manufacturer_str_descriptor(USBD_SpeedTypeDef speed,
|
||||||
uint16_t *length) {
|
uint16_t *length) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
USBD_GetString((uint8_t *)usb->str_table.manufacturer, usb->str_buf, length);
|
USBD_GetString((uint8_t *)drv->str_table.manufacturer, drv->str_buf, length);
|
||||||
return usb->str_buf;
|
return drv->str_buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t *usb_get_product_str_descriptor(USBD_SpeedTypeDef speed,
|
static uint8_t *usb_get_product_str_descriptor(USBD_SpeedTypeDef speed,
|
||||||
uint16_t *length) {
|
uint16_t *length) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
USBD_GetString((uint8_t *)usb->str_table.product, usb->str_buf, length);
|
USBD_GetString((uint8_t *)drv->str_table.product, drv->str_buf, length);
|
||||||
return usb->str_buf;
|
return drv->str_buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t *usb_get_serial_str_descriptor(USBD_SpeedTypeDef speed,
|
static uint8_t *usb_get_serial_str_descriptor(USBD_SpeedTypeDef speed,
|
||||||
uint16_t *length) {
|
uint16_t *length) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
USBD_GetString((uint8_t *)usb->str_table.serial_number, usb->str_buf, length);
|
USBD_GetString((uint8_t *)drv->str_table.serial_number, drv->str_buf, length);
|
||||||
return usb->str_buf;
|
return drv->str_buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t *usb_get_configuration_str_descriptor(USBD_SpeedTypeDef speed,
|
static uint8_t *usb_get_configuration_str_descriptor(USBD_SpeedTypeDef speed,
|
||||||
uint16_t *length) {
|
uint16_t *length) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
USBD_GetString((uint8_t *)"", usb->str_buf, length);
|
USBD_GetString((uint8_t *)"", drv->str_buf, length);
|
||||||
return usb->str_buf;
|
return drv->str_buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t *usb_get_interface_str_descriptor(USBD_SpeedTypeDef speed,
|
static uint8_t *usb_get_interface_str_descriptor(USBD_SpeedTypeDef speed,
|
||||||
uint16_t *length) {
|
uint16_t *length) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
USBD_GetString((uint8_t *)usb->str_table.interface, usb->str_buf, length);
|
USBD_GetString((uint8_t *)drv->str_table.interface, drv->str_buf, length);
|
||||||
return usb->str_buf;
|
return drv->str_buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t *usb_get_bos_descriptor(USBD_SpeedTypeDef speed,
|
static uint8_t *usb_get_bos_descriptor(USBD_SpeedTypeDef speed,
|
||||||
uint16_t *length) {
|
uint16_t *length) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
if (sectrue == usb->usb21_enabled) {
|
if (sectrue == drv->usb21_enabled) {
|
||||||
static uint8_t bos[] = {
|
static uint8_t bos[] = {
|
||||||
// usb_bos_descriptor {
|
// usb_bos_descriptor {
|
||||||
0x05, // uint8_t bLength
|
0x05, // uint8_t bLength
|
||||||
@ -348,7 +428,7 @@ static uint8_t *usb_get_bos_descriptor(USBD_SpeedTypeDef speed,
|
|||||||
USB_WEBUSB_LANDING_PAGE, // uint8_t iLandingPage
|
USB_WEBUSB_LANDING_PAGE, // uint8_t iLandingPage
|
||||||
// }
|
// }
|
||||||
};
|
};
|
||||||
bos[28] = (sectrue == usb->usb21_landing) ? USB_WEBUSB_LANDING_PAGE : 0;
|
bos[28] = (sectrue == drv->usb21_landing) ? USB_WEBUSB_LANDING_PAGE : 0;
|
||||||
*length = sizeof(bos);
|
*length = sizeof(bos);
|
||||||
return UNCONST(bos);
|
return UNCONST(bos);
|
||||||
} else {
|
} else {
|
||||||
@ -383,10 +463,10 @@ static const USBD_DescriptorsTypeDef usb_descriptors = {
|
|||||||
#define USB_WINUSB_REQ_GET_EXTENDED_PROPERTIES_OS_FEATURE_DESCRIPTOR 0x05
|
#define USB_WINUSB_REQ_GET_EXTENDED_PROPERTIES_OS_FEATURE_DESCRIPTOR 0x05
|
||||||
|
|
||||||
static uint8_t usb_class_init(USBD_HandleTypeDef *dev, uint8_t cfg_idx) {
|
static uint8_t usb_class_init(USBD_HandleTypeDef *dev, uint8_t cfg_idx) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
for (int i = 0; i < USBD_MAX_NUM_INTERFACES; i++) {
|
for (int i = 0; i < USBD_MAX_NUM_INTERFACES; i++) {
|
||||||
usb_iface_t *iface = &usb->ifaces[i];
|
usb_iface_t *iface = &drv->ifaces[i];
|
||||||
if (iface->class != NULL && iface->class->Init != NULL) {
|
if (iface->class != NULL && iface->class->Init != NULL) {
|
||||||
dev->pUserData = iface->state;
|
dev->pUserData = iface->state;
|
||||||
iface->class->Init(dev, cfg_idx);
|
iface->class->Init(dev, cfg_idx);
|
||||||
@ -399,10 +479,10 @@ static uint8_t usb_class_init(USBD_HandleTypeDef *dev, uint8_t cfg_idx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t usb_class_deinit(USBD_HandleTypeDef *dev, uint8_t cfg_idx) {
|
static uint8_t usb_class_deinit(USBD_HandleTypeDef *dev, uint8_t cfg_idx) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
for (int i = 0; i < USBD_MAX_NUM_INTERFACES; i++) {
|
for (int i = 0; i < USBD_MAX_NUM_INTERFACES; i++) {
|
||||||
usb_iface_t *iface = &usb->ifaces[i];
|
usb_iface_t *iface = &drv->ifaces[i];
|
||||||
if (iface->class != NULL && iface->class->DeInit != NULL) {
|
if (iface->class != NULL && iface->class->DeInit != NULL) {
|
||||||
dev->pUserData = iface->state;
|
dev->pUserData = iface->state;
|
||||||
iface->class->DeInit(dev, cfg_idx);
|
iface->class->DeInit(dev, cfg_idx);
|
||||||
@ -421,7 +501,7 @@ static uint8_t usb_class_deinit(USBD_HandleTypeDef *dev, uint8_t cfg_idx) {
|
|||||||
|
|
||||||
static uint8_t usb_class_setup(USBD_HandleTypeDef *dev,
|
static uint8_t usb_class_setup(USBD_HandleTypeDef *dev,
|
||||||
USBD_SetupReqTypedef *req) {
|
USBD_SetupReqTypedef *req) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
if (((req->bmRequest & USB_REQ_TYPE_MASK) != USB_REQ_TYPE_CLASS) &&
|
if (((req->bmRequest & USB_REQ_TYPE_MASK) != USB_REQ_TYPE_CLASS) &&
|
||||||
((req->bmRequest & USB_REQ_TYPE_MASK) != USB_REQ_TYPE_STANDARD) &&
|
((req->bmRequest & USB_REQ_TYPE_MASK) != USB_REQ_TYPE_STANDARD) &&
|
||||||
@ -431,7 +511,7 @@ static uint8_t usb_class_setup(USBD_HandleTypeDef *dev,
|
|||||||
|
|
||||||
if ((req->bmRequest & USB_REQ_TYPE_MASK) == USB_REQ_TYPE_VENDOR) {
|
if ((req->bmRequest & USB_REQ_TYPE_MASK) == USB_REQ_TYPE_VENDOR) {
|
||||||
if ((req->bmRequest & USB_REQ_RECIPIENT_MASK) == USB_REQ_RECIPIENT_DEVICE) {
|
if ((req->bmRequest & USB_REQ_RECIPIENT_MASK) == USB_REQ_RECIPIENT_DEVICE) {
|
||||||
if (sectrue == usb->usb21_enabled &&
|
if (sectrue == drv->usb21_enabled &&
|
||||||
req->bRequest == USB_WEBUSB_VENDOR_CODE) {
|
req->bRequest == USB_WEBUSB_VENDOR_CODE) {
|
||||||
if (req->wIndex == USB_WEBUSB_REQ_GET_URL &&
|
if (req->wIndex == USB_WEBUSB_REQ_GET_URL &&
|
||||||
req->wValue == USB_WEBUSB_LANDING_PAGE) {
|
req->wValue == USB_WEBUSB_LANDING_PAGE) {
|
||||||
@ -464,7 +544,7 @@ static uint8_t usb_class_setup(USBD_HandleTypeDef *dev,
|
|||||||
USBD_CtlError(dev, req);
|
USBD_CtlError(dev, req);
|
||||||
return USBD_FAIL;
|
return USBD_FAIL;
|
||||||
}
|
}
|
||||||
} else if (sectrue == usb->usb21_enabled &&
|
} else if (sectrue == drv->usb21_enabled &&
|
||||||
req->bRequest == USB_WINUSB_VENDOR_CODE) {
|
req->bRequest == USB_WINUSB_VENDOR_CODE) {
|
||||||
if (req->wIndex ==
|
if (req->wIndex ==
|
||||||
USB_WINUSB_REQ_GET_COMPATIBLE_ID_FEATURE_DESCRIPTOR) {
|
USB_WINUSB_REQ_GET_COMPATIBLE_ID_FEATURE_DESCRIPTOR) {
|
||||||
@ -497,7 +577,7 @@ static uint8_t usb_class_setup(USBD_HandleTypeDef *dev,
|
|||||||
}
|
}
|
||||||
if ((req->bmRequest & USB_REQ_RECIPIENT_MASK) ==
|
if ((req->bmRequest & USB_REQ_RECIPIENT_MASK) ==
|
||||||
USB_REQ_RECIPIENT_INTERFACE) {
|
USB_REQ_RECIPIENT_INTERFACE) {
|
||||||
if (sectrue == usb->usb21_enabled &&
|
if (sectrue == drv->usb21_enabled &&
|
||||||
req->bRequest == USB_WINUSB_VENDOR_CODE) {
|
req->bRequest == USB_WINUSB_VENDOR_CODE) {
|
||||||
if (req->wIndex ==
|
if (req->wIndex ==
|
||||||
USB_WINUSB_REQ_GET_EXTENDED_PROPERTIES_OS_FEATURE_DESCRIPTOR &&
|
USB_WINUSB_REQ_GET_EXTENDED_PROPERTIES_OS_FEATURE_DESCRIPTOR &&
|
||||||
@ -544,7 +624,7 @@ static uint8_t usb_class_setup(USBD_HandleTypeDef *dev,
|
|||||||
return USBD_FAIL;
|
return USBD_FAIL;
|
||||||
}
|
}
|
||||||
|
|
||||||
usb_iface_t *iface = &usb->ifaces[req->wIndex];
|
usb_iface_t *iface = &drv->ifaces[req->wIndex];
|
||||||
if (iface->class != NULL && iface->class->Setup != NULL) {
|
if (iface->class != NULL && iface->class->Setup != NULL) {
|
||||||
dev->pUserData = iface->state;
|
dev->pUserData = iface->state;
|
||||||
iface->class->Setup(dev, req);
|
iface->class->Setup(dev, req);
|
||||||
@ -559,14 +639,14 @@ static uint8_t usb_class_setup(USBD_HandleTypeDef *dev,
|
|||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t usb_class_data_in(USBD_HandleTypeDef *dev, uint8_t ep_num) {
|
static uint8_t usb_class_data_in(USBD_HandleTypeDef *dev, uint8_t ep_num) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
#ifdef RDI
|
#ifdef RDI
|
||||||
rdi_refresh_session_delay();
|
rdi_refresh_session_delay();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
for (int i = 0; i < USBD_MAX_NUM_INTERFACES; i++) {
|
for (int i = 0; i < USBD_MAX_NUM_INTERFACES; i++) {
|
||||||
usb_iface_t *iface = &usb->ifaces[i];
|
usb_iface_t *iface = &drv->ifaces[i];
|
||||||
if (iface->class != NULL && iface->class->DataIn != NULL) {
|
if (iface->class != NULL && iface->class->DataIn != NULL) {
|
||||||
dev->pUserData = iface->state;
|
dev->pUserData = iface->state;
|
||||||
iface->class->DataIn(dev, ep_num);
|
iface->class->DataIn(dev, ep_num);
|
||||||
@ -579,14 +659,14 @@ static uint8_t usb_class_data_in(USBD_HandleTypeDef *dev, uint8_t ep_num) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t usb_class_data_out(USBD_HandleTypeDef *dev, uint8_t ep_num) {
|
static uint8_t usb_class_data_out(USBD_HandleTypeDef *dev, uint8_t ep_num) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
#ifdef RDI
|
#ifdef RDI
|
||||||
rdi_refresh_session_delay();
|
rdi_refresh_session_delay();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
for (int i = 0; i < USBD_MAX_NUM_INTERFACES; i++) {
|
for (int i = 0; i < USBD_MAX_NUM_INTERFACES; i++) {
|
||||||
usb_iface_t *iface = &usb->ifaces[i];
|
usb_iface_t *iface = &drv->ifaces[i];
|
||||||
if (iface->class != NULL && iface->class->DataOut != NULL) {
|
if (iface->class != NULL && iface->class->DataOut != NULL) {
|
||||||
dev->pUserData = iface->state;
|
dev->pUserData = iface->state;
|
||||||
iface->class->DataOut(dev, ep_num);
|
iface->class->DataOut(dev, ep_num);
|
||||||
@ -599,10 +679,10 @@ static uint8_t usb_class_data_out(USBD_HandleTypeDef *dev, uint8_t ep_num) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t usb_class_sof(USBD_HandleTypeDef *dev) {
|
static uint8_t usb_class_sof(USBD_HandleTypeDef *dev) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
for (int i = 0; i < USBD_MAX_NUM_INTERFACES; i++) {
|
for (int i = 0; i < USBD_MAX_NUM_INTERFACES; i++) {
|
||||||
usb_iface_t *iface = &usb->ifaces[i];
|
usb_iface_t *iface = &drv->ifaces[i];
|
||||||
if (iface->class != NULL && iface->class->SOF != NULL) {
|
if (iface->class != NULL && iface->class->SOF != NULL) {
|
||||||
dev->pUserData = iface->state;
|
dev->pUserData = iface->state;
|
||||||
iface->class->SOF(dev);
|
iface->class->SOF(dev);
|
||||||
@ -615,17 +695,17 @@ static uint8_t usb_class_sof(USBD_HandleTypeDef *dev) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t *usb_class_get_cfg_desc(uint16_t *length) {
|
static uint8_t *usb_class_get_cfg_desc(uint16_t *length) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
*length = usb->config_desc->wTotalLength;
|
*length = drv->config_desc->wTotalLength;
|
||||||
return usb->desc_buffer;
|
return drv->desc_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t *usb_class_get_usrstr_desc(USBD_HandleTypeDef *dev,
|
static uint8_t *usb_class_get_usrstr_desc(USBD_HandleTypeDef *dev,
|
||||||
uint8_t index, uint16_t *length) {
|
uint8_t index, uint16_t *length) {
|
||||||
usb_driver_t *usb = &g_usb_driver;
|
usb_driver_t *drv = &g_usb_driver;
|
||||||
|
|
||||||
if (sectrue == usb->usb21_enabled && index == USB_WINUSB_EXTRA_STRING_INDEX) {
|
if (sectrue == drv->usb21_enabled && index == USB_WINUSB_EXTRA_STRING_INDEX) {
|
||||||
static const uint8_t winusb_string_descriptor[] = {
|
static const uint8_t winusb_string_descriptor[] = {
|
||||||
0x12, // bLength
|
0x12, // bLength
|
||||||
USB_DESC_TYPE_STRING, // bDescriptorType
|
USB_DESC_TYPE_STRING, // bDescriptorType
|
@ -20,10 +20,8 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "random_delays.h"
|
#include "random_delays.h"
|
||||||
|
|
||||||
#include "usbd_core.h"
|
|
||||||
#include "usbd_internal.h"
|
|
||||||
|
|
||||||
#include "usb_hid.h"
|
#include "usb_hid.h"
|
||||||
|
#include "usb_internal.h"
|
||||||
|
|
||||||
#define USB_CLASS_HID 0x03
|
#define USB_CLASS_HID 0x03
|
||||||
|
|
@ -19,9 +19,7 @@
|
|||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
#include "usbd_core.h"
|
#include "usb_internal.h"
|
||||||
#include "usbd_internal.h"
|
|
||||||
|
|
||||||
#include "usb_vcp.h"
|
#include "usb_vcp.h"
|
||||||
|
|
||||||
// Communications Device Class Code (bFunctionClass, bInterfaceClass)
|
// Communications Device Class Code (bFunctionClass, bInterfaceClass)
|
@ -20,9 +20,7 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "random_delays.h"
|
#include "random_delays.h"
|
||||||
|
|
||||||
#include "usbd_core.h"
|
#include "usb_internal.h"
|
||||||
#include "usbd_internal.h"
|
|
||||||
|
|
||||||
#include "usb_webusb.h"
|
#include "usb_webusb.h"
|
||||||
|
|
||||||
#define USB_CLASS_WEBUSB 0xFF
|
#define USB_CLASS_WEBUSB 0xFF
|
@ -21,6 +21,7 @@
|
|||||||
#define TREZORHAL_USBD_INTERNAL_H
|
#define TREZORHAL_USBD_INTERNAL_H
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include "usbd_core.h"
|
||||||
|
|
||||||
#define USB_EP_DIR_MASK 0x80
|
#define USB_EP_DIR_MASK 0x80
|
||||||
#define USB_EP_DIR_OUT 0x00
|
#define USB_EP_DIR_OUT 0x00
|
@ -118,6 +118,7 @@
|
|||||||
#define USB_MAX_EP0_SIZE 64
|
#define USB_MAX_EP0_SIZE 64
|
||||||
|
|
||||||
/* Device Status */
|
/* Device Status */
|
||||||
|
#define USBD_STATE_UNINITIALIZED 0
|
||||||
#define USBD_STATE_DEFAULT 1
|
#define USBD_STATE_DEFAULT 1
|
||||||
#define USBD_STATE_ADDRESSED 2
|
#define USBD_STATE_ADDRESSED 2
|
||||||
#define USBD_STATE_CONFIGURED 3
|
#define USBD_STATE_CONFIGURED 3
|
||||||
|
@ -53,7 +53,7 @@ static struct {
|
|||||||
socklen_t slen;
|
socklen_t slen;
|
||||||
} usb_ifaces[USBD_MAX_NUM_INTERFACES];
|
} usb_ifaces[USBD_MAX_NUM_INTERFACES];
|
||||||
|
|
||||||
void usb_init(const usb_dev_info_t *dev_info) {
|
secbool usb_init(const usb_dev_info_t *dev_info) {
|
||||||
(void)dev_info;
|
(void)dev_info;
|
||||||
for (int i = 0; i < USBD_MAX_NUM_INTERFACES; i++) {
|
for (int i = 0; i < USBD_MAX_NUM_INTERFACES; i++) {
|
||||||
usb_ifaces[i].type = USB_IFACE_TYPE_DISABLED;
|
usb_ifaces[i].type = USB_IFACE_TYPE_DISABLED;
|
||||||
@ -63,11 +63,12 @@ void usb_init(const usb_dev_info_t *dev_info) {
|
|||||||
memzero(&usb_ifaces[i].si_other, sizeof(struct sockaddr_in));
|
memzero(&usb_ifaces[i].si_other, sizeof(struct sockaddr_in));
|
||||||
usb_ifaces[i].slen = 0;
|
usb_ifaces[i].slen = 0;
|
||||||
}
|
}
|
||||||
|
return sectrue;
|
||||||
}
|
}
|
||||||
|
|
||||||
void usb_deinit(void) {}
|
void usb_deinit(void) {}
|
||||||
|
|
||||||
void usb_start(void) {
|
secbool usb_start(void) {
|
||||||
const char *ip = getenv("TREZOR_UDP_IP");
|
const char *ip = getenv("TREZOR_UDP_IP");
|
||||||
|
|
||||||
// iterate interfaces
|
// iterate interfaces
|
||||||
@ -96,6 +97,8 @@ void usb_start(void) {
|
|||||||
sizeof(struct sockaddr_in))),
|
sizeof(struct sockaddr_in))),
|
||||||
NULL);
|
NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return sectrue;
|
||||||
}
|
}
|
||||||
|
|
||||||
void usb_stop(void) {
|
void usb_stop(void) {
|
||||||
|
@ -27,6 +27,38 @@
|
|||||||
#include "usb_vcp.h"
|
#include "usb_vcp.h"
|
||||||
#include "usb_webusb.h"
|
#include "usb_webusb.h"
|
||||||
|
|
||||||
|
// clang-format off
|
||||||
|
//
|
||||||
|
// USB stack high-level state machine
|
||||||
|
// ------------------------------------
|
||||||
|
//
|
||||||
|
// +---------------+
|
||||||
|
// ----> | Uninitialized | - Stack is completely uninitialized
|
||||||
|
// | +---------------+
|
||||||
|
// | |
|
||||||
|
// | usb_init()
|
||||||
|
// usb_deinit() |
|
||||||
|
// | v
|
||||||
|
// | +---------------+ - Stack is partially initialized
|
||||||
|
// |-----| Initialized | - Ready for class registration
|
||||||
|
// | +---------------+
|
||||||
|
// | |
|
||||||
|
// | N x usb_xxx_add() - Multiple class drivers can be registered
|
||||||
|
// | |
|
||||||
|
// | v
|
||||||
|
// | +---------------+ - Stack is completely initialized
|
||||||
|
// |-----| Stopped | - USB hardware left uninitialized
|
||||||
|
// | +---------------+ - Can go low power at this mode
|
||||||
|
// | | ^
|
||||||
|
// | usb_start() |
|
||||||
|
// | | usb_stop()
|
||||||
|
// | v |
|
||||||
|
// | +---------------+ - USB hardware initialized
|
||||||
|
// ------| Running | - Stack is running if the USB host is connected
|
||||||
|
// +---------------+
|
||||||
|
//
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t device_class;
|
uint8_t device_class;
|
||||||
uint8_t device_subclass;
|
uint8_t device_subclass;
|
||||||
@ -42,10 +74,44 @@ typedef struct {
|
|||||||
secbool usb21_landing;
|
secbool usb21_landing;
|
||||||
} usb_dev_info_t;
|
} usb_dev_info_t;
|
||||||
|
|
||||||
void usb_init(const usb_dev_info_t *dev_info);
|
// 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.
|
||||||
|
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.
|
||||||
void usb_deinit(void);
|
void usb_deinit(void);
|
||||||
void usb_start(void);
|
|
||||||
|
// Starts USB driver and its class drivers
|
||||||
|
//
|
||||||
|
// Initializes the USB stack (and hardware) and starts all registered class
|
||||||
|
// drivers.
|
||||||
|
//
|
||||||
|
// This function can 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);
|
||||||
|
|
||||||
|
// Stops USB driver and its class drivers
|
||||||
|
//
|
||||||
|
// Unitializes 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.
|
||||||
void usb_stop(void);
|
void usb_stop(void);
|
||||||
|
|
||||||
|
// Returns `sectrue` if the device is connected to the host (or is expected to
|
||||||
|
// be)
|
||||||
|
//
|
||||||
|
// TODO: Review and clarify the logic of this function in the future
|
||||||
secbool usb_configured(void);
|
secbool usb_configured(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -76,14 +76,14 @@ def configure(
|
|||||||
|
|
||||||
if "usb" in features_wanted:
|
if "usb" in features_wanted:
|
||||||
sources += [
|
sources += [
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_hid.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_hid.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_vcp.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_vcp.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_webusb.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_webusb.c",
|
||||||
|
"embed/trezorhal/stm32f4/usb/usb.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_conf.c",
|
"embed/trezorhal/stm32f4/usb/usbd_conf.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_core.c",
|
"embed/trezorhal/stm32f4/usb/usbd_core.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_ctlreq.c",
|
"embed/trezorhal/stm32f4/usb/usbd_ctlreq.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_ioreq.c",
|
"embed/trezorhal/stm32f4/usb/usbd_ioreq.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd.c",
|
|
||||||
"vendor/micropython/lib/stm32lib/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c",
|
"vendor/micropython/lib/stm32lib/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c",
|
||||||
]
|
]
|
||||||
features_available.append("usb")
|
features_available.append("usb")
|
||||||
|
@ -82,14 +82,14 @@ def configure(
|
|||||||
|
|
||||||
if "usb" in features_wanted:
|
if "usb" in features_wanted:
|
||||||
sources += [
|
sources += [
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_class_hid.c",
|
"embed/trezorhal/stm32u5/usb/usb_class_hid.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_class_vcp.c",
|
"embed/trezorhal/stm32u5/usb/usb_class_vcp.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_class_webusb.c",
|
"embed/trezorhal/stm32u5/usb/usb_class_webusb.c",
|
||||||
|
"embed/trezorhal/stm32u5/usb/usb.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_conf.c",
|
"embed/trezorhal/stm32u5/usb/usbd_conf.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_core.c",
|
"embed/trezorhal/stm32u5/usb/usbd_core.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_ctlreq.c",
|
"embed/trezorhal/stm32u5/usb/usbd_ctlreq.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_ioreq.c",
|
"embed/trezorhal/stm32u5/usb/usbd_ioreq.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd.c",
|
|
||||||
"vendor/stm32u5xx_hal_driver/Src/stm32u5xx_ll_usb.c",
|
"vendor/stm32u5xx_hal_driver/Src/stm32u5xx_ll_usb.c",
|
||||||
]
|
]
|
||||||
features_available.append("usb")
|
features_available.append("usb")
|
||||||
|
@ -42,14 +42,14 @@ def configure(
|
|||||||
|
|
||||||
if "usb" in features_wanted:
|
if "usb" in features_wanted:
|
||||||
sources += [
|
sources += [
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_hid.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_hid.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_vcp.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_vcp.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_webusb.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_webusb.c",
|
||||||
|
"embed/trezorhal/stm32f4/usb/usb.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_conf.c",
|
"embed/trezorhal/stm32f4/usb/usbd_conf.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_core.c",
|
"embed/trezorhal/stm32f4/usb/usbd_core.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_ctlreq.c",
|
"embed/trezorhal/stm32f4/usb/usbd_ctlreq.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_ioreq.c",
|
"embed/trezorhal/stm32f4/usb/usbd_ioreq.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd.c",
|
|
||||||
"vendor/micropython/lib/stm32lib/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c",
|
"vendor/micropython/lib/stm32lib/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c",
|
||||||
]
|
]
|
||||||
features_available.append("usb")
|
features_available.append("usb")
|
||||||
|
@ -65,14 +65,14 @@ def configure(
|
|||||||
]
|
]
|
||||||
if "usb" in features_wanted:
|
if "usb" in features_wanted:
|
||||||
sources += [
|
sources += [
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_hid.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_hid.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_vcp.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_vcp.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_webusb.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_webusb.c",
|
||||||
|
"embed/trezorhal/stm32f4/usb/usb.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_conf.c",
|
"embed/trezorhal/stm32f4/usb/usbd_conf.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_core.c",
|
"embed/trezorhal/stm32f4/usb/usbd_core.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_ctlreq.c",
|
"embed/trezorhal/stm32f4/usb/usbd_ctlreq.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_ioreq.c",
|
"embed/trezorhal/stm32f4/usb/usbd_ioreq.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd.c",
|
|
||||||
"vendor/micropython/lib/stm32lib/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c",
|
"vendor/micropython/lib/stm32lib/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c",
|
||||||
]
|
]
|
||||||
features_available.append("usb")
|
features_available.append("usb")
|
||||||
|
@ -62,14 +62,14 @@ def configure(
|
|||||||
|
|
||||||
if "usb" in features_wanted:
|
if "usb" in features_wanted:
|
||||||
sources += [
|
sources += [
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_hid.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_hid.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_vcp.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_vcp.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_webusb.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_webusb.c",
|
||||||
|
"embed/trezorhal/stm32f4/usb/usb.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_conf.c",
|
"embed/trezorhal/stm32f4/usb/usbd_conf.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_core.c",
|
"embed/trezorhal/stm32f4/usb/usbd_core.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_ctlreq.c",
|
"embed/trezorhal/stm32f4/usb/usbd_ctlreq.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_ioreq.c",
|
"embed/trezorhal/stm32f4/usb/usbd_ioreq.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd.c",
|
|
||||||
"vendor/micropython/lib/stm32lib/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c",
|
"vendor/micropython/lib/stm32lib/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c",
|
||||||
]
|
]
|
||||||
features_available.append("usb")
|
features_available.append("usb")
|
||||||
|
@ -58,14 +58,14 @@ def configure(
|
|||||||
|
|
||||||
if "usb" in features_wanted:
|
if "usb" in features_wanted:
|
||||||
sources += [
|
sources += [
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_hid.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_hid.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_vcp.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_vcp.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_webusb.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_webusb.c",
|
||||||
|
"embed/trezorhal/stm32f4/usb/usb.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_conf.c",
|
"embed/trezorhal/stm32f4/usb/usbd_conf.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_core.c",
|
"embed/trezorhal/stm32f4/usb/usbd_core.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_ctlreq.c",
|
"embed/trezorhal/stm32f4/usb/usbd_ctlreq.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_ioreq.c",
|
"embed/trezorhal/stm32f4/usb/usbd_ioreq.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd.c",
|
|
||||||
"vendor/micropython/lib/stm32lib/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c",
|
"vendor/micropython/lib/stm32lib/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c",
|
||||||
]
|
]
|
||||||
features_available.append("usb")
|
features_available.append("usb")
|
||||||
|
@ -58,14 +58,14 @@ def configure(
|
|||||||
|
|
||||||
if "usb" in features_wanted:
|
if "usb" in features_wanted:
|
||||||
sources += [
|
sources += [
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_hid.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_hid.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_vcp.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_vcp.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_webusb.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_webusb.c",
|
||||||
|
"embed/trezorhal/stm32f4/usb/usb.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_conf.c",
|
"embed/trezorhal/stm32f4/usb/usbd_conf.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_core.c",
|
"embed/trezorhal/stm32f4/usb/usbd_core.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_ctlreq.c",
|
"embed/trezorhal/stm32f4/usb/usbd_ctlreq.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_ioreq.c",
|
"embed/trezorhal/stm32f4/usb/usbd_ioreq.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd.c",
|
|
||||||
"vendor/micropython/lib/stm32lib/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c",
|
"vendor/micropython/lib/stm32lib/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c",
|
||||||
]
|
]
|
||||||
features_available.append("usb")
|
features_available.append("usb")
|
||||||
|
@ -100,14 +100,14 @@ def configure(
|
|||||||
|
|
||||||
if "usb" in features_wanted:
|
if "usb" in features_wanted:
|
||||||
sources += [
|
sources += [
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_hid.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_hid.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_vcp.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_vcp.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_class_webusb.c",
|
"embed/trezorhal/stm32f4/usb/usb_class_webusb.c",
|
||||||
|
"embed/trezorhal/stm32f4/usb/usb.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_conf.c",
|
"embed/trezorhal/stm32f4/usb/usbd_conf.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_core.c",
|
"embed/trezorhal/stm32f4/usb/usbd_core.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_ctlreq.c",
|
"embed/trezorhal/stm32f4/usb/usbd_ctlreq.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd_ioreq.c",
|
"embed/trezorhal/stm32f4/usb/usbd_ioreq.c",
|
||||||
"embed/trezorhal/stm32f4/usb/usbd.c",
|
|
||||||
"vendor/micropython/lib/stm32lib/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c",
|
"vendor/micropython/lib/stm32lib/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c",
|
||||||
]
|
]
|
||||||
features_available.append("usb")
|
features_available.append("usb")
|
||||||
|
@ -100,14 +100,14 @@ def configure(
|
|||||||
|
|
||||||
if "usb" in features_wanted:
|
if "usb" in features_wanted:
|
||||||
sources += [
|
sources += [
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_class_hid.c",
|
"embed/trezorhal/stm32u5/usb/usb_class_hid.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_class_vcp.c",
|
"embed/trezorhal/stm32u5/usb/usb_class_vcp.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_class_webusb.c",
|
"embed/trezorhal/stm32u5/usb/usb_class_webusb.c",
|
||||||
|
"embed/trezorhal/stm32u5/usb/usb.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_conf.c",
|
"embed/trezorhal/stm32u5/usb/usbd_conf.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_core.c",
|
"embed/trezorhal/stm32u5/usb/usbd_core.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_ctlreq.c",
|
"embed/trezorhal/stm32u5/usb/usbd_ctlreq.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_ioreq.c",
|
"embed/trezorhal/stm32u5/usb/usbd_ioreq.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd.c",
|
|
||||||
"vendor/stm32u5xx_hal_driver/Src/stm32u5xx_ll_usb.c",
|
"vendor/stm32u5xx_hal_driver/Src/stm32u5xx_ll_usb.c",
|
||||||
]
|
]
|
||||||
features_available.append("usb")
|
features_available.append("usb")
|
||||||
|
@ -102,14 +102,14 @@ def configure(
|
|||||||
|
|
||||||
if "usb" in features_wanted:
|
if "usb" in features_wanted:
|
||||||
sources += [
|
sources += [
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_class_hid.c",
|
"embed/trezorhal/stm32u5/usb/usb_class_hid.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_class_vcp.c",
|
"embed/trezorhal/stm32u5/usb/usb_class_vcp.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_class_webusb.c",
|
"embed/trezorhal/stm32u5/usb/usb_class_webusb.c",
|
||||||
|
"embed/trezorhal/stm32u5/usb/usb.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_conf.c",
|
"embed/trezorhal/stm32u5/usb/usbd_conf.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_core.c",
|
"embed/trezorhal/stm32u5/usb/usbd_core.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_ctlreq.c",
|
"embed/trezorhal/stm32u5/usb/usbd_ctlreq.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd_ioreq.c",
|
"embed/trezorhal/stm32u5/usb/usbd_ioreq.c",
|
||||||
"embed/trezorhal/stm32u5/usb/usbd.c",
|
|
||||||
"vendor/stm32u5xx_hal_driver/Src/stm32u5xx_ll_usb.c",
|
"vendor/stm32u5xx_hal_driver/Src/stm32u5xx_ll_usb.c",
|
||||||
]
|
]
|
||||||
features_available.append("usb")
|
features_available.append("usb")
|
||||||
|
Loading…
Reference in New Issue
Block a user