blob: c81e95b45c14a6a8033475c874e6589b858f4dc4 [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
Grazvydas Ignotasd41776fa2010-08-17 22:46:53 +0300296 if (intr & WL1251_ACX_INTR_EVENT_A) {
297 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT_A");
298 wl1251_event_handle(wl, 0);
299 }
300
301 if (intr & WL1251_ACX_INTR_EVENT_B) {
302 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT_B");
303 wl1251_event_handle(wl, 1);
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200304 }
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
Grazvydas Ignotas7273b972010-08-17 22:46:55 +0300342 ret = wl1251_event_wait(wl, JOIN_EVENT_COMPLETE_ID, 100);
343 if (ret < 0)
344 wl1251_warning("join timeout");
Kalle Valoae46ae12009-08-07 13:34:42 +0300345
346out:
347 return ret;
348}
349
Kalle Valo80301cd2009-06-12 14:17:39 +0300350static void wl1251_filter_work(struct work_struct *work)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300351{
Kalle Valo80301cd2009-06-12 14:17:39 +0300352 struct wl1251 *wl =
353 container_of(work, struct wl1251, filter_work);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300354 int ret;
355
356 mutex_lock(&wl->mutex);
357
Kalle Valo80301cd2009-06-12 14:17:39 +0300358 if (wl->state == WL1251_STATE_OFF)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300359 goto out;
360
Kalle Valo80301cd2009-06-12 14:17:39 +0300361 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300362 if (ret < 0)
363 goto out;
364
Kalle Valoae46ae12009-08-07 13:34:42 +0300365 ret = wl1251_join(wl, wl->bss_type, wl->channel, wl->beacon_int,
366 wl->dtim_period);
Kalle Valoc5483b72009-06-12 14:16:32 +0300367 if (ret < 0)
368 goto out_sleep;
369
370out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300371 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300372
373out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300374 mutex_unlock(&wl->mutex);
375}
376
Kalle Valo80301cd2009-06-12 14:17:39 +0300377static int wl1251_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300378{
Kalle Valo80301cd2009-06-12 14:17:39 +0300379 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300380
381 skb_queue_tail(&wl->tx_queue, skb);
382
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +0300383 /*
384 * The chip specific setup must run before the first TX packet -
385 * before that, the tx_work will not be initialized!
386 */
387
Kalle Valo16e711f2009-08-07 13:35:04 +0300388 ieee80211_queue_work(wl->hw, &wl->tx_work);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300389
390 /*
391 * The workqueue is slow to process the tx_queue and we need stop
392 * the queue here, otherwise the queue will get too long.
393 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300394 if (skb_queue_len(&wl->tx_queue) >= WL1251_TX_QUEUE_MAX_LENGTH) {
Kalle Valod67e2612009-11-30 10:17:45 +0200395 wl1251_debug(DEBUG_TX, "op_tx: tx_queue full, stop queues");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300396 ieee80211_stop_queues(wl->hw);
397
398 /*
399 * FIXME: this is racy, the variable is not properly
400 * protected. Maybe fix this by removing the stupid
401 * variable altogether and checking the real queue state?
402 */
403 wl->tx_queue_stopped = true;
404 }
405
406 return NETDEV_TX_OK;
407}
408
Kalle Valo80301cd2009-06-12 14:17:39 +0300409static int wl1251_op_start(struct ieee80211_hw *hw)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300410{
Kalle Valo80301cd2009-06-12 14:17:39 +0300411 struct wl1251 *wl = hw->priv;
John W. Linville8b28e822010-07-28 16:59:41 -0400412 struct wiphy *wiphy = hw->wiphy;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300413 int ret = 0;
414
Kalle Valo80301cd2009-06-12 14:17:39 +0300415 wl1251_debug(DEBUG_MAC80211, "mac80211 start");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300416
417 mutex_lock(&wl->mutex);
418
Kalle Valo80301cd2009-06-12 14:17:39 +0300419 if (wl->state != WL1251_STATE_OFF) {
420 wl1251_error("cannot start because not in off state: %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300421 wl->state);
422 ret = -EBUSY;
423 goto out;
424 }
425
Kalle Valo80301cd2009-06-12 14:17:39 +0300426 ret = wl1251_chip_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300427 if (ret < 0)
Jiri Slabyfe643412009-07-14 22:37:13 +0200428 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300429
Kalle Valo0e71bb02009-08-07 13:33:57 +0300430 ret = wl1251_boot(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300431 if (ret < 0)
432 goto out;
433
Kalle Valo0e71bb02009-08-07 13:33:57 +0300434 ret = wl1251_hw_init(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300435 if (ret < 0)
436 goto out;
437
Kalle Valo80301cd2009-06-12 14:17:39 +0300438 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300439 if (ret < 0)
440 goto out;
441
Kalle Valo80301cd2009-06-12 14:17:39 +0300442 wl->state = WL1251_STATE_ON;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300443
Kalle Valo0e71bb02009-08-07 13:33:57 +0300444 wl1251_info("firmware booted (%s)", wl->fw_ver);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300445
John W. Linville8b28e822010-07-28 16:59:41 -0400446 /* update hw/fw version info in wiphy struct */
447 wiphy->hw_version = wl->chip_id;
448 strncpy(wiphy->fw_version, wl->fw_ver, sizeof(wiphy->fw_version));
449
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300450out:
451 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300452 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300453
454 mutex_unlock(&wl->mutex);
455
456 return ret;
457}
458
Kalle Valo80301cd2009-06-12 14:17:39 +0300459static void wl1251_op_stop(struct ieee80211_hw *hw)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300460{
Kalle Valo80301cd2009-06-12 14:17:39 +0300461 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300462
Kalle Valo80301cd2009-06-12 14:17:39 +0300463 wl1251_info("down");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300464
Kalle Valo80301cd2009-06-12 14:17:39 +0300465 wl1251_debug(DEBUG_MAC80211, "mac80211 stop");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300466
467 mutex_lock(&wl->mutex);
468
Kalle Valo80301cd2009-06-12 14:17:39 +0300469 WARN_ON(wl->state != WL1251_STATE_ON);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300470
471 if (wl->scanning) {
472 mutex_unlock(&wl->mutex);
473 ieee80211_scan_completed(wl->hw, true);
474 mutex_lock(&wl->mutex);
475 wl->scanning = false;
476 }
477
Kalle Valo80301cd2009-06-12 14:17:39 +0300478 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300479
Kalle Valo80301cd2009-06-12 14:17:39 +0300480 wl1251_disable_interrupts(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300481
482 mutex_unlock(&wl->mutex);
483
484 cancel_work_sync(&wl->irq_work);
485 cancel_work_sync(&wl->tx_work);
486 cancel_work_sync(&wl->filter_work);
487
488 mutex_lock(&wl->mutex);
489
490 /* let's notify MAC80211 about the remaining pending TX frames */
Kalle Valo0e71bb02009-08-07 13:33:57 +0300491 wl1251_tx_flush(wl);
Kalle Valo80301cd2009-06-12 14:17:39 +0300492 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300493
494 memset(wl->bssid, 0, ETH_ALEN);
495 wl->listen_int = 1;
496 wl->bss_type = MAX_BSS_TYPE;
497
498 wl->data_in_count = 0;
499 wl->rx_counter = 0;
500 wl->rx_handled = 0;
501 wl->rx_current_buffer = 0;
502 wl->rx_last_id = 0;
503 wl->next_tx_complete = 0;
504 wl->elp = false;
505 wl->psm = 0;
506 wl->tx_queue_stopped = false;
Kalle Valo80301cd2009-06-12 14:17:39 +0300507 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
Kalle Valo97802792009-08-07 13:34:27 +0300508 wl->channel = WL1251_DEFAULT_CHANNEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300509
Kalle Valo80301cd2009-06-12 14:17:39 +0300510 wl1251_debugfs_reset(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300511
512 mutex_unlock(&wl->mutex);
513}
514
Kalle Valo80301cd2009-06-12 14:17:39 +0300515static int wl1251_op_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100516 struct ieee80211_vif *vif)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300517{
Kalle Valo80301cd2009-06-12 14:17:39 +0300518 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300519 int ret = 0;
520
Johannes Berge91d8332009-07-15 17:21:41 +0200521 wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
Johannes Berg1ed32e42009-12-23 13:15:45 +0100522 vif->type, vif->addr);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300523
524 mutex_lock(&wl->mutex);
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200525 if (wl->vif) {
526 ret = -EBUSY;
527 goto out;
528 }
529
Johannes Berg1ed32e42009-12-23 13:15:45 +0100530 wl->vif = vif;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300531
Johannes Berg1ed32e42009-12-23 13:15:45 +0100532 switch (vif->type) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300533 case NL80211_IFTYPE_STATION:
534 wl->bss_type = BSS_TYPE_STA_BSS;
535 break;
536 case NL80211_IFTYPE_ADHOC:
537 wl->bss_type = BSS_TYPE_IBSS;
538 break;
539 default:
540 ret = -EOPNOTSUPP;
541 goto out;
542 }
543
Johannes Berg1ed32e42009-12-23 13:15:45 +0100544 if (memcmp(wl->mac_addr, vif->addr, ETH_ALEN)) {
545 memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300546 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
Kalle Valo80301cd2009-06-12 14:17:39 +0300547 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300548 if (ret < 0)
549 goto out;
550 }
551
552out:
553 mutex_unlock(&wl->mutex);
554 return ret;
555}
556
Kalle Valo80301cd2009-06-12 14:17:39 +0300557static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100558 struct ieee80211_vif *vif)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300559{
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200560 struct wl1251 *wl = hw->priv;
561
562 mutex_lock(&wl->mutex);
Kalle Valo80301cd2009-06-12 14:17:39 +0300563 wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200564 wl->vif = NULL;
565 mutex_unlock(&wl->mutex);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300566}
567
Kalle Valo8f8ff9162010-01-12 10:43:07 +0200568static int wl1251_build_qos_null_data(struct wl1251 *wl)
569{
570 struct ieee80211_qos_hdr template;
571
572 memset(&template, 0, sizeof(template));
573
574 memcpy(template.addr1, wl->bssid, ETH_ALEN);
575 memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
576 memcpy(template.addr3, wl->bssid, ETH_ALEN);
577
578 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
579 IEEE80211_STYPE_QOS_NULLFUNC |
580 IEEE80211_FCTL_TODS);
581
582 /* FIXME: not sure what priority to use here */
583 template.qos_ctrl = cpu_to_le16(0);
584
585 return wl1251_cmd_template_set(wl, CMD_QOS_NULL_DATA, &template,
586 sizeof(template));
587}
588
Kalle Valo80301cd2009-06-12 14:17:39 +0300589static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300590{
Kalle Valo80301cd2009-06-12 14:17:39 +0300591 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300592 struct ieee80211_conf *conf = &hw->conf;
593 int channel, ret = 0;
594
595 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
596
Kalle Valo80301cd2009-06-12 14:17:39 +0300597 wl1251_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300598 channel,
599 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
600 conf->power_level);
601
602 mutex_lock(&wl->mutex);
603
Kalle Valo80301cd2009-06-12 14:17:39 +0300604 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300605 if (ret < 0)
606 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300607
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300608 if (channel != wl->channel) {
Kalle Valofe9a9842009-08-07 13:34:49 +0300609 wl->channel = channel;
610
Kalle Valoae46ae12009-08-07 13:34:42 +0300611 ret = wl1251_join(wl, wl->bss_type, wl->channel,
612 wl->beacon_int, wl->dtim_period);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300613 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300614 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300615 }
616
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300617 if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
Luciano Coelho47af3fe2009-06-12 14:17:53 +0300618 wl1251_debug(DEBUG_PSM, "psm enabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300619
620 wl->psm_requested = true;
621
Johannes Berg56007a02010-01-26 14:19:52 +0100622 wl->dtim_period = conf->ps_dtim_period;
623
624 ret = wl1251_acx_wr_tbtt_and_dtim(wl, wl->beacon_int,
625 wl->dtim_period);
626
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300627 /*
Johannes Berg56007a02010-01-26 14:19:52 +0100628 * mac80211 enables PSM only if we're already associated.
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300629 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300630 ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
Kalle Valo478fdf22009-11-30 10:17:52 +0200631 if (ret < 0)
632 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300633 } else if (!(conf->flags & IEEE80211_CONF_PS) &&
634 wl->psm_requested) {
Luciano Coelho47af3fe2009-06-12 14:17:53 +0300635 wl1251_debug(DEBUG_PSM, "psm disabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300636
637 wl->psm_requested = false;
638
Kalle Valo478fdf22009-11-30 10:17:52 +0200639 if (wl->psm) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300640 ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
Kalle Valo478fdf22009-11-30 10:17:52 +0200641 if (ret < 0)
642 goto out_sleep;
643 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300644 }
645
646 if (conf->power_level != wl->power_level) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300647 ret = wl1251_acx_tx_power(wl, conf->power_level);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300648 if (ret < 0)
Kalle Valo478fdf22009-11-30 10:17:52 +0200649 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300650
651 wl->power_level = conf->power_level;
652 }
653
Kalle Valoc5483b72009-06-12 14:16:32 +0300654out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300655 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300656
657out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300658 mutex_unlock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +0300659
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300660 return ret;
661}
662
Kalle Valo80301cd2009-06-12 14:17:39 +0300663#define WL1251_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300664 FIF_ALLMULTI | \
665 FIF_FCSFAIL | \
666 FIF_BCN_PRBRESP_PROMISC | \
667 FIF_CONTROL | \
668 FIF_OTHER_BSS)
669
Kalle Valo80301cd2009-06-12 14:17:39 +0300670static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300671 unsigned int changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +0200672 unsigned int *total,u64 multicast)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300673{
Kalle Valo80301cd2009-06-12 14:17:39 +0300674 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300675
Kalle Valo80301cd2009-06-12 14:17:39 +0300676 wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300677
Kalle Valo80301cd2009-06-12 14:17:39 +0300678 *total &= WL1251_SUPPORTED_FILTERS;
679 changed &= WL1251_SUPPORTED_FILTERS;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300680
681 if (changed == 0)
682 /* no filters which we support changed */
683 return;
684
685 /* FIXME: wl->rx_config and wl->rx_filter are not protected */
686
Kalle Valo80301cd2009-06-12 14:17:39 +0300687 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
688 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300689
690 if (*total & FIF_PROMISC_IN_BSS) {
691 wl->rx_config |= CFG_BSSID_FILTER_EN;
692 wl->rx_config |= CFG_RX_ALL_GOOD;
693 }
694 if (*total & FIF_ALLMULTI)
695 /*
696 * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
697 * all multicast frames
698 */
699 wl->rx_config &= ~CFG_MC_FILTER_EN;
700 if (*total & FIF_FCSFAIL)
701 wl->rx_filter |= CFG_RX_FCS_ERROR;
702 if (*total & FIF_BCN_PRBRESP_PROMISC) {
703 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
704 wl->rx_config &= ~CFG_SSID_FILTER_EN;
705 }
706 if (*total & FIF_CONTROL)
707 wl->rx_filter |= CFG_RX_CTL_EN;
708 if (*total & FIF_OTHER_BSS)
709 wl->rx_filter &= ~CFG_BSSID_FILTER_EN;
710
711 /*
712 * FIXME: workqueues need to be properly cancelled on stop(), for
713 * now let's just disable changing the filter settings. They will
714 * be updated any on config().
715 */
716 /* schedule_work(&wl->filter_work); */
717}
718
719/* HW encryption */
Kalle Valo80301cd2009-06-12 14:17:39 +0300720static int wl1251_set_key_type(struct wl1251 *wl,
721 struct wl1251_cmd_set_keys *key,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300722 enum set_key_cmd cmd,
723 struct ieee80211_key_conf *mac80211_key,
724 const u8 *addr)
725{
Johannes Berg97359d12010-08-10 09:46:38 +0200726 switch (mac80211_key->cipher) {
727 case WLAN_CIPHER_SUITE_WEP40:
728 case WLAN_CIPHER_SUITE_WEP104:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300729 if (is_broadcast_ether_addr(addr))
730 key->key_type = KEY_WEP_DEFAULT;
731 else
732 key->key_type = KEY_WEP_ADDR;
733
734 mac80211_key->hw_key_idx = mac80211_key->keyidx;
735 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200736 case WLAN_CIPHER_SUITE_TKIP:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300737 if (is_broadcast_ether_addr(addr))
738 key->key_type = KEY_TKIP_MIC_GROUP;
739 else
740 key->key_type = KEY_TKIP_MIC_PAIRWISE;
741
742 mac80211_key->hw_key_idx = mac80211_key->keyidx;
743 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200744 case WLAN_CIPHER_SUITE_CCMP:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300745 if (is_broadcast_ether_addr(addr))
746 key->key_type = KEY_AES_GROUP;
747 else
748 key->key_type = KEY_AES_PAIRWISE;
749 mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
750 break;
751 default:
Johannes Berg97359d12010-08-10 09:46:38 +0200752 wl1251_error("Unknown key cipher 0x%x", mac80211_key->cipher);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300753 return -EOPNOTSUPP;
754 }
755
756 return 0;
757}
758
Kalle Valo80301cd2009-06-12 14:17:39 +0300759static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300760 struct ieee80211_vif *vif,
761 struct ieee80211_sta *sta,
762 struct ieee80211_key_conf *key)
763{
Kalle Valo80301cd2009-06-12 14:17:39 +0300764 struct wl1251 *wl = hw->priv;
765 struct wl1251_cmd_set_keys *wl_cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300766 const u8 *addr;
767 int ret;
768
769 static const u8 bcast_addr[ETH_ALEN] =
770 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
771
Kalle Valo80301cd2009-06-12 14:17:39 +0300772 wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300773
Kalle Valoff258392009-06-12 14:14:19 +0300774 wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
775 if (!wl_cmd) {
776 ret = -ENOMEM;
777 goto out;
778 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300779
780 addr = sta ? sta->addr : bcast_addr;
781
Kalle Valo80301cd2009-06-12 14:17:39 +0300782 wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
783 wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
784 wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
Johannes Berg97359d12010-08-10 09:46:38 +0200785 key->cipher, key->keyidx, key->keylen, key->flags);
Kalle Valo80301cd2009-06-12 14:17:39 +0300786 wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300787
Kalle Valoff258392009-06-12 14:14:19 +0300788 if (is_zero_ether_addr(addr)) {
789 /* We dont support TX only encryption */
790 ret = -EOPNOTSUPP;
791 goto out;
792 }
793
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300794 mutex_lock(&wl->mutex);
795
Kalle Valo80301cd2009-06-12 14:17:39 +0300796 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300797 if (ret < 0)
798 goto out_unlock;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300799
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300800 switch (cmd) {
801 case SET_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300802 wl_cmd->key_action = KEY_ADD_OR_REPLACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300803 break;
804 case DISABLE_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300805 wl_cmd->key_action = KEY_REMOVE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300806 break;
807 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300808 wl1251_error("Unsupported key cmd 0x%x", cmd);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300809 break;
810 }
811
Kalle Valo80301cd2009-06-12 14:17:39 +0300812 ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300813 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300814 wl1251_error("Set KEY type failed");
Kalle Valoc5483b72009-06-12 14:16:32 +0300815 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300816 }
817
Kalle Valoff258392009-06-12 14:14:19 +0300818 if (wl_cmd->key_type != KEY_WEP_DEFAULT)
819 memcpy(wl_cmd->addr, addr, ETH_ALEN);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300820
Kalle Valoff258392009-06-12 14:14:19 +0300821 if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
822 (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300823 /*
824 * We get the key in the following form:
825 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
826 * but the target is expecting:
827 * TKIP - RX MIC - TX MIC
828 */
Kalle Valoff258392009-06-12 14:14:19 +0300829 memcpy(wl_cmd->key, key->key, 16);
830 memcpy(wl_cmd->key + 16, key->key + 24, 8);
831 memcpy(wl_cmd->key + 24, key->key + 16, 8);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300832
833 } else {
Kalle Valoff258392009-06-12 14:14:19 +0300834 memcpy(wl_cmd->key, key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300835 }
Kalle Valoff258392009-06-12 14:14:19 +0300836 wl_cmd->key_size = key->keylen;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300837
Kalle Valoff258392009-06-12 14:14:19 +0300838 wl_cmd->id = key->keyidx;
839 wl_cmd->ssid_profile = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300840
Kalle Valo80301cd2009-06-12 14:17:39 +0300841 wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300842
Kalle Valo80301cd2009-06-12 14:17:39 +0300843 ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
Kalle Valoff258392009-06-12 14:14:19 +0300844 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300845 wl1251_warning("could not set keys");
Kalle Valoc5483b72009-06-12 14:16:32 +0300846 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300847 }
848
Kalle Valoc5483b72009-06-12 14:16:32 +0300849out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300850 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300851
852out_unlock:
Kalle Valoff258392009-06-12 14:14:19 +0300853 mutex_unlock(&wl->mutex);
854
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300855out:
Kalle Valoff258392009-06-12 14:14:19 +0300856 kfree(wl_cmd);
857
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300858 return ret;
859}
860
Kalle Valo80301cd2009-06-12 14:17:39 +0300861static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
Johannes Berga060bbf2010-04-27 11:59:34 +0200862 struct ieee80211_vif *vif,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300863 struct cfg80211_scan_request *req)
864{
Kalle Valo80301cd2009-06-12 14:17:39 +0300865 struct wl1251 *wl = hw->priv;
Kalle Valoe477c562010-01-05 20:16:57 +0200866 struct sk_buff *skb;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300867 size_t ssid_len = 0;
Kalle Valo3a98c302010-01-05 20:16:51 +0200868 u8 *ssid = NULL;
869 int ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300870
Kalle Valo80301cd2009-06-12 14:17:39 +0300871 wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300872
873 if (req->n_ssids) {
874 ssid = req->ssids[0].ssid;
875 ssid_len = req->ssids[0].ssid_len;
876 }
877
878 mutex_lock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +0300879
Kalle Valo3a98c302010-01-05 20:16:51 +0200880 if (wl->scanning) {
881 wl1251_debug(DEBUG_SCAN, "scan already in progress");
882 ret = -EINVAL;
883 goto out;
884 }
885
Kalle Valo80301cd2009-06-12 14:17:39 +0300886 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300887 if (ret < 0)
888 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300889
Kalle Valoe477c562010-01-05 20:16:57 +0200890 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
891 req->ie, req->ie_len);
892 if (!skb) {
893 ret = -ENOMEM;
894 goto out;
895 }
896
897 ret = wl1251_cmd_template_set(wl, CMD_PROBE_REQ, skb->data,
898 skb->len);
899 dev_kfree_skb(skb);
Kalle Valo3a98c302010-01-05 20:16:51 +0200900 if (ret < 0)
Kalle Valoe477c562010-01-05 20:16:57 +0200901 goto out_sleep;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300902
Kalle Valo3a98c302010-01-05 20:16:51 +0200903 ret = wl1251_cmd_trigger_scan_to(wl, 0);
904 if (ret < 0)
905 goto out_sleep;
906
907 wl->scanning = true;
908
Kalle Valodc52f0a2010-01-05 20:17:03 +0200909 ret = wl1251_cmd_scan(wl, ssid, ssid_len, req->channels,
910 req->n_channels, WL1251_SCAN_NUM_PROBES);
Kalle Valo3a98c302010-01-05 20:16:51 +0200911 if (ret < 0) {
912 wl->scanning = false;
913 goto out_sleep;
914 }
915
916out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300917 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300918
919out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300920 mutex_unlock(&wl->mutex);
921
922 return ret;
923}
924
Kalle Valo80301cd2009-06-12 14:17:39 +0300925static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300926{
Kalle Valo80301cd2009-06-12 14:17:39 +0300927 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300928 int ret;
929
Kalle Valocee4fd22009-06-12 14:16:20 +0300930 mutex_lock(&wl->mutex);
931
Kalle Valo80301cd2009-06-12 14:17:39 +0300932 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300933 if (ret < 0)
934 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300935
Kalle Valo80301cd2009-06-12 14:17:39 +0300936 ret = wl1251_acx_rts_threshold(wl, (u16) value);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300937 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300938 wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300939
Kalle Valo80301cd2009-06-12 14:17:39 +0300940 wl1251_ps_elp_sleep(wl);
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300941
Kalle Valoc5483b72009-06-12 14:16:32 +0300942out:
Kalle Valocee4fd22009-06-12 14:16:20 +0300943 mutex_unlock(&wl->mutex);
944
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300945 return ret;
946}
947
Kalle Valo80301cd2009-06-12 14:17:39 +0300948static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300949 struct ieee80211_vif *vif,
950 struct ieee80211_bss_conf *bss_conf,
951 u32 changed)
952{
Kalle Valo80301cd2009-06-12 14:17:39 +0300953 struct wl1251 *wl = hw->priv;
Kalle Valof7f70572010-01-05 20:16:32 +0200954 struct sk_buff *beacon, *skb;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300955 int ret;
956
Kalle Valo80301cd2009-06-12 14:17:39 +0300957 wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300958
959 mutex_lock(&wl->mutex);
960
Kalle Valo80301cd2009-06-12 14:17:39 +0300961 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300962 if (ret < 0)
963 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300964
Kalle Valode8df1e2009-11-26 10:56:06 +0200965 if (changed & BSS_CHANGED_BSSID) {
966 memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
967
Kalle Valof7f70572010-01-05 20:16:32 +0200968 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
969 if (!skb)
970 goto out_sleep;
971
972 ret = wl1251_cmd_template_set(wl, CMD_NULL_DATA,
973 skb->data, skb->len);
974 dev_kfree_skb(skb);
Kalle Valode8df1e2009-11-26 10:56:06 +0200975 if (ret < 0)
Kalle Valo80a112f2010-01-05 20:17:10 +0200976 goto out_sleep;
Kalle Valode8df1e2009-11-26 10:56:06 +0200977
Kalle Valo8f8ff9162010-01-12 10:43:07 +0200978 ret = wl1251_build_qos_null_data(wl);
979 if (ret < 0)
980 goto out;
981
Kalle Valode8df1e2009-11-26 10:56:06 +0200982 if (wl->bss_type != BSS_TYPE_IBSS) {
983 ret = wl1251_join(wl, wl->bss_type, wl->channel,
984 wl->beacon_int, wl->dtim_period);
985 if (ret < 0)
986 goto out_sleep;
987 }
988 }
989
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300990 if (changed & BSS_CHANGED_ASSOC) {
991 if (bss_conf->assoc) {
Kalle Valoe2fd4612009-08-07 13:34:12 +0300992 wl->beacon_int = bss_conf->beacon_int;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300993
Kalle Valof7f70572010-01-05 20:16:32 +0200994 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
995 if (!skb)
996 goto out_sleep;
997
998 ret = wl1251_cmd_template_set(wl, CMD_PS_POLL,
999 skb->data,
1000 skb->len);
1001 dev_kfree_skb(skb);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001002 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001003 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001004
Johannes Berg56007a02010-01-26 14:19:52 +01001005 ret = wl1251_acx_aid(wl, bss_conf->aid);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001006 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001007 goto out_sleep;
Kalle Valoe2fd4612009-08-07 13:34:12 +03001008 } else {
1009 /* use defaults when not associated */
1010 wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
1011 wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001012 }
1013 }
1014 if (changed & BSS_CHANGED_ERP_SLOT) {
1015 if (bss_conf->use_short_slot)
Kalle Valo80301cd2009-06-12 14:17:39 +03001016 ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001017 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001018 ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001019 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001020 wl1251_warning("Set slot time failed %d", ret);
Kalle Valoc5483b72009-06-12 14:16:32 +03001021 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001022 }
1023 }
1024
1025 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1026 if (bss_conf->use_short_preamble)
Kalle Valo80301cd2009-06-12 14:17:39 +03001027 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001028 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001029 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001030 }
1031
1032 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1033 if (bss_conf->use_cts_prot)
Kalle Valo80301cd2009-06-12 14:17:39 +03001034 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001035 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001036 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001037 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001038 wl1251_warning("Set ctsprotect failed %d", ret);
Kalle Valo80a112f2010-01-05 20:17:10 +02001039 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001040 }
1041 }
1042
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001043 if (changed & BSS_CHANGED_BEACON) {
1044 beacon = ieee80211_beacon_get(hw, vif);
Kalle Valo80301cd2009-06-12 14:17:39 +03001045 ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001046 beacon->len);
1047
1048 if (ret < 0) {
1049 dev_kfree_skb(beacon);
Kalle Valo80a112f2010-01-05 20:17:10 +02001050 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001051 }
1052
Kalle Valo80301cd2009-06-12 14:17:39 +03001053 ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001054 beacon->len);
1055
1056 dev_kfree_skb(beacon);
1057
1058 if (ret < 0)
Kalle Valo80a112f2010-01-05 20:17:10 +02001059 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001060
Kalle Valoae46ae12009-08-07 13:34:42 +03001061 ret = wl1251_join(wl, wl->bss_type, wl->beacon_int,
1062 wl->channel, wl->dtim_period);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001063
1064 if (ret < 0)
Kalle Valo80a112f2010-01-05 20:17:10 +02001065 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001066 }
1067
Kalle Valoc5483b72009-06-12 14:16:32 +03001068out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +03001069 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001070
1071out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001072 mutex_unlock(&wl->mutex);
1073}
1074
1075
1076/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001077static struct ieee80211_rate wl1251_rates[] = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001078 { .bitrate = 10,
1079 .hw_value = 0x1,
1080 .hw_value_short = 0x1, },
1081 { .bitrate = 20,
1082 .hw_value = 0x2,
1083 .hw_value_short = 0x2,
1084 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1085 { .bitrate = 55,
1086 .hw_value = 0x4,
1087 .hw_value_short = 0x4,
1088 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1089 { .bitrate = 110,
1090 .hw_value = 0x20,
1091 .hw_value_short = 0x20,
1092 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1093 { .bitrate = 60,
1094 .hw_value = 0x8,
1095 .hw_value_short = 0x8, },
1096 { .bitrate = 90,
1097 .hw_value = 0x10,
1098 .hw_value_short = 0x10, },
1099 { .bitrate = 120,
1100 .hw_value = 0x40,
1101 .hw_value_short = 0x40, },
1102 { .bitrate = 180,
1103 .hw_value = 0x80,
1104 .hw_value_short = 0x80, },
1105 { .bitrate = 240,
1106 .hw_value = 0x200,
1107 .hw_value_short = 0x200, },
1108 { .bitrate = 360,
1109 .hw_value = 0x400,
1110 .hw_value_short = 0x400, },
1111 { .bitrate = 480,
1112 .hw_value = 0x800,
1113 .hw_value_short = 0x800, },
1114 { .bitrate = 540,
1115 .hw_value = 0x1000,
1116 .hw_value_short = 0x1000, },
1117};
1118
1119/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001120static struct ieee80211_channel wl1251_channels[] = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001121 { .hw_value = 1, .center_freq = 2412},
1122 { .hw_value = 2, .center_freq = 2417},
1123 { .hw_value = 3, .center_freq = 2422},
1124 { .hw_value = 4, .center_freq = 2427},
1125 { .hw_value = 5, .center_freq = 2432},
1126 { .hw_value = 6, .center_freq = 2437},
1127 { .hw_value = 7, .center_freq = 2442},
1128 { .hw_value = 8, .center_freq = 2447},
1129 { .hw_value = 9, .center_freq = 2452},
1130 { .hw_value = 10, .center_freq = 2457},
1131 { .hw_value = 11, .center_freq = 2462},
1132 { .hw_value = 12, .center_freq = 2467},
1133 { .hw_value = 13, .center_freq = 2472},
1134};
1135
Kalle Valo86dff7a2009-11-30 10:18:19 +02001136static int wl1251_op_conf_tx(struct ieee80211_hw *hw, u16 queue,
1137 const struct ieee80211_tx_queue_params *params)
1138{
Kalle Valo4ff6ffa2010-01-12 10:43:15 +02001139 enum wl1251_acx_ps_scheme ps_scheme;
Kalle Valo86dff7a2009-11-30 10:18:19 +02001140 struct wl1251 *wl = hw->priv;
1141 int ret;
1142
1143 mutex_lock(&wl->mutex);
1144
1145 wl1251_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
1146
1147 ret = wl1251_ps_elp_wakeup(wl);
1148 if (ret < 0)
1149 goto out;
1150
Kalle Valo85359492010-02-04 15:33:25 +02001151 /* mac80211 uses units of 32 usec */
Kalle Valo86dff7a2009-11-30 10:18:19 +02001152 ret = wl1251_acx_ac_cfg(wl, wl1251_tx_get_queue(queue),
1153 params->cw_min, params->cw_max,
Kalle Valo85359492010-02-04 15:33:25 +02001154 params->aifs, params->txop * 32);
Kalle Valo27336f12009-11-30 10:18:27 +02001155 if (ret < 0)
1156 goto out_sleep;
Kalle Valo86dff7a2009-11-30 10:18:19 +02001157
Kalle Valo4ff6ffa2010-01-12 10:43:15 +02001158 if (params->uapsd)
1159 ps_scheme = WL1251_ACX_PS_SCHEME_UPSD_TRIGGER;
1160 else
1161 ps_scheme = WL1251_ACX_PS_SCHEME_LEGACY;
1162
Kalle Valo27336f12009-11-30 10:18:27 +02001163 ret = wl1251_acx_tid_cfg(wl, wl1251_tx_get_queue(queue),
Kalle Valo49e1b9f2009-11-30 10:18:33 +02001164 CHANNEL_TYPE_EDCF,
Kalle Valo4ff6ffa2010-01-12 10:43:15 +02001165 wl1251_tx_get_queue(queue), ps_scheme,
Kalle Valo27336f12009-11-30 10:18:27 +02001166 WL1251_ACX_ACK_POLICY_LEGACY);
1167 if (ret < 0)
1168 goto out_sleep;
1169
1170out_sleep:
Kalle Valo86dff7a2009-11-30 10:18:19 +02001171 wl1251_ps_elp_sleep(wl);
1172
1173out:
1174 mutex_unlock(&wl->mutex);
1175
1176 return ret;
1177}
1178
John W. Linville19434142010-07-28 15:23:30 -04001179static int wl1251_op_get_survey(struct ieee80211_hw *hw, int idx,
1180 struct survey_info *survey)
1181{
1182 struct wl1251 *wl = hw->priv;
1183 struct ieee80211_conf *conf = &hw->conf;
1184
1185 if (idx != 0)
1186 return -ENOENT;
1187
1188 survey->channel = conf->channel;
1189 survey->filled = SURVEY_INFO_NOISE_DBM;
1190 survey->noise = wl->noise;
1191
1192 return 0;
1193}
1194
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001195/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001196static struct ieee80211_supported_band wl1251_band_2ghz = {
1197 .channels = wl1251_channels,
1198 .n_channels = ARRAY_SIZE(wl1251_channels),
1199 .bitrates = wl1251_rates,
1200 .n_bitrates = ARRAY_SIZE(wl1251_rates),
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001201};
1202
Kalle Valo80301cd2009-06-12 14:17:39 +03001203static const struct ieee80211_ops wl1251_ops = {
1204 .start = wl1251_op_start,
1205 .stop = wl1251_op_stop,
1206 .add_interface = wl1251_op_add_interface,
1207 .remove_interface = wl1251_op_remove_interface,
1208 .config = wl1251_op_config,
1209 .configure_filter = wl1251_op_configure_filter,
1210 .tx = wl1251_op_tx,
1211 .set_key = wl1251_op_set_key,
1212 .hw_scan = wl1251_op_hw_scan,
1213 .bss_info_changed = wl1251_op_bss_info_changed,
1214 .set_rts_threshold = wl1251_op_set_rts_threshold,
Kalle Valo86dff7a2009-11-30 10:18:19 +02001215 .conf_tx = wl1251_op_conf_tx,
John W. Linville19434142010-07-28 15:23:30 -04001216 .get_survey = wl1251_op_get_survey,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001217};
1218
Grazvydas Ignotas61c2a802010-04-14 13:05:48 +03001219static int wl1251_read_eeprom_byte(struct wl1251 *wl, off_t offset, u8 *data)
1220{
1221 unsigned long timeout;
1222
1223 wl1251_reg_write32(wl, EE_ADDR, offset);
1224 wl1251_reg_write32(wl, EE_CTL, EE_CTL_READ);
1225
1226 /* EE_CTL_READ clears when data is ready */
1227 timeout = jiffies + msecs_to_jiffies(100);
1228 while (1) {
1229 if (!(wl1251_reg_read32(wl, EE_CTL) & EE_CTL_READ))
1230 break;
1231
1232 if (time_after(jiffies, timeout))
1233 return -ETIMEDOUT;
1234
1235 msleep(1);
1236 }
1237
1238 *data = wl1251_reg_read32(wl, EE_DATA);
1239 return 0;
1240}
1241
1242static int wl1251_read_eeprom(struct wl1251 *wl, off_t offset,
1243 u8 *data, size_t len)
1244{
1245 size_t i;
1246 int ret;
1247
1248 wl1251_reg_write32(wl, EE_START, 0);
1249
1250 for (i = 0; i < len; i++) {
1251 ret = wl1251_read_eeprom_byte(wl, offset + i, &data[i]);
1252 if (ret < 0)
1253 return ret;
1254 }
1255
1256 return 0;
1257}
1258
1259static int wl1251_read_eeprom_mac(struct wl1251 *wl)
1260{
1261 u8 mac[ETH_ALEN];
1262 int i, ret;
1263
1264 wl1251_set_partition(wl, 0, 0, REGISTERS_BASE, REGISTERS_DOWN_SIZE);
1265
1266 ret = wl1251_read_eeprom(wl, 0x1c, mac, sizeof(mac));
1267 if (ret < 0) {
1268 wl1251_warning("failed to read MAC address from EEPROM");
1269 return ret;
1270 }
1271
1272 /* MAC is stored in reverse order */
1273 for (i = 0; i < ETH_ALEN; i++)
1274 wl->mac_addr[i] = mac[ETH_ALEN - i - 1];
1275
1276 return 0;
1277}
1278
Kalle Valo80301cd2009-06-12 14:17:39 +03001279static int wl1251_register_hw(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001280{
1281 int ret;
1282
1283 if (wl->mac80211_registered)
1284 return 0;
1285
1286 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1287
1288 ret = ieee80211_register_hw(wl->hw);
1289 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001290 wl1251_error("unable to register mac80211 hw: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001291 return ret;
1292 }
1293
1294 wl->mac80211_registered = true;
1295
Kalle Valo80301cd2009-06-12 14:17:39 +03001296 wl1251_notice("loaded");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001297
1298 return 0;
1299}
1300
Bob Copeland8e639c02009-08-07 13:33:26 +03001301int wl1251_init_ieee80211(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001302{
Bob Copeland8e639c02009-08-07 13:33:26 +03001303 int ret;
1304
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001305 /* The tx descriptor buffer and the TKIP space */
1306 wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +03001307 + WL1251_TKIP_IV_SPACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001308
1309 /* unit us */
1310 /* FIXME: find a proper value */
1311 wl->hw->channel_change_time = 10000;
1312
1313 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
Juuso Oikarinen6b21a2c2009-11-17 18:48:30 +02001314 IEEE80211_HW_SUPPORTS_PS |
Kalle Valo4ff6ffa2010-01-12 10:43:15 +02001315 IEEE80211_HW_BEACON_FILTER |
1316 IEEE80211_HW_SUPPORTS_UAPSD;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001317
1318 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1319 wl->hw->wiphy->max_scan_ssids = 1;
Kalle Valo80301cd2009-06-12 14:17:39 +03001320 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1251_band_2ghz;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001321
Kalle Valoda2fb4e2009-11-30 10:18:47 +02001322 wl->hw->queues = 4;
1323
Grazvydas Ignotas61c2a802010-04-14 13:05:48 +03001324 if (wl->use_eeprom)
1325 wl1251_read_eeprom_mac(wl);
1326
Bob Copeland8e639c02009-08-07 13:33:26 +03001327 ret = wl1251_register_hw(wl);
1328 if (ret)
1329 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001330
Bob Copeland8e639c02009-08-07 13:33:26 +03001331 wl1251_debugfs_init(wl);
1332 wl1251_notice("initialized");
1333
1334 ret = 0;
1335
1336out:
1337 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001338}
Bob Copelandaf8c78e2009-08-07 13:33:34 +03001339EXPORT_SYMBOL_GPL(wl1251_init_ieee80211);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001340
Bob Copeland8e639c02009-08-07 13:33:26 +03001341struct ieee80211_hw *wl1251_alloc_hw(void)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001342{
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001343 struct ieee80211_hw *hw;
Kalle Valo80301cd2009-06-12 14:17:39 +03001344 struct wl1251 *wl;
Bob Copeland8e639c02009-08-07 13:33:26 +03001345 int i;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001346 static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1347
Kalle Valo80301cd2009-06-12 14:17:39 +03001348 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001349 if (!hw) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001350 wl1251_error("could not alloc ieee80211_hw");
Bob Copeland8e639c02009-08-07 13:33:26 +03001351 return ERR_PTR(-ENOMEM);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001352 }
1353
1354 wl = hw->priv;
1355 memset(wl, 0, sizeof(*wl));
1356
1357 wl->hw = hw;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001358
1359 wl->data_in_count = 0;
1360
1361 skb_queue_head_init(&wl->tx_queue);
1362
Kalle Valo80301cd2009-06-12 14:17:39 +03001363 INIT_WORK(&wl->filter_work, wl1251_filter_work);
Juuso Oikarinend5da79a2009-11-17 18:48:37 +02001364 INIT_DELAYED_WORK(&wl->elp_work, wl1251_elp_work);
Kalle Valo80301cd2009-06-12 14:17:39 +03001365 wl->channel = WL1251_DEFAULT_CHANNEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001366 wl->scanning = false;
1367 wl->default_key = 0;
1368 wl->listen_int = 1;
1369 wl->rx_counter = 0;
1370 wl->rx_handled = 0;
1371 wl->rx_current_buffer = 0;
1372 wl->rx_last_id = 0;
Kalle Valo80301cd2009-06-12 14:17:39 +03001373 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
1374 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001375 wl->elp = false;
1376 wl->psm = 0;
1377 wl->psm_requested = false;
1378 wl->tx_queue_stopped = false;
Kalle Valo80301cd2009-06-12 14:17:39 +03001379 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
Kalle Valoe2fd4612009-08-07 13:34:12 +03001380 wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
1381 wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
Juuso Oikarinen287f6f92009-11-17 18:48:23 +02001382 wl->vif = NULL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001383
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001384 for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
1385 wl->tx_frames[i] = NULL;
1386
1387 wl->next_tx_complete = 0;
1388
Kalle Valo0e71bb02009-08-07 13:33:57 +03001389 INIT_WORK(&wl->irq_work, wl1251_irq_work);
1390 INIT_WORK(&wl->tx_work, wl1251_tx_work);
1391
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001392 /*
1393 * In case our MAC address is not correctly set,
1394 * we use a random but Nokia MAC.
1395 */
1396 memcpy(wl->mac_addr, nokia_oui, 3);
1397 get_random_bytes(wl->mac_addr + 3, 3);
1398
Kalle Valo80301cd2009-06-12 14:17:39 +03001399 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001400 mutex_init(&wl->mutex);
1401
1402 wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
1403 wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
1404
Kalle Valo47212132009-06-12 14:15:08 +03001405 wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1406 if (!wl->rx_descriptor) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001407 wl1251_error("could not allocate memory for rx descriptor");
Bob Copeland8e639c02009-08-07 13:33:26 +03001408 ieee80211_free_hw(hw);
1409 return ERR_PTR(-ENOMEM);
Kalle Valo47212132009-06-12 14:15:08 +03001410 }
1411
Bob Copeland8e639c02009-08-07 13:33:26 +03001412 return hw;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001413}
Bob Copelandaf8c78e2009-08-07 13:33:34 +03001414EXPORT_SYMBOL_GPL(wl1251_alloc_hw);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001415
Bob Copeland8e639c02009-08-07 13:33:26 +03001416int wl1251_free_hw(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001417{
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001418 ieee80211_unregister_hw(wl->hw);
1419
Kalle Valo80301cd2009-06-12 14:17:39 +03001420 wl1251_debugfs_exit(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001421
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001422 kfree(wl->target_mem_map);
1423 kfree(wl->data_path);
Kalle Valoc74ddfd2009-11-17 18:48:45 +02001424 vfree(wl->fw);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001425 wl->fw = NULL;
1426 kfree(wl->nvs);
1427 wl->nvs = NULL;
Kalle Valo47212132009-06-12 14:15:08 +03001428
1429 kfree(wl->rx_descriptor);
1430 wl->rx_descriptor = NULL;
1431
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001432 ieee80211_free_hw(wl->hw);
1433
1434 return 0;
1435}
Bob Copelandaf8c78e2009-08-07 13:33:34 +03001436EXPORT_SYMBOL_GPL(wl1251_free_hw);
1437
1438MODULE_DESCRIPTION("TI wl1251 Wireles LAN Driver Core");
1439MODULE_LICENSE("GPL");
Kalle Valob1b0a2b2009-08-07 13:35:19 +03001440MODULE_AUTHOR("Kalle Valo <kalle.valo@nokia.com>");
Ben Hutchings49f146d2009-11-07 22:02:15 +00001441MODULE_FIRMWARE(WL1251_FW_NAME);