|  | /* | 
|  | * Misc utility routines used by kernel or app-level. | 
|  | * Contents are wifi-specific, used by any kernel or app-level | 
|  | * software that might want wifi things as it grows. | 
|  | * | 
|  | * Copyright (C) 1999-2011, Broadcom Corporation | 
|  | * | 
|  | *         Unless you and Broadcom execute a separate written software license | 
|  | * agreement governing use of this software, this software is licensed to you | 
|  | * under the terms of the GNU General Public License version 2 (the "GPL"), | 
|  | * available at http://www.broadcom.com/licenses/GPLv2.php, with the | 
|  | * following added to such license: | 
|  | * | 
|  | *      As a special exception, the copyright holders of this software give you | 
|  | * permission to link this software with independent modules, and to copy and | 
|  | * distribute the resulting executable under terms of your choice, provided that | 
|  | * you also meet, for each linked independent module, the terms and conditions of | 
|  | * the license of that module.  An independent module is a module which is not | 
|  | * derived from this software.  The special exception does not apply to any | 
|  | * modifications of the software. | 
|  | * | 
|  | *      Notwithstanding the above, under no circumstances may you combine this | 
|  | * software in any way with any other Broadcom software provided under a license | 
|  | * other than the GPL, without Broadcom's express prior written consent. | 
|  | * $Id: bcmwifi.c,v 1.31.8.1 2010-08-03 17:47:05 Exp $ | 
|  | */ | 
|  |  | 
|  |  | 
|  | #include <typedefs.h> | 
|  |  | 
|  | #ifdef BCMDRIVER | 
|  | #include <osl.h> | 
|  | #include <bcmutils.h> | 
|  | #define strtoul(nptr, endptr, base) bcm_strtoul((nptr), (endptr), (base)) | 
|  | #define tolower(c) (bcm_isupper((c)) ? ((c) + 'a' - 'A') : (c)) | 
|  | #else | 
|  | #include <stdio.h> | 
|  | #include <stdlib.h> | 
|  | #include <ctype.h> | 
|  | #ifndef ASSERT | 
|  | #define ASSERT(exp) | 
|  | #endif | 
|  | #endif | 
|  | #include <bcmwifi.h> | 
|  |  | 
|  | #if defined(WIN32) && (defined(BCMDLL) || defined(WLMDLL)) | 
|  | #include <bcmstdlib.h> | 
|  | #endif | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  |  | 
|  | char * | 
|  | wf_chspec_ntoa(chanspec_t chspec, char *buf) | 
|  | { | 
|  | const char *band, *bw, *sb; | 
|  | uint channel; | 
|  |  | 
|  | band = ""; | 
|  | bw = ""; | 
|  | sb = ""; | 
|  | channel = CHSPEC_CHANNEL(chspec); | 
|  |  | 
|  | if ((CHSPEC_IS2G(chspec) && channel > CH_MAX_2G_CHANNEL) || | 
|  | (CHSPEC_IS5G(chspec) && channel <= CH_MAX_2G_CHANNEL)) | 
|  | band = (CHSPEC_IS2G(chspec)) ? "b" : "a"; | 
|  | if (CHSPEC_IS40(chspec)) { | 
|  | if (CHSPEC_SB_UPPER(chspec)) { | 
|  | sb = "u"; | 
|  | channel += CH_10MHZ_APART; | 
|  | } else { | 
|  | sb = "l"; | 
|  | channel -= CH_10MHZ_APART; | 
|  | } | 
|  | } else if (CHSPEC_IS10(chspec)) { | 
|  | bw = "n"; | 
|  | } | 
|  |  | 
|  |  | 
|  | snprintf(buf, 6, "%d%s%s%s", channel, band, bw, sb); | 
|  | return (buf); | 
|  | } | 
|  |  | 
|  |  | 
|  | chanspec_t | 
|  | wf_chspec_aton(char *a) | 
|  | { | 
|  | char *endp = NULL; | 
|  | uint channel, band, bw, ctl_sb; | 
|  | char c; | 
|  |  | 
|  | channel = strtoul(a, &endp, 10); | 
|  |  | 
|  |  | 
|  | if (endp == a) | 
|  | return 0; | 
|  |  | 
|  | if (channel > MAXCHANNEL) | 
|  | return 0; | 
|  |  | 
|  | band = ((channel <= CH_MAX_2G_CHANNEL) ? WL_CHANSPEC_BAND_2G : WL_CHANSPEC_BAND_5G); | 
|  | bw = WL_CHANSPEC_BW_20; | 
|  | ctl_sb = WL_CHANSPEC_CTL_SB_NONE; | 
|  |  | 
|  | a = endp; | 
|  |  | 
|  | c = tolower(a[0]); | 
|  | if (c == '\0') | 
|  | goto done; | 
|  |  | 
|  |  | 
|  | if (c == 'a' || c == 'b') { | 
|  | band = (c == 'a') ? WL_CHANSPEC_BAND_5G : WL_CHANSPEC_BAND_2G; | 
|  | a++; | 
|  | c = tolower(a[0]); | 
|  | if (c == '\0') | 
|  | goto done; | 
|  | } | 
|  |  | 
|  |  | 
|  | if (c == 'n') { | 
|  | bw = WL_CHANSPEC_BW_10; | 
|  | } else if (c == 'l') { | 
|  | bw = WL_CHANSPEC_BW_40; | 
|  | ctl_sb = WL_CHANSPEC_CTL_SB_LOWER; | 
|  |  | 
|  | if (channel <= (MAXCHANNEL - CH_20MHZ_APART)) | 
|  | channel += CH_10MHZ_APART; | 
|  | else | 
|  | return 0; | 
|  | } else if (c == 'u') { | 
|  | bw = WL_CHANSPEC_BW_40; | 
|  | ctl_sb = WL_CHANSPEC_CTL_SB_UPPER; | 
|  |  | 
|  | if (channel > CH_20MHZ_APART) | 
|  | channel -= CH_10MHZ_APART; | 
|  | else | 
|  | return 0; | 
|  | } else { | 
|  | return 0; | 
|  | } | 
|  |  | 
|  | done: | 
|  | return (channel | band | bw | ctl_sb); | 
|  | } | 
|  |  | 
|  |  | 
|  | bool | 
|  | wf_chspec_malformed(chanspec_t chanspec) | 
|  | { | 
|  |  | 
|  | if (!CHSPEC_IS5G(chanspec) && !CHSPEC_IS2G(chanspec)) | 
|  | return TRUE; | 
|  |  | 
|  | if (!CHSPEC_IS40(chanspec) && !CHSPEC_IS20(chanspec)) | 
|  | return TRUE; | 
|  |  | 
|  |  | 
|  | if (CHSPEC_IS20_UNCOND(chanspec)) { | 
|  | if (!CHSPEC_SB_NONE(chanspec)) | 
|  | return TRUE; | 
|  | } else { | 
|  | if (!CHSPEC_SB_UPPER(chanspec) && !CHSPEC_SB_LOWER(chanspec)) | 
|  | return TRUE; | 
|  | } | 
|  |  | 
|  | return FALSE; | 
|  | } | 
|  |  | 
|  |  | 
|  | uint8 | 
|  | wf_chspec_ctlchan(chanspec_t chspec) | 
|  | { | 
|  | uint8 ctl_chan; | 
|  |  | 
|  |  | 
|  | if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_NONE) { | 
|  | return CHSPEC_CHANNEL(chspec); | 
|  | } else { | 
|  |  | 
|  | ASSERT(CHSPEC_BW(chspec) == WL_CHANSPEC_BW_40); | 
|  |  | 
|  | if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_UPPER) { | 
|  |  | 
|  | ctl_chan = UPPER_20_SB(CHSPEC_CHANNEL(chspec)); | 
|  | } else { | 
|  | ASSERT(CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_LOWER); | 
|  |  | 
|  | ctl_chan = LOWER_20_SB(CHSPEC_CHANNEL(chspec)); | 
|  | } | 
|  | } | 
|  |  | 
|  | return ctl_chan; | 
|  | } | 
|  |  | 
|  | chanspec_t | 
|  | wf_chspec_ctlchspec(chanspec_t chspec) | 
|  | { | 
|  | chanspec_t ctl_chspec = 0; | 
|  | uint8 channel; | 
|  |  | 
|  | ASSERT(!wf_chspec_malformed(chspec)); | 
|  |  | 
|  |  | 
|  | if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_NONE) { | 
|  | return chspec; | 
|  | } else { | 
|  | if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_UPPER) { | 
|  | channel = UPPER_20_SB(CHSPEC_CHANNEL(chspec)); | 
|  | } else { | 
|  | channel = LOWER_20_SB(CHSPEC_CHANNEL(chspec)); | 
|  | } | 
|  | ctl_chspec = channel | WL_CHANSPEC_BW_20 | WL_CHANSPEC_CTL_SB_NONE; | 
|  | ctl_chspec |= CHSPEC_BAND(chspec); | 
|  | } | 
|  | return ctl_chspec; | 
|  | } | 
|  |  | 
|  |  | 
|  | int | 
|  | wf_mhz2channel(uint freq, uint start_factor) | 
|  | { | 
|  | int ch = -1; | 
|  | uint base; | 
|  | int offset; | 
|  |  | 
|  |  | 
|  | if (start_factor == 0) { | 
|  | if (freq >= 2400 && freq <= 2500) | 
|  | start_factor = WF_CHAN_FACTOR_2_4_G; | 
|  | else if (freq >= 5000 && freq <= 6000) | 
|  | start_factor = WF_CHAN_FACTOR_5_G; | 
|  | } | 
|  |  | 
|  | if (freq == 2484 && start_factor == WF_CHAN_FACTOR_2_4_G) | 
|  | return 14; | 
|  |  | 
|  | base = start_factor / 2; | 
|  |  | 
|  |  | 
|  | if ((freq < base) || (freq > base + 1000)) | 
|  | return -1; | 
|  |  | 
|  | offset = freq - base; | 
|  | ch = offset / 5; | 
|  |  | 
|  |  | 
|  | if (offset != (ch * 5)) | 
|  | return -1; | 
|  |  | 
|  |  | 
|  | if (start_factor == WF_CHAN_FACTOR_2_4_G && (ch < 1 || ch > 13)) | 
|  | return -1; | 
|  |  | 
|  | return ch; | 
|  | } | 
|  |  | 
|  |  | 
|  | int | 
|  | wf_channel2mhz(uint ch, uint start_factor) | 
|  | { | 
|  | int freq; | 
|  |  | 
|  | if ((start_factor == WF_CHAN_FACTOR_2_4_G && (ch < 1 || ch > 14)) || | 
|  | (ch > 200)) | 
|  | freq = -1; | 
|  | else if ((start_factor == WF_CHAN_FACTOR_2_4_G) && (ch == 14)) | 
|  | freq = 2484; | 
|  | else | 
|  | freq = ch * 5 + start_factor / 2; | 
|  |  | 
|  | return freq; | 
|  | } |