blob: f9d9ad620cc9f0f538f7c7257f61cac8c20dbab9 [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 *
Kalle Valo2f01a1f2009-04-29 23:33:31 +03006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18 * 02110-1301 USA
19 *
20 */
21
22#include <linux/module.h>
23#include <linux/interrupt.h>
24#include <linux/firmware.h>
25#include <linux/delay.h>
26#include <linux/irq.h>
Kalle Valo2f01a1f2009-04-29 23:33:31 +030027#include <linux/crc32.h>
28#include <linux/etherdevice.h>
Kalle Valoc74ddfd2009-11-17 18:48:45 +020029#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Kalle Valo2f01a1f2009-04-29 23:33:31 +030031
Kalle Valo13674112009-06-12 14:17:25 +030032#include "wl1251.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030033#include "wl12xx_80211.h"
Kalle Valo29d904c2009-08-07 13:35:11 +030034#include "wl1251_reg.h"
Bob Copeland0764de62009-08-07 13:32:56 +030035#include "wl1251_io.h"
Bob Copeland8e639c02009-08-07 13:33:26 +030036#include "wl1251_cmd.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030037#include "wl1251_event.h"
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +030038#include "wl1251_tx.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030039#include "wl1251_rx.h"
40#include "wl1251_ps.h"
41#include "wl1251_init.h"
42#include "wl1251_debugfs.h"
Kalle Valo0e71bb02009-08-07 13:33:57 +030043#include "wl1251_boot.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030044
Bob Copelandb5ed9c12009-08-07 13:33:49 +030045void wl1251_enable_interrupts(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030046{
Bob Copelandb5ed9c12009-08-07 13:33:49 +030047 wl->if_ops->enable_irq(wl);
48}
49
50void wl1251_disable_interrupts(struct wl1251 *wl)
51{
52 wl->if_ops->disable_irq(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030053}
54
Kalle Valo80301cd2009-06-12 14:17:39 +030055static void wl1251_power_off(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030056{
57 wl->set_power(false);
58}
59
Kalle Valo80301cd2009-06-12 14:17:39 +030060static void wl1251_power_on(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030061{
62 wl->set_power(true);
63}
64
Kalle Valo80301cd2009-06-12 14:17:39 +030065static int wl1251_fetch_firmware(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030066{
67 const struct firmware *fw;
Bob Copeland6c766f42009-08-07 13:33:04 +030068 struct device *dev = wiphy_dev(wl->hw->wiphy);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030069 int ret;
70
Kalle Valo0e71bb02009-08-07 13:33:57 +030071 ret = request_firmware(&fw, WL1251_FW_NAME, dev);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030072
73 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +030074 wl1251_error("could not get firmware: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030075 return ret;
76 }
77
78 if (fw->size % 4) {
Kalle Valo80301cd2009-06-12 14:17:39 +030079 wl1251_error("firmware size is not multiple of 32 bits: %zu",
Kalle Valo2f01a1f2009-04-29 23:33:31 +030080 fw->size);
81 ret = -EILSEQ;
82 goto out;
83 }
84
85 wl->fw_len = fw->size;
Kalle Valoc74ddfd2009-11-17 18:48:45 +020086 wl->fw = vmalloc(wl->fw_len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030087
88 if (!wl->fw) {
Kalle Valo80301cd2009-06-12 14:17:39 +030089 wl1251_error("could not allocate memory for the firmware");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030090 ret = -ENOMEM;
91 goto out;
92 }
93
94 memcpy(wl->fw, fw->data, wl->fw_len);
95
96 ret = 0;
97
98out:
99 release_firmware(fw);
100
101 return ret;
102}
103
Kalle Valo80301cd2009-06-12 14:17:39 +0300104static int wl1251_fetch_nvs(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300105{
106 const struct firmware *fw;
Bob Copeland6c766f42009-08-07 13:33:04 +0300107 struct device *dev = wiphy_dev(wl->hw->wiphy);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300108 int ret;
109
Kalle Valo0e71bb02009-08-07 13:33:57 +0300110 ret = request_firmware(&fw, WL1251_NVS_NAME, dev);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300111
112 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300113 wl1251_error("could not get nvs file: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300114 return ret;
115 }
116
117 if (fw->size % 4) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300118 wl1251_error("nvs size is not multiple of 32 bits: %zu",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300119 fw->size);
120 ret = -EILSEQ;
121 goto out;
122 }
123
124 wl->nvs_len = fw->size;
Julia Lawall80caf602010-05-15 23:15:10 +0200125 wl->nvs = kmemdup(fw->data, wl->nvs_len, GFP_KERNEL);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300126
127 if (!wl->nvs) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300128 wl1251_error("could not allocate memory for the nvs file");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300129 ret = -ENOMEM;
130 goto out;
131 }
132
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300133 ret = 0;
134
135out:
136 release_firmware(fw);
137
138 return ret;
139}
140
Kalle Valo80301cd2009-06-12 14:17:39 +0300141static void wl1251_fw_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300142{
143 u32 elp_reg;
144
145 elp_reg = ELPCTRL_WAKE_UP;
Grazvydas Ignotas3f9e7502010-03-11 17:44:57 +0200146 wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
147 elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300148
Kalle Valo05fac682009-06-12 14:17:47 +0300149 if (!(elp_reg & ELPCTRL_WLAN_READY))
Kalle Valo80301cd2009-06-12 14:17:39 +0300150 wl1251_warning("WLAN not ready");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300151}
152
Kalle Valo80301cd2009-06-12 14:17:39 +0300153static int wl1251_chip_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300154{
155 int ret = 0;
156
Kalle Valo80301cd2009-06-12 14:17:39 +0300157 wl1251_power_on(wl);
Kalle Valo0e71bb02009-08-07 13:33:57 +0300158 msleep(WL1251_POWER_ON_SLEEP);
Bob Copeland08d9f572009-08-07 13:33:11 +0300159 wl->if_ops->reset(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300160
161 /* We don't need a real memory partition here, because we only want
162 * to use the registers at this point. */
Kalle Valo80301cd2009-06-12 14:17:39 +0300163 wl1251_set_partition(wl,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300164 0x00000000,
165 0x00000000,
166 REGISTERS_BASE,
167 REGISTERS_DOWN_SIZE);
168
169 /* ELP module wake up */
Kalle Valo80301cd2009-06-12 14:17:39 +0300170 wl1251_fw_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300171
172 /* whal_FwCtrl_BootSm() */
173
174 /* 0. read chip id from CHIP_ID */
Kalle Valo0e71bb02009-08-07 13:33:57 +0300175 wl->chip_id = wl1251_reg_read32(wl, CHIP_ID_B);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300176
177 /* 1. check if chip id is valid */
178
Kalle Valo0e71bb02009-08-07 13:33:57 +0300179 switch (wl->chip_id) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300180 case CHIP_ID_1251_PG12:
Kalle Valo80301cd2009-06-12 14:17:39 +0300181 wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG12)",
Kalle Valo0e71bb02009-08-07 13:33:57 +0300182 wl->chip_id);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300183 break;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300184 case CHIP_ID_1251_PG11:
David-John Willis2c759e02009-10-15 14:38:16 +0100185 wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG11)",
186 wl->chip_id);
187 break;
John W. Linville9a493e82009-10-27 15:15:05 -0400188 case CHIP_ID_1251_PG10:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300189 default:
Kalle Valo0e71bb02009-08-07 13:33:57 +0300190 wl1251_error("unsupported chip id: 0x%x", wl->chip_id);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300191 ret = -ENODEV;
192 goto out;
193 }
194
195 if (wl->fw == NULL) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300196 ret = wl1251_fetch_firmware(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300197 if (ret < 0)
198 goto out;
199 }
200
Grazvydas Ignotasafa5ec22010-04-13 01:21:40 +0300201 if (wl->nvs == NULL && !wl->use_eeprom) {
202 /* No NVS from netlink, try to get it from the filesystem */
Kalle Valo80301cd2009-06-12 14:17:39 +0300203 ret = wl1251_fetch_nvs(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300204 if (ret < 0)
205 goto out;
206 }
207
208out:
209 return ret;
210}
211
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200212#define WL1251_IRQ_LOOP_COUNT 10
Kalle Valo0e71bb02009-08-07 13:33:57 +0300213static void wl1251_irq_work(struct work_struct *work)
214{
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200215 u32 intr, ctr = WL1251_IRQ_LOOP_COUNT;
Kalle Valo0e71bb02009-08-07 13:33:57 +0300216 struct wl1251 *wl =
217 container_of(work, struct wl1251, irq_work);
218 int ret;
219
220 mutex_lock(&wl->mutex);
221
222 wl1251_debug(DEBUG_IRQ, "IRQ work");
223
224 if (wl->state == WL1251_STATE_OFF)
225 goto out;
226
227 ret = wl1251_ps_elp_wakeup(wl);
228 if (ret < 0)
229 goto out;
230
231 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, WL1251_ACX_INTR_ALL);
232
233 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
234 wl1251_debug(DEBUG_IRQ, "intr: 0x%x", intr);
235
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200236 do {
237 if (wl->data_path) {
238 wl->rx_counter = wl1251_mem_read32(
239 wl, wl->data_path->rx_control_addr);
Kalle Valo0e71bb02009-08-07 13:33:57 +0300240
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200241 /* We handle a frmware bug here */
242 switch ((wl->rx_counter - wl->rx_handled) & 0xf) {
243 case 0:
244 wl1251_debug(DEBUG_IRQ,
245 "RX: FW and host in sync");
246 intr &= ~WL1251_ACX_INTR_RX0_DATA;
247 intr &= ~WL1251_ACX_INTR_RX1_DATA;
248 break;
249 case 1:
250 wl1251_debug(DEBUG_IRQ, "RX: FW +1");
251 intr |= WL1251_ACX_INTR_RX0_DATA;
252 intr &= ~WL1251_ACX_INTR_RX1_DATA;
253 break;
254 case 2:
255 wl1251_debug(DEBUG_IRQ, "RX: FW +2");
256 intr |= WL1251_ACX_INTR_RX0_DATA;
257 intr |= WL1251_ACX_INTR_RX1_DATA;
258 break;
259 default:
260 wl1251_warning(
261 "RX: FW and host out of sync: %d",
262 wl->rx_counter - wl->rx_handled);
263 break;
264 }
265
266 wl->rx_handled = wl->rx_counter;
267
268 wl1251_debug(DEBUG_IRQ, "RX counter: %d",
269 wl->rx_counter);
Kalle Valo0e71bb02009-08-07 13:33:57 +0300270 }
271
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200272 intr &= wl->intr_mask;
Kalle Valo0e71bb02009-08-07 13:33:57 +0300273
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200274 if (intr == 0) {
275 wl1251_debug(DEBUG_IRQ, "INTR is 0");
276 goto out_sleep;
277 }
Kalle Valo0e71bb02009-08-07 13:33:57 +0300278
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200279 if (intr & WL1251_ACX_INTR_RX0_DATA) {
280 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX0_DATA");
281 wl1251_rx(wl);
282 }
Kalle Valo0e71bb02009-08-07 13:33:57 +0300283
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200284 if (intr & WL1251_ACX_INTR_RX1_DATA) {
285 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX1_DATA");
286 wl1251_rx(wl);
287 }
Kalle Valo0e71bb02009-08-07 13:33:57 +0300288
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200289 if (intr & WL1251_ACX_INTR_TX_RESULT) {
290 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_TX_RESULT");
291 wl1251_tx_complete(wl);
292 }
Kalle Valo0e71bb02009-08-07 13:33:57 +0300293
Grazvydas Ignotasd41776f2010-08-17 22:46:53 +0300294 if (intr & WL1251_ACX_INTR_EVENT_A) {
295 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT_A");
296 wl1251_event_handle(wl, 0);
297 }
298
299 if (intr & WL1251_ACX_INTR_EVENT_B) {
300 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT_B");
301 wl1251_event_handle(wl, 1);
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200302 }
Kalle Valo0e71bb02009-08-07 13:33:57 +0300303
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200304 if (intr & WL1251_ACX_INTR_INIT_COMPLETE)
305 wl1251_debug(DEBUG_IRQ,
306 "WL1251_ACX_INTR_INIT_COMPLETE");
Kalle Valo0e71bb02009-08-07 13:33:57 +0300307
Juuso Oikarinen79553f82009-11-17 18:49:46 +0200308 if (--ctr == 0)
309 break;
Kalle Valo0e71bb02009-08-07 13:33:57 +0300310
Juuso Oikarinen79553f82009-11-17 18:49:46 +0200311 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
312 } while (intr);
Kalle Valo0e71bb02009-08-07 13:33:57 +0300313
314out_sleep:
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200315 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, ~(wl->intr_mask));
Kalle Valo0e71bb02009-08-07 13:33:57 +0300316 wl1251_ps_elp_sleep(wl);
317
318out:
319 mutex_unlock(&wl->mutex);
320}
321
Kalle Valoae46ae12009-08-07 13:34:42 +0300322static int wl1251_join(struct wl1251 *wl, u8 bss_type, u8 channel,
323 u16 beacon_interval, u8 dtim_period)
324{
325 int ret;
326
327 ret = wl1251_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
328 DEFAULT_HW_GEN_MODULATION_TYPE,
329 wl->tx_mgmt_frm_rate,
330 wl->tx_mgmt_frm_mod);
331 if (ret < 0)
332 goto out;
333
334
335 ret = wl1251_cmd_join(wl, bss_type, channel, beacon_interval,
336 dtim_period);
337 if (ret < 0)
338 goto out;
339
Grazvydas Ignotas7273b972010-08-17 22:46:55 +0300340 ret = wl1251_event_wait(wl, JOIN_EVENT_COMPLETE_ID, 100);
341 if (ret < 0)
342 wl1251_warning("join timeout");
Kalle Valoae46ae12009-08-07 13:34:42 +0300343
344out:
345 return ret;
346}
347
Kalle Valo80301cd2009-06-12 14:17:39 +0300348static void wl1251_filter_work(struct work_struct *work)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300349{
Kalle Valo80301cd2009-06-12 14:17:39 +0300350 struct wl1251 *wl =
351 container_of(work, struct wl1251, filter_work);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300352 int ret;
353
354 mutex_lock(&wl->mutex);
355
Kalle Valo80301cd2009-06-12 14:17:39 +0300356 if (wl->state == WL1251_STATE_OFF)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300357 goto out;
358
Kalle Valo80301cd2009-06-12 14:17:39 +0300359 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300360 if (ret < 0)
361 goto out;
362
Kalle Valoae46ae12009-08-07 13:34:42 +0300363 ret = wl1251_join(wl, wl->bss_type, wl->channel, wl->beacon_int,
364 wl->dtim_period);
Kalle Valoc5483b72009-06-12 14:16:32 +0300365 if (ret < 0)
366 goto out_sleep;
367
368out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300369 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300370
371out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300372 mutex_unlock(&wl->mutex);
373}
374
Kalle Valo80301cd2009-06-12 14:17:39 +0300375static int wl1251_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300376{
Kalle Valo80301cd2009-06-12 14:17:39 +0300377 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300378
379 skb_queue_tail(&wl->tx_queue, skb);
380
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +0300381 /*
382 * The chip specific setup must run before the first TX packet -
383 * before that, the tx_work will not be initialized!
384 */
385
Kalle Valo16e711f2009-08-07 13:35:04 +0300386 ieee80211_queue_work(wl->hw, &wl->tx_work);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300387
388 /*
389 * The workqueue is slow to process the tx_queue and we need stop
390 * the queue here, otherwise the queue will get too long.
391 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300392 if (skb_queue_len(&wl->tx_queue) >= WL1251_TX_QUEUE_MAX_LENGTH) {
Kalle Valod67e2612009-11-30 10:17:45 +0200393 wl1251_debug(DEBUG_TX, "op_tx: tx_queue full, stop queues");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300394 ieee80211_stop_queues(wl->hw);
395
396 /*
397 * FIXME: this is racy, the variable is not properly
398 * protected. Maybe fix this by removing the stupid
399 * variable altogether and checking the real queue state?
400 */
401 wl->tx_queue_stopped = true;
402 }
403
404 return NETDEV_TX_OK;
405}
406
Kalle Valo80301cd2009-06-12 14:17:39 +0300407static int wl1251_op_start(struct ieee80211_hw *hw)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300408{
Kalle Valo80301cd2009-06-12 14:17:39 +0300409 struct wl1251 *wl = hw->priv;
John W. Linville8b28e822010-07-28 16:59:41 -0400410 struct wiphy *wiphy = hw->wiphy;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300411 int ret = 0;
412
Kalle Valo80301cd2009-06-12 14:17:39 +0300413 wl1251_debug(DEBUG_MAC80211, "mac80211 start");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300414
415 mutex_lock(&wl->mutex);
416
Kalle Valo80301cd2009-06-12 14:17:39 +0300417 if (wl->state != WL1251_STATE_OFF) {
418 wl1251_error("cannot start because not in off state: %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300419 wl->state);
420 ret = -EBUSY;
421 goto out;
422 }
423
Kalle Valo80301cd2009-06-12 14:17:39 +0300424 ret = wl1251_chip_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300425 if (ret < 0)
Jiri Slabyfe643412009-07-14 22:37:13 +0200426 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300427
Kalle Valo0e71bb02009-08-07 13:33:57 +0300428 ret = wl1251_boot(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300429 if (ret < 0)
430 goto out;
431
Kalle Valo0e71bb02009-08-07 13:33:57 +0300432 ret = wl1251_hw_init(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300433 if (ret < 0)
434 goto out;
435
Kalle Valo80301cd2009-06-12 14:17:39 +0300436 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300437 if (ret < 0)
438 goto out;
439
Kalle Valo80301cd2009-06-12 14:17:39 +0300440 wl->state = WL1251_STATE_ON;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300441
Kalle Valo0e71bb02009-08-07 13:33:57 +0300442 wl1251_info("firmware booted (%s)", wl->fw_ver);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300443
John W. Linville8b28e822010-07-28 16:59:41 -0400444 /* update hw/fw version info in wiphy struct */
445 wiphy->hw_version = wl->chip_id;
446 strncpy(wiphy->fw_version, wl->fw_ver, sizeof(wiphy->fw_version));
447
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300448out:
449 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300450 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300451
452 mutex_unlock(&wl->mutex);
453
454 return ret;
455}
456
Kalle Valo80301cd2009-06-12 14:17:39 +0300457static void wl1251_op_stop(struct ieee80211_hw *hw)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300458{
Kalle Valo80301cd2009-06-12 14:17:39 +0300459 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300460
Kalle Valo80301cd2009-06-12 14:17:39 +0300461 wl1251_info("down");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300462
Kalle Valo80301cd2009-06-12 14:17:39 +0300463 wl1251_debug(DEBUG_MAC80211, "mac80211 stop");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300464
465 mutex_lock(&wl->mutex);
466
Kalle Valo80301cd2009-06-12 14:17:39 +0300467 WARN_ON(wl->state != WL1251_STATE_ON);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300468
469 if (wl->scanning) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300470 ieee80211_scan_completed(wl->hw, true);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300471 wl->scanning = false;
472 }
473
Kalle Valo80301cd2009-06-12 14:17:39 +0300474 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300475
Kalle Valo80301cd2009-06-12 14:17:39 +0300476 wl1251_disable_interrupts(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300477
478 mutex_unlock(&wl->mutex);
479
480 cancel_work_sync(&wl->irq_work);
481 cancel_work_sync(&wl->tx_work);
482 cancel_work_sync(&wl->filter_work);
483
484 mutex_lock(&wl->mutex);
485
486 /* let's notify MAC80211 about the remaining pending TX frames */
Kalle Valo0e71bb02009-08-07 13:33:57 +0300487 wl1251_tx_flush(wl);
Kalle Valo80301cd2009-06-12 14:17:39 +0300488 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300489
490 memset(wl->bssid, 0, ETH_ALEN);
491 wl->listen_int = 1;
492 wl->bss_type = MAX_BSS_TYPE;
493
494 wl->data_in_count = 0;
495 wl->rx_counter = 0;
496 wl->rx_handled = 0;
497 wl->rx_current_buffer = 0;
498 wl->rx_last_id = 0;
499 wl->next_tx_complete = 0;
500 wl->elp = false;
501 wl->psm = 0;
502 wl->tx_queue_stopped = false;
Kalle Valo80301cd2009-06-12 14:17:39 +0300503 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
Kalle Valo97802792009-08-07 13:34:27 +0300504 wl->channel = WL1251_DEFAULT_CHANNEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300505
Kalle Valo80301cd2009-06-12 14:17:39 +0300506 wl1251_debugfs_reset(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300507
508 mutex_unlock(&wl->mutex);
509}
510
Kalle Valo80301cd2009-06-12 14:17:39 +0300511static int wl1251_op_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100512 struct ieee80211_vif *vif)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300513{
Kalle Valo80301cd2009-06-12 14:17:39 +0300514 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300515 int ret = 0;
516
Johannes Berge91d8332009-07-15 17:21:41 +0200517 wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
Johannes Berg1ed32e42009-12-23 13:15:45 +0100518 vif->type, vif->addr);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300519
520 mutex_lock(&wl->mutex);
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200521 if (wl->vif) {
522 ret = -EBUSY;
523 goto out;
524 }
525
Johannes Berg1ed32e42009-12-23 13:15:45 +0100526 wl->vif = vif;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300527
Johannes Berg1ed32e42009-12-23 13:15:45 +0100528 switch (vif->type) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300529 case NL80211_IFTYPE_STATION:
530 wl->bss_type = BSS_TYPE_STA_BSS;
531 break;
532 case NL80211_IFTYPE_ADHOC:
533 wl->bss_type = BSS_TYPE_IBSS;
534 break;
535 default:
536 ret = -EOPNOTSUPP;
537 goto out;
538 }
539
Johannes Berg1ed32e42009-12-23 13:15:45 +0100540 if (memcmp(wl->mac_addr, vif->addr, ETH_ALEN)) {
541 memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300542 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
Kalle Valo80301cd2009-06-12 14:17:39 +0300543 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300544 if (ret < 0)
545 goto out;
546 }
547
548out:
549 mutex_unlock(&wl->mutex);
550 return ret;
551}
552
Kalle Valo80301cd2009-06-12 14:17:39 +0300553static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100554 struct ieee80211_vif *vif)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300555{
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200556 struct wl1251 *wl = hw->priv;
557
558 mutex_lock(&wl->mutex);
Kalle Valo80301cd2009-06-12 14:17:39 +0300559 wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200560 wl->vif = NULL;
561 mutex_unlock(&wl->mutex);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300562}
563
Kalle Valo8f8ff912010-01-12 10:43:07 +0200564static int wl1251_build_qos_null_data(struct wl1251 *wl)
565{
566 struct ieee80211_qos_hdr template;
567
568 memset(&template, 0, sizeof(template));
569
570 memcpy(template.addr1, wl->bssid, ETH_ALEN);
571 memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
572 memcpy(template.addr3, wl->bssid, ETH_ALEN);
573
574 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
575 IEEE80211_STYPE_QOS_NULLFUNC |
576 IEEE80211_FCTL_TODS);
577
578 /* FIXME: not sure what priority to use here */
579 template.qos_ctrl = cpu_to_le16(0);
580
581 return wl1251_cmd_template_set(wl, CMD_QOS_NULL_DATA, &template,
582 sizeof(template));
583}
584
Kalle Valo80301cd2009-06-12 14:17:39 +0300585static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300586{
Kalle Valo80301cd2009-06-12 14:17:39 +0300587 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300588 struct ieee80211_conf *conf = &hw->conf;
589 int channel, ret = 0;
590
591 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
592
Kalle Valo80301cd2009-06-12 14:17:39 +0300593 wl1251_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300594 channel,
595 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
596 conf->power_level);
597
598 mutex_lock(&wl->mutex);
599
Kalle Valo80301cd2009-06-12 14:17:39 +0300600 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300601 if (ret < 0)
602 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300603
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300604 if (channel != wl->channel) {
Kalle Valofe9a9842009-08-07 13:34:49 +0300605 wl->channel = channel;
606
Kalle Valoae46ae12009-08-07 13:34:42 +0300607 ret = wl1251_join(wl, wl->bss_type, wl->channel,
608 wl->beacon_int, wl->dtim_period);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300609 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300610 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300611 }
612
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300613 if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
Luciano Coelho47af3fe2009-06-12 14:17:53 +0300614 wl1251_debug(DEBUG_PSM, "psm enabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300615
616 wl->psm_requested = true;
617
Johannes Berg56007a02010-01-26 14:19:52 +0100618 wl->dtim_period = conf->ps_dtim_period;
619
620 ret = wl1251_acx_wr_tbtt_and_dtim(wl, wl->beacon_int,
621 wl->dtim_period);
622
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300623 /*
Johannes Berg56007a02010-01-26 14:19:52 +0100624 * mac80211 enables PSM only if we're already associated.
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300625 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300626 ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
Kalle Valo478fdf22009-11-30 10:17:52 +0200627 if (ret < 0)
628 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300629 } else if (!(conf->flags & IEEE80211_CONF_PS) &&
630 wl->psm_requested) {
Luciano Coelho47af3fe2009-06-12 14:17:53 +0300631 wl1251_debug(DEBUG_PSM, "psm disabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300632
633 wl->psm_requested = false;
634
Kalle Valo478fdf22009-11-30 10:17:52 +0200635 if (wl->psm) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300636 ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
Kalle Valo478fdf22009-11-30 10:17:52 +0200637 if (ret < 0)
638 goto out_sleep;
639 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300640 }
641
642 if (conf->power_level != wl->power_level) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300643 ret = wl1251_acx_tx_power(wl, conf->power_level);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300644 if (ret < 0)
Kalle Valo478fdf22009-11-30 10:17:52 +0200645 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300646
647 wl->power_level = conf->power_level;
648 }
649
Kalle Valoc5483b72009-06-12 14:16:32 +0300650out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300651 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300652
653out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300654 mutex_unlock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +0300655
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300656 return ret;
657}
658
Kalle Valo80301cd2009-06-12 14:17:39 +0300659#define WL1251_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300660 FIF_ALLMULTI | \
661 FIF_FCSFAIL | \
662 FIF_BCN_PRBRESP_PROMISC | \
663 FIF_CONTROL | \
664 FIF_OTHER_BSS)
665
Kalle Valo80301cd2009-06-12 14:17:39 +0300666static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300667 unsigned int changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +0200668 unsigned int *total,u64 multicast)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300669{
Kalle Valo80301cd2009-06-12 14:17:39 +0300670 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300671
Kalle Valo80301cd2009-06-12 14:17:39 +0300672 wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300673
Kalle Valo80301cd2009-06-12 14:17:39 +0300674 *total &= WL1251_SUPPORTED_FILTERS;
675 changed &= WL1251_SUPPORTED_FILTERS;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300676
677 if (changed == 0)
678 /* no filters which we support changed */
679 return;
680
681 /* FIXME: wl->rx_config and wl->rx_filter are not protected */
682
Kalle Valo80301cd2009-06-12 14:17:39 +0300683 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
684 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300685
686 if (*total & FIF_PROMISC_IN_BSS) {
687 wl->rx_config |= CFG_BSSID_FILTER_EN;
688 wl->rx_config |= CFG_RX_ALL_GOOD;
689 }
690 if (*total & FIF_ALLMULTI)
691 /*
692 * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
693 * all multicast frames
694 */
695 wl->rx_config &= ~CFG_MC_FILTER_EN;
696 if (*total & FIF_FCSFAIL)
697 wl->rx_filter |= CFG_RX_FCS_ERROR;
698 if (*total & FIF_BCN_PRBRESP_PROMISC) {
699 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
700 wl->rx_config &= ~CFG_SSID_FILTER_EN;
701 }
702 if (*total & FIF_CONTROL)
703 wl->rx_filter |= CFG_RX_CTL_EN;
704 if (*total & FIF_OTHER_BSS)
705 wl->rx_filter &= ~CFG_BSSID_FILTER_EN;
706
707 /*
708 * FIXME: workqueues need to be properly cancelled on stop(), for
709 * now let's just disable changing the filter settings. They will
710 * be updated any on config().
711 */
712 /* schedule_work(&wl->filter_work); */
713}
714
715/* HW encryption */
Kalle Valo80301cd2009-06-12 14:17:39 +0300716static int wl1251_set_key_type(struct wl1251 *wl,
717 struct wl1251_cmd_set_keys *key,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300718 enum set_key_cmd cmd,
719 struct ieee80211_key_conf *mac80211_key,
720 const u8 *addr)
721{
Johannes Berg97359d12010-08-10 09:46:38 +0200722 switch (mac80211_key->cipher) {
723 case WLAN_CIPHER_SUITE_WEP40:
724 case WLAN_CIPHER_SUITE_WEP104:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300725 if (is_broadcast_ether_addr(addr))
726 key->key_type = KEY_WEP_DEFAULT;
727 else
728 key->key_type = KEY_WEP_ADDR;
729
730 mac80211_key->hw_key_idx = mac80211_key->keyidx;
731 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200732 case WLAN_CIPHER_SUITE_TKIP:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300733 if (is_broadcast_ether_addr(addr))
734 key->key_type = KEY_TKIP_MIC_GROUP;
735 else
736 key->key_type = KEY_TKIP_MIC_PAIRWISE;
737
738 mac80211_key->hw_key_idx = mac80211_key->keyidx;
739 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200740 case WLAN_CIPHER_SUITE_CCMP:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300741 if (is_broadcast_ether_addr(addr))
742 key->key_type = KEY_AES_GROUP;
743 else
744 key->key_type = KEY_AES_PAIRWISE;
745 mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
746 break;
747 default:
Johannes Berg97359d12010-08-10 09:46:38 +0200748 wl1251_error("Unknown key cipher 0x%x", mac80211_key->cipher);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300749 return -EOPNOTSUPP;
750 }
751
752 return 0;
753}
754
Kalle Valo80301cd2009-06-12 14:17:39 +0300755static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300756 struct ieee80211_vif *vif,
757 struct ieee80211_sta *sta,
758 struct ieee80211_key_conf *key)
759{
Kalle Valo80301cd2009-06-12 14:17:39 +0300760 struct wl1251 *wl = hw->priv;
761 struct wl1251_cmd_set_keys *wl_cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300762 const u8 *addr;
763 int ret;
764
765 static const u8 bcast_addr[ETH_ALEN] =
766 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
767
Kalle Valo80301cd2009-06-12 14:17:39 +0300768 wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300769
Kalle Valoff258392009-06-12 14:14:19 +0300770 wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
771 if (!wl_cmd) {
772 ret = -ENOMEM;
773 goto out;
774 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300775
776 addr = sta ? sta->addr : bcast_addr;
777
Kalle Valo80301cd2009-06-12 14:17:39 +0300778 wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
779 wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
780 wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
Johannes Berg97359d12010-08-10 09:46:38 +0200781 key->cipher, key->keyidx, key->keylen, key->flags);
Kalle Valo80301cd2009-06-12 14:17:39 +0300782 wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300783
Kalle Valoff258392009-06-12 14:14:19 +0300784 if (is_zero_ether_addr(addr)) {
785 /* We dont support TX only encryption */
786 ret = -EOPNOTSUPP;
787 goto out;
788 }
789
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300790 mutex_lock(&wl->mutex);
791
Kalle Valo80301cd2009-06-12 14:17:39 +0300792 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300793 if (ret < 0)
794 goto out_unlock;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300795
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300796 switch (cmd) {
797 case SET_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300798 wl_cmd->key_action = KEY_ADD_OR_REPLACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300799 break;
800 case DISABLE_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300801 wl_cmd->key_action = KEY_REMOVE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300802 break;
803 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300804 wl1251_error("Unsupported key cmd 0x%x", cmd);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300805 break;
806 }
807
Kalle Valo80301cd2009-06-12 14:17:39 +0300808 ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300809 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300810 wl1251_error("Set KEY type failed");
Kalle Valoc5483b72009-06-12 14:16:32 +0300811 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300812 }
813
Kalle Valoff258392009-06-12 14:14:19 +0300814 if (wl_cmd->key_type != KEY_WEP_DEFAULT)
815 memcpy(wl_cmd->addr, addr, ETH_ALEN);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300816
Kalle Valoff258392009-06-12 14:14:19 +0300817 if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
818 (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300819 /*
820 * We get the key in the following form:
821 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
822 * but the target is expecting:
823 * TKIP - RX MIC - TX MIC
824 */
Kalle Valoff258392009-06-12 14:14:19 +0300825 memcpy(wl_cmd->key, key->key, 16);
826 memcpy(wl_cmd->key + 16, key->key + 24, 8);
827 memcpy(wl_cmd->key + 24, key->key + 16, 8);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300828
829 } else {
Kalle Valoff258392009-06-12 14:14:19 +0300830 memcpy(wl_cmd->key, key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300831 }
Kalle Valoff258392009-06-12 14:14:19 +0300832 wl_cmd->key_size = key->keylen;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300833
Kalle Valoff258392009-06-12 14:14:19 +0300834 wl_cmd->id = key->keyidx;
835 wl_cmd->ssid_profile = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300836
Kalle Valo80301cd2009-06-12 14:17:39 +0300837 wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300838
Kalle Valo80301cd2009-06-12 14:17:39 +0300839 ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
Kalle Valoff258392009-06-12 14:14:19 +0300840 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300841 wl1251_warning("could not set keys");
Kalle Valoc5483b72009-06-12 14:16:32 +0300842 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300843 }
844
Kalle Valoc5483b72009-06-12 14:16:32 +0300845out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300846 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300847
848out_unlock:
Kalle Valoff258392009-06-12 14:14:19 +0300849 mutex_unlock(&wl->mutex);
850
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300851out:
Kalle Valoff258392009-06-12 14:14:19 +0300852 kfree(wl_cmd);
853
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300854 return ret;
855}
856
Kalle Valo80301cd2009-06-12 14:17:39 +0300857static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
Johannes Berga060bbf2010-04-27 11:59:34 +0200858 struct ieee80211_vif *vif,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300859 struct cfg80211_scan_request *req)
860{
Kalle Valo80301cd2009-06-12 14:17:39 +0300861 struct wl1251 *wl = hw->priv;
Kalle Valoe477c562010-01-05 20:16:57 +0200862 struct sk_buff *skb;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300863 size_t ssid_len = 0;
Kalle Valo3a98c302010-01-05 20:16:51 +0200864 u8 *ssid = NULL;
865 int ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300866
Kalle Valo80301cd2009-06-12 14:17:39 +0300867 wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300868
869 if (req->n_ssids) {
870 ssid = req->ssids[0].ssid;
871 ssid_len = req->ssids[0].ssid_len;
872 }
873
874 mutex_lock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +0300875
Kalle Valo3a98c302010-01-05 20:16:51 +0200876 if (wl->scanning) {
877 wl1251_debug(DEBUG_SCAN, "scan already in progress");
878 ret = -EINVAL;
879 goto out;
880 }
881
Kalle Valo80301cd2009-06-12 14:17:39 +0300882 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300883 if (ret < 0)
884 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300885
Kalle Valoe477c562010-01-05 20:16:57 +0200886 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
887 req->ie, req->ie_len);
888 if (!skb) {
889 ret = -ENOMEM;
890 goto out;
891 }
892
893 ret = wl1251_cmd_template_set(wl, CMD_PROBE_REQ, skb->data,
894 skb->len);
895 dev_kfree_skb(skb);
Kalle Valo3a98c302010-01-05 20:16:51 +0200896 if (ret < 0)
Kalle Valoe477c562010-01-05 20:16:57 +0200897 goto out_sleep;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300898
Kalle Valo3a98c302010-01-05 20:16:51 +0200899 ret = wl1251_cmd_trigger_scan_to(wl, 0);
900 if (ret < 0)
901 goto out_sleep;
902
903 wl->scanning = true;
904
Kalle Valodc52f0a2010-01-05 20:17:03 +0200905 ret = wl1251_cmd_scan(wl, ssid, ssid_len, req->channels,
906 req->n_channels, WL1251_SCAN_NUM_PROBES);
Kalle Valo3a98c302010-01-05 20:16:51 +0200907 if (ret < 0) {
908 wl->scanning = false;
909 goto out_sleep;
910 }
911
912out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300913 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300914
915out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300916 mutex_unlock(&wl->mutex);
917
918 return ret;
919}
920
Kalle Valo80301cd2009-06-12 14:17:39 +0300921static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300922{
Kalle Valo80301cd2009-06-12 14:17:39 +0300923 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300924 int ret;
925
Kalle Valocee4fd22009-06-12 14:16:20 +0300926 mutex_lock(&wl->mutex);
927
Kalle Valo80301cd2009-06-12 14:17:39 +0300928 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300929 if (ret < 0)
930 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300931
Kalle Valo80301cd2009-06-12 14:17:39 +0300932 ret = wl1251_acx_rts_threshold(wl, (u16) value);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300933 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300934 wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300935
Kalle Valo80301cd2009-06-12 14:17:39 +0300936 wl1251_ps_elp_sleep(wl);
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300937
Kalle Valoc5483b72009-06-12 14:16:32 +0300938out:
Kalle Valocee4fd22009-06-12 14:16:20 +0300939 mutex_unlock(&wl->mutex);
940
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300941 return ret;
942}
943
Kalle Valo80301cd2009-06-12 14:17:39 +0300944static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300945 struct ieee80211_vif *vif,
946 struct ieee80211_bss_conf *bss_conf,
947 u32 changed)
948{
Kalle Valo80301cd2009-06-12 14:17:39 +0300949 struct wl1251 *wl = hw->priv;
Kalle Valof7f70572010-01-05 20:16:32 +0200950 struct sk_buff *beacon, *skb;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300951 int ret;
952
Kalle Valo80301cd2009-06-12 14:17:39 +0300953 wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300954
955 mutex_lock(&wl->mutex);
956
Kalle Valo80301cd2009-06-12 14:17:39 +0300957 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300958 if (ret < 0)
959 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300960
Kalle Valode8df1e2009-11-26 10:56:06 +0200961 if (changed & BSS_CHANGED_BSSID) {
962 memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
963
Kalle Valof7f70572010-01-05 20:16:32 +0200964 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
965 if (!skb)
966 goto out_sleep;
967
968 ret = wl1251_cmd_template_set(wl, CMD_NULL_DATA,
969 skb->data, skb->len);
970 dev_kfree_skb(skb);
Kalle Valode8df1e2009-11-26 10:56:06 +0200971 if (ret < 0)
Kalle Valo80a112f2010-01-05 20:17:10 +0200972 goto out_sleep;
Kalle Valode8df1e2009-11-26 10:56:06 +0200973
Kalle Valo8f8ff912010-01-12 10:43:07 +0200974 ret = wl1251_build_qos_null_data(wl);
975 if (ret < 0)
976 goto out;
977
Kalle Valode8df1e2009-11-26 10:56:06 +0200978 if (wl->bss_type != BSS_TYPE_IBSS) {
979 ret = wl1251_join(wl, wl->bss_type, wl->channel,
980 wl->beacon_int, wl->dtim_period);
981 if (ret < 0)
982 goto out_sleep;
983 }
984 }
985
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300986 if (changed & BSS_CHANGED_ASSOC) {
987 if (bss_conf->assoc) {
Kalle Valoe2fd4612009-08-07 13:34:12 +0300988 wl->beacon_int = bss_conf->beacon_int;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300989
Kalle Valof7f70572010-01-05 20:16:32 +0200990 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
991 if (!skb)
992 goto out_sleep;
993
994 ret = wl1251_cmd_template_set(wl, CMD_PS_POLL,
995 skb->data,
996 skb->len);
997 dev_kfree_skb(skb);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300998 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300999 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001000
Johannes Berg56007a02010-01-26 14:19:52 +01001001 ret = wl1251_acx_aid(wl, bss_conf->aid);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001002 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001003 goto out_sleep;
Kalle Valoe2fd4612009-08-07 13:34:12 +03001004 } else {
1005 /* use defaults when not associated */
1006 wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
1007 wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001008 }
1009 }
1010 if (changed & BSS_CHANGED_ERP_SLOT) {
1011 if (bss_conf->use_short_slot)
Kalle Valo80301cd2009-06-12 14:17:39 +03001012 ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001013 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001014 ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001015 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001016 wl1251_warning("Set slot time failed %d", ret);
Kalle Valoc5483b72009-06-12 14:16:32 +03001017 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001018 }
1019 }
1020
1021 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1022 if (bss_conf->use_short_preamble)
Kalle Valo80301cd2009-06-12 14:17:39 +03001023 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001024 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001025 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001026 }
1027
1028 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1029 if (bss_conf->use_cts_prot)
Kalle Valo80301cd2009-06-12 14:17:39 +03001030 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001031 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001032 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001033 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001034 wl1251_warning("Set ctsprotect failed %d", ret);
Kalle Valo80a112f2010-01-05 20:17:10 +02001035 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001036 }
1037 }
1038
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001039 if (changed & BSS_CHANGED_BEACON) {
1040 beacon = ieee80211_beacon_get(hw, vif);
Kalle Valo80301cd2009-06-12 14:17:39 +03001041 ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001042 beacon->len);
1043
1044 if (ret < 0) {
1045 dev_kfree_skb(beacon);
Kalle Valo80a112f2010-01-05 20:17:10 +02001046 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001047 }
1048
Kalle Valo80301cd2009-06-12 14:17:39 +03001049 ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001050 beacon->len);
1051
1052 dev_kfree_skb(beacon);
1053
1054 if (ret < 0)
Kalle Valo80a112f2010-01-05 20:17:10 +02001055 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001056
Kalle Valoae46ae12009-08-07 13:34:42 +03001057 ret = wl1251_join(wl, wl->bss_type, wl->beacon_int,
1058 wl->channel, wl->dtim_period);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001059
1060 if (ret < 0)
Kalle Valo80a112f2010-01-05 20:17:10 +02001061 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001062 }
1063
Kalle Valoc5483b72009-06-12 14:16:32 +03001064out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +03001065 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001066
1067out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001068 mutex_unlock(&wl->mutex);
1069}
1070
1071
1072/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001073static struct ieee80211_rate wl1251_rates[] = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001074 { .bitrate = 10,
1075 .hw_value = 0x1,
1076 .hw_value_short = 0x1, },
1077 { .bitrate = 20,
1078 .hw_value = 0x2,
1079 .hw_value_short = 0x2,
1080 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1081 { .bitrate = 55,
1082 .hw_value = 0x4,
1083 .hw_value_short = 0x4,
1084 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1085 { .bitrate = 110,
1086 .hw_value = 0x20,
1087 .hw_value_short = 0x20,
1088 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1089 { .bitrate = 60,
1090 .hw_value = 0x8,
1091 .hw_value_short = 0x8, },
1092 { .bitrate = 90,
1093 .hw_value = 0x10,
1094 .hw_value_short = 0x10, },
1095 { .bitrate = 120,
1096 .hw_value = 0x40,
1097 .hw_value_short = 0x40, },
1098 { .bitrate = 180,
1099 .hw_value = 0x80,
1100 .hw_value_short = 0x80, },
1101 { .bitrate = 240,
1102 .hw_value = 0x200,
1103 .hw_value_short = 0x200, },
1104 { .bitrate = 360,
1105 .hw_value = 0x400,
1106 .hw_value_short = 0x400, },
1107 { .bitrate = 480,
1108 .hw_value = 0x800,
1109 .hw_value_short = 0x800, },
1110 { .bitrate = 540,
1111 .hw_value = 0x1000,
1112 .hw_value_short = 0x1000, },
1113};
1114
1115/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001116static struct ieee80211_channel wl1251_channels[] = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001117 { .hw_value = 1, .center_freq = 2412},
1118 { .hw_value = 2, .center_freq = 2417},
1119 { .hw_value = 3, .center_freq = 2422},
1120 { .hw_value = 4, .center_freq = 2427},
1121 { .hw_value = 5, .center_freq = 2432},
1122 { .hw_value = 6, .center_freq = 2437},
1123 { .hw_value = 7, .center_freq = 2442},
1124 { .hw_value = 8, .center_freq = 2447},
1125 { .hw_value = 9, .center_freq = 2452},
1126 { .hw_value = 10, .center_freq = 2457},
1127 { .hw_value = 11, .center_freq = 2462},
1128 { .hw_value = 12, .center_freq = 2467},
1129 { .hw_value = 13, .center_freq = 2472},
1130};
1131
Kalle Valo86dff7a2009-11-30 10:18:19 +02001132static int wl1251_op_conf_tx(struct ieee80211_hw *hw, u16 queue,
1133 const struct ieee80211_tx_queue_params *params)
1134{
Kalle Valo4ff6ffa2010-01-12 10:43:15 +02001135 enum wl1251_acx_ps_scheme ps_scheme;
Kalle Valo86dff7a2009-11-30 10:18:19 +02001136 struct wl1251 *wl = hw->priv;
1137 int ret;
1138
1139 mutex_lock(&wl->mutex);
1140
1141 wl1251_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
1142
1143 ret = wl1251_ps_elp_wakeup(wl);
1144 if (ret < 0)
1145 goto out;
1146
Kalle Valo85359492010-02-04 15:33:25 +02001147 /* mac80211 uses units of 32 usec */
Kalle Valo86dff7a2009-11-30 10:18:19 +02001148 ret = wl1251_acx_ac_cfg(wl, wl1251_tx_get_queue(queue),
1149 params->cw_min, params->cw_max,
Kalle Valo85359492010-02-04 15:33:25 +02001150 params->aifs, params->txop * 32);
Kalle Valo27336f12009-11-30 10:18:27 +02001151 if (ret < 0)
1152 goto out_sleep;
Kalle Valo86dff7a2009-11-30 10:18:19 +02001153
Kalle Valo4ff6ffa2010-01-12 10:43:15 +02001154 if (params->uapsd)
1155 ps_scheme = WL1251_ACX_PS_SCHEME_UPSD_TRIGGER;
1156 else
1157 ps_scheme = WL1251_ACX_PS_SCHEME_LEGACY;
1158
Kalle Valo27336f12009-11-30 10:18:27 +02001159 ret = wl1251_acx_tid_cfg(wl, wl1251_tx_get_queue(queue),
Kalle Valo49e1b9f2009-11-30 10:18:33 +02001160 CHANNEL_TYPE_EDCF,
Kalle Valo4ff6ffa2010-01-12 10:43:15 +02001161 wl1251_tx_get_queue(queue), ps_scheme,
Kalle Valo27336f12009-11-30 10:18:27 +02001162 WL1251_ACX_ACK_POLICY_LEGACY);
1163 if (ret < 0)
1164 goto out_sleep;
1165
1166out_sleep:
Kalle Valo86dff7a2009-11-30 10:18:19 +02001167 wl1251_ps_elp_sleep(wl);
1168
1169out:
1170 mutex_unlock(&wl->mutex);
1171
1172 return ret;
1173}
1174
John W. Linville19434142010-07-28 15:23:30 -04001175static int wl1251_op_get_survey(struct ieee80211_hw *hw, int idx,
1176 struct survey_info *survey)
1177{
1178 struct wl1251 *wl = hw->priv;
1179 struct ieee80211_conf *conf = &hw->conf;
1180
1181 if (idx != 0)
1182 return -ENOENT;
1183
1184 survey->channel = conf->channel;
1185 survey->filled = SURVEY_INFO_NOISE_DBM;
1186 survey->noise = wl->noise;
1187
1188 return 0;
1189}
1190
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001191/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001192static struct ieee80211_supported_band wl1251_band_2ghz = {
1193 .channels = wl1251_channels,
1194 .n_channels = ARRAY_SIZE(wl1251_channels),
1195 .bitrates = wl1251_rates,
1196 .n_bitrates = ARRAY_SIZE(wl1251_rates),
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001197};
1198
Kalle Valo80301cd2009-06-12 14:17:39 +03001199static const struct ieee80211_ops wl1251_ops = {
1200 .start = wl1251_op_start,
1201 .stop = wl1251_op_stop,
1202 .add_interface = wl1251_op_add_interface,
1203 .remove_interface = wl1251_op_remove_interface,
1204 .config = wl1251_op_config,
1205 .configure_filter = wl1251_op_configure_filter,
1206 .tx = wl1251_op_tx,
1207 .set_key = wl1251_op_set_key,
1208 .hw_scan = wl1251_op_hw_scan,
1209 .bss_info_changed = wl1251_op_bss_info_changed,
1210 .set_rts_threshold = wl1251_op_set_rts_threshold,
Kalle Valo86dff7a2009-11-30 10:18:19 +02001211 .conf_tx = wl1251_op_conf_tx,
John W. Linville19434142010-07-28 15:23:30 -04001212 .get_survey = wl1251_op_get_survey,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001213};
1214
Grazvydas Ignotas61c2a802010-04-14 13:05:48 +03001215static int wl1251_read_eeprom_byte(struct wl1251 *wl, off_t offset, u8 *data)
1216{
1217 unsigned long timeout;
1218
1219 wl1251_reg_write32(wl, EE_ADDR, offset);
1220 wl1251_reg_write32(wl, EE_CTL, EE_CTL_READ);
1221
1222 /* EE_CTL_READ clears when data is ready */
1223 timeout = jiffies + msecs_to_jiffies(100);
1224 while (1) {
1225 if (!(wl1251_reg_read32(wl, EE_CTL) & EE_CTL_READ))
1226 break;
1227
1228 if (time_after(jiffies, timeout))
1229 return -ETIMEDOUT;
1230
1231 msleep(1);
1232 }
1233
1234 *data = wl1251_reg_read32(wl, EE_DATA);
1235 return 0;
1236}
1237
1238static int wl1251_read_eeprom(struct wl1251 *wl, off_t offset,
1239 u8 *data, size_t len)
1240{
1241 size_t i;
1242 int ret;
1243
1244 wl1251_reg_write32(wl, EE_START, 0);
1245
1246 for (i = 0; i < len; i++) {
1247 ret = wl1251_read_eeprom_byte(wl, offset + i, &data[i]);
1248 if (ret < 0)
1249 return ret;
1250 }
1251
1252 return 0;
1253}
1254
1255static int wl1251_read_eeprom_mac(struct wl1251 *wl)
1256{
1257 u8 mac[ETH_ALEN];
1258 int i, ret;
1259
1260 wl1251_set_partition(wl, 0, 0, REGISTERS_BASE, REGISTERS_DOWN_SIZE);
1261
1262 ret = wl1251_read_eeprom(wl, 0x1c, mac, sizeof(mac));
1263 if (ret < 0) {
1264 wl1251_warning("failed to read MAC address from EEPROM");
1265 return ret;
1266 }
1267
1268 /* MAC is stored in reverse order */
1269 for (i = 0; i < ETH_ALEN; i++)
1270 wl->mac_addr[i] = mac[ETH_ALEN - i - 1];
1271
1272 return 0;
1273}
1274
Kalle Valo80301cd2009-06-12 14:17:39 +03001275static int wl1251_register_hw(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001276{
1277 int ret;
1278
1279 if (wl->mac80211_registered)
1280 return 0;
1281
1282 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1283
1284 ret = ieee80211_register_hw(wl->hw);
1285 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001286 wl1251_error("unable to register mac80211 hw: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001287 return ret;
1288 }
1289
1290 wl->mac80211_registered = true;
1291
Kalle Valo80301cd2009-06-12 14:17:39 +03001292 wl1251_notice("loaded");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001293
1294 return 0;
1295}
1296
Bob Copeland8e639c02009-08-07 13:33:26 +03001297int wl1251_init_ieee80211(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001298{
Bob Copeland8e639c02009-08-07 13:33:26 +03001299 int ret;
1300
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001301 /* The tx descriptor buffer and the TKIP space */
1302 wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +03001303 + WL1251_TKIP_IV_SPACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001304
1305 /* unit us */
1306 /* FIXME: find a proper value */
1307 wl->hw->channel_change_time = 10000;
1308
1309 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
Juuso Oikarinen6b21a2c2009-11-17 18:48:30 +02001310 IEEE80211_HW_SUPPORTS_PS |
Kalle Valo4ff6ffa2010-01-12 10:43:15 +02001311 IEEE80211_HW_BEACON_FILTER |
1312 IEEE80211_HW_SUPPORTS_UAPSD;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001313
1314 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1315 wl->hw->wiphy->max_scan_ssids = 1;
Kalle Valo80301cd2009-06-12 14:17:39 +03001316 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1251_band_2ghz;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001317
Kalle Valoda2fb4e2009-11-30 10:18:47 +02001318 wl->hw->queues = 4;
1319
Grazvydas Ignotas61c2a802010-04-14 13:05:48 +03001320 if (wl->use_eeprom)
1321 wl1251_read_eeprom_mac(wl);
1322
Bob Copeland8e639c02009-08-07 13:33:26 +03001323 ret = wl1251_register_hw(wl);
1324 if (ret)
1325 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001326
Bob Copeland8e639c02009-08-07 13:33:26 +03001327 wl1251_debugfs_init(wl);
1328 wl1251_notice("initialized");
1329
1330 ret = 0;
1331
1332out:
1333 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001334}
Bob Copelandaf8c78e2009-08-07 13:33:34 +03001335EXPORT_SYMBOL_GPL(wl1251_init_ieee80211);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001336
Bob Copeland8e639c02009-08-07 13:33:26 +03001337struct ieee80211_hw *wl1251_alloc_hw(void)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001338{
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001339 struct ieee80211_hw *hw;
Kalle Valo80301cd2009-06-12 14:17:39 +03001340 struct wl1251 *wl;
Bob Copeland8e639c02009-08-07 13:33:26 +03001341 int i;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001342 static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1343
Kalle Valo80301cd2009-06-12 14:17:39 +03001344 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001345 if (!hw) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001346 wl1251_error("could not alloc ieee80211_hw");
Bob Copeland8e639c02009-08-07 13:33:26 +03001347 return ERR_PTR(-ENOMEM);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001348 }
1349
1350 wl = hw->priv;
1351 memset(wl, 0, sizeof(*wl));
1352
1353 wl->hw = hw;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001354
1355 wl->data_in_count = 0;
1356
1357 skb_queue_head_init(&wl->tx_queue);
1358
Kalle Valo80301cd2009-06-12 14:17:39 +03001359 INIT_WORK(&wl->filter_work, wl1251_filter_work);
Juuso Oikarinend5da79a2009-11-17 18:48:37 +02001360 INIT_DELAYED_WORK(&wl->elp_work, wl1251_elp_work);
Kalle Valo80301cd2009-06-12 14:17:39 +03001361 wl->channel = WL1251_DEFAULT_CHANNEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001362 wl->scanning = false;
1363 wl->default_key = 0;
1364 wl->listen_int = 1;
1365 wl->rx_counter = 0;
1366 wl->rx_handled = 0;
1367 wl->rx_current_buffer = 0;
1368 wl->rx_last_id = 0;
Kalle Valo80301cd2009-06-12 14:17:39 +03001369 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
1370 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001371 wl->elp = false;
1372 wl->psm = 0;
1373 wl->psm_requested = false;
1374 wl->tx_queue_stopped = false;
Kalle Valo80301cd2009-06-12 14:17:39 +03001375 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
Kalle Valoe2fd4612009-08-07 13:34:12 +03001376 wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
1377 wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
Juuso Oikarinen287f6f92009-11-17 18:48:23 +02001378 wl->vif = NULL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001379
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001380 for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
1381 wl->tx_frames[i] = NULL;
1382
1383 wl->next_tx_complete = 0;
1384
Kalle Valo0e71bb02009-08-07 13:33:57 +03001385 INIT_WORK(&wl->irq_work, wl1251_irq_work);
1386 INIT_WORK(&wl->tx_work, wl1251_tx_work);
1387
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001388 /*
1389 * In case our MAC address is not correctly set,
1390 * we use a random but Nokia MAC.
1391 */
1392 memcpy(wl->mac_addr, nokia_oui, 3);
1393 get_random_bytes(wl->mac_addr + 3, 3);
1394
Kalle Valo80301cd2009-06-12 14:17:39 +03001395 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001396 mutex_init(&wl->mutex);
1397
1398 wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
1399 wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
1400
Kalle Valo47212132009-06-12 14:15:08 +03001401 wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1402 if (!wl->rx_descriptor) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001403 wl1251_error("could not allocate memory for rx descriptor");
Bob Copeland8e639c02009-08-07 13:33:26 +03001404 ieee80211_free_hw(hw);
1405 return ERR_PTR(-ENOMEM);
Kalle Valo47212132009-06-12 14:15:08 +03001406 }
1407
Bob Copeland8e639c02009-08-07 13:33:26 +03001408 return hw;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001409}
Bob Copelandaf8c78e2009-08-07 13:33:34 +03001410EXPORT_SYMBOL_GPL(wl1251_alloc_hw);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001411
Bob Copeland8e639c02009-08-07 13:33:26 +03001412int wl1251_free_hw(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001413{
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001414 ieee80211_unregister_hw(wl->hw);
1415
Kalle Valo80301cd2009-06-12 14:17:39 +03001416 wl1251_debugfs_exit(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001417
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001418 kfree(wl->target_mem_map);
1419 kfree(wl->data_path);
Kalle Valoc74ddfd2009-11-17 18:48:45 +02001420 vfree(wl->fw);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001421 wl->fw = NULL;
1422 kfree(wl->nvs);
1423 wl->nvs = NULL;
Kalle Valo47212132009-06-12 14:15:08 +03001424
1425 kfree(wl->rx_descriptor);
1426 wl->rx_descriptor = NULL;
1427
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001428 ieee80211_free_hw(wl->hw);
1429
1430 return 0;
1431}
Bob Copelandaf8c78e2009-08-07 13:33:34 +03001432EXPORT_SYMBOL_GPL(wl1251_free_hw);
1433
1434MODULE_DESCRIPTION("TI wl1251 Wireles LAN Driver Core");
1435MODULE_LICENSE("GPL");
Kalle Valo31c726f2010-08-22 22:46:15 +03001436MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
Ben Hutchings49f146d2009-11-07 22:02:15 +00001437MODULE_FIRMWARE(WL1251_FW_NAME);