2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright (c) 2003 Marcus R. Brown <mrbrown@0xd6.org>
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
10 # $Id: tty.c 629 2004-10-11 00:45:00Z mrbrown $
11 # TTY filesystem for UDPTTY.
13 modified by jimmikaelkael <jimmikaelkael@wanadoo.fr>
23 #include "intrman_add.h"
31 #define MODNAME "udptty"
32 IRX_ID(MODNAME
, 2, 1);
34 struct irx_export_table _exp_udptty
;
38 static int udp_socket
;
39 static int tty_sema
= -1;
41 static int tty_init(iop_device_t
*device
);
42 static int tty_deinit(iop_device_t
*device
);
43 static int tty_stdout_fd(void);
44 static int tty_write(iop_file_t
*file
, void *buf
, size_t size
);
45 static int tty_error(void);
48 static iop_device_ops_t tty_ops
= {
52 (void *)tty_stdout_fd
,
53 (void *)tty_stdout_fd
,
68 /* device descriptor */
69 static iop_device_t tty_device
= {
71 IOP_DT_CHAR
|IOP_DT_CONS
,
80 #define PRNT_IO_BEGIN 0x200
81 #define PRNT_IO_END 0x201
82 #define EA_SINGLE 0x00
85 typedef struct _KprArg
{
95 #define KPR_BUFFER_SIZE 0x1000
96 char kprbuffer
[KPR_BUFFER_SIZE
];
99 void PrntFunc(void *common
, int chr
)
101 KprArg
*kpa
= (KprArg
*)common
;
112 PrntFunc(common
, '\r');
114 if (kpa
->prpos
< kpa
->bsize
)
115 kpa
->kpbuf
[kpa
->prpos
++] = chr
;
120 void *Kprnt(void *common
, const char *format
, void *arg
)
123 prnt((print_callback_t
)PrntFunc
, common
, format
, arg
);
128 void *Kprintf_Handler(void *common
, const char *format
, void *arg
)
130 KprArg
*kpa
= (KprArg
*)common
;
133 res
= intrman_14(Kprnt
, kpa
, (void*)format
, arg
);
135 if (QueryIntrContext())
136 iSetEventFlag(kpa
->eflag
, 1);
138 SetEventFlag(kpa
->eflag
, 1);
143 void KPRTTY_Thread(void *args
)
146 KprArg
*kpa
= (KprArg
*)args
;
149 WaitEventFlag(kpa
->eflag
, 1, WEF_AND
| WEF_CLEAR
, &flags
);
152 if (strncmp(kpa
->kpbuf
, "WARNING: WaitSema KE_CAN_NOT_WAIT", kpa
->prpos
-2))
153 write(1, kpa
->kpbuf
, kpa
->prpos
);
159 void kprtty_init(void)
168 efp
.attr
= EA_SINGLE
;
174 thp
.thread
= (void *)KPRTTY_Thread
;
175 thp
.stacksize
= 0x800;
178 kpa
->eflag
= CreateEventFlag(&efp
);
179 kpa
->bsize
= KPR_BUFFER_SIZE
;
180 kpa
->kpbuf
= kprbuffer
;
184 thid
= CreateThread(&thp
);
185 StartThread(thid
, (void *)kpa
);
187 Kprintf_set((kprintf_handler_func_t
*)Kprintf_Handler
, (u32
)kpa
);
191 int _start(int argc
, char** argv
)
194 RegisterLibraryEntries(&_exp_udptty
);
197 udp_socket
= lwip_socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
199 return MODULE_NO_RESIDENT_END
;
205 if (AddDrv(&tty_device
) < 0)
206 return MODULE_NO_RESIDENT_END
;
208 open(DEVNAME
"00:", 0x1000|O_RDWR
);
209 open(DEVNAME
"00:", O_WRONLY
);
211 printf("UDPTTY loaded!\n");
215 printf("KPRTTY enabled!\n");
218 return MODULE_RESIDENT_END
;
223 lwip_close(udp_socket
);
228 /* Copy the data into place, calculate the various checksums, and send the
230 static int udp_send(void *buf
, size_t size
)
232 struct sockaddr_in peer
;
234 peer
.sin_family
= AF_INET
;
235 peer
.sin_port
= htons(18194);
236 peer
.sin_addr
.s_addr
= inet_addr("255.255.255.255");
238 lwip_sendto(udp_socket
, buf
, size
, 0, (struct sockaddr
*)&peer
, sizeof(peer
));
245 static int tty_init(iop_device_t
*device
)
247 if ((tty_sema
= CreateMutex(IOP_MUTEX_UNLOCKED
)) < 0)
253 static int tty_deinit(iop_device_t
*device
)
255 DeleteSema(tty_sema
);
259 static int tty_stdout_fd(void)
264 static int tty_write(iop_file_t
*file
, void *buf
, size_t size
)
269 res
= udp_send(buf
, size
);
270 SignalSema(tty_sema
);
275 static int tty_error(void)