Merge branch 'mini2440-dev-unlikely' into mini2440-dev
[linux-2.6/mini2440.git] / drivers / staging / winbond / mds.c
blob37e0c185d113e08d49270032dd99d0a67aa2d2ba
1 #include "mds_f.h"
2 #include "mlmetxrx_f.h"
3 #include "mto.h"
4 #include "sysdef.h"
5 #include "wbhal_f.h"
6 #include "wblinux_f.h"
8 unsigned char
9 Mds_initial(struct wbsoft_priv * adapter)
11 struct wb35_mds *pMds = &adapter->Mds;
13 pMds->TxPause = false;
14 pMds->TxRTSThreshold = DEFAULT_RTSThreshold;
15 pMds->TxFragmentThreshold = DEFAULT_FRAGMENT_THRESHOLD;
17 return hal_get_tx_buffer( &adapter->sHwData, &pMds->pTxBuffer );
20 void
21 Mds_Destroy(struct wbsoft_priv * adapter)
25 static void Mds_DurationSet(struct wbsoft_priv *adapter, struct wb35_descriptor *pDes, u8 *buffer)
27 PT00_DESCRIPTOR pT00;
28 PT01_DESCRIPTOR pT01;
29 u16 Duration, NextBodyLen, OffsetSize;
30 u8 Rate, i;
31 unsigned char CTS_on = false, RTS_on = false;
32 PT00_DESCRIPTOR pNextT00;
33 u16 BodyLen = 0;
34 unsigned char boGroupAddr = false;
36 OffsetSize = pDes->FragmentThreshold + 32 + 3;
37 OffsetSize &= ~0x03;
38 Rate = pDes->TxRate >> 1;
39 if (!Rate)
40 Rate = 1;
42 pT00 = (PT00_DESCRIPTOR)buffer;
43 pT01 = (PT01_DESCRIPTOR)(buffer+4);
44 pNextT00 = (PT00_DESCRIPTOR)(buffer+OffsetSize);
46 if( buffer[ DOT_11_DA_OFFSET+8 ] & 0x1 ) // +8 for USB hdr
47 boGroupAddr = true;
49 //========================================
50 // Set RTS/CTS mechanism
51 //========================================
52 if (!boGroupAddr)
54 //NOTE : If the protection mode is enabled and the MSDU will be fragmented,
55 // the tx rates of MPDUs will all be DSSS rates. So it will not use
56 // CTS-to-self in this case. CTS-To-self will only be used when without
57 // fragmentation. -- 20050112
58 BodyLen = (u16)pT00->T00_frame_length; //include 802.11 header
59 BodyLen += 4; //CRC
61 if( BodyLen >= CURRENT_RTS_THRESHOLD )
62 RTS_on = true; // Using RTS
63 else
65 if( pT01->T01_modulation_type ) // Is using OFDM
67 if( CURRENT_PROTECT_MECHANISM ) // Is using protect
68 CTS_on = true; // Using CTS
73 if( RTS_on || CTS_on )
75 if( pT01->T01_modulation_type) // Is using OFDM
77 //CTS duration
78 // 2 SIFS + DATA transmit time + 1 ACK
79 // ACK Rate : 24 Mega bps
80 // ACK frame length = 14 bytes
81 Duration = 2*DEFAULT_SIFSTIME +
82 2*PREAMBLE_PLUS_SIGNAL_PLUS_SIGNALEXTENSION +
83 ((BodyLen*8 + 22 + Rate*4 - 1)/(Rate*4))*Tsym +
84 ((112 + 22 + 95)/96)*Tsym;
86 else //DSSS
88 //CTS duration
89 // 2 SIFS + DATA transmit time + 1 ACK
90 // Rate : ?? Mega bps
91 // ACK frame length = 14 bytes
92 if( pT01->T01_plcp_header_length ) //long preamble
93 Duration = LONG_PREAMBLE_PLUS_PLCPHEADER_TIME*2;
94 else
95 Duration = SHORT_PREAMBLE_PLUS_PLCPHEADER_TIME*2;
97 Duration += ( ((BodyLen + 14)*8 + Rate-1) / Rate +
98 DEFAULT_SIFSTIME*2 );
101 if( RTS_on )
103 if( pT01->T01_modulation_type ) // Is using OFDM
105 //CTS + 1 SIFS + CTS duration
106 //CTS Rate : 24 Mega bps
107 //CTS frame length = 14 bytes
108 Duration += (DEFAULT_SIFSTIME +
109 PREAMBLE_PLUS_SIGNAL_PLUS_SIGNALEXTENSION +
110 ((112 + 22 + 95)/96)*Tsym);
112 else
114 //CTS + 1 SIFS + CTS duration
115 //CTS Rate : ?? Mega bps
116 //CTS frame length = 14 bytes
117 if( pT01->T01_plcp_header_length ) //long preamble
118 Duration += LONG_PREAMBLE_PLUS_PLCPHEADER_TIME;
119 else
120 Duration += SHORT_PREAMBLE_PLUS_PLCPHEADER_TIME;
122 Duration += ( ((112 + Rate-1) / Rate) + DEFAULT_SIFSTIME );
126 // Set the value into USB descriptor
127 pT01->T01_add_rts = RTS_on ? 1 : 0;
128 pT01->T01_add_cts = CTS_on ? 1 : 0;
129 pT01->T01_rts_cts_duration = Duration;
132 //=====================================
133 // Fill the more fragment descriptor
134 //=====================================
135 if( boGroupAddr )
136 Duration = 0;
137 else
139 for( i=pDes->FragmentCount-1; i>0; i-- )
141 NextBodyLen = (u16)pNextT00->T00_frame_length;
142 NextBodyLen += 4; //CRC
144 if( pT01->T01_modulation_type )
146 //OFDM
147 // data transmit time + 3 SIFS + 2 ACK
148 // Rate : ??Mega bps
149 // ACK frame length = 14 bytes, tx rate = 24M
150 Duration = PREAMBLE_PLUS_SIGNAL_PLUS_SIGNALEXTENSION * 3;
151 Duration += (((NextBodyLen*8 + 22 + Rate*4 - 1)/(Rate*4)) * Tsym +
152 (((2*14)*8 + 22 + 95)/96)*Tsym +
153 DEFAULT_SIFSTIME*3);
155 else
157 //DSSS
158 // data transmit time + 2 ACK + 3 SIFS
159 // Rate : ??Mega bps
160 // ACK frame length = 14 bytes
161 //TODO :
162 if( pT01->T01_plcp_header_length ) //long preamble
163 Duration = LONG_PREAMBLE_PLUS_PLCPHEADER_TIME*3;
164 else
165 Duration = SHORT_PREAMBLE_PLUS_PLCPHEADER_TIME*3;
167 Duration += ( ((NextBodyLen + (2*14))*8 + Rate-1) / Rate +
168 DEFAULT_SIFSTIME*3 );
171 ((u16 *)buffer)[5] = cpu_to_le16(Duration);// 4 USHOR for skip 8B USB, 2USHORT=FC + Duration
173 //----20061009 add by anson's endian
174 pNextT00->value = cpu_to_le32(pNextT00->value);
175 pT01->value = cpu_to_le32( pT01->value );
176 //----end 20061009 add by anson's endian
178 buffer += OffsetSize;
179 pT01 = (PT01_DESCRIPTOR)(buffer+4);
180 if (i != 1) //The last fragment will not have the next fragment
181 pNextT00 = (PT00_DESCRIPTOR)(buffer+OffsetSize);
184 //=====================================
185 // Fill the last fragment descriptor
186 //=====================================
187 if( pT01->T01_modulation_type )
189 //OFDM
190 // 1 SIFS + 1 ACK
191 // Rate : 24 Mega bps
192 // ACK frame length = 14 bytes
193 Duration = PREAMBLE_PLUS_SIGNAL_PLUS_SIGNALEXTENSION;
194 //The Tx rate of ACK use 24M
195 Duration += (((112 + 22 + 95)/96)*Tsym + DEFAULT_SIFSTIME );
197 else
199 // DSSS
200 // 1 ACK + 1 SIFS
201 // Rate : ?? Mega bps
202 // ACK frame length = 14 bytes(112 bits)
203 if( pT01->T01_plcp_header_length ) //long preamble
204 Duration = LONG_PREAMBLE_PLUS_PLCPHEADER_TIME;
205 else
206 Duration = SHORT_PREAMBLE_PLUS_PLCPHEADER_TIME;
208 Duration += ( (112 + Rate-1)/Rate + DEFAULT_SIFSTIME );
212 ((u16 *)buffer)[5] = cpu_to_le16(Duration);// 4 USHOR for skip 8B USB, 2USHORT=FC + Duration
213 pT00->value = cpu_to_le32(pT00->value);
214 pT01->value = cpu_to_le32(pT01->value);
215 //--end 20061009 add
219 // The function return the 4n size of usb pk
220 static u16 Mds_BodyCopy(struct wbsoft_priv *adapter, struct wb35_descriptor *pDes, u8 *TargetBuffer)
222 PT00_DESCRIPTOR pT00;
223 struct wb35_mds *pMds = &adapter->Mds;
224 u8 *buffer;
225 u8 *src_buffer;
226 u8 *pctmp;
227 u16 Size = 0;
228 u16 SizeLeft, CopySize, CopyLeft, stmp;
229 u8 buf_index, FragmentCount = 0;
232 // Copy fragment body
233 buffer = TargetBuffer; // shift 8B usb + 24B 802.11
234 SizeLeft = pDes->buffer_total_size;
235 buf_index = pDes->buffer_start_index;
237 pT00 = (PT00_DESCRIPTOR)buffer;
238 while (SizeLeft) {
239 pT00 = (PT00_DESCRIPTOR)buffer;
240 CopySize = SizeLeft;
241 if (SizeLeft > pDes->FragmentThreshold) {
242 CopySize = pDes->FragmentThreshold;
243 pT00->T00_frame_length = 24 + CopySize;//Set USB length
244 } else
245 pT00->T00_frame_length = 24 + SizeLeft;//Set USB length
247 SizeLeft -= CopySize;
249 // 1 Byte operation
250 pctmp = (u8 *)( buffer + 8 + DOT_11_SEQUENCE_OFFSET );
251 *pctmp &= 0xf0;
252 *pctmp |= FragmentCount;//931130.5.m
253 if( !FragmentCount )
254 pT00->T00_first_mpdu = 1;
256 buffer += 32; // 8B usb + 24B 802.11 header
257 Size += 32;
259 // Copy into buffer
260 stmp = CopySize + 3;
261 stmp &= ~0x03;//4n Alignment
262 Size += stmp;// Current 4n offset of mpdu
264 while (CopySize) {
265 // Copy body
266 src_buffer = pDes->buffer_address[buf_index];
267 CopyLeft = CopySize;
268 if (CopySize >= pDes->buffer_size[buf_index]) {
269 CopyLeft = pDes->buffer_size[buf_index];
271 // Get the next buffer of descriptor
272 buf_index++;
273 buf_index %= MAX_DESCRIPTOR_BUFFER_INDEX;
274 } else {
275 u8 *pctmp = pDes->buffer_address[buf_index];
276 pctmp += CopySize;
277 pDes->buffer_address[buf_index] = pctmp;
278 pDes->buffer_size[buf_index] -= CopySize;
281 memcpy(buffer, src_buffer, CopyLeft);
282 buffer += CopyLeft;
283 CopySize -= CopyLeft;
286 // 931130.5.n
287 if (pMds->MicAdd) {
288 if (!SizeLeft) {
289 pMds->MicWriteAddress[ pMds->MicWriteIndex ] = buffer - pMds->MicAdd;
290 pMds->MicWriteSize[ pMds->MicWriteIndex ] = pMds->MicAdd;
291 pMds->MicAdd = 0;
293 else if( SizeLeft < 8 ) //931130.5.p
295 pMds->MicAdd = SizeLeft;
296 pMds->MicWriteAddress[ pMds->MicWriteIndex ] = buffer - ( 8 - SizeLeft );
297 pMds->MicWriteSize[ pMds->MicWriteIndex ] = 8 - SizeLeft;
298 pMds->MicWriteIndex++;
302 // Does it need to generate the new header for next mpdu?
303 if (SizeLeft) {
304 buffer = TargetBuffer + Size; // Get the next 4n start address
305 memcpy( buffer, TargetBuffer, 32 );//Copy 8B USB +24B 802.11
306 pT00 = (PT00_DESCRIPTOR)buffer;
307 pT00->T00_first_mpdu = 0;
310 FragmentCount++;
313 pT00->T00_last_mpdu = 1;
314 pT00->T00_IsLastMpdu = 1;
315 buffer = (u8 *)pT00 + 8; // +8 for USB hdr
316 buffer[1] &= ~0x04; // Clear more frag bit of 802.11 frame control
317 pDes->FragmentCount = FragmentCount; // Update the correct fragment number
318 return Size;
321 static void Mds_HeaderCopy(struct wbsoft_priv * adapter, struct wb35_descriptor *pDes, u8 *TargetBuffer)
323 struct wb35_mds *pMds = &adapter->Mds;
324 u8 *src_buffer = pDes->buffer_address[0];//931130.5.g
325 PT00_DESCRIPTOR pT00;
326 PT01_DESCRIPTOR pT01;
327 u16 stmp;
328 u8 i, ctmp1, ctmp2, ctmpf;
329 u16 FragmentThreshold = CURRENT_FRAGMENT_THRESHOLD;
332 stmp = pDes->buffer_total_size;
334 // Set USB header 8 byte
336 pT00 = (PT00_DESCRIPTOR)TargetBuffer;
337 TargetBuffer += 4;
338 pT01 = (PT01_DESCRIPTOR)TargetBuffer;
339 TargetBuffer += 4;
341 pT00->value = 0;// Clear
342 pT01->value = 0;// Clear
344 pT00->T00_tx_packet_id = pDes->Descriptor_ID;// Set packet ID
345 pT00->T00_header_length = 24;// Set header length
346 pT01->T01_retry_abort_ebable = 1;//921013 931130.5.h
348 // Key ID setup
349 pT01->T01_wep_id = 0;
351 FragmentThreshold = DEFAULT_FRAGMENT_THRESHOLD; //Do not fragment
352 // Copy full data, the 1'st buffer contain all the data 931130.5.j
353 memcpy( TargetBuffer, src_buffer, DOT_11_MAC_HEADER_SIZE );// Copy header
354 pDes->buffer_address[0] = src_buffer + DOT_11_MAC_HEADER_SIZE;
355 pDes->buffer_total_size -= DOT_11_MAC_HEADER_SIZE;
356 pDes->buffer_size[0] = pDes->buffer_total_size;
358 // Set fragment threshold
359 FragmentThreshold -= (DOT_11_MAC_HEADER_SIZE + 4);
360 pDes->FragmentThreshold = FragmentThreshold;
362 // Set more frag bit
363 TargetBuffer[1] |= 0x04;// Set more frag bit
366 // Set tx rate
368 stmp = *(u16 *)(TargetBuffer+30); // 2n alignment address
370 //Use basic rate
371 ctmp1 = ctmpf = CURRENT_TX_RATE_FOR_MNG;
373 pDes->TxRate = ctmp1;
374 #ifdef _PE_TX_DUMP_
375 printk("Tx rate =%x\n", ctmp1);
376 #endif
378 pT01->T01_modulation_type = (ctmp1%3) ? 0 : 1;
380 for( i=0; i<2; i++ ) {
381 if( i == 1 )
382 ctmp1 = ctmpf;
384 pMds->TxRate[pDes->Descriptor_ID][i] = ctmp1; // backup the ta rate and fall back rate
386 if( ctmp1 == 108) ctmp2 = 7;
387 else if( ctmp1 == 96 ) ctmp2 = 6; // Rate convert for USB
388 else if( ctmp1 == 72 ) ctmp2 = 5;
389 else if( ctmp1 == 48 ) ctmp2 = 4;
390 else if( ctmp1 == 36 ) ctmp2 = 3;
391 else if( ctmp1 == 24 ) ctmp2 = 2;
392 else if( ctmp1 == 18 ) ctmp2 = 1;
393 else if( ctmp1 == 12 ) ctmp2 = 0;
394 else if( ctmp1 == 22 ) ctmp2 = 3;
395 else if( ctmp1 == 11 ) ctmp2 = 2;
396 else if( ctmp1 == 4 ) ctmp2 = 1;
397 else ctmp2 = 0; // if( ctmp1 == 2 ) or default
399 if( i == 0 )
400 pT01->T01_transmit_rate = ctmp2;
401 else
402 pT01->T01_fall_back_rate = ctmp2;
406 // Set preamble type
408 if ((pT01->T01_modulation_type == 0) && (pT01->T01_transmit_rate == 0)) // RATE_1M
409 pDes->PreambleMode = WLAN_PREAMBLE_TYPE_LONG;
410 else
411 pDes->PreambleMode = CURRENT_PREAMBLE_MODE;
412 pT01->T01_plcp_header_length = pDes->PreambleMode; // Set preamble
416 void
417 Mds_Tx(struct wbsoft_priv * adapter)
419 struct hw_data * pHwData = &adapter->sHwData;
420 struct wb35_mds *pMds = &adapter->Mds;
421 struct wb35_descriptor TxDes;
422 struct wb35_descriptor *pTxDes = &TxDes;
423 u8 *XmitBufAddress;
424 u16 XmitBufSize, PacketSize, stmp, CurrentSize, FragmentThreshold;
425 u8 FillIndex, TxDesIndex, FragmentCount, FillCount;
426 unsigned char BufferFilled = false;
429 if (pMds->TxPause)
430 return;
431 if (!hal_driver_init_OK(pHwData))
432 return;
434 //Only one thread can be run here
435 if (atomic_inc_return(&pMds->TxThreadCount) != 1)
436 goto cleanup;
438 // Start to fill the data
439 do {
440 FillIndex = pMds->TxFillIndex;
441 if (pMds->TxOwner[FillIndex]) { // Is owned by software 0:Yes 1:No
442 #ifdef _PE_TX_DUMP_
443 printk("[Mds_Tx] Tx Owner is H/W.\n");
444 #endif
445 break;
448 XmitBufAddress = pMds->pTxBuffer + (MAX_USB_TX_BUFFER * FillIndex); //Get buffer
449 XmitBufSize = 0;
450 FillCount = 0;
451 do {
452 PacketSize = adapter->sMlmeFrame.len;
453 if (!PacketSize)
454 break;
456 //For Check the buffer resource
457 FragmentThreshold = CURRENT_FRAGMENT_THRESHOLD;
458 //931130.5.b
459 FragmentCount = PacketSize/FragmentThreshold + 1;
460 stmp = PacketSize + FragmentCount*32 + 8;//931130.5.c 8:MIC
461 if ((XmitBufSize + stmp) >= MAX_USB_TX_BUFFER) {
462 printk("[Mds_Tx] Excess max tx buffer.\n");
463 break; // buffer is not enough
468 // Start transmitting
470 BufferFilled = true;
472 /* Leaves first u8 intact */
473 memset((u8 *)pTxDes + 1, 0, sizeof(struct wb35_descriptor) - 1);
475 TxDesIndex = pMds->TxDesIndex;//Get the current ID
476 pTxDes->Descriptor_ID = TxDesIndex;
477 pMds->TxDesFrom[ TxDesIndex ] = 2;//Storing the information of source comming from
478 pMds->TxDesIndex++;
479 pMds->TxDesIndex %= MAX_USB_TX_DESCRIPTOR;
481 MLME_GetNextPacket( adapter, pTxDes );
483 // Copy header. 8byte USB + 24byte 802.11Hdr. Set TxRate, Preamble type
484 Mds_HeaderCopy( adapter, pTxDes, XmitBufAddress );
486 // For speed up Key setting
487 if (pTxDes->EapFix) {
488 #ifdef _PE_TX_DUMP_
489 printk("35: EPA 4th frame detected. Size = %d\n", PacketSize);
490 #endif
491 pHwData->IsKeyPreSet = 1;
494 // Copy (fragment) frame body, and set USB, 802.11 hdr flag
495 CurrentSize = Mds_BodyCopy(adapter, pTxDes, XmitBufAddress);
497 // Set RTS/CTS and Normal duration field into buffer
498 Mds_DurationSet(adapter, pTxDes, XmitBufAddress);
500 //Shift to the next address
501 XmitBufSize += CurrentSize;
502 XmitBufAddress += CurrentSize;
504 #ifdef _IBSS_BEACON_SEQ_STICK_
505 if ((XmitBufAddress[ DOT_11_DA_OFFSET+8 ] & 0xfc) != MAC_SUBTYPE_MNGMNT_PROBE_REQUEST) // +8 for USB hdr
506 #endif
507 pMds->TxToggle = true;
509 // Get packet to transmit completed, 1:TESTSTA 2:MLME 3: Ndis data
510 MLME_SendComplete(adapter, 0, true);
512 // Software TSC count 20060214
513 pMds->TxTsc++;
514 if (pMds->TxTsc == 0)
515 pMds->TxTsc_2++;
517 FillCount++; // 20060928
518 } while (HAL_USB_MODE_BURST(pHwData)); // End of multiple MSDU copy loop. false = single true = multiple sending
520 // Move to the next one, if necessary
521 if (BufferFilled) {
522 // size setting
523 pMds->TxBufferSize[ FillIndex ] = XmitBufSize;
525 // 20060928 set Tx count
526 pMds->TxCountInBuffer[FillIndex] = FillCount;
528 // Set owner flag
529 pMds->TxOwner[FillIndex] = 1;
531 pMds->TxFillIndex++;
532 pMds->TxFillIndex %= MAX_USB_TX_BUFFER_NUMBER;
533 BufferFilled = false;
534 } else
535 break;
537 if (!PacketSize) // No more pk for transmitting
538 break;
540 } while(true);
543 // Start to send by lower module
545 if (!pHwData->IsKeyPreSet)
546 Wb35Tx_start(adapter);
548 cleanup:
549 atomic_dec(&pMds->TxThreadCount);
552 void
553 Mds_SendComplete(struct wbsoft_priv * adapter, PT02_DESCRIPTOR pT02)
555 struct wb35_mds *pMds = &adapter->Mds;
556 struct hw_data * pHwData = &adapter->sHwData;
557 u8 PacketId = (u8)pT02->T02_Tx_PktID;
558 unsigned char SendOK = true;
559 u8 RetryCount, TxRate;
561 if (pT02->T02_IgnoreResult) // Don't care the result
562 return;
563 if (pT02->T02_IsLastMpdu) {
564 //TODO: DTO -- get the retry count and fragment count
565 // Tx rate
566 TxRate = pMds->TxRate[ PacketId ][ 0 ];
567 RetryCount = (u8)pT02->T02_MPDU_Cnt;
568 if (pT02->value & FLAG_ERROR_TX_MASK) {
569 SendOK = false;
571 if (pT02->T02_transmit_abort || pT02->T02_out_of_MaxTxMSDULiftTime) {
572 //retry error
573 pHwData->dto_tx_retry_count += (RetryCount+1);
574 //[for tx debug]
575 if (RetryCount<7)
576 pHwData->tx_retry_count[RetryCount] += RetryCount;
577 else
578 pHwData->tx_retry_count[7] += RetryCount;
579 #ifdef _PE_STATE_DUMP_
580 printk("dto_tx_retry_count =%d\n", pHwData->dto_tx_retry_count);
581 #endif
582 MTO_SetTxCount(adapter, TxRate, RetryCount);
584 pHwData->dto_tx_frag_count += (RetryCount+1);
586 //[for tx debug]
587 if (pT02->T02_transmit_abort_due_to_TBTT)
588 pHwData->tx_TBTT_start_count++;
589 if (pT02->T02_transmit_without_encryption_due_to_wep_on_false)
590 pHwData->tx_WepOn_false_count++;
591 if (pT02->T02_discard_due_to_null_wep_key)
592 pHwData->tx_Null_key_count++;
593 } else {
594 if (pT02->T02_effective_transmission_rate)
595 pHwData->tx_ETR_count++;
596 MTO_SetTxCount(adapter, TxRate, RetryCount);
599 // Clear send result buffer
600 pMds->TxResult[ PacketId ] = 0;
601 } else
602 pMds->TxResult[ PacketId ] |= ((u16)(pT02->value & 0x0ffff));