Staging: w35und: merge wblinux struct to adapter
[linux-2.6] / drivers / staging / winbond / wblinux.c
1 //============================================================================
2 //  Copyright (c) 1996-2005 Winbond Electronic Corporation
3 //
4 //  Module Name:
5 //    wblinux.c
6 //
7 //  Abstract:
8 //    Linux releated routines
9 //
10 //============================================================================
11 #include "os_common.h"
12
13 u32
14 WBLINUX_MemoryAlloc(void* *VirtualAddress, u32 Length)
15 {
16         *VirtualAddress = kzalloc( Length, GFP_ATOMIC ); //GFP_KERNEL is not suitable
17
18         if (*VirtualAddress == NULL)
19                 return 0;
20         return 1;
21 }
22
23 s32
24 EncapAtomicInc(struct wb35_adapter * adapter, void* pAtomic)
25 {
26         u32     ltmp;
27         u32 *   pltmp = (u32 *)pAtomic;
28         spin_lock_irq( &adapter->AtomicSpinLock );
29         (*pltmp)++;
30         ltmp = (*pltmp);
31         spin_unlock_irq( &adapter->AtomicSpinLock );
32         return ltmp;
33 }
34
35 s32
36 EncapAtomicDec(struct wb35_adapter * adapter, void* pAtomic)
37 {
38         u32     ltmp;
39         u32 *   pltmp = (u32 *)pAtomic;
40         spin_lock_irq( &adapter->AtomicSpinLock );
41         (*pltmp)--;
42         ltmp = (*pltmp);
43         spin_unlock_irq( &adapter->AtomicSpinLock );
44         return ltmp;
45 }
46
47 unsigned char
48 WBLINUX_Initial(struct wb35_adapter * adapter)
49 {
50         spin_lock_init( &adapter->SpinLock );
51         spin_lock_init( &adapter->AtomicSpinLock );
52         return TRUE;
53 }
54
55 void
56 WBLinux_ReceivePacket(struct wb35_adapter * adapter, PRXLAYER1 pRxLayer1)
57 {
58         BUG();
59 }
60
61
62 void
63 WBLINUX_GetNextPacket(struct wb35_adapter * adapter,  PDESCRIPTOR pDes)
64 {
65         BUG();
66 }
67
68 void
69 WBLINUX_GetNextPacketCompleted(struct wb35_adapter * adapter, PDESCRIPTOR pDes)
70 {
71         BUG();
72 }
73
74 void
75 WBLINUX_Destroy(struct wb35_adapter * adapter)
76 {
77         WBLINUX_stop( adapter );
78 #ifdef _PE_USB_INI_DUMP_
79         WBDEBUG(("[w35und] unregister_netdev!\n"));
80 #endif
81 }
82
83 void
84 WBLINUX_stop(  struct wb35_adapter * adapter )
85 {
86         struct sk_buff *pSkb;
87
88         if (OS_ATOMIC_INC( adapter, &adapter->ThreadCount ) == 1) {
89                 // Shutdown module immediately
90                 adapter->shutdown = 1;
91
92                 while (adapter->skb_array[ adapter->skb_GetIndex ]) {
93                         // Trying to free the un-sending packet
94                         pSkb = adapter->skb_array[ adapter->skb_GetIndex ];
95                         adapter->skb_array[ adapter->skb_GetIndex ] = NULL;
96                         if( in_irq() )
97                                 dev_kfree_skb_irq( pSkb );
98                         else
99                                 dev_kfree_skb( pSkb );
100
101                         adapter->skb_GetIndex++;
102                         adapter->skb_GetIndex %= WBLINUX_PACKET_ARRAY_SIZE;
103                 }
104
105 #ifdef _PE_STATE_DUMP_
106                 WBDEBUG(( "[w35und] SKB_RELEASE OK\n" ));
107 #endif
108         }
109
110         OS_ATOMIC_DEC(adapter, &adapter->ThreadCount);
111 }
112
113 void
114 WbWlanHalt(struct wb35_adapter *adapter)
115 {
116         //---------------------
117         adapter->sLocalPara.ShutDowned = TRUE;
118
119         Mds_Destroy(adapter);
120
121         // Turn off Rx and Tx hardware ability
122         hal_stop(&adapter->sHwData);
123 #ifdef _PE_USB_INI_DUMP_
124         WBDEBUG(("[w35und] Hal_stop O.K.\n"));
125 #endif
126         msleep(100);// Waiting Irp completed
127
128         // Destroy the NDIS module
129         WBLINUX_Destroy(adapter);
130
131         // Halt the HAL
132         hal_halt(&adapter->sHwData, NULL);
133 }
134
135 unsigned char
136 WbWLanInitialize(struct wb35_adapter *adapter)
137 {
138         phw_data_t      pHwData;
139         u8              *pMacAddr;
140         u8              *pMacAddr2;
141         u32             InitStep = 0;
142         u8              EEPROM_region;
143         u8              HwRadioOff;
144
145         //
146         // Setting default value for Linux
147         //
148         adapter->sLocalPara.region_INF = REGION_AUTO;
149         adapter->sLocalPara.TxRateMode = RATE_AUTO;
150         psLOCAL->bMacOperationMode = MODE_802_11_BG;    // B/G mode
151         adapter->Mds.TxRTSThreshold = DEFAULT_RTSThreshold;
152         adapter->Mds.TxFragmentThreshold = DEFAULT_FRAGMENT_THRESHOLD;
153         hal_set_phy_type( &adapter->sHwData, RF_WB_242_1 );
154         adapter->sLocalPara.MTUsize = MAX_ETHERNET_PACKET_SIZE;
155         psLOCAL->bPreambleMode = AUTO_MODE;
156         adapter->sLocalPara.RadioOffStatus.boSwRadioOff = FALSE;
157         pHwData = &adapter->sHwData;
158         hal_set_phy_type( pHwData, RF_DECIDE_BY_INF );
159
160         //
161         // Initial each module and variable
162         //
163         if (!WBLINUX_Initial(adapter)) {
164 #ifdef _PE_USB_INI_DUMP_
165                 WBDEBUG(("[w35und]WBNDIS initialization failed\n"));
166 #endif
167                 goto error;
168         }
169
170         // Initial Software variable
171         adapter->sLocalPara.ShutDowned = FALSE;
172
173         //added by ws for wep key error detection
174         adapter->sLocalPara.bWepKeyError= FALSE;
175         adapter->sLocalPara.bToSelfPacketReceived = FALSE;
176         adapter->sLocalPara.WepKeyDetectTimerCount= 2 * 100; /// 2 seconds
177
178         // Initial USB hal
179         InitStep = 1;
180         pHwData = &adapter->sHwData;
181         if (!hal_init_hardware(pHwData, adapter))
182                 goto error;
183
184         EEPROM_region = hal_get_region_from_EEPROM( pHwData );
185         if (EEPROM_region != REGION_AUTO)
186                 psLOCAL->region = EEPROM_region;
187         else {
188                 if (psLOCAL->region_INF != REGION_AUTO)
189                         psLOCAL->region = psLOCAL->region_INF;
190                 else
191                         psLOCAL->region = REGION_USA;   //default setting
192         }
193
194         // Get Software setting flag from hal
195         adapter->sLocalPara.boAntennaDiversity = FALSE;
196         if (hal_software_set(pHwData) & 0x00000001)
197                 adapter->sLocalPara.boAntennaDiversity = TRUE;
198
199         //
200         // For TS module
201         //
202         InitStep = 2;
203
204         // For MDS module
205         InitStep = 3;
206         Mds_initial(adapter);
207
208         //=======================================
209         // Initialize the SME, SCAN, MLME, ROAM
210         //=======================================
211         InitStep = 4;
212         InitStep = 5;
213         InitStep = 6;
214
215         // If no user-defined address in the registry, use the addresss "burned" on the NIC instead.
216         pMacAddr = adapter->sLocalPara.ThisMacAddress;
217         pMacAddr2 = adapter->sLocalPara.PermanentAddress;
218         hal_get_permanent_address( pHwData, adapter->sLocalPara.PermanentAddress );// Reading ethernet address from EEPROM
219         if (OS_MEMORY_COMPARE(pMacAddr, "\x00\x00\x00\x00\x00\x00", MAC_ADDR_LENGTH )) // Is equal
220         {
221                 memcpy( pMacAddr, pMacAddr2, MAC_ADDR_LENGTH );
222         } else {
223                 // Set the user define MAC address
224                 hal_set_ethernet_address( pHwData, adapter->sLocalPara.ThisMacAddress );
225         }
226
227         //get current antenna
228         psLOCAL->bAntennaNo = hal_get_antenna_number(pHwData);
229 #ifdef _PE_STATE_DUMP_
230         WBDEBUG(("Driver init, antenna no = %d\n", psLOCAL->bAntennaNo));
231 #endif
232         hal_get_hw_radio_off( pHwData );
233
234         // Waiting for HAL setting OK
235         while (!hal_idle(pHwData))
236                 msleep(10);
237
238         MTO_Init(adapter);
239
240         HwRadioOff = hal_get_hw_radio_off( pHwData );
241         psLOCAL->RadioOffStatus.boHwRadioOff = !!HwRadioOff;
242
243         hal_set_radio_mode( pHwData, (unsigned char)(psLOCAL->RadioOffStatus.boSwRadioOff || psLOCAL->RadioOffStatus.boHwRadioOff) );
244
245         hal_driver_init_OK(pHwData) = 1; // Notify hal that the driver is ready now.
246         //set a tx power for reference.....
247 //      sme_set_tx_power_level(adapter, 12);    FIXME?
248         return TRUE;
249
250 error:
251         switch (InitStep) {
252         case 5:
253         case 4:
254         case 3: Mds_Destroy( adapter );
255         case 2:
256         case 1: WBLINUX_Destroy( adapter );
257                 hal_halt( pHwData, NULL );
258         case 0: break;
259         }
260
261         return FALSE;
262 }
263
264 void WBLINUX_ConnectStatus(struct wb35_adapter * adapter, u32 flag)
265 {
266         adapter->LinkStatus = flag; // OS_DISCONNECTED  or  OS_CONNECTED
267 }
268