1 /*---------------------------------------------------------------------------*
2   Project:      NWCM Library
3   File:         nwcm.h
4   Copyright (C)2008 Nintendo  All rights reserved.
5   These coded instructions, statements, and computer programs contain
6   proprietary information of Nintendo of America Inc. and/or Nintendo
7   Company Ltd., and are protected by Federal copyright law. They may
8   not be disclosed to third parties or copied or duplicated in any form,
9   in whole or in part, without the prior written consent of Nintendo.
10   @version $Id: nwcm.h 16441 2010-05-20 00:57:11Z takiguchi_shinichi $
11  *---------------------------------------------------------------------------
12 
13 
14 */
15 
16 #ifndef NWCM_H
17 #define NWCM_H
18 
19 #include <nnet.h>
20 
21 #ifdef  __cplusplus
22 extern "C" {
23 #endif
24 
25 /*---------------------------------------------------------------------------*
26 * WCM Temporary Definitions
27 *---------------------------------------------------------------------------*/
28 // Types of Control Constants
29 // Size of each type of data (bytes)
30 #define WM_SIZE_BSSID           6
31 #define WM_SIZE_SSID            32
32 
33 #define WCM_CAM_LIFETIME            80              // CAM lifetime (8s)
34 #define WCM_BSSID_SIZE              WM_SIZE_BSSID   // BSS-ID size (6 bytes)
35 #define WCM_ESSID_SIZE              WM_SIZE_SSID    // ESS-ID maximum size (32 bytes)
36 
37 #define WCM_OPTION_TEST_AUTH        0x00080000
38 #define WCM_OPTION_FILTER_AUTH      0x00040000
39 
40 // WEP encryption mode type
41 #define WCM_WEPMODE_NONE        0                      // No encryption
42 #define WCM_WEPMODE_40          1                      // RC4 (40-bit) encryption mode
43 #define WCM_WEPMODE_104         2                      // RC4 (104-bit) encryption mode
44 #define WCM_WEPMODE_128         3                      // RC4 (128-bit) encryption mode
45 
46 // AP authentication modes
47 #define WCM_OPTION_TEST_AUTH        0x00080000
48 #define WCM_OPTION_FILTER_AUTH      0x00040000
49 #define WCM_OPTION_MASK_AUTH        (WCM_OPTION_TEST_AUTH | WCM_OPTION_FILTER_AUTH)
50 #define WCM_OPTION_AUTH_OPENSYSTEM  (WCM_OPTION_TEST_AUTH | 0x00000000)
51 #define WCM_OPTION_AUTH_SHAREDKEY   (WCM_OPTION_TEST_AUTH | 0x00040000)
52 
53 #define WM_SIZE_WEPKEY          80
54 #define WCM_WEP_SIZE                WM_SIZE_WEPKEY  // 80 bytes
55 
56 /*---------------------------------------------------------------------------*
57  * Definitions
58  *---------------------------------------------------------------------------*/
59 #define NWCM_DEVICE_1               0           /* Only 1 interface can be used with this driver */
60 
61 #define NWCM_VENDOR_NINTENDO        0x0000
62 #define NWCM_COMMON_CONF_VERSION    0x1100
63 #define NWCM_INS_CONF_VERSION       0x2100
64 
65 #define NWCM_MEM_BASE                            0x00050000
66 #define NWCM_MEM_NAME(name)                    ((s32)(NWCM_MEM_BASE|(name)))
67 enum {
68     NWCM_MEM_INSTANCE = NWCM_MEM_NAME(1)
69 };
70 
71 
72 #define WM_SIZE_USER_GAMEINFO   112    // Maximum user area size for GameInfo
73 #define WM_SIZE_USERNAME        8
74 #define WM_SIZE_GAMENAME        16
75 typedef struct WMGameInfo
76 {
77     u16     magicNumber;               // == 0x0001
78     u8      ver;                       // GameInfo Version 1
79     u8      platform;                  // DS: 0, Wii: 8
80     u32     ggid;
81     u16     tgid;
82     u8      userGameInfoLength;        // Length of user area
83     union
84     {
85         // (CS_Flag):1, (KS_Flag):1, multibootFlag:1, entryFlag:1
86         u8      gameNameCount_attribute;        // Old name for compatibility purposes
87         u8      attribute;
88     };
89     u16     parentMaxSize;             //Maximum parent send size
90     u16     childMaxSize;              //Maximum child send size
91     union
92     {
93         u16     userGameInfo[WM_SIZE_USER_GAMEINFO / sizeof(u16)];      // User region (Maximum: 112 bytes (provisional))
94         struct
95         {
96             u16     userName[WM_SIZE_USERNAME / sizeof(u16)];   // WM_SIZE_USERNAME -> 8
97             u16     gameName[WM_SIZE_GAMENAME / sizeof(u16)];   // WM_SIZE_GAMENAME -> 16
98             u16     padd1[44];         // pad -> 88
99         }
100         old_type;                      // Total: 112 bytes
101     };
102 }
103 WMGameInfo, WMgameInfo;
104 
105 
106 
107 typedef struct WCMBssDesc
108 {
109     u16             length;                    // 2
110     u16             rssi;                      // 4
111     u8              bssid[WM_SIZE_BSSID];      // 10
112     u16             ssidLength;                // 12
113     u8              ssid[WM_SIZE_SSID];        // 44
114     u16             capaInfo;                  // 46
115     struct
116     {
117         u16             basic;                 // 48
118         u16             support;               // 50
119     }
120     rateSet;
121     u16             beaconPeriod;              // 52
122     u16             dtimPeriod;                // 54
123     u16             channel;                   // 56
124     u16             cfpPeriod;                 // 58
125     u16             cfpMaxDuration;            // 60
126     u16             gameInfoLength;            // 62
127     union
128     {
129         u16             otherElementCount;
130         u16             elementCount;
131     };                                         // 64
132     union
133     {
134         WMGameInfo      gameInfo;
135         u16             elements[1];
136     };
137 } WCMBssDesc;
138 
139 
140 
141 /* Please see man pages for details
142 
143  */
144 enum {
145     NWCM_STATE_INIT = 0,        /*Initial state*/
146     NWCM_STATE_WAIT_LINKUP,     /* Wait for association to AP*/
147     NWCM_STATE_ACTIVE,          /*Connection completed state*/
148     NWCM_STATE_ABORT,           /*Cancel association*/
149     NWCM_STATE_MORIBUND         /*Disconnecting*/
150 };
151 
152 /*---------------------------------------------------------------------------*
153  * Types/Declarations
154  *---------------------------------------------------------------------------*/
155 #define NWCM_MAC_LEN            6
156 
157 /* Please see man pages for details
158 
159  */
160 typedef struct NWCMProtocolList {
161     u16 protocol;                   /*IP, ARP, etc.*/
162     NNETInstance *upper_ins;            /*Upper instance*/
163     NOSMessageQueueId mq_receive;   /*Queue to insert packet when above protocol is received*/
164 } NWCMProtocolList;
165 
166 /* Please see man pages for details
167 
168  */
169 typedef struct NWCMCommonConfig {
170     u16         vendor;             /* NWCM_VENDOR_NINTENDO */
171     u16         version;            /* NWCM_COMMON_CONF_VERSION */
172 
173     /*
174         No arguments
175     */
176 
177 } NWCMCommonConfig;
178 
179 
180 /* Please see man pages for details
181 
182  */
183 #define NWCM_PROTOCOL_NUM   8   /* ARP, RARP, IP, PPPoE Discovery, PPPoE Session */
184 #define NWCM_UPPER_NUM      2   /*Number of upper layers*/
185 #define NWCM_MQ_NUM         16  /*Size of queue to pass to upper layers*/
186 #define NWCM_MAX_SEND_LEN   1600
187 typedef struct NWCMMessage {
188     NOSMessageQueue mq; /*Receive queue for upper layers*/
189     NOSMessage  mqarray[NWCM_MQ_NUM];   /*Receive queue instance for upper layers*/
190 } NWCMMessage;
191 
192 #define NWCM_VENDOR_NINTENDO        0x0000
193 #define NWCM_COMMON_CONF_VERSION    0x1100
194 #define NWCM_INS_CONF_VERSION       0x2100
195 
196 /* Please see man pages for details
197 
198  */
199 #define NWCM_BSSID_SIZE     WCM_BSSID_SIZE      /* BSSID size */
200 #define NWCM_ESSID_SIZE     WCM_ESSID_SIZE      /* ESSID size */
201 
202 /* AP authentication modes */
203 #define NWCM_OPTION_TEST_AUTH        WCM_OPTION_TEST_AUTH
204 #define NWCM_OPTION_FILTER_AUTH      WCM_OPTION_FILTER_AUTH
205 #define NWCM_OPTION_MASK_AUTH        WCM_OPTION_MASK_AUTH
206 #define NWCM_OPTION_AUTH_OPENSYSTEM  WCM_OPTION_AUTH_OPENSYSTEM
207 
208 
209 /* WEP encryption mode type */
210 #define NWCM_WEPMODE_NONE    WCM_WEPMODE_NONE    /* No encryption */
211 #define NWCM_WEPMODE_40      WCM_WEPMODE_40      /* RC4 (40-bit) encryption mode */
212 #define NWCM_WEPMODE_104     WCM_WEPMODE_104     /* RC4 (104-bit) encryption mode */
213 #define NWCM_WEPMODE_128     WCM_WEPMODE_128     /* RC4 (128-bit) encryption mode */
214 #define NWCM_SIZE_WEPKEY     WCM_WEP_SIZE        /* WEP key length */
215 typedef struct NWCMWepDesc {
216     u8  mode;                   /* WEP encryption mode ( WCM_WEPMODE_* ) */
217     u8  keyId;                  /*/ WEP key ID (0 to 3) */
218     u8  key[NWCM_SIZE_WEPKEY];  /*  WEP key data string [20 * 4 bytes] */
219 } NWCMWepDesc;
220 typedef NWCMWepDesc WCMWepDesc;
221 
222 
223 typedef struct NWCMInstanceConfig {
224     u16         vendor;             /* NWCM_VENDOR_NINTENDO */
225     u16         version;            /* NWCM_INS_CONF_VERSION */
226 
227     u8          bssId[NWCM_BSSID_SIZE];
228     NWCMWepDesc  wepDesc;
229     u8          essId[NWCM_ESSID_SIZE];
230     u32         auth_option;
231 } NWCMInstanceConfig;
232 
233 
234 typedef struct NWCMInstance {
235     NNETInstance ins;                   /* Instance information common to all layers */
236 
237     NWCMInstanceConfig config;          /*Instance-specific settings*/
238 
239     NWCMProtocolList upper_protocol[NWCM_PROTOCOL_NUM]; /*Upper protocol and message queue for sending*/
240     s32 upper_protocol_count;
241 
242     NWCMMessage upper_mq[NWCM_UPPER_NUM];   /*Receive queue for upper layers*/
243 
244     s32 upper_ins_count;
245 
246     s32 state;
247 
248     u8 macaddr[NWCM_MAC_LEN];   /*Local MAC address*/
249 
250     u8 sendBuf[NWCM_MAX_SEND_LEN];  /*Send buffer*/
251 
252 } NWCMInstance;
253 
254 
255 
256 
257 /*
258     Get NNETInstance from WCM instance
259 */
NWCM_GetNNETInstance(NWCMInstance * wcm_ins)260 NN_INLINE NNETInstance *NWCM_GetNNETInstance(NWCMInstance *wcm_ins)
261 {
262     return (NNETInstance *)wcm_ins;
263 }
264 
265 /*
266     Get WCM instance from NNETInstance
267 */
NWCM_GetNWCMInstance(NNETInstance * ins)268 NN_INLINE NWCMInstance *NWCM_GetNWCMInstance(NNETInstance *ins)
269 {
270     return (NWCMInstance *)ins;
271 }
272 
273 /*---------------------------------------------------------------------------*
274  * Function Prototypes
275  *---------------------------------------------------------------------------*/
276 /* nwcm_main.c */
277  s32 NWCM_Init(const NNETAllocator *allocator);
278  s32 NWCM_Finish(void);
279  s32 NWCM_SetCommonConfig(const NWCMCommonConfig *common_conf);
280  s32 NWCM_SetInstanceConfig(NWCMInstance *wcm_ins, const NWCMInstanceConfig *ins_conf);
281  s32 NWCM_StartupIns(NNETInstance *ins, s32 timeout);
282  s32 NWCM_CleanupIns(NNETInstance *ins, s32 timeout);
283  s32 NWCM_AbortIns(NNETInstance *ins);
284  s32 NWCM_CreateIns(NWCMInstance **wcm_ins, u32 device_id);
285  s32 NWCM_DestroyIns(NWCMInstance *wcm_ins);
286  s32 NWCM_GetStateIns(NWCMInstance *wcm_ins);
287  s32 NWCM_GetUpperAPI(NNETEthernetAPI *wcm_api, NWCMInstance *wcm_ins);
288  s32 NWCM_SetUpper(NNETInstance *ins, NNETInstance *upper_ins, u32 *receive_id, u16 entry[]);
289  s32 NWCM_ClearUpper(NNETInstance *ins, NNETInstance *upper_ins);
290  s32 NWCM_GetMacAddr(NNETInstance *ins, u8 *macaddr);
291  s32 NWCM_SetMulticastAddr(NNETInstance *ins, u8 *macaddr);
292  s32 NWCM_ClearMulticastAddr(NNETInstance *ins, u8 *macaddr);
293  s32 NWCM_Send(NNETInstance *ins, u8 *dstMAC, NOSMessageBuf *mbuf);
294  s32 NWCM_Receive(NNETInstance *ins, u32 receive_id, NOSMessageBuf **mbuf);
295  s32 NWCM_CancelReceive(NNETInstance *ins, u32 receive_id);
296 
297 #ifdef NDEBUG_ENABLE
298 extern void NWCM_Dummy_Recv(const u8 *srcAddr, const u8 *dstAddr, const u8 *buf, s32 len);
299 #endif
300 
301 /* nwcm_state.c */
302 extern void NWCM_ChangeStateIns(NWCMInstance *ins, s32 state);
303 
304 extern NWCMCommonConfig nwcmCommonConfig;
305 extern NWCMInstance *wcmIns;
306 extern NWCMInstanceConfig CU_nwcmInsConfig;
307 
308 #ifdef  __cplusplus
309 }
310 #endif
311 
312 /* NWCM_H */
313 #endif
314