]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/wireless/wl12xx/wl1251_main.c
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[net-next-2.6.git] / drivers / net / wireless / wl12xx / wl1251_main.c
CommitLineData
2f01a1f5 1/*
80301cdc 2 * This file is part of wl1251
2f01a1f5
KV
3 *
4 * Copyright (C) 2008-2009 Nokia Corporation
5 *
2f01a1f5
KV
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18 * 02110-1301 USA
19 *
20 */
21
22#include <linux/module.h>
23#include <linux/interrupt.h>
24#include <linux/firmware.h>
25#include <linux/delay.h>
26#include <linux/irq.h>
2f01a1f5
KV
27#include <linux/crc32.h>
28#include <linux/etherdevice.h>
c74ddfd5 29#include <linux/vmalloc.h>
5a0e3ad6 30#include <linux/slab.h>
2f01a1f5 31
13674118 32#include "wl1251.h"
2f01a1f5 33#include "wl12xx_80211.h"
29d904c4 34#include "wl1251_reg.h"
0764de64 35#include "wl1251_io.h"
8e639c06 36#include "wl1251_cmd.h"
ef2f8d45 37#include "wl1251_event.h"
9f2ad4fb 38#include "wl1251_tx.h"
ef2f8d45
KV
39#include "wl1251_rx.h"
40#include "wl1251_ps.h"
41#include "wl1251_init.h"
42#include "wl1251_debugfs.h"
0e71bb08 43#include "wl1251_boot.h"
2f01a1f5 44
b5ed9c1b 45void wl1251_enable_interrupts(struct wl1251 *wl)
2f01a1f5 46{
b5ed9c1b
BC
47 wl->if_ops->enable_irq(wl);
48}
49
50void wl1251_disable_interrupts(struct wl1251 *wl)
51{
52 wl->if_ops->disable_irq(wl);
2f01a1f5
KV
53}
54
80301cdc 55static void wl1251_power_off(struct wl1251 *wl)
2f01a1f5
KV
56{
57 wl->set_power(false);
58}
59
80301cdc 60static void wl1251_power_on(struct wl1251 *wl)
2f01a1f5
KV
61{
62 wl->set_power(true);
63}
64
80301cdc 65static int wl1251_fetch_firmware(struct wl1251 *wl)
2f01a1f5
KV
66{
67 const struct firmware *fw;
6c766f41 68 struct device *dev = wiphy_dev(wl->hw->wiphy);
2f01a1f5
KV
69 int ret;
70
0e71bb08 71 ret = request_firmware(&fw, WL1251_FW_NAME, dev);
2f01a1f5
KV
72
73 if (ret < 0) {
80301cdc 74 wl1251_error("could not get firmware: %d", ret);
2f01a1f5
KV
75 return ret;
76 }
77
78 if (fw->size % 4) {
80301cdc 79 wl1251_error("firmware size is not multiple of 32 bits: %zu",
2f01a1f5
KV
80 fw->size);
81 ret = -EILSEQ;
82 goto out;
83 }
84
85 wl->fw_len = fw->size;
c74ddfd5 86 wl->fw = vmalloc(wl->fw_len);
2f01a1f5
KV
87
88 if (!wl->fw) {
80301cdc 89 wl1251_error("could not allocate memory for the firmware");
2f01a1f5
KV
90 ret = -ENOMEM;
91 goto out;
92 }
93
94 memcpy(wl->fw, fw->data, wl->fw_len);
95
96 ret = 0;
97
98out:
99 release_firmware(fw);
100
101 return ret;
102}
103
80301cdc 104static int wl1251_fetch_nvs(struct wl1251 *wl)
2f01a1f5
KV
105{
106 const struct firmware *fw;
6c766f41 107 struct device *dev = wiphy_dev(wl->hw->wiphy);
2f01a1f5
KV
108 int ret;
109
0e71bb08 110 ret = request_firmware(&fw, WL1251_NVS_NAME, dev);
2f01a1f5
KV
111
112 if (ret < 0) {
80301cdc 113 wl1251_error("could not get nvs file: %d", ret);
2f01a1f5
KV
114 return ret;
115 }
116
117 if (fw->size % 4) {
80301cdc 118 wl1251_error("nvs size is not multiple of 32 bits: %zu",
2f01a1f5
KV
119 fw->size);
120 ret = -EILSEQ;
121 goto out;
122 }
123
124 wl->nvs_len = fw->size;
80caf601 125 wl->nvs = kmemdup(fw->data, wl->nvs_len, GFP_KERNEL);
2f01a1f5
KV
126
127 if (!wl->nvs) {
80301cdc 128 wl1251_error("could not allocate memory for the nvs file");
2f01a1f5
KV
129 ret = -ENOMEM;
130 goto out;
131 }
132
2f01a1f5
KV
133 ret = 0;
134
135out:
136 release_firmware(fw);
137
138 return ret;
139}
140
80301cdc 141static void wl1251_fw_wakeup(struct wl1251 *wl)
2f01a1f5
KV
142{
143 u32 elp_reg;
144
145 elp_reg = ELPCTRL_WAKE_UP;
3f9e750d
GI
146 wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
147 elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
2f01a1f5 148
05fac682 149 if (!(elp_reg & ELPCTRL_WLAN_READY))
80301cdc 150 wl1251_warning("WLAN not ready");
2f01a1f5
KV
151}
152
80301cdc 153static int wl1251_chip_wakeup(struct wl1251 *wl)
2f01a1f5
KV
154{
155 int ret = 0;
156
80301cdc 157 wl1251_power_on(wl);
0e71bb08 158 msleep(WL1251_POWER_ON_SLEEP);
08d9f572 159 wl->if_ops->reset(wl);
2f01a1f5
KV
160
161 /* We don't need a real memory partition here, because we only want
162 * to use the registers at this point. */
80301cdc 163 wl1251_set_partition(wl,
2f01a1f5
KV
164 0x00000000,
165 0x00000000,
166 REGISTERS_BASE,
167 REGISTERS_DOWN_SIZE);
168
169 /* ELP module wake up */
80301cdc 170 wl1251_fw_wakeup(wl);
2f01a1f5
KV
171
172 /* whal_FwCtrl_BootSm() */
173
174 /* 0. read chip id from CHIP_ID */
0e71bb08 175 wl->chip_id = wl1251_reg_read32(wl, CHIP_ID_B);
2f01a1f5
KV
176
177 /* 1. check if chip id is valid */
178
0e71bb08 179 switch (wl->chip_id) {
2f01a1f5 180 case CHIP_ID_1251_PG12:
80301cdc 181 wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG12)",
0e71bb08 182 wl->chip_id);
2f01a1f5 183 break;
2f01a1f5 184 case CHIP_ID_1251_PG11:
2c759e03
DJW
185 wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG11)",
186 wl->chip_id);
187 break;
9a493e8f 188 case CHIP_ID_1251_PG10:
2f01a1f5 189 default:
0e71bb08 190 wl1251_error("unsupported chip id: 0x%x", wl->chip_id);
2f01a1f5
KV
191 ret = -ENODEV;
192 goto out;
193 }
194
195 if (wl->fw == NULL) {
80301cdc 196 ret = wl1251_fetch_firmware(wl);
2f01a1f5
KV
197 if (ret < 0)
198 goto out;
199 }
200
afa5ec27
GI
201 if (wl->nvs == NULL && !wl->use_eeprom) {
202 /* No NVS from netlink, try to get it from the filesystem */
80301cdc 203 ret = wl1251_fetch_nvs(wl);
2f01a1f5
KV
204 if (ret < 0)
205 goto out;
206 }
207
208out:
209 return ret;
210}
211
dcea4dbe 212#define WL1251_IRQ_LOOP_COUNT 10
0e71bb08
KV
213static void wl1251_irq_work(struct work_struct *work)
214{
dcea4dbe 215 u32 intr, ctr = WL1251_IRQ_LOOP_COUNT;
0e71bb08
KV
216 struct wl1251 *wl =
217 container_of(work, struct wl1251, irq_work);
218 int ret;
219
220 mutex_lock(&wl->mutex);
221
222 wl1251_debug(DEBUG_IRQ, "IRQ work");
223
224 if (wl->state == WL1251_STATE_OFF)
225 goto out;
226
227 ret = wl1251_ps_elp_wakeup(wl);
228 if (ret < 0)
229 goto out;
230
231 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, WL1251_ACX_INTR_ALL);
232
233 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
234 wl1251_debug(DEBUG_IRQ, "intr: 0x%x", intr);
235
dcea4dbe
JY
236 do {
237 if (wl->data_path) {
238 wl->rx_counter = wl1251_mem_read32(
239 wl, wl->data_path->rx_control_addr);
240
241 /* We handle a frmware bug here */
242 switch ((wl->rx_counter - wl->rx_handled) & 0xf) {
243 case 0:
244 wl1251_debug(DEBUG_IRQ,
245 "RX: FW and host in sync");
246 intr &= ~WL1251_ACX_INTR_RX0_DATA;
247 intr &= ~WL1251_ACX_INTR_RX1_DATA;
248 break;
249 case 1:
250 wl1251_debug(DEBUG_IRQ, "RX: FW +1");
251 intr |= WL1251_ACX_INTR_RX0_DATA;
252 intr &= ~WL1251_ACX_INTR_RX1_DATA;
253 break;
254 case 2:
255 wl1251_debug(DEBUG_IRQ, "RX: FW +2");
256 intr |= WL1251_ACX_INTR_RX0_DATA;
257 intr |= WL1251_ACX_INTR_RX1_DATA;
258 break;
259 default:
260 wl1251_warning(
261 "RX: FW and host out of sync: %d",
262 wl->rx_counter - wl->rx_handled);
263 break;
264 }
265
266 wl->rx_handled = wl->rx_counter;
267
268 wl1251_debug(DEBUG_IRQ, "RX counter: %d",
269 wl->rx_counter);
0e71bb08
KV
270 }
271
dcea4dbe 272 intr &= wl->intr_mask;
0e71bb08 273
dcea4dbe
JY
274 if (intr == 0) {
275 wl1251_debug(DEBUG_IRQ, "INTR is 0");
276 goto out_sleep;
277 }
0e71bb08 278
dcea4dbe
JY
279 if (intr & WL1251_ACX_INTR_RX0_DATA) {
280 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX0_DATA");
281 wl1251_rx(wl);
282 }
0e71bb08 283
dcea4dbe
JY
284 if (intr & WL1251_ACX_INTR_RX1_DATA) {
285 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX1_DATA");
286 wl1251_rx(wl);
287 }
0e71bb08 288
dcea4dbe
JY
289 if (intr & WL1251_ACX_INTR_TX_RESULT) {
290 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_TX_RESULT");
291 wl1251_tx_complete(wl);
292 }
0e71bb08 293
d41776fa
GI
294 if (intr & WL1251_ACX_INTR_EVENT_A) {
295 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT_A");
296 wl1251_event_handle(wl, 0);
297 }
298
299 if (intr & WL1251_ACX_INTR_EVENT_B) {
300 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT_B");
301 wl1251_event_handle(wl, 1);
dcea4dbe 302 }
0e71bb08 303
dcea4dbe
JY
304 if (intr & WL1251_ACX_INTR_INIT_COMPLETE)
305 wl1251_debug(DEBUG_IRQ,
306 "WL1251_ACX_INTR_INIT_COMPLETE");
0e71bb08 307
79553f82
JO
308 if (--ctr == 0)
309 break;
0e71bb08 310
79553f82
JO
311 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
312 } while (intr);
0e71bb08
KV
313
314out_sleep:
dcea4dbe 315 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, ~(wl->intr_mask));
0e71bb08
KV
316 wl1251_ps_elp_sleep(wl);
317
318out:
319 mutex_unlock(&wl->mutex);
320}
321
ae46ae17
KV
322static int wl1251_join(struct wl1251 *wl, u8 bss_type, u8 channel,
323 u16 beacon_interval, u8 dtim_period)
324{
325 int ret;
326
327 ret = wl1251_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
328 DEFAULT_HW_GEN_MODULATION_TYPE,
329 wl->tx_mgmt_frm_rate,
330 wl->tx_mgmt_frm_mod);
331 if (ret < 0)
332 goto out;
333
334
335 ret = wl1251_cmd_join(wl, bss_type, channel, beacon_interval,
336 dtim_period);
337 if (ret < 0)
338 goto out;
339
7273b970
GI
340 ret = wl1251_event_wait(wl, JOIN_EVENT_COMPLETE_ID, 100);
341 if (ret < 0)
342 wl1251_warning("join timeout");
ae46ae17
KV
343
344out:
345 return ret;
346}
347
80301cdc 348static void wl1251_filter_work(struct work_struct *work)
2f01a1f5 349{
80301cdc
KV
350 struct wl1251 *wl =
351 container_of(work, struct wl1251, filter_work);
2f01a1f5
KV
352 int ret;
353
354 mutex_lock(&wl->mutex);
355
80301cdc 356 if (wl->state == WL1251_STATE_OFF)
2f01a1f5
KV
357 goto out;
358
80301cdc 359 ret = wl1251_ps_elp_wakeup(wl);
c5483b71
KV
360 if (ret < 0)
361 goto out;
01d9cfbd 362
ae46ae17
KV
363 ret = wl1251_join(wl, wl->bss_type, wl->channel, wl->beacon_int,
364 wl->dtim_period);
2f01a1f5 365 if (ret < 0)
c5483b71 366 goto out_sleep;
2f01a1f5 367
c5483b71 368out_sleep:
80301cdc 369 wl1251_ps_elp_sleep(wl);
c5483b71
KV
370
371out:
2f01a1f5
KV
372 mutex_unlock(&wl->mutex);
373}
374
80301cdc 375static int wl1251_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2f01a1f5 376{
80301cdc 377 struct wl1251 *wl = hw->priv;
9df86e2e 378 unsigned long flags;
2f01a1f5
KV
379
380 skb_queue_tail(&wl->tx_queue, skb);
381
9f2ad4fb
JO
382 /*
383 * The chip specific setup must run before the first TX packet -
384 * before that, the tx_work will not be initialized!
385 */
386
16e711f9 387 ieee80211_queue_work(wl->hw, &wl->tx_work);
2f01a1f5
KV
388
389 /*
390 * The workqueue is slow to process the tx_queue and we need stop
391 * the queue here, otherwise the queue will get too long.
392 */
9df86e2e 393 if (skb_queue_len(&wl->tx_queue) >= WL1251_TX_QUEUE_HIGH_WATERMARK) {
d67e2618 394 wl1251_debug(DEBUG_TX, "op_tx: tx_queue full, stop queues");
2f01a1f5 395
9df86e2e
DGC
396 spin_lock_irqsave(&wl->wl_lock, flags);
397 ieee80211_stop_queues(wl->hw);
2f01a1f5 398 wl->tx_queue_stopped = true;
9df86e2e 399 spin_unlock_irqrestore(&wl->wl_lock, flags);
2f01a1f5
KV
400 }
401
402 return NETDEV_TX_OK;
403}
404
80301cdc 405static int wl1251_op_start(struct ieee80211_hw *hw)
2f01a1f5 406{
80301cdc 407 struct wl1251 *wl = hw->priv;
8b28e822 408 struct wiphy *wiphy = hw->wiphy;
2f01a1f5
KV
409 int ret = 0;
410
80301cdc 411 wl1251_debug(DEBUG_MAC80211, "mac80211 start");
2f01a1f5
KV
412
413 mutex_lock(&wl->mutex);
414
80301cdc
KV
415 if (wl->state != WL1251_STATE_OFF) {
416 wl1251_error("cannot start because not in off state: %d",
2f01a1f5
KV
417 wl->state);
418 ret = -EBUSY;
419 goto out;
420 }
421
80301cdc 422 ret = wl1251_chip_wakeup(wl);
2f01a1f5 423 if (ret < 0)
fe643414 424 goto out;
2f01a1f5 425
0e71bb08 426 ret = wl1251_boot(wl);
2f01a1f5
KV
427 if (ret < 0)
428 goto out;
429
0e71bb08 430 ret = wl1251_hw_init(wl);
2f01a1f5
KV
431 if (ret < 0)
432 goto out;
433
80301cdc 434 ret = wl1251_acx_station_id(wl);
2f01a1f5
KV
435 if (ret < 0)
436 goto out;
437
80301cdc 438 wl->state = WL1251_STATE_ON;
2f01a1f5 439
0e71bb08 440 wl1251_info("firmware booted (%s)", wl->fw_ver);
2f01a1f5 441
8b28e822
JL
442 /* update hw/fw version info in wiphy struct */
443 wiphy->hw_version = wl->chip_id;
444 strncpy(wiphy->fw_version, wl->fw_ver, sizeof(wiphy->fw_version));
445
2f01a1f5
KV
446out:
447 if (ret < 0)
80301cdc 448 wl1251_power_off(wl);
2f01a1f5
KV
449
450 mutex_unlock(&wl->mutex);
451
452 return ret;
453}
454
80301cdc 455static void wl1251_op_stop(struct ieee80211_hw *hw)
2f01a1f5 456{
80301cdc 457 struct wl1251 *wl = hw->priv;
2f01a1f5 458
80301cdc 459 wl1251_info("down");
2f01a1f5 460
80301cdc 461 wl1251_debug(DEBUG_MAC80211, "mac80211 stop");
2f01a1f5
KV
462
463 mutex_lock(&wl->mutex);
464
80301cdc 465 WARN_ON(wl->state != WL1251_STATE_ON);
2f01a1f5
KV
466
467 if (wl->scanning) {
2f01a1f5 468 ieee80211_scan_completed(wl->hw, true);
2f01a1f5
KV
469 wl->scanning = false;
470 }
471
80301cdc 472 wl->state = WL1251_STATE_OFF;
2f01a1f5 473
80301cdc 474 wl1251_disable_interrupts(wl);
2f01a1f5
KV
475
476 mutex_unlock(&wl->mutex);
477
478 cancel_work_sync(&wl->irq_work);
479 cancel_work_sync(&wl->tx_work);
480 cancel_work_sync(&wl->filter_work);
481
482 mutex_lock(&wl->mutex);
483
484 /* let's notify MAC80211 about the remaining pending TX frames */
0e71bb08 485 wl1251_tx_flush(wl);
80301cdc 486 wl1251_power_off(wl);
2f01a1f5
KV
487
488 memset(wl->bssid, 0, ETH_ALEN);
489 wl->listen_int = 1;
490 wl->bss_type = MAX_BSS_TYPE;
491
492 wl->data_in_count = 0;
493 wl->rx_counter = 0;
494 wl->rx_handled = 0;
495 wl->rx_current_buffer = 0;
496 wl->rx_last_id = 0;
497 wl->next_tx_complete = 0;
498 wl->elp = false;
499 wl->psm = 0;
500 wl->tx_queue_stopped = false;
80301cdc 501 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
9780279c 502 wl->channel = WL1251_DEFAULT_CHANNEL;
2f01a1f5 503
80301cdc 504 wl1251_debugfs_reset(wl);
2f01a1f5
KV
505
506 mutex_unlock(&wl->mutex);
507}
508
80301cdc 509static int wl1251_op_add_interface(struct ieee80211_hw *hw,
1ed32e4f 510 struct ieee80211_vif *vif)
2f01a1f5 511{
80301cdc 512 struct wl1251 *wl = hw->priv;
2f01a1f5
KV
513 int ret = 0;
514
e91d8334 515 wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
1ed32e4f 516 vif->type, vif->addr);
2f01a1f5
KV
517
518 mutex_lock(&wl->mutex);
287f6f96
JO
519 if (wl->vif) {
520 ret = -EBUSY;
521 goto out;
522 }
523
1ed32e4f 524 wl->vif = vif;
2f01a1f5 525
1ed32e4f 526 switch (vif->type) {
2f01a1f5
KV
527 case NL80211_IFTYPE_STATION:
528 wl->bss_type = BSS_TYPE_STA_BSS;
529 break;
530 case NL80211_IFTYPE_ADHOC:
531 wl->bss_type = BSS_TYPE_IBSS;
532 break;
533 default:
534 ret = -EOPNOTSUPP;
535 goto out;
536 }
537
1ed32e4f
JB
538 if (memcmp(wl->mac_addr, vif->addr, ETH_ALEN)) {
539 memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
2f01a1f5 540 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
80301cdc 541 ret = wl1251_acx_station_id(wl);
2f01a1f5
KV
542 if (ret < 0)
543 goto out;
544 }
545
546out:
547 mutex_unlock(&wl->mutex);
548 return ret;
549}
550
80301cdc 551static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
1ed32e4f 552 struct ieee80211_vif *vif)
2f01a1f5 553{
287f6f96
JO
554 struct wl1251 *wl = hw->priv;
555
556 mutex_lock(&wl->mutex);
80301cdc 557 wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
287f6f96
JO
558 wl->vif = NULL;
559 mutex_unlock(&wl->mutex);
2f01a1f5
KV
560}
561
8f8ff916
KV
562static int wl1251_build_qos_null_data(struct wl1251 *wl)
563{
564 struct ieee80211_qos_hdr template;
565
566 memset(&template, 0, sizeof(template));
567
568 memcpy(template.addr1, wl->bssid, ETH_ALEN);
569 memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
570 memcpy(template.addr3, wl->bssid, ETH_ALEN);
571
572 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
573 IEEE80211_STYPE_QOS_NULLFUNC |
574 IEEE80211_FCTL_TODS);
575
576 /* FIXME: not sure what priority to use here */
577 template.qos_ctrl = cpu_to_le16(0);
578
579 return wl1251_cmd_template_set(wl, CMD_QOS_NULL_DATA, &template,
580 sizeof(template));
581}
582
80301cdc 583static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
2f01a1f5 584{
80301cdc 585 struct wl1251 *wl = hw->priv;
2f01a1f5
KV
586 struct ieee80211_conf *conf = &hw->conf;
587 int channel, ret = 0;
588
589 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
590
80301cdc 591 wl1251_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
2f01a1f5
KV
592 channel,
593 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
594 conf->power_level);
595
596 mutex_lock(&wl->mutex);
597
80301cdc 598 ret = wl1251_ps_elp_wakeup(wl);
c5483b71
KV
599 if (ret < 0)
600 goto out;
01d9cfbd 601
2f01a1f5 602 if (channel != wl->channel) {
fe9a9846
KV
603 wl->channel = channel;
604
ae46ae17
KV
605 ret = wl1251_join(wl, wl->bss_type, wl->channel,
606 wl->beacon_int, wl->dtim_period);
2f01a1f5 607 if (ret < 0)
c5483b71 608 goto out_sleep;
2f01a1f5
KV
609 }
610
2f01a1f5 611 if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
47af3fe3 612 wl1251_debug(DEBUG_PSM, "psm enabled");
2f01a1f5
KV
613
614 wl->psm_requested = true;
615
56007a02
JB
616 wl->dtim_period = conf->ps_dtim_period;
617
618 ret = wl1251_acx_wr_tbtt_and_dtim(wl, wl->beacon_int,
619 wl->dtim_period);
620
2f01a1f5 621 /*
56007a02 622 * mac80211 enables PSM only if we're already associated.
2f01a1f5 623 */
80301cdc 624 ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
478fdf2b
KV
625 if (ret < 0)
626 goto out_sleep;
2f01a1f5
KV
627 } else if (!(conf->flags & IEEE80211_CONF_PS) &&
628 wl->psm_requested) {
47af3fe3 629 wl1251_debug(DEBUG_PSM, "psm disabled");
2f01a1f5
KV
630
631 wl->psm_requested = false;
632
478fdf2b 633 if (wl->psm) {
80301cdc 634 ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
478fdf2b
KV
635 if (ret < 0)
636 goto out_sleep;
637 }
2f01a1f5
KV
638 }
639
640 if (conf->power_level != wl->power_level) {
80301cdc 641 ret = wl1251_acx_tx_power(wl, conf->power_level);
2f01a1f5 642 if (ret < 0)
478fdf2b 643 goto out_sleep;
2f01a1f5
KV
644
645 wl->power_level = conf->power_level;
646 }
647
c5483b71 648out_sleep:
80301cdc 649 wl1251_ps_elp_sleep(wl);
c5483b71
KV
650
651out:
2f01a1f5 652 mutex_unlock(&wl->mutex);
c5483b71 653
2f01a1f5
KV
654 return ret;
655}
656
80301cdc 657#define WL1251_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
2f01a1f5
KV
658 FIF_ALLMULTI | \
659 FIF_FCSFAIL | \
660 FIF_BCN_PRBRESP_PROMISC | \
661 FIF_CONTROL | \
662 FIF_OTHER_BSS)
663
80301cdc 664static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
2f01a1f5 665 unsigned int changed,
3ac64bee 666 unsigned int *total,u64 multicast)
2f01a1f5 667{
80301cdc 668 struct wl1251 *wl = hw->priv;
2f01a1f5 669
80301cdc 670 wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
2f01a1f5 671
80301cdc
KV
672 *total &= WL1251_SUPPORTED_FILTERS;
673 changed &= WL1251_SUPPORTED_FILTERS;
2f01a1f5
KV
674
675 if (changed == 0)
676 /* no filters which we support changed */
677 return;
678
679 /* FIXME: wl->rx_config and wl->rx_filter are not protected */
680
80301cdc
KV
681 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
682 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
2f01a1f5
KV
683
684 if (*total & FIF_PROMISC_IN_BSS) {
685 wl->rx_config |= CFG_BSSID_FILTER_EN;
686 wl->rx_config |= CFG_RX_ALL_GOOD;
687 }
688 if (*total & FIF_ALLMULTI)
689 /*
690 * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
691 * all multicast frames
692 */
693 wl->rx_config &= ~CFG_MC_FILTER_EN;
694 if (*total & FIF_FCSFAIL)
695 wl->rx_filter |= CFG_RX_FCS_ERROR;
696 if (*total & FIF_BCN_PRBRESP_PROMISC) {
697 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
698 wl->rx_config &= ~CFG_SSID_FILTER_EN;
699 }
700 if (*total & FIF_CONTROL)
701 wl->rx_filter |= CFG_RX_CTL_EN;
702 if (*total & FIF_OTHER_BSS)
703 wl->rx_filter &= ~CFG_BSSID_FILTER_EN;
704
705 /*
706 * FIXME: workqueues need to be properly cancelled on stop(), for
707 * now let's just disable changing the filter settings. They will
708 * be updated any on config().
709 */
710 /* schedule_work(&wl->filter_work); */
711}
712
713/* HW encryption */
80301cdc
KV
714static int wl1251_set_key_type(struct wl1251 *wl,
715 struct wl1251_cmd_set_keys *key,
2f01a1f5
KV
716 enum set_key_cmd cmd,
717 struct ieee80211_key_conf *mac80211_key,
718 const u8 *addr)
719{
97359d12
JB
720 switch (mac80211_key->cipher) {
721 case WLAN_CIPHER_SUITE_WEP40:
722 case WLAN_CIPHER_SUITE_WEP104:
2f01a1f5
KV
723 if (is_broadcast_ether_addr(addr))
724 key->key_type = KEY_WEP_DEFAULT;
725 else
726 key->key_type = KEY_WEP_ADDR;
727
728 mac80211_key->hw_key_idx = mac80211_key->keyidx;
729 break;
97359d12 730 case WLAN_CIPHER_SUITE_TKIP:
2f01a1f5
KV
731 if (is_broadcast_ether_addr(addr))
732 key->key_type = KEY_TKIP_MIC_GROUP;
733 else
734 key->key_type = KEY_TKIP_MIC_PAIRWISE;
735
736 mac80211_key->hw_key_idx = mac80211_key->keyidx;
737 break;
97359d12 738 case WLAN_CIPHER_SUITE_CCMP:
2f01a1f5
KV
739 if (is_broadcast_ether_addr(addr))
740 key->key_type = KEY_AES_GROUP;
741 else
742 key->key_type = KEY_AES_PAIRWISE;
743 mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
744 break;
745 default:
97359d12 746 wl1251_error("Unknown key cipher 0x%x", mac80211_key->cipher);
2f01a1f5
KV
747 return -EOPNOTSUPP;
748 }
749
750 return 0;
751}
752
80301cdc 753static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2f01a1f5
KV
754 struct ieee80211_vif *vif,
755 struct ieee80211_sta *sta,
756 struct ieee80211_key_conf *key)
757{
80301cdc
KV
758 struct wl1251 *wl = hw->priv;
759 struct wl1251_cmd_set_keys *wl_cmd;
2f01a1f5
KV
760 const u8 *addr;
761 int ret;
762
763 static const u8 bcast_addr[ETH_ALEN] =
764 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
765
80301cdc 766 wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
2f01a1f5 767
ff25839b
KV
768 wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
769 if (!wl_cmd) {
770 ret = -ENOMEM;
771 goto out;
772 }
2f01a1f5
KV
773
774 addr = sta ? sta->addr : bcast_addr;
775
80301cdc
KV
776 wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
777 wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
778 wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
97359d12 779 key->cipher, key->keyidx, key->keylen, key->flags);
80301cdc 780 wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
2f01a1f5 781
ff25839b
KV
782 if (is_zero_ether_addr(addr)) {
783 /* We dont support TX only encryption */
784 ret = -EOPNOTSUPP;
785 goto out;
786 }
787
2f01a1f5
KV
788 mutex_lock(&wl->mutex);
789
80301cdc 790 ret = wl1251_ps_elp_wakeup(wl);
c5483b71
KV
791 if (ret < 0)
792 goto out_unlock;
01d9cfbd 793
2f01a1f5
KV
794 switch (cmd) {
795 case SET_KEY:
ff25839b 796 wl_cmd->key_action = KEY_ADD_OR_REPLACE;
2f01a1f5
KV
797 break;
798 case DISABLE_KEY:
ff25839b 799 wl_cmd->key_action = KEY_REMOVE;
2f01a1f5
KV
800 break;
801 default:
80301cdc 802 wl1251_error("Unsupported key cmd 0x%x", cmd);
2f01a1f5
KV
803 break;
804 }
805
80301cdc 806 ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
2f01a1f5 807 if (ret < 0) {
80301cdc 808 wl1251_error("Set KEY type failed");
c5483b71 809 goto out_sleep;
2f01a1f5
KV
810 }
811
ff25839b
KV
812 if (wl_cmd->key_type != KEY_WEP_DEFAULT)
813 memcpy(wl_cmd->addr, addr, ETH_ALEN);
2f01a1f5 814
ff25839b
KV
815 if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
816 (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
2f01a1f5
KV
817 /*
818 * We get the key in the following form:
819 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
820 * but the target is expecting:
821 * TKIP - RX MIC - TX MIC
822 */
ff25839b
KV
823 memcpy(wl_cmd->key, key->key, 16);
824 memcpy(wl_cmd->key + 16, key->key + 24, 8);
825 memcpy(wl_cmd->key + 24, key->key + 16, 8);
2f01a1f5
KV
826
827 } else {
ff25839b 828 memcpy(wl_cmd->key, key->key, key->keylen);
2f01a1f5 829 }
ff25839b 830 wl_cmd->key_size = key->keylen;
2f01a1f5 831
ff25839b
KV
832 wl_cmd->id = key->keyidx;
833 wl_cmd->ssid_profile = 0;
2f01a1f5 834
80301cdc 835 wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
2f01a1f5 836
80301cdc 837 ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
ff25839b 838 if (ret < 0) {
80301cdc 839 wl1251_warning("could not set keys");
c5483b71 840 goto out_sleep;
2f01a1f5
KV
841 }
842
c5483b71 843out_sleep:
80301cdc 844 wl1251_ps_elp_sleep(wl);
c5483b71
KV
845
846out_unlock:
2f01a1f5 847 mutex_unlock(&wl->mutex);
ff25839b
KV
848
849out:
850 kfree(wl_cmd);
851
2f01a1f5
KV
852 return ret;
853}
854
80301cdc 855static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
a060bbfe 856 struct ieee80211_vif *vif,
2f01a1f5
KV
857 struct cfg80211_scan_request *req)
858{
80301cdc 859 struct wl1251 *wl = hw->priv;
e477c56e 860 struct sk_buff *skb;
2f01a1f5 861 size_t ssid_len = 0;
3a98c30f
KV
862 u8 *ssid = NULL;
863 int ret;
2f01a1f5 864
80301cdc 865 wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
2f01a1f5
KV
866
867 if (req->n_ssids) {
868 ssid = req->ssids[0].ssid;
869 ssid_len = req->ssids[0].ssid_len;
870 }
871
872 mutex_lock(&wl->mutex);
c5483b71 873
3a98c30f
KV
874 if (wl->scanning) {
875 wl1251_debug(DEBUG_SCAN, "scan already in progress");
876 ret = -EINVAL;
877 goto out;
878 }
879
80301cdc 880 ret = wl1251_ps_elp_wakeup(wl);
c5483b71
KV
881 if (ret < 0)
882 goto out;
01d9cfbd 883
e477c56e
KV
884 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
885 req->ie, req->ie_len);
886 if (!skb) {
887 ret = -ENOMEM;
888 goto out;
889 }
890
891 ret = wl1251_cmd_template_set(wl, CMD_PROBE_REQ, skb->data,
892 skb->len);
893 dev_kfree_skb(skb);
3a98c30f 894 if (ret < 0)
e477c56e 895 goto out_sleep;
3a98c30f
KV
896
897 ret = wl1251_cmd_trigger_scan_to(wl, 0);
898 if (ret < 0)
899 goto out_sleep;
900
901 wl->scanning = true;
01d9cfbd 902
dc52f0a8
KV
903 ret = wl1251_cmd_scan(wl, ssid, ssid_len, req->channels,
904 req->n_channels, WL1251_SCAN_NUM_PROBES);
3a98c30f
KV
905 if (ret < 0) {
906 wl->scanning = false;
907 goto out_sleep;
908 }
909
910out_sleep:
80301cdc 911 wl1251_ps_elp_sleep(wl);
c5483b71
KV
912
913out:
2f01a1f5
KV
914 mutex_unlock(&wl->mutex);
915
916 return ret;
917}
918
80301cdc 919static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2f01a1f5 920{
80301cdc 921 struct wl1251 *wl = hw->priv;
2f01a1f5
KV
922 int ret;
923
cee4fd27
KV
924 mutex_lock(&wl->mutex);
925
80301cdc 926 ret = wl1251_ps_elp_wakeup(wl);
c5483b71
KV
927 if (ret < 0)
928 goto out;
01d9cfbd 929
80301cdc 930 ret = wl1251_acx_rts_threshold(wl, (u16) value);
2f01a1f5 931 if (ret < 0)
80301cdc 932 wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
2f01a1f5 933
80301cdc 934 wl1251_ps_elp_sleep(wl);
01d9cfbd 935
c5483b71 936out:
cee4fd27
KV
937 mutex_unlock(&wl->mutex);
938
2f01a1f5
KV
939 return ret;
940}
941
80301cdc 942static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
2f01a1f5
KV
943 struct ieee80211_vif *vif,
944 struct ieee80211_bss_conf *bss_conf,
945 u32 changed)
946{
80301cdc 947 struct wl1251 *wl = hw->priv;
f7f70579 948 struct sk_buff *beacon, *skb;
2f01a1f5
KV
949 int ret;
950
80301cdc 951 wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
2f01a1f5
KV
952
953 mutex_lock(&wl->mutex);
954
80301cdc 955 ret = wl1251_ps_elp_wakeup(wl);
c5483b71
KV
956 if (ret < 0)
957 goto out;
01d9cfbd 958
de8df1ea
KV
959 if (changed & BSS_CHANGED_BSSID) {
960 memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
961
f7f70579
KV
962 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
963 if (!skb)
964 goto out_sleep;
965
966 ret = wl1251_cmd_template_set(wl, CMD_NULL_DATA,
967 skb->data, skb->len);
968 dev_kfree_skb(skb);
de8df1ea 969 if (ret < 0)
80a112ff 970 goto out_sleep;
de8df1ea 971
8f8ff916
KV
972 ret = wl1251_build_qos_null_data(wl);
973 if (ret < 0)
974 goto out;
975
de8df1ea
KV
976 if (wl->bss_type != BSS_TYPE_IBSS) {
977 ret = wl1251_join(wl, wl->bss_type, wl->channel,
978 wl->beacon_int, wl->dtim_period);
979 if (ret < 0)
980 goto out_sleep;
981 }
982 }
983
2f01a1f5
KV
984 if (changed & BSS_CHANGED_ASSOC) {
985 if (bss_conf->assoc) {
e2fd4611 986 wl->beacon_int = bss_conf->beacon_int;
2f01a1f5 987
f7f70579
KV
988 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
989 if (!skb)
990 goto out_sleep;
991
992 ret = wl1251_cmd_template_set(wl, CMD_PS_POLL,
993 skb->data,
994 skb->len);
995 dev_kfree_skb(skb);
2f01a1f5 996 if (ret < 0)
c5483b71 997 goto out_sleep;
2f01a1f5 998
56007a02 999 ret = wl1251_acx_aid(wl, bss_conf->aid);
2f01a1f5 1000 if (ret < 0)
c5483b71 1001 goto out_sleep;
e2fd4611
KV
1002 } else {
1003 /* use defaults when not associated */
1004 wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
1005 wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
2f01a1f5
KV
1006 }
1007 }
1008 if (changed & BSS_CHANGED_ERP_SLOT) {
1009 if (bss_conf->use_short_slot)
80301cdc 1010 ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
2f01a1f5 1011 else
80301cdc 1012 ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
2f01a1f5 1013 if (ret < 0) {
80301cdc 1014 wl1251_warning("Set slot time failed %d", ret);
c5483b71 1015 goto out_sleep;
2f01a1f5
KV
1016 }
1017 }
1018
1019 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1020 if (bss_conf->use_short_preamble)
80301cdc 1021 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
2f01a1f5 1022 else
80301cdc 1023 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
2f01a1f5
KV
1024 }
1025
1026 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1027 if (bss_conf->use_cts_prot)
80301cdc 1028 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
2f01a1f5 1029 else
80301cdc 1030 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
2f01a1f5 1031 if (ret < 0) {
80301cdc 1032 wl1251_warning("Set ctsprotect failed %d", ret);
80a112ff 1033 goto out_sleep;
2f01a1f5
KV
1034 }
1035 }
1036
2f01a1f5
KV
1037 if (changed & BSS_CHANGED_BEACON) {
1038 beacon = ieee80211_beacon_get(hw, vif);
80301cdc 1039 ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
2f01a1f5
KV
1040 beacon->len);
1041
1042 if (ret < 0) {
1043 dev_kfree_skb(beacon);
80a112ff 1044 goto out_sleep;
2f01a1f5
KV
1045 }
1046
80301cdc 1047 ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
2f01a1f5
KV
1048 beacon->len);
1049
1050 dev_kfree_skb(beacon);
1051
1052 if (ret < 0)
80a112ff 1053 goto out_sleep;
2f01a1f5 1054
ae46ae17
KV
1055 ret = wl1251_join(wl, wl->bss_type, wl->beacon_int,
1056 wl->channel, wl->dtim_period);
2f01a1f5
KV
1057
1058 if (ret < 0)
80a112ff 1059 goto out_sleep;
2f01a1f5
KV
1060 }
1061
c5483b71 1062out_sleep:
80301cdc 1063 wl1251_ps_elp_sleep(wl);
c5483b71
KV
1064
1065out:
2f01a1f5
KV
1066 mutex_unlock(&wl->mutex);
1067}
1068
1069
1070/* can't be const, mac80211 writes to this */
80301cdc 1071static struct ieee80211_rate wl1251_rates[] = {
2f01a1f5
KV
1072 { .bitrate = 10,
1073 .hw_value = 0x1,
1074 .hw_value_short = 0x1, },
1075 { .bitrate = 20,
1076 .hw_value = 0x2,
1077 .hw_value_short = 0x2,
1078 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1079 { .bitrate = 55,
1080 .hw_value = 0x4,
1081 .hw_value_short = 0x4,
1082 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1083 { .bitrate = 110,
1084 .hw_value = 0x20,
1085 .hw_value_short = 0x20,
1086 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1087 { .bitrate = 60,
1088 .hw_value = 0x8,
1089 .hw_value_short = 0x8, },
1090 { .bitrate = 90,
1091 .hw_value = 0x10,
1092 .hw_value_short = 0x10, },
1093 { .bitrate = 120,
1094 .hw_value = 0x40,
1095 .hw_value_short = 0x40, },
1096 { .bitrate = 180,
1097 .hw_value = 0x80,
1098 .hw_value_short = 0x80, },
1099 { .bitrate = 240,
1100 .hw_value = 0x200,
1101 .hw_value_short = 0x200, },
1102 { .bitrate = 360,
1103 .hw_value = 0x400,
1104 .hw_value_short = 0x400, },
1105 { .bitrate = 480,
1106 .hw_value = 0x800,
1107 .hw_value_short = 0x800, },
1108 { .bitrate = 540,
1109 .hw_value = 0x1000,
1110 .hw_value_short = 0x1000, },
1111};
1112
1113/* can't be const, mac80211 writes to this */
80301cdc 1114static struct ieee80211_channel wl1251_channels[] = {
2f01a1f5
KV
1115 { .hw_value = 1, .center_freq = 2412},
1116 { .hw_value = 2, .center_freq = 2417},
1117 { .hw_value = 3, .center_freq = 2422},
1118 { .hw_value = 4, .center_freq = 2427},
1119 { .hw_value = 5, .center_freq = 2432},
1120 { .hw_value = 6, .center_freq = 2437},
1121 { .hw_value = 7, .center_freq = 2442},
1122 { .hw_value = 8, .center_freq = 2447},
1123 { .hw_value = 9, .center_freq = 2452},
1124 { .hw_value = 10, .center_freq = 2457},
1125 { .hw_value = 11, .center_freq = 2462},
1126 { .hw_value = 12, .center_freq = 2467},
1127 { .hw_value = 13, .center_freq = 2472},
1128};
1129
86dff7a7
KV
1130static int wl1251_op_conf_tx(struct ieee80211_hw *hw, u16 queue,
1131 const struct ieee80211_tx_queue_params *params)
1132{
4ff6ffa1 1133 enum wl1251_acx_ps_scheme ps_scheme;
86dff7a7
KV
1134 struct wl1251 *wl = hw->priv;
1135 int ret;
1136
1137 mutex_lock(&wl->mutex);
1138
1139 wl1251_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
1140
1141 ret = wl1251_ps_elp_wakeup(wl);
1142 if (ret < 0)
1143 goto out;
1144
85359499 1145 /* mac80211 uses units of 32 usec */
86dff7a7
KV
1146 ret = wl1251_acx_ac_cfg(wl, wl1251_tx_get_queue(queue),
1147 params->cw_min, params->cw_max,
85359499 1148 params->aifs, params->txop * 32);
27336f1c
KV
1149 if (ret < 0)
1150 goto out_sleep;
1151
4ff6ffa1
KV
1152 if (params->uapsd)
1153 ps_scheme = WL1251_ACX_PS_SCHEME_UPSD_TRIGGER;
1154 else
1155 ps_scheme = WL1251_ACX_PS_SCHEME_LEGACY;
1156
27336f1c 1157 ret = wl1251_acx_tid_cfg(wl, wl1251_tx_get_queue(queue),
49e1b9fa 1158 CHANNEL_TYPE_EDCF,
4ff6ffa1 1159 wl1251_tx_get_queue(queue), ps_scheme,
27336f1c
KV
1160 WL1251_ACX_ACK_POLICY_LEGACY);
1161 if (ret < 0)
1162 goto out_sleep;
86dff7a7 1163
27336f1c 1164out_sleep:
86dff7a7
KV
1165 wl1251_ps_elp_sleep(wl);
1166
1167out:
1168 mutex_unlock(&wl->mutex);
1169
1170 return ret;
1171}
1172
19434148
JL
1173static int wl1251_op_get_survey(struct ieee80211_hw *hw, int idx,
1174 struct survey_info *survey)
1175{
1176 struct wl1251 *wl = hw->priv;
1177 struct ieee80211_conf *conf = &hw->conf;
1178
1179 if (idx != 0)
1180 return -ENOENT;
1181
1182 survey->channel = conf->channel;
1183 survey->filled = SURVEY_INFO_NOISE_DBM;
1184 survey->noise = wl->noise;
1185
1186 return 0;
1187}
1188
2f01a1f5 1189/* can't be const, mac80211 writes to this */
80301cdc
KV
1190static struct ieee80211_supported_band wl1251_band_2ghz = {
1191 .channels = wl1251_channels,
1192 .n_channels = ARRAY_SIZE(wl1251_channels),
1193 .bitrates = wl1251_rates,
1194 .n_bitrates = ARRAY_SIZE(wl1251_rates),
2f01a1f5
KV
1195};
1196
80301cdc
KV
1197static const struct ieee80211_ops wl1251_ops = {
1198 .start = wl1251_op_start,
1199 .stop = wl1251_op_stop,
1200 .add_interface = wl1251_op_add_interface,
1201 .remove_interface = wl1251_op_remove_interface,
1202 .config = wl1251_op_config,
1203 .configure_filter = wl1251_op_configure_filter,
1204 .tx = wl1251_op_tx,
1205 .set_key = wl1251_op_set_key,
1206 .hw_scan = wl1251_op_hw_scan,
1207 .bss_info_changed = wl1251_op_bss_info_changed,
1208 .set_rts_threshold = wl1251_op_set_rts_threshold,
86dff7a7 1209 .conf_tx = wl1251_op_conf_tx,
19434148 1210 .get_survey = wl1251_op_get_survey,
2f01a1f5
KV
1211};
1212
61c2a80b
GI
1213static int wl1251_read_eeprom_byte(struct wl1251 *wl, off_t offset, u8 *data)
1214{
1215 unsigned long timeout;
1216
1217 wl1251_reg_write32(wl, EE_ADDR, offset);
1218 wl1251_reg_write32(wl, EE_CTL, EE_CTL_READ);
1219
1220 /* EE_CTL_READ clears when data is ready */
1221 timeout = jiffies + msecs_to_jiffies(100);
1222 while (1) {
1223 if (!(wl1251_reg_read32(wl, EE_CTL) & EE_CTL_READ))
1224 break;
1225
1226 if (time_after(jiffies, timeout))
1227 return -ETIMEDOUT;
1228
1229 msleep(1);
1230 }
1231
1232 *data = wl1251_reg_read32(wl, EE_DATA);
1233 return 0;
1234}
1235
1236static int wl1251_read_eeprom(struct wl1251 *wl, off_t offset,
1237 u8 *data, size_t len)
1238{
1239 size_t i;
1240 int ret;
1241
1242 wl1251_reg_write32(wl, EE_START, 0);
1243
1244 for (i = 0; i < len; i++) {
1245 ret = wl1251_read_eeprom_byte(wl, offset + i, &data[i]);
1246 if (ret < 0)
1247 return ret;
1248 }
1249
1250 return 0;
1251}
1252
1253static int wl1251_read_eeprom_mac(struct wl1251 *wl)
1254{
1255 u8 mac[ETH_ALEN];
1256 int i, ret;
1257
1258 wl1251_set_partition(wl, 0, 0, REGISTERS_BASE, REGISTERS_DOWN_SIZE);
1259
1260 ret = wl1251_read_eeprom(wl, 0x1c, mac, sizeof(mac));
1261 if (ret < 0) {
1262 wl1251_warning("failed to read MAC address from EEPROM");
1263 return ret;
1264 }
1265
1266 /* MAC is stored in reverse order */
1267 for (i = 0; i < ETH_ALEN; i++)
1268 wl->mac_addr[i] = mac[ETH_ALEN - i - 1];
1269
1270 return 0;
1271}
1272
80301cdc 1273static int wl1251_register_hw(struct wl1251 *wl)
2f01a1f5
KV
1274{
1275 int ret;
1276
1277 if (wl->mac80211_registered)
1278 return 0;
1279
1280 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1281
1282 ret = ieee80211_register_hw(wl->hw);
1283 if (ret < 0) {
80301cdc 1284 wl1251_error("unable to register mac80211 hw: %d", ret);
2f01a1f5
KV
1285 return ret;
1286 }
1287
1288 wl->mac80211_registered = true;
1289
80301cdc 1290 wl1251_notice("loaded");
2f01a1f5
KV
1291
1292 return 0;
1293}
1294
8e639c06 1295int wl1251_init_ieee80211(struct wl1251 *wl)
2f01a1f5 1296{
8e639c06
BC
1297 int ret;
1298
2f01a1f5
KV
1299 /* The tx descriptor buffer and the TKIP space */
1300 wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
9f2ad4fb 1301 + WL1251_TKIP_IV_SPACE;
2f01a1f5
KV
1302
1303 /* unit us */
1304 /* FIXME: find a proper value */
1305 wl->hw->channel_change_time = 10000;
1306
1307 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
6b21a2cd 1308 IEEE80211_HW_SUPPORTS_PS |
4ff6ffa1
KV
1309 IEEE80211_HW_BEACON_FILTER |
1310 IEEE80211_HW_SUPPORTS_UAPSD;
2f01a1f5
KV
1311
1312 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1313 wl->hw->wiphy->max_scan_ssids = 1;
80301cdc 1314 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1251_band_2ghz;
2f01a1f5 1315
da2fb4e9
KV
1316 wl->hw->queues = 4;
1317
61c2a80b
GI
1318 if (wl->use_eeprom)
1319 wl1251_read_eeprom_mac(wl);
1320
8e639c06
BC
1321 ret = wl1251_register_hw(wl);
1322 if (ret)
1323 goto out;
2f01a1f5 1324
8e639c06
BC
1325 wl1251_debugfs_init(wl);
1326 wl1251_notice("initialized");
2f01a1f5 1327
8e639c06
BC
1328 ret = 0;
1329
1330out:
1331 return ret;
1332}
af8c78eb 1333EXPORT_SYMBOL_GPL(wl1251_init_ieee80211);
08d9f572 1334
8e639c06 1335struct ieee80211_hw *wl1251_alloc_hw(void)
2f01a1f5 1336{
2f01a1f5 1337 struct ieee80211_hw *hw;
80301cdc 1338 struct wl1251 *wl;
8e639c06 1339 int i;
2f01a1f5
KV
1340 static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1341
80301cdc 1342 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
2f01a1f5 1343 if (!hw) {
80301cdc 1344 wl1251_error("could not alloc ieee80211_hw");
8e639c06 1345 return ERR_PTR(-ENOMEM);
2f01a1f5
KV
1346 }
1347
1348 wl = hw->priv;
1349 memset(wl, 0, sizeof(*wl));
1350
1351 wl->hw = hw;
2f01a1f5
KV
1352
1353 wl->data_in_count = 0;
1354
1355 skb_queue_head_init(&wl->tx_queue);
1356
80301cdc 1357 INIT_WORK(&wl->filter_work, wl1251_filter_work);
d5da79ac 1358 INIT_DELAYED_WORK(&wl->elp_work, wl1251_elp_work);
80301cdc 1359 wl->channel = WL1251_DEFAULT_CHANNEL;
2f01a1f5
KV
1360 wl->scanning = false;
1361 wl->default_key = 0;
1362 wl->listen_int = 1;
1363 wl->rx_counter = 0;
1364 wl->rx_handled = 0;
1365 wl->rx_current_buffer = 0;
1366 wl->rx_last_id = 0;
80301cdc
KV
1367 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
1368 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
2f01a1f5
KV
1369 wl->elp = false;
1370 wl->psm = 0;
1371 wl->psm_requested = false;
1372 wl->tx_queue_stopped = false;
80301cdc 1373 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
e2fd4611
KV
1374 wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
1375 wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
287f6f96 1376 wl->vif = NULL;
2f01a1f5 1377
2f01a1f5
KV
1378 for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
1379 wl->tx_frames[i] = NULL;
1380
1381 wl->next_tx_complete = 0;
1382
0e71bb08
KV
1383 INIT_WORK(&wl->irq_work, wl1251_irq_work);
1384 INIT_WORK(&wl->tx_work, wl1251_tx_work);
1385
2f01a1f5
KV
1386 /*
1387 * In case our MAC address is not correctly set,
1388 * we use a random but Nokia MAC.
1389 */
1390 memcpy(wl->mac_addr, nokia_oui, 3);
1391 get_random_bytes(wl->mac_addr + 3, 3);
1392
80301cdc 1393 wl->state = WL1251_STATE_OFF;
2f01a1f5
KV
1394 mutex_init(&wl->mutex);
1395
1396 wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
1397 wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
1398
4721213f
KV
1399 wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1400 if (!wl->rx_descriptor) {
80301cdc 1401 wl1251_error("could not allocate memory for rx descriptor");
8e639c06
BC
1402 ieee80211_free_hw(hw);
1403 return ERR_PTR(-ENOMEM);
4721213f
KV
1404 }
1405
8e639c06 1406 return hw;
2f01a1f5 1407}
af8c78eb 1408EXPORT_SYMBOL_GPL(wl1251_alloc_hw);
2f01a1f5 1409
8e639c06 1410int wl1251_free_hw(struct wl1251 *wl)
2f01a1f5 1411{
2f01a1f5
KV
1412 ieee80211_unregister_hw(wl->hw);
1413
80301cdc 1414 wl1251_debugfs_exit(wl);
2f01a1f5 1415
2f01a1f5
KV
1416 kfree(wl->target_mem_map);
1417 kfree(wl->data_path);
c74ddfd5 1418 vfree(wl->fw);
2f01a1f5
KV
1419 wl->fw = NULL;
1420 kfree(wl->nvs);
1421 wl->nvs = NULL;
4721213f
KV
1422
1423 kfree(wl->rx_descriptor);
1424 wl->rx_descriptor = NULL;
1425
2f01a1f5
KV
1426 ieee80211_free_hw(wl->hw);
1427
1428 return 0;
1429}
af8c78eb
BC
1430EXPORT_SYMBOL_GPL(wl1251_free_hw);
1431
1432MODULE_DESCRIPTION("TI wl1251 Wireles LAN Driver Core");
1433MODULE_LICENSE("GPL");
31c726f0 1434MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
49f146de 1435MODULE_FIRMWARE(WL1251_FW_NAME);