]>
Commit | Line | Data |
---|---|---|
0ac0d6ce CL |
1 | /* |
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> | |
5a0e3ad6 | 20 | #include <linux/slab.h> |
0ac0d6ce CL |
21 | #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 | ||
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"); | |
36 | ||
17f6f054 JB |
37 | static 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 | ||
0ac0d6ce CL |
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) | |
56 | { | |
57 | struct p54_common *priv = dev->priv; | |
0ac0d6ce | 58 | |
17f6f054 | 59 | switch (notify_cmd) { |
0ac0d6ce CL |
60 | 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 | ||
69 | static 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 | ||
e0f114e8 | 77 | u8 *p54_find_ie(struct sk_buff *skb, u8 ie) |
0ac0d6ce | 78 | { |
0ac0d6ce CL |
79 | struct ieee80211_mgmt *mgmt = (void *)skb->data; |
80 | u8 *pos, *end; | |
81 | ||
82 | if (skb->len <= sizeof(mgmt)) | |
e0f114e8 | 83 | return NULL; |
0ac0d6ce CL |
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) | |
e0f114e8 | 89 | return NULL; |
0ac0d6ce | 90 | |
e0f114e8 CL |
91 | if (pos[0] == ie) |
92 | return pos; | |
0ac0d6ce | 93 | |
e0f114e8 CL |
94 | pos += 2 + pos[1]; |
95 | } | |
96 | return NULL; | |
97 | } | |
0ac0d6ce | 98 | |
e0f114e8 CL |
99 | static 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 | */ | |
106 | u8 *tim; | |
107 | u8 dtim_len; | |
108 | u8 dtim_period; | |
109 | u8 *next; | |
0ac0d6ce | 110 | |
e0f114e8 CL |
111 | tim = p54_find_ie(skb, WLAN_EID_TIM); |
112 | if (!tim) | |
113 | return 0; | |
0ac0d6ce | 114 | |
e0f114e8 CL |
115 | dtim_len = tim[1]; |
116 | dtim_period = tim[3]; | |
117 | next = tim + 2 + dtim_len; | |
118 | ||
119 | if (dtim_len < 3) | |
120 | return -EINVAL; | |
121 | ||
122 | memmove(tim, next, skb_tail_pointer(skb) - next); | |
123 | tim = skb_tail_pointer(skb) - (dtim_len + 2); | |
124 | ||
125 | /* 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; | |
131 | ||
132 | if (dtim_len > 3) | |
133 | skb_trim(skb, skb->len - (dtim_len - 3)); | |
0ac0d6ce | 134 | |
0ac0d6ce CL |
135 | return 0; |
136 | } | |
137 | ||
138 | static int p54_beacon_update(struct p54_common *priv, | |
139 | struct ieee80211_vif *vif) | |
140 | { | |
141 | struct sk_buff *beacon; | |
0ac0d6ce CL |
142 | 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 | ||
46df10ae CL |
151 | /* |
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 | */ | |
7bb45683 | 160 | p54_tx_80211(priv->hw, beacon); |
0ac0d6ce CL |
161 | priv->tsf_high32 = 0; |
162 | priv->tsf_low32 = 0; | |
163 | ||
164 | return 0; | |
165 | } | |
166 | ||
167 | static 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 | ||
42935eca | 192 | ieee80211_queue_delayed_work(dev, &priv->work, 0); |
0ac0d6ce CL |
193 | |
194 | priv->softled_state = 0; | |
195 | err = p54_set_leds(priv); | |
196 | ||
197 | out: | |
198 | mutex_unlock(&priv->conf_mutex); | |
199 | return err; | |
200 | } | |
201 | ||
202 | static void p54_stop(struct ieee80211_hw *dev) | |
203 | { | |
204 | struct p54_common *priv = dev->priv; | |
205 | int i; | |
206 | ||
0ac0d6ce CL |
207 | priv->mode = NL80211_IFTYPE_UNSPECIFIED; |
208 | priv->softled_state = 0; | |
0ac0d6ce | 209 | cancel_delayed_work_sync(&priv->work); |
0d78156e CL |
210 | mutex_lock(&priv->conf_mutex); |
211 | p54_set_leds(priv); | |
0ac0d6ce CL |
212 | 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 | ||
225 | static int p54_add_interface(struct ieee80211_hw *dev, | |
1ed32e4f | 226 | struct ieee80211_vif *vif) |
0ac0d6ce CL |
227 | { |
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 | ||
1ed32e4f | 236 | priv->vif = vif; |
0ac0d6ce | 237 | |
1ed32e4f | 238 | switch (vif->type) { |
0ac0d6ce CL |
239 | case NL80211_IFTYPE_STATION: |
240 | case NL80211_IFTYPE_ADHOC: | |
241 | case NL80211_IFTYPE_AP: | |
242 | case NL80211_IFTYPE_MESH_POINT: | |
1ed32e4f | 243 | priv->mode = vif->type; |
0ac0d6ce CL |
244 | break; |
245 | default: | |
246 | mutex_unlock(&priv->conf_mutex); | |
247 | return -EOPNOTSUPP; | |
248 | } | |
249 | ||
1ed32e4f | 250 | memcpy(priv->mac_addr, vif->addr, ETH_ALEN); |
0ac0d6ce CL |
251 | p54_setup_mac(priv); |
252 | mutex_unlock(&priv->conf_mutex); | |
253 | return 0; | |
254 | } | |
255 | ||
256 | static void p54_remove_interface(struct ieee80211_hw *dev, | |
1ed32e4f | 257 | struct ieee80211_vif *vif) |
0ac0d6ce CL |
258 | { |
259 | struct p54_common *priv = dev->priv; | |
260 | ||
261 | mutex_lock(&priv->conf_mutex); | |
262 | priv->vif = NULL; | |
46df10ae CL |
263 | |
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) { | |
0ac0d6ce | 269 | p54_tx_cancel(priv, priv->beacon_req_id); |
46df10ae | 270 | wait_for_completion_interruptible_timeout(&priv->beacon_comp, HZ); |
0ac0d6ce CL |
271 | } |
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 | ||
0d78156e CL |
279 | static 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 | ||
296 | static 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 | ||
0ac0d6ce CL |
315 | static 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) { | |
0d78156e CL |
325 | struct ieee80211_channel *oldchan; |
326 | WARN_ON(p54_wait_for_stats(dev)); | |
327 | oldchan = priv->curchan; | |
328 | priv->curchan = NULL; | |
0ac0d6ce | 329 | ret = p54_scan(priv, P54_SCAN_EXIT, 0); |
0d78156e CL |
330 | if (ret) { |
331 | priv->curchan = oldchan; | |
0ac0d6ce | 332 | goto out; |
0d78156e CL |
333 | } |
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)); | |
0ac0d6ce CL |
341 | } |
342 | if (changed & IEEE80211_CONF_CHANGE_PS) { | |
0d78156e | 343 | WARN_ON(p54_wait_for_stats(dev)); |
0ac0d6ce CL |
344 | ret = p54_set_ps(priv); |
345 | if (ret) | |
346 | goto out; | |
0d78156e | 347 | WARN_ON(p54_wait_for_stats(dev)); |
0ac0d6ce | 348 | } |
6208f8b2 | 349 | if (changed & IEEE80211_CONF_CHANGE_IDLE) { |
0d78156e | 350 | WARN_ON(p54_wait_for_stats(dev)); |
6208f8b2 CL |
351 | ret = p54_setup_mac(priv); |
352 | if (ret) | |
353 | goto out; | |
0d78156e | 354 | WARN_ON(p54_wait_for_stats(dev)); |
6208f8b2 | 355 | } |
0ac0d6ce CL |
356 | |
357 | out: | |
358 | mutex_unlock(&priv->conf_mutex); | |
359 | return ret; | |
360 | } | |
361 | ||
be8d98ea CL |
362 | static 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 | ||
0ac0d6ce CL |
387 | static void p54_configure_filter(struct ieee80211_hw *dev, |
388 | unsigned int changed_flags, | |
389 | unsigned int *total_flags, | |
3ac64bee | 390 | u64 multicast) |
0ac0d6ce CL |
391 | { |
392 | struct p54_common *priv = dev->priv; | |
393 | ||
394 | *total_flags &= FIF_PROMISC_IN_BSS | | |
be8d98ea | 395 | FIF_ALLMULTI | |
0ac0d6ce CL |
396 | 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); | |
be8d98ea CL |
402 | |
403 | if (changed_flags & FIF_ALLMULTI || multicast) | |
404 | p54_set_groupfilter(priv); | |
0ac0d6ce CL |
405 | } |
406 | ||
8a3a3c85 EP |
407 | static int p54_conf_tx(struct ieee80211_hw *dev, |
408 | struct ieee80211_vif *vif, u16 queue, | |
0ac0d6ce CL |
409 | const struct ieee80211_tx_queue_params *params) |
410 | { | |
411 | struct p54_common *priv = dev->priv; | |
412 | int ret; | |
413 | ||
414 | mutex_lock(&priv->conf_mutex); | |
718126a7 | 415 | if (queue < dev->queues) { |
0ac0d6ce CL |
416 | P54_SET_QUEUE(priv->qos_params[queue], params->aifs, |
417 | params->cw_min, params->cw_max, params->txop); | |
418 | ret = p54_set_edcf(priv); | |
419 | } else | |
420 | ret = -EINVAL; | |
421 | mutex_unlock(&priv->conf_mutex); | |
422 | return ret; | |
423 | } | |
424 | ||
425 | static void p54_work(struct work_struct *work) | |
426 | { | |
427 | struct p54_common *priv = container_of(work, struct p54_common, | |
428 | work.work); | |
429 | ||
430 | if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED)) | |
431 | return ; | |
432 | ||
433 | /* | |
434 | * TODO: walk through tx_queue and do the following tasks | |
435 | * 1. initiate bursts. | |
436 | * 2. cancel stuck frames / reset the device if necessary. | |
437 | */ | |
438 | ||
0d78156e CL |
439 | mutex_lock(&priv->conf_mutex); |
440 | WARN_ON_ONCE(p54_fetch_statistics(priv)); | |
441 | mutex_unlock(&priv->conf_mutex); | |
0ac0d6ce CL |
442 | } |
443 | ||
444 | static int p54_get_stats(struct ieee80211_hw *dev, | |
445 | struct ieee80211_low_level_stats *stats) | |
446 | { | |
447 | struct p54_common *priv = dev->priv; | |
448 | ||
449 | memcpy(stats, &priv->stats, sizeof(*stats)); | |
450 | return 0; | |
451 | } | |
452 | ||
0ac0d6ce CL |
453 | static void p54_bss_info_changed(struct ieee80211_hw *dev, |
454 | struct ieee80211_vif *vif, | |
455 | struct ieee80211_bss_conf *info, | |
456 | u32 changed) | |
457 | { | |
458 | struct p54_common *priv = dev->priv; | |
459 | ||
460 | mutex_lock(&priv->conf_mutex); | |
461 | if (changed & BSS_CHANGED_BSSID) { | |
462 | memcpy(priv->bssid, info->bssid, ETH_ALEN); | |
463 | p54_setup_mac(priv); | |
464 | } | |
465 | ||
466 | if (changed & BSS_CHANGED_BEACON) { | |
467 | p54_scan(priv, P54_SCAN_EXIT, 0); | |
468 | p54_setup_mac(priv); | |
469 | p54_beacon_update(priv, vif); | |
470 | p54_set_edcf(priv); | |
471 | } | |
472 | ||
473 | if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_BEACON)) { | |
474 | priv->use_short_slot = info->use_short_slot; | |
475 | p54_set_edcf(priv); | |
476 | } | |
477 | if (changed & BSS_CHANGED_BASIC_RATES) { | |
478 | if (dev->conf.channel->band == IEEE80211_BAND_5GHZ) | |
479 | priv->basic_rate_mask = (info->basic_rates << 4); | |
480 | else | |
481 | priv->basic_rate_mask = info->basic_rates; | |
482 | p54_setup_mac(priv); | |
483 | if (priv->fw_var >= 0x500) | |
484 | p54_scan(priv, P54_SCAN_EXIT, 0); | |
485 | } | |
486 | if (changed & BSS_CHANGED_ASSOC) { | |
487 | if (info->assoc) { | |
488 | priv->aid = info->aid; | |
489 | priv->wakeup_timer = info->beacon_int * | |
490 | info->dtim_period * 5; | |
491 | p54_setup_mac(priv); | |
e0f114e8 CL |
492 | } else { |
493 | priv->wakeup_timer = 500; | |
494 | priv->aid = 0; | |
0ac0d6ce CL |
495 | } |
496 | } | |
497 | ||
498 | mutex_unlock(&priv->conf_mutex); | |
499 | } | |
500 | ||
501 | static int p54_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd, | |
502 | struct ieee80211_vif *vif, struct ieee80211_sta *sta, | |
503 | struct ieee80211_key_conf *key) | |
504 | { | |
505 | struct p54_common *priv = dev->priv; | |
506 | int slot, ret = 0; | |
507 | u8 algo = 0; | |
508 | u8 *addr = NULL; | |
509 | ||
510 | if (modparam_nohwcrypt) | |
511 | return -EOPNOTSUPP; | |
512 | ||
513 | mutex_lock(&priv->conf_mutex); | |
514 | if (cmd == SET_KEY) { | |
97359d12 JB |
515 | switch (key->cipher) { |
516 | case WLAN_CIPHER_SUITE_TKIP: | |
0ac0d6ce CL |
517 | if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL | |
518 | BR_DESC_PRIV_CAP_TKIP))) { | |
519 | ret = -EOPNOTSUPP; | |
520 | goto out_unlock; | |
521 | } | |
522 | key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV; | |
523 | algo = P54_CRYPTO_TKIPMICHAEL; | |
524 | break; | |
97359d12 JB |
525 | case WLAN_CIPHER_SUITE_WEP40: |
526 | case WLAN_CIPHER_SUITE_WEP104: | |
0ac0d6ce CL |
527 | if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP)) { |
528 | ret = -EOPNOTSUPP; | |
529 | goto out_unlock; | |
530 | } | |
531 | key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV; | |
532 | algo = P54_CRYPTO_WEP; | |
533 | break; | |
97359d12 | 534 | case WLAN_CIPHER_SUITE_CCMP: |
0ac0d6ce CL |
535 | if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP)) { |
536 | ret = -EOPNOTSUPP; | |
537 | goto out_unlock; | |
538 | } | |
539 | key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV; | |
540 | algo = P54_CRYPTO_AESCCMP; | |
541 | break; | |
542 | default: | |
543 | ret = -EOPNOTSUPP; | |
544 | goto out_unlock; | |
545 | } | |
546 | slot = bitmap_find_free_region(priv->used_rxkeys, | |
547 | priv->rx_keycache_size, 0); | |
548 | ||
549 | if (slot < 0) { | |
550 | /* | |
25985edc | 551 | * The device supports the chosen algorithm, but the |
0ac0d6ce CL |
552 | * firmware does not provide enough key slots to store |
553 | * all of them. | |
554 | * But encryption offload for outgoing frames is always | |
555 | * possible, so we just pretend that the upload was | |
556 | * successful and do the decryption in software. | |
557 | */ | |
558 | ||
559 | /* mark the key as invalid. */ | |
560 | key->hw_key_idx = 0xff; | |
561 | goto out_unlock; | |
562 | } | |
563 | } else { | |
564 | slot = key->hw_key_idx; | |
565 | ||
566 | if (slot == 0xff) { | |
567 | /* This key was not uploaded into the rx key cache. */ | |
568 | ||
569 | goto out_unlock; | |
570 | } | |
571 | ||
572 | bitmap_release_region(priv->used_rxkeys, slot, 0); | |
573 | algo = 0; | |
574 | } | |
575 | ||
576 | if (sta) | |
577 | addr = sta->addr; | |
578 | ||
579 | ret = p54_upload_key(priv, algo, slot, key->keyidx, | |
580 | key->keylen, addr, key->key); | |
581 | if (ret) { | |
582 | bitmap_release_region(priv->used_rxkeys, slot, 0); | |
583 | ret = -EOPNOTSUPP; | |
584 | goto out_unlock; | |
585 | } | |
586 | ||
587 | key->hw_key_idx = slot; | |
588 | ||
589 | out_unlock: | |
590 | mutex_unlock(&priv->conf_mutex); | |
591 | return ret; | |
592 | } | |
593 | ||
1b2fb7dc JL |
594 | static int p54_get_survey(struct ieee80211_hw *dev, int idx, |
595 | struct survey_info *survey) | |
596 | { | |
597 | struct p54_common *priv = dev->priv; | |
0d78156e CL |
598 | struct ieee80211_channel *chan; |
599 | int err, tries; | |
600 | bool in_use = false; | |
1b2fb7dc | 601 | |
0d78156e | 602 | if (idx >= priv->chan_num) |
1b2fb7dc JL |
603 | return -ENOENT; |
604 | ||
0d78156e CL |
605 | #define MAX_TRIES 1 |
606 | for (tries = 0; tries < MAX_TRIES; tries++) { | |
607 | chan = priv->curchan; | |
608 | if (chan && chan->hw_value == idx) { | |
609 | mutex_lock(&priv->conf_mutex); | |
610 | err = p54_wait_for_stats(dev); | |
611 | mutex_unlock(&priv->conf_mutex); | |
612 | if (err) | |
613 | return err; | |
614 | ||
615 | in_use = true; | |
616 | } | |
1b2fb7dc | 617 | |
0d78156e CL |
618 | memcpy(survey, &priv->survey[idx], sizeof(*survey)); |
619 | ||
620 | if (in_use) { | |
621 | /* test if the reported statistics are valid. */ | |
622 | if (survey->channel_time != 0) { | |
623 | survey->filled |= SURVEY_INFO_IN_USE; | |
624 | } else { | |
625 | /* | |
626 | * hw/fw has not accumulated enough sample sets. | |
627 | * Wait for 100ms, this ought to be enough to | |
628 | * to get at least one non-null set of channel | |
629 | * usage statistics. | |
630 | */ | |
631 | msleep(100); | |
632 | continue; | |
633 | } | |
634 | } | |
635 | return 0; | |
636 | } | |
637 | return -ETIMEDOUT; | |
638 | #undef MAX_TRIES | |
1b2fb7dc JL |
639 | } |
640 | ||
0d4171e2 CL |
641 | static unsigned int p54_flush_count(struct p54_common *priv) |
642 | { | |
643 | unsigned int total = 0, i; | |
644 | ||
645 | BUILD_BUG_ON(P54_QUEUE_NUM > ARRAY_SIZE(priv->tx_stats)); | |
646 | ||
647 | /* | |
648 | * Because the firmware has the sole control over any frames | |
649 | * in the P54_QUEUE_BEACON or P54_QUEUE_SCAN queues, they | |
650 | * don't really count as pending or active. | |
651 | */ | |
652 | for (i = P54_QUEUE_MGMT; i < P54_QUEUE_NUM; i++) | |
653 | total += priv->tx_stats[i].len; | |
654 | return total; | |
655 | } | |
656 | ||
657 | static void p54_flush(struct ieee80211_hw *dev, bool drop) | |
658 | { | |
659 | struct p54_common *priv = dev->priv; | |
660 | unsigned int total, i; | |
661 | ||
662 | /* | |
663 | * Currently, it wouldn't really matter if we wait for one second | |
664 | * or 15 minutes. But once someone gets around and completes the | |
665 | * TODOs [ancel stuck frames / reset device] in p54_work, it will | |
666 | * suddenly make sense to wait that long. | |
667 | */ | |
668 | i = P54_STATISTICS_UPDATE * 2 / 20; | |
669 | ||
670 | /* | |
671 | * In this case no locking is required because as we speak the | |
672 | * queues have already been stopped and no new frames can sneak | |
673 | * up from behind. | |
674 | */ | |
675 | while ((total = p54_flush_count(priv) && i--)) { | |
676 | /* waste time */ | |
677 | msleep(20); | |
678 | } | |
679 | ||
680 | WARN(total, "tx flush timeout, unresponsive firmware"); | |
681 | } | |
682 | ||
3083e83c CL |
683 | static void p54_set_coverage_class(struct ieee80211_hw *dev, u8 coverage_class) |
684 | { | |
685 | struct p54_common *priv = dev->priv; | |
686 | ||
687 | mutex_lock(&priv->conf_mutex); | |
688 | /* support all coverage class values as in 802.11-2007 Table 7-27 */ | |
689 | priv->coverage_class = clamp_t(u8, coverage_class, 0, 31); | |
690 | p54_set_edcf(priv); | |
691 | mutex_unlock(&priv->conf_mutex); | |
692 | } | |
693 | ||
0ac0d6ce CL |
694 | static const struct ieee80211_ops p54_ops = { |
695 | .tx = p54_tx_80211, | |
696 | .start = p54_start, | |
697 | .stop = p54_stop, | |
698 | .add_interface = p54_add_interface, | |
699 | .remove_interface = p54_remove_interface, | |
700 | .set_tim = p54_set_tim, | |
701 | .sta_notify = p54_sta_notify, | |
17f6f054 JB |
702 | .sta_add = p54_sta_add_remove, |
703 | .sta_remove = p54_sta_add_remove, | |
0ac0d6ce CL |
704 | .set_key = p54_set_key, |
705 | .config = p54_config, | |
0d4171e2 | 706 | .flush = p54_flush, |
0ac0d6ce | 707 | .bss_info_changed = p54_bss_info_changed, |
be8d98ea | 708 | .prepare_multicast = p54_prepare_multicast, |
0ac0d6ce CL |
709 | .configure_filter = p54_configure_filter, |
710 | .conf_tx = p54_conf_tx, | |
711 | .get_stats = p54_get_stats, | |
1b2fb7dc | 712 | .get_survey = p54_get_survey, |
3083e83c | 713 | .set_coverage_class = p54_set_coverage_class, |
0ac0d6ce CL |
714 | }; |
715 | ||
716 | struct ieee80211_hw *p54_init_common(size_t priv_data_len) | |
717 | { | |
718 | struct ieee80211_hw *dev; | |
719 | struct p54_common *priv; | |
720 | ||
721 | dev = ieee80211_alloc_hw(priv_data_len, &p54_ops); | |
722 | if (!dev) | |
723 | return NULL; | |
724 | ||
725 | priv = dev->priv; | |
726 | priv->hw = dev; | |
727 | priv->mode = NL80211_IFTYPE_UNSPECIFIED; | |
728 | priv->basic_rate_mask = 0x15f; | |
729 | spin_lock_init(&priv->tx_stats_lock); | |
730 | skb_queue_head_init(&priv->tx_queue); | |
731 | skb_queue_head_init(&priv->tx_pending); | |
732 | dev->flags = IEEE80211_HW_RX_INCLUDES_FCS | | |
733 | IEEE80211_HW_SIGNAL_DBM | | |
e0f114e8 CL |
734 | IEEE80211_HW_SUPPORTS_PS | |
735 | IEEE80211_HW_PS_NULLFUNC_STACK | | |
736 | IEEE80211_HW_BEACON_FILTER | | |
f5c044e5 | 737 | IEEE80211_HW_REPORTS_TX_ACK_STATUS; |
0ac0d6ce CL |
738 | |
739 | dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) | | |
740 | BIT(NL80211_IFTYPE_ADHOC) | | |
741 | BIT(NL80211_IFTYPE_AP) | | |
742 | BIT(NL80211_IFTYPE_MESH_POINT); | |
743 | ||
744 | dev->channel_change_time = 1000; /* TODO: find actual value */ | |
46df10ae | 745 | priv->beacon_req_id = cpu_to_le32(0); |
0ac0d6ce CL |
746 | priv->tx_stats[P54_QUEUE_BEACON].limit = 1; |
747 | priv->tx_stats[P54_QUEUE_FWSCAN].limit = 1; | |
748 | priv->tx_stats[P54_QUEUE_MGMT].limit = 3; | |
749 | priv->tx_stats[P54_QUEUE_CAB].limit = 3; | |
750 | priv->tx_stats[P54_QUEUE_DATA].limit = 5; | |
751 | dev->queues = 1; | |
752 | priv->noise = -94; | |
753 | /* | |
754 | * We support at most 8 tries no matter which rate they're at, | |
755 | * we cannot support max_rates * max_rate_tries as we set it | |
756 | * here, but setting it correctly to 4/2 or so would limit us | |
757 | * artificially if the RC algorithm wants just two rates, so | |
758 | * let's say 4/7, we'll redistribute it at TX time, see the | |
759 | * comments there. | |
760 | */ | |
761 | dev->max_rates = 4; | |
762 | dev->max_rate_tries = 7; | |
763 | dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 + | |
764 | sizeof(struct p54_tx_data); | |
765 | ||
c46aaba7 CL |
766 | /* |
767 | * For now, disable PS by default because it affects | |
768 | * link stability significantly. | |
769 | */ | |
5be83de5 | 770 | dev->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT; |
c46aaba7 | 771 | |
0ac0d6ce CL |
772 | mutex_init(&priv->conf_mutex); |
773 | mutex_init(&priv->eeprom_mutex); | |
0d78156e | 774 | init_completion(&priv->stat_comp); |
0ac0d6ce | 775 | init_completion(&priv->eeprom_comp); |
46df10ae | 776 | init_completion(&priv->beacon_comp); |
0ac0d6ce CL |
777 | INIT_DELAYED_WORK(&priv->work, p54_work); |
778 | ||
be8d98ea | 779 | memset(&priv->mc_maclist[0], ~0, ETH_ALEN); |
0d78156e CL |
780 | priv->curchan = NULL; |
781 | p54_reset_stats(priv); | |
0ac0d6ce CL |
782 | return dev; |
783 | } | |
784 | EXPORT_SYMBOL_GPL(p54_init_common); | |
785 | ||
786 | int p54_register_common(struct ieee80211_hw *dev, struct device *pdev) | |
787 | { | |
fe0b7c61 | 788 | struct p54_common __maybe_unused *priv = dev->priv; |
0ac0d6ce CL |
789 | int err; |
790 | ||
791 | err = ieee80211_register_hw(dev); | |
792 | if (err) { | |
793 | dev_err(pdev, "Cannot register device (%d).\n", err); | |
794 | return err; | |
795 | } | |
796 | ||
797 | #ifdef CONFIG_P54_LEDS | |
798 | err = p54_init_leds(priv); | |
799 | if (err) | |
800 | return err; | |
801 | #endif /* CONFIG_P54_LEDS */ | |
802 | ||
803 | dev_info(pdev, "is registered as '%s'\n", wiphy_name(dev->wiphy)); | |
804 | return 0; | |
805 | } | |
806 | EXPORT_SYMBOL_GPL(p54_register_common); | |
807 | ||
808 | void p54_free_common(struct ieee80211_hw *dev) | |
809 | { | |
810 | struct p54_common *priv = dev->priv; | |
1a9b6679 CL |
811 | unsigned int i; |
812 | ||
813 | for (i = 0; i < IEEE80211_NUM_BANDS; i++) | |
814 | kfree(priv->band_table[i]); | |
0ac0d6ce CL |
815 | |
816 | kfree(priv->iq_autocal); | |
817 | kfree(priv->output_limit); | |
818 | kfree(priv->curve_data); | |
7a047f4f | 819 | kfree(priv->rssi_db); |
0ac0d6ce | 820 | kfree(priv->used_rxkeys); |
0d78156e | 821 | kfree(priv->survey); |
0ac0d6ce CL |
822 | priv->iq_autocal = NULL; |
823 | priv->output_limit = NULL; | |
824 | priv->curve_data = NULL; | |
7a047f4f | 825 | priv->rssi_db = NULL; |
0ac0d6ce | 826 | priv->used_rxkeys = NULL; |
0d78156e | 827 | priv->survey = NULL; |
0ac0d6ce CL |
828 | ieee80211_free_hw(dev); |
829 | } | |
830 | EXPORT_SYMBOL_GPL(p54_free_common); | |
831 | ||
832 | void p54_unregister_common(struct ieee80211_hw *dev) | |
833 | { | |
834 | struct p54_common *priv = dev->priv; | |
835 | ||
836 | #ifdef CONFIG_P54_LEDS | |
837 | p54_unregister_leds(priv); | |
838 | #endif /* CONFIG_P54_LEDS */ | |
839 | ||
840 | ieee80211_unregister_hw(dev); | |
841 | mutex_destroy(&priv->conf_mutex); | |
842 | mutex_destroy(&priv->eeprom_mutex); | |
843 | } | |
844 | EXPORT_SYMBOL_GPL(p54_unregister_common); |