1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2021 pureLiFi
4  */
5 
6 #include <linux/netdevice.h>
7 #include <linux/etherdevice.h>
8 #include <linux/slab.h>
9 #include <linux/usb.h>
10 #include <linux/gpio.h>
11 #include <linux/jiffies.h>
12 #include <net/ieee80211_radiotap.h>
13 
14 #include "chip.h"
15 #include "mac.h"
16 #include "usb.h"
17 
18 static const struct ieee80211_rate plfxlc_rates[] = {
19 	{ .bitrate = 10,
20 		.hw_value = PURELIFI_CCK_RATE_1M,
21 		.flags = 0 },
22 	{ .bitrate = 20,
23 		.hw_value = PURELIFI_CCK_RATE_2M,
24 		.hw_value_short = PURELIFI_CCK_RATE_2M
25 			| PURELIFI_CCK_PREA_SHORT,
26 		.flags = IEEE80211_RATE_SHORT_PREAMBLE },
27 	{ .bitrate = 55,
28 		.hw_value = PURELIFI_CCK_RATE_5_5M,
29 		.hw_value_short = PURELIFI_CCK_RATE_5_5M
30 			| PURELIFI_CCK_PREA_SHORT,
31 		.flags = IEEE80211_RATE_SHORT_PREAMBLE },
32 	{ .bitrate = 110,
33 		.hw_value = PURELIFI_CCK_RATE_11M,
34 		.hw_value_short = PURELIFI_CCK_RATE_11M
35 			| PURELIFI_CCK_PREA_SHORT,
36 		.flags = IEEE80211_RATE_SHORT_PREAMBLE },
37 	{ .bitrate = 60,
38 		.hw_value = PURELIFI_OFDM_RATE_6M,
39 		.flags = 0 },
40 	{ .bitrate = 90,
41 		.hw_value = PURELIFI_OFDM_RATE_9M,
42 		.flags = 0 },
43 	{ .bitrate = 120,
44 		.hw_value = PURELIFI_OFDM_RATE_12M,
45 		.flags = 0 },
46 	{ .bitrate = 180,
47 		.hw_value = PURELIFI_OFDM_RATE_18M,
48 		.flags = 0 },
49 	{ .bitrate = 240,
50 		.hw_value = PURELIFI_OFDM_RATE_24M,
51 		.flags = 0 },
52 	{ .bitrate = 360,
53 		.hw_value = PURELIFI_OFDM_RATE_36M,
54 		.flags = 0 },
55 	{ .bitrate = 480,
56 		.hw_value = PURELIFI_OFDM_RATE_48M,
57 		.flags = 0 },
58 	{ .bitrate = 540,
59 		.hw_value = PURELIFI_OFDM_RATE_54M,
60 		.flags = 0 }
61 };
62 
63 static const struct ieee80211_channel plfxlc_channels[] = {
64 	{ .center_freq = 2412, .hw_value = 1 },
65 	{ .center_freq = 2417, .hw_value = 2 },
66 	{ .center_freq = 2422, .hw_value = 3 },
67 	{ .center_freq = 2427, .hw_value = 4 },
68 	{ .center_freq = 2432, .hw_value = 5 },
69 	{ .center_freq = 2437, .hw_value = 6 },
70 	{ .center_freq = 2442, .hw_value = 7 },
71 	{ .center_freq = 2447, .hw_value = 8 },
72 	{ .center_freq = 2452, .hw_value = 9 },
73 	{ .center_freq = 2457, .hw_value = 10 },
74 	{ .center_freq = 2462, .hw_value = 11 },
75 	{ .center_freq = 2467, .hw_value = 12 },
76 	{ .center_freq = 2472, .hw_value = 13 },
77 	{ .center_freq = 2484, .hw_value = 14 },
78 };
79 
80 int plfxlc_mac_preinit_hw(struct ieee80211_hw *hw, const u8 *hw_address)
81 {
82 	SET_IEEE80211_PERM_ADDR(hw, hw_address);
83 	return 0;
84 }
85 
86 int plfxlc_mac_init_hw(struct ieee80211_hw *hw)
87 {
88 	struct plfxlc_mac *mac = plfxlc_hw_mac(hw);
89 	struct plfxlc_chip *chip = &mac->chip;
90 	int r;
91 
92 	r = plfxlc_chip_init_hw(chip);
93 	if (r) {
94 		dev_warn(plfxlc_mac_dev(mac), "init hw failed (%d)\n", r);
95 		return r;
96 	}
97 
98 	dev_dbg(plfxlc_mac_dev(mac), "irq_disabled (%d)\n", irqs_disabled());
99 	regulatory_hint(hw->wiphy, "00");
100 	return r;
101 }
102 
103 void plfxlc_mac_release(struct plfxlc_mac *mac)
104 {
105 	plfxlc_chip_release(&mac->chip);
106 	lockdep_assert_held(&mac->lock);
107 }
108 
109 int plfxlc_op_start(struct ieee80211_hw *hw)
110 {
111 	plfxlc_hw_mac(hw)->chip.usb.initialized = 1;
112 	return 0;
113 }
114 
115 void plfxlc_op_stop(struct ieee80211_hw *hw)
116 {
117 	struct plfxlc_mac *mac = plfxlc_hw_mac(hw);
118 
119 	clear_bit(PURELIFI_DEVICE_RUNNING, &mac->flags);
120 }
121 
122 int plfxlc_restore_settings(struct plfxlc_mac *mac)
123 {
124 	int beacon_interval, beacon_period;
125 	struct sk_buff *beacon;
126 
127 	spin_lock_irq(&mac->lock);
128 	beacon_interval = mac->beacon.interval;
129 	beacon_period = mac->beacon.period;
130 	spin_unlock_irq(&mac->lock);
131 
132 	if (mac->type != NL80211_IFTYPE_ADHOC)
133 		return 0;
134 
135 	if (mac->vif) {
136 		beacon = ieee80211_beacon_get(mac->hw, mac->vif, 0);
137 		if (beacon) {
138 			/*beacon is hardcoded in firmware */
139 			kfree_skb(beacon);
140 			/* Returned skb is used only once and lowlevel
141 			 * driver is responsible for freeing it.
142 			 */
143 		}
144 	}
145 
146 	plfxlc_set_beacon_interval(&mac->chip, beacon_interval,
147 				   beacon_period, mac->type);
148 
149 	spin_lock_irq(&mac->lock);
150 	mac->beacon.last_update = jiffies;
151 	spin_unlock_irq(&mac->lock);
152 
153 	return 0;
154 }
155 
156 static void plfxlc_mac_tx_status(struct ieee80211_hw *hw,
157 				 struct sk_buff *skb,
158 				 int ackssi,
159 				 struct tx_status *tx_status)
160 {
161 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
162 	int success = 1;
163 
164 	ieee80211_tx_info_clear_status(info);
165 	if (tx_status)
166 		success = !tx_status->failure;
167 
168 	if (success)
169 		info->flags |= IEEE80211_TX_STAT_ACK;
170 	else
171 		info->flags &= ~IEEE80211_TX_STAT_ACK;
172 
173 	info->status.ack_signal = 50;
174 	ieee80211_tx_status_irqsafe(hw, skb);
175 }
176 
177 void plfxlc_mac_tx_to_dev(struct sk_buff *skb, int error)
178 {
179 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
180 	struct ieee80211_hw *hw = info->rate_driver_data[0];
181 	struct plfxlc_mac *mac = plfxlc_hw_mac(hw);
182 	struct sk_buff_head *q = NULL;
183 
184 	ieee80211_tx_info_clear_status(info);
185 	skb_pull(skb, sizeof(struct plfxlc_ctrlset));
186 
187 	if (unlikely(error ||
188 		     (info->flags & IEEE80211_TX_CTL_NO_ACK))) {
189 		ieee80211_tx_status_irqsafe(hw, skb);
190 		return;
191 	}
192 
193 	q = &mac->ack_wait_queue;
194 
195 	skb_queue_tail(q, skb);
196 	while (skb_queue_len(q)/* > PURELIFI_MAC_MAX_ACK_WAITERS*/) {
197 		plfxlc_mac_tx_status(hw, skb_dequeue(q),
198 				     mac->ack_pending ?
199 				     mac->ack_signal : 0,
200 				     NULL);
201 		mac->ack_pending = 0;
202 	}
203 }
204 
205 static int plfxlc_fill_ctrlset(struct plfxlc_mac *mac, struct sk_buff *skb)
206 {
207 	unsigned int frag_len = skb->len;
208 	struct plfxlc_ctrlset *cs;
209 	u32 temp_payload_len = 0;
210 	unsigned int tmp;
211 	u32 temp_len = 0;
212 
213 	if (skb_headroom(skb) < sizeof(struct plfxlc_ctrlset)) {
214 		dev_dbg(plfxlc_mac_dev(mac), "Not enough hroom(1)\n");
215 		return 1;
216 	}
217 
218 	cs = (void *)skb_push(skb, sizeof(struct plfxlc_ctrlset));
219 	temp_payload_len = frag_len;
220 	temp_len = temp_payload_len +
221 		  sizeof(struct plfxlc_ctrlset) -
222 		  sizeof(cs->id) - sizeof(cs->len);
223 
224 	/* Data packet lengths must be multiple of four bytes and must
225 	 * not be a multiple of 512 bytes. First, it is attempted to
226 	 * append the data packet in the tailroom of the skb. In rare
227 	 * occasions, the tailroom is too small. In this case, the
228 	 * content of the packet is shifted into the headroom of the skb
229 	 * by memcpy. Headroom is allocated at startup (below in this
230 	 * file). Therefore, there will be always enough headroom. The
231 	 * call skb_headroom is an additional safety which might be
232 	 * dropped.
233 	 */
234 	/* check if 32 bit aligned and align data */
235 	tmp = skb->len & 3;
236 	if (tmp) {
237 		if (skb_tailroom(skb) < (3 - tmp)) {
238 			if (skb_headroom(skb) >= 4 - tmp) {
239 				u8 len;
240 				u8 *src_pt;
241 				u8 *dest_pt;
242 
243 				len = skb->len;
244 				src_pt = skb->data;
245 				dest_pt = skb_push(skb, 4 - tmp);
246 				memmove(dest_pt, src_pt, len);
247 			} else {
248 				return -ENOBUFS;
249 			}
250 		} else {
251 			skb_put(skb, 4 - tmp);
252 		}
253 		temp_len += 4 - tmp;
254 	}
255 
256 	/* check if not multiple of 512 and align data */
257 	tmp = skb->len & 0x1ff;
258 	if (!tmp) {
259 		if (skb_tailroom(skb) < 4) {
260 			if (skb_headroom(skb) >= 4) {
261 				u8 len = skb->len;
262 				u8 *src_pt = skb->data;
263 				u8 *dest_pt = skb_push(skb, 4);
264 
265 				memmove(dest_pt, src_pt, len);
266 			} else {
267 				/* should never happen because
268 				 * sufficient headroom was reserved
269 				 */
270 				return -ENOBUFS;
271 			}
272 		} else {
273 			skb_put(skb, 4);
274 		}
275 		temp_len += 4;
276 	}
277 
278 	cs->id = cpu_to_be32(USB_REQ_DATA_TX);
279 	cs->len = cpu_to_be32(temp_len);
280 	cs->payload_len_nw = cpu_to_be32(temp_payload_len);
281 
282 	return 0;
283 }
284 
285 static void plfxlc_op_tx(struct ieee80211_hw *hw,
286 			 struct ieee80211_tx_control *control,
287 			 struct sk_buff *skb)
288 {
289 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
290 	struct plfxlc_header *plhdr = (void *)skb->data;
291 	struct plfxlc_mac *mac = plfxlc_hw_mac(hw);
292 	struct plfxlc_usb *usb = &mac->chip.usb;
293 	unsigned long flags;
294 	int r;
295 
296 	r = plfxlc_fill_ctrlset(mac, skb);
297 	if (r)
298 		goto fail;
299 
300 	info->rate_driver_data[0] = hw;
301 
302 	if (plhdr->frametype  == IEEE80211_FTYPE_DATA) {
303 		u8 *dst_mac = plhdr->dmac;
304 		u8 sidx;
305 		bool found = false;
306 		struct plfxlc_usb_tx *tx = &usb->tx;
307 
308 		for (sidx = 0; sidx < MAX_STA_NUM; sidx++) {
309 			if (!(tx->station[sidx].flag & STATION_CONNECTED_FLAG))
310 				continue;
311 			if (memcmp(tx->station[sidx].mac, dst_mac, ETH_ALEN))
312 				continue;
313 			found = true;
314 			break;
315 		}
316 
317 		/* Default to broadcast address for unknown MACs */
318 		if (!found)
319 			sidx = STA_BROADCAST_INDEX;
320 
321 		/* Stop OS from sending packets, if the queue is half full */
322 		if (skb_queue_len(&tx->station[sidx].data_list) > 60)
323 			ieee80211_stop_queues(plfxlc_usb_to_hw(usb));
324 
325 		/* Schedule packet for transmission if queue is not full */
326 		if (skb_queue_len(&tx->station[sidx].data_list) > 256)
327 			goto fail;
328 		skb_queue_tail(&tx->station[sidx].data_list, skb);
329 		plfxlc_send_packet_from_data_queue(usb);
330 
331 	} else {
332 		spin_lock_irqsave(&usb->tx.lock, flags);
333 		r = plfxlc_usb_wreq_async(&mac->chip.usb, skb->data, skb->len,
334 					  USB_REQ_DATA_TX, plfxlc_tx_urb_complete, skb);
335 		spin_unlock_irqrestore(&usb->tx.lock, flags);
336 		if (r)
337 			goto fail;
338 	}
339 	return;
340 
341 fail:
342 	dev_kfree_skb(skb);
343 }
344 
345 static int plfxlc_filter_ack(struct ieee80211_hw *hw, struct ieee80211_hdr *rx_hdr,
346 			     struct ieee80211_rx_status *stats)
347 {
348 	struct plfxlc_mac *mac = plfxlc_hw_mac(hw);
349 	struct sk_buff_head *q;
350 	int i, position = 0;
351 	unsigned long flags;
352 	struct sk_buff *skb;
353 	bool found = false;
354 
355 	if (!ieee80211_is_ack(rx_hdr->frame_control))
356 		return 0;
357 
358 	dev_dbg(plfxlc_mac_dev(mac), "ACK Received\n");
359 
360 	/* code based on zy driver, this logic may need fix */
361 	q = &mac->ack_wait_queue;
362 	spin_lock_irqsave(&q->lock, flags);
363 
364 	skb_queue_walk(q, skb) {
365 		struct ieee80211_hdr *tx_hdr;
366 
367 		position++;
368 
369 		if (mac->ack_pending && skb_queue_is_first(q, skb))
370 			continue;
371 		if (mac->ack_pending == 0)
372 			break;
373 
374 		tx_hdr = (struct ieee80211_hdr *)skb->data;
375 		if (likely(ether_addr_equal(tx_hdr->addr2, rx_hdr->addr1))) {
376 			found = 1;
377 			break;
378 		}
379 	}
380 
381 	if (found) {
382 		for (i = 1; i < position; i++)
383 			skb = __skb_dequeue(q);
384 		if (i == position) {
385 			plfxlc_mac_tx_status(hw, skb,
386 					     mac->ack_pending ?
387 					     mac->ack_signal : 0,
388 					     NULL);
389 			mac->ack_pending = 0;
390 		}
391 
392 		mac->ack_pending = skb_queue_len(q) ? 1 : 0;
393 		mac->ack_signal = stats->signal;
394 	}
395 
396 	spin_unlock_irqrestore(&q->lock, flags);
397 	return 1;
398 }
399 
400 int plfxlc_mac_rx(struct ieee80211_hw *hw, const u8 *buffer,
401 		  unsigned int length)
402 {
403 	struct plfxlc_mac *mac = plfxlc_hw_mac(hw);
404 	struct ieee80211_rx_status stats;
405 	const struct rx_status *status;
406 	unsigned int payload_length;
407 	struct plfxlc_usb_tx *tx;
408 	struct sk_buff *skb;
409 	int need_padding;
410 	__le16 fc;
411 	int sidx;
412 
413 	/* Packet blockade during disabled interface. */
414 	if (!mac->vif)
415 		return 0;
416 
417 	status = (struct rx_status *)buffer;
418 
419 	memset(&stats, 0, sizeof(stats));
420 
421 	stats.flag     = 0;
422 	stats.freq     = 2412;
423 	stats.band     = NL80211_BAND_LC;
424 	mac->rssi      = -15 * be16_to_cpu(status->rssi) / 10;
425 
426 	stats.signal   = mac->rssi;
427 
428 	if (status->rate_idx > 7)
429 		stats.rate_idx = 0;
430 	else
431 		stats.rate_idx = status->rate_idx;
432 
433 	mac->crc_errors = be64_to_cpu(status->crc_error_count);
434 
435 	/* TODO bad frame check for CRC error*/
436 	if (plfxlc_filter_ack(hw, (struct ieee80211_hdr *)buffer, &stats) &&
437 	    !mac->pass_ctrl)
438 		return 0;
439 
440 	buffer += sizeof(struct rx_status);
441 	payload_length = get_unaligned_be32(buffer);
442 
443 	if (payload_length > 1560) {
444 		dev_err(plfxlc_mac_dev(mac), " > MTU %u\n", payload_length);
445 		return 0;
446 	}
447 	buffer += sizeof(u32);
448 
449 	fc = get_unaligned((__le16 *)buffer);
450 	need_padding = ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc);
451 
452 	tx = &mac->chip.usb.tx;
453 
454 	for (sidx = 0; sidx < MAX_STA_NUM - 1; sidx++) {
455 		if (memcmp(&buffer[10], tx->station[sidx].mac, ETH_ALEN))
456 			continue;
457 		if (tx->station[sidx].flag & STATION_CONNECTED_FLAG) {
458 			tx->station[sidx].flag |= STATION_HEARTBEAT_FLAG;
459 			break;
460 		}
461 	}
462 
463 	if (sidx == MAX_STA_NUM - 1) {
464 		for (sidx = 0; sidx < MAX_STA_NUM - 1; sidx++) {
465 			if (tx->station[sidx].flag & STATION_CONNECTED_FLAG)
466 				continue;
467 			memcpy(tx->station[sidx].mac, &buffer[10], ETH_ALEN);
468 			tx->station[sidx].flag |= STATION_CONNECTED_FLAG;
469 			tx->station[sidx].flag |= STATION_HEARTBEAT_FLAG;
470 			break;
471 		}
472 	}
473 
474 	switch (buffer[0]) {
475 	case IEEE80211_STYPE_PROBE_REQ:
476 		dev_dbg(plfxlc_mac_dev(mac), "Probe request\n");
477 		break;
478 	case IEEE80211_STYPE_ASSOC_REQ:
479 		dev_dbg(plfxlc_mac_dev(mac), "Association request\n");
480 		break;
481 	case IEEE80211_STYPE_AUTH:
482 		dev_dbg(plfxlc_mac_dev(mac), "Authentication req\n");
483 		break;
484 	case IEEE80211_FTYPE_DATA:
485 		dev_dbg(plfxlc_mac_dev(mac), "802.11 data frame\n");
486 		break;
487 	}
488 
489 	skb = dev_alloc_skb(payload_length + (need_padding ? 2 : 0));
490 	if (!skb)
491 		return -ENOMEM;
492 
493 	if (need_padding)
494 		/* Make sure that the payload data is 4 byte aligned. */
495 		skb_reserve(skb, 2);
496 
497 	skb_put_data(skb, buffer, payload_length);
498 	memcpy(IEEE80211_SKB_RXCB(skb), &stats, sizeof(stats));
499 	ieee80211_rx_irqsafe(hw, skb);
500 	return 0;
501 }
502 
503 static int plfxlc_op_add_interface(struct ieee80211_hw *hw,
504 				   struct ieee80211_vif *vif)
505 {
506 	struct plfxlc_mac *mac = plfxlc_hw_mac(hw);
507 	static const char * const iftype80211[] = {
508 		[NL80211_IFTYPE_STATION]	= "Station",
509 		[NL80211_IFTYPE_ADHOC]		= "Adhoc"
510 	};
511 
512 	if (mac->type != NL80211_IFTYPE_UNSPECIFIED)
513 		return -EOPNOTSUPP;
514 
515 	if (vif->type == NL80211_IFTYPE_ADHOC ||
516 	    vif->type == NL80211_IFTYPE_STATION) {
517 		dev_dbg(plfxlc_mac_dev(mac), "%s %s\n", __func__,
518 			iftype80211[vif->type]);
519 		mac->type = vif->type;
520 		mac->vif = vif;
521 		return 0;
522 	}
523 	dev_dbg(plfxlc_mac_dev(mac), "unsupported iftype\n");
524 	return -EOPNOTSUPP;
525 }
526 
527 static void plfxlc_op_remove_interface(struct ieee80211_hw *hw,
528 				       struct ieee80211_vif *vif)
529 {
530 	struct plfxlc_mac *mac = plfxlc_hw_mac(hw);
531 
532 	mac->type = NL80211_IFTYPE_UNSPECIFIED;
533 	mac->vif = NULL;
534 }
535 
536 static int plfxlc_op_config(struct ieee80211_hw *hw, u32 changed)
537 {
538 	return 0;
539 }
540 
541 #define SUPPORTED_FIF_FLAGS \
542 	(FIF_ALLMULTI | FIF_FCSFAIL | FIF_CONTROL | \
543 	 FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)
544 static void plfxlc_op_configure_filter(struct ieee80211_hw *hw,
545 				       unsigned int changed_flags,
546 				       unsigned int *new_flags,
547 				       u64 multicast)
548 {
549 	struct plfxlc_mc_hash hash = {
550 		.low = multicast,
551 		.high = multicast >> 32,
552 	};
553 	struct plfxlc_mac *mac = plfxlc_hw_mac(hw);
554 	unsigned long flags;
555 
556 	/* Only deal with supported flags */
557 	*new_flags &= SUPPORTED_FIF_FLAGS;
558 
559 	/* If multicast parameter
560 	 * (as returned by plfxlc_op_prepare_multicast)
561 	 * has changed, no bit in changed_flags is set. To handle this
562 	 * situation, we do not return if changed_flags is 0. If we do so,
563 	 * we will have some issue with IPv6 which uses multicast for link
564 	 * layer address resolution.
565 	 */
566 	if (*new_flags & (FIF_ALLMULTI))
567 		plfxlc_mc_add_all(&hash);
568 
569 	spin_lock_irqsave(&mac->lock, flags);
570 	mac->pass_failed_fcs = !!(*new_flags & FIF_FCSFAIL);
571 	mac->pass_ctrl = !!(*new_flags & FIF_CONTROL);
572 	mac->multicast_hash = hash;
573 	spin_unlock_irqrestore(&mac->lock, flags);
574 
575 	/* no handling required for FIF_OTHER_BSS as we don't currently
576 	 * do BSSID filtering
577 	 */
578 	/* FIXME: in future it would be nice to enable the probe response
579 	 * filter (so that the driver doesn't see them) until
580 	 * FIF_BCN_PRBRESP_PROMISC is set. however due to atomicity here, we'd
581 	 * have to schedule work to enable prbresp reception, which might
582 	 * happen too late. For now we'll just listen and forward them all the
583 	 * time.
584 	 */
585 }
586 
587 static void plfxlc_op_bss_info_changed(struct ieee80211_hw *hw,
588 				       struct ieee80211_vif *vif,
589 				       struct ieee80211_bss_conf *bss_conf,
590 				       u64 changes)
591 {
592 	struct plfxlc_mac *mac = plfxlc_hw_mac(hw);
593 	int associated;
594 
595 	dev_dbg(plfxlc_mac_dev(mac), "changes: %llx\n", changes);
596 
597 	if (mac->type != NL80211_IFTYPE_ADHOC) { /* for STATION */
598 		associated = is_valid_ether_addr(bss_conf->bssid);
599 		goto exit_all;
600 	}
601 	/* for ADHOC */
602 	associated = true;
603 	if (changes & BSS_CHANGED_BEACON) {
604 		struct sk_buff *beacon = ieee80211_beacon_get(hw, vif, 0);
605 
606 		if (beacon) {
607 			/*beacon is hardcoded in firmware */
608 			kfree_skb(beacon);
609 			/*Returned skb is used only once and
610 			 * low-level driver is
611 			 * responsible for freeing it.
612 			 */
613 		}
614 	}
615 
616 	if (changes & BSS_CHANGED_BEACON_ENABLED) {
617 		u16 interval = 0;
618 		u8 period = 0;
619 
620 		if (bss_conf->enable_beacon) {
621 			period = bss_conf->dtim_period;
622 			interval = bss_conf->beacon_int;
623 		}
624 
625 		spin_lock_irq(&mac->lock);
626 		mac->beacon.period = period;
627 		mac->beacon.interval = interval;
628 		mac->beacon.last_update = jiffies;
629 		spin_unlock_irq(&mac->lock);
630 
631 		plfxlc_set_beacon_interval(&mac->chip, interval,
632 					   period, mac->type);
633 	}
634 exit_all:
635 	spin_lock_irq(&mac->lock);
636 	mac->associated = associated;
637 	spin_unlock_irq(&mac->lock);
638 }
639 
640 static int plfxlc_get_stats(struct ieee80211_hw *hw,
641 			    struct ieee80211_low_level_stats *stats)
642 {
643 	stats->dot11ACKFailureCount = 0;
644 	stats->dot11RTSFailureCount = 0;
645 	stats->dot11FCSErrorCount   = 0;
646 	stats->dot11RTSSuccessCount = 0;
647 	return 0;
648 }
649 
650 static const char et_strings[][ETH_GSTRING_LEN] = {
651 	"phy_rssi",
652 	"phy_rx_crc_err"
653 };
654 
655 static int plfxlc_get_et_sset_count(struct ieee80211_hw *hw,
656 				    struct ieee80211_vif *vif, int sset)
657 {
658 	if (sset == ETH_SS_STATS)
659 		return ARRAY_SIZE(et_strings);
660 
661 	return 0;
662 }
663 
664 static void plfxlc_get_et_strings(struct ieee80211_hw *hw,
665 				  struct ieee80211_vif *vif,
666 				  u32 sset, u8 *data)
667 {
668 	if (sset == ETH_SS_STATS)
669 		memcpy(data, et_strings, sizeof(et_strings));
670 }
671 
672 static void plfxlc_get_et_stats(struct ieee80211_hw *hw,
673 				struct ieee80211_vif *vif,
674 				struct ethtool_stats *stats, u64 *data)
675 {
676 	struct plfxlc_mac *mac = plfxlc_hw_mac(hw);
677 
678 	data[0] = mac->rssi;
679 	data[1] = mac->crc_errors;
680 }
681 
682 static int plfxlc_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
683 {
684 	return 0;
685 }
686 
687 static const struct ieee80211_ops plfxlc_ops = {
688 	.tx = plfxlc_op_tx,
689 	.wake_tx_queue = ieee80211_handle_wake_tx_queue,
690 	.start = plfxlc_op_start,
691 	.stop = plfxlc_op_stop,
692 	.add_interface = plfxlc_op_add_interface,
693 	.remove_interface = plfxlc_op_remove_interface,
694 	.set_rts_threshold = plfxlc_set_rts_threshold,
695 	.config = plfxlc_op_config,
696 	.configure_filter = plfxlc_op_configure_filter,
697 	.bss_info_changed = plfxlc_op_bss_info_changed,
698 	.get_stats = plfxlc_get_stats,
699 	.get_et_sset_count = plfxlc_get_et_sset_count,
700 	.get_et_stats = plfxlc_get_et_stats,
701 	.get_et_strings = plfxlc_get_et_strings,
702 };
703 
704 struct ieee80211_hw *plfxlc_mac_alloc_hw(struct usb_interface *intf)
705 {
706 	struct ieee80211_hw *hw;
707 	struct plfxlc_mac *mac;
708 
709 	hw = ieee80211_alloc_hw(sizeof(struct plfxlc_mac), &plfxlc_ops);
710 	if (!hw) {
711 		dev_dbg(&intf->dev, "out of memory\n");
712 		return NULL;
713 	}
714 	set_wiphy_dev(hw->wiphy, &intf->dev);
715 
716 	mac = plfxlc_hw_mac(hw);
717 	memset(mac, 0, sizeof(*mac));
718 	spin_lock_init(&mac->lock);
719 	mac->hw = hw;
720 
721 	mac->type = NL80211_IFTYPE_UNSPECIFIED;
722 
723 	memcpy(mac->channels, plfxlc_channels, sizeof(plfxlc_channels));
724 	memcpy(mac->rates, plfxlc_rates, sizeof(plfxlc_rates));
725 	mac->band.n_bitrates = ARRAY_SIZE(plfxlc_rates);
726 	mac->band.bitrates = mac->rates;
727 	mac->band.n_channels = ARRAY_SIZE(plfxlc_channels);
728 	mac->band.channels = mac->channels;
729 	hw->wiphy->bands[NL80211_BAND_LC] = &mac->band;
730 	hw->conf.chandef.width = NL80211_CHAN_WIDTH_20;
731 
732 	ieee80211_hw_set(hw, RX_INCLUDES_FCS);
733 	ieee80211_hw_set(hw, SIGNAL_DBM);
734 	ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
735 	ieee80211_hw_set(hw, MFP_CAPABLE);
736 
737 	hw->wiphy->interface_modes =
738 		BIT(NL80211_IFTYPE_STATION) |
739 		BIT(NL80211_IFTYPE_ADHOC);
740 	hw->max_signal = 100;
741 	hw->queues = 1;
742 	/* 4 for 32 bit alignment if no tailroom */
743 	hw->extra_tx_headroom = sizeof(struct plfxlc_ctrlset) + 4;
744 	/* Tell mac80211 that we support multi rate retries */
745 	hw->max_rates = IEEE80211_TX_MAX_RATES;
746 	hw->max_rate_tries = 18;   /* 9 rates * 2 retries/rate */
747 
748 	skb_queue_head_init(&mac->ack_wait_queue);
749 	mac->ack_pending = 0;
750 
751 	plfxlc_chip_init(&mac->chip, hw, intf);
752 
753 	SET_IEEE80211_DEV(hw, &intf->dev);
754 	return hw;
755 }
756