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, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 */ 22 23#include <linux/netdevice.h> 24#include <linux/etherdevice.h> 25#include <linux/slab.h> 26#include <linux/usb.h> 27#include <linux/jiffies.h> 28#include <net/ieee80211_radiotap.h> 29 30#include "zd_def.h" 31#include "zd_chip.h" 32#include "zd_mac.h" 33#include "zd_rf.h" 34 35struct zd_reg_alpha2_map { 36 u32 reg; 37 char alpha2[2]; 38}; 39 40static struct zd_reg_alpha2_map reg_alpha2_map[] = { 41 { ZD_REGDOMAIN_FCC, "US" }, 42 { ZD_REGDOMAIN_IC, "CA" }, 43 { ZD_REGDOMAIN_ETSI, "DE" }, /* Generic ETSI, use most restrictive */ 44 { ZD_REGDOMAIN_JAPAN, "JP" }, 45 { ZD_REGDOMAIN_JAPAN_2, "JP" }, 46 { ZD_REGDOMAIN_JAPAN_3, "JP" }, 47 { ZD_REGDOMAIN_SPAIN, "ES" }, 48 { ZD_REGDOMAIN_FRANCE, "FR" }, 49}; 50 51/* This table contains the hardware specific values for the modulation rates. */ 52static const struct ieee80211_rate zd_rates[] = { 53 { .bitrate = 10, 54 .hw_value = ZD_CCK_RATE_1M, }, 55 { .bitrate = 20, 56 .hw_value = ZD_CCK_RATE_2M, 57 .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT, 58 .flags = IEEE80211_RATE_SHORT_PREAMBLE }, 59 { .bitrate = 55, 60 .hw_value = ZD_CCK_RATE_5_5M, 61 .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT, 62 .flags = IEEE80211_RATE_SHORT_PREAMBLE }, 63 { .bitrate = 110, 64 .hw_value = ZD_CCK_RATE_11M, 65 .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT, 66 .flags = IEEE80211_RATE_SHORT_PREAMBLE }, 67 { .bitrate = 60, 68 .hw_value = ZD_OFDM_RATE_6M, 69 .flags = 0 }, 70 { .bitrate = 90, 71 .hw_value = ZD_OFDM_RATE_9M, 72 .flags = 0 }, 73 { .bitrate = 120, 74 .hw_value = ZD_OFDM_RATE_12M, 75 .flags = 0 }, 76 { .bitrate = 180, 77 .hw_value = ZD_OFDM_RATE_18M, 78 .flags = 0 }, 79 { .bitrate = 240, 80 .hw_value = ZD_OFDM_RATE_24M, 81 .flags = 0 }, 82 { .bitrate = 360, 83 .hw_value = ZD_OFDM_RATE_36M, 84 .flags = 0 }, 85 { .bitrate = 480, 86 .hw_value = ZD_OFDM_RATE_48M, 87 .flags = 0 }, 88 { .bitrate = 540, 89 .hw_value = ZD_OFDM_RATE_54M, 90 .flags = 0 }, 91}; 92 93/* 94 * Zydas retry rates table. Each line is listed in the same order as 95 * in zd_rates[] and contains all the rate used when a packet is sent 96 * starting with a given rates. Let's consider an example : 97 * 98 * "11 Mbits : 4, 3, 2, 1, 0" means : 99 * - packet is sent using 4 different rates 100 * - 1st rate is index 3 (ie 11 Mbits) 101 * - 2nd rate is index 2 (ie 5.5 Mbits) 102 * - 3rd rate is index 1 (ie 2 Mbits) 103 * - 4th rate is index 0 (ie 1 Mbits) 104 */ 105 106static const struct tx_retry_rate zd_retry_rates[] = { 107 { /* 1 Mbits */ 1, { 0 }}, 108 { /* 2 Mbits */ 2, { 1, 0 }}, 109 { /* 5.5 Mbits */ 3, { 2, 1, 0 }}, 110 { /* 11 Mbits */ 4, { 3, 2, 1, 0 }}, 111 { /* 6 Mbits */ 5, { 4, 3, 2, 1, 0 }}, 112 { /* 9 Mbits */ 6, { 5, 4, 3, 2, 1, 0}}, 113 { /* 12 Mbits */ 5, { 6, 3, 2, 1, 0 }}, 114 { /* 18 Mbits */ 6, { 7, 6, 3, 2, 1, 0 }}, 115 { /* 24 Mbits */ 6, { 8, 6, 3, 2, 1, 0 }}, 116 { /* 36 Mbits */ 7, { 9, 8, 6, 3, 2, 1, 0 }}, 117 { /* 48 Mbits */ 8, {10, 9, 8, 6, 3, 2, 1, 0 }}, 118 { /* 54 Mbits */ 9, {11, 10, 9, 8, 6, 3, 2, 1, 0 }} 119}; 120 121static const struct ieee80211_channel zd_channels[] = { 122 { .center_freq = 2412, .hw_value = 1 }, 123 { .center_freq = 2417, .hw_value = 2 }, 124 { .center_freq = 2422, .hw_value = 3 }, 125 { .center_freq = 2427, .hw_value = 4 }, 126 { .center_freq = 2432, .hw_value = 5 }, 127 { .center_freq = 2437, .hw_value = 6 }, 128 { .center_freq = 2442, .hw_value = 7 }, 129 { .center_freq = 2447, .hw_value = 8 }, 130 { .center_freq = 2452, .hw_value = 9 }, 131 { .center_freq = 2457, .hw_value = 10 }, 132 { .center_freq = 2462, .hw_value = 11 }, 133 { .center_freq = 2467, .hw_value = 12 }, 134 { .center_freq = 2472, .hw_value = 13 }, 135 { .center_freq = 2484, .hw_value = 14 }, 136}; 137 138static void housekeeping_init(struct zd_mac *mac); 139static void housekeeping_enable(struct zd_mac *mac); 140static void housekeeping_disable(struct zd_mac *mac); 141 142static int zd_reg2alpha2(u8 regdomain, char *alpha2) 143{ 144 unsigned int i; 145 struct zd_reg_alpha2_map *reg_map; 146 for (i = 0; i < ARRAY_SIZE(reg_alpha2_map); i++) { 147 reg_map = ®_alpha2_map[i]; 148 if (regdomain == reg_map->reg) { 149 alpha2[0] = reg_map->alpha2[0]; 150 alpha2[1] = reg_map->alpha2[1]; 151 return 0; 152 } 153 } 154 return 1; 155} 156 157int zd_mac_preinit_hw(struct ieee80211_hw *hw) 158{ 159 int r; 160 u8 addr[ETH_ALEN]; 161 struct zd_mac *mac = zd_hw_mac(hw); 162 163 r = zd_chip_read_mac_addr_fw(&mac->chip, addr); 164 if (r) 165 return r; 166 167 SET_IEEE80211_PERM_ADDR(hw, addr); 168 169 return 0; 170} 171 172int zd_mac_init_hw(struct ieee80211_hw *hw) 173{ 174 int r; 175 struct zd_mac *mac = zd_hw_mac(hw); 176 struct zd_chip *chip = &mac->chip; 177 char alpha2[2]; 178 u8 default_regdomain; 179 180 r = zd_chip_enable_int(chip); 181 if (r) 182 goto out; 183 r = zd_chip_init_hw(chip); 184 if (r) 185 goto disable_int; 186 187 ZD_ASSERT(!irqs_disabled()); 188 189 r = zd_read_regdomain(chip, &default_regdomain); 190 if (r) 191 goto disable_int; 192 spin_lock_irq(&mac->lock); 193 mac->regdomain = mac->default_regdomain = default_regdomain; 194 spin_unlock_irq(&mac->lock); 195 196 /* We must inform the device that we are doing encryption/decryption in 197 * software at the moment. */ 198 r = zd_set_encryption_type(chip, ENC_SNIFFER); 199 if (r) 200 goto disable_int; 201 202 r = zd_reg2alpha2(mac->regdomain, alpha2); 203 if (r) 204 goto disable_int; 205 206 r = regulatory_hint(hw->wiphy, alpha2); 207disable_int: 208 zd_chip_disable_int(chip); 209out: 210 return r; 211} 212 213void zd_mac_clear(struct zd_mac *mac) 214{ 215 flush_workqueue(zd_workqueue); 216 zd_chip_clear(&mac->chip); 217 ZD_ASSERT(!spin_is_locked(&mac->lock)); 218 ZD_MEMCLEAR(mac, sizeof(struct zd_mac)); 219} 220 221static int set_rx_filter(struct zd_mac *mac) 222{ 223 unsigned long flags; 224 u32 filter = STA_RX_FILTER; 225 226 spin_lock_irqsave(&mac->lock, flags); 227 if (mac->pass_ctrl) 228 filter |= RX_FILTER_CTRL; 229 spin_unlock_irqrestore(&mac->lock, flags); 230 231 return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter); 232} 233 234static int set_mc_hash(struct zd_mac *mac) 235{ 236 struct zd_mc_hash hash; 237 zd_mc_clear(&hash); 238 return zd_chip_set_multicast_hash(&mac->chip, &hash); 239} 240 241static int zd_op_start(struct ieee80211_hw *hw) 242{ 243 struct zd_mac *mac = zd_hw_mac(hw); 244 struct zd_chip *chip = &mac->chip; 245 struct zd_usb *usb = &chip->usb; 246 int r; 247 248 if (!usb->initialized) { 249 r = zd_usb_init_hw(usb); 250 if (r) 251 goto out; 252 } 253 254 r = zd_chip_enable_int(chip); 255 if (r < 0) 256 goto out; 257 258 r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G); 259 if (r < 0) 260 goto disable_int; 261 r = set_rx_filter(mac); 262 if (r) 263 goto disable_int; 264 r = set_mc_hash(mac); 265 if (r) 266 goto disable_int; 267 r = zd_chip_switch_radio_on(chip); 268 if (r < 0) 269 goto disable_int; 270 r = zd_chip_enable_rxtx(chip); 271 if (r < 0) 272 goto disable_radio; 273 r = zd_chip_enable_hwint(chip); 274 if (r < 0) 275 goto disable_rxtx; 276 277 housekeeping_enable(mac); 278 return 0; 279disable_rxtx: 280 zd_chip_disable_rxtx(chip); 281disable_radio: 282 zd_chip_switch_radio_off(chip); 283disable_int: 284 zd_chip_disable_int(chip); 285out: 286 return r; 287} 288 289static void zd_op_stop(struct ieee80211_hw *hw) 290{ 291 struct zd_mac *mac = zd_hw_mac(hw); 292 struct zd_chip *chip = &mac->chip; 293 struct sk_buff *skb; 294 struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue; 295 296 /* The order here deliberately is a little different from the open() 297 * method, since we need to make sure there is no opportunity for RX 298 * frames to be processed by mac80211 after we have stopped it. 299 */ 300 301 zd_chip_disable_rxtx(chip); 302 housekeeping_disable(mac); 303 flush_workqueue(zd_workqueue); 304 305 zd_chip_disable_hwint(chip); 306 zd_chip_switch_radio_off(chip); 307 zd_chip_disable_int(chip); 308 309 310 while ((skb = skb_dequeue(ack_wait_queue))) 311 dev_kfree_skb_any(skb); 312} 313 314/** 315 * zd_mac_tx_status - reports tx status of a packet if required 316 * @hw - a &struct ieee80211_hw pointer 317 * @skb - a sk-buffer 318 * @flags: extra flags to set in the TX status info 319 * @ackssi: ACK signal strength 320 * @success - True for successful transmission of the frame 321 * 322 * This information calls ieee80211_tx_status_irqsafe() if required by the 323 * control information. It copies the control information into the status 324 * information. 325 * 326 * If no status information has been requested, the skb is freed. 327 */ 328static void zd_mac_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb, 329 int ackssi, struct tx_status *tx_status) 330{ 331 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 332 int i; 333 int success = 1, retry = 1; 334 int first_idx; 335 const struct tx_retry_rate *retries; 336 337 ieee80211_tx_info_clear_status(info); 338 339 if (tx_status) { 340 success = !tx_status->failure; 341 retry = tx_status->retry + success; 342 } 343 344 if (success) { 345 /* success */ 346 info->flags |= IEEE80211_TX_STAT_ACK; 347 } else { 348 /* failure */ 349 info->flags &= ~IEEE80211_TX_STAT_ACK; 350 } 351 352 first_idx = info->status.rates[0].idx; 353 ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates)); 354 retries = &zd_retry_rates[first_idx]; 355 ZD_ASSERT(1 <= retry && retry <= retries->count); 356 357 info->status.rates[0].idx = retries->rate[0]; 358 info->status.rates[0].count = 1; // (retry > 1 ? 2 : 1); 359 360 for (i=1; i<IEEE80211_TX_MAX_RATES-1 && i<retry; i++) { 361 info->status.rates[i].idx = retries->rate[i]; 362 info->status.rates[i].count = 1; // ((i==retry-1) && success ? 1:2); 363 } 364 for (; i<IEEE80211_TX_MAX_RATES && i<retry; i++) { 365 info->status.rates[i].idx = retries->rate[retry - 1]; 366 info->status.rates[i].count = 1; // (success ? 1:2); 367 } 368 if (i<IEEE80211_TX_MAX_RATES) 369 info->status.rates[i].idx = -1; /* terminate */ 370 371 info->status.ack_signal = ackssi; 372 ieee80211_tx_status_irqsafe(hw, skb); 373} 374 375/** 376 * zd_mac_tx_failed - callback for failed frames 377 * @dev: the mac80211 wireless device 378 * 379 * This function is called if a frame couldn't be successfully 380 * transferred. The first frame from the tx queue, will be selected and 381 * reported as error to the upper layers. 382 */ 383void zd_mac_tx_failed(struct urb *urb) 384{ 385 struct ieee80211_hw * hw = zd_usb_to_hw(urb->context); 386 struct zd_mac *mac = zd_hw_mac(hw); 387 struct sk_buff_head *q = &mac->ack_wait_queue; 388 struct sk_buff *skb; 389 struct tx_status *tx_status = (struct tx_status *)urb->transfer_buffer; 390 unsigned long flags; 391 int success = !tx_status->failure; 392 int retry = tx_status->retry + success; 393 int found = 0; 394 int i, position = 0; 395 396 q = &mac->ack_wait_queue; 397 spin_lock_irqsave(&q->lock, flags); 398 399 skb_queue_walk(q, skb) { 400 struct ieee80211_hdr *tx_hdr; 401 struct ieee80211_tx_info *info; 402 int first_idx, final_idx; 403 const struct tx_retry_rate *retries; 404 u8 final_rate; 405 406 position ++; 407 408 /* if the hardware reports a failure and we had a 802.11 ACK 409 * pending, then we skip the first skb when searching for a 410 * matching frame */ 411 if (tx_status->failure && mac->ack_pending && 412 skb_queue_is_first(q, skb)) { 413 continue; 414 } 415 416 tx_hdr = (struct ieee80211_hdr *)skb->data; 417 418 /* we skip all frames not matching the reported destination */ 419 if (unlikely(memcmp(tx_hdr->addr1, tx_status->mac, ETH_ALEN))) { 420 continue; 421 } 422 423 /* we skip all frames not matching the reported final rate */ 424 425 info = IEEE80211_SKB_CB(skb); 426 first_idx = info->status.rates[0].idx; 427 ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates)); 428 retries = &zd_retry_rates[first_idx]; 429 if (retry <= 0 || retry > retries->count) 430 continue; 431 432 final_idx = retries->rate[retry - 1]; 433 final_rate = zd_rates[final_idx].hw_value; 434 435 if (final_rate != tx_status->rate) { 436 continue; 437 } 438 439 found = 1; 440 break; 441 } 442 443 if (found) { 444 for (i=1; i<=position; i++) { 445 skb = __skb_dequeue(q); 446 zd_mac_tx_status(hw, skb, 447 mac->ack_pending ? mac->ack_signal : 0, 448 i == position ? tx_status : NULL); 449 mac->ack_pending = 0; 450 } 451 } 452 453 spin_unlock_irqrestore(&q->lock, flags); 454} 455 456/** 457 * zd_mac_tx_to_dev - callback for USB layer 458 * @skb: a &sk_buff pointer 459 * @error: error value, 0 if transmission successful 460 * 461 * Informs the MAC layer that the frame has successfully transferred to the 462 * device. If an ACK is required and the transfer to the device has been 463 * successful, the packets are put on the @ack_wait_queue with 464 * the control set removed. 465 */ 466void zd_mac_tx_to_dev(struct sk_buff *skb, int error) 467{ 468 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 469 struct ieee80211_hw *hw = info->rate_driver_data[0]; 470 struct zd_mac *mac = zd_hw_mac(hw); 471 472 ieee80211_tx_info_clear_status(info); 473 474 skb_pull(skb, sizeof(struct zd_ctrlset)); 475 if (unlikely(error || 476 (info->flags & IEEE80211_TX_CTL_NO_ACK))) { 477 ieee80211_tx_status_irqsafe(hw, skb); 478 } else { 479 struct sk_buff_head *q = &mac->ack_wait_queue; 480 481 skb_queue_tail(q, skb); 482 while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS) { 483 zd_mac_tx_status(hw, skb_dequeue(q), 484 mac->ack_pending ? mac->ack_signal : 0, 485 NULL); 486 mac->ack_pending = 0; 487 } 488 } 489} 490 491static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length) 492{ 493 /* ZD_PURE_RATE() must be used to remove the modulation type flag of 494 * the zd-rate values. 495 */ 496 static const u8 rate_divisor[] = { 497 [ZD_PURE_RATE(ZD_CCK_RATE_1M)] = 1, 498 [ZD_PURE_RATE(ZD_CCK_RATE_2M)] = 2, 499 /* Bits must be doubled. */ 500 [ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11, 501 [ZD_PURE_RATE(ZD_CCK_RATE_11M)] = 11, 502 [ZD_PURE_RATE(ZD_OFDM_RATE_6M)] = 6, 503 [ZD_PURE_RATE(ZD_OFDM_RATE_9M)] = 9, 504 [ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12, 505 [ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18, 506 [ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24, 507 [ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36, 508 [ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48, 509 [ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54, 510 }; 511 512 u32 bits = (u32)tx_length * 8; 513 u32 divisor; 514 515 divisor = rate_divisor[ZD_PURE_RATE(zd_rate)]; 516 if (divisor == 0) 517 return -EINVAL; 518 519 switch (zd_rate) { 520 case ZD_CCK_RATE_5_5M: 521 bits = (2*bits) + 10; /* round up to the next integer */ 522 break; 523 case ZD_CCK_RATE_11M: 524 if (service) { 525 u32 t = bits % 11; 526 *service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION; 527 if (0 < t && t <= 3) { 528 *service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION; 529 } 530 } 531 bits += 10; /* round up to the next integer */ 532 break; 533 } 534 535 return bits/divisor; 536} 537 538static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs, 539 struct ieee80211_hdr *header, 540 struct ieee80211_tx_info *info) 541{ 542 /* 543 * CONTROL TODO: 544 * - if backoff needed, enable bit 0 545 * - if burst (backoff not needed) disable bit 0 546 */ 547 548 cs->control = 0; 549 550 /* First fragment */ 551 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT) 552 cs->control |= ZD_CS_NEED_RANDOM_BACKOFF; 553 554 /* No ACK expected (multicast, etc.) */ 555 if (info->flags & IEEE80211_TX_CTL_NO_ACK) 556 cs->control |= ZD_CS_NO_ACK; 557 558 /* PS-POLL */ 559 if (ieee80211_is_pspoll(header->frame_control)) 560 cs->control |= ZD_CS_PS_POLL_FRAME; 561 562 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) 563 cs->control |= ZD_CS_RTS; 564 565 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) 566 cs->control |= ZD_CS_SELF_CTS; 567 568} 569 570static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon) 571{ 572 struct zd_mac *mac = zd_hw_mac(hw); 573 int r; 574 u32 tmp, j = 0; 575 /* 4 more bytes for tail CRC */ 576 u32 full_len = beacon->len + 4; 577 578 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 0); 579 if (r < 0) 580 return r; 581 r = zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp); 582 if (r < 0) 583 return r; 584 585 while (tmp & 0x2) { 586 r = zd_ioread32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, &tmp); 587 if (r < 0) 588 return r; 589 if ((++j % 100) == 0) { 590 printk(KERN_ERR "CR_BCN_FIFO_SEMAPHORE not ready\n"); 591 if (j >= 500) { 592 printk(KERN_ERR "Giving up beacon config.\n"); 593 return -ETIMEDOUT; 594 } 595 } 596 msleep(1); 597 } 598 599 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO, full_len - 1); 600 if (r < 0) 601 return r; 602 if (zd_chip_is_zd1211b(&mac->chip)) { 603 r = zd_iowrite32(&mac->chip, CR_BCN_LENGTH, full_len - 1); 604 if (r < 0) 605 return r; 606 } 607 608 for (j = 0 ; j < beacon->len; j++) { 609 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO, 610 *((u8 *)(beacon->data + j))); 611 if (r < 0) 612 return r; 613 } 614 615 for (j = 0; j < 4; j++) { 616 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO, 0x0); 617 if (r < 0) 618 return r; 619 } 620 621 r = zd_iowrite32(&mac->chip, CR_BCN_FIFO_SEMAPHORE, 1); 622 if (r < 0) 623 return r; 624 625 /* 802.11b/g 2.4G CCK 1Mb 626 * 802.11a, not yet implemented, uses different values (see GPL vendor 627 * driver) 628 */ 629 return zd_iowrite32(&mac->chip, CR_BCN_PLCP_CFG, 0x00000400 | 630 (full_len << 19)); 631} 632 633static int fill_ctrlset(struct zd_mac *mac, 634 struct sk_buff *skb) 635{ 636 int r; 637 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data; 638 unsigned int frag_len = skb->len + FCS_LEN; 639 unsigned int packet_length; 640 struct ieee80211_rate *txrate; 641 struct zd_ctrlset *cs = (struct zd_ctrlset *) 642 skb_push(skb, sizeof(struct zd_ctrlset)); 643 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 644 645 ZD_ASSERT(frag_len <= 0xffff); 646 647 txrate = ieee80211_get_tx_rate(mac->hw, info); 648 649 cs->modulation = txrate->hw_value; 650 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) 651 cs->modulation = txrate->hw_value_short; 652 653 cs->tx_length = cpu_to_le16(frag_len); 654 655 cs_set_control(mac, cs, hdr, info); 656 657 packet_length = frag_len + sizeof(struct zd_ctrlset) + 10; 658 ZD_ASSERT(packet_length <= 0xffff); 659 /* ZD1211B: Computing the length difference this way, gives us 660 * flexibility to compute the packet length. 661 */ 662 cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ? 663 packet_length - frag_len : packet_length); 664 665 /* 666 * CURRENT LENGTH: 667 * - transmit frame length in microseconds 668 * - seems to be derived from frame length 669 * - see Cal_Us_Service() in zdinlinef.h 670 * - if macp->bTxBurstEnable is enabled, then multiply by 4 671 * - bTxBurstEnable is never set in the vendor driver 672 * 673 * SERVICE: 674 * - "for PLCP configuration" 675 * - always 0 except in some situations at 802.11b 11M 676 * - see line 53 of zdinlinef.h 677 */ 678 cs->service = 0; 679 r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation), 680 le16_to_cpu(cs->tx_length)); 681 if (r < 0) 682 return r; 683 cs->current_length = cpu_to_le16(r); 684 cs->next_frame_length = 0; 685 686 return 0; 687} 688 689/** 690 * zd_op_tx - transmits a network frame to the device 691 * 692 * @dev: mac80211 hardware device 693 * @skb: socket buffer 694 * @control: the control structure 695 * 696 * This function transmit an IEEE 802.11 network frame to the device. The 697 * control block of the skbuff will be initialized. If necessary the incoming 698 * mac80211 queues will be stopped. 699 */ 700static int zd_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb) 701{ 702 struct zd_mac *mac = zd_hw_mac(hw); 703 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 704 int r; 705 706 r = fill_ctrlset(mac, skb); 707 if (r) 708 goto fail; 709 710 info->rate_driver_data[0] = hw; 711 712 r = zd_usb_tx(&mac->chip.usb, skb); 713 if (r) 714 goto fail; 715 return 0; 716 717fail: 718 dev_kfree_skb(skb); 719 return 0; 720} 721 722/** 723 * filter_ack - filters incoming packets for acknowledgements 724 * @dev: the mac80211 device 725 * @rx_hdr: received header 726 * @stats: the status for the received packet 727 * 728 * This functions looks for ACK packets and tries to match them with the 729 * frames in the tx queue. If a match is found the frame will be dequeued and 730 * the upper layers is informed about the successful transmission. If 731 * mac80211 queues have been stopped and the number of frames still to be 732 * transmitted is low the queues will be opened again. 733 * 734 * Returns 1 if the frame was an ACK, 0 if it was ignored. 735 */ 736static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr, 737 struct ieee80211_rx_status *stats) 738{ 739 struct zd_mac *mac = zd_hw_mac(hw); 740 struct sk_buff *skb; 741 struct sk_buff_head *q; 742 unsigned long flags; 743 int found = 0; 744 int i, position = 0; 745 746 if (!ieee80211_is_ack(rx_hdr->frame_control)) 747 return 0; 748 749 q = &mac->ack_wait_queue; 750 spin_lock_irqsave(&q->lock, flags); 751 skb_queue_walk(q, skb) { 752 struct ieee80211_hdr *tx_hdr; 753 754 position ++; 755 756 if (mac->ack_pending && skb_queue_is_first(q, skb)) 757 continue; 758 759 tx_hdr = (struct ieee80211_hdr *)skb->data; 760 if (likely(!memcmp(tx_hdr->addr2, rx_hdr->addr1, ETH_ALEN))) 761 { 762 found = 1; 763 break; 764 } 765 } 766 767 if (found) { 768 for (i=1; i<position; i++) { 769 skb = __skb_dequeue(q); 770 zd_mac_tx_status(hw, skb, 771 mac->ack_pending ? mac->ack_signal : 0, 772 NULL); 773 mac->ack_pending = 0; 774 } 775 776 mac->ack_pending = 1; 777 mac->ack_signal = stats->signal; 778 } 779 780 spin_unlock_irqrestore(&q->lock, flags); 781 return 1; 782} 783 784int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length) 785{ 786 struct zd_mac *mac = zd_hw_mac(hw); 787 struct ieee80211_rx_status stats; 788 const struct rx_status *status; 789 struct sk_buff *skb; 790 int bad_frame = 0; 791 __le16 fc; 792 int need_padding; 793 int i; 794 u8 rate; 795 796 if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ + 797 FCS_LEN + sizeof(struct rx_status)) 798 return -EINVAL; 799 800 memset(&stats, 0, sizeof(stats)); 801 802 /* Note about pass_failed_fcs and pass_ctrl access below: 803 * mac locking intentionally omitted here, as this is the only unlocked 804 * reader and the only writer is configure_filter. Plus, if there were 805 * any races accessing these variables, it wouldn't really matter. 806 * If mac80211 ever provides a way for us to access filter flags 807 * from outside configure_filter, we could improve on this. Also, this 808 * situation may change once we implement some kind of DMA-into-skb 809 * RX path. */ 810 811 /* Caller has to ensure that length >= sizeof(struct rx_status). */ 812 status = (struct rx_status *) 813 (buffer + (length - sizeof(struct rx_status))); 814 if (status->frame_status & ZD_RX_ERROR) { 815 if (mac->pass_failed_fcs && 816 (status->frame_status & ZD_RX_CRC32_ERROR)) { 817 stats.flag |= RX_FLAG_FAILED_FCS_CRC; 818 bad_frame = 1; 819 } else { 820 return -EINVAL; 821 } 822 } 823 824 stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq; 825 stats.band = IEEE80211_BAND_2GHZ; 826 stats.signal = status->signal_strength; 827 828 rate = zd_rx_rate(buffer, status); 829 830 /* todo: return index in the big switches in zd_rx_rate instead */ 831 for (i = 0; i < mac->band.n_bitrates; i++) 832 if (rate == mac->band.bitrates[i].hw_value) 833 stats.rate_idx = i; 834 835 length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status); 836 buffer += ZD_PLCP_HEADER_SIZE; 837 838 /* Except for bad frames, filter each frame to see if it is an ACK, in 839 * which case our internal TX tracking is updated. Normally we then 840 * bail here as there's no need to pass ACKs on up to the stack, but 841 * there is also the case where the stack has requested us to pass 842 * control frames on up (pass_ctrl) which we must consider. */ 843 if (!bad_frame && 844 filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats) 845 && !mac->pass_ctrl) 846 return 0; 847 848 fc = get_unaligned((__le16*)buffer); 849 need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc); 850 851 skb = dev_alloc_skb(length + (need_padding ? 2 : 0)); 852 if (skb == NULL) 853 return -ENOMEM; 854 if (need_padding) { 855 /* Make sure the payload data is 4 byte aligned. */ 856 skb_reserve(skb, 2); 857 } 858 859 memcpy(skb_put(skb, length), buffer, length); 860 861 memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats)); 862 ieee80211_rx_irqsafe(hw, skb); 863 return 0; 864} 865 866static int zd_op_add_interface(struct ieee80211_hw *hw, 867 struct ieee80211_vif *vif) 868{ 869 struct zd_mac *mac = zd_hw_mac(hw); 870 871 /* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */ 872 if (mac->type != NL80211_IFTYPE_UNSPECIFIED) 873 return -EOPNOTSUPP; 874 875 switch (vif->type) { 876 case NL80211_IFTYPE_MONITOR: 877 case NL80211_IFTYPE_MESH_POINT: 878 case NL80211_IFTYPE_STATION: 879 case NL80211_IFTYPE_ADHOC: 880 mac->type = vif->type; 881 break; 882 default: 883 return -EOPNOTSUPP; 884 } 885 886 return zd_write_mac_addr(&mac->chip, vif->addr); 887} 888 889static void zd_op_remove_interface(struct ieee80211_hw *hw, 890 struct ieee80211_vif *vif) 891{ 892 struct zd_mac *mac = zd_hw_mac(hw); 893 mac->type = NL80211_IFTYPE_UNSPECIFIED; 894 zd_set_beacon_interval(&mac->chip, 0); 895 zd_write_mac_addr(&mac->chip, NULL); 896} 897 898static int zd_op_config(struct ieee80211_hw *hw, u32 changed) 899{ 900 struct zd_mac *mac = zd_hw_mac(hw); 901 struct ieee80211_conf *conf = &hw->conf; 902 903 return zd_chip_set_channel(&mac->chip, conf->channel->hw_value); 904} 905 906static void zd_process_intr(struct work_struct *work) 907{ 908 u16 int_status; 909 struct zd_mac *mac = container_of(work, struct zd_mac, process_intr); 910 911 int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer+4)); 912 if (int_status & INT_CFG_NEXT_BCN) 913 dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n"); 914 else 915 dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n"); 916 917 zd_chip_enable_hwint(&mac->chip); 918} 919 920 921static void set_multicast_hash_handler(struct work_struct *work) 922{ 923 struct zd_mac *mac = 924 container_of(work, struct zd_mac, set_multicast_hash_work); 925 struct zd_mc_hash hash; 926 927 spin_lock_irq(&mac->lock); 928 hash = mac->multicast_hash; 929 spin_unlock_irq(&mac->lock); 930 931 zd_chip_set_multicast_hash(&mac->chip, &hash); 932} 933 934static void set_rx_filter_handler(struct work_struct *work) 935{ 936 struct zd_mac *mac = 937 container_of(work, struct zd_mac, set_rx_filter_work); 938 int r; 939 940 dev_dbg_f(zd_mac_dev(mac), "\n"); 941 r = set_rx_filter(mac); 942 if (r) 943 dev_err(zd_mac_dev(mac), "set_rx_filter_handler error %d\n", r); 944} 945 946static u64 zd_op_prepare_multicast(struct ieee80211_hw *hw, 947 struct netdev_hw_addr_list *mc_list) 948{ 949 struct zd_mac *mac = zd_hw_mac(hw); 950 struct zd_mc_hash hash; 951 struct netdev_hw_addr *ha; 952 953 zd_mc_clear(&hash); 954 955 netdev_hw_addr_list_for_each(ha, mc_list) { 956 dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n", ha->addr); 957 zd_mc_add_addr(&hash, ha->addr); 958 } 959 960 return hash.low | ((u64)hash.high << 32); 961} 962 963#define SUPPORTED_FIF_FLAGS \ 964 (FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \ 965 FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC) 966static void zd_op_configure_filter(struct ieee80211_hw *hw, 967 unsigned int changed_flags, 968 unsigned int *new_flags, 969 u64 multicast) 970{ 971 struct zd_mc_hash hash = { 972 .low = multicast, 973 .high = multicast >> 32, 974 }; 975 struct zd_mac *mac = zd_hw_mac(hw); 976 unsigned long flags; 977 978 /* Only deal with supported flags */ 979 changed_flags &= SUPPORTED_FIF_FLAGS; 980 *new_flags &= SUPPORTED_FIF_FLAGS; 981 982 /* 983 * If multicast parameter (as returned by zd_op_prepare_multicast) 984 * has changed, no bit in changed_flags is set. To handle this 985 * situation, we do not return if changed_flags is 0. If we do so, 986 * we will have some issue with IPv6 which uses multicast for link 987 * layer address resolution. 988 */ 989 if (*new_flags & (FIF_PROMISC_IN_BSS | FIF_ALLMULTI)) 990 zd_mc_add_all(&hash); 991 992 spin_lock_irqsave(&mac->lock, flags); 993 mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL); 994 mac->pass_ctrl = !!(*new_flags & FIF_CONTROL); 995 mac->multicast_hash = hash; 996 spin_unlock_irqrestore(&mac->lock, flags); 997 998 queue_work(zd_workqueue, &mac->set_multicast_hash_work); 999 1000 if (changed_flags & FIF_CONTROL) 1001 queue_work(zd_workqueue, &mac->set_rx_filter_work); 1002 1003 /* no handling required for FIF_OTHER_BSS as we don't currently 1004 * do BSSID filtering */ 1005} 1006 1007static void set_rts_cts_work(struct work_struct *work) 1008{ 1009 struct zd_mac *mac = 1010 container_of(work, struct zd_mac, set_rts_cts_work); 1011 unsigned long flags; 1012 unsigned int short_preamble; 1013 1014 mutex_lock(&mac->chip.mutex); 1015 1016 spin_lock_irqsave(&mac->lock, flags); 1017 mac->updating_rts_rate = 0; 1018 short_preamble = mac->short_preamble; 1019 spin_unlock_irqrestore(&mac->lock, flags); 1020 1021 zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble); 1022 mutex_unlock(&mac->chip.mutex); 1023} 1024 1025static void zd_op_bss_info_changed(struct ieee80211_hw *hw, 1026 struct ieee80211_vif *vif, 1027 struct ieee80211_bss_conf *bss_conf, 1028 u32 changes) 1029{ 1030 struct zd_mac *mac = zd_hw_mac(hw); 1031 unsigned long flags; 1032 int associated; 1033 1034 dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes); 1035 1036 if (mac->type == NL80211_IFTYPE_MESH_POINT || 1037 mac->type == NL80211_IFTYPE_ADHOC) { 1038 associated = true; 1039 if (changes & BSS_CHANGED_BEACON) { 1040 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif); 1041 1042 if (beacon) { 1043 zd_mac_config_beacon(hw, beacon); 1044 kfree_skb(beacon); 1045 } 1046 } 1047 1048 if (changes & BSS_CHANGED_BEACON_ENABLED) { 1049 u32 interval; 1050 1051 if (bss_conf->enable_beacon) 1052 interval = BCN_MODE_IBSS | 1053 bss_conf->beacon_int; 1054 else 1055 interval = 0; 1056 1057 zd_set_beacon_interval(&mac->chip, interval); 1058 } 1059 } else 1060 associated = is_valid_ether_addr(bss_conf->bssid); 1061 1062 spin_lock_irq(&mac->lock); 1063 mac->associated = associated; 1064 spin_unlock_irq(&mac->lock); 1065 1066 /* TODO: do hardware bssid filtering */ 1067 1068 if (changes & BSS_CHANGED_ERP_PREAMBLE) { 1069 spin_lock_irqsave(&mac->lock, flags); 1070 mac->short_preamble = bss_conf->use_short_preamble; 1071 if (!mac->updating_rts_rate) { 1072 mac->updating_rts_rate = 1; 1073 queue_work(zd_workqueue, &mac->set_rts_cts_work); 1074 } 1075 spin_unlock_irqrestore(&mac->lock, flags); 1076 } 1077} 1078 1079static u64 zd_op_get_tsf(struct ieee80211_hw *hw) 1080{ 1081 struct zd_mac *mac = zd_hw_mac(hw); 1082 return zd_chip_get_tsf(&mac->chip); 1083} 1084 1085static const struct ieee80211_ops zd_ops = { 1086 .tx = zd_op_tx, 1087 .start = zd_op_start, 1088 .stop = zd_op_stop, 1089 .add_interface = zd_op_add_interface, 1090 .remove_interface = zd_op_remove_interface, 1091 .config = zd_op_config, 1092 .prepare_multicast = zd_op_prepare_multicast, 1093 .configure_filter = zd_op_configure_filter, 1094 .bss_info_changed = zd_op_bss_info_changed, 1095 .get_tsf = zd_op_get_tsf, 1096}; 1097 1098struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf) 1099{ 1100 struct zd_mac *mac; 1101 struct ieee80211_hw *hw; 1102 1103 hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops); 1104 if (!hw) { 1105 dev_dbg_f(&intf->dev, "out of memory\n"); 1106 return NULL; 1107 } 1108 1109 mac = zd_hw_mac(hw); 1110 1111 memset(mac, 0, sizeof(*mac)); 1112 spin_lock_init(&mac->lock); 1113 mac->hw = hw; 1114 1115 mac->type = NL80211_IFTYPE_UNSPECIFIED; 1116 1117 memcpy(mac->channels, zd_channels, sizeof(zd_channels)); 1118 memcpy(mac->rates, zd_rates, sizeof(zd_rates)); 1119 mac->band.n_bitrates = ARRAY_SIZE(zd_rates); 1120 mac->band.bitrates = mac->rates; 1121 mac->band.n_channels = ARRAY_SIZE(zd_channels); 1122 mac->band.channels = mac->channels; 1123 1124 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &mac->band; 1125 1126 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS | 1127 IEEE80211_HW_SIGNAL_UNSPEC; 1128 1129 hw->wiphy->interface_modes = 1130 BIT(NL80211_IFTYPE_MESH_POINT) | 1131 BIT(NL80211_IFTYPE_STATION) | 1132 BIT(NL80211_IFTYPE_ADHOC); 1133 1134 hw->max_signal = 100; 1135 hw->queues = 1; 1136 hw->extra_tx_headroom = sizeof(struct zd_ctrlset); 1137 1138 /* 1139 * Tell mac80211 that we support multi rate retries 1140 */ 1141 hw->max_rates = IEEE80211_TX_MAX_RATES; 1142 hw->max_rate_tries = 18; /* 9 rates * 2 retries/rate */ 1143 1144 skb_queue_head_init(&mac->ack_wait_queue); 1145 mac->ack_pending = 0; 1146 1147 zd_chip_init(&mac->chip, hw, intf); 1148 housekeeping_init(mac); 1149 INIT_WORK(&mac->set_multicast_hash_work, set_multicast_hash_handler); 1150 INIT_WORK(&mac->set_rts_cts_work, set_rts_cts_work); 1151 INIT_WORK(&mac->set_rx_filter_work, set_rx_filter_handler); 1152 INIT_WORK(&mac->process_intr, zd_process_intr); 1153 1154 SET_IEEE80211_DEV(hw, &intf->dev); 1155 return hw; 1156} 1157 1158#define LINK_LED_WORK_DELAY HZ 1159 1160static void link_led_handler(struct work_struct *work) 1161{ 1162 struct zd_mac *mac = 1163 container_of(work, struct zd_mac, housekeeping.link_led_work.work); 1164 struct zd_chip *chip = &mac->chip; 1165 int is_associated; 1166 int r; 1167 1168 spin_lock_irq(&mac->lock); 1169 is_associated = mac->associated; 1170 spin_unlock_irq(&mac->lock); 1171 1172 r = zd_chip_control_leds(chip, 1173 is_associated ? ZD_LED_ASSOCIATED : ZD_LED_SCANNING); 1174 if (r) 1175 dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r); 1176 1177 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work, 1178 LINK_LED_WORK_DELAY); 1179} 1180 1181static void housekeeping_init(struct zd_mac *mac) 1182{ 1183 INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler); 1184} 1185 1186static void housekeeping_enable(struct zd_mac *mac) 1187{ 1188 dev_dbg_f(zd_mac_dev(mac), "\n"); 1189 queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work, 1190 0); 1191} 1192 1193static void housekeeping_disable(struct zd_mac *mac) 1194{ 1195 dev_dbg_f(zd_mac_dev(mac), "\n"); 1196 cancel_rearming_delayed_workqueue(zd_workqueue, 1197 &mac->housekeeping.link_led_work); 1198 zd_chip_control_leds(&mac->chip, ZD_LED_OFF); 1199} 1200