source: trunk/hal/tsar_mips32/drivers/soclib_mtty.h @ 535

Last change on this file since 535 was 534, checked in by nicolas.van.phan@…, 6 years ago

Add mtty driver.

File size: 7.8 KB
Line 
1/*
2 * soclib_multi_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 soclib_multi_tty 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 MTTY_WRITE              0
41#define MTTY_STATUS             1
42#define MTTY_READ               2
43#define MTTY_CONFIG             3
44
45#define MTTY_SPAN               4 
46
47/****************************************************************************************
48 * masks for TTY_STATUS_REG
49 ***************************************************************************************/
50
51#define MTTY_STATUS_RX_FULL     1       // TTY_READ_REG full if 1
52#define MTTY_STATUS_TX_FULL     2       // TTY_WRITE_REG full if 1
53
54/****************************************************************************************
55 * masks for TTY_CONFIG_REG
56 ***************************************************************************************/
57
58#define MTTY_CONFIG_RX_ENABLE   1       // TTY_RX IRQ enabled if 1
59#define MTTY_CONFIG_TX_ENABLE   2       // TTY_TX IRQ enabled if 1
60
61/****************************************************************************************
62 * This Rstructure is used by the soclib_multi_tty_isr for the RX channel.
63 ***************************************************************************************/
64
65#define MTTY_FIFO_DEPTH  128
66
67typedef struct mtty_fifo_s     // 32 bytes
68{
69    char          data[MTTY_FIFO_DEPTH];   // one char per slot
70    unsigned int  ptr;                    // next free slot index
71    unsigned int  ptw;                    // next full slot index
72    unsigned int  sts;                    // number of full slots
73} mtty_fifo_t;
74 
75
76/****************************************************************************************
77 * This function masks both the TTY_RX and TTY_TX IRQs.
78 * These IRQs are unmasked by the soclib_multi_tty_cmd() function.
79 ****************************************************************************************
80 * @ chdev     : pointer on the TXT chdev descriptor.
81 ***************************************************************************************/
82void soclib_mtty_init( chdev_t * chdev );
83
84/****************************************************************************************
85 * This function implements both the TXT_READ & TXT_WRITE commands registered in the
86 * client thread descriptor (in the txt_cmd field), even if ALMOS-MKH defines two
87 * different chdevs (and consequently two diffeerent server threads) for the RX and TX
88 * directions. The client thread is identified by the <thread_xp> argument.
89 * Depending on the command type, it unmasks the relevant TTY_RX / TTY_TX IRQ,
90 * and blocks the TXT device server thread on the THREAD_BLOCKED_DEV_ISR, as the data
91 * transfer is done by the ISR.
92 * ****************************************************************************************
93 * @ thread_xp : extended pointer on client thread descriptor.
94 ***************************************************************************************/
95void soclib_mtty_cmd( xptr_t thread_xp );
96
97/****************************************************************************************
98 * This function implements the TXT_SYNC_WRITE command registered in the txt_aux_t
99 * structure, using a busy waiting policy, without using the TTY IRQ.
100 * It is used by the kernel do display debug messages on TXT0 terminal, without
101 * interference with another TXT access to another terminal done by the same thread.
102 ****************************************************************************************
103 * @ thread_xp : pointer on the txt_aux_t structure containing the arguments.
104 ***************************************************************************************/
105void soclib_mtty_aux( void * args );
106
107/****************************************************************************************
108 * This ISR is executed to handle both the TTY_TX_IRQ and the TTY_RX_IRQ, even if
109 *   The RX_IRQ is activated as soon as the TTY_STATUS_RX_FULL bit is 1 in the
110 *   TTY_STATUS register, when the TTY_RX_IRQ_ENABLE is non zero, indicating that
111 *   the TTY_READ buffer is full and can be read.
112 *   The TX_IRQ is activated as soon as the TTY_STATUS_TX_FULL bit is 0 in the
113 *   TTY_STATUS register, when the TTY_TX_IRQ_ENABLE is non zero, indicating that
114 *   the TTY_WRITE buffer is empty, and can be written.
115 * WARNING : In ALMOS-MKH, the RX_IRQ is always enabled to catch the control signals,
116 * but the TX_IRQ is dynamically enabled by the TXT_WRITE command, and disabled when
117 * the command is completed.
118 *
119 * 1) The ISR first read the TTY_STATUS to get the current state of the TTY_READ and
120 *   the TTY_WRITE buffers.
121 *
122 * 2) It try to read the first command registered in the server thread queue associated
123 *    to the TTY channel
124 *
125 * 2) The ISR handles the RX when the TTY_READ buffer is full :
126 *   . it read the available character from the TTY_READ buffer, and this
127 *     acknowledges the RX_IRQ.
128 *   . if it is a control character ( ^C / ^D / ^Z ) it translate it to the proper
129 *     signal and execute the relevant sigaction for the foreground process.
130 *   . if it is a normal character, it try to get the first command registered in the
131 *     server thread queue. If it is a TXT_READ, it returns this character to the
132 *     command buffer in the client thread.
133 *
134 * 3) The ISR handles the TX when the TTY_WRITE buffer is empty and a TXT_WRITE
135 *   . it try to get it copies the
136 *     character to the command buffer, acknowledges the TTY_RX_IRQ, and unblock the
137 *     associated server thread.
138     
139 *   . the control characters ^C / ^D / ^Z  are directly handled by the ISR and
140 *     translated to the foreground process.
141
142 * - the
143 the TXT_READ and TXT_WRITE commands.
144 * It gets the command arguments from the first client thread in the TXT chdev queue:
145 * - if TXT_READ, it transfers one byte from the TTY_READ_REG to the command buffer.
146 *   It simply returns for retry if TTY_READ_REG is empty.
147 * - if TXT_WRITE, it tries to transfer several bytes from the command buffer to the
148 *   TTY_WRITE_REG. If the TTY_WRITE_REG is full, it updates the "count" and "buffer"
149 *   command arguments and returns for retry.
150 * When the I/O operation is completed, it sets the status field in the command, unblocks
151 * the server thread, and unblocks the client thread.
152 ****************************************************************************************
153 * @ chdev     : local pointer on TXT chdev descriptor.
154 ***************************************************************************************/
155void soclib_mtty_isr( chdev_t * chdev );
156
Note: See TracBrowser for help on using the repository browser.