1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file is part of wl1271
5 * Copyright (C) 2009-2010 Nokia Corporation
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/spi/spi.h>
14 #include <linux/etherdevice.h>
15 #include <linux/ieee80211.h>
16 #include <linux/slab.h>
22 #include "wl12xx_80211.h"
28 #define WL1271_CMD_FAST_POLL_COUNT 50
29 #define WL1271_WAIT_EVENT_FAST_POLL_COUNT 20
32 * send command to firmware
36 * @buf: buffer containing the command, must work with dma
37 * @len: length of the buffer
38 * return the cmd status code on success.
40 static int __wlcore_cmd_send(struct wl1271 *wl, u16 id, void *buf,
41 size_t len, size_t res_len)
43 struct wl1271_cmd_header *cmd;
44 unsigned long timeout;
50 if (unlikely(wl->state == WLCORE_STATE_RESTARTING &&
51 id != CMD_STOP_FWLOGGER))
54 if (WARN_ON_ONCE(len < sizeof(*cmd)))
58 cmd->id = cpu_to_le16(id);
61 WARN_ON(len % 4 != 0);
62 WARN_ON(test_bit(WL1271_FLAG_IN_ELP, &wl->flags));
64 ret = wlcore_write(wl, wl->cmd_box_addr, buf, len, false);
69 * TODO: we just need this because one bit is in a different
70 * place. Is there any better way?
72 ret = wl->ops->trigger_cmd(wl, wl->cmd_box_addr, buf, len);
76 timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
78 ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
82 while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
83 if (time_after(jiffies, timeout)) {
84 wl1271_error("command complete timeout");
89 if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
94 ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
99 /* read back the status code of the command */
101 res_len = sizeof(struct wl1271_cmd_header);
103 ret = wlcore_read(wl, wl->cmd_box_addr, cmd, res_len, false);
107 status = le16_to_cpu(cmd->status);
109 ret = wlcore_write_reg(wl, REG_INTERRUPT_ACK,
110 WL1271_ACX_INTR_CMD_COMPLETE);
118 * send command to fw and return cmd status on success
119 * valid_rets contains a bitmap of allowed error codes
121 static int wlcore_cmd_send_failsafe(struct wl1271 *wl, u16 id, void *buf,
122 size_t len, size_t res_len,
123 unsigned long valid_rets)
125 int ret = __wlcore_cmd_send(wl, id, buf, len, res_len);
130 /* success is always a valid status */
131 valid_rets |= BIT(CMD_STATUS_SUCCESS);
133 if (ret >= MAX_COMMAND_STATUS ||
134 !test_bit(ret, &valid_rets)) {
135 wl1271_error("command execute failure %d", ret);
141 wl12xx_queue_recovery_work(wl);
146 * wrapper for wlcore_cmd_send that accept only CMD_STATUS_SUCCESS
147 * return 0 on success.
149 int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
152 int ret = wlcore_cmd_send_failsafe(wl, id, buf, len, res_len, 0);
158 EXPORT_SYMBOL_GPL(wl1271_cmd_send);
161 * Poll the mailbox event field until any of the bits in the mask is set or a
162 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
164 int wlcore_cmd_wait_for_event_or_timeout(struct wl1271 *wl,
165 u32 mask, bool *timeout)
169 unsigned long timeout_time;
175 events_vector = kmalloc(sizeof(*events_vector), GFP_KERNEL | GFP_DMA);
179 timeout_time = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
181 ret = pm_runtime_resume_and_get(wl->dev);
186 if (time_after(jiffies, timeout_time)) {
187 wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
194 if (poll_count < WL1271_WAIT_EVENT_FAST_POLL_COUNT)
195 usleep_range(50, 51);
197 usleep_range(1000, 5000);
199 /* read from both event fields */
200 ret = wlcore_read(wl, wl->mbox_ptr[0], events_vector,
201 sizeof(*events_vector), false);
205 event = *events_vector & mask;
207 ret = wlcore_read(wl, wl->mbox_ptr[1], events_vector,
208 sizeof(*events_vector), false);
212 event |= *events_vector & mask;
216 pm_runtime_mark_last_busy(wl->dev);
217 pm_runtime_put_autosuspend(wl->dev);
219 kfree(events_vector);
222 EXPORT_SYMBOL_GPL(wlcore_cmd_wait_for_event_or_timeout);
224 int wl12xx_cmd_role_enable(struct wl1271 *wl, u8 *addr, u8 role_type,
227 struct wl12xx_cmd_role_enable *cmd;
230 wl1271_debug(DEBUG_CMD, "cmd role enable");
232 if (WARN_ON(*role_id != WL12XX_INVALID_ROLE_ID))
235 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
242 cmd->role_id = find_first_zero_bit(wl->roles_map, WL12XX_MAX_ROLES);
243 if (cmd->role_id >= WL12XX_MAX_ROLES) {
248 memcpy(cmd->mac_address, addr, ETH_ALEN);
249 cmd->role_type = role_type;
251 ret = wl1271_cmd_send(wl, CMD_ROLE_ENABLE, cmd, sizeof(*cmd), 0);
253 wl1271_error("failed to initiate cmd role enable");
257 __set_bit(cmd->role_id, wl->roles_map);
258 *role_id = cmd->role_id;
267 int wl12xx_cmd_role_disable(struct wl1271 *wl, u8 *role_id)
269 struct wl12xx_cmd_role_disable *cmd;
272 wl1271_debug(DEBUG_CMD, "cmd role disable");
274 if (WARN_ON(*role_id == WL12XX_INVALID_ROLE_ID))
277 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
282 cmd->role_id = *role_id;
284 ret = wl1271_cmd_send(wl, CMD_ROLE_DISABLE, cmd, sizeof(*cmd), 0);
286 wl1271_error("failed to initiate cmd role disable");
290 __clear_bit(*role_id, wl->roles_map);
291 *role_id = WL12XX_INVALID_ROLE_ID;
300 static int wlcore_get_new_session_id(struct wl1271 *wl, u8 hlid)
302 if (wl->session_ids[hlid] >= SESSION_COUNTER_MAX)
303 wl->session_ids[hlid] = 0;
305 wl->session_ids[hlid]++;
307 return wl->session_ids[hlid];
310 int wl12xx_allocate_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
313 u8 link = find_first_zero_bit(wl->links_map, wl->num_links);
314 if (link >= wl->num_links)
317 wl->session_ids[link] = wlcore_get_new_session_id(wl, link);
319 /* these bits are used by op_tx */
320 spin_lock_irqsave(&wl->wl_lock, flags);
321 __set_bit(link, wl->links_map);
322 __set_bit(link, wlvif->links_map);
323 spin_unlock_irqrestore(&wl->wl_lock, flags);
326 * take the last "freed packets" value from the current FW status.
327 * on recovery, we might not have fw_status yet, and
328 * tx_lnk_free_pkts will be NULL. check for it.
330 if (wl->fw_status->counters.tx_lnk_free_pkts)
331 wl->links[link].prev_freed_pkts =
332 wl->fw_status->counters.tx_lnk_free_pkts[link];
333 wl->links[link].wlvif = wlvif;
336 * Take the last sec_pn16 value from the current FW status. On recovery,
337 * we might not have fw_status yet, and tx_lnk_sec_pn16[] will be NULL.
339 if (wl->fw_status->counters.tx_lnk_sec_pn16)
340 wl->links[link].prev_sec_pn16 =
341 le16_to_cpu(wl->fw_status->counters.tx_lnk_sec_pn16[link]);
344 * Take saved value for total freed packets from wlvif, in case this is
347 if (wlvif->bss_type != BSS_TYPE_AP_BSS)
348 wl->links[link].total_freed_pkts = wlvif->total_freed_pkts;
352 wl->active_link_count++;
356 void wl12xx_free_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
360 if (*hlid == WL12XX_INVALID_LINK_ID)
363 /* these bits are used by op_tx */
364 spin_lock_irqsave(&wl->wl_lock, flags);
365 __clear_bit(*hlid, wl->links_map);
366 __clear_bit(*hlid, wlvif->links_map);
367 spin_unlock_irqrestore(&wl->wl_lock, flags);
369 wl->links[*hlid].allocated_pkts = 0;
370 wl->links[*hlid].prev_freed_pkts = 0;
371 wl->links[*hlid].prev_sec_pn16 = 0;
372 wl->links[*hlid].ba_bitmap = 0;
373 eth_zero_addr(wl->links[*hlid].addr);
376 * At this point op_tx() will not add more packets to the queues. We
379 wl1271_tx_reset_link_queues(wl, *hlid);
380 wl->links[*hlid].wlvif = NULL;
382 if (wlvif->bss_type == BSS_TYPE_AP_BSS &&
383 *hlid == wlvif->ap.bcast_hlid) {
384 u32 sqn_padding = WL1271_TX_SQN_POST_RECOVERY_PADDING;
386 * save the total freed packets in the wlvif, in case this is
387 * recovery or suspend
389 wlvif->total_freed_pkts = wl->links[*hlid].total_freed_pkts;
392 * increment the initial seq number on recovery to account for
393 * transmitted packets that we haven't yet got in the FW status
395 if (wlvif->encryption_type == KEY_GEM)
396 sqn_padding = WL1271_TX_SQN_POST_RECOVERY_PADDING_GEM;
398 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
399 wlvif->total_freed_pkts += sqn_padding;
402 wl->links[*hlid].total_freed_pkts = 0;
404 *hlid = WL12XX_INVALID_LINK_ID;
405 wl->active_link_count--;
406 WARN_ON_ONCE(wl->active_link_count < 0);
409 u8 wlcore_get_native_channel_type(u8 nl_channel_type)
411 switch (nl_channel_type) {
412 case NL80211_CHAN_NO_HT:
413 return WLCORE_CHAN_NO_HT;
414 case NL80211_CHAN_HT20:
415 return WLCORE_CHAN_HT20;
416 case NL80211_CHAN_HT40MINUS:
417 return WLCORE_CHAN_HT40MINUS;
418 case NL80211_CHAN_HT40PLUS:
419 return WLCORE_CHAN_HT40PLUS;
422 return WLCORE_CHAN_NO_HT;
425 EXPORT_SYMBOL_GPL(wlcore_get_native_channel_type);
427 static int wl12xx_cmd_role_start_dev(struct wl1271 *wl,
428 struct wl12xx_vif *wlvif,
429 enum nl80211_band band,
432 struct wl12xx_cmd_role_start *cmd;
435 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
441 wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wlvif->dev_role_id);
443 cmd->role_id = wlvif->dev_role_id;
444 if (band == NL80211_BAND_5GHZ)
445 cmd->band = WLCORE_BAND_5GHZ;
446 cmd->channel = channel;
448 if (wlvif->dev_hlid == WL12XX_INVALID_LINK_ID) {
449 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->dev_hlid);
453 cmd->device.hlid = wlvif->dev_hlid;
454 cmd->device.session = wl->session_ids[wlvif->dev_hlid];
456 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d",
457 cmd->role_id, cmd->device.hlid, cmd->device.session);
459 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
461 wl1271_error("failed to initiate cmd role enable");
468 /* clear links on error */
469 wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
478 static int wl12xx_cmd_role_stop_dev(struct wl1271 *wl,
479 struct wl12xx_vif *wlvif)
481 struct wl12xx_cmd_role_stop *cmd;
484 if (WARN_ON(wlvif->dev_hlid == WL12XX_INVALID_LINK_ID))
487 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
493 wl1271_debug(DEBUG_CMD, "cmd role stop dev");
495 cmd->role_id = wlvif->dev_role_id;
496 cmd->disc_type = DISCONNECT_IMMEDIATE;
497 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
499 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
501 wl1271_error("failed to initiate cmd role stop");
505 wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
514 int wl12xx_cmd_role_start_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
516 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
517 struct wl12xx_cmd_role_start *cmd;
521 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
527 wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wlvif->role_id);
529 cmd->role_id = wlvif->role_id;
530 if (wlvif->band == NL80211_BAND_5GHZ)
531 cmd->band = WLCORE_BAND_5GHZ;
532 cmd->channel = wlvif->channel;
533 cmd->sta.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
534 cmd->sta.beacon_interval = cpu_to_le16(wlvif->beacon_int);
535 cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY;
536 cmd->sta.ssid_len = wlvif->ssid_len;
537 memcpy(cmd->sta.ssid, wlvif->ssid, wlvif->ssid_len);
538 memcpy(cmd->sta.bssid, vif->bss_conf.bssid, ETH_ALEN);
540 supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES |
541 wlcore_hw_sta_get_ap_rate_mask(wl, wlvif);
543 supported_rates &= ~CONF_TX_CCK_RATES;
545 cmd->sta.local_rates = cpu_to_le32(supported_rates);
547 cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
549 if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
550 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
554 cmd->sta.hlid = wlvif->sta.hlid;
555 cmd->sta.session = wl->session_ids[wlvif->sta.hlid];
557 * We don't have the correct remote rates in this stage. The
558 * rates will be reconfigured later, after association, if the
559 * firmware supports ACX_PEER_CAP. Otherwise, there's nothing
560 * we can do, so use all supported_rates here.
562 cmd->sta.remote_rates = cpu_to_le32(supported_rates);
564 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
565 "basic_rate_set: 0x%x, remote_rates: 0x%x",
566 wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
567 wlvif->basic_rate_set, wlvif->rate_set);
569 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
571 wl1271_error("failed to initiate cmd role start sta");
575 wlvif->sta.role_chan_type = wlvif->channel_type;
579 /* clear links on error. */
580 wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
589 /* use this function to stop ibss as well */
590 int wl12xx_cmd_role_stop_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
592 struct wl12xx_cmd_role_stop *cmd;
595 if (WARN_ON(wlvif->sta.hlid == WL12XX_INVALID_LINK_ID))
598 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
604 wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wlvif->role_id);
606 cmd->role_id = wlvif->role_id;
607 cmd->disc_type = DISCONNECT_IMMEDIATE;
608 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
610 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
612 wl1271_error("failed to initiate cmd role stop sta");
616 wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
625 int wl12xx_cmd_role_start_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
627 struct wl12xx_cmd_role_start *cmd;
628 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
629 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
633 wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wlvif->role_id);
635 /* If MESH --> ssid_len is always 0 */
636 if (!ieee80211_vif_is_mesh(vif)) {
637 /* trying to use hidden SSID with an old hostapd version */
638 if (wlvif->ssid_len == 0 && !bss_conf->hidden_ssid) {
639 wl1271_error("got a null SSID from beacon/bss");
645 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
651 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.global_hlid);
655 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.bcast_hlid);
657 goto out_free_global;
659 /* use the previous security seq, if this is a recovery/resume */
660 wl->links[wlvif->ap.bcast_hlid].total_freed_pkts =
661 wlvif->total_freed_pkts;
663 cmd->role_id = wlvif->role_id;
664 cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
665 cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
666 cmd->ap.global_hlid = wlvif->ap.global_hlid;
667 cmd->ap.broadcast_hlid = wlvif->ap.bcast_hlid;
668 cmd->ap.global_session_id = wl->session_ids[wlvif->ap.global_hlid];
669 cmd->ap.bcast_session_id = wl->session_ids[wlvif->ap.bcast_hlid];
670 cmd->ap.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
671 cmd->ap.beacon_interval = cpu_to_le16(wlvif->beacon_int);
672 cmd->ap.dtim_interval = bss_conf->dtim_period;
673 cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
674 /* FIXME: Change when adding DFS */
675 cmd->ap.reset_tsf = 1; /* By default reset AP TSF */
676 cmd->ap.wmm = wlvif->wmm_enabled;
677 cmd->channel = wlvif->channel;
678 cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
680 if (!bss_conf->hidden_ssid) {
681 /* take the SSID from the beacon for backward compatibility */
682 cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
683 cmd->ap.ssid_len = wlvif->ssid_len;
684 memcpy(cmd->ap.ssid, wlvif->ssid, wlvif->ssid_len);
686 cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
687 cmd->ap.ssid_len = vif->cfg.ssid_len;
688 memcpy(cmd->ap.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
691 supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES |
692 wlcore_hw_ap_get_mimo_wide_rate_mask(wl, wlvif);
694 supported_rates &= ~CONF_TX_CCK_RATES;
696 wl1271_debug(DEBUG_CMD, "cmd role start ap with supported_rates 0x%08x",
699 cmd->ap.local_rates = cpu_to_le32(supported_rates);
701 switch (wlvif->band) {
702 case NL80211_BAND_2GHZ:
703 cmd->band = WLCORE_BAND_2_4GHZ;
705 case NL80211_BAND_5GHZ:
706 cmd->band = WLCORE_BAND_5GHZ;
709 wl1271_warning("ap start - unknown band: %d", (int)wlvif->band);
710 cmd->band = WLCORE_BAND_2_4GHZ;
714 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
716 wl1271_error("failed to initiate cmd role start ap");
723 wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
726 wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
735 int wl12xx_cmd_role_stop_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
737 struct wl12xx_cmd_role_stop *cmd;
740 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
746 wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wlvif->role_id);
748 cmd->role_id = wlvif->role_id;
750 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
752 wl1271_error("failed to initiate cmd role stop ap");
756 wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
757 wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
766 int wl12xx_cmd_role_start_ibss(struct wl1271 *wl, struct wl12xx_vif *wlvif)
768 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
769 struct wl12xx_cmd_role_start *cmd;
770 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
773 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
779 wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wlvif->role_id);
781 cmd->role_id = wlvif->role_id;
782 if (wlvif->band == NL80211_BAND_5GHZ)
783 cmd->band = WLCORE_BAND_5GHZ;
784 cmd->channel = wlvif->channel;
785 cmd->ibss.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
786 cmd->ibss.beacon_interval = cpu_to_le16(wlvif->beacon_int);
787 cmd->ibss.dtim_interval = bss_conf->dtim_period;
788 cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
789 cmd->ibss.ssid_len = wlvif->ssid_len;
790 memcpy(cmd->ibss.ssid, wlvif->ssid, wlvif->ssid_len);
791 memcpy(cmd->ibss.bssid, vif->bss_conf.bssid, ETH_ALEN);
792 cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
794 if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
795 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
799 cmd->ibss.hlid = wlvif->sta.hlid;
800 cmd->ibss.remote_rates = cpu_to_le32(wlvif->rate_set);
802 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
803 "basic_rate_set: 0x%x, remote_rates: 0x%x",
804 wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
805 wlvif->basic_rate_set, wlvif->rate_set);
807 wl1271_debug(DEBUG_CMD, "vif->bss_conf.bssid = %pM",
808 vif->bss_conf.bssid);
810 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
812 wl1271_error("failed to initiate cmd role enable");
819 /* clear links on error. */
820 wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
831 * wl1271_cmd_test - send test command to firmware
834 * @buf: buffer containing the command, with all headers, must work with dma
835 * @buf_len: length of the buffer
836 * @answer: is answer needed
838 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
843 wl1271_debug(DEBUG_CMD, "cmd test");
848 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
851 wl1271_warning("TEST command failed");
857 EXPORT_SYMBOL_GPL(wl1271_cmd_test);
860 * wl1271_cmd_interrogate - read acx from firmware
864 * @buf: buffer for the response, including all headers, must work with dma
865 * @cmd_len: length of command
866 * @res_len: length of payload
868 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf,
869 size_t cmd_len, size_t res_len)
871 struct acx_header *acx = buf;
874 wl1271_debug(DEBUG_CMD, "cmd interrogate");
876 acx->id = cpu_to_le16(id);
878 /* response payload length, does not include any headers */
879 acx->len = cpu_to_le16(res_len - sizeof(*acx));
881 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, cmd_len, res_len);
883 wl1271_error("INTERROGATE command failed");
889 * wlcore_cmd_configure_failsafe - write acx value to firmware
893 * @buf: buffer containing acx, including all headers, must work with dma
894 * @len: length of buf
895 * @valid_rets: bitmap of valid cmd status codes (i.e. return values).
896 * return the cmd status on success.
898 int wlcore_cmd_configure_failsafe(struct wl1271 *wl, u16 id, void *buf,
899 size_t len, unsigned long valid_rets)
901 struct acx_header *acx = buf;
904 wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
906 if (WARN_ON_ONCE(len < sizeof(*acx)))
909 acx->id = cpu_to_le16(id);
911 /* payload length, does not include any headers */
912 acx->len = cpu_to_le16(len - sizeof(*acx));
914 ret = wlcore_cmd_send_failsafe(wl, CMD_CONFIGURE, acx, len, 0,
917 wl1271_warning("CONFIGURE command NOK");
925 * wrapper for wlcore_cmd_configure that accepts only success status.
926 * return 0 on success
928 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
930 int ret = wlcore_cmd_configure_failsafe(wl, id, buf, len, 0);
936 EXPORT_SYMBOL_GPL(wl1271_cmd_configure);
938 int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
940 struct cmd_enabledisable_path *cmd;
944 wl1271_debug(DEBUG_CMD, "cmd data path");
946 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
952 /* the channel here is only used for calibration, so hardcoded to 1 */
956 cmd_rx = CMD_ENABLE_RX;
957 cmd_tx = CMD_ENABLE_TX;
959 cmd_rx = CMD_DISABLE_RX;
960 cmd_tx = CMD_DISABLE_TX;
963 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
965 wl1271_error("rx %s cmd for channel %d failed",
966 enable ? "start" : "stop", cmd->channel);
970 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
971 enable ? "start" : "stop", cmd->channel);
973 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
975 wl1271_error("tx %s cmd for channel %d failed",
976 enable ? "start" : "stop", cmd->channel);
980 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
981 enable ? "start" : "stop", cmd->channel);
987 EXPORT_SYMBOL_GPL(wl1271_cmd_data_path);
989 int wl1271_cmd_ps_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
990 u8 ps_mode, u16 auto_ps_timeout)
992 struct wl1271_cmd_ps_params *ps_params = NULL;
995 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
997 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
1003 ps_params->role_id = wlvif->role_id;
1004 ps_params->ps_mode = ps_mode;
1005 ps_params->auto_ps_timeout = auto_ps_timeout;
1007 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
1008 sizeof(*ps_params), 0);
1010 wl1271_error("cmd set_ps_mode failed");
1019 int wl1271_cmd_template_set(struct wl1271 *wl, u8 role_id,
1020 u16 template_id, void *buf, size_t buf_len,
1021 int index, u32 rates)
1023 struct wl1271_cmd_template_set *cmd;
1026 wl1271_debug(DEBUG_CMD, "cmd template_set %d (role %d)",
1027 template_id, role_id);
1029 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
1030 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
1032 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1038 /* during initialization wlvif is NULL */
1039 cmd->role_id = role_id;
1040 cmd->len = cpu_to_le16(buf_len);
1041 cmd->template_type = template_id;
1042 cmd->enabled_rates = cpu_to_le32(rates);
1043 cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
1044 cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
1048 memcpy(cmd->template_data, buf, buf_len);
1050 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
1052 wl1271_warning("cmd set_template failed: %d", ret);
1063 int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1065 struct sk_buff *skb = NULL;
1071 if (wlvif->bss_type == BSS_TYPE_IBSS) {
1072 size = sizeof(struct wl12xx_null_data_template);
1075 skb = ieee80211_nullfunc_get(wl->hw,
1076 wl12xx_wlvif_to_vif(wlvif),
1084 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1085 CMD_TEMPL_NULL_DATA, ptr, size, 0,
1091 wl1271_warning("cmd build null data failed %d", ret);
1097 int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl,
1098 struct wl12xx_vif *wlvif)
1100 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1101 struct sk_buff *skb = NULL;
1104 skb = ieee80211_nullfunc_get(wl->hw, vif,-1, false);
1108 ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_KLV,
1109 skb->data, skb->len,
1110 wlvif->sta.klv_template_id,
1116 wl1271_warning("cmd build klv null data failed %d", ret);
1122 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1125 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1126 struct sk_buff *skb;
1129 skb = ieee80211_pspoll_get(wl->hw, vif);
1133 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1134 CMD_TEMPL_PS_POLL, skb->data,
1135 skb->len, 0, wlvif->basic_rate_set);
1142 int wl12xx_cmd_build_probe_req(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1143 u8 role_id, u8 band,
1144 const u8 *ssid, size_t ssid_len,
1145 const u8 *ie0, size_t ie0_len, const u8 *ie1,
1146 size_t ie1_len, bool sched_scan)
1148 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1149 struct sk_buff *skb;
1152 u16 template_id_2_4 = wl->scan_templ_id_2_4;
1153 u16 template_id_5 = wl->scan_templ_id_5;
1155 wl1271_debug(DEBUG_SCAN, "build probe request band %d", band);
1157 skb = ieee80211_probereq_get(wl->hw, vif->addr, ssid, ssid_len,
1164 skb_put_data(skb, ie0, ie0_len);
1166 skb_put_data(skb, ie1, ie1_len);
1169 (wl->quirks & WLCORE_QUIRK_DUAL_PROBE_TMPL)) {
1170 template_id_2_4 = wl->sched_scan_templ_id_2_4;
1171 template_id_5 = wl->sched_scan_templ_id_5;
1174 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
1175 if (band == NL80211_BAND_2GHZ)
1176 ret = wl1271_cmd_template_set(wl, role_id,
1178 skb->data, skb->len, 0, rate);
1180 ret = wl1271_cmd_template_set(wl, role_id,
1182 skb->data, skb->len, 0, rate);
1188 EXPORT_SYMBOL_GPL(wl12xx_cmd_build_probe_req);
1190 struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
1191 struct wl12xx_vif *wlvif,
1192 struct sk_buff *skb)
1194 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1199 skb = ieee80211_ap_probereq_get(wl->hw, vif);
1203 wl1271_debug(DEBUG_SCAN, "set ap probe request template");
1205 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[wlvif->band]);
1206 if (wlvif->band == NL80211_BAND_2GHZ)
1207 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1208 CMD_TEMPL_CFG_PROBE_REQ_2_4,
1209 skb->data, skb->len, 0, rate);
1211 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1212 CMD_TEMPL_CFG_PROBE_REQ_5,
1213 skb->data, skb->len, 0, rate);
1216 wl1271_error("Unable to set ap probe request template.");
1222 int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1226 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1227 struct sk_buff *skb;
1228 struct wl12xx_arp_rsp_template *tmpl;
1229 struct ieee80211_hdr_3addr *hdr;
1230 struct arphdr *arp_hdr;
1232 skb = dev_alloc_skb(sizeof(*hdr) + sizeof(__le16) + sizeof(*tmpl) +
1233 WL1271_EXTRA_SPACE_MAX);
1235 wl1271_error("failed to allocate buffer for arp rsp template");
1239 skb_reserve(skb, sizeof(*hdr) + WL1271_EXTRA_SPACE_MAX);
1241 tmpl = skb_put_zero(skb, sizeof(*tmpl));
1244 memcpy(tmpl->llc_hdr, rfc1042_header, sizeof(rfc1042_header));
1245 tmpl->llc_type = cpu_to_be16(ETH_P_ARP);
1248 arp_hdr = &tmpl->arp_hdr;
1249 arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
1250 arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
1251 arp_hdr->ar_hln = ETH_ALEN;
1252 arp_hdr->ar_pln = 4;
1253 arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
1256 memcpy(tmpl->sender_hw, vif->addr, ETH_ALEN);
1257 tmpl->sender_ip = wlvif->ip_addr;
1259 /* encryption space */
1260 switch (wlvif->encryption_type) {
1262 if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
1263 extra = WL1271_EXTRA_SPACE_TKIP;
1266 extra = WL1271_EXTRA_SPACE_AES;
1274 wl1271_warning("Unknown encryption type: %d",
1275 wlvif->encryption_type);
1281 u8 *space = skb_push(skb, extra);
1282 memset(space, 0, extra);
1285 /* QoS header - BE */
1287 memset(skb_push(skb, sizeof(__le16)), 0, sizeof(__le16));
1289 /* mac80211 header */
1290 hdr = skb_push(skb, sizeof(*hdr));
1291 memset(hdr, 0, sizeof(*hdr));
1292 fc = IEEE80211_FTYPE_DATA | IEEE80211_FCTL_TODS;
1294 fc |= IEEE80211_STYPE_QOS_DATA;
1296 fc |= IEEE80211_STYPE_DATA;
1297 if (wlvif->encryption_type != KEY_NONE)
1298 fc |= IEEE80211_FCTL_PROTECTED;
1300 hdr->frame_control = cpu_to_le16(fc);
1301 memcpy(hdr->addr1, vif->bss_conf.bssid, ETH_ALEN);
1302 memcpy(hdr->addr2, vif->addr, ETH_ALEN);
1303 eth_broadcast_addr(hdr->addr3);
1305 ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_ARP_RSP,
1306 skb->data, skb->len, 0,
1313 int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
1315 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
1316 struct ieee80211_qos_hdr template;
1318 memset(&template, 0, sizeof(template));
1320 memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN);
1321 memcpy(template.addr2, vif->addr, ETH_ALEN);
1322 memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN);
1324 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1325 IEEE80211_STYPE_QOS_NULLFUNC |
1326 IEEE80211_FCTL_TODS);
1328 /* FIXME: not sure what priority to use here */
1329 template.qos_ctrl = cpu_to_le16(0);
1331 return wl1271_cmd_template_set(wl, wlvif->role_id,
1332 CMD_TEMPL_QOS_NULL_DATA, &template,
1333 sizeof(template), 0,
1337 int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
1339 struct wl1271_cmd_set_keys *cmd;
1342 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
1344 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1352 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1353 cmd->key_action = cpu_to_le16(KEY_SET_ID);
1354 cmd->key_type = KEY_WEP;
1356 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1358 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
1368 int wl1271_cmd_set_sta_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1369 u16 action, u8 id, u8 key_type,
1370 u8 key_size, const u8 *key, const u8 *addr,
1371 u32 tx_seq_32, u16 tx_seq_16)
1373 struct wl1271_cmd_set_keys *cmd;
1376 /* hlid might have already been deleted */
1377 if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
1380 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1386 cmd->hlid = wlvif->sta.hlid;
1388 if (key_type == KEY_WEP)
1389 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1390 else if (is_broadcast_ether_addr(addr))
1391 cmd->lid_key_type = BROADCAST_LID_TYPE;
1393 cmd->lid_key_type = UNICAST_LID_TYPE;
1395 cmd->key_action = cpu_to_le16(action);
1396 cmd->key_size = key_size;
1397 cmd->key_type = key_type;
1399 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1400 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1404 if (key_type == KEY_TKIP) {
1406 * We get the key in the following form:
1407 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1408 * but the target is expecting:
1409 * TKIP - RX MIC - TX MIC
1411 memcpy(cmd->key, key, 16);
1412 memcpy(cmd->key + 16, key + 24, 8);
1413 memcpy(cmd->key + 24, key + 16, 8);
1416 memcpy(cmd->key, key, key_size);
1419 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1421 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1423 wl1271_warning("could not set keys");
1434 * TODO: merge with sta/ibss into 1 set_key function.
1435 * note there are slight diffs
1437 int wl1271_cmd_set_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1438 u16 action, u8 id, u8 key_type,
1439 u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
1440 u16 tx_seq_16, bool is_pairwise)
1442 struct wl1271_cmd_set_keys *cmd;
1446 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1450 if (hlid == wlvif->ap.bcast_hlid) {
1451 if (key_type == KEY_WEP)
1452 lid_type = WEP_DEFAULT_LID_TYPE;
1454 lid_type = BROADCAST_LID_TYPE;
1455 } else if (is_pairwise) {
1456 lid_type = UNICAST_LID_TYPE;
1458 lid_type = BROADCAST_LID_TYPE;
1461 wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
1462 " hlid: %d", (int)action, (int)id, (int)lid_type,
1463 (int)key_type, (int)hlid);
1465 cmd->lid_key_type = lid_type;
1467 cmd->key_action = cpu_to_le16(action);
1468 cmd->key_size = key_size;
1469 cmd->key_type = key_type;
1471 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1472 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1474 if (key_type == KEY_TKIP) {
1476 * We get the key in the following form:
1477 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1478 * but the target is expecting:
1479 * TKIP - RX MIC - TX MIC
1481 memcpy(cmd->key, key, 16);
1482 memcpy(cmd->key + 16, key + 24, 8);
1483 memcpy(cmd->key + 24, key + 16, 8);
1485 memcpy(cmd->key, key, key_size);
1488 wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
1490 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1492 wl1271_warning("could not set ap keys");
1501 int wl12xx_cmd_set_peer_state(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1504 struct wl12xx_cmd_set_peer_state *cmd;
1507 wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
1509 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1516 cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1518 /* wmm param is valid only for station role */
1519 if (wlvif->bss_type == BSS_TYPE_STA_BSS)
1520 cmd->wmm = wlvif->wmm_enabled;
1522 ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
1524 wl1271_error("failed to send set peer state command");
1535 int wl12xx_cmd_add_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1536 struct ieee80211_sta *sta, u8 hlid)
1538 struct wl12xx_cmd_add_peer *cmd;
1542 wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
1544 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1550 memcpy(cmd->addr, sta->addr, ETH_ALEN);
1551 cmd->bss_index = WL1271_AP_BSS_INDEX;
1552 cmd->aid = sta->aid;
1554 cmd->sp_len = sta->max_sp;
1555 cmd->wmm = sta->wme ? 1 : 0;
1556 cmd->session_id = wl->session_ids[hlid];
1557 cmd->role_id = wlvif->role_id;
1559 for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
1560 if (sta->wme && (sta->uapsd_queues & BIT(i)))
1561 cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
1562 WL1271_PSD_UPSD_TRIGGER;
1564 cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
1568 sta_rates = sta->deflink.supp_rates[wlvif->band];
1569 if (sta->deflink.ht_cap.ht_supported)
1571 (sta->deflink.ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET) |
1572 (sta->deflink.ht_cap.mcs.rx_mask[1] << HW_MIMO_RATES_OFFSET);
1574 cmd->supported_rates =
1575 cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
1578 wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
1579 cmd->supported_rates, sta->uapsd_queues);
1581 ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
1583 wl1271_error("failed to initiate cmd add peer");
1594 int wl12xx_cmd_remove_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1597 struct wl12xx_cmd_remove_peer *cmd;
1599 bool timeout = false;
1601 wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
1603 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1610 /* We never send a deauth, mac80211 is in charge of this */
1611 cmd->reason_opcode = 0;
1612 cmd->send_deauth_flag = 0;
1613 cmd->role_id = wlvif->role_id;
1615 ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
1617 wl1271_error("failed to initiate cmd remove peer");
1621 ret = wl->ops->wait_for_event(wl,
1622 WLCORE_EVENT_PEER_REMOVE_COMPLETE,
1626 * We are ok with a timeout here. The event is sometimes not sent
1627 * due to a firmware bug. In case of another error (like SDIO timeout)
1631 wl12xx_queue_recovery_work(wl);
1640 static int wlcore_get_reg_conf_ch_idx(enum nl80211_band band, u16 ch)
1643 * map the given band/channel to the respective predefined
1644 * bit expected by the fw
1647 case NL80211_BAND_2GHZ:
1648 /* channels 1..14 are mapped to 0..13 */
1649 if (ch >= 1 && ch <= 14)
1652 case NL80211_BAND_5GHZ:
1655 /* channels 8,12,16 are mapped to 18,19,20 */
1656 return 18 + (ch-8)/4;
1658 /* channels 34,36..48 are mapped to 21..28 */
1659 return 21 + (ch-34)/2;
1661 /* channels 52,56..64 are mapped to 29..32 */
1662 return 29 + (ch-52)/4;
1664 /* channels 100,104..140 are mapped to 33..43 */
1665 return 33 + (ch-100)/4;
1667 /* channels 149,153..165 are mapped to 44..48 */
1668 return 44 + (ch-149)/4;
1677 wl1271_error("%s: unknown band/channel: %d/%d", __func__, band, ch);
1681 void wlcore_set_pending_regdomain_ch(struct wl1271 *wl, u16 channel,
1682 enum nl80211_band band)
1686 if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
1689 ch_bit_idx = wlcore_get_reg_conf_ch_idx(band, channel);
1691 if (ch_bit_idx >= 0 && ch_bit_idx <= WL1271_MAX_CHANNELS)
1692 __set_bit_le(ch_bit_idx, (long *)wl->reg_ch_conf_pending);
1695 int wlcore_cmd_regdomain_config_locked(struct wl1271 *wl)
1697 struct wl12xx_cmd_regdomain_dfs_config *cmd = NULL;
1698 int ret = 0, i, b, ch_bit_idx;
1699 __le32 tmp_ch_bitmap[2] __aligned(sizeof(unsigned long));
1700 struct wiphy *wiphy = wl->hw->wiphy;
1701 struct ieee80211_supported_band *band;
1702 bool timeout = false;
1704 if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
1707 wl1271_debug(DEBUG_CMD, "cmd reg domain config");
1709 memcpy(tmp_ch_bitmap, wl->reg_ch_conf_pending, sizeof(tmp_ch_bitmap));
1711 for (b = NL80211_BAND_2GHZ; b <= NL80211_BAND_5GHZ; b++) {
1712 band = wiphy->bands[b];
1713 for (i = 0; i < band->n_channels; i++) {
1714 struct ieee80211_channel *channel = &band->channels[i];
1715 u16 ch = channel->hw_value;
1716 u32 flags = channel->flags;
1718 if (flags & (IEEE80211_CHAN_DISABLED |
1719 IEEE80211_CHAN_NO_IR))
1722 if ((flags & IEEE80211_CHAN_RADAR) &&
1723 channel->dfs_state != NL80211_DFS_AVAILABLE)
1726 ch_bit_idx = wlcore_get_reg_conf_ch_idx(b, ch);
1730 __set_bit_le(ch_bit_idx, (long *)tmp_ch_bitmap);
1734 if (!memcmp(tmp_ch_bitmap, wl->reg_ch_conf_last, sizeof(tmp_ch_bitmap)))
1737 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1743 cmd->ch_bit_map1 = tmp_ch_bitmap[0];
1744 cmd->ch_bit_map2 = tmp_ch_bitmap[1];
1745 cmd->dfs_region = wl->dfs_region;
1747 wl1271_debug(DEBUG_CMD,
1748 "cmd reg domain bitmap1: 0x%08x, bitmap2: 0x%08x",
1749 cmd->ch_bit_map1, cmd->ch_bit_map2);
1751 ret = wl1271_cmd_send(wl, CMD_DFS_CHANNEL_CONFIG, cmd, sizeof(*cmd), 0);
1753 wl1271_error("failed to send reg domain dfs config");
1757 ret = wl->ops->wait_for_event(wl,
1758 WLCORE_EVENT_DFS_CONFIG_COMPLETE,
1760 if (ret < 0 || timeout) {
1761 wl1271_error("reg domain conf %serror",
1762 timeout ? "completion " : "");
1763 ret = timeout ? -ETIMEDOUT : ret;
1767 memcpy(wl->reg_ch_conf_last, tmp_ch_bitmap, sizeof(tmp_ch_bitmap));
1768 memset(wl->reg_ch_conf_pending, 0, sizeof(wl->reg_ch_conf_pending));
1775 int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
1777 struct wl12xx_cmd_config_fwlog *cmd;
1780 wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
1782 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1788 cmd->logger_mode = wl->conf.fwlog.mode;
1789 cmd->log_severity = wl->conf.fwlog.severity;
1790 cmd->timestamp = wl->conf.fwlog.timestamp;
1791 cmd->output = wl->conf.fwlog.output;
1792 cmd->threshold = wl->conf.fwlog.threshold;
1794 ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
1796 wl1271_error("failed to send config firmware logger command");
1807 int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
1809 struct wl12xx_cmd_start_fwlog *cmd;
1812 wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
1814 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1820 ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
1822 wl1271_error("failed to send start firmware logger command");
1833 int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
1835 struct wl12xx_cmd_stop_fwlog *cmd;
1838 wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
1840 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1846 ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
1848 wl1271_error("failed to send stop firmware logger command");
1859 static int wl12xx_cmd_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1860 u8 role_id, enum nl80211_band band, u8 channel)
1862 struct wl12xx_cmd_roc *cmd;
1865 wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", channel, role_id);
1867 if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
1870 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1876 cmd->role_id = role_id;
1877 cmd->channel = channel;
1879 case NL80211_BAND_2GHZ:
1880 cmd->band = WLCORE_BAND_2_4GHZ;
1882 case NL80211_BAND_5GHZ:
1883 cmd->band = WLCORE_BAND_5GHZ;
1886 wl1271_error("roc - unknown band: %d", (int)wlvif->band);
1892 ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
1894 wl1271_error("failed to send ROC command");
1905 static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
1907 struct wl12xx_cmd_croc *cmd;
1910 wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
1912 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1917 cmd->role_id = role_id;
1919 ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
1922 wl1271_error("failed to send ROC command");
1933 int wl12xx_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 role_id,
1934 enum nl80211_band band, u8 channel)
1938 if (WARN_ON(test_bit(role_id, wl->roc_map)))
1941 ret = wl12xx_cmd_roc(wl, wlvif, role_id, band, channel);
1945 __set_bit(role_id, wl->roc_map);
1950 int wl12xx_croc(struct wl1271 *wl, u8 role_id)
1954 if (WARN_ON(!test_bit(role_id, wl->roc_map)))
1957 ret = wl12xx_cmd_croc(wl, role_id);
1961 __clear_bit(role_id, wl->roc_map);
1964 * Rearm the tx watchdog when removing the last ROC. This prevents
1965 * recoveries due to just finished ROCs - when Tx hasn't yet had
1966 * a chance to get out.
1968 if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES)
1969 wl12xx_rearm_tx_watchdog_locked(wl);
1974 int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1976 struct wl12xx_cmd_stop_channel_switch *cmd;
1979 wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
1981 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1987 cmd->role_id = wlvif->role_id;
1989 ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
1991 wl1271_error("failed to stop channel switch command");
2002 /* start dev role and roc on its channel */
2003 int wl12xx_start_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2004 enum nl80211_band band, int channel)
2008 if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
2009 wlvif->bss_type == BSS_TYPE_IBSS)))
2012 /* the dev role is already started for p2p mgmt interfaces */
2013 if (!wlcore_is_p2p_mgmt(wlvif)) {
2014 ret = wl12xx_cmd_role_enable(wl,
2015 wl12xx_wlvif_to_vif(wlvif)->addr,
2017 &wlvif->dev_role_id);
2022 ret = wl12xx_cmd_role_start_dev(wl, wlvif, band, channel);
2026 ret = wl12xx_roc(wl, wlvif, wlvif->dev_role_id, band, channel);
2033 wl12xx_cmd_role_stop_dev(wl, wlvif);
2035 if (!wlcore_is_p2p_mgmt(wlvif))
2036 wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
2041 /* croc dev hlid, and stop the role */
2042 int wl12xx_stop_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2046 if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
2047 wlvif->bss_type == BSS_TYPE_IBSS)))
2050 /* flush all pending packets */
2051 ret = wlcore_tx_work_locked(wl);
2055 if (test_bit(wlvif->dev_role_id, wl->roc_map)) {
2056 ret = wl12xx_croc(wl, wlvif->dev_role_id);
2061 ret = wl12xx_cmd_role_stop_dev(wl, wlvif);
2065 if (!wlcore_is_p2p_mgmt(wlvif)) {
2066 ret = wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
2075 int wlcore_cmd_generic_cfg(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2076 u8 feature, u8 enable, u8 value)
2078 struct wlcore_cmd_generic_cfg *cmd;
2081 wl1271_debug(DEBUG_CMD,
2082 "cmd generic cfg (role %d feature %d enable %d value %d)",
2083 wlvif->role_id, feature, enable, value);
2085 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2089 cmd->role_id = wlvif->role_id;
2090 cmd->feature = feature;
2091 cmd->enable = enable;
2094 ret = wl1271_cmd_send(wl, CMD_GENERIC_CFG, cmd, sizeof(*cmd), 0);
2096 wl1271_error("failed to send generic cfg command");
2103 EXPORT_SYMBOL_GPL(wlcore_cmd_generic_cfg);