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/firmware.h>
21 #include <linux/etherdevice.h>
23 #include <net/mac80211.h>
28 static int modparam_nohwcrypt;
29 module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
30 MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
31 MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
32 MODULE_DESCRIPTION("Softmac Prism54 common code");
33 MODULE_LICENSE("GPL");
34 MODULE_ALIAS("prism54common");
36 static void p54_sta_notify(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
37 enum sta_notify_cmd notify_cmd,
38 struct ieee80211_sta *sta)
40 struct p54_common *priv = dev->priv;
43 case STA_NOTIFY_REMOVE:
45 * Notify the firmware that we don't want or we don't
46 * need to buffer frames for this station anymore.
49 p54_sta_unlock(priv, sta->addr);
51 case STA_NOTIFY_AWAKE:
52 /* update the firmware's filter table */
53 p54_sta_unlock(priv, sta->addr);
60 static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
63 struct p54_common *priv = dev->priv;
65 return p54_update_beacon_tim(priv, sta->aid, set);
68 static int p54_beacon_format_ie_tim(struct sk_buff *skb)
71 * the good excuse for this mess is ... the firmware.
72 * The dummy TIM MUST be at the end of the beacon frame,
73 * because it'll be overwritten!
76 struct ieee80211_mgmt *mgmt = (void *)skb->data;
79 if (skb->len <= sizeof(mgmt))
82 pos = (u8 *)mgmt->u.beacon.variable;
83 end = skb->data + skb->len;
85 if (pos + 2 + pos[1] > end)
88 if (pos[0] == WLAN_EID_TIM) {
90 u8 dtim_period = pos[3];
91 u8 *next = pos + 2 + dtim_len;
96 memmove(pos, next, end - next);
99 skb_trim(skb, skb->len - (dtim_len - 3));
101 pos = end - (dtim_len + 2);
103 /* add the dummy at the end */
104 pos[0] = WLAN_EID_TIM;
107 pos[3] = dtim_period;
116 static int p54_beacon_update(struct p54_common *priv,
117 struct ieee80211_vif *vif)
119 struct sk_buff *beacon;
120 __le32 old_beacon_req_id;
123 beacon = ieee80211_beacon_get(priv->hw, vif);
126 ret = p54_beacon_format_ie_tim(beacon);
130 old_beacon_req_id = priv->beacon_req_id;
131 priv->beacon_req_id = GET_REQ_ID(beacon);
133 ret = p54_tx_80211(priv->hw, beacon);
135 priv->beacon_req_id = old_beacon_req_id;
139 priv->tsf_high32 = 0;
145 static int p54_start(struct ieee80211_hw *dev)
147 struct p54_common *priv = dev->priv;
150 mutex_lock(&priv->conf_mutex);
151 err = priv->open(dev);
154 P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
155 P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
156 P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
157 P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
158 err = p54_set_edcf(priv);
162 memset(priv->bssid, ~0, ETH_ALEN);
163 priv->mode = NL80211_IFTYPE_MONITOR;
164 err = p54_setup_mac(priv);
166 priv->mode = NL80211_IFTYPE_UNSPECIFIED;
170 queue_delayed_work(dev->workqueue, &priv->work, 0);
172 priv->softled_state = 0;
173 err = p54_set_leds(priv);
176 mutex_unlock(&priv->conf_mutex);
180 static void p54_stop(struct ieee80211_hw *dev)
182 struct p54_common *priv = dev->priv;
185 mutex_lock(&priv->conf_mutex);
186 priv->mode = NL80211_IFTYPE_UNSPECIFIED;
187 priv->softled_state = 0;
190 cancel_delayed_work_sync(&priv->work);
193 skb_queue_purge(&priv->tx_pending);
194 skb_queue_purge(&priv->tx_queue);
195 for (i = 0; i < P54_QUEUE_NUM; i++) {
196 priv->tx_stats[i].count = 0;
197 priv->tx_stats[i].len = 0;
200 priv->beacon_req_id = cpu_to_le32(0);
201 priv->tsf_high32 = priv->tsf_low32 = 0;
202 mutex_unlock(&priv->conf_mutex);
205 static int p54_add_interface(struct ieee80211_hw *dev,
206 struct ieee80211_if_init_conf *conf)
208 struct p54_common *priv = dev->priv;
210 mutex_lock(&priv->conf_mutex);
211 if (priv->mode != NL80211_IFTYPE_MONITOR) {
212 mutex_unlock(&priv->conf_mutex);
216 priv->vif = conf->vif;
218 switch (conf->type) {
219 case NL80211_IFTYPE_STATION:
220 case NL80211_IFTYPE_ADHOC:
221 case NL80211_IFTYPE_AP:
222 case NL80211_IFTYPE_MESH_POINT:
223 priv->mode = conf->type;
226 mutex_unlock(&priv->conf_mutex);
230 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
232 mutex_unlock(&priv->conf_mutex);
236 static void p54_remove_interface(struct ieee80211_hw *dev,
237 struct ieee80211_if_init_conf *conf)
239 struct p54_common *priv = dev->priv;
241 mutex_lock(&priv->conf_mutex);
243 if (priv->beacon_req_id) {
244 p54_tx_cancel(priv, priv->beacon_req_id);
245 priv->beacon_req_id = cpu_to_le32(0);
247 priv->mode = NL80211_IFTYPE_MONITOR;
248 memset(priv->mac_addr, 0, ETH_ALEN);
249 memset(priv->bssid, 0, ETH_ALEN);
251 mutex_unlock(&priv->conf_mutex);
254 static int p54_config(struct ieee80211_hw *dev, u32 changed)
257 struct p54_common *priv = dev->priv;
258 struct ieee80211_conf *conf = &dev->conf;
260 mutex_lock(&priv->conf_mutex);
261 if (changed & IEEE80211_CONF_CHANGE_POWER)
262 priv->output_power = conf->power_level << 2;
263 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
264 ret = p54_scan(priv, P54_SCAN_EXIT, 0);
268 if (changed & IEEE80211_CONF_CHANGE_PS) {
269 ret = p54_set_ps(priv);
275 mutex_unlock(&priv->conf_mutex);
279 static void p54_configure_filter(struct ieee80211_hw *dev,
280 unsigned int changed_flags,
281 unsigned int *total_flags,
282 int mc_count, struct dev_mc_list *mclist)
284 struct p54_common *priv = dev->priv;
286 *total_flags &= FIF_PROMISC_IN_BSS |
289 priv->filter_flags = *total_flags;
291 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS))
295 static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
296 const struct ieee80211_tx_queue_params *params)
298 struct p54_common *priv = dev->priv;
301 mutex_lock(&priv->conf_mutex);
302 if ((params) && !(queue > 4)) {
303 P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
304 params->cw_min, params->cw_max, params->txop);
305 ret = p54_set_edcf(priv);
308 mutex_unlock(&priv->conf_mutex);
312 static void p54_work(struct work_struct *work)
314 struct p54_common *priv = container_of(work, struct p54_common,
317 if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
321 * TODO: walk through tx_queue and do the following tasks
322 * 1. initiate bursts.
323 * 2. cancel stuck frames / reset the device if necessary.
326 p54_fetch_statistics(priv);
329 static int p54_get_stats(struct ieee80211_hw *dev,
330 struct ieee80211_low_level_stats *stats)
332 struct p54_common *priv = dev->priv;
334 memcpy(stats, &priv->stats, sizeof(*stats));
338 static int p54_get_tx_stats(struct ieee80211_hw *dev,
339 struct ieee80211_tx_queue_stats *stats)
341 struct p54_common *priv = dev->priv;
343 memcpy(stats, &priv->tx_stats[P54_QUEUE_DATA],
344 sizeof(stats[0]) * dev->queues);
348 static void p54_bss_info_changed(struct ieee80211_hw *dev,
349 struct ieee80211_vif *vif,
350 struct ieee80211_bss_conf *info,
353 struct p54_common *priv = dev->priv;
355 mutex_lock(&priv->conf_mutex);
356 if (changed & BSS_CHANGED_BSSID) {
357 memcpy(priv->bssid, info->bssid, ETH_ALEN);
361 if (changed & BSS_CHANGED_BEACON) {
362 p54_scan(priv, P54_SCAN_EXIT, 0);
364 p54_beacon_update(priv, vif);
368 if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_BEACON)) {
369 priv->use_short_slot = info->use_short_slot;
372 if (changed & BSS_CHANGED_BASIC_RATES) {
373 if (dev->conf.channel->band == IEEE80211_BAND_5GHZ)
374 priv->basic_rate_mask = (info->basic_rates << 4);
376 priv->basic_rate_mask = info->basic_rates;
378 if (priv->fw_var >= 0x500)
379 p54_scan(priv, P54_SCAN_EXIT, 0);
381 if (changed & BSS_CHANGED_ASSOC) {
383 priv->aid = info->aid;
384 priv->wakeup_timer = info->beacon_int *
385 info->dtim_period * 5;
390 mutex_unlock(&priv->conf_mutex);
393 static int p54_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
394 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
395 struct ieee80211_key_conf *key)
397 struct p54_common *priv = dev->priv;
402 if (modparam_nohwcrypt)
405 mutex_lock(&priv->conf_mutex);
406 if (cmd == SET_KEY) {
409 if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL |
410 BR_DESC_PRIV_CAP_TKIP))) {
414 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
415 algo = P54_CRYPTO_TKIPMICHAEL;
418 if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP)) {
422 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
423 algo = P54_CRYPTO_WEP;
426 if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP)) {
430 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
431 algo = P54_CRYPTO_AESCCMP;
437 slot = bitmap_find_free_region(priv->used_rxkeys,
438 priv->rx_keycache_size, 0);
442 * The device supports the choosen algorithm, but the
443 * firmware does not provide enough key slots to store
445 * But encryption offload for outgoing frames is always
446 * possible, so we just pretend that the upload was
447 * successful and do the decryption in software.
450 /* mark the key as invalid. */
451 key->hw_key_idx = 0xff;
455 slot = key->hw_key_idx;
458 /* This key was not uploaded into the rx key cache. */
463 bitmap_release_region(priv->used_rxkeys, slot, 0);
470 ret = p54_upload_key(priv, algo, slot, key->keyidx,
471 key->keylen, addr, key->key);
473 bitmap_release_region(priv->used_rxkeys, slot, 0);
478 key->hw_key_idx = slot;
481 mutex_unlock(&priv->conf_mutex);
485 static const struct ieee80211_ops p54_ops = {
489 .add_interface = p54_add_interface,
490 .remove_interface = p54_remove_interface,
491 .set_tim = p54_set_tim,
492 .sta_notify = p54_sta_notify,
493 .set_key = p54_set_key,
494 .config = p54_config,
495 .bss_info_changed = p54_bss_info_changed,
496 .configure_filter = p54_configure_filter,
497 .conf_tx = p54_conf_tx,
498 .get_stats = p54_get_stats,
499 .get_tx_stats = p54_get_tx_stats
502 struct ieee80211_hw *p54_init_common(size_t priv_data_len)
504 struct ieee80211_hw *dev;
505 struct p54_common *priv;
507 dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
513 priv->mode = NL80211_IFTYPE_UNSPECIFIED;
514 priv->basic_rate_mask = 0x15f;
515 spin_lock_init(&priv->tx_stats_lock);
516 skb_queue_head_init(&priv->tx_queue);
517 skb_queue_head_init(&priv->tx_pending);
518 dev->flags = IEEE80211_HW_RX_INCLUDES_FCS |
519 IEEE80211_HW_SIGNAL_DBM |
520 IEEE80211_HW_NOISE_DBM;
522 dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
523 BIT(NL80211_IFTYPE_ADHOC) |
524 BIT(NL80211_IFTYPE_AP) |
525 BIT(NL80211_IFTYPE_MESH_POINT);
527 dev->channel_change_time = 1000; /* TODO: find actual value */
528 priv->tx_stats[P54_QUEUE_BEACON].limit = 1;
529 priv->tx_stats[P54_QUEUE_FWSCAN].limit = 1;
530 priv->tx_stats[P54_QUEUE_MGMT].limit = 3;
531 priv->tx_stats[P54_QUEUE_CAB].limit = 3;
532 priv->tx_stats[P54_QUEUE_DATA].limit = 5;
536 * We support at most 8 tries no matter which rate they're at,
537 * we cannot support max_rates * max_rate_tries as we set it
538 * here, but setting it correctly to 4/2 or so would limit us
539 * artificially if the RC algorithm wants just two rates, so
540 * let's say 4/7, we'll redistribute it at TX time, see the
544 dev->max_rate_tries = 7;
545 dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
546 sizeof(struct p54_tx_data);
548 mutex_init(&priv->conf_mutex);
549 mutex_init(&priv->eeprom_mutex);
550 init_completion(&priv->eeprom_comp);
551 INIT_DELAYED_WORK(&priv->work, p54_work);
555 EXPORT_SYMBOL_GPL(p54_init_common);
557 int p54_register_common(struct ieee80211_hw *dev, struct device *pdev)
559 struct p54_common *priv = dev->priv;
562 err = ieee80211_register_hw(dev);
564 dev_err(pdev, "Cannot register device (%d).\n", err);
568 #ifdef CONFIG_P54_LEDS
569 err = p54_init_leds(priv);
572 #endif /* CONFIG_P54_LEDS */
574 dev_info(pdev, "is registered as '%s'\n", wiphy_name(dev->wiphy));
577 EXPORT_SYMBOL_GPL(p54_register_common);
579 void p54_free_common(struct ieee80211_hw *dev)
581 struct p54_common *priv = dev->priv;
583 kfree(priv->iq_autocal);
584 kfree(priv->output_limit);
585 kfree(priv->curve_data);
586 kfree(priv->used_rxkeys);
587 priv->iq_autocal = NULL;
588 priv->output_limit = NULL;
589 priv->curve_data = NULL;
590 priv->used_rxkeys = NULL;
591 ieee80211_free_hw(dev);
593 EXPORT_SYMBOL_GPL(p54_free_common);
595 void p54_unregister_common(struct ieee80211_hw *dev)
597 struct p54_common *priv = dev->priv;
599 #ifdef CONFIG_P54_LEDS
600 p54_unregister_leds(priv);
601 #endif /* CONFIG_P54_LEDS */
603 ieee80211_unregister_hw(dev);
604 mutex_destroy(&priv->conf_mutex);
605 mutex_destroy(&priv->eeprom_mutex);
607 EXPORT_SYMBOL_GPL(p54_unregister_common);