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/pm_runtime.h>
27 #include <linux/spi/spi.h>
28 #include <linux/etherdevice.h>
29 #include <linux/ieee80211.h>
30 #include <linux/slab.h>
36 #include "wl12xx_80211.h"
42 #define WL1271_CMD_FAST_POLL_COUNT 50
43 #define WL1271_WAIT_EVENT_FAST_POLL_COUNT 20
46 * send command to firmware
50 * @buf: buffer containing the command, must work with dma
51 * @len: length of the buffer
52 * return the cmd status code on success.
54 static int __wlcore_cmd_send(struct wl1271 *wl, u16 id, void *buf,
55 size_t len, size_t res_len)
57 struct wl1271_cmd_header *cmd;
58 unsigned long timeout;
64 if (unlikely(wl->state == WLCORE_STATE_RESTARTING &&
65 id != CMD_STOP_FWLOGGER))
68 if (WARN_ON_ONCE(len < sizeof(*cmd)))
72 cmd->id = cpu_to_le16(id);
75 WARN_ON(len % 4 != 0);
76 WARN_ON(test_bit(WL1271_FLAG_IN_ELP, &wl->flags));
78 ret = wlcore_write(wl, wl->cmd_box_addr, buf, len, false);
83 * TODO: we just need this because one bit is in a different
84 * place. Is there any better way?
86 ret = wl->ops->trigger_cmd(wl, wl->cmd_box_addr, buf, len);
90 timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
92 ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
96 while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
97 if (time_after(jiffies, timeout)) {
98 wl1271_error("command complete timeout");
103 if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
108 ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
113 /* read back the status code of the command */
115 res_len = sizeof(struct wl1271_cmd_header);
117 ret = wlcore_read(wl, wl->cmd_box_addr, cmd, res_len, false);
121 status = le16_to_cpu(cmd->status);
123 ret = wlcore_write_reg(wl, REG_INTERRUPT_ACK,
124 WL1271_ACX_INTR_CMD_COMPLETE);
132 * send command to fw and return cmd status on success
133 * valid_rets contains a bitmap of allowed error codes
135 static int wlcore_cmd_send_failsafe(struct wl1271 *wl, u16 id, void *buf,
136 size_t len, size_t res_len,
137 unsigned long valid_rets)
139 int ret = __wlcore_cmd_send(wl, id, buf, len, res_len);
144 /* success is always a valid status */
145 valid_rets |= BIT(CMD_STATUS_SUCCESS);
147 if (ret >= MAX_COMMAND_STATUS ||
148 !test_bit(ret, &valid_rets)) {
149 wl1271_error("command execute failure %d", ret);
155 wl12xx_queue_recovery_work(wl);
160 * wrapper for wlcore_cmd_send that accept only CMD_STATUS_SUCCESS
161 * return 0 on success.
163 int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
166 int ret = wlcore_cmd_send_failsafe(wl, id, buf, len, res_len, 0);
172 EXPORT_SYMBOL_GPL(wl1271_cmd_send);
175 * Poll the mailbox event field until any of the bits in the mask is set or a
176 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
178 int wlcore_cmd_wait_for_event_or_timeout(struct wl1271 *wl,
179 u32 mask, bool *timeout)
183 unsigned long timeout_time;
189 events_vector = kmalloc(sizeof(*events_vector), GFP_KERNEL | GFP_DMA);
193 timeout_time = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
195 ret = pm_runtime_get_sync(wl->dev);
197 pm_runtime_put_noidle(wl->dev);
202 if (time_after(jiffies, timeout_time)) {
203 wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
210 if (poll_count < WL1271_WAIT_EVENT_FAST_POLL_COUNT)
211 usleep_range(50, 51);
213 usleep_range(1000, 5000);
215 /* read from both event fields */
216 ret = wlcore_read(wl, wl->mbox_ptr[0], events_vector,
217 sizeof(*events_vector), false);
221 event = *events_vector & mask;
223 ret = wlcore_read(wl, wl->mbox_ptr[1], events_vector,
224 sizeof(*events_vector), false);
228 event |= *events_vector & mask;
232 pm_runtime_mark_last_busy(wl->dev);
233 pm_runtime_put_autosuspend(wl->dev);
235 kfree(events_vector);
238 EXPORT_SYMBOL_GPL(wlcore_cmd_wait_for_event_or_timeout);
240 int wl12xx_cmd_role_enable(struct wl1271 *wl, u8 *addr, u8 role_type,
243 struct wl12xx_cmd_role_enable *cmd;
246 wl1271_debug(DEBUG_CMD, "cmd role enable");
248 if (WARN_ON(*role_id != WL12XX_INVALID_ROLE_ID))
251 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
258 cmd->role_id = find_first_zero_bit(wl->roles_map, WL12XX_MAX_ROLES);
259 if (cmd->role_id >= WL12XX_MAX_ROLES) {
264 memcpy(cmd->mac_address, addr, ETH_ALEN);
265 cmd->role_type = role_type;
267 ret = wl1271_cmd_send(wl, CMD_ROLE_ENABLE, cmd, sizeof(*cmd), 0);
269 wl1271_error("failed to initiate cmd role enable");
273 __set_bit(cmd->role_id, wl->roles_map);
274 *role_id = cmd->role_id;
283 int wl12xx_cmd_role_disable(struct wl1271 *wl, u8 *role_id)
285 struct wl12xx_cmd_role_disable *cmd;
288 wl1271_debug(DEBUG_CMD, "cmd role disable");
290 if (WARN_ON(*role_id == WL12XX_INVALID_ROLE_ID))
293 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
298 cmd->role_id = *role_id;
300 ret = wl1271_cmd_send(wl, CMD_ROLE_DISABLE, cmd, sizeof(*cmd), 0);
302 wl1271_error("failed to initiate cmd role disable");
306 __clear_bit(*role_id, wl->roles_map);
307 *role_id = WL12XX_INVALID_ROLE_ID;
316 static int wlcore_get_new_session_id(struct wl1271 *wl, u8 hlid)
318 if (wl->session_ids[hlid] >= SESSION_COUNTER_MAX)
319 wl->session_ids[hlid] = 0;
321 wl->session_ids[hlid]++;
323 return wl->session_ids[hlid];
326 int wl12xx_allocate_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
329 u8 link = find_first_zero_bit(wl->links_map, wl->num_links);
330 if (link >= wl->num_links)
333 wl->session_ids[link] = wlcore_get_new_session_id(wl, link);
335 /* these bits are used by op_tx */
336 spin_lock_irqsave(&wl->wl_lock, flags);
337 __set_bit(link, wl->links_map);
338 __set_bit(link, wlvif->links_map);
339 spin_unlock_irqrestore(&wl->wl_lock, flags);
342 * take the last "freed packets" value from the current FW status.
343 * on recovery, we might not have fw_status yet, and
344 * tx_lnk_free_pkts will be NULL. check for it.
346 if (wl->fw_status->counters.tx_lnk_free_pkts)
347 wl->links[link].prev_freed_pkts =
348 wl->fw_status->counters.tx_lnk_free_pkts[link];
349 wl->links[link].wlvif = wlvif;
352 * Take saved value for total freed packets from wlvif, in case this is
355 if (wlvif->bss_type != BSS_TYPE_AP_BSS)
356 wl->links[link].total_freed_pkts = wlvif->total_freed_pkts;
360 wl->active_link_count++;
364 void wl12xx_free_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
368 if (*hlid == WL12XX_INVALID_LINK_ID)
371 /* these bits are used by op_tx */
372 spin_lock_irqsave(&wl->wl_lock, flags);
373 __clear_bit(*hlid, wl->links_map);
374 __clear_bit(*hlid, wlvif->links_map);
375 spin_unlock_irqrestore(&wl->wl_lock, flags);
377 wl->links[*hlid].allocated_pkts = 0;
378 wl->links[*hlid].prev_freed_pkts = 0;
379 wl->links[*hlid].ba_bitmap = 0;
380 eth_zero_addr(wl->links[*hlid].addr);
383 * At this point op_tx() will not add more packets to the queues. We
386 wl1271_tx_reset_link_queues(wl, *hlid);
387 wl->links[*hlid].wlvif = NULL;
389 if (wlvif->bss_type == BSS_TYPE_AP_BSS &&
390 *hlid == wlvif->ap.bcast_hlid) {
391 u32 sqn_padding = WL1271_TX_SQN_POST_RECOVERY_PADDING;
393 * save the total freed packets in the wlvif, in case this is
394 * recovery or suspend
396 wlvif->total_freed_pkts = wl->links[*hlid].total_freed_pkts;
399 * increment the initial seq number on recovery to account for
400 * transmitted packets that we haven't yet got in the FW status
402 if (wlvif->encryption_type == KEY_GEM)
403 sqn_padding = WL1271_TX_SQN_POST_RECOVERY_PADDING_GEM;
405 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
406 wlvif->total_freed_pkts += sqn_padding;
409 wl->links[*hlid].total_freed_pkts = 0;
411 *hlid = WL12XX_INVALID_LINK_ID;
412 wl->active_link_count--;
413 WARN_ON_ONCE(wl->active_link_count < 0);
416 u8 wlcore_get_native_channel_type(u8 nl_channel_type)
418 switch (nl_channel_type) {
419 case NL80211_CHAN_NO_HT:
420 return WLCORE_CHAN_NO_HT;
421 case NL80211_CHAN_HT20:
422 return WLCORE_CHAN_HT20;
423 case NL80211_CHAN_HT40MINUS:
424 return WLCORE_CHAN_HT40MINUS;
425 case NL80211_CHAN_HT40PLUS:
426 return WLCORE_CHAN_HT40PLUS;
429 return WLCORE_CHAN_NO_HT;
432 EXPORT_SYMBOL_GPL(wlcore_get_native_channel_type);
434 static int wl12xx_cmd_role_start_dev(struct wl1271 *wl,
435 struct wl12xx_vif *wlvif,
436 enum nl80211_band band,
439 struct wl12xx_cmd_role_start *cmd;
442 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
448 wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wlvif->dev_role_id);
450 cmd->role_id = wlvif->dev_role_id;
451 if (band == NL80211_BAND_5GHZ)
452 cmd->band = WLCORE_BAND_5GHZ;
453 cmd->channel = channel;
455 if (wlvif->dev_hlid == WL12XX_INVALID_LINK_ID) {
456 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->dev_hlid);
460 cmd->device.hlid = wlvif->dev_hlid;
461 cmd->device.session = wl->session_ids[wlvif->dev_hlid];
463 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d",
464 cmd->role_id, cmd->device.hlid, cmd->device.session);
466 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
468 wl1271_error("failed to initiate cmd role enable");
475 /* clear links on error */
476 wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
485 static int wl12xx_cmd_role_stop_dev(struct wl1271 *wl,
486 struct wl12xx_vif *wlvif)
488 struct wl12xx_cmd_role_stop *cmd;
491 if (WARN_ON(wlvif->dev_hlid == WL12XX_INVALID_LINK_ID))
494 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
500 wl1271_debug(DEBUG_CMD, "cmd role stop dev");
502 cmd->role_id = wlvif->dev_role_id;
503 cmd->disc_type = DISCONNECT_IMMEDIATE;
504 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
506 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
508 wl1271_error("failed to initiate cmd role stop");
512 wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
521 int wl12xx_cmd_role_start_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
523 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
524 struct wl12xx_cmd_role_start *cmd;
528 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
534 wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wlvif->role_id);
536 cmd->role_id = wlvif->role_id;
537 if (wlvif->band == NL80211_BAND_5GHZ)
538 cmd->band = WLCORE_BAND_5GHZ;
539 cmd->channel = wlvif->channel;
540 cmd->sta.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
541 cmd->sta.beacon_interval = cpu_to_le16(wlvif->beacon_int);
542 cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY;
543 cmd->sta.ssid_len = wlvif->ssid_len;
544 memcpy(cmd->sta.ssid, wlvif->ssid, wlvif->ssid_len);
545 memcpy(cmd->sta.bssid, vif->bss_conf.bssid, ETH_ALEN);
547 supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES |
548 wlcore_hw_sta_get_ap_rate_mask(wl, wlvif);
550 supported_rates &= ~CONF_TX_CCK_RATES;
552 cmd->sta.local_rates = cpu_to_le32(supported_rates);
554 cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
556 if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
557 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
561 cmd->sta.hlid = wlvif->sta.hlid;
562 cmd->sta.session = wl->session_ids[wlvif->sta.hlid];
564 * We don't have the correct remote rates in this stage. The
565 * rates will be reconfigured later, after association, if the
566 * firmware supports ACX_PEER_CAP. Otherwise, there's nothing
567 * we can do, so use all supported_rates here.
569 cmd->sta.remote_rates = cpu_to_le32(supported_rates);
571 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
572 "basic_rate_set: 0x%x, remote_rates: 0x%x",
573 wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
574 wlvif->basic_rate_set, wlvif->rate_set);
576 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
578 wl1271_error("failed to initiate cmd role start sta");
582 wlvif->sta.role_chan_type = wlvif->channel_type;
586 /* clear links on error. */
587 wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
596 /* use this function to stop ibss as well */
597 int wl12xx_cmd_role_stop_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
599 struct wl12xx_cmd_role_stop *cmd;
602 if (WARN_ON(wlvif->sta.hlid == WL12XX_INVALID_LINK_ID))
605 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
611 wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wlvif->role_id);
613 cmd->role_id = wlvif->role_id;
614 cmd->disc_type = DISCONNECT_IMMEDIATE;
615 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
617 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
619 wl1271_error("failed to initiate cmd role stop sta");
623 wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
632 int wl12xx_cmd_role_start_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
634 struct wl12xx_cmd_role_start *cmd;
635 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
636 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
640 wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wlvif->role_id);
642 /* If MESH --> ssid_len is always 0 */
643 if (!ieee80211_vif_is_mesh(vif)) {
644 /* trying to use hidden SSID with an old hostapd version */
645 if (wlvif->ssid_len == 0 && !bss_conf->hidden_ssid) {
646 wl1271_error("got a null SSID from beacon/bss");
652 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
658 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.global_hlid);
662 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.bcast_hlid);
664 goto out_free_global;
666 /* use the previous security seq, if this is a recovery/resume */
667 wl->links[wlvif->ap.bcast_hlid].total_freed_pkts =
668 wlvif->total_freed_pkts;
670 cmd->role_id = wlvif->role_id;
671 cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
672 cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
673 cmd->ap.global_hlid = wlvif->ap.global_hlid;
674 cmd->ap.broadcast_hlid = wlvif->ap.bcast_hlid;
675 cmd->ap.global_session_id = wl->session_ids[wlvif->ap.global_hlid];
676 cmd->ap.bcast_session_id = wl->session_ids[wlvif->ap.bcast_hlid];
677 cmd->ap.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
678 cmd->ap.beacon_interval = cpu_to_le16(wlvif->beacon_int);
679 cmd->ap.dtim_interval = bss_conf->dtim_period;
680 cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
681 /* FIXME: Change when adding DFS */
682 cmd->ap.reset_tsf = 1; /* By default reset AP TSF */
683 cmd->ap.wmm = wlvif->wmm_enabled;
684 cmd->channel = wlvif->channel;
685 cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
687 if (!bss_conf->hidden_ssid) {
688 /* take the SSID from the beacon for backward compatibility */
689 cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
690 cmd->ap.ssid_len = wlvif->ssid_len;
691 memcpy(cmd->ap.ssid, wlvif->ssid, wlvif->ssid_len);
693 cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
694 cmd->ap.ssid_len = bss_conf->ssid_len;
695 memcpy(cmd->ap.ssid, bss_conf->ssid, bss_conf->ssid_len);
698 supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES |
699 wlcore_hw_ap_get_mimo_wide_rate_mask(wl, wlvif);
701 supported_rates &= ~CONF_TX_CCK_RATES;
703 wl1271_debug(DEBUG_CMD, "cmd role start ap with supported_rates 0x%08x",
706 cmd->ap.local_rates = cpu_to_le32(supported_rates);
708 switch (wlvif->band) {
709 case NL80211_BAND_2GHZ:
710 cmd->band = WLCORE_BAND_2_4GHZ;
712 case NL80211_BAND_5GHZ:
713 cmd->band = WLCORE_BAND_5GHZ;
716 wl1271_warning("ap start - unknown band: %d", (int)wlvif->band);
717 cmd->band = WLCORE_BAND_2_4GHZ;
721 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
723 wl1271_error("failed to initiate cmd role start ap");
730 wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
733 wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
742 int wl12xx_cmd_role_stop_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
744 struct wl12xx_cmd_role_stop *cmd;
747 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
753 wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wlvif->role_id);
755 cmd->role_id = wlvif->role_id;
757 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
759 wl1271_error("failed to initiate cmd role stop ap");
763 wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
764 wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
773 int wl12xx_cmd_role_start_ibss(struct wl1271 *wl, struct wl12xx_vif *wlvif)
775 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
776 struct wl12xx_cmd_role_start *cmd;
777 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
780 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
786 wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wlvif->role_id);
788 cmd->role_id = wlvif->role_id;
789 if (wlvif->band == NL80211_BAND_5GHZ)
790 cmd->band = WLCORE_BAND_5GHZ;
791 cmd->channel = wlvif->channel;
792 cmd->ibss.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
793 cmd->ibss.beacon_interval = cpu_to_le16(wlvif->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 = wlvif->ssid_len;
797 memcpy(cmd->ibss.ssid, wlvif->ssid, wlvif->ssid_len);
798 memcpy(cmd->ibss.bssid, vif->bss_conf.bssid, ETH_ALEN);
799 cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
801 if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
802 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
806 cmd->ibss.hlid = wlvif->sta.hlid;
807 cmd->ibss.remote_rates = cpu_to_le32(wlvif->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 wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
812 wlvif->basic_rate_set, wlvif->rate_set);
814 wl1271_debug(DEBUG_CMD, "vif->bss_conf.bssid = %pM",
815 vif->bss_conf.bssid);
817 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
819 wl1271_error("failed to initiate cmd role enable");
826 /* clear links on error. */
827 wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
838 * send test command to firmware
841 * @buf: buffer containing the command, with all headers, must work with dma
842 * @len: length of the buffer
843 * @answer: is answer needed
845 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
850 wl1271_debug(DEBUG_CMD, "cmd test");
855 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
858 wl1271_warning("TEST command failed");
864 EXPORT_SYMBOL_GPL(wl1271_cmd_test);
867 * read acx from firmware
871 * @buf: buffer for the response, including all headers, must work with dma
872 * @len: length of buf
874 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf,
875 size_t cmd_len, size_t res_len)
877 struct acx_header *acx = buf;
880 wl1271_debug(DEBUG_CMD, "cmd interrogate");
882 acx->id = cpu_to_le16(id);
884 /* response payload length, does not include any headers */
885 acx->len = cpu_to_le16(res_len - sizeof(*acx));
887 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, cmd_len, res_len);
889 wl1271_error("INTERROGATE command failed");
895 * write acx value to firmware
899 * @buf: buffer containing acx, including all headers, must work with dma
900 * @len: length of buf
901 * @valid_rets: bitmap of valid cmd status codes (i.e. return values).
902 * return the cmd status on success.
904 int wlcore_cmd_configure_failsafe(struct wl1271 *wl, u16 id, void *buf,
905 size_t len, unsigned long valid_rets)
907 struct acx_header *acx = buf;
910 wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
912 if (WARN_ON_ONCE(len < sizeof(*acx)))
915 acx->id = cpu_to_le16(id);
917 /* payload length, does not include any headers */
918 acx->len = cpu_to_le16(len - sizeof(*acx));
920 ret = wlcore_cmd_send_failsafe(wl, CMD_CONFIGURE, acx, len, 0,
923 wl1271_warning("CONFIGURE command NOK");
931 * wrapper for wlcore_cmd_configure that accepts only success status.
932 * return 0 on success
934 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
936 int ret = wlcore_cmd_configure_failsafe(wl, id, buf, len, 0);
942 EXPORT_SYMBOL_GPL(wl1271_cmd_configure);
944 int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
946 struct cmd_enabledisable_path *cmd;
950 wl1271_debug(DEBUG_CMD, "cmd data path");
952 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
958 /* the channel here is only used for calibration, so hardcoded to 1 */
962 cmd_rx = CMD_ENABLE_RX;
963 cmd_tx = CMD_ENABLE_TX;
965 cmd_rx = CMD_DISABLE_RX;
966 cmd_tx = CMD_DISABLE_TX;
969 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
971 wl1271_error("rx %s cmd for channel %d failed",
972 enable ? "start" : "stop", cmd->channel);
976 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
977 enable ? "start" : "stop", cmd->channel);
979 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
981 wl1271_error("tx %s cmd for channel %d failed",
982 enable ? "start" : "stop", cmd->channel);
986 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
987 enable ? "start" : "stop", cmd->channel);
993 EXPORT_SYMBOL_GPL(wl1271_cmd_data_path);
995 int wl1271_cmd_ps_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
996 u8 ps_mode, u16 auto_ps_timeout)
998 struct wl1271_cmd_ps_params *ps_params = NULL;
1001 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
1003 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
1009 ps_params->role_id = wlvif->role_id;
1010 ps_params->ps_mode = ps_mode;
1011 ps_params->auto_ps_timeout = auto_ps_timeout;
1013 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
1014 sizeof(*ps_params), 0);
1016 wl1271_error("cmd set_ps_mode failed");
1025 int wl1271_cmd_template_set(struct wl1271 *wl, u8 role_id,
1026 u16 template_id, void *buf, size_t buf_len,
1027 int index, u32 rates)
1029 struct wl1271_cmd_template_set *cmd;
1032 wl1271_debug(DEBUG_CMD, "cmd template_set %d (role %d)",
1033 template_id, role_id);
1035 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
1036 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
1038 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1044 /* during initialization wlvif is NULL */
1045 cmd->role_id = role_id;
1046 cmd->len = cpu_to_le16(buf_len);
1047 cmd->template_type = template_id;
1048 cmd->enabled_rates = cpu_to_le32(rates);
1049 cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
1050 cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
1054 memcpy(cmd->template_data, buf, buf_len);
1056 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
1058 wl1271_warning("cmd set_template failed: %d", ret);
1069 int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1071 struct sk_buff *skb = NULL;
1077 if (wlvif->bss_type == BSS_TYPE_IBSS) {
1078 size = sizeof(struct wl12xx_null_data_template);
1081 skb = ieee80211_nullfunc_get(wl->hw,
1082 wl12xx_wlvif_to_vif(wlvif),
1090 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1091 CMD_TEMPL_NULL_DATA, ptr, size, 0,
1097 wl1271_warning("cmd buld null data failed %d", ret);
1103 int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl,
1104 struct wl12xx_vif *wlvif)
1106 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1107 struct sk_buff *skb = NULL;
1110 skb = ieee80211_nullfunc_get(wl->hw, vif, false);
1114 ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_KLV,
1115 skb->data, skb->len,
1116 wlvif->sta.klv_template_id,
1122 wl1271_warning("cmd build klv null data failed %d", ret);
1128 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1131 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1132 struct sk_buff *skb;
1135 skb = ieee80211_pspoll_get(wl->hw, vif);
1139 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1140 CMD_TEMPL_PS_POLL, skb->data,
1141 skb->len, 0, wlvif->basic_rate_set);
1148 int wl12xx_cmd_build_probe_req(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1149 u8 role_id, u8 band,
1150 const u8 *ssid, size_t ssid_len,
1151 const u8 *ie0, size_t ie0_len, const u8 *ie1,
1152 size_t ie1_len, bool sched_scan)
1154 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1155 struct sk_buff *skb;
1158 u16 template_id_2_4 = wl->scan_templ_id_2_4;
1159 u16 template_id_5 = wl->scan_templ_id_5;
1161 wl1271_debug(DEBUG_SCAN, "build probe request band %d", band);
1163 skb = ieee80211_probereq_get(wl->hw, vif->addr, ssid, ssid_len,
1170 skb_put_data(skb, ie0, ie0_len);
1172 skb_put_data(skb, ie1, ie1_len);
1175 (wl->quirks & WLCORE_QUIRK_DUAL_PROBE_TMPL)) {
1176 template_id_2_4 = wl->sched_scan_templ_id_2_4;
1177 template_id_5 = wl->sched_scan_templ_id_5;
1180 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
1181 if (band == NL80211_BAND_2GHZ)
1182 ret = wl1271_cmd_template_set(wl, role_id,
1184 skb->data, skb->len, 0, rate);
1186 ret = wl1271_cmd_template_set(wl, role_id,
1188 skb->data, skb->len, 0, rate);
1194 EXPORT_SYMBOL_GPL(wl12xx_cmd_build_probe_req);
1196 struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
1197 struct wl12xx_vif *wlvif,
1198 struct sk_buff *skb)
1200 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1205 skb = ieee80211_ap_probereq_get(wl->hw, vif);
1209 wl1271_debug(DEBUG_SCAN, "set ap probe request template");
1211 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[wlvif->band]);
1212 if (wlvif->band == NL80211_BAND_2GHZ)
1213 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1214 CMD_TEMPL_CFG_PROBE_REQ_2_4,
1215 skb->data, skb->len, 0, rate);
1217 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1218 CMD_TEMPL_CFG_PROBE_REQ_5,
1219 skb->data, skb->len, 0, rate);
1222 wl1271_error("Unable to set ap probe request template.");
1228 int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1232 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1233 struct sk_buff *skb;
1234 struct wl12xx_arp_rsp_template *tmpl;
1235 struct ieee80211_hdr_3addr *hdr;
1236 struct arphdr *arp_hdr;
1238 skb = dev_alloc_skb(sizeof(*hdr) + sizeof(__le16) + sizeof(*tmpl) +
1239 WL1271_EXTRA_SPACE_MAX);
1241 wl1271_error("failed to allocate buffer for arp rsp template");
1245 skb_reserve(skb, sizeof(*hdr) + WL1271_EXTRA_SPACE_MAX);
1247 tmpl = skb_put_zero(skb, sizeof(*tmpl));
1250 memcpy(tmpl->llc_hdr, rfc1042_header, sizeof(rfc1042_header));
1251 tmpl->llc_type = cpu_to_be16(ETH_P_ARP);
1254 arp_hdr = &tmpl->arp_hdr;
1255 arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
1256 arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
1257 arp_hdr->ar_hln = ETH_ALEN;
1258 arp_hdr->ar_pln = 4;
1259 arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
1262 memcpy(tmpl->sender_hw, vif->addr, ETH_ALEN);
1263 tmpl->sender_ip = wlvif->ip_addr;
1265 /* encryption space */
1266 switch (wlvif->encryption_type) {
1268 if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
1269 extra = WL1271_EXTRA_SPACE_TKIP;
1272 extra = WL1271_EXTRA_SPACE_AES;
1280 wl1271_warning("Unknown encryption type: %d",
1281 wlvif->encryption_type);
1287 u8 *space = skb_push(skb, extra);
1288 memset(space, 0, extra);
1291 /* QoS header - BE */
1293 memset(skb_push(skb, sizeof(__le16)), 0, sizeof(__le16));
1295 /* mac80211 header */
1296 hdr = skb_push(skb, sizeof(*hdr));
1297 memset(hdr, 0, sizeof(*hdr));
1298 fc = IEEE80211_FTYPE_DATA | IEEE80211_FCTL_TODS;
1300 fc |= IEEE80211_STYPE_QOS_DATA;
1302 fc |= IEEE80211_STYPE_DATA;
1303 if (wlvif->encryption_type != KEY_NONE)
1304 fc |= IEEE80211_FCTL_PROTECTED;
1306 hdr->frame_control = cpu_to_le16(fc);
1307 memcpy(hdr->addr1, vif->bss_conf.bssid, ETH_ALEN);
1308 memcpy(hdr->addr2, vif->addr, ETH_ALEN);
1309 eth_broadcast_addr(hdr->addr3);
1311 ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_ARP_RSP,
1312 skb->data, skb->len, 0,
1319 int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
1321 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
1322 struct ieee80211_qos_hdr template;
1324 memset(&template, 0, sizeof(template));
1326 memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN);
1327 memcpy(template.addr2, vif->addr, ETH_ALEN);
1328 memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN);
1330 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1331 IEEE80211_STYPE_QOS_NULLFUNC |
1332 IEEE80211_FCTL_TODS);
1334 /* FIXME: not sure what priority to use here */
1335 template.qos_ctrl = cpu_to_le16(0);
1337 return wl1271_cmd_template_set(wl, wlvif->role_id,
1338 CMD_TEMPL_QOS_NULL_DATA, &template,
1339 sizeof(template), 0,
1343 int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
1345 struct wl1271_cmd_set_keys *cmd;
1348 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
1350 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1358 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1359 cmd->key_action = cpu_to_le16(KEY_SET_ID);
1360 cmd->key_type = KEY_WEP;
1362 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1364 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
1374 int wl1271_cmd_set_sta_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1375 u16 action, u8 id, u8 key_type,
1376 u8 key_size, const u8 *key, const u8 *addr,
1377 u32 tx_seq_32, u16 tx_seq_16)
1379 struct wl1271_cmd_set_keys *cmd;
1382 /* hlid might have already been deleted */
1383 if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
1386 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1392 cmd->hlid = wlvif->sta.hlid;
1394 if (key_type == KEY_WEP)
1395 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1396 else if (is_broadcast_ether_addr(addr))
1397 cmd->lid_key_type = BROADCAST_LID_TYPE;
1399 cmd->lid_key_type = UNICAST_LID_TYPE;
1401 cmd->key_action = cpu_to_le16(action);
1402 cmd->key_size = key_size;
1403 cmd->key_type = key_type;
1405 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1406 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1410 if (key_type == KEY_TKIP) {
1412 * We get the key in the following form:
1413 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1414 * but the target is expecting:
1415 * TKIP - RX MIC - TX MIC
1417 memcpy(cmd->key, key, 16);
1418 memcpy(cmd->key + 16, key + 24, 8);
1419 memcpy(cmd->key + 24, key + 16, 8);
1422 memcpy(cmd->key, key, key_size);
1425 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1427 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1429 wl1271_warning("could not set keys");
1440 * TODO: merge with sta/ibss into 1 set_key function.
1441 * note there are slight diffs
1443 int wl1271_cmd_set_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1444 u16 action, u8 id, u8 key_type,
1445 u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
1448 struct wl1271_cmd_set_keys *cmd;
1452 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1456 if (hlid == wlvif->ap.bcast_hlid) {
1457 if (key_type == KEY_WEP)
1458 lid_type = WEP_DEFAULT_LID_TYPE;
1460 lid_type = BROADCAST_LID_TYPE;
1462 lid_type = UNICAST_LID_TYPE;
1465 wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
1466 " hlid: %d", (int)action, (int)id, (int)lid_type,
1467 (int)key_type, (int)hlid);
1469 cmd->lid_key_type = lid_type;
1471 cmd->key_action = cpu_to_le16(action);
1472 cmd->key_size = key_size;
1473 cmd->key_type = key_type;
1475 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1476 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1478 if (key_type == KEY_TKIP) {
1480 * We get the key in the following form:
1481 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1482 * but the target is expecting:
1483 * TKIP - RX MIC - TX MIC
1485 memcpy(cmd->key, key, 16);
1486 memcpy(cmd->key + 16, key + 24, 8);
1487 memcpy(cmd->key + 24, key + 16, 8);
1489 memcpy(cmd->key, key, key_size);
1492 wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
1494 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1496 wl1271_warning("could not set ap keys");
1505 int wl12xx_cmd_set_peer_state(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1508 struct wl12xx_cmd_set_peer_state *cmd;
1511 wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
1513 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1520 cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1522 /* wmm param is valid only for station role */
1523 if (wlvif->bss_type == BSS_TYPE_STA_BSS)
1524 cmd->wmm = wlvif->wmm_enabled;
1526 ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
1528 wl1271_error("failed to send set peer state command");
1539 int wl12xx_cmd_add_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1540 struct ieee80211_sta *sta, u8 hlid)
1542 struct wl12xx_cmd_add_peer *cmd;
1546 wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
1548 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1554 memcpy(cmd->addr, sta->addr, ETH_ALEN);
1555 cmd->bss_index = WL1271_AP_BSS_INDEX;
1556 cmd->aid = sta->aid;
1558 cmd->sp_len = sta->max_sp;
1559 cmd->wmm = sta->wme ? 1 : 0;
1560 cmd->session_id = wl->session_ids[hlid];
1561 cmd->role_id = wlvif->role_id;
1563 for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
1564 if (sta->wme && (sta->uapsd_queues & BIT(i)))
1565 cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
1566 WL1271_PSD_UPSD_TRIGGER;
1568 cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
1572 sta_rates = sta->supp_rates[wlvif->band];
1573 if (sta->ht_cap.ht_supported)
1575 (sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET) |
1576 (sta->ht_cap.mcs.rx_mask[1] << HW_MIMO_RATES_OFFSET);
1578 cmd->supported_rates =
1579 cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
1582 if (!cmd->supported_rates) {
1583 wl1271_debug(DEBUG_CMD,
1584 "peer has no supported rates yet, configuring basic rates: 0x%x",
1585 wlvif->basic_rate_set);
1586 cmd->supported_rates = cpu_to_le32(wlvif->basic_rate_set);
1589 wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
1590 cmd->supported_rates, sta->uapsd_queues);
1592 ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
1594 wl1271_error("failed to initiate cmd add peer");
1605 int wl12xx_cmd_remove_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1608 struct wl12xx_cmd_remove_peer *cmd;
1610 bool timeout = false;
1612 wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
1614 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1621 /* We never send a deauth, mac80211 is in charge of this */
1622 cmd->reason_opcode = 0;
1623 cmd->send_deauth_flag = 0;
1624 cmd->role_id = wlvif->role_id;
1626 ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
1628 wl1271_error("failed to initiate cmd remove peer");
1632 ret = wl->ops->wait_for_event(wl,
1633 WLCORE_EVENT_PEER_REMOVE_COMPLETE,
1637 * We are ok with a timeout here. The event is sometimes not sent
1638 * due to a firmware bug. In case of another error (like SDIO timeout)
1642 wl12xx_queue_recovery_work(wl);
1651 static int wlcore_get_reg_conf_ch_idx(enum nl80211_band band, u16 ch)
1654 * map the given band/channel to the respective predefined
1655 * bit expected by the fw
1658 case NL80211_BAND_2GHZ:
1659 /* channels 1..14 are mapped to 0..13 */
1660 if (ch >= 1 && ch <= 14)
1663 case NL80211_BAND_5GHZ:
1666 /* channels 8,12,16 are mapped to 18,19,20 */
1667 return 18 + (ch-8)/4;
1669 /* channels 34,36..48 are mapped to 21..28 */
1670 return 21 + (ch-34)/2;
1672 /* channels 52,56..64 are mapped to 29..32 */
1673 return 29 + (ch-52)/4;
1675 /* channels 100,104..140 are mapped to 33..43 */
1676 return 33 + (ch-100)/4;
1678 /* channels 149,153..165 are mapped to 44..48 */
1679 return 44 + (ch-149)/4;
1688 wl1271_error("%s: unknown band/channel: %d/%d", __func__, band, ch);
1692 void wlcore_set_pending_regdomain_ch(struct wl1271 *wl, u16 channel,
1693 enum nl80211_band band)
1697 if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
1700 ch_bit_idx = wlcore_get_reg_conf_ch_idx(band, channel);
1702 if (ch_bit_idx >= 0 && ch_bit_idx <= WL1271_MAX_CHANNELS)
1703 __set_bit_le(ch_bit_idx, (long *)wl->reg_ch_conf_pending);
1706 int wlcore_cmd_regdomain_config_locked(struct wl1271 *wl)
1708 struct wl12xx_cmd_regdomain_dfs_config *cmd = NULL;
1709 int ret = 0, i, b, ch_bit_idx;
1710 __le32 tmp_ch_bitmap[2] __aligned(sizeof(unsigned long));
1711 struct wiphy *wiphy = wl->hw->wiphy;
1712 struct ieee80211_supported_band *band;
1713 bool timeout = false;
1715 if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
1718 wl1271_debug(DEBUG_CMD, "cmd reg domain config");
1720 memcpy(tmp_ch_bitmap, wl->reg_ch_conf_pending, sizeof(tmp_ch_bitmap));
1722 for (b = NL80211_BAND_2GHZ; b <= NL80211_BAND_5GHZ; b++) {
1723 band = wiphy->bands[b];
1724 for (i = 0; i < band->n_channels; i++) {
1725 struct ieee80211_channel *channel = &band->channels[i];
1726 u16 ch = channel->hw_value;
1727 u32 flags = channel->flags;
1729 if (flags & (IEEE80211_CHAN_DISABLED |
1730 IEEE80211_CHAN_NO_IR))
1733 if ((flags & IEEE80211_CHAN_RADAR) &&
1734 channel->dfs_state != NL80211_DFS_AVAILABLE)
1737 ch_bit_idx = wlcore_get_reg_conf_ch_idx(b, ch);
1741 __set_bit_le(ch_bit_idx, (long *)tmp_ch_bitmap);
1745 if (!memcmp(tmp_ch_bitmap, wl->reg_ch_conf_last, sizeof(tmp_ch_bitmap)))
1748 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1754 cmd->ch_bit_map1 = tmp_ch_bitmap[0];
1755 cmd->ch_bit_map2 = tmp_ch_bitmap[1];
1756 cmd->dfs_region = wl->dfs_region;
1758 wl1271_debug(DEBUG_CMD,
1759 "cmd reg domain bitmap1: 0x%08x, bitmap2: 0x%08x",
1760 cmd->ch_bit_map1, cmd->ch_bit_map2);
1762 ret = wl1271_cmd_send(wl, CMD_DFS_CHANNEL_CONFIG, cmd, sizeof(*cmd), 0);
1764 wl1271_error("failed to send reg domain dfs config");
1768 ret = wl->ops->wait_for_event(wl,
1769 WLCORE_EVENT_DFS_CONFIG_COMPLETE,
1771 if (ret < 0 || timeout) {
1772 wl1271_error("reg domain conf %serror",
1773 timeout ? "completion " : "");
1774 ret = timeout ? -ETIMEDOUT : ret;
1778 memcpy(wl->reg_ch_conf_last, tmp_ch_bitmap, sizeof(tmp_ch_bitmap));
1779 memset(wl->reg_ch_conf_pending, 0, sizeof(wl->reg_ch_conf_pending));
1786 int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
1788 struct wl12xx_cmd_config_fwlog *cmd;
1791 wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
1793 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1799 cmd->logger_mode = wl->conf.fwlog.mode;
1800 cmd->log_severity = wl->conf.fwlog.severity;
1801 cmd->timestamp = wl->conf.fwlog.timestamp;
1802 cmd->output = wl->conf.fwlog.output;
1803 cmd->threshold = wl->conf.fwlog.threshold;
1805 ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
1807 wl1271_error("failed to send config firmware logger command");
1818 int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
1820 struct wl12xx_cmd_start_fwlog *cmd;
1823 wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
1825 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1831 ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
1833 wl1271_error("failed to send start firmware logger command");
1844 int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
1846 struct wl12xx_cmd_stop_fwlog *cmd;
1849 wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
1851 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1857 ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
1859 wl1271_error("failed to send stop firmware logger command");
1870 static int wl12xx_cmd_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1871 u8 role_id, enum nl80211_band band, u8 channel)
1873 struct wl12xx_cmd_roc *cmd;
1876 wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", channel, role_id);
1878 if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
1881 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1887 cmd->role_id = role_id;
1888 cmd->channel = channel;
1890 case NL80211_BAND_2GHZ:
1891 cmd->band = WLCORE_BAND_2_4GHZ;
1893 case NL80211_BAND_5GHZ:
1894 cmd->band = WLCORE_BAND_5GHZ;
1897 wl1271_error("roc - unknown band: %d", (int)wlvif->band);
1903 ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
1905 wl1271_error("failed to send ROC command");
1916 static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
1918 struct wl12xx_cmd_croc *cmd;
1921 wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
1923 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1928 cmd->role_id = role_id;
1930 ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
1933 wl1271_error("failed to send ROC command");
1944 int wl12xx_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 role_id,
1945 enum nl80211_band band, u8 channel)
1949 if (WARN_ON(test_bit(role_id, wl->roc_map)))
1952 ret = wl12xx_cmd_roc(wl, wlvif, role_id, band, channel);
1956 __set_bit(role_id, wl->roc_map);
1961 int wl12xx_croc(struct wl1271 *wl, u8 role_id)
1965 if (WARN_ON(!test_bit(role_id, wl->roc_map)))
1968 ret = wl12xx_cmd_croc(wl, role_id);
1972 __clear_bit(role_id, wl->roc_map);
1975 * Rearm the tx watchdog when removing the last ROC. This prevents
1976 * recoveries due to just finished ROCs - when Tx hasn't yet had
1977 * a chance to get out.
1979 if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES)
1980 wl12xx_rearm_tx_watchdog_locked(wl);
1985 int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1987 struct wl12xx_cmd_stop_channel_switch *cmd;
1990 wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
1992 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1998 cmd->role_id = wlvif->role_id;
2000 ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
2002 wl1271_error("failed to stop channel switch command");
2013 /* start dev role and roc on its channel */
2014 int wl12xx_start_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2015 enum nl80211_band band, int channel)
2019 if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
2020 wlvif->bss_type == BSS_TYPE_IBSS)))
2023 /* the dev role is already started for p2p mgmt interfaces */
2024 if (!wlcore_is_p2p_mgmt(wlvif)) {
2025 ret = wl12xx_cmd_role_enable(wl,
2026 wl12xx_wlvif_to_vif(wlvif)->addr,
2028 &wlvif->dev_role_id);
2033 ret = wl12xx_cmd_role_start_dev(wl, wlvif, band, channel);
2037 ret = wl12xx_roc(wl, wlvif, wlvif->dev_role_id, band, channel);
2044 wl12xx_cmd_role_stop_dev(wl, wlvif);
2046 if (!wlcore_is_p2p_mgmt(wlvif))
2047 wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
2052 /* croc dev hlid, and stop the role */
2053 int wl12xx_stop_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2057 if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
2058 wlvif->bss_type == BSS_TYPE_IBSS)))
2061 /* flush all pending packets */
2062 ret = wlcore_tx_work_locked(wl);
2066 if (test_bit(wlvif->dev_role_id, wl->roc_map)) {
2067 ret = wl12xx_croc(wl, wlvif->dev_role_id);
2072 ret = wl12xx_cmd_role_stop_dev(wl, wlvif);
2076 if (!wlcore_is_p2p_mgmt(wlvif)) {
2077 ret = wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
2086 int wlcore_cmd_generic_cfg(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2087 u8 feature, u8 enable, u8 value)
2089 struct wlcore_cmd_generic_cfg *cmd;
2092 wl1271_debug(DEBUG_CMD,
2093 "cmd generic cfg (role %d feature %d enable %d value %d)",
2094 wlvif->role_id, feature, enable, value);
2096 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2100 cmd->role_id = wlvif->role_id;
2101 cmd->feature = feature;
2102 cmd->enable = enable;
2105 ret = wl1271_cmd_send(wl, CMD_GENERIC_CFG, cmd, sizeof(*cmd), 0);
2107 wl1271_error("failed to send generic cfg command");
2114 EXPORT_SYMBOL_GPL(wlcore_cmd_generic_cfg);