1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-04-04 09:25:56 +00:00

feat(core): add event polling to usb webusb driver

This commit is contained in:
cepetr 2025-03-20 15:29:14 +01:00
parent dc05c6d5f7
commit 56fc2a8c65

View File

@ -23,6 +23,7 @@
#include <io/usb_webusb.h>
#include <sec/random_delays.h>
#include <sys/sysevent_source.h>
#include "usb_internal.h"
@ -55,6 +56,7 @@ _Static_assert(sizeof(usb_webusb_state_t) <= USBD_CLASS_STATE_MAX_SIZE);
// interface dispatch functions
static const USBD_ClassTypeDef usb_webusb_class;
static const syshandle_vmt_t usb_webusb_handle_vmt;
#define usb_get_webusb_state(iface_num) \
((usb_webusb_state_t *)usb_get_iface_state(iface_num, &usb_webusb_class))
@ -265,6 +267,11 @@ static uint8_t usb_webusb_class_init(USBD_HandleTypeDef *dev, uint8_t cfg_idx) {
USBD_LL_PrepareReceive(dev, state->ep_out, state->rx_buffer,
state->max_packet_len);
uint8_t iface_num = state->desc_block->iface.bInterfaceNumber;
syshandle_t handle = SYSHANDLE_USB_IFACE_0 + iface_num;
syshandle_register(handle, &usb_webusb_handle_vmt, state);
// !@# TODO check result
return USBD_OK;
}
@ -272,6 +279,10 @@ static uint8_t usb_webusb_class_deinit(USBD_HandleTypeDef *dev,
uint8_t cfg_idx) {
usb_webusb_state_t *state = (usb_webusb_state_t *)dev->pUserData;
uint8_t iface_num = state->desc_block->iface.bInterfaceNumber;
syshandle_t handle = SYSHANDLE_USB_IFACE_0 + iface_num;
syshandle_unregister(handle);
// Flush endpoints
USBD_LL_FlushEP(dev, state->ep_in);
USBD_LL_FlushEP(dev, state->ep_out);
@ -358,4 +369,50 @@ static const USBD_ClassTypeDef usb_webusb_class = {
.GetUsrStrDescriptor = NULL,
};
static void on_event_poll(void *context, bool read_awaited,
bool write_awaited) {
usb_webusb_state_t *state = (usb_webusb_state_t *)context;
uint8_t iface_num = state->desc_block->iface.bInterfaceNumber;
syshandle_t handle = SYSHANDLE_USB_IFACE_0 + iface_num;
if (read_awaited && usb_webusb_can_read(iface_num)) {
syshandle_signal_read_ready(handle, NULL);
}
if (write_awaited && usb_webusb_can_write(iface_num)) {
syshandle_signal_write_ready(handle, NULL);
}
}
static bool on_check_read_ready(void *context, systask_id_t task_id,
void *param) {
usb_webusb_state_t *state = (usb_webusb_state_t *)context;
uint8_t iface_num = state->desc_block->iface.bInterfaceNumber;
UNUSED(task_id);
UNUSED(param);
return usb_webusb_can_read(iface_num);
}
static bool on_check_write_ready(void *context, systask_id_t task_id,
void *param) {
usb_webusb_state_t *state = (usb_webusb_state_t *)context;
uint8_t iface_num = state->desc_block->iface.bInterfaceNumber;
UNUSED(task_id);
UNUSED(param);
return usb_webusb_can_write(iface_num);
}
static const syshandle_vmt_t usb_webusb_handle_vmt = {
.task_created = NULL,
.task_killed = NULL,
.check_read_ready = on_check_read_ready,
.check_write_ready = on_check_write_ready,
.poll = on_event_poll,
};
#endif // KERNEL_MODE