Staging: w35und: remove hw_data_t typedef
[linux-2.6/mini2440.git] / drivers / staging / winbond / mds.c
blob59cdba8592be9da2c758bd8c7bb2661f6cc876db
1 #include "ds_tkip.h"
2 #include "gl_80211.h"
3 #include "mds_f.h"
4 #include "mlmetxrx_f.h"
5 #include "mto.h"
6 #include "sysdef.h"
7 #include "wbhal_f.h"
8 #include "wblinux_f.h"
10 unsigned char
11 Mds_initial(struct wbsoft_priv * adapter)
13 PMDS pMds = &adapter->Mds;
15 pMds->TxPause = false;
16 pMds->TxRTSThreshold = DEFAULT_RTSThreshold;
17 pMds->TxFragmentThreshold = DEFAULT_FRAGMENT_THRESHOLD;
19 return hal_get_tx_buffer( &adapter->sHwData, &pMds->pTxBuffer );
22 void
23 Mds_Destroy(struct wbsoft_priv * adapter)
27 static void Mds_DurationSet(struct wbsoft_priv *adapter, PDESCRIPTOR pDes, u8 *buffer)
29 PT00_DESCRIPTOR pT00;
30 PT01_DESCRIPTOR pT01;
31 u16 Duration, NextBodyLen, OffsetSize;
32 u8 Rate, i;
33 unsigned char CTS_on = false, RTS_on = false;
34 PT00_DESCRIPTOR pNextT00;
35 u16 BodyLen = 0;
36 unsigned char boGroupAddr = false;
38 OffsetSize = pDes->FragmentThreshold + 32 + 3;
39 OffsetSize &= ~0x03;
40 Rate = pDes->TxRate >> 1;
41 if (!Rate)
42 Rate = 1;
44 pT00 = (PT00_DESCRIPTOR)buffer;
45 pT01 = (PT01_DESCRIPTOR)(buffer+4);
46 pNextT00 = (PT00_DESCRIPTOR)(buffer+OffsetSize);
48 if( buffer[ DOT_11_DA_OFFSET+8 ] & 0x1 ) // +8 for USB hdr
49 boGroupAddr = true;
51 //========================================
52 // Set RTS/CTS mechanism
53 //========================================
54 if (!boGroupAddr)
56 //NOTE : If the protection mode is enabled and the MSDU will be fragmented,
57 // the tx rates of MPDUs will all be DSSS rates. So it will not use
58 // CTS-to-self in this case. CTS-To-self will only be used when without
59 // fragmentation. -- 20050112
60 BodyLen = (u16)pT00->T00_frame_length; //include 802.11 header
61 BodyLen += 4; //CRC
63 if( BodyLen >= CURRENT_RTS_THRESHOLD )
64 RTS_on = true; // Using RTS
65 else
67 if( pT01->T01_modulation_type ) // Is using OFDM
69 if( CURRENT_PROTECT_MECHANISM ) // Is using protect
70 CTS_on = true; // Using CTS
75 if( RTS_on || CTS_on )
77 if( pT01->T01_modulation_type) // Is using OFDM
79 //CTS duration
80 // 2 SIFS + DATA transmit time + 1 ACK
81 // ACK Rate : 24 Mega bps
82 // ACK frame length = 14 bytes
83 Duration = 2*DEFAULT_SIFSTIME +
84 2*PREAMBLE_PLUS_SIGNAL_PLUS_SIGNALEXTENSION +
85 ((BodyLen*8 + 22 + Rate*4 - 1)/(Rate*4))*Tsym +
86 ((112 + 22 + 95)/96)*Tsym;
88 else //DSSS
90 //CTS duration
91 // 2 SIFS + DATA transmit time + 1 ACK
92 // Rate : ?? Mega bps
93 // ACK frame length = 14 bytes
94 if( pT01->T01_plcp_header_length ) //long preamble
95 Duration = LONG_PREAMBLE_PLUS_PLCPHEADER_TIME*2;
96 else
97 Duration = SHORT_PREAMBLE_PLUS_PLCPHEADER_TIME*2;
99 Duration += ( ((BodyLen + 14)*8 + Rate-1) / Rate +
100 DEFAULT_SIFSTIME*2 );
103 if( RTS_on )
105 if( pT01->T01_modulation_type ) // Is using OFDM
107 //CTS + 1 SIFS + CTS duration
108 //CTS Rate : 24 Mega bps
109 //CTS frame length = 14 bytes
110 Duration += (DEFAULT_SIFSTIME +
111 PREAMBLE_PLUS_SIGNAL_PLUS_SIGNALEXTENSION +
112 ((112 + 22 + 95)/96)*Tsym);
114 else
116 //CTS + 1 SIFS + CTS duration
117 //CTS Rate : ?? Mega bps
118 //CTS frame length = 14 bytes
119 if( pT01->T01_plcp_header_length ) //long preamble
120 Duration += LONG_PREAMBLE_PLUS_PLCPHEADER_TIME;
121 else
122 Duration += SHORT_PREAMBLE_PLUS_PLCPHEADER_TIME;
124 Duration += ( ((112 + Rate-1) / Rate) + DEFAULT_SIFSTIME );
128 // Set the value into USB descriptor
129 pT01->T01_add_rts = RTS_on ? 1 : 0;
130 pT01->T01_add_cts = CTS_on ? 1 : 0;
131 pT01->T01_rts_cts_duration = Duration;
134 //=====================================
135 // Fill the more fragment descriptor
136 //=====================================
137 if( boGroupAddr )
138 Duration = 0;
139 else
141 for( i=pDes->FragmentCount-1; i>0; i-- )
143 NextBodyLen = (u16)pNextT00->T00_frame_length;
144 NextBodyLen += 4; //CRC
146 if( pT01->T01_modulation_type )
148 //OFDM
149 // data transmit time + 3 SIFS + 2 ACK
150 // Rate : ??Mega bps
151 // ACK frame length = 14 bytes, tx rate = 24M
152 Duration = PREAMBLE_PLUS_SIGNAL_PLUS_SIGNALEXTENSION * 3;
153 Duration += (((NextBodyLen*8 + 22 + Rate*4 - 1)/(Rate*4)) * Tsym +
154 (((2*14)*8 + 22 + 95)/96)*Tsym +
155 DEFAULT_SIFSTIME*3);
157 else
159 //DSSS
160 // data transmit time + 2 ACK + 3 SIFS
161 // Rate : ??Mega bps
162 // ACK frame length = 14 bytes
163 //TODO :
164 if( pT01->T01_plcp_header_length ) //long preamble
165 Duration = LONG_PREAMBLE_PLUS_PLCPHEADER_TIME*3;
166 else
167 Duration = SHORT_PREAMBLE_PLUS_PLCPHEADER_TIME*3;
169 Duration += ( ((NextBodyLen + (2*14))*8 + Rate-1) / Rate +
170 DEFAULT_SIFSTIME*3 );
173 ((u16 *)buffer)[5] = cpu_to_le16(Duration);// 4 USHOR for skip 8B USB, 2USHORT=FC + Duration
175 //----20061009 add by anson's endian
176 pNextT00->value = cpu_to_le32(pNextT00->value);
177 pT01->value = cpu_to_le32( pT01->value );
178 //----end 20061009 add by anson's endian
180 buffer += OffsetSize;
181 pT01 = (PT01_DESCRIPTOR)(buffer+4);
182 if (i != 1) //The last fragment will not have the next fragment
183 pNextT00 = (PT00_DESCRIPTOR)(buffer+OffsetSize);
186 //=====================================
187 // Fill the last fragment descriptor
188 //=====================================
189 if( pT01->T01_modulation_type )
191 //OFDM
192 // 1 SIFS + 1 ACK
193 // Rate : 24 Mega bps
194 // ACK frame length = 14 bytes
195 Duration = PREAMBLE_PLUS_SIGNAL_PLUS_SIGNALEXTENSION;
196 //The Tx rate of ACK use 24M
197 Duration += (((112 + 22 + 95)/96)*Tsym + DEFAULT_SIFSTIME );
199 else
201 // DSSS
202 // 1 ACK + 1 SIFS
203 // Rate : ?? Mega bps
204 // ACK frame length = 14 bytes(112 bits)
205 if( pT01->T01_plcp_header_length ) //long preamble
206 Duration = LONG_PREAMBLE_PLUS_PLCPHEADER_TIME;
207 else
208 Duration = SHORT_PREAMBLE_PLUS_PLCPHEADER_TIME;
210 Duration += ( (112 + Rate-1)/Rate + DEFAULT_SIFSTIME );
214 ((u16 *)buffer)[5] = cpu_to_le16(Duration);// 4 USHOR for skip 8B USB, 2USHORT=FC + Duration
215 pT00->value = cpu_to_le32(pT00->value);
216 pT01->value = cpu_to_le32(pT01->value);
217 //--end 20061009 add
221 // The function return the 4n size of usb pk
222 static u16 Mds_BodyCopy(struct wbsoft_priv *adapter, PDESCRIPTOR pDes, u8 *TargetBuffer)
224 PT00_DESCRIPTOR pT00;
225 PMDS pMds = &adapter->Mds;
226 u8 *buffer;
227 u8 *src_buffer;
228 u8 *pctmp;
229 u16 Size = 0;
230 u16 SizeLeft, CopySize, CopyLeft, stmp;
231 u8 buf_index, FragmentCount = 0;
234 // Copy fragment body
235 buffer = TargetBuffer; // shift 8B usb + 24B 802.11
236 SizeLeft = pDes->buffer_total_size;
237 buf_index = pDes->buffer_start_index;
239 pT00 = (PT00_DESCRIPTOR)buffer;
240 while (SizeLeft) {
241 pT00 = (PT00_DESCRIPTOR)buffer;
242 CopySize = SizeLeft;
243 if (SizeLeft > pDes->FragmentThreshold) {
244 CopySize = pDes->FragmentThreshold;
245 pT00->T00_frame_length = 24 + CopySize;//Set USB length
246 } else
247 pT00->T00_frame_length = 24 + SizeLeft;//Set USB length
249 SizeLeft -= CopySize;
251 // 1 Byte operation
252 pctmp = (u8 *)( buffer + 8 + DOT_11_SEQUENCE_OFFSET );
253 *pctmp &= 0xf0;
254 *pctmp |= FragmentCount;//931130.5.m
255 if( !FragmentCount )
256 pT00->T00_first_mpdu = 1;
258 buffer += 32; // 8B usb + 24B 802.11 header
259 Size += 32;
261 // Copy into buffer
262 stmp = CopySize + 3;
263 stmp &= ~0x03;//4n Alignment
264 Size += stmp;// Current 4n offset of mpdu
266 while (CopySize) {
267 // Copy body
268 src_buffer = pDes->buffer_address[buf_index];
269 CopyLeft = CopySize;
270 if (CopySize >= pDes->buffer_size[buf_index]) {
271 CopyLeft = pDes->buffer_size[buf_index];
273 // Get the next buffer of descriptor
274 buf_index++;
275 buf_index %= MAX_DESCRIPTOR_BUFFER_INDEX;
276 } else {
277 u8 *pctmp = pDes->buffer_address[buf_index];
278 pctmp += CopySize;
279 pDes->buffer_address[buf_index] = pctmp;
280 pDes->buffer_size[buf_index] -= CopySize;
283 memcpy(buffer, src_buffer, CopyLeft);
284 buffer += CopyLeft;
285 CopySize -= CopyLeft;
288 // 931130.5.n
289 if (pMds->MicAdd) {
290 if (!SizeLeft) {
291 pMds->MicWriteAddress[ pMds->MicWriteIndex ] = buffer - pMds->MicAdd;
292 pMds->MicWriteSize[ pMds->MicWriteIndex ] = pMds->MicAdd;
293 pMds->MicAdd = 0;
295 else if( SizeLeft < 8 ) //931130.5.p
297 pMds->MicAdd = SizeLeft;
298 pMds->MicWriteAddress[ pMds->MicWriteIndex ] = buffer - ( 8 - SizeLeft );
299 pMds->MicWriteSize[ pMds->MicWriteIndex ] = 8 - SizeLeft;
300 pMds->MicWriteIndex++;
304 // Does it need to generate the new header for next mpdu?
305 if (SizeLeft) {
306 buffer = TargetBuffer + Size; // Get the next 4n start address
307 memcpy( buffer, TargetBuffer, 32 );//Copy 8B USB +24B 802.11
308 pT00 = (PT00_DESCRIPTOR)buffer;
309 pT00->T00_first_mpdu = 0;
312 FragmentCount++;
315 pT00->T00_last_mpdu = 1;
316 pT00->T00_IsLastMpdu = 1;
317 buffer = (u8 *)pT00 + 8; // +8 for USB hdr
318 buffer[1] &= ~0x04; // Clear more frag bit of 802.11 frame control
319 pDes->FragmentCount = FragmentCount; // Update the correct fragment number
320 return Size;
323 static void Mds_HeaderCopy(struct wbsoft_priv * adapter, PDESCRIPTOR pDes, u8 *TargetBuffer)
325 PMDS pMds = &adapter->Mds;
326 u8 *src_buffer = pDes->buffer_address[0];//931130.5.g
327 PT00_DESCRIPTOR pT00;
328 PT01_DESCRIPTOR pT01;
329 u16 stmp;
330 u8 i, ctmp1, ctmp2, ctmpf;
331 u16 FragmentThreshold = CURRENT_FRAGMENT_THRESHOLD;
334 stmp = pDes->buffer_total_size;
336 // Set USB header 8 byte
338 pT00 = (PT00_DESCRIPTOR)TargetBuffer;
339 TargetBuffer += 4;
340 pT01 = (PT01_DESCRIPTOR)TargetBuffer;
341 TargetBuffer += 4;
343 pT00->value = 0;// Clear
344 pT01->value = 0;// Clear
346 pT00->T00_tx_packet_id = pDes->Descriptor_ID;// Set packet ID
347 pT00->T00_header_length = 24;// Set header length
348 pT01->T01_retry_abort_ebable = 1;//921013 931130.5.h
350 // Key ID setup
351 pT01->T01_wep_id = 0;
353 FragmentThreshold = DEFAULT_FRAGMENT_THRESHOLD; //Do not fragment
354 // Copy full data, the 1'st buffer contain all the data 931130.5.j
355 memcpy( TargetBuffer, src_buffer, DOT_11_MAC_HEADER_SIZE );// Copy header
356 pDes->buffer_address[0] = src_buffer + DOT_11_MAC_HEADER_SIZE;
357 pDes->buffer_total_size -= DOT_11_MAC_HEADER_SIZE;
358 pDes->buffer_size[0] = pDes->buffer_total_size;
360 // Set fragment threshold
361 FragmentThreshold -= (DOT_11_MAC_HEADER_SIZE + 4);
362 pDes->FragmentThreshold = FragmentThreshold;
364 // Set more frag bit
365 TargetBuffer[1] |= 0x04;// Set more frag bit
368 // Set tx rate
370 stmp = *(u16 *)(TargetBuffer+30); // 2n alignment address
372 //Use basic rate
373 ctmp1 = ctmpf = CURRENT_TX_RATE_FOR_MNG;
375 pDes->TxRate = ctmp1;
376 #ifdef _PE_TX_DUMP_
377 printk("Tx rate =%x\n", ctmp1);
378 #endif
380 pT01->T01_modulation_type = (ctmp1%3) ? 0 : 1;
382 for( i=0; i<2; i++ ) {
383 if( i == 1 )
384 ctmp1 = ctmpf;
386 pMds->TxRate[pDes->Descriptor_ID][i] = ctmp1; // backup the ta rate and fall back rate
388 if( ctmp1 == 108) ctmp2 = 7;
389 else if( ctmp1 == 96 ) ctmp2 = 6; // Rate convert for USB
390 else if( ctmp1 == 72 ) ctmp2 = 5;
391 else if( ctmp1 == 48 ) ctmp2 = 4;
392 else if( ctmp1 == 36 ) ctmp2 = 3;
393 else if( ctmp1 == 24 ) ctmp2 = 2;
394 else if( ctmp1 == 18 ) ctmp2 = 1;
395 else if( ctmp1 == 12 ) ctmp2 = 0;
396 else if( ctmp1 == 22 ) ctmp2 = 3;
397 else if( ctmp1 == 11 ) ctmp2 = 2;
398 else if( ctmp1 == 4 ) ctmp2 = 1;
399 else ctmp2 = 0; // if( ctmp1 == 2 ) or default
401 if( i == 0 )
402 pT01->T01_transmit_rate = ctmp2;
403 else
404 pT01->T01_fall_back_rate = ctmp2;
408 // Set preamble type
410 if ((pT01->T01_modulation_type == 0) && (pT01->T01_transmit_rate == 0)) // RATE_1M
411 pDes->PreambleMode = WLAN_PREAMBLE_TYPE_LONG;
412 else
413 pDes->PreambleMode = CURRENT_PREAMBLE_MODE;
414 pT01->T01_plcp_header_length = pDes->PreambleMode; // Set preamble
418 void
419 Mds_Tx(struct wbsoft_priv * adapter)
421 struct hw_data * pHwData = &adapter->sHwData;
422 PMDS pMds = &adapter->Mds;
423 DESCRIPTOR TxDes;
424 PDESCRIPTOR pTxDes = &TxDes;
425 u8 *XmitBufAddress;
426 u16 XmitBufSize, PacketSize, stmp, CurrentSize, FragmentThreshold;
427 u8 FillIndex, TxDesIndex, FragmentCount, FillCount;
428 unsigned char BufferFilled = false, MICAdd = 0;
431 if (pMds->TxPause)
432 return;
433 if (!hal_driver_init_OK(pHwData))
434 return;
436 //Only one thread can be run here
437 if (!atomic_inc_return(&pMds->TxThreadCount) == 1)
438 goto cleanup;
440 // Start to fill the data
441 do {
442 FillIndex = pMds->TxFillIndex;
443 if (pMds->TxOwner[FillIndex]) { // Is owned by software 0:Yes 1:No
444 #ifdef _PE_TX_DUMP_
445 printk("[Mds_Tx] Tx Owner is H/W.\n");
446 #endif
447 break;
450 XmitBufAddress = pMds->pTxBuffer + (MAX_USB_TX_BUFFER * FillIndex); //Get buffer
451 XmitBufSize = 0;
452 FillCount = 0;
453 do {
454 PacketSize = adapter->sMlmeFrame.len;
455 if (!PacketSize)
456 break;
458 //For Check the buffer resource
459 FragmentThreshold = CURRENT_FRAGMENT_THRESHOLD;
460 //931130.5.b
461 FragmentCount = PacketSize/FragmentThreshold + 1;
462 stmp = PacketSize + FragmentCount*32 + 8;//931130.5.c 8:MIC
463 if ((XmitBufSize + stmp) >= MAX_USB_TX_BUFFER) {
464 printk("[Mds_Tx] Excess max tx buffer.\n");
465 break; // buffer is not enough
470 // Start transmitting
472 BufferFilled = true;
474 /* Leaves first u8 intact */
475 memset((u8 *)pTxDes + 1, 0, sizeof(DESCRIPTOR) - 1);
477 TxDesIndex = pMds->TxDesIndex;//Get the current ID
478 pTxDes->Descriptor_ID = TxDesIndex;
479 pMds->TxDesFrom[ TxDesIndex ] = 2;//Storing the information of source comming from
480 pMds->TxDesIndex++;
481 pMds->TxDesIndex %= MAX_USB_TX_DESCRIPTOR;
483 MLME_GetNextPacket( adapter, pTxDes );
485 // Copy header. 8byte USB + 24byte 802.11Hdr. Set TxRate, Preamble type
486 Mds_HeaderCopy( adapter, pTxDes, XmitBufAddress );
488 // For speed up Key setting
489 if (pTxDes->EapFix) {
490 #ifdef _PE_TX_DUMP_
491 printk("35: EPA 4th frame detected. Size = %d\n", PacketSize);
492 #endif
493 pHwData->IsKeyPreSet = 1;
496 // Copy (fragment) frame body, and set USB, 802.11 hdr flag
497 CurrentSize = Mds_BodyCopy(adapter, pTxDes, XmitBufAddress);
499 // Set RTS/CTS and Normal duration field into buffer
500 Mds_DurationSet(adapter, pTxDes, XmitBufAddress);
503 // Calculation MIC from buffer which maybe fragment, then fill into temporary address 8 byte
504 // 931130.5.e
505 if (MICAdd)
506 Mds_MicFill( adapter, pTxDes, XmitBufAddress );
508 //Shift to the next address
509 XmitBufSize += CurrentSize;
510 XmitBufAddress += CurrentSize;
512 #ifdef _IBSS_BEACON_SEQ_STICK_
513 if ((XmitBufAddress[ DOT_11_DA_OFFSET+8 ] & 0xfc) != MAC_SUBTYPE_MNGMNT_PROBE_REQUEST) // +8 for USB hdr
514 #endif
515 pMds->TxToggle = true;
517 // Get packet to transmit completed, 1:TESTSTA 2:MLME 3: Ndis data
518 MLME_SendComplete(adapter, 0, true);
520 // Software TSC count 20060214
521 pMds->TxTsc++;
522 if (pMds->TxTsc == 0)
523 pMds->TxTsc_2++;
525 FillCount++; // 20060928
526 } while (HAL_USB_MODE_BURST(pHwData)); // End of multiple MSDU copy loop. false = single true = multiple sending
528 // Move to the next one, if necessary
529 if (BufferFilled) {
530 // size setting
531 pMds->TxBufferSize[ FillIndex ] = XmitBufSize;
533 // 20060928 set Tx count
534 pMds->TxCountInBuffer[FillIndex] = FillCount;
536 // Set owner flag
537 pMds->TxOwner[FillIndex] = 1;
539 pMds->TxFillIndex++;
540 pMds->TxFillIndex %= MAX_USB_TX_BUFFER_NUMBER;
541 BufferFilled = false;
542 } else
543 break;
545 if (!PacketSize) // No more pk for transmitting
546 break;
548 } while(true);
551 // Start to send by lower module
553 if (!pHwData->IsKeyPreSet)
554 Wb35Tx_start(adapter);
556 cleanup:
557 atomic_dec(&pMds->TxThreadCount);
560 void
561 Mds_SendComplete(struct wbsoft_priv * adapter, PT02_DESCRIPTOR pT02)
563 PMDS pMds = &adapter->Mds;
564 struct hw_data * pHwData = &adapter->sHwData;
565 u8 PacketId = (u8)pT02->T02_Tx_PktID;
566 unsigned char SendOK = true;
567 u8 RetryCount, TxRate;
569 if (pT02->T02_IgnoreResult) // Don't care the result
570 return;
571 if (pT02->T02_IsLastMpdu) {
572 //TODO: DTO -- get the retry count and fragment count
573 // Tx rate
574 TxRate = pMds->TxRate[ PacketId ][ 0 ];
575 RetryCount = (u8)pT02->T02_MPDU_Cnt;
576 if (pT02->value & FLAG_ERROR_TX_MASK) {
577 SendOK = false;
579 if (pT02->T02_transmit_abort || pT02->T02_out_of_MaxTxMSDULiftTime) {
580 //retry error
581 pHwData->dto_tx_retry_count += (RetryCount+1);
582 //[for tx debug]
583 if (RetryCount<7)
584 pHwData->tx_retry_count[RetryCount] += RetryCount;
585 else
586 pHwData->tx_retry_count[7] += RetryCount;
587 #ifdef _PE_STATE_DUMP_
588 printk("dto_tx_retry_count =%d\n", pHwData->dto_tx_retry_count);
589 #endif
590 MTO_SetTxCount(adapter, TxRate, RetryCount);
592 pHwData->dto_tx_frag_count += (RetryCount+1);
594 //[for tx debug]
595 if (pT02->T02_transmit_abort_due_to_TBTT)
596 pHwData->tx_TBTT_start_count++;
597 if (pT02->T02_transmit_without_encryption_due_to_wep_on_false)
598 pHwData->tx_WepOn_false_count++;
599 if (pT02->T02_discard_due_to_null_wep_key)
600 pHwData->tx_Null_key_count++;
601 } else {
602 if (pT02->T02_effective_transmission_rate)
603 pHwData->tx_ETR_count++;
604 MTO_SetTxCount(adapter, TxRate, RetryCount);
607 // Clear send result buffer
608 pMds->TxResult[ PacketId ] = 0;
609 } else
610 pMds->TxResult[ PacketId ] |= ((u16)(pT02->value & 0x0ffff));