PS2SDK
PS2 Homebrew Libraries
sbus_tty.c
1 /*
2 
3 IOP->EE TTY over SBUS by Herben
4 
5 This file contains the file system driver and glue needed to create a TTY file file system and redirect it to EE over SBUS.
6 
7 Additionally it installs a Kprintf handler so you can even see the things you don't normally see like the error messages WaitSema gives
8 for those naughty ps2ip modules. ;)
9 
10 Of course this requires that the EE-side code accept this command and output the string to screen/sio...
11 
12 */
13 
14 #include <tamtypes.h>
15 #include <stdio.h>
16 #include <stdarg.h>
17 #include <errno.h>
18 #include <sysclib.h>
19 #include <sysmem.h>
20 #include <excepman.h>
21 #include <intrman.h>
22 #include <ioman.h>
23 #include <ps2_debug.h>
24 #include <ps2_sbus.h>
25 
26 #include "iopdebug.h"
27 
28 extern void sbus_tty_puts(const char *str);
29 
30 static int ttyfs_init(iop_device_t *device)
31 {
32  (void)device;
33  //DBG_puts("SIOTTY: FS Init()\n");
34  return 0;
35 }
36 
37 static int ttyfs_deinit(iop_device_t *device)
38 {
39  (void)device;
40  //DBG_puts("SIOTTY: FS Deinit()\n");
41  return 0;
42 }
43 
44 static int ttyfs_open(iop_file_t *file, const char *name, int flags)
45 {
46  (void)file;
47  (void)name;
48  (void)flags;
49 
50  //DBG_puts("SIOTTY: FS Open()\n");
51  return 0;
52 }
53 
54 static int ttyfs_dopen(iop_file_t *file, const char *name)
55 {
56  (void)file;
57  (void)name;
58 
59  //DBG_puts("SIOTTY: FS Dopen()\n");
60  return 0;
61 }
62 
63 static int ttyfs_close(iop_file_t *file)
64 {
65  (void)file;
66 
67  //DBG_puts("SIOTTY: FS Close()\n");
68  return(0);
69 }
70 
71 static int ttyfs_write(iop_file_t *file, void *ptr, int size) {
72  char temp[65];
73  int bCount = 0;
74 
75  (void)file;
76 
77  //DBG_puts("SIOTTY: FS Write()\n");
78 
79  while(bCount < size)
80  {
81  int toWrite;
82 
83  toWrite = (size - bCount);
84  if(toWrite > 64)
85  toWrite = 64;
86 
87  memcpy(temp, &(((u8 *)ptr)[bCount]), toWrite);
88  temp[toWrite] = '\0';
89  sbus_tty_puts(temp);
90 
91  bCount += toWrite;
92  }
93 
94  return(bCount);
95 }
96 
97 IOMAN_RETURN_VALUE_IMPL(EPERM);
98 
99 static iop_device_ops_t fsd_ops =
100 {
101  &ttyfs_init, // init
102  &ttyfs_deinit, // deinit
103  IOMAN_RETURN_VALUE(EPERM), // format
104  &ttyfs_open, // open
105  &ttyfs_close, // close
106  IOMAN_RETURN_VALUE(EPERM), // read
107  &ttyfs_write, // write
108  IOMAN_RETURN_VALUE(EPERM), // lseek
109  IOMAN_RETURN_VALUE(EPERM), // ioctl
110  IOMAN_RETURN_VALUE(EPERM), // remove
111  IOMAN_RETURN_VALUE(EPERM), // mkdir
112  IOMAN_RETURN_VALUE(EPERM), // rmdir
113  &ttyfs_dopen, // dopen
114  &ttyfs_close, // dclose
115  IOMAN_RETURN_VALUE(EPERM), // dread
116  IOMAN_RETURN_VALUE(EPERM), // getstat
117  IOMAN_RETURN_VALUE(EPERM), // chstat
118 };
119 
120 static iop_device_t tty_fsd =
121 {
122  "tty",
123  IOP_DT_FS,
124  1,
125  "TTY via EE SIO",
126  &fsd_ops,
127 };
128 
129 void sprintf_putchar(void *context, int c)
130 {
131  char **string = (char **)context;
132 
133  if(c < 0x100) { ((*string)++)[0] = c; }
134  else { (*string)[0] = 0; }
135 }
136 
137 extern int _vsprintf(char * str, const char * format, va_list ap);
138 
139 static char kprint_buffer[1024];
140 
141 int _kPrintf(void *context, const char * format, va_list ap)
142 {
143  (void)context;
144 
145  int r = prnt(&sprintf_putchar, &format, format, ap);
146  sbus_tty_puts(kprint_buffer);
147  return r;
148 }
149 
150 int sbus_tty_init(void)
151 {
152  close(0);
153  close(1);
154  DelDrv(tty_fsd.name);
155  DelDrv("dummytty");
156 
157  if(AddDrv(&tty_fsd) != 0) { return(-1); }
158 
159  open("tty:", O_RDONLY);
160  open("tty:", O_WRONLY);
161 
162  KprintfSet(&_kPrintf, NULL);
163 
164  return(0);
165 }
sysclib.h
EPERM
#define EPERM
Definition: errno.h:21
excepman.h
ps2_sbus.h
_iop_file
Definition: ioman.h:53
tamtypes.h
stdio.h
ps2_debug.h
ioman.h
_iop_device_ops
Definition: ioman.h:79
sysmem.h
_iop_device
Definition: ioman.h:64
intrman.h
iopdebug.h
errno.h