drivers/am335xgpio: Release resources on error and when quitting

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 <stevemarple@googlemail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/7047
Tested-by: jenkins
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
This commit is contained in:
Steve Marple 2022-06-22 15:43:51 +01:00 committed by Antonio Borneo
parent ace028262b
commit 903f2e92a1
1 changed files with 11 additions and 0 deletions

View File

@ -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");
@ -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.
@ -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;
}