From 8c2e0ce946012a4275e8dfa9d8dfd1d5a68d6e3e Mon Sep 17 00:00:00 2001 From: sotech117 Date: Sat, 2 Mar 2024 23:05:26 +0000 Subject: decent coding session --- kernel/drivers/tty/tty.c | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) (limited to 'kernel/drivers/tty/tty.c') diff --git a/kernel/drivers/tty/tty.c b/kernel/drivers/tty/tty.c index 3694877..c47a582 100644 --- a/kernel/drivers/tty/tty.c +++ b/kernel/drivers/tty/tty.c @@ -69,8 +69,29 @@ void tty_init() */ ssize_t tty_read(chardev_t *cdev, size_t pos, void *buf, size_t count) { - NOT_YET_IMPLEMENTED("DRIVERS: tty_read"); - return -1; + // NOT_YET_IMPLEMENTED("DRIVERS: tty_read"); + + // get the mapped tty + tty_t *tty = cd_to_tty(cdev); + + // set the IPL to INTR_KEYBOARD + uint8_t prev_ipl = intr_setipl(INTR_KEYBOARD); + + // lock the read mutex of the tty + kmutex_lock(&tty->tty_read_mutex); + + // wait until there is something in the line discipline's buffer + ldisc_wait_read(&tty->tty_ldisc); + + // read from the ldisc's buffer if there are new characters + ssize_t bytes_read = ldisc_read(&tty->tty_ldisc, buf, count); + // unlock the read mutex of the tty + kmutex_unlock(&tty->tty_read_mutex); + + // revert the IPL + intr_setipl(prev_ipl); + + return bytes_read; } /** @@ -88,8 +109,27 @@ ssize_t tty_read(chardev_t *cdev, size_t pos, void *buf, size_t count) */ ssize_t tty_write(chardev_t *cdev, size_t pos, const void *buf, size_t count) { - NOT_YET_IMPLEMENTED("DRIVERS: tty_write"); - return -1; + // NOT_YET_IMPLEMENTED("DRIVERS: tty_write"); + + // get the mapped tty + tty_t *tty = cd_to_tty(cdev); + + // set the IPL to INTR_KEYBOARD + uint8_t prev_ipl = intr_setipl(INTR_KEYBOARD); + + // lock the write mutex of the tty + kmutex_lock(&tty->tty_write_mutex); + + // write to the terminal + size_t bytes_written = vterminal_write(&tty->tty_vterminal, buf, count); + + // unlock the write mutex of the tty + kmutex_unlock(&tty->tty_write_mutex); + // revert the IPL + intr_setipl(prev_ipl); + + + return bytes_written; } static void tty_receive_char_multiplexer(uint8_t c) -- cgit v1.2.3-70-g09d2