From 9511e4bd19d558c2f15f027480dee6d209b36e57 Mon Sep 17 00:00:00 2001 From: sakumisu <1203593632@qq.com> Date: Sun, 1 Sep 2024 00:24:37 +0800 Subject: [PATCH] update(cherryusb): fix dfs mem leak, add check for lwip stack size, select elmfat when use msc --- components/drivers/usb/cherryusb/Kconfig | 6 + .../drivers/usb/cherryusb/Kconfig.cherryusb | 312 ------------------ .../usb/cherryusb/osal/usb_osal_rtthread.c | 32 +- .../cherryusb/platform/rtthread/usb_check.c | 4 +- .../cherryusb/platform/rtthread/usbh_dfs.c | 18 +- .../cherryusb/platform/rtthread/usbh_lwip.c | 4 + 6 files changed, 54 insertions(+), 322 deletions(-) delete mode 100644 components/drivers/usb/cherryusb/Kconfig.cherryusb diff --git a/components/drivers/usb/cherryusb/Kconfig b/components/drivers/usb/cherryusb/Kconfig index c3bef25162..624b7acd1a 100644 --- a/components/drivers/usb/cherryusb/Kconfig +++ b/components/drivers/usb/cherryusb/Kconfig @@ -209,6 +209,7 @@ if RT_USING_CHERRYUSB prompt "Enable usb msc driver" default n select RT_USING_DFS + select RT_USING_DFS_ELMFAT config RT_CHERRYUSB_HOST_CDC_ECM bool @@ -300,6 +301,11 @@ if RT_USING_CHERRYUSB config CONFIG_USBHOST_PLATFORM_RTL8152 bool + config RT_LWIP_PBUF_POOL_BUFSIZE + int "The size of each pbuf in the pbuf pool" + range 1500 2000 + default 1600 + config RT_CHERRYUSB_HOST_TEMPLATE bool prompt "Use usb host template" diff --git a/components/drivers/usb/cherryusb/Kconfig.cherryusb b/components/drivers/usb/cherryusb/Kconfig.cherryusb deleted file mode 100644 index 0e36e73bb4..0000000000 --- a/components/drivers/usb/cherryusb/Kconfig.cherryusb +++ /dev/null @@ -1,312 +0,0 @@ -# Kconfig file for CherryUSB -menuconfig CHERRYUSB - bool "Using CherryUSB" - default n - -if CHERRYUSB - - menuconfig CHERRYUSB_DEVICE - bool "Enable usb device mode" - default n - - if CHERRYUSB_DEVICE - choice - prompt "Select usb device speed" - default CHERRYUSB_DEVICE_SPEED_FS - config CHERRYUSB_DEVICE_SPEED_FS - bool "FS" - config CHERRYUSB_DEVICE_SPEED_HS - bool "HS" - config CHERRYUSB_DEVICE_SPEED_AUTO - bool "AUTO" - endchoice - - choice - prompt "Select usb device ip, and some ip need config in usb_config.h, please check" - default CHERRYUSB_DEVICE_CUSTOM - config CHERRYUSB_DEVICE_CUSTOM - bool "CUSTOM (Implement it yourself)" - config CHERRYUSB_DEVICE_FSDEV - bool "fsdev" - config CHERRYUSB_DEVICE_DWC2_ST - bool "dwc2_st" - config CHERRYUSB_DEVICE_DWC2_ESP - bool "dwc2_esp" - config CHERRYUSB_DEVICE_DWC2_AT - bool "dwc2_at" - config CHERRYUSB_DEVICE_DWC2_GD - bool "dwc2_gd" - config CHERRYUSB_DEVICE_DWC2_HC - bool "dwc2_hc" - config CHERRYUSB_DEVICE_DWC2_CUSTOM - bool "dwc2_custom" - config CHERRYUSB_DEVICE_MUSB_ES - bool "musb_es" - config CHERRYUSB_DEVICE_MUSB_SUNXI - bool "musb_sunxi" - config CHERRYUSB_DEVICE_MUSB_BK - bool "musb_bk" - config CHERRYUSB_DEVICE_MUSB_CUSTOM - bool "musb_custom" - config CHERRYUSB_DEVICE_BL - bool "bouffalo" - config CHERRYUSB_DEVICE_HPM - bool "hpm" - config CHERRYUSB_DEVICE_AIC - bool "aic" - config CHERRYUSB_DEVICE_CH32 - bool "ch32" - config CHERRYUSB_DEVICE_PUSB2 - bool "pusb2" - endchoice - - config CHERRYUSB_DEVICE_CDC_ACM - bool - prompt "Enable usb cdc acm device" - default n - - config CHERRYUSB_DEVICE_HID - bool - prompt "Enable usb hid device" - default n - - config CHERRYUSB_DEVICE_MSC - bool - prompt "Enable usb msc device" - default n - - config CHERRYUSB_DEVICE_AUDIO - bool - prompt "Enable usb audio device" - default n - - config CHERRYUSB_DEVICE_VIDEO - bool - prompt "Enable usb video device" - default n - - config CHERRYUSB_DEVICE_CDC_RNDIS - bool - prompt "Enable usb cdc rndis device" - default n - - config CHERRYUSB_DEVICE_CDC_ECM - bool - prompt "Enable usb cdc ecm device" - default n - - config CHERRYUSB_DEVICE_CDC_NCM - bool - prompt "Enable usb cdc ncm device" - default n - - config CHERRYUSB_DEVICE_DFU - bool - prompt "Enable usb dfu device" - default n - - choice - prompt "Select usb device template" - default CHERRYUSB_DEVICE_TEMPLATE_NONE - config CHERRYUSB_DEVICE_TEMPLATE_NONE - bool "none (Implement it yourself)" - config CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM - bool "cdc_acm" - config CHERRYUSB_DEVICE_TEMPLATE_MSC - bool "msc" - config CHERRYUSB_DEVICE_TEMPLATE_HID_KEYBOARD - bool "hid_keyboard" - config CHERRYUSB_DEVICE_TEMPLATE_HID_MOUSE - bool "hid_mouse" - config CHERRYUSB_DEVICE_TEMPLATE_HID_CUSTOM - bool "hid_custom" - config CHERRYUSB_DEVICE_TEMPLATE_VIDEO - bool "video" - config CHERRYUSB_DEVICE_TEMPLATE_AUDIO_V1_MIC_SPEAKER - bool "audio_v1_mic_speaker_multichan" - config CHERRYUSB_DEVICE_TEMPLATE_AUDIO_V2_MIC_SPEAKER - bool "audio_v2_mic_speaker_multichan" - config CHERRYUSB_DEVICE_TEMPLATE_CDC_RNDIS - bool "cdc_rndis" - config CHERRYUSB_DEVICE_TEMPLATE_CDC_ECM - bool "cdc_ecm" - config CHERRYUSB_DEVICE_TEMPLATE_CDC_NCM - bool "cdc_ncm" - config CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM_MSC - bool "cdc_acm_msc" - config CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM_MSC_HID - bool "cdc_acm_msc_hid" - config CHERRYUSB_DEVICE_TEMPLATE_WINUSBV1 - bool "winusbv1" - config CHERRYUSB_DEVICE_TEMPLATE_WINUSBV2_CDC - bool "winusbv2_cdc" - config CHERRYUSB_DEVICE_TEMPLATE_WINUSBV2_HID - bool "winusbv2_hid" - endchoice - - endif - - menuconfig CHERRYUSB_HOST - bool "Enable usb host mode" - default n - - if CHERRYUSB_HOST - choice - prompt "Select usb host ip, and some ip need config in usb_config.h, please check" - default CHERRYUSB_HOST_CUSTOM - config CHERRYUSB_HOST_CUSTOM - bool "CUSTOM (Implement it yourself)" - config CHERRYUSB_HOST_EHCI_BL - bool "ehci_bouffalo" - config CHERRYUSB_HOST_EHCI_HPM - bool "ehci_hpm" - config CHERRYUSB_HOST_EHCI_AIC - bool "ehci_aic" - config CHERRYUSB_HOST_EHCI_NUVOTON_NUC980 - bool "ehci_nuvoton_nuc980" - config CHERRYUSB_HOST_EHCI_NUVOTON_MA35D0 - bool "ehci_nuvoton_ma35d0" - config CHERRYUSB_HOST_EHCI_CUSTOM - bool "ehci_custom" - config CHERRYUSB_HOST_DWC2_ST - bool "dwc2_st" - config CHERRYUSB_HOST_DWC2_ESP - bool "dwc2_esp" - config CHERRYUSB_HOST_DWC2_HC - bool "dwc2_hc" - config CHERRYUSB_HOST_DWC2_CUSTOM - bool "dwc2_custom" - config CHERRYUSB_HOST_MUSB_ES - bool "musb_es" - config CHERRYUSB_HOST_MUSB_SUNXI - bool "musb_sunxi" - config CHERRYUSB_HOST_MUSB_BK - bool "musb_bk" - config CHERRYUSB_HOST_MUSB_CUSTOM - bool "musb_custom" - config CHERRYUSB_HOST_PUSB2 - bool "pusb2" - config CHERRYUSB_HOST_XHCI - bool "xhci" - endchoice - - config CHERRYUSB_HOST_CDC_ACM - bool - prompt "Enable usb cdc acm driver" - default n - - config CHERRYUSB_HOST_HID - bool - prompt "Enable usb hid driver" - default n - - config CHERRYUSB_HOST_MSC - bool - prompt "Enable usb msc driver" - default n - - config CHERRYUSB_HOST_CDC_ECM - bool - prompt "Enable usb cdc ecm driver" - select USBHOST_PLATFORM_CDC_ECM - default n - - config CHERRYUSB_HOST_CDC_RNDIS - bool - prompt "Enable usb rndis driver" - select USBHOST_PLATFORM_CDC_RNDIS - default n - - config CHERRYUSB_HOST_CDC_NCM - bool - prompt "Enable usb cdc ncm driver" - select USBHOST_PLATFORM_CDC_NCM - default n - - config CHERRYUSB_HOST_VIDEO - bool - prompt "Enable usb video driver, it is commercial charge" - default n - - config CHERRYUSB_HOST_AUDIO - bool - prompt "Enable usb audio driver, it is commercial charge" - default n - - config CHERRYUSB_HOST_BLUETOOTH - bool - prompt "Enable usb bluetooth driver" - default n - - config CHERRYUSB_HOST_ASIX - bool - prompt "Enable usb asix driver" - select USBHOST_PLATFORM_ASIX - default n - - config CHERRYUSB_HOST_RTL8152 - bool - prompt "Enable usb rtl8152 driver" - select USBHOST_PLATFORM_RTL8152 - default n - - config CHERRYUSB_HOST_FTDI - bool - prompt "Enable usb ftdi driver" - default n - - config CHERRYUSB_HOST_CH34X - bool - prompt "Enable usb ch34x driver" - default n - - config CHERRYUSB_HOST_CP210X - bool - prompt "Enable usb cp210x driver" - default n - - config CHERRYUSB_HOST_PL2303 - bool - prompt "Enable usb pl2303 driver" - default n - - config USBHOST_PLATFORM_CDC_ECM - bool - - config USBHOST_PLATFORM_CDC_RNDIS - bool - - config USBHOST_PLATFORM_CDC_NCM - bool - - config USBHOST_PLATFORM_ASIX - bool - - config USBHOST_PLATFORM_RTL8152 - bool - - config CHERRYUSB_HOST_TEMPLATE - bool - prompt "Use usb host template" - default n - - if CHERRYUSB_HOST_TEMPLATE - config TEST_USBH_CDC_ACM - int - prompt "demo for test cdc acm" - default 0 - depends on CHERRYUSB_HOST_CDC_ACM - config TEST_USBH_HID - int - prompt "demo for test hid" - default 0 - depends on CHERRYUSB_HOST_HID - config TEST_USBH_MSC - int - prompt "demo for test msc" - default 0 - depends on CHERRYUSB_HOST_MSC - endif - endif - -endif diff --git a/components/drivers/usb/cherryusb/osal/usb_osal_rtthread.c b/components/drivers/usb/cherryusb/osal/usb_osal_rtthread.c index e8cbce3a79..d355699c06 100644 --- a/components/drivers/usb/cherryusb/osal/usb_osal_rtthread.c +++ b/components/drivers/usb/cherryusb/osal/usb_osal_rtthread.c @@ -5,6 +5,8 @@ */ #include "usb_osal.h" #include "usb_errno.h" +#include "usb_config.h" +#include "usb_log.h" #include #include @@ -12,6 +14,11 @@ usb_osal_thread_t usb_osal_thread_create(const char *name, uint32_t stack_size, { rt_thread_t htask; htask = rt_thread_create(name, entry, args, stack_size, prio, 10); + if (htask == NULL) { + USB_LOG_ERR("Create thread %s failed\r\n", name); + while (1) { + } + } rt_thread_startup(htask); return (usb_osal_thread_t)htask; } @@ -27,7 +34,13 @@ void usb_osal_thread_delete(usb_osal_thread_t thread) usb_osal_sem_t usb_osal_sem_create(uint32_t initial_count) { - return (usb_osal_sem_t)rt_sem_create("usbh_sem", initial_count, RT_IPC_FLAG_FIFO); + usb_osal_sem_t sem = (usb_osal_sem_t)rt_sem_create("usbh_sem", initial_count, RT_IPC_FLAG_FIFO); + if (sem == NULL) { + USB_LOG_ERR("Create semaphore failed\r\n"); + while (1) { + } + } + return sem; } void usb_osal_sem_delete(usb_osal_sem_t sem) @@ -68,7 +81,13 @@ void usb_osal_sem_reset(usb_osal_sem_t sem) usb_osal_mutex_t usb_osal_mutex_create(void) { - return (usb_osal_mutex_t)rt_mutex_create("usbh_mutex", RT_IPC_FLAG_FIFO); + usb_osal_mutex_t mutex = (usb_osal_mutex_t)rt_mutex_create("usbh_mutex", RT_IPC_FLAG_FIFO); + if (mutex == NULL) { + USB_LOG_ERR("Create mutex failed\r\n"); + while (1) { + } + } + return mutex; } void usb_osal_mutex_delete(usb_osal_mutex_t mutex) @@ -127,11 +146,18 @@ struct usb_osal_timer *usb_osal_timer_create(const char *name, uint32_t timeout_ struct usb_osal_timer *timer; timer = rt_malloc(sizeof(struct usb_osal_timer)); + if (timer == NULL) { + USB_LOG_ERR("Create usb_osal_timer failed\r\n"); + while (1) { + } + } memset(timer, 0, sizeof(struct usb_osal_timer)); timer->timer = (void *)rt_timer_create(name, handler, argument, timeout_ms, is_period ? (RT_TIMER_FLAG_PERIODIC | RT_TIMER_FLAG_SOFT_TIMER) : (RT_TIMER_FLAG_ONE_SHOT | RT_TIMER_FLAG_SOFT_TIMER)); if (timer->timer == NULL) { - return NULL; + USB_LOG_ERR("Create timer failed\r\n"); + while (1) { + } } return timer; } diff --git a/components/drivers/usb/cherryusb/platform/rtthread/usb_check.c b/components/drivers/usb/cherryusb/platform/rtthread/usb_check.c index 79ab320746..be16ea1495 100644 --- a/components/drivers/usb/cherryusb/platform/rtthread/usb_check.c +++ b/components/drivers/usb/cherryusb/platform/rtthread/usb_check.c @@ -7,11 +7,11 @@ #endif #if IDLE_THREAD_STACK_SIZE < 2048 -#error "IDLE_THREAD_STACK_SIZE must be greater than or equal to 2048" +#error "IDLE_THREAD_STACK_SIZE must be >= 2048" #endif #if RT_TIMER_THREAD_STACK_SIZE < 2048 -#error "RT_TIMER_THREAD_STACK_SIZE must be greater than or equal to 2048" +#error "RT_TIMER_THREAD_STACK_SIZE must be >= 2048" #endif #endif diff --git a/components/drivers/usb/cherryusb/platform/rtthread/usbh_dfs.c b/components/drivers/usb/cherryusb/platform/rtthread/usbh_dfs.c index 745ea5ea29..073d52ea4e 100644 --- a/components/drivers/usb/cherryusb/platform/rtthread/usbh_dfs.c +++ b/components/drivers/usb/cherryusb/platform/rtthread/usbh_dfs.c @@ -9,7 +9,7 @@ #include "rtthread.h" #include -#define DEV_FORMAT "/sd%c" +#define DEV_FORMAT "/dev/sd%c" #ifndef CONFIG_USB_DFS_MOUNT_POINT #define CONFIG_USB_DFS_MOUNT_POINT "/" @@ -46,7 +46,7 @@ static rt_err_t rt_udisk_init(rt_device_t dev) } static ssize_t rt_udisk_read(rt_device_t dev, rt_off_t pos, void *buffer, - rt_size_t size) + rt_size_t size) { struct usbh_msc *msc_class = (struct usbh_msc *)dev->user_data; int ret; @@ -85,7 +85,7 @@ static ssize_t rt_udisk_read(rt_device_t dev, rt_off_t pos, void *buffer, } static ssize_t rt_udisk_write(rt_device_t dev, rt_off_t pos, const void *buffer, - rt_size_t size) + rt_size_t size) { struct usbh_msc *msc_class = (struct usbh_msc *)dev->user_data; int ret; @@ -175,15 +175,17 @@ int udisk_init(struct usbh_msc *msc_class) ret = usbh_msc_scsi_read10(msc_class, 0, msc_sector, 1); if (ret != RT_EOK) { rt_kprintf("usb mass_storage read failed\n"); + rt_free(dev); return ret; } - for (i = 0; i < 16; i++) { + for (i = 0; i < 4; i++) { /* Get the first partition */ ret = dfs_filesystem_get_partition(&part0, msc_sector, i); if (ret == RT_EOK) { rt_kprintf("Found partition %d: type = %d, offet=0x%x, size=0x%x\n", i, part0.type, part0.offset, part0.size); + break; } else { break; } @@ -219,6 +221,8 @@ void usbh_msc_run(struct usbh_msc *msc_class) void usbh_msc_stop(struct usbh_msc *msc_class) { + struct rt_device *dev; + char name[CONFIG_USBHOST_DEV_NAMELEN]; char mount_point[CONFIG_USBHOST_DEV_NAMELEN]; @@ -226,5 +230,9 @@ void usbh_msc_stop(struct usbh_msc *msc_class) snprintf(mount_point, CONFIG_USBHOST_DEV_NAMELEN, CONFIG_USB_DFS_MOUNT_POINT, msc_class->sdchar); dfs_unmount(mount_point); - rt_device_unregister(rt_device_find(name)); + dev = rt_device_find(name); + if (dev) { + rt_device_unregister(dev); + rt_free(dev); + } } diff --git a/components/drivers/usb/cherryusb/platform/rtthread/usbh_lwip.c b/components/drivers/usb/cherryusb/platform/rtthread/usbh_lwip.c index 360a848953..1211b337d1 100644 --- a/components/drivers/usb/cherryusb/platform/rtthread/usbh_lwip.c +++ b/components/drivers/usb/cherryusb/platform/rtthread/usbh_lwip.c @@ -44,6 +44,10 @@ #error PBUF_POOL_BUFSIZE must be larger than 1600 #endif +#if RT_LWIP_TCPTHREAD_STACKSIZE < 2048 +#error RT_LWIP_TCPTHREAD_STACKSIZE must be larger than 2048 +#endif + // #define CONFIG_USBHOST_PLATFORM_CDC_ECM // #define CONFIG_USBHOST_PLATFORM_CDC_RNDIS // #define CONFIG_USBHOST_PLATFORM_CDC_NCM