Kernel/Serial - Add serial debug hooks
[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
14 // === TYPES ===
15 struct sSerialPort
16 {
17         tPTY    *PTY;
18         tSerial_OutFcn  OutputFcn;
19         void    *OutHandle;
20 };
21
22 // === PROTOTYPES ===
23  int    Serial_Install(char **Arguments);
24 //tSerialPort   *Serial_CreatePort( tSerial_OutFcn output, void *handle );
25 //void  Serial_ByteReceived(tSerialPort *Port, char Ch);
26 void    Serial_int_PTYOutput(void *Handle, size_t Length, const void *Buffer);
27  int    Serial_int_PTYSetArrib(void *Handle, const struct ptymode *Mode);
28  int    Serial_int_PTYSetDims(void *Handle, const struct ptydims *Dims);
29 void    Serial_int_OutputDebug(void *unused, char ch);
30
31 // === GLOBALS ===
32 MODULE_DEFINE(0, 0x100, Serial, Serial_Install, NULL, "PTY", NULL);
33 tSerialPort     *gSerial_KernelDebugPort;
34
35 // === CODE ===
36 int Serial_Install(char **Arguments)
37 {
38         gSerial_KernelDebugPort = Serial_CreatePort( Serial_int_OutputDebug, NULL );
39         return 0;
40 }
41
42 tSerialPort *Serial_CreatePort(tSerial_OutFcn output, void *handle)
43 {
44         tSerialPort     *ret = malloc( sizeof(tSerialPort) );
45         // Automatically indexed
46         ret->PTY = PTY_Create("serial#", ret, Serial_int_PTYOutput, Serial_int_PTYSetDims, Serial_int_PTYSetArrib);
47         ret->OutputFcn = output;
48         ret->OutHandle = handle;
49         struct ptymode mode = {
50                 .OutputMode = PTYBUFFMT_TEXT,
51                 .InputMode = PTYIMODE_CANON|PTYIMODE_ECHO
52         };
53         struct ptydims dims = {
54                 .W = 80, .H = 25,
55                 .PW = 0, .PH = 0
56         };
57         PTY_SetAttrib(ret->PTY, &dims, &mode, 0);
58         return ret;
59 }
60
61 void Serial_ByteReceived(tSerialPort *Port, char Ch)
62 {
63         if( !Port )
64                 return ;
65         if( Port == gSerial_KernelDebugPort )
66         {
67                 static int serial_debug_mode = 0;
68                 // Kernel serial debug hooks.
69                 if( serial_debug_mode )
70                 {
71                         switch(Ch)
72                         {
73                         case 'p':
74                                 Threads_Dump();
75                                 break;
76                         case 'X'-'A'+1:
77                                 PTY_SendInput(Port->PTY, &Ch, 1);
78                                 break;
79                         }
80                         serial_debug_mode = 0;
81                         return ;
82                 }
83                 else if( Ch == 'X'-'A'+1 )
84                 {
85                         serial_debug_mode = 1;
86                         return ;
87                 }
88                 else
89                 {
90                         // Fall
91                 }
92         }
93         if( Ch == '\r' )
94                 Ch = '\n';
95         PTY_SendInput(Port->PTY, &Ch, 1);
96 }
97
98 void Serial_int_PTYOutput(void *Handle, size_t Length, const void *Buffer)
99 {
100         tSerialPort     *Port = Handle;
101         const char      *buf = Buffer;
102         for( int i = 0; i < Length; i ++ )
103                 Port->OutputFcn( Port->OutHandle, *buf++ );
104 }
105 int Serial_int_PTYSetArrib(void *Handle, const struct ptymode *Mode)
106 {
107         return 0;
108 }
109 int Serial_int_PTYSetDims(void *Handle, const struct ptydims *Dims)
110 {
111         return 0;
112 }
113
114 void Serial_int_OutputDebug(void *unused, char ch)
115 {
116         Debug_PutCharDebug(ch);
117 }
118

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