b3a4f58249b0ddeaf7334e794ffcab0ffd9f1368
[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         ieee80211_queue_work(wl->hw, &wl->recovery_work);
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         ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
138         if (ret < 0) {
139                 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
140                 goto out;
141         }
142
143         gp->tx_bip_fem_manufacturer =
144                 gen_parms->general_params.tx_bip_fem_manufacturer;
145
146         wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
147                      answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
148
149 out:
150         kfree(gen_parms);
151         return ret;
152 }
153
154 int wl128x_cmd_general_parms(struct wl1271 *wl)
155 {
156         struct wl128x_general_parms_cmd *gen_parms;
157         struct wl128x_ini_general_params *gp =
158                 &((struct wl128x_nvs_file *)wl->nvs)->general_params;
159         bool answer = false;
160         int ret;
161
162         if (!wl->nvs)
163                 return -ENODEV;
164
165         gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
166         if (!gen_parms)
167                 return -ENOMEM;
168
169         gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
170
171         memcpy(&gen_parms->general_params, gp, sizeof(*gp));
172
173         if (gp->tx_bip_fem_auto_detect)
174                 answer = true;
175
176         /* Replace REF and TCXO CLKs with the ones from platform data */
177         gen_parms->general_params.ref_clock = wl->ref_clock;
178         gen_parms->general_params.tcxo_ref_clock = wl->tcxo_clock;
179
180         ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
181         if (ret < 0) {
182                 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
183                 goto out;
184         }
185
186         gp->tx_bip_fem_manufacturer =
187                 gen_parms->general_params.tx_bip_fem_manufacturer;
188
189         wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
190                      answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
191
192 out:
193         kfree(gen_parms);
194         return ret;
195 }
196
197 int wl1271_cmd_radio_parms(struct wl1271 *wl)
198 {
199         struct wl1271_nvs_file *nvs = (struct wl1271_nvs_file *)wl->nvs;
200         struct wl1271_radio_parms_cmd *radio_parms;
201         struct wl1271_ini_general_params *gp = &nvs->general_params;
202         int ret;
203
204         if (!wl->nvs)
205                 return -ENODEV;
206
207         radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
208         if (!radio_parms)
209                 return -ENOMEM;
210
211         radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
212
213         /* 2.4GHz parameters */
214         memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
215                sizeof(struct wl1271_ini_band_params_2));
216         memcpy(&radio_parms->dyn_params_2,
217                &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
218                sizeof(struct wl1271_ini_fem_params_2));
219
220         /* 5GHz parameters */
221         memcpy(&radio_parms->static_params_5,
222                &nvs->stat_radio_params_5,
223                sizeof(struct wl1271_ini_band_params_5));
224         memcpy(&radio_parms->dyn_params_5,
225                &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
226                sizeof(struct wl1271_ini_fem_params_5));
227
228         wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
229                     radio_parms, sizeof(*radio_parms));
230
231         ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
232         if (ret < 0)
233                 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
234
235         kfree(radio_parms);
236         return ret;
237 }
238
239 int wl128x_cmd_radio_parms(struct wl1271 *wl)
240 {
241         struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
242         struct wl128x_radio_parms_cmd *radio_parms;
243         struct wl128x_ini_general_params *gp = &nvs->general_params;
244         int ret;
245
246         if (!wl->nvs)
247                 return -ENODEV;
248
249         radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
250         if (!radio_parms)
251                 return -ENOMEM;
252
253         radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
254
255         /* 2.4GHz parameters */
256         memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
257                sizeof(struct wl128x_ini_band_params_2));
258         memcpy(&radio_parms->dyn_params_2,
259                &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
260                sizeof(struct wl128x_ini_fem_params_2));
261
262         /* 5GHz parameters */
263         memcpy(&radio_parms->static_params_5,
264                &nvs->stat_radio_params_5,
265                sizeof(struct wl128x_ini_band_params_5));
266         memcpy(&radio_parms->dyn_params_5,
267                &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
268                sizeof(struct wl128x_ini_fem_params_5));
269
270         radio_parms->fem_vendor_and_options = nvs->fem_vendor_and_options;
271
272         wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
273                     radio_parms, sizeof(*radio_parms));
274
275         ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
276         if (ret < 0)
277                 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
278
279         kfree(radio_parms);
280         return ret;
281 }
282
283 int wl1271_cmd_ext_radio_parms(struct wl1271 *wl)
284 {
285         struct wl1271_ext_radio_parms_cmd *ext_radio_parms;
286         struct conf_rf_settings *rf = &wl->conf.rf;
287         int ret;
288
289         if (!wl->nvs)
290                 return -ENODEV;
291
292         ext_radio_parms = kzalloc(sizeof(*ext_radio_parms), GFP_KERNEL);
293         if (!ext_radio_parms)
294                 return -ENOMEM;
295
296         ext_radio_parms->test.id = TEST_CMD_INI_FILE_RF_EXTENDED_PARAM;
297
298         memcpy(ext_radio_parms->tx_per_channel_power_compensation_2,
299                rf->tx_per_channel_power_compensation_2,
300                CONF_TX_PWR_COMPENSATION_LEN_2);
301         memcpy(ext_radio_parms->tx_per_channel_power_compensation_5,
302                rf->tx_per_channel_power_compensation_5,
303                CONF_TX_PWR_COMPENSATION_LEN_5);
304
305         wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_EXT_RADIO_PARAM: ",
306                     ext_radio_parms, sizeof(*ext_radio_parms));
307
308         ret = wl1271_cmd_test(wl, ext_radio_parms, sizeof(*ext_radio_parms), 0);
309         if (ret < 0)
310                 wl1271_warning("TEST_CMD_INI_FILE_RF_EXTENDED_PARAM failed");
311
312         kfree(ext_radio_parms);
313         return ret;
314 }
315
316 /*
317  * Poll the mailbox event field until any of the bits in the mask is set or a
318  * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
319  */
320 static int wl1271_cmd_wait_for_event_or_timeout(struct wl1271 *wl, u32 mask)
321 {
322         u32 events_vector, event;
323         unsigned long timeout;
324
325         timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
326
327         do {
328                 if (time_after(jiffies, timeout)) {
329                         wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
330                                      (int)mask);
331                         return -ETIMEDOUT;
332                 }
333
334                 msleep(1);
335
336                 /* read from both event fields */
337                 wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
338                             sizeof(events_vector), false);
339                 event = events_vector & mask;
340                 wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
341                             sizeof(events_vector), false);
342                 event |= events_vector & mask;
343         } while (!event);
344
345         return 0;
346 }
347
348 static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
349 {
350         int ret;
351
352         ret = wl1271_cmd_wait_for_event_or_timeout(wl, mask);
353         if (ret != 0) {
354                 ieee80211_queue_work(wl->hw, &wl->recovery_work);
355                 return ret;
356         }
357
358         return 0;
359 }
360
361 int wl1271_cmd_join(struct wl1271 *wl, u8 bss_type)
362 {
363         struct wl1271_cmd_join *join;
364         int ret, i;
365         u8 *bssid;
366
367         join = kzalloc(sizeof(*join), GFP_KERNEL);
368         if (!join) {
369                 ret = -ENOMEM;
370                 goto out;
371         }
372
373         wl1271_debug(DEBUG_CMD, "cmd join");
374
375         /* Reverse order BSSID */
376         bssid = (u8 *) &join->bssid_lsb;
377         for (i = 0; i < ETH_ALEN; i++)
378                 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
379
380         join->rx_config_options = cpu_to_le32(wl->rx_config);
381         join->rx_filter_options = cpu_to_le32(wl->rx_filter);
382         join->bss_type = bss_type;
383         join->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
384         join->supported_rate_set = cpu_to_le32(wl->rate_set);
385
386         if (wl->band == IEEE80211_BAND_5GHZ)
387                 join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
388
389         join->beacon_interval = cpu_to_le16(wl->beacon_int);
390         join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
391
392         join->channel = wl->channel;
393         join->ssid_len = wl->ssid_len;
394         memcpy(join->ssid, wl->ssid, wl->ssid_len);
395
396         join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
397
398         /* reset TX security counters */
399         wl->tx_security_last_seq = 0;
400         wl->tx_security_seq = 0;
401
402         wl1271_debug(DEBUG_CMD, "cmd join: basic_rate_set=0x%x, rate_set=0x%x",
403                 join->basic_rate_set, join->supported_rate_set);
404
405         ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
406         if (ret < 0) {
407                 wl1271_error("failed to initiate cmd join");
408                 goto out_free;
409         }
410
411         ret = wl1271_cmd_wait_for_event(wl, JOIN_EVENT_COMPLETE_ID);
412         if (ret < 0)
413                 wl1271_error("cmd join event completion error");
414
415 out_free:
416         kfree(join);
417
418 out:
419         return ret;
420 }
421
422 /**
423  * send test command to firmware
424  *
425  * @wl: wl struct
426  * @buf: buffer containing the command, with all headers, must work with dma
427  * @len: length of the buffer
428  * @answer: is answer needed
429  */
430 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
431 {
432         int ret;
433         size_t res_len = 0;
434
435         wl1271_debug(DEBUG_CMD, "cmd test");
436
437         if (answer)
438                 res_len = buf_len;
439
440         ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
441
442         if (ret < 0) {
443                 wl1271_warning("TEST command failed");
444                 return ret;
445         }
446
447         return ret;
448 }
449
450 /**
451  * read acx from firmware
452  *
453  * @wl: wl struct
454  * @id: acx id
455  * @buf: buffer for the response, including all headers, must work with dma
456  * @len: length of buf
457  */
458 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
459 {
460         struct acx_header *acx = buf;
461         int ret;
462
463         wl1271_debug(DEBUG_CMD, "cmd interrogate");
464
465         acx->id = cpu_to_le16(id);
466
467         /* payload length, does not include any headers */
468         acx->len = cpu_to_le16(len - sizeof(*acx));
469
470         ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
471         if (ret < 0)
472                 wl1271_error("INTERROGATE command failed");
473
474         return ret;
475 }
476
477 /**
478  * write acx value to firmware
479  *
480  * @wl: wl struct
481  * @id: acx id
482  * @buf: buffer containing acx, including all headers, must work with dma
483  * @len: length of buf
484  */
485 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
486 {
487         struct acx_header *acx = buf;
488         int ret;
489
490         wl1271_debug(DEBUG_CMD, "cmd configure");
491
492         acx->id = cpu_to_le16(id);
493
494         /* payload length, does not include any headers */
495         acx->len = cpu_to_le16(len - sizeof(*acx));
496
497         ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
498         if (ret < 0) {
499                 wl1271_warning("CONFIGURE command NOK");
500                 return ret;
501         }
502
503         return 0;
504 }
505
506 int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
507 {
508         struct cmd_enabledisable_path *cmd;
509         int ret;
510         u16 cmd_rx, cmd_tx;
511
512         wl1271_debug(DEBUG_CMD, "cmd data path");
513
514         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
515         if (!cmd) {
516                 ret = -ENOMEM;
517                 goto out;
518         }
519
520         /* the channel here is only used for calibration, so hardcoded to 1 */
521         cmd->channel = 1;
522
523         if (enable) {
524                 cmd_rx = CMD_ENABLE_RX;
525                 cmd_tx = CMD_ENABLE_TX;
526         } else {
527                 cmd_rx = CMD_DISABLE_RX;
528                 cmd_tx = CMD_DISABLE_TX;
529         }
530
531         ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
532         if (ret < 0) {
533                 wl1271_error("rx %s cmd for channel %d failed",
534                              enable ? "start" : "stop", cmd->channel);
535                 goto out;
536         }
537
538         wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
539                      enable ? "start" : "stop", cmd->channel);
540
541         ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
542         if (ret < 0) {
543                 wl1271_error("tx %s cmd for channel %d failed",
544                              enable ? "start" : "stop", cmd->channel);
545                 goto out;
546         }
547
548         wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
549                      enable ? "start" : "stop", cmd->channel);
550
551 out:
552         kfree(cmd);
553         return ret;
554 }
555
556 int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
557 {
558         struct wl1271_cmd_ps_params *ps_params = NULL;
559         int ret = 0;
560
561         wl1271_debug(DEBUG_CMD, "cmd set ps mode");
562
563         ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
564         if (!ps_params) {
565                 ret = -ENOMEM;
566                 goto out;
567         }
568
569         ps_params->ps_mode = ps_mode;
570
571         ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
572                               sizeof(*ps_params), 0);
573         if (ret < 0) {
574                 wl1271_error("cmd set_ps_mode failed");
575                 goto out;
576         }
577
578 out:
579         kfree(ps_params);
580         return ret;
581 }
582
583 int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
584                             void *buf, size_t buf_len, int index, u32 rates)
585 {
586         struct wl1271_cmd_template_set *cmd;
587         int ret = 0;
588
589         wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
590
591         WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
592         buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
593
594         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
595         if (!cmd) {
596                 ret = -ENOMEM;
597                 goto out;
598         }
599
600         cmd->len = cpu_to_le16(buf_len);
601         cmd->template_type = template_id;
602         cmd->enabled_rates = cpu_to_le32(rates);
603         cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
604         cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
605         cmd->index = index;
606
607         if (buf)
608                 memcpy(cmd->template_data, buf, buf_len);
609
610         ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
611         if (ret < 0) {
612                 wl1271_warning("cmd set_template failed: %d", ret);
613                 goto out_free;
614         }
615
616 out_free:
617         kfree(cmd);
618
619 out:
620         return ret;
621 }
622
623 int wl1271_cmd_build_null_data(struct wl1271 *wl)
624 {
625         struct sk_buff *skb = NULL;
626         int size;
627         void *ptr;
628         int ret = -ENOMEM;
629
630
631         if (wl->bss_type == BSS_TYPE_IBSS) {
632                 size = sizeof(struct wl12xx_null_data_template);
633                 ptr = NULL;
634         } else {
635                 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
636                 if (!skb)
637                         goto out;
638                 size = skb->len;
639                 ptr = skb->data;
640         }
641
642         ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
643                                       wl->basic_rate);
644
645 out:
646         dev_kfree_skb(skb);
647         if (ret)
648                 wl1271_warning("cmd buld null data failed %d", ret);
649
650         return ret;
651
652 }
653
654 int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
655 {
656         struct sk_buff *skb = NULL;
657         int ret = -ENOMEM;
658
659         skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
660         if (!skb)
661                 goto out;
662
663         ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
664                                       skb->data, skb->len,
665                                       CMD_TEMPL_KLV_IDX_NULL_DATA,
666                                       wl->basic_rate);
667
668 out:
669         dev_kfree_skb(skb);
670         if (ret)
671                 wl1271_warning("cmd build klv null data failed %d", ret);
672
673         return ret;
674
675 }
676
677 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
678 {
679         struct sk_buff *skb;
680         int ret = 0;
681
682         skb = ieee80211_pspoll_get(wl->hw, wl->vif);
683         if (!skb)
684                 goto out;
685
686         ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
687                                       skb->len, 0, wl->basic_rate_set);
688
689 out:
690         dev_kfree_skb(skb);
691         return ret;
692 }
693
694 int wl1271_cmd_build_probe_req(struct wl1271 *wl,
695                                const u8 *ssid, size_t ssid_len,
696                                const u8 *ie, size_t ie_len, u8 band)
697 {
698         struct sk_buff *skb;
699         int ret;
700
701         skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
702                                      ie, ie_len);
703         if (!skb) {
704                 ret = -ENOMEM;
705                 goto out;
706         }
707
708         wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
709
710         if (band == IEEE80211_BAND_2GHZ)
711                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
712                                               skb->data, skb->len, 0,
713                                               wl->conf.tx.basic_rate);
714         else
715                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
716                                               skb->data, skb->len, 0,
717                                               wl->conf.tx.basic_rate_5);
718
719 out:
720         dev_kfree_skb(skb);
721         return ret;
722 }
723
724 struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
725                                               struct sk_buff *skb)
726 {
727         int ret;
728
729         if (!skb)
730                 skb = ieee80211_ap_probereq_get(wl->hw, wl->vif);
731         if (!skb)
732                 goto out;
733
734         wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
735
736         if (wl->band == IEEE80211_BAND_2GHZ)
737                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
738                                               skb->data, skb->len, 0,
739                                               wl->conf.tx.basic_rate);
740         else
741                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
742                                               skb->data, skb->len, 0,
743                                               wl->conf.tx.basic_rate_5);
744
745         if (ret < 0)
746                 wl1271_error("Unable to set ap probe request template.");
747
748 out:
749         return skb;
750 }
751
752 int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, __be32 ip_addr)
753 {
754         int ret;
755         struct wl12xx_arp_rsp_template tmpl;
756         struct ieee80211_hdr_3addr *hdr;
757         struct arphdr *arp_hdr;
758
759         memset(&tmpl, 0, sizeof(tmpl));
760
761         /* mac80211 header */
762         hdr = &tmpl.hdr;
763         hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
764                                          IEEE80211_STYPE_DATA |
765                                          IEEE80211_FCTL_TODS);
766         memcpy(hdr->addr1, wl->vif->bss_conf.bssid, ETH_ALEN);
767         memcpy(hdr->addr2, wl->vif->addr, ETH_ALEN);
768         memset(hdr->addr3, 0xff, ETH_ALEN);
769
770         /* llc layer */
771         memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
772         tmpl.llc_type = cpu_to_be16(ETH_P_ARP);
773
774         /* arp header */
775         arp_hdr = &tmpl.arp_hdr;
776         arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
777         arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
778         arp_hdr->ar_hln = ETH_ALEN;
779         arp_hdr->ar_pln = 4;
780         arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
781
782         /* arp payload */
783         memcpy(tmpl.sender_hw, wl->vif->addr, ETH_ALEN);
784         tmpl.sender_ip = ip_addr;
785
786         ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
787                                       &tmpl, sizeof(tmpl), 0,
788                                       wl->basic_rate);
789
790         return ret;
791 }
792
793 int wl1271_build_qos_null_data(struct wl1271 *wl)
794 {
795         struct ieee80211_qos_hdr template;
796
797         memset(&template, 0, sizeof(template));
798
799         memcpy(template.addr1, wl->bssid, ETH_ALEN);
800         memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
801         memcpy(template.addr3, wl->bssid, ETH_ALEN);
802
803         template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
804                                              IEEE80211_STYPE_QOS_NULLFUNC |
805                                              IEEE80211_FCTL_TODS);
806
807         /* FIXME: not sure what priority to use here */
808         template.qos_ctrl = cpu_to_le16(0);
809
810         return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
811                                        sizeof(template), 0,
812                                        wl->basic_rate);
813 }
814
815 int wl1271_cmd_set_sta_default_wep_key(struct wl1271 *wl, u8 id)
816 {
817         struct wl1271_cmd_set_sta_keys *cmd;
818         int ret = 0;
819
820         wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
821
822         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
823         if (!cmd) {
824                 ret = -ENOMEM;
825                 goto out;
826         }
827
828         cmd->id = id;
829         cmd->key_action = cpu_to_le16(KEY_SET_ID);
830         cmd->key_type = KEY_WEP;
831
832         ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
833         if (ret < 0) {
834                 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
835                 goto out;
836         }
837
838 out:
839         kfree(cmd);
840
841         return ret;
842 }
843
844 int wl1271_cmd_set_ap_default_wep_key(struct wl1271 *wl, u8 id)
845 {
846         struct wl1271_cmd_set_ap_keys *cmd;
847         int ret = 0;
848
849         wl1271_debug(DEBUG_CMD, "cmd set_ap_default_wep_key %d", id);
850
851         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
852         if (!cmd) {
853                 ret = -ENOMEM;
854                 goto out;
855         }
856
857         cmd->hlid = WL1271_AP_BROADCAST_HLID;
858         cmd->key_id = id;
859         cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
860         cmd->key_action = cpu_to_le16(KEY_SET_ID);
861         cmd->key_type = KEY_WEP;
862
863         ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
864         if (ret < 0) {
865                 wl1271_warning("cmd set_ap_default_wep_key failed: %d", ret);
866                 goto out;
867         }
868
869 out:
870         kfree(cmd);
871
872         return ret;
873 }
874
875 int wl1271_cmd_set_sta_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
876                        u8 key_size, const u8 *key, const u8 *addr,
877                        u32 tx_seq_32, u16 tx_seq_16)
878 {
879         struct wl1271_cmd_set_sta_keys *cmd;
880         int ret = 0;
881
882         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
883         if (!cmd) {
884                 ret = -ENOMEM;
885                 goto out;
886         }
887
888         if (key_type != KEY_WEP)
889                 memcpy(cmd->addr, addr, ETH_ALEN);
890
891         cmd->key_action = cpu_to_le16(action);
892         cmd->key_size = key_size;
893         cmd->key_type = key_type;
894
895         cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
896         cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
897
898         /* we have only one SSID profile */
899         cmd->ssid_profile = 0;
900
901         cmd->id = id;
902
903         if (key_type == KEY_TKIP) {
904                 /*
905                  * We get the key in the following form:
906                  * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
907                  * but the target is expecting:
908                  * TKIP - RX MIC - TX MIC
909                  */
910                 memcpy(cmd->key, key, 16);
911                 memcpy(cmd->key + 16, key + 24, 8);
912                 memcpy(cmd->key + 24, key + 16, 8);
913
914         } else {
915                 memcpy(cmd->key, key, key_size);
916         }
917
918         wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
919
920         ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
921         if (ret < 0) {
922                 wl1271_warning("could not set keys");
923         goto out;
924         }
925
926 out:
927         kfree(cmd);
928
929         return ret;
930 }
931
932 int wl1271_cmd_set_ap_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
933                         u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
934                         u16 tx_seq_16)
935 {
936         struct wl1271_cmd_set_ap_keys *cmd;
937         int ret = 0;
938         u8 lid_type;
939
940         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
941         if (!cmd)
942                 return -ENOMEM;
943
944         if (hlid == WL1271_AP_BROADCAST_HLID) {
945                 if (key_type == KEY_WEP)
946                         lid_type = WEP_DEFAULT_LID_TYPE;
947                 else
948                         lid_type = BROADCAST_LID_TYPE;
949         } else {
950                 lid_type = UNICAST_LID_TYPE;
951         }
952
953         wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
954                      " hlid: %d", (int)action, (int)id, (int)lid_type,
955                      (int)key_type, (int)hlid);
956
957         cmd->lid_key_type = lid_type;
958         cmd->hlid = hlid;
959         cmd->key_action = cpu_to_le16(action);
960         cmd->key_size = key_size;
961         cmd->key_type = key_type;
962         cmd->key_id = id;
963         cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
964         cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
965
966         if (key_type == KEY_TKIP) {
967                 /*
968                  * We get the key in the following form:
969                  * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
970                  * but the target is expecting:
971                  * TKIP - RX MIC - TX MIC
972                  */
973                 memcpy(cmd->key, key, 16);
974                 memcpy(cmd->key + 16, key + 24, 8);
975                 memcpy(cmd->key + 24, key + 16, 8);
976         } else {
977                 memcpy(cmd->key, key, key_size);
978         }
979
980         wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
981
982         ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
983         if (ret < 0) {
984                 wl1271_warning("could not set ap keys");
985                 goto out;
986         }
987
988 out:
989         kfree(cmd);
990         return ret;
991 }
992
993 int wl1271_cmd_disconnect(struct wl1271 *wl)
994 {
995         struct wl1271_cmd_disconnect *cmd;
996         int ret = 0;
997
998         wl1271_debug(DEBUG_CMD, "cmd disconnect");
999
1000         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1001         if (!cmd) {
1002                 ret = -ENOMEM;
1003                 goto out;
1004         }
1005
1006         cmd->rx_config_options = cpu_to_le32(wl->rx_config);
1007         cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
1008         /* disconnect reason is not used in immediate disconnections */
1009         cmd->type = DISCONNECT_IMMEDIATE;
1010
1011         ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
1012         if (ret < 0) {
1013                 wl1271_error("failed to send disconnect command");
1014                 goto out_free;
1015         }
1016
1017         ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
1018         if (ret < 0)
1019                 wl1271_error("cmd disconnect event completion error");
1020
1021 out_free:
1022         kfree(cmd);
1023
1024 out:
1025         return ret;
1026 }
1027
1028 int wl1271_cmd_set_sta_state(struct wl1271 *wl)
1029 {
1030         struct wl1271_cmd_set_sta_state *cmd;
1031         int ret = 0;
1032
1033         wl1271_debug(DEBUG_CMD, "cmd set sta state");
1034
1035         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1036         if (!cmd) {
1037                 ret = -ENOMEM;
1038                 goto out;
1039         }
1040
1041         cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1042
1043         ret = wl1271_cmd_send(wl, CMD_SET_STA_STATE, cmd, sizeof(*cmd), 0);
1044         if (ret < 0) {
1045                 wl1271_error("failed to send set STA state command");
1046                 goto out_free;
1047         }
1048
1049 out_free:
1050         kfree(cmd);
1051
1052 out:
1053         return ret;
1054 }
1055
1056 int wl1271_cmd_start_bss(struct wl1271 *wl)
1057 {
1058         struct wl1271_cmd_bss_start *cmd;
1059         struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
1060         int ret;
1061
1062         wl1271_debug(DEBUG_CMD, "cmd start bss");
1063
1064         /*
1065          * FIXME: We currently do not support hidden SSID. The real SSID
1066          * should be fetched from mac80211 first.
1067          */
1068         if (wl->ssid_len == 0) {
1069                 wl1271_warning("Hidden SSID currently not supported for AP");
1070                 ret = -EINVAL;
1071                 goto out;
1072         }
1073
1074         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1075         if (!cmd) {
1076                 ret = -ENOMEM;
1077                 goto out;
1078         }
1079
1080         memcpy(cmd->bssid, bss_conf->bssid, ETH_ALEN);
1081
1082         cmd->aging_period = cpu_to_le16(WL1271_AP_DEF_INACTIV_SEC);
1083         cmd->bss_index = WL1271_AP_BSS_INDEX;
1084         cmd->global_hlid = WL1271_AP_GLOBAL_HLID;
1085         cmd->broadcast_hlid = WL1271_AP_BROADCAST_HLID;
1086         cmd->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
1087         cmd->beacon_interval = cpu_to_le16(wl->beacon_int);
1088         cmd->dtim_interval = bss_conf->dtim_period;
1089         cmd->beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
1090         cmd->channel = wl->channel;
1091         cmd->ssid_len = wl->ssid_len;
1092         cmd->ssid_type = SSID_TYPE_PUBLIC;
1093         memcpy(cmd->ssid, wl->ssid, wl->ssid_len);
1094
1095         switch (wl->band) {
1096         case IEEE80211_BAND_2GHZ:
1097                 cmd->band = RADIO_BAND_2_4GHZ;
1098                 break;
1099         case IEEE80211_BAND_5GHZ:
1100                 cmd->band = RADIO_BAND_5GHZ;
1101                 break;
1102         default:
1103                 wl1271_warning("bss start - unknown band: %d", (int)wl->band);
1104                 cmd->band = RADIO_BAND_2_4GHZ;
1105                 break;
1106         }
1107
1108         ret = wl1271_cmd_send(wl, CMD_BSS_START, cmd, sizeof(*cmd), 0);
1109         if (ret < 0) {
1110                 wl1271_error("failed to initiate cmd start bss");
1111                 goto out_free;
1112         }
1113
1114 out_free:
1115         kfree(cmd);
1116
1117 out:
1118         return ret;
1119 }
1120
1121 int wl1271_cmd_stop_bss(struct wl1271 *wl)
1122 {
1123         struct wl1271_cmd_bss_start *cmd;
1124         int ret;
1125
1126         wl1271_debug(DEBUG_CMD, "cmd stop bss");
1127
1128         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1129         if (!cmd) {
1130                 ret = -ENOMEM;
1131                 goto out;
1132         }
1133
1134         cmd->bss_index = WL1271_AP_BSS_INDEX;
1135
1136         ret = wl1271_cmd_send(wl, CMD_BSS_STOP, cmd, sizeof(*cmd), 0);
1137         if (ret < 0) {
1138                 wl1271_error("failed to initiate cmd stop bss");
1139                 goto out_free;
1140         }
1141
1142 out_free:
1143         kfree(cmd);
1144
1145 out:
1146         return ret;
1147 }
1148
1149 int wl1271_cmd_add_sta(struct wl1271 *wl, struct ieee80211_sta *sta, u8 hlid)
1150 {
1151         struct wl1271_cmd_add_sta *cmd;
1152         int ret;
1153
1154         wl1271_debug(DEBUG_CMD, "cmd add sta %d", (int)hlid);
1155
1156         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1157         if (!cmd) {
1158                 ret = -ENOMEM;
1159                 goto out;
1160         }
1161
1162         /* currently we don't support UAPSD */
1163         cmd->sp_len = 0;
1164
1165         memcpy(cmd->addr, sta->addr, ETH_ALEN);
1166         cmd->bss_index = WL1271_AP_BSS_INDEX;
1167         cmd->aid = sta->aid;
1168         cmd->hlid = hlid;
1169
1170         /*
1171          * FIXME: Does STA support QOS? We need to propagate this info from
1172          * hostapd. Currently not that important since this is only used for
1173          * sending the correct flavor of null-data packet in response to a
1174          * trigger.
1175          */
1176         cmd->wmm = 0;
1177
1178         cmd->supported_rates = cpu_to_le32(wl1271_tx_enabled_rates_get(wl,
1179                                                 sta->supp_rates[wl->band]));
1180
1181         wl1271_debug(DEBUG_CMD, "new sta rates: 0x%x", cmd->supported_rates);
1182
1183         ret = wl1271_cmd_send(wl, CMD_ADD_STA, cmd, sizeof(*cmd), 0);
1184         if (ret < 0) {
1185                 wl1271_error("failed to initiate cmd add sta");
1186                 goto out_free;
1187         }
1188
1189 out_free:
1190         kfree(cmd);
1191
1192 out:
1193         return ret;
1194 }
1195
1196 int wl1271_cmd_remove_sta(struct wl1271 *wl, u8 hlid)
1197 {
1198         struct wl1271_cmd_remove_sta *cmd;
1199         int ret;
1200
1201         wl1271_debug(DEBUG_CMD, "cmd remove sta %d", (int)hlid);
1202
1203         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1204         if (!cmd) {
1205                 ret = -ENOMEM;
1206                 goto out;
1207         }
1208
1209         cmd->hlid = hlid;
1210         /* We never send a deauth, mac80211 is in charge of this */
1211         cmd->reason_opcode = 0;
1212         cmd->send_deauth_flag = 0;
1213
1214         ret = wl1271_cmd_send(wl, CMD_REMOVE_STA, cmd, sizeof(*cmd), 0);
1215         if (ret < 0) {
1216                 wl1271_error("failed to initiate cmd remove sta");
1217                 goto out_free;
1218         }
1219
1220         /*
1221          * We are ok with a timeout here. The event is sometimes not sent
1222          * due to a firmware bug.
1223          */
1224         wl1271_cmd_wait_for_event_or_timeout(wl, STA_REMOVE_COMPLETE_EVENT_ID);
1225
1226 out_free:
1227         kfree(cmd);
1228
1229 out:
1230         return ret;
1231 }