wl1271: Prevent dropping of TX frames in joins
[pandora-kernel.git] / drivers / net / wireless / wl12xx / wl1271_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 "wl1271.h"
33 #include "wl1271_reg.h"
34 #include "wl1271_io.h"
35 #include "wl1271_acx.h"
36 #include "wl12xx_80211.h"
37 #include "wl1271_cmd.h"
38 #include "wl1271_event.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
66         wl1271_write(wl, wl->cmd_box_addr, buf, len, false);
67
68         wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
69
70         timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
71
72         intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
73         while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
74                 if (time_after(jiffies, timeout)) {
75                         wl1271_error("command complete timeout");
76                         ret = -ETIMEDOUT;
77                         goto out;
78                 }
79
80                 poll_count++;
81                 if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
82                         udelay(10);
83                 else
84                         msleep(1);
85
86                 intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
87         }
88
89         /* read back the status code of the command */
90         if (res_len == 0)
91                 res_len = sizeof(struct wl1271_cmd_header);
92         wl1271_read(wl, wl->cmd_box_addr, cmd, res_len, false);
93
94         status = le16_to_cpu(cmd->status);
95         if (status != CMD_STATUS_SUCCESS) {
96                 wl1271_error("command execute failure %d", status);
97                 ret = -EIO;
98         }
99
100         wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
101                        WL1271_ACX_INTR_CMD_COMPLETE);
102
103 out:
104         return ret;
105 }
106
107 static int wl1271_cmd_cal_channel_tune(struct wl1271 *wl)
108 {
109         struct wl1271_cmd_cal_channel_tune *cmd;
110         int ret = 0;
111
112         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
113         if (!cmd)
114                 return -ENOMEM;
115
116         cmd->test.id = TEST_CMD_CHANNEL_TUNE;
117
118         cmd->band = WL1271_CHANNEL_TUNE_BAND_2_4;
119         /* set up any channel, 7 is in the middle of the range */
120         cmd->channel = 7;
121
122         ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
123         if (ret < 0)
124                 wl1271_warning("TEST_CMD_CHANNEL_TUNE failed");
125
126         kfree(cmd);
127         return ret;
128 }
129
130 static int wl1271_cmd_cal_update_ref_point(struct wl1271 *wl)
131 {
132         struct wl1271_cmd_cal_update_ref_point *cmd;
133         int ret = 0;
134
135         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
136         if (!cmd)
137                 return -ENOMEM;
138
139         cmd->test.id = TEST_CMD_UPDATE_PD_REFERENCE_POINT;
140
141         /* FIXME: still waiting for the correct values */
142         cmd->ref_power    = 0;
143         cmd->ref_detector = 0;
144
145         cmd->sub_band     = WL1271_PD_REFERENCE_POINT_BAND_B_G;
146
147         ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
148         if (ret < 0)
149                 wl1271_warning("TEST_CMD_UPDATE_PD_REFERENCE_POINT failed");
150
151         kfree(cmd);
152         return ret;
153 }
154
155 static int wl1271_cmd_cal_p2g(struct wl1271 *wl)
156 {
157         struct wl1271_cmd_cal_p2g *cmd;
158         int ret = 0;
159
160         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
161         if (!cmd)
162                 return -ENOMEM;
163
164         cmd->test.id = TEST_CMD_P2G_CAL;
165
166         cmd->sub_band_mask = WL1271_CAL_P2G_BAND_B_G;
167
168         ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
169         if (ret < 0)
170                 wl1271_warning("TEST_CMD_P2G_CAL failed");
171
172         kfree(cmd);
173         return ret;
174 }
175
176 static int wl1271_cmd_cal(struct wl1271 *wl)
177 {
178         /*
179          * FIXME: we must make sure that we're not sleeping when calibration
180          * is done
181          */
182         int ret;
183
184         wl1271_notice("performing tx calibration");
185
186         ret = wl1271_cmd_cal_channel_tune(wl);
187         if (ret < 0)
188                 return ret;
189
190         ret = wl1271_cmd_cal_update_ref_point(wl);
191         if (ret < 0)
192                 return ret;
193
194         ret = wl1271_cmd_cal_p2g(wl);
195         if (ret < 0)
196                 return ret;
197
198         return ret;
199 }
200
201 int wl1271_cmd_general_parms(struct wl1271 *wl)
202 {
203         struct wl1271_general_parms_cmd *gen_parms;
204         int ret;
205
206         if (!wl->nvs)
207                 return -ENODEV;
208
209         gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
210         if (!gen_parms)
211                 return -ENOMEM;
212
213         gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
214
215         memcpy(&gen_parms->general_params, &wl->nvs->general_params,
216                sizeof(struct wl1271_ini_general_params));
217
218         ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), 0);
219         if (ret < 0)
220                 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
221
222         kfree(gen_parms);
223         return ret;
224 }
225
226 int wl1271_cmd_radio_parms(struct wl1271 *wl)
227 {
228         struct wl1271_radio_parms_cmd *radio_parms;
229         struct conf_radio_parms *rparam = &wl->conf.init.radioparam;
230         int ret;
231
232         if (!wl->nvs)
233                 return -ENODEV;
234
235         radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
236         if (!radio_parms)
237                 return -ENOMEM;
238
239         radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
240
241         /* 2.4GHz parameters */
242         memcpy(&radio_parms->static_params_2, &wl->nvs->stat_radio_params_2,
243                sizeof(struct wl1271_ini_band_params_2));
244         memcpy(&radio_parms->dyn_params_2,
245                &wl->nvs->dyn_radio_params_2[rparam->fem].params,
246                sizeof(struct wl1271_ini_fem_params_2));
247
248         /* 5GHz parameters */
249         memcpy(&radio_parms->static_params_5,
250                &wl->nvs->stat_radio_params_5,
251                sizeof(struct wl1271_ini_band_params_5));
252         memcpy(&radio_parms->dyn_params_5,
253                &wl->nvs->dyn_radio_params_5[rparam->fem].params,
254                sizeof(struct wl1271_ini_fem_params_5));
255
256         wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
257                     radio_parms, sizeof(*radio_parms));
258
259         ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
260         if (ret < 0)
261                 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
262
263         kfree(radio_parms);
264         return ret;
265 }
266
267 /*
268  * Poll the mailbox event field until any of the bits in the mask is set or a
269  * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
270  */
271 static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
272 {
273         u32 events_vector, event;
274         unsigned long timeout;
275
276         timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
277
278         do {
279                 if (time_after(jiffies, timeout))
280                         return -ETIMEDOUT;
281
282                 msleep(1);
283
284                 /* read from both event fields */
285                 wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
286                             sizeof(events_vector), false);
287                 event = events_vector & mask;
288                 wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
289                             sizeof(events_vector), false);
290                 event |= events_vector & mask;
291         } while (!event);
292
293         return 0;
294 }
295
296 int wl1271_cmd_join(struct wl1271 *wl, u8 bss_type)
297 {
298         static bool do_cal = true;
299         struct wl1271_cmd_join *join;
300         int ret, i;
301         u8 *bssid;
302
303         /* FIXME: remove when we get calibration from the factory */
304         if (do_cal) {
305                 ret = wl1271_cmd_cal(wl);
306                 if (ret < 0)
307                         wl1271_warning("couldn't calibrate");
308                 else
309                         do_cal = false;
310         }
311
312         join = kzalloc(sizeof(*join), GFP_KERNEL);
313         if (!join) {
314                 ret = -ENOMEM;
315                 goto out;
316         }
317
318         wl1271_debug(DEBUG_CMD, "cmd join");
319
320         /* Reverse order BSSID */
321         bssid = (u8 *) &join->bssid_lsb;
322         for (i = 0; i < ETH_ALEN; i++)
323                 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
324
325         join->rx_config_options = cpu_to_le32(wl->rx_config);
326         join->rx_filter_options = cpu_to_le32(wl->rx_filter);
327         join->bss_type = bss_type;
328         join->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
329
330         if (wl->band == IEEE80211_BAND_5GHZ)
331                 join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
332
333         join->beacon_interval = cpu_to_le16(wl->beacon_int);
334         join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
335
336         join->channel = wl->channel;
337         join->ssid_len = wl->ssid_len;
338         memcpy(join->ssid, wl->ssid, wl->ssid_len);
339
340         join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
341
342         /* reset TX security counters */
343         wl->tx_security_last_seq = 0;
344         wl->tx_security_seq = 0;
345
346         ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
347         if (ret < 0) {
348                 wl1271_error("failed to initiate cmd join");
349                 goto out_free;
350         }
351
352         ret = wl1271_cmd_wait_for_event(wl, JOIN_EVENT_COMPLETE_ID);
353         if (ret < 0)
354                 wl1271_error("cmd join event completion error");
355
356 out_free:
357         kfree(join);
358
359 out:
360         return ret;
361 }
362
363 /**
364  * send test command to firmware
365  *
366  * @wl: wl struct
367  * @buf: buffer containing the command, with all headers, must work with dma
368  * @len: length of the buffer
369  * @answer: is answer needed
370  */
371 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
372 {
373         int ret;
374         size_t res_len = 0;
375
376         wl1271_debug(DEBUG_CMD, "cmd test");
377
378         if (answer)
379                 res_len = buf_len;
380
381         ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
382
383         if (ret < 0) {
384                 wl1271_warning("TEST command failed");
385                 return ret;
386         }
387
388         return ret;
389 }
390
391 /**
392  * read acx from firmware
393  *
394  * @wl: wl struct
395  * @id: acx id
396  * @buf: buffer for the response, including all headers, must work with dma
397  * @len: lenght of buf
398  */
399 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
400 {
401         struct acx_header *acx = buf;
402         int ret;
403
404         wl1271_debug(DEBUG_CMD, "cmd interrogate");
405
406         acx->id = cpu_to_le16(id);
407
408         /* payload length, does not include any headers */
409         acx->len = cpu_to_le16(len - sizeof(*acx));
410
411         ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
412         if (ret < 0)
413                 wl1271_error("INTERROGATE command failed");
414
415         return ret;
416 }
417
418 /**
419  * write acx value to firmware
420  *
421  * @wl: wl struct
422  * @id: acx id
423  * @buf: buffer containing acx, including all headers, must work with dma
424  * @len: length of buf
425  */
426 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
427 {
428         struct acx_header *acx = buf;
429         int ret;
430
431         wl1271_debug(DEBUG_CMD, "cmd configure");
432
433         acx->id = cpu_to_le16(id);
434
435         /* payload length, does not include any headers */
436         acx->len = cpu_to_le16(len - sizeof(*acx));
437
438         ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
439         if (ret < 0) {
440                 wl1271_warning("CONFIGURE command NOK");
441                 return ret;
442         }
443
444         return 0;
445 }
446
447 int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
448 {
449         struct cmd_enabledisable_path *cmd;
450         int ret;
451         u16 cmd_rx, cmd_tx;
452
453         wl1271_debug(DEBUG_CMD, "cmd data path");
454
455         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
456         if (!cmd) {
457                 ret = -ENOMEM;
458                 goto out;
459         }
460
461         /* the channel here is only used for calibration, so hardcoded to 1 */
462         cmd->channel = 1;
463
464         if (enable) {
465                 cmd_rx = CMD_ENABLE_RX;
466                 cmd_tx = CMD_ENABLE_TX;
467         } else {
468                 cmd_rx = CMD_DISABLE_RX;
469                 cmd_tx = CMD_DISABLE_TX;
470         }
471
472         ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
473         if (ret < 0) {
474                 wl1271_error("rx %s cmd for channel %d failed",
475                              enable ? "start" : "stop", cmd->channel);
476                 goto out;
477         }
478
479         wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
480                      enable ? "start" : "stop", cmd->channel);
481
482         ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
483         if (ret < 0) {
484                 wl1271_error("tx %s cmd for channel %d failed",
485                              enable ? "start" : "stop", cmd->channel);
486                 goto out;
487         }
488
489         wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
490                      enable ? "start" : "stop", cmd->channel);
491
492 out:
493         kfree(cmd);
494         return ret;
495 }
496
497 int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode, bool send)
498 {
499         struct wl1271_cmd_ps_params *ps_params = NULL;
500         int ret = 0;
501
502         /* FIXME: this should be in ps.c */
503         ret = wl1271_acx_wake_up_conditions(wl);
504         if (ret < 0) {
505                 wl1271_error("couldn't set wake up conditions");
506                 goto out;
507         }
508
509         wl1271_debug(DEBUG_CMD, "cmd set ps mode");
510
511         ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
512         if (!ps_params) {
513                 ret = -ENOMEM;
514                 goto out;
515         }
516
517         ps_params->ps_mode = ps_mode;
518         ps_params->send_null_data = send;
519         ps_params->retries = 5;
520         ps_params->hang_over_period = 1;
521         ps_params->null_data_rate = cpu_to_le32(1); /* 1 Mbps */
522
523         ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
524                               sizeof(*ps_params), 0);
525         if (ret < 0) {
526                 wl1271_error("cmd set_ps_mode failed");
527                 goto out;
528         }
529
530 out:
531         kfree(ps_params);
532         return ret;
533 }
534
535 int wl1271_cmd_read_memory(struct wl1271 *wl, u32 addr, void *answer,
536                            size_t len)
537 {
538         struct cmd_read_write_memory *cmd;
539         int ret = 0;
540
541         wl1271_debug(DEBUG_CMD, "cmd read memory");
542
543         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
544         if (!cmd) {
545                 ret = -ENOMEM;
546                 goto out;
547         }
548
549         WARN_ON(len > MAX_READ_SIZE);
550         len = min_t(size_t, len, MAX_READ_SIZE);
551
552         cmd->addr = cpu_to_le32(addr);
553         cmd->size = cpu_to_le32(len);
554
555         ret = wl1271_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd),
556                               sizeof(*cmd));
557         if (ret < 0) {
558                 wl1271_error("read memory command failed: %d", ret);
559                 goto out;
560         }
561
562         /* the read command got in */
563         memcpy(answer, cmd->value, len);
564
565 out:
566         kfree(cmd);
567         return ret;
568 }
569
570 int wl1271_cmd_scan(struct wl1271 *wl, const u8 *ssid, size_t ssid_len,
571                     const u8 *ie, size_t ie_len, u8 active_scan,
572                     u8 high_prio, u8 band, u8 probe_requests)
573 {
574
575         struct wl1271_cmd_trigger_scan_to *trigger = NULL;
576         struct wl1271_cmd_scan *params = NULL;
577         struct ieee80211_channel *channels;
578         u32 rate;
579         int i, j, n_ch, ret;
580         u16 scan_options = 0;
581         u8 ieee_band;
582
583         if (band == WL1271_SCAN_BAND_2_4_GHZ) {
584                 ieee_band = IEEE80211_BAND_2GHZ;
585                 rate = wl->conf.tx.basic_rate;
586         } else if (band == WL1271_SCAN_BAND_DUAL && wl1271_11a_enabled()) {
587                 ieee_band = IEEE80211_BAND_2GHZ;
588                 rate = wl->conf.tx.basic_rate;
589         } else if (band == WL1271_SCAN_BAND_5_GHZ && wl1271_11a_enabled()) {
590                 ieee_band = IEEE80211_BAND_5GHZ;
591                 rate = wl->conf.tx.basic_rate_5;
592         } else
593                 return -EINVAL;
594
595         if (wl->hw->wiphy->bands[ieee_band]->channels == NULL)
596                 return -EINVAL;
597
598         channels = wl->hw->wiphy->bands[ieee_band]->channels;
599         n_ch = wl->hw->wiphy->bands[ieee_band]->n_channels;
600
601         if (test_bit(WL1271_FLAG_SCANNING, &wl->flags))
602                 return -EINVAL;
603
604         params = kzalloc(sizeof(*params), GFP_KERNEL);
605         if (!params)
606                 return -ENOMEM;
607
608         params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
609         params->params.rx_filter_options =
610                 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
611
612         if (!active_scan)
613                 scan_options |= WL1271_SCAN_OPT_PASSIVE;
614         if (high_prio)
615                 scan_options |= WL1271_SCAN_OPT_PRIORITY_HIGH;
616         params->params.scan_options = cpu_to_le16(scan_options);
617
618         params->params.num_probe_requests = probe_requests;
619         params->params.tx_rate = cpu_to_le32(rate);
620         params->params.tid_trigger = 0;
621         params->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
622
623         if (band == WL1271_SCAN_BAND_DUAL)
624                 params->params.band = WL1271_SCAN_BAND_2_4_GHZ;
625         else
626                 params->params.band = band;
627
628         for (i = 0, j = 0; i < n_ch && i < WL1271_SCAN_MAX_CHANNELS; i++) {
629                 if (!(channels[i].flags & IEEE80211_CHAN_DISABLED)) {
630                         params->channels[j].min_duration =
631                                 cpu_to_le32(WL1271_SCAN_CHAN_MIN_DURATION);
632                         params->channels[j].max_duration =
633                                 cpu_to_le32(WL1271_SCAN_CHAN_MAX_DURATION);
634                         memset(&params->channels[j].bssid_lsb, 0xff, 4);
635                         memset(&params->channels[j].bssid_msb, 0xff, 2);
636                         params->channels[j].early_termination = 0;
637                         params->channels[j].tx_power_att =
638                                 WL1271_SCAN_CURRENT_TX_PWR;
639                         params->channels[j].channel = channels[i].hw_value;
640                         j++;
641                 }
642         }
643
644         params->params.num_channels = j;
645
646         if (ssid_len && ssid) {
647                 params->params.ssid_len = ssid_len;
648                 memcpy(params->params.ssid, ssid, ssid_len);
649         }
650
651         ret = wl1271_cmd_build_probe_req(wl, ssid, ssid_len,
652                                          ie, ie_len, ieee_band);
653         if (ret < 0) {
654                 wl1271_error("PROBE request template failed");
655                 goto out;
656         }
657
658         trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
659         if (!trigger) {
660                 ret = -ENOMEM;
661                 goto out;
662         }
663
664         /* disable the timeout */
665         trigger->timeout = 0;
666
667         ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
668                               sizeof(*trigger), 0);
669         if (ret < 0) {
670                 wl1271_error("trigger scan to failed for hw scan");
671                 goto out;
672         }
673
674         wl1271_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
675
676         set_bit(WL1271_FLAG_SCANNING, &wl->flags);
677         if (wl1271_11a_enabled()) {
678                 wl->scan.state = band;
679                 if (band == WL1271_SCAN_BAND_DUAL) {
680                         wl->scan.active = active_scan;
681                         wl->scan.high_prio = high_prio;
682                         wl->scan.probe_requests = probe_requests;
683                         if (ssid_len && ssid) {
684                                 wl->scan.ssid_len = ssid_len;
685                                 memcpy(wl->scan.ssid, ssid, ssid_len);
686                         } else
687                                 wl->scan.ssid_len = 0;
688                 }
689         }
690
691         ret = wl1271_cmd_send(wl, CMD_SCAN, params, sizeof(*params), 0);
692         if (ret < 0) {
693                 wl1271_error("SCAN failed");
694                 clear_bit(WL1271_FLAG_SCANNING, &wl->flags);
695                 goto out;
696         }
697
698 out:
699         kfree(params);
700         kfree(trigger);
701         return ret;
702 }
703
704 int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
705                             void *buf, size_t buf_len, int index, u32 rates)
706 {
707         struct wl1271_cmd_template_set *cmd;
708         int ret = 0;
709
710         wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
711
712         WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
713         buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
714
715         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
716         if (!cmd) {
717                 ret = -ENOMEM;
718                 goto out;
719         }
720
721         cmd->len = cpu_to_le16(buf_len);
722         cmd->template_type = template_id;
723         cmd->enabled_rates = cpu_to_le32(rates);
724         cmd->short_retry_limit = wl->conf.tx.rc_conf.short_retry_limit;
725         cmd->long_retry_limit = wl->conf.tx.rc_conf.long_retry_limit;
726         cmd->index = index;
727
728         if (buf)
729                 memcpy(cmd->template_data, buf, buf_len);
730
731         ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
732         if (ret < 0) {
733                 wl1271_warning("cmd set_template failed: %d", ret);
734                 goto out_free;
735         }
736
737 out_free:
738         kfree(cmd);
739
740 out:
741         return ret;
742 }
743
744 int wl1271_cmd_build_null_data(struct wl1271 *wl)
745 {
746         struct sk_buff *skb = NULL;
747         int size;
748         void *ptr;
749         int ret = -ENOMEM;
750
751
752         if (wl->bss_type == BSS_TYPE_IBSS) {
753                 size = sizeof(struct wl12xx_null_data_template);
754                 ptr = NULL;
755         } else {
756                 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
757                 if (!skb)
758                         goto out;
759                 size = skb->len;
760                 ptr = skb->data;
761         }
762
763         ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
764                                       WL1271_RATE_AUTOMATIC);
765
766 out:
767         dev_kfree_skb(skb);
768         if (ret)
769                 wl1271_warning("cmd buld null data failed %d", ret);
770
771         return ret;
772
773 }
774
775 int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
776 {
777         struct sk_buff *skb = NULL;
778         int ret = -ENOMEM;
779
780         skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
781         if (!skb)
782                 goto out;
783
784         ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
785                                       skb->data, skb->len,
786                                       CMD_TEMPL_KLV_IDX_NULL_DATA,
787                                       WL1271_RATE_AUTOMATIC);
788
789 out:
790         dev_kfree_skb(skb);
791         if (ret)
792                 wl1271_warning("cmd build klv null data failed %d", ret);
793
794         return ret;
795
796 }
797
798 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
799 {
800         struct sk_buff *skb;
801         int ret = 0;
802
803         skb = ieee80211_pspoll_get(wl->hw, wl->vif);
804         if (!skb)
805                 goto out;
806
807         ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
808                                       skb->len, 0, wl->basic_rate);
809
810 out:
811         dev_kfree_skb(skb);
812         return ret;
813 }
814
815 int wl1271_cmd_build_probe_req(struct wl1271 *wl,
816                                const u8 *ssid, size_t ssid_len,
817                                const u8 *ie, size_t ie_len, u8 band)
818 {
819         struct sk_buff *skb;
820         int ret;
821
822         skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
823                                      ie, ie_len);
824         if (!skb) {
825                 ret = -ENOMEM;
826                 goto out;
827         }
828
829         wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
830
831         if (band == IEEE80211_BAND_2GHZ)
832                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
833                                               skb->data, skb->len, 0,
834                                               wl->conf.tx.basic_rate);
835         else
836                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
837                                               skb->data, skb->len, 0,
838                                               wl->conf.tx.basic_rate_5);
839
840 out:
841         dev_kfree_skb(skb);
842         return ret;
843 }
844
845 int wl1271_build_qos_null_data(struct wl1271 *wl)
846 {
847         struct ieee80211_qos_hdr template;
848
849         memset(&template, 0, sizeof(template));
850
851         memcpy(template.addr1, wl->bssid, ETH_ALEN);
852         memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
853         memcpy(template.addr3, wl->bssid, ETH_ALEN);
854
855         template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
856                                              IEEE80211_STYPE_QOS_NULLFUNC |
857                                              IEEE80211_FCTL_TODS);
858
859         /* FIXME: not sure what priority to use here */
860         template.qos_ctrl = cpu_to_le16(0);
861
862         return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
863                                        sizeof(template), 0,
864                                        WL1271_RATE_AUTOMATIC);
865 }
866
867 int wl1271_cmd_set_default_wep_key(struct wl1271 *wl, u8 id)
868 {
869         struct wl1271_cmd_set_keys *cmd;
870         int ret = 0;
871
872         wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
873
874         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
875         if (!cmd) {
876                 ret = -ENOMEM;
877                 goto out;
878         }
879
880         cmd->id = id;
881         cmd->key_action = cpu_to_le16(KEY_SET_ID);
882         cmd->key_type = KEY_WEP;
883
884         ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
885         if (ret < 0) {
886                 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
887                 goto out;
888         }
889
890 out:
891         kfree(cmd);
892
893         return ret;
894 }
895
896 int wl1271_cmd_set_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
897                        u8 key_size, const u8 *key, const u8 *addr,
898                        u32 tx_seq_32, u16 tx_seq_16)
899 {
900         struct wl1271_cmd_set_keys *cmd;
901         int ret = 0;
902
903         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
904         if (!cmd) {
905                 ret = -ENOMEM;
906                 goto out;
907         }
908
909         if (key_type != KEY_WEP)
910                 memcpy(cmd->addr, addr, ETH_ALEN);
911
912         cmd->key_action = cpu_to_le16(action);
913         cmd->key_size = key_size;
914         cmd->key_type = key_type;
915
916         cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
917         cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
918
919         /* we have only one SSID profile */
920         cmd->ssid_profile = 0;
921
922         cmd->id = id;
923
924         if (key_type == KEY_TKIP) {
925                 /*
926                  * We get the key in the following form:
927                  * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
928                  * but the target is expecting:
929                  * TKIP - RX MIC - TX MIC
930                  */
931                 memcpy(cmd->key, key, 16);
932                 memcpy(cmd->key + 16, key + 24, 8);
933                 memcpy(cmd->key + 24, key + 16, 8);
934
935         } else {
936                 memcpy(cmd->key, key, key_size);
937         }
938
939         wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
940
941         ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
942         if (ret < 0) {
943                 wl1271_warning("could not set keys");
944         goto out;
945         }
946
947 out:
948         kfree(cmd);
949
950         return ret;
951 }
952
953 int wl1271_cmd_disconnect(struct wl1271 *wl)
954 {
955         struct wl1271_cmd_disconnect *cmd;
956         int ret = 0;
957
958         wl1271_debug(DEBUG_CMD, "cmd disconnect");
959
960         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
961         if (!cmd) {
962                 ret = -ENOMEM;
963                 goto out;
964         }
965
966         cmd->rx_config_options = cpu_to_le32(wl->rx_config);
967         cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
968         /* disconnect reason is not used in immediate disconnections */
969         cmd->type = DISCONNECT_IMMEDIATE;
970
971         ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
972         if (ret < 0) {
973                 wl1271_error("failed to send disconnect command");
974                 goto out_free;
975         }
976
977         ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
978         if (ret < 0)
979                 wl1271_error("cmd disconnect event completion error");
980
981 out_free:
982         kfree(cmd);
983
984 out:
985         return ret;
986 }