From ab3ed439c8535b218ea17cdd7d5db8fcd7595135 Mon Sep 17 00:00:00 2001 From: Laczen JMS Date: Sun, 10 Nov 2019 12:09:44 +0100 Subject: [PATCH] drivers: eeprom: Unified simulator and native_posix EEPROM simulator and native_posix have been unified to one solution, the old eeprom,native_posix is removed. Signed-off-by: Laczen JMS --- boards/posix/native_posix/Kconfig.defconfig | 2 +- boards/posix/native_posix/native_posix.dts | 2 +- drivers/eeprom/CMakeLists.txt | 1 - drivers/eeprom/Kconfig | 6 - drivers/eeprom/eeprom_native_posix.c | 209 ------------------ drivers/eeprom/eeprom_simulator.c | 130 +++++++++-- .../mtd/zephyr,native-posix-eeprom.yaml | 12 - 7 files changed, 115 insertions(+), 247 deletions(-) delete mode 100644 drivers/eeprom/eeprom_native_posix.c delete mode 100644 dts/bindings/mtd/zephyr,native-posix-eeprom.yaml diff --git a/boards/posix/native_posix/Kconfig.defconfig b/boards/posix/native_posix/Kconfig.defconfig index 59d4349a038..89e48ecca75 100644 --- a/boards/posix/native_posix/Kconfig.defconfig +++ b/boards/posix/native_posix/Kconfig.defconfig @@ -104,7 +104,7 @@ endif # USB if EEPROM -config EEPROM_NATIVE_POSIX +config EEPROM_SIMULATOR default y endif # EEPROM diff --git a/boards/posix/native_posix/native_posix.dts b/boards/posix/native_posix/native_posix.dts index dfb82429595..5d14ca12fee 100644 --- a/boards/posix/native_posix/native_posix.dts +++ b/boards/posix/native_posix/native_posix.dts @@ -70,7 +70,7 @@ eeprom0: eeprom { status = "okay"; - compatible = "zephyr,native-posix-eeprom"; + compatible = "zephyr,sim-eeprom"; label = "EEPROM_0"; size = ; }; diff --git a/drivers/eeprom/CMakeLists.txt b/drivers/eeprom/CMakeLists.txt index 61f684a3aaf..dadb22b68fe 100644 --- a/drivers/eeprom/CMakeLists.txt +++ b/drivers/eeprom/CMakeLists.txt @@ -5,7 +5,6 @@ zephyr_library() zephyr_library_sources_ifdef(CONFIG_USERSPACE eeprom_handlers.c) zephyr_library_sources_ifdef(CONFIG_EEPROM_SHELL eeprom_shell.c) -zephyr_library_sources_ifdef(CONFIG_EEPROM_NATIVE_POSIX eeprom_native_posix.c) zephyr_library_sources_ifdef(CONFIG_EEPROM_AT2X eeprom_at2x.c) zephyr_library_sources_ifdef(CONFIG_EEPROM_STM32 eeprom_stm32.c) zephyr_library_sources_ifdef(CONFIG_EEPROM_SIMULATOR eeprom_simulator.c) diff --git a/drivers/eeprom/Kconfig b/drivers/eeprom/Kconfig index 47ca342898d..0d62d441e34 100644 --- a/drivers/eeprom/Kconfig +++ b/drivers/eeprom/Kconfig @@ -28,12 +28,6 @@ config EEPROM_SHELL_BUFFER_SIZE Size of the buffer used for EEPROM read/write commands in the EEPROM shell. -config EEPROM_NATIVE_POSIX - bool "Native POSIX EEPROM driver" - depends on BOARD_NATIVE_POSIX - help - Enable Native POSIX EEPROM driver. - config EEPROM_AT2X bool help diff --git a/drivers/eeprom/eeprom_native_posix.c b/drivers/eeprom/eeprom_native_posix.c deleted file mode 100644 index 53c5ffde3f7..00000000000 --- a/drivers/eeprom/eeprom_native_posix.c +++ /dev/null @@ -1,209 +0,0 @@ -/* - * Copyright (c) 2019 Vestas Wind Systems A/S - * - * Heavily based on flash_native_posix.c, which is: - * Copyright (c) 2019 Jan Van Winkel - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include "cmdline.h" -#include "soc.h" - -#define LOG_LEVEL CONFIG_EEPROM_LOG_LEVEL -#include -LOG_MODULE_REGISTER(eeprom_native_posix); - -static const char default_eeprom_path[] = "eeprom.bin"; - -struct eeprom_native_posix_data { - const char *path; - int fd; - u8_t *eeprom; - bool init_called; -}; - -struct eeprom_native_posix_config { - size_t size; - bool readonly; -}; - -#define DEV_NAME(dev) ((dev)->config->name) -#define DEV_CONFIG(dev) ((dev)->config->config_info) -#define DEV_DATA(dev) \ - ((struct eeprom_native_posix_data *const)(dev)->driver_data) - -static int eeprom_native_posix_read(struct device *dev, off_t offset, - void *buf, size_t len) -{ - struct eeprom_native_posix_data *const data = DEV_DATA(dev); - const struct eeprom_native_posix_config *config = DEV_CONFIG(dev); - - if (!len) { - return 0; - } - - if ((offset + len) > config->size) { - LOG_WRN("attempt to read past device boundary"); - return -EINVAL; - } - - if (data->eeprom == MAP_FAILED) { - LOG_ERR("no EEPROM device mapped"); - return -EIO; - } - - memcpy(buf, data->eeprom + offset, len); - - return 0; -} - -static int eeprom_native_posix_write(struct device *dev, off_t offset, - const void *buf, size_t len) -{ - struct eeprom_native_posix_data *const data = DEV_DATA(dev); - const struct eeprom_native_posix_config *config = DEV_CONFIG(dev); - - if (config->readonly) { - LOG_WRN("attempt to write to read-only device"); - return -EACCES; - } - - if (!len) { - return 0; - } - - if ((offset + len) > config->size) { - LOG_WRN("attempt to write past device boundary"); - return -EINVAL; - } - - if (data->eeprom == MAP_FAILED) { - LOG_ERR("no EEPROM device mapped"); - return -EIO; - } - - memcpy(data->eeprom + offset, buf, len); - - return 0; -} - - -static size_t eeprom_native_posix_size(struct device *dev) -{ - const struct eeprom_native_posix_config *config = DEV_CONFIG(dev); - - return config->size; -} - -static int eeprom_native_posix_init(struct device *dev) -{ - struct eeprom_native_posix_data *const data = DEV_DATA(dev); - const struct eeprom_native_posix_config *config = DEV_CONFIG(dev); - - data->init_called = true; - - if (data->path == NULL) { - data->path = default_eeprom_path; - } - - data->fd = open(data->path, O_RDWR | O_CREAT, (mode_t)0600); - if (data->fd == -1) { - posix_print_warning("failed to open EEPROM device file " - "%s: %s\n", - data->path, strerror(errno)); - return -EIO; - } - - if (ftruncate(data->fd, config->size) == -1) { - posix_print_warning("failed to resize EEPROM device file " - "%s: %s\n", - data->path, strerror(errno)); - return -EIO; - } - - data->eeprom = mmap(NULL, config->size, PROT_WRITE | PROT_READ, - MAP_SHARED, data->fd, 0); - if (data->eeprom == MAP_FAILED) { - posix_print_warning("failed to mmap EEPROM device file " - "%s: %s\n", - data->path, strerror(errno)); - return -EIO; - } - - return 0; -} - -static const struct eeprom_driver_api eeprom_native_posix_driver_api = { - .read = eeprom_native_posix_read, - .write = eeprom_native_posix_write, - .size = eeprom_native_posix_size, -}; - -static const struct eeprom_native_posix_config eeprom_native_posix_config_0 = { - .size = DT_INST_0_ZEPHYR_NATIVE_POSIX_EEPROM_SIZE, - .readonly = DT_INST_0_ZEPHYR_NATIVE_POSIX_EEPROM_READ_ONLY, -}; - -static struct eeprom_native_posix_data eeprom_native_posix_data_0; - -DEVICE_AND_API_INIT(eeprom_native_posix_0, - DT_INST_0_ZEPHYR_NATIVE_POSIX_EEPROM_LABEL, - &eeprom_native_posix_init, &eeprom_native_posix_data_0, - &eeprom_native_posix_config_0, POST_KERNEL, - CONFIG_KERNEL_INIT_PRIORITY_DEVICE, - &eeprom_native_posix_driver_api); - -static void eeprom_native_posix_cleanup_0(void) -{ - struct eeprom_native_posix_data *const data = - &eeprom_native_posix_data_0; - const struct eeprom_native_posix_config *config = - &eeprom_native_posix_config_0; - - if (!data->init_called) { - return; - } - - if (data->eeprom != MAP_FAILED) { - munmap(data->eeprom, config->size); - } - - if (data->fd != -1) { - close(data->fd); - } -} - -void eeprom_native_posix_options_0(void) -{ - static struct args_struct_t eeprom_options[] = { - { - .manual = false, - .is_mandatory = false, - .is_switch = false, - .option = "eeprom", - .name = "path", - .type = 's', - .dest = (void *)&eeprom_native_posix_data_0.path, - .call_when_found = NULL, - .descript = "Path to binary file to be used as EEPROM", - }, - ARG_TABLE_ENDMARKER - }; - - native_add_command_line_opts(eeprom_options); -} - -NATIVE_TASK(eeprom_native_posix_options_0, PRE_BOOT_1, 1); -NATIVE_TASK(eeprom_native_posix_cleanup_0, ON_EXIT, 1); diff --git a/drivers/eeprom/eeprom_simulator.c b/drivers/eeprom/eeprom_simulator.c index d44133c19a2..790326fb14f 100644 --- a/drivers/eeprom/eeprom_simulator.c +++ b/drivers/eeprom/eeprom_simulator.c @@ -1,4 +1,5 @@ /* + * Copyright (c) 2019 Laczen * Copyright (c) 2018 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 @@ -6,11 +7,22 @@ #include #include + #include #include #include #include #include +#include + +#ifdef CONFIG_ARCH_POSIX +#include +#include +#include +#include +#include "cmdline.h" +#include "soc.h" +#endif #define LOG_LEVEL CONFIG_EEPROM_LOG_LEVEL #include @@ -70,7 +82,14 @@ STATS_NAME(eeprom_sim_thresholds, max_write_calls) STATS_NAME(eeprom_sim_thresholds, max_len) STATS_NAME_END(eeprom_sim_thresholds); +#ifdef CONFIG_ARCH_POSIX +static u8_t *mock_eeprom; +static int eeprom_fd = -1; +static const char *eeprom_file_path; +static const char default_eeprom_file_path[] = "eeprom.bin"; +#else static u8_t mock_eeprom[DT_INST_0_ZEPHYR_SIM_EEPROM_SIZE]; +#endif /* CONFIG_ARCH_POSIX */ static int eeprom_range_is_valid(struct device *dev, off_t offset, size_t len) { @@ -83,8 +102,8 @@ static int eeprom_range_is_valid(struct device *dev, off_t offset, size_t len) return 0; } -static int eeprom_sim_read(struct device *dev, const off_t offset, void *data, - const size_t len) +static int eeprom_sim_read(struct device *dev, off_t offset, void *data, + size_t len) { if (!len) { return 0; @@ -112,8 +131,8 @@ static int eeprom_sim_read(struct device *dev, const off_t offset, void *data, return 0; } -static int eeprom_sim_write(struct device *dev, const off_t offset, - const void *data, const size_t len) +static int eeprom_sim_write(struct device *dev, off_t offset, const void *data, + size_t len) { const struct eeprom_sim_config *config = DEV_CONFIG(dev); @@ -140,27 +159,23 @@ static int eeprom_sim_write(struct device *dev, const off_t offset, if (eeprom_sim_thresholds.max_write_calls != 0) { if (eeprom_sim_stats.eeprom_write_calls > eeprom_sim_thresholds.max_write_calls) { - return 0; + goto end; } else if (eeprom_sim_stats.eeprom_write_calls == eeprom_sim_thresholds.max_write_calls) { if (eeprom_sim_thresholds.max_len == 0) { - return 0; + goto end; } data_part_ignored = true; } } - for (u32_t i = 0; i < len; i++) { - if (data_part_ignored) { - if (i >= eeprom_sim_thresholds.max_len) { - return 0; - } - } - - *(EEPROM(offset + i)) = *((u8_t *)data + i); + if ((data_part_ignored) && (len > eeprom_sim_thresholds.max_len)) { + len = eeprom_sim_thresholds.max_len; } + memcpy(EEPROM(offset), data, len); + STATS_INCN(eeprom_sim_stats, bytes_written, len); #ifdef CONFIG_EEPROM_SIMULATOR_SIMULATE_TIMING @@ -170,8 +185,8 @@ static int eeprom_sim_write(struct device *dev, const off_t offset, CONFIG_EEPROM_SIMULATOR_MIN_WRITE_TIME_US); #endif +end: SYNC_UNLOCK(); - return 0; } @@ -193,17 +208,98 @@ static const struct eeprom_sim_config eeprom_sim_config_0 = { .readonly = DT_INST_0_ZEPHYR_SIM_EEPROM_READ_ONLY, }; +#ifdef CONFIG_ARCH_POSIX + +static int eeprom_mock_init(struct device *dev) +{ + if (eeprom_file_path == NULL) { + eeprom_file_path = default_eeprom_file_path; + } + + eeprom_fd = open(eeprom_file_path, O_RDWR | O_CREAT, (mode_t)0600); + if (eeprom_fd == -1) { + posix_print_warning("Failed to open eeprom device file ", + "%s: %s\n", + eeprom_file_path, strerror(errno)); + return -EIO; + } + + if (ftruncate(eeprom_fd, DT_INST_0_ZEPHYR_SIM_EEPROM_SIZE) == -1) { + posix_print_warning("Failed to resize eeprom device file ", + "%s: %s\n", + eeprom_file_path, strerror(errno)); + return -EIO; + } + + mock_eeprom = mmap(NULL, DT_INST_0_ZEPHYR_SIM_EEPROM_SIZE, + PROT_WRITE | PROT_READ, MAP_SHARED, eeprom_fd, 0); + if (mock_eeprom == MAP_FAILED) { + posix_print_warning("Failed to mmap eeprom device file " + "%s: %s\n", + eeprom_file_path, strerror(errno)); + return -EIO; + } + + return 0; +} + +#else + +static int eeprom_mock_init(struct device *dev) +{ + memset(mock_eeprom, 0xFF, ARRAY_SIZE(mock_eeprom)); + return 0; +} + +#endif /* CONFIG_ARCH_POSIX */ + static int eeprom_sim_init(struct device *dev) { SYNC_INIT(); STATS_INIT_AND_REG(eeprom_sim_stats, STATS_SIZE_32, "eeprom_sim_stats"); STATS_INIT_AND_REG(eeprom_sim_thresholds, STATS_SIZE_32, "eeprom_sim_thresholds"); - memset(mock_eeprom, 0xFF, ARRAY_SIZE(mock_eeprom)); - return 0; + return eeprom_mock_init(dev); } DEVICE_AND_API_INIT(eeprom_sim_0, DT_INST_0_ZEPHYR_SIM_EEPROM_LABEL, &eeprom_sim_init, NULL, &eeprom_sim_config_0, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &eeprom_sim_api); + +#ifdef CONFIG_ARCH_POSIX + +static void eeprom_native_posix_cleanup(void) +{ + if ((mock_eeprom != MAP_FAILED) && (mock_eeprom != NULL)) { + munmap(mock_eeprom, DT_INST_0_ZEPHYR_SIM_EEPROM_SIZE); + } + + if (eeprom_fd != -1) { + close(eeprom_fd); + } +} + +static void eeprom_native_posix_options(void) +{ + static struct args_struct_t eeprom_options[] = { + { .manual = false, + .is_mandatory = false, + .is_switch = false, + .option = "eeprom", + .name = "path", + .type = 's', + .dest = (void *)&eeprom_file_path, + .call_when_found = NULL, + .descript = "Path to binary file to be used as eeprom" }, + ARG_TABLE_ENDMARKER + }; + + native_add_command_line_opts(eeprom_options); +} + + +NATIVE_TASK(eeprom_native_posix_options, PRE_BOOT_1, 1); +NATIVE_TASK(eeprom_native_posix_cleanup, ON_EXIT, 1); + +#endif /* CONFIG_ARCH_POSIX */ diff --git a/dts/bindings/mtd/zephyr,native-posix-eeprom.yaml b/dts/bindings/mtd/zephyr,native-posix-eeprom.yaml deleted file mode 100644 index 61b4aa09e2f..00000000000 --- a/dts/bindings/mtd/zephyr,native-posix-eeprom.yaml +++ /dev/null @@ -1,12 +0,0 @@ -# Copyright (c) 2019 Vestas Wind Systems A/S -# SPDX-License-Identifier: Apache-2.0 - -description: Zephyr Native POSIX EEPROM device - -compatible: "zephyr,native-posix-eeprom" - -include: eeprom-base.yaml - -properties: - size: - required: true