zd1211rw: port to mac80211
[pandora-kernel.git] / drivers / net / wireless / zd1211rw / zd_ieee80211.c
1 /* zd_ieee80211.c
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17
18 /*
19  * In the long term, we'll probably find a better way of handling regulatory
20  * requirements outside of the driver.
21  */
22
23 #include <linux/kernel.h>
24 #include <net/mac80211.h>
25
26 #include "zd_ieee80211.h"
27 #include "zd_mac.h"
28
29 struct channel_range {
30         u8 regdomain;
31         u8 start;
32         u8 end; /* exclusive (channel must be less than end) */
33 };
34
35 static const struct channel_range channel_ranges[] = {
36         { ZD_REGDOMAIN_FCC,             1, 12 },
37         { ZD_REGDOMAIN_IC,              1, 12 },
38         { ZD_REGDOMAIN_ETSI,            1, 14 },
39         { ZD_REGDOMAIN_JAPAN,           1, 14 },
40         { ZD_REGDOMAIN_SPAIN,           1, 14 },
41         { ZD_REGDOMAIN_FRANCE,          1, 14 },
42
43         /* Japan originally only had channel 14 available (see CHNL_ID 0x40 in
44          * 802.11). However, in 2001 the range was extended to include channels
45          * 1-13. The ZyDAS devices still use the old region code but are
46          * designed to allow the extra channel access in Japan. */
47         { ZD_REGDOMAIN_JAPAN_ADD,       1, 15 },
48 };
49
50 static const struct channel_range *zd_channel_range(u8 regdomain)
51 {
52         int i;
53         for (i = 0; i < ARRAY_SIZE(channel_ranges); i++) {
54                 const struct channel_range *range = &channel_ranges[i];
55                 if (range->regdomain == regdomain)
56                         return range;
57         }
58         return NULL;
59 }
60
61 #define CHAN_TO_IDX(chan) ((chan) - 1)
62
63 static void unmask_bg_channels(struct ieee80211_hw *hw,
64         const struct channel_range *range,
65         struct ieee80211_hw_mode *mode)
66 {
67         u8 channel;
68
69         for (channel = range->start; channel < range->end; channel++) {
70                 struct ieee80211_channel *chan =
71                         &mode->channels[CHAN_TO_IDX(channel)];
72                 chan->flag |= IEEE80211_CHAN_W_SCAN |
73                         IEEE80211_CHAN_W_ACTIVE_SCAN |
74                         IEEE80211_CHAN_W_IBSS;
75         }
76 }
77
78 void zd_geo_init(struct ieee80211_hw *hw, u8 regdomain)
79 {
80         struct zd_mac *mac = zd_hw_mac(hw);
81         const struct channel_range *range;
82
83         dev_dbg(zd_mac_dev(mac), "regdomain %#02x\n", regdomain);
84
85         range = zd_channel_range(regdomain);
86         if (!range) {
87                 /* The vendor driver overrides the regulatory domain and
88                  * allowed channel registers and unconditionally restricts
89                  * available channels to 1-11 everywhere. Match their
90                  * questionable behaviour only for regdomains which we don't
91                  * recognise. */
92                 dev_warn(zd_mac_dev(mac), "Unrecognised regulatory domain: "
93                         "%#02x. Defaulting to FCC.\n", regdomain);
94                 range = zd_channel_range(ZD_REGDOMAIN_FCC);
95         }
96
97         unmask_bg_channels(hw, range, &mac->modes[0]);
98         unmask_bg_channels(hw, range, &mac->modes[1]);
99 }
100