include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / drivers / net / wireless / libertas / rx.c
blobbfb8898ae518908f8394ef94a6f158b74f1bb232
1 /*
2 * This file contains the handling of RX in wlan driver.
3 */
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <linux/etherdevice.h>
8 #include <linux/hardirq.h>
9 #include <linux/slab.h>
10 #include <linux/types.h>
11 #include <net/cfg80211.h>
13 #include "defs.h"
14 #include "host.h"
15 #include "radiotap.h"
16 #include "decl.h"
17 #include "dev.h"
19 struct eth803hdr {
20 u8 dest_addr[6];
21 u8 src_addr[6];
22 u16 h803_len;
23 } __packed;
25 struct rfc1042hdr {
26 u8 llc_dsap;
27 u8 llc_ssap;
28 u8 llc_ctrl;
29 u8 snap_oui[3];
30 u16 snap_type;
31 } __packed;
33 struct rxpackethdr {
34 struct eth803hdr eth803_hdr;
35 struct rfc1042hdr rfc1042_hdr;
36 } __packed;
38 struct rx80211packethdr {
39 struct rxpd rx_pd;
40 void *eth80211_hdr;
41 } __packed;
43 static int process_rxed_802_11_packet(struct lbs_private *priv,
44 struct sk_buff *skb);
46 /**
47 * lbs_process_rxed_packet - processes received packet and forwards it
48 * to kernel/upper layer
50 * @priv: A pointer to &struct lbs_private
51 * @skb: A pointer to skb which includes the received packet
52 * returns: 0 or -1
54 int lbs_process_rxed_packet(struct lbs_private *priv, struct sk_buff *skb)
56 int ret = 0;
57 struct net_device *dev = priv->dev;
58 struct rxpackethdr *p_rx_pkt;
59 struct rxpd *p_rx_pd;
60 int hdrchop;
61 struct ethhdr *p_ethhdr;
62 static const u8 rfc1042_eth_hdr[] = {
63 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00
66 lbs_deb_enter(LBS_DEB_RX);
68 BUG_ON(!skb);
70 skb->ip_summed = CHECKSUM_NONE;
72 if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR)
73 return process_rxed_802_11_packet(priv, skb);
75 p_rx_pd = (struct rxpd *) skb->data;
76 p_rx_pkt = (struct rxpackethdr *) ((u8 *)p_rx_pd +
77 le32_to_cpu(p_rx_pd->pkt_ptr));
79 dev = lbs_mesh_set_dev(priv, dev, p_rx_pd);
81 lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data,
82 min_t(unsigned int, skb->len, 100));
84 if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
85 lbs_deb_rx("rx err: frame received with bad length\n");
86 dev->stats.rx_length_errors++;
87 ret = 0;
88 dev_kfree_skb(skb);
89 goto done;
92 lbs_deb_rx("rx data: skb->len - pkt_ptr = %d-%zd = %zd\n",
93 skb->len, (size_t)le32_to_cpu(p_rx_pd->pkt_ptr),
94 skb->len - (size_t)le32_to_cpu(p_rx_pd->pkt_ptr));
96 lbs_deb_hex(LBS_DEB_RX, "RX Data: Dest", p_rx_pkt->eth803_hdr.dest_addr,
97 sizeof(p_rx_pkt->eth803_hdr.dest_addr));
98 lbs_deb_hex(LBS_DEB_RX, "RX Data: Src", p_rx_pkt->eth803_hdr.src_addr,
99 sizeof(p_rx_pkt->eth803_hdr.src_addr));
101 if (memcmp(&p_rx_pkt->rfc1042_hdr,
102 rfc1042_eth_hdr, sizeof(rfc1042_eth_hdr)) == 0) {
104 * Replace the 803 header and rfc1042 header (llc/snap) with an
105 * EthernetII header, keep the src/dst and snap_type (ethertype)
107 * The firmware only passes up SNAP frames converting
108 * all RX Data from 802.11 to 802.2/LLC/SNAP frames.
110 * To create the Ethernet II, just move the src, dst address right
111 * before the snap_type.
113 p_ethhdr = (struct ethhdr *)
114 ((u8 *) &p_rx_pkt->eth803_hdr
115 + sizeof(p_rx_pkt->eth803_hdr) + sizeof(p_rx_pkt->rfc1042_hdr)
116 - sizeof(p_rx_pkt->eth803_hdr.dest_addr)
117 - sizeof(p_rx_pkt->eth803_hdr.src_addr)
118 - sizeof(p_rx_pkt->rfc1042_hdr.snap_type));
120 memcpy(p_ethhdr->h_source, p_rx_pkt->eth803_hdr.src_addr,
121 sizeof(p_ethhdr->h_source));
122 memcpy(p_ethhdr->h_dest, p_rx_pkt->eth803_hdr.dest_addr,
123 sizeof(p_ethhdr->h_dest));
125 /* Chop off the rxpd + the excess memory from the 802.2/llc/snap header
126 * that was removed
128 hdrchop = (u8 *)p_ethhdr - (u8 *)p_rx_pd;
129 } else {
130 lbs_deb_hex(LBS_DEB_RX, "RX Data: LLC/SNAP",
131 (u8 *) &p_rx_pkt->rfc1042_hdr,
132 sizeof(p_rx_pkt->rfc1042_hdr));
134 /* Chop off the rxpd */
135 hdrchop = (u8 *)&p_rx_pkt->eth803_hdr - (u8 *)p_rx_pd;
138 /* Chop off the leading header bytes so the skb points to the start of
139 * either the reconstructed EthII frame or the 802.2/llc/snap frame
141 skb_pull(skb, hdrchop);
143 priv->cur_rate = lbs_fw_index_to_data_rate(p_rx_pd->rx_rate);
145 lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
146 dev->stats.rx_bytes += skb->len;
147 dev->stats.rx_packets++;
149 skb->protocol = eth_type_trans(skb, dev);
150 if (in_interrupt())
151 netif_rx(skb);
152 else
153 netif_rx_ni(skb);
155 ret = 0;
156 done:
157 lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
158 return ret;
160 EXPORT_SYMBOL_GPL(lbs_process_rxed_packet);
163 * convert_mv_rate_to_radiotap - converts Tx/Rx rates from Marvell WLAN format
164 * (see Table 2 in Section 3.1) to IEEE80211_RADIOTAP_RATE units (500 Kb/s)
166 * @rate: Input rate
167 * returns: Output Rate (0 if invalid)
169 static u8 convert_mv_rate_to_radiotap(u8 rate)
171 switch (rate) {
172 case 0: /* 1 Mbps */
173 return 2;
174 case 1: /* 2 Mbps */
175 return 4;
176 case 2: /* 5.5 Mbps */
177 return 11;
178 case 3: /* 11 Mbps */
179 return 22;
180 /* case 4: reserved */
181 case 5: /* 6 Mbps */
182 return 12;
183 case 6: /* 9 Mbps */
184 return 18;
185 case 7: /* 12 Mbps */
186 return 24;
187 case 8: /* 18 Mbps */
188 return 36;
189 case 9: /* 24 Mbps */
190 return 48;
191 case 10: /* 36 Mbps */
192 return 72;
193 case 11: /* 48 Mbps */
194 return 96;
195 case 12: /* 54 Mbps */
196 return 108;
198 pr_alert("Invalid Marvell WLAN rate %i\n", rate);
199 return 0;
203 * process_rxed_802_11_packet - processes a received 802.11 packet and forwards
204 * it to kernel/upper layer
206 * @priv: A pointer to &struct lbs_private
207 * @skb: A pointer to skb which includes the received packet
208 * returns: 0 or -1
210 static int process_rxed_802_11_packet(struct lbs_private *priv,
211 struct sk_buff *skb)
213 int ret = 0;
214 struct net_device *dev = priv->dev;
215 struct rx80211packethdr *p_rx_pkt;
216 struct rxpd *prxpd;
217 struct rx_radiotap_hdr radiotap_hdr;
218 struct rx_radiotap_hdr *pradiotap_hdr;
220 lbs_deb_enter(LBS_DEB_RX);
222 p_rx_pkt = (struct rx80211packethdr *) skb->data;
223 prxpd = &p_rx_pkt->rx_pd;
225 /* lbs_deb_hex(LBS_DEB_RX, "RX Data: Before chop rxpd", skb->data, min(skb->len, 100)); */
227 if (skb->len < (ETH_HLEN + 8 + sizeof(struct rxpd))) {
228 lbs_deb_rx("rx err: frame received with bad length\n");
229 dev->stats.rx_length_errors++;
230 ret = -EINVAL;
231 kfree_skb(skb);
232 goto done;
235 lbs_deb_rx("rx data: skb->len-sizeof(RxPd) = %d-%zd = %zd\n",
236 skb->len, sizeof(struct rxpd), skb->len - sizeof(struct rxpd));
238 /* create the exported radio header */
240 /* radiotap header */
241 memset(&radiotap_hdr, 0, sizeof(radiotap_hdr));
242 /* XXX must check radiotap_hdr.hdr.it_pad for pad */
243 radiotap_hdr.hdr.it_len = cpu_to_le16 (sizeof(struct rx_radiotap_hdr));
244 radiotap_hdr.hdr.it_present = cpu_to_le32 (RX_RADIOTAP_PRESENT);
245 radiotap_hdr.rate = convert_mv_rate_to_radiotap(prxpd->rx_rate);
246 /* XXX must check no carryout */
247 radiotap_hdr.antsignal = prxpd->snr + prxpd->nf;
249 /* chop the rxpd */
250 skb_pull(skb, sizeof(struct rxpd));
252 /* add space for the new radio header */
253 if ((skb_headroom(skb) < sizeof(struct rx_radiotap_hdr)) &&
254 pskb_expand_head(skb, sizeof(struct rx_radiotap_hdr), 0, GFP_ATOMIC)) {
255 netdev_alert(dev, "%s: couldn't pskb_expand_head\n", __func__);
256 ret = -ENOMEM;
257 kfree_skb(skb);
258 goto done;
261 pradiotap_hdr = (void *)skb_push(skb, sizeof(struct rx_radiotap_hdr));
262 memcpy(pradiotap_hdr, &radiotap_hdr, sizeof(struct rx_radiotap_hdr));
264 priv->cur_rate = lbs_fw_index_to_data_rate(prxpd->rx_rate);
266 lbs_deb_rx("rx data: size of actual packet %d\n", skb->len);
267 dev->stats.rx_bytes += skb->len;
268 dev->stats.rx_packets++;
270 skb->protocol = eth_type_trans(skb, priv->dev);
272 if (in_interrupt())
273 netif_rx(skb);
274 else
275 netif_rx_ni(skb);
277 ret = 0;
279 done:
280 lbs_deb_leave_args(LBS_DEB_RX, "ret %d", ret);
281 return ret;