drivers/fs:Always use register_mtddriver() to register the MTD device.

We have adjusted the registration method for MTD devices
in nuttx/boards, replacing the previous approach using
ftl_initialize() and bchdev_register() with
register_mtddriver().

When registering MTD devices via register_mtddriver(),
FTL and BCH wrappers will be added during the open() process:

1. Character Device Mode:
   When accessing the MTD device node via the open() interface,
   the device will be automatically converted to a character
   device. Both FTL and BCH wrappers will be implicitly added,
   provided that BCH support is enabled in the configuration.

2. Block Device Mode:
   When accessing the MTD device node via open_blockdriver(),
   the device will be treated as a block device, with only
   the FTL wrapper automatically applied.

Due to the automatic wrapping of MTD devices during the
open() process, the legacy registration methods
ftl_initialize() and bchdev_register() are no longer
required for MTD device registration for user code and should
be used only internally within fs and driver code.

Signed-off-by: jingfei <jingfei@xiaomi.com>
This commit is contained in:
jingfei
2025-07-10 22:53:57 +08:00
committed by Xiang Xiao
parent a754b73e4c
commit d12cf1cb75
17 changed files with 67 additions and 264 deletions
@@ -88,12 +88,11 @@ struct mtd_dev_s *g_mtd_fs;
int s32k3xx_bringup(void)
{
int ret = OK;
#if defined(CONFIG_BCH) || defined(HAVE_MX25L_LITTLEFS)
#if defined(HAVE_MX25L_LITTLEFS)
char blockdev[32];
# if !defined(HAVE_MX25L_LITTLEFS) && !defined(HAVE_MX25L_NXFFS)
char chardev[32];
# endif /* !HAVE_MX25L_LITTLEFS && !HAVE_MX25L_NXFFS */
#endif /* CONFIG_BCH || HAVE_MX25L_LITTLEFS */
#elif defined(HAVE_MX25L_CHARDEV)
char mtddev[32];
#endif /* HAVE_MX25L_LITTLEFS */
#ifdef CONFIG_S32K3XX_LPSPI
/* Initialize SPI driver */
@@ -285,38 +284,17 @@ int s32k3xx_bringup(void)
}
# else /* if defined(HAVE_MX25L_CHARDEV) */
/* Use the FTL layer to wrap the MTD driver as a block driver */
/* Use the minor number to create device paths */
ret = ftl_initialize(MX25L_MTD_MINOR, g_mtd_fs);
snprintf(mtddev, sizeof(mtddev), "/dev/mtd%d", MX25L_MTD_MINOR);
/* Register the MTD driver */
ret = register_mtddriver(mtddev, g_mtd_fs, 0755, NULL);
if (ret < 0)
{
_err("ftl_initialize() failed: %d\n", ret);
_err("register_mtddriver for %s failed: %d\n", mtddev, ret);
}
# ifdef CONFIG_BCH
else
{
_info("ftl_initialize() successful\n");
/* Use the minor number to create device paths */
snprintf(blockdev, sizeof(blockdev), "/dev/mtdblock%d",
MX25L_MTD_MINOR);
snprintf(chardev, sizeof(chardev), "/dev/mtd%d",
MX25L_MTD_MINOR);
/* Now create a character device on the block device */
ret = bchdev_register(blockdev, chardev, false);
if (ret < 0)
{
_err("bchdev_register %s failed: %d\n", chardev, ret);
}
else
{
_info("bchdev_register %s successful\n", chardev);
}
}
# endif /* CONFIG_BCH */
# endif
}
}
+6 -15
View File
@@ -157,24 +157,15 @@ config SAMA5D4EK_AT25_BLOCKMOUNT
choice
prompt "AT25 serial FLASH configuration"
default SAMA5D4EK_AT25_FTL
default SAMA5D4EK_AT25_MTD
depends on SAMA5D4EK_AT25_BLOCKMOUNT
config SAMA5D4EK_AT25_FTL
bool "Create AT25 Serial FLASH block driver"
config SAMA5D4EK_AT25_MTD
bool "Create AT25 Serial FLASH MTD driver"
---help---
Create the MTD driver for the AT25 and "wrap" the AT25 as a standard
block driver that could then, for example, be mounted using FAT or
any other file system. Any file system may be used, but there will
be no wear-leveling.
config SAMA5D4EK_AT25_CHARDEV
bool "Create AT25 Serial FLASH character driver"
---help---
Create the MTD driver for the AT25 and "wrap" the AT25 as a standard
character driver that could then, for example, via simple open, close,
read, write file system operations. There will be no wear-leveling
in this configuration.
Create the MTD driver for the AT25 that could then, for example,
be mounted using FAT or any other file system. Any file system may be
used, but there will be no wear-leveling.
config SAMA5D4EK_AT25_NXFFS
bool "Create AT25 serial FLASH NXFFS file system"
@@ -39,7 +39,6 @@ CONFIG_RAW_BINARY=y
CONFIG_RR_INTERVAL=200
CONFIG_SAMA5D4EK_528MHZ=y
CONFIG_SAMA5D4EK_AT25_BLOCKMOUNT=y
CONFIG_SAMA5D4EK_AT25_CHARDEV=y
CONFIG_SAMA5D4EK_AT25_MAIN=y
CONFIG_SAMA5_DDRCS=y
CONFIG_SAMA5_DDRCS_LPDDR2=y
+2 -2
View File
@@ -55,8 +55,8 @@
# error CONFIG_SAMA5D4EK_AT25_BLOCKMOUNT must be selected
#endif
#ifndef CONFIG_SAMA5D4EK_AT25_CHARDEV
# error CONFIG_SAMA5D4EK_AT25_CHARDEV must be selected
#ifndef CONFIG_SAMA5D4EK_AT25_MTD
# error CONFIG_SAMA5D4EK_AT25_MTD must be selected
#endif
#ifdef CONFIG_BOOT_SDRAM_DATA
+7 -34
View File
@@ -62,11 +62,8 @@ int sam_at25_automount(int minor)
{
struct spi_dev_s *spi;
struct mtd_dev_s *mtd;
#ifdef CONFIG_SAMA5D4EK_AT25_CHARDEV
#if defined(CONFIG_BCH)
char blockdev[18];
char chardev[12];
#endif /* defined(CONFIG_BCH) */
#ifdef CONFIG_SAMA5D4EK_AT25_MTD
char mtddev[12];
#endif
static bool initialized = false;
int ret;
@@ -94,43 +91,19 @@ int sam_at25_automount(int minor)
return -ENODEV;
}
#if defined(CONFIG_SAMA5D4EK_AT25_FTL)
/* And finally, use the FTL layer to wrap the MTD driver as a block
* driver.
*/
ret = ftl_initialize(minor, mtd);
if (ret < 0)
{
ferr("ERROR: Failed to initialize the FTL layer: %d\n", ret);
return ret;
}
#elif defined(CONFIG_SAMA5D4EK_AT25_CHARDEV)
/* Use the FTL layer to wrap the MTD driver as a block driver */
ret = ftl_initialize(minor, mtd);
if (ret < 0)
{
ferr("ERROR: Failed to initialize the FTL layer: %d\n", ret);
return ret;
}
#if defined(CONFIG_BCH)
#if defined(CONFIG_SAMA5D4EK_AT25_MTD)
/* Use the minor number to create device paths */
snprintf(blockdev, sizeof(blockdev), "/dev/mtdblock%d", minor);
snprintf(chardev, sizeof(chardev), "/dev/mtd%d", minor);
snprintf(mtddev, sizeof(mtddev), "/dev/mtd%d", minor);
/* Now create a character device on the block device */
/* Register the MTD driver */
ret = bchdev_register(blockdev, chardev, false);
ret = register_mtddriver(mtddev, mtd, 0755, NULL);
if (ret < 0)
{
ferr("ERROR: bchdev_register %s failed: %d\n", chardev, ret);
ferr("register_mtddriver %s failed: %d\n", mtddev, ret);
return ret;
}
#endif /* defined(CONFIG_BCH) */
#elif defined(CONFIG_SAMA5D4EK_AT25_NXFFS)
/* Initialize to provide NXFFS on the MTD interface */
+2 -2
View File
@@ -46,8 +46,8 @@
# error AT25 Serial FLASH not supported
#endif
#ifndef CONFIG_SAMA5D4EK_AT25_FTL
# error AT25 FTL support required (CONFIG_SAMA5D4EK_AT25_FTL)
#ifndef CONFIG_SAMA5D4EK_AT25_MTD
# error AT25 MTD support required (CONFIG_SAMA5D4EK_AT25_MTD)
# undef HAVE_AT25
#endif
+3 -16
View File
@@ -152,25 +152,12 @@
# undef CONFIG_SAMA5D4EK_AT25_NXFFS
#endif
#if !defined(CONFIG_SAMA5D4EK_AT25_FTL) && !defined(CONFIG_SAMA5D4EK_AT25_CHARDEV) && \
!defined(CONFIG_SAMA5D4EK_AT25_NXFFS)
#if !defined(CONFIG_SAMA5D4EK_AT25_MTD) && !defined(CONFIG_SAMA5D4EK_AT25_NXFFS)
# undef HAVE_AT25
#endif
#if defined(CONFIG_SAMA5D4EK_AT25_FTL) && defined(CONFIG_SAMA5D4EK_AT25_CHARDEV)
# warning Both CONFIG_SAMA5D4EK_AT25_CHARDEV and CONFIG_SAMA5D4EK_AT25_FTL are set
# warning Ignoring CONFIG_SAMA5D4EK_AT25_FTL
# undef CONFIG_SAMA5D4EK_AT25_FTL
#endif
#if defined(CONFIG_SAMA5D4EK_AT25_FTL) && defined(CONFIG_SAMA5D4EK_AT25_NXFFS)
# warning Both CONFIG_SAMA5D4EK_AT25_FTL and CONFIG_SAMA5D4EK_AT25_NXFFS are set
# warning Ignoring CONFIG_SAMA5D4EK_AT25_NXFFS
# undef CONFIG_SAMA5D4EK_AT25_NXFFS
#endif
#if defined(CONFIG_SAMA5D4EK_AT25_CHARDEV) && defined(CONFIG_SAMA5D4EK_AT25_NXFFS)
# warning Both CONFIG_SAMA5D4EK_AT25_CHARDEV and CONFIG_SAMA5D4EK_AT25_NXFFS are set
#if defined(CONFIG_SAMA5D4EK_AT25_MTD) && defined(CONFIG_SAMA5D4EK_AT25_NXFFS)
# warning Both CONFIG_SAMA5D4EK_AT25_MTD and CONFIG_SAMA5D4EK_AT25_NXFFS are set
# warning Ignoring CONFIG_SAMA5D4EK_AT25_NXFFS
# undef CONFIG_SAMA5D4EK_AT25_NXFFS
#endif
+6 -21
View File
@@ -66,40 +66,25 @@
static int sam_progmem_register_driver(int minor, struct mtd_dev_s *mtd,
const char *devpath)
{
#ifdef CONFIG_BCH
char blockdev[18];
char chardev[12];
#endif
char mtddev[12];
int ret = OK;
/* Use the FTL layer to wrap the MTD driver as a block driver */
ret = ftl_initialize(minor, mtd);
if (ret < 0)
{
ferr("ERROR: Failed to initialize the FTL layer: %d\n", ret);
return ret;
}
#ifdef CONFIG_BCH
/* Use the minor number to create device paths */
snprintf(blockdev, sizeof(blockdev), "/dev/mtdblock%d", minor);
if (devpath == NULL)
{
snprintf(chardev, sizeof(chardev), "/dev/mtd%d", minor);
devpath = chardev;
snprintf(mtddev, sizeof(mtddev), "/dev/mtd%d", minor);
devpath = mtddev;
}
/* Now create a character device on the block device */
/* Register the MTD driver */
ret = bchdev_register(blockdev, devpath, false);
ret = register_mtddriver(devpath, mtd, 0755, NULL);
if (ret < 0)
{
ferr("ERROR: bchdev_register %s failed: %d\n", devpath, ret);
ferr("ERROR: register_mtddriver %s failed: %d\n", devpath, ret);
return ret;
}
#endif
return ret;
}
+6 -23
View File
@@ -218,10 +218,7 @@ int sam_bringup(void)
struct i2c_master_s *i2c;
#endif
#if defined(HAVE_S25FL1_CHARDEV)
#if defined(CONFIG_BCH)
char blockdev[18];
char chardev[12];
#endif /* defined(CONFIG_BCH) */
char mtddev[12];
#endif
int ret;
@@ -457,33 +454,19 @@ int sam_bringup(void)
}
#else /* if defined(HAVE_S25FL1_CHARDEV) */
/* Use the FTL layer to wrap the MTD driver as a block driver */
ret = ftl_initialize(S25FL1_MTD_MINOR, mtd);
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: Failed to initialize the FTL layer: %d\n",
ret);
return ret;
}
#if defined(CONFIG_BCH)
/* Use the minor number to create device paths */
snprintf(blockdev, sizeof(blockdev), "/dev/mtdblock%d",
S25FL1_MTD_MINOR);
snprintf(chardev, sizeof(chardev), "/dev/mtd%d", S25FL1_MTD_MINOR);
snprintf(mtddev, sizeof(mtddev), "/dev/mtd%d", S25FL1_MTD_MINOR);
/* Now create a character device on the block device */
/* Register the MTD driver */
ret = bchdev_register(blockdev, chardev, false);
ret = register_mtddriver(mtddev, mtd, 0755, NULL);
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: bchdev_register %s failed: %d\n",
chardev, ret);
syslog(LOG_ERR, "ERROR: register_mtddriver %s failed: %d\n",
mtddev, ret);
return ret;
}
#endif /* defined(CONFIG_BCH) */
#endif
}
#endif
@@ -106,10 +106,7 @@ int stm32_bringup(void)
struct mtd_dev_s *mtd_temp;
#endif
#if defined(HAVE_N25QXXX_CHARDEV)
#if defined(CONFIG_BCH)
char blockdev[18];
char chardev[12];
#endif /* defined(CONFIG_BCH) */
char mtddev[12];
#endif
int ret = OK;
@@ -231,31 +228,18 @@ int stm32_bringup(void)
}
#else /* if defined(HAVE_N25QXXX_CHARDEV) */
/* Use the FTL layer to wrap the MTD driver as a block driver */
ret = ftl_initialize(N25QXXX_MTD_MINOR, g_mtd_fs);
if (ret < 0)
{
_err("ERROR: Failed to initialize the FTL layer: %d\n", ret);
return ret;
}
#if defined(CONFIG_BCH)
/* Use the minor number to create device paths */
snprintf(blockdev, sizeof(blockdev), "/dev/mtdblock%d",
N25QXXX_MTD_MINOR);
snprintf(chardev, sizeof(chardev), "/dev/mtd%d", N25QXXX_MTD_MINOR);
snprintf(mtddev, sizeof(mtddev), "/dev/mtd%d", N25QXXX_MTD_MINOR);
/* Now create a character device on the block device */
/* Register the MTD driver */
ret = bchdev_register(blockdev, chardev, false);
ret = register_mtddriver(mtddev, g_mtd_fs, 0755, NULL);
if (ret < 0)
{
_err("ERROR: bchdev_register %s failed: %d\n", chardev, ret);
_err("ERROR: register_mtddriver %s failed: %d\n", mtddev, ret);
return ret;
}
#endif /* defined(CONFIG_BCH) */
#endif
}
#endif
@@ -138,9 +138,6 @@ static const struct ota_partition_s g_ota_partition_table[] =
static int init_ota_partitions(void)
{
struct mtd_dev_s *mtd;
#ifdef CONFIG_BCH
char blockdev[18];
#endif
int ret = OK;
for (int i = 0; i < nitems(g_ota_partition_table); ++i)
@@ -149,23 +146,13 @@ static int init_ota_partitions(void)
mtd = esp32c3_spiflash_alloc_mtdpart(part->offset, part->size,
OTA_ENCRYPT);
ret = ftl_initialize(i, mtd);
ret = register_mtddriver(part->devpath, mtd, 0755, NULL);
if (ret < 0)
{
ferr("ERROR: Failed to initialize the FTL layer: %d\n", ret);
ferr("ERROR: register_mtddriver %s failed: %d\n",
part->devpath, ret);
return ret;
}
#ifdef CONFIG_BCH
snprintf(blockdev, sizeof(blockdev), "/dev/mtdblock%d", i);
ret = bchdev_register(blockdev, part->devpath, false);
if (ret < 0)
{
ferr("ERROR: bchdev_register %s failed: %d\n", part->devpath, ret);
return ret;
}
#endif
}
return ret;
@@ -125,32 +125,18 @@ static int init_ota_partitions(void)
int ret = OK;
int i;
#ifdef CONFIG_BCH
char blockdev[18];
#endif
for (i = 0; i < nitems(g_ota_partition_table); ++i)
{
const struct ota_partition_s *part = &g_ota_partition_table[i];
mtd = esp_spiflash_alloc_mtdpart(part->offset, part->size);
ret = ftl_initialize(i, mtd);
ret = register_mtddriver(part->devpath, mtd, 0755, NULL);
if (ret < 0)
{
ferr("ERROR: Failed to initialize the FTL layer: %d\n", ret);
ferr("ERROR: register_mtddriver %s failed: %d\n",
part->devpath, ret);
return ret;
}
#ifdef CONFIG_BCH
snprintf(blockdev, sizeof(blockdev), "/dev/mtdblock%d", i);
ret = bchdev_register(blockdev, part->devpath, false);
if (ret < 0)
{
ferr("ERROR: bchdev_register %s failed: %d\n", part->devpath, ret);
return ret;
}
#endif
}
return ret;
@@ -119,9 +119,6 @@ static const struct ota_partition_s g_ota_partition_table[] =
static int init_ota_partitions(void)
{
struct mtd_dev_s *mtd;
#ifdef CONFIG_BCH
char blockdev[18];
#endif
int ret = OK;
for (int i = 0; i < nitems(g_ota_partition_table); ++i)
@@ -130,25 +127,13 @@ static int init_ota_partitions(void)
mtd = esp32_spiflash_alloc_mtdpart(part->offset, part->size,
OTA_ENCRYPT);
ret = ftl_initialize(i, mtd);
ret = register_mtddriver(part->devpath, mtd, 0755, NULL);
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: Failed to initialize the FTL layer: %d\n",
ret);
return ret;
}
#ifdef CONFIG_BCH
snprintf(blockdev, sizeof(blockdev), "/dev/mtdblock%d", i);
ret = bchdev_register(blockdev, part->devpath, false);
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: bchdev_register %s failed: %d\n",
syslog(LOG_ERR, "register_mtddriver %s failed: %d\n",
part->devpath, ret);
return ret;
}
#endif
}
return ret;
+2 -13
View File
@@ -68,22 +68,11 @@ config Z20X_W25_PROGSIZE
this program must run from SRAM, there would be no purpose int
making this size any larger than the size of the internal SRAM.
choice
prompt "Winbond W25 Usage"
default Z20X_W25_CHARDEV
depends on EZ80_SPI && MTD_W25
config Z20X_W25_CHARDEV
bool "Character device"
depends on EZ80_SPI && MTD_W25
select BCH
config Z20X_W25_BLOCKDEV
bool "Block device"
config Z20X_W25_MTDDEV
bool "MTD device"
endchoice
default y
config Z20X_W25_MINOR
int "W25 device minor number"
+3 -26
View File
@@ -74,37 +74,14 @@ int ez80_w25_initialize(int minor)
return -ENODEV;
}
#if defined(CONFIG_Z20X_W25_BLOCKDEV)
/* Use the FTL layer to wrap the MTD driver as a block driver. */
/* Register the MTD driver */
ret = ftl_initialize(minor, mtd);
ret = register_mtddriver(W25_DEV, mtd, 0755, NULL);
if (ret < 0)
{
ferr("ERROR: Failed to initialize the FTL layer: %d\n", ret);
ferr("ERROR: register_mtddriver %s failed: %d\n", W25_DEV, ret);
return ret;
}
#elif defined(CONFIG_Z20X_W25_CHARDEV)
/* Use the FTL layer to wrap the MTD driver as a block driver */
ret = ftl_initialize(minor, mtd);
if (ret < 0)
{
ferr("ERROR: Failed to initialize the FTL layer: %d\n", ret);
return ret;
}
#if defined(CONFIG_BCH)
/* Create a character device on the block device */
ret = bchdev_register(W25_BLOCKDEV, W25_CHARDEV, false);
if (ret < 0)
{
ferr("ERROR: bchdev_register %s failed: %d\n", W25_CHARDEV, ret);
return ret;
}
#endif /* defined(CONFIG_BCH) */
#endif
return OK;
}
+5 -5
View File
@@ -103,11 +103,11 @@ static int w25_read_hex(FAR uint24_t *len)
/* Open the W25 device for writing */
fd = open(W25_CHARDEV, O_WRONLY);
fd = open(W25_DEV, O_WRONLY);
if (fd < 0)
{
ret = -errno;
fprintf(stderr, "ERROR: Failed to open %s: %d\n", W25_CHARDEV, ret);
fprintf(stderr, "ERROR: Failed to open %s: %d\n", W25_DEV, ret);
return ret;
}
@@ -206,11 +206,11 @@ static int w25_write_binary(FAR const struct prog_header_s *hdr)
/* Open the W25 device for writing */
fd = open(W25_CHARDEV, O_WRONLY);
fd = open(W25_DEV, O_WRONLY);
if (fd < 0)
{
ret = -errno;
fprintf(stderr, "ERROR: Failed to open %s: %d\n", W25_CHARDEV, ret);
fprintf(stderr, "ERROR: Failed to open %s: %d\n", W25_DEV, ret);
return ret;
}
@@ -290,7 +290,7 @@ static int w25_read_binary(FAR struct prog_header_s *hdr)
/* Open the W25 device for reading */
fd = open(W25_CHARDEV, O_RDONLY);
fd = open(W25_DEV, O_RDONLY);
if (fd < 0)
{
ret = -errno;
+1 -2
View File
@@ -154,8 +154,7 @@ extern uint8_t _progend[];
#define __STR(s) #s
#define __XSTR(s) __STR(s)
#define W25_CHARDEV "/dev/mtd" __XSTR(CONFIG_Z20X_W25_MINOR)
#define W25_BLOCKDEV "/dev/mtdblock" __XSTR(CONFIG_Z20X_W25_MINOR)
#define W25_DEV "/dev/mtd" __XSTR(CONFIG_Z20X_W25_MINOR)
/****************************************************************************
* Public Function Prototypes