189160efd2ae45e3265f97baafd40c472224556a
[deliverable/linux.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 * A lot of this code is generic and should be moved into the upper layers
20 * at some point.
21 */
22
23 #include <linux/errno.h>
24 #include <linux/wireless.h>
25 #include <linux/kernel.h>
26 #include <net/ieee80211.h>
27
28 #include "zd_def.h"
29 #include "zd_ieee80211.h"
30 #include "zd_mac.h"
31
32 static const struct channel_range channel_ranges[] = {
33 [0] = { 0, 0},
34 [ZD_REGDOMAIN_FCC] = { 1, 12},
35 [ZD_REGDOMAIN_IC] = { 1, 12},
36 [ZD_REGDOMAIN_ETSI] = { 1, 14},
37 [ZD_REGDOMAIN_JAPAN] = { 1, 14},
38 [ZD_REGDOMAIN_SPAIN] = { 1, 14},
39 [ZD_REGDOMAIN_FRANCE] = { 1, 14},
40
41 /* Japan originally only had channel 14 available (see CHNL_ID 0x40 in
42 * 802.11). However, in 2001 the range was extended to include channels
43 * 1-13. The ZyDAS devices still use the old region code but are
44 * designed to allow the extra channel access in Japan. */
45 [ZD_REGDOMAIN_JAPAN_ADD] = { 1, 15},
46 };
47
48 const struct channel_range *zd_channel_range(u8 regdomain)
49 {
50 if (regdomain >= ARRAY_SIZE(channel_ranges))
51 regdomain = 0;
52 return &channel_ranges[regdomain];
53 }
54
55 int zd_regdomain_supports_channel(u8 regdomain, u8 channel)
56 {
57 const struct channel_range *range = zd_channel_range(regdomain);
58 return range->start <= channel && channel < range->end;
59 }
60
61 int zd_regdomain_supported(u8 regdomain)
62 {
63 const struct channel_range *range = zd_channel_range(regdomain);
64 return range->start != 0;
65 }
66
67 /* Stores channel frequencies in MHz. */
68 static const u16 channel_frequencies[] = {
69 2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447,
70 2452, 2457, 2462, 2467, 2472, 2484,
71 };
72
73 #define NUM_CHANNELS ARRAY_SIZE(channel_frequencies)
74
75 static int compute_freq(struct iw_freq *freq, u32 mhz, u32 hz)
76 {
77 u32 factor;
78
79 freq->e = 0;
80 if (mhz >= 1000000000U) {
81 pr_debug("zd1211 mhz %u to large\n", mhz);
82 freq->m = 0;
83 return -EINVAL;
84 }
85
86 factor = 1000;
87 while (mhz >= factor) {
88
89 freq->e += 1;
90 factor *= 10;
91 }
92
93 factor /= 1000U;
94 freq->m = mhz * (1000000U/factor) + hz/factor;
95
96 return 0;
97 }
98
99 int zd_channel_to_freq(struct iw_freq *freq, u8 channel)
100 {
101 if (channel > NUM_CHANNELS) {
102 freq->m = 0;
103 freq->e = 0;
104 return -EINVAL;
105 }
106 if (!channel) {
107 freq->m = 0;
108 freq->e = 0;
109 return -EINVAL;
110 }
111 return compute_freq(freq, channel_frequencies[channel-1], 0);
112 }
113
114 static int freq_to_mhz(const struct iw_freq *freq)
115 {
116 u32 factor;
117 int e;
118
119 /* Such high frequencies are not supported. */
120 if (freq->e > 6)
121 return -EINVAL;
122
123 factor = 1;
124 for (e = freq->e; e > 0; --e) {
125 factor *= 10;
126 }
127 factor = 1000000U / factor;
128
129 if (freq->m % factor) {
130 return -EINVAL;
131 }
132
133 return freq->m / factor;
134 }
135
136 int zd_find_channel(u8 *channel, const struct iw_freq *freq)
137 {
138 int i, r;
139 u32 mhz;
140
141 if (freq->m < 1000) {
142 if (freq->m > NUM_CHANNELS || freq->m == 0)
143 return -EINVAL;
144 *channel = freq->m;
145 return 1;
146 }
147
148 r = freq_to_mhz(freq);
149 if (r < 0)
150 return r;
151 mhz = r;
152
153 for (i = 0; i < NUM_CHANNELS; i++) {
154 if (mhz == channel_frequencies[i]) {
155 *channel = i+1;
156 return 1;
157 }
158 }
159
160 return -EINVAL;
161 }
162
163 int zd_geo_init(struct ieee80211_device *ieee, u8 regdomain)
164 {
165 struct ieee80211_geo geo;
166 const struct channel_range *range;
167 int i;
168 u8 channel;
169
170 dev_dbg(zd_mac_dev(zd_netdev_mac(ieee->dev)),
171 "regdomain %#04x\n", regdomain);
172
173 range = zd_channel_range(regdomain);
174 if (range->start == 0) {
175 dev_err(zd_mac_dev(zd_netdev_mac(ieee->dev)),
176 "zd1211 regdomain %#04x not supported\n",
177 regdomain);
178 return -EINVAL;
179 }
180
181 memset(&geo, 0, sizeof(geo));
182
183 for (i = 0, channel = range->start; channel < range->end; channel++) {
184 struct ieee80211_channel *chan = &geo.bg[i++];
185 chan->freq = channel_frequencies[channel - 1];
186 chan->channel = channel;
187 }
188
189 geo.bg_channels = i;
190 memcpy(geo.name, "XX ", 4);
191 ieee80211_set_geo(ieee, &geo);
192 return 0;
193 }
This page took 0.03568 seconds and 4 git commands to generate.