[PATCH] libertas: fix more mixed-case abuse
[linux-2.6] / drivers / net / wireless / libertas / rx.c
1 /**
2   * This file contains the handling of RX in wlan driver.
3   */
4 #include <linux/etherdevice.h>
5 #include <linux/types.h>
6
7 #include "hostcmd.h"
8 #include "radiotap.h"
9 #include "decl.h"
10 #include "dev.h"
11 #include "wext.h"
12
13 struct eth803hdr {
14         u8 dest_addr[6];
15         u8 src_addr[6];
16         u16 h803_len;
17 } __attribute__ ((packed));
18
19 struct rfc1042hdr {
20         u8 llc_dsap;
21         u8 llc_ssap;
22         u8 llc_ctrl;
23         u8 snap_oui[3];
24         u16 snap_type;
25 } __attribute__ ((packed));
26
27 struct rxpackethdr {
28         struct rxpd rx_pd;
29         struct eth803hdr eth803_hdr;
30         struct rfc1042hdr rfc1042_hdr;
31 } __attribute__ ((packed));
32
33 struct rx80211packethdr {
34         struct rxpd rx_pd;
35         void *eth80211_hdr;
36 } __attribute__ ((packed));
37
38 static int process_rxed_802_11_packet(wlan_private * priv, struct sk_buff *skb);
39
40 /**
41  *  @brief This function computes the avgSNR .
42  *
43  *  @param priv    A pointer to wlan_private structure
44  *  @return        avgSNR
45  */
46 static u8 wlan_getavgsnr(wlan_private * priv)
47 {
48         u8 i;
49         u16 temp = 0;
50         wlan_adapter *adapter = priv->adapter;
51         if (adapter->numSNRNF == 0)
52                 return 0;
53         for (i = 0; i < adapter->numSNRNF; i++)
54                 temp += adapter->rawSNR[i];
55         return (u8) (temp / adapter->numSNRNF);
56
57 }
58
59 /**
60  *  @brief This function computes the AvgNF
61  *
62  *  @param priv    A pointer to wlan_private structure
63  *  @return        AvgNF
64  */
65 static u8 wlan_getavgnf(wlan_private * priv)
66 {
67         u8 i;
68         u16 temp = 0;
69         wlan_adapter *adapter = priv->adapter;
70         if (adapter->numSNRNF == 0)
71                 return 0;
72         for (i = 0; i < adapter->numSNRNF; i++)
73                 temp += adapter->rawNF[i];
74         return (u8) (temp / adapter->numSNRNF);
75
76 }
77
78 /**
79  *  @brief This function save the raw SNR/NF to our internel buffer
80  *
81  *  @param priv    A pointer to wlan_private structure
82  *  @param prxpd   A pointer to rxpd structure of received packet
83  *  @return        n/a
84  */
85 static void wlan_save_rawSNRNF(wlan_private * priv, struct rxpd *p_rx_pd)
86 {
87         wlan_adapter *adapter = priv->adapter;
88         if (adapter->numSNRNF < adapter->data_avg_factor)
89                 adapter->numSNRNF++;
90         adapter->rawSNR[adapter->nextSNRNF] = p_rx_pd->snr;
91         adapter->rawNF[adapter->nextSNRNF] = p_rx_pd->nf;
92         adapter->nextSNRNF++;
93         if (adapter->nextSNRNF >= adapter->data_avg_factor)
94                 adapter->nextSNRNF = 0;
95         return;
96 }
97
98 /**
99  *  @brief This function computes the RSSI in received packet.
100  *
101  *  @param priv    A pointer to wlan_private structure
102  *  @param prxpd   A pointer to rxpd structure of received packet
103  *  @return        n/a
104  */
105 static void wlan_compute_rssi(wlan_private * priv, struct rxpd *p_rx_pd)
106 {
107         wlan_adapter *adapter = priv->adapter;
108
109         lbs_deb_enter(LBS_DEB_RX);
110
111         lbs_deb_rx("rxpd: SNR %d, NF %d\n", p_rx_pd->snr, p_rx_pd->nf);
112         lbs_deb_rx("before computing SNR: SNR-avg = %d, NF-avg = %d\n",
113                adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
114                adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
115
116         adapter->SNR[TYPE_RXPD][TYPE_NOAVG] = p_rx_pd->snr;
117         adapter->NF[TYPE_RXPD][TYPE_NOAVG] = p_rx_pd->nf;
118         wlan_save_rawSNRNF(priv, p_rx_pd);
119
120         adapter->rxpd_rate = p_rx_pd->rx_rate;
121
122         adapter->SNR[TYPE_RXPD][TYPE_AVG] = wlan_getavgsnr(priv) * AVG_SCALE;
123         adapter->NF[TYPE_RXPD][TYPE_AVG] = wlan_getavgnf(priv) * AVG_SCALE;
124         lbs_deb_rx("after computing SNR: SNR-avg = %d, NF-avg = %d\n",
125                adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
126                adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
127
128         adapter->RSSI[TYPE_RXPD][TYPE_NOAVG] =
129             CAL_RSSI(adapter->SNR[TYPE_RXPD][TYPE_NOAVG],
130                      adapter->NF[TYPE_RXPD][TYPE_NOAVG]);
131
132         adapter->RSSI[TYPE_RXPD][TYPE_AVG] =
133             CAL_RSSI(adapter->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
134                      adapter->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
135
136         lbs_deb_leave(LBS_DEB_RX);
137 }
138
139 void libertas_upload_rx_packet(wlan_private * priv, struct sk_buff *skb)
140 {
141         lbs_deb_rx("skb->data %p\n", skb->data);
142
143         if (priv->mesh_dev && IS_MESH_FRAME(skb))
144                 skb->protocol = eth_type_trans(skb, priv->mesh_dev);
145         else
146                 skb->protocol = eth_type_trans(skb, priv->dev);
147         skb->ip_summed = CHECKSUM_UNNECESSARY;
148
149         netif_rx(skb);
150 }
151
152 /**
153  *  @brief This function processes received packet and forwards it
154  *  to kernel/upper layer
155  *
156  *  @param priv    A pointer to wlan_private
157  *  @param skb     A pointer to skb which includes the received packet
158  *  @return        0 or -1
159  */
160 int libertas_process_rxed_packet(wlan_private * priv, struct sk_buff *skb)
161 {
162         wlan_adapter *adapter = priv->adapter;
163         int ret = 0;
164
165         struct rxpackethdr *p_rx_pkt;
166         struct rxpd *p_rx_pd;
167
168         int hdrchop;
169         struct ethhdr *p_ethhdr;
170
171         const u8 rfc1042_eth_hdr[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
172
173         lbs_deb_enter(LBS_DEB_RX);
174
175         if (priv->adapter->debugmode & MRVDRV_DEBUG_RX_PATH)
176                 lbs_dbg_hex("RX packet: ", skb->data,
177                          min_t(unsigned int, skb->len, 100));
178
179         if (priv->adapter->linkmode == WLAN_LINKMODE_802_11)
180                 return process_rxed_802_11_packet(priv, skb);
181
182         p_rx_pkt = (struct rxpackethdr *) skb->data;
183         p_rx_pd = &p_rx_pkt->rx_pd;
184         if (p_rx_pd->rx_control & RxPD_MESH_FRAME)
185                 SET_MESH_FRAME(skb);
186         else
187                 UNSET_MESH_FRAME(skb);
188
189         lbs_dbg_hex("RX Data: Before chop rxpd", skb->data,
190                  min_t(unsigned int, skb->len, 100));
191
192         if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
193                 lbs_deb_rx("rx err: frame received with bad length\n");
194                 priv->stats.rx_length_errors++;
195                 ret = 0;
196                 goto done;
197         }
198
199         /*
200          * Check rxpd status and update 802.3 stat,
201          */
202         if (!(p_rx_pd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK))) {
203                 lbs_deb_rx("rx err: frame received with bad status\n");
204                 lbs_pr_alert("rxpd not ok\n");
205                 priv->stats.rx_errors++;
206                 ret = 0;
207                 goto done;
208         }
209
210         lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
211                skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
212
213         lbs_dbg_hex("RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
214                 sizeof(p_rx_pkt->eth803_hdr.dest_addr));
215         lbs_dbg_hex("RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
216                 sizeof(p_rx_pkt->eth803_hdr.src_addr));
217
218         if (memcmp(&p_rx_pkt->rfc1042_hdr,
219                    rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
220                 /*
221                  *  Replace the 803 header and rfc1042 header (llc/snap) with an
222                  *    EthernetII header, keep the src/dst and snap_type (ethertype)
223                  *
224                  *  The firmware only passes up SNAP frames converting
225                  *    all RX Data from 802.11 to 802.2/LLC/SNAP frames.
226                  *
227                  *  To create the Ethernet II, just move the src, dst address right
228                  *    before the snap_type.
229                  */
230                 p_ethhdr = (struct ethhdr *)
231                     ((u8 *) & p_rx_pkt->eth803_hdr
232                      + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
233                      - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
234                      - sizeof(p_rx_pkt->eth803_hdr.src_addr)
235                      - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
236
237                 memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
238                        sizeof(p_ethhdr->h_source));
239                 memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
240                        sizeof(p_ethhdr->h_dest));
241
242                 /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
243                  *   that was removed
244                  */
245                 hdrchop = (u8 *) p_ethhdr - (u8 *) p_rx_pkt;
246         } else {
247                 lbs_dbg_hex("RX Data: LLC/SNAP",
248                         (u8 *) & p_rx_pkt->rfc1042_hdr,
249                         sizeof(p_rx_pkt->rfc1042_hdr));
250
251                 /* Chop off the rxpd */
252                 hdrchop = (u8 *) & p_rx_pkt->eth803_hdr - (u8 *) p_rx_pkt;
253         }
254
255         /* Chop off the leading header bytes so the skb points to the start of
256          *   either the reconstructed EthII frame or the 802.2/llc/snap frame
257          */
258         skb_pull(skb, hdrchop);
259
260         /* Take the data rate from the rxpd structure
261          * only if the rate is auto
262          */
263         if (adapter->is_datarate_auto)
264                 adapter->datarate = libertas_index_to_data_rate(p_rx_pd->rx_rate);
265
266         wlan_compute_rssi(priv, p_rx_pd);
267
268         lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
269         priv->stats.rx_bytes += skb->len;
270         priv->stats.rx_packets++;
271
272         libertas_upload_rx_packet(priv, skb);
273
274         ret = 0;
275 done:
276         lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
277         return ret;
278 }
279 EXPORT_SYMBOL_GPL(libertas_process_rxed_packet);
280
281 /**
282  *  @brief This function converts Tx/Rx rates from the Marvell WLAN format
283  *  (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
284  *
285  *  @param rate    Input rate
286  *  @return        Output Rate (0 if invalid)
287  */
288 static u8 convert_mv_rate_to_radiotap(u8 rate)
289 {
290         switch (rate) {
291         case 0:         /*   1 Mbps */
292                 return 2;
293         case 1:         /*   2 Mbps */
294                 return 4;
295         case 2:         /* 5.5 Mbps */
296                 return 11;
297         case 3:         /*  11 Mbps */
298                 return 22;
299         case 4:         /*   6 Mbps */
300                 return 12;
301         case 5:         /*   9 Mbps */
302                 return 18;
303         case 6:         /*  12 Mbps */
304                 return 24;
305         case 7:         /*  18 Mbps */
306                 return 36;
307         case 8:         /*  24 Mbps */
308                 return 48;
309         case 9:         /*  36 Mbps */
310                 return 72;
311         case 10:                /*  48 Mbps */
312                 return 96;
313         case 11:                /*  54 Mbps */
314                 return 108;
315         }
316         lbs_pr_alert("Invalid Marvell WLAN rate %i\n", rate);
317         return 0;
318 }
319
320 /**
321  *  @brief This function processes a received 802.11 packet and forwards it
322  *  to kernel/upper layer
323  *
324  *  @param priv    A pointer to wlan_private
325  *  @param skb     A pointer to skb which includes the received packet
326  *  @return        0 or -1
327  */
328 static int process_rxed_802_11_packet(wlan_private * priv, struct sk_buff *skb)
329 {
330         wlan_adapter *adapter = priv->adapter;
331         int ret = 0;
332
333         struct rx80211packethdr *p_rx_pkt;
334         struct rxpd *prxpd;
335         struct rx_radiotap_hdr radiotap_hdr;
336         struct rx_radiotap_hdr *pradiotap_hdr;
337
338         lbs_deb_enter(LBS_DEB_RX);
339
340         p_rx_pkt = (struct rx80211packethdr *) skb->data;
341         prxpd = &p_rx_pkt->rx_pd;
342
343         // lbs_dbg_hex("RX Data: Before chop rxpd", skb->data, min(skb->len, 100));
344
345         if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
346                 lbs_deb_rx("rx err: frame received wit bad length\n");
347                 priv->stats.rx_length_errors++;
348                 ret = 0;
349                 goto done;
350         }
351
352         /*
353          * Check rxpd status and update 802.3 stat,
354          */
355         if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK))) {
356                 //lbs_deb_rx("rx err: frame received with bad status\n");
357                 priv->stats.rx_errors++;
358         }
359
360         lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
361                skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
362
363         /* create the exported radio header */
364         switch (priv->adapter->radiomode) {
365         case WLAN_RADIOMODE_NONE:
366                 /* no radio header */
367                 /* chop the rxpd */
368                 skb_pull(skb, sizeof(struct rxpd));
369                 break;
370
371         case WLAN_RADIOMODE_RADIOTAP:
372                 /* radiotap header */
373                 radiotap_hdr.hdr.it_version = 0;
374                 /* XXX must check this value for pad */
375                 radiotap_hdr.hdr.it_pad = 0;
376                 radiotap_hdr.hdr.it_len = sizeof(struct rx_radiotap_hdr);
377                 radiotap_hdr.hdr.it_present = RX_RADIOTAP_PRESENT;
378                 /* unknown values */
379                 radiotap_hdr.flags = 0;
380                 radiotap_hdr.chan_freq = 0;
381                 radiotap_hdr.chan_flags = 0;
382                 radiotap_hdr.antenna = 0;
383                 /* known values */
384                 radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
385                 /* XXX must check no carryout */
386                 radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
387                 radiotap_hdr.rx_flags = 0;
388                 if (!(prxpd->status & cpu_to_le16(MRVDRV_RXPD_STATUS_OK)))
389                         radiotap_hdr.rx_flags |= IEEE80211_RADIOTAP_F_RX_BADFCS;
390                 //memset(radiotap_hdr.pad, 0x11, IEEE80211_RADIOTAP_HDRLEN - 18);
391
392                 // lbs_dbg_hex1("RX radiomode packet BEF: ", skb->data, min(skb->len, 100));
393
394                 /* chop the rxpd */
395                 skb_pull(skb, sizeof(struct rxpd));
396
397                 /* add space for the new radio header */
398                 if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
399                     pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0,
400                                      GFP_ATOMIC)) {
401                         lbs_pr_alert("%s: couldn't pskb_expand_head\n",
402                                __func__);
403                 }
404
405                 pradiotap_hdr =
406                     (struct rx_radiotap_hdr *)skb_push(skb,
407                                                      sizeof(struct
408                                                             rx_radiotap_hdr));
409                 memcpy(pradiotap_hdr, &radiotap_hdr,
410                        sizeof(struct rx_radiotap_hdr));
411                 //lbs_dbg_hex1("RX radiomode packet AFT: ", skb->data, min(skb->len, 100));
412                 break;
413
414         default:
415                 /* unknown header */
416                 lbs_pr_alert("Unknown radiomode %i\n",
417                        priv->adapter->radiomode);
418                 /* don't export any header */
419                 /* chop the rxpd */
420                 skb_pull(skb, sizeof(struct rxpd));
421                 break;
422         }
423
424         /* Take the data rate from the rxpd structure
425          * only if the rate is auto
426          */
427         if (adapter->is_datarate_auto) {
428                 adapter->datarate = libertas_index_to_data_rate(prxpd->rx_rate);
429         }
430
431         wlan_compute_rssi(priv, prxpd);
432
433         lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
434         priv->stats.rx_bytes += skb->len;
435         priv->stats.rx_packets++;
436
437         libertas_upload_rx_packet(priv, skb);
438
439         ret = 0;
440
441 done:
442         lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
443         return ret;
444 }