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