9b377bae47b407213daada78a06d52200e90e656
[safe/jmp/linux-2.6] / drivers / staging / rtl8192su / r819xU_cmdpkt.c
1 /*
2  * (c) Copyright 2008, RealTEK Technologies Inc. All Rights Reserved.
3  *
4  * Module: r819xusb_cmdpkt.c
5  * (RTL8190 TX/RX command packet handler Source C File)
6  *
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.
10  */
11 #include "r8192U.h"
12 #include "r819xU_cmdpkt.h"
13
14 bool SendTxCommandPacket(struct net_device *dev, void *pData, u32 DataLen)
15 {
16         bool    rtStatus = true;
17         struct r8192_priv   *priv = ieee80211_priv(dev);
18         struct sk_buff      *skb;
19         cb_desc             *tcb_desc;
20         unsigned char       *ptr_buf;
21
22         /* PlatformAcquireSpinLock(Adapter, RT_TX_SPINLOCK); */
23
24         /*
25          * Get TCB and local buffer from common pool.
26          * (It is shared by CmdQ, MgntQ, and USB coalesce DataQ)
27          */
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;
39
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);
45                 } else {
46                         priv->ieee80211->softmac_hard_start_xmit(skb, dev);
47                 }
48
49         //PlatformReleaseSpinLock(Adapter, RT_TX_SPINLOCK);
50         return rtStatus;
51 }
52
53 /*
54  * Function:    cmpk_message_handle_tx()
55  *
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.
61  *
62  * Input:               NONE
63  *
64  * Output:              NONE
65  *
66  * Return:              NONE
67  */
68  extern bool    cmpk_message_handle_tx(
69         struct net_device *dev,
70         u8 *codevirtualaddress,
71         u32     packettype,
72         u32     buffer_len)
73 {
74         bool rt_status = true;
75         return rt_status;
76 }
77
78 /*
79  * Function: cmpk_counttxstatistic()
80  */
81 static  void
82 cmpk_count_txstatistic(struct net_device *dev, cmpk_txfb_t *pstx_fb)
83 {
84         struct r8192_priv *priv = ieee80211_priv(dev);
85 #ifdef ENABLE_PS
86         RT_RF_POWER_STATE rtState;
87
88         pAdapter->HalFunc.GetHwRegHandler(pAdapter,
89                                                 HW_VAR_RF_STATE,
90                                                 (pu1Byte)(&rtState));
91
92         /*
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.
96          */
97         if (rtState == eRfOff)
98                 return;
99 #endif
100
101 #ifdef TODO
102         if (pAdapter->bInHctTest)
103                 return;
104 #endif
105         /*
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
109          */
110         if (pstx_fb->tok) {
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;
122                 } else {
123                         priv->stats.txunicast++;
124                         priv->stats.txbytesunicast += pstx_fb->pkt_length;
125                 }
126         } else {
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++;
135                 else
136                         priv->stats.txerrunicast++;
137         }
138         priv->stats.txretrycount += pstx_fb->retry_cnt;
139         priv->stats.txfeedbackretry += pstx_fb->retry_cnt;
140 }
141
142 /*
143  * Function:    cmpk_handle_tx_feedback()
144  *
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.
150  *
151  * Input:       struct net_device *    dev
152  *                              u8 *pmsg - Msg Ptr of the command packet.
153  *
154  * Output:      NONE
155  *
156  * Return:      NONE
157  */
158 static void cmpk_handle_tx_feedback(struct net_device *dev, u8 *pmsg)
159 {
160         struct r8192_priv *priv = ieee80211_priv(dev);
161         cmpk_txfb_t rx_tx_fb;
162
163         priv->stats.txfeedback++;
164
165         /* 1. Extract TX feedback info from RFD to temp structure buffer. */
166         memcpy((u8 *)&rx_tx_fb, pmsg, sizeof(cmpk_txfb_t));
167
168         /* 2. Use tx feedback info to count TX statistics. */
169         cmpk_count_txstatistic(dev, &rx_tx_fb);
170 }
171
172 void cmdpkt_beacontimerinterrupt_819xusb(struct net_device *dev)
173 {
174         struct r8192_priv *priv = ieee80211_priv(dev);
175         u16 tx_rate;
176
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))) {
181                 tx_rate = 60;
182                 DMESG("send beacon frame  tx rate is 6Mbpm\n");
183         } else {
184                 tx_rate = 10;
185                 DMESG("send beacon frame  tx rate is 1Mbpm\n");
186         }
187         rtl819xusb_beacon_tx(dev, tx_rate); /* HW Beacon */
188 }
189
190 /*
191  * Function:    cmpk_handle_interrupt_status()
192  *
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".
197  *
198  * Input:       struct net_device *dev,
199  *                      u8* pmsg - Message Pointer of the command packet.
200  *
201  * Output:      NONE
202  *
203  * Return:      NONE
204  */
205 static void cmpk_handle_interrupt_status(struct net_device *dev, u8 *pmsg)
206 {
207         cmpk_intr_sta_t         rx_intr_status; /* */
208         struct r8192_priv *priv = ieee80211_priv(dev);
209
210         DMESG("---> cmpk_Handle_Interrupt_Status()\n");
211
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");
216                 return;
217         }
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)));
223
224                 DMESG("interrupt status = 0x%x\n", rx_intr_status.interrupt_status);
225
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++;
232                 }
233
234                 if (rx_intr_status.interrupt_status & ISR_BcnTimerIntr)
235                         cmdpkt_beacontimerinterrupt_819xusb(dev);
236         }
237          /* Other informations in interrupt status we need? */
238         DMESG("<---- cmpk_handle_interrupt_status()\n");
239 }
240
241 /*
242  * Function:    cmpk_handle_query_config_rx()
243  *
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".
248  *
249  * Input:       u8 *  pmsg      -       Message Pointer of the command packet.
250  *
251  * Output:      NONE
252  *
253  * Return:      NONE
254  *
255  */
256 static void cmpk_handle_query_config_rx(struct net_device *dev, u8 *pmsg)
257 {
258         cmpk_query_cfg_t rx_query_cfg;
259         /*
260          * Extract TX feedback info from RFD to temp structure buffer.
261          */
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);
271 }
272
273 /*
274  * Function:    cmpk_count_tx_status()
275  *
276  * Overview:    Count aggregated tx status from firmware of one type rx command
277  *                              packet element id = RX_TX_STATUS.
278  *
279  * Input:               NONE
280  *
281  * Output:              NONE
282  *
283  * Return:              NONE
284  */
285 static void cmpk_count_tx_status(struct net_device *dev,
286                                         cmpk_tx_status_t *pstx_status)
287 {
288         struct r8192_priv *priv = ieee80211_priv(dev);
289
290 #ifdef ENABLE_PS
291
292         RT_RF_POWER_STATE       rtstate;
293
294         pAdapter->HalFunc.GetHwRegHandler(pAdapter, HW_VAR_RF_STATE, (pu1Byte)(&rtState));
295
296         /*
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.
300          */
301         if (rtState == eRfOff)
302                 return;
303 #endif
304
305         priv->stats.txfeedbackok        += pstx_status->txok;
306         priv->stats.txoktotal           += pstx_status->txok;
307
308         priv->stats.txfeedbackfail      += pstx_status->txfail;
309         priv->stats.txerrtotal          += pstx_status->txfail;
310
311         priv->stats.txretrycount        += pstx_status->txretry;
312         priv->stats.txfeedbackretry     += pstx_status->txretry;
313
314         priv->stats.txmulticast         += pstx_status->txmcok;
315         priv->stats.txbroadcast         += pstx_status->txbcok;
316         priv->stats.txunicast           += pstx_status->txucok;
317
318         priv->stats.txerrmulticast      += pstx_status->txmcfail;
319         priv->stats.txerrbroadcast      += pstx_status->txbcfail;
320         priv->stats.txerrunicast        += pstx_status->txucfail;
321
322         priv->stats.txbytesmulticast    += pstx_status->txmclength;
323         priv->stats.txbytesbroadcast    += pstx_status->txbclength;
324         priv->stats.txbytesunicast      += pstx_status->txuclength;
325
326         priv->stats.last_packet_rate    = pstx_status->rate;
327 }
328
329 /*
330  * Function:    cmpk_handle_tx_status()
331  *
332  * Overview:    Firmware add a new tx feedback status to reduce rx command
333  *                              packet buffer operation load.
334  *
335  * Input:               NONE
336  *
337  * Output:              NONE
338  *
339  * Return:              NONE
340  */
341 static  void
342 cmpk_handle_tx_status(struct net_device *dev, u8 *pmsg)
343 {
344         cmpk_tx_status_t rx_tx_sts;
345
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);
349 }
350
351 /*
352  * Function:    cmpk_handle_tx_rate_history()
353  *
354  * Overview:    Firmware add a new tx rate history
355  *
356  * Input:               NONE
357  *
358  * Output:              NONE
359  *
360  * Return:              NONE
361  */
362 static void cmpk_handle_tx_rate_history(struct net_device *dev, u8 *pmsg)
363 {
364         cmpk_tx_rahis_t *ptxrate;
365         u8 i, j;
366         u16 length = sizeof(cmpk_tx_rahis_t);
367         u32 *ptemp;
368         struct r8192_priv *priv = ieee80211_priv(dev);
369
370 #ifdef ENABLE_PS
371         pAdapter->HalFunc.GetHwRegHandler(pAdapter,
372                                                 HW_VAR_RF_STATE,
373                                                 (pu1Byte)(&rtState));
374         /*
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.
378          */
379         if (rtState == eRfOff)
380                 return;
381 #endif
382         ptemp = (u32 *)pmsg;
383
384         /*
385          * Do endian transfer to word alignment(16 bits) for windows system.
386          * You must do different endian transfer for linux and MAC OS
387          */
388         for (i = 0; i < (length/4); i++) {
389                 u16 temp1, temp2;
390                 temp1 = ptemp[i] & 0x0000FFFF;
391                 temp2 = ptemp[i] >> 16;
392                 ptemp[i] = (temp1 << 16) | temp2;
393         }
394
395         ptxrate = (cmpk_tx_rahis_t *)pmsg;
396
397         if (ptxrate == NULL)
398                 return;
399
400         for (i = 0; i < 16; i++) {
401                 /* Collect CCK rate packet num */
402                 if (i < 4)
403                         priv->stats.txrate.cck[i] += ptxrate->cck[i];
404                 /* Collect OFDM rate packet num */
405                 if (i < 8)
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];
409         }
410
411 }
412
413 /*
414  * Function:    cmpk_message_handle_rx()
415  *
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.
421  *
422  * Input:       NONE
423  *
424  * Output:      NONE
425  *
426  * Return:      NONE
427  */
428 extern  u32
429 cmpk_message_handle_rx(
430         struct net_device *dev,
431         struct ieee80211_rx_stats *pstats)
432 {
433         struct r8192_priv *priv = ieee80211_priv(dev);
434         int                     total_length;
435         u8                      cmd_length, exe_cnt = 0;
436         u8                      element_id;
437         u8                      *pcmd_buff;
438
439         /*
440          * 0. Check input arguments.
441          * If is is a command queue message or pointer is null
442          */
443         if ((pstats == NULL))
444                 return 0;       /* This is not a command packet. */
445
446         /* 1. Read received command packet message length from RFD. */
447         total_length = pstats->Length;
448
449         /* 2. Read virtual address from RFD. */
450         pcmd_buff = pstats->virtual_address;
451
452         /* 3. Read command pakcet element id and length. */
453         element_id = pcmd_buff[0];
454
455         /*
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.
459          */
460
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) {
466                 case RX_TX_FEEDBACK:
467                         cmpk_handle_tx_feedback(dev, pcmd_buff);
468                         cmd_length = CMPK_RX_TX_FB_SIZE;
469                         break;
470                 case RX_INTERRUPT_STATUS:
471                         cmpk_handle_interrupt_status(dev, pcmd_buff);
472                         cmd_length = sizeof(cmpk_intr_sta_t);
473                         break;
474                 case BOTH_QUERY_CONFIG:
475                         cmpk_handle_query_config_rx(dev, pcmd_buff);
476                         cmd_length = CMPK_BOTH_QUERY_CONFIG_SIZE;
477                         break;
478                 case RX_TX_STATUS:
479                         cmpk_handle_tx_status(dev, pcmd_buff);
480                         cmd_length = CMPK_RX_TX_STS_SIZE;
481                         break;
482                 case RX_TX_PER_PKT_FEEDBACK:
483                         cmd_length = CMPK_RX_TX_FB_SIZE;
484                         break;
485                 case RX_TX_RATE_HISTORY:
486                         cmpk_handle_tx_rate_history(dev, pcmd_buff);
487                         cmd_length = CMPK_TX_RAHIS_SIZE;
488                         break;
489                 default:
490                          RT_TRACE(COMP_ERR, "(%s): unknown CMD Element\n",
491                                                                 __func__);
492                         return 1;       /* This is a command packet. */
493                 }
494                 priv->stats.rxcmdpkt[element_id]++;
495                 total_length -= cmd_length;
496                 pcmd_buff    += cmd_length;
497         }
498         return 1;       /* This is a command packet. */
499 }