2 * mac80211 glue code for mac80211 Prism54 drivers
4 * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
5 * Copyright (c) 2007-2009, Christian Lamparter <chunkeey@web.de>
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
9 * - the islsm (softmac prism54) driver, which is:
10 * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
12 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
19 #include <linux/slab.h>
20 #include <linux/firmware.h>
21 #include <linux/etherdevice.h>
22 #include <linux/module.h>
24 #include <net/mac80211.h>
29 static bool modparam_nohwcrypt;
30 module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
31 MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
32 MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
33 MODULE_DESCRIPTION("Softmac Prism54 common code");
34 MODULE_LICENSE("GPL");
35 MODULE_ALIAS("prism54common");
37 static int p54_sta_add_remove(struct ieee80211_hw *hw,
38 struct ieee80211_vif *vif,
39 struct ieee80211_sta *sta)
41 struct p54_common *priv = hw->priv;
44 * Notify the firmware that we don't want or we don't
45 * need to buffer frames for this station anymore.
48 p54_sta_unlock(priv, sta->addr);
53 static void p54_sta_notify(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
54 enum sta_notify_cmd notify_cmd,
55 struct ieee80211_sta *sta)
57 struct p54_common *priv = dev->priv;
60 case STA_NOTIFY_AWAKE:
61 /* update the firmware's filter table */
62 p54_sta_unlock(priv, sta->addr);
69 static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
72 struct p54_common *priv = dev->priv;
74 return p54_update_beacon_tim(priv, sta->aid, set);
77 u8 *p54_find_ie(struct sk_buff *skb, u8 ie)
79 struct ieee80211_mgmt *mgmt = (void *)skb->data;
82 if (skb->len <= sizeof(mgmt))
85 pos = (u8 *)mgmt->u.beacon.variable;
86 end = skb->data + skb->len;
88 if (pos + 2 + pos[1] > end)
99 static int p54_beacon_format_ie_tim(struct sk_buff *skb)
102 * the good excuse for this mess is ... the firmware.
103 * The dummy TIM MUST be at the end of the beacon frame,
104 * because it'll be overwritten!
111 tim = p54_find_ie(skb, WLAN_EID_TIM);
116 dtim_period = tim[3];
117 next = tim + 2 + dtim_len;
122 memmove(tim, next, skb_tail_pointer(skb) - next);
123 tim = skb_tail_pointer(skb) - (dtim_len + 2);
125 /* add the dummy at the end */
126 tim[0] = WLAN_EID_TIM;
129 tim[3] = dtim_period;
133 skb_trim(skb, skb->len - (dtim_len - 3));
138 static int p54_beacon_update(struct p54_common *priv,
139 struct ieee80211_vif *vif)
141 struct ieee80211_tx_control control = { };
142 struct sk_buff *beacon;
145 beacon = ieee80211_beacon_get(priv->hw, vif);
148 ret = p54_beacon_format_ie_tim(beacon);
153 * During operation, the firmware takes care of beaconing.
154 * The driver only needs to upload a new beacon template, once
155 * the template was changed by the stack or userspace.
157 * LMAC API 3.2.2 also specifies that the driver does not need
158 * to cancel the old beacon template by hand, instead the firmware
159 * will release the previous one through the feedback mechanism.
161 p54_tx_80211(priv->hw, &control, beacon);
162 priv->tsf_high32 = 0;
168 static int p54_start(struct ieee80211_hw *dev)
170 struct p54_common *priv = dev->priv;
173 mutex_lock(&priv->conf_mutex);
174 err = priv->open(dev);
177 P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
178 P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
179 P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
180 P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
181 err = p54_set_edcf(priv);
185 memset(priv->bssid, ~0, ETH_ALEN);
186 priv->mode = NL80211_IFTYPE_MONITOR;
187 err = p54_setup_mac(priv);
189 priv->mode = NL80211_IFTYPE_UNSPECIFIED;
193 ieee80211_queue_delayed_work(dev, &priv->work, 0);
195 priv->softled_state = 0;
196 err = p54_set_leds(priv);
199 mutex_unlock(&priv->conf_mutex);
203 static void p54_stop(struct ieee80211_hw *dev)
205 struct p54_common *priv = dev->priv;
208 priv->mode = NL80211_IFTYPE_UNSPECIFIED;
209 priv->softled_state = 0;
210 cancel_delayed_work_sync(&priv->work);
211 mutex_lock(&priv->conf_mutex);
214 skb_queue_purge(&priv->tx_pending);
215 skb_queue_purge(&priv->tx_queue);
216 for (i = 0; i < P54_QUEUE_NUM; i++) {
217 priv->tx_stats[i].count = 0;
218 priv->tx_stats[i].len = 0;
221 priv->beacon_req_id = cpu_to_le32(0);
222 priv->tsf_high32 = priv->tsf_low32 = 0;
223 mutex_unlock(&priv->conf_mutex);
226 static int p54_add_interface(struct ieee80211_hw *dev,
227 struct ieee80211_vif *vif)
229 struct p54_common *priv = dev->priv;
232 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
234 mutex_lock(&priv->conf_mutex);
235 if (priv->mode != NL80211_IFTYPE_MONITOR) {
236 mutex_unlock(&priv->conf_mutex);
243 case NL80211_IFTYPE_STATION:
244 case NL80211_IFTYPE_ADHOC:
245 case NL80211_IFTYPE_AP:
246 case NL80211_IFTYPE_MESH_POINT:
247 priv->mode = vif->type;
250 mutex_unlock(&priv->conf_mutex);
254 memcpy(priv->mac_addr, vif->addr, ETH_ALEN);
255 err = p54_setup_mac(priv);
256 mutex_unlock(&priv->conf_mutex);
260 static void p54_remove_interface(struct ieee80211_hw *dev,
261 struct ieee80211_vif *vif)
263 struct p54_common *priv = dev->priv;
265 mutex_lock(&priv->conf_mutex);
269 * LMAC API 3.2.2 states that any active beacon template must be
270 * canceled by the driver before attempting a mode transition.
272 if (le32_to_cpu(priv->beacon_req_id) != 0) {
273 p54_tx_cancel(priv, priv->beacon_req_id);
274 wait_for_completion_interruptible_timeout(&priv->beacon_comp, HZ);
276 priv->mode = NL80211_IFTYPE_MONITOR;
277 memset(priv->mac_addr, 0, ETH_ALEN);
278 memset(priv->bssid, 0, ETH_ALEN);
280 mutex_unlock(&priv->conf_mutex);
283 static int p54_wait_for_stats(struct ieee80211_hw *dev)
285 struct p54_common *priv = dev->priv;
288 priv->update_stats = true;
289 ret = p54_fetch_statistics(priv);
293 ret = wait_for_completion_interruptible_timeout(&priv->stat_comp, HZ);
300 static void p54_reset_stats(struct p54_common *priv)
302 struct ieee80211_channel *chan = priv->curchan;
305 struct survey_info *info = &priv->survey[chan->hw_value];
307 /* only reset channel statistics, don't touch .filled, etc. */
308 info->channel_time = 0;
309 info->channel_time_busy = 0;
310 info->channel_time_tx = 0;
313 priv->update_stats = true;
314 priv->survey_raw.active = 0;
315 priv->survey_raw.cca = 0;
316 priv->survey_raw.tx = 0;
319 static int p54_config(struct ieee80211_hw *dev, u32 changed)
322 struct p54_common *priv = dev->priv;
323 struct ieee80211_conf *conf = &dev->conf;
325 mutex_lock(&priv->conf_mutex);
326 if (changed & IEEE80211_CONF_CHANGE_POWER)
327 priv->output_power = conf->power_level << 2;
328 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
329 struct ieee80211_channel *oldchan;
330 WARN_ON(p54_wait_for_stats(dev));
331 oldchan = priv->curchan;
332 priv->curchan = NULL;
333 ret = p54_scan(priv, P54_SCAN_EXIT, 0);
335 priv->curchan = oldchan;
339 * TODO: Use the LM_SCAN_TRAP to determine the current
342 priv->curchan = priv->hw->conf.chandef.chan;
343 p54_reset_stats(priv);
344 WARN_ON(p54_fetch_statistics(priv));
346 if (changed & IEEE80211_CONF_CHANGE_PS) {
347 WARN_ON(p54_wait_for_stats(dev));
348 ret = p54_set_ps(priv);
351 WARN_ON(p54_wait_for_stats(dev));
353 if (changed & IEEE80211_CONF_CHANGE_IDLE) {
354 WARN_ON(p54_wait_for_stats(dev));
355 ret = p54_setup_mac(priv);
358 WARN_ON(p54_wait_for_stats(dev));
362 mutex_unlock(&priv->conf_mutex);
366 static u64 p54_prepare_multicast(struct ieee80211_hw *dev,
367 struct netdev_hw_addr_list *mc_list)
369 struct p54_common *priv = dev->priv;
370 struct netdev_hw_addr *ha;
373 BUILD_BUG_ON(ARRAY_SIZE(priv->mc_maclist) !=
374 ARRAY_SIZE(((struct p54_group_address_table *)NULL)->mac_list));
376 * The first entry is reserved for the global broadcast MAC.
377 * Otherwise the firmware will drop it and ARP will no longer work.
380 priv->mc_maclist_num = netdev_hw_addr_list_count(mc_list) + i;
381 netdev_hw_addr_list_for_each(ha, mc_list) {
382 memcpy(&priv->mc_maclist[i], ha->addr, ETH_ALEN);
384 if (i >= ARRAY_SIZE(priv->mc_maclist))
388 return 1; /* update */
391 static void p54_configure_filter(struct ieee80211_hw *dev,
392 unsigned int changed_flags,
393 unsigned int *total_flags,
396 struct p54_common *priv = dev->priv;
398 *total_flags &= FIF_PROMISC_IN_BSS |
402 priv->filter_flags = *total_flags;
404 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS))
407 if (changed_flags & FIF_ALLMULTI || multicast)
408 p54_set_groupfilter(priv);
411 static int p54_conf_tx(struct ieee80211_hw *dev,
412 struct ieee80211_vif *vif, u16 queue,
413 const struct ieee80211_tx_queue_params *params)
415 struct p54_common *priv = dev->priv;
418 mutex_lock(&priv->conf_mutex);
419 if (queue < dev->queues) {
420 P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
421 params->cw_min, params->cw_max, params->txop);
422 ret = p54_set_edcf(priv);
425 mutex_unlock(&priv->conf_mutex);
429 static void p54_work(struct work_struct *work)
431 struct p54_common *priv = container_of(work, struct p54_common,
434 if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
438 * TODO: walk through tx_queue and do the following tasks
439 * 1. initiate bursts.
440 * 2. cancel stuck frames / reset the device if necessary.
443 mutex_lock(&priv->conf_mutex);
444 WARN_ON_ONCE(p54_fetch_statistics(priv));
445 mutex_unlock(&priv->conf_mutex);
448 static int p54_get_stats(struct ieee80211_hw *dev,
449 struct ieee80211_low_level_stats *stats)
451 struct p54_common *priv = dev->priv;
453 memcpy(stats, &priv->stats, sizeof(*stats));
457 static void p54_bss_info_changed(struct ieee80211_hw *dev,
458 struct ieee80211_vif *vif,
459 struct ieee80211_bss_conf *info,
462 struct p54_common *priv = dev->priv;
464 mutex_lock(&priv->conf_mutex);
465 if (changed & BSS_CHANGED_BSSID) {
466 memcpy(priv->bssid, info->bssid, ETH_ALEN);
470 if (changed & BSS_CHANGED_BEACON) {
471 p54_scan(priv, P54_SCAN_EXIT, 0);
473 p54_beacon_update(priv, vif);
477 if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_BEACON)) {
478 priv->use_short_slot = info->use_short_slot;
481 if (changed & BSS_CHANGED_BASIC_RATES) {
482 if (dev->conf.chandef.chan->band == IEEE80211_BAND_5GHZ)
483 priv->basic_rate_mask = (info->basic_rates << 4);
485 priv->basic_rate_mask = info->basic_rates;
487 if (priv->fw_var >= 0x500)
488 p54_scan(priv, P54_SCAN_EXIT, 0);
490 if (changed & BSS_CHANGED_ASSOC) {
492 priv->aid = info->aid;
493 priv->wakeup_timer = info->beacon_int *
494 info->dtim_period * 5;
497 priv->wakeup_timer = 500;
502 mutex_unlock(&priv->conf_mutex);
505 static int p54_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
506 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
507 struct ieee80211_key_conf *key)
509 struct p54_common *priv = dev->priv;
514 if (modparam_nohwcrypt)
517 if (key->flags & IEEE80211_KEY_FLAG_RX_MGMT) {
519 * Unfortunately most/all firmwares are trying to decrypt
520 * incoming management frames if a suitable key can be found.
521 * However, in doing so the data in these frames gets
522 * corrupted. So, we can't have firmware supported crypto
523 * offload in this case.
528 mutex_lock(&priv->conf_mutex);
529 if (cmd == SET_KEY) {
530 switch (key->cipher) {
531 case WLAN_CIPHER_SUITE_TKIP:
532 if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL |
533 BR_DESC_PRIV_CAP_TKIP))) {
537 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
538 algo = P54_CRYPTO_TKIPMICHAEL;
540 case WLAN_CIPHER_SUITE_WEP40:
541 case WLAN_CIPHER_SUITE_WEP104:
542 if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP)) {
546 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
547 algo = P54_CRYPTO_WEP;
549 case WLAN_CIPHER_SUITE_CCMP:
550 if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP)) {
554 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
555 algo = P54_CRYPTO_AESCCMP;
561 slot = bitmap_find_free_region(priv->used_rxkeys,
562 priv->rx_keycache_size, 0);
566 * The device supports the chosen algorithm, but the
567 * firmware does not provide enough key slots to store
569 * But encryption offload for outgoing frames is always
570 * possible, so we just pretend that the upload was
571 * successful and do the decryption in software.
574 /* mark the key as invalid. */
575 key->hw_key_idx = 0xff;
579 slot = key->hw_key_idx;
582 /* This key was not uploaded into the rx key cache. */
587 bitmap_release_region(priv->used_rxkeys, slot, 0);
594 ret = p54_upload_key(priv, algo, slot, key->keyidx,
595 key->keylen, addr, key->key);
597 bitmap_release_region(priv->used_rxkeys, slot, 0);
602 key->hw_key_idx = slot;
605 mutex_unlock(&priv->conf_mutex);
609 static int p54_get_survey(struct ieee80211_hw *dev, int idx,
610 struct survey_info *survey)
612 struct p54_common *priv = dev->priv;
613 struct ieee80211_channel *chan;
617 if (idx >= priv->chan_num)
621 for (tries = 0; tries < MAX_TRIES; tries++) {
622 chan = priv->curchan;
623 if (chan && chan->hw_value == idx) {
624 mutex_lock(&priv->conf_mutex);
625 err = p54_wait_for_stats(dev);
626 mutex_unlock(&priv->conf_mutex);
633 memcpy(survey, &priv->survey[idx], sizeof(*survey));
636 /* test if the reported statistics are valid. */
637 if (survey->channel_time != 0) {
638 survey->filled |= SURVEY_INFO_IN_USE;
641 * hw/fw has not accumulated enough sample sets.
642 * Wait for 100ms, this ought to be enough to
643 * to get at least one non-null set of channel
656 static unsigned int p54_flush_count(struct p54_common *priv)
658 unsigned int total = 0, i;
660 BUILD_BUG_ON(P54_QUEUE_NUM > ARRAY_SIZE(priv->tx_stats));
663 * Because the firmware has the sole control over any frames
664 * in the P54_QUEUE_BEACON or P54_QUEUE_SCAN queues, they
665 * don't really count as pending or active.
667 for (i = P54_QUEUE_MGMT; i < P54_QUEUE_NUM; i++)
668 total += priv->tx_stats[i].len;
672 static void p54_flush(struct ieee80211_hw *dev, u32 queues, bool drop)
674 struct p54_common *priv = dev->priv;
675 unsigned int total, i;
678 * Currently, it wouldn't really matter if we wait for one second
679 * or 15 minutes. But once someone gets around and completes the
680 * TODOs [ancel stuck frames / reset device] in p54_work, it will
681 * suddenly make sense to wait that long.
683 i = P54_STATISTICS_UPDATE * 2 / 20;
686 * In this case no locking is required because as we speak the
687 * queues have already been stopped and no new frames can sneak
690 while ((total = p54_flush_count(priv) && i--)) {
695 WARN(total, "tx flush timeout, unresponsive firmware");
698 static void p54_set_coverage_class(struct ieee80211_hw *dev, u8 coverage_class)
700 struct p54_common *priv = dev->priv;
702 mutex_lock(&priv->conf_mutex);
703 /* support all coverage class values as in 802.11-2007 Table 7-27 */
704 priv->coverage_class = clamp_t(u8, coverage_class, 0, 31);
706 mutex_unlock(&priv->conf_mutex);
709 static const struct ieee80211_ops p54_ops = {
713 .add_interface = p54_add_interface,
714 .remove_interface = p54_remove_interface,
715 .set_tim = p54_set_tim,
716 .sta_notify = p54_sta_notify,
717 .sta_add = p54_sta_add_remove,
718 .sta_remove = p54_sta_add_remove,
719 .set_key = p54_set_key,
720 .config = p54_config,
722 .bss_info_changed = p54_bss_info_changed,
723 .prepare_multicast = p54_prepare_multicast,
724 .configure_filter = p54_configure_filter,
725 .conf_tx = p54_conf_tx,
726 .get_stats = p54_get_stats,
727 .get_survey = p54_get_survey,
728 .set_coverage_class = p54_set_coverage_class,
731 struct ieee80211_hw *p54_init_common(size_t priv_data_len)
733 struct ieee80211_hw *dev;
734 struct p54_common *priv;
736 dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
742 priv->mode = NL80211_IFTYPE_UNSPECIFIED;
743 priv->basic_rate_mask = 0x15f;
744 spin_lock_init(&priv->tx_stats_lock);
745 skb_queue_head_init(&priv->tx_queue);
746 skb_queue_head_init(&priv->tx_pending);
747 dev->flags = IEEE80211_HW_RX_INCLUDES_FCS |
748 IEEE80211_HW_SIGNAL_DBM |
749 IEEE80211_HW_SUPPORTS_PS |
750 IEEE80211_HW_PS_NULLFUNC_STACK |
751 IEEE80211_HW_MFP_CAPABLE |
752 IEEE80211_HW_REPORTS_TX_ACK_STATUS;
754 dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
755 BIT(NL80211_IFTYPE_ADHOC) |
756 BIT(NL80211_IFTYPE_AP) |
757 BIT(NL80211_IFTYPE_MESH_POINT);
759 priv->beacon_req_id = cpu_to_le32(0);
760 priv->tx_stats[P54_QUEUE_BEACON].limit = 1;
761 priv->tx_stats[P54_QUEUE_FWSCAN].limit = 1;
762 priv->tx_stats[P54_QUEUE_MGMT].limit = 3;
763 priv->tx_stats[P54_QUEUE_CAB].limit = 3;
764 priv->tx_stats[P54_QUEUE_DATA].limit = 5;
768 * We support at most 8 tries no matter which rate they're at,
769 * we cannot support max_rates * max_rate_tries as we set it
770 * here, but setting it correctly to 4/2 or so would limit us
771 * artificially if the RC algorithm wants just two rates, so
772 * let's say 4/7, we'll redistribute it at TX time, see the
776 dev->max_rate_tries = 7;
777 dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
778 sizeof(struct p54_tx_data);
781 * For now, disable PS by default because it affects
782 * link stability significantly.
784 dev->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
786 mutex_init(&priv->conf_mutex);
787 mutex_init(&priv->eeprom_mutex);
788 init_completion(&priv->stat_comp);
789 init_completion(&priv->eeprom_comp);
790 init_completion(&priv->beacon_comp);
791 INIT_DELAYED_WORK(&priv->work, p54_work);
793 memset(&priv->mc_maclist[0], ~0, ETH_ALEN);
794 priv->curchan = NULL;
795 p54_reset_stats(priv);
798 EXPORT_SYMBOL_GPL(p54_init_common);
800 int p54_register_common(struct ieee80211_hw *dev, struct device *pdev)
802 struct p54_common __maybe_unused *priv = dev->priv;
805 err = ieee80211_register_hw(dev);
807 dev_err(pdev, "Cannot register device (%d).\n", err);
810 priv->registered = true;
812 #ifdef CONFIG_P54_LEDS
813 err = p54_init_leds(priv);
815 p54_unregister_common(dev);
818 #endif /* CONFIG_P54_LEDS */
820 dev_info(pdev, "is registered as '%s'\n", wiphy_name(dev->wiphy));
823 EXPORT_SYMBOL_GPL(p54_register_common);
825 void p54_free_common(struct ieee80211_hw *dev)
827 struct p54_common *priv = dev->priv;
830 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
831 kfree(priv->band_table[i]);
833 kfree(priv->iq_autocal);
834 kfree(priv->output_limit);
835 kfree(priv->curve_data);
836 kfree(priv->rssi_db);
837 kfree(priv->used_rxkeys);
839 priv->iq_autocal = NULL;
840 priv->output_limit = NULL;
841 priv->curve_data = NULL;
842 priv->rssi_db = NULL;
843 priv->used_rxkeys = NULL;
845 ieee80211_free_hw(dev);
847 EXPORT_SYMBOL_GPL(p54_free_common);
849 void p54_unregister_common(struct ieee80211_hw *dev)
851 struct p54_common *priv = dev->priv;
853 #ifdef CONFIG_P54_LEDS
854 p54_unregister_leds(priv);
855 #endif /* CONFIG_P54_LEDS */
857 if (priv->registered) {
858 priv->registered = false;
859 ieee80211_unregister_hw(dev);
862 mutex_destroy(&priv->conf_mutex);
863 mutex_destroy(&priv->eeprom_mutex);
865 EXPORT_SYMBOL_GPL(p54_unregister_common);