Linux Audio

Check our new training course

Loading...
v3.1
  1/*-
  2 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
  3 * Copyright (c) 2004-2005 Atheros Communications, Inc.
  4 * Copyright (c) 2006 Devicescape Software, Inc.
  5 * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
  6 * Copyright (c) 2007 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
  7 * Copyright (c) 2010 Bruno Randolf <br1@einfach.org>
  8 *
  9 * All rights reserved.
 10 *
 11 * Redistribution and use in source and binary forms, with or without
 12 * modification, are permitted provided that the following conditions
 13 * are met:
 14 * 1. Redistributions of source code must retain the above copyright
 15 *    notice, this list of conditions and the following disclaimer,
 16 *    without modification.
 17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 18 *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
 19 *    redistribution must be conditioned upon including a substantially
 20 *    similar Disclaimer requirement for further binary redistribution.
 21 * 3. Neither the names of the above-listed copyright holders nor the names
 22 *    of any contributors may be used to endorse or promote products derived
 23 *    from this software without specific prior written permission.
 24 *
 25 * Alternatively, this software may be distributed under the terms of the
 26 * GNU General Public License ("GPL") version 2 as published by the Free
 27 * Software Foundation.
 28 *
 29 * NO WARRANTY
 30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 32 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
 33 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 34 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
 35 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 38 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 40 * THE POSSIBILITY OF SUCH DAMAGES.
 41 *
 42 */
 43
 
 
 
 44#include <asm/unaligned.h>
 45
 
 46#include "base.h"
 47#include "reg.h"
 48
 49/********************\
 50* Mac80211 functions *
 51\********************/
 52
 53static void
 54ath5k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
 
 55{
 56	struct ath5k_hw *ah = hw->priv;
 57	u16 qnum = skb_get_queue_mapping(skb);
 58
 59	if (WARN_ON(qnum >= ah->ah_capabilities.cap_queues.q_tx_num)) {
 60		dev_kfree_skb_any(skb);
 61		return;
 62	}
 63
 64	ath5k_tx_queue(hw, skb, &ah->txqs[qnum]);
 65}
 66
 67
 68static int
 69ath5k_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
 70{
 71	struct ath5k_hw *ah = hw->priv;
 72	int ret;
 73	struct ath5k_vif *avf = (void *)vif->drv_priv;
 74
 75	mutex_lock(&ah->lock);
 76
 77	if ((vif->type == NL80211_IFTYPE_AP ||
 78	     vif->type == NL80211_IFTYPE_ADHOC)
 79	    && (ah->num_ap_vifs + ah->num_adhoc_vifs) >= ATH_BCBUF) {
 80		ret = -ELNRNG;
 81		goto end;
 82	}
 83
 84	/* Don't allow other interfaces if one ad-hoc is configured.
 85	 * TODO: Fix the problems with ad-hoc and multiple other interfaces.
 86	 * We would need to operate the HW in ad-hoc mode to allow TSF updates
 87	 * for the IBSS, but this breaks with additional AP or STA interfaces
 88	 * at the moment. */
 89	if (ah->num_adhoc_vifs ||
 90	    (ah->nvifs && vif->type == NL80211_IFTYPE_ADHOC)) {
 91		ATH5K_ERR(ah, "Only one single ad-hoc interface is allowed.\n");
 92		ret = -ELNRNG;
 93		goto end;
 94	}
 95
 96	switch (vif->type) {
 97	case NL80211_IFTYPE_AP:
 98	case NL80211_IFTYPE_STATION:
 99	case NL80211_IFTYPE_ADHOC:
100	case NL80211_IFTYPE_MESH_POINT:
101		avf->opmode = vif->type;
102		break;
103	default:
104		ret = -EOPNOTSUPP;
105		goto end;
106	}
107
108	ah->nvifs++;
109	ATH5K_DBG(ah, ATH5K_DEBUG_MODE, "add interface mode %d\n", avf->opmode);
110
111	/* Assign the vap/adhoc to a beacon xmit slot. */
112	if ((avf->opmode == NL80211_IFTYPE_AP) ||
113	    (avf->opmode == NL80211_IFTYPE_ADHOC) ||
114	    (avf->opmode == NL80211_IFTYPE_MESH_POINT)) {
115		int slot;
116
117		WARN_ON(list_empty(&ah->bcbuf));
118		avf->bbuf = list_first_entry(&ah->bcbuf, struct ath5k_buf,
119					     list);
120		list_del(&avf->bbuf->list);
121
122		avf->bslot = 0;
123		for (slot = 0; slot < ATH_BCBUF; slot++) {
124			if (!ah->bslot[slot]) {
125				avf->bslot = slot;
126				break;
127			}
128		}
129		BUG_ON(ah->bslot[avf->bslot] != NULL);
130		ah->bslot[avf->bslot] = vif;
131		if (avf->opmode == NL80211_IFTYPE_AP)
132			ah->num_ap_vifs++;
133		else if (avf->opmode == NL80211_IFTYPE_ADHOC)
134			ah->num_adhoc_vifs++;
 
 
135	}
136
137	/* Any MAC address is fine, all others are included through the
138	 * filter.
139	 */
140	memcpy(&ah->lladdr, vif->addr, ETH_ALEN);
141	ath5k_hw_set_lladdr(ah, vif->addr);
142
143	memcpy(&avf->lladdr, vif->addr, ETH_ALEN);
144
145	ath5k_update_bssid_mask_and_opmode(ah, vif);
146	ret = 0;
147end:
148	mutex_unlock(&ah->lock);
149	return ret;
150}
151
152
153static void
154ath5k_remove_interface(struct ieee80211_hw *hw,
155		       struct ieee80211_vif *vif)
156{
157	struct ath5k_hw *ah = hw->priv;
158	struct ath5k_vif *avf = (void *)vif->drv_priv;
159	unsigned int i;
160
161	mutex_lock(&ah->lock);
162	ah->nvifs--;
163
164	if (avf->bbuf) {
165		ath5k_txbuf_free_skb(ah, avf->bbuf);
166		list_add_tail(&avf->bbuf->list, &ah->bcbuf);
167		for (i = 0; i < ATH_BCBUF; i++) {
168			if (ah->bslot[i] == vif) {
169				ah->bslot[i] = NULL;
170				break;
171			}
172		}
173		avf->bbuf = NULL;
174	}
175	if (avf->opmode == NL80211_IFTYPE_AP)
176		ah->num_ap_vifs--;
177	else if (avf->opmode == NL80211_IFTYPE_ADHOC)
178		ah->num_adhoc_vifs--;
 
 
179
180	ath5k_update_bssid_mask_and_opmode(ah, NULL);
181	mutex_unlock(&ah->lock);
182}
183
184
185/*
186 * TODO: Phy disable/diversity etc
187 */
188static int
189ath5k_config(struct ieee80211_hw *hw, u32 changed)
190{
191	struct ath5k_hw *ah = hw->priv;
192	struct ieee80211_conf *conf = &hw->conf;
193	int ret = 0;
194	int i;
195
196	mutex_lock(&ah->lock);
197
198	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
199		ret = ath5k_chan_set(ah, conf->channel);
200		if (ret < 0)
201			goto unlock;
202	}
203
204	if ((changed & IEEE80211_CONF_CHANGE_POWER) &&
205	(ah->power_level != conf->power_level)) {
206		ah->power_level = conf->power_level;
207
208		/* Half dB steps */
209		ath5k_hw_set_txpower_limit(ah, (conf->power_level * 2));
210	}
211
212	if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
213		ah->ah_retry_long = conf->long_frame_max_tx_count;
214		ah->ah_retry_short = conf->short_frame_max_tx_count;
215
216		for (i = 0; i < ah->ah_capabilities.cap_queues.q_tx_num; i++)
217			ath5k_hw_set_tx_retry_limits(ah, i);
218	}
219
220	/* TODO:
221	 * 1) Move this on config_interface and handle each case
222	 * separately eg. when we have only one STA vif, use
223	 * AR5K_ANTMODE_SINGLE_AP
224	 *
225	 * 2) Allow the user to change antenna mode eg. when only
226	 * one antenna is present
227	 *
228	 * 3) Allow the user to set default/tx antenna when possible
229	 *
230	 * 4) Default mode should handle 90% of the cases, together
231	 * with fixed a/b and single AP modes we should be able to
232	 * handle 99%. Sectored modes are extreme cases and i still
233	 * haven't found a usage for them. If we decide to support them,
234	 * then we must allow the user to set how many tx antennas we
235	 * have available
236	 */
237	ath5k_hw_set_antenna_mode(ah, ah->ah_ant_mode);
238
239unlock:
240	mutex_unlock(&ah->lock);
241	return ret;
242}
243
244
245static void
246ath5k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
247		       struct ieee80211_bss_conf *bss_conf, u32 changes)
248{
249	struct ath5k_vif *avf = (void *)vif->drv_priv;
250	struct ath5k_hw *ah = hw->priv;
251	struct ath_common *common = ath5k_hw_common(ah);
252	unsigned long flags;
253
254	mutex_lock(&ah->lock);
255
256	if (changes & BSS_CHANGED_BSSID) {
257		/* Cache for later use during resets */
258		memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
259		common->curaid = 0;
260		ath5k_hw_set_bssid(ah);
261		mmiowb();
262	}
263
264	if (changes & BSS_CHANGED_BEACON_INT)
265		ah->bintval = bss_conf->beacon_int;
266
267	if (changes & BSS_CHANGED_ERP_SLOT) {
268		int slot_time;
269
270		ah->ah_short_slot = bss_conf->use_short_slot;
271		slot_time = ath5k_hw_get_default_slottime(ah) +
272			    3 * ah->ah_coverage_class;
273		ath5k_hw_set_ifs_intervals(ah, slot_time);
274	}
275
276	if (changes & BSS_CHANGED_ASSOC) {
277		avf->assoc = bss_conf->assoc;
278		if (bss_conf->assoc)
279			ah->assoc = bss_conf->assoc;
280		else
281			ah->assoc = ath5k_any_vif_assoc(ah);
282
283		if (ah->opmode == NL80211_IFTYPE_STATION)
284			ath5k_set_beacon_filter(hw, ah->assoc);
285		ath5k_hw_set_ledstate(ah, ah->assoc ?
286			AR5K_LED_ASSOC : AR5K_LED_INIT);
287		if (bss_conf->assoc) {
288			ATH5K_DBG(ah, ATH5K_DEBUG_ANY,
289				  "Bss Info ASSOC %d, bssid: %pM\n",
290				  bss_conf->aid, common->curbssid);
291			common->curaid = bss_conf->aid;
292			ath5k_hw_set_bssid(ah);
293			/* Once ANI is available you would start it here */
294		}
295	}
296
297	if (changes & BSS_CHANGED_BEACON) {
298		spin_lock_irqsave(&ah->block, flags);
299		ath5k_beacon_update(hw, vif);
300		spin_unlock_irqrestore(&ah->block, flags);
301	}
302
303	if (changes & BSS_CHANGED_BEACON_ENABLED)
304		ah->enable_beacon = bss_conf->enable_beacon;
305
306	if (changes & (BSS_CHANGED_BEACON | BSS_CHANGED_BEACON_ENABLED |
307		       BSS_CHANGED_BEACON_INT))
308		ath5k_beacon_config(ah);
309
310	mutex_unlock(&ah->lock);
311}
312
313
314static u64
315ath5k_prepare_multicast(struct ieee80211_hw *hw,
316			struct netdev_hw_addr_list *mc_list)
317{
318	u32 mfilt[2], val;
319	u8 pos;
320	struct netdev_hw_addr *ha;
321
322	mfilt[0] = 0;
323	mfilt[1] = 1;
324
325	netdev_hw_addr_list_for_each(ha, mc_list) {
326		/* calculate XOR of eight 6-bit values */
327		val = get_unaligned_le32(ha->addr + 0);
328		pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
329		val = get_unaligned_le32(ha->addr + 3);
330		pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
331		pos &= 0x3f;
332		mfilt[pos / 32] |= (1 << (pos % 32));
333		/* XXX: we might be able to just do this instead,
334		* but not sure, needs testing, if we do use this we'd
335		* need to inform below not to reset the mcast */
336		/* ath5k_hw_set_mcast_filterindex(ah,
337		 *      ha->addr[5]); */
338	}
339
340	return ((u64)(mfilt[1]) << 32) | mfilt[0];
341}
342
343
344/*
345 * o always accept unicast, broadcast, and multicast traffic
346 * o multicast traffic for all BSSIDs will be enabled if mac80211
347 *   says it should be
348 * o maintain current state of phy ofdm or phy cck error reception.
349 *   If the hardware detects any of these type of errors then
350 *   ath5k_hw_get_rx_filter() will pass to us the respective
351 *   hardware filters to be able to receive these type of frames.
352 * o probe request frames are accepted only when operating in
353 *   hostap, adhoc, or monitor modes
354 * o enable promiscuous mode according to the interface state
355 * o accept beacons:
356 *   - when operating in adhoc mode so the 802.11 layer creates
357 *     node table entries for peers,
358 *   - when operating in station mode for collecting rssi data when
359 *     the station is otherwise quiet, or
360 *   - when scanning
361 */
362static void
363ath5k_configure_filter(struct ieee80211_hw *hw, unsigned int changed_flags,
364		       unsigned int *new_flags, u64 multicast)
365{
366#define SUPPORTED_FIF_FLAGS \
367	(FIF_PROMISC_IN_BSS |  FIF_ALLMULTI | FIF_FCSFAIL | \
368	FIF_PLCPFAIL | FIF_CONTROL | FIF_OTHER_BSS | \
369	FIF_BCN_PRBRESP_PROMISC)
370
371	struct ath5k_hw *ah = hw->priv;
372	u32 mfilt[2], rfilt;
373	struct ath5k_vif_iter_data iter_data; /* to count STA interfaces */
374
375	mutex_lock(&ah->lock);
376
377	mfilt[0] = multicast;
378	mfilt[1] = multicast >> 32;
379
380	/* Only deal with supported flags */
381	changed_flags &= SUPPORTED_FIF_FLAGS;
382	*new_flags &= SUPPORTED_FIF_FLAGS;
383
384	/* If HW detects any phy or radar errors, leave those filters on.
385	 * Also, always enable Unicast, Broadcasts and Multicast
386	 * XXX: move unicast, bssid broadcasts and multicast to mac80211 */
387	rfilt = (ath5k_hw_get_rx_filter(ah) & (AR5K_RX_FILTER_PHYERR)) |
388		(AR5K_RX_FILTER_UCAST | AR5K_RX_FILTER_BCAST |
389		AR5K_RX_FILTER_MCAST);
390
391	if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
392		if (*new_flags & FIF_PROMISC_IN_BSS)
393			__set_bit(ATH_STAT_PROMISC, ah->status);
394		else
395			__clear_bit(ATH_STAT_PROMISC, ah->status);
396	}
397
398	if (test_bit(ATH_STAT_PROMISC, ah->status))
399		rfilt |= AR5K_RX_FILTER_PROM;
400
401	/* Note, AR5K_RX_FILTER_MCAST is already enabled */
402	if (*new_flags & FIF_ALLMULTI) {
403		mfilt[0] =  ~0;
404		mfilt[1] =  ~0;
405	}
406
407	/* This is the best we can do */
408	if (*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL))
409		rfilt |= AR5K_RX_FILTER_PHYERR;
410
411	/* FIF_BCN_PRBRESP_PROMISC really means to enable beacons
412	* and probes for any BSSID */
413	if ((*new_flags & FIF_BCN_PRBRESP_PROMISC) || (ah->nvifs > 1))
414		rfilt |= AR5K_RX_FILTER_BEACON;
415
416	/* FIF_CONTROL doc says that if FIF_PROMISC_IN_BSS is not
417	 * set we should only pass on control frames for this
418	 * station. This needs testing. I believe right now this
419	 * enables *all* control frames, which is OK.. but
420	 * but we should see if we can improve on granularity */
421	if (*new_flags & FIF_CONTROL)
422		rfilt |= AR5K_RX_FILTER_CONTROL;
423
424	/* Additional settings per mode -- this is per ath5k */
425
426	/* XXX move these to mac80211, and add a beacon IFF flag to mac80211 */
427
428	switch (ah->opmode) {
429	case NL80211_IFTYPE_MESH_POINT:
430		rfilt |= AR5K_RX_FILTER_CONTROL |
431			 AR5K_RX_FILTER_BEACON |
432			 AR5K_RX_FILTER_PROBEREQ |
433			 AR5K_RX_FILTER_PROM;
434		break;
435	case NL80211_IFTYPE_AP:
436	case NL80211_IFTYPE_ADHOC:
437		rfilt |= AR5K_RX_FILTER_PROBEREQ |
438			 AR5K_RX_FILTER_BEACON;
439		break;
440	case NL80211_IFTYPE_STATION:
441		if (ah->assoc)
442			rfilt |= AR5K_RX_FILTER_BEACON;
 
443	default:
444		break;
445	}
446
447	iter_data.hw_macaddr = NULL;
448	iter_data.n_stas = 0;
449	iter_data.need_set_hw_addr = false;
450	ieee80211_iterate_active_interfaces_atomic(ah->hw, ath5k_vif_iter,
451						   &iter_data);
 
452
453	/* Set up RX Filter */
454	if (iter_data.n_stas > 1) {
455		/* If you have multiple STA interfaces connected to
456		 * different APs, ARPs are not received (most of the time?)
457		 * Enabling PROMISC appears to fix that problem.
458		 */
459		rfilt |= AR5K_RX_FILTER_PROM;
460	}
461
462	/* Set filters */
463	ath5k_hw_set_rx_filter(ah, rfilt);
464
465	/* Set multicast bits */
466	ath5k_hw_set_mcast_filter(ah, mfilt[0], mfilt[1]);
467	/* Set the cached hw filter flags, this will later actually
468	 * be set in HW */
469	ah->filter_flags = rfilt;
 
 
470
471	mutex_unlock(&ah->lock);
472}
473
474
475static int
476ath5k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
477	      struct ieee80211_vif *vif, struct ieee80211_sta *sta,
478	      struct ieee80211_key_conf *key)
479{
480	struct ath5k_hw *ah = hw->priv;
481	struct ath_common *common = ath5k_hw_common(ah);
482	int ret = 0;
483
484	if (ath5k_modparam_nohwcrypt)
485		return -EOPNOTSUPP;
486
 
 
 
 
 
 
 
 
 
 
 
487	switch (key->cipher) {
488	case WLAN_CIPHER_SUITE_WEP40:
489	case WLAN_CIPHER_SUITE_WEP104:
490	case WLAN_CIPHER_SUITE_TKIP:
491		break;
492	case WLAN_CIPHER_SUITE_CCMP:
493		if (common->crypt_caps & ATH_CRYPT_CAP_CIPHER_AESCCM)
494			break;
495		return -EOPNOTSUPP;
496	default:
497		WARN_ON(1);
498		return -EINVAL;
499	}
500
501	mutex_lock(&ah->lock);
502
503	switch (cmd) {
504	case SET_KEY:
505		ret = ath_key_config(common, vif, sta, key);
506		if (ret >= 0) {
507			key->hw_key_idx = ret;
508			/* push IV and Michael MIC generation to stack */
509			key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
510			if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
511				key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
512			if (key->cipher == WLAN_CIPHER_SUITE_CCMP)
513				key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
514			ret = 0;
515		}
516		break;
517	case DISABLE_KEY:
518		ath_key_delete(common, key);
519		break;
520	default:
521		ret = -EINVAL;
522	}
523
524	mmiowb();
525	mutex_unlock(&ah->lock);
526	return ret;
527}
528
529
530static void
531ath5k_sw_scan_start(struct ieee80211_hw *hw)
 
 
532{
533	struct ath5k_hw *ah = hw->priv;
534	if (!ah->assoc)
535		ath5k_hw_set_ledstate(ah, AR5K_LED_SCAN);
536}
537
538
539static void
540ath5k_sw_scan_complete(struct ieee80211_hw *hw)
541{
542	struct ath5k_hw *ah = hw->priv;
543	ath5k_hw_set_ledstate(ah, ah->assoc ?
544		AR5K_LED_ASSOC : AR5K_LED_INIT);
545}
546
547
548static int
549ath5k_get_stats(struct ieee80211_hw *hw,
550		struct ieee80211_low_level_stats *stats)
551{
552	struct ath5k_hw *ah = hw->priv;
553
554	/* Force update */
555	ath5k_hw_update_mib_counters(ah);
556
557	stats->dot11ACKFailureCount = ah->stats.ack_fail;
558	stats->dot11RTSFailureCount = ah->stats.rts_fail;
559	stats->dot11RTSSuccessCount = ah->stats.rts_ok;
560	stats->dot11FCSErrorCount = ah->stats.fcs_error;
561
562	return 0;
563}
564
565
566static int
567ath5k_conf_tx(struct ieee80211_hw *hw, u16 queue,
 
568	      const struct ieee80211_tx_queue_params *params)
569{
570	struct ath5k_hw *ah = hw->priv;
571	struct ath5k_txq_info qi;
572	int ret = 0;
573
574	if (queue >= ah->ah_capabilities.cap_queues.q_tx_num)
575		return 0;
576
577	mutex_lock(&ah->lock);
578
579	ath5k_hw_get_tx_queueprops(ah, queue, &qi);
580
581	qi.tqi_aifs = params->aifs;
582	qi.tqi_cw_min = params->cw_min;
583	qi.tqi_cw_max = params->cw_max;
584	qi.tqi_burst_time = params->txop;
585
586	ATH5K_DBG(ah, ATH5K_DEBUG_ANY,
587		  "Configure tx [queue %d],  "
588		  "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
589		  queue, params->aifs, params->cw_min,
590		  params->cw_max, params->txop);
591
592	if (ath5k_hw_set_tx_queueprops(ah, queue, &qi)) {
593		ATH5K_ERR(ah,
594			  "Unable to update hardware queue %u!\n", queue);
595		ret = -EIO;
596	} else
597		ath5k_hw_reset_tx_queue(ah, queue);
598
599	mutex_unlock(&ah->lock);
600
601	return ret;
602}
603
604
605static u64
606ath5k_get_tsf(struct ieee80211_hw *hw)
607{
608	struct ath5k_hw *ah = hw->priv;
609
610	return ath5k_hw_get_tsf64(ah);
611}
612
613
614static void
615ath5k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
616{
617	struct ath5k_hw *ah = hw->priv;
618
619	ath5k_hw_set_tsf64(ah, tsf);
620}
621
622
623static void
624ath5k_reset_tsf(struct ieee80211_hw *hw)
625{
626	struct ath5k_hw *ah = hw->priv;
627
628	/*
629	 * in IBSS mode we need to update the beacon timers too.
630	 * this will also reset the TSF if we call it with 0
631	 */
632	if (ah->opmode == NL80211_IFTYPE_ADHOC)
633		ath5k_beacon_update_timers(ah, 0);
634	else
635		ath5k_hw_reset_tsf(ah);
636}
637
638
639static int
640ath5k_get_survey(struct ieee80211_hw *hw, int idx, struct survey_info *survey)
641{
642	struct ath5k_hw *ah = hw->priv;
643	struct ieee80211_conf *conf = &hw->conf;
644	struct ath_common *common = ath5k_hw_common(ah);
645	struct ath_cycle_counters *cc = &common->cc_survey;
646	unsigned int div = common->clockrate * 1000;
647
648	if (idx != 0)
649		return -ENOENT;
650
651	spin_lock_bh(&common->cc_lock);
652	ath_hw_cycle_counters_update(common);
653	if (cc->cycles > 0) {
654		ah->survey.channel_time += cc->cycles / div;
655		ah->survey.channel_time_busy += cc->rx_busy / div;
656		ah->survey.channel_time_rx += cc->rx_frame / div;
657		ah->survey.channel_time_tx += cc->tx_frame / div;
658	}
659	memset(cc, 0, sizeof(*cc));
660	spin_unlock_bh(&common->cc_lock);
661
662	memcpy(survey, &ah->survey, sizeof(*survey));
663
664	survey->channel = conf->channel;
665	survey->noise = ah->ah_noise_floor;
666	survey->filled = SURVEY_INFO_NOISE_DBM |
667			SURVEY_INFO_CHANNEL_TIME |
668			SURVEY_INFO_CHANNEL_TIME_BUSY |
669			SURVEY_INFO_CHANNEL_TIME_RX |
670			SURVEY_INFO_CHANNEL_TIME_TX;
 
671
672	return 0;
673}
674
675
676/**
677 * ath5k_set_coverage_class - Set IEEE 802.11 coverage class
678 *
679 * @hw: struct ieee80211_hw pointer
680 * @coverage_class: IEEE 802.11 coverage class number
681 *
682 * Mac80211 callback. Sets slot time, ACK timeout and CTS timeout for given
683 * coverage class. The values are persistent, they are restored after device
684 * reset.
685 */
686static void
687ath5k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
688{
689	struct ath5k_hw *ah = hw->priv;
690
691	mutex_lock(&ah->lock);
692	ath5k_hw_set_coverage_class(ah, coverage_class);
693	mutex_unlock(&ah->lock);
694}
695
696
697static int
698ath5k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
699{
700	struct ath5k_hw *ah = hw->priv;
701
702	if (tx_ant == 1 && rx_ant == 1)
703		ath5k_hw_set_antenna_mode(ah, AR5K_ANTMODE_FIXED_A);
704	else if (tx_ant == 2 && rx_ant == 2)
705		ath5k_hw_set_antenna_mode(ah, AR5K_ANTMODE_FIXED_B);
706	else if ((tx_ant & 3) == 3 && (rx_ant & 3) == 3)
707		ath5k_hw_set_antenna_mode(ah, AR5K_ANTMODE_DEFAULT);
708	else
709		return -EINVAL;
710	return 0;
711}
712
713
714static int
715ath5k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
716{
717	struct ath5k_hw *ah = hw->priv;
718
719	switch (ah->ah_ant_mode) {
720	case AR5K_ANTMODE_FIXED_A:
721		*tx_ant = 1; *rx_ant = 1; break;
722	case AR5K_ANTMODE_FIXED_B:
723		*tx_ant = 2; *rx_ant = 2; break;
724	case AR5K_ANTMODE_DEFAULT:
725		*tx_ant = 3; *rx_ant = 3; break;
726	}
727	return 0;
728}
729
730
731static void ath5k_get_ringparam(struct ieee80211_hw *hw,
732				u32 *tx, u32 *tx_max, u32 *rx, u32 *rx_max)
733{
734	struct ath5k_hw *ah = hw->priv;
735
736	*tx = ah->txqs[AR5K_TX_QUEUE_ID_DATA_MIN].txq_max;
737
738	*tx_max = ATH5K_TXQ_LEN_MAX;
739	*rx = *rx_max = ATH_RXBUF;
740}
741
742
743static int ath5k_set_ringparam(struct ieee80211_hw *hw, u32 tx, u32 rx)
744{
745	struct ath5k_hw *ah = hw->priv;
746	u16 qnum;
747
748	/* only support setting tx ring size for now */
749	if (rx != ATH_RXBUF)
750		return -EINVAL;
751
752	/* restrict tx ring size min/max */
753	if (!tx || tx > ATH5K_TXQ_LEN_MAX)
754		return -EINVAL;
755
756	for (qnum = 0; qnum < ARRAY_SIZE(ah->txqs); qnum++) {
757		if (!ah->txqs[qnum].setup)
758			continue;
759		if (ah->txqs[qnum].qnum < AR5K_TX_QUEUE_ID_DATA_MIN ||
760		    ah->txqs[qnum].qnum > AR5K_TX_QUEUE_ID_DATA_MAX)
761			continue;
762
763		ah->txqs[qnum].txq_max = tx;
764		if (ah->txqs[qnum].txq_len >= ah->txqs[qnum].txq_max)
765			ieee80211_stop_queue(hw, ah->txqs[qnum].qnum);
766	}
767
768	return 0;
769}
770
771
772const struct ieee80211_ops ath5k_hw_ops = {
773	.tx			= ath5k_tx,
 
774	.start			= ath5k_start,
775	.stop			= ath5k_stop,
776	.add_interface		= ath5k_add_interface,
777	/* .change_interface	= not implemented */
778	.remove_interface	= ath5k_remove_interface,
779	.config			= ath5k_config,
780	.bss_info_changed	= ath5k_bss_info_changed,
781	.prepare_multicast	= ath5k_prepare_multicast,
782	.configure_filter	= ath5k_configure_filter,
783	/* .set_tim		= not implemented */
784	.set_key		= ath5k_set_key,
785	/* .update_tkip_key	= not implemented */
786	/* .hw_scan		= not implemented */
787	.sw_scan_start		= ath5k_sw_scan_start,
788	.sw_scan_complete	= ath5k_sw_scan_complete,
789	.get_stats		= ath5k_get_stats,
790	/* .get_tkip_seq	= not implemented */
791	/* .set_frag_threshold	= not implemented */
792	/* .set_rts_threshold	= not implemented */
793	/* .sta_add		= not implemented */
794	/* .sta_remove		= not implemented */
795	/* .sta_notify		= not implemented */
796	.conf_tx		= ath5k_conf_tx,
797	.get_tsf		= ath5k_get_tsf,
798	.set_tsf		= ath5k_set_tsf,
799	.reset_tsf		= ath5k_reset_tsf,
800	/* .tx_last_beacon	= not implemented */
801	/* .ampdu_action	= not needed */
802	.get_survey		= ath5k_get_survey,
803	.set_coverage_class	= ath5k_set_coverage_class,
804	/* .rfkill_poll		= not implemented */
805	/* .flush		= not implemented */
806	/* .channel_switch	= not implemented */
807	/* .napi_poll		= not implemented */
808	.set_antenna		= ath5k_set_antenna,
809	.get_antenna		= ath5k_get_antenna,
810	.set_ringparam		= ath5k_set_ringparam,
811	.get_ringparam		= ath5k_get_ringparam,
812};
v6.2
  1/*-
  2 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
  3 * Copyright (c) 2004-2005 Atheros Communications, Inc.
  4 * Copyright (c) 2006 Devicescape Software, Inc.
  5 * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
  6 * Copyright (c) 2007 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
  7 * Copyright (c) 2010 Bruno Randolf <br1@einfach.org>
  8 *
  9 * All rights reserved.
 10 *
 11 * Redistribution and use in source and binary forms, with or without
 12 * modification, are permitted provided that the following conditions
 13 * are met:
 14 * 1. Redistributions of source code must retain the above copyright
 15 *    notice, this list of conditions and the following disclaimer,
 16 *    without modification.
 17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 18 *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
 19 *    redistribution must be conditioned upon including a substantially
 20 *    similar Disclaimer requirement for further binary redistribution.
 21 * 3. Neither the names of the above-listed copyright holders nor the names
 22 *    of any contributors may be used to endorse or promote products derived
 23 *    from this software without specific prior written permission.
 24 *
 25 * Alternatively, this software may be distributed under the terms of the
 26 * GNU General Public License ("GPL") version 2 as published by the Free
 27 * Software Foundation.
 28 *
 29 * NO WARRANTY
 30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 32 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
 33 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 34 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
 35 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 38 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 40 * THE POSSIBILITY OF SUCH DAMAGES.
 41 *
 42 */
 43
 44#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 45
 46#include <net/mac80211.h>
 47#include <asm/unaligned.h>
 48
 49#include "ath5k.h"
 50#include "base.h"
 51#include "reg.h"
 52
 53/********************\
 54* Mac80211 functions *
 55\********************/
 56
 57static void
 58ath5k_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
 59	 struct sk_buff *skb)
 60{
 61	struct ath5k_hw *ah = hw->priv;
 62	u16 qnum = skb_get_queue_mapping(skb);
 63
 64	if (WARN_ON(qnum >= ah->ah_capabilities.cap_queues.q_tx_num)) {
 65		ieee80211_free_txskb(hw, skb);
 66		return;
 67	}
 68
 69	ath5k_tx_queue(hw, skb, &ah->txqs[qnum], control);
 70}
 71
 72
 73static int
 74ath5k_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
 75{
 76	struct ath5k_hw *ah = hw->priv;
 77	int ret;
 78	struct ath5k_vif *avf = (void *)vif->drv_priv;
 79
 80	mutex_lock(&ah->lock);
 81
 82	if ((vif->type == NL80211_IFTYPE_AP ||
 83	     vif->type == NL80211_IFTYPE_ADHOC)
 84	    && (ah->num_ap_vifs + ah->num_adhoc_vifs) >= ATH_BCBUF) {
 85		ret = -ELNRNG;
 86		goto end;
 87	}
 88
 89	/* Don't allow other interfaces if one ad-hoc is configured.
 90	 * TODO: Fix the problems with ad-hoc and multiple other interfaces.
 91	 * We would need to operate the HW in ad-hoc mode to allow TSF updates
 92	 * for the IBSS, but this breaks with additional AP or STA interfaces
 93	 * at the moment. */
 94	if (ah->num_adhoc_vifs ||
 95	    (ah->nvifs && vif->type == NL80211_IFTYPE_ADHOC)) {
 96		ATH5K_ERR(ah, "Only one single ad-hoc interface is allowed.\n");
 97		ret = -ELNRNG;
 98		goto end;
 99	}
100
101	switch (vif->type) {
102	case NL80211_IFTYPE_AP:
103	case NL80211_IFTYPE_STATION:
104	case NL80211_IFTYPE_ADHOC:
105	case NL80211_IFTYPE_MESH_POINT:
106		avf->opmode = vif->type;
107		break;
108	default:
109		ret = -EOPNOTSUPP;
110		goto end;
111	}
112
113	ah->nvifs++;
114	ATH5K_DBG(ah, ATH5K_DEBUG_MODE, "add interface mode %d\n", avf->opmode);
115
116	/* Assign the vap/adhoc to a beacon xmit slot. */
117	if ((avf->opmode == NL80211_IFTYPE_AP) ||
118	    (avf->opmode == NL80211_IFTYPE_ADHOC) ||
119	    (avf->opmode == NL80211_IFTYPE_MESH_POINT)) {
120		int slot;
121
122		WARN_ON(list_empty(&ah->bcbuf));
123		avf->bbuf = list_first_entry(&ah->bcbuf, struct ath5k_buf,
124					     list);
125		list_del(&avf->bbuf->list);
126
127		avf->bslot = 0;
128		for (slot = 0; slot < ATH_BCBUF; slot++) {
129			if (!ah->bslot[slot]) {
130				avf->bslot = slot;
131				break;
132			}
133		}
134		BUG_ON(ah->bslot[avf->bslot] != NULL);
135		ah->bslot[avf->bslot] = vif;
136		if (avf->opmode == NL80211_IFTYPE_AP)
137			ah->num_ap_vifs++;
138		else if (avf->opmode == NL80211_IFTYPE_ADHOC)
139			ah->num_adhoc_vifs++;
140		else if (avf->opmode == NL80211_IFTYPE_MESH_POINT)
141			ah->num_mesh_vifs++;
142	}
143
144	/* Any MAC address is fine, all others are included through the
145	 * filter.
146	 */
 
147	ath5k_hw_set_lladdr(ah, vif->addr);
148
 
 
149	ath5k_update_bssid_mask_and_opmode(ah, vif);
150	ret = 0;
151end:
152	mutex_unlock(&ah->lock);
153	return ret;
154}
155
156
157static void
158ath5k_remove_interface(struct ieee80211_hw *hw,
159		       struct ieee80211_vif *vif)
160{
161	struct ath5k_hw *ah = hw->priv;
162	struct ath5k_vif *avf = (void *)vif->drv_priv;
163	unsigned int i;
164
165	mutex_lock(&ah->lock);
166	ah->nvifs--;
167
168	if (avf->bbuf) {
169		ath5k_txbuf_free_skb(ah, avf->bbuf);
170		list_add_tail(&avf->bbuf->list, &ah->bcbuf);
171		for (i = 0; i < ATH_BCBUF; i++) {
172			if (ah->bslot[i] == vif) {
173				ah->bslot[i] = NULL;
174				break;
175			}
176		}
177		avf->bbuf = NULL;
178	}
179	if (avf->opmode == NL80211_IFTYPE_AP)
180		ah->num_ap_vifs--;
181	else if (avf->opmode == NL80211_IFTYPE_ADHOC)
182		ah->num_adhoc_vifs--;
183	else if (avf->opmode == NL80211_IFTYPE_MESH_POINT)
184		ah->num_mesh_vifs--;
185
186	ath5k_update_bssid_mask_and_opmode(ah, NULL);
187	mutex_unlock(&ah->lock);
188}
189
190
191/*
192 * TODO: Phy disable/diversity etc
193 */
194static int
195ath5k_config(struct ieee80211_hw *hw, u32 changed)
196{
197	struct ath5k_hw *ah = hw->priv;
198	struct ieee80211_conf *conf = &hw->conf;
199	int ret = 0;
200	int i;
201
202	mutex_lock(&ah->lock);
203
204	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
205		ret = ath5k_chan_set(ah, &conf->chandef);
206		if (ret < 0)
207			goto unlock;
208	}
209
210	if ((changed & IEEE80211_CONF_CHANGE_POWER) &&
211	(ah->ah_txpower.txp_requested != conf->power_level)) {
212		ah->ah_txpower.txp_requested = conf->power_level;
213
214		/* Half dB steps */
215		ath5k_hw_set_txpower_limit(ah, (conf->power_level * 2));
216	}
217
218	if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
219		ah->ah_retry_long = conf->long_frame_max_tx_count;
220		ah->ah_retry_short = conf->short_frame_max_tx_count;
221
222		for (i = 0; i < ah->ah_capabilities.cap_queues.q_tx_num; i++)
223			ath5k_hw_set_tx_retry_limits(ah, i);
224	}
225
226	/* TODO:
227	 * 1) Move this on config_interface and handle each case
228	 * separately eg. when we have only one STA vif, use
229	 * AR5K_ANTMODE_SINGLE_AP
230	 *
231	 * 2) Allow the user to change antenna mode eg. when only
232	 * one antenna is present
233	 *
234	 * 3) Allow the user to set default/tx antenna when possible
235	 *
236	 * 4) Default mode should handle 90% of the cases, together
237	 * with fixed a/b and single AP modes we should be able to
238	 * handle 99%. Sectored modes are extreme cases and i still
239	 * haven't found a usage for them. If we decide to support them,
240	 * then we must allow the user to set how many tx antennas we
241	 * have available
242	 */
243	ath5k_hw_set_antenna_mode(ah, ah->ah_ant_mode);
244
245unlock:
246	mutex_unlock(&ah->lock);
247	return ret;
248}
249
250
251static void
252ath5k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
253		       struct ieee80211_bss_conf *bss_conf, u64 changes)
254{
255	struct ath5k_vif *avf = (void *)vif->drv_priv;
256	struct ath5k_hw *ah = hw->priv;
257	struct ath_common *common = ath5k_hw_common(ah);
 
258
259	mutex_lock(&ah->lock);
260
261	if (changes & BSS_CHANGED_BSSID) {
262		/* Cache for later use during resets */
263		memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
264		common->curaid = 0;
265		ath5k_hw_set_bssid(ah);
 
266	}
267
268	if (changes & BSS_CHANGED_BEACON_INT)
269		ah->bintval = bss_conf->beacon_int;
270
271	if (changes & BSS_CHANGED_ERP_SLOT) {
272		int slot_time;
273
274		ah->ah_short_slot = bss_conf->use_short_slot;
275		slot_time = ath5k_hw_get_default_slottime(ah) +
276			    3 * ah->ah_coverage_class;
277		ath5k_hw_set_ifs_intervals(ah, slot_time);
278	}
279
280	if (changes & BSS_CHANGED_ASSOC) {
281		avf->assoc = vif->cfg.assoc;
282		if (vif->cfg.assoc)
283			ah->assoc = vif->cfg.assoc;
284		else
285			ah->assoc = ath5k_any_vif_assoc(ah);
286
287		if (ah->opmode == NL80211_IFTYPE_STATION)
288			ath5k_set_beacon_filter(hw, ah->assoc);
289		ath5k_hw_set_ledstate(ah, ah->assoc ?
290			AR5K_LED_ASSOC : AR5K_LED_INIT);
291		if (vif->cfg.assoc) {
292			ATH5K_DBG(ah, ATH5K_DEBUG_ANY,
293				  "Bss Info ASSOC %d, bssid: %pM\n",
294				  vif->cfg.aid, common->curbssid);
295			common->curaid = vif->cfg.aid;
296			ath5k_hw_set_bssid(ah);
297			/* Once ANI is available you would start it here */
298		}
299	}
300
301	if (changes & BSS_CHANGED_BEACON) {
302		spin_lock_bh(&ah->block);
303		ath5k_beacon_update(hw, vif);
304		spin_unlock_bh(&ah->block);
305	}
306
307	if (changes & BSS_CHANGED_BEACON_ENABLED)
308		ah->enable_beacon = bss_conf->enable_beacon;
309
310	if (changes & (BSS_CHANGED_BEACON | BSS_CHANGED_BEACON_ENABLED |
311		       BSS_CHANGED_BEACON_INT))
312		ath5k_beacon_config(ah);
313
314	mutex_unlock(&ah->lock);
315}
316
317
318static u64
319ath5k_prepare_multicast(struct ieee80211_hw *hw,
320			struct netdev_hw_addr_list *mc_list)
321{
322	u32 mfilt[2], val;
323	u8 pos;
324	struct netdev_hw_addr *ha;
325
326	mfilt[0] = 0;
327	mfilt[1] = 0;
328
329	netdev_hw_addr_list_for_each(ha, mc_list) {
330		/* calculate XOR of eight 6-bit values */
331		val = get_unaligned_le32(ha->addr + 0);
332		pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
333		val = get_unaligned_le32(ha->addr + 3);
334		pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
335		pos &= 0x3f;
336		mfilt[pos / 32] |= (1 << (pos % 32));
337		/* XXX: we might be able to just do this instead,
338		* but not sure, needs testing, if we do use this we'd
339		* need to inform below not to reset the mcast */
340		/* ath5k_hw_set_mcast_filterindex(ah,
341		 *      ha->addr[5]); */
342	}
343
344	return ((u64)(mfilt[1]) << 32) | mfilt[0];
345}
346
347
348/*
349 * o always accept unicast, broadcast, and multicast traffic
350 * o multicast traffic for all BSSIDs will be enabled if mac80211
351 *   says it should be
352 * o maintain current state of phy ofdm or phy cck error reception.
353 *   If the hardware detects any of these type of errors then
354 *   ath5k_hw_get_rx_filter() will pass to us the respective
355 *   hardware filters to be able to receive these type of frames.
356 * o probe request frames are accepted only when operating in
357 *   hostap, adhoc, or monitor modes
358 * o enable promiscuous mode according to the interface state
359 * o accept beacons:
360 *   - when operating in adhoc mode so the 802.11 layer creates
361 *     node table entries for peers,
362 *   - when operating in station mode for collecting rssi data when
363 *     the station is otherwise quiet, or
364 *   - when scanning
365 */
366static void
367ath5k_configure_filter(struct ieee80211_hw *hw, unsigned int changed_flags,
368		       unsigned int *new_flags, u64 multicast)
369{
370#define SUPPORTED_FIF_FLAGS \
371	(FIF_ALLMULTI | FIF_FCSFAIL | \
372	FIF_PLCPFAIL | FIF_CONTROL | FIF_OTHER_BSS | \
373	FIF_BCN_PRBRESP_PROMISC)
374
375	struct ath5k_hw *ah = hw->priv;
376	u32 mfilt[2], rfilt;
377	struct ath5k_vif_iter_data iter_data; /* to count STA interfaces */
378
379	mutex_lock(&ah->lock);
380
381	mfilt[0] = multicast;
382	mfilt[1] = multicast >> 32;
383
384	/* Only deal with supported flags */
385	changed_flags &= SUPPORTED_FIF_FLAGS;
386	*new_flags &= SUPPORTED_FIF_FLAGS;
387
388	/* If HW detects any phy or radar errors, leave those filters on.
389	 * Also, always enable Unicast, Broadcasts and Multicast
390	 * XXX: move unicast, bssid broadcasts and multicast to mac80211 */
391	rfilt = (ath5k_hw_get_rx_filter(ah) & (AR5K_RX_FILTER_PHYERR)) |
392		(AR5K_RX_FILTER_UCAST | AR5K_RX_FILTER_BCAST |
393		AR5K_RX_FILTER_MCAST);
394
 
 
 
 
 
 
 
 
 
 
395	/* Note, AR5K_RX_FILTER_MCAST is already enabled */
396	if (*new_flags & FIF_ALLMULTI) {
397		mfilt[0] =  ~0;
398		mfilt[1] =  ~0;
399	}
400
401	/* This is the best we can do */
402	if (*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL))
403		rfilt |= AR5K_RX_FILTER_PHYERR;
404
405	/* FIF_BCN_PRBRESP_PROMISC really means to enable beacons
406	* and probes for any BSSID */
407	if ((*new_flags & FIF_BCN_PRBRESP_PROMISC) || (ah->nvifs > 1))
408		rfilt |= AR5K_RX_FILTER_BEACON;
409
410	/* FIF_CONTROL doc says we should only pass on control frames for this
 
411	 * station. This needs testing. I believe right now this
412	 * enables *all* control frames, which is OK.. but
413	 * we should see if we can improve on granularity */
414	if (*new_flags & FIF_CONTROL)
415		rfilt |= AR5K_RX_FILTER_CONTROL;
416
417	/* Additional settings per mode -- this is per ath5k */
418
419	/* XXX move these to mac80211, and add a beacon IFF flag to mac80211 */
420
421	switch (ah->opmode) {
422	case NL80211_IFTYPE_MESH_POINT:
423		rfilt |= AR5K_RX_FILTER_CONTROL |
424			 AR5K_RX_FILTER_BEACON |
425			 AR5K_RX_FILTER_PROBEREQ |
426			 AR5K_RX_FILTER_PROM;
427		break;
428	case NL80211_IFTYPE_AP:
429	case NL80211_IFTYPE_ADHOC:
430		rfilt |= AR5K_RX_FILTER_PROBEREQ |
431			 AR5K_RX_FILTER_BEACON;
432		break;
433	case NL80211_IFTYPE_STATION:
434		if (ah->assoc)
435			rfilt |= AR5K_RX_FILTER_BEACON;
436		break;
437	default:
438		break;
439	}
440
441	iter_data.hw_macaddr = NULL;
442	iter_data.n_stas = 0;
443	iter_data.need_set_hw_addr = false;
444	ieee80211_iterate_active_interfaces_atomic(
445		ah->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
446		ath5k_vif_iter, &iter_data);
447
448	/* Set up RX Filter */
449	if (iter_data.n_stas > 1) {
450		/* If you have multiple STA interfaces connected to
451		 * different APs, ARPs are not received (most of the time?)
452		 * Enabling PROMISC appears to fix that problem.
453		 */
454		rfilt |= AR5K_RX_FILTER_PROM;
455	}
456
457	/* Set filters */
458	ath5k_hw_set_rx_filter(ah, rfilt);
459
460	/* Set multicast bits */
461	ath5k_hw_set_mcast_filter(ah, mfilt[0], mfilt[1]);
462	/* Set the cached hw filter flags, this will later actually
463	 * be set in HW */
464	ah->filter_flags = rfilt;
465	/* Store current FIF filter flags */
466	ah->fif_filter_flags = *new_flags;
467
468	mutex_unlock(&ah->lock);
469}
470
471
472static int
473ath5k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
474	      struct ieee80211_vif *vif, struct ieee80211_sta *sta,
475	      struct ieee80211_key_conf *key)
476{
477	struct ath5k_hw *ah = hw->priv;
478	struct ath_common *common = ath5k_hw_common(ah);
479	int ret = 0;
480
481	if (ath5k_modparam_nohwcrypt)
482		return -EOPNOTSUPP;
483
484	if (key->flags & IEEE80211_KEY_FLAG_RX_MGMT)
485		return -EOPNOTSUPP;
486
487	if (vif->type == NL80211_IFTYPE_ADHOC &&
488	    (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
489	     key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
490	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
491		/* don't program group keys when using IBSS_RSN */
492		return -EOPNOTSUPP;
493	}
494
495	switch (key->cipher) {
496	case WLAN_CIPHER_SUITE_WEP40:
497	case WLAN_CIPHER_SUITE_WEP104:
498	case WLAN_CIPHER_SUITE_TKIP:
499		break;
500	case WLAN_CIPHER_SUITE_CCMP:
501		if (common->crypt_caps & ATH_CRYPT_CAP_CIPHER_AESCCM)
502			break;
503		return -EOPNOTSUPP;
504	default:
505		return -EOPNOTSUPP;
 
506	}
507
508	mutex_lock(&ah->lock);
509
510	switch (cmd) {
511	case SET_KEY:
512		ret = ath_key_config(common, vif, sta, key);
513		if (ret >= 0) {
514			key->hw_key_idx = ret;
515			/* push IV and Michael MIC generation to stack */
516			key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
517			if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
518				key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
519			if (key->cipher == WLAN_CIPHER_SUITE_CCMP)
520				key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
521			ret = 0;
522		}
523		break;
524	case DISABLE_KEY:
525		ath_key_delete(common, key->hw_key_idx);
526		break;
527	default:
528		ret = -EINVAL;
529	}
530
 
531	mutex_unlock(&ah->lock);
532	return ret;
533}
534
535
536static void
537ath5k_sw_scan_start(struct ieee80211_hw *hw,
538		    struct ieee80211_vif *vif,
539		    const u8 *mac_addr)
540{
541	struct ath5k_hw *ah = hw->priv;
542	if (!ah->assoc)
543		ath5k_hw_set_ledstate(ah, AR5K_LED_SCAN);
544}
545
546
547static void
548ath5k_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
549{
550	struct ath5k_hw *ah = hw->priv;
551	ath5k_hw_set_ledstate(ah, ah->assoc ?
552		AR5K_LED_ASSOC : AR5K_LED_INIT);
553}
554
555
556static int
557ath5k_get_stats(struct ieee80211_hw *hw,
558		struct ieee80211_low_level_stats *stats)
559{
560	struct ath5k_hw *ah = hw->priv;
561
562	/* Force update */
563	ath5k_hw_update_mib_counters(ah);
564
565	stats->dot11ACKFailureCount = ah->stats.ack_fail;
566	stats->dot11RTSFailureCount = ah->stats.rts_fail;
567	stats->dot11RTSSuccessCount = ah->stats.rts_ok;
568	stats->dot11FCSErrorCount = ah->stats.fcs_error;
569
570	return 0;
571}
572
573
574static int
575ath5k_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
576	      unsigned int link_id, u16 queue,
577	      const struct ieee80211_tx_queue_params *params)
578{
579	struct ath5k_hw *ah = hw->priv;
580	struct ath5k_txq_info qi;
581	int ret = 0;
582
583	if (queue >= ah->ah_capabilities.cap_queues.q_tx_num)
584		return 0;
585
586	mutex_lock(&ah->lock);
587
588	ath5k_hw_get_tx_queueprops(ah, queue, &qi);
589
590	qi.tqi_aifs = params->aifs;
591	qi.tqi_cw_min = params->cw_min;
592	qi.tqi_cw_max = params->cw_max;
593	qi.tqi_burst_time = params->txop * 32;
594
595	ATH5K_DBG(ah, ATH5K_DEBUG_ANY,
596		  "Configure tx [queue %d],  "
597		  "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
598		  queue, params->aifs, params->cw_min,
599		  params->cw_max, params->txop);
600
601	if (ath5k_hw_set_tx_queueprops(ah, queue, &qi)) {
602		ATH5K_ERR(ah,
603			  "Unable to update hardware queue %u!\n", queue);
604		ret = -EIO;
605	} else
606		ath5k_hw_reset_tx_queue(ah, queue);
607
608	mutex_unlock(&ah->lock);
609
610	return ret;
611}
612
613
614static u64
615ath5k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
616{
617	struct ath5k_hw *ah = hw->priv;
618
619	return ath5k_hw_get_tsf64(ah);
620}
621
622
623static void
624ath5k_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u64 tsf)
625{
626	struct ath5k_hw *ah = hw->priv;
627
628	ath5k_hw_set_tsf64(ah, tsf);
629}
630
631
632static void
633ath5k_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
634{
635	struct ath5k_hw *ah = hw->priv;
636
637	/*
638	 * in IBSS mode we need to update the beacon timers too.
639	 * this will also reset the TSF if we call it with 0
640	 */
641	if (ah->opmode == NL80211_IFTYPE_ADHOC)
642		ath5k_beacon_update_timers(ah, 0);
643	else
644		ath5k_hw_reset_tsf(ah);
645}
646
647
648static int
649ath5k_get_survey(struct ieee80211_hw *hw, int idx, struct survey_info *survey)
650{
651	struct ath5k_hw *ah = hw->priv;
652	struct ieee80211_conf *conf = &hw->conf;
653	struct ath_common *common = ath5k_hw_common(ah);
654	struct ath_cycle_counters *cc = &common->cc_survey;
655	unsigned int div = common->clockrate * 1000;
656
657	if (idx != 0)
658		return -ENOENT;
659
660	spin_lock_bh(&common->cc_lock);
661	ath_hw_cycle_counters_update(common);
662	if (cc->cycles > 0) {
663		ah->survey.time += cc->cycles / div;
664		ah->survey.time_busy += cc->rx_busy / div;
665		ah->survey.time_rx += cc->rx_frame / div;
666		ah->survey.time_tx += cc->tx_frame / div;
667	}
668	memset(cc, 0, sizeof(*cc));
669	spin_unlock_bh(&common->cc_lock);
670
671	memcpy(survey, &ah->survey, sizeof(*survey));
672
673	survey->channel = conf->chandef.chan;
674	survey->noise = ah->ah_noise_floor;
675	survey->filled = SURVEY_INFO_NOISE_DBM |
676			SURVEY_INFO_IN_USE |
677			SURVEY_INFO_TIME |
678			SURVEY_INFO_TIME_BUSY |
679			SURVEY_INFO_TIME_RX |
680			SURVEY_INFO_TIME_TX;
681
682	return 0;
683}
684
685
686/**
687 * ath5k_set_coverage_class - Set IEEE 802.11 coverage class
688 *
689 * @hw: struct ieee80211_hw pointer
690 * @coverage_class: IEEE 802.11 coverage class number
691 *
692 * Mac80211 callback. Sets slot time, ACK timeout and CTS timeout for given
693 * coverage class. The values are persistent, they are restored after device
694 * reset.
695 */
696static void
697ath5k_set_coverage_class(struct ieee80211_hw *hw, s16 coverage_class)
698{
699	struct ath5k_hw *ah = hw->priv;
700
701	mutex_lock(&ah->lock);
702	ath5k_hw_set_coverage_class(ah, coverage_class);
703	mutex_unlock(&ah->lock);
704}
705
706
707static int
708ath5k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
709{
710	struct ath5k_hw *ah = hw->priv;
711
712	if (tx_ant == 1 && rx_ant == 1)
713		ath5k_hw_set_antenna_mode(ah, AR5K_ANTMODE_FIXED_A);
714	else if (tx_ant == 2 && rx_ant == 2)
715		ath5k_hw_set_antenna_mode(ah, AR5K_ANTMODE_FIXED_B);
716	else if ((tx_ant & 3) == 3 && (rx_ant & 3) == 3)
717		ath5k_hw_set_antenna_mode(ah, AR5K_ANTMODE_DEFAULT);
718	else
719		return -EINVAL;
720	return 0;
721}
722
723
724static int
725ath5k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
726{
727	struct ath5k_hw *ah = hw->priv;
728
729	switch (ah->ah_ant_mode) {
730	case AR5K_ANTMODE_FIXED_A:
731		*tx_ant = 1; *rx_ant = 1; break;
732	case AR5K_ANTMODE_FIXED_B:
733		*tx_ant = 2; *rx_ant = 2; break;
734	case AR5K_ANTMODE_DEFAULT:
735		*tx_ant = 3; *rx_ant = 3; break;
736	}
737	return 0;
738}
739
740
741static void ath5k_get_ringparam(struct ieee80211_hw *hw,
742				u32 *tx, u32 *tx_max, u32 *rx, u32 *rx_max)
743{
744	struct ath5k_hw *ah = hw->priv;
745
746	*tx = ah->txqs[AR5K_TX_QUEUE_ID_DATA_MIN].txq_max;
747
748	*tx_max = ATH5K_TXQ_LEN_MAX;
749	*rx = *rx_max = ATH_RXBUF;
750}
751
752
753static int ath5k_set_ringparam(struct ieee80211_hw *hw, u32 tx, u32 rx)
754{
755	struct ath5k_hw *ah = hw->priv;
756	u16 qnum;
757
758	/* only support setting tx ring size for now */
759	if (rx != ATH_RXBUF)
760		return -EINVAL;
761
762	/* restrict tx ring size min/max */
763	if (!tx || tx > ATH5K_TXQ_LEN_MAX)
764		return -EINVAL;
765
766	for (qnum = 0; qnum < ARRAY_SIZE(ah->txqs); qnum++) {
767		if (!ah->txqs[qnum].setup)
768			continue;
769		if (ah->txqs[qnum].qnum < AR5K_TX_QUEUE_ID_DATA_MIN ||
770		    ah->txqs[qnum].qnum > AR5K_TX_QUEUE_ID_DATA_MAX)
771			continue;
772
773		ah->txqs[qnum].txq_max = tx;
774		if (ah->txqs[qnum].txq_len >= ah->txqs[qnum].txq_max)
775			ieee80211_stop_queue(hw, ah->txqs[qnum].qnum);
776	}
777
778	return 0;
779}
780
781
782const struct ieee80211_ops ath5k_hw_ops = {
783	.tx			= ath5k_tx,
784	.wake_tx_queue		= ieee80211_handle_wake_tx_queue,
785	.start			= ath5k_start,
786	.stop			= ath5k_stop,
787	.add_interface		= ath5k_add_interface,
788	/* .change_interface	= not implemented */
789	.remove_interface	= ath5k_remove_interface,
790	.config			= ath5k_config,
791	.bss_info_changed	= ath5k_bss_info_changed,
792	.prepare_multicast	= ath5k_prepare_multicast,
793	.configure_filter	= ath5k_configure_filter,
794	/* .set_tim		= not implemented */
795	.set_key		= ath5k_set_key,
796	/* .update_tkip_key	= not implemented */
797	/* .hw_scan		= not implemented */
798	.sw_scan_start		= ath5k_sw_scan_start,
799	.sw_scan_complete	= ath5k_sw_scan_complete,
800	.get_stats		= ath5k_get_stats,
 
801	/* .set_frag_threshold	= not implemented */
802	/* .set_rts_threshold	= not implemented */
803	/* .sta_add		= not implemented */
804	/* .sta_remove		= not implemented */
805	/* .sta_notify		= not implemented */
806	.conf_tx		= ath5k_conf_tx,
807	.get_tsf		= ath5k_get_tsf,
808	.set_tsf		= ath5k_set_tsf,
809	.reset_tsf		= ath5k_reset_tsf,
810	/* .tx_last_beacon	= not implemented */
811	/* .ampdu_action	= not needed */
812	.get_survey		= ath5k_get_survey,
813	.set_coverage_class	= ath5k_set_coverage_class,
814	/* .rfkill_poll		= not implemented */
815	/* .flush		= not implemented */
816	/* .channel_switch	= not implemented */
817	/* .napi_poll		= not implemented */
818	.set_antenna		= ath5k_set_antenna,
819	.get_antenna		= ath5k_get_antenna,
820	.set_ringparam		= ath5k_set_ringparam,
821	.get_ringparam		= ath5k_get_ringparam,
822};