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