wl12xx: Support routing FW logs to the host
[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 wl1271_cmd_join(struct wl1271 *wl, u8 bss_type)
367 {
368         struct wl1271_cmd_join *join;
369         int ret, i;
370         u8 *bssid;
371
372         join = kzalloc(sizeof(*join), GFP_KERNEL);
373         if (!join) {
374                 ret = -ENOMEM;
375                 goto out;
376         }
377
378         wl1271_debug(DEBUG_CMD, "cmd join");
379
380         /* Reverse order BSSID */
381         bssid = (u8 *) &join->bssid_lsb;
382         for (i = 0; i < ETH_ALEN; i++)
383                 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
384
385         join->rx_config_options = cpu_to_le32(wl->rx_config);
386         join->rx_filter_options = cpu_to_le32(wl->rx_filter);
387         join->bss_type = bss_type;
388         join->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
389         join->supported_rate_set = cpu_to_le32(wl->rate_set);
390
391         if (wl->band == IEEE80211_BAND_5GHZ)
392                 join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
393
394         join->beacon_interval = cpu_to_le16(wl->beacon_int);
395         join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
396
397         join->channel = wl->channel;
398         join->ssid_len = wl->ssid_len;
399         memcpy(join->ssid, wl->ssid, wl->ssid_len);
400
401         join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
402
403         /* reset TX security counters */
404         wl->tx_security_last_seq = 0;
405         wl->tx_security_seq = 0;
406
407         wl1271_debug(DEBUG_CMD, "cmd join: basic_rate_set=0x%x, rate_set=0x%x",
408                 join->basic_rate_set, join->supported_rate_set);
409
410         ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
411         if (ret < 0) {
412                 wl1271_error("failed to initiate cmd join");
413                 goto out_free;
414         }
415
416         ret = wl1271_cmd_wait_for_event(wl, JOIN_EVENT_COMPLETE_ID);
417         if (ret < 0)
418                 wl1271_error("cmd join event completion error");
419
420 out_free:
421         kfree(join);
422
423 out:
424         return ret;
425 }
426
427 /**
428  * send test command to firmware
429  *
430  * @wl: wl struct
431  * @buf: buffer containing the command, with all headers, must work with dma
432  * @len: length of the buffer
433  * @answer: is answer needed
434  */
435 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
436 {
437         int ret;
438         size_t res_len = 0;
439
440         wl1271_debug(DEBUG_CMD, "cmd test");
441
442         if (answer)
443                 res_len = buf_len;
444
445         ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
446
447         if (ret < 0) {
448                 wl1271_warning("TEST command failed");
449                 return ret;
450         }
451
452         return ret;
453 }
454
455 /**
456  * read acx from firmware
457  *
458  * @wl: wl struct
459  * @id: acx id
460  * @buf: buffer for the response, including all headers, must work with dma
461  * @len: length of buf
462  */
463 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
464 {
465         struct acx_header *acx = buf;
466         int ret;
467
468         wl1271_debug(DEBUG_CMD, "cmd interrogate");
469
470         acx->id = cpu_to_le16(id);
471
472         /* payload length, does not include any headers */
473         acx->len = cpu_to_le16(len - sizeof(*acx));
474
475         ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
476         if (ret < 0)
477                 wl1271_error("INTERROGATE command failed");
478
479         return ret;
480 }
481
482 /**
483  * write acx value to firmware
484  *
485  * @wl: wl struct
486  * @id: acx id
487  * @buf: buffer containing acx, including all headers, must work with dma
488  * @len: length of buf
489  */
490 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
491 {
492         struct acx_header *acx = buf;
493         int ret;
494
495         wl1271_debug(DEBUG_CMD, "cmd configure");
496
497         acx->id = cpu_to_le16(id);
498
499         /* payload length, does not include any headers */
500         acx->len = cpu_to_le16(len - sizeof(*acx));
501
502         ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
503         if (ret < 0) {
504                 wl1271_warning("CONFIGURE command NOK");
505                 return ret;
506         }
507
508         return 0;
509 }
510
511 int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
512 {
513         struct cmd_enabledisable_path *cmd;
514         int ret;
515         u16 cmd_rx, cmd_tx;
516
517         wl1271_debug(DEBUG_CMD, "cmd data path");
518
519         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
520         if (!cmd) {
521                 ret = -ENOMEM;
522                 goto out;
523         }
524
525         /* the channel here is only used for calibration, so hardcoded to 1 */
526         cmd->channel = 1;
527
528         if (enable) {
529                 cmd_rx = CMD_ENABLE_RX;
530                 cmd_tx = CMD_ENABLE_TX;
531         } else {
532                 cmd_rx = CMD_DISABLE_RX;
533                 cmd_tx = CMD_DISABLE_TX;
534         }
535
536         ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
537         if (ret < 0) {
538                 wl1271_error("rx %s cmd for channel %d failed",
539                              enable ? "start" : "stop", cmd->channel);
540                 goto out;
541         }
542
543         wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
544                      enable ? "start" : "stop", cmd->channel);
545
546         ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
547         if (ret < 0) {
548                 wl1271_error("tx %s cmd for channel %d failed",
549                              enable ? "start" : "stop", cmd->channel);
550                 goto out;
551         }
552
553         wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
554                      enable ? "start" : "stop", cmd->channel);
555
556 out:
557         kfree(cmd);
558         return ret;
559 }
560
561 int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
562 {
563         struct wl1271_cmd_ps_params *ps_params = NULL;
564         int ret = 0;
565
566         wl1271_debug(DEBUG_CMD, "cmd set ps mode");
567
568         ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
569         if (!ps_params) {
570                 ret = -ENOMEM;
571                 goto out;
572         }
573
574         ps_params->ps_mode = ps_mode;
575
576         ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
577                               sizeof(*ps_params), 0);
578         if (ret < 0) {
579                 wl1271_error("cmd set_ps_mode failed");
580                 goto out;
581         }
582
583 out:
584         kfree(ps_params);
585         return ret;
586 }
587
588 int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
589                             void *buf, size_t buf_len, int index, u32 rates)
590 {
591         struct wl1271_cmd_template_set *cmd;
592         int ret = 0;
593
594         wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
595
596         WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
597         buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
598
599         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
600         if (!cmd) {
601                 ret = -ENOMEM;
602                 goto out;
603         }
604
605         cmd->len = cpu_to_le16(buf_len);
606         cmd->template_type = template_id;
607         cmd->enabled_rates = cpu_to_le32(rates);
608         cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
609         cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
610         cmd->index = index;
611
612         if (buf)
613                 memcpy(cmd->template_data, buf, buf_len);
614
615         ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
616         if (ret < 0) {
617                 wl1271_warning("cmd set_template failed: %d", ret);
618                 goto out_free;
619         }
620
621 out_free:
622         kfree(cmd);
623
624 out:
625         return ret;
626 }
627
628 int wl1271_cmd_build_null_data(struct wl1271 *wl)
629 {
630         struct sk_buff *skb = NULL;
631         int size;
632         void *ptr;
633         int ret = -ENOMEM;
634
635
636         if (wl->bss_type == BSS_TYPE_IBSS) {
637                 size = sizeof(struct wl12xx_null_data_template);
638                 ptr = NULL;
639         } else {
640                 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
641                 if (!skb)
642                         goto out;
643                 size = skb->len;
644                 ptr = skb->data;
645         }
646
647         ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
648                                       wl->basic_rate);
649
650 out:
651         dev_kfree_skb(skb);
652         if (ret)
653                 wl1271_warning("cmd buld null data failed %d", ret);
654
655         return ret;
656
657 }
658
659 int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
660 {
661         struct sk_buff *skb = NULL;
662         int ret = -ENOMEM;
663
664         skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
665         if (!skb)
666                 goto out;
667
668         ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
669                                       skb->data, skb->len,
670                                       CMD_TEMPL_KLV_IDX_NULL_DATA,
671                                       wl->basic_rate);
672
673 out:
674         dev_kfree_skb(skb);
675         if (ret)
676                 wl1271_warning("cmd build klv null data failed %d", ret);
677
678         return ret;
679
680 }
681
682 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
683 {
684         struct sk_buff *skb;
685         int ret = 0;
686
687         skb = ieee80211_pspoll_get(wl->hw, wl->vif);
688         if (!skb)
689                 goto out;
690
691         ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
692                                       skb->len, 0, wl->basic_rate_set);
693
694 out:
695         dev_kfree_skb(skb);
696         return ret;
697 }
698
699 int wl1271_cmd_build_probe_req(struct wl1271 *wl,
700                                const u8 *ssid, size_t ssid_len,
701                                const u8 *ie, size_t ie_len, u8 band)
702 {
703         struct sk_buff *skb;
704         int ret;
705
706         skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
707                                      ie, ie_len);
708         if (!skb) {
709                 ret = -ENOMEM;
710                 goto out;
711         }
712
713         wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
714
715         if (band == IEEE80211_BAND_2GHZ)
716                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
717                                               skb->data, skb->len, 0,
718                                               wl->conf.tx.basic_rate);
719         else
720                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
721                                               skb->data, skb->len, 0,
722                                               wl->conf.tx.basic_rate_5);
723
724 out:
725         dev_kfree_skb(skb);
726         return ret;
727 }
728
729 struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
730                                               struct sk_buff *skb)
731 {
732         int ret;
733
734         if (!skb)
735                 skb = ieee80211_ap_probereq_get(wl->hw, wl->vif);
736         if (!skb)
737                 goto out;
738
739         wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
740
741         if (wl->band == IEEE80211_BAND_2GHZ)
742                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
743                                               skb->data, skb->len, 0,
744                                               wl->conf.tx.basic_rate);
745         else
746                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
747                                               skb->data, skb->len, 0,
748                                               wl->conf.tx.basic_rate_5);
749
750         if (ret < 0)
751                 wl1271_error("Unable to set ap probe request template.");
752
753 out:
754         return skb;
755 }
756
757 int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, __be32 ip_addr)
758 {
759         int ret;
760         struct wl12xx_arp_rsp_template tmpl;
761         struct ieee80211_hdr_3addr *hdr;
762         struct arphdr *arp_hdr;
763
764         memset(&tmpl, 0, sizeof(tmpl));
765
766         /* mac80211 header */
767         hdr = &tmpl.hdr;
768         hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
769                                          IEEE80211_STYPE_DATA |
770                                          IEEE80211_FCTL_TODS);
771         memcpy(hdr->addr1, wl->vif->bss_conf.bssid, ETH_ALEN);
772         memcpy(hdr->addr2, wl->vif->addr, ETH_ALEN);
773         memset(hdr->addr3, 0xff, ETH_ALEN);
774
775         /* llc layer */
776         memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
777         tmpl.llc_type = cpu_to_be16(ETH_P_ARP);
778
779         /* arp header */
780         arp_hdr = &tmpl.arp_hdr;
781         arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
782         arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
783         arp_hdr->ar_hln = ETH_ALEN;
784         arp_hdr->ar_pln = 4;
785         arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
786
787         /* arp payload */
788         memcpy(tmpl.sender_hw, wl->vif->addr, ETH_ALEN);
789         tmpl.sender_ip = ip_addr;
790
791         ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
792                                       &tmpl, sizeof(tmpl), 0,
793                                       wl->basic_rate);
794
795         return ret;
796 }
797
798 int wl1271_build_qos_null_data(struct wl1271 *wl)
799 {
800         struct ieee80211_qos_hdr template;
801
802         memset(&template, 0, sizeof(template));
803
804         memcpy(template.addr1, wl->bssid, ETH_ALEN);
805         memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
806         memcpy(template.addr3, wl->bssid, ETH_ALEN);
807
808         template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
809                                              IEEE80211_STYPE_QOS_NULLFUNC |
810                                              IEEE80211_FCTL_TODS);
811
812         /* FIXME: not sure what priority to use here */
813         template.qos_ctrl = cpu_to_le16(0);
814
815         return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
816                                        sizeof(template), 0,
817                                        wl->basic_rate);
818 }
819
820 int wl1271_cmd_set_sta_default_wep_key(struct wl1271 *wl, u8 id)
821 {
822         struct wl1271_cmd_set_sta_keys *cmd;
823         int ret = 0;
824
825         wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
826
827         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
828         if (!cmd) {
829                 ret = -ENOMEM;
830                 goto out;
831         }
832
833         cmd->id = id;
834         cmd->key_action = cpu_to_le16(KEY_SET_ID);
835         cmd->key_type = KEY_WEP;
836
837         ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
838         if (ret < 0) {
839                 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
840                 goto out;
841         }
842
843 out:
844         kfree(cmd);
845
846         return ret;
847 }
848
849 int wl1271_cmd_set_ap_default_wep_key(struct wl1271 *wl, u8 id)
850 {
851         struct wl1271_cmd_set_ap_keys *cmd;
852         int ret = 0;
853
854         wl1271_debug(DEBUG_CMD, "cmd set_ap_default_wep_key %d", id);
855
856         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
857         if (!cmd) {
858                 ret = -ENOMEM;
859                 goto out;
860         }
861
862         cmd->hlid = WL1271_AP_BROADCAST_HLID;
863         cmd->key_id = id;
864         cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
865         cmd->key_action = cpu_to_le16(KEY_SET_ID);
866         cmd->key_type = KEY_WEP;
867
868         ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
869         if (ret < 0) {
870                 wl1271_warning("cmd set_ap_default_wep_key failed: %d", ret);
871                 goto out;
872         }
873
874 out:
875         kfree(cmd);
876
877         return ret;
878 }
879
880 int wl1271_cmd_set_sta_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
881                        u8 key_size, const u8 *key, const u8 *addr,
882                        u32 tx_seq_32, u16 tx_seq_16)
883 {
884         struct wl1271_cmd_set_sta_keys *cmd;
885         int ret = 0;
886
887         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
888         if (!cmd) {
889                 ret = -ENOMEM;
890                 goto out;
891         }
892
893         if (key_type != KEY_WEP)
894                 memcpy(cmd->addr, addr, ETH_ALEN);
895
896         cmd->key_action = cpu_to_le16(action);
897         cmd->key_size = key_size;
898         cmd->key_type = key_type;
899
900         cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
901         cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
902
903         /* we have only one SSID profile */
904         cmd->ssid_profile = 0;
905
906         cmd->id = id;
907
908         if (key_type == KEY_TKIP) {
909                 /*
910                  * We get the key in the following form:
911                  * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
912                  * but the target is expecting:
913                  * TKIP - RX MIC - TX MIC
914                  */
915                 memcpy(cmd->key, key, 16);
916                 memcpy(cmd->key + 16, key + 24, 8);
917                 memcpy(cmd->key + 24, key + 16, 8);
918
919         } else {
920                 memcpy(cmd->key, key, key_size);
921         }
922
923         wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
924
925         ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
926         if (ret < 0) {
927                 wl1271_warning("could not set keys");
928         goto out;
929         }
930
931 out:
932         kfree(cmd);
933
934         return ret;
935 }
936
937 int wl1271_cmd_set_ap_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
938                         u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
939                         u16 tx_seq_16)
940 {
941         struct wl1271_cmd_set_ap_keys *cmd;
942         int ret = 0;
943         u8 lid_type;
944
945         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
946         if (!cmd)
947                 return -ENOMEM;
948
949         if (hlid == WL1271_AP_BROADCAST_HLID) {
950                 if (key_type == KEY_WEP)
951                         lid_type = WEP_DEFAULT_LID_TYPE;
952                 else
953                         lid_type = BROADCAST_LID_TYPE;
954         } else {
955                 lid_type = UNICAST_LID_TYPE;
956         }
957
958         wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
959                      " hlid: %d", (int)action, (int)id, (int)lid_type,
960                      (int)key_type, (int)hlid);
961
962         cmd->lid_key_type = lid_type;
963         cmd->hlid = hlid;
964         cmd->key_action = cpu_to_le16(action);
965         cmd->key_size = key_size;
966         cmd->key_type = key_type;
967         cmd->key_id = id;
968         cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
969         cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
970
971         if (key_type == KEY_TKIP) {
972                 /*
973                  * We get the key in the following form:
974                  * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
975                  * but the target is expecting:
976                  * TKIP - RX MIC - TX MIC
977                  */
978                 memcpy(cmd->key, key, 16);
979                 memcpy(cmd->key + 16, key + 24, 8);
980                 memcpy(cmd->key + 24, key + 16, 8);
981         } else {
982                 memcpy(cmd->key, key, key_size);
983         }
984
985         wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
986
987         ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
988         if (ret < 0) {
989                 wl1271_warning("could not set ap keys");
990                 goto out;
991         }
992
993 out:
994         kfree(cmd);
995         return ret;
996 }
997
998 int wl1271_cmd_disconnect(struct wl1271 *wl)
999 {
1000         struct wl1271_cmd_disconnect *cmd;
1001         int ret = 0;
1002
1003         wl1271_debug(DEBUG_CMD, "cmd disconnect");
1004
1005         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1006         if (!cmd) {
1007                 ret = -ENOMEM;
1008                 goto out;
1009         }
1010
1011         cmd->rx_config_options = cpu_to_le32(wl->rx_config);
1012         cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
1013         /* disconnect reason is not used in immediate disconnections */
1014         cmd->type = DISCONNECT_IMMEDIATE;
1015
1016         ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
1017         if (ret < 0) {
1018                 wl1271_error("failed to send disconnect command");
1019                 goto out_free;
1020         }
1021
1022         ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
1023         if (ret < 0)
1024                 wl1271_error("cmd disconnect event completion error");
1025
1026 out_free:
1027         kfree(cmd);
1028
1029 out:
1030         return ret;
1031 }
1032
1033 int wl1271_cmd_set_sta_state(struct wl1271 *wl)
1034 {
1035         struct wl1271_cmd_set_sta_state *cmd;
1036         int ret = 0;
1037
1038         wl1271_debug(DEBUG_CMD, "cmd set sta state");
1039
1040         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1041         if (!cmd) {
1042                 ret = -ENOMEM;
1043                 goto out;
1044         }
1045
1046         cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1047
1048         ret = wl1271_cmd_send(wl, CMD_SET_STA_STATE, cmd, sizeof(*cmd), 0);
1049         if (ret < 0) {
1050                 wl1271_error("failed to send set STA state command");
1051                 goto out_free;
1052         }
1053
1054 out_free:
1055         kfree(cmd);
1056
1057 out:
1058         return ret;
1059 }
1060
1061 int wl1271_cmd_start_bss(struct wl1271 *wl)
1062 {
1063         struct wl1271_cmd_bss_start *cmd;
1064         struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
1065         int ret;
1066
1067         wl1271_debug(DEBUG_CMD, "cmd start bss");
1068
1069         /*
1070          * FIXME: We currently do not support hidden SSID. The real SSID
1071          * should be fetched from mac80211 first.
1072          */
1073         if (wl->ssid_len == 0) {
1074                 wl1271_warning("Hidden SSID currently not supported for AP");
1075                 ret = -EINVAL;
1076                 goto out;
1077         }
1078
1079         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1080         if (!cmd) {
1081                 ret = -ENOMEM;
1082                 goto out;
1083         }
1084
1085         memcpy(cmd->bssid, bss_conf->bssid, ETH_ALEN);
1086
1087         cmd->aging_period = cpu_to_le16(WL1271_AP_DEF_INACTIV_SEC);
1088         cmd->bss_index = WL1271_AP_BSS_INDEX;
1089         cmd->global_hlid = WL1271_AP_GLOBAL_HLID;
1090         cmd->broadcast_hlid = WL1271_AP_BROADCAST_HLID;
1091         cmd->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
1092         cmd->beacon_interval = cpu_to_le16(wl->beacon_int);
1093         cmd->dtim_interval = bss_conf->dtim_period;
1094         cmd->beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
1095         cmd->channel = wl->channel;
1096         cmd->ssid_len = wl->ssid_len;
1097         cmd->ssid_type = SSID_TYPE_PUBLIC;
1098         memcpy(cmd->ssid, wl->ssid, wl->ssid_len);
1099
1100         switch (wl->band) {
1101         case IEEE80211_BAND_2GHZ:
1102                 cmd->band = RADIO_BAND_2_4GHZ;
1103                 break;
1104         case IEEE80211_BAND_5GHZ:
1105                 cmd->band = RADIO_BAND_5GHZ;
1106                 break;
1107         default:
1108                 wl1271_warning("bss start - unknown band: %d", (int)wl->band);
1109                 cmd->band = RADIO_BAND_2_4GHZ;
1110                 break;
1111         }
1112
1113         ret = wl1271_cmd_send(wl, CMD_BSS_START, cmd, sizeof(*cmd), 0);
1114         if (ret < 0) {
1115                 wl1271_error("failed to initiate cmd start bss");
1116                 goto out_free;
1117         }
1118
1119 out_free:
1120         kfree(cmd);
1121
1122 out:
1123         return ret;
1124 }
1125
1126 int wl1271_cmd_stop_bss(struct wl1271 *wl)
1127 {
1128         struct wl1271_cmd_bss_start *cmd;
1129         int ret;
1130
1131         wl1271_debug(DEBUG_CMD, "cmd stop bss");
1132
1133         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1134         if (!cmd) {
1135                 ret = -ENOMEM;
1136                 goto out;
1137         }
1138
1139         cmd->bss_index = WL1271_AP_BSS_INDEX;
1140
1141         ret = wl1271_cmd_send(wl, CMD_BSS_STOP, cmd, sizeof(*cmd), 0);
1142         if (ret < 0) {
1143                 wl1271_error("failed to initiate cmd stop bss");
1144                 goto out_free;
1145         }
1146
1147 out_free:
1148         kfree(cmd);
1149
1150 out:
1151         return ret;
1152 }
1153
1154 int wl1271_cmd_add_sta(struct wl1271 *wl, struct ieee80211_sta *sta, u8 hlid)
1155 {
1156         struct wl1271_cmd_add_sta *cmd;
1157         int ret;
1158
1159         wl1271_debug(DEBUG_CMD, "cmd add sta %d", (int)hlid);
1160
1161         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1162         if (!cmd) {
1163                 ret = -ENOMEM;
1164                 goto out;
1165         }
1166
1167         /* currently we don't support UAPSD */
1168         cmd->sp_len = 0;
1169
1170         memcpy(cmd->addr, sta->addr, ETH_ALEN);
1171         cmd->bss_index = WL1271_AP_BSS_INDEX;
1172         cmd->aid = sta->aid;
1173         cmd->hlid = hlid;
1174
1175         /*
1176          * FIXME: Does STA support QOS? We need to propagate this info from
1177          * hostapd. Currently not that important since this is only used for
1178          * sending the correct flavor of null-data packet in response to a
1179          * trigger.
1180          */
1181         cmd->wmm = 0;
1182
1183         cmd->supported_rates = cpu_to_le32(wl1271_tx_enabled_rates_get(wl,
1184                                                 sta->supp_rates[wl->band]));
1185
1186         wl1271_debug(DEBUG_CMD, "new sta rates: 0x%x", cmd->supported_rates);
1187
1188         ret = wl1271_cmd_send(wl, CMD_ADD_STA, cmd, sizeof(*cmd), 0);
1189         if (ret < 0) {
1190                 wl1271_error("failed to initiate cmd add sta");
1191                 goto out_free;
1192         }
1193
1194 out_free:
1195         kfree(cmd);
1196
1197 out:
1198         return ret;
1199 }
1200
1201 int wl1271_cmd_remove_sta(struct wl1271 *wl, u8 hlid)
1202 {
1203         struct wl1271_cmd_remove_sta *cmd;
1204         int ret;
1205
1206         wl1271_debug(DEBUG_CMD, "cmd remove sta %d", (int)hlid);
1207
1208         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1209         if (!cmd) {
1210                 ret = -ENOMEM;
1211                 goto out;
1212         }
1213
1214         cmd->hlid = hlid;
1215         /* We never send a deauth, mac80211 is in charge of this */
1216         cmd->reason_opcode = 0;
1217         cmd->send_deauth_flag = 0;
1218
1219         ret = wl1271_cmd_send(wl, CMD_REMOVE_STA, cmd, sizeof(*cmd), 0);
1220         if (ret < 0) {
1221                 wl1271_error("failed to initiate cmd remove sta");
1222                 goto out_free;
1223         }
1224
1225         /*
1226          * We are ok with a timeout here. The event is sometimes not sent
1227          * due to a firmware bug.
1228          */
1229         wl1271_cmd_wait_for_event_or_timeout(wl, STA_REMOVE_COMPLETE_EVENT_ID);
1230
1231 out_free:
1232         kfree(cmd);
1233
1234 out:
1235         return ret;
1236 }
1237
1238 int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
1239 {
1240         struct wl12xx_cmd_config_fwlog *cmd;
1241         int ret = 0;
1242
1243         wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
1244
1245         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1246         if (!cmd) {
1247                 ret = -ENOMEM;
1248                 goto out;
1249         }
1250
1251         cmd->logger_mode = wl->conf.fwlog.mode;
1252         cmd->log_severity = wl->conf.fwlog.severity;
1253         cmd->timestamp = wl->conf.fwlog.timestamp;
1254         cmd->output = wl->conf.fwlog.output;
1255         cmd->threshold = wl->conf.fwlog.threshold;
1256
1257         ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
1258         if (ret < 0) {
1259                 wl1271_error("failed to send config firmware logger command");
1260                 goto out_free;
1261         }
1262
1263 out_free:
1264         kfree(cmd);
1265
1266 out:
1267         return ret;
1268 }
1269
1270 int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
1271 {
1272         struct wl12xx_cmd_start_fwlog *cmd;
1273         int ret = 0;
1274
1275         wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
1276
1277         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1278         if (!cmd) {
1279                 ret = -ENOMEM;
1280                 goto out;
1281         }
1282
1283         ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
1284         if (ret < 0) {
1285                 wl1271_error("failed to send start firmware logger command");
1286                 goto out_free;
1287         }
1288
1289 out_free:
1290         kfree(cmd);
1291
1292 out:
1293         return ret;
1294 }
1295
1296 int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
1297 {
1298         struct wl12xx_cmd_stop_fwlog *cmd;
1299         int ret = 0;
1300
1301         wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
1302
1303         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1304         if (!cmd) {
1305                 ret = -ENOMEM;
1306                 goto out;
1307         }
1308
1309         ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
1310         if (ret < 0) {
1311                 wl1271_error("failed to send stop firmware logger command");
1312                 goto out_free;
1313         }
1314
1315 out_free:
1316         kfree(cmd);
1317
1318 out:
1319         return ret;
1320 }