Kernel - Move debug hooks to common handler
[tpg/acess2.git] / KernelLand / Kernel / drv / serial.c
1 /*
2  * Acess2 Kernel
3  * - By John Hodge (thePowersGang)
4  *
5  * drv/serial.c
6  * - Common serial port code
7  */
8 #include <acess.h>
9 #include <modules.h>
10 #include <fs_devfs.h>
11 #include <drv_serial.h>
12 #include <drv_pty.h>
13 #include <debug_hooks.h>
14
15 extern void     Validate_VirtualMemoryUsage(void);
16
17 // === TYPES ===
18 struct sSerialPort
19 {
20         tPTY    *PTY;
21         tSerial_OutFcn  OutputFcn;
22         void    *OutHandle;
23 };
24
25 // === PROTOTYPES ===
26  int    Serial_Install(char **Arguments);
27 //tSerialPort   *Serial_CreatePort( tSerial_OutFcn output, void *handle );
28 //void  Serial_ByteReceived(tSerialPort *Port, char Ch);
29 void    Serial_int_PTYOutput(void *Handle, size_t Length, const void *Buffer);
30  int    Serial_int_PTYSetArrib(void *Handle, const struct ptymode *Mode);
31  int    Serial_int_PTYSetDims(void *Handle, const struct ptydims *Dims);
32 void    Serial_int_OutputDebug(void *unused, char ch);
33
34 // === GLOBALS ===
35 MODULE_DEFINE(0, 0x100, Serial, Serial_Install, NULL, "PTY", NULL);
36 tSerialPort     *gSerial_KernelDebugPort;
37
38 // === CODE ===
39 int Serial_Install(char **Arguments)
40 {
41         gSerial_KernelDebugPort = Serial_CreatePort( Serial_int_OutputDebug, NULL );
42         return 0;
43 }
44
45 tSerialPort *Serial_CreatePort(tSerial_OutFcn output, void *handle)
46 {
47         tSerialPort     *ret = malloc( sizeof(tSerialPort) );
48         // Automatically indexed
49         struct ptydims dims = {
50                 .W = 80, .H = 25,
51                 .PW = 0, .PH = 0
52         };
53         struct ptymode mode = {
54                 .OutputMode = PTYBUFFMT_TEXT,
55                 .InputMode = PTYIMODE_CANON|PTYIMODE_ECHO
56         };
57         ret->PTY = PTY_Create("serial#", ret,
58                 Serial_int_PTYOutput, Serial_int_PTYSetDims, Serial_int_PTYSetArrib,
59                 &dims, &mode
60                 );
61         ret->OutputFcn = output;
62         ret->OutHandle = handle;
63         return ret;
64 }
65
66 void Serial_ByteReceived(tSerialPort *Port, char Ch)
67 {
68         if( !Port )
69                 return ;
70         if( Port == gSerial_KernelDebugPort )
71         {
72                 static tDebugHook       info;
73                 static int serial_debug_mode = 0;
74                 // Kernel serial debug hooks.
75                 if( serial_debug_mode == 2 )
76                 {
77                         // Leave latched mode
78                         if( Ch == '.' )
79                                 serial_debug_mode = 0;
80                         else
81                                 DebugHook_HandleInput(&info, 1, &Ch);
82                         return ;
83                 }
84                 else if( serial_debug_mode )
85                 {
86                         if( Ch == 'X'-'A'+1 ) {
87                                 PTY_SendInput(Port->PTY, &Ch, 1);
88                                 serial_debug_mode = 0;
89                         }
90                         else if( Ch == '~' ) {
91                                 // Enter latched mode
92                                 serial_debug_mode = 2;
93                         }
94                         else {
95                                 DebugHook_HandleInput(&info, 1, &Ch);
96                                 serial_debug_mode = 0;
97                         }
98                         return ;
99                 }
100                 else if( Ch == 'X'-'A'+1 )
101                 {
102                         serial_debug_mode = 1;
103                         return ;
104                 }
105                 else
106                 {
107                         // Fall
108                 }
109         }
110         if( Ch == '\r' )
111                 Ch = '\n';
112         PTY_SendInput(Port->PTY, &Ch, 1);
113 }
114
115 void Serial_int_PTYOutput(void *Handle, size_t Length, const void *Buffer)
116 {
117         tSerialPort     *Port = Handle;
118         const char      *buf = Buffer;
119         for( int i = 0; i < Length; i ++ )
120                 Port->OutputFcn( Port->OutHandle, *buf++ );
121 }
122 int Serial_int_PTYSetArrib(void *Handle, const struct ptymode *Mode)
123 {
124         return 0;
125 }
126 int Serial_int_PTYSetDims(void *Handle, const struct ptydims *Dims)
127 {
128         return 0;
129 }
130
131 void Serial_int_OutputDebug(void *unused, char ch)
132 {
133         Debug_PutCharDebug(ch);
134 }
135

UCC git Repository :: git.ucc.asn.au