Merge branch 'staging-next' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[pandora-kernel.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 #include <linux/ctype.h>
17 #include <linux/kernel.h>
18 #ifdef BRCM_FULLMAC
19 #include <linux/netdevice.h>
20 #endif
21 #include <osl.h>
22 #include <bcmdefs.h>
23 #include <bcmutils.h>
24 #include <bcmwifi.h>
25
26 /*
27  * Verify the chanspec is using a legal set of parameters, i.e. that the
28  * chanspec specified a band, bw, ctl_sb and channel and that the
29  * combination could be legal given any set of circumstances.
30  * RETURNS: true is the chanspec is malformed, false if it looks good.
31  */
32 bool wf_chspec_malformed(chanspec_t chanspec)
33 {
34         /* must be 2G or 5G band */
35         if (!CHSPEC_IS5G(chanspec) && !CHSPEC_IS2G(chanspec))
36                 return true;
37         /* must be 20 or 40 bandwidth */
38         if (!CHSPEC_IS40(chanspec) && !CHSPEC_IS20(chanspec))
39                 return true;
40
41         /* 20MHZ b/w must have no ctl sb, 40 must have a ctl sb */
42         if (CHSPEC_IS20(chanspec)) {
43                 if (!CHSPEC_SB_NONE(chanspec))
44                         return true;
45         } else {
46                 if (!CHSPEC_SB_UPPER(chanspec) && !CHSPEC_SB_LOWER(chanspec))
47                         return true;
48         }
49
50         return false;
51 }
52
53 /*
54  * This function returns the channel number that control traffic is being sent on, for legacy
55  * channels this is just the channel number, for 40MHZ channels it is the upper or lowre 20MHZ
56  * sideband depending on the chanspec selected
57  */
58 u8 wf_chspec_ctlchan(chanspec_t chspec)
59 {
60         u8 ctl_chan;
61
62         /* Is there a sideband ? */
63         if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_NONE) {
64                 return CHSPEC_CHANNEL(chspec);
65         } else {
66                 /* we only support 40MHZ with sidebands */
67                 ASSERT(CHSPEC_BW(chspec) == WL_CHANSPEC_BW_40);
68                 /* chanspec channel holds the centre frequency, use that and the
69                  * side band information to reconstruct the control channel number
70                  */
71                 if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_UPPER) {
72                         /* control chan is the upper 20 MHZ SB of the 40MHZ channel */
73                         ctl_chan = UPPER_20_SB(CHSPEC_CHANNEL(chspec));
74                 } else {
75                         ASSERT(CHSPEC_CTL_SB(chspec) ==
76                                WL_CHANSPEC_CTL_SB_LOWER);
77                         /* control chan is the lower 20 MHZ SB of the 40MHZ channel */
78                         ctl_chan = LOWER_20_SB(CHSPEC_CHANNEL(chspec));
79                 }
80         }
81
82         return ctl_chan;
83 }
84
85 chanspec_t wf_chspec_ctlchspec(chanspec_t chspec)
86 {
87         chanspec_t ctl_chspec = 0;
88         u8 channel;
89
90         ASSERT(!wf_chspec_malformed(chspec));
91
92         /* Is there a sideband ? */
93         if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_NONE) {
94                 return chspec;
95         } else {
96                 if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_UPPER) {
97                         channel = UPPER_20_SB(CHSPEC_CHANNEL(chspec));
98                 } else {
99                         channel = LOWER_20_SB(CHSPEC_CHANNEL(chspec));
100                 }
101                 ctl_chspec =
102                     channel | WL_CHANSPEC_BW_20 | WL_CHANSPEC_CTL_SB_NONE;
103                 ctl_chspec |= CHSPEC_BAND(chspec);
104         }
105         return ctl_chspec;
106 }
107
108 /*
109  * Return the channel number for a given frequency and base frequency.
110  * The returned channel number is relative to the given base frequency.
111  * If the given base frequency is zero, a base frequency of 5 GHz is assumed for
112  * frequencies from 5 - 6 GHz, and 2.407 GHz is assumed for 2.4 - 2.5 GHz.
113  *
114  * Frequency is specified in MHz.
115  * The base frequency is specified as (start_factor * 500 kHz).
116  * Constants WF_CHAN_FACTOR_2_4_G, WF_CHAN_FACTOR_5_G are defined for
117  * 2.4 GHz and 5 GHz bands.
118  *
119  * The returned channel will be in the range [1, 14] in the 2.4 GHz band
120  * and [0, 200] otherwise.
121  * -1 is returned if the start_factor is WF_CHAN_FACTOR_2_4_G and the
122  * frequency is not a 2.4 GHz channel, or if the frequency is not and even
123  * multiple of 5 MHz from the base frequency to the base plus 1 GHz.
124  *
125  * Reference 802.11 REVma, section 17.3.8.3, and 802.11B section 18.4.6.2
126  */
127 int wf_mhz2channel(uint freq, uint start_factor)
128 {
129         int ch = -1;
130         uint base;
131         int offset;
132
133         /* take the default channel start frequency */
134         if (start_factor == 0) {
135                 if (freq >= 2400 && freq <= 2500)
136                         start_factor = WF_CHAN_FACTOR_2_4_G;
137                 else if (freq >= 5000 && freq <= 6000)
138                         start_factor = WF_CHAN_FACTOR_5_G;
139         }
140
141         if (freq == 2484 && start_factor == WF_CHAN_FACTOR_2_4_G)
142                 return 14;
143
144         base = start_factor / 2;
145
146         /* check that the frequency is in 1GHz range of the base */
147         if ((freq < base) || (freq > base + 1000))
148                 return -1;
149
150         offset = freq - base;
151         ch = offset / 5;
152
153         /* check that frequency is a 5MHz multiple from the base */
154         if (offset != (ch * 5))
155                 return -1;
156
157         /* restricted channel range check for 2.4G */
158         if (start_factor == WF_CHAN_FACTOR_2_4_G && (ch < 1 || ch > 13))
159                 return -1;
160
161         return ch;
162 }
163
164 /*
165  * Return the center frequency in MHz of the given channel and base frequency.
166  * The channel number is interpreted relative to the given base frequency.
167  *
168  * The valid channel range is [1, 14] in the 2.4 GHz band and [0, 200] otherwise.
169  * The base frequency is specified as (start_factor * 500 kHz).
170  * Constants WF_CHAN_FACTOR_2_4_G, WF_CHAN_FACTOR_4_G, and WF_CHAN_FACTOR_5_G
171  * are defined for 2.4 GHz, 4 GHz, and 5 GHz bands.
172  * The channel range of [1, 14] is only checked for a start_factor of
173  * WF_CHAN_FACTOR_2_4_G (4814 = 2407 * 2).
174  * Odd start_factors produce channels on .5 MHz boundaries, in which case
175  * the answer is rounded down to an integral MHz.
176  * -1 is returned for an out of range channel.
177  *
178  * Reference 802.11 REVma, section 17.3.8.3, and 802.11B section 18.4.6.2
179  */
180 int wf_channel2mhz(uint ch, uint start_factor)
181 {
182         int freq;
183
184         if ((start_factor == WF_CHAN_FACTOR_2_4_G && (ch < 1 || ch > 14)) ||
185             (ch > 200))
186                 freq = -1;
187         else if ((start_factor == WF_CHAN_FACTOR_2_4_G) && (ch == 14))
188                 freq = 2484;
189         else
190                 freq = ch * 5 + start_factor / 2;
191
192         return freq;
193 }