xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/zydas/zd1211rw/zd_mac.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* ZD1211 USB-WLAN driver for Linux
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 2005-2007 Ulrich Kunitz <kune@deine-taler.de>
5*4882a593Smuzhiyun  * Copyright (C) 2006-2007 Daniel Drake <dsd@gentoo.org>
6*4882a593Smuzhiyun  * Copyright (C) 2006-2007 Michael Wu <flamingice@sourmilk.net>
7*4882a593Smuzhiyun  * Copyright (C) 2007-2008 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/netdevice.h>
11*4882a593Smuzhiyun #include <linux/etherdevice.h>
12*4882a593Smuzhiyun #include <linux/slab.h>
13*4882a593Smuzhiyun #include <linux/usb.h>
14*4882a593Smuzhiyun #include <linux/jiffies.h>
15*4882a593Smuzhiyun #include <net/ieee80211_radiotap.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include "zd_def.h"
18*4882a593Smuzhiyun #include "zd_chip.h"
19*4882a593Smuzhiyun #include "zd_mac.h"
20*4882a593Smuzhiyun #include "zd_rf.h"
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun struct zd_reg_alpha2_map {
23*4882a593Smuzhiyun 	u32 reg;
24*4882a593Smuzhiyun 	char alpha2[2];
25*4882a593Smuzhiyun };
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun static struct zd_reg_alpha2_map reg_alpha2_map[] = {
28*4882a593Smuzhiyun 	{ ZD_REGDOMAIN_FCC, "US" },
29*4882a593Smuzhiyun 	{ ZD_REGDOMAIN_IC, "CA" },
30*4882a593Smuzhiyun 	{ ZD_REGDOMAIN_ETSI, "DE" }, /* Generic ETSI, use most restrictive */
31*4882a593Smuzhiyun 	{ ZD_REGDOMAIN_JAPAN, "JP" },
32*4882a593Smuzhiyun 	{ ZD_REGDOMAIN_JAPAN_2, "JP" },
33*4882a593Smuzhiyun 	{ ZD_REGDOMAIN_JAPAN_3, "JP" },
34*4882a593Smuzhiyun 	{ ZD_REGDOMAIN_SPAIN, "ES" },
35*4882a593Smuzhiyun 	{ ZD_REGDOMAIN_FRANCE, "FR" },
36*4882a593Smuzhiyun };
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun /* This table contains the hardware specific values for the modulation rates. */
39*4882a593Smuzhiyun static const struct ieee80211_rate zd_rates[] = {
40*4882a593Smuzhiyun 	{ .bitrate = 10,
41*4882a593Smuzhiyun 	  .hw_value = ZD_CCK_RATE_1M, },
42*4882a593Smuzhiyun 	{ .bitrate = 20,
43*4882a593Smuzhiyun 	  .hw_value = ZD_CCK_RATE_2M,
44*4882a593Smuzhiyun 	  .hw_value_short = ZD_CCK_RATE_2M | ZD_CCK_PREA_SHORT,
45*4882a593Smuzhiyun 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
46*4882a593Smuzhiyun 	{ .bitrate = 55,
47*4882a593Smuzhiyun 	  .hw_value = ZD_CCK_RATE_5_5M,
48*4882a593Smuzhiyun 	  .hw_value_short = ZD_CCK_RATE_5_5M | ZD_CCK_PREA_SHORT,
49*4882a593Smuzhiyun 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
50*4882a593Smuzhiyun 	{ .bitrate = 110,
51*4882a593Smuzhiyun 	  .hw_value = ZD_CCK_RATE_11M,
52*4882a593Smuzhiyun 	  .hw_value_short = ZD_CCK_RATE_11M | ZD_CCK_PREA_SHORT,
53*4882a593Smuzhiyun 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
54*4882a593Smuzhiyun 	{ .bitrate = 60,
55*4882a593Smuzhiyun 	  .hw_value = ZD_OFDM_RATE_6M,
56*4882a593Smuzhiyun 	  .flags = 0 },
57*4882a593Smuzhiyun 	{ .bitrate = 90,
58*4882a593Smuzhiyun 	  .hw_value = ZD_OFDM_RATE_9M,
59*4882a593Smuzhiyun 	  .flags = 0 },
60*4882a593Smuzhiyun 	{ .bitrate = 120,
61*4882a593Smuzhiyun 	  .hw_value = ZD_OFDM_RATE_12M,
62*4882a593Smuzhiyun 	  .flags = 0 },
63*4882a593Smuzhiyun 	{ .bitrate = 180,
64*4882a593Smuzhiyun 	  .hw_value = ZD_OFDM_RATE_18M,
65*4882a593Smuzhiyun 	  .flags = 0 },
66*4882a593Smuzhiyun 	{ .bitrate = 240,
67*4882a593Smuzhiyun 	  .hw_value = ZD_OFDM_RATE_24M,
68*4882a593Smuzhiyun 	  .flags = 0 },
69*4882a593Smuzhiyun 	{ .bitrate = 360,
70*4882a593Smuzhiyun 	  .hw_value = ZD_OFDM_RATE_36M,
71*4882a593Smuzhiyun 	  .flags = 0 },
72*4882a593Smuzhiyun 	{ .bitrate = 480,
73*4882a593Smuzhiyun 	  .hw_value = ZD_OFDM_RATE_48M,
74*4882a593Smuzhiyun 	  .flags = 0 },
75*4882a593Smuzhiyun 	{ .bitrate = 540,
76*4882a593Smuzhiyun 	  .hw_value = ZD_OFDM_RATE_54M,
77*4882a593Smuzhiyun 	  .flags = 0 },
78*4882a593Smuzhiyun };
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun /*
81*4882a593Smuzhiyun  * Zydas retry rates table. Each line is listed in the same order as
82*4882a593Smuzhiyun  * in zd_rates[] and contains all the rate used when a packet is sent
83*4882a593Smuzhiyun  * starting with a given rates. Let's consider an example :
84*4882a593Smuzhiyun  *
85*4882a593Smuzhiyun  * "11 Mbits : 4, 3, 2, 1, 0" means :
86*4882a593Smuzhiyun  * - packet is sent using 4 different rates
87*4882a593Smuzhiyun  * - 1st rate is index 3 (ie 11 Mbits)
88*4882a593Smuzhiyun  * - 2nd rate is index 2 (ie 5.5 Mbits)
89*4882a593Smuzhiyun  * - 3rd rate is index 1 (ie 2 Mbits)
90*4882a593Smuzhiyun  * - 4th rate is index 0 (ie 1 Mbits)
91*4882a593Smuzhiyun  */
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun static const struct tx_retry_rate zd_retry_rates[] = {
94*4882a593Smuzhiyun 	{ /*  1 Mbits */	1, { 0 }},
95*4882a593Smuzhiyun 	{ /*  2 Mbits */	2, { 1,  0 }},
96*4882a593Smuzhiyun 	{ /*  5.5 Mbits */	3, { 2,  1, 0 }},
97*4882a593Smuzhiyun 	{ /* 11 Mbits */	4, { 3,  2, 1, 0 }},
98*4882a593Smuzhiyun 	{ /*  6 Mbits */	5, { 4,  3, 2, 1, 0 }},
99*4882a593Smuzhiyun 	{ /*  9 Mbits */	6, { 5,  4, 3, 2, 1, 0}},
100*4882a593Smuzhiyun 	{ /* 12 Mbits */	5, { 6,  3, 2, 1, 0 }},
101*4882a593Smuzhiyun 	{ /* 18 Mbits */	6, { 7,  6, 3, 2, 1, 0 }},
102*4882a593Smuzhiyun 	{ /* 24 Mbits */	6, { 8,  6, 3, 2, 1, 0 }},
103*4882a593Smuzhiyun 	{ /* 36 Mbits */	7, { 9,  8, 6, 3, 2, 1, 0 }},
104*4882a593Smuzhiyun 	{ /* 48 Mbits */	8, {10,  9, 8, 6, 3, 2, 1, 0 }},
105*4882a593Smuzhiyun 	{ /* 54 Mbits */	9, {11, 10, 9, 8, 6, 3, 2, 1, 0 }}
106*4882a593Smuzhiyun };
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun static const struct ieee80211_channel zd_channels[] = {
109*4882a593Smuzhiyun 	{ .center_freq = 2412, .hw_value = 1 },
110*4882a593Smuzhiyun 	{ .center_freq = 2417, .hw_value = 2 },
111*4882a593Smuzhiyun 	{ .center_freq = 2422, .hw_value = 3 },
112*4882a593Smuzhiyun 	{ .center_freq = 2427, .hw_value = 4 },
113*4882a593Smuzhiyun 	{ .center_freq = 2432, .hw_value = 5 },
114*4882a593Smuzhiyun 	{ .center_freq = 2437, .hw_value = 6 },
115*4882a593Smuzhiyun 	{ .center_freq = 2442, .hw_value = 7 },
116*4882a593Smuzhiyun 	{ .center_freq = 2447, .hw_value = 8 },
117*4882a593Smuzhiyun 	{ .center_freq = 2452, .hw_value = 9 },
118*4882a593Smuzhiyun 	{ .center_freq = 2457, .hw_value = 10 },
119*4882a593Smuzhiyun 	{ .center_freq = 2462, .hw_value = 11 },
120*4882a593Smuzhiyun 	{ .center_freq = 2467, .hw_value = 12 },
121*4882a593Smuzhiyun 	{ .center_freq = 2472, .hw_value = 13 },
122*4882a593Smuzhiyun 	{ .center_freq = 2484, .hw_value = 14 },
123*4882a593Smuzhiyun };
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun static void housekeeping_init(struct zd_mac *mac);
126*4882a593Smuzhiyun static void housekeeping_enable(struct zd_mac *mac);
127*4882a593Smuzhiyun static void housekeeping_disable(struct zd_mac *mac);
128*4882a593Smuzhiyun static void beacon_init(struct zd_mac *mac);
129*4882a593Smuzhiyun static void beacon_enable(struct zd_mac *mac);
130*4882a593Smuzhiyun static void beacon_disable(struct zd_mac *mac);
131*4882a593Smuzhiyun static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble);
132*4882a593Smuzhiyun static int zd_mac_config_beacon(struct ieee80211_hw *hw,
133*4882a593Smuzhiyun 				struct sk_buff *beacon, bool in_intr);
134*4882a593Smuzhiyun 
zd_reg2alpha2(u8 regdomain,char * alpha2)135*4882a593Smuzhiyun static int zd_reg2alpha2(u8 regdomain, char *alpha2)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun 	unsigned int i;
138*4882a593Smuzhiyun 	struct zd_reg_alpha2_map *reg_map;
139*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(reg_alpha2_map); i++) {
140*4882a593Smuzhiyun 		reg_map = &reg_alpha2_map[i];
141*4882a593Smuzhiyun 		if (regdomain == reg_map->reg) {
142*4882a593Smuzhiyun 			alpha2[0] = reg_map->alpha2[0];
143*4882a593Smuzhiyun 			alpha2[1] = reg_map->alpha2[1];
144*4882a593Smuzhiyun 			return 0;
145*4882a593Smuzhiyun 		}
146*4882a593Smuzhiyun 	}
147*4882a593Smuzhiyun 	return 1;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun 
zd_check_signal(struct ieee80211_hw * hw,int signal)150*4882a593Smuzhiyun static int zd_check_signal(struct ieee80211_hw *hw, int signal)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	struct zd_mac *mac = zd_hw_mac(hw);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	dev_dbg_f_cond(zd_mac_dev(mac), signal < 0 || signal > 100,
155*4882a593Smuzhiyun 			"%s: signal value from device not in range 0..100, "
156*4882a593Smuzhiyun 			"but %d.\n", __func__, signal);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	if (signal < 0)
159*4882a593Smuzhiyun 		signal = 0;
160*4882a593Smuzhiyun 	else if (signal > 100)
161*4882a593Smuzhiyun 		signal = 100;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	return signal;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun 
zd_mac_preinit_hw(struct ieee80211_hw * hw)166*4882a593Smuzhiyun int zd_mac_preinit_hw(struct ieee80211_hw *hw)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	int r;
169*4882a593Smuzhiyun 	u8 addr[ETH_ALEN];
170*4882a593Smuzhiyun 	struct zd_mac *mac = zd_hw_mac(hw);
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	r = zd_chip_read_mac_addr_fw(&mac->chip, addr);
173*4882a593Smuzhiyun 	if (r)
174*4882a593Smuzhiyun 		return r;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	SET_IEEE80211_PERM_ADDR(hw, addr);
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	return 0;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun 
zd_mac_init_hw(struct ieee80211_hw * hw)181*4882a593Smuzhiyun int zd_mac_init_hw(struct ieee80211_hw *hw)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun 	int r;
184*4882a593Smuzhiyun 	struct zd_mac *mac = zd_hw_mac(hw);
185*4882a593Smuzhiyun 	struct zd_chip *chip = &mac->chip;
186*4882a593Smuzhiyun 	char alpha2[2];
187*4882a593Smuzhiyun 	u8 default_regdomain;
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	r = zd_chip_enable_int(chip);
190*4882a593Smuzhiyun 	if (r)
191*4882a593Smuzhiyun 		goto out;
192*4882a593Smuzhiyun 	r = zd_chip_init_hw(chip);
193*4882a593Smuzhiyun 	if (r)
194*4882a593Smuzhiyun 		goto disable_int;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 	ZD_ASSERT(!irqs_disabled());
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	r = zd_read_regdomain(chip, &default_regdomain);
199*4882a593Smuzhiyun 	if (r)
200*4882a593Smuzhiyun 		goto disable_int;
201*4882a593Smuzhiyun 	spin_lock_irq(&mac->lock);
202*4882a593Smuzhiyun 	mac->regdomain = mac->default_regdomain = default_regdomain;
203*4882a593Smuzhiyun 	spin_unlock_irq(&mac->lock);
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	/* We must inform the device that we are doing encryption/decryption in
206*4882a593Smuzhiyun 	 * software at the moment. */
207*4882a593Smuzhiyun 	r = zd_set_encryption_type(chip, ENC_SNIFFER);
208*4882a593Smuzhiyun 	if (r)
209*4882a593Smuzhiyun 		goto disable_int;
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	r = zd_reg2alpha2(mac->regdomain, alpha2);
212*4882a593Smuzhiyun 	if (r)
213*4882a593Smuzhiyun 		goto disable_int;
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	r = regulatory_hint(hw->wiphy, alpha2);
216*4882a593Smuzhiyun disable_int:
217*4882a593Smuzhiyun 	zd_chip_disable_int(chip);
218*4882a593Smuzhiyun out:
219*4882a593Smuzhiyun 	return r;
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun 
zd_mac_clear(struct zd_mac * mac)222*4882a593Smuzhiyun void zd_mac_clear(struct zd_mac *mac)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun 	flush_workqueue(zd_workqueue);
225*4882a593Smuzhiyun 	zd_chip_clear(&mac->chip);
226*4882a593Smuzhiyun 	ZD_MEMCLEAR(mac, sizeof(struct zd_mac));
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun 
set_rx_filter(struct zd_mac * mac)229*4882a593Smuzhiyun static int set_rx_filter(struct zd_mac *mac)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun 	unsigned long flags;
232*4882a593Smuzhiyun 	u32 filter = STA_RX_FILTER;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	spin_lock_irqsave(&mac->lock, flags);
235*4882a593Smuzhiyun 	if (mac->pass_ctrl)
236*4882a593Smuzhiyun 		filter |= RX_FILTER_CTRL;
237*4882a593Smuzhiyun 	spin_unlock_irqrestore(&mac->lock, flags);
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	return zd_iowrite32(&mac->chip, CR_RX_FILTER, filter);
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun 
set_mac_and_bssid(struct zd_mac * mac)242*4882a593Smuzhiyun static int set_mac_and_bssid(struct zd_mac *mac)
243*4882a593Smuzhiyun {
244*4882a593Smuzhiyun 	int r;
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	if (!mac->vif)
247*4882a593Smuzhiyun 		return -1;
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	r = zd_write_mac_addr(&mac->chip, mac->vif->addr);
250*4882a593Smuzhiyun 	if (r)
251*4882a593Smuzhiyun 		return r;
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	/* Vendor driver after setting MAC either sets BSSID for AP or
254*4882a593Smuzhiyun 	 * filter for other modes.
255*4882a593Smuzhiyun 	 */
256*4882a593Smuzhiyun 	if (mac->type != NL80211_IFTYPE_AP)
257*4882a593Smuzhiyun 		return set_rx_filter(mac);
258*4882a593Smuzhiyun 	else
259*4882a593Smuzhiyun 		return zd_write_bssid(&mac->chip, mac->vif->addr);
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun 
set_mc_hash(struct zd_mac * mac)262*4882a593Smuzhiyun static int set_mc_hash(struct zd_mac *mac)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	struct zd_mc_hash hash;
265*4882a593Smuzhiyun 	zd_mc_clear(&hash);
266*4882a593Smuzhiyun 	return zd_chip_set_multicast_hash(&mac->chip, &hash);
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun 
zd_op_start(struct ieee80211_hw * hw)269*4882a593Smuzhiyun int zd_op_start(struct ieee80211_hw *hw)
270*4882a593Smuzhiyun {
271*4882a593Smuzhiyun 	struct zd_mac *mac = zd_hw_mac(hw);
272*4882a593Smuzhiyun 	struct zd_chip *chip = &mac->chip;
273*4882a593Smuzhiyun 	struct zd_usb *usb = &chip->usb;
274*4882a593Smuzhiyun 	int r;
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	if (!usb->initialized) {
277*4882a593Smuzhiyun 		r = zd_usb_init_hw(usb);
278*4882a593Smuzhiyun 		if (r)
279*4882a593Smuzhiyun 			goto out;
280*4882a593Smuzhiyun 	}
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	r = zd_chip_enable_int(chip);
283*4882a593Smuzhiyun 	if (r < 0)
284*4882a593Smuzhiyun 		goto out;
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	r = zd_chip_set_basic_rates(chip, CR_RATES_80211B | CR_RATES_80211G);
287*4882a593Smuzhiyun 	if (r < 0)
288*4882a593Smuzhiyun 		goto disable_int;
289*4882a593Smuzhiyun 	r = set_rx_filter(mac);
290*4882a593Smuzhiyun 	if (r)
291*4882a593Smuzhiyun 		goto disable_int;
292*4882a593Smuzhiyun 	r = set_mc_hash(mac);
293*4882a593Smuzhiyun 	if (r)
294*4882a593Smuzhiyun 		goto disable_int;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	/* Wait after setting the multicast hash table and powering on
297*4882a593Smuzhiyun 	 * the radio otherwise interface bring up will fail. This matches
298*4882a593Smuzhiyun 	 * what the vendor driver did.
299*4882a593Smuzhiyun 	 */
300*4882a593Smuzhiyun 	msleep(10);
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	r = zd_chip_switch_radio_on(chip);
303*4882a593Smuzhiyun 	if (r < 0) {
304*4882a593Smuzhiyun 		dev_err(zd_chip_dev(chip),
305*4882a593Smuzhiyun 			"%s: failed to set radio on\n", __func__);
306*4882a593Smuzhiyun 		goto disable_int;
307*4882a593Smuzhiyun 	}
308*4882a593Smuzhiyun 	r = zd_chip_enable_rxtx(chip);
309*4882a593Smuzhiyun 	if (r < 0)
310*4882a593Smuzhiyun 		goto disable_radio;
311*4882a593Smuzhiyun 	r = zd_chip_enable_hwint(chip);
312*4882a593Smuzhiyun 	if (r < 0)
313*4882a593Smuzhiyun 		goto disable_rxtx;
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun 	housekeeping_enable(mac);
316*4882a593Smuzhiyun 	beacon_enable(mac);
317*4882a593Smuzhiyun 	set_bit(ZD_DEVICE_RUNNING, &mac->flags);
318*4882a593Smuzhiyun 	return 0;
319*4882a593Smuzhiyun disable_rxtx:
320*4882a593Smuzhiyun 	zd_chip_disable_rxtx(chip);
321*4882a593Smuzhiyun disable_radio:
322*4882a593Smuzhiyun 	zd_chip_switch_radio_off(chip);
323*4882a593Smuzhiyun disable_int:
324*4882a593Smuzhiyun 	zd_chip_disable_int(chip);
325*4882a593Smuzhiyun out:
326*4882a593Smuzhiyun 	return r;
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun 
zd_op_stop(struct ieee80211_hw * hw)329*4882a593Smuzhiyun void zd_op_stop(struct ieee80211_hw *hw)
330*4882a593Smuzhiyun {
331*4882a593Smuzhiyun 	struct zd_mac *mac = zd_hw_mac(hw);
332*4882a593Smuzhiyun 	struct zd_chip *chip = &mac->chip;
333*4882a593Smuzhiyun 	struct sk_buff *skb;
334*4882a593Smuzhiyun 	struct sk_buff_head *ack_wait_queue = &mac->ack_wait_queue;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	clear_bit(ZD_DEVICE_RUNNING, &mac->flags);
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 	/* The order here deliberately is a little different from the open()
339*4882a593Smuzhiyun 	 * method, since we need to make sure there is no opportunity for RX
340*4882a593Smuzhiyun 	 * frames to be processed by mac80211 after we have stopped it.
341*4882a593Smuzhiyun 	 */
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	zd_chip_disable_rxtx(chip);
344*4882a593Smuzhiyun 	beacon_disable(mac);
345*4882a593Smuzhiyun 	housekeeping_disable(mac);
346*4882a593Smuzhiyun 	flush_workqueue(zd_workqueue);
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	zd_chip_disable_hwint(chip);
349*4882a593Smuzhiyun 	zd_chip_switch_radio_off(chip);
350*4882a593Smuzhiyun 	zd_chip_disable_int(chip);
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	while ((skb = skb_dequeue(ack_wait_queue)))
354*4882a593Smuzhiyun 		dev_kfree_skb_any(skb);
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun 
zd_restore_settings(struct zd_mac * mac)357*4882a593Smuzhiyun int zd_restore_settings(struct zd_mac *mac)
358*4882a593Smuzhiyun {
359*4882a593Smuzhiyun 	struct sk_buff *beacon;
360*4882a593Smuzhiyun 	struct zd_mc_hash multicast_hash;
361*4882a593Smuzhiyun 	unsigned int short_preamble;
362*4882a593Smuzhiyun 	int r, beacon_interval, beacon_period;
363*4882a593Smuzhiyun 	u8 channel;
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 	dev_dbg_f(zd_mac_dev(mac), "\n");
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 	spin_lock_irq(&mac->lock);
368*4882a593Smuzhiyun 	multicast_hash = mac->multicast_hash;
369*4882a593Smuzhiyun 	short_preamble = mac->short_preamble;
370*4882a593Smuzhiyun 	beacon_interval = mac->beacon.interval;
371*4882a593Smuzhiyun 	beacon_period = mac->beacon.period;
372*4882a593Smuzhiyun 	channel = mac->channel;
373*4882a593Smuzhiyun 	spin_unlock_irq(&mac->lock);
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	r = set_mac_and_bssid(mac);
376*4882a593Smuzhiyun 	if (r < 0) {
377*4882a593Smuzhiyun 		dev_dbg_f(zd_mac_dev(mac), "set_mac_and_bssid failed, %d\n", r);
378*4882a593Smuzhiyun 		return r;
379*4882a593Smuzhiyun 	}
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	r = zd_chip_set_channel(&mac->chip, channel);
382*4882a593Smuzhiyun 	if (r < 0) {
383*4882a593Smuzhiyun 		dev_dbg_f(zd_mac_dev(mac), "zd_chip_set_channel failed, %d\n",
384*4882a593Smuzhiyun 			  r);
385*4882a593Smuzhiyun 		return r;
386*4882a593Smuzhiyun 	}
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	set_rts_cts(mac, short_preamble);
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	r = zd_chip_set_multicast_hash(&mac->chip, &multicast_hash);
391*4882a593Smuzhiyun 	if (r < 0) {
392*4882a593Smuzhiyun 		dev_dbg_f(zd_mac_dev(mac),
393*4882a593Smuzhiyun 			  "zd_chip_set_multicast_hash failed, %d\n", r);
394*4882a593Smuzhiyun 		return r;
395*4882a593Smuzhiyun 	}
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	if (mac->type == NL80211_IFTYPE_MESH_POINT ||
398*4882a593Smuzhiyun 	    mac->type == NL80211_IFTYPE_ADHOC ||
399*4882a593Smuzhiyun 	    mac->type == NL80211_IFTYPE_AP) {
400*4882a593Smuzhiyun 		if (mac->vif != NULL) {
401*4882a593Smuzhiyun 			beacon = ieee80211_beacon_get(mac->hw, mac->vif);
402*4882a593Smuzhiyun 			if (beacon)
403*4882a593Smuzhiyun 				zd_mac_config_beacon(mac->hw, beacon, false);
404*4882a593Smuzhiyun 		}
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 		zd_set_beacon_interval(&mac->chip, beacon_interval,
407*4882a593Smuzhiyun 					beacon_period, mac->type);
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 		spin_lock_irq(&mac->lock);
410*4882a593Smuzhiyun 		mac->beacon.last_update = jiffies;
411*4882a593Smuzhiyun 		spin_unlock_irq(&mac->lock);
412*4882a593Smuzhiyun 	}
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	return 0;
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun /**
418*4882a593Smuzhiyun  * zd_mac_tx_status - reports tx status of a packet if required
419*4882a593Smuzhiyun  * @hw: a &struct ieee80211_hw pointer
420*4882a593Smuzhiyun  * @skb: a sk-buffer
421*4882a593Smuzhiyun  * @ackssi: ACK signal strength
422*4882a593Smuzhiyun  * @tx_status: success and/or retry
423*4882a593Smuzhiyun  *
424*4882a593Smuzhiyun  * This information calls ieee80211_tx_status_irqsafe() if required by the
425*4882a593Smuzhiyun  * control information. It copies the control information into the status
426*4882a593Smuzhiyun  * information.
427*4882a593Smuzhiyun  *
428*4882a593Smuzhiyun  * If no status information has been requested, the skb is freed.
429*4882a593Smuzhiyun  */
zd_mac_tx_status(struct ieee80211_hw * hw,struct sk_buff * skb,int ackssi,struct tx_status * tx_status)430*4882a593Smuzhiyun static void zd_mac_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb,
431*4882a593Smuzhiyun 		      int ackssi, struct tx_status *tx_status)
432*4882a593Smuzhiyun {
433*4882a593Smuzhiyun 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
434*4882a593Smuzhiyun 	int i;
435*4882a593Smuzhiyun 	int success = 1, retry = 1;
436*4882a593Smuzhiyun 	int first_idx;
437*4882a593Smuzhiyun 	const struct tx_retry_rate *retries;
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	ieee80211_tx_info_clear_status(info);
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun 	if (tx_status) {
442*4882a593Smuzhiyun 		success = !tx_status->failure;
443*4882a593Smuzhiyun 		retry = tx_status->retry + success;
444*4882a593Smuzhiyun 	}
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	if (success) {
447*4882a593Smuzhiyun 		/* success */
448*4882a593Smuzhiyun 		info->flags |= IEEE80211_TX_STAT_ACK;
449*4882a593Smuzhiyun 	} else {
450*4882a593Smuzhiyun 		/* failure */
451*4882a593Smuzhiyun 		info->flags &= ~IEEE80211_TX_STAT_ACK;
452*4882a593Smuzhiyun 	}
453*4882a593Smuzhiyun 
454*4882a593Smuzhiyun 	first_idx = info->status.rates[0].idx;
455*4882a593Smuzhiyun 	ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
456*4882a593Smuzhiyun 	retries = &zd_retry_rates[first_idx];
457*4882a593Smuzhiyun 	ZD_ASSERT(1 <= retry && retry <= retries->count);
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	info->status.rates[0].idx = retries->rate[0];
460*4882a593Smuzhiyun 	info->status.rates[0].count = 1; // (retry > 1 ? 2 : 1);
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	for (i=1; i<IEEE80211_TX_MAX_RATES-1 && i<retry; i++) {
463*4882a593Smuzhiyun 		info->status.rates[i].idx = retries->rate[i];
464*4882a593Smuzhiyun 		info->status.rates[i].count = 1; // ((i==retry-1) && success ? 1:2);
465*4882a593Smuzhiyun 	}
466*4882a593Smuzhiyun 	for (; i<IEEE80211_TX_MAX_RATES && i<retry; i++) {
467*4882a593Smuzhiyun 		info->status.rates[i].idx = retries->rate[retry - 1];
468*4882a593Smuzhiyun 		info->status.rates[i].count = 1; // (success ? 1:2);
469*4882a593Smuzhiyun 	}
470*4882a593Smuzhiyun 	if (i<IEEE80211_TX_MAX_RATES)
471*4882a593Smuzhiyun 		info->status.rates[i].idx = -1; /* terminate */
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	info->status.ack_signal = zd_check_signal(hw, ackssi);
474*4882a593Smuzhiyun 	ieee80211_tx_status_irqsafe(hw, skb);
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun /**
478*4882a593Smuzhiyun  * zd_mac_tx_failed - callback for failed frames
479*4882a593Smuzhiyun  * @urb: pointer to the urb structure
480*4882a593Smuzhiyun  *
481*4882a593Smuzhiyun  * This function is called if a frame couldn't be successfully
482*4882a593Smuzhiyun  * transferred. The first frame from the tx queue, will be selected and
483*4882a593Smuzhiyun  * reported as error to the upper layers.
484*4882a593Smuzhiyun  */
zd_mac_tx_failed(struct urb * urb)485*4882a593Smuzhiyun void zd_mac_tx_failed(struct urb *urb)
486*4882a593Smuzhiyun {
487*4882a593Smuzhiyun 	struct ieee80211_hw * hw = zd_usb_to_hw(urb->context);
488*4882a593Smuzhiyun 	struct zd_mac *mac = zd_hw_mac(hw);
489*4882a593Smuzhiyun 	struct sk_buff_head *q = &mac->ack_wait_queue;
490*4882a593Smuzhiyun 	struct sk_buff *skb;
491*4882a593Smuzhiyun 	struct tx_status *tx_status = (struct tx_status *)urb->transfer_buffer;
492*4882a593Smuzhiyun 	unsigned long flags;
493*4882a593Smuzhiyun 	int success = !tx_status->failure;
494*4882a593Smuzhiyun 	int retry = tx_status->retry + success;
495*4882a593Smuzhiyun 	int found = 0;
496*4882a593Smuzhiyun 	int i, position = 0;
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	spin_lock_irqsave(&q->lock, flags);
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 	skb_queue_walk(q, skb) {
501*4882a593Smuzhiyun 		struct ieee80211_hdr *tx_hdr;
502*4882a593Smuzhiyun 		struct ieee80211_tx_info *info;
503*4882a593Smuzhiyun 		int first_idx, final_idx;
504*4882a593Smuzhiyun 		const struct tx_retry_rate *retries;
505*4882a593Smuzhiyun 		u8 final_rate;
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 		position ++;
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 		/* if the hardware reports a failure and we had a 802.11 ACK
510*4882a593Smuzhiyun 		 * pending, then we skip the first skb when searching for a
511*4882a593Smuzhiyun 		 * matching frame */
512*4882a593Smuzhiyun 		if (tx_status->failure && mac->ack_pending &&
513*4882a593Smuzhiyun 		    skb_queue_is_first(q, skb)) {
514*4882a593Smuzhiyun 			continue;
515*4882a593Smuzhiyun 		}
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 		tx_hdr = (struct ieee80211_hdr *)skb->data;
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 		/* we skip all frames not matching the reported destination */
520*4882a593Smuzhiyun 		if (unlikely(!ether_addr_equal(tx_hdr->addr1, tx_status->mac)))
521*4882a593Smuzhiyun 			continue;
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 		/* we skip all frames not matching the reported final rate */
524*4882a593Smuzhiyun 
525*4882a593Smuzhiyun 		info = IEEE80211_SKB_CB(skb);
526*4882a593Smuzhiyun 		first_idx = info->status.rates[0].idx;
527*4882a593Smuzhiyun 		ZD_ASSERT(0<=first_idx && first_idx<ARRAY_SIZE(zd_retry_rates));
528*4882a593Smuzhiyun 		retries = &zd_retry_rates[first_idx];
529*4882a593Smuzhiyun 		if (retry <= 0 || retry > retries->count)
530*4882a593Smuzhiyun 			continue;
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 		final_idx = retries->rate[retry - 1];
533*4882a593Smuzhiyun 		final_rate = zd_rates[final_idx].hw_value;
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 		if (final_rate != tx_status->rate) {
536*4882a593Smuzhiyun 			continue;
537*4882a593Smuzhiyun 		}
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 		found = 1;
540*4882a593Smuzhiyun 		break;
541*4882a593Smuzhiyun 	}
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 	if (found) {
544*4882a593Smuzhiyun 		for (i=1; i<=position; i++) {
545*4882a593Smuzhiyun 			skb = __skb_dequeue(q);
546*4882a593Smuzhiyun 			zd_mac_tx_status(hw, skb,
547*4882a593Smuzhiyun 					 mac->ack_pending ? mac->ack_signal : 0,
548*4882a593Smuzhiyun 					 i == position ? tx_status : NULL);
549*4882a593Smuzhiyun 			mac->ack_pending = 0;
550*4882a593Smuzhiyun 		}
551*4882a593Smuzhiyun 	}
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 	spin_unlock_irqrestore(&q->lock, flags);
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun /**
557*4882a593Smuzhiyun  * zd_mac_tx_to_dev - callback for USB layer
558*4882a593Smuzhiyun  * @skb: a &sk_buff pointer
559*4882a593Smuzhiyun  * @error: error value, 0 if transmission successful
560*4882a593Smuzhiyun  *
561*4882a593Smuzhiyun  * Informs the MAC layer that the frame has successfully transferred to the
562*4882a593Smuzhiyun  * device. If an ACK is required and the transfer to the device has been
563*4882a593Smuzhiyun  * successful, the packets are put on the @ack_wait_queue with
564*4882a593Smuzhiyun  * the control set removed.
565*4882a593Smuzhiyun  */
zd_mac_tx_to_dev(struct sk_buff * skb,int error)566*4882a593Smuzhiyun void zd_mac_tx_to_dev(struct sk_buff *skb, int error)
567*4882a593Smuzhiyun {
568*4882a593Smuzhiyun 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
569*4882a593Smuzhiyun 	struct ieee80211_hw *hw = info->rate_driver_data[0];
570*4882a593Smuzhiyun 	struct zd_mac *mac = zd_hw_mac(hw);
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun 	ieee80211_tx_info_clear_status(info);
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 	skb_pull(skb, sizeof(struct zd_ctrlset));
575*4882a593Smuzhiyun 	if (unlikely(error ||
576*4882a593Smuzhiyun 	    (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
577*4882a593Smuzhiyun 		/*
578*4882a593Smuzhiyun 		 * FIXME : do we need to fill in anything ?
579*4882a593Smuzhiyun 		 */
580*4882a593Smuzhiyun 		ieee80211_tx_status_irqsafe(hw, skb);
581*4882a593Smuzhiyun 	} else {
582*4882a593Smuzhiyun 		struct sk_buff_head *q = &mac->ack_wait_queue;
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 		skb_queue_tail(q, skb);
585*4882a593Smuzhiyun 		while (skb_queue_len(q) > ZD_MAC_MAX_ACK_WAITERS) {
586*4882a593Smuzhiyun 			zd_mac_tx_status(hw, skb_dequeue(q),
587*4882a593Smuzhiyun 					 mac->ack_pending ? mac->ack_signal : 0,
588*4882a593Smuzhiyun 					 NULL);
589*4882a593Smuzhiyun 			mac->ack_pending = 0;
590*4882a593Smuzhiyun 		}
591*4882a593Smuzhiyun 	}
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun 
zd_calc_tx_length_us(u8 * service,u8 zd_rate,u16 tx_length)594*4882a593Smuzhiyun static int zd_calc_tx_length_us(u8 *service, u8 zd_rate, u16 tx_length)
595*4882a593Smuzhiyun {
596*4882a593Smuzhiyun 	/* ZD_PURE_RATE() must be used to remove the modulation type flag of
597*4882a593Smuzhiyun 	 * the zd-rate values.
598*4882a593Smuzhiyun 	 */
599*4882a593Smuzhiyun 	static const u8 rate_divisor[] = {
600*4882a593Smuzhiyun 		[ZD_PURE_RATE(ZD_CCK_RATE_1M)]   =  1,
601*4882a593Smuzhiyun 		[ZD_PURE_RATE(ZD_CCK_RATE_2M)]	 =  2,
602*4882a593Smuzhiyun 		/* Bits must be doubled. */
603*4882a593Smuzhiyun 		[ZD_PURE_RATE(ZD_CCK_RATE_5_5M)] = 11,
604*4882a593Smuzhiyun 		[ZD_PURE_RATE(ZD_CCK_RATE_11M)]	 = 11,
605*4882a593Smuzhiyun 		[ZD_PURE_RATE(ZD_OFDM_RATE_6M)]  =  6,
606*4882a593Smuzhiyun 		[ZD_PURE_RATE(ZD_OFDM_RATE_9M)]  =  9,
607*4882a593Smuzhiyun 		[ZD_PURE_RATE(ZD_OFDM_RATE_12M)] = 12,
608*4882a593Smuzhiyun 		[ZD_PURE_RATE(ZD_OFDM_RATE_18M)] = 18,
609*4882a593Smuzhiyun 		[ZD_PURE_RATE(ZD_OFDM_RATE_24M)] = 24,
610*4882a593Smuzhiyun 		[ZD_PURE_RATE(ZD_OFDM_RATE_36M)] = 36,
611*4882a593Smuzhiyun 		[ZD_PURE_RATE(ZD_OFDM_RATE_48M)] = 48,
612*4882a593Smuzhiyun 		[ZD_PURE_RATE(ZD_OFDM_RATE_54M)] = 54,
613*4882a593Smuzhiyun 	};
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	u32 bits = (u32)tx_length * 8;
616*4882a593Smuzhiyun 	u32 divisor;
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun 	divisor = rate_divisor[ZD_PURE_RATE(zd_rate)];
619*4882a593Smuzhiyun 	if (divisor == 0)
620*4882a593Smuzhiyun 		return -EINVAL;
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	switch (zd_rate) {
623*4882a593Smuzhiyun 	case ZD_CCK_RATE_5_5M:
624*4882a593Smuzhiyun 		bits = (2*bits) + 10; /* round up to the next integer */
625*4882a593Smuzhiyun 		break;
626*4882a593Smuzhiyun 	case ZD_CCK_RATE_11M:
627*4882a593Smuzhiyun 		if (service) {
628*4882a593Smuzhiyun 			u32 t = bits % 11;
629*4882a593Smuzhiyun 			*service &= ~ZD_PLCP_SERVICE_LENGTH_EXTENSION;
630*4882a593Smuzhiyun 			if (0 < t && t <= 3) {
631*4882a593Smuzhiyun 				*service |= ZD_PLCP_SERVICE_LENGTH_EXTENSION;
632*4882a593Smuzhiyun 			}
633*4882a593Smuzhiyun 		}
634*4882a593Smuzhiyun 		bits += 10; /* round up to the next integer */
635*4882a593Smuzhiyun 		break;
636*4882a593Smuzhiyun 	}
637*4882a593Smuzhiyun 
638*4882a593Smuzhiyun 	return bits/divisor;
639*4882a593Smuzhiyun }
640*4882a593Smuzhiyun 
cs_set_control(struct zd_mac * mac,struct zd_ctrlset * cs,struct ieee80211_hdr * header,struct ieee80211_tx_info * info)641*4882a593Smuzhiyun static void cs_set_control(struct zd_mac *mac, struct zd_ctrlset *cs,
642*4882a593Smuzhiyun 	                   struct ieee80211_hdr *header,
643*4882a593Smuzhiyun 	                   struct ieee80211_tx_info *info)
644*4882a593Smuzhiyun {
645*4882a593Smuzhiyun 	/*
646*4882a593Smuzhiyun 	 * CONTROL TODO:
647*4882a593Smuzhiyun 	 * - if backoff needed, enable bit 0
648*4882a593Smuzhiyun 	 * - if burst (backoff not needed) disable bit 0
649*4882a593Smuzhiyun 	 */
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	cs->control = 0;
652*4882a593Smuzhiyun 
653*4882a593Smuzhiyun 	/* First fragment */
654*4882a593Smuzhiyun 	if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
655*4882a593Smuzhiyun 		cs->control |= ZD_CS_NEED_RANDOM_BACKOFF;
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 	/* No ACK expected (multicast, etc.) */
658*4882a593Smuzhiyun 	if (info->flags & IEEE80211_TX_CTL_NO_ACK)
659*4882a593Smuzhiyun 		cs->control |= ZD_CS_NO_ACK;
660*4882a593Smuzhiyun 
661*4882a593Smuzhiyun 	/* PS-POLL */
662*4882a593Smuzhiyun 	if (ieee80211_is_pspoll(header->frame_control))
663*4882a593Smuzhiyun 		cs->control |= ZD_CS_PS_POLL_FRAME;
664*4882a593Smuzhiyun 
665*4882a593Smuzhiyun 	if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS)
666*4882a593Smuzhiyun 		cs->control |= ZD_CS_RTS;
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
669*4882a593Smuzhiyun 		cs->control |= ZD_CS_SELF_CTS;
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun 	/* FIXME: Management frame? */
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun 
zd_mac_match_cur_beacon(struct zd_mac * mac,struct sk_buff * beacon)674*4882a593Smuzhiyun static bool zd_mac_match_cur_beacon(struct zd_mac *mac, struct sk_buff *beacon)
675*4882a593Smuzhiyun {
676*4882a593Smuzhiyun 	if (!mac->beacon.cur_beacon)
677*4882a593Smuzhiyun 		return false;
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 	if (mac->beacon.cur_beacon->len != beacon->len)
680*4882a593Smuzhiyun 		return false;
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 	return !memcmp(beacon->data, mac->beacon.cur_beacon->data, beacon->len);
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun 
zd_mac_free_cur_beacon_locked(struct zd_mac * mac)685*4882a593Smuzhiyun static void zd_mac_free_cur_beacon_locked(struct zd_mac *mac)
686*4882a593Smuzhiyun {
687*4882a593Smuzhiyun 	ZD_ASSERT(mutex_is_locked(&mac->chip.mutex));
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	kfree_skb(mac->beacon.cur_beacon);
690*4882a593Smuzhiyun 	mac->beacon.cur_beacon = NULL;
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun 
zd_mac_free_cur_beacon(struct zd_mac * mac)693*4882a593Smuzhiyun static void zd_mac_free_cur_beacon(struct zd_mac *mac)
694*4882a593Smuzhiyun {
695*4882a593Smuzhiyun 	mutex_lock(&mac->chip.mutex);
696*4882a593Smuzhiyun 	zd_mac_free_cur_beacon_locked(mac);
697*4882a593Smuzhiyun 	mutex_unlock(&mac->chip.mutex);
698*4882a593Smuzhiyun }
699*4882a593Smuzhiyun 
zd_mac_config_beacon(struct ieee80211_hw * hw,struct sk_buff * beacon,bool in_intr)700*4882a593Smuzhiyun static int zd_mac_config_beacon(struct ieee80211_hw *hw, struct sk_buff *beacon,
701*4882a593Smuzhiyun 				bool in_intr)
702*4882a593Smuzhiyun {
703*4882a593Smuzhiyun 	struct zd_mac *mac = zd_hw_mac(hw);
704*4882a593Smuzhiyun 	int r, ret, num_cmds, req_pos = 0;
705*4882a593Smuzhiyun 	u32 tmp, j = 0;
706*4882a593Smuzhiyun 	/* 4 more bytes for tail CRC */
707*4882a593Smuzhiyun 	u32 full_len = beacon->len + 4;
708*4882a593Smuzhiyun 	unsigned long end_jiffies, message_jiffies;
709*4882a593Smuzhiyun 	struct zd_ioreq32 *ioreqs;
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 	mutex_lock(&mac->chip.mutex);
712*4882a593Smuzhiyun 
713*4882a593Smuzhiyun 	/* Check if hw already has this beacon. */
714*4882a593Smuzhiyun 	if (zd_mac_match_cur_beacon(mac, beacon)) {
715*4882a593Smuzhiyun 		r = 0;
716*4882a593Smuzhiyun 		goto out_nofree;
717*4882a593Smuzhiyun 	}
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun 	/* Alloc memory for full beacon write at once. */
720*4882a593Smuzhiyun 	num_cmds = 1 + zd_chip_is_zd1211b(&mac->chip) + full_len;
721*4882a593Smuzhiyun 	ioreqs = kmalloc_array(num_cmds, sizeof(struct zd_ioreq32),
722*4882a593Smuzhiyun 			       GFP_KERNEL);
723*4882a593Smuzhiyun 	if (!ioreqs) {
724*4882a593Smuzhiyun 		r = -ENOMEM;
725*4882a593Smuzhiyun 		goto out_nofree;
726*4882a593Smuzhiyun 	}
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	r = zd_iowrite32_locked(&mac->chip, 0, CR_BCN_FIFO_SEMAPHORE);
729*4882a593Smuzhiyun 	if (r < 0)
730*4882a593Smuzhiyun 		goto out;
731*4882a593Smuzhiyun 	r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
732*4882a593Smuzhiyun 	if (r < 0)
733*4882a593Smuzhiyun 		goto release_sema;
734*4882a593Smuzhiyun 	if (in_intr && tmp & 0x2) {
735*4882a593Smuzhiyun 		r = -EBUSY;
736*4882a593Smuzhiyun 		goto release_sema;
737*4882a593Smuzhiyun 	}
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	end_jiffies = jiffies + HZ / 2; /*~500ms*/
740*4882a593Smuzhiyun 	message_jiffies = jiffies + HZ / 10; /*~100ms*/
741*4882a593Smuzhiyun 	while (tmp & 0x2) {
742*4882a593Smuzhiyun 		r = zd_ioread32_locked(&mac->chip, &tmp, CR_BCN_FIFO_SEMAPHORE);
743*4882a593Smuzhiyun 		if (r < 0)
744*4882a593Smuzhiyun 			goto release_sema;
745*4882a593Smuzhiyun 		if (time_is_before_eq_jiffies(message_jiffies)) {
746*4882a593Smuzhiyun 			message_jiffies = jiffies + HZ / 10;
747*4882a593Smuzhiyun 			dev_err(zd_mac_dev(mac),
748*4882a593Smuzhiyun 					"CR_BCN_FIFO_SEMAPHORE not ready\n");
749*4882a593Smuzhiyun 			if (time_is_before_eq_jiffies(end_jiffies))  {
750*4882a593Smuzhiyun 				dev_err(zd_mac_dev(mac),
751*4882a593Smuzhiyun 						"Giving up beacon config.\n");
752*4882a593Smuzhiyun 				r = -ETIMEDOUT;
753*4882a593Smuzhiyun 				goto reset_device;
754*4882a593Smuzhiyun 			}
755*4882a593Smuzhiyun 		}
756*4882a593Smuzhiyun 		msleep(20);
757*4882a593Smuzhiyun 	}
758*4882a593Smuzhiyun 
759*4882a593Smuzhiyun 	ioreqs[req_pos].addr = CR_BCN_FIFO;
760*4882a593Smuzhiyun 	ioreqs[req_pos].value = full_len - 1;
761*4882a593Smuzhiyun 	req_pos++;
762*4882a593Smuzhiyun 	if (zd_chip_is_zd1211b(&mac->chip)) {
763*4882a593Smuzhiyun 		ioreqs[req_pos].addr = CR_BCN_LENGTH;
764*4882a593Smuzhiyun 		ioreqs[req_pos].value = full_len - 1;
765*4882a593Smuzhiyun 		req_pos++;
766*4882a593Smuzhiyun 	}
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	for (j = 0 ; j < beacon->len; j++) {
769*4882a593Smuzhiyun 		ioreqs[req_pos].addr = CR_BCN_FIFO;
770*4882a593Smuzhiyun 		ioreqs[req_pos].value = *((u8 *)(beacon->data + j));
771*4882a593Smuzhiyun 		req_pos++;
772*4882a593Smuzhiyun 	}
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 	for (j = 0; j < 4; j++) {
775*4882a593Smuzhiyun 		ioreqs[req_pos].addr = CR_BCN_FIFO;
776*4882a593Smuzhiyun 		ioreqs[req_pos].value = 0x0;
777*4882a593Smuzhiyun 		req_pos++;
778*4882a593Smuzhiyun 	}
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 	BUG_ON(req_pos != num_cmds);
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	r = zd_iowrite32a_locked(&mac->chip, ioreqs, num_cmds);
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun release_sema:
785*4882a593Smuzhiyun 	/*
786*4882a593Smuzhiyun 	 * Try very hard to release device beacon semaphore, as otherwise
787*4882a593Smuzhiyun 	 * device/driver can be left in unusable state.
788*4882a593Smuzhiyun 	 */
789*4882a593Smuzhiyun 	end_jiffies = jiffies + HZ / 2; /*~500ms*/
790*4882a593Smuzhiyun 	ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
791*4882a593Smuzhiyun 	while (ret < 0) {
792*4882a593Smuzhiyun 		if (in_intr || time_is_before_eq_jiffies(end_jiffies)) {
793*4882a593Smuzhiyun 			ret = -ETIMEDOUT;
794*4882a593Smuzhiyun 			break;
795*4882a593Smuzhiyun 		}
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 		msleep(20);
798*4882a593Smuzhiyun 		ret = zd_iowrite32_locked(&mac->chip, 1, CR_BCN_FIFO_SEMAPHORE);
799*4882a593Smuzhiyun 	}
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun 	if (ret < 0)
802*4882a593Smuzhiyun 		dev_err(zd_mac_dev(mac), "Could not release "
803*4882a593Smuzhiyun 					 "CR_BCN_FIFO_SEMAPHORE!\n");
804*4882a593Smuzhiyun 	if (r < 0 || ret < 0) {
805*4882a593Smuzhiyun 		if (r >= 0)
806*4882a593Smuzhiyun 			r = ret;
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 		/* We don't know if beacon was written successfully or not,
809*4882a593Smuzhiyun 		 * so clear current. */
810*4882a593Smuzhiyun 		zd_mac_free_cur_beacon_locked(mac);
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun 		goto out;
813*4882a593Smuzhiyun 	}
814*4882a593Smuzhiyun 
815*4882a593Smuzhiyun 	/* Beacon has now been written successfully, update current. */
816*4882a593Smuzhiyun 	zd_mac_free_cur_beacon_locked(mac);
817*4882a593Smuzhiyun 	mac->beacon.cur_beacon = beacon;
818*4882a593Smuzhiyun 	beacon = NULL;
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 	/* 802.11b/g 2.4G CCK 1Mb
821*4882a593Smuzhiyun 	 * 802.11a, not yet implemented, uses different values (see GPL vendor
822*4882a593Smuzhiyun 	 * driver)
823*4882a593Smuzhiyun 	 */
824*4882a593Smuzhiyun 	r = zd_iowrite32_locked(&mac->chip, 0x00000400 | (full_len << 19),
825*4882a593Smuzhiyun 				CR_BCN_PLCP_CFG);
826*4882a593Smuzhiyun out:
827*4882a593Smuzhiyun 	kfree(ioreqs);
828*4882a593Smuzhiyun out_nofree:
829*4882a593Smuzhiyun 	kfree_skb(beacon);
830*4882a593Smuzhiyun 	mutex_unlock(&mac->chip.mutex);
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	return r;
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun reset_device:
835*4882a593Smuzhiyun 	zd_mac_free_cur_beacon_locked(mac);
836*4882a593Smuzhiyun 	kfree_skb(beacon);
837*4882a593Smuzhiyun 
838*4882a593Smuzhiyun 	mutex_unlock(&mac->chip.mutex);
839*4882a593Smuzhiyun 	kfree(ioreqs);
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	/* semaphore stuck, reset device to avoid fw freeze later */
842*4882a593Smuzhiyun 	dev_warn(zd_mac_dev(mac), "CR_BCN_FIFO_SEMAPHORE stuck, "
843*4882a593Smuzhiyun 				  "resetting device...");
844*4882a593Smuzhiyun 	usb_queue_reset_device(mac->chip.usb.intf);
845*4882a593Smuzhiyun 
846*4882a593Smuzhiyun 	return r;
847*4882a593Smuzhiyun }
848*4882a593Smuzhiyun 
fill_ctrlset(struct zd_mac * mac,struct sk_buff * skb)849*4882a593Smuzhiyun static int fill_ctrlset(struct zd_mac *mac,
850*4882a593Smuzhiyun 			struct sk_buff *skb)
851*4882a593Smuzhiyun {
852*4882a593Smuzhiyun 	int r;
853*4882a593Smuzhiyun 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
854*4882a593Smuzhiyun 	unsigned int frag_len = skb->len + FCS_LEN;
855*4882a593Smuzhiyun 	unsigned int packet_length;
856*4882a593Smuzhiyun 	struct ieee80211_rate *txrate;
857*4882a593Smuzhiyun 	struct zd_ctrlset *cs = skb_push(skb, sizeof(struct zd_ctrlset));
858*4882a593Smuzhiyun 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
859*4882a593Smuzhiyun 
860*4882a593Smuzhiyun 	ZD_ASSERT(frag_len <= 0xffff);
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	/*
863*4882a593Smuzhiyun 	 * Firmware computes the duration itself (for all frames except PSPoll)
864*4882a593Smuzhiyun 	 * and needs the field set to 0 at input, otherwise firmware messes up
865*4882a593Smuzhiyun 	 * duration_id and sets bits 14 and 15 on.
866*4882a593Smuzhiyun 	 */
867*4882a593Smuzhiyun 	if (!ieee80211_is_pspoll(hdr->frame_control))
868*4882a593Smuzhiyun 		hdr->duration_id = 0;
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun 	txrate = ieee80211_get_tx_rate(mac->hw, info);
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun 	cs->modulation = txrate->hw_value;
873*4882a593Smuzhiyun 	if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
874*4882a593Smuzhiyun 		cs->modulation = txrate->hw_value_short;
875*4882a593Smuzhiyun 
876*4882a593Smuzhiyun 	cs->tx_length = cpu_to_le16(frag_len);
877*4882a593Smuzhiyun 
878*4882a593Smuzhiyun 	cs_set_control(mac, cs, hdr, info);
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun 	packet_length = frag_len + sizeof(struct zd_ctrlset) + 10;
881*4882a593Smuzhiyun 	ZD_ASSERT(packet_length <= 0xffff);
882*4882a593Smuzhiyun 	/* ZD1211B: Computing the length difference this way, gives us
883*4882a593Smuzhiyun 	 * flexibility to compute the packet length.
884*4882a593Smuzhiyun 	 */
885*4882a593Smuzhiyun 	cs->packet_length = cpu_to_le16(zd_chip_is_zd1211b(&mac->chip) ?
886*4882a593Smuzhiyun 			packet_length - frag_len : packet_length);
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 	/*
889*4882a593Smuzhiyun 	 * CURRENT LENGTH:
890*4882a593Smuzhiyun 	 * - transmit frame length in microseconds
891*4882a593Smuzhiyun 	 * - seems to be derived from frame length
892*4882a593Smuzhiyun 	 * - see Cal_Us_Service() in zdinlinef.h
893*4882a593Smuzhiyun 	 * - if macp->bTxBurstEnable is enabled, then multiply by 4
894*4882a593Smuzhiyun 	 *  - bTxBurstEnable is never set in the vendor driver
895*4882a593Smuzhiyun 	 *
896*4882a593Smuzhiyun 	 * SERVICE:
897*4882a593Smuzhiyun 	 * - "for PLCP configuration"
898*4882a593Smuzhiyun 	 * - always 0 except in some situations at 802.11b 11M
899*4882a593Smuzhiyun 	 * - see line 53 of zdinlinef.h
900*4882a593Smuzhiyun 	 */
901*4882a593Smuzhiyun 	cs->service = 0;
902*4882a593Smuzhiyun 	r = zd_calc_tx_length_us(&cs->service, ZD_RATE(cs->modulation),
903*4882a593Smuzhiyun 		                 le16_to_cpu(cs->tx_length));
904*4882a593Smuzhiyun 	if (r < 0)
905*4882a593Smuzhiyun 		return r;
906*4882a593Smuzhiyun 	cs->current_length = cpu_to_le16(r);
907*4882a593Smuzhiyun 	cs->next_frame_length = 0;
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 	return 0;
910*4882a593Smuzhiyun }
911*4882a593Smuzhiyun 
912*4882a593Smuzhiyun /**
913*4882a593Smuzhiyun  * zd_op_tx - transmits a network frame to the device
914*4882a593Smuzhiyun  *
915*4882a593Smuzhiyun  * @hw: a &struct ieee80211_hw pointer
916*4882a593Smuzhiyun  * @control: the control structure
917*4882a593Smuzhiyun  * @skb: socket buffer
918*4882a593Smuzhiyun  *
919*4882a593Smuzhiyun  * This function transmit an IEEE 802.11 network frame to the device. The
920*4882a593Smuzhiyun  * control block of the skbuff will be initialized. If necessary the incoming
921*4882a593Smuzhiyun  * mac80211 queues will be stopped.
922*4882a593Smuzhiyun  */
zd_op_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)923*4882a593Smuzhiyun static void zd_op_tx(struct ieee80211_hw *hw,
924*4882a593Smuzhiyun 		     struct ieee80211_tx_control *control,
925*4882a593Smuzhiyun 		     struct sk_buff *skb)
926*4882a593Smuzhiyun {
927*4882a593Smuzhiyun 	struct zd_mac *mac = zd_hw_mac(hw);
928*4882a593Smuzhiyun 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
929*4882a593Smuzhiyun 	int r;
930*4882a593Smuzhiyun 
931*4882a593Smuzhiyun 	r = fill_ctrlset(mac, skb);
932*4882a593Smuzhiyun 	if (r)
933*4882a593Smuzhiyun 		goto fail;
934*4882a593Smuzhiyun 
935*4882a593Smuzhiyun 	info->rate_driver_data[0] = hw;
936*4882a593Smuzhiyun 
937*4882a593Smuzhiyun 	r = zd_usb_tx(&mac->chip.usb, skb);
938*4882a593Smuzhiyun 	if (r)
939*4882a593Smuzhiyun 		goto fail;
940*4882a593Smuzhiyun 	return;
941*4882a593Smuzhiyun 
942*4882a593Smuzhiyun fail:
943*4882a593Smuzhiyun 	dev_kfree_skb(skb);
944*4882a593Smuzhiyun }
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun /**
947*4882a593Smuzhiyun  * filter_ack - filters incoming packets for acknowledgements
948*4882a593Smuzhiyun  * @hw: a &struct ieee80211_hw pointer
949*4882a593Smuzhiyun  * @rx_hdr: received header
950*4882a593Smuzhiyun  * @stats: the status for the received packet
951*4882a593Smuzhiyun  *
952*4882a593Smuzhiyun  * This functions looks for ACK packets and tries to match them with the
953*4882a593Smuzhiyun  * frames in the tx queue. If a match is found the frame will be dequeued and
954*4882a593Smuzhiyun  * the upper layers is informed about the successful transmission. If
955*4882a593Smuzhiyun  * mac80211 queues have been stopped and the number of frames still to be
956*4882a593Smuzhiyun  * transmitted is low the queues will be opened again.
957*4882a593Smuzhiyun  *
958*4882a593Smuzhiyun  * Returns 1 if the frame was an ACK, 0 if it was ignored.
959*4882a593Smuzhiyun  */
filter_ack(struct ieee80211_hw * hw,struct ieee80211_hdr * rx_hdr,struct ieee80211_rx_status * stats)960*4882a593Smuzhiyun static int filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
961*4882a593Smuzhiyun 		      struct ieee80211_rx_status *stats)
962*4882a593Smuzhiyun {
963*4882a593Smuzhiyun 	struct zd_mac *mac = zd_hw_mac(hw);
964*4882a593Smuzhiyun 	struct sk_buff *skb;
965*4882a593Smuzhiyun 	struct sk_buff_head *q;
966*4882a593Smuzhiyun 	unsigned long flags;
967*4882a593Smuzhiyun 	int found = 0;
968*4882a593Smuzhiyun 	int i, position = 0;
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun 	if (!ieee80211_is_ack(rx_hdr->frame_control))
971*4882a593Smuzhiyun 		return 0;
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 	q = &mac->ack_wait_queue;
974*4882a593Smuzhiyun 	spin_lock_irqsave(&q->lock, flags);
975*4882a593Smuzhiyun 	skb_queue_walk(q, skb) {
976*4882a593Smuzhiyun 		struct ieee80211_hdr *tx_hdr;
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun 		position ++;
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 		if (mac->ack_pending && skb_queue_is_first(q, skb))
981*4882a593Smuzhiyun 		    continue;
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 		tx_hdr = (struct ieee80211_hdr *)skb->data;
984*4882a593Smuzhiyun 		if (likely(ether_addr_equal(tx_hdr->addr2, rx_hdr->addr1)))
985*4882a593Smuzhiyun 		{
986*4882a593Smuzhiyun 			found = 1;
987*4882a593Smuzhiyun 			break;
988*4882a593Smuzhiyun 		}
989*4882a593Smuzhiyun 	}
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun 	if (found) {
992*4882a593Smuzhiyun 		for (i=1; i<position; i++) {
993*4882a593Smuzhiyun 			skb = __skb_dequeue(q);
994*4882a593Smuzhiyun 			zd_mac_tx_status(hw, skb,
995*4882a593Smuzhiyun 					 mac->ack_pending ? mac->ack_signal : 0,
996*4882a593Smuzhiyun 					 NULL);
997*4882a593Smuzhiyun 			mac->ack_pending = 0;
998*4882a593Smuzhiyun 		}
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun 		mac->ack_pending = 1;
1001*4882a593Smuzhiyun 		mac->ack_signal = stats->signal;
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun 		/* Prevent pending tx-packet on AP-mode */
1004*4882a593Smuzhiyun 		if (mac->type == NL80211_IFTYPE_AP) {
1005*4882a593Smuzhiyun 			skb = __skb_dequeue(q);
1006*4882a593Smuzhiyun 			zd_mac_tx_status(hw, skb, mac->ack_signal, NULL);
1007*4882a593Smuzhiyun 			mac->ack_pending = 0;
1008*4882a593Smuzhiyun 		}
1009*4882a593Smuzhiyun 	}
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	spin_unlock_irqrestore(&q->lock, flags);
1012*4882a593Smuzhiyun 	return 1;
1013*4882a593Smuzhiyun }
1014*4882a593Smuzhiyun 
zd_mac_rx(struct ieee80211_hw * hw,const u8 * buffer,unsigned int length)1015*4882a593Smuzhiyun int zd_mac_rx(struct ieee80211_hw *hw, const u8 *buffer, unsigned int length)
1016*4882a593Smuzhiyun {
1017*4882a593Smuzhiyun 	struct zd_mac *mac = zd_hw_mac(hw);
1018*4882a593Smuzhiyun 	struct ieee80211_rx_status stats;
1019*4882a593Smuzhiyun 	const struct rx_status *status;
1020*4882a593Smuzhiyun 	struct sk_buff *skb;
1021*4882a593Smuzhiyun 	int bad_frame = 0;
1022*4882a593Smuzhiyun 	__le16 fc;
1023*4882a593Smuzhiyun 	int need_padding;
1024*4882a593Smuzhiyun 	int i;
1025*4882a593Smuzhiyun 	u8 rate;
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun 	if (length < ZD_PLCP_HEADER_SIZE + 10 /* IEEE80211_1ADDR_LEN */ +
1028*4882a593Smuzhiyun 	             FCS_LEN + sizeof(struct rx_status))
1029*4882a593Smuzhiyun 		return -EINVAL;
1030*4882a593Smuzhiyun 
1031*4882a593Smuzhiyun 	memset(&stats, 0, sizeof(stats));
1032*4882a593Smuzhiyun 
1033*4882a593Smuzhiyun 	/* Note about pass_failed_fcs and pass_ctrl access below:
1034*4882a593Smuzhiyun 	 * mac locking intentionally omitted here, as this is the only unlocked
1035*4882a593Smuzhiyun 	 * reader and the only writer is configure_filter. Plus, if there were
1036*4882a593Smuzhiyun 	 * any races accessing these variables, it wouldn't really matter.
1037*4882a593Smuzhiyun 	 * If mac80211 ever provides a way for us to access filter flags
1038*4882a593Smuzhiyun 	 * from outside configure_filter, we could improve on this. Also, this
1039*4882a593Smuzhiyun 	 * situation may change once we implement some kind of DMA-into-skb
1040*4882a593Smuzhiyun 	 * RX path. */
1041*4882a593Smuzhiyun 
1042*4882a593Smuzhiyun 	/* Caller has to ensure that length >= sizeof(struct rx_status). */
1043*4882a593Smuzhiyun 	status = (struct rx_status *)
1044*4882a593Smuzhiyun 		(buffer + (length - sizeof(struct rx_status)));
1045*4882a593Smuzhiyun 	if (status->frame_status & ZD_RX_ERROR) {
1046*4882a593Smuzhiyun 		if (mac->pass_failed_fcs &&
1047*4882a593Smuzhiyun 				(status->frame_status & ZD_RX_CRC32_ERROR)) {
1048*4882a593Smuzhiyun 			stats.flag |= RX_FLAG_FAILED_FCS_CRC;
1049*4882a593Smuzhiyun 			bad_frame = 1;
1050*4882a593Smuzhiyun 		} else {
1051*4882a593Smuzhiyun 			return -EINVAL;
1052*4882a593Smuzhiyun 		}
1053*4882a593Smuzhiyun 	}
1054*4882a593Smuzhiyun 
1055*4882a593Smuzhiyun 	stats.freq = zd_channels[_zd_chip_get_channel(&mac->chip) - 1].center_freq;
1056*4882a593Smuzhiyun 	stats.band = NL80211_BAND_2GHZ;
1057*4882a593Smuzhiyun 	stats.signal = zd_check_signal(hw, status->signal_strength);
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun 	rate = zd_rx_rate(buffer, status);
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun 	/* todo: return index in the big switches in zd_rx_rate instead */
1062*4882a593Smuzhiyun 	for (i = 0; i < mac->band.n_bitrates; i++)
1063*4882a593Smuzhiyun 		if (rate == mac->band.bitrates[i].hw_value)
1064*4882a593Smuzhiyun 			stats.rate_idx = i;
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun 	length -= ZD_PLCP_HEADER_SIZE + sizeof(struct rx_status);
1067*4882a593Smuzhiyun 	buffer += ZD_PLCP_HEADER_SIZE;
1068*4882a593Smuzhiyun 
1069*4882a593Smuzhiyun 	/* Except for bad frames, filter each frame to see if it is an ACK, in
1070*4882a593Smuzhiyun 	 * which case our internal TX tracking is updated. Normally we then
1071*4882a593Smuzhiyun 	 * bail here as there's no need to pass ACKs on up to the stack, but
1072*4882a593Smuzhiyun 	 * there is also the case where the stack has requested us to pass
1073*4882a593Smuzhiyun 	 * control frames on up (pass_ctrl) which we must consider. */
1074*4882a593Smuzhiyun 	if (!bad_frame &&
1075*4882a593Smuzhiyun 			filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats)
1076*4882a593Smuzhiyun 			&& !mac->pass_ctrl)
1077*4882a593Smuzhiyun 		return 0;
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun 	fc = get_unaligned((__le16*)buffer);
1080*4882a593Smuzhiyun 	need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
1081*4882a593Smuzhiyun 
1082*4882a593Smuzhiyun 	skb = dev_alloc_skb(length + (need_padding ? 2 : 0));
1083*4882a593Smuzhiyun 	if (skb == NULL)
1084*4882a593Smuzhiyun 		return -ENOMEM;
1085*4882a593Smuzhiyun 	if (need_padding) {
1086*4882a593Smuzhiyun 		/* Make sure the payload data is 4 byte aligned. */
1087*4882a593Smuzhiyun 		skb_reserve(skb, 2);
1088*4882a593Smuzhiyun 	}
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun 	/* FIXME : could we avoid this big memcpy ? */
1091*4882a593Smuzhiyun 	skb_put_data(skb, buffer, length);
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun 	memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats));
1094*4882a593Smuzhiyun 	ieee80211_rx_irqsafe(hw, skb);
1095*4882a593Smuzhiyun 	return 0;
1096*4882a593Smuzhiyun }
1097*4882a593Smuzhiyun 
zd_op_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1098*4882a593Smuzhiyun static int zd_op_add_interface(struct ieee80211_hw *hw,
1099*4882a593Smuzhiyun 				struct ieee80211_vif *vif)
1100*4882a593Smuzhiyun {
1101*4882a593Smuzhiyun 	struct zd_mac *mac = zd_hw_mac(hw);
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun 	/* using NL80211_IFTYPE_UNSPECIFIED to indicate no mode selected */
1104*4882a593Smuzhiyun 	if (mac->type != NL80211_IFTYPE_UNSPECIFIED)
1105*4882a593Smuzhiyun 		return -EOPNOTSUPP;
1106*4882a593Smuzhiyun 
1107*4882a593Smuzhiyun 	switch (vif->type) {
1108*4882a593Smuzhiyun 	case NL80211_IFTYPE_MONITOR:
1109*4882a593Smuzhiyun 	case NL80211_IFTYPE_MESH_POINT:
1110*4882a593Smuzhiyun 	case NL80211_IFTYPE_STATION:
1111*4882a593Smuzhiyun 	case NL80211_IFTYPE_ADHOC:
1112*4882a593Smuzhiyun 	case NL80211_IFTYPE_AP:
1113*4882a593Smuzhiyun 		mac->type = vif->type;
1114*4882a593Smuzhiyun 		break;
1115*4882a593Smuzhiyun 	default:
1116*4882a593Smuzhiyun 		return -EOPNOTSUPP;
1117*4882a593Smuzhiyun 	}
1118*4882a593Smuzhiyun 
1119*4882a593Smuzhiyun 	mac->vif = vif;
1120*4882a593Smuzhiyun 
1121*4882a593Smuzhiyun 	return set_mac_and_bssid(mac);
1122*4882a593Smuzhiyun }
1123*4882a593Smuzhiyun 
zd_op_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1124*4882a593Smuzhiyun static void zd_op_remove_interface(struct ieee80211_hw *hw,
1125*4882a593Smuzhiyun 				    struct ieee80211_vif *vif)
1126*4882a593Smuzhiyun {
1127*4882a593Smuzhiyun 	struct zd_mac *mac = zd_hw_mac(hw);
1128*4882a593Smuzhiyun 	mac->type = NL80211_IFTYPE_UNSPECIFIED;
1129*4882a593Smuzhiyun 	mac->vif = NULL;
1130*4882a593Smuzhiyun 	zd_set_beacon_interval(&mac->chip, 0, 0, NL80211_IFTYPE_UNSPECIFIED);
1131*4882a593Smuzhiyun 	zd_write_mac_addr(&mac->chip, NULL);
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun 	zd_mac_free_cur_beacon(mac);
1134*4882a593Smuzhiyun }
1135*4882a593Smuzhiyun 
zd_op_config(struct ieee80211_hw * hw,u32 changed)1136*4882a593Smuzhiyun static int zd_op_config(struct ieee80211_hw *hw, u32 changed)
1137*4882a593Smuzhiyun {
1138*4882a593Smuzhiyun 	struct zd_mac *mac = zd_hw_mac(hw);
1139*4882a593Smuzhiyun 	struct ieee80211_conf *conf = &hw->conf;
1140*4882a593Smuzhiyun 
1141*4882a593Smuzhiyun 	spin_lock_irq(&mac->lock);
1142*4882a593Smuzhiyun 	mac->channel = conf->chandef.chan->hw_value;
1143*4882a593Smuzhiyun 	spin_unlock_irq(&mac->lock);
1144*4882a593Smuzhiyun 
1145*4882a593Smuzhiyun 	return zd_chip_set_channel(&mac->chip, conf->chandef.chan->hw_value);
1146*4882a593Smuzhiyun }
1147*4882a593Smuzhiyun 
zd_beacon_done(struct zd_mac * mac)1148*4882a593Smuzhiyun static void zd_beacon_done(struct zd_mac *mac)
1149*4882a593Smuzhiyun {
1150*4882a593Smuzhiyun 	struct sk_buff *skb, *beacon;
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun 	if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1153*4882a593Smuzhiyun 		return;
1154*4882a593Smuzhiyun 	if (!mac->vif || mac->vif->type != NL80211_IFTYPE_AP)
1155*4882a593Smuzhiyun 		return;
1156*4882a593Smuzhiyun 
1157*4882a593Smuzhiyun 	/*
1158*4882a593Smuzhiyun 	 * Send out buffered broad- and multicast frames.
1159*4882a593Smuzhiyun 	 */
1160*4882a593Smuzhiyun 	while (!ieee80211_queue_stopped(mac->hw, 0)) {
1161*4882a593Smuzhiyun 		skb = ieee80211_get_buffered_bc(mac->hw, mac->vif);
1162*4882a593Smuzhiyun 		if (!skb)
1163*4882a593Smuzhiyun 			break;
1164*4882a593Smuzhiyun 		zd_op_tx(mac->hw, NULL, skb);
1165*4882a593Smuzhiyun 	}
1166*4882a593Smuzhiyun 
1167*4882a593Smuzhiyun 	/*
1168*4882a593Smuzhiyun 	 * Fetch next beacon so that tim_count is updated.
1169*4882a593Smuzhiyun 	 */
1170*4882a593Smuzhiyun 	beacon = ieee80211_beacon_get(mac->hw, mac->vif);
1171*4882a593Smuzhiyun 	if (beacon)
1172*4882a593Smuzhiyun 		zd_mac_config_beacon(mac->hw, beacon, true);
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun 	spin_lock_irq(&mac->lock);
1175*4882a593Smuzhiyun 	mac->beacon.last_update = jiffies;
1176*4882a593Smuzhiyun 	spin_unlock_irq(&mac->lock);
1177*4882a593Smuzhiyun }
1178*4882a593Smuzhiyun 
zd_process_intr(struct work_struct * work)1179*4882a593Smuzhiyun static void zd_process_intr(struct work_struct *work)
1180*4882a593Smuzhiyun {
1181*4882a593Smuzhiyun 	u16 int_status;
1182*4882a593Smuzhiyun 	unsigned long flags;
1183*4882a593Smuzhiyun 	struct zd_mac *mac = container_of(work, struct zd_mac, process_intr);
1184*4882a593Smuzhiyun 
1185*4882a593Smuzhiyun 	spin_lock_irqsave(&mac->lock, flags);
1186*4882a593Smuzhiyun 	int_status = le16_to_cpu(*(__le16 *)(mac->intr_buffer + 4));
1187*4882a593Smuzhiyun 	spin_unlock_irqrestore(&mac->lock, flags);
1188*4882a593Smuzhiyun 
1189*4882a593Smuzhiyun 	if (int_status & INT_CFG_NEXT_BCN) {
1190*4882a593Smuzhiyun 		/*dev_dbg_f_limit(zd_mac_dev(mac), "INT_CFG_NEXT_BCN\n");*/
1191*4882a593Smuzhiyun 		zd_beacon_done(mac);
1192*4882a593Smuzhiyun 	} else {
1193*4882a593Smuzhiyun 		dev_dbg_f(zd_mac_dev(mac), "Unsupported interrupt\n");
1194*4882a593Smuzhiyun 	}
1195*4882a593Smuzhiyun 
1196*4882a593Smuzhiyun 	zd_chip_enable_hwint(&mac->chip);
1197*4882a593Smuzhiyun }
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun 
zd_op_prepare_multicast(struct ieee80211_hw * hw,struct netdev_hw_addr_list * mc_list)1200*4882a593Smuzhiyun static u64 zd_op_prepare_multicast(struct ieee80211_hw *hw,
1201*4882a593Smuzhiyun 				   struct netdev_hw_addr_list *mc_list)
1202*4882a593Smuzhiyun {
1203*4882a593Smuzhiyun 	struct zd_mac *mac = zd_hw_mac(hw);
1204*4882a593Smuzhiyun 	struct zd_mc_hash hash;
1205*4882a593Smuzhiyun 	struct netdev_hw_addr *ha;
1206*4882a593Smuzhiyun 
1207*4882a593Smuzhiyun 	zd_mc_clear(&hash);
1208*4882a593Smuzhiyun 
1209*4882a593Smuzhiyun 	netdev_hw_addr_list_for_each(ha, mc_list) {
1210*4882a593Smuzhiyun 		dev_dbg_f(zd_mac_dev(mac), "mc addr %pM\n", ha->addr);
1211*4882a593Smuzhiyun 		zd_mc_add_addr(&hash, ha->addr);
1212*4882a593Smuzhiyun 	}
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun 	return hash.low | ((u64)hash.high << 32);
1215*4882a593Smuzhiyun }
1216*4882a593Smuzhiyun 
1217*4882a593Smuzhiyun #define SUPPORTED_FIF_FLAGS \
1218*4882a593Smuzhiyun 	(FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
1219*4882a593Smuzhiyun 	FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
zd_op_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * new_flags,u64 multicast)1220*4882a593Smuzhiyun static void zd_op_configure_filter(struct ieee80211_hw *hw,
1221*4882a593Smuzhiyun 			unsigned int changed_flags,
1222*4882a593Smuzhiyun 			unsigned int *new_flags,
1223*4882a593Smuzhiyun 			u64 multicast)
1224*4882a593Smuzhiyun {
1225*4882a593Smuzhiyun 	struct zd_mc_hash hash = {
1226*4882a593Smuzhiyun 		.low = multicast,
1227*4882a593Smuzhiyun 		.high = multicast >> 32,
1228*4882a593Smuzhiyun 	};
1229*4882a593Smuzhiyun 	struct zd_mac *mac = zd_hw_mac(hw);
1230*4882a593Smuzhiyun 	unsigned long flags;
1231*4882a593Smuzhiyun 	int r;
1232*4882a593Smuzhiyun 
1233*4882a593Smuzhiyun 	/* Only deal with supported flags */
1234*4882a593Smuzhiyun 	changed_flags &= SUPPORTED_FIF_FLAGS;
1235*4882a593Smuzhiyun 	*new_flags &= SUPPORTED_FIF_FLAGS;
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun 	/*
1238*4882a593Smuzhiyun 	 * If multicast parameter (as returned by zd_op_prepare_multicast)
1239*4882a593Smuzhiyun 	 * has changed, no bit in changed_flags is set. To handle this
1240*4882a593Smuzhiyun 	 * situation, we do not return if changed_flags is 0. If we do so,
1241*4882a593Smuzhiyun 	 * we will have some issue with IPv6 which uses multicast for link
1242*4882a593Smuzhiyun 	 * layer address resolution.
1243*4882a593Smuzhiyun 	 */
1244*4882a593Smuzhiyun 	if (*new_flags & FIF_ALLMULTI)
1245*4882a593Smuzhiyun 		zd_mc_add_all(&hash);
1246*4882a593Smuzhiyun 
1247*4882a593Smuzhiyun 	spin_lock_irqsave(&mac->lock, flags);
1248*4882a593Smuzhiyun 	mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
1249*4882a593Smuzhiyun 	mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
1250*4882a593Smuzhiyun 	mac->multicast_hash = hash;
1251*4882a593Smuzhiyun 	spin_unlock_irqrestore(&mac->lock, flags);
1252*4882a593Smuzhiyun 
1253*4882a593Smuzhiyun 	zd_chip_set_multicast_hash(&mac->chip, &hash);
1254*4882a593Smuzhiyun 
1255*4882a593Smuzhiyun 	if (changed_flags & FIF_CONTROL) {
1256*4882a593Smuzhiyun 		r = set_rx_filter(mac);
1257*4882a593Smuzhiyun 		if (r)
1258*4882a593Smuzhiyun 			dev_err(zd_mac_dev(mac), "set_rx_filter error %d\n", r);
1259*4882a593Smuzhiyun 	}
1260*4882a593Smuzhiyun 
1261*4882a593Smuzhiyun 	/* no handling required for FIF_OTHER_BSS as we don't currently
1262*4882a593Smuzhiyun 	 * do BSSID filtering */
1263*4882a593Smuzhiyun 	/* FIXME: in future it would be nice to enable the probe response
1264*4882a593Smuzhiyun 	 * filter (so that the driver doesn't see them) until
1265*4882a593Smuzhiyun 	 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
1266*4882a593Smuzhiyun 	 * have to schedule work to enable prbresp reception, which might
1267*4882a593Smuzhiyun 	 * happen too late. For now we'll just listen and forward them all the
1268*4882a593Smuzhiyun 	 * time. */
1269*4882a593Smuzhiyun }
1270*4882a593Smuzhiyun 
set_rts_cts(struct zd_mac * mac,unsigned int short_preamble)1271*4882a593Smuzhiyun static void set_rts_cts(struct zd_mac *mac, unsigned int short_preamble)
1272*4882a593Smuzhiyun {
1273*4882a593Smuzhiyun 	mutex_lock(&mac->chip.mutex);
1274*4882a593Smuzhiyun 	zd_chip_set_rts_cts_rate_locked(&mac->chip, short_preamble);
1275*4882a593Smuzhiyun 	mutex_unlock(&mac->chip.mutex);
1276*4882a593Smuzhiyun }
1277*4882a593Smuzhiyun 
zd_op_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * bss_conf,u32 changes)1278*4882a593Smuzhiyun static void zd_op_bss_info_changed(struct ieee80211_hw *hw,
1279*4882a593Smuzhiyun 				   struct ieee80211_vif *vif,
1280*4882a593Smuzhiyun 				   struct ieee80211_bss_conf *bss_conf,
1281*4882a593Smuzhiyun 				   u32 changes)
1282*4882a593Smuzhiyun {
1283*4882a593Smuzhiyun 	struct zd_mac *mac = zd_hw_mac(hw);
1284*4882a593Smuzhiyun 	int associated;
1285*4882a593Smuzhiyun 
1286*4882a593Smuzhiyun 	dev_dbg_f(zd_mac_dev(mac), "changes: %x\n", changes);
1287*4882a593Smuzhiyun 
1288*4882a593Smuzhiyun 	if (mac->type == NL80211_IFTYPE_MESH_POINT ||
1289*4882a593Smuzhiyun 	    mac->type == NL80211_IFTYPE_ADHOC ||
1290*4882a593Smuzhiyun 	    mac->type == NL80211_IFTYPE_AP) {
1291*4882a593Smuzhiyun 		associated = true;
1292*4882a593Smuzhiyun 		if (changes & BSS_CHANGED_BEACON) {
1293*4882a593Smuzhiyun 			struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
1294*4882a593Smuzhiyun 
1295*4882a593Smuzhiyun 			if (beacon) {
1296*4882a593Smuzhiyun 				zd_chip_disable_hwint(&mac->chip);
1297*4882a593Smuzhiyun 				zd_mac_config_beacon(hw, beacon, false);
1298*4882a593Smuzhiyun 				zd_chip_enable_hwint(&mac->chip);
1299*4882a593Smuzhiyun 			}
1300*4882a593Smuzhiyun 		}
1301*4882a593Smuzhiyun 
1302*4882a593Smuzhiyun 		if (changes & BSS_CHANGED_BEACON_ENABLED) {
1303*4882a593Smuzhiyun 			u16 interval = 0;
1304*4882a593Smuzhiyun 			u8 period = 0;
1305*4882a593Smuzhiyun 
1306*4882a593Smuzhiyun 			if (bss_conf->enable_beacon) {
1307*4882a593Smuzhiyun 				period = bss_conf->dtim_period;
1308*4882a593Smuzhiyun 				interval = bss_conf->beacon_int;
1309*4882a593Smuzhiyun 			}
1310*4882a593Smuzhiyun 
1311*4882a593Smuzhiyun 			spin_lock_irq(&mac->lock);
1312*4882a593Smuzhiyun 			mac->beacon.period = period;
1313*4882a593Smuzhiyun 			mac->beacon.interval = interval;
1314*4882a593Smuzhiyun 			mac->beacon.last_update = jiffies;
1315*4882a593Smuzhiyun 			spin_unlock_irq(&mac->lock);
1316*4882a593Smuzhiyun 
1317*4882a593Smuzhiyun 			zd_set_beacon_interval(&mac->chip, interval, period,
1318*4882a593Smuzhiyun 					       mac->type);
1319*4882a593Smuzhiyun 		}
1320*4882a593Smuzhiyun 	} else
1321*4882a593Smuzhiyun 		associated = is_valid_ether_addr(bss_conf->bssid);
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun 	spin_lock_irq(&mac->lock);
1324*4882a593Smuzhiyun 	mac->associated = associated;
1325*4882a593Smuzhiyun 	spin_unlock_irq(&mac->lock);
1326*4882a593Smuzhiyun 
1327*4882a593Smuzhiyun 	/* TODO: do hardware bssid filtering */
1328*4882a593Smuzhiyun 
1329*4882a593Smuzhiyun 	if (changes & BSS_CHANGED_ERP_PREAMBLE) {
1330*4882a593Smuzhiyun 		spin_lock_irq(&mac->lock);
1331*4882a593Smuzhiyun 		mac->short_preamble = bss_conf->use_short_preamble;
1332*4882a593Smuzhiyun 		spin_unlock_irq(&mac->lock);
1333*4882a593Smuzhiyun 
1334*4882a593Smuzhiyun 		set_rts_cts(mac, bss_conf->use_short_preamble);
1335*4882a593Smuzhiyun 	}
1336*4882a593Smuzhiyun }
1337*4882a593Smuzhiyun 
zd_op_get_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1338*4882a593Smuzhiyun static u64 zd_op_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1339*4882a593Smuzhiyun {
1340*4882a593Smuzhiyun 	struct zd_mac *mac = zd_hw_mac(hw);
1341*4882a593Smuzhiyun 	return zd_chip_get_tsf(&mac->chip);
1342*4882a593Smuzhiyun }
1343*4882a593Smuzhiyun 
1344*4882a593Smuzhiyun static const struct ieee80211_ops zd_ops = {
1345*4882a593Smuzhiyun 	.tx			= zd_op_tx,
1346*4882a593Smuzhiyun 	.start			= zd_op_start,
1347*4882a593Smuzhiyun 	.stop			= zd_op_stop,
1348*4882a593Smuzhiyun 	.add_interface		= zd_op_add_interface,
1349*4882a593Smuzhiyun 	.remove_interface	= zd_op_remove_interface,
1350*4882a593Smuzhiyun 	.config			= zd_op_config,
1351*4882a593Smuzhiyun 	.prepare_multicast	= zd_op_prepare_multicast,
1352*4882a593Smuzhiyun 	.configure_filter	= zd_op_configure_filter,
1353*4882a593Smuzhiyun 	.bss_info_changed	= zd_op_bss_info_changed,
1354*4882a593Smuzhiyun 	.get_tsf		= zd_op_get_tsf,
1355*4882a593Smuzhiyun };
1356*4882a593Smuzhiyun 
zd_mac_alloc_hw(struct usb_interface * intf)1357*4882a593Smuzhiyun struct ieee80211_hw *zd_mac_alloc_hw(struct usb_interface *intf)
1358*4882a593Smuzhiyun {
1359*4882a593Smuzhiyun 	struct zd_mac *mac;
1360*4882a593Smuzhiyun 	struct ieee80211_hw *hw;
1361*4882a593Smuzhiyun 
1362*4882a593Smuzhiyun 	hw = ieee80211_alloc_hw(sizeof(struct zd_mac), &zd_ops);
1363*4882a593Smuzhiyun 	if (!hw) {
1364*4882a593Smuzhiyun 		dev_dbg_f(&intf->dev, "out of memory\n");
1365*4882a593Smuzhiyun 		return NULL;
1366*4882a593Smuzhiyun 	}
1367*4882a593Smuzhiyun 
1368*4882a593Smuzhiyun 	mac = zd_hw_mac(hw);
1369*4882a593Smuzhiyun 
1370*4882a593Smuzhiyun 	memset(mac, 0, sizeof(*mac));
1371*4882a593Smuzhiyun 	spin_lock_init(&mac->lock);
1372*4882a593Smuzhiyun 	mac->hw = hw;
1373*4882a593Smuzhiyun 
1374*4882a593Smuzhiyun 	mac->type = NL80211_IFTYPE_UNSPECIFIED;
1375*4882a593Smuzhiyun 
1376*4882a593Smuzhiyun 	memcpy(mac->channels, zd_channels, sizeof(zd_channels));
1377*4882a593Smuzhiyun 	memcpy(mac->rates, zd_rates, sizeof(zd_rates));
1378*4882a593Smuzhiyun 	mac->band.n_bitrates = ARRAY_SIZE(zd_rates);
1379*4882a593Smuzhiyun 	mac->band.bitrates = mac->rates;
1380*4882a593Smuzhiyun 	mac->band.n_channels = ARRAY_SIZE(zd_channels);
1381*4882a593Smuzhiyun 	mac->band.channels = mac->channels;
1382*4882a593Smuzhiyun 
1383*4882a593Smuzhiyun 	hw->wiphy->bands[NL80211_BAND_2GHZ] = &mac->band;
1384*4882a593Smuzhiyun 
1385*4882a593Smuzhiyun 	ieee80211_hw_set(hw, MFP_CAPABLE);
1386*4882a593Smuzhiyun 	ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
1387*4882a593Smuzhiyun 	ieee80211_hw_set(hw, RX_INCLUDES_FCS);
1388*4882a593Smuzhiyun 	ieee80211_hw_set(hw, SIGNAL_UNSPEC);
1389*4882a593Smuzhiyun 
1390*4882a593Smuzhiyun 	hw->wiphy->interface_modes =
1391*4882a593Smuzhiyun 		BIT(NL80211_IFTYPE_MESH_POINT) |
1392*4882a593Smuzhiyun 		BIT(NL80211_IFTYPE_STATION) |
1393*4882a593Smuzhiyun 		BIT(NL80211_IFTYPE_ADHOC) |
1394*4882a593Smuzhiyun 		BIT(NL80211_IFTYPE_AP);
1395*4882a593Smuzhiyun 
1396*4882a593Smuzhiyun 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
1397*4882a593Smuzhiyun 
1398*4882a593Smuzhiyun 	hw->max_signal = 100;
1399*4882a593Smuzhiyun 	hw->queues = 1;
1400*4882a593Smuzhiyun 	hw->extra_tx_headroom = sizeof(struct zd_ctrlset);
1401*4882a593Smuzhiyun 
1402*4882a593Smuzhiyun 	/*
1403*4882a593Smuzhiyun 	 * Tell mac80211 that we support multi rate retries
1404*4882a593Smuzhiyun 	 */
1405*4882a593Smuzhiyun 	hw->max_rates = IEEE80211_TX_MAX_RATES;
1406*4882a593Smuzhiyun 	hw->max_rate_tries = 18;	/* 9 rates * 2 retries/rate */
1407*4882a593Smuzhiyun 
1408*4882a593Smuzhiyun 	skb_queue_head_init(&mac->ack_wait_queue);
1409*4882a593Smuzhiyun 	mac->ack_pending = 0;
1410*4882a593Smuzhiyun 
1411*4882a593Smuzhiyun 	zd_chip_init(&mac->chip, hw, intf);
1412*4882a593Smuzhiyun 	housekeeping_init(mac);
1413*4882a593Smuzhiyun 	beacon_init(mac);
1414*4882a593Smuzhiyun 	INIT_WORK(&mac->process_intr, zd_process_intr);
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun 	SET_IEEE80211_DEV(hw, &intf->dev);
1417*4882a593Smuzhiyun 	return hw;
1418*4882a593Smuzhiyun }
1419*4882a593Smuzhiyun 
1420*4882a593Smuzhiyun #define BEACON_WATCHDOG_DELAY round_jiffies_relative(HZ)
1421*4882a593Smuzhiyun 
beacon_watchdog_handler(struct work_struct * work)1422*4882a593Smuzhiyun static void beacon_watchdog_handler(struct work_struct *work)
1423*4882a593Smuzhiyun {
1424*4882a593Smuzhiyun 	struct zd_mac *mac =
1425*4882a593Smuzhiyun 		container_of(work, struct zd_mac, beacon.watchdog_work.work);
1426*4882a593Smuzhiyun 	struct sk_buff *beacon;
1427*4882a593Smuzhiyun 	unsigned long timeout;
1428*4882a593Smuzhiyun 	int interval, period;
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun 	if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1431*4882a593Smuzhiyun 		goto rearm;
1432*4882a593Smuzhiyun 	if (mac->type != NL80211_IFTYPE_AP || !mac->vif)
1433*4882a593Smuzhiyun 		goto rearm;
1434*4882a593Smuzhiyun 
1435*4882a593Smuzhiyun 	spin_lock_irq(&mac->lock);
1436*4882a593Smuzhiyun 	interval = mac->beacon.interval;
1437*4882a593Smuzhiyun 	period = mac->beacon.period;
1438*4882a593Smuzhiyun 	timeout = mac->beacon.last_update +
1439*4882a593Smuzhiyun 			msecs_to_jiffies(interval * 1024 / 1000) * 3;
1440*4882a593Smuzhiyun 	spin_unlock_irq(&mac->lock);
1441*4882a593Smuzhiyun 
1442*4882a593Smuzhiyun 	if (interval > 0 && time_is_before_jiffies(timeout)) {
1443*4882a593Smuzhiyun 		dev_dbg_f(zd_mac_dev(mac), "beacon interrupt stalled, "
1444*4882a593Smuzhiyun 					   "restarting. "
1445*4882a593Smuzhiyun 					   "(interval: %d, dtim: %d)\n",
1446*4882a593Smuzhiyun 					   interval, period);
1447*4882a593Smuzhiyun 
1448*4882a593Smuzhiyun 		zd_chip_disable_hwint(&mac->chip);
1449*4882a593Smuzhiyun 
1450*4882a593Smuzhiyun 		beacon = ieee80211_beacon_get(mac->hw, mac->vif);
1451*4882a593Smuzhiyun 		if (beacon) {
1452*4882a593Smuzhiyun 			zd_mac_free_cur_beacon(mac);
1453*4882a593Smuzhiyun 
1454*4882a593Smuzhiyun 			zd_mac_config_beacon(mac->hw, beacon, false);
1455*4882a593Smuzhiyun 		}
1456*4882a593Smuzhiyun 
1457*4882a593Smuzhiyun 		zd_set_beacon_interval(&mac->chip, interval, period, mac->type);
1458*4882a593Smuzhiyun 
1459*4882a593Smuzhiyun 		zd_chip_enable_hwint(&mac->chip);
1460*4882a593Smuzhiyun 
1461*4882a593Smuzhiyun 		spin_lock_irq(&mac->lock);
1462*4882a593Smuzhiyun 		mac->beacon.last_update = jiffies;
1463*4882a593Smuzhiyun 		spin_unlock_irq(&mac->lock);
1464*4882a593Smuzhiyun 	}
1465*4882a593Smuzhiyun 
1466*4882a593Smuzhiyun rearm:
1467*4882a593Smuzhiyun 	queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
1468*4882a593Smuzhiyun 			   BEACON_WATCHDOG_DELAY);
1469*4882a593Smuzhiyun }
1470*4882a593Smuzhiyun 
beacon_init(struct zd_mac * mac)1471*4882a593Smuzhiyun static void beacon_init(struct zd_mac *mac)
1472*4882a593Smuzhiyun {
1473*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&mac->beacon.watchdog_work, beacon_watchdog_handler);
1474*4882a593Smuzhiyun }
1475*4882a593Smuzhiyun 
beacon_enable(struct zd_mac * mac)1476*4882a593Smuzhiyun static void beacon_enable(struct zd_mac *mac)
1477*4882a593Smuzhiyun {
1478*4882a593Smuzhiyun 	dev_dbg_f(zd_mac_dev(mac), "\n");
1479*4882a593Smuzhiyun 
1480*4882a593Smuzhiyun 	mac->beacon.last_update = jiffies;
1481*4882a593Smuzhiyun 	queue_delayed_work(zd_workqueue, &mac->beacon.watchdog_work,
1482*4882a593Smuzhiyun 			   BEACON_WATCHDOG_DELAY);
1483*4882a593Smuzhiyun }
1484*4882a593Smuzhiyun 
beacon_disable(struct zd_mac * mac)1485*4882a593Smuzhiyun static void beacon_disable(struct zd_mac *mac)
1486*4882a593Smuzhiyun {
1487*4882a593Smuzhiyun 	dev_dbg_f(zd_mac_dev(mac), "\n");
1488*4882a593Smuzhiyun 	cancel_delayed_work_sync(&mac->beacon.watchdog_work);
1489*4882a593Smuzhiyun 
1490*4882a593Smuzhiyun 	zd_mac_free_cur_beacon(mac);
1491*4882a593Smuzhiyun }
1492*4882a593Smuzhiyun 
1493*4882a593Smuzhiyun #define LINK_LED_WORK_DELAY HZ
1494*4882a593Smuzhiyun 
link_led_handler(struct work_struct * work)1495*4882a593Smuzhiyun static void link_led_handler(struct work_struct *work)
1496*4882a593Smuzhiyun {
1497*4882a593Smuzhiyun 	struct zd_mac *mac =
1498*4882a593Smuzhiyun 		container_of(work, struct zd_mac, housekeeping.link_led_work.work);
1499*4882a593Smuzhiyun 	struct zd_chip *chip = &mac->chip;
1500*4882a593Smuzhiyun 	int is_associated;
1501*4882a593Smuzhiyun 	int r;
1502*4882a593Smuzhiyun 
1503*4882a593Smuzhiyun 	if (!test_bit(ZD_DEVICE_RUNNING, &mac->flags))
1504*4882a593Smuzhiyun 		goto requeue;
1505*4882a593Smuzhiyun 
1506*4882a593Smuzhiyun 	spin_lock_irq(&mac->lock);
1507*4882a593Smuzhiyun 	is_associated = mac->associated;
1508*4882a593Smuzhiyun 	spin_unlock_irq(&mac->lock);
1509*4882a593Smuzhiyun 
1510*4882a593Smuzhiyun 	r = zd_chip_control_leds(chip,
1511*4882a593Smuzhiyun 		                 is_associated ? ZD_LED_ASSOCIATED : ZD_LED_SCANNING);
1512*4882a593Smuzhiyun 	if (r)
1513*4882a593Smuzhiyun 		dev_dbg_f(zd_mac_dev(mac), "zd_chip_control_leds error %d\n", r);
1514*4882a593Smuzhiyun 
1515*4882a593Smuzhiyun requeue:
1516*4882a593Smuzhiyun 	queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1517*4882a593Smuzhiyun 		           LINK_LED_WORK_DELAY);
1518*4882a593Smuzhiyun }
1519*4882a593Smuzhiyun 
housekeeping_init(struct zd_mac * mac)1520*4882a593Smuzhiyun static void housekeeping_init(struct zd_mac *mac)
1521*4882a593Smuzhiyun {
1522*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&mac->housekeeping.link_led_work, link_led_handler);
1523*4882a593Smuzhiyun }
1524*4882a593Smuzhiyun 
housekeeping_enable(struct zd_mac * mac)1525*4882a593Smuzhiyun static void housekeeping_enable(struct zd_mac *mac)
1526*4882a593Smuzhiyun {
1527*4882a593Smuzhiyun 	dev_dbg_f(zd_mac_dev(mac), "\n");
1528*4882a593Smuzhiyun 	queue_delayed_work(zd_workqueue, &mac->housekeeping.link_led_work,
1529*4882a593Smuzhiyun 			   0);
1530*4882a593Smuzhiyun }
1531*4882a593Smuzhiyun 
housekeeping_disable(struct zd_mac * mac)1532*4882a593Smuzhiyun static void housekeeping_disable(struct zd_mac *mac)
1533*4882a593Smuzhiyun {
1534*4882a593Smuzhiyun 	dev_dbg_f(zd_mac_dev(mac), "\n");
1535*4882a593Smuzhiyun 	cancel_delayed_work_sync(&mac->housekeeping.link_led_work);
1536*4882a593Smuzhiyun 	zd_chip_control_leds(&mac->chip, ZD_LED_OFF);
1537*4882a593Smuzhiyun }
1538