1/* 2 Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com> 3 <http://rt2x00.serialmonkey.com> 4 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 17 Free Software Foundation, Inc., 18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19 */ 20 21/* 22 Module: rt2x00lib 23 Abstract: rt2x00 generic device routines. 24 */ 25 26#include <linux/kernel.h> 27#include <linux/module.h> 28#include <linux/slab.h> 29 30#include "rt2x00.h" 31#include "rt2x00lib.h" 32 33/* 34 * Radio control handlers. 35 */ 36int rt2x00lib_enable_radio(struct rt2x00_dev *rt2x00dev) 37{ 38 int status; 39 40 /* 41 * Don't enable the radio twice. 42 * And check if the hardware button has been disabled. 43 */ 44 if (test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags)) 45 return 0; 46 47 /* 48 * Initialize all data queues. 49 */ 50 rt2x00queue_init_queues(rt2x00dev); 51 52 /* 53 * Enable radio. 54 */ 55 status = 56 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_ON); 57 if (status) 58 return status; 59 60 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_ON); 61 62 rt2x00leds_led_radio(rt2x00dev, true); 63 rt2x00led_led_activity(rt2x00dev, true); 64 65 set_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags); 66 67 /* 68 * Enable RX. 69 */ 70 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON); 71 72 /* 73 * Start watchdog monitoring. 74 */ 75 rt2x00link_start_watchdog(rt2x00dev); 76 77 /* 78 * Start the TX queues. 79 */ 80 ieee80211_wake_queues(rt2x00dev->hw); 81 82 return 0; 83} 84 85void rt2x00lib_disable_radio(struct rt2x00_dev *rt2x00dev) 86{ 87 if (!test_and_clear_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags)) 88 return; 89 90 /* 91 * Stop the TX queues in mac80211. 92 */ 93 ieee80211_stop_queues(rt2x00dev->hw); 94 rt2x00queue_stop_queues(rt2x00dev); 95 96 /* 97 * Stop watchdog monitoring. 98 */ 99 rt2x00link_stop_watchdog(rt2x00dev); 100 101 /* 102 * Disable RX. 103 */ 104 rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF); 105 106 /* 107 * Disable radio. 108 */ 109 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_OFF); 110 rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_OFF); 111 rt2x00led_led_activity(rt2x00dev, false); 112 rt2x00leds_led_radio(rt2x00dev, false); 113} 114 115void rt2x00lib_toggle_rx(struct rt2x00_dev *rt2x00dev, enum dev_state state) 116{ 117 /* 118 * When we are disabling the RX, we should also stop the link tuner. 119 */ 120 if (state == STATE_RADIO_RX_OFF) 121 rt2x00link_stop_tuner(rt2x00dev); 122 123 rt2x00dev->ops->lib->set_device_state(rt2x00dev, state); 124 125 /* 126 * When we are enabling the RX, we should also start the link tuner. 127 */ 128 if (state == STATE_RADIO_RX_ON) 129 rt2x00link_start_tuner(rt2x00dev); 130} 131 132static void rt2x00lib_intf_scheduled_iter(void *data, u8 *mac, 133 struct ieee80211_vif *vif) 134{ 135 struct rt2x00_dev *rt2x00dev = data; 136 struct rt2x00_intf *intf = vif_to_intf(vif); 137 int delayed_flags; 138 139 /* 140 * Copy all data we need during this action under the protection 141 * of a spinlock. Otherwise race conditions might occur which results 142 * into an invalid configuration. 143 */ 144 spin_lock(&intf->lock); 145 146 delayed_flags = intf->delayed_flags; 147 intf->delayed_flags = 0; 148 149 spin_unlock(&intf->lock); 150 151 /* 152 * It is possible the radio was disabled while the work had been 153 * scheduled. If that happens we should return here immediately, 154 * note that in the spinlock protected area above the delayed_flags 155 * have been cleared correctly. 156 */ 157 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags)) 158 return; 159 160 if (delayed_flags & DELAYED_UPDATE_BEACON) 161 rt2x00queue_update_beacon(rt2x00dev, vif, true); 162} 163 164static void rt2x00lib_intf_scheduled(struct work_struct *work) 165{ 166 struct rt2x00_dev *rt2x00dev = 167 container_of(work, struct rt2x00_dev, intf_work); 168 169 /* 170 * Iterate over each interface and perform the 171 * requested configurations. 172 */ 173 ieee80211_iterate_active_interfaces(rt2x00dev->hw, 174 rt2x00lib_intf_scheduled_iter, 175 rt2x00dev); 176} 177 178/* 179 * Interrupt context handlers. 180 */ 181static void rt2x00lib_bc_buffer_iter(void *data, u8 *mac, 182 struct ieee80211_vif *vif) 183{ 184 struct rt2x00_dev *rt2x00dev = data; 185 struct sk_buff *skb; 186 187 /* 188 * Only AP mode interfaces do broad- and multicast buffering 189 */ 190 if (vif->type != NL80211_IFTYPE_AP) 191 return; 192 193 /* 194 * Send out buffered broad- and multicast frames 195 */ 196 skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif); 197 while (skb) { 198 rt2x00mac_tx(rt2x00dev->hw, skb); 199 skb = ieee80211_get_buffered_bc(rt2x00dev->hw, vif); 200 } 201} 202 203static void rt2x00lib_beaconupdate_iter(void *data, u8 *mac, 204 struct ieee80211_vif *vif) 205{ 206 struct rt2x00_dev *rt2x00dev = data; 207 208 if (vif->type != NL80211_IFTYPE_AP && 209 vif->type != NL80211_IFTYPE_ADHOC && 210 vif->type != NL80211_IFTYPE_MESH_POINT && 211 vif->type != NL80211_IFTYPE_WDS) 212 return; 213 214 rt2x00queue_update_beacon(rt2x00dev, vif, true); 215} 216 217void rt2x00lib_beacondone(struct rt2x00_dev *rt2x00dev) 218{ 219 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags)) 220 return; 221 222 /* send buffered bc/mc frames out for every bssid */ 223 ieee80211_iterate_active_interfaces(rt2x00dev->hw, 224 rt2x00lib_bc_buffer_iter, 225 rt2x00dev); 226 /* 227 * Devices with pre tbtt interrupt don't need to update the beacon 228 * here as they will fetch the next beacon directly prior to 229 * transmission. 230 */ 231 if (test_bit(DRIVER_SUPPORT_PRE_TBTT_INTERRUPT, &rt2x00dev->flags)) 232 return; 233 234 /* fetch next beacon */ 235 ieee80211_iterate_active_interfaces(rt2x00dev->hw, 236 rt2x00lib_beaconupdate_iter, 237 rt2x00dev); 238} 239EXPORT_SYMBOL_GPL(rt2x00lib_beacondone); 240 241void rt2x00lib_pretbtt(struct rt2x00_dev *rt2x00dev) 242{ 243 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags)) 244 return; 245 246 /* fetch next beacon */ 247 ieee80211_iterate_active_interfaces(rt2x00dev->hw, 248 rt2x00lib_beaconupdate_iter, 249 rt2x00dev); 250} 251EXPORT_SYMBOL_GPL(rt2x00lib_pretbtt); 252 253void rt2x00lib_txdone(struct queue_entry *entry, 254 struct txdone_entry_desc *txdesc) 255{ 256 struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev; 257 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(entry->skb); 258 struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb); 259 enum data_queue_qid qid = skb_get_queue_mapping(entry->skb); 260 unsigned int header_length = ieee80211_get_hdrlen_from_skb(entry->skb); 261 u8 rate_idx, rate_flags, retry_rates; 262 u8 skbdesc_flags = skbdesc->flags; 263 unsigned int i; 264 bool success; 265 266 /* 267 * Unmap the skb. 268 */ 269 rt2x00queue_unmap_skb(rt2x00dev, entry->skb); 270 271 /* 272 * Remove the extra tx headroom from the skb. 273 */ 274 skb_pull(entry->skb, rt2x00dev->ops->extra_tx_headroom); 275 276 /* 277 * Signal that the TX descriptor is no longer in the skb. 278 */ 279 skbdesc->flags &= ~SKBDESC_DESC_IN_SKB; 280 281 /* 282 * Remove L2 padding which was added during 283 */ 284 if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags)) 285 rt2x00queue_remove_l2pad(entry->skb, header_length); 286 287 /* 288 * If the IV/EIV data was stripped from the frame before it was 289 * passed to the hardware, we should now reinsert it again because 290 * mac80211 will expect the same data to be present it the 291 * frame as it was passed to us. 292 */ 293 if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags)) 294 rt2x00crypto_tx_insert_iv(entry->skb, header_length); 295 296 /* 297 * Send frame to debugfs immediately, after this call is completed 298 * we are going to overwrite the skb->cb array. 299 */ 300 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_TXDONE, entry->skb); 301 302 /* 303 * Determine if the frame has been successfully transmitted. 304 */ 305 success = 306 test_bit(TXDONE_SUCCESS, &txdesc->flags) || 307 test_bit(TXDONE_UNKNOWN, &txdesc->flags); 308 309 /* 310 * Update TX statistics. 311 */ 312 rt2x00dev->link.qual.tx_success += success; 313 rt2x00dev->link.qual.tx_failed += !success; 314 315 rate_idx = skbdesc->tx_rate_idx; 316 rate_flags = skbdesc->tx_rate_flags; 317 retry_rates = test_bit(TXDONE_FALLBACK, &txdesc->flags) ? 318 (txdesc->retry + 1) : 1; 319 320 /* 321 * Initialize TX status 322 */ 323 memset(&tx_info->status, 0, sizeof(tx_info->status)); 324 tx_info->status.ack_signal = 0; 325 326 /* 327 * Frame was send with retries, hardware tried 328 * different rates to send out the frame, at each 329 * retry it lowered the rate 1 step except when the 330 * lowest rate was used. 331 */ 332 for (i = 0; i < retry_rates && i < IEEE80211_TX_MAX_RATES; i++) { 333 tx_info->status.rates[i].idx = rate_idx - i; 334 tx_info->status.rates[i].flags = rate_flags; 335 336 if (rate_idx - i == 0) { 337 /* 338 * The lowest rate (index 0) was used until the 339 * number of max retries was reached. 340 */ 341 tx_info->status.rates[i].count = retry_rates - i; 342 i++; 343 break; 344 } 345 tx_info->status.rates[i].count = 1; 346 } 347 if (i < (IEEE80211_TX_MAX_RATES - 1)) 348 tx_info->status.rates[i].idx = -1; /* terminate */ 349 350 if (!(tx_info->flags & IEEE80211_TX_CTL_NO_ACK)) { 351 if (success) 352 tx_info->flags |= IEEE80211_TX_STAT_ACK; 353 else 354 rt2x00dev->low_level_stats.dot11ACKFailureCount++; 355 } 356 357 /* 358 * Every single frame has it's own tx status, hence report 359 * every frame as ampdu of size 1. 360 * 361 * TODO: if we can find out how many frames were aggregated 362 * by the hw we could provide the real ampdu_len to mac80211 363 * which would allow the rc algorithm to better decide on 364 * which rates are suitable. 365 */ 366 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU) { 367 tx_info->flags |= IEEE80211_TX_STAT_AMPDU; 368 tx_info->status.ampdu_len = 1; 369 tx_info->status.ampdu_ack_len = success ? 1 : 0; 370 } 371 372 if (rate_flags & IEEE80211_TX_RC_USE_RTS_CTS) { 373 if (success) 374 rt2x00dev->low_level_stats.dot11RTSSuccessCount++; 375 else 376 rt2x00dev->low_level_stats.dot11RTSFailureCount++; 377 } 378 379 /* 380 * Only send the status report to mac80211 when it's a frame 381 * that originated in mac80211. If this was a extra frame coming 382 * through a mac80211 library call (RTS/CTS) then we should not 383 * send the status report back. 384 */ 385 if (!(skbdesc_flags & SKBDESC_NOT_MAC80211)) 386 /* 387 * Only PCI and SOC devices process the tx status in process 388 * context. Hence use ieee80211_tx_status for PCI and SOC 389 * devices and stick to ieee80211_tx_status_irqsafe for USB. 390 */ 391 if (rt2x00_is_usb(rt2x00dev)) 392 ieee80211_tx_status_irqsafe(rt2x00dev->hw, entry->skb); 393 else 394 ieee80211_tx_status(rt2x00dev->hw, entry->skb); 395 else 396 dev_kfree_skb_any(entry->skb); 397 398 /* 399 * Make this entry available for reuse. 400 */ 401 entry->skb = NULL; 402 entry->flags = 0; 403 404 rt2x00dev->ops->lib->clear_entry(entry); 405 406 clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags); 407 rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE); 408 409 /* 410 * If the data queue was below the threshold before the txdone 411 * handler we must make sure the packet queue in the mac80211 stack 412 * is reenabled when the txdone handler has finished. 413 */ 414 if (!rt2x00queue_threshold(entry->queue)) 415 ieee80211_wake_queue(rt2x00dev->hw, qid); 416} 417EXPORT_SYMBOL_GPL(rt2x00lib_txdone); 418 419static int rt2x00lib_rxdone_read_signal(struct rt2x00_dev *rt2x00dev, 420 struct rxdone_entry_desc *rxdesc) 421{ 422 struct ieee80211_supported_band *sband; 423 const struct rt2x00_rate *rate; 424 unsigned int i; 425 int signal; 426 int type; 427 428 /* 429 * For non-HT rates the MCS value needs to contain the 430 * actually used rate modulation (CCK or OFDM). 431 */ 432 if (rxdesc->dev_flags & RXDONE_SIGNAL_MCS) 433 signal = RATE_MCS(rxdesc->rate_mode, rxdesc->signal); 434 else 435 signal = rxdesc->signal; 436 437 type = (rxdesc->dev_flags & RXDONE_SIGNAL_MASK); 438 439 sband = &rt2x00dev->bands[rt2x00dev->curr_band]; 440 for (i = 0; i < sband->n_bitrates; i++) { 441 rate = rt2x00_get_rate(sband->bitrates[i].hw_value); 442 443 if (((type == RXDONE_SIGNAL_PLCP) && 444 (rate->plcp == signal)) || 445 ((type == RXDONE_SIGNAL_BITRATE) && 446 (rate->bitrate == signal)) || 447 ((type == RXDONE_SIGNAL_MCS) && 448 (rate->mcs == signal))) { 449 return i; 450 } 451 } 452 453 WARNING(rt2x00dev, "Frame received with unrecognized signal, " 454 "signal=0x%.4x, type=%d.\n", signal, type); 455 return 0; 456} 457 458void rt2x00lib_rxdone(struct rt2x00_dev *rt2x00dev, 459 struct queue_entry *entry) 460{ 461 struct rxdone_entry_desc rxdesc; 462 struct sk_buff *skb; 463 struct ieee80211_rx_status *rx_status = &rt2x00dev->rx_status; 464 unsigned int header_length; 465 int rate_idx; 466 /* 467 * Allocate a new sk_buffer. If no new buffer available, drop the 468 * received frame and reuse the existing buffer. 469 */ 470 skb = rt2x00queue_alloc_rxskb(rt2x00dev, entry); 471 if (!skb) 472 return; 473 474 /* 475 * Unmap the skb. 476 */ 477 rt2x00queue_unmap_skb(rt2x00dev, entry->skb); 478 479 /* 480 * Extract the RXD details. 481 */ 482 memset(&rxdesc, 0, sizeof(rxdesc)); 483 rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc); 484 485 /* 486 * The data behind the ieee80211 header must be 487 * aligned on a 4 byte boundary. 488 */ 489 header_length = ieee80211_get_hdrlen_from_skb(entry->skb); 490 491 /* 492 * Hardware might have stripped the IV/EIV/ICV data, 493 * in that case it is possible that the data was 494 * provided separately (through hardware descriptor) 495 * in which case we should reinsert the data into the frame. 496 */ 497 if ((rxdesc.dev_flags & RXDONE_CRYPTO_IV) && 498 (rxdesc.flags & RX_FLAG_IV_STRIPPED)) 499 rt2x00crypto_rx_insert_iv(entry->skb, header_length, 500 &rxdesc); 501 else if (header_length && 502 (rxdesc.size > header_length) && 503 (rxdesc.dev_flags & RXDONE_L2PAD)) 504 rt2x00queue_remove_l2pad(entry->skb, header_length); 505 else 506 rt2x00queue_align_payload(entry->skb, header_length); 507 508 /* Trim buffer to correct size */ 509 skb_trim(entry->skb, rxdesc.size); 510 511 /* 512 * Check if the frame was received using HT. In that case, 513 * the rate is the MCS index and should be passed to mac80211 514 * directly. Otherwise we need to translate the signal to 515 * the correct bitrate index. 516 */ 517 if (rxdesc.rate_mode == RATE_MODE_CCK || 518 rxdesc.rate_mode == RATE_MODE_OFDM) { 519 rate_idx = rt2x00lib_rxdone_read_signal(rt2x00dev, &rxdesc); 520 } else { 521 rxdesc.flags |= RX_FLAG_HT; 522 rate_idx = rxdesc.signal; 523 } 524 525 /* 526 * Update extra components 527 */ 528 rt2x00link_update_stats(rt2x00dev, entry->skb, &rxdesc); 529 rt2x00debug_update_crypto(rt2x00dev, &rxdesc); 530 531 rx_status->mactime = rxdesc.timestamp; 532 rx_status->rate_idx = rate_idx; 533 rx_status->signal = rxdesc.rssi; 534 rx_status->flag = rxdesc.flags; 535 rx_status->antenna = rt2x00dev->link.ant.active.rx; 536 537 /* 538 * Send frame to mac80211 & debugfs. 539 * mac80211 will clean up the skb structure. 540 */ 541 rt2x00debug_dump_frame(rt2x00dev, DUMP_FRAME_RXDONE, entry->skb); 542 memcpy(IEEE80211_SKB_RXCB(entry->skb), rx_status, sizeof(*rx_status)); 543 544 /* 545 * Currently only PCI and SOC devices handle rx interrupts in process 546 * context. Hence, use ieee80211_rx_irqsafe for USB and ieee80211_rx_ni 547 * for PCI and SOC devices. 548 */ 549 if (rt2x00_is_usb(rt2x00dev)) 550 ieee80211_rx_irqsafe(rt2x00dev->hw, entry->skb); 551 else 552 ieee80211_rx_ni(rt2x00dev->hw, entry->skb); 553 554 /* 555 * Replace the skb with the freshly allocated one. 556 */ 557 entry->skb = skb; 558 entry->flags = 0; 559 560 rt2x00dev->ops->lib->clear_entry(entry); 561 562 rt2x00queue_index_inc(entry->queue, Q_INDEX); 563} 564EXPORT_SYMBOL_GPL(rt2x00lib_rxdone); 565 566/* 567 * Driver initialization handlers. 568 */ 569const struct rt2x00_rate rt2x00_supported_rates[12] = { 570 { 571 .flags = DEV_RATE_CCK, 572 .bitrate = 10, 573 .ratemask = BIT(0), 574 .plcp = 0x00, 575 .mcs = RATE_MCS(RATE_MODE_CCK, 0), 576 }, 577 { 578 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE, 579 .bitrate = 20, 580 .ratemask = BIT(1), 581 .plcp = 0x01, 582 .mcs = RATE_MCS(RATE_MODE_CCK, 1), 583 }, 584 { 585 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE, 586 .bitrate = 55, 587 .ratemask = BIT(2), 588 .plcp = 0x02, 589 .mcs = RATE_MCS(RATE_MODE_CCK, 2), 590 }, 591 { 592 .flags = DEV_RATE_CCK | DEV_RATE_SHORT_PREAMBLE, 593 .bitrate = 110, 594 .ratemask = BIT(3), 595 .plcp = 0x03, 596 .mcs = RATE_MCS(RATE_MODE_CCK, 3), 597 }, 598 { 599 .flags = DEV_RATE_OFDM, 600 .bitrate = 60, 601 .ratemask = BIT(4), 602 .plcp = 0x0b, 603 .mcs = RATE_MCS(RATE_MODE_OFDM, 0), 604 }, 605 { 606 .flags = DEV_RATE_OFDM, 607 .bitrate = 90, 608 .ratemask = BIT(5), 609 .plcp = 0x0f, 610 .mcs = RATE_MCS(RATE_MODE_OFDM, 1), 611 }, 612 { 613 .flags = DEV_RATE_OFDM, 614 .bitrate = 120, 615 .ratemask = BIT(6), 616 .plcp = 0x0a, 617 .mcs = RATE_MCS(RATE_MODE_OFDM, 2), 618 }, 619 { 620 .flags = DEV_RATE_OFDM, 621 .bitrate = 180, 622 .ratemask = BIT(7), 623 .plcp = 0x0e, 624 .mcs = RATE_MCS(RATE_MODE_OFDM, 3), 625 }, 626 { 627 .flags = DEV_RATE_OFDM, 628 .bitrate = 240, 629 .ratemask = BIT(8), 630 .plcp = 0x09, 631 .mcs = RATE_MCS(RATE_MODE_OFDM, 4), 632 }, 633 { 634 .flags = DEV_RATE_OFDM, 635 .bitrate = 360, 636 .ratemask = BIT(9), 637 .plcp = 0x0d, 638 .mcs = RATE_MCS(RATE_MODE_OFDM, 5), 639 }, 640 { 641 .flags = DEV_RATE_OFDM, 642 .bitrate = 480, 643 .ratemask = BIT(10), 644 .plcp = 0x08, 645 .mcs = RATE_MCS(RATE_MODE_OFDM, 6), 646 }, 647 { 648 .flags = DEV_RATE_OFDM, 649 .bitrate = 540, 650 .ratemask = BIT(11), 651 .plcp = 0x0c, 652 .mcs = RATE_MCS(RATE_MODE_OFDM, 7), 653 }, 654}; 655 656static void rt2x00lib_channel(struct ieee80211_channel *entry, 657 const int channel, const int tx_power, 658 const int value) 659{ 660 entry->center_freq = ieee80211_channel_to_frequency(channel); 661 entry->hw_value = value; 662 entry->max_power = tx_power; 663 entry->max_antenna_gain = 0xff; 664} 665 666static void rt2x00lib_rate(struct ieee80211_rate *entry, 667 const u16 index, const struct rt2x00_rate *rate) 668{ 669 entry->flags = 0; 670 entry->bitrate = rate->bitrate; 671 entry->hw_value =index; 672 entry->hw_value_short = index; 673 674 if (rate->flags & DEV_RATE_SHORT_PREAMBLE) 675 entry->flags |= IEEE80211_RATE_SHORT_PREAMBLE; 676} 677 678static int rt2x00lib_probe_hw_modes(struct rt2x00_dev *rt2x00dev, 679 struct hw_mode_spec *spec) 680{ 681 struct ieee80211_hw *hw = rt2x00dev->hw; 682 struct ieee80211_channel *channels; 683 struct ieee80211_rate *rates; 684 unsigned int num_rates; 685 unsigned int i; 686 687 num_rates = 0; 688 if (spec->supported_rates & SUPPORT_RATE_CCK) 689 num_rates += 4; 690 if (spec->supported_rates & SUPPORT_RATE_OFDM) 691 num_rates += 8; 692 693 channels = kzalloc(sizeof(*channels) * spec->num_channels, GFP_KERNEL); 694 if (!channels) 695 return -ENOMEM; 696 697 rates = kzalloc(sizeof(*rates) * num_rates, GFP_KERNEL); 698 if (!rates) 699 goto exit_free_channels; 700 701 /* 702 * Initialize Rate list. 703 */ 704 for (i = 0; i < num_rates; i++) 705 rt2x00lib_rate(&rates[i], i, rt2x00_get_rate(i)); 706 707 /* 708 * Initialize Channel list. 709 */ 710 for (i = 0; i < spec->num_channels; i++) { 711 rt2x00lib_channel(&channels[i], 712 spec->channels[i].channel, 713 spec->channels_info[i].max_power, i); 714 } 715 716 /* 717 * Intitialize 802.11b, 802.11g 718 * Rates: CCK, OFDM. 719 * Channels: 2.4 GHz 720 */ 721 if (spec->supported_bands & SUPPORT_BAND_2GHZ) { 722 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_channels = 14; 723 rt2x00dev->bands[IEEE80211_BAND_2GHZ].n_bitrates = num_rates; 724 rt2x00dev->bands[IEEE80211_BAND_2GHZ].channels = channels; 725 rt2x00dev->bands[IEEE80211_BAND_2GHZ].bitrates = rates; 726 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = 727 &rt2x00dev->bands[IEEE80211_BAND_2GHZ]; 728 memcpy(&rt2x00dev->bands[IEEE80211_BAND_2GHZ].ht_cap, 729 &spec->ht, sizeof(spec->ht)); 730 } 731 732 /* 733 * Intitialize 802.11a 734 * Rates: OFDM. 735 * Channels: OFDM, UNII, HiperLAN2. 736 */ 737 if (spec->supported_bands & SUPPORT_BAND_5GHZ) { 738 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_channels = 739 spec->num_channels - 14; 740 rt2x00dev->bands[IEEE80211_BAND_5GHZ].n_bitrates = 741 num_rates - 4; 742 rt2x00dev->bands[IEEE80211_BAND_5GHZ].channels = &channels[14]; 743 rt2x00dev->bands[IEEE80211_BAND_5GHZ].bitrates = &rates[4]; 744 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = 745 &rt2x00dev->bands[IEEE80211_BAND_5GHZ]; 746 memcpy(&rt2x00dev->bands[IEEE80211_BAND_5GHZ].ht_cap, 747 &spec->ht, sizeof(spec->ht)); 748 } 749 750 return 0; 751 752 exit_free_channels: 753 kfree(channels); 754 ERROR(rt2x00dev, "Allocation ieee80211 modes failed.\n"); 755 return -ENOMEM; 756} 757 758static void rt2x00lib_remove_hw(struct rt2x00_dev *rt2x00dev) 759{ 760 if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags)) 761 ieee80211_unregister_hw(rt2x00dev->hw); 762 763 if (likely(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ])) { 764 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->channels); 765 kfree(rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ]->bitrates); 766 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL; 767 rt2x00dev->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL; 768 } 769 770 kfree(rt2x00dev->spec.channels_info); 771} 772 773static int rt2x00lib_probe_hw(struct rt2x00_dev *rt2x00dev) 774{ 775 struct hw_mode_spec *spec = &rt2x00dev->spec; 776 int status; 777 778 if (test_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags)) 779 return 0; 780 781 /* 782 * Initialize HW modes. 783 */ 784 status = rt2x00lib_probe_hw_modes(rt2x00dev, spec); 785 if (status) 786 return status; 787 788 /* 789 * Initialize HW fields. 790 */ 791 rt2x00dev->hw->queues = rt2x00dev->ops->tx_queues; 792 793 /* 794 * Initialize extra TX headroom required. 795 */ 796 rt2x00dev->hw->extra_tx_headroom = 797 max_t(unsigned int, IEEE80211_TX_STATUS_HEADROOM, 798 rt2x00dev->ops->extra_tx_headroom); 799 800 /* 801 * Take TX headroom required for alignment into account. 802 */ 803 if (test_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags)) 804 rt2x00dev->hw->extra_tx_headroom += RT2X00_L2PAD_SIZE; 805 else if (test_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags)) 806 rt2x00dev->hw->extra_tx_headroom += RT2X00_ALIGN_SIZE; 807 808 /* 809 * Register HW. 810 */ 811 status = ieee80211_register_hw(rt2x00dev->hw); 812 if (status) 813 return status; 814 815 set_bit(DEVICE_STATE_REGISTERED_HW, &rt2x00dev->flags); 816 817 return 0; 818} 819 820/* 821 * Initialization/uninitialization handlers. 822 */ 823static void rt2x00lib_uninitialize(struct rt2x00_dev *rt2x00dev) 824{ 825 if (!test_and_clear_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags)) 826 return; 827 828 /* 829 * Unregister extra components. 830 */ 831 rt2x00rfkill_unregister(rt2x00dev); 832 833 /* 834 * Allow the HW to uninitialize. 835 */ 836 rt2x00dev->ops->lib->uninitialize(rt2x00dev); 837 838 /* 839 * Free allocated queue entries. 840 */ 841 rt2x00queue_uninitialize(rt2x00dev); 842} 843 844static int rt2x00lib_initialize(struct rt2x00_dev *rt2x00dev) 845{ 846 int status; 847 848 if (test_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags)) 849 return 0; 850 851 /* 852 * Allocate all queue entries. 853 */ 854 status = rt2x00queue_initialize(rt2x00dev); 855 if (status) 856 return status; 857 858 /* 859 * Initialize the device. 860 */ 861 status = rt2x00dev->ops->lib->initialize(rt2x00dev); 862 if (status) { 863 rt2x00queue_uninitialize(rt2x00dev); 864 return status; 865 } 866 867 set_bit(DEVICE_STATE_INITIALIZED, &rt2x00dev->flags); 868 869 /* 870 * Register the extra components. 871 */ 872 rt2x00rfkill_register(rt2x00dev); 873 874 return 0; 875} 876 877int rt2x00lib_start(struct rt2x00_dev *rt2x00dev) 878{ 879 int retval; 880 881 if (test_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags)) 882 return 0; 883 884 /* 885 * If this is the first interface which is added, 886 * we should load the firmware now. 887 */ 888 retval = rt2x00lib_load_firmware(rt2x00dev); 889 if (retval) 890 return retval; 891 892 /* 893 * Initialize the device. 894 */ 895 retval = rt2x00lib_initialize(rt2x00dev); 896 if (retval) 897 return retval; 898 899 rt2x00dev->intf_ap_count = 0; 900 rt2x00dev->intf_sta_count = 0; 901 rt2x00dev->intf_associated = 0; 902 903 /* Enable the radio */ 904 retval = rt2x00lib_enable_radio(rt2x00dev); 905 if (retval) { 906 rt2x00queue_uninitialize(rt2x00dev); 907 return retval; 908 } 909 910 set_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags); 911 912 return 0; 913} 914 915void rt2x00lib_stop(struct rt2x00_dev *rt2x00dev) 916{ 917 if (!test_and_clear_bit(DEVICE_STATE_STARTED, &rt2x00dev->flags)) 918 return; 919 920 /* 921 * Perhaps we can add something smarter here, 922 * but for now just disabling the radio should do. 923 */ 924 rt2x00lib_disable_radio(rt2x00dev); 925 926 rt2x00dev->intf_ap_count = 0; 927 rt2x00dev->intf_sta_count = 0; 928 rt2x00dev->intf_associated = 0; 929} 930 931/* 932 * driver allocation handlers. 933 */ 934int rt2x00lib_probe_dev(struct rt2x00_dev *rt2x00dev) 935{ 936 int retval = -ENOMEM; 937 938 mutex_init(&rt2x00dev->csr_mutex); 939 940 set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags); 941 942 /* 943 * Make room for rt2x00_intf inside the per-interface 944 * structure ieee80211_vif. 945 */ 946 rt2x00dev->hw->vif_data_size = sizeof(struct rt2x00_intf); 947 948 /* 949 * Determine which operating modes are supported, all modes 950 * which require beaconing, depend on the availability of 951 * beacon entries. 952 */ 953 rt2x00dev->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION); 954 if (rt2x00dev->ops->bcn->entry_num > 0) 955 rt2x00dev->hw->wiphy->interface_modes |= 956 BIT(NL80211_IFTYPE_ADHOC) | 957 BIT(NL80211_IFTYPE_AP) | 958 BIT(NL80211_IFTYPE_MESH_POINT) | 959 BIT(NL80211_IFTYPE_WDS); 960 961 /* 962 * Initialize configuration work. 963 */ 964 INIT_WORK(&rt2x00dev->intf_work, rt2x00lib_intf_scheduled); 965 966 /* 967 * Let the driver probe the device to detect the capabilities. 968 */ 969 retval = rt2x00dev->ops->lib->probe_hw(rt2x00dev); 970 if (retval) { 971 ERROR(rt2x00dev, "Failed to allocate device.\n"); 972 goto exit; 973 } 974 975 /* 976 * Allocate queue array. 977 */ 978 retval = rt2x00queue_allocate(rt2x00dev); 979 if (retval) 980 goto exit; 981 982 /* 983 * Initialize ieee80211 structure. 984 */ 985 retval = rt2x00lib_probe_hw(rt2x00dev); 986 if (retval) { 987 ERROR(rt2x00dev, "Failed to initialize hw.\n"); 988 goto exit; 989 } 990 991 /* 992 * Register extra components. 993 */ 994 rt2x00link_register(rt2x00dev); 995 rt2x00leds_register(rt2x00dev); 996 rt2x00debug_register(rt2x00dev); 997 998 return 0; 999 1000exit: 1001 rt2x00lib_remove_dev(rt2x00dev); 1002 1003 return retval; 1004} 1005EXPORT_SYMBOL_GPL(rt2x00lib_probe_dev); 1006 1007void rt2x00lib_remove_dev(struct rt2x00_dev *rt2x00dev) 1008{ 1009 clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags); 1010 1011 /* 1012 * Disable radio. 1013 */ 1014 rt2x00lib_disable_radio(rt2x00dev); 1015 1016 /* 1017 * Stop all work. 1018 */ 1019 cancel_work_sync(&rt2x00dev->intf_work); 1020 1021 /* 1022 * Uninitialize device. 1023 */ 1024 rt2x00lib_uninitialize(rt2x00dev); 1025 1026 /* 1027 * Free extra components 1028 */ 1029 rt2x00debug_deregister(rt2x00dev); 1030 rt2x00leds_unregister(rt2x00dev); 1031 1032 /* 1033 * Free ieee80211_hw memory. 1034 */ 1035 rt2x00lib_remove_hw(rt2x00dev); 1036 1037 /* 1038 * Free firmware image. 1039 */ 1040 rt2x00lib_free_firmware(rt2x00dev); 1041 1042 /* 1043 * Free queue structures. 1044 */ 1045 rt2x00queue_free(rt2x00dev); 1046} 1047EXPORT_SYMBOL_GPL(rt2x00lib_remove_dev); 1048 1049/* 1050 * Device state handlers 1051 */ 1052#ifdef CONFIG_PM 1053int rt2x00lib_suspend(struct rt2x00_dev *rt2x00dev, pm_message_t state) 1054{ 1055 NOTICE(rt2x00dev, "Going to sleep.\n"); 1056 1057 /* 1058 * Prevent mac80211 from accessing driver while suspended. 1059 */ 1060 if (!test_and_clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags)) 1061 return 0; 1062 1063 /* 1064 * Cleanup as much as possible. 1065 */ 1066 rt2x00lib_uninitialize(rt2x00dev); 1067 1068 /* 1069 * Suspend/disable extra components. 1070 */ 1071 rt2x00leds_suspend(rt2x00dev); 1072 rt2x00debug_deregister(rt2x00dev); 1073 1074 /* 1075 * Set device mode to sleep for power management, 1076 * on some hardware this call seems to consistently fail. 1077 * From the specifications it is hard to tell why it fails, 1078 * and if this is a "bad thing". 1079 * Overall it is safe to just ignore the failure and 1080 * continue suspending. The only downside is that the 1081 * device will not be in optimal power save mode, but with 1082 * the radio and the other components already disabled the 1083 * device is as good as disabled. 1084 */ 1085 if (rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_SLEEP)) 1086 WARNING(rt2x00dev, "Device failed to enter sleep state, " 1087 "continue suspending.\n"); 1088 1089 return 0; 1090} 1091EXPORT_SYMBOL_GPL(rt2x00lib_suspend); 1092 1093int rt2x00lib_resume(struct rt2x00_dev *rt2x00dev) 1094{ 1095 NOTICE(rt2x00dev, "Waking up.\n"); 1096 1097 /* 1098 * Restore/enable extra components. 1099 */ 1100 rt2x00debug_register(rt2x00dev); 1101 rt2x00leds_resume(rt2x00dev); 1102 1103 /* 1104 * We are ready again to receive requests from mac80211. 1105 */ 1106 set_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags); 1107 1108 return 0; 1109} 1110EXPORT_SYMBOL_GPL(rt2x00lib_resume); 1111#endif /* CONFIG_PM */ 1112 1113/* 1114 * rt2x00lib module information. 1115 */ 1116MODULE_AUTHOR(DRV_PROJECT); 1117MODULE_VERSION(DRV_VERSION); 1118MODULE_DESCRIPTION("rt2x00 library"); 1119MODULE_LICENSE("GPL"); 1120