blob: faf221ca3f41b6aa4646c633d18c9620bfb55e1e [file] [log] [blame]
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001/*
Kalle Valo80301cd2009-06-12 14:17:39 +03002 * This file is part of wl1251
Kalle Valo2f01a1f2009-04-29 23:33:31 +03003 *
4 * Copyright (C) 2008-2009 Nokia Corporation
5 *
Kalle Valo2f01a1f2009-04-29 23:33:31 +03006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18 * 02110-1301 USA
19 *
20 */
21
22#include <linux/module.h>
23#include <linux/interrupt.h>
24#include <linux/firmware.h>
25#include <linux/delay.h>
26#include <linux/irq.h>
Kalle Valo2f01a1f2009-04-29 23:33:31 +030027#include <linux/crc32.h>
28#include <linux/etherdevice.h>
Kalle Valoc74ddfd2009-11-17 18:48:45 +020029#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Kalle Valo2f01a1f2009-04-29 23:33:31 +030031
Kalle Valo13674112009-06-12 14:17:25 +030032#include "wl1251.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030033#include "wl12xx_80211.h"
Kalle Valo29d904c2009-08-07 13:35:11 +030034#include "wl1251_reg.h"
Bob Copeland0764de62009-08-07 13:32:56 +030035#include "wl1251_io.h"
Bob Copeland8e639c02009-08-07 13:33:26 +030036#include "wl1251_cmd.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030037#include "wl1251_event.h"
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +030038#include "wl1251_tx.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030039#include "wl1251_rx.h"
40#include "wl1251_ps.h"
41#include "wl1251_init.h"
42#include "wl1251_debugfs.h"
Kalle Valo0e71bb02009-08-07 13:33:57 +030043#include "wl1251_boot.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030044
Bob Copelandb5ed9c12009-08-07 13:33:49 +030045void wl1251_enable_interrupts(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030046{
Bob Copelandb5ed9c12009-08-07 13:33:49 +030047 wl->if_ops->enable_irq(wl);
48}
49
50void wl1251_disable_interrupts(struct wl1251 *wl)
51{
52 wl->if_ops->disable_irq(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030053}
54
Kalle Valo80301cd2009-06-12 14:17:39 +030055static void wl1251_power_off(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030056{
57 wl->set_power(false);
58}
59
Kalle Valo80301cd2009-06-12 14:17:39 +030060static void wl1251_power_on(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030061{
62 wl->set_power(true);
63}
64
Kalle Valo80301cd2009-06-12 14:17:39 +030065static int wl1251_fetch_firmware(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030066{
67 const struct firmware *fw;
Bob Copeland6c766f42009-08-07 13:33:04 +030068 struct device *dev = wiphy_dev(wl->hw->wiphy);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030069 int ret;
70
Kalle Valo0e71bb02009-08-07 13:33:57 +030071 ret = request_firmware(&fw, WL1251_FW_NAME, dev);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030072
73 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +030074 wl1251_error("could not get firmware: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030075 return ret;
76 }
77
78 if (fw->size % 4) {
Kalle Valo80301cd2009-06-12 14:17:39 +030079 wl1251_error("firmware size is not multiple of 32 bits: %zu",
Kalle Valo2f01a1f2009-04-29 23:33:31 +030080 fw->size);
81 ret = -EILSEQ;
82 goto out;
83 }
84
85 wl->fw_len = fw->size;
Kalle Valoc74ddfd2009-11-17 18:48:45 +020086 wl->fw = vmalloc(wl->fw_len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030087
88 if (!wl->fw) {
Kalle Valo80301cd2009-06-12 14:17:39 +030089 wl1251_error("could not allocate memory for the firmware");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030090 ret = -ENOMEM;
91 goto out;
92 }
93
94 memcpy(wl->fw, fw->data, wl->fw_len);
95
96 ret = 0;
97
98out:
99 release_firmware(fw);
100
101 return ret;
102}
103
Kalle Valo80301cd2009-06-12 14:17:39 +0300104static int wl1251_fetch_nvs(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300105{
106 const struct firmware *fw;
Bob Copeland6c766f42009-08-07 13:33:04 +0300107 struct device *dev = wiphy_dev(wl->hw->wiphy);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300108 int ret;
109
Kalle Valo0e71bb02009-08-07 13:33:57 +0300110 ret = request_firmware(&fw, WL1251_NVS_NAME, dev);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300111
112 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300113 wl1251_error("could not get nvs file: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300114 return ret;
115 }
116
117 if (fw->size % 4) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300118 wl1251_error("nvs size is not multiple of 32 bits: %zu",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300119 fw->size);
120 ret = -EILSEQ;
121 goto out;
122 }
123
124 wl->nvs_len = fw->size;
Julia Lawall80caf602010-05-15 23:15:10 +0200125 wl->nvs = kmemdup(fw->data, wl->nvs_len, GFP_KERNEL);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300126
127 if (!wl->nvs) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300128 wl1251_error("could not allocate memory for the nvs file");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300129 ret = -ENOMEM;
130 goto out;
131 }
132
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300133 ret = 0;
134
135out:
136 release_firmware(fw);
137
138 return ret;
139}
140
Kalle Valo80301cd2009-06-12 14:17:39 +0300141static void wl1251_fw_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300142{
143 u32 elp_reg;
144
145 elp_reg = ELPCTRL_WAKE_UP;
Grazvydas Ignotas3f9e7502010-03-11 17:44:57 +0200146 wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
147 elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300148
Kalle Valo05fac682009-06-12 14:17:47 +0300149 if (!(elp_reg & ELPCTRL_WLAN_READY))
Kalle Valo80301cd2009-06-12 14:17:39 +0300150 wl1251_warning("WLAN not ready");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300151}
152
Kalle Valo80301cd2009-06-12 14:17:39 +0300153static int wl1251_chip_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300154{
155 int ret = 0;
156
Kalle Valo80301cd2009-06-12 14:17:39 +0300157 wl1251_power_on(wl);
Kalle Valo0e71bb02009-08-07 13:33:57 +0300158 msleep(WL1251_POWER_ON_SLEEP);
Bob Copeland08d9f5722009-08-07 13:33:11 +0300159 wl->if_ops->reset(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300160
161 /* We don't need a real memory partition here, because we only want
162 * to use the registers at this point. */
Kalle Valo80301cd2009-06-12 14:17:39 +0300163 wl1251_set_partition(wl,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300164 0x00000000,
165 0x00000000,
166 REGISTERS_BASE,
167 REGISTERS_DOWN_SIZE);
168
169 /* ELP module wake up */
Kalle Valo80301cd2009-06-12 14:17:39 +0300170 wl1251_fw_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300171
172 /* whal_FwCtrl_BootSm() */
173
174 /* 0. read chip id from CHIP_ID */
Kalle Valo0e71bb02009-08-07 13:33:57 +0300175 wl->chip_id = wl1251_reg_read32(wl, CHIP_ID_B);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300176
177 /* 1. check if chip id is valid */
178
Kalle Valo0e71bb02009-08-07 13:33:57 +0300179 switch (wl->chip_id) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300180 case CHIP_ID_1251_PG12:
Kalle Valo80301cd2009-06-12 14:17:39 +0300181 wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG12)",
Kalle Valo0e71bb02009-08-07 13:33:57 +0300182 wl->chip_id);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300183 break;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300184 case CHIP_ID_1251_PG11:
David-John Willis2c759e02009-10-15 14:38:16 +0100185 wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG11)",
186 wl->chip_id);
187 break;
John W. Linville9a493e82009-10-27 15:15:05 -0400188 case CHIP_ID_1251_PG10:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300189 default:
Kalle Valo0e71bb02009-08-07 13:33:57 +0300190 wl1251_error("unsupported chip id: 0x%x", wl->chip_id);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300191 ret = -ENODEV;
192 goto out;
193 }
194
195 if (wl->fw == NULL) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300196 ret = wl1251_fetch_firmware(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300197 if (ret < 0)
198 goto out;
199 }
200
Grazvydas Ignotasafa5ec22010-04-13 01:21:40 +0300201 if (wl->nvs == NULL && !wl->use_eeprom) {
202 /* No NVS from netlink, try to get it from the filesystem */
Kalle Valo80301cd2009-06-12 14:17:39 +0300203 ret = wl1251_fetch_nvs(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300204 if (ret < 0)
205 goto out;
206 }
207
208out:
209 return ret;
210}
211
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200212#define WL1251_IRQ_LOOP_COUNT 10
Kalle Valo0e71bb02009-08-07 13:33:57 +0300213static void wl1251_irq_work(struct work_struct *work)
214{
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200215 u32 intr, ctr = WL1251_IRQ_LOOP_COUNT;
Kalle Valo0e71bb02009-08-07 13:33:57 +0300216 struct wl1251 *wl =
217 container_of(work, struct wl1251, irq_work);
218 int ret;
219
220 mutex_lock(&wl->mutex);
221
222 wl1251_debug(DEBUG_IRQ, "IRQ work");
223
224 if (wl->state == WL1251_STATE_OFF)
225 goto out;
226
227 ret = wl1251_ps_elp_wakeup(wl);
228 if (ret < 0)
229 goto out;
230
231 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, WL1251_ACX_INTR_ALL);
232
233 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
234 wl1251_debug(DEBUG_IRQ, "intr: 0x%x", intr);
235
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200236 do {
237 if (wl->data_path) {
238 wl->rx_counter = wl1251_mem_read32(
239 wl, wl->data_path->rx_control_addr);
Kalle Valo0e71bb02009-08-07 13:33:57 +0300240
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200241 /* We handle a frmware bug here */
242 switch ((wl->rx_counter - wl->rx_handled) & 0xf) {
243 case 0:
244 wl1251_debug(DEBUG_IRQ,
245 "RX: FW and host in sync");
246 intr &= ~WL1251_ACX_INTR_RX0_DATA;
247 intr &= ~WL1251_ACX_INTR_RX1_DATA;
248 break;
249 case 1:
250 wl1251_debug(DEBUG_IRQ, "RX: FW +1");
251 intr |= WL1251_ACX_INTR_RX0_DATA;
252 intr &= ~WL1251_ACX_INTR_RX1_DATA;
253 break;
254 case 2:
255 wl1251_debug(DEBUG_IRQ, "RX: FW +2");
256 intr |= WL1251_ACX_INTR_RX0_DATA;
257 intr |= WL1251_ACX_INTR_RX1_DATA;
258 break;
259 default:
260 wl1251_warning(
261 "RX: FW and host out of sync: %d",
262 wl->rx_counter - wl->rx_handled);
263 break;
264 }
265
266 wl->rx_handled = wl->rx_counter;
267
268 wl1251_debug(DEBUG_IRQ, "RX counter: %d",
269 wl->rx_counter);
Kalle Valo0e71bb02009-08-07 13:33:57 +0300270 }
271
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200272 intr &= wl->intr_mask;
Kalle Valo0e71bb02009-08-07 13:33:57 +0300273
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200274 if (intr == 0) {
275 wl1251_debug(DEBUG_IRQ, "INTR is 0");
276 goto out_sleep;
277 }
Kalle Valo0e71bb02009-08-07 13:33:57 +0300278
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200279 if (intr & WL1251_ACX_INTR_RX0_DATA) {
280 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX0_DATA");
281 wl1251_rx(wl);
282 }
Kalle Valo0e71bb02009-08-07 13:33:57 +0300283
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200284 if (intr & WL1251_ACX_INTR_RX1_DATA) {
285 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX1_DATA");
286 wl1251_rx(wl);
287 }
Kalle Valo0e71bb02009-08-07 13:33:57 +0300288
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200289 if (intr & WL1251_ACX_INTR_TX_RESULT) {
290 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_TX_RESULT");
291 wl1251_tx_complete(wl);
292 }
Kalle Valo0e71bb02009-08-07 13:33:57 +0300293
Grazvydas Ignotasd41776fa2010-08-17 22:46:53 +0300294 if (intr & WL1251_ACX_INTR_EVENT_A) {
295 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT_A");
296 wl1251_event_handle(wl, 0);
297 }
298
299 if (intr & WL1251_ACX_INTR_EVENT_B) {
300 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT_B");
301 wl1251_event_handle(wl, 1);
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200302 }
Kalle Valo0e71bb02009-08-07 13:33:57 +0300303
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200304 if (intr & WL1251_ACX_INTR_INIT_COMPLETE)
305 wl1251_debug(DEBUG_IRQ,
306 "WL1251_ACX_INTR_INIT_COMPLETE");
Kalle Valo0e71bb02009-08-07 13:33:57 +0300307
Juuso Oikarinen79553f82009-11-17 18:49:46 +0200308 if (--ctr == 0)
309 break;
Kalle Valo0e71bb02009-08-07 13:33:57 +0300310
Juuso Oikarinen79553f82009-11-17 18:49:46 +0200311 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
312 } while (intr);
Kalle Valo0e71bb02009-08-07 13:33:57 +0300313
314out_sleep:
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200315 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, ~(wl->intr_mask));
Kalle Valo0e71bb02009-08-07 13:33:57 +0300316 wl1251_ps_elp_sleep(wl);
317
318out:
319 mutex_unlock(&wl->mutex);
320}
321
Kalle Valoae46ae12009-08-07 13:34:42 +0300322static int wl1251_join(struct wl1251 *wl, u8 bss_type, u8 channel,
323 u16 beacon_interval, u8 dtim_period)
324{
325 int ret;
326
327 ret = wl1251_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
328 DEFAULT_HW_GEN_MODULATION_TYPE,
329 wl->tx_mgmt_frm_rate,
330 wl->tx_mgmt_frm_mod);
331 if (ret < 0)
332 goto out;
333
334
335 ret = wl1251_cmd_join(wl, bss_type, channel, beacon_interval,
336 dtim_period);
337 if (ret < 0)
338 goto out;
339
Grazvydas Ignotas7273b972010-08-17 22:46:55 +0300340 ret = wl1251_event_wait(wl, JOIN_EVENT_COMPLETE_ID, 100);
341 if (ret < 0)
342 wl1251_warning("join timeout");
Kalle Valoae46ae12009-08-07 13:34:42 +0300343
344out:
345 return ret;
346}
347
Kalle Valo80301cd2009-06-12 14:17:39 +0300348static void wl1251_filter_work(struct work_struct *work)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300349{
Kalle Valo80301cd2009-06-12 14:17:39 +0300350 struct wl1251 *wl =
351 container_of(work, struct wl1251, filter_work);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300352 int ret;
353
354 mutex_lock(&wl->mutex);
355
Kalle Valo80301cd2009-06-12 14:17:39 +0300356 if (wl->state == WL1251_STATE_OFF)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300357 goto out;
358
Kalle Valo80301cd2009-06-12 14:17:39 +0300359 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300360 if (ret < 0)
361 goto out;
362
Kalle Valoae46ae12009-08-07 13:34:42 +0300363 ret = wl1251_join(wl, wl->bss_type, wl->channel, wl->beacon_int,
364 wl->dtim_period);
Kalle Valoc5483b72009-06-12 14:16:32 +0300365 if (ret < 0)
366 goto out_sleep;
367
368out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300369 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300370
371out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300372 mutex_unlock(&wl->mutex);
373}
374
Kalle Valo80301cd2009-06-12 14:17:39 +0300375static int wl1251_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300376{
Kalle Valo80301cd2009-06-12 14:17:39 +0300377 struct wl1251 *wl = hw->priv;
Denis 'GNUtoo' Carikli9df86e22010-08-27 23:48:19 +0200378 unsigned long flags;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300379
380 skb_queue_tail(&wl->tx_queue, skb);
381
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +0300382 /*
383 * The chip specific setup must run before the first TX packet -
384 * before that, the tx_work will not be initialized!
385 */
386
Kalle Valo16e711f2009-08-07 13:35:04 +0300387 ieee80211_queue_work(wl->hw, &wl->tx_work);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300388
389 /*
390 * The workqueue is slow to process the tx_queue and we need stop
391 * the queue here, otherwise the queue will get too long.
392 */
Denis 'GNUtoo' Carikli9df86e22010-08-27 23:48:19 +0200393 if (skb_queue_len(&wl->tx_queue) >= WL1251_TX_QUEUE_HIGH_WATERMARK) {
Kalle Valod67e2612009-11-30 10:17:45 +0200394 wl1251_debug(DEBUG_TX, "op_tx: tx_queue full, stop queues");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300395
Denis 'GNUtoo' Carikli9df86e22010-08-27 23:48:19 +0200396 spin_lock_irqsave(&wl->wl_lock, flags);
397 ieee80211_stop_queues(wl->hw);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300398 wl->tx_queue_stopped = true;
Denis 'GNUtoo' Carikli9df86e22010-08-27 23:48:19 +0200399 spin_unlock_irqrestore(&wl->wl_lock, flags);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300400 }
401
402 return NETDEV_TX_OK;
403}
404
Kalle Valo80301cd2009-06-12 14:17:39 +0300405static int wl1251_op_start(struct ieee80211_hw *hw)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300406{
Kalle Valo80301cd2009-06-12 14:17:39 +0300407 struct wl1251 *wl = hw->priv;
John W. Linville8b28e822010-07-28 16:59:41 -0400408 struct wiphy *wiphy = hw->wiphy;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300409 int ret = 0;
410
Kalle Valo80301cd2009-06-12 14:17:39 +0300411 wl1251_debug(DEBUG_MAC80211, "mac80211 start");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300412
413 mutex_lock(&wl->mutex);
414
Kalle Valo80301cd2009-06-12 14:17:39 +0300415 if (wl->state != WL1251_STATE_OFF) {
416 wl1251_error("cannot start because not in off state: %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300417 wl->state);
418 ret = -EBUSY;
419 goto out;
420 }
421
Kalle Valo80301cd2009-06-12 14:17:39 +0300422 ret = wl1251_chip_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300423 if (ret < 0)
Jiri Slabyfe643412009-07-14 22:37:13 +0200424 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300425
Kalle Valo0e71bb02009-08-07 13:33:57 +0300426 ret = wl1251_boot(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300427 if (ret < 0)
428 goto out;
429
Kalle Valo0e71bb02009-08-07 13:33:57 +0300430 ret = wl1251_hw_init(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300431 if (ret < 0)
432 goto out;
433
Kalle Valo80301cd2009-06-12 14:17:39 +0300434 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300435 if (ret < 0)
436 goto out;
437
Kalle Valo80301cd2009-06-12 14:17:39 +0300438 wl->state = WL1251_STATE_ON;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300439
Kalle Valo0e71bb02009-08-07 13:33:57 +0300440 wl1251_info("firmware booted (%s)", wl->fw_ver);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300441
John W. Linville8b28e822010-07-28 16:59:41 -0400442 /* update hw/fw version info in wiphy struct */
443 wiphy->hw_version = wl->chip_id;
444 strncpy(wiphy->fw_version, wl->fw_ver, sizeof(wiphy->fw_version));
445
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300446out:
447 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300448 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300449
450 mutex_unlock(&wl->mutex);
451
452 return ret;
453}
454
Kalle Valo80301cd2009-06-12 14:17:39 +0300455static void wl1251_op_stop(struct ieee80211_hw *hw)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300456{
Kalle Valo80301cd2009-06-12 14:17:39 +0300457 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300458
Kalle Valo80301cd2009-06-12 14:17:39 +0300459 wl1251_info("down");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300460
Kalle Valo80301cd2009-06-12 14:17:39 +0300461 wl1251_debug(DEBUG_MAC80211, "mac80211 stop");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300462
463 mutex_lock(&wl->mutex);
464
Kalle Valo80301cd2009-06-12 14:17:39 +0300465 WARN_ON(wl->state != WL1251_STATE_ON);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300466
467 if (wl->scanning) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300468 ieee80211_scan_completed(wl->hw, true);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300469 wl->scanning = false;
470 }
471
Kalle Valo80301cd2009-06-12 14:17:39 +0300472 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300473
Kalle Valo80301cd2009-06-12 14:17:39 +0300474 wl1251_disable_interrupts(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300475
476 mutex_unlock(&wl->mutex);
477
478 cancel_work_sync(&wl->irq_work);
479 cancel_work_sync(&wl->tx_work);
480 cancel_work_sync(&wl->filter_work);
481
482 mutex_lock(&wl->mutex);
483
484 /* let's notify MAC80211 about the remaining pending TX frames */
Kalle Valo0e71bb02009-08-07 13:33:57 +0300485 wl1251_tx_flush(wl);
Kalle Valo80301cd2009-06-12 14:17:39 +0300486 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300487
488 memset(wl->bssid, 0, ETH_ALEN);
489 wl->listen_int = 1;
490 wl->bss_type = MAX_BSS_TYPE;
491
492 wl->data_in_count = 0;
493 wl->rx_counter = 0;
494 wl->rx_handled = 0;
495 wl->rx_current_buffer = 0;
496 wl->rx_last_id = 0;
497 wl->next_tx_complete = 0;
498 wl->elp = false;
499 wl->psm = 0;
500 wl->tx_queue_stopped = false;
Kalle Valo80301cd2009-06-12 14:17:39 +0300501 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
Kalle Valo97802792009-08-07 13:34:27 +0300502 wl->channel = WL1251_DEFAULT_CHANNEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300503
Kalle Valo80301cd2009-06-12 14:17:39 +0300504 wl1251_debugfs_reset(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300505
506 mutex_unlock(&wl->mutex);
507}
508
Kalle Valo80301cd2009-06-12 14:17:39 +0300509static int wl1251_op_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100510 struct ieee80211_vif *vif)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300511{
Kalle Valo80301cd2009-06-12 14:17:39 +0300512 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300513 int ret = 0;
514
Johannes Berge91d8332009-07-15 17:21:41 +0200515 wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
Johannes Berg1ed32e42009-12-23 13:15:45 +0100516 vif->type, vif->addr);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300517
518 mutex_lock(&wl->mutex);
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200519 if (wl->vif) {
520 ret = -EBUSY;
521 goto out;
522 }
523
Johannes Berg1ed32e42009-12-23 13:15:45 +0100524 wl->vif = vif;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300525
Johannes Berg1ed32e42009-12-23 13:15:45 +0100526 switch (vif->type) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300527 case NL80211_IFTYPE_STATION:
528 wl->bss_type = BSS_TYPE_STA_BSS;
529 break;
530 case NL80211_IFTYPE_ADHOC:
531 wl->bss_type = BSS_TYPE_IBSS;
532 break;
533 default:
534 ret = -EOPNOTSUPP;
535 goto out;
536 }
537
Johannes Berg1ed32e42009-12-23 13:15:45 +0100538 if (memcmp(wl->mac_addr, vif->addr, ETH_ALEN)) {
539 memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300540 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
Kalle Valo80301cd2009-06-12 14:17:39 +0300541 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300542 if (ret < 0)
543 goto out;
544 }
545
546out:
547 mutex_unlock(&wl->mutex);
548 return ret;
549}
550
Kalle Valo80301cd2009-06-12 14:17:39 +0300551static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100552 struct ieee80211_vif *vif)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300553{
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200554 struct wl1251 *wl = hw->priv;
555
556 mutex_lock(&wl->mutex);
Kalle Valo80301cd2009-06-12 14:17:39 +0300557 wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200558 wl->vif = NULL;
559 mutex_unlock(&wl->mutex);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300560}
561
Kalle Valo8f8ff9162010-01-12 10:43:07 +0200562static int wl1251_build_qos_null_data(struct wl1251 *wl)
563{
564 struct ieee80211_qos_hdr template;
565
566 memset(&template, 0, sizeof(template));
567
568 memcpy(template.addr1, wl->bssid, ETH_ALEN);
569 memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
570 memcpy(template.addr3, wl->bssid, ETH_ALEN);
571
572 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
573 IEEE80211_STYPE_QOS_NULLFUNC |
574 IEEE80211_FCTL_TODS);
575
576 /* FIXME: not sure what priority to use here */
577 template.qos_ctrl = cpu_to_le16(0);
578
579 return wl1251_cmd_template_set(wl, CMD_QOS_NULL_DATA, &template,
580 sizeof(template));
581}
582
Kalle Valo80301cd2009-06-12 14:17:39 +0300583static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300584{
Kalle Valo80301cd2009-06-12 14:17:39 +0300585 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300586 struct ieee80211_conf *conf = &hw->conf;
587 int channel, ret = 0;
588
589 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
590
Kalle Valo80301cd2009-06-12 14:17:39 +0300591 wl1251_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300592 channel,
593 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
594 conf->power_level);
595
596 mutex_lock(&wl->mutex);
597
Kalle Valo80301cd2009-06-12 14:17:39 +0300598 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300599 if (ret < 0)
600 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300601
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300602 if (channel != wl->channel) {
Kalle Valofe9a9842009-08-07 13:34:49 +0300603 wl->channel = channel;
604
Kalle Valoae46ae12009-08-07 13:34:42 +0300605 ret = wl1251_join(wl, wl->bss_type, wl->channel,
606 wl->beacon_int, wl->dtim_period);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300607 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300608 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300609 }
610
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300611 if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
Luciano Coelho47af3fe2009-06-12 14:17:53 +0300612 wl1251_debug(DEBUG_PSM, "psm enabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300613
614 wl->psm_requested = true;
615
Johannes Berg56007a02010-01-26 14:19:52 +0100616 wl->dtim_period = conf->ps_dtim_period;
617
618 ret = wl1251_acx_wr_tbtt_and_dtim(wl, wl->beacon_int,
619 wl->dtim_period);
620
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300621 /*
Johannes Berg56007a02010-01-26 14:19:52 +0100622 * mac80211 enables PSM only if we're already associated.
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300623 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300624 ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
Kalle Valo478fdf22009-11-30 10:17:52 +0200625 if (ret < 0)
626 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300627 } else if (!(conf->flags & IEEE80211_CONF_PS) &&
628 wl->psm_requested) {
Luciano Coelho47af3fe2009-06-12 14:17:53 +0300629 wl1251_debug(DEBUG_PSM, "psm disabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300630
631 wl->psm_requested = false;
632
Kalle Valo478fdf22009-11-30 10:17:52 +0200633 if (wl->psm) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300634 ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
Kalle Valo478fdf22009-11-30 10:17:52 +0200635 if (ret < 0)
636 goto out_sleep;
637 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300638 }
639
640 if (conf->power_level != wl->power_level) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300641 ret = wl1251_acx_tx_power(wl, conf->power_level);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300642 if (ret < 0)
Kalle Valo478fdf22009-11-30 10:17:52 +0200643 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300644
645 wl->power_level = conf->power_level;
646 }
647
Kalle Valoc5483b72009-06-12 14:16:32 +0300648out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300649 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300650
651out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300652 mutex_unlock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +0300653
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300654 return ret;
655}
656
Kalle Valo80301cd2009-06-12 14:17:39 +0300657#define WL1251_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300658 FIF_ALLMULTI | \
659 FIF_FCSFAIL | \
660 FIF_BCN_PRBRESP_PROMISC | \
661 FIF_CONTROL | \
662 FIF_OTHER_BSS)
663
Kalle Valo80301cd2009-06-12 14:17:39 +0300664static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300665 unsigned int changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +0200666 unsigned int *total,u64 multicast)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300667{
Kalle Valo80301cd2009-06-12 14:17:39 +0300668 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300669
Kalle Valo80301cd2009-06-12 14:17:39 +0300670 wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300671
Kalle Valo80301cd2009-06-12 14:17:39 +0300672 *total &= WL1251_SUPPORTED_FILTERS;
673 changed &= WL1251_SUPPORTED_FILTERS;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300674
675 if (changed == 0)
676 /* no filters which we support changed */
677 return;
678
679 /* FIXME: wl->rx_config and wl->rx_filter are not protected */
680
Kalle Valo80301cd2009-06-12 14:17:39 +0300681 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
682 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300683
684 if (*total & FIF_PROMISC_IN_BSS) {
685 wl->rx_config |= CFG_BSSID_FILTER_EN;
686 wl->rx_config |= CFG_RX_ALL_GOOD;
687 }
688 if (*total & FIF_ALLMULTI)
689 /*
690 * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
691 * all multicast frames
692 */
693 wl->rx_config &= ~CFG_MC_FILTER_EN;
694 if (*total & FIF_FCSFAIL)
695 wl->rx_filter |= CFG_RX_FCS_ERROR;
696 if (*total & FIF_BCN_PRBRESP_PROMISC) {
697 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
698 wl->rx_config &= ~CFG_SSID_FILTER_EN;
699 }
700 if (*total & FIF_CONTROL)
701 wl->rx_filter |= CFG_RX_CTL_EN;
702 if (*total & FIF_OTHER_BSS)
703 wl->rx_filter &= ~CFG_BSSID_FILTER_EN;
704
705 /*
706 * FIXME: workqueues need to be properly cancelled on stop(), for
707 * now let's just disable changing the filter settings. They will
708 * be updated any on config().
709 */
710 /* schedule_work(&wl->filter_work); */
711}
712
713/* HW encryption */
Kalle Valo80301cd2009-06-12 14:17:39 +0300714static int wl1251_set_key_type(struct wl1251 *wl,
715 struct wl1251_cmd_set_keys *key,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300716 enum set_key_cmd cmd,
717 struct ieee80211_key_conf *mac80211_key,
718 const u8 *addr)
719{
Johannes Berg97359d12010-08-10 09:46:38 +0200720 switch (mac80211_key->cipher) {
721 case WLAN_CIPHER_SUITE_WEP40:
722 case WLAN_CIPHER_SUITE_WEP104:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300723 if (is_broadcast_ether_addr(addr))
724 key->key_type = KEY_WEP_DEFAULT;
725 else
726 key->key_type = KEY_WEP_ADDR;
727
728 mac80211_key->hw_key_idx = mac80211_key->keyidx;
729 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200730 case WLAN_CIPHER_SUITE_TKIP:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300731 if (is_broadcast_ether_addr(addr))
732 key->key_type = KEY_TKIP_MIC_GROUP;
733 else
734 key->key_type = KEY_TKIP_MIC_PAIRWISE;
735
736 mac80211_key->hw_key_idx = mac80211_key->keyidx;
737 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200738 case WLAN_CIPHER_SUITE_CCMP:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300739 if (is_broadcast_ether_addr(addr))
740 key->key_type = KEY_AES_GROUP;
741 else
742 key->key_type = KEY_AES_PAIRWISE;
743 mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
744 break;
745 default:
Johannes Berg97359d12010-08-10 09:46:38 +0200746 wl1251_error("Unknown key cipher 0x%x", mac80211_key->cipher);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300747 return -EOPNOTSUPP;
748 }
749
750 return 0;
751}
752
Kalle Valo80301cd2009-06-12 14:17:39 +0300753static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300754 struct ieee80211_vif *vif,
755 struct ieee80211_sta *sta,
756 struct ieee80211_key_conf *key)
757{
Kalle Valo80301cd2009-06-12 14:17:39 +0300758 struct wl1251 *wl = hw->priv;
759 struct wl1251_cmd_set_keys *wl_cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300760 const u8 *addr;
761 int ret;
762
763 static const u8 bcast_addr[ETH_ALEN] =
764 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
765
Kalle Valo80301cd2009-06-12 14:17:39 +0300766 wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300767
Kalle Valoff258392009-06-12 14:14:19 +0300768 wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
769 if (!wl_cmd) {
770 ret = -ENOMEM;
771 goto out;
772 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300773
774 addr = sta ? sta->addr : bcast_addr;
775
Kalle Valo80301cd2009-06-12 14:17:39 +0300776 wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
777 wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
778 wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
Johannes Berg97359d12010-08-10 09:46:38 +0200779 key->cipher, key->keyidx, key->keylen, key->flags);
Kalle Valo80301cd2009-06-12 14:17:39 +0300780 wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300781
Kalle Valoff258392009-06-12 14:14:19 +0300782 if (is_zero_ether_addr(addr)) {
783 /* We dont support TX only encryption */
784 ret = -EOPNOTSUPP;
785 goto out;
786 }
787
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300788 mutex_lock(&wl->mutex);
789
Kalle Valo80301cd2009-06-12 14:17:39 +0300790 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300791 if (ret < 0)
792 goto out_unlock;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300793
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300794 switch (cmd) {
795 case SET_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300796 wl_cmd->key_action = KEY_ADD_OR_REPLACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300797 break;
798 case DISABLE_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300799 wl_cmd->key_action = KEY_REMOVE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300800 break;
801 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300802 wl1251_error("Unsupported key cmd 0x%x", cmd);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300803 break;
804 }
805
Kalle Valo80301cd2009-06-12 14:17:39 +0300806 ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300807 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300808 wl1251_error("Set KEY type failed");
Kalle Valoc5483b72009-06-12 14:16:32 +0300809 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300810 }
811
Kalle Valoff258392009-06-12 14:14:19 +0300812 if (wl_cmd->key_type != KEY_WEP_DEFAULT)
813 memcpy(wl_cmd->addr, addr, ETH_ALEN);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300814
Kalle Valoff258392009-06-12 14:14:19 +0300815 if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
816 (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300817 /*
818 * We get the key in the following form:
819 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
820 * but the target is expecting:
821 * TKIP - RX MIC - TX MIC
822 */
Kalle Valoff258392009-06-12 14:14:19 +0300823 memcpy(wl_cmd->key, key->key, 16);
824 memcpy(wl_cmd->key + 16, key->key + 24, 8);
825 memcpy(wl_cmd->key + 24, key->key + 16, 8);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300826
827 } else {
Kalle Valoff258392009-06-12 14:14:19 +0300828 memcpy(wl_cmd->key, key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300829 }
Kalle Valoff258392009-06-12 14:14:19 +0300830 wl_cmd->key_size = key->keylen;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300831
Kalle Valoff258392009-06-12 14:14:19 +0300832 wl_cmd->id = key->keyidx;
833 wl_cmd->ssid_profile = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300834
Kalle Valo80301cd2009-06-12 14:17:39 +0300835 wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300836
Kalle Valo80301cd2009-06-12 14:17:39 +0300837 ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
Kalle Valoff258392009-06-12 14:14:19 +0300838 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300839 wl1251_warning("could not set keys");
Kalle Valoc5483b72009-06-12 14:16:32 +0300840 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300841 }
842
Kalle Valoc5483b72009-06-12 14:16:32 +0300843out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300844 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300845
846out_unlock:
Kalle Valoff258392009-06-12 14:14:19 +0300847 mutex_unlock(&wl->mutex);
848
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300849out:
Kalle Valoff258392009-06-12 14:14:19 +0300850 kfree(wl_cmd);
851
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300852 return ret;
853}
854
Kalle Valo80301cd2009-06-12 14:17:39 +0300855static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
Johannes Berga060bbf2010-04-27 11:59:34 +0200856 struct ieee80211_vif *vif,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300857 struct cfg80211_scan_request *req)
858{
Kalle Valo80301cd2009-06-12 14:17:39 +0300859 struct wl1251 *wl = hw->priv;
Kalle Valoe477c562010-01-05 20:16:57 +0200860 struct sk_buff *skb;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300861 size_t ssid_len = 0;
Kalle Valo3a98c302010-01-05 20:16:51 +0200862 u8 *ssid = NULL;
863 int ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300864
Kalle Valo80301cd2009-06-12 14:17:39 +0300865 wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300866
867 if (req->n_ssids) {
868 ssid = req->ssids[0].ssid;
869 ssid_len = req->ssids[0].ssid_len;
870 }
871
872 mutex_lock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +0300873
Kalle Valo3a98c302010-01-05 20:16:51 +0200874 if (wl->scanning) {
875 wl1251_debug(DEBUG_SCAN, "scan already in progress");
876 ret = -EINVAL;
877 goto out;
878 }
879
Kalle Valo80301cd2009-06-12 14:17:39 +0300880 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300881 if (ret < 0)
882 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300883
Kalle Valoe477c562010-01-05 20:16:57 +0200884 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
885 req->ie, req->ie_len);
886 if (!skb) {
887 ret = -ENOMEM;
888 goto out;
889 }
890
891 ret = wl1251_cmd_template_set(wl, CMD_PROBE_REQ, skb->data,
892 skb->len);
893 dev_kfree_skb(skb);
Kalle Valo3a98c302010-01-05 20:16:51 +0200894 if (ret < 0)
Kalle Valoe477c562010-01-05 20:16:57 +0200895 goto out_sleep;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300896
Kalle Valo3a98c302010-01-05 20:16:51 +0200897 ret = wl1251_cmd_trigger_scan_to(wl, 0);
898 if (ret < 0)
899 goto out_sleep;
900
901 wl->scanning = true;
902
Kalle Valodc52f0a2010-01-05 20:17:03 +0200903 ret = wl1251_cmd_scan(wl, ssid, ssid_len, req->channels,
904 req->n_channels, WL1251_SCAN_NUM_PROBES);
Kalle Valo3a98c302010-01-05 20:16:51 +0200905 if (ret < 0) {
906 wl->scanning = false;
907 goto out_sleep;
908 }
909
910out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300911 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300912
913out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300914 mutex_unlock(&wl->mutex);
915
916 return ret;
917}
918
Kalle Valo80301cd2009-06-12 14:17:39 +0300919static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300920{
Kalle Valo80301cd2009-06-12 14:17:39 +0300921 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300922 int ret;
923
Kalle Valocee4fd22009-06-12 14:16:20 +0300924 mutex_lock(&wl->mutex);
925
Kalle Valo80301cd2009-06-12 14:17:39 +0300926 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300927 if (ret < 0)
928 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300929
Kalle Valo80301cd2009-06-12 14:17:39 +0300930 ret = wl1251_acx_rts_threshold(wl, (u16) value);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300931 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300932 wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300933
Kalle Valo80301cd2009-06-12 14:17:39 +0300934 wl1251_ps_elp_sleep(wl);
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300935
Kalle Valoc5483b72009-06-12 14:16:32 +0300936out:
Kalle Valocee4fd22009-06-12 14:16:20 +0300937 mutex_unlock(&wl->mutex);
938
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300939 return ret;
940}
941
Kalle Valo80301cd2009-06-12 14:17:39 +0300942static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300943 struct ieee80211_vif *vif,
944 struct ieee80211_bss_conf *bss_conf,
945 u32 changed)
946{
Kalle Valo80301cd2009-06-12 14:17:39 +0300947 struct wl1251 *wl = hw->priv;
Kalle Valof7f70572010-01-05 20:16:32 +0200948 struct sk_buff *beacon, *skb;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300949 int ret;
950
Kalle Valo80301cd2009-06-12 14:17:39 +0300951 wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300952
953 mutex_lock(&wl->mutex);
954
Kalle Valo80301cd2009-06-12 14:17:39 +0300955 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300956 if (ret < 0)
957 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300958
Kalle Valode8df1e2009-11-26 10:56:06 +0200959 if (changed & BSS_CHANGED_BSSID) {
960 memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
961
Kalle Valof7f70572010-01-05 20:16:32 +0200962 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
963 if (!skb)
964 goto out_sleep;
965
966 ret = wl1251_cmd_template_set(wl, CMD_NULL_DATA,
967 skb->data, skb->len);
968 dev_kfree_skb(skb);
Kalle Valode8df1e2009-11-26 10:56:06 +0200969 if (ret < 0)
Kalle Valo80a112f2010-01-05 20:17:10 +0200970 goto out_sleep;
Kalle Valode8df1e2009-11-26 10:56:06 +0200971
Kalle Valo8f8ff9162010-01-12 10:43:07 +0200972 ret = wl1251_build_qos_null_data(wl);
973 if (ret < 0)
974 goto out;
975
Kalle Valode8df1e2009-11-26 10:56:06 +0200976 if (wl->bss_type != BSS_TYPE_IBSS) {
977 ret = wl1251_join(wl, wl->bss_type, wl->channel,
978 wl->beacon_int, wl->dtim_period);
979 if (ret < 0)
980 goto out_sleep;
981 }
982 }
983
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300984 if (changed & BSS_CHANGED_ASSOC) {
985 if (bss_conf->assoc) {
Kalle Valoe2fd4612009-08-07 13:34:12 +0300986 wl->beacon_int = bss_conf->beacon_int;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300987
Kalle Valof7f70572010-01-05 20:16:32 +0200988 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
989 if (!skb)
990 goto out_sleep;
991
992 ret = wl1251_cmd_template_set(wl, CMD_PS_POLL,
993 skb->data,
994 skb->len);
995 dev_kfree_skb(skb);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300996 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300997 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300998
Johannes Berg56007a02010-01-26 14:19:52 +0100999 ret = wl1251_acx_aid(wl, bss_conf->aid);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001000 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001001 goto out_sleep;
Kalle Valoe2fd4612009-08-07 13:34:12 +03001002 } else {
1003 /* use defaults when not associated */
1004 wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
1005 wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001006 }
1007 }
1008 if (changed & BSS_CHANGED_ERP_SLOT) {
1009 if (bss_conf->use_short_slot)
Kalle Valo80301cd2009-06-12 14:17:39 +03001010 ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001011 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001012 ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001013 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001014 wl1251_warning("Set slot time failed %d", ret);
Kalle Valoc5483b72009-06-12 14:16:32 +03001015 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001016 }
1017 }
1018
1019 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1020 if (bss_conf->use_short_preamble)
Kalle Valo80301cd2009-06-12 14:17:39 +03001021 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001022 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001023 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001024 }
1025
1026 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1027 if (bss_conf->use_cts_prot)
Kalle Valo80301cd2009-06-12 14:17:39 +03001028 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001029 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001030 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001031 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001032 wl1251_warning("Set ctsprotect failed %d", ret);
Kalle Valo80a112f2010-01-05 20:17:10 +02001033 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001034 }
1035 }
1036
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001037 if (changed & BSS_CHANGED_BEACON) {
1038 beacon = ieee80211_beacon_get(hw, vif);
Kalle Valo80301cd2009-06-12 14:17:39 +03001039 ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001040 beacon->len);
1041
1042 if (ret < 0) {
1043 dev_kfree_skb(beacon);
Kalle Valo80a112f2010-01-05 20:17:10 +02001044 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001045 }
1046
Kalle Valo80301cd2009-06-12 14:17:39 +03001047 ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001048 beacon->len);
1049
1050 dev_kfree_skb(beacon);
1051
1052 if (ret < 0)
Kalle Valo80a112f2010-01-05 20:17:10 +02001053 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001054
Kalle Valoae46ae12009-08-07 13:34:42 +03001055 ret = wl1251_join(wl, wl->bss_type, wl->beacon_int,
1056 wl->channel, wl->dtim_period);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001057
1058 if (ret < 0)
Kalle Valo80a112f2010-01-05 20:17:10 +02001059 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001060 }
1061
Kalle Valoc5483b72009-06-12 14:16:32 +03001062out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +03001063 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001064
1065out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001066 mutex_unlock(&wl->mutex);
1067}
1068
1069
1070/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001071static struct ieee80211_rate wl1251_rates[] = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001072 { .bitrate = 10,
1073 .hw_value = 0x1,
1074 .hw_value_short = 0x1, },
1075 { .bitrate = 20,
1076 .hw_value = 0x2,
1077 .hw_value_short = 0x2,
1078 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1079 { .bitrate = 55,
1080 .hw_value = 0x4,
1081 .hw_value_short = 0x4,
1082 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1083 { .bitrate = 110,
1084 .hw_value = 0x20,
1085 .hw_value_short = 0x20,
1086 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1087 { .bitrate = 60,
1088 .hw_value = 0x8,
1089 .hw_value_short = 0x8, },
1090 { .bitrate = 90,
1091 .hw_value = 0x10,
1092 .hw_value_short = 0x10, },
1093 { .bitrate = 120,
1094 .hw_value = 0x40,
1095 .hw_value_short = 0x40, },
1096 { .bitrate = 180,
1097 .hw_value = 0x80,
1098 .hw_value_short = 0x80, },
1099 { .bitrate = 240,
1100 .hw_value = 0x200,
1101 .hw_value_short = 0x200, },
1102 { .bitrate = 360,
1103 .hw_value = 0x400,
1104 .hw_value_short = 0x400, },
1105 { .bitrate = 480,
1106 .hw_value = 0x800,
1107 .hw_value_short = 0x800, },
1108 { .bitrate = 540,
1109 .hw_value = 0x1000,
1110 .hw_value_short = 0x1000, },
1111};
1112
1113/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001114static struct ieee80211_channel wl1251_channels[] = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001115 { .hw_value = 1, .center_freq = 2412},
1116 { .hw_value = 2, .center_freq = 2417},
1117 { .hw_value = 3, .center_freq = 2422},
1118 { .hw_value = 4, .center_freq = 2427},
1119 { .hw_value = 5, .center_freq = 2432},
1120 { .hw_value = 6, .center_freq = 2437},
1121 { .hw_value = 7, .center_freq = 2442},
1122 { .hw_value = 8, .center_freq = 2447},
1123 { .hw_value = 9, .center_freq = 2452},
1124 { .hw_value = 10, .center_freq = 2457},
1125 { .hw_value = 11, .center_freq = 2462},
1126 { .hw_value = 12, .center_freq = 2467},
1127 { .hw_value = 13, .center_freq = 2472},
1128};
1129
Kalle Valo86dff7a2009-11-30 10:18:19 +02001130static int wl1251_op_conf_tx(struct ieee80211_hw *hw, u16 queue,
1131 const struct ieee80211_tx_queue_params *params)
1132{
Kalle Valo4ff6ffa2010-01-12 10:43:15 +02001133 enum wl1251_acx_ps_scheme ps_scheme;
Kalle Valo86dff7a2009-11-30 10:18:19 +02001134 struct wl1251 *wl = hw->priv;
1135 int ret;
1136
1137 mutex_lock(&wl->mutex);
1138
1139 wl1251_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
1140
1141 ret = wl1251_ps_elp_wakeup(wl);
1142 if (ret < 0)
1143 goto out;
1144
Kalle Valo85359492010-02-04 15:33:25 +02001145 /* mac80211 uses units of 32 usec */
Kalle Valo86dff7a2009-11-30 10:18:19 +02001146 ret = wl1251_acx_ac_cfg(wl, wl1251_tx_get_queue(queue),
1147 params->cw_min, params->cw_max,
Kalle Valo85359492010-02-04 15:33:25 +02001148 params->aifs, params->txop * 32);
Kalle Valo27336f12009-11-30 10:18:27 +02001149 if (ret < 0)
1150 goto out_sleep;
Kalle Valo86dff7a2009-11-30 10:18:19 +02001151
Kalle Valo4ff6ffa2010-01-12 10:43:15 +02001152 if (params->uapsd)
1153 ps_scheme = WL1251_ACX_PS_SCHEME_UPSD_TRIGGER;
1154 else
1155 ps_scheme = WL1251_ACX_PS_SCHEME_LEGACY;
1156
Kalle Valo27336f12009-11-30 10:18:27 +02001157 ret = wl1251_acx_tid_cfg(wl, wl1251_tx_get_queue(queue),
Kalle Valo49e1b9f2009-11-30 10:18:33 +02001158 CHANNEL_TYPE_EDCF,
Kalle Valo4ff6ffa2010-01-12 10:43:15 +02001159 wl1251_tx_get_queue(queue), ps_scheme,
Kalle Valo27336f12009-11-30 10:18:27 +02001160 WL1251_ACX_ACK_POLICY_LEGACY);
1161 if (ret < 0)
1162 goto out_sleep;
1163
1164out_sleep:
Kalle Valo86dff7a2009-11-30 10:18:19 +02001165 wl1251_ps_elp_sleep(wl);
1166
1167out:
1168 mutex_unlock(&wl->mutex);
1169
1170 return ret;
1171}
1172
John W. Linville19434142010-07-28 15:23:30 -04001173static int wl1251_op_get_survey(struct ieee80211_hw *hw, int idx,
1174 struct survey_info *survey)
1175{
1176 struct wl1251 *wl = hw->priv;
1177 struct ieee80211_conf *conf = &hw->conf;
1178
1179 if (idx != 0)
1180 return -ENOENT;
1181
1182 survey->channel = conf->channel;
1183 survey->filled = SURVEY_INFO_NOISE_DBM;
1184 survey->noise = wl->noise;
1185
1186 return 0;
1187}
1188
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001189/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001190static struct ieee80211_supported_band wl1251_band_2ghz = {
1191 .channels = wl1251_channels,
1192 .n_channels = ARRAY_SIZE(wl1251_channels),
1193 .bitrates = wl1251_rates,
1194 .n_bitrates = ARRAY_SIZE(wl1251_rates),
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001195};
1196
Kalle Valo80301cd2009-06-12 14:17:39 +03001197static const struct ieee80211_ops wl1251_ops = {
1198 .start = wl1251_op_start,
1199 .stop = wl1251_op_stop,
1200 .add_interface = wl1251_op_add_interface,
1201 .remove_interface = wl1251_op_remove_interface,
1202 .config = wl1251_op_config,
1203 .configure_filter = wl1251_op_configure_filter,
1204 .tx = wl1251_op_tx,
1205 .set_key = wl1251_op_set_key,
1206 .hw_scan = wl1251_op_hw_scan,
1207 .bss_info_changed = wl1251_op_bss_info_changed,
1208 .set_rts_threshold = wl1251_op_set_rts_threshold,
Kalle Valo86dff7a2009-11-30 10:18:19 +02001209 .conf_tx = wl1251_op_conf_tx,
John W. Linville19434142010-07-28 15:23:30 -04001210 .get_survey = wl1251_op_get_survey,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001211};
1212
Grazvydas Ignotas61c2a802010-04-14 13:05:48 +03001213static int wl1251_read_eeprom_byte(struct wl1251 *wl, off_t offset, u8 *data)
1214{
1215 unsigned long timeout;
1216
1217 wl1251_reg_write32(wl, EE_ADDR, offset);
1218 wl1251_reg_write32(wl, EE_CTL, EE_CTL_READ);
1219
1220 /* EE_CTL_READ clears when data is ready */
1221 timeout = jiffies + msecs_to_jiffies(100);
1222 while (1) {
1223 if (!(wl1251_reg_read32(wl, EE_CTL) & EE_CTL_READ))
1224 break;
1225
1226 if (time_after(jiffies, timeout))
1227 return -ETIMEDOUT;
1228
1229 msleep(1);
1230 }
1231
1232 *data = wl1251_reg_read32(wl, EE_DATA);
1233 return 0;
1234}
1235
1236static int wl1251_read_eeprom(struct wl1251 *wl, off_t offset,
1237 u8 *data, size_t len)
1238{
1239 size_t i;
1240 int ret;
1241
1242 wl1251_reg_write32(wl, EE_START, 0);
1243
1244 for (i = 0; i < len; i++) {
1245 ret = wl1251_read_eeprom_byte(wl, offset + i, &data[i]);
1246 if (ret < 0)
1247 return ret;
1248 }
1249
1250 return 0;
1251}
1252
1253static int wl1251_read_eeprom_mac(struct wl1251 *wl)
1254{
1255 u8 mac[ETH_ALEN];
1256 int i, ret;
1257
1258 wl1251_set_partition(wl, 0, 0, REGISTERS_BASE, REGISTERS_DOWN_SIZE);
1259
1260 ret = wl1251_read_eeprom(wl, 0x1c, mac, sizeof(mac));
1261 if (ret < 0) {
1262 wl1251_warning("failed to read MAC address from EEPROM");
1263 return ret;
1264 }
1265
1266 /* MAC is stored in reverse order */
1267 for (i = 0; i < ETH_ALEN; i++)
1268 wl->mac_addr[i] = mac[ETH_ALEN - i - 1];
1269
1270 return 0;
1271}
1272
Kalle Valo80301cd2009-06-12 14:17:39 +03001273static int wl1251_register_hw(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001274{
1275 int ret;
1276
1277 if (wl->mac80211_registered)
1278 return 0;
1279
1280 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1281
1282 ret = ieee80211_register_hw(wl->hw);
1283 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001284 wl1251_error("unable to register mac80211 hw: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001285 return ret;
1286 }
1287
1288 wl->mac80211_registered = true;
1289
Kalle Valo80301cd2009-06-12 14:17:39 +03001290 wl1251_notice("loaded");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001291
1292 return 0;
1293}
1294
Bob Copeland8e639c02009-08-07 13:33:26 +03001295int wl1251_init_ieee80211(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001296{
Bob Copeland8e639c02009-08-07 13:33:26 +03001297 int ret;
1298
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001299 /* The tx descriptor buffer and the TKIP space */
1300 wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +03001301 + WL1251_TKIP_IV_SPACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001302
1303 /* unit us */
1304 /* FIXME: find a proper value */
1305 wl->hw->channel_change_time = 10000;
1306
1307 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
Juuso Oikarinen6b21a2c2009-11-17 18:48:30 +02001308 IEEE80211_HW_SUPPORTS_PS |
Kalle Valo4ff6ffa2010-01-12 10:43:15 +02001309 IEEE80211_HW_BEACON_FILTER |
1310 IEEE80211_HW_SUPPORTS_UAPSD;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001311
1312 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1313 wl->hw->wiphy->max_scan_ssids = 1;
Kalle Valo80301cd2009-06-12 14:17:39 +03001314 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1251_band_2ghz;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001315
Kalle Valoda2fb4e2009-11-30 10:18:47 +02001316 wl->hw->queues = 4;
1317
Grazvydas Ignotas61c2a802010-04-14 13:05:48 +03001318 if (wl->use_eeprom)
1319 wl1251_read_eeprom_mac(wl);
1320
Bob Copeland8e639c02009-08-07 13:33:26 +03001321 ret = wl1251_register_hw(wl);
1322 if (ret)
1323 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001324
Bob Copeland8e639c02009-08-07 13:33:26 +03001325 wl1251_debugfs_init(wl);
1326 wl1251_notice("initialized");
1327
1328 ret = 0;
1329
1330out:
1331 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001332}
Bob Copelandaf8c78e2009-08-07 13:33:34 +03001333EXPORT_SYMBOL_GPL(wl1251_init_ieee80211);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001334
Bob Copeland8e639c02009-08-07 13:33:26 +03001335struct ieee80211_hw *wl1251_alloc_hw(void)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001336{
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001337 struct ieee80211_hw *hw;
Kalle Valo80301cd2009-06-12 14:17:39 +03001338 struct wl1251 *wl;
Bob Copeland8e639c02009-08-07 13:33:26 +03001339 int i;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001340 static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1341
Kalle Valo80301cd2009-06-12 14:17:39 +03001342 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001343 if (!hw) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001344 wl1251_error("could not alloc ieee80211_hw");
Bob Copeland8e639c02009-08-07 13:33:26 +03001345 return ERR_PTR(-ENOMEM);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001346 }
1347
1348 wl = hw->priv;
1349 memset(wl, 0, sizeof(*wl));
1350
1351 wl->hw = hw;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001352
1353 wl->data_in_count = 0;
1354
1355 skb_queue_head_init(&wl->tx_queue);
1356
Kalle Valo80301cd2009-06-12 14:17:39 +03001357 INIT_WORK(&wl->filter_work, wl1251_filter_work);
Juuso Oikarinend5da79a2009-11-17 18:48:37 +02001358 INIT_DELAYED_WORK(&wl->elp_work, wl1251_elp_work);
Kalle Valo80301cd2009-06-12 14:17:39 +03001359 wl->channel = WL1251_DEFAULT_CHANNEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001360 wl->scanning = false;
1361 wl->default_key = 0;
1362 wl->listen_int = 1;
1363 wl->rx_counter = 0;
1364 wl->rx_handled = 0;
1365 wl->rx_current_buffer = 0;
1366 wl->rx_last_id = 0;
Kalle Valo80301cd2009-06-12 14:17:39 +03001367 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
1368 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001369 wl->elp = false;
1370 wl->psm = 0;
1371 wl->psm_requested = false;
1372 wl->tx_queue_stopped = false;
Kalle Valo80301cd2009-06-12 14:17:39 +03001373 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
Kalle Valoe2fd4612009-08-07 13:34:12 +03001374 wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
1375 wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
Juuso Oikarinen287f6f92009-11-17 18:48:23 +02001376 wl->vif = NULL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001377
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001378 for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
1379 wl->tx_frames[i] = NULL;
1380
1381 wl->next_tx_complete = 0;
1382
Kalle Valo0e71bb02009-08-07 13:33:57 +03001383 INIT_WORK(&wl->irq_work, wl1251_irq_work);
1384 INIT_WORK(&wl->tx_work, wl1251_tx_work);
1385
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001386 /*
1387 * In case our MAC address is not correctly set,
1388 * we use a random but Nokia MAC.
1389 */
1390 memcpy(wl->mac_addr, nokia_oui, 3);
1391 get_random_bytes(wl->mac_addr + 3, 3);
1392
Kalle Valo80301cd2009-06-12 14:17:39 +03001393 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001394 mutex_init(&wl->mutex);
1395
1396 wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
1397 wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
1398
Kalle Valo47212132009-06-12 14:15:08 +03001399 wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1400 if (!wl->rx_descriptor) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001401 wl1251_error("could not allocate memory for rx descriptor");
Bob Copeland8e639c02009-08-07 13:33:26 +03001402 ieee80211_free_hw(hw);
1403 return ERR_PTR(-ENOMEM);
Kalle Valo47212132009-06-12 14:15:08 +03001404 }
1405
Bob Copeland8e639c02009-08-07 13:33:26 +03001406 return hw;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001407}
Bob Copelandaf8c78e2009-08-07 13:33:34 +03001408EXPORT_SYMBOL_GPL(wl1251_alloc_hw);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001409
Bob Copeland8e639c02009-08-07 13:33:26 +03001410int wl1251_free_hw(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001411{
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001412 ieee80211_unregister_hw(wl->hw);
1413
Kalle Valo80301cd2009-06-12 14:17:39 +03001414 wl1251_debugfs_exit(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001415
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001416 kfree(wl->target_mem_map);
1417 kfree(wl->data_path);
Kalle Valoc74ddfd2009-11-17 18:48:45 +02001418 vfree(wl->fw);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001419 wl->fw = NULL;
1420 kfree(wl->nvs);
1421 wl->nvs = NULL;
Kalle Valo47212132009-06-12 14:15:08 +03001422
1423 kfree(wl->rx_descriptor);
1424 wl->rx_descriptor = NULL;
1425
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001426 ieee80211_free_hw(wl->hw);
1427
1428 return 0;
1429}
Bob Copelandaf8c78e2009-08-07 13:33:34 +03001430EXPORT_SYMBOL_GPL(wl1251_free_hw);
1431
1432MODULE_DESCRIPTION("TI wl1251 Wireles LAN Driver Core");
1433MODULE_LICENSE("GPL");
Kalle Valo31c726f2010-08-22 22:46:15 +03001434MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
Ben Hutchings49f146d2009-11-07 22:02:15 +00001435MODULE_FIRMWARE(WL1251_FW_NAME);