Disabling auto-refresh of game list by default, as it is causing bugs sometimes
[open-ps2-loader.git] / modules / wip / lanman / udptty.c
blobdf4c4e88c82bed6ae9fdc1c4147f5b7ee7ebb5ef
1 /*
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.
14 #include <tamtypes.h>
15 #include <stdio.h>
16 #include <thsemap.h>
17 #include <ioman.h>
18 #include <errno.h>
20 #include "lanman.h"
21 #include "smsutils.h"
22 #include "smap.h"
23 #include "udp.h"
24 #include "inet.h"
26 #define DEVNAME "tty"
28 #ifdef 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);
42 /* device ops */
43 static iop_device_ops_t tty_ops = {
44 tty_init,
45 tty_deinit,
46 (void *)tty_error,
47 (void *)tty_stdout_fd,
48 (void *)tty_stdout_fd,
49 (void *)tty_error,
50 (void *)tty_write,
51 (void *)tty_error,
52 (void *)tty_error,
53 (void *)tty_error,
54 (void *)tty_error,
55 (void *)tty_error,
56 (void *)tty_error,
57 (void *)tty_error,
58 (void *)tty_error,
59 (void *)tty_error,
60 (void *)tty_error
63 /* device descriptor */
64 static iop_device_t tty_device = {
65 DEVNAME,
66 IOP_DT_CHAR|IOP_DT_CONS,
68 "TTY via SMAP UDP",
69 &tty_ops
72 /* Init TTY */
73 void ttyInit(g_param_t *gparam)
75 g_param = gparam;
77 close(0);
78 close(1);
79 DelDrv(DEVNAME);
81 if (AddDrv(&tty_device) < 0)
82 return;
84 open(DEVNAME "00:", 0x1000|O_RDWR);
85 open(DEVNAME "00:", O_WRONLY);
88 /* UDP */
90 static int udp_init(void)
92 udp_pkt_t *udp_pkt;
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;
101 udp_pkt->ip.tos = 0;
102 udp_pkt->ip.id = 0;
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);
113 return 0;
116 /* Copy the data into place, calculate the various checksums, and send the
117 final packet. */
118 static int udp_send(void *buf, size_t size)
120 udp_pkt_t *udp_pkt;
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);
142 return 0;
146 /* TTY driver. */
148 static int tty_init(iop_device_t *device)
150 int res;
152 if ((res = udp_init()) < 0)
153 return res;
155 if ((tty_sema = CreateMutex(IOP_MUTEX_UNLOCKED)) < 0)
156 return -1;
158 return 0;
161 static int tty_deinit(iop_device_t *device)
163 DeleteSema(tty_sema);
164 return 0;
167 static int tty_stdout_fd(void)
169 return 1;
172 static int tty_write(iop_file_t *file, void *buf, size_t size)
174 int res = 0;
176 WaitSema(tty_sema);
177 res = udp_send(buf, size);
179 SignalSema(tty_sema);
180 return res;
183 static int tty_error(void)
185 return -EIO;
188 #endif