PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
tty.c
1/*
2
3IOP->PPC TTY
4
5*/
6
7#include <tamtypes.h>
8#include <stdio.h>
9#include <stdarg.h>
10#include <errno.h>
11#include <sysclib.h>
12#include <sysmem.h>
13#include <excepman.h>
14#include <intrman.h>
15#include <ioman.h>
16#include <thsemap.h>
17#include "tty.h"
18
19#ifdef DEBUG
20#define DPRINTF(fmt, x...) printf(MODNAME ": " fmt, ##x)
21#else
22#define DPRINTF(x...)
23#endif
24
25static int tty_sema = -1;
26
27extern void tty_puts(const char *str);
28
29static int ttyfs_init(iop_device_t *device)
30{
31 (void)device;
32 DPRINTF("FS Init()\n");
33 if ((tty_sema = CreateMutex(IOP_MUTEX_UNLOCKED)) < 0)
34 {
35 DPRINTF("Failed to create mutex\n");
36 return -1;
37 }
38 return 0;
39}
40
41static int ttyfs_deinit(iop_device_t *device)
42{
43 (void)device;
44 DPRINTF("FS Deinit()\n");
45 DeleteSema(tty_sema);
46 return 0;
47}
48
49static int ttyfs_write(iop_file_t *file, void *ptr, int size) {
50 char temp[65];
51 int bCount = 0;
52
53 (void)file;
54
55 DPRINTF("FS Write()\n");
56
57 WaitSema(tty_sema);
58 while(bCount < size)
59 {
60 int toWrite;
61
62 toWrite = (size - bCount);
63 if(toWrite > 64)
64 toWrite = 64;
65
66 memcpy(temp, &(((u8 *)ptr)[bCount]), toWrite);
67 temp[toWrite] = '\0';
68 tty_puts(temp);
69
70 bCount += toWrite;
71 }
72 SignalSema(tty_sema);
73
74 return(bCount);
75}
76
77IOMAN_RETURN_VALUE_IMPL(0);
78IOMAN_RETURN_VALUE_IMPL(EPERM);
79
80static iop_device_ops_t fsd_ops =
81{
82 &ttyfs_init, // init
83 &ttyfs_deinit, // deinit
84 IOMAN_RETURN_VALUE(EPERM), // format
85 IOMAN_RETURN_VALUE(0), // open
86 IOMAN_RETURN_VALUE(0), // close
87 IOMAN_RETURN_VALUE(EPERM), // read
88 &ttyfs_write, // write
89 IOMAN_RETURN_VALUE(EPERM), // lseek
90 IOMAN_RETURN_VALUE(EPERM), // ioctl
91 IOMAN_RETURN_VALUE(EPERM), // remove
92 IOMAN_RETURN_VALUE(EPERM), // mkdir
93 IOMAN_RETURN_VALUE(EPERM), // rmdir
94 IOMAN_RETURN_VALUE(0), // dopen
95 IOMAN_RETURN_VALUE(0), // dclose
96 IOMAN_RETURN_VALUE(EPERM), // dread
97 IOMAN_RETURN_VALUE(EPERM), // getstat
98 IOMAN_RETURN_VALUE(EPERM), // chstat
99};
100
101static iop_device_t tty_fsd =
102{
103 "tty",
104 IOP_DT_CHAR | IOP_DT_CONS,
105 1,
106 "TTY via PPC SIO",
107 &fsd_ops,
108};
109
110#ifdef KPRINTF
111void sprintf_putchar(void *context, int c)
112{
113 char **string = (char **)context;
114
115 if(c < 0x100) { ((*string)++)[0] = c; }
116 else { (*string)[0] = 0; }
117}
118
119extern int _vsprintf(char * str, const char * format, va_list ap);
120
121static char kprint_buffer[1024];
122
123int _kPrintf(void *context, const char * format, va_list ap)
124{
125 (void)context;
126
127 int r = prnt(&sprintf_putchar, &format, format, ap);
128 tty_puts(kprint_buffer);
129 return r;
130}
131#endif
132
133int tty_init(void)
134{
135 DelDrv(tty_fsd.name);
136 DelDrv("dummytty");
137
138 if(AddDrv(&tty_fsd) != 0) { return(-1); }
139
140 // open stdin
141 close(0);
142 open("tty:", O_RDONLY);
143 // open stdout
144 close(1);
145 open("tty:", O_WRONLY);
146#ifdef KPRINTF
147 printf("PPCTTY: KprintfSet\n");
148 KprintfSet(&_kPrintf, NULL);
149#endif
150 return(0);
151}
#define EPERM
Definition errno.h:21