cmd.c revision 633e27132625a0692440c4db58b901fb3cb67c55
1/*
2 * This file is part of wl1271
3 *
4 * Copyright (C) 2009-2010 Nokia Corporation
5 *
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7 *
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.
11 *
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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 *
22 */
23
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>
30
31#include "wlcore.h"
32#include "debug.h"
33#include "io.h"
34#include "acx.h"
35#include "wl12xx_80211.h"
36#include "cmd.h"
37#include "event.h"
38#include "tx.h"
39#include "hw_ops.h"
40
41#define WL1271_CMD_FAST_POLL_COUNT       50
42#define WL1271_WAIT_EVENT_FAST_POLL_COUNT 20
43
44/*
45 * send command to firmware
46 *
47 * @wl: wl struct
48 * @id: command id
49 * @buf: buffer containing the command, must work with dma
50 * @len: length of the buffer
51 * return the cmd status code on success.
52 */
53static int __wlcore_cmd_send(struct wl1271 *wl, u16 id, void *buf,
54			     size_t len, size_t res_len)
55{
56	struct wl1271_cmd_header *cmd;
57	unsigned long timeout;
58	u32 intr;
59	int ret;
60	u16 status;
61	u16 poll_count = 0;
62
63	if (unlikely(wl->state == WLCORE_STATE_RESTARTING &&
64		     id != CMD_STOP_FWLOGGER))
65		return -EIO;
66
67	cmd = buf;
68	cmd->id = cpu_to_le16(id);
69	cmd->status = 0;
70
71	WARN_ON(len % 4 != 0);
72	WARN_ON(test_bit(WL1271_FLAG_IN_ELP, &wl->flags));
73
74	ret = wlcore_write(wl, wl->cmd_box_addr, buf, len, false);
75	if (ret < 0)
76		return ret;
77
78	/*
79	 * TODO: we just need this because one bit is in a different
80	 * place.  Is there any better way?
81	 */
82	ret = wl->ops->trigger_cmd(wl, wl->cmd_box_addr, buf, len);
83	if (ret < 0)
84		return ret;
85
86	timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
87
88	ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
89	if (ret < 0)
90		return ret;
91
92	while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
93		if (time_after(jiffies, timeout)) {
94			wl1271_error("command complete timeout");
95			return -ETIMEDOUT;
96		}
97
98		poll_count++;
99		if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
100			udelay(10);
101		else
102			msleep(1);
103
104		ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
105		if (ret < 0)
106			return ret;
107	}
108
109	/* read back the status code of the command */
110	if (res_len == 0)
111		res_len = sizeof(struct wl1271_cmd_header);
112
113	ret = wlcore_read(wl, wl->cmd_box_addr, cmd, res_len, false);
114	if (ret < 0)
115		return ret;
116
117	status = le16_to_cpu(cmd->status);
118
119	ret = wlcore_write_reg(wl, REG_INTERRUPT_ACK,
120			       WL1271_ACX_INTR_CMD_COMPLETE);
121	if (ret < 0)
122		return ret;
123
124	return status;
125}
126
127/*
128 * send command to fw and return cmd status on success
129 * valid_rets contains a bitmap of allowed error codes
130 */
131int wlcore_cmd_send_failsafe(struct wl1271 *wl, u16 id, void *buf, size_t len,
132			     size_t res_len, unsigned long valid_rets)
133{
134	int ret = __wlcore_cmd_send(wl, id, buf, len, res_len);
135
136	if (ret < 0)
137		goto fail;
138
139	/* success is always a valid status */
140	valid_rets |= BIT(CMD_STATUS_SUCCESS);
141
142	if (ret >= MAX_COMMAND_STATUS ||
143	    !test_bit(ret, &valid_rets)) {
144		wl1271_error("command execute failure %d", ret);
145		ret = -EIO;
146		goto fail;
147	}
148	return ret;
149fail:
150	wl12xx_queue_recovery_work(wl);
151	return ret;
152}
153EXPORT_SYMBOL_GPL(wl1271_cmd_send);
154
155/*
156 * wrapper for wlcore_cmd_send that accept only CMD_STATUS_SUCCESS
157 * return 0 on success.
158 */
159int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
160		    size_t res_len)
161{
162	int ret = wlcore_cmd_send_failsafe(wl, id, buf, len, res_len, 0);
163
164	if (ret < 0)
165		return ret;
166	return 0;
167}
168
169/*
170 * Poll the mailbox event field until any of the bits in the mask is set or a
171 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
172 */
173int wlcore_cmd_wait_for_event_or_timeout(struct wl1271 *wl,
174					 u32 mask, bool *timeout)
175{
176	u32 *events_vector;
177	u32 event;
178	unsigned long timeout_time;
179	u16 poll_count = 0;
180	int ret = 0;
181
182	*timeout = false;
183
184	events_vector = kmalloc(sizeof(*events_vector), GFP_KERNEL | GFP_DMA);
185	if (!events_vector)
186		return -ENOMEM;
187
188	timeout_time = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
189
190	do {
191		if (time_after(jiffies, timeout_time)) {
192			wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
193				     (int)mask);
194			*timeout = true;
195			goto out;
196		}
197
198		poll_count++;
199		if (poll_count < WL1271_WAIT_EVENT_FAST_POLL_COUNT)
200			usleep_range(50, 51);
201		else
202			usleep_range(1000, 5000);
203
204		/* read from both event fields */
205		ret = wlcore_read(wl, wl->mbox_ptr[0], events_vector,
206				  sizeof(*events_vector), false);
207		if (ret < 0)
208			goto out;
209
210		event = *events_vector & mask;
211
212		ret = wlcore_read(wl, wl->mbox_ptr[1], events_vector,
213				  sizeof(*events_vector), false);
214		if (ret < 0)
215			goto out;
216
217		event |= *events_vector & mask;
218	} while (!event);
219
220out:
221	kfree(events_vector);
222	return ret;
223}
224EXPORT_SYMBOL_GPL(wlcore_cmd_wait_for_event_or_timeout);
225
226int wl12xx_cmd_role_enable(struct wl1271 *wl, u8 *addr, u8 role_type,
227			   u8 *role_id)
228{
229	struct wl12xx_cmd_role_enable *cmd;
230	int ret;
231
232	wl1271_debug(DEBUG_CMD, "cmd role enable");
233
234	if (WARN_ON(*role_id != WL12XX_INVALID_ROLE_ID))
235		return -EBUSY;
236
237	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
238	if (!cmd) {
239		ret = -ENOMEM;
240		goto out;
241	}
242
243	/* get role id */
244	cmd->role_id = find_first_zero_bit(wl->roles_map, WL12XX_MAX_ROLES);
245	if (cmd->role_id >= WL12XX_MAX_ROLES) {
246		ret = -EBUSY;
247		goto out_free;
248	}
249
250	memcpy(cmd->mac_address, addr, ETH_ALEN);
251	cmd->role_type = role_type;
252
253	ret = wl1271_cmd_send(wl, CMD_ROLE_ENABLE, cmd, sizeof(*cmd), 0);
254	if (ret < 0) {
255		wl1271_error("failed to initiate cmd role enable");
256		goto out_free;
257	}
258
259	__set_bit(cmd->role_id, wl->roles_map);
260	*role_id = cmd->role_id;
261
262out_free:
263	kfree(cmd);
264
265out:
266	return ret;
267}
268
269int wl12xx_cmd_role_disable(struct wl1271 *wl, u8 *role_id)
270{
271	struct wl12xx_cmd_role_disable *cmd;
272	int ret;
273
274	wl1271_debug(DEBUG_CMD, "cmd role disable");
275
276	if (WARN_ON(*role_id == WL12XX_INVALID_ROLE_ID))
277		return -ENOENT;
278
279	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
280	if (!cmd) {
281		ret = -ENOMEM;
282		goto out;
283	}
284	cmd->role_id = *role_id;
285
286	ret = wl1271_cmd_send(wl, CMD_ROLE_DISABLE, cmd, sizeof(*cmd), 0);
287	if (ret < 0) {
288		wl1271_error("failed to initiate cmd role disable");
289		goto out_free;
290	}
291
292	__clear_bit(*role_id, wl->roles_map);
293	*role_id = WL12XX_INVALID_ROLE_ID;
294
295out_free:
296	kfree(cmd);
297
298out:
299	return ret;
300}
301
302static int wlcore_get_new_session_id(struct wl1271 *wl, u8 hlid)
303{
304	if (wl->session_ids[hlid] >= SESSION_COUNTER_MAX)
305		wl->session_ids[hlid] = 0;
306
307	wl->session_ids[hlid]++;
308
309	return wl->session_ids[hlid];
310}
311
312int wl12xx_allocate_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
313{
314	unsigned long flags;
315	u8 link = find_first_zero_bit(wl->links_map, wl->num_links);
316	if (link >= wl->num_links)
317		return -EBUSY;
318
319	wl->session_ids[link] = wlcore_get_new_session_id(wl, link);
320
321	/* these bits are used by op_tx */
322	spin_lock_irqsave(&wl->wl_lock, flags);
323	__set_bit(link, wl->links_map);
324	__set_bit(link, wlvif->links_map);
325	spin_unlock_irqrestore(&wl->wl_lock, flags);
326
327	/*
328	 * take the last "freed packets" value from the current FW status.
329	 * on recovery, we might not have fw_status yet, and
330	 * tx_lnk_free_pkts will be NULL. check for it.
331	 */
332	if (wl->fw_status->counters.tx_lnk_free_pkts)
333		wl->links[link].prev_freed_pkts =
334			wl->fw_status->counters.tx_lnk_free_pkts[link];
335	wl->links[link].wlvif = wlvif;
336
337	/*
338	 * Take saved value for total freed packets from wlvif, in case this is
339	 * recovery/resume
340	 */
341	if (wlvif->bss_type != BSS_TYPE_AP_BSS)
342		wl->links[link].total_freed_pkts = wlvif->total_freed_pkts;
343
344	*hlid = link;
345
346	wl->active_link_count++;
347	return 0;
348}
349
350void wl12xx_free_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
351{
352	unsigned long flags;
353
354	if (*hlid == WL12XX_INVALID_LINK_ID)
355		return;
356
357	/* these bits are used by op_tx */
358	spin_lock_irqsave(&wl->wl_lock, flags);
359	__clear_bit(*hlid, wl->links_map);
360	__clear_bit(*hlid, wlvif->links_map);
361	spin_unlock_irqrestore(&wl->wl_lock, flags);
362
363	wl->links[*hlid].allocated_pkts = 0;
364	wl->links[*hlid].prev_freed_pkts = 0;
365	wl->links[*hlid].ba_bitmap = 0;
366	memset(wl->links[*hlid].addr, 0, ETH_ALEN);
367
368	/*
369	 * At this point op_tx() will not add more packets to the queues. We
370	 * can purge them.
371	 */
372	wl1271_tx_reset_link_queues(wl, *hlid);
373	wl->links[*hlid].wlvif = NULL;
374
375	if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
376	    (wlvif->bss_type == BSS_TYPE_AP_BSS &&
377	     *hlid == wlvif->ap.bcast_hlid)) {
378		/*
379		 * save the total freed packets in the wlvif, in case this is
380		 * recovery or suspend
381		 */
382		wlvif->total_freed_pkts = wl->links[*hlid].total_freed_pkts;
383
384		/*
385		 * increment the initial seq number on recovery to account for
386		 * transmitted packets that we haven't yet got in the FW status
387		 */
388		if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
389			wlvif->total_freed_pkts +=
390					WL1271_TX_SQN_POST_RECOVERY_PADDING;
391	}
392
393	wl->links[*hlid].total_freed_pkts = 0;
394
395	*hlid = WL12XX_INVALID_LINK_ID;
396	wl->active_link_count--;
397	WARN_ON_ONCE(wl->active_link_count < 0);
398}
399
400static u8 wlcore_get_native_channel_type(u8 nl_channel_type)
401{
402	switch (nl_channel_type) {
403	case NL80211_CHAN_NO_HT:
404		return WLCORE_CHAN_NO_HT;
405	case NL80211_CHAN_HT20:
406		return WLCORE_CHAN_HT20;
407	case NL80211_CHAN_HT40MINUS:
408		return WLCORE_CHAN_HT40MINUS;
409	case NL80211_CHAN_HT40PLUS:
410		return WLCORE_CHAN_HT40PLUS;
411	default:
412		WARN_ON(1);
413		return WLCORE_CHAN_NO_HT;
414	}
415}
416
417static int wl12xx_cmd_role_start_dev(struct wl1271 *wl,
418				     struct wl12xx_vif *wlvif,
419				     enum ieee80211_band band,
420				     int channel)
421{
422	struct wl12xx_cmd_role_start *cmd;
423	int ret;
424
425	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
426	if (!cmd) {
427		ret = -ENOMEM;
428		goto out;
429	}
430
431	wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wlvif->dev_role_id);
432
433	cmd->role_id = wlvif->dev_role_id;
434	if (band == IEEE80211_BAND_5GHZ)
435		cmd->band = WLCORE_BAND_5GHZ;
436	cmd->channel = channel;
437
438	if (wlvif->dev_hlid == WL12XX_INVALID_LINK_ID) {
439		ret = wl12xx_allocate_link(wl, wlvif, &wlvif->dev_hlid);
440		if (ret)
441			goto out_free;
442	}
443	cmd->device.hlid = wlvif->dev_hlid;
444	cmd->device.session = wl->session_ids[wlvif->dev_hlid];
445
446	wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d",
447		     cmd->role_id, cmd->device.hlid, cmd->device.session);
448
449	ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
450	if (ret < 0) {
451		wl1271_error("failed to initiate cmd role enable");
452		goto err_hlid;
453	}
454
455	goto out_free;
456
457err_hlid:
458	/* clear links on error */
459	wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
460
461out_free:
462	kfree(cmd);
463
464out:
465	return ret;
466}
467
468static int wl12xx_cmd_role_stop_dev(struct wl1271 *wl,
469				    struct wl12xx_vif *wlvif)
470{
471	struct wl12xx_cmd_role_stop *cmd;
472	int ret;
473
474	if (WARN_ON(wlvif->dev_hlid == WL12XX_INVALID_LINK_ID))
475		return -EINVAL;
476
477	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
478	if (!cmd) {
479		ret = -ENOMEM;
480		goto out;
481	}
482
483	wl1271_debug(DEBUG_CMD, "cmd role stop dev");
484
485	cmd->role_id = wlvif->dev_role_id;
486	cmd->disc_type = DISCONNECT_IMMEDIATE;
487	cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
488
489	ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
490	if (ret < 0) {
491		wl1271_error("failed to initiate cmd role stop");
492		goto out_free;
493	}
494
495	wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
496
497out_free:
498	kfree(cmd);
499
500out:
501	return ret;
502}
503
504int wl12xx_cmd_role_start_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
505{
506	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
507	struct wl12xx_cmd_role_start *cmd;
508	u32 supported_rates;
509	int ret;
510
511	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
512	if (!cmd) {
513		ret = -ENOMEM;
514		goto out;
515	}
516
517	wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wlvif->role_id);
518
519	cmd->role_id = wlvif->role_id;
520	if (wlvif->band == IEEE80211_BAND_5GHZ)
521		cmd->band = WLCORE_BAND_5GHZ;
522	cmd->channel = wlvif->channel;
523	cmd->sta.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
524	cmd->sta.beacon_interval = cpu_to_le16(wlvif->beacon_int);
525	cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY;
526	cmd->sta.ssid_len = wlvif->ssid_len;
527	memcpy(cmd->sta.ssid, wlvif->ssid, wlvif->ssid_len);
528	memcpy(cmd->sta.bssid, vif->bss_conf.bssid, ETH_ALEN);
529
530	supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES |
531			  wlcore_hw_sta_get_ap_rate_mask(wl, wlvif);
532	if (wlvif->p2p)
533		supported_rates &= ~CONF_TX_CCK_RATES;
534
535	cmd->sta.local_rates = cpu_to_le32(supported_rates);
536
537	cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
538
539	if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
540		ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
541		if (ret)
542			goto out_free;
543	}
544	cmd->sta.hlid = wlvif->sta.hlid;
545	cmd->sta.session = wl->session_ids[wlvif->sta.hlid];
546	/*
547	 * We don't have the correct remote rates in this stage.  The
548	 * rates will be reconfigured later, after association, if the
549	 * firmware supports ACX_PEER_CAP.  Otherwise, there's nothing
550	 * we can do, so use all supported_rates here.
551	 */
552	cmd->sta.remote_rates = cpu_to_le32(supported_rates);
553
554	wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
555		     "basic_rate_set: 0x%x, remote_rates: 0x%x",
556		     wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
557		     wlvif->basic_rate_set, wlvif->rate_set);
558
559	ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
560	if (ret < 0) {
561		wl1271_error("failed to initiate cmd role start sta");
562		goto err_hlid;
563	}
564
565	wlvif->sta.role_chan_type = wlvif->channel_type;
566	goto out_free;
567
568err_hlid:
569	/* clear links on error. */
570	wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
571
572out_free:
573	kfree(cmd);
574
575out:
576	return ret;
577}
578
579/* use this function to stop ibss as well */
580int wl12xx_cmd_role_stop_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
581{
582	struct wl12xx_cmd_role_stop *cmd;
583	int ret;
584
585	if (WARN_ON(wlvif->sta.hlid == WL12XX_INVALID_LINK_ID))
586		return -EINVAL;
587
588	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
589	if (!cmd) {
590		ret = -ENOMEM;
591		goto out;
592	}
593
594	wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wlvif->role_id);
595
596	cmd->role_id = wlvif->role_id;
597	cmd->disc_type = DISCONNECT_IMMEDIATE;
598	cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
599
600	ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
601	if (ret < 0) {
602		wl1271_error("failed to initiate cmd role stop sta");
603		goto out_free;
604	}
605
606	wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
607
608out_free:
609	kfree(cmd);
610
611out:
612	return ret;
613}
614
615int wl12xx_cmd_role_start_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
616{
617	struct wl12xx_cmd_role_start *cmd;
618	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
619	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
620	u32 supported_rates;
621	int ret;
622
623	wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wlvif->role_id);
624
625	/* trying to use hidden SSID with an old hostapd version */
626	if (wlvif->ssid_len == 0 && !bss_conf->hidden_ssid) {
627		wl1271_error("got a null SSID from beacon/bss");
628		ret = -EINVAL;
629		goto out;
630	}
631
632	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
633	if (!cmd) {
634		ret = -ENOMEM;
635		goto out;
636	}
637
638	ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.global_hlid);
639	if (ret < 0)
640		goto out_free;
641
642	ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.bcast_hlid);
643	if (ret < 0)
644		goto out_free_global;
645
646	/* use the previous security seq, if this is a recovery/resume */
647	wl->links[wlvif->ap.bcast_hlid].total_freed_pkts =
648						wlvif->total_freed_pkts;
649
650	cmd->role_id = wlvif->role_id;
651	cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
652	cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
653	cmd->ap.global_hlid = wlvif->ap.global_hlid;
654	cmd->ap.broadcast_hlid = wlvif->ap.bcast_hlid;
655	cmd->ap.global_session_id = wl->session_ids[wlvif->ap.global_hlid];
656	cmd->ap.bcast_session_id = wl->session_ids[wlvif->ap.bcast_hlid];
657	cmd->ap.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
658	cmd->ap.beacon_interval = cpu_to_le16(wlvif->beacon_int);
659	cmd->ap.dtim_interval = bss_conf->dtim_period;
660	cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
661	/* FIXME: Change when adding DFS */
662	cmd->ap.reset_tsf = 1;  /* By default reset AP TSF */
663	cmd->ap.wmm = wlvif->wmm_enabled;
664	cmd->channel = wlvif->channel;
665	cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
666
667	if (!bss_conf->hidden_ssid) {
668		/* take the SSID from the beacon for backward compatibility */
669		cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
670		cmd->ap.ssid_len = wlvif->ssid_len;
671		memcpy(cmd->ap.ssid, wlvif->ssid, wlvif->ssid_len);
672	} else {
673		cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
674		cmd->ap.ssid_len = bss_conf->ssid_len;
675		memcpy(cmd->ap.ssid, bss_conf->ssid, bss_conf->ssid_len);
676	}
677
678	supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES |
679		wlcore_hw_ap_get_mimo_wide_rate_mask(wl, wlvif);
680	if (wlvif->p2p)
681		supported_rates &= ~CONF_TX_CCK_RATES;
682
683	wl1271_debug(DEBUG_CMD, "cmd role start ap with supported_rates 0x%08x",
684		     supported_rates);
685
686	cmd->ap.local_rates = cpu_to_le32(supported_rates);
687
688	switch (wlvif->band) {
689	case IEEE80211_BAND_2GHZ:
690		cmd->band = WLCORE_BAND_2_4GHZ;
691		break;
692	case IEEE80211_BAND_5GHZ:
693		cmd->band = WLCORE_BAND_5GHZ;
694		break;
695	default:
696		wl1271_warning("ap start - unknown band: %d", (int)wlvif->band);
697		cmd->band = WLCORE_BAND_2_4GHZ;
698		break;
699	}
700
701	ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
702	if (ret < 0) {
703		wl1271_error("failed to initiate cmd role start ap");
704		goto out_free_bcast;
705	}
706
707	goto out_free;
708
709out_free_bcast:
710	wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
711
712out_free_global:
713	wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
714
715out_free:
716	kfree(cmd);
717
718out:
719	return ret;
720}
721
722int wl12xx_cmd_role_stop_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
723{
724	struct wl12xx_cmd_role_stop *cmd;
725	int ret;
726
727	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
728	if (!cmd) {
729		ret = -ENOMEM;
730		goto out;
731	}
732
733	wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wlvif->role_id);
734
735	cmd->role_id = wlvif->role_id;
736
737	ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
738	if (ret < 0) {
739		wl1271_error("failed to initiate cmd role stop ap");
740		goto out_free;
741	}
742
743	wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
744	wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
745
746out_free:
747	kfree(cmd);
748
749out:
750	return ret;
751}
752
753int wl12xx_cmd_role_start_ibss(struct wl1271 *wl, struct wl12xx_vif *wlvif)
754{
755	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
756	struct wl12xx_cmd_role_start *cmd;
757	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
758	int ret;
759
760	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
761	if (!cmd) {
762		ret = -ENOMEM;
763		goto out;
764	}
765
766	wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wlvif->role_id);
767
768	cmd->role_id = wlvif->role_id;
769	if (wlvif->band == IEEE80211_BAND_5GHZ)
770		cmd->band = WLCORE_BAND_5GHZ;
771	cmd->channel = wlvif->channel;
772	cmd->ibss.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
773	cmd->ibss.beacon_interval = cpu_to_le16(wlvif->beacon_int);
774	cmd->ibss.dtim_interval = bss_conf->dtim_period;
775	cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
776	cmd->ibss.ssid_len = wlvif->ssid_len;
777	memcpy(cmd->ibss.ssid, wlvif->ssid, wlvif->ssid_len);
778	memcpy(cmd->ibss.bssid, vif->bss_conf.bssid, ETH_ALEN);
779	cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
780
781	if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
782		ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
783		if (ret)
784			goto out_free;
785	}
786	cmd->ibss.hlid = wlvif->sta.hlid;
787	cmd->ibss.remote_rates = cpu_to_le32(wlvif->rate_set);
788
789	wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
790		     "basic_rate_set: 0x%x, remote_rates: 0x%x",
791		     wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
792		     wlvif->basic_rate_set, wlvif->rate_set);
793
794	wl1271_debug(DEBUG_CMD, "vif->bss_conf.bssid = %pM",
795		     vif->bss_conf.bssid);
796
797	ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
798	if (ret < 0) {
799		wl1271_error("failed to initiate cmd role enable");
800		goto err_hlid;
801	}
802
803	goto out_free;
804
805err_hlid:
806	/* clear links on error. */
807	wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
808
809out_free:
810	kfree(cmd);
811
812out:
813	return ret;
814}
815
816
817/**
818 * send test command to firmware
819 *
820 * @wl: wl struct
821 * @buf: buffer containing the command, with all headers, must work with dma
822 * @len: length of the buffer
823 * @answer: is answer needed
824 */
825int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
826{
827	int ret;
828	size_t res_len = 0;
829
830	wl1271_debug(DEBUG_CMD, "cmd test");
831
832	if (answer)
833		res_len = buf_len;
834
835	ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
836
837	if (ret < 0) {
838		wl1271_warning("TEST command failed");
839		return ret;
840	}
841
842	return ret;
843}
844EXPORT_SYMBOL_GPL(wl1271_cmd_test);
845
846/**
847 * read acx from firmware
848 *
849 * @wl: wl struct
850 * @id: acx id
851 * @buf: buffer for the response, including all headers, must work with dma
852 * @len: length of buf
853 */
854int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf,
855			   size_t cmd_len, size_t res_len)
856{
857	struct acx_header *acx = buf;
858	int ret;
859
860	wl1271_debug(DEBUG_CMD, "cmd interrogate");
861
862	acx->id = cpu_to_le16(id);
863
864	/* response payload length, does not include any headers */
865	acx->len = cpu_to_le16(res_len - sizeof(*acx));
866
867	ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, cmd_len, res_len);
868	if (ret < 0)
869		wl1271_error("INTERROGATE command failed");
870
871	return ret;
872}
873
874/**
875 * write acx value to firmware
876 *
877 * @wl: wl struct
878 * @id: acx id
879 * @buf: buffer containing acx, including all headers, must work with dma
880 * @len: length of buf
881 * @valid_rets: bitmap of valid cmd status codes (i.e. return values).
882 * return the cmd status on success.
883 */
884int wlcore_cmd_configure_failsafe(struct wl1271 *wl, u16 id, void *buf,
885				  size_t len, unsigned long valid_rets)
886{
887	struct acx_header *acx = buf;
888	int ret;
889
890	wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
891
892	acx->id = cpu_to_le16(id);
893
894	/* payload length, does not include any headers */
895	acx->len = cpu_to_le16(len - sizeof(*acx));
896
897	ret = wlcore_cmd_send_failsafe(wl, CMD_CONFIGURE, acx, len, 0,
898				       valid_rets);
899	if (ret < 0) {
900		wl1271_warning("CONFIGURE command NOK");
901		return ret;
902	}
903
904	return ret;
905}
906
907/*
908 * wrapper for wlcore_cmd_configure that accepts only success status.
909 * return 0 on success
910 */
911int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
912{
913	int ret = wlcore_cmd_configure_failsafe(wl, id, buf, len, 0);
914
915	if (ret < 0)
916		return ret;
917	return 0;
918}
919EXPORT_SYMBOL_GPL(wl1271_cmd_configure);
920
921int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
922{
923	struct cmd_enabledisable_path *cmd;
924	int ret;
925	u16 cmd_rx, cmd_tx;
926
927	wl1271_debug(DEBUG_CMD, "cmd data path");
928
929	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
930	if (!cmd) {
931		ret = -ENOMEM;
932		goto out;
933	}
934
935	/* the channel here is only used for calibration, so hardcoded to 1 */
936	cmd->channel = 1;
937
938	if (enable) {
939		cmd_rx = CMD_ENABLE_RX;
940		cmd_tx = CMD_ENABLE_TX;
941	} else {
942		cmd_rx = CMD_DISABLE_RX;
943		cmd_tx = CMD_DISABLE_TX;
944	}
945
946	ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
947	if (ret < 0) {
948		wl1271_error("rx %s cmd for channel %d failed",
949			     enable ? "start" : "stop", cmd->channel);
950		goto out;
951	}
952
953	wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
954		     enable ? "start" : "stop", cmd->channel);
955
956	ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
957	if (ret < 0) {
958		wl1271_error("tx %s cmd for channel %d failed",
959			     enable ? "start" : "stop", cmd->channel);
960		goto out;
961	}
962
963	wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
964		     enable ? "start" : "stop", cmd->channel);
965
966out:
967	kfree(cmd);
968	return ret;
969}
970EXPORT_SYMBOL_GPL(wl1271_cmd_data_path);
971
972int wl1271_cmd_ps_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
973		       u8 ps_mode, u16 auto_ps_timeout)
974{
975	struct wl1271_cmd_ps_params *ps_params = NULL;
976	int ret = 0;
977
978	wl1271_debug(DEBUG_CMD, "cmd set ps mode");
979
980	ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
981	if (!ps_params) {
982		ret = -ENOMEM;
983		goto out;
984	}
985
986	ps_params->role_id = wlvif->role_id;
987	ps_params->ps_mode = ps_mode;
988	ps_params->auto_ps_timeout = auto_ps_timeout;
989
990	ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
991			      sizeof(*ps_params), 0);
992	if (ret < 0) {
993		wl1271_error("cmd set_ps_mode failed");
994		goto out;
995	}
996
997out:
998	kfree(ps_params);
999	return ret;
1000}
1001
1002int wl1271_cmd_template_set(struct wl1271 *wl, u8 role_id,
1003			    u16 template_id, void *buf, size_t buf_len,
1004			    int index, u32 rates)
1005{
1006	struct wl1271_cmd_template_set *cmd;
1007	int ret = 0;
1008
1009	wl1271_debug(DEBUG_CMD, "cmd template_set %d (role %d)",
1010		     template_id, role_id);
1011
1012	WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
1013	buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
1014
1015	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1016	if (!cmd) {
1017		ret = -ENOMEM;
1018		goto out;
1019	}
1020
1021	/* during initialization wlvif is NULL */
1022	cmd->role_id = role_id;
1023	cmd->len = cpu_to_le16(buf_len);
1024	cmd->template_type = template_id;
1025	cmd->enabled_rates = cpu_to_le32(rates);
1026	cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
1027	cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
1028	cmd->index = index;
1029
1030	if (buf)
1031		memcpy(cmd->template_data, buf, buf_len);
1032
1033	ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
1034	if (ret < 0) {
1035		wl1271_warning("cmd set_template failed: %d", ret);
1036		goto out_free;
1037	}
1038
1039out_free:
1040	kfree(cmd);
1041
1042out:
1043	return ret;
1044}
1045
1046int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1047{
1048	struct sk_buff *skb = NULL;
1049	int size;
1050	void *ptr;
1051	int ret = -ENOMEM;
1052
1053
1054	if (wlvif->bss_type == BSS_TYPE_IBSS) {
1055		size = sizeof(struct wl12xx_null_data_template);
1056		ptr = NULL;
1057	} else {
1058		skb = ieee80211_nullfunc_get(wl->hw,
1059					     wl12xx_wlvif_to_vif(wlvif));
1060		if (!skb)
1061			goto out;
1062		size = skb->len;
1063		ptr = skb->data;
1064	}
1065
1066	ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1067				      CMD_TEMPL_NULL_DATA, ptr, size, 0,
1068				      wlvif->basic_rate);
1069
1070out:
1071	dev_kfree_skb(skb);
1072	if (ret)
1073		wl1271_warning("cmd buld null data failed %d", ret);
1074
1075	return ret;
1076
1077}
1078
1079int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl,
1080				   struct wl12xx_vif *wlvif)
1081{
1082	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1083	struct sk_buff *skb = NULL;
1084	int ret = -ENOMEM;
1085
1086	skb = ieee80211_nullfunc_get(wl->hw, vif);
1087	if (!skb)
1088		goto out;
1089
1090	ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_KLV,
1091				      skb->data, skb->len,
1092				      wlvif->sta.klv_template_id,
1093				      wlvif->basic_rate);
1094
1095out:
1096	dev_kfree_skb(skb);
1097	if (ret)
1098		wl1271_warning("cmd build klv null data failed %d", ret);
1099
1100	return ret;
1101
1102}
1103
1104int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1105			     u16 aid)
1106{
1107	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1108	struct sk_buff *skb;
1109	int ret = 0;
1110
1111	skb = ieee80211_pspoll_get(wl->hw, vif);
1112	if (!skb)
1113		goto out;
1114
1115	ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1116				      CMD_TEMPL_PS_POLL, skb->data,
1117				      skb->len, 0, wlvif->basic_rate_set);
1118
1119out:
1120	dev_kfree_skb(skb);
1121	return ret;
1122}
1123
1124int wl12xx_cmd_build_probe_req(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1125			       u8 role_id, u8 band,
1126			       const u8 *ssid, size_t ssid_len,
1127			       const u8 *ie0, size_t ie0_len, const u8 *ie1,
1128			       size_t ie1_len, bool sched_scan)
1129{
1130	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1131	struct sk_buff *skb;
1132	int ret;
1133	u32 rate;
1134	u16 template_id_2_4 = wl->scan_templ_id_2_4;
1135	u16 template_id_5 = wl->scan_templ_id_5;
1136
1137	wl1271_debug(DEBUG_SCAN, "build probe request band %d", band);
1138
1139	skb = ieee80211_probereq_get(wl->hw, vif, ssid, ssid_len,
1140				     ie0_len + ie1_len);
1141	if (!skb) {
1142		ret = -ENOMEM;
1143		goto out;
1144	}
1145	if (ie0_len)
1146		memcpy(skb_put(skb, ie0_len), ie0, ie0_len);
1147	if (ie1_len)
1148		memcpy(skb_put(skb, ie1_len), ie1, ie1_len);
1149
1150	if (sched_scan &&
1151	    (wl->quirks & WLCORE_QUIRK_DUAL_PROBE_TMPL)) {
1152		template_id_2_4 = wl->sched_scan_templ_id_2_4;
1153		template_id_5 = wl->sched_scan_templ_id_5;
1154	}
1155
1156	rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
1157	if (band == IEEE80211_BAND_2GHZ)
1158		ret = wl1271_cmd_template_set(wl, role_id,
1159					      template_id_2_4,
1160					      skb->data, skb->len, 0, rate);
1161	else
1162		ret = wl1271_cmd_template_set(wl, role_id,
1163					      template_id_5,
1164					      skb->data, skb->len, 0, rate);
1165
1166out:
1167	dev_kfree_skb(skb);
1168	return ret;
1169}
1170EXPORT_SYMBOL_GPL(wl12xx_cmd_build_probe_req);
1171
1172struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
1173					      struct wl12xx_vif *wlvif,
1174					      struct sk_buff *skb)
1175{
1176	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1177	int ret;
1178	u32 rate;
1179
1180	if (!skb)
1181		skb = ieee80211_ap_probereq_get(wl->hw, vif);
1182	if (!skb)
1183		goto out;
1184
1185	wl1271_debug(DEBUG_SCAN, "set ap probe request template");
1186
1187	rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[wlvif->band]);
1188	if (wlvif->band == IEEE80211_BAND_2GHZ)
1189		ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1190					      CMD_TEMPL_CFG_PROBE_REQ_2_4,
1191					      skb->data, skb->len, 0, rate);
1192	else
1193		ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1194					      CMD_TEMPL_CFG_PROBE_REQ_5,
1195					      skb->data, skb->len, 0, rate);
1196
1197	if (ret < 0)
1198		wl1271_error("Unable to set ap probe request template.");
1199
1200out:
1201	return skb;
1202}
1203
1204int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1205{
1206	int ret, extra = 0;
1207	u16 fc;
1208	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1209	struct sk_buff *skb;
1210	struct wl12xx_arp_rsp_template *tmpl;
1211	struct ieee80211_hdr_3addr *hdr;
1212	struct arphdr *arp_hdr;
1213
1214	skb = dev_alloc_skb(sizeof(*hdr) + sizeof(__le16) + sizeof(*tmpl) +
1215			    WL1271_EXTRA_SPACE_MAX);
1216	if (!skb) {
1217		wl1271_error("failed to allocate buffer for arp rsp template");
1218		return -ENOMEM;
1219	}
1220
1221	skb_reserve(skb, sizeof(*hdr) + WL1271_EXTRA_SPACE_MAX);
1222
1223	tmpl = (struct wl12xx_arp_rsp_template *)skb_put(skb, sizeof(*tmpl));
1224	memset(tmpl, 0, sizeof(*tmpl));
1225
1226	/* llc layer */
1227	memcpy(tmpl->llc_hdr, rfc1042_header, sizeof(rfc1042_header));
1228	tmpl->llc_type = cpu_to_be16(ETH_P_ARP);
1229
1230	/* arp header */
1231	arp_hdr = &tmpl->arp_hdr;
1232	arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
1233	arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
1234	arp_hdr->ar_hln = ETH_ALEN;
1235	arp_hdr->ar_pln = 4;
1236	arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
1237
1238	/* arp payload */
1239	memcpy(tmpl->sender_hw, vif->addr, ETH_ALEN);
1240	tmpl->sender_ip = wlvif->ip_addr;
1241
1242	/* encryption space */
1243	switch (wlvif->encryption_type) {
1244	case KEY_TKIP:
1245		if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
1246			extra = WL1271_EXTRA_SPACE_TKIP;
1247		break;
1248	case KEY_AES:
1249		extra = WL1271_EXTRA_SPACE_AES;
1250		break;
1251	case KEY_NONE:
1252	case KEY_WEP:
1253	case KEY_GEM:
1254		extra = 0;
1255		break;
1256	default:
1257		wl1271_warning("Unknown encryption type: %d",
1258			       wlvif->encryption_type);
1259		ret = -EINVAL;
1260		goto out;
1261	}
1262
1263	if (extra) {
1264		u8 *space = skb_push(skb, extra);
1265		memset(space, 0, extra);
1266	}
1267
1268	/* QoS header - BE */
1269	if (wlvif->sta.qos)
1270		memset(skb_push(skb, sizeof(__le16)), 0, sizeof(__le16));
1271
1272	/* mac80211 header */
1273	hdr = (struct ieee80211_hdr_3addr *)skb_push(skb, sizeof(*hdr));
1274	memset(hdr, 0, sizeof(*hdr));
1275	fc = IEEE80211_FTYPE_DATA | IEEE80211_FCTL_TODS;
1276	if (wlvif->sta.qos)
1277		fc |= IEEE80211_STYPE_QOS_DATA;
1278	else
1279		fc |= IEEE80211_STYPE_DATA;
1280	if (wlvif->encryption_type != KEY_NONE)
1281		fc |= IEEE80211_FCTL_PROTECTED;
1282
1283	hdr->frame_control = cpu_to_le16(fc);
1284	memcpy(hdr->addr1, vif->bss_conf.bssid, ETH_ALEN);
1285	memcpy(hdr->addr2, vif->addr, ETH_ALEN);
1286	memset(hdr->addr3, 0xff, ETH_ALEN);
1287
1288	ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_ARP_RSP,
1289				      skb->data, skb->len, 0,
1290				      wlvif->basic_rate);
1291out:
1292	dev_kfree_skb(skb);
1293	return ret;
1294}
1295
1296int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
1297{
1298	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
1299	struct ieee80211_qos_hdr template;
1300
1301	memset(&template, 0, sizeof(template));
1302
1303	memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN);
1304	memcpy(template.addr2, vif->addr, ETH_ALEN);
1305	memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN);
1306
1307	template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1308					     IEEE80211_STYPE_QOS_NULLFUNC |
1309					     IEEE80211_FCTL_TODS);
1310
1311	/* FIXME: not sure what priority to use here */
1312	template.qos_ctrl = cpu_to_le16(0);
1313
1314	return wl1271_cmd_template_set(wl, wlvif->role_id,
1315				       CMD_TEMPL_QOS_NULL_DATA, &template,
1316				       sizeof(template), 0,
1317				       wlvif->basic_rate);
1318}
1319
1320int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
1321{
1322	struct wl1271_cmd_set_keys *cmd;
1323	int ret = 0;
1324
1325	wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
1326
1327	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1328	if (!cmd) {
1329		ret = -ENOMEM;
1330		goto out;
1331	}
1332
1333	cmd->hlid = hlid;
1334	cmd->key_id = id;
1335	cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1336	cmd->key_action = cpu_to_le16(KEY_SET_ID);
1337	cmd->key_type = KEY_WEP;
1338
1339	ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1340	if (ret < 0) {
1341		wl1271_warning("cmd set_default_wep_key failed: %d", ret);
1342		goto out;
1343	}
1344
1345out:
1346	kfree(cmd);
1347
1348	return ret;
1349}
1350
1351int wl1271_cmd_set_sta_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1352		       u16 action, u8 id, u8 key_type,
1353		       u8 key_size, const u8 *key, const u8 *addr,
1354		       u32 tx_seq_32, u16 tx_seq_16)
1355{
1356	struct wl1271_cmd_set_keys *cmd;
1357	int ret = 0;
1358
1359	/* hlid might have already been deleted */
1360	if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
1361		return 0;
1362
1363	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1364	if (!cmd) {
1365		ret = -ENOMEM;
1366		goto out;
1367	}
1368
1369	cmd->hlid = wlvif->sta.hlid;
1370
1371	if (key_type == KEY_WEP)
1372		cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1373	else if (is_broadcast_ether_addr(addr))
1374		cmd->lid_key_type = BROADCAST_LID_TYPE;
1375	else
1376		cmd->lid_key_type = UNICAST_LID_TYPE;
1377
1378	cmd->key_action = cpu_to_le16(action);
1379	cmd->key_size = key_size;
1380	cmd->key_type = key_type;
1381
1382	cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1383	cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1384
1385	cmd->key_id = id;
1386
1387	if (key_type == KEY_TKIP) {
1388		/*
1389		 * We get the key in the following form:
1390		 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1391		 * but the target is expecting:
1392		 * TKIP - RX MIC - TX MIC
1393		 */
1394		memcpy(cmd->key, key, 16);
1395		memcpy(cmd->key + 16, key + 24, 8);
1396		memcpy(cmd->key + 24, key + 16, 8);
1397
1398	} else {
1399		memcpy(cmd->key, key, key_size);
1400	}
1401
1402	wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1403
1404	ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1405	if (ret < 0) {
1406		wl1271_warning("could not set keys");
1407	goto out;
1408	}
1409
1410out:
1411	kfree(cmd);
1412
1413	return ret;
1414}
1415
1416/*
1417 * TODO: merge with sta/ibss into 1 set_key function.
1418 * note there are slight diffs
1419 */
1420int wl1271_cmd_set_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1421			  u16 action, u8 id, u8 key_type,
1422			  u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
1423			  u16 tx_seq_16)
1424{
1425	struct wl1271_cmd_set_keys *cmd;
1426	int ret = 0;
1427	u8 lid_type;
1428
1429	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1430	if (!cmd)
1431		return -ENOMEM;
1432
1433	if (hlid == wlvif->ap.bcast_hlid) {
1434		if (key_type == KEY_WEP)
1435			lid_type = WEP_DEFAULT_LID_TYPE;
1436		else
1437			lid_type = BROADCAST_LID_TYPE;
1438	} else {
1439		lid_type = UNICAST_LID_TYPE;
1440	}
1441
1442	wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
1443		     " hlid: %d", (int)action, (int)id, (int)lid_type,
1444		     (int)key_type, (int)hlid);
1445
1446	cmd->lid_key_type = lid_type;
1447	cmd->hlid = hlid;
1448	cmd->key_action = cpu_to_le16(action);
1449	cmd->key_size = key_size;
1450	cmd->key_type = key_type;
1451	cmd->key_id = id;
1452	cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1453	cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1454
1455	if (key_type == KEY_TKIP) {
1456		/*
1457		 * We get the key in the following form:
1458		 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1459		 * but the target is expecting:
1460		 * TKIP - RX MIC - TX MIC
1461		 */
1462		memcpy(cmd->key, key, 16);
1463		memcpy(cmd->key + 16, key + 24, 8);
1464		memcpy(cmd->key + 24, key + 16, 8);
1465	} else {
1466		memcpy(cmd->key, key, key_size);
1467	}
1468
1469	wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
1470
1471	ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1472	if (ret < 0) {
1473		wl1271_warning("could not set ap keys");
1474		goto out;
1475	}
1476
1477out:
1478	kfree(cmd);
1479	return ret;
1480}
1481
1482int wl12xx_cmd_set_peer_state(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1483			      u8 hlid)
1484{
1485	struct wl12xx_cmd_set_peer_state *cmd;
1486	int ret = 0;
1487
1488	wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
1489
1490	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1491	if (!cmd) {
1492		ret = -ENOMEM;
1493		goto out;
1494	}
1495
1496	cmd->hlid = hlid;
1497	cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1498
1499	/* wmm param is valid only for station role */
1500	if (wlvif->bss_type == BSS_TYPE_STA_BSS)
1501		cmd->wmm = wlvif->wmm_enabled;
1502
1503	ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
1504	if (ret < 0) {
1505		wl1271_error("failed to send set peer state command");
1506		goto out_free;
1507	}
1508
1509out_free:
1510	kfree(cmd);
1511
1512out:
1513	return ret;
1514}
1515
1516int wl12xx_cmd_add_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1517			struct ieee80211_sta *sta, u8 hlid)
1518{
1519	struct wl12xx_cmd_add_peer *cmd;
1520	int i, ret;
1521	u32 sta_rates;
1522
1523	wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
1524
1525	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1526	if (!cmd) {
1527		ret = -ENOMEM;
1528		goto out;
1529	}
1530
1531	memcpy(cmd->addr, sta->addr, ETH_ALEN);
1532	cmd->bss_index = WL1271_AP_BSS_INDEX;
1533	cmd->aid = sta->aid;
1534	cmd->hlid = hlid;
1535	cmd->sp_len = sta->max_sp;
1536	cmd->wmm = sta->wme ? 1 : 0;
1537	cmd->session_id = wl->session_ids[hlid];
1538	cmd->role_id = wlvif->role_id;
1539
1540	for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
1541		if (sta->wme && (sta->uapsd_queues & BIT(i)))
1542			cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
1543					WL1271_PSD_UPSD_TRIGGER;
1544		else
1545			cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
1546					WL1271_PSD_LEGACY;
1547
1548
1549	sta_rates = sta->supp_rates[wlvif->band];
1550	if (sta->ht_cap.ht_supported)
1551		sta_rates |=
1552			(sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET) |
1553			(sta->ht_cap.mcs.rx_mask[1] << HW_MIMO_RATES_OFFSET);
1554
1555	cmd->supported_rates =
1556		cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
1557							wlvif->band));
1558
1559	wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
1560		     cmd->supported_rates, sta->uapsd_queues);
1561
1562	ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
1563	if (ret < 0) {
1564		wl1271_error("failed to initiate cmd add peer");
1565		goto out_free;
1566	}
1567
1568out_free:
1569	kfree(cmd);
1570
1571out:
1572	return ret;
1573}
1574
1575int wl12xx_cmd_remove_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1576			   u8 hlid)
1577{
1578	struct wl12xx_cmd_remove_peer *cmd;
1579	int ret;
1580	bool timeout = false;
1581
1582	wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
1583
1584	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1585	if (!cmd) {
1586		ret = -ENOMEM;
1587		goto out;
1588	}
1589
1590	cmd->hlid = hlid;
1591	/* We never send a deauth, mac80211 is in charge of this */
1592	cmd->reason_opcode = 0;
1593	cmd->send_deauth_flag = 0;
1594	cmd->role_id = wlvif->role_id;
1595
1596	ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
1597	if (ret < 0) {
1598		wl1271_error("failed to initiate cmd remove peer");
1599		goto out_free;
1600	}
1601
1602	ret = wl->ops->wait_for_event(wl,
1603				      WLCORE_EVENT_PEER_REMOVE_COMPLETE,
1604				      &timeout);
1605
1606	/*
1607	 * We are ok with a timeout here. The event is sometimes not sent
1608	 * due to a firmware bug. In case of another error (like SDIO timeout)
1609	 * queue a recovery.
1610	 */
1611	if (ret)
1612		wl12xx_queue_recovery_work(wl);
1613
1614out_free:
1615	kfree(cmd);
1616
1617out:
1618	return ret;
1619}
1620
1621static int wlcore_get_reg_conf_ch_idx(enum ieee80211_band band, u16 ch)
1622{
1623	/*
1624	 * map the given band/channel to the respective predefined
1625	 * bit expected by the fw
1626	 */
1627	switch (band) {
1628	case IEEE80211_BAND_2GHZ:
1629		/* channels 1..14 are mapped to 0..13 */
1630		if (ch >= 1 && ch <= 14)
1631			return ch - 1;
1632		break;
1633	case IEEE80211_BAND_5GHZ:
1634		switch (ch) {
1635		case 8 ... 16:
1636			/* channels 8,12,16 are mapped to 18,19,20 */
1637			return 18 + (ch-8)/4;
1638		case 34 ... 48:
1639			/* channels 34,36..48 are mapped to 21..28 */
1640			return 21 + (ch-34)/2;
1641		case 52 ... 64:
1642			/* channels 52,56..64 are mapped to 29..32 */
1643			return 29 + (ch-52)/4;
1644		case 100 ... 140:
1645			/* channels 100,104..140 are mapped to 33..43 */
1646			return 33 + (ch-100)/4;
1647		case 149 ... 165:
1648			/* channels 149,153..165 are mapped to 44..48 */
1649			return 44 + (ch-149)/4;
1650		default:
1651			break;
1652		}
1653		break;
1654	default:
1655		break;
1656	}
1657
1658	wl1271_error("%s: unknown band/channel: %d/%d", __func__, band, ch);
1659	return -1;
1660}
1661
1662void wlcore_set_pending_regdomain_ch(struct wl1271 *wl, u16 channel,
1663				     enum ieee80211_band band)
1664{
1665	int ch_bit_idx = 0;
1666
1667	if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
1668		return;
1669
1670	ch_bit_idx = wlcore_get_reg_conf_ch_idx(band, channel);
1671
1672	if (ch_bit_idx >= 0 && ch_bit_idx <= WL1271_MAX_CHANNELS)
1673		set_bit(ch_bit_idx, (long *)wl->reg_ch_conf_pending);
1674}
1675
1676int wlcore_cmd_regdomain_config_locked(struct wl1271 *wl)
1677{
1678	struct wl12xx_cmd_regdomain_dfs_config *cmd = NULL;
1679	int ret = 0, i, b, ch_bit_idx;
1680	struct ieee80211_channel *channel;
1681	u32 tmp_ch_bitmap[2];
1682	u16 ch;
1683	struct wiphy *wiphy = wl->hw->wiphy;
1684	struct ieee80211_supported_band *band;
1685	bool timeout = false;
1686
1687	if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
1688		return 0;
1689
1690	wl1271_debug(DEBUG_CMD, "cmd reg domain config");
1691
1692	memset(tmp_ch_bitmap, 0, sizeof(tmp_ch_bitmap));
1693
1694	for (b = IEEE80211_BAND_2GHZ; b <= IEEE80211_BAND_5GHZ; b++) {
1695		band = wiphy->bands[b];
1696		for (i = 0; i < band->n_channels; i++) {
1697			channel = &band->channels[i];
1698			ch = channel->hw_value;
1699
1700			if (channel->flags & (IEEE80211_CHAN_DISABLED |
1701					      IEEE80211_CHAN_RADAR |
1702					      IEEE80211_CHAN_NO_IR))
1703				continue;
1704
1705			ch_bit_idx = wlcore_get_reg_conf_ch_idx(b, ch);
1706			if (ch_bit_idx < 0)
1707				continue;
1708
1709			set_bit(ch_bit_idx, (long *)tmp_ch_bitmap);
1710		}
1711	}
1712
1713	tmp_ch_bitmap[0] |= wl->reg_ch_conf_pending[0];
1714	tmp_ch_bitmap[1] |= wl->reg_ch_conf_pending[1];
1715
1716	if (!memcmp(tmp_ch_bitmap, wl->reg_ch_conf_last, sizeof(tmp_ch_bitmap)))
1717		goto out;
1718
1719	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1720	if (!cmd) {
1721		ret = -ENOMEM;
1722		goto out;
1723	}
1724
1725	cmd->ch_bit_map1 = cpu_to_le32(tmp_ch_bitmap[0]);
1726	cmd->ch_bit_map2 = cpu_to_le32(tmp_ch_bitmap[1]);
1727
1728	wl1271_debug(DEBUG_CMD,
1729		     "cmd reg domain bitmap1: 0x%08x, bitmap2: 0x%08x",
1730		     cmd->ch_bit_map1, cmd->ch_bit_map2);
1731
1732	ret = wl1271_cmd_send(wl, CMD_DFS_CHANNEL_CONFIG, cmd, sizeof(*cmd), 0);
1733	if (ret < 0) {
1734		wl1271_error("failed to send reg domain dfs config");
1735		goto out;
1736	}
1737
1738	ret = wl->ops->wait_for_event(wl,
1739				      WLCORE_EVENT_DFS_CONFIG_COMPLETE,
1740				      &timeout);
1741	if (ret < 0 || timeout) {
1742		wl1271_error("reg domain conf %serror",
1743			     timeout ? "completion " : "");
1744		ret = timeout ? -ETIMEDOUT : ret;
1745		goto out;
1746	}
1747
1748	memcpy(wl->reg_ch_conf_last, tmp_ch_bitmap, sizeof(tmp_ch_bitmap));
1749	memset(wl->reg_ch_conf_pending, 0, sizeof(wl->reg_ch_conf_pending));
1750
1751out:
1752	kfree(cmd);
1753	return ret;
1754}
1755
1756int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
1757{
1758	struct wl12xx_cmd_config_fwlog *cmd;
1759	int ret = 0;
1760
1761	wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
1762
1763	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1764	if (!cmd) {
1765		ret = -ENOMEM;
1766		goto out;
1767	}
1768
1769	cmd->logger_mode = wl->conf.fwlog.mode;
1770	cmd->log_severity = wl->conf.fwlog.severity;
1771	cmd->timestamp = wl->conf.fwlog.timestamp;
1772	cmd->output = wl->conf.fwlog.output;
1773	cmd->threshold = wl->conf.fwlog.threshold;
1774
1775	ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
1776	if (ret < 0) {
1777		wl1271_error("failed to send config firmware logger command");
1778		goto out_free;
1779	}
1780
1781out_free:
1782	kfree(cmd);
1783
1784out:
1785	return ret;
1786}
1787
1788int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
1789{
1790	struct wl12xx_cmd_start_fwlog *cmd;
1791	int ret = 0;
1792
1793	wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
1794
1795	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1796	if (!cmd) {
1797		ret = -ENOMEM;
1798		goto out;
1799	}
1800
1801	ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
1802	if (ret < 0) {
1803		wl1271_error("failed to send start firmware logger command");
1804		goto out_free;
1805	}
1806
1807out_free:
1808	kfree(cmd);
1809
1810out:
1811	return ret;
1812}
1813
1814int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
1815{
1816	struct wl12xx_cmd_stop_fwlog *cmd;
1817	int ret = 0;
1818
1819	wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
1820
1821	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1822	if (!cmd) {
1823		ret = -ENOMEM;
1824		goto out;
1825	}
1826
1827	ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
1828	if (ret < 0) {
1829		wl1271_error("failed to send stop firmware logger command");
1830		goto out_free;
1831	}
1832
1833out_free:
1834	kfree(cmd);
1835
1836out:
1837	return ret;
1838}
1839
1840static int wl12xx_cmd_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1841			  u8 role_id, enum ieee80211_band band, u8 channel)
1842{
1843	struct wl12xx_cmd_roc *cmd;
1844	int ret = 0;
1845
1846	wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", channel, role_id);
1847
1848	if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
1849		return -EINVAL;
1850
1851	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1852	if (!cmd) {
1853		ret = -ENOMEM;
1854		goto out;
1855	}
1856
1857	cmd->role_id = role_id;
1858	cmd->channel = channel;
1859	switch (band) {
1860	case IEEE80211_BAND_2GHZ:
1861		cmd->band = WLCORE_BAND_2_4GHZ;
1862		break;
1863	case IEEE80211_BAND_5GHZ:
1864		cmd->band = WLCORE_BAND_5GHZ;
1865		break;
1866	default:
1867		wl1271_error("roc - unknown band: %d", (int)wlvif->band);
1868		ret = -EINVAL;
1869		goto out_free;
1870	}
1871
1872
1873	ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
1874	if (ret < 0) {
1875		wl1271_error("failed to send ROC command");
1876		goto out_free;
1877	}
1878
1879out_free:
1880	kfree(cmd);
1881
1882out:
1883	return ret;
1884}
1885
1886static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
1887{
1888	struct wl12xx_cmd_croc *cmd;
1889	int ret = 0;
1890
1891	wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
1892
1893	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1894	if (!cmd) {
1895		ret = -ENOMEM;
1896		goto out;
1897	}
1898	cmd->role_id = role_id;
1899
1900	ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
1901			      sizeof(*cmd), 0);
1902	if (ret < 0) {
1903		wl1271_error("failed to send ROC command");
1904		goto out_free;
1905	}
1906
1907out_free:
1908	kfree(cmd);
1909
1910out:
1911	return ret;
1912}
1913
1914int wl12xx_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 role_id,
1915	       enum ieee80211_band band, u8 channel)
1916{
1917	int ret = 0;
1918
1919	if (WARN_ON(test_bit(role_id, wl->roc_map)))
1920		return 0;
1921
1922	ret = wl12xx_cmd_roc(wl, wlvif, role_id, band, channel);
1923	if (ret < 0)
1924		goto out;
1925
1926	__set_bit(role_id, wl->roc_map);
1927out:
1928	return ret;
1929}
1930
1931int wl12xx_croc(struct wl1271 *wl, u8 role_id)
1932{
1933	int ret = 0;
1934
1935	if (WARN_ON(!test_bit(role_id, wl->roc_map)))
1936		return 0;
1937
1938	ret = wl12xx_cmd_croc(wl, role_id);
1939	if (ret < 0)
1940		goto out;
1941
1942	__clear_bit(role_id, wl->roc_map);
1943
1944	/*
1945	 * Rearm the tx watchdog when removing the last ROC. This prevents
1946	 * recoveries due to just finished ROCs - when Tx hasn't yet had
1947	 * a chance to get out.
1948	 */
1949	if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES)
1950		wl12xx_rearm_tx_watchdog_locked(wl);
1951out:
1952	return ret;
1953}
1954
1955int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1956{
1957	struct wl12xx_cmd_stop_channel_switch *cmd;
1958	int ret;
1959
1960	wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
1961
1962	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1963	if (!cmd) {
1964		ret = -ENOMEM;
1965		goto out;
1966	}
1967
1968	cmd->role_id = wlvif->role_id;
1969
1970	ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
1971	if (ret < 0) {
1972		wl1271_error("failed to stop channel switch command");
1973		goto out_free;
1974	}
1975
1976out_free:
1977	kfree(cmd);
1978
1979out:
1980	return ret;
1981}
1982
1983/* start dev role and roc on its channel */
1984int wl12xx_start_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1985		     enum ieee80211_band band, int channel)
1986{
1987	int ret;
1988
1989	if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
1990		      wlvif->bss_type == BSS_TYPE_IBSS)))
1991		return -EINVAL;
1992
1993	ret = wl12xx_cmd_role_enable(wl,
1994				     wl12xx_wlvif_to_vif(wlvif)->addr,
1995				     WL1271_ROLE_DEVICE,
1996				     &wlvif->dev_role_id);
1997	if (ret < 0)
1998		goto out;
1999
2000	ret = wl12xx_cmd_role_start_dev(wl, wlvif, band, channel);
2001	if (ret < 0)
2002		goto out_disable;
2003
2004	ret = wl12xx_roc(wl, wlvif, wlvif->dev_role_id, band, channel);
2005	if (ret < 0)
2006		goto out_stop;
2007
2008	return 0;
2009
2010out_stop:
2011	wl12xx_cmd_role_stop_dev(wl, wlvif);
2012out_disable:
2013	wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
2014out:
2015	return ret;
2016}
2017
2018/* croc dev hlid, and stop the role */
2019int wl12xx_stop_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2020{
2021	int ret;
2022
2023	if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
2024		      wlvif->bss_type == BSS_TYPE_IBSS)))
2025		return -EINVAL;
2026
2027	/* flush all pending packets */
2028	ret = wlcore_tx_work_locked(wl);
2029	if (ret < 0)
2030		goto out;
2031
2032	if (test_bit(wlvif->dev_role_id, wl->roc_map)) {
2033		ret = wl12xx_croc(wl, wlvif->dev_role_id);
2034		if (ret < 0)
2035			goto out;
2036	}
2037
2038	ret = wl12xx_cmd_role_stop_dev(wl, wlvif);
2039	if (ret < 0)
2040		goto out;
2041
2042	ret = wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
2043	if (ret < 0)
2044		goto out;
2045
2046out:
2047	return ret;
2048}
2049