blob: 861a5f33761e9ea94d15d355a360835a0766ade0 [file] [log] [blame]
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001/*
Kalle Valo80301cd2009-06-12 14:17:39 +03002 * This file is part of wl1251
Kalle Valo2f01a1f2009-04-29 23:33:31 +03003 *
4 * Copyright (C) 2008-2009 Nokia Corporation
5 *
6 * Contact: Kalle Valo <kalle.valo@nokia.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 *
22 */
23
24#include <linux/module.h>
25#include <linux/interrupt.h>
26#include <linux/firmware.h>
27#include <linux/delay.h>
28#include <linux/irq.h>
Kalle Valo2f01a1f2009-04-29 23:33:31 +030029#include <linux/crc32.h>
30#include <linux/etherdevice.h>
Kalle Valoc74ddfd2009-11-17 18:48:45 +020031#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Kalle Valo2f01a1f2009-04-29 23:33:31 +030033
Kalle Valo13674112009-06-12 14:17:25 +030034#include "wl1251.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030035#include "wl12xx_80211.h"
Kalle Valo29d904c2009-08-07 13:35:11 +030036#include "wl1251_reg.h"
Bob Copeland0764de62009-08-07 13:32:56 +030037#include "wl1251_io.h"
Bob Copeland8e639c02009-08-07 13:33:26 +030038#include "wl1251_cmd.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030039#include "wl1251_event.h"
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +030040#include "wl1251_tx.h"
Kalle Valoef2f8d42009-06-12 14:17:19 +030041#include "wl1251_rx.h"
42#include "wl1251_ps.h"
43#include "wl1251_init.h"
44#include "wl1251_debugfs.h"
Kalle Valo0e71bb02009-08-07 13:33:57 +030045#include "wl1251_boot.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030046
Bob Copelandb5ed9c12009-08-07 13:33:49 +030047void wl1251_enable_interrupts(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030048{
Bob Copelandb5ed9c12009-08-07 13:33:49 +030049 wl->if_ops->enable_irq(wl);
50}
51
52void wl1251_disable_interrupts(struct wl1251 *wl)
53{
54 wl->if_ops->disable_irq(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030055}
56
Kalle Valo80301cd2009-06-12 14:17:39 +030057static void wl1251_power_off(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030058{
59 wl->set_power(false);
60}
61
Kalle Valo80301cd2009-06-12 14:17:39 +030062static void wl1251_power_on(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030063{
64 wl->set_power(true);
65}
66
Kalle Valo80301cd2009-06-12 14:17:39 +030067static int wl1251_fetch_firmware(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030068{
69 const struct firmware *fw;
Bob Copeland6c766f42009-08-07 13:33:04 +030070 struct device *dev = wiphy_dev(wl->hw->wiphy);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030071 int ret;
72
Kalle Valo0e71bb02009-08-07 13:33:57 +030073 ret = request_firmware(&fw, WL1251_FW_NAME, dev);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030074
75 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +030076 wl1251_error("could not get firmware: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030077 return ret;
78 }
79
80 if (fw->size % 4) {
Kalle Valo80301cd2009-06-12 14:17:39 +030081 wl1251_error("firmware size is not multiple of 32 bits: %zu",
Kalle Valo2f01a1f2009-04-29 23:33:31 +030082 fw->size);
83 ret = -EILSEQ;
84 goto out;
85 }
86
87 wl->fw_len = fw->size;
Kalle Valoc74ddfd2009-11-17 18:48:45 +020088 wl->fw = vmalloc(wl->fw_len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030089
90 if (!wl->fw) {
Kalle Valo80301cd2009-06-12 14:17:39 +030091 wl1251_error("could not allocate memory for the firmware");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030092 ret = -ENOMEM;
93 goto out;
94 }
95
96 memcpy(wl->fw, fw->data, wl->fw_len);
97
98 ret = 0;
99
100out:
101 release_firmware(fw);
102
103 return ret;
104}
105
Kalle Valo80301cd2009-06-12 14:17:39 +0300106static int wl1251_fetch_nvs(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300107{
108 const struct firmware *fw;
Bob Copeland6c766f42009-08-07 13:33:04 +0300109 struct device *dev = wiphy_dev(wl->hw->wiphy);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300110 int ret;
111
Kalle Valo0e71bb02009-08-07 13:33:57 +0300112 ret = request_firmware(&fw, WL1251_NVS_NAME, dev);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300113
114 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300115 wl1251_error("could not get nvs file: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300116 return ret;
117 }
118
119 if (fw->size % 4) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300120 wl1251_error("nvs size is not multiple of 32 bits: %zu",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300121 fw->size);
122 ret = -EILSEQ;
123 goto out;
124 }
125
126 wl->nvs_len = fw->size;
Julia Lawall80caf602010-05-15 23:15:10 +0200127 wl->nvs = kmemdup(fw->data, wl->nvs_len, GFP_KERNEL);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300128
129 if (!wl->nvs) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300130 wl1251_error("could not allocate memory for the nvs file");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300131 ret = -ENOMEM;
132 goto out;
133 }
134
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300135 ret = 0;
136
137out:
138 release_firmware(fw);
139
140 return ret;
141}
142
Kalle Valo80301cd2009-06-12 14:17:39 +0300143static void wl1251_fw_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300144{
145 u32 elp_reg;
146
147 elp_reg = ELPCTRL_WAKE_UP;
Grazvydas Ignotas3f9e7502010-03-11 17:44:57 +0200148 wl1251_write_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
149 elp_reg = wl1251_read_elp(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300150
Kalle Valo05fac682009-06-12 14:17:47 +0300151 if (!(elp_reg & ELPCTRL_WLAN_READY))
Kalle Valo80301cd2009-06-12 14:17:39 +0300152 wl1251_warning("WLAN not ready");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300153}
154
Kalle Valo80301cd2009-06-12 14:17:39 +0300155static int wl1251_chip_wakeup(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300156{
157 int ret = 0;
158
Kalle Valo80301cd2009-06-12 14:17:39 +0300159 wl1251_power_on(wl);
Kalle Valo0e71bb02009-08-07 13:33:57 +0300160 msleep(WL1251_POWER_ON_SLEEP);
Bob Copeland08d9f5722009-08-07 13:33:11 +0300161 wl->if_ops->reset(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300162
163 /* We don't need a real memory partition here, because we only want
164 * to use the registers at this point. */
Kalle Valo80301cd2009-06-12 14:17:39 +0300165 wl1251_set_partition(wl,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300166 0x00000000,
167 0x00000000,
168 REGISTERS_BASE,
169 REGISTERS_DOWN_SIZE);
170
171 /* ELP module wake up */
Kalle Valo80301cd2009-06-12 14:17:39 +0300172 wl1251_fw_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300173
174 /* whal_FwCtrl_BootSm() */
175
176 /* 0. read chip id from CHIP_ID */
Kalle Valo0e71bb02009-08-07 13:33:57 +0300177 wl->chip_id = wl1251_reg_read32(wl, CHIP_ID_B);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300178
179 /* 1. check if chip id is valid */
180
Kalle Valo0e71bb02009-08-07 13:33:57 +0300181 switch (wl->chip_id) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300182 case CHIP_ID_1251_PG12:
Kalle Valo80301cd2009-06-12 14:17:39 +0300183 wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG12)",
Kalle Valo0e71bb02009-08-07 13:33:57 +0300184 wl->chip_id);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300185 break;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300186 case CHIP_ID_1251_PG11:
David-John Willis2c759e02009-10-15 14:38:16 +0100187 wl1251_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG11)",
188 wl->chip_id);
189 break;
John W. Linville9a493e82009-10-27 15:15:05 -0400190 case CHIP_ID_1251_PG10:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300191 default:
Kalle Valo0e71bb02009-08-07 13:33:57 +0300192 wl1251_error("unsupported chip id: 0x%x", wl->chip_id);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300193 ret = -ENODEV;
194 goto out;
195 }
196
197 if (wl->fw == NULL) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300198 ret = wl1251_fetch_firmware(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300199 if (ret < 0)
200 goto out;
201 }
202
Grazvydas Ignotasafa5ec22010-04-13 01:21:40 +0300203 if (wl->nvs == NULL && !wl->use_eeprom) {
204 /* No NVS from netlink, try to get it from the filesystem */
Kalle Valo80301cd2009-06-12 14:17:39 +0300205 ret = wl1251_fetch_nvs(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300206 if (ret < 0)
207 goto out;
208 }
209
210out:
211 return ret;
212}
213
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200214#define WL1251_IRQ_LOOP_COUNT 10
Kalle Valo0e71bb02009-08-07 13:33:57 +0300215static void wl1251_irq_work(struct work_struct *work)
216{
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200217 u32 intr, ctr = WL1251_IRQ_LOOP_COUNT;
Kalle Valo0e71bb02009-08-07 13:33:57 +0300218 struct wl1251 *wl =
219 container_of(work, struct wl1251, irq_work);
220 int ret;
221
222 mutex_lock(&wl->mutex);
223
224 wl1251_debug(DEBUG_IRQ, "IRQ work");
225
226 if (wl->state == WL1251_STATE_OFF)
227 goto out;
228
229 ret = wl1251_ps_elp_wakeup(wl);
230 if (ret < 0)
231 goto out;
232
233 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, WL1251_ACX_INTR_ALL);
234
235 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
236 wl1251_debug(DEBUG_IRQ, "intr: 0x%x", intr);
237
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200238 do {
239 if (wl->data_path) {
240 wl->rx_counter = wl1251_mem_read32(
241 wl, wl->data_path->rx_control_addr);
Kalle Valo0e71bb02009-08-07 13:33:57 +0300242
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200243 /* We handle a frmware bug here */
244 switch ((wl->rx_counter - wl->rx_handled) & 0xf) {
245 case 0:
246 wl1251_debug(DEBUG_IRQ,
247 "RX: FW and host in sync");
248 intr &= ~WL1251_ACX_INTR_RX0_DATA;
249 intr &= ~WL1251_ACX_INTR_RX1_DATA;
250 break;
251 case 1:
252 wl1251_debug(DEBUG_IRQ, "RX: FW +1");
253 intr |= WL1251_ACX_INTR_RX0_DATA;
254 intr &= ~WL1251_ACX_INTR_RX1_DATA;
255 break;
256 case 2:
257 wl1251_debug(DEBUG_IRQ, "RX: FW +2");
258 intr |= WL1251_ACX_INTR_RX0_DATA;
259 intr |= WL1251_ACX_INTR_RX1_DATA;
260 break;
261 default:
262 wl1251_warning(
263 "RX: FW and host out of sync: %d",
264 wl->rx_counter - wl->rx_handled);
265 break;
266 }
267
268 wl->rx_handled = wl->rx_counter;
269
270 wl1251_debug(DEBUG_IRQ, "RX counter: %d",
271 wl->rx_counter);
Kalle Valo0e71bb02009-08-07 13:33:57 +0300272 }
273
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200274 intr &= wl->intr_mask;
Kalle Valo0e71bb02009-08-07 13:33:57 +0300275
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200276 if (intr == 0) {
277 wl1251_debug(DEBUG_IRQ, "INTR is 0");
278 goto out_sleep;
279 }
Kalle Valo0e71bb02009-08-07 13:33:57 +0300280
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200281 if (intr & WL1251_ACX_INTR_RX0_DATA) {
282 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX0_DATA");
283 wl1251_rx(wl);
284 }
Kalle Valo0e71bb02009-08-07 13:33:57 +0300285
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200286 if (intr & WL1251_ACX_INTR_RX1_DATA) {
287 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_RX1_DATA");
288 wl1251_rx(wl);
289 }
Kalle Valo0e71bb02009-08-07 13:33:57 +0300290
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200291 if (intr & WL1251_ACX_INTR_TX_RESULT) {
292 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_TX_RESULT");
293 wl1251_tx_complete(wl);
294 }
Kalle Valo0e71bb02009-08-07 13:33:57 +0300295
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200296 if (intr & (WL1251_ACX_INTR_EVENT_A |
297 WL1251_ACX_INTR_EVENT_B)) {
298 wl1251_debug(DEBUG_IRQ, "WL1251_ACX_INTR_EVENT (0x%x)",
299 intr);
300 if (intr & WL1251_ACX_INTR_EVENT_A)
301 wl1251_event_handle(wl, 0);
302 else
303 wl1251_event_handle(wl, 1);
304 }
Kalle Valo0e71bb02009-08-07 13:33:57 +0300305
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200306 if (intr & WL1251_ACX_INTR_INIT_COMPLETE)
307 wl1251_debug(DEBUG_IRQ,
308 "WL1251_ACX_INTR_INIT_COMPLETE");
Kalle Valo0e71bb02009-08-07 13:33:57 +0300309
Juuso Oikarinen79553f82009-11-17 18:49:46 +0200310 if (--ctr == 0)
311 break;
Kalle Valo0e71bb02009-08-07 13:33:57 +0300312
Juuso Oikarinen79553f82009-11-17 18:49:46 +0200313 intr = wl1251_reg_read32(wl, ACX_REG_INTERRUPT_CLEAR);
314 } while (intr);
Kalle Valo0e71bb02009-08-07 13:33:57 +0300315
316out_sleep:
Janne Ylalehtodcea4db2009-11-17 18:49:31 +0200317 wl1251_reg_write32(wl, ACX_REG_INTERRUPT_MASK, ~(wl->intr_mask));
Kalle Valo0e71bb02009-08-07 13:33:57 +0300318 wl1251_ps_elp_sleep(wl);
319
320out:
321 mutex_unlock(&wl->mutex);
322}
323
Kalle Valoae46ae12009-08-07 13:34:42 +0300324static int wl1251_join(struct wl1251 *wl, u8 bss_type, u8 channel,
325 u16 beacon_interval, u8 dtim_period)
326{
327 int ret;
328
329 ret = wl1251_acx_frame_rates(wl, DEFAULT_HW_GEN_TX_RATE,
330 DEFAULT_HW_GEN_MODULATION_TYPE,
331 wl->tx_mgmt_frm_rate,
332 wl->tx_mgmt_frm_mod);
333 if (ret < 0)
334 goto out;
335
336
337 ret = wl1251_cmd_join(wl, bss_type, channel, beacon_interval,
338 dtim_period);
339 if (ret < 0)
340 goto out;
341
342 /*
343 * FIXME: we should wait for JOIN_EVENT_COMPLETE_ID but to simplify
344 * locking we just sleep instead, for now
345 */
346 msleep(10);
347
348out:
349 return ret;
350}
351
Kalle Valo80301cd2009-06-12 14:17:39 +0300352static void wl1251_filter_work(struct work_struct *work)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300353{
Kalle Valo80301cd2009-06-12 14:17:39 +0300354 struct wl1251 *wl =
355 container_of(work, struct wl1251, filter_work);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300356 int ret;
357
358 mutex_lock(&wl->mutex);
359
Kalle Valo80301cd2009-06-12 14:17:39 +0300360 if (wl->state == WL1251_STATE_OFF)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300361 goto out;
362
Kalle Valo80301cd2009-06-12 14:17:39 +0300363 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300364 if (ret < 0)
365 goto out;
366
Kalle Valoae46ae12009-08-07 13:34:42 +0300367 ret = wl1251_join(wl, wl->bss_type, wl->channel, wl->beacon_int,
368 wl->dtim_period);
Kalle Valoc5483b72009-06-12 14:16:32 +0300369 if (ret < 0)
370 goto out_sleep;
371
372out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300373 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300374
375out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300376 mutex_unlock(&wl->mutex);
377}
378
Kalle Valo80301cd2009-06-12 14:17:39 +0300379static int wl1251_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300380{
Kalle Valo80301cd2009-06-12 14:17:39 +0300381 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300382
383 skb_queue_tail(&wl->tx_queue, skb);
384
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +0300385 /*
386 * The chip specific setup must run before the first TX packet -
387 * before that, the tx_work will not be initialized!
388 */
389
Kalle Valo16e711f2009-08-07 13:35:04 +0300390 ieee80211_queue_work(wl->hw, &wl->tx_work);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300391
392 /*
393 * The workqueue is slow to process the tx_queue and we need stop
394 * the queue here, otherwise the queue will get too long.
395 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300396 if (skb_queue_len(&wl->tx_queue) >= WL1251_TX_QUEUE_MAX_LENGTH) {
Kalle Valod67e2612009-11-30 10:17:45 +0200397 wl1251_debug(DEBUG_TX, "op_tx: tx_queue full, stop queues");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300398 ieee80211_stop_queues(wl->hw);
399
400 /*
401 * FIXME: this is racy, the variable is not properly
402 * protected. Maybe fix this by removing the stupid
403 * variable altogether and checking the real queue state?
404 */
405 wl->tx_queue_stopped = true;
406 }
407
408 return NETDEV_TX_OK;
409}
410
Kalle Valo80301cd2009-06-12 14:17:39 +0300411static int wl1251_op_start(struct ieee80211_hw *hw)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300412{
Kalle Valo80301cd2009-06-12 14:17:39 +0300413 struct wl1251 *wl = hw->priv;
John W. Linville8b28e822010-07-28 16:59:41 -0400414 struct wiphy *wiphy = hw->wiphy;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300415 int ret = 0;
416
Kalle Valo80301cd2009-06-12 14:17:39 +0300417 wl1251_debug(DEBUG_MAC80211, "mac80211 start");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300418
419 mutex_lock(&wl->mutex);
420
Kalle Valo80301cd2009-06-12 14:17:39 +0300421 if (wl->state != WL1251_STATE_OFF) {
422 wl1251_error("cannot start because not in off state: %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300423 wl->state);
424 ret = -EBUSY;
425 goto out;
426 }
427
Kalle Valo80301cd2009-06-12 14:17:39 +0300428 ret = wl1251_chip_wakeup(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300429 if (ret < 0)
Jiri Slabyfe643412009-07-14 22:37:13 +0200430 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300431
Kalle Valo0e71bb02009-08-07 13:33:57 +0300432 ret = wl1251_boot(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300433 if (ret < 0)
434 goto out;
435
Kalle Valo0e71bb02009-08-07 13:33:57 +0300436 ret = wl1251_hw_init(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300437 if (ret < 0)
438 goto out;
439
Kalle Valo80301cd2009-06-12 14:17:39 +0300440 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300441 if (ret < 0)
442 goto out;
443
Kalle Valo80301cd2009-06-12 14:17:39 +0300444 wl->state = WL1251_STATE_ON;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300445
Kalle Valo0e71bb02009-08-07 13:33:57 +0300446 wl1251_info("firmware booted (%s)", wl->fw_ver);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300447
John W. Linville8b28e822010-07-28 16:59:41 -0400448 /* update hw/fw version info in wiphy struct */
449 wiphy->hw_version = wl->chip_id;
450 strncpy(wiphy->fw_version, wl->fw_ver, sizeof(wiphy->fw_version));
451
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300452out:
453 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300454 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300455
456 mutex_unlock(&wl->mutex);
457
458 return ret;
459}
460
Kalle Valo80301cd2009-06-12 14:17:39 +0300461static void wl1251_op_stop(struct ieee80211_hw *hw)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300462{
Kalle Valo80301cd2009-06-12 14:17:39 +0300463 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300464
Kalle Valo80301cd2009-06-12 14:17:39 +0300465 wl1251_info("down");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300466
Kalle Valo80301cd2009-06-12 14:17:39 +0300467 wl1251_debug(DEBUG_MAC80211, "mac80211 stop");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300468
469 mutex_lock(&wl->mutex);
470
Kalle Valo80301cd2009-06-12 14:17:39 +0300471 WARN_ON(wl->state != WL1251_STATE_ON);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300472
473 if (wl->scanning) {
474 mutex_unlock(&wl->mutex);
475 ieee80211_scan_completed(wl->hw, true);
476 mutex_lock(&wl->mutex);
477 wl->scanning = false;
478 }
479
Kalle Valo80301cd2009-06-12 14:17:39 +0300480 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300481
Kalle Valo80301cd2009-06-12 14:17:39 +0300482 wl1251_disable_interrupts(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300483
484 mutex_unlock(&wl->mutex);
485
486 cancel_work_sync(&wl->irq_work);
487 cancel_work_sync(&wl->tx_work);
488 cancel_work_sync(&wl->filter_work);
489
490 mutex_lock(&wl->mutex);
491
492 /* let's notify MAC80211 about the remaining pending TX frames */
Kalle Valo0e71bb02009-08-07 13:33:57 +0300493 wl1251_tx_flush(wl);
Kalle Valo80301cd2009-06-12 14:17:39 +0300494 wl1251_power_off(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300495
496 memset(wl->bssid, 0, ETH_ALEN);
497 wl->listen_int = 1;
498 wl->bss_type = MAX_BSS_TYPE;
499
500 wl->data_in_count = 0;
501 wl->rx_counter = 0;
502 wl->rx_handled = 0;
503 wl->rx_current_buffer = 0;
504 wl->rx_last_id = 0;
505 wl->next_tx_complete = 0;
506 wl->elp = false;
507 wl->psm = 0;
508 wl->tx_queue_stopped = false;
Kalle Valo80301cd2009-06-12 14:17:39 +0300509 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
Kalle Valo97802792009-08-07 13:34:27 +0300510 wl->channel = WL1251_DEFAULT_CHANNEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300511
Kalle Valo80301cd2009-06-12 14:17:39 +0300512 wl1251_debugfs_reset(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300513
514 mutex_unlock(&wl->mutex);
515}
516
Kalle Valo80301cd2009-06-12 14:17:39 +0300517static int wl1251_op_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100518 struct ieee80211_vif *vif)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300519{
Kalle Valo80301cd2009-06-12 14:17:39 +0300520 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300521 int ret = 0;
522
Johannes Berge91d8332009-07-15 17:21:41 +0200523 wl1251_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
Johannes Berg1ed32e42009-12-23 13:15:45 +0100524 vif->type, vif->addr);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300525
526 mutex_lock(&wl->mutex);
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200527 if (wl->vif) {
528 ret = -EBUSY;
529 goto out;
530 }
531
Johannes Berg1ed32e42009-12-23 13:15:45 +0100532 wl->vif = vif;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300533
Johannes Berg1ed32e42009-12-23 13:15:45 +0100534 switch (vif->type) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300535 case NL80211_IFTYPE_STATION:
536 wl->bss_type = BSS_TYPE_STA_BSS;
537 break;
538 case NL80211_IFTYPE_ADHOC:
539 wl->bss_type = BSS_TYPE_IBSS;
540 break;
541 default:
542 ret = -EOPNOTSUPP;
543 goto out;
544 }
545
Johannes Berg1ed32e42009-12-23 13:15:45 +0100546 if (memcmp(wl->mac_addr, vif->addr, ETH_ALEN)) {
547 memcpy(wl->mac_addr, vif->addr, ETH_ALEN);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300548 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
Kalle Valo80301cd2009-06-12 14:17:39 +0300549 ret = wl1251_acx_station_id(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300550 if (ret < 0)
551 goto out;
552 }
553
554out:
555 mutex_unlock(&wl->mutex);
556 return ret;
557}
558
Kalle Valo80301cd2009-06-12 14:17:39 +0300559static void wl1251_op_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100560 struct ieee80211_vif *vif)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300561{
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200562 struct wl1251 *wl = hw->priv;
563
564 mutex_lock(&wl->mutex);
Kalle Valo80301cd2009-06-12 14:17:39 +0300565 wl1251_debug(DEBUG_MAC80211, "mac80211 remove interface");
Juuso Oikarinen287f6f92009-11-17 18:48:23 +0200566 wl->vif = NULL;
567 mutex_unlock(&wl->mutex);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300568}
569
Kalle Valo8f8ff9162010-01-12 10:43:07 +0200570static int wl1251_build_qos_null_data(struct wl1251 *wl)
571{
572 struct ieee80211_qos_hdr template;
573
574 memset(&template, 0, sizeof(template));
575
576 memcpy(template.addr1, wl->bssid, ETH_ALEN);
577 memcpy(template.addr2, wl->mac_addr, ETH_ALEN);
578 memcpy(template.addr3, wl->bssid, ETH_ALEN);
579
580 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
581 IEEE80211_STYPE_QOS_NULLFUNC |
582 IEEE80211_FCTL_TODS);
583
584 /* FIXME: not sure what priority to use here */
585 template.qos_ctrl = cpu_to_le16(0);
586
587 return wl1251_cmd_template_set(wl, CMD_QOS_NULL_DATA, &template,
588 sizeof(template));
589}
590
Kalle Valo80301cd2009-06-12 14:17:39 +0300591static int wl1251_op_config(struct ieee80211_hw *hw, u32 changed)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300592{
Kalle Valo80301cd2009-06-12 14:17:39 +0300593 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300594 struct ieee80211_conf *conf = &hw->conf;
595 int channel, ret = 0;
596
597 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
598
Kalle Valo80301cd2009-06-12 14:17:39 +0300599 wl1251_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300600 channel,
601 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
602 conf->power_level);
603
604 mutex_lock(&wl->mutex);
605
Kalle Valo80301cd2009-06-12 14:17:39 +0300606 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300607 if (ret < 0)
608 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300609
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300610 if (channel != wl->channel) {
Kalle Valofe9a9842009-08-07 13:34:49 +0300611 wl->channel = channel;
612
Kalle Valoae46ae12009-08-07 13:34:42 +0300613 ret = wl1251_join(wl, wl->bss_type, wl->channel,
614 wl->beacon_int, wl->dtim_period);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300615 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +0300616 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300617 }
618
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300619 if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
Luciano Coelho47af3fe2009-06-12 14:17:53 +0300620 wl1251_debug(DEBUG_PSM, "psm enabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300621
622 wl->psm_requested = true;
623
Johannes Berg56007a02010-01-26 14:19:52 +0100624 wl->dtim_period = conf->ps_dtim_period;
625
626 ret = wl1251_acx_wr_tbtt_and_dtim(wl, wl->beacon_int,
627 wl->dtim_period);
628
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300629 /*
Johannes Berg56007a02010-01-26 14:19:52 +0100630 * mac80211 enables PSM only if we're already associated.
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300631 */
Kalle Valo80301cd2009-06-12 14:17:39 +0300632 ret = wl1251_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
Kalle Valo478fdf22009-11-30 10:17:52 +0200633 if (ret < 0)
634 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300635 } else if (!(conf->flags & IEEE80211_CONF_PS) &&
636 wl->psm_requested) {
Luciano Coelho47af3fe2009-06-12 14:17:53 +0300637 wl1251_debug(DEBUG_PSM, "psm disabled");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300638
639 wl->psm_requested = false;
640
Kalle Valo478fdf22009-11-30 10:17:52 +0200641 if (wl->psm) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300642 ret = wl1251_ps_set_mode(wl, STATION_ACTIVE_MODE);
Kalle Valo478fdf22009-11-30 10:17:52 +0200643 if (ret < 0)
644 goto out_sleep;
645 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300646 }
647
648 if (conf->power_level != wl->power_level) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300649 ret = wl1251_acx_tx_power(wl, conf->power_level);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300650 if (ret < 0)
Kalle Valo478fdf22009-11-30 10:17:52 +0200651 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300652
653 wl->power_level = conf->power_level;
654 }
655
Kalle Valoc5483b72009-06-12 14:16:32 +0300656out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300657 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300658
659out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300660 mutex_unlock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +0300661
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300662 return ret;
663}
664
Kalle Valo80301cd2009-06-12 14:17:39 +0300665#define WL1251_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300666 FIF_ALLMULTI | \
667 FIF_FCSFAIL | \
668 FIF_BCN_PRBRESP_PROMISC | \
669 FIF_CONTROL | \
670 FIF_OTHER_BSS)
671
Kalle Valo80301cd2009-06-12 14:17:39 +0300672static void wl1251_op_configure_filter(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300673 unsigned int changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +0200674 unsigned int *total,u64 multicast)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300675{
Kalle Valo80301cd2009-06-12 14:17:39 +0300676 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300677
Kalle Valo80301cd2009-06-12 14:17:39 +0300678 wl1251_debug(DEBUG_MAC80211, "mac80211 configure filter");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300679
Kalle Valo80301cd2009-06-12 14:17:39 +0300680 *total &= WL1251_SUPPORTED_FILTERS;
681 changed &= WL1251_SUPPORTED_FILTERS;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300682
683 if (changed == 0)
684 /* no filters which we support changed */
685 return;
686
687 /* FIXME: wl->rx_config and wl->rx_filter are not protected */
688
Kalle Valo80301cd2009-06-12 14:17:39 +0300689 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
690 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300691
692 if (*total & FIF_PROMISC_IN_BSS) {
693 wl->rx_config |= CFG_BSSID_FILTER_EN;
694 wl->rx_config |= CFG_RX_ALL_GOOD;
695 }
696 if (*total & FIF_ALLMULTI)
697 /*
698 * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
699 * all multicast frames
700 */
701 wl->rx_config &= ~CFG_MC_FILTER_EN;
702 if (*total & FIF_FCSFAIL)
703 wl->rx_filter |= CFG_RX_FCS_ERROR;
704 if (*total & FIF_BCN_PRBRESP_PROMISC) {
705 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
706 wl->rx_config &= ~CFG_SSID_FILTER_EN;
707 }
708 if (*total & FIF_CONTROL)
709 wl->rx_filter |= CFG_RX_CTL_EN;
710 if (*total & FIF_OTHER_BSS)
711 wl->rx_filter &= ~CFG_BSSID_FILTER_EN;
712
713 /*
714 * FIXME: workqueues need to be properly cancelled on stop(), for
715 * now let's just disable changing the filter settings. They will
716 * be updated any on config().
717 */
718 /* schedule_work(&wl->filter_work); */
719}
720
721/* HW encryption */
Kalle Valo80301cd2009-06-12 14:17:39 +0300722static int wl1251_set_key_type(struct wl1251 *wl,
723 struct wl1251_cmd_set_keys *key,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300724 enum set_key_cmd cmd,
725 struct ieee80211_key_conf *mac80211_key,
726 const u8 *addr)
727{
728 switch (mac80211_key->alg) {
729 case ALG_WEP:
730 if (is_broadcast_ether_addr(addr))
731 key->key_type = KEY_WEP_DEFAULT;
732 else
733 key->key_type = KEY_WEP_ADDR;
734
735 mac80211_key->hw_key_idx = mac80211_key->keyidx;
736 break;
737 case ALG_TKIP:
738 if (is_broadcast_ether_addr(addr))
739 key->key_type = KEY_TKIP_MIC_GROUP;
740 else
741 key->key_type = KEY_TKIP_MIC_PAIRWISE;
742
743 mac80211_key->hw_key_idx = mac80211_key->keyidx;
744 break;
745 case ALG_CCMP:
746 if (is_broadcast_ether_addr(addr))
747 key->key_type = KEY_AES_GROUP;
748 else
749 key->key_type = KEY_AES_PAIRWISE;
750 mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
751 break;
752 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300753 wl1251_error("Unknown key algo 0x%x", mac80211_key->alg);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300754 return -EOPNOTSUPP;
755 }
756
757 return 0;
758}
759
Kalle Valo80301cd2009-06-12 14:17:39 +0300760static int wl1251_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300761 struct ieee80211_vif *vif,
762 struct ieee80211_sta *sta,
763 struct ieee80211_key_conf *key)
764{
Kalle Valo80301cd2009-06-12 14:17:39 +0300765 struct wl1251 *wl = hw->priv;
766 struct wl1251_cmd_set_keys *wl_cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300767 const u8 *addr;
768 int ret;
769
770 static const u8 bcast_addr[ETH_ALEN] =
771 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
772
Kalle Valo80301cd2009-06-12 14:17:39 +0300773 wl1251_debug(DEBUG_MAC80211, "mac80211 set key");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300774
Kalle Valoff258392009-06-12 14:14:19 +0300775 wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
776 if (!wl_cmd) {
777 ret = -ENOMEM;
778 goto out;
779 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300780
781 addr = sta ? sta->addr : bcast_addr;
782
Kalle Valo80301cd2009-06-12 14:17:39 +0300783 wl1251_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
784 wl1251_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
785 wl1251_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300786 key->alg, key->keyidx, key->keylen, key->flags);
Kalle Valo80301cd2009-06-12 14:17:39 +0300787 wl1251_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300788
Kalle Valoff258392009-06-12 14:14:19 +0300789 if (is_zero_ether_addr(addr)) {
790 /* We dont support TX only encryption */
791 ret = -EOPNOTSUPP;
792 goto out;
793 }
794
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300795 mutex_lock(&wl->mutex);
796
Kalle Valo80301cd2009-06-12 14:17:39 +0300797 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300798 if (ret < 0)
799 goto out_unlock;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300800
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300801 switch (cmd) {
802 case SET_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300803 wl_cmd->key_action = KEY_ADD_OR_REPLACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300804 break;
805 case DISABLE_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300806 wl_cmd->key_action = KEY_REMOVE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300807 break;
808 default:
Kalle Valo80301cd2009-06-12 14:17:39 +0300809 wl1251_error("Unsupported key cmd 0x%x", cmd);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300810 break;
811 }
812
Kalle Valo80301cd2009-06-12 14:17:39 +0300813 ret = wl1251_set_key_type(wl, wl_cmd, cmd, key, addr);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300814 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300815 wl1251_error("Set KEY type failed");
Kalle Valoc5483b72009-06-12 14:16:32 +0300816 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300817 }
818
Kalle Valoff258392009-06-12 14:14:19 +0300819 if (wl_cmd->key_type != KEY_WEP_DEFAULT)
820 memcpy(wl_cmd->addr, addr, ETH_ALEN);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300821
Kalle Valoff258392009-06-12 14:14:19 +0300822 if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
823 (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300824 /*
825 * We get the key in the following form:
826 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
827 * but the target is expecting:
828 * TKIP - RX MIC - TX MIC
829 */
Kalle Valoff258392009-06-12 14:14:19 +0300830 memcpy(wl_cmd->key, key->key, 16);
831 memcpy(wl_cmd->key + 16, key->key + 24, 8);
832 memcpy(wl_cmd->key + 24, key->key + 16, 8);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300833
834 } else {
Kalle Valoff258392009-06-12 14:14:19 +0300835 memcpy(wl_cmd->key, key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300836 }
Kalle Valoff258392009-06-12 14:14:19 +0300837 wl_cmd->key_size = key->keylen;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300838
Kalle Valoff258392009-06-12 14:14:19 +0300839 wl_cmd->id = key->keyidx;
840 wl_cmd->ssid_profile = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300841
Kalle Valo80301cd2009-06-12 14:17:39 +0300842 wl1251_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300843
Kalle Valo80301cd2009-06-12 14:17:39 +0300844 ret = wl1251_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
Kalle Valoff258392009-06-12 14:14:19 +0300845 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +0300846 wl1251_warning("could not set keys");
Kalle Valoc5483b72009-06-12 14:16:32 +0300847 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300848 }
849
Kalle Valoc5483b72009-06-12 14:16:32 +0300850out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300851 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300852
853out_unlock:
Kalle Valoff258392009-06-12 14:14:19 +0300854 mutex_unlock(&wl->mutex);
855
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300856out:
Kalle Valoff258392009-06-12 14:14:19 +0300857 kfree(wl_cmd);
858
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300859 return ret;
860}
861
Kalle Valo80301cd2009-06-12 14:17:39 +0300862static int wl1251_op_hw_scan(struct ieee80211_hw *hw,
Johannes Berga060bbf2010-04-27 11:59:34 +0200863 struct ieee80211_vif *vif,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300864 struct cfg80211_scan_request *req)
865{
Kalle Valo80301cd2009-06-12 14:17:39 +0300866 struct wl1251 *wl = hw->priv;
Kalle Valoe477c562010-01-05 20:16:57 +0200867 struct sk_buff *skb;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300868 size_t ssid_len = 0;
Kalle Valo3a98c302010-01-05 20:16:51 +0200869 u8 *ssid = NULL;
870 int ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300871
Kalle Valo80301cd2009-06-12 14:17:39 +0300872 wl1251_debug(DEBUG_MAC80211, "mac80211 hw scan");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300873
874 if (req->n_ssids) {
875 ssid = req->ssids[0].ssid;
876 ssid_len = req->ssids[0].ssid_len;
877 }
878
879 mutex_lock(&wl->mutex);
Kalle Valoc5483b72009-06-12 14:16:32 +0300880
Kalle Valo3a98c302010-01-05 20:16:51 +0200881 if (wl->scanning) {
882 wl1251_debug(DEBUG_SCAN, "scan already in progress");
883 ret = -EINVAL;
884 goto out;
885 }
886
Kalle Valo80301cd2009-06-12 14:17:39 +0300887 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300888 if (ret < 0)
889 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300890
Kalle Valoe477c562010-01-05 20:16:57 +0200891 skb = ieee80211_probereq_get(wl->hw, wl->vif, ssid, ssid_len,
892 req->ie, req->ie_len);
893 if (!skb) {
894 ret = -ENOMEM;
895 goto out;
896 }
897
898 ret = wl1251_cmd_template_set(wl, CMD_PROBE_REQ, skb->data,
899 skb->len);
900 dev_kfree_skb(skb);
Kalle Valo3a98c302010-01-05 20:16:51 +0200901 if (ret < 0)
Kalle Valoe477c562010-01-05 20:16:57 +0200902 goto out_sleep;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300903
Kalle Valo3a98c302010-01-05 20:16:51 +0200904 ret = wl1251_cmd_trigger_scan_to(wl, 0);
905 if (ret < 0)
906 goto out_sleep;
907
908 wl->scanning = true;
909
Kalle Valodc52f0a2010-01-05 20:17:03 +0200910 ret = wl1251_cmd_scan(wl, ssid, ssid_len, req->channels,
911 req->n_channels, WL1251_SCAN_NUM_PROBES);
Kalle Valo3a98c302010-01-05 20:16:51 +0200912 if (ret < 0) {
913 wl->scanning = false;
914 goto out_sleep;
915 }
916
917out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +0300918 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300919
920out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300921 mutex_unlock(&wl->mutex);
922
923 return ret;
924}
925
Kalle Valo80301cd2009-06-12 14:17:39 +0300926static int wl1251_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300927{
Kalle Valo80301cd2009-06-12 14:17:39 +0300928 struct wl1251 *wl = hw->priv;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300929 int ret;
930
Kalle Valocee4fd22009-06-12 14:16:20 +0300931 mutex_lock(&wl->mutex);
932
Kalle Valo80301cd2009-06-12 14:17:39 +0300933 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300934 if (ret < 0)
935 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300936
Kalle Valo80301cd2009-06-12 14:17:39 +0300937 ret = wl1251_acx_rts_threshold(wl, (u16) value);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300938 if (ret < 0)
Kalle Valo80301cd2009-06-12 14:17:39 +0300939 wl1251_warning("wl1251_op_set_rts_threshold failed: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300940
Kalle Valo80301cd2009-06-12 14:17:39 +0300941 wl1251_ps_elp_sleep(wl);
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300942
Kalle Valoc5483b72009-06-12 14:16:32 +0300943out:
Kalle Valocee4fd22009-06-12 14:16:20 +0300944 mutex_unlock(&wl->mutex);
945
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300946 return ret;
947}
948
Kalle Valo80301cd2009-06-12 14:17:39 +0300949static void wl1251_op_bss_info_changed(struct ieee80211_hw *hw,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300950 struct ieee80211_vif *vif,
951 struct ieee80211_bss_conf *bss_conf,
952 u32 changed)
953{
Kalle Valo80301cd2009-06-12 14:17:39 +0300954 struct wl1251 *wl = hw->priv;
Kalle Valof7f70572010-01-05 20:16:32 +0200955 struct sk_buff *beacon, *skb;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300956 int ret;
957
Kalle Valo80301cd2009-06-12 14:17:39 +0300958 wl1251_debug(DEBUG_MAC80211, "mac80211 bss info changed");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300959
960 mutex_lock(&wl->mutex);
961
Kalle Valo80301cd2009-06-12 14:17:39 +0300962 ret = wl1251_ps_elp_wakeup(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +0300963 if (ret < 0)
964 goto out;
Kalle Valo01d9cfb2009-06-12 14:16:26 +0300965
Kalle Valode8df1e2009-11-26 10:56:06 +0200966 if (changed & BSS_CHANGED_BSSID) {
967 memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
968
Kalle Valof7f70572010-01-05 20:16:32 +0200969 skb = ieee80211_nullfunc_get(wl->hw, wl->vif);
970 if (!skb)
971 goto out_sleep;
972
973 ret = wl1251_cmd_template_set(wl, CMD_NULL_DATA,
974 skb->data, skb->len);
975 dev_kfree_skb(skb);
Kalle Valode8df1e2009-11-26 10:56:06 +0200976 if (ret < 0)
Kalle Valo80a112f2010-01-05 20:17:10 +0200977 goto out_sleep;
Kalle Valode8df1e2009-11-26 10:56:06 +0200978
Kalle Valo8f8ff9162010-01-12 10:43:07 +0200979 ret = wl1251_build_qos_null_data(wl);
980 if (ret < 0)
981 goto out;
982
Kalle Valode8df1e2009-11-26 10:56:06 +0200983 if (wl->bss_type != BSS_TYPE_IBSS) {
984 ret = wl1251_join(wl, wl->bss_type, wl->channel,
985 wl->beacon_int, wl->dtim_period);
986 if (ret < 0)
987 goto out_sleep;
988 }
989 }
990
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300991 if (changed & BSS_CHANGED_ASSOC) {
992 if (bss_conf->assoc) {
Kalle Valoe2fd4612009-08-07 13:34:12 +0300993 wl->beacon_int = bss_conf->beacon_int;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300994
Kalle Valof7f70572010-01-05 20:16:32 +0200995 skb = ieee80211_pspoll_get(wl->hw, wl->vif);
996 if (!skb)
997 goto out_sleep;
998
999 ret = wl1251_cmd_template_set(wl, CMD_PS_POLL,
1000 skb->data,
1001 skb->len);
1002 dev_kfree_skb(skb);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001003 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001004 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001005
Johannes Berg56007a02010-01-26 14:19:52 +01001006 ret = wl1251_acx_aid(wl, bss_conf->aid);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001007 if (ret < 0)
Kalle Valoc5483b72009-06-12 14:16:32 +03001008 goto out_sleep;
Kalle Valoe2fd4612009-08-07 13:34:12 +03001009 } else {
1010 /* use defaults when not associated */
1011 wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
1012 wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001013 }
1014 }
1015 if (changed & BSS_CHANGED_ERP_SLOT) {
1016 if (bss_conf->use_short_slot)
Kalle Valo80301cd2009-06-12 14:17:39 +03001017 ret = wl1251_acx_slot(wl, SLOT_TIME_SHORT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001018 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001019 ret = wl1251_acx_slot(wl, SLOT_TIME_LONG);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001020 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001021 wl1251_warning("Set slot time failed %d", ret);
Kalle Valoc5483b72009-06-12 14:16:32 +03001022 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001023 }
1024 }
1025
1026 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1027 if (bss_conf->use_short_preamble)
Kalle Valo80301cd2009-06-12 14:17:39 +03001028 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001029 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001030 wl1251_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001031 }
1032
1033 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1034 if (bss_conf->use_cts_prot)
Kalle Valo80301cd2009-06-12 14:17:39 +03001035 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_ENABLE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001036 else
Kalle Valo80301cd2009-06-12 14:17:39 +03001037 ret = wl1251_acx_cts_protect(wl, CTSPROTECT_DISABLE);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001038 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001039 wl1251_warning("Set ctsprotect failed %d", ret);
Kalle Valo80a112f2010-01-05 20:17:10 +02001040 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001041 }
1042 }
1043
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001044 if (changed & BSS_CHANGED_BEACON) {
1045 beacon = ieee80211_beacon_get(hw, vif);
Kalle Valo80301cd2009-06-12 14:17:39 +03001046 ret = wl1251_cmd_template_set(wl, CMD_BEACON, beacon->data,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001047 beacon->len);
1048
1049 if (ret < 0) {
1050 dev_kfree_skb(beacon);
Kalle Valo80a112f2010-01-05 20:17:10 +02001051 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001052 }
1053
Kalle Valo80301cd2009-06-12 14:17:39 +03001054 ret = wl1251_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001055 beacon->len);
1056
1057 dev_kfree_skb(beacon);
1058
1059 if (ret < 0)
Kalle Valo80a112f2010-01-05 20:17:10 +02001060 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001061
Kalle Valoae46ae12009-08-07 13:34:42 +03001062 ret = wl1251_join(wl, wl->bss_type, wl->beacon_int,
1063 wl->channel, wl->dtim_period);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001064
1065 if (ret < 0)
Kalle Valo80a112f2010-01-05 20:17:10 +02001066 goto out_sleep;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001067 }
1068
Kalle Valoc5483b72009-06-12 14:16:32 +03001069out_sleep:
Kalle Valo80301cd2009-06-12 14:17:39 +03001070 wl1251_ps_elp_sleep(wl);
Kalle Valoc5483b72009-06-12 14:16:32 +03001071
1072out:
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001073 mutex_unlock(&wl->mutex);
1074}
1075
1076
1077/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001078static struct ieee80211_rate wl1251_rates[] = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001079 { .bitrate = 10,
1080 .hw_value = 0x1,
1081 .hw_value_short = 0x1, },
1082 { .bitrate = 20,
1083 .hw_value = 0x2,
1084 .hw_value_short = 0x2,
1085 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1086 { .bitrate = 55,
1087 .hw_value = 0x4,
1088 .hw_value_short = 0x4,
1089 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1090 { .bitrate = 110,
1091 .hw_value = 0x20,
1092 .hw_value_short = 0x20,
1093 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1094 { .bitrate = 60,
1095 .hw_value = 0x8,
1096 .hw_value_short = 0x8, },
1097 { .bitrate = 90,
1098 .hw_value = 0x10,
1099 .hw_value_short = 0x10, },
1100 { .bitrate = 120,
1101 .hw_value = 0x40,
1102 .hw_value_short = 0x40, },
1103 { .bitrate = 180,
1104 .hw_value = 0x80,
1105 .hw_value_short = 0x80, },
1106 { .bitrate = 240,
1107 .hw_value = 0x200,
1108 .hw_value_short = 0x200, },
1109 { .bitrate = 360,
1110 .hw_value = 0x400,
1111 .hw_value_short = 0x400, },
1112 { .bitrate = 480,
1113 .hw_value = 0x800,
1114 .hw_value_short = 0x800, },
1115 { .bitrate = 540,
1116 .hw_value = 0x1000,
1117 .hw_value_short = 0x1000, },
1118};
1119
1120/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001121static struct ieee80211_channel wl1251_channels[] = {
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001122 { .hw_value = 1, .center_freq = 2412},
1123 { .hw_value = 2, .center_freq = 2417},
1124 { .hw_value = 3, .center_freq = 2422},
1125 { .hw_value = 4, .center_freq = 2427},
1126 { .hw_value = 5, .center_freq = 2432},
1127 { .hw_value = 6, .center_freq = 2437},
1128 { .hw_value = 7, .center_freq = 2442},
1129 { .hw_value = 8, .center_freq = 2447},
1130 { .hw_value = 9, .center_freq = 2452},
1131 { .hw_value = 10, .center_freq = 2457},
1132 { .hw_value = 11, .center_freq = 2462},
1133 { .hw_value = 12, .center_freq = 2467},
1134 { .hw_value = 13, .center_freq = 2472},
1135};
1136
Kalle Valo86dff7a2009-11-30 10:18:19 +02001137static int wl1251_op_conf_tx(struct ieee80211_hw *hw, u16 queue,
1138 const struct ieee80211_tx_queue_params *params)
1139{
Kalle Valo4ff6ffa2010-01-12 10:43:15 +02001140 enum wl1251_acx_ps_scheme ps_scheme;
Kalle Valo86dff7a2009-11-30 10:18:19 +02001141 struct wl1251 *wl = hw->priv;
1142 int ret;
1143
1144 mutex_lock(&wl->mutex);
1145
1146 wl1251_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
1147
1148 ret = wl1251_ps_elp_wakeup(wl);
1149 if (ret < 0)
1150 goto out;
1151
Kalle Valo85359492010-02-04 15:33:25 +02001152 /* mac80211 uses units of 32 usec */
Kalle Valo86dff7a2009-11-30 10:18:19 +02001153 ret = wl1251_acx_ac_cfg(wl, wl1251_tx_get_queue(queue),
1154 params->cw_min, params->cw_max,
Kalle Valo85359492010-02-04 15:33:25 +02001155 params->aifs, params->txop * 32);
Kalle Valo27336f12009-11-30 10:18:27 +02001156 if (ret < 0)
1157 goto out_sleep;
Kalle Valo86dff7a2009-11-30 10:18:19 +02001158
Kalle Valo4ff6ffa2010-01-12 10:43:15 +02001159 if (params->uapsd)
1160 ps_scheme = WL1251_ACX_PS_SCHEME_UPSD_TRIGGER;
1161 else
1162 ps_scheme = WL1251_ACX_PS_SCHEME_LEGACY;
1163
Kalle Valo27336f12009-11-30 10:18:27 +02001164 ret = wl1251_acx_tid_cfg(wl, wl1251_tx_get_queue(queue),
Kalle Valo49e1b9f2009-11-30 10:18:33 +02001165 CHANNEL_TYPE_EDCF,
Kalle Valo4ff6ffa2010-01-12 10:43:15 +02001166 wl1251_tx_get_queue(queue), ps_scheme,
Kalle Valo27336f12009-11-30 10:18:27 +02001167 WL1251_ACX_ACK_POLICY_LEGACY);
1168 if (ret < 0)
1169 goto out_sleep;
1170
1171out_sleep:
Kalle Valo86dff7a2009-11-30 10:18:19 +02001172 wl1251_ps_elp_sleep(wl);
1173
1174out:
1175 mutex_unlock(&wl->mutex);
1176
1177 return ret;
1178}
1179
John W. Linville19434142010-07-28 15:23:30 -04001180static int wl1251_op_get_survey(struct ieee80211_hw *hw, int idx,
1181 struct survey_info *survey)
1182{
1183 struct wl1251 *wl = hw->priv;
1184 struct ieee80211_conf *conf = &hw->conf;
1185
1186 if (idx != 0)
1187 return -ENOENT;
1188
1189 survey->channel = conf->channel;
1190 survey->filled = SURVEY_INFO_NOISE_DBM;
1191 survey->noise = wl->noise;
1192
1193 return 0;
1194}
1195
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001196/* can't be const, mac80211 writes to this */
Kalle Valo80301cd2009-06-12 14:17:39 +03001197static struct ieee80211_supported_band wl1251_band_2ghz = {
1198 .channels = wl1251_channels,
1199 .n_channels = ARRAY_SIZE(wl1251_channels),
1200 .bitrates = wl1251_rates,
1201 .n_bitrates = ARRAY_SIZE(wl1251_rates),
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001202};
1203
Kalle Valo80301cd2009-06-12 14:17:39 +03001204static const struct ieee80211_ops wl1251_ops = {
1205 .start = wl1251_op_start,
1206 .stop = wl1251_op_stop,
1207 .add_interface = wl1251_op_add_interface,
1208 .remove_interface = wl1251_op_remove_interface,
1209 .config = wl1251_op_config,
1210 .configure_filter = wl1251_op_configure_filter,
1211 .tx = wl1251_op_tx,
1212 .set_key = wl1251_op_set_key,
1213 .hw_scan = wl1251_op_hw_scan,
1214 .bss_info_changed = wl1251_op_bss_info_changed,
1215 .set_rts_threshold = wl1251_op_set_rts_threshold,
Kalle Valo86dff7a2009-11-30 10:18:19 +02001216 .conf_tx = wl1251_op_conf_tx,
John W. Linville19434142010-07-28 15:23:30 -04001217 .get_survey = wl1251_op_get_survey,
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001218};
1219
Grazvydas Ignotas61c2a802010-04-14 13:05:48 +03001220static int wl1251_read_eeprom_byte(struct wl1251 *wl, off_t offset, u8 *data)
1221{
1222 unsigned long timeout;
1223
1224 wl1251_reg_write32(wl, EE_ADDR, offset);
1225 wl1251_reg_write32(wl, EE_CTL, EE_CTL_READ);
1226
1227 /* EE_CTL_READ clears when data is ready */
1228 timeout = jiffies + msecs_to_jiffies(100);
1229 while (1) {
1230 if (!(wl1251_reg_read32(wl, EE_CTL) & EE_CTL_READ))
1231 break;
1232
1233 if (time_after(jiffies, timeout))
1234 return -ETIMEDOUT;
1235
1236 msleep(1);
1237 }
1238
1239 *data = wl1251_reg_read32(wl, EE_DATA);
1240 return 0;
1241}
1242
1243static int wl1251_read_eeprom(struct wl1251 *wl, off_t offset,
1244 u8 *data, size_t len)
1245{
1246 size_t i;
1247 int ret;
1248
1249 wl1251_reg_write32(wl, EE_START, 0);
1250
1251 for (i = 0; i < len; i++) {
1252 ret = wl1251_read_eeprom_byte(wl, offset + i, &data[i]);
1253 if (ret < 0)
1254 return ret;
1255 }
1256
1257 return 0;
1258}
1259
1260static int wl1251_read_eeprom_mac(struct wl1251 *wl)
1261{
1262 u8 mac[ETH_ALEN];
1263 int i, ret;
1264
1265 wl1251_set_partition(wl, 0, 0, REGISTERS_BASE, REGISTERS_DOWN_SIZE);
1266
1267 ret = wl1251_read_eeprom(wl, 0x1c, mac, sizeof(mac));
1268 if (ret < 0) {
1269 wl1251_warning("failed to read MAC address from EEPROM");
1270 return ret;
1271 }
1272
1273 /* MAC is stored in reverse order */
1274 for (i = 0; i < ETH_ALEN; i++)
1275 wl->mac_addr[i] = mac[ETH_ALEN - i - 1];
1276
1277 return 0;
1278}
1279
Kalle Valo80301cd2009-06-12 14:17:39 +03001280static int wl1251_register_hw(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001281{
1282 int ret;
1283
1284 if (wl->mac80211_registered)
1285 return 0;
1286
1287 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1288
1289 ret = ieee80211_register_hw(wl->hw);
1290 if (ret < 0) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001291 wl1251_error("unable to register mac80211 hw: %d", ret);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001292 return ret;
1293 }
1294
1295 wl->mac80211_registered = true;
1296
Kalle Valo80301cd2009-06-12 14:17:39 +03001297 wl1251_notice("loaded");
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001298
1299 return 0;
1300}
1301
Bob Copeland8e639c02009-08-07 13:33:26 +03001302int wl1251_init_ieee80211(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001303{
Bob Copeland8e639c02009-08-07 13:33:26 +03001304 int ret;
1305
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001306 /* The tx descriptor buffer and the TKIP space */
1307 wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +03001308 + WL1251_TKIP_IV_SPACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001309
1310 /* unit us */
1311 /* FIXME: find a proper value */
1312 wl->hw->channel_change_time = 10000;
1313
1314 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
Juuso Oikarinen6b21a2c2009-11-17 18:48:30 +02001315 IEEE80211_HW_SUPPORTS_PS |
Kalle Valo4ff6ffa2010-01-12 10:43:15 +02001316 IEEE80211_HW_BEACON_FILTER |
1317 IEEE80211_HW_SUPPORTS_UAPSD;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001318
1319 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1320 wl->hw->wiphy->max_scan_ssids = 1;
Kalle Valo80301cd2009-06-12 14:17:39 +03001321 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl1251_band_2ghz;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001322
Kalle Valoda2fb4e2009-11-30 10:18:47 +02001323 wl->hw->queues = 4;
1324
Grazvydas Ignotas61c2a802010-04-14 13:05:48 +03001325 if (wl->use_eeprom)
1326 wl1251_read_eeprom_mac(wl);
1327
Bob Copeland8e639c02009-08-07 13:33:26 +03001328 ret = wl1251_register_hw(wl);
1329 if (ret)
1330 goto out;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001331
Bob Copeland8e639c02009-08-07 13:33:26 +03001332 wl1251_debugfs_init(wl);
1333 wl1251_notice("initialized");
1334
1335 ret = 0;
1336
1337out:
1338 return ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001339}
Bob Copelandaf8c78e2009-08-07 13:33:34 +03001340EXPORT_SYMBOL_GPL(wl1251_init_ieee80211);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001341
Bob Copeland8e639c02009-08-07 13:33:26 +03001342struct ieee80211_hw *wl1251_alloc_hw(void)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001343{
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001344 struct ieee80211_hw *hw;
Kalle Valo80301cd2009-06-12 14:17:39 +03001345 struct wl1251 *wl;
Bob Copeland8e639c02009-08-07 13:33:26 +03001346 int i;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001347 static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1348
Kalle Valo80301cd2009-06-12 14:17:39 +03001349 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1251_ops);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001350 if (!hw) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001351 wl1251_error("could not alloc ieee80211_hw");
Bob Copeland8e639c02009-08-07 13:33:26 +03001352 return ERR_PTR(-ENOMEM);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001353 }
1354
1355 wl = hw->priv;
1356 memset(wl, 0, sizeof(*wl));
1357
1358 wl->hw = hw;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001359
1360 wl->data_in_count = 0;
1361
1362 skb_queue_head_init(&wl->tx_queue);
1363
Kalle Valo80301cd2009-06-12 14:17:39 +03001364 INIT_WORK(&wl->filter_work, wl1251_filter_work);
Juuso Oikarinend5da79a2009-11-17 18:48:37 +02001365 INIT_DELAYED_WORK(&wl->elp_work, wl1251_elp_work);
Kalle Valo80301cd2009-06-12 14:17:39 +03001366 wl->channel = WL1251_DEFAULT_CHANNEL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001367 wl->scanning = false;
1368 wl->default_key = 0;
1369 wl->listen_int = 1;
1370 wl->rx_counter = 0;
1371 wl->rx_handled = 0;
1372 wl->rx_current_buffer = 0;
1373 wl->rx_last_id = 0;
Kalle Valo80301cd2009-06-12 14:17:39 +03001374 wl->rx_config = WL1251_DEFAULT_RX_CONFIG;
1375 wl->rx_filter = WL1251_DEFAULT_RX_FILTER;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001376 wl->elp = false;
1377 wl->psm = 0;
1378 wl->psm_requested = false;
1379 wl->tx_queue_stopped = false;
Kalle Valo80301cd2009-06-12 14:17:39 +03001380 wl->power_level = WL1251_DEFAULT_POWER_LEVEL;
Kalle Valoe2fd4612009-08-07 13:34:12 +03001381 wl->beacon_int = WL1251_DEFAULT_BEACON_INT;
1382 wl->dtim_period = WL1251_DEFAULT_DTIM_PERIOD;
Juuso Oikarinen287f6f92009-11-17 18:48:23 +02001383 wl->vif = NULL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001384
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001385 for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
1386 wl->tx_frames[i] = NULL;
1387
1388 wl->next_tx_complete = 0;
1389
Kalle Valo0e71bb02009-08-07 13:33:57 +03001390 INIT_WORK(&wl->irq_work, wl1251_irq_work);
1391 INIT_WORK(&wl->tx_work, wl1251_tx_work);
1392
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001393 /*
1394 * In case our MAC address is not correctly set,
1395 * we use a random but Nokia MAC.
1396 */
1397 memcpy(wl->mac_addr, nokia_oui, 3);
1398 get_random_bytes(wl->mac_addr + 3, 3);
1399
Kalle Valo80301cd2009-06-12 14:17:39 +03001400 wl->state = WL1251_STATE_OFF;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001401 mutex_init(&wl->mutex);
1402
1403 wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
1404 wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
1405
Kalle Valo47212132009-06-12 14:15:08 +03001406 wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1407 if (!wl->rx_descriptor) {
Kalle Valo80301cd2009-06-12 14:17:39 +03001408 wl1251_error("could not allocate memory for rx descriptor");
Bob Copeland8e639c02009-08-07 13:33:26 +03001409 ieee80211_free_hw(hw);
1410 return ERR_PTR(-ENOMEM);
Kalle Valo47212132009-06-12 14:15:08 +03001411 }
1412
Bob Copeland8e639c02009-08-07 13:33:26 +03001413 return hw;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001414}
Bob Copelandaf8c78e2009-08-07 13:33:34 +03001415EXPORT_SYMBOL_GPL(wl1251_alloc_hw);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001416
Bob Copeland8e639c02009-08-07 13:33:26 +03001417int wl1251_free_hw(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001418{
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001419 ieee80211_unregister_hw(wl->hw);
1420
Kalle Valo80301cd2009-06-12 14:17:39 +03001421 wl1251_debugfs_exit(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001422
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001423 kfree(wl->target_mem_map);
1424 kfree(wl->data_path);
Kalle Valoc74ddfd2009-11-17 18:48:45 +02001425 vfree(wl->fw);
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001426 wl->fw = NULL;
1427 kfree(wl->nvs);
1428 wl->nvs = NULL;
Kalle Valo47212132009-06-12 14:15:08 +03001429
1430 kfree(wl->rx_descriptor);
1431 wl->rx_descriptor = NULL;
1432
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001433 ieee80211_free_hw(wl->hw);
1434
1435 return 0;
1436}
Bob Copelandaf8c78e2009-08-07 13:33:34 +03001437EXPORT_SYMBOL_GPL(wl1251_free_hw);
1438
1439MODULE_DESCRIPTION("TI wl1251 Wireles LAN Driver Core");
1440MODULE_LICENSE("GPL");
Kalle Valob1b0a2b2009-08-07 13:35:19 +03001441MODULE_AUTHOR("Kalle Valo <kalle.valo@nokia.com>");
Ben Hutchings49f146d2009-11-07 22:02:15 +00001442MODULE_FIRMWARE(WL1251_FW_NAME);