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