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/init.h>
20 #include <linux/slab.h>
21 #include <linux/firmware.h>
22 #include <linux/etherdevice.h>
24 #include <net/mac80211.h>
29 static int 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 sk_buff *beacon;
144 beacon = ieee80211_beacon_get(priv->hw, vif);
147 ret = p54_beacon_format_ie_tim(beacon);
152 * During operation, the firmware takes care of beaconing.
153 * The driver only needs to upload a new beacon template, once
154 * the template was changed by the stack or userspace.
156 * LMAC API 3.2.2 also specifies that the driver does not need
157 * to cancel the old beacon template by hand, instead the firmware
158 * will release the previous one through the feedback mechanism.
160 WARN_ON(p54_tx_80211(priv->hw, beacon));
161 priv->tsf_high32 = 0;
167 static int p54_start(struct ieee80211_hw *dev)
169 struct p54_common *priv = dev->priv;
172 mutex_lock(&priv->conf_mutex);
173 err = priv->open(dev);
176 P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
177 P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
178 P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
179 P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
180 err = p54_set_edcf(priv);
184 memset(priv->bssid, ~0, ETH_ALEN);
185 priv->mode = NL80211_IFTYPE_MONITOR;
186 err = p54_setup_mac(priv);
188 priv->mode = NL80211_IFTYPE_UNSPECIFIED;
192 ieee80211_queue_delayed_work(dev, &priv->work, 0);
194 priv->softled_state = 0;
195 err = p54_set_leds(priv);
198 mutex_unlock(&priv->conf_mutex);
202 static void p54_stop(struct ieee80211_hw *dev)
204 struct p54_common *priv = dev->priv;
207 mutex_lock(&priv->conf_mutex);
208 priv->mode = NL80211_IFTYPE_UNSPECIFIED;
209 priv->softled_state = 0;
212 cancel_delayed_work_sync(&priv->work);
215 skb_queue_purge(&priv->tx_pending);
216 skb_queue_purge(&priv->tx_queue);
217 for (i = 0; i < P54_QUEUE_NUM; i++) {
218 priv->tx_stats[i].count = 0;
219 priv->tx_stats[i].len = 0;
222 priv->beacon_req_id = cpu_to_le32(0);
223 priv->tsf_high32 = priv->tsf_low32 = 0;
224 mutex_unlock(&priv->conf_mutex);
227 static int p54_add_interface(struct ieee80211_hw *dev,
228 struct ieee80211_vif *vif)
230 struct p54_common *priv = dev->priv;
232 mutex_lock(&priv->conf_mutex);
233 if (priv->mode != NL80211_IFTYPE_MONITOR) {
234 mutex_unlock(&priv->conf_mutex);
241 case NL80211_IFTYPE_STATION:
242 case NL80211_IFTYPE_ADHOC:
243 case NL80211_IFTYPE_AP:
244 case NL80211_IFTYPE_MESH_POINT:
245 priv->mode = vif->type;
248 mutex_unlock(&priv->conf_mutex);
252 memcpy(priv->mac_addr, vif->addr, ETH_ALEN);
254 mutex_unlock(&priv->conf_mutex);
258 static void p54_remove_interface(struct ieee80211_hw *dev,
259 struct ieee80211_vif *vif)
261 struct p54_common *priv = dev->priv;
263 mutex_lock(&priv->conf_mutex);
267 * LMAC API 3.2.2 states that any active beacon template must be
268 * canceled by the driver before attempting a mode transition.
270 if (le32_to_cpu(priv->beacon_req_id) != 0) {
271 p54_tx_cancel(priv, priv->beacon_req_id);
272 wait_for_completion_interruptible_timeout(&priv->beacon_comp, HZ);
274 priv->mode = NL80211_IFTYPE_MONITOR;
275 memset(priv->mac_addr, 0, ETH_ALEN);
276 memset(priv->bssid, 0, ETH_ALEN);
278 mutex_unlock(&priv->conf_mutex);
281 static int p54_config(struct ieee80211_hw *dev, u32 changed)
284 struct p54_common *priv = dev->priv;
285 struct ieee80211_conf *conf = &dev->conf;
287 mutex_lock(&priv->conf_mutex);
288 if (changed & IEEE80211_CONF_CHANGE_POWER)
289 priv->output_power = conf->power_level << 2;
290 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
291 ret = p54_scan(priv, P54_SCAN_EXIT, 0);
295 if (changed & IEEE80211_CONF_CHANGE_PS) {
296 ret = p54_set_ps(priv);
300 if (changed & IEEE80211_CONF_CHANGE_IDLE) {
301 ret = p54_setup_mac(priv);
307 mutex_unlock(&priv->conf_mutex);
311 static void p54_configure_filter(struct ieee80211_hw *dev,
312 unsigned int changed_flags,
313 unsigned int *total_flags,
316 struct p54_common *priv = dev->priv;
318 *total_flags &= FIF_PROMISC_IN_BSS |
321 priv->filter_flags = *total_flags;
323 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS))
327 static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
328 const struct ieee80211_tx_queue_params *params)
330 struct p54_common *priv = dev->priv;
333 mutex_lock(&priv->conf_mutex);
334 if (queue < dev->queues) {
335 P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
336 params->cw_min, params->cw_max, params->txop);
337 ret = p54_set_edcf(priv);
340 mutex_unlock(&priv->conf_mutex);
344 static void p54_work(struct work_struct *work)
346 struct p54_common *priv = container_of(work, struct p54_common,
349 if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
353 * TODO: walk through tx_queue and do the following tasks
354 * 1. initiate bursts.
355 * 2. cancel stuck frames / reset the device if necessary.
358 p54_fetch_statistics(priv);
361 static int p54_get_stats(struct ieee80211_hw *dev,
362 struct ieee80211_low_level_stats *stats)
364 struct p54_common *priv = dev->priv;
366 memcpy(stats, &priv->stats, sizeof(*stats));
370 static void p54_bss_info_changed(struct ieee80211_hw *dev,
371 struct ieee80211_vif *vif,
372 struct ieee80211_bss_conf *info,
375 struct p54_common *priv = dev->priv;
377 mutex_lock(&priv->conf_mutex);
378 if (changed & BSS_CHANGED_BSSID) {
379 memcpy(priv->bssid, info->bssid, ETH_ALEN);
383 if (changed & BSS_CHANGED_BEACON) {
384 p54_scan(priv, P54_SCAN_EXIT, 0);
386 p54_beacon_update(priv, vif);
390 if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_BEACON)) {
391 priv->use_short_slot = info->use_short_slot;
394 if (changed & BSS_CHANGED_BASIC_RATES) {
395 if (dev->conf.channel->band == IEEE80211_BAND_5GHZ)
396 priv->basic_rate_mask = (info->basic_rates << 4);
398 priv->basic_rate_mask = info->basic_rates;
400 if (priv->fw_var >= 0x500)
401 p54_scan(priv, P54_SCAN_EXIT, 0);
403 if (changed & BSS_CHANGED_ASSOC) {
405 priv->aid = info->aid;
406 priv->wakeup_timer = info->beacon_int *
407 info->dtim_period * 5;
410 priv->wakeup_timer = 500;
415 mutex_unlock(&priv->conf_mutex);
418 static int p54_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
419 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
420 struct ieee80211_key_conf *key)
422 struct p54_common *priv = dev->priv;
427 if (modparam_nohwcrypt)
430 mutex_lock(&priv->conf_mutex);
431 if (cmd == SET_KEY) {
434 if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL |
435 BR_DESC_PRIV_CAP_TKIP))) {
439 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
440 algo = P54_CRYPTO_TKIPMICHAEL;
443 if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP)) {
447 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
448 algo = P54_CRYPTO_WEP;
451 if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP)) {
455 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
456 algo = P54_CRYPTO_AESCCMP;
462 slot = bitmap_find_free_region(priv->used_rxkeys,
463 priv->rx_keycache_size, 0);
467 * The device supports the choosen algorithm, but the
468 * firmware does not provide enough key slots to store
470 * But encryption offload for outgoing frames is always
471 * possible, so we just pretend that the upload was
472 * successful and do the decryption in software.
475 /* mark the key as invalid. */
476 key->hw_key_idx = 0xff;
480 slot = key->hw_key_idx;
483 /* This key was not uploaded into the rx key cache. */
488 bitmap_release_region(priv->used_rxkeys, slot, 0);
495 ret = p54_upload_key(priv, algo, slot, key->keyidx,
496 key->keylen, addr, key->key);
498 bitmap_release_region(priv->used_rxkeys, slot, 0);
503 key->hw_key_idx = slot;
506 mutex_unlock(&priv->conf_mutex);
510 static const struct ieee80211_ops p54_ops = {
514 .add_interface = p54_add_interface,
515 .remove_interface = p54_remove_interface,
516 .set_tim = p54_set_tim,
517 .sta_notify = p54_sta_notify,
518 .sta_add = p54_sta_add_remove,
519 .sta_remove = p54_sta_add_remove,
520 .set_key = p54_set_key,
521 .config = p54_config,
522 .bss_info_changed = p54_bss_info_changed,
523 .configure_filter = p54_configure_filter,
524 .conf_tx = p54_conf_tx,
525 .get_stats = p54_get_stats,
528 struct ieee80211_hw *p54_init_common(size_t priv_data_len)
530 struct ieee80211_hw *dev;
531 struct p54_common *priv;
533 dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
539 priv->mode = NL80211_IFTYPE_UNSPECIFIED;
540 priv->basic_rate_mask = 0x15f;
541 spin_lock_init(&priv->tx_stats_lock);
542 skb_queue_head_init(&priv->tx_queue);
543 skb_queue_head_init(&priv->tx_pending);
544 dev->flags = IEEE80211_HW_RX_INCLUDES_FCS |
545 IEEE80211_HW_SIGNAL_DBM |
546 IEEE80211_HW_SUPPORTS_PS |
547 IEEE80211_HW_PS_NULLFUNC_STACK |
548 IEEE80211_HW_BEACON_FILTER |
549 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
550 IEEE80211_HW_NOISE_DBM;
552 dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
553 BIT(NL80211_IFTYPE_ADHOC) |
554 BIT(NL80211_IFTYPE_AP) |
555 BIT(NL80211_IFTYPE_MESH_POINT);
557 dev->channel_change_time = 1000; /* TODO: find actual value */
558 priv->beacon_req_id = cpu_to_le32(0);
559 priv->tx_stats[P54_QUEUE_BEACON].limit = 1;
560 priv->tx_stats[P54_QUEUE_FWSCAN].limit = 1;
561 priv->tx_stats[P54_QUEUE_MGMT].limit = 3;
562 priv->tx_stats[P54_QUEUE_CAB].limit = 3;
563 priv->tx_stats[P54_QUEUE_DATA].limit = 5;
567 * We support at most 8 tries no matter which rate they're at,
568 * we cannot support max_rates * max_rate_tries as we set it
569 * here, but setting it correctly to 4/2 or so would limit us
570 * artificially if the RC algorithm wants just two rates, so
571 * let's say 4/7, we'll redistribute it at TX time, see the
575 dev->max_rate_tries = 7;
576 dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
577 sizeof(struct p54_tx_data);
580 * For now, disable PS by default because it affects
581 * link stability significantly.
583 dev->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
585 mutex_init(&priv->conf_mutex);
586 mutex_init(&priv->eeprom_mutex);
587 init_completion(&priv->eeprom_comp);
588 init_completion(&priv->beacon_comp);
589 INIT_DELAYED_WORK(&priv->work, p54_work);
593 EXPORT_SYMBOL_GPL(p54_init_common);
595 int p54_register_common(struct ieee80211_hw *dev, struct device *pdev)
597 struct p54_common *priv = dev->priv;
600 err = ieee80211_register_hw(dev);
602 dev_err(pdev, "Cannot register device (%d).\n", err);
606 #ifdef CONFIG_P54_LEDS
607 err = p54_init_leds(priv);
610 #endif /* CONFIG_P54_LEDS */
612 dev_info(pdev, "is registered as '%s'\n", wiphy_name(dev->wiphy));
615 EXPORT_SYMBOL_GPL(p54_register_common);
617 void p54_free_common(struct ieee80211_hw *dev)
619 struct p54_common *priv = dev->priv;
622 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
623 kfree(priv->band_table[i]);
625 kfree(priv->iq_autocal);
626 kfree(priv->output_limit);
627 kfree(priv->curve_data);
628 kfree(priv->used_rxkeys);
629 priv->iq_autocal = NULL;
630 priv->output_limit = NULL;
631 priv->curve_data = NULL;
632 priv->used_rxkeys = NULL;
633 ieee80211_free_hw(dev);
635 EXPORT_SYMBOL_GPL(p54_free_common);
637 void p54_unregister_common(struct ieee80211_hw *dev)
639 struct p54_common *priv = dev->priv;
641 #ifdef CONFIG_P54_LEDS
642 p54_unregister_leds(priv);
643 #endif /* CONFIG_P54_LEDS */
645 ieee80211_unregister_hw(dev);
646 mutex_destroy(&priv->conf_mutex);
647 mutex_destroy(&priv->eeprom_mutex);
649 EXPORT_SYMBOL_GPL(p54_unregister_common);