blob: f4bc5796c245d251e527a3515cf8f8e45d2e55ed [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
158 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}
162
Kalle Valo80301cd2009-06-12 14:17:39 +0300163static int wl1251_chip_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300164{
165 int ret = 0;
166
Kalle Valo80301cd2009-06-12 14:17:39 +0300167 wl1251_power_on(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300168 msleep(wl->chip.power_on_sleep);
Kalle Valo80301cd2009-06-12 14:17:39 +0300169 wl1251_spi_reset(wl);
170 wl1251_spi_init(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300171
172 /* We don't need a real memory partition here, because we only want
173 * to use the registers at this point. */
Kalle Valo80301cd2009-06-12 14:17:39 +0300174 wl1251_set_partition(wl,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300175 0x00000000,
176 0x00000000,
177 REGISTERS_BASE,
178 REGISTERS_DOWN_SIZE);
179
180 /* ELP module wake up */
Kalle Valo80301cd2009-06-12 14:17:39 +0300181 wl1251_fw_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300182
183 /* whal_FwCtrl_BootSm() */
184
185 /* 0. read chip id from CHIP_ID */
Kalle Valo80301cd2009-06-12 14:17:39 +0300186 wl->chip.id = wl1251_reg_read32(wl, CHIP_ID_B);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300187
188 /* 1. check if chip id is valid */
189
190 switch (wl->chip.id) {
191 case CHIP_ID_1251_PG12:
Kalle Valo80301cd2009-06-12 14:17:39 +0300192 wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG12)",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300193 wl->chip.id);
194
195 wl1251_setup(wl);
196
197 break;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300198 case CHIP_ID_1251_PG10:
199 case CHIP_ID_1251_PG11:
200 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300201 wl1251_error("unsupported chip id: 0x%x", wl->chip.id);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300202 ret = -ENODEV;
203 goto out;
204 }
205
206 if (wl->fw == NULL) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300207 ret = wl1251_fetch_firmware(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300208 if (ret < 0)
209 goto out;
210 }
211
212 /* No NVS from netlink, try to get it from the filesystem */
213 if (wl->nvs == NULL) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300214 ret = wl1251_fetch_nvs(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300215 if (ret < 0)
216 goto out;
217 }
218
219out:
220 return ret;
221}
222
Kalle Valo80301cd2009-06-12 14:17:39 +0300223static void wl1251_filter_work(struct work_struct *work)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300224{
Kalle Valo80301cd2009-06-12 14:17:39 +0300225 struct wl1251 *wl =
226 container_of(work, struct wl1251, filter_work);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300227 int ret;
228
229 mutex_lock(&wl->mutex);
230
Kalle Valo80301cd2009-06-12 14:17:39 +0300231 if (wl->state == WL1251_STATE_OFF)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300232 goto out;
233
Kalle Valo80301cd2009-06-12 14:17:39 +0300234 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300235 if (ret < 0)
236 goto out;
237
Juuso Oikarinen77cc9e42009-06-12 14:16:52 +0300238 /* FIXME: replace the magic numbers with proper definitions */
239 ret = wl->chip.op_cmd_join(wl, wl->bss_type, 1, 100, 0);
Kalle Valoc5483b72009-06-12 14:16:32 +0300240 if (ret < 0)
241 goto out_sleep;
242
243out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300244 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300245
246out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300247 mutex_unlock(&wl->mutex);
248}
249
Kalle Valo80301cd2009-06-12 14:17:39 +0300250int wl1251_plt_start(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300251{
252 int ret;
253
Kalle Valo80301cd2009-06-12 14:17:39 +0300254 mutex_lock(&wl->mutex);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300255
Kalle Valo80301cd2009-06-12 14:17:39 +0300256 wl1251_notice("power up");
257
258 if (wl->state != WL1251_STATE_OFF) {
259 wl1251_error("cannot go into PLT state because not "
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300260 "in off state: %d", wl->state);
261 return -EBUSY;
262 }
263
Kalle Valo80301cd2009-06-12 14:17:39 +0300264 wl->state = WL1251_STATE_PLT;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300265
Kalle Valo80301cd2009-06-12 14:17:39 +0300266 ret = wl1251_chip_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300267 if (ret < 0)
268 return ret;
269
270 ret = wl->chip.op_boot(wl);
271 if (ret < 0)
272 return ret;
273
Kalle Valo80301cd2009-06-12 14:17:39 +0300274 wl1251_notice("firmware booted in PLT mode (%s)", wl->chip.fw_ver);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300275
276 ret = wl->chip.op_plt_init(wl);
277 if (ret < 0)
278 return ret;
279
280 return 0;
281}
282
Kalle Valo80301cd2009-06-12 14:17:39 +0300283int wl1251_plt_stop(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300284{
Kalle Valo80301cd2009-06-12 14:17:39 +0300285 mutex_lock(&wl->mutex);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300286
Kalle Valo80301cd2009-06-12 14:17:39 +0300287 wl1251_notice("power down");
288
289 if (wl->state != WL1251_STATE_PLT) {
290 wl1251_error("cannot power down because not in PLT "
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300291 "state: %d", wl->state);
292 return -EBUSY;
293 }
294
Kalle Valo80301cd2009-06-12 14:17:39 +0300295 wl1251_disable_interrupts(wl);
296 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300297
Kalle Valo80301cd2009-06-12 14:17:39 +0300298 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300299
300 return 0;
301}
302
303
Kalle Valo80301cd2009-06-12 14:17:39 +0300304static int wl1251_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300305{
Kalle Valo80301cd2009-06-12 14:17:39 +0300306 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300307
308 skb_queue_tail(&wl->tx_queue, skb);
309
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +0300310 /*
311 * The chip specific setup must run before the first TX packet -
312 * before that, the tx_work will not be initialized!
313 */
314
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300315 schedule_work(&wl->tx_work);
316
317 /*
318 * The workqueue is slow to process the tx_queue and we need stop
319 * the queue here, otherwise the queue will get too long.
320 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300321 if (skb_queue_len(&wl->tx_queue) >= WL1251_TX_QUEUE_MAX_LENGTH) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300322 ieee80211_stop_queues(wl->hw);
323
324 /*
325 * FIXME: this is racy, the variable is not properly
326 * protected. Maybe fix this by removing the stupid
327 * variable altogether and checking the real queue state?
328 */
329 wl->tx_queue_stopped = true;
330 }
331
332 return NETDEV_TX_OK;
333}
334
Kalle Valo80301cd2009-06-12 14:17:39 +0300335static int wl1251_op_start(struct ieee80211_hw *hw)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300336{
Kalle Valo80301cd2009-06-12 14:17:39 +0300337 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300338 int ret = 0;
339
Kalle Valo80301cd2009-06-12 14:17:39 +0300340 wl1251_debug(DEBUG_MAC80211, "mac80211 start");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300341
342 mutex_lock(&wl->mutex);
343
Kalle Valo80301cd2009-06-12 14:17:39 +0300344 if (wl->state != WL1251_STATE_OFF) {
345 wl1251_error("cannot start because not in off state: %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300346 wl->state);
347 ret = -EBUSY;
348 goto out;
349 }
350
Kalle Valo80301cd2009-06-12 14:17:39 +0300351 ret = wl1251_chip_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300352 if (ret < 0)
353 return ret;
354
355 ret = wl->chip.op_boot(wl);
356 if (ret < 0)
357 goto out;
358
359 ret = wl->chip.op_hw_init(wl);
360 if (ret < 0)
361 goto out;
362
Kalle Valo80301cd2009-06-12 14:17:39 +0300363 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300364 if (ret < 0)
365 goto out;
366
Kalle Valo80301cd2009-06-12 14:17:39 +0300367 wl->state = WL1251_STATE_ON;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300368
Kalle Valo80301cd2009-06-12 14:17:39 +0300369 wl1251_info("firmware booted (%s)", wl->chip.fw_ver);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300370
371out:
372 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300373 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300374
375 mutex_unlock(&wl->mutex);
376
377 return ret;
378}
379
Kalle Valo80301cd2009-06-12 14:17:39 +0300380static void wl1251_op_stop(struct ieee80211_hw *hw)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300381{
Kalle Valo80301cd2009-06-12 14:17:39 +0300382 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300383
Kalle Valo80301cd2009-06-12 14:17:39 +0300384 wl1251_info("down");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300385
Kalle Valo80301cd2009-06-12 14:17:39 +0300386 wl1251_debug(DEBUG_MAC80211, "mac80211 stop");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300387
388 mutex_lock(&wl->mutex);
389
Kalle Valo80301cd2009-06-12 14:17:39 +0300390 WARN_ON(wl->state != WL1251_STATE_ON);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300391
392 if (wl->scanning) {
393 mutex_unlock(&wl->mutex);
394 ieee80211_scan_completed(wl->hw, true);
395 mutex_lock(&wl->mutex);
396 wl->scanning = false;
397 }
398
Kalle Valo80301cd2009-06-12 14:17:39 +0300399 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300400
Kalle Valo80301cd2009-06-12 14:17:39 +0300401 wl1251_disable_interrupts(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300402
403 mutex_unlock(&wl->mutex);
404
405 cancel_work_sync(&wl->irq_work);
406 cancel_work_sync(&wl->tx_work);
407 cancel_work_sync(&wl->filter_work);
408
409 mutex_lock(&wl->mutex);
410
411 /* let's notify MAC80211 about the remaining pending TX frames */
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +0300412 wl->chip.op_tx_flush(wl);
Kalle Valo80301cd2009-06-12 14:17:39 +0300413 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300414
415 memset(wl->bssid, 0, ETH_ALEN);
416 wl->listen_int = 1;
417 wl->bss_type = MAX_BSS_TYPE;
418
419 wl->data_in_count = 0;
420 wl->rx_counter = 0;
421 wl->rx_handled = 0;
422 wl->rx_current_buffer = 0;
423 wl->rx_last_id = 0;
424 wl->next_tx_complete = 0;
425 wl->elp = false;
426 wl->psm = 0;
427 wl->tx_queue_stopped = false;
Kalle Valo80301cd2009-06-12 14:17:39 +0300428 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300429
Kalle Valo80301cd2009-06-12 14:17:39 +0300430 wl1251_debugfs_reset(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300431
432 mutex_unlock(&wl->mutex);
433}
434
Kalle Valo80301cd2009-06-12 14:17:39 +0300435static int wl1251_op_add_interface(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300436 struct ieee80211_if_init_conf *conf)
437{
Kalle Valo80301cd2009-06-12 14:17:39 +0300438 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300439 DECLARE_MAC_BUF(mac);
440 int ret = 0;
441
Kalle Valo80301cd2009-06-12 14:17:39 +0300442 wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %s",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300443 conf->type, print_mac(mac, conf->mac_addr));
444
445 mutex_lock(&wl->mutex);
446
447 switch (conf->type) {
448 case NL80211_IFTYPE_STATION:
449 wl->bss_type = BSS_TYPE_STA_BSS;
450 break;
451 case NL80211_IFTYPE_ADHOC:
452 wl->bss_type = BSS_TYPE_IBSS;
453 break;
454 default:
455 ret = -EOPNOTSUPP;
456 goto out;
457 }
458
459 if (memcmp(wl->mac_addr, conf->mac_addr, ETH_ALEN)) {
460 memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
461 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
Kalle Valo80301cd2009-06-12 14:17:39 +0300462 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300463 if (ret < 0)
464 goto out;
465 }
466
467out:
468 mutex_unlock(&wl->mutex);
469 return ret;
470}
471
Kalle Valo80301cd2009-06-12 14:17:39 +0300472static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300473 struct ieee80211_if_init_conf *conf)
474{
Kalle Valo80301cd2009-06-12 14:17:39 +0300475 wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300476}
477
Kalle Valo80301cd2009-06-12 14:17:39 +0300478static int wl1251_build_null_data(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300479{
480 struct wl12xx_null_data_template template;
481
482 if (!is_zero_ether_addr(wl->bssid)) {
483 memcpy(template.header.da, wl->bssid, ETH_ALEN);
484 memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
485 } else {
486 memset(template.header.da, 0xff, ETH_ALEN);
487 memset(template.header.bssid, 0xff, ETH_ALEN);
488 }
489
490 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
491 template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
492 IEEE80211_STYPE_NULLFUNC);
493
Kalle Valo80301cd2009-06-12 14:17:39 +0300494 return wl1251_cmd_template_set(wl, CMD_NULL_DATA, &template,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300495 sizeof(template));
496
497}
498
Kalle Valo80301cd2009-06-12 14:17:39 +0300499static int wl1251_build_ps_poll(struct wl1251 *wl, u16 aid)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300500{
501 struct wl12xx_ps_poll_template template;
502
503 memcpy(template.bssid, wl->bssid, ETH_ALEN);
504 memcpy(template.ta, wl->mac_addr, ETH_ALEN);
505 template.aid = aid;
506 template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
507
Kalle Valo80301cd2009-06-12 14:17:39 +0300508 return wl1251_cmd_template_set(wl, CMD_PS_POLL, &template,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300509 sizeof(template));
510
511}
512
Kalle Valo80301cd2009-06-12 14:17:39 +0300513static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300514{
Kalle Valo80301cd2009-06-12 14:17:39 +0300515 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300516 struct ieee80211_conf *conf = &hw->conf;
517 int channel, ret = 0;
518
519 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
520
Kalle Valo80301cd2009-06-12 14:17:39 +0300521 wl1251_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300522 channel,
523 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
524 conf->power_level);
525
526 mutex_lock(&wl->mutex);
527
Kalle Valo80301cd2009-06-12 14:17:39 +0300528 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300529 if (ret < 0)
530 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300531
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300532 if (channel != wl->channel) {
533 /* FIXME: use beacon interval provided by mac80211 */
Juuso Oikarinen77cc9e42009-06-12 14:16:52 +0300534 ret = wl->chip.op_cmd_join(wl, wl->bss_type, 1, 100, 0);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300535 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300536 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300537
538 wl->channel = channel;
539 }
540
Kalle Valo80301cd2009-06-12 14:17:39 +0300541 ret = wl1251_build_null_data(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300542 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300543 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300544
545 if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300546 wl1251_info("psm enabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300547
548 wl->psm_requested = true;
549
550 /*
551 * We enter PSM only if we're already associated.
552 * If we're not, we'll enter it when joining an SSID,
553 * through the bss_info_changed() hook.
554 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300555 ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300556 } else if (!(conf->flags & IEEE80211_CONF_PS) &&
557 wl->psm_requested) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300558 wl1251_info("psm disabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300559
560 wl->psm_requested = false;
561
562 if (wl->psm)
Kalle Valo80301cd2009-06-12 14:17:39 +0300563 ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300564 }
565
566 if (conf->power_level != wl->power_level) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300567 ret = wl1251_acx_tx_power(wl, conf->power_level);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300568 if (ret < 0)
569 goto out;
570
571 wl->power_level = conf->power_level;
572 }
573
Kalle Valoc5483b72009-06-12 14:16:32 +0300574out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300575 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300576
577out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300578 mutex_unlock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +0300579
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300580 return ret;
581}
582
Kalle Valo80301cd2009-06-12 14:17:39 +0300583#define WL1251_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300584 FIF_ALLMULTI | \
585 FIF_FCSFAIL | \
586 FIF_BCN_PRBRESP_PROMISC | \
587 FIF_CONTROL | \
588 FIF_OTHER_BSS)
589
Kalle Valo80301cd2009-06-12 14:17:39 +0300590static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300591 unsigned int changed,
592 unsigned int *total,
593 int mc_count,
594 struct dev_addr_list *mc_list)
595{
Kalle Valo80301cd2009-06-12 14:17:39 +0300596 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300597
Kalle Valo80301cd2009-06-12 14:17:39 +0300598 wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300599
Kalle Valo80301cd2009-06-12 14:17:39 +0300600 *total &= WL1251_SUPPORTED_FILTERS;
601 changed &= WL1251_SUPPORTED_FILTERS;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300602
603 if (changed == 0)
604 /* no filters which we support changed */
605 return;
606
607 /* FIXME: wl->rx_config and wl->rx_filter are not protected */
608
Kalle Valo80301cd2009-06-12 14:17:39 +0300609 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
610 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300611
612 if (*total & FIF_PROMISC_IN_BSS) {
613 wl->rx_config |= CFG_BSSID_FILTER_EN;
614 wl->rx_config |= CFG_RX_ALL_GOOD;
615 }
616 if (*total & FIF_ALLMULTI)
617 /*
618 * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
619 * all multicast frames
620 */
621 wl->rx_config &= ~CFG_MC_FILTER_EN;
622 if (*total & FIF_FCSFAIL)
623 wl->rx_filter |= CFG_RX_FCS_ERROR;
624 if (*total & FIF_BCN_PRBRESP_PROMISC) {
625 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
626 wl->rx_config &= ~CFG_SSID_FILTER_EN;
627 }
628 if (*total & FIF_CONTROL)
629 wl->rx_filter |= CFG_RX_CTL_EN;
630 if (*total & FIF_OTHER_BSS)
631 wl->rx_filter &= ~CFG_BSSID_FILTER_EN;
632
633 /*
634 * FIXME: workqueues need to be properly cancelled on stop(), for
635 * now let's just disable changing the filter settings. They will
636 * be updated any on config().
637 */
638 /* schedule_work(&wl->filter_work); */
639}
640
641/* HW encryption */
Kalle Valo80301cd2009-06-12 14:17:39 +0300642static int wl1251_set_key_type(struct wl1251 *wl,
643 struct wl1251_cmd_set_keys *key,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300644 enum set_key_cmd cmd,
645 struct ieee80211_key_conf *mac80211_key,
646 const u8 *addr)
647{
648 switch (mac80211_key->alg) {
649 case ALG_WEP:
650 if (is_broadcast_ether_addr(addr))
651 key->key_type = KEY_WEP_DEFAULT;
652 else
653 key->key_type = KEY_WEP_ADDR;
654
655 mac80211_key->hw_key_idx = mac80211_key->keyidx;
656 break;
657 case ALG_TKIP:
658 if (is_broadcast_ether_addr(addr))
659 key->key_type = KEY_TKIP_MIC_GROUP;
660 else
661 key->key_type = KEY_TKIP_MIC_PAIRWISE;
662
663 mac80211_key->hw_key_idx = mac80211_key->keyidx;
664 break;
665 case ALG_CCMP:
666 if (is_broadcast_ether_addr(addr))
667 key->key_type = KEY_AES_GROUP;
668 else
669 key->key_type = KEY_AES_PAIRWISE;
670 mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
671 break;
672 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300673 wl1251_error("Unknown key algo 0x%x", mac80211_key->alg);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300674 return -EOPNOTSUPP;
675 }
676
677 return 0;
678}
679
Kalle Valo80301cd2009-06-12 14:17:39 +0300680static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300681 struct ieee80211_vif *vif,
682 struct ieee80211_sta *sta,
683 struct ieee80211_key_conf *key)
684{
Kalle Valo80301cd2009-06-12 14:17:39 +0300685 struct wl1251 *wl = hw->priv;
686 struct wl1251_cmd_set_keys *wl_cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300687 const u8 *addr;
688 int ret;
689
690 static const u8 bcast_addr[ETH_ALEN] =
691 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
692
Kalle Valo80301cd2009-06-12 14:17:39 +0300693 wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300694
Kalle Valoff258392009-06-12 14:14:19 +0300695 wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
696 if (!wl_cmd) {
697 ret = -ENOMEM;
698 goto out;
699 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300700
701 addr = sta ? sta->addr : bcast_addr;
702
Kalle Valo80301cd2009-06-12 14:17:39 +0300703 wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
704 wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
705 wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300706 key->alg, key->keyidx, key->keylen, key->flags);
Kalle Valo80301cd2009-06-12 14:17:39 +0300707 wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300708
Kalle Valoff258392009-06-12 14:14:19 +0300709 if (is_zero_ether_addr(addr)) {
710 /* We dont support TX only encryption */
711 ret = -EOPNOTSUPP;
712 goto out;
713 }
714
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300715 mutex_lock(&wl->mutex);
716
Kalle Valo80301cd2009-06-12 14:17:39 +0300717 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300718 if (ret < 0)
719 goto out_unlock;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300720
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300721 switch (cmd) {
722 case SET_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300723 wl_cmd->key_action = KEY_ADD_OR_REPLACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300724 break;
725 case DISABLE_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300726 wl_cmd->key_action = KEY_REMOVE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300727 break;
728 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300729 wl1251_error("Unsupported key cmd 0x%x", cmd);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300730 break;
731 }
732
Kalle Valo80301cd2009-06-12 14:17:39 +0300733 ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300734 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300735 wl1251_error("Set KEY type failed");
Kalle Valoc5483b72009-06-12 14:16:32 +0300736 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300737 }
738
Kalle Valoff258392009-06-12 14:14:19 +0300739 if (wl_cmd->key_type != KEY_WEP_DEFAULT)
740 memcpy(wl_cmd->addr, addr, ETH_ALEN);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300741
Kalle Valoff258392009-06-12 14:14:19 +0300742 if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
743 (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300744 /*
745 * We get the key in the following form:
746 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
747 * but the target is expecting:
748 * TKIP - RX MIC - TX MIC
749 */
Kalle Valoff258392009-06-12 14:14:19 +0300750 memcpy(wl_cmd->key, key->key, 16);
751 memcpy(wl_cmd->key + 16, key->key + 24, 8);
752 memcpy(wl_cmd->key + 24, key->key + 16, 8);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300753
754 } else {
Kalle Valoff258392009-06-12 14:14:19 +0300755 memcpy(wl_cmd->key, key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300756 }
Kalle Valoff258392009-06-12 14:14:19 +0300757 wl_cmd->key_size = key->keylen;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300758
Kalle Valoff258392009-06-12 14:14:19 +0300759 wl_cmd->id = key->keyidx;
760 wl_cmd->ssid_profile = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300761
Kalle Valo80301cd2009-06-12 14:17:39 +0300762 wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300763
Kalle Valo80301cd2009-06-12 14:17:39 +0300764 ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
Kalle Valoff258392009-06-12 14:14:19 +0300765 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300766 wl1251_warning("could not set keys");
Kalle Valoc5483b72009-06-12 14:16:32 +0300767 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300768 }
769
Kalle Valoc5483b72009-06-12 14:16:32 +0300770out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300771 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300772
773out_unlock:
Kalle Valoff258392009-06-12 14:14:19 +0300774 mutex_unlock(&wl->mutex);
775
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300776out:
Kalle Valoff258392009-06-12 14:14:19 +0300777 kfree(wl_cmd);
778
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300779 return ret;
780}
781
Kalle Valo80301cd2009-06-12 14:17:39 +0300782static int wl1251_build_basic_rates(char *rates)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300783{
784 u8 index = 0;
785
786 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
787 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
788 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
789 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
790
791 return index;
792}
793
Kalle Valo80301cd2009-06-12 14:17:39 +0300794static int wl1251_build_extended_rates(char *rates)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300795{
796 u8 index = 0;
797
798 rates[index++] = IEEE80211_OFDM_RATE_6MB;
799 rates[index++] = IEEE80211_OFDM_RATE_9MB;
800 rates[index++] = IEEE80211_OFDM_RATE_12MB;
801 rates[index++] = IEEE80211_OFDM_RATE_18MB;
802 rates[index++] = IEEE80211_OFDM_RATE_24MB;
803 rates[index++] = IEEE80211_OFDM_RATE_36MB;
804 rates[index++] = IEEE80211_OFDM_RATE_48MB;
805 rates[index++] = IEEE80211_OFDM_RATE_54MB;
806
807 return index;
808}
809
810
Kalle Valo80301cd2009-06-12 14:17:39 +0300811static int wl1251_build_probe_req(struct wl1251 *wl, u8 *ssid, size_t ssid_len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300812{
813 struct wl12xx_probe_req_template template;
814 struct wl12xx_ie_rates *rates;
815 char *ptr;
816 u16 size;
817
818 ptr = (char *)&template;
819 size = sizeof(struct ieee80211_header);
820
821 memset(template.header.da, 0xff, ETH_ALEN);
822 memset(template.header.bssid, 0xff, ETH_ALEN);
823 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
824 template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
825
826 /* IEs */
827 /* SSID */
828 template.ssid.header.id = WLAN_EID_SSID;
829 template.ssid.header.len = ssid_len;
830 if (ssid_len && ssid)
831 memcpy(template.ssid.ssid, ssid, ssid_len);
832 size += sizeof(struct wl12xx_ie_header) + ssid_len;
833 ptr += size;
834
835 /* Basic Rates */
836 rates = (struct wl12xx_ie_rates *)ptr;
837 rates->header.id = WLAN_EID_SUPP_RATES;
Kalle Valo80301cd2009-06-12 14:17:39 +0300838 rates->header.len = wl1251_build_basic_rates(rates->rates);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300839 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
840 ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
841
842 /* Extended rates */
843 rates = (struct wl12xx_ie_rates *)ptr;
844 rates->header.id = WLAN_EID_EXT_SUPP_RATES;
Kalle Valo80301cd2009-06-12 14:17:39 +0300845 rates->header.len = wl1251_build_extended_rates(rates->rates);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300846 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
847
Kalle Valo80301cd2009-06-12 14:17:39 +0300848 wl1251_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300849
Kalle Valo80301cd2009-06-12 14:17:39 +0300850 return wl1251_cmd_template_set(wl, CMD_PROBE_REQ, &template,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300851 size);
852}
853
Kalle Valo80301cd2009-06-12 14:17:39 +0300854static int wl1251_hw_scan(struct wl1251 *wl, u8 *ssid, size_t len,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300855 u8 active_scan, u8 high_prio, u8 num_channels,
856 u8 probe_requests)
857{
Kalle Valo80301cd2009-06-12 14:17:39 +0300858 struct wl1251_cmd_trigger_scan_to *trigger = NULL;
Kalle Valoff258392009-06-12 14:14:19 +0300859 struct cmd_scan *params = NULL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300860 int i, ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300861 u16 scan_options = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300862
863 if (wl->scanning)
864 return -EINVAL;
865
866 params = kzalloc(sizeof(*params), GFP_KERNEL);
867 if (!params)
868 return -ENOMEM;
869
870 params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
871 params->params.rx_filter_options =
872 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
873
874 /* High priority scan */
875 if (!active_scan)
876 scan_options |= SCAN_PASSIVE;
877 if (high_prio)
878 scan_options |= SCAN_PRIORITY_HIGH;
879 params->params.scan_options = scan_options;
880
881 params->params.num_channels = num_channels;
882 params->params.num_probe_requests = probe_requests;
883 params->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
884 params->params.tid_trigger = 0;
885
886 for (i = 0; i < num_channels; i++) {
887 params->channels[i].min_duration = cpu_to_le32(30000);
888 params->channels[i].max_duration = cpu_to_le32(60000);
889 memset(&params->channels[i].bssid_lsb, 0xff, 4);
890 memset(&params->channels[i].bssid_msb, 0xff, 2);
891 params->channels[i].early_termination = 0;
892 params->channels[i].tx_power_att = 0;
893 params->channels[i].channel = i + 1;
894 memset(params->channels[i].pad, 0, 3);
895 }
896
897 for (i = num_channels; i < SCAN_MAX_NUM_OF_CHANNELS; i++)
898 memset(&params->channels[i], 0,
899 sizeof(struct basic_scan_channel_parameters));
900
901 if (len && ssid) {
902 params->params.ssid_len = len;
903 memcpy(params->params.ssid, ssid, len);
904 } else {
905 params->params.ssid_len = 0;
906 memset(params->params.ssid, 0, 32);
907 }
908
Kalle Valo80301cd2009-06-12 14:17:39 +0300909 ret = wl1251_build_probe_req(wl, ssid, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300910 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300911 wl1251_error("PROBE request template failed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300912 goto out;
913 }
914
Kalle Valoff258392009-06-12 14:14:19 +0300915 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
916 if (!trigger)
917 goto out;
918
919 trigger->timeout = 0;
920
Kalle Valo80301cd2009-06-12 14:17:39 +0300921 ret = wl1251_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
Kalle Valoff258392009-06-12 14:14:19 +0300922 sizeof(*trigger));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300923 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300924 wl1251_error("trigger scan to failed for hw scan");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300925 goto out;
926 }
927
Kalle Valo80301cd2009-06-12 14:17:39 +0300928 wl1251_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300929
930 wl->scanning = true;
931
Kalle Valo80301cd2009-06-12 14:17:39 +0300932 ret = wl1251_cmd_send(wl, CMD_SCAN, params, sizeof(*params));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300933 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300934 wl1251_error("SCAN failed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300935
Kalle Valo80301cd2009-06-12 14:17:39 +0300936 wl1251_spi_mem_read(wl, wl->cmd_box_addr, params, sizeof(*params));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300937
Kalle Valoff258392009-06-12 14:14:19 +0300938 if (params->header.status != CMD_STATUS_SUCCESS) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300939 wl1251_error("TEST command answer error: %d",
Kalle Valoff258392009-06-12 14:14:19 +0300940 params->header.status);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300941 wl->scanning = false;
942 ret = -EIO;
943 goto out;
944 }
945
946out:
947 kfree(params);
948 return ret;
949
950}
951
Kalle Valo80301cd2009-06-12 14:17:39 +0300952static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300953 struct cfg80211_scan_request *req)
954{
Kalle Valo80301cd2009-06-12 14:17:39 +0300955 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300956 int ret;
957 u8 *ssid = NULL;
958 size_t ssid_len = 0;
959
Kalle Valo80301cd2009-06-12 14:17:39 +0300960 wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300961
962 if (req->n_ssids) {
963 ssid = req->ssids[0].ssid;
964 ssid_len = req->ssids[0].ssid_len;
965 }
966
967 mutex_lock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +0300968
Kalle Valo80301cd2009-06-12 14:17:39 +0300969 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300970 if (ret < 0)
971 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300972
Kalle Valo80301cd2009-06-12 14:17:39 +0300973 ret = wl1251_hw_scan(hw->priv, ssid, ssid_len, 1, 0, 13, 3);
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300974
Kalle Valo80301cd2009-06-12 14:17:39 +0300975 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300976
977out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300978 mutex_unlock(&wl->mutex);
979
980 return ret;
981}
982
Kalle Valo80301cd2009-06-12 14:17:39 +0300983static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300984{
Kalle Valo80301cd2009-06-12 14:17:39 +0300985 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300986 int ret;
987
Kalle Valocee4fd22009-06-12 14:16:20 +0300988 mutex_lock(&wl->mutex);
989
Kalle Valo80301cd2009-06-12 14:17:39 +0300990 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300991 if (ret < 0)
992 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300993
Kalle Valo80301cd2009-06-12 14:17:39 +0300994 ret = wl1251_acx_rts_threshold(wl, (u16) value);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300995 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300996 wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300997
Kalle Valo80301cd2009-06-12 14:17:39 +0300998 wl1251_ps_elp_sleep(wl);
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300999
Kalle Valoc5483b72009-06-12 14:16:32 +03001000out:
Kalle Valocee4fd22009-06-12 14:16:20 +03001001 mutex_unlock(&wl->mutex);
1002
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001003 return ret;
1004}
1005
Kalle Valo80301cd2009-06-12 14:17:39 +03001006static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001007 struct ieee80211_vif *vif,
1008 struct ieee80211_bss_conf *bss_conf,
1009 u32 changed)
1010{
Kalle Valo80301cd2009-06-12 14:17:39 +03001011 enum wl1251_cmd_ps_mode mode;
1012 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001013 struct sk_buff *beacon;
1014 int ret;
1015
Kalle Valo80301cd2009-06-12 14:17:39 +03001016 wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001017
1018 mutex_lock(&wl->mutex);
1019
Kalle Valo80301cd2009-06-12 14:17:39 +03001020 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001021 if (ret < 0)
1022 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +03001023
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001024 if (changed & BSS_CHANGED_ASSOC) {
1025 if (bss_conf->assoc) {
1026 wl->aid = bss_conf->aid;
1027
Kalle Valo80301cd2009-06-12 14:17:39 +03001028 ret = wl1251_build_ps_poll(wl, wl->aid);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001029 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001030 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001031
Kalle Valo80301cd2009-06-12 14:17:39 +03001032 ret = wl1251_acx_aid(wl, wl->aid);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001033 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001034 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001035
1036 /* If we want to go in PSM but we're not there yet */
1037 if (wl->psm_requested && !wl->psm) {
1038 mode = STATION_POWER_SAVE_MODE;
Kalle Valo80301cd2009-06-12 14:17:39 +03001039 ret = wl1251_ps_set_mode(wl, mode);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001040 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001041 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001042 }
1043 }
1044 }
1045 if (changed & BSS_CHANGED_ERP_SLOT) {
1046 if (bss_conf->use_short_slot)
Kalle Valo80301cd2009-06-12 14:17:39 +03001047 ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001048 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001049 ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001050 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001051 wl1251_warning("Set slot time failed %d", ret);
Kalle Valoc5483b72009-06-12 14:16:32 +03001052 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001053 }
1054 }
1055
1056 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1057 if (bss_conf->use_short_preamble)
Kalle Valo80301cd2009-06-12 14:17:39 +03001058 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001059 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001060 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001061 }
1062
1063 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1064 if (bss_conf->use_cts_prot)
Kalle Valo80301cd2009-06-12 14:17:39 +03001065 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001066 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001067 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001068 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001069 wl1251_warning("Set ctsprotect failed %d", ret);
1070 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001071 }
1072 }
1073
1074 if (changed & BSS_CHANGED_BSSID) {
1075 memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
1076
Kalle Valo80301cd2009-06-12 14:17:39 +03001077 ret = wl1251_build_null_data(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001078 if (ret < 0)
1079 goto out;
1080
1081 if (wl->bss_type != BSS_TYPE_IBSS) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001082 ret = wl1251_cmd_join(wl, wl->bss_type, 5, 100, 1);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001083 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +03001084 goto out_sleep;
1085 wl1251_warning("Set ctsprotect failed %d", ret);
1086 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001087 }
1088 }
1089
1090 if (changed & BSS_CHANGED_BEACON) {
1091 beacon = ieee80211_beacon_get(hw, vif);
Kalle Valo80301cd2009-06-12 14:17:39 +03001092 ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001093 beacon->len);
1094
1095 if (ret < 0) {
1096 dev_kfree_skb(beacon);
1097 goto out;
1098 }
1099
Kalle Valo80301cd2009-06-12 14:17:39 +03001100 ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001101 beacon->len);
1102
1103 dev_kfree_skb(beacon);
1104
1105 if (ret < 0)
1106 goto out;
1107
Juuso Oikarinen77cc9e42009-06-12 14:16:52 +03001108 ret = wl->chip.op_cmd_join(wl, wl->bss_type, 1, 100, 0);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001109
1110 if (ret < 0)
1111 goto out;
1112 }
1113
Kalle Valoc5483b72009-06-12 14:16:32 +03001114out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +03001115 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001116
1117out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001118 mutex_unlock(&wl->mutex);
1119}
1120
1121
1122/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001123static struct ieee80211_rate wl1251_rates[] = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001124 { .bitrate = 10,
1125 .hw_value = 0x1,
1126 .hw_value_short = 0x1, },
1127 { .bitrate = 20,
1128 .hw_value = 0x2,
1129 .hw_value_short = 0x2,
1130 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1131 { .bitrate = 55,
1132 .hw_value = 0x4,
1133 .hw_value_short = 0x4,
1134 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1135 { .bitrate = 110,
1136 .hw_value = 0x20,
1137 .hw_value_short = 0x20,
1138 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1139 { .bitrate = 60,
1140 .hw_value = 0x8,
1141 .hw_value_short = 0x8, },
1142 { .bitrate = 90,
1143 .hw_value = 0x10,
1144 .hw_value_short = 0x10, },
1145 { .bitrate = 120,
1146 .hw_value = 0x40,
1147 .hw_value_short = 0x40, },
1148 { .bitrate = 180,
1149 .hw_value = 0x80,
1150 .hw_value_short = 0x80, },
1151 { .bitrate = 240,
1152 .hw_value = 0x200,
1153 .hw_value_short = 0x200, },
1154 { .bitrate = 360,
1155 .hw_value = 0x400,
1156 .hw_value_short = 0x400, },
1157 { .bitrate = 480,
1158 .hw_value = 0x800,
1159 .hw_value_short = 0x800, },
1160 { .bitrate = 540,
1161 .hw_value = 0x1000,
1162 .hw_value_short = 0x1000, },
1163};
1164
1165/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001166static struct ieee80211_channel wl1251_channels[] = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001167 { .hw_value = 1, .center_freq = 2412},
1168 { .hw_value = 2, .center_freq = 2417},
1169 { .hw_value = 3, .center_freq = 2422},
1170 { .hw_value = 4, .center_freq = 2427},
1171 { .hw_value = 5, .center_freq = 2432},
1172 { .hw_value = 6, .center_freq = 2437},
1173 { .hw_value = 7, .center_freq = 2442},
1174 { .hw_value = 8, .center_freq = 2447},
1175 { .hw_value = 9, .center_freq = 2452},
1176 { .hw_value = 10, .center_freq = 2457},
1177 { .hw_value = 11, .center_freq = 2462},
1178 { .hw_value = 12, .center_freq = 2467},
1179 { .hw_value = 13, .center_freq = 2472},
1180};
1181
1182/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001183static struct ieee80211_supported_band wl1251_band_2ghz = {
1184 .channels = wl1251_channels,
1185 .n_channels = ARRAY_SIZE(wl1251_channels),
1186 .bitrates = wl1251_rates,
1187 .n_bitrates = ARRAY_SIZE(wl1251_rates),
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001188};
1189
Kalle Valo80301cd2009-06-12 14:17:39 +03001190static const struct ieee80211_ops wl1251_ops = {
1191 .start = wl1251_op_start,
1192 .stop = wl1251_op_stop,
1193 .add_interface = wl1251_op_add_interface,
1194 .remove_interface = wl1251_op_remove_interface,
1195 .config = wl1251_op_config,
1196 .configure_filter = wl1251_op_configure_filter,
1197 .tx = wl1251_op_tx,
1198 .set_key = wl1251_op_set_key,
1199 .hw_scan = wl1251_op_hw_scan,
1200 .bss_info_changed = wl1251_op_bss_info_changed,
1201 .set_rts_threshold = wl1251_op_set_rts_threshold,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001202};
1203
Kalle Valo80301cd2009-06-12 14:17:39 +03001204static int wl1251_register_hw(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001205{
1206 int ret;
1207
1208 if (wl->mac80211_registered)
1209 return 0;
1210
1211 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1212
1213 ret = ieee80211_register_hw(wl->hw);
1214 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001215 wl1251_error("unable to register mac80211 hw: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001216 return ret;
1217 }
1218
1219 wl->mac80211_registered = true;
1220
Kalle Valo80301cd2009-06-12 14:17:39 +03001221 wl1251_notice("loaded");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001222
1223 return 0;
1224}
1225
Kalle Valo80301cd2009-06-12 14:17:39 +03001226static int wl1251_init_ieee80211(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001227{
1228 /* The tx descriptor buffer and the TKIP space */
1229 wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +03001230 + WL1251_TKIP_IV_SPACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001231
1232 /* unit us */
1233 /* FIXME: find a proper value */
1234 wl->hw->channel_change_time = 10000;
1235
1236 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
1237 IEEE80211_HW_NOISE_DBM;
1238
1239 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1240 wl->hw->wiphy->max_scan_ssids = 1;
Kalle Valo80301cd2009-06-12 14:17:39 +03001241 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1251_band_2ghz;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001242
1243 SET_IEEE80211_DEV(wl->hw, &wl->spi->dev);
1244
1245 return 0;
1246}
1247
Kalle Valo80301cd2009-06-12 14:17:39 +03001248#define WL1251_DEFAULT_CHANNEL 1
1249static int __devinit wl1251_probe(struct spi_device *spi)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001250{
1251 struct wl12xx_platform_data *pdata;
1252 struct ieee80211_hw *hw;
Kalle Valo80301cd2009-06-12 14:17:39 +03001253 struct wl1251 *wl;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001254 int ret, i;
1255 static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1256
1257 pdata = spi->dev.platform_data;
1258 if (!pdata) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001259 wl1251_error("no platform data");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001260 return -ENODEV;
1261 }
1262
Kalle Valo80301cd2009-06-12 14:17:39 +03001263 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001264 if (!hw) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001265 wl1251_error("could not alloc ieee80211_hw");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001266 return -ENOMEM;
1267 }
1268
1269 wl = hw->priv;
1270 memset(wl, 0, sizeof(*wl));
1271
1272 wl->hw = hw;
1273 dev_set_drvdata(&spi->dev, wl);
1274 wl->spi = spi;
1275
1276 wl->data_in_count = 0;
1277
1278 skb_queue_head_init(&wl->tx_queue);
1279
Kalle Valo80301cd2009-06-12 14:17:39 +03001280 INIT_WORK(&wl->filter_work, wl1251_filter_work);
1281 wl->channel = WL1251_DEFAULT_CHANNEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001282 wl->scanning = false;
1283 wl->default_key = 0;
1284 wl->listen_int = 1;
1285 wl->rx_counter = 0;
1286 wl->rx_handled = 0;
1287 wl->rx_current_buffer = 0;
1288 wl->rx_last_id = 0;
Kalle Valo80301cd2009-06-12 14:17:39 +03001289 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
1290 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001291 wl->elp = false;
1292 wl->psm = 0;
1293 wl->psm_requested = false;
1294 wl->tx_queue_stopped = false;
Kalle Valo80301cd2009-06-12 14:17:39 +03001295 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001296
1297 /* We use the default power on sleep time until we know which chip
1298 * we're using */
Kalle Valo80301cd2009-06-12 14:17:39 +03001299 wl->chip.power_on_sleep = WL1251_DEFAULT_POWER_ON_SLEEP;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001300
1301 for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
1302 wl->tx_frames[i] = NULL;
1303
1304 wl->next_tx_complete = 0;
1305
1306 /*
1307 * In case our MAC address is not correctly set,
1308 * we use a random but Nokia MAC.
1309 */
1310 memcpy(wl->mac_addr, nokia_oui, 3);
1311 get_random_bytes(wl->mac_addr + 3, 3);
1312
Kalle Valo80301cd2009-06-12 14:17:39 +03001313 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001314 mutex_init(&wl->mutex);
1315
1316 wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
1317 wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
1318
Kalle Valo47212132009-06-12 14:15:08 +03001319 wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1320 if (!wl->rx_descriptor) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001321 wl1251_error("could not allocate memory for rx descriptor");
Kalle Valo47212132009-06-12 14:15:08 +03001322 ret = -ENOMEM;
1323 goto out_free;
1324 }
1325
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001326 /* This is the only SPI value that we need to set here, the rest
1327 * comes from the board-peripherals file */
1328 spi->bits_per_word = 32;
1329
1330 ret = spi_setup(spi);
1331 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001332 wl1251_error("spi_setup failed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001333 goto out_free;
1334 }
1335
1336 wl->set_power = pdata->set_power;
1337 if (!wl->set_power) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001338 wl1251_error("set power function missing in platform data");
Kalle Valoc4f5c852009-06-12 14:14:34 +03001339 ret = -ENODEV;
1340 goto out_free;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001341 }
1342
1343 wl->irq = spi->irq;
1344 if (wl->irq < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001345 wl1251_error("irq missing in platform data");
Kalle Valoc4f5c852009-06-12 14:14:34 +03001346 ret = -ENODEV;
1347 goto out_free;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001348 }
1349
Kalle Valo80301cd2009-06-12 14:17:39 +03001350 ret = request_irq(wl->irq, wl1251_irq, 0, DRIVER_NAME, wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001351 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001352 wl1251_error("request_irq() failed: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001353 goto out_free;
1354 }
1355
1356 set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
1357
1358 disable_irq(wl->irq);
1359
Kalle Valo80301cd2009-06-12 14:17:39 +03001360 ret = wl1251_init_ieee80211(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001361 if (ret)
1362 goto out_irq;
1363
Kalle Valo80301cd2009-06-12 14:17:39 +03001364 ret = wl1251_register_hw(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001365 if (ret)
1366 goto out_irq;
1367
Kalle Valo80301cd2009-06-12 14:17:39 +03001368 wl1251_debugfs_init(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001369
Kalle Valo80301cd2009-06-12 14:17:39 +03001370 wl1251_notice("initialized");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001371
1372 return 0;
1373
1374 out_irq:
1375 free_irq(wl->irq, wl);
1376
1377 out_free:
Kalle Valo47212132009-06-12 14:15:08 +03001378 kfree(wl->rx_descriptor);
1379 wl->rx_descriptor = NULL;
1380
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001381 ieee80211_free_hw(hw);
1382
1383 return ret;
1384}
1385
Kalle Valo80301cd2009-06-12 14:17:39 +03001386static int __devexit wl1251_remove(struct spi_device *spi)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001387{
Kalle Valo80301cd2009-06-12 14:17:39 +03001388 struct wl1251 *wl = dev_get_drvdata(&spi->dev);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001389
1390 ieee80211_unregister_hw(wl->hw);
1391
Kalle Valo80301cd2009-06-12 14:17:39 +03001392 wl1251_debugfs_exit(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001393
1394 free_irq(wl->irq, wl);
1395 kfree(wl->target_mem_map);
1396 kfree(wl->data_path);
1397 kfree(wl->fw);
1398 wl->fw = NULL;
1399 kfree(wl->nvs);
1400 wl->nvs = NULL;
Kalle Valo47212132009-06-12 14:15:08 +03001401
1402 kfree(wl->rx_descriptor);
1403 wl->rx_descriptor = NULL;
1404
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001405 ieee80211_free_hw(wl->hw);
1406
1407 return 0;
1408}
1409
1410
Kalle Valo80301cd2009-06-12 14:17:39 +03001411static struct spi_driver wl1251_spi_driver = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001412 .driver = {
Kalle Valo80301cd2009-06-12 14:17:39 +03001413 /* FIXME: use wl12xx name to not break the user space */
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001414 .name = "wl12xx",
1415 .bus = &spi_bus_type,
1416 .owner = THIS_MODULE,
1417 },
1418
Kalle Valo80301cd2009-06-12 14:17:39 +03001419 .probe = wl1251_probe,
1420 .remove = __devexit_p(wl1251_remove),
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001421};
1422
Kalle Valo80301cd2009-06-12 14:17:39 +03001423static int __init wl1251_init(void)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001424{
1425 int ret;
1426
Kalle Valo80301cd2009-06-12 14:17:39 +03001427 ret = spi_register_driver(&wl1251_spi_driver);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001428 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001429 wl1251_error("failed to register spi driver: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001430 goto out;
1431 }
1432
1433out:
1434 return ret;
1435}
1436
Kalle Valo80301cd2009-06-12 14:17:39 +03001437static void __exit wl1251_exit(void)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001438{
Kalle Valo80301cd2009-06-12 14:17:39 +03001439 spi_unregister_driver(&wl1251_spi_driver);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001440
Kalle Valo80301cd2009-06-12 14:17:39 +03001441 wl1251_notice("unloaded");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001442}
1443
Kalle Valo80301cd2009-06-12 14:17:39 +03001444module_init(wl1251_init);
1445module_exit(wl1251_exit);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001446
1447MODULE_LICENSE("GPL");
1448MODULE_AUTHOR("Kalle Valo <Kalle.Valo@nokia.com>, "
1449 "Luciano Coelho <luciano.coelho@nokia.com>");