1 /*
2  * This file is part of wl1271
3  *
4  * Copyright (C) 2009-2010 Nokia Corporation
5  *
6  * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23 
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/crc7.h>
27 #include <linux/spi/spi.h>
28 #include <linux/etherdevice.h>
29 #include <linux/ieee80211.h>
30 #include <linux/slab.h>
31 
32 #include "wl12xx.h"
33 #include "reg.h"
34 #include "io.h"
35 #include "acx.h"
36 #include "wl12xx_80211.h"
37 #include "cmd.h"
38 #include "event.h"
39 #include "tx.h"
40 
41 #define WL1271_CMD_FAST_POLL_COUNT       50
42 
43 /*
44  * send command to firmware
45  *
46  * @wl: wl struct
47  * @id: command id
48  * @buf: buffer containing the command, must work with dma
49  * @len: length of the buffer
50  */
wl1271_cmd_send(struct wl1271 * wl,u16 id,void * buf,size_t len,size_t res_len)51 int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
52 		    size_t res_len)
53 {
54 	struct wl1271_cmd_header *cmd;
55 	unsigned long timeout;
56 	u32 intr;
57 	int ret = 0;
58 	u16 status;
59 	u16 poll_count = 0;
60 
61 	cmd = buf;
62 	cmd->id = cpu_to_le16(id);
63 	cmd->status = 0;
64 
65 	WARN_ON(len % 4 != 0);
66 	WARN_ON(test_bit(WL1271_FLAG_IN_ELP, &wl->flags));
67 
68 	wl1271_write(wl, wl->cmd_box_addr, buf, len, false);
69 
70 	wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_CMD);
71 
72 	timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
73 
74 	intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
75 	while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
76 		if (time_after(jiffies, timeout)) {
77 			wl1271_error("command complete timeout");
78 			ret = -ETIMEDOUT;
79 			goto out;
80 		}
81 
82 		poll_count++;
83 		if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
84 			udelay(10);
85 		else
86 			msleep(1);
87 
88 		intr = wl1271_read32(wl, ACX_REG_INTERRUPT_NO_CLEAR);
89 	}
90 
91 	/* read back the status code of the command */
92 	if (res_len == 0)
93 		res_len = sizeof(struct wl1271_cmd_header);
94 	wl1271_read(wl, wl->cmd_box_addr, cmd, res_len, false);
95 
96 	status = le16_to_cpu(cmd->status);
97 	if (status != CMD_STATUS_SUCCESS) {
98 		wl1271_error("command execute failure %d", status);
99 		ieee80211_queue_work(wl->hw, &wl->recovery_work);
100 		ret = -EIO;
101 	}
102 
103 	wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
104 		       WL1271_ACX_INTR_CMD_COMPLETE);
105 
106 out:
107 	return ret;
108 }
109 
wl1271_cmd_general_parms(struct wl1271 * wl)110 int wl1271_cmd_general_parms(struct wl1271 *wl)
111 {
112 	struct wl1271_general_parms_cmd *gen_parms;
113 	struct wl1271_ini_general_params *gp = &wl->nvs->general_params;
114 	bool answer = false;
115 	int ret;
116 
117 	if (!wl->nvs)
118 		return -ENODEV;
119 
120 	gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
121 	if (!gen_parms)
122 		return -ENOMEM;
123 
124 	gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
125 
126 	memcpy(&gen_parms->general_params, gp, sizeof(*gp));
127 
128 	if (gp->tx_bip_fem_auto_detect)
129 		answer = true;
130 
131 	ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
132 	if (ret < 0) {
133 		wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
134 		goto out;
135 	}
136 
137 	gp->tx_bip_fem_manufacturer =
138 		gen_parms->general_params.tx_bip_fem_manufacturer;
139 
140 	wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
141 		     answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
142 
143 out:
144 	kfree(gen_parms);
145 	return ret;
146 }
147 
wl1271_cmd_radio_parms(struct wl1271 * wl)148 int wl1271_cmd_radio_parms(struct wl1271 *wl)
149 {
150 	struct wl1271_radio_parms_cmd *radio_parms;
151 	struct wl1271_ini_general_params *gp = &wl->nvs->general_params;
152 	int ret;
153 
154 	if (!wl->nvs)
155 		return -ENODEV;
156 
157 	radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
158 	if (!radio_parms)
159 		return -ENOMEM;
160 
161 	radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
162 
163 	/* 2.4GHz parameters */
164 	memcpy(&radio_parms->static_params_2, &wl->nvs->stat_radio_params_2,
165 	       sizeof(struct wl1271_ini_band_params_2));
166 	memcpy(&radio_parms->dyn_params_2,
167 	       &wl->nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
168 	       sizeof(struct wl1271_ini_fem_params_2));
169 
170 	/* 5GHz parameters */
171 	memcpy(&radio_parms->static_params_5,
172 	       &wl->nvs->stat_radio_params_5,
173 	       sizeof(struct wl1271_ini_band_params_5));
174 	memcpy(&radio_parms->dyn_params_5,
175 	       &wl->nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
176 	       sizeof(struct wl1271_ini_fem_params_5));
177 
178 	wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
179 		    radio_parms, sizeof(*radio_parms));
180 
181 	ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
182 	if (ret < 0)
183 		wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
184 
185 	kfree(radio_parms);
186 	return ret;
187 }
188 
wl1271_cmd_ext_radio_parms(struct wl1271 * wl)189 int wl1271_cmd_ext_radio_parms(struct wl1271 *wl)
190 {
191 	struct wl1271_ext_radio_parms_cmd *ext_radio_parms;
192 	struct conf_rf_settings *rf = &wl->conf.rf;
193 	int ret;
194 
195 	if (!wl->nvs)
196 		return -ENODEV;
197 
198 	ext_radio_parms = kzalloc(sizeof(*ext_radio_parms), GFP_KERNEL);
199 	if (!ext_radio_parms)
200 		return -ENOMEM;
201 
202 	ext_radio_parms->test.id = TEST_CMD_INI_FILE_RF_EXTENDED_PARAM;
203 
204 	memcpy(ext_radio_parms->tx_per_channel_power_compensation_2,
205 	       rf->tx_per_channel_power_compensation_2,
206 	       CONF_TX_PWR_COMPENSATION_LEN_2);
207 	memcpy(ext_radio_parms->tx_per_channel_power_compensation_5,
208 	       rf->tx_per_channel_power_compensation_5,
209 	       CONF_TX_PWR_COMPENSATION_LEN_5);
210 
211 	wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_EXT_RADIO_PARAM: ",
212 		    ext_radio_parms, sizeof(*ext_radio_parms));
213 
214 	ret = wl1271_cmd_test(wl, ext_radio_parms, sizeof(*ext_radio_parms), 0);
215 	if (ret < 0)
216 		wl1271_warning("TEST_CMD_INI_FILE_RF_EXTENDED_PARAM failed");
217 
218 	kfree(ext_radio_parms);
219 	return ret;
220 }
221 
222 /*
223  * Poll the mailbox event field until any of the bits in the mask is set or a
224  * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
225  */
wl1271_cmd_wait_for_event_or_timeout(struct wl1271 * wl,u32 mask)226 static int wl1271_cmd_wait_for_event_or_timeout(struct wl1271 *wl, u32 mask)
227 {
228 	u32 events_vector, event;
229 	unsigned long timeout;
230 
231 	timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
232 
233 	do {
234 		if (time_after(jiffies, timeout)) {
235 			wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
236 				     (int)mask);
237 			return -ETIMEDOUT;
238 		}
239 
240 		msleep(1);
241 
242 		/* read from both event fields */
243 		wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
244 			    sizeof(events_vector), false);
245 		event = events_vector & mask;
246 		wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
247 			    sizeof(events_vector), false);
248 		event |= events_vector & mask;
249 	} while (!event);
250 
251 	return 0;
252 }
253 
wl1271_cmd_wait_for_event(struct wl1271 * wl,u32 mask)254 static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
255 {
256 	int ret;
257 
258 	ret = wl1271_cmd_wait_for_event_or_timeout(wl, mask);
259 	if (ret != 0) {
260 		ieee80211_queue_work(wl->hw, &wl->recovery_work);
261 		return ret;
262 	}
263 
264 	return 0;
265 }
266 
wl1271_cmd_join(struct wl1271 * wl,u8 bss_type)267 int wl1271_cmd_join(struct wl1271 *wl, u8 bss_type)
268 {
269 	struct wl1271_cmd_join *join;
270 	int ret, i;
271 	u8 *bssid;
272 
273 	join = kzalloc(sizeof(*join), GFP_KERNEL);
274 	if (!join) {
275 		ret = -ENOMEM;
276 		goto out;
277 	}
278 
279 	wl1271_debug(DEBUG_CMD, "cmd join");
280 
281 	/* Reverse order BSSID */
282 	bssid = (u8 *) &join->bssid_lsb;
283 	for (i = 0; i < ETH_ALEN; i++)
284 		bssid[i] = wl->bssid[ETH_ALEN - i - 1];
285 
286 	join->rx_config_options = cpu_to_le32(wl->rx_config);
287 	join->rx_filter_options = cpu_to_le32(wl->rx_filter);
288 	join->bss_type = bss_type;
289 	join->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
290 	join->supported_rate_set = cpu_to_le32(wl->rate_set);
291 
292 	if (wl->band == IEEE80211_BAND_5GHZ)
293 		join->bss_type |= WL1271_JOIN_CMD_BSS_TYPE_5GHZ;
294 
295 	join->beacon_interval = cpu_to_le16(wl->beacon_int);
296 	join->dtim_interval = WL1271_DEFAULT_DTIM_PERIOD;
297 
298 	join->channel = wl->channel;
299 	join->ssid_len = wl->ssid_len;
300 	memcpy(join->ssid, wl->ssid, wl->ssid_len);
301 
302 	join->ctrl |= wl->session_counter << WL1271_JOIN_CMD_TX_SESSION_OFFSET;
303 
304 	/* reset TX security counters */
305 	wl->tx_security_last_seq = 0;
306 	wl->tx_security_seq = 0;
307 
308 	wl1271_debug(DEBUG_CMD, "cmd join: basic_rate_set=0x%x, rate_set=0x%x",
309 		join->basic_rate_set, join->supported_rate_set);
310 
311 	ret = wl1271_cmd_send(wl, CMD_START_JOIN, join, sizeof(*join), 0);
312 	if (ret < 0) {
313 		wl1271_error("failed to initiate cmd join");
314 		goto out_free;
315 	}
316 
317 	ret = wl1271_cmd_wait_for_event(wl, JOIN_EVENT_COMPLETE_ID);
318 	if (ret < 0)
319 		wl1271_error("cmd join event completion error");
320 
321 out_free:
322 	kfree(join);
323 
324 out:
325 	return ret;
326 }
327 
328 /**
329  * send test command to firmware
330  *
331  * @wl: wl struct
332  * @buf: buffer containing the command, with all headers, must work with dma
333  * @len: length of the buffer
334  * @answer: is answer needed
335  */
wl1271_cmd_test(struct wl1271 * wl,void * buf,size_t buf_len,u8 answer)336 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
337 {
338 	int ret;
339 	size_t res_len = 0;
340 
341 	wl1271_debug(DEBUG_CMD, "cmd test");
342 
343 	if (answer)
344 		res_len = buf_len;
345 
346 	ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
347 
348 	if (ret < 0) {
349 		wl1271_warning("TEST command failed");
350 		return ret;
351 	}
352 
353 	return ret;
354 }
355 
356 /**
357  * read acx from firmware
358  *
359  * @wl: wl struct
360  * @id: acx id
361  * @buf: buffer for the response, including all headers, must work with dma
362  * @len: length of buf
363  */
wl1271_cmd_interrogate(struct wl1271 * wl,u16 id,void * buf,size_t len)364 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
365 {
366 	struct acx_header *acx = buf;
367 	int ret;
368 
369 	wl1271_debug(DEBUG_CMD, "cmd interrogate");
370 
371 	acx->id = cpu_to_le16(id);
372 
373 	/* payload length, does not include any headers */
374 	acx->len = cpu_to_le16(len - sizeof(*acx));
375 
376 	ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
377 	if (ret < 0)
378 		wl1271_error("INTERROGATE command failed");
379 
380 	return ret;
381 }
382 
383 /**
384  * write acx value to firmware
385  *
386  * @wl: wl struct
387  * @id: acx id
388  * @buf: buffer containing acx, including all headers, must work with dma
389  * @len: length of buf
390  */
wl1271_cmd_configure(struct wl1271 * wl,u16 id,void * buf,size_t len)391 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
392 {
393 	struct acx_header *acx = buf;
394 	int ret;
395 
396 	wl1271_debug(DEBUG_CMD, "cmd configure");
397 
398 	acx->id = cpu_to_le16(id);
399 
400 	/* payload length, does not include any headers */
401 	acx->len = cpu_to_le16(len - sizeof(*acx));
402 
403 	ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
404 	if (ret < 0) {
405 		wl1271_warning("CONFIGURE command NOK");
406 		return ret;
407 	}
408 
409 	return 0;
410 }
411 
wl1271_cmd_data_path(struct wl1271 * wl,bool enable)412 int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
413 {
414 	struct cmd_enabledisable_path *cmd;
415 	int ret;
416 	u16 cmd_rx, cmd_tx;
417 
418 	wl1271_debug(DEBUG_CMD, "cmd data path");
419 
420 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
421 	if (!cmd) {
422 		ret = -ENOMEM;
423 		goto out;
424 	}
425 
426 	/* the channel here is only used for calibration, so hardcoded to 1 */
427 	cmd->channel = 1;
428 
429 	if (enable) {
430 		cmd_rx = CMD_ENABLE_RX;
431 		cmd_tx = CMD_ENABLE_TX;
432 	} else {
433 		cmd_rx = CMD_DISABLE_RX;
434 		cmd_tx = CMD_DISABLE_TX;
435 	}
436 
437 	ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
438 	if (ret < 0) {
439 		wl1271_error("rx %s cmd for channel %d failed",
440 			     enable ? "start" : "stop", cmd->channel);
441 		goto out;
442 	}
443 
444 	wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
445 		     enable ? "start" : "stop", cmd->channel);
446 
447 	ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
448 	if (ret < 0) {
449 		wl1271_error("tx %s cmd for channel %d failed",
450 			     enable ? "start" : "stop", cmd->channel);
451 		goto out;
452 	}
453 
454 	wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
455 		     enable ? "start" : "stop", cmd->channel);
456 
457 out:
458 	kfree(cmd);
459 	return ret;
460 }
461 
wl1271_cmd_ps_mode(struct wl1271 * wl,u8 ps_mode)462 int wl1271_cmd_ps_mode(struct wl1271 *wl, u8 ps_mode)
463 {
464 	struct wl1271_cmd_ps_params *ps_params = NULL;
465 	int ret = 0;
466 
467 	wl1271_debug(DEBUG_CMD, "cmd set ps mode");
468 
469 	ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
470 	if (!ps_params) {
471 		ret = -ENOMEM;
472 		goto out;
473 	}
474 
475 	ps_params->ps_mode = ps_mode;
476 
477 	ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
478 			      sizeof(*ps_params), 0);
479 	if (ret < 0) {
480 		wl1271_error("cmd set_ps_mode failed");
481 		goto out;
482 	}
483 
484 out:
485 	kfree(ps_params);
486 	return ret;
487 }
488 
wl1271_cmd_template_set(struct wl1271 * wl,u16 template_id,void * buf,size_t buf_len,int index,u32 rates)489 int wl1271_cmd_template_set(struct wl1271 *wl, u16 template_id,
490 			    void *buf, size_t buf_len, int index, u32 rates)
491 {
492 	struct wl1271_cmd_template_set *cmd;
493 	int ret = 0;
494 
495 	wl1271_debug(DEBUG_CMD, "cmd template_set %d", template_id);
496 
497 	WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
498 	buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
499 
500 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
501 	if (!cmd) {
502 		ret = -ENOMEM;
503 		goto out;
504 	}
505 
506 	cmd->len = cpu_to_le16(buf_len);
507 	cmd->template_type = template_id;
508 	cmd->enabled_rates = cpu_to_le32(rates);
509 	cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
510 	cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
511 	cmd->index = index;
512 
513 	if (buf)
514 		memcpy(cmd->template_data, buf, buf_len);
515 
516 	ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
517 	if (ret < 0) {
518 		wl1271_warning("cmd set_template failed: %d", ret);
519 		goto out_free;
520 	}
521 
522 out_free:
523 	kfree(cmd);
524 
525 out:
526 	return ret;
527 }
528 
wl1271_cmd_build_null_data(struct wl1271 * wl)529 int wl1271_cmd_build_null_data(struct wl1271 *wl)
530 {
531 	struct sk_buff *skb = NULL;
532 	int size;
533 	void *ptr;
534 	int ret = -ENOMEM;
535 
536 
537 	if (wl->bss_type == BSS_TYPE_IBSS) {
538 		size = sizeof(struct wl12xx_null_data_template);
539 		ptr = NULL;
540 	} else {
541 		skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
542 		if (!skb)
543 			goto out;
544 		size = skb->len;
545 		ptr = skb->data;
546 	}
547 
548 	ret = wl1271_cmd_template_set(wl, CMD_TEMPL_NULL_DATA, ptr, size, 0,
549 				      wl->basic_rate);
550 
551 out:
552 	dev_kfree_skb(skb);
553 	if (ret)
554 		wl1271_warning("cmd buld null data failed %d", ret);
555 
556 	return ret;
557 
558 }
559 
wl1271_cmd_build_klv_null_data(struct wl1271 * wl)560 int wl1271_cmd_build_klv_null_data(struct wl1271 *wl)
561 {
562 	struct sk_buff *skb = NULL;
563 	int ret = -ENOMEM;
564 
565 	skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
566 	if (!skb)
567 		goto out;
568 
569 	ret = wl1271_cmd_template_set(wl, CMD_TEMPL_KLV,
570 				      skb->data, skb->len,
571 				      CMD_TEMPL_KLV_IDX_NULL_DATA,
572 				      wl->basic_rate);
573 
574 out:
575 	dev_kfree_skb(skb);
576 	if (ret)
577 		wl1271_warning("cmd build klv null data failed %d", ret);
578 
579 	return ret;
580 
581 }
582 
wl1271_cmd_build_ps_poll(struct wl1271 * wl,u16 aid)583 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, u16 aid)
584 {
585 	struct sk_buff *skb;
586 	int ret = 0;
587 
588 	skb = ieee80211_pspoll_get(wl->hw, wl->vif);
589 	if (!skb)
590 		goto out;
591 
592 	ret = wl1271_cmd_template_set(wl, CMD_TEMPL_PS_POLL, skb->data,
593 				      skb->len, 0, wl->basic_rate_set);
594 
595 out:
596 	dev_kfree_skb(skb);
597 	return ret;
598 }
599 
wl1271_cmd_build_probe_req(struct wl1271 * wl,const u8 * ssid,size_t ssid_len,const u8 * ie,size_t ie_len,u8 band)600 int wl1271_cmd_build_probe_req(struct wl1271 *wl,
601 			       const u8 *ssid, size_t ssid_len,
602 			       const u8 *ie, size_t ie_len, u8 band)
603 {
604 	struct sk_buff *skb;
605 	int ret;
606 
607 	skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
608 				     ie, ie_len);
609 	if (!skb) {
610 		ret = -ENOMEM;
611 		goto out;
612 	}
613 
614 	wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
615 
616 	if (band == IEEE80211_BAND_2GHZ)
617 		ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
618 					      skb->data, skb->len, 0,
619 					      wl->conf.tx.basic_rate);
620 	else
621 		ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
622 					      skb->data, skb->len, 0,
623 					      wl->conf.tx.basic_rate_5);
624 
625 out:
626 	dev_kfree_skb(skb);
627 	return ret;
628 }
629 
wl1271_cmd_build_ap_probe_req(struct wl1271 * wl,struct sk_buff * skb)630 struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
631 					      struct sk_buff *skb)
632 {
633 	int ret;
634 
635 	if (!skb)
636 		skb = ieee80211_ap_probereq_get(wl->hw, wl->vif);
637 	if (!skb)
638 		goto out;
639 
640 	wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
641 
642 	if (wl->band == IEEE80211_BAND_2GHZ)
643 		ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_2_4,
644 					      skb->data, skb->len, 0,
645 					      wl->conf.tx.basic_rate);
646 	else
647 		ret = wl1271_cmd_template_set(wl, CMD_TEMPL_CFG_PROBE_REQ_5,
648 					      skb->data, skb->len, 0,
649 					      wl->conf.tx.basic_rate_5);
650 
651 	if (ret < 0)
652 		wl1271_error("Unable to set ap probe request template.");
653 
654 out:
655 	return skb;
656 }
657 
wl1271_cmd_build_arp_rsp(struct wl1271 * wl,__be32 ip_addr)658 int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, __be32 ip_addr)
659 {
660 	int ret;
661 	struct wl12xx_arp_rsp_template tmpl;
662 	struct ieee80211_hdr_3addr *hdr;
663 	struct arphdr *arp_hdr;
664 
665 	memset(&tmpl, 0, sizeof(tmpl));
666 
667 	/* mac80211 header */
668 	hdr = &tmpl.hdr;
669 	hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
670 					 IEEE80211_STYPE_DATA |
671 					 IEEE80211_FCTL_TODS);
672 	memcpy(hdr->addr1, wl->vif->bss_conf.bssid, ETH_ALEN);
673 	memcpy(hdr->addr2, wl->vif->addr, ETH_ALEN);
674 	memset(hdr->addr3, 0xff, ETH_ALEN);
675 
676 	/* llc layer */
677 	memcpy(tmpl.llc_hdr, rfc1042_header, sizeof(rfc1042_header));
678 	tmpl.llc_type = cpu_to_be16(ETH_P_ARP);
679 
680 	/* arp header */
681 	arp_hdr = &tmpl.arp_hdr;
682 	arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
683 	arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
684 	arp_hdr->ar_hln = ETH_ALEN;
685 	arp_hdr->ar_pln = 4;
686 	arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
687 
688 	/* arp payload */
689 	memcpy(tmpl.sender_hw, wl->vif->addr, ETH_ALEN);
690 	tmpl.sender_ip = ip_addr;
691 
692 	ret = wl1271_cmd_template_set(wl, CMD_TEMPL_ARP_RSP,
693 				      &tmpl, sizeof(tmpl), 0,
694 				      wl->basic_rate);
695 
696 	return ret;
697 }
698 
wl1271_build_qos_null_data(struct wl1271 * wl)699 int wl1271_build_qos_null_data(struct wl1271 *wl)
700 {
701 	struct ieee80211_qos_hdr template;
702 
703 	memset(&template, 0, sizeof(template));
704 
705 	memcpy(template.addr1, wl->bssid, ETH_ALEN);
706 	memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
707 	memcpy(template.addr3, wl->bssid, ETH_ALEN);
708 
709 	template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
710 					     IEEE80211_STYPE_QOS_NULLFUNC |
711 					     IEEE80211_FCTL_TODS);
712 
713 	/* FIXME: not sure what priority to use here */
714 	template.qos_ctrl = cpu_to_le16(0);
715 
716 	return wl1271_cmd_template_set(wl, CMD_TEMPL_QOS_NULL_DATA, &template,
717 				       sizeof(template), 0,
718 				       wl->basic_rate);
719 }
720 
wl1271_cmd_set_sta_default_wep_key(struct wl1271 * wl,u8 id)721 int wl1271_cmd_set_sta_default_wep_key(struct wl1271 *wl, u8 id)
722 {
723 	struct wl1271_cmd_set_sta_keys *cmd;
724 	int ret = 0;
725 
726 	wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
727 
728 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
729 	if (!cmd) {
730 		ret = -ENOMEM;
731 		goto out;
732 	}
733 
734 	cmd->id = id;
735 	cmd->key_action = cpu_to_le16(KEY_SET_ID);
736 	cmd->key_type = KEY_WEP;
737 
738 	ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
739 	if (ret < 0) {
740 		wl1271_warning("cmd set_default_wep_key failed: %d", ret);
741 		goto out;
742 	}
743 
744 out:
745 	kfree(cmd);
746 
747 	return ret;
748 }
749 
wl1271_cmd_set_ap_default_wep_key(struct wl1271 * wl,u8 id)750 int wl1271_cmd_set_ap_default_wep_key(struct wl1271 *wl, u8 id)
751 {
752 	struct wl1271_cmd_set_ap_keys *cmd;
753 	int ret = 0;
754 
755 	wl1271_debug(DEBUG_CMD, "cmd set_ap_default_wep_key %d", id);
756 
757 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
758 	if (!cmd) {
759 		ret = -ENOMEM;
760 		goto out;
761 	}
762 
763 	cmd->hlid = WL1271_AP_BROADCAST_HLID;
764 	cmd->key_id = id;
765 	cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
766 	cmd->key_action = cpu_to_le16(KEY_SET_ID);
767 	cmd->key_type = KEY_WEP;
768 
769 	ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
770 	if (ret < 0) {
771 		wl1271_warning("cmd set_ap_default_wep_key failed: %d", ret);
772 		goto out;
773 	}
774 
775 out:
776 	kfree(cmd);
777 
778 	return ret;
779 }
780 
wl1271_cmd_set_sta_key(struct wl1271 * wl,u16 action,u8 id,u8 key_type,u8 key_size,const u8 * key,const u8 * addr,u32 tx_seq_32,u16 tx_seq_16)781 int wl1271_cmd_set_sta_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
782 		       u8 key_size, const u8 *key, const u8 *addr,
783 		       u32 tx_seq_32, u16 tx_seq_16)
784 {
785 	struct wl1271_cmd_set_sta_keys *cmd;
786 	int ret = 0;
787 
788 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
789 	if (!cmd) {
790 		ret = -ENOMEM;
791 		goto out;
792 	}
793 
794 	if (key_type != KEY_WEP)
795 		memcpy(cmd->addr, addr, ETH_ALEN);
796 
797 	cmd->key_action = cpu_to_le16(action);
798 	cmd->key_size = key_size;
799 	cmd->key_type = key_type;
800 
801 	cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
802 	cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
803 
804 	/* we have only one SSID profile */
805 	cmd->ssid_profile = 0;
806 
807 	cmd->id = id;
808 
809 	if (key_type == KEY_TKIP) {
810 		/*
811 		 * We get the key in the following form:
812 		 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
813 		 * but the target is expecting:
814 		 * TKIP - RX MIC - TX MIC
815 		 */
816 		memcpy(cmd->key, key, 16);
817 		memcpy(cmd->key + 16, key + 24, 8);
818 		memcpy(cmd->key + 24, key + 16, 8);
819 
820 	} else {
821 		memcpy(cmd->key, key, key_size);
822 	}
823 
824 	wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
825 
826 	ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
827 	if (ret < 0) {
828 		wl1271_warning("could not set keys");
829 	goto out;
830 	}
831 
832 out:
833 	kfree(cmd);
834 
835 	return ret;
836 }
837 
wl1271_cmd_set_ap_key(struct wl1271 * wl,u16 action,u8 id,u8 key_type,u8 key_size,const u8 * key,u8 hlid,u32 tx_seq_32,u16 tx_seq_16)838 int wl1271_cmd_set_ap_key(struct wl1271 *wl, u16 action, u8 id, u8 key_type,
839 			u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
840 			u16 tx_seq_16)
841 {
842 	struct wl1271_cmd_set_ap_keys *cmd;
843 	int ret = 0;
844 	u8 lid_type;
845 
846 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
847 	if (!cmd)
848 		return -ENOMEM;
849 
850 	if (hlid == WL1271_AP_BROADCAST_HLID) {
851 		if (key_type == KEY_WEP)
852 			lid_type = WEP_DEFAULT_LID_TYPE;
853 		else
854 			lid_type = BROADCAST_LID_TYPE;
855 	} else {
856 		lid_type = UNICAST_LID_TYPE;
857 	}
858 
859 	wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
860 		     " hlid: %d", (int)action, (int)id, (int)lid_type,
861 		     (int)key_type, (int)hlid);
862 
863 	cmd->lid_key_type = lid_type;
864 	cmd->hlid = hlid;
865 	cmd->key_action = cpu_to_le16(action);
866 	cmd->key_size = key_size;
867 	cmd->key_type = key_type;
868 	cmd->key_id = id;
869 	cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
870 	cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
871 
872 	if (key_type == KEY_TKIP) {
873 		/*
874 		 * We get the key in the following form:
875 		 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
876 		 * but the target is expecting:
877 		 * TKIP - RX MIC - TX MIC
878 		 */
879 		memcpy(cmd->key, key, 16);
880 		memcpy(cmd->key + 16, key + 24, 8);
881 		memcpy(cmd->key + 24, key + 16, 8);
882 	} else {
883 		memcpy(cmd->key, key, key_size);
884 	}
885 
886 	wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
887 
888 	ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
889 	if (ret < 0) {
890 		wl1271_warning("could not set ap keys");
891 		goto out;
892 	}
893 
894 out:
895 	kfree(cmd);
896 	return ret;
897 }
898 
wl1271_cmd_disconnect(struct wl1271 * wl)899 int wl1271_cmd_disconnect(struct wl1271 *wl)
900 {
901 	struct wl1271_cmd_disconnect *cmd;
902 	int ret = 0;
903 
904 	wl1271_debug(DEBUG_CMD, "cmd disconnect");
905 
906 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
907 	if (!cmd) {
908 		ret = -ENOMEM;
909 		goto out;
910 	}
911 
912 	cmd->rx_config_options = cpu_to_le32(wl->rx_config);
913 	cmd->rx_filter_options = cpu_to_le32(wl->rx_filter);
914 	/* disconnect reason is not used in immediate disconnections */
915 	cmd->type = DISCONNECT_IMMEDIATE;
916 
917 	ret = wl1271_cmd_send(wl, CMD_DISCONNECT, cmd, sizeof(*cmd), 0);
918 	if (ret < 0) {
919 		wl1271_error("failed to send disconnect command");
920 		goto out_free;
921 	}
922 
923 	ret = wl1271_cmd_wait_for_event(wl, DISCONNECT_EVENT_COMPLETE_ID);
924 	if (ret < 0)
925 		wl1271_error("cmd disconnect event completion error");
926 
927 out_free:
928 	kfree(cmd);
929 
930 out:
931 	return ret;
932 }
933 
wl1271_cmd_set_sta_state(struct wl1271 * wl)934 int wl1271_cmd_set_sta_state(struct wl1271 *wl)
935 {
936 	struct wl1271_cmd_set_sta_state *cmd;
937 	int ret = 0;
938 
939 	wl1271_debug(DEBUG_CMD, "cmd set sta state");
940 
941 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
942 	if (!cmd) {
943 		ret = -ENOMEM;
944 		goto out;
945 	}
946 
947 	cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
948 
949 	ret = wl1271_cmd_send(wl, CMD_SET_STA_STATE, cmd, sizeof(*cmd), 0);
950 	if (ret < 0) {
951 		wl1271_error("failed to send set STA state command");
952 		goto out_free;
953 	}
954 
955 out_free:
956 	kfree(cmd);
957 
958 out:
959 	return ret;
960 }
961 
wl1271_cmd_start_bss(struct wl1271 * wl)962 int wl1271_cmd_start_bss(struct wl1271 *wl)
963 {
964 	struct wl1271_cmd_bss_start *cmd;
965 	struct ieee80211_bss_conf *bss_conf = &wl->vif->bss_conf;
966 	int ret;
967 
968 	wl1271_debug(DEBUG_CMD, "cmd start bss");
969 
970 	/*
971 	 * FIXME: We currently do not support hidden SSID. The real SSID
972 	 * should be fetched from mac80211 first.
973 	 */
974 	if (wl->ssid_len == 0) {
975 		wl1271_warning("Hidden SSID currently not supported for AP");
976 		ret = -EINVAL;
977 		goto out;
978 	}
979 
980 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
981 	if (!cmd) {
982 		ret = -ENOMEM;
983 		goto out;
984 	}
985 
986 	memcpy(cmd->bssid, bss_conf->bssid, ETH_ALEN);
987 
988 	cmd->aging_period = cpu_to_le16(WL1271_AP_DEF_INACTIV_SEC);
989 	cmd->bss_index = WL1271_AP_BSS_INDEX;
990 	cmd->global_hlid = WL1271_AP_GLOBAL_HLID;
991 	cmd->broadcast_hlid = WL1271_AP_BROADCAST_HLID;
992 	cmd->basic_rate_set = cpu_to_le32(wl->basic_rate_set);
993 	cmd->beacon_interval = cpu_to_le16(wl->beacon_int);
994 	cmd->dtim_interval = bss_conf->dtim_period;
995 	cmd->beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
996 	cmd->channel = wl->channel;
997 	cmd->ssid_len = wl->ssid_len;
998 	cmd->ssid_type = SSID_TYPE_PUBLIC;
999 	memcpy(cmd->ssid, wl->ssid, wl->ssid_len);
1000 
1001 	switch (wl->band) {
1002 	case IEEE80211_BAND_2GHZ:
1003 		cmd->band = RADIO_BAND_2_4GHZ;
1004 		break;
1005 	case IEEE80211_BAND_5GHZ:
1006 		cmd->band = RADIO_BAND_5GHZ;
1007 		break;
1008 	default:
1009 		wl1271_warning("bss start - unknown band: %d", (int)wl->band);
1010 		cmd->band = RADIO_BAND_2_4GHZ;
1011 		break;
1012 	}
1013 
1014 	ret = wl1271_cmd_send(wl, CMD_BSS_START, cmd, sizeof(*cmd), 0);
1015 	if (ret < 0) {
1016 		wl1271_error("failed to initiate cmd start bss");
1017 		goto out_free;
1018 	}
1019 
1020 out_free:
1021 	kfree(cmd);
1022 
1023 out:
1024 	return ret;
1025 }
1026 
wl1271_cmd_stop_bss(struct wl1271 * wl)1027 int wl1271_cmd_stop_bss(struct wl1271 *wl)
1028 {
1029 	struct wl1271_cmd_bss_start *cmd;
1030 	int ret;
1031 
1032 	wl1271_debug(DEBUG_CMD, "cmd stop bss");
1033 
1034 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1035 	if (!cmd) {
1036 		ret = -ENOMEM;
1037 		goto out;
1038 	}
1039 
1040 	cmd->bss_index = WL1271_AP_BSS_INDEX;
1041 
1042 	ret = wl1271_cmd_send(wl, CMD_BSS_STOP, cmd, sizeof(*cmd), 0);
1043 	if (ret < 0) {
1044 		wl1271_error("failed to initiate cmd stop bss");
1045 		goto out_free;
1046 	}
1047 
1048 out_free:
1049 	kfree(cmd);
1050 
1051 out:
1052 	return ret;
1053 }
1054 
wl1271_cmd_add_sta(struct wl1271 * wl,struct ieee80211_sta * sta,u8 hlid)1055 int wl1271_cmd_add_sta(struct wl1271 *wl, struct ieee80211_sta *sta, u8 hlid)
1056 {
1057 	struct wl1271_cmd_add_sta *cmd;
1058 	int ret;
1059 
1060 	wl1271_debug(DEBUG_CMD, "cmd add sta %d", (int)hlid);
1061 
1062 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1063 	if (!cmd) {
1064 		ret = -ENOMEM;
1065 		goto out;
1066 	}
1067 
1068 	/* currently we don't support UAPSD */
1069 	cmd->sp_len = 0;
1070 
1071 	memcpy(cmd->addr, sta->addr, ETH_ALEN);
1072 	cmd->bss_index = WL1271_AP_BSS_INDEX;
1073 	cmd->aid = sta->aid;
1074 	cmd->hlid = hlid;
1075 
1076 	/*
1077 	 * FIXME: Does STA support QOS? We need to propagate this info from
1078 	 * hostapd. Currently not that important since this is only used for
1079 	 * sending the correct flavor of null-data packet in response to a
1080 	 * trigger.
1081 	 */
1082 	cmd->wmm = 0;
1083 
1084 	cmd->supported_rates = cpu_to_le32(wl1271_tx_enabled_rates_get(wl,
1085 						sta->supp_rates[wl->band]));
1086 
1087 	wl1271_debug(DEBUG_CMD, "new sta rates: 0x%x", cmd->supported_rates);
1088 
1089 	ret = wl1271_cmd_send(wl, CMD_ADD_STA, cmd, sizeof(*cmd), 0);
1090 	if (ret < 0) {
1091 		wl1271_error("failed to initiate cmd add sta");
1092 		goto out_free;
1093 	}
1094 
1095 out_free:
1096 	kfree(cmd);
1097 
1098 out:
1099 	return ret;
1100 }
1101 
wl1271_cmd_remove_sta(struct wl1271 * wl,u8 hlid)1102 int wl1271_cmd_remove_sta(struct wl1271 *wl, u8 hlid)
1103 {
1104 	struct wl1271_cmd_remove_sta *cmd;
1105 	int ret;
1106 
1107 	wl1271_debug(DEBUG_CMD, "cmd remove sta %d", (int)hlid);
1108 
1109 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1110 	if (!cmd) {
1111 		ret = -ENOMEM;
1112 		goto out;
1113 	}
1114 
1115 	cmd->hlid = hlid;
1116 	/* We never send a deauth, mac80211 is in charge of this */
1117 	cmd->reason_opcode = 0;
1118 	cmd->send_deauth_flag = 0;
1119 
1120 	ret = wl1271_cmd_send(wl, CMD_REMOVE_STA, cmd, sizeof(*cmd), 0);
1121 	if (ret < 0) {
1122 		wl1271_error("failed to initiate cmd remove sta");
1123 		goto out_free;
1124 	}
1125 
1126 	/*
1127 	 * We are ok with a timeout here. The event is sometimes not sent
1128 	 * due to a firmware bug.
1129 	 */
1130 	wl1271_cmd_wait_for_event_or_timeout(wl, STA_REMOVE_COMPLETE_EVENT_ID);
1131 
1132 out_free:
1133 	kfree(cmd);
1134 
1135 out:
1136 	return ret;
1137 }
1138