diff --git a/embed/bootloader/main.c b/embed/bootloader/main.c index ec1775102e..bd5d557992 100644 --- a/embed/bootloader/main.c +++ b/embed/bootloader/main.c @@ -162,7 +162,7 @@ void usb_init_all(void) { }; usb_init(&dev_info); - ensure(0 == usb_hid_add(&hid_info), NULL); + ensure(usb_hid_add(&hid_info), NULL); usb_start(); } diff --git a/embed/extmod/modtrezorio/modtrezorio-msg.h b/embed/extmod/modtrezorio/modtrezorio-msg.h index 6984eeec9c..78a31b951e 100644 --- a/embed/extmod/modtrezorio/modtrezorio-msg.h +++ b/embed/extmod/modtrezorio/modtrezorio-msg.h @@ -449,13 +449,13 @@ STATIC mp_obj_t mod_trezorio_USB_open(mp_obj_t self) { if (MP_OBJ_IS_TYPE(iface, &mod_trezorio_HID_type)) { mp_obj_HID_t *hid = MP_OBJ_TO_PTR(iface); - if (usb_hid_add(&hid->info) != 0) { + if (!usb_hid_add(&hid->info)) { usb_deinit(); mp_raise_msg(&mp_type_RuntimeError, "failed to add HID interface"); } } else if (MP_OBJ_IS_TYPE(iface, &mod_trezorio_VCP_type)) { mp_obj_VCP_t *vcp = MP_OBJ_TO_PTR(iface); - if (usb_vcp_add(&vcp->info) != 0) { + if (!usb_vcp_add(&vcp->info)) { usb_deinit(); mp_raise_msg(&mp_type_RuntimeError, "failed to add VCP interface"); } diff --git a/embed/extmod/modtrezorio/unix-msg-mock.h b/embed/extmod/modtrezorio/unix-msg-mock.h index 9f19cabde5..96b47e643d 100644 --- a/embed/extmod/modtrezorio/unix-msg-mock.h +++ b/embed/extmod/modtrezorio/unix-msg-mock.h @@ -58,42 +58,34 @@ void usb_start(void) { void usb_stop(void) { } -int usb_hid_add(const usb_hid_info_t *info) { - return 0; +bool usb_hid_add(const usb_hid_info_t *info) { + return true; } -int usb_vcp_add(const usb_vcp_info_t *info) { - return 0; +bool usb_vcp_add(const usb_vcp_info_t *info) { + return true; } -int usb_hid_can_read(uint8_t iface_num) { +bool usb_hid_can_read(uint8_t iface_num) { if (iface_num != TREZOR_UDP_IFACE) { - return 0; + return false; } struct pollfd fds[] = { { sock, POLLIN, 0 }, }; int r = poll(fds, 1, 0); - if (r > 0) { - return 1; - } else { - return 0; - } + return r > 0; } -int usb_hid_can_write(uint8_t iface_num) { +bool usb_hid_can_write(uint8_t iface_num) { if (iface_num != TREZOR_UDP_IFACE) { - return 0; + return false; } struct pollfd fds[] = { { sock, POLLOUT, 0 }, }; int r = poll(fds, 1, 0); - if (r > 0) { - return 1; - } else { - return 0; - } + return r > 0; } int usb_hid_read(uint8_t iface_num, uint8_t *buf, uint32_t len) { diff --git a/embed/trezorhal/usb_hid-defs.h b/embed/trezorhal/usb_hid-defs.h index 4e74e8793b..3d6ce4993d 100644 --- a/embed/trezorhal/usb_hid-defs.h +++ b/embed/trezorhal/usb_hid-defs.h @@ -69,9 +69,9 @@ typedef struct { uint8_t ep_in_is_idle; // Set to 1 after IN endpoint gets idle } usb_hid_state_t; -int usb_hid_add(const usb_hid_info_t *hid_info); -int usb_hid_can_read(uint8_t iface_num); -int usb_hid_can_write(uint8_t iface_num); +bool usb_hid_add(const usb_hid_info_t *hid_info); +bool usb_hid_can_read(uint8_t iface_num); +bool usb_hid_can_write(uint8_t iface_num); int usb_hid_read(uint8_t iface_num, uint8_t *buf, uint32_t len); int usb_hid_write(uint8_t iface_num, const uint8_t *buf, uint32_t len); diff --git a/embed/trezorhal/usb_hid-impl.h b/embed/trezorhal/usb_hid-impl.h index 233af4f3fd..520d99ff09 100644 --- a/embed/trezorhal/usb_hid-impl.h +++ b/embed/trezorhal/usb_hid-impl.h @@ -17,34 +17,34 @@ /* usb_hid_add adds and configures new USB HID interface according to * configuration options passed in `info`. */ -int usb_hid_add(const usb_hid_info_t *info) { +bool usb_hid_add(const usb_hid_info_t *info) { usb_iface_t *iface = usb_get_iface(info->iface_num); if (iface == NULL) { - return 1; // Invalid interface number + return false; // Invalid interface number } if (iface->type != USB_IFACE_TYPE_DISABLED) { - return 1; // Interface is already enabled + return false; // Interface is already enabled } usb_hid_descriptor_block_t *d = usb_desc_alloc_iface(sizeof(usb_hid_descriptor_block_t)); if (d == NULL) { - return 1; // Not enough space in the configuration descriptor + return false; // Not enough space in the configuration descriptor } if ((info->ep_in & USB_EP_DIR_MSK) != USB_EP_DIR_IN) { - return 1; // IN EP is invalid + return false; // IN EP is invalid } if ((info->ep_out & USB_EP_DIR_MSK) != USB_EP_DIR_OUT) { - return 1; // OUT EP is invalid + return false; // OUT EP is invalid } if (info->rx_buffer == NULL) { - return 1; + return false; } if (info->report_desc == NULL) { - return 1; + return false; } // Interface descriptor @@ -101,41 +101,41 @@ int usb_hid_add(const usb_hid_info_t *info) { iface->hid.last_read_len = 0; iface->hid.ep_in_is_idle = 1; - return 0; + return true; } -int usb_hid_can_read(uint8_t iface_num) { +bool usb_hid_can_read(uint8_t iface_num) { usb_iface_t *iface = usb_get_iface(iface_num); if (iface == NULL) { - return 0; // Invalid interface number + return false; // Invalid interface number } if (iface->type != USB_IFACE_TYPE_HID) { - return 0; // Invalid interface type + return false; // Invalid interface type } if (iface->hid.last_read_len == 0) { - return 0; // Nothing in the receiving buffer + return false; // Nothing in the receiving buffer } if (usb_dev_handle.dev_state != USBD_STATE_CONFIGURED) { - return 0; // Device is not configured + return false; // Device is not configured } - return 1; + return true; } -int usb_hid_can_write(uint8_t iface_num) { +bool usb_hid_can_write(uint8_t iface_num) { usb_iface_t *iface = usb_get_iface(iface_num); if (iface == NULL) { - return 0; // Invalid interface number + return false; // Invalid interface number } if (iface->type != USB_IFACE_TYPE_HID) { - return 0; // Invalid interface type + return false; // Invalid interface type } if (iface->hid.ep_in_is_idle == 0) { - return 0; // Last transmission is not over yet + return false; // Last transmission is not over yet } if (usb_dev_handle.dev_state != USBD_STATE_CONFIGURED) { - return 0; // Device is not configured + return false; // Device is not configured } - return 1; + return true; } int usb_hid_read(uint8_t iface_num, uint8_t *buf, uint32_t len) { @@ -216,7 +216,7 @@ int usb_hid_write_blocking(uint8_t iface_num, const uint8_t *buf, uint32_t len, return usb_hid_write(iface_num, buf, len); } -static int usb_hid_class_init(USBD_HandleTypeDef *dev, usb_hid_state_t *state, uint8_t cfg_idx) { +static void usb_hid_class_init(USBD_HandleTypeDef *dev, usb_hid_state_t *state, uint8_t cfg_idx) { // Open endpoints USBD_LL_OpenEP(dev, state->ep_in, USBD_EP_TYPE_INTR, state->max_packet_len); USBD_LL_OpenEP(dev, state->ep_out, USBD_EP_TYPE_INTR, state->max_packet_len); @@ -230,16 +230,12 @@ static int usb_hid_class_init(USBD_HandleTypeDef *dev, usb_hid_state_t *state, u // Prepare the OUT EP to receive next packet USBD_LL_PrepareReceive(dev, state->ep_out, state->rx_buffer, state->max_packet_len); - - return USBD_OK; } -static int usb_hid_class_deinit(USBD_HandleTypeDef *dev, usb_hid_state_t *state, uint8_t cfg_idx) { +static void usb_hid_class_deinit(USBD_HandleTypeDef *dev, usb_hid_state_t *state, uint8_t cfg_idx) { // Close endpoints USBD_LL_CloseEP(dev, state->ep_in); USBD_LL_CloseEP(dev, state->ep_out); - - return USBD_OK; } static int usb_hid_class_setup(USBD_HandleTypeDef *dev, usb_hid_state_t *state, USBD_SetupReqTypedef *req) { @@ -298,17 +294,17 @@ static int usb_hid_class_setup(USBD_HandleTypeDef *dev, usb_hid_state_t *state, } break; } + return USBD_OK; } -static uint8_t usb_hid_class_data_in(USBD_HandleTypeDef *dev, usb_hid_state_t *state, uint8_t ep_num) { +static void usb_hid_class_data_in(USBD_HandleTypeDef *dev, usb_hid_state_t *state, uint8_t ep_num) { if ((ep_num | USB_EP_DIR_IN) == state->ep_in) { state->ep_in_is_idle = 1; } - return USBD_OK; } -static uint8_t usb_hid_class_data_out(USBD_HandleTypeDef *dev, usb_hid_state_t *state, uint8_t ep_num) { +static void usb_hid_class_data_out(USBD_HandleTypeDef *dev, usb_hid_state_t *state, uint8_t ep_num) { if (ep_num == state->ep_out) { state->last_read_len = USBD_LL_GetRxDataSize(dev, ep_num); @@ -321,5 +317,4 @@ static uint8_t usb_hid_class_data_out(USBD_HandleTypeDef *dev, usb_hid_state_t * usb_ep_set_nak(dev, ep_num); } } - return USBD_OK; } diff --git a/embed/trezorhal/usb_vcp-defs.h b/embed/trezorhal/usb_vcp-defs.h index 83cc671bd5..6db7a52988 100644 --- a/embed/trezorhal/usb_vcp-defs.h +++ b/embed/trezorhal/usb_vcp-defs.h @@ -119,9 +119,9 @@ typedef struct { uint8_t ep_in_is_idle; // Set to 1 after IN endpoint gets idle } usb_vcp_state_t; -int usb_vcp_add(const usb_vcp_info_t *vcp_info); -int usb_vcp_can_read(uint8_t iface_num); -int usb_vcp_can_write(uint8_t iface_num); +bool usb_vcp_add(const usb_vcp_info_t *vcp_info); +bool usb_vcp_can_read(uint8_t iface_num); +bool usb_vcp_can_write(uint8_t iface_num); int usb_vcp_read(uint8_t iface_num, uint8_t *buf, uint32_t len); int usb_vcp_write(uint8_t iface_num, const uint8_t *buf, uint32_t len); diff --git a/embed/trezorhal/usb_vcp-impl.h b/embed/trezorhal/usb_vcp-impl.h index 34887ee5a5..e0b51605b5 100644 --- a/embed/trezorhal/usb_vcp-impl.h +++ b/embed/trezorhal/usb_vcp-impl.h @@ -42,49 +42,49 @@ /* usb_vcp_add adds and configures new USB VCP interface according to * configuration options passed in `info`. */ -int usb_vcp_add(const usb_vcp_info_t *info) { +bool usb_vcp_add(const usb_vcp_info_t *info) { usb_iface_t *iface = usb_get_iface(info->iface_num); if (iface == NULL) { - return 1; // Invalid interface number + return false; // Invalid interface number } if (iface->type != USB_IFACE_TYPE_DISABLED) { - return 1; // Interface is already enabled + return false; // Interface is already enabled } usb_vcp_descriptor_block_t *d = usb_desc_alloc_iface(sizeof(usb_vcp_descriptor_block_t)); if (d == NULL) { - return 1; // Not enough space in the configuration descriptor + return false; // Not enough space in the configuration descriptor } if ((info->ep_cmd & USB_EP_DIR_MSK) != USB_EP_DIR_IN) { - return 1; // IN CMD EP is invalid + return false; // IN CMD EP is invalid } if ((info->ep_in & USB_EP_DIR_MSK) != USB_EP_DIR_IN) { - return 1; // IN EP is invalid + return false; // IN EP is invalid } if ((info->ep_out & USB_EP_DIR_MSK) != USB_EP_DIR_OUT) { - return 1; // OUT EP is invalid + return false; // OUT EP is invalid } if ((info->rx_buffer_len == 0) || (info->rx_buffer_len & (info->rx_buffer_len - 1)) != 0) { - return 1; // Capacity needs to be a power of 2 + return false; // Capacity needs to be a power of 2 } if ((info->tx_buffer_len == 0) || (info->tx_buffer_len & (info->tx_buffer_len - 1)) != 0) { - return 1; // Capacity needs to be a power of 2 + return false; // Capacity needs to be a power of 2 } if (info->rx_buffer == NULL) { - return 1; + return false; } if (info->rx_packet == NULL) { - return 1; + return false; } if (info->tx_buffer == NULL) { - return 1; + return false; } if (info->tx_packet == NULL) { - return 1; + return false; } // Interface association descriptor @@ -205,7 +205,7 @@ int usb_vcp_add(const usb_vcp_info_t *info) { iface->vcp.ep_in_is_idle = 1; - return 0; + return true; } static inline size_t ring_length(usb_rbuf_t *b) { @@ -220,32 +220,32 @@ static inline int ring_full(usb_rbuf_t *b) { return ring_length(b) == b->cap; } -int usb_vcp_can_read(uint8_t iface_num) { +bool usb_vcp_can_read(uint8_t iface_num) { usb_iface_t *iface = usb_get_iface(iface_num); if (iface == NULL) { - return 0; // Invalid interface number + return false; // Invalid interface number } if (iface->type != USB_IFACE_TYPE_VCP) { - return 0; // Invalid interface type + return false; // Invalid interface type } if (ring_empty(&iface->vcp.rx_ring)) { - return 0; // Nothing in the rx buffer + return false; // Nothing in the rx buffer } - return 1; + return true; } -int usb_vcp_can_write(uint8_t iface_num) { +bool usb_vcp_can_write(uint8_t iface_num) { usb_iface_t *iface = usb_get_iface(iface_num); if (iface == NULL) { - return 0; // Invalid interface number + return false; // Invalid interface number } if (iface->type != USB_IFACE_TYPE_VCP) { - return 0; // Invalid interface type + return false; // Invalid interface type } if (ring_full(&iface->vcp.tx_ring)) { - return 0; // Tx ring buffer is full + return false; // Tx ring buffer is full } - return 1; + return true; } int usb_vcp_read(uint8_t iface_num, uint8_t *buf, uint32_t len) { @@ -313,7 +313,7 @@ int usb_vcp_write_blocking(uint8_t iface_num, const uint8_t *buf, uint32_t len, return usb_vcp_write(iface_num, buf, len); } -static int usb_vcp_class_init(USBD_HandleTypeDef *dev, usb_vcp_state_t *state, uint8_t cfg_idx) { +static void usb_vcp_class_init(USBD_HandleTypeDef *dev, usb_vcp_state_t *state, uint8_t cfg_idx) { // Open endpoints USBD_LL_OpenEP(dev, state->ep_in, USBD_EP_TYPE_BULK, state->max_packet_len); USBD_LL_OpenEP(dev, state->ep_out, USBD_EP_TYPE_BULK, state->max_packet_len); @@ -328,17 +328,13 @@ static int usb_vcp_class_init(USBD_HandleTypeDef *dev, usb_vcp_state_t *state, u // Prepare the OUT EP to receive next packet USBD_LL_PrepareReceive(dev, state->ep_out, state->rx_packet, state->max_packet_len); - - return USBD_OK; } -static int usb_vcp_class_deinit(USBD_HandleTypeDef *dev, usb_vcp_state_t *state, uint8_t cfg_idx) { +static void usb_vcp_class_deinit(USBD_HandleTypeDef *dev, usb_vcp_state_t *state, uint8_t cfg_idx) { // Close endpoints USBD_LL_CloseEP(dev, state->ep_in); USBD_LL_CloseEP(dev, state->ep_out); USBD_LL_CloseEP(dev, state->ep_cmd); - - return USBD_OK; } static int usb_vcp_class_setup(USBD_HandleTypeDef *dev, usb_vcp_state_t *state, USBD_SetupReqTypedef *req) { @@ -377,14 +373,13 @@ static int usb_vcp_class_setup(USBD_HandleTypeDef *dev, usb_vcp_state_t *state, return USBD_OK; } -static uint8_t usb_vcp_class_data_in(USBD_HandleTypeDef *dev, usb_vcp_state_t *state, uint8_t ep_num) { +static void usb_vcp_class_data_in(USBD_HandleTypeDef *dev, usb_vcp_state_t *state, uint8_t ep_num) { if ((ep_num | USB_EP_DIR_IN) == state->ep_in) { state->ep_in_is_idle = 1; } - return USBD_OK; } -static uint8_t usb_vcp_class_data_out(USBD_HandleTypeDef *dev, usb_vcp_state_t *state, uint8_t ep_num) { +static void usb_vcp_class_data_out(USBD_HandleTypeDef *dev, usb_vcp_state_t *state, uint8_t ep_num) { if (ep_num == state->ep_out) { uint32_t len = USBD_LL_GetRxDataSize(dev, ep_num); @@ -407,12 +402,11 @@ static uint8_t usb_vcp_class_data_out(USBD_HandleTypeDef *dev, usb_vcp_state_t * // Prepare the OUT EP to receive next packet USBD_LL_PrepareReceive(dev, state->ep_out, state->rx_packet, state->max_packet_len); } - return USBD_OK; } -static uint8_t usb_vcp_class_sof(USBD_HandleTypeDef *dev, usb_vcp_state_t *state) { +static void usb_vcp_class_sof(USBD_HandleTypeDef *dev, usb_vcp_state_t *state) { if (!state->ep_in_is_idle) { - return USBD_OK; + return; } // Read from the tx ring buffer @@ -432,6 +426,4 @@ static uint8_t usb_vcp_class_sof(USBD_HandleTypeDef *dev, usb_vcp_state_t *state state->ep_in_is_idle = 0; USBD_LL_Transmit(&usb_dev_handle, state->ep_in, buf, (uint16_t)i); } - - return USBD_OK; }