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