blob: 509cbef5e2719e82fedfeda150c4d15c7894eb41 [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>
29#include <linux/spi/spi.h>
30#include <linux/crc32.h>
31#include <linux/etherdevice.h>
32#include <linux/spi/wl12xx.h>
33
Kalle Valo13674112009-06-12 14:17:25 +030034#include "wl1251.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030035#include "wl12xx_80211.h"
36#include "reg.h"
Kalle Valoe6f0b5c2009-06-12 14:16:58 +030037#include "wl1251_ops.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030038#include "wl1251_spi.h"
39#include "wl1251_event.h"
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +030040#include "wl1251_tx.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030041#include "wl1251_rx.h"
42#include "wl1251_ps.h"
43#include "wl1251_init.h"
44#include "wl1251_debugfs.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030045
Kalle Valo80301cd2009-06-12 14:17:39 +030046static void wl1251_disable_interrupts(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030047{
48 disable_irq(wl->irq);
49}
50
Kalle Valo80301cd2009-06-12 14:17:39 +030051static void wl1251_power_off(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030052{
53 wl->set_power(false);
54}
55
Kalle Valo80301cd2009-06-12 14:17:39 +030056static void wl1251_power_on(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030057{
58 wl->set_power(true);
59}
60
Kalle Valo80301cd2009-06-12 14:17:39 +030061static irqreturn_t wl1251_irq(int irq, void *cookie)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030062{
Kalle Valo80301cd2009-06-12 14:17:39 +030063 struct wl1251 *wl;
Kalle Valo2f01a1f2009-04-29 23:33:31 +030064
Kalle Valo80301cd2009-06-12 14:17:39 +030065 wl1251_debug(DEBUG_IRQ, "IRQ");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030066
67 wl = cookie;
68
69 schedule_work(&wl->irq_work);
70
71 return IRQ_HANDLED;
72}
73
Kalle Valo80301cd2009-06-12 14:17:39 +030074static int wl1251_fetch_firmware(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030075{
76 const struct firmware *fw;
77 int ret;
78
79 ret = request_firmware(&fw, wl->chip.fw_filename, &wl->spi->dev);
80
81 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +030082 wl1251_error("could not get firmware: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030083 return ret;
84 }
85
86 if (fw->size % 4) {
Kalle Valo80301cd2009-06-12 14:17:39 +030087 wl1251_error("firmware size is not multiple of 32 bits: %zu",
Kalle Valo2f01a1f2009-04-29 23:33:31 +030088 fw->size);
89 ret = -EILSEQ;
90 goto out;
91 }
92
93 wl->fw_len = fw->size;
94 wl->fw = kmalloc(wl->fw_len, GFP_KERNEL);
95
96 if (!wl->fw) {
Kalle Valo80301cd2009-06-12 14:17:39 +030097 wl1251_error("could not allocate memory for the firmware");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030098 ret = -ENOMEM;
99 goto out;
100 }
101
102 memcpy(wl->fw, fw->data, wl->fw_len);
103
104 ret = 0;
105
106out:
107 release_firmware(fw);
108
109 return ret;
110}
111
Kalle Valo80301cd2009-06-12 14:17:39 +0300112static int wl1251_fetch_nvs(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300113{
114 const struct firmware *fw;
115 int ret;
116
117 ret = request_firmware(&fw, wl->chip.nvs_filename, &wl->spi->dev);
118
119 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300120 wl1251_error("could not get nvs file: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300121 return ret;
122 }
123
124 if (fw->size % 4) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300125 wl1251_error("nvs size is not multiple of 32 bits: %zu",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300126 fw->size);
127 ret = -EILSEQ;
128 goto out;
129 }
130
131 wl->nvs_len = fw->size;
132 wl->nvs = kmalloc(wl->nvs_len, GFP_KERNEL);
133
134 if (!wl->nvs) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300135 wl1251_error("could not allocate memory for the nvs file");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300136 ret = -ENOMEM;
137 goto out;
138 }
139
140 memcpy(wl->nvs, fw->data, wl->nvs_len);
141
142 ret = 0;
143
144out:
145 release_firmware(fw);
146
147 return ret;
148}
149
Kalle Valo80301cd2009-06-12 14:17:39 +0300150static void wl1251_fw_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300151{
152 u32 elp_reg;
153
154 elp_reg = ELPCTRL_WAKE_UP;
Kalle Valo80301cd2009-06-12 14:17:39 +0300155 wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
156 elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300157
Kalle Valo05fac682009-06-12 14:17:47 +0300158 if (!(elp_reg & ELPCTRL_WLAN_READY))
Kalle Valo80301cd2009-06-12 14:17:39 +0300159 wl1251_warning("WLAN not ready");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300160}
161
Kalle Valo80301cd2009-06-12 14:17:39 +0300162static int wl1251_chip_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300163{
164 int ret = 0;
165
Kalle Valo80301cd2009-06-12 14:17:39 +0300166 wl1251_power_on(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300167 msleep(wl->chip.power_on_sleep);
Kalle Valo80301cd2009-06-12 14:17:39 +0300168 wl1251_spi_reset(wl);
169 wl1251_spi_init(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300170
171 /* We don't need a real memory partition here, because we only want
172 * to use the registers at this point. */
Kalle Valo80301cd2009-06-12 14:17:39 +0300173 wl1251_set_partition(wl,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300174 0x00000000,
175 0x00000000,
176 REGISTERS_BASE,
177 REGISTERS_DOWN_SIZE);
178
179 /* ELP module wake up */
Kalle Valo80301cd2009-06-12 14:17:39 +0300180 wl1251_fw_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300181
182 /* whal_FwCtrl_BootSm() */
183
184 /* 0. read chip id from CHIP_ID */
Kalle Valo80301cd2009-06-12 14:17:39 +0300185 wl->chip.id = wl1251_reg_read32(wl, CHIP_ID_B);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300186
187 /* 1. check if chip id is valid */
188
189 switch (wl->chip.id) {
190 case CHIP_ID_1251_PG12:
Kalle Valo80301cd2009-06-12 14:17:39 +0300191 wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG12)",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300192 wl->chip.id);
193
194 wl1251_setup(wl);
195
196 break;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300197 case CHIP_ID_1251_PG10:
198 case CHIP_ID_1251_PG11:
199 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300200 wl1251_error("unsupported chip id: 0x%x", wl->chip.id);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300201 ret = -ENODEV;
202 goto out;
203 }
204
205 if (wl->fw == NULL) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300206 ret = wl1251_fetch_firmware(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300207 if (ret < 0)
208 goto out;
209 }
210
211 /* No NVS from netlink, try to get it from the filesystem */
212 if (wl->nvs == NULL) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300213 ret = wl1251_fetch_nvs(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300214 if (ret < 0)
215 goto out;
216 }
217
218out:
219 return ret;
220}
221
Kalle Valo80301cd2009-06-12 14:17:39 +0300222static void wl1251_filter_work(struct work_struct *work)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300223{
Kalle Valo80301cd2009-06-12 14:17:39 +0300224 struct wl1251 *wl =
225 container_of(work, struct wl1251, filter_work);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300226 int ret;
227
228 mutex_lock(&wl->mutex);
229
Kalle Valo80301cd2009-06-12 14:17:39 +0300230 if (wl->state == WL1251_STATE_OFF)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300231 goto out;
232
Kalle Valo80301cd2009-06-12 14:17:39 +0300233 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300234 if (ret < 0)
235 goto out;
236
Juuso Oikarinen77cc9e42009-06-12 14:16:52 +0300237 /* FIXME: replace the magic numbers with proper definitions */
238 ret = wl->chip.op_cmd_join(wl, wl->bss_type, 1, 100, 0);
Kalle Valoc5483b72009-06-12 14:16:32 +0300239 if (ret < 0)
240 goto out_sleep;
241
242out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300243 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300244
245out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300246 mutex_unlock(&wl->mutex);
247}
248
Kalle Valo80301cd2009-06-12 14:17:39 +0300249int wl1251_plt_start(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300250{
251 int ret;
252
Kalle Valo80301cd2009-06-12 14:17:39 +0300253 mutex_lock(&wl->mutex);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300254
Kalle Valo80301cd2009-06-12 14:17:39 +0300255 wl1251_notice("power up");
256
257 if (wl->state != WL1251_STATE_OFF) {
258 wl1251_error("cannot go into PLT state because not "
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300259 "in off state: %d", wl->state);
260 return -EBUSY;
261 }
262
Kalle Valo80301cd2009-06-12 14:17:39 +0300263 wl->state = WL1251_STATE_PLT;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300264
Kalle Valo80301cd2009-06-12 14:17:39 +0300265 ret = wl1251_chip_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300266 if (ret < 0)
267 return ret;
268
269 ret = wl->chip.op_boot(wl);
270 if (ret < 0)
271 return ret;
272
Kalle Valo80301cd2009-06-12 14:17:39 +0300273 wl1251_notice("firmware booted in PLT mode (%s)", wl->chip.fw_ver);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300274
275 ret = wl->chip.op_plt_init(wl);
276 if (ret < 0)
277 return ret;
278
279 return 0;
280}
281
Kalle Valo80301cd2009-06-12 14:17:39 +0300282int wl1251_plt_stop(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300283{
Kalle Valo80301cd2009-06-12 14:17:39 +0300284 mutex_lock(&wl->mutex);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300285
Kalle Valo80301cd2009-06-12 14:17:39 +0300286 wl1251_notice("power down");
287
288 if (wl->state != WL1251_STATE_PLT) {
289 wl1251_error("cannot power down because not in PLT "
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300290 "state: %d", wl->state);
291 return -EBUSY;
292 }
293
Kalle Valo80301cd2009-06-12 14:17:39 +0300294 wl1251_disable_interrupts(wl);
295 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300296
Kalle Valo80301cd2009-06-12 14:17:39 +0300297 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300298
299 return 0;
300}
301
302
Kalle Valo80301cd2009-06-12 14:17:39 +0300303static int wl1251_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300304{
Kalle Valo80301cd2009-06-12 14:17:39 +0300305 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300306
307 skb_queue_tail(&wl->tx_queue, skb);
308
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +0300309 /*
310 * The chip specific setup must run before the first TX packet -
311 * before that, the tx_work will not be initialized!
312 */
313
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300314 schedule_work(&wl->tx_work);
315
316 /*
317 * The workqueue is slow to process the tx_queue and we need stop
318 * the queue here, otherwise the queue will get too long.
319 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300320 if (skb_queue_len(&wl->tx_queue) >= WL1251_TX_QUEUE_MAX_LENGTH) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300321 ieee80211_stop_queues(wl->hw);
322
323 /*
324 * FIXME: this is racy, the variable is not properly
325 * protected. Maybe fix this by removing the stupid
326 * variable altogether and checking the real queue state?
327 */
328 wl->tx_queue_stopped = true;
329 }
330
331 return NETDEV_TX_OK;
332}
333
Kalle Valo80301cd2009-06-12 14:17:39 +0300334static int wl1251_op_start(struct ieee80211_hw *hw)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300335{
Kalle Valo80301cd2009-06-12 14:17:39 +0300336 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300337 int ret = 0;
338
Kalle Valo80301cd2009-06-12 14:17:39 +0300339 wl1251_debug(DEBUG_MAC80211, "mac80211 start");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300340
341 mutex_lock(&wl->mutex);
342
Kalle Valo80301cd2009-06-12 14:17:39 +0300343 if (wl->state != WL1251_STATE_OFF) {
344 wl1251_error("cannot start because not in off state: %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300345 wl->state);
346 ret = -EBUSY;
347 goto out;
348 }
349
Kalle Valo80301cd2009-06-12 14:17:39 +0300350 ret = wl1251_chip_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300351 if (ret < 0)
Jiri Slabyfe643412009-07-14 22:37:13 +0200352 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300353
354 ret = wl->chip.op_boot(wl);
355 if (ret < 0)
356 goto out;
357
358 ret = wl->chip.op_hw_init(wl);
359 if (ret < 0)
360 goto out;
361
Kalle Valo80301cd2009-06-12 14:17:39 +0300362 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300363 if (ret < 0)
364 goto out;
365
Kalle Valo80301cd2009-06-12 14:17:39 +0300366 wl->state = WL1251_STATE_ON;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300367
Kalle Valo80301cd2009-06-12 14:17:39 +0300368 wl1251_info("firmware booted (%s)", wl->chip.fw_ver);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300369
370out:
371 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300372 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300373
374 mutex_unlock(&wl->mutex);
375
376 return ret;
377}
378
Kalle Valo80301cd2009-06-12 14:17:39 +0300379static void wl1251_op_stop(struct ieee80211_hw *hw)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300380{
Kalle Valo80301cd2009-06-12 14:17:39 +0300381 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300382
Kalle Valo80301cd2009-06-12 14:17:39 +0300383 wl1251_info("down");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300384
Kalle Valo80301cd2009-06-12 14:17:39 +0300385 wl1251_debug(DEBUG_MAC80211, "mac80211 stop");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300386
387 mutex_lock(&wl->mutex);
388
Kalle Valo80301cd2009-06-12 14:17:39 +0300389 WARN_ON(wl->state != WL1251_STATE_ON);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300390
391 if (wl->scanning) {
392 mutex_unlock(&wl->mutex);
393 ieee80211_scan_completed(wl->hw, true);
394 mutex_lock(&wl->mutex);
395 wl->scanning = false;
396 }
397
Kalle Valo80301cd2009-06-12 14:17:39 +0300398 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300399
Kalle Valo80301cd2009-06-12 14:17:39 +0300400 wl1251_disable_interrupts(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300401
402 mutex_unlock(&wl->mutex);
403
404 cancel_work_sync(&wl->irq_work);
405 cancel_work_sync(&wl->tx_work);
406 cancel_work_sync(&wl->filter_work);
407
408 mutex_lock(&wl->mutex);
409
410 /* let's notify MAC80211 about the remaining pending TX frames */
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +0300411 wl->chip.op_tx_flush(wl);
Kalle Valo80301cd2009-06-12 14:17:39 +0300412 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300413
414 memset(wl->bssid, 0, ETH_ALEN);
415 wl->listen_int = 1;
416 wl->bss_type = MAX_BSS_TYPE;
417
418 wl->data_in_count = 0;
419 wl->rx_counter = 0;
420 wl->rx_handled = 0;
421 wl->rx_current_buffer = 0;
422 wl->rx_last_id = 0;
423 wl->next_tx_complete = 0;
424 wl->elp = false;
425 wl->psm = 0;
426 wl->tx_queue_stopped = false;
Kalle Valo80301cd2009-06-12 14:17:39 +0300427 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300428
Kalle Valo80301cd2009-06-12 14:17:39 +0300429 wl1251_debugfs_reset(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300430
431 mutex_unlock(&wl->mutex);
432}
433
Kalle Valo80301cd2009-06-12 14:17:39 +0300434static int wl1251_op_add_interface(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300435 struct ieee80211_if_init_conf *conf)
436{
Kalle Valo80301cd2009-06-12 14:17:39 +0300437 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300438 int ret = 0;
439
Johannes Berge91d8332009-07-15 17:21:41 +0200440 wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
441 conf->type, conf->mac_addr);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300442
443 mutex_lock(&wl->mutex);
444
445 switch (conf->type) {
446 case NL80211_IFTYPE_STATION:
447 wl->bss_type = BSS_TYPE_STA_BSS;
448 break;
449 case NL80211_IFTYPE_ADHOC:
450 wl->bss_type = BSS_TYPE_IBSS;
451 break;
452 default:
453 ret = -EOPNOTSUPP;
454 goto out;
455 }
456
457 if (memcmp(wl->mac_addr, conf->mac_addr, ETH_ALEN)) {
458 memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
459 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
Kalle Valo80301cd2009-06-12 14:17:39 +0300460 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300461 if (ret < 0)
462 goto out;
463 }
464
465out:
466 mutex_unlock(&wl->mutex);
467 return ret;
468}
469
Kalle Valo80301cd2009-06-12 14:17:39 +0300470static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300471 struct ieee80211_if_init_conf *conf)
472{
Kalle Valo80301cd2009-06-12 14:17:39 +0300473 wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300474}
475
Kalle Valo80301cd2009-06-12 14:17:39 +0300476static int wl1251_build_null_data(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300477{
478 struct wl12xx_null_data_template template;
479
480 if (!is_zero_ether_addr(wl->bssid)) {
481 memcpy(template.header.da, wl->bssid, ETH_ALEN);
482 memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
483 } else {
484 memset(template.header.da, 0xff, ETH_ALEN);
485 memset(template.header.bssid, 0xff, ETH_ALEN);
486 }
487
488 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
489 template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
490 IEEE80211_STYPE_NULLFUNC);
491
Kalle Valo80301cd2009-06-12 14:17:39 +0300492 return wl1251_cmd_template_set(wl, CMD_NULL_DATA, &template,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300493 sizeof(template));
494
495}
496
Kalle Valo80301cd2009-06-12 14:17:39 +0300497static int wl1251_build_ps_poll(struct wl1251 *wl, u16 aid)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300498{
499 struct wl12xx_ps_poll_template template;
500
501 memcpy(template.bssid, wl->bssid, ETH_ALEN);
502 memcpy(template.ta, wl->mac_addr, ETH_ALEN);
503 template.aid = aid;
504 template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
505
Kalle Valo80301cd2009-06-12 14:17:39 +0300506 return wl1251_cmd_template_set(wl, CMD_PS_POLL, &template,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300507 sizeof(template));
508
509}
510
Kalle Valo80301cd2009-06-12 14:17:39 +0300511static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300512{
Kalle Valo80301cd2009-06-12 14:17:39 +0300513 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300514 struct ieee80211_conf *conf = &hw->conf;
515 int channel, ret = 0;
516
517 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
518
Kalle Valo80301cd2009-06-12 14:17:39 +0300519 wl1251_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300520 channel,
521 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
522 conf->power_level);
523
524 mutex_lock(&wl->mutex);
525
Kalle Valo80301cd2009-06-12 14:17:39 +0300526 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300527 if (ret < 0)
528 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300529
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300530 if (channel != wl->channel) {
531 /* FIXME: use beacon interval provided by mac80211 */
Juuso Oikarinen77cc9e42009-06-12 14:16:52 +0300532 ret = wl->chip.op_cmd_join(wl, wl->bss_type, 1, 100, 0);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300533 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300534 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300535
536 wl->channel = channel;
537 }
538
Kalle Valo80301cd2009-06-12 14:17:39 +0300539 ret = wl1251_build_null_data(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300540 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300541 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300542
543 if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
Luciano Coelho47af3fe2009-06-12 14:17:53 +0300544 wl1251_debug(DEBUG_PSM, "psm enabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300545
546 wl->psm_requested = true;
547
548 /*
549 * We enter PSM only if we're already associated.
550 * If we're not, we'll enter it when joining an SSID,
551 * through the bss_info_changed() hook.
552 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300553 ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300554 } else if (!(conf->flags & IEEE80211_CONF_PS) &&
555 wl->psm_requested) {
Luciano Coelho47af3fe2009-06-12 14:17:53 +0300556 wl1251_debug(DEBUG_PSM, "psm disabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300557
558 wl->psm_requested = false;
559
560 if (wl->psm)
Kalle Valo80301cd2009-06-12 14:17:39 +0300561 ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300562 }
563
564 if (conf->power_level != wl->power_level) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300565 ret = wl1251_acx_tx_power(wl, conf->power_level);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300566 if (ret < 0)
567 goto out;
568
569 wl->power_level = conf->power_level;
570 }
571
Kalle Valoc5483b72009-06-12 14:16:32 +0300572out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300573 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300574
575out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300576 mutex_unlock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +0300577
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300578 return ret;
579}
580
Kalle Valo80301cd2009-06-12 14:17:39 +0300581#define WL1251_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300582 FIF_ALLMULTI | \
583 FIF_FCSFAIL | \
584 FIF_BCN_PRBRESP_PROMISC | \
585 FIF_CONTROL | \
586 FIF_OTHER_BSS)
587
Kalle Valo80301cd2009-06-12 14:17:39 +0300588static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300589 unsigned int changed,
590 unsigned int *total,
591 int mc_count,
592 struct dev_addr_list *mc_list)
593{
Kalle Valo80301cd2009-06-12 14:17:39 +0300594 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300595
Kalle Valo80301cd2009-06-12 14:17:39 +0300596 wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300597
Kalle Valo80301cd2009-06-12 14:17:39 +0300598 *total &= WL1251_SUPPORTED_FILTERS;
599 changed &= WL1251_SUPPORTED_FILTERS;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300600
601 if (changed == 0)
602 /* no filters which we support changed */
603 return;
604
605 /* FIXME: wl->rx_config and wl->rx_filter are not protected */
606
Kalle Valo80301cd2009-06-12 14:17:39 +0300607 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
608 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300609
610 if (*total & FIF_PROMISC_IN_BSS) {
611 wl->rx_config |= CFG_BSSID_FILTER_EN;
612 wl->rx_config |= CFG_RX_ALL_GOOD;
613 }
614 if (*total & FIF_ALLMULTI)
615 /*
616 * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
617 * all multicast frames
618 */
619 wl->rx_config &= ~CFG_MC_FILTER_EN;
620 if (*total & FIF_FCSFAIL)
621 wl->rx_filter |= CFG_RX_FCS_ERROR;
622 if (*total & FIF_BCN_PRBRESP_PROMISC) {
623 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
624 wl->rx_config &= ~CFG_SSID_FILTER_EN;
625 }
626 if (*total & FIF_CONTROL)
627 wl->rx_filter |= CFG_RX_CTL_EN;
628 if (*total & FIF_OTHER_BSS)
629 wl->rx_filter &= ~CFG_BSSID_FILTER_EN;
630
631 /*
632 * FIXME: workqueues need to be properly cancelled on stop(), for
633 * now let's just disable changing the filter settings. They will
634 * be updated any on config().
635 */
636 /* schedule_work(&wl->filter_work); */
637}
638
639/* HW encryption */
Kalle Valo80301cd2009-06-12 14:17:39 +0300640static int wl1251_set_key_type(struct wl1251 *wl,
641 struct wl1251_cmd_set_keys *key,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300642 enum set_key_cmd cmd,
643 struct ieee80211_key_conf *mac80211_key,
644 const u8 *addr)
645{
646 switch (mac80211_key->alg) {
647 case ALG_WEP:
648 if (is_broadcast_ether_addr(addr))
649 key->key_type = KEY_WEP_DEFAULT;
650 else
651 key->key_type = KEY_WEP_ADDR;
652
653 mac80211_key->hw_key_idx = mac80211_key->keyidx;
654 break;
655 case ALG_TKIP:
656 if (is_broadcast_ether_addr(addr))
657 key->key_type = KEY_TKIP_MIC_GROUP;
658 else
659 key->key_type = KEY_TKIP_MIC_PAIRWISE;
660
661 mac80211_key->hw_key_idx = mac80211_key->keyidx;
662 break;
663 case ALG_CCMP:
664 if (is_broadcast_ether_addr(addr))
665 key->key_type = KEY_AES_GROUP;
666 else
667 key->key_type = KEY_AES_PAIRWISE;
668 mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
669 break;
670 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300671 wl1251_error("Unknown key algo 0x%x", mac80211_key->alg);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300672 return -EOPNOTSUPP;
673 }
674
675 return 0;
676}
677
Kalle Valo80301cd2009-06-12 14:17:39 +0300678static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300679 struct ieee80211_vif *vif,
680 struct ieee80211_sta *sta,
681 struct ieee80211_key_conf *key)
682{
Kalle Valo80301cd2009-06-12 14:17:39 +0300683 struct wl1251 *wl = hw->priv;
684 struct wl1251_cmd_set_keys *wl_cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300685 const u8 *addr;
686 int ret;
687
688 static const u8 bcast_addr[ETH_ALEN] =
689 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
690
Kalle Valo80301cd2009-06-12 14:17:39 +0300691 wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300692
Kalle Valoff258392009-06-12 14:14:19 +0300693 wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
694 if (!wl_cmd) {
695 ret = -ENOMEM;
696 goto out;
697 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300698
699 addr = sta ? sta->addr : bcast_addr;
700
Kalle Valo80301cd2009-06-12 14:17:39 +0300701 wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
702 wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
703 wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300704 key->alg, key->keyidx, key->keylen, key->flags);
Kalle Valo80301cd2009-06-12 14:17:39 +0300705 wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300706
Kalle Valoff258392009-06-12 14:14:19 +0300707 if (is_zero_ether_addr(addr)) {
708 /* We dont support TX only encryption */
709 ret = -EOPNOTSUPP;
710 goto out;
711 }
712
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300713 mutex_lock(&wl->mutex);
714
Kalle Valo80301cd2009-06-12 14:17:39 +0300715 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300716 if (ret < 0)
717 goto out_unlock;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300718
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300719 switch (cmd) {
720 case SET_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300721 wl_cmd->key_action = KEY_ADD_OR_REPLACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300722 break;
723 case DISABLE_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300724 wl_cmd->key_action = KEY_REMOVE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300725 break;
726 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300727 wl1251_error("Unsupported key cmd 0x%x", cmd);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300728 break;
729 }
730
Kalle Valo80301cd2009-06-12 14:17:39 +0300731 ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300732 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300733 wl1251_error("Set KEY type failed");
Kalle Valoc5483b72009-06-12 14:16:32 +0300734 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300735 }
736
Kalle Valoff258392009-06-12 14:14:19 +0300737 if (wl_cmd->key_type != KEY_WEP_DEFAULT)
738 memcpy(wl_cmd->addr, addr, ETH_ALEN);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300739
Kalle Valoff258392009-06-12 14:14:19 +0300740 if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
741 (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300742 /*
743 * We get the key in the following form:
744 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
745 * but the target is expecting:
746 * TKIP - RX MIC - TX MIC
747 */
Kalle Valoff258392009-06-12 14:14:19 +0300748 memcpy(wl_cmd->key, key->key, 16);
749 memcpy(wl_cmd->key + 16, key->key + 24, 8);
750 memcpy(wl_cmd->key + 24, key->key + 16, 8);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300751
752 } else {
Kalle Valoff258392009-06-12 14:14:19 +0300753 memcpy(wl_cmd->key, key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300754 }
Kalle Valoff258392009-06-12 14:14:19 +0300755 wl_cmd->key_size = key->keylen;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300756
Kalle Valoff258392009-06-12 14:14:19 +0300757 wl_cmd->id = key->keyidx;
758 wl_cmd->ssid_profile = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300759
Kalle Valo80301cd2009-06-12 14:17:39 +0300760 wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300761
Kalle Valo80301cd2009-06-12 14:17:39 +0300762 ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
Kalle Valoff258392009-06-12 14:14:19 +0300763 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300764 wl1251_warning("could not set keys");
Kalle Valoc5483b72009-06-12 14:16:32 +0300765 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300766 }
767
Kalle Valoc5483b72009-06-12 14:16:32 +0300768out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300769 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300770
771out_unlock:
Kalle Valoff258392009-06-12 14:14:19 +0300772 mutex_unlock(&wl->mutex);
773
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300774out:
Kalle Valoff258392009-06-12 14:14:19 +0300775 kfree(wl_cmd);
776
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300777 return ret;
778}
779
Kalle Valo80301cd2009-06-12 14:17:39 +0300780static int wl1251_build_basic_rates(char *rates)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300781{
782 u8 index = 0;
783
784 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
785 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
786 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
787 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
788
789 return index;
790}
791
Kalle Valo80301cd2009-06-12 14:17:39 +0300792static int wl1251_build_extended_rates(char *rates)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300793{
794 u8 index = 0;
795
796 rates[index++] = IEEE80211_OFDM_RATE_6MB;
797 rates[index++] = IEEE80211_OFDM_RATE_9MB;
798 rates[index++] = IEEE80211_OFDM_RATE_12MB;
799 rates[index++] = IEEE80211_OFDM_RATE_18MB;
800 rates[index++] = IEEE80211_OFDM_RATE_24MB;
801 rates[index++] = IEEE80211_OFDM_RATE_36MB;
802 rates[index++] = IEEE80211_OFDM_RATE_48MB;
803 rates[index++] = IEEE80211_OFDM_RATE_54MB;
804
805 return index;
806}
807
808
Kalle Valo80301cd2009-06-12 14:17:39 +0300809static int wl1251_build_probe_req(struct wl1251 *wl, u8 *ssid, size_t ssid_len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300810{
811 struct wl12xx_probe_req_template template;
812 struct wl12xx_ie_rates *rates;
813 char *ptr;
814 u16 size;
815
816 ptr = (char *)&template;
817 size = sizeof(struct ieee80211_header);
818
819 memset(template.header.da, 0xff, ETH_ALEN);
820 memset(template.header.bssid, 0xff, ETH_ALEN);
821 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
822 template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
823
824 /* IEs */
825 /* SSID */
826 template.ssid.header.id = WLAN_EID_SSID;
827 template.ssid.header.len = ssid_len;
828 if (ssid_len && ssid)
829 memcpy(template.ssid.ssid, ssid, ssid_len);
830 size += sizeof(struct wl12xx_ie_header) + ssid_len;
831 ptr += size;
832
833 /* Basic Rates */
834 rates = (struct wl12xx_ie_rates *)ptr;
835 rates->header.id = WLAN_EID_SUPP_RATES;
Kalle Valo80301cd2009-06-12 14:17:39 +0300836 rates->header.len = wl1251_build_basic_rates(rates->rates);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300837 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
838 ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
839
840 /* Extended rates */
841 rates = (struct wl12xx_ie_rates *)ptr;
842 rates->header.id = WLAN_EID_EXT_SUPP_RATES;
Kalle Valo80301cd2009-06-12 14:17:39 +0300843 rates->header.len = wl1251_build_extended_rates(rates->rates);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300844 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
845
Kalle Valo80301cd2009-06-12 14:17:39 +0300846 wl1251_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300847
Kalle Valo80301cd2009-06-12 14:17:39 +0300848 return wl1251_cmd_template_set(wl, CMD_PROBE_REQ, &template,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300849 size);
850}
851
Kalle Valo80301cd2009-06-12 14:17:39 +0300852static int wl1251_hw_scan(struct wl1251 *wl, u8 *ssid, size_t len,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300853 u8 active_scan, u8 high_prio, u8 num_channels,
854 u8 probe_requests)
855{
Kalle Valo80301cd2009-06-12 14:17:39 +0300856 struct wl1251_cmd_trigger_scan_to *trigger = NULL;
Kalle Valoff258392009-06-12 14:14:19 +0300857 struct cmd_scan *params = NULL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300858 int i, ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300859 u16 scan_options = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300860
861 if (wl->scanning)
862 return -EINVAL;
863
864 params = kzalloc(sizeof(*params), GFP_KERNEL);
865 if (!params)
866 return -ENOMEM;
867
868 params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
869 params->params.rx_filter_options =
870 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
871
872 /* High priority scan */
873 if (!active_scan)
874 scan_options |= SCAN_PASSIVE;
875 if (high_prio)
876 scan_options |= SCAN_PRIORITY_HIGH;
877 params->params.scan_options = scan_options;
878
879 params->params.num_channels = num_channels;
880 params->params.num_probe_requests = probe_requests;
881 params->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
882 params->params.tid_trigger = 0;
883
884 for (i = 0; i < num_channels; i++) {
885 params->channels[i].min_duration = cpu_to_le32(30000);
886 params->channels[i].max_duration = cpu_to_le32(60000);
887 memset(&params->channels[i].bssid_lsb, 0xff, 4);
888 memset(&params->channels[i].bssid_msb, 0xff, 2);
889 params->channels[i].early_termination = 0;
890 params->channels[i].tx_power_att = 0;
891 params->channels[i].channel = i + 1;
892 memset(params->channels[i].pad, 0, 3);
893 }
894
895 for (i = num_channels; i < SCAN_MAX_NUM_OF_CHANNELS; i++)
896 memset(&params->channels[i], 0,
897 sizeof(struct basic_scan_channel_parameters));
898
899 if (len && ssid) {
900 params->params.ssid_len = len;
901 memcpy(params->params.ssid, ssid, len);
902 } else {
903 params->params.ssid_len = 0;
904 memset(params->params.ssid, 0, 32);
905 }
906
Kalle Valo80301cd2009-06-12 14:17:39 +0300907 ret = wl1251_build_probe_req(wl, ssid, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300908 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300909 wl1251_error("PROBE request template failed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300910 goto out;
911 }
912
Kalle Valoff258392009-06-12 14:14:19 +0300913 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
914 if (!trigger)
915 goto out;
916
917 trigger->timeout = 0;
918
Kalle Valo80301cd2009-06-12 14:17:39 +0300919 ret = wl1251_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
Kalle Valoff258392009-06-12 14:14:19 +0300920 sizeof(*trigger));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300921 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300922 wl1251_error("trigger scan to failed for hw scan");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300923 goto out;
924 }
925
Kalle Valo80301cd2009-06-12 14:17:39 +0300926 wl1251_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300927
928 wl->scanning = true;
929
Kalle Valo80301cd2009-06-12 14:17:39 +0300930 ret = wl1251_cmd_send(wl, CMD_SCAN, params, sizeof(*params));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300931 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300932 wl1251_error("SCAN failed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300933
Kalle Valo80301cd2009-06-12 14:17:39 +0300934 wl1251_spi_mem_read(wl, wl->cmd_box_addr, params, sizeof(*params));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300935
Kalle Valoff258392009-06-12 14:14:19 +0300936 if (params->header.status != CMD_STATUS_SUCCESS) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300937 wl1251_error("TEST command answer error: %d",
Kalle Valoff258392009-06-12 14:14:19 +0300938 params->header.status);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300939 wl->scanning = false;
940 ret = -EIO;
941 goto out;
942 }
943
944out:
945 kfree(params);
946 return ret;
947
948}
949
Kalle Valo80301cd2009-06-12 14:17:39 +0300950static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300951 struct cfg80211_scan_request *req)
952{
Kalle Valo80301cd2009-06-12 14:17:39 +0300953 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300954 int ret;
955 u8 *ssid = NULL;
956 size_t ssid_len = 0;
957
Kalle Valo80301cd2009-06-12 14:17:39 +0300958 wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300959
960 if (req->n_ssids) {
961 ssid = req->ssids[0].ssid;
962 ssid_len = req->ssids[0].ssid_len;
963 }
964
965 mutex_lock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +0300966
Kalle Valo80301cd2009-06-12 14:17:39 +0300967 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300968 if (ret < 0)
969 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300970
Kalle Valo80301cd2009-06-12 14:17:39 +0300971 ret = wl1251_hw_scan(hw->priv, ssid, ssid_len, 1, 0, 13, 3);
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300972
Kalle Valo80301cd2009-06-12 14:17:39 +0300973 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300974
975out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300976 mutex_unlock(&wl->mutex);
977
978 return ret;
979}
980
Kalle Valo80301cd2009-06-12 14:17:39 +0300981static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300982{
Kalle Valo80301cd2009-06-12 14:17:39 +0300983 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300984 int ret;
985
Kalle Valocee4fd22009-06-12 14:16:20 +0300986 mutex_lock(&wl->mutex);
987
Kalle Valo80301cd2009-06-12 14:17:39 +0300988 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300989 if (ret < 0)
990 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300991
Kalle Valo80301cd2009-06-12 14:17:39 +0300992 ret = wl1251_acx_rts_threshold(wl, (u16) value);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300993 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300994 wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300995
Kalle Valo80301cd2009-06-12 14:17:39 +0300996 wl1251_ps_elp_sleep(wl);
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300997
Kalle Valoc5483b72009-06-12 14:16:32 +0300998out:
Kalle Valocee4fd22009-06-12 14:16:20 +0300999 mutex_unlock(&wl->mutex);
1000
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001001 return ret;
1002}
1003
Kalle Valo80301cd2009-06-12 14:17:39 +03001004static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001005 struct ieee80211_vif *vif,
1006 struct ieee80211_bss_conf *bss_conf,
1007 u32 changed)
1008{
Kalle Valo80301cd2009-06-12 14:17:39 +03001009 enum wl1251_cmd_ps_mode mode;
1010 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001011 struct sk_buff *beacon;
1012 int ret;
1013
Kalle Valo80301cd2009-06-12 14:17:39 +03001014 wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001015
1016 mutex_lock(&wl->mutex);
1017
Kalle Valo80301cd2009-06-12 14:17:39 +03001018 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001019 if (ret < 0)
1020 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +03001021
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001022 if (changed & BSS_CHANGED_ASSOC) {
1023 if (bss_conf->assoc) {
1024 wl->aid = bss_conf->aid;
1025
Kalle Valo80301cd2009-06-12 14:17:39 +03001026 ret = wl1251_build_ps_poll(wl, wl->aid);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001027 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001028 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001029
Kalle Valo80301cd2009-06-12 14:17:39 +03001030 ret = wl1251_acx_aid(wl, wl->aid);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001031 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001032 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001033
1034 /* If we want to go in PSM but we're not there yet */
1035 if (wl->psm_requested && !wl->psm) {
1036 mode = STATION_POWER_SAVE_MODE;
Kalle Valo80301cd2009-06-12 14:17:39 +03001037 ret = wl1251_ps_set_mode(wl, mode);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001038 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001039 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001040 }
1041 }
1042 }
1043 if (changed & BSS_CHANGED_ERP_SLOT) {
1044 if (bss_conf->use_short_slot)
Kalle Valo80301cd2009-06-12 14:17:39 +03001045 ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001046 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001047 ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001048 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001049 wl1251_warning("Set slot time failed %d", ret);
Kalle Valoc5483b72009-06-12 14:16:32 +03001050 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001051 }
1052 }
1053
1054 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1055 if (bss_conf->use_short_preamble)
Kalle Valo80301cd2009-06-12 14:17:39 +03001056 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001057 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001058 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001059 }
1060
1061 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1062 if (bss_conf->use_cts_prot)
Kalle Valo80301cd2009-06-12 14:17:39 +03001063 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001064 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001065 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001066 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001067 wl1251_warning("Set ctsprotect failed %d", ret);
1068 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001069 }
1070 }
1071
1072 if (changed & BSS_CHANGED_BSSID) {
1073 memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
1074
Kalle Valo80301cd2009-06-12 14:17:39 +03001075 ret = wl1251_build_null_data(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001076 if (ret < 0)
1077 goto out;
1078
1079 if (wl->bss_type != BSS_TYPE_IBSS) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001080 ret = wl1251_cmd_join(wl, wl->bss_type, 5, 100, 1);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001081 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +03001082 goto out_sleep;
1083 wl1251_warning("Set ctsprotect failed %d", ret);
1084 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001085 }
1086 }
1087
1088 if (changed & BSS_CHANGED_BEACON) {
1089 beacon = ieee80211_beacon_get(hw, vif);
Kalle Valo80301cd2009-06-12 14:17:39 +03001090 ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001091 beacon->len);
1092
1093 if (ret < 0) {
1094 dev_kfree_skb(beacon);
1095 goto out;
1096 }
1097
Kalle Valo80301cd2009-06-12 14:17:39 +03001098 ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001099 beacon->len);
1100
1101 dev_kfree_skb(beacon);
1102
1103 if (ret < 0)
1104 goto out;
1105
Juuso Oikarinen77cc9e42009-06-12 14:16:52 +03001106 ret = wl->chip.op_cmd_join(wl, wl->bss_type, 1, 100, 0);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001107
1108 if (ret < 0)
1109 goto out;
1110 }
1111
Kalle Valoc5483b72009-06-12 14:16:32 +03001112out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +03001113 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001114
1115out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001116 mutex_unlock(&wl->mutex);
1117}
1118
1119
1120/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001121static struct ieee80211_rate wl1251_rates[] = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001122 { .bitrate = 10,
1123 .hw_value = 0x1,
1124 .hw_value_short = 0x1, },
1125 { .bitrate = 20,
1126 .hw_value = 0x2,
1127 .hw_value_short = 0x2,
1128 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1129 { .bitrate = 55,
1130 .hw_value = 0x4,
1131 .hw_value_short = 0x4,
1132 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1133 { .bitrate = 110,
1134 .hw_value = 0x20,
1135 .hw_value_short = 0x20,
1136 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1137 { .bitrate = 60,
1138 .hw_value = 0x8,
1139 .hw_value_short = 0x8, },
1140 { .bitrate = 90,
1141 .hw_value = 0x10,
1142 .hw_value_short = 0x10, },
1143 { .bitrate = 120,
1144 .hw_value = 0x40,
1145 .hw_value_short = 0x40, },
1146 { .bitrate = 180,
1147 .hw_value = 0x80,
1148 .hw_value_short = 0x80, },
1149 { .bitrate = 240,
1150 .hw_value = 0x200,
1151 .hw_value_short = 0x200, },
1152 { .bitrate = 360,
1153 .hw_value = 0x400,
1154 .hw_value_short = 0x400, },
1155 { .bitrate = 480,
1156 .hw_value = 0x800,
1157 .hw_value_short = 0x800, },
1158 { .bitrate = 540,
1159 .hw_value = 0x1000,
1160 .hw_value_short = 0x1000, },
1161};
1162
1163/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001164static struct ieee80211_channel wl1251_channels[] = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001165 { .hw_value = 1, .center_freq = 2412},
1166 { .hw_value = 2, .center_freq = 2417},
1167 { .hw_value = 3, .center_freq = 2422},
1168 { .hw_value = 4, .center_freq = 2427},
1169 { .hw_value = 5, .center_freq = 2432},
1170 { .hw_value = 6, .center_freq = 2437},
1171 { .hw_value = 7, .center_freq = 2442},
1172 { .hw_value = 8, .center_freq = 2447},
1173 { .hw_value = 9, .center_freq = 2452},
1174 { .hw_value = 10, .center_freq = 2457},
1175 { .hw_value = 11, .center_freq = 2462},
1176 { .hw_value = 12, .center_freq = 2467},
1177 { .hw_value = 13, .center_freq = 2472},
1178};
1179
1180/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001181static struct ieee80211_supported_band wl1251_band_2ghz = {
1182 .channels = wl1251_channels,
1183 .n_channels = ARRAY_SIZE(wl1251_channels),
1184 .bitrates = wl1251_rates,
1185 .n_bitrates = ARRAY_SIZE(wl1251_rates),
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001186};
1187
Kalle Valo80301cd2009-06-12 14:17:39 +03001188static const struct ieee80211_ops wl1251_ops = {
1189 .start = wl1251_op_start,
1190 .stop = wl1251_op_stop,
1191 .add_interface = wl1251_op_add_interface,
1192 .remove_interface = wl1251_op_remove_interface,
1193 .config = wl1251_op_config,
1194 .configure_filter = wl1251_op_configure_filter,
1195 .tx = wl1251_op_tx,
1196 .set_key = wl1251_op_set_key,
1197 .hw_scan = wl1251_op_hw_scan,
1198 .bss_info_changed = wl1251_op_bss_info_changed,
1199 .set_rts_threshold = wl1251_op_set_rts_threshold,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001200};
1201
Kalle Valo80301cd2009-06-12 14:17:39 +03001202static int wl1251_register_hw(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001203{
1204 int ret;
1205
1206 if (wl->mac80211_registered)
1207 return 0;
1208
1209 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1210
1211 ret = ieee80211_register_hw(wl->hw);
1212 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001213 wl1251_error("unable to register mac80211 hw: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001214 return ret;
1215 }
1216
1217 wl->mac80211_registered = true;
1218
Kalle Valo80301cd2009-06-12 14:17:39 +03001219 wl1251_notice("loaded");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001220
1221 return 0;
1222}
1223
Kalle Valo80301cd2009-06-12 14:17:39 +03001224static int wl1251_init_ieee80211(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001225{
1226 /* The tx descriptor buffer and the TKIP space */
1227 wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +03001228 + WL1251_TKIP_IV_SPACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001229
1230 /* unit us */
1231 /* FIXME: find a proper value */
1232 wl->hw->channel_change_time = 10000;
1233
1234 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
1235 IEEE80211_HW_NOISE_DBM;
1236
1237 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1238 wl->hw->wiphy->max_scan_ssids = 1;
Kalle Valo80301cd2009-06-12 14:17:39 +03001239 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1251_band_2ghz;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001240
1241 SET_IEEE80211_DEV(wl->hw, &wl->spi->dev);
1242
1243 return 0;
1244}
1245
Kalle Valo80301cd2009-06-12 14:17:39 +03001246#define WL1251_DEFAULT_CHANNEL 1
1247static int __devinit wl1251_probe(struct spi_device *spi)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001248{
1249 struct wl12xx_platform_data *pdata;
1250 struct ieee80211_hw *hw;
Kalle Valo80301cd2009-06-12 14:17:39 +03001251 struct wl1251 *wl;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001252 int ret, i;
1253 static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1254
1255 pdata = spi->dev.platform_data;
1256 if (!pdata) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001257 wl1251_error("no platform data");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001258 return -ENODEV;
1259 }
1260
Kalle Valo80301cd2009-06-12 14:17:39 +03001261 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001262 if (!hw) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001263 wl1251_error("could not alloc ieee80211_hw");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001264 return -ENOMEM;
1265 }
1266
1267 wl = hw->priv;
1268 memset(wl, 0, sizeof(*wl));
1269
1270 wl->hw = hw;
1271 dev_set_drvdata(&spi->dev, wl);
1272 wl->spi = spi;
1273
1274 wl->data_in_count = 0;
1275
1276 skb_queue_head_init(&wl->tx_queue);
1277
Kalle Valo80301cd2009-06-12 14:17:39 +03001278 INIT_WORK(&wl->filter_work, wl1251_filter_work);
1279 wl->channel = WL1251_DEFAULT_CHANNEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001280 wl->scanning = false;
1281 wl->default_key = 0;
1282 wl->listen_int = 1;
1283 wl->rx_counter = 0;
1284 wl->rx_handled = 0;
1285 wl->rx_current_buffer = 0;
1286 wl->rx_last_id = 0;
Kalle Valo80301cd2009-06-12 14:17:39 +03001287 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
1288 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001289 wl->elp = false;
1290 wl->psm = 0;
1291 wl->psm_requested = false;
1292 wl->tx_queue_stopped = false;
Kalle Valo80301cd2009-06-12 14:17:39 +03001293 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001294
1295 /* We use the default power on sleep time until we know which chip
1296 * we're using */
Kalle Valo80301cd2009-06-12 14:17:39 +03001297 wl->chip.power_on_sleep = WL1251_DEFAULT_POWER_ON_SLEEP;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001298
1299 for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
1300 wl->tx_frames[i] = NULL;
1301
1302 wl->next_tx_complete = 0;
1303
1304 /*
1305 * In case our MAC address is not correctly set,
1306 * we use a random but Nokia MAC.
1307 */
1308 memcpy(wl->mac_addr, nokia_oui, 3);
1309 get_random_bytes(wl->mac_addr + 3, 3);
1310
Kalle Valo80301cd2009-06-12 14:17:39 +03001311 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001312 mutex_init(&wl->mutex);
1313
1314 wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
1315 wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
1316
Kalle Valo47212132009-06-12 14:15:08 +03001317 wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1318 if (!wl->rx_descriptor) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001319 wl1251_error("could not allocate memory for rx descriptor");
Kalle Valo47212132009-06-12 14:15:08 +03001320 ret = -ENOMEM;
1321 goto out_free;
1322 }
1323
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001324 /* This is the only SPI value that we need to set here, the rest
1325 * comes from the board-peripherals file */
1326 spi->bits_per_word = 32;
1327
1328 ret = spi_setup(spi);
1329 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001330 wl1251_error("spi_setup failed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001331 goto out_free;
1332 }
1333
1334 wl->set_power = pdata->set_power;
1335 if (!wl->set_power) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001336 wl1251_error("set power function missing in platform data");
Kalle Valoc4f5c852009-06-12 14:14:34 +03001337 ret = -ENODEV;
1338 goto out_free;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001339 }
1340
1341 wl->irq = spi->irq;
1342 if (wl->irq < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001343 wl1251_error("irq missing in platform data");
Kalle Valoc4f5c852009-06-12 14:14:34 +03001344 ret = -ENODEV;
1345 goto out_free;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001346 }
1347
Kalle Valo80301cd2009-06-12 14:17:39 +03001348 ret = request_irq(wl->irq, wl1251_irq, 0, DRIVER_NAME, wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001349 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001350 wl1251_error("request_irq() failed: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001351 goto out_free;
1352 }
1353
1354 set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
1355
1356 disable_irq(wl->irq);
1357
Kalle Valo80301cd2009-06-12 14:17:39 +03001358 ret = wl1251_init_ieee80211(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001359 if (ret)
1360 goto out_irq;
1361
Kalle Valo80301cd2009-06-12 14:17:39 +03001362 ret = wl1251_register_hw(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001363 if (ret)
1364 goto out_irq;
1365
Kalle Valo80301cd2009-06-12 14:17:39 +03001366 wl1251_debugfs_init(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001367
Kalle Valo80301cd2009-06-12 14:17:39 +03001368 wl1251_notice("initialized");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001369
1370 return 0;
1371
1372 out_irq:
1373 free_irq(wl->irq, wl);
1374
1375 out_free:
Kalle Valo47212132009-06-12 14:15:08 +03001376 kfree(wl->rx_descriptor);
1377 wl->rx_descriptor = NULL;
1378
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001379 ieee80211_free_hw(hw);
1380
1381 return ret;
1382}
1383
Kalle Valo80301cd2009-06-12 14:17:39 +03001384static int __devexit wl1251_remove(struct spi_device *spi)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001385{
Kalle Valo80301cd2009-06-12 14:17:39 +03001386 struct wl1251 *wl = dev_get_drvdata(&spi->dev);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001387
1388 ieee80211_unregister_hw(wl->hw);
1389
Kalle Valo80301cd2009-06-12 14:17:39 +03001390 wl1251_debugfs_exit(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001391
1392 free_irq(wl->irq, wl);
1393 kfree(wl->target_mem_map);
1394 kfree(wl->data_path);
1395 kfree(wl->fw);
1396 wl->fw = NULL;
1397 kfree(wl->nvs);
1398 wl->nvs = NULL;
Kalle Valo47212132009-06-12 14:15:08 +03001399
1400 kfree(wl->rx_descriptor);
1401 wl->rx_descriptor = NULL;
1402
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001403 ieee80211_free_hw(wl->hw);
1404
1405 return 0;
1406}
1407
1408
Kalle Valo80301cd2009-06-12 14:17:39 +03001409static struct spi_driver wl1251_spi_driver = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001410 .driver = {
Kalle Valo80301cd2009-06-12 14:17:39 +03001411 /* FIXME: use wl12xx name to not break the user space */
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001412 .name = "wl12xx",
1413 .bus = &spi_bus_type,
1414 .owner = THIS_MODULE,
1415 },
1416
Kalle Valo80301cd2009-06-12 14:17:39 +03001417 .probe = wl1251_probe,
1418 .remove = __devexit_p(wl1251_remove),
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001419};
1420
Kalle Valo80301cd2009-06-12 14:17:39 +03001421static int __init wl1251_init(void)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001422{
1423 int ret;
1424
Kalle Valo80301cd2009-06-12 14:17:39 +03001425 ret = spi_register_driver(&wl1251_spi_driver);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001426 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001427 wl1251_error("failed to register spi driver: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001428 goto out;
1429 }
1430
1431out:
1432 return ret;
1433}
1434
Kalle Valo80301cd2009-06-12 14:17:39 +03001435static void __exit wl1251_exit(void)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001436{
Kalle Valo80301cd2009-06-12 14:17:39 +03001437 spi_unregister_driver(&wl1251_spi_driver);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001438
Kalle Valo80301cd2009-06-12 14:17:39 +03001439 wl1251_notice("unloaded");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001440}
1441
Kalle Valo80301cd2009-06-12 14:17:39 +03001442module_init(wl1251_init);
1443module_exit(wl1251_exit);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001444
1445MODULE_LICENSE("GPL");
1446MODULE_AUTHOR("Kalle Valo <Kalle.Valo@nokia.com>, "
1447 "Luciano Coelho <luciano.coelho@nokia.com>");