Kernel/sysfs - (minor) Fixed extra newline in log message
[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         // TODO: Make PTY code handle 'serial#' and auto-number
46         ret->PTY = PTY_Create("serial0", 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( Ch == '\r' )
66                 Ch = '\n';
67         PTY_SendInput(Port->PTY, &Ch, 1);
68 }
69
70 void Serial_int_PTYOutput(void *Handle, size_t Length, const void *Buffer)
71 {
72         tSerialPort     *Port = Handle;
73         const char      *buf = Buffer;
74         for( int i = 0; i < Length; i ++ )
75                 Port->OutputFcn( Port->OutHandle, *buf++ );
76 }
77 int Serial_int_PTYSetArrib(void *Handle, const struct ptymode *Mode)
78 {
79         return 0;
80 }
81 int Serial_int_PTYSetDims(void *Handle, const struct ptydims *Dims)
82 {
83         return 0;
84 }
85
86 void Serial_int_OutputDebug(void *unused, char ch)
87 {
88         Debug_PutCharDebug(ch);
89 }
90

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