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