ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
ps2ip.c
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
9 */
10 
16 #include <stdio.h>
17 #include <string.h>
18 #include <netman.h>
19 #include <lwip/memp.h>
20 
21 #include "lwip/sys.h"
22 #include "lwip/tcp.h"
23 #include "lwip/tcpip.h"
24 #include "lwip/netif.h"
25 #include "lwip/dhcp.h"
26 #include "lwip/prot/dhcp.h"
27 #include "lwip/inet.h"
28 #include "netif/etharp.h"
29 
30 #include "ps2ip_internal.h"
31 
32 typedef struct pbuf PBuf;
33 typedef struct netif NetIF;
34 typedef struct ip4_addr IPAddr;
35 
36 static struct netif NIF;
37 static struct pbuf *TxHead, *TxTail;
38 
39 unsigned short int hsyncTicksPerMSec = 16;
40 
41 int ps2ip_getconfig(char* pszName, t_ip_info* pInfo)
42 {
43  NetIF* pNetIF=netif_find(pszName);
44 
45  if (pNetIF==NULL)
46  {
47  //Net interface not found.
48  memset(pInfo,0,sizeof(*pInfo));
49  return 0;
50  }
51  strcpy(pInfo->netif_name,pszName);
52  pInfo->ipaddr.s_addr=pNetIF->ip_addr.addr;
53  pInfo->netmask.s_addr=pNetIF->netmask.addr;
54  pInfo->gw.s_addr=pNetIF->gw.addr;
55 
56  memcpy(pInfo->hw_addr,pNetIF->hwaddr,sizeof(pInfo->hw_addr));
57 
58 #if LWIP_DHCP
59  struct dhcp *dhcp = netif_dhcp_data(pNetIF);
60 
61  if ((dhcp != NULL) && (dhcp->state != DHCP_STATE_OFF))
62  {
63  pInfo->dhcp_enabled=1;
64  pInfo->dhcp_status=dhcp->state;
65  }
66  else
67  {
68  pInfo->dhcp_enabled=0;
70  }
71 
72 #else
73  pInfo->dhcp_enabled=0;
74 #endif
75 
76  return 1;
77 }
78 
79 int ps2ip_setconfig(const t_ip_info* pInfo)
80 {
81  NetIF* pNetIF=netif_find(pInfo->netif_name);
82 
83  if (pNetIF==NULL)
84  {
85  return 0;
86  }
87 
88 #if LWIP_DHCP
89  struct dhcp *dhcp = netif_dhcp_data(pNetIF);
90 
91  //Enable dhcp here
92 
93  if (pInfo->dhcp_enabled)
94  {
95  if ((dhcp == NULL) || (dhcp->state == DHCP_STATE_OFF))
96  {
97  //Set initial IP address
98  netif_set_ipaddr(pNetIF,(IPAddr*)&pInfo->ipaddr);
99  netif_set_netmask(pNetIF,(IPAddr*)&pInfo->netmask);
100  netif_set_gw(pNetIF,(IPAddr*)&pInfo->gw);
101 
102  //Start DHCP client
103  dhcp_start(pNetIF);
104  }
105  }
106  else
107  {
108  if ((dhcp != NULL) && (dhcp->state != DHCP_STATE_OFF))
109  {
110  //Release DHCP lease
111  dhcp_release(pNetIF);
112 
113  //Stop DHCP client
114  dhcp_stop(pNetIF);
115  }
116 
117  netif_set_ipaddr(pNetIF,(IPAddr*)&pInfo->ipaddr);
118  netif_set_netmask(pNetIF,(IPAddr*)&pInfo->netmask);
119  netif_set_gw(pNetIF,(IPAddr*)&pInfo->gw);
120  }
121 
122 #else
123  netif_set_ipaddr(pNetIF,(IPAddr*)&pInfo->ipaddr);
124  netif_set_netmask(pNetIF,(IPAddr*)&pInfo->netmask);
125  netif_set_gw(pNetIF,(IPAddr*)&pInfo->gw);
126 #endif
127 
128  return 1;
129 }
130 
131 static void EnQTxPacket(struct pbuf *tx)
132 {
133  DI();
134 
135  if(TxHead != NULL)
136  TxHead->next = tx;
137 
138  TxHead = tx;
139  tx->next = NULL;
140 
141  if(TxTail == NULL) //Queue empty
142  TxTail = tx;
143 
144  EI();
145 }
146 
147 static err_t SMapLowLevelOutput(struct netif* pNetIF, struct pbuf* pOutput)
148 {
149  struct pbuf* pbuf;
150  err_t result;
151 
152  result = ERR_OK;
153  if(pOutput->tot_len > pOutput->len)
154  {
155  pbuf_ref(pOutput); //Increment reference count because LWIP must free the PBUF, not the driver!
156  if((pbuf = pbuf_coalesce(pOutput, PBUF_RAW)) != pOutput)
157  { //No need to increase reference count because pbuf_coalesce() does it.
158  EnQTxPacket(pbuf);
159  NetManNetIFXmit();
160  } else
161  result = ERR_MEM;
162  } else {
163  pbuf_ref(pOutput); //This will be freed later.
164  EnQTxPacket(pOutput);
165  NetManNetIFXmit();
166  }
167 
168  return result;
169 }
170 
171 static void LinkStateUp(void)
172 {
173  tcpip_callback((void*)&netif_set_link_up, &NIF);
174 }
175 
176 static void LinkStateDown(void)
177 {
178  tcpip_callback((void*)&netif_set_link_down, &NIF);
179 }
180 
181 static void *AllocRxPacket(unsigned int size, void **payload)
182 {
183  struct pbuf* pbuf;
184 
185  if((pbuf = pbuf_alloc(PBUF_RAW, size, PBUF_POOL)) != NULL)
186  *payload = pbuf->payload;
187 
188  return pbuf;
189 }
190 
191 static void ReallocRxPacket(void *packet, unsigned int size)
192 {
193  pbuf_realloc((struct pbuf *)packet, size);
194 }
195 
196 static void FreeRxPacket(void *packet)
197 {
198  pbuf_free(packet);
199 }
200 
201 static void EnQRxPacket(void *packet)
202 {
204 }
205 
206 static int NextTxPacket(void **payload)
207 {
208  int len;
209 
210  if(TxTail != NULL)
211  {
212  *payload = TxTail->payload;
213  len = TxTail->len;
214  } else
215  len = 0;
216 
217  return len;
218 }
219 
220 static void DeQTxPacket(void)
221 {
222  struct pbuf *toFree;
223 
224  toFree = NULL;
225 
226  DI();
227  if(TxTail != NULL)
228  {
229  toFree = TxTail;
230 
231  if(TxTail == TxHead) {
232  //Last in queue.
233  TxTail = NULL;
234  TxHead = NULL;
235  } else {
236  TxTail = TxTail->next;
237  }
238  }
239  EI();
240 
241  if(toFree != NULL)
242  {
243  toFree->next = NULL;
244  pbuf_free(toFree);
245  }
246 }
247 
248 static int AfterTxPacket(void **payload)
249 {
250  int len;
251 
252  if(TxTail != NULL && TxTail->next != NULL)
253  {
254  *payload = TxTail->next->payload;
255  len = TxTail->next->len;
256  } else
257  len = 0;
258 
259  return len;
260 }
261 
262 static void InitDone(void* pvArg)
263 {
264  dbgprintf("InitDone: TCPIP initialized\n");
265  sys_sem_signal((sys_sem_t*)pvArg);
266 }
267 
269 static err_t SMapIFInit(struct netif* pNetIF)
270 {
271  TxHead = NULL;
272  TxTail = NULL;
273 
274  pNetIF->name[0]='s';
275  pNetIF->name[1]='m';
276  pNetIF->output=&etharp_output; // For LWIP 1.3.0 and later.
279  pNetIF->flags|=(NETIF_FLAG_ETHARP|NETIF_FLAG_BROADCAST); // For LWIP v1.3.0 and later.
280  pNetIF->mtu=1500;
281 
282  //Get MAC address.
283  NetManIoctl(NETMAN_NETIF_IOCTL_ETH_GET_MAC, NULL, 0, pNetIF->hwaddr, sizeof(pNetIF->hwaddr));
284  dbgprintf("MAC address : %02d:%02d:%02d:%02d:%02d:%02d\n",pNetIF->hwaddr[0],pNetIF->hwaddr[1],pNetIF->hwaddr[2],
285  pNetIF->hwaddr[3],pNetIF->hwaddr[4],pNetIF->hwaddr[5]);
286 
287  return ERR_OK;
288 }
289 
290 err_t ps2ip_input(PBuf* pInput, NetIF* pNetIF)
291 {
292  err_t result;
293 
294  if((result = pNetIF->input(pInput, pNetIF)) != ERR_OK)
295  pbuf_free(pInput);
296 
297  return result;
298 }
299 
300 static inline void InitializeLWIP(void)
301 {
302  sys_sem_t Sema;
303 
304  dbgprintf("PS2IP: Module Loaded.\n");
305 
306  sys_sem_new(&Sema, 0);
307  dbgprintf("PS2IP: Calling tcpip_init\n");
308  tcpip_init(InitDone,&Sema);
309 
310  sys_arch_sem_wait(&Sema, 0);
311  sys_sem_free(&Sema);
312 
313  dbgprintf("PS2IP: System Initialised\n");
314 }
315 
316 int ps2ipInit(struct ip4_addr *ip_address, struct ip4_addr *subnet_mask, struct ip4_addr *gateway){
317  static struct NetManNetProtStack stack={
318  &LinkStateUp,
319  &LinkStateDown,
320  &AllocRxPacket,
321  &FreeRxPacket,
322  &EnQRxPacket,
323  &NextTxPacket,
324  &DeQTxPacket,
325  &AfterTxPacket,
327  };
328 
329  NetManInit();
330 
331  InitializeLWIP();
332 
333  netif_add(&NIF, ip_address, subnet_mask, gateway, NULL, &SMapIFInit, tcpip_input);
335 
337  netif_set_up(&NIF);
338 
339  return 0;
340 }
341 
342 void ps2ipDeinit(void){
344 }
345 
346 void ps2ipSetHsyncTicksPerMSec(unsigned char ticks){
347  hsyncTicksPerMSec = ticks;
348 }
packet_t packet
Definition: font.c:24
#define NETIF_FLAG_ETHARP
Definition: tcpip.h:931
#define NETIF_FLAG_BROADCAST
Definition: tcpip.h:921
#define DI
Definition: kernel.h:24
#define EI
Definition: kernel.h:25
void NetManNetIFXmit(void)
Definition: netman.c:103
void NetManUnregisterNetworkStack(void)
Definition: netman.c:92
int NetManIoctl(unsigned int command, void *arg, unsigned int arg_len, void *output, unsigned int length)
Definition: netman.c:109
int NetManRegisterNetworkStack(const struct NetManNetProtStack *stack)
Definition: netman.c:70
int NetManInit(void)
Definition: netman.c:43
@ NETMAN_NETIF_IOCTL_ETH_GET_MAC
Definition: netman.h:75
void netif_set_netmask(struct netif *netif, const ip4_addr_t *netmask)
void pbuf_realloc(struct pbuf *p, u16 size)
struct pbuf * pbuf_coalesce(struct pbuf *p, pbuf_layer layer)
struct pbuf * pbuf_alloc(pbuf_layer l, u16 size, pbuf_type type)
struct netif * netif_add(struct netif *netif, void *state, netif_init_fn init, netif_input_fn input)
struct netif * netif_find(const char *name)
void netif_set_ipaddr(struct netif *netif, const ip4_addr_t *ipaddr)
void pbuf_ref(struct pbuf *p)
void netif_set_gw(struct netif *netif, const ip4_addr_t *gw)
u8 pbuf_free(struct pbuf *p)
void netif_set_default(struct netif *netif)
err_t etharp_output(struct netif *netif, struct pbuf *q, const ip_addr_t *ipaddr)
err_t tcpip_input(struct pbuf *p, struct netif *inp)
void netif_set_up(struct netif *netif)
#define dbgprintf(args...)
#define netif_dhcp_data(netif)
s32 result
Definition: rpc_client.c:23
static void FreeRxPacket(void *packet)
Definition: ps2ip.c:196
static void DeQTxPacket(void)
Definition: ps2ip.c:220
static struct netif NIF
Definition: ps2ip.c:36
static void LinkStateUp(void)
Definition: ps2ip.c:171
static void LinkStateDown(void)
Definition: ps2ip.c:176
int ps2ipInit(struct ip4_addr *ip_address, struct ip4_addr *subnet_mask, struct ip4_addr *gateway)
Definition: ps2ip.c:316
static int AfterTxPacket(void **payload)
Definition: ps2ip.c:248
static err_t SMapLowLevelOutput(struct netif *pNetIF, struct pbuf *pOutput)
Definition: ps2ip.c:147
static struct pbuf * TxHead
Definition: ps2ip.c:37
int ps2ip_getconfig(char *pszName, t_ip_info *pInfo)
Definition: ps2ip.c:41
int ps2ip_setconfig(const t_ip_info *pInfo)
Definition: ps2ip.c:79
void ps2ipSetHsyncTicksPerMSec(unsigned char ticks)
Definition: ps2ip.c:346
static void EnQTxPacket(struct pbuf *tx)
Definition: ps2ip.c:131
static void * AllocRxPacket(unsigned int size, void **payload)
Definition: ps2ip.c:181
static int NextTxPacket(void **payload)
Definition: ps2ip.c:206
err_t ps2ip_input(PBuf *pInput, NetIF *pNetIF)
Definition: ps2ip.c:290
static void InitializeLWIP(void)
Definition: ps2ip.c:300
static struct pbuf * TxTail
Definition: ps2ip.c:37
void ps2ipDeinit(void)
Definition: ps2ip.c:342
static void InitDone(void *pvArg)
Definition: ps2ip.c:262
unsigned short int hsyncTicksPerMSec
Definition: ps2ip.c:39
static void EnQRxPacket(void *packet)
Definition: ps2ip.c:201
static void ReallocRxPacket(void *packet, unsigned int size)
Definition: ps2ip.c:191
static err_t SMapIFInit(struct netif *pNetIF)
Definition: ps2ip.c:269
u32 s_addr
Definition: tcpip.h:1278
Definition: tcpip.h:1065
u8 flags
Definition: tcpip.h:1145
char name[2]
Definition: tcpip.h:1147
netif_output_fn output
Definition: tcpip.h:1090
netif_linkoutput_fn linkoutput
Definition: tcpip.h:1095
u8 hwaddr_len
Definition: tcpip.h:1141
u16 mtu
Definition: tcpip.h:1139
u8 hwaddr[NETIF_MAX_HWADDR_LEN]
Definition: tcpip.h:1143
Definition: tcpip.h:165
struct pbuf * next
Definition: tcpip.h:167
void * payload
Definition: tcpip.h:170
u16 tot_len
Definition: tcpip.h:179
u16 len
Definition: tcpip.h:182
struct in_addr ipaddr
u32 dhcp_enabled
struct in_addr gw
u8 hw_addr[8]
struct in_addr netmask
char netif_name[4]
void sys_sem_free(sys_sem_t *Sema)
Definition: sys_arch.c:411
void sys_sem_signal(sys_sem_t *Sema)
Definition: sys_arch.c:406
u32_t sys_arch_sem_wait(sys_sem_t *sema, u32_t timeout)
Definition: sys_arch.c:373
err_t sys_sem_new(sys_sem_t *sem, u8_t count)
Definition: sys_arch.c:352
int sys_sem_t
Definition: sys_arch.h:25
#define NULL
Definition: tamtypes.h:91
signed char err_t
Definition: tcpip.h:95
@ PBUF_POOL
Definition: tcpip.h:161
#define NETIF_MAX_HWADDR_LEN
Definition: tcpip.h:903
@ DHCP_STATE_OFF
Definition: tcpip.h:1260
@ PBUF_RAW
Definition: tcpip.h:132