Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 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 | |
Kalle Valo | 4721213 | 2009-06-12 14:15:08 +0300 | [diff] [blame^] | 42 | wl12xx_spi_mem_read(wl, rx_packet_ring_addr, desc, sizeof(*desc)); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static void wl12xx_rx_status(struct wl12xx *wl, |
| 46 | struct wl12xx_rx_descriptor *desc, |
| 47 | struct ieee80211_rx_status *status, |
| 48 | u8 beacon) |
| 49 | { |
Kalle Valo | ff25839 | 2009-06-12 14:14:19 +0300 | [diff] [blame] | 50 | u64 mactime; |
| 51 | int ret; |
| 52 | |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 53 | memset(status, 0, sizeof(struct ieee80211_rx_status)); |
| 54 | |
| 55 | status->band = IEEE80211_BAND_2GHZ; |
| 56 | status->mactime = desc->timestamp; |
| 57 | |
| 58 | /* |
| 59 | * The rx status timestamp is a 32 bits value while the TSF is a |
| 60 | * 64 bits one. |
| 61 | * For IBSS merging, TSF is mandatory, so we have to get it |
| 62 | * somehow, so we ask for ACX_TSF_INFO. |
| 63 | * That could be moved to the get_tsf() hook, but unfortunately, |
| 64 | * this one must be atomic, while our SPI routines can sleep. |
| 65 | */ |
| 66 | if ((wl->bss_type == BSS_TYPE_IBSS) && beacon) { |
Kalle Valo | ff25839 | 2009-06-12 14:14:19 +0300 | [diff] [blame] | 67 | ret = wl12xx_acx_tsf_info(wl, &mactime); |
| 68 | if (ret == 0) |
| 69 | status->mactime = mactime; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | status->signal = desc->rssi; |
| 73 | status->qual = (desc->rssi - WL12XX_RX_MIN_RSSI) * 100 / |
| 74 | (WL12XX_RX_MAX_RSSI - WL12XX_RX_MIN_RSSI); |
| 75 | status->qual = min(status->qual, 100); |
| 76 | status->qual = max(status->qual, 0); |
| 77 | |
| 78 | /* |
| 79 | * FIXME: guessing that snr needs to be divided by two, otherwise |
| 80 | * the values don't make any sense |
| 81 | */ |
| 82 | status->noise = desc->rssi - desc->snr / 2; |
| 83 | |
| 84 | status->freq = ieee80211_channel_to_frequency(desc->channel); |
| 85 | |
| 86 | status->flag |= RX_FLAG_TSFT; |
| 87 | |
| 88 | if (desc->flags & RX_DESC_ENCRYPTION_MASK) { |
| 89 | status->flag |= RX_FLAG_IV_STRIPPED | RX_FLAG_MMIC_STRIPPED; |
| 90 | |
| 91 | if (likely(!(desc->flags & RX_DESC_DECRYPT_FAIL))) |
| 92 | status->flag |= RX_FLAG_DECRYPTED; |
| 93 | |
| 94 | if (unlikely(desc->flags & RX_DESC_MIC_FAIL)) |
| 95 | status->flag |= RX_FLAG_MMIC_ERROR; |
| 96 | } |
| 97 | |
| 98 | if (unlikely(!(desc->flags & RX_DESC_VALID_FCS))) |
| 99 | status->flag |= RX_FLAG_FAILED_FCS_CRC; |
| 100 | |
| 101 | |
| 102 | /* FIXME: set status->rate_idx */ |
| 103 | } |
| 104 | |
| 105 | static void wl12xx_rx_body(struct wl12xx *wl, |
| 106 | struct wl12xx_rx_descriptor *desc) |
| 107 | { |
| 108 | struct sk_buff *skb; |
| 109 | struct ieee80211_rx_status status; |
| 110 | u8 *rx_buffer, beacon = 0; |
| 111 | u16 length, *fc; |
| 112 | u32 curr_id, last_id_inc, rx_packet_ring_addr; |
| 113 | |
| 114 | length = WL12XX_RX_ALIGN(desc->length - PLCP_HEADER_LENGTH); |
| 115 | curr_id = (desc->flags & RX_DESC_SEQNUM_MASK) >> RX_DESC_PACKETID_SHIFT; |
| 116 | last_id_inc = (wl->rx_last_id + 1) % (RX_MAX_PACKET_ID + 1); |
| 117 | |
| 118 | if (last_id_inc != curr_id) { |
| 119 | wl12xx_warning("curr ID:%d, last ID inc:%d", |
| 120 | curr_id, last_id_inc); |
| 121 | wl->rx_last_id = curr_id; |
| 122 | } else { |
| 123 | wl->rx_last_id = last_id_inc; |
| 124 | } |
| 125 | |
| 126 | rx_packet_ring_addr = wl->data_path->rx_packet_ring_addr + |
| 127 | sizeof(struct wl12xx_rx_descriptor) + 20; |
| 128 | if (wl->rx_current_buffer) |
| 129 | rx_packet_ring_addr += wl->data_path->rx_packet_ring_chunk_size; |
| 130 | |
| 131 | skb = dev_alloc_skb(length); |
| 132 | if (!skb) { |
| 133 | wl12xx_error("Couldn't allocate RX frame"); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | rx_buffer = skb_put(skb, length); |
| 138 | wl12xx_spi_mem_read(wl, rx_packet_ring_addr, rx_buffer, length); |
| 139 | |
| 140 | /* The actual lenght doesn't include the target's alignment */ |
| 141 | skb->len = desc->length - PLCP_HEADER_LENGTH; |
| 142 | |
| 143 | fc = (u16 *)skb->data; |
| 144 | |
| 145 | if ((*fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON) |
| 146 | beacon = 1; |
| 147 | |
| 148 | wl12xx_rx_status(wl, desc, &status, beacon); |
| 149 | |
| 150 | wl12xx_debug(DEBUG_RX, "rx skb 0x%p: %d B %s", skb, skb->len, |
| 151 | beacon ? "beacon" : ""); |
| 152 | |
| 153 | ieee80211_rx(wl->hw, skb, &status); |
| 154 | } |
| 155 | |
| 156 | static void wl12xx_rx_ack(struct wl12xx *wl) |
| 157 | { |
| 158 | u32 data, addr; |
| 159 | |
| 160 | if (wl->rx_current_buffer) { |
| 161 | addr = ACX_REG_INTERRUPT_TRIG_H; |
| 162 | data = INTR_TRIG_RX_PROC1; |
| 163 | } else { |
| 164 | addr = ACX_REG_INTERRUPT_TRIG; |
| 165 | data = INTR_TRIG_RX_PROC0; |
| 166 | } |
| 167 | |
| 168 | wl12xx_reg_write32(wl, addr, data); |
| 169 | |
| 170 | /* Toggle buffer ring */ |
| 171 | wl->rx_current_buffer = !wl->rx_current_buffer; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | void wl12xx_rx(struct wl12xx *wl) |
| 176 | { |
Kalle Valo | 4721213 | 2009-06-12 14:15:08 +0300 | [diff] [blame^] | 177 | struct wl12xx_rx_descriptor *rx_desc; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 178 | |
| 179 | if (wl->state != WL12XX_STATE_ON) |
| 180 | return; |
| 181 | |
Kalle Valo | 4721213 | 2009-06-12 14:15:08 +0300 | [diff] [blame^] | 182 | rx_desc = wl->rx_descriptor; |
| 183 | |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 184 | /* We first read the frame's header */ |
Kalle Valo | 4721213 | 2009-06-12 14:15:08 +0300 | [diff] [blame^] | 185 | wl12xx_rx_header(wl, rx_desc); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 186 | |
| 187 | /* Now we can read the body */ |
Kalle Valo | 4721213 | 2009-06-12 14:15:08 +0300 | [diff] [blame^] | 188 | wl12xx_rx_body(wl, rx_desc); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 189 | |
| 190 | /* Finally, we need to ACK the RX */ |
| 191 | wl12xx_rx_ack(wl); |
| 192 | |
| 193 | return; |
| 194 | } |