Merge git://git.kernel.org/pub/scm/linux/kernel/git/linville/wireless-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / wl12xx / wl1271_cmd.c
bloba74259bb596bb99cd10ee3bc84630907dbf70b0f
1 /*
2 * This file is part of wl1271
4 * Copyright (C) 2009 Nokia Corporation
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/crc7.h>
27 #include <linux/spi/spi.h>
28 #include <linux/etherdevice.h>
30 #include "wl1271.h"
31 #include "wl1271_reg.h"
32 #include "wl1271_spi.h"
33 #include "wl1271_acx.h"
34 #include "wl12xx_80211.h"
35 #include "wl1271_cmd.h"
38 * send command to firmware
40 * @wl: wl struct
41 * @id: command id
42 * @buf: buffer containing the command, must work with dma
43 * @len: length of the buffer
45 int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
46 size_t res_len)
48 struct wl1271_cmd_header *cmd;
49 unsigned long timeout;
50 u32 intr;
51 int ret = 0;
52 u16 status;
54 cmd = buf;
55 cmd->id = cpu_to_le16(id);
56 cmd->status = 0;
58 WARN_ON(len % 4 != 0);
60 wl1271_spi_write(wl, wl->cmd_box_addr, buf, len, false);
62 wl1271_spi_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
64 timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
66 intr = wl1271_spi_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
67 while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
68 if (time_after(jiffies, timeout)) {
69 wl1271_error("command complete timeout");
70 ret = -ETIMEDOUT;
71 goto out;
74 msleep(1);
76 intr = wl1271_spi_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
79 /* read back the status code of the command */
80 if (res_len == 0)
81 res_len = sizeof(struct wl1271_cmd_header);
82 wl1271_spi_read(wl, wl->cmd_box_addr, cmd, res_len, false);
84 status = le16_to_cpu(cmd->status);
85 if (status != CMD_STATUS_SUCCESS) {
86 wl1271_error("command execute failure %d", status);
87 ret = -EIO;
90 wl1271_spi_write32(wl, ACX_REG_INTERRUPT_ACK,
91 WL1271_ACX_INTR_CMD_COMPLETE);
93 out:
94 return ret;
97 static int wl1271_cmd_cal_channel_tune(struct wl1271 *wl)
99 struct wl1271_cmd_cal_channel_tune *cmd;
100 int ret = 0;
102 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
103 if (!cmd)
104 return -ENOMEM;
106 cmd->test.id = TEST_CMD_CHANNEL_TUNE;
108 cmd->band = WL1271_CHANNEL_TUNE_BAND_2_4;
109 /* set up any channel, 7 is in the middle of the range */
110 cmd->channel = 7;
112 ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
113 if (ret < 0)
114 wl1271_warning("TEST_CMD_CHANNEL_TUNE failed");
116 kfree(cmd);
117 return ret;
120 static int wl1271_cmd_cal_update_ref_point(struct wl1271 *wl)
122 struct wl1271_cmd_cal_update_ref_point *cmd;
123 int ret = 0;
125 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
126 if (!cmd)
127 return -ENOMEM;
129 cmd->test.id = TEST_CMD_UPDATE_PD_REFERENCE_POINT;
131 /* FIXME: still waiting for the correct values */
132 cmd->ref_power = 0;
133 cmd->ref_detector = 0;
135 cmd->sub_band = WL1271_PD_REFERENCE_POINT_BAND_B_G;
137 ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
138 if (ret < 0)
139 wl1271_warning("TEST_CMD_UPDATE_PD_REFERENCE_POINT failed");
141 kfree(cmd);
142 return ret;
145 static int wl1271_cmd_cal_p2g(struct wl1271 *wl)
147 struct wl1271_cmd_cal_p2g *cmd;
148 int ret = 0;
150 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
151 if (!cmd)
152 return -ENOMEM;
154 cmd->test.id = TEST_CMD_P2G_CAL;
156 cmd->sub_band_mask = WL1271_CAL_P2G_BAND_B_G;
158 ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
159 if (ret < 0)
160 wl1271_warning("TEST_CMD_P2G_CAL failed");
162 kfree(cmd);
163 return ret;
166 static int wl1271_cmd_cal(struct wl1271 *wl)
169 * FIXME: we must make sure that we're not sleeping when calibration
170 * is done
172 int ret;
174 wl1271_notice("performing tx calibration");
176 ret = wl1271_cmd_cal_channel_tune(wl);
177 if (ret < 0)
178 return ret;
180 ret = wl1271_cmd_cal_update_ref_point(wl);
181 if (ret < 0)
182 return ret;
184 ret = wl1271_cmd_cal_p2g(wl);
185 if (ret < 0)
186 return ret;
188 return ret;
191 int wl1271_cmd_general_parms(struct wl1271 *wl)
193 struct wl1271_general_parms_cmd *gen_parms;
194 struct conf_general_parms *g = &wl->conf.init.genparam;
195 int ret;
197 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
198 if (!gen_parms)
199 return -ENOMEM;
201 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
203 gen_parms->ref_clk = g->ref_clk;
204 gen_parms->settling_time = g->settling_time;
205 gen_parms->clk_valid_on_wakeup = g->clk_valid_on_wakeup;
206 gen_parms->dc2dcmode = g->dc2dcmode;
207 gen_parms->single_dual_band = g->single_dual_band;
208 gen_parms->tx_bip_fem_autodetect = g->tx_bip_fem_autodetect;
209 gen_parms->tx_bip_fem_manufacturer = g->tx_bip_fem_manufacturer;
210 gen_parms->settings = g->settings;
212 gen_parms->sr_state = g->sr_state;
214 memcpy(gen_parms->srf1,
215 g->srf1,
216 CONF_MAX_SMART_REFLEX_PARAMS);
217 memcpy(gen_parms->srf2,
218 g->srf2,
219 CONF_MAX_SMART_REFLEX_PARAMS);
220 memcpy(gen_parms->srf3,
221 g->srf3,
222 CONF_MAX_SMART_REFLEX_PARAMS);
223 memcpy(gen_parms->sr_debug_table,
224 g->sr_debug_table,
225 CONF_MAX_SMART_REFLEX_PARAMS);
227 gen_parms->sr_sen_n_p = g->sr_sen_n_p;
228 gen_parms->sr_sen_n_p_gain = g->sr_sen_n_p_gain;
229 gen_parms->sr_sen_nrn = g->sr_sen_nrn;
230 gen_parms->sr_sen_prn = g->sr_sen_prn;
232 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), 0);
233 if (ret < 0)
234 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
236 kfree(gen_parms);
237 return ret;
240 int wl1271_cmd_radio_parms(struct wl1271 *wl)
242 struct wl1271_radio_parms_cmd *radio_parms;
243 struct conf_radio_parms *r = &wl->conf.init.radioparam;
244 int i, ret;
246 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
247 if (!radio_parms)
248 return -ENOMEM;
250 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
252 /* Static radio parameters */
253 radio_parms->rx_trace_loss = r->rx_trace_loss;
254 radio_parms->tx_trace_loss = r->tx_trace_loss;
255 memcpy(radio_parms->rx_rssi_and_proc_compens,
256 r->rx_rssi_and_proc_compens,
257 CONF_RSSI_AND_PROCESS_COMPENSATION_SIZE);
259 memcpy(radio_parms->rx_trace_loss_5, r->rx_trace_loss_5,
260 CONF_NUMBER_OF_SUB_BANDS_5);
261 memcpy(radio_parms->tx_trace_loss_5, r->tx_trace_loss_5,
262 CONF_NUMBER_OF_SUB_BANDS_5);
263 memcpy(radio_parms->rx_rssi_and_proc_compens_5,
264 r->rx_rssi_and_proc_compens_5,
265 CONF_RSSI_AND_PROCESS_COMPENSATION_SIZE);
267 /* Dynamic radio parameters */
268 radio_parms->tx_ref_pd_voltage = cpu_to_le16(r->tx_ref_pd_voltage);
269 radio_parms->tx_ref_power = r->tx_ref_power;
270 radio_parms->tx_offset_db = r->tx_offset_db;
272 memcpy(radio_parms->tx_rate_limits_normal, r->tx_rate_limits_normal,
273 CONF_NUMBER_OF_RATE_GROUPS);
274 memcpy(radio_parms->tx_rate_limits_degraded, r->tx_rate_limits_degraded,
275 CONF_NUMBER_OF_RATE_GROUPS);
276 memcpy(radio_parms->tx_rate_limits_extreme, r->tx_rate_limits_extreme,
277 CONF_NUMBER_OF_RATE_GROUPS);
279 memcpy(radio_parms->tx_channel_limits_11b, r->tx_channel_limits_11b,
280 CONF_NUMBER_OF_CHANNELS_2_4);
281 memcpy(radio_parms->tx_channel_limits_ofdm, r->tx_channel_limits_ofdm,
282 CONF_NUMBER_OF_CHANNELS_2_4);
283 memcpy(radio_parms->tx_pdv_rate_offsets, r->tx_pdv_rate_offsets,
284 CONF_NUMBER_OF_RATE_GROUPS);
285 memcpy(radio_parms->tx_ibias, r->tx_ibias, CONF_NUMBER_OF_RATE_GROUPS);
287 radio_parms->rx_fem_insertion_loss = r->rx_fem_insertion_loss;
288 radio_parms->degraded_low_to_normal_threshold =
289 r->degraded_low_to_normal_threshold;
290 radio_parms->degraded_normal_to_high_threshold =
291 r->degraded_normal_to_high_threshold;
294 for (i = 0; i < CONF_NUMBER_OF_SUB_BANDS_5; i++)
295 radio_parms->tx_ref_pd_voltage_5[i] =
296 cpu_to_le16(r->tx_ref_pd_voltage_5[i]);
297 memcpy(radio_parms->tx_ref_power_5, r->tx_ref_power_5,
298 CONF_NUMBER_OF_SUB_BANDS_5);
299 memcpy(radio_parms->tx_offset_db_5, r->tx_offset_db_5,
300 CONF_NUMBER_OF_SUB_BANDS_5);
301 memcpy(radio_parms->tx_rate_limits_normal_5,
302 r->tx_rate_limits_normal_5, CONF_NUMBER_OF_RATE_GROUPS);
303 memcpy(radio_parms->tx_rate_limits_degraded_5,
304 r->tx_rate_limits_degraded_5, CONF_NUMBER_OF_RATE_GROUPS);
305 memcpy(radio_parms->tx_rate_limits_extreme_5,
306 r->tx_rate_limits_extreme_5, CONF_NUMBER_OF_RATE_GROUPS);
307 memcpy(radio_parms->tx_channel_limits_ofdm_5,
308 r->tx_channel_limits_ofdm_5, CONF_NUMBER_OF_CHANNELS_5);
309 memcpy(radio_parms->tx_pdv_rate_offsets_5, r->tx_pdv_rate_offsets_5,
310 CONF_NUMBER_OF_RATE_GROUPS);
311 memcpy(radio_parms->tx_ibias_5, r->tx_ibias_5,
312 CONF_NUMBER_OF_RATE_GROUPS);
313 memcpy(radio_parms->rx_fem_insertion_loss_5,
314 r->rx_fem_insertion_loss_5, CONF_NUMBER_OF_SUB_BANDS_5);
315 radio_parms->degraded_low_to_normal_threshold_5 =
316 r->degraded_low_to_normal_threshold_5;
317 radio_parms->degraded_normal_to_high_threshold_5 =
318 r->degraded_normal_to_high_threshold_5;
320 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
321 radio_parms, sizeof(*radio_parms));
323 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
324 if (ret < 0)
325 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
327 kfree(radio_parms);
328 return ret;
331 int wl1271_cmd_join(struct wl1271 *wl)
333 static bool do_cal = true;
334 struct wl1271_cmd_join *join;
335 int ret, i;
336 u8 *bssid;
338 /* FIXME: remove when we get calibration from the factory */
339 if (do_cal) {
340 ret = wl1271_cmd_cal(wl);
341 if (ret < 0)
342 wl1271_warning("couldn't calibrate");
343 else
344 do_cal = false;
347 join = kzalloc(sizeof(*join), GFP_KERNEL);
348 if (!join) {
349 ret = -ENOMEM;
350 goto out;
353 wl1271_debug(DEBUG_CMD, "cmd join");
355 /* Reverse order BSSID */
356 bssid = (u8 *) &join->bssid_lsb;
357 for (i = 0; i < ETH_ALEN; i++)
358 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
360 join->rx_config_options = cpu_to_le32(wl->rx_config);
361 join->rx_filter_options = cpu_to_le32(wl->rx_filter);
362 join->bss_type = wl->bss_type;
365 * FIXME: disable temporarily all filters because after commit
366 * 9cef8737 "mac80211: fix managed mode BSSID handling" broke
367 * association. The filter logic needs to be implemented properly
368 * and once that is done, this hack can be removed.
370 join->rx_config_options = cpu_to_le32(0);
371 join->rx_filter_options = cpu_to_le32(WL1271_DEFAULT_RX_FILTER);
373 if (wl->band == IEEE80211_BAND_2GHZ)
374 join->basic_rate_set = cpu_to_le32(CONF_HW_BIT_RATE_1MBPS |
375 CONF_HW_BIT_RATE_2MBPS |
376 CONF_HW_BIT_RATE_5_5MBPS |
377 CONF_HW_BIT_RATE_11MBPS);
378 else {
379 join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
380 join->basic_rate_set = cpu_to_le32(CONF_HW_BIT_RATE_6MBPS |
381 CONF_HW_BIT_RATE_12MBPS |
382 CONF_HW_BIT_RATE_24MBPS);
385 join->beacon_interval = cpu_to_le16(WL1271_DEFAULT_BEACON_INT);
386 join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
388 join->channel = wl->channel;
389 join->ssid_len = wl->ssid_len;
390 memcpy(join->ssid, wl->ssid, wl->ssid_len);
391 join->ctrl = WL1271_JOIN_CMD_CTRL_TX_FLUSH;
393 /* increment the session counter */
394 wl->session_counter++;
395 if (wl->session_counter >= SESSION_COUNTER_MAX)
396 wl->session_counter = 0;
398 join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
400 /* reset TX security counters */
401 wl->tx_security_last_seq = 0;
402 wl->tx_security_seq_16 = 0;
403 wl->tx_security_seq_32 = 0;
405 ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
406 if (ret < 0) {
407 wl1271_error("failed to initiate cmd join");
408 goto out_free;
412 * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
413 * simplify locking we just sleep instead, for now
415 msleep(10);
417 out_free:
418 kfree(join);
420 out:
421 return ret;
425 * send test command to firmware
427 * @wl: wl struct
428 * @buf: buffer containing the command, with all headers, must work with dma
429 * @len: length of the buffer
430 * @answer: is answer needed
432 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
434 int ret;
435 size_t res_len = 0;
437 wl1271_debug(DEBUG_CMD, "cmd test");
439 if (answer)
440 res_len = buf_len;
442 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
444 if (ret < 0) {
445 wl1271_warning("TEST command failed");
446 return ret;
449 return ret;
453 * read acx from firmware
455 * @wl: wl struct
456 * @id: acx id
457 * @buf: buffer for the response, including all headers, must work with dma
458 * @len: lenght of buf
460 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
462 struct acx_header *acx = buf;
463 int ret;
465 wl1271_debug(DEBUG_CMD, "cmd interrogate");
467 acx->id = cpu_to_le16(id);
469 /* payload length, does not include any headers */
470 acx->len = cpu_to_le16(len - sizeof(*acx));
472 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
473 if (ret < 0)
474 wl1271_error("INTERROGATE command failed");
476 return ret;
480 * write acx value to firmware
482 * @wl: wl struct
483 * @id: acx id
484 * @buf: buffer containing acx, including all headers, must work with dma
485 * @len: length of buf
487 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
489 struct acx_header *acx = buf;
490 int ret;
492 wl1271_debug(DEBUG_CMD, "cmd configure");
494 acx->id = cpu_to_le16(id);
496 /* payload length, does not include any headers */
497 acx->len = cpu_to_le16(len - sizeof(*acx));
499 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
500 if (ret < 0) {
501 wl1271_warning("CONFIGURE command NOK");
502 return ret;
505 return 0;
508 int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
510 struct cmd_enabledisable_path *cmd;
511 int ret;
512 u16 cmd_rx, cmd_tx;
514 wl1271_debug(DEBUG_CMD, "cmd data path");
516 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
517 if (!cmd) {
518 ret = -ENOMEM;
519 goto out;
522 /* the channel here is only used for calibration, so hardcoded to 1 */
523 cmd->channel = 1;
525 if (enable) {
526 cmd_rx = CMD_ENABLE_RX;
527 cmd_tx = CMD_ENABLE_TX;
528 } else {
529 cmd_rx = CMD_DISABLE_RX;
530 cmd_tx = CMD_DISABLE_TX;
533 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
534 if (ret < 0) {
535 wl1271_error("rx %s cmd for channel %d failed",
536 enable ? "start" : "stop", cmd->channel);
537 goto out;
540 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
541 enable ? "start" : "stop", cmd->channel);
543 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
544 if (ret < 0) {
545 wl1271_error("tx %s cmd for channel %d failed",
546 enable ? "start" : "stop", cmd->channel);
547 return ret;
550 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
551 enable ? "start" : "stop", cmd->channel);
553 out:
554 kfree(cmd);
555 return ret;
558 int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
560 struct wl1271_cmd_ps_params *ps_params = NULL;
561 int ret = 0;
563 /* FIXME: this should be in ps.c */
564 ret = wl1271_acx_wake_up_conditions(wl);
565 if (ret < 0) {
566 wl1271_error("couldn't set wake up conditions");
567 goto out;
570 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
572 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
573 if (!ps_params) {
574 ret = -ENOMEM;
575 goto out;
578 ps_params->ps_mode = ps_mode;
579 ps_params->send_null_data = 1;
580 ps_params->retries = 5;
581 ps_params->hang_over_period = 128;
582 ps_params->null_data_rate = cpu_to_le32(1); /* 1 Mbps */
584 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
585 sizeof(*ps_params), 0);
586 if (ret < 0) {
587 wl1271_error("cmd set_ps_mode failed");
588 goto out;
591 out:
592 kfree(ps_params);
593 return ret;
596 int wl1271_cmd_read_memory(struct wl1271 *wl, u32 addr, void *answer,
597 size_t len)
599 struct cmd_read_write_memory *cmd;
600 int ret = 0;
602 wl1271_debug(DEBUG_CMD, "cmd read memory");
604 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
605 if (!cmd) {
606 ret = -ENOMEM;
607 goto out;
610 WARN_ON(len > MAX_READ_SIZE);
611 len = min_t(size_t, len, MAX_READ_SIZE);
613 cmd->addr = cpu_to_le32(addr);
614 cmd->size = cpu_to_le32(len);
616 ret = wl1271_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd),
617 sizeof(*cmd));
618 if (ret < 0) {
619 wl1271_error("read memory command failed: %d", ret);
620 goto out;
623 /* the read command got in */
624 memcpy(answer, cmd->value, len);
626 out:
627 kfree(cmd);
628 return ret;
631 int wl1271_cmd_scan(struct wl1271 *wl, u8 *ssid, size_t len,
632 u8 active_scan, u8 high_prio, u8 band,
633 u8 probe_requests)
636 struct wl1271_cmd_trigger_scan_to *trigger = NULL;
637 struct wl1271_cmd_scan *params = NULL;
638 struct ieee80211_channel *channels;
639 int i, j, n_ch, ret;
640 u16 scan_options = 0;
641 u8 ieee_band;
643 if (band == WL1271_SCAN_BAND_2_4_GHZ)
644 ieee_band = IEEE80211_BAND_2GHZ;
645 else if (band == WL1271_SCAN_BAND_DUAL && wl1271_11a_enabled())
646 ieee_band = IEEE80211_BAND_2GHZ;
647 else if (band == WL1271_SCAN_BAND_5_GHZ && wl1271_11a_enabled())
648 ieee_band = IEEE80211_BAND_5GHZ;
649 else
650 return -EINVAL;
652 if (wl->hw->wiphy->bands[ieee_band]->channels == NULL)
653 return -EINVAL;
655 channels = wl->hw->wiphy->bands[ieee_band]->channels;
656 n_ch = wl->hw->wiphy->bands[ieee_band]->n_channels;
658 if (test_bit(WL1271_FLAG_SCANNING, &wl->flags))
659 return -EINVAL;
661 params = kzalloc(sizeof(*params), GFP_KERNEL);
662 if (!params)
663 return -ENOMEM;
665 params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
666 params->params.rx_filter_options =
667 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
669 if (!active_scan)
670 scan_options |= WL1271_SCAN_OPT_PASSIVE;
671 if (high_prio)
672 scan_options |= WL1271_SCAN_OPT_PRIORITY_HIGH;
673 params->params.scan_options = cpu_to_le16(scan_options);
675 params->params.num_probe_requests = probe_requests;
676 /* Let the fw autodetect suitable tx_rate for probes */
677 params->params.tx_rate = 0;
678 params->params.tid_trigger = 0;
679 params->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
681 if (band == WL1271_SCAN_BAND_DUAL)
682 params->params.band = WL1271_SCAN_BAND_2_4_GHZ;
683 else
684 params->params.band = band;
686 for (i = 0, j = 0; i < n_ch && i < WL1271_SCAN_MAX_CHANNELS; i++) {
687 if (!(channels[i].flags & IEEE80211_CHAN_DISABLED)) {
688 params->channels[j].min_duration =
689 cpu_to_le32(WL1271_SCAN_CHAN_MIN_DURATION);
690 params->channels[j].max_duration =
691 cpu_to_le32(WL1271_SCAN_CHAN_MAX_DURATION);
692 memset(&params->channels[j].bssid_lsb, 0xff, 4);
693 memset(&params->channels[j].bssid_msb, 0xff, 2);
694 params->channels[j].early_termination = 0;
695 params->channels[j].tx_power_att =
696 WL1271_SCAN_CURRENT_TX_PWR;
697 params->channels[j].channel = channels[i].hw_value;
698 j++;
702 params->params.num_channels = j;
704 if (len && ssid) {
705 params->params.ssid_len = len;
706 memcpy(params->params.ssid, ssid, len);
709 ret = wl1271_cmd_build_probe_req(wl, ssid, len, ieee_band);
710 if (ret < 0) {
711 wl1271_error("PROBE request template failed");
712 goto out;
715 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
716 if (!trigger) {
717 ret = -ENOMEM;
718 goto out;
721 /* disable the timeout */
722 trigger->timeout = 0;
724 ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
725 sizeof(*trigger), 0);
726 if (ret < 0) {
727 wl1271_error("trigger scan to failed for hw scan");
728 goto out;
731 wl1271_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
733 set_bit(WL1271_FLAG_SCANNING, &wl->flags);
734 if (wl1271_11a_enabled()) {
735 wl->scan.state = band;
736 if (band == WL1271_SCAN_BAND_DUAL) {
737 wl->scan.active = active_scan;
738 wl->scan.high_prio = high_prio;
739 wl->scan.probe_requests = probe_requests;
740 if (len && ssid) {
741 wl->scan.ssid_len = len;
742 memcpy(wl->scan.ssid, ssid, len);
743 } else
744 wl->scan.ssid_len = 0;
748 ret = wl1271_cmd_send(wl, CMD_SCAN, params, sizeof(*params), 0);
749 if (ret < 0) {
750 wl1271_error("SCAN failed");
751 clear_bit(WL1271_FLAG_SCANNING, &wl->flags);
752 goto out;
755 out:
756 kfree(params);
757 return ret;
760 int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
761 void *buf, size_t buf_len)
763 struct wl1271_cmd_template_set *cmd;
764 int ret = 0;
766 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
768 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
769 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
771 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
772 if (!cmd) {
773 ret = -ENOMEM;
774 goto out;
777 cmd->len = cpu_to_le16(buf_len);
778 cmd->template_type = template_id;
779 cmd->enabled_rates = cpu_to_le32(wl->conf.tx.rc_conf.enabled_rates);
780 cmd->short_retry_limit = wl->conf.tx.rc_conf.short_retry_limit;
781 cmd->long_retry_limit = wl->conf.tx.rc_conf.long_retry_limit;
783 if (buf)
784 memcpy(cmd->template_data, buf, buf_len);
786 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
787 if (ret < 0) {
788 wl1271_warning("cmd set_template failed: %d", ret);
789 goto out_free;
792 out_free:
793 kfree(cmd);
795 out:
796 return ret;
799 static int wl1271_build_basic_rates(u8 *rates, u8 band)
801 u8 index = 0;
803 if (band == IEEE80211_BAND_2GHZ) {
804 rates[index++] =
805 IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
806 rates[index++] =
807 IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
808 rates[index++] =
809 IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
810 rates[index++] =
811 IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
812 } else if (band == IEEE80211_BAND_5GHZ) {
813 rates[index++] =
814 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_6MB;
815 rates[index++] =
816 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_12MB;
817 rates[index++] =
818 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
819 } else {
820 wl1271_error("build_basic_rates invalid band: %d", band);
823 return index;
826 static int wl1271_build_extended_rates(u8 *rates, u8 band)
828 u8 index = 0;
830 if (band == IEEE80211_BAND_2GHZ) {
831 rates[index++] = IEEE80211_OFDM_RATE_6MB;
832 rates[index++] = IEEE80211_OFDM_RATE_9MB;
833 rates[index++] = IEEE80211_OFDM_RATE_12MB;
834 rates[index++] = IEEE80211_OFDM_RATE_18MB;
835 rates[index++] = IEEE80211_OFDM_RATE_24MB;
836 rates[index++] = IEEE80211_OFDM_RATE_36MB;
837 rates[index++] = IEEE80211_OFDM_RATE_48MB;
838 rates[index++] = IEEE80211_OFDM_RATE_54MB;
839 } else if (band == IEEE80211_BAND_5GHZ) {
840 rates[index++] =
841 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_9MB;
842 rates[index++] =
843 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_18MB;
844 rates[index++] =
845 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
846 rates[index++] =
847 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_36MB;
848 rates[index++] =
849 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_48MB;
850 rates[index++] =
851 IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_54MB;
852 } else {
853 wl1271_error("build_basic_rates invalid band: %d", band);
856 return index;
859 int wl1271_cmd_build_null_data(struct wl1271 *wl)
861 struct wl12xx_null_data_template template;
863 if (!is_zero_ether_addr(wl->bssid)) {
864 memcpy(template.header.da, wl->bssid, ETH_ALEN);
865 memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
866 } else {
867 memset(template.header.da, 0xff, ETH_ALEN);
868 memset(template.header.bssid, 0xff, ETH_ALEN);
871 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
872 template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
873 IEEE80211_STYPE_NULLFUNC |
874 IEEE80211_FCTL_TODS);
876 return wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, &template,
877 sizeof(template));
881 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
883 struct wl12xx_ps_poll_template template;
885 memcpy(template.bssid, wl->bssid, ETH_ALEN);
886 memcpy(template.ta, wl->mac_addr, ETH_ALEN);
888 /* aid in PS-Poll has its two MSBs each set to 1 */
889 template.aid = cpu_to_le16(1 << 15 | 1 << 14 | aid);
891 template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
893 return wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, &template,
894 sizeof(template));
898 int wl1271_cmd_build_probe_req(struct wl1271 *wl, u8 *ssid, size_t ssid_len,
899 u8 band)
901 struct wl12xx_probe_req_template template;
902 struct wl12xx_ie_rates *rates;
903 char *ptr;
904 u16 size;
905 int ret;
907 ptr = (char *)&template;
908 size = sizeof(struct ieee80211_header);
910 memset(template.header.da, 0xff, ETH_ALEN);
911 memset(template.header.bssid, 0xff, ETH_ALEN);
912 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
913 template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
915 /* IEs */
916 /* SSID */
917 template.ssid.header.id = WLAN_EID_SSID;
918 template.ssid.header.len = ssid_len;
919 if (ssid_len && ssid)
920 memcpy(template.ssid.ssid, ssid, ssid_len);
921 size += sizeof(struct wl12xx_ie_header) + ssid_len;
922 ptr += size;
924 /* Basic Rates */
925 rates = (struct wl12xx_ie_rates *)ptr;
926 rates->header.id = WLAN_EID_SUPP_RATES;
927 rates->header.len = wl1271_build_basic_rates(rates->rates, band);
928 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
929 ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
931 /* Extended rates */
932 rates = (struct wl12xx_ie_rates *)ptr;
933 rates->header.id = WLAN_EID_EXT_SUPP_RATES;
934 rates->header.len = wl1271_build_extended_rates(rates->rates, band);
935 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
937 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
939 if (band == IEEE80211_BAND_2GHZ)
940 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
941 &template, size);
942 else
943 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
944 &template, size);
945 return ret;
948 int wl1271_cmd_set_default_wep_key(struct wl1271 *wl, u8 id)
950 struct wl1271_cmd_set_keys *cmd;
951 int ret = 0;
953 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
955 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
956 if (!cmd) {
957 ret = -ENOMEM;
958 goto out;
961 cmd->id = id;
962 cmd->key_action = cpu_to_le16(KEY_SET_ID);
963 cmd->key_type = KEY_WEP;
965 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
966 if (ret < 0) {
967 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
968 goto out;
971 out:
972 kfree(cmd);
974 return ret;
977 int wl1271_cmd_set_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
978 u8 key_size, const u8 *key, const u8 *addr,
979 u32 tx_seq_32, u16 tx_seq_16)
981 struct wl1271_cmd_set_keys *cmd;
982 int ret = 0;
984 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
985 if (!cmd) {
986 ret = -ENOMEM;
987 goto out;
990 if (key_type != KEY_WEP)
991 memcpy(cmd->addr, addr, ETH_ALEN);
993 cmd->key_action = cpu_to_le16(action);
994 cmd->key_size = key_size;
995 cmd->key_type = key_type;
997 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
998 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1000 /* we have only one SSID profile */
1001 cmd->ssid_profile = 0;
1003 cmd->id = id;
1005 if (key_type == KEY_TKIP) {
1007 * We get the key in the following form:
1008 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1009 * but the target is expecting:
1010 * TKIP - RX MIC - TX MIC
1012 memcpy(cmd->key, key, 16);
1013 memcpy(cmd->key + 16, key + 24, 8);
1014 memcpy(cmd->key + 24, key + 16, 8);
1016 } else {
1017 memcpy(cmd->key, key, key_size);
1020 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1022 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1023 if (ret < 0) {
1024 wl1271_warning("could not set keys");
1025 goto out;
1028 out:
1029 kfree(cmd);
1031 return ret;
1034 int wl1271_cmd_disconnect(struct wl1271 *wl)
1036 struct wl1271_cmd_disconnect *cmd;
1037 int ret = 0;
1039 wl1271_debug(DEBUG_CMD, "cmd disconnect");
1041 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1042 if (!cmd) {
1043 ret = -ENOMEM;
1044 goto out;
1047 cmd->rx_config_options = cpu_to_le32(wl->rx_config);
1048 cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
1049 /* disconnect reason is not used in immediate disconnections */
1050 cmd->type = DISCONNECT_IMMEDIATE;
1052 ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
1053 if (ret < 0) {
1054 wl1271_error("failed to send disconnect command");
1055 goto out_free;
1058 out_free:
1059 kfree(cmd);
1061 out:
1062 return ret;