Skip to content

Commit

Permalink
drivers/misc/devmem: remove unnecessary register parser
Browse files Browse the repository at this point in the history
Signed-off-by: chao an <[email protected]>
  • Loading branch information
anchao committed Oct 15, 2024
1 parent 5033a4e commit 75daaab
Showing 1 changed file with 16 additions and 82 deletions.
98 changes: 16 additions & 82 deletions drivers/misc/dev_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,31 @@
#include <sys/mman.h>

/****************************************************************************
* Pre-processor Definitions
* Private Data
****************************************************************************/

#define DEVMEM_REGION 8

#ifdef CONFIG_BOARD_MEMORY_RANGE
static const struct memory_region_s g_memory_region[] =
{
CONFIG_BOARD_MEMORY_RANGE
};
#endif

/****************************************************************************
* Public Data
****************************************************************************/

#else
extern uint8_t _stext[]; /* Start of .text */
extern uint8_t _etext[]; /* End_1 of .text + .rodata */
extern uint8_t _sdata[]; /* Start of .data */
extern uint8_t _edata[]; /* End+1 of .data */
extern uint8_t _sbss[]; /* Start of .bss */
extern uint8_t _ebss[]; /* End+1 of .bss */

static const struct memory_region_s g_memory_region[] =
{
{ (uintptr_t)_stext, (uintptr_t)_etext, PROT_EXEC | PROT_READ },
{ (uintptr_t)_sdata, (uintptr_t)_edata, PROT_WRITE | PROT_READ },
{ (uintptr_t)_sbss, (uintptr_t)_ebss, PROT_WRITE | PROT_READ },
{ 0, 0, 0 },
};
#endif

/****************************************************************************
* Private Function Prototypes
****************************************************************************/
Expand Down Expand Up @@ -98,15 +100,8 @@ static ssize_t devmem_read(FAR struct file *filep, FAR char *buffer,
ssize_t len;
int i;

DEBUGASSERT(region && src);

for (i = 0; i < DEVMEM_REGION; i++)
for (i = 0; region[i].start != 0 && region[i].end != 0; i++)
{
if (region[i].start == 0 && region[i].end == 0)
{
break;
}

start = MAX(src, region[i].start);
end = MIN(start + buflen, region[i].end);
len = end - start;
Expand Down Expand Up @@ -135,15 +130,8 @@ static ssize_t devmem_write(FAR struct file *filep, FAR const char *buffer,
ssize_t len;
int i;

DEBUGASSERT(region && dest);

for (i = 0; i < DEVMEM_REGION; i++)
for (i = 0; region[i].start != 0 && region[i].end != 0; i++)
{
if (region[i].start == 0 && region[i].end == 0)
{
break;
}

start = MAX(dest, region[i].start);
end = MIN(start + buflen, region[i].end);
len = end - start;
Expand All @@ -170,8 +158,6 @@ static int devmem_mmap(FAR struct file *filep,
uintptr_t end;
int i;

DEBUGASSERT(region);

if (map->offset < 0)
{
return -EINVAL;
Expand All @@ -180,13 +166,8 @@ static int devmem_mmap(FAR struct file *filep,
start = map->offset;
end = start + map->length;

for (i = 0; i < DEVMEM_REGION; i++)
for (i = 0; region[i].start != 0 && region[i].end != 0; i++)
{
if (region[i].start == 0 && region[i].end == 0)
{
break;
}

if (start >= region[i].start && end <= region[i].end)
{
map->vaddr = (FAR void *)start;
Expand Down Expand Up @@ -214,53 +195,6 @@ static int devmem_mmap(FAR struct file *filep,

int devmem_register(void)
{
FAR struct memory_region_s *region;
int ret;

#ifndef CONFIG_BOARD_MEMORY_RANGE
bool merge = (&_edata[0] == &_sbss[0]);
ssize_t len = 0;
region = kmm_calloc(DEVMEM_REGION, sizeof(*region));
if (region == NULL)
{
return -ENOMEM;
}

if (len + (4 - merge) > DEVMEM_REGION)
{
len = DEVMEM_REGION - (4 - merge);
}

region[len].flags = PROT_EXEC | PROT_READ;
region[len].start = (uintptr_t)_stext;
region[len++].end = (uintptr_t)_etext;
region[len].flags = PROT_WRITE | PROT_READ;
region[len].start = (uintptr_t)_sdata;
region[len++].end = (uintptr_t)_edata;

if (merge)
{
region[len - 1].end = (uintptr_t)_ebss;
}
else
{
region[len].flags = PROT_WRITE | PROT_READ;
region[len].start = (uintptr_t)_sbss;
region[len++].end = (uintptr_t)_ebss;
}
#else
region = (FAR struct memory_region_s *)g_memory_region;
#endif
/* register the new MEM driver */

ret = register_driver("/dev/mem", &g_devmem_fops, 0666, region);
if (ret < 0)
{
#ifndef CONFIG_BOARD_MEMORY_RANGE
kmm_free(region);
#endif
return -ENOMEM;
}

return ret;
return register_driver("/dev/mem", &g_devmem_fops,
0666, (FAR void *)g_memory_region);
}

0 comments on commit 75daaab

Please sign in to comment.