026a5a8bc3403e5af885eb5131f6921ee783676a
[pandora-kernel.git] / drivers / staging / winbond / wbhal.c
1 #include "os_common.h"
2 #include "wbhal_f.h"
3 #include "wblinux_f.h"
4
5 void hal_get_ethernet_address( phw_data_t pHwData, u8 *current_address )
6 {
7         if( pHwData->SurpriseRemove ) return;
8
9         memcpy( current_address, pHwData->CurrentMacAddress, ETH_LENGTH_OF_ADDRESS );
10 }
11
12 void hal_set_ethernet_address( phw_data_t pHwData, u8 *current_address )
13 {
14         u32 ltmp[2];
15
16         if( pHwData->SurpriseRemove ) return;
17
18         memcpy( pHwData->CurrentMacAddress, current_address, ETH_LENGTH_OF_ADDRESS );
19
20         ltmp[0]= cpu_to_le32( *(u32 *)pHwData->CurrentMacAddress );
21         ltmp[1]= cpu_to_le32( *(u32 *)(pHwData->CurrentMacAddress + 4) ) & 0xffff;
22
23         Wb35Reg_BurstWrite( pHwData, 0x03e8, ltmp, 2, AUTO_INCREMENT );
24 }
25
26 void hal_get_permanent_address( phw_data_t pHwData, u8 *pethernet_address )
27 {
28         if( pHwData->SurpriseRemove ) return;
29
30         memcpy( pethernet_address, pHwData->PermanentMacAddress, 6 );
31 }
32
33 static void hal_led_control(unsigned long data)
34 {
35         struct wbsoft_priv *adapter = (struct wbsoft_priv *) data;
36         phw_data_t pHwData = &adapter->sHwData;
37         struct wb35_reg *reg = &pHwData->reg;
38         u32     LEDSet = (pHwData->SoftwareSet & HAL_LED_SET_MASK) >> HAL_LED_SET_SHIFT;
39         u8      LEDgray[20] = { 0,3,4,6,8,10,11,12,13,14,15,14,13,12,11,10,8,6,4,2 };
40         u8      LEDgray2[30] = { 7,8,9,10,11,12,13,14,15,0,0,0,0,0,0,0,0,0,0,0,0,0,15,14,13,12,11,10,9,8 };
41         u32     TimeInterval = 500, ltmp, ltmp2;
42         ltmp=0;
43
44         if( pHwData->SurpriseRemove ) return;
45
46         if( pHwData->LED_control ) {
47                 ltmp2 = pHwData->LED_control & 0xff;
48                 if( ltmp2 == 5 ) // 5 is WPS mode
49                 {
50                         TimeInterval = 100;
51                         ltmp2 = (pHwData->LED_control>>8) & 0xff;
52                         switch( ltmp2 )
53                         {
54                                 case 1: // [0.2 On][0.1 Off]...
55                                         pHwData->LED_Blinking %= 3;
56                                         ltmp = 0x1010; // Led 1 & 0 Green and Red
57                                         if( pHwData->LED_Blinking == 2 ) // Turn off
58                                                 ltmp = 0;
59                                         break;
60                                 case 2: // [0.1 On][0.1 Off]...
61                                         pHwData->LED_Blinking %= 2;
62                                         ltmp = 0x0010; // Led 0 red color
63                                         if( pHwData->LED_Blinking ) // Turn off
64                                                 ltmp = 0;
65                                         break;
66                                 case 3: // [0.1 On][0.1 Off][0.1 On][0.1 Off][0.1 On][0.1 Off][0.1 On][0.1 Off][0.1 On][0.1 Off][0.5 Off]...
67                                         pHwData->LED_Blinking %= 15;
68                                         ltmp = 0x0010; // Led 0 red color
69                                         if( (pHwData->LED_Blinking >= 9) || (pHwData->LED_Blinking%2) ) // Turn off 0.6 sec
70                                                 ltmp = 0;
71                                         break;
72                                 case 4: // [300 On][ off ]
73                                         ltmp = 0x1000; // Led 1 Green color
74                                         if( pHwData->LED_Blinking >= 3000 )
75                                                 ltmp = 0; // led maybe on after 300sec * 32bit counter overlap.
76                                         break;
77                         }
78                         pHwData->LED_Blinking++;
79
80                         reg->U1BC_LEDConfigure = ltmp;
81                         if( LEDSet != 7 ) // Only 111 mode has 2 LEDs on PCB.
82                         {
83                                 reg->U1BC_LEDConfigure |= (ltmp &0xff)<<8; // Copy LED result to each LED control register
84                                 reg->U1BC_LEDConfigure |= (ltmp &0xff00)>>8;
85                         }
86                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure );
87                 }
88         }
89         else if( pHwData->CurrentRadioSw || pHwData->CurrentRadioHw ) // If radio off
90         {
91                 if( reg->U1BC_LEDConfigure & 0x1010 )
92                 {
93                         reg->U1BC_LEDConfigure &= ~0x1010;
94                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure );
95                 }
96         }
97         else
98         {
99                 switch( LEDSet )
100                 {
101                         case 4: // [100] Only 1 Led be placed on PCB and use pin 21 of IC. Use LED_0 for showing
102                                 if( !pHwData->LED_LinkOn ) // Blink only if not Link On
103                                 {
104                                         // Blinking if scanning is on progress
105                                         if( pHwData->LED_Scanning )
106                                         {
107                                                 if( pHwData->LED_Blinking == 0 )
108                                                 {
109                                                         reg->U1BC_LEDConfigure |= 0x10;
110                                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_0 On
111                                                         pHwData->LED_Blinking = 1;
112                                                         TimeInterval = 300;
113                                                 }
114                                                 else
115                                                 {
116                                                         reg->U1BC_LEDConfigure &= ~0x10;
117                                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_0 Off
118                                                         pHwData->LED_Blinking = 0;
119                                                         TimeInterval = 300;
120                                                 }
121                                         }
122                                         else
123                                         {
124                                                 //Turn Off LED_0
125                                                 if( reg->U1BC_LEDConfigure & 0x10 )
126                                                 {
127                                                         reg->U1BC_LEDConfigure &= ~0x10;
128                                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_0 Off
129                                                 }
130                                         }
131                                 }
132                                 else
133                                 {
134                                         // Turn On LED_0
135                                         if( (reg->U1BC_LEDConfigure & 0x10) == 0 )
136                                         {
137                                                 reg->U1BC_LEDConfigure |= 0x10;
138                                                 Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_0 Off
139                                         }
140                                 }
141                                 break;
142
143                         case 6: // [110] Only 1 Led be placed on PCB and use pin 21 of IC. Use LED_0 for showing
144                                 if( !pHwData->LED_LinkOn ) // Blink only if not Link On
145                                 {
146                                         // Blinking if scanning is on progress
147                                         if( pHwData->LED_Scanning )
148                                         {
149                                                 if( pHwData->LED_Blinking == 0 )
150                                                 {
151                                                         reg->U1BC_LEDConfigure &= ~0xf;
152                                                         reg->U1BC_LEDConfigure |= 0x10;
153                                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_0 On
154                                                         pHwData->LED_Blinking = 1;
155                                                         TimeInterval = 300;
156                                                 }
157                                                 else
158                                                 {
159                                                         reg->U1BC_LEDConfigure &= ~0x1f;
160                                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_0 Off
161                                                         pHwData->LED_Blinking = 0;
162                                                         TimeInterval = 300;
163                                                 }
164                                         }
165                                         else
166                                         {
167                                                 // 20060901 Gray blinking if in disconnect state and not scanning
168                                                 ltmp = reg->U1BC_LEDConfigure;
169                                                 reg->U1BC_LEDConfigure &= ~0x1f;
170                                                 if( LEDgray2[(pHwData->LED_Blinking%30)] )
171                                                 {
172                                                         reg->U1BC_LEDConfigure |= 0x10;
173                                                         reg->U1BC_LEDConfigure |= LEDgray2[ (pHwData->LED_Blinking%30) ];
174                                                 }
175                                                 pHwData->LED_Blinking++;
176                                                 if( reg->U1BC_LEDConfigure != ltmp )
177                                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_0 Off
178                                                 TimeInterval = 100;
179                                         }
180                                 }
181                                 else
182                                 {
183                                         // Turn On LED_0
184                                         if( (reg->U1BC_LEDConfigure & 0x10) == 0 )
185                                         {
186                                                 reg->U1BC_LEDConfigure |= 0x10;
187                                                 Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_0 Off
188                                         }
189                                 }
190                                 break;
191
192                         case 5: // [101] Only 1 Led be placed on PCB and use LED_1 for showing
193                                 if( !pHwData->LED_LinkOn ) // Blink only if not Link On
194                                 {
195                                         // Blinking if scanning is on progress
196                                         if( pHwData->LED_Scanning )
197                                         {
198                                                 if( pHwData->LED_Blinking == 0 )
199                                                 {
200                                                         reg->U1BC_LEDConfigure |= 0x1000;
201                                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_1 On
202                                                         pHwData->LED_Blinking = 1;
203                                                         TimeInterval = 300;
204                                                 }
205                                                 else
206                                                 {
207                                                         reg->U1BC_LEDConfigure &= ~0x1000;
208                                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_1 Off
209                                                         pHwData->LED_Blinking = 0;
210                                                         TimeInterval = 300;
211                                                 }
212                                         }
213                                         else
214                                         {
215                                                 //Turn Off LED_1
216                                                 if( reg->U1BC_LEDConfigure & 0x1000 )
217                                                 {
218                                                         reg->U1BC_LEDConfigure &= ~0x1000;
219                                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_1 Off
220                                                 }
221                                         }
222                                 }
223                                 else
224                                 {
225                                         // Is transmitting/receiving ??
226                                         if( (adapter->RxByteCount != pHwData->RxByteCountLast ) ||
227                                                 (adapter->TxByteCount != pHwData->TxByteCountLast ) )
228                                         {
229                                                 if( (reg->U1BC_LEDConfigure & 0x3000) != 0x3000 )
230                                                 {
231                                                         reg->U1BC_LEDConfigure |= 0x3000;
232                                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_1 On
233                                                 }
234
235                                                 // Update variable
236                                                 pHwData->RxByteCountLast = adapter->RxByteCount;
237                                                 pHwData->TxByteCountLast = adapter->TxByteCount;
238                                                 TimeInterval = 200;
239                                         }
240                                         else
241                                         {
242                                                 // Turn On LED_1 and blinking if transmitting/receiving
243                                                  if( (reg->U1BC_LEDConfigure & 0x3000) != 0x1000 )
244                                                  {
245                                                          reg->U1BC_LEDConfigure &= ~0x3000;
246                                                          reg->U1BC_LEDConfigure |= 0x1000;
247                                                          Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_1 On
248                                                  }
249                                         }
250                                 }
251                                 break;
252
253                         default: // Default setting. 2 LED be placed on PCB. LED_0: Link On LED_1 Active
254                                 if( (reg->U1BC_LEDConfigure & 0x3000) != 0x3000 )
255                                 {
256                                         reg->U1BC_LEDConfigure |= 0x3000;// LED_1 is always on and event enable
257                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure );
258                                 }
259
260                                 if( pHwData->LED_Blinking )
261                                 {
262                                         // Gray blinking
263                                         reg->U1BC_LEDConfigure &= ~0x0f;
264                                         reg->U1BC_LEDConfigure |= 0x10;
265                                         reg->U1BC_LEDConfigure |= LEDgray[ (pHwData->LED_Blinking-1)%20 ];
266                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure );
267
268                                         pHwData->LED_Blinking += 2;
269                                         if( pHwData->LED_Blinking < 40 )
270                                                 TimeInterval = 100;
271                                         else
272                                         {
273                                                 pHwData->LED_Blinking = 0; // Stop blinking
274                                                 reg->U1BC_LEDConfigure &= ~0x0f;
275                                                 Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure );
276                                         }
277                                         break;
278                                 }
279
280                                 if( pHwData->LED_LinkOn )
281                                 {
282                                         if( !(reg->U1BC_LEDConfigure & 0x10) ) // Check the LED_0
283                                         {
284                                                 //Try to turn ON LED_0 after gray blinking
285                                                 reg->U1BC_LEDConfigure |= 0x10;
286                                                 pHwData->LED_Blinking = 1; //Start blinking
287                                                 TimeInterval = 50;
288                                         }
289                                 }
290                                 else
291                                 {
292                                         if( reg->U1BC_LEDConfigure & 0x10 ) // Check the LED_0
293                                         {
294                                                 reg->U1BC_LEDConfigure &= ~0x10;
295                                                 Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure );
296                                         }
297                                 }
298                                 break;
299                 }
300
301                 //20060828.1 Active send null packet to avoid AP disconnect
302                 if( pHwData->LED_LinkOn )
303                 {
304                         pHwData->NullPacketCount += TimeInterval;
305                         if( pHwData->NullPacketCount >= DEFAULT_NULL_PACKET_COUNT )
306                         {
307                                 pHwData->NullPacketCount = 0;
308                         }
309                 }
310         }
311
312         pHwData->time_count += TimeInterval;
313         Wb35Tx_CurrentTime(adapter, pHwData->time_count); // 20060928 add
314         pHwData->LEDTimer.expires = jiffies + msecs_to_jiffies(TimeInterval);
315         add_timer(&pHwData->LEDTimer);
316 }
317
318 u8 hal_init_hardware(struct ieee80211_hw *hw)
319 {
320         struct wbsoft_priv *priv = hw->priv;
321         phw_data_t pHwData = &priv->sHwData;
322         u16 SoftwareSet;
323
324         // Initial the variable
325         pHwData->MaxReceiveLifeTime = DEFAULT_MSDU_LIFE_TIME; // Setting Rx maximum MSDU life time
326         pHwData->FragmentThreshold = DEFAULT_FRAGMENT_THRESHOLD; // Setting default fragment threshold
327
328         pHwData->InitialResource = 1;
329         if( Wb35Reg_initial(pHwData)) {
330                 pHwData->InitialResource = 2;
331                 if (Wb35Tx_initial(pHwData)) {
332                         pHwData->InitialResource = 3;
333                         if (Wb35Rx_initial(pHwData)) {
334                                 pHwData->InitialResource = 4;
335                                 init_timer(&pHwData->LEDTimer);
336                                 pHwData->LEDTimer.function = hal_led_control;
337                                 pHwData->LEDTimer.data = (unsigned long) priv;
338                                 pHwData->LEDTimer.expires = jiffies + msecs_to_jiffies(1000);
339                                 add_timer(&pHwData->LEDTimer);
340
341                                 //
342                                 // For restrict to vendor's hardware
343                                 //
344                                 SoftwareSet = hal_software_set( pHwData );
345
346                                 #ifdef Vendor2
347                                 // Try to make sure the EEPROM contain
348                                 SoftwareSet >>= 8;
349                                 if( SoftwareSet != 0x82 )
350                                         return false;
351                                 #endif
352
353                                 Wb35Rx_start(hw);
354                                 Wb35Tx_EP2VM_start(priv);
355
356                                 return true;
357                         }
358                 }
359         }
360
361         pHwData->SurpriseRemove = 1;
362         return false;
363 }
364
365
366 void hal_halt(phw_data_t pHwData, void *ppa_data)
367 {
368         switch( pHwData->InitialResource )
369         {
370                 case 4:
371                 case 3: del_timer_sync(&pHwData->LEDTimer);
372                         msleep(100); // Wait for Timer DPC exit 940623.2
373                         Wb35Rx_destroy( pHwData ); // Release the Rx
374                 case 2: Wb35Tx_destroy( pHwData ); // Release the Tx
375                 case 1: Wb35Reg_destroy( pHwData ); // Release the Wb35 Regisster resources
376         }
377 }
378
379 //---------------------------------------------------------------------------------------------------
380 void hal_set_rates(phw_data_t pHwData, u8 *pbss_rates,
381                    u8 length, unsigned char basic_rate_set)
382 {
383         struct wb35_reg *reg = &pHwData->reg;
384         u32             tmp, tmp1;
385         u8              Rate[12]={ 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 };
386         u8              SupportedRate[16];
387         u8              i, j, k, Count1, Count2, Byte;
388
389         if( pHwData->SurpriseRemove ) return;
390
391         if (basic_rate_set) {
392                 reg->M28_MacControl &= ~0x000fff00;
393                 tmp1 = 0x00000100;
394         } else {
395                 reg->M28_MacControl &= ~0xfff00000;
396                 tmp1 = 0x00100000;
397         }
398
399         tmp = 0;
400         for (i=0; i<length; i++) {
401                 Byte = pbss_rates[i] & 0x7f;
402                 for (j=0; j<12; j++) {
403                         if( Byte == Rate[j] )
404                                 break;
405                 }
406
407                 if (j < 12)
408                         tmp |= (tmp1<<j);
409         }
410
411         reg->M28_MacControl |= tmp;
412         Wb35Reg_Write( pHwData, 0x0828, reg->M28_MacControl );
413
414         // 930206.2.c M78 setting
415         j = k = Count1 = Count2 = 0;
416         memset( SupportedRate, 0, 16 );
417         tmp = 0x00100000;
418         tmp1 = 0x00000100;
419         for (i=0; i<12; i++) { // Get the supported rate
420                 if (tmp & reg->M28_MacControl) {
421                         SupportedRate[j] = Rate[i];
422
423                         if (tmp1 & reg->M28_MacControl)
424                                 SupportedRate[j] |= 0x80;
425
426                         if (k)
427                                 Count2++;
428                         else
429                                 Count1++;
430
431                         j++;
432                 }
433
434                 if (i==4 && k==0) {
435                         if( !(reg->M28_MacControl & 0x000ff000) ) // if basic rate in 11g domain)
436                         {
437                                 k = 1;
438                                 j = 8;
439                         }
440                 }
441
442                 tmp <<= 1;
443                 tmp1 <<= 1;
444         }
445
446         // Fill data into support rate until buffer full
447         //---20060926 add by anson's endian
448         for (i=0; i<4; i++)
449                 *(u32 *)(SupportedRate+(i<<2)) = cpu_to_le32( *(u32 *)(SupportedRate+(i<<2)) );
450         //--- end 20060926 add by anson's endian
451         Wb35Reg_BurstWrite( pHwData,0x087c, (u32 *)SupportedRate, 4, AUTO_INCREMENT );
452         reg->M7C_MacControl = ((u32 *)SupportedRate)[0];
453         reg->M80_MacControl = ((u32 *)SupportedRate)[1];
454         reg->M84_MacControl = ((u32 *)SupportedRate)[2];
455         reg->M88_MacControl = ((u32 *)SupportedRate)[3];
456
457         // Fill length
458         tmp = Count1<<28 | Count2<<24;
459         reg->M78_ERPInformation &= ~0xff000000;
460         reg->M78_ERPInformation |= tmp;
461         Wb35Reg_Write( pHwData, 0x0878, reg->M78_ERPInformation );
462 }
463
464
465 //---------------------------------------------------------------------------------------------------
466 void hal_set_beacon_period(  phw_data_t pHwData,  u16 beacon_period )
467 {
468         u32     tmp;
469
470         if( pHwData->SurpriseRemove ) return;
471
472         pHwData->BeaconPeriod = beacon_period;
473         tmp = pHwData->BeaconPeriod << 16;
474         tmp |= pHwData->ProbeDelay;
475         Wb35Reg_Write( pHwData, 0x0848, tmp );
476 }
477
478
479 void hal_set_current_channel_ex(  phw_data_t pHwData,  ChanInfo channel )
480 {
481         struct wb35_reg *reg = &pHwData->reg;
482
483         if( pHwData->SurpriseRemove )
484                 return;
485
486         printk("Going to channel: %d/%d\n", channel.band, channel.ChanNo);
487
488         RFSynthesizer_SwitchingChannel( pHwData, channel );// Switch channel
489         pHwData->Channel = channel.ChanNo;
490         pHwData->band = channel.band;
491         #ifdef _PE_STATE_DUMP_
492         WBDEBUG(("Set channel is %d, band =%d\n", pHwData->Channel, pHwData->band));
493         #endif
494         reg->M28_MacControl &= ~0xff; // Clean channel information field
495         reg->M28_MacControl |= channel.ChanNo;
496         Wb35Reg_WriteWithCallbackValue( pHwData, 0x0828, reg->M28_MacControl,
497                                         (s8 *)&channel, sizeof(ChanInfo));
498 }
499 //---------------------------------------------------------------------------------------------------
500 void hal_set_current_channel(  phw_data_t pHwData,  ChanInfo channel )
501 {
502         hal_set_current_channel_ex( pHwData, channel );
503 }
504 //---------------------------------------------------------------------------------------------------
505 void hal_get_current_channel(  phw_data_t pHwData,  ChanInfo *channel )
506 {
507         channel->ChanNo = pHwData->Channel;
508         channel->band = pHwData->band;
509 }
510 //---------------------------------------------------------------------------------------------------
511 void hal_set_accept_broadcast(  phw_data_t pHwData,  u8 enable )
512 {
513         struct wb35_reg *reg = &pHwData->reg;
514
515         if( pHwData->SurpriseRemove ) return;
516
517         reg->M00_MacControl &= ~0x02000000;//The HW value
518
519         if (enable)
520                 reg->M00_MacControl |= 0x02000000;//The HW value
521
522         Wb35Reg_Write( pHwData, 0x0800, reg->M00_MacControl );
523 }
524
525 //for wep key error detection, we need to accept broadcast packets to be received temporary.
526 void hal_set_accept_promiscuous( phw_data_t pHwData,  u8 enable)
527 {
528         struct wb35_reg *reg = &pHwData->reg;
529
530         if (pHwData->SurpriseRemove) return;
531         if (enable) {
532                 reg->M00_MacControl |= 0x00400000;
533                 Wb35Reg_Write( pHwData, 0x0800, reg->M00_MacControl );
534         } else {
535                 reg->M00_MacControl&=~0x00400000;
536                 Wb35Reg_Write( pHwData, 0x0800, reg->M00_MacControl );
537         }
538 }
539
540 void hal_set_accept_multicast(  phw_data_t pHwData,  u8 enable )
541 {
542         struct wb35_reg *reg = &pHwData->reg;
543
544         if( pHwData->SurpriseRemove ) return;
545
546         reg->M00_MacControl &= ~0x01000000;//The HW value
547         if (enable)  reg->M00_MacControl |= 0x01000000;//The HW value
548         Wb35Reg_Write( pHwData, 0x0800, reg->M00_MacControl );
549 }
550
551 void hal_set_accept_beacon(  phw_data_t pHwData,  u8 enable )
552 {
553         struct wb35_reg *reg = &pHwData->reg;
554
555         if( pHwData->SurpriseRemove ) return;
556
557         // 20040108 debug
558         if( !enable )//Due to SME and MLME are not suitable for 35
559                 return;
560
561         reg->M00_MacControl &= ~0x04000000;//The HW value
562         if( enable )
563                 reg->M00_MacControl |= 0x04000000;//The HW value
564
565         Wb35Reg_Write( pHwData, 0x0800, reg->M00_MacControl );
566 }
567 //---------------------------------------------------------------------------------------------------
568 void hal_set_multicast_address( phw_data_t pHwData, u8 *address, u8 number )
569 {
570         struct wb35_reg *reg = &pHwData->reg;
571         u8              Byte, Bit;
572
573         if( pHwData->SurpriseRemove ) return;
574
575         //Erases and refills the card multicast registers. Used when an address
576         //    has been deleted and all bits must be recomputed.
577         reg->M04_MulticastAddress1 = 0;
578         reg->M08_MulticastAddress2 = 0;
579
580         while( number )
581         {
582                 number--;
583                 CardGetMulticastBit( (address+(number*ETH_LENGTH_OF_ADDRESS)), &Byte, &Bit);
584                 reg->Multicast[Byte] |= Bit;
585         }
586
587         // Updating register
588         Wb35Reg_BurstWrite( pHwData, 0x0804, (u32 *)reg->Multicast, 2, AUTO_INCREMENT );
589 }
590 //---------------------------------------------------------------------------------------------------
591 u8 hal_get_accept_beacon(  phw_data_t pHwData )
592 {
593         struct wb35_reg *reg = &pHwData->reg;
594
595         if( pHwData->SurpriseRemove ) return 0;
596
597         if( reg->M00_MacControl & 0x04000000 )
598                 return 1;
599         else
600                 return 0;
601 }
602
603 unsigned char hal_reset_hardware( phw_data_t pHwData, void* ppa )
604 {
605         // Not implement yet
606         return true;
607 }
608
609 void hal_stop(  phw_data_t pHwData )
610 {
611         struct wb35_reg *reg = &pHwData->reg;
612
613         pHwData->Wb35Rx.rx_halt = 1;
614         Wb35Rx_stop( pHwData );
615
616         pHwData->Wb35Tx.tx_halt = 1;
617         Wb35Tx_stop( pHwData );
618
619         reg->D00_DmaControl &= ~0xc0000000;//Tx Off, Rx Off
620         Wb35Reg_Write( pHwData, 0x0400, reg->D00_DmaControl );
621 }
622
623 unsigned char hal_idle(phw_data_t pHwData)
624 {
625         struct wb35_reg *reg = &pHwData->reg;
626         PWBUSB  pWbUsb = &pHwData->WbUsb;
627
628         if( !pHwData->SurpriseRemove && ( pWbUsb->DetectCount || reg->EP0vm_state!=VM_STOP ) )
629                 return false;
630
631         return true;
632 }
633 //---------------------------------------------------------------------------------------------------
634 void hal_set_cwmin(  phw_data_t pHwData,  u8    cwin_min )
635 {
636         struct wb35_reg *reg = &pHwData->reg;
637
638         if( pHwData->SurpriseRemove ) return;
639
640         pHwData->cwmin = cwin_min;
641         reg->M2C_MacControl &= ~0x7c00; //bit 10 ~ 14
642         reg->M2C_MacControl |= (pHwData->cwmin<<10);
643         Wb35Reg_Write( pHwData, 0x082c, reg->M2C_MacControl );
644 }
645
646 s32 hal_get_rssi(  phw_data_t pHwData,  u32 *HalRssiArry,  u8 Count )
647 {
648         struct wb35_reg *reg = &pHwData->reg;
649         R01_DESCRIPTOR  r01;
650         s32 ltmp = 0, tmp;
651         u8      i;
652
653         if( pHwData->SurpriseRemove ) return -200;
654         if( Count > MAX_ACC_RSSI_COUNT ) // Because the TS may use this funtion
655                 Count = MAX_ACC_RSSI_COUNT;
656
657         // RSSI = C1 + C2 * (agc_state[7:0] + offset_map(lna_state[1:0]))
658         // C1 = -195, C2 = 0.66 = 85/128
659         for (i=0; i<Count; i++)
660         {
661                 r01.value = HalRssiArry[i];
662                 tmp = ((( r01.R01_AGC_state + reg->LNAValue[r01.R01_LNA_state]) * 85 ) >>7 ) - 195;
663                 ltmp += tmp;
664         }
665         ltmp /= Count;
666         if( pHwData->phy_type == RF_AIROHA_2230 ) ltmp -= 5; // 10;
667         if( pHwData->phy_type == RF_AIROHA_2230S ) ltmp -= 5; // 10; 20060420 Add this
668
669         //if( ltmp < -200 ) ltmp = -200;
670         if( ltmp < -110 ) ltmp = -110;// 1.0.24.0 For NJRC
671
672         return ltmp;
673 }
674 //----------------------------------------------------------------------------------------------------
675 s32 hal_get_rssi_bss(struct wbsoft_priv *adapter,  u16 idx,  u8 Count)
676 {
677         phw_data_t pHwData = &adapter->sHwData;
678         struct wb35_reg *reg = &pHwData->reg;
679         R01_DESCRIPTOR  r01;
680         s32 ltmp = 0, tmp;
681         u8      i, j;
682 //      u32 *HalRssiArry = psBSS(idx)->HalRssi;
683
684         if( pHwData->SurpriseRemove ) return -200;
685         if( Count > MAX_ACC_RSSI_COUNT ) // Because the TS may use this funtion
686                 Count = MAX_ACC_RSSI_COUNT;
687
688         // RSSI = C1 + C2 * (agc_state[7:0] + offset_map(lna_state[1:0]))
689         // C1 = -195, C2 = 0.66 = 85/128
690 #if 0
691         for (i=0; i<Count; i++)
692         {
693                 r01.value = HalRssiArry[i];
694                 tmp = ((( r01.R01_AGC_state + reg->LNAValue[r01.R01_LNA_state]) * 85 ) >>7 ) - 195;
695                 ltmp += tmp;
696         }
697 #else
698         if (psBSS(idx)->HalRssiIndex == 0)
699                 psBSS(idx)->HalRssiIndex = MAX_ACC_RSSI_COUNT;
700         j = (u8)psBSS(idx)->HalRssiIndex-1;
701
702         for (i=0; i<Count; i++)
703         {
704                 r01.value = psBSS(idx)->HalRssi[j];
705                 tmp = ((( r01.R01_AGC_state + reg->LNAValue[r01.R01_LNA_state]) * 85 ) >>7 ) - 195;
706                 ltmp += tmp;
707                 if (j == 0)
708                 {
709                         j = MAX_ACC_RSSI_COUNT;
710                 }
711                 j--;
712         }
713 #endif
714         ltmp /= Count;
715         if( pHwData->phy_type == RF_AIROHA_2230 ) ltmp -= 5; // 10;
716         if( pHwData->phy_type == RF_AIROHA_2230S ) ltmp -= 5; // 10; 20060420 Add this
717
718         //if( ltmp < -200 ) ltmp = -200;
719         if( ltmp < -110 ) ltmp = -110;// 1.0.24.0 For NJRC
720
721         return ltmp;
722 }
723
724 //---------------------------------------------------------------------------
725
726 void hal_set_phy_type(  phw_data_t pHwData,  u8 PhyType )
727 {
728         pHwData->phy_type = PhyType;
729 }
730
731 void hal_get_phy_type(  phw_data_t pHwData,  u8 *PhyType )
732 {
733         *PhyType = pHwData->phy_type;
734 }
735
736 void hal_reset_counter(  phw_data_t pHwData )
737 {
738         pHwData->dto_tx_retry_count = 0;
739         pHwData->dto_tx_frag_count = 0;
740         memset( pHwData->tx_retry_count, 0, 8);
741 }
742
743 void hal_set_radio_mode( phw_data_t pHwData,  unsigned char radio_off)
744 {
745         struct wb35_reg *reg = &pHwData->reg;
746
747         if( pHwData->SurpriseRemove ) return;
748
749         if (radio_off)  //disable Baseband receive off
750         {
751                 pHwData->CurrentRadioSw = 1; // off
752                 reg->M24_MacControl &= 0xffffffbf;
753         }
754         else
755         {
756                 pHwData->CurrentRadioSw = 0; // on
757                 reg->M24_MacControl |= 0x00000040;
758         }
759         Wb35Reg_Write( pHwData, 0x0824, reg->M24_MacControl );
760 }
761
762 u8 hal_get_antenna_number(  phw_data_t pHwData )
763 {
764         struct wb35_reg *reg = &pHwData->reg;
765
766         if ((reg->BB2C & BIT(11)) == 0)
767                 return 0;
768         else
769                 return 1;
770 }
771
772 void hal_set_antenna_number(  phw_data_t pHwData, u8 number )
773 {
774
775         struct wb35_reg *reg = &pHwData->reg;
776
777         if (number == 1) {
778                 reg->BB2C |= BIT(11);
779         } else {
780                 reg->BB2C &= ~BIT(11);
781         }
782         Wb35Reg_Write( pHwData, 0x102c, reg->BB2C );
783 #ifdef _PE_STATE_DUMP_
784         WBDEBUG(("Current antenna number : %d\n", number));
785 #endif
786 }
787
788 //----------------------------------------------------------------------------------------------------
789 //0 : radio on; 1: radio off
790 u8 hal_get_hw_radio_off(  phw_data_t pHwData )
791 {
792         struct wb35_reg *reg = &pHwData->reg;
793
794         if( pHwData->SurpriseRemove ) return 1;
795
796         //read the bit16 of register U1B0
797         Wb35Reg_Read( pHwData, 0x3b0, &reg->U1B0 );
798         if ((reg->U1B0 & 0x00010000)) {
799                 pHwData->CurrentRadioHw = 1;
800                 return 1;
801         } else {
802                 pHwData->CurrentRadioHw = 0;
803                 return 0;
804         }
805 }
806
807 unsigned char hal_get_dxx_reg(  phw_data_t pHwData,  u16 number,  u32 * pValue )
808 {
809         if( number < 0x1000 )
810                 number += 0x1000;
811         return Wb35Reg_ReadSync( pHwData, number, pValue );
812 }
813
814 unsigned char hal_set_dxx_reg(  phw_data_t pHwData,  u16 number,  u32 value )
815 {
816         unsigned char   ret;
817
818         if( number < 0x1000 )
819                 number += 0x1000;
820         ret = Wb35Reg_WriteSync( pHwData, number, value );
821         return ret;
822 }
823
824 void hal_scan_status_indicate(phw_data_t pHwData, unsigned char IsOnProgress)
825 {
826         if( pHwData->SurpriseRemove ) return;
827         pHwData->LED_Scanning = IsOnProgress ? 1 : 0;
828 }
829
830 void hal_system_power_change(phw_data_t pHwData, u32 PowerState)
831 {
832         if( PowerState != 0 )
833         {
834                 pHwData->SurpriseRemove = 1;
835                 if( pHwData->WbUsb.IsUsb20 )
836                         hal_stop( pHwData );
837         }
838         else
839         {
840                 if( !pHwData->WbUsb.IsUsb20 )
841                         hal_stop( pHwData );
842         }
843 }
844
845 void hal_surprise_remove(struct wbsoft_priv *adapter)
846 {
847         phw_data_t pHwData = &adapter->sHwData;
848
849         if (atomic_inc_return( &pHwData->SurpriseRemoveCount ) == 1) {
850                 #ifdef _PE_STATE_DUMP_
851                 WBDEBUG(("Calling hal_surprise_remove\n"));
852                 #endif
853                 WBLINUX_stop( adapter );
854         }
855 }
856
857 void hal_rate_change(struct wbsoft_priv *adapter) // Notify the HAL rate is changing 20060613.1
858 {
859         phw_data_t pHwData = &adapter->sHwData;
860         u8              rate = CURRENT_TX_RATE;
861
862         BBProcessor_RateChanging( pHwData, rate );
863 }
864
865 void hal_set_rf_power(phw_data_t pHwData, u8 PowerIndex)
866 {
867         RFSynthesizer_SetPowerIndex( pHwData, PowerIndex );
868 }
869
870 unsigned char hal_set_LED(phw_data_t pHwData, u32 Mode) // 20061108 for WPS led control
871 {
872         pHwData->LED_Blinking = 0;
873         pHwData->LED_control = Mode;
874         pHwData->LEDTimer.expires = jiffies + msecs_to_jiffies(10);
875         add_timer(&pHwData->LEDTimer);
876         return true;
877 }
878