blob: 106b0f265192e0df8310793fa7e3f212a0468ac9 [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 DECLARE_MAC_BUF(mac);
439 int ret = 0;
440
Kalle Valo80301cd2009-06-12 14:17:39 +0300441 wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %s",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300442 conf->type, print_mac(mac, conf->mac_addr));
443
444 mutex_lock(&wl->mutex);
445
446 switch (conf->type) {
447 case NL80211_IFTYPE_STATION:
448 wl->bss_type = BSS_TYPE_STA_BSS;
449 break;
450 case NL80211_IFTYPE_ADHOC:
451 wl->bss_type = BSS_TYPE_IBSS;
452 break;
453 default:
454 ret = -EOPNOTSUPP;
455 goto out;
456 }
457
458 if (memcmp(wl->mac_addr, conf->mac_addr, ETH_ALEN)) {
459 memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
460 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
Kalle Valo80301cd2009-06-12 14:17:39 +0300461 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300462 if (ret < 0)
463 goto out;
464 }
465
466out:
467 mutex_unlock(&wl->mutex);
468 return ret;
469}
470
Kalle Valo80301cd2009-06-12 14:17:39 +0300471static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300472 struct ieee80211_if_init_conf *conf)
473{
Kalle Valo80301cd2009-06-12 14:17:39 +0300474 wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300475}
476
Kalle Valo80301cd2009-06-12 14:17:39 +0300477static int wl1251_build_null_data(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300478{
479 struct wl12xx_null_data_template template;
480
481 if (!is_zero_ether_addr(wl->bssid)) {
482 memcpy(template.header.da, wl->bssid, ETH_ALEN);
483 memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
484 } else {
485 memset(template.header.da, 0xff, ETH_ALEN);
486 memset(template.header.bssid, 0xff, ETH_ALEN);
487 }
488
489 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
490 template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
491 IEEE80211_STYPE_NULLFUNC);
492
Kalle Valo80301cd2009-06-12 14:17:39 +0300493 return wl1251_cmd_template_set(wl, CMD_NULL_DATA, &template,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300494 sizeof(template));
495
496}
497
Kalle Valo80301cd2009-06-12 14:17:39 +0300498static int wl1251_build_ps_poll(struct wl1251 *wl, u16 aid)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300499{
500 struct wl12xx_ps_poll_template template;
501
502 memcpy(template.bssid, wl->bssid, ETH_ALEN);
503 memcpy(template.ta, wl->mac_addr, ETH_ALEN);
504 template.aid = aid;
505 template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
506
Kalle Valo80301cd2009-06-12 14:17:39 +0300507 return wl1251_cmd_template_set(wl, CMD_PS_POLL, &template,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300508 sizeof(template));
509
510}
511
Kalle Valo80301cd2009-06-12 14:17:39 +0300512static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300513{
Kalle Valo80301cd2009-06-12 14:17:39 +0300514 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300515 struct ieee80211_conf *conf = &hw->conf;
516 int channel, ret = 0;
517
518 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
519
Kalle Valo80301cd2009-06-12 14:17:39 +0300520 wl1251_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300521 channel,
522 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
523 conf->power_level);
524
525 mutex_lock(&wl->mutex);
526
Kalle Valo80301cd2009-06-12 14:17:39 +0300527 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300528 if (ret < 0)
529 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300530
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300531 if (channel != wl->channel) {
532 /* FIXME: use beacon interval provided by mac80211 */
Juuso Oikarinen77cc9e42009-06-12 14:16:52 +0300533 ret = wl->chip.op_cmd_join(wl, wl->bss_type, 1, 100, 0);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300534 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300535 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300536
537 wl->channel = channel;
538 }
539
Kalle Valo80301cd2009-06-12 14:17:39 +0300540 ret = wl1251_build_null_data(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300541 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300542 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300543
544 if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
Luciano Coelho47af3fe2009-06-12 14:17:53 +0300545 wl1251_debug(DEBUG_PSM, "psm enabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300546
547 wl->psm_requested = true;
548
549 /*
550 * We enter PSM only if we're already associated.
551 * If we're not, we'll enter it when joining an SSID,
552 * through the bss_info_changed() hook.
553 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300554 ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300555 } else if (!(conf->flags & IEEE80211_CONF_PS) &&
556 wl->psm_requested) {
Luciano Coelho47af3fe2009-06-12 14:17:53 +0300557 wl1251_debug(DEBUG_PSM, "psm disabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300558
559 wl->psm_requested = false;
560
561 if (wl->psm)
Kalle Valo80301cd2009-06-12 14:17:39 +0300562 ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300563 }
564
565 if (conf->power_level != wl->power_level) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300566 ret = wl1251_acx_tx_power(wl, conf->power_level);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300567 if (ret < 0)
568 goto out;
569
570 wl->power_level = conf->power_level;
571 }
572
Kalle Valoc5483b72009-06-12 14:16:32 +0300573out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300574 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300575
576out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300577 mutex_unlock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +0300578
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300579 return ret;
580}
581
Kalle Valo80301cd2009-06-12 14:17:39 +0300582#define WL1251_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300583 FIF_ALLMULTI | \
584 FIF_FCSFAIL | \
585 FIF_BCN_PRBRESP_PROMISC | \
586 FIF_CONTROL | \
587 FIF_OTHER_BSS)
588
Kalle Valo80301cd2009-06-12 14:17:39 +0300589static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300590 unsigned int changed,
591 unsigned int *total,
592 int mc_count,
593 struct dev_addr_list *mc_list)
594{
Kalle Valo80301cd2009-06-12 14:17:39 +0300595 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300596
Kalle Valo80301cd2009-06-12 14:17:39 +0300597 wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300598
Kalle Valo80301cd2009-06-12 14:17:39 +0300599 *total &= WL1251_SUPPORTED_FILTERS;
600 changed &= WL1251_SUPPORTED_FILTERS;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300601
602 if (changed == 0)
603 /* no filters which we support changed */
604 return;
605
606 /* FIXME: wl->rx_config and wl->rx_filter are not protected */
607
Kalle Valo80301cd2009-06-12 14:17:39 +0300608 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
609 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300610
611 if (*total & FIF_PROMISC_IN_BSS) {
612 wl->rx_config |= CFG_BSSID_FILTER_EN;
613 wl->rx_config |= CFG_RX_ALL_GOOD;
614 }
615 if (*total & FIF_ALLMULTI)
616 /*
617 * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
618 * all multicast frames
619 */
620 wl->rx_config &= ~CFG_MC_FILTER_EN;
621 if (*total & FIF_FCSFAIL)
622 wl->rx_filter |= CFG_RX_FCS_ERROR;
623 if (*total & FIF_BCN_PRBRESP_PROMISC) {
624 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
625 wl->rx_config &= ~CFG_SSID_FILTER_EN;
626 }
627 if (*total & FIF_CONTROL)
628 wl->rx_filter |= CFG_RX_CTL_EN;
629 if (*total & FIF_OTHER_BSS)
630 wl->rx_filter &= ~CFG_BSSID_FILTER_EN;
631
632 /*
633 * FIXME: workqueues need to be properly cancelled on stop(), for
634 * now let's just disable changing the filter settings. They will
635 * be updated any on config().
636 */
637 /* schedule_work(&wl->filter_work); */
638}
639
640/* HW encryption */
Kalle Valo80301cd2009-06-12 14:17:39 +0300641static int wl1251_set_key_type(struct wl1251 *wl,
642 struct wl1251_cmd_set_keys *key,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300643 enum set_key_cmd cmd,
644 struct ieee80211_key_conf *mac80211_key,
645 const u8 *addr)
646{
647 switch (mac80211_key->alg) {
648 case ALG_WEP:
649 if (is_broadcast_ether_addr(addr))
650 key->key_type = KEY_WEP_DEFAULT;
651 else
652 key->key_type = KEY_WEP_ADDR;
653
654 mac80211_key->hw_key_idx = mac80211_key->keyidx;
655 break;
656 case ALG_TKIP:
657 if (is_broadcast_ether_addr(addr))
658 key->key_type = KEY_TKIP_MIC_GROUP;
659 else
660 key->key_type = KEY_TKIP_MIC_PAIRWISE;
661
662 mac80211_key->hw_key_idx = mac80211_key->keyidx;
663 break;
664 case ALG_CCMP:
665 if (is_broadcast_ether_addr(addr))
666 key->key_type = KEY_AES_GROUP;
667 else
668 key->key_type = KEY_AES_PAIRWISE;
669 mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
670 break;
671 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300672 wl1251_error("Unknown key algo 0x%x", mac80211_key->alg);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300673 return -EOPNOTSUPP;
674 }
675
676 return 0;
677}
678
Kalle Valo80301cd2009-06-12 14:17:39 +0300679static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300680 struct ieee80211_vif *vif,
681 struct ieee80211_sta *sta,
682 struct ieee80211_key_conf *key)
683{
Kalle Valo80301cd2009-06-12 14:17:39 +0300684 struct wl1251 *wl = hw->priv;
685 struct wl1251_cmd_set_keys *wl_cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300686 const u8 *addr;
687 int ret;
688
689 static const u8 bcast_addr[ETH_ALEN] =
690 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
691
Kalle Valo80301cd2009-06-12 14:17:39 +0300692 wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300693
Kalle Valoff258392009-06-12 14:14:19 +0300694 wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
695 if (!wl_cmd) {
696 ret = -ENOMEM;
697 goto out;
698 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300699
700 addr = sta ? sta->addr : bcast_addr;
701
Kalle Valo80301cd2009-06-12 14:17:39 +0300702 wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
703 wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
704 wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300705 key->alg, key->keyidx, key->keylen, key->flags);
Kalle Valo80301cd2009-06-12 14:17:39 +0300706 wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300707
Kalle Valoff258392009-06-12 14:14:19 +0300708 if (is_zero_ether_addr(addr)) {
709 /* We dont support TX only encryption */
710 ret = -EOPNOTSUPP;
711 goto out;
712 }
713
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300714 mutex_lock(&wl->mutex);
715
Kalle Valo80301cd2009-06-12 14:17:39 +0300716 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300717 if (ret < 0)
718 goto out_unlock;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300719
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300720 switch (cmd) {
721 case SET_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300722 wl_cmd->key_action = KEY_ADD_OR_REPLACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300723 break;
724 case DISABLE_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300725 wl_cmd->key_action = KEY_REMOVE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300726 break;
727 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300728 wl1251_error("Unsupported key cmd 0x%x", cmd);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300729 break;
730 }
731
Kalle Valo80301cd2009-06-12 14:17:39 +0300732 ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300733 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300734 wl1251_error("Set KEY type failed");
Kalle Valoc5483b72009-06-12 14:16:32 +0300735 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300736 }
737
Kalle Valoff258392009-06-12 14:14:19 +0300738 if (wl_cmd->key_type != KEY_WEP_DEFAULT)
739 memcpy(wl_cmd->addr, addr, ETH_ALEN);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300740
Kalle Valoff258392009-06-12 14:14:19 +0300741 if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
742 (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300743 /*
744 * We get the key in the following form:
745 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
746 * but the target is expecting:
747 * TKIP - RX MIC - TX MIC
748 */
Kalle Valoff258392009-06-12 14:14:19 +0300749 memcpy(wl_cmd->key, key->key, 16);
750 memcpy(wl_cmd->key + 16, key->key + 24, 8);
751 memcpy(wl_cmd->key + 24, key->key + 16, 8);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300752
753 } else {
Kalle Valoff258392009-06-12 14:14:19 +0300754 memcpy(wl_cmd->key, key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300755 }
Kalle Valoff258392009-06-12 14:14:19 +0300756 wl_cmd->key_size = key->keylen;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300757
Kalle Valoff258392009-06-12 14:14:19 +0300758 wl_cmd->id = key->keyidx;
759 wl_cmd->ssid_profile = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300760
Kalle Valo80301cd2009-06-12 14:17:39 +0300761 wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300762
Kalle Valo80301cd2009-06-12 14:17:39 +0300763 ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
Kalle Valoff258392009-06-12 14:14:19 +0300764 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300765 wl1251_warning("could not set keys");
Kalle Valoc5483b72009-06-12 14:16:32 +0300766 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300767 }
768
Kalle Valoc5483b72009-06-12 14:16:32 +0300769out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300770 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300771
772out_unlock:
Kalle Valoff258392009-06-12 14:14:19 +0300773 mutex_unlock(&wl->mutex);
774
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300775out:
Kalle Valoff258392009-06-12 14:14:19 +0300776 kfree(wl_cmd);
777
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300778 return ret;
779}
780
Kalle Valo80301cd2009-06-12 14:17:39 +0300781static int wl1251_build_basic_rates(char *rates)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300782{
783 u8 index = 0;
784
785 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
786 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
787 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
788 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
789
790 return index;
791}
792
Kalle Valo80301cd2009-06-12 14:17:39 +0300793static int wl1251_build_extended_rates(char *rates)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300794{
795 u8 index = 0;
796
797 rates[index++] = IEEE80211_OFDM_RATE_6MB;
798 rates[index++] = IEEE80211_OFDM_RATE_9MB;
799 rates[index++] = IEEE80211_OFDM_RATE_12MB;
800 rates[index++] = IEEE80211_OFDM_RATE_18MB;
801 rates[index++] = IEEE80211_OFDM_RATE_24MB;
802 rates[index++] = IEEE80211_OFDM_RATE_36MB;
803 rates[index++] = IEEE80211_OFDM_RATE_48MB;
804 rates[index++] = IEEE80211_OFDM_RATE_54MB;
805
806 return index;
807}
808
809
Kalle Valo80301cd2009-06-12 14:17:39 +0300810static int wl1251_build_probe_req(struct wl1251 *wl, u8 *ssid, size_t ssid_len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300811{
812 struct wl12xx_probe_req_template template;
813 struct wl12xx_ie_rates *rates;
814 char *ptr;
815 u16 size;
816
817 ptr = (char *)&template;
818 size = sizeof(struct ieee80211_header);
819
820 memset(template.header.da, 0xff, ETH_ALEN);
821 memset(template.header.bssid, 0xff, ETH_ALEN);
822 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
823 template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
824
825 /* IEs */
826 /* SSID */
827 template.ssid.header.id = WLAN_EID_SSID;
828 template.ssid.header.len = ssid_len;
829 if (ssid_len && ssid)
830 memcpy(template.ssid.ssid, ssid, ssid_len);
831 size += sizeof(struct wl12xx_ie_header) + ssid_len;
832 ptr += size;
833
834 /* Basic Rates */
835 rates = (struct wl12xx_ie_rates *)ptr;
836 rates->header.id = WLAN_EID_SUPP_RATES;
Kalle Valo80301cd2009-06-12 14:17:39 +0300837 rates->header.len = wl1251_build_basic_rates(rates->rates);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300838 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
839 ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
840
841 /* Extended rates */
842 rates = (struct wl12xx_ie_rates *)ptr;
843 rates->header.id = WLAN_EID_EXT_SUPP_RATES;
Kalle Valo80301cd2009-06-12 14:17:39 +0300844 rates->header.len = wl1251_build_extended_rates(rates->rates);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300845 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
846
Kalle Valo80301cd2009-06-12 14:17:39 +0300847 wl1251_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300848
Kalle Valo80301cd2009-06-12 14:17:39 +0300849 return wl1251_cmd_template_set(wl, CMD_PROBE_REQ, &template,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300850 size);
851}
852
Kalle Valo80301cd2009-06-12 14:17:39 +0300853static int wl1251_hw_scan(struct wl1251 *wl, u8 *ssid, size_t len,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300854 u8 active_scan, u8 high_prio, u8 num_channels,
855 u8 probe_requests)
856{
Kalle Valo80301cd2009-06-12 14:17:39 +0300857 struct wl1251_cmd_trigger_scan_to *trigger = NULL;
Kalle Valoff258392009-06-12 14:14:19 +0300858 struct cmd_scan *params = NULL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300859 int i, ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300860 u16 scan_options = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300861
862 if (wl->scanning)
863 return -EINVAL;
864
865 params = kzalloc(sizeof(*params), GFP_KERNEL);
866 if (!params)
867 return -ENOMEM;
868
869 params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
870 params->params.rx_filter_options =
871 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
872
873 /* High priority scan */
874 if (!active_scan)
875 scan_options |= SCAN_PASSIVE;
876 if (high_prio)
877 scan_options |= SCAN_PRIORITY_HIGH;
878 params->params.scan_options = scan_options;
879
880 params->params.num_channels = num_channels;
881 params->params.num_probe_requests = probe_requests;
882 params->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
883 params->params.tid_trigger = 0;
884
885 for (i = 0; i < num_channels; i++) {
886 params->channels[i].min_duration = cpu_to_le32(30000);
887 params->channels[i].max_duration = cpu_to_le32(60000);
888 memset(&params->channels[i].bssid_lsb, 0xff, 4);
889 memset(&params->channels[i].bssid_msb, 0xff, 2);
890 params->channels[i].early_termination = 0;
891 params->channels[i].tx_power_att = 0;
892 params->channels[i].channel = i + 1;
893 memset(params->channels[i].pad, 0, 3);
894 }
895
896 for (i = num_channels; i < SCAN_MAX_NUM_OF_CHANNELS; i++)
897 memset(&params->channels[i], 0,
898 sizeof(struct basic_scan_channel_parameters));
899
900 if (len && ssid) {
901 params->params.ssid_len = len;
902 memcpy(params->params.ssid, ssid, len);
903 } else {
904 params->params.ssid_len = 0;
905 memset(params->params.ssid, 0, 32);
906 }
907
Kalle Valo80301cd2009-06-12 14:17:39 +0300908 ret = wl1251_build_probe_req(wl, ssid, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300909 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300910 wl1251_error("PROBE request template failed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300911 goto out;
912 }
913
Kalle Valoff258392009-06-12 14:14:19 +0300914 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
915 if (!trigger)
916 goto out;
917
918 trigger->timeout = 0;
919
Kalle Valo80301cd2009-06-12 14:17:39 +0300920 ret = wl1251_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
Kalle Valoff258392009-06-12 14:14:19 +0300921 sizeof(*trigger));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300922 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300923 wl1251_error("trigger scan to failed for hw scan");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300924 goto out;
925 }
926
Kalle Valo80301cd2009-06-12 14:17:39 +0300927 wl1251_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300928
929 wl->scanning = true;
930
Kalle Valo80301cd2009-06-12 14:17:39 +0300931 ret = wl1251_cmd_send(wl, CMD_SCAN, params, sizeof(*params));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300932 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300933 wl1251_error("SCAN failed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300934
Kalle Valo80301cd2009-06-12 14:17:39 +0300935 wl1251_spi_mem_read(wl, wl->cmd_box_addr, params, sizeof(*params));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300936
Kalle Valoff258392009-06-12 14:14:19 +0300937 if (params->header.status != CMD_STATUS_SUCCESS) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300938 wl1251_error("TEST command answer error: %d",
Kalle Valoff258392009-06-12 14:14:19 +0300939 params->header.status);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300940 wl->scanning = false;
941 ret = -EIO;
942 goto out;
943 }
944
945out:
946 kfree(params);
947 return ret;
948
949}
950
Kalle Valo80301cd2009-06-12 14:17:39 +0300951static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300952 struct cfg80211_scan_request *req)
953{
Kalle Valo80301cd2009-06-12 14:17:39 +0300954 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300955 int ret;
956 u8 *ssid = NULL;
957 size_t ssid_len = 0;
958
Kalle Valo80301cd2009-06-12 14:17:39 +0300959 wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300960
961 if (req->n_ssids) {
962 ssid = req->ssids[0].ssid;
963 ssid_len = req->ssids[0].ssid_len;
964 }
965
966 mutex_lock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +0300967
Kalle Valo80301cd2009-06-12 14:17:39 +0300968 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300969 if (ret < 0)
970 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300971
Kalle Valo80301cd2009-06-12 14:17:39 +0300972 ret = wl1251_hw_scan(hw->priv, ssid, ssid_len, 1, 0, 13, 3);
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300973
Kalle Valo80301cd2009-06-12 14:17:39 +0300974 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300975
976out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300977 mutex_unlock(&wl->mutex);
978
979 return ret;
980}
981
Kalle Valo80301cd2009-06-12 14:17:39 +0300982static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300983{
Kalle Valo80301cd2009-06-12 14:17:39 +0300984 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300985 int ret;
986
Kalle Valocee4fd22009-06-12 14:16:20 +0300987 mutex_lock(&wl->mutex);
988
Kalle Valo80301cd2009-06-12 14:17:39 +0300989 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300990 if (ret < 0)
991 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300992
Kalle Valo80301cd2009-06-12 14:17:39 +0300993 ret = wl1251_acx_rts_threshold(wl, (u16) value);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300994 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300995 wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300996
Kalle Valo80301cd2009-06-12 14:17:39 +0300997 wl1251_ps_elp_sleep(wl);
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300998
Kalle Valoc5483b72009-06-12 14:16:32 +0300999out:
Kalle Valocee4fd22009-06-12 14:16:20 +03001000 mutex_unlock(&wl->mutex);
1001
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001002 return ret;
1003}
1004
Kalle Valo80301cd2009-06-12 14:17:39 +03001005static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001006 struct ieee80211_vif *vif,
1007 struct ieee80211_bss_conf *bss_conf,
1008 u32 changed)
1009{
Kalle Valo80301cd2009-06-12 14:17:39 +03001010 enum wl1251_cmd_ps_mode mode;
1011 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001012 struct sk_buff *beacon;
1013 int ret;
1014
Kalle Valo80301cd2009-06-12 14:17:39 +03001015 wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001016
1017 mutex_lock(&wl->mutex);
1018
Kalle Valo80301cd2009-06-12 14:17:39 +03001019 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001020 if (ret < 0)
1021 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +03001022
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001023 if (changed & BSS_CHANGED_ASSOC) {
1024 if (bss_conf->assoc) {
1025 wl->aid = bss_conf->aid;
1026
Kalle Valo80301cd2009-06-12 14:17:39 +03001027 ret = wl1251_build_ps_poll(wl, wl->aid);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001028 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001029 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001030
Kalle Valo80301cd2009-06-12 14:17:39 +03001031 ret = wl1251_acx_aid(wl, wl->aid);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001032 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001033 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001034
1035 /* If we want to go in PSM but we're not there yet */
1036 if (wl->psm_requested && !wl->psm) {
1037 mode = STATION_POWER_SAVE_MODE;
Kalle Valo80301cd2009-06-12 14:17:39 +03001038 ret = wl1251_ps_set_mode(wl, mode);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001039 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001040 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001041 }
1042 }
1043 }
1044 if (changed & BSS_CHANGED_ERP_SLOT) {
1045 if (bss_conf->use_short_slot)
Kalle Valo80301cd2009-06-12 14:17:39 +03001046 ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001047 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001048 ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001049 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001050 wl1251_warning("Set slot time failed %d", ret);
Kalle Valoc5483b72009-06-12 14:16:32 +03001051 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001052 }
1053 }
1054
1055 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1056 if (bss_conf->use_short_preamble)
Kalle Valo80301cd2009-06-12 14:17:39 +03001057 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001058 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001059 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001060 }
1061
1062 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1063 if (bss_conf->use_cts_prot)
Kalle Valo80301cd2009-06-12 14:17:39 +03001064 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001065 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001066 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001067 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001068 wl1251_warning("Set ctsprotect failed %d", ret);
1069 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001070 }
1071 }
1072
1073 if (changed & BSS_CHANGED_BSSID) {
1074 memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
1075
Kalle Valo80301cd2009-06-12 14:17:39 +03001076 ret = wl1251_build_null_data(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001077 if (ret < 0)
1078 goto out;
1079
1080 if (wl->bss_type != BSS_TYPE_IBSS) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001081 ret = wl1251_cmd_join(wl, wl->bss_type, 5, 100, 1);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001082 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +03001083 goto out_sleep;
1084 wl1251_warning("Set ctsprotect failed %d", ret);
1085 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001086 }
1087 }
1088
1089 if (changed & BSS_CHANGED_BEACON) {
1090 beacon = ieee80211_beacon_get(hw, vif);
Kalle Valo80301cd2009-06-12 14:17:39 +03001091 ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001092 beacon->len);
1093
1094 if (ret < 0) {
1095 dev_kfree_skb(beacon);
1096 goto out;
1097 }
1098
Kalle Valo80301cd2009-06-12 14:17:39 +03001099 ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001100 beacon->len);
1101
1102 dev_kfree_skb(beacon);
1103
1104 if (ret < 0)
1105 goto out;
1106
Juuso Oikarinen77cc9e42009-06-12 14:16:52 +03001107 ret = wl->chip.op_cmd_join(wl, wl->bss_type, 1, 100, 0);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001108
1109 if (ret < 0)
1110 goto out;
1111 }
1112
Kalle Valoc5483b72009-06-12 14:16:32 +03001113out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +03001114 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001115
1116out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001117 mutex_unlock(&wl->mutex);
1118}
1119
1120
1121/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001122static struct ieee80211_rate wl1251_rates[] = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001123 { .bitrate = 10,
1124 .hw_value = 0x1,
1125 .hw_value_short = 0x1, },
1126 { .bitrate = 20,
1127 .hw_value = 0x2,
1128 .hw_value_short = 0x2,
1129 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1130 { .bitrate = 55,
1131 .hw_value = 0x4,
1132 .hw_value_short = 0x4,
1133 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1134 { .bitrate = 110,
1135 .hw_value = 0x20,
1136 .hw_value_short = 0x20,
1137 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1138 { .bitrate = 60,
1139 .hw_value = 0x8,
1140 .hw_value_short = 0x8, },
1141 { .bitrate = 90,
1142 .hw_value = 0x10,
1143 .hw_value_short = 0x10, },
1144 { .bitrate = 120,
1145 .hw_value = 0x40,
1146 .hw_value_short = 0x40, },
1147 { .bitrate = 180,
1148 .hw_value = 0x80,
1149 .hw_value_short = 0x80, },
1150 { .bitrate = 240,
1151 .hw_value = 0x200,
1152 .hw_value_short = 0x200, },
1153 { .bitrate = 360,
1154 .hw_value = 0x400,
1155 .hw_value_short = 0x400, },
1156 { .bitrate = 480,
1157 .hw_value = 0x800,
1158 .hw_value_short = 0x800, },
1159 { .bitrate = 540,
1160 .hw_value = 0x1000,
1161 .hw_value_short = 0x1000, },
1162};
1163
1164/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001165static struct ieee80211_channel wl1251_channels[] = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001166 { .hw_value = 1, .center_freq = 2412},
1167 { .hw_value = 2, .center_freq = 2417},
1168 { .hw_value = 3, .center_freq = 2422},
1169 { .hw_value = 4, .center_freq = 2427},
1170 { .hw_value = 5, .center_freq = 2432},
1171 { .hw_value = 6, .center_freq = 2437},
1172 { .hw_value = 7, .center_freq = 2442},
1173 { .hw_value = 8, .center_freq = 2447},
1174 { .hw_value = 9, .center_freq = 2452},
1175 { .hw_value = 10, .center_freq = 2457},
1176 { .hw_value = 11, .center_freq = 2462},
1177 { .hw_value = 12, .center_freq = 2467},
1178 { .hw_value = 13, .center_freq = 2472},
1179};
1180
1181/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001182static struct ieee80211_supported_band wl1251_band_2ghz = {
1183 .channels = wl1251_channels,
1184 .n_channels = ARRAY_SIZE(wl1251_channels),
1185 .bitrates = wl1251_rates,
1186 .n_bitrates = ARRAY_SIZE(wl1251_rates),
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001187};
1188
Kalle Valo80301cd2009-06-12 14:17:39 +03001189static const struct ieee80211_ops wl1251_ops = {
1190 .start = wl1251_op_start,
1191 .stop = wl1251_op_stop,
1192 .add_interface = wl1251_op_add_interface,
1193 .remove_interface = wl1251_op_remove_interface,
1194 .config = wl1251_op_config,
1195 .configure_filter = wl1251_op_configure_filter,
1196 .tx = wl1251_op_tx,
1197 .set_key = wl1251_op_set_key,
1198 .hw_scan = wl1251_op_hw_scan,
1199 .bss_info_changed = wl1251_op_bss_info_changed,
1200 .set_rts_threshold = wl1251_op_set_rts_threshold,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001201};
1202
Kalle Valo80301cd2009-06-12 14:17:39 +03001203static int wl1251_register_hw(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001204{
1205 int ret;
1206
1207 if (wl->mac80211_registered)
1208 return 0;
1209
1210 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1211
1212 ret = ieee80211_register_hw(wl->hw);
1213 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001214 wl1251_error("unable to register mac80211 hw: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001215 return ret;
1216 }
1217
1218 wl->mac80211_registered = true;
1219
Kalle Valo80301cd2009-06-12 14:17:39 +03001220 wl1251_notice("loaded");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001221
1222 return 0;
1223}
1224
Kalle Valo80301cd2009-06-12 14:17:39 +03001225static int wl1251_init_ieee80211(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001226{
1227 /* The tx descriptor buffer and the TKIP space */
1228 wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +03001229 + WL1251_TKIP_IV_SPACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001230
1231 /* unit us */
1232 /* FIXME: find a proper value */
1233 wl->hw->channel_change_time = 10000;
1234
1235 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
1236 IEEE80211_HW_NOISE_DBM;
1237
1238 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1239 wl->hw->wiphy->max_scan_ssids = 1;
Kalle Valo80301cd2009-06-12 14:17:39 +03001240 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1251_band_2ghz;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001241
1242 SET_IEEE80211_DEV(wl->hw, &wl->spi->dev);
1243
1244 return 0;
1245}
1246
Kalle Valo80301cd2009-06-12 14:17:39 +03001247#define WL1251_DEFAULT_CHANNEL 1
1248static int __devinit wl1251_probe(struct spi_device *spi)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001249{
1250 struct wl12xx_platform_data *pdata;
1251 struct ieee80211_hw *hw;
Kalle Valo80301cd2009-06-12 14:17:39 +03001252 struct wl1251 *wl;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001253 int ret, i;
1254 static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1255
1256 pdata = spi->dev.platform_data;
1257 if (!pdata) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001258 wl1251_error("no platform data");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001259 return -ENODEV;
1260 }
1261
Kalle Valo80301cd2009-06-12 14:17:39 +03001262 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001263 if (!hw) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001264 wl1251_error("could not alloc ieee80211_hw");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001265 return -ENOMEM;
1266 }
1267
1268 wl = hw->priv;
1269 memset(wl, 0, sizeof(*wl));
1270
1271 wl->hw = hw;
1272 dev_set_drvdata(&spi->dev, wl);
1273 wl->spi = spi;
1274
1275 wl->data_in_count = 0;
1276
1277 skb_queue_head_init(&wl->tx_queue);
1278
Kalle Valo80301cd2009-06-12 14:17:39 +03001279 INIT_WORK(&wl->filter_work, wl1251_filter_work);
1280 wl->channel = WL1251_DEFAULT_CHANNEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001281 wl->scanning = false;
1282 wl->default_key = 0;
1283 wl->listen_int = 1;
1284 wl->rx_counter = 0;
1285 wl->rx_handled = 0;
1286 wl->rx_current_buffer = 0;
1287 wl->rx_last_id = 0;
Kalle Valo80301cd2009-06-12 14:17:39 +03001288 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
1289 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001290 wl->elp = false;
1291 wl->psm = 0;
1292 wl->psm_requested = false;
1293 wl->tx_queue_stopped = false;
Kalle Valo80301cd2009-06-12 14:17:39 +03001294 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001295
1296 /* We use the default power on sleep time until we know which chip
1297 * we're using */
Kalle Valo80301cd2009-06-12 14:17:39 +03001298 wl->chip.power_on_sleep = WL1251_DEFAULT_POWER_ON_SLEEP;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001299
1300 for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
1301 wl->tx_frames[i] = NULL;
1302
1303 wl->next_tx_complete = 0;
1304
1305 /*
1306 * In case our MAC address is not correctly set,
1307 * we use a random but Nokia MAC.
1308 */
1309 memcpy(wl->mac_addr, nokia_oui, 3);
1310 get_random_bytes(wl->mac_addr + 3, 3);
1311
Kalle Valo80301cd2009-06-12 14:17:39 +03001312 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001313 mutex_init(&wl->mutex);
1314
1315 wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
1316 wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
1317
Kalle Valo47212132009-06-12 14:15:08 +03001318 wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1319 if (!wl->rx_descriptor) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001320 wl1251_error("could not allocate memory for rx descriptor");
Kalle Valo47212132009-06-12 14:15:08 +03001321 ret = -ENOMEM;
1322 goto out_free;
1323 }
1324
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001325 /* This is the only SPI value that we need to set here, the rest
1326 * comes from the board-peripherals file */
1327 spi->bits_per_word = 32;
1328
1329 ret = spi_setup(spi);
1330 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001331 wl1251_error("spi_setup failed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001332 goto out_free;
1333 }
1334
1335 wl->set_power = pdata->set_power;
1336 if (!wl->set_power) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001337 wl1251_error("set power function missing in platform data");
Kalle Valoc4f5c852009-06-12 14:14:34 +03001338 ret = -ENODEV;
1339 goto out_free;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001340 }
1341
1342 wl->irq = spi->irq;
1343 if (wl->irq < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001344 wl1251_error("irq missing in platform data");
Kalle Valoc4f5c852009-06-12 14:14:34 +03001345 ret = -ENODEV;
1346 goto out_free;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001347 }
1348
Kalle Valo80301cd2009-06-12 14:17:39 +03001349 ret = request_irq(wl->irq, wl1251_irq, 0, DRIVER_NAME, wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001350 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001351 wl1251_error("request_irq() failed: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001352 goto out_free;
1353 }
1354
1355 set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
1356
1357 disable_irq(wl->irq);
1358
Kalle Valo80301cd2009-06-12 14:17:39 +03001359 ret = wl1251_init_ieee80211(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001360 if (ret)
1361 goto out_irq;
1362
Kalle Valo80301cd2009-06-12 14:17:39 +03001363 ret = wl1251_register_hw(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001364 if (ret)
1365 goto out_irq;
1366
Kalle Valo80301cd2009-06-12 14:17:39 +03001367 wl1251_debugfs_init(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001368
Kalle Valo80301cd2009-06-12 14:17:39 +03001369 wl1251_notice("initialized");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001370
1371 return 0;
1372
1373 out_irq:
1374 free_irq(wl->irq, wl);
1375
1376 out_free:
Kalle Valo47212132009-06-12 14:15:08 +03001377 kfree(wl->rx_descriptor);
1378 wl->rx_descriptor = NULL;
1379
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001380 ieee80211_free_hw(hw);
1381
1382 return ret;
1383}
1384
Kalle Valo80301cd2009-06-12 14:17:39 +03001385static int __devexit wl1251_remove(struct spi_device *spi)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001386{
Kalle Valo80301cd2009-06-12 14:17:39 +03001387 struct wl1251 *wl = dev_get_drvdata(&spi->dev);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001388
1389 ieee80211_unregister_hw(wl->hw);
1390
Kalle Valo80301cd2009-06-12 14:17:39 +03001391 wl1251_debugfs_exit(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001392
1393 free_irq(wl->irq, wl);
1394 kfree(wl->target_mem_map);
1395 kfree(wl->data_path);
1396 kfree(wl->fw);
1397 wl->fw = NULL;
1398 kfree(wl->nvs);
1399 wl->nvs = NULL;
Kalle Valo47212132009-06-12 14:15:08 +03001400
1401 kfree(wl->rx_descriptor);
1402 wl->rx_descriptor = NULL;
1403
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001404 ieee80211_free_hw(wl->hw);
1405
1406 return 0;
1407}
1408
1409
Kalle Valo80301cd2009-06-12 14:17:39 +03001410static struct spi_driver wl1251_spi_driver = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001411 .driver = {
Kalle Valo80301cd2009-06-12 14:17:39 +03001412 /* FIXME: use wl12xx name to not break the user space */
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001413 .name = "wl12xx",
1414 .bus = &spi_bus_type,
1415 .owner = THIS_MODULE,
1416 },
1417
Kalle Valo80301cd2009-06-12 14:17:39 +03001418 .probe = wl1251_probe,
1419 .remove = __devexit_p(wl1251_remove),
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001420};
1421
Kalle Valo80301cd2009-06-12 14:17:39 +03001422static int __init wl1251_init(void)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001423{
1424 int ret;
1425
Kalle Valo80301cd2009-06-12 14:17:39 +03001426 ret = spi_register_driver(&wl1251_spi_driver);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001427 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001428 wl1251_error("failed to register spi driver: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001429 goto out;
1430 }
1431
1432out:
1433 return ret;
1434}
1435
Kalle Valo80301cd2009-06-12 14:17:39 +03001436static void __exit wl1251_exit(void)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001437{
Kalle Valo80301cd2009-06-12 14:17:39 +03001438 spi_unregister_driver(&wl1251_spi_driver);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001439
Kalle Valo80301cd2009-06-12 14:17:39 +03001440 wl1251_notice("unloaded");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001441}
1442
Kalle Valo80301cd2009-06-12 14:17:39 +03001443module_init(wl1251_init);
1444module_exit(wl1251_exit);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001445
1446MODULE_LICENSE("GPL");
1447MODULE_AUTHOR("Kalle Valo <Kalle.Valo@nokia.com>, "
1448 "Luciano Coelho <luciano.coelho@nokia.com>");