blob: 953cdb4fd38f6b264894e9ec0bc64ccf95f0c2bd [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"
Bob Copeland0764de62009-08-07 13:32:56 +030038#include "wl1251_io.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030039#include "wl1251_spi.h"
40#include "wl1251_event.h"
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +030041#include "wl1251_tx.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030042#include "wl1251_rx.h"
43#include "wl1251_ps.h"
44#include "wl1251_init.h"
45#include "wl1251_debugfs.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030046
Kalle Valo80301cd2009-06-12 14:17:39 +030047static void wl1251_disable_interrupts(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030048{
49 disable_irq(wl->irq);
50}
51
Kalle Valo80301cd2009-06-12 14:17:39 +030052static void wl1251_power_off(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030053{
54 wl->set_power(false);
55}
56
Kalle Valo80301cd2009-06-12 14:17:39 +030057static void wl1251_power_on(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030058{
59 wl->set_power(true);
60}
61
Kalle Valo80301cd2009-06-12 14:17:39 +030062static irqreturn_t wl1251_irq(int irq, void *cookie)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030063{
Kalle Valo80301cd2009-06-12 14:17:39 +030064 struct wl1251 *wl;
Kalle Valo2f01a1f2009-04-29 23:33:31 +030065
Kalle Valo80301cd2009-06-12 14:17:39 +030066 wl1251_debug(DEBUG_IRQ, "IRQ");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030067
68 wl = cookie;
69
70 schedule_work(&wl->irq_work);
71
72 return IRQ_HANDLED;
73}
74
Kalle Valo80301cd2009-06-12 14:17:39 +030075static int wl1251_fetch_firmware(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030076{
77 const struct firmware *fw;
Bob Copeland6c766f42009-08-07 13:33:04 +030078 struct device *dev = wiphy_dev(wl->hw->wiphy);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030079 int ret;
80
Bob Copeland6c766f42009-08-07 13:33:04 +030081 ret = request_firmware(&fw, wl->chip.fw_filename, dev);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030082
83 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +030084 wl1251_error("could not get firmware: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030085 return ret;
86 }
87
88 if (fw->size % 4) {
Kalle Valo80301cd2009-06-12 14:17:39 +030089 wl1251_error("firmware size is not multiple of 32 bits: %zu",
Kalle Valo2f01a1f2009-04-29 23:33:31 +030090 fw->size);
91 ret = -EILSEQ;
92 goto out;
93 }
94
95 wl->fw_len = fw->size;
96 wl->fw = kmalloc(wl->fw_len, GFP_KERNEL);
97
98 if (!wl->fw) {
Kalle Valo80301cd2009-06-12 14:17:39 +030099 wl1251_error("could not allocate memory for the firmware");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300100 ret = -ENOMEM;
101 goto out;
102 }
103
104 memcpy(wl->fw, fw->data, wl->fw_len);
105
106 ret = 0;
107
108out:
109 release_firmware(fw);
110
111 return ret;
112}
113
Kalle Valo80301cd2009-06-12 14:17:39 +0300114static int wl1251_fetch_nvs(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300115{
116 const struct firmware *fw;
Bob Copeland6c766f42009-08-07 13:33:04 +0300117 struct device *dev = wiphy_dev(wl->hw->wiphy);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300118 int ret;
119
Bob Copeland6c766f42009-08-07 13:33:04 +0300120 ret = request_firmware(&fw, wl->chip.nvs_filename, dev);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300121
122 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300123 wl1251_error("could not get nvs file: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300124 return ret;
125 }
126
127 if (fw->size % 4) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300128 wl1251_error("nvs size is not multiple of 32 bits: %zu",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300129 fw->size);
130 ret = -EILSEQ;
131 goto out;
132 }
133
134 wl->nvs_len = fw->size;
135 wl->nvs = kmalloc(wl->nvs_len, GFP_KERNEL);
136
137 if (!wl->nvs) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300138 wl1251_error("could not allocate memory for the nvs file");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300139 ret = -ENOMEM;
140 goto out;
141 }
142
143 memcpy(wl->nvs, fw->data, wl->nvs_len);
144
145 ret = 0;
146
147out:
148 release_firmware(fw);
149
150 return ret;
151}
152
Kalle Valo80301cd2009-06-12 14:17:39 +0300153static void wl1251_fw_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300154{
155 u32 elp_reg;
156
157 elp_reg = ELPCTRL_WAKE_UP;
Kalle Valo80301cd2009-06-12 14:17:39 +0300158 wl1251_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
159 elp_reg = wl1251_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300160
Kalle Valo05fac682009-06-12 14:17:47 +0300161 if (!(elp_reg & ELPCTRL_WLAN_READY))
Kalle Valo80301cd2009-06-12 14:17:39 +0300162 wl1251_warning("WLAN not ready");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300163}
164
Kalle Valo80301cd2009-06-12 14:17:39 +0300165static int wl1251_chip_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300166{
167 int ret = 0;
168
Kalle Valo80301cd2009-06-12 14:17:39 +0300169 wl1251_power_on(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300170 msleep(wl->chip.power_on_sleep);
Kalle Valo80301cd2009-06-12 14:17:39 +0300171 wl1251_spi_reset(wl);
172 wl1251_spi_init(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300173
174 /* We don't need a real memory partition here, because we only want
175 * to use the registers at this point. */
Kalle Valo80301cd2009-06-12 14:17:39 +0300176 wl1251_set_partition(wl,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300177 0x00000000,
178 0x00000000,
179 REGISTERS_BASE,
180 REGISTERS_DOWN_SIZE);
181
182 /* ELP module wake up */
Kalle Valo80301cd2009-06-12 14:17:39 +0300183 wl1251_fw_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300184
185 /* whal_FwCtrl_BootSm() */
186
187 /* 0. read chip id from CHIP_ID */
Kalle Valo80301cd2009-06-12 14:17:39 +0300188 wl->chip.id = wl1251_reg_read32(wl, CHIP_ID_B);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300189
190 /* 1. check if chip id is valid */
191
192 switch (wl->chip.id) {
193 case CHIP_ID_1251_PG12:
Kalle Valo80301cd2009-06-12 14:17:39 +0300194 wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG12)",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300195 wl->chip.id);
196
197 wl1251_setup(wl);
198
199 break;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300200 case CHIP_ID_1251_PG10:
201 case CHIP_ID_1251_PG11:
202 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300203 wl1251_error("unsupported chip id: 0x%x", wl->chip.id);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300204 ret = -ENODEV;
205 goto out;
206 }
207
208 if (wl->fw == NULL) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300209 ret = wl1251_fetch_firmware(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300210 if (ret < 0)
211 goto out;
212 }
213
214 /* No NVS from netlink, try to get it from the filesystem */
215 if (wl->nvs == NULL) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300216 ret = wl1251_fetch_nvs(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300217 if (ret < 0)
218 goto out;
219 }
220
221out:
222 return ret;
223}
224
Kalle Valo80301cd2009-06-12 14:17:39 +0300225static void wl1251_filter_work(struct work_struct *work)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300226{
Kalle Valo80301cd2009-06-12 14:17:39 +0300227 struct wl1251 *wl =
228 container_of(work, struct wl1251, filter_work);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300229 int ret;
230
231 mutex_lock(&wl->mutex);
232
Kalle Valo80301cd2009-06-12 14:17:39 +0300233 if (wl->state == WL1251_STATE_OFF)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300234 goto out;
235
Kalle Valo80301cd2009-06-12 14:17:39 +0300236 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300237 if (ret < 0)
238 goto out;
239
Juuso Oikarinen77cc9e42009-06-12 14:16:52 +0300240 /* FIXME: replace the magic numbers with proper definitions */
241 ret = wl->chip.op_cmd_join(wl, wl->bss_type, 1, 100, 0);
Kalle Valoc5483b72009-06-12 14:16:32 +0300242 if (ret < 0)
243 goto out_sleep;
244
245out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300246 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300247
248out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300249 mutex_unlock(&wl->mutex);
250}
251
Kalle Valo80301cd2009-06-12 14:17:39 +0300252static int wl1251_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300253{
Kalle Valo80301cd2009-06-12 14:17:39 +0300254 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300255
256 skb_queue_tail(&wl->tx_queue, skb);
257
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +0300258 /*
259 * The chip specific setup must run before the first TX packet -
260 * before that, the tx_work will not be initialized!
261 */
262
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300263 schedule_work(&wl->tx_work);
264
265 /*
266 * The workqueue is slow to process the tx_queue and we need stop
267 * the queue here, otherwise the queue will get too long.
268 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300269 if (skb_queue_len(&wl->tx_queue) >= WL1251_TX_QUEUE_MAX_LENGTH) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300270 ieee80211_stop_queues(wl->hw);
271
272 /*
273 * FIXME: this is racy, the variable is not properly
274 * protected. Maybe fix this by removing the stupid
275 * variable altogether and checking the real queue state?
276 */
277 wl->tx_queue_stopped = true;
278 }
279
280 return NETDEV_TX_OK;
281}
282
Kalle Valo80301cd2009-06-12 14:17:39 +0300283static int wl1251_op_start(struct ieee80211_hw *hw)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300284{
Kalle Valo80301cd2009-06-12 14:17:39 +0300285 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300286 int ret = 0;
287
Kalle Valo80301cd2009-06-12 14:17:39 +0300288 wl1251_debug(DEBUG_MAC80211, "mac80211 start");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300289
290 mutex_lock(&wl->mutex);
291
Kalle Valo80301cd2009-06-12 14:17:39 +0300292 if (wl->state != WL1251_STATE_OFF) {
293 wl1251_error("cannot start because not in off state: %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300294 wl->state);
295 ret = -EBUSY;
296 goto out;
297 }
298
Kalle Valo80301cd2009-06-12 14:17:39 +0300299 ret = wl1251_chip_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300300 if (ret < 0)
Jiri Slabyfe643412009-07-14 22:37:13 +0200301 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300302
303 ret = wl->chip.op_boot(wl);
304 if (ret < 0)
305 goto out;
306
307 ret = wl->chip.op_hw_init(wl);
308 if (ret < 0)
309 goto out;
310
Kalle Valo80301cd2009-06-12 14:17:39 +0300311 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300312 if (ret < 0)
313 goto out;
314
Kalle Valo80301cd2009-06-12 14:17:39 +0300315 wl->state = WL1251_STATE_ON;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300316
Kalle Valo80301cd2009-06-12 14:17:39 +0300317 wl1251_info("firmware booted (%s)", wl->chip.fw_ver);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300318
319out:
320 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300321 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300322
323 mutex_unlock(&wl->mutex);
324
325 return ret;
326}
327
Kalle Valo80301cd2009-06-12 14:17:39 +0300328static void wl1251_op_stop(struct ieee80211_hw *hw)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300329{
Kalle Valo80301cd2009-06-12 14:17:39 +0300330 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300331
Kalle Valo80301cd2009-06-12 14:17:39 +0300332 wl1251_info("down");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300333
Kalle Valo80301cd2009-06-12 14:17:39 +0300334 wl1251_debug(DEBUG_MAC80211, "mac80211 stop");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300335
336 mutex_lock(&wl->mutex);
337
Kalle Valo80301cd2009-06-12 14:17:39 +0300338 WARN_ON(wl->state != WL1251_STATE_ON);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300339
340 if (wl->scanning) {
341 mutex_unlock(&wl->mutex);
342 ieee80211_scan_completed(wl->hw, true);
343 mutex_lock(&wl->mutex);
344 wl->scanning = false;
345 }
346
Kalle Valo80301cd2009-06-12 14:17:39 +0300347 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300348
Kalle Valo80301cd2009-06-12 14:17:39 +0300349 wl1251_disable_interrupts(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300350
351 mutex_unlock(&wl->mutex);
352
353 cancel_work_sync(&wl->irq_work);
354 cancel_work_sync(&wl->tx_work);
355 cancel_work_sync(&wl->filter_work);
356
357 mutex_lock(&wl->mutex);
358
359 /* let's notify MAC80211 about the remaining pending TX frames */
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +0300360 wl->chip.op_tx_flush(wl);
Kalle Valo80301cd2009-06-12 14:17:39 +0300361 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300362
363 memset(wl->bssid, 0, ETH_ALEN);
364 wl->listen_int = 1;
365 wl->bss_type = MAX_BSS_TYPE;
366
367 wl->data_in_count = 0;
368 wl->rx_counter = 0;
369 wl->rx_handled = 0;
370 wl->rx_current_buffer = 0;
371 wl->rx_last_id = 0;
372 wl->next_tx_complete = 0;
373 wl->elp = false;
374 wl->psm = 0;
375 wl->tx_queue_stopped = false;
Kalle Valo80301cd2009-06-12 14:17:39 +0300376 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300377
Kalle Valo80301cd2009-06-12 14:17:39 +0300378 wl1251_debugfs_reset(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300379
380 mutex_unlock(&wl->mutex);
381}
382
Kalle Valo80301cd2009-06-12 14:17:39 +0300383static int wl1251_op_add_interface(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300384 struct ieee80211_if_init_conf *conf)
385{
Kalle Valo80301cd2009-06-12 14:17:39 +0300386 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300387 int ret = 0;
388
Johannes Berge91d8332009-07-15 17:21:41 +0200389 wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
390 conf->type, conf->mac_addr);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300391
392 mutex_lock(&wl->mutex);
393
394 switch (conf->type) {
395 case NL80211_IFTYPE_STATION:
396 wl->bss_type = BSS_TYPE_STA_BSS;
397 break;
398 case NL80211_IFTYPE_ADHOC:
399 wl->bss_type = BSS_TYPE_IBSS;
400 break;
401 default:
402 ret = -EOPNOTSUPP;
403 goto out;
404 }
405
406 if (memcmp(wl->mac_addr, conf->mac_addr, ETH_ALEN)) {
407 memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
408 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
Kalle Valo80301cd2009-06-12 14:17:39 +0300409 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300410 if (ret < 0)
411 goto out;
412 }
413
414out:
415 mutex_unlock(&wl->mutex);
416 return ret;
417}
418
Kalle Valo80301cd2009-06-12 14:17:39 +0300419static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300420 struct ieee80211_if_init_conf *conf)
421{
Kalle Valo80301cd2009-06-12 14:17:39 +0300422 wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300423}
424
Kalle Valo80301cd2009-06-12 14:17:39 +0300425static int wl1251_build_null_data(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300426{
427 struct wl12xx_null_data_template template;
428
429 if (!is_zero_ether_addr(wl->bssid)) {
430 memcpy(template.header.da, wl->bssid, ETH_ALEN);
431 memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
432 } else {
433 memset(template.header.da, 0xff, ETH_ALEN);
434 memset(template.header.bssid, 0xff, ETH_ALEN);
435 }
436
437 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
438 template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
439 IEEE80211_STYPE_NULLFUNC);
440
Kalle Valo80301cd2009-06-12 14:17:39 +0300441 return wl1251_cmd_template_set(wl, CMD_NULL_DATA, &template,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300442 sizeof(template));
443
444}
445
Kalle Valo80301cd2009-06-12 14:17:39 +0300446static int wl1251_build_ps_poll(struct wl1251 *wl, u16 aid)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300447{
448 struct wl12xx_ps_poll_template template;
449
450 memcpy(template.bssid, wl->bssid, ETH_ALEN);
451 memcpy(template.ta, wl->mac_addr, ETH_ALEN);
452 template.aid = aid;
453 template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
454
Kalle Valo80301cd2009-06-12 14:17:39 +0300455 return wl1251_cmd_template_set(wl, CMD_PS_POLL, &template,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300456 sizeof(template));
457
458}
459
Kalle Valo80301cd2009-06-12 14:17:39 +0300460static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300461{
Kalle Valo80301cd2009-06-12 14:17:39 +0300462 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300463 struct ieee80211_conf *conf = &hw->conf;
464 int channel, ret = 0;
465
466 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
467
Kalle Valo80301cd2009-06-12 14:17:39 +0300468 wl1251_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300469 channel,
470 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
471 conf->power_level);
472
473 mutex_lock(&wl->mutex);
474
Kalle Valo80301cd2009-06-12 14:17:39 +0300475 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300476 if (ret < 0)
477 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300478
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300479 if (channel != wl->channel) {
480 /* FIXME: use beacon interval provided by mac80211 */
Juuso Oikarinen77cc9e42009-06-12 14:16:52 +0300481 ret = wl->chip.op_cmd_join(wl, wl->bss_type, 1, 100, 0);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300482 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300483 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300484
485 wl->channel = channel;
486 }
487
Kalle Valo80301cd2009-06-12 14:17:39 +0300488 ret = wl1251_build_null_data(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300489 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300490 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300491
492 if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
Luciano Coelho47af3fe2009-06-12 14:17:53 +0300493 wl1251_debug(DEBUG_PSM, "psm enabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300494
495 wl->psm_requested = true;
496
497 /*
498 * We enter PSM only if we're already associated.
499 * If we're not, we'll enter it when joining an SSID,
500 * through the bss_info_changed() hook.
501 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300502 ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300503 } else if (!(conf->flags & IEEE80211_CONF_PS) &&
504 wl->psm_requested) {
Luciano Coelho47af3fe2009-06-12 14:17:53 +0300505 wl1251_debug(DEBUG_PSM, "psm disabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300506
507 wl->psm_requested = false;
508
509 if (wl->psm)
Kalle Valo80301cd2009-06-12 14:17:39 +0300510 ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300511 }
512
513 if (conf->power_level != wl->power_level) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300514 ret = wl1251_acx_tx_power(wl, conf->power_level);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300515 if (ret < 0)
516 goto out;
517
518 wl->power_level = conf->power_level;
519 }
520
Kalle Valoc5483b72009-06-12 14:16:32 +0300521out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300522 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300523
524out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300525 mutex_unlock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +0300526
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300527 return ret;
528}
529
Kalle Valo80301cd2009-06-12 14:17:39 +0300530#define WL1251_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300531 FIF_ALLMULTI | \
532 FIF_FCSFAIL | \
533 FIF_BCN_PRBRESP_PROMISC | \
534 FIF_CONTROL | \
535 FIF_OTHER_BSS)
536
Kalle Valo80301cd2009-06-12 14:17:39 +0300537static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300538 unsigned int changed,
539 unsigned int *total,
540 int mc_count,
541 struct dev_addr_list *mc_list)
542{
Kalle Valo80301cd2009-06-12 14:17:39 +0300543 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300544
Kalle Valo80301cd2009-06-12 14:17:39 +0300545 wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300546
Kalle Valo80301cd2009-06-12 14:17:39 +0300547 *total &= WL1251_SUPPORTED_FILTERS;
548 changed &= WL1251_SUPPORTED_FILTERS;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300549
550 if (changed == 0)
551 /* no filters which we support changed */
552 return;
553
554 /* FIXME: wl->rx_config and wl->rx_filter are not protected */
555
Kalle Valo80301cd2009-06-12 14:17:39 +0300556 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
557 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300558
559 if (*total & FIF_PROMISC_IN_BSS) {
560 wl->rx_config |= CFG_BSSID_FILTER_EN;
561 wl->rx_config |= CFG_RX_ALL_GOOD;
562 }
563 if (*total & FIF_ALLMULTI)
564 /*
565 * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
566 * all multicast frames
567 */
568 wl->rx_config &= ~CFG_MC_FILTER_EN;
569 if (*total & FIF_FCSFAIL)
570 wl->rx_filter |= CFG_RX_FCS_ERROR;
571 if (*total & FIF_BCN_PRBRESP_PROMISC) {
572 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
573 wl->rx_config &= ~CFG_SSID_FILTER_EN;
574 }
575 if (*total & FIF_CONTROL)
576 wl->rx_filter |= CFG_RX_CTL_EN;
577 if (*total & FIF_OTHER_BSS)
578 wl->rx_filter &= ~CFG_BSSID_FILTER_EN;
579
580 /*
581 * FIXME: workqueues need to be properly cancelled on stop(), for
582 * now let's just disable changing the filter settings. They will
583 * be updated any on config().
584 */
585 /* schedule_work(&wl->filter_work); */
586}
587
588/* HW encryption */
Kalle Valo80301cd2009-06-12 14:17:39 +0300589static int wl1251_set_key_type(struct wl1251 *wl,
590 struct wl1251_cmd_set_keys *key,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300591 enum set_key_cmd cmd,
592 struct ieee80211_key_conf *mac80211_key,
593 const u8 *addr)
594{
595 switch (mac80211_key->alg) {
596 case ALG_WEP:
597 if (is_broadcast_ether_addr(addr))
598 key->key_type = KEY_WEP_DEFAULT;
599 else
600 key->key_type = KEY_WEP_ADDR;
601
602 mac80211_key->hw_key_idx = mac80211_key->keyidx;
603 break;
604 case ALG_TKIP:
605 if (is_broadcast_ether_addr(addr))
606 key->key_type = KEY_TKIP_MIC_GROUP;
607 else
608 key->key_type = KEY_TKIP_MIC_PAIRWISE;
609
610 mac80211_key->hw_key_idx = mac80211_key->keyidx;
611 break;
612 case ALG_CCMP:
613 if (is_broadcast_ether_addr(addr))
614 key->key_type = KEY_AES_GROUP;
615 else
616 key->key_type = KEY_AES_PAIRWISE;
617 mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
618 break;
619 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300620 wl1251_error("Unknown key algo 0x%x", mac80211_key->alg);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300621 return -EOPNOTSUPP;
622 }
623
624 return 0;
625}
626
Kalle Valo80301cd2009-06-12 14:17:39 +0300627static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300628 struct ieee80211_vif *vif,
629 struct ieee80211_sta *sta,
630 struct ieee80211_key_conf *key)
631{
Kalle Valo80301cd2009-06-12 14:17:39 +0300632 struct wl1251 *wl = hw->priv;
633 struct wl1251_cmd_set_keys *wl_cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300634 const u8 *addr;
635 int ret;
636
637 static const u8 bcast_addr[ETH_ALEN] =
638 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
639
Kalle Valo80301cd2009-06-12 14:17:39 +0300640 wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300641
Kalle Valoff258392009-06-12 14:14:19 +0300642 wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
643 if (!wl_cmd) {
644 ret = -ENOMEM;
645 goto out;
646 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300647
648 addr = sta ? sta->addr : bcast_addr;
649
Kalle Valo80301cd2009-06-12 14:17:39 +0300650 wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
651 wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
652 wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300653 key->alg, key->keyidx, key->keylen, key->flags);
Kalle Valo80301cd2009-06-12 14:17:39 +0300654 wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300655
Kalle Valoff258392009-06-12 14:14:19 +0300656 if (is_zero_ether_addr(addr)) {
657 /* We dont support TX only encryption */
658 ret = -EOPNOTSUPP;
659 goto out;
660 }
661
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300662 mutex_lock(&wl->mutex);
663
Kalle Valo80301cd2009-06-12 14:17:39 +0300664 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300665 if (ret < 0)
666 goto out_unlock;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300667
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300668 switch (cmd) {
669 case SET_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300670 wl_cmd->key_action = KEY_ADD_OR_REPLACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300671 break;
672 case DISABLE_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300673 wl_cmd->key_action = KEY_REMOVE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300674 break;
675 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300676 wl1251_error("Unsupported key cmd 0x%x", cmd);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300677 break;
678 }
679
Kalle Valo80301cd2009-06-12 14:17:39 +0300680 ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300681 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300682 wl1251_error("Set KEY type failed");
Kalle Valoc5483b72009-06-12 14:16:32 +0300683 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300684 }
685
Kalle Valoff258392009-06-12 14:14:19 +0300686 if (wl_cmd->key_type != KEY_WEP_DEFAULT)
687 memcpy(wl_cmd->addr, addr, ETH_ALEN);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300688
Kalle Valoff258392009-06-12 14:14:19 +0300689 if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
690 (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300691 /*
692 * We get the key in the following form:
693 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
694 * but the target is expecting:
695 * TKIP - RX MIC - TX MIC
696 */
Kalle Valoff258392009-06-12 14:14:19 +0300697 memcpy(wl_cmd->key, key->key, 16);
698 memcpy(wl_cmd->key + 16, key->key + 24, 8);
699 memcpy(wl_cmd->key + 24, key->key + 16, 8);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300700
701 } else {
Kalle Valoff258392009-06-12 14:14:19 +0300702 memcpy(wl_cmd->key, key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300703 }
Kalle Valoff258392009-06-12 14:14:19 +0300704 wl_cmd->key_size = key->keylen;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300705
Kalle Valoff258392009-06-12 14:14:19 +0300706 wl_cmd->id = key->keyidx;
707 wl_cmd->ssid_profile = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300708
Kalle Valo80301cd2009-06-12 14:17:39 +0300709 wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300710
Kalle Valo80301cd2009-06-12 14:17:39 +0300711 ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
Kalle Valoff258392009-06-12 14:14:19 +0300712 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300713 wl1251_warning("could not set keys");
Kalle Valoc5483b72009-06-12 14:16:32 +0300714 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300715 }
716
Kalle Valoc5483b72009-06-12 14:16:32 +0300717out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300718 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300719
720out_unlock:
Kalle Valoff258392009-06-12 14:14:19 +0300721 mutex_unlock(&wl->mutex);
722
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300723out:
Kalle Valoff258392009-06-12 14:14:19 +0300724 kfree(wl_cmd);
725
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300726 return ret;
727}
728
Kalle Valo80301cd2009-06-12 14:17:39 +0300729static int wl1251_build_basic_rates(char *rates)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300730{
731 u8 index = 0;
732
733 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
734 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
735 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
736 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
737
738 return index;
739}
740
Kalle Valo80301cd2009-06-12 14:17:39 +0300741static int wl1251_build_extended_rates(char *rates)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300742{
743 u8 index = 0;
744
745 rates[index++] = IEEE80211_OFDM_RATE_6MB;
746 rates[index++] = IEEE80211_OFDM_RATE_9MB;
747 rates[index++] = IEEE80211_OFDM_RATE_12MB;
748 rates[index++] = IEEE80211_OFDM_RATE_18MB;
749 rates[index++] = IEEE80211_OFDM_RATE_24MB;
750 rates[index++] = IEEE80211_OFDM_RATE_36MB;
751 rates[index++] = IEEE80211_OFDM_RATE_48MB;
752 rates[index++] = IEEE80211_OFDM_RATE_54MB;
753
754 return index;
755}
756
757
Kalle Valo80301cd2009-06-12 14:17:39 +0300758static int wl1251_build_probe_req(struct wl1251 *wl, u8 *ssid, size_t ssid_len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300759{
760 struct wl12xx_probe_req_template template;
761 struct wl12xx_ie_rates *rates;
762 char *ptr;
763 u16 size;
764
765 ptr = (char *)&template;
766 size = sizeof(struct ieee80211_header);
767
768 memset(template.header.da, 0xff, ETH_ALEN);
769 memset(template.header.bssid, 0xff, ETH_ALEN);
770 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
771 template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
772
773 /* IEs */
774 /* SSID */
775 template.ssid.header.id = WLAN_EID_SSID;
776 template.ssid.header.len = ssid_len;
777 if (ssid_len && ssid)
778 memcpy(template.ssid.ssid, ssid, ssid_len);
779 size += sizeof(struct wl12xx_ie_header) + ssid_len;
780 ptr += size;
781
782 /* Basic Rates */
783 rates = (struct wl12xx_ie_rates *)ptr;
784 rates->header.id = WLAN_EID_SUPP_RATES;
Kalle Valo80301cd2009-06-12 14:17:39 +0300785 rates->header.len = wl1251_build_basic_rates(rates->rates);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300786 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
787 ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
788
789 /* Extended rates */
790 rates = (struct wl12xx_ie_rates *)ptr;
791 rates->header.id = WLAN_EID_EXT_SUPP_RATES;
Kalle Valo80301cd2009-06-12 14:17:39 +0300792 rates->header.len = wl1251_build_extended_rates(rates->rates);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300793 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
794
Kalle Valo80301cd2009-06-12 14:17:39 +0300795 wl1251_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300796
Kalle Valo80301cd2009-06-12 14:17:39 +0300797 return wl1251_cmd_template_set(wl, CMD_PROBE_REQ, &template,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300798 size);
799}
800
Kalle Valo80301cd2009-06-12 14:17:39 +0300801static int wl1251_hw_scan(struct wl1251 *wl, u8 *ssid, size_t len,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300802 u8 active_scan, u8 high_prio, u8 num_channels,
803 u8 probe_requests)
804{
Kalle Valo80301cd2009-06-12 14:17:39 +0300805 struct wl1251_cmd_trigger_scan_to *trigger = NULL;
Kalle Valoff258392009-06-12 14:14:19 +0300806 struct cmd_scan *params = NULL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300807 int i, ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300808 u16 scan_options = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300809
810 if (wl->scanning)
811 return -EINVAL;
812
813 params = kzalloc(sizeof(*params), GFP_KERNEL);
814 if (!params)
815 return -ENOMEM;
816
817 params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
818 params->params.rx_filter_options =
819 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
820
821 /* High priority scan */
822 if (!active_scan)
823 scan_options |= SCAN_PASSIVE;
824 if (high_prio)
825 scan_options |= SCAN_PRIORITY_HIGH;
826 params->params.scan_options = scan_options;
827
828 params->params.num_channels = num_channels;
829 params->params.num_probe_requests = probe_requests;
830 params->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
831 params->params.tid_trigger = 0;
832
833 for (i = 0; i < num_channels; i++) {
834 params->channels[i].min_duration = cpu_to_le32(30000);
835 params->channels[i].max_duration = cpu_to_le32(60000);
836 memset(&params->channels[i].bssid_lsb, 0xff, 4);
837 memset(&params->channels[i].bssid_msb, 0xff, 2);
838 params->channels[i].early_termination = 0;
839 params->channels[i].tx_power_att = 0;
840 params->channels[i].channel = i + 1;
841 memset(params->channels[i].pad, 0, 3);
842 }
843
844 for (i = num_channels; i < SCAN_MAX_NUM_OF_CHANNELS; i++)
845 memset(&params->channels[i], 0,
846 sizeof(struct basic_scan_channel_parameters));
847
848 if (len && ssid) {
849 params->params.ssid_len = len;
850 memcpy(params->params.ssid, ssid, len);
851 } else {
852 params->params.ssid_len = 0;
853 memset(params->params.ssid, 0, 32);
854 }
855
Kalle Valo80301cd2009-06-12 14:17:39 +0300856 ret = wl1251_build_probe_req(wl, ssid, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300857 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300858 wl1251_error("PROBE request template failed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300859 goto out;
860 }
861
Kalle Valoff258392009-06-12 14:14:19 +0300862 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
863 if (!trigger)
864 goto out;
865
866 trigger->timeout = 0;
867
Kalle Valo80301cd2009-06-12 14:17:39 +0300868 ret = wl1251_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
Kalle Valoff258392009-06-12 14:14:19 +0300869 sizeof(*trigger));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300870 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300871 wl1251_error("trigger scan to failed for hw scan");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300872 goto out;
873 }
874
Kalle Valo80301cd2009-06-12 14:17:39 +0300875 wl1251_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300876
877 wl->scanning = true;
878
Kalle Valo80301cd2009-06-12 14:17:39 +0300879 ret = wl1251_cmd_send(wl, CMD_SCAN, params, sizeof(*params));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300880 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300881 wl1251_error("SCAN failed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300882
Bob Copeland0764de62009-08-07 13:32:56 +0300883 wl1251_mem_read(wl, wl->cmd_box_addr, params, sizeof(*params));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300884
Kalle Valoff258392009-06-12 14:14:19 +0300885 if (params->header.status != CMD_STATUS_SUCCESS) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300886 wl1251_error("TEST command answer error: %d",
Kalle Valoff258392009-06-12 14:14:19 +0300887 params->header.status);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300888 wl->scanning = false;
889 ret = -EIO;
890 goto out;
891 }
892
893out:
894 kfree(params);
895 return ret;
896
897}
898
Kalle Valo80301cd2009-06-12 14:17:39 +0300899static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300900 struct cfg80211_scan_request *req)
901{
Kalle Valo80301cd2009-06-12 14:17:39 +0300902 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300903 int ret;
904 u8 *ssid = NULL;
905 size_t ssid_len = 0;
906
Kalle Valo80301cd2009-06-12 14:17:39 +0300907 wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300908
909 if (req->n_ssids) {
910 ssid = req->ssids[0].ssid;
911 ssid_len = req->ssids[0].ssid_len;
912 }
913
914 mutex_lock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +0300915
Kalle Valo80301cd2009-06-12 14:17:39 +0300916 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300917 if (ret < 0)
918 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300919
Kalle Valo80301cd2009-06-12 14:17:39 +0300920 ret = wl1251_hw_scan(hw->priv, ssid, ssid_len, 1, 0, 13, 3);
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300921
Kalle Valo80301cd2009-06-12 14:17:39 +0300922 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300923
924out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300925 mutex_unlock(&wl->mutex);
926
927 return ret;
928}
929
Kalle Valo80301cd2009-06-12 14:17:39 +0300930static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300931{
Kalle Valo80301cd2009-06-12 14:17:39 +0300932 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300933 int ret;
934
Kalle Valocee4fd22009-06-12 14:16:20 +0300935 mutex_lock(&wl->mutex);
936
Kalle Valo80301cd2009-06-12 14:17:39 +0300937 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300938 if (ret < 0)
939 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300940
Kalle Valo80301cd2009-06-12 14:17:39 +0300941 ret = wl1251_acx_rts_threshold(wl, (u16) value);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300942 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300943 wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300944
Kalle Valo80301cd2009-06-12 14:17:39 +0300945 wl1251_ps_elp_sleep(wl);
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300946
Kalle Valoc5483b72009-06-12 14:16:32 +0300947out:
Kalle Valocee4fd22009-06-12 14:16:20 +0300948 mutex_unlock(&wl->mutex);
949
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300950 return ret;
951}
952
Kalle Valo80301cd2009-06-12 14:17:39 +0300953static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300954 struct ieee80211_vif *vif,
955 struct ieee80211_bss_conf *bss_conf,
956 u32 changed)
957{
Kalle Valo80301cd2009-06-12 14:17:39 +0300958 enum wl1251_cmd_ps_mode mode;
959 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300960 struct sk_buff *beacon;
961 int ret;
962
Kalle Valo80301cd2009-06-12 14:17:39 +0300963 wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300964
965 mutex_lock(&wl->mutex);
966
Kalle Valo80301cd2009-06-12 14:17:39 +0300967 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300968 if (ret < 0)
969 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300970
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300971 if (changed & BSS_CHANGED_ASSOC) {
972 if (bss_conf->assoc) {
973 wl->aid = bss_conf->aid;
974
Kalle Valo80301cd2009-06-12 14:17:39 +0300975 ret = wl1251_build_ps_poll(wl, wl->aid);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300976 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300977 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300978
Kalle Valo80301cd2009-06-12 14:17:39 +0300979 ret = wl1251_acx_aid(wl, wl->aid);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300980 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300981 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300982
983 /* If we want to go in PSM but we're not there yet */
984 if (wl->psm_requested && !wl->psm) {
985 mode = STATION_POWER_SAVE_MODE;
Kalle Valo80301cd2009-06-12 14:17:39 +0300986 ret = wl1251_ps_set_mode(wl, mode);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300987 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300988 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300989 }
990 }
991 }
992 if (changed & BSS_CHANGED_ERP_SLOT) {
993 if (bss_conf->use_short_slot)
Kalle Valo80301cd2009-06-12 14:17:39 +0300994 ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300995 else
Kalle Valo80301cd2009-06-12 14:17:39 +0300996 ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300997 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300998 wl1251_warning("Set slot time failed %d", ret);
Kalle Valoc5483b72009-06-12 14:16:32 +0300999 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001000 }
1001 }
1002
1003 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1004 if (bss_conf->use_short_preamble)
Kalle Valo80301cd2009-06-12 14:17:39 +03001005 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001006 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001007 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001008 }
1009
1010 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1011 if (bss_conf->use_cts_prot)
Kalle Valo80301cd2009-06-12 14:17:39 +03001012 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001013 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001014 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001015 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001016 wl1251_warning("Set ctsprotect failed %d", ret);
1017 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001018 }
1019 }
1020
1021 if (changed & BSS_CHANGED_BSSID) {
1022 memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
1023
Kalle Valo80301cd2009-06-12 14:17:39 +03001024 ret = wl1251_build_null_data(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001025 if (ret < 0)
1026 goto out;
1027
1028 if (wl->bss_type != BSS_TYPE_IBSS) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001029 ret = wl1251_cmd_join(wl, wl->bss_type, 5, 100, 1);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001030 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +03001031 goto out_sleep;
1032 wl1251_warning("Set ctsprotect failed %d", ret);
1033 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001034 }
1035 }
1036
1037 if (changed & BSS_CHANGED_BEACON) {
1038 beacon = ieee80211_beacon_get(hw, vif);
Kalle Valo80301cd2009-06-12 14:17:39 +03001039 ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001040 beacon->len);
1041
1042 if (ret < 0) {
1043 dev_kfree_skb(beacon);
1044 goto out;
1045 }
1046
Kalle Valo80301cd2009-06-12 14:17:39 +03001047 ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001048 beacon->len);
1049
1050 dev_kfree_skb(beacon);
1051
1052 if (ret < 0)
1053 goto out;
1054
Juuso Oikarinen77cc9e42009-06-12 14:16:52 +03001055 ret = wl->chip.op_cmd_join(wl, wl->bss_type, 1, 100, 0);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001056
1057 if (ret < 0)
1058 goto out;
1059 }
1060
Kalle Valoc5483b72009-06-12 14:16:32 +03001061out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +03001062 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001063
1064out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001065 mutex_unlock(&wl->mutex);
1066}
1067
1068
1069/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001070static struct ieee80211_rate wl1251_rates[] = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001071 { .bitrate = 10,
1072 .hw_value = 0x1,
1073 .hw_value_short = 0x1, },
1074 { .bitrate = 20,
1075 .hw_value = 0x2,
1076 .hw_value_short = 0x2,
1077 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1078 { .bitrate = 55,
1079 .hw_value = 0x4,
1080 .hw_value_short = 0x4,
1081 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1082 { .bitrate = 110,
1083 .hw_value = 0x20,
1084 .hw_value_short = 0x20,
1085 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1086 { .bitrate = 60,
1087 .hw_value = 0x8,
1088 .hw_value_short = 0x8, },
1089 { .bitrate = 90,
1090 .hw_value = 0x10,
1091 .hw_value_short = 0x10, },
1092 { .bitrate = 120,
1093 .hw_value = 0x40,
1094 .hw_value_short = 0x40, },
1095 { .bitrate = 180,
1096 .hw_value = 0x80,
1097 .hw_value_short = 0x80, },
1098 { .bitrate = 240,
1099 .hw_value = 0x200,
1100 .hw_value_short = 0x200, },
1101 { .bitrate = 360,
1102 .hw_value = 0x400,
1103 .hw_value_short = 0x400, },
1104 { .bitrate = 480,
1105 .hw_value = 0x800,
1106 .hw_value_short = 0x800, },
1107 { .bitrate = 540,
1108 .hw_value = 0x1000,
1109 .hw_value_short = 0x1000, },
1110};
1111
1112/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001113static struct ieee80211_channel wl1251_channels[] = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001114 { .hw_value = 1, .center_freq = 2412},
1115 { .hw_value = 2, .center_freq = 2417},
1116 { .hw_value = 3, .center_freq = 2422},
1117 { .hw_value = 4, .center_freq = 2427},
1118 { .hw_value = 5, .center_freq = 2432},
1119 { .hw_value = 6, .center_freq = 2437},
1120 { .hw_value = 7, .center_freq = 2442},
1121 { .hw_value = 8, .center_freq = 2447},
1122 { .hw_value = 9, .center_freq = 2452},
1123 { .hw_value = 10, .center_freq = 2457},
1124 { .hw_value = 11, .center_freq = 2462},
1125 { .hw_value = 12, .center_freq = 2467},
1126 { .hw_value = 13, .center_freq = 2472},
1127};
1128
1129/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001130static struct ieee80211_supported_band wl1251_band_2ghz = {
1131 .channels = wl1251_channels,
1132 .n_channels = ARRAY_SIZE(wl1251_channels),
1133 .bitrates = wl1251_rates,
1134 .n_bitrates = ARRAY_SIZE(wl1251_rates),
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001135};
1136
Kalle Valo80301cd2009-06-12 14:17:39 +03001137static const struct ieee80211_ops wl1251_ops = {
1138 .start = wl1251_op_start,
1139 .stop = wl1251_op_stop,
1140 .add_interface = wl1251_op_add_interface,
1141 .remove_interface = wl1251_op_remove_interface,
1142 .config = wl1251_op_config,
1143 .configure_filter = wl1251_op_configure_filter,
1144 .tx = wl1251_op_tx,
1145 .set_key = wl1251_op_set_key,
1146 .hw_scan = wl1251_op_hw_scan,
1147 .bss_info_changed = wl1251_op_bss_info_changed,
1148 .set_rts_threshold = wl1251_op_set_rts_threshold,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001149};
1150
Kalle Valo80301cd2009-06-12 14:17:39 +03001151static int wl1251_register_hw(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001152{
1153 int ret;
1154
1155 if (wl->mac80211_registered)
1156 return 0;
1157
1158 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1159
1160 ret = ieee80211_register_hw(wl->hw);
1161 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001162 wl1251_error("unable to register mac80211 hw: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001163 return ret;
1164 }
1165
1166 wl->mac80211_registered = true;
1167
Kalle Valo80301cd2009-06-12 14:17:39 +03001168 wl1251_notice("loaded");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001169
1170 return 0;
1171}
1172
Kalle Valo80301cd2009-06-12 14:17:39 +03001173static int wl1251_init_ieee80211(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001174{
1175 /* The tx descriptor buffer and the TKIP space */
1176 wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +03001177 + WL1251_TKIP_IV_SPACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001178
1179 /* unit us */
1180 /* FIXME: find a proper value */
1181 wl->hw->channel_change_time = 10000;
1182
1183 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
1184 IEEE80211_HW_NOISE_DBM;
1185
1186 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1187 wl->hw->wiphy->max_scan_ssids = 1;
Kalle Valo80301cd2009-06-12 14:17:39 +03001188 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1251_band_2ghz;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001189
1190 SET_IEEE80211_DEV(wl->hw, &wl->spi->dev);
1191
1192 return 0;
1193}
1194
Kalle Valo80301cd2009-06-12 14:17:39 +03001195#define WL1251_DEFAULT_CHANNEL 1
1196static int __devinit wl1251_probe(struct spi_device *spi)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001197{
1198 struct wl12xx_platform_data *pdata;
1199 struct ieee80211_hw *hw;
Kalle Valo80301cd2009-06-12 14:17:39 +03001200 struct wl1251 *wl;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001201 int ret, i;
1202 static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1203
1204 pdata = spi->dev.platform_data;
1205 if (!pdata) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001206 wl1251_error("no platform data");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001207 return -ENODEV;
1208 }
1209
Kalle Valo80301cd2009-06-12 14:17:39 +03001210 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001211 if (!hw) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001212 wl1251_error("could not alloc ieee80211_hw");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001213 return -ENOMEM;
1214 }
1215
1216 wl = hw->priv;
1217 memset(wl, 0, sizeof(*wl));
1218
1219 wl->hw = hw;
1220 dev_set_drvdata(&spi->dev, wl);
1221 wl->spi = spi;
1222
1223 wl->data_in_count = 0;
1224
1225 skb_queue_head_init(&wl->tx_queue);
1226
Kalle Valo80301cd2009-06-12 14:17:39 +03001227 INIT_WORK(&wl->filter_work, wl1251_filter_work);
1228 wl->channel = WL1251_DEFAULT_CHANNEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001229 wl->scanning = false;
1230 wl->default_key = 0;
1231 wl->listen_int = 1;
1232 wl->rx_counter = 0;
1233 wl->rx_handled = 0;
1234 wl->rx_current_buffer = 0;
1235 wl->rx_last_id = 0;
Kalle Valo80301cd2009-06-12 14:17:39 +03001236 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
1237 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001238 wl->elp = false;
1239 wl->psm = 0;
1240 wl->psm_requested = false;
1241 wl->tx_queue_stopped = false;
Kalle Valo80301cd2009-06-12 14:17:39 +03001242 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001243
1244 /* We use the default power on sleep time until we know which chip
1245 * we're using */
Kalle Valo80301cd2009-06-12 14:17:39 +03001246 wl->chip.power_on_sleep = WL1251_DEFAULT_POWER_ON_SLEEP;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001247
1248 for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
1249 wl->tx_frames[i] = NULL;
1250
1251 wl->next_tx_complete = 0;
1252
1253 /*
1254 * In case our MAC address is not correctly set,
1255 * we use a random but Nokia MAC.
1256 */
1257 memcpy(wl->mac_addr, nokia_oui, 3);
1258 get_random_bytes(wl->mac_addr + 3, 3);
1259
Kalle Valo80301cd2009-06-12 14:17:39 +03001260 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001261 mutex_init(&wl->mutex);
1262
1263 wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
1264 wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
1265
Kalle Valo47212132009-06-12 14:15:08 +03001266 wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1267 if (!wl->rx_descriptor) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001268 wl1251_error("could not allocate memory for rx descriptor");
Kalle Valo47212132009-06-12 14:15:08 +03001269 ret = -ENOMEM;
1270 goto out_free;
1271 }
1272
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001273 /* This is the only SPI value that we need to set here, the rest
1274 * comes from the board-peripherals file */
1275 spi->bits_per_word = 32;
1276
1277 ret = spi_setup(spi);
1278 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001279 wl1251_error("spi_setup failed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001280 goto out_free;
1281 }
1282
1283 wl->set_power = pdata->set_power;
1284 if (!wl->set_power) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001285 wl1251_error("set power function missing in platform data");
Kalle Valoc4f5c852009-06-12 14:14:34 +03001286 ret = -ENODEV;
1287 goto out_free;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001288 }
1289
1290 wl->irq = spi->irq;
1291 if (wl->irq < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001292 wl1251_error("irq missing in platform data");
Kalle Valoc4f5c852009-06-12 14:14:34 +03001293 ret = -ENODEV;
1294 goto out_free;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001295 }
1296
Kalle Valo80301cd2009-06-12 14:17:39 +03001297 ret = request_irq(wl->irq, wl1251_irq, 0, DRIVER_NAME, wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001298 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001299 wl1251_error("request_irq() failed: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001300 goto out_free;
1301 }
1302
1303 set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
1304
1305 disable_irq(wl->irq);
1306
Kalle Valo80301cd2009-06-12 14:17:39 +03001307 ret = wl1251_init_ieee80211(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001308 if (ret)
1309 goto out_irq;
1310
Kalle Valo80301cd2009-06-12 14:17:39 +03001311 ret = wl1251_register_hw(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001312 if (ret)
1313 goto out_irq;
1314
Kalle Valo80301cd2009-06-12 14:17:39 +03001315 wl1251_debugfs_init(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001316
Kalle Valo80301cd2009-06-12 14:17:39 +03001317 wl1251_notice("initialized");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001318
1319 return 0;
1320
1321 out_irq:
1322 free_irq(wl->irq, wl);
1323
1324 out_free:
Kalle Valo47212132009-06-12 14:15:08 +03001325 kfree(wl->rx_descriptor);
1326 wl->rx_descriptor = NULL;
1327
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001328 ieee80211_free_hw(hw);
1329
1330 return ret;
1331}
1332
Kalle Valo80301cd2009-06-12 14:17:39 +03001333static int __devexit wl1251_remove(struct spi_device *spi)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001334{
Kalle Valo80301cd2009-06-12 14:17:39 +03001335 struct wl1251 *wl = dev_get_drvdata(&spi->dev);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001336
1337 ieee80211_unregister_hw(wl->hw);
1338
Kalle Valo80301cd2009-06-12 14:17:39 +03001339 wl1251_debugfs_exit(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001340
1341 free_irq(wl->irq, wl);
1342 kfree(wl->target_mem_map);
1343 kfree(wl->data_path);
1344 kfree(wl->fw);
1345 wl->fw = NULL;
1346 kfree(wl->nvs);
1347 wl->nvs = NULL;
Kalle Valo47212132009-06-12 14:15:08 +03001348
1349 kfree(wl->rx_descriptor);
1350 wl->rx_descriptor = NULL;
1351
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001352 ieee80211_free_hw(wl->hw);
1353
1354 return 0;
1355}
1356
1357
Kalle Valo80301cd2009-06-12 14:17:39 +03001358static struct spi_driver wl1251_spi_driver = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001359 .driver = {
Kalle Valo80301cd2009-06-12 14:17:39 +03001360 /* FIXME: use wl12xx name to not break the user space */
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001361 .name = "wl12xx",
1362 .bus = &spi_bus_type,
1363 .owner = THIS_MODULE,
1364 },
1365
Kalle Valo80301cd2009-06-12 14:17:39 +03001366 .probe = wl1251_probe,
1367 .remove = __devexit_p(wl1251_remove),
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001368};
1369
Kalle Valo80301cd2009-06-12 14:17:39 +03001370static int __init wl1251_init(void)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001371{
1372 int ret;
1373
Kalle Valo80301cd2009-06-12 14:17:39 +03001374 ret = spi_register_driver(&wl1251_spi_driver);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001375 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001376 wl1251_error("failed to register spi driver: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001377 goto out;
1378 }
1379
1380out:
1381 return ret;
1382}
1383
Kalle Valo80301cd2009-06-12 14:17:39 +03001384static void __exit wl1251_exit(void)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001385{
Kalle Valo80301cd2009-06-12 14:17:39 +03001386 spi_unregister_driver(&wl1251_spi_driver);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001387
Kalle Valo80301cd2009-06-12 14:17:39 +03001388 wl1251_notice("unloaded");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001389}
1390
Kalle Valo80301cd2009-06-12 14:17:39 +03001391module_init(wl1251_init);
1392module_exit(wl1251_exit);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001393
1394MODULE_LICENSE("GPL");
1395MODULE_AUTHOR("Kalle Valo <Kalle.Valo@nokia.com>, "
1396 "Luciano Coelho <luciano.coelho@nokia.com>");