1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (c) 2021 pureLiFi
4  */
5 
6 #ifndef PLFXLC_MAC_H
7 #define PLFXLC_MAC_H
8 
9 #include <linux/kernel.h>
10 #include <net/mac80211.h>
11 
12 #include "chip.h"
13 
14 #define PURELIFI_CCK                  0x00
15 #define PURELIFI_OFDM                 0x10
16 #define PURELIFI_CCK_PREA_SHORT       0x20
17 
18 #define PURELIFI_OFDM_PLCP_RATE_6M	0xb
19 #define PURELIFI_OFDM_PLCP_RATE_9M	0xf
20 #define PURELIFI_OFDM_PLCP_RATE_12M	0xa
21 #define PURELIFI_OFDM_PLCP_RATE_18M	0xe
22 #define PURELIFI_OFDM_PLCP_RATE_24M	0x9
23 #define PURELIFI_OFDM_PLCP_RATE_36M	0xd
24 #define PURELIFI_OFDM_PLCP_RATE_48M	0x8
25 #define PURELIFI_OFDM_PLCP_RATE_54M	0xc
26 
27 #define PURELIFI_CCK_RATE_1M	(PURELIFI_CCK | 0x00)
28 #define PURELIFI_CCK_RATE_2M	(PURELIFI_CCK | 0x01)
29 #define PURELIFI_CCK_RATE_5_5M	(PURELIFI_CCK | 0x02)
30 #define PURELIFI_CCK_RATE_11M	(PURELIFI_CCK | 0x03)
31 #define PURELIFI_OFDM_RATE_6M	(PURELIFI_OFDM | PURELIFI_OFDM_PLCP_RATE_6M)
32 #define PURELIFI_OFDM_RATE_9M	(PURELIFI_OFDM | PURELIFI_OFDM_PLCP_RATE_9M)
33 #define PURELIFI_OFDM_RATE_12M	(PURELIFI_OFDM | PURELIFI_OFDM_PLCP_RATE_12M)
34 #define PURELIFI_OFDM_RATE_18M	(PURELIFI_OFDM | PURELIFI_OFDM_PLCP_RATE_18M)
35 #define PURELIFI_OFDM_RATE_24M	(PURELIFI_OFDM | PURELIFI_OFDM_PLCP_RATE_24M)
36 #define PURELIFI_OFDM_RATE_36M	(PURELIFI_OFDM | PURELIFI_OFDM_PLCP_RATE_36M)
37 #define PURELIFI_OFDM_RATE_48M	(PURELIFI_OFDM | PURELIFI_OFDM_PLCP_RATE_48M)
38 #define PURELIFI_OFDM_RATE_54M	(PURELIFI_OFDM | PURELIFI_OFDM_PLCP_RATE_54M)
39 
40 #define PURELIFI_RX_ERROR		0x80
41 #define PURELIFI_RX_CRC32_ERROR		0x10
42 
43 #define PLF_REGDOMAIN_FCC	0x10
44 #define PLF_REGDOMAIN_IC	0x20
45 #define PLF_REGDOMAIN_ETSI	0x30
46 #define PLF_REGDOMAIN_SPAIN	0x31
47 #define PLF_REGDOMAIN_FRANCE	0x32
48 #define PLF_REGDOMAIN_JAPAN_2	0x40
49 #define PLF_REGDOMAIN_JAPAN	0x41
50 #define PLF_REGDOMAIN_JAPAN_3	0x49
51 
52 #define PLF_RX_ERROR		0x80
53 #define PLF_RX_CRC32_ERROR	0x10
54 
55 enum {
56 	MODULATION_RATE_BPSK_1_2 = 0,
57 	MODULATION_RATE_BPSK_3_4,
58 	MODULATION_RATE_QPSK_1_2,
59 	MODULATION_RATE_QPSK_3_4,
60 	MODULATION_RATE_QAM16_1_2,
61 	MODULATION_RATE_QAM16_3_4,
62 	MODULATION_RATE_QAM64_1_2,
63 	MODULATION_RATE_QAM64_3_4,
64 	MODULATION_RATE_AUTO,
65 	MODULATION_RATE_NUM
66 };
67 
68 #define plfxlc_mac_dev(mac) plfxlc_chip_dev(&(mac)->chip)
69 
70 #define PURELIFI_MAC_STATS_BUFFER_SIZE 16
71 #define PURELIFI_MAC_MAX_ACK_WAITERS 50
72 
73 struct plfxlc_ctrlset {
74 	/* id should be plf_usb_req_enum */
75 	__be32		id;
76 	__be32		len;
77 	u8		modulation;
78 	u8		control;
79 	u8		service;
80 	u8		pad;
81 	__le16		packet_length;
82 	__le16		current_length;
83 	__le16		next_frame_length;
84 	__le16		tx_length;
85 	__be32		payload_len_nw;
86 } __packed;
87 
88 /* overlay */
89 struct plfxlc_header {
90 	struct plfxlc_ctrlset plf_ctrl;
91 	u32    frametype;
92 	u8    *dmac;
93 } __packed;
94 
95 struct tx_status {
96 	u8 type;
97 	u8 id;
98 	u8 rate;
99 	u8 pad;
100 	u8 mac[ETH_ALEN];
101 	u8 retry;
102 	u8 failure;
103 } __packed;
104 
105 struct beacon {
106 	struct delayed_work watchdog_work;
107 	struct sk_buff *cur_beacon;
108 	unsigned long last_update;
109 	u16 interval;
110 	u8 period;
111 };
112 
113 enum plfxlc_device_flags {
114 	PURELIFI_DEVICE_RUNNING,
115 };
116 
117 struct plfxlc_mac {
118 	struct ieee80211_hw *hw;
119 	struct ieee80211_vif *vif;
120 	struct beacon beacon;
121 	struct work_struct set_rts_cts_work;
122 	struct work_struct process_intr;
123 	struct plfxlc_mc_hash multicast_hash;
124 	struct sk_buff_head ack_wait_queue;
125 	struct ieee80211_channel channels[14];
126 	struct ieee80211_rate rates[12];
127 	struct ieee80211_supported_band band;
128 	struct plfxlc_chip chip;
129 	spinlock_t lock; /* lock for mac data */
130 	u8 intr_buffer[USB_MAX_EP_INT_BUFFER];
131 	char serial_number[PURELIFI_SERIAL_LEN];
132 	unsigned char hw_address[ETH_ALEN];
133 	u8 default_regdomain;
134 	unsigned long flags;
135 	bool pass_failed_fcs;
136 	bool pass_ctrl;
137 	bool ack_pending;
138 	int ack_signal;
139 	int associated;
140 	u8 regdomain;
141 	u8 channel;
142 	int type;
143 	u64 crc_errors;
144 	u64 rssi;
145 };
146 
147 static inline struct plfxlc_mac *
148 plfxlc_hw_mac(struct ieee80211_hw *hw)
149 {
150 	return hw->priv;
151 }
152 
153 static inline struct plfxlc_mac *
154 plfxlc_chip_to_mac(struct plfxlc_chip *chip)
155 {
156 	return container_of(chip, struct plfxlc_mac, chip);
157 }
158 
159 static inline struct plfxlc_mac *
160 plfxlc_usb_to_mac(struct plfxlc_usb *usb)
161 {
162 	return plfxlc_chip_to_mac(plfxlc_usb_to_chip(usb));
163 }
164 
165 static inline u8 *plfxlc_mac_get_perm_addr(struct plfxlc_mac *mac)
166 {
167 	return mac->hw->wiphy->perm_addr;
168 }
169 
170 struct ieee80211_hw *plfxlc_mac_alloc_hw(struct usb_interface *intf);
171 void plfxlc_mac_release(struct plfxlc_mac *mac);
172 
173 int plfxlc_mac_preinit_hw(struct ieee80211_hw *hw, const u8 *hw_address);
174 int plfxlc_mac_init_hw(struct ieee80211_hw *hw);
175 
176 int plfxlc_mac_rx(struct ieee80211_hw *hw, const u8 *buffer,
177 		  unsigned int length);
178 void plfxlc_mac_tx_failed(struct urb *urb);
179 void plfxlc_mac_tx_to_dev(struct sk_buff *skb, int error);
180 int plfxlc_op_start(struct ieee80211_hw *hw);
181 void plfxlc_op_stop(struct ieee80211_hw *hw);
182 int plfxlc_restore_settings(struct plfxlc_mac *mac);
183 
184 #endif /* PLFXLC_MAC_H */
185