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