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