blob: 1f4c238a3371dba46a8c51421010144ab1a2c997 [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 Valoc74ddfd2009-11-17 18:48:45 +020031#include <linux/vmalloc.h>
Kalle Valo2f01a1f2009-04-29 23:33:31 +030032
Kalle Valo13674112009-06-12 14:17:25 +030033#include "wl1251.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030034#include "wl12xx_80211.h"
Kalle Valo29d904c2009-08-07 13:35:11 +030035#include "wl1251_reg.h"
Bob Copeland0764de62009-08-07 13:32:56 +030036#include "wl1251_io.h"
Bob Copeland8e639c02009-08-07 13:33:26 +030037#include "wl1251_cmd.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030038#include "wl1251_event.h"
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +030039#include "wl1251_tx.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030040#include "wl1251_rx.h"
41#include "wl1251_ps.h"
42#include "wl1251_init.h"
43#include "wl1251_debugfs.h"
Kalle Valo0e71bb02009-08-07 13:33:57 +030044#include "wl1251_boot.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030045
Bob Copelandb5ed9c12009-08-07 13:33:49 +030046void wl1251_enable_interrupts(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030047{
Bob Copelandb5ed9c12009-08-07 13:33:49 +030048 wl->if_ops->enable_irq(wl);
49}
50
51void wl1251_disable_interrupts(struct wl1251 *wl)
52{
53 wl->if_ops->disable_irq(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030054}
55
Kalle Valo80301cd2009-06-12 14:17:39 +030056static void wl1251_power_off(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030057{
58 wl->set_power(false);
59}
60
Kalle Valo80301cd2009-06-12 14:17:39 +030061static void wl1251_power_on(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030062{
63 wl->set_power(true);
64}
65
Kalle Valo80301cd2009-06-12 14:17:39 +030066static int wl1251_fetch_firmware(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030067{
68 const struct firmware *fw;
Bob Copeland6c766f42009-08-07 13:33:04 +030069 struct device *dev = wiphy_dev(wl->hw->wiphy);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030070 int ret;
71
Kalle Valo0e71bb02009-08-07 13:33:57 +030072 ret = request_firmware(&fw, WL1251_FW_NAME, dev);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030073
74 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +030075 wl1251_error("could not get firmware: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030076 return ret;
77 }
78
79 if (fw->size % 4) {
Kalle Valo80301cd2009-06-12 14:17:39 +030080 wl1251_error("firmware size is not multiple of 32 bits: %zu",
Kalle Valo2f01a1f2009-04-29 23:33:31 +030081 fw->size);
82 ret = -EILSEQ;
83 goto out;
84 }
85
86 wl->fw_len = fw->size;
Kalle Valoc74ddfd2009-11-17 18:48:45 +020087 wl->fw = vmalloc(wl->fw_len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030088
89 if (!wl->fw) {
Kalle Valo80301cd2009-06-12 14:17:39 +030090 wl1251_error("could not allocate memory for the firmware");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030091 ret = -ENOMEM;
92 goto out;
93 }
94
95 memcpy(wl->fw, fw->data, wl->fw_len);
96
97 ret = 0;
98
99out:
100 release_firmware(fw);
101
102 return ret;
103}
104
Kalle Valo80301cd2009-06-12 14:17:39 +0300105static int wl1251_fetch_nvs(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300106{
107 const struct firmware *fw;
Bob Copeland6c766f42009-08-07 13:33:04 +0300108 struct device *dev = wiphy_dev(wl->hw->wiphy);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300109 int ret;
110
Kalle Valo0e71bb02009-08-07 13:33:57 +0300111 ret = request_firmware(&fw, WL1251_NVS_NAME, dev);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300112
113 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300114 wl1251_error("could not get nvs file: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300115 return ret;
116 }
117
118 if (fw->size % 4) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300119 wl1251_error("nvs size is not multiple of 32 bits: %zu",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300120 fw->size);
121 ret = -EILSEQ;
122 goto out;
123 }
124
125 wl->nvs_len = fw->size;
126 wl->nvs = kmalloc(wl->nvs_len, GFP_KERNEL);
127
128 if (!wl->nvs) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300129 wl1251_error("could not allocate memory for the nvs file");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300130 ret = -ENOMEM;
131 goto out;
132 }
133
134 memcpy(wl->nvs, fw->data, wl->nvs_len);
135
136 ret = 0;
137
138out:
139 release_firmware(fw);
140
141 return ret;
142}
143
Kalle Valo80301cd2009-06-12 14:17:39 +0300144static void wl1251_fw_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300145{
146 u32 elp_reg;
147
148 elp_reg = ELPCTRL_WAKE_UP;
Kalle Valo80301cd2009-06-12 14:17:39 +0300149 wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
150 elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300151
Kalle Valo05fac682009-06-12 14:17:47 +0300152 if (!(elp_reg & ELPCTRL_WLAN_READY))
Kalle Valo80301cd2009-06-12 14:17:39 +0300153 wl1251_warning("WLAN not ready");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300154}
155
Kalle Valo80301cd2009-06-12 14:17:39 +0300156static int wl1251_chip_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300157{
158 int ret = 0;
159
Kalle Valo80301cd2009-06-12 14:17:39 +0300160 wl1251_power_on(wl);
Kalle Valo0e71bb02009-08-07 13:33:57 +0300161 msleep(WL1251_POWER_ON_SLEEP);
Bob Copeland08d9f5722009-08-07 13:33:11 +0300162 wl->if_ops->reset(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300163
164 /* We don't need a real memory partition here, because we only want
165 * to use the registers at this point. */
Kalle Valo80301cd2009-06-12 14:17:39 +0300166 wl1251_set_partition(wl,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300167 0x00000000,
168 0x00000000,
169 REGISTERS_BASE,
170 REGISTERS_DOWN_SIZE);
171
172 /* ELP module wake up */
Kalle Valo80301cd2009-06-12 14:17:39 +0300173 wl1251_fw_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300174
175 /* whal_FwCtrl_BootSm() */
176
177 /* 0. read chip id from CHIP_ID */
Kalle Valo0e71bb02009-08-07 13:33:57 +0300178 wl->chip_id = wl1251_reg_read32(wl, CHIP_ID_B);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300179
180 /* 1. check if chip id is valid */
181
Kalle Valo0e71bb02009-08-07 13:33:57 +0300182 switch (wl->chip_id) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300183 case CHIP_ID_1251_PG12:
Kalle Valo80301cd2009-06-12 14:17:39 +0300184 wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG12)",
Kalle Valo0e71bb02009-08-07 13:33:57 +0300185 wl->chip_id);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300186 break;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300187 case CHIP_ID_1251_PG11:
David-John Willis2c759e02009-10-15 14:38:16 +0100188 wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG11)",
189 wl->chip_id);
190 break;
John W. Linville9a493e82009-10-27 15:15:05 -0400191 case CHIP_ID_1251_PG10:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300192 default:
Kalle Valo0e71bb02009-08-07 13:33:57 +0300193 wl1251_error("unsupported chip id: 0x%x", wl->chip_id);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300194 ret = -ENODEV;
195 goto out;
196 }
197
198 if (wl->fw == NULL) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300199 ret = wl1251_fetch_firmware(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300200 if (ret < 0)
201 goto out;
202 }
203
204 /* No NVS from netlink, try to get it from the filesystem */
205 if (wl->nvs == NULL) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300206 ret = wl1251_fetch_nvs(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300207 if (ret < 0)
208 goto out;
209 }
210
211out:
212 return ret;
213}
214
Kalle Valo0e71bb02009-08-07 13:33:57 +0300215static void wl1251_irq_work(struct work_struct *work)
216{
217 u32 intr;
218 struct wl1251 *wl =
219 container_of(work, struct wl1251, irq_work);
220 int ret;
221
222 mutex_lock(&wl->mutex);
223
224 wl1251_debug(DEBUG_IRQ, "IRQ work");
225
226 if (wl->state == WL1251_STATE_OFF)
227 goto out;
228
229 ret = wl1251_ps_elp_wakeup(wl);
230 if (ret < 0)
231 goto out;
232
233 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, WL1251_ACX_INTR_ALL);
234
235 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
236 wl1251_debug(DEBUG_IRQ, "intr: 0x%x", intr);
237
238 if (wl->data_path) {
239 wl->rx_counter =
240 wl1251_mem_read32(wl, wl->data_path->rx_control_addr);
241
242 /* We handle a frmware bug here */
243 switch ((wl->rx_counter - wl->rx_handled) & 0xf) {
244 case 0:
245 wl1251_debug(DEBUG_IRQ, "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("RX: FW and host out of sync: %d",
261 wl->rx_counter - wl->rx_handled);
262 break;
263 }
264
265 wl->rx_handled = wl->rx_counter;
266
267
268 wl1251_debug(DEBUG_IRQ, "RX counter: %d", wl->rx_counter);
269 }
270
271 intr &= wl->intr_mask;
272
273 if (intr == 0) {
274 wl1251_debug(DEBUG_IRQ, "INTR is 0");
275 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK,
276 ~(wl->intr_mask));
277
278 goto out_sleep;
279 }
280
281 if (intr & WL1251_ACX_INTR_RX0_DATA) {
282 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX0_DATA");
283 wl1251_rx(wl);
284 }
285
286 if (intr & WL1251_ACX_INTR_RX1_DATA) {
287 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX1_DATA");
288 wl1251_rx(wl);
289 }
290
291 if (intr & WL1251_ACX_INTR_TX_RESULT) {
292 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_TX_RESULT");
293 wl1251_tx_complete(wl);
294 }
295
296 if (intr & (WL1251_ACX_INTR_EVENT_A | WL1251_ACX_INTR_EVENT_B)) {
297 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT (0x%x)", intr);
298 if (intr & WL1251_ACX_INTR_EVENT_A)
299 wl1251_event_handle(wl, 0);
300 else
301 wl1251_event_handle(wl, 1);
302 }
303
304 if (intr & WL1251_ACX_INTR_INIT_COMPLETE)
305 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_INIT_COMPLETE");
306
307 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, ~(wl->intr_mask));
308
309out_sleep:
310 wl1251_ps_elp_sleep(wl);
311
312out:
313 mutex_unlock(&wl->mutex);
314}
315
Kalle Valoae46ae12009-08-07 13:34:42 +0300316static int wl1251_join(struct wl1251 *wl, u8 bss_type, u8 channel,
317 u16 beacon_interval, u8 dtim_period)
318{
319 int ret;
320
321 ret = wl1251_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
322 DEFAULT_HW_GEN_MODULATION_TYPE,
323 wl->tx_mgmt_frm_rate,
324 wl->tx_mgmt_frm_mod);
325 if (ret < 0)
326 goto out;
327
328
329 ret = wl1251_cmd_join(wl, bss_type, channel, beacon_interval,
330 dtim_period);
331 if (ret < 0)
332 goto out;
333
334 /*
335 * FIXME: we should wait for JOIN_EVENT_COMPLETE_ID but to simplify
336 * locking we just sleep instead, for now
337 */
338 msleep(10);
339
340out:
341 return ret;
342}
343
Kalle Valo80301cd2009-06-12 14:17:39 +0300344static void wl1251_filter_work(struct work_struct *work)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300345{
Kalle Valo80301cd2009-06-12 14:17:39 +0300346 struct wl1251 *wl =
347 container_of(work, struct wl1251, filter_work);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300348 int ret;
349
350 mutex_lock(&wl->mutex);
351
Kalle Valo80301cd2009-06-12 14:17:39 +0300352 if (wl->state == WL1251_STATE_OFF)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300353 goto out;
354
Kalle Valo80301cd2009-06-12 14:17:39 +0300355 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300356 if (ret < 0)
357 goto out;
358
Kalle Valoae46ae12009-08-07 13:34:42 +0300359 ret = wl1251_join(wl, wl->bss_type, wl->channel, wl->beacon_int,
360 wl->dtim_period);
Kalle Valoc5483b72009-06-12 14:16:32 +0300361 if (ret < 0)
362 goto out_sleep;
363
364out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300365 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300366
367out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300368 mutex_unlock(&wl->mutex);
369}
370
Kalle Valo80301cd2009-06-12 14:17:39 +0300371static int wl1251_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300372{
Kalle Valo80301cd2009-06-12 14:17:39 +0300373 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300374
375 skb_queue_tail(&wl->tx_queue, skb);
376
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +0300377 /*
378 * The chip specific setup must run before the first TX packet -
379 * before that, the tx_work will not be initialized!
380 */
381
Kalle Valo16e711f2009-08-07 13:35:04 +0300382 ieee80211_queue_work(wl->hw, &wl->tx_work);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300383
384 /*
385 * The workqueue is slow to process the tx_queue and we need stop
386 * the queue here, otherwise the queue will get too long.
387 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300388 if (skb_queue_len(&wl->tx_queue) >= WL1251_TX_QUEUE_MAX_LENGTH) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300389 ieee80211_stop_queues(wl->hw);
390
391 /*
392 * FIXME: this is racy, the variable is not properly
393 * protected. Maybe fix this by removing the stupid
394 * variable altogether and checking the real queue state?
395 */
396 wl->tx_queue_stopped = true;
397 }
398
399 return NETDEV_TX_OK;
400}
401
Kalle Valo80301cd2009-06-12 14:17:39 +0300402static int wl1251_op_start(struct ieee80211_hw *hw)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300403{
Kalle Valo80301cd2009-06-12 14:17:39 +0300404 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300405 int ret = 0;
406
Kalle Valo80301cd2009-06-12 14:17:39 +0300407 wl1251_debug(DEBUG_MAC80211, "mac80211 start");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300408
409 mutex_lock(&wl->mutex);
410
Kalle Valo80301cd2009-06-12 14:17:39 +0300411 if (wl->state != WL1251_STATE_OFF) {
412 wl1251_error("cannot start because not in off state: %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300413 wl->state);
414 ret = -EBUSY;
415 goto out;
416 }
417
Kalle Valo80301cd2009-06-12 14:17:39 +0300418 ret = wl1251_chip_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300419 if (ret < 0)
Jiri Slabyfe643412009-07-14 22:37:13 +0200420 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300421
Kalle Valo0e71bb02009-08-07 13:33:57 +0300422 ret = wl1251_boot(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300423 if (ret < 0)
424 goto out;
425
Kalle Valo0e71bb02009-08-07 13:33:57 +0300426 ret = wl1251_hw_init(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300427 if (ret < 0)
428 goto out;
429
Kalle Valo80301cd2009-06-12 14:17:39 +0300430 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300431 if (ret < 0)
432 goto out;
433
Kalle Valo80301cd2009-06-12 14:17:39 +0300434 wl->state = WL1251_STATE_ON;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300435
Kalle Valo0e71bb02009-08-07 13:33:57 +0300436 wl1251_info("firmware booted (%s)", wl->fw_ver);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300437
438out:
439 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300440 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300441
442 mutex_unlock(&wl->mutex);
443
444 return ret;
445}
446
Kalle Valo80301cd2009-06-12 14:17:39 +0300447static void wl1251_op_stop(struct ieee80211_hw *hw)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300448{
Kalle Valo80301cd2009-06-12 14:17:39 +0300449 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300450
Kalle Valo80301cd2009-06-12 14:17:39 +0300451 wl1251_info("down");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300452
Kalle Valo80301cd2009-06-12 14:17:39 +0300453 wl1251_debug(DEBUG_MAC80211, "mac80211 stop");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300454
455 mutex_lock(&wl->mutex);
456
Kalle Valo80301cd2009-06-12 14:17:39 +0300457 WARN_ON(wl->state != WL1251_STATE_ON);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300458
459 if (wl->scanning) {
460 mutex_unlock(&wl->mutex);
461 ieee80211_scan_completed(wl->hw, true);
462 mutex_lock(&wl->mutex);
463 wl->scanning = false;
464 }
465
Kalle Valo80301cd2009-06-12 14:17:39 +0300466 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300467
Kalle Valo80301cd2009-06-12 14:17:39 +0300468 wl1251_disable_interrupts(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300469
470 mutex_unlock(&wl->mutex);
471
472 cancel_work_sync(&wl->irq_work);
473 cancel_work_sync(&wl->tx_work);
474 cancel_work_sync(&wl->filter_work);
475
476 mutex_lock(&wl->mutex);
477
478 /* let's notify MAC80211 about the remaining pending TX frames */
Kalle Valo0e71bb02009-08-07 13:33:57 +0300479 wl1251_tx_flush(wl);
Kalle Valo80301cd2009-06-12 14:17:39 +0300480 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300481
482 memset(wl->bssid, 0, ETH_ALEN);
483 wl->listen_int = 1;
484 wl->bss_type = MAX_BSS_TYPE;
485
486 wl->data_in_count = 0;
487 wl->rx_counter = 0;
488 wl->rx_handled = 0;
489 wl->rx_current_buffer = 0;
490 wl->rx_last_id = 0;
491 wl->next_tx_complete = 0;
492 wl->elp = false;
493 wl->psm = 0;
494 wl->tx_queue_stopped = false;
Kalle Valo80301cd2009-06-12 14:17:39 +0300495 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
Kalle Valo97802792009-08-07 13:34:27 +0300496 wl->channel = WL1251_DEFAULT_CHANNEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300497
Kalle Valo80301cd2009-06-12 14:17:39 +0300498 wl1251_debugfs_reset(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300499
500 mutex_unlock(&wl->mutex);
501}
502
Kalle Valo80301cd2009-06-12 14:17:39 +0300503static int wl1251_op_add_interface(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300504 struct ieee80211_if_init_conf *conf)
505{
Kalle Valo80301cd2009-06-12 14:17:39 +0300506 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300507 int ret = 0;
508
Johannes Berge91d8332009-07-15 17:21:41 +0200509 wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
510 conf->type, conf->mac_addr);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300511
512 mutex_lock(&wl->mutex);
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200513 if (wl->vif) {
514 ret = -EBUSY;
515 goto out;
516 }
517
518 wl->vif = conf->vif;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300519
520 switch (conf->type) {
521 case NL80211_IFTYPE_STATION:
522 wl->bss_type = BSS_TYPE_STA_BSS;
523 break;
524 case NL80211_IFTYPE_ADHOC:
525 wl->bss_type = BSS_TYPE_IBSS;
526 break;
527 default:
528 ret = -EOPNOTSUPP;
529 goto out;
530 }
531
532 if (memcmp(wl->mac_addr, conf->mac_addr, ETH_ALEN)) {
533 memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
534 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
Kalle Valo80301cd2009-06-12 14:17:39 +0300535 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300536 if (ret < 0)
537 goto out;
538 }
539
540out:
541 mutex_unlock(&wl->mutex);
542 return ret;
543}
544
Kalle Valo80301cd2009-06-12 14:17:39 +0300545static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300546 struct ieee80211_if_init_conf *conf)
547{
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200548 struct wl1251 *wl = hw->priv;
549
550 mutex_lock(&wl->mutex);
Kalle Valo80301cd2009-06-12 14:17:39 +0300551 wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200552 wl->vif = NULL;
553 mutex_unlock(&wl->mutex);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300554}
555
Kalle Valo80301cd2009-06-12 14:17:39 +0300556static int wl1251_build_null_data(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300557{
558 struct wl12xx_null_data_template template;
559
560 if (!is_zero_ether_addr(wl->bssid)) {
561 memcpy(template.header.da, wl->bssid, ETH_ALEN);
562 memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
563 } else {
564 memset(template.header.da, 0xff, ETH_ALEN);
565 memset(template.header.bssid, 0xff, ETH_ALEN);
566 }
567
568 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
569 template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
570 IEEE80211_STYPE_NULLFUNC);
571
Kalle Valo80301cd2009-06-12 14:17:39 +0300572 return wl1251_cmd_template_set(wl, CMD_NULL_DATA, &template,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300573 sizeof(template));
574
575}
576
Kalle Valo80301cd2009-06-12 14:17:39 +0300577static int wl1251_build_ps_poll(struct wl1251 *wl, u16 aid)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300578{
579 struct wl12xx_ps_poll_template template;
580
581 memcpy(template.bssid, wl->bssid, ETH_ALEN);
582 memcpy(template.ta, wl->mac_addr, ETH_ALEN);
583 template.aid = aid;
584 template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
585
Kalle Valo80301cd2009-06-12 14:17:39 +0300586 return wl1251_cmd_template_set(wl, CMD_PS_POLL, &template,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300587 sizeof(template));
588
589}
590
Kalle Valo80301cd2009-06-12 14:17:39 +0300591static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300592{
Kalle Valo80301cd2009-06-12 14:17:39 +0300593 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300594 struct ieee80211_conf *conf = &hw->conf;
595 int channel, ret = 0;
596
597 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
598
Kalle Valo80301cd2009-06-12 14:17:39 +0300599 wl1251_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300600 channel,
601 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
602 conf->power_level);
603
604 mutex_lock(&wl->mutex);
605
Kalle Valo80301cd2009-06-12 14:17:39 +0300606 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300607 if (ret < 0)
608 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300609
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300610 if (channel != wl->channel) {
Kalle Valofe9a9842009-08-07 13:34:49 +0300611 wl->channel = channel;
612
Kalle Valoae46ae12009-08-07 13:34:42 +0300613 ret = wl1251_join(wl, wl->bss_type, wl->channel,
614 wl->beacon_int, wl->dtim_period);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300615 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300616 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300617 }
618
Kalle Valo80301cd2009-06-12 14:17:39 +0300619 ret = wl1251_build_null_data(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300620 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300621 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300622
623 if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
Luciano Coelho47af3fe2009-06-12 14:17:53 +0300624 wl1251_debug(DEBUG_PSM, "psm enabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300625
626 wl->psm_requested = true;
627
628 /*
629 * We enter PSM only if we're already associated.
630 * If we're not, we'll enter it when joining an SSID,
631 * through the bss_info_changed() hook.
632 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300633 ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300634 } else if (!(conf->flags & IEEE80211_CONF_PS) &&
635 wl->psm_requested) {
Luciano Coelho47af3fe2009-06-12 14:17:53 +0300636 wl1251_debug(DEBUG_PSM, "psm disabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300637
638 wl->psm_requested = false;
639
640 if (wl->psm)
Kalle Valo80301cd2009-06-12 14:17:39 +0300641 ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300642 }
643
644 if (conf->power_level != wl->power_level) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300645 ret = wl1251_acx_tx_power(wl, conf->power_level);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300646 if (ret < 0)
647 goto out;
648
649 wl->power_level = conf->power_level;
650 }
651
Kalle Valoc5483b72009-06-12 14:16:32 +0300652out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300653 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300654
655out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300656 mutex_unlock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +0300657
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300658 return ret;
659}
660
Kalle Valo80301cd2009-06-12 14:17:39 +0300661#define WL1251_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300662 FIF_ALLMULTI | \
663 FIF_FCSFAIL | \
664 FIF_BCN_PRBRESP_PROMISC | \
665 FIF_CONTROL | \
666 FIF_OTHER_BSS)
667
Kalle Valo80301cd2009-06-12 14:17:39 +0300668static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300669 unsigned int changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +0200670 unsigned int *total,u64 multicast)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300671{
Kalle Valo80301cd2009-06-12 14:17:39 +0300672 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300673
Kalle Valo80301cd2009-06-12 14:17:39 +0300674 wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300675
Kalle Valo80301cd2009-06-12 14:17:39 +0300676 *total &= WL1251_SUPPORTED_FILTERS;
677 changed &= WL1251_SUPPORTED_FILTERS;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300678
679 if (changed == 0)
680 /* no filters which we support changed */
681 return;
682
683 /* FIXME: wl->rx_config and wl->rx_filter are not protected */
684
Kalle Valo80301cd2009-06-12 14:17:39 +0300685 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
686 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300687
688 if (*total & FIF_PROMISC_IN_BSS) {
689 wl->rx_config |= CFG_BSSID_FILTER_EN;
690 wl->rx_config |= CFG_RX_ALL_GOOD;
691 }
692 if (*total & FIF_ALLMULTI)
693 /*
694 * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
695 * all multicast frames
696 */
697 wl->rx_config &= ~CFG_MC_FILTER_EN;
698 if (*total & FIF_FCSFAIL)
699 wl->rx_filter |= CFG_RX_FCS_ERROR;
700 if (*total & FIF_BCN_PRBRESP_PROMISC) {
701 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
702 wl->rx_config &= ~CFG_SSID_FILTER_EN;
703 }
704 if (*total & FIF_CONTROL)
705 wl->rx_filter |= CFG_RX_CTL_EN;
706 if (*total & FIF_OTHER_BSS)
707 wl->rx_filter &= ~CFG_BSSID_FILTER_EN;
708
709 /*
710 * FIXME: workqueues need to be properly cancelled on stop(), for
711 * now let's just disable changing the filter settings. They will
712 * be updated any on config().
713 */
714 /* schedule_work(&wl->filter_work); */
715}
716
717/* HW encryption */
Kalle Valo80301cd2009-06-12 14:17:39 +0300718static int wl1251_set_key_type(struct wl1251 *wl,
719 struct wl1251_cmd_set_keys *key,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300720 enum set_key_cmd cmd,
721 struct ieee80211_key_conf *mac80211_key,
722 const u8 *addr)
723{
724 switch (mac80211_key->alg) {
725 case ALG_WEP:
726 if (is_broadcast_ether_addr(addr))
727 key->key_type = KEY_WEP_DEFAULT;
728 else
729 key->key_type = KEY_WEP_ADDR;
730
731 mac80211_key->hw_key_idx = mac80211_key->keyidx;
732 break;
733 case ALG_TKIP:
734 if (is_broadcast_ether_addr(addr))
735 key->key_type = KEY_TKIP_MIC_GROUP;
736 else
737 key->key_type = KEY_TKIP_MIC_PAIRWISE;
738
739 mac80211_key->hw_key_idx = mac80211_key->keyidx;
740 break;
741 case ALG_CCMP:
742 if (is_broadcast_ether_addr(addr))
743 key->key_type = KEY_AES_GROUP;
744 else
745 key->key_type = KEY_AES_PAIRWISE;
746 mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
747 break;
748 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300749 wl1251_error("Unknown key algo 0x%x", mac80211_key->alg);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300750 return -EOPNOTSUPP;
751 }
752
753 return 0;
754}
755
Kalle Valo80301cd2009-06-12 14:17:39 +0300756static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300757 struct ieee80211_vif *vif,
758 struct ieee80211_sta *sta,
759 struct ieee80211_key_conf *key)
760{
Kalle Valo80301cd2009-06-12 14:17:39 +0300761 struct wl1251 *wl = hw->priv;
762 struct wl1251_cmd_set_keys *wl_cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300763 const u8 *addr;
764 int ret;
765
766 static const u8 bcast_addr[ETH_ALEN] =
767 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
768
Kalle Valo80301cd2009-06-12 14:17:39 +0300769 wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300770
Kalle Valoff258392009-06-12 14:14:19 +0300771 wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
772 if (!wl_cmd) {
773 ret = -ENOMEM;
774 goto out;
775 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300776
777 addr = sta ? sta->addr : bcast_addr;
778
Kalle Valo80301cd2009-06-12 14:17:39 +0300779 wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
780 wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
781 wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300782 key->alg, key->keyidx, key->keylen, key->flags);
Kalle Valo80301cd2009-06-12 14:17:39 +0300783 wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300784
Kalle Valoff258392009-06-12 14:14:19 +0300785 if (is_zero_ether_addr(addr)) {
786 /* We dont support TX only encryption */
787 ret = -EOPNOTSUPP;
788 goto out;
789 }
790
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300791 mutex_lock(&wl->mutex);
792
Kalle Valo80301cd2009-06-12 14:17:39 +0300793 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300794 if (ret < 0)
795 goto out_unlock;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300796
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300797 switch (cmd) {
798 case SET_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300799 wl_cmd->key_action = KEY_ADD_OR_REPLACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300800 break;
801 case DISABLE_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300802 wl_cmd->key_action = KEY_REMOVE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300803 break;
804 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300805 wl1251_error("Unsupported key cmd 0x%x", cmd);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300806 break;
807 }
808
Kalle Valo80301cd2009-06-12 14:17:39 +0300809 ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300810 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300811 wl1251_error("Set KEY type failed");
Kalle Valoc5483b72009-06-12 14:16:32 +0300812 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300813 }
814
Kalle Valoff258392009-06-12 14:14:19 +0300815 if (wl_cmd->key_type != KEY_WEP_DEFAULT)
816 memcpy(wl_cmd->addr, addr, ETH_ALEN);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300817
Kalle Valoff258392009-06-12 14:14:19 +0300818 if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
819 (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300820 /*
821 * We get the key in the following form:
822 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
823 * but the target is expecting:
824 * TKIP - RX MIC - TX MIC
825 */
Kalle Valoff258392009-06-12 14:14:19 +0300826 memcpy(wl_cmd->key, key->key, 16);
827 memcpy(wl_cmd->key + 16, key->key + 24, 8);
828 memcpy(wl_cmd->key + 24, key->key + 16, 8);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300829
830 } else {
Kalle Valoff258392009-06-12 14:14:19 +0300831 memcpy(wl_cmd->key, key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300832 }
Kalle Valoff258392009-06-12 14:14:19 +0300833 wl_cmd->key_size = key->keylen;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300834
Kalle Valoff258392009-06-12 14:14:19 +0300835 wl_cmd->id = key->keyidx;
836 wl_cmd->ssid_profile = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300837
Kalle Valo80301cd2009-06-12 14:17:39 +0300838 wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300839
Kalle Valo80301cd2009-06-12 14:17:39 +0300840 ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
Kalle Valoff258392009-06-12 14:14:19 +0300841 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300842 wl1251_warning("could not set keys");
Kalle Valoc5483b72009-06-12 14:16:32 +0300843 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300844 }
845
Kalle Valoc5483b72009-06-12 14:16:32 +0300846out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300847 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300848
849out_unlock:
Kalle Valoff258392009-06-12 14:14:19 +0300850 mutex_unlock(&wl->mutex);
851
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300852out:
Kalle Valoff258392009-06-12 14:14:19 +0300853 kfree(wl_cmd);
854
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300855 return ret;
856}
857
Kalle Valo80301cd2009-06-12 14:17:39 +0300858static int wl1251_build_basic_rates(char *rates)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300859{
860 u8 index = 0;
861
862 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
863 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
864 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
865 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
866
867 return index;
868}
869
Kalle Valo80301cd2009-06-12 14:17:39 +0300870static int wl1251_build_extended_rates(char *rates)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300871{
872 u8 index = 0;
873
874 rates[index++] = IEEE80211_OFDM_RATE_6MB;
875 rates[index++] = IEEE80211_OFDM_RATE_9MB;
876 rates[index++] = IEEE80211_OFDM_RATE_12MB;
877 rates[index++] = IEEE80211_OFDM_RATE_18MB;
878 rates[index++] = IEEE80211_OFDM_RATE_24MB;
879 rates[index++] = IEEE80211_OFDM_RATE_36MB;
880 rates[index++] = IEEE80211_OFDM_RATE_48MB;
881 rates[index++] = IEEE80211_OFDM_RATE_54MB;
882
883 return index;
884}
885
886
Kalle Valo80301cd2009-06-12 14:17:39 +0300887static int wl1251_build_probe_req(struct wl1251 *wl, u8 *ssid, size_t ssid_len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300888{
889 struct wl12xx_probe_req_template template;
890 struct wl12xx_ie_rates *rates;
891 char *ptr;
892 u16 size;
893
894 ptr = (char *)&template;
895 size = sizeof(struct ieee80211_header);
896
897 memset(template.header.da, 0xff, ETH_ALEN);
898 memset(template.header.bssid, 0xff, ETH_ALEN);
899 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
900 template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
901
902 /* IEs */
903 /* SSID */
904 template.ssid.header.id = WLAN_EID_SSID;
905 template.ssid.header.len = ssid_len;
906 if (ssid_len && ssid)
907 memcpy(template.ssid.ssid, ssid, ssid_len);
908 size += sizeof(struct wl12xx_ie_header) + ssid_len;
909 ptr += size;
910
911 /* Basic Rates */
912 rates = (struct wl12xx_ie_rates *)ptr;
913 rates->header.id = WLAN_EID_SUPP_RATES;
Kalle Valo80301cd2009-06-12 14:17:39 +0300914 rates->header.len = wl1251_build_basic_rates(rates->rates);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300915 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
916 ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
917
918 /* Extended rates */
919 rates = (struct wl12xx_ie_rates *)ptr;
920 rates->header.id = WLAN_EID_EXT_SUPP_RATES;
Kalle Valo80301cd2009-06-12 14:17:39 +0300921 rates->header.len = wl1251_build_extended_rates(rates->rates);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300922 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
923
Kalle Valo80301cd2009-06-12 14:17:39 +0300924 wl1251_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300925
Kalle Valo80301cd2009-06-12 14:17:39 +0300926 return wl1251_cmd_template_set(wl, CMD_PROBE_REQ, &template,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300927 size);
928}
929
Kalle Valo80301cd2009-06-12 14:17:39 +0300930static int wl1251_hw_scan(struct wl1251 *wl, u8 *ssid, size_t len,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300931 u8 active_scan, u8 high_prio, u8 num_channels,
932 u8 probe_requests)
933{
Kalle Valo80301cd2009-06-12 14:17:39 +0300934 struct wl1251_cmd_trigger_scan_to *trigger = NULL;
Kalle Valoff258392009-06-12 14:14:19 +0300935 struct cmd_scan *params = NULL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300936 int i, ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300937 u16 scan_options = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300938
939 if (wl->scanning)
940 return -EINVAL;
941
942 params = kzalloc(sizeof(*params), GFP_KERNEL);
943 if (!params)
944 return -ENOMEM;
945
946 params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
947 params->params.rx_filter_options =
948 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
949
950 /* High priority scan */
951 if (!active_scan)
952 scan_options |= SCAN_PASSIVE;
953 if (high_prio)
954 scan_options |= SCAN_PRIORITY_HIGH;
955 params->params.scan_options = scan_options;
956
957 params->params.num_channels = num_channels;
958 params->params.num_probe_requests = probe_requests;
959 params->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
960 params->params.tid_trigger = 0;
961
962 for (i = 0; i < num_channels; i++) {
963 params->channels[i].min_duration = cpu_to_le32(30000);
964 params->channels[i].max_duration = cpu_to_le32(60000);
965 memset(&params->channels[i].bssid_lsb, 0xff, 4);
966 memset(&params->channels[i].bssid_msb, 0xff, 2);
967 params->channels[i].early_termination = 0;
968 params->channels[i].tx_power_att = 0;
969 params->channels[i].channel = i + 1;
970 memset(params->channels[i].pad, 0, 3);
971 }
972
973 for (i = num_channels; i < SCAN_MAX_NUM_OF_CHANNELS; i++)
974 memset(&params->channels[i], 0,
975 sizeof(struct basic_scan_channel_parameters));
976
977 if (len && ssid) {
978 params->params.ssid_len = len;
979 memcpy(params->params.ssid, ssid, len);
980 } else {
981 params->params.ssid_len = 0;
982 memset(params->params.ssid, 0, 32);
983 }
984
Kalle Valo80301cd2009-06-12 14:17:39 +0300985 ret = wl1251_build_probe_req(wl, ssid, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300986 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300987 wl1251_error("PROBE request template failed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300988 goto out;
989 }
990
Kalle Valoff258392009-06-12 14:14:19 +0300991 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
992 if (!trigger)
993 goto out;
994
995 trigger->timeout = 0;
996
Kalle Valo80301cd2009-06-12 14:17:39 +0300997 ret = wl1251_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
Kalle Valoff258392009-06-12 14:14:19 +0300998 sizeof(*trigger));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300999 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001000 wl1251_error("trigger scan to failed for hw scan");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001001 goto out;
1002 }
1003
Kalle Valo80301cd2009-06-12 14:17:39 +03001004 wl1251_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001005
1006 wl->scanning = true;
1007
Kalle Valo80301cd2009-06-12 14:17:39 +03001008 ret = wl1251_cmd_send(wl, CMD_SCAN, params, sizeof(*params));
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001009 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +03001010 wl1251_error("SCAN failed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001011
Bob Copeland0764de62009-08-07 13:32:56 +03001012 wl1251_mem_read(wl, wl->cmd_box_addr, params, sizeof(*params));
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001013
Kalle Valoff258392009-06-12 14:14:19 +03001014 if (params->header.status != CMD_STATUS_SUCCESS) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001015 wl1251_error("TEST command answer error: %d",
Kalle Valoff258392009-06-12 14:14:19 +03001016 params->header.status);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001017 wl->scanning = false;
1018 ret = -EIO;
1019 goto out;
1020 }
1021
1022out:
1023 kfree(params);
1024 return ret;
1025
1026}
1027
Kalle Valo80301cd2009-06-12 14:17:39 +03001028static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001029 struct cfg80211_scan_request *req)
1030{
Kalle Valo80301cd2009-06-12 14:17:39 +03001031 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001032 int ret;
1033 u8 *ssid = NULL;
1034 size_t ssid_len = 0;
1035
Kalle Valo80301cd2009-06-12 14:17:39 +03001036 wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001037
1038 if (req->n_ssids) {
1039 ssid = req->ssids[0].ssid;
1040 ssid_len = req->ssids[0].ssid_len;
1041 }
1042
1043 mutex_lock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +03001044
Kalle Valo80301cd2009-06-12 14:17:39 +03001045 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001046 if (ret < 0)
1047 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +03001048
Kalle Valo80301cd2009-06-12 14:17:39 +03001049 ret = wl1251_hw_scan(hw->priv, ssid, ssid_len, 1, 0, 13, 3);
Kalle Valo01d9cfb2009-06-12 14:16:26 +03001050
Kalle Valo80301cd2009-06-12 14:17:39 +03001051 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001052
1053out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001054 mutex_unlock(&wl->mutex);
1055
1056 return ret;
1057}
1058
Kalle Valo80301cd2009-06-12 14:17:39 +03001059static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001060{
Kalle Valo80301cd2009-06-12 14:17:39 +03001061 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001062 int ret;
1063
Kalle Valocee4fd22009-06-12 14:16:20 +03001064 mutex_lock(&wl->mutex);
1065
Kalle Valo80301cd2009-06-12 14:17:39 +03001066 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001067 if (ret < 0)
1068 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +03001069
Kalle Valo80301cd2009-06-12 14:17:39 +03001070 ret = wl1251_acx_rts_threshold(wl, (u16) value);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001071 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +03001072 wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001073
Kalle Valo80301cd2009-06-12 14:17:39 +03001074 wl1251_ps_elp_sleep(wl);
Kalle Valo01d9cfb2009-06-12 14:16:26 +03001075
Kalle Valoc5483b72009-06-12 14:16:32 +03001076out:
Kalle Valocee4fd22009-06-12 14:16:20 +03001077 mutex_unlock(&wl->mutex);
1078
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001079 return ret;
1080}
1081
Kalle Valo80301cd2009-06-12 14:17:39 +03001082static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001083 struct ieee80211_vif *vif,
1084 struct ieee80211_bss_conf *bss_conf,
1085 u32 changed)
1086{
Kalle Valo80301cd2009-06-12 14:17:39 +03001087 enum wl1251_cmd_ps_mode mode;
1088 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001089 struct sk_buff *beacon;
1090 int ret;
1091
Kalle Valo80301cd2009-06-12 14:17:39 +03001092 wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001093
1094 mutex_lock(&wl->mutex);
1095
Kalle Valo80301cd2009-06-12 14:17:39 +03001096 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001097 if (ret < 0)
1098 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +03001099
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001100 if (changed & BSS_CHANGED_ASSOC) {
1101 if (bss_conf->assoc) {
Kalle Valoe2fd4612009-08-07 13:34:12 +03001102 wl->beacon_int = bss_conf->beacon_int;
1103 wl->dtim_period = bss_conf->dtim_period;
1104
1105 /* FIXME: call join */
1106
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001107 wl->aid = bss_conf->aid;
1108
Kalle Valo80301cd2009-06-12 14:17:39 +03001109 ret = wl1251_build_ps_poll(wl, wl->aid);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001110 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001111 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001112
Kalle Valo80301cd2009-06-12 14:17:39 +03001113 ret = wl1251_acx_aid(wl, wl->aid);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001114 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001115 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001116
1117 /* If we want to go in PSM but we're not there yet */
1118 if (wl->psm_requested && !wl->psm) {
1119 mode = STATION_POWER_SAVE_MODE;
Kalle Valo80301cd2009-06-12 14:17:39 +03001120 ret = wl1251_ps_set_mode(wl, mode);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001121 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001122 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001123 }
Kalle Valoe2fd4612009-08-07 13:34:12 +03001124 } else {
1125 /* use defaults when not associated */
1126 wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
1127 wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001128 }
1129 }
1130 if (changed & BSS_CHANGED_ERP_SLOT) {
1131 if (bss_conf->use_short_slot)
Kalle Valo80301cd2009-06-12 14:17:39 +03001132 ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001133 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001134 ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001135 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001136 wl1251_warning("Set slot time failed %d", ret);
Kalle Valoc5483b72009-06-12 14:16:32 +03001137 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001138 }
1139 }
1140
1141 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1142 if (bss_conf->use_short_preamble)
Kalle Valo80301cd2009-06-12 14:17:39 +03001143 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001144 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001145 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001146 }
1147
1148 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1149 if (bss_conf->use_cts_prot)
Kalle Valo80301cd2009-06-12 14:17:39 +03001150 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001151 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001152 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001153 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001154 wl1251_warning("Set ctsprotect failed %d", ret);
1155 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001156 }
1157 }
1158
1159 if (changed & BSS_CHANGED_BSSID) {
1160 memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
1161
Kalle Valo80301cd2009-06-12 14:17:39 +03001162 ret = wl1251_build_null_data(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001163 if (ret < 0)
1164 goto out;
1165
1166 if (wl->bss_type != BSS_TYPE_IBSS) {
Kalle Valoae46ae12009-08-07 13:34:42 +03001167 ret = wl1251_join(wl, wl->bss_type, wl->channel,
1168 wl->beacon_int, wl->dtim_period);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001169 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +03001170 goto out_sleep;
1171 wl1251_warning("Set ctsprotect failed %d", ret);
1172 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001173 }
1174 }
1175
1176 if (changed & BSS_CHANGED_BEACON) {
1177 beacon = ieee80211_beacon_get(hw, vif);
Kalle Valo80301cd2009-06-12 14:17:39 +03001178 ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001179 beacon->len);
1180
1181 if (ret < 0) {
1182 dev_kfree_skb(beacon);
1183 goto out;
1184 }
1185
Kalle Valo80301cd2009-06-12 14:17:39 +03001186 ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001187 beacon->len);
1188
1189 dev_kfree_skb(beacon);
1190
1191 if (ret < 0)
1192 goto out;
1193
Kalle Valoae46ae12009-08-07 13:34:42 +03001194 ret = wl1251_join(wl, wl->bss_type, wl->beacon_int,
1195 wl->channel, wl->dtim_period);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001196
1197 if (ret < 0)
1198 goto out;
1199 }
1200
Kalle Valoc5483b72009-06-12 14:16:32 +03001201out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +03001202 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001203
1204out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001205 mutex_unlock(&wl->mutex);
1206}
1207
1208
1209/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001210static struct ieee80211_rate wl1251_rates[] = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001211 { .bitrate = 10,
1212 .hw_value = 0x1,
1213 .hw_value_short = 0x1, },
1214 { .bitrate = 20,
1215 .hw_value = 0x2,
1216 .hw_value_short = 0x2,
1217 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1218 { .bitrate = 55,
1219 .hw_value = 0x4,
1220 .hw_value_short = 0x4,
1221 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1222 { .bitrate = 110,
1223 .hw_value = 0x20,
1224 .hw_value_short = 0x20,
1225 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1226 { .bitrate = 60,
1227 .hw_value = 0x8,
1228 .hw_value_short = 0x8, },
1229 { .bitrate = 90,
1230 .hw_value = 0x10,
1231 .hw_value_short = 0x10, },
1232 { .bitrate = 120,
1233 .hw_value = 0x40,
1234 .hw_value_short = 0x40, },
1235 { .bitrate = 180,
1236 .hw_value = 0x80,
1237 .hw_value_short = 0x80, },
1238 { .bitrate = 240,
1239 .hw_value = 0x200,
1240 .hw_value_short = 0x200, },
1241 { .bitrate = 360,
1242 .hw_value = 0x400,
1243 .hw_value_short = 0x400, },
1244 { .bitrate = 480,
1245 .hw_value = 0x800,
1246 .hw_value_short = 0x800, },
1247 { .bitrate = 540,
1248 .hw_value = 0x1000,
1249 .hw_value_short = 0x1000, },
1250};
1251
1252/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001253static struct ieee80211_channel wl1251_channels[] = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001254 { .hw_value = 1, .center_freq = 2412},
1255 { .hw_value = 2, .center_freq = 2417},
1256 { .hw_value = 3, .center_freq = 2422},
1257 { .hw_value = 4, .center_freq = 2427},
1258 { .hw_value = 5, .center_freq = 2432},
1259 { .hw_value = 6, .center_freq = 2437},
1260 { .hw_value = 7, .center_freq = 2442},
1261 { .hw_value = 8, .center_freq = 2447},
1262 { .hw_value = 9, .center_freq = 2452},
1263 { .hw_value = 10, .center_freq = 2457},
1264 { .hw_value = 11, .center_freq = 2462},
1265 { .hw_value = 12, .center_freq = 2467},
1266 { .hw_value = 13, .center_freq = 2472},
1267};
1268
1269/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001270static struct ieee80211_supported_band wl1251_band_2ghz = {
1271 .channels = wl1251_channels,
1272 .n_channels = ARRAY_SIZE(wl1251_channels),
1273 .bitrates = wl1251_rates,
1274 .n_bitrates = ARRAY_SIZE(wl1251_rates),
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001275};
1276
Kalle Valo80301cd2009-06-12 14:17:39 +03001277static const struct ieee80211_ops wl1251_ops = {
1278 .start = wl1251_op_start,
1279 .stop = wl1251_op_stop,
1280 .add_interface = wl1251_op_add_interface,
1281 .remove_interface = wl1251_op_remove_interface,
1282 .config = wl1251_op_config,
1283 .configure_filter = wl1251_op_configure_filter,
1284 .tx = wl1251_op_tx,
1285 .set_key = wl1251_op_set_key,
1286 .hw_scan = wl1251_op_hw_scan,
1287 .bss_info_changed = wl1251_op_bss_info_changed,
1288 .set_rts_threshold = wl1251_op_set_rts_threshold,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001289};
1290
Kalle Valo80301cd2009-06-12 14:17:39 +03001291static int wl1251_register_hw(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001292{
1293 int ret;
1294
1295 if (wl->mac80211_registered)
1296 return 0;
1297
1298 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1299
1300 ret = ieee80211_register_hw(wl->hw);
1301 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001302 wl1251_error("unable to register mac80211 hw: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001303 return ret;
1304 }
1305
1306 wl->mac80211_registered = true;
1307
Kalle Valo80301cd2009-06-12 14:17:39 +03001308 wl1251_notice("loaded");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001309
1310 return 0;
1311}
1312
Bob Copeland8e639c02009-08-07 13:33:26 +03001313int wl1251_init_ieee80211(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001314{
Bob Copeland8e639c02009-08-07 13:33:26 +03001315 int ret;
1316
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001317 /* The tx descriptor buffer and the TKIP space */
1318 wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +03001319 + WL1251_TKIP_IV_SPACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001320
1321 /* unit us */
1322 /* FIXME: find a proper value */
1323 wl->hw->channel_change_time = 10000;
1324
1325 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
Kalle Valo8c8746f2009-10-27 17:36:25 +02001326 IEEE80211_HW_NOISE_DBM |
Juuso Oikarinen6b21a2c2009-11-17 18:48:30 +02001327 IEEE80211_HW_SUPPORTS_PS |
1328 IEEE80211_HW_BEACON_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001329
1330 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1331 wl->hw->wiphy->max_scan_ssids = 1;
Kalle Valo80301cd2009-06-12 14:17:39 +03001332 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1251_band_2ghz;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001333
Bob Copeland8e639c02009-08-07 13:33:26 +03001334 ret = wl1251_register_hw(wl);
1335 if (ret)
1336 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001337
Bob Copeland8e639c02009-08-07 13:33:26 +03001338 wl1251_debugfs_init(wl);
1339 wl1251_notice("initialized");
1340
1341 ret = 0;
1342
1343out:
1344 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001345}
Bob Copelandaf8c78e2009-08-07 13:33:34 +03001346EXPORT_SYMBOL_GPL(wl1251_init_ieee80211);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001347
Bob Copeland8e639c02009-08-07 13:33:26 +03001348struct ieee80211_hw *wl1251_alloc_hw(void)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001349{
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001350 struct ieee80211_hw *hw;
Kalle Valo80301cd2009-06-12 14:17:39 +03001351 struct wl1251 *wl;
Bob Copeland8e639c02009-08-07 13:33:26 +03001352 int i;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001353 static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1354
Kalle Valo80301cd2009-06-12 14:17:39 +03001355 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001356 if (!hw) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001357 wl1251_error("could not alloc ieee80211_hw");
Bob Copeland8e639c02009-08-07 13:33:26 +03001358 return ERR_PTR(-ENOMEM);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001359 }
1360
1361 wl = hw->priv;
1362 memset(wl, 0, sizeof(*wl));
1363
1364 wl->hw = hw;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001365
1366 wl->data_in_count = 0;
1367
1368 skb_queue_head_init(&wl->tx_queue);
1369
Kalle Valo80301cd2009-06-12 14:17:39 +03001370 INIT_WORK(&wl->filter_work, wl1251_filter_work);
Juuso Oikarinend5da79a2009-11-17 18:48:37 +02001371 INIT_DELAYED_WORK(&wl->elp_work, wl1251_elp_work);
Kalle Valo80301cd2009-06-12 14:17:39 +03001372 wl->channel = WL1251_DEFAULT_CHANNEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001373 wl->scanning = false;
1374 wl->default_key = 0;
1375 wl->listen_int = 1;
1376 wl->rx_counter = 0;
1377 wl->rx_handled = 0;
1378 wl->rx_current_buffer = 0;
1379 wl->rx_last_id = 0;
Kalle Valo80301cd2009-06-12 14:17:39 +03001380 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
1381 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001382 wl->elp = false;
1383 wl->psm = 0;
1384 wl->psm_requested = false;
1385 wl->tx_queue_stopped = false;
Kalle Valo80301cd2009-06-12 14:17:39 +03001386 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
Kalle Valoe2fd4612009-08-07 13:34:12 +03001387 wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
1388 wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
Juuso Oikarinen287f6f92009-11-17 18:48:23 +02001389 wl->vif = NULL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001390
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001391 for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
1392 wl->tx_frames[i] = NULL;
1393
1394 wl->next_tx_complete = 0;
1395
Kalle Valo0e71bb02009-08-07 13:33:57 +03001396 INIT_WORK(&wl->irq_work, wl1251_irq_work);
1397 INIT_WORK(&wl->tx_work, wl1251_tx_work);
1398
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001399 /*
1400 * In case our MAC address is not correctly set,
1401 * we use a random but Nokia MAC.
1402 */
1403 memcpy(wl->mac_addr, nokia_oui, 3);
1404 get_random_bytes(wl->mac_addr + 3, 3);
1405
Kalle Valo80301cd2009-06-12 14:17:39 +03001406 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001407 mutex_init(&wl->mutex);
1408
1409 wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
1410 wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
1411
Kalle Valo47212132009-06-12 14:15:08 +03001412 wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1413 if (!wl->rx_descriptor) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001414 wl1251_error("could not allocate memory for rx descriptor");
Bob Copeland8e639c02009-08-07 13:33:26 +03001415 ieee80211_free_hw(hw);
1416 return ERR_PTR(-ENOMEM);
Kalle Valo47212132009-06-12 14:15:08 +03001417 }
1418
Bob Copeland8e639c02009-08-07 13:33:26 +03001419 return hw;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001420}
Bob Copelandaf8c78e2009-08-07 13:33:34 +03001421EXPORT_SYMBOL_GPL(wl1251_alloc_hw);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001422
Bob Copeland8e639c02009-08-07 13:33:26 +03001423int wl1251_free_hw(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001424{
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001425 ieee80211_unregister_hw(wl->hw);
1426
Kalle Valo80301cd2009-06-12 14:17:39 +03001427 wl1251_debugfs_exit(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001428
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001429 kfree(wl->target_mem_map);
1430 kfree(wl->data_path);
Kalle Valoc74ddfd2009-11-17 18:48:45 +02001431 vfree(wl->fw);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001432 wl->fw = NULL;
1433 kfree(wl->nvs);
1434 wl->nvs = NULL;
Kalle Valo47212132009-06-12 14:15:08 +03001435
1436 kfree(wl->rx_descriptor);
1437 wl->rx_descriptor = NULL;
1438
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001439 ieee80211_free_hw(wl->hw);
1440
1441 return 0;
1442}
Bob Copelandaf8c78e2009-08-07 13:33:34 +03001443EXPORT_SYMBOL_GPL(wl1251_free_hw);
1444
1445MODULE_DESCRIPTION("TI wl1251 Wireles LAN Driver Core");
1446MODULE_LICENSE("GPL");
Kalle Valob1b0a2b2009-08-07 13:35:19 +03001447MODULE_AUTHOR("Kalle Valo <kalle.valo@nokia.com>");
Kalle Valo40f54242009-10-13 20:41:20 +03001448MODULE_ALIAS("spi:wl1251");
Ben Hutchings49f146d2009-11-07 22:02:15 +00001449MODULE_FIRMWARE(WL1251_FW_NAME);