/*
 * soclib_tty.c - soclib tty driver definition.
 *
 * Author  Alain Greiner (2016)
 *
 * Copyright (c)  UPMC Sorbonne Universites
 *
 * This file is part of ALMOS-MKH.
 *
 * ALMOS-MKH is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2.0 of the License.
 *
 * ALMOS-MKH is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ALMOS-kernel; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <dev_txt.h>
#include <chdev.h>
#include <spinlock.h>


/****************************************************************************************
 * This driver supports the soclib_multi_tty component.
 * It implements the generic TXT device API:
 * - transfer one single character from TTY to command "buffer" if to_mem is non-zero.
 * - transfer "count" characters from command "buffer" to TTY if "to_mem is zero.
 ***************************************************************************************/

/****************************************************************************************
 *     SOCLIB_TTY registers offsets
 ***************************************************************************************/

#define TTY_WRITE              0
#define TTY_STATUS             1
#define TTY_READ               2
#define TTY_RX_IRQ_ENABLE      3
#define TTY_TX_IRQ_ENABLE      4

#define TTY_SPAN               8       // number of registers per channel

/****************************************************************************************
 * masks for TTY_STATUS_REG
 ***************************************************************************************/

#define TTY_STATUS_RX_FULL     1       // TTY_READ_REG full if 1
#define TTY_STATUS_TX_FULL     2       // TTY_WRITE_REG full if 1

/****************************************************************************************
 * This function masks both the TTY_RX and TTY_TX IRQs.
 * These IRQs are unmasked by the soclib_tty_cmd() function.
 ****************************************************************************************
 * @ chdev     : pointer on the TXT chdev descriptor.
 ***************************************************************************************/
void soclib_tty_init( chdev_t * chdev );

/****************************************************************************************
 * This function implements the TXT_READ & TXT_WRITE commands registered in the client
 * thread descriptor (in the txt_cmd field), identified by the <xp_thread> argument.
 * Depending on the command type, it only unmasks the relevant TTY_RX / TTY_TX IRQ,
 * and blocks the TXT device server thread on the THREAD_BLOCKED_DEV_ISR, as the data
 * transfer is done by the ISR.
 * ****************************************************************************************
 * @ thread_xp : extended pointer on client thread descriptor.
 ***************************************************************************************/
void soclib_tty_cmd( xptr_t thread_xp );

/****************************************************************************************
 * This function implements the TXT_SYNC_WRITE command registered in the txt_aux_t
 * structure, using a busy waiting policy, without using the TTY IRQ.
 * It is used by the kernel do display debug messages on TXT0 terminal, without
 * interference with another TXT access to another terminal done by the same thread.
 ****************************************************************************************
 * @ thread_xp : pointer on the txt_aux_t structure containing the arguments.
 ***************************************************************************************/
void soclib_tty_aux( void * args );

/****************************************************************************************
 * This ISR should be executed only for the TXT_READ and TXT_WRITE commands.
 * It gets the command arguments from the first client thread in the TXT chdev queue:
 * - if TXT_READ, it transfers one byte from the TTY_READ_REG to the command buffer.
 *   It simply returns for retry if TTY_READ_REG is empty.
 * - if TXT_WRITE, it tries to transfer several bytes from the command buffer to the
 *   TTY_WRITE_REG. If the TTY_WRITE_REG is full, it updates the "count" and "buffer"
 *   command arguments and returns for retry.
 * When the I/O operation is completed, it sets the status field in the command, unblocks
 * the server thread, and unblocks the client thread.
 ****************************************************************************************
 * @ chdev     : local pointer on TXT chdev descriptor.
 ***************************************************************************************/
void soclib_tty_isr( chdev_t * chdev );

