blob: 8feba36ff48d57c4e2606073d08952d964892fd7 [file] [log] [blame]
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001/*
2 * This file is part of wl12xx
3 *
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>
29#include <linux/spi/spi.h>
30#include <linux/crc32.h>
31#include <linux/etherdevice.h>
32#include <linux/spi/wl12xx.h>
33
34#include "wl12xx.h"
35#include "wl12xx_80211.h"
36#include "reg.h"
37#include "wl1251.h"
38#include "spi.h"
39#include "event.h"
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +030040#include "wl1251_tx.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030041#include "rx.h"
42#include "ps.h"
43#include "init.h"
44#include "debugfs.h"
45
46static void wl12xx_disable_interrupts(struct wl12xx *wl)
47{
48 disable_irq(wl->irq);
49}
50
51static void wl12xx_power_off(struct wl12xx *wl)
52{
53 wl->set_power(false);
54}
55
56static void wl12xx_power_on(struct wl12xx *wl)
57{
58 wl->set_power(true);
59}
60
61static irqreturn_t wl12xx_irq(int irq, void *cookie)
62{
63 struct wl12xx *wl;
64
65 wl12xx_debug(DEBUG_IRQ, "IRQ");
66
67 wl = cookie;
68
69 schedule_work(&wl->irq_work);
70
71 return IRQ_HANDLED;
72}
73
74static int wl12xx_fetch_firmware(struct wl12xx *wl)
75{
76 const struct firmware *fw;
77 int ret;
78
79 ret = request_firmware(&fw, wl->chip.fw_filename, &wl->spi->dev);
80
81 if (ret < 0) {
82 wl12xx_error("could not get firmware: %d", ret);
83 return ret;
84 }
85
86 if (fw->size % 4) {
Bob Copeland8bce6122009-05-01 08:20:07 -040087 wl12xx_error("firmware size is not multiple of 32 bits: %zu",
Kalle Valo2f01a1f2009-04-29 23:33:31 +030088 fw->size);
89 ret = -EILSEQ;
90 goto out;
91 }
92
93 wl->fw_len = fw->size;
94 wl->fw = kmalloc(wl->fw_len, GFP_KERNEL);
95
96 if (!wl->fw) {
97 wl12xx_error("could not allocate memory for the firmware");
98 ret = -ENOMEM;
99 goto out;
100 }
101
102 memcpy(wl->fw, fw->data, wl->fw_len);
103
104 ret = 0;
105
106out:
107 release_firmware(fw);
108
109 return ret;
110}
111
112static int wl12xx_fetch_nvs(struct wl12xx *wl)
113{
114 const struct firmware *fw;
115 int ret;
116
117 ret = request_firmware(&fw, wl->chip.nvs_filename, &wl->spi->dev);
118
119 if (ret < 0) {
120 wl12xx_error("could not get nvs file: %d", ret);
121 return ret;
122 }
123
124 if (fw->size % 4) {
Bob Copeland8bce6122009-05-01 08:20:07 -0400125 wl12xx_error("nvs size is not multiple of 32 bits: %zu",
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300126 fw->size);
127 ret = -EILSEQ;
128 goto out;
129 }
130
131 wl->nvs_len = fw->size;
132 wl->nvs = kmalloc(wl->nvs_len, GFP_KERNEL);
133
134 if (!wl->nvs) {
135 wl12xx_error("could not allocate memory for the nvs file");
136 ret = -ENOMEM;
137 goto out;
138 }
139
140 memcpy(wl->nvs, fw->data, wl->nvs_len);
141
142 ret = 0;
143
144out:
145 release_firmware(fw);
146
147 return ret;
148}
149
150static void wl12xx_fw_wakeup(struct wl12xx *wl)
151{
152 u32 elp_reg;
153
154 elp_reg = ELPCTRL_WAKE_UP;
155 wl12xx_write32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR, elp_reg);
156 elp_reg = wl12xx_read32(wl, HW_ACCESS_ELP_CTRL_REG_ADDR);
157
158 if (!(elp_reg & ELPCTRL_WLAN_READY)) {
159 wl12xx_warning("WLAN not ready");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300160 }
161}
162
163static int wl12xx_chip_wakeup(struct wl12xx *wl)
164{
165 int ret = 0;
166
167 wl12xx_power_on(wl);
168 msleep(wl->chip.power_on_sleep);
169 wl12xx_spi_reset(wl);
170 wl12xx_spi_init(wl);
171
172 /* We don't need a real memory partition here, because we only want
173 * to use the registers at this point. */
174 wl12xx_set_partition(wl,
175 0x00000000,
176 0x00000000,
177 REGISTERS_BASE,
178 REGISTERS_DOWN_SIZE);
179
180 /* ELP module wake up */
181 wl12xx_fw_wakeup(wl);
182
183 /* whal_FwCtrl_BootSm() */
184
185 /* 0. read chip id from CHIP_ID */
186 wl->chip.id = wl12xx_reg_read32(wl, CHIP_ID_B);
187
188 /* 1. check if chip id is valid */
189
190 switch (wl->chip.id) {
191 case CHIP_ID_1251_PG12:
192 wl12xx_debug(DEBUG_BOOT, "chip id 0x%x (1251 PG12)",
193 wl->chip.id);
194
195 wl1251_setup(wl);
196
197 break;
198 case CHIP_ID_1271_PG10:
Luciano Coelho27797d62009-06-12 14:15:33 +0300199 wl12xx_warning("chip id 0x%x (1271 PG10) support is obsolete",
200 wl->chip.id);
201 break;
202 case CHIP_ID_1271_PG20:
203 wl12xx_debug(DEBUG_BOOT, "chip id 0x%x (1271 PG20)",
204 wl->chip.id);
205 break;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300206 case CHIP_ID_1251_PG10:
207 case CHIP_ID_1251_PG11:
208 default:
209 wl12xx_error("unsupported chip id: 0x%x", wl->chip.id);
210 ret = -ENODEV;
211 goto out;
212 }
213
214 if (wl->fw == NULL) {
215 ret = wl12xx_fetch_firmware(wl);
216 if (ret < 0)
217 goto out;
218 }
219
220 /* No NVS from netlink, try to get it from the filesystem */
221 if (wl->nvs == NULL) {
222 ret = wl12xx_fetch_nvs(wl);
223 if (ret < 0)
224 goto out;
225 }
226
227out:
228 return ret;
229}
230
231static void wl12xx_filter_work(struct work_struct *work)
232{
233 struct wl12xx *wl =
234 container_of(work, struct wl12xx, filter_work);
235 int ret;
236
237 mutex_lock(&wl->mutex);
238
239 if (wl->state == WL12XX_STATE_OFF)
240 goto out;
241
242 ret = wl12xx_cmd_join(wl, wl->bss_type, 1, 100, 0);
243 if (ret < 0)
244 goto out;
245
246out:
247 mutex_unlock(&wl->mutex);
248}
249
250int wl12xx_plt_start(struct wl12xx *wl)
251{
252 int ret;
253
254 wl12xx_notice("power up");
255
256 if (wl->state != WL12XX_STATE_OFF) {
257 wl12xx_error("cannot go into PLT state because not "
258 "in off state: %d", wl->state);
259 return -EBUSY;
260 }
261
262 wl->state = WL12XX_STATE_PLT;
263
264 ret = wl12xx_chip_wakeup(wl);
265 if (ret < 0)
266 return ret;
267
268 ret = wl->chip.op_boot(wl);
269 if (ret < 0)
270 return ret;
271
272 wl12xx_notice("firmware booted in PLT mode (%s)", wl->chip.fw_ver);
273
274 ret = wl->chip.op_plt_init(wl);
275 if (ret < 0)
276 return ret;
277
278 return 0;
279}
280
281int wl12xx_plt_stop(struct wl12xx *wl)
282{
283 wl12xx_notice("power down");
284
285 if (wl->state != WL12XX_STATE_PLT) {
286 wl12xx_error("cannot power down because not in PLT "
287 "state: %d", wl->state);
288 return -EBUSY;
289 }
290
291 wl12xx_disable_interrupts(wl);
292 wl12xx_power_off(wl);
293
294 wl->state = WL12XX_STATE_OFF;
295
296 return 0;
297}
298
299
300static int wl12xx_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
301{
302 struct wl12xx *wl = hw->priv;
303
304 skb_queue_tail(&wl->tx_queue, skb);
305
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +0300306 /*
307 * The chip specific setup must run before the first TX packet -
308 * before that, the tx_work will not be initialized!
309 */
310
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300311 schedule_work(&wl->tx_work);
312
313 /*
314 * The workqueue is slow to process the tx_queue and we need stop
315 * the queue here, otherwise the queue will get too long.
316 */
317 if (skb_queue_len(&wl->tx_queue) >= WL12XX_TX_QUEUE_MAX_LENGTH) {
318 ieee80211_stop_queues(wl->hw);
319
320 /*
321 * FIXME: this is racy, the variable is not properly
322 * protected. Maybe fix this by removing the stupid
323 * variable altogether and checking the real queue state?
324 */
325 wl->tx_queue_stopped = true;
326 }
327
328 return NETDEV_TX_OK;
329}
330
331static int wl12xx_op_start(struct ieee80211_hw *hw)
332{
333 struct wl12xx *wl = hw->priv;
334 int ret = 0;
335
336 wl12xx_debug(DEBUG_MAC80211, "mac80211 start");
337
338 mutex_lock(&wl->mutex);
339
340 if (wl->state != WL12XX_STATE_OFF) {
341 wl12xx_error("cannot start because not in off state: %d",
342 wl->state);
343 ret = -EBUSY;
344 goto out;
345 }
346
347 ret = wl12xx_chip_wakeup(wl);
348 if (ret < 0)
349 return ret;
350
351 ret = wl->chip.op_boot(wl);
352 if (ret < 0)
353 goto out;
354
355 ret = wl->chip.op_hw_init(wl);
356 if (ret < 0)
357 goto out;
358
359 ret = wl12xx_acx_station_id(wl);
360 if (ret < 0)
361 goto out;
362
363 wl->state = WL12XX_STATE_ON;
364
365 wl12xx_info("firmware booted (%s)", wl->chip.fw_ver);
366
367out:
368 if (ret < 0)
369 wl12xx_power_off(wl);
370
371 mutex_unlock(&wl->mutex);
372
373 return ret;
374}
375
376static void wl12xx_op_stop(struct ieee80211_hw *hw)
377{
378 struct wl12xx *wl = hw->priv;
379
380 wl12xx_info("down");
381
382 wl12xx_debug(DEBUG_MAC80211, "mac80211 stop");
383
384 mutex_lock(&wl->mutex);
385
386 WARN_ON(wl->state != WL12XX_STATE_ON);
387
388 if (wl->scanning) {
389 mutex_unlock(&wl->mutex);
390 ieee80211_scan_completed(wl->hw, true);
391 mutex_lock(&wl->mutex);
392 wl->scanning = false;
393 }
394
395 wl->state = WL12XX_STATE_OFF;
396
397 wl12xx_disable_interrupts(wl);
398
399 mutex_unlock(&wl->mutex);
400
401 cancel_work_sync(&wl->irq_work);
402 cancel_work_sync(&wl->tx_work);
403 cancel_work_sync(&wl->filter_work);
404
405 mutex_lock(&wl->mutex);
406
407 /* let's notify MAC80211 about the remaining pending TX frames */
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +0300408 wl->chip.op_tx_flush(wl);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300409 wl12xx_power_off(wl);
410
411 memset(wl->bssid, 0, ETH_ALEN);
412 wl->listen_int = 1;
413 wl->bss_type = MAX_BSS_TYPE;
414
415 wl->data_in_count = 0;
416 wl->rx_counter = 0;
417 wl->rx_handled = 0;
418 wl->rx_current_buffer = 0;
419 wl->rx_last_id = 0;
420 wl->next_tx_complete = 0;
421 wl->elp = false;
422 wl->psm = 0;
423 wl->tx_queue_stopped = false;
424 wl->power_level = WL12XX_DEFAULT_POWER_LEVEL;
425
426 wl12xx_debugfs_reset(wl);
427
428 mutex_unlock(&wl->mutex);
429}
430
431static int wl12xx_op_add_interface(struct ieee80211_hw *hw,
432 struct ieee80211_if_init_conf *conf)
433{
434 struct wl12xx *wl = hw->priv;
435 DECLARE_MAC_BUF(mac);
436 int ret = 0;
437
438 wl12xx_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %s",
439 conf->type, print_mac(mac, conf->mac_addr));
440
441 mutex_lock(&wl->mutex);
442
443 switch (conf->type) {
444 case NL80211_IFTYPE_STATION:
445 wl->bss_type = BSS_TYPE_STA_BSS;
446 break;
447 case NL80211_IFTYPE_ADHOC:
448 wl->bss_type = BSS_TYPE_IBSS;
449 break;
450 default:
451 ret = -EOPNOTSUPP;
452 goto out;
453 }
454
455 if (memcmp(wl->mac_addr, conf->mac_addr, ETH_ALEN)) {
456 memcpy(wl->mac_addr, conf->mac_addr, ETH_ALEN);
457 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
458 ret = wl12xx_acx_station_id(wl);
459 if (ret < 0)
460 goto out;
461 }
462
463out:
464 mutex_unlock(&wl->mutex);
465 return ret;
466}
467
468static void wl12xx_op_remove_interface(struct ieee80211_hw *hw,
469 struct ieee80211_if_init_conf *conf)
470{
471 wl12xx_debug(DEBUG_MAC80211, "mac80211 remove interface");
472}
473
474static int wl12xx_build_null_data(struct wl12xx *wl)
475{
476 struct wl12xx_null_data_template template;
477
478 if (!is_zero_ether_addr(wl->bssid)) {
479 memcpy(template.header.da, wl->bssid, ETH_ALEN);
480 memcpy(template.header.bssid, wl->bssid, ETH_ALEN);
481 } else {
482 memset(template.header.da, 0xff, ETH_ALEN);
483 memset(template.header.bssid, 0xff, ETH_ALEN);
484 }
485
486 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
487 template.header.frame_ctl = cpu_to_le16(IEEE80211_FTYPE_DATA |
488 IEEE80211_STYPE_NULLFUNC);
489
490 return wl12xx_cmd_template_set(wl, CMD_NULL_DATA, &template,
491 sizeof(template));
492
493}
494
495static int wl12xx_build_ps_poll(struct wl12xx *wl, u16 aid)
496{
497 struct wl12xx_ps_poll_template template;
498
499 memcpy(template.bssid, wl->bssid, ETH_ALEN);
500 memcpy(template.ta, wl->mac_addr, ETH_ALEN);
501 template.aid = aid;
502 template.fc = cpu_to_le16(IEEE80211_FTYPE_CTL | IEEE80211_STYPE_PSPOLL);
503
504 return wl12xx_cmd_template_set(wl, CMD_PS_POLL, &template,
505 sizeof(template));
506
507}
508
509static int wl12xx_op_config(struct ieee80211_hw *hw, u32 changed)
510{
511 struct wl12xx *wl = hw->priv;
512 struct ieee80211_conf *conf = &hw->conf;
513 int channel, ret = 0;
514
515 channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
516
517 wl12xx_debug(DEBUG_MAC80211, "mac80211 config ch %d psm %s power %d",
518 channel,
519 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
520 conf->power_level);
521
522 mutex_lock(&wl->mutex);
523
524 if (channel != wl->channel) {
525 /* FIXME: use beacon interval provided by mac80211 */
526 ret = wl12xx_cmd_join(wl, wl->bss_type, 1, 100, 0);
527 if (ret < 0)
528 goto out;
529
530 wl->channel = channel;
531 }
532
533 ret = wl12xx_build_null_data(wl);
534 if (ret < 0)
535 goto out;
536
537 if (conf->flags & IEEE80211_CONF_PS && !wl->psm_requested) {
538 wl12xx_info("psm enabled");
539
540 wl->psm_requested = true;
541
542 /*
543 * We enter PSM only if we're already associated.
544 * If we're not, we'll enter it when joining an SSID,
545 * through the bss_info_changed() hook.
546 */
547 ret = wl12xx_ps_set_mode(wl, STATION_POWER_SAVE_MODE);
548 } else if (!(conf->flags & IEEE80211_CONF_PS) &&
549 wl->psm_requested) {
550 wl12xx_info("psm disabled");
551
552 wl->psm_requested = false;
553
554 if (wl->psm)
555 ret = wl12xx_ps_set_mode(wl, STATION_ACTIVE_MODE);
556 }
557
558 if (conf->power_level != wl->power_level) {
559 ret = wl12xx_acx_tx_power(wl, conf->power_level);
560 if (ret < 0)
561 goto out;
562
563 wl->power_level = conf->power_level;
564 }
565
566out:
567 mutex_unlock(&wl->mutex);
568 return ret;
569}
570
571#define WL12XX_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
572 FIF_ALLMULTI | \
573 FIF_FCSFAIL | \
574 FIF_BCN_PRBRESP_PROMISC | \
575 FIF_CONTROL | \
576 FIF_OTHER_BSS)
577
578static void wl12xx_op_configure_filter(struct ieee80211_hw *hw,
579 unsigned int changed,
580 unsigned int *total,
581 int mc_count,
582 struct dev_addr_list *mc_list)
583{
584 struct wl12xx *wl = hw->priv;
585
586 wl12xx_debug(DEBUG_MAC80211, "mac80211 configure filter");
587
588 *total &= WL12XX_SUPPORTED_FILTERS;
589 changed &= WL12XX_SUPPORTED_FILTERS;
590
591 if (changed == 0)
592 /* no filters which we support changed */
593 return;
594
595 /* FIXME: wl->rx_config and wl->rx_filter are not protected */
596
597 wl->rx_config = WL12XX_DEFAULT_RX_CONFIG;
598 wl->rx_filter = WL12XX_DEFAULT_RX_FILTER;
599
600 if (*total & FIF_PROMISC_IN_BSS) {
601 wl->rx_config |= CFG_BSSID_FILTER_EN;
602 wl->rx_config |= CFG_RX_ALL_GOOD;
603 }
604 if (*total & FIF_ALLMULTI)
605 /*
606 * CFG_MC_FILTER_EN in rx_config needs to be 0 to receive
607 * all multicast frames
608 */
609 wl->rx_config &= ~CFG_MC_FILTER_EN;
610 if (*total & FIF_FCSFAIL)
611 wl->rx_filter |= CFG_RX_FCS_ERROR;
612 if (*total & FIF_BCN_PRBRESP_PROMISC) {
613 wl->rx_config &= ~CFG_BSSID_FILTER_EN;
614 wl->rx_config &= ~CFG_SSID_FILTER_EN;
615 }
616 if (*total & FIF_CONTROL)
617 wl->rx_filter |= CFG_RX_CTL_EN;
618 if (*total & FIF_OTHER_BSS)
619 wl->rx_filter &= ~CFG_BSSID_FILTER_EN;
620
621 /*
622 * FIXME: workqueues need to be properly cancelled on stop(), for
623 * now let's just disable changing the filter settings. They will
624 * be updated any on config().
625 */
626 /* schedule_work(&wl->filter_work); */
627}
628
629/* HW encryption */
Kalle Valoff258392009-06-12 14:14:19 +0300630static int wl12xx_set_key_type(struct wl12xx *wl,
631 struct wl12xx_cmd_set_keys *key,
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300632 enum set_key_cmd cmd,
633 struct ieee80211_key_conf *mac80211_key,
634 const u8 *addr)
635{
636 switch (mac80211_key->alg) {
637 case ALG_WEP:
638 if (is_broadcast_ether_addr(addr))
639 key->key_type = KEY_WEP_DEFAULT;
640 else
641 key->key_type = KEY_WEP_ADDR;
642
643 mac80211_key->hw_key_idx = mac80211_key->keyidx;
644 break;
645 case ALG_TKIP:
646 if (is_broadcast_ether_addr(addr))
647 key->key_type = KEY_TKIP_MIC_GROUP;
648 else
649 key->key_type = KEY_TKIP_MIC_PAIRWISE;
650
651 mac80211_key->hw_key_idx = mac80211_key->keyidx;
652 break;
653 case ALG_CCMP:
654 if (is_broadcast_ether_addr(addr))
655 key->key_type = KEY_AES_GROUP;
656 else
657 key->key_type = KEY_AES_PAIRWISE;
658 mac80211_key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
659 break;
660 default:
661 wl12xx_error("Unknown key algo 0x%x", mac80211_key->alg);
662 return -EOPNOTSUPP;
663 }
664
665 return 0;
666}
667
668static int wl12xx_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
669 struct ieee80211_vif *vif,
670 struct ieee80211_sta *sta,
671 struct ieee80211_key_conf *key)
672{
673 struct wl12xx *wl = hw->priv;
Kalle Valoff258392009-06-12 14:14:19 +0300674 struct wl12xx_cmd_set_keys *wl_cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300675 const u8 *addr;
676 int ret;
677
678 static const u8 bcast_addr[ETH_ALEN] =
679 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
680
681 wl12xx_debug(DEBUG_MAC80211, "mac80211 set key");
682
Kalle Valoff258392009-06-12 14:14:19 +0300683 wl_cmd = kzalloc(sizeof(*wl_cmd), GFP_KERNEL);
684 if (!wl_cmd) {
685 ret = -ENOMEM;
686 goto out;
687 }
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300688
689 addr = sta ? sta->addr : bcast_addr;
690
691 wl12xx_debug(DEBUG_CRYPT, "CMD: 0x%x", cmd);
692 wl12xx_dump(DEBUG_CRYPT, "ADDR: ", addr, ETH_ALEN);
693 wl12xx_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
694 key->alg, key->keyidx, key->keylen, key->flags);
695 wl12xx_dump(DEBUG_CRYPT, "KEY: ", key->key, key->keylen);
696
Kalle Valoff258392009-06-12 14:14:19 +0300697 if (is_zero_ether_addr(addr)) {
698 /* We dont support TX only encryption */
699 ret = -EOPNOTSUPP;
700 goto out;
701 }
702
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300703 mutex_lock(&wl->mutex);
704
705 switch (cmd) {
706 case SET_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300707 wl_cmd->key_action = KEY_ADD_OR_REPLACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300708 break;
709 case DISABLE_KEY:
Kalle Valoff258392009-06-12 14:14:19 +0300710 wl_cmd->key_action = KEY_REMOVE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300711 break;
712 default:
713 wl12xx_error("Unsupported key cmd 0x%x", cmd);
714 break;
715 }
716
Kalle Valoff258392009-06-12 14:14:19 +0300717 ret = wl12xx_set_key_type(wl, wl_cmd, cmd, key, addr);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300718 if (ret < 0) {
719 wl12xx_error("Set KEY type failed");
Kalle Valoff258392009-06-12 14:14:19 +0300720 goto out_unlock;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300721 }
722
Kalle Valoff258392009-06-12 14:14:19 +0300723 if (wl_cmd->key_type != KEY_WEP_DEFAULT)
724 memcpy(wl_cmd->addr, addr, ETH_ALEN);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300725
Kalle Valoff258392009-06-12 14:14:19 +0300726 if ((wl_cmd->key_type == KEY_TKIP_MIC_GROUP) ||
727 (wl_cmd->key_type == KEY_TKIP_MIC_PAIRWISE)) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300728 /*
729 * We get the key in the following form:
730 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
731 * but the target is expecting:
732 * TKIP - RX MIC - TX MIC
733 */
Kalle Valoff258392009-06-12 14:14:19 +0300734 memcpy(wl_cmd->key, key->key, 16);
735 memcpy(wl_cmd->key + 16, key->key + 24, 8);
736 memcpy(wl_cmd->key + 24, key->key + 16, 8);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300737
738 } else {
Kalle Valoff258392009-06-12 14:14:19 +0300739 memcpy(wl_cmd->key, key->key, key->keylen);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300740 }
Kalle Valoff258392009-06-12 14:14:19 +0300741 wl_cmd->key_size = key->keylen;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300742
Kalle Valoff258392009-06-12 14:14:19 +0300743 wl_cmd->id = key->keyidx;
744 wl_cmd->ssid_profile = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300745
Kalle Valoff258392009-06-12 14:14:19 +0300746 wl12xx_dump(DEBUG_CRYPT, "TARGET KEY: ", wl_cmd, sizeof(*wl_cmd));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300747
Kalle Valoff258392009-06-12 14:14:19 +0300748 ret = wl12xx_cmd_send(wl, CMD_SET_KEYS, wl_cmd, sizeof(*wl_cmd));
749 if (ret < 0) {
750 wl12xx_warning("could not set keys");
751 goto out_unlock;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300752 }
753
Kalle Valoff258392009-06-12 14:14:19 +0300754out_unlock:
755 mutex_unlock(&wl->mutex);
756
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300757out:
Kalle Valoff258392009-06-12 14:14:19 +0300758 kfree(wl_cmd);
759
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300760 return ret;
761}
762
763static int wl12xx_build_basic_rates(char *rates)
764{
765 u8 index = 0;
766
767 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_1MB;
768 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_2MB;
769 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_5MB;
770 rates[index++] = IEEE80211_BASIC_RATE_MASK | IEEE80211_CCK_RATE_11MB;
771
772 return index;
773}
774
775static int wl12xx_build_extended_rates(char *rates)
776{
777 u8 index = 0;
778
779 rates[index++] = IEEE80211_OFDM_RATE_6MB;
780 rates[index++] = IEEE80211_OFDM_RATE_9MB;
781 rates[index++] = IEEE80211_OFDM_RATE_12MB;
782 rates[index++] = IEEE80211_OFDM_RATE_18MB;
783 rates[index++] = IEEE80211_OFDM_RATE_24MB;
784 rates[index++] = IEEE80211_OFDM_RATE_36MB;
785 rates[index++] = IEEE80211_OFDM_RATE_48MB;
786 rates[index++] = IEEE80211_OFDM_RATE_54MB;
787
788 return index;
789}
790
791
792static int wl12xx_build_probe_req(struct wl12xx *wl, u8 *ssid, size_t ssid_len)
793{
794 struct wl12xx_probe_req_template template;
795 struct wl12xx_ie_rates *rates;
796 char *ptr;
797 u16 size;
798
799 ptr = (char *)&template;
800 size = sizeof(struct ieee80211_header);
801
802 memset(template.header.da, 0xff, ETH_ALEN);
803 memset(template.header.bssid, 0xff, ETH_ALEN);
804 memcpy(template.header.sa, wl->mac_addr, ETH_ALEN);
805 template.header.frame_ctl = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
806
807 /* IEs */
808 /* SSID */
809 template.ssid.header.id = WLAN_EID_SSID;
810 template.ssid.header.len = ssid_len;
811 if (ssid_len && ssid)
812 memcpy(template.ssid.ssid, ssid, ssid_len);
813 size += sizeof(struct wl12xx_ie_header) + ssid_len;
814 ptr += size;
815
816 /* Basic Rates */
817 rates = (struct wl12xx_ie_rates *)ptr;
818 rates->header.id = WLAN_EID_SUPP_RATES;
819 rates->header.len = wl12xx_build_basic_rates(rates->rates);
820 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
821 ptr += sizeof(struct wl12xx_ie_header) + rates->header.len;
822
823 /* Extended rates */
824 rates = (struct wl12xx_ie_rates *)ptr;
825 rates->header.id = WLAN_EID_EXT_SUPP_RATES;
826 rates->header.len = wl12xx_build_extended_rates(rates->rates);
827 size += sizeof(struct wl12xx_ie_header) + rates->header.len;
828
829 wl12xx_dump(DEBUG_SCAN, "PROBE REQ: ", &template, size);
830
831 return wl12xx_cmd_template_set(wl, CMD_PROBE_REQ, &template,
832 size);
833}
834
835static int wl12xx_hw_scan(struct wl12xx *wl, u8 *ssid, size_t len,
836 u8 active_scan, u8 high_prio, u8 num_channels,
837 u8 probe_requests)
838{
Kalle Valoff258392009-06-12 14:14:19 +0300839 struct wl12xx_cmd_trigger_scan_to *trigger = NULL;
840 struct cmd_scan *params = NULL;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300841 int i, ret;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300842 u16 scan_options = 0;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300843
844 if (wl->scanning)
845 return -EINVAL;
846
847 params = kzalloc(sizeof(*params), GFP_KERNEL);
848 if (!params)
849 return -ENOMEM;
850
851 params->params.rx_config_options = cpu_to_le32(CFG_RX_ALL_GOOD);
852 params->params.rx_filter_options =
853 cpu_to_le32(CFG_RX_PRSP_EN | CFG_RX_MGMT_EN | CFG_RX_BCN_EN);
854
855 /* High priority scan */
856 if (!active_scan)
857 scan_options |= SCAN_PASSIVE;
858 if (high_prio)
859 scan_options |= SCAN_PRIORITY_HIGH;
860 params->params.scan_options = scan_options;
861
862 params->params.num_channels = num_channels;
863 params->params.num_probe_requests = probe_requests;
864 params->params.tx_rate = cpu_to_le16(1 << 1); /* 2 Mbps */
865 params->params.tid_trigger = 0;
866
867 for (i = 0; i < num_channels; i++) {
868 params->channels[i].min_duration = cpu_to_le32(30000);
869 params->channels[i].max_duration = cpu_to_le32(60000);
870 memset(&params->channels[i].bssid_lsb, 0xff, 4);
871 memset(&params->channels[i].bssid_msb, 0xff, 2);
872 params->channels[i].early_termination = 0;
873 params->channels[i].tx_power_att = 0;
874 params->channels[i].channel = i + 1;
875 memset(params->channels[i].pad, 0, 3);
876 }
877
878 for (i = num_channels; i < SCAN_MAX_NUM_OF_CHANNELS; i++)
879 memset(&params->channels[i], 0,
880 sizeof(struct basic_scan_channel_parameters));
881
882 if (len && ssid) {
883 params->params.ssid_len = len;
884 memcpy(params->params.ssid, ssid, len);
885 } else {
886 params->params.ssid_len = 0;
887 memset(params->params.ssid, 0, 32);
888 }
889
890 ret = wl12xx_build_probe_req(wl, ssid, len);
891 if (ret < 0) {
892 wl12xx_error("PROBE request template failed");
893 goto out;
894 }
895
Kalle Valoff258392009-06-12 14:14:19 +0300896 trigger = kzalloc(sizeof(*trigger), GFP_KERNEL);
897 if (!trigger)
898 goto out;
899
900 trigger->timeout = 0;
901
902 ret = wl12xx_cmd_send(wl, CMD_TRIGGER_SCAN_TO, trigger,
903 sizeof(*trigger));
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300904 if (ret < 0) {
Kalle Valoff258392009-06-12 14:14:19 +0300905 wl12xx_error("trigger scan to failed for hw scan");
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300906 goto out;
907 }
908
909 wl12xx_dump(DEBUG_SCAN, "SCAN: ", params, sizeof(*params));
910
911 wl->scanning = true;
912
913 ret = wl12xx_cmd_send(wl, CMD_SCAN, params, sizeof(*params));
914 if (ret < 0)
915 wl12xx_error("SCAN failed");
916
917 wl12xx_spi_mem_read(wl, wl->cmd_box_addr, params, sizeof(*params));
918
Kalle Valoff258392009-06-12 14:14:19 +0300919 if (params->header.status != CMD_STATUS_SUCCESS) {
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300920 wl12xx_error("TEST command answer error: %d",
Kalle Valoff258392009-06-12 14:14:19 +0300921 params->header.status);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300922 wl->scanning = false;
923 ret = -EIO;
924 goto out;
925 }
926
927out:
928 kfree(params);
929 return ret;
930
931}
932
933static int wl12xx_op_hw_scan(struct ieee80211_hw *hw,
934 struct cfg80211_scan_request *req)
935{
936 struct wl12xx *wl = hw->priv;
937 int ret;
938 u8 *ssid = NULL;
939 size_t ssid_len = 0;
940
941 wl12xx_debug(DEBUG_MAC80211, "mac80211 hw scan");
942
943 if (req->n_ssids) {
944 ssid = req->ssids[0].ssid;
945 ssid_len = req->ssids[0].ssid_len;
946 }
947
948 mutex_lock(&wl->mutex);
949 ret = wl12xx_hw_scan(hw->priv, ssid, ssid_len, 1, 0, 13, 3);
950 mutex_unlock(&wl->mutex);
951
952 return ret;
953}
954
955static int wl12xx_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
956{
957 struct wl12xx *wl = hw->priv;
958 int ret;
959
960 ret = wl12xx_acx_rts_threshold(wl, (u16) value);
961
962 if (ret < 0)
963 wl12xx_warning("wl12xx_op_set_rts_threshold failed: %d", ret);
964
965 return ret;
966}
967
968static void wl12xx_op_bss_info_changed(struct ieee80211_hw *hw,
969 struct ieee80211_vif *vif,
970 struct ieee80211_bss_conf *bss_conf,
971 u32 changed)
972{
Kalle Valoff258392009-06-12 14:14:19 +0300973 enum wl12xx_cmd_ps_mode mode;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300974 struct wl12xx *wl = hw->priv;
975 struct sk_buff *beacon;
976 int ret;
977
978 wl12xx_debug(DEBUG_MAC80211, "mac80211 bss info changed");
979
980 mutex_lock(&wl->mutex);
981
982 if (changed & BSS_CHANGED_ASSOC) {
983 if (bss_conf->assoc) {
984 wl->aid = bss_conf->aid;
985
986 ret = wl12xx_build_ps_poll(wl, wl->aid);
987 if (ret < 0)
988 goto out;
989
990 ret = wl12xx_acx_aid(wl, wl->aid);
991 if (ret < 0)
992 goto out;
993
994 /* If we want to go in PSM but we're not there yet */
995 if (wl->psm_requested && !wl->psm) {
996 mode = STATION_POWER_SAVE_MODE;
997 ret = wl12xx_ps_set_mode(wl, mode);
998 if (ret < 0)
999 goto out;
1000 }
1001 }
1002 }
1003 if (changed & BSS_CHANGED_ERP_SLOT) {
1004 if (bss_conf->use_short_slot)
1005 ret = wl12xx_acx_slot(wl, SLOT_TIME_SHORT);
1006 else
1007 ret = wl12xx_acx_slot(wl, SLOT_TIME_LONG);
1008 if (ret < 0) {
1009 wl12xx_warning("Set slot time failed %d", ret);
1010 goto out;
1011 }
1012 }
1013
1014 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
1015 if (bss_conf->use_short_preamble)
1016 wl12xx_acx_set_preamble(wl, ACX_PREAMBLE_SHORT);
1017 else
1018 wl12xx_acx_set_preamble(wl, ACX_PREAMBLE_LONG);
1019 }
1020
1021 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
1022 if (bss_conf->use_cts_prot)
1023 ret = wl12xx_acx_cts_protect(wl, CTSPROTECT_ENABLE);
1024 else
1025 ret = wl12xx_acx_cts_protect(wl, CTSPROTECT_DISABLE);
1026 if (ret < 0) {
1027 wl12xx_warning("Set ctsprotect failed %d", ret);
1028 goto out;
1029 }
1030 }
1031
1032 if (changed & BSS_CHANGED_BSSID) {
1033 memcpy(wl->bssid, bss_conf->bssid, ETH_ALEN);
1034
1035 ret = wl12xx_build_null_data(wl);
1036 if (ret < 0)
1037 goto out;
1038
1039 if (wl->bss_type != BSS_TYPE_IBSS) {
1040 ret = wl12xx_cmd_join(wl, wl->bss_type, 5, 100, 1);
1041 if (ret < 0)
1042 goto out;
1043 }
1044 }
1045
1046 if (changed & BSS_CHANGED_BEACON) {
1047 beacon = ieee80211_beacon_get(hw, vif);
1048 ret = wl12xx_cmd_template_set(wl, CMD_BEACON, beacon->data,
1049 beacon->len);
1050
1051 if (ret < 0) {
1052 dev_kfree_skb(beacon);
1053 goto out;
1054 }
1055
1056 ret = wl12xx_cmd_template_set(wl, CMD_PROBE_RESP, beacon->data,
1057 beacon->len);
1058
1059 dev_kfree_skb(beacon);
1060
1061 if (ret < 0)
1062 goto out;
1063
1064 ret = wl12xx_cmd_join(wl, wl->bss_type, 1, 100, 0);
1065
1066 if (ret < 0)
1067 goto out;
1068 }
1069
1070out:
1071 mutex_unlock(&wl->mutex);
1072}
1073
1074
1075/* can't be const, mac80211 writes to this */
1076static struct ieee80211_rate wl12xx_rates[] = {
1077 { .bitrate = 10,
1078 .hw_value = 0x1,
1079 .hw_value_short = 0x1, },
1080 { .bitrate = 20,
1081 .hw_value = 0x2,
1082 .hw_value_short = 0x2,
1083 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1084 { .bitrate = 55,
1085 .hw_value = 0x4,
1086 .hw_value_short = 0x4,
1087 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1088 { .bitrate = 110,
1089 .hw_value = 0x20,
1090 .hw_value_short = 0x20,
1091 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
1092 { .bitrate = 60,
1093 .hw_value = 0x8,
1094 .hw_value_short = 0x8, },
1095 { .bitrate = 90,
1096 .hw_value = 0x10,
1097 .hw_value_short = 0x10, },
1098 { .bitrate = 120,
1099 .hw_value = 0x40,
1100 .hw_value_short = 0x40, },
1101 { .bitrate = 180,
1102 .hw_value = 0x80,
1103 .hw_value_short = 0x80, },
1104 { .bitrate = 240,
1105 .hw_value = 0x200,
1106 .hw_value_short = 0x200, },
1107 { .bitrate = 360,
1108 .hw_value = 0x400,
1109 .hw_value_short = 0x400, },
1110 { .bitrate = 480,
1111 .hw_value = 0x800,
1112 .hw_value_short = 0x800, },
1113 { .bitrate = 540,
1114 .hw_value = 0x1000,
1115 .hw_value_short = 0x1000, },
1116};
1117
1118/* can't be const, mac80211 writes to this */
1119static struct ieee80211_channel wl12xx_channels[] = {
1120 { .hw_value = 1, .center_freq = 2412},
1121 { .hw_value = 2, .center_freq = 2417},
1122 { .hw_value = 3, .center_freq = 2422},
1123 { .hw_value = 4, .center_freq = 2427},
1124 { .hw_value = 5, .center_freq = 2432},
1125 { .hw_value = 6, .center_freq = 2437},
1126 { .hw_value = 7, .center_freq = 2442},
1127 { .hw_value = 8, .center_freq = 2447},
1128 { .hw_value = 9, .center_freq = 2452},
1129 { .hw_value = 10, .center_freq = 2457},
1130 { .hw_value = 11, .center_freq = 2462},
1131 { .hw_value = 12, .center_freq = 2467},
1132 { .hw_value = 13, .center_freq = 2472},
1133};
1134
1135/* can't be const, mac80211 writes to this */
1136static struct ieee80211_supported_band wl12xx_band_2ghz = {
1137 .channels = wl12xx_channels,
1138 .n_channels = ARRAY_SIZE(wl12xx_channels),
1139 .bitrates = wl12xx_rates,
1140 .n_bitrates = ARRAY_SIZE(wl12xx_rates),
1141};
1142
1143static const struct ieee80211_ops wl12xx_ops = {
1144 .start = wl12xx_op_start,
1145 .stop = wl12xx_op_stop,
1146 .add_interface = wl12xx_op_add_interface,
1147 .remove_interface = wl12xx_op_remove_interface,
1148 .config = wl12xx_op_config,
1149 .configure_filter = wl12xx_op_configure_filter,
1150 .tx = wl12xx_op_tx,
1151 .set_key = wl12xx_op_set_key,
1152 .hw_scan = wl12xx_op_hw_scan,
1153 .bss_info_changed = wl12xx_op_bss_info_changed,
1154 .set_rts_threshold = wl12xx_op_set_rts_threshold,
1155};
1156
1157static int wl12xx_register_hw(struct wl12xx *wl)
1158{
1159 int ret;
1160
1161 if (wl->mac80211_registered)
1162 return 0;
1163
1164 SET_IEEE80211_PERM_ADDR(wl->hw, wl->mac_addr);
1165
1166 ret = ieee80211_register_hw(wl->hw);
1167 if (ret < 0) {
1168 wl12xx_error("unable to register mac80211 hw: %d", ret);
1169 return ret;
1170 }
1171
1172 wl->mac80211_registered = true;
1173
1174 wl12xx_notice("loaded");
1175
1176 return 0;
1177}
1178
1179static int wl12xx_init_ieee80211(struct wl12xx *wl)
1180{
1181 /* The tx descriptor buffer and the TKIP space */
1182 wl->hw->extra_tx_headroom = sizeof(struct tx_double_buffer_desc)
Juuso Oikarinen9f2ad4f2009-06-12 14:15:54 +03001183 + WL1251_TKIP_IV_SPACE;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001184
1185 /* unit us */
1186 /* FIXME: find a proper value */
1187 wl->hw->channel_change_time = 10000;
1188
1189 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
1190 IEEE80211_HW_NOISE_DBM;
1191
1192 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1193 wl->hw->wiphy->max_scan_ssids = 1;
1194 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &wl12xx_band_2ghz;
1195
1196 SET_IEEE80211_DEV(wl->hw, &wl->spi->dev);
1197
1198 return 0;
1199}
1200
1201#define WL12XX_DEFAULT_CHANNEL 1
1202static int __devinit wl12xx_probe(struct spi_device *spi)
1203{
1204 struct wl12xx_platform_data *pdata;
1205 struct ieee80211_hw *hw;
1206 struct wl12xx *wl;
1207 int ret, i;
1208 static const u8 nokia_oui[3] = {0x00, 0x1f, 0xdf};
1209
1210 pdata = spi->dev.platform_data;
1211 if (!pdata) {
1212 wl12xx_error("no platform data");
1213 return -ENODEV;
1214 }
1215
1216 hw = ieee80211_alloc_hw(sizeof(*wl), &wl12xx_ops);
1217 if (!hw) {
1218 wl12xx_error("could not alloc ieee80211_hw");
1219 return -ENOMEM;
1220 }
1221
1222 wl = hw->priv;
1223 memset(wl, 0, sizeof(*wl));
1224
1225 wl->hw = hw;
1226 dev_set_drvdata(&spi->dev, wl);
1227 wl->spi = spi;
1228
1229 wl->data_in_count = 0;
1230
1231 skb_queue_head_init(&wl->tx_queue);
1232
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001233 INIT_WORK(&wl->filter_work, wl12xx_filter_work);
1234 wl->channel = WL12XX_DEFAULT_CHANNEL;
1235 wl->scanning = false;
1236 wl->default_key = 0;
1237 wl->listen_int = 1;
1238 wl->rx_counter = 0;
1239 wl->rx_handled = 0;
1240 wl->rx_current_buffer = 0;
1241 wl->rx_last_id = 0;
1242 wl->rx_config = WL12XX_DEFAULT_RX_CONFIG;
1243 wl->rx_filter = WL12XX_DEFAULT_RX_FILTER;
1244 wl->elp = false;
1245 wl->psm = 0;
1246 wl->psm_requested = false;
1247 wl->tx_queue_stopped = false;
1248 wl->power_level = WL12XX_DEFAULT_POWER_LEVEL;
1249
1250 /* We use the default power on sleep time until we know which chip
1251 * we're using */
1252 wl->chip.power_on_sleep = WL12XX_DEFAULT_POWER_ON_SLEEP;
1253
1254 for (i = 0; i < FW_TX_CMPLT_BLOCK_SIZE; i++)
1255 wl->tx_frames[i] = NULL;
1256
1257 wl->next_tx_complete = 0;
1258
1259 /*
1260 * In case our MAC address is not correctly set,
1261 * we use a random but Nokia MAC.
1262 */
1263 memcpy(wl->mac_addr, nokia_oui, 3);
1264 get_random_bytes(wl->mac_addr + 3, 3);
1265
1266 wl->state = WL12XX_STATE_OFF;
1267 mutex_init(&wl->mutex);
1268
1269 wl->tx_mgmt_frm_rate = DEFAULT_HW_GEN_TX_RATE;
1270 wl->tx_mgmt_frm_mod = DEFAULT_HW_GEN_MODULATION_TYPE;
1271
Kalle Valo47212132009-06-12 14:15:08 +03001272 wl->rx_descriptor = kmalloc(sizeof(*wl->rx_descriptor), GFP_KERNEL);
1273 if (!wl->rx_descriptor) {
1274 wl12xx_error("could not allocate memory for rx descriptor");
1275 ret = -ENOMEM;
1276 goto out_free;
1277 }
1278
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001279 /* This is the only SPI value that we need to set here, the rest
1280 * comes from the board-peripherals file */
1281 spi->bits_per_word = 32;
1282
1283 ret = spi_setup(spi);
1284 if (ret < 0) {
1285 wl12xx_error("spi_setup failed");
1286 goto out_free;
1287 }
1288
1289 wl->set_power = pdata->set_power;
1290 if (!wl->set_power) {
1291 wl12xx_error("set power function missing in platform data");
Kalle Valoc4f5c852009-06-12 14:14:34 +03001292 ret = -ENODEV;
1293 goto out_free;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001294 }
1295
1296 wl->irq = spi->irq;
1297 if (wl->irq < 0) {
1298 wl12xx_error("irq missing in platform data");
Kalle Valoc4f5c852009-06-12 14:14:34 +03001299 ret = -ENODEV;
1300 goto out_free;
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001301 }
1302
1303 ret = request_irq(wl->irq, wl12xx_irq, 0, DRIVER_NAME, wl);
1304 if (ret < 0) {
1305 wl12xx_error("request_irq() failed: %d", ret);
1306 goto out_free;
1307 }
1308
1309 set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
1310
1311 disable_irq(wl->irq);
1312
1313 ret = wl12xx_init_ieee80211(wl);
1314 if (ret)
1315 goto out_irq;
1316
1317 ret = wl12xx_register_hw(wl);
1318 if (ret)
1319 goto out_irq;
1320
1321 wl12xx_debugfs_init(wl);
1322
1323 wl12xx_notice("initialized");
1324
1325 return 0;
1326
1327 out_irq:
1328 free_irq(wl->irq, wl);
1329
1330 out_free:
Kalle Valo47212132009-06-12 14:15:08 +03001331 kfree(wl->rx_descriptor);
1332 wl->rx_descriptor = NULL;
1333
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001334 ieee80211_free_hw(hw);
1335
1336 return ret;
1337}
1338
1339static int __devexit wl12xx_remove(struct spi_device *spi)
1340{
1341 struct wl12xx *wl = dev_get_drvdata(&spi->dev);
1342
1343 ieee80211_unregister_hw(wl->hw);
1344
1345 wl12xx_debugfs_exit(wl);
1346
1347 free_irq(wl->irq, wl);
1348 kfree(wl->target_mem_map);
1349 kfree(wl->data_path);
1350 kfree(wl->fw);
1351 wl->fw = NULL;
1352 kfree(wl->nvs);
1353 wl->nvs = NULL;
Kalle Valo47212132009-06-12 14:15:08 +03001354
1355 kfree(wl->rx_descriptor);
1356 wl->rx_descriptor = NULL;
1357
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001358 ieee80211_free_hw(wl->hw);
1359
1360 return 0;
1361}
1362
1363
1364static struct spi_driver wl12xx_spi_driver = {
1365 .driver = {
1366 .name = "wl12xx",
1367 .bus = &spi_bus_type,
1368 .owner = THIS_MODULE,
1369 },
1370
1371 .probe = wl12xx_probe,
1372 .remove = __devexit_p(wl12xx_remove),
1373};
1374
1375static int __init wl12xx_init(void)
1376{
1377 int ret;
1378
1379 ret = spi_register_driver(&wl12xx_spi_driver);
1380 if (ret < 0) {
1381 wl12xx_error("failed to register spi driver: %d", ret);
1382 goto out;
1383 }
1384
1385out:
1386 return ret;
1387}
1388
1389static void __exit wl12xx_exit(void)
1390{
1391 spi_unregister_driver(&wl12xx_spi_driver);
1392
1393 wl12xx_notice("unloaded");
1394}
1395
1396module_init(wl12xx_init);
1397module_exit(wl12xx_exit);
1398
1399MODULE_LICENSE("GPL");
1400MODULE_AUTHOR("Kalle Valo <Kalle.Valo@nokia.com>, "
1401 "Luciano Coelho <luciano.coelho@nokia.com>");