1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  *   Driver for KeyStream IEEE802.11 b/g wireless LAN cards.
4  *
5  *   Copyright (C) 2006-2008 KeyStream Corp.
6  *   Copyright (C) 2009 Renesas Technology Corp.
7  */
8 
9 #ifndef _KS_WLAN_H
10 #define _KS_WLAN_H
11 
12 #include <linux/atomic.h>
13 #include <linux/circ_buf.h>
14 #include <linux/completion.h>
15 #include <linux/netdevice.h>
16 #include <linux/sched.h>
17 #include <linux/spinlock.h>
18 #include <linux/wireless.h>
19 
20 struct ks_wlan_parameter {
21 	u8 operation_mode;
22 	u8 channel;
23 	u8 tx_rate;
24 	struct {
25 		u8 size;
26 		u8 body[16];
27 	} rate_set;
28 	u8 bssid[ETH_ALEN];
29 	struct {
30 		u8 size;
31 		u8 body[32 + 1];
32 	} ssid;
33 	u8 preamble;
34 	u8 power_mgmt;
35 	u32 scan_type;
36 #define BEACON_LOST_COUNT_MAX 65535
37 	u32 beacon_lost_count;
38 	u32 rts;
39 	u32 fragment;
40 	u32 privacy_invoked;
41 	u32 wep_index;
42 	struct {
43 		u8 size;
44 		u8 val[13 * 2 + 1];
45 	} wep_key[4];
46 	u16 authenticate_type;
47 	u16 phy_type;
48 	u16 cts_mode;
49 	u16 phy_info_timer;
50 };
51 
52 enum {
53 	DEVICE_STATE_OFF = 0,	/* this means hw_unavailable is != 0 */
54 	DEVICE_STATE_PREBOOT,	/* we are in a pre-boot state (empty RAM) */
55 	DEVICE_STATE_BOOT,	/* boot state (fw upload, run fw) */
56 	DEVICE_STATE_PREINIT,	/* pre-init state */
57 	DEVICE_STATE_INIT,	/* init state (restore MIB backup to device) */
58 	DEVICE_STATE_READY,	/* driver&device are in operational state */
59 	DEVICE_STATE_SLEEP	/* device in sleep mode */
60 };
61 
62 /* SME flag */
63 #define SME_MODE_SET	    BIT(0)
64 #define SME_RTS             BIT(1)
65 #define SME_FRAG            BIT(2)
66 #define SME_WEP_FLAG        BIT(3)
67 #define SME_WEP_INDEX       BIT(4)
68 #define SME_WEP_VAL1        BIT(5)
69 #define SME_WEP_VAL2        BIT(6)
70 #define SME_WEP_VAL3        BIT(7)
71 #define SME_WEP_VAL4        BIT(8)
72 #define SME_WEP_VAL_MASK    GENMASK(8, 5)
73 #define SME_RSN             BIT(9)
74 #define SME_RSN_MULTICAST   BIT(10)
75 #define SME_RSN_UNICAST	    BIT(11)
76 #define SME_RSN_AUTH	    BIT(12)
77 
78 #define SME_AP_SCAN         BIT(13)
79 #define SME_MULTICAST       BIT(14)
80 
81 /* SME Event */
82 enum {
83 	SME_START,
84 
85 	SME_MULTICAST_REQUEST,
86 	SME_MACADDRESS_SET_REQUEST,
87 	SME_BSS_SCAN_REQUEST,
88 	SME_SET_FLAG,
89 	SME_SET_TXKEY,
90 	SME_SET_KEY1,
91 	SME_SET_KEY2,
92 	SME_SET_KEY3,
93 	SME_SET_KEY4,
94 	SME_SET_PMK_TSC,
95 	SME_SET_GMK1_TSC,
96 	SME_SET_GMK2_TSC,
97 	SME_SET_GMK3_TSC,
98 	SME_SET_PMKSA,
99 	SME_POW_MNGMT_REQUEST,
100 	SME_PHY_INFO_REQUEST,
101 	SME_MIC_FAILURE_REQUEST,
102 	SME_GET_MAC_ADDRESS,
103 	SME_GET_PRODUCT_VERSION,
104 	SME_STOP_REQUEST,
105 	SME_RTS_THRESHOLD_REQUEST,
106 	SME_FRAGMENTATION_THRESHOLD_REQUEST,
107 	SME_WEP_INDEX_REQUEST,
108 	SME_WEP_KEY1_REQUEST,
109 	SME_WEP_KEY2_REQUEST,
110 	SME_WEP_KEY3_REQUEST,
111 	SME_WEP_KEY4_REQUEST,
112 	SME_WEP_FLAG_REQUEST,
113 	SME_RSN_UCAST_REQUEST,
114 	SME_RSN_MCAST_REQUEST,
115 	SME_RSN_AUTH_REQUEST,
116 	SME_RSN_ENABLED_REQUEST,
117 	SME_RSN_MODE_REQUEST,
118 	SME_WPS_ENABLE_REQUEST,
119 	SME_WPS_PROBE_REQUEST,
120 	SME_SET_GAIN,
121 	SME_GET_GAIN,
122 	SME_SLEEP_REQUEST,
123 	SME_SET_REGION,
124 	SME_MODE_SET_REQUEST,
125 	SME_START_REQUEST,
126 	SME_GET_EEPROM_CKSUM,
127 
128 	SME_MIC_FAILURE_CONFIRM,
129 	SME_START_CONFIRM,
130 
131 	SME_MULTICAST_CONFIRM,
132 	SME_BSS_SCAN_CONFIRM,
133 	SME_GET_CURRENT_AP,
134 	SME_POW_MNGMT_CONFIRM,
135 	SME_PHY_INFO_CONFIRM,
136 	SME_STOP_CONFIRM,
137 	SME_RTS_THRESHOLD_CONFIRM,
138 	SME_FRAGMENTATION_THRESHOLD_CONFIRM,
139 	SME_WEP_INDEX_CONFIRM,
140 	SME_WEP_KEY1_CONFIRM,
141 	SME_WEP_KEY2_CONFIRM,
142 	SME_WEP_KEY3_CONFIRM,
143 	SME_WEP_KEY4_CONFIRM,
144 	SME_WEP_FLAG_CONFIRM,
145 	SME_RSN_UCAST_CONFIRM,
146 	SME_RSN_MCAST_CONFIRM,
147 	SME_RSN_AUTH_CONFIRM,
148 	SME_RSN_ENABLED_CONFIRM,
149 	SME_RSN_MODE_CONFIRM,
150 	SME_MODE_SET_CONFIRM,
151 	SME_SLEEP_CONFIRM,
152 
153 	SME_RSN_SET_CONFIRM,
154 	SME_WEP_SET_CONFIRM,
155 	SME_TERMINATE,
156 
157 	SME_EVENT_SIZE
158 };
159 
160 /* SME Status */
161 enum {
162 	SME_IDLE,
163 	SME_SETUP,
164 	SME_DISCONNECT,
165 	SME_CONNECT
166 };
167 
168 #define	SME_EVENT_BUFF_SIZE	128
169 
170 struct sme_info {
171 	int sme_status;
172 	int event_buff[SME_EVENT_BUFF_SIZE];
173 	unsigned int qhead;
174 	unsigned int qtail;
175 	spinlock_t sme_spin;
176 	unsigned long sme_flag;
177 };
178 
179 struct hostt {
180 	int buff[SME_EVENT_BUFF_SIZE];
181 	unsigned int qhead;
182 	unsigned int qtail;
183 };
184 
185 #define RSN_IE_BODY_MAX 64
186 struct rsn_ie {
187 	u8 id;	/* 0xdd = WPA or 0x30 = RSN */
188 	u8 size;	/* max ? 255 ? */
189 	u8 body[RSN_IE_BODY_MAX];
190 } __packed;
191 
192 #define WPA_INFO_ELEM_ID 0xdd
193 #define RSN_INFO_ELEM_ID 0x30
194 
195 #define WPS_IE_BODY_MAX 255
196 struct wps_ie {
197 	u8 id;	/* 221 'dd <len> 00 50 F2 04' */
198 	u8 size;	/* max ? 255 ? */
199 	u8 body[WPS_IE_BODY_MAX];
200 } __packed;
201 
202 struct local_ap {
203 	u8 bssid[6];
204 	u8 rssi;
205 	u8 sq;
206 	struct {
207 		u8 size;
208 		u8 body[32];
209 		u8 ssid_pad;
210 	} ssid;
211 	struct {
212 		u8 size;
213 		u8 body[16];
214 		u8 rate_pad;
215 	} rate_set;
216 	u16 capability;
217 	u8 channel;
218 	u8 noise;
219 	struct rsn_ie wpa_ie;
220 	struct rsn_ie rsn_ie;
221 	struct wps_ie wps_ie;
222 };
223 
224 #define LOCAL_APLIST_MAX 31
225 #define LOCAL_CURRENT_AP LOCAL_APLIST_MAX
226 struct local_aplist {
227 	int size;
228 	struct local_ap ap[LOCAL_APLIST_MAX + 1];
229 };
230 
231 struct local_gain {
232 	u8 tx_mode;
233 	u8 rx_mode;
234 	u8 tx_gain;
235 	u8 rx_gain;
236 };
237 
238 struct local_eeprom_sum {
239 	u8 type;
240 	u8 result;
241 };
242 
243 enum {
244 	EEPROM_OK,
245 	EEPROM_CHECKSUM_NONE,
246 	EEPROM_FW_NOT_SUPPORT,
247 	EEPROM_NG,
248 };
249 
250 /* Power Save Status */
251 enum {
252 	PS_NONE,
253 	PS_ACTIVE_SET,
254 	PS_SAVE_SET,
255 	PS_CONF_WAIT,
256 	PS_SNOOZE,
257 	PS_WAKEUP
258 };
259 
260 struct power_save_status {
261 	atomic_t status;	/* initialvalue 0 */
262 	struct completion wakeup_wait;
263 	atomic_t confirm_wait;
264 	atomic_t snooze_guard;
265 };
266 
267 struct sleep_status {
268 	atomic_t status;	/* initialvalue 0 */
269 	atomic_t doze_request;
270 	atomic_t wakeup_request;
271 };
272 
273 /* WPA */
274 struct scan_ext {
275 	unsigned int flag;
276 	char ssid[IW_ESSID_MAX_SIZE + 1];
277 };
278 
279 #define CIPHER_ID_WPA_NONE    "\x00\x50\xf2\x00"
280 #define CIPHER_ID_WPA_WEP40   "\x00\x50\xf2\x01"
281 #define CIPHER_ID_WPA_TKIP    "\x00\x50\xf2\x02"
282 #define CIPHER_ID_WPA_CCMP    "\x00\x50\xf2\x04"
283 #define CIPHER_ID_WPA_WEP104  "\x00\x50\xf2\x05"
284 
285 #define CIPHER_ID_WPA2_NONE   "\x00\x0f\xac\x00"
286 #define CIPHER_ID_WPA2_WEP40  "\x00\x0f\xac\x01"
287 #define CIPHER_ID_WPA2_TKIP   "\x00\x0f\xac\x02"
288 #define CIPHER_ID_WPA2_CCMP   "\x00\x0f\xac\x04"
289 #define CIPHER_ID_WPA2_WEP104 "\x00\x0f\xac\x05"
290 
291 #define CIPHER_ID_LEN    4
292 
293 enum {
294 	KEY_MGMT_802_1X,
295 	KEY_MGMT_PSK,
296 	KEY_MGMT_WPANONE,
297 };
298 
299 #define KEY_MGMT_ID_WPA_NONE     "\x00\x50\xf2\x00"
300 #define KEY_MGMT_ID_WPA_1X       "\x00\x50\xf2\x01"
301 #define KEY_MGMT_ID_WPA_PSK      "\x00\x50\xf2\x02"
302 #define KEY_MGMT_ID_WPA_WPANONE  "\x00\x50\xf2\xff"
303 
304 #define KEY_MGMT_ID_WPA2_NONE    "\x00\x0f\xac\x00"
305 #define KEY_MGMT_ID_WPA2_1X      "\x00\x0f\xac\x01"
306 #define KEY_MGMT_ID_WPA2_PSK     "\x00\x0f\xac\x02"
307 #define KEY_MGMT_ID_WPA2_WPANONE "\x00\x0f\xac\xff"
308 
309 #define KEY_MGMT_ID_LEN  4
310 
311 #define MIC_KEY_SIZE 8
312 
313 struct wpa_key {
314 	u32 ext_flags;	/* IW_ENCODE_EXT_xxx */
315 	u8 tx_seq[IW_ENCODE_SEQ_MAX_SIZE];	/* LSB first */
316 	u8 rx_seq[IW_ENCODE_SEQ_MAX_SIZE];	/* LSB first */
317 	struct sockaddr addr;	/* ff:ff:ff:ff:ff:ff for broadcast/multicast
318 				 * (group) keys or unicast address for
319 				 * individual keys
320 				 */
321 	u16 alg;
322 	u16 key_len;	/* WEP: 5 or 13, TKIP: 32, CCMP: 16 */
323 	u8 key_val[IW_ENCODING_TOKEN_MAX];
324 	u8 tx_mic_key[MIC_KEY_SIZE];
325 	u8 rx_mic_key[MIC_KEY_SIZE];
326 };
327 
328 #define WPA_KEY_INDEX_MAX 4
329 #define WPA_RX_SEQ_LEN 6
330 
331 struct mic_failure {
332 	u16 failure;	/* MIC Failure counter 0 or 1 or 2 */
333 	u16 counter;	/* 1sec counter 0-60 */
334 	u32 last_failure_time;
335 	int stop;
336 };
337 
338 struct wpa_status {
339 	int wpa_enabled;
340 	bool rsn_enabled;
341 	int version;
342 	int pairwise_suite;	/* unicast cipher */
343 	int group_suite;	/* multicast cipher */
344 	int key_mgmt_suite;
345 	int auth_alg;
346 	int txkey;
347 	struct wpa_key key[WPA_KEY_INDEX_MAX];
348 	struct scan_ext scan_ext;
349 	struct mic_failure mic_failure;
350 };
351 
352 #include <linux/list.h>
353 #define PMK_LIST_MAX 8
354 struct pmk_list {
355 	u16 size;
356 	struct list_head head;
357 	struct pmk {
358 		struct list_head list;
359 		u8 bssid[ETH_ALEN];
360 		u8 pmkid[IW_PMKID_LEN];
361 	} pmk[PMK_LIST_MAX];
362 };
363 
364 struct wps_status {
365 	int wps_enabled;
366 	int ielen;
367 	u8 ie[255];
368 };
369 
370 /* Tx Device struct */
371 #define	TX_DEVICE_BUFF_SIZE	1024
372 
373 struct ks_wlan_private;
374 
375 /**
376  * struct tx_device_buffer - Queue item for the tx queue.
377  * @sendp: Pointer to the send request data.
378  * @size: Size of @sendp data.
379  * @complete_handler: Function called once data write to device is complete.
380  * @arg1: First argument to @complete_handler.
381  * @arg2: Second argument to @complete_handler.
382  */
383 struct tx_device_buffer {
384 	unsigned char *sendp;
385 	unsigned int size;
386 	void (*complete_handler)(struct ks_wlan_private *priv,
387 				 struct sk_buff *skb);
388 	struct sk_buff *skb;
389 };
390 
391 /**
392  * struct tx_device - Tx buffer queue.
393  * @tx_device_buffer: Queue buffer.
394  * @qhead: Head of tx queue.
395  * @qtail: Tail of tx queue.
396  * @tx_dev_lock: Queue lock.
397  */
398 struct tx_device {
399 	struct tx_device_buffer tx_dev_buff[TX_DEVICE_BUFF_SIZE];
400 	unsigned int qhead;
401 	unsigned int qtail;
402 	spinlock_t tx_dev_lock;	/* protect access to the queue */
403 };
404 
405 /* Rx Device struct */
406 #define	RX_DATA_SIZE	(2 + 2 + 2347 + 1)
407 #define	RX_DEVICE_BUFF_SIZE	32
408 
409 /**
410  * struct rx_device_buffer - Queue item for the rx queue.
411  * @data: rx data.
412  * @size: Size of @data.
413  */
414 struct rx_device_buffer {
415 	unsigned char data[RX_DATA_SIZE];
416 	unsigned int size;
417 };
418 
419 /**
420  * struct rx_device - Rx buffer queue.
421  * @rx_device_buffer: Queue buffer.
422  * @qhead: Head of rx queue.
423  * @qtail: Tail of rx queue.
424  * @rx_dev_lock: Queue lock.
425  */
426 struct rx_device {
427 	struct rx_device_buffer rx_dev_buff[RX_DEVICE_BUFF_SIZE];
428 	unsigned int qhead;
429 	unsigned int qtail;
430 	spinlock_t rx_dev_lock;	/* protect access to the queue */
431 };
432 
433 struct ks_wlan_private {
434 	/* hardware information */
435 	void *if_hw;
436 	struct workqueue_struct *wq;
437 	struct delayed_work rw_dwork;
438 	struct tasklet_struct rx_bh_task;
439 
440 	struct net_device *net_dev;
441 	struct net_device_stats nstats;
442 	struct iw_statistics wstats;
443 
444 	struct completion confirm_wait;
445 
446 	/* trx device & sme */
447 	struct tx_device tx_dev;
448 	struct rx_device rx_dev;
449 	struct sme_info sme_i;
450 	u8 *rxp;
451 	unsigned int rx_size;
452 	struct tasklet_struct sme_task;
453 	struct work_struct wakeup_work;
454 	int scan_ind_count;
455 
456 	unsigned char eth_addr[ETH_ALEN];
457 
458 	struct local_aplist aplist;
459 	struct local_ap current_ap;
460 	struct power_save_status psstatus;
461 	struct sleep_status sleepstatus;
462 	struct wpa_status wpa;
463 	struct pmk_list pmklist;
464 	/* wireless parameter */
465 	struct ks_wlan_parameter reg;
466 	u8 current_rate;
467 
468 	char nick[IW_ESSID_MAX_SIZE + 1];
469 
470 	spinlock_t multicast_spin;
471 
472 	spinlock_t dev_read_lock;
473 	wait_queue_head_t devread_wait;
474 
475 	unsigned int need_commit;	/* for ioctl */
476 
477 	/* DeviceIoControl */
478 	bool is_device_open;
479 	atomic_t event_count;
480 	atomic_t rec_count;
481 	int dev_count;
482 #define DEVICE_STOCK_COUNT 20
483 	unsigned char *dev_data[DEVICE_STOCK_COUNT];
484 	int dev_size[DEVICE_STOCK_COUNT];
485 
486 	/* ioctl : IOCTL_FIRMWARE_VERSION */
487 	unsigned char firmware_version[128 + 1];
488 	int version_size;
489 
490 	bool mac_address_valid;
491 
492 	int dev_state;
493 
494 	struct sk_buff *skb;
495 	unsigned int cur_rx;	/* Index into the Rx buffer of next Rx pkt. */
496 #define FORCE_DISCONNECT    0x80000000
497 #define CONNECT_STATUS_MASK 0x7FFFFFFF
498 	u32 connect_status;
499 	int infra_status;
500 	u8 scan_ssid_len;
501 	u8 scan_ssid[IW_ESSID_MAX_SIZE + 1];
502 	struct local_gain gain;
503 	struct wps_status wps;
504 	u8 sleep_mode;
505 
506 	u8 region;
507 	struct local_eeprom_sum eeprom_sum;
508 	u8 eeprom_checksum;
509 
510 	struct hostt hostt;
511 
512 	unsigned long last_doze;
513 	unsigned long last_wakeup;
514 
515 	unsigned int wakeup_count;	/* for detect wakeup loop */
516 };
517 
inc_txqhead(struct ks_wlan_private * priv)518 static inline void inc_txqhead(struct ks_wlan_private *priv)
519 {
520 	priv->tx_dev.qhead = (priv->tx_dev.qhead + 1) % TX_DEVICE_BUFF_SIZE;
521 }
522 
inc_txqtail(struct ks_wlan_private * priv)523 static inline void inc_txqtail(struct ks_wlan_private *priv)
524 {
525 	priv->tx_dev.qtail = (priv->tx_dev.qtail + 1) % TX_DEVICE_BUFF_SIZE;
526 }
527 
txq_has_space(struct ks_wlan_private * priv)528 static inline bool txq_has_space(struct ks_wlan_private *priv)
529 {
530 	return (CIRC_SPACE(priv->tx_dev.qhead, priv->tx_dev.qtail,
531 			   TX_DEVICE_BUFF_SIZE) > 0);
532 }
533 
inc_rxqhead(struct ks_wlan_private * priv)534 static inline void inc_rxqhead(struct ks_wlan_private *priv)
535 {
536 	priv->rx_dev.qhead = (priv->rx_dev.qhead + 1) % RX_DEVICE_BUFF_SIZE;
537 }
538 
inc_rxqtail(struct ks_wlan_private * priv)539 static inline void inc_rxqtail(struct ks_wlan_private *priv)
540 {
541 	priv->rx_dev.qtail = (priv->rx_dev.qtail + 1) % RX_DEVICE_BUFF_SIZE;
542 }
543 
rxq_has_space(struct ks_wlan_private * priv)544 static inline bool rxq_has_space(struct ks_wlan_private *priv)
545 {
546 	return (CIRC_SPACE(priv->rx_dev.qhead, priv->rx_dev.qtail,
547 			   RX_DEVICE_BUFF_SIZE) > 0);
548 }
549 
txq_count(struct ks_wlan_private * priv)550 static inline unsigned int txq_count(struct ks_wlan_private *priv)
551 {
552 	return CIRC_CNT_TO_END(priv->tx_dev.qhead, priv->tx_dev.qtail,
553 			       TX_DEVICE_BUFF_SIZE);
554 }
555 
rxq_count(struct ks_wlan_private * priv)556 static inline unsigned int rxq_count(struct ks_wlan_private *priv)
557 {
558 	return CIRC_CNT_TO_END(priv->rx_dev.qhead, priv->rx_dev.qtail,
559 			       RX_DEVICE_BUFF_SIZE);
560 }
561 
562 int ks_wlan_net_start(struct net_device *dev);
563 int ks_wlan_net_stop(struct net_device *dev);
564 bool is_connect_status(u32 status);
565 bool is_disconnect_status(u32 status);
566 
567 #endif /* _KS_WLAN_H */
568