x86: Fix keeping track of AMD C1E
[sfrench/cifs-2.6.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->params, wl->nvs->general_params,
216                WL1271_NVS_GENERAL_PARAMS_SIZE);
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         memcpy(radio_parms->stat_radio_params, wl->nvs->stat_radio_params,
242                WL1271_NVS_STAT_RADIO_PARAMS_SIZE);
243         memcpy(radio_parms->dyn_radio_params,
244                wl->nvs->dyn_radio_params[rparam->fem],
245                WL1271_NVS_DYN_RADIO_PARAMS_SIZE);
246
247         /* FIXME: current NVS is missing 5GHz parameters */
248
249         wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
250                     radio_parms, sizeof(*radio_parms));
251
252         ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
253         if (ret < 0)
254                 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
255
256         kfree(radio_parms);
257         return ret;
258 }
259
260 /*
261  * Poll the mailbox event field until any of the bits in the mask is set or a
262  * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
263  */
264 static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
265 {
266         u32 events_vector, event;
267         unsigned long timeout;
268
269         timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
270
271         do {
272                 if (time_after(jiffies, timeout))
273                         return -ETIMEDOUT;
274
275                 msleep(1);
276
277                 /* read from both event fields */
278                 wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
279                             sizeof(events_vector), false);
280                 event = events_vector & mask;
281                 wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
282                             sizeof(events_vector), false);
283                 event |= events_vector & mask;
284         } while (!event);
285
286         return 0;
287 }
288
289 int wl1271_cmd_join(struct wl1271 *wl, u8 bss_type)
290 {
291         static bool do_cal = true;
292         struct wl1271_cmd_join *join;
293         int ret, i;
294         u8 *bssid;
295
296         /* FIXME: remove when we get calibration from the factory */
297         if (do_cal) {
298                 ret = wl1271_cmd_cal(wl);
299                 if (ret < 0)
300                         wl1271_warning("couldn't calibrate");
301                 else
302                         do_cal = false;
303         }
304
305         join = kzalloc(sizeof(*join), GFP_KERNEL);
306         if (!join) {
307                 ret = -ENOMEM;
308                 goto out;
309         }
310
311         wl1271_debug(DEBUG_CMD, "cmd join");
312
313         /* Reverse order BSSID */
314         bssid = (u8 *) &join->bssid_lsb;
315         for (i = 0; i < ETH_ALEN; i++)
316                 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
317
318         join->rx_config_options = cpu_to_le32(wl->rx_config);
319         join->rx_filter_options = cpu_to_le32(wl->rx_filter);
320         join->bss_type = bss_type;
321         join->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
322
323         if (wl->band == IEEE80211_BAND_5GHZ)
324                 join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
325
326         join->beacon_interval = cpu_to_le16(wl->beacon_int);
327         join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
328
329         join->channel = wl->channel;
330         join->ssid_len = wl->ssid_len;
331         memcpy(join->ssid, wl->ssid, wl->ssid_len);
332         join->ctrl = WL1271_JOIN_CMD_CTRL_TX_FLUSH;
333
334         /* increment the session counter */
335         wl->session_counter++;
336         if (wl->session_counter >= SESSION_COUNTER_MAX)
337                 wl->session_counter = 0;
338
339         join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
340
341         /* reset TX security counters */
342         wl->tx_security_last_seq = 0;
343         wl->tx_security_seq = 0;
344
345         ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
346         if (ret < 0) {
347                 wl1271_error("failed to initiate cmd join");
348                 goto out_free;
349         }
350
351         ret = wl1271_cmd_wait_for_event(wl, JOIN_EVENT_COMPLETE_ID);
352         if (ret < 0)
353                 wl1271_error("cmd join event completion error");
354
355 out_free:
356         kfree(join);
357
358 out:
359         return ret;
360 }
361
362 /**
363  * send test command to firmware
364  *
365  * @wl: wl struct
366  * @buf: buffer containing the command, with all headers, must work with dma
367  * @len: length of the buffer
368  * @answer: is answer needed
369  */
370 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
371 {
372         int ret;
373         size_t res_len = 0;
374
375         wl1271_debug(DEBUG_CMD, "cmd test");
376
377         if (answer)
378                 res_len = buf_len;
379
380         ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
381
382         if (ret < 0) {
383                 wl1271_warning("TEST command failed");
384                 return ret;
385         }
386
387         return ret;
388 }
389
390 /**
391  * read acx from firmware
392  *
393  * @wl: wl struct
394  * @id: acx id
395  * @buf: buffer for the response, including all headers, must work with dma
396  * @len: lenght of buf
397  */
398 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
399 {
400         struct acx_header *acx = buf;
401         int ret;
402
403         wl1271_debug(DEBUG_CMD, "cmd interrogate");
404
405         acx->id = cpu_to_le16(id);
406
407         /* payload length, does not include any headers */
408         acx->len = cpu_to_le16(len - sizeof(*acx));
409
410         ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
411         if (ret < 0)
412                 wl1271_error("INTERROGATE command failed");
413
414         return ret;
415 }
416
417 /**
418  * write acx value to firmware
419  *
420  * @wl: wl struct
421  * @id: acx id
422  * @buf: buffer containing acx, including all headers, must work with dma
423  * @len: length of buf
424  */
425 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
426 {
427         struct acx_header *acx = buf;
428         int ret;
429
430         wl1271_debug(DEBUG_CMD, "cmd configure");
431
432         acx->id = cpu_to_le16(id);
433
434         /* payload length, does not include any headers */
435         acx->len = cpu_to_le16(len - sizeof(*acx));
436
437         ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
438         if (ret < 0) {
439                 wl1271_warning("CONFIGURE command NOK");
440                 return ret;
441         }
442
443         return 0;
444 }
445
446 int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
447 {
448         struct cmd_enabledisable_path *cmd;
449         int ret;
450         u16 cmd_rx, cmd_tx;
451
452         wl1271_debug(DEBUG_CMD, "cmd data path");
453
454         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
455         if (!cmd) {
456                 ret = -ENOMEM;
457                 goto out;
458         }
459
460         /* the channel here is only used for calibration, so hardcoded to 1 */
461         cmd->channel = 1;
462
463         if (enable) {
464                 cmd_rx = CMD_ENABLE_RX;
465                 cmd_tx = CMD_ENABLE_TX;
466         } else {
467                 cmd_rx = CMD_DISABLE_RX;
468                 cmd_tx = CMD_DISABLE_TX;
469         }
470
471         ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
472         if (ret < 0) {
473                 wl1271_error("rx %s cmd for channel %d failed",
474                              enable ? "start" : "stop", cmd->channel);
475                 goto out;
476         }
477
478         wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
479                      enable ? "start" : "stop", cmd->channel);
480
481         ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
482         if (ret < 0) {
483                 wl1271_error("tx %s cmd for channel %d failed",
484                              enable ? "start" : "stop", cmd->channel);
485                 goto out;
486         }
487
488         wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
489                      enable ? "start" : "stop", cmd->channel);
490
491 out:
492         kfree(cmd);
493         return ret;
494 }
495
496 int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode, bool send)
497 {
498         struct wl1271_cmd_ps_params *ps_params = NULL;
499         int ret = 0;
500
501         /* FIXME: this should be in ps.c */
502         ret = wl1271_acx_wake_up_conditions(wl);
503         if (ret < 0) {
504                 wl1271_error("couldn't set wake up conditions");
505                 goto out;
506         }
507
508         wl1271_debug(DEBUG_CMD, "cmd set ps mode");
509
510         ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
511         if (!ps_params) {
512                 ret = -ENOMEM;
513                 goto out;
514         }
515
516         ps_params->ps_mode = ps_mode;
517         ps_params->send_null_data = send;
518         ps_params->retries = 5;
519         ps_params->hang_over_period = 1;
520         ps_params->null_data_rate = cpu_to_le32(1); /* 1 Mbps */
521
522         ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
523                               sizeof(*ps_params), 0);
524         if (ret < 0) {
525                 wl1271_error("cmd set_ps_mode failed");
526                 goto out;
527         }
528
529 out:
530         kfree(ps_params);
531         return ret;
532 }
533
534 int wl1271_cmd_read_memory(struct wl1271 *wl, u32 addr, void *answer,
535                            size_t len)
536 {
537         struct cmd_read_write_memory *cmd;
538         int ret = 0;
539
540         wl1271_debug(DEBUG_CMD, "cmd read memory");
541
542         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
543         if (!cmd) {
544                 ret = -ENOMEM;
545                 goto out;
546         }
547
548         WARN_ON(len > MAX_READ_SIZE);
549         len = min_t(size_t, len, MAX_READ_SIZE);
550
551         cmd->addr = cpu_to_le32(addr);
552         cmd->size = cpu_to_le32(len);
553
554         ret = wl1271_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd),
555                               sizeof(*cmd));
556         if (ret < 0) {
557                 wl1271_error("read memory command failed: %d", ret);
558                 goto out;
559         }
560
561         /* the read command got in */
562         memcpy(answer, cmd->value, len);
563
564 out:
565         kfree(cmd);
566         return ret;
567 }
568
569 int wl1271_cmd_scan(struct wl1271 *wl, const u8 *ssid, size_t ssid_len,
570                     const u8 *ie, size_t ie_len, u8 active_scan,
571                     u8 high_prio, u8 band, u8 probe_requests)
572 {
573
574         struct wl1271_cmd_trigger_scan_to *trigger = NULL;
575         struct wl1271_cmd_scan *params = NULL;
576         struct ieee80211_channel *channels;
577         u32 rate;
578         int i, j, n_ch, ret;
579         u16 scan_options = 0;
580         u8 ieee_band;
581
582         if (band == WL1271_SCAN_BAND_2_4_GHZ) {
583                 ieee_band = IEEE80211_BAND_2GHZ;
584                 rate = wl->conf.tx.basic_rate;
585         } else if (band == WL1271_SCAN_BAND_DUAL && wl1271_11a_enabled()) {
586                 ieee_band = IEEE80211_BAND_2GHZ;
587                 rate = wl->conf.tx.basic_rate;
588         } else if (band == WL1271_SCAN_BAND_5_GHZ && wl1271_11a_enabled()) {
589                 ieee_band = IEEE80211_BAND_5GHZ;
590                 rate = wl->conf.tx.basic_rate_5;
591         } else
592                 return -EINVAL;
593
594         if (wl->hw->wiphy->bands[ieee_band]->channels == NULL)
595                 return -EINVAL;
596
597         channels = wl->hw->wiphy->bands[ieee_band]->channels;
598         n_ch = wl->hw->wiphy->bands[ieee_band]->n_channels;
599
600         if (test_bit(WL1271_FLAG_SCANNING, &wl->flags))
601                 return -EINVAL;
602
603         params = kzalloc(sizeof(*params), GFP_KERNEL);
604         if (!params)
605                 return -ENOMEM;
606
607         params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
608         params->params.rx_filter_options =
609                 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
610
611         if (!active_scan)
612                 scan_options |= WL1271_SCAN_OPT_PASSIVE;
613         if (high_prio)
614                 scan_options |= WL1271_SCAN_OPT_PRIORITY_HIGH;
615         params->params.scan_options = cpu_to_le16(scan_options);
616
617         params->params.num_probe_requests = probe_requests;
618         params->params.tx_rate = cpu_to_le32(rate);
619         params->params.tid_trigger = 0;
620         params->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
621
622         if (band == WL1271_SCAN_BAND_DUAL)
623                 params->params.band = WL1271_SCAN_BAND_2_4_GHZ;
624         else
625                 params->params.band = band;
626
627         for (i = 0, j = 0; i < n_ch && i < WL1271_SCAN_MAX_CHANNELS; i++) {
628                 if (!(channels[i].flags & IEEE80211_CHAN_DISABLED)) {
629                         params->channels[j].min_duration =
630                                 cpu_to_le32(WL1271_SCAN_CHAN_MIN_DURATION);
631                         params->channels[j].max_duration =
632                                 cpu_to_le32(WL1271_SCAN_CHAN_MAX_DURATION);
633                         memset(&params->channels[j].bssid_lsb, 0xff, 4);
634                         memset(&params->channels[j].bssid_msb, 0xff, 2);
635                         params->channels[j].early_termination = 0;
636                         params->channels[j].tx_power_att =
637                                 WL1271_SCAN_CURRENT_TX_PWR;
638                         params->channels[j].channel = channels[i].hw_value;
639                         j++;
640                 }
641         }
642
643         params->params.num_channels = j;
644
645         if (ssid_len && ssid) {
646                 params->params.ssid_len = ssid_len;
647                 memcpy(params->params.ssid, ssid, ssid_len);
648         }
649
650         ret = wl1271_cmd_build_probe_req(wl, ssid, ssid_len,
651                                          ie, ie_len, ieee_band);
652         if (ret < 0) {
653                 wl1271_error("PROBE request template failed");
654                 goto out;
655         }
656
657         trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
658         if (!trigger) {
659                 ret = -ENOMEM;
660                 goto out;
661         }
662
663         /* disable the timeout */
664         trigger->timeout = 0;
665
666         ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
667                               sizeof(*trigger), 0);
668         if (ret < 0) {
669                 wl1271_error("trigger scan to failed for hw scan");
670                 goto out;
671         }
672
673         wl1271_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
674
675         set_bit(WL1271_FLAG_SCANNING, &wl->flags);
676         if (wl1271_11a_enabled()) {
677                 wl->scan.state = band;
678                 if (band == WL1271_SCAN_BAND_DUAL) {
679                         wl->scan.active = active_scan;
680                         wl->scan.high_prio = high_prio;
681                         wl->scan.probe_requests = probe_requests;
682                         if (ssid_len && ssid) {
683                                 wl->scan.ssid_len = ssid_len;
684                                 memcpy(wl->scan.ssid, ssid, ssid_len);
685                         } else
686                                 wl->scan.ssid_len = 0;
687                 }
688         }
689
690         ret = wl1271_cmd_send(wl, CMD_SCAN, params, sizeof(*params), 0);
691         if (ret < 0) {
692                 wl1271_error("SCAN failed");
693                 clear_bit(WL1271_FLAG_SCANNING, &wl->flags);
694                 goto out;
695         }
696
697 out:
698         kfree(params);
699         kfree(trigger);
700         return ret;
701 }
702
703 int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
704                             void *buf, size_t buf_len, int index, u32 rates)
705 {
706         struct wl1271_cmd_template_set *cmd;
707         int ret = 0;
708
709         wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
710
711         WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
712         buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
713
714         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
715         if (!cmd) {
716                 ret = -ENOMEM;
717                 goto out;
718         }
719
720         cmd->len = cpu_to_le16(buf_len);
721         cmd->template_type = template_id;
722         cmd->enabled_rates = cpu_to_le32(rates);
723         cmd->short_retry_limit = wl->conf.tx.rc_conf.short_retry_limit;
724         cmd->long_retry_limit = wl->conf.tx.rc_conf.long_retry_limit;
725         cmd->index = index;
726
727         if (buf)
728                 memcpy(cmd->template_data, buf, buf_len);
729
730         ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
731         if (ret < 0) {
732                 wl1271_warning("cmd set_template failed: %d", ret);
733                 goto out_free;
734         }
735
736 out_free:
737         kfree(cmd);
738
739 out:
740         return ret;
741 }
742
743 int wl1271_cmd_build_null_data(struct wl1271 *wl)
744 {
745         struct sk_buff *skb = NULL;
746         int size;
747         void *ptr;
748         int ret = -ENOMEM;
749
750
751         if (wl->bss_type == BSS_TYPE_IBSS) {
752                 size = sizeof(struct wl12xx_null_data_template);
753                 ptr = NULL;
754         } else {
755                 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
756                 if (!skb)
757                         goto out;
758                 size = skb->len;
759                 ptr = skb->data;
760         }
761
762         ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
763                                       WL1271_RATE_AUTOMATIC);
764
765 out:
766         dev_kfree_skb(skb);
767         if (ret)
768                 wl1271_warning("cmd buld null data failed %d", ret);
769
770         return ret;
771
772 }
773
774 int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
775 {
776         struct sk_buff *skb = NULL;
777         int ret = -ENOMEM;
778
779         skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
780         if (!skb)
781                 goto out;
782
783         ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
784                                       skb->data, skb->len,
785                                       CMD_TEMPL_KLV_IDX_NULL_DATA,
786                                       WL1271_RATE_AUTOMATIC);
787
788 out:
789         dev_kfree_skb(skb);
790         if (ret)
791                 wl1271_warning("cmd build klv null data failed %d", ret);
792
793         return ret;
794
795 }
796
797 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
798 {
799         struct sk_buff *skb;
800         int ret = 0;
801
802         skb = ieee80211_pspoll_get(wl->hw, wl->vif);
803         if (!skb)
804                 goto out;
805
806         ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
807                                       skb->len, 0, wl->basic_rate);
808
809 out:
810         dev_kfree_skb(skb);
811         return ret;
812 }
813
814 int wl1271_cmd_build_probe_req(struct wl1271 *wl,
815                                const u8 *ssid, size_t ssid_len,
816                                const u8 *ie, size_t ie_len, u8 band)
817 {
818         struct sk_buff *skb;
819         int ret;
820
821         skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
822                                      ie, ie_len);
823         if (!skb) {
824                 ret = -ENOMEM;
825                 goto out;
826         }
827
828         wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
829
830         if (band == IEEE80211_BAND_2GHZ)
831                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
832                                               skb->data, skb->len, 0,
833                                               wl->conf.tx.basic_rate);
834         else
835                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
836                                               skb->data, skb->len, 0,
837                                               wl->conf.tx.basic_rate_5);
838
839 out:
840         dev_kfree_skb(skb);
841         return ret;
842 }
843
844 int wl1271_build_qos_null_data(struct wl1271 *wl)
845 {
846         struct ieee80211_qos_hdr template;
847
848         memset(&template, 0, sizeof(template));
849
850         memcpy(template.addr1, wl->bssid, ETH_ALEN);
851         memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
852         memcpy(template.addr3, wl->bssid, ETH_ALEN);
853
854         template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
855                                              IEEE80211_STYPE_QOS_NULLFUNC |
856                                              IEEE80211_FCTL_TODS);
857
858         /* FIXME: not sure what priority to use here */
859         template.qos_ctrl = cpu_to_le16(0);
860
861         return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
862                                        sizeof(template), 0,
863                                        WL1271_RATE_AUTOMATIC);
864 }
865
866 int wl1271_cmd_set_default_wep_key(struct wl1271 *wl, u8 id)
867 {
868         struct wl1271_cmd_set_keys *cmd;
869         int ret = 0;
870
871         wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
872
873         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
874         if (!cmd) {
875                 ret = -ENOMEM;
876                 goto out;
877         }
878
879         cmd->id = id;
880         cmd->key_action = cpu_to_le16(KEY_SET_ID);
881         cmd->key_type = KEY_WEP;
882
883         ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
884         if (ret < 0) {
885                 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
886                 goto out;
887         }
888
889 out:
890         kfree(cmd);
891
892         return ret;
893 }
894
895 int wl1271_cmd_set_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
896                        u8 key_size, const u8 *key, const u8 *addr,
897                        u32 tx_seq_32, u16 tx_seq_16)
898 {
899         struct wl1271_cmd_set_keys *cmd;
900         int ret = 0;
901
902         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
903         if (!cmd) {
904                 ret = -ENOMEM;
905                 goto out;
906         }
907
908         if (key_type != KEY_WEP)
909                 memcpy(cmd->addr, addr, ETH_ALEN);
910
911         cmd->key_action = cpu_to_le16(action);
912         cmd->key_size = key_size;
913         cmd->key_type = key_type;
914
915         cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
916         cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
917
918         /* we have only one SSID profile */
919         cmd->ssid_profile = 0;
920
921         cmd->id = id;
922
923         if (key_type == KEY_TKIP) {
924                 /*
925                  * We get the key in the following form:
926                  * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
927                  * but the target is expecting:
928                  * TKIP - RX MIC - TX MIC
929                  */
930                 memcpy(cmd->key, key, 16);
931                 memcpy(cmd->key + 16, key + 24, 8);
932                 memcpy(cmd->key + 24, key + 16, 8);
933
934         } else {
935                 memcpy(cmd->key, key, key_size);
936         }
937
938         wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
939
940         ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
941         if (ret < 0) {
942                 wl1271_warning("could not set keys");
943         goto out;
944         }
945
946 out:
947         kfree(cmd);
948
949         return ret;
950 }
951
952 int wl1271_cmd_disconnect(struct wl1271 *wl)
953 {
954         struct wl1271_cmd_disconnect *cmd;
955         int ret = 0;
956
957         wl1271_debug(DEBUG_CMD, "cmd disconnect");
958
959         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
960         if (!cmd) {
961                 ret = -ENOMEM;
962                 goto out;
963         }
964
965         cmd->rx_config_options = cpu_to_le32(wl->rx_config);
966         cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
967         /* disconnect reason is not used in immediate disconnections */
968         cmd->type = DISCONNECT_IMMEDIATE;
969
970         ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
971         if (ret < 0) {
972                 wl1271_error("failed to send disconnect command");
973                 goto out_free;
974         }
975
976         ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
977         if (ret < 0)
978                 wl1271_error("cmd disconnect event completion error");
979
980 out_free:
981         kfree(cmd);
982
983 out:
984         return ret;
985 }