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