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