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