blob: a6d8d1ad5dc0105a16e92d8d27b1f542e2833908 [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>
Kalle Valo2f01a1f2009-04-29 23:33:31 +030029#include <linux/crc32.h>
30#include <linux/etherdevice.h>
Kalle Valoc74ddfd2009-11-17 18:48:45 +020031#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Kalle Valo2f01a1f2009-04-29 23:33:31 +030033
Kalle Valo13674112009-06-12 14:17:25 +030034#include "wl1251.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030035#include "wl12xx_80211.h"
Kalle Valo29d904c2009-08-07 13:35:11 +030036#include "wl1251_reg.h"
Bob Copeland0764de62009-08-07 13:32:56 +030037#include "wl1251_io.h"
Bob Copeland8e639c02009-08-07 13:33:26 +030038#include "wl1251_cmd.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030039#include "wl1251_event.h"
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +030040#include "wl1251_tx.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030041#include "wl1251_rx.h"
42#include "wl1251_ps.h"
43#include "wl1251_init.h"
44#include "wl1251_debugfs.h"
Kalle Valo0e71bb02009-08-07 13:33:57 +030045#include "wl1251_boot.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030046
Bob Copelandb5ed9c12009-08-07 13:33:49 +030047void wl1251_enable_interrupts(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030048{
Bob Copelandb5ed9c12009-08-07 13:33:49 +030049 wl->if_ops->enable_irq(wl);
50}
51
52void wl1251_disable_interrupts(struct wl1251 *wl)
53{
54 wl->if_ops->disable_irq(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030055}
56
Kalle Valo80301cd2009-06-12 14:17:39 +030057static void wl1251_power_off(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030058{
59 wl->set_power(false);
60}
61
Kalle Valo80301cd2009-06-12 14:17:39 +030062static void wl1251_power_on(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030063{
64 wl->set_power(true);
65}
66
Kalle Valo80301cd2009-06-12 14:17:39 +030067static int wl1251_fetch_firmware(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030068{
69 const struct firmware *fw;
Bob Copeland6c766f42009-08-07 13:33:04 +030070 struct device *dev = wiphy_dev(wl->hw->wiphy);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030071 int ret;
72
Kalle Valo0e71bb02009-08-07 13:33:57 +030073 ret = request_firmware(&fw, WL1251_FW_NAME, dev);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030074
75 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +030076 wl1251_error("could not get firmware: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030077 return ret;
78 }
79
80 if (fw->size % 4) {
Kalle Valo80301cd2009-06-12 14:17:39 +030081 wl1251_error("firmware size is not multiple of 32 bits: %zu",
Kalle Valo2f01a1f2009-04-29 23:33:31 +030082 fw->size);
83 ret = -EILSEQ;
84 goto out;
85 }
86
87 wl->fw_len = fw->size;
Kalle Valoc74ddfd2009-11-17 18:48:45 +020088 wl->fw = vmalloc(wl->fw_len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030089
90 if (!wl->fw) {
Kalle Valo80301cd2009-06-12 14:17:39 +030091 wl1251_error("could not allocate memory for the firmware");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030092 ret = -ENOMEM;
93 goto out;
94 }
95
96 memcpy(wl->fw, fw->data, wl->fw_len);
97
98 ret = 0;
99
100out:
101 release_firmware(fw);
102
103 return ret;
104}
105
Kalle Valo80301cd2009-06-12 14:17:39 +0300106static int wl1251_fetch_nvs(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300107{
108 const struct firmware *fw;
Bob Copeland6c766f42009-08-07 13:33:04 +0300109 struct device *dev = wiphy_dev(wl->hw->wiphy);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300110 int ret;
111
Kalle Valo0e71bb02009-08-07 13:33:57 +0300112 ret = request_firmware(&fw, WL1251_NVS_NAME, dev);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300113
114 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300115 wl1251_error("could not get nvs file: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300116 return ret;
117 }
118
119 if (fw->size % 4) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300120 wl1251_error("nvs size is not multiple of 32 bits: %zu",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300121 fw->size);
122 ret = -EILSEQ;
123 goto out;
124 }
125
126 wl->nvs_len = fw->size;
Julia Lawall80caf602010-05-15 23:15:10 +0200127 wl->nvs = kmemdup(fw->data, wl->nvs_len, GFP_KERNEL);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300128
129 if (!wl->nvs) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300130 wl1251_error("could not allocate memory for the nvs file");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300131 ret = -ENOMEM;
132 goto out;
133 }
134
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300135 ret = 0;
136
137out:
138 release_firmware(fw);
139
140 return ret;
141}
142
Kalle Valo80301cd2009-06-12 14:17:39 +0300143static void wl1251_fw_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300144{
145 u32 elp_reg;
146
147 elp_reg = ELPCTRL_WAKE_UP;
Grazvydas Ignotas3f9e7502010-03-11 17:44:57 +0200148 wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
149 elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300150
Kalle Valo05fac682009-06-12 14:17:47 +0300151 if (!(elp_reg & ELPCTRL_WLAN_READY))
Kalle Valo80301cd2009-06-12 14:17:39 +0300152 wl1251_warning("WLAN not ready");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300153}
154
Kalle Valo80301cd2009-06-12 14:17:39 +0300155static int wl1251_chip_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300156{
157 int ret = 0;
158
Kalle Valo80301cd2009-06-12 14:17:39 +0300159 wl1251_power_on(wl);
Kalle Valo0e71bb02009-08-07 13:33:57 +0300160 msleep(WL1251_POWER_ON_SLEEP);
Bob Copeland08d9f5722009-08-07 13:33:11 +0300161 wl->if_ops->reset(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300162
163 /* We don't need a real memory partition here, because we only want
164 * to use the registers at this point. */
Kalle Valo80301cd2009-06-12 14:17:39 +0300165 wl1251_set_partition(wl,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300166 0x00000000,
167 0x00000000,
168 REGISTERS_BASE,
169 REGISTERS_DOWN_SIZE);
170
171 /* ELP module wake up */
Kalle Valo80301cd2009-06-12 14:17:39 +0300172 wl1251_fw_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300173
174 /* whal_FwCtrl_BootSm() */
175
176 /* 0. read chip id from CHIP_ID */
Kalle Valo0e71bb02009-08-07 13:33:57 +0300177 wl->chip_id = wl1251_reg_read32(wl, CHIP_ID_B);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300178
179 /* 1. check if chip id is valid */
180
Kalle Valo0e71bb02009-08-07 13:33:57 +0300181 switch (wl->chip_id) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300182 case CHIP_ID_1251_PG12:
Kalle Valo80301cd2009-06-12 14:17:39 +0300183 wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG12)",
Kalle Valo0e71bb02009-08-07 13:33:57 +0300184 wl->chip_id);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300185 break;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300186 case CHIP_ID_1251_PG11:
David-John Willis2c759e02009-10-15 14:38:16 +0100187 wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG11)",
188 wl->chip_id);
189 break;
John W. Linville9a493e82009-10-27 15:15:05 -0400190 case CHIP_ID_1251_PG10:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300191 default:
Kalle Valo0e71bb02009-08-07 13:33:57 +0300192 wl1251_error("unsupported chip id: 0x%x", wl->chip_id);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300193 ret = -ENODEV;
194 goto out;
195 }
196
197 if (wl->fw == NULL) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300198 ret = wl1251_fetch_firmware(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300199 if (ret < 0)
200 goto out;
201 }
202
Grazvydas Ignotasafa5ec22010-04-13 01:21:40 +0300203 if (wl->nvs == NULL && !wl->use_eeprom) {
204 /* No NVS from netlink, try to get it from the filesystem */
Kalle Valo80301cd2009-06-12 14:17:39 +0300205 ret = wl1251_fetch_nvs(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300206 if (ret < 0)
207 goto out;
208 }
209
210out:
211 return ret;
212}
213
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200214#define WL1251_IRQ_LOOP_COUNT 10
Kalle Valo0e71bb02009-08-07 13:33:57 +0300215static void wl1251_irq_work(struct work_struct *work)
216{
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200217 u32 intr, ctr = WL1251_IRQ_LOOP_COUNT;
Kalle Valo0e71bb02009-08-07 13:33:57 +0300218 struct wl1251 *wl =
219 container_of(work, struct wl1251, irq_work);
220 int ret;
221
222 mutex_lock(&wl->mutex);
223
224 wl1251_debug(DEBUG_IRQ, "IRQ work");
225
226 if (wl->state == WL1251_STATE_OFF)
227 goto out;
228
229 ret = wl1251_ps_elp_wakeup(wl);
230 if (ret < 0)
231 goto out;
232
233 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, WL1251_ACX_INTR_ALL);
234
235 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
236 wl1251_debug(DEBUG_IRQ, "intr: 0x%x", intr);
237
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200238 do {
239 if (wl->data_path) {
240 wl->rx_counter = wl1251_mem_read32(
241 wl, wl->data_path->rx_control_addr);
Kalle Valo0e71bb02009-08-07 13:33:57 +0300242
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200243 /* We handle a frmware bug here */
244 switch ((wl->rx_counter - wl->rx_handled) & 0xf) {
245 case 0:
246 wl1251_debug(DEBUG_IRQ,
247 "RX: FW and host in sync");
248 intr &= ~WL1251_ACX_INTR_RX0_DATA;
249 intr &= ~WL1251_ACX_INTR_RX1_DATA;
250 break;
251 case 1:
252 wl1251_debug(DEBUG_IRQ, "RX: FW +1");
253 intr |= WL1251_ACX_INTR_RX0_DATA;
254 intr &= ~WL1251_ACX_INTR_RX1_DATA;
255 break;
256 case 2:
257 wl1251_debug(DEBUG_IRQ, "RX: FW +2");
258 intr |= WL1251_ACX_INTR_RX0_DATA;
259 intr |= WL1251_ACX_INTR_RX1_DATA;
260 break;
261 default:
262 wl1251_warning(
263 "RX: FW and host out of sync: %d",
264 wl->rx_counter - wl->rx_handled);
265 break;
266 }
267
268 wl->rx_handled = wl->rx_counter;
269
270 wl1251_debug(DEBUG_IRQ, "RX counter: %d",
271 wl->rx_counter);
Kalle Valo0e71bb02009-08-07 13:33:57 +0300272 }
273
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200274 intr &= wl->intr_mask;
Kalle Valo0e71bb02009-08-07 13:33:57 +0300275
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200276 if (intr == 0) {
277 wl1251_debug(DEBUG_IRQ, "INTR is 0");
278 goto out_sleep;
279 }
Kalle Valo0e71bb02009-08-07 13:33:57 +0300280
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200281 if (intr & WL1251_ACX_INTR_RX0_DATA) {
282 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX0_DATA");
283 wl1251_rx(wl);
284 }
Kalle Valo0e71bb02009-08-07 13:33:57 +0300285
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200286 if (intr & WL1251_ACX_INTR_RX1_DATA) {
287 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX1_DATA");
288 wl1251_rx(wl);
289 }
Kalle Valo0e71bb02009-08-07 13:33:57 +0300290
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200291 if (intr & WL1251_ACX_INTR_TX_RESULT) {
292 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_TX_RESULT");
293 wl1251_tx_complete(wl);
294 }
Kalle Valo0e71bb02009-08-07 13:33:57 +0300295
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200296 if (intr & (WL1251_ACX_INTR_EVENT_A |
297 WL1251_ACX_INTR_EVENT_B)) {
298 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT (0x%x)",
299 intr);
300 if (intr & WL1251_ACX_INTR_EVENT_A)
301 wl1251_event_handle(wl, 0);
302 else
303 wl1251_event_handle(wl, 1);
304 }
Kalle Valo0e71bb02009-08-07 13:33:57 +0300305
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200306 if (intr & WL1251_ACX_INTR_INIT_COMPLETE)
307 wl1251_debug(DEBUG_IRQ,
308 "WL1251_ACX_INTR_INIT_COMPLETE");
Kalle Valo0e71bb02009-08-07 13:33:57 +0300309
Juuso Oikarinen79553f82009-11-17 18:49:46 +0200310 if (--ctr == 0)
311 break;
Kalle Valo0e71bb02009-08-07 13:33:57 +0300312
Juuso Oikarinen79553f82009-11-17 18:49:46 +0200313 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
314 } while (intr);
Kalle Valo0e71bb02009-08-07 13:33:57 +0300315
316out_sleep:
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200317 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, ~(wl->intr_mask));
Kalle Valo0e71bb02009-08-07 13:33:57 +0300318 wl1251_ps_elp_sleep(wl);
319
320out:
321 mutex_unlock(&wl->mutex);
322}
323
Kalle Valoae46ae12009-08-07 13:34:42 +0300324static int wl1251_join(struct wl1251 *wl, u8 bss_type, u8 channel,
325 u16 beacon_interval, u8 dtim_period)
326{
327 int ret;
328
329 ret = wl1251_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
330 DEFAULT_HW_GEN_MODULATION_TYPE,
331 wl->tx_mgmt_frm_rate,
332 wl->tx_mgmt_frm_mod);
333 if (ret < 0)
334 goto out;
335
336
337 ret = wl1251_cmd_join(wl, bss_type, channel, beacon_interval,
338 dtim_period);
339 if (ret < 0)
340 goto out;
341
342 /*
343 * FIXME: we should wait for JOIN_EVENT_COMPLETE_ID but to simplify
344 * locking we just sleep instead, for now
345 */
346 msleep(10);
347
348out:
349 return ret;
350}
351
Kalle Valo80301cd2009-06-12 14:17:39 +0300352static void wl1251_filter_work(struct work_struct *work)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300353{
Kalle Valo80301cd2009-06-12 14:17:39 +0300354 struct wl1251 *wl =
355 container_of(work, struct wl1251, filter_work);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300356 int ret;
357
358 mutex_lock(&wl->mutex);
359
Kalle Valo80301cd2009-06-12 14:17:39 +0300360 if (wl->state == WL1251_STATE_OFF)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300361 goto out;
362
Kalle Valo80301cd2009-06-12 14:17:39 +0300363 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300364 if (ret < 0)
365 goto out;
366
Kalle Valoae46ae12009-08-07 13:34:42 +0300367 ret = wl1251_join(wl, wl->bss_type, wl->channel, wl->beacon_int,
368 wl->dtim_period);
Kalle Valoc5483b72009-06-12 14:16:32 +0300369 if (ret < 0)
370 goto out_sleep;
371
372out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300373 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300374
375out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300376 mutex_unlock(&wl->mutex);
377}
378
Kalle Valo80301cd2009-06-12 14:17:39 +0300379static int wl1251_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300380{
Kalle Valo80301cd2009-06-12 14:17:39 +0300381 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300382
383 skb_queue_tail(&wl->tx_queue, skb);
384
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +0300385 /*
386 * The chip specific setup must run before the first TX packet -
387 * before that, the tx_work will not be initialized!
388 */
389
Kalle Valo16e711f2009-08-07 13:35:04 +0300390 ieee80211_queue_work(wl->hw, &wl->tx_work);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300391
392 /*
393 * The workqueue is slow to process the tx_queue and we need stop
394 * the queue here, otherwise the queue will get too long.
395 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300396 if (skb_queue_len(&wl->tx_queue) >= WL1251_TX_QUEUE_MAX_LENGTH) {
Kalle Valod67e2612009-11-30 10:17:45 +0200397 wl1251_debug(DEBUG_TX, "op_tx: tx_queue full, stop queues");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300398 ieee80211_stop_queues(wl->hw);
399
400 /*
401 * FIXME: this is racy, the variable is not properly
402 * protected. Maybe fix this by removing the stupid
403 * variable altogether and checking the real queue state?
404 */
405 wl->tx_queue_stopped = true;
406 }
407
408 return NETDEV_TX_OK;
409}
410
Kalle Valo80301cd2009-06-12 14:17:39 +0300411static int wl1251_op_start(struct ieee80211_hw *hw)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300412{
Kalle Valo80301cd2009-06-12 14:17:39 +0300413 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300414 int ret = 0;
415
Kalle Valo80301cd2009-06-12 14:17:39 +0300416 wl1251_debug(DEBUG_MAC80211, "mac80211 start");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300417
418 mutex_lock(&wl->mutex);
419
Kalle Valo80301cd2009-06-12 14:17:39 +0300420 if (wl->state != WL1251_STATE_OFF) {
421 wl1251_error("cannot start because not in off state: %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300422 wl->state);
423 ret = -EBUSY;
424 goto out;
425 }
426
Kalle Valo80301cd2009-06-12 14:17:39 +0300427 ret = wl1251_chip_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300428 if (ret < 0)
Jiri Slabyfe643412009-07-14 22:37:13 +0200429 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300430
Kalle Valo0e71bb02009-08-07 13:33:57 +0300431 ret = wl1251_boot(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300432 if (ret < 0)
433 goto out;
434
Kalle Valo0e71bb02009-08-07 13:33:57 +0300435 ret = wl1251_hw_init(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300436 if (ret < 0)
437 goto out;
438
Kalle Valo80301cd2009-06-12 14:17:39 +0300439 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300440 if (ret < 0)
441 goto out;
442
Kalle Valo80301cd2009-06-12 14:17:39 +0300443 wl->state = WL1251_STATE_ON;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300444
Kalle Valo0e71bb02009-08-07 13:33:57 +0300445 wl1251_info("firmware booted (%s)", wl->fw_ver);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300446
447out:
448 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300449 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300450
451 mutex_unlock(&wl->mutex);
452
453 return ret;
454}
455
Kalle Valo80301cd2009-06-12 14:17:39 +0300456static void wl1251_op_stop(struct ieee80211_hw *hw)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300457{
Kalle Valo80301cd2009-06-12 14:17:39 +0300458 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300459
Kalle Valo80301cd2009-06-12 14:17:39 +0300460 wl1251_info("down");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300461
Kalle Valo80301cd2009-06-12 14:17:39 +0300462 wl1251_debug(DEBUG_MAC80211, "mac80211 stop");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300463
464 mutex_lock(&wl->mutex);
465
Kalle Valo80301cd2009-06-12 14:17:39 +0300466 WARN_ON(wl->state != WL1251_STATE_ON);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300467
468 if (wl->scanning) {
469 mutex_unlock(&wl->mutex);
470 ieee80211_scan_completed(wl->hw, true);
471 mutex_lock(&wl->mutex);
472 wl->scanning = false;
473 }
474
Kalle Valo80301cd2009-06-12 14:17:39 +0300475 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300476
Kalle Valo80301cd2009-06-12 14:17:39 +0300477 wl1251_disable_interrupts(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300478
479 mutex_unlock(&wl->mutex);
480
481 cancel_work_sync(&wl->irq_work);
482 cancel_work_sync(&wl->tx_work);
483 cancel_work_sync(&wl->filter_work);
484
485 mutex_lock(&wl->mutex);
486
487 /* let's notify MAC80211 about the remaining pending TX frames */
Kalle Valo0e71bb02009-08-07 13:33:57 +0300488 wl1251_tx_flush(wl);
Kalle Valo80301cd2009-06-12 14:17:39 +0300489 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300490
491 memset(wl->bssid, 0, ETH_ALEN);
492 wl->listen_int = 1;
493 wl->bss_type = MAX_BSS_TYPE;
494
495 wl->data_in_count = 0;
496 wl->rx_counter = 0;
497 wl->rx_handled = 0;
498 wl->rx_current_buffer = 0;
499 wl->rx_last_id = 0;
500 wl->next_tx_complete = 0;
501 wl->elp = false;
502 wl->psm = 0;
503 wl->tx_queue_stopped = false;
Kalle Valo80301cd2009-06-12 14:17:39 +0300504 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
Kalle Valo97802792009-08-07 13:34:27 +0300505 wl->channel = WL1251_DEFAULT_CHANNEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300506
Kalle Valo80301cd2009-06-12 14:17:39 +0300507 wl1251_debugfs_reset(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300508
509 mutex_unlock(&wl->mutex);
510}
511
Kalle Valo80301cd2009-06-12 14:17:39 +0300512static int wl1251_op_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100513 struct ieee80211_vif *vif)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300514{
Kalle Valo80301cd2009-06-12 14:17:39 +0300515 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300516 int ret = 0;
517
Johannes Berge91d8332009-07-15 17:21:41 +0200518 wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
Johannes Berg1ed32e42009-12-23 13:15:45 +0100519 vif->type, vif->addr);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300520
521 mutex_lock(&wl->mutex);
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200522 if (wl->vif) {
523 ret = -EBUSY;
524 goto out;
525 }
526
Johannes Berg1ed32e42009-12-23 13:15:45 +0100527 wl->vif = vif;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300528
Johannes Berg1ed32e42009-12-23 13:15:45 +0100529 switch (vif->type) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300530 case NL80211_IFTYPE_STATION:
531 wl->bss_type = BSS_TYPE_STA_BSS;
532 break;
533 case NL80211_IFTYPE_ADHOC:
534 wl->bss_type = BSS_TYPE_IBSS;
535 break;
536 default:
537 ret = -EOPNOTSUPP;
538 goto out;
539 }
540
Johannes Berg1ed32e42009-12-23 13:15:45 +0100541 if (memcmp(wl->mac_addr, vif->addr, ETH_ALEN)) {
542 memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300543 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
Kalle Valo80301cd2009-06-12 14:17:39 +0300544 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300545 if (ret < 0)
546 goto out;
547 }
548
549out:
550 mutex_unlock(&wl->mutex);
551 return ret;
552}
553
Kalle Valo80301cd2009-06-12 14:17:39 +0300554static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100555 struct ieee80211_vif *vif)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300556{
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200557 struct wl1251 *wl = hw->priv;
558
559 mutex_lock(&wl->mutex);
Kalle Valo80301cd2009-06-12 14:17:39 +0300560 wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200561 wl->vif = NULL;
562 mutex_unlock(&wl->mutex);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300563}
564
Kalle Valo8f8ff9162010-01-12 10:43:07 +0200565static int wl1251_build_qos_null_data(struct wl1251 *wl)
566{
567 struct ieee80211_qos_hdr template;
568
569 memset(&template, 0, sizeof(template));
570
571 memcpy(template.addr1, wl->bssid, ETH_ALEN);
572 memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
573 memcpy(template.addr3, wl->bssid, ETH_ALEN);
574
575 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
576 IEEE80211_STYPE_QOS_NULLFUNC |
577 IEEE80211_FCTL_TODS);
578
579 /* FIXME: not sure what priority to use here */
580 template.qos_ctrl = cpu_to_le16(0);
581
582 return wl1251_cmd_template_set(wl, CMD_QOS_NULL_DATA, &template,
583 sizeof(template));
584}
585
Kalle Valo80301cd2009-06-12 14:17:39 +0300586static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300587{
Kalle Valo80301cd2009-06-12 14:17:39 +0300588 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300589 struct ieee80211_conf *conf = &hw->conf;
590 int channel, ret = 0;
591
592 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
593
Kalle Valo80301cd2009-06-12 14:17:39 +0300594 wl1251_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300595 channel,
596 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
597 conf->power_level);
598
599 mutex_lock(&wl->mutex);
600
Kalle Valo80301cd2009-06-12 14:17:39 +0300601 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300602 if (ret < 0)
603 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300604
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300605 if (channel != wl->channel) {
Kalle Valofe9a9842009-08-07 13:34:49 +0300606 wl->channel = channel;
607
Kalle Valoae46ae12009-08-07 13:34:42 +0300608 ret = wl1251_join(wl, wl->bss_type, wl->channel,
609 wl->beacon_int, wl->dtim_period);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300610 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300611 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300612 }
613
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300614 if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
Luciano Coelho47af3fe2009-06-12 14:17:53 +0300615 wl1251_debug(DEBUG_PSM, "psm enabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300616
617 wl->psm_requested = true;
618
Johannes Berg56007a02010-01-26 14:19:52 +0100619 wl->dtim_period = conf->ps_dtim_period;
620
621 ret = wl1251_acx_wr_tbtt_and_dtim(wl, wl->beacon_int,
622 wl->dtim_period);
623
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300624 /*
Johannes Berg56007a02010-01-26 14:19:52 +0100625 * mac80211 enables PSM only if we're already associated.
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300626 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300627 ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
Kalle Valo478fdf22009-11-30 10:17:52 +0200628 if (ret < 0)
629 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300630 } else if (!(conf->flags & IEEE80211_CONF_PS) &&
631 wl->psm_requested) {
Luciano Coelho47af3fe2009-06-12 14:17:53 +0300632 wl1251_debug(DEBUG_PSM, "psm disabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300633
634 wl->psm_requested = false;
635
Kalle Valo478fdf22009-11-30 10:17:52 +0200636 if (wl->psm) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300637 ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
Kalle Valo478fdf22009-11-30 10:17:52 +0200638 if (ret < 0)
639 goto out_sleep;
640 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300641 }
642
643 if (conf->power_level != wl->power_level) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300644 ret = wl1251_acx_tx_power(wl, conf->power_level);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300645 if (ret < 0)
Kalle Valo478fdf22009-11-30 10:17:52 +0200646 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300647
648 wl->power_level = conf->power_level;
649 }
650
Kalle Valoc5483b72009-06-12 14:16:32 +0300651out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300652 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300653
654out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300655 mutex_unlock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +0300656
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300657 return ret;
658}
659
Kalle Valo80301cd2009-06-12 14:17:39 +0300660#define WL1251_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300661 FIF_ALLMULTI | \
662 FIF_FCSFAIL | \
663 FIF_BCN_PRBRESP_PROMISC | \
664 FIF_CONTROL | \
665 FIF_OTHER_BSS)
666
Kalle Valo80301cd2009-06-12 14:17:39 +0300667static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300668 unsigned int changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +0200669 unsigned int *total,u64 multicast)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300670{
Kalle Valo80301cd2009-06-12 14:17:39 +0300671 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300672
Kalle Valo80301cd2009-06-12 14:17:39 +0300673 wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300674
Kalle Valo80301cd2009-06-12 14:17:39 +0300675 *total &= WL1251_SUPPORTED_FILTERS;
676 changed &= WL1251_SUPPORTED_FILTERS;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300677
678 if (changed == 0)
679 /* no filters which we support changed */
680 return;
681
682 /* FIXME: wl->rx_config and wl->rx_filter are not protected */
683
Kalle Valo80301cd2009-06-12 14:17:39 +0300684 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
685 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300686
687 if (*total & FIF_PROMISC_IN_BSS) {
688 wl->rx_config |= CFG_BSSID_FILTER_EN;
689 wl->rx_config |= CFG_RX_ALL_GOOD;
690 }
691 if (*total & FIF_ALLMULTI)
692 /*
693 * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
694 * all multicast frames
695 */
696 wl->rx_config &= ~CFG_MC_FILTER_EN;
697 if (*total & FIF_FCSFAIL)
698 wl->rx_filter |= CFG_RX_FCS_ERROR;
699 if (*total & FIF_BCN_PRBRESP_PROMISC) {
700 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
701 wl->rx_config &= ~CFG_SSID_FILTER_EN;
702 }
703 if (*total & FIF_CONTROL)
704 wl->rx_filter |= CFG_RX_CTL_EN;
705 if (*total & FIF_OTHER_BSS)
706 wl->rx_filter &= ~CFG_BSSID_FILTER_EN;
707
708 /*
709 * FIXME: workqueues need to be properly cancelled on stop(), for
710 * now let's just disable changing the filter settings. They will
711 * be updated any on config().
712 */
713 /* schedule_work(&wl->filter_work); */
714}
715
716/* HW encryption */
Kalle Valo80301cd2009-06-12 14:17:39 +0300717static int wl1251_set_key_type(struct wl1251 *wl,
718 struct wl1251_cmd_set_keys *key,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300719 enum set_key_cmd cmd,
720 struct ieee80211_key_conf *mac80211_key,
721 const u8 *addr)
722{
723 switch (mac80211_key->alg) {
724 case ALG_WEP:
725 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;
732 case ALG_TKIP:
733 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;
740 case ALG_CCMP:
741 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:
Kalle Valo80301cd2009-06-12 14:17:39 +0300748 wl1251_error("Unknown key algo 0x%x", mac80211_key->alg);
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",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300781 key->alg, 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 Valo8f8ff9162010-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 Valob1b0a2b2009-08-07 13:35:19 +03001436MODULE_AUTHOR("Kalle Valo <kalle.valo@nokia.com>");
Ben Hutchings49f146d2009-11-07 22:02:15 +00001437MODULE_FIRMWARE(WL1251_FW_NAME);