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
30static bool modparam_nohwcrypt;
31module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
32MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
33MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");
34MODULE_DESCRIPTION("Softmac Prism54 common code");
35MODULE_LICENSE("GPL");
36MODULE_ALIAS("prism54common");
37
38static 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
54static 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
70static 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
78u8 *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
100static 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
139static 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
168static 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
198out:
199	mutex_unlock(&priv->conf_mutex);
200	return err;
201}
202
203static 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
226static 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
257static 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
280static int p54_wait_for_stats(struct ieee80211_hw *dev)
281{
282	struct p54_common *priv = dev->priv;
283	int ret;
284
285	priv->update_stats = true;
286	ret = p54_fetch_statistics(priv);
287	if (ret)
288		return ret;
289
290	ret = wait_for_completion_interruptible_timeout(&priv->stat_comp, HZ);
291	if (ret == 0)
292		return -ETIMEDOUT;
293
294	return 0;
295}
296
297static void p54_reset_stats(struct p54_common *priv)
298{
299	struct ieee80211_channel *chan = priv->curchan;
300
301	if (chan) {
302		struct survey_info *info = &priv->survey[chan->hw_value];
303
304		/* only reset channel statistics, don't touch .filled, etc. */
305		info->channel_time = 0;
306		info->channel_time_busy = 0;
307		info->channel_time_tx = 0;
308	}
309
310	priv->update_stats = true;
311	priv->survey_raw.active = 0;
312	priv->survey_raw.cca = 0;
313	priv->survey_raw.tx = 0;
314}
315
316static int p54_config(struct ieee80211_hw *dev, u32 changed)
317{
318	int ret = 0;
319	struct p54_common *priv = dev->priv;
320	struct ieee80211_conf *conf = &dev->conf;
321
322	mutex_lock(&priv->conf_mutex);
323	if (changed & IEEE80211_CONF_CHANGE_POWER)
324		priv->output_power = conf->power_level << 2;
325	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
326		struct ieee80211_channel *oldchan;
327		WARN_ON(p54_wait_for_stats(dev));
328		oldchan = priv->curchan;
329		priv->curchan = NULL;
330		ret = p54_scan(priv, P54_SCAN_EXIT, 0);
331		if (ret) {
332			priv->curchan = oldchan;
333			goto out;
334		}
335		/*
336		 * TODO: Use the LM_SCAN_TRAP to determine the current
337		 * operating channel.
338		 */
339		priv->curchan = priv->hw->conf.channel;
340		p54_reset_stats(priv);
341		WARN_ON(p54_fetch_statistics(priv));
342	}
343	if (changed & IEEE80211_CONF_CHANGE_PS) {
344		WARN_ON(p54_wait_for_stats(dev));
345		ret = p54_set_ps(priv);
346		if (ret)
347			goto out;
348		WARN_ON(p54_wait_for_stats(dev));
349	}
350	if (changed & IEEE80211_CONF_CHANGE_IDLE) {
351		WARN_ON(p54_wait_for_stats(dev));
352		ret = p54_setup_mac(priv);
353		if (ret)
354			goto out;
355		WARN_ON(p54_wait_for_stats(dev));
356	}
357
358out:
359	mutex_unlock(&priv->conf_mutex);
360	return ret;
361}
362
363static u64 p54_prepare_multicast(struct ieee80211_hw *dev,
364				 struct netdev_hw_addr_list *mc_list)
365{
366	struct p54_common *priv = dev->priv;
367	struct netdev_hw_addr *ha;
368	int i;
369
370	BUILD_BUG_ON(ARRAY_SIZE(priv->mc_maclist) !=
371		ARRAY_SIZE(((struct p54_group_address_table *)NULL)->mac_list));
372	/*
373	 * The first entry is reserved for the global broadcast MAC.
374	 * Otherwise the firmware will drop it and ARP will no longer work.
375	 */
376	i = 1;
377	priv->mc_maclist_num = netdev_hw_addr_list_count(mc_list) + i;
378	netdev_hw_addr_list_for_each(ha, mc_list) {
379		memcpy(&priv->mc_maclist[i], ha->addr, ETH_ALEN);
380		i++;
381		if (i >= ARRAY_SIZE(priv->mc_maclist))
382			break;
383	}
384
385	return 1; /* update */
386}
387
388static void p54_configure_filter(struct ieee80211_hw *dev,
389				 unsigned int changed_flags,
390				 unsigned int *total_flags,
391				 u64 multicast)
392{
393	struct p54_common *priv = dev->priv;
394
395	*total_flags &= FIF_PROMISC_IN_BSS |
396			FIF_ALLMULTI |
397			FIF_OTHER_BSS;
398
399	priv->filter_flags = *total_flags;
400
401	if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS))
402		p54_setup_mac(priv);
403
404	if (changed_flags & FIF_ALLMULTI || multicast)
405		p54_set_groupfilter(priv);
406}
407
408static int p54_conf_tx(struct ieee80211_hw *dev,
409		       struct ieee80211_vif *vif, u16 queue,
410		       const struct ieee80211_tx_queue_params *params)
411{
412	struct p54_common *priv = dev->priv;
413	int ret;
414
415	mutex_lock(&priv->conf_mutex);
416	if (queue < dev->queues) {
417		P54_SET_QUEUE(priv->qos_params[queue], params->aifs,
418			params->cw_min, params->cw_max, params->txop);
419		ret = p54_set_edcf(priv);
420	} else
421		ret = -EINVAL;
422	mutex_unlock(&priv->conf_mutex);
423	return ret;
424}
425
426static void p54_work(struct work_struct *work)
427{
428	struct p54_common *priv = container_of(work, struct p54_common,
429					       work.work);
430
431	if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
432		return ;
433
434	/*
435	 * TODO: walk through tx_queue and do the following tasks
436	 * 	1. initiate bursts.
437	 *      2. cancel stuck frames / reset the device if necessary.
438	 */
439
440	mutex_lock(&priv->conf_mutex);
441	WARN_ON_ONCE(p54_fetch_statistics(priv));
442	mutex_unlock(&priv->conf_mutex);
443}
444
445static int p54_get_stats(struct ieee80211_hw *dev,
446			 struct ieee80211_low_level_stats *stats)
447{
448	struct p54_common *priv = dev->priv;
449
450	memcpy(stats, &priv->stats, sizeof(*stats));
451	return 0;
452}
453
454static void p54_bss_info_changed(struct ieee80211_hw *dev,
455				 struct ieee80211_vif *vif,
456				 struct ieee80211_bss_conf *info,
457				 u32 changed)
458{
459	struct p54_common *priv = dev->priv;
460
461	mutex_lock(&priv->conf_mutex);
462	if (changed & BSS_CHANGED_BSSID) {
463		memcpy(priv->bssid, info->bssid, ETH_ALEN);
464		p54_setup_mac(priv);
465	}
466
467	if (changed & BSS_CHANGED_BEACON) {
468		p54_scan(priv, P54_SCAN_EXIT, 0);
469		p54_setup_mac(priv);
470		p54_beacon_update(priv, vif);
471		p54_set_edcf(priv);
472	}
473
474	if (changed & (BSS_CHANGED_ERP_SLOT | BSS_CHANGED_BEACON)) {
475		priv->use_short_slot = info->use_short_slot;
476		p54_set_edcf(priv);
477	}
478	if (changed & BSS_CHANGED_BASIC_RATES) {
479		if (dev->conf.channel->band == IEEE80211_BAND_5GHZ)
480			priv->basic_rate_mask = (info->basic_rates << 4);
481		else
482			priv->basic_rate_mask = info->basic_rates;
483		p54_setup_mac(priv);
484		if (priv->fw_var >= 0x500)
485			p54_scan(priv, P54_SCAN_EXIT, 0);
486	}
487	if (changed & BSS_CHANGED_ASSOC) {
488		if (info->assoc) {
489			priv->aid = info->aid;
490			priv->wakeup_timer = info->beacon_int *
491					     info->dtim_period * 5;
492			p54_setup_mac(priv);
493		} else {
494			priv->wakeup_timer = 500;
495			priv->aid = 0;
496		}
497	}
498
499	mutex_unlock(&priv->conf_mutex);
500}
501
502static int p54_set_key(struct ieee80211_hw *dev, enum set_key_cmd cmd,
503		       struct ieee80211_vif *vif, struct ieee80211_sta *sta,
504		       struct ieee80211_key_conf *key)
505{
506	struct p54_common *priv = dev->priv;
507	int slot, ret = 0;
508	u8 algo = 0;
509	u8 *addr = NULL;
510
511	if (modparam_nohwcrypt)
512		return -EOPNOTSUPP;
513
514	mutex_lock(&priv->conf_mutex);
515	if (cmd == SET_KEY) {
516		switch (key->cipher) {
517		case WLAN_CIPHER_SUITE_TKIP:
518			if (!(priv->privacy_caps & (BR_DESC_PRIV_CAP_MICHAEL |
519			      BR_DESC_PRIV_CAP_TKIP))) {
520				ret = -EOPNOTSUPP;
521				goto out_unlock;
522			}
523			key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
524			algo = P54_CRYPTO_TKIPMICHAEL;
525			break;
526		case WLAN_CIPHER_SUITE_WEP40:
527		case WLAN_CIPHER_SUITE_WEP104:
528			if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_WEP)) {
529				ret = -EOPNOTSUPP;
530				goto out_unlock;
531			}
532			key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
533			algo = P54_CRYPTO_WEP;
534			break;
535		case WLAN_CIPHER_SUITE_CCMP:
536			if (!(priv->privacy_caps & BR_DESC_PRIV_CAP_AESCCMP)) {
537				ret = -EOPNOTSUPP;
538				goto out_unlock;
539			}
540			key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
541			algo = P54_CRYPTO_AESCCMP;
542			break;
543		default:
544			ret = -EOPNOTSUPP;
545			goto out_unlock;
546		}
547		slot = bitmap_find_free_region(priv->used_rxkeys,
548					       priv->rx_keycache_size, 0);
549
550		if (slot < 0) {
551			/*
552			 * The device supports the chosen algorithm, but the
553			 * firmware does not provide enough key slots to store
554			 * all of them.
555			 * But encryption offload for outgoing frames is always
556			 * possible, so we just pretend that the upload was
557			 * successful and do the decryption in software.
558			 */
559
560			/* mark the key as invalid. */
561			key->hw_key_idx = 0xff;
562			goto out_unlock;
563		}
564	} else {
565		slot = key->hw_key_idx;
566
567		if (slot == 0xff) {
568			/* This key was not uploaded into the rx key cache. */
569
570			goto out_unlock;
571		}
572
573		bitmap_release_region(priv->used_rxkeys, slot, 0);
574		algo = 0;
575	}
576
577	if (sta)
578		addr = sta->addr;
579
580	ret = p54_upload_key(priv, algo, slot, key->keyidx,
581			     key->keylen, addr, key->key);
582	if (ret) {
583		bitmap_release_region(priv->used_rxkeys, slot, 0);
584		ret = -EOPNOTSUPP;
585		goto out_unlock;
586	}
587
588	key->hw_key_idx = slot;
589
590out_unlock:
591	mutex_unlock(&priv->conf_mutex);
592	return ret;
593}
594
595static int p54_get_survey(struct ieee80211_hw *dev, int idx,
596				struct survey_info *survey)
597{
598	struct p54_common *priv = dev->priv;
599	struct ieee80211_channel *chan;
600	int err, tries;
601	bool in_use = false;
602
603	if (idx >= priv->chan_num)
604		return -ENOENT;
605
606#define MAX_TRIES 1
607	for (tries = 0; tries < MAX_TRIES; tries++) {
608		chan = priv->curchan;
609		if (chan && chan->hw_value == idx) {
610			mutex_lock(&priv->conf_mutex);
611			err = p54_wait_for_stats(dev);
612			mutex_unlock(&priv->conf_mutex);
613			if (err)
614				return err;
615
616			in_use = true;
617		}
618
619		memcpy(survey, &priv->survey[idx], sizeof(*survey));
620
621		if (in_use) {
622			/* test if the reported statistics are valid. */
623			if  (survey->channel_time != 0) {
624				survey->filled |= SURVEY_INFO_IN_USE;
625			} else {
626				/*
627				 * hw/fw has not accumulated enough sample sets.
628				 * Wait for 100ms, this ought to be enough to
629				 * to get at least one non-null set of channel
630				 * usage statistics.
631				 */
632				msleep(100);
633				continue;
634			}
635		}
636		return 0;
637	}
638	return -ETIMEDOUT;
639#undef MAX_TRIES
640}
641
642static unsigned int p54_flush_count(struct p54_common *priv)
643{
644	unsigned int total = 0, i;
645
646	BUILD_BUG_ON(P54_QUEUE_NUM > ARRAY_SIZE(priv->tx_stats));
647
648	/*
649	 * Because the firmware has the sole control over any frames
650	 * in the P54_QUEUE_BEACON or P54_QUEUE_SCAN queues, they
651	 * don't really count as pending or active.
652	 */
653	for (i = P54_QUEUE_MGMT; i < P54_QUEUE_NUM; i++)
654		total += priv->tx_stats[i].len;
655	return total;
656}
657
658static void p54_flush(struct ieee80211_hw *dev, bool drop)
659{
660	struct p54_common *priv = dev->priv;
661	unsigned int total, i;
662
663	/*
664	 * Currently, it wouldn't really matter if we wait for one second
665	 * or 15 minutes. But once someone gets around and completes the
666	 * TODOs [ancel stuck frames / reset device] in p54_work, it will
667	 * suddenly make sense to wait that long.
668	 */
669	i = P54_STATISTICS_UPDATE * 2 / 20;
670
671	/*
672	 * In this case no locking is required because as we speak the
673	 * queues have already been stopped and no new frames can sneak
674	 * up from behind.
675	 */
676	while ((total = p54_flush_count(priv) && i--)) {
677		/* waste time */
678		msleep(20);
679	}
680
681	WARN(total, "tx flush timeout, unresponsive firmware");
682}
683
684static void p54_set_coverage_class(struct ieee80211_hw *dev, u8 coverage_class)
685{
686	struct p54_common *priv = dev->priv;
687
688	mutex_lock(&priv->conf_mutex);
689	/* support all coverage class values as in 802.11-2007 Table 7-27 */
690	priv->coverage_class = clamp_t(u8, coverage_class, 0, 31);
691	p54_set_edcf(priv);
692	mutex_unlock(&priv->conf_mutex);
693}
694
695static const struct ieee80211_ops p54_ops = {
696	.tx			= p54_tx_80211,
697	.start			= p54_start,
698	.stop			= p54_stop,
699	.add_interface		= p54_add_interface,
700	.remove_interface	= p54_remove_interface,
701	.set_tim		= p54_set_tim,
702	.sta_notify		= p54_sta_notify,
703	.sta_add		= p54_sta_add_remove,
704	.sta_remove		= p54_sta_add_remove,
705	.set_key		= p54_set_key,
706	.config			= p54_config,
707	.flush			= p54_flush,
708	.bss_info_changed	= p54_bss_info_changed,
709	.prepare_multicast	= p54_prepare_multicast,
710	.configure_filter	= p54_configure_filter,
711	.conf_tx		= p54_conf_tx,
712	.get_stats		= p54_get_stats,
713	.get_survey		= p54_get_survey,
714	.set_coverage_class	= p54_set_coverage_class,
715};
716
717struct ieee80211_hw *p54_init_common(size_t priv_data_len)
718{
719	struct ieee80211_hw *dev;
720	struct p54_common *priv;
721
722	dev = ieee80211_alloc_hw(priv_data_len, &p54_ops);
723	if (!dev)
724		return NULL;
725
726	priv = dev->priv;
727	priv->hw = dev;
728	priv->mode = NL80211_IFTYPE_UNSPECIFIED;
729	priv->basic_rate_mask = 0x15f;
730	spin_lock_init(&priv->tx_stats_lock);
731	skb_queue_head_init(&priv->tx_queue);
732	skb_queue_head_init(&priv->tx_pending);
733	dev->flags = IEEE80211_HW_RX_INCLUDES_FCS |
734		     IEEE80211_HW_SIGNAL_DBM |
735		     IEEE80211_HW_SUPPORTS_PS |
736		     IEEE80211_HW_PS_NULLFUNC_STACK |
737		     IEEE80211_HW_BEACON_FILTER |
738		     IEEE80211_HW_REPORTS_TX_ACK_STATUS;
739
740	dev->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
741				      BIT(NL80211_IFTYPE_ADHOC) |
742				      BIT(NL80211_IFTYPE_AP) |
743				      BIT(NL80211_IFTYPE_MESH_POINT);
744
745	dev->channel_change_time = 1000;	/* TODO: find actual value */
746	priv->beacon_req_id = cpu_to_le32(0);
747	priv->tx_stats[P54_QUEUE_BEACON].limit = 1;
748	priv->tx_stats[P54_QUEUE_FWSCAN].limit = 1;
749	priv->tx_stats[P54_QUEUE_MGMT].limit = 3;
750	priv->tx_stats[P54_QUEUE_CAB].limit = 3;
751	priv->tx_stats[P54_QUEUE_DATA].limit = 5;
752	dev->queues = 1;
753	priv->noise = -94;
754	/*
755	 * We support at most 8 tries no matter which rate they're at,
756	 * we cannot support max_rates * max_rate_tries as we set it
757	 * here, but setting it correctly to 4/2 or so would limit us
758	 * artificially if the RC algorithm wants just two rates, so
759	 * let's say 4/7, we'll redistribute it at TX time, see the
760	 * comments there.
761	 */
762	dev->max_rates = 4;
763	dev->max_rate_tries = 7;
764	dev->extra_tx_headroom = sizeof(struct p54_hdr) + 4 +
765				 sizeof(struct p54_tx_data);
766
767	/*
768	 * For now, disable PS by default because it affects
769	 * link stability significantly.
770	 */
771	dev->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
772
773	mutex_init(&priv->conf_mutex);
774	mutex_init(&priv->eeprom_mutex);
775	init_completion(&priv->stat_comp);
776	init_completion(&priv->eeprom_comp);
777	init_completion(&priv->beacon_comp);
778	INIT_DELAYED_WORK(&priv->work, p54_work);
779
780	memset(&priv->mc_maclist[0], ~0, ETH_ALEN);
781	priv->curchan = NULL;
782	p54_reset_stats(priv);
783	return dev;
784}
785EXPORT_SYMBOL_GPL(p54_init_common);
786
787int p54_register_common(struct ieee80211_hw *dev, struct device *pdev)
788{
789	struct p54_common __maybe_unused *priv = dev->priv;
790	int err;
791
792	err = ieee80211_register_hw(dev);
793	if (err) {
794		dev_err(pdev, "Cannot register device (%d).\n", err);
795		return err;
796	}
797
798#ifdef CONFIG_P54_LEDS
799	err = p54_init_leds(priv);
800	if (err)
801		return err;
802#endif /* CONFIG_P54_LEDS */
803
804	dev_info(pdev, "is registered as '%s'\n", wiphy_name(dev->wiphy));
805	return 0;
806}
807EXPORT_SYMBOL_GPL(p54_register_common);
808
809void p54_free_common(struct ieee80211_hw *dev)
810{
811	struct p54_common *priv = dev->priv;
812	unsigned int i;
813
814	for (i = 0; i < IEEE80211_NUM_BANDS; i++)
815		kfree(priv->band_table[i]);
816
817	kfree(priv->iq_autocal);
818	kfree(priv->output_limit);
819	kfree(priv->curve_data);
820	kfree(priv->rssi_db);
821	kfree(priv->used_rxkeys);
822	kfree(priv->survey);
823	priv->iq_autocal = NULL;
824	priv->output_limit = NULL;
825	priv->curve_data = NULL;
826	priv->rssi_db = NULL;
827	priv->used_rxkeys = NULL;
828	priv->survey = NULL;
829	ieee80211_free_hw(dev);
830}
831EXPORT_SYMBOL_GPL(p54_free_common);
832
833void p54_unregister_common(struct ieee80211_hw *dev)
834{
835	struct p54_common *priv = dev->priv;
836
837#ifdef CONFIG_P54_LEDS
838	p54_unregister_leds(priv);
839#endif /* CONFIG_P54_LEDS */
840
841	ieee80211_unregister_hw(dev);
842	mutex_destroy(&priv->conf_mutex);
843	mutex_destroy(&priv->eeprom_mutex);
844}
845EXPORT_SYMBOL_GPL(p54_unregister_common);
846