2 * This file contains the handling of RX in wlan driver.
4 #include <linux/etherdevice.h>
5 #include <linux/types.h>
17 } __attribute__ ((packed));
25 } __attribute__ ((packed));
28 struct eth803hdr eth803_hdr;
29 struct rfc1042hdr rfc1042_hdr;
30 } __attribute__ ((packed));
32 struct rx80211packethdr {
35 } __attribute__ ((packed));
37 static int process_rxed_802_11_packet(struct lbs_private *priv,
41 * @brief This function computes the avgSNR .
43 * @param priv A pointer to struct lbs_private structure
46 static u8 lbs_getavgsnr(struct lbs_private *priv)
50 if (priv->numSNRNF == 0)
52 for (i = 0; i < priv->numSNRNF; i++)
53 temp += priv->rawSNR[i];
54 return (u8) (temp / priv->numSNRNF);
59 * @brief This function computes the AvgNF
61 * @param priv A pointer to struct lbs_private structure
64 static u8 lbs_getavgnf(struct lbs_private *priv)
68 if (priv->numSNRNF == 0)
70 for (i = 0; i < priv->numSNRNF; i++)
71 temp += priv->rawNF[i];
72 return (u8) (temp / priv->numSNRNF);
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
83 static void lbs_save_rawSNRNF(struct lbs_private *priv, struct rxpd *p_rx_pd)
85 if (priv->numSNRNF < DEFAULT_DATA_AVG_FACTOR)
87 priv->rawSNR[priv->nextSNRNF] = p_rx_pd->snr;
88 priv->rawNF[priv->nextSNRNF] = p_rx_pd->nf;
90 if (priv->nextSNRNF >= DEFAULT_DATA_AVG_FACTOR)
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
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
141 int lbs_process_rxed_packet(struct lbs_private *priv, struct sk_buff *skb)
144 struct net_device *dev = priv->dev;
145 struct rxpackethdr *p_rx_pkt;
146 struct rxpd *p_rx_pd;
148 struct ethhdr *p_ethhdr;
149 const u8 rfc1042_eth_hdr[] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
151 lbs_deb_enter(LBS_DEB_RX);
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++;
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
220 hdrchop = (u8 *)p_ethhdr - (u8 *)p_rx_pd;
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);
255 lbs_deb_leave_args(LBS_DEB_RX, "ret %d", 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)
274 case 2: /* 5.5 Mbps */
276 case 3: /* 11 Mbps */
278 /* case 4: reserved */
283 case 7: /* 12 Mbps */
285 case 8: /* 18 Mbps */
287 case 9: /* 24 Mbps */
289 case 10: /* 36 Mbps */
291 case 11: /* 48 Mbps */
293 case 12: /* 54 Mbps */
296 lbs_pr_alert("Invalid Marvell WLAN rate %i\n", rate);
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
308 static int process_rxed_802_11_packet(struct lbs_private *priv,
312 struct net_device *dev = priv->dev;
313 struct rx80211packethdr *p_rx_pkt;
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++;
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;
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__);
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);
381 lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);