diff --git a/drivers/Kconfig b/drivers/Kconfig index 8341c7efa5..4cd24d4fb6 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -3,6 +3,10 @@ # see the file kconfig-language.txt in the NuttX tools repository. # +config DEV_SIMPLE_ADDRENV + bool "Simple AddrEnv" + default n + config DEV_NULL bool "Enable /dev/null" default y diff --git a/drivers/Makefile b/drivers/Makefile index a79545480c..3e355c57c2 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -88,6 +88,10 @@ ifeq ($(CONFIG_SPECIFIC_DRIVERS),y) include platform$(DELIM)Make.defs endif +ifeq ($(CONFIG_DEV_SIMPLE_ADDRENV),y) + CSRCS += addrenv.c +endif + CSRCS += dev_null.c dev_zero.c ifneq ($(CONFIG_DISABLE_MOUNTPOINT),y) diff --git a/drivers/addrenv.c b/drivers/addrenv.c new file mode 100644 index 0000000000..92080f5d48 --- /dev/null +++ b/drivers/addrenv.c @@ -0,0 +1,92 @@ +/**************************************************************************** + * drivers/addrenv.c + * + * Copyright (C) 2018 Pinecone Inc. All rights reserved. + * Author: Guiding Li + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +#include + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct simple_addrenv_s g_addrenv_dummy; +static const struct simple_addrenv_s *g_addrenv = &g_addrenv_dummy; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +void simple_addrenv_initialize(const struct simple_addrenv_s *addrenv) +{ + g_addrenv = addrenv; +} + +void *up_addrenv_pa_to_va(uintptr_t pa) +{ + uint32_t i; + + for (i = 0; g_addrenv[i].size; i++) + { + if (pa - g_addrenv[i].pa < g_addrenv[i].size) + { + return (void *)(g_addrenv[i].va + B2C(pa - g_addrenv[i].pa)); + } + } + + return (void *)B2C(pa); +} + +uintptr_t up_addrenv_va_to_pa(void *va_) +{ + uintptr_t va = C2B((uintptr_t)va_); + uint32_t i; + + for (i = 0; g_addrenv[i].size; i++) + { + uintptr_t tmp = C2B(g_addrenv[i].va); + if (va - tmp < g_addrenv[i].size) + { + return g_addrenv[i].pa + (va - tmp); + } + } + + return va; +} diff --git a/include/nuttx/drivers/addrenv.h b/include/nuttx/drivers/addrenv.h new file mode 100644 index 0000000000..4b113ea278 --- /dev/null +++ b/include/nuttx/drivers/addrenv.h @@ -0,0 +1,81 @@ +/**************************************************************************** + * include/nuttx/drivers/addrenv.h + * + * Copyright (C) 2018 Pinecone Inc. All rights reserved. + * Author: Guiding Li + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_DRIVERS_ADDRENV_H +#define __INCLUDE_NUTTX_DRIVERS_ADDRENV_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +struct simple_addrenv_s +{ + uintptr_t va; + uintptr_t pa; + size_t size; +}; + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +void simple_addrenv_initialize(const struct simple_addrenv_s *addrenv); + +#ifdef __cplusplus +} +#endif + +#endif /* __INCLUDE_NUTTX_DRIVERS_ADDRENV_H */