Skip to content

Commit

Permalink
drivers/am335xgpio: Release resources on error and when quitting
Browse files Browse the repository at this point in the history
The /dev/mem file descriptor can be closed without invalidating the
mappings so close as soon as possible.

munmap() all memory, either on error or from quit.

Change-Id: I9466edd2f43791e64f2dce719957c67728f3ec06
Signed-off-by: Steve Marple <[email protected]>
Reviewed-on: https://review.openocd.org/c/openocd/+/7047
Tested-by: jenkins
Reviewed-by: Antonio Borneo <[email protected]>
  • Loading branch information
stevemarple authored and borneoa committed Aug 15, 2022
1 parent ace0282 commit 903f2e9
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/jtag/drivers/am335xgpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,13 @@ static bool am335xgpio_swd_mode_possible(void)
return true;
}

static void am335xgpio_munmap(void)
{
for (unsigned int i = 0; i < AM335XGPIO_NUM_GPIO_CHIPS && am335xgpio_gpio_chip_mmap_addr[i] != MAP_FAILED; ++i)
if (munmap((void *)am335xgpio_gpio_chip_mmap_addr[i], sysconf(_SC_PAGE_SIZE)) < 0)
LOG_ERROR("Cannot unmap GPIO memory for chip %d: %s", i, strerror(errno));
}

static int am335xgpio_init(void)
{
LOG_INFO("AM335x GPIO JTAG/SWD bitbang driver");
Expand Down Expand Up @@ -410,10 +417,12 @@ static int am335xgpio_init(void)

if (am335xgpio_gpio_chip_mmap_addr[i] == MAP_FAILED) {
LOG_ERROR("mmap: %s", strerror(errno));
am335xgpio_munmap();
close(dev_mem_fd);
return ERROR_JTAG_INIT_FAILED;
}
}
close(dev_mem_fd);

/* Configure JTAG/SWD signals. Default directions and initial states are handled
* by adapter.c and "adapter gpio" command.
Expand Down Expand Up @@ -476,6 +485,8 @@ static int am335xgpio_quit(void)
restore_gpio(ADAPTER_GPIO_IDX_SRST);
restore_gpio(ADAPTER_GPIO_IDX_LED);

am335xgpio_munmap();

return ERROR_OK;
}

Expand Down

0 comments on commit 903f2e9

Please sign in to comment.