]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/staging/brcm80211/util/bcmwifi.c
83e7e1edeaa9504d0de9fa2820d2d34d760d65bb
[linux-beck.git] / drivers / staging / brcm80211 / util / bcmwifi.c
1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <typedefs.h>
18
19 #include <osl.h>
20 #include <linux/ctype.h>
21 #include <linux/kernel.h>
22 #include <bcmutils.h>
23 #include <bcmwifi.h>
24
25 /*
26  * Verify the chanspec is using a legal set of parameters, i.e. that the
27  * chanspec specified a band, bw, ctl_sb and channel and that the
28  * combination could be legal given any set of circumstances.
29  * RETURNS: true is the chanspec is malformed, false if it looks good.
30  */
31 bool wf_chspec_malformed(chanspec_t chanspec)
32 {
33         /* must be 2G or 5G band */
34         if (!CHSPEC_IS5G(chanspec) && !CHSPEC_IS2G(chanspec))
35                 return true;
36         /* must be 20 or 40 bandwidth */
37         if (!CHSPEC_IS40(chanspec) && !CHSPEC_IS20(chanspec))
38                 return true;
39
40         /* 20MHZ b/w must have no ctl sb, 40 must have a ctl sb */
41         if (CHSPEC_IS20(chanspec)) {
42                 if (!CHSPEC_SB_NONE(chanspec))
43                         return true;
44         } else {
45                 if (!CHSPEC_SB_UPPER(chanspec) && !CHSPEC_SB_LOWER(chanspec))
46                         return true;
47         }
48
49         return FALSE;
50 }
51
52 /*
53  * This function returns the channel number that control traffic is being sent on, for legacy
54  * channels this is just the channel number, for 40MHZ channels it is the upper or lowre 20MHZ
55  * sideband depending on the chanspec selected
56  */
57 u8 wf_chspec_ctlchan(chanspec_t chspec)
58 {
59         u8 ctl_chan;
60
61         /* Is there a sideband ? */
62         if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_NONE) {
63                 return CHSPEC_CHANNEL(chspec);
64         } else {
65                 /* we only support 40MHZ with sidebands */
66                 ASSERT(CHSPEC_BW(chspec) == WL_CHANSPEC_BW_40);
67                 /* chanspec channel holds the centre frequency, use that and the
68                  * side band information to reconstruct the control channel number
69                  */
70                 if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_UPPER) {
71                         /* control chan is the upper 20 MHZ SB of the 40MHZ channel */
72                         ctl_chan = UPPER_20_SB(CHSPEC_CHANNEL(chspec));
73                 } else {
74                         ASSERT(CHSPEC_CTL_SB(chspec) ==
75                                WL_CHANSPEC_CTL_SB_LOWER);
76                         /* control chan is the lower 20 MHZ SB of the 40MHZ channel */
77                         ctl_chan = LOWER_20_SB(CHSPEC_CHANNEL(chspec));
78                 }
79         }
80
81         return ctl_chan;
82 }
83
84 chanspec_t wf_chspec_ctlchspec(chanspec_t chspec)
85 {
86         chanspec_t ctl_chspec = 0;
87         u8 channel;
88
89         ASSERT(!wf_chspec_malformed(chspec));
90
91         /* Is there a sideband ? */
92         if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_NONE) {
93                 return chspec;
94         } else {
95                 if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_UPPER) {
96                         channel = UPPER_20_SB(CHSPEC_CHANNEL(chspec));
97                 } else {
98                         channel = LOWER_20_SB(CHSPEC_CHANNEL(chspec));
99                 }
100                 ctl_chspec =
101                     channel | WL_CHANSPEC_BW_20 | WL_CHANSPEC_CTL_SB_NONE;
102                 ctl_chspec |= CHSPEC_BAND(chspec);
103         }
104         return ctl_chspec;
105 }
106
107 /*
108  * Return the channel number for a given frequency and base frequency.
109  * The returned channel number is relative to the given base frequency.
110  * If the given base frequency is zero, a base frequency of 5 GHz is assumed for
111  * frequencies from 5 - 6 GHz, and 2.407 GHz is assumed for 2.4 - 2.5 GHz.
112  *
113  * Frequency is specified in MHz.
114  * The base frequency is specified as (start_factor * 500 kHz).
115  * Constants WF_CHAN_FACTOR_2_4_G, WF_CHAN_FACTOR_5_G are defined for
116  * 2.4 GHz and 5 GHz bands.
117  *
118  * The returned channel will be in the range [1, 14] in the 2.4 GHz band
119  * and [0, 200] otherwise.
120  * -1 is returned if the start_factor is WF_CHAN_FACTOR_2_4_G and the
121  * frequency is not a 2.4 GHz channel, or if the frequency is not and even
122  * multiple of 5 MHz from the base frequency to the base plus 1 GHz.
123  *
124  * Reference 802.11 REVma, section 17.3.8.3, and 802.11B section 18.4.6.2
125  */
126 int wf_mhz2channel(uint freq, uint start_factor)
127 {
128         int ch = -1;
129         uint base;
130         int offset;
131
132         /* take the default channel start frequency */
133         if (start_factor == 0) {
134                 if (freq >= 2400 && freq <= 2500)
135                         start_factor = WF_CHAN_FACTOR_2_4_G;
136                 else if (freq >= 5000 && freq <= 6000)
137                         start_factor = WF_CHAN_FACTOR_5_G;
138         }
139
140         if (freq == 2484 && start_factor == WF_CHAN_FACTOR_2_4_G)
141                 return 14;
142
143         base = start_factor / 2;
144
145         /* check that the frequency is in 1GHz range of the base */
146         if ((freq < base) || (freq > base + 1000))
147                 return -1;
148
149         offset = freq - base;
150         ch = offset / 5;
151
152         /* check that frequency is a 5MHz multiple from the base */
153         if (offset != (ch * 5))
154                 return -1;
155
156         /* restricted channel range check for 2.4G */
157         if (start_factor == WF_CHAN_FACTOR_2_4_G && (ch < 1 || ch > 13))
158                 return -1;
159
160         return ch;
161 }
162
163 /*
164  * Return the center frequency in MHz of the given channel and base frequency.
165  * The channel number is interpreted relative to the given base frequency.
166  *
167  * The valid channel range is [1, 14] in the 2.4 GHz band and [0, 200] otherwise.
168  * The base frequency is specified as (start_factor * 500 kHz).
169  * Constants WF_CHAN_FACTOR_2_4_G, WF_CHAN_FACTOR_4_G, and WF_CHAN_FACTOR_5_G
170  * are defined for 2.4 GHz, 4 GHz, and 5 GHz bands.
171  * The channel range of [1, 14] is only checked for a start_factor of
172  * WF_CHAN_FACTOR_2_4_G (4814 = 2407 * 2).
173  * Odd start_factors produce channels on .5 MHz boundaries, in which case
174  * the answer is rounded down to an integral MHz.
175  * -1 is returned for an out of range channel.
176  *
177  * Reference 802.11 REVma, section 17.3.8.3, and 802.11B section 18.4.6.2
178  */
179 int wf_channel2mhz(uint ch, uint start_factor)
180 {
181         int freq;
182
183         if ((start_factor == WF_CHAN_FACTOR_2_4_G && (ch < 1 || ch > 14)) ||
184             (ch > 200))
185                 freq = -1;
186         else if ((start_factor == WF_CHAN_FACTOR_2_4_G) && (ch == 14))
187                 freq = 2484;
188         else
189                 freq = ch * 5 + start_factor / 2;
190
191         return freq;
192 }