[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / net / wireless / libertas / rx.c
blob65f02cc6752fe05bbf0b81da42f22024fd13d149
1 /**
2 * This file contains the handling of RX in wlan driver.
3 */
4 #include <linux/etherdevice.h>
5 #include <linux/types.h>
7 #include "hostcmd.h"
8 #include "radiotap.h"
9 #include "decl.h"
10 #include "dev.h"
11 #include "wext.h"
13 struct eth803hdr {
14 u8 dest_addr[6];
15 u8 src_addr[6];
16 u16 h803_len;
17 } __attribute__ ((packed));
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));
27 struct rxpackethdr {
28 struct eth803hdr eth803_hdr;
29 struct rfc1042hdr rfc1042_hdr;
30 } __attribute__ ((packed));
32 struct rx80211packethdr {
33 struct rxpd rx_pd;
34 void *eth80211_hdr;
35 } __attribute__ ((packed));
37 static int process_rxed_802_11_packet(struct lbs_private *priv,
38 struct sk_buff *skb);
40 /**
41 * @brief This function computes the avgSNR .
43 * @param priv A pointer to struct lbs_private structure
44 * @return avgSNR
46 static u8 lbs_getavgsnr(struct lbs_private *priv)
48 u8 i;
49 u16 temp = 0;
50 if (priv->numSNRNF == 0)
51 return 0;
52 for (i = 0; i < priv->numSNRNF; i++)
53 temp += priv->rawSNR[i];
54 return (u8) (temp / priv->numSNRNF);
58 /**
59 * @brief This function computes the AvgNF
61 * @param priv A pointer to struct lbs_private structure
62 * @return AvgNF
64 static u8 lbs_getavgnf(struct lbs_private *priv)
66 u8 i;
67 u16 temp = 0;
68 if (priv->numSNRNF == 0)
69 return 0;
70 for (i = 0; i < priv->numSNRNF; i++)
71 temp += priv->rawNF[i];
72 return (u8) (temp / priv->numSNRNF);
76 /**
77 * @brief This function save the raw SNR/NF to our internel buffer
79 * @param priv A pointer to struct lbs_private structure
80 * @param prxpd A pointer to rxpd structure of received packet
81 * @return n/a
83 static void lbs_save_rawSNRNF(struct lbs_private *priv, struct rxpd *p_rx_pd)
85 if (priv->numSNRNF < DEFAULT_DATA_AVG_FACTOR)
86 priv->numSNRNF++;
87 priv->rawSNR[priv->nextSNRNF] = p_rx_pd->snr;
88 priv->rawNF[priv->nextSNRNF] = p_rx_pd->nf;
89 priv->nextSNRNF++;
90 if (priv->nextSNRNF >= DEFAULT_DATA_AVG_FACTOR)
91 priv->nextSNRNF = 0;
92 return;
95 /**
96 * @brief This function computes the RSSI in received packet.
98 * @param priv A pointer to struct lbs_private structure
99 * @param prxpd A pointer to rxpd structure of received packet
100 * @return n/a
102 static void lbs_compute_rssi(struct lbs_private *priv, struct rxpd *p_rx_pd)
105 lbs_deb_enter(LBS_DEB_RX);
107 lbs_deb_rx("rxpd: SNR %d, NF %d\n", p_rx_pd->snr, p_rx_pd->nf);
108 lbs_deb_rx("before computing SNR: SNR-avg = %d, NF-avg = %d\n",
109 priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
110 priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
112 priv->SNR[TYPE_RXPD][TYPE_NOAVG] = p_rx_pd->snr;
113 priv->NF[TYPE_RXPD][TYPE_NOAVG] = p_rx_pd->nf;
114 lbs_save_rawSNRNF(priv, p_rx_pd);
116 priv->SNR[TYPE_RXPD][TYPE_AVG] = lbs_getavgsnr(priv) * AVG_SCALE;
117 priv->NF[TYPE_RXPD][TYPE_AVG] = lbs_getavgnf(priv) * AVG_SCALE;
118 lbs_deb_rx("after computing SNR: SNR-avg = %d, NF-avg = %d\n",
119 priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
120 priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
122 priv->RSSI[TYPE_RXPD][TYPE_NOAVG] =
123 CAL_RSSI(priv->SNR[TYPE_RXPD][TYPE_NOAVG],
124 priv->NF[TYPE_RXPD][TYPE_NOAVG]);
126 priv->RSSI[TYPE_RXPD][TYPE_AVG] =
127 CAL_RSSI(priv->SNR[TYPE_RXPD][TYPE_AVG] / AVG_SCALE,
128 priv->NF[TYPE_RXPD][TYPE_AVG] / AVG_SCALE);
130 lbs_deb_leave(LBS_DEB_RX);
134 * @brief This function processes received packet and forwards it
135 * to kernel/upper layer
137 * @param priv A pointer to struct lbs_private
138 * @param skb A pointer to skb which includes the received packet
139 * @return 0 or -1
141 int lbs_process_rxed_packet(struct lbs_private *priv, struct sk_buff *skb)
143 int ret = 0;
144 struct net_device *dev = priv->dev;
145 struct rxpackethdr *p_rx_pkt;
146 struct rxpd *p_rx_pd;
147 int hdrchop;
148 struct ethhdr *p_ethhdr;
149 const u8 rfc1042_eth_hdr[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
151 lbs_deb_enter(LBS_DEB_RX);
153 BUG_ON(!skb);
155 skb->ip_summed = CHECKSUM_NONE;
157 if (priv->monitormode)
158 return process_rxed_802_11_packet(priv, skb);
160 p_rx_pd = (struct rxpd *) skb->data;
161 p_rx_pkt = (struct rxpackethdr *) ((u8 *)p_rx_pd +
162 le32_to_cpu(p_rx_pd->pkt_ptr));
163 if (priv->mesh_dev) {
164 if (priv->mesh_fw_ver == MESH_FW_OLD) {
165 if (p_rx_pd->rx_control & RxPD_MESH_FRAME)
166 dev = priv->mesh_dev;
167 } else if (priv->mesh_fw_ver == MESH_FW_NEW) {
168 if (p_rx_pd->u.bss.bss_num == MESH_IFACE_ID)
169 dev = priv->mesh_dev;
173 lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
174 min_t(unsigned int, skb->len, 100));
176 if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
177 lbs_deb_rx("rx err: frame received with bad length\n");
178 dev->stats.rx_length_errors++;
179 ret = 0;
180 dev_kfree_skb(skb);
181 goto done;
184 lbs_deb_rx("rx data: skb->len - pkt_ptr = %d-%zd = %zd\n",
185 skb->len, (size_t)le32_to_cpu(p_rx_pd->pkt_ptr),
186 skb->len - (size_t)le32_to_cpu(p_rx_pd->pkt_ptr));
188 lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
189 sizeof(p_rx_pkt->eth803_hdr.dest_addr));
190 lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
191 sizeof(p_rx_pkt->eth803_hdr.src_addr));
193 if (memcmp(&p_rx_pkt->rfc1042_hdr,
194 rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
196 * Replace the 803 header and rfc1042 header (llc/snap) with an
197 * EthernetII header, keep the src/dst and snap_type (ethertype)
199 * The firmware only passes up SNAP frames converting
200 * all RX Data from 802.11 to 802.2/LLC/SNAP frames.
202 * To create the Ethernet II, just move the src, dst address right
203 * before the snap_type.
205 p_ethhdr = (struct ethhdr *)
206 ((u8 *) & p_rx_pkt->eth803_hdr
207 + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
208 - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
209 - sizeof(p_rx_pkt->eth803_hdr.src_addr)
210 - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
212 memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
213 sizeof(p_ethhdr->h_source));
214 memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
215 sizeof(p_ethhdr->h_dest));
217 /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
218 * that was removed
220 hdrchop = (u8 *)p_ethhdr - (u8 *)p_rx_pd;
221 } else {
222 lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
223 (u8 *) & p_rx_pkt->rfc1042_hdr,
224 sizeof(p_rx_pkt->rfc1042_hdr));
226 /* Chop off the rxpd */
227 hdrchop = (u8 *)&p_rx_pkt->eth803_hdr - (u8 *)p_rx_pd;
230 /* Chop off the leading header bytes so the skb points to the start of
231 * either the reconstructed EthII frame or the 802.2/llc/snap frame
233 skb_pull(skb, hdrchop);
235 /* Take the data rate from the rxpd structure
236 * only if the rate is auto
238 if (priv->enablehwauto)
239 priv->cur_rate = lbs_fw_index_to_data_rate(p_rx_pd->rx_rate);
241 lbs_compute_rssi(priv, p_rx_pd);
243 lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
244 dev->stats.rx_bytes += skb->len;
245 dev->stats.rx_packets++;
247 skb->protocol = eth_type_trans(skb, dev);
248 if (in_interrupt())
249 netif_rx(skb);
250 else
251 netif_rx_ni(skb);
253 ret = 0;
254 done:
255 lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
256 return ret;
258 EXPORT_SYMBOL_GPL(lbs_process_rxed_packet);
261 * @brief This function converts Tx/Rx rates from the Marvell WLAN format
262 * (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
264 * @param rate Input rate
265 * @return Output Rate (0 if invalid)
267 static u8 convert_mv_rate_to_radiotap(u8 rate)
269 switch (rate) {
270 case 0: /* 1 Mbps */
271 return 2;
272 case 1: /* 2 Mbps */
273 return 4;
274 case 2: /* 5.5 Mbps */
275 return 11;
276 case 3: /* 11 Mbps */
277 return 22;
278 /* case 4: reserved */
279 case 5: /* 6 Mbps */
280 return 12;
281 case 6: /* 9 Mbps */
282 return 18;
283 case 7: /* 12 Mbps */
284 return 24;
285 case 8: /* 18 Mbps */
286 return 36;
287 case 9: /* 24 Mbps */
288 return 48;
289 case 10: /* 36 Mbps */
290 return 72;
291 case 11: /* 48 Mbps */
292 return 96;
293 case 12: /* 54 Mbps */
294 return 108;
296 lbs_pr_alert("Invalid Marvell WLAN rate %i\n", rate);
297 return 0;
301 * @brief This function processes a received 802.11 packet and forwards it
302 * to kernel/upper layer
304 * @param priv A pointer to struct lbs_private
305 * @param skb A pointer to skb which includes the received packet
306 * @return 0 or -1
308 static int process_rxed_802_11_packet(struct lbs_private *priv,
309 struct sk_buff *skb)
311 int ret = 0;
312 struct net_device *dev = priv->dev;
313 struct rx80211packethdr *p_rx_pkt;
314 struct rxpd *prxpd;
315 struct rx_radiotap_hdr radiotap_hdr;
316 struct rx_radiotap_hdr *pradiotap_hdr;
318 lbs_deb_enter(LBS_DEB_RX);
320 p_rx_pkt = (struct rx80211packethdr *) skb->data;
321 prxpd = &p_rx_pkt->rx_pd;
323 // lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100));
325 if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
326 lbs_deb_rx("rx err: frame received with bad length\n");
327 dev->stats.rx_length_errors++;
328 ret = -EINVAL;
329 kfree_skb(skb);
330 goto done;
333 lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
334 skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
336 /* create the exported radio header */
338 /* radiotap header */
339 radiotap_hdr.hdr.it_version = 0;
340 /* XXX must check this value for pad */
341 radiotap_hdr.hdr.it_pad = 0;
342 radiotap_hdr.hdr.it_len = cpu_to_le16 (sizeof(struct rx_radiotap_hdr));
343 radiotap_hdr.hdr.it_present = cpu_to_le32 (RX_RADIOTAP_PRESENT);
344 radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
345 /* XXX must check no carryout */
346 radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
348 /* chop the rxpd */
349 skb_pull(skb, sizeof(struct rxpd));
351 /* add space for the new radio header */
352 if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
353 pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0, GFP_ATOMIC)) {
354 lbs_pr_alert("%s: couldn't pskb_expand_head\n", __func__);
355 ret = -ENOMEM;
356 kfree_skb(skb);
357 goto done;
360 pradiotap_hdr = (void *)skb_push(skb, sizeof(struct rx_radiotap_hdr));
361 memcpy(pradiotap_hdr, &radiotap_hdr, sizeof(struct rx_radiotap_hdr));
363 /* Take the data rate from the rxpd structure
364 * only if the rate is auto
366 if (priv->enablehwauto)
367 priv->cur_rate = lbs_fw_index_to_data_rate(prxpd->rx_rate);
369 lbs_compute_rssi(priv, prxpd);
371 lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
372 dev->stats.rx_bytes += skb->len;
373 dev->stats.rx_packets++;
375 skb->protocol = eth_type_trans(skb, priv->rtap_net_dev);
376 netif_rx(skb);
378 ret = 0;
380 done:
381 lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
382 return ret;