1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - WM - libraries
3   File:     wm_dcf.c
4 
5   Copyright 2003-2008 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-09-18#$
14   $Rev: 8573 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #include    <nitro/wm.h>
19 #include    "wm_arm9_private.h"
20 
21 
22 /*---------------------------------------------------------------------------*
23   Name:         WM_StartDCF
24 
25   Description:  Starts infrastructure-mode communications.
26 
27   Arguments:    callback    -   Callback function that is called when the asynchronous process completes.
28                 recvBuf     -   Pointer to the data receive buffer.
29                                 Pay attention to cache because ARM7 writes out data directly.
30                 recvBufSize -   Size of data receive buffer.
31 
32   Returns:      WMErrCode   -   Returns the processing result. Returns WM_ERRCODE_OPERATING if asynchronous processing started successfully. Afterwards, the asynchronous processing results will be passed to the callback.
33 
34 
35  *---------------------------------------------------------------------------*/
WM_StartDCF(WMCallbackFunc callback,WMDcfRecvBuf * recvBuf,u16 recvBufSize)36 WMErrCode WM_StartDCF(WMCallbackFunc callback, WMDcfRecvBuf *recvBuf, u16 recvBufSize)
37 {
38     WMErrCode result;
39     WMArm9Buf *p = WMi_GetSystemWork();
40 
41     // Check the state
42     result = WMi_CheckState(WM_STATE_CHILD);
43     WM_CHECK_RESULT(result);
44 
45     // Confirm DCF state
46     DC_InvalidateRange(&(p->status->dcf_flag), 4);
47     if (p->status->dcf_flag == TRUE)
48     {
49         WM_WARNING("Already DCF mode. So can't start DCF again.\n");
50         return WM_ERRCODE_ILLEGAL_STATE;
51     }
52 
53     // Check parameters
54     if (recvBufSize < 16)
55     {
56         WM_WARNING("Parameter \"recvBufSize\" must not be less than %d.\n", 16);
57         return WM_ERRCODE_INVALID_PARAM;
58     }
59     if (recvBuf == NULL)
60     {
61         WM_WARNING("Parameter \"recvBuf\" must not be NULL.\n");
62         return WM_ERRCODE_INVALID_PARAM;
63     }
64     if ((u32)recvBuf & 0x01f)
65     {
66         // Alignment check is a warning only, not an error
67         WM_WARNING("Parameter \"recvBuf\" is not 32-byte aligned.\n");
68     }
69 
70     // Write out the specified buffer's cache
71     DC_StoreRange(recvBuf, recvBufSize);
72 
73     // Register callback function
74     WMi_SetCallbackTable(WM_APIID_START_DCF, callback);
75 
76     // Notify ARM7 with FIFO
77     result = WMi_SendCommand(WM_APIID_START_DCF, 2, (u32)recvBuf, (u32)recvBufSize);
78     WM_CHECK_RESULT(result);
79 
80     return WM_ERRCODE_OPERATING;
81 }
82 
83 /*---------------------------------------------------------------------------*
84   Name:         WM_SetDCFData
85 
86   Description:  Reserves data for sending by infrastructure-mode communication.
87 
88   Arguments:    callback    -   Callback function that is called when the asynchronous process completes.
89                 destAdr     -   Pointer to the buffer that shows the MAC address of the communication partner.
90                 sendData    -   Pointer to the data to reserve send.
91                                 NOTE: The instance of the data reserved for sending is forcibly stored in cache.
92 
93                 sendDataSize -  Size of the data to reserve send.
94 
95   Returns:      WMErrCode   -   Returns the processing result. Returns WM_ERRCODE_OPERATING if asynchronous processing started successfully. Afterwards, the asynchronous processing results will be passed to the callback.
96 
97 
98  *---------------------------------------------------------------------------*/
99 WMErrCode
WM_SetDCFData(WMCallbackFunc callback,const u8 * destAdr,const u16 * sendData,u16 sendDataSize)100 WM_SetDCFData(WMCallbackFunc callback, const u8 *destAdr, const u16 *sendData, u16 sendDataSize)
101 {
102     WMErrCode result;
103     WMArm9Buf *p = WMi_GetSystemWork();
104     u32     wMac[2];
105 
106     // Check the state
107     result = WMi_CheckState(WM_STATE_DCF_CHILD);
108     WM_CHECK_RESULT(result);
109 
110     // Confirm DCF state
111     DC_InvalidateRange(&(p->status->dcf_flag), 4);
112     if (p->status->dcf_flag == FALSE)
113     {
114         WM_WARNING("It is not DCF mode now.\n");
115         return WM_ERRCODE_ILLEGAL_STATE;
116     }
117 
118     // Check parameters
119     if (sendDataSize > WM_DCF_MAX_SIZE)
120     {
121         WM_WARNING("Parameter \"sendDataSize\" is over %d.\n", WM_DCF_MAX_SIZE);
122         return WM_ERRCODE_INVALID_PARAM;
123     }
124     if ((u32)sendData & 0x01f)
125     {
126         // Alignment check is a warning only, not an error
127         WM_WARNING("Parameter \"sendData\" is not 32-byte aligned.\n");
128     }
129 
130     // Write out the specified buffer's cache
131     DC_StoreRange((void *)sendData, sendDataSize);
132 
133     // Register callback function
134     WMi_SetCallbackTable(WM_APIID_SET_DCF_DATA, callback);
135 
136     // Copy MAC address
137     MI_CpuCopy8(destAdr, wMac, 6);
138 
139     // Notify ARM7 with FIFO
140     result = WMi_SendCommand(WM_APIID_SET_DCF_DATA, 4,
141                              (u32)wMac[0], (u32)wMac[1], (u32)sendData, (u32)sendDataSize);
142     WM_CHECK_RESULT(result);
143 
144     return WM_ERRCODE_OPERATING;
145 }
146 
147 /*---------------------------------------------------------------------------*
148   Name:         WM_EndDCF
149 
150   Description:  Stops infrastructure-mode communications.
151 
152   Arguments:    callback    -   Callback function that is called when the asynchronous process completes.
153 
154   Returns:      WMErrCode   -   Returns the processing result. Returns WM_ERRCODE_OPERATING if asynchronous processing started successfully. Afterwards, the asynchronous processing results will be passed to the callback.
155 
156 
157  *---------------------------------------------------------------------------*/
WM_EndDCF(WMCallbackFunc callback)158 WMErrCode WM_EndDCF(WMCallbackFunc callback)
159 {
160     WMErrCode result;
161     WMArm9Buf *p = WMi_GetSystemWork();
162 
163     // Check the state
164     result = WMi_CheckState(WM_STATE_DCF_CHILD);
165     WM_CHECK_RESULT(result);
166 
167     // Confirm DCF state
168     DC_InvalidateRange(&(p->status->dcf_flag), 4);
169     if (p->status->dcf_flag == FALSE)
170     {
171         WM_WARNING("It is not DCF mode now.\n");
172         return WM_ERRCODE_ILLEGAL_STATE;
173     }
174 
175     // Register callback function
176     WMi_SetCallbackTable(WM_APIID_END_DCF, callback);
177 
178     // Notify ARM7 with FIFO
179     result = WMi_SendCommand(WM_APIID_END_DCF, 0);
180     WM_CHECK_RESULT(result);
181 
182     return WM_ERRCODE_OPERATING;
183 }
184 
185 /*---------------------------------------------------------------------------*
186     End of file
187  *---------------------------------------------------------------------------*/
188