2 * This file is part of wl1271
4 * Copyright (C) 2008-2009 Nokia Corporation
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
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.
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.
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
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/interrupt.h>
27 #include <linux/firmware.h>
28 #include <linux/delay.h>
29 #include <linux/irq.h>
30 #include <linux/spi/spi.h>
31 #include <linux/crc32.h>
32 #include <linux/etherdevice.h>
33 #include <linux/vmalloc.h>
34 #include <linux/spi/wl12xx.h>
37 #include "wl12xx_80211.h"
38 #include "wl1271_reg.h"
39 #include "wl1271_spi.h"
40 #include "wl1271_event.h"
41 #include "wl1271_tx.h"
42 #include "wl1271_rx.h"
43 #include "wl1271_ps.h"
44 #include "wl1271_init.h"
45 #include "wl1271_debugfs.h"
46 #include "wl1271_cmd.h"
47 #include "wl1271_boot.h"
49 static int wl1271_plt_init(struct wl1271
*wl
)
53 ret
= wl1271_acx_init_mem_config(wl
);
57 ret
= wl1271_cmd_data_path(wl
, wl
->channel
, 1);
64 static void wl1271_disable_interrupts(struct wl1271
*wl
)
69 static void wl1271_power_off(struct wl1271
*wl
)
74 static void wl1271_power_on(struct wl1271
*wl
)
79 static void wl1271_fw_status(struct wl1271
*wl
,
80 struct wl1271_fw_status
*status
)
85 wl1271_spi_read(wl
, FW_STATUS_ADDR
, status
,
86 sizeof(*status
), false);
88 wl1271_debug(DEBUG_IRQ
, "intr: 0x%x (fw_rx_counter = %d, "
89 "drv_rx_counter = %d, tx_results_counter = %d)",
91 status
->fw_rx_counter
,
92 status
->drv_rx_counter
,
93 status
->tx_results_counter
);
95 /* update number of available TX blocks */
96 for (i
= 0; i
< NUM_TX_QUEUES
; i
++) {
97 u32 cnt
= status
->tx_released_blks
[i
] - wl
->tx_blocks_freed
[i
];
98 wl
->tx_blocks_freed
[i
] = status
->tx_released_blks
[i
];
99 wl
->tx_blocks_available
+= cnt
;
103 /* if more blocks are available now, schedule some tx work */
104 if (total
&& !skb_queue_empty(&wl
->tx_queue
))
105 ieee80211_queue_work(wl
->hw
, &wl
->tx_work
);
107 /* update the host-chipset time offset */
108 wl
->time_offset
= jiffies_to_usecs(jiffies
) - status
->fw_localtime
;
111 static void wl1271_irq_work(struct work_struct
*work
)
116 container_of(work
, struct wl1271
, irq_work
);
118 mutex_lock(&wl
->mutex
);
120 wl1271_debug(DEBUG_IRQ
, "IRQ work");
122 if (wl
->state
== WL1271_STATE_OFF
)
125 ret
= wl1271_ps_elp_wakeup(wl
, true);
129 wl1271_spi_write32(wl
, ACX_REG_INTERRUPT_MASK
, WL1271_ACX_INTR_ALL
);
131 wl1271_fw_status(wl
, wl
->fw_status
);
132 intr
= wl
->fw_status
->intr
;
134 wl1271_debug(DEBUG_IRQ
, "Zero interrupt received.");
138 intr
&= WL1271_INTR_MASK
;
140 if (intr
& (WL1271_ACX_INTR_EVENT_A
|
141 WL1271_ACX_INTR_EVENT_B
)) {
142 wl1271_debug(DEBUG_IRQ
,
143 "WL1271_ACX_INTR_EVENT (0x%x)", intr
);
144 if (intr
& WL1271_ACX_INTR_EVENT_A
)
145 wl1271_event_handle(wl
, 0);
147 wl1271_event_handle(wl
, 1);
150 if (intr
& WL1271_ACX_INTR_INIT_COMPLETE
)
151 wl1271_debug(DEBUG_IRQ
,
152 "WL1271_ACX_INTR_INIT_COMPLETE");
154 if (intr
& WL1271_ACX_INTR_HW_AVAILABLE
)
155 wl1271_debug(DEBUG_IRQ
, "WL1271_ACX_INTR_HW_AVAILABLE");
157 if (intr
& WL1271_ACX_INTR_DATA
) {
158 u8 tx_res_cnt
= wl
->fw_status
->tx_results_counter
-
159 wl
->tx_results_count
;
161 wl1271_debug(DEBUG_IRQ
, "WL1271_ACX_INTR_DATA");
163 /* check for tx results */
165 wl1271_tx_complete(wl
, tx_res_cnt
);
167 wl1271_rx(wl
, wl
->fw_status
);
171 wl1271_spi_write32(wl
, ACX_REG_INTERRUPT_MASK
,
172 WL1271_ACX_INTR_ALL
& ~(WL1271_INTR_MASK
));
173 wl1271_ps_elp_sleep(wl
);
176 mutex_unlock(&wl
->mutex
);
179 static irqreturn_t
wl1271_irq(int irq
, void *cookie
)
184 wl1271_debug(DEBUG_IRQ
, "IRQ");
188 /* complete the ELP completion */
189 spin_lock_irqsave(&wl
->wl_lock
, flags
);
191 complete(wl
->elp_compl
);
192 wl
->elp_compl
= NULL
;
195 ieee80211_queue_work(wl
->hw
, &wl
->irq_work
);
196 spin_unlock_irqrestore(&wl
->wl_lock
, flags
);
201 static int wl1271_fetch_firmware(struct wl1271
*wl
)
203 const struct firmware
*fw
;
206 ret
= request_firmware(&fw
, WL1271_FW_NAME
, &wl
->spi
->dev
);
209 wl1271_error("could not get firmware: %d", ret
);
214 wl1271_error("firmware size is not multiple of 32 bits: %zu",
220 wl
->fw_len
= fw
->size
;
221 wl
->fw
= vmalloc(wl
->fw_len
);
224 wl1271_error("could not allocate memory for the firmware");
229 memcpy(wl
->fw
, fw
->data
, wl
->fw_len
);
234 release_firmware(fw
);
239 static int wl1271_fetch_nvs(struct wl1271
*wl
)
241 const struct firmware
*fw
;
244 ret
= request_firmware(&fw
, WL1271_NVS_NAME
, &wl
->spi
->dev
);
247 wl1271_error("could not get nvs file: %d", ret
);
252 wl1271_error("nvs size is not multiple of 32 bits: %zu",
258 wl
->nvs_len
= fw
->size
;
259 wl
->nvs
= kmalloc(wl
->nvs_len
, GFP_KERNEL
);
262 wl1271_error("could not allocate memory for the nvs file");
267 memcpy(wl
->nvs
, fw
->data
, wl
->nvs_len
);
272 release_firmware(fw
);
277 static void wl1271_fw_wakeup(struct wl1271
*wl
)
281 elp_reg
= ELPCTRL_WAKE_UP
;
282 wl1271_raw_write32(wl
, HW_ACCESS_ELP_CTRL_REG_ADDR
, elp_reg
);
285 static int wl1271_setup(struct wl1271
*wl
)
287 wl
->fw_status
= kmalloc(sizeof(*wl
->fw_status
), GFP_KERNEL
);
291 wl
->tx_res_if
= kmalloc(sizeof(*wl
->tx_res_if
), GFP_KERNEL
);
292 if (!wl
->tx_res_if
) {
293 kfree(wl
->fw_status
);
297 INIT_WORK(&wl
->irq_work
, wl1271_irq_work
);
298 INIT_WORK(&wl
->tx_work
, wl1271_tx_work
);
302 static int wl1271_chip_wakeup(struct wl1271
*wl
)
304 struct wl1271_partition_set partition
;
308 msleep(WL1271_POWER_ON_SLEEP
);
309 wl1271_spi_reset(wl
);
312 /* We don't need a real memory partition here, because we only want
313 * to use the registers at this point. */
314 memset(&partition
, 0, sizeof(partition
));
315 partition
.reg
.start
= REGISTERS_BASE
;
316 partition
.reg
.size
= REGISTERS_DOWN_SIZE
;
317 wl1271_set_partition(wl
, &partition
);
319 /* ELP module wake up */
320 wl1271_fw_wakeup(wl
);
322 /* whal_FwCtrl_BootSm() */
324 /* 0. read chip id from CHIP_ID */
325 wl
->chip
.id
= wl1271_spi_read32(wl
, CHIP_ID_B
);
327 /* 1. check if chip id is valid */
329 switch (wl
->chip
.id
) {
330 case CHIP_ID_1271_PG10
:
331 wl1271_warning("chip id 0x%x (1271 PG10) support is obsolete",
334 ret
= wl1271_setup(wl
);
338 case CHIP_ID_1271_PG20
:
339 wl1271_debug(DEBUG_BOOT
, "chip id 0x%x (1271 PG20)",
342 ret
= wl1271_setup(wl
);
347 wl1271_error("unsupported chip id: 0x%x", wl
->chip
.id
);
352 if (wl
->fw
== NULL
) {
353 ret
= wl1271_fetch_firmware(wl
);
358 /* No NVS from netlink, try to get it from the filesystem */
359 if (wl
->nvs
== NULL
) {
360 ret
= wl1271_fetch_nvs(wl
);
369 struct wl1271_filter_params
{
370 unsigned int filters
;
371 unsigned int changed
;
373 u8 mc_list
[ACX_MC_ADDRESS_GROUP_MAX
][ETH_ALEN
];
376 #define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
379 FIF_BCN_PRBRESP_PROMISC | \
383 static void wl1271_filter_work(struct work_struct
*work
)
386 container_of(work
, struct wl1271
, filter_work
);
387 struct wl1271_filter_params
*fp
;
392 /* first, get the filter parameters */
393 spin_lock_irqsave(&wl
->wl_lock
, flags
);
394 fp
= wl
->filter_params
;
395 wl
->filter_params
= NULL
;
396 spin_unlock_irqrestore(&wl
->wl_lock
, flags
);
401 /* then, lock the mutex without risk of lock-up */
402 mutex_lock(&wl
->mutex
);
404 if (wl
->state
== WL1271_STATE_OFF
)
407 ret
= wl1271_ps_elp_wakeup(wl
, false);
411 /* configure the mc filter regardless of the changed flags */
412 if (fp
->filters
& FIF_ALLMULTI
)
415 ret
= wl1271_acx_group_address_tbl(wl
, enabled
,
416 fp
->mc_list
, fp
->mc_list_length
);
420 /* determine, whether supported filter values have changed */
421 if (fp
->changed
== 0)
424 /* apply configured filters */
425 ret
= wl1271_acx_rx_config(wl
, wl
->rx_config
, wl
->rx_filter
);
430 wl1271_ps_elp_sleep(wl
);
433 mutex_unlock(&wl
->mutex
);
437 int wl1271_plt_start(struct wl1271
*wl
)
441 mutex_lock(&wl
->mutex
);
443 wl1271_notice("power up");
445 if (wl
->state
!= WL1271_STATE_OFF
) {
446 wl1271_error("cannot go into PLT state because not "
447 "in off state: %d", wl
->state
);
452 wl
->state
= WL1271_STATE_PLT
;
454 ret
= wl1271_chip_wakeup(wl
);
458 ret
= wl1271_boot(wl
);
462 wl1271_notice("firmware booted in PLT mode (%s)", wl
->chip
.fw_ver
);
464 ret
= wl1271_plt_init(wl
);
469 mutex_unlock(&wl
->mutex
);
474 int wl1271_plt_stop(struct wl1271
*wl
)
478 mutex_lock(&wl
->mutex
);
480 wl1271_notice("power down");
482 if (wl
->state
!= WL1271_STATE_PLT
) {
483 wl1271_error("cannot power down because not in PLT "
484 "state: %d", wl
->state
);
489 wl1271_disable_interrupts(wl
);
490 wl1271_power_off(wl
);
492 wl
->state
= WL1271_STATE_OFF
;
495 mutex_unlock(&wl
->mutex
);
501 static int wl1271_op_tx(struct ieee80211_hw
*hw
, struct sk_buff
*skb
)
503 struct wl1271
*wl
= hw
->priv
;
505 skb_queue_tail(&wl
->tx_queue
, skb
);
508 * The chip specific setup must run before the first TX packet -
509 * before that, the tx_work will not be initialized!
512 ieee80211_queue_work(wl
->hw
, &wl
->tx_work
);
515 * The workqueue is slow to process the tx_queue and we need stop
516 * the queue here, otherwise the queue will get too long.
518 if (skb_queue_len(&wl
->tx_queue
) >= WL1271_TX_QUEUE_MAX_LENGTH
) {
519 ieee80211_stop_queues(wl
->hw
);
522 * FIXME: this is racy, the variable is not properly
523 * protected. Maybe fix this by removing the stupid
524 * variable altogether and checking the real queue state?
526 wl
->tx_queue_stopped
= true;
532 static int wl1271_op_start(struct ieee80211_hw
*hw
)
534 struct wl1271
*wl
= hw
->priv
;
537 wl1271_debug(DEBUG_MAC80211
, "mac80211 start");
539 mutex_lock(&wl
->mutex
);
541 if (wl
->state
!= WL1271_STATE_OFF
) {
542 wl1271_error("cannot start because not in off state: %d",
548 ret
= wl1271_chip_wakeup(wl
);
552 ret
= wl1271_boot(wl
);
556 ret
= wl1271_hw_init(wl
);
560 wl
->state
= WL1271_STATE_ON
;
562 wl1271_info("firmware booted (%s)", wl
->chip
.fw_ver
);
566 wl1271_power_off(wl
);
568 mutex_unlock(&wl
->mutex
);
573 static void wl1271_op_stop(struct ieee80211_hw
*hw
)
575 struct wl1271
*wl
= hw
->priv
;
581 wl1271_debug(DEBUG_MAC80211
, "mac80211 stop");
583 /* complete/cancel ongoing work */
584 cancel_work_sync(&wl
->filter_work
);
585 spin_lock_irqsave(&wl
->wl_lock
, flags
);
586 kfree(wl
->filter_params
);
587 wl
->filter_params
= NULL
;
588 spin_unlock_irqrestore(&wl
->wl_lock
, flags
);
590 mutex_lock(&wl
->mutex
);
592 WARN_ON(wl
->state
!= WL1271_STATE_ON
);
595 mutex_unlock(&wl
->mutex
);
596 ieee80211_scan_completed(wl
->hw
, true);
597 mutex_lock(&wl
->mutex
);
598 wl
->scanning
= false;
601 wl
->state
= WL1271_STATE_OFF
;
603 wl1271_disable_interrupts(wl
);
605 mutex_unlock(&wl
->mutex
);
607 cancel_work_sync(&wl
->irq_work
);
608 cancel_work_sync(&wl
->tx_work
);
609 cancel_work_sync(&wl
->filter_work
);
611 mutex_lock(&wl
->mutex
);
613 /* let's notify MAC80211 about the remaining pending TX frames */
615 wl1271_power_off(wl
);
617 memset(wl
->bssid
, 0, ETH_ALEN
);
618 memset(wl
->ssid
, 0, IW_ESSID_MAX_SIZE
+ 1);
621 wl
->bss_type
= MAX_BSS_TYPE
;
622 wl
->band
= IEEE80211_BAND_2GHZ
;
627 wl
->tx_queue_stopped
= false;
628 wl
->power_level
= WL1271_DEFAULT_POWER_LEVEL
;
629 wl
->tx_blocks_available
= 0;
630 wl
->tx_results_count
= 0;
631 wl
->tx_packets_count
= 0;
632 wl
->tx_security_last_seq
= 0;
633 wl
->tx_security_seq_16
= 0;
634 wl
->tx_security_seq_32
= 0;
636 wl
->session_counter
= 0;
639 for (i
= 0; i
< NUM_TX_QUEUES
; i
++)
640 wl
->tx_blocks_freed
[i
] = 0;
642 wl1271_debugfs_reset(wl
);
643 mutex_unlock(&wl
->mutex
);
646 static int wl1271_op_add_interface(struct ieee80211_hw
*hw
,
647 struct ieee80211_if_init_conf
*conf
)
649 struct wl1271
*wl
= hw
->priv
;
652 wl1271_debug(DEBUG_MAC80211
, "mac80211 add interface type %d mac %pM",
653 conf
->type
, conf
->mac_addr
);
655 mutex_lock(&wl
->mutex
);
663 switch (conf
->type
) {
664 case NL80211_IFTYPE_STATION
:
665 wl
->bss_type
= BSS_TYPE_STA_BSS
;
667 case NL80211_IFTYPE_ADHOC
:
668 wl
->bss_type
= BSS_TYPE_IBSS
;
675 /* FIXME: what if conf->mac_addr changes? */
678 mutex_unlock(&wl
->mutex
);
682 static void wl1271_op_remove_interface(struct ieee80211_hw
*hw
,
683 struct ieee80211_if_init_conf
*conf
)
685 struct wl1271
*wl
= hw
->priv
;
687 mutex_lock(&wl
->mutex
);
688 wl1271_debug(DEBUG_MAC80211
, "mac80211 remove interface");
690 mutex_unlock(&wl
->mutex
);
694 static int wl1271_op_config_interface(struct ieee80211_hw
*hw
,
695 struct ieee80211_vif
*vif
,
696 struct ieee80211_if_conf
*conf
)
698 struct wl1271
*wl
= hw
->priv
;
699 struct sk_buff
*beacon
;
702 wl1271_debug(DEBUG_MAC80211
, "mac80211 config_interface bssid %pM",
704 wl1271_dump_ascii(DEBUG_MAC80211
, "ssid: ", conf
->ssid
,
707 mutex_lock(&wl
->mutex
);
709 ret
= wl1271_ps_elp_wakeup(wl
, false);
713 if (memcmp(wl
->bssid
, conf
->bssid
, ETH_ALEN
)) {
714 wl1271_debug(DEBUG_MAC80211
, "bssid changed");
716 memcpy(wl
->bssid
, conf
->bssid
, ETH_ALEN
);
718 ret
= wl1271_cmd_join(wl
);
723 ret
= wl1271_cmd_build_null_data(wl
);
727 wl
->ssid_len
= conf
->ssid_len
;
729 memcpy(wl
->ssid
, conf
->ssid
, wl
->ssid_len
);
731 if (conf
->changed
& IEEE80211_IFCC_BEACON
) {
732 beacon
= ieee80211_beacon_get(hw
, vif
);
733 ret
= wl1271_cmd_template_set(wl
, CMD_TEMPL_BEACON
,
734 beacon
->data
, beacon
->len
);
737 dev_kfree_skb(beacon
);
741 ret
= wl1271_cmd_template_set(wl
, CMD_TEMPL_PROBE_RESPONSE
,
742 beacon
->data
, beacon
->len
);
744 dev_kfree_skb(beacon
);
751 wl1271_ps_elp_sleep(wl
);
754 mutex_unlock(&wl
->mutex
);
760 static int wl1271_op_config(struct ieee80211_hw
*hw
, u32 changed
)
762 struct wl1271
*wl
= hw
->priv
;
763 struct ieee80211_conf
*conf
= &hw
->conf
;
764 int channel
, ret
= 0;
766 channel
= ieee80211_frequency_to_channel(conf
->channel
->center_freq
);
768 wl1271_debug(DEBUG_MAC80211
, "mac80211 config ch %d psm %s power %d",
770 conf
->flags
& IEEE80211_CONF_PS
? "on" : "off",
773 mutex_lock(&wl
->mutex
);
775 wl
->band
= conf
->channel
->band
;
777 ret
= wl1271_ps_elp_wakeup(wl
, false);
781 if (channel
!= wl
->channel
) {
783 * We assume that the stack will configure the right channel
784 * before associating, so we don't need to send a join
785 * command here. We will join the right channel when the
788 wl
->channel
= channel
;
791 ret
= wl1271_cmd_build_null_data(wl
);
795 if (conf
->flags
& IEEE80211_CONF_PS
&& !wl
->psm_requested
) {
796 wl1271_info("psm enabled");
798 wl
->psm_requested
= true;
801 * We enter PSM only if we're already associated.
802 * If we're not, we'll enter it when joining an SSID,
803 * through the bss_info_changed() hook.
805 ret
= wl1271_ps_set_mode(wl
, STATION_POWER_SAVE_MODE
);
806 } else if (!(conf
->flags
& IEEE80211_CONF_PS
) &&
808 wl1271_info("psm disabled");
810 wl
->psm_requested
= false;
813 ret
= wl1271_ps_set_mode(wl
, STATION_ACTIVE_MODE
);
816 if (conf
->power_level
!= wl
->power_level
) {
817 ret
= wl1271_acx_tx_power(wl
, conf
->power_level
);
821 wl
->power_level
= conf
->power_level
;
825 wl1271_ps_elp_sleep(wl
);
828 mutex_unlock(&wl
->mutex
);
833 static u64
wl1271_op_prepare_multicast(struct ieee80211_hw
*hw
, int mc_count
,
834 struct dev_addr_list
*mc_list
)
836 struct wl1271
*wl
= hw
->priv
;
837 struct wl1271_filter_params
*fp
;
842 * FIXME: we should return a hash that will be passed to
843 * configure_filter() instead of saving everything in the context.
846 fp
= kzalloc(sizeof(*fp
), GFP_KERNEL
);
848 wl1271_error("Out of memory setting filters.");
852 /* update multicast filtering parameters */
853 if (mc_count
> ACX_MC_ADDRESS_GROUP_MAX
) {
855 fp
->filters
|= FIF_ALLMULTI
;
858 fp
->mc_list_length
= 0;
859 for (i
= 0; i
< mc_count
; i
++) {
860 if (mc_list
->da_addrlen
== ETH_ALEN
) {
861 memcpy(fp
->mc_list
[fp
->mc_list_length
],
862 mc_list
->da_addr
, ETH_ALEN
);
863 fp
->mc_list_length
++;
865 wl1271_warning("Unknown mc address length.");
868 /* FIXME: We still need to set our filters properly */
870 spin_lock_irqsave(&wl
->wl_lock
, flags
);
871 kfree(wl
->filter_params
);
872 wl
->filter_params
= fp
;
873 spin_unlock_irqrestore(&wl
->wl_lock
, flags
);
878 static void wl1271_op_configure_filter(struct ieee80211_hw
*hw
,
879 unsigned int changed
,
880 unsigned int *total
, u64 multicast
)
882 struct wl1271
*wl
= hw
->priv
;
884 wl1271_debug(DEBUG_MAC80211
, "mac80211 configure filter");
886 *total
&= WL1271_SUPPORTED_FILTERS
;
887 changed
&= WL1271_SUPPORTED_FILTERS
;
893 * FIXME: for now we are still using a workqueue for filter
894 * configuration, but with the new mac80211, this is not needed,
895 * since configure_filter can now sleep. We now have
896 * prepare_multicast, which needs to be atomic instead.
899 /* store current filter config */
900 wl
->filter_params
->filters
= *total
;
901 wl
->filter_params
->changed
= changed
;
903 ieee80211_queue_work(wl
->hw
, &wl
->filter_work
);
906 static int wl1271_op_set_key(struct ieee80211_hw
*hw
, enum set_key_cmd cmd
,
907 struct ieee80211_vif
*vif
,
908 struct ieee80211_sta
*sta
,
909 struct ieee80211_key_conf
*key_conf
)
911 struct wl1271
*wl
= hw
->priv
;
918 static const u8 bcast_addr
[ETH_ALEN
] =
919 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
921 wl1271_debug(DEBUG_MAC80211
, "mac80211 set key");
923 addr
= sta
? sta
->addr
: bcast_addr
;
925 wl1271_debug(DEBUG_CRYPT
, "CMD: 0x%x", cmd
);
926 wl1271_dump(DEBUG_CRYPT
, "ADDR: ", addr
, ETH_ALEN
);
927 wl1271_debug(DEBUG_CRYPT
, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
928 key_conf
->alg
, key_conf
->keyidx
,
929 key_conf
->keylen
, key_conf
->flags
);
930 wl1271_dump(DEBUG_CRYPT
, "KEY: ", key_conf
->key
, key_conf
->keylen
);
932 if (is_zero_ether_addr(addr
)) {
933 /* We dont support TX only encryption */
938 mutex_lock(&wl
->mutex
);
940 ret
= wl1271_ps_elp_wakeup(wl
, false);
944 switch (key_conf
->alg
) {
948 key_conf
->hw_key_idx
= key_conf
->keyidx
;
953 key_conf
->hw_key_idx
= key_conf
->keyidx
;
954 tx_seq_32
= wl
->tx_security_seq_32
;
955 tx_seq_16
= wl
->tx_security_seq_16
;
960 key_conf
->flags
|= IEEE80211_KEY_FLAG_GENERATE_IV
;
961 tx_seq_32
= wl
->tx_security_seq_32
;
962 tx_seq_16
= wl
->tx_security_seq_16
;
965 wl1271_error("Unknown key algo 0x%x", key_conf
->alg
);
973 ret
= wl1271_cmd_set_key(wl
, KEY_ADD_OR_REPLACE
,
974 key_conf
->keyidx
, key_type
,
975 key_conf
->keylen
, key_conf
->key
,
976 addr
, tx_seq_32
, tx_seq_16
);
978 wl1271_error("Could not add or replace key");
984 ret
= wl1271_cmd_set_key(wl
, KEY_REMOVE
,
985 key_conf
->keyidx
, key_type
,
986 key_conf
->keylen
, key_conf
->key
,
989 wl1271_error("Could not remove key");
995 wl1271_error("Unsupported key cmd 0x%x", cmd
);
1003 wl1271_ps_elp_sleep(wl
);
1006 mutex_unlock(&wl
->mutex
);
1012 static int wl1271_op_hw_scan(struct ieee80211_hw
*hw
,
1013 struct cfg80211_scan_request
*req
)
1015 struct wl1271
*wl
= hw
->priv
;
1018 size_t ssid_len
= 0;
1020 wl1271_debug(DEBUG_MAC80211
, "mac80211 hw scan");
1023 ssid
= req
->ssids
[0].ssid
;
1024 ssid_len
= req
->ssids
[0].ssid_len
;
1027 mutex_lock(&wl
->mutex
);
1029 ret
= wl1271_ps_elp_wakeup(wl
, false);
1033 ret
= wl1271_cmd_scan(hw
->priv
, ssid
, ssid_len
, 1, 0, 13, 3);
1035 wl1271_ps_elp_sleep(wl
);
1038 mutex_unlock(&wl
->mutex
);
1043 static int wl1271_op_set_rts_threshold(struct ieee80211_hw
*hw
, u32 value
)
1045 struct wl1271
*wl
= hw
->priv
;
1048 mutex_lock(&wl
->mutex
);
1050 ret
= wl1271_ps_elp_wakeup(wl
, false);
1054 ret
= wl1271_acx_rts_threshold(wl
, (u16
) value
);
1056 wl1271_warning("wl1271_op_set_rts_threshold failed: %d", ret
);
1058 wl1271_ps_elp_sleep(wl
);
1061 mutex_unlock(&wl
->mutex
);
1066 static u32
wl1271_enabled_rates_get(struct wl1271
*wl
, u64 basic_rate_set
)
1068 struct ieee80211_supported_band
*band
;
1069 u32 enabled_rates
= 0;
1072 band
= wl
->hw
->wiphy
->bands
[wl
->band
];
1073 for (bit
= 0; bit
< band
->n_bitrates
; bit
++) {
1074 if (basic_rate_set
& 0x1)
1075 enabled_rates
|= band
->bitrates
[bit
].hw_value
;
1076 basic_rate_set
>>= 1;
1079 return enabled_rates
;
1082 static void wl1271_op_bss_info_changed(struct ieee80211_hw
*hw
,
1083 struct ieee80211_vif
*vif
,
1084 struct ieee80211_bss_conf
*bss_conf
,
1087 enum wl1271_cmd_ps_mode mode
;
1088 struct wl1271
*wl
= hw
->priv
;
1091 wl1271_debug(DEBUG_MAC80211
, "mac80211 bss info changed");
1093 mutex_lock(&wl
->mutex
);
1095 ret
= wl1271_ps_elp_wakeup(wl
, false);
1099 if (changed
& BSS_CHANGED_ASSOC
) {
1100 if (bss_conf
->assoc
) {
1101 wl
->aid
= bss_conf
->aid
;
1104 * with wl1271, we don't need to update the
1105 * beacon_int and dtim_period, because the firmware
1106 * updates it by itself when the first beacon is
1107 * received after a join.
1109 ret
= wl1271_cmd_build_ps_poll(wl
, wl
->aid
);
1113 ret
= wl1271_acx_aid(wl
, wl
->aid
);
1117 /* If we want to go in PSM but we're not there yet */
1118 if (wl
->psm_requested
&& !wl
->psm
) {
1119 mode
= STATION_POWER_SAVE_MODE
;
1120 ret
= wl1271_ps_set_mode(wl
, mode
);
1125 /* use defaults when not associated */
1126 wl
->basic_rate_set
= WL1271_DEFAULT_BASIC_RATE_SET
;
1132 if (changed
& BSS_CHANGED_ERP_SLOT
) {
1133 if (bss_conf
->use_short_slot
)
1134 ret
= wl1271_acx_slot(wl
, SLOT_TIME_SHORT
);
1136 ret
= wl1271_acx_slot(wl
, SLOT_TIME_LONG
);
1138 wl1271_warning("Set slot time failed %d", ret
);
1143 if (changed
& BSS_CHANGED_ERP_PREAMBLE
) {
1144 if (bss_conf
->use_short_preamble
)
1145 wl1271_acx_set_preamble(wl
, ACX_PREAMBLE_SHORT
);
1147 wl1271_acx_set_preamble(wl
, ACX_PREAMBLE_LONG
);
1150 if (changed
& BSS_CHANGED_ERP_CTS_PROT
) {
1151 if (bss_conf
->use_cts_prot
)
1152 ret
= wl1271_acx_cts_protect(wl
, CTSPROTECT_ENABLE
);
1154 ret
= wl1271_acx_cts_protect(wl
, CTSPROTECT_DISABLE
);
1156 wl1271_warning("Set ctsprotect failed %d", ret
);
1161 if (changed
& BSS_CHANGED_BASIC_RATES
) {
1162 wl
->basic_rate_set
= wl1271_enabled_rates_get(
1163 wl
, bss_conf
->basic_rates
);
1165 ret
= wl1271_acx_rate_policies(wl
, wl
->basic_rate_set
);
1167 wl1271_warning("Set rate policies failed %d", ret
);
1173 wl1271_ps_elp_sleep(wl
);
1176 mutex_unlock(&wl
->mutex
);
1180 /* can't be const, mac80211 writes to this */
1181 static struct ieee80211_rate wl1271_rates
[] = {
1184 .hw_value_short
= 0x1, },
1187 .hw_value_short
= 0x2,
1188 .flags
= IEEE80211_RATE_SHORT_PREAMBLE
},
1191 .hw_value_short
= 0x4,
1192 .flags
= IEEE80211_RATE_SHORT_PREAMBLE
},
1195 .hw_value_short
= 0x20,
1196 .flags
= IEEE80211_RATE_SHORT_PREAMBLE
},
1199 .hw_value_short
= 0x8, },
1202 .hw_value_short
= 0x10, },
1205 .hw_value_short
= 0x40, },
1208 .hw_value_short
= 0x80, },
1211 .hw_value_short
= 0x200, },
1214 .hw_value_short
= 0x400, },
1217 .hw_value_short
= 0x800, },
1220 .hw_value_short
= 0x1000, },
1223 /* can't be const, mac80211 writes to this */
1224 static struct ieee80211_channel wl1271_channels
[] = {
1225 { .hw_value
= 1, .center_freq
= 2412},
1226 { .hw_value
= 2, .center_freq
= 2417},
1227 { .hw_value
= 3, .center_freq
= 2422},
1228 { .hw_value
= 4, .center_freq
= 2427},
1229 { .hw_value
= 5, .center_freq
= 2432},
1230 { .hw_value
= 6, .center_freq
= 2437},
1231 { .hw_value
= 7, .center_freq
= 2442},
1232 { .hw_value
= 8, .center_freq
= 2447},
1233 { .hw_value
= 9, .center_freq
= 2452},
1234 { .hw_value
= 10, .center_freq
= 2457},
1235 { .hw_value
= 11, .center_freq
= 2462},
1236 { .hw_value
= 12, .center_freq
= 2467},
1237 { .hw_value
= 13, .center_freq
= 2472},
1240 /* can't be const, mac80211 writes to this */
1241 static struct ieee80211_supported_band wl1271_band_2ghz
= {
1242 .channels
= wl1271_channels
,
1243 .n_channels
= ARRAY_SIZE(wl1271_channels
),
1244 .bitrates
= wl1271_rates
,
1245 .n_bitrates
= ARRAY_SIZE(wl1271_rates
),
1248 static const struct ieee80211_ops wl1271_ops
= {
1249 .start
= wl1271_op_start
,
1250 .stop
= wl1271_op_stop
,
1251 .add_interface
= wl1271_op_add_interface
,
1252 .remove_interface
= wl1271_op_remove_interface
,
1253 .config
= wl1271_op_config
,
1254 /* .config_interface = wl1271_op_config_interface, */
1255 .prepare_multicast
= wl1271_op_prepare_multicast
,
1256 .configure_filter
= wl1271_op_configure_filter
,
1258 .set_key
= wl1271_op_set_key
,
1259 .hw_scan
= wl1271_op_hw_scan
,
1260 .bss_info_changed
= wl1271_op_bss_info_changed
,
1261 .set_rts_threshold
= wl1271_op_set_rts_threshold
,
1264 static int wl1271_register_hw(struct wl1271
*wl
)
1268 if (wl
->mac80211_registered
)
1271 SET_IEEE80211_PERM_ADDR(wl
->hw
, wl
->mac_addr
);
1273 ret
= ieee80211_register_hw(wl
->hw
);
1275 wl1271_error("unable to register mac80211 hw: %d", ret
);
1279 wl
->mac80211_registered
= true;
1281 wl1271_notice("loaded");
1286 static int wl1271_init_ieee80211(struct wl1271
*wl
)
1288 /* The tx descriptor buffer and the TKIP space. */
1289 wl
->hw
->extra_tx_headroom
= WL1271_TKIP_IV_SPACE
+
1290 sizeof(struct wl1271_tx_hw_descr
);
1293 /* FIXME: find a proper value */
1294 wl
->hw
->channel_change_time
= 10000;
1296 wl
->hw
->flags
= IEEE80211_HW_SIGNAL_DBM
|
1297 IEEE80211_HW_NOISE_DBM
|
1298 IEEE80211_HW_BEACON_FILTER
;
1300 wl
->hw
->wiphy
->interface_modes
= BIT(NL80211_IFTYPE_STATION
);
1301 wl
->hw
->wiphy
->max_scan_ssids
= 1;
1302 wl
->hw
->wiphy
->bands
[IEEE80211_BAND_2GHZ
] = &wl1271_band_2ghz
;
1304 SET_IEEE80211_DEV(wl
->hw
, &wl
->spi
->dev
);
1309 static void wl1271_device_release(struct device
*dev
)
1314 static struct platform_device wl1271_device
= {
1318 /* device model insists to have a release function */
1320 .release
= wl1271_device_release
,
1324 #define WL1271_DEFAULT_CHANNEL 0
1325 static int __devinit
wl1271_probe(struct spi_device
*spi
)
1327 struct wl12xx_platform_data
*pdata
;
1328 struct ieee80211_hw
*hw
;
1331 static const u8 nokia_oui
[3] = {0x00, 0x1f, 0xdf};
1333 pdata
= spi
->dev
.platform_data
;
1335 wl1271_error("no platform data");
1339 hw
= ieee80211_alloc_hw(sizeof(*wl
), &wl1271_ops
);
1341 wl1271_error("could not alloc ieee80211_hw");
1346 memset(wl
, 0, sizeof(*wl
));
1349 dev_set_drvdata(&spi
->dev
, wl
);
1352 skb_queue_head_init(&wl
->tx_queue
);
1354 INIT_WORK(&wl
->filter_work
, wl1271_filter_work
);
1355 INIT_DELAYED_WORK(&wl
->elp_work
, wl1271_elp_work
);
1356 wl
->channel
= WL1271_DEFAULT_CHANNEL
;
1357 wl
->scanning
= false;
1358 wl
->default_key
= 0;
1361 wl
->rx_config
= WL1271_DEFAULT_RX_CONFIG
;
1362 wl
->rx_filter
= WL1271_DEFAULT_RX_FILTER
;
1365 wl
->psm_requested
= false;
1366 wl
->tx_queue_stopped
= false;
1367 wl
->power_level
= WL1271_DEFAULT_POWER_LEVEL
;
1368 wl
->basic_rate_set
= WL1271_DEFAULT_BASIC_RATE_SET
;
1369 wl
->band
= IEEE80211_BAND_2GHZ
;
1373 for (i
= 0; i
< ACX_TX_DESCRIPTORS
; i
++)
1374 wl
->tx_frames
[i
] = NULL
;
1376 spin_lock_init(&wl
->wl_lock
);
1379 * In case our MAC address is not correctly set,
1380 * we use a random but Nokia MAC.
1382 memcpy(wl
->mac_addr
, nokia_oui
, 3);
1383 get_random_bytes(wl
->mac_addr
+ 3, 3);
1385 wl
->state
= WL1271_STATE_OFF
;
1386 mutex_init(&wl
->mutex
);
1388 wl
->rx_descriptor
= kmalloc(sizeof(*wl
->rx_descriptor
), GFP_KERNEL
);
1389 if (!wl
->rx_descriptor
) {
1390 wl1271_error("could not allocate memory for rx descriptor");
1395 /* This is the only SPI value that we need to set here, the rest
1396 * comes from the board-peripherals file */
1397 spi
->bits_per_word
= 32;
1399 ret
= spi_setup(spi
);
1401 wl1271_error("spi_setup failed");
1405 wl
->set_power
= pdata
->set_power
;
1406 if (!wl
->set_power
) {
1407 wl1271_error("set power function missing in platform data");
1414 wl1271_error("irq missing in platform data");
1419 ret
= request_irq(wl
->irq
, wl1271_irq
, 0, DRIVER_NAME
, wl
);
1421 wl1271_error("request_irq() failed: %d", ret
);
1425 set_irq_type(wl
->irq
, IRQ_TYPE_EDGE_RISING
);
1427 disable_irq(wl
->irq
);
1429 ret
= platform_device_register(&wl1271_device
);
1431 wl1271_error("couldn't register platform device");
1434 dev_set_drvdata(&wl1271_device
.dev
, wl
);
1436 ret
= wl1271_init_ieee80211(wl
);
1440 ret
= wl1271_register_hw(wl
);
1444 wl1271_debugfs_init(wl
);
1446 wl1271_notice("initialized");
1451 platform_device_unregister(&wl1271_device
);
1454 free_irq(wl
->irq
, wl
);
1457 kfree(wl
->rx_descriptor
);
1458 wl
->rx_descriptor
= NULL
;
1460 ieee80211_free_hw(hw
);
1465 static int __devexit
wl1271_remove(struct spi_device
*spi
)
1467 struct wl1271
*wl
= dev_get_drvdata(&spi
->dev
);
1469 ieee80211_unregister_hw(wl
->hw
);
1471 wl1271_debugfs_exit(wl
);
1472 platform_device_unregister(&wl1271_device
);
1473 free_irq(wl
->irq
, wl
);
1474 kfree(wl
->target_mem_map
);
1480 kfree(wl
->rx_descriptor
);
1481 wl
->rx_descriptor
= NULL
;
1483 kfree(wl
->fw_status
);
1484 kfree(wl
->tx_res_if
);
1486 ieee80211_free_hw(wl
->hw
);
1492 static struct spi_driver wl1271_spi_driver
= {
1495 .bus
= &spi_bus_type
,
1496 .owner
= THIS_MODULE
,
1499 .probe
= wl1271_probe
,
1500 .remove
= __devexit_p(wl1271_remove
),
1503 static int __init
wl1271_init(void)
1507 ret
= spi_register_driver(&wl1271_spi_driver
);
1509 wl1271_error("failed to register spi driver: %d", ret
);
1517 static void __exit
wl1271_exit(void)
1519 spi_unregister_driver(&wl1271_spi_driver
);
1521 wl1271_notice("unloaded");
1524 module_init(wl1271_init
);
1525 module_exit(wl1271_exit
);
1527 MODULE_LICENSE("GPL");
1528 MODULE_AUTHOR("Luciano Coelho <luciano.coelho@nokia.com>");
1529 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");