2 * This file is part of wl1271
4 * Copyright (C) 2009-2010 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
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/spi/spi.h>
27 #include <linux/etherdevice.h>
28 #include <linux/ieee80211.h>
29 #include <linux/slab.h>
35 #include "wl12xx_80211.h"
40 #define WL1271_CMD_FAST_POLL_COUNT 50
43 * send command to firmware
47 * @buf: buffer containing the command, must work with dma
48 * @len: length of the buffer
50 int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
53 struct wl1271_cmd_header *cmd;
54 unsigned long timeout;
61 cmd->id = cpu_to_le16(id);
64 WARN_ON(len % 4 != 0);
65 WARN_ON(test_bit(WL1271_FLAG_IN_ELP, &wl->flags));
67 wl1271_write(wl, wl->cmd_box_addr, buf, len, false);
69 wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
71 timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
73 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
74 while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
75 if (time_after(jiffies, timeout)) {
76 wl1271_error("command complete timeout");
82 if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
87 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
90 /* read back the status code of the command */
92 res_len = sizeof(struct wl1271_cmd_header);
93 wl1271_read(wl, wl->cmd_box_addr, cmd, res_len, false);
95 status = le16_to_cpu(cmd->status);
96 if (status != CMD_STATUS_SUCCESS) {
97 wl1271_error("command execute failure %d", status);
102 wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
103 WL1271_ACX_INTR_CMD_COMPLETE);
108 wl12xx_queue_recovery_work(wl);
112 int wl1271_cmd_general_parms(struct wl1271 *wl)
114 struct wl1271_general_parms_cmd *gen_parms;
115 struct wl1271_ini_general_params *gp =
116 &((struct wl1271_nvs_file *)wl->nvs)->general_params;
123 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
127 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
129 memcpy(&gen_parms->general_params, gp, sizeof(*gp));
131 if (gp->tx_bip_fem_auto_detect)
134 /* Override the REF CLK from the NVS with the one from platform data */
135 gen_parms->general_params.ref_clock = wl->ref_clock;
137 /* LPD mode enable (bits 6-7) in WL1271 AP mode only */
138 if (wl->quirks & WL12XX_QUIRK_LPD_MODE)
139 gen_parms->general_params.general_settings |=
140 GENERAL_SETTINGS_DRPW_LPD;
142 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
144 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
148 gp->tx_bip_fem_manufacturer =
149 gen_parms->general_params.tx_bip_fem_manufacturer;
151 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
152 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
159 int wl128x_cmd_general_parms(struct wl1271 *wl)
161 struct wl128x_general_parms_cmd *gen_parms;
162 struct wl128x_ini_general_params *gp =
163 &((struct wl128x_nvs_file *)wl->nvs)->general_params;
170 gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
174 gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
176 memcpy(&gen_parms->general_params, gp, sizeof(*gp));
178 if (gp->tx_bip_fem_auto_detect)
181 /* Replace REF and TCXO CLKs with the ones from platform data */
182 gen_parms->general_params.ref_clock = wl->ref_clock;
183 gen_parms->general_params.tcxo_ref_clock = wl->tcxo_clock;
185 ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
187 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
191 gp->tx_bip_fem_manufacturer =
192 gen_parms->general_params.tx_bip_fem_manufacturer;
194 wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
195 answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
202 int wl1271_cmd_radio_parms(struct wl1271 *wl)
204 struct wl1271_nvs_file *nvs = (struct wl1271_nvs_file *)wl->nvs;
205 struct wl1271_radio_parms_cmd *radio_parms;
206 struct wl1271_ini_general_params *gp = &nvs->general_params;
212 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
216 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
218 /* 2.4GHz parameters */
219 memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
220 sizeof(struct wl1271_ini_band_params_2));
221 memcpy(&radio_parms->dyn_params_2,
222 &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
223 sizeof(struct wl1271_ini_fem_params_2));
225 /* 5GHz parameters */
226 memcpy(&radio_parms->static_params_5,
227 &nvs->stat_radio_params_5,
228 sizeof(struct wl1271_ini_band_params_5));
229 memcpy(&radio_parms->dyn_params_5,
230 &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
231 sizeof(struct wl1271_ini_fem_params_5));
233 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
234 radio_parms, sizeof(*radio_parms));
236 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
238 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
244 int wl128x_cmd_radio_parms(struct wl1271 *wl)
246 struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
247 struct wl128x_radio_parms_cmd *radio_parms;
248 struct wl128x_ini_general_params *gp = &nvs->general_params;
254 radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
258 radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
260 /* 2.4GHz parameters */
261 memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
262 sizeof(struct wl128x_ini_band_params_2));
263 memcpy(&radio_parms->dyn_params_2,
264 &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
265 sizeof(struct wl128x_ini_fem_params_2));
267 /* 5GHz parameters */
268 memcpy(&radio_parms->static_params_5,
269 &nvs->stat_radio_params_5,
270 sizeof(struct wl128x_ini_band_params_5));
271 memcpy(&radio_parms->dyn_params_5,
272 &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
273 sizeof(struct wl128x_ini_fem_params_5));
275 radio_parms->fem_vendor_and_options = nvs->fem_vendor_and_options;
277 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
278 radio_parms, sizeof(*radio_parms));
280 ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
282 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
288 int wl1271_cmd_ext_radio_parms(struct wl1271 *wl)
290 struct wl1271_ext_radio_parms_cmd *ext_radio_parms;
291 struct conf_rf_settings *rf = &wl->conf.rf;
297 ext_radio_parms = kzalloc(sizeof(*ext_radio_parms), GFP_KERNEL);
298 if (!ext_radio_parms)
301 ext_radio_parms->test.id = TEST_CMD_INI_FILE_RF_EXTENDED_PARAM;
303 memcpy(ext_radio_parms->tx_per_channel_power_compensation_2,
304 rf->tx_per_channel_power_compensation_2,
305 CONF_TX_PWR_COMPENSATION_LEN_2);
306 memcpy(ext_radio_parms->tx_per_channel_power_compensation_5,
307 rf->tx_per_channel_power_compensation_5,
308 CONF_TX_PWR_COMPENSATION_LEN_5);
310 wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_EXT_RADIO_PARAM: ",
311 ext_radio_parms, sizeof(*ext_radio_parms));
313 ret = wl1271_cmd_test(wl, ext_radio_parms, sizeof(*ext_radio_parms), 0);
315 wl1271_warning("TEST_CMD_INI_FILE_RF_EXTENDED_PARAM failed");
317 kfree(ext_radio_parms);
322 * Poll the mailbox event field until any of the bits in the mask is set or a
323 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
325 static int wl1271_cmd_wait_for_event_or_timeout(struct wl1271 *wl, u32 mask)
327 u32 events_vector, event;
328 unsigned long timeout;
330 timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
333 if (time_after(jiffies, timeout)) {
334 wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
341 /* read from both event fields */
342 wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
343 sizeof(events_vector), false);
344 event = events_vector & mask;
345 wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
346 sizeof(events_vector), false);
347 event |= events_vector & mask;
353 static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
357 ret = wl1271_cmd_wait_for_event_or_timeout(wl, mask);
359 wl12xx_queue_recovery_work(wl);
366 int wl12xx_cmd_role_enable(struct wl1271 *wl, u8 role_type, u8 *role_id)
368 struct wl12xx_cmd_role_enable *cmd;
371 wl1271_debug(DEBUG_CMD, "cmd role enable");
373 if (WARN_ON(*role_id != WL12XX_INVALID_ROLE_ID))
376 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
383 cmd->role_id = find_first_zero_bit(wl->roles_map, WL12XX_MAX_ROLES);
384 if (cmd->role_id >= WL12XX_MAX_ROLES) {
389 memcpy(cmd->mac_address, wl->mac_addr, ETH_ALEN);
390 cmd->role_type = role_type;
392 ret = wl1271_cmd_send(wl, CMD_ROLE_ENABLE, cmd, sizeof(*cmd), 0);
394 wl1271_error("failed to initiate cmd role enable");
398 __set_bit(cmd->role_id, wl->roles_map);
399 *role_id = cmd->role_id;
408 int wl12xx_cmd_role_disable(struct wl1271 *wl, u8 *role_id)
410 struct wl12xx_cmd_role_disable *cmd;
413 wl1271_debug(DEBUG_CMD, "cmd role disable");
415 if (WARN_ON(*role_id == WL12XX_INVALID_ROLE_ID))
418 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
423 cmd->role_id = *role_id;
425 ret = wl1271_cmd_send(wl, CMD_ROLE_DISABLE, cmd, sizeof(*cmd), 0);
427 wl1271_error("failed to initiate cmd role disable");
431 __clear_bit(*role_id, wl->roles_map);
432 *role_id = WL12XX_INVALID_ROLE_ID;
441 static int wl12xx_allocate_link(struct wl1271 *wl, u8 *hlid)
443 u8 link = find_first_zero_bit(wl->links_map, WL12XX_MAX_LINKS);
444 if (link >= WL12XX_MAX_LINKS)
447 __set_bit(link, wl->links_map);
452 static void wl12xx_free_link(struct wl1271 *wl, u8 *hlid)
454 if (*hlid == WL12XX_INVALID_LINK_ID)
457 __clear_bit(*hlid, wl->links_map);
458 *hlid = WL12XX_INVALID_LINK_ID;
461 static int wl12xx_get_new_session_id(struct wl1271 *wl)
463 if (wl->session_counter >= SESSION_COUNTER_MAX)
464 wl->session_counter = 0;
466 wl->session_counter++;
468 return wl->session_counter;
471 int wl12xx_cmd_role_start_dev(struct wl1271 *wl)
473 struct wl12xx_cmd_role_start *cmd;
476 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
482 wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wl->dev_role_id);
484 cmd->role_id = wl->dev_role_id;
485 if (wl->band == IEEE80211_BAND_5GHZ)
486 cmd->band = WL12XX_BAND_5GHZ;
487 cmd->channel = wl->channel;
489 if (wl->dev_hlid == WL12XX_INVALID_LINK_ID) {
490 ret = wl12xx_allocate_link(wl, &wl->dev_hlid);
494 cmd->device.hlid = wl->dev_hlid;
495 cmd->device.session = wl->session_counter;
497 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d",
498 cmd->role_id, cmd->device.hlid, cmd->device.session);
500 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
502 wl1271_error("failed to initiate cmd role enable");
509 /* clear links on error */
510 __clear_bit(wl->dev_hlid, wl->links_map);
511 wl->dev_hlid = WL12XX_INVALID_LINK_ID;
521 int wl12xx_cmd_role_stop_dev(struct wl1271 *wl)
523 struct wl12xx_cmd_role_stop *cmd;
526 if (WARN_ON(wl->dev_hlid == WL12XX_INVALID_LINK_ID))
529 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
535 wl1271_debug(DEBUG_CMD, "cmd role stop dev");
537 cmd->role_id = wl->dev_role_id;
538 cmd->disc_type = DISCONNECT_IMMEDIATE;
539 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
541 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
543 wl1271_error("failed to initiate cmd role stop");
547 ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
549 wl1271_error("cmd role stop dev event completion error");
553 wl12xx_free_link(wl, &wl->dev_hlid);
562 int wl12xx_cmd_role_start_sta(struct wl1271 *wl)
564 struct wl12xx_cmd_role_start *cmd;
567 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
573 wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wl->role_id);
575 cmd->role_id = wl->role_id;
576 if (wl->band == IEEE80211_BAND_5GHZ)
577 cmd->band = WL12XX_BAND_5GHZ;
578 cmd->channel = wl->channel;
579 cmd->sta.basic_rate_set = cpu_to_le32(wl->basic_rate_set);
580 cmd->sta.beacon_interval = cpu_to_le16(wl->beacon_int);
581 cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY;
582 cmd->sta.ssid_len = wl->ssid_len;
583 memcpy(cmd->sta.ssid, wl->ssid, wl->ssid_len);
584 memcpy(cmd->sta.bssid, wl->bssid, ETH_ALEN);
585 cmd->sta.local_rates = cpu_to_le32(wl->rate_set);
587 if (wl->sta_hlid == WL12XX_INVALID_LINK_ID) {
588 ret = wl12xx_allocate_link(wl, &wl->sta_hlid);
592 cmd->sta.hlid = wl->sta_hlid;
593 cmd->sta.session = wl12xx_get_new_session_id(wl);
594 cmd->sta.remote_rates = cpu_to_le32(wl->rate_set);
596 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
597 "basic_rate_set: 0x%x, remote_rates: 0x%x",
598 wl->role_id, cmd->sta.hlid, cmd->sta.session,
599 wl->basic_rate_set, wl->rate_set);
601 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
603 wl1271_error("failed to initiate cmd role start sta");
610 /* clear links on error. */
611 wl12xx_free_link(wl, &wl->sta_hlid);
620 /* use this function to stop ibss as well */
621 int wl12xx_cmd_role_stop_sta(struct wl1271 *wl)
623 struct wl12xx_cmd_role_stop *cmd;
626 if (WARN_ON(wl->sta_hlid == WL12XX_INVALID_LINK_ID))
629 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
635 wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wl->role_id);
637 cmd->role_id = wl->role_id;
638 cmd->disc_type = DISCONNECT_IMMEDIATE;
639 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
641 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
643 wl1271_error("failed to initiate cmd role stop sta");
647 wl12xx_free_link(wl, &wl->sta_hlid);
656 int wl12xx_cmd_role_start_ap(struct wl1271 *wl)
658 struct wl12xx_cmd_role_start *cmd;
659 struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
662 wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wl->role_id);
664 /* trying to use hidden SSID with an old hostapd version */
665 if (wl->ssid_len == 0 && !bss_conf->hidden_ssid) {
666 wl1271_error("got a null SSID from beacon/bss");
671 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
677 ret = wl12xx_allocate_link(wl, &wl->ap_global_hlid);
681 ret = wl12xx_allocate_link(wl, &wl->ap_bcast_hlid);
683 goto out_free_global;
685 cmd->role_id = wl->role_id;
686 cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
687 cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
688 cmd->ap.global_hlid = wl->ap_global_hlid;
689 cmd->ap.broadcast_hlid = wl->ap_bcast_hlid;
690 cmd->ap.basic_rate_set = cpu_to_le32(wl->basic_rate_set);
691 cmd->ap.beacon_interval = cpu_to_le16(wl->beacon_int);
692 cmd->ap.dtim_interval = bss_conf->dtim_period;
693 cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
694 cmd->channel = wl->channel;
696 if (!bss_conf->hidden_ssid) {
697 /* take the SSID from the beacon for backward compatibility */
698 cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
699 cmd->ap.ssid_len = wl->ssid_len;
700 memcpy(cmd->ap.ssid, wl->ssid, wl->ssid_len);
702 cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
703 cmd->ap.ssid_len = bss_conf->ssid_len;
704 memcpy(cmd->ap.ssid, bss_conf->ssid, bss_conf->ssid_len);
707 cmd->ap.local_rates = cpu_to_le32(0xffffffff);
710 case IEEE80211_BAND_2GHZ:
711 cmd->band = RADIO_BAND_2_4GHZ;
713 case IEEE80211_BAND_5GHZ:
714 cmd->band = RADIO_BAND_5GHZ;
717 wl1271_warning("ap start - unknown band: %d", (int)wl->band);
718 cmd->band = RADIO_BAND_2_4GHZ;
722 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
724 wl1271_error("failed to initiate cmd role start ap");
731 wl12xx_free_link(wl, &wl->ap_bcast_hlid);
734 wl12xx_free_link(wl, &wl->ap_global_hlid);
743 int wl12xx_cmd_role_stop_ap(struct wl1271 *wl)
745 struct wl12xx_cmd_role_stop *cmd;
748 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
754 wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wl->role_id);
756 cmd->role_id = wl->role_id;
758 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
760 wl1271_error("failed to initiate cmd role stop ap");
764 wl12xx_free_link(wl, &wl->ap_bcast_hlid);
765 wl12xx_free_link(wl, &wl->ap_global_hlid);
774 int wl12xx_cmd_role_start_ibss(struct wl1271 *wl)
776 struct wl12xx_cmd_role_start *cmd;
777 struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
780 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
786 wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wl->role_id);
788 cmd->role_id = wl->role_id;
789 if (wl->band == IEEE80211_BAND_5GHZ)
790 cmd->band = WL12XX_BAND_5GHZ;
791 cmd->channel = wl->channel;
792 cmd->ibss.basic_rate_set = cpu_to_le32(wl->basic_rate_set);
793 cmd->ibss.beacon_interval = cpu_to_le16(wl->beacon_int);
794 cmd->ibss.dtim_interval = bss_conf->dtim_period;
795 cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
796 cmd->ibss.ssid_len = wl->ssid_len;
797 memcpy(cmd->ibss.ssid, wl->ssid, wl->ssid_len);
798 memcpy(cmd->ibss.bssid, wl->bssid, ETH_ALEN);
799 cmd->sta.local_rates = cpu_to_le32(wl->rate_set);
801 if (wl->sta_hlid == WL12XX_INVALID_LINK_ID) {
802 ret = wl12xx_allocate_link(wl, &wl->sta_hlid);
806 cmd->ibss.hlid = wl->sta_hlid;
807 cmd->ibss.remote_rates = cpu_to_le32(wl->rate_set);
809 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
810 "basic_rate_set: 0x%x, remote_rates: 0x%x",
811 wl->role_id, cmd->sta.hlid, cmd->sta.session,
812 wl->basic_rate_set, wl->rate_set);
814 wl1271_debug(DEBUG_CMD, "wl->bssid = %pM", wl->bssid);
816 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
818 wl1271_error("failed to initiate cmd role enable");
825 /* clear links on error. */
826 wl12xx_free_link(wl, &wl->sta_hlid);
837 * send test command to firmware
840 * @buf: buffer containing the command, with all headers, must work with dma
841 * @len: length of the buffer
842 * @answer: is answer needed
844 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
849 wl1271_debug(DEBUG_CMD, "cmd test");
854 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
857 wl1271_warning("TEST command failed");
865 * read acx from firmware
869 * @buf: buffer for the response, including all headers, must work with dma
870 * @len: length of buf
872 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
874 struct acx_header *acx = buf;
877 wl1271_debug(DEBUG_CMD, "cmd interrogate");
879 acx->id = cpu_to_le16(id);
881 /* payload length, does not include any headers */
882 acx->len = cpu_to_le16(len - sizeof(*acx));
884 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
886 wl1271_error("INTERROGATE command failed");
892 * write acx value to firmware
896 * @buf: buffer containing acx, including all headers, must work with dma
897 * @len: length of buf
899 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
901 struct acx_header *acx = buf;
904 wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
906 acx->id = cpu_to_le16(id);
908 /* payload length, does not include any headers */
909 acx->len = cpu_to_le16(len - sizeof(*acx));
911 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
913 wl1271_warning("CONFIGURE command NOK");
920 int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
922 struct cmd_enabledisable_path *cmd;
926 wl1271_debug(DEBUG_CMD, "cmd data path");
928 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
934 /* the channel here is only used for calibration, so hardcoded to 1 */
938 cmd_rx = CMD_ENABLE_RX;
939 cmd_tx = CMD_ENABLE_TX;
941 cmd_rx = CMD_DISABLE_RX;
942 cmd_tx = CMD_DISABLE_TX;
945 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
947 wl1271_error("rx %s cmd for channel %d failed",
948 enable ? "start" : "stop", cmd->channel);
952 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
953 enable ? "start" : "stop", cmd->channel);
955 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
957 wl1271_error("tx %s cmd for channel %d failed",
958 enable ? "start" : "stop", cmd->channel);
962 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
963 enable ? "start" : "stop", cmd->channel);
970 int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
972 struct wl1271_cmd_ps_params *ps_params = NULL;
975 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
977 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
983 ps_params->role_id = wl->role_id;
984 ps_params->ps_mode = ps_mode;
986 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
987 sizeof(*ps_params), 0);
989 wl1271_error("cmd set_ps_mode failed");
998 int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
999 void *buf, size_t buf_len, int index, u32 rates)
1001 struct wl1271_cmd_template_set *cmd;
1004 wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
1006 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
1007 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
1009 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1015 cmd->len = cpu_to_le16(buf_len);
1016 cmd->template_type = template_id;
1017 cmd->enabled_rates = cpu_to_le32(rates);
1018 cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
1019 cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
1023 memcpy(cmd->template_data, buf, buf_len);
1025 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
1027 wl1271_warning("cmd set_template failed: %d", ret);
1038 int wl1271_cmd_build_null_data(struct wl1271 *wl)
1040 struct sk_buff *skb = NULL;
1046 if (wl->bss_type == BSS_TYPE_IBSS) {
1047 size = sizeof(struct wl12xx_null_data_template);
1050 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
1057 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
1063 wl1271_warning("cmd buld null data failed %d", ret);
1069 int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
1071 struct sk_buff *skb = NULL;
1074 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
1078 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
1079 skb->data, skb->len,
1080 CMD_TEMPL_KLV_IDX_NULL_DATA,
1086 wl1271_warning("cmd build klv null data failed %d", ret);
1092 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
1094 struct sk_buff *skb;
1097 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
1101 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
1102 skb->len, 0, wl->basic_rate_set);
1109 int wl1271_cmd_build_probe_req(struct wl1271 *wl,
1110 const u8 *ssid, size_t ssid_len,
1111 const u8 *ie, size_t ie_len, u8 band)
1113 struct sk_buff *skb;
1117 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
1124 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
1126 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[band]);
1127 if (band == IEEE80211_BAND_2GHZ)
1128 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
1129 skb->data, skb->len, 0, rate);
1131 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
1132 skb->data, skb->len, 0, rate);
1139 struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
1140 struct sk_buff *skb)
1146 skb = ieee80211_ap_probereq_get(wl->hw, wl->vif);
1150 wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
1152 rate = wl1271_tx_min_rate_get(wl, wl->bitrate_masks[wl->band]);
1153 if (wl->band == IEEE80211_BAND_2GHZ)
1154 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
1155 skb->data, skb->len, 0, rate);
1157 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
1158 skb->data, skb->len, 0, rate);
1161 wl1271_error("Unable to set ap probe request template.");
1167 int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, __be32 ip_addr)
1170 struct wl12xx_arp_rsp_template tmpl;
1171 struct ieee80211_hdr_3addr *hdr;
1172 struct arphdr *arp_hdr;
1174 memset(&tmpl, 0, sizeof(tmpl));
1176 /* mac80211 header */
1178 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1179 IEEE80211_STYPE_DATA |
1180 IEEE80211_FCTL_TODS);
1181 memcpy(hdr->addr1, wl->vif->bss_conf.bssid, ETH_ALEN);
1182 memcpy(hdr->addr2, wl->vif->addr, ETH_ALEN);
1183 memset(hdr->addr3, 0xff, ETH_ALEN);
1186 memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
1187 tmpl.llc_type = cpu_to_be16(ETH_P_ARP);
1190 arp_hdr = &tmpl.arp_hdr;
1191 arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
1192 arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
1193 arp_hdr->ar_hln = ETH_ALEN;
1194 arp_hdr->ar_pln = 4;
1195 arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
1198 memcpy(tmpl.sender_hw, wl->vif->addr, ETH_ALEN);
1199 tmpl.sender_ip = ip_addr;
1201 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
1202 &tmpl, sizeof(tmpl), 0,
1208 int wl1271_build_qos_null_data(struct wl1271 *wl)
1210 struct ieee80211_qos_hdr template;
1212 memset(&template, 0, sizeof(template));
1214 memcpy(template.addr1, wl->bssid, ETH_ALEN);
1215 memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
1216 memcpy(template.addr3, wl->bssid, ETH_ALEN);
1218 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1219 IEEE80211_STYPE_QOS_NULLFUNC |
1220 IEEE80211_FCTL_TODS);
1222 /* FIXME: not sure what priority to use here */
1223 template.qos_ctrl = cpu_to_le16(0);
1225 return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
1226 sizeof(template), 0,
1230 int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
1232 struct wl1271_cmd_set_keys *cmd;
1235 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
1237 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1245 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1246 cmd->key_action = cpu_to_le16(KEY_SET_ID);
1247 cmd->key_type = KEY_WEP;
1249 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1251 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
1261 int wl1271_cmd_set_sta_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
1262 u8 key_size, const u8 *key, const u8 *addr,
1263 u32 tx_seq_32, u16 tx_seq_16)
1265 struct wl1271_cmd_set_keys *cmd;
1268 /* hlid might have already been deleted */
1269 if (wl->sta_hlid == WL12XX_INVALID_LINK_ID)
1272 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1278 cmd->hlid = wl->sta_hlid;
1280 if (key_type == KEY_WEP)
1281 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1282 else if (is_broadcast_ether_addr(addr))
1283 cmd->lid_key_type = BROADCAST_LID_TYPE;
1285 cmd->lid_key_type = UNICAST_LID_TYPE;
1287 cmd->key_action = cpu_to_le16(action);
1288 cmd->key_size = key_size;
1289 cmd->key_type = key_type;
1291 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1292 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1296 if (key_type == KEY_TKIP) {
1298 * We get the key in the following form:
1299 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1300 * but the target is expecting:
1301 * TKIP - RX MIC - TX MIC
1303 memcpy(cmd->key, key, 16);
1304 memcpy(cmd->key + 16, key + 24, 8);
1305 memcpy(cmd->key + 24, key + 16, 8);
1308 memcpy(cmd->key, key, key_size);
1311 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1313 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1315 wl1271_warning("could not set keys");
1326 * TODO: merge with sta/ibss into 1 set_key function.
1327 * note there are slight diffs
1329 int wl1271_cmd_set_ap_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
1330 u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
1333 struct wl1271_cmd_set_keys *cmd;
1337 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1341 if (hlid == wl->ap_bcast_hlid) {
1342 if (key_type == KEY_WEP)
1343 lid_type = WEP_DEFAULT_LID_TYPE;
1345 lid_type = BROADCAST_LID_TYPE;
1347 lid_type = UNICAST_LID_TYPE;
1350 wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
1351 " hlid: %d", (int)action, (int)id, (int)lid_type,
1352 (int)key_type, (int)hlid);
1354 cmd->lid_key_type = lid_type;
1356 cmd->key_action = cpu_to_le16(action);
1357 cmd->key_size = key_size;
1358 cmd->key_type = key_type;
1360 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1361 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1363 if (key_type == KEY_TKIP) {
1365 * We get the key in the following form:
1366 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1367 * but the target is expecting:
1368 * TKIP - RX MIC - TX MIC
1370 memcpy(cmd->key, key, 16);
1371 memcpy(cmd->key + 16, key + 24, 8);
1372 memcpy(cmd->key + 24, key + 16, 8);
1374 memcpy(cmd->key, key, key_size);
1377 wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
1379 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1381 wl1271_warning("could not set ap keys");
1390 int wl12xx_cmd_set_peer_state(struct wl1271 *wl, u8 hlid)
1392 struct wl12xx_cmd_set_peer_state *cmd;
1395 wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
1397 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1404 cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1406 ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
1408 wl1271_error("failed to send set peer state command");
1419 int wl12xx_cmd_add_peer(struct wl1271 *wl, struct ieee80211_sta *sta, u8 hlid)
1421 struct wl12xx_cmd_add_peer *cmd;
1425 wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
1427 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1433 memcpy(cmd->addr, sta->addr, ETH_ALEN);
1434 cmd->bss_index = WL1271_AP_BSS_INDEX;
1435 cmd->aid = sta->aid;
1437 cmd->sp_len = sta->max_sp;
1438 cmd->wmm = sta->wme ? 1 : 0;
1440 for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
1441 if (sta->wme && (sta->uapsd_queues & BIT(i)))
1442 cmd->psd_type[i] = WL1271_PSD_UPSD_TRIGGER;
1444 cmd->psd_type[i] = WL1271_PSD_LEGACY;
1446 sta_rates = sta->supp_rates[wl->band];
1447 if (sta->ht_cap.ht_supported)
1448 sta_rates |= sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET;
1450 cmd->supported_rates =
1451 cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
1454 wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
1455 cmd->supported_rates, sta->uapsd_queues);
1457 ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
1459 wl1271_error("failed to initiate cmd add peer");
1470 int wl12xx_cmd_remove_peer(struct wl1271 *wl, u8 hlid)
1472 struct wl12xx_cmd_remove_peer *cmd;
1475 wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
1477 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1484 /* We never send a deauth, mac80211 is in charge of this */
1485 cmd->reason_opcode = 0;
1486 cmd->send_deauth_flag = 0;
1488 ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
1490 wl1271_error("failed to initiate cmd remove peer");
1495 * We are ok with a timeout here. The event is sometimes not sent
1496 * due to a firmware bug.
1498 wl1271_cmd_wait_for_event_or_timeout(wl,
1499 PEER_REMOVE_COMPLETE_EVENT_ID);
1508 int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
1510 struct wl12xx_cmd_config_fwlog *cmd;
1513 wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
1515 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1521 cmd->logger_mode = wl->conf.fwlog.mode;
1522 cmd->log_severity = wl->conf.fwlog.severity;
1523 cmd->timestamp = wl->conf.fwlog.timestamp;
1524 cmd->output = wl->conf.fwlog.output;
1525 cmd->threshold = wl->conf.fwlog.threshold;
1527 ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
1529 wl1271_error("failed to send config firmware logger command");
1540 int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
1542 struct wl12xx_cmd_start_fwlog *cmd;
1545 wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
1547 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1553 ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
1555 wl1271_error("failed to send start firmware logger command");
1566 int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
1568 struct wl12xx_cmd_stop_fwlog *cmd;
1571 wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
1573 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1579 ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
1581 wl1271_error("failed to send stop firmware logger command");
1592 static int wl12xx_cmd_roc(struct wl1271 *wl, u8 role_id)
1594 struct wl12xx_cmd_roc *cmd;
1597 wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", wl->channel, role_id);
1599 if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
1602 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1608 cmd->role_id = role_id;
1609 cmd->channel = wl->channel;
1611 case IEEE80211_BAND_2GHZ:
1612 cmd->band = RADIO_BAND_2_4GHZ;
1614 case IEEE80211_BAND_5GHZ:
1615 cmd->band = RADIO_BAND_5GHZ;
1618 wl1271_error("roc - unknown band: %d", (int)wl->band);
1624 ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
1626 wl1271_error("failed to send ROC command");
1637 static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
1639 struct wl12xx_cmd_croc *cmd;
1642 wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
1644 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1649 cmd->role_id = role_id;
1651 ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
1654 wl1271_error("failed to send ROC command");
1665 int wl12xx_roc(struct wl1271 *wl, u8 role_id)
1669 if (WARN_ON(test_bit(role_id, wl->roc_map)))
1672 ret = wl12xx_cmd_roc(wl, role_id);
1676 ret = wl1271_cmd_wait_for_event(wl,
1677 REMAIN_ON_CHANNEL_COMPLETE_EVENT_ID);
1679 wl1271_error("cmd roc event completion error");
1683 __set_bit(role_id, wl->roc_map);
1688 int wl12xx_croc(struct wl1271 *wl, u8 role_id)
1692 if (WARN_ON(!test_bit(role_id, wl->roc_map)))
1695 ret = wl12xx_cmd_croc(wl, role_id);
1699 __clear_bit(role_id, wl->roc_map);