Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[sfrench/cifs-2.6.git] / drivers / net / wireless / p54 / main.c
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>
20 #include <linux/firmware.h>
21 #include <linux/etherdevice.h>
22
23 #include <net/mac80211.h>
24
25 #include "p54.h"
26 #include "lmac.h"
27
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");
35
36 static int p54_sta_add_remove(struct ieee80211_hw *hw,
37                               struct ieee80211_vif *vif,
38                               struct ieee80211_sta *sta)
39 {
40         struct p54_common *priv = hw->priv;
41
42         /*
43          * Notify the firmware that we don't want or we don't
44          * need to buffer frames for this station anymore.
45          */
46
47         p54_sta_unlock(priv, sta->addr);
48
49         return 0;
50 }
51
52 static void p54_sta_notify(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
53                               enum sta_notify_cmd notify_cmd,
54                               struct ieee80211_sta *sta)
55 {
56         struct p54_common *priv = dev->priv;
57
58         switch (notify_cmd) {
59         case STA_NOTIFY_AWAKE:
60                 /* update the firmware's filter table */
61                 p54_sta_unlock(priv, sta->addr);
62                 break;
63         default:
64                 break;
65         }
66 }
67
68 static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
69                         bool set)
70 {
71         struct p54_common *priv = dev->priv;
72
73         return p54_update_beacon_tim(priv, sta->aid, set);
74 }
75
76 u8 *p54_find_ie(struct sk_buff *skb, u8 ie)
77 {
78         struct ieee80211_mgmt *mgmt = (void *)skb->data;
79         u8 *pos, *end;
80
81         if (skb->len <= sizeof(mgmt))
82                 return NULL;
83
84         pos = (u8 *)mgmt->u.beacon.variable;
85         end = skb->data + skb->len;
86         while (pos < end) {
87                 if (pos + 2 + pos[1] > end)
88                         return NULL;
89
90                 if (pos[0] == ie)
91                         return pos;
92
93                 pos += 2 + pos[1];
94         }
95         return NULL;
96 }
97
98 static int p54_beacon_format_ie_tim(struct sk_buff *skb)
99 {
100         /*
101          * the good excuse for this mess is ... the firmware.
102          * The dummy TIM MUST be at the end of the beacon frame,
103          * because it'll be overwritten!
104          */
105         u8 *tim;
106         u8 dtim_len;
107         u8 dtim_period;
108         u8 *next;
109
110         tim = p54_find_ie(skb, WLAN_EID_TIM);
111         if (!tim)
112                 return 0;
113
114         dtim_len = tim[1];
115         dtim_period = tim[3];
116         next = tim + 2 + dtim_len;
117
118         if (dtim_len < 3)
119                 return -EINVAL;
120
121         memmove(tim, next, skb_tail_pointer(skb) - next);
122         tim = skb_tail_pointer(skb) - (dtim_len + 2);
123
124         /* add the dummy at the end */
125         tim[0] = WLAN_EID_TIM;
126         tim[1] = 3;
127         tim[2] = 0;
128         tim[3] = dtim_period;
129         tim[4] = 0;
130
131         if (dtim_len > 3)
132                 skb_trim(skb, skb->len - (dtim_len - 3));
133
134         return 0;
135 }
136
137 static int p54_beacon_update(struct p54_common *priv,
138                         struct ieee80211_vif *vif)
139 {
140         struct sk_buff *beacon;
141         int ret;
142
143         beacon = ieee80211_beacon_get(priv->hw, vif);
144         if (!beacon)
145                 return -ENOMEM;
146         ret = p54_beacon_format_ie_tim(beacon);
147         if (ret)
148                 return ret;
149
150         /*
151          * During operation, the firmware takes care of beaconing.
152          * The driver only needs to upload a new beacon template, once
153          * the template was changed by the stack or userspace.
154          *
155          * LMAC API 3.2.2 also specifies that the driver does not need
156          * to cancel the old beacon template by hand, instead the firmware
157          * will release the previous one through the feedback mechanism.
158          */
159         WARN_ON(p54_tx_80211(priv->hw, beacon));
160         priv->tsf_high32 = 0;
161         priv->tsf_low32 = 0;
162
163         return 0;
164 }
165
166 static int p54_start(struct ieee80211_hw *dev)
167 {
168         struct p54_common *priv = dev->priv;
169         int err;
170
171         mutex_lock(&priv->conf_mutex);
172         err = priv->open(dev);
173         if (err)
174                 goto out;
175         P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
176         P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
177         P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
178         P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
179         err = p54_set_edcf(priv);
180         if (err)
181                 goto out;
182
183         memset(priv->bssid, ~0, ETH_ALEN);
184         priv->mode = NL80211_IFTYPE_MONITOR;
185         err = p54_setup_mac(priv);
186         if (err) {
187                 priv->mode = NL80211_IFTYPE_UNSPECIFIED;
188                 goto out;
189         }
190
191         ieee80211_queue_delayed_work(dev, &priv->work, 0);
192
193         priv->softled_state = 0;
194         err = p54_set_leds(priv);
195
196 out:
197         mutex_unlock(&priv->conf_mutex);
198         return err;
199 }
200
201 static void p54_stop(struct ieee80211_hw *dev)
202 {
203         struct p54_common *priv = dev->priv;
204         int i;
205
206         mutex_lock(&priv->conf_mutex);
207         priv->mode = NL80211_IFTYPE_UNSPECIFIED;
208         priv->softled_state = 0;
209         p54_set_leds(priv);
210
211         cancel_delayed_work_sync(&priv->work);
212
213         priv->stop(dev);
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;
219         }
220
221         priv->beacon_req_id = cpu_to_le32(0);
222         priv->tsf_high32 = priv->tsf_low32 = 0;
223         mutex_unlock(&priv->conf_mutex);
224 }
225
226 static int p54_add_interface(struct ieee80211_hw *dev,
227                              struct ieee80211_vif *vif)
228 {
229         struct p54_common *priv = dev->priv;
230
231         mutex_lock(&priv->conf_mutex);
232         if (priv->mode != NL80211_IFTYPE_MONITOR) {
233                 mutex_unlock(&priv->conf_mutex);
234                 return -EOPNOTSUPP;
235         }
236
237         priv->vif = vif;
238
239         switch (vif->type) {
240         case NL80211_IFTYPE_STATION:
241         case NL80211_IFTYPE_ADHOC:
242         case NL80211_IFTYPE_AP:
243         case NL80211_IFTYPE_MESH_POINT:
244                 priv->mode = vif->type;
245                 break;
246         default:
247                 mutex_unlock(&priv->conf_mutex);
248                 return -EOPNOTSUPP;
249         }
250
251         memcpy(priv->mac_addr, vif->addr, ETH_ALEN);
252         p54_setup_mac(priv);
253         mutex_unlock(&priv->conf_mutex);
254         return 0;
255 }
256
257 static void p54_remove_interface(struct ieee80211_hw *dev,
258                                  struct ieee80211_vif *vif)
259 {
260         struct p54_common *priv = dev->priv;
261
262         mutex_lock(&priv->conf_mutex);
263         priv->vif = NULL;
264
265         /*
266          * LMAC API 3.2.2 states that any active beacon template must be
267          * canceled by the driver before attempting a mode transition.
268          */
269         if (le32_to_cpu(priv->beacon_req_id) != 0) {
270                 p54_tx_cancel(priv, priv->beacon_req_id);
271                 wait_for_completion_interruptible_timeout(&priv->beacon_comp, HZ);
272         }
273         priv->mode = NL80211_IFTYPE_MONITOR;
274         memset(priv->mac_addr, 0, ETH_ALEN);
275         memset(priv->bssid, 0, ETH_ALEN);
276         p54_setup_mac(priv);
277         mutex_unlock(&priv->conf_mutex);
278 }
279
280 static int p54_config(struct ieee80211_hw *dev, u32 changed)
281 {
282         int ret = 0;
283         struct p54_common *priv = dev->priv;
284         struct ieee80211_conf *conf = &dev->conf;
285
286         mutex_lock(&priv->conf_mutex);
287         if (changed & IEEE80211_CONF_CHANGE_POWER)
288                 priv->output_power = conf->power_level << 2;
289         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
290                 ret = p54_scan(priv, P54_SCAN_EXIT, 0);
291                 if (ret)
292                         goto out;
293         }
294         if (changed & IEEE80211_CONF_CHANGE_PS) {
295                 ret = p54_set_ps(priv);
296                 if (ret)
297                         goto out;
298         }
299         if (changed & IEEE80211_CONF_CHANGE_IDLE) {
300                 ret = p54_setup_mac(priv);
301                 if (ret)
302                         goto out;
303         }
304
305 out:
306         mutex_unlock(&priv->conf_mutex);
307         return ret;
308 }
309
310 static void p54_configure_filter(struct ieee80211_hw *dev,
311                                  unsigned int changed_flags,
312                                  unsigned int *total_flags,
313                                  u64 multicast)
314 {
315         struct p54_common *priv = dev->priv;
316
317         *total_flags &= FIF_PROMISC_IN_BSS |
318                         FIF_OTHER_BSS;
319
320         priv->filter_flags = *total_flags;
321
322         if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS))
323                 p54_setup_mac(priv);
324 }
325
326 static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
327                        const struct ieee80211_tx_queue_params *params)
328 {
329         struct p54_common *priv = dev->priv;
330         int ret;
331
332         mutex_lock(&priv->conf_mutex);
333         if (queue < dev->queues) {
334                 P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
335                         params->cw_min, params->cw_max, params->txop);
336                 ret = p54_set_edcf(priv);
337         } else
338                 ret = -EINVAL;
339         mutex_unlock(&priv->conf_mutex);
340         return ret;
341 }
342
343 static void p54_work(struct work_struct *work)
344 {
345         struct p54_common *priv = container_of(work, struct p54_common,
346                                                work.work);
347
348         if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
349                 return ;
350
351         /*
352          * TODO: walk through tx_queue and do the following tasks
353          *      1. initiate bursts.
354          *      2. cancel stuck frames / reset the device if necessary.
355          */
356
357         p54_fetch_statistics(priv);
358 }
359
360 static int p54_get_stats(struct ieee80211_hw *dev,
361                          struct ieee80211_low_level_stats *stats)
362 {
363         struct p54_common *priv = dev->priv;
364
365         memcpy(stats, &priv->stats, sizeof(*stats));
366         return 0;
367 }
368
369 static void p54_bss_info_changed(struct ieee80211_hw *dev,
370                                  struct ieee80211_vif *vif,
371                                  struct ieee80211_bss_conf *info,
372                                  u32 changed)
373 {
374         struct p54_common *priv = dev->priv;
375
376         mutex_lock(&priv->conf_mutex);
377         if (changed & BSS_CHANGED_BSSID) {
378                 memcpy(priv->bssid, info->bssid, ETH_ALEN);
379                 p54_setup_mac(priv);
380         }
381
382         if (changed & BSS_CHANGED_BEACON) {
383                 p54_scan(priv, P54_SCAN_EXIT, 0);
384                 p54_setup_mac(priv);
385                 p54_beacon_update(priv, vif);
386                 p54_set_edcf(priv);
387         }
388
389         if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_BEACON)) {
390                 priv->use_short_slot = info->use_short_slot;
391                 p54_set_edcf(priv);
392         }
393         if (changed & BSS_CHANGED_BASIC_RATES) {
394                 if (dev->conf.channel->band == IEEE80211_BAND_5GHZ)
395                         priv->basic_rate_mask = (info->basic_rates << 4);
396                 else
397                         priv->basic_rate_mask = info->basic_rates;
398                 p54_setup_mac(priv);
399                 if (priv->fw_var >= 0x500)
400                         p54_scan(priv, P54_SCAN_EXIT, 0);
401         }
402         if (changed & BSS_CHANGED_ASSOC) {
403                 if (info->assoc) {
404                         priv->aid = info->aid;
405                         priv->wakeup_timer = info->beacon_int *
406                                              info->dtim_period * 5;
407                         p54_setup_mac(priv);
408                 } else {
409                         priv->wakeup_timer = 500;
410                         priv->aid = 0;
411                 }
412         }
413
414         mutex_unlock(&priv->conf_mutex);
415 }
416
417 static int p54_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
418                        struct ieee80211_vif *vif, struct ieee80211_sta *sta,
419                        struct ieee80211_key_conf *key)
420 {
421         struct p54_common *priv = dev->priv;
422         int slot, ret = 0;
423         u8 algo = 0;
424         u8 *addr = NULL;
425
426         if (modparam_nohwcrypt)
427                 return -EOPNOTSUPP;
428
429         mutex_lock(&priv->conf_mutex);
430         if (cmd == SET_KEY) {
431                 switch (key->alg) {
432                 case ALG_TKIP:
433                         if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL |
434                               BR_DESC_PRIV_CAP_TKIP))) {
435                                 ret = -EOPNOTSUPP;
436                                 goto out_unlock;
437                         }
438                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
439                         algo = P54_CRYPTO_TKIPMICHAEL;
440                         break;
441                 case ALG_WEP:
442                         if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP)) {
443                                 ret = -EOPNOTSUPP;
444                                 goto out_unlock;
445                         }
446                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
447                         algo = P54_CRYPTO_WEP;
448                         break;
449                 case ALG_CCMP:
450                         if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP)) {
451                                 ret = -EOPNOTSUPP;
452                                 goto out_unlock;
453                         }
454                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
455                         algo = P54_CRYPTO_AESCCMP;
456                         break;
457                 default:
458                         ret = -EOPNOTSUPP;
459                         goto out_unlock;
460                 }
461                 slot = bitmap_find_free_region(priv->used_rxkeys,
462                                                priv->rx_keycache_size, 0);
463
464                 if (slot < 0) {
465                         /*
466                          * The device supports the choosen algorithm, but the
467                          * firmware does not provide enough key slots to store
468                          * all of them.
469                          * But encryption offload for outgoing frames is always
470                          * possible, so we just pretend that the upload was
471                          * successful and do the decryption in software.
472                          */
473
474                         /* mark the key as invalid. */
475                         key->hw_key_idx = 0xff;
476                         goto out_unlock;
477                 }
478         } else {
479                 slot = key->hw_key_idx;
480
481                 if (slot == 0xff) {
482                         /* This key was not uploaded into the rx key cache. */
483
484                         goto out_unlock;
485                 }
486
487                 bitmap_release_region(priv->used_rxkeys, slot, 0);
488                 algo = 0;
489         }
490
491         if (sta)
492                 addr = sta->addr;
493
494         ret = p54_upload_key(priv, algo, slot, key->keyidx,
495                              key->keylen, addr, key->key);
496         if (ret) {
497                 bitmap_release_region(priv->used_rxkeys, slot, 0);
498                 ret = -EOPNOTSUPP;
499                 goto out_unlock;
500         }
501
502         key->hw_key_idx = slot;
503
504 out_unlock:
505         mutex_unlock(&priv->conf_mutex);
506         return ret;
507 }
508
509 static const struct ieee80211_ops p54_ops = {
510         .tx                     = p54_tx_80211,
511         .start                  = p54_start,
512         .stop                   = p54_stop,
513         .add_interface          = p54_add_interface,
514         .remove_interface       = p54_remove_interface,
515         .set_tim                = p54_set_tim,
516         .sta_notify             = p54_sta_notify,
517         .sta_add                = p54_sta_add_remove,
518         .sta_remove             = p54_sta_add_remove,
519         .set_key                = p54_set_key,
520         .config                 = p54_config,
521         .bss_info_changed       = p54_bss_info_changed,
522         .configure_filter       = p54_configure_filter,
523         .conf_tx                = p54_conf_tx,
524         .get_stats              = p54_get_stats,
525 };
526
527 struct ieee80211_hw *p54_init_common(size_t priv_data_len)
528 {
529         struct ieee80211_hw *dev;
530         struct p54_common *priv;
531
532         dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
533         if (!dev)
534                 return NULL;
535
536         priv = dev->priv;
537         priv->hw = dev;
538         priv->mode = NL80211_IFTYPE_UNSPECIFIED;
539         priv->basic_rate_mask = 0x15f;
540         spin_lock_init(&priv->tx_stats_lock);
541         skb_queue_head_init(&priv->tx_queue);
542         skb_queue_head_init(&priv->tx_pending);
543         dev->flags = IEEE80211_HW_RX_INCLUDES_FCS |
544                      IEEE80211_HW_SIGNAL_DBM |
545                      IEEE80211_HW_SUPPORTS_PS |
546                      IEEE80211_HW_PS_NULLFUNC_STACK |
547                      IEEE80211_HW_BEACON_FILTER |
548                      IEEE80211_HW_NOISE_DBM;
549
550         dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
551                                       BIT(NL80211_IFTYPE_ADHOC) |
552                                       BIT(NL80211_IFTYPE_AP) |
553                                       BIT(NL80211_IFTYPE_MESH_POINT);
554
555         dev->channel_change_time = 1000;        /* TODO: find actual value */
556         priv->beacon_req_id = cpu_to_le32(0);
557         priv->tx_stats[P54_QUEUE_BEACON].limit = 1;
558         priv->tx_stats[P54_QUEUE_FWSCAN].limit = 1;
559         priv->tx_stats[P54_QUEUE_MGMT].limit = 3;
560         priv->tx_stats[P54_QUEUE_CAB].limit = 3;
561         priv->tx_stats[P54_QUEUE_DATA].limit = 5;
562         dev->queues = 1;
563         priv->noise = -94;
564         /*
565          * We support at most 8 tries no matter which rate they're at,
566          * we cannot support max_rates * max_rate_tries as we set it
567          * here, but setting it correctly to 4/2 or so would limit us
568          * artificially if the RC algorithm wants just two rates, so
569          * let's say 4/7, we'll redistribute it at TX time, see the
570          * comments there.
571          */
572         dev->max_rates = 4;
573         dev->max_rate_tries = 7;
574         dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
575                                  sizeof(struct p54_tx_data);
576
577         /*
578          * For now, disable PS by default because it affects
579          * link stability significantly.
580          */
581         dev->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
582
583         mutex_init(&priv->conf_mutex);
584         mutex_init(&priv->eeprom_mutex);
585         init_completion(&priv->eeprom_comp);
586         init_completion(&priv->beacon_comp);
587         INIT_DELAYED_WORK(&priv->work, p54_work);
588
589         return dev;
590 }
591 EXPORT_SYMBOL_GPL(p54_init_common);
592
593 int p54_register_common(struct ieee80211_hw *dev, struct device *pdev)
594 {
595         struct p54_common *priv = dev->priv;
596         int err;
597
598         err = ieee80211_register_hw(dev);
599         if (err) {
600                 dev_err(pdev, "Cannot register device (%d).\n", err);
601                 return err;
602         }
603
604 #ifdef CONFIG_P54_LEDS
605         err = p54_init_leds(priv);
606         if (err)
607                 return err;
608 #endif /* CONFIG_P54_LEDS */
609
610         dev_info(pdev, "is registered as '%s'\n", wiphy_name(dev->wiphy));
611         return 0;
612 }
613 EXPORT_SYMBOL_GPL(p54_register_common);
614
615 void p54_free_common(struct ieee80211_hw *dev)
616 {
617         struct p54_common *priv = dev->priv;
618         unsigned int i;
619
620         for (i = 0; i < IEEE80211_NUM_BANDS; i++)
621                 kfree(priv->band_table[i]);
622
623         kfree(priv->iq_autocal);
624         kfree(priv->output_limit);
625         kfree(priv->curve_data);
626         kfree(priv->used_rxkeys);
627         priv->iq_autocal = NULL;
628         priv->output_limit = NULL;
629         priv->curve_data = NULL;
630         priv->used_rxkeys = NULL;
631         ieee80211_free_hw(dev);
632 }
633 EXPORT_SYMBOL_GPL(p54_free_common);
634
635 void p54_unregister_common(struct ieee80211_hw *dev)
636 {
637         struct p54_common *priv = dev->priv;
638
639 #ifdef CONFIG_P54_LEDS
640         p54_unregister_leds(priv);
641 #endif /* CONFIG_P54_LEDS */
642
643         ieee80211_unregister_hw(dev);
644         mutex_destroy(&priv->conf_mutex);
645         mutex_destroy(&priv->eeprom_mutex);
646 }
647 EXPORT_SYMBOL_GPL(p54_unregister_common);