Kernel/serial - Heap dump option
[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 'h':
77                                 Heap_Dump();
78                                 break;
79                         case 'X'-'A'+1:
80                                 PTY_SendInput(Port->PTY, &Ch, 1);
81                                 break;
82                         }
83                         serial_debug_mode = 0;
84                         return ;
85                 }
86                 else if( Ch == 'X'-'A'+1 )
87                 {
88                         serial_debug_mode = 1;
89                         return ;
90                 }
91                 else
92                 {
93                         // Fall
94                 }
95         }
96         if( Ch == '\r' )
97                 Ch = '\n';
98         PTY_SendInput(Port->PTY, &Ch, 1);
99 }
100
101 void Serial_int_PTYOutput(void *Handle, size_t Length, const void *Buffer)
102 {
103         tSerialPort     *Port = Handle;
104         const char      *buf = Buffer;
105         for( int i = 0; i < Length; i ++ )
106                 Port->OutputFcn( Port->OutHandle, *buf++ );
107 }
108 int Serial_int_PTYSetArrib(void *Handle, const struct ptymode *Mode)
109 {
110         return 0;
111 }
112 int Serial_int_PTYSetDims(void *Handle, const struct ptydims *Dims)
113 {
114         return 0;
115 }
116
117 void Serial_int_OutputDebug(void *unused, char ch)
118 {
119         Debug_PutCharDebug(ch);
120 }
121

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