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