2 * hostapd / Hardware feature query and different modes
3 * Copyright 2002-2003, Instant802 Networks, Inc.
4 * Copyright 2005-2006, Devicescape Software, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Alternatively, this software may be distributed under the terms of BSD
13 * See README and COPYING for more details.
19 #include "hw_features.h"
25 void hostapd_free_hw_features(struct hostapd_hw_modes *hw_features,
26 size_t num_hw_features)
30 if (hw_features == NULL)
33 for (i = 0; i < num_hw_features; i++) {
34 os_free(hw_features[i].channels);
35 os_free(hw_features[i].rates);
42 int hostapd_get_hw_features(struct hostapd_iface *iface)
44 struct hostapd_data *hapd = iface->bss[0];
47 struct hostapd_hw_modes *modes;
49 if (hostapd_drv_none(hapd))
51 modes = hostapd_get_hw_feature_data(hapd, &num_modes, &flags);
53 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
55 "Fetching hardware channel/rate support not "
60 iface->hw_flags = flags;
62 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
63 iface->hw_features = modes;
64 iface->num_hw_features = num_modes;
66 for (i = 0; i < num_modes; i++) {
67 struct hostapd_hw_modes *feature = &modes[i];
68 /* set flag for channels we can use in current regulatory
70 for (j = 0; j < feature->num_channels; j++) {
72 * Disable all channels that are marked not to allow
73 * IBSS operation or active scanning. In addition,
74 * disable all channels that require radar detection,
75 * since that (in addition to full DFS) is not yet
78 if (feature->channels[j].flag &
79 (HOSTAPD_CHAN_NO_IBSS |
80 HOSTAPD_CHAN_PASSIVE_SCAN |
82 feature->channels[j].flag |=
83 HOSTAPD_CHAN_DISABLED;
84 if (feature->channels[j].flag & HOSTAPD_CHAN_DISABLED)
86 wpa_printf(MSG_MSGDUMP, "Allowed channel: mode=%d "
87 "chan=%d freq=%d MHz",
89 feature->channels[j].chan,
90 feature->channels[j].freq);
98 static int hostapd_prepare_rates(struct hostapd_data *hapd,
99 struct hostapd_hw_modes *mode)
101 int i, num_basic_rates = 0;
102 int basic_rates_a[] = { 60, 120, 240, -1 };
103 int basic_rates_b[] = { 10, 20, -1 };
104 int basic_rates_g[] = { 10, 20, 55, 110, -1 };
107 if (hapd->iconf->basic_rates)
108 basic_rates = hapd->iconf->basic_rates;
109 else switch (mode->mode) {
110 case HOSTAPD_MODE_IEEE80211A:
111 basic_rates = basic_rates_a;
113 case HOSTAPD_MODE_IEEE80211B:
114 basic_rates = basic_rates_b;
116 case HOSTAPD_MODE_IEEE80211G:
117 basic_rates = basic_rates_g;
123 if (hostapd_set_rate_sets(hapd, hapd->iconf->supported_rates,
124 basic_rates, mode->mode)) {
125 wpa_printf(MSG_ERROR, "Failed to update rate sets in kernel "
129 os_free(hapd->iface->current_rates);
130 hapd->iface->num_rates = 0;
132 hapd->iface->current_rates =
133 os_malloc(mode->num_rates * sizeof(struct hostapd_rate_data));
134 if (!hapd->iface->current_rates) {
135 wpa_printf(MSG_ERROR, "Failed to allocate memory for rate "
140 for (i = 0; i < mode->num_rates; i++) {
141 struct hostapd_rate_data *rate;
143 if (hapd->iconf->supported_rates &&
144 !hostapd_rate_found(hapd->iconf->supported_rates,
145 mode->rates[i].rate))
148 rate = &hapd->iface->current_rates[hapd->iface->num_rates];
149 os_memcpy(rate, &mode->rates[i],
150 sizeof(struct hostapd_rate_data));
151 if (hostapd_rate_found(basic_rates, rate->rate)) {
152 rate->flags |= HOSTAPD_RATE_BASIC;
155 rate->flags &= ~HOSTAPD_RATE_BASIC;
156 wpa_printf(MSG_DEBUG, "RATE[%d] rate=%d flags=0x%x",
157 hapd->iface->num_rates, rate->rate, rate->flags);
158 hapd->iface->num_rates++;
161 if (hapd->iface->num_rates == 0 || num_basic_rates == 0) {
162 wpa_printf(MSG_ERROR, "No rates remaining in supported/basic "
163 "rate sets (%d,%d).",
164 hapd->iface->num_rates, num_basic_rates);
172 static void select_hw_mode_start(void *eloop_data, void *user_ctx);
173 static void select_hw_mode2_handler(void *eloop_data, void *user_ctx);
176 * select_hw_mode_finalize - Finish select HW mode & call the callback
177 * @iface: Pointer to interface data.
178 * @status: Status of the select HW mode (0 on success; -1 on failure).
179 * Returns: 0 on success; -1 on failure (e.g., was not in progress).
181 static int select_hw_mode_finalize(struct hostapd_iface *iface, int status)
185 if (!iface->hw_mode_sel_cb)
188 eloop_cancel_timeout(select_hw_mode_start, iface, NULL);
189 eloop_cancel_timeout(select_hw_mode2_handler, iface, NULL);
191 cb = iface->hw_mode_sel_cb;
193 iface->hw_mode_sel_cb = NULL;
202 * select_hw_mode2 - Select the hardware mode (part 2)
203 * @iface: Pointer to interface data.
204 * @status: Status of auto chanel selection.
206 * Setup the rates and passive scanning based on the configuration.
208 static void select_hw_mode2(struct hostapd_iface *iface, int status)
214 if (iface->current_mode == NULL) {
215 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
216 HOSTAPD_LEVEL_WARNING,
217 "Hardware does not support configured channel");
222 if (hostapd_prepare_rates(iface->bss[0], iface->current_mode)) {
223 wpa_printf(MSG_ERROR, "Failed to prepare rates table.");
224 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
225 HOSTAPD_LEVEL_WARNING,
226 "Failed to prepare rates table.");
231 ret = hostapd_passive_scan(iface->bss[0], 0,
232 iface->conf->passive_scan_mode,
233 iface->conf->passive_scan_interval,
234 iface->conf->passive_scan_listen,
237 wpa_printf(MSG_ERROR, "Could not set passive scanning: %s",
243 select_hw_mode_finalize(iface, ret);
248 * select_hw_mode2_handler - Calls select_hw_mode2 when auto chan isn't used
249 * @eloop_data: Stores the struct hostapd_iface * for the interface.
252 static void select_hw_mode2_handler(void *eloop_data, void *user_ctx)
254 struct hostapd_iface *iface = eloop_data;
256 select_hw_mode2(iface, 0);
261 * select_hw_mode1 - Select the hardware mode (part 1)
262 * @iface: Pointer to interface data.
263 * Returns: 0 on success; -1 on failure.
265 * Setup the hardware mode and channel based on the configuration.
266 * Schedules select_hw_mode2() to be called immediately or after automatic
267 * channel selection takes place.
269 static int select_hw_mode1(struct hostapd_iface *iface)
273 if (iface->num_hw_features < 1)
276 iface->current_mode = NULL;
277 for (i = 0; i < iface->num_hw_features; i++) {
278 struct hostapd_hw_modes *mode = &iface->hw_features[i];
279 if (mode->mode == (int) iface->conf->hw_mode) {
280 iface->current_mode = mode;
285 if (iface->current_mode == NULL) {
286 wpa_printf(MSG_ERROR, "Hardware does not support configured "
288 hostapd_logger(iface->bss[0], NULL, HOSTAPD_MODULE_IEEE80211,
289 HOSTAPD_LEVEL_WARNING,
290 "Hardware does not support configured mode "
291 "(%d)", (int) iface->conf->hw_mode);
296 for (j = 0; j < iface->current_mode->num_channels; j++) {
297 struct hostapd_channel_data *chan =
298 &iface->current_mode->channels[j];
299 if (!(chan->flag & HOSTAPD_CHAN_DISABLED) &&
300 (chan->chan == iface->conf->channel)) {
305 if (ok == 0 && iface->conf->channel != 0) {
306 hostapd_logger(iface->bss[0], NULL,
307 HOSTAPD_MODULE_IEEE80211,
308 HOSTAPD_LEVEL_WARNING,
309 "Configured channel (%d) not found from the "
310 "channel list of current mode (%d) %s",
311 iface->conf->channel,
312 iface->current_mode->mode,
313 hostapd_hw_mode_txt(iface->current_mode->mode));
314 iface->current_mode = NULL;
318 * Calls select_hw_mode2() via a handler, so that the function is
319 * always executed from eloop.
321 eloop_register_timeout(0, 0, select_hw_mode2_handler, iface, NULL);
327 * select_hw_mode_start - Handler to start select HW mode
328 * @eloop_data: Stores the struct hostapd_iface * for the interface.
331 * An eloop handler is used so that all errors can be processed by the
332 * callback without introducing stack recursion.
334 static void select_hw_mode_start(void *eloop_data, void *user_ctx)
336 struct hostapd_iface *iface = (struct hostapd_iface *)eloop_data;
340 ret = select_hw_mode1(iface);
342 select_hw_mode_finalize(iface, ret);
347 * hostapd_select_hw_mode_start - Start selection of the hardware mode
348 * @iface: Pointer to interface data.
349 * @cb: The function to callback when done.
350 * Returns: 0 if it starts successfully; cb will be called when done.
351 * -1 on failure; cb will not be called.
353 * Sets up the hardware mode, channel, rates, and passive scanning
354 * based on the configuration.
356 int hostapd_select_hw_mode_start(struct hostapd_iface *iface,
359 if (iface->hw_mode_sel_cb) {
360 wpa_printf(MSG_DEBUG,
361 "%s: Hardware mode select already in progress.",
362 iface->bss[0]->conf->iface);
366 iface->hw_mode_sel_cb = cb;
368 eloop_register_timeout(0, 0, select_hw_mode_start, iface, NULL);
375 * hostapd_auto_chan_select_stop - Stops automatic channel selection
376 * @iface: Pointer to interface data.
377 * Returns: 0 if successfully stopped;
378 * -1 on failure (i.e., was not in progress)
380 int hostapd_select_hw_mode_stop(struct hostapd_iface *iface)
382 return select_hw_mode_finalize(iface, -1);
386 const char * hostapd_hw_mode_txt(int mode)
389 case HOSTAPD_MODE_IEEE80211A:
390 return "IEEE 802.11a";
391 case HOSTAPD_MODE_IEEE80211B:
392 return "IEEE 802.11b";
393 case HOSTAPD_MODE_IEEE80211G:
394 return "IEEE 802.11g";
401 int hostapd_hw_get_freq(struct hostapd_data *hapd, int chan)
405 if (!hapd->iface->current_mode)
408 for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
409 struct hostapd_channel_data *ch =
410 &hapd->iface->current_mode->channels[i];
411 if (ch->chan == chan)
419 int hostapd_hw_get_channel(struct hostapd_data *hapd, int freq)
423 if (!hapd->iface->current_mode)
426 for (i = 0; i < hapd->iface->current_mode->num_channels; i++) {
427 struct hostapd_channel_data *ch =
428 &hapd->iface->current_mode->channels[i];
429 if (ch->freq == freq)