Merge branch 'next' into for-linus
[linux-2.6] / drivers / staging / winbond / wbhal.c
1 #include "sysdef.h"
2 #include "wbhal_f.h"
3 #include "wblinux_f.h"
4
5 void hal_set_ethernet_address( struct hw_data * pHwData, u8 *current_address )
6 {
7         u32 ltmp[2];
8
9         if( pHwData->SurpriseRemove ) return;
10
11         memcpy( pHwData->CurrentMacAddress, current_address, ETH_ALEN );
12
13         ltmp[0]= cpu_to_le32( *(u32 *)pHwData->CurrentMacAddress );
14         ltmp[1]= cpu_to_le32( *(u32 *)(pHwData->CurrentMacAddress + 4) ) & 0xffff;
15
16         Wb35Reg_BurstWrite( pHwData, 0x03e8, ltmp, 2, AUTO_INCREMENT );
17 }
18
19 void hal_get_permanent_address( struct hw_data * pHwData, u8 *pethernet_address )
20 {
21         if( pHwData->SurpriseRemove ) return;
22
23         memcpy( pethernet_address, pHwData->PermanentMacAddress, 6 );
24 }
25
26 static void hal_led_control(unsigned long data)
27 {
28         struct wbsoft_priv *adapter = (struct wbsoft_priv *) data;
29         struct hw_data * pHwData = &adapter->sHwData;
30         struct wb35_reg *reg = &pHwData->reg;
31         u32     LEDSet = (pHwData->SoftwareSet & HAL_LED_SET_MASK) >> HAL_LED_SET_SHIFT;
32         u8      LEDgray[20] = { 0,3,4,6,8,10,11,12,13,14,15,14,13,12,11,10,8,6,4,2 };
33         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 };
34         u32     TimeInterval = 500, ltmp, ltmp2;
35         ltmp=0;
36
37         if( pHwData->SurpriseRemove ) return;
38
39         if( pHwData->LED_control ) {
40                 ltmp2 = pHwData->LED_control & 0xff;
41                 if( ltmp2 == 5 ) // 5 is WPS mode
42                 {
43                         TimeInterval = 100;
44                         ltmp2 = (pHwData->LED_control>>8) & 0xff;
45                         switch( ltmp2 )
46                         {
47                                 case 1: // [0.2 On][0.1 Off]...
48                                         pHwData->LED_Blinking %= 3;
49                                         ltmp = 0x1010; // Led 1 & 0 Green and Red
50                                         if( pHwData->LED_Blinking == 2 ) // Turn off
51                                                 ltmp = 0;
52                                         break;
53                                 case 2: // [0.1 On][0.1 Off]...
54                                         pHwData->LED_Blinking %= 2;
55                                         ltmp = 0x0010; // Led 0 red color
56                                         if( pHwData->LED_Blinking ) // Turn off
57                                                 ltmp = 0;
58                                         break;
59                                 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]...
60                                         pHwData->LED_Blinking %= 15;
61                                         ltmp = 0x0010; // Led 0 red color
62                                         if( (pHwData->LED_Blinking >= 9) || (pHwData->LED_Blinking%2) ) // Turn off 0.6 sec
63                                                 ltmp = 0;
64                                         break;
65                                 case 4: // [300 On][ off ]
66                                         ltmp = 0x1000; // Led 1 Green color
67                                         if( pHwData->LED_Blinking >= 3000 )
68                                                 ltmp = 0; // led maybe on after 300sec * 32bit counter overlap.
69                                         break;
70                         }
71                         pHwData->LED_Blinking++;
72
73                         reg->U1BC_LEDConfigure = ltmp;
74                         if( LEDSet != 7 ) // Only 111 mode has 2 LEDs on PCB.
75                         {
76                                 reg->U1BC_LEDConfigure |= (ltmp &0xff)<<8; // Copy LED result to each LED control register
77                                 reg->U1BC_LEDConfigure |= (ltmp &0xff00)>>8;
78                         }
79                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure );
80                 }
81         }
82         else if( pHwData->CurrentRadioSw || pHwData->CurrentRadioHw ) // If radio off
83         {
84                 if( reg->U1BC_LEDConfigure & 0x1010 )
85                 {
86                         reg->U1BC_LEDConfigure &= ~0x1010;
87                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure );
88                 }
89         }
90         else
91         {
92                 switch( LEDSet )
93                 {
94                         case 4: // [100] Only 1 Led be placed on PCB and use pin 21 of IC. Use LED_0 for showing
95                                 if( !pHwData->LED_LinkOn ) // Blink only if not Link On
96                                 {
97                                         // Blinking if scanning is on progress
98                                         if( pHwData->LED_Scanning )
99                                         {
100                                                 if( pHwData->LED_Blinking == 0 )
101                                                 {
102                                                         reg->U1BC_LEDConfigure |= 0x10;
103                                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_0 On
104                                                         pHwData->LED_Blinking = 1;
105                                                         TimeInterval = 300;
106                                                 }
107                                                 else
108                                                 {
109                                                         reg->U1BC_LEDConfigure &= ~0x10;
110                                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_0 Off
111                                                         pHwData->LED_Blinking = 0;
112                                                         TimeInterval = 300;
113                                                 }
114                                         }
115                                         else
116                                         {
117                                                 //Turn Off LED_0
118                                                 if( reg->U1BC_LEDConfigure & 0x10 )
119                                                 {
120                                                         reg->U1BC_LEDConfigure &= ~0x10;
121                                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_0 Off
122                                                 }
123                                         }
124                                 }
125                                 else
126                                 {
127                                         // Turn On LED_0
128                                         if( (reg->U1BC_LEDConfigure & 0x10) == 0 )
129                                         {
130                                                 reg->U1BC_LEDConfigure |= 0x10;
131                                                 Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_0 Off
132                                         }
133                                 }
134                                 break;
135
136                         case 6: // [110] Only 1 Led be placed on PCB and use pin 21 of IC. Use LED_0 for showing
137                                 if( !pHwData->LED_LinkOn ) // Blink only if not Link On
138                                 {
139                                         // Blinking if scanning is on progress
140                                         if( pHwData->LED_Scanning )
141                                         {
142                                                 if( pHwData->LED_Blinking == 0 )
143                                                 {
144                                                         reg->U1BC_LEDConfigure &= ~0xf;
145                                                         reg->U1BC_LEDConfigure |= 0x10;
146                                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_0 On
147                                                         pHwData->LED_Blinking = 1;
148                                                         TimeInterval = 300;
149                                                 }
150                                                 else
151                                                 {
152                                                         reg->U1BC_LEDConfigure &= ~0x1f;
153                                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_0 Off
154                                                         pHwData->LED_Blinking = 0;
155                                                         TimeInterval = 300;
156                                                 }
157                                         }
158                                         else
159                                         {
160                                                 // 20060901 Gray blinking if in disconnect state and not scanning
161                                                 ltmp = reg->U1BC_LEDConfigure;
162                                                 reg->U1BC_LEDConfigure &= ~0x1f;
163                                                 if( LEDgray2[(pHwData->LED_Blinking%30)] )
164                                                 {
165                                                         reg->U1BC_LEDConfigure |= 0x10;
166                                                         reg->U1BC_LEDConfigure |= LEDgray2[ (pHwData->LED_Blinking%30) ];
167                                                 }
168                                                 pHwData->LED_Blinking++;
169                                                 if( reg->U1BC_LEDConfigure != ltmp )
170                                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_0 Off
171                                                 TimeInterval = 100;
172                                         }
173                                 }
174                                 else
175                                 {
176                                         // Turn On LED_0
177                                         if( (reg->U1BC_LEDConfigure & 0x10) == 0 )
178                                         {
179                                                 reg->U1BC_LEDConfigure |= 0x10;
180                                                 Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_0 Off
181                                         }
182                                 }
183                                 break;
184
185                         case 5: // [101] Only 1 Led be placed on PCB and use LED_1 for showing
186                                 if( !pHwData->LED_LinkOn ) // Blink only if not Link On
187                                 {
188                                         // Blinking if scanning is on progress
189                                         if( pHwData->LED_Scanning )
190                                         {
191                                                 if( pHwData->LED_Blinking == 0 )
192                                                 {
193                                                         reg->U1BC_LEDConfigure |= 0x1000;
194                                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_1 On
195                                                         pHwData->LED_Blinking = 1;
196                                                         TimeInterval = 300;
197                                                 }
198                                                 else
199                                                 {
200                                                         reg->U1BC_LEDConfigure &= ~0x1000;
201                                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_1 Off
202                                                         pHwData->LED_Blinking = 0;
203                                                         TimeInterval = 300;
204                                                 }
205                                         }
206                                         else
207                                         {
208                                                 //Turn Off LED_1
209                                                 if( reg->U1BC_LEDConfigure & 0x1000 )
210                                                 {
211                                                         reg->U1BC_LEDConfigure &= ~0x1000;
212                                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_1 Off
213                                                 }
214                                         }
215                                 }
216                                 else
217                                 {
218                                         // Is transmitting/receiving ??
219                                         if( (adapter->RxByteCount != pHwData->RxByteCountLast ) ||
220                                                 (adapter->TxByteCount != pHwData->TxByteCountLast ) )
221                                         {
222                                                 if( (reg->U1BC_LEDConfigure & 0x3000) != 0x3000 )
223                                                 {
224                                                         reg->U1BC_LEDConfigure |= 0x3000;
225                                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_1 On
226                                                 }
227
228                                                 // Update variable
229                                                 pHwData->RxByteCountLast = adapter->RxByteCount;
230                                                 pHwData->TxByteCountLast = adapter->TxByteCount;
231                                                 TimeInterval = 200;
232                                         }
233                                         else
234                                         {
235                                                 // Turn On LED_1 and blinking if transmitting/receiving
236                                                  if( (reg->U1BC_LEDConfigure & 0x3000) != 0x1000 )
237                                                  {
238                                                          reg->U1BC_LEDConfigure &= ~0x3000;
239                                                          reg->U1BC_LEDConfigure |= 0x1000;
240                                                          Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure ); // LED_1 On
241                                                  }
242                                         }
243                                 }
244                                 break;
245
246                         default: // Default setting. 2 LED be placed on PCB. LED_0: Link On LED_1 Active
247                                 if( (reg->U1BC_LEDConfigure & 0x3000) != 0x3000 )
248                                 {
249                                         reg->U1BC_LEDConfigure |= 0x3000;// LED_1 is always on and event enable
250                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure );
251                                 }
252
253                                 if( pHwData->LED_Blinking )
254                                 {
255                                         // Gray blinking
256                                         reg->U1BC_LEDConfigure &= ~0x0f;
257                                         reg->U1BC_LEDConfigure |= 0x10;
258                                         reg->U1BC_LEDConfigure |= LEDgray[ (pHwData->LED_Blinking-1)%20 ];
259                                         Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure );
260
261                                         pHwData->LED_Blinking += 2;
262                                         if( pHwData->LED_Blinking < 40 )
263                                                 TimeInterval = 100;
264                                         else
265                                         {
266                                                 pHwData->LED_Blinking = 0; // Stop blinking
267                                                 reg->U1BC_LEDConfigure &= ~0x0f;
268                                                 Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure );
269                                         }
270                                         break;
271                                 }
272
273                                 if( pHwData->LED_LinkOn )
274                                 {
275                                         if( !(reg->U1BC_LEDConfigure & 0x10) ) // Check the LED_0
276                                         {
277                                                 //Try to turn ON LED_0 after gray blinking
278                                                 reg->U1BC_LEDConfigure |= 0x10;
279                                                 pHwData->LED_Blinking = 1; //Start blinking
280                                                 TimeInterval = 50;
281                                         }
282                                 }
283                                 else
284                                 {
285                                         if( reg->U1BC_LEDConfigure & 0x10 ) // Check the LED_0
286                                         {
287                                                 reg->U1BC_LEDConfigure &= ~0x10;
288                                                 Wb35Reg_Write( pHwData, 0x03bc, reg->U1BC_LEDConfigure );
289                                         }
290                                 }
291                                 break;
292                 }
293
294                 //20060828.1 Active send null packet to avoid AP disconnect
295                 if( pHwData->LED_LinkOn )
296                 {
297                         pHwData->NullPacketCount += TimeInterval;
298                         if( pHwData->NullPacketCount >= DEFAULT_NULL_PACKET_COUNT )
299                         {
300                                 pHwData->NullPacketCount = 0;
301                         }
302                 }
303         }
304
305         pHwData->time_count += TimeInterval;
306         Wb35Tx_CurrentTime(adapter, pHwData->time_count); // 20060928 add
307         pHwData->LEDTimer.expires = jiffies + msecs_to_jiffies(TimeInterval);
308         add_timer(&pHwData->LEDTimer);
309 }
310
311 u8 hal_init_hardware(struct ieee80211_hw *hw)
312 {
313         struct wbsoft_priv *priv = hw->priv;
314         struct hw_data * pHwData = &priv->sHwData;
315         u16 SoftwareSet;
316
317         // Initial the variable
318         pHwData->MaxReceiveLifeTime = DEFAULT_MSDU_LIFE_TIME; // Setting Rx maximum MSDU life time
319         pHwData->FragmentThreshold = DEFAULT_FRAGMENT_THRESHOLD; // Setting default fragment threshold
320
321         pHwData->InitialResource = 1;
322         if( Wb35Reg_initial(pHwData)) {
323                 pHwData->InitialResource = 2;
324                 if (Wb35Tx_initial(pHwData)) {
325                         pHwData->InitialResource = 3;
326                         if (Wb35Rx_initial(pHwData)) {
327                                 pHwData->InitialResource = 4;
328                                 init_timer(&pHwData->LEDTimer);
329                                 pHwData->LEDTimer.function = hal_led_control;
330                                 pHwData->LEDTimer.data = (unsigned long) priv;
331                                 pHwData->LEDTimer.expires = jiffies + msecs_to_jiffies(1000);
332                                 add_timer(&pHwData->LEDTimer);
333
334                                 //
335                                 // For restrict to vendor's hardware
336                                 //
337                                 SoftwareSet = hal_software_set( pHwData );
338
339                                 #ifdef Vendor2
340                                 // Try to make sure the EEPROM contain
341                                 SoftwareSet >>= 8;
342                                 if( SoftwareSet != 0x82 )
343                                         return false;
344                                 #endif
345
346                                 Wb35Rx_start(hw);
347                                 Wb35Tx_EP2VM_start(priv);
348
349                                 return true;
350                         }
351                 }
352         }
353
354         pHwData->SurpriseRemove = 1;
355         return false;
356 }
357
358
359 void hal_halt(struct hw_data * pHwData, void *ppa_data)
360 {
361         switch( pHwData->InitialResource )
362         {
363                 case 4:
364                 case 3: del_timer_sync(&pHwData->LEDTimer);
365                         msleep(100); // Wait for Timer DPC exit 940623.2
366                         Wb35Rx_destroy( pHwData ); // Release the Rx
367                 case 2: Wb35Tx_destroy( pHwData ); // Release the Tx
368                 case 1: Wb35Reg_destroy( pHwData ); // Release the Wb35 Regisster resources
369         }
370 }
371
372 //---------------------------------------------------------------------------------------------------
373 void hal_set_beacon_period(  struct hw_data * pHwData,  u16 beacon_period )
374 {
375         u32     tmp;
376
377         if( pHwData->SurpriseRemove ) return;
378
379         pHwData->BeaconPeriod = beacon_period;
380         tmp = pHwData->BeaconPeriod << 16;
381         tmp |= pHwData->ProbeDelay;
382         Wb35Reg_Write( pHwData, 0x0848, tmp );
383 }
384
385
386 static void hal_set_current_channel_ex(  struct hw_data * pHwData,  ChanInfo channel )
387 {
388         struct wb35_reg *reg = &pHwData->reg;
389
390         if( pHwData->SurpriseRemove )
391                 return;
392
393         printk("Going to channel: %d/%d\n", channel.band, channel.ChanNo);
394
395         RFSynthesizer_SwitchingChannel( pHwData, channel );// Switch channel
396         pHwData->Channel = channel.ChanNo;
397         pHwData->band = channel.band;
398         #ifdef _PE_STATE_DUMP_
399         printk("Set channel is %d, band =%d\n", pHwData->Channel, pHwData->band);
400         #endif
401         reg->M28_MacControl &= ~0xff; // Clean channel information field
402         reg->M28_MacControl |= channel.ChanNo;
403         Wb35Reg_WriteWithCallbackValue( pHwData, 0x0828, reg->M28_MacControl,
404                                         (s8 *)&channel, sizeof(ChanInfo));
405 }
406 //---------------------------------------------------------------------------------------------------
407 void hal_set_current_channel(  struct hw_data * pHwData,  ChanInfo channel )
408 {
409         hal_set_current_channel_ex( pHwData, channel );
410 }
411 //---------------------------------------------------------------------------------------------------
412 void hal_set_accept_broadcast(  struct hw_data * pHwData,  u8 enable )
413 {
414         struct wb35_reg *reg = &pHwData->reg;
415
416         if( pHwData->SurpriseRemove ) return;
417
418         reg->M00_MacControl &= ~0x02000000;//The HW value
419
420         if (enable)
421                 reg->M00_MacControl |= 0x02000000;//The HW value
422
423         Wb35Reg_Write( pHwData, 0x0800, reg->M00_MacControl );
424 }
425
426 //for wep key error detection, we need to accept broadcast packets to be received temporary.
427 void hal_set_accept_promiscuous( struct hw_data * pHwData,  u8 enable)
428 {
429         struct wb35_reg *reg = &pHwData->reg;
430
431         if (pHwData->SurpriseRemove) return;
432         if (enable) {
433                 reg->M00_MacControl |= 0x00400000;
434                 Wb35Reg_Write( pHwData, 0x0800, reg->M00_MacControl );
435         } else {
436                 reg->M00_MacControl&=~0x00400000;
437                 Wb35Reg_Write( pHwData, 0x0800, reg->M00_MacControl );
438         }
439 }
440
441 void hal_set_accept_multicast(  struct hw_data * pHwData,  u8 enable )
442 {
443         struct wb35_reg *reg = &pHwData->reg;
444
445         if( pHwData->SurpriseRemove ) return;
446
447         reg->M00_MacControl &= ~0x01000000;//The HW value
448         if (enable)  reg->M00_MacControl |= 0x01000000;//The HW value
449         Wb35Reg_Write( pHwData, 0x0800, reg->M00_MacControl );
450 }
451
452 void hal_set_accept_beacon(  struct hw_data * pHwData,  u8 enable )
453 {
454         struct wb35_reg *reg = &pHwData->reg;
455
456         if( pHwData->SurpriseRemove ) return;
457
458         // 20040108 debug
459         if( !enable )//Due to SME and MLME are not suitable for 35
460                 return;
461
462         reg->M00_MacControl &= ~0x04000000;//The HW value
463         if( enable )
464                 reg->M00_MacControl |= 0x04000000;//The HW value
465
466         Wb35Reg_Write( pHwData, 0x0800, reg->M00_MacControl );
467 }
468 //---------------------------------------------------------------------------------------------------
469
470 void hal_stop(  struct hw_data * pHwData )
471 {
472         struct wb35_reg *reg = &pHwData->reg;
473
474         pHwData->Wb35Rx.rx_halt = 1;
475         Wb35Rx_stop( pHwData );
476
477         pHwData->Wb35Tx.tx_halt = 1;
478         Wb35Tx_stop( pHwData );
479
480         reg->D00_DmaControl &= ~0xc0000000;//Tx Off, Rx Off
481         Wb35Reg_Write( pHwData, 0x0400, reg->D00_DmaControl );
482 }
483
484 unsigned char hal_idle(struct hw_data * pHwData)
485 {
486         struct wb35_reg *reg = &pHwData->reg;
487         struct wb_usb *pWbUsb = &pHwData->WbUsb;
488
489         if( !pHwData->SurpriseRemove && ( pWbUsb->DetectCount || reg->EP0vm_state!=VM_STOP ) )
490                 return false;
491
492         return true;
493 }
494 //---------------------------------------------------------------------------------------------------
495 void hal_set_phy_type(  struct hw_data * pHwData,  u8 PhyType )
496 {
497         pHwData->phy_type = PhyType;
498 }
499
500 void hal_set_radio_mode( struct hw_data * pHwData,  unsigned char radio_off)
501 {
502         struct wb35_reg *reg = &pHwData->reg;
503
504         if( pHwData->SurpriseRemove ) return;
505
506         if (radio_off)  //disable Baseband receive off
507         {
508                 pHwData->CurrentRadioSw = 1; // off
509                 reg->M24_MacControl &= 0xffffffbf;
510         }
511         else
512         {
513                 pHwData->CurrentRadioSw = 0; // on
514                 reg->M24_MacControl |= 0x00000040;
515         }
516         Wb35Reg_Write( pHwData, 0x0824, reg->M24_MacControl );
517 }
518
519 u8 hal_get_antenna_number(  struct hw_data * pHwData )
520 {
521         struct wb35_reg *reg = &pHwData->reg;
522
523         if ((reg->BB2C & BIT(11)) == 0)
524                 return 0;
525         else
526                 return 1;
527 }
528
529 //----------------------------------------------------------------------------------------------------
530 //0 : radio on; 1: radio off
531 u8 hal_get_hw_radio_off(  struct hw_data * pHwData )
532 {
533         struct wb35_reg *reg = &pHwData->reg;
534
535         if( pHwData->SurpriseRemove ) return 1;
536
537         //read the bit16 of register U1B0
538         Wb35Reg_Read( pHwData, 0x3b0, &reg->U1B0 );
539         if ((reg->U1B0 & 0x00010000)) {
540                 pHwData->CurrentRadioHw = 1;
541                 return 1;
542         } else {
543                 pHwData->CurrentRadioHw = 0;
544                 return 0;
545         }
546 }
547
548 unsigned char hal_get_dxx_reg(  struct hw_data * pHwData,  u16 number,  u32 * pValue )
549 {
550         if( number < 0x1000 )
551                 number += 0x1000;
552         return Wb35Reg_ReadSync( pHwData, number, pValue );
553 }
554
555 unsigned char hal_set_dxx_reg(  struct hw_data * pHwData,  u16 number,  u32 value )
556 {
557         unsigned char   ret;
558
559         if( number < 0x1000 )
560                 number += 0x1000;
561         ret = Wb35Reg_WriteSync( pHwData, number, value );
562         return ret;
563 }
564
565 void hal_set_rf_power(struct hw_data * pHwData, u8 PowerIndex)
566 {
567         RFSynthesizer_SetPowerIndex( pHwData, PowerIndex );
568 }