Daniel Drake | 66bb42f | 2007-11-19 16:20:12 +0000 | [diff] [blame] | 1 | /* ZD1211 USB-WLAN driver for Linux |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 2 | * |
Daniel Drake | 66bb42f | 2007-11-19 16:20:12 +0000 | [diff] [blame] | 3 | * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de> |
| 4 | * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org> |
| 5 | * Copyright (C) 2006-2007 Michael Wu <flamingice@sourmilk.net> |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 6 | * Copyright (c) 2007 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu> |
| 7 | * |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/netdevice.h> |
| 24 | #include <linux/etherdevice.h> |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 25 | #include <linux/usb.h> |
| 26 | #include <linux/jiffies.h> |
| 27 | #include <net/ieee80211_radiotap.h> |
| 28 | |
| 29 | #include "zd_def.h" |
| 30 | #include "zd_chip.h" |
| 31 | #include "zd_mac.h" |
| 32 | #include "zd_ieee80211.h" |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 33 | #include "zd_rf.h" |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 34 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 35 | /* This table contains the hardware specific values for the modulation rates. */ |
| 36 | static const struct ieee80211_rate zd_rates[] = { |
| 37 | { .rate = 10, |
| 38 | .val = ZD_CCK_RATE_1M, |
| 39 | .flags = IEEE80211_RATE_CCK }, |
| 40 | { .rate = 20, |
| 41 | .val = ZD_CCK_RATE_2M, |
| 42 | .val2 = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT, |
| 43 | .flags = IEEE80211_RATE_CCK_2 }, |
| 44 | { .rate = 55, |
| 45 | .val = ZD_CCK_RATE_5_5M, |
| 46 | .val2 = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT, |
| 47 | .flags = IEEE80211_RATE_CCK_2 }, |
| 48 | { .rate = 110, |
| 49 | .val = ZD_CCK_RATE_11M, |
| 50 | .val2 = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT, |
| 51 | .flags = IEEE80211_RATE_CCK_2 }, |
| 52 | { .rate = 60, |
| 53 | .val = ZD_OFDM_RATE_6M, |
| 54 | .flags = IEEE80211_RATE_OFDM }, |
| 55 | { .rate = 90, |
| 56 | .val = ZD_OFDM_RATE_9M, |
| 57 | .flags = IEEE80211_RATE_OFDM }, |
| 58 | { .rate = 120, |
| 59 | .val = ZD_OFDM_RATE_12M, |
| 60 | .flags = IEEE80211_RATE_OFDM }, |
| 61 | { .rate = 180, |
| 62 | .val = ZD_OFDM_RATE_18M, |
| 63 | .flags = IEEE80211_RATE_OFDM }, |
| 64 | { .rate = 240, |
| 65 | .val = ZD_OFDM_RATE_24M, |
| 66 | .flags = IEEE80211_RATE_OFDM }, |
| 67 | { .rate = 360, |
| 68 | .val = ZD_OFDM_RATE_36M, |
| 69 | .flags = IEEE80211_RATE_OFDM }, |
| 70 | { .rate = 480, |
| 71 | .val = ZD_OFDM_RATE_48M, |
| 72 | .flags = IEEE80211_RATE_OFDM }, |
| 73 | { .rate = 540, |
| 74 | .val = ZD_OFDM_RATE_54M, |
| 75 | .flags = IEEE80211_RATE_OFDM }, |
| 76 | }; |
| 77 | |
| 78 | static const struct ieee80211_channel zd_channels[] = { |
| 79 | { .chan = 1, |
| 80 | .freq = 2412}, |
| 81 | { .chan = 2, |
| 82 | .freq = 2417}, |
| 83 | { .chan = 3, |
| 84 | .freq = 2422}, |
| 85 | { .chan = 4, |
| 86 | .freq = 2427}, |
| 87 | { .chan = 5, |
| 88 | .freq = 2432}, |
| 89 | { .chan = 6, |
| 90 | .freq = 2437}, |
| 91 | { .chan = 7, |
| 92 | .freq = 2442}, |
| 93 | { .chan = 8, |
| 94 | .freq = 2447}, |
| 95 | { .chan = 9, |
| 96 | .freq = 2452}, |
| 97 | { .chan = 10, |
| 98 | .freq = 2457}, |
| 99 | { .chan = 11, |
| 100 | .freq = 2462}, |
| 101 | { .chan = 12, |
| 102 | .freq = 2467}, |
| 103 | { .chan = 13, |
| 104 | .freq = 2472}, |
| 105 | { .chan = 14, |
| 106 | .freq = 2484} |
| 107 | }; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 108 | |
Ulrich Kunitz | 583afd1 | 2006-09-13 02:42:38 +0100 | [diff] [blame] | 109 | static void housekeeping_init(struct zd_mac *mac); |
| 110 | static void housekeeping_enable(struct zd_mac *mac); |
| 111 | static void housekeeping_disable(struct zd_mac *mac); |
| 112 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 113 | int zd_mac_preinit_hw(struct ieee80211_hw *hw) |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 114 | { |
| 115 | int r; |
| 116 | u8 addr[ETH_ALEN]; |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 117 | struct zd_mac *mac = zd_hw_mac(hw); |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 118 | |
| 119 | r = zd_chip_read_mac_addr_fw(&mac->chip, addr); |
| 120 | if (r) |
| 121 | return r; |
| 122 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 123 | SET_IEEE80211_PERM_ADDR(hw, addr); |
| 124 | |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 128 | int zd_mac_init_hw(struct ieee80211_hw *hw) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 129 | { |
| 130 | int r; |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 131 | struct zd_mac *mac = zd_hw_mac(hw); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 132 | struct zd_chip *chip = &mac->chip; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 133 | u8 default_regdomain; |
| 134 | |
| 135 | r = zd_chip_enable_int(chip); |
| 136 | if (r) |
| 137 | goto out; |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 138 | r = zd_chip_init_hw(chip); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 139 | if (r) |
| 140 | goto disable_int; |
| 141 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 142 | ZD_ASSERT(!irqs_disabled()); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 143 | |
| 144 | r = zd_read_regdomain(chip, &default_regdomain); |
| 145 | if (r) |
| 146 | goto disable_int; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 147 | spin_lock_irq(&mac->lock); |
| 148 | mac->regdomain = mac->default_regdomain = default_regdomain; |
| 149 | spin_unlock_irq(&mac->lock); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 150 | |
Daniel Drake | 40da08b | 2006-08-01 23:43:32 +0200 | [diff] [blame] | 151 | /* We must inform the device that we are doing encryption/decryption in |
| 152 | * software at the moment. */ |
| 153 | r = zd_set_encryption_type(chip, ENC_SNIFFER); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 154 | if (r) |
| 155 | goto disable_int; |
| 156 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 157 | zd_geo_init(hw, mac->regdomain); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 158 | |
| 159 | r = 0; |
| 160 | disable_int: |
| 161 | zd_chip_disable_int(chip); |
| 162 | out: |
| 163 | return r; |
| 164 | } |
| 165 | |
| 166 | void zd_mac_clear(struct zd_mac *mac) |
| 167 | { |
Ulrich Kunitz | 9cdac96 | 2006-12-01 00:58:07 +0000 | [diff] [blame] | 168 | flush_workqueue(zd_workqueue); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 169 | zd_chip_clear(&mac->chip); |
Ulrich Kunitz | c48cf12 | 2006-08-12 18:00:17 +0100 | [diff] [blame] | 170 | ZD_ASSERT(!spin_is_locked(&mac->lock)); |
| 171 | ZD_MEMCLEAR(mac, sizeof(struct zd_mac)); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Ulrich Kunitz | c569123 | 2007-07-21 22:42:13 +0100 | [diff] [blame] | 174 | static int set_rx_filter(struct zd_mac *mac) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 175 | { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 176 | unsigned long flags; |
| 177 | u32 filter = STA_RX_FILTER; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 178 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 179 | spin_lock_irqsave(&mac->lock, flags); |
| 180 | if (mac->pass_ctrl) |
| 181 | filter |= RX_FILTER_CTRL; |
| 182 | spin_unlock_irqrestore(&mac->lock, flags); |
| 183 | |
| 184 | return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter); |
Ulrich Kunitz | c569123 | 2007-07-21 22:42:13 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static int set_mc_hash(struct zd_mac *mac) |
| 188 | { |
| 189 | struct zd_mc_hash hash; |
Ulrich Kunitz | c569123 | 2007-07-21 22:42:13 +0100 | [diff] [blame] | 190 | zd_mc_clear(&hash); |
Ulrich Kunitz | c569123 | 2007-07-21 22:42:13 +0100 | [diff] [blame] | 191 | return zd_chip_set_multicast_hash(&mac->chip, &hash); |
| 192 | } |
| 193 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 194 | static int zd_op_start(struct ieee80211_hw *hw) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 195 | { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 196 | struct zd_mac *mac = zd_hw_mac(hw); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 197 | struct zd_chip *chip = &mac->chip; |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 198 | struct zd_usb *usb = &chip->usb; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 199 | int r; |
| 200 | |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 201 | if (!usb->initialized) { |
| 202 | r = zd_usb_init_hw(usb); |
| 203 | if (r) |
| 204 | goto out; |
| 205 | } |
| 206 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 207 | r = zd_chip_enable_int(chip); |
| 208 | if (r < 0) |
| 209 | goto out; |
| 210 | |
| 211 | r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G); |
| 212 | if (r < 0) |
| 213 | goto disable_int; |
Ulrich Kunitz | c569123 | 2007-07-21 22:42:13 +0100 | [diff] [blame] | 214 | r = set_rx_filter(mac); |
| 215 | if (r) |
| 216 | goto disable_int; |
Ulrich Kunitz | c569123 | 2007-07-21 22:42:13 +0100 | [diff] [blame] | 217 | r = set_mc_hash(mac); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 218 | if (r) |
| 219 | goto disable_int; |
| 220 | r = zd_chip_switch_radio_on(chip); |
| 221 | if (r < 0) |
| 222 | goto disable_int; |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 223 | r = zd_chip_enable_rxtx(chip); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 224 | if (r < 0) |
| 225 | goto disable_radio; |
| 226 | r = zd_chip_enable_hwint(chip); |
| 227 | if (r < 0) |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 228 | goto disable_rxtx; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 229 | |
Ulrich Kunitz | 583afd1 | 2006-09-13 02:42:38 +0100 | [diff] [blame] | 230 | housekeeping_enable(mac); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 231 | return 0; |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 232 | disable_rxtx: |
| 233 | zd_chip_disable_rxtx(chip); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 234 | disable_radio: |
| 235 | zd_chip_switch_radio_off(chip); |
| 236 | disable_int: |
| 237 | zd_chip_disable_int(chip); |
| 238 | out: |
| 239 | return r; |
| 240 | } |
| 241 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 242 | /** |
| 243 | * clear_tx_skb_control_block - clears the control block of tx skbuffs |
| 244 | * @skb: a &struct sk_buff pointer |
| 245 | * |
| 246 | * This clears the control block of skbuff buffers, which were transmitted to |
| 247 | * the device. Notify that the function is not thread-safe, so prevent |
| 248 | * multiple calls. |
| 249 | */ |
| 250 | static void clear_tx_skb_control_block(struct sk_buff *skb) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 251 | { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 252 | struct zd_tx_skb_control_block *cb = |
| 253 | (struct zd_tx_skb_control_block *)skb->cb; |
| 254 | |
| 255 | kfree(cb->control); |
| 256 | cb->control = NULL; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * kfree_tx_skb - frees a tx skbuff |
| 261 | * @skb: a &struct sk_buff pointer |
| 262 | * |
| 263 | * Frees the tx skbuff. Frees also the allocated control structure in the |
| 264 | * control block if necessary. |
| 265 | */ |
| 266 | static void kfree_tx_skb(struct sk_buff *skb) |
| 267 | { |
| 268 | clear_tx_skb_control_block(skb); |
| 269 | dev_kfree_skb_any(skb); |
| 270 | } |
| 271 | |
| 272 | static void zd_op_stop(struct ieee80211_hw *hw) |
| 273 | { |
| 274 | struct zd_mac *mac = zd_hw_mac(hw); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 275 | struct zd_chip *chip = &mac->chip; |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 276 | struct sk_buff *skb; |
| 277 | struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 278 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 279 | /* The order here deliberately is a little different from the open() |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 280 | * method, since we need to make sure there is no opportunity for RX |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 281 | * frames to be processed by mac80211 after we have stopped it. |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 282 | */ |
| 283 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 284 | zd_chip_disable_rxtx(chip); |
Ulrich Kunitz | 583afd1 | 2006-09-13 02:42:38 +0100 | [diff] [blame] | 285 | housekeeping_disable(mac); |
Daniel Drake | b1382ed | 2006-11-22 00:06:48 +0000 | [diff] [blame] | 286 | flush_workqueue(zd_workqueue); |
Daniel Drake | b1382ed | 2006-11-22 00:06:48 +0000 | [diff] [blame] | 287 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 288 | zd_chip_disable_hwint(chip); |
| 289 | zd_chip_switch_radio_off(chip); |
| 290 | zd_chip_disable_int(chip); |
| 291 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 292 | |
| 293 | while ((skb = skb_dequeue(ack_wait_queue))) |
| 294 | kfree_tx_skb(skb); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 295 | } |
| 296 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 297 | /** |
| 298 | * init_tx_skb_control_block - initializes skb control block |
| 299 | * @skb: a &sk_buff pointer |
| 300 | * @dev: pointer to the mac80221 device |
| 301 | * @control: mac80211 tx control applying for the frame in @skb |
| 302 | * |
| 303 | * Initializes the control block of the skbuff to be transmitted. |
| 304 | */ |
| 305 | static int init_tx_skb_control_block(struct sk_buff *skb, |
| 306 | struct ieee80211_hw *hw, |
| 307 | struct ieee80211_tx_control *control) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 308 | { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 309 | struct zd_tx_skb_control_block *cb = |
| 310 | (struct zd_tx_skb_control_block *)skb->cb; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 311 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 312 | ZD_ASSERT(sizeof(*cb) <= sizeof(skb->cb)); |
| 313 | memset(cb, 0, sizeof(*cb)); |
| 314 | cb->hw= hw; |
| 315 | cb->control = kmalloc(sizeof(*control), GFP_ATOMIC); |
| 316 | if (cb->control == NULL) |
| 317 | return -ENOMEM; |
| 318 | memcpy(cb->control, control, sizeof(*control)); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 323 | /** |
| 324 | * tx_status - reports tx status of a packet if required |
| 325 | * @hw - a &struct ieee80211_hw pointer |
| 326 | * @skb - a sk-buffer |
| 327 | * @status - the tx status of the packet without control information |
| 328 | * @success - True for successfull transmission of the frame |
| 329 | * |
| 330 | * This information calls ieee80211_tx_status_irqsafe() if required by the |
| 331 | * control information. It copies the control information into the status |
| 332 | * information. |
| 333 | * |
| 334 | * If no status information has been requested, the skb is freed. |
| 335 | */ |
| 336 | static void tx_status(struct ieee80211_hw *hw, struct sk_buff *skb, |
| 337 | struct ieee80211_tx_status *status, |
| 338 | bool success) |
Ulrich Kunitz | 9cdac96 | 2006-12-01 00:58:07 +0000 | [diff] [blame] | 339 | { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 340 | struct zd_tx_skb_control_block *cb = (struct zd_tx_skb_control_block *) |
| 341 | skb->cb; |
Ulrich Kunitz | 9cdac96 | 2006-12-01 00:58:07 +0000 | [diff] [blame] | 342 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 343 | ZD_ASSERT(cb->control != NULL); |
| 344 | memcpy(&status->control, cb->control, sizeof(status->control)); |
| 345 | if (!success) |
| 346 | status->excessive_retries = 1; |
| 347 | clear_tx_skb_control_block(skb); |
| 348 | ieee80211_tx_status_irqsafe(hw, skb, status); |
Ulrich Kunitz | 9cdac96 | 2006-12-01 00:58:07 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 351 | /** |
| 352 | * zd_mac_tx_failed - callback for failed frames |
| 353 | * @dev: the mac80211 wireless device |
| 354 | * |
| 355 | * This function is called if a frame couldn't be succesfully be |
| 356 | * transferred. The first frame from the tx queue, will be selected and |
| 357 | * reported as error to the upper layers. |
| 358 | */ |
| 359 | void zd_mac_tx_failed(struct ieee80211_hw *hw) |
Ulrich Kunitz | 9cdac96 | 2006-12-01 00:58:07 +0000 | [diff] [blame] | 360 | { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 361 | struct sk_buff_head *q = &zd_hw_mac(hw)->ack_wait_queue; |
| 362 | struct sk_buff *skb; |
Johannes Berg | 5078ed5 | 2008-02-20 12:09:25 +0100 | [diff] [blame^] | 363 | struct ieee80211_tx_status status; |
Ulrich Kunitz | 9cdac96 | 2006-12-01 00:58:07 +0000 | [diff] [blame] | 364 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 365 | skb = skb_dequeue(q); |
| 366 | if (skb == NULL) |
| 367 | return; |
Johannes Berg | 5078ed5 | 2008-02-20 12:09:25 +0100 | [diff] [blame^] | 368 | |
| 369 | memset(&status, 0, sizeof(status)); |
| 370 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 371 | tx_status(hw, skb, &status, 0); |
Ulrich Kunitz | 9cdac96 | 2006-12-01 00:58:07 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 374 | /** |
| 375 | * zd_mac_tx_to_dev - callback for USB layer |
| 376 | * @skb: a &sk_buff pointer |
| 377 | * @error: error value, 0 if transmission successful |
| 378 | * |
| 379 | * Informs the MAC layer that the frame has successfully transferred to the |
| 380 | * device. If an ACK is required and the transfer to the device has been |
| 381 | * successful, the packets are put on the @ack_wait_queue with |
| 382 | * the control set removed. |
| 383 | */ |
| 384 | void zd_mac_tx_to_dev(struct sk_buff *skb, int error) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 385 | { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 386 | struct zd_tx_skb_control_block *cb = |
| 387 | (struct zd_tx_skb_control_block *)skb->cb; |
| 388 | struct ieee80211_hw *hw = cb->hw; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 389 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 390 | if (likely(cb->control)) { |
| 391 | skb_pull(skb, sizeof(struct zd_ctrlset)); |
| 392 | if (unlikely(error || |
| 393 | (cb->control->flags & IEEE80211_TXCTL_NO_ACK))) |
| 394 | { |
Johannes Berg | 5078ed5 | 2008-02-20 12:09:25 +0100 | [diff] [blame^] | 395 | struct ieee80211_tx_status status; |
| 396 | memset(&status, 0, sizeof(status)); |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 397 | tx_status(hw, skb, &status, !error); |
Daniel Drake | b1382ed | 2006-11-22 00:06:48 +0000 | [diff] [blame] | 398 | } else { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 399 | struct sk_buff_head *q = |
| 400 | &zd_hw_mac(hw)->ack_wait_queue; |
Daniel Drake | b1382ed | 2006-11-22 00:06:48 +0000 | [diff] [blame] | 401 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 402 | skb_queue_tail(q, skb); |
| 403 | while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS) |
| 404 | zd_mac_tx_failed(hw); |
Daniel Drake | b1382ed | 2006-11-22 00:06:48 +0000 | [diff] [blame] | 405 | } |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 406 | } else { |
| 407 | kfree_tx_skb(skb); |
Daniel Drake | b1382ed | 2006-11-22 00:06:48 +0000 | [diff] [blame] | 408 | } |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 409 | } |
| 410 | |
Daniel Drake | b1cd8416 | 2006-11-22 00:06:38 +0000 | [diff] [blame] | 411 | static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 412 | { |
Ulrich Kunitz | 64f222c | 2007-08-06 01:24:31 +0100 | [diff] [blame] | 413 | /* ZD_PURE_RATE() must be used to remove the modulation type flag of |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 414 | * the zd-rate values. |
| 415 | */ |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 416 | static const u8 rate_divisor[] = { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 417 | [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1, |
| 418 | [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2, |
| 419 | /* Bits must be doubled. */ |
| 420 | [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11, |
| 421 | [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11, |
| 422 | [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6, |
| 423 | [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9, |
| 424 | [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12, |
| 425 | [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18, |
| 426 | [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24, |
| 427 | [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36, |
| 428 | [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48, |
| 429 | [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54, |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 430 | }; |
| 431 | |
| 432 | u32 bits = (u32)tx_length * 8; |
| 433 | u32 divisor; |
| 434 | |
Ulrich Kunitz | 64f222c | 2007-08-06 01:24:31 +0100 | [diff] [blame] | 435 | divisor = rate_divisor[ZD_PURE_RATE(zd_rate)]; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 436 | if (divisor == 0) |
| 437 | return -EINVAL; |
| 438 | |
Daniel Drake | b1cd8416 | 2006-11-22 00:06:38 +0000 | [diff] [blame] | 439 | switch (zd_rate) { |
| 440 | case ZD_CCK_RATE_5_5M: |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 441 | bits = (2*bits) + 10; /* round up to the next integer */ |
| 442 | break; |
Daniel Drake | b1cd8416 | 2006-11-22 00:06:38 +0000 | [diff] [blame] | 443 | case ZD_CCK_RATE_11M: |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 444 | if (service) { |
| 445 | u32 t = bits % 11; |
| 446 | *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION; |
| 447 | if (0 < t && t <= 3) { |
| 448 | *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION; |
| 449 | } |
| 450 | } |
| 451 | bits += 10; /* round up to the next integer */ |
| 452 | break; |
| 453 | } |
| 454 | |
| 455 | return bits/divisor; |
| 456 | } |
| 457 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 458 | static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs, |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 459 | struct ieee80211_hdr *header, u32 flags) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 460 | { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 461 | u16 fctl = le16_to_cpu(header->frame_control); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 462 | |
| 463 | /* |
Daniel Drake | b1382ed | 2006-11-22 00:06:48 +0000 | [diff] [blame] | 464 | * CONTROL TODO: |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 465 | * - if backoff needed, enable bit 0 |
| 466 | * - if burst (backoff not needed) disable bit 0 |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 467 | */ |
| 468 | |
| 469 | cs->control = 0; |
| 470 | |
| 471 | /* First fragment */ |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 472 | if (flags & IEEE80211_TXCTL_FIRST_FRAGMENT) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 473 | cs->control |= ZD_CS_NEED_RANDOM_BACKOFF; |
| 474 | |
| 475 | /* Multicast */ |
| 476 | if (is_multicast_ether_addr(header->addr1)) |
| 477 | cs->control |= ZD_CS_MULTICAST; |
| 478 | |
| 479 | /* PS-POLL */ |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 480 | if ((fctl & (IEEE80211_FCTL_FTYPE|IEEE80211_FCTL_STYPE)) == |
| 481 | (IEEE80211_FTYPE_CTL|IEEE80211_STYPE_PSPOLL)) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 482 | cs->control |= ZD_CS_PS_POLL_FRAME; |
| 483 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 484 | if (flags & IEEE80211_TXCTL_USE_RTS_CTS) |
Daniel Drake | b1382ed | 2006-11-22 00:06:48 +0000 | [diff] [blame] | 485 | cs->control |= ZD_CS_RTS; |
| 486 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 487 | if (flags & IEEE80211_TXCTL_USE_CTS_PROTECT) |
Daniel Drake | b1382ed | 2006-11-22 00:06:48 +0000 | [diff] [blame] | 488 | cs->control |= ZD_CS_SELF_CTS; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 489 | |
| 490 | /* FIXME: Management frame? */ |
| 491 | } |
| 492 | |
| 493 | static int fill_ctrlset(struct zd_mac *mac, |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 494 | struct sk_buff *skb, |
| 495 | struct ieee80211_tx_control *control) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 496 | { |
| 497 | int r; |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 498 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; |
| 499 | unsigned int frag_len = skb->len + FCS_LEN; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 500 | unsigned int packet_length; |
| 501 | struct zd_ctrlset *cs = (struct zd_ctrlset *) |
| 502 | skb_push(skb, sizeof(struct zd_ctrlset)); |
| 503 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 504 | ZD_ASSERT(frag_len <= 0xffff); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 505 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 506 | cs->modulation = control->tx_rate; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 507 | |
| 508 | cs->tx_length = cpu_to_le16(frag_len); |
| 509 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 510 | cs_set_control(mac, cs, hdr, control->flags); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 511 | |
| 512 | packet_length = frag_len + sizeof(struct zd_ctrlset) + 10; |
| 513 | ZD_ASSERT(packet_length <= 0xffff); |
| 514 | /* ZD1211B: Computing the length difference this way, gives us |
| 515 | * flexibility to compute the packet length. |
| 516 | */ |
Daniel Drake | 74553ae | 2007-07-01 18:22:32 +0100 | [diff] [blame] | 517 | cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ? |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 518 | packet_length - frag_len : packet_length); |
| 519 | |
| 520 | /* |
| 521 | * CURRENT LENGTH: |
| 522 | * - transmit frame length in microseconds |
| 523 | * - seems to be derived from frame length |
| 524 | * - see Cal_Us_Service() in zdinlinef.h |
| 525 | * - if macp->bTxBurstEnable is enabled, then multiply by 4 |
| 526 | * - bTxBurstEnable is never set in the vendor driver |
| 527 | * |
| 528 | * SERVICE: |
| 529 | * - "for PLCP configuration" |
| 530 | * - always 0 except in some situations at 802.11b 11M |
| 531 | * - see line 53 of zdinlinef.h |
| 532 | */ |
| 533 | cs->service = 0; |
Ulrich Kunitz | 64f222c | 2007-08-06 01:24:31 +0100 | [diff] [blame] | 534 | r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation), |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 535 | le16_to_cpu(cs->tx_length)); |
| 536 | if (r < 0) |
| 537 | return r; |
| 538 | cs->current_length = cpu_to_le16(r); |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 539 | cs->next_frame_length = 0; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 540 | |
| 541 | return 0; |
| 542 | } |
| 543 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 544 | /** |
| 545 | * zd_op_tx - transmits a network frame to the device |
Ulrich Kunitz | 741fec5 | 2006-11-22 00:05:53 +0000 | [diff] [blame] | 546 | * |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 547 | * @dev: mac80211 hardware device |
| 548 | * @skb: socket buffer |
| 549 | * @control: the control structure |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 550 | * |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 551 | * This function transmit an IEEE 802.11 network frame to the device. The |
| 552 | * control block of the skbuff will be initialized. If necessary the incoming |
| 553 | * mac80211 queues will be stopped. |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 554 | */ |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 555 | static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb, |
| 556 | struct ieee80211_tx_control *control) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 557 | { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 558 | struct zd_mac *mac = zd_hw_mac(hw); |
| 559 | int r; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 560 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 561 | r = fill_ctrlset(mac, skb, control); |
| 562 | if (r) |
| 563 | return r; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 564 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 565 | r = init_tx_skb_control_block(skb, hw, control); |
| 566 | if (r) |
| 567 | return r; |
| 568 | r = zd_usb_tx(&mac->chip.usb, skb); |
| 569 | if (r) { |
| 570 | clear_tx_skb_control_block(skb); |
| 571 | return r; |
| 572 | } |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * filter_ack - filters incoming packets for acknowledgements |
| 578 | * @dev: the mac80211 device |
| 579 | * @rx_hdr: received header |
| 580 | * @stats: the status for the received packet |
| 581 | * |
| 582 | * This functions looks for ACK packets and tries to match them with the |
| 583 | * frames in the tx queue. If a match is found the frame will be dequeued and |
| 584 | * the upper layers is informed about the successful transmission. If |
| 585 | * mac80211 queues have been stopped and the number of frames still to be |
| 586 | * transmitted is low the queues will be opened again. |
| 587 | * |
| 588 | * Returns 1 if the frame was an ACK, 0 if it was ignored. |
| 589 | */ |
| 590 | static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr, |
| 591 | struct ieee80211_rx_status *stats) |
| 592 | { |
| 593 | u16 fc = le16_to_cpu(rx_hdr->frame_control); |
| 594 | struct sk_buff *skb; |
| 595 | struct sk_buff_head *q; |
| 596 | unsigned long flags; |
| 597 | |
| 598 | if ((fc & (IEEE80211_FCTL_FTYPE | IEEE80211_FCTL_STYPE)) != |
| 599 | (IEEE80211_FTYPE_CTL | IEEE80211_STYPE_ACK)) |
| 600 | return 0; |
| 601 | |
| 602 | q = &zd_hw_mac(hw)->ack_wait_queue; |
| 603 | spin_lock_irqsave(&q->lock, flags); |
| 604 | for (skb = q->next; skb != (struct sk_buff *)q; skb = skb->next) { |
| 605 | struct ieee80211_hdr *tx_hdr; |
| 606 | |
| 607 | tx_hdr = (struct ieee80211_hdr *)skb->data; |
| 608 | if (likely(!compare_ether_addr(tx_hdr->addr2, rx_hdr->addr1))) |
| 609 | { |
Johannes Berg | 5078ed5 | 2008-02-20 12:09:25 +0100 | [diff] [blame^] | 610 | struct ieee80211_tx_status status; |
| 611 | |
| 612 | memset(&status, 0, sizeof(status)); |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 613 | status.flags = IEEE80211_TX_STATUS_ACK; |
| 614 | status.ack_signal = stats->ssi; |
| 615 | __skb_unlink(skb, q); |
| 616 | tx_status(hw, skb, &status, 1); |
| 617 | goto out; |
| 618 | } |
| 619 | } |
| 620 | out: |
| 621 | spin_unlock_irqrestore(&q->lock, flags); |
| 622 | return 1; |
| 623 | } |
| 624 | |
| 625 | int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length) |
| 626 | { |
| 627 | struct zd_mac *mac = zd_hw_mac(hw); |
| 628 | struct ieee80211_rx_status stats; |
| 629 | const struct rx_status *status; |
| 630 | struct sk_buff *skb; |
| 631 | int bad_frame = 0; |
Michael Buesch | 9081728 | 2007-12-29 17:24:23 +0100 | [diff] [blame] | 632 | u16 fc; |
| 633 | bool is_qos, is_4addr, need_padding; |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 634 | |
| 635 | if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ + |
| 636 | FCS_LEN + sizeof(struct rx_status)) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 637 | return -EINVAL; |
| 638 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 639 | memset(&stats, 0, sizeof(stats)); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 640 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 641 | /* Note about pass_failed_fcs and pass_ctrl access below: |
| 642 | * mac locking intentionally omitted here, as this is the only unlocked |
| 643 | * reader and the only writer is configure_filter. Plus, if there were |
| 644 | * any races accessing these variables, it wouldn't really matter. |
| 645 | * If mac80211 ever provides a way for us to access filter flags |
| 646 | * from outside configure_filter, we could improve on this. Also, this |
| 647 | * situation may change once we implement some kind of DMA-into-skb |
| 648 | * RX path. */ |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 649 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 650 | /* Caller has to ensure that length >= sizeof(struct rx_status). */ |
| 651 | status = (struct rx_status *) |
Ulrich Kunitz | 937a049 | 2007-10-02 18:36:53 +0100 | [diff] [blame] | 652 | (buffer + (length - sizeof(struct rx_status))); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 653 | if (status->frame_status & ZD_RX_ERROR) { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 654 | if (mac->pass_failed_fcs && |
| 655 | (status->frame_status & ZD_RX_CRC32_ERROR)) { |
| 656 | stats.flag |= RX_FLAG_FAILED_FCS_CRC; |
| 657 | bad_frame = 1; |
| 658 | } else { |
| 659 | return -EINVAL; |
Ulrich Kunitz | 22d3405 | 2007-01-29 01:00:03 +0000 | [diff] [blame] | 660 | } |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 661 | } |
Ulrich Kunitz | 22d3405 | 2007-01-29 01:00:03 +0000 | [diff] [blame] | 662 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 663 | stats.channel = _zd_chip_get_channel(&mac->chip); |
| 664 | stats.freq = zd_channels[stats.channel - 1].freq; |
| 665 | stats.phymode = MODE_IEEE80211G; |
| 666 | stats.ssi = status->signal_strength; |
| 667 | stats.signal = zd_rx_qual_percent(buffer, |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 668 | length - sizeof(struct rx_status), |
| 669 | status); |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 670 | stats.rate = zd_rx_rate(buffer, status); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 671 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 672 | length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status); |
| 673 | buffer += ZD_PLCP_HEADER_SIZE; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 674 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 675 | /* Except for bad frames, filter each frame to see if it is an ACK, in |
| 676 | * which case our internal TX tracking is updated. Normally we then |
| 677 | * bail here as there's no need to pass ACKs on up to the stack, but |
| 678 | * there is also the case where the stack has requested us to pass |
| 679 | * control frames on up (pass_ctrl) which we must consider. */ |
| 680 | if (!bad_frame && |
| 681 | filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats) |
| 682 | && !mac->pass_ctrl) |
| 683 | return 0; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 684 | |
Michael Buesch | 9081728 | 2007-12-29 17:24:23 +0100 | [diff] [blame] | 685 | fc = le16_to_cpu(*((__le16 *) buffer)); |
| 686 | |
| 687 | is_qos = ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) && |
| 688 | ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_QOS_DATA); |
| 689 | is_4addr = (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) == |
| 690 | (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS); |
| 691 | need_padding = is_qos ^ is_4addr; |
| 692 | |
| 693 | skb = dev_alloc_skb(length + (need_padding ? 2 : 0)); |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 694 | if (skb == NULL) |
Ulrich Kunitz | 4d1feab | 2006-12-10 11:13:12 -0800 | [diff] [blame] | 695 | return -ENOMEM; |
Michael Buesch | 9081728 | 2007-12-29 17:24:23 +0100 | [diff] [blame] | 696 | if (need_padding) { |
| 697 | /* Make sure the the payload data is 4 byte aligned. */ |
| 698 | skb_reserve(skb, 2); |
| 699 | } |
| 700 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 701 | memcpy(skb_put(skb, length), buffer, length); |
| 702 | |
| 703 | ieee80211_rx_irqsafe(hw, skb, &stats); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 704 | return 0; |
| 705 | } |
| 706 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 707 | static int zd_op_add_interface(struct ieee80211_hw *hw, |
| 708 | struct ieee80211_if_init_conf *conf) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 709 | { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 710 | struct zd_mac *mac = zd_hw_mac(hw); |
| 711 | |
| 712 | /* using IEEE80211_IF_TYPE_INVALID to indicate no mode selected */ |
| 713 | if (mac->type != IEEE80211_IF_TYPE_INVALID) |
| 714 | return -EOPNOTSUPP; |
| 715 | |
| 716 | switch (conf->type) { |
| 717 | case IEEE80211_IF_TYPE_MNTR: |
| 718 | case IEEE80211_IF_TYPE_STA: |
| 719 | mac->type = conf->type; |
| 720 | break; |
| 721 | default: |
| 722 | return -EOPNOTSUPP; |
| 723 | } |
| 724 | |
| 725 | return zd_write_mac_addr(&mac->chip, conf->mac_addr); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 726 | } |
| 727 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 728 | static void zd_op_remove_interface(struct ieee80211_hw *hw, |
| 729 | struct ieee80211_if_init_conf *conf) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 730 | { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 731 | struct zd_mac *mac = zd_hw_mac(hw); |
| 732 | mac->type = IEEE80211_IF_TYPE_INVALID; |
| 733 | zd_write_mac_addr(&mac->chip, NULL); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 734 | } |
| 735 | |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 736 | static int zd_op_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 737 | { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 738 | struct zd_mac *mac = zd_hw_mac(hw); |
| 739 | return zd_chip_set_channel(&mac->chip, conf->channel); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 740 | } |
| 741 | |
Johannes Berg | 32bfd35 | 2007-12-19 01:31:26 +0100 | [diff] [blame] | 742 | static int zd_op_config_interface(struct ieee80211_hw *hw, |
| 743 | struct ieee80211_vif *vif, |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 744 | struct ieee80211_if_conf *conf) |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 745 | { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 746 | struct zd_mac *mac = zd_hw_mac(hw); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 747 | |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 748 | spin_lock_irq(&mac->lock); |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 749 | mac->associated = is_valid_ether_addr(conf->bssid); |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 750 | spin_unlock_irq(&mac->lock); |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 751 | |
| 752 | /* TODO: do hardware bssid filtering */ |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | static void set_multicast_hash_handler(struct work_struct *work) |
| 757 | { |
| 758 | struct zd_mac *mac = |
| 759 | container_of(work, struct zd_mac, set_multicast_hash_work); |
| 760 | struct zd_mc_hash hash; |
| 761 | |
| 762 | spin_lock_irq(&mac->lock); |
| 763 | hash = mac->multicast_hash; |
| 764 | spin_unlock_irq(&mac->lock); |
| 765 | |
| 766 | zd_chip_set_multicast_hash(&mac->chip, &hash); |
| 767 | } |
| 768 | |
| 769 | static void set_rx_filter_handler(struct work_struct *work) |
| 770 | { |
| 771 | struct zd_mac *mac = |
| 772 | container_of(work, struct zd_mac, set_rx_filter_work); |
| 773 | int r; |
| 774 | |
| 775 | dev_dbg_f(zd_mac_dev(mac), "\n"); |
| 776 | r = set_rx_filter(mac); |
| 777 | if (r) |
| 778 | dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r); |
| 779 | } |
| 780 | |
| 781 | #define SUPPORTED_FIF_FLAGS \ |
| 782 | (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \ |
| 783 | FIF_OTHER_BSS) |
| 784 | static void zd_op_configure_filter(struct ieee80211_hw *hw, |
| 785 | unsigned int changed_flags, |
| 786 | unsigned int *new_flags, |
| 787 | int mc_count, struct dev_mc_list *mclist) |
| 788 | { |
| 789 | struct zd_mc_hash hash; |
| 790 | struct zd_mac *mac = zd_hw_mac(hw); |
| 791 | unsigned long flags; |
| 792 | int i; |
| 793 | |
| 794 | /* Only deal with supported flags */ |
| 795 | changed_flags &= SUPPORTED_FIF_FLAGS; |
| 796 | *new_flags &= SUPPORTED_FIF_FLAGS; |
| 797 | |
| 798 | /* changed_flags is always populated but this driver |
| 799 | * doesn't support all FIF flags so its possible we don't |
| 800 | * need to do anything */ |
| 801 | if (!changed_flags) |
| 802 | return; |
| 803 | |
| 804 | if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)) { |
| 805 | zd_mc_add_all(&hash); |
Ulrich Kunitz | db888ae | 2006-08-29 23:50:29 +0100 | [diff] [blame] | 806 | } else { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 807 | DECLARE_MAC_BUF(macbuf); |
| 808 | |
| 809 | zd_mc_clear(&hash); |
| 810 | for (i = 0; i < mc_count; i++) { |
| 811 | if (!mclist) |
| 812 | break; |
| 813 | dev_dbg_f(zd_mac_dev(mac), "mc addr %s\n", |
| 814 | print_mac(macbuf, mclist->dmi_addr)); |
| 815 | zd_mc_add_addr(&hash, mclist->dmi_addr); |
| 816 | mclist = mclist->next; |
| 817 | } |
Ulrich Kunitz | db888ae | 2006-08-29 23:50:29 +0100 | [diff] [blame] | 818 | } |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 819 | |
| 820 | spin_lock_irqsave(&mac->lock, flags); |
| 821 | mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL); |
| 822 | mac->pass_ctrl = !!(*new_flags & FIF_CONTROL); |
| 823 | mac->multicast_hash = hash; |
| 824 | spin_unlock_irqrestore(&mac->lock, flags); |
| 825 | queue_work(zd_workqueue, &mac->set_multicast_hash_work); |
| 826 | |
| 827 | if (changed_flags & FIF_CONTROL) |
| 828 | queue_work(zd_workqueue, &mac->set_rx_filter_work); |
| 829 | |
| 830 | /* no handling required for FIF_OTHER_BSS as we don't currently |
| 831 | * do BSSID filtering */ |
| 832 | /* FIXME: in future it would be nice to enable the probe response |
| 833 | * filter (so that the driver doesn't see them) until |
| 834 | * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd |
| 835 | * have to schedule work to enable prbresp reception, which might |
| 836 | * happen too late. For now we'll just listen and forward them all the |
| 837 | * time. */ |
| 838 | } |
| 839 | |
| 840 | static void set_rts_cts_work(struct work_struct *work) |
| 841 | { |
| 842 | struct zd_mac *mac = |
| 843 | container_of(work, struct zd_mac, set_rts_cts_work); |
| 844 | unsigned long flags; |
| 845 | unsigned int short_preamble; |
| 846 | |
| 847 | mutex_lock(&mac->chip.mutex); |
| 848 | |
| 849 | spin_lock_irqsave(&mac->lock, flags); |
| 850 | mac->updating_rts_rate = 0; |
| 851 | short_preamble = mac->short_preamble; |
| 852 | spin_unlock_irqrestore(&mac->lock, flags); |
| 853 | |
| 854 | zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble); |
| 855 | mutex_unlock(&mac->chip.mutex); |
| 856 | } |
| 857 | |
Johannes Berg | 471b3ef | 2007-12-28 14:32:58 +0100 | [diff] [blame] | 858 | static void zd_op_bss_info_changed(struct ieee80211_hw *hw, |
| 859 | struct ieee80211_vif *vif, |
| 860 | struct ieee80211_bss_conf *bss_conf, |
| 861 | u32 changes) |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 862 | { |
| 863 | struct zd_mac *mac = zd_hw_mac(hw); |
| 864 | unsigned long flags; |
| 865 | |
| 866 | dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes); |
| 867 | |
Johannes Berg | 471b3ef | 2007-12-28 14:32:58 +0100 | [diff] [blame] | 868 | if (changes & BSS_CHANGED_ERP_PREAMBLE) { |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 869 | spin_lock_irqsave(&mac->lock, flags); |
Johannes Berg | 471b3ef | 2007-12-28 14:32:58 +0100 | [diff] [blame] | 870 | mac->short_preamble = bss_conf->use_short_preamble; |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 871 | if (!mac->updating_rts_rate) { |
| 872 | mac->updating_rts_rate = 1; |
| 873 | /* FIXME: should disable TX here, until work has |
| 874 | * completed and RTS_CTS reg is updated */ |
| 875 | queue_work(zd_workqueue, &mac->set_rts_cts_work); |
| 876 | } |
| 877 | spin_unlock_irqrestore(&mac->lock, flags); |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | static const struct ieee80211_ops zd_ops = { |
| 882 | .tx = zd_op_tx, |
| 883 | .start = zd_op_start, |
| 884 | .stop = zd_op_stop, |
| 885 | .add_interface = zd_op_add_interface, |
| 886 | .remove_interface = zd_op_remove_interface, |
| 887 | .config = zd_op_config, |
| 888 | .config_interface = zd_op_config_interface, |
| 889 | .configure_filter = zd_op_configure_filter, |
Johannes Berg | 471b3ef | 2007-12-28 14:32:58 +0100 | [diff] [blame] | 890 | .bss_info_changed = zd_op_bss_info_changed, |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 891 | }; |
| 892 | |
| 893 | struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf) |
| 894 | { |
| 895 | struct zd_mac *mac; |
| 896 | struct ieee80211_hw *hw; |
| 897 | int i; |
| 898 | |
| 899 | hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops); |
| 900 | if (!hw) { |
| 901 | dev_dbg_f(&intf->dev, "out of memory\n"); |
| 902 | return NULL; |
| 903 | } |
| 904 | |
| 905 | mac = zd_hw_mac(hw); |
| 906 | |
| 907 | memset(mac, 0, sizeof(*mac)); |
| 908 | spin_lock_init(&mac->lock); |
| 909 | mac->hw = hw; |
| 910 | |
| 911 | mac->type = IEEE80211_IF_TYPE_INVALID; |
| 912 | |
| 913 | memcpy(mac->channels, zd_channels, sizeof(zd_channels)); |
| 914 | memcpy(mac->rates, zd_rates, sizeof(zd_rates)); |
| 915 | mac->modes[0].mode = MODE_IEEE80211G; |
| 916 | mac->modes[0].num_rates = ARRAY_SIZE(zd_rates); |
| 917 | mac->modes[0].rates = mac->rates; |
| 918 | mac->modes[0].num_channels = ARRAY_SIZE(zd_channels); |
| 919 | mac->modes[0].channels = mac->channels; |
| 920 | mac->modes[1].mode = MODE_IEEE80211B; |
| 921 | mac->modes[1].num_rates = 4; |
| 922 | mac->modes[1].rates = mac->rates; |
| 923 | mac->modes[1].num_channels = ARRAY_SIZE(zd_channels); |
| 924 | mac->modes[1].channels = mac->channels; |
| 925 | |
| 926 | hw->flags = IEEE80211_HW_RX_INCLUDES_FCS | |
| 927 | IEEE80211_HW_DEFAULT_REG_DOMAIN_CONFIGURED; |
| 928 | hw->max_rssi = 100; |
| 929 | hw->max_signal = 100; |
| 930 | |
| 931 | hw->queues = 1; |
| 932 | hw->extra_tx_headroom = sizeof(struct zd_ctrlset); |
| 933 | |
| 934 | skb_queue_head_init(&mac->ack_wait_queue); |
| 935 | |
| 936 | for (i = 0; i < 2; i++) { |
| 937 | if (ieee80211_register_hwmode(hw, &mac->modes[i])) { |
| 938 | dev_dbg_f(&intf->dev, "cannot register hwmode\n"); |
| 939 | ieee80211_free_hw(hw); |
| 940 | return NULL; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | zd_chip_init(&mac->chip, hw, intf); |
| 945 | housekeeping_init(mac); |
| 946 | INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler); |
| 947 | INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work); |
| 948 | INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler); |
| 949 | |
| 950 | SET_IEEE80211_DEV(hw, &intf->dev); |
| 951 | return hw; |
Daniel Drake | e85d091 | 2006-06-02 17:11:32 +0100 | [diff] [blame] | 952 | } |
| 953 | |
Ulrich Kunitz | 583afd1 | 2006-09-13 02:42:38 +0100 | [diff] [blame] | 954 | #define LINK_LED_WORK_DELAY HZ |
| 955 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 956 | static void link_led_handler(struct work_struct *work) |
Ulrich Kunitz | 583afd1 | 2006-09-13 02:42:38 +0100 | [diff] [blame] | 957 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 958 | struct zd_mac *mac = |
| 959 | container_of(work, struct zd_mac, housekeeping.link_led_work.work); |
Ulrich Kunitz | 583afd1 | 2006-09-13 02:42:38 +0100 | [diff] [blame] | 960 | struct zd_chip *chip = &mac->chip; |
Ulrich Kunitz | 583afd1 | 2006-09-13 02:42:38 +0100 | [diff] [blame] | 961 | int is_associated; |
| 962 | int r; |
| 963 | |
| 964 | spin_lock_irq(&mac->lock); |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 965 | is_associated = mac->associated; |
Ulrich Kunitz | 583afd1 | 2006-09-13 02:42:38 +0100 | [diff] [blame] | 966 | spin_unlock_irq(&mac->lock); |
| 967 | |
| 968 | r = zd_chip_control_leds(chip, |
| 969 | is_associated ? LED_ASSOCIATED : LED_SCANNING); |
| 970 | if (r) |
Daniel Drake | 459c51a | 2007-11-19 15:00:29 +0000 | [diff] [blame] | 971 | dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r); |
Ulrich Kunitz | 583afd1 | 2006-09-13 02:42:38 +0100 | [diff] [blame] | 972 | |
| 973 | queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work, |
| 974 | LINK_LED_WORK_DELAY); |
| 975 | } |
| 976 | |
| 977 | static void housekeeping_init(struct zd_mac *mac) |
| 978 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 979 | INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler); |
Ulrich Kunitz | 583afd1 | 2006-09-13 02:42:38 +0100 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | static void housekeeping_enable(struct zd_mac *mac) |
| 983 | { |
| 984 | dev_dbg_f(zd_mac_dev(mac), "\n"); |
| 985 | queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work, |
| 986 | 0); |
| 987 | } |
| 988 | |
| 989 | static void housekeeping_disable(struct zd_mac *mac) |
| 990 | { |
| 991 | dev_dbg_f(zd_mac_dev(mac), "\n"); |
| 992 | cancel_rearming_delayed_workqueue(zd_workqueue, |
| 993 | &mac->housekeeping.link_led_work); |
| 994 | zd_chip_control_leds(&mac->chip, LED_OFF); |
| 995 | } |