]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/wireless/p54/main.c
mac80211: make beacon filtering per virtual interface
[karo-tx-linux.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/slab.h>
21 #include <linux/firmware.h>
22 #include <linux/etherdevice.h>
23 #include <linux/module.h>
24
25 #include <net/mac80211.h>
26
27 #include "p54.h"
28 #include "lmac.h"
29
30 static bool modparam_nohwcrypt;
31 module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
32 MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
33 MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
34 MODULE_DESCRIPTION("Softmac Prism54 common code");
35 MODULE_LICENSE("GPL");
36 MODULE_ALIAS("prism54common");
37
38 static int p54_sta_add_remove(struct ieee80211_hw *hw,
39                               struct ieee80211_vif *vif,
40                               struct ieee80211_sta *sta)
41 {
42         struct p54_common *priv = hw->priv;
43
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
51         return 0;
52 }
53
54 static void p54_sta_notify(struct ieee80211_hw *dev, struct ieee80211_vif *vif,
55                               enum sta_notify_cmd notify_cmd,
56                               struct ieee80211_sta *sta)
57 {
58         struct p54_common *priv = dev->priv;
59
60         switch (notify_cmd) {
61         case STA_NOTIFY_AWAKE:
62                 /* update the firmware's filter table */
63                 p54_sta_unlock(priv, sta->addr);
64                 break;
65         default:
66                 break;
67         }
68 }
69
70 static int p54_set_tim(struct ieee80211_hw *dev, struct ieee80211_sta *sta,
71                         bool set)
72 {
73         struct p54_common *priv = dev->priv;
74
75         return p54_update_beacon_tim(priv, sta->aid, set);
76 }
77
78 u8 *p54_find_ie(struct sk_buff *skb, u8 ie)
79 {
80         struct ieee80211_mgmt *mgmt = (void *)skb->data;
81         u8 *pos, *end;
82
83         if (skb->len <= sizeof(mgmt))
84                 return NULL;
85
86         pos = (u8 *)mgmt->u.beacon.variable;
87         end = skb->data + skb->len;
88         while (pos < end) {
89                 if (pos + 2 + pos[1] > end)
90                         return NULL;
91
92                 if (pos[0] == ie)
93                         return pos;
94
95                 pos += 2 + pos[1];
96         }
97         return NULL;
98 }
99
100 static int p54_beacon_format_ie_tim(struct sk_buff *skb)
101 {
102         /*
103          * the good excuse for this mess is ... the firmware.
104          * The dummy TIM MUST be at the end of the beacon frame,
105          * because it'll be overwritten!
106          */
107         u8 *tim;
108         u8 dtim_len;
109         u8 dtim_period;
110         u8 *next;
111
112         tim = p54_find_ie(skb, WLAN_EID_TIM);
113         if (!tim)
114                 return 0;
115
116         dtim_len = tim[1];
117         dtim_period = tim[3];
118         next = tim + 2 + dtim_len;
119
120         if (dtim_len < 3)
121                 return -EINVAL;
122
123         memmove(tim, next, skb_tail_pointer(skb) - next);
124         tim = skb_tail_pointer(skb) - (dtim_len + 2);
125
126         /* add the dummy at the end */
127         tim[0] = WLAN_EID_TIM;
128         tim[1] = 3;
129         tim[2] = 0;
130         tim[3] = dtim_period;
131         tim[4] = 0;
132
133         if (dtim_len > 3)
134                 skb_trim(skb, skb->len - (dtim_len - 3));
135
136         return 0;
137 }
138
139 static int p54_beacon_update(struct p54_common *priv,
140                         struct ieee80211_vif *vif)
141 {
142         struct sk_buff *beacon;
143         int ret;
144
145         beacon = ieee80211_beacon_get(priv->hw, vif);
146         if (!beacon)
147                 return -ENOMEM;
148         ret = p54_beacon_format_ie_tim(beacon);
149         if (ret)
150                 return ret;
151
152         /*
153          * During operation, the firmware takes care of beaconing.
154          * The driver only needs to upload a new beacon template, once
155          * the template was changed by the stack or userspace.
156          *
157          * LMAC API 3.2.2 also specifies that the driver does not need
158          * to cancel the old beacon template by hand, instead the firmware
159          * will release the previous one through the feedback mechanism.
160          */
161         p54_tx_80211(priv->hw, beacon);
162         priv->tsf_high32 = 0;
163         priv->tsf_low32 = 0;
164
165         return 0;
166 }
167
168 static int p54_start(struct ieee80211_hw *dev)
169 {
170         struct p54_common *priv = dev->priv;
171         int err;
172
173         mutex_lock(&priv->conf_mutex);
174         err = priv->open(dev);
175         if (err)
176                 goto out;
177         P54_SET_QUEUE(priv->qos_params[0], 0x0002, 0x0003, 0x0007, 47);
178         P54_SET_QUEUE(priv->qos_params[1], 0x0002, 0x0007, 0x000f, 94);
179         P54_SET_QUEUE(priv->qos_params[2], 0x0003, 0x000f, 0x03ff, 0);
180         P54_SET_QUEUE(priv->qos_params[3], 0x0007, 0x000f, 0x03ff, 0);
181         err = p54_set_edcf(priv);
182         if (err)
183                 goto out;
184
185         memset(priv->bssid, ~0, ETH_ALEN);
186         priv->mode = NL80211_IFTYPE_MONITOR;
187         err = p54_setup_mac(priv);
188         if (err) {
189                 priv->mode = NL80211_IFTYPE_UNSPECIFIED;
190                 goto out;
191         }
192
193         ieee80211_queue_delayed_work(dev, &priv->work, 0);
194
195         priv->softled_state = 0;
196         err = p54_set_leds(priv);
197
198 out:
199         mutex_unlock(&priv->conf_mutex);
200         return err;
201 }
202
203 static void p54_stop(struct ieee80211_hw *dev)
204 {
205         struct p54_common *priv = dev->priv;
206         int i;
207
208         priv->mode = NL80211_IFTYPE_UNSPECIFIED;
209         priv->softled_state = 0;
210         cancel_delayed_work_sync(&priv->work);
211         mutex_lock(&priv->conf_mutex);
212         p54_set_leds(priv);
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         vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
232
233         mutex_lock(&priv->conf_mutex);
234         if (priv->mode != NL80211_IFTYPE_MONITOR) {
235                 mutex_unlock(&priv->conf_mutex);
236                 return -EOPNOTSUPP;
237         }
238
239         priv->vif = vif;
240
241         switch (vif->type) {
242         case NL80211_IFTYPE_STATION:
243         case NL80211_IFTYPE_ADHOC:
244         case NL80211_IFTYPE_AP:
245         case NL80211_IFTYPE_MESH_POINT:
246                 priv->mode = vif->type;
247                 break;
248         default:
249                 mutex_unlock(&priv->conf_mutex);
250                 return -EOPNOTSUPP;
251         }
252
253         memcpy(priv->mac_addr, vif->addr, ETH_ALEN);
254         p54_setup_mac(priv);
255         mutex_unlock(&priv->conf_mutex);
256         return 0;
257 }
258
259 static void p54_remove_interface(struct ieee80211_hw *dev,
260                                  struct ieee80211_vif *vif)
261 {
262         struct p54_common *priv = dev->priv;
263
264         mutex_lock(&priv->conf_mutex);
265         priv->vif = NULL;
266
267         /*
268          * LMAC API 3.2.2 states that any active beacon template must be
269          * canceled by the driver before attempting a mode transition.
270          */
271         if (le32_to_cpu(priv->beacon_req_id) != 0) {
272                 p54_tx_cancel(priv, priv->beacon_req_id);
273                 wait_for_completion_interruptible_timeout(&priv->beacon_comp, HZ);
274         }
275         priv->mode = NL80211_IFTYPE_MONITOR;
276         memset(priv->mac_addr, 0, ETH_ALEN);
277         memset(priv->bssid, 0, ETH_ALEN);
278         p54_setup_mac(priv);
279         mutex_unlock(&priv->conf_mutex);
280 }
281
282 static int p54_wait_for_stats(struct ieee80211_hw *dev)
283 {
284         struct p54_common *priv = dev->priv;
285         int ret;
286
287         priv->update_stats = true;
288         ret = p54_fetch_statistics(priv);
289         if (ret)
290                 return ret;
291
292         ret = wait_for_completion_interruptible_timeout(&priv->stat_comp, HZ);
293         if (ret == 0)
294                 return -ETIMEDOUT;
295
296         return 0;
297 }
298
299 static void p54_reset_stats(struct p54_common *priv)
300 {
301         struct ieee80211_channel *chan = priv->curchan;
302
303         if (chan) {
304                 struct survey_info *info = &priv->survey[chan->hw_value];
305
306                 /* only reset channel statistics, don't touch .filled, etc. */
307                 info->channel_time = 0;
308                 info->channel_time_busy = 0;
309                 info->channel_time_tx = 0;
310         }
311
312         priv->update_stats = true;
313         priv->survey_raw.active = 0;
314         priv->survey_raw.cca = 0;
315         priv->survey_raw.tx = 0;
316 }
317
318 static int p54_config(struct ieee80211_hw *dev, u32 changed)
319 {
320         int ret = 0;
321         struct p54_common *priv = dev->priv;
322         struct ieee80211_conf *conf = &dev->conf;
323
324         mutex_lock(&priv->conf_mutex);
325         if (changed & IEEE80211_CONF_CHANGE_POWER)
326                 priv->output_power = conf->power_level << 2;
327         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
328                 struct ieee80211_channel *oldchan;
329                 WARN_ON(p54_wait_for_stats(dev));
330                 oldchan = priv->curchan;
331                 priv->curchan = NULL;
332                 ret = p54_scan(priv, P54_SCAN_EXIT, 0);
333                 if (ret) {
334                         priv->curchan = oldchan;
335                         goto out;
336                 }
337                 /*
338                  * TODO: Use the LM_SCAN_TRAP to determine the current
339                  * operating channel.
340                  */
341                 priv->curchan = priv->hw->conf.channel;
342                 p54_reset_stats(priv);
343                 WARN_ON(p54_fetch_statistics(priv));
344         }
345         if (changed & IEEE80211_CONF_CHANGE_PS) {
346                 WARN_ON(p54_wait_for_stats(dev));
347                 ret = p54_set_ps(priv);
348                 if (ret)
349                         goto out;
350                 WARN_ON(p54_wait_for_stats(dev));
351         }
352         if (changed & IEEE80211_CONF_CHANGE_IDLE) {
353                 WARN_ON(p54_wait_for_stats(dev));
354                 ret = p54_setup_mac(priv);
355                 if (ret)
356                         goto out;
357                 WARN_ON(p54_wait_for_stats(dev));
358         }
359
360 out:
361         mutex_unlock(&priv->conf_mutex);
362         return ret;
363 }
364
365 static u64 p54_prepare_multicast(struct ieee80211_hw *dev,
366                                  struct netdev_hw_addr_list *mc_list)
367 {
368         struct p54_common *priv = dev->priv;
369         struct netdev_hw_addr *ha;
370         int i;
371
372         BUILD_BUG_ON(ARRAY_SIZE(priv->mc_maclist) !=
373                 ARRAY_SIZE(((struct p54_group_address_table *)NULL)->mac_list));
374         /*
375          * The first entry is reserved for the global broadcast MAC.
376          * Otherwise the firmware will drop it and ARP will no longer work.
377          */
378         i = 1;
379         priv->mc_maclist_num = netdev_hw_addr_list_count(mc_list) + i;
380         netdev_hw_addr_list_for_each(ha, mc_list) {
381                 memcpy(&priv->mc_maclist[i], ha->addr, ETH_ALEN);
382                 i++;
383                 if (i >= ARRAY_SIZE(priv->mc_maclist))
384                         break;
385         }
386
387         return 1; /* update */
388 }
389
390 static void p54_configure_filter(struct ieee80211_hw *dev,
391                                  unsigned int changed_flags,
392                                  unsigned int *total_flags,
393                                  u64 multicast)
394 {
395         struct p54_common *priv = dev->priv;
396
397         *total_flags &= FIF_PROMISC_IN_BSS |
398                         FIF_ALLMULTI |
399                         FIF_OTHER_BSS;
400
401         priv->filter_flags = *total_flags;
402
403         if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS))
404                 p54_setup_mac(priv);
405
406         if (changed_flags & FIF_ALLMULTI || multicast)
407                 p54_set_groupfilter(priv);
408 }
409
410 static int p54_conf_tx(struct ieee80211_hw *dev,
411                        struct ieee80211_vif *vif, u16 queue,
412                        const struct ieee80211_tx_queue_params *params)
413 {
414         struct p54_common *priv = dev->priv;
415         int ret;
416
417         mutex_lock(&priv->conf_mutex);
418         if (queue < dev->queues) {
419                 P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
420                         params->cw_min, params->cw_max, params->txop);
421                 ret = p54_set_edcf(priv);
422         } else
423                 ret = -EINVAL;
424         mutex_unlock(&priv->conf_mutex);
425         return ret;
426 }
427
428 static void p54_work(struct work_struct *work)
429 {
430         struct p54_common *priv = container_of(work, struct p54_common,
431                                                work.work);
432
433         if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
434                 return ;
435
436         /*
437          * TODO: walk through tx_queue and do the following tasks
438          *      1. initiate bursts.
439          *      2. cancel stuck frames / reset the device if necessary.
440          */
441
442         mutex_lock(&priv->conf_mutex);
443         WARN_ON_ONCE(p54_fetch_statistics(priv));
444         mutex_unlock(&priv->conf_mutex);
445 }
446
447 static int p54_get_stats(struct ieee80211_hw *dev,
448                          struct ieee80211_low_level_stats *stats)
449 {
450         struct p54_common *priv = dev->priv;
451
452         memcpy(stats, &priv->stats, sizeof(*stats));
453         return 0;
454 }
455
456 static void p54_bss_info_changed(struct ieee80211_hw *dev,
457                                  struct ieee80211_vif *vif,
458                                  struct ieee80211_bss_conf *info,
459                                  u32 changed)
460 {
461         struct p54_common *priv = dev->priv;
462
463         mutex_lock(&priv->conf_mutex);
464         if (changed & BSS_CHANGED_BSSID) {
465                 memcpy(priv->bssid, info->bssid, ETH_ALEN);
466                 p54_setup_mac(priv);
467         }
468
469         if (changed & BSS_CHANGED_BEACON) {
470                 p54_scan(priv, P54_SCAN_EXIT, 0);
471                 p54_setup_mac(priv);
472                 p54_beacon_update(priv, vif);
473                 p54_set_edcf(priv);
474         }
475
476         if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_BEACON)) {
477                 priv->use_short_slot = info->use_short_slot;
478                 p54_set_edcf(priv);
479         }
480         if (changed & BSS_CHANGED_BASIC_RATES) {
481                 if (dev->conf.channel->band == IEEE80211_BAND_5GHZ)
482                         priv->basic_rate_mask = (info->basic_rates << 4);
483                 else
484                         priv->basic_rate_mask = info->basic_rates;
485                 p54_setup_mac(priv);
486                 if (priv->fw_var >= 0x500)
487                         p54_scan(priv, P54_SCAN_EXIT, 0);
488         }
489         if (changed & BSS_CHANGED_ASSOC) {
490                 if (info->assoc) {
491                         priv->aid = info->aid;
492                         priv->wakeup_timer = info->beacon_int *
493                                              info->dtim_period * 5;
494                         p54_setup_mac(priv);
495                 } else {
496                         priv->wakeup_timer = 500;
497                         priv->aid = 0;
498                 }
499         }
500
501         mutex_unlock(&priv->conf_mutex);
502 }
503
504 static int p54_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
505                        struct ieee80211_vif *vif, struct ieee80211_sta *sta,
506                        struct ieee80211_key_conf *key)
507 {
508         struct p54_common *priv = dev->priv;
509         int slot, ret = 0;
510         u8 algo = 0;
511         u8 *addr = NULL;
512
513         if (modparam_nohwcrypt)
514                 return -EOPNOTSUPP;
515
516         mutex_lock(&priv->conf_mutex);
517         if (cmd == SET_KEY) {
518                 switch (key->cipher) {
519                 case WLAN_CIPHER_SUITE_TKIP:
520                         if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL |
521                               BR_DESC_PRIV_CAP_TKIP))) {
522                                 ret = -EOPNOTSUPP;
523                                 goto out_unlock;
524                         }
525                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
526                         algo = P54_CRYPTO_TKIPMICHAEL;
527                         break;
528                 case WLAN_CIPHER_SUITE_WEP40:
529                 case WLAN_CIPHER_SUITE_WEP104:
530                         if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP)) {
531                                 ret = -EOPNOTSUPP;
532                                 goto out_unlock;
533                         }
534                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
535                         algo = P54_CRYPTO_WEP;
536                         break;
537                 case WLAN_CIPHER_SUITE_CCMP:
538                         if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP)) {
539                                 ret = -EOPNOTSUPP;
540                                 goto out_unlock;
541                         }
542                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
543                         algo = P54_CRYPTO_AESCCMP;
544                         break;
545                 default:
546                         ret = -EOPNOTSUPP;
547                         goto out_unlock;
548                 }
549                 slot = bitmap_find_free_region(priv->used_rxkeys,
550                                                priv->rx_keycache_size, 0);
551
552                 if (slot < 0) {
553                         /*
554                          * The device supports the chosen algorithm, but the
555                          * firmware does not provide enough key slots to store
556                          * all of them.
557                          * But encryption offload for outgoing frames is always
558                          * possible, so we just pretend that the upload was
559                          * successful and do the decryption in software.
560                          */
561
562                         /* mark the key as invalid. */
563                         key->hw_key_idx = 0xff;
564                         goto out_unlock;
565                 }
566         } else {
567                 slot = key->hw_key_idx;
568
569                 if (slot == 0xff) {
570                         /* This key was not uploaded into the rx key cache. */
571
572                         goto out_unlock;
573                 }
574
575                 bitmap_release_region(priv->used_rxkeys, slot, 0);
576                 algo = 0;
577         }
578
579         if (sta)
580                 addr = sta->addr;
581
582         ret = p54_upload_key(priv, algo, slot, key->keyidx,
583                              key->keylen, addr, key->key);
584         if (ret) {
585                 bitmap_release_region(priv->used_rxkeys, slot, 0);
586                 ret = -EOPNOTSUPP;
587                 goto out_unlock;
588         }
589
590         key->hw_key_idx = slot;
591
592 out_unlock:
593         mutex_unlock(&priv->conf_mutex);
594         return ret;
595 }
596
597 static int p54_get_survey(struct ieee80211_hw *dev, int idx,
598                                 struct survey_info *survey)
599 {
600         struct p54_common *priv = dev->priv;
601         struct ieee80211_channel *chan;
602         int err, tries;
603         bool in_use = false;
604
605         if (idx >= priv->chan_num)
606                 return -ENOENT;
607
608 #define MAX_TRIES 1
609         for (tries = 0; tries < MAX_TRIES; tries++) {
610                 chan = priv->curchan;
611                 if (chan && chan->hw_value == idx) {
612                         mutex_lock(&priv->conf_mutex);
613                         err = p54_wait_for_stats(dev);
614                         mutex_unlock(&priv->conf_mutex);
615                         if (err)
616                                 return err;
617
618                         in_use = true;
619                 }
620
621                 memcpy(survey, &priv->survey[idx], sizeof(*survey));
622
623                 if (in_use) {
624                         /* test if the reported statistics are valid. */
625                         if  (survey->channel_time != 0) {
626                                 survey->filled |= SURVEY_INFO_IN_USE;
627                         } else {
628                                 /*
629                                  * hw/fw has not accumulated enough sample sets.
630                                  * Wait for 100ms, this ought to be enough to
631                                  * to get at least one non-null set of channel
632                                  * usage statistics.
633                                  */
634                                 msleep(100);
635                                 continue;
636                         }
637                 }
638                 return 0;
639         }
640         return -ETIMEDOUT;
641 #undef MAX_TRIES
642 }
643
644 static unsigned int p54_flush_count(struct p54_common *priv)
645 {
646         unsigned int total = 0, i;
647
648         BUILD_BUG_ON(P54_QUEUE_NUM > ARRAY_SIZE(priv->tx_stats));
649
650         /*
651          * Because the firmware has the sole control over any frames
652          * in the P54_QUEUE_BEACON or P54_QUEUE_SCAN queues, they
653          * don't really count as pending or active.
654          */
655         for (i = P54_QUEUE_MGMT; i < P54_QUEUE_NUM; i++)
656                 total += priv->tx_stats[i].len;
657         return total;
658 }
659
660 static void p54_flush(struct ieee80211_hw *dev, bool drop)
661 {
662         struct p54_common *priv = dev->priv;
663         unsigned int total, i;
664
665         /*
666          * Currently, it wouldn't really matter if we wait for one second
667          * or 15 minutes. But once someone gets around and completes the
668          * TODOs [ancel stuck frames / reset device] in p54_work, it will
669          * suddenly make sense to wait that long.
670          */
671         i = P54_STATISTICS_UPDATE * 2 / 20;
672
673         /*
674          * In this case no locking is required because as we speak the
675          * queues have already been stopped and no new frames can sneak
676          * up from behind.
677          */
678         while ((total = p54_flush_count(priv) && i--)) {
679                 /* waste time */
680                 msleep(20);
681         }
682
683         WARN(total, "tx flush timeout, unresponsive firmware");
684 }
685
686 static void p54_set_coverage_class(struct ieee80211_hw *dev, u8 coverage_class)
687 {
688         struct p54_common *priv = dev->priv;
689
690         mutex_lock(&priv->conf_mutex);
691         /* support all coverage class values as in 802.11-2007 Table 7-27 */
692         priv->coverage_class = clamp_t(u8, coverage_class, 0, 31);
693         p54_set_edcf(priv);
694         mutex_unlock(&priv->conf_mutex);
695 }
696
697 static const struct ieee80211_ops p54_ops = {
698         .tx                     = p54_tx_80211,
699         .start                  = p54_start,
700         .stop                   = p54_stop,
701         .add_interface          = p54_add_interface,
702         .remove_interface       = p54_remove_interface,
703         .set_tim                = p54_set_tim,
704         .sta_notify             = p54_sta_notify,
705         .sta_add                = p54_sta_add_remove,
706         .sta_remove             = p54_sta_add_remove,
707         .set_key                = p54_set_key,
708         .config                 = p54_config,
709         .flush                  = p54_flush,
710         .bss_info_changed       = p54_bss_info_changed,
711         .prepare_multicast      = p54_prepare_multicast,
712         .configure_filter       = p54_configure_filter,
713         .conf_tx                = p54_conf_tx,
714         .get_stats              = p54_get_stats,
715         .get_survey             = p54_get_survey,
716         .set_coverage_class     = p54_set_coverage_class,
717 };
718
719 struct ieee80211_hw *p54_init_common(size_t priv_data_len)
720 {
721         struct ieee80211_hw *dev;
722         struct p54_common *priv;
723
724         dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
725         if (!dev)
726                 return NULL;
727
728         priv = dev->priv;
729         priv->hw = dev;
730         priv->mode = NL80211_IFTYPE_UNSPECIFIED;
731         priv->basic_rate_mask = 0x15f;
732         spin_lock_init(&priv->tx_stats_lock);
733         skb_queue_head_init(&priv->tx_queue);
734         skb_queue_head_init(&priv->tx_pending);
735         dev->flags = IEEE80211_HW_RX_INCLUDES_FCS |
736                      IEEE80211_HW_SIGNAL_DBM |
737                      IEEE80211_HW_SUPPORTS_PS |
738                      IEEE80211_HW_PS_NULLFUNC_STACK |
739                      IEEE80211_HW_REPORTS_TX_ACK_STATUS;
740
741         dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
742                                       BIT(NL80211_IFTYPE_ADHOC) |
743                                       BIT(NL80211_IFTYPE_AP) |
744                                       BIT(NL80211_IFTYPE_MESH_POINT);
745
746         dev->channel_change_time = 1000;        /* TODO: find actual value */
747         priv->beacon_req_id = cpu_to_le32(0);
748         priv->tx_stats[P54_QUEUE_BEACON].limit = 1;
749         priv->tx_stats[P54_QUEUE_FWSCAN].limit = 1;
750         priv->tx_stats[P54_QUEUE_MGMT].limit = 3;
751         priv->tx_stats[P54_QUEUE_CAB].limit = 3;
752         priv->tx_stats[P54_QUEUE_DATA].limit = 5;
753         dev->queues = 1;
754         priv->noise = -94;
755         /*
756          * We support at most 8 tries no matter which rate they're at,
757          * we cannot support max_rates * max_rate_tries as we set it
758          * here, but setting it correctly to 4/2 or so would limit us
759          * artificially if the RC algorithm wants just two rates, so
760          * let's say 4/7, we'll redistribute it at TX time, see the
761          * comments there.
762          */
763         dev->max_rates = 4;
764         dev->max_rate_tries = 7;
765         dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
766                                  sizeof(struct p54_tx_data);
767
768         /*
769          * For now, disable PS by default because it affects
770          * link stability significantly.
771          */
772         dev->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
773
774         mutex_init(&priv->conf_mutex);
775         mutex_init(&priv->eeprom_mutex);
776         init_completion(&priv->stat_comp);
777         init_completion(&priv->eeprom_comp);
778         init_completion(&priv->beacon_comp);
779         INIT_DELAYED_WORK(&priv->work, p54_work);
780
781         memset(&priv->mc_maclist[0], ~0, ETH_ALEN);
782         priv->curchan = NULL;
783         p54_reset_stats(priv);
784         return dev;
785 }
786 EXPORT_SYMBOL_GPL(p54_init_common);
787
788 int p54_register_common(struct ieee80211_hw *dev, struct device *pdev)
789 {
790         struct p54_common __maybe_unused *priv = dev->priv;
791         int err;
792
793         err = ieee80211_register_hw(dev);
794         if (err) {
795                 dev_err(pdev, "Cannot register device (%d).\n", err);
796                 return err;
797         }
798
799 #ifdef CONFIG_P54_LEDS
800         err = p54_init_leds(priv);
801         if (err)
802                 return err;
803 #endif /* CONFIG_P54_LEDS */
804
805         dev_info(pdev, "is registered as '%s'\n", wiphy_name(dev->wiphy));
806         return 0;
807 }
808 EXPORT_SYMBOL_GPL(p54_register_common);
809
810 void p54_free_common(struct ieee80211_hw *dev)
811 {
812         struct p54_common *priv = dev->priv;
813         unsigned int i;
814
815         for (i = 0; i < IEEE80211_NUM_BANDS; i++)
816                 kfree(priv->band_table[i]);
817
818         kfree(priv->iq_autocal);
819         kfree(priv->output_limit);
820         kfree(priv->curve_data);
821         kfree(priv->rssi_db);
822         kfree(priv->used_rxkeys);
823         kfree(priv->survey);
824         priv->iq_autocal = NULL;
825         priv->output_limit = NULL;
826         priv->curve_data = NULL;
827         priv->rssi_db = NULL;
828         priv->used_rxkeys = NULL;
829         priv->survey = NULL;
830         ieee80211_free_hw(dev);
831 }
832 EXPORT_SYMBOL_GPL(p54_free_common);
833
834 void p54_unregister_common(struct ieee80211_hw *dev)
835 {
836         struct p54_common *priv = dev->priv;
837
838 #ifdef CONFIG_P54_LEDS
839         p54_unregister_leds(priv);
840 #endif /* CONFIG_P54_LEDS */
841
842         ieee80211_unregister_hw(dev);
843         mutex_destroy(&priv->conf_mutex);
844         mutex_destroy(&priv->eeprom_mutex);
845 }
846 EXPORT_SYMBOL_GPL(p54_unregister_common);