1 /* ZD1211 USB-WLAN driver for Linux 2 * 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> 6 * Copyright (C) 2007-2008 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu> 7 * 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, see <http://www.gnu.org/licenses/>. 20 */ 21 22 #include <linux/netdevice.h> 23 #include <linux/etherdevice.h> 24 #include <linux/slab.h> 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_rf.h" 33 34 struct zd_reg_alpha2_map { 35 u32 reg; 36 char alpha2[2]; 37 }; 38 39 static struct zd_reg_alpha2_map reg_alpha2_map[] = { 40 { ZD_REGDOMAIN_FCC, "US" }, 41 { ZD_REGDOMAIN_IC, "CA" }, 42 { ZD_REGDOMAIN_ETSI, "DE" }, /* Generic ETSI, use most restrictive */ 43 { ZD_REGDOMAIN_JAPAN, "JP" }, 44 { ZD_REGDOMAIN_JAPAN_2, "JP" }, 45 { ZD_REGDOMAIN_JAPAN_3, "JP" }, 46 { ZD_REGDOMAIN_SPAIN, "ES" }, 47 { ZD_REGDOMAIN_FRANCE, "FR" }, 48 }; 49 50 /* This table contains the hardware specific values for the modulation rates. */ 51 static const struct ieee80211_rate zd_rates[] = { 52 { .bitrate = 10, 53 .hw_value = ZD_CCK_RATE_1M, }, 54 { .bitrate = 20, 55 .hw_value = ZD_CCK_RATE_2M, 56 .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT, 57 .flags = IEEE80211_RATE_SHORT_PREAMBLE }, 58 { .bitrate = 55, 59 .hw_value = ZD_CCK_RATE_5_5M, 60 .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT, 61 .flags = IEEE80211_RATE_SHORT_PREAMBLE }, 62 { .bitrate = 110, 63 .hw_value = ZD_CCK_RATE_11M, 64 .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT, 65 .flags = IEEE80211_RATE_SHORT_PREAMBLE }, 66 { .bitrate = 60, 67 .hw_value = ZD_OFDM_RATE_6M, 68 .flags = 0 }, 69 { .bitrate = 90, 70 .hw_value = ZD_OFDM_RATE_9M, 71 .flags = 0 }, 72 { .bitrate = 120, 73 .hw_value = ZD_OFDM_RATE_12M, 74 .flags = 0 }, 75 { .bitrate = 180, 76 .hw_value = ZD_OFDM_RATE_18M, 77 .flags = 0 }, 78 { .bitrate = 240, 79 .hw_value = ZD_OFDM_RATE_24M, 80 .flags = 0 }, 81 { .bitrate = 360, 82 .hw_value = ZD_OFDM_RATE_36M, 83 .flags = 0 }, 84 { .bitrate = 480, 85 .hw_value = ZD_OFDM_RATE_48M, 86 .flags = 0 }, 87 { .bitrate = 540, 88 .hw_value = ZD_OFDM_RATE_54M, 89 .flags = 0 }, 90 }; 91 92 /* 93 * Zydas retry rates table. Each line is listed in the same order as 94 * in zd_rates[] and contains all the rate used when a packet is sent 95 * starting with a given rates. Let's consider an example : 96 * 97 * "11 Mbits : 4, 3, 2, 1, 0" means : 98 * - packet is sent using 4 different rates 99 * - 1st rate is index 3 (ie 11 Mbits) 100 * - 2nd rate is index 2 (ie 5.5 Mbits) 101 * - 3rd rate is index 1 (ie 2 Mbits) 102 * - 4th rate is index 0 (ie 1 Mbits) 103 */ 104 105 static const struct tx_retry_rate zd_retry_rates[] = { 106 { /* 1 Mbits */ 1, { 0 }}, 107 { /* 2 Mbits */ 2, { 1, 0 }}, 108 { /* 5.5 Mbits */ 3, { 2, 1, 0 }}, 109 { /* 11 Mbits */ 4, { 3, 2, 1, 0 }}, 110 { /* 6 Mbits */ 5, { 4, 3, 2, 1, 0 }}, 111 { /* 9 Mbits */ 6, { 5, 4, 3, 2, 1, 0}}, 112 { /* 12 Mbits */ 5, { 6, 3, 2, 1, 0 }}, 113 { /* 18 Mbits */ 6, { 7, 6, 3, 2, 1, 0 }}, 114 { /* 24 Mbits */ 6, { 8, 6, 3, 2, 1, 0 }}, 115 { /* 36 Mbits */ 7, { 9, 8, 6, 3, 2, 1, 0 }}, 116 { /* 48 Mbits */ 8, {10, 9, 8, 6, 3, 2, 1, 0 }}, 117 { /* 54 Mbits */ 9, {11, 10, 9, 8, 6, 3, 2, 1, 0 }} 118 }; 119 120 static const struct ieee80211_channel zd_channels[] = { 121 { .center_freq = 2412, .hw_value = 1 }, 122 { .center_freq = 2417, .hw_value = 2 }, 123 { .center_freq = 2422, .hw_value = 3 }, 124 { .center_freq = 2427, .hw_value = 4 }, 125 { .center_freq = 2432, .hw_value = 5 }, 126 { .center_freq = 2437, .hw_value = 6 }, 127 { .center_freq = 2442, .hw_value = 7 }, 128 { .center_freq = 2447, .hw_value = 8 }, 129 { .center_freq = 2452, .hw_value = 9 }, 130 { .center_freq = 2457, .hw_value = 10 }, 131 { .center_freq = 2462, .hw_value = 11 }, 132 { .center_freq = 2467, .hw_value = 12 }, 133 { .center_freq = 2472, .hw_value = 13 }, 134 { .center_freq = 2484, .hw_value = 14 }, 135 }; 136 137 static void housekeeping_init(struct zd_mac *mac); 138 static void housekeeping_enable(struct zd_mac *mac); 139 static void housekeeping_disable(struct zd_mac *mac); 140 static void beacon_init(struct zd_mac *mac); 141 static void beacon_enable(struct zd_mac *mac); 142 static void beacon_disable(struct zd_mac *mac); 143 static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble); 144 static int zd_mac_config_beacon(struct ieee80211_hw *hw, 145 struct sk_buff *beacon, bool in_intr); 146 147 static int zd_reg2alpha2(u8 regdomain, char *alpha2) 148 { 149 unsigned int i; 150 struct zd_reg_alpha2_map *reg_map; 151 for (i = 0; i < ARRAY_SIZE(reg_alpha2_map); i++) { 152 reg_map = ®_alpha2_map[i]; 153 if (regdomain == reg_map->reg) { 154 alpha2[0] = reg_map->alpha2[0]; 155 alpha2[1] = reg_map->alpha2[1]; 156 return 0; 157 } 158 } 159 return 1; 160 } 161 162 static int zd_check_signal(struct ieee80211_hw *hw, int signal) 163 { 164 struct zd_mac *mac = zd_hw_mac(hw); 165 166 dev_dbg_f_cond(zd_mac_dev(mac), signal < 0 || signal > 100, 167 "%s: signal value from device not in range 0..100, " 168 "but %d.\n", __func__, signal); 169 170 if (signal < 0) 171 signal = 0; 172 else if (signal > 100) 173 signal = 100; 174 175 return signal; 176 } 177 178 int zd_mac_preinit_hw(struct ieee80211_hw *hw) 179 { 180 int r; 181 u8 addr[ETH_ALEN]; 182 struct zd_mac *mac = zd_hw_mac(hw); 183 184 r = zd_chip_read_mac_addr_fw(&mac->chip, addr); 185 if (r) 186 return r; 187 188 SET_IEEE80211_PERM_ADDR(hw, addr); 189 190 return 0; 191 } 192 193 int zd_mac_init_hw(struct ieee80211_hw *hw) 194 { 195 int r; 196 struct zd_mac *mac = zd_hw_mac(hw); 197 struct zd_chip *chip = &mac->chip; 198 char alpha2[2]; 199 u8 default_regdomain; 200 201 r = zd_chip_enable_int(chip); 202 if (r) 203 goto out; 204 r = zd_chip_init_hw(chip); 205 if (r) 206 goto disable_int; 207 208 ZD_ASSERT(!irqs_disabled()); 209 210 r = zd_read_regdomain(chip, &default_regdomain); 211 if (r) 212 goto disable_int; 213 spin_lock_irq(&mac->lock); 214 mac->regdomain = mac->default_regdomain = default_regdomain; 215 spin_unlock_irq(&mac->lock); 216 217 /* We must inform the device that we are doing encryption/decryption in 218 * software at the moment. */ 219 r = zd_set_encryption_type(chip, ENC_SNIFFER); 220 if (r) 221 goto disable_int; 222 223 r = zd_reg2alpha2(mac->regdomain, alpha2); 224 if (r) 225 goto disable_int; 226 227 r = regulatory_hint(hw->wiphy, alpha2); 228 disable_int: 229 zd_chip_disable_int(chip); 230 out: 231 return r; 232 } 233 234 void zd_mac_clear(struct zd_mac *mac) 235 { 236 flush_workqueue(zd_workqueue); 237 zd_chip_clear(&mac->chip); 238 ZD_ASSERT(!spin_is_locked(&mac->lock)); 239 ZD_MEMCLEAR(mac, sizeof(struct zd_mac)); 240 } 241 242 static int set_rx_filter(struct zd_mac *mac) 243 { 244 unsigned long flags; 245 u32 filter = STA_RX_FILTER; 246 247 spin_lock_irqsave(&mac->lock, flags); 248 if (mac->pass_ctrl) 249 filter |= RX_FILTER_CTRL; 250 spin_unlock_irqrestore(&mac->lock, flags); 251 252 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter); 253 } 254 255 static int set_mac_and_bssid(struct zd_mac *mac) 256 { 257 int r; 258 259 if (!mac->vif) 260 return -1; 261 262 r = zd_write_mac_addr(&mac->chip, mac->vif->addr); 263 if (r) 264 return r; 265 266 /* Vendor driver after setting MAC either sets BSSID for AP or 267 * filter for other modes. 268 */ 269 if (mac->type != NL80211_IFTYPE_AP) 270 return set_rx_filter(mac); 271 else 272 return zd_write_bssid(&mac->chip, mac->vif->addr); 273 } 274 275 static int set_mc_hash(struct zd_mac *mac) 276 { 277 struct zd_mc_hash hash; 278 zd_mc_clear(&hash); 279 return zd_chip_set_multicast_hash(&mac->chip, &hash); 280 } 281 282 int zd_op_start(struct ieee80211_hw *hw) 283 { 284 struct zd_mac *mac = zd_hw_mac(hw); 285 struct zd_chip *chip = &mac->chip; 286 struct zd_usb *usb = &chip->usb; 287 int r; 288 289 if (!usb->initialized) { 290 r = zd_usb_init_hw(usb); 291 if (r) 292 goto out; 293 } 294 295 r = zd_chip_enable_int(chip); 296 if (r < 0) 297 goto out; 298 299 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G); 300 if (r < 0) 301 goto disable_int; 302 r = set_rx_filter(mac); 303 if (r) 304 goto disable_int; 305 r = set_mc_hash(mac); 306 if (r) 307 goto disable_int; 308 309 /* Wait after setting the multicast hash table and powering on 310 * the radio otherwise interface bring up will fail. This matches 311 * what the vendor driver did. 312 */ 313 msleep(10); 314 315 r = zd_chip_switch_radio_on(chip); 316 if (r < 0) { 317 dev_err(zd_chip_dev(chip), 318 "%s: failed to set radio on\n", __func__); 319 goto disable_int; 320 } 321 r = zd_chip_enable_rxtx(chip); 322 if (r < 0) 323 goto disable_radio; 324 r = zd_chip_enable_hwint(chip); 325 if (r < 0) 326 goto disable_rxtx; 327 328 housekeeping_enable(mac); 329 beacon_enable(mac); 330 set_bit(ZD_DEVICE_RUNNING, &mac->flags); 331 return 0; 332 disable_rxtx: 333 zd_chip_disable_rxtx(chip); 334 disable_radio: 335 zd_chip_switch_radio_off(chip); 336 disable_int: 337 zd_chip_disable_int(chip); 338 out: 339 return r; 340 } 341 342 void zd_op_stop(struct ieee80211_hw *hw) 343 { 344 struct zd_mac *mac = zd_hw_mac(hw); 345 struct zd_chip *chip = &mac->chip; 346 struct sk_buff *skb; 347 struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue; 348 349 clear_bit(ZD_DEVICE_RUNNING, &mac->flags); 350 351 /* The order here deliberately is a little different from the open() 352 * method, since we need to make sure there is no opportunity for RX 353 * frames to be processed by mac80211 after we have stopped it. 354 */ 355 356 zd_chip_disable_rxtx(chip); 357 beacon_disable(mac); 358 housekeeping_disable(mac); 359 flush_workqueue(zd_workqueue); 360 361 zd_chip_disable_hwint(chip); 362 zd_chip_switch_radio_off(chip); 363 zd_chip_disable_int(chip); 364 365 366 while ((skb = skb_dequeue(ack_wait_queue))) 367 dev_kfree_skb_any(skb); 368 } 369 370 int zd_restore_settings(struct zd_mac *mac) 371 { 372 struct sk_buff *beacon; 373 struct zd_mc_hash multicast_hash; 374 unsigned int short_preamble; 375 int r, beacon_interval, beacon_period; 376 u8 channel; 377 378 dev_dbg_f(zd_mac_dev(mac), "\n"); 379 380 spin_lock_irq(&mac->lock); 381 multicast_hash = mac->multicast_hash; 382 short_preamble = mac->short_preamble; 383 beacon_interval = mac->beacon.interval; 384 beacon_period = mac->beacon.period; 385 channel = mac->channel; 386 spin_unlock_irq(&mac->lock); 387 388 r = set_mac_and_bssid(mac); 389 if (r < 0) { 390 dev_dbg_f(zd_mac_dev(mac), "set_mac_and_bssid failed, %d\n", r); 391 return r; 392 } 393 394 r = zd_chip_set_channel(&mac->chip, channel); 395 if (r < 0) { 396 dev_dbg_f(zd_mac_dev(mac), "zd_chip_set_channel failed, %d\n", 397 r); 398 return r; 399 } 400 401 set_rts_cts(mac, short_preamble); 402 403 r = zd_chip_set_multicast_hash(&mac->chip, &multicast_hash); 404 if (r < 0) { 405 dev_dbg_f(zd_mac_dev(mac), 406 "zd_chip_set_multicast_hash failed, %d\n", r); 407 return r; 408 } 409 410 if (mac->type == NL80211_IFTYPE_MESH_POINT || 411 mac->type == NL80211_IFTYPE_ADHOC || 412 mac->type == NL80211_IFTYPE_AP) { 413 if (mac->vif != NULL) { 414 beacon = ieee80211_beacon_get(mac->hw, mac->vif); 415 if (beacon) 416 zd_mac_config_beacon(mac->hw, beacon, false); 417 } 418 419 zd_set_beacon_interval(&mac->chip, beacon_interval, 420 beacon_period, mac->type); 421 422 spin_lock_irq(&mac->lock); 423 mac->beacon.last_update = jiffies; 424 spin_unlock_irq(&mac->lock); 425 } 426 427 return 0; 428 } 429 430 /** 431 * zd_mac_tx_status - reports tx status of a packet if required 432 * @hw - a &struct ieee80211_hw pointer 433 * @skb - a sk-buffer 434 * @flags: extra flags to set in the TX status info 435 * @ackssi: ACK signal strength 436 * @success - True for successful transmission of the frame 437 * 438 * This information calls ieee80211_tx_status_irqsafe() if required by the 439 * control information. It copies the control information into the status 440 * information. 441 * 442 * If no status information has been requested, the skb is freed. 443 */ 444 static void zd_mac_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb, 445 int ackssi, struct tx_status *tx_status) 446 { 447 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 448 int i; 449 int success = 1, retry = 1; 450 int first_idx; 451 const struct tx_retry_rate *retries; 452 453 ieee80211_tx_info_clear_status(info); 454 455 if (tx_status) { 456 success = !tx_status->failure; 457 retry = tx_status->retry + success; 458 } 459 460 if (success) { 461 /* success */ 462 info->flags |= IEEE80211_TX_STAT_ACK; 463 } else { 464 /* failure */ 465 info->flags &= ~IEEE80211_TX_STAT_ACK; 466 } 467 468 first_idx = info->status.rates[0].idx; 469 ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates)); 470 retries = &zd_retry_rates[first_idx]; 471 ZD_ASSERT(1 <= retry && retry <= retries->count); 472 473 info->status.rates[0].idx = retries->rate[0]; 474 info->status.rates[0].count = 1; // (retry > 1 ? 2 : 1); 475 476 for (i=1; i<IEEE80211_TX_MAX_RATES-1 && i<retry; i++) { 477 info->status.rates[i].idx = retries->rate[i]; 478 info->status.rates[i].count = 1; // ((i==retry-1) && success ? 1:2); 479 } 480 for (; i<IEEE80211_TX_MAX_RATES && i<retry; i++) { 481 info->status.rates[i].idx = retries->rate[retry - 1]; 482 info->status.rates[i].count = 1; // (success ? 1:2); 483 } 484 if (i<IEEE80211_TX_MAX_RATES) 485 info->status.rates[i].idx = -1; /* terminate */ 486 487 info->status.ack_signal = zd_check_signal(hw, ackssi); 488 ieee80211_tx_status_irqsafe(hw, skb); 489 } 490 491 /** 492 * zd_mac_tx_failed - callback for failed frames 493 * @dev: the mac80211 wireless device 494 * 495 * This function is called if a frame couldn't be successfully 496 * transferred. The first frame from the tx queue, will be selected and 497 * reported as error to the upper layers. 498 */ 499 void zd_mac_tx_failed(struct urb *urb) 500 { 501 struct ieee80211_hw * hw = zd_usb_to_hw(urb->context); 502 struct zd_mac *mac = zd_hw_mac(hw); 503 struct sk_buff_head *q = &mac->ack_wait_queue; 504 struct sk_buff *skb; 505 struct tx_status *tx_status = (struct tx_status *)urb->transfer_buffer; 506 unsigned long flags; 507 int success = !tx_status->failure; 508 int retry = tx_status->retry + success; 509 int found = 0; 510 int i, position = 0; 511 512 spin_lock_irqsave(&q->lock, flags); 513 514 skb_queue_walk(q, skb) { 515 struct ieee80211_hdr *tx_hdr; 516 struct ieee80211_tx_info *info; 517 int first_idx, final_idx; 518 const struct tx_retry_rate *retries; 519 u8 final_rate; 520 521 position ++; 522 523 /* if the hardware reports a failure and we had a 802.11 ACK 524 * pending, then we skip the first skb when searching for a 525 * matching frame */ 526 if (tx_status->failure && mac->ack_pending && 527 skb_queue_is_first(q, skb)) { 528 continue; 529 } 530 531 tx_hdr = (struct ieee80211_hdr *)skb->data; 532 533 /* we skip all frames not matching the reported destination */ 534 if (unlikely(!ether_addr_equal(tx_hdr->addr1, tx_status->mac))) 535 continue; 536 537 /* we skip all frames not matching the reported final rate */ 538 539 info = IEEE80211_SKB_CB(skb); 540 first_idx = info->status.rates[0].idx; 541 ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates)); 542 retries = &zd_retry_rates[first_idx]; 543 if (retry <= 0 || retry > retries->count) 544 continue; 545 546 final_idx = retries->rate[retry - 1]; 547 final_rate = zd_rates[final_idx].hw_value; 548 549 if (final_rate != tx_status->rate) { 550 continue; 551 } 552 553 found = 1; 554 break; 555 } 556 557 if (found) { 558 for (i=1; i<=position; i++) { 559 skb = __skb_dequeue(q); 560 zd_mac_tx_status(hw, skb, 561 mac->ack_pending ? mac->ack_signal : 0, 562 i == position ? tx_status : NULL); 563 mac->ack_pending = 0; 564 } 565 } 566 567 spin_unlock_irqrestore(&q->lock, flags); 568 } 569 570 /** 571 * zd_mac_tx_to_dev - callback for USB layer 572 * @skb: a &sk_buff pointer 573 * @error: error value, 0 if transmission successful 574 * 575 * Informs the MAC layer that the frame has successfully transferred to the 576 * device. If an ACK is required and the transfer to the device has been 577 * successful, the packets are put on the @ack_wait_queue with 578 * the control set removed. 579 */ 580 void zd_mac_tx_to_dev(struct sk_buff *skb, int error) 581 { 582 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 583 struct ieee80211_hw *hw = info->rate_driver_data[0]; 584 struct zd_mac *mac = zd_hw_mac(hw); 585 586 ieee80211_tx_info_clear_status(info); 587 588 skb_pull(skb, sizeof(struct zd_ctrlset)); 589 if (unlikely(error || 590 (info->flags & IEEE80211_TX_CTL_NO_ACK))) { 591 /* 592 * FIXME : do we need to fill in anything ? 593 */ 594 ieee80211_tx_status_irqsafe(hw, skb); 595 } else { 596 struct sk_buff_head *q = &mac->ack_wait_queue; 597 598 skb_queue_tail(q, skb); 599 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS) { 600 zd_mac_tx_status(hw, skb_dequeue(q), 601 mac->ack_pending ? mac->ack_signal : 0, 602 NULL); 603 mac->ack_pending = 0; 604 } 605 } 606 } 607 608 static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length) 609 { 610 /* ZD_PURE_RATE() must be used to remove the modulation type flag of 611 * the zd-rate values. 612 */ 613 static const u8 rate_divisor[] = { 614 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1, 615 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2, 616 /* Bits must be doubled. */ 617 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11, 618 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11, 619 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6, 620 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9, 621 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12, 622 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18, 623 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24, 624 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36, 625 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48, 626 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54, 627 }; 628 629 u32 bits = (u32)tx_length * 8; 630 u32 divisor; 631 632 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)]; 633 if (divisor == 0) 634 return -EINVAL; 635 636 switch (zd_rate) { 637 case ZD_CCK_RATE_5_5M: 638 bits = (2*bits) + 10; /* round up to the next integer */ 639 break; 640 case ZD_CCK_RATE_11M: 641 if (service) { 642 u32 t = bits % 11; 643 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION; 644 if (0 < t && t <= 3) { 645 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION; 646 } 647 } 648 bits += 10; /* round up to the next integer */ 649 break; 650 } 651 652 return bits/divisor; 653 } 654 655 static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs, 656 struct ieee80211_hdr *header, 657 struct ieee80211_tx_info *info) 658 { 659 /* 660 * CONTROL TODO: 661 * - if backoff needed, enable bit 0 662 * - if burst (backoff not needed) disable bit 0 663 */ 664 665 cs->control = 0; 666 667 /* First fragment */ 668 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) 669 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF; 670 671 /* No ACK expected (multicast, etc.) */ 672 if (info->flags & IEEE80211_TX_CTL_NO_ACK) 673 cs->control |= ZD_CS_NO_ACK; 674 675 /* PS-POLL */ 676 if (ieee80211_is_pspoll(header->frame_control)) 677 cs->control |= ZD_CS_PS_POLL_FRAME; 678 679 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) 680 cs->control |= ZD_CS_RTS; 681 682 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) 683 cs->control |= ZD_CS_SELF_CTS; 684 685 /* FIXME: Management frame? */ 686 } 687 688 static bool zd_mac_match_cur_beacon(struct zd_mac *mac, struct sk_buff *beacon) 689 { 690 if (!mac->beacon.cur_beacon) 691 return false; 692 693 if (mac->beacon.cur_beacon->len != beacon->len) 694 return false; 695 696 return !memcmp(beacon->data, mac->beacon.cur_beacon->data, beacon->len); 697 } 698 699 static void zd_mac_free_cur_beacon_locked(struct zd_mac *mac) 700 { 701 ZD_ASSERT(mutex_is_locked(&mac->chip.mutex)); 702 703 kfree_skb(mac->beacon.cur_beacon); 704 mac->beacon.cur_beacon = NULL; 705 } 706 707 static void zd_mac_free_cur_beacon(struct zd_mac *mac) 708 { 709 mutex_lock(&mac->chip.mutex); 710 zd_mac_free_cur_beacon_locked(mac); 711 mutex_unlock(&mac->chip.mutex); 712 } 713 714 static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon, 715 bool in_intr) 716 { 717 struct zd_mac *mac = zd_hw_mac(hw); 718 int r, ret, num_cmds, req_pos = 0; 719 u32 tmp, j = 0; 720 /* 4 more bytes for tail CRC */ 721 u32 full_len = beacon->len + 4; 722 unsigned long end_jiffies, message_jiffies; 723 struct zd_ioreq32 *ioreqs; 724 725 mutex_lock(&mac->chip.mutex); 726 727 /* Check if hw already has this beacon. */ 728 if (zd_mac_match_cur_beacon(mac, beacon)) { 729 r = 0; 730 goto out_nofree; 731 } 732 733 /* Alloc memory for full beacon write at once. */ 734 num_cmds = 1 + zd_chip_is_zd1211b(&mac->chip) + full_len; 735 ioreqs = kmalloc(num_cmds * sizeof(struct zd_ioreq32), GFP_KERNEL); 736 if (!ioreqs) { 737 r = -ENOMEM; 738 goto out_nofree; 739 } 740 741 r = zd_iowrite32_locked(&mac->chip, 0, CR_BCN_FIFO_SEMAPHORE); 742 if (r < 0) 743 goto out; 744 r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE); 745 if (r < 0) 746 goto release_sema; 747 if (in_intr && tmp & 0x2) { 748 r = -EBUSY; 749 goto release_sema; 750 } 751 752 end_jiffies = jiffies + HZ / 2; /*~500ms*/ 753 message_jiffies = jiffies + HZ / 10; /*~100ms*/ 754 while (tmp & 0x2) { 755 r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE); 756 if (r < 0) 757 goto release_sema; 758 if (time_is_before_eq_jiffies(message_jiffies)) { 759 message_jiffies = jiffies + HZ / 10; 760 dev_err(zd_mac_dev(mac), 761 "CR_BCN_FIFO_SEMAPHORE not ready\n"); 762 if (time_is_before_eq_jiffies(end_jiffies)) { 763 dev_err(zd_mac_dev(mac), 764 "Giving up beacon config.\n"); 765 r = -ETIMEDOUT; 766 goto reset_device; 767 } 768 } 769 msleep(20); 770 } 771 772 ioreqs[req_pos].addr = CR_BCN_FIFO; 773 ioreqs[req_pos].value = full_len - 1; 774 req_pos++; 775 if (zd_chip_is_zd1211b(&mac->chip)) { 776 ioreqs[req_pos].addr = CR_BCN_LENGTH; 777 ioreqs[req_pos].value = full_len - 1; 778 req_pos++; 779 } 780 781 for (j = 0 ; j < beacon->len; j++) { 782 ioreqs[req_pos].addr = CR_BCN_FIFO; 783 ioreqs[req_pos].value = *((u8 *)(beacon->data + j)); 784 req_pos++; 785 } 786 787 for (j = 0; j < 4; j++) { 788 ioreqs[req_pos].addr = CR_BCN_FIFO; 789 ioreqs[req_pos].value = 0x0; 790 req_pos++; 791 } 792 793 BUG_ON(req_pos != num_cmds); 794 795 r = zd_iowrite32a_locked(&mac->chip, ioreqs, num_cmds); 796 797 release_sema: 798 /* 799 * Try very hard to release device beacon semaphore, as otherwise 800 * device/driver can be left in unusable state. 801 */ 802 end_jiffies = jiffies + HZ / 2; /*~500ms*/ 803 ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE); 804 while (ret < 0) { 805 if (in_intr || time_is_before_eq_jiffies(end_jiffies)) { 806 ret = -ETIMEDOUT; 807 break; 808 } 809 810 msleep(20); 811 ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE); 812 } 813 814 if (ret < 0) 815 dev_err(zd_mac_dev(mac), "Could not release " 816 "CR_BCN_FIFO_SEMAPHORE!\n"); 817 if (r < 0 || ret < 0) { 818 if (r >= 0) 819 r = ret; 820 821 /* We don't know if beacon was written successfully or not, 822 * so clear current. */ 823 zd_mac_free_cur_beacon_locked(mac); 824 825 goto out; 826 } 827 828 /* Beacon has now been written successfully, update current. */ 829 zd_mac_free_cur_beacon_locked(mac); 830 mac->beacon.cur_beacon = beacon; 831 beacon = NULL; 832 833 /* 802.11b/g 2.4G CCK 1Mb 834 * 802.11a, not yet implemented, uses different values (see GPL vendor 835 * driver) 836 */ 837 r = zd_iowrite32_locked(&mac->chip, 0x00000400 | (full_len << 19), 838 CR_BCN_PLCP_CFG); 839 out: 840 kfree(ioreqs); 841 out_nofree: 842 kfree_skb(beacon); 843 mutex_unlock(&mac->chip.mutex); 844 845 return r; 846 847 reset_device: 848 zd_mac_free_cur_beacon_locked(mac); 849 kfree_skb(beacon); 850 851 mutex_unlock(&mac->chip.mutex); 852 kfree(ioreqs); 853 854 /* semaphore stuck, reset device to avoid fw freeze later */ 855 dev_warn(zd_mac_dev(mac), "CR_BCN_FIFO_SEMAPHORE stuck, " 856 "resetting device..."); 857 usb_queue_reset_device(mac->chip.usb.intf); 858 859 return r; 860 } 861 862 static int fill_ctrlset(struct zd_mac *mac, 863 struct sk_buff *skb) 864 { 865 int r; 866 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; 867 unsigned int frag_len = skb->len + FCS_LEN; 868 unsigned int packet_length; 869 struct ieee80211_rate *txrate; 870 struct zd_ctrlset *cs = skb_push(skb, sizeof(struct zd_ctrlset)); 871 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 872 873 ZD_ASSERT(frag_len <= 0xffff); 874 875 /* 876 * Firmware computes the duration itself (for all frames except PSPoll) 877 * and needs the field set to 0 at input, otherwise firmware messes up 878 * duration_id and sets bits 14 and 15 on. 879 */ 880 if (!ieee80211_is_pspoll(hdr->frame_control)) 881 hdr->duration_id = 0; 882 883 txrate = ieee80211_get_tx_rate(mac->hw, info); 884 885 cs->modulation = txrate->hw_value; 886 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) 887 cs->modulation = txrate->hw_value_short; 888 889 cs->tx_length = cpu_to_le16(frag_len); 890 891 cs_set_control(mac, cs, hdr, info); 892 893 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10; 894 ZD_ASSERT(packet_length <= 0xffff); 895 /* ZD1211B: Computing the length difference this way, gives us 896 * flexibility to compute the packet length. 897 */ 898 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ? 899 packet_length - frag_len : packet_length); 900 901 /* 902 * CURRENT LENGTH: 903 * - transmit frame length in microseconds 904 * - seems to be derived from frame length 905 * - see Cal_Us_Service() in zdinlinef.h 906 * - if macp->bTxBurstEnable is enabled, then multiply by 4 907 * - bTxBurstEnable is never set in the vendor driver 908 * 909 * SERVICE: 910 * - "for PLCP configuration" 911 * - always 0 except in some situations at 802.11b 11M 912 * - see line 53 of zdinlinef.h 913 */ 914 cs->service = 0; 915 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation), 916 le16_to_cpu(cs->tx_length)); 917 if (r < 0) 918 return r; 919 cs->current_length = cpu_to_le16(r); 920 cs->next_frame_length = 0; 921 922 return 0; 923 } 924 925 /** 926 * zd_op_tx - transmits a network frame to the device 927 * 928 * @dev: mac80211 hardware device 929 * @skb: socket buffer 930 * @control: the control structure 931 * 932 * This function transmit an IEEE 802.11 network frame to the device. The 933 * control block of the skbuff will be initialized. If necessary the incoming 934 * mac80211 queues will be stopped. 935 */ 936 static void zd_op_tx(struct ieee80211_hw *hw, 937 struct ieee80211_tx_control *control, 938 struct sk_buff *skb) 939 { 940 struct zd_mac *mac = zd_hw_mac(hw); 941 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 942 int r; 943 944 r = fill_ctrlset(mac, skb); 945 if (r) 946 goto fail; 947 948 info->rate_driver_data[0] = hw; 949 950 r = zd_usb_tx(&mac->chip.usb, skb); 951 if (r) 952 goto fail; 953 return; 954 955 fail: 956 dev_kfree_skb(skb); 957 } 958 959 /** 960 * filter_ack - filters incoming packets for acknowledgements 961 * @dev: the mac80211 device 962 * @rx_hdr: received header 963 * @stats: the status for the received packet 964 * 965 * This functions looks for ACK packets and tries to match them with the 966 * frames in the tx queue. If a match is found the frame will be dequeued and 967 * the upper layers is informed about the successful transmission. If 968 * mac80211 queues have been stopped and the number of frames still to be 969 * transmitted is low the queues will be opened again. 970 * 971 * Returns 1 if the frame was an ACK, 0 if it was ignored. 972 */ 973 static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr, 974 struct ieee80211_rx_status *stats) 975 { 976 struct zd_mac *mac = zd_hw_mac(hw); 977 struct sk_buff *skb; 978 struct sk_buff_head *q; 979 unsigned long flags; 980 int found = 0; 981 int i, position = 0; 982 983 if (!ieee80211_is_ack(rx_hdr->frame_control)) 984 return 0; 985 986 q = &mac->ack_wait_queue; 987 spin_lock_irqsave(&q->lock, flags); 988 skb_queue_walk(q, skb) { 989 struct ieee80211_hdr *tx_hdr; 990 991 position ++; 992 993 if (mac->ack_pending && skb_queue_is_first(q, skb)) 994 continue; 995 996 tx_hdr = (struct ieee80211_hdr *)skb->data; 997 if (likely(ether_addr_equal(tx_hdr->addr2, rx_hdr->addr1))) 998 { 999 found = 1; 1000 break; 1001 } 1002 } 1003 1004 if (found) { 1005 for (i=1; i<position; i++) { 1006 skb = __skb_dequeue(q); 1007 zd_mac_tx_status(hw, skb, 1008 mac->ack_pending ? mac->ack_signal : 0, 1009 NULL); 1010 mac->ack_pending = 0; 1011 } 1012 1013 mac->ack_pending = 1; 1014 mac->ack_signal = stats->signal; 1015 1016 /* Prevent pending tx-packet on AP-mode */ 1017 if (mac->type == NL80211_IFTYPE_AP) { 1018 skb = __skb_dequeue(q); 1019 zd_mac_tx_status(hw, skb, mac->ack_signal, NULL); 1020 mac->ack_pending = 0; 1021 } 1022 } 1023 1024 spin_unlock_irqrestore(&q->lock, flags); 1025 return 1; 1026 } 1027 1028 int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length) 1029 { 1030 struct zd_mac *mac = zd_hw_mac(hw); 1031 struct ieee80211_rx_status stats; 1032 const struct rx_status *status; 1033 struct sk_buff *skb; 1034 int bad_frame = 0; 1035 __le16 fc; 1036 int need_padding; 1037 int i; 1038 u8 rate; 1039 1040 if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ + 1041 FCS_LEN + sizeof(struct rx_status)) 1042 return -EINVAL; 1043 1044 memset(&stats, 0, sizeof(stats)); 1045 1046 /* Note about pass_failed_fcs and pass_ctrl access below: 1047 * mac locking intentionally omitted here, as this is the only unlocked 1048 * reader and the only writer is configure_filter. Plus, if there were 1049 * any races accessing these variables, it wouldn't really matter. 1050 * If mac80211 ever provides a way for us to access filter flags 1051 * from outside configure_filter, we could improve on this. Also, this 1052 * situation may change once we implement some kind of DMA-into-skb 1053 * RX path. */ 1054 1055 /* Caller has to ensure that length >= sizeof(struct rx_status). */ 1056 status = (struct rx_status *) 1057 (buffer + (length - sizeof(struct rx_status))); 1058 if (status->frame_status & ZD_RX_ERROR) { 1059 if (mac->pass_failed_fcs && 1060 (status->frame_status & ZD_RX_CRC32_ERROR)) { 1061 stats.flag |= RX_FLAG_FAILED_FCS_CRC; 1062 bad_frame = 1; 1063 } else { 1064 return -EINVAL; 1065 } 1066 } 1067 1068 stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq; 1069 stats.band = NL80211_BAND_2GHZ; 1070 stats.signal = zd_check_signal(hw, status->signal_strength); 1071 1072 rate = zd_rx_rate(buffer, status); 1073 1074 /* todo: return index in the big switches in zd_rx_rate instead */ 1075 for (i = 0; i < mac->band.n_bitrates; i++) 1076 if (rate == mac->band.bitrates[i].hw_value) 1077 stats.rate_idx = i; 1078 1079 length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status); 1080 buffer += ZD_PLCP_HEADER_SIZE; 1081 1082 /* Except for bad frames, filter each frame to see if it is an ACK, in 1083 * which case our internal TX tracking is updated. Normally we then 1084 * bail here as there's no need to pass ACKs on up to the stack, but 1085 * there is also the case where the stack has requested us to pass 1086 * control frames on up (pass_ctrl) which we must consider. */ 1087 if (!bad_frame && 1088 filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats) 1089 && !mac->pass_ctrl) 1090 return 0; 1091 1092 fc = get_unaligned((__le16*)buffer); 1093 need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc); 1094 1095 skb = dev_alloc_skb(length + (need_padding ? 2 : 0)); 1096 if (skb == NULL) 1097 return -ENOMEM; 1098 if (need_padding) { 1099 /* Make sure the payload data is 4 byte aligned. */ 1100 skb_reserve(skb, 2); 1101 } 1102 1103 /* FIXME : could we avoid this big memcpy ? */ 1104 skb_put_data(skb, buffer, length); 1105 1106 memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats)); 1107 ieee80211_rx_irqsafe(hw, skb); 1108 return 0; 1109 } 1110 1111 static int zd_op_add_interface(struct ieee80211_hw *hw, 1112 struct ieee80211_vif *vif) 1113 { 1114 struct zd_mac *mac = zd_hw_mac(hw); 1115 1116 /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */ 1117 if (mac->type != NL80211_IFTYPE_UNSPECIFIED) 1118 return -EOPNOTSUPP; 1119 1120 switch (vif->type) { 1121 case NL80211_IFTYPE_MONITOR: 1122 case NL80211_IFTYPE_MESH_POINT: 1123 case NL80211_IFTYPE_STATION: 1124 case NL80211_IFTYPE_ADHOC: 1125 case NL80211_IFTYPE_AP: 1126 mac->type = vif->type; 1127 break; 1128 default: 1129 return -EOPNOTSUPP; 1130 } 1131 1132 mac->vif = vif; 1133 1134 return set_mac_and_bssid(mac); 1135 } 1136 1137 static void zd_op_remove_interface(struct ieee80211_hw *hw, 1138 struct ieee80211_vif *vif) 1139 { 1140 struct zd_mac *mac = zd_hw_mac(hw); 1141 mac->type = NL80211_IFTYPE_UNSPECIFIED; 1142 mac->vif = NULL; 1143 zd_set_beacon_interval(&mac->chip, 0, 0, NL80211_IFTYPE_UNSPECIFIED); 1144 zd_write_mac_addr(&mac->chip, NULL); 1145 1146 zd_mac_free_cur_beacon(mac); 1147 } 1148 1149 static int zd_op_config(struct ieee80211_hw *hw, u32 changed) 1150 { 1151 struct zd_mac *mac = zd_hw_mac(hw); 1152 struct ieee80211_conf *conf = &hw->conf; 1153 1154 spin_lock_irq(&mac->lock); 1155 mac->channel = conf->chandef.chan->hw_value; 1156 spin_unlock_irq(&mac->lock); 1157 1158 return zd_chip_set_channel(&mac->chip, conf->chandef.chan->hw_value); 1159 } 1160 1161 static void zd_beacon_done(struct zd_mac *mac) 1162 { 1163 struct sk_buff *skb, *beacon; 1164 1165 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags)) 1166 return; 1167 if (!mac->vif || mac->vif->type != NL80211_IFTYPE_AP) 1168 return; 1169 1170 /* 1171 * Send out buffered broad- and multicast frames. 1172 */ 1173 while (!ieee80211_queue_stopped(mac->hw, 0)) { 1174 skb = ieee80211_get_buffered_bc(mac->hw, mac->vif); 1175 if (!skb) 1176 break; 1177 zd_op_tx(mac->hw, NULL, skb); 1178 } 1179 1180 /* 1181 * Fetch next beacon so that tim_count is updated. 1182 */ 1183 beacon = ieee80211_beacon_get(mac->hw, mac->vif); 1184 if (beacon) 1185 zd_mac_config_beacon(mac->hw, beacon, true); 1186 1187 spin_lock_irq(&mac->lock); 1188 mac->beacon.last_update = jiffies; 1189 spin_unlock_irq(&mac->lock); 1190 } 1191 1192 static void zd_process_intr(struct work_struct *work) 1193 { 1194 u16 int_status; 1195 unsigned long flags; 1196 struct zd_mac *mac = container_of(work, struct zd_mac, process_intr); 1197 1198 spin_lock_irqsave(&mac->lock, flags); 1199 int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer + 4)); 1200 spin_unlock_irqrestore(&mac->lock, flags); 1201 1202 if (int_status & INT_CFG_NEXT_BCN) { 1203 /*dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");*/ 1204 zd_beacon_done(mac); 1205 } else { 1206 dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n"); 1207 } 1208 1209 zd_chip_enable_hwint(&mac->chip); 1210 } 1211 1212 1213 static u64 zd_op_prepare_multicast(struct ieee80211_hw *hw, 1214 struct netdev_hw_addr_list *mc_list) 1215 { 1216 struct zd_mac *mac = zd_hw_mac(hw); 1217 struct zd_mc_hash hash; 1218 struct netdev_hw_addr *ha; 1219 1220 zd_mc_clear(&hash); 1221 1222 netdev_hw_addr_list_for_each(ha, mc_list) { 1223 dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n", ha->addr); 1224 zd_mc_add_addr(&hash, ha->addr); 1225 } 1226 1227 return hash.low | ((u64)hash.high << 32); 1228 } 1229 1230 #define SUPPORTED_FIF_FLAGS \ 1231 (FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \ 1232 FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC) 1233 static void zd_op_configure_filter(struct ieee80211_hw *hw, 1234 unsigned int changed_flags, 1235 unsigned int *new_flags, 1236 u64 multicast) 1237 { 1238 struct zd_mc_hash hash = { 1239 .low = multicast, 1240 .high = multicast >> 32, 1241 }; 1242 struct zd_mac *mac = zd_hw_mac(hw); 1243 unsigned long flags; 1244 int r; 1245 1246 /* Only deal with supported flags */ 1247 changed_flags &= SUPPORTED_FIF_FLAGS; 1248 *new_flags &= SUPPORTED_FIF_FLAGS; 1249 1250 /* 1251 * If multicast parameter (as returned by zd_op_prepare_multicast) 1252 * has changed, no bit in changed_flags is set. To handle this 1253 * situation, we do not return if changed_flags is 0. If we do so, 1254 * we will have some issue with IPv6 which uses multicast for link 1255 * layer address resolution. 1256 */ 1257 if (*new_flags & FIF_ALLMULTI) 1258 zd_mc_add_all(&hash); 1259 1260 spin_lock_irqsave(&mac->lock, flags); 1261 mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL); 1262 mac->pass_ctrl = !!(*new_flags & FIF_CONTROL); 1263 mac->multicast_hash = hash; 1264 spin_unlock_irqrestore(&mac->lock, flags); 1265 1266 zd_chip_set_multicast_hash(&mac->chip, &hash); 1267 1268 if (changed_flags & FIF_CONTROL) { 1269 r = set_rx_filter(mac); 1270 if (r) 1271 dev_err(zd_mac_dev(mac), "set_rx_filter error %d\n", r); 1272 } 1273 1274 /* no handling required for FIF_OTHER_BSS as we don't currently 1275 * do BSSID filtering */ 1276 /* FIXME: in future it would be nice to enable the probe response 1277 * filter (so that the driver doesn't see them) until 1278 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd 1279 * have to schedule work to enable prbresp reception, which might 1280 * happen too late. For now we'll just listen and forward them all the 1281 * time. */ 1282 } 1283 1284 static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble) 1285 { 1286 mutex_lock(&mac->chip.mutex); 1287 zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble); 1288 mutex_unlock(&mac->chip.mutex); 1289 } 1290 1291 static void zd_op_bss_info_changed(struct ieee80211_hw *hw, 1292 struct ieee80211_vif *vif, 1293 struct ieee80211_bss_conf *bss_conf, 1294 u32 changes) 1295 { 1296 struct zd_mac *mac = zd_hw_mac(hw); 1297 int associated; 1298 1299 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes); 1300 1301 if (mac->type == NL80211_IFTYPE_MESH_POINT || 1302 mac->type == NL80211_IFTYPE_ADHOC || 1303 mac->type == NL80211_IFTYPE_AP) { 1304 associated = true; 1305 if (changes & BSS_CHANGED_BEACON) { 1306 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif); 1307 1308 if (beacon) { 1309 zd_chip_disable_hwint(&mac->chip); 1310 zd_mac_config_beacon(hw, beacon, false); 1311 zd_chip_enable_hwint(&mac->chip); 1312 } 1313 } 1314 1315 if (changes & BSS_CHANGED_BEACON_ENABLED) { 1316 u16 interval = 0; 1317 u8 period = 0; 1318 1319 if (bss_conf->enable_beacon) { 1320 period = bss_conf->dtim_period; 1321 interval = bss_conf->beacon_int; 1322 } 1323 1324 spin_lock_irq(&mac->lock); 1325 mac->beacon.period = period; 1326 mac->beacon.interval = interval; 1327 mac->beacon.last_update = jiffies; 1328 spin_unlock_irq(&mac->lock); 1329 1330 zd_set_beacon_interval(&mac->chip, interval, period, 1331 mac->type); 1332 } 1333 } else 1334 associated = is_valid_ether_addr(bss_conf->bssid); 1335 1336 spin_lock_irq(&mac->lock); 1337 mac->associated = associated; 1338 spin_unlock_irq(&mac->lock); 1339 1340 /* TODO: do hardware bssid filtering */ 1341 1342 if (changes & BSS_CHANGED_ERP_PREAMBLE) { 1343 spin_lock_irq(&mac->lock); 1344 mac->short_preamble = bss_conf->use_short_preamble; 1345 spin_unlock_irq(&mac->lock); 1346 1347 set_rts_cts(mac, bss_conf->use_short_preamble); 1348 } 1349 } 1350 1351 static u64 zd_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif) 1352 { 1353 struct zd_mac *mac = zd_hw_mac(hw); 1354 return zd_chip_get_tsf(&mac->chip); 1355 } 1356 1357 static const struct ieee80211_ops zd_ops = { 1358 .tx = zd_op_tx, 1359 .start = zd_op_start, 1360 .stop = zd_op_stop, 1361 .add_interface = zd_op_add_interface, 1362 .remove_interface = zd_op_remove_interface, 1363 .config = zd_op_config, 1364 .prepare_multicast = zd_op_prepare_multicast, 1365 .configure_filter = zd_op_configure_filter, 1366 .bss_info_changed = zd_op_bss_info_changed, 1367 .get_tsf = zd_op_get_tsf, 1368 }; 1369 1370 struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf) 1371 { 1372 struct zd_mac *mac; 1373 struct ieee80211_hw *hw; 1374 1375 hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops); 1376 if (!hw) { 1377 dev_dbg_f(&intf->dev, "out of memory\n"); 1378 return NULL; 1379 } 1380 1381 mac = zd_hw_mac(hw); 1382 1383 memset(mac, 0, sizeof(*mac)); 1384 spin_lock_init(&mac->lock); 1385 mac->hw = hw; 1386 1387 mac->type = NL80211_IFTYPE_UNSPECIFIED; 1388 1389 memcpy(mac->channels, zd_channels, sizeof(zd_channels)); 1390 memcpy(mac->rates, zd_rates, sizeof(zd_rates)); 1391 mac->band.n_bitrates = ARRAY_SIZE(zd_rates); 1392 mac->band.bitrates = mac->rates; 1393 mac->band.n_channels = ARRAY_SIZE(zd_channels); 1394 mac->band.channels = mac->channels; 1395 1396 hw->wiphy->bands[NL80211_BAND_2GHZ] = &mac->band; 1397 1398 ieee80211_hw_set(hw, MFP_CAPABLE); 1399 ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING); 1400 ieee80211_hw_set(hw, RX_INCLUDES_FCS); 1401 ieee80211_hw_set(hw, SIGNAL_UNSPEC); 1402 1403 hw->wiphy->interface_modes = 1404 BIT(NL80211_IFTYPE_MESH_POINT) | 1405 BIT(NL80211_IFTYPE_STATION) | 1406 BIT(NL80211_IFTYPE_ADHOC) | 1407 BIT(NL80211_IFTYPE_AP); 1408 1409 wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST); 1410 1411 hw->max_signal = 100; 1412 hw->queues = 1; 1413 hw->extra_tx_headroom = sizeof(struct zd_ctrlset); 1414 1415 /* 1416 * Tell mac80211 that we support multi rate retries 1417 */ 1418 hw->max_rates = IEEE80211_TX_MAX_RATES; 1419 hw->max_rate_tries = 18; /* 9 rates * 2 retries/rate */ 1420 1421 skb_queue_head_init(&mac->ack_wait_queue); 1422 mac->ack_pending = 0; 1423 1424 zd_chip_init(&mac->chip, hw, intf); 1425 housekeeping_init(mac); 1426 beacon_init(mac); 1427 INIT_WORK(&mac->process_intr, zd_process_intr); 1428 1429 SET_IEEE80211_DEV(hw, &intf->dev); 1430 return hw; 1431 } 1432 1433 #define BEACON_WATCHDOG_DELAY round_jiffies_relative(HZ) 1434 1435 static void beacon_watchdog_handler(struct work_struct *work) 1436 { 1437 struct zd_mac *mac = 1438 container_of(work, struct zd_mac, beacon.watchdog_work.work); 1439 struct sk_buff *beacon; 1440 unsigned long timeout; 1441 int interval, period; 1442 1443 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags)) 1444 goto rearm; 1445 if (mac->type != NL80211_IFTYPE_AP || !mac->vif) 1446 goto rearm; 1447 1448 spin_lock_irq(&mac->lock); 1449 interval = mac->beacon.interval; 1450 period = mac->beacon.period; 1451 timeout = mac->beacon.last_update + 1452 msecs_to_jiffies(interval * 1024 / 1000) * 3; 1453 spin_unlock_irq(&mac->lock); 1454 1455 if (interval > 0 && time_is_before_jiffies(timeout)) { 1456 dev_dbg_f(zd_mac_dev(mac), "beacon interrupt stalled, " 1457 "restarting. " 1458 "(interval: %d, dtim: %d)\n", 1459 interval, period); 1460 1461 zd_chip_disable_hwint(&mac->chip); 1462 1463 beacon = ieee80211_beacon_get(mac->hw, mac->vif); 1464 if (beacon) { 1465 zd_mac_free_cur_beacon(mac); 1466 1467 zd_mac_config_beacon(mac->hw, beacon, false); 1468 } 1469 1470 zd_set_beacon_interval(&mac->chip, interval, period, mac->type); 1471 1472 zd_chip_enable_hwint(&mac->chip); 1473 1474 spin_lock_irq(&mac->lock); 1475 mac->beacon.last_update = jiffies; 1476 spin_unlock_irq(&mac->lock); 1477 } 1478 1479 rearm: 1480 queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work, 1481 BEACON_WATCHDOG_DELAY); 1482 } 1483 1484 static void beacon_init(struct zd_mac *mac) 1485 { 1486 INIT_DELAYED_WORK(&mac->beacon.watchdog_work, beacon_watchdog_handler); 1487 } 1488 1489 static void beacon_enable(struct zd_mac *mac) 1490 { 1491 dev_dbg_f(zd_mac_dev(mac), "\n"); 1492 1493 mac->beacon.last_update = jiffies; 1494 queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work, 1495 BEACON_WATCHDOG_DELAY); 1496 } 1497 1498 static void beacon_disable(struct zd_mac *mac) 1499 { 1500 dev_dbg_f(zd_mac_dev(mac), "\n"); 1501 cancel_delayed_work_sync(&mac->beacon.watchdog_work); 1502 1503 zd_mac_free_cur_beacon(mac); 1504 } 1505 1506 #define LINK_LED_WORK_DELAY HZ 1507 1508 static void link_led_handler(struct work_struct *work) 1509 { 1510 struct zd_mac *mac = 1511 container_of(work, struct zd_mac, housekeeping.link_led_work.work); 1512 struct zd_chip *chip = &mac->chip; 1513 int is_associated; 1514 int r; 1515 1516 if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags)) 1517 goto requeue; 1518 1519 spin_lock_irq(&mac->lock); 1520 is_associated = mac->associated; 1521 spin_unlock_irq(&mac->lock); 1522 1523 r = zd_chip_control_leds(chip, 1524 is_associated ? ZD_LED_ASSOCIATED : ZD_LED_SCANNING); 1525 if (r) 1526 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r); 1527 1528 requeue: 1529 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work, 1530 LINK_LED_WORK_DELAY); 1531 } 1532 1533 static void housekeeping_init(struct zd_mac *mac) 1534 { 1535 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler); 1536 } 1537 1538 static void housekeeping_enable(struct zd_mac *mac) 1539 { 1540 dev_dbg_f(zd_mac_dev(mac), "\n"); 1541 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work, 1542 0); 1543 } 1544 1545 static void housekeeping_disable(struct zd_mac *mac) 1546 { 1547 dev_dbg_f(zd_mac_dev(mac), "\n"); 1548 cancel_delayed_work_sync(&mac->housekeeping.link_led_work); 1549 zd_chip_control_leds(&mac->chip, ZD_LED_OFF); 1550 } 1551