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/spi/spi.h>
27 #include <linux/etherdevice.h>
28 #include <linux/ieee80211.h>
29 #include <linux/slab.h>
30 
31 #include "wl12xx.h"
32 #include "debug.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 fail;
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 		ret = -EIO;
100 		goto fail;
101 	}
102 
103 	wl1271_write32(wl, ACX_REG_INTERRUPT_ACK,
104 		       WL1271_ACX_INTR_CMD_COMPLETE);
105 	return 0;
106 
107 fail:
108 	WARN_ON(1);
109 	wl12xx_queue_recovery_work(wl);
110 	return ret;
111 }
112 
wl1271_cmd_general_parms(struct wl1271 * wl)113 int wl1271_cmd_general_parms(struct wl1271 *wl)
114 {
115 	struct wl1271_general_parms_cmd *gen_parms;
116 	struct wl1271_ini_general_params *gp =
117 		&((struct wl1271_nvs_file *)wl->nvs)->general_params;
118 	bool answer = false;
119 	int ret;
120 
121 	if (!wl->nvs)
122 		return -ENODEV;
123 
124 	if (gp->tx_bip_fem_manufacturer >= WL1271_INI_FEM_MODULE_COUNT) {
125 		wl1271_warning("FEM index from INI out of bounds");
126 		return -EINVAL;
127 	}
128 
129 	gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
130 	if (!gen_parms)
131 		return -ENOMEM;
132 
133 	gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
134 
135 	memcpy(&gen_parms->general_params, gp, sizeof(*gp));
136 
137 	if (gp->tx_bip_fem_auto_detect)
138 		answer = true;
139 
140 	/* Override the REF CLK from the NVS with the one from platform data */
141 	gen_parms->general_params.ref_clock = wl->ref_clock;
142 
143 	ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
144 	if (ret < 0) {
145 		wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
146 		goto out;
147 	}
148 
149 	gp->tx_bip_fem_manufacturer =
150 		gen_parms->general_params.tx_bip_fem_manufacturer;
151 
152 	if (gp->tx_bip_fem_manufacturer >= WL1271_INI_FEM_MODULE_COUNT) {
153 		wl1271_warning("FEM index from FW out of bounds");
154 		ret = -EINVAL;
155 		goto out;
156 	}
157 
158 	wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
159 		     answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
160 
161 out:
162 	kfree(gen_parms);
163 	return ret;
164 }
165 
wl128x_cmd_general_parms(struct wl1271 * wl)166 int wl128x_cmd_general_parms(struct wl1271 *wl)
167 {
168 	struct wl128x_general_parms_cmd *gen_parms;
169 	struct wl128x_ini_general_params *gp =
170 		&((struct wl128x_nvs_file *)wl->nvs)->general_params;
171 	bool answer = false;
172 	int ret;
173 
174 	if (!wl->nvs)
175 		return -ENODEV;
176 
177 	if (gp->tx_bip_fem_manufacturer >= WL1271_INI_FEM_MODULE_COUNT) {
178 		wl1271_warning("FEM index from ini out of bounds");
179 		return -EINVAL;
180 	}
181 
182 	gen_parms = kzalloc(sizeof(*gen_parms), GFP_KERNEL);
183 	if (!gen_parms)
184 		return -ENOMEM;
185 
186 	gen_parms->test.id = TEST_CMD_INI_FILE_GENERAL_PARAM;
187 
188 	memcpy(&gen_parms->general_params, gp, sizeof(*gp));
189 
190 	if (gp->tx_bip_fem_auto_detect)
191 		answer = true;
192 
193 	/* Replace REF and TCXO CLKs with the ones from platform data */
194 	gen_parms->general_params.ref_clock = wl->ref_clock;
195 	gen_parms->general_params.tcxo_ref_clock = wl->tcxo_clock;
196 
197 	ret = wl1271_cmd_test(wl, gen_parms, sizeof(*gen_parms), answer);
198 	if (ret < 0) {
199 		wl1271_warning("CMD_INI_FILE_GENERAL_PARAM failed");
200 		goto out;
201 	}
202 
203 	gp->tx_bip_fem_manufacturer =
204 		gen_parms->general_params.tx_bip_fem_manufacturer;
205 
206 	if (gp->tx_bip_fem_manufacturer >= WL1271_INI_FEM_MODULE_COUNT) {
207 		wl1271_warning("FEM index from FW out of bounds");
208 		ret = -EINVAL;
209 		goto out;
210 	}
211 
212 	wl1271_debug(DEBUG_CMD, "FEM autodetect: %s, manufacturer: %d\n",
213 		     answer ? "auto" : "manual", gp->tx_bip_fem_manufacturer);
214 
215 out:
216 	kfree(gen_parms);
217 	return ret;
218 }
219 
wl1271_cmd_radio_parms(struct wl1271 * wl)220 int wl1271_cmd_radio_parms(struct wl1271 *wl)
221 {
222 	struct wl1271_nvs_file *nvs = (struct wl1271_nvs_file *)wl->nvs;
223 	struct wl1271_radio_parms_cmd *radio_parms;
224 	struct wl1271_ini_general_params *gp = &nvs->general_params;
225 	int ret;
226 
227 	if (!wl->nvs)
228 		return -ENODEV;
229 
230 	radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
231 	if (!radio_parms)
232 		return -ENOMEM;
233 
234 	radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
235 
236 	/* 2.4GHz parameters */
237 	memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
238 	       sizeof(struct wl1271_ini_band_params_2));
239 	memcpy(&radio_parms->dyn_params_2,
240 	       &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
241 	       sizeof(struct wl1271_ini_fem_params_2));
242 
243 	/* 5GHz parameters */
244 	memcpy(&radio_parms->static_params_5,
245 	       &nvs->stat_radio_params_5,
246 	       sizeof(struct wl1271_ini_band_params_5));
247 	memcpy(&radio_parms->dyn_params_5,
248 	       &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
249 	       sizeof(struct wl1271_ini_fem_params_5));
250 
251 	wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
252 		    radio_parms, sizeof(*radio_parms));
253 
254 	ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
255 	if (ret < 0)
256 		wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
257 
258 	kfree(radio_parms);
259 	return ret;
260 }
261 
wl128x_cmd_radio_parms(struct wl1271 * wl)262 int wl128x_cmd_radio_parms(struct wl1271 *wl)
263 {
264 	struct wl128x_nvs_file *nvs = (struct wl128x_nvs_file *)wl->nvs;
265 	struct wl128x_radio_parms_cmd *radio_parms;
266 	struct wl128x_ini_general_params *gp = &nvs->general_params;
267 	int ret;
268 
269 	if (!wl->nvs)
270 		return -ENODEV;
271 
272 	radio_parms = kzalloc(sizeof(*radio_parms), GFP_KERNEL);
273 	if (!radio_parms)
274 		return -ENOMEM;
275 
276 	radio_parms->test.id = TEST_CMD_INI_FILE_RADIO_PARAM;
277 
278 	/* 2.4GHz parameters */
279 	memcpy(&radio_parms->static_params_2, &nvs->stat_radio_params_2,
280 	       sizeof(struct wl128x_ini_band_params_2));
281 	memcpy(&radio_parms->dyn_params_2,
282 	       &nvs->dyn_radio_params_2[gp->tx_bip_fem_manufacturer].params,
283 	       sizeof(struct wl128x_ini_fem_params_2));
284 
285 	/* 5GHz parameters */
286 	memcpy(&radio_parms->static_params_5,
287 	       &nvs->stat_radio_params_5,
288 	       sizeof(struct wl128x_ini_band_params_5));
289 	memcpy(&radio_parms->dyn_params_5,
290 	       &nvs->dyn_radio_params_5[gp->tx_bip_fem_manufacturer].params,
291 	       sizeof(struct wl128x_ini_fem_params_5));
292 
293 	radio_parms->fem_vendor_and_options = nvs->fem_vendor_and_options;
294 
295 	wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_RADIO_PARAM: ",
296 		    radio_parms, sizeof(*radio_parms));
297 
298 	ret = wl1271_cmd_test(wl, radio_parms, sizeof(*radio_parms), 0);
299 	if (ret < 0)
300 		wl1271_warning("CMD_INI_FILE_RADIO_PARAM failed");
301 
302 	kfree(radio_parms);
303 	return ret;
304 }
305 
wl1271_cmd_ext_radio_parms(struct wl1271 * wl)306 int wl1271_cmd_ext_radio_parms(struct wl1271 *wl)
307 {
308 	struct wl1271_ext_radio_parms_cmd *ext_radio_parms;
309 	struct conf_rf_settings *rf = &wl->conf.rf;
310 	int ret;
311 
312 	if (!wl->nvs)
313 		return -ENODEV;
314 
315 	ext_radio_parms = kzalloc(sizeof(*ext_radio_parms), GFP_KERNEL);
316 	if (!ext_radio_parms)
317 		return -ENOMEM;
318 
319 	ext_radio_parms->test.id = TEST_CMD_INI_FILE_RF_EXTENDED_PARAM;
320 
321 	memcpy(ext_radio_parms->tx_per_channel_power_compensation_2,
322 	       rf->tx_per_channel_power_compensation_2,
323 	       CONF_TX_PWR_COMPENSATION_LEN_2);
324 	memcpy(ext_radio_parms->tx_per_channel_power_compensation_5,
325 	       rf->tx_per_channel_power_compensation_5,
326 	       CONF_TX_PWR_COMPENSATION_LEN_5);
327 
328 	wl1271_dump(DEBUG_CMD, "TEST_CMD_INI_FILE_EXT_RADIO_PARAM: ",
329 		    ext_radio_parms, sizeof(*ext_radio_parms));
330 
331 	ret = wl1271_cmd_test(wl, ext_radio_parms, sizeof(*ext_radio_parms), 0);
332 	if (ret < 0)
333 		wl1271_warning("TEST_CMD_INI_FILE_RF_EXTENDED_PARAM failed");
334 
335 	kfree(ext_radio_parms);
336 	return ret;
337 }
338 
339 /*
340  * Poll the mailbox event field until any of the bits in the mask is set or a
341  * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
342  */
wl1271_cmd_wait_for_event_or_timeout(struct wl1271 * wl,u32 mask)343 static int wl1271_cmd_wait_for_event_or_timeout(struct wl1271 *wl, u32 mask)
344 {
345 	u32 events_vector, event;
346 	unsigned long timeout;
347 
348 	timeout = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
349 
350 	do {
351 		if (time_after(jiffies, timeout)) {
352 			wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
353 				     (int)mask);
354 			return -ETIMEDOUT;
355 		}
356 
357 		msleep(1);
358 
359 		/* read from both event fields */
360 		wl1271_read(wl, wl->mbox_ptr[0], &events_vector,
361 			    sizeof(events_vector), false);
362 		event = events_vector & mask;
363 		wl1271_read(wl, wl->mbox_ptr[1], &events_vector,
364 			    sizeof(events_vector), false);
365 		event |= events_vector & mask;
366 	} while (!event);
367 
368 	return 0;
369 }
370 
wl1271_cmd_wait_for_event(struct wl1271 * wl,u32 mask)371 static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
372 {
373 	int ret;
374 
375 	ret = wl1271_cmd_wait_for_event_or_timeout(wl, mask);
376 	if (ret != 0) {
377 		wl12xx_queue_recovery_work(wl);
378 		return ret;
379 	}
380 
381 	return 0;
382 }
383 
wl12xx_cmd_role_enable(struct wl1271 * wl,u8 * addr,u8 role_type,u8 * role_id)384 int wl12xx_cmd_role_enable(struct wl1271 *wl, u8 *addr, u8 role_type,
385 			   u8 *role_id)
386 {
387 	struct wl12xx_cmd_role_enable *cmd;
388 	int ret;
389 
390 	wl1271_debug(DEBUG_CMD, "cmd role enable");
391 
392 	if (WARN_ON(*role_id != WL12XX_INVALID_ROLE_ID))
393 		return -EBUSY;
394 
395 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
396 	if (!cmd) {
397 		ret = -ENOMEM;
398 		goto out;
399 	}
400 
401 	/* get role id */
402 	cmd->role_id = find_first_zero_bit(wl->roles_map, WL12XX_MAX_ROLES);
403 	if (cmd->role_id >= WL12XX_MAX_ROLES) {
404 		ret = -EBUSY;
405 		goto out_free;
406 	}
407 
408 	memcpy(cmd->mac_address, addr, ETH_ALEN);
409 	cmd->role_type = role_type;
410 
411 	ret = wl1271_cmd_send(wl, CMD_ROLE_ENABLE, cmd, sizeof(*cmd), 0);
412 	if (ret < 0) {
413 		wl1271_error("failed to initiate cmd role enable");
414 		goto out_free;
415 	}
416 
417 	__set_bit(cmd->role_id, wl->roles_map);
418 	*role_id = cmd->role_id;
419 
420 out_free:
421 	kfree(cmd);
422 
423 out:
424 	return ret;
425 }
426 
wl12xx_cmd_role_disable(struct wl1271 * wl,u8 * role_id)427 int wl12xx_cmd_role_disable(struct wl1271 *wl, u8 *role_id)
428 {
429 	struct wl12xx_cmd_role_disable *cmd;
430 	int ret;
431 
432 	wl1271_debug(DEBUG_CMD, "cmd role disable");
433 
434 	if (WARN_ON(*role_id == WL12XX_INVALID_ROLE_ID))
435 		return -ENOENT;
436 
437 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
438 	if (!cmd) {
439 		ret = -ENOMEM;
440 		goto out;
441 	}
442 	cmd->role_id = *role_id;
443 
444 	ret = wl1271_cmd_send(wl, CMD_ROLE_DISABLE, cmd, sizeof(*cmd), 0);
445 	if (ret < 0) {
446 		wl1271_error("failed to initiate cmd role disable");
447 		goto out_free;
448 	}
449 
450 	__clear_bit(*role_id, wl->roles_map);
451 	*role_id = WL12XX_INVALID_ROLE_ID;
452 
453 out_free:
454 	kfree(cmd);
455 
456 out:
457 	return ret;
458 }
459 
wl12xx_allocate_link(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 * hlid)460 int wl12xx_allocate_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
461 {
462 	unsigned long flags;
463 	u8 link = find_first_zero_bit(wl->links_map, WL12XX_MAX_LINKS);
464 	if (link >= WL12XX_MAX_LINKS)
465 		return -EBUSY;
466 
467 	/* these bits are used by op_tx */
468 	spin_lock_irqsave(&wl->wl_lock, flags);
469 	__set_bit(link, wl->links_map);
470 	__set_bit(link, wlvif->links_map);
471 	spin_unlock_irqrestore(&wl->wl_lock, flags);
472 	*hlid = link;
473 	return 0;
474 }
475 
wl12xx_free_link(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 * hlid)476 void wl12xx_free_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
477 {
478 	unsigned long flags;
479 
480 	if (*hlid == WL12XX_INVALID_LINK_ID)
481 		return;
482 
483 	/* these bits are used by op_tx */
484 	spin_lock_irqsave(&wl->wl_lock, flags);
485 	__clear_bit(*hlid, wl->links_map);
486 	__clear_bit(*hlid, wlvif->links_map);
487 	spin_unlock_irqrestore(&wl->wl_lock, flags);
488 
489 	/*
490 	 * At this point op_tx() will not add more packets to the queues. We
491 	 * can purge them.
492 	 */
493 	wl1271_tx_reset_link_queues(wl, *hlid);
494 
495 	*hlid = WL12XX_INVALID_LINK_ID;
496 }
497 
wl12xx_get_new_session_id(struct wl1271 * wl,struct wl12xx_vif * wlvif)498 static int wl12xx_get_new_session_id(struct wl1271 *wl,
499 				     struct wl12xx_vif *wlvif)
500 {
501 	if (wlvif->session_counter >= SESSION_COUNTER_MAX)
502 		wlvif->session_counter = 0;
503 
504 	wlvif->session_counter++;
505 
506 	return wlvif->session_counter;
507 }
508 
wl12xx_cmd_role_start_dev(struct wl1271 * wl,struct wl12xx_vif * wlvif)509 static int wl12xx_cmd_role_start_dev(struct wl1271 *wl,
510 				     struct wl12xx_vif *wlvif)
511 {
512 	struct wl12xx_cmd_role_start *cmd;
513 	int ret;
514 
515 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
516 	if (!cmd) {
517 		ret = -ENOMEM;
518 		goto out;
519 	}
520 
521 	wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wlvif->dev_role_id);
522 
523 	cmd->role_id = wlvif->dev_role_id;
524 	if (wlvif->band == IEEE80211_BAND_5GHZ)
525 		cmd->band = WL12XX_BAND_5GHZ;
526 	cmd->channel = wlvif->channel;
527 
528 	if (wlvif->dev_hlid == WL12XX_INVALID_LINK_ID) {
529 		ret = wl12xx_allocate_link(wl, wlvif, &wlvif->dev_hlid);
530 		if (ret)
531 			goto out_free;
532 	}
533 	cmd->device.hlid = wlvif->dev_hlid;
534 	cmd->device.session = wl12xx_get_new_session_id(wl, wlvif);
535 
536 	wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d",
537 		     cmd->role_id, cmd->device.hlid, cmd->device.session);
538 
539 	ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
540 	if (ret < 0) {
541 		wl1271_error("failed to initiate cmd role enable");
542 		goto err_hlid;
543 	}
544 
545 	goto out_free;
546 
547 err_hlid:
548 	/* clear links on error */
549 	wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
550 
551 out_free:
552 	kfree(cmd);
553 
554 out:
555 	return ret;
556 }
557 
wl12xx_cmd_role_stop_dev(struct wl1271 * wl,struct wl12xx_vif * wlvif)558 static int wl12xx_cmd_role_stop_dev(struct wl1271 *wl,
559 				    struct wl12xx_vif *wlvif)
560 {
561 	struct wl12xx_cmd_role_stop *cmd;
562 	int ret;
563 
564 	if (WARN_ON(wlvif->dev_hlid == WL12XX_INVALID_LINK_ID))
565 		return -EINVAL;
566 
567 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
568 	if (!cmd) {
569 		ret = -ENOMEM;
570 		goto out;
571 	}
572 
573 	wl1271_debug(DEBUG_CMD, "cmd role stop dev");
574 
575 	cmd->role_id = wlvif->dev_role_id;
576 	cmd->disc_type = DISCONNECT_IMMEDIATE;
577 	cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
578 
579 	ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
580 	if (ret < 0) {
581 		wl1271_error("failed to initiate cmd role stop");
582 		goto out_free;
583 	}
584 
585 	ret = wl1271_cmd_wait_for_event(wl, ROLE_STOP_COMPLETE_EVENT_ID);
586 	if (ret < 0) {
587 		wl1271_error("cmd role stop dev event completion error");
588 		goto out_free;
589 	}
590 
591 	wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
592 
593 out_free:
594 	kfree(cmd);
595 
596 out:
597 	return ret;
598 }
599 
wl12xx_cmd_role_start_sta(struct wl1271 * wl,struct wl12xx_vif * wlvif)600 int wl12xx_cmd_role_start_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
601 {
602 	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
603 	struct wl12xx_cmd_role_start *cmd;
604 	int ret;
605 
606 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
607 	if (!cmd) {
608 		ret = -ENOMEM;
609 		goto out;
610 	}
611 
612 	wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wlvif->role_id);
613 
614 	cmd->role_id = wlvif->role_id;
615 	if (wlvif->band == IEEE80211_BAND_5GHZ)
616 		cmd->band = WL12XX_BAND_5GHZ;
617 	cmd->channel = wlvif->channel;
618 	cmd->sta.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
619 	cmd->sta.beacon_interval = cpu_to_le16(wlvif->beacon_int);
620 	cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY;
621 	cmd->sta.ssid_len = wlvif->ssid_len;
622 	memcpy(cmd->sta.ssid, wlvif->ssid, wlvif->ssid_len);
623 	memcpy(cmd->sta.bssid, vif->bss_conf.bssid, ETH_ALEN);
624 	cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
625 
626 	if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
627 		ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
628 		if (ret)
629 			goto out_free;
630 	}
631 	cmd->sta.hlid = wlvif->sta.hlid;
632 	cmd->sta.session = wl12xx_get_new_session_id(wl, wlvif);
633 	cmd->sta.remote_rates = cpu_to_le32(wlvif->rate_set);
634 
635 	wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
636 		     "basic_rate_set: 0x%x, remote_rates: 0x%x",
637 		     wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
638 		     wlvif->basic_rate_set, wlvif->rate_set);
639 
640 	ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
641 	if (ret < 0) {
642 		wl1271_error("failed to initiate cmd role start sta");
643 		goto err_hlid;
644 	}
645 
646 	goto out_free;
647 
648 err_hlid:
649 	/* clear links on error. */
650 	wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
651 
652 out_free:
653 	kfree(cmd);
654 
655 out:
656 	return ret;
657 }
658 
659 /* use this function to stop ibss as well */
wl12xx_cmd_role_stop_sta(struct wl1271 * wl,struct wl12xx_vif * wlvif)660 int wl12xx_cmd_role_stop_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
661 {
662 	struct wl12xx_cmd_role_stop *cmd;
663 	int ret;
664 
665 	if (WARN_ON(wlvif->sta.hlid == WL12XX_INVALID_LINK_ID))
666 		return -EINVAL;
667 
668 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
669 	if (!cmd) {
670 		ret = -ENOMEM;
671 		goto out;
672 	}
673 
674 	wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wlvif->role_id);
675 
676 	cmd->role_id = wlvif->role_id;
677 	cmd->disc_type = DISCONNECT_IMMEDIATE;
678 	cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
679 
680 	ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
681 	if (ret < 0) {
682 		wl1271_error("failed to initiate cmd role stop sta");
683 		goto out_free;
684 	}
685 
686 	wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
687 
688 out_free:
689 	kfree(cmd);
690 
691 out:
692 	return ret;
693 }
694 
wl12xx_cmd_role_start_ap(struct wl1271 * wl,struct wl12xx_vif * wlvif)695 int wl12xx_cmd_role_start_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
696 {
697 	struct wl12xx_cmd_role_start *cmd;
698 	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
699 	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
700 	int ret;
701 
702 	wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wlvif->role_id);
703 
704 	/* trying to use hidden SSID with an old hostapd version */
705 	if (wlvif->ssid_len == 0 && !bss_conf->hidden_ssid) {
706 		wl1271_error("got a null SSID from beacon/bss");
707 		ret = -EINVAL;
708 		goto out;
709 	}
710 
711 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
712 	if (!cmd) {
713 		ret = -ENOMEM;
714 		goto out;
715 	}
716 
717 	ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.global_hlid);
718 	if (ret < 0)
719 		goto out_free;
720 
721 	ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.bcast_hlid);
722 	if (ret < 0)
723 		goto out_free_global;
724 
725 	cmd->role_id = wlvif->role_id;
726 	cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
727 	cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
728 	cmd->ap.global_hlid = wlvif->ap.global_hlid;
729 	cmd->ap.broadcast_hlid = wlvif->ap.bcast_hlid;
730 	cmd->ap.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
731 	cmd->ap.beacon_interval = cpu_to_le16(wlvif->beacon_int);
732 	cmd->ap.dtim_interval = bss_conf->dtim_period;
733 	cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
734 	/* FIXME: Change when adding DFS */
735 	cmd->ap.reset_tsf = 1;  /* By default reset AP TSF */
736 	cmd->channel = wlvif->channel;
737 
738 	if (!bss_conf->hidden_ssid) {
739 		/* take the SSID from the beacon for backward compatibility */
740 		cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
741 		cmd->ap.ssid_len = wlvif->ssid_len;
742 		memcpy(cmd->ap.ssid, wlvif->ssid, wlvif->ssid_len);
743 	} else {
744 		cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
745 		cmd->ap.ssid_len = bss_conf->ssid_len;
746 		memcpy(cmd->ap.ssid, bss_conf->ssid, bss_conf->ssid_len);
747 	}
748 
749 	cmd->ap.local_rates = cpu_to_le32(0xffffffff);
750 
751 	switch (wlvif->band) {
752 	case IEEE80211_BAND_2GHZ:
753 		cmd->band = RADIO_BAND_2_4GHZ;
754 		break;
755 	case IEEE80211_BAND_5GHZ:
756 		cmd->band = RADIO_BAND_5GHZ;
757 		break;
758 	default:
759 		wl1271_warning("ap start - unknown band: %d", (int)wlvif->band);
760 		cmd->band = RADIO_BAND_2_4GHZ;
761 		break;
762 	}
763 
764 	ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
765 	if (ret < 0) {
766 		wl1271_error("failed to initiate cmd role start ap");
767 		goto out_free_bcast;
768 	}
769 
770 	goto out_free;
771 
772 out_free_bcast:
773 	wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
774 
775 out_free_global:
776 	wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
777 
778 out_free:
779 	kfree(cmd);
780 
781 out:
782 	return ret;
783 }
784 
wl12xx_cmd_role_stop_ap(struct wl1271 * wl,struct wl12xx_vif * wlvif)785 int wl12xx_cmd_role_stop_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
786 {
787 	struct wl12xx_cmd_role_stop *cmd;
788 	int ret;
789 
790 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
791 	if (!cmd) {
792 		ret = -ENOMEM;
793 		goto out;
794 	}
795 
796 	wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wlvif->role_id);
797 
798 	cmd->role_id = wlvif->role_id;
799 
800 	ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
801 	if (ret < 0) {
802 		wl1271_error("failed to initiate cmd role stop ap");
803 		goto out_free;
804 	}
805 
806 	wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
807 	wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
808 
809 out_free:
810 	kfree(cmd);
811 
812 out:
813 	return ret;
814 }
815 
wl12xx_cmd_role_start_ibss(struct wl1271 * wl,struct wl12xx_vif * wlvif)816 int wl12xx_cmd_role_start_ibss(struct wl1271 *wl, struct wl12xx_vif *wlvif)
817 {
818 	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
819 	struct wl12xx_cmd_role_start *cmd;
820 	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
821 	int ret;
822 
823 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
824 	if (!cmd) {
825 		ret = -ENOMEM;
826 		goto out;
827 	}
828 
829 	wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wlvif->role_id);
830 
831 	cmd->role_id = wlvif->role_id;
832 	if (wlvif->band == IEEE80211_BAND_5GHZ)
833 		cmd->band = WL12XX_BAND_5GHZ;
834 	cmd->channel = wlvif->channel;
835 	cmd->ibss.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
836 	cmd->ibss.beacon_interval = cpu_to_le16(wlvif->beacon_int);
837 	cmd->ibss.dtim_interval = bss_conf->dtim_period;
838 	cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
839 	cmd->ibss.ssid_len = wlvif->ssid_len;
840 	memcpy(cmd->ibss.ssid, wlvif->ssid, wlvif->ssid_len);
841 	memcpy(cmd->ibss.bssid, vif->bss_conf.bssid, ETH_ALEN);
842 	cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
843 
844 	if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
845 		ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
846 		if (ret)
847 			goto out_free;
848 	}
849 	cmd->ibss.hlid = wlvif->sta.hlid;
850 	cmd->ibss.remote_rates = cpu_to_le32(wlvif->rate_set);
851 
852 	wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
853 		     "basic_rate_set: 0x%x, remote_rates: 0x%x",
854 		     wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
855 		     wlvif->basic_rate_set, wlvif->rate_set);
856 
857 	wl1271_debug(DEBUG_CMD, "vif->bss_conf.bssid = %pM",
858 		     vif->bss_conf.bssid);
859 
860 	ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
861 	if (ret < 0) {
862 		wl1271_error("failed to initiate cmd role enable");
863 		goto err_hlid;
864 	}
865 
866 	goto out_free;
867 
868 err_hlid:
869 	/* clear links on error. */
870 	wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
871 
872 out_free:
873 	kfree(cmd);
874 
875 out:
876 	return ret;
877 }
878 
879 
880 /**
881  * send test command to firmware
882  *
883  * @wl: wl struct
884  * @buf: buffer containing the command, with all headers, must work with dma
885  * @len: length of the buffer
886  * @answer: is answer needed
887  */
wl1271_cmd_test(struct wl1271 * wl,void * buf,size_t buf_len,u8 answer)888 int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
889 {
890 	int ret;
891 	size_t res_len = 0;
892 
893 	wl1271_debug(DEBUG_CMD, "cmd test");
894 
895 	if (answer)
896 		res_len = buf_len;
897 
898 	ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
899 
900 	if (ret < 0) {
901 		wl1271_warning("TEST command failed");
902 		return ret;
903 	}
904 
905 	return ret;
906 }
907 
908 /**
909  * read acx from firmware
910  *
911  * @wl: wl struct
912  * @id: acx id
913  * @buf: buffer for the response, including all headers, must work with dma
914  * @len: length of buf
915  */
wl1271_cmd_interrogate(struct wl1271 * wl,u16 id,void * buf,size_t len)916 int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
917 {
918 	struct acx_header *acx = buf;
919 	int ret;
920 
921 	wl1271_debug(DEBUG_CMD, "cmd interrogate");
922 
923 	acx->id = cpu_to_le16(id);
924 
925 	/* payload length, does not include any headers */
926 	acx->len = cpu_to_le16(len - sizeof(*acx));
927 
928 	ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
929 	if (ret < 0)
930 		wl1271_error("INTERROGATE command failed");
931 
932 	return ret;
933 }
934 
935 /**
936  * write acx value to firmware
937  *
938  * @wl: wl struct
939  * @id: acx id
940  * @buf: buffer containing acx, including all headers, must work with dma
941  * @len: length of buf
942  */
wl1271_cmd_configure(struct wl1271 * wl,u16 id,void * buf,size_t len)943 int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
944 {
945 	struct acx_header *acx = buf;
946 	int ret;
947 
948 	wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
949 
950 	acx->id = cpu_to_le16(id);
951 
952 	/* payload length, does not include any headers */
953 	acx->len = cpu_to_le16(len - sizeof(*acx));
954 
955 	ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
956 	if (ret < 0) {
957 		wl1271_warning("CONFIGURE command NOK");
958 		return ret;
959 	}
960 
961 	return 0;
962 }
963 
wl1271_cmd_data_path(struct wl1271 * wl,bool enable)964 int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
965 {
966 	struct cmd_enabledisable_path *cmd;
967 	int ret;
968 	u16 cmd_rx, cmd_tx;
969 
970 	wl1271_debug(DEBUG_CMD, "cmd data path");
971 
972 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
973 	if (!cmd) {
974 		ret = -ENOMEM;
975 		goto out;
976 	}
977 
978 	/* the channel here is only used for calibration, so hardcoded to 1 */
979 	cmd->channel = 1;
980 
981 	if (enable) {
982 		cmd_rx = CMD_ENABLE_RX;
983 		cmd_tx = CMD_ENABLE_TX;
984 	} else {
985 		cmd_rx = CMD_DISABLE_RX;
986 		cmd_tx = CMD_DISABLE_TX;
987 	}
988 
989 	ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
990 	if (ret < 0) {
991 		wl1271_error("rx %s cmd for channel %d failed",
992 			     enable ? "start" : "stop", cmd->channel);
993 		goto out;
994 	}
995 
996 	wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
997 		     enable ? "start" : "stop", cmd->channel);
998 
999 	ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
1000 	if (ret < 0) {
1001 		wl1271_error("tx %s cmd for channel %d failed",
1002 			     enable ? "start" : "stop", cmd->channel);
1003 		goto out;
1004 	}
1005 
1006 	wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
1007 		     enable ? "start" : "stop", cmd->channel);
1008 
1009 out:
1010 	kfree(cmd);
1011 	return ret;
1012 }
1013 
wl1271_cmd_ps_mode(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 ps_mode,u16 auto_ps_timeout)1014 int wl1271_cmd_ps_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1015 		       u8 ps_mode, u16 auto_ps_timeout)
1016 {
1017 	struct wl1271_cmd_ps_params *ps_params = NULL;
1018 	int ret = 0;
1019 
1020 	wl1271_debug(DEBUG_CMD, "cmd set ps mode");
1021 
1022 	ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
1023 	if (!ps_params) {
1024 		ret = -ENOMEM;
1025 		goto out;
1026 	}
1027 
1028 	ps_params->role_id = wlvif->role_id;
1029 	ps_params->ps_mode = ps_mode;
1030 	ps_params->auto_ps_timeout = auto_ps_timeout;
1031 
1032 	ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
1033 			      sizeof(*ps_params), 0);
1034 	if (ret < 0) {
1035 		wl1271_error("cmd set_ps_mode failed");
1036 		goto out;
1037 	}
1038 
1039 out:
1040 	kfree(ps_params);
1041 	return ret;
1042 }
1043 
wl1271_cmd_template_set(struct wl1271 * wl,u8 role_id,u16 template_id,void * buf,size_t buf_len,int index,u32 rates)1044 int wl1271_cmd_template_set(struct wl1271 *wl, u8 role_id,
1045 			    u16 template_id, void *buf, size_t buf_len,
1046 			    int index, u32 rates)
1047 {
1048 	struct wl1271_cmd_template_set *cmd;
1049 	int ret = 0;
1050 
1051 	wl1271_debug(DEBUG_CMD, "cmd template_set %d (role %d)",
1052 		     template_id, role_id);
1053 
1054 	WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
1055 	buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
1056 
1057 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1058 	if (!cmd) {
1059 		ret = -ENOMEM;
1060 		goto out;
1061 	}
1062 
1063 	/* during initialization wlvif is NULL */
1064 	cmd->role_id = role_id;
1065 	cmd->len = cpu_to_le16(buf_len);
1066 	cmd->template_type = template_id;
1067 	cmd->enabled_rates = cpu_to_le32(rates);
1068 	cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
1069 	cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
1070 	cmd->index = index;
1071 
1072 	if (buf)
1073 		memcpy(cmd->template_data, buf, buf_len);
1074 
1075 	ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
1076 	if (ret < 0) {
1077 		wl1271_warning("cmd set_template failed: %d", ret);
1078 		goto out_free;
1079 	}
1080 
1081 out_free:
1082 	kfree(cmd);
1083 
1084 out:
1085 	return ret;
1086 }
1087 
wl12xx_cmd_build_null_data(struct wl1271 * wl,struct wl12xx_vif * wlvif)1088 int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1089 {
1090 	struct sk_buff *skb = NULL;
1091 	int size;
1092 	void *ptr;
1093 	int ret = -ENOMEM;
1094 
1095 
1096 	if (wlvif->bss_type == BSS_TYPE_IBSS) {
1097 		size = sizeof(struct wl12xx_null_data_template);
1098 		ptr = NULL;
1099 	} else {
1100 		skb = ieee80211_nullfunc_get(wl->hw,
1101 					     wl12xx_wlvif_to_vif(wlvif));
1102 		if (!skb)
1103 			goto out;
1104 		size = skb->len;
1105 		ptr = skb->data;
1106 	}
1107 
1108 	ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1109 				      CMD_TEMPL_NULL_DATA, ptr, size, 0,
1110 				      wlvif->basic_rate);
1111 
1112 out:
1113 	dev_kfree_skb(skb);
1114 	if (ret)
1115 		wl1271_warning("cmd buld null data failed %d", ret);
1116 
1117 	return ret;
1118 
1119 }
1120 
wl12xx_cmd_build_klv_null_data(struct wl1271 * wl,struct wl12xx_vif * wlvif)1121 int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl,
1122 				   struct wl12xx_vif *wlvif)
1123 {
1124 	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1125 	struct sk_buff *skb = NULL;
1126 	int ret = -ENOMEM;
1127 
1128 	skb = ieee80211_nullfunc_get(wl->hw, vif);
1129 	if (!skb)
1130 		goto out;
1131 
1132 	ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_KLV,
1133 				      skb->data, skb->len,
1134 				      CMD_TEMPL_KLV_IDX_NULL_DATA,
1135 				      wlvif->basic_rate);
1136 
1137 out:
1138 	dev_kfree_skb(skb);
1139 	if (ret)
1140 		wl1271_warning("cmd build klv null data failed %d", ret);
1141 
1142 	return ret;
1143 
1144 }
1145 
wl1271_cmd_build_ps_poll(struct wl1271 * wl,struct wl12xx_vif * wlvif,u16 aid)1146 int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1147 			     u16 aid)
1148 {
1149 	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1150 	struct sk_buff *skb;
1151 	int ret = 0;
1152 
1153 	skb = ieee80211_pspoll_get(wl->hw, vif);
1154 	if (!skb)
1155 		goto out;
1156 
1157 	ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1158 				      CMD_TEMPL_PS_POLL, skb->data,
1159 				      skb->len, 0, wlvif->basic_rate_set);
1160 
1161 out:
1162 	dev_kfree_skb(skb);
1163 	return ret;
1164 }
1165 
wl12xx_cmd_build_probe_req(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 role_id,u8 band,const u8 * ssid,size_t ssid_len,const u8 * ie,size_t ie_len)1166 int wl12xx_cmd_build_probe_req(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1167 			       u8 role_id, u8 band,
1168 			       const u8 *ssid, size_t ssid_len,
1169 			       const u8 *ie, size_t ie_len)
1170 {
1171 	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1172 	struct sk_buff *skb;
1173 	int ret;
1174 	u32 rate;
1175 
1176 	skb = ieee80211_probereq_get(wl->hw, vif, ssid, ssid_len,
1177 				     ie, ie_len);
1178 	if (!skb) {
1179 		ret = -ENOMEM;
1180 		goto out;
1181 	}
1182 
1183 	wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
1184 
1185 	rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
1186 	if (band == IEEE80211_BAND_2GHZ)
1187 		ret = wl1271_cmd_template_set(wl, role_id,
1188 					      CMD_TEMPL_CFG_PROBE_REQ_2_4,
1189 					      skb->data, skb->len, 0, rate);
1190 	else
1191 		ret = wl1271_cmd_template_set(wl, role_id,
1192 					      CMD_TEMPL_CFG_PROBE_REQ_5,
1193 					      skb->data, skb->len, 0, rate);
1194 
1195 out:
1196 	dev_kfree_skb(skb);
1197 	return ret;
1198 }
1199 
wl1271_cmd_build_ap_probe_req(struct wl1271 * wl,struct wl12xx_vif * wlvif,struct sk_buff * skb)1200 struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
1201 					      struct wl12xx_vif *wlvif,
1202 					      struct sk_buff *skb)
1203 {
1204 	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1205 	int ret;
1206 	u32 rate;
1207 
1208 	if (!skb)
1209 		skb = ieee80211_ap_probereq_get(wl->hw, vif);
1210 	if (!skb)
1211 		goto out;
1212 
1213 	wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
1214 
1215 	rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[wlvif->band]);
1216 	if (wlvif->band == IEEE80211_BAND_2GHZ)
1217 		ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1218 					      CMD_TEMPL_CFG_PROBE_REQ_2_4,
1219 					      skb->data, skb->len, 0, rate);
1220 	else
1221 		ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1222 					      CMD_TEMPL_CFG_PROBE_REQ_5,
1223 					      skb->data, skb->len, 0, rate);
1224 
1225 	if (ret < 0)
1226 		wl1271_error("Unable to set ap probe request template.");
1227 
1228 out:
1229 	return skb;
1230 }
1231 
wl1271_cmd_build_arp_rsp(struct wl1271 * wl,struct wl12xx_vif * wlvif)1232 int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1233 {
1234 	int ret, extra;
1235 	u16 fc;
1236 	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
1237 	struct sk_buff *skb;
1238 	struct wl12xx_arp_rsp_template *tmpl;
1239 	struct ieee80211_hdr_3addr *hdr;
1240 	struct arphdr *arp_hdr;
1241 
1242 	skb = dev_alloc_skb(sizeof(*hdr) + sizeof(__le16) + sizeof(*tmpl) +
1243 			    WL1271_EXTRA_SPACE_MAX);
1244 	if (!skb) {
1245 		wl1271_error("failed to allocate buffer for arp rsp template");
1246 		return -ENOMEM;
1247 	}
1248 
1249 	skb_reserve(skb, sizeof(*hdr) + WL1271_EXTRA_SPACE_MAX);
1250 
1251 	tmpl = (struct wl12xx_arp_rsp_template *)skb_put(skb, sizeof(*tmpl));
1252 	memset(tmpl, 0, sizeof(tmpl));
1253 
1254 	/* llc layer */
1255 	memcpy(tmpl->llc_hdr, rfc1042_header, sizeof(rfc1042_header));
1256 	tmpl->llc_type = cpu_to_be16(ETH_P_ARP);
1257 
1258 	/* arp header */
1259 	arp_hdr = &tmpl->arp_hdr;
1260 	arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
1261 	arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
1262 	arp_hdr->ar_hln = ETH_ALEN;
1263 	arp_hdr->ar_pln = 4;
1264 	arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
1265 
1266 	/* arp payload */
1267 	memcpy(tmpl->sender_hw, vif->addr, ETH_ALEN);
1268 	tmpl->sender_ip = wlvif->ip_addr;
1269 
1270 	/* encryption space */
1271 	switch (wlvif->encryption_type) {
1272 	case KEY_TKIP:
1273 		extra = WL1271_EXTRA_SPACE_TKIP;
1274 		break;
1275 	case KEY_AES:
1276 		extra = WL1271_EXTRA_SPACE_AES;
1277 		break;
1278 	case KEY_NONE:
1279 	case KEY_WEP:
1280 	case KEY_GEM:
1281 		extra = 0;
1282 		break;
1283 	default:
1284 		wl1271_warning("Unknown encryption type: %d",
1285 			       wlvif->encryption_type);
1286 		ret = -EINVAL;
1287 		goto out;
1288 	}
1289 
1290 	if (extra) {
1291 		u8 *space = skb_push(skb, extra);
1292 		memset(space, 0, extra);
1293 	}
1294 
1295 	/* QoS header - BE */
1296 	if (wlvif->sta.qos)
1297 		memset(skb_push(skb, sizeof(__le16)), 0, sizeof(__le16));
1298 
1299 	/* mac80211 header */
1300 	hdr = (struct ieee80211_hdr_3addr *)skb_push(skb, sizeof(*hdr));
1301 	memset(hdr, 0, sizeof(hdr));
1302 	fc = IEEE80211_FTYPE_DATA | IEEE80211_FCTL_TODS;
1303 	if (wlvif->sta.qos)
1304 		fc |= IEEE80211_STYPE_QOS_DATA;
1305 	else
1306 		fc |= IEEE80211_STYPE_DATA;
1307 	if (wlvif->encryption_type != KEY_NONE)
1308 		fc |= IEEE80211_FCTL_PROTECTED;
1309 
1310 	hdr->frame_control = cpu_to_le16(fc);
1311 	memcpy(hdr->addr1, vif->bss_conf.bssid, ETH_ALEN);
1312 	memcpy(hdr->addr2, vif->addr, ETH_ALEN);
1313 	memset(hdr->addr3, 0xff, ETH_ALEN);
1314 
1315 	ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_ARP_RSP,
1316 				      skb->data, skb->len, 0,
1317 				      wlvif->basic_rate);
1318 out:
1319 	dev_kfree_skb(skb);
1320 	return ret;
1321 }
1322 
wl1271_build_qos_null_data(struct wl1271 * wl,struct ieee80211_vif * vif)1323 int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
1324 {
1325 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
1326 	struct ieee80211_qos_hdr template;
1327 
1328 	memset(&template, 0, sizeof(template));
1329 
1330 	memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN);
1331 	memcpy(template.addr2, vif->addr, ETH_ALEN);
1332 	memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN);
1333 
1334 	template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1335 					     IEEE80211_STYPE_QOS_NULLFUNC |
1336 					     IEEE80211_FCTL_TODS);
1337 
1338 	/* FIXME: not sure what priority to use here */
1339 	template.qos_ctrl = cpu_to_le16(0);
1340 
1341 	return wl1271_cmd_template_set(wl, wlvif->role_id,
1342 				       CMD_TEMPL_QOS_NULL_DATA, &template,
1343 				       sizeof(template), 0,
1344 				       wlvif->basic_rate);
1345 }
1346 
wl12xx_cmd_set_default_wep_key(struct wl1271 * wl,u8 id,u8 hlid)1347 int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
1348 {
1349 	struct wl1271_cmd_set_keys *cmd;
1350 	int ret = 0;
1351 
1352 	wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
1353 
1354 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1355 	if (!cmd) {
1356 		ret = -ENOMEM;
1357 		goto out;
1358 	}
1359 
1360 	cmd->hlid = hlid;
1361 	cmd->key_id = id;
1362 	cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1363 	cmd->key_action = cpu_to_le16(KEY_SET_ID);
1364 	cmd->key_type = KEY_WEP;
1365 
1366 	ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1367 	if (ret < 0) {
1368 		wl1271_warning("cmd set_default_wep_key failed: %d", ret);
1369 		goto out;
1370 	}
1371 
1372 out:
1373 	kfree(cmd);
1374 
1375 	return ret;
1376 }
1377 
wl1271_cmd_set_sta_key(struct wl1271 * wl,struct wl12xx_vif * wlvif,u16 action,u8 id,u8 key_type,u8 key_size,const u8 * key,const u8 * addr,u32 tx_seq_32,u16 tx_seq_16)1378 int wl1271_cmd_set_sta_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1379 		       u16 action, u8 id, u8 key_type,
1380 		       u8 key_size, const u8 *key, const u8 *addr,
1381 		       u32 tx_seq_32, u16 tx_seq_16)
1382 {
1383 	struct wl1271_cmd_set_keys *cmd;
1384 	int ret = 0;
1385 
1386 	/* hlid might have already been deleted */
1387 	if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
1388 		return 0;
1389 
1390 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1391 	if (!cmd) {
1392 		ret = -ENOMEM;
1393 		goto out;
1394 	}
1395 
1396 	cmd->hlid = wlvif->sta.hlid;
1397 
1398 	if (key_type == KEY_WEP)
1399 		cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1400 	else if (is_broadcast_ether_addr(addr))
1401 		cmd->lid_key_type = BROADCAST_LID_TYPE;
1402 	else
1403 		cmd->lid_key_type = UNICAST_LID_TYPE;
1404 
1405 	cmd->key_action = cpu_to_le16(action);
1406 	cmd->key_size = key_size;
1407 	cmd->key_type = key_type;
1408 
1409 	cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1410 	cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1411 
1412 	cmd->key_id = id;
1413 
1414 	if (key_type == KEY_TKIP) {
1415 		/*
1416 		 * We get the key in the following form:
1417 		 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1418 		 * but the target is expecting:
1419 		 * TKIP - RX MIC - TX MIC
1420 		 */
1421 		memcpy(cmd->key, key, 16);
1422 		memcpy(cmd->key + 16, key + 24, 8);
1423 		memcpy(cmd->key + 24, key + 16, 8);
1424 
1425 	} else {
1426 		memcpy(cmd->key, key, key_size);
1427 	}
1428 
1429 	wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1430 
1431 	ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1432 	if (ret < 0) {
1433 		wl1271_warning("could not set keys");
1434 	goto out;
1435 	}
1436 
1437 out:
1438 	kfree(cmd);
1439 
1440 	return ret;
1441 }
1442 
1443 /*
1444  * TODO: merge with sta/ibss into 1 set_key function.
1445  * note there are slight diffs
1446  */
wl1271_cmd_set_ap_key(struct wl1271 * wl,struct wl12xx_vif * wlvif,u16 action,u8 id,u8 key_type,u8 key_size,const u8 * key,u8 hlid,u32 tx_seq_32,u16 tx_seq_16)1447 int wl1271_cmd_set_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1448 			  u16 action, u8 id, u8 key_type,
1449 			  u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
1450 			  u16 tx_seq_16)
1451 {
1452 	struct wl1271_cmd_set_keys *cmd;
1453 	int ret = 0;
1454 	u8 lid_type;
1455 
1456 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1457 	if (!cmd)
1458 		return -ENOMEM;
1459 
1460 	if (hlid == wlvif->ap.bcast_hlid) {
1461 		if (key_type == KEY_WEP)
1462 			lid_type = WEP_DEFAULT_LID_TYPE;
1463 		else
1464 			lid_type = BROADCAST_LID_TYPE;
1465 	} else {
1466 		lid_type = UNICAST_LID_TYPE;
1467 	}
1468 
1469 	wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
1470 		     " hlid: %d", (int)action, (int)id, (int)lid_type,
1471 		     (int)key_type, (int)hlid);
1472 
1473 	cmd->lid_key_type = lid_type;
1474 	cmd->hlid = hlid;
1475 	cmd->key_action = cpu_to_le16(action);
1476 	cmd->key_size = key_size;
1477 	cmd->key_type = key_type;
1478 	cmd->key_id = id;
1479 	cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1480 	cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1481 
1482 	if (key_type == KEY_TKIP) {
1483 		/*
1484 		 * We get the key in the following form:
1485 		 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1486 		 * but the target is expecting:
1487 		 * TKIP - RX MIC - TX MIC
1488 		 */
1489 		memcpy(cmd->key, key, 16);
1490 		memcpy(cmd->key + 16, key + 24, 8);
1491 		memcpy(cmd->key + 24, key + 16, 8);
1492 	} else {
1493 		memcpy(cmd->key, key, key_size);
1494 	}
1495 
1496 	wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
1497 
1498 	ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1499 	if (ret < 0) {
1500 		wl1271_warning("could not set ap keys");
1501 		goto out;
1502 	}
1503 
1504 out:
1505 	kfree(cmd);
1506 	return ret;
1507 }
1508 
wl12xx_cmd_set_peer_state(struct wl1271 * wl,u8 hlid)1509 int wl12xx_cmd_set_peer_state(struct wl1271 *wl, u8 hlid)
1510 {
1511 	struct wl12xx_cmd_set_peer_state *cmd;
1512 	int ret = 0;
1513 
1514 	wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
1515 
1516 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1517 	if (!cmd) {
1518 		ret = -ENOMEM;
1519 		goto out;
1520 	}
1521 
1522 	cmd->hlid = hlid;
1523 	cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1524 
1525 	ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
1526 	if (ret < 0) {
1527 		wl1271_error("failed to send set peer state command");
1528 		goto out_free;
1529 	}
1530 
1531 out_free:
1532 	kfree(cmd);
1533 
1534 out:
1535 	return ret;
1536 }
1537 
wl12xx_cmd_add_peer(struct wl1271 * wl,struct wl12xx_vif * wlvif,struct ieee80211_sta * sta,u8 hlid)1538 int wl12xx_cmd_add_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1539 			struct ieee80211_sta *sta, u8 hlid)
1540 {
1541 	struct wl12xx_cmd_add_peer *cmd;
1542 	int i, ret;
1543 	u32 sta_rates;
1544 
1545 	wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
1546 
1547 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1548 	if (!cmd) {
1549 		ret = -ENOMEM;
1550 		goto out;
1551 	}
1552 
1553 	memcpy(cmd->addr, sta->addr, ETH_ALEN);
1554 	cmd->bss_index = WL1271_AP_BSS_INDEX;
1555 	cmd->aid = sta->aid;
1556 	cmd->hlid = hlid;
1557 	cmd->sp_len = sta->max_sp;
1558 	cmd->wmm = sta->wme ? 1 : 0;
1559 
1560 	for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
1561 		if (sta->wme && (sta->uapsd_queues & BIT(i)))
1562 			cmd->psd_type[i] = WL1271_PSD_UPSD_TRIGGER;
1563 		else
1564 			cmd->psd_type[i] = WL1271_PSD_LEGACY;
1565 
1566 	sta_rates = sta->supp_rates[wlvif->band];
1567 	if (sta->ht_cap.ht_supported)
1568 		sta_rates |= sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET;
1569 
1570 	cmd->supported_rates =
1571 		cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
1572 							wlvif->band));
1573 
1574 	wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
1575 		     cmd->supported_rates, sta->uapsd_queues);
1576 
1577 	ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
1578 	if (ret < 0) {
1579 		wl1271_error("failed to initiate cmd add peer");
1580 		goto out_free;
1581 	}
1582 
1583 out_free:
1584 	kfree(cmd);
1585 
1586 out:
1587 	return ret;
1588 }
1589 
wl12xx_cmd_remove_peer(struct wl1271 * wl,u8 hlid)1590 int wl12xx_cmd_remove_peer(struct wl1271 *wl, u8 hlid)
1591 {
1592 	struct wl12xx_cmd_remove_peer *cmd;
1593 	int ret;
1594 
1595 	wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
1596 
1597 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1598 	if (!cmd) {
1599 		ret = -ENOMEM;
1600 		goto out;
1601 	}
1602 
1603 	cmd->hlid = hlid;
1604 	/* We never send a deauth, mac80211 is in charge of this */
1605 	cmd->reason_opcode = 0;
1606 	cmd->send_deauth_flag = 0;
1607 
1608 	ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
1609 	if (ret < 0) {
1610 		wl1271_error("failed to initiate cmd remove peer");
1611 		goto out_free;
1612 	}
1613 
1614 	/*
1615 	 * We are ok with a timeout here. The event is sometimes not sent
1616 	 * due to a firmware bug.
1617 	 */
1618 	wl1271_cmd_wait_for_event_or_timeout(wl,
1619 					     PEER_REMOVE_COMPLETE_EVENT_ID);
1620 
1621 out_free:
1622 	kfree(cmd);
1623 
1624 out:
1625 	return ret;
1626 }
1627 
wl12xx_cmd_config_fwlog(struct wl1271 * wl)1628 int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
1629 {
1630 	struct wl12xx_cmd_config_fwlog *cmd;
1631 	int ret = 0;
1632 
1633 	wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
1634 
1635 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1636 	if (!cmd) {
1637 		ret = -ENOMEM;
1638 		goto out;
1639 	}
1640 
1641 	cmd->logger_mode = wl->conf.fwlog.mode;
1642 	cmd->log_severity = wl->conf.fwlog.severity;
1643 	cmd->timestamp = wl->conf.fwlog.timestamp;
1644 	cmd->output = wl->conf.fwlog.output;
1645 	cmd->threshold = wl->conf.fwlog.threshold;
1646 
1647 	ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
1648 	if (ret < 0) {
1649 		wl1271_error("failed to send config firmware logger command");
1650 		goto out_free;
1651 	}
1652 
1653 out_free:
1654 	kfree(cmd);
1655 
1656 out:
1657 	return ret;
1658 }
1659 
wl12xx_cmd_start_fwlog(struct wl1271 * wl)1660 int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
1661 {
1662 	struct wl12xx_cmd_start_fwlog *cmd;
1663 	int ret = 0;
1664 
1665 	wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
1666 
1667 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1668 	if (!cmd) {
1669 		ret = -ENOMEM;
1670 		goto out;
1671 	}
1672 
1673 	ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
1674 	if (ret < 0) {
1675 		wl1271_error("failed to send start firmware logger command");
1676 		goto out_free;
1677 	}
1678 
1679 out_free:
1680 	kfree(cmd);
1681 
1682 out:
1683 	return ret;
1684 }
1685 
wl12xx_cmd_stop_fwlog(struct wl1271 * wl)1686 int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
1687 {
1688 	struct wl12xx_cmd_stop_fwlog *cmd;
1689 	int ret = 0;
1690 
1691 	wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
1692 
1693 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1694 	if (!cmd) {
1695 		ret = -ENOMEM;
1696 		goto out;
1697 	}
1698 
1699 	ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
1700 	if (ret < 0) {
1701 		wl1271_error("failed to send stop firmware logger command");
1702 		goto out_free;
1703 	}
1704 
1705 out_free:
1706 	kfree(cmd);
1707 
1708 out:
1709 	return ret;
1710 }
1711 
wl12xx_cmd_roc(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 role_id)1712 static int wl12xx_cmd_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1713 			  u8 role_id)
1714 {
1715 	struct wl12xx_cmd_roc *cmd;
1716 	int ret = 0;
1717 
1718 	wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", wlvif->channel, role_id);
1719 
1720 	if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
1721 		return -EINVAL;
1722 
1723 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1724 	if (!cmd) {
1725 		ret = -ENOMEM;
1726 		goto out;
1727 	}
1728 
1729 	cmd->role_id = role_id;
1730 	cmd->channel = wlvif->channel;
1731 	switch (wlvif->band) {
1732 	case IEEE80211_BAND_2GHZ:
1733 		cmd->band = RADIO_BAND_2_4GHZ;
1734 		break;
1735 	case IEEE80211_BAND_5GHZ:
1736 		cmd->band = RADIO_BAND_5GHZ;
1737 		break;
1738 	default:
1739 		wl1271_error("roc - unknown band: %d", (int)wlvif->band);
1740 		ret = -EINVAL;
1741 		goto out_free;
1742 	}
1743 
1744 
1745 	ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
1746 	if (ret < 0) {
1747 		wl1271_error("failed to send ROC command");
1748 		goto out_free;
1749 	}
1750 
1751 out_free:
1752 	kfree(cmd);
1753 
1754 out:
1755 	return ret;
1756 }
1757 
wl12xx_cmd_croc(struct wl1271 * wl,u8 role_id)1758 static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
1759 {
1760 	struct wl12xx_cmd_croc *cmd;
1761 	int ret = 0;
1762 
1763 	wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
1764 
1765 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1766 	if (!cmd) {
1767 		ret = -ENOMEM;
1768 		goto out;
1769 	}
1770 	cmd->role_id = role_id;
1771 
1772 	ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
1773 			      sizeof(*cmd), 0);
1774 	if (ret < 0) {
1775 		wl1271_error("failed to send ROC command");
1776 		goto out_free;
1777 	}
1778 
1779 out_free:
1780 	kfree(cmd);
1781 
1782 out:
1783 	return ret;
1784 }
1785 
wl12xx_roc(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 role_id)1786 int wl12xx_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 role_id)
1787 {
1788 	int ret = 0;
1789 
1790 	if (WARN_ON(test_bit(role_id, wl->roc_map)))
1791 		return 0;
1792 
1793 	ret = wl12xx_cmd_roc(wl, wlvif, role_id);
1794 	if (ret < 0)
1795 		goto out;
1796 
1797 	ret = wl1271_cmd_wait_for_event(wl,
1798 					REMAIN_ON_CHANNEL_COMPLETE_EVENT_ID);
1799 	if (ret < 0) {
1800 		wl1271_error("cmd roc event completion error");
1801 		goto out;
1802 	}
1803 
1804 	__set_bit(role_id, wl->roc_map);
1805 out:
1806 	return ret;
1807 }
1808 
wl12xx_croc(struct wl1271 * wl,u8 role_id)1809 int wl12xx_croc(struct wl1271 *wl, u8 role_id)
1810 {
1811 	int ret = 0;
1812 
1813 	if (WARN_ON(!test_bit(role_id, wl->roc_map)))
1814 		return 0;
1815 
1816 	ret = wl12xx_cmd_croc(wl, role_id);
1817 	if (ret < 0)
1818 		goto out;
1819 
1820 	__clear_bit(role_id, wl->roc_map);
1821 
1822 	/*
1823 	 * Rearm the tx watchdog when removing the last ROC. This prevents
1824 	 * recoveries due to just finished ROCs - when Tx hasn't yet had
1825 	 * a chance to get out.
1826 	 */
1827 	if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES)
1828 		wl12xx_rearm_tx_watchdog_locked(wl);
1829 out:
1830 	return ret;
1831 }
1832 
wl12xx_cmd_channel_switch(struct wl1271 * wl,struct wl12xx_vif * wlvif,struct ieee80211_channel_switch * ch_switch)1833 int wl12xx_cmd_channel_switch(struct wl1271 *wl,
1834 			      struct wl12xx_vif *wlvif,
1835 			      struct ieee80211_channel_switch *ch_switch)
1836 {
1837 	struct wl12xx_cmd_channel_switch *cmd;
1838 	int ret;
1839 
1840 	wl1271_debug(DEBUG_ACX, "cmd channel switch");
1841 
1842 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1843 	if (!cmd) {
1844 		ret = -ENOMEM;
1845 		goto out;
1846 	}
1847 
1848 	cmd->role_id = wlvif->role_id;
1849 	cmd->channel = ch_switch->channel->hw_value;
1850 	cmd->switch_time = ch_switch->count;
1851 	cmd->stop_tx = ch_switch->block_tx;
1852 
1853 	/* FIXME: control from mac80211 in the future */
1854 	cmd->post_switch_tx_disable = 0;  /* Enable TX on the target channel */
1855 
1856 	ret = wl1271_cmd_send(wl, CMD_CHANNEL_SWITCH, cmd, sizeof(*cmd), 0);
1857 	if (ret < 0) {
1858 		wl1271_error("failed to send channel switch command");
1859 		goto out_free;
1860 	}
1861 
1862 out_free:
1863 	kfree(cmd);
1864 
1865 out:
1866 	return ret;
1867 }
1868 
wl12xx_cmd_stop_channel_switch(struct wl1271 * wl)1869 int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl)
1870 {
1871 	struct wl12xx_cmd_stop_channel_switch *cmd;
1872 	int ret;
1873 
1874 	wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
1875 
1876 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1877 	if (!cmd) {
1878 		ret = -ENOMEM;
1879 		goto out;
1880 	}
1881 
1882 	ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
1883 	if (ret < 0) {
1884 		wl1271_error("failed to stop channel switch command");
1885 		goto out_free;
1886 	}
1887 
1888 out_free:
1889 	kfree(cmd);
1890 
1891 out:
1892 	return ret;
1893 }
1894 
1895 /* start dev role and roc on its channel */
wl12xx_start_dev(struct wl1271 * wl,struct wl12xx_vif * wlvif)1896 int wl12xx_start_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1897 {
1898 	int ret;
1899 
1900 	if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
1901 		      wlvif->bss_type == BSS_TYPE_IBSS)))
1902 		return -EINVAL;
1903 
1904 	ret = wl12xx_cmd_role_start_dev(wl, wlvif);
1905 	if (ret < 0)
1906 		goto out;
1907 
1908 	ret = wl12xx_roc(wl, wlvif, wlvif->dev_role_id);
1909 	if (ret < 0)
1910 		goto out_stop;
1911 
1912 	return 0;
1913 
1914 out_stop:
1915 	wl12xx_cmd_role_stop_dev(wl, wlvif);
1916 out:
1917 	return ret;
1918 }
1919 
1920 /* croc dev hlid, and stop the role */
wl12xx_stop_dev(struct wl1271 * wl,struct wl12xx_vif * wlvif)1921 int wl12xx_stop_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1922 {
1923 	int ret;
1924 
1925 	if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
1926 		      wlvif->bss_type == BSS_TYPE_IBSS)))
1927 		return -EINVAL;
1928 
1929 	/* flush all pending packets */
1930 	wl1271_tx_work_locked(wl);
1931 
1932 	if (test_bit(wlvif->dev_role_id, wl->roc_map)) {
1933 		ret = wl12xx_croc(wl, wlvif->dev_role_id);
1934 		if (ret < 0)
1935 			goto out;
1936 	}
1937 
1938 	ret = wl12xx_cmd_role_stop_dev(wl, wlvif);
1939 	if (ret < 0)
1940 		goto out;
1941 out:
1942 	return ret;
1943 }
1944