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