rk: reset drivers/net/wireless drivers/video/display/display-sysfs.c sound/soc/codecs...
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / bcmdhd / bcmwifi.c
1 /*
2  * Misc utility routines used by kernel or app-level.
3  * Contents are wifi-specific, used by any kernel or app-level
4  * software that might want wifi things as it grows.
5  *
6  * Copyright (C) 1999-2011, Broadcom Corporation
7  * 
8  *         Unless you and Broadcom execute a separate written software license
9  * agreement governing use of this software, this software is licensed to you
10  * under the terms of the GNU General Public License version 2 (the "GPL"),
11  * available at http://www.broadcom.com/licenses/GPLv2.php, with the
12  * following added to such license:
13  * 
14  *      As a special exception, the copyright holders of this software give you
15  * permission to link this software with independent modules, and to copy and
16  * distribute the resulting executable under terms of your choice, provided that
17  * you also meet, for each linked independent module, the terms and conditions of
18  * the license of that module.  An independent module is a module which is not
19  * derived from this software.  The special exception does not apply to any
20  * modifications of the software.
21  * 
22  *      Notwithstanding the above, under no circumstances may you combine this
23  * software in any way with any other Broadcom software provided under a license
24  * other than the GPL, without Broadcom's express prior written consent.
25  * $Id: bcmwifi.c,v 1.31.8.1 2010-08-03 17:47:05 Exp $
26  */
27
28
29 #include <typedefs.h>
30
31 #ifdef BCMDRIVER
32 #include <osl.h>
33 #include <bcmutils.h>
34 #define strtoul(nptr, endptr, base) bcm_strtoul((nptr), (endptr), (base))
35 #define tolower(c) (bcm_isupper((c)) ? ((c) + 'a' - 'A') : (c))
36 #else
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <ctype.h>
40 #ifndef ASSERT
41 #define ASSERT(exp)
42 #endif
43 #endif 
44 #include <bcmwifi.h>
45
46 #if defined(WIN32) && (defined(BCMDLL) || defined(WLMDLL))
47 #include <bcmstdlib.h>  
48 #endif
49
50
51
52
53
54 char *
55 wf_chspec_ntoa(chanspec_t chspec, char *buf)
56 {
57         const char *band, *bw, *sb;
58         uint channel;
59
60         band = "";
61         bw = "";
62         sb = "";
63         channel = CHSPEC_CHANNEL(chspec);
64         
65         if ((CHSPEC_IS2G(chspec) && channel > CH_MAX_2G_CHANNEL) ||
66             (CHSPEC_IS5G(chspec) && channel <= CH_MAX_2G_CHANNEL))
67                 band = (CHSPEC_IS2G(chspec)) ? "b" : "a";
68         if (CHSPEC_IS40(chspec)) {
69                 if (CHSPEC_SB_UPPER(chspec)) {
70                         sb = "u";
71                         channel += CH_10MHZ_APART;
72                 } else {
73                         sb = "l";
74                         channel -= CH_10MHZ_APART;
75                 }
76         } else if (CHSPEC_IS10(chspec)) {
77                 bw = "n";
78         }
79
80         
81         snprintf(buf, 6, "%d%s%s%s", channel, band, bw, sb);
82         return (buf);
83 }
84
85
86 chanspec_t
87 wf_chspec_aton(char *a)
88 {
89         char *endp = NULL;
90         uint channel, band, bw, ctl_sb;
91         char c;
92
93         channel = strtoul(a, &endp, 10);
94
95         
96         if (endp == a)
97                 return 0;
98
99         if (channel > MAXCHANNEL)
100                 return 0;
101
102         band = ((channel <= CH_MAX_2G_CHANNEL) ? WL_CHANSPEC_BAND_2G : WL_CHANSPEC_BAND_5G);
103         bw = WL_CHANSPEC_BW_20;
104         ctl_sb = WL_CHANSPEC_CTL_SB_NONE;
105
106         a = endp;
107
108         c = tolower(a[0]);
109         if (c == '\0')
110                 goto done;
111
112         
113         if (c == 'a' || c == 'b') {
114                 band = (c == 'a') ? WL_CHANSPEC_BAND_5G : WL_CHANSPEC_BAND_2G;
115                 a++;
116                 c = tolower(a[0]);
117                 if (c == '\0')
118                         goto done;
119         }
120
121         
122         if (c == 'n') {
123                 bw = WL_CHANSPEC_BW_10;
124         } else if (c == 'l') {
125                 bw = WL_CHANSPEC_BW_40;
126                 ctl_sb = WL_CHANSPEC_CTL_SB_LOWER;
127                 
128                 if (channel <= (MAXCHANNEL - CH_20MHZ_APART))
129                         channel += CH_10MHZ_APART;
130                 else
131                         return 0;
132         } else if (c == 'u') {
133                 bw = WL_CHANSPEC_BW_40;
134                 ctl_sb = WL_CHANSPEC_CTL_SB_UPPER;
135                 
136                 if (channel > CH_20MHZ_APART)
137                         channel -= CH_10MHZ_APART;
138                 else
139                         return 0;
140         } else {
141                 return 0;
142         }
143
144 done:
145         return (channel | band | bw | ctl_sb);
146 }
147
148
149 bool
150 wf_chspec_malformed(chanspec_t chanspec)
151 {
152         
153         if (!CHSPEC_IS5G(chanspec) && !CHSPEC_IS2G(chanspec))
154                 return TRUE;
155         
156         if (!CHSPEC_IS40(chanspec) && !CHSPEC_IS20(chanspec))
157                 return TRUE;
158
159         
160         if (CHSPEC_IS20_UNCOND(chanspec)) {
161                 if (!CHSPEC_SB_NONE(chanspec))
162                         return TRUE;
163         } else {
164                 if (!CHSPEC_SB_UPPER(chanspec) && !CHSPEC_SB_LOWER(chanspec))
165                 return TRUE;
166         }
167
168         return FALSE;
169 }
170
171
172 uint8
173 wf_chspec_ctlchan(chanspec_t chspec)
174 {
175         uint8 ctl_chan;
176
177         
178         if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_NONE) {
179                 return CHSPEC_CHANNEL(chspec);
180         } else {
181                 
182                 ASSERT(CHSPEC_BW(chspec) == WL_CHANSPEC_BW_40);
183                 
184                 if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_UPPER) {
185                         
186                         ctl_chan = UPPER_20_SB(CHSPEC_CHANNEL(chspec));
187                 } else {
188                         ASSERT(CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_LOWER);
189                         
190                         ctl_chan = LOWER_20_SB(CHSPEC_CHANNEL(chspec));
191                 }
192         }
193
194         return ctl_chan;
195 }
196
197 chanspec_t
198 wf_chspec_ctlchspec(chanspec_t chspec)
199 {
200         chanspec_t ctl_chspec = 0;
201         uint8 channel;
202
203         ASSERT(!wf_chspec_malformed(chspec));
204
205         
206         if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_NONE) {
207                 return chspec;
208         } else {
209                 if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_UPPER) {
210                         channel = UPPER_20_SB(CHSPEC_CHANNEL(chspec));
211                 } else {
212                         channel = LOWER_20_SB(CHSPEC_CHANNEL(chspec));
213                 }
214                 ctl_chspec = channel | WL_CHANSPEC_BW_20 | WL_CHANSPEC_CTL_SB_NONE;
215                 ctl_chspec |= CHSPEC_BAND(chspec);
216         }
217         return ctl_chspec;
218 }
219
220
221 int
222 wf_mhz2channel(uint freq, uint start_factor)
223 {
224         int ch = -1;
225         uint base;
226         int offset;
227
228         
229         if (start_factor == 0) {
230                 if (freq >= 2400 && freq <= 2500)
231                         start_factor = WF_CHAN_FACTOR_2_4_G;
232                 else if (freq >= 5000 && freq <= 6000)
233                         start_factor = WF_CHAN_FACTOR_5_G;
234         }
235
236         if (freq == 2484 && start_factor == WF_CHAN_FACTOR_2_4_G)
237                 return 14;
238
239         base = start_factor / 2;
240
241         
242         if ((freq < base) || (freq > base + 1000))
243                 return -1;
244
245         offset = freq - base;
246         ch = offset / 5;
247
248         
249         if (offset != (ch * 5))
250                 return -1;
251
252         
253         if (start_factor == WF_CHAN_FACTOR_2_4_G && (ch < 1 || ch > 13))
254                 return -1;
255
256         return ch;
257 }
258
259
260 int
261 wf_channel2mhz(uint ch, uint start_factor)
262 {
263         int freq;
264
265         if ((start_factor == WF_CHAN_FACTOR_2_4_G && (ch < 1 || ch > 14)) ||
266             (ch > 200))
267                 freq = -1;
268         else if ((start_factor == WF_CHAN_FACTOR_2_4_G) && (ch == 14))
269                 freq = 2484;
270         else
271                 freq = ch * 5 + start_factor / 2;
272
273         return freq;
274 }