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