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