From ca063ae563adb48b62f25b25b93ad421c18f7391 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 28 Mar 2025 11:18:51 -0700 Subject: [PATCH] Update TinyUSB and close device endpoints --- lib/tinyusb | 2 +- ports/raspberrypi/lib/Pico-PIO-USB | 2 +- shared-bindings/usb/core/Device.c | 22 ++++++++++++++++++++++ shared-bindings/usb/core/Device.h | 1 + shared-module/usb/core/Device.c | 18 +++++++++++++++--- 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/lib/tinyusb b/lib/tinyusb index 6bba41045a422..8c1802e41d37c 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit 6bba41045a4224abb68256dcf2fce893da47a743 +Subproject commit 8c1802e41d37c915334a19b859b24cb2a1b48ee5 diff --git a/ports/raspberrypi/lib/Pico-PIO-USB b/ports/raspberrypi/lib/Pico-PIO-USB index 1862cc008e026..e0aba546813d8 160000 --- a/ports/raspberrypi/lib/Pico-PIO-USB +++ b/ports/raspberrypi/lib/Pico-PIO-USB @@ -1 +1 @@ -Subproject commit 1862cc008e026cbd07b97b28e29eafb5f38b35fb +Subproject commit e0aba546813d89cb7f321bef3363bfba5f282e14 diff --git a/shared-bindings/usb/core/Device.c b/shared-bindings/usb/core/Device.c index 28d178b748eae..683115a842102 100644 --- a/shared-bindings/usb/core/Device.c +++ b/shared-bindings/usb/core/Device.c @@ -39,6 +39,7 @@ #include "py/objproperty.h" #include "shared-bindings/usb/core/Device.h" +#include "shared-bindings/util.h" #include "py/runtime.h" //| class Device: @@ -49,6 +50,12 @@ //| ... //| +static void check_for_deinit(usb_core_device_obj_t *self) { + if (common_hal_usb_core_device_deinited(self)) { + raise_deinited_error(); + } +} + //| def __del__(self) -> None: //| """Closes any resources used for this device.""" //| ... @@ -64,6 +71,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_deinit_obj, usb_core_device_dei //| """The USB vendor ID of the device""" static mp_obj_t usb_core_device_obj_get_idVendor(mp_obj_t self_in) { usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); return MP_OBJ_NEW_SMALL_INT(common_hal_usb_core_device_get_idVendor(self)); } MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_idVendor_obj, usb_core_device_obj_get_idVendor); @@ -75,6 +83,7 @@ MP_PROPERTY_GETTER(usb_core_device_idVendor_obj, //| """The USB product ID of the device""" static mp_obj_t usb_core_device_obj_get_idProduct(mp_obj_t self_in) { usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); return MP_OBJ_NEW_SMALL_INT(common_hal_usb_core_device_get_idProduct(self)); } MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_idProduct_obj, usb_core_device_obj_get_idProduct); @@ -86,6 +95,7 @@ MP_PROPERTY_GETTER(usb_core_device_idProduct_obj, //| """The USB device's serial number string.""" static mp_obj_t usb_core_device_obj_get_serial_number(mp_obj_t self_in) { usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); return common_hal_usb_core_device_get_serial_number(self); } MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_serial_number_obj, usb_core_device_obj_get_serial_number); @@ -97,6 +107,7 @@ MP_PROPERTY_GETTER(usb_core_device_serial_number_obj, //| """The USB device's product string.""" static mp_obj_t usb_core_device_obj_get_product(mp_obj_t self_in) { usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); return common_hal_usb_core_device_get_product(self); } MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_product_obj, usb_core_device_obj_get_product); @@ -109,6 +120,7 @@ MP_PROPERTY_GETTER(usb_core_device_product_obj, //| static mp_obj_t usb_core_device_obj_get_manufacturer(mp_obj_t self_in) { usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); return common_hal_usb_core_device_get_manufacturer(self); } MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_manufacturer_obj, usb_core_device_obj_get_manufacturer); @@ -121,6 +133,7 @@ MP_PROPERTY_GETTER(usb_core_device_manufacturer_obj, //| static mp_obj_t usb_core_device_obj_get_bus(mp_obj_t self_in) { usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); return MP_OBJ_NEW_SMALL_INT(common_hal_usb_core_device_get_bus(self)); } MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_bus_obj, usb_core_device_obj_get_bus); @@ -134,6 +147,7 @@ MP_PROPERTY_GETTER(usb_core_device_bus_obj, //| static mp_obj_t usb_core_device_obj_get_port_numbers(mp_obj_t self_in) { usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); return common_hal_usb_core_device_get_port_numbers(self); } MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_port_numbers_obj, usb_core_device_obj_get_port_numbers); @@ -147,6 +161,7 @@ MP_PROPERTY_GETTER(usb_core_device_port_numbers_obj, //| static mp_obj_t usb_core_device_obj_get_speed(mp_obj_t self_in) { usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); return MP_OBJ_NEW_SMALL_INT(common_hal_usb_core_device_get_speed(self)); } MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_speed_obj, usb_core_device_obj_get_speed); @@ -171,6 +186,7 @@ static mp_obj_t usb_core_device_set_configuration(size_t n_args, const mp_obj_t { MP_QSTR_configuration, MP_ARG_INT, {.u_int = 1} }, }; usb_core_device_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + check_for_deinit(self); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -197,6 +213,7 @@ static mp_obj_t usb_core_device_write(size_t n_args, const mp_obj_t *pos_args, m { MP_QSTR_timeout, MP_ARG_INT, {.u_int = 0} }, }; usb_core_device_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + check_for_deinit(self); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -228,6 +245,7 @@ static mp_obj_t usb_core_device_read(size_t n_args, const mp_obj_t *pos_args, mp { MP_QSTR_timeout, MP_ARG_INT, {.u_int = 0} }, }; usb_core_device_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + check_for_deinit(self); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -277,6 +295,7 @@ static mp_obj_t usb_core_device_ctrl_transfer(size_t n_args, const mp_obj_t *pos { MP_QSTR_timeout, MP_ARG_INT, {.u_int = 0} }, }; usb_core_device_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + check_for_deinit(self); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -310,6 +329,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(usb_core_device_ctrl_transfer_obj, 2, usb_core_device //| static mp_obj_t usb_core_device_is_kernel_driver_active(mp_obj_t self_in, mp_obj_t interface_in) { usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); mp_int_t interface = mp_obj_get_int(interface_in); bool active = common_hal_usb_core_device_is_kernel_driver_active(self, interface); return mp_obj_new_bool(active); @@ -327,6 +347,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(usb_core_device_is_kernel_driver_active_obj, usb_core_ //| static mp_obj_t usb_core_device_detach_kernel_driver(mp_obj_t self_in, mp_obj_t interface_in) { usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); mp_int_t interface = mp_obj_get_int(interface_in); common_hal_usb_core_device_detach_kernel_driver(self, interface); return mp_const_none; @@ -343,6 +364,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(usb_core_device_detach_kernel_driver_obj, usb_core_dev //| static mp_obj_t usb_core_device_attach_kernel_driver(mp_obj_t self_in, mp_obj_t interface_in) { usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); mp_int_t interface = mp_obj_get_int(interface_in); common_hal_usb_core_device_attach_kernel_driver(self, interface); return mp_const_none; diff --git a/shared-bindings/usb/core/Device.h b/shared-bindings/usb/core/Device.h index 7278ca297ede3..28c2cfbe19860 100644 --- a/shared-bindings/usb/core/Device.h +++ b/shared-bindings/usb/core/Device.h @@ -13,6 +13,7 @@ extern const mp_obj_type_t usb_core_device_type; bool common_hal_usb_core_device_construct(usb_core_device_obj_t *self, uint8_t device_number); +bool common_hal_usb_core_device_deinited(usb_core_device_obj_t *self); void common_hal_usb_core_device_deinit(usb_core_device_obj_t *self); uint16_t common_hal_usb_core_device_get_idVendor(usb_core_device_obj_t *self); uint16_t common_hal_usb_core_device_get_idProduct(usb_core_device_obj_t *self); diff --git a/shared-module/usb/core/Device.c b/shared-module/usb/core/Device.c index 10fdf63b1ed6f..0a52b925facf1 100644 --- a/shared-module/usb/core/Device.c +++ b/shared-module/usb/core/Device.c @@ -49,10 +49,22 @@ bool common_hal_usb_core_device_construct(usb_core_device_obj_t *self, uint8_t d return true; } +bool common_hal_usb_core_device_deinited(usb_core_device_obj_t *self) { + return self->device_address == 0; +} + void common_hal_usb_core_device_deinit(usb_core_device_obj_t *self) { - // TODO: Close all of the endpoints we've opened. Some drivers store state - // for each open endpoint. If we don't close them, then we'll leak memory. - // Waiting for TinyUSB to add this. + if (common_hal_usb_core_device_deinited(self)) { + return; + } + size_t open_size = sizeof(self->open_endpoints); + for (size_t i = 0; i < open_size; i++) { + if (self->open_endpoints[i] != 0) { + tuh_edpt_close(self->device_address, self->open_endpoints[i]); + self->open_endpoints[i] = 0; + } + } + self->device_address = 0; } uint16_t common_hal_usb_core_device_get_idVendor(usb_core_device_obj_t *self) { pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy