Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / drivers / net / wireless / wl12xx / rx.c
1 /*
2  * This file is part of wl12xx
3  *
4  * Copyright (c) 1998-2007 Texas Instruments Incorporated
5  * Copyright (C) 2008 Nokia Corporation
6  *
7  * Contact: Kalle Valo <kalle.valo@nokia.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 #include <linux/skbuff.h>
26 #include <net/mac80211.h>
27
28 #include "wl12xx.h"
29 #include "reg.h"
30 #include "spi.h"
31 #include "rx.h"
32
33 static void wl12xx_rx_header(struct wl12xx *wl,
34                              struct wl12xx_rx_descriptor *desc)
35 {
36         u32 rx_packet_ring_addr;
37
38         rx_packet_ring_addr = wl->data_path->rx_packet_ring_addr;
39         if (wl->rx_current_buffer)
40                 rx_packet_ring_addr += wl->data_path->rx_packet_ring_chunk_size;
41
42         wl12xx_spi_mem_read(wl, rx_packet_ring_addr, desc,
43                             sizeof(struct wl12xx_rx_descriptor));
44 }
45
46 static void wl12xx_rx_status(struct wl12xx *wl,
47                              struct wl12xx_rx_descriptor *desc,
48                              struct ieee80211_rx_status *status,
49                              u8 beacon)
50 {
51         memset(status, 0, sizeof(struct ieee80211_rx_status));
52
53         status->band = IEEE80211_BAND_2GHZ;
54         status->mactime = desc->timestamp;
55
56         /*
57          * The rx status timestamp is a 32 bits value while the TSF is a
58          * 64 bits one.
59          * For IBSS merging, TSF is mandatory, so we have to get it
60          * somehow, so we ask for ACX_TSF_INFO.
61          * That could be moved to the get_tsf() hook, but unfortunately,
62          * this one must be atomic, while our SPI routines can sleep.
63          */
64         if ((wl->bss_type == BSS_TYPE_IBSS) && beacon) {
65                 u64 mactime;
66                 int ret;
67                 struct wl12xx_command cmd;
68                 struct acx_tsf_info *tsf_info;
69
70                 memset(&cmd, 0, sizeof(cmd));
71
72                 ret = wl12xx_cmd_interrogate(wl, ACX_TSF_INFO,
73                                              sizeof(struct acx_tsf_info),
74                                              &cmd);
75                 if (ret < 0) {
76                         wl12xx_warning("ACX_FW_REV interrogate failed");
77                         return;
78                 }
79
80                 tsf_info = (struct acx_tsf_info *)&(cmd.parameters);
81
82                 mactime = tsf_info->current_tsf_lsb |
83                         (tsf_info->current_tsf_msb << 31);
84
85                 status->mactime = mactime;
86         }
87
88         status->signal = desc->rssi;
89         status->qual = (desc->rssi - WL12XX_RX_MIN_RSSI) * 100 /
90                 (WL12XX_RX_MAX_RSSI - WL12XX_RX_MIN_RSSI);
91         status->qual = min(status->qual, 100);
92         status->qual = max(status->qual, 0);
93
94         /*
95          * FIXME: guessing that snr needs to be divided by two, otherwise
96          * the values don't make any sense
97          */
98         status->noise = desc->rssi - desc->snr / 2;
99
100         status->freq = ieee80211_channel_to_frequency(desc->channel);
101
102         status->flag |= RX_FLAG_TSFT;
103
104         if (desc->flags & RX_DESC_ENCRYPTION_MASK) {
105                 status->flag |= RX_FLAG_IV_STRIPPED | RX_FLAG_MMIC_STRIPPED;
106
107                 if (likely(!(desc->flags & RX_DESC_DECRYPT_FAIL)))
108                         status->flag |= RX_FLAG_DECRYPTED;
109
110                 if (unlikely(desc->flags & RX_DESC_MIC_FAIL))
111                         status->flag |= RX_FLAG_MMIC_ERROR;
112         }
113
114         if (unlikely(!(desc->flags & RX_DESC_VALID_FCS)))
115                 status->flag |= RX_FLAG_FAILED_FCS_CRC;
116
117
118         /* FIXME: set status->rate_idx */
119 }
120
121 static void wl12xx_rx_body(struct wl12xx *wl,
122                            struct wl12xx_rx_descriptor *desc)
123 {
124         struct sk_buff *skb;
125         struct ieee80211_rx_status status;
126         u8 *rx_buffer, beacon = 0;
127         u16 length, *fc;
128         u32 curr_id, last_id_inc, rx_packet_ring_addr;
129
130         length = WL12XX_RX_ALIGN(desc->length  - PLCP_HEADER_LENGTH);
131         curr_id = (desc->flags & RX_DESC_SEQNUM_MASK) >> RX_DESC_PACKETID_SHIFT;
132         last_id_inc = (wl->rx_last_id + 1) % (RX_MAX_PACKET_ID + 1);
133
134         if (last_id_inc != curr_id) {
135                 wl12xx_warning("curr ID:%d, last ID inc:%d",
136                                curr_id, last_id_inc);
137                 wl->rx_last_id = curr_id;
138         } else {
139                 wl->rx_last_id = last_id_inc;
140         }
141
142         rx_packet_ring_addr = wl->data_path->rx_packet_ring_addr +
143                 sizeof(struct wl12xx_rx_descriptor) + 20;
144         if (wl->rx_current_buffer)
145                 rx_packet_ring_addr += wl->data_path->rx_packet_ring_chunk_size;
146
147         skb = dev_alloc_skb(length);
148         if (!skb) {
149                 wl12xx_error("Couldn't allocate RX frame");
150                 return;
151         }
152
153         rx_buffer = skb_put(skb, length);
154         wl12xx_spi_mem_read(wl, rx_packet_ring_addr, rx_buffer, length);
155
156         /* The actual lenght doesn't include the target's alignment */
157         skb->len = desc->length  - PLCP_HEADER_LENGTH;
158
159         fc = (u16 *)skb->data;
160
161         if ((*fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON)
162                 beacon = 1;
163
164         wl12xx_rx_status(wl, desc, &status, beacon);
165
166         wl12xx_debug(DEBUG_RX, "rx skb 0x%p: %d B %s", skb, skb->len,
167                      beacon ? "beacon" : "");
168
169         ieee80211_rx(wl->hw, skb, &status);
170 }
171
172 static void wl12xx_rx_ack(struct wl12xx *wl)
173 {
174         u32 data, addr;
175
176         if (wl->rx_current_buffer) {
177                 addr = ACX_REG_INTERRUPT_TRIG_H;
178                 data = INTR_TRIG_RX_PROC1;
179         } else {
180                 addr = ACX_REG_INTERRUPT_TRIG;
181                 data = INTR_TRIG_RX_PROC0;
182         }
183
184         wl12xx_reg_write32(wl, addr, data);
185
186         /* Toggle buffer ring */
187         wl->rx_current_buffer = !wl->rx_current_buffer;
188 }
189
190
191 void wl12xx_rx(struct wl12xx *wl)
192 {
193         struct wl12xx_rx_descriptor rx_desc;
194
195         if (wl->state != WL12XX_STATE_ON)
196                 return;
197
198         /* We first read the frame's header */
199         wl12xx_rx_header(wl, &rx_desc);
200
201         /* Now we can read the body */
202         wl12xx_rx_body(wl, &rx_desc);
203
204         /* Finally, we need to ACK the RX */
205         wl12xx_rx_ack(wl);
206
207         return;
208 }