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.
30 static g_param_t
*g_param
;
31 static int tty_sema
= -1;
33 /* The max we'll send is a MTU, 1514 bytes. */
34 static u8 pktbuf
[1514];
36 static int tty_init(iop_device_t
*device
);
37 static int tty_deinit(iop_device_t
*device
);
38 static int tty_stdout_fd(void);
39 static int tty_write(iop_file_t
*file
, void *buf
, size_t size
);
40 static int tty_error(void);
43 static iop_device_ops_t tty_ops
= {
47 (void *)tty_stdout_fd
,
48 (void *)tty_stdout_fd
,
63 /* device descriptor */
64 static iop_device_t tty_device
= {
66 IOP_DT_CHAR
|IOP_DT_CONS
,
73 void ttyInit(g_param_t
*gparam
)
81 if (AddDrv(&tty_device
) < 0)
84 open(DEVNAME
"00:", 0x1000|O_RDWR
);
85 open(DEVNAME
"00:", O_WRONLY
);
90 static int udp_init(void)
94 /* Initialize the static elements of our UDP packet. */
95 udp_pkt
= (udp_pkt_t
*)pktbuf
;
97 mips_memcpy(udp_pkt
->eth
.addr_dst
, g_param
->eth_addr_dst
, 12);
98 udp_pkt
->eth
.type
= 0x0008; /* Network byte order: 0x800 */
100 udp_pkt
->ip
.hlen
= 0x45;
103 udp_pkt
->ip
.flags
= 0;
104 udp_pkt
->ip
.frag_offset
= 0;
105 udp_pkt
->ip
.ttl
= 64;
106 udp_pkt
->ip
.proto
= 0x11;
107 mips_memcpy(&udp_pkt
->ip
.addr_src
.addr
, &g_param
->ip_addr_src
, 4);
108 mips_memcpy(&udp_pkt
->ip
.addr_dst
.addr
, &g_param
->ip_addr_dst
, 4);
110 udp_pkt
->udp_port_src
= g_param
->ip_port_src
;
111 udp_pkt
->udp_port_dst
= IP_PORT(0x4712);
116 /* Copy the data into place, calculate the various checksums, and send the
118 static int udp_send(void *buf
, size_t size
)
121 size_t pktsize
, udpsize
;
123 if ((size
+ sizeof(udp_pkt_t
)) > sizeof(pktbuf
))
124 size
= sizeof(pktbuf
) - sizeof(udp_pkt_t
);
126 udp_pkt
= (udp_pkt_t
*)pktbuf
;
127 pktsize
= size
+ sizeof(udp_pkt_t
);
129 udp_pkt
->ip
.len
= htons(pktsize
- 14); /* Subtract the ethernet header. */
131 udp_pkt
->ip
.csum
= 0;
132 udp_pkt
->ip
.csum
= inet_chksum(&udp_pkt
->ip
, 20); /* Checksum the IP header (20 bytes). */
134 udpsize
= htons(size
+ 8); /* Size of the UDP header + data. */
135 udp_pkt
->udp_len
= udpsize
;
136 mips_memcpy(pktbuf
+ sizeof(udp_pkt_t
), buf
, size
);
138 udp_pkt
->udp_csum
= 0; /* Don't bother. */
140 while (smap_xmit(udp_pkt
, pktsize
) != 0);
148 static int tty_init(iop_device_t
*device
)
152 if ((res
= udp_init()) < 0)
155 if ((tty_sema
= CreateMutex(IOP_MUTEX_UNLOCKED
)) < 0)
161 static int tty_deinit(iop_device_t
*device
)
163 DeleteSema(tty_sema
);
167 static int tty_stdout_fd(void)
172 static int tty_write(iop_file_t
*file
, void *buf
, size_t size
)
177 res
= udp_send(buf
, size
);
179 SignalSema(tty_sema
);
183 static int tty_error(void)