From a783a159af7a47c72ca67d55a8b84c0928f838cb Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Tue, 11 Apr 2023 18:43:27 +0200 Subject: [PATCH] sys/usbus/msc: use MTD pointers in XFA for LUN handling --- sys/include/usb/usbus/msc.h | 14 +------------- sys/usb/usbus/msc/msc.c | 16 ++++++++++------ 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/sys/include/usb/usbus/msc.h b/sys/include/usb/usbus/msc.h index b4048822a3..58e2e57d2c 100644 --- a/sys/include/usb/usbus/msc.h +++ b/sys/include/usb/usbus/msc.h @@ -50,17 +50,6 @@ extern "C" { */ #define USBUS_MSC_EP_OUT_REQUIRED_NUMOF 1 -/** - * @brief USBUS MSC Number of exported MTD device through USB - */ -#ifndef USBUS_MSC_EXPORTED_NUMOF -#ifdef MTD_NUMOF -#define USBUS_MSC_EXPORTED_NUMOF MTD_NUMOF -#else -#define USBUS_MSC_EXPORTED_NUMOF 0 -#endif -#endif /* USBUS_MSC_EXPORTED_NUMOF */ - /** * @brief USBUS MSC internal state machine enum */ @@ -102,8 +91,7 @@ typedef struct usbus_msc_device { uint16_t block_nb; /**< Number of block to transfer for READ and WRITE operations */ uint16_t block_offset; /**< Internal offset for endpoint size chunk transfer */ - usbus_msc_lun_t lun_dev[USBUS_MSC_EXPORTED_NUMOF]; /**< Array holding exported logical - unit descriptor */ + usbus_msc_lun_t *lun_dev; /**< Array holding exported logical unit descriptor */ } usbus_msc_device_t; /** diff --git a/sys/usb/usbus/msc/msc.c b/sys/usb/usbus/msc/msc.c index 05198ceae6..dec3581360 100644 --- a/sys/usb/usbus/msc/msc.c +++ b/sys/usb/usbus/msc/msc.c @@ -198,7 +198,7 @@ static unsigned _get_lun(usbus_msc_device_t *msc) /* Count only registered MTD devices as USB LUN, (using usbus_msc_add_lun) * not every MTD devices available on board */ - for (int i = 0; i < USBUS_MSC_EXPORTED_NUMOF; i++) { + for (unsigned i = 0; i < MTD_NUMOF; i++) { if (msc->lun_dev[i].mtd != NULL) { count++; } @@ -217,14 +217,14 @@ int usbus_msc_add_lun(usbus_t *usbus, mtd_dev_t *dev) } /* Check if MTD isn't already registered */ - for (int i = 0; i < USBUS_MSC_EXPORTED_NUMOF; i++) { + for (unsigned i = 0; i < MTD_NUMOF; i++) { if (dev == msc->lun_dev[i].mtd) { return -EBUSY; } } /* Store new MTD device in first slot available Also re alloc internal buffer if needed */ - for (int i = 0; i < USBUS_MSC_EXPORTED_NUMOF; i++) { + for (unsigned i = 0; i < MTD_NUMOF; i++) { if (msc->lun_dev[i].mtd == NULL) { int ret = mtd_init(dev); if (ret != 0) { @@ -283,7 +283,7 @@ int usbus_msc_remove_lun(usbus_t *usbus, mtd_dev_t *dev) usbus_msc_device_t *msc = _get_msc_handler(usbus); /* Identify the LUN to unexport */ - for (int i = 0; i < USBUS_MSC_EXPORTED_NUMOF; i++) { + for (unsigned i = 0; i < MTD_NUMOF; i++) { if (dev == msc->lun_dev[i].mtd) { /* Wait for any pending transaction to end */ @@ -329,7 +329,11 @@ static void _init(usbus_t *usbus, usbus_handler_t *handler) msc->block = 0; msc->state = WAITING; - for (int i = 0; i < USBUS_MSC_EXPORTED_NUMOF; i++) { + /* allocate the array holding exported logical unit descriptors */ + msc->lun_dev = malloc(sizeof(usbus_msc_lun_t) * MTD_NUMOF); + assert(msc->lun_dev); + + for (unsigned i = 0; i < MTD_NUMOF; i++) { msc->lun_dev[i].mtd = NULL; msc->lun_dev[i].block_size = 0; msc->lun_dev[i].block_nb = 0; @@ -369,7 +373,7 @@ static void _init(usbus_t *usbus, usbus_handler_t *handler) /* Auto-configure all MTD devices */ if (CONFIG_USBUS_MSC_AUTO_MTD) { - for (int i = 0; i < USBUS_MSC_EXPORTED_NUMOF; i++) { + for (unsigned i = 0; i < MTD_NUMOF; i++) { usbus_msc_add_lun(usbus, mtd_default_get_dev(i)); } }