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);
29 memcpy((unsigned char *)(skb->cb), &dev, sizeof(dev));
30 tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
31 tcb_desc->queue_index = TXCMD_QUEUE;
32 tcb_desc->bCmdOrInit = DESC_PACKET_TYPE_NORMAL;
33 tcb_desc->bLastIniPkt = 0;
34 skb_reserve(skb, USB_HWDESC_HEADER_LEN);
35 ptr_buf = skb_put(skb, DataLen);
36 memset(ptr_buf, 0, DataLen);
37 memcpy(ptr_buf, pData, DataLen);
38 tcb_desc->txbuf_size = (u16)DataLen;
40 if (!priv->ieee80211->check_nic_enough_desc(dev, tcb_desc->queue_index) ||
41 (!skb_queue_empty(&priv->ieee80211->skb_waitQ[tcb_desc->queue_index])) ||
42 (priv->ieee80211->queue_stop)) {
43 RT_TRACE(COMP_FIRMWARE, "NULL packet => tx full\n");
44 skb_queue_tail(&priv->ieee80211->skb_waitQ[tcb_desc->queue_index], skb);
46 priv->ieee80211->softmac_hard_start_xmit(skb, dev);
49 //PlatformReleaseSpinLock(Adapter, RT_TX_SPINLOCK);
54 * Function: cmpk_message_handle_tx()
56 * Overview: Driver internal module can call the API to send message to
57 * firmware side. For example, you can send a debug command packet.
58 * Or you can send a request for FW to modify RLX4181 LBUS HW bank.
59 * Otherwise, you can change MAC/PHT/RF register by firmware at
60 * run time. We do not support message more than one segment now.
68 extern bool cmpk_message_handle_tx(
69 struct net_device *dev,
70 u8 *codevirtualaddress,
74 bool rt_status = true;
79 * Function: cmpk_counttxstatistic()
82 cmpk_count_txstatistic(struct net_device *dev, cmpk_txfb_t *pstx_fb)
84 struct r8192_priv *priv = ieee80211_priv(dev);
86 RT_RF_POWER_STATE rtState;
88 pAdapter->HalFunc.GetHwRegHandler(pAdapter,
93 * When RF is off, we should not count the packet for hw/sw synchronize
94 * reason, ie. there may be a duration while sw switch is changed and hw
95 * switch is being changed.
97 if (rtState == eRfOff)
102 if (pAdapter->bInHctTest)
106 * We can not know the packet length and transmit type:
107 * broadcast or uni or multicast.
108 * So the relative statistics must be collected in tx feedback info
111 priv->stats.txfeedbackok++;
112 priv->stats.txoktotal++;
113 priv->stats.txokbytestotal += pstx_fb->pkt_length;
114 priv->stats.txokinperiod++;
115 /* We can not make sure broadcast/multicast or unicast mode. */
116 if (pstx_fb->pkt_type == PACKET_MULTICAST) {
117 priv->stats.txmulticast++;
118 priv->stats.txbytesmulticast += pstx_fb->pkt_length;
119 } else if (pstx_fb->pkt_type == PACKET_BROADCAST) {
120 priv->stats.txbroadcast++;
121 priv->stats.txbytesbroadcast += pstx_fb->pkt_length;
123 priv->stats.txunicast++;
124 priv->stats.txbytesunicast += pstx_fb->pkt_length;
127 priv->stats.txfeedbackfail++;
128 priv->stats.txerrtotal++;
129 priv->stats.txerrbytestotal += pstx_fb->pkt_length;
130 /* We can not make sure broadcast/multicast or unicast mode. */
131 if (pstx_fb->pkt_type == PACKET_MULTICAST)
132 priv->stats.txerrmulticast++;
133 else if (pstx_fb->pkt_type == PACKET_BROADCAST)
134 priv->stats.txerrbroadcast++;
136 priv->stats.txerrunicast++;
138 priv->stats.txretrycount += pstx_fb->retry_cnt;
139 priv->stats.txfeedbackretry += pstx_fb->retry_cnt;
143 * Function: cmpk_handle_tx_feedback()
145 * Overview: The function is responsible for extract the message inside TX
146 * feedbck message from firmware. It will contain dedicated info in
147 * ws-06-0063-rtl8190-command-packet-specification. Please
148 * refer to chapter "TX Feedback Element". We have to read 20 bytes
149 * in the command packet.
151 * Input: struct net_device * dev
152 * u8 *pmsg - Msg Ptr of the command packet.
158 static void cmpk_handle_tx_feedback(struct net_device *dev, u8 *pmsg)
160 struct r8192_priv *priv = ieee80211_priv(dev);
161 cmpk_txfb_t rx_tx_fb;
163 priv->stats.txfeedback++;
165 /* 1. Extract TX feedback info from RFD to temp structure buffer. */
166 memcpy((u8 *)&rx_tx_fb, pmsg, sizeof(cmpk_txfb_t));
168 /* 2. Use tx feedback info to count TX statistics. */
169 cmpk_count_txstatistic(dev, &rx_tx_fb);
172 void cmdpkt_beacontimerinterrupt_819xusb(struct net_device *dev)
174 struct r8192_priv *priv = ieee80211_priv(dev);
177 if (priv->ieee80211->current_network.mode == IEEE_A ||
178 priv->ieee80211->current_network.mode == IEEE_N_5G ||
179 (priv->ieee80211->current_network.mode == IEEE_N_24G &&
180 (!priv->ieee80211->pHTInfo->bCurSuppCCK))) {
182 DMESG("send beacon frame tx rate is 6Mbpm\n");
185 DMESG("send beacon frame tx rate is 1Mbpm\n");
187 rtl819xusb_beacon_tx(dev, tx_rate); /* HW Beacon */
191 * Function: cmpk_handle_interrupt_status()
193 * Overview: The function is responsible for extract the message from
194 * firmware. It will contain dedicated info in
195 * ws-07-0063-v06-rtl819x-command-packet-specification-070315.doc.
196 * Please refer to chapter "Interrupt Status Element".
198 * Input: struct net_device *dev,
199 * u8* pmsg - Message Pointer of the command packet.
205 static void cmpk_handle_interrupt_status(struct net_device *dev, u8 *pmsg)
207 cmpk_intr_sta_t rx_intr_status; /* */
208 struct r8192_priv *priv = ieee80211_priv(dev);
210 DMESG("---> cmpk_Handle_Interrupt_Status()\n");
212 /* 1. Extract TX feedback info from RFD to temp structure buffer. */
213 rx_intr_status.length = pmsg[1];
214 if (rx_intr_status.length != (sizeof(cmpk_intr_sta_t) - 2)) {
215 DMESG("cmpk_Handle_Interrupt_Status: wrong length!\n");
218 /* Statistics of beacon for ad-hoc mode. */
219 if (priv->ieee80211->iw_mode == IW_MODE_ADHOC) {
220 //2 maybe need endian transform?
221 rx_intr_status.interrupt_status = *((u32 *)(pmsg + 4));
222 //rx_intr_status.InterruptStatus = N2H4BYTE(*((UINT32 *)(pMsg + 4)));
224 DMESG("interrupt status = 0x%x\n", rx_intr_status.interrupt_status);
226 if (rx_intr_status.interrupt_status & ISR_TxBcnOk) {
227 priv->ieee80211->bibsscoordinator = true;
228 priv->stats.txbeaconokint++;
229 } else if (rx_intr_status.interrupt_status & ISR_TxBcnErr) {
230 priv->ieee80211->bibsscoordinator = false;
231 priv->stats.txbeaconerr++;
234 if (rx_intr_status.interrupt_status & ISR_BcnTimerIntr)
235 cmdpkt_beacontimerinterrupt_819xusb(dev);
237 /* Other informations in interrupt status we need? */
238 DMESG("<---- cmpk_handle_interrupt_status()\n");
242 * Function: cmpk_handle_query_config_rx()
244 * Overview: The function is responsible for extract the message from
245 * firmware. It will contain dedicated info in
246 * ws-06-0063-rtl8190-command-packet-specification
247 * Please refer to chapter "Beacon State Element".
249 * Input: u8 * pmsg - Message Pointer of the command packet.
256 static void cmpk_handle_query_config_rx(struct net_device *dev, u8 *pmsg)
258 cmpk_query_cfg_t rx_query_cfg;
260 * Extract TX feedback info from RFD to temp structure buffer.
262 rx_query_cfg.cfg_action = (pmsg[4] & 0x80000000) >> 31;
263 rx_query_cfg.cfg_type = (pmsg[4] & 0x60) >> 5;
264 rx_query_cfg.cfg_size = (pmsg[4] & 0x18) >> 3;
265 rx_query_cfg.cfg_page = (pmsg[6] & 0x0F) >> 0;
266 rx_query_cfg.cfg_offset = pmsg[7];
267 rx_query_cfg.value = (pmsg[8] << 24) | (pmsg[9] << 16) |
268 (pmsg[10] << 8) | (pmsg[11] << 0);
269 rx_query_cfg.mask = (pmsg[12] << 24) | (pmsg[13] << 16) |
270 (pmsg[14] << 8) | (pmsg[15] << 0);
274 * Function: cmpk_count_tx_status()
276 * Overview: Count aggregated tx status from firmware of one type rx command
277 * packet element id = RX_TX_STATUS.
285 static void cmpk_count_tx_status(struct net_device *dev,
286 cmpk_tx_status_t *pstx_status)
288 struct r8192_priv *priv = ieee80211_priv(dev);
292 RT_RF_POWER_STATE rtstate;
294 pAdapter->HalFunc.GetHwRegHandler(pAdapter, HW_VAR_RF_STATE, (pu1Byte)(&rtState));
297 * When RF is off, we should not count the packet for hw/sw synchronize
298 * reason, ie. there may be a duration while sw switch is changed and hw
299 * switch is being changed.
301 if (rtState == eRfOff)
305 priv->stats.txfeedbackok += pstx_status->txok;
306 priv->stats.txoktotal += pstx_status->txok;
308 priv->stats.txfeedbackfail += pstx_status->txfail;
309 priv->stats.txerrtotal += pstx_status->txfail;
311 priv->stats.txretrycount += pstx_status->txretry;
312 priv->stats.txfeedbackretry += pstx_status->txretry;
314 priv->stats.txmulticast += pstx_status->txmcok;
315 priv->stats.txbroadcast += pstx_status->txbcok;
316 priv->stats.txunicast += pstx_status->txucok;
318 priv->stats.txerrmulticast += pstx_status->txmcfail;
319 priv->stats.txerrbroadcast += pstx_status->txbcfail;
320 priv->stats.txerrunicast += pstx_status->txucfail;
322 priv->stats.txbytesmulticast += pstx_status->txmclength;
323 priv->stats.txbytesbroadcast += pstx_status->txbclength;
324 priv->stats.txbytesunicast += pstx_status->txuclength;
326 priv->stats.last_packet_rate = pstx_status->rate;
330 * Function: cmpk_handle_tx_status()
332 * Overview: Firmware add a new tx feedback status to reduce rx command
333 * packet buffer operation load.
342 cmpk_handle_tx_status(struct net_device *dev, u8 *pmsg)
344 cmpk_tx_status_t rx_tx_sts;
346 memcpy((void *)&rx_tx_sts, (void *)pmsg, sizeof(cmpk_tx_status_t));
347 /* 2. Use tx feedback info to count TX statistics. */
348 cmpk_count_tx_status(dev, &rx_tx_sts);
352 * Function: cmpk_handle_tx_rate_history()
354 * Overview: Firmware add a new tx rate history
362 static void cmpk_handle_tx_rate_history(struct net_device *dev, u8 *pmsg)
364 cmpk_tx_rahis_t *ptxrate;
366 u16 length = sizeof(cmpk_tx_rahis_t);
368 struct r8192_priv *priv = ieee80211_priv(dev);
371 pAdapter->HalFunc.GetHwRegHandler(pAdapter,
373 (pu1Byte)(&rtState));
375 * When RF is off, we should not count the packet for hw/sw synchronize
376 * reason, ie. there may be a duration while sw switch is changed and hw
377 * switch is being changed.
379 if (rtState == eRfOff)
385 * Do endian transfer to word alignment(16 bits) for windows system.
386 * You must do different endian transfer for linux and MAC OS
388 for (i = 0; i < (length/4); i++) {
390 temp1 = ptemp[i] & 0x0000FFFF;
391 temp2 = ptemp[i] >> 16;
392 ptemp[i] = (temp1 << 16) | temp2;
395 ptxrate = (cmpk_tx_rahis_t *)pmsg;
400 for (i = 0; i < 16; i++) {
401 /* Collect CCK rate packet num */
403 priv->stats.txrate.cck[i] += ptxrate->cck[i];
404 /* Collect OFDM rate packet num */
406 priv->stats.txrate.ofdm[i] += ptxrate->ofdm[i];
407 for (j = 0; j < 4; j++)
408 priv->stats.txrate.ht_mcs[j][i] += ptxrate->ht_mcs[j][i];
414 * Function: cmpk_message_handle_rx()
416 * Overview: In the function, we will capture different RX command packet
417 * info. Every RX command packet element has different message
418 * length and meaning in content. We only support three type of RX
419 * command packet now. Please refer to document
420 * ws-06-0063-rtl8190-command-packet-specification.
429 cmpk_message_handle_rx(
430 struct net_device *dev,
431 struct ieee80211_rx_stats *pstats)
433 struct r8192_priv *priv = ieee80211_priv(dev);
435 u8 cmd_length, exe_cnt = 0;
440 * 0. Check input arguments.
441 * If is is a command queue message or pointer is null
443 if ((pstats == NULL))
444 return 0; /* This is not a command packet. */
446 /* 1. Read received command packet message length from RFD. */
447 total_length = pstats->Length;
449 /* 2. Read virtual address from RFD. */
450 pcmd_buff = pstats->virtual_address;
452 /* 3. Read command pakcet element id and length. */
453 element_id = pcmd_buff[0];
456 * 4. Check every received command packet conent according to different
457 * element type. Because FW may aggregate RX command packet to minimize
458 * transmit time between DRV and FW.
461 /* Add a counter to prevent to locked in the loop too long */
462 while (total_length > 0 || exe_cnt++ > 100) {
463 /* We support aggregation of different cmd in the same packet */
464 element_id = pcmd_buff[0];
465 switch (element_id) {
467 cmpk_handle_tx_feedback(dev, pcmd_buff);
468 cmd_length = CMPK_RX_TX_FB_SIZE;
470 case RX_INTERRUPT_STATUS:
471 cmpk_handle_interrupt_status(dev, pcmd_buff);
472 cmd_length = sizeof(cmpk_intr_sta_t);
474 case BOTH_QUERY_CONFIG:
475 cmpk_handle_query_config_rx(dev, pcmd_buff);
476 cmd_length = CMPK_BOTH_QUERY_CONFIG_SIZE;
479 cmpk_handle_tx_status(dev, pcmd_buff);
480 cmd_length = CMPK_RX_TX_STS_SIZE;
482 case RX_TX_PER_PKT_FEEDBACK:
483 cmd_length = CMPK_RX_TX_FB_SIZE;
485 case RX_TX_RATE_HISTORY:
486 cmpk_handle_tx_rate_history(dev, pcmd_buff);
487 cmd_length = CMPK_TX_RAHIS_SIZE;
490 RT_TRACE(COMP_ERR, "(%s): unknown CMD Element\n",
492 return 1; /* This is a command packet. */
494 priv->stats.rxcmdpkt[element_id]++;
495 total_length -= cmd_length;
496 pcmd_buff += cmd_length;
498 return 1; /* This is a command packet. */