Merge branch 'for-linus' of git://www.jni.nu/cris
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / rtl8192su / r819xU_cmdpkt.c
CommitLineData
a6f81aaf
FS
1/******************************************************************************
2 * Copyright(c) 2008 - 2010 Realtek Corporation. All rights reserved.
3 * Linux device driver for RTL8192U
8dba599d 4 *
a6f81aaf
FS
5 * This program is distributed in the hope that it will be useful, but WITHOUT
6 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
8 * more details.
8dba599d 9 *
a6f81aaf
FS
10 * You should have received a copy of the GNU General Public License along with
11 * this program; if not, write to the Free Software Foundation, Inc.,
12 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
13 *
14 * The full GNU General Public License is included in this distribution in the
15 * file called LICENSE.
16 *
17 * Contact Information:
18 * wlanfae <wlanfae@realtek.com>
19******************************************************************************/
5f53d8ca
JC
20#include "r8192U.h"
21#include "r819xU_cmdpkt.h"
8dba599d
FS
22
23bool SendTxCommandPacket(struct net_device *dev, void *pData, u32 DataLen)
5f53d8ca
JC
24{
25 bool rtStatus = true;
26 struct r8192_priv *priv = ieee80211_priv(dev);
27 struct sk_buff *skb;
28 cb_desc *tcb_desc;
29 unsigned char *ptr_buf;
5f53d8ca 30
8dba599d
FS
31 /*
32 * Get TCB and local buffer from common pool.
33 * (It is shared by CmdQ, MgntQ, and USB coalesce DataQ)
34 */
5f53d8ca 35 skb = dev_alloc_skb(USB_HWDESC_HEADER_LEN + DataLen + 4);
a6f81aaf
FS
36 if (!skb)
37 return false;
8dba599d
FS
38 memcpy((unsigned char *)(skb->cb), &dev, sizeof(dev));
39 tcb_desc = (cb_desc *)(skb->cb + MAX_DEV_ADDR_SIZE);
5f53d8ca
JC
40 tcb_desc->queue_index = TXCMD_QUEUE;
41 tcb_desc->bCmdOrInit = DESC_PACKET_TYPE_NORMAL;
42 tcb_desc->bLastIniPkt = 0;
43 skb_reserve(skb, USB_HWDESC_HEADER_LEN);
44 ptr_buf = skb_put(skb, DataLen);
8dba599d
FS
45 memcpy(ptr_buf, pData, DataLen);
46 tcb_desc->txbuf_size = (u16)DataLen;
47
48 if (!priv->ieee80211->check_nic_enough_desc(dev, tcb_desc->queue_index) ||
49 (!skb_queue_empty(&priv->ieee80211->skb_waitQ[tcb_desc->queue_index])) ||
50 (priv->ieee80211->queue_stop)) {
51 RT_TRACE(COMP_FIRMWARE, "NULL packet => tx full\n");
5f53d8ca
JC
52 skb_queue_tail(&priv->ieee80211->skb_waitQ[tcb_desc->queue_index], skb);
53 } else {
8dba599d 54 priv->ieee80211->softmac_hard_start_xmit(skb, dev);
5f53d8ca
JC
55 }
56
5f53d8ca
JC
57 return rtStatus;
58}
59
8dba599d 60/*
5f53d8ca
JC
61 * Function: cmpk_message_handle_tx()
62 *
63 * Overview: Driver internal module can call the API to send message to
8dba599d
FS
64 * firmware side. For example, you can send a debug command packet.
65 * Or you can send a request for FW to modify RLX4181 LBUS HW bank.
66 * Otherwise, you can change MAC/PHT/RF register by firmware at
67 * run time. We do not support message more than one segment now.
5f53d8ca
JC
68 *
69 * Input: NONE
70 *
71 * Output: NONE
72 *
73 * Return: NONE
8dba599d 74 */
5f53d8ca
JC
75 extern bool cmpk_message_handle_tx(
76 struct net_device *dev,
8dba599d 77 u8 *codevirtualaddress,
5f53d8ca
JC
78 u32 packettype,
79 u32 buffer_len)
80{
8dba599d 81 bool rt_status = true;
5f53d8ca 82 return rt_status;
8dba599d 83}
5f53d8ca 84
8dba599d
FS
85/*
86 * Function: cmpk_counttxstatistic()
87 */
5f53d8ca 88static void
8dba599d 89cmpk_count_txstatistic(struct net_device *dev, cmpk_txfb_t *pstx_fb)
5f53d8ca
JC
90{
91 struct r8192_priv *priv = ieee80211_priv(dev);
92#ifdef ENABLE_PS
8dba599d 93 RT_RF_POWER_STATE rtState;
5f53d8ca 94
8dba599d
FS
95 pAdapter->HalFunc.GetHwRegHandler(pAdapter,
96 HW_VAR_RF_STATE,
97 (pu1Byte)(&rtState));
5f53d8ca 98
8dba599d
FS
99 /*
100 * When RF is off, we should not count the packet for hw/sw synchronize
101 * reason, ie. there may be a duration while sw switch is changed and hw
102 * switch is being changed.
103 */
5f53d8ca 104 if (rtState == eRfOff)
5f53d8ca 105 return;
5f53d8ca
JC
106#endif
107
108#ifdef TODO
8dba599d 109 if (pAdapter->bInHctTest)
5f53d8ca
JC
110 return;
111#endif
8dba599d
FS
112 /*
113 * We can not know the packet length and transmit type:
114 * broadcast or uni or multicast.
115 * So the relative statistics must be collected in tx feedback info
116 */
117 if (pstx_fb->tok) {
5f53d8ca
JC
118 priv->stats.txfeedbackok++;
119 priv->stats.txoktotal++;
120 priv->stats.txokbytestotal += pstx_fb->pkt_length;
121 priv->stats.txokinperiod++;
5f53d8ca 122 /* We can not make sure broadcast/multicast or unicast mode. */
8dba599d 123 if (pstx_fb->pkt_type == PACKET_MULTICAST) {
5f53d8ca
JC
124 priv->stats.txmulticast++;
125 priv->stats.txbytesmulticast += pstx_fb->pkt_length;
8dba599d 126 } else if (pstx_fb->pkt_type == PACKET_BROADCAST) {
5f53d8ca
JC
127 priv->stats.txbroadcast++;
128 priv->stats.txbytesbroadcast += pstx_fb->pkt_length;
8dba599d 129 } else {
5f53d8ca
JC
130 priv->stats.txunicast++;
131 priv->stats.txbytesunicast += pstx_fb->pkt_length;
132 }
8dba599d 133 } else {
5f53d8ca
JC
134 priv->stats.txfeedbackfail++;
135 priv->stats.txerrtotal++;
136 priv->stats.txerrbytestotal += pstx_fb->pkt_length;
5f53d8ca
JC
137 /* We can not make sure broadcast/multicast or unicast mode. */
138 if (pstx_fb->pkt_type == PACKET_MULTICAST)
5f53d8ca 139 priv->stats.txerrmulticast++;
5f53d8ca 140 else if (pstx_fb->pkt_type == PACKET_BROADCAST)
5f53d8ca 141 priv->stats.txerrbroadcast++;
5f53d8ca 142 else
5f53d8ca 143 priv->stats.txerrunicast++;
5f53d8ca 144 }
5f53d8ca
JC
145 priv->stats.txretrycount += pstx_fb->retry_cnt;
146 priv->stats.txfeedbackretry += pstx_fb->retry_cnt;
8dba599d 147}
5f53d8ca 148
8dba599d 149/*
5f53d8ca
JC
150 * Function: cmpk_handle_tx_feedback()
151 *
152 * Overview: The function is responsible for extract the message inside TX
8dba599d
FS
153 * feedbck message from firmware. It will contain dedicated info in
154 * ws-06-0063-rtl8190-command-packet-specification. Please
155 * refer to chapter "TX Feedback Element". We have to read 20 bytes
156 * in the command packet.
5f53d8ca
JC
157 *
158 * Input: struct net_device * dev
8dba599d 159 * u8 *pmsg - Msg Ptr of the command packet.
5f53d8ca
JC
160 *
161 * Output: NONE
162 *
163 * Return: NONE
8dba599d
FS
164 */
165static void cmpk_handle_tx_feedback(struct net_device *dev, u8 *pmsg)
5f53d8ca
JC
166{
167 struct r8192_priv *priv = ieee80211_priv(dev);
8dba599d 168 cmpk_txfb_t rx_tx_fb;
5f53d8ca
JC
169
170 priv->stats.txfeedback++;
171
5f53d8ca 172 /* 1. Extract TX feedback info from RFD to temp structure buffer. */
8dba599d 173 memcpy((u8 *)&rx_tx_fb, pmsg, sizeof(cmpk_txfb_t));
35c1b462 174
5f53d8ca
JC
175 /* 2. Use tx feedback info to count TX statistics. */
176 cmpk_count_txstatistic(dev, &rx_tx_fb);
8dba599d 177}
35c1b462 178
8dba599d 179void cmdpkt_beacontimerinterrupt_819xusb(struct net_device *dev)
5f53d8ca
JC
180{
181 struct r8192_priv *priv = ieee80211_priv(dev);
182 u16 tx_rate;
5f53d8ca 183
8dba599d
FS
184 if (priv->ieee80211->current_network.mode == IEEE_A ||
185 priv->ieee80211->current_network.mode == IEEE_N_5G ||
186 (priv->ieee80211->current_network.mode == IEEE_N_24G &&
187 (!priv->ieee80211->pHTInfo->bCurSuppCCK))) {
188 tx_rate = 60;
189 DMESG("send beacon frame tx rate is 6Mbpm\n");
190 } else {
191 tx_rate = 10;
192 DMESG("send beacon frame tx rate is 1Mbpm\n");
5f53d8ca 193 }
8dba599d 194 rtl819xusb_beacon_tx(dev, tx_rate); /* HW Beacon */
5f53d8ca
JC
195}
196
8dba599d 197/*
5f53d8ca
JC
198 * Function: cmpk_handle_interrupt_status()
199 *
200 * Overview: The function is responsible for extract the message from
8dba599d
FS
201 * firmware. It will contain dedicated info in
202 * ws-07-0063-v06-rtl819x-command-packet-specification-070315.doc.
203 * Please refer to chapter "Interrupt Status Element".
5f53d8ca
JC
204 *
205 * Input: struct net_device *dev,
8dba599d 206 * u8* pmsg - Message Pointer of the command packet.
5f53d8ca
JC
207 *
208 * Output: NONE
209 *
210 * Return: NONE
8dba599d
FS
211 */
212static void cmpk_handle_interrupt_status(struct net_device *dev, u8 *pmsg)
5f53d8ca
JC
213{
214 cmpk_intr_sta_t rx_intr_status; /* */
215 struct r8192_priv *priv = ieee80211_priv(dev);
216
217 DMESG("---> cmpk_Handle_Interrupt_Status()\n");
218
5f53d8ca 219 /* 1. Extract TX feedback info from RFD to temp structure buffer. */
5f53d8ca 220 rx_intr_status.length = pmsg[1];
8dba599d 221 if (rx_intr_status.length != (sizeof(cmpk_intr_sta_t) - 2)) {
5f53d8ca
JC
222 DMESG("cmpk_Handle_Interrupt_Status: wrong length!\n");
223 return;
224 }
8dba599d
FS
225 /* Statistics of beacon for ad-hoc mode. */
226 if (priv->ieee80211->iw_mode == IW_MODE_ADHOC) {
5f53d8ca
JC
227 //2 maybe need endian transform?
228 rx_intr_status.interrupt_status = *((u32 *)(pmsg + 4));
5f53d8ca
JC
229
230 DMESG("interrupt status = 0x%x\n", rx_intr_status.interrupt_status);
231
8dba599d 232 if (rx_intr_status.interrupt_status & ISR_TxBcnOk) {
5f53d8ca
JC
233 priv->ieee80211->bibsscoordinator = true;
234 priv->stats.txbeaconokint++;
8dba599d 235 } else if (rx_intr_status.interrupt_status & ISR_TxBcnErr) {
5f53d8ca
JC
236 priv->ieee80211->bibsscoordinator = false;
237 priv->stats.txbeaconerr++;
238 }
239
240 if (rx_intr_status.interrupt_status & ISR_BcnTimerIntr)
5f53d8ca 241 cmdpkt_beacontimerinterrupt_819xusb(dev);
5f53d8ca 242 }
8dba599d 243 /* Other informations in interrupt status we need? */
5f53d8ca 244 DMESG("<---- cmpk_handle_interrupt_status()\n");
8dba599d 245}
5f53d8ca 246
8dba599d 247/*
5f53d8ca
JC
248 * Function: cmpk_handle_query_config_rx()
249 *
250 * Overview: The function is responsible for extract the message from
251 * firmware. It will contain dedicated info in
8dba599d
FS
252 * ws-06-0063-rtl8190-command-packet-specification
253 * Please refer to chapter "Beacon State Element".
5f53d8ca
JC
254 *
255 * Input: u8 * pmsg - Message Pointer of the command packet.
256 *
257 * Output: NONE
258 *
259 * Return: NONE
260 *
8dba599d
FS
261 */
262static void cmpk_handle_query_config_rx(struct net_device *dev, u8 *pmsg)
5f53d8ca 263{
8dba599d
FS
264 cmpk_query_cfg_t rx_query_cfg;
265 /*
266 * Extract TX feedback info from RFD to temp structure buffer.
267 */
268 rx_query_cfg.cfg_action = (pmsg[4] & 0x80000000) >> 31;
269 rx_query_cfg.cfg_type = (pmsg[4] & 0x60) >> 5;
270 rx_query_cfg.cfg_size = (pmsg[4] & 0x18) >> 3;
271 rx_query_cfg.cfg_page = (pmsg[6] & 0x0F) >> 0;
272 rx_query_cfg.cfg_offset = pmsg[7];
273 rx_query_cfg.value = (pmsg[8] << 24) | (pmsg[9] << 16) |
274 (pmsg[10] << 8) | (pmsg[11] << 0);
275 rx_query_cfg.mask = (pmsg[12] << 24) | (pmsg[13] << 16) |
276 (pmsg[14] << 8) | (pmsg[15] << 0);
277}
5f53d8ca 278
8dba599d 279/*
5f53d8ca
JC
280 * Function: cmpk_count_tx_status()
281 *
8dba599d 282 * Overview: Count aggregated tx status from firmware of one type rx command
5f53d8ca
JC
283 * packet element id = RX_TX_STATUS.
284 *
285 * Input: NONE
286 *
287 * Output: NONE
288 *
289 * Return: NONE
8dba599d
FS
290 */
291static void cmpk_count_tx_status(struct net_device *dev,
292 cmpk_tx_status_t *pstx_status)
5f53d8ca
JC
293{
294 struct r8192_priv *priv = ieee80211_priv(dev);
295
296#ifdef ENABLE_PS
297
298 RT_RF_POWER_STATE rtstate;
299
300 pAdapter->HalFunc.GetHwRegHandler(pAdapter, HW_VAR_RF_STATE, (pu1Byte)(&rtState));
301
8dba599d
FS
302 /*
303 * When RF is off, we should not count the packet for hw/sw synchronize
304 * reason, ie. there may be a duration while sw switch is changed and hw
305 * switch is being changed.
306 */
5f53d8ca 307 if (rtState == eRfOff)
5f53d8ca 308 return;
5f53d8ca
JC
309#endif
310
311 priv->stats.txfeedbackok += pstx_status->txok;
312 priv->stats.txoktotal += pstx_status->txok;
313
314 priv->stats.txfeedbackfail += pstx_status->txfail;
315 priv->stats.txerrtotal += pstx_status->txfail;
316
8dba599d 317 priv->stats.txretrycount += pstx_status->txretry;
5f53d8ca
JC
318 priv->stats.txfeedbackretry += pstx_status->txretry;
319
8dba599d
FS
320 priv->stats.txmulticast += pstx_status->txmcok;
321 priv->stats.txbroadcast += pstx_status->txbcok;
5f53d8ca
JC
322 priv->stats.txunicast += pstx_status->txucok;
323
324 priv->stats.txerrmulticast += pstx_status->txmcfail;
325 priv->stats.txerrbroadcast += pstx_status->txbcfail;
326 priv->stats.txerrunicast += pstx_status->txucfail;
327
328 priv->stats.txbytesmulticast += pstx_status->txmclength;
329 priv->stats.txbytesbroadcast += pstx_status->txbclength;
8dba599d 330 priv->stats.txbytesunicast += pstx_status->txuclength;
5f53d8ca 331
8dba599d
FS
332 priv->stats.last_packet_rate = pstx_status->rate;
333}
5f53d8ca 334
8dba599d 335/*
5f53d8ca
JC
336 * Function: cmpk_handle_tx_status()
337 *
338 * Overview: Firmware add a new tx feedback status to reduce rx command
339 * packet buffer operation load.
340 *
341 * Input: NONE
342 *
343 * Output: NONE
344 *
345 * Return: NONE
8dba599d 346 */
5f53d8ca 347static void
8dba599d 348cmpk_handle_tx_status(struct net_device *dev, u8 *pmsg)
5f53d8ca 349{
8dba599d 350 cmpk_tx_status_t rx_tx_sts;
5f53d8ca 351
8dba599d 352 memcpy((void *)&rx_tx_sts, (void *)pmsg, sizeof(cmpk_tx_status_t));
5f53d8ca
JC
353 /* 2. Use tx feedback info to count TX statistics. */
354 cmpk_count_tx_status(dev, &rx_tx_sts);
8dba599d 355}
5f53d8ca 356
8dba599d 357/*
5f53d8ca
JC
358 * Function: cmpk_handle_tx_rate_history()
359 *
360 * Overview: Firmware add a new tx rate history
361 *
362 * Input: NONE
363 *
364 * Output: NONE
365 *
366 * Return: NONE
8dba599d
FS
367 */
368static void cmpk_handle_tx_rate_history(struct net_device *dev, u8 *pmsg)
5f53d8ca
JC
369{
370 cmpk_tx_rahis_t *ptxrate;
8dba599d
FS
371 u8 i, j;
372 u16 length = sizeof(cmpk_tx_rahis_t);
373 u32 *ptemp;
5f53d8ca
JC
374 struct r8192_priv *priv = ieee80211_priv(dev);
375
5f53d8ca 376#ifdef ENABLE_PS
8dba599d
FS
377 pAdapter->HalFunc.GetHwRegHandler(pAdapter,
378 HW_VAR_RF_STATE,
379 (pu1Byte)(&rtState));
380 /*
381 * When RF is off, we should not count the packet for hw/sw synchronize
382 * reason, ie. there may be a duration while sw switch is changed and hw
383 * switch is being changed.
384 */
5f53d8ca 385 if (rtState == eRfOff)
5f53d8ca 386 return;
5f53d8ca 387#endif
5f53d8ca
JC
388 ptemp = (u32 *)pmsg;
389
8dba599d
FS
390 /*
391 * Do endian transfer to word alignment(16 bits) for windows system.
392 * You must do different endian transfer for linux and MAC OS
393 */
394 for (i = 0; i < (length/4); i++) {
395 u16 temp1, temp2;
396 temp1 = ptemp[i] & 0x0000FFFF;
397 temp2 = ptemp[i] >> 16;
398 ptemp[i] = (temp1 << 16) | temp2;
5f53d8ca
JC
399 }
400
401 ptxrate = (cmpk_tx_rahis_t *)pmsg;
402
8dba599d 403 if (ptxrate == NULL)
5f53d8ca 404 return;
5f53d8ca 405
8dba599d
FS
406 for (i = 0; i < 16; i++) {
407 /* Collect CCK rate packet num */
5f53d8ca
JC
408 if (i < 4)
409 priv->stats.txrate.cck[i] += ptxrate->cck[i];
8dba599d
FS
410 /* Collect OFDM rate packet num */
411 if (i < 8)
5f53d8ca 412 priv->stats.txrate.ofdm[i] += ptxrate->ofdm[i];
5f53d8ca
JC
413 for (j = 0; j < 4; j++)
414 priv->stats.txrate.ht_mcs[j][i] += ptxrate->ht_mcs[j][i];
415 }
416
8dba599d 417}
5f53d8ca 418
8dba599d 419/*
5f53d8ca
JC
420 * Function: cmpk_message_handle_rx()
421 *
422 * Overview: In the function, we will capture different RX command packet
8dba599d
FS
423 * info. Every RX command packet element has different message
424 * length and meaning in content. We only support three type of RX
425 * command packet now. Please refer to document
426 * ws-06-0063-rtl8190-command-packet-specification.
5f53d8ca
JC
427 *
428 * Input: NONE
429 *
430 * Output: NONE
431 *
432 * Return: NONE
8dba599d 433 */
5f53d8ca
JC
434extern u32
435cmpk_message_handle_rx(
436 struct net_device *dev,
437 struct ieee80211_rx_stats *pstats)
438{
5f53d8ca
JC
439 struct r8192_priv *priv = ieee80211_priv(dev);
440 int total_length;
441 u8 cmd_length, exe_cnt = 0;
442 u8 element_id;
443 u8 *pcmd_buff;
444
8dba599d
FS
445 /*
446 * 0. Check input arguments.
447 * If is is a command queue message or pointer is null
448 */
449 if ((pstats == NULL))
5f53d8ca 450 return 0; /* This is not a command packet. */
5f53d8ca
JC
451
452 /* 1. Read received command packet message length from RFD. */
453 total_length = pstats->Length;
454
455 /* 2. Read virtual address from RFD. */
456 pcmd_buff = pstats->virtual_address;
457
458 /* 3. Read command pakcet element id and length. */
459 element_id = pcmd_buff[0];
5f53d8ca 460
8dba599d
FS
461 /*
462 * 4. Check every received command packet conent according to different
463 * element type. Because FW may aggregate RX command packet to minimize
464 * transmit time between DRV and FW.
465 */
5f53d8ca 466
8dba599d
FS
467 /* Add a counter to prevent to locked in the loop too long */
468 while (total_length > 0 || exe_cnt++ > 100) {
469 /* We support aggregation of different cmd in the same packet */
470 element_id = pcmd_buff[0];
471 switch (element_id) {
472 case RX_TX_FEEDBACK:
473 cmpk_handle_tx_feedback(dev, pcmd_buff);
474 cmd_length = CMPK_RX_TX_FB_SIZE;
475 break;
476 case RX_INTERRUPT_STATUS:
477 cmpk_handle_interrupt_status(dev, pcmd_buff);
478 cmd_length = sizeof(cmpk_intr_sta_t);
479 break;
480 case BOTH_QUERY_CONFIG:
481 cmpk_handle_query_config_rx(dev, pcmd_buff);
482 cmd_length = CMPK_BOTH_QUERY_CONFIG_SIZE;
483 break;
484 case RX_TX_STATUS:
485 cmpk_handle_tx_status(dev, pcmd_buff);
486 cmd_length = CMPK_RX_TX_STS_SIZE;
487 break;
488 case RX_TX_PER_PKT_FEEDBACK:
489 cmd_length = CMPK_RX_TX_FB_SIZE;
490 break;
491 case RX_TX_RATE_HISTORY:
492 cmpk_handle_tx_rate_history(dev, pcmd_buff);
493 cmd_length = CMPK_TX_RAHIS_SIZE;
494 break;
a6f81aaf
FS
495 case RX_TX_TSSI_MEAN_BACK:
496 {
497 u32 *pMsg;
498 pMsg = (u32 *)pcmd_buff;
499 }
500 cmd_length = 32;
501 break;
8dba599d
FS
502 default:
503 RT_TRACE(COMP_ERR, "(%s): unknown CMD Element\n",
504 __func__);
505 return 1; /* This is a command packet. */
506 }
5f53d8ca 507 priv->stats.rxcmdpkt[element_id]++;
5f53d8ca
JC
508 total_length -= cmd_length;
509 pcmd_buff += cmd_length;
8dba599d
FS
510 }
511 return 1; /* This is a command packet. */
512}