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