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