| 1 | /* | 
|---|
| 2 | * soclib_tty.c - soclib tty driver definition. | 
|---|
| 3 | * | 
|---|
| 4 | * Author  Alain Greiner (2016) | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright (c)  UPMC Sorbonne Universites | 
|---|
| 7 | * | 
|---|
| 8 | * This file is part of ALMOS-MKH. | 
|---|
| 9 | * | 
|---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it | 
|---|
| 11 | * under the terms of the GNU General Public License as published by | 
|---|
| 12 | * the Free Software Foundation; version 2.0 of the License. | 
|---|
| 13 | * | 
|---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but | 
|---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 17 | * General Public License for more details. | 
|---|
| 18 | * | 
|---|
| 19 | * You should have received a copy of the GNU General Public License | 
|---|
| 20 | * along with ALMOS-kernel; if not, write to the Free Software Foundation, | 
|---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | 
|---|
| 22 | */ | 
|---|
| 23 |  | 
|---|
| 24 | #include <dev_txt.h> | 
|---|
| 25 | #include <chdev.h> | 
|---|
| 26 | #include <spinlock.h> | 
|---|
| 27 |  | 
|---|
| 28 |  | 
|---|
| 29 | /**************************************************************************************** | 
|---|
| 30 | * This driver supports the vci_tty_tsar component. | 
|---|
| 31 | * It implements the generic TXT device API: | 
|---|
| 32 | * - transfer one single character from TTY to command "buffer" if to_mem is non-zero. | 
|---|
| 33 | * - transfer "count" characters from command "buffer" to TTY if "to_mem is zero. | 
|---|
| 34 | ***************************************************************************************/ | 
|---|
| 35 |  | 
|---|
| 36 | /**************************************************************************************** | 
|---|
| 37 | *     SOCLIB_TTY registers offsets | 
|---|
| 38 | ***************************************************************************************/ | 
|---|
| 39 |  | 
|---|
| 40 | #define TTY_WRITE              0 | 
|---|
| 41 | #define TTY_STATUS             1 | 
|---|
| 42 | #define TTY_READ               2 | 
|---|
| 43 | #define TTY_RX_IRQ_ENABLE      3 | 
|---|
| 44 | #define TTY_TX_IRQ_ENABLE      4 | 
|---|
| 45 |  | 
|---|
| 46 | #define TTY_SPAN               8       // number of registers per channel | 
|---|
| 47 |  | 
|---|
| 48 | /**************************************************************************************** | 
|---|
| 49 | * masks for TTY_STATUS_REG | 
|---|
| 50 | ***************************************************************************************/ | 
|---|
| 51 |  | 
|---|
| 52 | #define TTY_STATUS_RX_FULL     1       // TTY_READ_REG full if 1 | 
|---|
| 53 | #define TTY_STATUS_TX_FULL     2       // TTY_WRITE_REG full if 1 | 
|---|
| 54 |  | 
|---|
| 55 | /**************************************************************************************** | 
|---|
| 56 | * This Rstructure is used by the soclib_tty_isr for the RX channel. | 
|---|
| 57 | ***************************************************************************************/ | 
|---|
| 58 |  | 
|---|
| 59 | #define TTY_FIFO_DEPTH  128 | 
|---|
| 60 |  | 
|---|
| 61 | typedef struct tty_fifo_s     // 32 bytes | 
|---|
| 62 | { | 
|---|
| 63 | char          data[TTY_FIFO_DEPTH];   // one char per slot | 
|---|
| 64 | unsigned int  ptr;                    // next free slot index | 
|---|
| 65 | unsigned int  ptw;                    // next full slot index | 
|---|
| 66 | unsigned int  sts;                    // number of full slots | 
|---|
| 67 | } tty_fifo_t; | 
|---|
| 68 |  | 
|---|
| 69 |  | 
|---|
| 70 | /**************************************************************************************** | 
|---|
| 71 | * This function masks both the TTY_RX and TTY_TX IRQs. | 
|---|
| 72 | * These IRQs are unmasked by the soclib_tty_cmd() function. | 
|---|
| 73 | **************************************************************************************** | 
|---|
| 74 | * @ chdev     : pointer on the TXT chdev descriptor. | 
|---|
| 75 | ***************************************************************************************/ | 
|---|
| 76 | void soclib_tty_init( chdev_t * chdev ); | 
|---|
| 77 |  | 
|---|
| 78 | /**************************************************************************************** | 
|---|
| 79 | * This function implements both the TXT_READ & TXT_WRITE commands registered in the | 
|---|
| 80 | * client thread descriptor (in the txt_cmd field), even if ALMOS-MKH defines two | 
|---|
| 81 | * different chdevs (and consequently two diffeerent server threads) for the RX and TX | 
|---|
| 82 | * directions. The client thread is identified by the <thread_xp> argument. | 
|---|
| 83 | * Depending on the command type, it unmasks the relevant TTY_RX / TTY_TX IRQ, | 
|---|
| 84 | * and blocks the TXT device server thread on the THREAD_BLOCKED_DEV_ISR, as the data | 
|---|
| 85 | * transfer is done by the ISR. | 
|---|
| 86 | * **************************************************************************************** | 
|---|
| 87 | * @ thread_xp : extended pointer on client thread descriptor. | 
|---|
| 88 | ***************************************************************************************/ | 
|---|
| 89 | void soclib_tty_cmd( xptr_t thread_xp ); | 
|---|
| 90 |  | 
|---|
| 91 | /**************************************************************************************** | 
|---|
| 92 | * This function implements the TXT_SYNC_WRITE command registered in the txt_aux_t | 
|---|
| 93 | * structure, using a busy waiting policy, without using the TTY IRQ. | 
|---|
| 94 | * It is used by the kernel do display debug messages on TXT0 terminal, without | 
|---|
| 95 | * interference with another TXT access to another terminal done by the same thread. | 
|---|
| 96 | **************************************************************************************** | 
|---|
| 97 | * @ thread_xp : pointer on the txt_aux_t structure containing the arguments. | 
|---|
| 98 | ***************************************************************************************/ | 
|---|
| 99 | void soclib_tty_aux( void * args ); | 
|---|
| 100 |  | 
|---|
| 101 | /**************************************************************************************** | 
|---|
| 102 | * This ISR is executed to handle both the TTY_TX_IRQ and the TTY_RX_IRQ, even if | 
|---|
| 103 | *   The RX_IRQ is activated as soon as the TTY_STATUS_RX_FULL bit is 1 in the | 
|---|
| 104 | *   TTY_STATUS register, when the TTY_RX_IRQ_ENABLE is non zero, indicating that | 
|---|
| 105 | *   the TTY_READ buffer is full and can be read. | 
|---|
| 106 | *   The TX_IRQ is activated as soon as the TTY_STATUS_TX_FULL bit is 0 in the | 
|---|
| 107 | *   TTY_STATUS register, when the TTY_TX_IRQ_ENABLE is non zero, indicating that | 
|---|
| 108 | *   the TTY_WRITE buffer is empty, and can be written. | 
|---|
| 109 | * WARNING : In ALMOS-MKH, the RX_IRQ is always enabled to catch the control signals, | 
|---|
| 110 | * but the TX_IRQ is dynamically enabled by the TXT_WRITE command, and disabled when | 
|---|
| 111 | * the command is completed. | 
|---|
| 112 | * | 
|---|
| 113 | * 1) The ISR first read the TTY_STATUS to get the current state of the TTY_READ and | 
|---|
| 114 | *   the TTY_WRITE buffers. | 
|---|
| 115 | * | 
|---|
| 116 | * 2) It try to read the first command registered in the server thread queue associated | 
|---|
| 117 | *    to the TTY channel | 
|---|
| 118 | * | 
|---|
| 119 | * 2) The ISR handles the RX when the TTY_READ buffer is full : | 
|---|
| 120 | *   . it read the available character from the TTY_READ buffer, and this | 
|---|
| 121 | *     acknowledges the RX_IRQ. | 
|---|
| 122 | *   . if it is a control character ( ^C / ^D / ^Z ) it translate it to the proper | 
|---|
| 123 | *     signal and execute the relevant sigaction for the foreground process. | 
|---|
| 124 | *   . if it is a normal character, it try to get the first command registered in the | 
|---|
| 125 | *     server thread queue. If it is a TXT_READ, it returns this character to the | 
|---|
| 126 | *     command buffer in the client thread. | 
|---|
| 127 | * | 
|---|
| 128 | * 3) The ISR handles the TX when the TTY_WRITE buffer is empty and a TXT_WRITE | 
|---|
| 129 | *   . it try to get it copies the | 
|---|
| 130 | *     character to the command buffer, acknowledges the TTY_RX_IRQ, and unblock the | 
|---|
| 131 | *     associated server thread. | 
|---|
| 132 |  | 
|---|
| 133 | *   . the control characters ^C / ^D / ^Z  are directly handled by the ISR and | 
|---|
| 134 | *     translated to the foreground process. | 
|---|
| 135 |  | 
|---|
| 136 | * - the | 
|---|
| 137 | the TXT_READ and TXT_WRITE commands. | 
|---|
| 138 | * It gets the command arguments from the first client thread in the TXT chdev queue: | 
|---|
| 139 | * - if TXT_READ, it transfers one byte from the TTY_READ_REG to the command buffer. | 
|---|
| 140 | *   It simply returns for retry if TTY_READ_REG is empty. | 
|---|
| 141 | * - if TXT_WRITE, it tries to transfer several bytes from the command buffer to the | 
|---|
| 142 | *   TTY_WRITE_REG. If the TTY_WRITE_REG is full, it updates the "count" and "buffer" | 
|---|
| 143 | *   command arguments and returns for retry. | 
|---|
| 144 | * When the I/O operation is completed, it sets the status field in the command, unblocks | 
|---|
| 145 | * the server thread, and unblocks the client thread. | 
|---|
| 146 | **************************************************************************************** | 
|---|
| 147 | * @ chdev     : local pointer on TXT chdev descriptor. | 
|---|
| 148 | ***************************************************************************************/ | 
|---|
| 149 | void soclib_tty_isr( chdev_t * chdev ); | 
|---|
| 150 |  | 
|---|