Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[pandora-kernel.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 void p54_sta_notify(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
37                               enum sta_notify_cmd notify_cmd,
38                               struct ieee80211_sta *sta)
39 {
40         struct p54_common *priv = dev->priv;
41         switch (notify_cmd) {
42         case STA_NOTIFY_ADD:
43         case STA_NOTIFY_REMOVE:
44                 /*
45                  * Notify the firmware that we don't want or we don't
46                  * need to buffer frames for this station anymore.
47                  */
48
49                 p54_sta_unlock(priv, sta->addr);
50                 break;
51         case STA_NOTIFY_AWAKE:
52                 /* update the firmware's filter table */
53                 p54_sta_unlock(priv, sta->addr);
54                 break;
55         default:
56                 break;
57         }
58 }
59
60 static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
61                         bool set)
62 {
63         struct p54_common *priv = dev->priv;
64
65         return p54_update_beacon_tim(priv, sta->aid, set);
66 }
67
68 static int p54_beacon_format_ie_tim(struct sk_buff *skb)
69 {
70         /*
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!
74          */
75
76         struct ieee80211_mgmt *mgmt = (void *)skb->data;
77         u8 *pos, *end;
78
79         if (skb->len <= sizeof(mgmt))
80                 return -EINVAL;
81
82         pos = (u8 *)mgmt->u.beacon.variable;
83         end = skb->data + skb->len;
84         while (pos < end) {
85                 if (pos + 2 + pos[1] > end)
86                         return -EINVAL;
87
88                 if (pos[0] == WLAN_EID_TIM) {
89                         u8 dtim_len = pos[1];
90                         u8 dtim_period = pos[3];
91                         u8 *next = pos + 2 + dtim_len;
92
93                         if (dtim_len < 3)
94                                 return -EINVAL;
95
96                         memmove(pos, next, end - next);
97
98                         if (dtim_len > 3)
99                                 skb_trim(skb, skb->len - (dtim_len - 3));
100
101                         pos = end - (dtim_len + 2);
102
103                         /* add the dummy at the end */
104                         pos[0] = WLAN_EID_TIM;
105                         pos[1] = 3;
106                         pos[2] = 0;
107                         pos[3] = dtim_period;
108                         pos[4] = 0;
109                         return 0;
110                 }
111                 pos += 2 + pos[1];
112         }
113         return 0;
114 }
115
116 static int p54_beacon_update(struct p54_common *priv,
117                         struct ieee80211_vif *vif)
118 {
119         struct sk_buff *beacon;
120         __le32 old_beacon_req_id;
121         int ret;
122
123         beacon = ieee80211_beacon_get(priv->hw, vif);
124         if (!beacon)
125                 return -ENOMEM;
126         ret = p54_beacon_format_ie_tim(beacon);
127         if (ret)
128                 return ret;
129
130         old_beacon_req_id = priv->beacon_req_id;
131         priv->beacon_req_id = GET_REQ_ID(beacon);
132
133         ret = p54_tx_80211(priv->hw, beacon);
134         if (ret) {
135                 priv->beacon_req_id = old_beacon_req_id;
136                 return -ENOSPC;
137         }
138
139         priv->tsf_high32 = 0;
140         priv->tsf_low32 = 0;
141
142         return 0;
143 }
144
145 static int p54_start(struct ieee80211_hw *dev)
146 {
147         struct p54_common *priv = dev->priv;
148         int err;
149
150         mutex_lock(&priv->conf_mutex);
151         err = priv->open(dev);
152         if (err)
153                 goto out;
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);
159         if (err)
160                 goto out;
161
162         memset(priv->bssid, ~0, ETH_ALEN);
163         priv->mode = NL80211_IFTYPE_MONITOR;
164         err = p54_setup_mac(priv);
165         if (err) {
166                 priv->mode = NL80211_IFTYPE_UNSPECIFIED;
167                 goto out;
168         }
169
170         queue_delayed_work(dev->workqueue, &priv->work, 0);
171
172         priv->softled_state = 0;
173         err = p54_set_leds(priv);
174
175 out:
176         mutex_unlock(&priv->conf_mutex);
177         return err;
178 }
179
180 static void p54_stop(struct ieee80211_hw *dev)
181 {
182         struct p54_common *priv = dev->priv;
183         int i;
184
185         mutex_lock(&priv->conf_mutex);
186         priv->mode = NL80211_IFTYPE_UNSPECIFIED;
187         priv->softled_state = 0;
188         p54_set_leds(priv);
189
190         cancel_delayed_work_sync(&priv->work);
191
192         priv->stop(dev);
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;
198         }
199
200         priv->beacon_req_id = cpu_to_le32(0);
201         priv->tsf_high32 = priv->tsf_low32 = 0;
202         mutex_unlock(&priv->conf_mutex);
203 }
204
205 static int p54_add_interface(struct ieee80211_hw *dev,
206                              struct ieee80211_if_init_conf *conf)
207 {
208         struct p54_common *priv = dev->priv;
209
210         mutex_lock(&priv->conf_mutex);
211         if (priv->mode != NL80211_IFTYPE_MONITOR) {
212                 mutex_unlock(&priv->conf_mutex);
213                 return -EOPNOTSUPP;
214         }
215
216         priv->vif = conf->vif;
217
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;
224                 break;
225         default:
226                 mutex_unlock(&priv->conf_mutex);
227                 return -EOPNOTSUPP;
228         }
229
230         memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
231         p54_setup_mac(priv);
232         mutex_unlock(&priv->conf_mutex);
233         return 0;
234 }
235
236 static void p54_remove_interface(struct ieee80211_hw *dev,
237                                  struct ieee80211_if_init_conf *conf)
238 {
239         struct p54_common *priv = dev->priv;
240
241         mutex_lock(&priv->conf_mutex);
242         priv->vif = NULL;
243         if (priv->beacon_req_id) {
244                 p54_tx_cancel(priv, priv->beacon_req_id);
245                 priv->beacon_req_id = cpu_to_le32(0);
246         }
247         priv->mode = NL80211_IFTYPE_MONITOR;
248         memset(priv->mac_addr, 0, ETH_ALEN);
249         memset(priv->bssid, 0, ETH_ALEN);
250         p54_setup_mac(priv);
251         mutex_unlock(&priv->conf_mutex);
252 }
253
254 static int p54_config(struct ieee80211_hw *dev, u32 changed)
255 {
256         int ret = 0;
257         struct p54_common *priv = dev->priv;
258         struct ieee80211_conf *conf = &dev->conf;
259
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);
265                 if (ret)
266                         goto out;
267         }
268         if (changed & IEEE80211_CONF_CHANGE_PS) {
269                 ret = p54_set_ps(priv);
270                 if (ret)
271                         goto out;
272         }
273
274 out:
275         mutex_unlock(&priv->conf_mutex);
276         return ret;
277 }
278
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)
283 {
284         struct p54_common *priv = dev->priv;
285
286         *total_flags &= FIF_PROMISC_IN_BSS |
287                         FIF_OTHER_BSS;
288
289         priv->filter_flags = *total_flags;
290
291         if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS))
292                 p54_setup_mac(priv);
293 }
294
295 static int p54_conf_tx(struct ieee80211_hw *dev, u16 queue,
296                        const struct ieee80211_tx_queue_params *params)
297 {
298         struct p54_common *priv = dev->priv;
299         int ret;
300
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);
306         } else
307                 ret = -EINVAL;
308         mutex_unlock(&priv->conf_mutex);
309         return ret;
310 }
311
312 static void p54_work(struct work_struct *work)
313 {
314         struct p54_common *priv = container_of(work, struct p54_common,
315                                                work.work);
316
317         if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
318                 return ;
319
320         /*
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.
324          */
325
326         p54_fetch_statistics(priv);
327 }
328
329 static int p54_get_stats(struct ieee80211_hw *dev,
330                          struct ieee80211_low_level_stats *stats)
331 {
332         struct p54_common *priv = dev->priv;
333
334         memcpy(stats, &priv->stats, sizeof(*stats));
335         return 0;
336 }
337
338 static int p54_get_tx_stats(struct ieee80211_hw *dev,
339                             struct ieee80211_tx_queue_stats *stats)
340 {
341         struct p54_common *priv = dev->priv;
342
343         memcpy(stats, &priv->tx_stats[P54_QUEUE_DATA],
344                sizeof(stats[0]) * dev->queues);
345         return 0;
346 }
347
348 static void p54_bss_info_changed(struct ieee80211_hw *dev,
349                                  struct ieee80211_vif *vif,
350                                  struct ieee80211_bss_conf *info,
351                                  u32 changed)
352 {
353         struct p54_common *priv = dev->priv;
354
355         mutex_lock(&priv->conf_mutex);
356         if (changed & BSS_CHANGED_BSSID) {
357                 memcpy(priv->bssid, info->bssid, ETH_ALEN);
358                 p54_setup_mac(priv);
359         }
360
361         if (changed & BSS_CHANGED_BEACON) {
362                 p54_scan(priv, P54_SCAN_EXIT, 0);
363                 p54_setup_mac(priv);
364                 p54_beacon_update(priv, vif);
365                 p54_set_edcf(priv);
366         }
367
368         if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_BEACON)) {
369                 priv->use_short_slot = info->use_short_slot;
370                 p54_set_edcf(priv);
371         }
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);
375                 else
376                         priv->basic_rate_mask = info->basic_rates;
377                 p54_setup_mac(priv);
378                 if (priv->fw_var >= 0x500)
379                         p54_scan(priv, P54_SCAN_EXIT, 0);
380         }
381         if (changed & BSS_CHANGED_ASSOC) {
382                 if (info->assoc) {
383                         priv->aid = info->aid;
384                         priv->wakeup_timer = info->beacon_int *
385                                              info->dtim_period * 5;
386                         p54_setup_mac(priv);
387                 }
388         }
389
390         mutex_unlock(&priv->conf_mutex);
391 }
392
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)
396 {
397         struct p54_common *priv = dev->priv;
398         int slot, ret = 0;
399         u8 algo = 0;
400         u8 *addr = NULL;
401
402         if (modparam_nohwcrypt)
403                 return -EOPNOTSUPP;
404
405         mutex_lock(&priv->conf_mutex);
406         if (cmd == SET_KEY) {
407                 switch (key->alg) {
408                 case ALG_TKIP:
409                         if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL |
410                               BR_DESC_PRIV_CAP_TKIP))) {
411                                 ret = -EOPNOTSUPP;
412                                 goto out_unlock;
413                         }
414                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
415                         algo = P54_CRYPTO_TKIPMICHAEL;
416                         break;
417                 case ALG_WEP:
418                         if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP)) {
419                                 ret = -EOPNOTSUPP;
420                                 goto out_unlock;
421                         }
422                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
423                         algo = P54_CRYPTO_WEP;
424                         break;
425                 case ALG_CCMP:
426                         if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP)) {
427                                 ret = -EOPNOTSUPP;
428                                 goto out_unlock;
429                         }
430                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
431                         algo = P54_CRYPTO_AESCCMP;
432                         break;
433                 default:
434                         ret = -EOPNOTSUPP;
435                         goto out_unlock;
436                 }
437                 slot = bitmap_find_free_region(priv->used_rxkeys,
438                                                priv->rx_keycache_size, 0);
439
440                 if (slot < 0) {
441                         /*
442                          * The device supports the choosen algorithm, but the
443                          * firmware does not provide enough key slots to store
444                          * all of them.
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.
448                          */
449
450                         /* mark the key as invalid. */
451                         key->hw_key_idx = 0xff;
452                         goto out_unlock;
453                 }
454         } else {
455                 slot = key->hw_key_idx;
456
457                 if (slot == 0xff) {
458                         /* This key was not uploaded into the rx key cache. */
459
460                         goto out_unlock;
461                 }
462
463                 bitmap_release_region(priv->used_rxkeys, slot, 0);
464                 algo = 0;
465         }
466
467         if (sta)
468                 addr = sta->addr;
469
470         ret = p54_upload_key(priv, algo, slot, key->keyidx,
471                              key->keylen, addr, key->key);
472         if (ret) {
473                 bitmap_release_region(priv->used_rxkeys, slot, 0);
474                 ret = -EOPNOTSUPP;
475                 goto out_unlock;
476         }
477
478         key->hw_key_idx = slot;
479
480 out_unlock:
481         mutex_unlock(&priv->conf_mutex);
482         return ret;
483 }
484
485 static const struct ieee80211_ops p54_ops = {
486         .tx                     = p54_tx_80211,
487         .start                  = p54_start,
488         .stop                   = p54_stop,
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
500 };
501
502 struct ieee80211_hw *p54_init_common(size_t priv_data_len)
503 {
504         struct ieee80211_hw *dev;
505         struct p54_common *priv;
506
507         dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
508         if (!dev)
509                 return NULL;
510
511         priv = dev->priv;
512         priv->hw = dev;
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;
521
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);
526
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;
533         dev->queues = 1;
534         priv->noise = -94;
535         /*
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
541          * comments there.
542          */
543         dev->max_rates = 4;
544         dev->max_rate_tries = 7;
545         dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
546                                  sizeof(struct p54_tx_data);
547
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);
552
553         return dev;
554 }
555 EXPORT_SYMBOL_GPL(p54_init_common);
556
557 int p54_register_common(struct ieee80211_hw *dev, struct device *pdev)
558 {
559         struct p54_common *priv = dev->priv;
560         int err;
561
562         err = ieee80211_register_hw(dev);
563         if (err) {
564                 dev_err(pdev, "Cannot register device (%d).\n", err);
565                 return err;
566         }
567
568 #ifdef CONFIG_P54_LEDS
569         err = p54_init_leds(priv);
570         if (err)
571                 return err;
572 #endif /* CONFIG_P54_LEDS */
573
574         dev_info(pdev, "is registered as '%s'\n", wiphy_name(dev->wiphy));
575         return 0;
576 }
577 EXPORT_SYMBOL_GPL(p54_register_common);
578
579 void p54_free_common(struct ieee80211_hw *dev)
580 {
581         struct p54_common *priv = dev->priv;
582
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);
592 }
593 EXPORT_SYMBOL_GPL(p54_free_common);
594
595 void p54_unregister_common(struct ieee80211_hw *dev)
596 {
597         struct p54_common *priv = dev->priv;
598
599 #ifdef CONFIG_P54_LEDS
600         p54_unregister_leds(priv);
601 #endif /* CONFIG_P54_LEDS */
602
603         ieee80211_unregister_hw(dev);
604         mutex_destroy(&priv->conf_mutex);
605         mutex_destroy(&priv->eeprom_mutex);
606 }
607 EXPORT_SYMBOL_GPL(p54_unregister_common);