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