]> Pileus Git - ~andy/linux/blob - drivers/staging/brcm80211/util/bcmwifi.c
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[~andy/linux] / 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 <linux/module.h>
19 #include <bcmdefs.h>
20 #include <bcmutils.h>
21 #include <bcmwifi.h>
22
23 /*
24  * Verify the chanspec is using a legal set of parameters, i.e. that the
25  * chanspec specified a band, bw, ctl_sb and channel and that the
26  * combination could be legal given any set of circumstances.
27  * RETURNS: true is the chanspec is malformed, false if it looks good.
28  */
29 bool bcm_chspec_malformed(chanspec_t chanspec)
30 {
31         /* must be 2G or 5G band */
32         if (!CHSPEC_IS5G(chanspec) && !CHSPEC_IS2G(chanspec))
33                 return true;
34         /* must be 20 or 40 bandwidth */
35         if (!CHSPEC_IS40(chanspec) && !CHSPEC_IS20(chanspec))
36                 return true;
37
38         /* 20MHZ b/w must have no ctl sb, 40 must have a ctl sb */
39         if (CHSPEC_IS20(chanspec)) {
40                 if (!CHSPEC_SB_NONE(chanspec))
41                         return true;
42         } else {
43                 if (!CHSPEC_SB_UPPER(chanspec) && !CHSPEC_SB_LOWER(chanspec))
44                         return true;
45         }
46
47         return false;
48 }
49 EXPORT_SYMBOL(bcm_chspec_malformed);
50
51 /*
52  * This function returns the channel number that control traffic is being sent on, for legacy
53  * channels this is just the channel number, for 40MHZ channels it is the upper or lowre 20MHZ
54  * sideband depending on the chanspec selected
55  */
56 u8 bcm_chspec_ctlchan(chanspec_t chspec)
57 {
58         u8 ctl_chan;
59
60         /* Is there a sideband ? */
61         if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_NONE) {
62                 return CHSPEC_CHANNEL(chspec);
63         } else {
64                 /* we only support 40MHZ with sidebands */
65                 /* chanspec channel holds the centre frequency, use that and the
66                  * side band information to reconstruct the control channel number
67                  */
68                 if (CHSPEC_CTL_SB(chspec) == WL_CHANSPEC_CTL_SB_UPPER) {
69                         /* control chan is the upper 20 MHZ SB of the 40MHZ channel */
70                         ctl_chan = UPPER_20_SB(CHSPEC_CHANNEL(chspec));
71                 } else {
72                         /* control chan is the lower 20 MHZ SB of the 40MHZ channel */
73                         ctl_chan = LOWER_20_SB(CHSPEC_CHANNEL(chspec));
74                 }
75         }
76
77         return ctl_chan;
78 }
79 EXPORT_SYMBOL(bcm_chspec_ctlchan);
80
81 /*
82  * Return the channel number for a given frequency and base frequency.
83  * The returned channel number is relative to the given base frequency.
84  * If the given base frequency is zero, a base frequency of 5 GHz is assumed for
85  * frequencies from 5 - 6 GHz, and 2.407 GHz is assumed for 2.4 - 2.5 GHz.
86  *
87  * Frequency is specified in MHz.
88  * The base frequency is specified as (start_factor * 500 kHz).
89  * Constants WF_CHAN_FACTOR_2_4_G, WF_CHAN_FACTOR_5_G are defined for
90  * 2.4 GHz and 5 GHz bands.
91  *
92  * The returned channel will be in the range [1, 14] in the 2.4 GHz band
93  * and [0, 200] otherwise.
94  * -1 is returned if the start_factor is WF_CHAN_FACTOR_2_4_G and the
95  * frequency is not a 2.4 GHz channel, or if the frequency is not and even
96  * multiple of 5 MHz from the base frequency to the base plus 1 GHz.
97  *
98  * Reference 802.11 REVma, section 17.3.8.3, and 802.11B section 18.4.6.2
99  */
100 int bcm_mhz2channel(uint freq, uint start_factor)
101 {
102         int ch = -1;
103         uint base;
104         int offset;
105
106         /* take the default channel start frequency */
107         if (start_factor == 0) {
108                 if (freq >= 2400 && freq <= 2500)
109                         start_factor = WF_CHAN_FACTOR_2_4_G;
110                 else if (freq >= 5000 && freq <= 6000)
111                         start_factor = WF_CHAN_FACTOR_5_G;
112         }
113
114         if (freq == 2484 && start_factor == WF_CHAN_FACTOR_2_4_G)
115                 return 14;
116
117         base = start_factor / 2;
118
119         /* check that the frequency is in 1GHz range of the base */
120         if ((freq < base) || (freq > base + 1000))
121                 return -1;
122
123         offset = freq - base;
124         ch = offset / 5;
125
126         /* check that frequency is a 5MHz multiple from the base */
127         if (offset != (ch * 5))
128                 return -1;
129
130         /* restricted channel range check for 2.4G */
131         if (start_factor == WF_CHAN_FACTOR_2_4_G && (ch < 1 || ch > 13))
132                 return -1;
133
134         return ch;
135 }
136 EXPORT_SYMBOL(bcm_mhz2channel);
137