]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/staging/rt3090/common/mlme_ex.c
Staging: rt2860: add RT3090 chipset support
[net-next-2.6.git] / drivers / staging / rt3090 / common / mlme_ex.c
1 /*
2  *************************************************************************
3  * Ralink Tech Inc.
4  * 5F., No.36, Taiyuan St., Jhubei City,
5  * Hsinchu County 302,
6  * Taiwan, R.O.C.
7  *
8  * (c) Copyright 2002-2007, Ralink Technology, Inc.
9  *
10  * This program is free software; you can redistribute it and/or modify  *
11  * it under the terms of the GNU General Public License as published by  *
12  * the Free Software Foundation; either version 2 of the License, or     *
13  * (at your option) any later version.                                   *
14  *                                                                       *
15  * This program is distributed in the hope that it will be useful,       *
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  * GNU General Public License for more details.                          *
19  *                                                                       *
20  * You should have received a copy of the GNU General Public License     *
21  * along with this program; if not, write to the                         *
22  * Free Software Foundation, Inc.,                                       *
23  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
24  *                                                                       *
25  *************************************************************************
26
27         Module Name:
28         mlme_ex.c
29
30         Abstract:
31         Miniport generic portion header file
32
33         Revision History:
34         Who         When          What
35         --------    ----------    ----------------------------------------------
36         Fonchi          2007-06-25              Extend original mlme APIs to support multi-entries
37 */
38
39 #include "../rt_config.h"
40 #include "../mlme_ex_def.h"
41 //#include <stdarg.h>
42
43
44 // ===========================================================================================
45 // state_machine
46 // ===========================================================================================
47
48 /*! \brief Initialize the state machine.
49  *  \param *S           pointer to the state machine
50  *  \param  Trans       State machine transition function
51  *  \param  StNr        number of states
52  *  \param  MsgNr       number of messages
53  *  \param  DefFunc     default function, when there is invalid state/message combination
54  *  \param  InitState   initial state of the state machine
55  *  \param  Base        StateMachine base, internal use only
56  *  \pre p_sm should be a legal pointer
57  *  \post
58  */
59 VOID StateMachineInitEx(
60         IN STATE_MACHINE_EX *S,
61         IN STATE_MACHINE_FUNC_EX Trans[],
62         IN ULONG StNr,
63         IN ULONG MsgNr,
64         IN STATE_MACHINE_FUNC_EX DefFunc,
65         IN ULONG InitState,
66         IN ULONG Base)
67 {
68         ULONG i, j;
69
70         // set number of states and messages
71         S->NrState = StNr;
72         S->NrMsg   = MsgNr;
73         S->Base    = Base;
74
75         S->TransFunc  = Trans;
76
77         // init all state transition to default function
78         for (i = 0; i < StNr; i++)
79         {
80                 for (j = 0; j < MsgNr; j++)
81                 {
82                         S->TransFunc[i * MsgNr + j] = DefFunc;
83                 }
84         }
85
86         // set the starting state
87         S->CurrState = InitState;
88
89         return;
90 }
91
92 /*! \brief This function fills in the function pointer into the cell in the state machine
93  *  \param *S   pointer to the state machine
94  *  \param St   state
95  *  \param Msg  incoming message
96  *  \param f    the function to be executed when (state, message) combination occurs at the state machine
97  *  \pre *S should be a legal pointer to the state machine, st, msg, should be all within the range, Base should be set in the initial state
98  *  \post
99  */
100 VOID StateMachineSetActionEx(
101         IN STATE_MACHINE_EX *S,
102         IN ULONG St,
103         IN ULONG Msg,
104         IN STATE_MACHINE_FUNC_EX Func)
105 {
106         ULONG MsgIdx;
107
108         MsgIdx = Msg - S->Base;
109
110         if (St < S->NrState && MsgIdx < S->NrMsg)
111         {
112                 // boundary checking before setting the action
113                 S->TransFunc[St * S->NrMsg + MsgIdx] = Func;
114         }
115
116         return;
117 }
118
119 /*! \brief   This function does the state transition
120  *  \param   *Adapter the NIC adapter pointer
121  *  \param   *S       the state machine
122  *  \param   *Elem    the message to be executed
123  *  \return   None
124  */
125 VOID StateMachinePerformActionEx(
126         IN PRTMP_ADAPTER        pAd,
127         IN STATE_MACHINE_EX *S,
128         IN MLME_QUEUE_ELEM *Elem,
129         USHORT Idx,
130         PULONG pCurrState)
131 {
132         if (S->TransFunc[(*pCurrState) * S->NrMsg + Elem->MsgType - S->Base])
133                 (*(S->TransFunc[(*pCurrState) * S->NrMsg + Elem->MsgType - S->Base]))(pAd, Elem, pCurrState, Idx);
134
135         return;
136 }
137
138 /*! \brief   Enqueue a message for other threads, if they want to send messages to MLME thread
139  *  \param  *Queue    The MLME Queue
140  *  \param   Machine  The State Machine Id
141  *  \param   MsgType  The Message Type
142  *  \param   MsgLen   The Message length
143  *  \param  *Msg      The message pointer
144  *  \return  TRUE if enqueue is successful, FALSE if the queue is full
145  *  \pre
146  *  \post
147  *  \note    The message has to be initialized
148  */
149 BOOLEAN MlmeEnqueueEx(
150         IN      PRTMP_ADAPTER   pAd,
151         IN ULONG Machine,
152         IN ULONG MsgType,
153         IN ULONG MsgLen,
154         IN VOID *Msg,
155         IN USHORT Idx)
156 {
157         INT Tail;
158         MLME_QUEUE *Queue = (MLME_QUEUE *)&pAd->Mlme.Queue;
159
160         // Do nothing if the driver is starting halt state.
161         // This might happen when timer already been fired before cancel timer with mlmehalt
162         if (RTMP_TEST_FLAG(pAd, fRTMP_ADAPTER_HALT_IN_PROGRESS))
163                 return FALSE;
164
165
166         // First check the size, it MUST not exceed the mlme queue size
167         if (MsgLen > MAX_LEN_OF_MLME_BUFFER)
168         {
169                 DBGPRINT_ERR(("MlmeEnqueueEx: msg too large, size = %ld \n", MsgLen));
170                 return FALSE;
171         }
172
173         if (MlmeQueueFull(Queue))
174         {
175
176                 return FALSE;
177         }
178
179         RTMP_SEM_LOCK(&Queue->Lock);
180         Tail = Queue->Tail;
181         Queue->Tail++;
182         Queue->Num++;
183         if (Queue->Tail == MAX_LEN_OF_MLME_QUEUE)
184         {
185                 Queue->Tail = 0;
186         }
187         Queue->Entry[Tail].Occupied = TRUE;
188         Queue->Entry[Tail].Machine = Machine;
189         Queue->Entry[Tail].MsgType = MsgType;
190         Queue->Entry[Tail].MsgLen = MsgLen;
191         Queue->Entry[Tail].Idx = Idx;
192         if (Msg != NULL)
193                 NdisMoveMemory(Queue->Entry[Tail].Msg, Msg, MsgLen);
194
195         RTMP_SEM_UNLOCK(&Queue->Lock);
196
197         return TRUE;
198 }
199
200 /*
201     ==========================================================================
202     Description:
203         The drop function, when machine executes this, the message is simply
204         ignored. This function does nothing, the message is freed in
205         StateMachinePerformAction()
206     ==========================================================================
207  */
208 VOID DropEx(
209     IN PRTMP_ADAPTER pAd,
210     IN MLME_QUEUE_ELEM *Elem,
211         PULONG pCurrState,
212         USHORT Idx)
213 {
214         return;
215 }