blob: b5e3bdb084480f1a880396f3b9e26182866ecf40 [file] [log] [blame]
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001/*
Kalle Valo80301cd2009-06-12 14:17:39 +03002 * This file is part of wl1251
Kalle Valo2f01a1f2009-04-29 23:33:31 +03003 *
4 * Copyright (C) 2008-2009 Nokia Corporation
5 *
6 * Contact: Kalle Valo <kalle.valo@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/interrupt.h>
26#include <linux/firmware.h>
27#include <linux/delay.h>
28#include <linux/irq.h>
Kalle Valo2f01a1f2009-04-29 23:33:31 +030029#include <linux/crc32.h>
30#include <linux/etherdevice.h>
Kalle Valo2f01a1f2009-04-29 23:33:31 +030031
Kalle Valo13674112009-06-12 14:17:25 +030032#include "wl1251.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030033#include "wl12xx_80211.h"
Kalle Valo29d904c2009-08-07 13:35:11 +030034#include "wl1251_reg.h"
Bob Copeland0764de62009-08-07 13:32:56 +030035#include "wl1251_io.h"
Bob Copeland8e639c02009-08-07 13:33:26 +030036#include "wl1251_cmd.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030037#include "wl1251_event.h"
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +030038#include "wl1251_tx.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030039#include "wl1251_rx.h"
40#include "wl1251_ps.h"
41#include "wl1251_init.h"
42#include "wl1251_debugfs.h"
Kalle Valo0e71bb02009-08-07 13:33:57 +030043#include "wl1251_boot.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030044
Bob Copelandb5ed9c12009-08-07 13:33:49 +030045void wl1251_enable_interrupts(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030046{
Bob Copelandb5ed9c12009-08-07 13:33:49 +030047 wl->if_ops->enable_irq(wl);
48}
49
50void wl1251_disable_interrupts(struct wl1251 *wl)
51{
52 wl->if_ops->disable_irq(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030053}
54
Kalle Valo80301cd2009-06-12 14:17:39 +030055static void wl1251_power_off(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030056{
57 wl->set_power(false);
58}
59
Kalle Valo80301cd2009-06-12 14:17:39 +030060static void wl1251_power_on(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030061{
62 wl->set_power(true);
63}
64
Kalle Valo80301cd2009-06-12 14:17:39 +030065static int wl1251_fetch_firmware(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030066{
67 const struct firmware *fw;
Bob Copeland6c766f42009-08-07 13:33:04 +030068 struct device *dev = wiphy_dev(wl->hw->wiphy);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030069 int ret;
70
Kalle Valo0e71bb02009-08-07 13:33:57 +030071 ret = request_firmware(&fw, WL1251_FW_NAME, dev);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030072
73 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +030074 wl1251_error("could not get firmware: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030075 return ret;
76 }
77
78 if (fw->size % 4) {
Kalle Valo80301cd2009-06-12 14:17:39 +030079 wl1251_error("firmware size is not multiple of 32 bits: %zu",
Kalle Valo2f01a1f2009-04-29 23:33:31 +030080 fw->size);
81 ret = -EILSEQ;
82 goto out;
83 }
84
85 wl->fw_len = fw->size;
86 wl->fw = kmalloc(wl->fw_len, GFP_KERNEL);
87
88 if (!wl->fw) {
Kalle Valo80301cd2009-06-12 14:17:39 +030089 wl1251_error("could not allocate memory for the firmware");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030090 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
Kalle Valo80301cd2009-06-12 14:17:39 +0300104static int wl1251_fetch_nvs(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300105{
106 const struct firmware *fw;
Bob Copeland6c766f42009-08-07 13:33:04 +0300107 struct device *dev = wiphy_dev(wl->hw->wiphy);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300108 int ret;
109
Kalle Valo0e71bb02009-08-07 13:33:57 +0300110 ret = request_firmware(&fw, WL1251_NVS_NAME, dev);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300111
112 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300113 wl1251_error("could not get nvs file: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300114 return ret;
115 }
116
117 if (fw->size % 4) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300118 wl1251_error("nvs size is not multiple of 32 bits: %zu",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300119 fw->size);
120 ret = -EILSEQ;
121 goto out;
122 }
123
124 wl->nvs_len = fw->size;
125 wl->nvs = kmalloc(wl->nvs_len, GFP_KERNEL);
126
127 if (!wl->nvs) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300128 wl1251_error("could not allocate memory for the nvs file");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300129 ret = -ENOMEM;
130 goto out;
131 }
132
133 memcpy(wl->nvs, fw->data, wl->nvs_len);
134
135 ret = 0;
136
137out:
138 release_firmware(fw);
139
140 return ret;
141}
142
Kalle Valo80301cd2009-06-12 14:17:39 +0300143static void wl1251_fw_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300144{
145 u32 elp_reg;
146
147 elp_reg = ELPCTRL_WAKE_UP;
Kalle Valo80301cd2009-06-12 14:17:39 +0300148 wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
149 elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300150
Kalle Valo05fac682009-06-12 14:17:47 +0300151 if (!(elp_reg & ELPCTRL_WLAN_READY))
Kalle Valo80301cd2009-06-12 14:17:39 +0300152 wl1251_warning("WLAN not ready");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300153}
154
Kalle Valo80301cd2009-06-12 14:17:39 +0300155static int wl1251_chip_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300156{
157 int ret = 0;
158
Kalle Valo80301cd2009-06-12 14:17:39 +0300159 wl1251_power_on(wl);
Kalle Valo0e71bb02009-08-07 13:33:57 +0300160 msleep(WL1251_POWER_ON_SLEEP);
Bob Copeland08d9f5722009-08-07 13:33:11 +0300161 wl->if_ops->reset(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300162
163 /* We don't need a real memory partition here, because we only want
164 * to use the registers at this point. */
Kalle Valo80301cd2009-06-12 14:17:39 +0300165 wl1251_set_partition(wl,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300166 0x00000000,
167 0x00000000,
168 REGISTERS_BASE,
169 REGISTERS_DOWN_SIZE);
170
171 /* ELP module wake up */
Kalle Valo80301cd2009-06-12 14:17:39 +0300172 wl1251_fw_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300173
174 /* whal_FwCtrl_BootSm() */
175
176 /* 0. read chip id from CHIP_ID */
Kalle Valo0e71bb02009-08-07 13:33:57 +0300177 wl->chip_id = wl1251_reg_read32(wl, CHIP_ID_B);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300178
179 /* 1. check if chip id is valid */
180
Kalle Valo0e71bb02009-08-07 13:33:57 +0300181 switch (wl->chip_id) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300182 case CHIP_ID_1251_PG12:
Kalle Valo80301cd2009-06-12 14:17:39 +0300183 wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG12)",
Kalle Valo0e71bb02009-08-07 13:33:57 +0300184 wl->chip_id);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300185 break;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300186 case CHIP_ID_1251_PG11:
David-John Willis2c759e02009-10-15 14:38:16 +0100187 wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG11)",
188 wl->chip_id);
189 break;
John W. Linville9a493e82009-10-27 15:15:05 -0400190 case CHIP_ID_1251_PG10:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300191 default:
Kalle Valo0e71bb02009-08-07 13:33:57 +0300192 wl1251_error("unsupported chip id: 0x%x", wl->chip_id);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300193 ret = -ENODEV;
194 goto out;
195 }
196
197 if (wl->fw == NULL) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300198 ret = wl1251_fetch_firmware(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300199 if (ret < 0)
200 goto out;
201 }
202
203 /* No NVS from netlink, try to get it from the filesystem */
204 if (wl->nvs == NULL) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300205 ret = wl1251_fetch_nvs(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300206 if (ret < 0)
207 goto out;
208 }
209
210out:
211 return ret;
212}
213
Kalle Valo0e71bb02009-08-07 13:33:57 +0300214static void wl1251_irq_work(struct work_struct *work)
215{
216 u32 intr;
217 struct wl1251 *wl =
218 container_of(work, struct wl1251, irq_work);
219 int ret;
220
221 mutex_lock(&wl->mutex);
222
223 wl1251_debug(DEBUG_IRQ, "IRQ work");
224
225 if (wl->state == WL1251_STATE_OFF)
226 goto out;
227
228 ret = wl1251_ps_elp_wakeup(wl);
229 if (ret < 0)
230 goto out;
231
232 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, WL1251_ACX_INTR_ALL);
233
234 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
235 wl1251_debug(DEBUG_IRQ, "intr: 0x%x", intr);
236
237 if (wl->data_path) {
238 wl->rx_counter =
239 wl1251_mem_read32(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, "RX: FW and host in sync");
245 intr &= ~WL1251_ACX_INTR_RX0_DATA;
246 intr &= ~WL1251_ACX_INTR_RX1_DATA;
247 break;
248 case 1:
249 wl1251_debug(DEBUG_IRQ, "RX: FW +1");
250 intr |= WL1251_ACX_INTR_RX0_DATA;
251 intr &= ~WL1251_ACX_INTR_RX1_DATA;
252 break;
253 case 2:
254 wl1251_debug(DEBUG_IRQ, "RX: FW +2");
255 intr |= WL1251_ACX_INTR_RX0_DATA;
256 intr |= WL1251_ACX_INTR_RX1_DATA;
257 break;
258 default:
259 wl1251_warning("RX: FW and host out of sync: %d",
260 wl->rx_counter - wl->rx_handled);
261 break;
262 }
263
264 wl->rx_handled = wl->rx_counter;
265
266
267 wl1251_debug(DEBUG_IRQ, "RX counter: %d", wl->rx_counter);
268 }
269
270 intr &= wl->intr_mask;
271
272 if (intr == 0) {
273 wl1251_debug(DEBUG_IRQ, "INTR is 0");
274 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK,
275 ~(wl->intr_mask));
276
277 goto out_sleep;
278 }
279
280 if (intr & WL1251_ACX_INTR_RX0_DATA) {
281 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX0_DATA");
282 wl1251_rx(wl);
283 }
284
285 if (intr & WL1251_ACX_INTR_RX1_DATA) {
286 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX1_DATA");
287 wl1251_rx(wl);
288 }
289
290 if (intr & WL1251_ACX_INTR_TX_RESULT) {
291 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_TX_RESULT");
292 wl1251_tx_complete(wl);
293 }
294
295 if (intr & (WL1251_ACX_INTR_EVENT_A | WL1251_ACX_INTR_EVENT_B)) {
296 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT (0x%x)", intr);
297 if (intr & WL1251_ACX_INTR_EVENT_A)
298 wl1251_event_handle(wl, 0);
299 else
300 wl1251_event_handle(wl, 1);
301 }
302
303 if (intr & WL1251_ACX_INTR_INIT_COMPLETE)
304 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_INIT_COMPLETE");
305
306 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, ~(wl->intr_mask));
307
308out_sleep:
309 wl1251_ps_elp_sleep(wl);
310
311out:
312 mutex_unlock(&wl->mutex);
313}
314
Kalle Valoae46ae12009-08-07 13:34:42 +0300315static int wl1251_join(struct wl1251 *wl, u8 bss_type, u8 channel,
316 u16 beacon_interval, u8 dtim_period)
317{
318 int ret;
319
320 ret = wl1251_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
321 DEFAULT_HW_GEN_MODULATION_TYPE,
322 wl->tx_mgmt_frm_rate,
323 wl->tx_mgmt_frm_mod);
324 if (ret < 0)
325 goto out;
326
327
328 ret = wl1251_cmd_join(wl, bss_type, channel, beacon_interval,
329 dtim_period);
330 if (ret < 0)
331 goto out;
332
333 /*
334 * FIXME: we should wait for JOIN_EVENT_COMPLETE_ID but to simplify
335 * locking we just sleep instead, for now
336 */
337 msleep(10);
338
339out:
340 return ret;
341}
342
Kalle Valo80301cd2009-06-12 14:17:39 +0300343static void wl1251_filter_work(struct work_struct *work)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300344{
Kalle Valo80301cd2009-06-12 14:17:39 +0300345 struct wl1251 *wl =
346 container_of(work, struct wl1251, filter_work);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300347 int ret;
348
349 mutex_lock(&wl->mutex);
350
Kalle Valo80301cd2009-06-12 14:17:39 +0300351 if (wl->state == WL1251_STATE_OFF)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300352 goto out;
353
Kalle Valo80301cd2009-06-12 14:17:39 +0300354 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300355 if (ret < 0)
356 goto out;
357
Kalle Valoae46ae12009-08-07 13:34:42 +0300358 ret = wl1251_join(wl, wl->bss_type, wl->channel, wl->beacon_int,
359 wl->dtim_period);
Kalle Valoc5483b72009-06-12 14:16:32 +0300360 if (ret < 0)
361 goto out_sleep;
362
363out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300364 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300365
366out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300367 mutex_unlock(&wl->mutex);
368}
369
Kalle Valo80301cd2009-06-12 14:17:39 +0300370static int wl1251_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300371{
Kalle Valo80301cd2009-06-12 14:17:39 +0300372 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300373
374 skb_queue_tail(&wl->tx_queue, skb);
375
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +0300376 /*
377 * The chip specific setup must run before the first TX packet -
378 * before that, the tx_work will not be initialized!
379 */
380
Kalle Valo16e711f2009-08-07 13:35:04 +0300381 ieee80211_queue_work(wl->hw, &wl->tx_work);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300382
383 /*
384 * The workqueue is slow to process the tx_queue and we need stop
385 * the queue here, otherwise the queue will get too long.
386 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300387 if (skb_queue_len(&wl->tx_queue) >= WL1251_TX_QUEUE_MAX_LENGTH) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300388 ieee80211_stop_queues(wl->hw);
389
390 /*
391 * FIXME: this is racy, the variable is not properly
392 * protected. Maybe fix this by removing the stupid
393 * variable altogether and checking the real queue state?
394 */
395 wl->tx_queue_stopped = true;
396 }
397
398 return NETDEV_TX_OK;
399}
400
Kalle Valo80301cd2009-06-12 14:17:39 +0300401static int wl1251_op_start(struct ieee80211_hw *hw)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300402{
Kalle Valo80301cd2009-06-12 14:17:39 +0300403 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300404 int ret = 0;
405
Kalle Valo80301cd2009-06-12 14:17:39 +0300406 wl1251_debug(DEBUG_MAC80211, "mac80211 start");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300407
408 mutex_lock(&wl->mutex);
409
Kalle Valo80301cd2009-06-12 14:17:39 +0300410 if (wl->state != WL1251_STATE_OFF) {
411 wl1251_error("cannot start because not in off state: %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300412 wl->state);
413 ret = -EBUSY;
414 goto out;
415 }
416
Kalle Valo80301cd2009-06-12 14:17:39 +0300417 ret = wl1251_chip_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300418 if (ret < 0)
Jiri Slabyfe643412009-07-14 22:37:13 +0200419 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300420
Kalle Valo0e71bb02009-08-07 13:33:57 +0300421 ret = wl1251_boot(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300422 if (ret < 0)
423 goto out;
424
Kalle Valo0e71bb02009-08-07 13:33:57 +0300425 ret = wl1251_hw_init(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300426 if (ret < 0)
427 goto out;
428
Kalle Valo80301cd2009-06-12 14:17:39 +0300429 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300430 if (ret < 0)
431 goto out;
432
Kalle Valo80301cd2009-06-12 14:17:39 +0300433 wl->state = WL1251_STATE_ON;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300434
Kalle Valo0e71bb02009-08-07 13:33:57 +0300435 wl1251_info("firmware booted (%s)", wl->fw_ver);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300436
437out:
438 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300439 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300440
441 mutex_unlock(&wl->mutex);
442
443 return ret;
444}
445
Kalle Valo80301cd2009-06-12 14:17:39 +0300446static void wl1251_op_stop(struct ieee80211_hw *hw)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300447{
Kalle Valo80301cd2009-06-12 14:17:39 +0300448 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300449
Kalle Valo80301cd2009-06-12 14:17:39 +0300450 wl1251_info("down");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300451
Kalle Valo80301cd2009-06-12 14:17:39 +0300452 wl1251_debug(DEBUG_MAC80211, "mac80211 stop");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300453
454 mutex_lock(&wl->mutex);
455
Kalle Valo80301cd2009-06-12 14:17:39 +0300456 WARN_ON(wl->state != WL1251_STATE_ON);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300457
458 if (wl->scanning) {
459 mutex_unlock(&wl->mutex);
460 ieee80211_scan_completed(wl->hw, true);
461 mutex_lock(&wl->mutex);
462 wl->scanning = false;
463 }
464
Kalle Valo80301cd2009-06-12 14:17:39 +0300465 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300466
Kalle Valo80301cd2009-06-12 14:17:39 +0300467 wl1251_disable_interrupts(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300468
469 mutex_unlock(&wl->mutex);
470
471 cancel_work_sync(&wl->irq_work);
472 cancel_work_sync(&wl->tx_work);
473 cancel_work_sync(&wl->filter_work);
474
475 mutex_lock(&wl->mutex);
476
477 /* let's notify MAC80211 about the remaining pending TX frames */
Kalle Valo0e71bb02009-08-07 13:33:57 +0300478 wl1251_tx_flush(wl);
Kalle Valo80301cd2009-06-12 14:17:39 +0300479 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300480
481 memset(wl->bssid, 0, ETH_ALEN);
482 wl->listen_int = 1;
483 wl->bss_type = MAX_BSS_TYPE;
484
485 wl->data_in_count = 0;
486 wl->rx_counter = 0;
487 wl->rx_handled = 0;
488 wl->rx_current_buffer = 0;
489 wl->rx_last_id = 0;
490 wl->next_tx_complete = 0;
491 wl->elp = false;
492 wl->psm = 0;
493 wl->tx_queue_stopped = false;
Kalle Valo80301cd2009-06-12 14:17:39 +0300494 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
Kalle Valo97802792009-08-07 13:34:27 +0300495 wl->channel = WL1251_DEFAULT_CHANNEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300496
Kalle Valo80301cd2009-06-12 14:17:39 +0300497 wl1251_debugfs_reset(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300498
499 mutex_unlock(&wl->mutex);
500}
501
Kalle Valo80301cd2009-06-12 14:17:39 +0300502static int wl1251_op_add_interface(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300503 struct ieee80211_if_init_conf *conf)
504{
Kalle Valo80301cd2009-06-12 14:17:39 +0300505 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300506 int ret = 0;
507
Johannes Berge91d8332009-07-15 17:21:41 +0200508 wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
509 conf->type, conf->mac_addr);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300510
511 mutex_lock(&wl->mutex);
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200512 if (wl->vif) {
513 ret = -EBUSY;
514 goto out;
515 }
516
517 wl->vif = conf->vif;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300518
519 switch (conf->type) {
520 case NL80211_IFTYPE_STATION:
521 wl->bss_type = BSS_TYPE_STA_BSS;
522 break;
523 case NL80211_IFTYPE_ADHOC:
524 wl->bss_type = BSS_TYPE_IBSS;
525 break;
526 default:
527 ret = -EOPNOTSUPP;
528 goto out;
529 }
530
531 if (memcmp(wl->mac_addr, conf->mac_addr, ETH_ALEN)) {
532 memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
533 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
Kalle Valo80301cd2009-06-12 14:17:39 +0300534 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300535 if (ret < 0)
536 goto out;
537 }
538
539out:
540 mutex_unlock(&wl->mutex);
541 return ret;
542}
543
Kalle Valo80301cd2009-06-12 14:17:39 +0300544static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300545 struct ieee80211_if_init_conf *conf)
546{
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200547 struct wl1251 *wl = hw->priv;
548
549 mutex_lock(&wl->mutex);
Kalle Valo80301cd2009-06-12 14:17:39 +0300550 wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200551 wl->vif = NULL;
552 mutex_unlock(&wl->mutex);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300553}
554
Kalle Valo80301cd2009-06-12 14:17:39 +0300555static int wl1251_build_null_data(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300556{
557 struct wl12xx_null_data_template template;
558
559 if (!is_zero_ether_addr(wl->bssid)) {
560 memcpy(template.header.da, wl->bssid, ETH_ALEN);
561 memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
562 } else {
563 memset(template.header.da, 0xff, ETH_ALEN);
564 memset(template.header.bssid, 0xff, ETH_ALEN);
565 }
566
567 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
568 template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
569 IEEE80211_STYPE_NULLFUNC);
570
Kalle Valo80301cd2009-06-12 14:17:39 +0300571 return wl1251_cmd_template_set(wl, CMD_NULL_DATA, &template,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300572 sizeof(template));
573
574}
575
Kalle Valo80301cd2009-06-12 14:17:39 +0300576static int wl1251_build_ps_poll(struct wl1251 *wl, u16 aid)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300577{
578 struct wl12xx_ps_poll_template template;
579
580 memcpy(template.bssid, wl->bssid, ETH_ALEN);
581 memcpy(template.ta, wl->mac_addr, ETH_ALEN);
582 template.aid = aid;
583 template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
584
Kalle Valo80301cd2009-06-12 14:17:39 +0300585 return wl1251_cmd_template_set(wl, CMD_PS_POLL, &template,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300586 sizeof(template));
587
588}
589
Kalle Valo80301cd2009-06-12 14:17:39 +0300590static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300591{
Kalle Valo80301cd2009-06-12 14:17:39 +0300592 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300593 struct ieee80211_conf *conf = &hw->conf;
594 int channel, ret = 0;
595
596 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
597
Kalle Valo80301cd2009-06-12 14:17:39 +0300598 wl1251_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300599 channel,
600 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
601 conf->power_level);
602
603 mutex_lock(&wl->mutex);
604
Kalle Valo80301cd2009-06-12 14:17:39 +0300605 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300606 if (ret < 0)
607 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300608
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300609 if (channel != wl->channel) {
Kalle Valofe9a9842009-08-07 13:34:49 +0300610 wl->channel = channel;
611
Kalle Valoae46ae12009-08-07 13:34:42 +0300612 ret = wl1251_join(wl, wl->bss_type, wl->channel,
613 wl->beacon_int, wl->dtim_period);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300614 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300615 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300616 }
617
Kalle Valo80301cd2009-06-12 14:17:39 +0300618 ret = wl1251_build_null_data(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300619 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300620 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300621
622 if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
Luciano Coelho47af3fe2009-06-12 14:17:53 +0300623 wl1251_debug(DEBUG_PSM, "psm enabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300624
625 wl->psm_requested = true;
626
627 /*
628 * We enter PSM only if we're already associated.
629 * If we're not, we'll enter it when joining an SSID,
630 * through the bss_info_changed() hook.
631 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300632 ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300633 } else if (!(conf->flags & IEEE80211_CONF_PS) &&
634 wl->psm_requested) {
Luciano Coelho47af3fe2009-06-12 14:17:53 +0300635 wl1251_debug(DEBUG_PSM, "psm disabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300636
637 wl->psm_requested = false;
638
639 if (wl->psm)
Kalle Valo80301cd2009-06-12 14:17:39 +0300640 ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300641 }
642
643 if (conf->power_level != wl->power_level) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300644 ret = wl1251_acx_tx_power(wl, conf->power_level);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300645 if (ret < 0)
646 goto out;
647
648 wl->power_level = conf->power_level;
649 }
650
Kalle Valoc5483b72009-06-12 14:16:32 +0300651out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300652 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300653
654out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300655 mutex_unlock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +0300656
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300657 return ret;
658}
659
Kalle Valo80301cd2009-06-12 14:17:39 +0300660#define WL1251_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300661 FIF_ALLMULTI | \
662 FIF_FCSFAIL | \
663 FIF_BCN_PRBRESP_PROMISC | \
664 FIF_CONTROL | \
665 FIF_OTHER_BSS)
666
Kalle Valo80301cd2009-06-12 14:17:39 +0300667static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300668 unsigned int changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +0200669 unsigned int *total,u64 multicast)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300670{
Kalle Valo80301cd2009-06-12 14:17:39 +0300671 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300672
Kalle Valo80301cd2009-06-12 14:17:39 +0300673 wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300674
Kalle Valo80301cd2009-06-12 14:17:39 +0300675 *total &= WL1251_SUPPORTED_FILTERS;
676 changed &= WL1251_SUPPORTED_FILTERS;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300677
678 if (changed == 0)
679 /* no filters which we support changed */
680 return;
681
682 /* FIXME: wl->rx_config and wl->rx_filter are not protected */
683
Kalle Valo80301cd2009-06-12 14:17:39 +0300684 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
685 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300686
687 if (*total & FIF_PROMISC_IN_BSS) {
688 wl->rx_config |= CFG_BSSID_FILTER_EN;
689 wl->rx_config |= CFG_RX_ALL_GOOD;
690 }
691 if (*total & FIF_ALLMULTI)
692 /*
693 * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
694 * all multicast frames
695 */
696 wl->rx_config &= ~CFG_MC_FILTER_EN;
697 if (*total & FIF_FCSFAIL)
698 wl->rx_filter |= CFG_RX_FCS_ERROR;
699 if (*total & FIF_BCN_PRBRESP_PROMISC) {
700 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
701 wl->rx_config &= ~CFG_SSID_FILTER_EN;
702 }
703 if (*total & FIF_CONTROL)
704 wl->rx_filter |= CFG_RX_CTL_EN;
705 if (*total & FIF_OTHER_BSS)
706 wl->rx_filter &= ~CFG_BSSID_FILTER_EN;
707
708 /*
709 * FIXME: workqueues need to be properly cancelled on stop(), for
710 * now let's just disable changing the filter settings. They will
711 * be updated any on config().
712 */
713 /* schedule_work(&wl->filter_work); */
714}
715
716/* HW encryption */
Kalle Valo80301cd2009-06-12 14:17:39 +0300717static int wl1251_set_key_type(struct wl1251 *wl,
718 struct wl1251_cmd_set_keys *key,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300719 enum set_key_cmd cmd,
720 struct ieee80211_key_conf *mac80211_key,
721 const u8 *addr)
722{
723 switch (mac80211_key->alg) {
724 case ALG_WEP:
725 if (is_broadcast_ether_addr(addr))
726 key->key_type = KEY_WEP_DEFAULT;
727 else
728 key->key_type = KEY_WEP_ADDR;
729
730 mac80211_key->hw_key_idx = mac80211_key->keyidx;
731 break;
732 case ALG_TKIP:
733 if (is_broadcast_ether_addr(addr))
734 key->key_type = KEY_TKIP_MIC_GROUP;
735 else
736 key->key_type = KEY_TKIP_MIC_PAIRWISE;
737
738 mac80211_key->hw_key_idx = mac80211_key->keyidx;
739 break;
740 case ALG_CCMP:
741 if (is_broadcast_ether_addr(addr))
742 key->key_type = KEY_AES_GROUP;
743 else
744 key->key_type = KEY_AES_PAIRWISE;
745 mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
746 break;
747 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300748 wl1251_error("Unknown key algo 0x%x", mac80211_key->alg);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300749 return -EOPNOTSUPP;
750 }
751
752 return 0;
753}
754
Kalle Valo80301cd2009-06-12 14:17:39 +0300755static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300756 struct ieee80211_vif *vif,
757 struct ieee80211_sta *sta,
758 struct ieee80211_key_conf *key)
759{
Kalle Valo80301cd2009-06-12 14:17:39 +0300760 struct wl1251 *wl = hw->priv;
761 struct wl1251_cmd_set_keys *wl_cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300762 const u8 *addr;
763 int ret;
764
765 static const u8 bcast_addr[ETH_ALEN] =
766 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
767
Kalle Valo80301cd2009-06-12 14:17:39 +0300768 wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300769
Kalle Valoff258392009-06-12 14:14:19 +0300770 wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
771 if (!wl_cmd) {
772 ret = -ENOMEM;
773 goto out;
774 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300775
776 addr = sta ? sta->addr : bcast_addr;
777
Kalle Valo80301cd2009-06-12 14:17:39 +0300778 wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
779 wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
780 wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300781 key->alg, key->keyidx, key->keylen, key->flags);
Kalle Valo80301cd2009-06-12 14:17:39 +0300782 wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300783
Kalle Valoff258392009-06-12 14:14:19 +0300784 if (is_zero_ether_addr(addr)) {
785 /* We dont support TX only encryption */
786 ret = -EOPNOTSUPP;
787 goto out;
788 }
789
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300790 mutex_lock(&wl->mutex);
791
Kalle Valo80301cd2009-06-12 14:17:39 +0300792 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300793 if (ret < 0)
794 goto out_unlock;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300795
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300796 switch (cmd) {
797 case SET_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300798 wl_cmd->key_action = KEY_ADD_OR_REPLACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300799 break;
800 case DISABLE_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300801 wl_cmd->key_action = KEY_REMOVE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300802 break;
803 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300804 wl1251_error("Unsupported key cmd 0x%x", cmd);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300805 break;
806 }
807
Kalle Valo80301cd2009-06-12 14:17:39 +0300808 ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300809 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300810 wl1251_error("Set KEY type failed");
Kalle Valoc5483b72009-06-12 14:16:32 +0300811 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300812 }
813
Kalle Valoff258392009-06-12 14:14:19 +0300814 if (wl_cmd->key_type != KEY_WEP_DEFAULT)
815 memcpy(wl_cmd->addr, addr, ETH_ALEN);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300816
Kalle Valoff258392009-06-12 14:14:19 +0300817 if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
818 (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300819 /*
820 * We get the key in the following form:
821 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
822 * but the target is expecting:
823 * TKIP - RX MIC - TX MIC
824 */
Kalle Valoff258392009-06-12 14:14:19 +0300825 memcpy(wl_cmd->key, key->key, 16);
826 memcpy(wl_cmd->key + 16, key->key + 24, 8);
827 memcpy(wl_cmd->key + 24, key->key + 16, 8);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300828
829 } else {
Kalle Valoff258392009-06-12 14:14:19 +0300830 memcpy(wl_cmd->key, key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300831 }
Kalle Valoff258392009-06-12 14:14:19 +0300832 wl_cmd->key_size = key->keylen;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300833
Kalle Valoff258392009-06-12 14:14:19 +0300834 wl_cmd->id = key->keyidx;
835 wl_cmd->ssid_profile = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300836
Kalle Valo80301cd2009-06-12 14:17:39 +0300837 wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300838
Kalle Valo80301cd2009-06-12 14:17:39 +0300839 ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
Kalle Valoff258392009-06-12 14:14:19 +0300840 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300841 wl1251_warning("could not set keys");
Kalle Valoc5483b72009-06-12 14:16:32 +0300842 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300843 }
844
Kalle Valoc5483b72009-06-12 14:16:32 +0300845out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300846 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300847
848out_unlock:
Kalle Valoff258392009-06-12 14:14:19 +0300849 mutex_unlock(&wl->mutex);
850
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300851out:
Kalle Valoff258392009-06-12 14:14:19 +0300852 kfree(wl_cmd);
853
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300854 return ret;
855}
856
Kalle Valo80301cd2009-06-12 14:17:39 +0300857static int wl1251_build_basic_rates(char *rates)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300858{
859 u8 index = 0;
860
861 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
862 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
863 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
864 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
865
866 return index;
867}
868
Kalle Valo80301cd2009-06-12 14:17:39 +0300869static int wl1251_build_extended_rates(char *rates)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300870{
871 u8 index = 0;
872
873 rates[index++] = IEEE80211_OFDM_RATE_6MB;
874 rates[index++] = IEEE80211_OFDM_RATE_9MB;
875 rates[index++] = IEEE80211_OFDM_RATE_12MB;
876 rates[index++] = IEEE80211_OFDM_RATE_18MB;
877 rates[index++] = IEEE80211_OFDM_RATE_24MB;
878 rates[index++] = IEEE80211_OFDM_RATE_36MB;
879 rates[index++] = IEEE80211_OFDM_RATE_48MB;
880 rates[index++] = IEEE80211_OFDM_RATE_54MB;
881
882 return index;
883}
884
885
Kalle Valo80301cd2009-06-12 14:17:39 +0300886static int wl1251_build_probe_req(struct wl1251 *wl, u8 *ssid, size_t ssid_len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300887{
888 struct wl12xx_probe_req_template template;
889 struct wl12xx_ie_rates *rates;
890 char *ptr;
891 u16 size;
892
893 ptr = (char *)&template;
894 size = sizeof(struct ieee80211_header);
895
896 memset(template.header.da, 0xff, ETH_ALEN);
897 memset(template.header.bssid, 0xff, ETH_ALEN);
898 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
899 template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
900
901 /* IEs */
902 /* SSID */
903 template.ssid.header.id = WLAN_EID_SSID;
904 template.ssid.header.len = ssid_len;
905 if (ssid_len && ssid)
906 memcpy(template.ssid.ssid, ssid, ssid_len);
907 size += sizeof(struct wl12xx_ie_header) + ssid_len;
908 ptr += size;
909
910 /* Basic Rates */
911 rates = (struct wl12xx_ie_rates *)ptr;
912 rates->header.id = WLAN_EID_SUPP_RATES;
Kalle Valo80301cd2009-06-12 14:17:39 +0300913 rates->header.len = wl1251_build_basic_rates(rates->rates);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300914 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
915 ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
916
917 /* Extended rates */
918 rates = (struct wl12xx_ie_rates *)ptr;
919 rates->header.id = WLAN_EID_EXT_SUPP_RATES;
Kalle Valo80301cd2009-06-12 14:17:39 +0300920 rates->header.len = wl1251_build_extended_rates(rates->rates);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300921 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
922
Kalle Valo80301cd2009-06-12 14:17:39 +0300923 wl1251_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300924
Kalle Valo80301cd2009-06-12 14:17:39 +0300925 return wl1251_cmd_template_set(wl, CMD_PROBE_REQ, &template,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300926 size);
927}
928
Kalle Valo80301cd2009-06-12 14:17:39 +0300929static int wl1251_hw_scan(struct wl1251 *wl, u8 *ssid, size_t len,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300930 u8 active_scan, u8 high_prio, u8 num_channels,
931 u8 probe_requests)
932{
Kalle Valo80301cd2009-06-12 14:17:39 +0300933 struct wl1251_cmd_trigger_scan_to *trigger = NULL;
Kalle Valoff258392009-06-12 14:14:19 +0300934 struct cmd_scan *params = NULL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300935 int i, ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300936 u16 scan_options = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300937
938 if (wl->scanning)
939 return -EINVAL;
940
941 params = kzalloc(sizeof(*params), GFP_KERNEL);
942 if (!params)
943 return -ENOMEM;
944
945 params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
946 params->params.rx_filter_options =
947 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
948
949 /* High priority scan */
950 if (!active_scan)
951 scan_options |= SCAN_PASSIVE;
952 if (high_prio)
953 scan_options |= SCAN_PRIORITY_HIGH;
954 params->params.scan_options = scan_options;
955
956 params->params.num_channels = num_channels;
957 params->params.num_probe_requests = probe_requests;
958 params->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
959 params->params.tid_trigger = 0;
960
961 for (i = 0; i < num_channels; i++) {
962 params->channels[i].min_duration = cpu_to_le32(30000);
963 params->channels[i].max_duration = cpu_to_le32(60000);
964 memset(&params->channels[i].bssid_lsb, 0xff, 4);
965 memset(&params->channels[i].bssid_msb, 0xff, 2);
966 params->channels[i].early_termination = 0;
967 params->channels[i].tx_power_att = 0;
968 params->channels[i].channel = i + 1;
969 memset(params->channels[i].pad, 0, 3);
970 }
971
972 for (i = num_channels; i < SCAN_MAX_NUM_OF_CHANNELS; i++)
973 memset(&params->channels[i], 0,
974 sizeof(struct basic_scan_channel_parameters));
975
976 if (len && ssid) {
977 params->params.ssid_len = len;
978 memcpy(params->params.ssid, ssid, len);
979 } else {
980 params->params.ssid_len = 0;
981 memset(params->params.ssid, 0, 32);
982 }
983
Kalle Valo80301cd2009-06-12 14:17:39 +0300984 ret = wl1251_build_probe_req(wl, ssid, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300985 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300986 wl1251_error("PROBE request template failed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300987 goto out;
988 }
989
Kalle Valoff258392009-06-12 14:14:19 +0300990 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
991 if (!trigger)
992 goto out;
993
994 trigger->timeout = 0;
995
Kalle Valo80301cd2009-06-12 14:17:39 +0300996 ret = wl1251_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
Kalle Valoff258392009-06-12 14:14:19 +0300997 sizeof(*trigger));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300998 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300999 wl1251_error("trigger scan to failed for hw scan");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001000 goto out;
1001 }
1002
Kalle Valo80301cd2009-06-12 14:17:39 +03001003 wl1251_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001004
1005 wl->scanning = true;
1006
Kalle Valo80301cd2009-06-12 14:17:39 +03001007 ret = wl1251_cmd_send(wl, CMD_SCAN, params, sizeof(*params));
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001008 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +03001009 wl1251_error("SCAN failed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001010
Bob Copeland0764de62009-08-07 13:32:56 +03001011 wl1251_mem_read(wl, wl->cmd_box_addr, params, sizeof(*params));
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001012
Kalle Valoff258392009-06-12 14:14:19 +03001013 if (params->header.status != CMD_STATUS_SUCCESS) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001014 wl1251_error("TEST command answer error: %d",
Kalle Valoff258392009-06-12 14:14:19 +03001015 params->header.status);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001016 wl->scanning = false;
1017 ret = -EIO;
1018 goto out;
1019 }
1020
1021out:
1022 kfree(params);
1023 return ret;
1024
1025}
1026
Kalle Valo80301cd2009-06-12 14:17:39 +03001027static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001028 struct cfg80211_scan_request *req)
1029{
Kalle Valo80301cd2009-06-12 14:17:39 +03001030 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001031 int ret;
1032 u8 *ssid = NULL;
1033 size_t ssid_len = 0;
1034
Kalle Valo80301cd2009-06-12 14:17:39 +03001035 wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001036
1037 if (req->n_ssids) {
1038 ssid = req->ssids[0].ssid;
1039 ssid_len = req->ssids[0].ssid_len;
1040 }
1041
1042 mutex_lock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +03001043
Kalle Valo80301cd2009-06-12 14:17:39 +03001044 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001045 if (ret < 0)
1046 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +03001047
Kalle Valo80301cd2009-06-12 14:17:39 +03001048 ret = wl1251_hw_scan(hw->priv, ssid, ssid_len, 1, 0, 13, 3);
Kalle Valo01d9cfb2009-06-12 14:16:26 +03001049
Kalle Valo80301cd2009-06-12 14:17:39 +03001050 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001051
1052out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001053 mutex_unlock(&wl->mutex);
1054
1055 return ret;
1056}
1057
Kalle Valo80301cd2009-06-12 14:17:39 +03001058static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001059{
Kalle Valo80301cd2009-06-12 14:17:39 +03001060 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001061 int ret;
1062
Kalle Valocee4fd22009-06-12 14:16:20 +03001063 mutex_lock(&wl->mutex);
1064
Kalle Valo80301cd2009-06-12 14:17:39 +03001065 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001066 if (ret < 0)
1067 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +03001068
Kalle Valo80301cd2009-06-12 14:17:39 +03001069 ret = wl1251_acx_rts_threshold(wl, (u16) value);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001070 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +03001071 wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001072
Kalle Valo80301cd2009-06-12 14:17:39 +03001073 wl1251_ps_elp_sleep(wl);
Kalle Valo01d9cfb2009-06-12 14:16:26 +03001074
Kalle Valoc5483b72009-06-12 14:16:32 +03001075out:
Kalle Valocee4fd22009-06-12 14:16:20 +03001076 mutex_unlock(&wl->mutex);
1077
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001078 return ret;
1079}
1080
Kalle Valo80301cd2009-06-12 14:17:39 +03001081static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001082 struct ieee80211_vif *vif,
1083 struct ieee80211_bss_conf *bss_conf,
1084 u32 changed)
1085{
Kalle Valo80301cd2009-06-12 14:17:39 +03001086 enum wl1251_cmd_ps_mode mode;
1087 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001088 struct sk_buff *beacon;
1089 int ret;
1090
Kalle Valo80301cd2009-06-12 14:17:39 +03001091 wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001092
1093 mutex_lock(&wl->mutex);
1094
Kalle Valo80301cd2009-06-12 14:17:39 +03001095 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001096 if (ret < 0)
1097 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +03001098
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001099 if (changed & BSS_CHANGED_ASSOC) {
1100 if (bss_conf->assoc) {
Kalle Valoe2fd4612009-08-07 13:34:12 +03001101 wl->beacon_int = bss_conf->beacon_int;
1102 wl->dtim_period = bss_conf->dtim_period;
1103
1104 /* FIXME: call join */
1105
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001106 wl->aid = bss_conf->aid;
1107
Kalle Valo80301cd2009-06-12 14:17:39 +03001108 ret = wl1251_build_ps_poll(wl, wl->aid);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001109 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001110 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001111
Kalle Valo80301cd2009-06-12 14:17:39 +03001112 ret = wl1251_acx_aid(wl, wl->aid);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001113 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001114 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001115
1116 /* If we want to go in PSM but we're not there yet */
1117 if (wl->psm_requested && !wl->psm) {
1118 mode = STATION_POWER_SAVE_MODE;
Kalle Valo80301cd2009-06-12 14:17:39 +03001119 ret = wl1251_ps_set_mode(wl, mode);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001120 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001121 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001122 }
Kalle Valoe2fd4612009-08-07 13:34:12 +03001123 } else {
1124 /* use defaults when not associated */
1125 wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
1126 wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001127 }
1128 }
1129 if (changed & BSS_CHANGED_ERP_SLOT) {
1130 if (bss_conf->use_short_slot)
Kalle Valo80301cd2009-06-12 14:17:39 +03001131 ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001132 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001133 ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001134 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001135 wl1251_warning("Set slot time failed %d", ret);
Kalle Valoc5483b72009-06-12 14:16:32 +03001136 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001137 }
1138 }
1139
1140 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1141 if (bss_conf->use_short_preamble)
Kalle Valo80301cd2009-06-12 14:17:39 +03001142 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001143 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001144 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001145 }
1146
1147 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1148 if (bss_conf->use_cts_prot)
Kalle Valo80301cd2009-06-12 14:17:39 +03001149 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001150 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001151 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001152 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001153 wl1251_warning("Set ctsprotect failed %d", ret);
1154 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001155 }
1156 }
1157
1158 if (changed & BSS_CHANGED_BSSID) {
1159 memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
1160
Kalle Valo80301cd2009-06-12 14:17:39 +03001161 ret = wl1251_build_null_data(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001162 if (ret < 0)
1163 goto out;
1164
1165 if (wl->bss_type != BSS_TYPE_IBSS) {
Kalle Valoae46ae12009-08-07 13:34:42 +03001166 ret = wl1251_join(wl, wl->bss_type, wl->channel,
1167 wl->beacon_int, wl->dtim_period);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001168 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +03001169 goto out_sleep;
1170 wl1251_warning("Set ctsprotect failed %d", ret);
1171 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001172 }
1173 }
1174
1175 if (changed & BSS_CHANGED_BEACON) {
1176 beacon = ieee80211_beacon_get(hw, vif);
Kalle Valo80301cd2009-06-12 14:17:39 +03001177 ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001178 beacon->len);
1179
1180 if (ret < 0) {
1181 dev_kfree_skb(beacon);
1182 goto out;
1183 }
1184
Kalle Valo80301cd2009-06-12 14:17:39 +03001185 ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001186 beacon->len);
1187
1188 dev_kfree_skb(beacon);
1189
1190 if (ret < 0)
1191 goto out;
1192
Kalle Valoae46ae12009-08-07 13:34:42 +03001193 ret = wl1251_join(wl, wl->bss_type, wl->beacon_int,
1194 wl->channel, wl->dtim_period);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001195
1196 if (ret < 0)
1197 goto out;
1198 }
1199
Kalle Valoc5483b72009-06-12 14:16:32 +03001200out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +03001201 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001202
1203out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001204 mutex_unlock(&wl->mutex);
1205}
1206
1207
1208/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001209static struct ieee80211_rate wl1251_rates[] = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001210 { .bitrate = 10,
1211 .hw_value = 0x1,
1212 .hw_value_short = 0x1, },
1213 { .bitrate = 20,
1214 .hw_value = 0x2,
1215 .hw_value_short = 0x2,
1216 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1217 { .bitrate = 55,
1218 .hw_value = 0x4,
1219 .hw_value_short = 0x4,
1220 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1221 { .bitrate = 110,
1222 .hw_value = 0x20,
1223 .hw_value_short = 0x20,
1224 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1225 { .bitrate = 60,
1226 .hw_value = 0x8,
1227 .hw_value_short = 0x8, },
1228 { .bitrate = 90,
1229 .hw_value = 0x10,
1230 .hw_value_short = 0x10, },
1231 { .bitrate = 120,
1232 .hw_value = 0x40,
1233 .hw_value_short = 0x40, },
1234 { .bitrate = 180,
1235 .hw_value = 0x80,
1236 .hw_value_short = 0x80, },
1237 { .bitrate = 240,
1238 .hw_value = 0x200,
1239 .hw_value_short = 0x200, },
1240 { .bitrate = 360,
1241 .hw_value = 0x400,
1242 .hw_value_short = 0x400, },
1243 { .bitrate = 480,
1244 .hw_value = 0x800,
1245 .hw_value_short = 0x800, },
1246 { .bitrate = 540,
1247 .hw_value = 0x1000,
1248 .hw_value_short = 0x1000, },
1249};
1250
1251/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001252static struct ieee80211_channel wl1251_channels[] = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001253 { .hw_value = 1, .center_freq = 2412},
1254 { .hw_value = 2, .center_freq = 2417},
1255 { .hw_value = 3, .center_freq = 2422},
1256 { .hw_value = 4, .center_freq = 2427},
1257 { .hw_value = 5, .center_freq = 2432},
1258 { .hw_value = 6, .center_freq = 2437},
1259 { .hw_value = 7, .center_freq = 2442},
1260 { .hw_value = 8, .center_freq = 2447},
1261 { .hw_value = 9, .center_freq = 2452},
1262 { .hw_value = 10, .center_freq = 2457},
1263 { .hw_value = 11, .center_freq = 2462},
1264 { .hw_value = 12, .center_freq = 2467},
1265 { .hw_value = 13, .center_freq = 2472},
1266};
1267
1268/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001269static struct ieee80211_supported_band wl1251_band_2ghz = {
1270 .channels = wl1251_channels,
1271 .n_channels = ARRAY_SIZE(wl1251_channels),
1272 .bitrates = wl1251_rates,
1273 .n_bitrates = ARRAY_SIZE(wl1251_rates),
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001274};
1275
Kalle Valo80301cd2009-06-12 14:17:39 +03001276static const struct ieee80211_ops wl1251_ops = {
1277 .start = wl1251_op_start,
1278 .stop = wl1251_op_stop,
1279 .add_interface = wl1251_op_add_interface,
1280 .remove_interface = wl1251_op_remove_interface,
1281 .config = wl1251_op_config,
1282 .configure_filter = wl1251_op_configure_filter,
1283 .tx = wl1251_op_tx,
1284 .set_key = wl1251_op_set_key,
1285 .hw_scan = wl1251_op_hw_scan,
1286 .bss_info_changed = wl1251_op_bss_info_changed,
1287 .set_rts_threshold = wl1251_op_set_rts_threshold,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001288};
1289
Kalle Valo80301cd2009-06-12 14:17:39 +03001290static int wl1251_register_hw(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001291{
1292 int ret;
1293
1294 if (wl->mac80211_registered)
1295 return 0;
1296
1297 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1298
1299 ret = ieee80211_register_hw(wl->hw);
1300 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001301 wl1251_error("unable to register mac80211 hw: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001302 return ret;
1303 }
1304
1305 wl->mac80211_registered = true;
1306
Kalle Valo80301cd2009-06-12 14:17:39 +03001307 wl1251_notice("loaded");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001308
1309 return 0;
1310}
1311
Bob Copeland8e639c02009-08-07 13:33:26 +03001312int wl1251_init_ieee80211(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001313{
Bob Copeland8e639c02009-08-07 13:33:26 +03001314 int ret;
1315
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001316 /* The tx descriptor buffer and the TKIP space */
1317 wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +03001318 + WL1251_TKIP_IV_SPACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001319
1320 /* unit us */
1321 /* FIXME: find a proper value */
1322 wl->hw->channel_change_time = 10000;
1323
1324 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
Kalle Valo8c8746f2009-10-27 17:36:25 +02001325 IEEE80211_HW_NOISE_DBM |
Juuso Oikarinen6b21a2c2009-11-17 18:48:30 +02001326 IEEE80211_HW_SUPPORTS_PS |
1327 IEEE80211_HW_BEACON_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001328
1329 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1330 wl->hw->wiphy->max_scan_ssids = 1;
Kalle Valo80301cd2009-06-12 14:17:39 +03001331 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1251_band_2ghz;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001332
Bob Copeland8e639c02009-08-07 13:33:26 +03001333 ret = wl1251_register_hw(wl);
1334 if (ret)
1335 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001336
Bob Copeland8e639c02009-08-07 13:33:26 +03001337 wl1251_debugfs_init(wl);
1338 wl1251_notice("initialized");
1339
1340 ret = 0;
1341
1342out:
1343 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001344}
Bob Copelandaf8c78e2009-08-07 13:33:34 +03001345EXPORT_SYMBOL_GPL(wl1251_init_ieee80211);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001346
Bob Copeland8e639c02009-08-07 13:33:26 +03001347struct ieee80211_hw *wl1251_alloc_hw(void)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001348{
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001349 struct ieee80211_hw *hw;
Kalle Valo80301cd2009-06-12 14:17:39 +03001350 struct wl1251 *wl;
Bob Copeland8e639c02009-08-07 13:33:26 +03001351 int i;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001352 static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1353
Kalle Valo80301cd2009-06-12 14:17:39 +03001354 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001355 if (!hw) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001356 wl1251_error("could not alloc ieee80211_hw");
Bob Copeland8e639c02009-08-07 13:33:26 +03001357 return ERR_PTR(-ENOMEM);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001358 }
1359
1360 wl = hw->priv;
1361 memset(wl, 0, sizeof(*wl));
1362
1363 wl->hw = hw;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001364
1365 wl->data_in_count = 0;
1366
1367 skb_queue_head_init(&wl->tx_queue);
1368
Kalle Valo80301cd2009-06-12 14:17:39 +03001369 INIT_WORK(&wl->filter_work, wl1251_filter_work);
Juuso Oikarinend5da79a2009-11-17 18:48:37 +02001370 INIT_DELAYED_WORK(&wl->elp_work, wl1251_elp_work);
Kalle Valo80301cd2009-06-12 14:17:39 +03001371 wl->channel = WL1251_DEFAULT_CHANNEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001372 wl->scanning = false;
1373 wl->default_key = 0;
1374 wl->listen_int = 1;
1375 wl->rx_counter = 0;
1376 wl->rx_handled = 0;
1377 wl->rx_current_buffer = 0;
1378 wl->rx_last_id = 0;
Kalle Valo80301cd2009-06-12 14:17:39 +03001379 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
1380 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001381 wl->elp = false;
1382 wl->psm = 0;
1383 wl->psm_requested = false;
1384 wl->tx_queue_stopped = false;
Kalle Valo80301cd2009-06-12 14:17:39 +03001385 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
Kalle Valoe2fd4612009-08-07 13:34:12 +03001386 wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
1387 wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
Juuso Oikarinen287f6f92009-11-17 18:48:23 +02001388 wl->vif = NULL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001389
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001390 for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
1391 wl->tx_frames[i] = NULL;
1392
1393 wl->next_tx_complete = 0;
1394
Kalle Valo0e71bb02009-08-07 13:33:57 +03001395 INIT_WORK(&wl->irq_work, wl1251_irq_work);
1396 INIT_WORK(&wl->tx_work, wl1251_tx_work);
1397
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001398 /*
1399 * In case our MAC address is not correctly set,
1400 * we use a random but Nokia MAC.
1401 */
1402 memcpy(wl->mac_addr, nokia_oui, 3);
1403 get_random_bytes(wl->mac_addr + 3, 3);
1404
Kalle Valo80301cd2009-06-12 14:17:39 +03001405 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001406 mutex_init(&wl->mutex);
1407
1408 wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
1409 wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
1410
Kalle Valo47212132009-06-12 14:15:08 +03001411 wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1412 if (!wl->rx_descriptor) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001413 wl1251_error("could not allocate memory for rx descriptor");
Bob Copeland8e639c02009-08-07 13:33:26 +03001414 ieee80211_free_hw(hw);
1415 return ERR_PTR(-ENOMEM);
Kalle Valo47212132009-06-12 14:15:08 +03001416 }
1417
Bob Copeland8e639c02009-08-07 13:33:26 +03001418 return hw;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001419}
Bob Copelandaf8c78e2009-08-07 13:33:34 +03001420EXPORT_SYMBOL_GPL(wl1251_alloc_hw);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001421
Bob Copeland8e639c02009-08-07 13:33:26 +03001422int wl1251_free_hw(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001423{
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001424 ieee80211_unregister_hw(wl->hw);
1425
Kalle Valo80301cd2009-06-12 14:17:39 +03001426 wl1251_debugfs_exit(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001427
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001428 kfree(wl->target_mem_map);
1429 kfree(wl->data_path);
1430 kfree(wl->fw);
1431 wl->fw = NULL;
1432 kfree(wl->nvs);
1433 wl->nvs = NULL;
Kalle Valo47212132009-06-12 14:15:08 +03001434
1435 kfree(wl->rx_descriptor);
1436 wl->rx_descriptor = NULL;
1437
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001438 ieee80211_free_hw(wl->hw);
1439
1440 return 0;
1441}
Bob Copelandaf8c78e2009-08-07 13:33:34 +03001442EXPORT_SYMBOL_GPL(wl1251_free_hw);
1443
1444MODULE_DESCRIPTION("TI wl1251 Wireles LAN Driver Core");
1445MODULE_LICENSE("GPL");
Kalle Valob1b0a2b2009-08-07 13:35:19 +03001446MODULE_AUTHOR("Kalle Valo <kalle.valo@nokia.com>");
Kalle Valo40f54242009-10-13 20:41:20 +03001447MODULE_ALIAS("spi:wl1251");
Ben Hutchings49f146d2009-11-07 22:02:15 +00001448MODULE_FIRMWARE(WL1251_FW_NAME);