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