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

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