2 * (c) Copyright 2008, RealTEK Technologies Inc. All Rights Reserved.
4 * Module: r819xusb_cmdpkt.c
5 * (RTL8190 TX/RX command packet handler Source C File)
7 * Note: The module is responsible for handling TX and RX command packet.
8 * 1.TX: Send set and query configuration command packet.
9 * 2.RX: Receive tx feedback, beacon state, query configuration, command packet.
12 #include "r819xU_cmdpkt.h"
14 bool SendTxCommandPacket(struct net_device
*dev
, void *pData
, u32 DataLen
)
17 struct r8192_priv
*priv
= ieee80211_priv(dev
);
20 unsigned char *ptr_buf
;
22 /* PlatformAcquireSpinLock(Adapter, RT_TX_SPINLOCK); */
25 * Get TCB and local buffer from common pool.
26 * (It is shared by CmdQ, MgntQ, and USB coalesce DataQ)
28 skb
= dev_alloc_skb(USB_HWDESC_HEADER_LEN
+ DataLen
+ 4);
30 RT_TRACE(COMP_ERR
, "(%s): unable to alloc skb buffer\n",
35 memcpy((unsigned char *)(skb
->cb
), &dev
, sizeof(dev
));
36 tcb_desc
= (cb_desc
*)(skb
->cb
+ MAX_DEV_ADDR_SIZE
);
37 tcb_desc
->queue_index
= TXCMD_QUEUE
;
38 tcb_desc
->bCmdOrInit
= DESC_PACKET_TYPE_NORMAL
;
39 tcb_desc
->bLastIniPkt
= 0;
40 skb_reserve(skb
, USB_HWDESC_HEADER_LEN
);
41 ptr_buf
= skb_put(skb
, DataLen
);
42 memcpy(ptr_buf
, pData
, DataLen
);
43 tcb_desc
->txbuf_size
= (u16
)DataLen
;
45 if (!priv
->ieee80211
->check_nic_enough_desc(dev
, tcb_desc
->queue_index
) ||
46 (!skb_queue_empty(&priv
->ieee80211
->skb_waitQ
[tcb_desc
->queue_index
])) ||
47 (priv
->ieee80211
->queue_stop
)) {
48 RT_TRACE(COMP_FIRMWARE
, "NULL packet => tx full\n");
49 skb_queue_tail(&priv
->ieee80211
->skb_waitQ
[tcb_desc
->queue_index
], skb
);
51 priv
->ieee80211
->softmac_hard_start_xmit(skb
, dev
);
54 //PlatformReleaseSpinLock(Adapter, RT_TX_SPINLOCK);
59 * Function: cmpk_message_handle_tx()
61 * Overview: Driver internal module can call the API to send message to
62 * firmware side. For example, you can send a debug command packet.
63 * Or you can send a request for FW to modify RLX4181 LBUS HW bank.
64 * Otherwise, you can change MAC/PHT/RF register by firmware at
65 * run time. We do not support message more than one segment now.
73 extern bool cmpk_message_handle_tx(
74 struct net_device
*dev
,
75 u8
*codevirtualaddress
,
79 bool rt_status
= true;
84 * Function: cmpk_counttxstatistic()
87 cmpk_count_txstatistic(struct net_device
*dev
, cmpk_txfb_t
*pstx_fb
)
89 struct r8192_priv
*priv
= ieee80211_priv(dev
);
91 RT_RF_POWER_STATE rtState
;
93 pAdapter
->HalFunc
.GetHwRegHandler(pAdapter
,
98 * When RF is off, we should not count the packet for hw/sw synchronize
99 * reason, ie. there may be a duration while sw switch is changed and hw
100 * switch is being changed.
102 if (rtState
== eRfOff
)
107 if (pAdapter
->bInHctTest
)
111 * We can not know the packet length and transmit type:
112 * broadcast or uni or multicast.
113 * So the relative statistics must be collected in tx feedback info
116 priv
->stats
.txfeedbackok
++;
117 priv
->stats
.txoktotal
++;
118 priv
->stats
.txokbytestotal
+= pstx_fb
->pkt_length
;
119 priv
->stats
.txokinperiod
++;
120 /* We can not make sure broadcast/multicast or unicast mode. */
121 if (pstx_fb
->pkt_type
== PACKET_MULTICAST
) {
122 priv
->stats
.txmulticast
++;
123 priv
->stats
.txbytesmulticast
+= pstx_fb
->pkt_length
;
124 } else if (pstx_fb
->pkt_type
== PACKET_BROADCAST
) {
125 priv
->stats
.txbroadcast
++;
126 priv
->stats
.txbytesbroadcast
+= pstx_fb
->pkt_length
;
128 priv
->stats
.txunicast
++;
129 priv
->stats
.txbytesunicast
+= pstx_fb
->pkt_length
;
132 priv
->stats
.txfeedbackfail
++;
133 priv
->stats
.txerrtotal
++;
134 priv
->stats
.txerrbytestotal
+= pstx_fb
->pkt_length
;
135 /* We can not make sure broadcast/multicast or unicast mode. */
136 if (pstx_fb
->pkt_type
== PACKET_MULTICAST
)
137 priv
->stats
.txerrmulticast
++;
138 else if (pstx_fb
->pkt_type
== PACKET_BROADCAST
)
139 priv
->stats
.txerrbroadcast
++;
141 priv
->stats
.txerrunicast
++;
143 priv
->stats
.txretrycount
+= pstx_fb
->retry_cnt
;
144 priv
->stats
.txfeedbackretry
+= pstx_fb
->retry_cnt
;
148 * Function: cmpk_handle_tx_feedback()
150 * Overview: The function is responsible for extract the message inside TX
151 * feedbck message from firmware. It will contain dedicated info in
152 * ws-06-0063-rtl8190-command-packet-specification. Please
153 * refer to chapter "TX Feedback Element". We have to read 20 bytes
154 * in the command packet.
156 * Input: struct net_device * dev
157 * u8 *pmsg - Msg Ptr of the command packet.
163 static void cmpk_handle_tx_feedback(struct net_device
*dev
, u8
*pmsg
)
165 struct r8192_priv
*priv
= ieee80211_priv(dev
);
166 cmpk_txfb_t rx_tx_fb
;
168 priv
->stats
.txfeedback
++;
170 /* 1. Extract TX feedback info from RFD to temp structure buffer. */
171 memcpy((u8
*)&rx_tx_fb
, pmsg
, sizeof(cmpk_txfb_t
));
173 /* 2. Use tx feedback info to count TX statistics. */
174 cmpk_count_txstatistic(dev
, &rx_tx_fb
);
177 void cmdpkt_beacontimerinterrupt_819xusb(struct net_device
*dev
)
179 struct r8192_priv
*priv
= ieee80211_priv(dev
);
182 if (priv
->ieee80211
->current_network
.mode
== IEEE_A
||
183 priv
->ieee80211
->current_network
.mode
== IEEE_N_5G
||
184 (priv
->ieee80211
->current_network
.mode
== IEEE_N_24G
&&
185 (!priv
->ieee80211
->pHTInfo
->bCurSuppCCK
))) {
187 DMESG("send beacon frame tx rate is 6Mbpm\n");
190 DMESG("send beacon frame tx rate is 1Mbpm\n");
192 rtl819xusb_beacon_tx(dev
, tx_rate
); /* HW Beacon */
196 * Function: cmpk_handle_interrupt_status()
198 * Overview: The function is responsible for extract the message from
199 * firmware. It will contain dedicated info in
200 * ws-07-0063-v06-rtl819x-command-packet-specification-070315.doc.
201 * Please refer to chapter "Interrupt Status Element".
203 * Input: struct net_device *dev,
204 * u8* pmsg - Message Pointer of the command packet.
210 static void cmpk_handle_interrupt_status(struct net_device
*dev
, u8
*pmsg
)
212 cmpk_intr_sta_t rx_intr_status
; /* */
213 struct r8192_priv
*priv
= ieee80211_priv(dev
);
215 DMESG("---> cmpk_Handle_Interrupt_Status()\n");
217 /* 1. Extract TX feedback info from RFD to temp structure buffer. */
218 rx_intr_status
.length
= pmsg
[1];
219 if (rx_intr_status
.length
!= (sizeof(cmpk_intr_sta_t
) - 2)) {
220 DMESG("cmpk_Handle_Interrupt_Status: wrong length!\n");
223 /* Statistics of beacon for ad-hoc mode. */
224 if (priv
->ieee80211
->iw_mode
== IW_MODE_ADHOC
) {
225 //2 maybe need endian transform?
226 rx_intr_status
.interrupt_status
= *((u32
*)(pmsg
+ 4));
227 //rx_intr_status.InterruptStatus = N2H4BYTE(*((UINT32 *)(pMsg + 4)));
229 DMESG("interrupt status = 0x%x\n", rx_intr_status
.interrupt_status
);
231 if (rx_intr_status
.interrupt_status
& ISR_TxBcnOk
) {
232 priv
->ieee80211
->bibsscoordinator
= true;
233 priv
->stats
.txbeaconokint
++;
234 } else if (rx_intr_status
.interrupt_status
& ISR_TxBcnErr
) {
235 priv
->ieee80211
->bibsscoordinator
= false;
236 priv
->stats
.txbeaconerr
++;
239 if (rx_intr_status
.interrupt_status
& ISR_BcnTimerIntr
)
240 cmdpkt_beacontimerinterrupt_819xusb(dev
);
242 /* Other informations in interrupt status we need? */
243 DMESG("<---- cmpk_handle_interrupt_status()\n");
247 * Function: cmpk_handle_query_config_rx()
249 * Overview: The function is responsible for extract the message from
250 * firmware. It will contain dedicated info in
251 * ws-06-0063-rtl8190-command-packet-specification
252 * Please refer to chapter "Beacon State Element".
254 * Input: u8 * pmsg - Message Pointer of the command packet.
261 static void cmpk_handle_query_config_rx(struct net_device
*dev
, u8
*pmsg
)
263 cmpk_query_cfg_t rx_query_cfg
;
265 * Extract TX feedback info from RFD to temp structure buffer.
267 rx_query_cfg
.cfg_action
= (pmsg
[4] & 0x80000000) >> 31;
268 rx_query_cfg
.cfg_type
= (pmsg
[4] & 0x60) >> 5;
269 rx_query_cfg
.cfg_size
= (pmsg
[4] & 0x18) >> 3;
270 rx_query_cfg
.cfg_page
= (pmsg
[6] & 0x0F) >> 0;
271 rx_query_cfg
.cfg_offset
= pmsg
[7];
272 rx_query_cfg
.value
= (pmsg
[8] << 24) | (pmsg
[9] << 16) |
273 (pmsg
[10] << 8) | (pmsg
[11] << 0);
274 rx_query_cfg
.mask
= (pmsg
[12] << 24) | (pmsg
[13] << 16) |
275 (pmsg
[14] << 8) | (pmsg
[15] << 0);
279 * Function: cmpk_count_tx_status()
281 * Overview: Count aggregated tx status from firmware of one type rx command
282 * packet element id = RX_TX_STATUS.
290 static void cmpk_count_tx_status(struct net_device
*dev
,
291 cmpk_tx_status_t
*pstx_status
)
293 struct r8192_priv
*priv
= ieee80211_priv(dev
);
297 RT_RF_POWER_STATE rtstate
;
299 pAdapter
->HalFunc
.GetHwRegHandler(pAdapter
, HW_VAR_RF_STATE
, (pu1Byte
)(&rtState
));
302 * When RF is off, we should not count the packet for hw/sw synchronize
303 * reason, ie. there may be a duration while sw switch is changed and hw
304 * switch is being changed.
306 if (rtState
== eRfOff
)
310 priv
->stats
.txfeedbackok
+= pstx_status
->txok
;
311 priv
->stats
.txoktotal
+= pstx_status
->txok
;
313 priv
->stats
.txfeedbackfail
+= pstx_status
->txfail
;
314 priv
->stats
.txerrtotal
+= pstx_status
->txfail
;
316 priv
->stats
.txretrycount
+= pstx_status
->txretry
;
317 priv
->stats
.txfeedbackretry
+= pstx_status
->txretry
;
319 priv
->stats
.txmulticast
+= pstx_status
->txmcok
;
320 priv
->stats
.txbroadcast
+= pstx_status
->txbcok
;
321 priv
->stats
.txunicast
+= pstx_status
->txucok
;
323 priv
->stats
.txerrmulticast
+= pstx_status
->txmcfail
;
324 priv
->stats
.txerrbroadcast
+= pstx_status
->txbcfail
;
325 priv
->stats
.txerrunicast
+= pstx_status
->txucfail
;
327 priv
->stats
.txbytesmulticast
+= pstx_status
->txmclength
;
328 priv
->stats
.txbytesbroadcast
+= pstx_status
->txbclength
;
329 priv
->stats
.txbytesunicast
+= pstx_status
->txuclength
;
331 priv
->stats
.last_packet_rate
= pstx_status
->rate
;
335 * Function: cmpk_handle_tx_status()
337 * Overview: Firmware add a new tx feedback status to reduce rx command
338 * packet buffer operation load.
347 cmpk_handle_tx_status(struct net_device
*dev
, u8
*pmsg
)
349 cmpk_tx_status_t rx_tx_sts
;
351 memcpy((void *)&rx_tx_sts
, (void *)pmsg
, sizeof(cmpk_tx_status_t
));
352 /* 2. Use tx feedback info to count TX statistics. */
353 cmpk_count_tx_status(dev
, &rx_tx_sts
);
357 * Function: cmpk_handle_tx_rate_history()
359 * Overview: Firmware add a new tx rate history
367 static void cmpk_handle_tx_rate_history(struct net_device
*dev
, u8
*pmsg
)
369 cmpk_tx_rahis_t
*ptxrate
;
371 u16 length
= sizeof(cmpk_tx_rahis_t
);
373 struct r8192_priv
*priv
= ieee80211_priv(dev
);
376 pAdapter
->HalFunc
.GetHwRegHandler(pAdapter
,
378 (pu1Byte
)(&rtState
));
380 * When RF is off, we should not count the packet for hw/sw synchronize
381 * reason, ie. there may be a duration while sw switch is changed and hw
382 * switch is being changed.
384 if (rtState
== eRfOff
)
390 * Do endian transfer to word alignment(16 bits) for windows system.
391 * You must do different endian transfer for linux and MAC OS
393 for (i
= 0; i
< (length
/4); i
++) {
395 temp1
= ptemp
[i
] & 0x0000FFFF;
396 temp2
= ptemp
[i
] >> 16;
397 ptemp
[i
] = (temp1
<< 16) | temp2
;
400 ptxrate
= (cmpk_tx_rahis_t
*)pmsg
;
405 for (i
= 0; i
< 16; i
++) {
406 /* Collect CCK rate packet num */
408 priv
->stats
.txrate
.cck
[i
] += ptxrate
->cck
[i
];
409 /* Collect OFDM rate packet num */
411 priv
->stats
.txrate
.ofdm
[i
] += ptxrate
->ofdm
[i
];
412 for (j
= 0; j
< 4; j
++)
413 priv
->stats
.txrate
.ht_mcs
[j
][i
] += ptxrate
->ht_mcs
[j
][i
];
419 * Function: cmpk_message_handle_rx()
421 * Overview: In the function, we will capture different RX command packet
422 * info. Every RX command packet element has different message
423 * length and meaning in content. We only support three type of RX
424 * command packet now. Please refer to document
425 * ws-06-0063-rtl8190-command-packet-specification.
434 cmpk_message_handle_rx(
435 struct net_device
*dev
,
436 struct ieee80211_rx_stats
*pstats
)
438 struct r8192_priv
*priv
= ieee80211_priv(dev
);
440 u8 cmd_length
, exe_cnt
= 0;
445 * 0. Check input arguments.
446 * If is is a command queue message or pointer is null
448 if ((pstats
== NULL
))
449 return 0; /* This is not a command packet. */
451 /* 1. Read received command packet message length from RFD. */
452 total_length
= pstats
->Length
;
454 /* 2. Read virtual address from RFD. */
455 pcmd_buff
= pstats
->virtual_address
;
457 /* 3. Read command pakcet element id and length. */
458 element_id
= pcmd_buff
[0];
461 * 4. Check every received command packet conent according to different
462 * element type. Because FW may aggregate RX command packet to minimize
463 * transmit time between DRV and FW.
466 /* Add a counter to prevent to locked in the loop too long */
467 while (total_length
> 0 || exe_cnt
++ > 100) {
468 /* We support aggregation of different cmd in the same packet */
469 element_id
= pcmd_buff
[0];
470 switch (element_id
) {
472 cmpk_handle_tx_feedback(dev
, pcmd_buff
);
473 cmd_length
= CMPK_RX_TX_FB_SIZE
;
475 case RX_INTERRUPT_STATUS
:
476 cmpk_handle_interrupt_status(dev
, pcmd_buff
);
477 cmd_length
= sizeof(cmpk_intr_sta_t
);
479 case BOTH_QUERY_CONFIG
:
480 cmpk_handle_query_config_rx(dev
, pcmd_buff
);
481 cmd_length
= CMPK_BOTH_QUERY_CONFIG_SIZE
;
484 cmpk_handle_tx_status(dev
, pcmd_buff
);
485 cmd_length
= CMPK_RX_TX_STS_SIZE
;
487 case RX_TX_PER_PKT_FEEDBACK
:
488 cmd_length
= CMPK_RX_TX_FB_SIZE
;
490 case RX_TX_RATE_HISTORY
:
491 cmpk_handle_tx_rate_history(dev
, pcmd_buff
);
492 cmd_length
= CMPK_TX_RAHIS_SIZE
;
495 RT_TRACE(COMP_ERR
, "(%s): unknown CMD Element\n",
497 return 1; /* This is a command packet. */
499 priv
->stats
.rxcmdpkt
[element_id
]++;
500 total_length
-= cmd_length
;
501 pcmd_buff
+= cmd_length
;
503 return 1; /* This is a command packet. */