From 1a2a07476b9a6b102bd9499cf3928dc50c598a9e Mon Sep 17 00:00:00 2001 From: Yin Fengwei Date: Fri, 13 Apr 2018 10:31:37 +0800 Subject: [PATCH] DM: cleanup resource for uart. Add deinit function for uart. Another work is add resource cleanup functions which is called by other components when they are using uart. Signed-off-by: Yin Fengwei Acked-by: Anthony Xu --- devicemodel/hw/pci/uart.c | 13 +++++++++++++ devicemodel/hw/platform/uart_core.c | 28 ++++++++++++++++++++++++++++ devicemodel/include/uart_core.h | 1 + 3 files changed, 42 insertions(+) diff --git a/devicemodel/hw/pci/uart.c b/devicemodel/hw/pci/uart.c index f262cdb6f..d2bb7ed5f 100644 --- a/devicemodel/hw/pci/uart.c +++ b/devicemodel/hw/pci/uart.c @@ -106,9 +106,22 @@ pci_uart_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts) return 0; } +static void +pci_uart_deinit(struct vmctx *ctx, struct pci_vdev *dev, char *opts) +{ + struct uart_vdev *uart = (struct uart_vdev *)dev->arg; + + if (uart == NULL) + return; + + uart_release_backend(uart, opts); + uart_deinit(uart); +} + struct pci_vdev_ops pci_ops_com = { .class_name = "uart", .vdev_init = pci_uart_init, + .vdev_deinit = pci_uart_deinit, .vdev_barwrite = pci_uart_write, .vdev_barread = pci_uart_read }; diff --git a/devicemodel/hw/platform/uart_core.c b/devicemodel/hw/platform/uart_core.c index 7679b6d1d..760df5cf6 100644 --- a/devicemodel/hw/platform/uart_core.c +++ b/devicemodel/hw/platform/uart_core.c @@ -274,6 +274,18 @@ uart_opentty(struct uart_vdev *uart) } } +static void +uart_closetty(struct uart_vdev *uart) +{ + if (uart->tty.fd != STDIN_FILENO) + mevent_delete_close(uart->mev); + else + mevent_delete(uart->mev); + + uart->mev = 0; + ttyclose(); +} + static uint8_t modem_status(uint8_t mcr) { @@ -692,3 +704,19 @@ uart_set_backend(struct uart_vdev *uart, const char *opts) return retval; } + +void +uart_release_backend(struct uart_vdev *uart, const char *opts) +{ + if (opts == NULL) + return; + + uart_closetty(uart); + if (strcmp("stdio", opts) == 0) { + stdio_in_use = false; + } else + close(uart->tty.fd); + + uart->tty.fd = 0; + uart->tty.opened = false; +} diff --git a/devicemodel/include/uart_core.h b/devicemodel/include/uart_core.h index 98fb17c65..052b87c2c 100644 --- a/devicemodel/include/uart_core.h +++ b/devicemodel/include/uart_core.h @@ -44,4 +44,5 @@ void uart_legacy_dealloc(int which); uint8_t uart_read(struct uart_vdev *uart, int offset); void uart_write(struct uart_vdev *uart, int offset, uint8_t value); int uart_set_backend(struct uart_vdev *uart, const char *opt); +void uart_release_backend(struct uart_vdev *uart, const char *opts); #endif