blob: 726a9343f514cb9517f3836617b4354ec30d31fa [file] [log] [blame]
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -05001/*
2 * mac80211 glue code for mac80211 Prism54 drivers
3 *
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>
7 *
8 * Based on:
9 * - the islsm (softmac prism54) driver, which is:
10 * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
11 * - stlc45xx driver
12 * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
13 *
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.
17 */
18
19#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -050021#include <linux/firmware.h>
22#include <linux/etherdevice.h>
23
24#include <net/mac80211.h>
25
26#include "p54.h"
27#include "lmac.h"
28
29static int modparam_nohwcrypt;
30module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
31MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
32MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
33MODULE_DESCRIPTION("Softmac Prism54 common code");
34MODULE_LICENSE("GPL");
35MODULE_ALIAS("prism54common");
36
Johannes Berg17f6f052010-02-19 19:06:54 +010037static int p54_sta_add_remove(struct ieee80211_hw *hw,
38 struct ieee80211_vif *vif,
39 struct ieee80211_sta *sta)
40{
41 struct p54_common *priv = hw->priv;
42
43 /*
44 * Notify the firmware that we don't want or we don't
45 * need to buffer frames for this station anymore.
46 */
47
48 p54_sta_unlock(priv, sta->addr);
49
50 return 0;
51}
52
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -050053static void p54_sta_notify(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
54 enum sta_notify_cmd notify_cmd,
55 struct ieee80211_sta *sta)
56{
57 struct p54_common *priv = dev->priv;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -050058
Johannes Berg17f6f052010-02-19 19:06:54 +010059 switch (notify_cmd) {
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -050060 case STA_NOTIFY_AWAKE:
61 /* update the firmware's filter table */
62 p54_sta_unlock(priv, sta->addr);
63 break;
64 default:
65 break;
66 }
67}
68
69static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
70 bool set)
71{
72 struct p54_common *priv = dev->priv;
73
74 return p54_update_beacon_tim(priv, sta->aid, set);
75}
76
Christian Lampartere0f114e2009-07-07 19:08:07 +020077u8 *p54_find_ie(struct sk_buff *skb, u8 ie)
78{
79 struct ieee80211_mgmt *mgmt = (void *)skb->data;
80 u8 *pos, *end;
81
82 if (skb->len <= sizeof(mgmt))
83 return NULL;
84
85 pos = (u8 *)mgmt->u.beacon.variable;
86 end = skb->data + skb->len;
87 while (pos < end) {
88 if (pos + 2 + pos[1] > end)
89 return NULL;
90
91 if (pos[0] == ie)
92 return pos;
93
94 pos += 2 + pos[1];
95 }
96 return NULL;
97}
98
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -050099static int p54_beacon_format_ie_tim(struct sk_buff *skb)
100{
101 /*
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!
105 */
Christian Lampartere0f114e2009-07-07 19:08:07 +0200106 u8 *tim;
107 u8 dtim_len;
108 u8 dtim_period;
109 u8 *next;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500110
Christian Lampartere0f114e2009-07-07 19:08:07 +0200111 tim = p54_find_ie(skb, WLAN_EID_TIM);
112 if (!tim)
113 return 0;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500114
Christian Lampartere0f114e2009-07-07 19:08:07 +0200115 dtim_len = tim[1];
116 dtim_period = tim[3];
117 next = tim + 2 + dtim_len;
118
119 if (dtim_len < 3)
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500120 return -EINVAL;
121
Christian Lampartere0f114e2009-07-07 19:08:07 +0200122 memmove(tim, next, skb_tail_pointer(skb) - next);
123 tim = skb_tail_pointer(skb) - (dtim_len + 2);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500124
Christian Lampartere0f114e2009-07-07 19:08:07 +0200125 /* add the dummy at the end */
126 tim[0] = WLAN_EID_TIM;
127 tim[1] = 3;
128 tim[2] = 0;
129 tim[3] = dtim_period;
130 tim[4] = 0;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500131
Christian Lampartere0f114e2009-07-07 19:08:07 +0200132 if (dtim_len > 3)
133 skb_trim(skb, skb->len - (dtim_len - 3));
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500134
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500135 return 0;
136}
137
138static int p54_beacon_update(struct p54_common *priv,
139 struct ieee80211_vif *vif)
140{
141 struct sk_buff *beacon;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500142 int ret;
143
144 beacon = ieee80211_beacon_get(priv->hw, vif);
145 if (!beacon)
146 return -ENOMEM;
147 ret = p54_beacon_format_ie_tim(beacon);
148 if (ret)
149 return ret;
150
Christian Lamparter46df10a2009-07-16 20:03:47 +0200151 /*
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.
155 *
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.
159 */
Johannes Berg7bb45682011-02-24 14:42:06 +0100160 p54_tx_80211(priv->hw, beacon);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500161 priv->tsf_high32 = 0;
162 priv->tsf_low32 = 0;
163
164 return 0;
165}
166
167static int p54_start(struct ieee80211_hw *dev)
168{
169 struct p54_common *priv = dev->priv;
170 int err;
171
172 mutex_lock(&priv->conf_mutex);
173 err = priv->open(dev);
174 if (err)
175 goto out;
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);
181 if (err)
182 goto out;
183
184 memset(priv->bssid, ~0, ETH_ALEN);
185 priv->mode = NL80211_IFTYPE_MONITOR;
186 err = p54_setup_mac(priv);
187 if (err) {
188 priv->mode = NL80211_IFTYPE_UNSPECIFIED;
189 goto out;
190 }
191
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400192 ieee80211_queue_delayed_work(dev, &priv->work, 0);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500193
194 priv->softled_state = 0;
195 err = p54_set_leds(priv);
196
197out:
198 mutex_unlock(&priv->conf_mutex);
199 return err;
200}
201
202static void p54_stop(struct ieee80211_hw *dev)
203{
204 struct p54_common *priv = dev->priv;
205 int i;
206
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500207 priv->mode = NL80211_IFTYPE_UNSPECIFIED;
208 priv->softled_state = 0;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500209 cancel_delayed_work_sync(&priv->work);
Christian Lamparter0d781562011-08-20 01:53:59 +0200210 mutex_lock(&priv->conf_mutex);
211 p54_set_leds(priv);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500212 priv->stop(dev);
213 skb_queue_purge(&priv->tx_pending);
214 skb_queue_purge(&priv->tx_queue);
215 for (i = 0; i < P54_QUEUE_NUM; i++) {
216 priv->tx_stats[i].count = 0;
217 priv->tx_stats[i].len = 0;
218 }
219
220 priv->beacon_req_id = cpu_to_le32(0);
221 priv->tsf_high32 = priv->tsf_low32 = 0;
222 mutex_unlock(&priv->conf_mutex);
223}
224
225static int p54_add_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100226 struct ieee80211_vif *vif)
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500227{
228 struct p54_common *priv = dev->priv;
229
230 mutex_lock(&priv->conf_mutex);
231 if (priv->mode != NL80211_IFTYPE_MONITOR) {
232 mutex_unlock(&priv->conf_mutex);
233 return -EOPNOTSUPP;
234 }
235
Johannes Berg1ed32e42009-12-23 13:15:45 +0100236 priv->vif = vif;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500237
Johannes Berg1ed32e42009-12-23 13:15:45 +0100238 switch (vif->type) {
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500239 case NL80211_IFTYPE_STATION:
240 case NL80211_IFTYPE_ADHOC:
241 case NL80211_IFTYPE_AP:
242 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg1ed32e42009-12-23 13:15:45 +0100243 priv->mode = vif->type;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500244 break;
245 default:
246 mutex_unlock(&priv->conf_mutex);
247 return -EOPNOTSUPP;
248 }
249
Johannes Berg1ed32e42009-12-23 13:15:45 +0100250 memcpy(priv->mac_addr, vif->addr, ETH_ALEN);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500251 p54_setup_mac(priv);
252 mutex_unlock(&priv->conf_mutex);
253 return 0;
254}
255
256static void p54_remove_interface(struct ieee80211_hw *dev,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100257 struct ieee80211_vif *vif)
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500258{
259 struct p54_common *priv = dev->priv;
260
261 mutex_lock(&priv->conf_mutex);
262 priv->vif = NULL;
Christian Lamparter46df10a2009-07-16 20:03:47 +0200263
264 /*
265 * LMAC API 3.2.2 states that any active beacon template must be
266 * canceled by the driver before attempting a mode transition.
267 */
268 if (le32_to_cpu(priv->beacon_req_id) != 0) {
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500269 p54_tx_cancel(priv, priv->beacon_req_id);
Christian Lamparter46df10a2009-07-16 20:03:47 +0200270 wait_for_completion_interruptible_timeout(&priv->beacon_comp, HZ);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500271 }
272 priv->mode = NL80211_IFTYPE_MONITOR;
273 memset(priv->mac_addr, 0, ETH_ALEN);
274 memset(priv->bssid, 0, ETH_ALEN);
275 p54_setup_mac(priv);
276 mutex_unlock(&priv->conf_mutex);
277}
278
Christian Lamparter0d781562011-08-20 01:53:59 +0200279static int p54_wait_for_stats(struct ieee80211_hw *dev)
280{
281 struct p54_common *priv = dev->priv;
282 int ret;
283
284 priv->update_stats = true;
285 ret = p54_fetch_statistics(priv);
286 if (ret)
287 return ret;
288
289 ret = wait_for_completion_interruptible_timeout(&priv->stat_comp, HZ);
290 if (ret == 0)
291 return -ETIMEDOUT;
292
293 return 0;
294}
295
296static void p54_reset_stats(struct p54_common *priv)
297{
298 struct ieee80211_channel *chan = priv->curchan;
299
300 if (chan) {
301 struct survey_info *info = &priv->survey[chan->hw_value];
302
303 /* only reset channel statistics, don't touch .filled, etc. */
304 info->channel_time = 0;
305 info->channel_time_busy = 0;
306 info->channel_time_tx = 0;
307 }
308
309 priv->update_stats = true;
310 priv->survey_raw.active = 0;
311 priv->survey_raw.cca = 0;
312 priv->survey_raw.tx = 0;
313}
314
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500315static int p54_config(struct ieee80211_hw *dev, u32 changed)
316{
317 int ret = 0;
318 struct p54_common *priv = dev->priv;
319 struct ieee80211_conf *conf = &dev->conf;
320
321 mutex_lock(&priv->conf_mutex);
322 if (changed & IEEE80211_CONF_CHANGE_POWER)
323 priv->output_power = conf->power_level << 2;
324 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
Christian Lamparter0d781562011-08-20 01:53:59 +0200325 struct ieee80211_channel *oldchan;
326 WARN_ON(p54_wait_for_stats(dev));
327 oldchan = priv->curchan;
328 priv->curchan = NULL;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500329 ret = p54_scan(priv, P54_SCAN_EXIT, 0);
Christian Lamparter0d781562011-08-20 01:53:59 +0200330 if (ret) {
331 priv->curchan = oldchan;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500332 goto out;
Christian Lamparter0d781562011-08-20 01:53:59 +0200333 }
334 /*
335 * TODO: Use the LM_SCAN_TRAP to determine the current
336 * operating channel.
337 */
338 priv->curchan = priv->hw->conf.channel;
339 p54_reset_stats(priv);
340 WARN_ON(p54_fetch_statistics(priv));
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500341 }
342 if (changed & IEEE80211_CONF_CHANGE_PS) {
Christian Lamparter0d781562011-08-20 01:53:59 +0200343 WARN_ON(p54_wait_for_stats(dev));
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500344 ret = p54_set_ps(priv);
345 if (ret)
346 goto out;
Christian Lamparter0d781562011-08-20 01:53:59 +0200347 WARN_ON(p54_wait_for_stats(dev));
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500348 }
Christian Lamparter6208f8b2009-08-07 19:39:05 +0200349 if (changed & IEEE80211_CONF_CHANGE_IDLE) {
Christian Lamparter0d781562011-08-20 01:53:59 +0200350 WARN_ON(p54_wait_for_stats(dev));
Christian Lamparter6208f8b2009-08-07 19:39:05 +0200351 ret = p54_setup_mac(priv);
352 if (ret)
353 goto out;
Christian Lamparter0d781562011-08-20 01:53:59 +0200354 WARN_ON(p54_wait_for_stats(dev));
Christian Lamparter6208f8b2009-08-07 19:39:05 +0200355 }
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500356
357out:
358 mutex_unlock(&priv->conf_mutex);
359 return ret;
360}
361
Christian Lamparterbe8d98e2011-04-24 17:22:59 +0200362static u64 p54_prepare_multicast(struct ieee80211_hw *dev,
363 struct netdev_hw_addr_list *mc_list)
364{
365 struct p54_common *priv = dev->priv;
366 struct netdev_hw_addr *ha;
367 int i;
368
369 BUILD_BUG_ON(ARRAY_SIZE(priv->mc_maclist) !=
370 ARRAY_SIZE(((struct p54_group_address_table *)NULL)->mac_list));
371 /*
372 * The first entry is reserved for the global broadcast MAC.
373 * Otherwise the firmware will drop it and ARP will no longer work.
374 */
375 i = 1;
376 priv->mc_maclist_num = netdev_hw_addr_list_count(mc_list) + i;
377 netdev_hw_addr_list_for_each(ha, mc_list) {
378 memcpy(&priv->mc_maclist[i], ha->addr, ETH_ALEN);
379 i++;
380 if (i >= ARRAY_SIZE(priv->mc_maclist))
381 break;
382 }
383
384 return 1; /* update */
385}
386
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500387static void p54_configure_filter(struct ieee80211_hw *dev,
388 unsigned int changed_flags,
389 unsigned int *total_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +0200390 u64 multicast)
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500391{
392 struct p54_common *priv = dev->priv;
393
394 *total_flags &= FIF_PROMISC_IN_BSS |
Christian Lamparterbe8d98e2011-04-24 17:22:59 +0200395 FIF_ALLMULTI |
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500396 FIF_OTHER_BSS;
397
398 priv->filter_flags = *total_flags;
399
400 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS))
401 p54_setup_mac(priv);
Christian Lamparterbe8d98e2011-04-24 17:22:59 +0200402
403 if (changed_flags & FIF_ALLMULTI || multicast)
404 p54_set_groupfilter(priv);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500405}
406
407static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
408 const struct ieee80211_tx_queue_params *params)
409{
410 struct p54_common *priv = dev->priv;
411 int ret;
412
413 mutex_lock(&priv->conf_mutex);
Christian Lamparter718126a2009-08-07 19:38:51 +0200414 if (queue < dev->queues) {
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500415 P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
416 params->cw_min, params->cw_max, params->txop);
417 ret = p54_set_edcf(priv);
418 } else
419 ret = -EINVAL;
420 mutex_unlock(&priv->conf_mutex);
421 return ret;
422}
423
424static void p54_work(struct work_struct *work)
425{
426 struct p54_common *priv = container_of(work, struct p54_common,
427 work.work);
428
429 if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
430 return ;
431
432 /*
433 * TODO: walk through tx_queue and do the following tasks
434 * 1. initiate bursts.
435 * 2. cancel stuck frames / reset the device if necessary.
436 */
437
Christian Lamparter0d781562011-08-20 01:53:59 +0200438 mutex_lock(&priv->conf_mutex);
439 WARN_ON_ONCE(p54_fetch_statistics(priv));
440 mutex_unlock(&priv->conf_mutex);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500441}
442
443static int p54_get_stats(struct ieee80211_hw *dev,
444 struct ieee80211_low_level_stats *stats)
445{
446 struct p54_common *priv = dev->priv;
447
448 memcpy(stats, &priv->stats, sizeof(*stats));
449 return 0;
450}
451
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500452static void p54_bss_info_changed(struct ieee80211_hw *dev,
453 struct ieee80211_vif *vif,
454 struct ieee80211_bss_conf *info,
455 u32 changed)
456{
457 struct p54_common *priv = dev->priv;
458
459 mutex_lock(&priv->conf_mutex);
460 if (changed & BSS_CHANGED_BSSID) {
461 memcpy(priv->bssid, info->bssid, ETH_ALEN);
462 p54_setup_mac(priv);
463 }
464
465 if (changed & BSS_CHANGED_BEACON) {
466 p54_scan(priv, P54_SCAN_EXIT, 0);
467 p54_setup_mac(priv);
468 p54_beacon_update(priv, vif);
469 p54_set_edcf(priv);
470 }
471
472 if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_BEACON)) {
473 priv->use_short_slot = info->use_short_slot;
474 p54_set_edcf(priv);
475 }
476 if (changed & BSS_CHANGED_BASIC_RATES) {
477 if (dev->conf.channel->band == IEEE80211_BAND_5GHZ)
478 priv->basic_rate_mask = (info->basic_rates << 4);
479 else
480 priv->basic_rate_mask = info->basic_rates;
481 p54_setup_mac(priv);
482 if (priv->fw_var >= 0x500)
483 p54_scan(priv, P54_SCAN_EXIT, 0);
484 }
485 if (changed & BSS_CHANGED_ASSOC) {
486 if (info->assoc) {
487 priv->aid = info->aid;
488 priv->wakeup_timer = info->beacon_int *
489 info->dtim_period * 5;
490 p54_setup_mac(priv);
Christian Lampartere0f114e2009-07-07 19:08:07 +0200491 } else {
492 priv->wakeup_timer = 500;
493 priv->aid = 0;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500494 }
495 }
496
497 mutex_unlock(&priv->conf_mutex);
498}
499
500static int p54_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
501 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
502 struct ieee80211_key_conf *key)
503{
504 struct p54_common *priv = dev->priv;
505 int slot, ret = 0;
506 u8 algo = 0;
507 u8 *addr = NULL;
508
509 if (modparam_nohwcrypt)
510 return -EOPNOTSUPP;
511
512 mutex_lock(&priv->conf_mutex);
513 if (cmd == SET_KEY) {
Johannes Berg97359d12010-08-10 09:46:38 +0200514 switch (key->cipher) {
515 case WLAN_CIPHER_SUITE_TKIP:
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500516 if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL |
517 BR_DESC_PRIV_CAP_TKIP))) {
518 ret = -EOPNOTSUPP;
519 goto out_unlock;
520 }
521 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
522 algo = P54_CRYPTO_TKIPMICHAEL;
523 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200524 case WLAN_CIPHER_SUITE_WEP40:
525 case WLAN_CIPHER_SUITE_WEP104:
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500526 if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP)) {
527 ret = -EOPNOTSUPP;
528 goto out_unlock;
529 }
530 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
531 algo = P54_CRYPTO_WEP;
532 break;
Johannes Berg97359d12010-08-10 09:46:38 +0200533 case WLAN_CIPHER_SUITE_CCMP:
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500534 if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP)) {
535 ret = -EOPNOTSUPP;
536 goto out_unlock;
537 }
538 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
539 algo = P54_CRYPTO_AESCCMP;
540 break;
541 default:
542 ret = -EOPNOTSUPP;
543 goto out_unlock;
544 }
545 slot = bitmap_find_free_region(priv->used_rxkeys,
546 priv->rx_keycache_size, 0);
547
548 if (slot < 0) {
549 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300550 * The device supports the chosen algorithm, but the
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500551 * firmware does not provide enough key slots to store
552 * all of them.
553 * But encryption offload for outgoing frames is always
554 * possible, so we just pretend that the upload was
555 * successful and do the decryption in software.
556 */
557
558 /* mark the key as invalid. */
559 key->hw_key_idx = 0xff;
560 goto out_unlock;
561 }
562 } else {
563 slot = key->hw_key_idx;
564
565 if (slot == 0xff) {
566 /* This key was not uploaded into the rx key cache. */
567
568 goto out_unlock;
569 }
570
571 bitmap_release_region(priv->used_rxkeys, slot, 0);
572 algo = 0;
573 }
574
575 if (sta)
576 addr = sta->addr;
577
578 ret = p54_upload_key(priv, algo, slot, key->keyidx,
579 key->keylen, addr, key->key);
580 if (ret) {
581 bitmap_release_region(priv->used_rxkeys, slot, 0);
582 ret = -EOPNOTSUPP;
583 goto out_unlock;
584 }
585
586 key->hw_key_idx = slot;
587
588out_unlock:
589 mutex_unlock(&priv->conf_mutex);
590 return ret;
591}
592
John W. Linville1b2fb7d2010-05-03 16:06:47 -0400593static int p54_get_survey(struct ieee80211_hw *dev, int idx,
594 struct survey_info *survey)
595{
596 struct p54_common *priv = dev->priv;
Christian Lamparter0d781562011-08-20 01:53:59 +0200597 struct ieee80211_channel *chan;
598 int err, tries;
599 bool in_use = false;
John W. Linville1b2fb7d2010-05-03 16:06:47 -0400600
Christian Lamparter0d781562011-08-20 01:53:59 +0200601 if (idx >= priv->chan_num)
John W. Linville1b2fb7d2010-05-03 16:06:47 -0400602 return -ENOENT;
603
Christian Lamparter0d781562011-08-20 01:53:59 +0200604#define MAX_TRIES 1
605 for (tries = 0; tries < MAX_TRIES; tries++) {
606 chan = priv->curchan;
607 if (chan && chan->hw_value == idx) {
608 mutex_lock(&priv->conf_mutex);
609 err = p54_wait_for_stats(dev);
610 mutex_unlock(&priv->conf_mutex);
611 if (err)
612 return err;
John W. Linville1b2fb7d2010-05-03 16:06:47 -0400613
Christian Lamparter0d781562011-08-20 01:53:59 +0200614 in_use = true;
615 }
616
617 memcpy(survey, &priv->survey[idx], sizeof(*survey));
618
619 if (in_use) {
620 /* test if the reported statistics are valid. */
621 if (survey->channel_time != 0) {
622 survey->filled |= SURVEY_INFO_IN_USE;
623 } else {
624 /*
625 * hw/fw has not accumulated enough sample sets.
626 * Wait for 100ms, this ought to be enough to
627 * to get at least one non-null set of channel
628 * usage statistics.
629 */
630 msleep(100);
631 continue;
632 }
633 }
634 return 0;
635 }
636 return -ETIMEDOUT;
637#undef MAX_TRIES
John W. Linville1b2fb7d2010-05-03 16:06:47 -0400638}
639
Christian Lamparter0d4171e2011-02-16 19:43:06 +0100640static unsigned int p54_flush_count(struct p54_common *priv)
641{
642 unsigned int total = 0, i;
643
644 BUILD_BUG_ON(P54_QUEUE_NUM > ARRAY_SIZE(priv->tx_stats));
645
646 /*
647 * Because the firmware has the sole control over any frames
648 * in the P54_QUEUE_BEACON or P54_QUEUE_SCAN queues, they
649 * don't really count as pending or active.
650 */
651 for (i = P54_QUEUE_MGMT; i < P54_QUEUE_NUM; i++)
652 total += priv->tx_stats[i].len;
653 return total;
654}
655
656static void p54_flush(struct ieee80211_hw *dev, bool drop)
657{
658 struct p54_common *priv = dev->priv;
659 unsigned int total, i;
660
661 /*
662 * Currently, it wouldn't really matter if we wait for one second
663 * or 15 minutes. But once someone gets around and completes the
664 * TODOs [ancel stuck frames / reset device] in p54_work, it will
665 * suddenly make sense to wait that long.
666 */
667 i = P54_STATISTICS_UPDATE * 2 / 20;
668
669 /*
670 * In this case no locking is required because as we speak the
671 * queues have already been stopped and no new frames can sneak
672 * up from behind.
673 */
674 while ((total = p54_flush_count(priv) && i--)) {
675 /* waste time */
676 msleep(20);
677 }
678
679 WARN(total, "tx flush timeout, unresponsive firmware");
680}
681
Christian Lamparter3083e832011-02-24 14:12:20 +0100682static void p54_set_coverage_class(struct ieee80211_hw *dev, u8 coverage_class)
683{
684 struct p54_common *priv = dev->priv;
685
686 mutex_lock(&priv->conf_mutex);
687 /* support all coverage class values as in 802.11-2007 Table 7-27 */
688 priv->coverage_class = clamp_t(u8, coverage_class, 0, 31);
689 p54_set_edcf(priv);
690 mutex_unlock(&priv->conf_mutex);
691}
692
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500693static const struct ieee80211_ops p54_ops = {
694 .tx = p54_tx_80211,
695 .start = p54_start,
696 .stop = p54_stop,
697 .add_interface = p54_add_interface,
698 .remove_interface = p54_remove_interface,
699 .set_tim = p54_set_tim,
700 .sta_notify = p54_sta_notify,
Johannes Berg17f6f052010-02-19 19:06:54 +0100701 .sta_add = p54_sta_add_remove,
702 .sta_remove = p54_sta_add_remove,
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500703 .set_key = p54_set_key,
704 .config = p54_config,
Christian Lamparter0d4171e2011-02-16 19:43:06 +0100705 .flush = p54_flush,
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500706 .bss_info_changed = p54_bss_info_changed,
Christian Lamparterbe8d98e2011-04-24 17:22:59 +0200707 .prepare_multicast = p54_prepare_multicast,
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500708 .configure_filter = p54_configure_filter,
709 .conf_tx = p54_conf_tx,
710 .get_stats = p54_get_stats,
John W. Linville1b2fb7d2010-05-03 16:06:47 -0400711 .get_survey = p54_get_survey,
Christian Lamparter3083e832011-02-24 14:12:20 +0100712 .set_coverage_class = p54_set_coverage_class,
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500713};
714
715struct ieee80211_hw *p54_init_common(size_t priv_data_len)
716{
717 struct ieee80211_hw *dev;
718 struct p54_common *priv;
719
720 dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
721 if (!dev)
722 return NULL;
723
724 priv = dev->priv;
725 priv->hw = dev;
726 priv->mode = NL80211_IFTYPE_UNSPECIFIED;
727 priv->basic_rate_mask = 0x15f;
728 spin_lock_init(&priv->tx_stats_lock);
729 skb_queue_head_init(&priv->tx_queue);
730 skb_queue_head_init(&priv->tx_pending);
731 dev->flags = IEEE80211_HW_RX_INCLUDES_FCS |
732 IEEE80211_HW_SIGNAL_DBM |
Christian Lampartere0f114e2009-07-07 19:08:07 +0200733 IEEE80211_HW_SUPPORTS_PS |
734 IEEE80211_HW_PS_NULLFUNC_STACK |
735 IEEE80211_HW_BEACON_FILTER |
John W. Linvillef5c044e2010-04-30 15:37:00 -0400736 IEEE80211_HW_REPORTS_TX_ACK_STATUS;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500737
738 dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
739 BIT(NL80211_IFTYPE_ADHOC) |
740 BIT(NL80211_IFTYPE_AP) |
741 BIT(NL80211_IFTYPE_MESH_POINT);
742
743 dev->channel_change_time = 1000; /* TODO: find actual value */
Christian Lamparter46df10a2009-07-16 20:03:47 +0200744 priv->beacon_req_id = cpu_to_le32(0);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500745 priv->tx_stats[P54_QUEUE_BEACON].limit = 1;
746 priv->tx_stats[P54_QUEUE_FWSCAN].limit = 1;
747 priv->tx_stats[P54_QUEUE_MGMT].limit = 3;
748 priv->tx_stats[P54_QUEUE_CAB].limit = 3;
749 priv->tx_stats[P54_QUEUE_DATA].limit = 5;
750 dev->queues = 1;
751 priv->noise = -94;
752 /*
753 * We support at most 8 tries no matter which rate they're at,
754 * we cannot support max_rates * max_rate_tries as we set it
755 * here, but setting it correctly to 4/2 or so would limit us
756 * artificially if the RC algorithm wants just two rates, so
757 * let's say 4/7, we'll redistribute it at TX time, see the
758 * comments there.
759 */
760 dev->max_rates = 4;
761 dev->max_rate_tries = 7;
762 dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
763 sizeof(struct p54_tx_data);
764
Christian Lamparterc46aaba2009-08-14 13:23:05 +0200765 /*
766 * For now, disable PS by default because it affects
767 * link stability significantly.
768 */
Johannes Berg5be83de2009-11-19 00:56:28 +0100769 dev->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
Christian Lamparterc46aaba2009-08-14 13:23:05 +0200770
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500771 mutex_init(&priv->conf_mutex);
772 mutex_init(&priv->eeprom_mutex);
Christian Lamparter0d781562011-08-20 01:53:59 +0200773 init_completion(&priv->stat_comp);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500774 init_completion(&priv->eeprom_comp);
Christian Lamparter46df10a2009-07-16 20:03:47 +0200775 init_completion(&priv->beacon_comp);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500776 INIT_DELAYED_WORK(&priv->work, p54_work);
777
Christian Lamparterbe8d98e2011-04-24 17:22:59 +0200778 memset(&priv->mc_maclist[0], ~0, ETH_ALEN);
Christian Lamparter0d781562011-08-20 01:53:59 +0200779 priv->curchan = NULL;
780 p54_reset_stats(priv);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500781 return dev;
782}
783EXPORT_SYMBOL_GPL(p54_init_common);
784
785int p54_register_common(struct ieee80211_hw *dev, struct device *pdev)
786{
Larry Fingerfe0b7c62011-02-15 09:37:06 -0600787 struct p54_common __maybe_unused *priv = dev->priv;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500788 int err;
789
790 err = ieee80211_register_hw(dev);
791 if (err) {
792 dev_err(pdev, "Cannot register device (%d).\n", err);
793 return err;
794 }
795
796#ifdef CONFIG_P54_LEDS
797 err = p54_init_leds(priv);
798 if (err)
799 return err;
800#endif /* CONFIG_P54_LEDS */
801
802 dev_info(pdev, "is registered as '%s'\n", wiphy_name(dev->wiphy));
803 return 0;
804}
805EXPORT_SYMBOL_GPL(p54_register_common);
806
807void p54_free_common(struct ieee80211_hw *dev)
808{
809 struct p54_common *priv = dev->priv;
Christian Lamparter1a9b6672009-07-11 01:22:26 +0200810 unsigned int i;
811
812 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
813 kfree(priv->band_table[i]);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500814
815 kfree(priv->iq_autocal);
816 kfree(priv->output_limit);
817 kfree(priv->curve_data);
Christian Lamparter7a047f42011-02-12 22:32:49 +0100818 kfree(priv->rssi_db);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500819 kfree(priv->used_rxkeys);
Christian Lamparter0d781562011-08-20 01:53:59 +0200820 kfree(priv->survey);
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500821 priv->iq_autocal = NULL;
822 priv->output_limit = NULL;
823 priv->curve_data = NULL;
Christian Lamparter7a047f42011-02-12 22:32:49 +0100824 priv->rssi_db = NULL;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500825 priv->used_rxkeys = NULL;
Christian Lamparter0d781562011-08-20 01:53:59 +0200826 priv->survey = NULL;
Christian Lamparter0ac0d6c2009-06-23 10:38:49 -0500827 ieee80211_free_hw(dev);
828}
829EXPORT_SYMBOL_GPL(p54_free_common);
830
831void p54_unregister_common(struct ieee80211_hw *dev)
832{
833 struct p54_common *priv = dev->priv;
834
835#ifdef CONFIG_P54_LEDS
836 p54_unregister_leds(priv);
837#endif /* CONFIG_P54_LEDS */
838
839 ieee80211_unregister_hw(dev);
840 mutex_destroy(&priv->conf_mutex);
841 mutex_destroy(&priv->eeprom_mutex);
842}
843EXPORT_SYMBOL_GPL(p54_unregister_common);