]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/net/wireless/wl12xx/wl1271_cmd.c
wl1271: Retrieve device mac address from the nvs file
[mv-sheeva.git] / drivers / net / wireless / wl12xx / wl1271_cmd.c
1 /*
2  * This file is part of wl1271
3  *
4  * Copyright (C) 2009 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
30 #include "wl1271.h"
31 #include "wl1271_reg.h"
32 #include "wl1271_spi.h"
33 #include "wl1271_acx.h"
34 #include "wl12xx_80211.h"
35 #include "wl1271_cmd.h"
36
37 /*
38  * send command to firmware
39  *
40  * @wl: wl struct
41  * @id: command id
42  * @buf: buffer containing the command, must work with dma
43  * @len: length of the buffer
44  */
45 int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
46                     size_t res_len)
47 {
48         struct wl1271_cmd_header *cmd;
49         unsigned long timeout;
50         u32 intr;
51         int ret = 0;
52         u16 status;
53
54         cmd = buf;
55         cmd->id = cpu_to_le16(id);
56         cmd->status = 0;
57
58         WARN_ON(len % 4 != 0);
59
60         wl1271_spi_write(wl, wl->cmd_box_addr, buf, len, false);
61
62         wl1271_spi_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
63
64         timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
65
66         intr = wl1271_spi_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
67         while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
68                 if (time_after(jiffies, timeout)) {
69                         wl1271_error("command complete timeout");
70                         ret = -ETIMEDOUT;
71                         goto out;
72                 }
73
74                 msleep(1);
75
76                 intr = wl1271_spi_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
77         }
78
79         /* read back the status code of the command */
80         if (res_len == 0)
81                 res_len = sizeof(struct wl1271_cmd_header);
82         wl1271_spi_read(wl, wl->cmd_box_addr, cmd, res_len, false);
83
84         status = le16_to_cpu(cmd->status);
85         if (status != CMD_STATUS_SUCCESS) {
86                 wl1271_error("command execute failure %d", status);
87                 ret = -EIO;
88         }
89
90         wl1271_spi_write32(wl, ACX_REG_INTERRUPT_ACK,
91                            WL1271_ACX_INTR_CMD_COMPLETE);
92
93 out:
94         return ret;
95 }
96
97 static int wl1271_cmd_cal_channel_tune(struct wl1271 *wl)
98 {
99         struct wl1271_cmd_cal_channel_tune *cmd;
100         int ret = 0;
101
102         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
103         if (!cmd)
104                 return -ENOMEM;
105
106         cmd->test.id = TEST_CMD_CHANNEL_TUNE;
107
108         cmd->band = WL1271_CHANNEL_TUNE_BAND_2_4;
109         /* set up any channel, 7 is in the middle of the range */
110         cmd->channel = 7;
111
112         ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
113         if (ret < 0)
114                 wl1271_warning("TEST_CMD_CHANNEL_TUNE failed");
115
116         kfree(cmd);
117         return ret;
118 }
119
120 static int wl1271_cmd_cal_update_ref_point(struct wl1271 *wl)
121 {
122         struct wl1271_cmd_cal_update_ref_point *cmd;
123         int ret = 0;
124
125         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
126         if (!cmd)
127                 return -ENOMEM;
128
129         cmd->test.id = TEST_CMD_UPDATE_PD_REFERENCE_POINT;
130
131         /* FIXME: still waiting for the correct values */
132         cmd->ref_power    = 0;
133         cmd->ref_detector = 0;
134
135         cmd->sub_band     = WL1271_PD_REFERENCE_POINT_BAND_B_G;
136
137         ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
138         if (ret < 0)
139                 wl1271_warning("TEST_CMD_UPDATE_PD_REFERENCE_POINT failed");
140
141         kfree(cmd);
142         return ret;
143 }
144
145 static int wl1271_cmd_cal_p2g(struct wl1271 *wl)
146 {
147         struct wl1271_cmd_cal_p2g *cmd;
148         int ret = 0;
149
150         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
151         if (!cmd)
152                 return -ENOMEM;
153
154         cmd->test.id = TEST_CMD_P2G_CAL;
155
156         cmd->sub_band_mask = WL1271_CAL_P2G_BAND_B_G;
157
158         ret = wl1271_cmd_test(wl, cmd, sizeof(*cmd), 0);
159         if (ret < 0)
160                 wl1271_warning("TEST_CMD_P2G_CAL failed");
161
162         kfree(cmd);
163         return ret;
164 }
165
166 static int wl1271_cmd_cal(struct wl1271 *wl)
167 {
168         /*
169          * FIXME: we must make sure that we're not sleeping when calibration
170          * is done
171          */
172         int ret;
173
174         wl1271_notice("performing tx calibration");
175
176         ret = wl1271_cmd_cal_channel_tune(wl);
177         if (ret < 0)
178                 return ret;
179
180         ret = wl1271_cmd_cal_update_ref_point(wl);
181         if (ret < 0)
182                 return ret;
183
184         ret = wl1271_cmd_cal_p2g(wl);
185         if (ret < 0)
186                 return ret;
187
188         return ret;
189 }
190
191 int wl1271_cmd_general_parms(struct wl1271 *wl)
192 {
193         struct wl1271_general_parms_cmd *gen_parms;
194         int ret;
195
196         if (!wl->nvs)
197                 return -ENODEV;
198
199         gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
200         if (!gen_parms)
201                 return -ENOMEM;
202
203         gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
204
205         memcpy(gen_parms->params, wl->nvs->general_params,
206                WL1271_NVS_GENERAL_PARAMS_SIZE);
207
208         ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), 0);
209         if (ret < 0)
210                 wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
211
212         kfree(gen_parms);
213         return ret;
214 }
215
216 int wl1271_cmd_radio_parms(struct wl1271 *wl)
217 {
218         struct wl1271_radio_parms_cmd *radio_parms;
219         struct conf_radio_parms *rparam = &wl->conf.init.radioparam;
220         int ret;
221
222         if (!wl->nvs)
223                 return -ENODEV;
224
225         radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
226         if (!radio_parms)
227                 return -ENOMEM;
228
229         radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
230
231         memcpy(radio_parms->stat_radio_params, wl->nvs->stat_radio_params,
232                WL1271_NVS_STAT_RADIO_PARAMS_SIZE);
233         memcpy(radio_parms->dyn_radio_params,
234                wl->nvs->dyn_radio_params[rparam->fem],
235                WL1271_NVS_DYN_RADIO_PARAMS_SIZE);
236
237         /* FIXME: current NVS is missing 5GHz parameters */
238
239         wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
240                     radio_parms, sizeof(*radio_parms));
241
242         ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
243         if (ret < 0)
244                 wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
245
246         kfree(radio_parms);
247         return ret;
248 }
249
250 int wl1271_cmd_join(struct wl1271 *wl)
251 {
252         static bool do_cal = true;
253         struct wl1271_cmd_join *join;
254         int ret, i;
255         u8 *bssid;
256
257         /* FIXME: remove when we get calibration from the factory */
258         if (do_cal) {
259                 ret = wl1271_cmd_cal(wl);
260                 if (ret < 0)
261                         wl1271_warning("couldn't calibrate");
262                 else
263                         do_cal = false;
264         }
265
266         join = kzalloc(sizeof(*join), GFP_KERNEL);
267         if (!join) {
268                 ret = -ENOMEM;
269                 goto out;
270         }
271
272         wl1271_debug(DEBUG_CMD, "cmd join");
273
274         /* Reverse order BSSID */
275         bssid = (u8 *) &join->bssid_lsb;
276         for (i = 0; i < ETH_ALEN; i++)
277                 bssid[i] = wl->bssid[ETH_ALEN - i - 1];
278
279         join->rx_config_options = cpu_to_le32(wl->rx_config);
280         join->rx_filter_options = cpu_to_le32(wl->rx_filter);
281         join->bss_type = wl->bss_type;
282
283         /*
284          * FIXME: disable temporarily all filters because after commit
285          * 9cef8737 "mac80211: fix managed mode BSSID handling" broke
286          * association. The filter logic needs to be implemented properly
287          * and once that is done, this hack can be removed.
288          */
289         join->rx_config_options = cpu_to_le32(0);
290         join->rx_filter_options = cpu_to_le32(WL1271_DEFAULT_RX_FILTER);
291
292         if (wl->band == IEEE80211_BAND_2GHZ)
293                 join->basic_rate_set = cpu_to_le32(CONF_HW_BIT_RATE_1MBPS   |
294                                                    CONF_HW_BIT_RATE_2MBPS   |
295                                                    CONF_HW_BIT_RATE_5_5MBPS |
296                                                    CONF_HW_BIT_RATE_11MBPS);
297         else {
298                 join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
299                 join->basic_rate_set = cpu_to_le32(CONF_HW_BIT_RATE_6MBPS  |
300                                                    CONF_HW_BIT_RATE_12MBPS |
301                                                    CONF_HW_BIT_RATE_24MBPS);
302         }
303
304         join->beacon_interval = cpu_to_le16(WL1271_DEFAULT_BEACON_INT);
305         join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
306
307         join->channel = wl->channel;
308         join->ssid_len = wl->ssid_len;
309         memcpy(join->ssid, wl->ssid, wl->ssid_len);
310         join->ctrl = WL1271_JOIN_CMD_CTRL_TX_FLUSH;
311
312         /* increment the session counter */
313         wl->session_counter++;
314         if (wl->session_counter >= SESSION_COUNTER_MAX)
315                 wl->session_counter = 0;
316
317         join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
318
319         /* reset TX security counters */
320         wl->tx_security_last_seq = 0;
321         wl->tx_security_seq_16 = 0;
322         wl->tx_security_seq_32 = 0;
323
324         ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
325         if (ret < 0) {
326                 wl1271_error("failed to initiate cmd join");
327                 goto out_free;
328         }
329
330         /*
331          * ugly hack: we should wait for JOIN_EVENT_COMPLETE_ID but to
332          * simplify locking we just sleep instead, for now
333          */
334         msleep(10);
335
336 out_free:
337         kfree(join);
338
339 out:
340         return ret;
341 }
342
343 /**
344  * send test command to firmware
345  *
346  * @wl: wl struct
347  * @buf: buffer containing the command, with all headers, must work with dma
348  * @len: length of the buffer
349  * @answer: is answer needed
350  */
351 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
352 {
353         int ret;
354         size_t res_len = 0;
355
356         wl1271_debug(DEBUG_CMD, "cmd test");
357
358         if (answer)
359                 res_len = buf_len;
360
361         ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
362
363         if (ret < 0) {
364                 wl1271_warning("TEST command failed");
365                 return ret;
366         }
367
368         return ret;
369 }
370
371 /**
372  * read acx from firmware
373  *
374  * @wl: wl struct
375  * @id: acx id
376  * @buf: buffer for the response, including all headers, must work with dma
377  * @len: lenght of buf
378  */
379 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
380 {
381         struct acx_header *acx = buf;
382         int ret;
383
384         wl1271_debug(DEBUG_CMD, "cmd interrogate");
385
386         acx->id = cpu_to_le16(id);
387
388         /* payload length, does not include any headers */
389         acx->len = cpu_to_le16(len - sizeof(*acx));
390
391         ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
392         if (ret < 0)
393                 wl1271_error("INTERROGATE command failed");
394
395         return ret;
396 }
397
398 /**
399  * write acx value to firmware
400  *
401  * @wl: wl struct
402  * @id: acx id
403  * @buf: buffer containing acx, including all headers, must work with dma
404  * @len: length of buf
405  */
406 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
407 {
408         struct acx_header *acx = buf;
409         int ret;
410
411         wl1271_debug(DEBUG_CMD, "cmd configure");
412
413         acx->id = cpu_to_le16(id);
414
415         /* payload length, does not include any headers */
416         acx->len = cpu_to_le16(len - sizeof(*acx));
417
418         ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
419         if (ret < 0) {
420                 wl1271_warning("CONFIGURE command NOK");
421                 return ret;
422         }
423
424         return 0;
425 }
426
427 int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
428 {
429         struct cmd_enabledisable_path *cmd;
430         int ret;
431         u16 cmd_rx, cmd_tx;
432
433         wl1271_debug(DEBUG_CMD, "cmd data path");
434
435         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
436         if (!cmd) {
437                 ret = -ENOMEM;
438                 goto out;
439         }
440
441         /* the channel here is only used for calibration, so hardcoded to 1 */
442         cmd->channel = 1;
443
444         if (enable) {
445                 cmd_rx = CMD_ENABLE_RX;
446                 cmd_tx = CMD_ENABLE_TX;
447         } else {
448                 cmd_rx = CMD_DISABLE_RX;
449                 cmd_tx = CMD_DISABLE_TX;
450         }
451
452         ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
453         if (ret < 0) {
454                 wl1271_error("rx %s cmd for channel %d failed",
455                              enable ? "start" : "stop", cmd->channel);
456                 goto out;
457         }
458
459         wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
460                      enable ? "start" : "stop", cmd->channel);
461
462         ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
463         if (ret < 0) {
464                 wl1271_error("tx %s cmd for channel %d failed",
465                              enable ? "start" : "stop", cmd->channel);
466                 return ret;
467         }
468
469         wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
470                      enable ? "start" : "stop", cmd->channel);
471
472 out:
473         kfree(cmd);
474         return ret;
475 }
476
477 int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode, bool send)
478 {
479         struct wl1271_cmd_ps_params *ps_params = NULL;
480         int ret = 0;
481
482         /* FIXME: this should be in ps.c */
483         ret = wl1271_acx_wake_up_conditions(wl);
484         if (ret < 0) {
485                 wl1271_error("couldn't set wake up conditions");
486                 goto out;
487         }
488
489         wl1271_debug(DEBUG_CMD, "cmd set ps mode");
490
491         ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
492         if (!ps_params) {
493                 ret = -ENOMEM;
494                 goto out;
495         }
496
497         ps_params->ps_mode = ps_mode;
498         ps_params->send_null_data = send;
499         ps_params->retries = 5;
500         ps_params->hang_over_period = 128;
501         ps_params->null_data_rate = cpu_to_le32(1); /* 1 Mbps */
502
503         ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
504                               sizeof(*ps_params), 0);
505         if (ret < 0) {
506                 wl1271_error("cmd set_ps_mode failed");
507                 goto out;
508         }
509
510 out:
511         kfree(ps_params);
512         return ret;
513 }
514
515 int wl1271_cmd_read_memory(struct wl1271 *wl, u32 addr, void *answer,
516                            size_t len)
517 {
518         struct cmd_read_write_memory *cmd;
519         int ret = 0;
520
521         wl1271_debug(DEBUG_CMD, "cmd read memory");
522
523         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
524         if (!cmd) {
525                 ret = -ENOMEM;
526                 goto out;
527         }
528
529         WARN_ON(len > MAX_READ_SIZE);
530         len = min_t(size_t, len, MAX_READ_SIZE);
531
532         cmd->addr = cpu_to_le32(addr);
533         cmd->size = cpu_to_le32(len);
534
535         ret = wl1271_cmd_send(wl, CMD_READ_MEMORY, cmd, sizeof(*cmd),
536                               sizeof(*cmd));
537         if (ret < 0) {
538                 wl1271_error("read memory command failed: %d", ret);
539                 goto out;
540         }
541
542         /* the read command got in */
543         memcpy(answer, cmd->value, len);
544
545 out:
546         kfree(cmd);
547         return ret;
548 }
549
550 int wl1271_cmd_scan(struct wl1271 *wl, u8 *ssid, size_t len,
551                     u8 active_scan, u8 high_prio, u8 band,
552                     u8 probe_requests)
553 {
554
555         struct wl1271_cmd_trigger_scan_to *trigger = NULL;
556         struct wl1271_cmd_scan *params = NULL;
557         struct ieee80211_channel *channels;
558         int i, j, n_ch, ret;
559         u16 scan_options = 0;
560         u8 ieee_band;
561
562         if (band == WL1271_SCAN_BAND_2_4_GHZ)
563                 ieee_band = IEEE80211_BAND_2GHZ;
564         else if (band == WL1271_SCAN_BAND_DUAL && wl1271_11a_enabled())
565                 ieee_band = IEEE80211_BAND_2GHZ;
566         else if (band == WL1271_SCAN_BAND_5_GHZ && wl1271_11a_enabled())
567                 ieee_band = IEEE80211_BAND_5GHZ;
568         else
569                 return -EINVAL;
570
571         if (wl->hw->wiphy->bands[ieee_band]->channels == NULL)
572                 return -EINVAL;
573
574         channels = wl->hw->wiphy->bands[ieee_band]->channels;
575         n_ch = wl->hw->wiphy->bands[ieee_band]->n_channels;
576
577         if (test_bit(WL1271_FLAG_SCANNING, &wl->flags))
578                 return -EINVAL;
579
580         params = kzalloc(sizeof(*params), GFP_KERNEL);
581         if (!params)
582                 return -ENOMEM;
583
584         params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
585         params->params.rx_filter_options =
586                 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
587
588         if (!active_scan)
589                 scan_options |= WL1271_SCAN_OPT_PASSIVE;
590         if (high_prio)
591                 scan_options |= WL1271_SCAN_OPT_PRIORITY_HIGH;
592         params->params.scan_options = cpu_to_le16(scan_options);
593
594         params->params.num_probe_requests = probe_requests;
595         /* Let the fw autodetect suitable tx_rate for probes */
596         params->params.tx_rate = 0;
597         params->params.tid_trigger = 0;
598         params->params.scan_tag = WL1271_SCAN_DEFAULT_TAG;
599
600         if (band == WL1271_SCAN_BAND_DUAL)
601                 params->params.band = WL1271_SCAN_BAND_2_4_GHZ;
602         else
603                 params->params.band = band;
604
605         for (i = 0, j = 0; i < n_ch && i < WL1271_SCAN_MAX_CHANNELS; i++) {
606                 if (!(channels[i].flags & IEEE80211_CHAN_DISABLED)) {
607                         params->channels[j].min_duration =
608                                 cpu_to_le32(WL1271_SCAN_CHAN_MIN_DURATION);
609                         params->channels[j].max_duration =
610                                 cpu_to_le32(WL1271_SCAN_CHAN_MAX_DURATION);
611                         memset(&params->channels[j].bssid_lsb, 0xff, 4);
612                         memset(&params->channels[j].bssid_msb, 0xff, 2);
613                         params->channels[j].early_termination = 0;
614                         params->channels[j].tx_power_att =
615                                 WL1271_SCAN_CURRENT_TX_PWR;
616                         params->channels[j].channel = channels[i].hw_value;
617                         j++;
618                 }
619         }
620
621         params->params.num_channels = j;
622
623         if (len && ssid) {
624                 params->params.ssid_len = len;
625                 memcpy(params->params.ssid, ssid, len);
626         }
627
628         ret = wl1271_cmd_build_probe_req(wl, ssid, len, ieee_band);
629         if (ret < 0) {
630                 wl1271_error("PROBE request template failed");
631                 goto out;
632         }
633
634         trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
635         if (!trigger) {
636                 ret = -ENOMEM;
637                 goto out;
638         }
639
640         /* disable the timeout */
641         trigger->timeout = 0;
642
643         ret = wl1271_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
644                               sizeof(*trigger), 0);
645         if (ret < 0) {
646                 wl1271_error("trigger scan to failed for hw scan");
647                 goto out;
648         }
649
650         wl1271_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
651
652         set_bit(WL1271_FLAG_SCANNING, &wl->flags);
653         if (wl1271_11a_enabled()) {
654                 wl->scan.state = band;
655                 if (band == WL1271_SCAN_BAND_DUAL) {
656                         wl->scan.active = active_scan;
657                         wl->scan.high_prio = high_prio;
658                         wl->scan.probe_requests = probe_requests;
659                         if (len && ssid) {
660                                 wl->scan.ssid_len = len;
661                                 memcpy(wl->scan.ssid, ssid, len);
662                         } else
663                                 wl->scan.ssid_len = 0;
664                 }
665         }
666
667         ret = wl1271_cmd_send(wl, CMD_SCAN, params, sizeof(*params), 0);
668         if (ret < 0) {
669                 wl1271_error("SCAN failed");
670                 clear_bit(WL1271_FLAG_SCANNING, &wl->flags);
671                 goto out;
672         }
673
674 out:
675         kfree(params);
676         return ret;
677 }
678
679 int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
680                             void *buf, size_t buf_len)
681 {
682         struct wl1271_cmd_template_set *cmd;
683         int ret = 0;
684
685         wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
686
687         WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
688         buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
689
690         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
691         if (!cmd) {
692                 ret = -ENOMEM;
693                 goto out;
694         }
695
696         cmd->len = cpu_to_le16(buf_len);
697         cmd->template_type = template_id;
698         cmd->enabled_rates = cpu_to_le32(wl->conf.tx.rc_conf.enabled_rates);
699         cmd->short_retry_limit = wl->conf.tx.rc_conf.short_retry_limit;
700         cmd->long_retry_limit = wl->conf.tx.rc_conf.long_retry_limit;
701
702         if (buf)
703                 memcpy(cmd->template_data, buf, buf_len);
704
705         ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
706         if (ret < 0) {
707                 wl1271_warning("cmd set_template failed: %d", ret);
708                 goto out_free;
709         }
710
711 out_free:
712         kfree(cmd);
713
714 out:
715         return ret;
716 }
717
718 static int wl1271_build_basic_rates(u8 *rates, u8 band)
719 {
720         u8 index = 0;
721
722         if (band == IEEE80211_BAND_2GHZ) {
723                 rates[index++] =
724                         IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
725                 rates[index++] =
726                         IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
727                 rates[index++] =
728                         IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
729                 rates[index++] =
730                         IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
731         } else if (band == IEEE80211_BAND_5GHZ) {
732                 rates[index++] =
733                         IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_6MB;
734                 rates[index++] =
735                         IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_12MB;
736                 rates[index++] =
737                         IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
738         } else {
739                 wl1271_error("build_basic_rates invalid band: %d", band);
740         }
741
742         return index;
743 }
744
745 static int wl1271_build_extended_rates(u8 *rates, u8 band)
746 {
747         u8 index = 0;
748
749         if (band == IEEE80211_BAND_2GHZ) {
750                 rates[index++] = IEEE80211_OFDM_RATE_6MB;
751                 rates[index++] = IEEE80211_OFDM_RATE_9MB;
752                 rates[index++] = IEEE80211_OFDM_RATE_12MB;
753                 rates[index++] = IEEE80211_OFDM_RATE_18MB;
754                 rates[index++] = IEEE80211_OFDM_RATE_24MB;
755                 rates[index++] = IEEE80211_OFDM_RATE_36MB;
756                 rates[index++] = IEEE80211_OFDM_RATE_48MB;
757                 rates[index++] = IEEE80211_OFDM_RATE_54MB;
758         } else if (band == IEEE80211_BAND_5GHZ) {
759                 rates[index++] =
760                         IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_9MB;
761                 rates[index++] =
762                         IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_18MB;
763                 rates[index++] =
764                         IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_24MB;
765                 rates[index++] =
766                         IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_36MB;
767                 rates[index++] =
768                         IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_48MB;
769                 rates[index++] =
770                         IEEE80211_BASIC_RATE_MASK | IEEE80211_OFDM_RATE_54MB;
771         } else {
772                 wl1271_error("build_basic_rates invalid band: %d", band);
773         }
774
775         return index;
776 }
777
778 int wl1271_cmd_build_null_data(struct wl1271 *wl)
779 {
780         struct wl12xx_null_data_template template;
781
782         if (!is_zero_ether_addr(wl->bssid)) {
783                 memcpy(template.header.da, wl->bssid, ETH_ALEN);
784                 memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
785         } else {
786                 memset(template.header.da, 0xff, ETH_ALEN);
787                 memset(template.header.bssid, 0xff, ETH_ALEN);
788         }
789
790         memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
791         template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
792                                                 IEEE80211_STYPE_NULLFUNC |
793                                                 IEEE80211_FCTL_TODS);
794
795         return wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, &template,
796                                        sizeof(template));
797
798 }
799
800 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
801 {
802         struct wl12xx_ps_poll_template template;
803
804         memcpy(template.bssid, wl->bssid, ETH_ALEN);
805         memcpy(template.ta, wl->mac_addr, ETH_ALEN);
806
807         /* aid in PS-Poll has its two MSBs each set to 1 */
808         template.aid = cpu_to_le16(1 << 15 | 1 << 14 | aid);
809
810         template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
811
812         return wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, &template,
813                                        sizeof(template));
814
815 }
816
817 int wl1271_cmd_build_probe_req(struct wl1271 *wl, u8 *ssid, size_t ssid_len,
818                                u8 band)
819 {
820         struct wl12xx_probe_req_template template;
821         struct wl12xx_ie_rates *rates;
822         char *ptr;
823         u16 size;
824         int ret;
825
826         ptr = (char *)&template;
827         size = sizeof(struct ieee80211_header);
828
829         memset(template.header.da, 0xff, ETH_ALEN);
830         memset(template.header.bssid, 0xff, ETH_ALEN);
831         memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
832         template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
833
834         /* IEs */
835         /* SSID */
836         template.ssid.header.id = WLAN_EID_SSID;
837         template.ssid.header.len = ssid_len;
838         if (ssid_len && ssid)
839                 memcpy(template.ssid.ssid, ssid, ssid_len);
840         size += sizeof(struct wl12xx_ie_header) + ssid_len;
841         ptr += size;
842
843         /* Basic Rates */
844         rates = (struct wl12xx_ie_rates *)ptr;
845         rates->header.id = WLAN_EID_SUPP_RATES;
846         rates->header.len = wl1271_build_basic_rates(rates->rates, band);
847         size += sizeof(struct wl12xx_ie_header) + rates->header.len;
848         ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
849
850         /* Extended rates */
851         rates = (struct wl12xx_ie_rates *)ptr;
852         rates->header.id = WLAN_EID_EXT_SUPP_RATES;
853         rates->header.len = wl1271_build_extended_rates(rates->rates, band);
854         size += sizeof(struct wl12xx_ie_header) + rates->header.len;
855
856         wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
857
858         if (band == IEEE80211_BAND_2GHZ)
859                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
860                                               &template, size);
861         else
862                 ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
863                                               &template, size);
864         return ret;
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 out_free:
978         kfree(cmd);
979
980 out:
981         return ret;
982 }